dnssec-dsfromkey.c 13.4 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

17
/* $Id: dnssec-dsfromkey.c,v 1.13 2009/09/01 00:22:24 jinmei Exp $ */
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

/*! \file */

#include <config.h>

#include <stdlib.h>

#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/hash.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/string.h>
#include <isc/util.h>

#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/ds.h>
#include <dns/fixedname.h>
#include <dns/log.h>
39 40
#include <dns/keyvalues.h>
#include <dns/master.h>
41 42 43 44 45 46 47 48 49 50 51 52
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatatype.h>
#include <dns/result.h>

#include <dst/dst.h>

#include "dnssectool.h"

53
#ifndef PATH_MAX
Evan Hunt's avatar
Evan Hunt committed
54
#define PATH_MAX 1024   /* AIX, WIN32, and others don't define this. */
55 56
#endif

57 58 59 60
const char *program = "dnssec-dsfromkey";
int verbose;

static dns_rdataclass_t rdclass;
61 62 63
static dns_fixedname_t	fixed;
static dns_name_t	*name = NULL;
static isc_mem_t	*mctx = NULL;
64

65 66 67 68
static isc_result_t
initname(char *setname) {
	isc_result_t result;
	isc_buffer_t buf;
69

Automatic Updater's avatar
Automatic Updater committed
70 71 72 73 74
	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);

	isc_buffer_init(&buf, setname, strlen(setname));
	isc_buffer_add(&buf, strlen(setname));
75
	result = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
76 77
	return (result);
}
78

79 80 81 82 83 84
static isc_result_t
loadsetfromfile(char *filename, dns_rdataset_t *rdataset) {
	isc_result_t	 result;
	dns_db_t	 *db = NULL;
	dns_dbnode_t	 *node = NULL;
	char setname[DNS_NAME_FORMATSIZE];
85

86
	dns_name_format(name, setname, sizeof(setname));
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

	result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
			       rdclass, 0, NULL, &db);
	if (result != ISC_R_SUCCESS)
		fatal("can't create database");

	result = dns_db_load(db, filename);
	if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
		fatal("can't load %s: %s", filename, isc_result_totext(result));

	result = dns_db_findnode(db, name, ISC_FALSE, &node);
	if (result != ISC_R_SUCCESS)
		fatal("can't find %s node in %s", setname, filename);

	result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey,
102 103
				     0, 0, rdataset, NULL);

104 105 106 107
	if (result == ISC_R_NOTFOUND)
		fatal("no DNSKEY RR for %s in %s", setname, filename);
	else if (result != ISC_R_SUCCESS)
		fatal("dns_db_findrdataset");
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	if (node != NULL)
		dns_db_detachnode(db, &node);
	if (db != NULL)
		dns_db_detach(&db);
	return (result);
}

static isc_result_t
loadkeyset(char *dirname, dns_rdataset_t *rdataset) {
	isc_result_t	 result;
	char		 filename[PATH_MAX + 1];
	isc_buffer_t	 buf;

	dns_rdataset_init(rdataset);

	isc_buffer_init(&buf, filename, sizeof(filename));
	if (dirname != NULL) {
		/* allow room for a trailing slash */
		if (strlen(dirname) >= isc_buffer_availablelength(&buf))
			return (ISC_R_NOSPACE);
		isc_buffer_putstr(&buf, dirname);
		if (dirname[strlen(dirname) - 1] != '/')
			isc_buffer_putstr(&buf, "/");
	}

	if (isc_buffer_availablelength(&buf) < 7)
		return (ISC_R_NOSPACE);
	isc_buffer_putstr(&buf, "keyset-");

	result = dns_name_tofilenametext(name, ISC_FALSE, &buf);
	check_result(result, "dns_name_tofilenametext()");
	if (isc_buffer_availablelength(&buf) == 0)
		return (ISC_R_NOSPACE);
	isc_buffer_putuint8(&buf, 0);

	return (loadsetfromfile(filename, rdataset));
145 146 147
}

static void
148 149
loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
	dns_rdata_t *rdata)
