]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/bin/dnssec/dnssec-keyfromlabel.c
Update BIND to 9.9.6-P1
[FreeBSD/stable/9.git] / contrib / bind9 / bin / dnssec / dnssec-keyfromlabel.c
1 /*
2  * Copyright (C) 2007-2012, 2014  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /*! \file */
18
19 #include <config.h>
20
21 #include <ctype.h>
22 #include <stdlib.h>
23
24 #include <isc/buffer.h>
25 #include <isc/commandline.h>
26 #include <isc/entropy.h>
27 #include <isc/mem.h>
28 #include <isc/region.h>
29 #include <isc/print.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
32
33 #include <dns/dnssec.h>
34 #include <dns/fixedname.h>
35 #include <dns/keyvalues.h>
36 #include <dns/log.h>
37 #include <dns/name.h>
38 #include <dns/rdataclass.h>
39 #include <dns/result.h>
40 #include <dns/secalg.h>
41
42 #include <dst/dst.h>
43
44 #include "dnssectool.h"
45
46 #define MAX_RSA 4096 /* should be long enough... */
47
48 const char *program = "dnssec-keyfromlabel";
49 int verbose;
50
51 #define DEFAULT_ALGORITHM "RSASHA1"
52 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
53
54 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
55                           " NSEC3DSA | NSEC3RSASHA1 |"
56                           " RSASHA256 | RSASHA512 | ECCGOST |"
57                           " ECDSAP256SHA256 | ECDSAP384SHA384";
58
59 ISC_PLATFORM_NORETURN_PRE static void
60 usage(void) ISC_PLATFORM_NORETURN_POST;
61
62 static void
63 usage(void) {
64         fprintf(stderr, "Usage:\n");
65         fprintf(stderr, "    %s -l label [options] name\n\n",
66                 program);
67         fprintf(stderr, "Version: %s\n", VERSION);
68         fprintf(stderr, "Required options:\n");
69         fprintf(stderr, "    -l label: label of the key pair\n");
70         fprintf(stderr, "    name: owner of the key\n");
71         fprintf(stderr, "Other options:\n");
72         fprintf(stderr, "    -a algorithm: %s\n", algs);
73         fprintf(stderr, "       (default: RSASHA1, or "
74                                "NSEC3RSASHA1 if using -3)\n");
75         fprintf(stderr, "    -3: use NSEC3-capable algorithm\n");
76         fprintf(stderr, "    -c class (default: IN)\n");
77 #ifdef USE_PKCS11
78         fprintf(stderr, "    -E enginename (default: pkcs11)\n");
79 #else
80         fprintf(stderr, "    -E enginename\n");
81 #endif
82         fprintf(stderr, "    -f keyflag: KSK | REVOKE\n");
83         fprintf(stderr, "    -K directory: directory in which to place "
84                         "key files\n");
85         fprintf(stderr, "    -k: generate a TYPE=KEY key\n");
86         fprintf(stderr, "    -L ttl: default key TTL\n");
87         fprintf(stderr, "    -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
88         fprintf(stderr, "        (DNSKEY generation defaults to ZONE\n");
89         fprintf(stderr, "    -p protocol: default: 3 [dnssec]\n");
90         fprintf(stderr, "    -t type: "
91                 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
92                 "(default: AUTHCONF)\n");
93         fprintf(stderr, "    -y: permit keys that might collide\n");
94         fprintf(stderr, "    -v verbose level\n");
95         fprintf(stderr, "    -V: print version information\n");
96         fprintf(stderr, "Date options:\n");
97         fprintf(stderr, "    -P date/[+-]offset: set key publication date\n");
98         fprintf(stderr, "    -A date/[+-]offset: set key activation date\n");
99         fprintf(stderr, "    -R date/[+-]offset: set key revocation date\n");
100         fprintf(stderr, "    -I date/[+-]offset: set key inactivation date\n");
101         fprintf(stderr, "    -D date/[+-]offset: set key deletion date\n");
102         fprintf(stderr, "    -G: generate key only; do not set -P or -A\n");
103         fprintf(stderr, "    -C: generate a backward-compatible key, omitting"
104                         " all dates\n");
105         fprintf(stderr, "    -S <key>: generate a successor to an existing "
106                                       "key\n");
107         fprintf(stderr, "    -i <interval>: prepublication interval for "
108                                            "successor key "
109                                            "(default: 30 days)\n");
110         fprintf(stderr, "Output:\n");
111         fprintf(stderr, "     K<name>+<alg>+<id>.key, "
112                         "K<name>+<alg>+<id>.private\n");
113
114         exit (-1);
115 }
116
117 int
118 main(int argc, char **argv) {
119         char            *algname = NULL, *freeit = NULL;
120         char            *nametype = NULL, *type = NULL;
121         const char      *directory = NULL;
122         const char      *predecessor = NULL;
123         dst_key_t       *prevkey = NULL;
124 #ifdef USE_PKCS11
125         const char      *engine = "pkcs11";
126 #else
127         const char      *engine = NULL;
128 #endif
129         char            *classname = NULL;
130         char            *endp;
131         dst_key_t       *key = NULL;
132         dns_fixedname_t fname;
133         dns_name_t      *name;
134         isc_uint16_t    flags = 0, kskflag = 0, revflag = 0;
135         dns_secalg_t    alg;
136         isc_boolean_t   oldstyle = ISC_FALSE;
137         isc_mem_t       *mctx = NULL;
138         int             ch;
139         int             protocol = -1, signatory = 0;
140         isc_result_t    ret;
141         isc_textregion_t r;
142         char            filename[255];
143         isc_buffer_t    buf;
144         isc_log_t       *log = NULL;
145         isc_entropy_t   *ectx = NULL;
146         dns_rdataclass_t rdclass;
147         int             options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
148         char            *label = NULL;
149         dns_ttl_t       ttl = 0;
150         isc_stdtime_t   publish = 0, activate = 0, revoke = 0;
151         isc_stdtime_t   inactive = 0, delete = 0;
152         isc_stdtime_t   now;
153         int             prepub = -1;
154         isc_boolean_t   setpub = ISC_FALSE, setact = ISC_FALSE;
155         isc_boolean_t   setrev = ISC_FALSE, setinact = ISC_FALSE;
156         isc_boolean_t   setdel = ISC_FALSE, setttl = ISC_FALSE;
157         isc_boolean_t   unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
158         isc_boolean_t   unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
159         isc_boolean_t   unsetdel = ISC_FALSE;
160         isc_boolean_t   genonly = ISC_FALSE;
161         isc_boolean_t   use_nsec3 = ISC_FALSE;
162         isc_boolean_t   avoid_collisions = ISC_TRUE;
163         isc_boolean_t   exact;
164         unsigned char   c;
165
166         if (argc == 1)
167                 usage();
168
169         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
170
171         dns_result_register();
172
173         isc_commandline_errprint = ISC_FALSE;
174
175         isc_stdtime_get(&now);
176
177 #define CMDLINE_FLAGS "3A:a:Cc:D:E:Ff:GhI:i:kK:L:l:n:P:p:R:S:t:v:Vy"
178         while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
179             switch (ch) {
180                 case '3':
181                         use_nsec3 = ISC_TRUE;
182                         break;
183                 case 'a':
184                         algname = isc_commandline_argument;
185                         break;
186                 case 'C':
187                         oldstyle = ISC_TRUE;
188                         break;
189                 case 'c':
190                         classname = isc_commandline_argument;
191                         break;
192                 case 'E':
193                         engine = isc_commandline_argument;
194                         break;
195                 case 'f':
196                         c = (unsigned char)(isc_commandline_argument[0]);
197                         if (toupper(c) == 'K')
198                                 kskflag = DNS_KEYFLAG_KSK;
199                         else if (toupper(c) == 'R')
200                                 revflag = DNS_KEYFLAG_REVOKE;
201                         else
202                                 fatal("unknown flag '%s'",
203                                       isc_commandline_argument);
204                         break;
205                 case 'K':
206                         directory = isc_commandline_argument;
207                         ret = try_dir(directory);
208                         if (ret != ISC_R_SUCCESS)
209                                 fatal("cannot open directory %s: %s",
210                                       directory, isc_result_totext(ret));
211                         break;
212                 case 'k':
213                         options |= DST_TYPE_KEY;
214                         break;
215                 case 'L':
216                         ttl = strtottl(isc_commandline_argument);
217                         setttl = ISC_TRUE;
218                         break;
219                 case 'l':
220                         label = isc_mem_strdup(mctx, isc_commandline_argument);
221                         break;
222                 case 'n':
223                         nametype = isc_commandline_argument;
224                         break;
225                 case 'p':
226                         protocol = strtol(isc_commandline_argument, &endp, 10);
227                         if (*endp != '\0' || protocol < 0 || protocol > 255)
228                                 fatal("-p must be followed by a number "
229                                       "[0..255]");
230                         break;
231                 case 't':
232                         type = isc_commandline_argument;
233                         break;
234                 case 'v':
235                         verbose = strtol(isc_commandline_argument, &endp, 0);
236                         if (*endp != '\0')
237                                 fatal("-v must be followed by a number");
238                         break;
239                 case 'y':
240                         avoid_collisions = ISC_FALSE;
241                         break;
242                 case 'G':
243                         genonly = ISC_TRUE;
244                         break;
245                 case 'P':
246                         if (setpub || unsetpub)
247                                 fatal("-P specified more than once");
248
249                         publish = strtotime(isc_commandline_argument,
250                                             now, now, &setpub);
251                         unsetpub = !setpub;
252                         break;
253                 case 'A':
254                         if (setact || unsetact)
255                                 fatal("-A specified more than once");
256
257                         activate = strtotime(isc_commandline_argument,
258                                              now, now, &setact);
259                         unsetact = !setact;
260                         break;
261                 case 'R':
262                         if (setrev || unsetrev)
263                                 fatal("-R specified more than once");
264
265                         revoke = strtotime(isc_commandline_argument,
266                                            now, now, &setrev);
267                         unsetrev = !setrev;
268                         break;
269                 case 'I':
270                         if (setinact || unsetinact)
271                                 fatal("-I specified more than once");
272
273                         inactive = strtotime(isc_commandline_argument,
274                                              now, now, &setinact);
275                         unsetinact = !setinact;
276                         break;
277                 case 'D':
278                         if (setdel || unsetdel)
279                                 fatal("-D specified more than once");
280
281                         delete = strtotime(isc_commandline_argument,
282                                            now, now, &setdel);
283                         unsetdel = !setdel;
284                         break;
285                 case 'S':
286                         predecessor = isc_commandline_argument;
287                         break;
288                 case 'i':
289                         prepub = strtottl(isc_commandline_argument);
290                         break;
291                 case 'F':
292                         /* Reserved for FIPS mode */
293                         /* FALLTHROUGH */
294                 case '?':
295                         if (isc_commandline_option != '?')
296                                 fprintf(stderr, "%s: invalid argument -%c\n",
297                                         program, isc_commandline_option);
298                         /* FALLTHROUGH */
299                 case 'h':
300                         /* Does not return. */
301                         usage();
302
303                 case 'V':
304                         /* Does not return. */
305                         version(program);
306
307                 default:
308                         fprintf(stderr, "%s: unhandled option -%c\n",
309                                 program, isc_commandline_option);
310                         exit(1);
311                 }
312         }
313
314         if (ectx == NULL)
315                 setup_entropy(mctx, NULL, &ectx);
316         ret = dst_lib_init2(mctx, ectx, engine,
317                             ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
318         if (ret != ISC_R_SUCCESS)
319                 fatal("could not initialize dst: %s",
320                       isc_result_totext(ret));
321
322         setup_logging(verbose, mctx, &log);
323
324         if (predecessor == NULL) {
325                 if (label == NULL)
326                         fatal("the key label was not specified");
327                 if (argc < isc_commandline_index + 1)
328                         fatal("the key name was not specified");
329                 if (argc > isc_commandline_index + 1)
330                         fatal("extraneous arguments");
331
332                 dns_fixedname_init(&fname);
333                 name = dns_fixedname_name(&fname);
334                 isc_buffer_init(&buf, argv[isc_commandline_index],
335                                 strlen(argv[isc_commandline_index]));
336                 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
337                 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
338                 if (ret != ISC_R_SUCCESS)
339                         fatal("invalid key name %s: %s",
340                               argv[isc_commandline_index],
341                               isc_result_totext(ret));
342
343                 if (strchr(label, ':') == NULL) {
344                         char *l;
345                         int len;
346
347                         len = strlen(label) + 8;
348                         l = isc_mem_allocate(mctx, len);
349                         if (l == NULL)
350                                 fatal("cannot allocate memory");
351                         snprintf(l, len, "pkcs11:%s", label);
352                         isc_mem_free(mctx, label);
353                         label = l;
354                 }
355
356                 if (algname == NULL) {
357                         if (use_nsec3)
358                                 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
359                         else
360                                 algname = strdup(DEFAULT_ALGORITHM);
361                         if (algname == NULL)
362                                 fatal("strdup failed");
363                         freeit = algname;
364                         if (verbose > 0)
365                                 fprintf(stderr, "no algorithm specified; "
366                                         "defaulting to %s\n", algname);
367                 }
368
369                 if (strcasecmp(algname, "RSA") == 0) {
370                         fprintf(stderr, "The use of RSA (RSAMD5) is not "
371                                         "recommended.\nIf you still wish to "
372                                         "use RSA (RSAMD5) please specify "
373                                         "\"-a RSAMD5\"\n");
374                         if (freeit != NULL)
375                                 free(freeit);
376                         return (1);
377                 } else {
378                         r.base = algname;
379                         r.length = strlen(algname);
380                         ret = dns_secalg_fromtext(&alg, &r);
381                         if (ret != ISC_R_SUCCESS)
382                                 fatal("unknown algorithm %s", algname);
383                         if (alg == DST_ALG_DH)
384                                 options |= DST_TYPE_KEY;
385                 }
386
387                 if (use_nsec3 &&
388                     alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
389                     alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512 &&
390                     alg != DST_ALG_ECCGOST &&
391                     alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
392                         fatal("%s is incompatible with NSEC3; "
393                               "do not use the -3 option", algname);
394                 }
395
396                 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
397                         if (strcasecmp(type, "NOAUTH") == 0)
398                                 flags |= DNS_KEYTYPE_NOAUTH;
399                         else if (strcasecmp(type, "NOCONF") == 0)
400                                 flags |= DNS_KEYTYPE_NOCONF;
401                         else if (strcasecmp(type, "NOAUTHCONF") == 0)
402                                 flags |= (DNS_KEYTYPE_NOAUTH |
403                                           DNS_KEYTYPE_NOCONF);
404                         else if (strcasecmp(type, "AUTHCONF") == 0)
405                                 /* nothing */;
406                         else
407                                 fatal("invalid type %s", type);
408                 }
409
410                 if (!oldstyle && prepub > 0) {
411                         if (setpub && setact && (activate - prepub) < publish)
412                                 fatal("Activation and publication dates "
413                                       "are closer together than the\n\t"
414                                       "prepublication interval.");
415
416                         if (!setpub && !setact) {
417                                 setpub = setact = ISC_TRUE;
418                                 publish = now;
419                                 activate = now + prepub;
420                         } else if (setpub && !setact) {
421                                 setact = ISC_TRUE;
422                                 activate = publish + prepub;
423                         } else if (setact && !setpub) {
424                                 setpub = ISC_TRUE;
425                                 publish = activate - prepub;
426                         }
427
428                         if ((activate - prepub) < now)
429                                 fatal("Time until activation is shorter "
430                                       "than the\n\tprepublication interval.");
431                 }
432         } else {
433                 char keystr[DST_KEY_FORMATSIZE];
434                 isc_stdtime_t when;
435                 int major, minor;
436
437                 if (prepub == -1)
438                         prepub = (30 * 86400);
439
440                 if (algname != NULL)
441                         fatal("-S and -a cannot be used together");
442                 if (nametype != NULL)
443                         fatal("-S and -n cannot be used together");
444                 if (type != NULL)
445                         fatal("-S and -t cannot be used together");
446                 if (setpub || unsetpub)
447                         fatal("-S and -P cannot be used together");
448                 if (setact || unsetact)
449                         fatal("-S and -A cannot be used together");
450                 if (use_nsec3)
451                         fatal("-S and -3 cannot be used together");
452                 if (oldstyle)
453                         fatal("-S and -C cannot be used together");
454                 if (genonly)
455                         fatal("-S and -G cannot be used together");
456
457                 ret = dst_key_fromnamedfile(predecessor, directory,
458                                             DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
459                                             mctx, &prevkey);
460                 if (ret != ISC_R_SUCCESS)
461                         fatal("Invalid keyfile %s: %s",
462                               predecessor, isc_result_totext(ret));
463                 if (!dst_key_isprivate(prevkey))
464                         fatal("%s is not a private key", predecessor);
465
466                 name = dst_key_name(prevkey);
467                 alg = dst_key_alg(prevkey);
468                 flags = dst_key_flags(prevkey);
469
470                 dst_key_format(prevkey, keystr, sizeof(keystr));
471                 dst_key_getprivateformat(prevkey, &major, &minor);
472                 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
473                         fatal("Key %s has incompatible format version %d.%d\n\t"
474                               "It is not possible to generate a successor key.",
475                               keystr, major, minor);
476
477                 ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
478                 if (ret != ISC_R_SUCCESS)
479                         fatal("Key %s has no activation date.\n\t"
480                               "You must use dnssec-settime -A to set one "
481                               "before generating a successor.", keystr);
482
483                 ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
484                 if (ret != ISC_R_SUCCESS)
485                         fatal("Key %s has no inactivation date.\n\t"
486                               "You must use dnssec-settime -I to set one "
487                               "before generating a successor.", keystr);
488
489                 publish = activate - prepub;
490                 if (publish < now)
491                         fatal("Key %s becomes inactive\n\t"
492                               "sooner than the prepublication period "
493                               "for the new key ends.\n\t"
494                               "Either change the inactivation date with "
495                               "dnssec-settime -I,\n\t"
496                               "or use the -i option to set a shorter "
497                               "prepublication interval.", keystr);
498
499                 ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
500                 if (ret != ISC_R_SUCCESS)
501                         fprintf(stderr, "%s: WARNING: Key %s has no removal "
502                                         "date;\n\t it will remain in the zone "
503                                         "indefinitely after rollover.\n\t "
504                                         "You can use dnssec-settime -D to "
505                                         "change this.\n", program, keystr);
506
507                 setpub = setact = ISC_TRUE;
508         }
509
510         if (nametype == NULL) {
511                 if ((options & DST_TYPE_KEY) != 0) /* KEY */
512                         fatal("no nametype specified");
513                 flags |= DNS_KEYOWNER_ZONE;     /* DNSKEY */
514         } else if (strcasecmp(nametype, "zone") == 0)
515                 flags |= DNS_KEYOWNER_ZONE;
516         else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
517                 if (strcasecmp(nametype, "host") == 0 ||
518                          strcasecmp(nametype, "entity") == 0)
519                         flags |= DNS_KEYOWNER_ENTITY;
520                 else if (strcasecmp(nametype, "user") == 0)
521                         flags |= DNS_KEYOWNER_USER;
522                 else
523                         fatal("invalid KEY nametype %s", nametype);
524         } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
525                 fatal("invalid DNSKEY nametype %s", nametype);
526
527         rdclass = strtoclass(classname);
528
529         if (directory == NULL)
530                 directory = ".";
531
532         if ((options & DST_TYPE_KEY) != 0)  /* KEY */
533                 flags |= signatory;
534         else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
535                 flags |= kskflag;
536                 flags |= revflag;
537         }
538
539         if (protocol == -1)
540                 protocol = DNS_KEYPROTO_DNSSEC;
541         else if ((options & DST_TYPE_KEY) == 0 &&
542                  protocol != DNS_KEYPROTO_DNSSEC)
543                 fatal("invalid DNSKEY protocol: %d", protocol);
544
545         if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
546                 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
547                         fatal("specified null key with signing authority");
548         }
549
550         if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
551             alg == DNS_KEYALG_DH)
552                 fatal("a key with algorithm '%s' cannot be a zone key",
553                       algname);
554
555         isc_buffer_init(&buf, filename, sizeof(filename) - 1);
556
557         /* associate the key */
558         ret = dst_key_fromlabel(name, alg, flags, protocol,
559                                 rdclass, engine, label, NULL, mctx, &key);
560         isc_entropy_stopcallbacksources(ectx);
561
562         if (ret != ISC_R_SUCCESS) {
563                 char namestr[DNS_NAME_FORMATSIZE];
564                 char algstr[DNS_SECALG_FORMATSIZE];
565                 dns_name_format(name, namestr, sizeof(namestr));
566                 dns_secalg_format(alg, algstr, sizeof(algstr));
567                 fatal("failed to get key %s/%s: %s\n",
568                       namestr, algstr, isc_result_totext(ret));
569                 /* NOTREACHED */
570                 exit(-1);
571         }
572
573         /*
574          * Set key timing metadata (unless using -C)
575          *
576          * Publish and activation dates are set to "now" by default, but
577          * can be overridden.  Creation date is always set to "now".
578          */
579         if (!oldstyle) {
580                 dst_key_settime(key, DST_TIME_CREATED, now);
581
582                 if (genonly && (setpub || setact))
583                         fatal("cannot use -G together with -P or -A options");
584
585                 if (setpub)
586                         dst_key_settime(key, DST_TIME_PUBLISH, publish);
587                 else if (setact)
588                         dst_key_settime(key, DST_TIME_PUBLISH, activate);
589                 else if (!genonly && !unsetpub)
590                         dst_key_settime(key, DST_TIME_PUBLISH, now);
591
592                 if (setact)
593                         dst_key_settime(key, DST_TIME_ACTIVATE, activate);
594                 else if (!genonly && !unsetact)
595                         dst_key_settime(key, DST_TIME_ACTIVATE, now);
596
597                 if (setrev) {
598                         if (kskflag == 0)
599                                 fprintf(stderr, "%s: warning: Key is "
600                                         "not flagged as a KSK, but -R "
601                                         "was used. Revoking a ZSK is "
602                                         "legal, but undefined.\n",
603                                         program);
604                         dst_key_settime(key, DST_TIME_REVOKE, revoke);
605                 }
606
607                 if (setinact)
608                         dst_key_settime(key, DST_TIME_INACTIVE, inactive);
609
610                 if (setdel)
611                         dst_key_settime(key, DST_TIME_DELETE, delete);
612         } else {
613                 if (setpub || setact || setrev || setinact ||
614                     setdel || unsetpub || unsetact ||
615                     unsetrev || unsetinact || unsetdel || genonly)
616                         fatal("cannot use -C together with "
617                               "-P, -A, -R, -I, -D, or -G options");
618                 /*
619                  * Compatibility mode: Private-key-format
620                  * should be set to 1.2.
621                  */
622                 dst_key_setprivateformat(key, 1, 2);
623         }
624
625         /* Set default key TTL */
626         if (setttl)
627                 dst_key_setttl(key, ttl);
628
629         /*
630          * Do not overwrite an existing key.  Warn LOUDLY if there
631          * is a risk of ID collision due to this key or another key
632          * being revoked.
633          */
634         if (key_collision(key, name, directory, mctx, &exact)) {
635                 isc_buffer_clear(&buf);
636                 ret = dst_key_buildfilename(key, 0, directory, &buf);
637                 if (ret != ISC_R_SUCCESS)
638                         fatal("dst_key_buildfilename returned: %s\n",
639                               isc_result_totext(ret));
640                 if (exact)
641                         fatal("%s: %s already exists\n", program, filename);
642
643                 if (avoid_collisions)
644                         fatal("%s: %s could collide with another key upon "
645                               "revokation\n", program, filename);
646
647                 fprintf(stderr, "%s: WARNING: Key %s could collide with "
648                                 "another key upon revokation.  If you plan "
649                                 "to revoke keys, destroy this key and "
650                                 "generate a different one.\n",
651                                 program, filename);
652         }
653
654         ret = dst_key_tofile(key, options, directory);
655         if (ret != ISC_R_SUCCESS) {
656                 char keystr[DST_KEY_FORMATSIZE];
657                 dst_key_format(key, keystr, sizeof(keystr));
658                 fatal("failed to write key %s: %s\n", keystr,
659                       isc_result_totext(ret));
660         }
661
662         isc_buffer_clear(&buf);
663         ret = dst_key_buildfilename(key, 0, NULL, &buf);
664         if (ret != ISC_R_SUCCESS)
665                 fatal("dst_key_buildfilename returned: %s\n",
666                       isc_result_totext(ret));
667         printf("%s\n", filename);
668         dst_key_free(&key);
669         if (prevkey != NULL)
670                 dst_key_free(&prevkey);
671
672         cleanup_logging(&log);
673         cleanup_entropy(&ectx);
674         dst_lib_destroy();
675         dns_name_destroy();
676         if (verbose > 10)
677                 isc_mem_stats(mctx, stdout);
678         isc_mem_free(mctx, label);
679         isc_mem_destroy(&mctx);
680
681         if (freeit != NULL)
682                 free(freeit);
683
684         return (0);
685 }