2 * Portions Copyright (C) 2004-2007 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.79 2007/08/28 07:20:42 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 | HMAC-MD5 |"
66 " HMAC-SHA1 | HMAC-SHA224 | HMAC-SHA256 | "
67 " HMAC-SHA384 | HMAC-SHA512";
70 dsa_size_ok(int size
) {
71 return (ISC_TF(size
>= 512 && size
<= 1024 && size
% 64 == 0));
76 fprintf(stderr
, "Usage:\n");
77 fprintf(stderr
, " %s -a alg -b bits [-n type] [options] name\n\n",
79 fprintf(stderr
, "Version: %s\n", VERSION
);
80 fprintf(stderr
, "Required options:\n");
81 fprintf(stderr
, " -a algorithm: %s\n", algs
);
82 fprintf(stderr
, " -b key size, in bits:\n");
83 fprintf(stderr
, " RSAMD5:\t\t[512..%d]\n", MAX_RSA
);
84 fprintf(stderr
, " RSASHA1:\t\t[512..%d]\n", MAX_RSA
);
85 fprintf(stderr
, " DH:\t\t[128..4096]\n");
86 fprintf(stderr
, " DSA:\t\t[512..1024] and divisible by 64\n");
87 fprintf(stderr
, " HMAC-MD5:\t[1..512]\n");
88 fprintf(stderr
, " HMAC-SHA1:\t[1..160]\n");
89 fprintf(stderr
, " HMAC-SHA224:\t[1..224]\n");
90 fprintf(stderr
, " HMAC-SHA256:\t[1..256]\n");
91 fprintf(stderr
, " HMAC-SHA384:\t[1..384]\n");
92 fprintf(stderr
, " HMAC-SHA512:\t[1..512]\n");
93 fprintf(stderr
, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
94 fprintf(stderr
, " (DNSKEY generation defaults to ZONE\n");
95 fprintf(stderr
, " name: owner of the key\n");
96 fprintf(stderr
, "Other options:\n");
97 fprintf(stderr
, " -c <class> (default: IN)\n");
98 fprintf(stderr
, " -d <digest bits> (0 => max, default)\n");
99 fprintf(stderr
, " -e use large exponent (RSAMD5/RSASHA1 only)\n");
100 fprintf(stderr
, " -f keyflag: KSK\n");
101 fprintf(stderr
, " -g <generator> use specified generator "
103 fprintf(stderr
, " -t <type>: "
104 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
105 "(default: AUTHCONF)\n");
106 fprintf(stderr
, " -p <protocol>: "
107 "default: 3 [dnssec]\n");
108 fprintf(stderr
, " -s <strength> strength value this key signs DNS "
109 "records with (default: 0)\n");
110 fprintf(stderr
, " -r <randomdev>: a file containing random data\n");
111 fprintf(stderr
, " -v <verbose level>\n");
112 fprintf(stderr
, " -k : generate a TYPE=KEY key\n");
113 fprintf(stderr
, "Output:\n");
114 fprintf(stderr
, " K<name>+<alg>+<id>.key, "
115 "K<name>+<alg>+<id>.private\n");
121 main(int argc
, char **argv
) {
122 char *algname
= NULL
, *nametype
= NULL
, *type
= NULL
;
123 char *classname
= NULL
;
125 dst_key_t
*key
= NULL
, *oldkey
;
126 dns_fixedname_t fname
;
128 isc_uint16_t flags
= 0, ksk
= 0;
130 isc_boolean_t conflict
= ISC_FALSE
, null_key
= ISC_FALSE
;
131 isc_mem_t
*mctx
= NULL
;
132 int ch
, rsa_exp
= 0, generator
= 0, param
= 0;
133 int protocol
= -1, size
= -1, signatory
= 0;
138 isc_log_t
*log
= NULL
;
139 isc_entropy_t
*ectx
= NULL
;
140 dns_rdataclass_t rdclass
;
141 int options
= DST_TYPE_PRIVATE
| DST_TYPE_PUBLIC
;
147 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
149 dns_result_register();
151 isc_commandline_errprint
= ISC_FALSE
;
153 while ((ch
= isc_commandline_parse(argc
, argv
,
154 "a:b:c:d:ef:g:kn:t:p:s:r:v:h")) != -1)
158 algname
= isc_commandline_argument
;
161 size
= strtol(isc_commandline_argument
, &endp
, 10);
162 if (*endp
!= '\0' || size
< 0)
163 fatal("-b requires a non-negative number");
166 classname
= isc_commandline_argument
;
169 dbits
= strtol(isc_commandline_argument
, &endp
, 10);
170 if (*endp
!= '\0' || dbits
< 0)
171 fatal("-d requires a non-negative number");
177 if (strcasecmp(isc_commandline_argument
, "KSK") == 0)
178 ksk
= DNS_KEYFLAG_KSK
;
180 fatal("unknown flag '%s'",
181 isc_commandline_argument
);
184 generator
= strtol(isc_commandline_argument
,
186 if (*endp
!= '\0' || generator
<= 0)
187 fatal("-g requires a positive number");
190 options
|= DST_TYPE_KEY
;
193 nametype
= isc_commandline_argument
;
196 type
= isc_commandline_argument
;
199 protocol
= strtol(isc_commandline_argument
, &endp
, 10);
200 if (*endp
!= '\0' || protocol
< 0 || protocol
> 255)
201 fatal("-p must be followed by a number "
205 signatory
= strtol(isc_commandline_argument
,
207 if (*endp
!= '\0' || signatory
< 0 || signatory
> 15)
208 fatal("-s must be followed by a number "
212 setup_entropy(mctx
, isc_commandline_argument
, &ectx
);
216 verbose
= strtol(isc_commandline_argument
, &endp
, 0);
218 fatal("-v must be followed by a number");
222 if (isc_commandline_option
!= '?')
223 fprintf(stderr
, "%s: invalid argument -%c\n",
224 program
, isc_commandline_option
);
229 fprintf(stderr
, "%s: unhandled option -%c\n",
230 program
, isc_commandline_option
);
236 setup_entropy(mctx
, NULL
, &ectx
);
237 ret
= dst_lib_init(mctx
, ectx
,
238 ISC_ENTROPY_BLOCKING
| ISC_ENTROPY_GOODONLY
);
239 if (ret
!= ISC_R_SUCCESS
)
240 fatal("could not initialize dst");
242 setup_logging(verbose
, mctx
, &log
);
244 if (argc
< isc_commandline_index
+ 1)
245 fatal("the key name was not specified");
246 if (argc
> isc_commandline_index
+ 1)
247 fatal("extraneous arguments");
250 fatal("no algorithm was specified");
251 if (strcasecmp(algname
, "RSA") == 0) {
252 fprintf(stderr
, "The use of RSA (RSAMD5) is not recommended.\n"
253 "If you still wish to use RSA (RSAMD5) please "
254 "specify \"-a RSAMD5\"\n");
256 } else if (strcasecmp(algname
, "HMAC-MD5") == 0) {
257 options
|= DST_TYPE_KEY
;
258 alg
= DST_ALG_HMACMD5
;
259 } else if (strcasecmp(algname
, "HMAC-SHA1") == 0) {
260 options
|= DST_TYPE_KEY
;
261 alg
= DST_ALG_HMACSHA1
;
262 } else if (strcasecmp(algname
, "HMAC-SHA224") == 0) {
263 options
|= DST_TYPE_KEY
;
264 alg
= DST_ALG_HMACSHA224
;
265 } else if (strcasecmp(algname
, "HMAC-SHA256") == 0) {
266 options
|= DST_TYPE_KEY
;
267 alg
= DST_ALG_HMACSHA256
;
268 } else if (strcasecmp(algname
, "HMAC-SHA384") == 0) {
269 options
|= DST_TYPE_KEY
;
270 alg
= DST_ALG_HMACSHA384
;
271 } else if (strcasecmp(algname
, "HMAC-SHA512") == 0) {
272 options
|= DST_TYPE_KEY
;
273 alg
= DST_ALG_HMACSHA512
;
276 r
.length
= strlen(algname
);
277 ret
= dns_secalg_fromtext(&alg
, &r
);
278 if (ret
!= ISC_R_SUCCESS
)
279 fatal("unknown algorithm %s", algname
);
280 if (alg
== DST_ALG_DH
)
281 options
|= DST_TYPE_KEY
;
284 if (type
!= NULL
&& (options
& DST_TYPE_KEY
) != 0) {
285 if (strcasecmp(type
, "NOAUTH") == 0)
286 flags
|= DNS_KEYTYPE_NOAUTH
;
287 else if (strcasecmp(type
, "NOCONF") == 0)
288 flags
|= DNS_KEYTYPE_NOCONF
;
289 else if (strcasecmp(type
, "NOAUTHCONF") == 0) {
290 flags
|= (DNS_KEYTYPE_NOAUTH
| DNS_KEYTYPE_NOCONF
);
294 else if (strcasecmp(type
, "AUTHCONF") == 0)
297 fatal("invalid type %s", type
);
301 fatal("key size not specified (-b option)");
304 case DNS_KEYALG_RSAMD5
:
305 case DNS_KEYALG_RSASHA1
:
306 if (size
!= 0 && (size
< 512 || size
> MAX_RSA
))
307 fatal("RSA key size %d out of range", size
);
310 if (size
!= 0 && (size
< 128 || size
> 4096))
311 fatal("DH key size %d out of range", size
);
314 if (size
!= 0 && !dsa_size_ok(size
))
315 fatal("invalid DSS key size: %d", size
);
317 case DST_ALG_HMACMD5
:
318 if (size
< 1 || size
> 512)
319 fatal("HMAC-MD5 key size %d out of range", size
);
320 if (dbits
!= 0 && (dbits
< 80 || dbits
> 128))
321 fatal("HMAC-MD5 digest bits %d out of range", dbits
);
322 if ((dbits
% 8) != 0)
323 fatal("HMAC-MD5 digest bits %d not divisible by 8",
326 case DST_ALG_HMACSHA1
:
327 if (size
< 1 || size
> 160)
328 fatal("HMAC-SHA1 key size %d out of range", size
);
329 if (dbits
!= 0 && (dbits
< 80 || dbits
> 160))
330 fatal("HMAC-SHA1 digest bits %d out of range", dbits
);
331 if ((dbits
% 8) != 0)
332 fatal("HMAC-SHA1 digest bits %d not divisible by 8",
335 case DST_ALG_HMACSHA224
:
336 if (size
< 1 || size
> 224)
337 fatal("HMAC-SHA224 key size %d out of range", size
);
338 if (dbits
!= 0 && (dbits
< 112 || dbits
> 224))
339 fatal("HMAC-SHA224 digest bits %d out of range", dbits
);
340 if ((dbits
% 8) != 0)
341 fatal("HMAC-SHA224 digest bits %d not divisible by 8",
344 case DST_ALG_HMACSHA256
:
345 if (size
< 1 || size
> 256)
346 fatal("HMAC-SHA256 key size %d out of range", size
);
347 if (dbits
!= 0 && (dbits
< 128 || dbits
> 256))
348 fatal("HMAC-SHA256 digest bits %d out of range", dbits
);
349 if ((dbits
% 8) != 0)
350 fatal("HMAC-SHA256 digest bits %d not divisible by 8",
353 case DST_ALG_HMACSHA384
:
354 if (size
< 1 || size
> 384)
355 fatal("HMAC-384 key size %d out of range", size
);
356 if (dbits
!= 0 && (dbits
< 192 || dbits
> 384))
357 fatal("HMAC-SHA384 digest bits %d out of range", dbits
);
358 if ((dbits
% 8) != 0)
359 fatal("HMAC-SHA384 digest bits %d not divisible by 8",
362 case DST_ALG_HMACSHA512
:
363 if (size
< 1 || size
> 512)
364 fatal("HMAC-SHA512 key size %d out of range", size
);
365 if (dbits
!= 0 && (dbits
< 256 || dbits
> 512))
366 fatal("HMAC-SHA512 digest bits %d out of range", dbits
);
367 if ((dbits
% 8) != 0)
368 fatal("HMAC-SHA512 digest bits %d not divisible by 8",
373 if (!(alg
== DNS_KEYALG_RSAMD5
|| alg
== DNS_KEYALG_RSASHA1
) &&
375 fatal("specified RSA exponent for a non-RSA key");
377 if (alg
!= DNS_KEYALG_DH
&& generator
!= 0)
378 fatal("specified DH generator for a non-DH key");
380 if (nametype
== NULL
) {
381 if ((options
& DST_TYPE_KEY
) != 0) /* KEY / HMAC */
382 fatal("no nametype specified");
383 flags
|= DNS_KEYOWNER_ZONE
; /* DNSKEY */
384 } else if (strcasecmp(nametype
, "zone") == 0)
385 flags
|= DNS_KEYOWNER_ZONE
;
386 else if ((options
& DST_TYPE_KEY
) != 0) { /* KEY / HMAC */
387 if (strcasecmp(nametype
, "host") == 0 ||
388 strcasecmp(nametype
, "entity") == 0)
389 flags
|= DNS_KEYOWNER_ENTITY
;
390 else if (strcasecmp(nametype
, "user") == 0)
391 flags
|= DNS_KEYOWNER_USER
;
393 fatal("invalid KEY nametype %s", nametype
);
394 } else if (strcasecmp(nametype
, "other") != 0) /* DNSKEY */
395 fatal("invalid DNSKEY nametype %s", nametype
);
397 rdclass
= strtoclass(classname
);
399 if ((options
& DST_TYPE_KEY
) != 0) /* KEY / HMAC */
401 else if ((flags
& DNS_KEYOWNER_ZONE
) != 0) /* DNSKEY */
405 protocol
= DNS_KEYPROTO_DNSSEC
;
406 else if ((options
& DST_TYPE_KEY
) == 0 &&
407 protocol
!= DNS_KEYPROTO_DNSSEC
)
408 fatal("invalid DNSKEY protocol: %d", protocol
);
410 if ((flags
& DNS_KEYFLAG_TYPEMASK
) == DNS_KEYTYPE_NOKEY
) {
412 fatal("specified null key with non-zero size");
413 if ((flags
& DNS_KEYFLAG_SIGNATORYMASK
) != 0)
414 fatal("specified null key with signing authority");
417 if ((flags
& DNS_KEYFLAG_OWNERMASK
) == DNS_KEYOWNER_ZONE
&&
418 (alg
== DNS_KEYALG_DH
|| alg
== DST_ALG_HMACMD5
||
419 alg
== DST_ALG_HMACSHA1
|| alg
== DST_ALG_HMACSHA224
||
420 alg
== DST_ALG_HMACSHA256
|| alg
== DST_ALG_HMACSHA384
||
421 alg
== DST_ALG_HMACSHA512
))
422 fatal("a key with algorithm '%s' cannot be a zone key",
425 dns_fixedname_init(&fname
);
426 name
= dns_fixedname_name(&fname
);
427 isc_buffer_init(&buf
, argv
[isc_commandline_index
],
428 strlen(argv
[isc_commandline_index
]));
429 isc_buffer_add(&buf
, strlen(argv
[isc_commandline_index
]));
430 ret
= dns_name_fromtext(name
, &buf
, dns_rootname
, ISC_FALSE
, NULL
);
431 if (ret
!= ISC_R_SUCCESS
)
432 fatal("invalid key name %s: %s", argv
[isc_commandline_index
],
433 isc_result_totext(ret
));
436 case DNS_KEYALG_RSAMD5
:
437 case DNS_KEYALG_RSASHA1
:
444 case DST_ALG_HMACMD5
:
445 case DST_ALG_HMACSHA1
:
446 case DST_ALG_HMACSHA224
:
447 case DST_ALG_HMACSHA256
:
448 case DST_ALG_HMACSHA384
:
449 case DST_ALG_HMACSHA512
:
454 if ((flags
& DNS_KEYFLAG_TYPEMASK
) == DNS_KEYTYPE_NOKEY
)
457 isc_buffer_init(&buf
, filename
, sizeof(filename
) - 1);
460 conflict
= ISC_FALSE
;
463 /* generate the key */
464 ret
= dst_key_generate(name
, alg
, size
, param
, flags
, protocol
,
465 rdclass
, mctx
, &key
);
466 isc_entropy_stopcallbacksources(ectx
);
468 if (ret
!= ISC_R_SUCCESS
) {
469 char namestr
[DNS_NAME_FORMATSIZE
];
470 char algstr
[ALG_FORMATSIZE
];
471 dns_name_format(name
, namestr
, sizeof(namestr
));
472 alg_format(alg
, algstr
, sizeof(algstr
));
473 fatal("failed to generate key %s/%s: %s\n",
474 namestr
, algstr
, isc_result_totext(ret
));
478 dst_key_setbits(key
, dbits
);
481 * Try to read a key with the same name, alg and id from disk.
482 * If there is one we must continue generating a new one
483 * unless we were asked to generate a null key, in which
484 * case we return failure.
486 ret
= dst_key_fromfile(name
, dst_key_id(key
), alg
,
487 DST_TYPE_PRIVATE
, NULL
, mctx
, &oldkey
);
488 /* do not overwrite an existing key */
489 if (ret
== ISC_R_SUCCESS
) {
490 dst_key_free(&oldkey
);
495 if (conflict
== ISC_TRUE
) {
497 isc_buffer_clear(&buf
);
498 ret
= dst_key_buildfilename(key
, 0, NULL
, &buf
);
500 "%s: %s already exists, "
501 "generating a new key\n",
507 } while (conflict
== ISC_TRUE
);
510 fatal("cannot generate a null key when a key with id 0 "
513 ret
= dst_key_tofile(key
, options
, NULL
);
514 if (ret
!= ISC_R_SUCCESS
) {
515 char keystr
[KEY_FORMATSIZE
];
516 key_format(key
, keystr
, sizeof(keystr
));
517 fatal("failed to write key %s: %s\n", keystr
,
518 isc_result_totext(ret
));
521 isc_buffer_clear(&buf
);
522 ret
= dst_key_buildfilename(key
, 0, NULL
, &buf
);
523 printf("%s\n", filename
);
526 cleanup_logging(&log
);
527 cleanup_entropy(&ectx
);
531 isc_mem_stats(mctx
, stdout
);
532 isc_mem_destroy(&mctx
);