Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / contrib / bind-9.3 / bin / dnssec / dnssec-keygen.c
blob7feaf7c3d977fe412160ed31558977a58ca38090
1 /*
2 * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2000-2003 Internet Software Consortium.
4 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
13 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* $Id: dnssec-keygen.c,v 1.48.2.1.10.11 2004/06/11 01:17:34 marka Exp $ */
21 #include <config.h>
23 #include <stdlib.h>
25 #include <isc/buffer.h>
26 #include <isc/commandline.h>
27 #include <isc/entropy.h>
28 #include <isc/mem.h>
29 #include <isc/region.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
33 #include <dns/fixedname.h>
34 #include <dns/keyvalues.h>
35 #include <dns/log.h>
36 #include <dns/name.h>
37 #include <dns/rdataclass.h>
38 #include <dns/result.h>
39 #include <dns/secalg.h>
41 #include <dst/dst.h>
43 #include "dnssectool.h"
45 #define MAX_RSA 4096 /* should be long enough... */
47 const char *program = "dnssec-keygen";
48 int verbose;
50 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 | HMAC-MD5";
52 static isc_boolean_t
53 dsa_size_ok(int size) {
54 return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
57 static void
58 usage(void) {
59 fprintf(stderr, "Usage:\n");
60 fprintf(stderr, " %s -a alg -b bits -n type [options] name\n\n",
61 program);
62 fprintf(stderr, "Version: %s\n", VERSION);
63 fprintf(stderr, "Required options:\n");
64 fprintf(stderr, " -a algorithm: %s\n", algs);
65 fprintf(stderr, " -b key size, in bits:\n");
66 fprintf(stderr, " RSAMD5:\t\t[512..%d]\n", MAX_RSA);
67 fprintf(stderr, " RSASHA1:\t\t[512..%d]\n", MAX_RSA);
68 fprintf(stderr, " DH:\t\t[128..4096]\n");
69 fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
70 fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
71 fprintf(stderr, " -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
72 fprintf(stderr, " name: owner of the key\n");
73 fprintf(stderr, "Other options:\n");
74 fprintf(stderr, " -c <class> (default: IN)\n");
75 fprintf(stderr, " -e use large exponent (RSAMD5/RSASHA1 only)\n");
76 fprintf(stderr, " -f keyflag: KSK\n");
77 fprintf(stderr, " -g <generator> use specified generator "
78 "(DH only)\n");
79 fprintf(stderr, " -t <type>: "
80 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
81 "(default: AUTHCONF)\n");
82 fprintf(stderr, " -p <protocol>: "
83 "default: 3 [dnssec]\n");
84 fprintf(stderr, " -s <strength> strength value this key signs DNS "
85 "records with (default: 0)\n");
86 fprintf(stderr, " -r <randomdev>: a file containing random data\n");
87 fprintf(stderr, " -v <verbose level>\n");
88 fprintf(stderr, " -k : generate a TYPE=KEY key\n");
89 fprintf(stderr, "Output:\n");
90 fprintf(stderr, " K<name>+<alg>+<id>.key, "
91 "K<name>+<alg>+<id>.private\n");
93 exit (-1);
96 int
97 main(int argc, char **argv) {
98 char *algname = NULL, *nametype = NULL, *type = NULL;
99 char *classname = NULL;
100 char *endp;
101 dst_key_t *key = NULL, *oldkey;
102 dns_fixedname_t fname;
103 dns_name_t *name;
104 isc_uint16_t flags = 0, ksk = 0;
105 dns_secalg_t alg;
106 isc_boolean_t conflict = ISC_FALSE, null_key = ISC_FALSE;
107 isc_mem_t *mctx = NULL;
108 int ch, rsa_exp = 0, generator = 0, param = 0;
109 int protocol = -1, size = -1, signatory = 0;
110 isc_result_t ret;
111 isc_textregion_t r;
112 char filename[255];
113 isc_buffer_t buf;
114 isc_log_t *log = NULL;
115 isc_entropy_t *ectx = NULL;
116 dns_rdataclass_t rdclass;
117 int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
119 if (argc == 1)
120 usage();
122 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
124 dns_result_register();
126 while ((ch = isc_commandline_parse(argc, argv,
127 "a:b:c:ef:g:kn:t:p:s:r:v:h")) != -1)
129 switch (ch) {
130 case 'a':
131 algname = isc_commandline_argument;
132 break;
133 case 'b':
134 size = strtol(isc_commandline_argument, &endp, 10);
135 if (*endp != '\0' || size < 0)
136 fatal("-b requires a non-negative number");
137 break;
138 case 'c':
139 classname = isc_commandline_argument;
140 break;
141 case 'e':
142 rsa_exp = 1;
143 break;
144 case 'f':
145 if (strcasecmp(isc_commandline_argument, "KSK") == 0)
146 ksk = DNS_KEYFLAG_KSK;
147 else
148 fatal("unknown flag '%s'",
149 isc_commandline_argument);
150 break;
151 case 'g':
152 generator = strtol(isc_commandline_argument,
153 &endp, 10);
154 if (*endp != '\0' || generator <= 0)
155 fatal("-g requires a positive number");
156 break;
157 case 'k':
158 options |= DST_TYPE_KEY;
159 break;
160 case 'n':
161 nametype = isc_commandline_argument;
162 break;
163 case 't':
164 type = isc_commandline_argument;
165 break;
166 case 'p':
167 protocol = strtol(isc_commandline_argument, &endp, 10);
168 if (*endp != '\0' || protocol < 0 || protocol > 255)
169 fatal("-p must be followed by a number "
170 "[0..255]");
171 break;
172 case 's':
173 signatory = strtol(isc_commandline_argument,
174 &endp, 10);
175 if (*endp != '\0' || signatory < 0 || signatory > 15)
176 fatal("-s must be followed by a number "
177 "[0..15]");
178 break;
179 case 'r':
180 setup_entropy(mctx, isc_commandline_argument, &ectx);
181 break;
182 case 'v':
183 endp = NULL;
184 verbose = strtol(isc_commandline_argument, &endp, 0);
185 if (*endp != '\0')
186 fatal("-v must be followed by a number");
187 break;
189 case 'h':
190 usage();
191 default:
192 fprintf(stderr, "%s: invalid argument -%c\n",
193 program, ch);
194 usage();
198 if (ectx == NULL)
199 setup_entropy(mctx, NULL, &ectx);
200 ret = dst_lib_init(mctx, ectx,
201 ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
202 if (ret != ISC_R_SUCCESS)
203 fatal("could not initialize dst");
205 setup_logging(verbose, mctx, &log);
207 if (argc < isc_commandline_index + 1)
208 fatal("the key name was not specified");
209 if (argc > isc_commandline_index + 1)
210 fatal("extraneous arguments");
212 if (algname == NULL)
213 fatal("no algorithm was specified");
214 if (strcasecmp(algname, "HMAC-MD5") == 0) {
215 options |= DST_TYPE_KEY;
216 alg = DST_ALG_HMACMD5;
217 } else {
218 r.base = algname;
219 r.length = strlen(algname);
220 ret = dns_secalg_fromtext(&alg, &r);
221 if (ret != ISC_R_SUCCESS)
222 fatal("unknown algorithm %s", algname);
223 if (alg == DST_ALG_DH)
224 options |= DST_TYPE_KEY;
227 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
228 if (strcasecmp(type, "NOAUTH") == 0)
229 flags |= DNS_KEYTYPE_NOAUTH;
230 else if (strcasecmp(type, "NOCONF") == 0)
231 flags |= DNS_KEYTYPE_NOCONF;
232 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
233 flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
234 if (size < 0)
235 size = 0;
237 else if (strcasecmp(type, "AUTHCONF") == 0)
238 /* nothing */;
239 else
240 fatal("invalid type %s", type);
243 if (size < 0)
244 fatal("key size not specified (-b option)");
246 switch (alg) {
247 case DNS_KEYALG_RSAMD5:
248 case DNS_KEYALG_RSASHA1:
249 if (size != 0 && (size < 512 || size > MAX_RSA))
250 fatal("RSA key size %d out of range", size);
251 break;
252 case DNS_KEYALG_DH:
253 if (size != 0 && (size < 128 || size > 4096))
254 fatal("DH key size %d out of range", size);
255 break;
256 case DNS_KEYALG_DSA:
257 if (size != 0 && !dsa_size_ok(size))
258 fatal("invalid DSS key size: %d", size);
259 break;
260 case DST_ALG_HMACMD5:
261 if (size < 1 || size > 512)
262 fatal("HMAC-MD5 key size %d out of range", size);
263 break;
266 if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1) &&
267 rsa_exp != 0)
268 fatal("specified RSA exponent for a non-RSA key");
270 if (alg != DNS_KEYALG_DH && generator != 0)
271 fatal("specified DH generator for a non-DH key");
273 if (nametype == NULL)
274 fatal("no nametype specified");
275 if (strcasecmp(nametype, "zone") == 0)
276 flags |= DNS_KEYOWNER_ZONE;
277 else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
278 if (strcasecmp(nametype, "host") == 0 ||
279 strcasecmp(nametype, "entity") == 0)
280 flags |= DNS_KEYOWNER_ENTITY;
281 else if (strcasecmp(nametype, "user") == 0)
282 flags |= DNS_KEYOWNER_USER;
283 else
284 fatal("invalid KEY nametype %s", nametype);
285 } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
286 fatal("invalid DNSKEY nametype %s", nametype);
288 rdclass = strtoclass(classname);
290 if ((options & DST_TYPE_KEY) != 0) /* KEY */
291 flags |= signatory;
292 else if ((flags & DNS_KEYOWNER_ZONE) != 0) /* DNSKEY */
293 flags |= ksk;
295 if (protocol == -1)
296 protocol = DNS_KEYPROTO_DNSSEC;
297 else if ((options & DST_TYPE_KEY) == 0 &&
298 protocol != DNS_KEYPROTO_DNSSEC)
299 fatal("invalid DNSKEY protocol: %d", protocol);
301 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
302 if (size > 0)
303 fatal("specified null key with non-zero size");
304 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
305 fatal("specified null key with signing authority");
308 if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
309 (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5))
310 fatal("a key with algorithm '%s' cannot be a zone key",
311 algname);
313 dns_fixedname_init(&fname);
314 name = dns_fixedname_name(&fname);
315 isc_buffer_init(&buf, argv[isc_commandline_index],
316 strlen(argv[isc_commandline_index]));
317 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
318 ret = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
319 if (ret != ISC_R_SUCCESS)
320 fatal("invalid key name %s: %s", argv[isc_commandline_index],
321 isc_result_totext(ret));
323 switch(alg) {
324 case DNS_KEYALG_RSAMD5:
325 case DNS_KEYALG_RSASHA1:
326 param = rsa_exp;
327 break;
328 case DNS_KEYALG_DH:
329 param = generator;
330 break;
331 case DNS_KEYALG_DSA:
332 case DST_ALG_HMACMD5:
333 param = 0;
334 break;
337 if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
338 null_key = ISC_TRUE;
340 isc_buffer_init(&buf, filename, sizeof(filename) - 1);
342 do {
343 conflict = ISC_FALSE;
344 oldkey = NULL;
346 /* generate the key */
347 ret = dst_key_generate(name, alg, size, param, flags, protocol,
348 rdclass, mctx, &key);
349 isc_entropy_stopcallbacksources(ectx);
351 if (ret != ISC_R_SUCCESS) {
352 char namestr[DNS_NAME_FORMATSIZE];
353 char algstr[ALG_FORMATSIZE];
354 dns_name_format(name, namestr, sizeof(namestr));
355 alg_format(alg, algstr, sizeof(algstr));
356 fatal("failed to generate key %s/%s: %s\n",
357 namestr, algstr, isc_result_totext(ret));
358 exit(-1);
362 * Try to read a key with the same name, alg and id from disk.
363 * If there is one we must continue generating a new one
364 * unless we were asked to generate a null key, in which
365 * case we return failure.
367 ret = dst_key_fromfile(name, dst_key_id(key), alg,
368 DST_TYPE_PRIVATE, NULL, mctx, &oldkey);
369 /* do not overwrite an existing key */
370 if (ret == ISC_R_SUCCESS) {
371 dst_key_free(&oldkey);
372 conflict = ISC_TRUE;
373 if (null_key)
374 break;
376 if (conflict == ISC_TRUE) {
377 if (verbose > 0) {
378 isc_buffer_clear(&buf);
379 ret = dst_key_buildfilename(key, 0, NULL, &buf);
380 fprintf(stderr,
381 "%s: %s already exists, "
382 "generating a new key\n",
383 program, filename);
385 dst_key_free(&key);
388 } while (conflict == ISC_TRUE);
390 if (conflict)
391 fatal("cannot generate a null key when a key with id 0 "
392 "already exists");
394 ret = dst_key_tofile(key, options, NULL);
395 if (ret != ISC_R_SUCCESS) {
396 char keystr[KEY_FORMATSIZE];
397 key_format(key, keystr, sizeof(keystr));
398 fatal("failed to write key %s: %s\n", keystr,
399 isc_result_totext(ret));
402 isc_buffer_clear(&buf);
403 ret = dst_key_buildfilename(key, 0, NULL, &buf);
404 printf("%s\n", filename);
405 dst_key_free(&key);
407 cleanup_logging(&log);
408 cleanup_entropy(&ectx);
409 dst_lib_destroy();
410 if (verbose > 10)
411 isc_mem_stats(mctx, stdout);
412 isc_mem_destroy(&mctx);
414 return (0);