No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / dnssec / dnssec-keyfromlabel.c
blob5e006c90646168c3f2e5ba81931d97e2ca9a6b35
1 /* $NetBSD: dnssec-keyfromlabel.c,v 1.1.1.3 2009/12/26 22:18:58 christos Exp $ */
3 /*
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 */
21 /*! \file */
23 #include <config.h>
25 #include <ctype.h>
26 #include <stdlib.h>
28 #include <isc/buffer.h>
29 #include <isc/commandline.h>
30 #include <isc/entropy.h>
31 #include <isc/mem.h>
32 #include <isc/region.h>
33 #include <isc/print.h>
34 #include <isc/string.h>
35 #include <isc/util.h>
37 #include <dns/fixedname.h>
38 #include <dns/keyvalues.h>
39 #include <dns/log.h>
40 #include <dns/name.h>
41 #include <dns/rdataclass.h>
42 #include <dns/result.h>
43 #include <dns/secalg.h>
45 #include <dst/dst.h>
47 #include "dnssectool.h"
49 #define MAX_RSA 4096 /* should be long enough... */
51 const char *program = "dnssec-keyfromlabel";
52 int verbose;
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;
64 static void
65 usage(void) {
66 fprintf(stderr, "Usage:\n");
67 fprintf(stderr, " %s -l label [options] name\n\n",
68 program);
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");
79 #ifdef USE_PKCS11
80 fprintf(stderr, " -E enginename (default: pkcs11)\n");
81 #else
82 fprintf(stderr, " -E enginename\n");
83 #endif
84 fprintf(stderr, " -f keyflag: KSK | REVOKE\n");
85 fprintf(stderr, " -K directory: directory in which to place "
86 "key files\n");
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"
103 " all dates\n");
104 fprintf(stderr, "Output:\n");
105 fprintf(stderr, " K<name>+<alg>+<id>.key, "
106 "K<name>+<alg>+<id>.private\n");
108 exit (-1);
112 main(int argc, char **argv) {
113 char *algname = NULL, *nametype = NULL, *type = NULL;
114 const char *directory = NULL;
115 #ifdef USE_PKCS11
116 const char *engine = "pkcs11";
117 #else
118 const char *engine = NULL;
119 #endif
120 char *classname = NULL;
121 char *endp;
122 dst_key_t *key = NULL, *oldkey = NULL;
123 dns_fixedname_t fname;
124 dns_name_t *name;
125 isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
126 dns_secalg_t alg;
127 isc_boolean_t oldstyle = ISC_FALSE;
128 isc_mem_t *mctx = NULL;
129 int ch;
130 int protocol = -1, signatory = 0;
131 isc_result_t ret;
132 isc_textregion_t r;
133 char filename[255];
134 isc_buffer_t buf;
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;
139 char *label = NULL;
140 isc_stdtime_t publish = 0, activate = 0, revoke = 0;
141 isc_stdtime_t inactive = 0, delete = 0;
142 isc_stdtime_t now;
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;
151 unsigned char c;
153 if (argc == 1)
154 usage();
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)
167 switch (ch) {
168 case '3':
169 use_nsec3 = ISC_TRUE;
170 break;
171 case 'a':
172 algname = isc_commandline_argument;
173 break;
174 case 'C':
175 oldstyle = ISC_TRUE;
176 break;
177 case 'c':
178 classname = isc_commandline_argument;
179 break;
180 case 'E':
181 engine = isc_commandline_argument;
182 break;
183 case 'f':
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;
189 else
190 fatal("unknown flag '%s'",
191 isc_commandline_argument);
192 break;
193 case 'K':
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));
199 break;
200 case 'k':
201 options |= DST_TYPE_KEY;
202 break;
203 case 'l':
204 label = isc_mem_strdup(mctx, isc_commandline_argument);
205 break;
206 case 'n':
207 nametype = isc_commandline_argument;
208 break;
209 case 'p':
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 "
213 "[0..255]");
214 break;
215 case 't':
216 type = isc_commandline_argument;
217 break;
218 case 'v':
219 verbose = strtol(isc_commandline_argument, &endp, 0);
220 if (*endp != '\0')
221 fatal("-v must be followed by a number");
222 break;
223 case 'G':
224 genonly = ISC_TRUE;
225 break;
226 case 'P':
227 if (setpub || unsetpub)
228 fatal("-P specified more than once");
230 if (strcasecmp(isc_commandline_argument, "none")) {
231 setpub = ISC_TRUE;
232 publish = strtotime(isc_commandline_argument,
233 now, now);
234 } else {
235 unsetpub = ISC_TRUE;
237 break;
238 case 'A':
239 if (setact || unsetact)
240 fatal("-A specified more than once");
242 if (strcasecmp(isc_commandline_argument, "none")) {
243 setact = ISC_TRUE;
244 activate = strtotime(isc_commandline_argument,
245 now, now);
246 } else {
247 unsetact = ISC_TRUE;
249 break;
250 case 'R':
251 if (setrev || unsetrev)
252 fatal("-R specified more than once");
254 if (strcasecmp(isc_commandline_argument, "none")) {
255 setrev = ISC_TRUE;
256 revoke = strtotime(isc_commandline_argument,
257 now, now);
258 } else {
259 unsetrev = ISC_TRUE;
261 break;
262 case 'I':
263 if (setinact || unsetinact)
264 fatal("-I specified more than once");
266 if (strcasecmp(isc_commandline_argument, "none")) {
267 setinact = ISC_TRUE;
268 inactive = strtotime(isc_commandline_argument,
269 now, now);
270 } else {
271 unsetinact = ISC_TRUE;
273 break;
274 case 'D':
275 if (setdel || unsetdel)
276 fatal("-D specified more than once");
278 if (strcasecmp(isc_commandline_argument, "none")) {
279 setdel = ISC_TRUE;
280 delete = strtotime(isc_commandline_argument,
281 now, now);
282 } else {
283 unsetdel = ISC_TRUE;
285 break;
286 case 'F':
287 /* Reserved for FIPS mode */
288 /* FALLTHROUGH */
289 case '?':
290 if (isc_commandline_option != '?')
291 fprintf(stderr, "%s: invalid argument -%c\n",
292 program, isc_commandline_option);
293 /* FALLTHROUGH */
294 case 'h':
295 usage();
297 default:
298 fprintf(stderr, "%s: unhandled option -%c\n",
299 program, isc_commandline_option);
300 exit(1);
304 if (ectx == NULL)
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);
314 if (label == NULL)
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) {
323 char *l;
324 int len;
326 len = strlen(label) + strlen(engine) + 2;
327 l = isc_mem_allocate(mctx, len);
328 if (l == NULL)
329 fatal("cannot allocate memory");
330 snprintf(l, len, "%s:%s", engine, label);
331 isc_mem_free(mctx, label);
332 label = l;
335 if (algname == NULL) {
336 if (use_nsec3)
337 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
338 else
339 algname = strdup(DEFAULT_ALGORITHM);
340 if (verbose > 0)
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");
349 return (1);
350 } else {
351 r.base = algname;
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;
360 if (use_nsec3 &&
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)
376 /* nothing */;
377 else
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;
393 else
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)
401 directory = ".";
403 if ((options & DST_TYPE_KEY) != 0) /* KEY */
404 flags |= signatory;
405 else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
406 flags |= kskflag;
407 flags |= revflag;
410 if (protocol == -1)
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",
424 algname);
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));
450 /* NOTREACHED */
451 exit(-1);
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".
460 if (!oldstyle) {
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");
466 if (setpub)
467 dst_key_settime(key, DST_TIME_PUBLISH, publish);
468 else if (setact)
469 dst_key_settime(key, DST_TIME_PUBLISH, activate);
470 else if (!genonly && !unsetpub)
471 dst_key_settime(key, DST_TIME_PUBLISH, now);
473 if (setact)
474 dst_key_settime(key, DST_TIME_ACTIVATE, activate);
475 else if (!genonly && !unsetact)
476 dst_key_settime(key, DST_TIME_ACTIVATE, now);
478 if (setrev) {
479 if (kskflag == 0)
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",
484 program);
485 dst_key_settime(key, DST_TIME_REVOKE, revoke);
488 if (setinact)
489 dst_key_settime(key, DST_TIME_INACTIVE, inactive);
491 if (setdel)
492 dst_key_settime(key, DST_TIME_DELETE, delete);
493 } else {
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);
530 dst_key_free(&key);
532 cleanup_logging(&log);
533 cleanup_entropy(&ectx);
534 dst_lib_destroy();
535 dns_name_destroy();
536 if (verbose > 10)
537 isc_mem_stats(mctx, stdout);
538 isc_mem_free(mctx, label);
539 isc_mem_destroy(&mctx);
541 return (0);