1 /* $NetBSD: dnssec-settime.c,v 1.12 2015/07/08 17:28:55 christos Exp $ */
4 * Copyright (C) 2009-2015 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.
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>
45 #include <pk11/result.h>
48 #include "dnssectool.h"
50 const char *program
= "dnssec-settime";
53 static isc_mem_t
*mctx
= NULL
;
55 ISC_PLATFORM_NORETURN_PRE
static void
56 usage(void) ISC_PLATFORM_NORETURN_POST
;
60 fprintf(stderr
, "Usage:\n");
61 fprintf(stderr
, " %s [options] keyfile\n\n", program
);
62 fprintf(stderr
, "Version: %s\n", VERSION
);
63 fprintf(stderr
, "General options:\n");
64 #if defined(PKCS11CRYPTO)
65 fprintf(stderr
, " -E engine: specify PKCS#11 provider "
66 "(default: %s)\n", PK11_LIB_LOCATION
);
67 #elif defined(USE_PKCS11)
68 fprintf(stderr
, " -E engine: specify OpenSSL engine "
69 "(default \"pkcs11\")\n");
71 fprintf(stderr
, " -E engine: specify OpenSSL engine\n");
73 fprintf(stderr
, " -f: force update of old-style "
75 fprintf(stderr
, " -K directory: set key file location\n");
76 fprintf(stderr
, " -L ttl: set default key TTL\n");
77 fprintf(stderr
, " -v level: set level of verbosity\n");
78 fprintf(stderr
, " -V: print version information\n");
79 fprintf(stderr
, " -h: help\n");
80 fprintf(stderr
, "Timing options:\n");
81 fprintf(stderr
, " -P date/[+-]offset/none: set/unset key "
82 "publication date\n");
83 fprintf(stderr
, " -A date/[+-]offset/none: set/unset key "
85 fprintf(stderr
, " -R date/[+-]offset/none: set/unset key "
87 fprintf(stderr
, " -I date/[+-]offset/none: set/unset key "
88 "inactivation date\n");
89 fprintf(stderr
, " -D date/[+-]offset/none: set/unset key "
91 fprintf(stderr
, "Printing options:\n");
92 fprintf(stderr
, " -p C/P/A/R/I/D/all: print a particular time "
94 fprintf(stderr
, " -u: print times in unix epoch "
96 fprintf(stderr
, "Output:\n");
97 fprintf(stderr
, " K<name>+<alg>+<new id>.key, "
98 "K<name>+<alg>+<new id>.private\n");
104 printtime(dst_key_t
*key
, int type
, const char *tag
, isc_boolean_t epoch
,
108 const char *output
= NULL
;
112 fprintf(stream
, "%s: ", tag
);
114 result
= dst_key_gettime(key
, type
, &when
);
115 if (result
== ISC_R_NOTFOUND
) {
116 fprintf(stream
, "UNSET\n");
118 fprintf(stream
, "%d\n", (int) when
);
121 output
= ctime(&time
);
122 fprintf(stream
, "%s", output
);
127 main(int argc
, char **argv
) {
130 const char *engine
= PKCS11_ENGINE
;
132 const char *engine
= NULL
;
134 char *filename
= NULL
, *directory
= NULL
;
136 char keystr
[DST_KEY_FORMATSIZE
];
139 isc_entropy_t
*ectx
= NULL
;
140 const char *predecessor
= NULL
;
141 dst_key_t
*prevkey
= NULL
;
142 dst_key_t
*key
= NULL
;
144 dns_name_t
*name
= NULL
;
145 dns_secalg_t alg
= 0;
146 unsigned int size
= 0;
147 isc_uint16_t flags
= 0;
151 isc_stdtime_t pub
= 0, act
= 0, rev
= 0, inact
= 0, del
= 0;
152 isc_stdtime_t prevact
= 0, previnact
= 0, prevdel
= 0;
153 isc_boolean_t setpub
= ISC_FALSE
, setact
= ISC_FALSE
;
154 isc_boolean_t setrev
= ISC_FALSE
, setinact
= ISC_FALSE
;
155 isc_boolean_t setdel
= ISC_FALSE
, setttl
= ISC_FALSE
;
156 isc_boolean_t unsetpub
= ISC_FALSE
, unsetact
= ISC_FALSE
;
157 isc_boolean_t unsetrev
= ISC_FALSE
, unsetinact
= ISC_FALSE
;
158 isc_boolean_t unsetdel
= ISC_FALSE
;
159 isc_boolean_t printcreate
= ISC_FALSE
, printpub
= ISC_FALSE
;
160 isc_boolean_t printact
= ISC_FALSE
, printrev
= ISC_FALSE
;
161 isc_boolean_t printinact
= ISC_FALSE
, printdel
= ISC_FALSE
;
162 isc_boolean_t force
= ISC_FALSE
;
163 isc_boolean_t epoch
= ISC_FALSE
;
164 isc_boolean_t changed
= ISC_FALSE
;
165 isc_log_t
*log
= NULL
;
170 result
= isc_mem_create(0, 0, &mctx
);
171 if (result
!= ISC_R_SUCCESS
)
172 fatal("Out of memory");
174 setup_logging(mctx
, &log
);
177 pk11_result_register();
179 dns_result_register();
181 isc_commandline_errprint
= ISC_FALSE
;
183 isc_stdtime_get(&now
);
185 #define CMDLINE_FLAGS "A:D:E:fhI:i:K:L:P:p:R:S:uv:V"
186 while ((ch
= isc_commandline_parse(argc
, argv
, CMDLINE_FLAGS
)) != -1) {
189 engine
= isc_commandline_argument
;
195 p
= isc_commandline_argument
;
196 if (!strcasecmp(p
, "all")) {
197 printcreate
= ISC_TRUE
;
201 printinact
= ISC_TRUE
;
209 printcreate
= ISC_TRUE
;
221 printinact
= ISC_TRUE
;
232 } while (*p
!= '\0');
239 * We don't have to copy it here, but do it to
240 * simplify cleanup later
242 directory
= isc_mem_strdup(mctx
,
243 isc_commandline_argument
);
244 if (directory
== NULL
) {
245 fatal("Failed to allocate memory for "
250 ttl
= strtottl(isc_commandline_argument
);
254 verbose
= strtol(isc_commandline_argument
, &endp
, 0);
256 fatal("-v must be followed by a number");
259 if (setpub
|| unsetpub
)
260 fatal("-P specified more than once");
263 pub
= strtotime(isc_commandline_argument
,
268 if (setact
|| unsetact
)
269 fatal("-A specified more than once");
272 act
= strtotime(isc_commandline_argument
,
277 if (setrev
|| unsetrev
)
278 fatal("-R specified more than once");
281 rev
= strtotime(isc_commandline_argument
,
286 if (setinact
|| unsetinact
)
287 fatal("-I specified more than once");
290 inact
= strtotime(isc_commandline_argument
,
291 now
, now
, &setinact
);
292 unsetinact
= !setinact
;
295 if (setdel
|| unsetdel
)
296 fatal("-D specified more than once");
299 del
= strtotime(isc_commandline_argument
,
304 predecessor
= isc_commandline_argument
;
307 prepub
= strtottl(isc_commandline_argument
);
310 if (isc_commandline_option
!= '?')
311 fprintf(stderr
, "%s: invalid argument -%c\n",
312 program
, isc_commandline_option
);
315 /* Does not return. */
319 /* Does not return. */
323 fprintf(stderr
, "%s: unhandled option -%c\n",
324 program
, isc_commandline_option
);
329 if (argc
< isc_commandline_index
+ 1 ||
330 argv
[isc_commandline_index
] == NULL
)
331 fatal("The key file name was not specified");
332 if (argc
> isc_commandline_index
+ 1)
333 fatal("Extraneous arguments");
336 setup_entropy(mctx
, NULL
, &ectx
);
337 result
= isc_hash_create(mctx
, ectx
, DNS_NAME_MAXWIRE
);
338 if (result
!= ISC_R_SUCCESS
)
339 fatal("Could not initialize hash");
340 result
= dst_lib_init2(mctx
, ectx
, engine
,
341 ISC_ENTROPY_BLOCKING
| ISC_ENTROPY_GOODONLY
);
342 if (result
!= ISC_R_SUCCESS
)
343 fatal("Could not initialize dst: %s",
344 isc_result_totext(result
));
345 isc_entropy_stopcallbacksources(ectx
);
347 if (predecessor
!= NULL
) {
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
) && !dst_key_isexternal(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
, &prevact
);
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
,
387 if (result
!= ISC_R_SUCCESS
)
388 fatal("Predecessor has no inactivation date. "
389 "You must set one before\n\t"
390 "generating a successor.");
392 pub
= prevact
- prepub
;
393 if (pub
< now
&& prepub
!= 0)
394 fatal("Predecessor will become inactive before the\n\t"
395 "prepublication period ends. Either change "
396 "its inactivation date,\n\t"
397 "or use the -i option to set a shorter "
398 "prepublication interval.");
400 result
= dst_key_gettime(prevkey
, DST_TIME_DELETE
, &prevdel
);
401 if (result
!= ISC_R_SUCCESS
)
402 fprintf(stderr
, "%s: warning: Predecessor has no "
404 "it will remain in the zone "
405 "indefinitely after rollover.\n",
407 else if (prevdel
< previnact
)
408 fprintf(stderr
, "%s: warning: Predecessor is "
409 "scheduled to be deleted\n\t"
410 "before it is scheduled to be "
411 "inactive.\n", program
);
413 changed
= setpub
= setact
= ISC_TRUE
;
414 dst_key_free(&prevkey
);
420 if (setpub
&& setact
&& (act
- prepub
) < pub
)
421 fatal("Activation and publication dates "
422 "are closer together than the\n\t"
423 "prepublication interval.");
425 if (setpub
&& !setact
) {
428 } else if (setact
&& !setpub
) {
433 if ((act
- prepub
) < now
)
434 fatal("Time until activation is shorter "
435 "than the\n\tprepublication interval.");
439 if (directory
!= NULL
) {
440 filename
= argv
[isc_commandline_index
];
442 result
= isc_file_splitpath(mctx
, argv
[isc_commandline_index
],
443 &directory
, &filename
);
444 if (result
!= ISC_R_SUCCESS
)
445 fatal("cannot process filename %s: %s",
446 argv
[isc_commandline_index
],
447 isc_result_totext(result
));
450 result
= dst_key_fromnamedfile(filename
, directory
,
451 DST_TYPE_PUBLIC
| DST_TYPE_PRIVATE
,
453 if (result
!= ISC_R_SUCCESS
)
454 fatal("Invalid keyfile %s: %s",
455 filename
, isc_result_totext(result
));
457 if (!dst_key_isprivate(key
) && !dst_key_isexternal(key
))
458 fatal("%s is not a private key", filename
);
460 dst_key_format(key
, keystr
, sizeof(keystr
));
462 if (predecessor
!= NULL
) {
463 if (!dns_name_equal(name
, dst_key_name(key
)))
464 fatal("Key name mismatch");
465 if (alg
!= dst_key_alg(key
))
466 fatal("Key algorithm mismatch");
467 if (size
!= dst_key_size(key
))
468 fatal("Key size mismatch");
469 if (flags
!= dst_key_flags(key
))
470 fatal("Key flags mismatch");
473 prevdel
= previnact
= 0;
474 if ((setdel
&& setinact
&& del
< inact
) ||
475 (dst_key_gettime(key
, DST_TIME_INACTIVE
,
476 &previnact
) == ISC_R_SUCCESS
&&
477 setdel
&& !setinact
&& del
< previnact
) ||
478 (dst_key_gettime(key
, DST_TIME_DELETE
,
479 &prevdel
) == ISC_R_SUCCESS
&&
480 setinact
&& !setdel
&& prevdel
< inact
) ||
481 (!setdel
&& !setinact
&& prevdel
< previnact
))
482 fprintf(stderr
, "%s: warning: Key is scheduled to "
483 "be deleted before it is\n\t"
484 "scheduled to be inactive.\n",
490 check_keyversion(key
, keystr
);
493 fprintf(stderr
, "%s: %s\n", program
, keystr
);
499 dst_key_settime(key
, DST_TIME_PUBLISH
, pub
);
501 dst_key_unsettime(key
, DST_TIME_PUBLISH
);
504 dst_key_settime(key
, DST_TIME_ACTIVATE
, act
);
506 dst_key_unsettime(key
, DST_TIME_ACTIVATE
);
509 if ((dst_key_flags(key
) & DNS_KEYFLAG_REVOKE
) != 0)
510 fprintf(stderr
, "%s: warning: Key %s is already "
511 "revoked; changing the revocation date "
512 "will not affect this.\n",
514 if ((dst_key_flags(key
) & DNS_KEYFLAG_KSK
) == 0)
515 fprintf(stderr
, "%s: warning: Key %s is not flagged as "
516 "a KSK, but -R was used. Revoking a "
517 "ZSK is legal, but undefined.\n",
519 dst_key_settime(key
, DST_TIME_REVOKE
, rev
);
520 } else if (unsetrev
) {
521 if ((dst_key_flags(key
) & DNS_KEYFLAG_REVOKE
) != 0)
522 fprintf(stderr
, "%s: warning: Key %s is already "
523 "revoked; removing the revocation date "
524 "will not affect this.\n",
526 dst_key_unsettime(key
, DST_TIME_REVOKE
);
530 dst_key_settime(key
, DST_TIME_INACTIVE
, inact
);
532 dst_key_unsettime(key
, DST_TIME_INACTIVE
);
535 dst_key_settime(key
, DST_TIME_DELETE
, del
);
537 dst_key_unsettime(key
, DST_TIME_DELETE
);
540 dst_key_setttl(key
, ttl
);
543 * No metadata changes were made but we're forcing an upgrade
544 * to the new format anyway: use "-P now -A now" as the default
546 if (force
&& !changed
) {
547 dst_key_settime(key
, DST_TIME_PUBLISH
, now
);
548 dst_key_settime(key
, DST_TIME_ACTIVATE
, now
);
552 if (!changed
&& setttl
)
556 * Print out time values, if -p was used.
559 printtime(key
, DST_TIME_CREATED
, "Created", epoch
, stdout
);
562 printtime(key
, DST_TIME_PUBLISH
, "Publish", epoch
, stdout
);
565 printtime(key
, DST_TIME_ACTIVATE
, "Activate", epoch
, stdout
);
568 printtime(key
, DST_TIME_REVOKE
, "Revoke", epoch
, stdout
);
571 printtime(key
, DST_TIME_INACTIVE
, "Inactive", epoch
, stdout
);
574 printtime(key
, DST_TIME_DELETE
, "Delete", epoch
, stdout
);
577 isc_buffer_init(&buf
, newname
, sizeof(newname
));
578 result
= dst_key_buildfilename(key
, DST_TYPE_PUBLIC
, directory
,
580 if (result
!= ISC_R_SUCCESS
) {
581 fatal("Failed to build public key filename: %s",
582 isc_result_totext(result
));
585 result
= dst_key_tofile(key
, DST_TYPE_PUBLIC
|DST_TYPE_PRIVATE
,
587 if (result
!= ISC_R_SUCCESS
) {
588 dst_key_format(key
, keystr
, sizeof(keystr
));
589 fatal("Failed to write key %s: %s", keystr
,
590 isc_result_totext(result
));
593 printf("%s\n", newname
);
595 isc_buffer_clear(&buf
);
596 result
= dst_key_buildfilename(key
, DST_TYPE_PRIVATE
, directory
,
598 if (result
!= ISC_R_SUCCESS
) {
599 fatal("Failed to build private key filename: %s",
600 isc_result_totext(result
));
602 printf("%s\n", newname
);
608 cleanup_entropy(&ectx
);
610 isc_mem_stats(mctx
, stdout
);
611 cleanup_logging(&log
);
612 isc_mem_free(mctx
, directory
);
613 isc_mem_destroy(&mctx
);