Reinstate DE, Johannes had done research on this based on
[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_HT40)
87                 printf(", NO-HT40");
88
89         printf("\n");
90 }
91
92 int main(int argc, char **argv)
93 {
94         int fd;
95         struct stat stat;
96         __u8 *db;
97         struct regdb_file_header *header;
98         struct regdb_file_reg_country *countries;
99         int dblen, siglen, num_countries, i, j;
100 #ifdef USE_OPENSSL
101         RSA *rsa;
102         __u8 hash[SHA_DIGEST_LENGTH];
103         int ok = 0;
104 #endif
105 #ifdef USE_GCRYPT
106         gcry_mpi_t mpi_e, mpi_n;
107         gcry_sexp_t rsa, signature, data;
108         __u8 hash[20];
109         int ok = 0;
110 #endif
111
112         if (argc != 2) {
113                 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
114                 return 2;
115         }
116
117         fd = open(argv[1], O_RDONLY);
118         if (fd < 0) {
119                 perror("failed to open db file");
120                 return 2;
121         }
122
123         if (fstat(fd, &stat)) {
124                 perror("failed to fstat db file");
125                 return 2;
126         }
127
128         dblen = stat.st_size;
129
130         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
131         if (db == MAP_FAILED) {
132                 perror("failed to mmap db file");
133                 return 2;
134         }
135
136         header = get_file_ptr(db, dblen, sizeof(*header), 0);
137
138         if (ntohl(header->magic) != REGDB_MAGIC) {
139                 fprintf(stderr, "Invalid database magic\n");
140                 return 2;
141         }
142
143         if (ntohl(header->version) != REGDB_VERSION) {
144                 fprintf(stderr, "Invalid database version\n");
145                 return 2;
146         }
147
148         siglen = ntohl(header->signature_length);
149         /* adjust dblen so later sanity checks don't run into the signature */
150         dblen -= siglen;
151
152         if (dblen <= sizeof(*header)) {
153                 fprintf(stderr, "Invalid signature length %d\n", siglen);
154                 return 2;
155         }
156
157         /* verify signature */
158 #ifdef USE_OPENSSL
159         rsa = RSA_new();
160         if (!rsa) {
161                 fprintf(stderr, "Failed to create RSA key\n");
162                 return 2;
163         }
164
165         if (SHA1(db, dblen, hash) != hash) {
166                 fprintf(stderr, "Failed to calculate SHA sum\n");
167                 return 2;
168         }
169
170         for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
171                 rsa->e = &keys[i].e;
172                 rsa->n = &keys[i].n;
173
174                 if (RSA_size(rsa) != siglen)
175                         continue;
176
177                 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
178                                 db + dblen, siglen, rsa) == 1;
179                 if (ok)
180                         break;
181         }
182
183         if (!ok) {
184                 fprintf(stderr, "Database signature wrong\n");
185                 return 2;
186         }
187
188         rsa->e = NULL;
189         rsa->n = NULL;
190         RSA_free(rsa);
191
192         BN_print_fp(stdout, &keys[0].n);
193
194         return 0;
195 #endif
196
197 #ifdef USE_GCRYPT
198         /* hash the db */
199         gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
200
201         if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
202                             20, hash)) {
203                 fprintf(stderr, "failed to build data expression\n");
204                 return 2;
205         }
206
207         if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
208                             siglen, db + dblen)) {
209                 fprintf(stderr, "failed to build signature expression\n");
210                 return 2;
211         }
212
213         for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
214                 if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
215                                   keys[0].e, keys[0].len_e, NULL) ||
216                     gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
217                                   keys[0].n, keys[0].len_n, NULL)) {
218                         fprintf(stderr, "failed to convert numbers\n");
219                         return 2;
220                 }
221
222                 if (gcry_sexp_build(&rsa, NULL,
223                                     "(public-key (rsa (n %m) (e %m)))",
224                                     mpi_n, mpi_e)) {
225                         fprintf(stderr, "failed to build rsa key\n");
226                         return 2;
227                 }
228
229                 if (!gcry_pk_verify(signature, data, rsa)) {
230                         ok = 1;
231                         break;
232                 }
233         }
234
235         if (!ok) {
236                 fprintf(stderr, "Database signature wrong\n");
237                 return 2;
238         }
239
240 #endif
241
242         num_countries = ntohl(header->reg_country_num);
243         countries = get_file_ptr(db, dblen,
244                                  sizeof(struct regdb_file_reg_country) * num_countries,
245                                  header->reg_country_ptr);
246
247         for (i = 0; i < num_countries; i++) {
248                 struct regdb_file_reg_rules_collection *rcoll;
249                 struct regdb_file_reg_country *country = countries + i;
250                 int num_rules;
251
252                 printf("country %.2s:\n", country->alpha2);
253                 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
254                 num_rules = ntohl(rcoll->reg_rule_num);
255                 /* re-get pointer with sanity checking for num_rules */
256                 rcoll = get_file_ptr(db, dblen,
257                                      sizeof(*rcoll) + num_rules * sizeof(__be32),
258                                      country->reg_collection_ptr);
259                 for (j = 0; j < num_rules; j++)
260                         print_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j]);
261                 printf("\n");
262         }
263
264         return 0;
265 }