dnssec-makekeyset.c 11.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Copyright (C) 1999, 2000  Internet Software Consortium.
 * 
 * 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.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM 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.
 */

#include <config.h>

#include <stdlib.h>

#include <isc/commandline.h>
#include <isc/mem.h>
24
#include <isc/string.h>
Brian Wellington's avatar
Brian Wellington committed
25
#include <isc/util.h>
26
27

#include <dns/db.h>
28
29
30
#include <dns/dnssec.h>
#include <dns/fixedname.h>
#include <dns/log.h>
31
32
33
34
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/result.h>
35
#include <dns/secalg.h>
36
37
#include <dns/time.h>

38
#define PROGRAM "dnssec-makekeyset"
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define BUFSIZE 2048

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 int verbose;

static isc_mem_t *mctx = NULL;

static keylist_t keylist;
56

57
static void
58
59
60
61
62
63
64
65
fatal(char *format, ...) {
	va_list args;

	fprintf(stderr, "%s: ", PROGRAM);
	va_start(args, format);
	vfprintf(stderr, format, args);
	va_end(args);
	fprintf(stderr, "\n");
66
67
68
69
70
71
	exit(1);
}

static inline void
check_result(isc_result_t result, char *message) {
	if (result != ISC_R_SUCCESS) {
72
		fatal("%s: %s\n", message, isc_result_totext(result));
73
74
75
76
		exit(1);
	}
}

77
78
79
80
81
/* Not thread-safe! */
static char *
nametostr(dns_name_t *name) {
	isc_buffer_t b;
	isc_region_t r;
82
	isc_result_t result;
83
84
85
	static char data[1025];

	isc_buffer_init(&b, data, sizeof(data));
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	result = dns_name_totext(name, ISC_FALSE, &b);
	check_result(result, "dns_name_totext()");
	isc_buffer_usedregion(&b, &r);
	r.base[r.length] = 0;
	return (char *) r.base;
}

/* Not thread-safe! */
static char *
algtostr(const dns_secalg_t alg) {
	isc_buffer_t b;
	isc_region_t r;
	isc_result_t result;
	static char data[10];

	isc_buffer_init(&b, data, sizeof(data));
	result = dns_secalg_totext(alg, &b);
	check_result(result, "dns_secalg_totext()");
104
105
106
107
108
109
	isc_buffer_usedregion(&b, &r);
	r.base[r.length] = 0;
	return (char *) r.base;
}


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
static isc_stdtime_t
strtotime(char *str, isc_int64_t now, isc_int64_t base) {
	isc_int64_t val, offset;
	isc_result_t result;
	char *endp = "";

	if (str[0] == '+') {
		offset = strtol(str + 1, &endp, 0);
		val = base + offset;
	}
	else if (strncmp(str, "now+", 4) == 0) {
		offset = strtol(str + 4, &endp, 0);
		val = now + offset;
	}
	else {
		result = dns_time64_fromtext(str, &val);
126
127
		if (result != ISC_R_SUCCESS)
			fatal("time %s must be numeric", str);
128
129
	}
	if (*endp != '\0')
130
		fatal("time value %s is invalid", str);
131
132
133
134
135

	return ((isc_stdtime_t) val);
}

static void
136
usage(void) {
137
	fprintf(stderr, "Usage:\n");
138
	fprintf(stderr, "\t%s [options] keys\n", PROGRAM);
139
140
141
142
143
144
145

	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");
146
147
	fprintf(stderr, "\t\tSIG end time  - "
			     "absolute|from start|from now (now + 30 days)\n");
148
149
150
151
152
153
	fprintf(stderr, "\t-t ttl\n");
	fprintf(stderr, "\t-v level:\n");
	fprintf(stderr, "\t\tverbose level (0)\n");

	fprintf(stderr, "\n");

154
155
	fprintf(stderr, "keys:\n");
	fprintf(stderr, "\tkeyfile (Kname+alg+id)\n");
156
157
158
159
160
161
162
163
	exit(0);
}

