reglib.c 15.2 KB
Newer Older
1
#include <errno.h>
2
#include <stdio.h>
3
#include <arpa/inet.h>
4 5
#include <sys/types.h>
#include <dirent.h>
6 7 8 9
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
10 11
#include <stdbool.h>
#include <unistd.h>
12
#include <string.h>
13 14 15

#include <arpa/inet.h> /* ntohl */

16
#include "reglib.h"
17
#include "regdb.h"
18 19

#ifdef USE_OPENSSL
20 21 22
#include <openssl/objects.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
23
#include <openssl/pem.h>
24 25 26 27 28 29
#endif

#ifdef USE_GCRYPT
#include <gcrypt.h>
#endif

30
#include "reglib.h"
31 32 33 34 35 36 37 38 39

#ifdef USE_OPENSSL
#include "keys-ssl.c"
#endif

#ifdef USE_GCRYPT
#include "keys-gcrypt.c"
#endif

40
void *reglib_get_file_ptr(uint8_t *db, int dblen, int structlen, uint32_t ptr)
41
{
42
	uint32_t p = ntohl(ptr);
43 44 45 46 47 48 49 50 51

	if (p > dblen - structlen) {
		fprintf(stderr, "Invalid database file, bad pointer!\n");
		exit(3);
	}

	return (void *)(db + p);
}

52
/*
53
 * reglib_verify_db_signature():
54
 *
55 56 57 58 59
 * Checks the validity of the signature found on the regulatory
 * database against the array 'keys'. Returns 1 if there exists
 * at least one key in the array such that the signature is valid
 * against that key; 0 otherwise.
 */
60 61

#ifdef USE_OPENSSL
62
int reglib_verify_db_signature(uint8_t *db, int dblen, int siglen)
63 64
{
	RSA *rsa;
65
	uint8_t hash[SHA_DIGEST_LENGTH];
66 67
	unsigned int i;
	int ok = 0;
68 69 70 71
	DIR *pubkey_dir;
	struct dirent *nextfile;
	FILE *keyfile;
	char filename[PATH_MAX];
72 73 74 75 76 77 78

	if (SHA1(db, dblen, hash) != hash) {
		fprintf(stderr, "Failed to calculate SHA1 sum.\n");
		goto out;
	}

	for (i = 0; (i < sizeof(keys)/sizeof(keys[0])) && (!ok); i++) {
79 80 81 82 83 84
		rsa = RSA_new();
		if (!rsa) {
			fprintf(stderr, "Failed to create RSA key.\n");
			goto out;
		}

85 86 87 88 89 90
		rsa->e = &keys[i].e;
		rsa->n = &keys[i].n;

		ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
				db + dblen, siglen, rsa) == 1;

91 92 93 94
		rsa->e = NULL;
		rsa->n = NULL;
		RSA_free(rsa);
	}
95 96 97 98 99 100 101
	if (!ok && (pubkey_dir = opendir(PUBKEY_DIR))) {
		while (!ok && (nextfile = readdir(pubkey_dir))) {
			snprintf(filename, PATH_MAX, "%s/%s", PUBKEY_DIR,
				nextfile->d_name);
			if ((keyfile = fopen(filename, "rb"))) {
				rsa = PEM_read_RSA_PUBKEY(keyfile,
					NULL, NULL, NULL);
Luis R. Rodriguez's avatar
Luis R. Rodriguez committed
102
				if (rsa)
103 104 105 106 107 108 109 110
					ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
						db + dblen, siglen, rsa) == 1;
				RSA_free(rsa);
				fclose(keyfile);
			}
		}
		closedir(pubkey_dir);
	}
111 112 113 114 115 116 117 118

	if (!ok)
		fprintf(stderr, "Database signature verification failed.\n");

out:
	return ok;
}
#endif /* USE_OPENSSL */
119 120

