dnssec-signkey.c 13 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Portions Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Portions Copyright (C) 2000-2003  Internet Software Consortium.
4
 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
5
 *
6
7
8
 * Permission to use, copy, modify, and 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.
9
 *
Mark Andrews's avatar
Mark Andrews committed
10
11
12
13
14
15
16
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES 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
18
 */

Mark Andrews's avatar
Mark Andrews committed
19
/* $Id: dnssec-signkey.c,v 1.65 2005/04/29 00:22:25 marka Exp $ */
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

/*! \file */

/*% dnssec-signkey - DNSSEC key set signing tool
 * 
 * \section dnssec-signkey-synopsis SYNOPSIS
 * \par
 *        dnssec-signkey [ -a ]  [ -c class ]  [ -s start-time ]  [ -e end-time ]
 *        [ -h ]  [ -p ]  [ -r randomdev ]  [ -v level ]  keyset key...
 * 
 * \section dnssec-signkey-description DESCRIPTION
 * \par
 *        dnssec-signkey signs a keyset. Typically the keyset will be for a child
 *        zone,  and  will  have  been  generated by dnssec-makekeyset. The child
 *        zone's keyset is signed with the zone keys for  its  parent  zone.  The
 *        output  file  is  of  the  form signedkey-nnnn., where nnnn is the zone
 *        name.
 * 
 * \link org.isc.doc.0038 More ... \endlink
 */

/** \page org.isc.doc.0038 dnssec-signkey
 *  \htmlinclude org.isc.doc.0038.html
 */
David Lawrence's avatar
David Lawrence committed
44

45
46
47
48
#include <config.h>

#include <stdlib.h>

49
#include <isc/string.h>
50
#include <isc/commandline.h>
Brian Wellington's avatar
Brian Wellington committed
51
#include <isc/entropy.h>
52
#include <isc/mem.h>
53
#include <isc/print.h>
Brian Wellington's avatar
Brian Wellington committed
54
#include <isc/util.h>
55
56

#include <dns/db.h>
57
#include <dns/dbiterator.h>
Brian Wellington's avatar
Brian Wellington committed
58
#include <dns/diff.h>
59
#include <dns/dnssec.h>
60
#include <dns/fixedname.h>
61
#include <dns/log.h>
62
#include <dns/rdata.h>
63
#include <dns/rdataclass.h>
64
#include <dns/rdataset.h>
65
#include <dns/rdatasetiter.h>
66
67
#include <dns/rdatastruct.h>
#include <dns/result.h>
68
#include <dns/secalg.h>
69

70
71
72
73
#include <dst/dst.h>

#include "dnssectool.h"

David Lawrence's avatar
David Lawrence committed
74
const char *program = "dnssec-signkey";
75
int verbose;
76

77
78
79
80
81
82
83
84
typedef struct keynode keynode_t;
struct keynode {
	dst_key_t *key;
	isc_boolean_t verified;
	ISC_LINK(keynode_t) link;
};
typedef ISC_LIST(keynode_t) keylist_t;

85
static isc_stdtime_t starttime = 0, endtime = 0, now;
86
87

static isc_mem_t *mctx = NULL;
Brian Wellington's avatar
Brian Wellington committed
88
static isc_entropy_t *ectx = NULL;
89
90
91
static keylist_t keylist;

