dnssec-keyfromlabel.c revision 6098d364b690cb9dabf96e9664c4689c8559bd2e
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (C) 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
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 *
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 */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/* $Id: dnssec-keyfromlabel.c,v 1.4 2008/09/24 02:46:21 marka Exp $ */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*! \file */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <config.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <stdlib.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/buffer.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/commandline.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/entropy.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/mem.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/region.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/string.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/util.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/fixedname.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/keyvalues.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/log.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/name.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/rdataclass.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/result.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/secalg.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dst/dst.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include "dnssectool.h"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define MAX_RSA 4096 /* should be long enough... */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconst char *program = "dnssec-keyfromlabel";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint verbose;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews " NSEC3DSA | NSEC3RSASHA1";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic void
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsusage(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "Usage:\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " %s -a alg -l label [options] name\n\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews program);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "Version: %s\n", VERSION);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "Required options:\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -a algorithm: %s\n", algs);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -l label: label of the key\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " name: owner of the key\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "Other options:\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 fprintf(stderr, " -f keyflag: KSK\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -t <type>: "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "(default: AUTHCONF)\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -p <protocol>: "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "default: 3 [dnssec]\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -v <verbose level>\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " -k : generate a TYPE=KEY key\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "Output:\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, " K<name>+<alg>+<id>.key, "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "K<name>+<alg>+<id>.private\n");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews exit (-1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsmain(int argc, char **argv) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *algname = NULL, *nametype = NULL, *type = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *classname = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *endp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dst_key_t *key = NULL, *oldkey;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_fixedname_t fname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_t *name;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_uint16_t flags = 0, ksk = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_secalg_t alg;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_boolean_t null_key = ISC_FALSE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_t *mctx = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int ch;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int protocol = -1, signatory = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_result_t ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_textregion_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char filename[255];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_t buf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_log_t *log = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_entropy_t *ectx = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_rdataclass_t rdclass;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *label = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (argc == 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews usage();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_result_register();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_commandline_errprint = ISC_FALSE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while ((ch = isc_commandline_parse(argc, argv,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "a:c:f:kl:n:p:t:v:h")) != -1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (ch) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'a':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews algname = isc_commandline_argument;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'c':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews classname = isc_commandline_argument;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'f':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcasecmp(isc_commandline_argument, "KSK") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ksk = DNS_KEYFLAG_KSK;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("unknown flag '%s'",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_commandline_argument);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'k':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews options |= DST_TYPE_KEY;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'l':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews label = isc_commandline_argument;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'n':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews nametype = isc_commandline_argument;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'p':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews protocol = strtol(isc_commandline_argument, &endp, 10);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*endp != '\0' || protocol < 0 || protocol > 255)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("-p must be followed by a number "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "[0..255]");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 't':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews type = isc_commandline_argument;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'v':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews verbose = strtol(isc_commandline_argument, &endp, 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*endp != '\0')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("-v must be followed by a number");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case '?':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (isc_commandline_option != '?')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "%s: invalid argument -%c\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews program, isc_commandline_option);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 'h':
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews usage();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "%s: unhandled option -%c\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews program, isc_commandline_option);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews exit(1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ectx == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews setup_entropy(mctx, NULL, &ectx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_lib_init(mctx, ectx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ret != ISC_R_SUCCESS)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("could not initialize dst");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews setup_logging(verbose, mctx, &log);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (label == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("the key label was not specified");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (argc < isc_commandline_index + 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("the key name was not specified");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (argc > isc_commandline_index + 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("extraneous arguments");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (algname == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("no algorithm was specified");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcasecmp(algname, "RSA") == 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 return (1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r.base = algname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r.length = strlen(algname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dns_secalg_fromtext(&alg, &r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ret != ISC_R_SUCCESS)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("unknown algorithm %s", algname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (alg == DST_ALG_DH)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews options |= DST_TYPE_KEY;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (type != NULL && (options & DST_TYPE_KEY) != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcasecmp(type, "NOAUTH") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= DNS_KEYTYPE_NOAUTH;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (strcasecmp(type, "NOCONF") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= DNS_KEYTYPE_NOCONF;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (strcasecmp(type, "NOAUTHCONF") == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (strcasecmp(type, "AUTHCONF") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* nothing */;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid type %s", type);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (nametype == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((options & DST_TYPE_KEY) != 0) /* KEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("no nametype specified");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (strcasecmp(nametype, "zone") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= DNS_KEYOWNER_ZONE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcasecmp(nametype, "host") == 0 ||
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews strcasecmp(nametype, "entity") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= DNS_KEYOWNER_ENTITY;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (strcasecmp(nametype, "user") == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= DNS_KEYOWNER_USER;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid KEY nametype %s", nametype);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid DNSKEY nametype %s", nametype);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews rdclass = strtoclass(classname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((options & DST_TYPE_KEY) != 0) /* KEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= signatory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ((flags & DNS_KEYOWNER_ZONE) != 0) /* DNSKEY */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews flags |= ksk;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (protocol == -1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews protocol = DNS_KEYPROTO_DNSSEC;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ((options & DST_TYPE_KEY) == 0 &&
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews protocol != DNS_KEYPROTO_DNSSEC)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid DNSKEY protocol: %d", protocol);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("specified null key with signing authority");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews alg == DNS_KEYALG_DH)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("a key with algorithm '%s' cannot be a zone key",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews algname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_fixedname_init(&fname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews name = dns_fixedname_name(&fname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_init(&buf, argv[isc_commandline_index],
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews strlen(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 if (ret != ISC_R_SUCCESS)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("invalid key name %s: %s", argv[isc_commandline_index],
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_result_totext(ret));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews null_key = ISC_TRUE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_init(&buf, filename, sizeof(filename) - 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* associate the key */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_fromlabel(name, alg, flags, protocol,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews rdclass, "", label, NULL, mctx, &key);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_entropy_stopcallbacksources(ectx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ret != ISC_R_SUCCESS) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char namestr[DNS_NAME_FORMATSIZE];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char algstr[ALG_FORMATSIZE];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_format(name, namestr, sizeof(namestr));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews alg_format(alg, algstr, sizeof(algstr));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("failed to generate key %s/%s: %s\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews namestr, algstr, isc_result_totext(ret));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews exit(-1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
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 */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_fromfile(name, dst_key_id(key), alg,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews DST_TYPE_PRIVATE, NULL, mctx, &oldkey);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* do not overwrite an existing key */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ret == ISC_R_SUCCESS) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_clear(&buf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_buildfilename(key, 0, NULL, &buf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fprintf(stderr, "%s: %s already exists\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews program, filename);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dst_key_free(&key);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews exit (1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_tofile(key, options, NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ret != ISC_R_SUCCESS) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char keystr[KEY_FORMATSIZE];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews key_format(key, keystr, sizeof(keystr));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fatal("failed to write key %s: %s\n", keystr,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_result_totext(ret));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_buffer_clear(&buf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ret = dst_key_buildfilename(key, 0, NULL, &buf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews printf("%s\n", filename);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dst_key_free(&key);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cleanup_logging(&log);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cleanup_entropy(&ectx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dst_lib_destroy();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_destroy();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (verbose > 10)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_stats(mctx, stdout);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_destroy(&mctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews