1999-09-10 19:52:56 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Portions Copyright (C) 2000, 2001 Internet Software Consortium.
|
2000-06-09 21:32:05 +00:00
|
|
|
* Portions Copyright (C) 1995-2000 by Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-06-09 21:32:05 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
1999-09-10 19:52:56 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-06-09 21:32:05 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
|
|
|
|
* NETWORK ASSOCIATES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
* SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR NETWORK
|
|
|
|
* ASSOCIATES BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
|
|
|
|
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
1999-09-10 19:52:56 +00:00
|
|
|
*/
|
|
|
|
|
2001-09-19 00:15:05 +00:00
|
|
|
/* $Id: dnssec-keygen.c,v 1.52 2001/09/19 00:15:05 bwelling Exp $ */
|
1999-09-10 19:52:56 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2000-05-15 21:06:41 +00:00
|
|
|
#include <isc/buffer.h>
|
1999-10-06 20:07:25 +00:00
|
|
|
#include <isc/commandline.h>
|
2000-06-09 22:34:40 +00:00
|
|
|
#include <isc/entropy.h>
|
1999-09-10 19:52:56 +00:00
|
|
|
#include <isc/mem.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/region.h>
|
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2000-05-24 23:13:32 +00:00
|
|
|
#include <dns/fixedname.h>
|
1999-09-10 19:52:56 +00:00
|
|
|
#include <dns/keyvalues.h>
|
2000-05-24 23:54:43 +00:00
|
|
|
#include <dns/log.h>
|
2000-05-24 23:13:32 +00:00
|
|
|
#include <dns/name.h>
|
2000-09-12 10:07:50 +00:00
|
|
|
#include <dns/rdataclass.h>
|
2000-05-24 23:13:32 +00:00
|
|
|
#include <dns/result.h>
|
2000-04-25 17:57:10 +00:00
|
|
|
#include <dns/secalg.h>
|
2000-05-24 23:13:32 +00:00
|
|
|
|
1999-09-10 19:52:56 +00:00
|
|
|
#include <dst/dst.h>
|
|
|
|
#include <dst/result.h>
|
|
|
|
|
2000-05-24 23:54:43 +00:00
|
|
|
#include "dnssectool.h"
|
1999-09-10 19:52:56 +00:00
|
|
|
|
2000-11-07 20:10:14 +00:00
|
|
|
#define MAX_RSA 4096 /* should be long enough... */
|
2000-04-27 18:24:26 +00:00
|
|
|
|
2000-06-01 18:49:22 +00:00
|
|
|
const char *program = "dnssec-keygen";
|
2000-05-24 23:54:43 +00:00
|
|
|
int verbose;
|
2000-05-16 18:41:00 +00:00
|
|
|
|
2001-09-15 00:01:58 +00:00
|
|
|
static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 | HMAC-MD5";
|
|
|
|
|
2000-05-16 18:41:00 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
dsa_size_ok(int size) {
|
|
|
|
return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-05-24 03:16:19 +00:00
|
|
|
usage(void) {
|
2001-02-15 23:26:29 +00:00
|
|
|
fprintf(stderr, "Usage:\n");
|
|
|
|
fprintf(stderr, " %s -a alg -b bits -n type [options] name\n\n",
|
|
|
|
program);
|
|
|
|
fprintf(stderr, "Required options:\n");
|
2001-09-15 00:01:58 +00:00
|
|
|
fprintf(stderr, " -a algorithm: %s\n", algs);
|
2001-02-15 23:26:29 +00:00
|
|
|
fprintf(stderr, " -b key size, in bits:\n");
|
2001-09-19 00:15:05 +00:00
|
|
|
fprintf(stderr, " RSAMD5:\t\t[512..%d]\n", MAX_RSA);
|
|
|
|
fprintf(stderr, " RSASHA1:\t\t[512..%d]\n", MAX_RSA);
|
2001-02-15 23:26:29 +00:00
|
|
|
fprintf(stderr, " DH:\t\t[128..4096]\n");
|
|
|
|
fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
|
|
|
|
fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
|
|
|
|
fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER\n");
|
|
|
|
fprintf(stderr, " name: owner of the key\n");
|
|
|
|
fprintf(stderr, "Other options:\n");
|
|
|
|
fprintf(stderr, " -c class (default: IN)\n");
|
2001-09-19 00:15:05 +00:00
|
|
|
fprintf(stderr, " -e use large exponent (RSAMD5/RSASHA1 only)\n");
|
2001-02-15 23:26:29 +00:00
|
|
|
fprintf(stderr, " -g use specified generator (DH only)\n");
|
|
|
|
fprintf(stderr, " -t type: AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
|
2000-09-21 17:18:14 +00:00
|
|
|
"(default: AUTHCONF)\n");
|
2001-02-15 23:26:29 +00:00
|
|
|
fprintf(stderr, " -p protocol value "
|
2000-09-21 17:18:14 +00:00
|
|
|
"(default: 2 [email] for USER, 3 [dnssec] otherwise)\n");
|
2001-02-15 23:26:29 +00:00
|
|
|
fprintf(stderr, " -s strength value this key signs DNS records "
|
|
|
|
"with (default: 0)\n");
|
|
|
|
fprintf(stderr, " -r randomdev (a file containing random data)\n");
|
|
|
|
fprintf(stderr, " -v verbose level\n");
|
|
|
|
fprintf(stderr, "Output:\n");
|
|
|
|
fprintf(stderr, " K<name>+<alg>+<id>.key, "
|
|
|
|
"K<name>+<alg>+<id>.private\n");
|
2000-05-16 18:41:00 +00:00
|
|
|
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
1999-09-10 19:52:56 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv) {
|
2000-05-08 14:38:29 +00:00
|
|
|
char *algname = NULL, *nametype = NULL, *type = NULL;
|
2000-09-12 10:07:50 +00:00
|
|
|
char *classname = NULL;
|
2001-09-19 00:03:37 +00:00
|
|
|
char *endp;
|
2000-05-19 00:20:59 +00:00
|
|
|
dst_key_t *key = NULL, *oldkey;
|
2000-05-24 23:13:32 +00:00
|
|
|
dns_fixedname_t fname;
|
|
|
|
dns_name_t *name;
|
2000-05-08 14:38:29 +00:00
|
|
|
isc_uint16_t flags = 0;
|
|
|
|
dns_secalg_t alg;
|
2000-06-09 22:34:40 +00:00
|
|
|
isc_boolean_t conflict = ISC_FALSE, null_key = ISC_FALSE;
|
2000-05-08 14:38:29 +00:00
|
|
|
isc_mem_t *mctx = NULL;
|
|
|
|
int ch, rsa_exp = 0, generator = 0, param = 0;
|
|
|
|
int protocol = -1, size = -1, signatory = 0;
|
|
|
|
isc_result_t ret;
|
|
|
|
isc_textregion_t r;
|
2000-05-15 21:06:41 +00:00
|
|
|
char filename[255];
|
|
|
|
isc_buffer_t buf;
|
2000-05-24 23:54:43 +00:00
|
|
|
isc_log_t *log = NULL;
|
2000-06-09 22:34:40 +00:00
|
|
|
isc_entropy_t *ectx = NULL;
|
2000-09-12 10:07:50 +00:00
|
|
|
dns_rdataclass_t rdclass;
|
1999-09-10 19:52:56 +00:00
|
|
|
|
2000-04-25 17:57:10 +00:00
|
|
|
if (argc == 1)
|
2000-05-18 22:04:02 +00:00
|
|
|
usage();
|
1999-09-10 19:52:56 +00:00
|
|
|
|
2001-09-19 00:03:37 +00:00
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
|
|
|
|
2000-05-24 23:13:32 +00:00
|
|
|
dns_result_register();
|
|
|
|
|
1999-10-06 20:07:25 +00:00
|
|
|
while ((ch = isc_commandline_parse(argc, argv,
|
2000-09-12 10:07:50 +00:00
|
|
|
"a:b:c:eg:n:t:p:s:hr:v:")) != -1)
|
2000-04-25 17:57:10 +00:00
|
|
|
{
|
1999-09-10 19:52:56 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
2000-09-12 10:07:50 +00:00
|
|
|
algname = isc_commandline_argument;
|
1999-09-10 19:52:56 +00:00
|
|
|
break;
|
2000-04-25 17:57:10 +00:00
|
|
|
case 'b':
|
|
|
|
size = strtol(isc_commandline_argument, &endp, 10);
|
|
|
|
if (*endp != '\0' || size < 0)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("-b requires a non-negative number");
|
1999-09-10 19:52:56 +00:00
|
|
|
break;
|
2000-09-12 10:07:50 +00:00
|
|
|
case 'c':
|
|
|
|
classname = isc_commandline_argument;
|
|
|
|
break;
|
2000-04-25 17:57:10 +00:00
|
|
|
case 'e':
|
|
|
|
rsa_exp = 1;
|
1999-09-10 19:52:56 +00:00
|
|
|
break;
|
1999-09-27 17:11:41 +00:00
|
|
|
case 'g':
|
2000-05-24 03:16:19 +00:00
|
|
|
generator = strtol(isc_commandline_argument,
|
|
|
|
&endp, 10);
|
2000-04-25 17:57:10 +00:00
|
|
|
if (*endp != '\0' || generator <= 0)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("-g requires a positive number");
|
2000-04-25 17:57:10 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2000-09-12 10:07:50 +00:00
|
|
|
nametype = isc_commandline_argument;
|
2000-04-25 17:57:10 +00:00
|
|
|
if (nametype == NULL)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("out of memory");
|
2000-04-25 17:57:10 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2000-09-12 10:07:50 +00:00
|
|
|
type = isc_commandline_argument;
|
2000-04-25 17:57:10 +00:00
|
|
|
if (type == NULL)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("out of memory");
|
1999-09-27 17:11:41 +00:00
|
|
|
break;
|
1999-09-10 19:52:56 +00:00
|
|
|
case 'p':
|
2000-04-25 17:57:10 +00:00
|
|
|
protocol = strtol(isc_commandline_argument, &endp, 10);
|
|
|
|
if (*endp != '\0' || protocol < 0 || protocol > 255)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("-p must be followed by a number "
|
|
|
|
"[0..255]");
|
1999-09-10 19:52:56 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2000-05-08 14:38:29 +00:00
|
|
|
signatory = strtol(isc_commandline_argument,
|
|
|
|
&endp, 10);
|
2000-04-25 17:57:10 +00:00
|
|
|
if (*endp != '\0' || signatory < 0 || signatory > 15)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("-s must be followed by a number "
|
|
|
|
"[0..15]");
|
1999-09-10 19:52:56 +00:00
|
|
|
break;
|
2000-06-10 01:28:11 +00:00
|
|
|
case 'r':
|
2001-09-05 23:15:42 +00:00
|
|
|
setup_entropy(mctx, isc_commandline_argument, &ectx);
|
2000-06-10 01:28:11 +00:00
|
|
|
break;
|
2000-04-27 18:24:26 +00:00
|
|
|
case 'v':
|
|
|
|
endp = NULL;
|
|
|
|
verbose = strtol(isc_commandline_argument, &endp, 0);
|
|
|
|
if (*endp != '\0')
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("-v must be followed by a number");
|
2000-04-27 18:24:26 +00:00
|
|
|
break;
|
|
|
|
|
1999-09-10 19:52:56 +00:00
|
|
|
case 'h':
|
2000-05-18 22:04:02 +00:00
|
|
|
usage();
|
1999-09-10 19:52:56 +00:00
|
|
|
default:
|
2000-05-18 22:04:02 +00:00
|
|
|
fprintf(stderr, "%s: invalid argument -%c\n",
|
2000-05-24 23:54:43 +00:00
|
|
|
program, ch);
|
2000-05-18 22:04:02 +00:00
|
|
|
usage();
|
2000-08-01 01:33:37 +00:00
|
|
|
}
|
1999-09-10 19:52:56 +00:00
|
|
|
}
|
|
|
|
|
2001-09-05 23:15:42 +00:00
|
|
|
if (ectx == NULL)
|
|
|
|
setup_entropy(mctx, NULL, &ectx);
|
2000-06-09 22:34:40 +00:00
|
|
|
ret = dst_lib_init(mctx, ectx,
|
|
|
|
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
fatal("could not initialize dst");
|
|
|
|
|
2000-05-24 23:54:43 +00:00
|
|
|
setup_logging(verbose, mctx, &log);
|
|
|
|
|
2000-05-05 19:55:56 +00:00
|
|
|
if (argc < isc_commandline_index + 1)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("the key name was not specified");
|
2000-05-05 19:55:56 +00:00
|
|
|
if (argc > isc_commandline_index + 1)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("extraneous arguments");
|
1999-09-10 19:52:56 +00:00
|
|
|
|
2000-04-25 17:57:10 +00:00
|
|
|
if (algname == NULL)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("no algorithm was specified");
|
2000-04-25 17:57:10 +00:00
|
|
|
if (strcasecmp(algname, "RSA") == 0)
|
2001-09-19 00:15:05 +00:00
|
|
|
alg = DNS_KEYALG_RSAMD5;
|
2000-04-25 17:57:10 +00:00
|
|
|
else if (strcasecmp(algname, "HMAC-MD5") == 0)
|
|
|
|
alg = DST_ALG_HMACMD5;
|
|
|
|
else {
|
|
|
|
r.base = algname;
|
|
|
|
r.length = strlen(algname);
|
|
|
|
ret = dns_secalg_fromtext(&alg, &r);
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("unknown algorithm %s", algname);
|
2000-04-25 17:57:10 +00:00
|
|
|
}
|
1999-09-10 19:52:56 +00:00
|
|
|
|
2000-04-25 17:57:10 +00:00
|
|
|
if (type != NULL) {
|
|
|
|
if (strcasecmp(type, "NOAUTH") == 0)
|
|
|
|
flags |= DNS_KEYTYPE_NOAUTH;
|
|
|
|
else if (strcasecmp(type, "NOCONF") == 0)
|
|
|
|
flags |= DNS_KEYTYPE_NOCONF;
|
2000-05-10 17:57:53 +00:00
|
|
|
else if (strcasecmp(type, "NOAUTHCONF") == 0) {
|
2000-04-25 17:57:10 +00:00
|
|
|
flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
|
2000-05-10 17:57:53 +00:00
|
|
|
if (size < 0)
|
|
|
|
size = 0;
|
|
|
|
}
|
2000-04-25 17:57:10 +00:00
|
|
|
else if (strcasecmp(type, "AUTHCONF") == 0)
|
|
|
|
/* nothing */;
|
|
|
|
else
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("invalid type %s", type);
|
2000-04-25 17:57:10 +00:00
|
|
|
}
|
|
|
|
|
2000-05-10 17:57:53 +00:00
|
|
|
if (size < 0)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("key size not specified (-b option)");
|
2000-05-10 17:57:53 +00:00
|
|
|
|
2000-04-25 17:57:10 +00:00
|
|
|
switch (alg) {
|
2001-09-19 00:15:05 +00:00
|
|
|
case DNS_KEYALG_RSAMD5:
|
|
|
|
case DNS_KEYALG_RSASHA1:
|
2000-05-10 17:57:53 +00:00
|
|
|
if (size != 0 && (size < 512 || size > MAX_RSA))
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("RSA key size %d out of range", size);
|
2000-04-25 17:57:10 +00:00
|
|
|
break;
|
|
|
|
case DNS_KEYALG_DH:
|
|
|
|
if (size != 0 && (size < 128 || size > 4096))
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("DH key size %d out of range", size);
|
2000-04-25 17:57:10 +00:00
|
|
|
break;
|
|
|
|
case DNS_KEYALG_DSA:
|
2000-05-10 17:57:53 +00:00
|
|
|
if (size != 0 && !dsa_size_ok(size))
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("Invalid DSS key size: %d", size);
|
2000-04-25 17:57:10 +00:00
|
|
|
break;
|
|
|
|
case DST_ALG_HMACMD5:
|
|
|
|
if (size < 1 || size > 512)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("HMAC-MD5 key size %d out of range", size);
|
2000-04-25 17:57:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-19 00:15:05 +00:00
|
|
|
if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1) &&
|
|
|
|
rsa_exp != 0)
|
|
|
|
fatal("specified RSA exponent for a non-RSA key");
|
2000-04-25 17:57:10 +00:00
|
|
|
|
|
|
|
if (alg != DNS_KEYALG_DH && generator != 0)
|
2001-09-19 00:15:05 +00:00
|
|
|
fatal("specified DH generator for a non-DH key");
|
2000-04-25 17:57:10 +00:00
|
|
|
|
|
|
|
if (nametype == NULL)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("no nametype specified");
|
2000-04-25 17:57:10 +00:00
|
|
|
if (strcasecmp(nametype, "zone") == 0)
|
|
|
|
flags |= DNS_KEYOWNER_ZONE;
|
|
|
|
else if (strcasecmp(nametype, "host") == 0 ||
|
|
|
|
strcasecmp(nametype, "entity") == 0)
|
|
|
|
flags |= DNS_KEYOWNER_ENTITY;
|
|
|
|
else if (strcasecmp(nametype, "user") == 0)
|
|
|
|
flags |= DNS_KEYOWNER_USER;
|
|
|
|
else
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("invalid nametype %s", nametype);
|
2000-04-25 17:57:10 +00:00
|
|
|
|
2000-09-12 10:07:50 +00:00
|
|
|
if (classname != NULL) {
|
|
|
|
r.base = classname;
|
|
|
|
r.length = strlen(classname);
|
|
|
|
ret = dns_rdataclass_fromtext(&rdclass, &r);
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
fatal("unknown class %s",classname);
|
|
|
|
} else
|
|
|
|
rdclass = dns_rdataclass_in;
|
|
|
|
|
2000-04-25 17:57:10 +00:00
|
|
|
flags |= signatory;
|
|
|
|
|
1999-09-10 19:52:56 +00:00
|
|
|
if (protocol == -1) {
|
|
|
|
if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_USER)
|
|
|
|
protocol = DNS_KEYPROTO_EMAIL;
|
|
|
|
else
|
|
|
|
protocol = DNS_KEYPROTO_DNSSEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
|
|
|
|
if (size > 0)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("Specified null key with non-zero size");
|
1999-09-10 19:52:56 +00:00
|
|
|
if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("Specified null key with signing authority");
|
1999-09-10 19:52:56 +00:00
|
|
|
}
|
2000-04-25 17:57:10 +00:00
|
|
|
|
2000-05-24 23:13:32 +00:00
|
|
|
dns_fixedname_init(&fname);
|
|
|
|
name = dns_fixedname_name(&fname);
|
|
|
|
isc_buffer_init(&buf, argv[isc_commandline_index],
|
|
|
|
strlen(argv[isc_commandline_index]));
|
|
|
|
isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
|
|
|
|
ret = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
fatal("Invalid key name %s: %s", argv[isc_commandline_index],
|
|
|
|
isc_result_totext(ret));
|
1999-09-10 19:52:56 +00:00
|
|
|
|
|
|
|
switch(alg) {
|
2001-09-19 00:15:05 +00:00
|
|
|
case DNS_KEYALG_RSAMD5:
|
|
|
|
case DNS_KEYALG_RSASHA1:
|
2000-04-25 17:57:10 +00:00
|
|
|
param = rsa_exp;
|
|
|
|
break;
|
|
|
|
case DNS_KEYALG_DH:
|
|
|
|
param = generator;
|
|
|
|
break;
|
|
|
|
case DNS_KEYALG_DSA:
|
|
|
|
case DST_ALG_HMACMD5:
|
|
|
|
param = 0;
|
|
|
|
break;
|
1999-09-10 19:52:56 +00:00
|
|
|
}
|
|
|
|
|
2000-05-15 21:06:41 +00:00
|
|
|
if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
|
|
|
|
null_key = ISC_TRUE;
|
|
|
|
|
|
|
|
isc_buffer_init(&buf, filename, sizeof(filename) - 1);
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
do {
|
|
|
|
conflict = ISC_FALSE;
|
2000-05-15 21:06:41 +00:00
|
|
|
oldkey = NULL;
|
2000-05-10 17:57:53 +00:00
|
|
|
|
|
|
|
/* generate the key */
|
|
|
|
ret = dst_key_generate(name, alg, size, param, flags, protocol,
|
2000-09-12 10:07:50 +00:00
|
|
|
rdclass, mctx, &key);
|
2000-06-22 02:48:12 +00:00
|
|
|
isc_entropy_stopcallbacksources(ectx);
|
2000-05-10 17:57:53 +00:00
|
|
|
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
2000-08-14 04:43:17 +00:00
|
|
|
char namestr[DNS_NAME_FORMATSIZE];
|
|
|
|
char algstr[ALG_FORMATSIZE];
|
|
|
|
dns_name_format(name, namestr, sizeof namestr);
|
|
|
|
alg_format(alg, algstr, sizeof algstr);
|
2000-06-10 01:28:11 +00:00
|
|
|
fatal("failed to generate key %s/%s: %s\n",
|
2000-08-14 04:43:17 +00:00
|
|
|
namestr, algstr, dst_result_totext(ret));
|
2000-05-10 17:57:53 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-15 21:06:41 +00:00
|
|
|
/*
|
|
|
|
* Try to read a key with the same name, alg and id from disk.
|
2000-08-01 01:33:37 +00:00
|
|
|
* If there is one we must continue generating a new one
|
2000-05-15 21:06:41 +00:00
|
|
|
* unless we were asked to generate a null key, in which
|
|
|
|
* case we return failure.
|
2000-05-10 17:57:53 +00:00
|
|
|
*/
|
2000-08-01 01:33:37 +00:00
|
|
|
ret = dst_key_fromfile(name, dst_key_id(key), alg,
|
2000-06-06 22:01:49 +00:00
|
|
|
DST_TYPE_PRIVATE, NULL, mctx, &oldkey);
|
2000-05-15 21:06:41 +00:00
|
|
|
/* do not overwrite an existing key */
|
|
|
|
if (ret == ISC_R_SUCCESS) {
|
2000-05-19 00:20:59 +00:00
|
|
|
dst_key_free(&oldkey);
|
2000-05-10 17:57:53 +00:00
|
|
|
conflict = ISC_TRUE;
|
2000-05-15 21:06:41 +00:00
|
|
|
if (null_key)
|
|
|
|
break;
|
2000-05-10 17:57:53 +00:00
|
|
|
}
|
2000-05-24 17:13:29 +00:00
|
|
|
if (conflict == ISC_TRUE) {
|
|
|
|
if (verbose > 0) {
|
|
|
|
isc_buffer_clear(&buf);
|
2000-06-06 22:01:49 +00:00
|
|
|
ret = dst_key_buildfilename(key, 0, NULL, &buf);
|
2000-05-24 17:13:29 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s already exists, "
|
|
|
|
"generating a new key\n",
|
2000-05-24 23:54:43 +00:00
|
|
|
program, filename);
|
2000-05-24 17:13:29 +00:00
|
|
|
}
|
2000-06-01 02:32:12 +00:00
|
|
|
dst_key_free(&key);
|
2000-05-24 17:13:29 +00:00
|
|
|
}
|
2000-05-10 17:57:53 +00:00
|
|
|
|
2000-05-15 21:06:41 +00:00
|
|
|
} while (conflict == ISC_TRUE);
|
2000-05-10 17:57:53 +00:00
|
|
|
|
2000-05-15 21:06:41 +00:00
|
|
|
if (conflict)
|
2000-05-16 18:41:00 +00:00
|
|
|
fatal("cannot generate a null key when a key with id 0 "
|
|
|
|
"already exists");
|
2000-05-10 17:57:53 +00:00
|
|
|
|
2000-06-06 22:01:49 +00:00
|
|
|
ret = dst_key_tofile(key, DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, NULL);
|
2000-08-14 04:43:17 +00:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
char keystr[KEY_FORMATSIZE];
|
|
|
|
key_format(key, keystr, sizeof keystr);
|
|
|
|
fatal("failed to write key %s: %s\n", keystr,
|
|
|
|
isc_result_totext(ret));
|
|
|
|
}
|
2000-05-15 21:06:41 +00:00
|
|
|
|
|
|
|
isc_buffer_clear(&buf);
|
2000-06-06 22:01:49 +00:00
|
|
|
ret = dst_key_buildfilename(key, 0, NULL, &buf);
|
2000-05-15 21:06:41 +00:00
|
|
|
printf("%s\n", filename);
|
2000-05-19 00:20:59 +00:00
|
|
|
dst_key_free(&key);
|
2000-05-24 23:54:43 +00:00
|
|
|
|
2000-09-26 22:11:25 +00:00
|
|
|
cleanup_logging(&log);
|
2000-06-09 22:34:40 +00:00
|
|
|
cleanup_entropy(&ectx);
|
2000-06-06 22:01:49 +00:00
|
|
|
dst_lib_destroy();
|
2000-06-02 19:02:52 +00:00
|
|
|
if (verbose > 10)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
2000-12-11 19:24:30 +00:00
|
|
|
isc_mem_destroy(&mctx);
|
2000-05-08 20:12:46 +00:00
|
|
|
|
|
|
|
return (0);
|
1999-09-10 19:52:56 +00:00
|
|
|
}
|