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