int
main(int argc, char *argv[]) {
	int i, ch;
	char *startstr = NULL, *endstr = NULL;
	dns_fixedname_t fdomain;
164
	dns_name_t *domain = NULL;
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	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;
	isc_logconfig_t *logconfig;
	keynode_t *keynode;
181
	char *savedname = NULL;
182
183
184
185

	dns_result_register();

	result = isc_mem_create(0, 0, &mctx);
186
187
188
	if (result != ISC_R_SUCCESS)
		fatal("failed to create memory context: %s",
		      isc_result_totext(result));
189
190
191
192
193
194
195
196

	while ((ch = isc_commandline_parse(argc, argv, "s:e:t:v:")) != -1)
	{
		switch (ch) {
		case 's':
			startstr = isc_mem_strdup(mctx,
						  isc_commandline_argument);
			if (startstr == NULL)
197
				fatal("out of memory");
198
199
200
201
202
203
			break;

		case 'e':
			endstr = isc_mem_strdup(mctx,
						isc_commandline_argument);
			if (endstr == NULL)
204
				fatal("out of memory");
205
206
207
208
209
210
			break;

		case 't':
			endp = NULL;
			ttl = strtol(isc_commandline_argument, &endp, 0);
			if (*endp != '\0')
211
				fatal("TTL must be numeric");
212
213
214
215
216
217
			break;

		case 'v':
			endp = NULL;
			verbose = strtol(isc_commandline_argument, &endp, 0);
			if (*endp != '\0')
218
				fatal("verbose level must be numeric");
219
220
221
222
223
224
225
226
227
228
229
			break;

		default:
			usage();

		}
	}

	argc -= isc_commandline_index;
	argv += isc_commandline_index;

230
	if (argc < 1)
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
		usage();

	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;
251
252
		fprintf(stderr, "%s: TTL not specified, assuming 3600\n",
			PROGRAM);
253
254
255
256
257
	}

	if (verbose > 0) {
		RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig)
			      == ISC_R_SUCCESS);
258
		isc_log_setcontext(log);
259
		dns_log_init(log);
260
		dns_log_setcontext(log);
261
262
263
264
265
266
267
268
269
270
271
272
273
		RUNTIME_CHECK(isc_log_usechannel(logconfig, "default_stderr",
						 NULL, NULL) == ISC_R_SUCCESS);
	}

	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++) {
274
275
276
277
278
279
280
281
282
		isc_uint16_t id;
		int alg;
		char *namestr = NULL;

		isc_buffer_init(&b, argv[i], strlen(argv[i]));
		isc_buffer_add(&b, strlen(argv[i]));
		result = dst_key_parsefilename(&b, mctx, &namestr, &id, &alg,
					       NULL);
		if (result != ISC_R_SUCCESS)
283
			fatal("%s is not a valid key filename", argv[i]);
284

285
286
287
288
289
290
291
292
293
294
295
		if (savedname == NULL) {
			savedname = isc_mem_strdup(mctx, namestr);
			if (savedname == NULL)
				fatal("out of memory");
		}
		else {
			if (strcmp(savedname, namestr) != 0)
				fatal("all keys must have the same owner - %s "
				      "and %s do not match",
				      savedname, namestr);
		}
296
297
298
299
300
		if (output == NULL) {
			output = isc_mem_allocate(mctx,
						  strlen(namestr) +
						  strlen("keyset") + 1);
			if (output == NULL)
301
				fatal("out of memory");
302
303
304
305
306
307
308
309
310
311
			strcpy(output, namestr);
			strcat(output, "keyset");
		}
		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);
312
313
314
			if (result != ISC_R_SUCCESS)
				fatal("%s is not a valid name: %s",
				      namestr, isc_result_totext(result));
315
		}
316
		key = NULL;
317
		result = dst_key_fromfile(namestr, id, alg, DST_TYPE_PUBLIC,
318
					  mctx, &key);