150 151 152 153 154 155 156 157
{
	isc_result_t  result;
	dst_key_t     *key = NULL;
	isc_buffer_t  keyb;
	isc_region_t  r;

	dns_rdata_init(rdata);

158
	isc_buffer_init(&keyb, key_buf, key_buf_size);
159

160 161
	result = dst_key_fromnamedfile(filename, NULL, DST_TYPE_PUBLIC,
				       mctx, &key);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	if (result != ISC_R_SUCCESS)
		fatal("invalid keyfile name %s: %s",
		      filename, isc_result_totext(result));

	if (verbose > 2) {
		char keystr[KEY_FORMATSIZE];

		key_format(key, keystr, sizeof(keystr));
		fprintf(stderr, "%s: %s\n", program, keystr);
	}

	result = dst_key_todns(key, &keyb);
	if (result != ISC_R_SUCCESS)
		fatal("can't decode key");

	isc_buffer_usedregion(&keyb, &r);
	dns_rdata_fromregion(rdata, dst_key_class(key),
			     dns_rdatatype_dnskey, &r);

	rdclass = dst_key_class(key);

	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);
	result = dns_name_copy(dst_key_name(key), name, NULL);
	if (result != ISC_R_SUCCESS)
		fatal("can't copy name");

	dst_key_free(&key);
}

static void
logkey(dns_rdata_t *rdata)
{
	isc_result_t result;
	dst_key_t    *key = NULL;
	isc_buffer_t buf;
198
	char	     keystr[KEY_FORMATSIZE];
199 200 201 202 203 204 205 206 207 208 209 210 211 212

	isc_buffer_init(&buf, rdata->data, rdata->length);
	isc_buffer_add(&buf, rdata->length);
	result = dst_key_fromdns(name, rdclass, &buf, mctx, &key);
	if (result != ISC_R_SUCCESS)
		return;

	key_format(key, keystr, sizeof(keystr));
	fprintf(stderr, "%s: %s\n", program, keystr);

	dst_key_free(&key);
}

static void
213 214
emit(unsigned int dtype, isc_boolean_t showall, char *lookaside,
     dns_rdata_t *rdata)
215
{
216 217 218 219 220 221 222 223 224
	isc_result_t result;
	unsigned char buf[DNS_DS_BUFFERSIZE];
	char text_buf[DST_KEY_MAXTEXTSIZE];
	char name_buf[DNS_NAME_MAXWIRE];
	char class_buf[10];
	isc_buffer_t textb, nameb, classb;
	isc_region_t r;
	dns_rdata_t ds;
	dns_rdata_dnskey_t dnskey;
225 226

	isc_buffer_init(&textb, text_buf, sizeof(text_buf));
227
	isc_buffer_init(&nameb, name_buf, sizeof(name_buf));
228 229 230 231
	isc_buffer_init(&classb, class_buf, sizeof(class_buf));

	dns_rdata_init(&ds);

232 233 234 235 236 237 238
	result = dns_rdata_tostruct(rdata, &dnskey, NULL);
	if (result != ISC_R_SUCCESS)
		fatal("can't convert DNSKEY");

	if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0 && !showall)
		return;

239 240
	result = dns_ds_buildrdata(name, rdata, dtype, buf, &ds);
	if (result != ISC_R_SUCCESS)
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		fatal("can't build record");

	result = dns_name_totext(name, ISC_FALSE, &nameb);
	if (result != ISC_R_SUCCESS)
		fatal("can't print name");

	/* Add lookaside origin, if set */
	if (lookaside != NULL) {
		if (isc_buffer_availablelength(&nameb) < strlen(lookaside))
			fatal("DLV origin '%s' is too long", lookaside);
		isc_buffer_putstr(&nameb, lookaside);
		if (lookaside[strlen(lookaside) - 1] != '.') {
			if (isc_buffer_availablelength(&nameb) < 1)
				fatal("DLV origin '%s' is too long", lookaside);
			isc_buffer_putstr(&nameb, ".");
		}
	}
258 259 260

	result = dns_rdata_totext(&ds, (dns_name_t *) NULL, &textb);
	if (result != ISC_R_SUCCESS)
261
		fatal("can't print rdata");
262 263 264

	result = dns_rdataclass_totext(rdclass, &classb);
	if (result != ISC_R_SUCCESS)
265
		fatal("can't print class");
266

267 268
	isc_buffer_usedregion(&nameb, &r);
	fwrite(r.base, 1, r.length, stdout);
269 270 271 272 273 274

	putchar(' ');

	isc_buffer_usedregion(&classb, &r);
	fwrite(r.base, 1, r.length, stdout);

275 276 277 278
	if (lookaside == NULL)
		printf(" DS ");
	else
		printf(" DLV ");
279 280 281 282 283 284 285 286 287

	isc_buffer_usedregion(&textb, &r);
	fwrite(r.base, 1, r.length, stdout);
	putchar('\n');
}

static void
usage(void) {
	fprintf(stderr, "Usage:\n");
288 289
	fprintf(stderr,	"    %s options [-K dir] keyfile\n\n", program);
	fprintf(stderr, "    %s options [-K dir] [-c class] -s dnsname\n\n",
290
		program);
291 292
	fprintf(stderr, "    %s options -f zonefile (as zone name)\n\n", program);
	fprintf(stderr, "    %s options -f zonefile zonename\n\n", program);
293 294 295
	fprintf(stderr, "Version: %s\n", VERSION);
	fprintf(stderr, "Options:\n");
	fprintf(stderr, "    -v <verbose level>\n");
296 297 298 299
	fprintf(stderr, "    -K <directory>: directory in which to find "
			"key file or keyset file\n");
	fprintf(stderr, "    -a algorithm: digest algorithm "
			"(SHA-1 or SHA-256)\n");
300 301
	fprintf(stderr, "    -1: use SHA-1\n");
	fprintf(stderr, "    -2: use SHA-256\n");
302
	fprintf(stderr, "    -l: add lookaside zone and print DLV records\n");
303 304 305 306 307
	fprintf(stderr, "    -s: read keyset from keyset-<dnsname> file\n");
	fprintf(stderr, "    -c class: rdata class for DS set (default: IN)\n");
	fprintf(stderr, "    -f file: read keyset from zone file\n");
	fprintf(stderr, "    -A: when used with -f, "
			"include all keys in DS set, not just KSKs\n");
308
	fprintf(stderr, "Output: DS or DLV RRs\n");
309 310 311 312 313 314

	exit (-1);
}