#ifdef USE_GCRYPT
121
int reglib_verify_db_signature(uint8_t *db, int dblen, int siglen)
122
{
123 124
	gcry_mpi_t mpi_e, mpi_n;
	gcry_sexp_t rsa, signature, data;
125
	uint8_t hash[20];
126 127 128 129 130 131 132 133 134 135 136 137
	unsigned int i;
	int ok = 0;

	/* initialise */
	gcry_check_version(NULL);

	/* hash the db */
	gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);

	if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
			    20, hash)) {
		fprintf(stderr, "Failed to build data S-expression.\n");
138
		return ok;
139 140 141 142 143
	}

	if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
			    siglen, db + dblen)) {
		fprintf(stderr, "Failed to build signature S-expression.\n");
144 145
		gcry_sexp_release(data);
		return ok;
146 147 148 149
	}

	for (i = 0; (i < sizeof(keys)/sizeof(keys[0])) && (!ok); i++) {
		if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
150
				keys[i].e, keys[i].len_e, NULL) ||
151
		    gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
152
				keys[i].n, keys[i].len_n, NULL)) {
153 154 155 156 157 158 159 160
			fprintf(stderr, "Failed to convert numbers.\n");
			goto out;
		}

		if (gcry_sexp_build(&rsa, NULL,
				    "(public-key (rsa (n %m) (e %m)))",
				    mpi_n, mpi_e)) {
			fprintf(stderr, "Failed to build RSA S-expression.\n");
161 162
			gcry_mpi_release(mpi_e);
			gcry_mpi_release(mpi_n);
163 164 165 166
			goto out;
		}

		ok = gcry_pk_verify(signature, data, rsa) == 0;
167 168
		gcry_mpi_release(mpi_e);
		gcry_mpi_release(mpi_n);
169
		gcry_sexp_release(rsa);
170 171 172 173 174 175
	}

	if (!ok)
		fprintf(stderr, "Database signature verification failed.\n");

out:
176 177
	gcry_sexp_release(data);
	gcry_sexp_release(signature);
178
	return ok;
179 180 181 182
}
#endif /* USE_GCRYPT */

#if !defined(USE_OPENSSL) && !defined(USE_GCRYPT)
183
int reglib_verify_db_signature(uint8_t *db, int dblen, int siglen)
184
{
185 186
	return 1;
}
187
#endif
188 189 190 191 192 193 194 195 196 197 198

static void reg_rule2rd(uint8_t *db, int dblen,
	uint32_t ruleptr, struct ieee80211_reg_rule *rd_reg_rule)
{
	struct regdb_file_reg_rule *rule;
	struct regdb_file_freq_range *freq;
	struct regdb_file_power_rule *power;

	struct ieee80211_freq_range *rd_freq_range = &rd_reg_rule->freq_range;
	struct ieee80211_power_rule *rd_power_rule = &rd_reg_rule->power_rule;

199 200 201
	rule  = reglib_get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
	freq  = reglib_get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
	power = reglib_get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
202 203 204 205 206 207 208 209 210 211 212 213

	rd_freq_range->start_freq_khz = ntohl(freq->start_freq);
	rd_freq_range->end_freq_khz = ntohl(freq->end_freq);
	rd_freq_range->max_bandwidth_khz = ntohl(freq->max_bandwidth);

	rd_power_rule->max_antenna_gain = ntohl(power->max_antenna_gain);
	rd_power_rule->max_eirp = ntohl(power->max_eirp);

	rd_reg_rule->flags = ntohl(rule->flags);
}

/* Converts a file regdomain to ieee80211_regdomain, easier to manage */
214
const static struct ieee80211_regdomain *
215 216
country2rd(uint8_t *db, int dblen,
	   struct regdb_file_reg_country *country)
217 218 219 220 221
{
	struct regdb_file_reg_rules_collection *rcoll;
	struct ieee80211_regdomain *rd;
	int i, num_rules, size_of_rd;

222
	rcoll = reglib_get_file_ptr(db, dblen, sizeof(*rcoll),
223 224 225
				country->reg_collection_ptr);
	num_rules = ntohl(rcoll->reg_rule_num);
	/* re-get pointer with sanity checking for num_rules */
226
	rcoll = reglib_get_file_ptr(db, dblen,
227 228 229 230 231 232 233 234 235 236 237 238 239 240
			sizeof(*rcoll) + num_rules * sizeof(uint32_t),
			country->reg_collection_ptr);

	size_of_rd = sizeof(struct ieee80211_regdomain) +
		num_rules * sizeof(struct ieee80211_reg_rule);

	rd = malloc(size_of_rd);
	if (!rd)
		return NULL;

	memset(rd, 0, size_of_rd);

	rd->alpha2[0] = country->alpha2[0];
	rd->alpha2[1] = country->alpha2[1];
241
	rd->dfs_region = country->creqs & 0x3;
242 243 244 245 246 247 248 249 250
	rd->n_reg_rules = num_rules;

	for (i = 0; i < num_rules; i++) {
		reg_rule2rd(db, dblen, rcoll->reg_rule_ptrs[i],
			&rd->reg_rules[i]);
	}

	return rd;
}
251

