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;
44 char *ofdm = "", *cck = "", env[3] = { 'O', 'I', '\0' };
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 if (ntohl(freq->modulation_cap) & 2)
52 if (ntohl(freq->modulation_cap) & 1)
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,
61 if (power->environment_cap != ' ') {
62 env[0] = power->environment_cap;
66 printf("(%s, %.3f, %.3f, %.3f, %.3f, %.3f)\n",
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);
75 int main(int argc, char **argv)
80 struct regdb_file_header *header;
81 struct regdb_file_reg_country *countries;
82 int dblen, siglen, num_countries, i, j;
85 __u8 hash[SHA_DIGEST_LENGTH];
89 gcry_mpi_t mpi_e, mpi_n;
90 gcry_sexp_t rsa, signature, data;
96 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
100 fd = open(argv[1], O_RDONLY);
102 perror("failed to open db file");
106 if (fstat(fd, &stat)) {
107 perror("failed to fstat db file");
111 dblen = stat.st_size;
113 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
114 if (db == MAP_FAILED) {
115 perror("failed to mmap db file");
119 header = get_file_ptr(db, dblen, sizeof(*header), 0);
121 if (ntohl(header->magic) != REGDB_MAGIC) {
122 fprintf(stderr, "Invalid database magic\n");
126 if (ntohl(header->version) != REGDB_VERSION) {
127 fprintf(stderr, "Invalid database version\n");
131 siglen = ntohl(header->signature_length);
132 /* adjust dblen so later sanity checks don't run into the signature */
135 if (dblen <= sizeof(*header)) {
136 fprintf(stderr, "Invalid signature length %d\n", siglen);
140 /* verify signature */
144 fprintf(stderr, "Failed to create RSA key\n");
148 if (SHA1(db, dblen, hash) != hash) {
149 fprintf(stderr, "Failed to calculate SHA sum\n");
153 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
157 if (RSA_size(rsa) != siglen)
160 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
161 db + dblen, siglen, rsa) == 1;
167 fprintf(stderr, "Database signature wrong\n");
175 BN_print_fp(stdout, &keys[0].n);
182 gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
184 if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
186 fprintf(stderr, "failed to build data expression\n");
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");
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");
205 if (gcry_sexp_build(&rsa, NULL,
206 "(public-key (rsa (n %m) (e %m)))",
208 fprintf(stderr, "failed to build rsa key\n");
212 if (!gcry_pk_verify(signature, data, rsa)) {
219 fprintf(stderr, "Database signature wrong\n");
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);
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;
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]);