2 * Portions Copyright (C) 2004-2015 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
12 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
24 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
26 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <isc/buffer.h>
41 #include <isc/commandline.h>
42 #include <isc/entropy.h>
44 #include <isc/print.h>
45 #include <isc/region.h>
46 #include <isc/string.h>
49 #include <dns/dnssec.h>
50 #include <dns/fixedname.h>
51 #include <dns/keyvalues.h>
54 #include <dns/rdataclass.h>
55 #include <dns/result.h>
56 #include <dns/secalg.h>
60 #include "dnssectool.h"
62 #define MAX_RSA 4096 /* should be long enough... */
64 const char *program = "dnssec-keygen";
67 #define DEFAULT_ALGORITHM "RSASHA1"
68 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
70 ISC_PLATFORM_NORETURN_PRE static void
71 usage(void) ISC_PLATFORM_NORETURN_POST;
73 static void progress(int p);
77 fprintf(stderr, "Usage:\n");
78 fprintf(stderr, " %s [options] name\n\n", program);
79 fprintf(stderr, "Version: %s\n", VERSION);
80 fprintf(stderr, " name: owner of the key\n");
81 fprintf(stderr, "Options:\n");
82 fprintf(stderr, " -K <directory>: write keys into directory\n");
83 fprintf(stderr, " -a <algorithm>:\n");
84 fprintf(stderr, " RSA | RSAMD5 | DSA | RSASHA1 | NSEC3RSASHA1"
86 fprintf(stderr, " RSASHA256 | RSASHA512 | ECCGOST |\n");
87 fprintf(stderr, " ECDSAP256SHA256 | ECDSAP384SHA384 |\n");
88 fprintf(stderr, " DH | HMAC-MD5 | HMAC-SHA1 | HMAC-SHA224 | "
90 fprintf(stderr, " HMAC-SHA384 | HMAC-SHA512\n");
91 fprintf(stderr, " (default: RSASHA1, or "
92 "NSEC3RSASHA1 if using -3)\n");
93 fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
94 fprintf(stderr, " -b <key size in bits>:\n");
95 fprintf(stderr, " RSAMD5:\t[512..%d]\n", MAX_RSA);
96 fprintf(stderr, " RSASHA1:\t[512..%d]\n", MAX_RSA);
97 fprintf(stderr, " NSEC3RSASHA1:\t[512..%d]\n", MAX_RSA);
98 fprintf(stderr, " RSASHA256:\t[512..%d]\n", MAX_RSA);
99 fprintf(stderr, " RSASHA512:\t[1024..%d]\n", MAX_RSA);
100 fprintf(stderr, " DH:\t\t[128..4096]\n");
101 fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
102 fprintf(stderr, " NSEC3DSA:\t[512..1024] and divisible "
104 fprintf(stderr, " ECCGOST:\tignored\n");
105 fprintf(stderr, " ECDSAP256SHA256:\tignored\n");
106 fprintf(stderr, " ECDSAP384SHA384:\tignored\n");
107 fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
108 fprintf(stderr, " HMAC-SHA1:\t[1..160]\n");
109 fprintf(stderr, " HMAC-SHA224:\t[1..224]\n");
110 fprintf(stderr, " HMAC-SHA256:\t[1..256]\n");
111 fprintf(stderr, " HMAC-SHA384:\t[1..384]\n");
112 fprintf(stderr, " HMAC-SHA512:\t[1..512]\n");
113 fprintf(stderr, " (if using the default algorithm, key size\n"
114 " defaults to 2048 for KSK, or 1024 for all "
116 fprintf(stderr, " -n <nametype>: ZONE | HOST | ENTITY | "
118 fprintf(stderr, " (DNSKEY generation defaults to ZONE)\n");
119 fprintf(stderr, " -c <class>: (default: IN)\n");
120 fprintf(stderr, " -d <digest bits> (0 => max, default)\n");
122 fprintf(stderr, " -E <engine name> (default \"pkcs11\")\n");
124 fprintf(stderr, " -E <engine name>\n");
126 fprintf(stderr, " -f <keyflag>: KSK | REVOKE\n");
127 fprintf(stderr, " -g <generator>: use specified generator "
129 fprintf(stderr, " -L <ttl>: default key TTL\n");
130 fprintf(stderr, " -p <protocol>: (default: 3 [dnssec])\n");
131 fprintf(stderr, " -r <randomdev>: a file containing random data\n");
132 fprintf(stderr, " -s <strength>: strength value this key signs DNS "
133 "records with (default: 0)\n");
134 fprintf(stderr, " -T <rrtype>: DNSKEY | KEY (default: DNSKEY; "
135 "use KEY for SIG(0))\n");
136 fprintf(stderr, " ECCGOST:\tignored\n");
137 fprintf(stderr, " -t <type>: "
138 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
139 "(default: AUTHCONF)\n");
140 fprintf(stderr, " -h: print usage and exit\n");
141 fprintf(stderr, " -m <memory debugging mode>:\n");
142 fprintf(stderr, " usage | trace | record | size | mctx\n");
143 fprintf(stderr, " -v <level>: set verbosity level (0 - 10)\n");
144 fprintf(stderr, " -V: print version information\n");
145 fprintf(stderr, "Timing options:\n");
146 fprintf(stderr, " -P date/[+-]offset/none: set key publication date "
148 fprintf(stderr, " -A date/[+-]offset/none: set key activation date "
150 fprintf(stderr, " -R date/[+-]offset/none: set key "
151 "revocation date\n");
152 fprintf(stderr, " -I date/[+-]offset/none: set key "
153 "inactivation date\n");
154 fprintf(stderr, " -D date/[+-]offset/none: set key deletion date\n");
155 fprintf(stderr, " -G: generate key only; do not set -P or -A\n");
156 fprintf(stderr, " -C: generate a backward-compatible key, omitting "
158 fprintf(stderr, " -S <key>: generate a successor to an existing "
160 fprintf(stderr, " -i <interval>: prepublication interval for "
162 "(default: 30 days)\n");
163 fprintf(stderr, "Output:\n");
164 fprintf(stderr, " K<name>+<alg>+<id>.key, "
165 "K<name>+<alg>+<id>.private\n");
171 dsa_size_ok(int size) {
172 return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
196 (void) putc(c, stderr);
197 (void) fflush(stderr);
201 main(int argc, char **argv) {
202 char *algname = NULL, *freeit = NULL;
203 char *nametype = NULL, *type = NULL;
204 char *classname = NULL;
206 dst_key_t *key = NULL;
207 dns_fixedname_t fname;
209 isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
211 isc_boolean_t conflict = ISC_FALSE, null_key = ISC_FALSE;
212 isc_boolean_t oldstyle = ISC_FALSE;
213 isc_mem_t *mctx = NULL;
214 int ch, generator = 0, param = 0;
215 int protocol = -1, size = -1, signatory = 0;
219 const char *directory = NULL;
220 const char *predecessor = NULL;
221 dst_key_t *prevkey = NULL;
223 isc_log_t *log = NULL;
224 isc_entropy_t *ectx = NULL;
226 const char *engine = "pkcs11";
228 const char *engine = NULL;
230 dns_rdataclass_t rdclass;
231 int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
234 isc_boolean_t use_default = ISC_FALSE, use_nsec3 = ISC_FALSE;
235 isc_stdtime_t publish = 0, activate = 0, revokekey = 0;
236 isc_stdtime_t inactive = 0, delete = 0;
239 isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
240 isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
241 isc_boolean_t setdel = ISC_FALSE, setttl = ISC_FALSE;
242 isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
243 isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
244 isc_boolean_t unsetdel = ISC_FALSE;
245 isc_boolean_t genonly = ISC_FALSE;
246 isc_boolean_t quiet = ISC_FALSE;
247 isc_boolean_t show_progress = ISC_FALSE;
253 dns_result_register();
255 isc_commandline_errprint = ISC_FALSE;
258 * Process memory debugging argument first.
260 #define CMDLINE_FLAGS "3A:a:b:Cc:D:d:E:eFf:Gg:hI:i:K:kL:m:n:P:p:qR:r:S:s:T:t:" \
262 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
265 if (strcasecmp(isc_commandline_argument, "record") == 0)
266 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
267 if (strcasecmp(isc_commandline_argument, "trace") == 0)
268 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
269 if (strcasecmp(isc_commandline_argument, "usage") == 0)
270 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
271 if (strcasecmp(isc_commandline_argument, "size") == 0)
272 isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
273 if (strcasecmp(isc_commandline_argument, "mctx") == 0)
274 isc_mem_debugging |= ISC_MEM_DEBUGCTX;
280 isc_commandline_reset = ISC_TRUE;
282 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
284 isc_stdtime_get(&now);
286 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
289 use_nsec3 = ISC_TRUE;
292 algname = isc_commandline_argument;
295 size = strtol(isc_commandline_argument, &endp, 10);
296 if (*endp != '\0' || size < 0)
297 fatal("-b requires a non-negative number");
303 classname = isc_commandline_argument;
306 dbits = strtol(isc_commandline_argument, &endp, 10);
307 if (*endp != '\0' || dbits < 0)
308 fatal("-d requires a non-negative number");
311 engine = isc_commandline_argument;
315 "phased-out option -e "
316 "(was 'use (RSA) large exponent)\n");
319 c = (unsigned char)(isc_commandline_argument[0]);
320 if (toupper(c) == 'K')
321 kskflag = DNS_KEYFLAG_KSK;
322 else if (toupper(c) == 'R')
323 revflag = DNS_KEYFLAG_REVOKE;
325 fatal("unknown flag '%s'",
326 isc_commandline_argument);
329 generator = strtol(isc_commandline_argument,
331 if (*endp != '\0' || generator <= 0)
332 fatal("-g requires a positive number");
335 directory = isc_commandline_argument;
336 ret = try_dir(directory);
337 if (ret != ISC_R_SUCCESS)
338 fatal("cannot open directory %s: %s",
339 directory, isc_result_totext(ret));
342 fatal("The -k option has been deprecated.\n"
343 "To generate a key-signing key, use -f KSK.\n"
344 "To generate a key with TYPE=KEY, use -T KEY.\n");
347 ttl = strtottl(isc_commandline_argument);
351 nametype = isc_commandline_argument;
356 protocol = strtol(isc_commandline_argument, &endp, 10);
357 if (*endp != '\0' || protocol < 0 || protocol > 255)
358 fatal("-p must be followed by a number "
365 setup_entropy(mctx, isc_commandline_argument, &ectx);
368 signatory = strtol(isc_commandline_argument,
370 if (*endp != '\0' || signatory < 0 || signatory > 15)
371 fatal("-s must be followed by a number "
375 if (strcasecmp(isc_commandline_argument, "KEY") == 0)
376 options |= DST_TYPE_KEY;
377 else if (strcasecmp(isc_commandline_argument,
379 /* default behavior */
382 fatal("unknown type '%s'",
383 isc_commandline_argument);
386 type = isc_commandline_argument;
390 verbose = strtol(isc_commandline_argument, &endp, 0);
392 fatal("-v must be followed by a number");
395 /* already the default */
401 if (setpub || unsetpub)
402 fatal("-P specified more than once");
404 publish = strtotime(isc_commandline_argument,
409 if (setact || unsetact)
410 fatal("-A specified more than once");
412 activate = strtotime(isc_commandline_argument,
417 if (setrev || unsetrev)
418 fatal("-R specified more than once");
420 revokekey = strtotime(isc_commandline_argument,
425 if (setinact || unsetinact)
426 fatal("-I specified more than once");
428 inactive = strtotime(isc_commandline_argument,
429 now, now, &setinact);
430 unsetinact = !setinact;
433 if (setdel || unsetdel)
434 fatal("-D specified more than once");
436 delete = strtotime(isc_commandline_argument,
441 predecessor = isc_commandline_argument;
444 prepub = strtottl(isc_commandline_argument);
447 /* Reserved for FIPS mode */
450 if (isc_commandline_option != '?')
451 fprintf(stderr, "%s: invalid argument -%c\n",
452 program, isc_commandline_option);
455 /* Does not return. */
459 /* Does not return. */
463 fprintf(stderr, "%s: unhandled option -%c\n",
464 program, isc_commandline_option);
473 setup_entropy(mctx, NULL, &ectx);
474 ret = dst_lib_init2(mctx, ectx, engine,
475 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
476 if (ret != ISC_R_SUCCESS)
477 fatal("could not initialize dst: %s",
478 isc_result_totext(ret));
480 setup_logging(mctx, &log);
482 if (predecessor == NULL) {
486 if (argc < isc_commandline_index + 1)
487 fatal("the key name was not specified");
488 if (argc > isc_commandline_index + 1)
489 fatal("extraneous arguments");
491 dns_fixedname_init(&fname);
492 name = dns_fixedname_name(&fname);
493 isc_buffer_init(&buf, argv[isc_commandline_index],
494 strlen(argv[isc_commandline_index]));
495 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
496 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
497 if (ret != ISC_R_SUCCESS)
498 fatal("invalid key name %s: %s",
499 argv[isc_commandline_index],
500 isc_result_totext(ret));
502 if (algname == NULL) {
503 use_default = ISC_TRUE;
505 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
507 algname = strdup(DEFAULT_ALGORITHM);
509 fatal("strdup failed");
512 fprintf(stderr, "no algorithm specified; "
513 "defaulting to %s\n", algname);
516 if (strcasecmp(algname, "RSA") == 0) {
517 fprintf(stderr, "The use of RSA (RSAMD5) is not "
518 "recommended.\nIf you still wish to "
519 "use RSA (RSAMD5) please specify "
521 INSIST(freeit == NULL);
523 } else if (strcasecmp(algname, "HMAC-MD5") == 0)
524 alg = DST_ALG_HMACMD5;
525 else if (strcasecmp(algname, "HMAC-SHA1") == 0)
526 alg = DST_ALG_HMACSHA1;
527 else if (strcasecmp(algname, "HMAC-SHA224") == 0)
528 alg = DST_ALG_HMACSHA224;
529 else if (strcasecmp(algname, "HMAC-SHA256") == 0)
530 alg = DST_ALG_HMACSHA256;
531 else if (strcasecmp(algname, "HMAC-SHA384") == 0)
532 alg = DST_ALG_HMACSHA384;
533 else if (strcasecmp(algname, "HMAC-SHA512") == 0)
534 alg = DST_ALG_HMACSHA512;
537 r.length = strlen(algname);
538 ret = dns_secalg_fromtext(&alg, &r);
539 if (ret != ISC_R_SUCCESS)
540 fatal("unknown algorithm %s", algname);
541 if (alg == DST_ALG_DH)
542 options |= DST_TYPE_KEY;
545 if (!dst_algorithm_supported(alg))
546 fatal("unsupported algorithm: %d", alg);
549 alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
550 alg != DST_ALG_RSASHA256 && alg!= DST_ALG_RSASHA512 &&
551 alg != DST_ALG_ECCGOST &&
552 alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
553 fatal("%s is incompatible with NSEC3; "
554 "do not use the -3 option", algname);
557 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
558 if (strcasecmp(type, "NOAUTH") == 0)
559 flags |= DNS_KEYTYPE_NOAUTH;
560 else if (strcasecmp(type, "NOCONF") == 0)
561 flags |= DNS_KEYTYPE_NOCONF;
562 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
563 flags |= (DNS_KEYTYPE_NOAUTH |
568 else if (strcasecmp(type, "AUTHCONF") == 0)
571 fatal("invalid type %s", type);
576 if ((kskflag & DNS_KEYFLAG_KSK) != 0)
581 fprintf(stderr, "key size not "
582 "specified; defaulting"
584 } else if (alg != DST_ALG_ECCGOST &&
585 alg != DST_ALG_ECDSA256 &&
586 alg != DST_ALG_ECDSA384)
587 fatal("key size not specified (-b option)");
590 if (!oldstyle && prepub > 0) {
591 if (setpub && setact && (activate - prepub) < publish)
592 fatal("Activation and publication dates "
593 "are closer together than the\n\t"
594 "prepublication interval.");
596 if (!setpub && !setact) {
597 setpub = setact = ISC_TRUE;
599 activate = now + prepub;
600 } else if (setpub && !setact) {
602 activate = publish + prepub;
603 } else if (setact && !setpub) {
605 publish = activate - prepub;
608 if ((activate - prepub) < now)
609 fatal("Time until activation is shorter "
610 "than the\n\tprepublication interval.");
613 char keystr[DST_KEY_FORMATSIZE];
618 prepub = (30 * 86400);
621 fatal("-S and -a cannot be used together");
623 fatal("-S and -b cannot be used together");
624 if (nametype != NULL)
625 fatal("-S and -n cannot be used together");
627 fatal("-S and -t cannot be used together");
628 if (setpub || unsetpub)
629 fatal("-S and -P cannot be used together");
630 if (setact || unsetact)
631 fatal("-S and -A cannot be used together");
633 fatal("-S and -3 cannot be used together");
635 fatal("-S and -C cannot be used together");
637 fatal("-S and -G cannot be used together");
639 ret = dst_key_fromnamedfile(predecessor, directory,
640 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
642 if (ret != ISC_R_SUCCESS)
643 fatal("Invalid keyfile %s: %s",
644 predecessor, isc_result_totext(ret));
645 if (!dst_key_isprivate(prevkey))
646 fatal("%s is not a private key", predecessor);
648 name = dst_key_name(prevkey);
649 alg = dst_key_alg(prevkey);
650 size = dst_key_size(prevkey);
651 flags = dst_key_flags(prevkey);
653 dst_key_format(prevkey, keystr, sizeof(keystr));
654 dst_key_getprivateformat(prevkey, &major, &minor);
655 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
656 fatal("Key %s has incompatible format version %d.%d\n\t"
657 "It is not possible to generate a successor key.",
658 keystr, major, minor);
660 ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
661 if (ret != ISC_R_SUCCESS)
662 fatal("Key %s has no activation date.\n\t"
663 "You must use dnssec-settime -A to set one "
664 "before generating a successor.", keystr);
666 ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
667 if (ret != ISC_R_SUCCESS)
668 fatal("Key %s has no inactivation date.\n\t"
669 "You must use dnssec-settime -I to set one "
670 "before generating a successor.", keystr);
672 publish = activate - prepub;
674 fatal("Key %s becomes inactive\n\t"
675 "sooner than the prepublication period "
676 "for the new key ends.\n\t"
677 "Either change the inactivation date with "
678 "dnssec-settime -I,\n\t"
679 "or use the -i option to set a shorter "
680 "prepublication interval.", keystr);
682 ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
683 if (ret != ISC_R_SUCCESS)
684 fprintf(stderr, "%s: WARNING: Key %s has no removal "
685 "date;\n\t it will remain in the zone "
686 "indefinitely after rollover.\n\t "
687 "You can use dnssec-settime -D to "
688 "change this.\n", program, keystr);
690 setpub = setact = ISC_TRUE;
694 case DNS_KEYALG_RSAMD5:
695 case DNS_KEYALG_RSASHA1:
696 case DNS_KEYALG_NSEC3RSASHA1:
697 case DNS_KEYALG_RSASHA256:
698 if (size != 0 && (size < 512 || size > MAX_RSA))
699 fatal("RSA key size %d out of range", size);
701 case DNS_KEYALG_RSASHA512:
702 if (size != 0 && (size < 1024 || size > MAX_RSA))
703 fatal("RSA key size %d out of range", size);
706 if (size != 0 && (size < 128 || size > 4096))
707 fatal("DH key size %d out of range", size);
710 case DNS_KEYALG_NSEC3DSA:
711 if (size != 0 && !dsa_size_ok(size))
712 fatal("invalid DSS key size: %d", size);
714 case DST_ALG_ECCGOST:
717 case DST_ALG_ECDSA256:
720 case DST_ALG_ECDSA384:
723 case DST_ALG_HMACMD5:
724 options |= DST_TYPE_KEY;
725 if (size < 1 || size > 512)
726 fatal("HMAC-MD5 key size %d out of range", size);
727 if (dbits != 0 && (dbits < 80 || dbits > 128))
728 fatal("HMAC-MD5 digest bits %d out of range", dbits);
729 if ((dbits % 8) != 0)
730 fatal("HMAC-MD5 digest bits %d not divisible by 8",
733 case DST_ALG_HMACSHA1:
734 options |= DST_TYPE_KEY;
735 if (size < 1 || size > 160)
736 fatal("HMAC-SHA1 key size %d out of range", size);
737 if (dbits != 0 && (dbits < 80 || dbits > 160))
738 fatal("HMAC-SHA1 digest bits %d out of range", dbits);
739 if ((dbits % 8) != 0)
740 fatal("HMAC-SHA1 digest bits %d not divisible by 8",
743 case DST_ALG_HMACSHA224:
744 options |= DST_TYPE_KEY;
745 if (size < 1 || size > 224)
746 fatal("HMAC-SHA224 key size %d out of range", size);
747 if (dbits != 0 && (dbits < 112 || dbits > 224))
748 fatal("HMAC-SHA224 digest bits %d out of range", dbits);
749 if ((dbits % 8) != 0)
750 fatal("HMAC-SHA224 digest bits %d not divisible by 8",
753 case DST_ALG_HMACSHA256:
754 options |= DST_TYPE_KEY;
755 if (size < 1 || size > 256)
756 fatal("HMAC-SHA256 key size %d out of range", size);
757 if (dbits != 0 && (dbits < 128 || dbits > 256))
758 fatal("HMAC-SHA256 digest bits %d out of range", dbits);
759 if ((dbits % 8) != 0)
760 fatal("HMAC-SHA256 digest bits %d not divisible by 8",
763 case DST_ALG_HMACSHA384:
764 options |= DST_TYPE_KEY;
765 if (size < 1 || size > 384)
766 fatal("HMAC-384 key size %d out of range", size);
767 if (dbits != 0 && (dbits < 192 || dbits > 384))
768 fatal("HMAC-SHA384 digest bits %d out of range", dbits);
769 if ((dbits % 8) != 0)
770 fatal("HMAC-SHA384 digest bits %d not divisible by 8",
773 case DST_ALG_HMACSHA512:
774 options |= DST_TYPE_KEY;
775 if (size < 1 || size > 512)
776 fatal("HMAC-SHA512 key size %d out of range", size);
777 if (dbits != 0 && (dbits < 256 || dbits > 512))
778 fatal("HMAC-SHA512 digest bits %d out of range", dbits);
779 if ((dbits % 8) != 0)
780 fatal("HMAC-SHA512 digest bits %d not divisible by 8",
785 if (alg != DNS_KEYALG_DH && generator != 0)
786 fatal("specified DH generator for a non-DH key");
788 if (nametype == NULL) {
789 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
790 fatal("no nametype specified");
791 flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
792 } else if (strcasecmp(nametype, "zone") == 0)
793 flags |= DNS_KEYOWNER_ZONE;
794 else if ((options & DST_TYPE_KEY) != 0) { /* KEY / HMAC */
795 if (strcasecmp(nametype, "host") == 0 ||
796 strcasecmp(nametype, "entity") == 0)
797 flags |= DNS_KEYOWNER_ENTITY;
798 else if (strcasecmp(nametype, "user") == 0)
799 flags |= DNS_KEYOWNER_USER;
801 fatal("invalid KEY nametype %s", nametype);
802 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
803 fatal("invalid DNSKEY nametype %s", nametype);
805 rdclass = strtoclass(classname);
807 if (directory == NULL)
810 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
812 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
818 protocol = DNS_KEYPROTO_DNSSEC;
819 else if ((options & DST_TYPE_KEY) == 0 &&
820 protocol != DNS_KEYPROTO_DNSSEC)
821 fatal("invalid DNSKEY protocol: %d", protocol);
823 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
825 fatal("specified null key with non-zero size");
826 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
827 fatal("specified null key with signing authority");
830 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
831 (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5 ||
832 alg == DST_ALG_HMACSHA1 || alg == DST_ALG_HMACSHA224 ||
833 alg == DST_ALG_HMACSHA256 || alg == DST_ALG_HMACSHA384 ||
834 alg == DST_ALG_HMACSHA512))
835 fatal("a key with algorithm '%s' cannot be a zone key",
839 case DNS_KEYALG_RSAMD5:
840 case DNS_KEYALG_RSASHA1:
841 case DNS_KEYALG_NSEC3RSASHA1:
842 case DNS_KEYALG_RSASHA256:
843 case DNS_KEYALG_RSASHA512:
844 show_progress = ISC_TRUE;
852 case DNS_KEYALG_NSEC3DSA:
853 case DST_ALG_ECCGOST:
854 case DST_ALG_ECDSA256:
855 case DST_ALG_ECDSA384:
856 show_progress = ISC_TRUE;
859 case DST_ALG_HMACMD5:
860 case DST_ALG_HMACSHA1:
861 case DST_ALG_HMACSHA224:
862 case DST_ALG_HMACSHA256:
863 case DST_ALG_HMACSHA384:
864 case DST_ALG_HMACSHA512:
869 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
872 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
875 conflict = ISC_FALSE;
877 if (!quiet && show_progress) {
878 fprintf(stderr, "Generating key pair.");
879 ret = dst_key_generate2(name, alg, size, param, flags,
880 protocol, rdclass, mctx, &key,
885 ret = dst_key_generate2(name, alg, size, param, flags,
886 protocol, rdclass, mctx, &key,
890 isc_entropy_stopcallbacksources(ectx);
892 if (ret != ISC_R_SUCCESS) {
893 char namestr[DNS_NAME_FORMATSIZE];
894 char algstr[DNS_SECALG_FORMATSIZE];
895 dns_name_format(name, namestr, sizeof(namestr));
896 dns_secalg_format(alg, algstr, sizeof(algstr));
897 fatal("failed to generate key %s/%s: %s\n",
898 namestr, algstr, isc_result_totext(ret));
903 dst_key_setbits(key, dbits);
906 * Set key timing metadata (unless using -C)
908 * Creation date is always set to "now".
910 * For a new key without an explicit predecessor, publish
911 * and activation dates are set to "now" by default, but
912 * can both be overridden.
914 * For a successor key, activation is set to match the
915 * predecessor's inactivation date. Publish is set to 30
916 * days earlier than that (XXX: this should be configurable).
917 * If either of the resulting dates are in the past, that's
918 * an error; the inactivation date of the predecessor key
919 * must be updated before a successor key can be created.
922 dst_key_settime(key, DST_TIME_CREATED, now);
924 if (genonly && (setpub || setact))
925 fatal("cannot use -G together with "
929 dst_key_settime(key, DST_TIME_PUBLISH, publish);
930 else if (setact && !unsetpub)
931 dst_key_settime(key, DST_TIME_PUBLISH,
933 else if (!genonly && !unsetpub)
934 dst_key_settime(key, DST_TIME_PUBLISH, now);
937 dst_key_settime(key, DST_TIME_ACTIVATE,
939 else if (!genonly && !unsetact)
940 dst_key_settime(key, DST_TIME_ACTIVATE, now);
944 fprintf(stderr, "%s: warning: Key is "
945 "not flagged as a KSK, but -R "
946 "was used. Revoking a ZSK is "
947 "legal, but undefined.\n",
949 dst_key_settime(key, DST_TIME_REVOKE, revokekey);
953 dst_key_settime(key, DST_TIME_INACTIVE,
957 if (setinact && delete < inactive)
958 fprintf(stderr, "%s: warning: Key is "
959 "scheduled to be deleted "
960 "before it is scheduled to be "
963 dst_key_settime(key, DST_TIME_DELETE, delete);
966 if (setpub || setact || setrev || setinact ||
967 setdel || unsetpub || unsetact ||
968 unsetrev || unsetinact || unsetdel || genonly)
969 fatal("cannot use -C together with "
970 "-P, -A, -R, -I, -D, or -G options");
972 * Compatibility mode: Private-key-format
973 * should be set to 1.2.
975 dst_key_setprivateformat(key, 1, 2);
978 /* Set the default key TTL */
980 dst_key_setttl(key, ttl);
983 * Do not overwrite an existing key, or create a key
984 * if there is a risk of ID collision due to this key
985 * or another key being revoked.
987 if (key_collision(key, name, directory, mctx, NULL)) {
995 isc_buffer_clear(&buf);
996 ret = dst_key_buildfilename(key, 0,
998 if (ret == ISC_R_SUCCESS)
1000 "%s: %s already exists, or "
1001 "might collide with another "
1002 "key upon revokation. "
1003 "Generating a new key\n",
1009 } while (conflict == ISC_TRUE);
1012 fatal("cannot generate a null key due to possible key ID "
1015 ret = dst_key_tofile(key, options, directory);
1016 if (ret != ISC_R_SUCCESS) {
1017 char keystr[DST_KEY_FORMATSIZE];
1018 dst_key_format(key, keystr, sizeof(keystr));
1019 fatal("failed to write key %s: %s\n", keystr,
1020 isc_result_totext(ret));
1023 isc_buffer_clear(&buf);
1024 ret = dst_key_buildfilename(key, 0, NULL, &buf);
1025 if (ret != ISC_R_SUCCESS)
1026 fatal("dst_key_buildfilename returned: %s\n",
1027 isc_result_totext(ret));
1028 printf("%s\n", filename);
1030 if (prevkey != NULL)
1031 dst_key_free(&prevkey);
1033 cleanup_logging(&log);
1034 cleanup_entropy(&ectx);
1038 isc_mem_stats(mctx, stdout);
1039 isc_mem_destroy(&mctx);