8 #include <arpa/inet.h> /* ntohl */
13 #include <openssl/objects.h>
14 #include <openssl/bn.h>
15 #include <openssl/rsa.h>
16 #include <openssl/sha.h>
24 #include "keys-gcrypt.c"
27 static void *get_file_ptr(__u8 *db, int dblen, int structlen, __be32 ptr)
31 if (p > dblen - structlen) {
32 fprintf(stderr, "Invalid database file, bad pointer!\n");
36 return (void *)(db + p);
39 static void print_reg_rule(__u8 *db, int dblen, __be32 ruleptr)
41 struct regdb_file_reg_rule *rule;
42 struct regdb_file_freq_range *freq;
43 struct regdb_file_power_rule *power;
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);
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);
57 if (power->max_antenna_gain)
58 printf("%.3f, ", ((float)ntohl(power->max_antenna_gain)/100.0));
63 printf("%.3f, ", ((float)ntohl(power->max_ir)/100.0));
68 printf("%.3f)", ((float)ntohl(power->max_eirp)/100.0));
72 flags = ntohl(rule->flags);
73 if (flags & RRF_NO_OFDM)
75 if (flags & RRF_NO_CCK)
77 if (flags & RRF_NO_INDOOR)
78 printf(", NO-INDOOR");
79 if (flags & RRF_NO_OUTDOOR)
80 printf(", NO-OUTDOOR");
83 if (flags & RRF_PTP_ONLY)
85 if (flags & RRF_PTMP_ONLY)
86 printf(", PTMP-ONLY");
91 int main(int argc, char **argv)
96 struct regdb_file_header *header;
97 struct regdb_file_reg_country *countries;
98 int dblen, siglen, num_countries, i, j;
101 __u8 hash[SHA_DIGEST_LENGTH];
105 gcry_mpi_t mpi_e, mpi_n;
106 gcry_sexp_t rsa, signature, data;
112 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
116 fd = open(argv[1], O_RDONLY);
118 perror("failed to open db file");
122 if (fstat(fd, &stat)) {
123 perror("failed to fstat db file");
127 dblen = stat.st_size;
129 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
130 if (db == MAP_FAILED) {
131 perror("failed to mmap db file");
135 header = get_file_ptr(db, dblen, sizeof(*header), 0);
137 if (ntohl(header->magic) != REGDB_MAGIC) {
138 fprintf(stderr, "Invalid database magic\n");
142 if (ntohl(header->version) != REGDB_VERSION) {
143 fprintf(stderr, "Invalid database version\n");
147 siglen = ntohl(header->signature_length);
148 /* adjust dblen so later sanity checks don't run into the signature */
151 if (dblen <= sizeof(*header)) {
152 fprintf(stderr, "Invalid signature length %d\n", siglen);
156 /* verify signature */
160 fprintf(stderr, "Failed to create RSA key\n");
164 if (SHA1(db, dblen, hash) != hash) {
165 fprintf(stderr, "Failed to calculate SHA sum\n");
169 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
173 if (RSA_size(rsa) != siglen)
176 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
177 db + dblen, siglen, rsa) == 1;
183 fprintf(stderr, "Database signature wrong\n");
191 BN_print_fp(stdout, &keys[0].n);
198 gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
200 if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
202 fprintf(stderr, "failed to build data expression\n");
206 if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
207 siglen, db + dblen)) {
208 fprintf(stderr, "failed to build signature expression\n");
212 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
213 if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
214 keys[0].e, keys[0].len_e, NULL) ||
215 gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
216 keys[0].n, keys[0].len_n, NULL)) {
217 fprintf(stderr, "failed to convert numbers\n");
221 if (gcry_sexp_build(&rsa, NULL,
222 "(public-key (rsa (n %m) (e %m)))",
224 fprintf(stderr, "failed to build rsa key\n");
228 if (!gcry_pk_verify(signature, data, rsa)) {
235 fprintf(stderr, "Database signature wrong\n");
241 num_countries = ntohl(header->reg_country_num);
242 countries = get_file_ptr(db, dblen,
243 sizeof(struct regdb_file_reg_country) * num_countries,
244 header->reg_country_ptr);
246 for (i = 0; i < num_countries; i++) {
247 struct regdb_file_reg_rules_collection *rcoll;
248 struct regdb_file_reg_country *country = countries + i;
251 printf("country %.2s:\n", country->alpha2);
252 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
253 num_rules = ntohl(rcoll->reg_rule_num);
254 /* re-get pointer with sanity checking for num_rules */
255 rcoll = get_file_ptr(db, dblen,
256 sizeof(*rcoll) + num_rules * sizeof(__be32),
257 country->reg_collection_ptr);
258 for (j = 0; j < num_rules; j++)
259 print_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j]);