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

https://bitbucket.org/freebsd/freebsd-head/ · C · 570 lines · 470 code · 63 blank · 37 comment · 176 complexity · 4b85619adc79496df43fd5f24f96b224 MD5 · raw file

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