add NO-HT20, NO-HT40 flags
[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         __u32 flags;
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         printf("\t(%.3f - %.3f @ %.3f), ",
51                ((float)ntohl(freq->start_freq))/1000.0,
52                ((float)ntohl(freq->end_freq))/1000.0,
53                ((float)ntohl(freq->max_bandwidth))/1000.0);
54
55         printf("(");
56
57         if (power->max_antenna_gain)
58                 printf("%.2f, ", ((float)ntohl(power->max_antenna_gain)/100.0));
59         else
60                 printf("N/A, ");
61
62         if (power->max_eirp)
63                 printf("%.2f)", ((float)ntohl(power->max_eirp)/100.0));
64         else
65                 printf("N/A)");
66
67         flags = ntohl(rule->flags);
68         if (flags & RRF_NO_OFDM)
69                 printf(", NO-OFDM");
70         if (flags & RRF_NO_CCK)
71                 printf(", NO-CCK");
72         if (flags & RRF_NO_INDOOR)
73                 printf(", NO-INDOOR");
74         if (flags & RRF_NO_OUTDOOR)
75                 printf(", NO-OUTDOOR");
76         if (flags & RRF_DFS)
77                 printf(", DFS");
78         if (flags & RRF_PTP_ONLY)
79                 printf(", PTP-ONLY");
80         if (flags & RRF_PTMP_ONLY)
81                 printf(", PTMP-ONLY");
82         if (flags & RRF_PASSIVE_SCAN)
83                 printf(", PASSIVE-SCAN");
84         if (flags & RRF_NO_IBSS)
85                 printf(", NO-IBSS");
86         if (flags & RRF_NO_HT20)
87                 printf(", NO-HT20");
88         if (flags & RRF_NO_HT40)
89                 printf(", NO-HT40");
90
91         printf("\n");
92 }
93
94 int main(int argc, char **argv)
95 {
96         int fd;
97         struct stat stat;
98         __u8 *db;
99         struct regdb_file_header *header;
100         struct regdb_file_reg_country *countries;
101         int dblen, siglen, num_countries, i, j;
102 #ifdef USE_OPENSSL
103         RSA *rsa;
104         __u8 hash[SHA_DIGEST_LENGTH];
105         int ok = 0;
106 #endif
107 #ifdef USE_GCRYPT
108         gcry_mpi_t mpi_e, mpi_n;
109         gcry_sexp_t rsa, signature, data;
110         __u8 hash[20];
111         int ok = 0;
112 #endif
113
114         if (argc != 2) {
115                 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
116                 return 2;
117         }
118
119         fd = open(argv[1], O_RDONLY);
120         if (fd < 0) {
121                 perror("failed to open db file");
122                 return 2;
123         }
124
125         if (fstat(fd, &stat)) {
126                 perror("failed to fstat db file");
127                 return 2;
128         }
129
130         dblen = stat.st_size;
131
132         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
133         if (db == MAP_FAILED) {
134                 perror("failed to mmap db file");
135                 return 2;
136         }
137
138         header = get_file_ptr(db, dblen, sizeof(*header), 0);
139
140         if (ntohl(header->magic) != REGDB_MAGIC) {
141                 fprintf(stderr, "Invalid database magic\n");
142                 return 2;
143         }
144
145         if (ntohl(header->version) != REGDB_VERSION) {
146                 fprintf(stderr, "Invalid database version\n");
147                 return 2;
148         }
149
150         siglen = ntohl(header->signature_length);
151         /* adjust dblen so later sanity checks don't run into the signature */
152         dblen -= siglen;
153
154         if (dblen <= sizeof(*header)) {
155                 fprintf(stderr, "Invalid signature length %d\n", siglen);
156                 return 2;
157         }
158
159         /* verify signature */
160 #ifdef USE_OPENSSL
161         rsa = RSA_new();
162         if (!rsa) {
163                 fprintf(stderr, "Failed to create RSA key\n");
164                 return 2;
165         }
166
167         if (SHA1(db, dblen, hash) != hash) {
168                 fprintf(stderr, "Failed to calculate SHA sum\n");
169                 return 2;
170         }
171
172         for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
173                 rsa->e = &keys[i].e;
174                 rsa->n = &keys[i].n;
175
176                 if (RSA_size(rsa) != siglen)
177                         continue;
178
179                 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
180                                 db + dblen, siglen, rsa) == 1;
181                 if (ok)
182                         break;
183         }
184
185         if (!ok) {
186                 fprintf(stderr, "Database signature wrong\n");
187                 return 2;
188         }
189
190         rsa->e = NULL;
191         rsa->n = NULL;
192         RSA_free(rsa);
193
194         BN_print_fp(stdout, &keys[0].n);
195
196         return 0;
197 #endif
198
199 #ifdef USE_GCRYPT
200         /* hash the db */
201         gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
202
203         if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
204                             20, hash)) {
205                 fprintf(stderr, "failed to build data expression\n");
206                 return 2;
207         }
208
209         if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
210                             siglen, db + dblen)) {
211                 fprintf(stderr, "failed to build signature expression\n");
212                 return 2;
213         }
214
215         for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
216                 if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
217                                   keys[0].e, keys[0].len_e, NULL) ||
218                     gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
219                                   keys[0].n, keys[0].len_n, NULL)) {
220                         fprintf(stderr, "failed to convert numbers\n");
221                         return 2;
222                 }
223
224                 if (gcry_sexp_build(&rsa, NULL,
225                                     "(public-key (rsa (n %m) (e %m)))",
226                                     mpi_n, mpi_e)) {
227                         fprintf(stderr, "failed to build rsa key\n");
228                         return 2;
229                 }
230
231                 if (!gcry_pk_verify(signature, data, rsa)) {
232                         ok = 1;
233                         break;
234                 }
235         }
236
237         if (!ok) {
238                 fprintf(stderr, "Database signature wrong\n");
239                 return 2;
240         }
241
242 #endif
243
244         num_countries = ntohl(header->reg_country_num);
245         countries = get_file_ptr(db, dblen,
246                                  sizeof(struct regdb_file_reg_country) * num_countries,
247                                  header->reg_country_ptr);
248
249         for (i = 0; i < num_countries; i++) {
250                 struct regdb_file_reg_rules_collection *rcoll;
251                 struct regdb_file_reg_country *country = countries + i;
252                 int num_rules;
253
254                 printf("country %.2s:\n", country->alpha2);
255                 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
256                 num_rules = ntohl(rcoll->reg_rule_num);
257                 /* re-get pointer with sanity checking for num_rules */
258                 rcoll = get_file_ptr(db, dblen,
259                                      sizeof(*rcoll) + num_rules * sizeof(__be32),
260                                      country->reg_collection_ptr);
261                 for (j = 0; j < num_rules; j++)
262                         print_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j]);
263                 printf("\n");
264         }
265
266         return 0;
267 }