2 * Copyright (C) 2007-2012, 2014 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
24 #include <isc/buffer.h>
25 #include <isc/commandline.h>
26 #include <isc/entropy.h>
28 #include <isc/region.h>
29 #include <isc/print.h>
30 #include <isc/string.h>
33 #include <dns/dnssec.h>
34 #include <dns/fixedname.h>
35 #include <dns/keyvalues.h>
38 #include <dns/rdataclass.h>
39 #include <dns/result.h>
40 #include <dns/secalg.h>
44 #include "dnssectool.h"
46 #define MAX_RSA 4096 /* should be long enough... */
48 const char *program = "dnssec-keyfromlabel";
51 #define DEFAULT_ALGORITHM "RSASHA1"
52 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
54 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
55 " NSEC3DSA | NSEC3RSASHA1 |"
56 " RSASHA256 | RSASHA512 | ECCGOST |"
57 " ECDSAP256SHA256 | ECDSAP384SHA384";
59 ISC_PLATFORM_NORETURN_PRE static void
60 usage(void) ISC_PLATFORM_NORETURN_POST;
64 fprintf(stderr, "Usage:\n");
65 fprintf(stderr, " %s -l label [options] name\n\n",
67 fprintf(stderr, "Version: %s\n", VERSION);
68 fprintf(stderr, "Required options:\n");
69 fprintf(stderr, " -l label: label of the key pair\n");
70 fprintf(stderr, " name: owner of the key\n");
71 fprintf(stderr, "Other options:\n");
72 fprintf(stderr, " -a algorithm: %s\n", algs);
73 fprintf(stderr, " (default: RSASHA1, or "
74 "NSEC3RSASHA1 if using -3)\n");
75 fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
76 fprintf(stderr, " -c class (default: IN)\n");
78 fprintf(stderr, " -E enginename (default: pkcs11)\n");
80 fprintf(stderr, " -E enginename\n");
82 fprintf(stderr, " -f keyflag: KSK | REVOKE\n");
83 fprintf(stderr, " -K directory: directory in which to place "
85 fprintf(stderr, " -k: generate a TYPE=KEY key\n");
86 fprintf(stderr, " -L ttl: default key TTL\n");
87 fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
88 fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
89 fprintf(stderr, " -p protocol: default: 3 [dnssec]\n");
90 fprintf(stderr, " -t type: "
91 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
92 "(default: AUTHCONF)\n");
93 fprintf(stderr, " -y: permit keys that might collide\n");
94 fprintf(stderr, " -v verbose level\n");
95 fprintf(stderr, " -V: print version information\n");
96 fprintf(stderr, "Date options:\n");
97 fprintf(stderr, " -P date/[+-]offset: set key publication date\n");
98 fprintf(stderr, " -A date/[+-]offset: set key activation date\n");
99 fprintf(stderr, " -R date/[+-]offset: set key revocation date\n");
100 fprintf(stderr, " -I date/[+-]offset: set key inactivation date\n");
101 fprintf(stderr, " -D date/[+-]offset: set key 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"
105 fprintf(stderr, " -S <key>: generate a successor to an existing "
107 fprintf(stderr, " -i <interval>: prepublication interval for "
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");
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;
125 const char *engine = "pkcs11";
127 const char *engine = NULL;
129 char *classname = NULL;
131 dst_key_t *key = NULL;
132 dns_fixedname_t fname;
134 isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
136 isc_boolean_t oldstyle = ISC_FALSE;
137 isc_mem_t *mctx = NULL;
139 int protocol = -1, signatory = 0;
144 isc_log_t *log = NULL;
145 isc_entropy_t *ectx = NULL;
146 dns_rdataclass_t rdclass;
147 int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
150 isc_stdtime_t publish = 0, activate = 0, revoke = 0;
151 isc_stdtime_t inactive = 0, delete = 0;
154 isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
155 isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
156 isc_boolean_t setdel = ISC_FALSE, setttl = ISC_FALSE;
157 isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
158 isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
159 isc_boolean_t unsetdel = ISC_FALSE;
160 isc_boolean_t genonly = ISC_FALSE;
161 isc_boolean_t use_nsec3 = ISC_FALSE;
162 isc_boolean_t avoid_collisions = ISC_TRUE;
169 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
171 dns_result_register();
173 isc_commandline_errprint = ISC_FALSE;
175 isc_stdtime_get(&now);
177 #define CMDLINE_FLAGS "3A:a:Cc:D:E:Ff:GhI:i:kK:L:l:n:P:p:R:S:t:v:Vy"
178 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
181 use_nsec3 = ISC_TRUE;
184 algname = isc_commandline_argument;
190 classname = isc_commandline_argument;
193 engine = isc_commandline_argument;
196 c = (unsigned char)(isc_commandline_argument[0]);
197 if (toupper(c) == 'K')
198 kskflag = DNS_KEYFLAG_KSK;
199 else if (toupper(c) == 'R')
200 revflag = DNS_KEYFLAG_REVOKE;
202 fatal("unknown flag '%s'",
203 isc_commandline_argument);
206 directory = isc_commandline_argument;
207 ret = try_dir(directory);
208 if (ret != ISC_R_SUCCESS)
209 fatal("cannot open directory %s: %s",
210 directory, isc_result_totext(ret));
213 options |= DST_TYPE_KEY;
216 ttl = strtottl(isc_commandline_argument);
220 label = isc_mem_strdup(mctx, isc_commandline_argument);
223 nametype = isc_commandline_argument;
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 "
232 type = isc_commandline_argument;
235 verbose = strtol(isc_commandline_argument, &endp, 0);
237 fatal("-v must be followed by a number");
240 avoid_collisions = ISC_FALSE;
246 if (setpub || unsetpub)
247 fatal("-P specified more than once");
249 publish = strtotime(isc_commandline_argument,
254 if (setact || unsetact)
255 fatal("-A specified more than once");
257 activate = strtotime(isc_commandline_argument,
262 if (setrev || unsetrev)
263 fatal("-R specified more than once");
265 revoke = strtotime(isc_commandline_argument,
270 if (setinact || unsetinact)
271 fatal("-I specified more than once");
273 inactive = strtotime(isc_commandline_argument,
274 now, now, &setinact);
275 unsetinact = !setinact;
278 if (setdel || unsetdel)
279 fatal("-D specified more than once");
281 delete = strtotime(isc_commandline_argument,
286 predecessor = isc_commandline_argument;
289 prepub = strtottl(isc_commandline_argument);
292 /* Reserved for FIPS mode */
295 if (isc_commandline_option != '?')
296 fprintf(stderr, "%s: invalid argument -%c\n",
297 program, isc_commandline_option);
300 /* Does not return. */
304 /* Does not return. */
308 fprintf(stderr, "%s: unhandled option -%c\n",
309 program, isc_commandline_option);
315 setup_entropy(mctx, NULL, &ectx);
316 ret = dst_lib_init2(mctx, ectx, engine,
317 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
318 if (ret != ISC_R_SUCCESS)
319 fatal("could not initialize dst: %s",
320 isc_result_totext(ret));
322 setup_logging(verbose, mctx, &log);
324 if (predecessor == NULL) {
326 fatal("the key label was not specified");
327 if (argc < isc_commandline_index + 1)
328 fatal("the key name was not specified");
329 if (argc > isc_commandline_index + 1)
330 fatal("extraneous arguments");
332 dns_fixedname_init(&fname);
333 name = dns_fixedname_name(&fname);
334 isc_buffer_init(&buf, argv[isc_commandline_index],
335 strlen(argv[isc_commandline_index]));
336 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
337 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
338 if (ret != ISC_R_SUCCESS)
339 fatal("invalid key name %s: %s",
340 argv[isc_commandline_index],
341 isc_result_totext(ret));
343 if (strchr(label, ':') == NULL) {
347 len = strlen(label) + 8;
348 l = isc_mem_allocate(mctx, len);
350 fatal("cannot allocate memory");
351 snprintf(l, len, "pkcs11:%s", label);
352 isc_mem_free(mctx, label);
356 if (algname == NULL) {
358 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
360 algname = strdup(DEFAULT_ALGORITHM);
362 fatal("strdup failed");
365 fprintf(stderr, "no algorithm specified; "
366 "defaulting to %s\n", algname);
369 if (strcasecmp(algname, "RSA") == 0) {
370 fprintf(stderr, "The use of RSA (RSAMD5) is not "
371 "recommended.\nIf you still wish to "
372 "use RSA (RSAMD5) please specify "
379 r.length = strlen(algname);
380 ret = dns_secalg_fromtext(&alg, &r);
381 if (ret != ISC_R_SUCCESS)
382 fatal("unknown algorithm %s", algname);
383 if (alg == DST_ALG_DH)
384 options |= DST_TYPE_KEY;
388 alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
389 alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512 &&
390 alg != DST_ALG_ECCGOST &&
391 alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
392 fatal("%s is incompatible with NSEC3; "
393 "do not use the -3 option", algname);
396 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
397 if (strcasecmp(type, "NOAUTH") == 0)
398 flags |= DNS_KEYTYPE_NOAUTH;
399 else if (strcasecmp(type, "NOCONF") == 0)
400 flags |= DNS_KEYTYPE_NOCONF;
401 else if (strcasecmp(type, "NOAUTHCONF") == 0)
402 flags |= (DNS_KEYTYPE_NOAUTH |
404 else if (strcasecmp(type, "AUTHCONF") == 0)
407 fatal("invalid type %s", type);
410 if (!oldstyle && prepub > 0) {
411 if (setpub && setact && (activate - prepub) < publish)
412 fatal("Activation and publication dates "
413 "are closer together than the\n\t"
414 "prepublication interval.");
416 if (!setpub && !setact) {
417 setpub = setact = ISC_TRUE;
419 activate = now + prepub;
420 } else if (setpub && !setact) {
422 activate = publish + prepub;
423 } else if (setact && !setpub) {
425 publish = activate - prepub;
428 if ((activate - prepub) < now)
429 fatal("Time until activation is shorter "
430 "than the\n\tprepublication interval.");
433 char keystr[DST_KEY_FORMATSIZE];
438 prepub = (30 * 86400);
441 fatal("-S and -a cannot be used together");
442 if (nametype != NULL)
443 fatal("-S and -n cannot be used together");
445 fatal("-S and -t cannot be used together");
446 if (setpub || unsetpub)
447 fatal("-S and -P cannot be used together");
448 if (setact || unsetact)
449 fatal("-S and -A cannot be used together");
451 fatal("-S and -3 cannot be used together");
453 fatal("-S and -C cannot be used together");
455 fatal("-S and -G cannot be used together");
457 ret = dst_key_fromnamedfile(predecessor, directory,
458 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
460 if (ret != ISC_R_SUCCESS)
461 fatal("Invalid keyfile %s: %s",
462 predecessor, isc_result_totext(ret));
463 if (!dst_key_isprivate(prevkey))
464 fatal("%s is not a private key", predecessor);
466 name = dst_key_name(prevkey);
467 alg = dst_key_alg(prevkey);
468 flags = dst_key_flags(prevkey);
470 dst_key_format(prevkey, keystr, sizeof(keystr));
471 dst_key_getprivateformat(prevkey, &major, &minor);
472 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
473 fatal("Key %s has incompatible format version %d.%d\n\t"
474 "It is not possible to generate a successor key.",
475 keystr, major, minor);
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.", keystr);
483 ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
484 if (ret != ISC_R_SUCCESS)
485 fatal("Key %s has no inactivation date.\n\t"
486 "You must use dnssec-settime -I to set one "
487 "before generating a successor.", keystr);
489 publish = activate - prepub;
491 fatal("Key %s becomes inactive\n\t"
492 "sooner than the prepublication period "
493 "for the new key ends.\n\t"
494 "Either change the inactivation date with "
495 "dnssec-settime -I,\n\t"
496 "or use the -i option to set a shorter "
497 "prepublication interval.", keystr);
499 ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
500 if (ret != ISC_R_SUCCESS)
501 fprintf(stderr, "%s: WARNING: Key %s has no removal "
502 "date;\n\t it will remain in the zone "
503 "indefinitely after rollover.\n\t "
504 "You can use dnssec-settime -D to "
505 "change this.\n", program, keystr);
507 setpub = setact = ISC_TRUE;
510 if (nametype == NULL) {
511 if ((options & DST_TYPE_KEY) != 0) /* KEY */
512 fatal("no nametype specified");
513 flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
514 } else if (strcasecmp(nametype, "zone") == 0)
515 flags |= DNS_KEYOWNER_ZONE;
516 else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
517 if (strcasecmp(nametype, "host") == 0 ||
518 strcasecmp(nametype, "entity") == 0)
519 flags |= DNS_KEYOWNER_ENTITY;
520 else if (strcasecmp(nametype, "user") == 0)
521 flags |= DNS_KEYOWNER_USER;
523 fatal("invalid KEY nametype %s", nametype);
524 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
525 fatal("invalid DNSKEY nametype %s", nametype);
527 rdclass = strtoclass(classname);
529 if (directory == NULL)
532 if ((options & DST_TYPE_KEY) != 0) /* KEY */
534 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
540 protocol = DNS_KEYPROTO_DNSSEC;
541 else if ((options & DST_TYPE_KEY) == 0 &&
542 protocol != DNS_KEYPROTO_DNSSEC)
543 fatal("invalid DNSKEY protocol: %d", protocol);
545 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
546 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
547 fatal("specified null key with signing authority");
550 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
551 alg == DNS_KEYALG_DH)
552 fatal("a key with algorithm '%s' cannot be a zone key",
555 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
557 /* associate the key */
558 ret = dst_key_fromlabel(name, alg, flags, protocol,
559 rdclass, engine, label, NULL, mctx, &key);
560 isc_entropy_stopcallbacksources(ectx);
562 if (ret != ISC_R_SUCCESS) {
563 char namestr[DNS_NAME_FORMATSIZE];
564 char algstr[DNS_SECALG_FORMATSIZE];
565 dns_name_format(name, namestr, sizeof(namestr));
566 dns_secalg_format(alg, algstr, sizeof(algstr));
567 fatal("failed to get key %s/%s: %s\n",
568 namestr, algstr, isc_result_totext(ret));
574 * Set key timing metadata (unless using -C)
576 * Publish and activation dates are set to "now" by default, but
577 * can be overridden. Creation date is always set to "now".
580 dst_key_settime(key, DST_TIME_CREATED, now);
582 if (genonly && (setpub || setact))
583 fatal("cannot use -G together with -P or -A options");
586 dst_key_settime(key, DST_TIME_PUBLISH, publish);
588 dst_key_settime(key, DST_TIME_PUBLISH, activate);
589 else if (!genonly && !unsetpub)
590 dst_key_settime(key, DST_TIME_PUBLISH, now);
593 dst_key_settime(key, DST_TIME_ACTIVATE, activate);
594 else if (!genonly && !unsetact)
595 dst_key_settime(key, DST_TIME_ACTIVATE, now);
599 fprintf(stderr, "%s: warning: Key is "
600 "not flagged as a KSK, but -R "
601 "was used. Revoking a ZSK is "
602 "legal, but undefined.\n",
604 dst_key_settime(key, DST_TIME_REVOKE, revoke);
608 dst_key_settime(key, DST_TIME_INACTIVE, inactive);
611 dst_key_settime(key, DST_TIME_DELETE, delete);
613 if (setpub || setact || setrev || setinact ||
614 setdel || unsetpub || unsetact ||
615 unsetrev || unsetinact || unsetdel || genonly)
616 fatal("cannot use -C together with "
617 "-P, -A, -R, -I, -D, or -G options");
619 * Compatibility mode: Private-key-format
620 * should be set to 1.2.
622 dst_key_setprivateformat(key, 1, 2);
625 /* Set default key TTL */
627 dst_key_setttl(key, ttl);
630 * Do not overwrite an existing key. Warn LOUDLY if there
631 * is a risk of ID collision due to this key or another key
634 if (key_collision(key, name, directory, mctx, &exact)) {
635 isc_buffer_clear(&buf);
636 ret = dst_key_buildfilename(key, 0, directory, &buf);
637 if (ret != ISC_R_SUCCESS)
638 fatal("dst_key_buildfilename returned: %s\n",
639 isc_result_totext(ret));
641 fatal("%s: %s already exists\n", program, filename);
643 if (avoid_collisions)
644 fatal("%s: %s could collide with another key upon "
645 "revokation\n", program, filename);
647 fprintf(stderr, "%s: WARNING: Key %s could collide with "
648 "another key upon revokation. If you plan "
649 "to revoke keys, destroy this key and "
650 "generate a different one.\n",
654 ret = dst_key_tofile(key, options, directory);
655 if (ret != ISC_R_SUCCESS) {
656 char keystr[DST_KEY_FORMATSIZE];
657 dst_key_format(key, keystr, sizeof(keystr));
658 fatal("failed to write key %s: %s\n", keystr,
659 isc_result_totext(ret));
662 isc_buffer_clear(&buf);
663 ret = dst_key_buildfilename(key, 0, NULL, &buf);
664 if (ret != ISC_R_SUCCESS)
665 fatal("dst_key_buildfilename returned: %s\n",
666 isc_result_totext(ret));
667 printf("%s\n", filename);
670 dst_key_free(&prevkey);
672 cleanup_logging(&log);
673 cleanup_entropy(&ectx);
677 isc_mem_stats(mctx, stdout);
678 isc_mem_free(mctx, label);
679 isc_mem_destroy(&mctx);