2 * Copyright (C) 2009-2013 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.32 2011/06/02 20:24:45 each Exp $ */
28 #include <isc/buffer.h>
29 #include <isc/commandline.h>
30 #include <isc/entropy.h>
34 #include <isc/print.h>
35 #include <isc/string.h>
38 #include <dns/keyvalues.h>
39 #include <dns/result.h>
44 #include "dnssectool.h"
46 const char *program = "dnssec-settime";
49 static isc_mem_t *mctx = NULL;
51 ISC_PLATFORM_NORETURN_PRE static void
52 usage(void) ISC_PLATFORM_NORETURN_POST;
56 fprintf(stderr, "Usage:\n");
57 fprintf(stderr, " %s [options] keyfile\n\n", program);
58 fprintf(stderr, "Version: %s\n", VERSION);
59 fprintf(stderr, "General options:\n");
61 fprintf(stderr, " -E engine: specify OpenSSL engine "
62 "(default \"pkcs11\")\n");
64 fprintf(stderr, " -E engine: specify OpenSSL engine\n");
66 fprintf(stderr, " -f: force update of old-style "
68 fprintf(stderr, " -K directory: set key file location\n");
69 fprintf(stderr, " -L ttl: set default key TTL\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;
143 isc_stdtime_t pub = 0, act = 0, rev = 0, inact = 0, del = 0;
144 isc_stdtime_t prevact = 0, previnact = 0, prevdel = 0;
145 isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
146 isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
147 isc_boolean_t setdel = ISC_FALSE, setttl = ISC_FALSE;
148 isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
149 isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
150 isc_boolean_t unsetdel = ISC_FALSE;
151 isc_boolean_t printcreate = ISC_FALSE, printpub = ISC_FALSE;
152 isc_boolean_t printact = ISC_FALSE, printrev = ISC_FALSE;
153 isc_boolean_t printinact = ISC_FALSE, printdel = ISC_FALSE;
154 isc_boolean_t force = ISC_FALSE;
155 isc_boolean_t epoch = ISC_FALSE;
156 isc_boolean_t changed = ISC_FALSE;
157 isc_log_t *log = NULL;
162 result = isc_mem_create(0, 0, &mctx);
163 if (result != ISC_R_SUCCESS)
164 fatal("Out of memory");
166 setup_logging(verbose, mctx, &log);
168 dns_result_register();
170 isc_commandline_errprint = ISC_FALSE;
172 isc_stdtime_get(&now);
174 #define CMDLINE_FLAGS "A:D:E:fhI:i:K:L:P:p:R:S:uv:"
175 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
178 engine = isc_commandline_argument;
184 p = isc_commandline_argument;
185 if (!strcasecmp(p, "all")) {
186 printcreate = ISC_TRUE;
190 printinact = ISC_TRUE;
198 printcreate = ISC_TRUE;
210 printinact = ISC_TRUE;
221 } while (*p != '\0');
228 * We don't have to copy it here, but do it to
229 * simplify cleanup later
231 directory = isc_mem_strdup(mctx,
232 isc_commandline_argument);
233 if (directory == NULL) {
234 fatal("Failed to allocate memory for "
239 if (strcmp(isc_commandline_argument, "none") == 0)
242 ttl = strtottl(isc_commandline_argument);
246 verbose = strtol(isc_commandline_argument, &endp, 0);
248 fatal("-v must be followed by a number");
251 if (setpub || unsetpub)
252 fatal("-P specified more than once");
255 if (!strcasecmp(isc_commandline_argument, "none")) {
259 pub = strtotime(isc_commandline_argument,
264 if (setact || unsetact)
265 fatal("-A specified more than once");
268 if (!strcasecmp(isc_commandline_argument, "none")) {
272 act = strtotime(isc_commandline_argument,
277 if (setrev || unsetrev)
278 fatal("-R specified more than once");
281 if (!strcasecmp(isc_commandline_argument, "none")) {
285 rev = strtotime(isc_commandline_argument,
290 if (setinact || unsetinact)
291 fatal("-I specified more than once");
294 if (!strcasecmp(isc_commandline_argument, "none")) {
295 unsetinact = ISC_TRUE;
298 inact = strtotime(isc_commandline_argument,
303 if (setdel || unsetdel)
304 fatal("-D specified more than once");
307 if (!strcasecmp(isc_commandline_argument, "none")) {
311 del = strtotime(isc_commandline_argument,
316 predecessor = isc_commandline_argument;
319 prepub = strtottl(isc_commandline_argument);
322 if (isc_commandline_option != '?')
323 fprintf(stderr, "%s: invalid argument -%c\n",
324 program, isc_commandline_option);
330 fprintf(stderr, "%s: unhandled option -%c\n",
331 program, isc_commandline_option);
336 if (argc < isc_commandline_index + 1 ||
337 argv[isc_commandline_index] == NULL)
338 fatal("The key file name was not specified");
339 if (argc > isc_commandline_index + 1)
340 fatal("Extraneous arguments");
343 setup_entropy(mctx, NULL, &ectx);
344 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
345 if (result != ISC_R_SUCCESS)
346 fatal("Could not initialize hash");
347 result = dst_lib_init2(mctx, ectx, engine,
348 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
349 if (result != ISC_R_SUCCESS)
350 fatal("Could not initialize dst: %s",
351 isc_result_totext(result));
352 isc_entropy_stopcallbacksources(ectx);
354 if (predecessor != NULL) {
355 char keystr[DST_KEY_FORMATSIZE];
359 prepub = (30 * 86400);
361 if (setpub || unsetpub)
362 fatal("-S and -P cannot be used together");
363 if (setact || unsetact)
364 fatal("-S and -A cannot be used together");
366 result = dst_key_fromnamedfile(predecessor, directory,
370 if (result != ISC_R_SUCCESS)
371 fatal("Invalid keyfile %s: %s",
372 filename, isc_result_totext(result));
373 if (!dst_key_isprivate(prevkey))
374 fatal("%s is not a private key", filename);
376 name = dst_key_name(prevkey);
377 alg = dst_key_alg(prevkey);
378 size = dst_key_size(prevkey);
379 flags = dst_key_flags(prevkey);
381 dst_key_format(prevkey, keystr, sizeof(keystr));
382 dst_key_getprivateformat(prevkey, &major, &minor);
383 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
384 fatal("Predecessor has incompatible format "
385 "version %d.%d\n\t", major, minor);
387 result = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &prevact);
388 if (result != ISC_R_SUCCESS)
389 fatal("Predecessor has no activation date. "
390 "You must set one before\n\t"
391 "generating a successor.");
393 result = dst_key_gettime(prevkey, DST_TIME_INACTIVE,
395 if (result != ISC_R_SUCCESS)
396 fatal("Predecessor has no inactivation date. "
397 "You must set one before\n\t"
398 "generating a successor.");
400 pub = prevact - prepub;
401 if (pub < now && prepub != 0)
402 fatal("Predecessor will become inactive before the\n\t"
403 "prepublication period ends. Either change "
404 "its inactivation date,\n\t"
405 "or use the -i option to set a shorter "
406 "prepublication interval.");
408 result = dst_key_gettime(prevkey, DST_TIME_DELETE, &prevdel);
409 if (result != ISC_R_SUCCESS)
410 fprintf(stderr, "%s: warning: Predecessor has no "
412 "it will remain in the zone "
413 "indefinitely after rollover.\n",
415 else if (prevdel < previnact)
416 fprintf(stderr, "%s: warning: Predecessor is "
417 "scheduled to be deleted\n\t"
418 "before it is scheduled to be "
419 "inactive.\n", program);
421 changed = setpub = setact = ISC_TRUE;
422 dst_key_free(&prevkey);
428 if (setpub && setact && (act - prepub) < pub)
429 fatal("Activation and publication dates "
430 "are closer together than the\n\t"
431 "prepublication interval.");
433 if (setpub && !setact) {
436 } else if (setact && !setpub) {
441 if ((act - prepub) < now)
442 fatal("Time until activation is shorter "
443 "than the\n\tprepublication interval.");
447 if (directory != NULL) {
448 filename = argv[isc_commandline_index];
450 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
451 &directory, &filename);
452 if (result != ISC_R_SUCCESS)
453 fatal("cannot process filename %s: %s",
454 argv[isc_commandline_index],
455 isc_result_totext(result));
458 result = dst_key_fromnamedfile(filename, directory,
459 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
461 if (result != ISC_R_SUCCESS)
462 fatal("Invalid keyfile %s: %s",
463 filename, isc_result_totext(result));
465 if (!dst_key_isprivate(key))
466 fatal("%s is not a private key", filename);
468 dst_key_format(key, keystr, sizeof(keystr));
470 if (predecessor != NULL) {
471 if (!dns_name_equal(name, dst_key_name(key)))
472 fatal("Key name mismatch");
473 if (alg != dst_key_alg(key))
474 fatal("Key algorithm mismatch");
475 if (size != dst_key_size(key))
476 fatal("Key size mismatch");
477 if (flags != dst_key_flags(key))
478 fatal("Key flags mismatch");
481 prevdel = previnact = 0;
482 if ((setdel && setinact && del < inact) ||
483 (dst_key_gettime(key, DST_TIME_INACTIVE,
484 &previnact) == ISC_R_SUCCESS &&
485 setdel && !setinact && del < previnact) ||
486 (dst_key_gettime(key, DST_TIME_DELETE,
487 &prevdel) == ISC_R_SUCCESS &&
488 setinact && !setdel && prevdel < inact) ||
489 (!setdel && !setinact && prevdel < previnact))
490 fprintf(stderr, "%s: warning: Key is scheduled to "
491 "be deleted before it is\n\t"
492 "scheduled to be inactive.\n",
498 check_keyversion(key, keystr);
501 fprintf(stderr, "%s: %s\n", program, keystr);
507 dst_key_settime(key, DST_TIME_PUBLISH, pub);
509 dst_key_unsettime(key, DST_TIME_PUBLISH);
512 dst_key_settime(key, DST_TIME_ACTIVATE, act);
514 dst_key_unsettime(key, DST_TIME_ACTIVATE);
517 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
518 fprintf(stderr, "%s: warning: Key %s is already "
519 "revoked; changing the revocation date "
520 "will not affect this.\n",
522 if ((dst_key_flags(key) & DNS_KEYFLAG_KSK) == 0)
523 fprintf(stderr, "%s: warning: Key %s is not flagged as "
524 "a KSK, but -R was used. Revoking a "
525 "ZSK is legal, but undefined.\n",
527 dst_key_settime(key, DST_TIME_REVOKE, rev);
528 } else if (unsetrev) {
529 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
530 fprintf(stderr, "%s: warning: Key %s is already "
531 "revoked; removing the revocation date "
532 "will not affect this.\n",
534 dst_key_unsettime(key, DST_TIME_REVOKE);
538 dst_key_settime(key, DST_TIME_INACTIVE, inact);
540 dst_key_unsettime(key, DST_TIME_INACTIVE);
543 dst_key_settime(key, DST_TIME_DELETE, del);
545 dst_key_unsettime(key, DST_TIME_DELETE);
548 dst_key_setttl(key, ttl);
551 * No metadata changes were made but we're forcing an upgrade
552 * to the new format anyway: use "-P now -A now" as the default
554 if (force && !changed) {
555 dst_key_settime(key, DST_TIME_PUBLISH, now);
556 dst_key_settime(key, DST_TIME_ACTIVATE, now);
560 if (!changed && setttl)
564 * Print out time values, if -p was used.
567 printtime(key, DST_TIME_CREATED, "Created", epoch, stdout);
570 printtime(key, DST_TIME_PUBLISH, "Publish", epoch, stdout);
573 printtime(key, DST_TIME_ACTIVATE, "Activate", epoch, stdout);
576 printtime(key, DST_TIME_REVOKE, "Revoke", epoch, stdout);
579 printtime(key, DST_TIME_INACTIVE, "Inactive", epoch, stdout);
582 printtime(key, DST_TIME_DELETE, "Delete", epoch, stdout);
585 isc_buffer_init(&buf, newname, sizeof(newname));
586 result = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory,
588 if (result != ISC_R_SUCCESS) {
589 fatal("Failed to build public key filename: %s",
590 isc_result_totext(result));
593 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
595 if (result != ISC_R_SUCCESS) {
596 dst_key_format(key, keystr, sizeof(keystr));
597 fatal("Failed to write key %s: %s", keystr,
598 isc_result_totext(result));
601 printf("%s\n", newname);
603 isc_buffer_clear(&buf);
604 result = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory,
606 if (result != ISC_R_SUCCESS) {
607 fatal("Failed to build private key filename: %s",
608 isc_result_totext(result));
610 printf("%s\n", newname);
616 cleanup_entropy(&ectx);
618 isc_mem_stats(mctx, stdout);
619 cleanup_logging(&log);
620 isc_mem_free(mctx, directory);
621 isc_mem_destroy(&mctx);