1 /* $NetBSD: dnssec-keyfromlabel.c,v 1.1.1.3 2009/12/26 22:18:58 christos Exp $ */
4 * Copyright (C) 2007-2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: dnssec-keyfromlabel.c,v 1.29 2009/11/25 23:00:32 marka Exp */
28 #include <isc/buffer.h>
29 #include <isc/commandline.h>
30 #include <isc/entropy.h>
32 #include <isc/region.h>
33 #include <isc/print.h>
34 #include <isc/string.h>
37 #include <dns/fixedname.h>
38 #include <dns/keyvalues.h>
41 #include <dns/rdataclass.h>
42 #include <dns/result.h>
43 #include <dns/secalg.h>
47 #include "dnssectool.h"
49 #define MAX_RSA 4096 /* should be long enough... */
51 const char *program
= "dnssec-keyfromlabel";
54 #define DEFAULT_ALGORITHM "RSASHA1"
55 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
57 static const char *algs
= "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
58 " NSEC3DSA | NSEC3RSASHA1 |"
59 " RSASHA256 | RSASHA512";
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
, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
89 fprintf(stderr
, " (DNSKEY generation defaults to ZONE\n");
90 fprintf(stderr
, " -p protocol: default: 3 [dnssec]\n");
91 fprintf(stderr
, " -t type: "
92 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
93 "(default: AUTHCONF)\n");
94 fprintf(stderr
, " -v verbose level\n");
95 fprintf(stderr
, "Date options:\n");
96 fprintf(stderr
, " -P date/[+-]offset: set key publication date\n");
97 fprintf(stderr
, " -A date/[+-]offset: set key activation date\n");
98 fprintf(stderr
, " -R date/[+-]offset: set key revocation date\n");
99 fprintf(stderr
, " -I date/[+-]offset: set key inactivation date\n");
100 fprintf(stderr
, " -D date/[+-]offset: set key deletion date\n");
101 fprintf(stderr
, " -G: generate key only; do not set -P or -A\n");
102 fprintf(stderr
, " -C: generate a backward-compatible key, omitting"
104 fprintf(stderr
, "Output:\n");
105 fprintf(stderr
, " K<name>+<alg>+<id>.key, "
106 "K<name>+<alg>+<id>.private\n");
112 main(int argc
, char **argv
) {
113 char *algname
= NULL
, *nametype
= NULL
, *type
= NULL
;
114 const char *directory
= NULL
;
116 const char *engine
= "pkcs11";
118 const char *engine
= NULL
;
120 char *classname
= NULL
;
122 dst_key_t
*key
= NULL
, *oldkey
= NULL
;
123 dns_fixedname_t fname
;
125 isc_uint16_t flags
= 0, kskflag
= 0, revflag
= 0;
127 isc_boolean_t oldstyle
= ISC_FALSE
;
128 isc_mem_t
*mctx
= NULL
;
130 int protocol
= -1, signatory
= 0;
135 isc_log_t
*log
= NULL
;
136 isc_entropy_t
*ectx
= NULL
;
137 dns_rdataclass_t rdclass
;
138 int options
= DST_TYPE_PRIVATE
| DST_TYPE_PUBLIC
;
140 isc_stdtime_t publish
= 0, activate
= 0, revoke
= 0;
141 isc_stdtime_t inactive
= 0, delete = 0;
143 isc_boolean_t setpub
= ISC_FALSE
, setact
= ISC_FALSE
;
144 isc_boolean_t setrev
= ISC_FALSE
, setinact
= ISC_FALSE
;
145 isc_boolean_t setdel
= ISC_FALSE
;
146 isc_boolean_t unsetpub
= ISC_FALSE
, unsetact
= ISC_FALSE
;
147 isc_boolean_t unsetrev
= ISC_FALSE
, unsetinact
= ISC_FALSE
;
148 isc_boolean_t unsetdel
= ISC_FALSE
;
149 isc_boolean_t genonly
= ISC_FALSE
;
150 isc_boolean_t use_nsec3
= ISC_FALSE
;
156 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
158 dns_result_register();
160 isc_commandline_errprint
= ISC_FALSE
;
162 isc_stdtime_get(&now
);
164 while ((ch
= isc_commandline_parse(argc
, argv
,
165 "3a:Cc:E:f:K:kl:n:p:t:v:FhGP:A:R:I:D:")) != -1)
169 use_nsec3
= ISC_TRUE
;
172 algname
= isc_commandline_argument
;
178 classname
= isc_commandline_argument
;
181 engine
= isc_commandline_argument
;
184 c
= (unsigned char)(isc_commandline_argument
[0]);
185 if (toupper(c
) == 'K')
186 kskflag
= DNS_KEYFLAG_KSK
;
187 else if (toupper(c
) == 'R')
188 revflag
= DNS_KEYFLAG_REVOKE
;
190 fatal("unknown flag '%s'",
191 isc_commandline_argument
);
194 directory
= isc_commandline_argument
;
195 ret
= try_dir(directory
);
196 if (ret
!= ISC_R_SUCCESS
)
197 fatal("cannot open directory %s: %s",
198 directory
, isc_result_totext(ret
));
201 options
|= DST_TYPE_KEY
;
204 label
= isc_mem_strdup(mctx
, isc_commandline_argument
);
207 nametype
= isc_commandline_argument
;
210 protocol
= strtol(isc_commandline_argument
, &endp
, 10);
211 if (*endp
!= '\0' || protocol
< 0 || protocol
> 255)
212 fatal("-p must be followed by a number "
216 type
= isc_commandline_argument
;
219 verbose
= strtol(isc_commandline_argument
, &endp
, 0);
221 fatal("-v must be followed by a number");
227 if (setpub
|| unsetpub
)
228 fatal("-P specified more than once");
230 if (strcasecmp(isc_commandline_argument
, "none")) {
232 publish
= strtotime(isc_commandline_argument
,
239 if (setact
|| unsetact
)
240 fatal("-A specified more than once");
242 if (strcasecmp(isc_commandline_argument
, "none")) {
244 activate
= strtotime(isc_commandline_argument
,
251 if (setrev
|| unsetrev
)
252 fatal("-R specified more than once");
254 if (strcasecmp(isc_commandline_argument
, "none")) {
256 revoke
= strtotime(isc_commandline_argument
,
263 if (setinact
|| unsetinact
)
264 fatal("-I specified more than once");
266 if (strcasecmp(isc_commandline_argument
, "none")) {
268 inactive
= strtotime(isc_commandline_argument
,
271 unsetinact
= ISC_TRUE
;
275 if (setdel
|| unsetdel
)
276 fatal("-D specified more than once");
278 if (strcasecmp(isc_commandline_argument
, "none")) {
280 delete = strtotime(isc_commandline_argument
,
287 /* Reserved for FIPS mode */
290 if (isc_commandline_option
!= '?')
291 fprintf(stderr
, "%s: invalid argument -%c\n",
292 program
, isc_commandline_option
);
298 fprintf(stderr
, "%s: unhandled option -%c\n",
299 program
, isc_commandline_option
);
305 setup_entropy(mctx
, NULL
, &ectx
);
306 ret
= dst_lib_init2(mctx
, ectx
, engine
,
307 ISC_ENTROPY_BLOCKING
| ISC_ENTROPY_GOODONLY
);
308 if (ret
!= ISC_R_SUCCESS
)
309 fatal("could not initialize dst: %s",
310 isc_result_totext(ret
));
312 setup_logging(verbose
, mctx
, &log
);
315 fatal("the key label was not specified");
316 if (argc
< isc_commandline_index
+ 1)
317 fatal("the key name was not specified");
318 if (argc
> isc_commandline_index
+ 1)
319 fatal("extraneous arguments");
321 if (strchr(label
, ':') == NULL
&&
322 engine
!= NULL
&& strlen(engine
) != 0U) {
326 len
= strlen(label
) + strlen(engine
) + 2;
327 l
= isc_mem_allocate(mctx
, len
);
329 fatal("cannot allocate memory");
330 snprintf(l
, len
, "%s:%s", engine
, label
);
331 isc_mem_free(mctx
, label
);
335 if (algname
== NULL
) {
337 algname
= strdup(DEFAULT_NSEC3_ALGORITHM
);
339 algname
= strdup(DEFAULT_ALGORITHM
);
341 fprintf(stderr
, "no algorithm specified; "
342 "defaulting to %s\n", algname
);
345 if (strcasecmp(algname
, "RSA") == 0) {
346 fprintf(stderr
, "The use of RSA (RSAMD5) is not recommended.\n"
347 "If you still wish to use RSA (RSAMD5) please "
348 "specify \"-a RSAMD5\"\n");
352 r
.length
= strlen(algname
);
353 ret
= dns_secalg_fromtext(&alg
, &r
);
354 if (ret
!= ISC_R_SUCCESS
)
355 fatal("unknown algorithm %s", algname
);
356 if (alg
== DST_ALG_DH
)
357 options
|= DST_TYPE_KEY
;
361 alg
!= DST_ALG_NSEC3DSA
&& alg
!= DST_ALG_NSEC3RSASHA1
&&
362 alg
!= DST_ALG_RSASHA256
&& alg
!= DST_ALG_RSASHA512
) {
363 fatal("%s is incompatible with NSEC3; "
364 "do not use the -3 option", algname
);
367 if (type
!= NULL
&& (options
& DST_TYPE_KEY
) != 0) {
368 if (strcasecmp(type
, "NOAUTH") == 0)
369 flags
|= DNS_KEYTYPE_NOAUTH
;
370 else if (strcasecmp(type
, "NOCONF") == 0)
371 flags
|= DNS_KEYTYPE_NOCONF
;
372 else if (strcasecmp(type
, "NOAUTHCONF") == 0) {
373 flags
|= (DNS_KEYTYPE_NOAUTH
| DNS_KEYTYPE_NOCONF
);
375 else if (strcasecmp(type
, "AUTHCONF") == 0)
378 fatal("invalid type %s", type
);
381 if (nametype
== NULL
) {
382 if ((options
& DST_TYPE_KEY
) != 0) /* KEY */
383 fatal("no nametype specified");
384 flags
|= DNS_KEYOWNER_ZONE
; /* DNSKEY */
385 } else if (strcasecmp(nametype
, "zone") == 0)
386 flags
|= DNS_KEYOWNER_ZONE
;
387 else if ((options
& DST_TYPE_KEY
) != 0) { /* KEY */
388 if (strcasecmp(nametype
, "host") == 0 ||
389 strcasecmp(nametype
, "entity") == 0)
390 flags
|= DNS_KEYOWNER_ENTITY
;
391 else if (strcasecmp(nametype
, "user") == 0)
392 flags
|= DNS_KEYOWNER_USER
;
394 fatal("invalid KEY nametype %s", nametype
);
395 } else if (strcasecmp(nametype
, "other") != 0) /* DNSKEY */
396 fatal("invalid DNSKEY nametype %s", nametype
);
398 rdclass
= strtoclass(classname
);
400 if (directory
== NULL
)
403 if ((options
& DST_TYPE_KEY
) != 0) /* KEY */
405 else if ((flags
& DNS_KEYOWNER_ZONE
) != 0) { /* DNSKEY */
411 protocol
= DNS_KEYPROTO_DNSSEC
;
412 else if ((options
& DST_TYPE_KEY
) == 0 &&
413 protocol
!= DNS_KEYPROTO_DNSSEC
)
414 fatal("invalid DNSKEY protocol: %d", protocol
);
416 if ((flags
& DNS_KEYFLAG_TYPEMASK
) == DNS_KEYTYPE_NOKEY
) {
417 if ((flags
& DNS_KEYFLAG_SIGNATORYMASK
) != 0)
418 fatal("specified null key with signing authority");
421 if ((flags
& DNS_KEYFLAG_OWNERMASK
) == DNS_KEYOWNER_ZONE
&&
422 alg
== DNS_KEYALG_DH
)
423 fatal("a key with algorithm '%s' cannot be a zone key",
426 dns_fixedname_init(&fname
);
427 name
= dns_fixedname_name(&fname
);
428 isc_buffer_init(&buf
, argv
[isc_commandline_index
],
429 strlen(argv
[isc_commandline_index
]));
430 isc_buffer_add(&buf
, strlen(argv
[isc_commandline_index
]));
431 ret
= dns_name_fromtext(name
, &buf
, dns_rootname
, 0, NULL
);
432 if (ret
!= ISC_R_SUCCESS
)
433 fatal("invalid key name %s: %s", argv
[isc_commandline_index
],
434 isc_result_totext(ret
));
436 isc_buffer_init(&buf
, filename
, sizeof(filename
) - 1);
438 /* associate the key */
439 ret
= dst_key_fromlabel(name
, alg
, flags
, protocol
,
440 rdclass
, engine
, label
, NULL
, mctx
, &key
);
441 isc_entropy_stopcallbacksources(ectx
);
443 if (ret
!= ISC_R_SUCCESS
) {
444 char namestr
[DNS_NAME_FORMATSIZE
];
445 char algstr
[DNS_SECALG_FORMATSIZE
];
446 dns_name_format(name
, namestr
, sizeof(namestr
));
447 dns_secalg_format(alg
, algstr
, sizeof(algstr
));
448 fatal("failed to get key %s/%s: %s\n",
449 namestr
, algstr
, isc_result_totext(ret
));
455 * Set key timing metadata (unless using -C)
457 * Publish and activation dates are set to "now" by default, but
458 * can be overridden. Creation date is always set to "now".
461 dst_key_settime(key
, DST_TIME_CREATED
, now
);
463 if (genonly
&& (setpub
|| setact
))
464 fatal("cannot use -G together with -P or -A options");
467 dst_key_settime(key
, DST_TIME_PUBLISH
, publish
);
469 dst_key_settime(key
, DST_TIME_PUBLISH
, activate
);
470 else if (!genonly
&& !unsetpub
)
471 dst_key_settime(key
, DST_TIME_PUBLISH
, now
);
474 dst_key_settime(key
, DST_TIME_ACTIVATE
, activate
);
475 else if (!genonly
&& !unsetact
)
476 dst_key_settime(key
, DST_TIME_ACTIVATE
, now
);
480 fprintf(stderr
, "%s: warning: Key is "
481 "not flagged as a KSK, but -R "
482 "was used. Revoking a ZSK is "
483 "legal, but undefined.\n",
485 dst_key_settime(key
, DST_TIME_REVOKE
, revoke
);
489 dst_key_settime(key
, DST_TIME_INACTIVE
, inactive
);
492 dst_key_settime(key
, DST_TIME_DELETE
, delete);
494 if (setpub
|| setact
|| setrev
|| setinact
||
495 setdel
|| unsetpub
|| unsetact
||
496 unsetrev
|| unsetinact
|| unsetdel
|| genonly
)
497 fatal("cannot use -C together with "
498 "-P, -A, -R, -I, -D, or -G options");
500 * Compatibility mode: Private-key-format
501 * should be set to 1.2.
503 dst_key_setprivateformat(key
, 1, 2);
507 * Try to read a key with the same name, alg and id from disk.
508 * If there is one we must return failure.
510 ret
= dst_key_fromfile(name
, dst_key_id(key
), alg
,
511 DST_TYPE_PRIVATE
, directory
, mctx
, &oldkey
);
512 /* do not overwrite an existing key */
513 if (ret
== ISC_R_SUCCESS
) {
514 isc_buffer_clear(&buf
);
515 ret
= dst_key_buildfilename(key
, 0, directory
, &buf
);
516 fatal("%s: %s already exists\n", program
, filename
);
519 ret
= dst_key_tofile(key
, options
, directory
);
520 if (ret
!= ISC_R_SUCCESS
) {
521 char keystr
[DST_KEY_FORMATSIZE
];
522 dst_key_format(key
, keystr
, sizeof(keystr
));
523 fatal("failed to write key %s: %s\n", keystr
,
524 isc_result_totext(ret
));
527 isc_buffer_clear(&buf
);
528 ret
= dst_key_buildfilename(key
, 0, NULL
, &buf
);
529 printf("%s\n", filename
);
532 cleanup_logging(&log
);
533 cleanup_entropy(&ectx
);
537 isc_mem_stats(mctx
, stdout
);
538 isc_mem_free(mctx
, label
);
539 isc_mem_destroy(&mctx
);