dnssec-keygen.c revision f66c8eed515745b9bb81bfcd8af7dfeba3736aad
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley/*
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Portions Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Portions Copyright (C) 1999-2003 Internet Software Consortium.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley *
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Permission to use, copy, modify, and/or distribute this software for any
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * purpose with or without fee is hereby granted, provided that the above
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * copyright notice and this permission notice appear in all copies.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley *
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley *
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
e419f613d8591885df608cb73065921be07dd12eBob Halley *
e419f613d8591885df608cb73065921be07dd12eBob Halley * Permission to use, copy, modify, and/or distribute this software for any
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * purpose with or without fee is hereby granted, provided that the above
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley * copyright notice and this permission notice appear in all copies.
e419f613d8591885df608cb73065921be07dd12eBob Halley *
e419f613d8591885df608cb73065921be07dd12eBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
9695ae1c24b168996e3a267855dc754971ccb32cBob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
1c776a2909632bc755f3fddd3b53addd792ab4d0Brian Wellington * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
e419f613d8591885df608cb73065921be07dd12eBob Halley * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington */
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/* $Id: dnssec-keygen.c,v 1.87 2009/06/30 23:48:00 tbox Exp $ */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence/*! \file */
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <config.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <stdlib.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/buffer.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/commandline.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/entropy.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/mem.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/region.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/string.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/util.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/fixedname.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/keyvalues.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/log.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/name.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/rdataclass.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/result.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <dns/secalg.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <dst/dst.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley#include "dnssectool.h"
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley#define MAX_RSA 4096 /* should be long enough... */
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halleyconst char *program = "dnssec-keygen";
e419f613d8591885df608cb73065921be07dd12eBob Halleyint verbose;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystatic const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 | NSEC3DSA |"
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington " NSEC3RSASHA1 | HMAC-MD5 |"
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley " HMAC-SHA1 | HMAC-SHA224 | HMAC-SHA256 |"
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley " HMAC-SHA384 | HMAC-SHA512";
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#define DEFAULT_ALGORITHM "RSASHA1"
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic isc_boolean_t
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtondsa_size_ok(int size) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington}
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halleystatic void
e419f613d8591885df608cb73065921be07dd12eBob Halleyusage(void) {
e419f613d8591885df608cb73065921be07dd12eBob Halley fprintf(stderr, "Usage:\n");
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley fprintf(stderr, " %s [options] name\n\n",
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley program);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fprintf(stderr, "Version: %s\n", VERSION);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " name: owner of the key\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, "Options:\n");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fprintf(stderr, " -a algorithm: %s (default RSASHA1)\n", algs);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fprintf(stderr, " -b key size, in bits:\n");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fprintf(stderr, " RSAMD5:\t\t[512..%d]\n", MAX_RSA);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington fprintf(stderr, " RSASHA1:\t\t[512..%d]\n", MAX_RSA);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington fprintf(stderr, " NSEC3RSASHA1:\t\t[512..%d]\n", MAX_RSA);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington fprintf(stderr, " DH:\t\t[128..4096]\n");
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington fprintf(stderr, " NSEC3DSA:\t\t[512..1024] and divisible by 64\n");
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
d325d53d03013a50a286b0066c9647370466ae26Andreas Gustafsson fprintf(stderr, " HMAC-SHA1:\t[1..160]\n");
d325d53d03013a50a286b0066c9647370466ae26Andreas Gustafsson fprintf(stderr, " HMAC-SHA224:\t[1..224]\n");
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington fprintf(stderr, " HMAC-SHA256:\t[1..256]\n");
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson fprintf(stderr, " HMAC-SHA384:\t[1..384]\n");
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson fprintf(stderr, " HMAC-SHA512:\t[1..512]\n");
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson fprintf(stderr, " (default 1024 for RSASHA1 ZSK, 2048 for KSK\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " (DNSKEY generation defaults to ZONE)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -c <class> (default: IN)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -d <digest bits> (0 => max, default)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -e use large exponent (RSAMD5/RSASHA1 only)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -f keyflag (KSK or REVOKE)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -g <generator> use specified generator "
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence "(DH only)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -t <type>: "
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "(default: AUTHCONF)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -p <protocol>: "
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "default: 3 [dnssec]\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -s <strength> strength value this key signs DNS "
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "records with (default: 0)\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -r <randomdev>: a file containing random data\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " -v <verbose level>\n");
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence fprintf(stderr, " -k : generate a TYPE=KEY key\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, "Output:\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr, " K<name>+<alg>+<id>.key, "
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence "K<name>+<alg>+<id>.private\n");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley exit (-1);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyint
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleymain(int argc, char **argv) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley char *algname = NULL, *nametype = NULL, *type = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley char *classname = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley char *endp;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dst_key_t *key = NULL, *oldkey;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_fixedname_t fname;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_name_t *name;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_uint16_t flags = 0, ksk = 0, revoke = 0;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff dns_secalg_t alg;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_boolean_t conflict = ISC_FALSE, null_key = ISC_FALSE;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_mem_t *mctx = NULL;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff int ch, rsa_exp = 0, generator = 0, param = 0;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff int protocol = -1, size = -1, signatory = 0;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_result_t ret;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_textregion_t r;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley char filename[255];
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_buffer_t buf;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_log_t *log = NULL;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_entropy_t *ectx = NULL;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington dns_rdataclass_t rdclass;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington int dbits = 0;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_boolean_t use_default = ISC_FALSE;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if (argc == 1)
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff usage();
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington dns_result_register();
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_commandline_errprint = ISC_FALSE;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington while ((ch = isc_commandline_parse(argc, argv,
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington "a:b:c:d:ef:g:kn:t:p:s:r:v:Fh")) != -1)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence switch (ch) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'a':
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington algname = isc_commandline_argument;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington break;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington case 'b':
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington size = strtol(isc_commandline_argument, &endp, 10);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (*endp != '\0' || size < 0)
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington fatal("-b requires a non-negative number");
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington break;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington case 'c':
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington classname = isc_commandline_argument;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington break;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington case 'd':
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington dbits = strtol(isc_commandline_argument, &endp, 10);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (*endp != '\0' || dbits < 0)
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson fatal("-d requires a non-negative number");
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson break;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington case 'e':
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington rsa_exp = 1;
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington break;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'f':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (strcasecmp(isc_commandline_argument, "KSK") == 0)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ksk = DNS_KEYFLAG_KSK;
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington else if (strcasecmp(isc_commandline_argument,
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington "REVOKE") == 0)
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington revoke = DNS_KEYFLAG_REVOKE;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington else
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington fatal("unknown flag '%s'",
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington isc_commandline_argument);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'g':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington generator = strtol(isc_commandline_argument,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington &endp, 10);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (*endp != '\0' || generator <= 0)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fatal("-g requires a positive number");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'k':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington options |= DST_TYPE_KEY;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'n':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington nametype = isc_commandline_argument;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 't':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington type = isc_commandline_argument;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'p':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington protocol = strtol(isc_commandline_argument, &endp, 10);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (*endp != '\0' || protocol < 0 || protocol > 255)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence fatal("-p must be followed by a number "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "[0..255]");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 's':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington signatory = strtol(isc_commandline_argument,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington &endp, 10);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (*endp != '\0' || signatory < 0 || signatory > 15)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fatal("-s must be followed by a number "
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "[0..15]");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'r':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington setup_entropy(mctx, isc_commandline_argument, &ectx);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'v':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington endp = NULL;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington verbose = strtol(isc_commandline_argument, &endp, 0);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (*endp != '\0')
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fatal("-v must be followed by a number");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence break;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'F':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /* Reserved for FIPS mode */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /* FALLTHROUGH */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case '?':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (isc_commandline_option != '?')
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence fprintf(stderr, "%s: invalid argument -%c\n",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence program, isc_commandline_option);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington /* FALLTHROUGH */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case 'h':
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington usage();
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington default:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fprintf(stderr, "%s: unhandled option -%c\n",
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington program, isc_commandline_option);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington exit(1);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (ectx == NULL)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington setup_entropy(mctx, NULL, &ectx);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ret = dst_lib_init(mctx, ectx,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (ret != ISC_R_SUCCESS)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence fatal("could not initialize dst");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence setup_logging(verbose, mctx, &log);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (argc < isc_commandline_index + 1)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fatal("the key name was not specified");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (argc > isc_commandline_index + 1)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fatal("extraneous arguments");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (algname == NULL) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington algname = strdup(DEFAULT_ALGORITHM);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington use_default = ISC_TRUE;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (verbose > 0)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington fprintf(stderr, "no algorithm specified; "
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "defaulting to %s\n", algname);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (strcasecmp(algname, "RSA") == 0) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "If you still wish to use RSA (RSAMD5) please "
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "specify \"-a RSAMD5\"\n");
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington return (1);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington } else if (strcasecmp(algname, "HMAC-MD5") == 0) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington options |= DST_TYPE_KEY;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington alg = DST_ALG_HMACMD5;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington } else if (strcasecmp(algname, "HMAC-SHA1") == 0) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington options |= DST_TYPE_KEY;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington alg = DST_ALG_HMACSHA1;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington } else if (strcasecmp(algname, "HMAC-SHA224") == 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington options |= DST_TYPE_KEY;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington alg = DST_ALG_HMACSHA224;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington } else if (strcasecmp(algname, "HMAC-SHA256") == 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington options |= DST_TYPE_KEY;
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington alg = DST_ALG_HMACSHA256;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington } else if (strcasecmp(algname, "HMAC-SHA384") == 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington options |= DST_TYPE_KEY;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington alg = DST_ALG_HMACSHA384;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff } else if (strcasecmp(algname, "HMAC-SHA512") == 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington options |= DST_TYPE_KEY;
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington alg = DST_ALG_HMACSHA512;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff } else {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington r.base = algname;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson r.length = strlen(algname);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington ret = dns_secalg_fromtext(&alg, &r);
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington if (ret != ISC_R_SUCCESS)
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington fatal("unknown algorithm %s", algname);
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington if (alg == DST_ALG_DH)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence options |= DST_TYPE_KEY;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington if (type != NULL && (options & DST_TYPE_KEY) != 0) {
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington if (strcasecmp(type, "NOAUTH") == 0)
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington flags |= DNS_KEYTYPE_NOAUTH;
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington else if (strcasecmp(type, "NOCONF") == 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington flags |= DNS_KEYTYPE_NOCONF;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington else if (strcasecmp(type, "NOAUTHCONF") == 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington if (size < 0)
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington size = 0;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington }
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington else if (strcasecmp(type, "AUTHCONF") == 0)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington /* nothing */;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson else
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson fatal("invalid type %s", type);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson }
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington if (size < 0) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (use_default) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence size = (ksk != 0) ? 2048 : 1024;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (verbose > 0)
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington fprintf(stderr, "key size not specified; "
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington "defaulting to %d\n", size);
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington } else {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington fatal("key size not specified (-b option)");
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington }
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington switch (alg) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DNS_KEYALG_RSAMD5:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DNS_KEYALG_RSASHA1:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DNS_KEYALG_NSEC3RSASHA1:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size != 0 && (size < 512 || size > MAX_RSA))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("RSA key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DNS_KEYALG_DH:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size != 0 && (size < 128 || size > 4096))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("DH key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DNS_KEYALG_DSA:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DNS_KEYALG_NSEC3DSA:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size != 0 && !dsa_size_ok(size))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("invalid DSS key size: %d", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DST_ALG_HMACMD5:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size < 1 || size > 512)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-MD5 key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dbits != 0 && (dbits < 80 || dbits > 128))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-MD5 digest bits %d out of range", dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((dbits % 8) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-MD5 digest bits %d not divisible by 8",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DST_ALG_HMACSHA1:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size < 1 || size > 160)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA1 key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dbits != 0 && (dbits < 80 || dbits > 160))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA1 digest bits %d out of range", dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((dbits % 8) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA1 digest bits %d not divisible by 8",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DST_ALG_HMACSHA224:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size < 1 || size > 224)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA224 key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dbits != 0 && (dbits < 112 || dbits > 224))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA224 digest bits %d out of range", dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((dbits % 8) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA224 digest bits %d not divisible by 8",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DST_ALG_HMACSHA256:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size < 1 || size > 256)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA256 key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dbits != 0 && (dbits < 128 || dbits > 256))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA256 digest bits %d out of range", dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((dbits % 8) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA256 digest bits %d not divisible by 8",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DST_ALG_HMACSHA384:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size < 1 || size > 384)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-384 key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dbits != 0 && (dbits < 192 || dbits > 384))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA384 digest bits %d out of range", dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((dbits % 8) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA384 digest bits %d not divisible by 8",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington case DST_ALG_HMACSHA512:
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (size < 1 || size > 512)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA512 key size %d out of range", size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dbits != 0 && (dbits < 256 || dbits > 512))
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA512 digest bits %d out of range", dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((dbits % 8) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("HMAC-SHA512 digest bits %d not divisible by 8",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dbits);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington break;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1 ||
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington alg == DNS_KEYALG_NSEC3RSASHA1) && rsa_exp != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("specified RSA exponent for a non-RSA key");
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (alg != DNS_KEYALG_DH && generator != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("specified DH generator for a non-DH key");
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (nametype == NULL) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("no nametype specified");
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington } else if (strcasecmp(nametype, "zone") == 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington flags |= DNS_KEYOWNER_ZONE;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington else if ((options & DST_TYPE_KEY) != 0) { /* KEY / HMAC */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (strcasecmp(nametype, "host") == 0 ||
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington strcasecmp(nametype, "entity") == 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington flags |= DNS_KEYOWNER_ENTITY;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington else if (strcasecmp(nametype, "user") == 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington flags |= DNS_KEYOWNER_USER;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington else
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("invalid KEY nametype %s", nametype);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fatal("invalid DNSKEY nametype %s", nametype);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington rdclass = strtoclass(classname);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington flags |= signatory;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington flags |= ksk;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington flags |= revoke;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (protocol == -1)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington protocol = DNS_KEYPROTO_DNSSEC;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington else if ((options & DST_TYPE_KEY) == 0 &&
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington protocol != DNS_KEYPROTO_DNSSEC)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fatal("invalid DNSKEY protocol: %d", protocol);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (size > 0)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fatal("specified null key with non-zero size");
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fatal("specified null key with signing authority");
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5 ||
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington alg == DST_ALG_HMACSHA1 || alg == DST_ALG_HMACSHA224 ||
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington alg == DST_ALG_HMACSHA256 || alg == DST_ALG_HMACSHA384 ||
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington alg == DST_ALG_HMACSHA512))
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fatal("a key with algorithm '%s' cannot be a zone key",
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington algname);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_fixedname_init(&fname);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence name = dns_fixedname_name(&fname);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_buffer_init(&buf, argv[isc_commandline_index],
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington strlen(argv[isc_commandline_index]));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ret = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (ret != ISC_R_SUCCESS)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fatal("invalid key name %s: %s", argv[isc_commandline_index],
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_result_totext(ret));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington switch(alg) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DNS_KEYALG_RSAMD5:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DNS_KEYALG_RSASHA1:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington param = rsa_exp;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DNS_KEYALG_DH:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington param = generator;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DNS_KEYALG_DSA:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DST_ALG_HMACMD5:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DST_ALG_HMACSHA1:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DST_ALG_HMACSHA224:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DST_ALG_HMACSHA256:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DST_ALG_HMACSHA384:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington case DST_ALG_HMACSHA512:
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington param = 0;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington break;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington null_key = ISC_TRUE;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_buffer_init(&buf, filename, sizeof(filename) - 1);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington do {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington conflict = ISC_FALSE;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington oldkey = NULL;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington /* generate the key */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ret = dst_key_generate(name, alg, size, param, flags, protocol,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington rdclass, mctx, &key);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_entropy_stopcallbacksources(ectx);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (ret != ISC_R_SUCCESS) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington char namestr[DNS_NAME_FORMATSIZE];
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson char algstr[ALG_FORMATSIZE];
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson dns_name_format(name, namestr, sizeof(namestr));
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson alg_format(alg, algstr, sizeof(algstr));
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson fatal("failed to generate key %s/%s: %s\n",
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson namestr, algstr, isc_result_totext(ret));
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson exit(-1);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dst_key_setbits(key, dbits);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Try to read a key with the same name, alg and id from disk.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * If there is one we must continue generating a new one
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * unless we were asked to generate a null key, in which
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * case we return failure.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ret = dst_key_fromfile(name, dst_key_id(key), alg,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley DST_TYPE_PRIVATE, NULL, mctx, &oldkey);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /* do not overwrite an existing key */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ret == ISC_R_SUCCESS) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dst_key_free(&oldkey);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley conflict = ISC_TRUE;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (null_key)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley break;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (conflict == ISC_TRUE) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (verbose > 0) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_buffer_clear(&buf);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ret = dst_key_buildfilename(key, 0, NULL, &buf);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fprintf(stderr,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "%s: %s already exists, "
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "generating a new key\n",
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley program, filename);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dst_key_free(&key);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } while (conflict == ISC_TRUE);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (conflict)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley fatal("cannot generate a null key when a key with id 0 "
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley "already exists");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ret = dst_key_tofile(key, options, NULL);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ret != ISC_R_SUCCESS) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley char keystr[KEY_FORMATSIZE];
63bf060be4ff2a7ade02fd86abb98694a5afc250Brian Wellington key_format(key, keystr, sizeof(keystr));
63bf060be4ff2a7ade02fd86abb98694a5afc250Brian Wellington fatal("failed to write key %s: %s\n", keystr,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_result_totext(ret));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_buffer_clear(&buf);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ret = dst_key_buildfilename(key, 0, NULL, &buf);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley printf("%s\n", filename);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dst_key_free(&key);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley cleanup_logging(&log);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley cleanup_entropy(&ectx);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dst_lib_destroy();
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_name_destroy();
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (verbose > 10)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_stats(mctx, stdout);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_destroy(&mctx);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (0);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley