2 * Portions Copyright (C) 2004-2008, 2010 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.81.48.2 2010/01/15 23:47:31 tbox Exp $ */
40 #include <isc/buffer.h>
41 #include <isc/commandline.h>
42 #include <isc/entropy.h>
44 #include <isc/region.h>
45 #include <isc/string.h>
48 #include <dns/fixedname.h>
49 #include <dns/keyvalues.h>
52 #include <dns/rdataclass.h>
53 #include <dns/result.h>
54 #include <dns/secalg.h>
58 #include "dnssectool.h"
60 #define MAX_RSA 4096 /* should be long enough... */
62 const char *program = "dnssec-keygen";
65 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 | RSASHA256 |"
66 " RSASHA512 | NSEC3DSA | NSEC3RSASHA1 | HMAC-MD5 |"
67 " HMAC-SHA1 | HMAC-SHA224 | HMAC-SHA256 |"
68 " HMAC-SHA384 | HMAC-SHA512";
71 dsa_size_ok(int size) {
72 return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
77 fprintf(stderr, "Usage:\n");
78 fprintf(stderr, " %s -a alg -b bits [-n type] [options] name\n\n",
80 fprintf(stderr, "Version: %s\n", VERSION);
81 fprintf(stderr, "Required options:\n");
82 fprintf(stderr, " -a algorithm: %s\n", algs);
83 fprintf(stderr, " -b key size, in bits:\n");
84 fprintf(stderr, " RSAMD5:\t\t[512..%d]\n", MAX_RSA);
85 fprintf(stderr, " RSASHA1:\t\t[512..%d]\n", MAX_RSA);
86 fprintf(stderr, " NSEC3RSASHA1:\t\t[512..%d]\n", MAX_RSA);
87 fprintf(stderr, " RSASHA256:\t[512..%d]\n", MAX_RSA);
88 fprintf(stderr, " RSASHA512:\t[1024..%d]\n", MAX_RSA);
89 fprintf(stderr, " DH:\t\t[128..4096]\n");
90 fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
91 fprintf(stderr, " NSEC3DSA:\t\t[512..1024] and divisible by 64\n");
92 fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
93 fprintf(stderr, " HMAC-SHA1:\t[1..160]\n");
94 fprintf(stderr, " HMAC-SHA224:\t[1..224]\n");
95 fprintf(stderr, " HMAC-SHA256:\t[1..256]\n");
96 fprintf(stderr, " HMAC-SHA384:\t[1..384]\n");
97 fprintf(stderr, " HMAC-SHA512:\t[1..512]\n");
98 fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
99 fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
100 fprintf(stderr, " name: owner of the key\n");
101 fprintf(stderr, "Other options:\n");
102 fprintf(stderr, " -c <class> (default: IN)\n");
103 fprintf(stderr, " -d <digest bits> (0 => max, default)\n");
104 fprintf(stderr, " -e use large exponent (RSAMD5/RSASHA1 only)\n");
105 fprintf(stderr, " -f keyflag: KSK\n");
106 fprintf(stderr, " -g <generator> use specified generator "
108 fprintf(stderr, " -t <type>: "
109 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
110 "(default: AUTHCONF)\n");
111 fprintf(stderr, " -p <protocol>: "
112 "default: 3 [dnssec]\n");
113 fprintf(stderr, " -s <strength> strength value this key signs DNS "
114 "records with (default: 0)\n");
115 fprintf(stderr, " -r <randomdev>: a file containing random data\n");
116 fprintf(stderr, " -v <verbose level>\n");
117 fprintf(stderr, " -k : generate a TYPE=KEY key\n");
118 fprintf(stderr, "Output:\n");
119 fprintf(stderr, " K<name>+<alg>+<id>.key, "
120 "K<name>+<alg>+<id>.private\n");
126 main(int argc, char **argv) {
127 char *algname = NULL, *nametype = NULL, *type = NULL;
128 char *classname = NULL;
130 dst_key_t *key = NULL, *oldkey;
131 dns_fixedname_t fname;
133 isc_uint16_t flags = 0, ksk = 0;
135 isc_boolean_t conflict = ISC_FALSE, null_key = ISC_FALSE;
136 isc_mem_t *mctx = NULL;
137 int ch, rsa_exp = 0, generator = 0, param = 0;
138 int protocol = -1, size = -1, signatory = 0;
143 isc_log_t *log = NULL;
144 isc_entropy_t *ectx = NULL;
145 dns_rdataclass_t rdclass;
146 int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
152 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
154 dns_result_register();
156 isc_commandline_errprint = ISC_FALSE;
158 while ((ch = isc_commandline_parse(argc, argv,
159 "a:b:c:d:ef:g:kn:t:p:s:r:v:h")) != -1)
163 algname = isc_commandline_argument;
166 size = strtol(isc_commandline_argument, &endp, 10);
167 if (*endp != '\0' || size < 0)
168 fatal("-b requires a non-negative number");
171 classname = isc_commandline_argument;
174 dbits = strtol(isc_commandline_argument, &endp, 10);
175 if (*endp != '\0' || dbits < 0)
176 fatal("-d requires a non-negative number");
182 if (strcasecmp(isc_commandline_argument, "KSK") == 0)
183 ksk = DNS_KEYFLAG_KSK;
185 fatal("unknown flag '%s'",
186 isc_commandline_argument);
189 generator = strtol(isc_commandline_argument,
191 if (*endp != '\0' || generator <= 0)
192 fatal("-g requires a positive number");
195 options |= DST_TYPE_KEY;
198 nametype = isc_commandline_argument;
201 type = isc_commandline_argument;
204 protocol = strtol(isc_commandline_argument, &endp, 10);
205 if (*endp != '\0' || protocol < 0 || protocol > 255)
206 fatal("-p must be followed by a number "
210 signatory = strtol(isc_commandline_argument,
212 if (*endp != '\0' || signatory < 0 || signatory > 15)
213 fatal("-s must be followed by a number "
217 setup_entropy(mctx, isc_commandline_argument, &ectx);
221 verbose = strtol(isc_commandline_argument, &endp, 0);
223 fatal("-v must be followed by a number");
227 if (isc_commandline_option != '?')
228 fprintf(stderr, "%s: invalid argument -%c\n",
229 program, isc_commandline_option);
234 fprintf(stderr, "%s: unhandled option -%c\n",
235 program, isc_commandline_option);
241 setup_entropy(mctx, NULL, &ectx);
242 ret = dst_lib_init(mctx, ectx,
243 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
244 if (ret != ISC_R_SUCCESS)
245 fatal("could not initialize dst");
247 setup_logging(verbose, mctx, &log);
249 if (argc < isc_commandline_index + 1)
250 fatal("the key name was not specified");
251 if (argc > isc_commandline_index + 1)
252 fatal("extraneous arguments");
255 fatal("no algorithm was specified");
256 if (strcasecmp(algname, "RSA") == 0) {
257 fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
258 "If you still wish to use RSA (RSAMD5) please "
259 "specify \"-a RSAMD5\"\n");
261 } else if (strcasecmp(algname, "HMAC-MD5") == 0) {
262 options |= DST_TYPE_KEY;
263 alg = DST_ALG_HMACMD5;
264 } else if (strcasecmp(algname, "HMAC-SHA1") == 0) {
265 options |= DST_TYPE_KEY;
266 alg = DST_ALG_HMACSHA1;
267 } else if (strcasecmp(algname, "HMAC-SHA224") == 0) {
268 options |= DST_TYPE_KEY;
269 alg = DST_ALG_HMACSHA224;
270 } else if (strcasecmp(algname, "HMAC-SHA256") == 0) {
271 options |= DST_TYPE_KEY;
272 alg = DST_ALG_HMACSHA256;
273 } else if (strcasecmp(algname, "HMAC-SHA384") == 0) {
274 options |= DST_TYPE_KEY;
275 alg = DST_ALG_HMACSHA384;
276 } else if (strcasecmp(algname, "HMAC-SHA512") == 0) {
277 options |= DST_TYPE_KEY;
278 alg = DST_ALG_HMACSHA512;
281 r.length = strlen(algname);
282 ret = dns_secalg_fromtext(&alg, &r);
283 if (ret != ISC_R_SUCCESS)
284 fatal("unknown algorithm %s", algname);
285 if (alg == DST_ALG_DH)
286 options |= DST_TYPE_KEY;
289 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
290 if (strcasecmp(type, "NOAUTH") == 0)
291 flags |= DNS_KEYTYPE_NOAUTH;
292 else if (strcasecmp(type, "NOCONF") == 0)
293 flags |= DNS_KEYTYPE_NOCONF;
294 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
295 flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
299 else if (strcasecmp(type, "AUTHCONF") == 0)
302 fatal("invalid type %s", type);
306 fatal("key size not specified (-b option)");
309 case DNS_KEYALG_RSAMD5:
310 case DNS_KEYALG_RSASHA1:
311 case DNS_KEYALG_NSEC3RSASHA1:
312 case DNS_KEYALG_RSASHA256:
313 if (size != 0 && (size < 512 || size > MAX_RSA))
314 fatal("RSA key size %d out of range", size);
316 case DNS_KEYALG_RSASHA512:
317 if (size != 0 && (size < 1024 || size > MAX_RSA))
318 fatal("RSA key size %d out of range", size);
321 if (size != 0 && (size < 128 || size > 4096))
322 fatal("DH key size %d out of range", size);
325 case DNS_KEYALG_NSEC3DSA:
326 if (size != 0 && !dsa_size_ok(size))
327 fatal("invalid DSS key size: %d", size);
329 case DST_ALG_HMACMD5:
330 if (size < 1 || size > 512)
331 fatal("HMAC-MD5 key size %d out of range", size);
332 if (dbits != 0 && (dbits < 80 || dbits > 128))
333 fatal("HMAC-MD5 digest bits %d out of range", dbits);
334 if ((dbits % 8) != 0)
335 fatal("HMAC-MD5 digest bits %d not divisible by 8",
338 case DST_ALG_HMACSHA1:
339 if (size < 1 || size > 160)
340 fatal("HMAC-SHA1 key size %d out of range", size);
341 if (dbits != 0 && (dbits < 80 || dbits > 160))
342 fatal("HMAC-SHA1 digest bits %d out of range", dbits);
343 if ((dbits % 8) != 0)
344 fatal("HMAC-SHA1 digest bits %d not divisible by 8",
347 case DST_ALG_HMACSHA224:
348 if (size < 1 || size > 224)
349 fatal("HMAC-SHA224 key size %d out of range", size);
350 if (dbits != 0 && (dbits < 112 || dbits > 224))
351 fatal("HMAC-SHA224 digest bits %d out of range", dbits);
352 if ((dbits % 8) != 0)
353 fatal("HMAC-SHA224 digest bits %d not divisible by 8",
356 case DST_ALG_HMACSHA256:
357 if (size < 1 || size > 256)
358 fatal("HMAC-SHA256 key size %d out of range", size);
359 if (dbits != 0 && (dbits < 128 || dbits > 256))
360 fatal("HMAC-SHA256 digest bits %d out of range", dbits);
361 if ((dbits % 8) != 0)
362 fatal("HMAC-SHA256 digest bits %d not divisible by 8",
365 case DST_ALG_HMACSHA384:
366 if (size < 1 || size > 384)
367 fatal("HMAC-384 key size %d out of range", size);
368 if (dbits != 0 && (dbits < 192 || dbits > 384))
369 fatal("HMAC-SHA384 digest bits %d out of range", dbits);
370 if ((dbits % 8) != 0)
371 fatal("HMAC-SHA384 digest bits %d not divisible by 8",
374 case DST_ALG_HMACSHA512:
375 if (size < 1 || size > 512)
376 fatal("HMAC-SHA512 key size %d out of range", size);
377 if (dbits != 0 && (dbits < 256 || dbits > 512))
378 fatal("HMAC-SHA512 digest bits %d out of range", dbits);
379 if ((dbits % 8) != 0)
380 fatal("HMAC-SHA512 digest bits %d not divisible by 8",
385 if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1 ||
386 alg == DNS_KEYALG_NSEC3RSASHA1 || alg == DNS_KEYALG_RSASHA256 ||
387 alg == DNS_KEYALG_RSASHA512) && rsa_exp != 0)
388 fatal("specified RSA exponent for a non-RSA key");
390 if (alg != DNS_KEYALG_DH && generator != 0)
391 fatal("specified DH generator for a non-DH key");
393 if (nametype == NULL) {
394 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
395 fatal("no nametype specified");
396 flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
397 } else if (strcasecmp(nametype, "zone") == 0)
398 flags |= DNS_KEYOWNER_ZONE;
399 else if ((options & DST_TYPE_KEY) != 0) { /* KEY / HMAC */
400 if (strcasecmp(nametype, "host") == 0 ||
401 strcasecmp(nametype, "entity") == 0)
402 flags |= DNS_KEYOWNER_ENTITY;
403 else if (strcasecmp(nametype, "user") == 0)
404 flags |= DNS_KEYOWNER_USER;
406 fatal("invalid KEY nametype %s", nametype);
407 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
408 fatal("invalid DNSKEY nametype %s", nametype);
410 rdclass = strtoclass(classname);
412 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
414 else if ((flags & DNS_KEYOWNER_ZONE) != 0) /* DNSKEY */
418 protocol = DNS_KEYPROTO_DNSSEC;
419 else if ((options & DST_TYPE_KEY) == 0 &&
420 protocol != DNS_KEYPROTO_DNSSEC)
421 fatal("invalid DNSKEY protocol: %d", protocol);
423 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
425 fatal("specified null key with non-zero size");
426 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
427 fatal("specified null key with signing authority");
430 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
431 (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5 ||
432 alg == DST_ALG_HMACSHA1 || alg == DST_ALG_HMACSHA224 ||
433 alg == DST_ALG_HMACSHA256 || alg == DST_ALG_HMACSHA384 ||
434 alg == DST_ALG_HMACSHA512))
435 fatal("a key with algorithm '%s' cannot be a zone key",
438 dns_fixedname_init(&fname);
439 name = dns_fixedname_name(&fname);
440 isc_buffer_init(&buf, argv[isc_commandline_index],
441 strlen(argv[isc_commandline_index]));
442 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
443 ret = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
444 if (ret != ISC_R_SUCCESS)
445 fatal("invalid key name %s: %s", argv[isc_commandline_index],
446 isc_result_totext(ret));
449 case DNS_KEYALG_RSAMD5:
450 case DNS_KEYALG_RSASHA1:
451 case DNS_KEYALG_NSEC3RSASHA1:
452 case DNS_KEYALG_RSASHA256:
453 case DNS_KEYALG_RSASHA512:
460 case DNS_KEYALG_NSEC3DSA:
461 case DST_ALG_HMACMD5:
462 case DST_ALG_HMACSHA1:
463 case DST_ALG_HMACSHA224:
464 case DST_ALG_HMACSHA256:
465 case DST_ALG_HMACSHA384:
466 case DST_ALG_HMACSHA512:
471 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
474 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
477 conflict = ISC_FALSE;
480 /* generate the key */
481 ret = dst_key_generate(name, alg, size, param, flags, protocol,
482 rdclass, mctx, &key);
483 isc_entropy_stopcallbacksources(ectx);
485 if (ret != ISC_R_SUCCESS) {
486 char namestr[DNS_NAME_FORMATSIZE];
487 char algstr[ALG_FORMATSIZE];
488 dns_name_format(name, namestr, sizeof(namestr));
489 alg_format(alg, algstr, sizeof(algstr));
490 fatal("failed to generate key %s/%s: %s\n",
491 namestr, algstr, isc_result_totext(ret));
495 dst_key_setbits(key, dbits);
498 * Try to read a key with the same name, alg and id from disk.
499 * If there is one we must continue generating a new one
500 * unless we were asked to generate a null key, in which
501 * case we return failure.
503 ret = dst_key_fromfile(name, dst_key_id(key), alg,
504 DST_TYPE_PRIVATE, NULL, mctx, &oldkey);
505 /* do not overwrite an existing key */
506 if (ret == ISC_R_SUCCESS) {
507 dst_key_free(&oldkey);
512 if (conflict == ISC_TRUE) {
514 isc_buffer_clear(&buf);
515 ret = dst_key_buildfilename(key, 0, NULL, &buf);
517 "%s: %s already exists, "
518 "generating a new key\n",
524 } while (conflict == ISC_TRUE);
527 fatal("cannot generate a null key when a key with id 0 "
530 ret = dst_key_tofile(key, options, NULL);
531 if (ret != ISC_R_SUCCESS) {
532 char keystr[KEY_FORMATSIZE];
533 key_format(key, keystr, sizeof(keystr));
534 fatal("failed to write key %s: %s\n", keystr,
535 isc_result_totext(ret));
538 isc_buffer_clear(&buf);
539 ret = dst_key_buildfilename(key, 0, NULL, &buf);
540 printf("%s\n", filename);
543 cleanup_logging(&log);
544 cleanup_entropy(&ectx);
548 isc_mem_stats(mctx, stdout);
549 isc_mem_destroy(&mctx);