update README
[platform/upstream/crda.git] / dump.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/mman.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <arpa/inet.h> /* ntohl */
9
10 #include "regdb.h"
11
12 #ifdef VERIFY_SIGNATURE
13
14 #ifdef USE_OPENSSL
15 #include <openssl/objects.h>
16 #include <openssl/bn.h>
17 #include <openssl/rsa.h>
18 #include <openssl/sha.h>
19
20 #include "keys-ssl.c"
21 #endif
22
23 #ifdef USE_GCRYPT
24 #include <gcrypt.h>
25
26 #include "keys-gcrypt.c"
27 #endif
28
29 #endif
30
31 static void *get_file_ptr(__u8 *db, int dblen, int structlen, __be32 ptr)
32 {
33         __u32 p = ntohl(ptr);
34
35         if (p > dblen - structlen) {
36                 fprintf(stderr, "Invalid database file, bad pointer!\n");
37                 exit(3);
38         }
39
40         return (void *)(db + p);
41 }
42
43 static void print_reg_rule(__u8 *db, int dblen, __be32 ruleptr)
44 {
45         struct regdb_file_reg_rule *rule;
46         struct regdb_file_freq_range *freq;
47         struct regdb_file_power_rule *power;
48         char *ofdm = "", *cck = "", env[3] = { 'O', 'I', '\0' };
49
50         rule = get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
51         freq = get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
52         power = get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
53
54         if (ntohl(freq->modulation_cap) & 2)
55                 ofdm = ", OFDM";
56         if (ntohl(freq->modulation_cap) & 1)
57                 cck = ", CCK";
58
59         printf("\t(%.3f - %.3f @ %.3f%s%s), ",
60                (float)ntohl(freq->start_freq)/1000.0,
61                (float)ntohl(freq->end_freq)/1000.0,
62                (float)ntohl(freq->max_bandwidth)/1000.0,
63                cck, ofdm);
64
65         if (power->environment_cap != ' ') {
66                 env[0] = power->environment_cap;
67                 env[1] = '\0';
68         }
69
70         printf("(%s, %.3f, %.3f, %.3f, %.3f, %.3f)\n",
71                env,
72                (float)ntohl(power->max_antenna_gain/100.0),
73                (float)ntohl(power->max_ir_ptmp/100.0),
74                (float)ntohl(power->max_ir_ptp/100.0),
75                (float)ntohl(power->max_eirp_pmtp/100.0),
76                (float)ntohl(power->max_eirp_ptp)/100.0);
77 }
78
79 int main(int argc, char **argv)
80 {
81         int fd;
82         struct stat stat;
83         __u8 *db;
84         struct regdb_file_header *header;
85         struct regdb_file_reg_country *countries;
86         int dblen, siglen, num_countries, i, j;
87 #ifdef USE_OPENSSL
88         RSA *rsa;
89         __u8 hash[SHA_DIGEST_LENGTH];
90         int ok = 0;
91 #endif
92 #ifdef USE_GCRYPT
93         gcry_mpi_t mpi_e, mpi_n;
94         gcry_sexp_t rsa, signature, data;
95         __u8 hash[20];
96         int ok = 0;
97 #endif
98
99         if (argc != 2) {
100                 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
101                 return 2;
102         }
103
104         fd = open(argv[1], O_RDONLY);
105         if (fd < 0) {
106                 perror("failed to open db file");
107                 return 2;
108         }
109
110         if (fstat(fd, &stat)) {
111                 perror("failed to fstat db file");
112                 return 2;
113         }
114
115         dblen = stat.st_size;
116
117         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
118         if (db == MAP_FAILED) {
119                 perror("failed to mmap db file");
120                 return 2;
121         }
122
123         header = get_file_ptr(db, dblen, sizeof(*header), 0);
124
125         if (ntohl(header->magic) != REGDB_MAGIC) {
126                 fprintf(stderr, "Invalid database magic\n");
127                 return 2;
128         }
129
130         if (ntohl(header->version) != REGDB_VERSION) {
131                 fprintf(stderr, "Invalid database version\n");
132                 return 2;
133         }
134
135         siglen = ntohl(header->signature_length);
136         /* adjust dblen so later sanity checks don't run into the signature */
137         dblen -= siglen;
138
139         if (dblen <= sizeof(*header)) {
140                 fprintf(stderr, "Invalid signature length %d\n", siglen);
141                 return 2;
142         }
143
144         /* verify signature */
145 #ifdef USE_OPENSSL
146         rsa = RSA_new();
147         if (!rsa) {
148                 fprintf(stderr, "Failed to create RSA key\n");
149                 return 2;
150         }
151
152         if (SHA1(db, dblen, hash) != hash) {
153                 fprintf(stderr, "Failed to calculate SHA sum\n");
154                 return 2;
155         }
156
157         for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
158                 rsa->e = &keys[i].e;
159                 rsa->n = &keys[i].n;
160
161                 if (RSA_size(rsa) != siglen)
162                         continue;
163
164                 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
165                                 db + dblen, siglen, rsa) == 1;
166                 if (ok)
167                         break;
168         }
169
170         if (!ok) {
171                 fprintf(stderr, "Database signature wrong\n");
172                 return 2;
173         }
174
175         rsa->e = NULL;
176         rsa->n = NULL;
177         RSA_free(rsa);
178
179         BN_print_fp(stdout, &keys[0].n);
180
181         return 0;
182 #endif
183
184 #ifdef USE_GCRYPT
185         /* hash the db */
186         gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
187
188         if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
189                             20, hash)) {
190                 fprintf(stderr, "failed to build data expression\n");
191                 return 2;
192         }
193
194         if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
195                             siglen, db + dblen)) {
196                 fprintf(stderr, "failed to build signature expression\n");
197                 return 2;
198         }
199
200         for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
201                 if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
202                                   keys[0].e, keys[0].len_e, NULL) ||
203                     gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
204                                   keys[0].n, keys[0].len_n, NULL)) {
205                         fprintf(stderr, "failed to convert numbers\n");
206                         return 2;
207                 }
208
209                 if (gcry_sexp_build(&rsa, NULL,
210                                     "(public-key (rsa (n %m) (e %m)))",
211                                     mpi_n, mpi_e)) {
212                         fprintf(stderr, "failed to build rsa key\n");
213                         return 2;
214                 }
215
216                 if (!gcry_pk_verify(signature, data, rsa)) {
217                         ok = 1;
218                         break;
219                 }
220         }
221
222         if (!ok) {
223                 fprintf(stderr, "Database signature wrong\n");
224                 return 2;
225         }
226
227 #endif
228
229         num_countries = ntohl(header->reg_country_num);
230         countries = get_file_ptr(db, dblen,
231                                  sizeof(struct regdb_file_reg_country) * num_countries,
232                                  header->reg_country_ptr);
233
234         for (i = 0; i < num_countries; i++) {
235                 struct regdb_file_reg_rules_collection *rcoll;
236                 struct regdb_file_reg_country *country = countries + i;
237                 int num_rules;
238
239                 printf("country %.2s:\n", country->alpha2);
240                 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
241                 num_rules = ntohl(rcoll->reg_rule_num);
242                 /* re-get pointer with sanity checking for num_rules */
243                 rcoll = get_file_ptr(db, dblen,
244                                      sizeof(*rcoll) + num_rules * sizeof(__be32),
245                                      country->reg_collection_ptr);
246                 for (j = 0; j < num_rules; j++)
247                         print_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j]);
248                 printf("\n");
249         }
250
251         return 0;
252 }