1 /* $NetBSD: dnssec-revoke.c,v 1.8 2014/12/10 04:37:51 christos Exp $ */
4 * Copyright (C) 2009-2012, 2014 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.
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 <pk11/result.h>
45 #include "dnssectool.h"
47 const char *program
= "dnssec-revoke";
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 #if defined(PKCS11CRYPTO)
61 fprintf(stderr
, " -E engine: specify PKCS#11 provider "
62 "(default: %s)\n", PK11_LIB_LOCATION
);
63 #elif defined(USE_PKCS11)
64 fprintf(stderr
, " -E engine: specify OpenSSL engine "
65 "(default \"pkcs11\")\n");
67 fprintf(stderr
, " -E engine: specify OpenSSL engine\n");
69 fprintf(stderr
, " -f: force overwrite\n");
70 fprintf(stderr
, " -K directory: use directory for key files\n");
71 fprintf(stderr
, " -h: help\n");
72 fprintf(stderr
, " -r: remove old keyfiles after "
73 "creating revoked version\n");
74 fprintf(stderr
, " -v level: set level of verbosity\n");
75 fprintf(stderr
, " -V: print version information\n");
76 fprintf(stderr
, "Output:\n");
77 fprintf(stderr
, " K<name>+<alg>+<new id>.key, "
78 "K<name>+<alg>+<new id>.private\n");
84 main(int argc
, char **argv
) {
87 const char *engine
= PKCS11_ENGINE
;
89 const char *engine
= NULL
;
91 char *filename
= NULL
, *dir
= NULL
;
92 char newname
[1024], oldname
[1024];
93 char keystr
[DST_KEY_FORMATSIZE
];
96 isc_entropy_t
*ectx
= NULL
;
97 dst_key_t
*key
= NULL
;
100 isc_boolean_t force
= ISC_FALSE
;
101 isc_boolean_t remove
= ISC_FALSE
;
102 isc_boolean_t id
= ISC_FALSE
;
107 result
= isc_mem_create(0, 0, &mctx
);
108 if (result
!= ISC_R_SUCCESS
)
109 fatal("Out of memory");
112 pk11_result_register();
114 dns_result_register();
116 isc_commandline_errprint
= ISC_FALSE
;
118 while ((ch
= isc_commandline_parse(argc
, argv
, "E:fK:rRhv:V")) != -1) {
121 engine
= isc_commandline_argument
;
128 * We don't have to copy it here, but do it to
129 * simplify cleanup later
131 dir
= isc_mem_strdup(mctx
, isc_commandline_argument
);
133 fatal("Failed to allocate memory for "
144 verbose
= strtol(isc_commandline_argument
, &endp
, 0);
146 fatal("-v must be followed by a number");
149 if (isc_commandline_option
!= '?')
150 fprintf(stderr
, "%s: invalid argument -%c\n",
151 program
, isc_commandline_option
);
154 /* Does not return. */
158 /* Does not return. */
162 fprintf(stderr
, "%s: unhandled option -%c\n",
163 program
, isc_commandline_option
);
168 if (argc
< isc_commandline_index
+ 1 ||
169 argv
[isc_commandline_index
] == NULL
)
170 fatal("The key file name was not specified");
171 if (argc
> isc_commandline_index
+ 1)
172 fatal("Extraneous arguments");
175 filename
= argv
[isc_commandline_index
];
177 result
= isc_file_splitpath(mctx
, argv
[isc_commandline_index
],
179 if (result
!= ISC_R_SUCCESS
)
180 fatal("cannot process filename %s: %s",
181 argv
[isc_commandline_index
],
182 isc_result_totext(result
));
183 if (strcmp(dir
, ".") == 0) {
184 isc_mem_free(mctx
, dir
);
190 setup_entropy(mctx
, NULL
, &ectx
);
191 result
= isc_hash_create(mctx
, ectx
, DNS_NAME_MAXWIRE
);
192 if (result
!= ISC_R_SUCCESS
)
193 fatal("Could not initialize hash");
194 result
= dst_lib_init2(mctx
, ectx
, engine
,
195 ISC_ENTROPY_BLOCKING
| ISC_ENTROPY_GOODONLY
);
196 if (result
!= ISC_R_SUCCESS
)
197 fatal("Could not initialize dst: %s",
198 isc_result_totext(result
));
199 isc_entropy_stopcallbacksources(ectx
);
201 result
= dst_key_fromnamedfile(filename
, dir
,
202 DST_TYPE_PUBLIC
|DST_TYPE_PRIVATE
,
204 if (result
!= ISC_R_SUCCESS
)
205 fatal("Invalid keyfile name %s: %s",
206 filename
, isc_result_totext(result
));
209 fprintf(stdout
, "%u\n", dst_key_rid(key
));
212 dst_key_format(key
, keystr
, sizeof(keystr
));
215 fprintf(stderr
, "%s: %s\n", program
, keystr
);
220 check_keyversion(key
, keystr
);
223 flags
= dst_key_flags(key
);
224 if ((flags
& DNS_KEYFLAG_REVOKE
) == 0) {
227 if ((flags
& DNS_KEYFLAG_KSK
) == 0)
228 fprintf(stderr
, "%s: warning: Key is not flagged "
229 "as a KSK. Revoking a ZSK is "
230 "legal, but undefined.\n",
233 isc_stdtime_get(&now
);
234 dst_key_settime(key
, DST_TIME_REVOKE
, now
);
236 dst_key_setflags(key
, flags
| DNS_KEYFLAG_REVOKE
);
238 isc_buffer_init(&buf
, newname
, sizeof(newname
));
239 dst_key_buildfilename(key
, DST_TYPE_PUBLIC
, dir
, &buf
);
241 if (access(newname
, F_OK
) == 0 && !force
) {
242 fatal("Key file %s already exists; "
243 "use -f to force overwrite", newname
);
246 result
= dst_key_tofile(key
, DST_TYPE_PUBLIC
|DST_TYPE_PRIVATE
,
248 if (result
!= ISC_R_SUCCESS
) {
249 dst_key_format(key
, keystr
, sizeof(keystr
));
250 fatal("Failed to write key %s: %s", keystr
,
251 isc_result_totext(result
));
254 isc_buffer_clear(&buf
);
255 dst_key_buildfilename(key
, 0, dir
, &buf
);
256 printf("%s\n", newname
);
259 * Remove old key file, if told to (and if
260 * it isn't the same as the new file)
262 if (remove
&& dst_key_alg(key
) != DST_ALG_RSAMD5
) {
263 isc_buffer_init(&buf
, oldname
, sizeof(oldname
));
264 dst_key_setflags(key
, flags
& ~DNS_KEYFLAG_REVOKE
);
265 dst_key_buildfilename(key
, DST_TYPE_PRIVATE
, dir
, &buf
);
266 if (strcmp(oldname
, newname
) == 0)
268 (void)unlink(oldname
);
269 isc_buffer_clear(&buf
);
270 dst_key_buildfilename(key
, DST_TYPE_PUBLIC
, dir
, &buf
);
271 (void)unlink(oldname
);
274 dst_key_format(key
, keystr
, sizeof(keystr
));
275 fatal("Key %s is already revoked", keystr
);
282 cleanup_entropy(&ectx
);
284 isc_mem_stats(mctx
, stdout
);
286 isc_mem_free(mctx
, dir
);
287 isc_mem_destroy(&mctx
);