252
const struct ieee80211_regdomain *
253
reglib_get_rd_idx(unsigned int idx, const char *file)
254 255 256 257 258 259 260
{
	int fd;
	struct stat stat;
	uint8_t *db;
	struct regdb_file_header *header;
	struct regdb_file_reg_country *countries;
	int dblen, siglen, num_countries;
261
	const struct ieee80211_regdomain *rd = NULL;
262 263 264 265 266 267 268
	struct regdb_file_reg_country *country;

	fd = open(file, O_RDONLY);

	if (fd < 0)
		return NULL;

269 270
	if (fstat(fd, &stat)) {
		close(fd);
271
		return NULL;
272
	}
273 274 275 276

	dblen = stat.st_size;

	db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
277 278
	if (db == MAP_FAILED) {
		close(fd);
279
		return NULL;
280
	}
281

282
	header = reglib_get_file_ptr(db, dblen, sizeof(*header), 0);
283 284

	if (ntohl(header->magic) != REGDB_MAGIC)
285
		goto out;
286 287

	if (ntohl(header->version) != REGDB_VERSION)
288
		goto out;
289 290 291 292 293 294

	siglen = ntohl(header->signature_length);
	/* adjust dblen so later sanity checks don't run into the signature */
	dblen -= siglen;

	if (dblen <= (int)sizeof(*header))
295
		goto out;
296 297

	/* verify signature */
298
	if (!reglib_verify_db_signature(db, dblen, siglen))
299
		goto out;
300 301

	num_countries = ntohl(header->reg_country_num);
302
	countries = reglib_get_file_ptr(db, dblen,
303 304 305 306
			sizeof(struct regdb_file_reg_country) * num_countries,
			header->reg_country_ptr);

	if (idx >= num_countries)
307
		goto out;
308 309 310 311 312

	country = countries + idx;

	rd = country2rd(db, dblen, country);
	if (!rd)
313
		goto out;
314

315 316 317
out:
	close(fd);
	munmap(db, dblen);
318 319
	return rd;
}
320

321
const struct ieee80211_regdomain *
322
reglib_get_rd_alpha2(const char *alpha2, const char *file)
323 324 325 326 327 328 329
{
	int fd;
	struct stat stat;
	uint8_t *db;
	struct regdb_file_header *header;
	struct regdb_file_reg_country *countries;
	int dblen, siglen, num_countries;
330
	const struct ieee80211_regdomain *rd = NULL;
331 332 333 334 335 336 337 338 339
	struct regdb_file_reg_country *country;
	unsigned int i;
	bool found_country = false;

	fd = open(file, O_RDONLY);

	if (fd < 0)
		return NULL;

340 341
	if (fstat(fd, &stat)) {
		close(fd);
342
		return NULL;
343
	}
344 345 346 347

	dblen = stat.st_size;

	db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
348 349
	if (db == MAP_FAILED) {
		close(fd);
350
		return NULL;
351
	}
352

353
	header = reglib_get_file_ptr(db, dblen, sizeof(*header), 0);
354 355

	if (ntohl(header->magic) != REGDB_MAGIC)
356
		goto out;
357 358

	if (ntohl(header->version) != REGDB_VERSION)
359
		goto out;
360 361 362 363 364 365

	siglen = ntohl(header->signature_length);
	/* adjust dblen so later sanity checks don't run into the signature */
	dblen -= siglen;

	if (dblen <= (int)sizeof(*header))
366
		goto out;
367 368

	/* verify signature */
369
	if (!reglib_verify_db_signature(db, dblen, siglen))
370
		goto out;
371 372

	num_countries = ntohl(header->reg_country_num);
373
	countries = reglib_get_file_ptr(db, dblen,
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			sizeof(struct regdb_file_reg_country) * num_countries,
			header->reg_country_ptr);

	for (i = 0; i < num_countries; i++) {
		country = countries + i;
		if (memcmp(country->alpha2, alpha2, 2) == 0) {
			found_country = 1;
			break;
		}
	}

	if (!found_country)
		goto out;

	rd = country2rd(db, dblen, country);
	if (!rd)
		goto out;

out:
	close(fd);
394
	munmap(db, dblen);
395 396
	return rd;
}
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

