2 * Copyright (C) 2009-2012, 2014, 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.
24 #include <isc/buffer.h>
25 #include <isc/commandline.h>
26 #include <isc/entropy.h>
30 #include <isc/print.h>
31 #include <isc/string.h>
34 #include <dns/keyvalues.h>
35 #include <dns/result.h>
39 #include "dnssectool.h"
41 const char *program = "dnssec-revoke";
44 static isc_mem_t *mctx = NULL;
46 ISC_PLATFORM_NORETURN_PRE static void
47 usage(void) ISC_PLATFORM_NORETURN_POST;
51 fprintf(stderr, "Usage:\n");
52 fprintf(stderr, " %s [options] keyfile\n\n", program);
53 fprintf(stderr, "Version: %s\n", VERSION);
55 fprintf(stderr, " -E engine: specify OpenSSL engine "
56 "(default \"pkcs11\")\n");
58 fprintf(stderr, " -E engine: specify OpenSSL engine\n");
60 fprintf(stderr, " -f: force overwrite\n");
61 fprintf(stderr, " -K directory: use directory for key files\n");
62 fprintf(stderr, " -h: help\n");
63 fprintf(stderr, " -r: remove old keyfiles after "
64 "creating revoked version\n");
65 fprintf(stderr, " -v level: set level of verbosity\n");
66 fprintf(stderr, " -V: print version information\n");
67 fprintf(stderr, "Output:\n");
68 fprintf(stderr, " K<name>+<alg>+<new id>.key, "
69 "K<name>+<alg>+<new id>.private\n");
75 main(int argc, char **argv) {
78 const char *engine = "pkcs11";
80 const char *engine = NULL;
82 char *filename = NULL, *dir = NULL;
83 char newname[1024], oldname[1024];
84 char keystr[DST_KEY_FORMATSIZE];
87 isc_entropy_t *ectx = NULL;
88 dst_key_t *key = NULL;
91 isc_boolean_t force = ISC_FALSE;
92 isc_boolean_t removefile = ISC_FALSE;
93 isc_boolean_t id = ISC_FALSE;
98 result = isc_mem_create(0, 0, &mctx);
99 if (result != ISC_R_SUCCESS)
100 fatal("Out of memory");
102 dns_result_register();
104 isc_commandline_errprint = ISC_FALSE;
106 while ((ch = isc_commandline_parse(argc, argv, "E:fK:rRhv:V")) != -1) {
109 engine = isc_commandline_argument;
116 * We don't have to copy it here, but do it to
117 * simplify cleanup later
119 dir = isc_mem_strdup(mctx, isc_commandline_argument);
121 fatal("Failed to allocate memory for "
126 removefile = ISC_TRUE;
132 verbose = strtol(isc_commandline_argument, &endp, 0);
134 fatal("-v must be followed by a number");
137 if (isc_commandline_option != '?')
138 fprintf(stderr, "%s: invalid argument -%c\n",
139 program, isc_commandline_option);
142 /* Does not return. */
146 /* Does not return. */
150 fprintf(stderr, "%s: unhandled option -%c\n",
151 program, isc_commandline_option);
156 if (argc < isc_commandline_index + 1 ||
157 argv[isc_commandline_index] == NULL)
158 fatal("The key file name was not specified");
159 if (argc > isc_commandline_index + 1)
160 fatal("Extraneous arguments");
163 filename = argv[isc_commandline_index];
165 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
167 if (result != ISC_R_SUCCESS)
168 fatal("cannot process filename %s: %s",
169 argv[isc_commandline_index],
170 isc_result_totext(result));
171 if (strcmp(dir, ".") == 0) {
172 isc_mem_free(mctx, dir);
178 setup_entropy(mctx, NULL, &ectx);
179 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
180 if (result != ISC_R_SUCCESS)
181 fatal("Could not initialize hash");
182 result = dst_lib_init2(mctx, ectx, engine,
183 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
184 if (result != ISC_R_SUCCESS)
185 fatal("Could not initialize dst: %s",
186 isc_result_totext(result));
187 isc_entropy_stopcallbacksources(ectx);
189 result = dst_key_fromnamedfile(filename, dir,
190 DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
192 if (result != ISC_R_SUCCESS)
193 fatal("Invalid keyfile name %s: %s",
194 filename, isc_result_totext(result));
197 fprintf(stdout, "%u\n", dst_key_rid(key));
200 dst_key_format(key, keystr, sizeof(keystr));
203 fprintf(stderr, "%s: %s\n", program, keystr);
208 check_keyversion(key, keystr);
211 flags = dst_key_flags(key);
212 if ((flags & DNS_KEYFLAG_REVOKE) == 0) {
215 if ((flags & DNS_KEYFLAG_KSK) == 0)
216 fprintf(stderr, "%s: warning: Key is not flagged "
217 "as a KSK. Revoking a ZSK is "
218 "legal, but undefined.\n",
221 isc_stdtime_get(&now);
222 dst_key_settime(key, DST_TIME_REVOKE, now);
224 dst_key_setflags(key, flags | DNS_KEYFLAG_REVOKE);
226 isc_buffer_init(&buf, newname, sizeof(newname));
227 dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf);
229 if (access(newname, F_OK) == 0 && !force) {
230 fatal("Key file %s already exists; "
231 "use -f to force overwrite", newname);
234 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
236 if (result != ISC_R_SUCCESS) {
237 dst_key_format(key, keystr, sizeof(keystr));
238 fatal("Failed to write key %s: %s", keystr,
239 isc_result_totext(result));
242 isc_buffer_clear(&buf);
243 dst_key_buildfilename(key, 0, dir, &buf);
244 printf("%s\n", newname);
247 * Remove old key file, if told to (and if
248 * it isn't the same as the new file)
250 if (removefile && dst_key_alg(key) != DST_ALG_RSAMD5) {
251 isc_buffer_init(&buf, oldname, sizeof(oldname));
252 dst_key_setflags(key, flags & ~DNS_KEYFLAG_REVOKE);
253 dst_key_buildfilename(key, DST_TYPE_PRIVATE, dir, &buf);
254 if (strcmp(oldname, newname) == 0)
256 (void)unlink(oldname);
257 isc_buffer_clear(&buf);
258 dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf);
259 (void)unlink(oldname);
262 dst_key_format(key, keystr, sizeof(keystr));
263 fatal("Key %s is already revoked", keystr);
270 cleanup_entropy(&ectx);
272 isc_mem_stats(mctx, stdout);
274 isc_mem_free(mctx, dir);
275 isc_mem_destroy(&mctx);