dnssec-makekeyset.c 11.6 KB
Newer Older
1
/*
2
3
 * Portions Copyright (C) 2000  Internet Software Consortium.
 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
4
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
10
11
12
13
14
15
16
17
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
 * NETWORK ASSOCIATES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR NETWORK
 * ASSOCIATES 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.
18
19
 */

20
/* $Id: dnssec-makekeyset.c,v 1.30 2000/07/31 15:28:12 bwelling Exp $ */
David Lawrence's avatar
David Lawrence committed
21

22
23
24
25
26
#include <config.h>

#include <stdlib.h>

#include <isc/commandline.h>
Brian Wellington's avatar
Brian Wellington committed
27
#include <isc/entropy.h>
28
#include <isc/mem.h>
29
#include <isc/string.h>
Brian Wellington's avatar
Brian Wellington committed
30
#include <isc/util.h>
31
32

#include <dns/db.h>
33
34
35
#include <dns/dnssec.h>
#include <dns/fixedname.h>
#include <dns/log.h>
36
37
38
39
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/result.h>
40
#include <dns/secalg.h>
41
42
#include <dns/time.h>

43
44
45
#include <dst/dst.h>

#include "dnssectool.h"
46

47
48
#define BUFSIZE 2048

David Lawrence's avatar
David Lawrence committed
49
const char *program = "dnssec-makekeyset";
50
51
int verbose;

52
53
54
55
56
57
58
59
60
61
62
typedef struct keynode keynode_t;
struct keynode {
	dst_key_t *key;
	ISC_LINK(keynode_t) link;
};
typedef ISC_LIST(keynode_t) keylist_t;

static isc_stdtime_t starttime = 0, endtime = 0, now;
static int ttl = -1;

static isc_mem_t *mctx = NULL;
Brian Wellington's avatar
Brian Wellington committed
63
static isc_entropy_t *ectx = NULL;
64
65

static keylist_t keylist;
66

67
68
69
70
static isc_stdtime_t
strtotime(char *str, isc_int64_t now, isc_int64_t base) {
	isc_int64_t val, offset;
	isc_result_t result;
David Lawrence's avatar
David Lawrence committed
71
	char *endp;
72
73
74

	if (str[0] == '+') {
		offset = strtol(str + 1, &endp, 0);
David Lawrence's avatar
David Lawrence committed
75
76
		if (*endp != '\0')
			fatal("time value %s is invalid", str);
77
		val = base + offset;
David Lawrence's avatar
David Lawrence committed
78
	} else if (strncmp(str, "now+", 4) == 0) {
79
		offset = strtol(str + 4, &endp, 0);
David Lawrence's avatar
David Lawrence committed
80
81
		if (*endp != '\0')
			fatal("time value %s is invalid", str);
82
		val = now + offset;
David Lawrence's avatar
David Lawrence committed
83
	} else {
84
		result = dns_time64_fromtext(str, &val);
85
86
		if (result != ISC_R_SUCCESS)
			fatal("time %s must be numeric", str);
87
88
89
90
91
92
	}

	return ((isc_stdtime_t) val);
}

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

	fprintf(stderr, "\n");

	fprintf(stderr, "Options: (default value in parenthesis) \n");
	fprintf(stderr, "\t-s YYYYMMDDHHMMSS|+offset:\n");
	fprintf(stderr, "\t\tSIG start time - absolute|offset (now)\n");
	fprintf(stderr, "\t-e YYYYMMDDHHMMSS|+offset|\"now\"+offset]:\n");
103
104
	fprintf(stderr, "\t\tSIG end time  - "
			     "absolute|from start|from now (now + 30 days)\n");
105
	fprintf(stderr, "\t-t ttl\n");
106
107
	fprintf(stderr, "\t-p\n");
	fprintf(stderr, "\t\tuse pseudorandom data (faster but less secure)\n");
108
109
	fprintf(stderr, "\t-r randomdev:\n");
	fprintf(stderr, "\t\ta file containing random data\n");
110
111
112
113
114
	fprintf(stderr, "\t-v level:\n");
	fprintf(stderr, "\t\tverbose level (0)\n");

	fprintf(stderr, "\n");

115
116
	fprintf(stderr, "keys:\n");
	fprintf(stderr, "\tkeyfile (Kname+alg+id)\n");
117
118
119
120
121
122
123
	exit(0);
}

int
main(int argc, char *argv[]) {
	int i, ch;
	char *startstr = NULL, *endstr = NULL;
124
	char *randomfile = NULL;
125
	dns_fixedname_t fdomain;
126
	dns_name_t *domain = NULL;
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
	char *output = NULL;
	char *endp;
	unsigned char *data;
	dns_db_t *db;
	dns_dbnode_t *node;
	dns_dbversion_t *version;
	dst_key_t *key = NULL;
	dns_rdata_t *rdata;
	dns_rdatalist_t rdatalist, sigrdatalist;
	dns_rdataset_t rdataset, sigrdataset;
	isc_result_t result;
	isc_buffer_t b;
	isc_region_t r;
	isc_log_t *log = NULL;
	keynode_t *keynode;
142
	dns_name_t *savedname = NULL;
143
144
	unsigned int eflags;
	isc_boolean_t pseudorandom = ISC_FALSE;
145
146

	result = isc_mem_create(0, 0, &mctx);
147
148
149
	if (result != ISC_R_SUCCESS)
		fatal("failed to create memory context: %s",
		      isc_result_totext(result));
150

Brian Wellington's avatar
Brian Wellington committed
151
152
	dns_result_register();

153
	while ((ch = isc_commandline_parse(argc, argv, "s:e:t:r:v:ph")) != -1)
154
155
156
157
158
159
	{
		switch (ch) {
		case 's':
			startstr = isc_mem_strdup(mctx,
						  isc_commandline_argument);
			if (startstr == NULL)
160
				fatal("out of memory");
161
162
163
164
165
166
			break;

		case 'e':
			endstr = isc_mem_strdup(mctx,
						isc_commandline_argument);
			if (endstr == NULL)
167
				fatal("out of memory");
168
169
170
171
172
173
			break;

		case 't':
			endp = NULL;
			ttl = strtol(isc_commandline_argument, &endp, 0);
			if (*endp != '\0')
174
				fatal("TTL must be numeric");
175
176
			break;

177
178
179
180
181
182
183
		case 'r':
			randomfile = isc_mem_strdup(mctx,
						    isc_commandline_argument);
			if (randomfile == NULL)
				fatal("out of memory");
			break;

184
185
186
187
		case 'v':
			endp = NULL;
			verbose = strtol(isc_commandline_argument, &endp, 0);
			if (*endp != '\0')
188
				fatal("verbose level must be numeric");
189
190
			break;

191
192
193
194
		case 'p':
			pseudorandom = ISC_TRUE;
			break;

Brian Wellington's avatar
Brian Wellington committed
195
		case 'h':
196
197
198
199
200
201
202
203
204
		default:
			usage();

		}
	}

	argc -= isc_commandline_index;
	argv += isc_commandline_index;

205
	if (argc < 1)
206
207
		usage();

208
209
210
	setup_entropy(mctx, randomfile, &ectx);
	if (randomfile != NULL)
		isc_mem_free(mctx, randomfile);
211
212
213
214
	eflags = ISC_ENTROPY_BLOCKING;
	if (!pseudorandom)
		eflags |= ISC_ENTROPY_GOODONLY;
	result = dst_lib_init(mctx, ectx, eflags);
Brian Wellington's avatar
Brian Wellington committed
215
216
217
	if (result != ISC_R_SUCCESS)
		fatal("could not initialize dst");

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	isc_stdtime_get(&now);

	if (startstr != NULL) {
		starttime = strtotime(startstr, now, now);
		isc_mem_free(mctx, startstr);
	}
	else
		starttime = now;

	if (endstr != NULL) {
		endtime = strtotime(endstr, now, starttime);
		isc_mem_free(mctx, endstr);
	}
	else
		endtime = starttime + (30 * 24 * 60 * 60);

	if (ttl == -1) {
		ttl = 3600;
236
		fprintf(stderr, "%s: TTL not specified, assuming 3600\n",
237
			program);
238
239
	}

240
	setup_logging(verbose, mctx, &log);
241
242
243
244
245
246
247
248
249
250

	dns_rdatalist_init(&rdatalist);
	rdatalist.rdclass = dns_rdataclass_in;
	rdatalist.type = dns_rdatatype_key;
	rdatalist.covers = 0;
	rdatalist.ttl = ttl;

	ISC_LIST_INIT(keylist);

	for (i = 0; i < argc; i++) {
251
		char namestr[1025];
Brian Wellington's avatar
Brian Wellington committed
252
253
254
		key = NULL;
		result = dst_key_fromnamedfile(argv[i], DST_TYPE_PUBLIC,
					       mctx, &key);
255
		if (result != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
256
257
258
259
			fatal("error loading key from %s", argv[i]);

		strncpy(namestr, nametostr(dst_key_name(key)),
			sizeof(namestr) - 1);
260
		namestr[sizeof(namestr) - 1] = 0;
261

262
		if (savedname == NULL) {
Brian Wellington's avatar
Brian Wellington committed
263
264
265
266
267
268
269
270
271
272
			savedname = isc_mem_get(mctx, sizeof(dns_name_t));
			if (savedname == NULL)
				fatal("out of memory");
			dns_name_init(savedname, NULL);
			result = dns_name_dup(dst_key_name(key), mctx,
					      savedname);
			if (result != ISC_R_SUCCESS)
				fatal("out of memory");
		} else {
			if (!dns_name_equal(savedname, dst_key_name(key)) != 0)
273
274
				fatal("all keys must have the same owner - %s "
				      "and %s do not match",
275
				      nametostr(savedname), namestr);
276
		}
277
278
		if (output == NULL) {
			output = isc_mem_allocate(mctx,
279
280
						  strlen("keyset-") +
						  strlen(namestr) + 1);
281
			if (output == NULL)
282
				fatal("out of memory");
283
284
			strcpy(output, "keyset-");
			strcat(output, namestr);
285
286
287
288
289
290
291
292
		}
		if (domain == NULL) {
			dns_fixedname_init(&fdomain);
			domain = dns_fixedname_name(&fdomain);
			isc_buffer_init(&b, namestr, strlen(namestr));
			isc_buffer_add(&b, strlen(namestr));
			result = dns_name_fromtext(domain, &b, dns_rootname,
						   ISC_FALSE, NULL);
293
294
295
			if (result != ISC_R_SUCCESS)
				fatal("%s is not a valid name: %s",
				      namestr, isc_result_totext(result));
296
		}
297
298
		if (dst_key_iszonekey(key)) {
			dst_key_t *zonekey = NULL;
Brian Wellington's avatar
Brian Wellington committed
299
300
301
			result = dst_key_fromnamedfile(argv[i],
						       DST_TYPE_PRIVATE,
						       mctx, &zonekey);
302
			if (result != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
303
304
				fatal("failed to read key %s: %s",
				      argv[i], isc_result_totext(result));
305
306
			keynode = isc_mem_get(mctx, sizeof (keynode_t));
			if (keynode == NULL)
307
				fatal("out of memory");
308
309
310
311
312
313
			keynode->key = zonekey;
			ISC_LINK_INIT(keynode, link);
			ISC_LIST_APPEND(keylist, keynode, link);
		}
		rdata = isc_mem_get(mctx, sizeof(dns_rdata_t));
		if (rdata == NULL)
314
			fatal("out of memory");
315
316
		data = isc_mem_get(mctx, BUFSIZE);
		if (data == NULL)
317
			fatal("out of memory");
318
		isc_buffer_init(&b, data, BUFSIZE);
319
		result = dst_key_todns(key, &b);
320
		if (result != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
321
322
			fatal("failed to convert key %s to a DNS KEY: %s",
			      argv[i], isc_result_totext(result));
Bob Halley's avatar
Bob Halley committed
323
		isc_buffer_usedregion(&b, &r);
324
325
326
		dns_rdata_fromregion(rdata, dns_rdataclass_in,
				     dns_rdatatype_key, &r);
		ISC_LIST_APPEND(rdatalist.rdata, rdata, link);
327
		dst_key_free(&key);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	}

	dns_rdataset_init(&rdataset);
	result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
	check_result(result, "dns_rdatalist_tordataset()");

	dns_rdatalist_init(&sigrdatalist);
	sigrdatalist.rdclass = dns_rdataclass_in;
	sigrdatalist.type = dns_rdatatype_sig;
	sigrdatalist.covers = dns_rdatatype_key;
	sigrdatalist.ttl = ttl;

	if (ISC_LIST_EMPTY(keylist))
		fprintf(stderr,
342
			"%s: no private zone key found; not self-signing\n",
343
			program);
344
345
346
347
348
349
	for (keynode = ISC_LIST_HEAD(keylist);
	     keynode != NULL;
	     keynode = ISC_LIST_NEXT(keynode, link))
	{
		rdata = isc_mem_get(mctx, sizeof(dns_rdata_t));
		if (rdata == NULL)
350
			fatal("out of memory");
351
352
		data = isc_mem_get(mctx, BUFSIZE);
		if (data == NULL)
353
			fatal("out of memory");
354
		isc_buffer_init(&b, data, BUFSIZE);
355
		result = dns_dnssec_sign(domain, &rdataset, keynode->key,
356
357
					 &starttime, &endtime, mctx, &b,
					 rdata);
358
		isc_entropy_stopcallbacksources(ectx);
359
		if (result != ISC_R_SUCCESS)
360
			fatal("failed to sign keyset with key %s/%s/%d: %s",
361
			      nametostr(dst_key_name(keynode->key)),
362
			      algtostr(dst_key_alg(keynode->key)),
363
364
			      dst_key_id(keynode->key),
			      isc_result_totext(result));
365
366
367
368
369
370
371
		ISC_LIST_APPEND(sigrdatalist.rdata, rdata, link);
		dns_rdataset_init(&sigrdataset);
		result = dns_rdatalist_tordataset(&sigrdatalist, &sigrdataset);
		check_result(result, "dns_rdatalist_tordataset()");
	}

	db = NULL;
372
	result = dns_db_create(mctx, "rbt", domain, dns_dbtype_zone,
373
			       dns_rdataclass_in, 0, NULL, &db);
374
375
	if (result != ISC_R_SUCCESS)
		fatal("failed to create a database for %s", nametostr(domain));
376
377
378
379
380
381
382
383
384
385

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

	node = NULL;
	result = dns_db_findnode(db, domain, ISC_TRUE, &node);
	check_result(result, "dns_db_findnode()");

	dns_db_addrdataset(db, node, version, 0, &rdataset, 0, NULL);
	if (!ISC_LIST_EMPTY(keylist))
386
387
		dns_db_addrdataset(db, node, version, 0, &sigrdataset, 0,
				   NULL);
388
389
390
391

	dns_db_detachnode(db, &node);
	dns_db_closeversion(db, &version, ISC_TRUE);
	result = dns_db_dump(db, version, output);
392
393
394
	if (result != ISC_R_SUCCESS)
		fatal("failed to write database for %s to %s",
		      nametostr(domain), output);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

	dns_db_detach(&db);

	dns_rdataset_disassociate(&rdataset);
	while (!ISC_LIST_EMPTY(rdatalist.rdata)) {
		rdata = ISC_LIST_HEAD(rdatalist.rdata);
		ISC_LIST_UNLINK(rdatalist.rdata, rdata, link);
		isc_mem_put(mctx, rdata->data, BUFSIZE);
		isc_mem_put(mctx, rdata, sizeof *rdata);
	}
	while (!ISC_LIST_EMPTY(sigrdatalist.rdata)) {
		rdata = ISC_LIST_HEAD(sigrdatalist.rdata);
		ISC_LIST_UNLINK(sigrdatalist.rdata, rdata, link);
		isc_mem_put(mctx, rdata->data, BUFSIZE);
		isc_mem_put(mctx, rdata, sizeof *rdata);
	}

	while (!ISC_LIST_EMPTY(keylist)) {
		keynode = ISC_LIST_HEAD(keylist);
		ISC_LIST_UNLINK(keylist, keynode, link);
415
		dst_key_free(&keynode->key);
416
417
418
		isc_mem_put(mctx, keynode, sizeof(keynode_t));
	}

Brian Wellington's avatar
Brian Wellington committed
419
420
421
422
423
	if (savedname != NULL) {
		dns_name_free(savedname, mctx);
		isc_mem_put(mctx, savedname, sizeof(dns_name_t));
	}

424
425
	if (log != NULL)
		isc_log_destroy(&log);
Brian Wellington's avatar
Brian Wellington committed
426
	cleanup_entropy(&ectx);
427
428

	isc_mem_free(mctx, output);
Brian Wellington's avatar
Brian Wellington committed
429
	dst_lib_destroy();
430
431
	if (verbose > 10)
		isc_mem_stats(mctx, stdout);
432
433
434
	isc_mem_destroy(&mctx);
	return (0);
}