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