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