2 * Copyright (C) 2009-2015 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.
26 #include <isc/buffer.h>
27 #include <isc/commandline.h>
28 #include <isc/entropy.h>
32 #include <isc/print.h>
33 #include <isc/string.h>
36 #include <dns/keyvalues.h>
37 #include <dns/result.h>
42 #include "dnssectool.h"
44 const char *program = "dnssec-settime";
47 static isc_mem_t *mctx = NULL;
49 ISC_PLATFORM_NORETURN_PRE static void
50 usage(void) ISC_PLATFORM_NORETURN_POST;
54 fprintf(stderr, "Usage:\n");
55 fprintf(stderr, " %s [options] keyfile\n\n", program);
56 fprintf(stderr, "Version: %s\n", VERSION);
57 fprintf(stderr, "General options:\n");
59 fprintf(stderr, " -E engine: specify OpenSSL engine "
60 "(default \"pkcs11\")\n");
62 fprintf(stderr, " -E engine: specify OpenSSL engine\n");
64 fprintf(stderr, " -f: force update of old-style "
66 fprintf(stderr, " -K directory: set key file location\n");
67 fprintf(stderr, " -L ttl: set default key TTL\n");
68 fprintf(stderr, " -v level: set level of verbosity\n");
69 fprintf(stderr, " -V: print version information\n");
70 fprintf(stderr, " -h: help\n");
71 fprintf(stderr, "Timing options:\n");
72 fprintf(stderr, " -P date/[+-]offset/none: set/unset key "
73 "publication date\n");
74 fprintf(stderr, " -A date/[+-]offset/none: set/unset key "
76 fprintf(stderr, " -R date/[+-]offset/none: set/unset key "
78 fprintf(stderr, " -I date/[+-]offset/none: set/unset key "
79 "inactivation date\n");
80 fprintf(stderr, " -D date/[+-]offset/none: set/unset key "
82 fprintf(stderr, "Printing options:\n");
83 fprintf(stderr, " -p C/P/A/R/I/D/all: print a particular time "
85 fprintf(stderr, " -u: print times in unix epoch "
87 fprintf(stderr, "Output:\n");
88 fprintf(stderr, " K<name>+<alg>+<new id>.key, "
89 "K<name>+<alg>+<new id>.private\n");
95 printtime(dst_key_t *key, int type, const char *tag, isc_boolean_t epoch,
99 const char *output = NULL;
103 fprintf(stream, "%s: ", tag);
105 result = dst_key_gettime(key, type, &when);
106 if (result == ISC_R_NOTFOUND) {
107 fprintf(stream, "UNSET\n");
109 fprintf(stream, "%d\n", (int) when);
112 output = ctime(&timet);
113 fprintf(stream, "%s", output);
118 main(int argc, char **argv) {
121 const char *engine = "pkcs11";
123 const char *engine = NULL;
125 char *filename = NULL, *directory = NULL;
127 char keystr[DST_KEY_FORMATSIZE];
130 isc_entropy_t *ectx = NULL;
131 const char *predecessor = NULL;
132 dst_key_t *prevkey = NULL;
133 dst_key_t *key = NULL;
135 dns_name_t *name = NULL;
136 dns_secalg_t alg = 0;
137 unsigned int size = 0;
138 isc_uint16_t flags = 0;
142 isc_stdtime_t pub = 0, act = 0, rev = 0, inact = 0, del = 0;
143 isc_stdtime_t prevact = 0, previnact = 0, prevdel = 0;
144 isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
145 isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
146 isc_boolean_t setdel = ISC_FALSE, setttl = ISC_FALSE;
147 isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
148 isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
149 isc_boolean_t unsetdel = ISC_FALSE;
150 isc_boolean_t printcreate = ISC_FALSE, printpub = ISC_FALSE;
151 isc_boolean_t printact = ISC_FALSE, printrev = ISC_FALSE;
152 isc_boolean_t printinact = ISC_FALSE, printdel = ISC_FALSE;
153 isc_boolean_t force = ISC_FALSE;
154 isc_boolean_t epoch = ISC_FALSE;
155 isc_boolean_t changed = ISC_FALSE;
156 isc_log_t *log = NULL;
161 result = isc_mem_create(0, 0, &mctx);
162 if (result != ISC_R_SUCCESS)
163 fatal("Out of memory");
165 setup_logging(mctx, &log);
167 dns_result_register();
169 isc_commandline_errprint = ISC_FALSE;
171 isc_stdtime_get(&now);
173 #define CMDLINE_FLAGS "A:D:E:fhI:i:K:L:P:p:R:S:uv:V"
174 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
177 engine = isc_commandline_argument;
183 p = isc_commandline_argument;
184 if (!strcasecmp(p, "all")) {
185 printcreate = ISC_TRUE;
189 printinact = ISC_TRUE;
197 printcreate = ISC_TRUE;
209 printinact = ISC_TRUE;
220 } while (*p != '\0');
227 * We don't have to copy it here, but do it to
228 * simplify cleanup later
230 directory = isc_mem_strdup(mctx,
231 isc_commandline_argument);
232 if (directory == NULL) {
233 fatal("Failed to allocate memory for "
238 ttl = strtottl(isc_commandline_argument);
242 verbose = strtol(isc_commandline_argument, &endp, 0);
244 fatal("-v must be followed by a number");
247 if (setpub || unsetpub)
248 fatal("-P specified more than once");
251 pub = strtotime(isc_commandline_argument,
256 if (setact || unsetact)
257 fatal("-A specified more than once");
260 act = strtotime(isc_commandline_argument,
265 if (setrev || unsetrev)
266 fatal("-R specified more than once");
269 rev = strtotime(isc_commandline_argument,
274 if (setinact || unsetinact)
275 fatal("-I specified more than once");
278 inact = strtotime(isc_commandline_argument,
279 now, now, &setinact);
280 unsetinact = !setinact;
283 if (setdel || unsetdel)
284 fatal("-D specified more than once");
287 del = strtotime(isc_commandline_argument,
292 predecessor = isc_commandline_argument;
295 prepub = strtottl(isc_commandline_argument);
298 if (isc_commandline_option != '?')
299 fprintf(stderr, "%s: invalid argument -%c\n",
300 program, isc_commandline_option);
303 /* Does not return. */
307 /* Does not return. */
311 fprintf(stderr, "%s: unhandled option -%c\n",
312 program, isc_commandline_option);
317 if (argc < isc_commandline_index + 1 ||
318 argv[isc_commandline_index] == NULL)
319 fatal("The key file name was not specified");
320 if (argc > isc_commandline_index + 1)
321 fatal("Extraneous arguments");
324 setup_entropy(mctx, NULL, &ectx);
325 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
326 if (result != ISC_R_SUCCESS)
327 fatal("Could not initialize hash");
328 result = dst_lib_init2(mctx, ectx, engine,
329 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
330 if (result != ISC_R_SUCCESS)
331 fatal("Could not initialize dst: %s",
332 isc_result_totext(result));
333 isc_entropy_stopcallbacksources(ectx);
335 if (predecessor != NULL) {
339 prepub = (30 * 86400);
341 if (setpub || unsetpub)
342 fatal("-S and -P cannot be used together");
343 if (setact || unsetact)
344 fatal("-S and -A cannot be used together");
346 result = dst_key_fromnamedfile(predecessor, directory,
350 if (result != ISC_R_SUCCESS)
351 fatal("Invalid keyfile %s: %s",
352 filename, isc_result_totext(result));
353 if (!dst_key_isprivate(prevkey) && !dst_key_isexternal(prevkey))
354 fatal("%s is not a private key", filename);
356 name = dst_key_name(prevkey);
357 alg = dst_key_alg(prevkey);
358 size = dst_key_size(prevkey);
359 flags = dst_key_flags(prevkey);
361 dst_key_format(prevkey, keystr, sizeof(keystr));
362 dst_key_getprivateformat(prevkey, &major, &minor);
363 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
364 fatal("Predecessor has incompatible format "
365 "version %d.%d\n\t", major, minor);
367 result = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &prevact);
368 if (result != ISC_R_SUCCESS)
369 fatal("Predecessor has no activation date. "
370 "You must set one before\n\t"
371 "generating a successor.");
373 result = dst_key_gettime(prevkey, DST_TIME_INACTIVE,
375 if (result != ISC_R_SUCCESS)
376 fatal("Predecessor has no inactivation date. "
377 "You must set one before\n\t"
378 "generating a successor.");
380 pub = prevact - prepub;
381 if (pub < now && prepub != 0)
382 fatal("Predecessor will become inactive before the\n\t"
383 "prepublication period ends. Either change "
384 "its inactivation date,\n\t"
385 "or use the -i option to set a shorter "
386 "prepublication interval.");
388 result = dst_key_gettime(prevkey, DST_TIME_DELETE, &prevdel);
389 if (result != ISC_R_SUCCESS)
390 fprintf(stderr, "%s: warning: Predecessor has no "
392 "it will remain in the zone "
393 "indefinitely after rollover.\n",
395 else if (prevdel < previnact)
396 fprintf(stderr, "%s: warning: Predecessor is "
397 "scheduled to be deleted\n\t"
398 "before it is scheduled to be "
399 "inactive.\n", program);
401 changed = setpub = setact = ISC_TRUE;
407 if (setpub && setact && (act - prepub) < pub)
408 fatal("Activation and publication dates "
409 "are closer together than the\n\t"
410 "prepublication interval.");
412 if (setpub && !setact) {
415 } else if (setact && !setpub) {
420 if ((act - prepub) < now)
421 fatal("Time until activation is shorter "
422 "than the\n\tprepublication interval.");
426 if (directory != NULL) {
427 filename = argv[isc_commandline_index];
429 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
430 &directory, &filename);
431 if (result != ISC_R_SUCCESS)
432 fatal("cannot process filename %s: %s",
433 argv[isc_commandline_index],
434 isc_result_totext(result));
437 result = dst_key_fromnamedfile(filename, directory,
438 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
440 if (result != ISC_R_SUCCESS)
441 fatal("Invalid keyfile %s: %s",
442 filename, isc_result_totext(result));
444 if (!dst_key_isprivate(key) && !dst_key_isexternal(key))
445 fatal("%s is not a private key", filename);
447 dst_key_format(key, keystr, sizeof(keystr));
449 if (predecessor != NULL) {
450 if (!dns_name_equal(name, dst_key_name(key)))
451 fatal("Key name mismatch");
452 if (alg != dst_key_alg(key))
453 fatal("Key algorithm mismatch");
454 if (size != dst_key_size(key))
455 fatal("Key size mismatch");
456 if (flags != dst_key_flags(key))
457 fatal("Key flags mismatch");
460 prevdel = previnact = 0;
461 if ((setdel && setinact && del < inact) ||
462 (dst_key_gettime(key, DST_TIME_INACTIVE,
463 &previnact) == ISC_R_SUCCESS &&
464 setdel && !setinact && del < previnact) ||
465 (dst_key_gettime(key, DST_TIME_DELETE,
466 &prevdel) == ISC_R_SUCCESS &&
467 setinact && !setdel && prevdel < inact) ||
468 (!setdel && !setinact && prevdel < previnact))
469 fprintf(stderr, "%s: warning: Key is scheduled to "
470 "be deleted before it is\n\t"
471 "scheduled to be inactive.\n",
477 check_keyversion(key, keystr);
480 fprintf(stderr, "%s: %s\n", program, keystr);
486 dst_key_settime(key, DST_TIME_PUBLISH, pub);
488 dst_key_unsettime(key, DST_TIME_PUBLISH);
491 dst_key_settime(key, DST_TIME_ACTIVATE, act);
493 dst_key_unsettime(key, DST_TIME_ACTIVATE);
496 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
497 fprintf(stderr, "%s: warning: Key %s is already "
498 "revoked; changing the revocation date "
499 "will not affect this.\n",
501 if ((dst_key_flags(key) & DNS_KEYFLAG_KSK) == 0)
502 fprintf(stderr, "%s: warning: Key %s is not flagged as "
503 "a KSK, but -R was used. Revoking a "
504 "ZSK is legal, but undefined.\n",
506 dst_key_settime(key, DST_TIME_REVOKE, rev);
507 } else if (unsetrev) {
508 if ((dst_key_flags(key) & DNS_KEYFLAG_REVOKE) != 0)
509 fprintf(stderr, "%s: warning: Key %s is already "
510 "revoked; removing the revocation date "
511 "will not affect this.\n",
513 dst_key_unsettime(key, DST_TIME_REVOKE);
517 dst_key_settime(key, DST_TIME_INACTIVE, inact);
519 dst_key_unsettime(key, DST_TIME_INACTIVE);
522 dst_key_settime(key, DST_TIME_DELETE, del);
524 dst_key_unsettime(key, DST_TIME_DELETE);
527 dst_key_setttl(key, ttl);
530 * No metadata changes were made but we're forcing an upgrade
531 * to the new format anyway: use "-P now -A now" as the default
533 if (force && !changed) {
534 dst_key_settime(key, DST_TIME_PUBLISH, now);
535 dst_key_settime(key, DST_TIME_ACTIVATE, now);
539 if (!changed && setttl)
543 * Print out time values, if -p was used.
546 printtime(key, DST_TIME_CREATED, "Created", epoch, stdout);
549 printtime(key, DST_TIME_PUBLISH, "Publish", epoch, stdout);
552 printtime(key, DST_TIME_ACTIVATE, "Activate", epoch, stdout);
555 printtime(key, DST_TIME_REVOKE, "Revoke", epoch, stdout);
558 printtime(key, DST_TIME_INACTIVE, "Inactive", epoch, stdout);
561 printtime(key, DST_TIME_DELETE, "Delete", epoch, stdout);
564 isc_buffer_init(&buf, newname, sizeof(newname));
565 result = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory,
567 if (result != ISC_R_SUCCESS) {
568 fatal("Failed to build public key filename: %s",
569 isc_result_totext(result));
572 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
574 if (result != ISC_R_SUCCESS) {
575 dst_key_format(key, keystr, sizeof(keystr));
576 fatal("Failed to write key %s: %s", keystr,
577 isc_result_totext(result));
580 printf("%s\n", newname);
582 isc_buffer_clear(&buf);
583 result = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory,
585 if (result != ISC_R_SUCCESS) {
586 fatal("Failed to build private key filename: %s",
587 isc_result_totext(result));
589 printf("%s\n", newname);
593 dst_key_free(&prevkey);
597 cleanup_entropy(&ectx);
599 isc_mem_stats(mctx, stdout);
600 cleanup_logging(&log);
601 isc_mem_free(mctx, directory);
602 isc_mem_destroy(&mctx);