/* Sanity check on a regulatory rule */
static int is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
{
	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
	uint32_t freq_diff;

	if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
		return 0;

	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
		return 0;

	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;

	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
	    freq_range->max_bandwidth_khz > freq_diff)
		return 0;

	return 1;
}

/*
420
 * Helper for reglib_intersect_rds(), this does the real
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
 * mathematical intersection fun
 */
static int reg_rules_intersect(const struct ieee80211_reg_rule *rule1,
			       const struct ieee80211_reg_rule *rule2,
			       struct ieee80211_reg_rule *intersected_rule)
{
	const struct ieee80211_freq_range *freq_range1, *freq_range2;
	struct ieee80211_freq_range *freq_range;
	const struct ieee80211_power_rule *power_rule1, *power_rule2;
	struct ieee80211_power_rule *power_rule;
	uint32_t freq_diff;

	freq_range1 = &rule1->freq_range;
	freq_range2 = &rule2->freq_range;
	freq_range = &intersected_rule->freq_range;

	power_rule1 = &rule1->power_rule;
	power_rule2 = &rule2->power_rule;
	power_rule = &intersected_rule->power_rule;

441
	freq_range->start_freq_khz = reglib_max(freq_range1->start_freq_khz,
442
					 freq_range2->start_freq_khz);
443
	freq_range->end_freq_khz = reglib_min(freq_range1->end_freq_khz,
444
				       freq_range2->end_freq_khz);
445
	freq_range->max_bandwidth_khz = reglib_min(freq_range1->max_bandwidth_khz,
446 447 448 449 450 451
					    freq_range2->max_bandwidth_khz);

	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
	if (freq_range->max_bandwidth_khz > freq_diff)
		freq_range->max_bandwidth_khz = freq_diff;

452
	power_rule->max_eirp = reglib_min(power_rule1->max_eirp,
453
		power_rule2->max_eirp);
454
	power_rule->max_antenna_gain = reglib_min(power_rule1->max_antenna_gain,
455 456 457 458 459 460 461 462 463 464 465
		power_rule2->max_antenna_gain);

	intersected_rule->flags = rule1->flags | rule2->flags;

	if (!is_valid_reg_rule(intersected_rule))
		return -EINVAL;

	return 0;
}

/**
466
 * reglib_intersect_rds - do the intersection between two regulatory domains
467 468 469 470 471 472 473 474 475 476 477 478
 * @rd1: first regulatory domain
 * @rd2: second regulatory domain
 *
 * Use this function to get the intersection between two regulatory domains.
 * Once completed we will mark the alpha2 for the rd as intersected, "98",
 * as no one single alpha2 can represent this regulatory domain.
 *
 * Returns a pointer to the regulatory domain structure which will hold the
 * resulting intersection of rules between rd1 and rd2. We will
 * malloc() this structure for you.
 */
