dnssec-keyfromlabel.c revision 6098d364b690cb9dabf96e9664c4689c8559bd2e
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (C) 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * purpose with or without fee is hereby granted, provided that the above
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * copyright notice and this permission notice appear in all copies.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * PERFORMANCE OF THIS SOFTWARE.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/* $Id: dnssec-keyfromlabel.c,v 1.4 2008/09/24 02:46:21 marka Exp $ */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define MAX_RSA 4096 /* should be long enough... */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews " NSEC3DSA | NSEC3RSASHA1";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " %s -a alg -l label [options] name\n\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -l label: label of the key\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " (DNSKEY generation defaults to ZONE\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -c <class> (default: IN)\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "(default: AUTHCONF)\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "default: 3 [dnssec]\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -k : generate a TYPE=KEY key\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "K<name>+<alg>+<id>.private\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *algname = NULL, *nametype = NULL, *type = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcasecmp(isc_commandline_argument, "KSK") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews protocol = strtol(isc_commandline_argument, &endp, 10);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*endp != '\0' || protocol < 0 || protocol > 255)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "[0..255]");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews verbose = strtol(isc_commandline_argument, &endp, 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "If you still wish to use RSA (RSAMD5) please "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "specify \"-a RSAMD5\"\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (type != NULL && (options & DST_TYPE_KEY) != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (strcasecmp(type, "NOAUTHCONF") == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* nothing */;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ((flags & DNS_KEYOWNER_ZONE) != 0) /* DNSKEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid DNSKEY protocol: %d", protocol);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("specified null key with signing authority");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("a key with algorithm '%s' cannot be a zone key",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_init(&buf, argv[isc_commandline_index],
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid key name %s: %s", argv[isc_commandline_index],
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_init(&buf, filename, sizeof(filename) - 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* associate the key */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_fromlabel(name, alg, flags, protocol,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_format(name, namestr, sizeof(namestr));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Try to read a key with the same name, alg and id from disk.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * If there is one we must continue generating a new one
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * unless we were asked to generate a null key, in which
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * case we return failure.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_fromfile(name, dst_key_id(key), alg,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* do not overwrite an existing key */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_buildfilename(key, 0, NULL, &buf);