static void
92
usage(void) {
93
	fprintf(stderr, "Usage:\n");
94
	fprintf(stderr, "\t%s [options] keyset keys\n", program);
95
96
97

	fprintf(stderr, "\n");

98
99
	fprintf(stderr, "Version: %s\n", VERSION);

100
	fprintf(stderr, "Options: (default value in parenthesis) \n");
101
102
	fprintf(stderr, "\t-a\n");
	fprintf(stderr, "\t\tverify generated signatures\n");
103
	fprintf(stderr, "\t-c class (IN)\n");
104
105
106
107
108
	fprintf(stderr, "\t-s YYYYMMDDHHMMSS|+offset:\n");
	fprintf(stderr, "\t\tSIG start time - absolute|offset (from keyset)\n");
	fprintf(stderr, "\t-e YYYYMMDDHHMMSS|+offset|\"now\"+offset]:\n");
	fprintf(stderr, "\t\tSIG end time  - absolute|from start|from now "
		"(from keyset)\n");
109
110
	fprintf(stderr, "\t-v level:\n");
	fprintf(stderr, "\t\tverbose level (0)\n");
111
112
	fprintf(stderr, "\t-p\n");
	fprintf(stderr, "\t\tuse pseudorandom data (faster but less secure)\n");
113
114
	fprintf(stderr, "\t-r randomdev:\n");
	fprintf(stderr, "\t\ta file containing random data\n");
115
116
117
118

	fprintf(stderr, "\n");

	fprintf(stderr, "keyset:\n");
119
	fprintf(stderr, "\tfile with keyset to be signed (keyset-<name>)\n");
120
	fprintf(stderr, "keys:\n");
121
	fprintf(stderr, "\tkeyfile (Kname+alg+tag)\n");
122
123
124
125

	fprintf(stderr, "\n");
	fprintf(stderr, "Output:\n");
	fprintf(stderr, "\tsigned keyset (signedkey-<name>)\n");
126
127
128
129
130
131
	exit(0);
}

static void
loadkeys(dns_name_t *name, dns_rdataset_t *rdataset) {
	dst_key_t *key;
132
	dns_rdata_t rdata = DNS_RDATA_INIT;
133
134
135
136
137
138
139
	keynode_t *keynode;
	isc_result_t result;

	ISC_LIST_INIT(keylist);
	result = dns_rdataset_first(rdataset);
	check_result(result, "dns_rdataset_first");
	for (; result == ISC_R_SUCCESS; result = dns_rdataset_next(rdataset)) {
140
		dns_rdata_reset(&rdata);
141
142
143
144
145
		dns_rdataset_current(rdataset, &rdata);
		key = NULL;
		result = dns_dnssec_keyfromrdata(name, &rdata, mctx, &key);
		if (result != ISC_R_SUCCESS)
			continue;
Brian Wellington's avatar
Brian Wellington committed
146
147
		if (!dst_key_iszonekey(key)) {
			dst_key_free(&key);
148
			continue;
Brian Wellington's avatar
Brian Wellington committed
149
		}
Andreas Gustafsson's avatar
Andreas Gustafsson committed
150
		keynode = isc_mem_get(mctx, sizeof(keynode_t));
151
		if (keynode == NULL)
152
			fatal("out of memory");
153
154
		keynode->key = key;
		keynode->verified = ISC_FALSE;
155
		ISC_LIST_INITANDAPPEND(keylist, keynode, link);
156
	}
157
158
	if (result != ISC_R_NOMORE)
		fatal("failure traversing key list");
159
160
161
}

static dst_key_t *
162
findkey(dns_rdata_rrsig_t *sig) {
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	keynode_t *keynode;
	for (keynode = ISC_LIST_HEAD(keylist);
	     keynode != NULL;
	     keynode = ISC_LIST_NEXT(keynode, link))
	{
		if (dst_key_id(keynode->key) == sig->keyid &&
		    dst_key_alg(keynode->key) == sig->algorithm) {
			keynode->verified = ISC_TRUE;
			return (keynode->key);
		}
	}
	fatal("signature generated by non-zone or missing key");
	return (NULL);
}

