BIND - Update BIND to 9.5.2
[dragonfly.git] / contrib / bind-9.5.2 / bin / dnssec / dnssec-keygen.c
blob3fd29b379cee7f4c2a8467021c17c453510ceba1
1 /*
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 $ */
34 /*! \file */
36 #include <config.h>
38 #include <stdlib.h>
40 #include <isc/buffer.h>
41 #include <isc/commandline.h>
42 #include <isc/entropy.h>
43 #include <isc/mem.h>
44 #include <isc/region.h>
45 #include <isc/string.h>
46 #include <isc/util.h>
48 #include <dns/fixedname.h>
49 #include <dns/keyvalues.h>
50 #include <dns/log.h>
51 #include <dns/name.h>
52 #include <dns/rdataclass.h>
53 #include <dns/result.h>
54 #include <dns/secalg.h>
56 #include <dst/dst.h>
58 #include "dnssectool.h"
60 #define MAX_RSA 4096 /* should be long enough... */
62 const char *program = "dnssec-keygen";
63 int verbose;
65 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 | HMAC-MD5 |"
66 " HMAC-SHA1 | HMAC-SHA224 | HMAC-SHA256 | "
67 " HMAC-SHA384 | HMAC-SHA512";
69 static isc_boolean_t
70 dsa_size_ok(int size) {
71 return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
74 static void
75 usage(void) {
76 fprintf(stderr, "Usage:\n");
77 fprintf(stderr, " %s -a alg -b bits [-n type] [options] name\n\n",
78 program);
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 "
102 "(DH only)\n");
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");
117 exit (-1);
121 main(int argc, char **argv) {
122 char *algname = NULL, *nametype = NULL, *type = NULL;
123 char *classname = NULL;
124 char *endp;
125 dst_key_t *key = NULL, *oldkey;
126 dns_fixedname_t fname;
127 dns_name_t *name;
128 isc_uint16_t flags = 0, ksk = 0;
129 dns_secalg_t alg;
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;
134 isc_result_t ret;
135 isc_textregion_t r;
136 char filename[255];
137 isc_buffer_t buf;
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;
142 int dbits = 0;
144 if (argc == 1)
145 usage();
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)
156 switch (ch) {
157 case 'a':
158 algname = isc_commandline_argument;
159 break;
160 case 'b':
161 size = strtol(isc_commandline_argument, &endp, 10);
162 if (*endp != '\0' || size < 0)
163 fatal("-b requires a non-negative number");
164 break;
165 case 'c':
166 classname = isc_commandline_argument;
167 break;
168 case 'd':
169 dbits = strtol(isc_commandline_argument, &endp, 10);
170 if (*endp != '\0' || dbits < 0)
171 fatal("-d requires a non-negative number");
172 break;
173 case 'e':
174 rsa_exp = 1;
175 break;
176 case 'f':
177 if (strcasecmp(isc_commandline_argument, "KSK") == 0)
178 ksk = DNS_KEYFLAG_KSK;
179 else
180 fatal("unknown flag '%s'",
181 isc_commandline_argument);
182 break;
183 case 'g':
184 generator = strtol(isc_commandline_argument,
185 &endp, 10);
186 if (*endp != '\0' || generator <= 0)
187 fatal("-g requires a positive number");
188 break;
189 case 'k':
190 options |= DST_TYPE_KEY;
191 break;
192 case 'n':
193 nametype = isc_commandline_argument;
194 break;
195 case 't':
196 type = isc_commandline_argument;
197 break;
198 case 'p':
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 "
202 "[0..255]");
203 break;
204 case 's':
205 signatory = strtol(isc_commandline_argument,
206 &endp, 10);
207 if (*endp != '\0' || signatory < 0 || signatory > 15)
208 fatal("-s must be followed by a number "
209 "[0..15]");
210 break;
211 case 'r':
212 setup_entropy(mctx, isc_commandline_argument, &ectx);
213 break;
214 case 'v':
215 endp = NULL;
216 verbose = strtol(isc_commandline_argument, &endp, 0);
217 if (*endp != '\0')
218 fatal("-v must be followed by a number");
219 break;
221 case '?':
222 if (isc_commandline_option != '?')
223 fprintf(stderr, "%s: invalid argument -%c\n",
224 program, isc_commandline_option);
225 case 'h':
226 usage();
228 default:
229 fprintf(stderr, "%s: unhandled option -%c\n",
230 program, isc_commandline_option);
231 exit(1);
235 if (ectx == NULL)
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");
249 if (algname == NULL)
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");
255 return (1);
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;
274 } else {
275 r.base = algname;
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);
291 if (size < 0)
292 size = 0;
294 else if (strcasecmp(type, "AUTHCONF") == 0)
295 /* nothing */;
296 else
297 fatal("invalid type %s", type);
300 if (size < 0)
301 fatal("key size not specified (-b option)");
303 switch (alg) {
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);
308 break;
309 case DNS_KEYALG_DH:
310 if (size != 0 && (size < 128 || size > 4096))
311 fatal("DH key size %d out of range", size);
312 break;
313 case DNS_KEYALG_DSA:
314 if (size != 0 && !dsa_size_ok(size))
315 fatal("invalid DSS key size: %d", size);
316 break;
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",
324 dbits);
325 break;
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",
333 dbits);
334 break;
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",
342 dbits);
343 break;
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",
351 dbits);
352 break;
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",
360 dbits);
361 break;
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",
369 dbits);
370 break;
373 if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1) &&
374 rsa_exp != 0)
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;
392 else
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 */
400 flags |= signatory;
401 else if ((flags & DNS_KEYOWNER_ZONE) != 0) /* DNSKEY */
402 flags |= ksk;
404 if (protocol == -1)
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) {
411 if (size > 0)
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",
423 algname);
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));
435 switch(alg) {
436 case DNS_KEYALG_RSAMD5:
437 case DNS_KEYALG_RSASHA1:
438 param = rsa_exp;
439 break;
440 case DNS_KEYALG_DH:
441 param = generator;
442 break;
443 case DNS_KEYALG_DSA:
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:
450 param = 0;
451 break;
454 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
455 null_key = ISC_TRUE;
457 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
459 do {
460 conflict = ISC_FALSE;
461 oldkey = NULL;
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));
475 exit(-1);
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);
491 conflict = ISC_TRUE;
492 if (null_key)
493 break;
495 if (conflict == ISC_TRUE) {
496 if (verbose > 0) {
497 isc_buffer_clear(&buf);
498 ret = dst_key_buildfilename(key, 0, NULL, &buf);
499 fprintf(stderr,
500 "%s: %s already exists, "
501 "generating a new key\n",
502 program, filename);
504 dst_key_free(&key);
507 } while (conflict == ISC_TRUE);
509 if (conflict)
510 fatal("cannot generate a null key when a key with id 0 "
511 "already exists");
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);
524 dst_key_free(&key);
526 cleanup_logging(&log);
527 cleanup_entropy(&ectx);
528 dst_lib_destroy();
529 dns_name_destroy();
530 if (verbose > 10)
531 isc_mem_stats(mctx, stdout);
532 isc_mem_destroy(&mctx);
534 return (0);