2 * Copyright (C) 2007-2012 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: dnssec-keyfromlabel.c,v 1.38 2011/11/30 00:48:51 marka Exp $ */
26 #include <isc/buffer.h>
27 #include <isc/commandline.h>
28 #include <isc/entropy.h>
30 #include <isc/region.h>
31 #include <isc/print.h>
32 #include <isc/string.h>
35 #include <dns/dnssec.h>
36 #include <dns/fixedname.h>
37 #include <dns/keyvalues.h>
40 #include <dns/rdataclass.h>
41 #include <dns/result.h>
42 #include <dns/secalg.h>
46 #include "dnssectool.h"
48 #define MAX_RSA 4096 /* should be long enough... */
50 const char *program = "dnssec-keyfromlabel";
53 #define DEFAULT_ALGORITHM "RSASHA1"
54 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
56 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
57 " NSEC3DSA | NSEC3RSASHA1 |"
58 " RSASHA256 | RSASHA512 | ECCGOST |"
59 " ECDSAP256SHA256 | ECDSAP384SHA384";
61 ISC_PLATFORM_NORETURN_PRE static void
62 usage(void) ISC_PLATFORM_NORETURN_POST;
66 fprintf(stderr, "Usage:\n");
67 fprintf(stderr, " %s -l label [options] name\n\n",
69 fprintf(stderr, "Version: %s\n", VERSION);
70 fprintf(stderr, "Required options:\n");
71 fprintf(stderr, " -l label: label of the key pair\n");
72 fprintf(stderr, " name: owner of the key\n");
73 fprintf(stderr, "Other options:\n");
74 fprintf(stderr, " -a algorithm: %s\n", algs);
75 fprintf(stderr, " (default: RSASHA1, or "
76 "NSEC3RSASHA1 if using -3)\n");
77 fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
78 fprintf(stderr, " -c class (default: IN)\n");
80 fprintf(stderr, " -E enginename (default: pkcs11)\n");
82 fprintf(stderr, " -E enginename\n");
84 fprintf(stderr, " -f keyflag: KSK | REVOKE\n");
85 fprintf(stderr, " -K directory: directory in which to place "
87 fprintf(stderr, " -k: generate a TYPE=KEY key\n");
88 fprintf(stderr, " -L ttl: default key TTL\n");
89 fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
90 fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
91 fprintf(stderr, " -p protocol: default: 3 [dnssec]\n");
92 fprintf(stderr, " -t type: "
93 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
94 "(default: AUTHCONF)\n");
95 fprintf(stderr, " -y: permit keys that might collide\n");
96 fprintf(stderr, " -v verbose level\n");
97 fprintf(stderr, "Date options:\n");
98 fprintf(stderr, " -P date/[+-]offset: set key publication date\n");
99 fprintf(stderr, " -A date/[+-]offset: set key activation date\n");
100 fprintf(stderr, " -R date/[+-]offset: set key revocation date\n");
101 fprintf(stderr, " -I date/[+-]offset: set key inactivation date\n");
102 fprintf(stderr, " -D date/[+-]offset: set key deletion date\n");
103 fprintf(stderr, " -G: generate key only; do not set -P or -A\n");
104 fprintf(stderr, " -C: generate a backward-compatible key, omitting"
106 fprintf(stderr, "Output:\n");
107 fprintf(stderr, " K<name>+<alg>+<id>.key, "
108 "K<name>+<alg>+<id>.private\n");
114 main(int argc, char **argv) {
115 char *algname = NULL, *freeit = NULL;
116 char *nametype = NULL, *type = NULL;
117 const char *directory = NULL;
119 const char *engine = "pkcs11";
121 const char *engine = NULL;
123 char *classname = NULL;
125 dst_key_t *key = NULL;
126 dns_fixedname_t fname;
128 isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
130 isc_boolean_t oldstyle = ISC_FALSE;
131 isc_mem_t *mctx = NULL;
133 int protocol = -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;
144 isc_stdtime_t publish = 0, activate = 0, revoke = 0;
145 isc_stdtime_t inactive = 0, delete = 0;
147 isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
148 isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
149 isc_boolean_t setdel = ISC_FALSE, setttl = ISC_FALSE;
150 isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
151 isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
152 isc_boolean_t unsetdel = ISC_FALSE;
153 isc_boolean_t genonly = ISC_FALSE;
154 isc_boolean_t use_nsec3 = ISC_FALSE;
155 isc_boolean_t avoid_collisions = ISC_TRUE;
162 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
164 dns_result_register();
166 isc_commandline_errprint = ISC_FALSE;
168 isc_stdtime_get(&now);
170 while ((ch = isc_commandline_parse(argc, argv,
171 "3a:Cc:E:f:K:kl:L:n:p:t:v:yFhGP:A:R:I:D:")) != -1)
175 use_nsec3 = ISC_TRUE;
178 algname = isc_commandline_argument;
184 classname = isc_commandline_argument;
187 engine = isc_commandline_argument;
190 c = (unsigned char)(isc_commandline_argument[0]);
191 if (toupper(c) == 'K')
192 kskflag = DNS_KEYFLAG_KSK;
193 else if (toupper(c) == 'R')
194 revflag = DNS_KEYFLAG_REVOKE;
196 fatal("unknown flag '%s'",
197 isc_commandline_argument);
200 directory = isc_commandline_argument;
201 ret = try_dir(directory);
202 if (ret != ISC_R_SUCCESS)
203 fatal("cannot open directory %s: %s",
204 directory, isc_result_totext(ret));
207 options |= DST_TYPE_KEY;
210 if (strcmp(isc_commandline_argument, "none") == 0)
213 ttl = strtottl(isc_commandline_argument);
217 label = isc_mem_strdup(mctx, isc_commandline_argument);
220 nametype = isc_commandline_argument;
223 protocol = strtol(isc_commandline_argument, &endp, 10);
224 if (*endp != '\0' || protocol < 0 || protocol > 255)
225 fatal("-p must be followed by a number "
229 type = isc_commandline_argument;
232 verbose = strtol(isc_commandline_argument, &endp, 0);
234 fatal("-v must be followed by a number");
237 avoid_collisions = ISC_FALSE;
243 if (setpub || unsetpub)
244 fatal("-P specified more than once");
246 if (strcasecmp(isc_commandline_argument, "none")) {
248 publish = strtotime(isc_commandline_argument,
255 if (setact || unsetact)
256 fatal("-A specified more than once");
258 if (strcasecmp(isc_commandline_argument, "none")) {
260 activate = strtotime(isc_commandline_argument,
267 if (setrev || unsetrev)
268 fatal("-R specified more than once");
270 if (strcasecmp(isc_commandline_argument, "none")) {
272 revoke = strtotime(isc_commandline_argument,
279 if (setinact || unsetinact)
280 fatal("-I specified more than once");
282 if (strcasecmp(isc_commandline_argument, "none")) {
284 inactive = strtotime(isc_commandline_argument,
287 unsetinact = ISC_TRUE;
291 if (setdel || unsetdel)
292 fatal("-D specified more than once");
294 if (strcasecmp(isc_commandline_argument, "none")) {
296 delete = strtotime(isc_commandline_argument,
303 /* Reserved for FIPS mode */
306 if (isc_commandline_option != '?')
307 fprintf(stderr, "%s: invalid argument -%c\n",
308 program, isc_commandline_option);
314 fprintf(stderr, "%s: unhandled option -%c\n",
315 program, isc_commandline_option);
321 setup_entropy(mctx, NULL, &ectx);
322 ret = dst_lib_init2(mctx, ectx, engine,
323 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
324 if (ret != ISC_R_SUCCESS)
325 fatal("could not initialize dst: %s",
326 isc_result_totext(ret));
328 setup_logging(verbose, mctx, &log);
331 fatal("the key label was not specified");
332 if (argc < isc_commandline_index + 1)
333 fatal("the key name was not specified");
334 if (argc > isc_commandline_index + 1)
335 fatal("extraneous arguments");
337 if (strchr(label, ':') == NULL &&
338 engine != NULL && strlen(engine) != 0U) {
342 len = strlen(label) + strlen(engine) + 2;
343 l = isc_mem_allocate(mctx, len);
345 fatal("cannot allocate memory");
346 snprintf(l, len, "%s:%s", engine, label);
347 isc_mem_free(mctx, label);
351 if (algname == NULL) {
353 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
355 algname = strdup(DEFAULT_ALGORITHM);
357 fatal("strdup failed");
360 fprintf(stderr, "no algorithm specified; "
361 "defaulting to %s\n", algname);
364 if (strcasecmp(algname, "RSA") == 0) {
365 fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
366 "If you still wish to use RSA (RSAMD5) please "
367 "specify \"-a RSAMD5\"\n");
373 r.length = strlen(algname);
374 ret = dns_secalg_fromtext(&alg, &r);
375 if (ret != ISC_R_SUCCESS)
376 fatal("unknown algorithm %s", algname);
377 if (alg == DST_ALG_DH)
378 options |= DST_TYPE_KEY;
382 alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
383 alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512 &&
384 alg != DST_ALG_ECCGOST &&
385 alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
386 fatal("%s is incompatible with NSEC3; "
387 "do not use the -3 option", algname);
390 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
391 if (strcasecmp(type, "NOAUTH") == 0)
392 flags |= DNS_KEYTYPE_NOAUTH;
393 else if (strcasecmp(type, "NOCONF") == 0)
394 flags |= DNS_KEYTYPE_NOCONF;
395 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
396 flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
398 else if (strcasecmp(type, "AUTHCONF") == 0)
401 fatal("invalid type %s", type);
404 if (nametype == NULL) {
405 if ((options & DST_TYPE_KEY) != 0) /* KEY */
406 fatal("no nametype specified");
407 flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
408 } else if (strcasecmp(nametype, "zone") == 0)
409 flags |= DNS_KEYOWNER_ZONE;
410 else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
411 if (strcasecmp(nametype, "host") == 0 ||
412 strcasecmp(nametype, "entity") == 0)
413 flags |= DNS_KEYOWNER_ENTITY;
414 else if (strcasecmp(nametype, "user") == 0)
415 flags |= DNS_KEYOWNER_USER;
417 fatal("invalid KEY nametype %s", nametype);
418 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
419 fatal("invalid DNSKEY nametype %s", nametype);
421 rdclass = strtoclass(classname);
423 if (directory == NULL)
426 if ((options & DST_TYPE_KEY) != 0) /* KEY */
428 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
434 protocol = DNS_KEYPROTO_DNSSEC;
435 else if ((options & DST_TYPE_KEY) == 0 &&
436 protocol != DNS_KEYPROTO_DNSSEC)
437 fatal("invalid DNSKEY protocol: %d", protocol);
439 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
440 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
441 fatal("specified null key with signing authority");
444 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
445 alg == DNS_KEYALG_DH)
446 fatal("a key with algorithm '%s' cannot be a zone key",
449 dns_fixedname_init(&fname);
450 name = dns_fixedname_name(&fname);
451 isc_buffer_init(&buf, argv[isc_commandline_index],
452 strlen(argv[isc_commandline_index]));
453 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
454 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
455 if (ret != ISC_R_SUCCESS)
456 fatal("invalid key name %s: %s", argv[isc_commandline_index],
457 isc_result_totext(ret));
459 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
461 /* associate the key */
462 ret = dst_key_fromlabel(name, alg, flags, protocol,
463 rdclass, engine, label, NULL, mctx, &key);
464 isc_entropy_stopcallbacksources(ectx);
466 if (ret != ISC_R_SUCCESS) {
467 char namestr[DNS_NAME_FORMATSIZE];
468 char algstr[DNS_SECALG_FORMATSIZE];
469 dns_name_format(name, namestr, sizeof(namestr));
470 dns_secalg_format(alg, algstr, sizeof(algstr));
471 fatal("failed to get key %s/%s: %s\n",
472 namestr, algstr, isc_result_totext(ret));
478 * Set key timing metadata (unless using -C)
480 * Publish and activation dates are set to "now" by default, but
481 * can be overridden. Creation date is always set to "now".
484 dst_key_settime(key, DST_TIME_CREATED, now);
486 if (genonly && (setpub || setact))
487 fatal("cannot use -G together with -P or -A options");
490 dst_key_settime(key, DST_TIME_PUBLISH, publish);
492 dst_key_settime(key, DST_TIME_PUBLISH, activate);
493 else if (!genonly && !unsetpub)
494 dst_key_settime(key, DST_TIME_PUBLISH, now);
497 dst_key_settime(key, DST_TIME_ACTIVATE, activate);
498 else if (!genonly && !unsetact)
499 dst_key_settime(key, DST_TIME_ACTIVATE, now);
503 fprintf(stderr, "%s: warning: Key is "
504 "not flagged as a KSK, but -R "
505 "was used. Revoking a ZSK is "
506 "legal, but undefined.\n",
508 dst_key_settime(key, DST_TIME_REVOKE, revoke);
512 dst_key_settime(key, DST_TIME_INACTIVE, inactive);
515 dst_key_settime(key, DST_TIME_DELETE, delete);
517 if (setpub || setact || setrev || setinact ||
518 setdel || unsetpub || unsetact ||
519 unsetrev || unsetinact || unsetdel || genonly)
520 fatal("cannot use -C together with "
521 "-P, -A, -R, -I, -D, or -G options");
523 * Compatibility mode: Private-key-format
524 * should be set to 1.2.
526 dst_key_setprivateformat(key, 1, 2);
529 /* Set default key TTL */
531 dst_key_setttl(key, ttl);
534 * Do not overwrite an existing key. Warn LOUDLY if there
535 * is a risk of ID collision due to this key or another key
538 if (key_collision(key, name, directory, mctx, &exact)) {
539 isc_buffer_clear(&buf);
540 ret = dst_key_buildfilename(key, 0, directory, &buf);
541 if (ret != ISC_R_SUCCESS)
542 fatal("dst_key_buildfilename returned: %s\n",
543 isc_result_totext(ret));
545 fatal("%s: %s already exists\n", program, filename);
547 if (avoid_collisions)
548 fatal("%s: %s could collide with another key upon "
549 "revokation\n", program, filename);
551 fprintf(stderr, "%s: WARNING: Key %s could collide with "
552 "another key upon revokation. If you plan "
553 "to revoke keys, destroy this key and "
554 "generate a different one.\n",
558 ret = dst_key_tofile(key, options, directory);
559 if (ret != ISC_R_SUCCESS) {
560 char keystr[DST_KEY_FORMATSIZE];
561 dst_key_format(key, keystr, sizeof(keystr));
562 fatal("failed to write key %s: %s\n", keystr,
563 isc_result_totext(ret));
566 isc_buffer_clear(&buf);
567 ret = dst_key_buildfilename(key, 0, NULL, &buf);
568 if (ret != ISC_R_SUCCESS)
569 fatal("dst_key_buildfilename returned: %s\n",
570 isc_result_totext(ret));
571 printf("%s\n", filename);
574 cleanup_logging(&log);
575 cleanup_entropy(&ectx);
579 isc_mem_stats(mctx, stdout);
580 isc_mem_free(mctx, label);
581 isc_mem_destroy(&mctx);