int
main(int argc, char *argv[]) {
	int i, ch;
181
	char *startstr = NULL, *endstr = NULL, *classname = NULL;
182
183
184
185
186
	char tdomain[1025];
	dns_fixedname_t fdomain;
	dns_name_t *domain;
	char *output = NULL;
	char *endp;
Brian Wellington's avatar
Brian Wellington committed
187
	unsigned char data[65536];
188
189
190
	dns_db_t *db;
	dns_dbnode_t *node;
	dns_dbversion_t *version;
Brian Wellington's avatar
Brian Wellington committed
191
192
	dns_diff_t diff;
	dns_difftuple_t *tuple;
193
194
	dns_dbiterator_t *dbiter;
	dns_rdatasetiter_t *rdsiter;
195
	dst_key_t *key = NULL;
Brian Wellington's avatar
Brian Wellington committed
196
	dns_rdata_t rdata = DNS_RDATA_INIT;
197
	dns_rdata_t sigrdata = DNS_RDATA_INIT;
Brian Wellington's avatar
Brian Wellington committed
198
	dns_rdataset_t rdataset, sigrdataset;
199
	dns_rdata_rrsig_t sig;
200
201
202
203
	isc_result_t result;
	isc_buffer_t b;
	isc_log_t *log = NULL;
	keynode_t *keynode;
204
205
	isc_boolean_t pseudorandom = ISC_FALSE;
	unsigned int eflags;
206
	dns_rdataclass_t rdclass;
Brian Wellington's avatar
Brian Wellington committed
207
208
	isc_boolean_t tryverify = ISC_FALSE;
	isc_boolean_t settime = ISC_FALSE;
209
210
211
212

	result = isc_mem_create(0, 0, &mctx);
	check_result(result, "isc_mem_create()");

Brian Wellington's avatar
Brian Wellington committed
213
214
	dns_result_register();

215
	while ((ch = isc_commandline_parse(argc, argv, "ac:s:e:pr:v:h")) != -1)
216
217
	{
		switch (ch) {
218
219
220
		case 'a':
			tryverify = ISC_TRUE;
			break;
221
222
223
224
		case 'c':
			classname = isc_commandline_argument;
			break;

225
226
227
228
229
230
231
232
		case 's':
			startstr = isc_commandline_argument;
			break;
						
		case 'e':
			endstr = isc_commandline_argument;
			break;

233
234
235
236
		case 'p':
			pseudorandom = ISC_TRUE;
			break;

237
		case 'r':
238
			setup_entropy(mctx, isc_commandline_argument, &ectx);
239
240
			break;

241
242
243
244
		case 'v':
			endp = NULL;
			verbose = strtol(isc_commandline_argument, &endp, 0);
			if (*endp != '\0')
245
				fatal("verbose level must be numeric");
246
247
			break;

248
		case 'h':
249
250
251
252
253
254
255
256
257
258
259
260
		default:
			usage();

		}
	}

	argc -= isc_commandline_index;
	argv += isc_commandline_index;

	if (argc < 2)
		usage();

261
	rdclass = strtoclass(classname);
262

263
264
	if (ectx == NULL)
		setup_entropy(mctx, NULL, &ectx);
265
266
267
268
	eflags = ISC_ENTROPY_BLOCKING;
	if (!pseudorandom)
		eflags |= ISC_ENTROPY_GOODONLY;
	result = dst_lib_init(mctx, ectx, eflags);
Brian Wellington's avatar
Brian Wellington committed
269
	if (result != ISC_R_SUCCESS)
270
271
		fatal("could not initialize dst: %s", 
		      isc_result_totext(result));
Brian Wellington's avatar
Brian Wellington committed
272

273
274
	isc_stdtime_get(&now);

275
276
277
278
	if ((startstr == NULL || endstr == NULL) &&
	    !(startstr == NULL && endstr == NULL))
		fatal("if -s or -e is specified, both must be");

Brian Wellington's avatar
Brian Wellington committed
279
280
281
282
283
284
	if (startstr != NULL) {
		starttime = strtotime(startstr, now, now);
		endtime = strtotime(endstr, now, starttime);
		settime = ISC_TRUE;
	}

285
	setup_logging(verbose, mctx, &log);
286

287
	if (strlen(argv[0]) < 8U || strncmp(argv[0], "keyset-", 7) != 0)
288
		fatal("keyset file '%s' must start with keyset-", argv[0]);
289

290
291
292
293
294
295
296
297
298
299
	db = NULL;
	result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
			       rdclass, 0, NULL, &db);
	check_result(result, "dns_db_create()");

	result = dns_db_load(db, argv[0]);
	if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
		fatal("failed to load database from '%s': %s", argv[0],
		      isc_result_totext(result));

