]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/bin/dnssec/dnssec-keyfromlabel.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / bin / dnssec / dnssec-keyfromlabel.c
1 /*
2  * Copyright (C) 2007-2012  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 /* $Id: dnssec-keyfromlabel.c,v 1.38 2011/11/30 00:48:51 marka Exp $ */
18
19 /*! \file */
20
21 #include <config.h>
22
23 #include <ctype.h>
24 #include <stdlib.h>
25
26 #include <isc/buffer.h>
27 #include <isc/commandline.h>
28 #include <isc/entropy.h>
29 #include <isc/mem.h>
30 #include <isc/region.h>
31 #include <isc/print.h>
32 #include <isc/string.h>
33 #include <isc/util.h>
34
35 #include <dns/dnssec.h>
36 #include <dns/fixedname.h>
37 #include <dns/keyvalues.h>
38 #include <dns/log.h>
39 #include <dns/name.h>
40 #include <dns/rdataclass.h>
41 #include <dns/result.h>
42 #include <dns/secalg.h>
43
44 #include <dst/dst.h>
45
46 #include "dnssectool.h"
47
48 #define MAX_RSA 4096 /* should be long enough... */
49
50 const char *program = "dnssec-keyfromlabel";
51 int verbose;
52
53 #define DEFAULT_ALGORITHM "RSASHA1"
54 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
55
56 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
57                           " NSEC3DSA | NSEC3RSASHA1 |"
58                           " RSASHA256 | RSASHA512 | ECCGOST |"
59                           " ECDSAP256SHA256 | ECDSAP384SHA384";
60
61 ISC_PLATFORM_NORETURN_PRE static void
62 usage(void) ISC_PLATFORM_NORETURN_POST;
63
64 static void
65 usage(void) {
66         fprintf(stderr, "Usage:\n");
67         fprintf(stderr, "    %s -l label [options] name\n\n",
68                 program);
69         fprintf(stderr, "Version: %s\n", VERSION);
70         fprintf(stderr, "Required options:\n");
71         fprintf(stderr, "    -l label: label of the key pair\n");
72         fprintf(stderr, "    name: owner of the key\n");
73         fprintf(stderr, "Other options:\n");
74         fprintf(stderr, "    -a algorithm: %s\n", algs);
75         fprintf(stderr, "       (default: RSASHA1, or "
76                                "NSEC3RSASHA1 if using -3)\n");
77         fprintf(stderr, "    -3: use NSEC3-capable algorithm\n");
78         fprintf(stderr, "    -c class (default: IN)\n");
79 #ifdef USE_PKCS11
80         fprintf(stderr, "    -E enginename (default: pkcs11)\n");
81 #else
82         fprintf(stderr, "    -E enginename\n");
83 #endif
84         fprintf(stderr, "    -f keyflag: KSK | REVOKE\n");
85         fprintf(stderr, "    -K directory: directory in which to place "
86                         "key files\n");
87         fprintf(stderr, "    -k: generate a TYPE=KEY key\n");
88         fprintf(stderr, "    -L ttl: default key TTL\n");
89         fprintf(stderr, "    -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
90         fprintf(stderr, "        (DNSKEY generation defaults to ZONE\n");
91         fprintf(stderr, "    -p protocol: default: 3 [dnssec]\n");
92         fprintf(stderr, "    -t type: "
93                 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
94                 "(default: AUTHCONF)\n");
95         fprintf(stderr, "    -y: permit keys that might collide\n");
96         fprintf(stderr, "    -v verbose level\n");
97         fprintf(stderr, "Date options:\n");
98         fprintf(stderr, "    -P date/[+-]offset: set key publication date\n");
99         fprintf(stderr, "    -A date/[+-]offset: set key activation date\n");
100         fprintf(stderr, "    -R date/[+-]offset: set key revocation date\n");
101         fprintf(stderr, "    -I date/[+-]offset: set key inactivation date\n");
102         fprintf(stderr, "    -D date/[+-]offset: set key deletion date\n");
103         fprintf(stderr, "    -G: generate key only; do not set -P or -A\n");
104         fprintf(stderr, "    -C: generate a backward-compatible key, omitting"
105                         " all dates\n");
106         fprintf(stderr, "Output:\n");
107         fprintf(stderr, "     K<name>+<alg>+<id>.key, "
108                         "K<name>+<alg>+<id>.private\n");
109
110         exit (-1);
111 }
112
113 int
114 main(int argc, char **argv) {
115         char            *algname = NULL, *freeit = NULL;
116         char            *nametype = NULL, *type = NULL;
117         const char      *directory = NULL;
118 #ifdef USE_PKCS11
119         const char      *engine = "pkcs11";
120 #else
121         const char      *engine = NULL;
122 #endif
123         char            *classname = NULL;
124         char            *endp;
125         dst_key_t       *key = NULL;
126         dns_fixedname_t fname;
127         dns_name_t      *name;
128         isc_uint16_t    flags = 0, kskflag = 0, revflag = 0;
129         dns_secalg_t    alg;
130         isc_boolean_t   oldstyle = ISC_FALSE;
131         isc_mem_t       *mctx = NULL;
132         int             ch;
133         int             protocol = -1, signatory = 0;
134         isc_result_t    ret;
135         isc_textregion_t r;
136         char            filename[255];
137         isc_buffer_t    buf;
138         isc_log_t       *log = NULL;
139         isc_entropy_t   *ectx = NULL;
140         dns_rdataclass_t rdclass;
141         int             options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
142         char            *label = NULL;
143         dns_ttl_t       ttl = 0;
144         isc_stdtime_t   publish = 0, activate = 0, revoke = 0;
145         isc_stdtime_t   inactive = 0, delete = 0;
146         isc_stdtime_t   now;
147         isc_boolean_t   setpub = ISC_FALSE, setact = ISC_FALSE;
148         isc_boolean_t   setrev = ISC_FALSE, setinact = ISC_FALSE;
149         isc_boolean_t   setdel = ISC_FALSE, setttl = ISC_FALSE;
150         isc_boolean_t   unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
151         isc_boolean_t   unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
152         isc_boolean_t   unsetdel = ISC_FALSE;
153         isc_boolean_t   genonly = ISC_FALSE;
154         isc_boolean_t   use_nsec3 = ISC_FALSE;
155         isc_boolean_t   avoid_collisions = ISC_TRUE;
156         isc_boolean_t   exact;
157         unsigned char   c;
158
159         if (argc == 1)
160                 usage();
161
162         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
163
164         dns_result_register();
165
166         isc_commandline_errprint = ISC_FALSE;
167
168         isc_stdtime_get(&now);
169
170         while ((ch = isc_commandline_parse(argc, argv,
171                         "3a:Cc:E:f:K:kl:L:n:p:t:v:yFhGP:A:R:I:D:")) != -1)
172         {
173             switch (ch) {
174                 case '3':
175                         use_nsec3 = ISC_TRUE;
176                         break;
177                 case 'a':
178                         algname = isc_commandline_argument;
179                         break;
180                 case 'C':
181                         oldstyle = ISC_TRUE;
182                         break;
183                 case 'c':
184                         classname = isc_commandline_argument;
185                         break;
186                 case 'E':
187                         engine = isc_commandline_argument;
188                         break;
189                 case 'f':
190                         c = (unsigned char)(isc_commandline_argument[0]);
191                         if (toupper(c) == 'K')
192                                 kskflag = DNS_KEYFLAG_KSK;
193                         else if (toupper(c) == 'R')
194                                 revflag = DNS_KEYFLAG_REVOKE;
195                         else
196                                 fatal("unknown flag '%s'",
197                                       isc_commandline_argument);
198                         break;
199                 case 'K':
200                         directory = isc_commandline_argument;
201                         ret = try_dir(directory);
202                         if (ret != ISC_R_SUCCESS)
203                                 fatal("cannot open directory %s: %s",
204                                       directory, isc_result_totext(ret));
205                         break;
206                 case 'k':
207                         options |= DST_TYPE_KEY;
208                         break;
209                 case 'L':
210                         if (strcmp(isc_commandline_argument, "none") == 0)
211                                 ttl = 0;
212                         else
213                                 ttl = strtottl(isc_commandline_argument);
214                         setttl = ISC_TRUE;
215                         break;
216                 case 'l':
217                         label = isc_mem_strdup(mctx, isc_commandline_argument);
218                         break;
219                 case 'n':
220                         nametype = isc_commandline_argument;
221                         break;
222                 case 'p':
223                         protocol = strtol(isc_commandline_argument, &endp, 10);
224                         if (*endp != '\0' || protocol < 0 || protocol > 255)
225                                 fatal("-p must be followed by a number "
226                                       "[0..255]");
227                         break;
228                 case 't':
229                         type = isc_commandline_argument;
230                         break;
231                 case 'v':
232                         verbose = strtol(isc_commandline_argument, &endp, 0);
233                         if (*endp != '\0')
234                                 fatal("-v must be followed by a number");
235                         break;
236                 case 'y':
237                         avoid_collisions = ISC_FALSE;
238                         break;
239                 case 'G':
240                         genonly = ISC_TRUE;
241                         break;
242                 case 'P':
243                         if (setpub || unsetpub)
244                                 fatal("-P specified more than once");
245
246                         if (strcasecmp(isc_commandline_argument, "none")) {
247                                 setpub = ISC_TRUE;
248                                 publish = strtotime(isc_commandline_argument,
249                                                     now, now);
250                         } else {
251                                 unsetpub = ISC_TRUE;
252                         }
253                         break;
254                 case 'A':
255                         if (setact || unsetact)
256                                 fatal("-A specified more than once");
257
258                         if (strcasecmp(isc_commandline_argument, "none")) {
259                                 setact = ISC_TRUE;
260                                 activate = strtotime(isc_commandline_argument,
261                                                      now, now);
262                         } else {
263                                 unsetact = ISC_TRUE;
264                         }
265                         break;
266                 case 'R':
267                         if (setrev || unsetrev)
268                                 fatal("-R specified more than once");
269
270                         if (strcasecmp(isc_commandline_argument, "none")) {
271                                 setrev = ISC_TRUE;
272                                 revoke = strtotime(isc_commandline_argument,
273                                                    now, now);
274                         } else {
275                                 unsetrev = ISC_TRUE;
276                         }
277                         break;
278                 case 'I':
279                         if (setinact || unsetinact)
280                                 fatal("-I specified more than once");
281
282                         if (strcasecmp(isc_commandline_argument, "none")) {
283                                 setinact = ISC_TRUE;
284                                 inactive = strtotime(isc_commandline_argument,
285                                                      now, now);
286                         } else {
287                                 unsetinact = ISC_TRUE;
288                         }
289                         break;
290                 case 'D':
291                         if (setdel || unsetdel)
292                                 fatal("-D specified more than once");
293
294                         if (strcasecmp(isc_commandline_argument, "none")) {
295                                 setdel = ISC_TRUE;
296                                 delete = strtotime(isc_commandline_argument,
297                                                    now, now);
298                         } else {
299                                 unsetdel = ISC_TRUE;
300                         }
301                         break;
302                 case 'F':
303                         /* Reserved for FIPS mode */
304                         /* FALLTHROUGH */
305                 case '?':
306                         if (isc_commandline_option != '?')
307                                 fprintf(stderr, "%s: invalid argument -%c\n",
308                                         program, isc_commandline_option);
309                         /* FALLTHROUGH */
310                 case 'h':
311                         usage();
312
313                 default:
314                         fprintf(stderr, "%s: unhandled option -%c\n",
315                                 program, isc_commandline_option);
316                         exit(1);
317                 }
318         }
319
320         if (ectx == NULL)
321                 setup_entropy(mctx, NULL, &ectx);
322         ret = dst_lib_init2(mctx, ectx, engine,
323                             ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
324         if (ret != ISC_R_SUCCESS)
325                 fatal("could not initialize dst: %s",
326                       isc_result_totext(ret));
327
328         setup_logging(verbose, mctx, &log);
329
330         if (label == NULL)
331                 fatal("the key label was not specified");
332         if (argc < isc_commandline_index + 1)
333                 fatal("the key name was not specified");
334         if (argc > isc_commandline_index + 1)
335                 fatal("extraneous arguments");
336
337         if (strchr(label, ':') == NULL &&
338             engine != NULL && strlen(engine) != 0U) {
339                 char *l;
340                 int len;
341
342                 len = strlen(label) + strlen(engine) + 2;
343                 l = isc_mem_allocate(mctx, len);
344                 if (l == NULL)
345                         fatal("cannot allocate memory");
346                 snprintf(l, len, "%s:%s", engine, label);
347                 isc_mem_free(mctx, label);
348                 label = l;
349         }
350
351         if (algname == NULL) {
352                 if (use_nsec3)
353                         algname = strdup(DEFAULT_NSEC3_ALGORITHM);
354                 else
355                         algname = strdup(DEFAULT_ALGORITHM);
356                 if (algname == NULL)
357                         fatal("strdup failed");
358                 freeit = algname;
359                 if (verbose > 0)
360                         fprintf(stderr, "no algorithm specified; "
361                                 "defaulting to %s\n", algname);
362         }
363
364         if (strcasecmp(algname, "RSA") == 0) {
365                 fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
366                                 "If you still wish to use RSA (RSAMD5) please "
367                                 "specify \"-a RSAMD5\"\n");
368                 if (freeit != NULL)
369                         free(freeit);
370                 return (1);
371         } else {
372                 r.base = algname;
373                 r.length = strlen(algname);
374                 ret = dns_secalg_fromtext(&alg, &r);
375                 if (ret != ISC_R_SUCCESS)
376                         fatal("unknown algorithm %s", algname);
377                 if (alg == DST_ALG_DH)
378                         options |= DST_TYPE_KEY;
379         }
380
381         if (use_nsec3 &&
382             alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
383             alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512 &&
384             alg != DST_ALG_ECCGOST &&
385             alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
386                 fatal("%s is incompatible with NSEC3; "
387                       "do not use the -3 option", algname);
388         }
389
390         if (type != NULL && (options & DST_TYPE_KEY) != 0) {
391                 if (strcasecmp(type, "NOAUTH") == 0)
392                         flags |= DNS_KEYTYPE_NOAUTH;
393                 else if (strcasecmp(type, "NOCONF") == 0)
394                         flags |= DNS_KEYTYPE_NOCONF;
395                 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
396                         flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
397                 }
398                 else if (strcasecmp(type, "AUTHCONF") == 0)
399                         /* nothing */;
400                 else
401                         fatal("invalid type %s", type);
402         }
403
404         if (nametype == NULL) {
405                 if ((options & DST_TYPE_KEY) != 0) /* KEY */
406                         fatal("no nametype specified");
407                 flags |= DNS_KEYOWNER_ZONE;     /* DNSKEY */
408         } else if (strcasecmp(nametype, "zone") == 0)
409                 flags |= DNS_KEYOWNER_ZONE;
410         else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
411                 if (strcasecmp(nametype, "host") == 0 ||
412                          strcasecmp(nametype, "entity") == 0)
413                         flags |= DNS_KEYOWNER_ENTITY;
414                 else if (strcasecmp(nametype, "user") == 0)
415                         flags |= DNS_KEYOWNER_USER;
416                 else
417                         fatal("invalid KEY nametype %s", nametype);
418         } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
419                 fatal("invalid DNSKEY nametype %s", nametype);
420
421         rdclass = strtoclass(classname);
422
423         if (directory == NULL)
424                 directory = ".";
425
426         if ((options & DST_TYPE_KEY) != 0)  /* KEY */
427                 flags |= signatory;
428         else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
429                 flags |= kskflag;
430                 flags |= revflag;
431         }
432
433         if (protocol == -1)
434                 protocol = DNS_KEYPROTO_DNSSEC;
435         else if ((options & DST_TYPE_KEY) == 0 &&
436                  protocol != DNS_KEYPROTO_DNSSEC)
437                 fatal("invalid DNSKEY protocol: %d", protocol);
438
439         if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
440                 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
441                         fatal("specified null key with signing authority");
442         }
443
444         if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
445             alg == DNS_KEYALG_DH)
446                 fatal("a key with algorithm '%s' cannot be a zone key",
447                       algname);
448
449         dns_fixedname_init(&fname);
450         name = dns_fixedname_name(&fname);
451         isc_buffer_init(&buf, argv[isc_commandline_index],
452                         strlen(argv[isc_commandline_index]));
453         isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
454         ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
455         if (ret != ISC_R_SUCCESS)
456                 fatal("invalid key name %s: %s", argv[isc_commandline_index],
457                       isc_result_totext(ret));
458
459         isc_buffer_init(&buf, filename, sizeof(filename) - 1);
460
461         /* associate the key */
462         ret = dst_key_fromlabel(name, alg, flags, protocol,
463                                 rdclass, engine, label, NULL, mctx, &key);
464         isc_entropy_stopcallbacksources(ectx);
465
466         if (ret != ISC_R_SUCCESS) {
467                 char namestr[DNS_NAME_FORMATSIZE];
468                 char algstr[DNS_SECALG_FORMATSIZE];
469                 dns_name_format(name, namestr, sizeof(namestr));
470                 dns_secalg_format(alg, algstr, sizeof(algstr));
471                 fatal("failed to get key %s/%s: %s\n",
472                       namestr, algstr, isc_result_totext(ret));
473                 /* NOTREACHED */
474                 exit(-1);
475         }
476
477         /*
478          * Set key timing metadata (unless using -C)
479          *
480          * Publish and activation dates are set to "now" by default, but
481          * can be overridden.  Creation date is always set to "now".
482          */
483         if (!oldstyle) {
484                 dst_key_settime(key, DST_TIME_CREATED, now);
485
486                 if (genonly && (setpub || setact))
487                         fatal("cannot use -G together with -P or -A options");
488
489                 if (setpub)
490                         dst_key_settime(key, DST_TIME_PUBLISH, publish);
491                 else if (setact)
492                         dst_key_settime(key, DST_TIME_PUBLISH, activate);
493                 else if (!genonly && !unsetpub)
494                         dst_key_settime(key, DST_TIME_PUBLISH, now);
495
496                 if (setact)
497                         dst_key_settime(key, DST_TIME_ACTIVATE, activate);
498                 else if (!genonly && !unsetact)
499                         dst_key_settime(key, DST_TIME_ACTIVATE, now);
500
501                 if (setrev) {
502                         if (kskflag == 0)
503                                 fprintf(stderr, "%s: warning: Key is "
504                                         "not flagged as a KSK, but -R "
505                                         "was used. Revoking a ZSK is "
506                                         "legal, but undefined.\n",
507                                         program);
508                         dst_key_settime(key, DST_TIME_REVOKE, revoke);
509                 }
510
511                 if (setinact)
512                         dst_key_settime(key, DST_TIME_INACTIVE, inactive);
513
514                 if (setdel)
515                         dst_key_settime(key, DST_TIME_DELETE, delete);
516         } else {
517                 if (setpub || setact || setrev || setinact ||
518                     setdel || unsetpub || unsetact ||
519                     unsetrev || unsetinact || unsetdel || genonly)
520                         fatal("cannot use -C together with "
521                               "-P, -A, -R, -I, -D, or -G options");
522                 /*
523                  * Compatibility mode: Private-key-format
524                  * should be set to 1.2.
525                  */
526                 dst_key_setprivateformat(key, 1, 2);
527         }
528
529         /* Set default key TTL */
530         if (setttl)
531                 dst_key_setttl(key, ttl);
532
533         /*
534          * Do not overwrite an existing key.  Warn LOUDLY if there
535          * is a risk of ID collision due to this key or another key
536          * being revoked.
537          */
538         if (key_collision(key, name, directory, mctx, &exact)) {
539                 isc_buffer_clear(&buf);
540                 ret = dst_key_buildfilename(key, 0, directory, &buf);
541                 if (ret != ISC_R_SUCCESS)
542                         fatal("dst_key_buildfilename returned: %s\n",
543                               isc_result_totext(ret));
544                 if (exact)
545                         fatal("%s: %s already exists\n", program, filename);
546
547                 if (avoid_collisions)
548                         fatal("%s: %s could collide with another key upon "
549                               "revokation\n", program, filename);
550
551                 fprintf(stderr, "%s: WARNING: Key %s could collide with "
552                                 "another key upon revokation.  If you plan "
553                                 "to revoke keys, destroy this key and "
554                                 "generate a different one.\n",
555                                 program, filename);
556         }
557
558         ret = dst_key_tofile(key, options, directory);
559         if (ret != ISC_R_SUCCESS) {
560                 char keystr[DST_KEY_FORMATSIZE];
561                 dst_key_format(key, keystr, sizeof(keystr));
562                 fatal("failed to write key %s: %s\n", keystr,
563                       isc_result_totext(ret));
564         }
565
566         isc_buffer_clear(&buf);
567         ret = dst_key_buildfilename(key, 0, NULL, &buf);
568         if (ret != ISC_R_SUCCESS)
569                 fatal("dst_key_buildfilename returned: %s\n",
570                       isc_result_totext(ret));
571         printf("%s\n", filename);
572         dst_key_free(&key);
573
574         cleanup_logging(&log);
575         cleanup_entropy(&ectx);
576         dst_lib_destroy();
577         dns_name_destroy();
578         if (verbose > 10)
579                 isc_mem_stats(mctx, stdout);
580         isc_mem_free(mctx, label);
581         isc_mem_destroy(&mctx);
582
583         if (freeit != NULL)
584                 free(freeit);
585
586         return (0);
587 }