Update repub branch u/fanf2/patch to rebasing branch u/fanf2/rebasing revision v9_13_...
[ipreg/bind9.git] / bin / dnssec / dnssec-keyfromlabel.c
1 /*
2 * 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 http://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*! \file */
13
14 #include <ctype.h>
15 #include <inttypes.h>
16 #include <stdbool.h>
17 #include <stdlib.h>
18
19 #include <isc/buffer.h>
20 #include <isc/commandline.h>
21 #include <isc/mem.h>
22 #include <isc/region.h>
23 #include <isc/print.h>
24 #include <isc/string.h>
25 #include <isc/util.h>
26
27 #include <pk11/site.h>
28
29 #include <dns/dnssec.h>
30 #include <dns/fixedname.h>
31 #include <dns/keyvalues.h>
32 #include <dns/log.h>
33 #include <dns/name.h>
34 #include <dns/rdataclass.h>
35 #include <dns/result.h>
36 #include <dns/secalg.h>
37
38 #include <dst/dst.h>
39
40 #if USE_PKCS11
41 #include <pk11/result.h>
42 #endif
43
44 #include "dnssectool.h"
45
46 #define MAX_RSA 4096 /* should be long enough... */
47
48 const char *program = "dnssec-keyfromlabel";
49 int verbose;
50
51 ISC_PLATFORM_NORETURN_PRE static void
52 usage(void) ISC_PLATFORM_NORETURN_POST;
53
54 static void
55 usage(void) {
56 fprintf(stderr, "Usage:\n");
57 fprintf(stderr, " %s -l label [options] name\n\n",
58 program);
59 fprintf(stderr, "Version: %s\n", VERSION);
60 fprintf(stderr, "Required options:\n");
61 fprintf(stderr, " -l label: label of the key pair\n");
62 fprintf(stderr, " name: owner of the key\n");
63 fprintf(stderr, "Other options:\n");
64 fprintf(stderr, " -a algorithm: \n"
65 " DH | RSASHA1 |\n"
66 " NSEC3RSASHA1 |\n"
67 " RSASHA256 | RSASHA512 |\n"
68 " ECDSAP256SHA256 | ECDSAP384SHA384\n");
69 fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
70 fprintf(stderr, " -c class (default: IN)\n");
71 fprintf(stderr, " -E <engine>:\n");
72 #if USE_PKCS11
73 fprintf(stderr, " path to PKCS#11 provider library "
74 "(default is %s)\n", PK11_LIB_LOCATION);
75 #else
76 fprintf(stderr, " name of an OpenSSL engine to use\n");
77 #endif
78 fprintf(stderr, " -f keyflag: KSK | REVOKE\n");
79 fprintf(stderr, " -K directory: directory in which to place "
80 "key files\n");
81 fprintf(stderr, " -k: generate a TYPE=KEY key\n");
82 fprintf(stderr, " -L ttl: default key TTL\n");
83 fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
84 fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
85 fprintf(stderr, " -p protocol: default: 3 [dnssec]\n");
86 fprintf(stderr, " -t type: "
87 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
88 "(default: AUTHCONF)\n");
89 fprintf(stderr, " -y: permit keys that might collide\n");
90 fprintf(stderr, " -v verbose level\n");
91 fprintf(stderr, " -V: print version information\n");
92 fprintf(stderr, "Date options:\n");
93 fprintf(stderr, " -P date/[+-]offset: set key publication date\n");
94 fprintf(stderr, " -P sync date/[+-]offset: set CDS and CDNSKEY "
95 "publication date\n");
96 fprintf(stderr, " -A date/[+-]offset: set key activation date\n");
97 fprintf(stderr, " -R date/[+-]offset: set key revocation date\n");
98 fprintf(stderr, " -I date/[+-]offset: set key inactivation date\n");
99 fprintf(stderr, " -D date/[+-]offset: set key deletion date\n");
100 fprintf(stderr, " -D sync date/[+-]offset: set CDS and CDNSKEY "
101 "deletion date\n");
102 fprintf(stderr, " -G: generate key only; do not set -P or -A\n");
103 fprintf(stderr, " -C: generate a backward-compatible key, omitting"
104 " all dates\n");
105 fprintf(stderr, " -S <key>: generate a successor to an existing "
106 "key\n");
107 fprintf(stderr, " -i <interval>: prepublication interval for "
108 "successor key "
109 "(default: 30 days)\n");
110 fprintf(stderr, "Output:\n");
111 fprintf(stderr, " K<name>+<alg>+<id>.key, "
112 "K<name>+<alg>+<id>.private\n");
113
114 exit (-1);
115 }
116
117 int
118 main(int argc, char **argv) {
119 char *algname = NULL, *freeit = NULL;
120 char *nametype = NULL, *type = NULL;
121 const char *directory = NULL;
122 const char *predecessor = NULL;
123 dst_key_t *prevkey = NULL;
124 const char *engine = NULL;
125 char *classname = NULL;
126 char *endp;
127 dst_key_t *key = NULL;
128 dns_fixedname_t fname;
129 dns_name_t *name;
130 uint16_t flags = 0, kskflag = 0, revflag = 0;
131 dns_secalg_t alg;
132 bool oldstyle = false;
133 isc_mem_t *mctx = NULL;
134 int ch;
135 int protocol = -1, signatory = 0;
136 isc_result_t ret;
137 isc_textregion_t r;
138 char filename[255];
139 isc_buffer_t buf;
140 isc_log_t *log = NULL;
141 dns_rdataclass_t rdclass;
142 int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
143 char *label = NULL;
144 dns_ttl_t ttl = 0;
145 isc_stdtime_t publish = 0, activate = 0, revoke = 0;
146 isc_stdtime_t inactive = 0, deltime = 0;
147 isc_stdtime_t now;
148 int prepub = -1;
149 bool setpub = false, setact = false;
150 bool setrev = false, setinact = false;
151 bool setdel = false, setttl = false;
152 bool unsetpub = false, unsetact = false;
153 bool unsetrev = false, unsetinact = false;
154 bool unsetdel = false;
155 bool genonly = false;
156 bool use_nsec3 = false;
157 bool avoid_collisions = true;
158 bool exact;
159 unsigned char c;
160 isc_stdtime_t syncadd = 0, syncdel = 0;
161 bool unsetsyncadd = false, setsyncadd = false;
162 bool unsetsyncdel = false, setsyncdel = false;
163
164 if (argc == 1)
165 usage();
166
167 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
168
169 #if USE_PKCS11
170 pk11_result_register();
171 #endif
172 dns_result_register();
173
174 isc_commandline_errprint = false;
175
176 isc_stdtime_get(&now);
177
178 #define CMDLINE_FLAGS "3A:a:Cc:D:E:Ff:GhI:i:kK:L:l:n:P:p:R:S:t:v:Vy"
179 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
180 switch (ch) {
181 case '3':
182 use_nsec3 = true;
183 break;
184 case 'a':
185 algname = isc_commandline_argument;
186 break;
187 case 'C':
188 oldstyle = true;
189 break;
190 case 'c':
191 classname = isc_commandline_argument;
192 break;
193 case 'E':
194 engine = isc_commandline_argument;
195 break;
196 case 'f':
197 c = (unsigned char)(isc_commandline_argument[0]);
198 if (toupper(c) == 'K')
199 kskflag = DNS_KEYFLAG_KSK;
200 else if (toupper(c) == 'R')
201 revflag = DNS_KEYFLAG_REVOKE;
202 else
203 fatal("unknown flag '%s'",
204 isc_commandline_argument);
205 break;
206 case 'K':
207 directory = isc_commandline_argument;
208 ret = try_dir(directory);
209 if (ret != ISC_R_SUCCESS)
210 fatal("cannot open directory %s: %s",
211 directory, isc_result_totext(ret));
212 break;
213 case 'k':
214 options |= DST_TYPE_KEY;
215 break;
216 case 'L':
217 ttl = strtottl(isc_commandline_argument);
218 setttl = true;
219 break;
220 case 'l':
221 label = isc_mem_strdup(mctx, isc_commandline_argument);
222 break;
223 case 'n':
224 nametype = isc_commandline_argument;
225 break;
226 case 'p':
227 protocol = strtol(isc_commandline_argument, &endp, 10);
228 if (*endp != '\0' || protocol < 0 || protocol > 255)
229 fatal("-p must be followed by a number "
230 "[0..255]");
231 break;
232 case 't':
233 type = isc_commandline_argument;
234 break;
235 case 'v':
236 verbose = strtol(isc_commandline_argument, &endp, 0);
237 if (*endp != '\0')
238 fatal("-v must be followed by a number");
239 break;
240 case 'y':
241 avoid_collisions = false;
242 break;
243 case 'G':
244 genonly = true;
245 break;
246 case 'P':
247 /* -Psync ? */
248 if (isoptarg("sync", argv, usage)) {
249 if (unsetsyncadd || setsyncadd)
250 fatal("-P sync specified more than "
251 "once");
252
253 syncadd = strtotime(isc_commandline_argument,
254 now, now, &setsyncadd);
255 unsetsyncadd = !setsyncadd;
256 break;
257 }
258 /* -Pdnskey ? */
259 (void)isoptarg("dnskey", argv, usage);
260 if (setpub || unsetpub)
261 fatal("-P specified more than once");
262
263 publish = strtotime(isc_commandline_argument,
264 now, now, &setpub);
265 unsetpub = !setpub;
266 break;
267 case 'A':
268 if (setact || unsetact)
269 fatal("-A specified more than once");
270
271 activate = strtotime(isc_commandline_argument,
272 now, now, &setact);
273 unsetact = !setact;
274 break;
275 case 'R':
276 if (setrev || unsetrev)
277 fatal("-R specified more than once");
278
279 revoke = strtotime(isc_commandline_argument,
280 now, now, &setrev);
281 unsetrev = !setrev;
282 break;
283 case 'I':
284 if (setinact || unsetinact)
285 fatal("-I specified more than once");
286
287 inactive = strtotime(isc_commandline_argument,
288 now, now, &setinact);
289 unsetinact = !setinact;
290 break;
291 case 'D':
292 /* -Dsync ? */
293 if (isoptarg("sync", argv, usage)) {
294 if (unsetsyncdel || setsyncdel)
295 fatal("-D sync specified more than "
296 "once");
297
298 syncdel = strtotime(isc_commandline_argument,
299 now, now, &setsyncdel);
300 unsetsyncdel = !setsyncdel;
301 break;
302 }
303 /* -Ddnskey ? */
304 (void)isoptarg("dnskey", argv, usage);
305 if (setdel || unsetdel)
306 fatal("-D specified more than once");
307
308 deltime = strtotime(isc_commandline_argument,
309 now, now, &setdel);
310 unsetdel = !setdel;
311 break;
312 case 'S':
313 predecessor = isc_commandline_argument;
314 break;
315 case 'i':
316 prepub = strtottl(isc_commandline_argument);
317 break;
318 case 'F':
319 /* Reserved for FIPS mode */
320 /* FALLTHROUGH */
321 case '?':
322 if (isc_commandline_option != '?')
323 fprintf(stderr, "%s: invalid argument -%c\n",
324 program, isc_commandline_option);
325 /* FALLTHROUGH */
326 case 'h':
327 /* Does not return. */
328 usage();
329
330 case 'V':
331 /* Does not return. */
332 version(program);
333
334 default:
335 fprintf(stderr, "%s: unhandled option -%c\n",
336 program, isc_commandline_option);
337 exit(1);
338 }
339 }
340
341 ret = dst_lib_init(mctx, engine);
342 if (ret != ISC_R_SUCCESS)
343 fatal("could not initialize dst: %s",
344 isc_result_totext(ret));
345
346 setup_logging(mctx, &log);
347
348 if (predecessor == NULL) {
349 if (label == NULL)
350 fatal("the key label was not specified");
351 if (argc < isc_commandline_index + 1)
352 fatal("the key name was not specified");
353 if (argc > isc_commandline_index + 1)
354 fatal("extraneous arguments");
355
356 name = dns_fixedname_initname(&fname);
357 isc_buffer_init(&buf, argv[isc_commandline_index],
358 strlen(argv[isc_commandline_index]));
359 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
360 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
361 if (ret != ISC_R_SUCCESS)
362 fatal("invalid key name %s: %s",
363 argv[isc_commandline_index],
364 isc_result_totext(ret));
365
366 if (strchr(label, ':') == NULL) {
367 char *l;
368 int len;
369
370 len = strlen(label) + 8;
371 l = isc_mem_allocate(mctx, len);
372 if (l == NULL)
373 fatal("cannot allocate memory");
374 snprintf(l, len, "pkcs11:%s", label);
375 isc_mem_free(mctx, label);
376 label = l;
377 }
378
379 if (algname == NULL) {
380 fatal("no algorithm specified");
381 }
382
383 r.base = algname;
384 r.length = strlen(algname);
385 ret = dns_secalg_fromtext(&alg, &r);
386 if (ret != ISC_R_SUCCESS) {
387 fatal("unknown algorithm %s", algname);
388 }
389 if (alg == DST_ALG_DH) {
390 options |= DST_TYPE_KEY;
391 }
392
393 if (use_nsec3) {
394 switch (alg) {
395 case DST_ALG_RSASHA1:
396 alg = DST_ALG_NSEC3RSASHA1;
397 break;
398 case DST_ALG_NSEC3RSASHA1:
399 case DST_ALG_RSASHA256:
400 case DST_ALG_RSASHA512:
401 case DST_ALG_ECDSA256:
402 case DST_ALG_ECDSA384:
403 case DST_ALG_ED25519:
404 case DST_ALG_ED448:
405 break;
406 default:
407 fatal("%s is incompatible with NSEC3; "
408 "do not use the -3 option", algname);
409 }
410 }
411
412 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
413 if (strcasecmp(type, "NOAUTH") == 0)
414 flags |= DNS_KEYTYPE_NOAUTH;
415 else if (strcasecmp(type, "NOCONF") == 0)
416 flags |= DNS_KEYTYPE_NOCONF;
417 else if (strcasecmp(type, "NOAUTHCONF") == 0)
418 flags |= (DNS_KEYTYPE_NOAUTH |
419 DNS_KEYTYPE_NOCONF);
420 else if (strcasecmp(type, "AUTHCONF") == 0)
421 /* nothing */;
422 else
423 fatal("invalid type %s", type);
424 }
425
426 if (!oldstyle && prepub > 0) {
427 if (setpub && setact && (activate - prepub) < publish)
428 fatal("Activation and publication dates "
429 "are closer together than the\n\t"
430 "prepublication interval.");
431
432 if (!setpub && !setact) {
433 setpub = setact = true;
434 publish = now;
435 activate = now + prepub;
436 } else if (setpub && !setact) {
437 setact = true;
438 activate = publish + prepub;
439 } else if (setact && !setpub) {
440 setpub = true;
441 publish = activate - prepub;
442 }
443
444 if ((activate - prepub) < now)
445 fatal("Time until activation is shorter "
446 "than the\n\tprepublication interval.");
447 }
448 } else {
449 char keystr[DST_KEY_FORMATSIZE];
450 isc_stdtime_t when;
451 int major, minor;
452
453 if (prepub == -1)
454 prepub = (30 * 86400);
455
456 if (algname != NULL)
457 fatal("-S and -a cannot be used together");
458 if (nametype != NULL)
459 fatal("-S and -n cannot be used together");
460 if (type != NULL)
461 fatal("-S and -t cannot be used together");
462 if (setpub || unsetpub)
463 fatal("-S and -P cannot be used together");
464 if (setact || unsetact)
465 fatal("-S and -A cannot be used together");
466 if (use_nsec3)
467 fatal("-S and -3 cannot be used together");
468 if (oldstyle)
469 fatal("-S and -C cannot be used together");
470 if (genonly)
471 fatal("-S and -G cannot be used together");
472
473 ret = dst_key_fromnamedfile(predecessor, directory,
474 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
475 mctx, &prevkey);
476 if (ret != ISC_R_SUCCESS)
477 fatal("Invalid keyfile %s: %s",
478 predecessor, isc_result_totext(ret));
479 if (!dst_key_isprivate(prevkey))
480 fatal("%s is not a private key", predecessor);
481
482 name = dst_key_name(prevkey);
483 alg = dst_key_alg(prevkey);
484 flags = dst_key_flags(prevkey);
485
486 dst_key_format(prevkey, keystr, sizeof(keystr));
487 dst_key_getprivateformat(prevkey, &major, &minor);
488 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
489 fatal("Key %s has incompatible format version %d.%d\n\t"
490 "It is not possible to generate a successor key.",
491 keystr, major, minor);
492
493 ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
494 if (ret != ISC_R_SUCCESS)
495 fatal("Key %s has no activation date.\n\t"
496 "You must use dnssec-settime -A to set one "
497 "before generating a successor.", keystr);
498
499 ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
500 if (ret != ISC_R_SUCCESS)
501 fatal("Key %s has no inactivation date.\n\t"
502 "You must use dnssec-settime -I to set one "
503 "before generating a successor.", keystr);
504
505 publish = activate - prepub;
506 if (publish < now)
507 fatal("Key %s becomes inactive\n\t"
508 "sooner than the prepublication period "
509 "for the new key ends.\n\t"
510 "Either change the inactivation date with "
511 "dnssec-settime -I,\n\t"
512 "or use the -i option to set a shorter "
513 "prepublication interval.", keystr);
514
515 ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
516 if (ret != ISC_R_SUCCESS)
517 fprintf(stderr, "%s: WARNING: Key %s has no removal "
518 "date;\n\t it will remain in the zone "
519 "indefinitely after rollover.\n\t "
520 "You can use dnssec-settime -D to "
521 "change this.\n", program, keystr);
522
523 setpub = setact = true;
524 }
525
526 if (nametype == NULL) {
527 if ((options & DST_TYPE_KEY) != 0) /* KEY */
528 fatal("no nametype specified");
529 flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
530 } else if (strcasecmp(nametype, "zone") == 0)
531 flags |= DNS_KEYOWNER_ZONE;
532 else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
533 if (strcasecmp(nametype, "host") == 0 ||
534 strcasecmp(nametype, "entity") == 0)
535 flags |= DNS_KEYOWNER_ENTITY;
536 else if (strcasecmp(nametype, "user") == 0)
537 flags |= DNS_KEYOWNER_USER;
538 else
539 fatal("invalid KEY nametype %s", nametype);
540 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
541 fatal("invalid DNSKEY nametype %s", nametype);
542
543 rdclass = strtoclass(classname);
544
545 if (directory == NULL)
546 directory = ".";
547
548 if ((options & DST_TYPE_KEY) != 0) /* KEY */
549 flags |= signatory;
550 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
551 flags |= kskflag;
552 flags |= revflag;
553 }
554
555 if (protocol == -1)
556 protocol = DNS_KEYPROTO_DNSSEC;
557 else if ((options & DST_TYPE_KEY) == 0 &&
558 protocol != DNS_KEYPROTO_DNSSEC)
559 fatal("invalid DNSKEY protocol: %d", protocol);
560
561 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
562 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
563 fatal("specified null key with signing authority");
564 }
565
566 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
567 alg == DNS_KEYALG_DH)
568 fatal("a key with algorithm '%s' cannot be a zone key",
569 algname);
570
571 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
572
573 /* associate the key */
574 ret = dst_key_fromlabel(name, alg, flags, protocol, rdclass,
575 #if USE_PKCS11
576 "pkcs11",
577 #else
578 engine,
579 #endif
580 label, NULL, mctx, &key);
581
582 if (ret != ISC_R_SUCCESS) {
583 char namestr[DNS_NAME_FORMATSIZE];
584 char algstr[DNS_SECALG_FORMATSIZE];
585 dns_name_format(name, namestr, sizeof(namestr));
586 dns_secalg_format(alg, algstr, sizeof(algstr));
587 fatal("failed to get key %s/%s: %s",
588 namestr, algstr, isc_result_totext(ret));
589 /* NOTREACHED */
590 exit(-1);
591 }
592
593 /*
594 * Set key timing metadata (unless using -C)
595 *
596 * Publish and activation dates are set to "now" by default, but
597 * can be overridden. Creation date is always set to "now".
598 */
599 if (!oldstyle) {
600 dst_key_settime(key, DST_TIME_CREATED, now);
601
602 if (genonly && (setpub || setact))
603 fatal("cannot use -G together with -P or -A options");
604
605 if (setpub)
606 dst_key_settime(key, DST_TIME_PUBLISH, publish);
607 else if (setact)
608 dst_key_settime(key, DST_TIME_PUBLISH, activate);
609 else if (!genonly && !unsetpub)
610 dst_key_settime(key, DST_TIME_PUBLISH, now);
611
612 if (setact)
613 dst_key_settime(key, DST_TIME_ACTIVATE, activate);
614 else if (!genonly && !unsetact)
615 dst_key_settime(key, DST_TIME_ACTIVATE, now);
616
617 if (setrev) {
618 if (kskflag == 0)
619 fprintf(stderr, "%s: warning: Key is "
620 "not flagged as a KSK, but -R "
621 "was used. Revoking a ZSK is "
622 "legal, but undefined.\n",
623 program);
624 dst_key_settime(key, DST_TIME_REVOKE, revoke);
625 }
626
627 if (setinact)
628 dst_key_settime(key, DST_TIME_INACTIVE, inactive);
629
630 if (setdel)
631 dst_key_settime(key, DST_TIME_DELETE, deltime);
632 if (setsyncadd)
633 dst_key_settime(key, DST_TIME_SYNCPUBLISH, syncadd);
634 if (setsyncdel)
635 dst_key_settime(key, DST_TIME_SYNCDELETE, syncdel);
636
637 } else {
638 if (setpub || setact || setrev || setinact ||
639 setdel || unsetpub || unsetact ||
640 unsetrev || unsetinact || unsetdel || genonly ||
641 setsyncadd || setsyncdel)
642 fatal("cannot use -C together with "
643 "-P, -A, -R, -I, -D, or -G options");
644 /*
645 * Compatibility mode: Private-key-format
646 * should be set to 1.2.
647 */
648 dst_key_setprivateformat(key, 1, 2);
649 }
650
651 /* Set default key TTL */
652 if (setttl)
653 dst_key_setttl(key, ttl);
654
655 /*
656 * Do not overwrite an existing key. Warn LOUDLY if there
657 * is a risk of ID collision due to this key or another key
658 * being revoked.
659 */
660 if (key_collision(key, name, directory, mctx, &exact)) {
661 isc_buffer_clear(&buf);
662 ret = dst_key_buildfilename(key, 0, directory, &buf);
663 if (ret != ISC_R_SUCCESS)
664 fatal("dst_key_buildfilename returned: %s\n",
665 isc_result_totext(ret));
666 if (exact)
667 fatal("%s: %s already exists\n", program, filename);
668
669 if (avoid_collisions)
670 fatal("%s: %s could collide with another key upon "
671 "revokation\n", program, filename);
672
673 fprintf(stderr, "%s: WARNING: Key %s could collide with "
674 "another key upon revokation. If you plan "
675 "to revoke keys, destroy this key and "
676 "generate a different one.\n",
677 program, filename);
678 }
679
680 ret = dst_key_tofile(key, options, directory);
681 if (ret != ISC_R_SUCCESS) {
682 char keystr[DST_KEY_FORMATSIZE];
683 dst_key_format(key, keystr, sizeof(keystr));
684 fatal("failed to write key %s: %s\n", keystr,
685 isc_result_totext(ret));
686 }
687
688 isc_buffer_clear(&buf);
689 ret = dst_key_buildfilename(key, 0, NULL, &buf);
690 if (ret != ISC_R_SUCCESS)
691 fatal("dst_key_buildfilename returned: %s\n",
692 isc_result_totext(ret));
693 printf("%s\n", filename);
694 dst_key_free(&key);
695 if (prevkey != NULL)
696 dst_key_free(&prevkey);
697
698 cleanup_logging(&log);
699 dst_lib_destroy();
700 dns_name_destroy();
701 if (verbose > 10)
702 isc_mem_stats(mctx, stdout);
703 isc_mem_free(mctx, label);
704 isc_mem_destroy(&mctx);
705
706 if (freeit != NULL)
707 free(freeit);
708
709 return (0);
710 }