300
301
	dns_fixedname_init(&fdomain);
	domain = dns_fixedname_name(&fdomain);
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

	dbiter = NULL;
	result = dns_db_createiterator(db, ISC_FALSE, &dbiter);
	check_result(result, "dns_db_createiterator()");

	result = dns_dbiterator_first(dbiter);
	check_result(result, "dns_dbiterator_first()");
	while (result == ISC_R_SUCCESS) {
		node = NULL;
		dns_dbiterator_current(dbiter, &node, domain);
		rdsiter = NULL;
		result = dns_db_allrdatasets(db, node, NULL, 0, &rdsiter);
		check_result(result, "dns_db_allrdatasets()");
		result = dns_rdatasetiter_first(rdsiter);
		dns_rdatasetiter_destroy(&rdsiter);
		if (result == ISC_R_SUCCESS)
			break;
		dns_db_detachnode(db, &node);
		result = dns_dbiterator_next(dbiter);
	}
	dns_dbiterator_destroy(&dbiter);
323
	if (result != ISC_R_SUCCESS)
324
325
		fatal("failed to find data in keyset file");

326
	isc_buffer_init(&b, tdomain, sizeof(tdomain) - 1);
327
328
329
	result = dns_name_tofilenametext(domain, ISC_FALSE, &b);
	check_result(result, "dns_name_tofilenametext()");
	isc_buffer_putuint8(&b, 0);
330
331

	output = isc_mem_allocate(mctx,
332
				  strlen("signedkey-") + strlen(tdomain) + 1);
333
	if (output == NULL)
334
		fatal("out of memory");
Brian Wellington's avatar
Brian Wellington committed
335
	sprintf(output, "signedkey-%s", tdomain);
336
337
338
339
340
341

	version = NULL;
	dns_db_newversion(db, &version);

	dns_rdataset_init(&rdataset);
	dns_rdataset_init(&sigrdataset);
342
	result = dns_db_findrdataset(db, node, version, dns_rdatatype_dnskey, 0,
343
				     0, &rdataset, &sigrdataset);
344
345
	if (result != ISC_R_SUCCESS) {
		char domainstr[DNS_NAME_FORMATSIZE];
Andreas Gustafsson's avatar
Andreas Gustafsson committed
346
		dns_name_format(domain, domainstr, sizeof(domainstr));
347
		fatal("failed to find rdataset '%s KEY': %s",
348
349
		      domainstr, isc_result_totext(result));
	}
350
351
352

	loadkeys(domain, &rdataset);

Brian Wellington's avatar
Brian Wellington committed
353
354
	dns_diff_init(mctx, &diff);

355
356
357
	if (!dns_rdataset_isassociated(&sigrdataset))
		fatal("no SIG KEY set present");

358
359
360
361
362
	result = dns_rdataset_first(&sigrdataset);
	check_result(result, "dns_rdataset_first()");
	do {
		dns_rdataset_current(&sigrdataset, &sigrdata);
		result = dns_rdata_tostruct(&sigrdata, &sig, mctx);
363
		check_result(result, "dns_rdata_tostruct()");
364
365
366
		key = findkey(&sig);
		result = dns_dnssec_verify(domain, &rdataset, key,
					   ISC_TRUE, mctx, &sigrdata);
367
368
		if (result != ISC_R_SUCCESS) {
			char keystr[KEY_FORMATSIZE];
Andreas Gustafsson's avatar
Andreas Gustafsson committed
369
			key_format(key, keystr, sizeof(keystr));
370
371
372
			fatal("signature by key '%s' did not verify: %s",
			      keystr, isc_result_totext(result));
		}
Brian Wellington's avatar
Brian Wellington committed
373
374
375
376
377
		if (!settime) {
			starttime = sig.timesigned;
			endtime = sig.timeexpire;
			settime = ISC_TRUE;
		}
378
		dns_rdata_freestruct(&sig);
Brian Wellington's avatar
Brian Wellington committed
379
		dns_rdata_reset(&sigrdata);
380
381
382
383
384
385
386
		result = dns_rdataset_next(&sigrdataset);
	} while (result == ISC_R_SUCCESS);

	for (keynode = ISC_LIST_HEAD(keylist);
	     keynode != NULL;
	     keynode = ISC_LIST_NEXT(keynode, link))
		if (!keynode->verified)
387
			fatal("not all zone keys self signed the key set");
