]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/bin/dnssec/dnssec-keyfromlabel.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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.32.14.4 2011/11/30 00:51:38 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, "    -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
89         fprintf(stderr, "        (DNSKEY generation defaults to ZONE\n");
90         fprintf(stderr, "    -p protocol: default: 3 [dnssec]\n");
91         fprintf(stderr, "    -t type: "
92                 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
93                 "(default: AUTHCONF)\n");
94         fprintf(stderr, "    -y: permit keys that might collide\n");
95         fprintf(stderr, "    -v verbose level\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, "Output:\n");
106         fprintf(stderr, "     K<name>+<alg>+<id>.key, "
107                         "K<name>+<alg>+<id>.private\n");
108
109         exit (-1);
110 }
111
112 int
113 main(int argc, char **argv) {
114         char            *algname = NULL, *freeit = NULL;
115         char            *nametype = NULL, *type = NULL;
116         const char      *directory = NULL;
117 #ifdef USE_PKCS11
118         const char      *engine = "pkcs11";
119 #else
120         const char      *engine = NULL;
121 #endif
122         char            *classname = NULL;
123         char            *endp;
124         dst_key_t       *key = NULL;
125         dns_fixedname_t fname;
126         dns_name_t      *name;
127         isc_uint16_t    flags = 0, kskflag = 0, revflag = 0;
128         dns_secalg_t    alg;
129         isc_boolean_t   oldstyle = ISC_FALSE;
130         isc_mem_t       *mctx = NULL;
131         int             ch;
132         int             protocol = -1, signatory = 0;
133         isc_result_t    ret;
134         isc_textregion_t r;
135         char            filename[255];
136         isc_buffer_t    buf;
137         isc_log_t       *log = NULL;
138         isc_entropy_t   *ectx = NULL;
139         dns_rdataclass_t rdclass;
140         int             options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
141         char            *label = NULL;
142         isc_stdtime_t   publish = 0, activate = 0, revoke = 0;
143         isc_stdtime_t   inactive = 0, delete = 0;
144         isc_stdtime_t   now;
145         isc_boolean_t   setpub = ISC_FALSE, setact = ISC_FALSE;
146         isc_boolean_t   setrev = ISC_FALSE, setinact = ISC_FALSE;
147         isc_boolean_t   setdel = ISC_FALSE;
148         isc_boolean_t   unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
149         isc_boolean_t   unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
150         isc_boolean_t   unsetdel = ISC_FALSE;
151         isc_boolean_t   genonly = ISC_FALSE;
152         isc_boolean_t   use_nsec3 = ISC_FALSE;
153         isc_boolean_t   avoid_collisions = ISC_TRUE;
154         isc_boolean_t   exact;
155         unsigned char   c;
156
157         if (argc == 1)
158                 usage();
159
160         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
161
162         dns_result_register();
163
164         isc_commandline_errprint = ISC_FALSE;
165
166         isc_stdtime_get(&now);
167
168         while ((ch = isc_commandline_parse(argc, argv,
169                                 "3a:Cc:E:f:K:kl:n:p:t:v:yFhGP:A:R:I:D:")) != -1)
170         {
171             switch (ch) {
172                 case '3':
173                         use_nsec3 = ISC_TRUE;
174                         break;
175                 case 'a':
176                         algname = isc_commandline_argument;
177                         break;
178                 case 'C':
179                         oldstyle = ISC_TRUE;
180                         break;
181                 case 'c':
182                         classname = isc_commandline_argument;
183                         break;
184                 case 'E':
185                         engine = isc_commandline_argument;
186                         break;
187                 case 'f':
188                         c = (unsigned char)(isc_commandline_argument[0]);
189                         if (toupper(c) == 'K')
190                                 kskflag = DNS_KEYFLAG_KSK;
191                         else if (toupper(c) == 'R')
192                                 revflag = DNS_KEYFLAG_REVOKE;
193                         else
194                                 fatal("unknown flag '%s'",
195                                       isc_commandline_argument);
196                         break;
197                 case 'K':
198                         directory = isc_commandline_argument;
199                         ret = try_dir(directory);
200                         if (ret != ISC_R_SUCCESS)
201                                 fatal("cannot open directory %s: %s",
202                                       directory, isc_result_totext(ret));
203                         break;
204                 case 'k':
205                         options |= DST_TYPE_KEY;
206                         break;
207                 case 'l':
208                         label = isc_mem_strdup(mctx, isc_commandline_argument);
209                         break;
210                 case 'n':
211                         nametype = isc_commandline_argument;
212                         break;
213                 case 'p':
214                         protocol = strtol(isc_commandline_argument, &endp, 10);
215                         if (*endp != '\0' || protocol < 0 || protocol > 255)
216                                 fatal("-p must be followed by a number "
217                                       "[0..255]");
218                         break;
219                 case 't':
220                         type = isc_commandline_argument;
221                         break;
222                 case 'v':
223                         verbose = strtol(isc_commandline_argument, &endp, 0);
224                         if (*endp != '\0')
225                                 fatal("-v must be followed by a number");
226                         break;
227                 case 'y':
228                         avoid_collisions = ISC_FALSE;
229                         break;
230                 case 'G':
231                         genonly = ISC_TRUE;
232                         break;
233                 case 'P':
234                         if (setpub || unsetpub)
235                                 fatal("-P specified more than once");
236
237                         if (strcasecmp(isc_commandline_argument, "none")) {
238                                 setpub = ISC_TRUE;
239                                 publish = strtotime(isc_commandline_argument,
240                                                     now, now);
241                         } else {
242                                 unsetpub = ISC_TRUE;
243                         }
244                         break;
245                 case 'A':
246                         if (setact || unsetact)
247                                 fatal("-A specified more than once");
248
249                         if (strcasecmp(isc_commandline_argument, "none")) {
250                                 setact = ISC_TRUE;
251                                 activate = strtotime(isc_commandline_argument,
252                                                      now, now);
253                         } else {
254                                 unsetact = ISC_TRUE;
255                         }
256                         break;
257                 case 'R':
258                         if (setrev || unsetrev)
259                                 fatal("-R specified more than once");
260
261                         if (strcasecmp(isc_commandline_argument, "none")) {
262                                 setrev = ISC_TRUE;
263                                 revoke = strtotime(isc_commandline_argument,
264                                                    now, now);
265                         } else {
266                                 unsetrev = ISC_TRUE;
267                         }
268                         break;
269                 case 'I':
270                         if (setinact || unsetinact)
271                                 fatal("-I specified more than once");
272
273                         if (strcasecmp(isc_commandline_argument, "none")) {
274                                 setinact = ISC_TRUE;
275                                 inactive = strtotime(isc_commandline_argument,
276                                                      now, now);
277                         } else {
278                                 unsetinact = ISC_TRUE;
279                         }
280                         break;
281                 case 'D':
282                         if (setdel || unsetdel)
283                                 fatal("-D specified more than once");
284
285                         if (strcasecmp(isc_commandline_argument, "none")) {
286                                 setdel = ISC_TRUE;
287                                 delete = strtotime(isc_commandline_argument,
288                                                    now, now);
289                         } else {
290                                 unsetdel = ISC_TRUE;
291                         }
292                         break;
293                 case 'F':
294                         /* Reserved for FIPS mode */
295                         /* FALLTHROUGH */
296                 case '?':
297                         if (isc_commandline_option != '?')
298                                 fprintf(stderr, "%s: invalid argument -%c\n",
299                                         program, isc_commandline_option);
300                         /* FALLTHROUGH */
301                 case 'h':
302                         usage();
303
304                 default:
305                         fprintf(stderr, "%s: unhandled option -%c\n",
306                                 program, isc_commandline_option);
307                         exit(1);
308                 }
309         }
310
311         if (ectx == NULL)
312                 setup_entropy(mctx, NULL, &ectx);
313         ret = dst_lib_init2(mctx, ectx, engine,
314                             ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
315         if (ret != ISC_R_SUCCESS)
316                 fatal("could not initialize dst: %s",
317                       isc_result_totext(ret));
318
319         setup_logging(verbose, mctx, &log);
320
321         if (label == NULL)
322                 fatal("the key label was not specified");
323         if (argc < isc_commandline_index + 1)
324                 fatal("the key name was not specified");
325         if (argc > isc_commandline_index + 1)
326                 fatal("extraneous arguments");
327
328         if (strchr(label, ':') == NULL &&
329             engine != NULL && strlen(engine) != 0U) {
330                 char *l;
331                 int len;
332
333                 len = strlen(label) + strlen(engine) + 2;
334                 l = isc_mem_allocate(mctx, len);
335                 if (l == NULL)
336                         fatal("cannot allocate memory");
337                 snprintf(l, len, "%s:%s", engine, label);
338                 isc_mem_free(mctx, label);
339                 label = l;
340         }
341
342         if (algname == NULL) {
343                 if (use_nsec3)
344                         algname = strdup(DEFAULT_NSEC3_ALGORITHM);
345                 else
346                         algname = strdup(DEFAULT_ALGORITHM);
347                 if (algname == NULL)
348                         fatal("strdup failed");
349                 freeit = algname;
350                 if (verbose > 0)
351                         fprintf(stderr, "no algorithm specified; "
352                                 "defaulting to %s\n", algname);
353         }
354
355         if (strcasecmp(algname, "RSA") == 0) {
356                 fprintf(stderr, "The use of RSA (RSAMD5) is not recommended.\n"
357                                 "If you still wish to use RSA (RSAMD5) please "
358                                 "specify \"-a RSAMD5\"\n");
359                 return (1);
360         } else {
361                 r.base = algname;
362                 r.length = strlen(algname);
363                 ret = dns_secalg_fromtext(&alg, &r);
364                 if (ret != ISC_R_SUCCESS)
365                         fatal("unknown algorithm %s", algname);
366                 if (alg == DST_ALG_DH)
367                         options |= DST_TYPE_KEY;
368         }
369
370         if (use_nsec3 &&
371             alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
372             alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512 &&
373             alg != DST_ALG_ECCGOST &&
374             alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
375                 fatal("%s is incompatible with NSEC3; "
376                       "do not use the -3 option", algname);
377         }
378
379         if (type != NULL && (options & DST_TYPE_KEY) != 0) {
380                 if (strcasecmp(type, "NOAUTH") == 0)
381                         flags |= DNS_KEYTYPE_NOAUTH;
382                 else if (strcasecmp(type, "NOCONF") == 0)
383                         flags |= DNS_KEYTYPE_NOCONF;
384                 else if (strcasecmp(type, "NOAUTHCONF") == 0) {
385                         flags |= (DNS_KEYTYPE_NOAUTH | DNS_KEYTYPE_NOCONF);
386                 }
387                 else if (strcasecmp(type, "AUTHCONF") == 0)
388                         /* nothing */;
389                 else
390                         fatal("invalid type %s", type);
391         }
392
393         if (nametype == NULL) {
394                 if ((options & DST_TYPE_KEY) != 0) /* KEY */
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 */
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 (directory == NULL)
413                 directory = ".";
414
415         if ((options & DST_TYPE_KEY) != 0)  /* KEY */
416                 flags |= signatory;
417         else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
418                 flags |= kskflag;
419                 flags |= revflag;
420         }
421
422         if (protocol == -1)
423                 protocol = DNS_KEYPROTO_DNSSEC;
424         else if ((options & DST_TYPE_KEY) == 0 &&
425                  protocol != DNS_KEYPROTO_DNSSEC)
426                 fatal("invalid DNSKEY protocol: %d", protocol);
427
428         if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
429                 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
430                         fatal("specified null key with signing authority");
431         }
432
433         if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
434             alg == DNS_KEYALG_DH)
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, 0, 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         isc_buffer_init(&buf, filename, sizeof(filename) - 1);
449
450         /* associate the key */
451         ret = dst_key_fromlabel(name, alg, flags, protocol,
452                                 rdclass, engine, label, NULL, mctx, &key);
453         isc_entropy_stopcallbacksources(ectx);
454
455         if (ret != ISC_R_SUCCESS) {
456                 char namestr[DNS_NAME_FORMATSIZE];
457                 char algstr[DNS_SECALG_FORMATSIZE];
458                 dns_name_format(name, namestr, sizeof(namestr));
459                 dns_secalg_format(alg, algstr, sizeof(algstr));
460                 fatal("failed to get key %s/%s: %s\n",
461                       namestr, algstr, isc_result_totext(ret));
462                 /* NOTREACHED */
463                 exit(-1);
464         }
465
466         /*
467          * Set key timing metadata (unless using -C)
468          *
469          * Publish and activation dates are set to "now" by default, but
470          * can be overridden.  Creation date is always set to "now".
471          */
472         if (!oldstyle) {
473                 dst_key_settime(key, DST_TIME_CREATED, now);
474
475                 if (genonly && (setpub || setact))
476                         fatal("cannot use -G together with -P or -A options");
477
478                 if (setpub)
479                         dst_key_settime(key, DST_TIME_PUBLISH, publish);
480                 else if (setact)
481                         dst_key_settime(key, DST_TIME_PUBLISH, activate);
482                 else if (!genonly && !unsetpub)
483                         dst_key_settime(key, DST_TIME_PUBLISH, now);
484
485                 if (setact)
486                         dst_key_settime(key, DST_TIME_ACTIVATE, activate);
487                 else if (!genonly && !unsetact)
488                         dst_key_settime(key, DST_TIME_ACTIVATE, now);
489
490                 if (setrev) {
491                         if (kskflag == 0)
492                                 fprintf(stderr, "%s: warning: Key is "
493                                         "not flagged as a KSK, but -R "
494                                         "was used. Revoking a ZSK is "
495                                         "legal, but undefined.\n",
496                                         program);
497                         dst_key_settime(key, DST_TIME_REVOKE, revoke);
498                 }
499
500                 if (setinact)
501                         dst_key_settime(key, DST_TIME_INACTIVE, inactive);
502
503                 if (setdel)
504                         dst_key_settime(key, DST_TIME_DELETE, delete);
505         } else {
506                 if (setpub || setact || setrev || setinact ||
507                     setdel || unsetpub || unsetact ||
508                     unsetrev || unsetinact || unsetdel || genonly)
509                         fatal("cannot use -C together with "
510                               "-P, -A, -R, -I, -D, or -G options");
511                 /*
512                  * Compatibility mode: Private-key-format
513                  * should be set to 1.2.
514                  */
515                 dst_key_setprivateformat(key, 1, 2);
516         }
517
518         /*
519          * Do not overwrite an existing key.  Warn LOUDLY if there
520          * is a risk of ID collision due to this key or another key
521          * being revoked.
522          */
523         if (key_collision(key, name, directory, mctx, &exact)) {
524                 isc_buffer_clear(&buf);
525                 ret = dst_key_buildfilename(key, 0, directory, &buf);
526                 if (ret != ISC_R_SUCCESS)
527                         fatal("dst_key_buildfilename returned: %s\n",
528                               isc_result_totext(ret));
529                 if (exact)
530                         fatal("%s: %s already exists\n", program, filename);
531
532                 if (avoid_collisions)
533                         fatal("%s: %s could collide with another key upon "
534                               "revokation\n", program, filename);
535
536                 fprintf(stderr, "%s: WARNING: Key %s could collide with "
537                                 "another key upon revokation.  If you plan "
538                                 "to revoke keys, destroy this key and "
539                                 "generate a different one.\n",
540                                 program, filename);
541         }
542
543         ret = dst_key_tofile(key, options, directory);
544         if (ret != ISC_R_SUCCESS) {
545                 char keystr[DST_KEY_FORMATSIZE];
546                 dst_key_format(key, keystr, sizeof(keystr));
547                 fatal("failed to write key %s: %s\n", keystr,
548                       isc_result_totext(ret));
549         }
550
551         isc_buffer_clear(&buf);
552         ret = dst_key_buildfilename(key, 0, NULL, &buf);
553         if (ret != ISC_R_SUCCESS)
554                 fatal("dst_key_buildfilename returned: %s\n",
555                       isc_result_totext(ret));
556         printf("%s\n", filename);
557         dst_key_free(&key);
558
559         cleanup_logging(&log);
560         cleanup_entropy(&ectx);
561         dst_lib_destroy();
562         dns_name_destroy();
563         if (verbose > 10)
564                 isc_mem_stats(mctx, stdout);
565         isc_mem_free(mctx, label);
566         isc_mem_destroy(&mctx);
567
568         if (freeit != NULL)
569                 free(freeit);
570
571         return (0);
572 }