dnssec-keyfromlabel.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007-2012, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /*! \file */
00018 
00019 #include <config.h>
00020 
00021 #include <ctype.h>
00022 #include <stdlib.h>
00023 
00024 #include <isc/buffer.h>
00025 #include <isc/commandline.h>
00026 #include <isc/entropy.h>
00027 #include <isc/mem.h>
00028 #include <isc/region.h>
00029 #include <isc/print.h>
00030 #include <isc/string.h>
00031 #include <isc/util.h>
00032 
00033 #include <dns/dnssec.h>
00034 #include <dns/fixedname.h>
00035 #include <dns/keyvalues.h>
00036 #include <dns/log.h>
00037 #include <dns/name.h>
00038 #include <dns/rdataclass.h>
00039 #include <dns/result.h>
00040 #include <dns/secalg.h>
00041 
00042 #include <dst/dst.h>
00043 
00044 #ifdef PKCS11CRYPTO
00045 #include <pk11/result.h>
00046 #endif
00047 
00048 #include "dnssectool.h"
00049 
00050 #define MAX_RSA 4096 /* should be long enough... */
00051 
00052 const char *program = "dnssec-keyfromlabel";
00053 int verbose;
00054 
00055 #define DEFAULT_ALGORITHM "RSASHA1"
00056 #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
00057 
00058 static const char *algs = "RSA | RSAMD5 | DH | DSA | RSASHA1 |"
00059                           " NSEC3DSA | NSEC3RSASHA1 |"
00060                           " RSASHA256 | RSASHA512 | ECCGOST |"
00061                           " ECDSAP256SHA256 | ECDSAP384SHA384";
00062 
00063 ISC_PLATFORM_NORETURN_PRE static void
00064 usage(void) ISC_PLATFORM_NORETURN_POST;
00065 
00066 static void
00067 usage(void) {
00068         fprintf(stderr, "Usage:\n");
00069         fprintf(stderr, "    %s -l label [options] name\n\n",
00070                 program);
00071         fprintf(stderr, "Version: %s\n", VERSION);
00072         fprintf(stderr, "Required options:\n");
00073         fprintf(stderr, "    -l label: label of the key pair\n");
00074         fprintf(stderr, "    name: owner of the key\n");
00075         fprintf(stderr, "Other options:\n");
00076         fprintf(stderr, "    -a algorithm: %s\n", algs);
00077         fprintf(stderr, "       (default: RSASHA1, or "
00078                                "NSEC3RSASHA1 if using -3)\n");
00079         fprintf(stderr, "    -3: use NSEC3-capable algorithm\n");
00080         fprintf(stderr, "    -c class (default: IN)\n");
00081         fprintf(stderr, "    -E <engine>:\n");
00082 #if defined(PKCS11CRYPTO)
00083         fprintf(stderr, "        path to PKCS#11 provider library "
00084                                 "(default is %s)\n", PK11_LIB_LOCATION);
00085 #elif defined(USE_PKCS11)
00086         fprintf(stderr, "        name of an OpenSSL engine to use "
00087                                 "(default is \"pkcs11\")\n");
00088 #else
00089         fprintf(stderr, "        name of an OpenSSL engine to use\n");
00090 #endif
00091         fprintf(stderr, "    -f keyflag: KSK | REVOKE\n");
00092         fprintf(stderr, "    -K directory: directory in which to place "
00093                         "key files\n");
00094         fprintf(stderr, "    -k: generate a TYPE=KEY key\n");
00095         fprintf(stderr, "    -L ttl: default key TTL\n");
00096         fprintf(stderr, "    -n nametype: ZONE | HOST | ENTITY | USER | OTHER\n");
00097         fprintf(stderr, "        (DNSKEY generation defaults to ZONE\n");
00098         fprintf(stderr, "    -p protocol: default: 3 [dnssec]\n");
00099         fprintf(stderr, "    -t type: "
00100                 "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
00101                 "(default: AUTHCONF)\n");
00102         fprintf(stderr, "    -y: permit keys that might collide\n");
00103         fprintf(stderr, "    -v verbose level\n");
00104         fprintf(stderr, "    -V: print version information\n");
00105         fprintf(stderr, "Date options:\n");
00106         fprintf(stderr, "    -P date/[+-]offset: set key publication date\n");
00107         fprintf(stderr, "    -A date/[+-]offset: set key activation date\n");
00108         fprintf(stderr, "    -R date/[+-]offset: set key revocation date\n");
00109         fprintf(stderr, "    -I date/[+-]offset: set key inactivation date\n");
00110         fprintf(stderr, "    -D date/[+-]offset: set key deletion date\n");
00111         fprintf(stderr, "    -G: generate key only; do not set -P or -A\n");
00112         fprintf(stderr, "    -C: generate a backward-compatible key, omitting"
00113                         " all dates\n");
00114         fprintf(stderr, "    -S <key>: generate a successor to an existing "
00115                                       "key\n");
00116         fprintf(stderr, "    -i <interval>: prepublication interval for "
00117                                            "successor key "
00118                                            "(default: 30 days)\n");
00119         fprintf(stderr, "Output:\n");
00120         fprintf(stderr, "     K<name>+<alg>+<id>.key, "
00121                         "K<name>+<alg>+<id>.private\n");
00122 
00123         exit (-1);
00124 }
00125 
00126 int
00127 main(int argc, char **argv) {
00128         char            *algname = NULL, *freeit = NULL;
00129         char            *nametype = NULL, *type = NULL;
00130         const char      *directory = NULL;
00131         const char      *predecessor = NULL;
00132         dst_key_t       *prevkey = NULL;
00133 #ifdef USE_PKCS11
00134         const char      *engine = PKCS11_ENGINE;
00135 #else
00136         const char      *engine = NULL;
00137 #endif
00138         char            *classname = NULL;
00139         char            *endp;
00140         dst_key_t       *key = NULL;
00141         dns_fixedname_t fname;
00142         dns_name_t      *name;
00143         isc_uint16_t    flags = 0, kskflag = 0, revflag = 0;
00144         dns_secalg_t    alg;
00145         isc_boolean_t   oldstyle = ISC_FALSE;
00146         isc_mem_t       *mctx = NULL;
00147         int             ch;
00148         int             protocol = -1, signatory = 0;
00149         isc_result_t    ret;
00150         isc_textregion_t r;
00151         char            filename[255];
00152         isc_buffer_t    buf;
00153         isc_log_t       *log = NULL;
00154         isc_entropy_t   *ectx = NULL;
00155         dns_rdataclass_t rdclass;
00156         int             options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
00157         char            *label = NULL;
00158         dns_ttl_t       ttl = 0;
00159         isc_stdtime_t   publish = 0, activate = 0, revoke = 0;
00160         isc_stdtime_t   inactive = 0, delete = 0;
00161         isc_stdtime_t   now;
00162         int             prepub = -1;
00163         isc_boolean_t   setpub = ISC_FALSE, setact = ISC_FALSE;
00164         isc_boolean_t   setrev = ISC_FALSE, setinact = ISC_FALSE;
00165         isc_boolean_t   setdel = ISC_FALSE, setttl = ISC_FALSE;
00166         isc_boolean_t   unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
00167         isc_boolean_t   unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
00168         isc_boolean_t   unsetdel = ISC_FALSE;
00169         isc_boolean_t   genonly = ISC_FALSE;
00170         isc_boolean_t   use_nsec3 = ISC_FALSE;
00171         isc_boolean_t   avoid_collisions = ISC_TRUE;
00172         isc_boolean_t   exact;
00173         unsigned char   c;
00174 
00175         if (argc == 1)
00176                 usage();
00177 
00178         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
00179 
00180 #ifdef PKCS11CRYPTO
00181         pk11_result_register();
00182 #endif
00183         dns_result_register();
00184 
00185         isc_commandline_errprint = ISC_FALSE;
00186 
00187         isc_stdtime_get(&now);
00188 
00189 #define CMDLINE_FLAGS "3A:a:Cc:D:E:Ff:GhI:i:kK:L:l:n:P:p:R:S:t:v:Vy"
00190         while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
00191             switch (ch) {
00192                 case '3':
00193                         use_nsec3 = ISC_TRUE;
00194                         break;
00195                 case 'a':
00196                         algname = isc_commandline_argument;
00197                         break;
00198                 case 'C':
00199                         oldstyle = ISC_TRUE;
00200                         break;
00201                 case 'c':
00202                         classname = isc_commandline_argument;
00203                         break;
00204                 case 'E':
00205                         engine = isc_commandline_argument;
00206                         break;
00207                 case 'f':
00208                         c = (unsigned char)(isc_commandline_argument[0]);
00209                         if (toupper(c) == 'K')
00210                                 kskflag = DNS_KEYFLAG_KSK;
00211                         else if (toupper(c) == 'R')
00212                                 revflag = DNS_KEYFLAG_REVOKE;
00213                         else
00214                                 fatal("unknown flag '%s'",
00215                                       isc_commandline_argument);
00216                         break;
00217                 case 'K':
00218                         directory = isc_commandline_argument;
00219                         ret = try_dir(directory);
00220                         if (ret != ISC_R_SUCCESS)
00221                                 fatal("cannot open directory %s: %s",
00222                                       directory, isc_result_totext(ret));
00223                         break;
00224                 case 'k':
00225                         options |= DST_TYPE_KEY;
00226                         break;
00227                 case 'L':
00228                         ttl = strtottl(isc_commandline_argument);
00229                         setttl = ISC_TRUE;
00230                         break;
00231                 case 'l':
00232                         label = isc_mem_strdup(mctx, isc_commandline_argument);
00233                         break;
00234                 case 'n':
00235                         nametype = isc_commandline_argument;
00236                         break;
00237                 case 'p':
00238                         protocol = strtol(isc_commandline_argument, &endp, 10);
00239                         if (*endp != '\0' || protocol < 0 || protocol > 255)
00240                                 fatal("-p must be followed by a number "
00241                                       "[0..255]");
00242                         break;
00243                 case 't':
00244                         type = isc_commandline_argument;
00245                         break;
00246                 case 'v':
00247                         verbose = strtol(isc_commandline_argument, &endp, 0);
00248                         if (*endp != '\0')
00249                                 fatal("-v must be followed by a number");
00250                         break;
00251                 case 'y':
00252                         avoid_collisions = ISC_FALSE;
00253                         break;
00254                 case 'G':
00255                         genonly = ISC_TRUE;
00256                         break;
00257                 case 'P':
00258                         if (setpub || unsetpub)
00259                                 fatal("-P specified more than once");
00260 
00261                         publish = strtotime(isc_commandline_argument,
00262                                             now, now, &setpub);
00263                         unsetpub = !setpub;
00264                         break;
00265                 case 'A':
00266                         if (setact || unsetact)
00267                                 fatal("-A specified more than once");
00268 
00269                         activate = strtotime(isc_commandline_argument,
00270                                              now, now, &setact);
00271                         unsetact = !setact;
00272                         break;
00273                 case 'R':
00274                         if (setrev || unsetrev)
00275                                 fatal("-R specified more than once");
00276 
00277                         revoke = strtotime(isc_commandline_argument,
00278                                            now, now, &setrev);
00279                         unsetrev = !setrev;
00280                         break;
00281                 case 'I':
00282                         if (setinact || unsetinact)
00283                                 fatal("-I specified more than once");
00284 
00285                         inactive = strtotime(isc_commandline_argument,
00286                                              now, now, &setinact);
00287                         unsetinact = !setinact;
00288                         break;
00289                 case 'D':
00290                         if (setdel || unsetdel)
00291                                 fatal("-D specified more than once");
00292 
00293                         delete = strtotime(isc_commandline_argument,
00294                                            now, now, &setdel);
00295                         unsetdel = !setdel;
00296                         break;
00297                 case 'S':
00298                         predecessor = isc_commandline_argument;
00299                         break;
00300                 case 'i':
00301                         prepub = strtottl(isc_commandline_argument);
00302                         break;
00303                 case 'F':
00304                         /* Reserved for FIPS mode */
00305                         /* FALLTHROUGH */
00306                 case '?':
00307                         if (isc_commandline_option != '?')
00308                                 fprintf(stderr, "%s: invalid argument -%c\n",
00309                                         program, isc_commandline_option);
00310                         /* FALLTHROUGH */
00311                 case 'h':
00312                         /* Does not return. */
00313                         usage();
00314 
00315                 case 'V':
00316                         /* Does not return. */
00317                         version(program);
00318 
00319                 default:
00320                         fprintf(stderr, "%s: unhandled option -%c\n",
00321                                 program, isc_commandline_option);
00322                         exit(1);
00323                 }
00324         }
00325 
00326         if (ectx == NULL)
00327                 setup_entropy(mctx, NULL, &ectx);
00328         ret = dst_lib_init2(mctx, ectx, engine,
00329                             ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
00330         if (ret != ISC_R_SUCCESS)
00331                 fatal("could not initialize dst: %s",
00332                       isc_result_totext(ret));
00333 
00334         setup_logging(mctx, &log);
00335 
00336         if (predecessor == NULL) {
00337                 if (label == NULL)
00338                         fatal("the key label was not specified");
00339                 if (argc < isc_commandline_index + 1)
00340                         fatal("the key name was not specified");
00341                 if (argc > isc_commandline_index + 1)
00342                         fatal("extraneous arguments");
00343 
00344                 dns_fixedname_init(&fname);
00345                 name = dns_fixedname_name(&fname);
00346                 isc_buffer_init(&buf, argv[isc_commandline_index],
00347                                 strlen(argv[isc_commandline_index]));
00348                 isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
00349                 ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
00350                 if (ret != ISC_R_SUCCESS)
00351                         fatal("invalid key name %s: %s",
00352                               argv[isc_commandline_index],
00353                               isc_result_totext(ret));
00354 
00355                 if (strchr(label, ':') == NULL) {
00356                         char *l;
00357                         int len;
00358 
00359                         len = strlen(label) + 8;
00360                         l = isc_mem_allocate(mctx, len);
00361                         if (l == NULL)
00362                                 fatal("cannot allocate memory");
00363                         snprintf(l, len, "pkcs11:%s", label);
00364                         isc_mem_free(mctx, label);
00365                         label = l;
00366                 }
00367 
00368                 if (algname == NULL) {
00369                         if (use_nsec3)
00370                                 algname = strdup(DEFAULT_NSEC3_ALGORITHM);
00371                         else
00372                                 algname = strdup(DEFAULT_ALGORITHM);
00373                         if (algname == NULL)
00374                                 fatal("strdup failed");
00375                         freeit = algname;
00376                         if (verbose > 0)
00377                                 fprintf(stderr, "no algorithm specified; "
00378                                         "defaulting to %s\n", algname);
00379                 }
00380 
00381                 if (strcasecmp(algname, "RSA") == 0) {
00382                         fprintf(stderr, "The use of RSA (RSAMD5) is not "
00383                                         "recommended.\nIf you still wish to "
00384                                         "use RSA (RSAMD5) please specify "
00385                                         "\"-a RSAMD5\"\n");
00386                         if (freeit != NULL)
00387                                 free(freeit);
00388                         return (1);
00389                 } else {
00390                         r.base = algname;
00391                         r.length = strlen(algname);
00392                         ret = dns_secalg_fromtext(&alg, &r);
00393                         if (ret != ISC_R_SUCCESS)
00394                                 fatal("unknown algorithm %s", algname);
00395                         if (alg == DST_ALG_DH)
00396                                 options |= DST_TYPE_KEY;
00397                 }
00398 
00399                 if (use_nsec3 &&
00400                     alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
00401                     alg != DST_ALG_RSASHA256 && alg != DST_ALG_RSASHA512 &&
00402                     alg != DST_ALG_ECCGOST &&
00403                     alg != DST_ALG_ECDSA256 && alg != DST_ALG_ECDSA384) {
00404                         fatal("%s is incompatible with NSEC3; "
00405                               "do not use the -3 option", algname);
00406                 }
00407 
00408                 if (type != NULL && (options & DST_TYPE_KEY) != 0) {
00409                         if (strcasecmp(type, "NOAUTH") == 0)
00410                                 flags |= DNS_KEYTYPE_NOAUTH;
00411                         else if (strcasecmp(type, "NOCONF") == 0)
00412                                 flags |= DNS_KEYTYPE_NOCONF;
00413                         else if (strcasecmp(type, "NOAUTHCONF") == 0)
00414                                 flags |= (DNS_KEYTYPE_NOAUTH |
00415                                           DNS_KEYTYPE_NOCONF);
00416                         else if (strcasecmp(type, "AUTHCONF") == 0)
00417                                 /* nothing */;
00418                         else
00419                                 fatal("invalid type %s", type);
00420                 }
00421 
00422                 if (!oldstyle && prepub > 0) {
00423                         if (setpub && setact && (activate - prepub) < publish)
00424                                 fatal("Activation and publication dates "
00425                                       "are closer together than the\n\t"
00426                                       "prepublication interval.");
00427 
00428                         if (!setpub && !setact) {
00429                                 setpub = setact = ISC_TRUE;
00430                                 publish = now;
00431                                 activate = now + prepub;
00432                         } else if (setpub && !setact) {
00433                                 setact = ISC_TRUE;
00434                                 activate = publish + prepub;
00435                         } else if (setact && !setpub) {
00436                                 setpub = ISC_TRUE;
00437                                 publish = activate - prepub;
00438                         }
00439 
00440                         if ((activate - prepub) < now)
00441                                 fatal("Time until activation is shorter "
00442                                       "than the\n\tprepublication interval.");
00443                 }
00444         } else {
00445                 char keystr[DST_KEY_FORMATSIZE];
00446                 isc_stdtime_t when;
00447                 int major, minor;
00448 
00449                 if (prepub == -1)
00450                         prepub = (30 * 86400);
00451 
00452                 if (algname != NULL)
00453                         fatal("-S and -a cannot be used together");
00454                 if (nametype != NULL)
00455                         fatal("-S and -n cannot be used together");
00456                 if (type != NULL)
00457                         fatal("-S and -t cannot be used together");
00458                 if (setpub || unsetpub)
00459                         fatal("-S and -P cannot be used together");
00460                 if (setact || unsetact)
00461                         fatal("-S and -A cannot be used together");
00462                 if (use_nsec3)
00463                         fatal("-S and -3 cannot be used together");
00464                 if (oldstyle)
00465                         fatal("-S and -C cannot be used together");
00466                 if (genonly)
00467                         fatal("-S and -G cannot be used together");
00468 
00469                 ret = dst_key_fromnamedfile(predecessor, directory,
00470                                             DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
00471                                             mctx, &prevkey);
00472                 if (ret != ISC_R_SUCCESS)
00473                         fatal("Invalid keyfile %s: %s",
00474                               predecessor, isc_result_totext(ret));
00475                 if (!dst_key_isprivate(prevkey))
00476                         fatal("%s is not a private key", predecessor);
00477 
00478                 name = dst_key_name(prevkey);
00479                 alg = dst_key_alg(prevkey);
00480                 flags = dst_key_flags(prevkey);
00481 
00482                 dst_key_format(prevkey, keystr, sizeof(keystr));
00483                 dst_key_getprivateformat(prevkey, &major, &minor);
00484                 if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
00485                         fatal("Key %s has incompatible format version %d.%d\n\t"
00486                               "It is not possible to generate a successor key.",
00487                               keystr, major, minor);
00488 
00489                 ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
00490                 if (ret != ISC_R_SUCCESS)
00491                         fatal("Key %s has no activation date.\n\t"
00492                               "You must use dnssec-settime -A to set one "
00493                               "before generating a successor.", keystr);
00494 
00495                 ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
00496                 if (ret != ISC_R_SUCCESS)
00497                         fatal("Key %s has no inactivation date.\n\t"
00498                               "You must use dnssec-settime -I to set one "
00499                               "before generating a successor.", keystr);
00500 
00501                 publish = activate - prepub;
00502                 if (publish < now)
00503                         fatal("Key %s becomes inactive\n\t"
00504                               "sooner than the prepublication period "
00505                               "for the new key ends.\n\t"
00506                               "Either change the inactivation date with "
00507                               "dnssec-settime -I,\n\t"
00508                               "or use the -i option to set a shorter "
00509                               "prepublication interval.", keystr);
00510 
00511                 ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
00512                 if (ret != ISC_R_SUCCESS)
00513                         fprintf(stderr, "%s: WARNING: Key %s has no removal "
00514                                         "date;\n\t it will remain in the zone "
00515                                         "indefinitely after rollover.\n\t "
00516                                         "You can use dnssec-settime -D to "
00517                                         "change this.\n", program, keystr);
00518 
00519                 setpub = setact = ISC_TRUE;
00520         }
00521 
00522         if (nametype == NULL) {
00523                 if ((options & DST_TYPE_KEY) != 0) /* KEY */
00524                         fatal("no nametype specified");
00525                 flags |= DNS_KEYOWNER_ZONE;     /* DNSKEY */
00526         } else if (strcasecmp(nametype, "zone") == 0)
00527                 flags |= DNS_KEYOWNER_ZONE;
00528         else if ((options & DST_TYPE_KEY) != 0) { /* KEY */
00529                 if (strcasecmp(nametype, "host") == 0 ||
00530                          strcasecmp(nametype, "entity") == 0)
00531                         flags |= DNS_KEYOWNER_ENTITY;
00532                 else if (strcasecmp(nametype, "user") == 0)
00533                         flags |= DNS_KEYOWNER_USER;
00534                 else
00535                         fatal("invalid KEY nametype %s", nametype);
00536         } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
00537                 fatal("invalid DNSKEY nametype %s", nametype);
00538 
00539         rdclass = strtoclass(classname);
00540 
00541         if (directory == NULL)
00542                 directory = ".";
00543 
00544         if ((options & DST_TYPE_KEY) != 0)  /* KEY */
00545                 flags |= signatory;
00546         else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
00547                 flags |= kskflag;
00548                 flags |= revflag;
00549         }
00550 
00551         if (protocol == -1)
00552                 protocol = DNS_KEYPROTO_DNSSEC;
00553         else if ((options & DST_TYPE_KEY) == 0 &&
00554                  protocol != DNS_KEYPROTO_DNSSEC)
00555                 fatal("invalid DNSKEY protocol: %d", protocol);
00556 
00557         if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
00558                 if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
00559                         fatal("specified null key with signing authority");
00560         }
00561 
00562         if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
00563             alg == DNS_KEYALG_DH)
00564                 fatal("a key with algorithm '%s' cannot be a zone key",
00565                       algname);
00566 
00567         isc_buffer_init(&buf, filename, sizeof(filename) - 1);
00568 
00569         /* associate the key */
00570         ret = dst_key_fromlabel(name, alg, flags, protocol,
00571                                 rdclass, "pkcs11", label, NULL, mctx, &key);
00572         isc_entropy_stopcallbacksources(ectx);
00573 
00574         if (ret != ISC_R_SUCCESS) {
00575                 char namestr[DNS_NAME_FORMATSIZE];
00576                 char algstr[DNS_SECALG_FORMATSIZE];
00577                 dns_name_format(name, namestr, sizeof(namestr));
00578                 dns_secalg_format(alg, algstr, sizeof(algstr));
00579                 fatal("failed to get key %s/%s: %s",
00580                       namestr, algstr, isc_result_totext(ret));
00581                 /* NOTREACHED */
00582                 exit(-1);
00583         }
00584 
00585         /*
00586          * Set key timing metadata (unless using -C)
00587          *
00588          * Publish and activation dates are set to "now" by default, but
00589          * can be overridden.  Creation date is always set to "now".
00590          */
00591         if (!oldstyle) {
00592                 dst_key_settime(key, DST_TIME_CREATED, now);
00593 
00594                 if (genonly && (setpub || setact))
00595                         fatal("cannot use -G together with -P or -A options");
00596 
00597                 if (setpub)
00598                         dst_key_settime(key, DST_TIME_PUBLISH, publish);
00599                 else if (setact)
00600                         dst_key_settime(key, DST_TIME_PUBLISH, activate);
00601                 else if (!genonly && !unsetpub)
00602                         dst_key_settime(key, DST_TIME_PUBLISH, now);
00603 
00604                 if (setact)
00605                         dst_key_settime(key, DST_TIME_ACTIVATE, activate);
00606                 else if (!genonly && !unsetact)
00607                         dst_key_settime(key, DST_TIME_ACTIVATE, now);
00608 
00609                 if (setrev) {
00610                         if (kskflag == 0)
00611                                 fprintf(stderr, "%s: warning: Key is "
00612                                         "not flagged as a KSK, but -R "
00613                                         "was used. Revoking a ZSK is "
00614                                         "legal, but undefined.\n",
00615                                         program);
00616                         dst_key_settime(key, DST_TIME_REVOKE, revoke);
00617                 }
00618 
00619                 if (setinact)
00620                         dst_key_settime(key, DST_TIME_INACTIVE, inactive);
00621 
00622                 if (setdel)
00623                         dst_key_settime(key, DST_TIME_DELETE, delete);
00624         } else {
00625                 if (setpub || setact || setrev || setinact ||
00626                     setdel || unsetpub || unsetact ||
00627                     unsetrev || unsetinact || unsetdel || genonly)
00628                         fatal("cannot use -C together with "
00629                               "-P, -A, -R, -I, -D, or -G options");
00630                 /*
00631                  * Compatibility mode: Private-key-format
00632                  * should be set to 1.2.
00633                  */
00634                 dst_key_setprivateformat(key, 1, 2);
00635         }
00636 
00637         /* Set default key TTL */
00638         if (setttl)
00639                 dst_key_setttl(key, ttl);
00640 
00641         /*
00642          * Do not overwrite an existing key.  Warn LOUDLY if there
00643          * is a risk of ID collision due to this key or another key
00644          * being revoked.
00645          */
00646         if (key_collision(key, name, directory, mctx, &exact)) {
00647                 isc_buffer_clear(&buf);
00648                 ret = dst_key_buildfilename(key, 0, directory, &buf);
00649                 if (ret != ISC_R_SUCCESS)
00650                         fatal("dst_key_buildfilename returned: %s\n",
00651                               isc_result_totext(ret));
00652                 if (exact)
00653                         fatal("%s: %s already exists\n", program, filename);
00654 
00655                 if (avoid_collisions)
00656                         fatal("%s: %s could collide with another key upon "
00657                               "revokation\n", program, filename);
00658 
00659                 fprintf(stderr, "%s: WARNING: Key %s could collide with "
00660                                 "another key upon revokation.  If you plan "
00661                                 "to revoke keys, destroy this key and "
00662                                 "generate a different one.\n",
00663                                 program, filename);
00664         }
00665 
00666         ret = dst_key_tofile(key, options, directory);
00667         if (ret != ISC_R_SUCCESS) {
00668                 char keystr[DST_KEY_FORMATSIZE];
00669                 dst_key_format(key, keystr, sizeof(keystr));
00670                 fatal("failed to write key %s: %s\n", keystr,
00671                       isc_result_totext(ret));
00672         }
00673 
00674         isc_buffer_clear(&buf);
00675         ret = dst_key_buildfilename(key, 0, NULL, &buf);
00676         if (ret != ISC_R_SUCCESS)
00677                 fatal("dst_key_buildfilename returned: %s\n",
00678                       isc_result_totext(ret));
00679         printf("%s\n", filename);
00680         dst_key_free(&key);
00681         if (prevkey != NULL)
00682                 dst_key_free(&prevkey);
00683 
00684         cleanup_logging(&log);
00685         cleanup_entropy(&ectx);
00686         dst_lib_destroy();
00687         dns_name_destroy();
00688         if (verbose > 10)
00689                 isc_mem_stats(mctx, stdout);
00690         isc_mem_free(mctx, label);
00691         isc_mem_destroy(&mctx);
00692 
00693         if (freeit != NULL)
00694                 free(freeit);
00695 
00696         return (0);
00697 }

Generated on Tue Apr 28 17:40:53 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha