Update repub branch u/fanf2/patch to rebasing branch u/fanf2/rebasing revision v9_13_...
[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";
49int verbose;
50
debd489a
FD
51ISC_PLATFORM_NORETURN_PRE static void
52usage(void) ISC_PLATFORM_NORETURN_POST;
53
2a31bd53
FD
54static void
55usage(void) {
56 fprintf(stderr, "Usage:\n");
d1f39121 57 fprintf(stderr, " %s -l label [options] name\n\n",
2a31bd53
FD
58 program);
59 fprintf(stderr, "Version: %s\n", VERSION);
60 fprintf(stderr, "Required options:\n");
210970a2 61 fprintf(stderr, " -l label: label of the key pair\n");
2a31bd53
FD
62 fprintf(stderr, " name: owner of the key\n");
63 fprintf(stderr, "Other options:\n");
45afdb26 64 fprintf(stderr, " -a algorithm: \n"
e69dc0db 65 " DH | RSASHA1 |\n"
d6c50674 66 " NSEC3RSASHA1 |\n"
27593e65 67 " RSASHA256 | RSASHA512 |\n"
45afdb26 68 " ECDSAP256SHA256 | ECDSAP384SHA384\n");
d1f39121 69 fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
8b78c993 70 fprintf(stderr, " -c class (default: IN)\n");
ba751492 71 fprintf(stderr, " -E <engine>:\n");
c3b8130f 72#if USE_PKCS11
ba751492
EH
73 fprintf(stderr, " path to PKCS#11 provider library "
74 "(default is %s)\n", PK11_LIB_LOCATION);
d1f39121 75#else
ba751492 76 fprintf(stderr, " name of an OpenSSL engine to use\n");
8b78c993 77#endif
1f821c10 78 fprintf(stderr, " -f keyflag: KSK | REVOKE\n");
553ead32
EH
79 fprintf(stderr, " -K directory: directory in which to place "
80 "key files\n");
8a198fa7 81 fprintf(stderr, " -k: generate a TYPE=KEY key\n");
61bcc232 82 fprintf(stderr, " -L ttl: default key TTL\n");
1f821c10
FD
83 fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
84 fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
8b78c993
FD
85 fprintf(stderr, " -p protocol: default: 3 [dnssec]\n");
86 fprintf(stderr, " -t type: "
2a31bd53
FD
87 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
88 "(default: AUTHCONF)\n");
8a198fa7 89 fprintf(stderr, " -y: permit keys that might collide\n");
8b78c993 90 fprintf(stderr, " -v verbose level\n");
42782931 91 fprintf(stderr, " -V: print version information\n");
1f821c10
FD
92 fprintf(stderr, "Date options:\n");
93 fprintf(stderr, " -P date/[+-]offset: set key publication date\n");
e939674d
MA
94 fprintf(stderr, " -P sync date/[+-]offset: set CDS and CDNSKEY "
95 "publication date\n");
1f821c10
FD
96 fprintf(stderr, " -A date/[+-]offset: set key activation date\n");
97 fprintf(stderr, " -R date/[+-]offset: set key revocation date\n");
b843f577 98 fprintf(stderr, " -I date/[+-]offset: set key inactivation date\n");
1f821c10 99 fprintf(stderr, " -D date/[+-]offset: set key deletion date\n");
e939674d
MA
100 fprintf(stderr, " -D sync date/[+-]offset: set CDS and CDNSKEY "
101 "deletion date\n");
b843f577 102 fprintf(stderr, " -G: generate key only; do not set -P or -A\n");
1f821c10 103 fprintf(stderr, " -C: generate a backward-compatible key, omitting"
b843f577 104 " all dates\n");
a60bf97f
EH
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");
2a31bd53
FD
110 fprintf(stderr, "Output:\n");
111 fprintf(stderr, " K<name>+<alg>+<id>.key, "
1f821c10 112 "K<name>+<alg>+<id>.private\n");
2a31bd53
FD
113
114 exit (-1);
115}
116
117int
118main(int argc, char **argv) {
411d2914
MA
119 char *algname = NULL, *freeit = NULL;
120 char *nametype = NULL, *type = NULL;
1f821c10 121 const char *directory = NULL;
a60bf97f
EH
122 const char *predecessor = NULL;
123 dst_key_t *prevkey = NULL;
8b78c993 124 const char *engine = NULL;
2a31bd53
FD
125 char *classname = NULL;
126 char *endp;
8a198fa7 127 dst_key_t *key = NULL;
2a31bd53
FD
128 dns_fixedname_t fname;
129 dns_name_t *name;
cb6a185c 130 uint16_t flags = 0, kskflag = 0, revflag = 0;
2a31bd53 131 dns_secalg_t alg;
994e6569 132 bool oldstyle = false;
2a31bd53
FD
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;
2a31bd53
FD
141 dns_rdataclass_t rdclass;
142 int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
8b78c993 143 char *label = NULL;
f79a36ee 144 dns_ttl_t ttl = 0;
1f821c10 145 isc_stdtime_t publish = 0, activate = 0, revoke = 0;
4bf32aa5 146 isc_stdtime_t inactive = 0, deltime = 0;
1f821c10 147 isc_stdtime_t now;
a60bf97f 148 int prepub = -1;
994e6569
OS
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;
4bf4beed 159 unsigned char c;
e939674d 160 isc_stdtime_t syncadd = 0, syncdel = 0;
994e6569
OS
161 bool unsetsyncadd = false, setsyncadd = false;
162 bool unsetsyncdel = false, setsyncdel = false;
2a31bd53
FD
163
164 if (argc == 1)
165 usage();
166
167 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
168
c3b8130f 169#if USE_PKCS11
acbb301e
EH
170 pk11_result_register();
171#endif
2a31bd53
FD
172 dns_result_register();
173
994e6569 174 isc_commandline_errprint = false;
2a31bd53 175
1f821c10
FD
176 isc_stdtime_get(&now);
177
42782931 178#define CMDLINE_FLAGS "3A:a:Cc:D:E:Ff:GhI:i:kK:L:l:n:P:p:R:S:t:v:Vy"
a60bf97f 179 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
2a31bd53 180 switch (ch) {
d1f39121 181 case '3':
994e6569 182 use_nsec3 = true;
d1f39121 183 break;
2a31bd53
FD
184 case 'a':
185 algname = isc_commandline_argument;
186 break;
1f821c10 187 case 'C':
994e6569 188 oldstyle = true;
1f821c10 189 break;
2a31bd53
FD
190 case 'c':
191 classname = isc_commandline_argument;
192 break;
8b78c993
FD
193 case 'E':
194 engine = isc_commandline_argument;
195 break;
2a31bd53 196 case 'f':
4bf4beed
MA
197 c = (unsigned char)(isc_commandline_argument[0]);
198 if (toupper(c) == 'K')
1f821c10 199 kskflag = DNS_KEYFLAG_KSK;
4bf4beed 200 else if (toupper(c) == 'R')
1f821c10 201 revflag = DNS_KEYFLAG_REVOKE;
2a31bd53
FD
202 else
203 fatal("unknown flag '%s'",
204 isc_commandline_argument);
205 break;
553ead32
EH
206 case 'K':
207 directory = isc_commandline_argument;
8f0502e9
EH
208 ret = try_dir(directory);
209 if (ret != ISC_R_SUCCESS)
e3b59e4a 210 fatal("cannot open directory %s: %s",
8f0502e9 211 directory, isc_result_totext(ret));
553ead32 212 break;
2a31bd53
FD
213 case 'k':
214 options |= DST_TYPE_KEY;
215 break;
61bcc232 216 case 'L':
a165a17a 217 ttl = strtottl(isc_commandline_argument);
994e6569 218 setttl = true;
61bcc232 219 break;
2a31bd53 220 case 'l':
6f6f08b7 221 label = isc_mem_strdup(mctx, isc_commandline_argument);
2a31bd53
FD
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;
8a198fa7 240 case 'y':
994e6569 241 avoid_collisions = false;
8a198fa7 242 break;
b843f577 243 case 'G':
994e6569 244 genonly = true;
b843f577 245 break;
1f821c10 246 case 'P':
e939674d
MA
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);
1f821c10
FD
260 if (setpub || unsetpub)
261 fatal("-P specified more than once");
262
a165a17a
EH
263 publish = strtotime(isc_commandline_argument,
264 now, now, &setpub);
265 unsetpub = !setpub;
1f821c10
FD
266 break;
267 case 'A':
268 if (setact || unsetact)
269 fatal("-A specified more than once");
270
a165a17a
EH
271 activate = strtotime(isc_commandline_argument,
272 now, now, &setact);
273 unsetact = !setact;
1f821c10
FD
274 break;
275 case 'R':
276 if (setrev || unsetrev)
277 fatal("-R specified more than once");
278
a165a17a
EH
279 revoke = strtotime(isc_commandline_argument,
280 now, now, &setrev);
281 unsetrev = !setrev;
1f821c10 282 break;
b843f577
EH
283 case 'I':
284 if (setinact || unsetinact)
285 fatal("-I specified more than once");
1f821c10 286
a165a17a
EH
287 inactive = strtotime(isc_commandline_argument,
288 now, now, &setinact);
289 unsetinact = !setinact;
1f821c10
FD
290 break;
291 case 'D':
e939674d
MA
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);
1f821c10
FD
305 if (setdel || unsetdel)
306 fatal("-D specified more than once");
307
4bf32aa5
MA
308 deltime = strtotime(isc_commandline_argument,
309 now, now, &setdel);
a165a17a 310 unsetdel = !setdel;
1f821c10 311 break;
a60bf97f
EH
312 case 'S':
313 predecessor = isc_commandline_argument;
314 break;
315 case 'i':
316 prepub = strtottl(isc_commandline_argument);
317 break;
ddac1a2b
FD
318 case 'F':
319 /* Reserved for FIPS mode */
320 /* FALLTHROUGH */
2a31bd53
FD
321 case '?':
322 if (isc_commandline_option != '?')
323 fprintf(stderr, "%s: invalid argument -%c\n",
324 program, isc_commandline_option);
ddac1a2b 325 /* FALLTHROUGH */
2a31bd53 326 case 'h':
42782931 327 /* Does not return. */
2a31bd53
FD
328 usage();
329
42782931
MS
330 case 'V':
331 /* Does not return. */
332 version(program);
333
2a31bd53
FD
334 default:
335 fprintf(stderr, "%s: unhandled option -%c\n",
336 program, isc_commandline_option);
337 exit(1);
338 }
339 }
340
3a4f820d 341 ret = dst_lib_init(mctx, engine);
2a31bd53 342 if (ret != ISC_R_SUCCESS)
8b78c993
FD
343 fatal("could not initialize dst: %s",
344 isc_result_totext(ret));
2a31bd53 345
11463c0a 346 setup_logging(mctx, &log);
2a31bd53 347
a60bf97f
EH
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
4df4a8e7 356 name = dns_fixedname_initname(&fname);
7d769b7b
EH
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
a60bf97f
EH
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 }
d1f39121 378
a60bf97f 379 if (algname == NULL) {
45afdb26 380 fatal("no algorithm specified");
a60bf97f
EH
381 }
382
e69dc0db
OS
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;
a60bf97f
EH
391 }
392
45afdb26
EH
393 if (use_nsec3) {
394 switch (alg) {
45afdb26
EH
395 case DST_ALG_RSASHA1:
396 alg = DST_ALG_NSEC3RSASHA1;
397 break;
45afdb26
EH
398 case DST_ALG_NSEC3RSASHA1:
399 case DST_ALG_RSASHA256:
400 case DST_ALG_RSASHA512:
45afdb26
EH
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 }
a60bf97f 410 }
d1f39121 411
a60bf97f
EH
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) {
e23798e2
TU
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) {
994e6569 433 setpub = setact = true;
e23798e2
TU
434 publish = now;
435 activate = now + prepub;
436 } else if (setpub && !setact) {
994e6569 437 setact = true;
e23798e2
TU
438 activate = publish + prepub;
439 } else if (setact && !setpub) {
994e6569 440 setpub = true;
e23798e2
TU
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 }
2a31bd53 448 } else {
a60bf97f
EH
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);
2a31bd53 476 if (ret != ISC_R_SUCCESS)
a60bf97f
EH
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);
2a31bd53 498
a60bf97f
EH
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);
d1f39121 522
994e6569 523 setpub = setact = true;
2a31bd53
FD
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
1f821c10
FD
545 if (directory == NULL)
546 directory = ".";
547
2a31bd53
FD
548 if ((options & DST_TYPE_KEY) != 0) /* KEY */
549 flags |= signatory;
cfb1587e 550 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
1f821c10
FD
551 flags |= kskflag;
552 flags |= revflag;
f66c8eed 553 }
2a31bd53
FD
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
2a31bd53
FD
571 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
572
573 /* associate the key */
1f33a989 574 ret = dst_key_fromlabel(name, alg, flags, protocol, rdclass,
c3b8130f 575#if USE_PKCS11
1f33a989
FD
576 "pkcs11",
577#else
578 engine,
579#endif
580 label, NULL, mctx, &key);
2a31bd53
FD
581
582 if (ret != ISC_R_SUCCESS) {
583 char namestr[DNS_NAME_FORMATSIZE];
77b8f88f 584 char algstr[DNS_SECALG_FORMATSIZE];
2a31bd53 585 dns_name_format(name, namestr, sizeof(namestr));
77b8f88f 586 dns_secalg_format(alg, algstr, sizeof(algstr));
ba751492 587 fatal("failed to get key %s/%s: %s",
2a31bd53 588 namestr, algstr, isc_result_totext(ret));
12107993 589 /* NOTREACHED */
2a31bd53
FD
590 exit(-1);
591 }
592
1f821c10
FD
593 /*
594 * Set key timing metadata (unless using -C)
b843f577
EH
595 *
596 * Publish and activation dates are set to "now" by default, but
597 * can be overridden. Creation date is always set to "now".
1f821c10
FD
598 */
599 if (!oldstyle) {
600 dst_key_settime(key, DST_TIME_CREATED, now);
601
b843f577
EH
602 if (genonly && (setpub || setact))
603 fatal("cannot use -G together with -P or -A options");
604
1f821c10
FD
605 if (setpub)
606 dst_key_settime(key, DST_TIME_PUBLISH, publish);
cef109ef
EH
607 else if (setact)
608 dst_key_settime(key, DST_TIME_PUBLISH, activate);
609 else if (!genonly && !unsetpub)
b843f577
EH
610 dst_key_settime(key, DST_TIME_PUBLISH, now);
611
1f821c10
FD
612 if (setact)
613 dst_key_settime(key, DST_TIME_ACTIVATE, activate);
cef109ef 614 else if (!genonly && !unsetact)
b843f577
EH
615 dst_key_settime(key, DST_TIME_ACTIVATE, now);
616
53c22b8e
EH
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);
1f821c10 624 dst_key_settime(key, DST_TIME_REVOKE, revoke);
53c22b8e 625 }
b843f577
EH
626
627 if (setinact)
628 dst_key_settime(key, DST_TIME_INACTIVE, inactive);
629
1f821c10 630 if (setdel)
4bf32aa5 631 dst_key_settime(key, DST_TIME_DELETE, deltime);
c69ad952
MA
632 if (setsyncadd)
633 dst_key_settime(key, DST_TIME_SYNCPUBLISH, syncadd);
634 if (setsyncdel)
635 dst_key_settime(key, DST_TIME_SYNCDELETE, syncdel);
e939674d 636
1f821c10 637 } else {
b843f577 638 if (setpub || setact || setrev || setinact ||
1f821c10 639 setdel || unsetpub || unsetact ||
e939674d
MA
640 unsetrev || unsetinact || unsetdel || genonly ||
641 setsyncadd || setsyncdel)
1f821c10 642 fatal("cannot use -C together with "
b843f577 643 "-P, -A, -R, -I, -D, or -G options");
1f821c10
FD
644 /*
645 * Compatibility mode: Private-key-format
646 * should be set to 1.2.
647 */
648 dst_key_setprivateformat(key, 1, 2);
649 }
650
61bcc232
EH
651 /* Set default key TTL */
652 if (setttl)
653 dst_key_setttl(key, ttl);
654
2a31bd53 655 /*
8a198fa7
EH
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.
2a31bd53 659 */
1946c596 660 if (key_collision(key, name, directory, mctx, &exact)) {
2a31bd53 661 isc_buffer_clear(&buf);
553ead32 662 ret = dst_key_buildfilename(key, 0, directory, &buf);
0874abad
MA
663 if (ret != ISC_R_SUCCESS)
664 fatal("dst_key_buildfilename returned: %s\n",
665 isc_result_totext(ret));
8a198fa7
EH
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);
2a31bd53
FD
678 }
679
553ead32 680 ret = dst_key_tofile(key, options, directory);
2a31bd53 681 if (ret != ISC_R_SUCCESS) {
77b8f88f
EH
682 char keystr[DST_KEY_FORMATSIZE];
683 dst_key_format(key, keystr, sizeof(keystr));
2a31bd53
FD
684 fatal("failed to write key %s: %s\n", keystr,
685 isc_result_totext(ret));
686 }
687
688 isc_buffer_clear(&buf);
1f821c10 689 ret = dst_key_buildfilename(key, 0, NULL, &buf);
0874abad
MA
690 if (ret != ISC_R_SUCCESS)
691 fatal("dst_key_buildfilename returned: %s\n",
692 isc_result_totext(ret));
2a31bd53
FD
693 printf("%s\n", filename);
694 dst_key_free(&key);
a60bf97f
EH
695 if (prevkey != NULL)
696 dst_key_free(&prevkey);
2a31bd53
FD
697
698 cleanup_logging(&log);
2a31bd53
FD
699 dst_lib_destroy();
700 dns_name_destroy();
701 if (verbose > 10)
702 isc_mem_stats(mctx, stdout);
6f6f08b7 703 isc_mem_free(mctx, label);
2a31bd53
FD
704 isc_mem_destroy(&mctx);
705
411d2914
MA
706 if (freeit != NULL)
707 free(freeit);
708
2a31bd53
FD
709 return (0);
710}