Home | History | Annotate | Line # | Download | only in dnssec
dnssec-dsfromkey.c revision 1.8
      1 /*	$NetBSD: dnssec-dsfromkey.c,v 1.8 2020/05/24 19:46:11 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * This Source Code Form is subject to the terms of the Mozilla Public
      7  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  *
     10  * See the COPYRIGHT file distributed with this work for additional
     11  * information regarding copyright ownership.
     12  */
     13 
     14 /*! \file */
     15 
     16 #include <inttypes.h>
     17 #include <stdbool.h>
     18 #include <stdlib.h>
     19 
     20 #include <isc/buffer.h>
     21 #include <isc/commandline.h>
     22 #include <isc/hash.h>
     23 #include <isc/mem.h>
     24 #include <isc/print.h>
     25 #include <isc/string.h>
     26 #include <isc/util.h>
     27 
     28 #include <dns/callbacks.h>
     29 #include <dns/db.h>
     30 #include <dns/dbiterator.h>
     31 #include <dns/ds.h>
     32 #include <dns/fixedname.h>
     33 #include <dns/keyvalues.h>
     34 #include <dns/log.h>
     35 #include <dns/master.h>
     36 #include <dns/name.h>
     37 #include <dns/rdata.h>
     38 #include <dns/rdataclass.h>
     39 #include <dns/rdataset.h>
     40 #include <dns/rdatasetiter.h>
     41 #include <dns/rdatatype.h>
     42 #include <dns/result.h>
     43 
     44 #include <dst/dst.h>
     45 
     46 #if USE_PKCS11
     47 #include <pk11/result.h>
     48 #endif /* if USE_PKCS11 */
     49 
     50 #include "dnssectool.h"
     51 
     52 const char *program = "dnssec-dsfromkey";
     53 
     54 static dns_rdataclass_t rdclass;
     55 static dns_fixedname_t fixed;
     56 static dns_name_t *name = NULL;
     57 static isc_mem_t *mctx = NULL;
     58 static uint32_t ttl;
     59 static bool emitttl = false;
     60 
     61 static isc_result_t
     62 initname(char *setname) {
     63 	isc_result_t result;
     64 	isc_buffer_t buf;
     65 
     66 	name = dns_fixedname_initname(&fixed);
     67 
     68 	isc_buffer_init(&buf, setname, strlen(setname));
     69 	isc_buffer_add(&buf, strlen(setname));
     70 	result = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
     71 	return (result);
     72 }
     73 
     74 static void
     75 db_load_from_stream(dns_db_t *db, FILE *fp) {
     76 	isc_result_t result;
     77 	dns_rdatacallbacks_t callbacks;
     78 
     79 	dns_rdatacallbacks_init(&callbacks);
     80 	result = dns_db_beginload(db, &callbacks);
     81 	if (result != ISC_R_SUCCESS) {
     82 		fatal("dns_db_beginload failed: %s", isc_result_totext(result));
     83 	}
     84 
     85 	result = dns_master_loadstream(fp, name, name, rdclass, 0, &callbacks,
     86 				       mctx);
     87 	if (result != ISC_R_SUCCESS) {
     88 		fatal("can't load from input: %s", isc_result_totext(result));
     89 	}
     90 
     91 	result = dns_db_endload(db, &callbacks);
     92 	if (result != ISC_R_SUCCESS) {
     93 		fatal("dns_db_endload failed: %s", isc_result_totext(result));
     94 	}
     95 }
     96 
     97 static isc_result_t
     98 loadset(const char *filename, dns_rdataset_t *rdataset) {
     99 	isc_result_t result;
    100 	dns_db_t *db = NULL;
    101 	dns_dbnode_t *node = NULL;
    102 	char setname[DNS_NAME_FORMATSIZE];
    103 
    104 	dns_name_format(name, setname, sizeof(setname));
    105 
    106 	result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone, rdclass, 0,
    107 			       NULL, &db);
    108 	if (result != ISC_R_SUCCESS) {
    109 		fatal("can't create database");
    110 	}
    111 
    112 	if (strcmp(filename, "-") == 0) {
    113 		db_load_from_stream(db, stdin);
    114 		filename = "input";
    115 	} else {
    116 		result = dns_db_load(db, filename, dns_masterformat_text, 0);
    117 		if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
    118 			fatal("can't load %s: %s", filename,
    119 			      isc_result_totext(result));
    120 		}
    121 	}
    122 
    123 	result = dns_db_findnode(db, name, false, &node);
    124 	if (result != ISC_R_SUCCESS) {
    125 		fatal("can't find %s node in %s", setname, filename);
    126 	}
    127 
    128 	result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey, 0, 0,
    129 				     rdataset, NULL);
    130 
    131 	if (result == ISC_R_NOTFOUND) {
    132 		fatal("no DNSKEY RR for %s in %s", setname, filename);
    133 	} else if (result != ISC_R_SUCCESS) {
    134 		fatal("dns_db_findrdataset");
    135 	}
    136 
    137 	if (node != NULL) {
    138 		dns_db_detachnode(db, &node);
    139 	}
    140 	if (db != NULL) {
    141 		dns_db_detach(&db);
    142 	}
    143 	return (result);
    144 }
    145 
    146 static isc_result_t
    147 loadkeyset(char *dirname, dns_rdataset_t *rdataset) {
    148 	isc_result_t result;
    149 	char filename[PATH_MAX + 1];
    150 	isc_buffer_t buf;
    151 
    152 	dns_rdataset_init(rdataset);
    153 
    154 	isc_buffer_init(&buf, filename, sizeof(filename));
    155 	if (dirname != NULL) {
    156 		/* allow room for a trailing slash */
    157 		if (strlen(dirname) >= isc_buffer_availablelength(&buf)) {
    158 			return (ISC_R_NOSPACE);
    159 		}
    160 		isc_buffer_putstr(&buf, dirname);
    161 		if (dirname[strlen(dirname) - 1] != '/') {
    162 			isc_buffer_putstr(&buf, "/");
    163 		}
    164 	}
    165 
    166 	if (isc_buffer_availablelength(&buf) < 7) {
    167 		return (ISC_R_NOSPACE);
    168 	}
    169 	isc_buffer_putstr(&buf, "keyset-");
    170 
    171 	result = dns_name_tofilenametext(name, false, &buf);
    172 	check_result(result, "dns_name_tofilenametext()");
    173 	if (isc_buffer_availablelength(&buf) == 0) {
    174 		return (ISC_R_NOSPACE);
    175 	}
    176 	isc_buffer_putuint8(&buf, 0);
    177 
    178 	return (loadset(filename, rdataset));
    179 }
    180 
    181 static void
    182 loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
    183 	dns_rdata_t *rdata) {
    184 	isc_result_t result;
    185 	dst_key_t *key = NULL;
    186 	isc_buffer_t keyb;
    187 	isc_region_t r;
    188 
    189 	dns_rdata_init(rdata);
    190 
    191 	isc_buffer_init(&keyb, key_buf, key_buf_size);
    192 
    193 	result = dst_key_fromnamedfile(filename, NULL, DST_TYPE_PUBLIC, mctx,
    194 				       &key);
    195 	if (result != ISC_R_SUCCESS) {
    196 		fatal("can't load %s.key: %s", filename,
    197 		      isc_result_totext(result));
    198 	}
    199 
    200 	if (verbose > 2) {
    201 		char keystr[DST_KEY_FORMATSIZE];
    202 
    203 		dst_key_format(key, keystr, sizeof(keystr));
    204 		fprintf(stderr, "%s: %s\n", program, keystr);
    205 	}
    206 
    207 	result = dst_key_todns(key, &keyb);
    208 	if (result != ISC_R_SUCCESS) {
    209 		fatal("can't decode key");
    210 	}
    211 
    212 	isc_buffer_usedregion(&keyb, &r);
    213 	dns_rdata_fromregion(rdata, dst_key_class(key), dns_rdatatype_dnskey,
    214 			     &r);
    215 
    216 	rdclass = dst_key_class(key);
    217 
    218 	name = dns_fixedname_initname(&fixed);
    219 	dns_name_copynf(dst_key_name(key), name);
    220 
    221 	dst_key_free(&key);
    222 }
    223 
    224 static void
    225 logkey(dns_rdata_t *rdata) {
    226 	isc_result_t result;
    227 	dst_key_t *key = NULL;
    228 	isc_buffer_t buf;
    229 	char keystr[DST_KEY_FORMATSIZE];
    230 
    231 	isc_buffer_init(&buf, rdata->data, rdata->length);
    232 	isc_buffer_add(&buf, rdata->length);
    233 	result = dst_key_fromdns(name, rdclass, &buf, mctx, &key);
    234 	if (result != ISC_R_SUCCESS) {
    235 		return;
    236 	}
    237 
    238 	dst_key_format(key, keystr, sizeof(keystr));
    239 	fprintf(stderr, "%s: %s\n", program, keystr);
    240 
    241 	dst_key_free(&key);
    242 }
    243 
    244 static void
    245 emit(dns_dsdigest_t dt, bool showall, bool cds, dns_rdata_t *rdata) {
    246 	isc_result_t result;
    247 	unsigned char buf[DNS_DS_BUFFERSIZE];
    248 	char text_buf[DST_KEY_MAXTEXTSIZE];
    249 	char name_buf[DNS_NAME_MAXWIRE];
    250 	char class_buf[10];
    251 	isc_buffer_t textb, nameb, classb;
    252 	isc_region_t r;
    253 	dns_rdata_t ds;
    254 	dns_rdata_dnskey_t dnskey;
    255 
    256 	isc_buffer_init(&textb, text_buf, sizeof(text_buf));
    257 	isc_buffer_init(&nameb, name_buf, sizeof(name_buf));
    258 	isc_buffer_init(&classb, class_buf, sizeof(class_buf));
    259 
    260 	dns_rdata_init(&ds);
    261 
    262 	result = dns_rdata_tostruct(rdata, &dnskey, NULL);
    263 	if (result != ISC_R_SUCCESS) {
    264 		fatal("can't convert DNSKEY");
    265 	}
    266 
    267 	if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0 && !showall) {
    268 		return;
    269 	}
    270 
    271 	result = dns_ds_buildrdata(name, rdata, dt, buf, &ds);
    272 	if (result != ISC_R_SUCCESS) {
    273 		fatal("can't build record");
    274 	}
    275 
    276 	result = dns_name_totext(name, false, &nameb);
    277 	if (result != ISC_R_SUCCESS) {
    278 		fatal("can't print name");
    279 	}
    280 
    281 	result = dns_rdata_tofmttext(&ds, (dns_name_t *)NULL, 0, 0, 0, "",
    282 				     &textb);
    283 
    284 	if (result != ISC_R_SUCCESS) {
    285 		fatal("can't print rdata");
    286 	}
    287 
    288 	result = dns_rdataclass_totext(rdclass, &classb);
    289 	if (result != ISC_R_SUCCESS) {
    290 		fatal("can't print class");
    291 	}
    292 
    293 	isc_buffer_usedregion(&nameb, &r);
    294 	printf("%.*s ", (int)r.length, r.base);
    295 
    296 	if (emitttl) {
    297 		printf("%u ", ttl);
    298 	}
    299 
    300 	isc_buffer_usedregion(&classb, &r);
    301 	printf("%.*s", (int)r.length, r.base);
    302 
    303 	if (cds) {
    304 		printf(" CDS ");
    305 	} else {
    306 		printf(" DS ");
    307 	}
    308 
    309 	isc_buffer_usedregion(&textb, &r);
    310 	printf("%.*s\n", (int)r.length, r.base);
    311 }
    312 
    313 static void
    314 emits(bool showall, bool cds, dns_rdata_t *rdata) {
    315 	unsigned i, n;
    316 
    317 	n = sizeof(dtype) / sizeof(dtype[0]);
    318 	for (i = 0; i < n; i++) {
    319 		if (dtype[i] != 0) {
    320 			emit(dtype[i], showall, cds, rdata);
    321 		}
    322 	}
    323 }
    324 
    325 ISC_PLATFORM_NORETURN_PRE static void
    326 usage(void) ISC_PLATFORM_NORETURN_POST;
    327 
    328 static void
    329 usage(void) {
    330 	fprintf(stderr, "Usage:\n");
    331 	fprintf(stderr, "    %s [options] keyfile\n\n", program);
    332 	fprintf(stderr, "    %s [options] -f zonefile [zonename]\n\n", program);
    333 	fprintf(stderr, "    %s [options] -s dnsname\n\n", program);
    334 	fprintf(stderr, "    %s [-h|-V]\n\n", program);
    335 	fprintf(stderr, "Version: %s\n", VERSION);
    336 	fprintf(stderr, "Options:\n"
    337 			"    -1: digest algorithm SHA-1\n"
    338 			"    -2: digest algorithm SHA-256\n"
    339 			"    -a algorithm: digest algorithm (SHA-1, SHA-256 or "
    340 			"SHA-384)\n"
    341 			"    -A: include all keys in DS set, not just KSKs (-f "
    342 			"only)\n"
    343 			"    -c class: rdata class for DS set (default IN) (-f "
    344 			"or -s only)\n"
    345 			"    -C: print CDS records\n"
    346 			"    -f zonefile: read keys from a zone file\n"
    347 			"    -h: print help information\n"
    348 			"    -K directory: where to find key or keyset files\n"
    349 			"    -s: read keys from keyset-<dnsname> file\n"
    350 			"    -T: TTL of output records (omitted by default)\n"
    351 			"    -v level: verbosity\n"
    352 			"    -V: print version information\n");
    353 	fprintf(stderr, "Output: DS or CDS RRs\n");
    354 
    355 	exit(-1);
    356 }
    357 
    358 int
    359 main(int argc, char **argv) {
    360 	char *classname = NULL;
    361 	char *filename = NULL, *dir = NULL, *namestr;
    362 	char *endp, *arg1;
    363 	int ch;
    364 	bool cds = false;
    365 	bool usekeyset = false;
    366 	bool showall = false;
    367 	isc_result_t result;
    368 	isc_log_t *log = NULL;
    369 	dns_rdataset_t rdataset;
    370 	dns_rdata_t rdata;
    371 
    372 	dns_rdata_init(&rdata);
    373 
    374 	if (argc == 1) {
    375 		usage();
    376 	}
    377 
    378 	isc_mem_create(&mctx);
    379 
    380 #if USE_PKCS11
    381 	pk11_result_register();
    382 #endif /* if USE_PKCS11 */
    383 	dns_result_register();
    384 
    385 	isc_commandline_errprint = false;
    386 
    387 #define OPTIONS "12Aa:Cc:d:Ff:K:l:sT:v:hV"
    388 	while ((ch = isc_commandline_parse(argc, argv, OPTIONS)) != -1) {
    389 		switch (ch) {
    390 		case '1':
    391 			add_dtype(DNS_DSDIGEST_SHA1);
    392 			break;
    393 		case '2':
    394 			add_dtype(DNS_DSDIGEST_SHA256);
    395 			break;
    396 		case 'A':
    397 			showall = true;
    398 			break;
    399 		case 'a':
    400 			add_dtype(strtodsdigest(isc_commandline_argument));
    401 			break;
    402 		case 'C':
    403 			cds = true;
    404 			break;
    405 		case 'c':
    406 			classname = isc_commandline_argument;
    407 			break;
    408 		case 'd':
    409 			fprintf(stderr,
    410 				"%s: the -d option is deprecated; "
    411 				"use -K\n",
    412 				program);
    413 		/* fall through */
    414 		case 'K':
    415 			dir = isc_commandline_argument;
    416 			if (strlen(dir) == 0U) {
    417 				fatal("directory must be non-empty string");
    418 			}
    419 			break;
    420 		case 'f':
    421 			filename = isc_commandline_argument;
    422 			break;
    423 		case 'l':
    424 			fatal("-l option (DLV lookaside) is obsolete");
    425 			break;
    426 		case 's':
    427 			usekeyset = true;
    428 			break;
    429 		case 'T':
    430 			emitttl = true;
    431 			ttl = strtottl(isc_commandline_argument);
    432 			break;
    433 		case 'v':
    434 			verbose = strtol(isc_commandline_argument, &endp, 0);
    435 			if (*endp != '\0') {
    436 				fatal("-v must be followed by a number");
    437 			}
    438 			break;
    439 		case 'F':
    440 		/* Reserved for FIPS mode */
    441 		/* FALLTHROUGH */
    442 		case '?':
    443 			if (isc_commandline_option != '?') {
    444 				fprintf(stderr, "%s: invalid argument -%c\n",
    445 					program, isc_commandline_option);
    446 			}
    447 		/* FALLTHROUGH */
    448 		case 'h':
    449 			/* Does not return. */
    450 			usage();
    451 
    452 		case 'V':
    453 			/* Does not return. */
    454 			version(program);
    455 
    456 		default:
    457 			fprintf(stderr, "%s: unhandled option -%c\n", program,
    458 				isc_commandline_option);
    459 			exit(1);
    460 		}
    461 	}
    462 
    463 	rdclass = strtoclass(classname);
    464 
    465 	if (usekeyset && filename != NULL) {
    466 		fatal("cannot use both -s and -f");
    467 	}
    468 
    469 	/* When not using -f, -A is implicit */
    470 	if (filename == NULL) {
    471 		showall = true;
    472 	}
    473 
    474 	/* Default digest type if none specified. */
    475 	if (dtype[0] == 0) {
    476 		dtype[0] = DNS_DSDIGEST_SHA256;
    477 	}
    478 
    479 	/*
    480 	 * Use local variable arg1 so that clang can correctly analyse
    481 	 * reachable paths rather than 'argc < isc_commandline_index + 1'.
    482 	 */
    483 	arg1 = argv[isc_commandline_index];
    484 	if (arg1 == NULL && filename == NULL) {
    485 		fatal("the key file name was not specified");
    486 	}
    487 	if (arg1 != NULL && argv[isc_commandline_index + 1] != NULL) {
    488 		fatal("extraneous arguments");
    489 	}
    490 
    491 	result = dst_lib_init(mctx, NULL);
    492 	if (result != ISC_R_SUCCESS) {
    493 		fatal("could not initialize dst: %s",
    494 		      isc_result_totext(result));
    495 	}
    496 
    497 	setup_logging(mctx, &log);
    498 
    499 	dns_rdataset_init(&rdataset);
    500 
    501 	if (usekeyset || filename != NULL) {
    502 		if (arg1 == NULL) {
    503 			/* using file name as the zone name */
    504 			namestr = filename;
    505 		} else {
    506 			namestr = arg1;
    507 		}
    508 
    509 		result = initname(namestr);
    510 		if (result != ISC_R_SUCCESS) {
    511 			fatal("could not initialize name %s", namestr);
    512 		}
    513 
    514 		if (usekeyset) {
    515 			result = loadkeyset(dir, &rdataset);
    516 		} else {
    517 			INSIST(filename != NULL);
    518 			result = loadset(filename, &rdataset);
    519 		}
    520 
    521 		if (result != ISC_R_SUCCESS) {
    522 			fatal("could not load DNSKEY set: %s\n",
    523 			      isc_result_totext(result));
    524 		}
    525 
    526 		for (result = dns_rdataset_first(&rdataset);
    527 		     result == ISC_R_SUCCESS;
    528 		     result = dns_rdataset_next(&rdataset))
    529 		{
    530 			dns_rdata_init(&rdata);
    531 			dns_rdataset_current(&rdataset, &rdata);
    532 
    533 			if (verbose > 2) {
    534 				logkey(&rdata);
    535 			}
    536 
    537 			emits(showall, cds, &rdata);
    538 		}
    539 	} else {
    540 		unsigned char key_buf[DST_KEY_MAXSIZE];
    541 
    542 		loadkey(arg1, key_buf, DST_KEY_MAXSIZE, &rdata);
    543 
    544 		emits(showall, cds, &rdata);
    545 	}
    546 
    547 	if (dns_rdataset_isassociated(&rdataset)) {
    548 		dns_rdataset_disassociate(&rdataset);
    549 	}
    550 	cleanup_logging(&log);
    551 	dst_lib_destroy();
    552 	if (verbose > 10) {
    553 		isc_mem_stats(mctx, stdout);
    554 	}
    555 	isc_mem_destroy(&mctx);
    556 
    557 	fflush(stdout);
    558 	if (ferror(stdout)) {
    559 		fprintf(stderr, "write error\n");
    560 		return (1);
    561 	} else {
    562 		return (0);
    563 	}
    564 }
    565