dnssec-keyfromlabel.c revision 8a198fa776a09beb4dabf40b73a54d9c7bd70ac9
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont/*
54cdd2b3070640311cd9ddeb6ca3514b3116e62bAutomatic Updater * Copyright (C) 2007-2009 Internet Systems Consortium, Inc. ("ISC")
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont *
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont * Permission to use, copy, modify, and/or distribute this software for any
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont * purpose with or without fee is hereby granted, provided that the above
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont * copyright notice and this permission notice appear in all copies.
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont *
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
db0008c4486414b81e90dca9938e1fc2320e5133Automatic Updater * PERFORMANCE OF THIS SOFTWARE.
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt/* $Id: dnssec-keyfromlabel.c,v 1.30 2010/01/19 20:26:07 each Exp $ */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont/*! \file */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <config.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont#include <ctype.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <stdlib.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/buffer.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/commandline.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/entropy.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/mem.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/region.h>
22b23fb59dcedebdb735ec1406d0557225b649abEvan Hunt#include <isc/print.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/string.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <isc/util.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt#include <dns/dnssec.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/fixedname.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/keyvalues.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/log.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/name.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/rdataclass.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/result.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dns/secalg.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include <dst/dst.h>
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#include "dnssectool.h"
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#define MAX_RSA 4096 /* should be long enough... */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupontconst char *program = "dnssec-keyfromlabel";
2a31bd531072824ef252c18303859d6af7451b00Francis Dupontint verbose;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt#define DEFAULT_ALGORITHM "RSASHA1"
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt#define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsstatic const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
cc6cddfd94e8f0c58c290317b0853dac30b1b895Evan Hunt " NSEC3DSA | NSEC3RSASHA1 |"
cc6cddfd94e8f0c58c290317b0853dac30b1b895Evan Hunt " RSASHA256 | RSASHA512";
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
debd489a44363870f96f75818e89ec27d3cab736Francis DupontISC_PLATFORM_NORETURN_PRE static void
debd489a44363870f96f75818e89ec27d3cab736Francis Dupontusage(void) ISC_PLATFORM_NORETURN_POST;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupontstatic void
2a31bd531072824ef252c18303859d6af7451b00Francis Dupontusage(void) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "Usage:\n");
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, " %s -l label [options] name\n\n",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont program);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "Version: %s\n", VERSION);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "Required options:\n");
210970a2488bb4c7eb679b3cbd918a9031fc5649Francis Dupont fprintf(stderr, " -l label: label of the key pair\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, " name: owner of the key\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "Other options:\n");
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, " -a algorithm: %s\n", algs);
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, " (default: RSASHA1, or "
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt "NSEC3RSASHA1 if using -3)\n");
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont fprintf(stderr, " -c class (default: IN)\n");
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont#ifdef USE_PKCS11
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont fprintf(stderr, " -E enginename (default: pkcs11)\n");
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt#else
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, " -E enginename\n");
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont#endif
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -f keyflag: KSK | REVOKE\n");
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt fprintf(stderr, " -K directory: directory in which to place "
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt "key files\n");
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fprintf(stderr, " -k: generate a TYPE=KEY key\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont fprintf(stderr, " -p protocol: default: 3 [dnssec]\n");
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont fprintf(stderr, " -t type: "
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "(default: AUTHCONF)\n");
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fprintf(stderr, " -y: permit keys that might collide\n");
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont fprintf(stderr, " -v verbose level\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, "Date options:\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -P date/[+-]offset: set key publication date\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -A date/[+-]offset: set key activation date\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -R date/[+-]offset: set key revocation date\n");
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt fprintf(stderr, " -I date/[+-]offset: set key inactivation date\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -D date/[+-]offset: set key deletion date\n");
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt fprintf(stderr, " -G: generate key only; do not set -P or -A\n");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fprintf(stderr, " -C: generate a backward-compatible key, omitting"
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt " all dates\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "Output:\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, " K<name>+<alg>+<id>.key, "
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont "K<name>+<alg>+<id>.private\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont exit (-1);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont}
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupontint
2a31bd531072824ef252c18303859d6af7451b00Francis Dupontmain(int argc, char **argv) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont char *algname = NULL, *nametype = NULL, *type = NULL;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont const char *directory = NULL;
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont#ifdef USE_PKCS11
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont const char *engine = "pkcs11";
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont#else
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont const char *engine = NULL;
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont#endif
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont char *classname = NULL;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont char *endp;
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt dst_key_t *key = NULL;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_fixedname_t fname;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_name_t *name;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_secalg_t alg;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_boolean_t oldstyle = ISC_FALSE;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_mem_t *mctx = NULL;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont int ch;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont int protocol = -1, signatory = 0;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_result_t ret;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_textregion_t r;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont char filename[255];
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_t buf;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_log_t *log = NULL;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_entropy_t *ectx = NULL;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_rdataclass_t rdclass;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont char *label = NULL;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_stdtime_t publish = 0, activate = 0, revoke = 0;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt isc_stdtime_t inactive = 0, delete = 0;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_stdtime_t now;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_boolean_t setdel = ISC_FALSE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_boolean_t unsetdel = ISC_FALSE;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt isc_boolean_t genonly = ISC_FALSE;
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt isc_boolean_t use_nsec3 = ISC_FALSE;
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt isc_boolean_t avoid_collisions = ISC_TRUE;
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt isc_boolean_t exact;
4bf4beede27aff2da69d31c5baa995f594838378Mark Andrews unsigned char c;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (argc == 1)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont usage();
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_result_register();
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_commandline_errprint = ISC_FALSE;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_stdtime_get(&now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont while ((ch = isc_commandline_parse(argc, argv,
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt "3a:Cc:E:f:K:kl:n:p:t:v:yFhGP:A:R:I:D:")) != -1)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont switch (ch) {
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt case '3':
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt use_nsec3 = ISC_TRUE;
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'a':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont algname = isc_commandline_argument;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont case 'C':
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont oldstyle = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'c':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont classname = isc_commandline_argument;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont case 'E':
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont engine = isc_commandline_argument;
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'f':
4bf4beede27aff2da69d31c5baa995f594838378Mark Andrews c = (unsigned char)(isc_commandline_argument[0]);
4bf4beede27aff2da69d31c5baa995f594838378Mark Andrews if (toupper(c) == 'K')
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont kskflag = DNS_KEYFLAG_KSK;
4bf4beede27aff2da69d31c5baa995f594838378Mark Andrews else if (toupper(c) == 'R')
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont revflag = DNS_KEYFLAG_REVOKE;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("unknown flag '%s'",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_commandline_argument);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt case 'K':
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt directory = isc_commandline_argument;
8f0502e922120f27207fbf6b6dda18f1112e486cEvan Hunt ret = try_dir(directory);
8f0502e922120f27207fbf6b6dda18f1112e486cEvan Hunt if (ret != ISC_R_SUCCESS)
e3b59e4af757d4b26ecb96e65f9953488283c216Evan Hunt fatal("cannot open directory %s: %s",
8f0502e922120f27207fbf6b6dda18f1112e486cEvan Hunt directory, isc_result_totext(ret));
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'k':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont options |= DST_TYPE_KEY;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'l':
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt label = isc_mem_strdup(mctx, isc_commandline_argument);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'n':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont nametype = isc_commandline_argument;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'p':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont protocol = strtol(isc_commandline_argument, &endp, 10);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (*endp != '\0' || protocol < 0 || protocol > 255)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("-p must be followed by a number "
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "[0..255]");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 't':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont type = isc_commandline_argument;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'v':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont verbose = strtol(isc_commandline_argument, &endp, 0);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (*endp != '\0')
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("-v must be followed by a number");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont break;
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt case 'y':
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt avoid_collisions = ISC_FALSE;
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt break;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt case 'G':
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt genonly = ISC_TRUE;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt break;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont case 'P':
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setpub || unsetpub)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fatal("-P specified more than once");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont setpub = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont publish = strtotime(isc_commandline_argument,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont now, now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont } else {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont unsetpub = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont }
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont break;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont case 'A':
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setact || unsetact)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fatal("-A specified more than once");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont setact = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont activate = strtotime(isc_commandline_argument,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont now, now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont } else {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont unsetact = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont }
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont break;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont case 'R':
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setrev || unsetrev)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fatal("-R specified more than once");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont setrev = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont revoke = strtotime(isc_commandline_argument,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont now, now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont } else {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont unsetrev = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont }
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont break;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt case 'I':
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt if (setinact || unsetinact)
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt fatal("-I specified more than once");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt setinact = ISC_TRUE;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt inactive = strtotime(isc_commandline_argument,
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt now, now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont } else {
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt unsetinact = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont }
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont break;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont case 'D':
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setdel || unsetdel)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fatal("-D specified more than once");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont setdel = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont delete = strtotime(isc_commandline_argument,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont now, now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont } else {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont unsetdel = ISC_TRUE;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont }
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont break;
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont case 'F':
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont /* Reserved for FIPS mode */
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont /* FALLTHROUGH */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case '?':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (isc_commandline_option != '?')
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "%s: invalid argument -%c\n",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont program, isc_commandline_option);
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont /* FALLTHROUGH */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont case 'h':
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont usage();
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont default:
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "%s: unhandled option -%c\n",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont program, isc_commandline_option);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont exit(1);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (ectx == NULL)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont setup_entropy(mctx, NULL, &ectx);
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont ret = dst_lib_init2(mctx, ectx, engine,
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (ret != ISC_R_SUCCESS)
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont fatal("could not initialize dst: %s",
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont isc_result_totext(ret));
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont setup_logging(verbose, mctx, &log);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (label == NULL)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("the key label was not specified");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (argc < isc_commandline_index + 1)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("the key name was not specified");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (argc > isc_commandline_index + 1)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("extraneous arguments");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt if (strchr(label, ':') == NULL &&
1ed01b339066bbf48d8f65e9ef36e782618f4fd6Mark Andrews engine != NULL && strlen(engine) != 0U) {
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt char *l;
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt int len;
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt len = strlen(label) + strlen(engine) + 2;
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt l = isc_mem_allocate(mctx, len);
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt if (l == NULL)
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt fatal("cannot allocate memory");
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt snprintf(l, len, "%s:%s", engine, label);
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt isc_mem_free(mctx, label);
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt label = l;
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt }
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt if (algname == NULL) {
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt if (use_nsec3)
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt algname = strdup(DEFAULT_NSEC3_ALGORITHM);
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt else
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt algname = strdup(DEFAULT_ALGORITHM);
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt if (verbose > 0)
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, "no algorithm specified; "
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt "defaulting to %s\n", algname);
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt }
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (strcasecmp(algname, "RSA") == 0) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "If you still wish to use RSA (RSAMD5) please "
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "specify \"-a RSAMD5\"\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont return (1);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont } else {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont r.base = algname;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont r.length = strlen(algname);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont ret = dns_secalg_fromtext(&alg, &r);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (ret != ISC_R_SUCCESS)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("unknown algorithm %s", algname);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (alg == DST_ALG_DH)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont options |= DST_TYPE_KEY;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt if (use_nsec3 &&
1cd538c05181b7236e4a2d159a99997dcfa1cb5cFrancis Dupont alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
1cd538c05181b7236e4a2d159a99997dcfa1cb5cFrancis Dupont alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512) {
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fatal("%s is incompatible with NSEC3; "
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt "do not use the -3 option", algname);
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt }
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (type != NULL && (options & DST_TYPE_KEY) != 0) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (strcasecmp(type, "NOAUTH") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= DNS_KEYTYPE_NOAUTH;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if (strcasecmp(type, "NOCONF") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= DNS_KEYTYPE_NOCONF;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if (strcasecmp(type, "NOAUTHCONF") == 0) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if (strcasecmp(type, "AUTHCONF") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont /* nothing */;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid type %s", type);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (nametype == NULL) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if ((options & DST_TYPE_KEY) != 0) /* KEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("no nametype specified");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont } else if (strcasecmp(nametype, "zone") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= DNS_KEYOWNER_ZONE;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (strcasecmp(nametype, "host") == 0 ||
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont strcasecmp(nametype, "entity") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= DNS_KEYOWNER_ENTITY;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if (strcasecmp(nametype, "user") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= DNS_KEYOWNER_USER;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid KEY nametype %s", nametype);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid DNSKEY nametype %s", nametype);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont rdclass = strtoclass(classname);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (directory == NULL)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont directory = ".";
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if ((options & DST_TYPE_KEY) != 0) /* KEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= signatory;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont flags |= kskflag;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont flags |= revflag;
f66c8eed515745b9bb81bfcd8af7dfeba3736aadAutomatic Updater }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (protocol == -1)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont protocol = DNS_KEYPROTO_DNSSEC;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if ((options & DST_TYPE_KEY) == 0 &&
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont protocol != DNS_KEYPROTO_DNSSEC)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid DNSKEY protocol: %d", protocol);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("specified null key with signing authority");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont alg == DNS_KEYALG_DH)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("a key with algorithm '%s' cannot be a zone key",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont algname);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_fixedname_init(&fname);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont name = dns_fixedname_name(&fname);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_init(&buf, argv[isc_commandline_index],
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont strlen(argv[isc_commandline_index]));
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (ret != ISC_R_SUCCESS)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid key name %s: %s", argv[isc_commandline_index],
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_result_totext(ret));
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_init(&buf, filename, sizeof(filename) - 1);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont /* associate the key */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont ret = dst_key_fromlabel(name, alg, flags, protocol,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont rdclass, engine, label, NULL, mctx, &key);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_entropy_stopcallbacksources(ectx);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (ret != ISC_R_SUCCESS) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont char namestr[DNS_NAME_FORMATSIZE];
77b8f88f144928eddcca144c348d6ef53e7d5c43Evan Hunt char algstr[DNS_SECALG_FORMATSIZE];
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_name_format(name, namestr, sizeof(namestr));
77b8f88f144928eddcca144c348d6ef53e7d5c43Evan Hunt dns_secalg_format(alg, algstr, sizeof(algstr));
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fatal("failed to get key %s/%s: %s\n",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont namestr, algstr, isc_result_totext(ret));
121079934564d8cedaec267dbfb6597bb784303eEvan Hunt /* NOTREACHED */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont exit(-1);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont /*
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont * Set key timing metadata (unless using -C)
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt *
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt * Publish and activation dates are set to "now" by default, but
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt * can be overridden. Creation date is always set to "now".
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont */
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (!oldstyle) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_CREATED, now);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt if (genonly && (setpub || setact))
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt fatal("cannot use -G together with -P or -A options");
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setpub)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_PUBLISH, publish);
cef109efa70b55ec1cd3ed706a9607ba1c3382d5Evan Hunt else if (setact)
cef109efa70b55ec1cd3ed706a9607ba1c3382d5Evan Hunt dst_key_settime(key, DST_TIME_PUBLISH, activate);
cef109efa70b55ec1cd3ed706a9607ba1c3382d5Evan Hunt else if (!genonly && !unsetpub)
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt dst_key_settime(key, DST_TIME_PUBLISH, now);
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setact)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_ACTIVATE, activate);
cef109efa70b55ec1cd3ed706a9607ba1c3382d5Evan Hunt else if (!genonly && !unsetact)
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt dst_key_settime(key, DST_TIME_ACTIVATE, now);
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt if (setrev) {
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt if (kskflag == 0)
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt fprintf(stderr, "%s: warning: Key is "
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt "not flagged as a KSK, but -R "
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt "was used. Revoking a ZSK is "
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt "legal, but undefined.\n",
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt program);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_REVOKE, revoke);
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt }
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt if (setinact)
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt dst_key_settime(key, DST_TIME_INACTIVE, inactive);
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (setdel)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_DELETE, delete);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont } else {
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt if (setpub || setact || setrev || setinact ||
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont setdel || unsetpub || unsetact ||
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt unsetrev || unsetinact || unsetdel || genonly)
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont fatal("cannot use -C together with "
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt "-P, -A, -R, -I, -D, or -G options");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont /*
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont * Compatibility mode: Private-key-format
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont * should be set to 1.2.
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont */
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_setprivateformat(key, 1, 2);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont }
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont /*
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt * Do not overwrite an existing key. Warn LOUDLY if there
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt * is a risk of ID collision due to this key or another key
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt * being revoked.
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont */
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt if (key_collision(dst_key_id(key), name, directory, alg, mctx, &exact))
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_clear(&buf);
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt ret = dst_key_buildfilename(key, 0, directory, &buf);
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt if (exact)
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fatal("%s: %s already exists\n", program, filename);
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt if (avoid_collisions)
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fatal("%s: %s could collide with another key upon "
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt "revokation\n", program, filename);
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fprintf(stderr, "%s: WARNING: Key %s could collide with "
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt "another key upon revokation. If you plan "
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt "to revoke keys, destroy this key and "
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt "generate a different one.\n",
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt program, filename);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt ret = dst_key_tofile(key, options, directory);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (ret != ISC_R_SUCCESS) {
77b8f88f144928eddcca144c348d6ef53e7d5c43Evan Hunt char keystr[DST_KEY_FORMATSIZE];
77b8f88f144928eddcca144c348d6ef53e7d5c43Evan Hunt dst_key_format(key, keystr, sizeof(keystr));
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("failed to write key %s: %s\n", keystr,
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_result_totext(ret));
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_clear(&buf);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont ret = dst_key_buildfilename(key, 0, NULL, &buf);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont printf("%s\n", filename);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dst_key_free(&key);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont cleanup_logging(&log);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont cleanup_entropy(&ectx);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dst_lib_destroy();
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_name_destroy();
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (verbose > 10)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_mem_stats(mctx, stdout);
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt isc_mem_free(mctx, label);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_mem_destroy(&mctx);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont return (0);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont}