/contrib/bind9/bin/dnssec/dnssec-keygen.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1030 lines · 869 code · 94 blank · 67 comment · 377 complexity · 8c3555d71d0311d23bb8be6dc8d3fe90 MD5 · raw file

  1. /*
  2. * Portions Copyright (C) 2004-2011 Internet Systems Consortium, Inc. ("ISC")
  3. * Portions Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
  10. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
  12. * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  15. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. *
  17. * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
  18. *
  19. * Permission to use, copy, modify, and/or distribute this software for any
  20. * purpose with or without fee is hereby granted, provided that the above
  21. * copyright notice and this permission notice appear in all copies.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
  24. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
  26. * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  27. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  28. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  29. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  30. */
  31. /* $Id: dnssec-keygen.c,v 1.115.14.4 2011/11/30 00:51:38 marka Exp $ */
  32. /*! \file */
  33. #include <config.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <isc/buffer.h>
  38. #include <isc/commandline.h>
  39. #include <isc/entropy.h>
  40. #include <isc/mem.h>
  41. #include <isc/region.h>
  42. #include <isc/string.h>
  43. #include <isc/util.h>
  44. #include <dns/dnssec.h>
  45. #include <dns/fixedname.h>
  46. #include <dns/keyvalues.h>
  47. #include <dns/log.h>
  48. #include <dns/name.h>
  49. #include <dns/rdataclass.h>
  50. #include <dns/result.h>
  51. #include <dns/secalg.h>
  52. #include <dst/dst.h>
  53. #include "dnssectool.h"
  54. #define MAX_RSA 4096 /* should be long enough... */
  55. const char *program = "dnssec-keygen";
  56. int verbose;
  57. #define DEFAULT_ALGORITHM "RSASHA1"
  58. #define DEFAULT_NSEC3_ALGORITHM "NSEC3RSASHA1"
  59. ISC_PLATFORM_NORETURN_PRE static void
  60. usage(void) ISC_PLATFORM_NORETURN_POST;
  61. static void progress(int p);
  62. static void
  63. usage(void) {
  64. fprintf(stderr, "Usage:\n");
  65. fprintf(stderr, " %s [options] name\n\n", program);
  66. fprintf(stderr, "Version: %s\n", VERSION);
  67. fprintf(stderr, " name: owner of the key\n");
  68. fprintf(stderr, "Options:\n");
  69. fprintf(stderr, " -K <directory>: write keys into directory\n");
  70. fprintf(stderr, " -a <algorithm>:\n");
  71. fprintf(stderr, " RSA | RSAMD5 | DSA | RSASHA1 | NSEC3RSASHA1"
  72. " | NSEC3DSA |\n");
  73. fprintf(stderr, " RSASHA256 | RSASHA512 | ECCGOST |\n");
  74. fprintf(stderr, " DH | HMAC-MD5 | HMAC-SHA1 | HMAC-SHA224 | "
  75. "HMAC-SHA256 | \n");
  76. fprintf(stderr, " HMAC-SHA384 | HMAC-SHA512\n");
  77. fprintf(stderr, " (default: RSASHA1, or "
  78. "NSEC3RSASHA1 if using -3)\n");
  79. fprintf(stderr, " -3: use NSEC3-capable algorithm\n");
  80. fprintf(stderr, " -b <key size in bits>:\n");
  81. fprintf(stderr, " RSAMD5:\t[512..%d]\n", MAX_RSA);
  82. fprintf(stderr, " RSASHA1:\t[512..%d]\n", MAX_RSA);
  83. fprintf(stderr, " NSEC3RSASHA1:\t[512..%d]\n", MAX_RSA);
  84. fprintf(stderr, " RSASHA256:\t[512..%d]\n", MAX_RSA);
  85. fprintf(stderr, " RSASHA512:\t[1024..%d]\n", MAX_RSA);
  86. fprintf(stderr, " DH:\t\t[128..4096]\n");
  87. fprintf(stderr, " DSA:\t\t[512..1024] and divisible by 64\n");
  88. fprintf(stderr, " NSEC3DSA:\t[512..1024] and divisible "
  89. "by 64\n");
  90. fprintf(stderr, " ECCGOST:\tignored\n");
  91. fprintf(stderr, " HMAC-MD5:\t[1..512]\n");
  92. fprintf(stderr, " HMAC-SHA1:\t[1..160]\n");
  93. fprintf(stderr, " HMAC-SHA224:\t[1..224]\n");
  94. fprintf(stderr, " HMAC-SHA256:\t[1..256]\n");
  95. fprintf(stderr, " HMAC-SHA384:\t[1..384]\n");
  96. fprintf(stderr, " HMAC-SHA512:\t[1..512]\n");
  97. fprintf(stderr, " (if using the default algorithm, key size\n"
  98. " defaults to 2048 for KSK, or 1024 for all "
  99. "others)\n");
  100. fprintf(stderr, " -n <nametype>: ZONE | HOST | ENTITY | "
  101. "USER | OTHER\n");
  102. fprintf(stderr, " (DNSKEY generation defaults to ZONE)\n");
  103. fprintf(stderr, " -c <class>: (default: IN)\n");
  104. fprintf(stderr, " -d <digest bits> (0 => max, default)\n");
  105. #ifdef USE_PKCS11
  106. fprintf(stderr, " -E <engine name> (default \"pkcs11\")\n");
  107. #else
  108. fprintf(stderr, " -E <engine name>\n");
  109. #endif
  110. fprintf(stderr, " -e: use large exponent (RSAMD5/RSASHA1 only)\n");
  111. fprintf(stderr, " -f <keyflag>: KSK | REVOKE\n");
  112. fprintf(stderr, " -g <generator>: use specified generator "
  113. "(DH only)\n");
  114. fprintf(stderr, " -p <protocol>: (default: 3 [dnssec])\n");
  115. fprintf(stderr, " -s <strength>: strength value this key signs DNS "
  116. "records with (default: 0)\n");
  117. fprintf(stderr, " -T <rrtype>: DNSKEY | KEY (default: DNSKEY; "
  118. "use KEY for SIG(0))\n");
  119. fprintf(stderr, " ECCGOST:\tignored\n");
  120. fprintf(stderr, " -t <type>: "
  121. "AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF "
  122. "(default: AUTHCONF)\n");
  123. fprintf(stderr, " -r <randomdev>: a file containing random data\n");
  124. fprintf(stderr, " -h: print usage and exit\n");
  125. fprintf(stderr, " -m <memory debugging mode>:\n");
  126. fprintf(stderr, " usage | trace | record | size | mctx\n");
  127. fprintf(stderr, " -v <level>: set verbosity level (0 - 10)\n");
  128. fprintf(stderr, "Timing options:\n");
  129. fprintf(stderr, " -P date/[+-]offset/none: set key publication date "
  130. "(default: now)\n");
  131. fprintf(stderr, " -A date/[+-]offset/none: set key activation date "
  132. "(default: now)\n");
  133. fprintf(stderr, " -R date/[+-]offset/none: set key "
  134. "revocation date\n");
  135. fprintf(stderr, " -I date/[+-]offset/none: set key "
  136. "inactivation date\n");
  137. fprintf(stderr, " -D date/[+-]offset/none: set key deletion date\n");
  138. fprintf(stderr, " -G: generate key only; do not set -P or -A\n");
  139. fprintf(stderr, " -C: generate a backward-compatible key, omitting "
  140. "all dates\n");
  141. fprintf(stderr, " -S <key>: generate a successor to an existing "
  142. "key\n");
  143. fprintf(stderr, " -i <interval>: prepublication interval for "
  144. "successor key "
  145. "(default: 30 days)\n");
  146. fprintf(stderr, "Output:\n");
  147. fprintf(stderr, " K<name>+<alg>+<id>.key, "
  148. "K<name>+<alg>+<id>.private\n");
  149. exit (-1);
  150. }
  151. static isc_boolean_t
  152. dsa_size_ok(int size) {
  153. return (ISC_TF(size >= 512 && size <= 1024 && size % 64 == 0));
  154. }
  155. static void
  156. progress(int p)
  157. {
  158. char c = '*';
  159. switch (p) {
  160. case 0:
  161. c = '.';
  162. break;
  163. case 1:
  164. c = '+';
  165. break;
  166. case 2:
  167. c = '*';
  168. break;
  169. case 3:
  170. c = ' ';
  171. break;
  172. default:
  173. break;
  174. }
  175. (void) putc(c, stderr);
  176. (void) fflush(stderr);
  177. }
  178. int
  179. main(int argc, char **argv) {
  180. char *algname = NULL, *freeit = NULL;
  181. char *nametype = NULL, *type = NULL;
  182. char *classname = NULL;
  183. char *endp;
  184. dst_key_t *key = NULL;
  185. dns_fixedname_t fname;
  186. dns_name_t *name;
  187. isc_uint16_t flags = 0, kskflag = 0, revflag = 0;
  188. dns_secalg_t alg;
  189. isc_boolean_t conflict = ISC_FALSE, null_key = ISC_FALSE;
  190. isc_boolean_t oldstyle = ISC_FALSE;
  191. isc_mem_t *mctx = NULL;
  192. int ch, rsa_exp = 0, generator = 0, param = 0;
  193. int protocol = -1, size = -1, signatory = 0;
  194. isc_result_t ret;
  195. isc_textregion_t r;
  196. char filename[255];
  197. const char *directory = NULL;
  198. const char *predecessor = NULL;
  199. dst_key_t *prevkey = NULL;
  200. isc_buffer_t buf;
  201. isc_log_t *log = NULL;
  202. isc_entropy_t *ectx = NULL;
  203. #ifdef USE_PKCS11
  204. const char *engine = "pkcs11";
  205. #else
  206. const char *engine = NULL;
  207. #endif
  208. dns_rdataclass_t rdclass;
  209. int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
  210. int dbits = 0;
  211. isc_boolean_t use_default = ISC_FALSE, use_nsec3 = ISC_FALSE;
  212. isc_stdtime_t publish = 0, activate = 0, revoke = 0;
  213. isc_stdtime_t inactive = 0, delete = 0;
  214. isc_stdtime_t now;
  215. int prepub = -1;
  216. isc_boolean_t setpub = ISC_FALSE, setact = ISC_FALSE;
  217. isc_boolean_t setrev = ISC_FALSE, setinact = ISC_FALSE;
  218. isc_boolean_t setdel = ISC_FALSE;
  219. isc_boolean_t unsetpub = ISC_FALSE, unsetact = ISC_FALSE;
  220. isc_boolean_t unsetrev = ISC_FALSE, unsetinact = ISC_FALSE;
  221. isc_boolean_t unsetdel = ISC_FALSE;
  222. isc_boolean_t genonly = ISC_FALSE;
  223. isc_boolean_t quiet = ISC_FALSE;
  224. isc_boolean_t show_progress = ISC_FALSE;
  225. unsigned char c;
  226. if (argc == 1)
  227. usage();
  228. dns_result_register();
  229. isc_commandline_errprint = ISC_FALSE;
  230. /*
  231. * Process memory debugging argument first.
  232. */
  233. #define CMDLINE_FLAGS "3A:a:b:Cc:D:d:E:eFf:Gg:hI:i:K:km:n:P:p:qR:r:S:s:T:t:v:"
  234. while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
  235. switch (ch) {
  236. case 'm':
  237. if (strcasecmp(isc_commandline_argument, "record") == 0)
  238. isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
  239. if (strcasecmp(isc_commandline_argument, "trace") == 0)
  240. isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
  241. if (strcasecmp(isc_commandline_argument, "usage") == 0)
  242. isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
  243. if (strcasecmp(isc_commandline_argument, "size") == 0)
  244. isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
  245. if (strcasecmp(isc_commandline_argument, "mctx") == 0)
  246. isc_mem_debugging |= ISC_MEM_DEBUGCTX;
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. isc_commandline_reset = ISC_TRUE;
  253. RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
  254. isc_stdtime_get(&now);
  255. while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
  256. switch (ch) {
  257. case '3':
  258. use_nsec3 = ISC_TRUE;
  259. break;
  260. case 'a':
  261. algname = isc_commandline_argument;
  262. break;
  263. case 'b':
  264. size = strtol(isc_commandline_argument, &endp, 10);
  265. if (*endp != '\0' || size < 0)
  266. fatal("-b requires a non-negative number");
  267. break;
  268. case 'C':
  269. oldstyle = ISC_TRUE;
  270. break;
  271. case 'c':
  272. classname = isc_commandline_argument;
  273. break;
  274. case 'd':
  275. dbits = strtol(isc_commandline_argument, &endp, 10);
  276. if (*endp != '\0' || dbits < 0)
  277. fatal("-d requires a non-negative number");
  278. break;
  279. case 'E':
  280. engine = isc_commandline_argument;
  281. break;
  282. case 'e':
  283. rsa_exp = 1;
  284. break;
  285. case 'f':
  286. c = (unsigned char)(isc_commandline_argument[0]);
  287. if (toupper(c) == 'K')
  288. kskflag = DNS_KEYFLAG_KSK;
  289. else if (toupper(c) == 'R')
  290. revflag = DNS_KEYFLAG_REVOKE;
  291. else
  292. fatal("unknown flag '%s'",
  293. isc_commandline_argument);
  294. break;
  295. case 'g':
  296. generator = strtol(isc_commandline_argument,
  297. &endp, 10);
  298. if (*endp != '\0' || generator <= 0)
  299. fatal("-g requires a positive number");
  300. break;
  301. case 'K':
  302. directory = isc_commandline_argument;
  303. ret = try_dir(directory);
  304. if (ret != ISC_R_SUCCESS)
  305. fatal("cannot open directory %s: %s",
  306. directory, isc_result_totext(ret));
  307. break;
  308. case 'k':
  309. fatal("The -k option has been deprecated.\n"
  310. "To generate a key-signing key, use -f KSK.\n"
  311. "To generate a key with TYPE=KEY, use -T KEY.\n");
  312. break;
  313. case 'n':
  314. nametype = isc_commandline_argument;
  315. break;
  316. case 'm':
  317. break;
  318. case 'p':
  319. protocol = strtol(isc_commandline_argument, &endp, 10);
  320. if (*endp != '\0' || protocol < 0 || protocol > 255)
  321. fatal("-p must be followed by a number "
  322. "[0..255]");
  323. break;
  324. case 'q':
  325. quiet = ISC_TRUE;
  326. break;
  327. case 'r':
  328. setup_entropy(mctx, isc_commandline_argument, &ectx);
  329. break;
  330. case 's':
  331. signatory = strtol(isc_commandline_argument,
  332. &endp, 10);
  333. if (*endp != '\0' || signatory < 0 || signatory > 15)
  334. fatal("-s must be followed by a number "
  335. "[0..15]");
  336. break;
  337. case 'T':
  338. if (strcasecmp(isc_commandline_argument, "KEY") == 0)
  339. options |= DST_TYPE_KEY;
  340. else if (strcasecmp(isc_commandline_argument,
  341. "DNSKEY") == 0)
  342. /* default behavior */
  343. ;
  344. else
  345. fatal("unknown type '%s'",
  346. isc_commandline_argument);
  347. break;
  348. case 't':
  349. type = isc_commandline_argument;
  350. break;
  351. case 'v':
  352. endp = NULL;
  353. verbose = strtol(isc_commandline_argument, &endp, 0);
  354. if (*endp != '\0')
  355. fatal("-v must be followed by a number");
  356. break;
  357. case 'z':
  358. /* already the default */
  359. break;
  360. case 'G':
  361. genonly = ISC_TRUE;
  362. break;
  363. case 'P':
  364. if (setpub || unsetpub)
  365. fatal("-P specified more than once");
  366. if (strcasecmp(isc_commandline_argument, "none")) {
  367. setpub = ISC_TRUE;
  368. publish = strtotime(isc_commandline_argument,
  369. now, now);
  370. } else {
  371. unsetpub = ISC_TRUE;
  372. }
  373. break;
  374. case 'A':
  375. if (setact || unsetact)
  376. fatal("-A specified more than once");
  377. if (strcasecmp(isc_commandline_argument, "none")) {
  378. setact = ISC_TRUE;
  379. activate = strtotime(isc_commandline_argument,
  380. now, now);
  381. } else {
  382. unsetact = ISC_TRUE;
  383. }
  384. break;
  385. case 'R':
  386. if (setrev || unsetrev)
  387. fatal("-R specified more than once");
  388. if (strcasecmp(isc_commandline_argument, "none")) {
  389. setrev = ISC_TRUE;
  390. revoke = strtotime(isc_commandline_argument,
  391. now, now);
  392. } else {
  393. unsetrev = ISC_TRUE;
  394. }
  395. break;
  396. case 'I':
  397. if (setinact || unsetinact)
  398. fatal("-I specified more than once");
  399. if (strcasecmp(isc_commandline_argument, "none")) {
  400. setinact = ISC_TRUE;
  401. inactive = strtotime(isc_commandline_argument,
  402. now, now);
  403. } else {
  404. unsetinact = ISC_TRUE;
  405. }
  406. break;
  407. case 'D':
  408. if (setdel || unsetdel)
  409. fatal("-D specified more than once");
  410. if (strcasecmp(isc_commandline_argument, "none")) {
  411. setdel = ISC_TRUE;
  412. delete = strtotime(isc_commandline_argument,
  413. now, now);
  414. } else {
  415. unsetdel = ISC_TRUE;
  416. }
  417. break;
  418. case 'S':
  419. predecessor = isc_commandline_argument;
  420. break;
  421. case 'i':
  422. prepub = strtottl(isc_commandline_argument);
  423. break;
  424. case 'F':
  425. /* Reserved for FIPS mode */
  426. /* FALLTHROUGH */
  427. case '?':
  428. if (isc_commandline_option != '?')
  429. fprintf(stderr, "%s: invalid argument -%c\n",
  430. program, isc_commandline_option);
  431. /* FALLTHROUGH */
  432. case 'h':
  433. usage();
  434. default:
  435. fprintf(stderr, "%s: unhandled option -%c\n",
  436. program, isc_commandline_option);
  437. exit(1);
  438. }
  439. }
  440. if (!isatty(0))
  441. quiet = ISC_TRUE;
  442. if (ectx == NULL)
  443. setup_entropy(mctx, NULL, &ectx);
  444. ret = dst_lib_init2(mctx, ectx, engine,
  445. ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
  446. if (ret != ISC_R_SUCCESS)
  447. fatal("could not initialize dst: %s",
  448. isc_result_totext(ret));
  449. setup_logging(verbose, mctx, &log);
  450. if (predecessor == NULL) {
  451. if (prepub == -1)
  452. prepub = 0;
  453. if (argc < isc_commandline_index + 1)
  454. fatal("the key name was not specified");
  455. if (argc > isc_commandline_index + 1)
  456. fatal("extraneous arguments");
  457. dns_fixedname_init(&fname);
  458. name = dns_fixedname_name(&fname);
  459. isc_buffer_init(&buf, argv[isc_commandline_index],
  460. strlen(argv[isc_commandline_index]));
  461. isc_buffer_add(&buf, strlen(argv[isc_commandline_index]));
  462. ret = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
  463. if (ret != ISC_R_SUCCESS)
  464. fatal("invalid key name %s: %s",
  465. argv[isc_commandline_index],
  466. isc_result_totext(ret));
  467. if (algname == NULL) {
  468. use_default = ISC_TRUE;
  469. if (use_nsec3)
  470. algname = strdup(DEFAULT_NSEC3_ALGORITHM);
  471. else
  472. algname = strdup(DEFAULT_ALGORITHM);
  473. if (algname == NULL)
  474. fatal("strdup failed");
  475. freeit = algname;
  476. if (verbose > 0)
  477. fprintf(stderr, "no algorithm specified; "
  478. "defaulting to %s\n", algname);
  479. }
  480. if (strcasecmp(algname, "RSA") == 0) {
  481. fprintf(stderr, "The use of RSA (RSAMD5) is not "
  482. "recommended.\nIf you still wish to "
  483. "use RSA (RSAMD5) please specify "
  484. "\"-a RSAMD5\"\n");
  485. return (1);
  486. } else if (strcasecmp(algname, "HMAC-MD5") == 0)
  487. alg = DST_ALG_HMACMD5;
  488. else if (strcasecmp(algname, "HMAC-SHA1") == 0)
  489. alg = DST_ALG_HMACSHA1;
  490. else if (strcasecmp(algname, "HMAC-SHA224") == 0)
  491. alg = DST_ALG_HMACSHA224;
  492. else if (strcasecmp(algname, "HMAC-SHA256") == 0)
  493. alg = DST_ALG_HMACSHA256;
  494. else if (strcasecmp(algname, "HMAC-SHA384") == 0)
  495. alg = DST_ALG_HMACSHA384;
  496. else if (strcasecmp(algname, "HMAC-SHA512") == 0)
  497. alg = DST_ALG_HMACSHA512;
  498. else {
  499. r.base = algname;
  500. r.length = strlen(algname);
  501. ret = dns_secalg_fromtext(&alg, &r);
  502. if (ret != ISC_R_SUCCESS)
  503. fatal("unknown algorithm %s", algname);
  504. if (alg == DST_ALG_DH)
  505. options |= DST_TYPE_KEY;
  506. }
  507. if (use_nsec3 &&
  508. alg != DST_ALG_NSEC3DSA && alg != DST_ALG_NSEC3RSASHA1 &&
  509. alg != DST_ALG_RSASHA256 && alg!= DST_ALG_RSASHA512 &&
  510. alg != DST_ALG_ECCGOST) {
  511. fatal("%s is incompatible with NSEC3; "
  512. "do not use the -3 option", algname);
  513. }
  514. if (type != NULL && (options & DST_TYPE_KEY) != 0) {
  515. if (strcasecmp(type, "NOAUTH") == 0)
  516. flags |= DNS_KEYTYPE_NOAUTH;
  517. else if (strcasecmp(type, "NOCONF") == 0)
  518. flags |= DNS_KEYTYPE_NOCONF;
  519. else if (strcasecmp(type, "NOAUTHCONF") == 0) {
  520. flags |= (DNS_KEYTYPE_NOAUTH |
  521. DNS_KEYTYPE_NOCONF);
  522. if (size < 0)
  523. size = 0;
  524. }
  525. else if (strcasecmp(type, "AUTHCONF") == 0)
  526. /* nothing */;
  527. else
  528. fatal("invalid type %s", type);
  529. }
  530. if (size < 0) {
  531. if (use_default) {
  532. if ((kskflag & DNS_KEYFLAG_KSK) != 0)
  533. size = 2048;
  534. else
  535. size = 1024;
  536. if (verbose > 0)
  537. fprintf(stderr, "key size not "
  538. "specified; defaulting "
  539. "to %d\n", size);
  540. } else if (alg != DST_ALG_ECCGOST)
  541. fatal("key size not specified (-b option)");
  542. }
  543. if (!oldstyle && prepub > 0) {
  544. if (setpub && setact && (activate - prepub) < publish)
  545. fatal("Activation and publication dates "
  546. "are closer together than the\n\t"
  547. "prepublication interval.");
  548. if (!setpub && !setact) {
  549. setpub = setact = ISC_TRUE;
  550. publish = now;
  551. activate = now + prepub;
  552. } else if (setpub && !setact) {
  553. setact = ISC_TRUE;
  554. activate = publish + prepub;
  555. } else if (setact && !setpub) {
  556. setpub = ISC_TRUE;
  557. publish = activate - prepub;
  558. }
  559. if ((activate - prepub) < now)
  560. fatal("Time until activation is shorter "
  561. "than the\n\tprepublication interval.");
  562. }
  563. } else {
  564. char keystr[DST_KEY_FORMATSIZE];
  565. isc_stdtime_t when;
  566. int major, minor;
  567. if (prepub == -1)
  568. prepub = (30 * 86400);
  569. if (algname != NULL)
  570. fatal("-S and -a cannot be used together");
  571. if (size >= 0)
  572. fatal("-S and -b cannot be used together");
  573. if (nametype != NULL)
  574. fatal("-S and -n cannot be used together");
  575. if (type != NULL)
  576. fatal("-S and -t cannot be used together");
  577. if (setpub || unsetpub)
  578. fatal("-S and -P cannot be used together");
  579. if (setact || unsetact)
  580. fatal("-S and -A cannot be used together");
  581. if (use_nsec3)
  582. fatal("-S and -3 cannot be used together");
  583. if (oldstyle)
  584. fatal("-S and -C cannot be used together");
  585. if (genonly)
  586. fatal("-S and -G cannot be used together");
  587. ret = dst_key_fromnamedfile(predecessor, directory,
  588. DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
  589. mctx, &prevkey);
  590. if (ret != ISC_R_SUCCESS)
  591. fatal("Invalid keyfile %s: %s",
  592. filename, isc_result_totext(ret));
  593. if (!dst_key_isprivate(prevkey))
  594. fatal("%s is not a private key", filename);
  595. name = dst_key_name(prevkey);
  596. alg = dst_key_alg(prevkey);
  597. size = dst_key_size(prevkey);
  598. flags = dst_key_flags(prevkey);
  599. dst_key_format(prevkey, keystr, sizeof(keystr));
  600. dst_key_getprivateformat(prevkey, &major, &minor);
  601. if (major != DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
  602. fatal("Key %s has incompatible format version %d.%d\n\t"
  603. "It is not possible to generate a successor key.",
  604. keystr, major, minor);
  605. ret = dst_key_gettime(prevkey, DST_TIME_ACTIVATE, &when);
  606. if (ret != ISC_R_SUCCESS)
  607. fatal("Key %s has no activation date.\n\t"
  608. "You must use dnssec-settime -A to set one "
  609. "before generating a successor.", keystr);
  610. ret = dst_key_gettime(prevkey, DST_TIME_INACTIVE, &activate);
  611. if (ret != ISC_R_SUCCESS)
  612. fatal("Key %s has no inactivation date.\n\t"
  613. "You must use dnssec-settime -I to set one "
  614. "before generating a successor.", keystr);
  615. publish = activate - prepub;
  616. if (publish < now)
  617. fatal("Key %s becomes inactive\n\t"
  618. "sooner than the prepublication period "
  619. "for the new key ends.\n\t"
  620. "Either change the inactivation date with "
  621. "dnssec-settime -I,\n\t"
  622. "or use the -i option to set a shorter "
  623. "prepublication interval.", keystr);
  624. ret = dst_key_gettime(prevkey, DST_TIME_DELETE, &when);
  625. if (ret != ISC_R_SUCCESS)
  626. fprintf(stderr, "%s: WARNING: Key %s has no removal "
  627. "date;\n\t it will remain in the zone "
  628. "indefinitely after rollover.\n\t "
  629. "You can use dnssec-settime -D to "
  630. "change this.\n", program, keystr);
  631. setpub = setact = ISC_TRUE;
  632. }
  633. switch (alg) {
  634. case DNS_KEYALG_RSAMD5:
  635. case DNS_KEYALG_RSASHA1:
  636. case DNS_KEYALG_NSEC3RSASHA1:
  637. case DNS_KEYALG_RSASHA256:
  638. if (size != 0 && (size < 512 || size > MAX_RSA))
  639. fatal("RSA key size %d out of range", size);
  640. break;
  641. case DNS_KEYALG_RSASHA512:
  642. if (size != 0 && (size < 1024 || size > MAX_RSA))
  643. fatal("RSA key size %d out of range", size);
  644. break;
  645. case DNS_KEYALG_DH:
  646. if (size != 0 && (size < 128 || size > 4096))
  647. fatal("DH key size %d out of range", size);
  648. break;
  649. case DNS_KEYALG_DSA:
  650. case DNS_KEYALG_NSEC3DSA:
  651. if (size != 0 && !dsa_size_ok(size))
  652. fatal("invalid DSS key size: %d", size);
  653. break;
  654. case DST_ALG_ECCGOST:
  655. break;
  656. case DST_ALG_HMACMD5:
  657. options |= DST_TYPE_KEY;
  658. if (size < 1 || size > 512)
  659. fatal("HMAC-MD5 key size %d out of range", size);
  660. if (dbits != 0 && (dbits < 80 || dbits > 128))
  661. fatal("HMAC-MD5 digest bits %d out of range", dbits);
  662. if ((dbits % 8) != 0)
  663. fatal("HMAC-MD5 digest bits %d not divisible by 8",
  664. dbits);
  665. break;
  666. case DST_ALG_HMACSHA1:
  667. options |= DST_TYPE_KEY;
  668. if (size < 1 || size > 160)
  669. fatal("HMAC-SHA1 key size %d out of range", size);
  670. if (dbits != 0 && (dbits < 80 || dbits > 160))
  671. fatal("HMAC-SHA1 digest bits %d out of range", dbits);
  672. if ((dbits % 8) != 0)
  673. fatal("HMAC-SHA1 digest bits %d not divisible by 8",
  674. dbits);
  675. break;
  676. case DST_ALG_HMACSHA224:
  677. options |= DST_TYPE_KEY;
  678. if (size < 1 || size > 224)
  679. fatal("HMAC-SHA224 key size %d out of range", size);
  680. if (dbits != 0 && (dbits < 112 || dbits > 224))
  681. fatal("HMAC-SHA224 digest bits %d out of range", dbits);
  682. if ((dbits % 8) != 0)
  683. fatal("HMAC-SHA224 digest bits %d not divisible by 8",
  684. dbits);
  685. break;
  686. case DST_ALG_HMACSHA256:
  687. options |= DST_TYPE_KEY;
  688. if (size < 1 || size > 256)
  689. fatal("HMAC-SHA256 key size %d out of range", size);
  690. if (dbits != 0 && (dbits < 128 || dbits > 256))
  691. fatal("HMAC-SHA256 digest bits %d out of range", dbits);
  692. if ((dbits % 8) != 0)
  693. fatal("HMAC-SHA256 digest bits %d not divisible by 8",
  694. dbits);
  695. break;
  696. case DST_ALG_HMACSHA384:
  697. options |= DST_TYPE_KEY;
  698. if (size < 1 || size > 384)
  699. fatal("HMAC-384 key size %d out of range", size);
  700. if (dbits != 0 && (dbits < 192 || dbits > 384))
  701. fatal("HMAC-SHA384 digest bits %d out of range", dbits);
  702. if ((dbits % 8) != 0)
  703. fatal("HMAC-SHA384 digest bits %d not divisible by 8",
  704. dbits);
  705. break;
  706. case DST_ALG_HMACSHA512:
  707. options |= DST_TYPE_KEY;
  708. if (size < 1 || size > 512)
  709. fatal("HMAC-SHA512 key size %d out of range", size);
  710. if (dbits != 0 && (dbits < 256 || dbits > 512))
  711. fatal("HMAC-SHA512 digest bits %d out of range", dbits);
  712. if ((dbits % 8) != 0)
  713. fatal("HMAC-SHA512 digest bits %d not divisible by 8",
  714. dbits);
  715. break;
  716. }
  717. if (!(alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_RSASHA1 ||
  718. alg == DNS_KEYALG_NSEC3RSASHA1 || alg == DNS_KEYALG_RSASHA256 ||
  719. alg == DNS_KEYALG_RSASHA512 || alg == DST_ALG_ECCGOST) &&
  720. rsa_exp != 0)
  721. fatal("specified RSA exponent for a non-RSA key");
  722. if (alg != DNS_KEYALG_DH && generator != 0)
  723. fatal("specified DH generator for a non-DH key");
  724. if (nametype == NULL) {
  725. if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
  726. fatal("no nametype specified");
  727. flags |= DNS_KEYOWNER_ZONE; /* DNSKEY */
  728. } else if (strcasecmp(nametype, "zone") == 0)
  729. flags |= DNS_KEYOWNER_ZONE;
  730. else if ((options & DST_TYPE_KEY) != 0) { /* KEY / HMAC */
  731. if (strcasecmp(nametype, "host") == 0 ||
  732. strcasecmp(nametype, "entity") == 0)
  733. flags |= DNS_KEYOWNER_ENTITY;
  734. else if (strcasecmp(nametype, "user") == 0)
  735. flags |= DNS_KEYOWNER_USER;
  736. else
  737. fatal("invalid KEY nametype %s", nametype);
  738. } else if (strcasecmp(nametype, "other") != 0) /* DNSKEY */
  739. fatal("invalid DNSKEY nametype %s", nametype);
  740. rdclass = strtoclass(classname);
  741. if (directory == NULL)
  742. directory = ".";
  743. if ((options & DST_TYPE_KEY) != 0) /* KEY / HMAC */
  744. flags |= signatory;
  745. else if ((flags & DNS_KEYOWNER_ZONE) != 0) { /* DNSKEY */
  746. flags |= kskflag;
  747. flags |= revflag;
  748. }
  749. if (protocol == -1)
  750. protocol = DNS_KEYPROTO_DNSSEC;
  751. else if ((options & DST_TYPE_KEY) == 0 &&
  752. protocol != DNS_KEYPROTO_DNSSEC)
  753. fatal("invalid DNSKEY protocol: %d", protocol);
  754. if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
  755. if (size > 0)
  756. fatal("specified null key with non-zero size");
  757. if ((flags & DNS_KEYFLAG_SIGNATORYMASK) != 0)
  758. fatal("specified null key with signing authority");
  759. }
  760. if ((flags & DNS_KEYFLAG_OWNERMASK) == DNS_KEYOWNER_ZONE &&
  761. (alg == DNS_KEYALG_DH || alg == DST_ALG_HMACMD5 ||
  762. alg == DST_ALG_HMACSHA1 || alg == DST_ALG_HMACSHA224 ||
  763. alg == DST_ALG_HMACSHA256 || alg == DST_ALG_HMACSHA384 ||
  764. alg == DST_ALG_HMACSHA512))
  765. fatal("a key with algorithm '%s' cannot be a zone key",
  766. algname);
  767. switch(alg) {
  768. case DNS_KEYALG_RSAMD5:
  769. case DNS_KEYALG_RSASHA1:
  770. case DNS_KEYALG_NSEC3RSASHA1:
  771. case DNS_KEYALG_RSASHA256:
  772. case DNS_KEYALG_RSASHA512:
  773. param = rsa_exp;
  774. show_progress = ISC_TRUE;
  775. break;
  776. case DNS_KEYALG_DH:
  777. param = generator;
  778. break;
  779. case DNS_KEYALG_DSA:
  780. case DNS_KEYALG_NSEC3DSA:
  781. case DST_ALG_ECCGOST:
  782. show_progress = ISC_TRUE;
  783. /* fall through */
  784. case DST_ALG_HMACMD5:
  785. case DST_ALG_HMACSHA1:
  786. case DST_ALG_HMACSHA224:
  787. case DST_ALG_HMACSHA256:
  788. case DST_ALG_HMACSHA384:
  789. case DST_ALG_HMACSHA512:
  790. param = 0;
  791. break;
  792. }
  793. if ((flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY)
  794. null_key = ISC_TRUE;
  795. isc_buffer_init(&buf, filename, sizeof(filename) - 1);
  796. do {
  797. conflict = ISC_FALSE;
  798. if (!quiet && show_progress) {
  799. fprintf(stderr, "Generating key pair.");
  800. ret = dst_key_generate2(name, alg, size, param, flags,
  801. protocol, rdclass, mctx, &key,
  802. &progress);
  803. putc('\n', stderr);
  804. fflush(stderr);
  805. } else {
  806. ret = dst_key_generate2(name, alg, size, param, flags,
  807. protocol, rdclass, mctx, &key,
  808. NULL);
  809. }
  810. isc_entropy_stopcallbacksources(ectx);
  811. if (ret != ISC_R_SUCCESS) {
  812. char namestr[DNS_NAME_FORMATSIZE];
  813. char algstr[DNS_SECALG_FORMATSIZE];
  814. dns_name_format(name, namestr, sizeof(namestr));
  815. dns_secalg_format(alg, algstr, sizeof(algstr));
  816. fatal("failed to generate key %s/%s: %s\n",
  817. namestr, algstr, isc_result_totext(ret));
  818. /* NOTREACHED */
  819. exit(-1);
  820. }
  821. dst_key_setbits(key, dbits);
  822. /*
  823. * Set key timing metadata (unless using -C)
  824. *
  825. * Creation date is always set to "now".
  826. *
  827. * For a new key without an explicit predecessor, publish
  828. * and activation dates are set to "now" by default, but
  829. * can both be overridden.
  830. *
  831. * For a successor key, activation is set to match the
  832. * predecessor's inactivation date. Publish is set to 30
  833. * days earlier than that (XXX: this should be configurable).
  834. * If either of the resulting dates are in the past, that's
  835. * an error; the inactivation date of the predecessor key
  836. * must be updated before a successor key can be created.
  837. */
  838. if (!oldstyle) {
  839. dst_key_settime(key, DST_TIME_CREATED, now);
  840. if (genonly && (setpub || setact))
  841. fatal("cannot use -G together with "
  842. "-P or -A options");
  843. if (setpub)
  844. dst_key_settime(key, DST_TIME_PUBLISH, publish);
  845. else if (setact)
  846. dst_key_settime(key, DST_TIME_PUBLISH,
  847. activate);
  848. else if (!genonly && !unsetpub)
  849. dst_key_settime(key, DST_TIME_PUBLISH, now);
  850. if (setact)
  851. dst_key_settime(key, DST_TIME_ACTIVATE,
  852. activate);
  853. else if (!genonly && !unsetact)
  854. dst_key_settime(key, DST_TIME_ACTIVATE, now);
  855. if (setrev) {
  856. if (kskflag == 0)
  857. fprintf(stderr, "%s: warning: Key is "
  858. "not flagged as a KSK, but -R "
  859. "was used. Revoking a ZSK is "
  860. "legal, but undefined.\n",
  861. program);
  862. dst_key_settime(key, DST_TIME_REVOKE, revoke);
  863. }
  864. if (setinact)
  865. dst_key_settime(key, DST_TIME_INACTIVE,
  866. inactive);
  867. if (setdel)
  868. dst_key_settime(key, DST_TIME_DELETE, delete);
  869. } else {
  870. if (setpub || setact || setrev || setinact ||
  871. setdel || unsetpub || unsetact ||
  872. unsetrev || unsetinact || unsetdel || genonly)
  873. fatal("cannot use -C together with "
  874. "-P, -A, -R, -I, -D, or -G options");
  875. /*
  876. * Compatibility mode: Private-key-format
  877. * should be set to 1.2.
  878. */
  879. dst_key_setprivateformat(key, 1, 2);
  880. }
  881. /*
  882. * Do not overwrite an existing key, or create a key
  883. * if there is a risk of ID collision due to this key
  884. * or another key being revoked.
  885. */
  886. if (key_collision(key, name, directory, mctx, NULL)) {
  887. conflict = ISC_TRUE;
  888. if (null_key) {
  889. dst_key_free(&key);
  890. break;
  891. }
  892. if (verbose > 0) {
  893. isc_buffer_clear(&buf);
  894. ret = dst_key_buildfilename(key, 0,
  895. directory, &buf);
  896. if (ret == ISC_R_SUCCESS)
  897. fprintf(stderr,
  898. "%s: %s already exists, or "
  899. "might collide with another "
  900. "key upon revokation. "
  901. "Generating a new key\n",
  902. program, filename);
  903. }
  904. dst_key_free(&key);
  905. }
  906. } while (conflict == ISC_TRUE);
  907. if (conflict)
  908. fatal("cannot generate a null key due to possible key ID "
  909. "collision");
  910. ret = dst_key_tofile(key, options, directory);
  911. if (ret != ISC_R_SUCCESS) {
  912. char keystr[DST_KEY_FORMATSIZE];
  913. dst_key_format(key, keystr, sizeof(keystr));
  914. fatal("failed to write key %s: %s\n", keystr,
  915. isc_result_totext(ret));
  916. }
  917. isc_buffer_clear(&buf);
  918. ret = dst_key_buildfilename(key, 0, NULL, &buf);
  919. if (ret != ISC_R_SUCCESS)
  920. fatal("dst_key_buildfilename returned: %s\n",
  921. isc_result_totext(ret));
  922. printf("%s\n", filename);
  923. dst_key_free(&key);
  924. if (prevkey != NULL)
  925. dst_key_free(&prevkey);
  926. cleanup_logging(&log);
  927. cleanup_entropy(&ectx);
  928. dst_lib_destroy();
  929. dns_name_destroy();
  930. if (verbose > 10)
  931. isc_mem_stats(mctx, stdout);
  932. isc_mem_destroy(&mctx);
  933. if (freeit != NULL)
  934. free(freeit);
  935. return (0);
  936. }