dnssec-keyfromlabel.c revision 8a198fa776a09beb4dabf40b73a54d9c7bd70ac9
54cdd2b3070640311cd9ddeb6ca3514b3116e62bAutomatic Updater * Copyright (C) 2007-2009 Internet Systems Consortium, Inc. ("ISC")
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.
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.
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt/* $Id: dnssec-keyfromlabel.c,v 1.30 2010/01/19 20:26:07 each Exp $ */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#define MAX_RSA 4096 /* should be long enough... */
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsstatic const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
cc6cddfd94e8f0c58c290317b0853dac30b1b895Evan Hunt " NSEC3DSA | NSEC3RSASHA1 |"
cc6cddfd94e8f0c58c290317b0853dac30b1b895Evan Hunt " RSASHA256 | RSASHA512";
d1f39121a69b6afa6c0c9e44eceb60910d1d7f81Evan Hunt fprintf(stderr, " %s -l label [options] name\n\n",
210970a2488bb4c7eb679b3cbd918a9031fc5649Francis Dupont fprintf(stderr, " -l label: label of the key pair\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, " name: owner of the key\n");
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 fprintf(stderr, " -E enginename (default: pkcs11)\n");
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");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont "(default: AUTHCONF)\n");
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fprintf(stderr, " -y: permit keys that might collide\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");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont "K<name>+<alg>+<id>.private\n");
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont char *algname = NULL, *nametype = NULL, *type = NULL;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont isc_stdtime_t publish = 0, activate = 0, revoke = 0;
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 unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
4bf4beede27aff2da69d31c5baa995f594838378Mark Andrews unsigned char c;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont while ((ch = isc_commandline_parse(argc, argv,
4bf4beede27aff2da69d31c5baa995f594838378Mark Andrews c = (unsigned char)(isc_commandline_argument[0]);
6f6f08b7a4ea50806099b23ca97010195c1365cdEvan Hunt label = isc_mem_strdup(mctx, isc_commandline_argument);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont protocol = strtol(isc_commandline_argument, &endp, 10);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (*endp != '\0' || protocol < 0 || protocol > 255)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont verbose = strtol(isc_commandline_argument, &endp, 0);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont publish = strtotime(isc_commandline_argument,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont activate = strtotime(isc_commandline_argument,
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont if (strcasecmp(isc_commandline_argument, "none")) {
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont /* Reserved for FIPS mode */
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont /* FALLTHROUGH */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "%s: invalid argument -%c\n",
ddac1a2b9822b00aa936ef24c6d971f4a1407532Francis Dupont /* FALLTHROUGH */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fprintf(stderr, "%s: unhandled option -%c\n",
8b78c993cb475cc94e88560941b28c37684789d9Francis Dupont ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
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");
1cd538c05181b7236e4a2d159a99997dcfa1cb5cFrancis Dupont alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
1cd538c05181b7236e4a2d159a99997dcfa1cb5cFrancis Dupont alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont if (type != NULL && (options & DST_TYPE_KEY) != 0) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if (strcasecmp(type, "NOAUTHCONF") == 0) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont /* nothing */;
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont } else if (strcasecmp(nametype, "zone") == 0)
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid DNSKEY nametype %s", nametype);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("invalid DNSKEY protocol: %d", protocol);
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 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("a key with algorithm '%s' cannot be a zone key",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_init(&buf, 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 fatal("invalid key name %s: %s", argv[isc_commandline_index],
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont isc_buffer_init(&buf, filename, sizeof(filename) - 1);
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont /* associate the key */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont ret = dst_key_fromlabel(name, alg, flags, protocol,
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont dns_name_format(name, namestr, sizeof(namestr));
121079934564d8cedaec267dbfb6597bb784303eEvan Hunt /* NOTREACHED */
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont * Set key timing metadata (unless using -C)
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".
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt fatal("cannot use -G together with -P or -A options");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_PUBLISH, publish);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_ACTIVATE, activate);
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt "not flagged as a KSK, but -R "
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt "was used. Revoking a ZSK is "
53c22b8e0da67ca756ca309d5f84db9c189cd0a2Evan Hunt "legal, but undefined.\n",
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_REVOKE, revoke);
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt dst_key_settime(key, DST_TIME_INACTIVE, inactive);
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont dst_key_settime(key, DST_TIME_DELETE, delete);
b843f577bbcd6660fbaa506d9e55b156c689a5a8Evan Hunt "-P, -A, -R, -I, -D, or -G options");
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont * Compatibility mode: Private-key-format
1f821c10583d9cddbaf3626a96ff8cf10cdb645bFrancis Dupont * should be set to 1.2.
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.
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt if (key_collision(dst_key_id(key), name, directory, alg, mctx, &exact))
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt ret = dst_key_buildfilename(key, 0, directory, &buf);
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fatal("%s: %s already exists\n", program, filename);
8a198fa776a09beb4dabf40b73a54d9c7bd70ac9Evan Hunt fatal("%s: %s could collide with another key upon "
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",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont fatal("failed to write key %s: %s\n", keystr,