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