2009-06-30 02:53:46 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2009-06-30 02:53:46 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2009-06-30 02:53:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2009-06-30 02:53:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/commandline.h>
|
2009-08-28 03:13:08 +00:00
|
|
|
#include <isc/file.h>
|
2009-06-30 02:53:46 +00:00
|
|
|
#include <isc/hash.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/print.h>
|
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/keyvalues.h>
|
|
|
|
#include <dns/result.h>
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if USE_PKCS11
|
2014-03-12 20:52:01 -07:00
|
|
|
#include <pk11/result.h>
|
|
|
|
#endif
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
#include "dnssectool.h"
|
|
|
|
|
|
|
|
const char *program = "dnssec-revoke";
|
|
|
|
int verbose;
|
|
|
|
|
|
|
|
static isc_mem_t *mctx = NULL;
|
|
|
|
|
2009-09-29 15:06:07 +00:00
|
|
|
ISC_PLATFORM_NORETURN_PRE static void
|
|
|
|
usage(void) ISC_PLATFORM_NORETURN_POST;
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
static void
|
|
|
|
usage(void) {
|
|
|
|
fprintf(stderr, "Usage:\n");
|
|
|
|
fprintf(stderr, " %s [options] keyfile\n\n", program);
|
|
|
|
fprintf(stderr, "Version: %s\n", VERSION);
|
2018-06-12 11:26:04 +02:00
|
|
|
#if USE_PKCS11
|
2014-01-14 15:40:56 -08:00
|
|
|
fprintf(stderr, " -E engine: specify PKCS#11 provider "
|
|
|
|
"(default: %s)\n", PK11_LIB_LOCATION);
|
2009-10-05 17:30:49 +00:00
|
|
|
#else
|
2009-12-18 07:49:42 +00:00
|
|
|
fprintf(stderr, " -E engine: specify OpenSSL engine\n");
|
2009-10-05 17:30:49 +00:00
|
|
|
#endif
|
2009-07-19 04:18:05 +00:00
|
|
|
fprintf(stderr, " -f: force overwrite\n");
|
|
|
|
fprintf(stderr, " -K directory: use directory for key files\n");
|
2009-06-30 02:53:46 +00:00
|
|
|
fprintf(stderr, " -h: help\n");
|
|
|
|
fprintf(stderr, " -r: remove old keyfiles after "
|
|
|
|
"creating revoked version\n");
|
|
|
|
fprintf(stderr, " -v level: set level of verbosity\n");
|
2014-06-13 06:27:43 +05:30
|
|
|
fprintf(stderr, " -V: print version information\n");
|
2009-06-30 02:53:46 +00:00
|
|
|
fprintf(stderr, "Output:\n");
|
|
|
|
fprintf(stderr, " K<name>+<alg>+<new id>.key, "
|
|
|
|
"K<name>+<alg>+<new id>.private\n");
|
|
|
|
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv) {
|
|
|
|
isc_result_t result;
|
2009-10-05 17:30:49 +00:00
|
|
|
const char *engine = NULL;
|
2015-11-20 18:38:24 +11:00
|
|
|
char const *filename = NULL;
|
|
|
|
char *dir = NULL;
|
2009-06-30 02:53:46 +00:00
|
|
|
char newname[1024], oldname[1024];
|
2009-10-12 20:48:12 +00:00
|
|
|
char keystr[DST_KEY_FORMATSIZE];
|
2009-06-30 02:53:46 +00:00
|
|
|
char *endp;
|
|
|
|
int ch;
|
|
|
|
dst_key_t *key = NULL;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t flags;
|
2009-06-30 02:53:46 +00:00
|
|
|
isc_buffer_t buf;
|
|
|
|
isc_boolean_t force = ISC_FALSE;
|
2015-02-27 10:55:55 +11:00
|
|
|
isc_boolean_t removefile = ISC_FALSE;
|
2011-10-20 21:20:02 +00:00
|
|
|
isc_boolean_t id = ISC_FALSE;
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
result = isc_mem_create(0, 0, &mctx);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
fatal("Out of memory");
|
|
|
|
|
2018-05-22 15:24:37 +02:00
|
|
|
#if HAVE_PKCS11
|
2014-03-12 20:52:01 -07:00
|
|
|
pk11_result_register();
|
|
|
|
#endif
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_result_register();
|
|
|
|
|
|
|
|
isc_commandline_errprint = ISC_FALSE;
|
|
|
|
|
2014-06-13 06:27:43 +05:30
|
|
|
while ((ch = isc_commandline_parse(argc, argv, "E:fK:rRhv:V")) != -1) {
|
2009-06-30 02:53:46 +00:00
|
|
|
switch (ch) {
|
2009-10-05 17:30:49 +00:00
|
|
|
case 'E':
|
|
|
|
engine = isc_commandline_argument;
|
|
|
|
break;
|
2009-06-30 02:53:46 +00:00
|
|
|
case 'f':
|
|
|
|
force = ISC_TRUE;
|
|
|
|
break;
|
2009-07-19 04:18:05 +00:00
|
|
|
case 'K':
|
2009-09-04 16:57:22 +00:00
|
|
|
/*
|
|
|
|
* We don't have to copy it here, but do it to
|
|
|
|
* simplify cleanup later
|
|
|
|
*/
|
|
|
|
dir = isc_mem_strdup(mctx, isc_commandline_argument);
|
|
|
|
if (dir == NULL) {
|
|
|
|
fatal("Failed to allocate memory for "
|
|
|
|
"directory");
|
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
break;
|
2009-06-30 02:53:46 +00:00
|
|
|
case 'r':
|
2015-02-27 10:55:55 +11:00
|
|
|
removefile = ISC_TRUE;
|
2009-06-30 02:53:46 +00:00
|
|
|
break;
|
2011-10-20 21:20:02 +00:00
|
|
|
case 'R':
|
|
|
|
id = ISC_TRUE;
|
|
|
|
break;
|
2009-06-30 02:53:46 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = strtol(isc_commandline_argument, &endp, 0);
|
|
|
|
if (*endp != '\0')
|
|
|
|
fatal("-v must be followed by a number");
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
if (isc_commandline_option != '?')
|
|
|
|
fprintf(stderr, "%s: invalid argument -%c\n",
|
|
|
|
program, isc_commandline_option);
|
2017-08-09 00:17:44 -07:00
|
|
|
/* FALLTHROUGH */
|
2009-06-30 02:53:46 +00:00
|
|
|
case 'h':
|
2014-06-13 06:27:43 +05:30
|
|
|
/* Does not return. */
|
2009-06-30 02:53:46 +00:00
|
|
|
usage();
|
|
|
|
|
2014-06-13 06:27:43 +05:30
|
|
|
case 'V':
|
|
|
|
/* Does not return. */
|
|
|
|
version(program);
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: unhandled option -%c\n",
|
|
|
|
program, isc_commandline_option);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < isc_commandline_index + 1 ||
|
|
|
|
argv[isc_commandline_index] == NULL)
|
|
|
|
fatal("The key file name was not specified");
|
|
|
|
if (argc > isc_commandline_index + 1)
|
|
|
|
fatal("Extraneous arguments");
|
|
|
|
|
2009-08-28 03:13:08 +00:00
|
|
|
if (dir != NULL) {
|
|
|
|
filename = argv[isc_commandline_index];
|
|
|
|
} else {
|
2009-10-27 18:56:49 +00:00
|
|
|
result = isc_file_splitpath(mctx, argv[isc_commandline_index],
|
|
|
|
&dir, &filename);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
fatal("cannot process filename %s: %s",
|
|
|
|
argv[isc_commandline_index],
|
|
|
|
isc_result_totext(result));
|
2010-05-06 05:31:19 +00:00
|
|
|
if (strcmp(dir, ".") == 0) {
|
|
|
|
isc_mem_free(mctx, dir);
|
|
|
|
dir = NULL;
|
|
|
|
}
|
2009-07-17 23:47:41 +00:00
|
|
|
}
|
2009-07-17 06:25:45 +00:00
|
|
|
|
2018-04-22 14:56:28 +02:00
|
|
|
result = dst_lib_init(mctx, engine);
|
2009-06-30 02:53:46 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2009-10-05 17:30:49 +00:00
|
|
|
fatal("Could not initialize dst: %s",
|
|
|
|
isc_result_totext(result));
|
2009-06-30 02:53:46 +00:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
result = dst_key_fromnamedfile(filename, dir,
|
2009-06-30 02:53:46 +00:00
|
|
|
DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
|
|
|
|
mctx, &key);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
fatal("Invalid keyfile name %s: %s",
|
|
|
|
filename, isc_result_totext(result));
|
|
|
|
|
2011-10-20 21:20:02 +00:00
|
|
|
if (id) {
|
|
|
|
fprintf(stdout, "%u\n", dst_key_rid(key));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-10-26 21:18:24 +00:00
|
|
|
dst_key_format(key, keystr, sizeof(keystr));
|
2009-06-30 02:53:46 +00:00
|
|
|
|
2009-10-26 21:18:24 +00:00
|
|
|
if (verbose > 2)
|
2009-06-30 02:53:46 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", program, keystr);
|
2009-10-26 21:18:24 +00:00
|
|
|
|
|
|
|
if (force)
|
|
|
|
set_keyversion(key);
|
|
|
|
else
|
|
|
|
check_keyversion(key, keystr);
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
flags = dst_key_flags(key);
|
|
|
|
if ((flags & DNS_KEYFLAG_REVOKE) == 0) {
|
2009-09-02 23:48:03 +00:00
|
|
|
isc_stdtime_t now;
|
2009-09-02 06:29:01 +00:00
|
|
|
|
2009-09-23 16:01:57 +00:00
|
|
|
if ((flags & DNS_KEYFLAG_KSK) == 0)
|
|
|
|
fprintf(stderr, "%s: warning: Key is not flagged "
|
|
|
|
"as a KSK. Revoking a ZSK is "
|
|
|
|
"legal, but undefined.\n",
|
|
|
|
program);
|
|
|
|
|
2009-09-02 23:48:03 +00:00
|
|
|
isc_stdtime_get(&now);
|
2009-09-02 06:29:01 +00:00
|
|
|
dst_key_settime(key, DST_TIME_REVOKE, now);
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
dst_key_setflags(key, flags | DNS_KEYFLAG_REVOKE);
|
|
|
|
|
|
|
|
isc_buffer_init(&buf, newname, sizeof(newname));
|
|
|
|
dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf);
|
|
|
|
|
|
|
|
if (access(newname, F_OK) == 0 && !force) {
|
|
|
|
fatal("Key file %s already exists; "
|
|
|
|
"use -f to force overwrite", newname);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
|
|
|
|
dir);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-10-12 20:48:12 +00:00
|
|
|
dst_key_format(key, keystr, sizeof(keystr));
|
2009-06-30 02:53:46 +00:00
|
|
|
fatal("Failed to write key %s: %s", keystr,
|
|
|
|
isc_result_totext(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_clear(&buf);
|
2010-05-06 05:31:19 +00:00
|
|
|
dst_key_buildfilename(key, 0, dir, &buf);
|
2009-06-30 02:53:46 +00:00
|
|
|
printf("%s\n", newname);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove old key file, if told to (and if
|
|
|
|
* it isn't the same as the new file)
|
|
|
|
*/
|
2015-02-27 10:55:55 +11:00
|
|
|
if (removefile && dst_key_alg(key) != DST_ALG_RSAMD5) {
|
2009-06-30 02:53:46 +00:00
|
|
|
isc_buffer_init(&buf, oldname, sizeof(oldname));
|
|
|
|
dst_key_setflags(key, flags & ~DNS_KEYFLAG_REVOKE);
|
|
|
|
dst_key_buildfilename(key, DST_TYPE_PRIVATE, dir, &buf);
|
|
|
|
if (strcmp(oldname, newname) == 0)
|
|
|
|
goto cleanup;
|
2014-07-02 15:28:02 +10:00
|
|
|
(void)unlink(oldname);
|
2009-06-30 02:53:46 +00:00
|
|
|
isc_buffer_clear(&buf);
|
|
|
|
dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf);
|
2014-07-02 15:28:02 +10:00
|
|
|
(void)unlink(oldname);
|
2009-06-30 02:53:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-10-12 20:48:12 +00:00
|
|
|
dst_key_format(key, keystr, sizeof(keystr));
|
2009-06-30 02:53:46 +00:00
|
|
|
fatal("Key %s is already revoked", keystr);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
dst_key_free(&key);
|
2017-09-12 19:05:46 -07:00
|
|
|
dst_lib_destroy();
|
2009-06-30 02:53:46 +00:00
|
|
|
if (verbose > 10)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
2010-05-06 05:31:19 +00:00
|
|
|
if (dir != NULL)
|
|
|
|
isc_mem_free(mctx, dir);
|
2009-06-30 02:53:46 +00:00
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|