2 * Copyright (C) 2009-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-settime.c,v 1.28.16.3 2011/06/02 20:24:11 each Exp $ */
29 #include <isc/buffer.h>
30 #include <isc/commandline.h>
31 #include <isc/entropy.h>
35 #include <isc/print.h>
36 #include <isc/string.h>
39 #include <dns/keyvalues.h>
40 #include <dns/result.h>
45 #include "dnssectool.h"
47 const char *program = "dnssec-settime";
50 static isc_mem_t *mctx = NULL;
52 ISC_PLATFORM_NORETURN_PRE static void
53 usage(void) ISC_PLATFORM_NORETURN_POST;
57 fprintf(stderr, "Usage:\n");
58 fprintf(stderr, " %s [options] keyfile\n\n", program);
59 fprintf(stderr, "Version: %s\n", VERSION);
60 fprintf(stderr, "General options:\n");
62 fprintf(stderr, " -E engine: specify OpenSSL engine "
63 "(default \"pkcs11\")\n");
65 fprintf(stderr, " -E engine: specify OpenSSL engine\n");
67 fprintf(stderr, " -f: force update of old-style "
69 fprintf(stderr, " -K directory: set key file location\n");
70 fprintf(stderr, " -v level: set level of verbosity\n");
71 fprintf(stderr, " -h: help\n");
72 fprintf(stderr, "Timing options:\n");
73 fprintf(stderr, " -P date/[+-]offset/none: set/unset key "
74 "publication date\n");
75 fprintf(stderr, " -A date/[+-]offset/none: set/unset key "
77 fprintf(stderr, " -R date/[+-]offset/none: set/unset key "
79 fprintf(stderr, " -I date/[+-]offset/none: set/unset key "
80 "inactivation date\n");
81 fprintf(stderr, " -D date/[+-]offset/none: set/unset key "
83 fprintf(stderr, "Printing options:\n");
84 fprintf(stderr, " -p C/P/A/R/I/D/all: print a particular time "
86 fprintf(stderr, " -u: print times in unix epoch "
88 fprintf(stderr, "Output:\n");
89 fprintf(stderr, " K<name>+<alg>+<new id>.key, "
90 "K<name>+<alg>+<new id>.private\n");
96 printtime(dst_key_t *key, int type, const char *tag, isc_boolean_t epoch,
100 const char *output = NULL;
104 fprintf(stream, "%s: ", tag);
106 result = dst_key_gettime(key, type, &when);
107 if (result == ISC_R_NOTFOUND) {
108 fprintf(stream, "UNSET\n");
110 fprintf(stream, "%d\n", (int) when);
113 output = ctime(&time);
114 fprintf(stream, "%s", output);
119 main(int argc, char **argv) {
122 const char *engine = "pkcs11";
124 const char *engine = NULL;
126 char *filename = NULL, *directory = NULL;
128 char keystr[DST_KEY_FORMATSIZE];
131 isc_entropy_t *ectx = NULL;
132 const char *predecessor = NULL;
133 dst_key_t *prevkey = NULL;
134 dst_key_t *key = NULL;
136 dns_name_t *name = NULL;
137 dns_secalg_t alg = 0;
138 unsigned int size = 0;
139 isc_uint16_t flags = 0;
142 isc_stdtime_t pub = 0, act = 0, rev = 0, inact = 0, del = 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 printcreate = ISC_FALSE, printpub = ISC_FALSE;
150 isc_boolean_t printact = ISC_FALSE, printrev = ISC_FALSE;
151 isc_boolean_t printinact = ISC_FALSE, printdel = ISC_FALSE;
152 isc_boolean_t force = ISC_FALSE;
153 isc_boolean_t epoch = ISC_FALSE;
154 isc_boolean_t changed = ISC_FALSE;
155 isc_log_t *log = NULL;
160 result = isc_mem_create(0, 0, &mctx);
161 if (result != ISC_R_SUCCESS)
162 fatal("Out of memory");
164 setup_logging(verbose, mctx, &log);
166 dns_result_register();
168 isc_commandline_errprint = ISC_FALSE;
170 isc_stdtime_get(&now);
172 #define CMDLINE_FLAGS "A:D:E:fhI:i:K:P:p:R:S:uv:"
173 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
176 engine = isc_commandline_argument;
182 p = isc_commandline_argument;
183 if (!strcasecmp(p, "all")) {
184 printcreate = ISC_TRUE;
188 printinact = ISC_TRUE;
196 printcreate = ISC_TRUE;
208 printinact = ISC_TRUE;
219 } while (*p != '\0');
226 * We don't have to copy it here, but do it to
227 * simplify cleanup later
229 directory = isc_mem_strdup(mctx,
230 isc_commandline_argument);
231 if (directory == NULL) {
232 fatal("Failed to allocate memory for "
237 verbose = strtol(isc_commandline_argument, &endp, 0);
239 fatal("-v must be followed by a number");
242 if (setpub || unsetpub)
243 fatal("-P specified more than once");
246 if (!strcasecmp(isc_commandline_argument, "none")) {
250 pub = strtotime(isc_commandline_argument,
255 if (setact || unsetact)
256 fatal("-A specified more than once");
259 if (!strcasecmp(isc_commandline_argument, "none")) {
263 act = strtotime(isc_commandline_argument,
268 if (setrev || unsetrev)
269 fatal("-R specified more than once");
272 if (!strcasecmp(isc_commandline_argument, "none")) {
276 rev = strtotime(isc_commandline_argument,
281 if (setinact || unsetinact)
282 fatal("-I specified more than once");
285 if (!strcasecmp(isc_commandline_argument, "none")) {
286 unsetinact = ISC_TRUE;
289 inact = strtotime(isc_commandline_argument,
294 if (setdel || unsetdel)
295 fatal("-D specified more than once");
298 if (!strcasecmp(isc_commandline_argument, "none")) {
302 del = strtotime(isc_commandline_argument,
307 predecessor = isc_commandline_argument;
310 prepub = strtottl(isc_commandline_argument);
313 if (isc_commandline_option != '?')
314 fprintf(stderr, "%s: invalid argument -%c\n",
315 program, isc_commandline_option);
321 fprintf(stderr, "%s: unhandled option -%c\n",
322 program, isc_commandline_option);
327 if (argc < isc_commandline_index + 1 ||
328 argv[isc_commandline_index] == NULL)
329 fatal("The key file name was not specified");
330 if (argc > isc_commandline_index + 1)
331 fatal("Extraneous arguments");
334 setup_entropy(mctx, NULL, &ectx);
335 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
336 if (result != ISC_R_SUCCESS)
337 fatal("Could not initialize hash");
338 result = dst_lib_init2(mctx, ectx, engine,
339 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
340 if (result != ISC_R_SUCCESS)
341 fatal("Could not initialize dst: %s",
342 isc_result_totext(result));
343 isc_entropy_stopcallbacksources(ectx);
345 if (predecessor != NULL) {
346 char keystr[DST_KEY_FORMATSIZE];
351 prepub = (30 * 86400);
353 if (setpub || unsetpub)
354 fatal("-S and -P cannot be used together");
355 if (setact || unsetact)
356 fatal("-S and -A cannot be used together");
358 result = dst_key_fromnamedfile(predecessor, directory,
362 if (result != ISC_R_SUCCESS)
363 fatal("Invalid keyfile %s: %s",
364 filename, isc_result_totext(result));
365 if (!dst_key_isprivate(prevkey))
366 fatal("%s is not a private key", filename);
368 name = dst_key_name(prevkey);
369 alg = dst_key_alg(prevkey);
370 size = dst_key_size(prevkey);
371 flags = dst_key_flags(prevkey);
373 dst_key_format(prevkey, keystr, sizeof(keystr));
374 dst_key_getprivateformat(prevkey, &major, &minor);
375 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
376 fatal("Predecessor has incompatible format "
377 "version %d.%d\n\t", major, minor);
379 result = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
380 if (result != ISC_R_SUCCESS)
381 fatal("Predecessor has no activation date. "
382 "You must set one before\n\t"
383 "generating a successor.");
385 result = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &act);
386 if (result != ISC_R_SUCCESS)
387 fatal("Predecessor has no inactivation date. "
388 "You must set one before\n\t"
389 "generating a successor.");
392 if (pub < now && prepub != 0)
393 fatal("Predecessor will become inactive before the\n\t"
394 "prepublication period ends. Either change "
395 "its inactivation date,\n\t"
396 "or use the -i option to set a shorter "
397 "prepublication interval.");
399 result = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
400 if (result != ISC_R_SUCCESS)
401 fprintf(stderr, "%s: WARNING: Predecessor has no "
403 "it will remain in the zone "
404 "indefinitely after rollover.\n",
407 changed = setpub = setact = ISC_TRUE;
408 dst_key_free(&prevkey);
414 if (setpub && setact && (act - prepub) < pub)
415 fatal("Activation and publication dates "
416 "are closer together than the\n\t"
417 "prepublication interval.");
419 if (setpub && !setact) {
422 } else if (setact && !setpub) {
427 if ((act - prepub) < now)
428 fatal("Time until activation is shorter "
429 "than the\n\tprepublication interval.");
433 if (directory != NULL) {
434 filename = argv[isc_commandline_index];
436 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
437 &directory, &filename);
438 if (result != ISC_R_SUCCESS)
439 fatal("cannot process filename %s: %s",
440 argv[isc_commandline_index],
441 isc_result_totext(result));
444 result = dst_key_fromnamedfile(filename, directory,
445 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
447 if (result != ISC_R_SUCCESS)
448 fatal("Invalid keyfile %s: %s",
449 filename, isc_result_totext(result));
451 if (!dst_key_isprivate(key))
452 fatal("%s is not a private key", filename);
454 dst_key_format(key, keystr, sizeof(keystr));
456 if (predecessor != NULL) {
457 if (!dns_name_equal(name, dst_key_name(key)))
458 fatal("Key name mismatch");
459 if (alg != dst_key_alg(key))
460 fatal("Key algorithm mismatch");
461 if (size != dst_key_size(key))
462 fatal("Key size mismatch");
463 if (flags != dst_key_flags(key))
464 fatal("Key flags mismatch");
470 check_keyversion(key, keystr);
473 fprintf(stderr, "%s: %s\n", program, keystr);
479 dst_key_settime(key, DST_TIME_PUBLISH, pub);
481 dst_key_unsettime(key, DST_TIME_PUBLISH);
484 dst_key_settime(key, DST_TIME_ACTIVATE, act);
486 dst_key_unsettime(key, DST_TIME_ACTIVATE);
489 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
490 fprintf(stderr, "%s: warning: Key %s is already "
491 "revoked; changing the revocation date "
492 "will not affect this.\n",
494 if ((dst_key_flags(key) & DNS_KEYFLAG_KSK) == 0)
495 fprintf(stderr, "%s: warning: Key %s is not flagged as "
496 "a KSK, but -R was used. Revoking a "
497 "ZSK is legal, but undefined.\n",
499 dst_key_settime(key, DST_TIME_REVOKE, rev);
500 } else if (unsetrev) {
501 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
502 fprintf(stderr, "%s: warning: Key %s is already "
503 "revoked; removing the revocation date "
504 "will not affect this.\n",
506 dst_key_unsettime(key, DST_TIME_REVOKE);
510 dst_key_settime(key, DST_TIME_INACTIVE, inact);
512 dst_key_unsettime(key, DST_TIME_INACTIVE);
515 dst_key_settime(key, DST_TIME_DELETE, del);
517 dst_key_unsettime(key, DST_TIME_DELETE);
520 * No metadata changes were made but we're forcing an upgrade
521 * to the new format anyway: use "-P now -A now" as the default
523 if (force && !changed) {
524 dst_key_settime(key, DST_TIME_PUBLISH, now);
525 dst_key_settime(key, DST_TIME_ACTIVATE, now);
530 * Print out time values, if -p was used.
533 printtime(key, DST_TIME_CREATED, "Created", epoch, stdout);
536 printtime(key, DST_TIME_PUBLISH, "Publish", epoch, stdout);
539 printtime(key, DST_TIME_ACTIVATE, "Activate", epoch, stdout);
542 printtime(key, DST_TIME_REVOKE, "Revoke", epoch, stdout);
545 printtime(key, DST_TIME_INACTIVE, "Inactive", epoch, stdout);
548 printtime(key, DST_TIME_DELETE, "Delete", epoch, stdout);
551 isc_buffer_init(&buf, newname, sizeof(newname));
552 result = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory,
554 if (result != ISC_R_SUCCESS) {
555 fatal("Failed to build public key filename: %s",
556 isc_result_totext(result));
559 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
561 if (result != ISC_R_SUCCESS) {
562 dst_key_format(key, keystr, sizeof(keystr));
563 fatal("Failed to write key %s: %s", keystr,
564 isc_result_totext(result));
567 printf("%s\n", newname);
569 isc_buffer_clear(&buf);
570 result = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory,
572 if (result != ISC_R_SUCCESS) {
573 fatal("Failed to build private key filename: %s",
574 isc_result_totext(result));
576 printf("%s\n", newname);
582 cleanup_entropy(&ectx);
584 isc_mem_stats(mctx, stdout);
585 cleanup_logging(&log);
586 isc_mem_free(mctx, directory);
587 isc_mem_destroy(&mctx);