int
main(int argc, char **argv) {
315 316
	char		*algname = NULL, *classname = NULL;
	char		*filename = NULL, *dir = NULL, *namestr;
317 318 319 320 321 322
	char		*lookaside = NULL;
	char		*endp;
	int		ch;
	unsigned int	dtype = DNS_DSDIGEST_SHA1;
	isc_boolean_t	both = ISC_TRUE;
	isc_boolean_t	usekeyset = ISC_FALSE;
323
	isc_boolean_t	showall = ISC_FALSE;
324
	isc_result_t	result;
325
	isc_log_t	*log = NULL;
326
	isc_entropy_t	*ectx = NULL;
327
	dns_rdataset_t	rdataset;
328
	dns_rdata_t	rdata;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	dns_rdata_init(&rdata);

	if (argc == 1)
		usage();

	result = isc_mem_create(0, 0, &mctx);
	if (result != ISC_R_SUCCESS)
		fatal("out of memory");

	dns_result_register();

	isc_commandline_errprint = ISC_FALSE;

	while ((ch = isc_commandline_parse(argc, argv,
344
					   "12Aa:c:d:Ff:K:l:sv:h")) != -1) {
345 346 347 348 349 350 351 352 353
		switch (ch) {
		case '1':
			dtype = DNS_DSDIGEST_SHA1;
			both = ISC_FALSE;
			break;
		case '2':
			dtype = DNS_DSDIGEST_SHA256;
			both = ISC_FALSE;
			break;
354 355 356
		case 'A':
			showall = ISC_TRUE;
			break;
357 358 359 360 361 362 363 364
		case 'a':
			algname = isc_commandline_argument;
			both = ISC_FALSE;
			break;
		case 'c':
			classname = isc_commandline_argument;
			break;
		case 'd':
365 366 367 368 369
			fprintf(stderr, "%s: the -d option is deprecated; "
					"use -K\n", program);
			/* fall through */
		case 'K':
			dir = isc_commandline_argument;
Mark Andrews's avatar
Mark Andrews committed
370
			if (strlen(dir) == 0U)
371 372 373 374
				fatal("directory must be non-empty string");
			break;
		case 'f':
			filename = isc_commandline_argument;
375 376 377
			break;
		case 'l':
			lookaside = isc_commandline_argument;
Mark Andrews's avatar
Mark Andrews committed
378
			if (strlen(lookaside) == 0U)
379
				fatal("lookaside must be a non-empty string");
380 381 382 383 384 385 386 387 388
			break;
		case 's':
			usekeyset = ISC_TRUE;
			break;
		case 'v':
			verbose = strtol(isc_commandline_argument, &endp, 0);
			if (*endp != '\0')
				fatal("-v must be followed by a number");
			break;
Francis Dupont's avatar
Francis Dupont committed
389 390 391
		case 'F':
			/* Reserved for FIPS mode */
			/* FALLTHROUGH */
392 393 394 395
		case '?':
			if (isc_commandline_option != '?')
				fprintf(stderr, "%s: invalid argument -%c\n",
					program, isc_commandline_option);
Francis Dupont's avatar
Francis Dupont committed
396
			/* FALLTHROUGH */
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
		case 'h':
			usage();

		default:
			fprintf(stderr, "%s: unhandled option -%c\n",
				program, isc_commandline_option);
			exit(1);
		}
	}

	if (algname != NULL) {
		if (strcasecmp(algname, "SHA1") == 0 ||
		    strcasecmp(algname, "SHA-1") == 0)
			dtype = DNS_DSDIGEST_SHA1;
		else if (strcasecmp(algname, "SHA256") == 0 ||
			 strcasecmp(algname, "SHA-256") == 0)
			dtype = DNS_DSDIGEST_SHA256;
		else
			fatal("unknown algorithm %s", algname);
	}

	rdclass = strtoclass(classname);

420 421 422 423 424 425 426
	if (usekeyset && filename != NULL)
		fatal("cannot use both -s and -f");

	/* When not using -f, -A is implicit */
	if (filename == NULL)
		showall = ISC_TRUE;

Automatic Updater's avatar
Automatic Updater committed
427
	if (argc < isc_commandline_index + 1 && filename == NULL)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		fatal("the key file name was not specified");
	if (argc > isc_commandline_index + 1)
		fatal("extraneous arguments");

	if (ectx == NULL)
		setup_entropy(mctx, NULL, &ectx);
	result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
	if (result != ISC_R_SUCCESS)
		fatal("could not initialize hash");
	result = dst_lib_init(mctx, ectx,
			      ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
	if (result != ISC_R_SUCCESS)
		fatal("could not initialize dst");
	isc_entropy_stopcallbacksources(ectx);

	setup_logging(verbose, mctx, &log);

445 446 447 448 449 450 451 452
	dns_rdataset_init(&rdataset);

	if (usekeyset || filename != NULL) {
		if (argc < isc_commandline_index + 1 && filename != NULL) {
			/* using zone name as the zone file name */
			namestr = filename;
		} else
			namestr = argv[isc_commandline_index];
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467
		result = initname(namestr);
		if (result != ISC_R_SUCCESS)
			fatal("could not initialize name %s", namestr);

		if (usekeyset)
			result = loadkeyset(dir, &rdataset);
		else
			result = loadsetfromfile(filename, &rdataset);

		if (result != ISC_R_SUCCESS)
			fatal("could not load DNSKEY set: %s\n",
			      isc_result_totext(result));

		for (result = dns_rdataset_first(&rdataset);
468
		     result == ISC_R_SUCCESS;
469
		     result = dns_rdataset_next(&rdataset)) {
470
			dns_rdata_init(&rdata);
471
			dns_rdataset_current(&rdataset, &rdata);
472 473 474 475 476

			if (verbose > 2)
				logkey(&rdata);

			if (both) {
477
				emit(DNS_DSDIGEST_SHA1, showall, lookaside,
Automatic Updater's avatar
Automatic Updater committed
478
				     &rdata);
479
				emit(DNS_DSDIGEST_SHA256, showall, lookaside,
Automatic Updater's avatar
Automatic Updater committed
480
				     &rdata);
481
			} else
482
				emit(dtype, showall, lookaside, &rdata);
483 484
		}
	} else {
485 486 487 488
		unsigned char key_buf[DST_KEY_MAXSIZE];

		loadkey(argv[isc_commandline_index], key_buf,
			DST_KEY_MAXSIZE, &rdata);
489 490

		if (both) {
491 492
			emit(DNS_DSDIGEST_SHA1, showall, lookaside, &rdata);
			emit(DNS_DSDIGEST_SHA256, showall, lookaside, &rdata);
493
		} else
494
			emit(dtype, showall, lookaside, &rdata);
495 496
	}

497 498
	if (dns_rdataset_isassociated(&rdataset))
		dns_rdataset_disassociate(&rdataset);
499 500 501 502 503 504 505 506 507
	cleanup_logging(&log);
	dst_lib_destroy();
	isc_hash_destroy();
	cleanup_entropy(&ectx);
	dns_name_destroy();
	if (verbose > 10)
		isc_mem_stats(mctx, stdout);
	isc_mem_destroy(&mctx);

508 509 510 511 512 513
	fflush(stdout);
	if (ferror(stdout)) {
		fprintf(stderr, "write error\n");
		return (1);
	} else
		return (0);
514
}