388
389
390
391
392
393

	argc -= 1;
	argv += 1;

	for (i = 0; i < argc; i++) {
		key = NULL;
394
395
396
		result = dst_key_fromnamedfile(argv[i],
					       DST_TYPE_PUBLIC |
					       DST_TYPE_PRIVATE,
Brian Wellington's avatar
Brian Wellington committed
397
					       mctx, &key);
398
		if (result != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
399
400
			fatal("failed to read key %s from disk: %s",
			      argv[i], isc_result_totext(result));
401

Brian Wellington's avatar
Brian Wellington committed
402
403
		dns_rdata_reset(&rdata);
		isc_buffer_init(&b, data, sizeof(data));
404
		result = dns_dnssec_sign(domain, &rdataset, key,
405
					 &starttime, &endtime,
Brian Wellington's avatar
Brian Wellington committed
406
					 mctx, &b, &rdata);
407
		isc_entropy_stopcallbacksources(ectx);
408
409
		if (result != ISC_R_SUCCESS) {
			char keystr[KEY_FORMATSIZE];
Andreas Gustafsson's avatar
Andreas Gustafsson committed
410
			key_format(key, keystr, sizeof(keystr));
411
412
413
			fatal("key '%s' failed to sign data: %s",
			      keystr, isc_result_totext(result));
		}
414
415
		if (tryverify) {
			result = dns_dnssec_verify(domain, &rdataset, key,
Brian Wellington's avatar
Brian Wellington committed
416
						   ISC_TRUE, mctx, &rdata);
417
418
			if (result != ISC_R_SUCCESS) {
				char keystr[KEY_FORMATSIZE];
Andreas Gustafsson's avatar
Andreas Gustafsson committed
419
				key_format(key, keystr, sizeof(keystr));
420
421
422
423
424
				fatal("signature from key '%s' failed to "
				      "verify: %s",
				      keystr, isc_result_totext(result));
			}
		}
Brian Wellington's avatar
Brian Wellington committed
425
426
427
428
429
430
		tuple = NULL;
		result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD,
					      domain, rdataset.ttl,
					      &rdata, &tuple);
		check_result(result, "dns_difftuple_create");
		dns_diff_append(&diff, &tuple);
431
		dst_key_free(&key);
432
433
	}

434
435
	result = dns_db_deleterdataset(db, node, version, dns_rdatatype_rrsig,
				       dns_rdatatype_dnskey);
Brian Wellington's avatar
Brian Wellington committed
436
	check_result(result, "dns_db_deleterdataset");
437

Brian Wellington's avatar
Brian Wellington committed
438
439
440
	result = dns_diff_apply(&diff, db, version);
	check_result(result, "dns_diff_apply");
	dns_diff_clear(&diff);
441
442
443
444

	dns_db_detachnode(db, &node);
	dns_db_closeversion(db, &version, ISC_TRUE);
	result = dns_db_dump(db, version, output);
445
446
447
	if (result != ISC_R_SUCCESS)
		fatal("failed to write database to '%s': %s",
		      output, isc_result_totext(result));
448

449
450
	printf("%s\n", output);

451
	dns_rdataset_disassociate(&rdataset);
Brian Wellington's avatar
Brian Wellington committed
452
	dns_rdataset_disassociate(&sigrdataset);
453
454
455
456
457
458

	dns_db_detach(&db);

	while (!ISC_LIST_EMPTY(keylist)) {
		keynode = ISC_LIST_HEAD(keylist);
		ISC_LIST_UNLINK(keylist, keynode, link);
459
		dst_key_free(&keynode->key);
460
461
462
		isc_mem_put(mctx, keynode, sizeof(keynode_t));
	}

Brian Wellington's avatar
Brian Wellington committed
463
	cleanup_logging(&log);
464

Brian Wellington's avatar
Brian Wellington committed
465
	isc_mem_free(mctx, output);
Brian Wellington's avatar
Brian Wellington committed
466
	cleanup_entropy(&ectx);
Brian Wellington's avatar
Brian Wellington committed
467
	dst_lib_destroy();
468
469
	if (verbose > 10)
		isc_mem_stats(mctx, stdout);
470
471
472
	isc_mem_destroy(&mctx);
	return (0);
}