2 * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2000, 2001 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.2 2004/03/09 06:09:14 marka Exp $ */
25 #include <isc/buffer.h>
26 #include <isc/commandline.h>
27 #include <isc/entropy.h>
29 #include <isc/region.h>
30 #include <isc/string.h>
33 #include <dns/fixedname.h>
34 #include <dns/keyvalues.h>
37 #include <dns/rdataclass.h>
38 #include <dns/result.h>
39 #include <dns/secalg.h>
43 #include "dnssectool.h"
45 #define MAX_RSA 4096 /* should be long enough... */
47 const char *program
= "dnssec-keygen";
51 dsa_size_ok(int size
) {
52 return (ISC_TF(size
>= 512 && size
<= 1024 && size
% 64 == 0));
57 fprintf(stderr
, "Usage:\n");
58 fprintf(stderr
, " %s -a alg -b bits -n type [options] name\n\n",
60 fprintf(stderr
, "Required options:\n");
61 fprintf(stderr
, " -a algorithm: RSA | RSAMD5 | DH | DSA | HMAC-MD5"
63 fprintf(stderr
, " -b key size, in bits:\n");
64 fprintf(stderr
, " RSA:\t\t[512..%d]\n", MAX_RSA
);
65 fprintf(stderr
, " DH:\t\t[128..4096]\n");
66 fprintf(stderr
, " DSA:\t\t[512..1024] and divisible by 64\n");
67 fprintf(stderr
, " HMAC-MD5:\t[1..512]\n");
68 fprintf(stderr
, " -n nametype: ZONE | HOST | ENTITY | USER\n");
69 fprintf(stderr
, " name: owner of the key\n");
70 fprintf(stderr
, "Other options:\n");
71 fprintf(stderr
, " -c class (default: IN)\n");
72 fprintf(stderr
, " -e use large exponent (RSA only)\n");
73 fprintf(stderr
, " -g use specified generator (DH only)\n");
74 fprintf(stderr
, " -t type: AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
75 "(default: AUTHCONF)\n");
76 fprintf(stderr
, " -p protocol value "
77 "(default: 2 [email] for USER, 3 [dnssec] otherwise)\n");
78 fprintf(stderr
, " -s strength value this key signs DNS records "
79 "with (default: 0)\n");
80 fprintf(stderr
, " -r randomdev (a file containing random data)\n");
81 fprintf(stderr
, " -v verbose level\n");
82 fprintf(stderr
, "Output:\n");
83 fprintf(stderr
, " K<name>+<alg>+<id>.key, "
84 "K<name>+<alg>+<id>.private\n");
90 main(int argc
, char **argv
) {
91 char *algname
= NULL
, *nametype
= NULL
, *type
= NULL
;
92 char *classname
= NULL
;
93 char *randomfile
= NULL
;
95 dst_key_t
*key
= NULL
, *oldkey
;
96 dns_fixedname_t fname
;
98 isc_uint16_t flags
= 0;
100 isc_boolean_t conflict
= ISC_FALSE
, null_key
= ISC_FALSE
;
101 isc_mem_t
*mctx
= NULL
;
102 int ch
, rsa_exp
= 0, generator
= 0, param
= 0;
103 int protocol
= -1, size
= -1, signatory
= 0;
108 isc_log_t
*log
= NULL
;
109 isc_entropy_t
*ectx
= NULL
;
110 dns_rdataclass_t rdclass
;
112 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
114 if ((prog
= strrchr(argv
[0],'/')) == NULL
)
115 prog
= isc_mem_strdup(mctx
, argv
[0]);
117 prog
= isc_mem_strdup(mctx
, ++prog
);
119 fatal("out of memory");
124 dns_result_register();
126 while ((ch
= isc_commandline_parse(argc
, argv
,
127 "a:b:c:eg:n:t:p:s:hr:v:")) != -1)
131 algname
= isc_commandline_argument
;
134 size
= strtol(isc_commandline_argument
, &endp
, 10);
135 if (*endp
!= '\0' || size
< 0)
136 fatal("-b requires a non-negative number");
139 classname
= isc_commandline_argument
;
145 generator
= strtol(isc_commandline_argument
,
147 if (*endp
!= '\0' || generator
<= 0)
148 fatal("-g requires a positive number");
151 nametype
= isc_commandline_argument
;
152 if (nametype
== NULL
)
153 fatal("out of memory");
156 type
= isc_commandline_argument
;
158 fatal("out of memory");
161 protocol
= strtol(isc_commandline_argument
, &endp
, 10);
162 if (*endp
!= '\0' || protocol
< 0 || protocol
> 255)
163 fatal("-p must be followed by a number "
167 signatory
= strtol(isc_commandline_argument
,
169 if (*endp
!= '\0' || signatory
< 0 || signatory
> 15)
170 fatal("-s must be followed by a number "
174 randomfile
= isc_commandline_argument
;
178 verbose
= strtol(isc_commandline_argument
, &endp
, 0);
180 fatal("-v must be followed by a number");
186 fprintf(stderr
, "%s: invalid argument -%c\n",
192 setup_entropy(mctx
, randomfile
, &ectx
);
193 ret
= dst_lib_init(mctx
, ectx
,
194 ISC_ENTROPY_BLOCKING
| ISC_ENTROPY_GOODONLY
);
195 if (ret
!= ISC_R_SUCCESS
)
196 fatal("could not initialize dst");
198 setup_logging(verbose
, mctx
, &log
);
200 if (argc
< isc_commandline_index
+ 1)
201 fatal("the key name was not specified");
202 if (argc
> isc_commandline_index
+ 1)
203 fatal("extraneous arguments");
206 fatal("no algorithm was specified");
207 if (strcasecmp(algname
, "RSA") == 0)
208 alg
= DNS_KEYALG_RSA
;
209 else if (strcasecmp(algname
, "HMAC-MD5") == 0)
210 alg
= DST_ALG_HMACMD5
;
213 r
.length
= strlen(algname
);
214 ret
= dns_secalg_fromtext(&alg
, &r
);
215 if (ret
!= ISC_R_SUCCESS
)
216 fatal("unknown algorithm %s", algname
);
220 if (strcasecmp(type
, "NOAUTH") == 0)
221 flags
|= DNS_KEYTYPE_NOAUTH
;
222 else if (strcasecmp(type
, "NOCONF") == 0)
223 flags
|= DNS_KEYTYPE_NOCONF
;
224 else if (strcasecmp(type
, "NOAUTHCONF") == 0) {
225 flags
|= (DNS_KEYTYPE_NOAUTH
| DNS_KEYTYPE_NOCONF
);
229 else if (strcasecmp(type
, "AUTHCONF") == 0)
232 fatal("invalid type %s", type
);
236 fatal("key size not specified (-b option)");
240 if (size
!= 0 && (size
< 512 || size
> MAX_RSA
))
241 fatal("RSA key size %d out of range", size
);
244 if (size
!= 0 && (size
< 128 || size
> 4096))
245 fatal("DH key size %d out of range", size
);
248 if (size
!= 0 && !dsa_size_ok(size
))
249 fatal("Invalid DSS key size: %d", size
);
251 case DST_ALG_HMACMD5
:
252 if (size
< 1 || size
> 512)
253 fatal("HMAC-MD5 key size %d out of range", size
);
257 if (alg
!= DNS_KEYALG_RSA
&& rsa_exp
!= 0)
258 fatal("specified RSA exponent without RSA");
260 if (alg
!= DNS_KEYALG_DH
&& generator
!= 0)
261 fatal("specified DH generator without DH");
263 if (nametype
== NULL
)
264 fatal("no nametype specified");
265 if (strcasecmp(nametype
, "zone") == 0)
266 flags
|= DNS_KEYOWNER_ZONE
;
267 else if (strcasecmp(nametype
, "host") == 0 ||
268 strcasecmp(nametype
, "entity") == 0)
269 flags
|= DNS_KEYOWNER_ENTITY
;
270 else if (strcasecmp(nametype
, "user") == 0)
271 flags
|= DNS_KEYOWNER_USER
;
273 fatal("invalid nametype %s", nametype
);
275 if (classname
!= NULL
) {
277 r
.length
= strlen(classname
);
278 ret
= dns_rdataclass_fromtext(&rdclass
, &r
);
279 if (ret
!= ISC_R_SUCCESS
)
280 fatal("unknown class %s",classname
);
282 rdclass
= dns_rdataclass_in
;
286 if (protocol
== -1) {
287 if ((flags
& DNS_KEYFLAG_OWNERMASK
) == DNS_KEYOWNER_USER
)
288 protocol
= DNS_KEYPROTO_EMAIL
;
290 protocol
= DNS_KEYPROTO_DNSSEC
;
293 if ((flags
& DNS_KEYFLAG_TYPEMASK
) == DNS_KEYTYPE_NOKEY
) {
295 fatal("Specified null key with non-zero size");
296 if ((flags
& DNS_KEYFLAG_SIGNATORYMASK
) != 0)
297 fatal("Specified null key with signing authority");
300 dns_fixedname_init(&fname
);
301 name
= dns_fixedname_name(&fname
);
302 isc_buffer_init(&buf
, argv
[isc_commandline_index
],
303 strlen(argv
[isc_commandline_index
]));
304 isc_buffer_add(&buf
, strlen(argv
[isc_commandline_index
]));
305 ret
= dns_name_fromtext(name
, &buf
, dns_rootname
, ISC_FALSE
, NULL
);
306 if (ret
!= ISC_R_SUCCESS
)
307 fatal("Invalid key name %s: %s", argv
[isc_commandline_index
],
308 isc_result_totext(ret
));
318 case DST_ALG_HMACMD5
:
323 if ((flags
& DNS_KEYFLAG_TYPEMASK
) == DNS_KEYTYPE_NOKEY
)
326 isc_buffer_init(&buf
, filename
, sizeof(filename
) - 1);
329 conflict
= ISC_FALSE
;
332 /* generate the key */
333 ret
= dst_key_generate(name
, alg
, size
, param
, flags
, protocol
,
334 rdclass
, mctx
, &key
);
335 isc_entropy_stopcallbacksources(ectx
);
337 if (ret
!= ISC_R_SUCCESS
) {
338 char namestr
[DNS_NAME_FORMATSIZE
];
339 char algstr
[ALG_FORMATSIZE
];
340 dns_name_format(name
, namestr
, sizeof namestr
);
341 alg_format(alg
, algstr
, sizeof algstr
);
342 fatal("failed to generate key %s/%s: %s\n",
343 namestr
, algstr
, isc_result_totext(ret
));
348 * Try to read a key with the same name, alg and id from disk.
349 * If there is one we must continue generating a new one
350 * unless we were asked to generate a null key, in which
351 * case we return failure.
353 ret
= dst_key_fromfile(name
, dst_key_id(key
), alg
,
354 DST_TYPE_PRIVATE
, NULL
, mctx
, &oldkey
);
355 /* do not overwrite an existing key */
356 if (ret
== ISC_R_SUCCESS
) {
357 dst_key_free(&oldkey
);
362 if (conflict
== ISC_TRUE
) {
364 isc_buffer_clear(&buf
);
365 ret
= dst_key_buildfilename(key
, 0, NULL
, &buf
);
367 "%s: %s already exists, "
368 "generating a new key\n",
374 } while (conflict
== ISC_TRUE
);
377 fatal("cannot generate a null key when a key with id 0 "
380 ret
= dst_key_tofile(key
, DST_TYPE_PUBLIC
| DST_TYPE_PRIVATE
, NULL
);
381 if (ret
!= ISC_R_SUCCESS
) {
382 char keystr
[KEY_FORMATSIZE
];
383 key_format(key
, keystr
, sizeof keystr
);
384 fatal("failed to write key %s: %s\n", keystr
,
385 isc_result_totext(ret
));
388 isc_buffer_clear(&buf
);
389 ret
= dst_key_buildfilename(key
, 0, NULL
, &buf
);
390 printf("%s\n", filename
);
391 isc_mem_free(mctx
, prog
);
394 cleanup_logging(&log
);
395 cleanup_entropy(&ectx
);
398 isc_mem_stats(mctx
, stdout
);
399 isc_mem_destroy(&mctx
);