1 /*
2  * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  *
11  * Portions Copyright (C) Network Associates, Inc.
12  *
13  * Permission to use, copy, modify, and/or distribute this software for any
14  * purpose with or without fee is hereby granted, provided that the above
15  * copyright notice and this permission notice appear in all copies.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
18  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
20  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
23  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  */
25 
26 /*! \file */
27 
28 #include <ctype.h>
29 #include <inttypes.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 #include <isc/attributes.h>
35 #include <isc/buffer.h>
36 #include <isc/commandline.h>
37 #include <isc/mem.h>
38 #include <isc/print.h>
39 #include <isc/region.h>
40 #include <isc/result.h>
41 #include <isc/string.h>
42 #include <isc/util.h>
43 
44 #include <dns/dnssec.h>
45 #include <dns/fixedname.h>
46 #include <dns/kasp.h>
47 #include <dns/keyvalues.h>
48 #include <dns/log.h>
49 #include <dns/name.h>
50 #include <dns/rdataclass.h>
51 #include <dns/secalg.h>
52 
53 #include <dst/dst.h>
54 
55 #include <isccfg/cfg.h>
56 #include <isccfg/grammar.h>
57 #include <isccfg/kaspconf.h>
58 #include <isccfg/namedconf.h>
59 
60 #include "dnssectool.h"
61 
62 #define MAX_RSA 4096 /* should be long enough... */
63 
64 const char *program = "dnssec-keygen";
65 
66 isc_log_t *lctx = NULL;
67 
68 ISC_NORETURN static void
69 usage(void);
70 
71 static void
72 progress(int p);
73 
74 struct keygen_ctx {
75 	const char *predecessor;
76 	const char *policy;
77 	const char *configfile;
78 	const char *directory;
79 	char *algname;
80 	char *nametype;
81 	char *type;
82 	int generator;
83 	int protocol;
84 	int size;
85 	int signatory;
86 	dns_rdataclass_t rdclass;
87 	int options;
88 	int dbits;
89 	dns_ttl_t ttl;
90 	uint16_t kskflag;
91 	uint16_t revflag;
92 	dns_secalg_t alg;
93 	/* timing data */
94 	int prepub;
95 	isc_stdtime_t now;
96 	isc_stdtime_t publish;
97 	isc_stdtime_t activate;
98 	isc_stdtime_t inactive;
99 	isc_stdtime_t revokekey;
100 	isc_stdtime_t deltime;
101 	isc_stdtime_t syncadd;
102 	isc_stdtime_t syncdel;
103 	bool setpub;
104 	bool setact;
105 	bool setinact;
106 	bool setrev;
107 	bool setdel;
108 	bool setsyncadd;
109 	bool setsyncdel;
110 	bool unsetpub;
111 	bool unsetact;
112 	bool unsetinact;
113 	bool unsetrev;
114 	bool unsetdel;
115 	/* how to generate the key */
116 	bool setttl;
117 	bool use_nsec3;
118 	bool genonly;
119 	bool showprogress;
120 	bool quiet;
121 	bool oldstyle;
122 	/* state */
123 	time_t lifetime;
124 	bool ksk;
125 	bool zsk;
126 };
127 
128 typedef struct keygen_ctx keygen_ctx_t;
129 
130 static void
usage(void)131 usage(void) {
132 	fprintf(stderr, "Usage:\n");
133 	fprintf(stderr, "    %s [options] name\n\n", program);
134 	fprintf(stderr, "Version: %s\n", PACKAGE_VERSION);
135 	fprintf(stderr, "    name: owner of the key\n");
136 	fprintf(stderr, "Options:\n");
137 	fprintf(stderr, "    -K <directory>: write keys into directory\n");
138 	fprintf(stderr, "    -k <policy>: generate keys for dnssec-policy\n");
139 	fprintf(stderr, "    -l <file>: configuration file with dnssec-policy "
140 			"statement\n");
141 	fprintf(stderr, "    -a <algorithm>:\n");
142 	fprintf(stderr, "        RSASHA1 | NSEC3RSASHA1 |\n");
143 	fprintf(stderr, "        RSASHA256 | RSASHA512 |\n");
144 	fprintf(stderr, "        ECDSAP256SHA256 | ECDSAP384SHA384 |\n");
145 	fprintf(stderr, "        ED25519 | ED448 | DH\n");
146 	fprintf(stderr, "    -3: use NSEC3-capable algorithm\n");
147 	fprintf(stderr, "    -b <key size in bits>:\n");
148 	fprintf(stderr, "        RSASHA1:\t[1024..%d]\n", MAX_RSA);
149 	fprintf(stderr, "        NSEC3RSASHA1:\t[1024..%d]\n", MAX_RSA);
150 	fprintf(stderr, "        RSASHA256:\t[1024..%d]\n", MAX_RSA);
151 	fprintf(stderr, "        RSASHA512:\t[1024..%d]\n", MAX_RSA);
152 	fprintf(stderr, "        DH:\t\t[128..4096]\n");
153 	fprintf(stderr, "        ECDSAP256SHA256:\tignored\n");
154 	fprintf(stderr, "        ECDSAP384SHA384:\tignored\n");
155 	fprintf(stderr, "        ED25519:\tignored\n");
156 	fprintf(stderr, "        ED448:\tignored\n");
157 	fprintf(stderr, "        (key size defaults are set according to\n"
158 			"        algorithm and usage (ZSK or KSK)\n");
159 	fprintf(stderr, "    -n <nametype>: ZONE | HOST | ENTITY | "
160 			"USER | OTHER\n");
161 	fprintf(stderr, "        (DNSKEY generation defaults to ZONE)\n");
162 	fprintf(stderr, "    -c <class>: (default: IN)\n");
163 	fprintf(stderr, "    -d <digest bits> (0 => max, default)\n");
164 	fprintf(stderr, "    -E <engine>:\n");
165 	fprintf(stderr, "        name of an OpenSSL engine to use\n");
166 	fprintf(stderr, "    -f <keyflag>: KSK | REVOKE\n");
167 	fprintf(stderr, "    -g <generator>: use specified generator "
168 			"(DH only)\n");
169 	fprintf(stderr, "    -L <ttl>: default key TTL\n");
170 	fprintf(stderr, "    -p <protocol>: (default: 3 [dnssec])\n");
171 	fprintf(stderr, "    -s <strength>: strength value this key signs DNS "
172 			"records with (default: 0)\n");
173 	fprintf(stderr, "    -T <rrtype>: DNSKEY | KEY (default: DNSKEY; "
174 			"use KEY for SIG(0))\n");
175 	fprintf(stderr, "    -t <type>: "
176 			"AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
177 			"(default: AUTHCONF)\n");
178 	fprintf(stderr, "    -h: print usage and exit\n");
179 	fprintf(stderr, "    -m <memory debugging mode>:\n");
180 	fprintf(stderr, "       usage | trace | record | size | mctx\n");
181 	fprintf(stderr, "    -v <level>: set verbosity level (0 - 10)\n");
182 	fprintf(stderr, "    -V: print version information\n");
183 	fprintf(stderr, "Timing options:\n");
184 	fprintf(stderr, "    -P date/[+-]offset/none: set key publication date "
185 			"(default: now)\n");
186 	fprintf(stderr, "    -P sync date/[+-]offset/none: set CDS and CDNSKEY "
187 			"publication date\n");
188 	fprintf(stderr, "    -A date/[+-]offset/none: set key activation date "
189 			"(default: now)\n");
190 	fprintf(stderr, "    -R date/[+-]offset/none: set key "
191 			"revocation date\n");
192 	fprintf(stderr, "    -I date/[+-]offset/none: set key "
193 			"inactivation date\n");
194 	fprintf(stderr, "    -D date/[+-]offset/none: set key deletion date\n");
195 	fprintf(stderr, "    -D sync date/[+-]offset/none: set CDS and CDNSKEY "
196 			"deletion date\n");
197 
198 	fprintf(stderr, "    -G: generate key only; do not set -P or -A\n");
199 	fprintf(stderr, "    -C: generate a backward-compatible key, omitting "
200 			"all dates\n");
201 	fprintf(stderr, "    -S <key>: generate a successor to an existing "
202 			"key\n");
203 	fprintf(stderr, "    -i <interval>: prepublication interval for "
204 			"successor key "
205 			"(default: 30 days)\n");
206 	fprintf(stderr, "Output:\n");
207 	fprintf(stderr, "     K<name>+<alg>+<id>.key, "
208 			"K<name>+<alg>+<id>.private\n");
209 
210 	exit(-1);
211 }
212 
213 static void
progress(int p)214 progress(int p) {
215 	char c = '*';
216 
217 	switch (p) {
218 	case 0:
219 		c = '.';
220 		break;
221 	case 1:
222 		c = '+';
223 		break;
224 	case 2:
225 		c = '*';
226 		break;
227 	case 3:
228 		c = ' ';
229 		break;
230 	default:
231 		break;
232 	}
233 	(void)putc(c, stderr);
234 	(void)fflush(stderr);
235 }
236 
237 static void
kasp_from_conf(cfg_obj_t * config,isc_mem_t * mctx,const char * name,dns_kasp_t ** kaspp)238 kasp_from_conf(cfg_obj_t *config, isc_mem_t *mctx, const char *name,
239 	       dns_kasp_t **kaspp) {
240 	const cfg_listelt_t *element;
241 	const cfg_obj_t *kasps = NULL;
242 	dns_kasp_t *kasp = NULL, *kasp_next;
243 	isc_result_t result = ISC_R_NOTFOUND;
244 	dns_kasplist_t kasplist;
245 
246 	ISC_LIST_INIT(kasplist);
247 
248 	(void)cfg_map_get(config, "dnssec-policy", &kasps);
249 	for (element = cfg_list_first(kasps); element != NULL;
250 	     element = cfg_list_next(element))
251 	{
252 		cfg_obj_t *kconfig = cfg_listelt_value(element);
253 		kasp = NULL;
254 		if (strcmp(cfg_obj_asstring(cfg_tuple_get(kconfig, "name")),
255 			   name) != 0) {
256 			continue;
257 		}
258 
259 		result = cfg_kasp_fromconfig(kconfig, NULL, mctx, lctx,
260 					     &kasplist, &kasp);
261 		if (result != ISC_R_SUCCESS) {
262 			fatal("failed to configure dnssec-policy '%s': %s",
263 			      cfg_obj_asstring(cfg_tuple_get(kconfig, "name")),
264 			      isc_result_totext(result));
265 		}
266 		INSIST(kasp != NULL);
267 		dns_kasp_freeze(kasp);
268 		break;
269 	}
270 
271 	*kaspp = kasp;
272 
273 	/*
274 	 * Cleanup kasp list.
275 	 */
276 	for (kasp = ISC_LIST_HEAD(kasplist); kasp != NULL; kasp = kasp_next) {
277 		kasp_next = ISC_LIST_NEXT(kasp, link);
278 		ISC_LIST_UNLINK(kasplist, kasp, link);
279 		dns_kasp_detach(&kasp);
280 	}
281 }
282 
283 static void
keygen(keygen_ctx_t * ctx,isc_mem_t * mctx,int argc,char ** argv)284 keygen(keygen_ctx_t *ctx, isc_mem_t *mctx, int argc, char **argv) {
285 	char filename[255];
286 	char algstr[DNS_SECALG_FORMATSIZE];
287 	uint16_t flags = 0;
288 	int param = 0;
289 	bool null_key = false;
290 	bool conflict = false;
291 	bool show_progress = false;
292 	isc_buffer_t buf;
293 	dns_name_t *name;
294 	dns_fixedname_t fname;
295 	isc_result_t ret;
296 	dst_key_t *key = NULL;
297 	dst_key_t *prevkey = NULL;
298 
299 	UNUSED(argc);
300 
301 	dns_secalg_format(ctx->alg, algstr, sizeof(algstr));
302 
303 	if (ctx->predecessor == NULL) {
304 		if (ctx->prepub == -1) {
305 			ctx->prepub = 0;
306 		}
307 
308 		name = dns_fixedname_initname(&fname);
309 		isc_buffer_init(&buf, argv[isc_commandline_index],
310 				strlen(argv[isc_commandline_index]));
311 		isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
312 		ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
313 		if (ret != ISC_R_SUCCESS) {
314 			fatal("invalid key name %s: %s",
315 			      argv[isc_commandline_index],
316 			      isc_result_totext(ret));
317 		}
318 
319 		if (!dst_algorithm_supported(ctx->alg)) {
320 			fatal("unsupported algorithm: %s", algstr);
321 		}
322 
323 		if (ctx->alg == DST_ALG_DH) {
324 			ctx->options |= DST_TYPE_KEY;
325 		}
326 
327 		if (ctx->use_nsec3) {
328 			switch (ctx->alg) {
329 			case DST_ALG_RSASHA1:
330 				ctx->alg = DST_ALG_NSEC3RSASHA1;
331 				break;
332 			case DST_ALG_NSEC3RSASHA1:
333 			case DST_ALG_RSASHA256:
334 			case DST_ALG_RSASHA512:
335 			case DST_ALG_ECDSA256:
336 			case DST_ALG_ECDSA384:
337 			case DST_ALG_ED25519:
338 			case DST_ALG_ED448:
339 				break;
340 			default:
341 				fatal("algorithm %s is incompatible with NSEC3"
342 				      ", do not use the -3 option",
343 				      algstr);
344 			}
345 		}
346 
347 		if (ctx->type != NULL && (ctx->options & DST_TYPE_KEY) != 0) {
348 			if (strcasecmp(ctx->type, "NOAUTH") == 0) {
349 				flags |= DNS_KEYTYPE_NOAUTH;
350 			} else if (strcasecmp(ctx->type, "NOCONF") == 0) {
351 				flags |= DNS_KEYTYPE_NOCONF;
352 			} else if (strcasecmp(ctx->type, "NOAUTHCONF") == 0) {
353 				flags |= (DNS_KEYTYPE_NOAUTH |
354 					  DNS_KEYTYPE_NOCONF);
355 				if (ctx->size < 0) {
356 					ctx->size = 0;
357 				}
358 			} else if (strcasecmp(ctx->type, "AUTHCONF") == 0) {
359 				/* nothing */
360 			} else {
361 				fatal("invalid type %s", ctx->type);
362 			}
363 		}
364 
365 		if (ctx->size < 0) {
366 			switch (ctx->alg) {
367 			case DST_ALG_RSASHA1:
368 			case DST_ALG_NSEC3RSASHA1:
369 			case DST_ALG_RSASHA256:
370 			case DST_ALG_RSASHA512:
371 				ctx->size = 2048;
372 				if (verbose > 0) {
373 					fprintf(stderr,
374 						"key size not "
375 						"specified; defaulting"
376 						" to %d\n",
377 						ctx->size);
378 				}
379 				break;
380 			case DST_ALG_ECDSA256:
381 			case DST_ALG_ECDSA384:
382 			case DST_ALG_ED25519:
383 			case DST_ALG_ED448:
384 				break;
385 			default:
386 				fatal("key size not specified (-b option)");
387 			}
388 		}
389 
390 		if (!ctx->oldstyle && ctx->prepub > 0) {
391 			if (ctx->setpub && ctx->setact &&
392 			    (ctx->activate - ctx->prepub) < ctx->publish) {
393 				fatal("Activation and publication dates "
394 				      "are closer together than the\n\t"
395 				      "prepublication interval.");
396 			}
397 
398 			if (!ctx->setpub && !ctx->setact) {
399 				ctx->setpub = ctx->setact = true;
400 				ctx->publish = ctx->now;
401 				ctx->activate = ctx->now + ctx->prepub;
402 			} else if (ctx->setpub && !ctx->setact) {
403 				ctx->setact = true;
404 				ctx->activate = ctx->publish + ctx->prepub;
405 			} else if (ctx->setact && !ctx->setpub) {
406 				ctx->setpub = true;
407 				ctx->publish = ctx->activate - ctx->prepub;
408 			}
409 
410 			if ((ctx->activate - ctx->prepub) < ctx->now) {
411 				fatal("Time until activation is shorter "
412 				      "than the\n\tprepublication interval.");
413 			}
414 		}
415 	} else {
416 		char keystr[DST_KEY_FORMATSIZE];
417 		isc_stdtime_t when;
418 		int major, minor;
419 
420 		if (ctx->prepub == -1) {
421 			ctx->prepub = (30 * 86400);
422 		}
423 
424 		if (ctx->alg != 0) {
425 			fatal("-S and -a cannot be used together");
426 		}
427 		if (ctx->size >= 0) {
428 			fatal("-S and -b cannot be used together");
429 		}
430 		if (ctx->nametype != NULL) {
431 			fatal("-S and -n cannot be used together");
432 		}
433 		if (ctx->type != NULL) {
434 			fatal("-S and -t cannot be used together");
435 		}
436 		if (ctx->setpub || ctx->unsetpub) {
437 			fatal("-S and -P cannot be used together");
438 		}
439 		if (ctx->setact || ctx->unsetact) {
440 			fatal("-S and -A cannot be used together");
441 		}
442 		if (ctx->use_nsec3) {
443 			fatal("-S and -3 cannot be used together");
444 		}
445 		if (ctx->oldstyle) {
446 			fatal("-S and -C cannot be used together");
447 		}
448 		if (ctx->genonly) {
449 			fatal("-S and -G cannot be used together");
450 		}
451 
452 		ret = dst_key_fromnamedfile(
453 			ctx->predecessor, ctx->directory,
454 			(DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | DST_TYPE_STATE),
455 			mctx, &prevkey);
456 		if (ret != ISC_R_SUCCESS) {
457 			fatal("Invalid keyfile %s: %s", ctx->predecessor,
458 			      isc_result_totext(ret));
459 		}
460 		if (!dst_key_isprivate(prevkey)) {
461 			fatal("%s is not a private key", ctx->predecessor);
462 		}
463 
464 		name = dst_key_name(prevkey);
465 		ctx->alg = dst_key_alg(prevkey);
466 		ctx->size = dst_key_size(prevkey);
467 		flags = dst_key_flags(prevkey);
468 
469 		dst_key_format(prevkey, keystr, sizeof(keystr));
470 		dst_key_getprivateformat(prevkey, &major, &minor);
471 		if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION) {
472 			fatal("Key %s has incompatible format version %d.%d\n\t"
473 			      "It is not possible to generate a successor key.",
474 			      keystr, major, minor);
475 		}
476 
477 		ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
478 		if (ret != ISC_R_SUCCESS) {
479 			fatal("Key %s has no activation date.\n\t"
480 			      "You must use dnssec-settime -A to set one "
481 			      "before generating a successor.",
482 			      keystr);
483 		}
484 
485 		ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE,
486 				      &ctx->activate);
487 		if (ret != ISC_R_SUCCESS) {
488 			fatal("Key %s has no inactivation date.\n\t"
489 			      "You must use dnssec-settime -I to set one "
490 			      "before generating a successor.",
491 			      keystr);
492 		}
493 
494 		ctx->publish = ctx->activate - ctx->prepub;
495 		if (ctx->publish < ctx->now) {
496 			fatal("Key %s becomes inactive\n\t"
497 			      "sooner than the prepublication period "
498 			      "for the new key ends.\n\t"
499 			      "Either change the inactivation date with "
500 			      "dnssec-settime -I,\n\t"
501 			      "or use the -i option to set a shorter "
502 			      "prepublication interval.",
503 			      keystr);
504 		}
505 
506 		ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
507 		if (ret != ISC_R_SUCCESS) {
508 			fprintf(stderr,
509 				"%s: WARNING: Key %s has no removal "
510 				"date;\n\t it will remain in the zone "
511 				"indefinitely after rollover.\n\t "
512 				"You can use dnssec-settime -D to "
513 				"change this.\n",
514 				program, keystr);
515 		}
516 
517 		ctx->setpub = ctx->setact = true;
518 	}
519 
520 	switch (ctx->alg) {
521 	case DNS_KEYALG_RSASHA1:
522 	case DNS_KEYALG_NSEC3RSASHA1:
523 	case DNS_KEYALG_RSASHA256:
524 		if (ctx->size != 0 && (ctx->size < 1024 || ctx->size > MAX_RSA))
525 		{
526 			fatal("RSA key size %d out of range", ctx->size);
527 		}
528 		break;
529 	case DNS_KEYALG_RSASHA512:
530 		if (ctx->size != 0 && (ctx->size < 1024 || ctx->size > MAX_RSA))
531 		{
532 			fatal("RSA key size %d out of range", ctx->size);
533 		}
534 		break;
535 	case DNS_KEYALG_DH:
536 		if (ctx->size != 0 && (ctx->size < 128 || ctx->size > 4096)) {
537 			fatal("DH key size %d out of range", ctx->size);
538 		}
539 		break;
540 	case DST_ALG_ECDSA256:
541 		ctx->size = 256;
542 		break;
543 	case DST_ALG_ECDSA384:
544 		ctx->size = 384;
545 		break;
546 	case DST_ALG_ED25519:
547 		ctx->size = 256;
548 		break;
549 	case DST_ALG_ED448:
550 		ctx->size = 456;
551 		break;
552 	}
553 
554 	if (ctx->alg != DNS_KEYALG_DH && ctx->generator != 0) {
555 		fatal("specified DH generator for a non-DH key");
556 	}
557 
558 	if (ctx->nametype == NULL) {
559 		if ((ctx->options & DST_TYPE_KEY) != 0) { /* KEY */
560 			fatal("no nametype specified");
561 		}
562 		flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
563 	} else if (strcasecmp(ctx->nametype, "zone") == 0) {
564 		flags |= DNS_KEYOWNER_ZONE;
565 	} else if ((ctx->options & DST_TYPE_KEY) != 0) { /* KEY */
566 		if (strcasecmp(ctx->nametype, "host") == 0 ||
567 		    strcasecmp(ctx->nametype, "entity") == 0)
568 		{
569 			flags |= DNS_KEYOWNER_ENTITY;
570 		} else if (strcasecmp(ctx->nametype, "user") == 0) {
571 			flags |= DNS_KEYOWNER_USER;
572 		} else {
573 			fatal("invalid KEY nametype %s", ctx->nametype);
574 		}
575 	} else if (strcasecmp(ctx->nametype, "other") != 0) { /* DNSKEY */
576 		fatal("invalid DNSKEY nametype %s", ctx->nametype);
577 	}
578 
579 	if (ctx->directory == NULL) {
580 		ctx->directory = ".";
581 	}
582 
583 	if ((ctx->options & DST_TYPE_KEY) != 0) { /* KEY */
584 		flags |= ctx->signatory;
585 	} else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
586 		flags |= ctx->kskflag;
587 		flags |= ctx->revflag;
588 	}
589 
590 	if (ctx->protocol == -1) {
591 		ctx->protocol = DNS_KEYPROTO_DNSSEC;
592 	} else if ((ctx->options & DST_TYPE_KEY) == 0 &&
593 		   ctx->protocol != DNS_KEYPROTO_DNSSEC)
594 	{
595 		fatal("invalid DNSKEY protocol: %d", ctx->protocol);
596 	}
597 
598 	if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
599 		if (ctx->size > 0) {
600 			fatal("specified null key with non-zero size");
601 		}
602 		if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0) {
603 			fatal("specified null key with signing authority");
604 		}
605 	}
606 
607 	if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
608 	    ctx->alg == DNS_KEYALG_DH)
609 	{
610 		fatal("a key with algorithm %s cannot be a zone key", algstr);
611 	}
612 
613 	switch (ctx->alg) {
614 	case DNS_KEYALG_RSASHA1:
615 	case DNS_KEYALG_NSEC3RSASHA1:
616 	case DNS_KEYALG_RSASHA256:
617 	case DNS_KEYALG_RSASHA512:
618 		show_progress = true;
619 		break;
620 
621 	case DNS_KEYALG_DH:
622 		param = ctx->generator;
623 		break;
624 
625 	case DST_ALG_ECDSA256:
626 	case DST_ALG_ECDSA384:
627 	case DST_ALG_ED25519:
628 	case DST_ALG_ED448:
629 		show_progress = true;
630 		break;
631 	}
632 
633 	if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
634 		null_key = true;
635 	}
636 
637 	isc_buffer_init(&buf, filename, sizeof(filename) - 1);
638 
639 	do {
640 		conflict = false;
641 
642 		if (!ctx->quiet && show_progress) {
643 			fprintf(stderr, "Generating key pair.");
644 			ret = dst_key_generate(name, ctx->alg, ctx->size, param,
645 					       flags, ctx->protocol,
646 					       ctx->rdclass, mctx, &key,
647 					       &progress);
648 			putc('\n', stderr);
649 			fflush(stderr);
650 		} else {
651 			ret = dst_key_generate(name, ctx->alg, ctx->size, param,
652 					       flags, ctx->protocol,
653 					       ctx->rdclass, mctx, &key, NULL);
654 		}
655 
656 		if (ret != ISC_R_SUCCESS) {
657 			char namestr[DNS_NAME_FORMATSIZE];
658 			dns_name_format(name, namestr, sizeof(namestr));
659 			fatal("failed to generate key %s/%s: %s\n", namestr,
660 			      algstr, isc_result_totext(ret));
661 		}
662 
663 		dst_key_setbits(key, ctx->dbits);
664 
665 		/*
666 		 * Set key timing metadata (unless using -C)
667 		 *
668 		 * Creation date is always set to "now".
669 		 *
670 		 * For a new key without an explicit predecessor, publish
671 		 * and activation dates are set to "now" by default, but
672 		 * can both be overridden.
673 		 *
674 		 * For a successor key, activation is set to match the
675 		 * predecessor's inactivation date.  Publish is set to 30
676 		 * days earlier than that (XXX: this should be configurable).
677 		 * If either of the resulting dates are in the past, that's
678 		 * an error; the inactivation date of the predecessor key
679 		 * must be updated before a successor key can be created.
680 		 */
681 		if (!ctx->oldstyle) {
682 			dst_key_settime(key, DST_TIME_CREATED, ctx->now);
683 
684 			if (ctx->genonly && (ctx->setpub || ctx->setact)) {
685 				fatal("cannot use -G together with "
686 				      "-P or -A options");
687 			}
688 
689 			if (ctx->setpub) {
690 				dst_key_settime(key, DST_TIME_PUBLISH,
691 						ctx->publish);
692 			} else if (ctx->setact && !ctx->unsetpub) {
693 				dst_key_settime(key, DST_TIME_PUBLISH,
694 						ctx->activate - ctx->prepub);
695 			} else if (!ctx->genonly && !ctx->unsetpub) {
696 				dst_key_settime(key, DST_TIME_PUBLISH,
697 						ctx->now);
698 			}
699 
700 			if (ctx->setact) {
701 				dst_key_settime(key, DST_TIME_ACTIVATE,
702 						ctx->activate);
703 			} else if (!ctx->genonly && !ctx->unsetact) {
704 				dst_key_settime(key, DST_TIME_ACTIVATE,
705 						ctx->now);
706 			}
707 
708 			if (ctx->setrev) {
709 				if (ctx->kskflag == 0) {
710 					fprintf(stderr,
711 						"%s: warning: Key is "
712 						"not flagged as a KSK, but -R "
713 						"was used. Revoking a ZSK is "
714 						"legal, but undefined.\n",
715 						program);
716 				}
717 				dst_key_settime(key, DST_TIME_REVOKE,
718 						ctx->revokekey);
719 			}
720 
721 			if (ctx->setinact) {
722 				dst_key_settime(key, DST_TIME_INACTIVE,
723 						ctx->inactive);
724 			}
725 
726 			if (ctx->setdel) {
727 				if (ctx->setinact &&
728 				    ctx->deltime < ctx->inactive) {
729 					fprintf(stderr,
730 						"%s: warning: Key is "
731 						"scheduled to be deleted "
732 						"before it is scheduled to be "
733 						"made inactive.\n",
734 						program);
735 				}
736 				dst_key_settime(key, DST_TIME_DELETE,
737 						ctx->deltime);
738 			}
739 
740 			if (ctx->setsyncadd) {
741 				dst_key_settime(key, DST_TIME_SYNCPUBLISH,
742 						ctx->syncadd);
743 			}
744 
745 			if (ctx->setsyncdel) {
746 				dst_key_settime(key, DST_TIME_SYNCDELETE,
747 						ctx->syncdel);
748 			}
749 		} else {
750 			if (ctx->setpub || ctx->setact || ctx->setrev ||
751 			    ctx->setinact || ctx->setdel || ctx->unsetpub ||
752 			    ctx->unsetact || ctx->unsetrev || ctx->unsetinact ||
753 			    ctx->unsetdel || ctx->genonly || ctx->setsyncadd ||
754 			    ctx->setsyncdel)
755 			{
756 				fatal("cannot use -C together with "
757 				      "-P, -A, -R, -I, -D, or -G options");
758 			}
759 			/*
760 			 * Compatibility mode: Private-key-format
761 			 * should be set to 1.2.
762 			 */
763 			dst_key_setprivateformat(key, 1, 2);
764 		}
765 
766 		/* Set the default key TTL */
767 		if (ctx->setttl) {
768 			dst_key_setttl(key, ctx->ttl);
769 		}
770 
771 		/* Set dnssec-policy related metadata */
772 		if (ctx->policy != NULL) {
773 			dst_key_setnum(key, DST_NUM_LIFETIME, ctx->lifetime);
774 			dst_key_setbool(key, DST_BOOL_KSK, ctx->ksk);
775 			dst_key_setbool(key, DST_BOOL_ZSK, ctx->zsk);
776 		}
777 
778 		/*
779 		 * Do not overwrite an existing key, or create a key
780 		 * if there is a risk of ID collision due to this key
781 		 * or another key being revoked.
782 		 */
783 		if (key_collision(key, name, ctx->directory, mctx, NULL)) {
784 			conflict = true;
785 			if (null_key) {
786 				dst_key_free(&key);
787 				break;
788 			}
789 
790 			if (verbose > 0) {
791 				isc_buffer_clear(&buf);
792 				ret = dst_key_buildfilename(
793 					key, 0, ctx->directory, &buf);
794 				if (ret == ISC_R_SUCCESS) {
795 					fprintf(stderr,
796 						"%s: %s already exists, or "
797 						"might collide with another "
798 						"key upon revokation.  "
799 						"Generating a new key\n",
800 						program, filename);
801 				}
802 			}
803 
804 			dst_key_free(&key);
805 		}
806 	} while (conflict);
807 
808 	if (conflict) {
809 		fatal("cannot generate a null key due to possible key ID "
810 		      "collision");
811 	}
812 
813 	if (ctx->predecessor != NULL && prevkey != NULL) {
814 		dst_key_setnum(prevkey, DST_NUM_SUCCESSOR, dst_key_id(key));
815 		dst_key_setnum(key, DST_NUM_PREDECESSOR, dst_key_id(prevkey));
816 
817 		ret = dst_key_tofile(prevkey, ctx->options, ctx->directory);
818 		if (ret != ISC_R_SUCCESS) {
819 			char keystr[DST_KEY_FORMATSIZE];
820 			dst_key_format(prevkey, keystr, sizeof(keystr));
821 			fatal("failed to update predecessor %s: %s\n", keystr,
822 			      isc_result_totext(ret));
823 		}
824 	}
825 
826 	ret = dst_key_tofile(key, ctx->options, ctx->directory);
827 	if (ret != ISC_R_SUCCESS) {
828 		char keystr[DST_KEY_FORMATSIZE];
829 		dst_key_format(key, keystr, sizeof(keystr));
830 		fatal("failed to write key %s: %s\n", keystr,
831 		      isc_result_totext(ret));
832 	}
833 
834 	isc_buffer_clear(&buf);
835 	ret = dst_key_buildfilename(key, 0, NULL, &buf);
836 	if (ret != ISC_R_SUCCESS) {
837 		fatal("dst_key_buildfilename returned: %s\n",
838 		      isc_result_totext(ret));
839 	}
840 	printf("%s\n", filename);
841 
842 	dst_key_free(&key);
843 	if (prevkey != NULL) {
844 		dst_key_free(&prevkey);
845 	}
846 }
847 
848 int
main(int argc,char ** argv)849 main(int argc, char **argv) {
850 	char *algname = NULL, *freeit = NULL;
851 	char *classname = NULL;
852 	char *endp;
853 	isc_mem_t *mctx = NULL;
854 	isc_result_t ret;
855 	isc_textregion_t r;
856 	const char *engine = NULL;
857 	unsigned char c;
858 	int ch;
859 
860 	keygen_ctx_t ctx = {
861 		.options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC,
862 		.prepub = -1,
863 		.protocol = -1,
864 		.size = -1,
865 	};
866 
867 	if (argc == 1) {
868 		usage();
869 	}
870 
871 	isc_commandline_errprint = false;
872 
873 	/*
874 	 * Process memory debugging argument first.
875 	 */
876 #define CMDLINE_FLAGS                                           \
877 	"3A:a:b:Cc:D:d:E:eFf:Gg:hI:i:K:k:L:l:m:n:P:p:qR:r:S:s:" \
878 	"T:t:v:V"
879 	while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
880 		switch (ch) {
881 		case 'm':
882 			if (strcasecmp(isc_commandline_argument, "record") == 0)
883 			{
884 				isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
885 			}
886 			if (strcasecmp(isc_commandline_argument, "trace") == 0)
887 			{
888 				isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
889 			}
890 			if (strcasecmp(isc_commandline_argument, "usage") == 0)
891 			{
892 				isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
893 			}
894 			break;
895 		default:
896 			break;
897 		}
898 	}
899 	isc_commandline_reset = true;
900 
901 	isc_mem_create(&mctx);
902 	isc_stdtime_get(&ctx.now);
903 
904 	while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
905 		switch (ch) {
906 		case '3':
907 			ctx.use_nsec3 = true;
908 			break;
909 		case 'a':
910 			algname = isc_commandline_argument;
911 			break;
912 		case 'b':
913 			ctx.size = strtol(isc_commandline_argument, &endp, 10);
914 			if (*endp != '\0' || ctx.size < 0) {
915 				fatal("-b requires a non-negative number");
916 			}
917 			break;
918 		case 'C':
919 			ctx.oldstyle = true;
920 			break;
921 		case 'c':
922 			classname = isc_commandline_argument;
923 			break;
924 		case 'd':
925 			ctx.dbits = strtol(isc_commandline_argument, &endp, 10);
926 			if (*endp != '\0' || ctx.dbits < 0) {
927 				fatal("-d requires a non-negative number");
928 			}
929 			break;
930 		case 'E':
931 			engine = isc_commandline_argument;
932 			break;
933 		case 'e':
934 			fprintf(stderr, "phased-out option -e "
935 					"(was 'use (RSA) large exponent')\n");
936 			break;
937 		case 'f':
938 			c = (unsigned char)(isc_commandline_argument[0]);
939 			if (toupper(c) == 'K') {
940 				ctx.kskflag = DNS_KEYFLAG_KSK;
941 			} else if (toupper(c) == 'R') {
942 				ctx.revflag = DNS_KEYFLAG_REVOKE;
943 			} else {
944 				fatal("unknown flag '%s'",
945 				      isc_commandline_argument);
946 			}
947 			break;
948 		case 'g':
949 			ctx.generator = strtol(isc_commandline_argument, &endp,
950 					       10);
951 			if (*endp != '\0' || ctx.generator <= 0) {
952 				fatal("-g requires a positive number");
953 			}
954 			break;
955 		case 'K':
956 			ctx.directory = isc_commandline_argument;
957 			ret = try_dir(ctx.directory);
958 			if (ret != ISC_R_SUCCESS) {
959 				fatal("cannot open directory %s: %s",
960 				      ctx.directory, isc_result_totext(ret));
961 			}
962 			break;
963 		case 'k':
964 			ctx.policy = isc_commandline_argument;
965 			break;
966 		case 'L':
967 			ctx.ttl = strtottl(isc_commandline_argument);
968 			ctx.setttl = true;
969 			break;
970 		case 'l':
971 			ctx.configfile = isc_commandline_argument;
972 			break;
973 		case 'n':
974 			ctx.nametype = isc_commandline_argument;
975 			break;
976 		case 'm':
977 			break;
978 		case 'p':
979 			ctx.protocol = strtol(isc_commandline_argument, &endp,
980 					      10);
981 			if (*endp != '\0' || ctx.protocol < 0 ||
982 			    ctx.protocol > 255) {
983 				fatal("-p must be followed by a number "
984 				      "[0..255]");
985 			}
986 			break;
987 		case 'q':
988 			ctx.quiet = true;
989 			break;
990 		case 'r':
991 			fatal("The -r option has been deprecated.\n"
992 			      "System random data is always used.\n");
993 			break;
994 		case 's':
995 			ctx.signatory = strtol(isc_commandline_argument, &endp,
996 					       10);
997 			if (*endp != '\0' || ctx.signatory < 0 ||
998 			    ctx.signatory > 15) {
999 				fatal("-s must be followed by a number "
1000 				      "[0..15]");
1001 			}
1002 			break;
1003 		case 'T':
1004 			if (strcasecmp(isc_commandline_argument, "KEY") == 0) {
1005 				ctx.options |= DST_TYPE_KEY;
1006 			} else if (strcasecmp(isc_commandline_argument,
1007 					      "DNSKE"
1008 					      "Y") == 0)
1009 			{
1010 				/* default behavior */
1011 			} else {
1012 				fatal("unknown type '%s'",
1013 				      isc_commandline_argument);
1014 			}
1015 			break;
1016 		case 't':
1017 			ctx.type = isc_commandline_argument;
1018 			break;
1019 		case 'v':
1020 			endp = NULL;
1021 			verbose = strtol(isc_commandline_argument, &endp, 0);
1022 			if (*endp != '\0') {
1023 				fatal("-v must be followed by a number");
1024 			}
1025 			break;
1026 		case 'G':
1027 			ctx.genonly = true;
1028 			break;
1029 		case 'P':
1030 			/* -Psync ? */
1031 			if (isoptarg("sync", argv, usage)) {
1032 				if (ctx.setsyncadd) {
1033 					fatal("-P sync specified more than "
1034 					      "once");
1035 				}
1036 
1037 				ctx.syncadd = strtotime(
1038 					isc_commandline_argument, ctx.now,
1039 					ctx.now, &ctx.setsyncadd);
1040 				break;
1041 			}
1042 			(void)isoptarg("dnskey", argv, usage);
1043 			if (ctx.setpub || ctx.unsetpub) {
1044 				fatal("-P specified more than once");
1045 			}
1046 
1047 			ctx.publish = strtotime(isc_commandline_argument,
1048 						ctx.now, ctx.now, &ctx.setpub);
1049 			ctx.unsetpub = !ctx.setpub;
1050 			break;
1051 		case 'A':
1052 			if (ctx.setact || ctx.unsetact) {
1053 				fatal("-A specified more than once");
1054 			}
1055 
1056 			ctx.activate = strtotime(isc_commandline_argument,
1057 						 ctx.now, ctx.now, &ctx.setact);
1058 			ctx.unsetact = !ctx.setact;
1059 			break;
1060 		case 'R':
1061 			if (ctx.setrev || ctx.unsetrev) {
1062 				fatal("-R specified more than once");
1063 			}
1064 
1065 			ctx.revokekey = strtotime(isc_commandline_argument,
1066 						  ctx.now, ctx.now,
1067 						  &ctx.setrev);
1068 			ctx.unsetrev = !ctx.setrev;
1069 			break;
1070 		case 'I':
1071 			if (ctx.setinact || ctx.unsetinact) {
1072 				fatal("-I specified more than once");
1073 			}
1074 
1075 			ctx.inactive = strtotime(isc_commandline_argument,
1076 						 ctx.now, ctx.now,
1077 						 &ctx.setinact);
1078 			ctx.unsetinact = !ctx.setinact;
1079 			break;
1080 		case 'D':
1081 			/* -Dsync ? */
1082 			if (isoptarg("sync", argv, usage)) {
1083 				if (ctx.setsyncdel) {
1084 					fatal("-D sync specified more than "
1085 					      "once");
1086 				}
1087 
1088 				ctx.syncdel = strtotime(
1089 					isc_commandline_argument, ctx.now,
1090 					ctx.now, &ctx.setsyncdel);
1091 				break;
1092 			}
1093 			(void)isoptarg("dnskey", argv, usage);
1094 			if (ctx.setdel || ctx.unsetdel) {
1095 				fatal("-D specified more than once");
1096 			}
1097 
1098 			ctx.deltime = strtotime(isc_commandline_argument,
1099 						ctx.now, ctx.now, &ctx.setdel);
1100 			ctx.unsetdel = !ctx.setdel;
1101 			break;
1102 		case 'S':
1103 			ctx.predecessor = isc_commandline_argument;
1104 			break;
1105 		case 'i':
1106 			ctx.prepub = strtottl(isc_commandline_argument);
1107 			break;
1108 		case 'F':
1109 		/* Reserved for FIPS mode */
1110 		/* FALLTHROUGH */
1111 		case '?':
1112 			if (isc_commandline_option != '?') {
1113 				fprintf(stderr, "%s: invalid argument -%c\n",
1114 					program, isc_commandline_option);
1115 			}
1116 		/* FALLTHROUGH */
1117 		case 'h':
1118 			/* Does not return. */
1119 			usage();
1120 
1121 		case 'V':
1122 			/* Does not return. */
1123 			version(program);
1124 
1125 		default:
1126 			fprintf(stderr, "%s: unhandled option -%c\n", program,
1127 				isc_commandline_option);
1128 			exit(1);
1129 		}
1130 	}
1131 
1132 	if (!isatty(0)) {
1133 		ctx.quiet = true;
1134 	}
1135 
1136 	ret = dst_lib_init(mctx, engine);
1137 	if (ret != ISC_R_SUCCESS) {
1138 		fatal("could not initialize dst: %s", isc_result_totext(ret));
1139 	}
1140 
1141 	setup_logging(mctx, &lctx);
1142 
1143 	ctx.rdclass = strtoclass(classname);
1144 
1145 	if (ctx.configfile == NULL || ctx.configfile[0] == '\0') {
1146 		ctx.configfile = NAMED_CONFFILE;
1147 	}
1148 
1149 	if (ctx.predecessor == NULL) {
1150 		if (argc < isc_commandline_index + 1) {
1151 			fatal("the key name was not specified");
1152 		}
1153 		if (argc > isc_commandline_index + 1) {
1154 			fatal("extraneous arguments");
1155 		}
1156 	}
1157 
1158 	if (ctx.predecessor == NULL && ctx.policy == NULL) {
1159 		if (algname == NULL) {
1160 			fatal("no algorithm specified");
1161 		}
1162 		r.base = algname;
1163 		r.length = strlen(algname);
1164 		ret = dns_secalg_fromtext(&ctx.alg, &r);
1165 		if (ret != ISC_R_SUCCESS) {
1166 			fatal("unknown algorithm %s", algname);
1167 		}
1168 		if (!dst_algorithm_supported(ctx.alg)) {
1169 			fatal("unsupported algorithm: %s", algname);
1170 		}
1171 	}
1172 
1173 	if (ctx.policy != NULL) {
1174 		if (ctx.nametype != NULL) {
1175 			fatal("-k and -n cannot be used together");
1176 		}
1177 		if (ctx.predecessor != NULL) {
1178 			fatal("-k and -S cannot be used together");
1179 		}
1180 		if (ctx.oldstyle) {
1181 			fatal("-k and -C cannot be used together");
1182 		}
1183 		if (ctx.setttl) {
1184 			fatal("-k and -L cannot be used together");
1185 		}
1186 		if (ctx.prepub > 0) {
1187 			fatal("-k and -i cannot be used together");
1188 		}
1189 		if (ctx.size != -1) {
1190 			fatal("-k and -b cannot be used together");
1191 		}
1192 		if (ctx.kskflag || ctx.revflag) {
1193 			fatal("-k and -f cannot be used together");
1194 		}
1195 		if (ctx.options & DST_TYPE_KEY) {
1196 			fatal("-k and -T KEY cannot be used together");
1197 		}
1198 		if (ctx.use_nsec3) {
1199 			fatal("-k and -3 cannot be used together");
1200 		}
1201 
1202 		ctx.options |= DST_TYPE_STATE;
1203 
1204 		if (strcmp(ctx.policy, "default") == 0) {
1205 			ctx.use_nsec3 = false;
1206 			ctx.alg = DST_ALG_ECDSA256;
1207 			ctx.size = 0;
1208 			ctx.kskflag = DNS_KEYFLAG_KSK;
1209 			ctx.ttl = 3600;
1210 			ctx.setttl = true;
1211 			ctx.ksk = true;
1212 			ctx.zsk = true;
1213 			ctx.lifetime = 0;
1214 
1215 			keygen(&ctx, mctx, argc, argv);
1216 		} else {
1217 			cfg_parser_t *parser = NULL;
1218 			cfg_obj_t *config = NULL;
1219 			dns_kasp_t *kasp = NULL;
1220 			dns_kasp_key_t *kaspkey = NULL;
1221 
1222 			RUNTIME_CHECK(cfg_parser_create(mctx, lctx, &parser) ==
1223 				      ISC_R_SUCCESS);
1224 			if (cfg_parse_file(parser, ctx.configfile,
1225 					   &cfg_type_namedconf,
1226 					   &config) != ISC_R_SUCCESS)
1227 			{
1228 				fatal("unable to load dnssec-policy '%s' from "
1229 				      "'%s'",
1230 				      ctx.policy, ctx.configfile);
1231 			}
1232 
1233 			kasp_from_conf(config, mctx, ctx.policy, &kasp);
1234 			if (kasp == NULL) {
1235 				fatal("failed to load dnssec-policy '%s'",
1236 				      ctx.policy);
1237 			}
1238 			if (ISC_LIST_EMPTY(dns_kasp_keys(kasp))) {
1239 				fatal("dnssec-policy '%s' has no keys "
1240 				      "configured",
1241 				      ctx.policy);
1242 			}
1243 
1244 			ctx.ttl = dns_kasp_dnskeyttl(kasp);
1245 			ctx.setttl = true;
1246 
1247 			kaspkey = ISC_LIST_HEAD(dns_kasp_keys(kasp));
1248 
1249 			while (kaspkey != NULL) {
1250 				ctx.use_nsec3 = false;
1251 				ctx.alg = dns_kasp_key_algorithm(kaspkey);
1252 				ctx.size = dns_kasp_key_size(kaspkey);
1253 				ctx.kskflag = dns_kasp_key_ksk(kaspkey)
1254 						      ? DNS_KEYFLAG_KSK
1255 						      : 0;
1256 				ctx.ksk = dns_kasp_key_ksk(kaspkey);
1257 				ctx.zsk = dns_kasp_key_zsk(kaspkey);
1258 				ctx.lifetime = dns_kasp_key_lifetime(kaspkey);
1259 
1260 				keygen(&ctx, mctx, argc, argv);
1261 
1262 				kaspkey = ISC_LIST_NEXT(kaspkey, link);
1263 			}
1264 
1265 			dns_kasp_detach(&kasp);
1266 			cfg_obj_destroy(parser, &config);
1267 			cfg_parser_destroy(&parser);
1268 		}
1269 	} else {
1270 		keygen(&ctx, mctx, argc, argv);
1271 	}
1272 
1273 	cleanup_logging(&lctx);
1274 	dst_lib_destroy();
1275 	if (verbose > 10) {
1276 		isc_mem_stats(mctx, stdout);
1277 	}
1278 	isc_mem_destroy(&mctx);
1279 
1280 	if (freeit != NULL) {
1281 		free(freeit);
1282 	}
1283 
1284 	return (0);
1285 }
1286