struct ieee80211_regdomain *
479 480
reglib_intersect_rds(const struct ieee80211_regdomain *rd1,
		     const struct ieee80211_regdomain *rd2)
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
{
	int r, size_of_regd;
	unsigned int x, y;
	unsigned int num_rules = 0, rule_idx = 0;
	const struct ieee80211_reg_rule *rule1, *rule2;
	struct ieee80211_reg_rule *intersected_rule;
	struct ieee80211_regdomain *rd;
	/* This is just a dummy holder to help us count */
	struct ieee80211_reg_rule irule;

	/* Uses the stack temporarily for counter arithmetic */
	intersected_rule = &irule;

	memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));

	if (!rd1 || !rd2)
		return NULL;

	/* First we get a count of the rules we'll need, then we actually
	 * build them. This is to so we can malloc() and free() a
	 * regdomain once. The reason we use reg_rules_intersect() here
	 * is it will return -EINVAL if the rule computed makes no sense.
	 * All rules that do check out OK are valid. */

	for (x = 0; x < rd1->n_reg_rules; x++) {
		rule1 = &rd1->reg_rules[x];
		for (y = 0; y < rd2->n_reg_rules; y++) {
			rule2 = &rd2->reg_rules[y];
			if (!reg_rules_intersect(rule1, rule2,
					intersected_rule))
				num_rules++;
			memset(intersected_rule, 0,
					sizeof(struct ieee80211_reg_rule));
		}
	}

	if (!num_rules)
		return NULL;

	size_of_regd = sizeof(struct ieee80211_regdomain) +
		((num_rules + 1) * sizeof(struct ieee80211_reg_rule));

	rd = malloc(size_of_regd);
	if (!rd)
		return NULL;

	memset(rd, 0, size_of_regd);

	for (x = 0; x < rd1->n_reg_rules; x++) {
		rule1 = &rd1->reg_rules[x];
		for (y = 0; y < rd2->n_reg_rules; y++) {
			rule2 = &rd2->reg_rules[y];
			/* This time around instead of using the stack lets
			 * write to the target rule directly saving ourselves
			 * a memcpy() */
			intersected_rule = &rd->reg_rules[rule_idx];
			r = reg_rules_intersect(rule1, rule2,
				intersected_rule);
			if (r)
				continue;
			rule_idx++;
		}
	}

	if (rule_idx != num_rules) {
		free(rd);
		return NULL;
	}

	rd->n_reg_rules = num_rules;
	rd->alpha2[0] = '9';
	rd->alpha2[1] = '9';

	return rd;
}
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

static const char *dfs_domain_name(enum regdb_dfs_regions region)
{
	switch (region) {
	case REGDB_DFS_UNSET:
		return "DFS-UNSET";
	case REGDB_DFS_FCC:
		return "DFS-FCC";
	case REGDB_DFS_ETSI:
		return "DFS-ETSI";
	case REGDB_DFS_JP:
		return "DFS-JP";
	default:
		return "DFS-invalid";
	}
}

static void print_reg_rule(const struct ieee80211_reg_rule *rule)
{
	const struct ieee80211_freq_range *freq;
	const struct ieee80211_power_rule *power;

	freq  = &rule->freq_range;
	power = &rule->power_rule;

	printf("\t(%.3f - %.3f @ %.3f), ",
	       ((float)(freq->start_freq_khz))/1000.0,
	       ((float)(freq->end_freq_khz))/1000.0,
	       ((float)(freq->max_bandwidth_khz))/1000.0);

	printf("(");

	if (power->max_antenna_gain)
		printf("%.2f, ", ((float)(power->max_antenna_gain)/100.0));
	else
		printf("N/A, ");

	if (power->max_eirp)
		printf("%.2f)", ((float)(power->max_eirp)/100.0));
	else
		printf("N/A)");

	if (rule->flags & RRF_NO_OFDM)
		printf(", NO-OFDM");
	if (rule->flags & RRF_NO_CCK)
		printf(", NO-CCK");
	if (rule->flags & RRF_NO_INDOOR)
		printf(", NO-INDOOR");
	if (rule->flags & RRF_NO_OUTDOOR)
		printf(", NO-OUTDOOR");
	if (rule->flags & RRF_DFS)
		printf(", DFS");
	if (rule->flags & RRF_PTP_ONLY)
		printf(", PTP-ONLY");
	if (rule->flags & RRF_PTMP_ONLY)
		printf(", PTMP-ONLY");
	if (rule->flags & RRF_PASSIVE_SCAN)
		printf(", PASSIVE-SCAN");
	if (rule->flags & RRF_NO_IBSS)
		printf(", NO-IBSS");

	printf("\n");
}

void reglib_print_regdom(const struct ieee80211_regdomain *rd)
{
	unsigned int i;
	printf("country %.2s: %s\n", rd->alpha2,
	       dfs_domain_name(rd->dfs_region));
	for (i = 0; i < rd->n_reg_rules; i++)
		print_reg_rule(&rd->reg_rules[i]);
	printf("\n");
}