2 * Portions Copyright (C) 2004-2013 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.
32 /* $Id: dnssec-keygen.c,v 1.120 2011/11/30 00:48:51 marka Exp $ */
42 #include <isc/buffer.h>
43 #include <isc/commandline.h>
44 #include <isc/entropy.h>
46 #include <isc/region.h>
47 #include <isc/string.h>
50 #include <dns/dnssec.h>
51 #include <dns/fixedname.h>
52 #include <dns/keyvalues.h>
55 #include <dns/rdataclass.h>
56 #include <dns/result.h>
57 #include <dns/secalg.h>
61 #include "dnssectool.h"
63 #define MAX_RSA 4096 /* should be long enough... */
65 const char *program = "dnssec-keygen";
68 #define DEFAULT_ALGORITHM "RSASHA1"
69 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
71 ISC_PLATFORM_NORETURN_PRE static void
72 usage(void) ISC_PLATFORM_NORETURN_POST;
74 static void progress(int p);
78 fprintf(stderr, "Usage:\n");
79 fprintf(stderr, " %s [options] name\n\n", program);
80 fprintf(stderr, "Version: %s\n", VERSION);
81 fprintf(stderr, " name: owner of the key\n");
82 fprintf(stderr, "Options:\n");
83 fprintf(stderr, " -K <directory>: write keys into directory\n");
84 fprintf(stderr, " -a <algorithm>:\n");
85 fprintf(stderr, " RSA | RSAMD5 | DSA | RSASHA1 | NSEC3RSASHA1"
87 fprintf(stderr, " RSASHA256 | RSASHA512 | ECCGOST |\n");
88 fprintf(stderr, " ECDSAP256SHA256 | ECDSAP384SHA384 |\n");
89 fprintf(stderr, " DH | HMAC-MD5 | HMAC-SHA1 | HMAC-SHA224 | "
91 fprintf(stderr, " HMAC-SHA384 | HMAC-SHA512\n");
92 fprintf(stderr, " (default: RSASHA1, or "
93 "NSEC3RSASHA1 if using -3)\n");
94 fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
95 fprintf(stderr, " -b <key size in bits>:\n");
96 fprintf(stderr, " RSAMD5:\t[512..%d]\n", MAX_RSA);
97 fprintf(stderr, " RSASHA1:\t[512..%d]\n", MAX_RSA);
98 fprintf(stderr, " NSEC3RSASHA1:\t[512..%d]\n", MAX_RSA);
99 fprintf(stderr, " RSASHA256:\t[512..%d]\n", MAX_RSA);
100 fprintf(stderr, " RSASHA512:\t[1024..%d]\n", MAX_RSA);
101 fprintf(stderr, " DH:\t\t[128..4096]\n");
102 fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
103 fprintf(stderr, " NSEC3DSA:\t[512..1024] and divisible "
105 fprintf(stderr, " ECCGOST:\tignored\n");
106 fprintf(stderr, " ECDSAP256SHA256:\tignored\n");
107 fprintf(stderr, " ECDSAP384SHA384:\tignored\n");
108 fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
109 fprintf(stderr, " HMAC-SHA1:\t[1..160]\n");
110 fprintf(stderr, " HMAC-SHA224:\t[1..224]\n");
111 fprintf(stderr, " HMAC-SHA256:\t[1..256]\n");
112 fprintf(stderr, " HMAC-SHA384:\t[1..384]\n");
113 fprintf(stderr, " HMAC-SHA512:\t[1..512]\n");
114 fprintf(stderr, " (if using the default algorithm, key size\n"
115 " defaults to 2048 for KSK, or 1024 for all "
117 fprintf(stderr, " -n <nametype>: ZONE | HOST | ENTITY | "
119 fprintf(stderr, " (DNSKEY generation defaults to ZONE)\n");
120 fprintf(stderr, " -c <class>: (default: IN)\n");
121 fprintf(stderr, " -d <digest bits> (0 => max, default)\n");
123 fprintf(stderr, " -E <engine name> (default \"pkcs11\")\n");
125 fprintf(stderr, " -E <engine name>\n");
127 fprintf(stderr, " -f <keyflag>: KSK | REVOKE\n");
128 fprintf(stderr, " -g <generator>: use specified generator "
130 fprintf(stderr, " -L <ttl>: default key TTL\n");
131 fprintf(stderr, " -p <protocol>: (default: 3 [dnssec])\n");
132 fprintf(stderr, " -r <randomdev>: a file containing random data\n");
133 fprintf(stderr, " -s <strength>: strength value this key signs DNS "
134 "records with (default: 0)\n");
135 fprintf(stderr, " -T <rrtype>: DNSKEY | KEY (default: DNSKEY; "
136 "use KEY for SIG(0))\n");
137 fprintf(stderr, " ECCGOST:\tignored\n");
138 fprintf(stderr, " -t <type>: "
139 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
140 "(default: AUTHCONF)\n");
141 fprintf(stderr, " -h: print usage and exit\n");
142 fprintf(stderr, " -m <memory debugging mode>:\n");
143 fprintf(stderr, " usage | trace | record | size | mctx\n");
144 fprintf(stderr, " -v <level>: set verbosity level (0 - 10)\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, revoke = 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:v:"
261 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
264 if (strcasecmp(isc_commandline_argument, "record") == 0)
265 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
266 if (strcasecmp(isc_commandline_argument, "trace") == 0)
267 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
268 if (strcasecmp(isc_commandline_argument, "usage") == 0)
269 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
270 if (strcasecmp(isc_commandline_argument, "size") == 0)
271 isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
272 if (strcasecmp(isc_commandline_argument, "mctx") == 0)
273 isc_mem_debugging |= ISC_MEM_DEBUGCTX;
279 isc_commandline_reset = ISC_TRUE;
281 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
283 isc_stdtime_get(&now);
285 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
288 use_nsec3 = ISC_TRUE;
291 algname = isc_commandline_argument;
294 size = strtol(isc_commandline_argument, &endp, 10);
295 if (*endp != '\0' || size < 0)
296 fatal("-b requires a non-negative number");
302 classname = isc_commandline_argument;
305 dbits = strtol(isc_commandline_argument, &endp, 10);
306 if (*endp != '\0' || dbits < 0)
307 fatal("-d requires a non-negative number");
310 engine = isc_commandline_argument;
314 "phased-out option -e "
315 "(was 'use (RSA) large exponent)\n");
318 c = (unsigned char)(isc_commandline_argument[0]);
319 if (toupper(c) == 'K')
320 kskflag = DNS_KEYFLAG_KSK;
321 else if (toupper(c) == 'R')
322 revflag = DNS_KEYFLAG_REVOKE;
324 fatal("unknown flag '%s'",
325 isc_commandline_argument);
328 generator = strtol(isc_commandline_argument,
330 if (*endp != '\0' || generator <= 0)
331 fatal("-g requires a positive number");
334 directory = isc_commandline_argument;
335 ret = try_dir(directory);
336 if (ret != ISC_R_SUCCESS)
337 fatal("cannot open directory %s: %s",
338 directory, isc_result_totext(ret));
341 fatal("The -k option has been deprecated.\n"
342 "To generate a key-signing key, use -f KSK.\n"
343 "To generate a key with TYPE=KEY, use -T KEY.\n");
346 if (strcmp(isc_commandline_argument, "none") == 0)
349 ttl = strtottl(isc_commandline_argument);
353 nametype = isc_commandline_argument;
358 protocol = strtol(isc_commandline_argument, &endp, 10);
359 if (*endp != '\0' || protocol < 0 || protocol > 255)
360 fatal("-p must be followed by a number "
367 setup_entropy(mctx, isc_commandline_argument, &ectx);
370 signatory = strtol(isc_commandline_argument,
372 if (*endp != '\0' || signatory < 0 || signatory > 15)
373 fatal("-s must be followed by a number "
377 if (strcasecmp(isc_commandline_argument, "KEY") == 0)
378 options |= DST_TYPE_KEY;
379 else if (strcasecmp(isc_commandline_argument,
381 /* default behavior */
384 fatal("unknown type '%s'",
385 isc_commandline_argument);
388 type = isc_commandline_argument;
392 verbose = strtol(isc_commandline_argument, &endp, 0);
394 fatal("-v must be followed by a number");
397 /* already the default */
403 if (setpub || unsetpub)
404 fatal("-P specified more than once");
406 if (strcasecmp(isc_commandline_argument, "none")) {
408 publish = strtotime(isc_commandline_argument,
415 if (setact || unsetact)
416 fatal("-A specified more than once");
418 if (strcasecmp(isc_commandline_argument, "none")) {
420 activate = strtotime(isc_commandline_argument,
427 if (setrev || unsetrev)
428 fatal("-R specified more than once");
430 if (strcasecmp(isc_commandline_argument, "none")) {
432 revoke = strtotime(isc_commandline_argument,
439 if (setinact || unsetinact)
440 fatal("-I specified more than once");
442 if (strcasecmp(isc_commandline_argument, "none")) {
444 inactive = strtotime(isc_commandline_argument,
447 unsetinact = ISC_TRUE;
451 if (setdel || unsetdel)
452 fatal("-D specified more than once");
454 if (strcasecmp(isc_commandline_argument, "none")) {
456 delete = strtotime(isc_commandline_argument,
463 predecessor = isc_commandline_argument;
466 prepub = strtottl(isc_commandline_argument);
469 /* Reserved for FIPS mode */
472 if (isc_commandline_option != '?')
473 fprintf(stderr, "%s: invalid argument -%c\n",
474 program, isc_commandline_option);
480 fprintf(stderr, "%s: unhandled option -%c\n",
481 program, isc_commandline_option);
490 setup_entropy(mctx, NULL, &ectx);
491 ret = dst_lib_init2(mctx, ectx, engine,
492 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
493 if (ret != ISC_R_SUCCESS)
494 fatal("could not initialize dst: %s",
495 isc_result_totext(ret));
497 setup_logging(verbose, mctx, &log);
499 if (predecessor == NULL) {
503 if (argc < isc_commandline_index + 1)
504 fatal("the key name was not specified");
505 if (argc > isc_commandline_index + 1)
506 fatal("extraneous arguments");
508 dns_fixedname_init(&fname);
509 name = dns_fixedname_name(&fname);
510 isc_buffer_init(&buf, argv[isc_commandline_index],
511 strlen(argv[isc_commandline_index]));
512 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
513 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
514 if (ret != ISC_R_SUCCESS)
515 fatal("invalid key name %s: %s",
516 argv[isc_commandline_index],
517 isc_result_totext(ret));
519 if (algname == NULL) {
520 use_default = ISC_TRUE;
522 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
524 algname = strdup(DEFAULT_ALGORITHM);
526 fatal("strdup failed");
529 fprintf(stderr, "no algorithm specified; "
530 "defaulting to %s\n", algname);
533 if (strcasecmp(algname, "RSA") == 0) {
534 fprintf(stderr, "The use of RSA (RSAMD5) is not "
535 "recommended.\nIf you still wish to "
536 "use RSA (RSAMD5) please specify "
538 INSIST(freeit == NULL);
540 } else if (strcasecmp(algname, "HMAC-MD5") == 0)
541 alg = DST_ALG_HMACMD5;
542 else if (strcasecmp(algname, "HMAC-SHA1") == 0)
543 alg = DST_ALG_HMACSHA1;
544 else if (strcasecmp(algname, "HMAC-SHA224") == 0)
545 alg = DST_ALG_HMACSHA224;
546 else if (strcasecmp(algname, "HMAC-SHA256") == 0)
547 alg = DST_ALG_HMACSHA256;
548 else if (strcasecmp(algname, "HMAC-SHA384") == 0)
549 alg = DST_ALG_HMACSHA384;
550 else if (strcasecmp(algname, "HMAC-SHA512") == 0)
551 alg = DST_ALG_HMACSHA512;
554 r.length = strlen(algname);
555 ret = dns_secalg_fromtext(&alg, &r);
556 if (ret != ISC_R_SUCCESS)
557 fatal("unknown algorithm %s", algname);
558 if (alg == DST_ALG_DH)
559 options |= DST_TYPE_KEY;
563 alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
564 alg != DST_ALG_RSASHA256 && alg!= DST_ALG_RSASHA512 &&
565 alg != DST_ALG_ECCGOST &&
566 alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
567 fatal("%s is incompatible with NSEC3; "
568 "do not use the -3 option", algname);
571 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
572 if (strcasecmp(type, "NOAUTH") == 0)
573 flags |= DNS_KEYTYPE_NOAUTH;
574 else if (strcasecmp(type, "NOCONF") == 0)
575 flags |= DNS_KEYTYPE_NOCONF;
576 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
577 flags |= (DNS_KEYTYPE_NOAUTH |
582 else if (strcasecmp(type, "AUTHCONF") == 0)
585 fatal("invalid type %s", type);
590 if ((kskflag & DNS_KEYFLAG_KSK) != 0)
595 fprintf(stderr, "key size not "
596 "specified; defaulting"
598 } else if (alg != DST_ALG_ECCGOST &&
599 alg != DST_ALG_ECDSA256 &&
600 alg != DST_ALG_ECDSA384)
601 fatal("key size not specified (-b option)");
604 if (!oldstyle && prepub > 0) {
605 if (setpub && setact && (activate - prepub) < publish)
606 fatal("Activation and publication dates "
607 "are closer together than the\n\t"
608 "prepublication interval.");
610 if (!setpub && !setact) {
611 setpub = setact = ISC_TRUE;
613 activate = now + prepub;
614 } else if (setpub && !setact) {
616 activate = publish + prepub;
617 } else if (setact && !setpub) {
619 publish = activate - prepub;
622 if ((activate - prepub) < now)
623 fatal("Time until activation is shorter "
624 "than the\n\tprepublication interval.");
627 char keystr[DST_KEY_FORMATSIZE];
632 prepub = (30 * 86400);
635 fatal("-S and -a cannot be used together");
637 fatal("-S and -b cannot be used together");
638 if (nametype != NULL)
639 fatal("-S and -n cannot be used together");
641 fatal("-S and -t cannot be used together");
642 if (setpub || unsetpub)
643 fatal("-S and -P cannot be used together");
644 if (setact || unsetact)
645 fatal("-S and -A cannot be used together");
647 fatal("-S and -3 cannot be used together");
649 fatal("-S and -C cannot be used together");
651 fatal("-S and -G cannot be used together");
653 ret = dst_key_fromnamedfile(predecessor, directory,
654 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
656 if (ret != ISC_R_SUCCESS)
657 fatal("Invalid keyfile %s: %s",
658 predecessor, isc_result_totext(ret));
659 if (!dst_key_isprivate(prevkey))
660 fatal("%s is not a private key", predecessor);
662 name = dst_key_name(prevkey);
663 alg = dst_key_alg(prevkey);
664 size = dst_key_size(prevkey);
665 flags = dst_key_flags(prevkey);
667 dst_key_format(prevkey, keystr, sizeof(keystr));
668 dst_key_getprivateformat(prevkey, &major, &minor);
669 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
670 fatal("Key %s has incompatible format version %d.%d\n\t"
671 "It is not possible to generate a successor key.",
672 keystr, major, minor);
674 ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
675 if (ret != ISC_R_SUCCESS)
676 fatal("Key %s has no activation date.\n\t"
677 "You must use dnssec-settime -A to set one "
678 "before generating a successor.", keystr);
680 ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
681 if (ret != ISC_R_SUCCESS)
682 fatal("Key %s has no inactivation date.\n\t"
683 "You must use dnssec-settime -I to set one "
684 "before generating a successor.", keystr);
686 publish = activate - prepub;
688 fatal("Key %s becomes inactive\n\t"
689 "sooner than the prepublication period "
690 "for the new key ends.\n\t"
691 "Either change the inactivation date with "
692 "dnssec-settime -I,\n\t"
693 "or use the -i option to set a shorter "
694 "prepublication interval.", keystr);
696 ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
697 if (ret != ISC_R_SUCCESS)
698 fprintf(stderr, "%s: WARNING: Key %s has no removal "
699 "date;\n\t it will remain in the zone "
700 "indefinitely after rollover.\n\t "
701 "You can use dnssec-settime -D to "
702 "change this.\n", program, keystr);
704 setpub = setact = ISC_TRUE;
708 case DNS_KEYALG_RSAMD5:
709 case DNS_KEYALG_RSASHA1:
710 case DNS_KEYALG_NSEC3RSASHA1:
711 case DNS_KEYALG_RSASHA256:
712 if (size != 0 && (size < 512 || size > MAX_RSA))
713 fatal("RSA key size %d out of range", size);
715 case DNS_KEYALG_RSASHA512:
716 if (size != 0 && (size < 1024 || size > MAX_RSA))
717 fatal("RSA key size %d out of range", size);
720 if (size != 0 && (size < 128 || size > 4096))
721 fatal("DH key size %d out of range", size);
724 case DNS_KEYALG_NSEC3DSA:
725 if (size != 0 && !dsa_size_ok(size))
726 fatal("invalid DSS key size: %d", size);
728 case DST_ALG_ECCGOST:
729 case DST_ALG_ECDSA256:
730 case DST_ALG_ECDSA384:
732 case DST_ALG_HMACMD5:
733 options |= DST_TYPE_KEY;
734 if (size < 1 || size > 512)
735 fatal("HMAC-MD5 key size %d out of range", size);
736 if (dbits != 0 && (dbits < 80 || dbits > 128))
737 fatal("HMAC-MD5 digest bits %d out of range", dbits);
738 if ((dbits % 8) != 0)
739 fatal("HMAC-MD5 digest bits %d not divisible by 8",
742 case DST_ALG_HMACSHA1:
743 options |= DST_TYPE_KEY;
744 if (size < 1 || size > 160)
745 fatal("HMAC-SHA1 key size %d out of range", size);
746 if (dbits != 0 && (dbits < 80 || dbits > 160))
747 fatal("HMAC-SHA1 digest bits %d out of range", dbits);
748 if ((dbits % 8) != 0)
749 fatal("HMAC-SHA1 digest bits %d not divisible by 8",
752 case DST_ALG_HMACSHA224:
753 options |= DST_TYPE_KEY;
754 if (size < 1 || size > 224)
755 fatal("HMAC-SHA224 key size %d out of range", size);
756 if (dbits != 0 && (dbits < 112 || dbits > 224))
757 fatal("HMAC-SHA224 digest bits %d out of range", dbits);
758 if ((dbits % 8) != 0)
759 fatal("HMAC-SHA224 digest bits %d not divisible by 8",
762 case DST_ALG_HMACSHA256:
763 options |= DST_TYPE_KEY;
764 if (size < 1 || size > 256)
765 fatal("HMAC-SHA256 key size %d out of range", size);
766 if (dbits != 0 && (dbits < 128 || dbits > 256))
767 fatal("HMAC-SHA256 digest bits %d out of range", dbits);
768 if ((dbits % 8) != 0)
769 fatal("HMAC-SHA256 digest bits %d not divisible by 8",
772 case DST_ALG_HMACSHA384:
773 options |= DST_TYPE_KEY;
774 if (size < 1 || size > 384)
775 fatal("HMAC-384 key size %d out of range", size);
776 if (dbits != 0 && (dbits < 192 || dbits > 384))
777 fatal("HMAC-SHA384 digest bits %d out of range", dbits);
778 if ((dbits % 8) != 0)
779 fatal("HMAC-SHA384 digest bits %d not divisible by 8",
782 case DST_ALG_HMACSHA512:
783 options |= DST_TYPE_KEY;
784 if (size < 1 || size > 512)
785 fatal("HMAC-SHA512 key size %d out of range", size);
786 if (dbits != 0 && (dbits < 256 || dbits > 512))
787 fatal("HMAC-SHA512 digest bits %d out of range", dbits);
788 if ((dbits % 8) != 0)
789 fatal("HMAC-SHA512 digest bits %d not divisible by 8",
794 if (alg != DNS_KEYALG_DH && generator != 0)
795 fatal("specified DH generator for a non-DH key");
797 if (nametype == NULL) {
798 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
799 fatal("no nametype specified");
800 flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
801 } else if (strcasecmp(nametype, "zone") == 0)
802 flags |= DNS_KEYOWNER_ZONE;
803 else if ((options & DST_TYPE_KEY) != 0) { /* KEY / HMAC */
804 if (strcasecmp(nametype, "host") == 0 ||
805 strcasecmp(nametype, "entity") == 0)
806 flags |= DNS_KEYOWNER_ENTITY;
807 else if (strcasecmp(nametype, "user") == 0)
808 flags |= DNS_KEYOWNER_USER;
810 fatal("invalid KEY nametype %s", nametype);
811 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
812 fatal("invalid DNSKEY nametype %s", nametype);
814 rdclass = strtoclass(classname);
816 if (directory == NULL)
819 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
821 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
827 protocol = DNS_KEYPROTO_DNSSEC;
828 else if ((options & DST_TYPE_KEY) == 0 &&
829 protocol != DNS_KEYPROTO_DNSSEC)
830 fatal("invalid DNSKEY protocol: %d", protocol);
832 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
834 fatal("specified null key with non-zero size");
835 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
836 fatal("specified null key with signing authority");
839 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
840 (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5 ||
841 alg == DST_ALG_HMACSHA1 || alg == DST_ALG_HMACSHA224 ||
842 alg == DST_ALG_HMACSHA256 || alg == DST_ALG_HMACSHA384 ||
843 alg == DST_ALG_HMACSHA512))
844 fatal("a key with algorithm '%s' cannot be a zone key",
848 case DNS_KEYALG_RSAMD5:
849 case DNS_KEYALG_RSASHA1:
850 case DNS_KEYALG_NSEC3RSASHA1:
851 case DNS_KEYALG_RSASHA256:
852 case DNS_KEYALG_RSASHA512:
853 show_progress = ISC_TRUE;
861 case DNS_KEYALG_NSEC3DSA:
862 case DST_ALG_ECCGOST:
863 case DST_ALG_ECDSA256:
864 case DST_ALG_ECDSA384:
865 show_progress = ISC_TRUE;
868 case DST_ALG_HMACMD5:
869 case DST_ALG_HMACSHA1:
870 case DST_ALG_HMACSHA224:
871 case DST_ALG_HMACSHA256:
872 case DST_ALG_HMACSHA384:
873 case DST_ALG_HMACSHA512:
878 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
881 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
884 conflict = ISC_FALSE;
886 if (!quiet && show_progress) {
887 fprintf(stderr, "Generating key pair.");
888 ret = dst_key_generate2(name, alg, size, param, flags,
889 protocol, rdclass, mctx, &key,
894 ret = dst_key_generate2(name, alg, size, param, flags,
895 protocol, rdclass, mctx, &key,
899 isc_entropy_stopcallbacksources(ectx);
901 if (ret != ISC_R_SUCCESS) {
902 char namestr[DNS_NAME_FORMATSIZE];
903 char algstr[DNS_SECALG_FORMATSIZE];
904 dns_name_format(name, namestr, sizeof(namestr));
905 dns_secalg_format(alg, algstr, sizeof(algstr));
906 fatal("failed to generate key %s/%s: %s\n",
907 namestr, algstr, isc_result_totext(ret));
912 dst_key_setbits(key, dbits);
915 * Set key timing metadata (unless using -C)
917 * Creation date is always set to "now".
919 * For a new key without an explicit predecessor, publish
920 * and activation dates are set to "now" by default, but
921 * can both be overridden.
923 * For a successor key, activation is set to match the
924 * predecessor's inactivation date. Publish is set to 30
925 * days earlier than that (XXX: this should be configurable).
926 * If either of the resulting dates are in the past, that's
927 * an error; the inactivation date of the predecessor key
928 * must be updated before a successor key can be created.
931 dst_key_settime(key, DST_TIME_CREATED, now);
933 if (genonly && (setpub || setact))
934 fatal("cannot use -G together with "
938 dst_key_settime(key, DST_TIME_PUBLISH, publish);
940 dst_key_settime(key, DST_TIME_PUBLISH,
942 else if (!genonly && !unsetpub)
943 dst_key_settime(key, DST_TIME_PUBLISH, now);
946 dst_key_settime(key, DST_TIME_ACTIVATE,
948 else if (!genonly && !unsetact)
949 dst_key_settime(key, DST_TIME_ACTIVATE, now);
953 fprintf(stderr, "%s: warning: Key is "
954 "not flagged as a KSK, but -R "
955 "was used. Revoking a ZSK is "
956 "legal, but undefined.\n",
958 dst_key_settime(key, DST_TIME_REVOKE, revoke);
962 dst_key_settime(key, DST_TIME_INACTIVE,
966 if (setinact && delete < inactive)
967 fprintf(stderr, "%s: warning: Key is "
968 "scheduled to be deleted "
969 "before it is scheduled to be "
972 dst_key_settime(key, DST_TIME_DELETE, delete);
975 if (setpub || setact || setrev || setinact ||
976 setdel || unsetpub || unsetact ||
977 unsetrev || unsetinact || unsetdel || genonly)
978 fatal("cannot use -C together with "
979 "-P, -A, -R, -I, -D, or -G options");
981 * Compatibility mode: Private-key-format
982 * should be set to 1.2.
984 dst_key_setprivateformat(key, 1, 2);
987 /* Set the default key TTL */
989 dst_key_setttl(key, ttl);
992 * Do not overwrite an existing key, or create a key
993 * if there is a risk of ID collision due to this key
994 * or another key being revoked.
996 if (key_collision(key, name, directory, mctx, NULL)) {
1004 isc_buffer_clear(&buf);
1005 ret = dst_key_buildfilename(key, 0,
1007 if (ret == ISC_R_SUCCESS)
1009 "%s: %s already exists, or "
1010 "might collide with another "
1011 "key upon revokation. "
1012 "Generating a new key\n",
1018 } while (conflict == ISC_TRUE);
1021 fatal("cannot generate a null key due to possible key ID "
1024 ret = dst_key_tofile(key, options, directory);
1025 if (ret != ISC_R_SUCCESS) {
1026 char keystr[DST_KEY_FORMATSIZE];
1027 dst_key_format(key, keystr, sizeof(keystr));
1028 fatal("failed to write key %s: %s\n", keystr,
1029 isc_result_totext(ret));
1032 isc_buffer_clear(&buf);
1033 ret = dst_key_buildfilename(key, 0, NULL, &buf);
1034 if (ret != ISC_R_SUCCESS)
1035 fatal("dst_key_buildfilename returned: %s\n",
1036 isc_result_totext(ret));
1037 printf("%s\n", filename);
1039 if (prevkey != NULL)
1040 dst_key_free(&prevkey);
1042 cleanup_logging(&log);
1043 cleanup_entropy(&ectx);
1047 isc_mem_stats(mctx, stdout);
1048 isc_mem_destroy(&mctx);