Andreas Gustafsson's avatar
style    
Andreas Gustafsson committed
319
		check_result(result, "dst_key_fromfile");
320
321
		if (dst_key_iszonekey(key)) {
			dst_key_t *zonekey = NULL;
322
			result = dst_key_fromfile(namestr, id, alg,
323
324
						  DST_TYPE_PRIVATE, mctx,
						  &zonekey);
325
326
			
			if (result != ISC_R_SUCCESS)
327
328
				fatal("failed to read key %s/%s/%d: %s",
				      namestr, id, algtostr(alg),
329
				      isc_result_totext(result));
330
331
			keynode = isc_mem_get(mctx, sizeof (keynode_t));
			if (keynode == NULL)
332
				fatal("out of memory");
333
334
335
336
337
338
			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)
339
			fatal("out of memory");
340
341
		data = isc_mem_get(mctx, BUFSIZE);
		if (data == NULL)
342
			fatal("out of memory");
343
		isc_buffer_init(&b, data, BUFSIZE);
344
		result = dst_key_todns(key, &b);
345
		if (result != ISC_R_SUCCESS)
David Lawrence's avatar
David Lawrence committed
346
347
			fatal("failed to convert key %s/%s/%d "
			      "to a DNS KEY: %s",
348
349
			      namestr, id, algtostr(alg),
			      isc_result_totext(result));
Bob Halley's avatar
Bob Halley committed
350
		isc_buffer_usedregion(&b, &r);
351
352
353
		dns_rdata_fromregion(rdata, dns_rdataclass_in,
				     dns_rdatatype_key, &r);
		ISC_LIST_APPEND(rdatalist.rdata, rdata, link);
354
		isc_mem_put(mctx, namestr, strlen(namestr) + 1);
355
		dst_key_free(&key);
356
357
	}

358
359
	isc_mem_free(mctx, savedname);

360
361
362
363
364
365
366
367
368
369
370
371
	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,
372
373
			"%s: no private zone key found; not self-signing\n",
			PROGRAM);
374
375
376
377
378
379
	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)
380
			fatal("out of memory");
381
382
		data = isc_mem_get(mctx, BUFSIZE);
		if (data == NULL)
383
			fatal("out of memory");
384
		isc_buffer_init(&b, data, BUFSIZE);
385
		result = dns_dnssec_sign(domain, &rdataset, keynode->key,
386
387
					 &starttime, &endtime, mctx, &b,
					 rdata);
388
		if (result != ISC_R_SUCCESS)
389
			fatal("failed to sign keyset with key %s/%s/%d: %s",
390
			      dst_key_name(keynode->key),
391
			      algtostr(dst_key_alg(keynode->key)),
392
393
			      dst_key_id(keynode->key),
			      isc_result_totext(result));
394
395
396
397
398
399
400
401
402
		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;
	result = dns_db_create(mctx, "rbt", domain, ISC_FALSE,
			       dns_rdataclass_in, 0, NULL, &db);
403
404
	if (result != ISC_R_SUCCESS)
		fatal("failed to create a database for %s", nametostr(domain));
405
406
407
408
409
410
411
412
413
414

	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))
415
416
		dns_db_addrdataset(db, node, version, 0, &sigrdataset, 0,
				   NULL);
417
418
419
420

	dns_db_detachnode(db, &node);
	dns_db_closeversion(db, &version, ISC_TRUE);
	result = dns_db_dump(db, version, output);
421
422
423
	if (result != ISC_R_SUCCESS)
		fatal("failed to write database for %s to %s",
		      nametostr(domain), output);
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

	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);
444
		dst_key_free(&keynode->key);
445
446
447
448
449
450
451
452
453
454
		isc_mem_put(mctx, keynode, sizeof(keynode_t));
	}

	if (log != NULL)
		isc_log_destroy(&log);

	isc_mem_free(mctx, output);
	isc_mem_destroy(&mctx);
	return (0);
}