]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/bind9/bin/dnssec/dnssec-keygen.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / bind9 / bin / dnssec / dnssec-keygen.c
1 /*
2  * Portions Copyright (C) 2004-2008, 2010  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
10  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
12  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
18  *
19  * Permission to use, copy, modify, and/or distribute this software for any
20  * purpose with or without fee is hereby granted, provided that the above
21  * copyright notice and this permission notice appear in all copies.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
24  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
26  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30  */
31
32 /* $Id: dnssec-keygen.c,v 1.81.48.2 2010/01/15 23:47:31 tbox Exp $ */
33
34 /*! \file */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39
40 #include <isc/buffer.h>
41 #include <isc/commandline.h>
42 #include <isc/entropy.h>
43 #include <isc/mem.h>
44 #include <isc/region.h>
45 #include <isc/string.h>
46 #include <isc/util.h>
47
48 #include <dns/fixedname.h>
49 #include <dns/keyvalues.h>
50 #include <dns/log.h>
51 #include <dns/name.h>
52 #include <dns/rdataclass.h>
53 #include <dns/result.h>
54 #include <dns/secalg.h>
55
56 #include <dst/dst.h>
57
58 #include "dnssectool.h"
59
60 #define MAX_RSA 4096 /* should be long enough... */
61
62 const char *program = "dnssec-keygen";
63 int verbose;
64
65 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 | RSASHA256 |"
66                           " RSASHA512 | NSEC3DSA | NSEC3RSASHA1 | HMAC-MD5 |"
67                           " HMAC-SHA1 | HMAC-SHA224 | HMAC-SHA256 |"
68                           " HMAC-SHA384 | HMAC-SHA512";
69
70 static isc_boolean_t
71 dsa_size_ok(int size) {
72         return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
73 }
74
75 static void
76 usage(void) {
77         fprintf(stderr, "Usage:\n");
78         fprintf(stderr, "    %s -a alg -b bits [-n type] [options] name\n\n",
79                 program);
80         fprintf(stderr, "Version: %s\n", VERSION);
81         fprintf(stderr, "Required options:\n");
82         fprintf(stderr, "    -a algorithm: %s\n", algs);
83         fprintf(stderr, "    -b key size, in bits:\n");
84         fprintf(stderr, "        RSAMD5:\t\t[512..%d]\n", MAX_RSA);
85         fprintf(stderr, "        RSASHA1:\t\t[512..%d]\n", MAX_RSA);
86         fprintf(stderr, "        NSEC3RSASHA1:\t\t[512..%d]\n", MAX_RSA);
87         fprintf(stderr, "        RSASHA256:\t[512..%d]\n", MAX_RSA);
88         fprintf(stderr, "        RSASHA512:\t[1024..%d]\n", MAX_RSA);
89         fprintf(stderr, "        DH:\t\t[128..4096]\n");
90         fprintf(stderr, "        DSA:\t\t[512..1024] and divisible by 64\n");
91         fprintf(stderr, "        NSEC3DSA:\t\t[512..1024] and divisible by 64\n");
92         fprintf(stderr, "        HMAC-MD5:\t[1..512]\n");
93         fprintf(stderr, "        HMAC-SHA1:\t[1..160]\n");
94         fprintf(stderr, "        HMAC-SHA224:\t[1..224]\n");
95         fprintf(stderr, "        HMAC-SHA256:\t[1..256]\n");
96         fprintf(stderr, "        HMAC-SHA384:\t[1..384]\n");
97         fprintf(stderr, "        HMAC-SHA512:\t[1..512]\n");
98         fprintf(stderr, "    -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
99         fprintf(stderr, "        (DNSKEY generation defaults to ZONE\n");
100         fprintf(stderr, "    name: owner of the key\n");
101         fprintf(stderr, "Other options:\n");
102         fprintf(stderr, "    -c <class> (default: IN)\n");
103         fprintf(stderr, "    -d <digest bits> (0 => max, default)\n");
104         fprintf(stderr, "    -e use large exponent (RSAMD5/RSASHA1 only)\n");
105         fprintf(stderr, "    -f keyflag: KSK\n");
106         fprintf(stderr, "    -g <generator> use specified generator "
107                 "(DH only)\n");
108         fprintf(stderr, "    -t <type>: "
109                 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
110                 "(default: AUTHCONF)\n");
111         fprintf(stderr, "    -p <protocol>: "
112                "default: 3 [dnssec]\n");
113         fprintf(stderr, "    -s <strength> strength value this key signs DNS "
114                 "records with (default: 0)\n");
115         fprintf(stderr, "    -r <randomdev>: a file containing random data\n");
116         fprintf(stderr, "    -v <verbose level>\n");
117         fprintf(stderr, "    -k : generate a TYPE=KEY key\n");
118         fprintf(stderr, "Output:\n");
119         fprintf(stderr, "     K<name>+<alg>+<id>.key, "
120                 "K<name>+<alg>+<id>.private\n");
121
122         exit (-1);
123 }
124
125 int
126 main(int argc, char **argv) {
127         char            *algname = NULL, *nametype = NULL, *type = NULL;
128         char            *classname = NULL;
129         char            *endp;
130         dst_key_t       *key = NULL, *oldkey;
131         dns_fixedname_t fname;
132         dns_name_t      *name;
133         isc_uint16_t    flags = 0, ksk = 0;
134         dns_secalg_t    alg;
135         isc_boolean_t   conflict = ISC_FALSE, null_key = ISC_FALSE;
136         isc_mem_t       *mctx = NULL;
137         int             ch, rsa_exp = 0, generator = 0, param = 0;
138         int             protocol = -1, size = -1, signatory = 0;
139         isc_result_t    ret;
140         isc_textregion_t r;
141         char            filename[255];
142         isc_buffer_t    buf;
143         isc_log_t       *log = NULL;
144         isc_entropy_t   *ectx = NULL;
145         dns_rdataclass_t rdclass;
146         int             options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
147         int             dbits = 0;
148
149         if (argc == 1)
150                 usage();
151
152         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
153
154         dns_result_register();
155
156         isc_commandline_errprint = ISC_FALSE;
157
158         while ((ch = isc_commandline_parse(argc, argv,
159                                          "a:b:c:d:ef:g:kn:t:p:s:r:v:h")) != -1)
160         {
161             switch (ch) {
162                 case 'a':
163                         algname = isc_commandline_argument;
164                         break;
165                 case 'b':
166                         size = strtol(isc_commandline_argument, &endp, 10);
167                         if (*endp != '\0' || size < 0)
168                                 fatal("-b requires a non-negative number");
169                         break;
170                 case 'c':
171                         classname = isc_commandline_argument;
172                         break;
173                 case 'd':
174                         dbits = strtol(isc_commandline_argument, &endp, 10);
175                         if (*endp != '\0' || dbits < 0)
176                                 fatal("-d requires a non-negative number");
177                         break;
178                 case 'e':
179                         rsa_exp = 1;
180                         break;
181                 case 'f':
182                         if (strcasecmp(isc_commandline_argument, "KSK") == 0)
183                                 ksk = DNS_KEYFLAG_KSK;
184                         else
185                                 fatal("unknown flag '%s'",
186                                       isc_commandline_argument);
187                         break;
188                 case 'g':
189                         generator = strtol(isc_commandline_argument,
190                                            &endp, 10);
191                         if (*endp != '\0' || generator <= 0)
192                                 fatal("-g requires a positive number");
193                         break;
194                 case 'k':
195                         options |= DST_TYPE_KEY;
196                         break;
197                 case 'n':
198                         nametype = isc_commandline_argument;
199                         break;
200                 case 't':
201                         type = isc_commandline_argument;
202                         break;
203                 case 'p':
204                         protocol = strtol(isc_commandline_argument, &endp, 10);
205                         if (*endp != '\0' || protocol < 0 || protocol > 255)
206                                 fatal("-p must be followed by a number "
207                                       "[0..255]");
208                         break;
209                 case 's':
210                         signatory = strtol(isc_commandline_argument,
211                                            &endp, 10);
212                         if (*endp != '\0' || signatory < 0 || signatory > 15)
213                                 fatal("-s must be followed by a number "
214                                       "[0..15]");
215                         break;
216                 case 'r':
217                         setup_entropy(mctx, isc_commandline_argument, &ectx);
218                         break;
219                 case 'v':
220                         endp = NULL;
221                         verbose = strtol(isc_commandline_argument, &endp, 0);
222                         if (*endp != '\0')
223                                 fatal("-v must be followed by a number");
224                         break;
225
226                 case '?':
227                         if (isc_commandline_option != '?')
228                                 fprintf(stderr, "%s: invalid argument -%c\n",
229                                         program, isc_commandline_option);
230                 case 'h':
231                         usage();
232
233                 default:
234                         fprintf(stderr, "%s: unhandled option -%c\n",
235                                 program, isc_commandline_option);
236                         exit(1);
237                 }
238         }
239
240         if (ectx == NULL)
241                 setup_entropy(mctx, NULL, &ectx);
242         ret = dst_lib_init(mctx, ectx,
243                            ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
244         if (ret != ISC_R_SUCCESS)
245                 fatal("could not initialize dst");
246
247         setup_logging(verbose, mctx, &log);
248
249         if (argc < isc_commandline_index + 1)
250                 fatal("the key name was not specified");
251         if (argc > isc_commandline_index + 1)
252                 fatal("extraneous arguments");
253
254         if (algname == NULL)
255                 fatal("no algorithm was specified");
256         if (strcasecmp(algname, "RSA") == 0) {
257                 fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
258                                 "If you still wish to use RSA (RSAMD5) please "
259                                 "specify \"-a RSAMD5\"\n");
260                 return (1);
261         } else if (strcasecmp(algname, "HMAC-MD5") == 0) {
262                 options |= DST_TYPE_KEY;
263                 alg = DST_ALG_HMACMD5;
264         } else if (strcasecmp(algname, "HMAC-SHA1") == 0) {
265                 options |= DST_TYPE_KEY;
266                 alg = DST_ALG_HMACSHA1;
267         } else if (strcasecmp(algname, "HMAC-SHA224") == 0) {
268                 options |= DST_TYPE_KEY;
269                 alg = DST_ALG_HMACSHA224;
270         } else if (strcasecmp(algname, "HMAC-SHA256") == 0) {
271                 options |= DST_TYPE_KEY;
272                 alg = DST_ALG_HMACSHA256;
273         } else if (strcasecmp(algname, "HMAC-SHA384") == 0) {
274                 options |= DST_TYPE_KEY;
275                 alg = DST_ALG_HMACSHA384;
276         } else if (strcasecmp(algname, "HMAC-SHA512") == 0) {
277                 options |= DST_TYPE_KEY;
278                 alg = DST_ALG_HMACSHA512;
279         } else {
280                 r.base = algname;
281                 r.length = strlen(algname);
282                 ret = dns_secalg_fromtext(&alg, &r);
283                 if (ret != ISC_R_SUCCESS)
284                         fatal("unknown algorithm %s", algname);
285                 if (alg == DST_ALG_DH)
286                         options |= DST_TYPE_KEY;
287         }
288
289         if (type != NULL && (options & DST_TYPE_KEY) != 0) {
290                 if (strcasecmp(type, "NOAUTH") == 0)
291                         flags |= DNS_KEYTYPE_NOAUTH;
292                 else if (strcasecmp(type, "NOCONF") == 0)
293                         flags |= DNS_KEYTYPE_NOCONF;
294                 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
295                         flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
296                         if (size < 0)
297                                 size = 0;
298                 }
299                 else if (strcasecmp(type, "AUTHCONF") == 0)
300                         /* nothing */;
301                 else
302                         fatal("invalid type %s", type);
303         }
304
305         if (size < 0)
306                 fatal("key size not specified (-b option)");
307
308         switch (alg) {
309         case DNS_KEYALG_RSAMD5:
310         case DNS_KEYALG_RSASHA1:
311         case DNS_KEYALG_NSEC3RSASHA1:
312         case DNS_KEYALG_RSASHA256:
313                 if (size != 0 && (size < 512 || size > MAX_RSA))
314                         fatal("RSA key size %d out of range", size);
315                 break;
316         case DNS_KEYALG_RSASHA512:
317                 if (size != 0 && (size < 1024 || size > MAX_RSA))
318                         fatal("RSA key size %d out of range", size);
319                 break;
320         case DNS_KEYALG_DH:
321                 if (size != 0 && (size < 128 || size > 4096))
322                         fatal("DH key size %d out of range", size);
323                 break;
324         case DNS_KEYALG_DSA:
325         case DNS_KEYALG_NSEC3DSA:
326                 if (size != 0 && !dsa_size_ok(size))
327                         fatal("invalid DSS key size: %d", size);
328                 break;
329         case DST_ALG_HMACMD5:
330                 if (size < 1 || size > 512)
331                         fatal("HMAC-MD5 key size %d out of range", size);
332                 if (dbits != 0 && (dbits < 80 || dbits > 128))
333                         fatal("HMAC-MD5 digest bits %d out of range", dbits);
334                 if ((dbits % 8) != 0)
335                         fatal("HMAC-MD5 digest bits %d not divisible by 8",
336                               dbits);
337                 break;
338         case DST_ALG_HMACSHA1:
339                 if (size < 1 || size > 160)
340                         fatal("HMAC-SHA1 key size %d out of range", size);
341                 if (dbits != 0 && (dbits < 80 || dbits > 160))
342                         fatal("HMAC-SHA1 digest bits %d out of range", dbits);
343                 if ((dbits % 8) != 0)
344                         fatal("HMAC-SHA1 digest bits %d not divisible by 8",
345                               dbits);
346                 break;
347         case DST_ALG_HMACSHA224:
348                 if (size < 1 || size > 224)
349                         fatal("HMAC-SHA224 key size %d out of range", size);
350                 if (dbits != 0 && (dbits < 112 || dbits > 224))
351                         fatal("HMAC-SHA224 digest bits %d out of range", dbits);
352                 if ((dbits % 8) != 0)
353                         fatal("HMAC-SHA224 digest bits %d not divisible by 8",
354                               dbits);
355                 break;
356         case DST_ALG_HMACSHA256:
357                 if (size < 1 || size > 256)
358                         fatal("HMAC-SHA256 key size %d out of range", size);
359                 if (dbits != 0 && (dbits < 128 || dbits > 256))
360                         fatal("HMAC-SHA256 digest bits %d out of range", dbits);
361                 if ((dbits % 8) != 0)
362                         fatal("HMAC-SHA256 digest bits %d not divisible by 8",
363                               dbits);
364                 break;
365         case DST_ALG_HMACSHA384:
366                 if (size < 1 || size > 384)
367                         fatal("HMAC-384 key size %d out of range", size);
368                 if (dbits != 0 && (dbits < 192 || dbits > 384))
369                         fatal("HMAC-SHA384 digest bits %d out of range", dbits);
370                 if ((dbits % 8) != 0)
371                         fatal("HMAC-SHA384 digest bits %d not divisible by 8",
372                               dbits);
373                 break;
374         case DST_ALG_HMACSHA512:
375                 if (size < 1 || size > 512)
376                         fatal("HMAC-SHA512 key size %d out of range", size);
377                 if (dbits != 0 && (dbits < 256 || dbits > 512))
378                         fatal("HMAC-SHA512 digest bits %d out of range", dbits);
379                 if ((dbits % 8) != 0)
380                         fatal("HMAC-SHA512 digest bits %d not divisible by 8",
381                               dbits);
382                 break;
383         }
384
385         if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1 ||
386               alg == DNS_KEYALG_NSEC3RSASHA1 || alg == DNS_KEYALG_RSASHA256 ||
387               alg == DNS_KEYALG_RSASHA512) && rsa_exp != 0)
388                 fatal("specified RSA exponent for a non-RSA key");
389
390         if (alg != DNS_KEYALG_DH && generator != 0)
391                 fatal("specified DH generator for a non-DH key");
392
393         if (nametype == NULL) {
394                 if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
395                         fatal("no nametype specified");
396                 flags |= DNS_KEYOWNER_ZONE;     /* DNSKEY */
397         } else if (strcasecmp(nametype, "zone") == 0)
398                 flags |= DNS_KEYOWNER_ZONE;
399         else if ((options & DST_TYPE_KEY) != 0) { /* KEY / HMAC */
400                 if (strcasecmp(nametype, "host") == 0 ||
401                          strcasecmp(nametype, "entity") == 0)
402                         flags |= DNS_KEYOWNER_ENTITY;
403                 else if (strcasecmp(nametype, "user") == 0)
404                         flags |= DNS_KEYOWNER_USER;
405                 else
406                         fatal("invalid KEY nametype %s", nametype);
407         } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
408                 fatal("invalid DNSKEY nametype %s", nametype);
409
410         rdclass = strtoclass(classname);
411
412         if ((options & DST_TYPE_KEY) != 0)  /* KEY / HMAC */
413                 flags |= signatory;
414         else if ((flags & DNS_KEYOWNER_ZONE) != 0) /* DNSKEY */
415                 flags |= ksk;
416
417         if (protocol == -1)
418                 protocol = DNS_KEYPROTO_DNSSEC;
419         else if ((options & DST_TYPE_KEY) == 0 &&
420                  protocol != DNS_KEYPROTO_DNSSEC)
421                 fatal("invalid DNSKEY protocol: %d", protocol);
422
423         if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
424                 if (size > 0)
425                         fatal("specified null key with non-zero size");
426                 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
427                         fatal("specified null key with signing authority");
428         }
429
430         if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
431             (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5 ||
432              alg == DST_ALG_HMACSHA1 || alg == DST_ALG_HMACSHA224 ||
433              alg == DST_ALG_HMACSHA256 || alg == DST_ALG_HMACSHA384 ||
434              alg == DST_ALG_HMACSHA512))
435                 fatal("a key with algorithm '%s' cannot be a zone key",
436                       algname);
437
438         dns_fixedname_init(&fname);
439         name = dns_fixedname_name(&fname);
440         isc_buffer_init(&buf, argv[isc_commandline_index],
441                         strlen(argv[isc_commandline_index]));
442         isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
443         ret = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
444         if (ret != ISC_R_SUCCESS)
445                 fatal("invalid key name %s: %s", argv[isc_commandline_index],
446                       isc_result_totext(ret));
447
448         switch(alg) {
449         case DNS_KEYALG_RSAMD5:
450         case DNS_KEYALG_RSASHA1:
451         case DNS_KEYALG_NSEC3RSASHA1:
452         case DNS_KEYALG_RSASHA256:
453         case DNS_KEYALG_RSASHA512:
454                 param = rsa_exp;
455                 break;
456         case DNS_KEYALG_DH:
457                 param = generator;
458                 break;
459         case DNS_KEYALG_DSA:
460         case DNS_KEYALG_NSEC3DSA:
461         case DST_ALG_HMACMD5:
462         case DST_ALG_HMACSHA1:
463         case DST_ALG_HMACSHA224:
464         case DST_ALG_HMACSHA256:
465         case DST_ALG_HMACSHA384:
466         case DST_ALG_HMACSHA512:
467                 param = 0;
468                 break;
469         }
470
471         if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
472                 null_key = ISC_TRUE;
473
474         isc_buffer_init(&buf, filename, sizeof(filename) - 1);
475
476         do {
477                 conflict = ISC_FALSE;
478                 oldkey = NULL;
479
480                 /* generate the key */
481                 ret = dst_key_generate(name, alg, size, param, flags, protocol,
482                                        rdclass, mctx, &key);
483                 isc_entropy_stopcallbacksources(ectx);
484
485                 if (ret != ISC_R_SUCCESS) {
486                         char namestr[DNS_NAME_FORMATSIZE];
487                         char algstr[ALG_FORMATSIZE];
488                         dns_name_format(name, namestr, sizeof(namestr));
489                         alg_format(alg, algstr, sizeof(algstr));
490                         fatal("failed to generate key %s/%s: %s\n",
491                               namestr, algstr, isc_result_totext(ret));
492                         exit(-1);
493                 }
494
495                 dst_key_setbits(key, dbits);
496
497                 /*
498                  * Try to read a key with the same name, alg and id from disk.
499                  * If there is one we must continue generating a new one
500                  * unless we were asked to generate a null key, in which
501                  * case we return failure.
502                  */
503                 ret = dst_key_fromfile(name, dst_key_id(key), alg,
504                                        DST_TYPE_PRIVATE, NULL, mctx, &oldkey);
505                 /* do not overwrite an existing key  */
506                 if (ret == ISC_R_SUCCESS) {
507                         dst_key_free(&oldkey);
508                         conflict = ISC_TRUE;
509                         if (null_key)
510                                 break;
511                 }
512                 if (conflict == ISC_TRUE) {
513                         if (verbose > 0) {
514                                 isc_buffer_clear(&buf);
515                                 ret = dst_key_buildfilename(key, 0, NULL, &buf);
516                                 fprintf(stderr,
517                                         "%s: %s already exists, "
518                                         "generating a new key\n",
519                                         program, filename);
520                         }
521                         dst_key_free(&key);
522                 }
523
524         } while (conflict == ISC_TRUE);
525
526         if (conflict)
527                 fatal("cannot generate a null key when a key with id 0 "
528                       "already exists");
529
530         ret = dst_key_tofile(key, options, NULL);
531         if (ret != ISC_R_SUCCESS) {
532                 char keystr[KEY_FORMATSIZE];
533                 key_format(key, keystr, sizeof(keystr));
534                 fatal("failed to write key %s: %s\n", keystr,
535                       isc_result_totext(ret));
536         }
537
538         isc_buffer_clear(&buf);
539         ret = dst_key_buildfilename(key, 0, NULL, &buf);
540         printf("%s\n", filename);
541         dst_key_free(&key);
542
543         cleanup_logging(&log);
544         cleanup_entropy(&ectx);
545         dst_lib_destroy();
546         dns_name_destroy();
547         if (verbose > 10)
548                 isc_mem_stats(mctx, stdout);
549         isc_mem_destroy(&mctx);
550
551         return (0);
552 }