dnssec-signkey.c revision 3de75141d2e042a1db4c58b397ea4fc905cecf6e
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley/*
72d6be2c1101df11c05360f78d47197027d419c3Automatic Updater * Copyright (C) 1999, 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * copyright notice and this permission notice appear in all copies.
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley */
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley
be40453080750077f1161c70f03c5f6921a27961Automatic Updater#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdlib.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley#include <isc/string.h>
a3a11c4f3fc9ba972802b811c4d95a9884d6ff4aMichael Sawyer#include <isc/commandline.h>
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley#include <isc/mem.h>
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews#include <isc/util.h>
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley#include <dns/db.h>
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley#include <dns/dnssec.h>
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley#include <dns/log.h>
2aa67e804d85f4d88153368ce65ce4df7b5390e6Bob Halley#include <dns/rdata.h>
de8661e517ed679cfaa12e47eb9a8e23829ed320David Lawrence#include <dns/rdatalist.h>
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley#include <dns/rdataset.h>
de8661e517ed679cfaa12e47eb9a8e23829ed320David Lawrence#include <dns/rdatastruct.h>
de8661e517ed679cfaa12e47eb9a8e23829ed320David Lawrence#include <dns/result.h>
90c4900d5cf1a1b556ae4c1de6338b2e7475ee55Mark Andrews
de8661e517ed679cfaa12e47eb9a8e23829ed320David Lawrence#define PROGRAM "keysigner"
764808211e952f1617aaa609281da66d80120c0dMark Andrews
764808211e952f1617aaa609281da66d80120c0dMark Andrews#define BUFSIZE 2048
764808211e952f1617aaa609281da66d80120c0dMark Andrews
de8661e517ed679cfaa12e47eb9a8e23829ed320David Lawrencetypedef struct keynode keynode_t;
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halleystruct keynode {
cf300e03de3df3ff422db922520bf07c686c86daMark Andrews dst_key_t *key;
76477bd0e0a8f150f06f45c347d286b782cfa679Brian Wellington isc_boolean_t verified;
108490a7f8529aff50a0ac7897580b59a73d9845David Lawrence ISC_LINK(keynode_t) link;
6d54a6fc180acaf8772c9447cb925b31f39c7158Mark Andrews};
a3a11c4f3fc9ba972802b811c4d95a9884d6ff4aMichael Sawyertypedef ISC_LIST(keynode_t) keylist_t;
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews
108490a7f8529aff50a0ac7897580b59a73d9845David Lawrencestatic isc_stdtime_t now;
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halleystatic int verbose;
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halleystatic isc_mem_t *mctx = NULL;
f754fa97bc698cc251d227173a95e4d39a88ac01Mark Andrewsstatic keylist_t keylist;
f754fa97bc698cc251d227173a95e4d39a88ac01Mark Andrews
f754fa97bc698cc251d227173a95e4d39a88ac01Mark Andrewsstatic inline void
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halleyfatal(char *message) {
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley fprintf(stderr, "%s: %s\n", PROGRAM, message);
c52ae25dd70636c673d4a299859137a1c8ba611aMark Andrews exit(1);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington}
ee80f4506479e189ca1320eb87ac89188c5a7848Mark Andrews
ee80f4506479e189ca1320eb87ac89188c5a7848Mark Andrewsstatic inline void
ee80f4506479e189ca1320eb87ac89188c5a7848Mark Andrewscheck_result(isc_result_t result, char *message) {
ee80f4506479e189ca1320eb87ac89188c5a7848Mark Andrews if (result != ISC_R_SUCCESS) {
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington fprintf(stderr, "%s: %s: %s\n", PROGRAM, message,
1d8cbe855fc355b80802dcf29f4ac24bebdd1193Brian Wellington isc_result_totext(result));
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington exit(1);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington }
f8727bd90366af835f551da1b5e1fdfcd2d3d01fBrian Wellington}
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellingtonstatic void
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellingtonusage() {
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington fprintf(stderr, "Usage:\n");
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews fprintf(stderr, "\tkeysigner [options] keyset keys\n");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews fprintf(stderr, "\n");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews fprintf(stderr, "Options: (default value in parenthesis) \n");
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews fprintf(stderr, "\t-v level:\n");
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews fprintf(stderr, "\t\tverbose level (0)\n");
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews fprintf(stderr, "\n");
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington fprintf(stderr, "keyset:\n");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington fprintf(stderr, "\tfile name of key set to be signed\n");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington fprintf(stderr, "keys:\n");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington fprintf(stderr, "\tkeyfile (Kname+alg+id)\n");
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews exit(0);
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews}
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrewsstatic void
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrewsloadkeys(dns_name_t *name, dns_rdataset_t *rdataset) {
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington dst_key_t *key;
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington dns_rdata_t rdata;
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews keynode_t *keynode;
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington isc_result_t result;
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington ISC_LIST_INIT(keylist);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington result = dns_rdataset_first(rdataset);
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews check_result(result, "dns_rdataset_first");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington for (; result == ISC_R_SUCCESS; result = dns_rdataset_next(rdataset)) {
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews dns_rdataset_current(rdataset, &rdata);
5d98cf67b32d785aca1a72ea1dc4d559fab39208Mark Andrews key = NULL;
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington result = dns_dnssec_keyfromrdata(name, &rdata, mctx, &key);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington if (result != ISC_R_SUCCESS)
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington continue;
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington if (!dst_key_iszonekey(key))
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington continue;
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley keynode = isc_mem_get(mctx, sizeof (keynode_t));
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley if (keynode == NULL)
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley check_result(ISC_R_NOMEMORY, "isc_mem_get()");
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley keynode->key = key;
4e5388b45908ce8b8b35825ca6f16c1d236643baBrian Wellington keynode->verified = ISC_FALSE;
c336121fb5a5c1b9cb9b2cd4cea50f0788270505Brian Wellington ISC_LINK_INIT(keynode, link);
2c329da87c5c886e7f4468c69a9e6323121068cbMark Andrews ISC_LIST_APPEND(keylist, keynode, link);
202991557a4b7e8d3df7725d84f0fcae90dbaee6David Lawrence }
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley if (result == ISC_R_NOMORE)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley result = ISC_R_SUCCESS;
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington check_result(result, "loadkeys()");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington}
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrewsstatic dst_key_t *
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrewsfindkey(dns_rdata_sig_t *sig) {
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews keynode_t *keynode;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein for (keynode = ISC_LIST_HEAD(keylist);
077daa21229ffaedda79588fa70fbaeae19ae998Bob Halley keynode != NULL;
077daa21229ffaedda79588fa70fbaeae19ae998Bob Halley keynode = ISC_LIST_NEXT(keynode, link))
077daa21229ffaedda79588fa70fbaeae19ae998Bob Halley {
077daa21229ffaedda79588fa70fbaeae19ae998Bob Halley if (dst_key_id(keynode->key) == sig->keyid &&
077daa21229ffaedda79588fa70fbaeae19ae998Bob Halley dst_key_alg(keynode->key) == sig->algorithm) {
077daa21229ffaedda79588fa70fbaeae19ae998Bob Halley keynode->verified = ISC_TRUE;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt return (keynode->key);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt }
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt }
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt fatal("signature generated by non-zone or missing key");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt return (NULL);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt}
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Huntint
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Huntmain(int argc, char *argv[]) {
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley int i, ch;
f8727bd90366af835f551da1b5e1fdfcd2d3d01fBrian Wellington char tdomain[1025];
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_fixedname_t fdomain;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington dns_name_t *domain;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington char *output = NULL;
1d8cbe855fc355b80802dcf29f4ac24bebdd1193Brian Wellington char *endp;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington unsigned char *data;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington dns_db_t *db;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington dns_dbnode_t *node;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington dns_dbversion_t *version;
da76a8046e01e1c1c2e6f75772afb2c4f202cc25Brian Wellington dst_key_t *key = NULL;
da76a8046e01e1c1c2e6f75772afb2c4f202cc25Brian Wellington dns_rdata_t *rdata, sigrdata;
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_rdatalist_t sigrdatalist;
d1e4b08844175357a925ddd6dcfa750cccd2b116Brian Wellington dns_rdataset_t rdataset, sigrdataset, newsigrdataset;
f8727bd90366af835f551da1b5e1fdfcd2d3d01fBrian Wellington dns_rdata_sig_t sig;
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley isc_result_t result;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_buffer_t b;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_region_t r;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_log_t *log = NULL;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_logconfig_t *logconfig;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt keynode_t *keynode;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley dns_result_register();
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt result = isc_mem_create(0, 0, &mctx);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt check_result(result, "isc_mem_create()");
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley while ((ch = isc_commandline_parse(argc, argv, "v:")) != -1)
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt {
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews switch (ch) {
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley case 'v':
77771185071bf74d53378f1a3099a04d2af5153eBrian Wellington endp = NULL;
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley verbose = strtol(isc_commandline_argument, &endp, 0);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt if (*endp != '\0')
2f734e0a7e518c89c2b2b179714b8885b7626b3aAndreas Gustafsson check_result(ISC_R_FAILURE, "strtol()");
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley break;
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley
2f734e0a7e518c89c2b2b179714b8885b7626b3aAndreas Gustafsson default:
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley usage();
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley
bb99a72b219ad4f1daa80c636883ab485174c9a6Mark Andrews }
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt }
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt argc -= isc_commandline_index;
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews argv += isc_commandline_index;
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt if (argc < 2)
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews usage();
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews isc_stdtime_get(&now);
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews if (verbose > 0) {
dc2a0aa7aaa8b85398ae183c7274c0eeec5009afMark Andrews RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig)
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley == ISC_R_SUCCESS);
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley isc_log_setcontext(log);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt dns_log_init(log);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt dns_log_setcontext(log);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt RUNTIME_CHECK(isc_log_usechannel(logconfig, "default_stderr",
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt NULL, NULL) == ISC_R_SUCCESS);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews }
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews if (strlen(argv[0]) < 8 ||
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews strcmp(argv[0] + strlen(argv[0]) - 7, ".keyset") != 0)
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews fatal("keyset file must end in .keyset");
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews dns_fixedname_init(&fdomain);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews domain = dns_fixedname_name(&fdomain);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews isc_buffer_init(&b, argv[0], strlen(argv[0]) - 7);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews isc_buffer_add(&b, strlen(argv[0]) - 7);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews result = dns_name_fromtext(domain, &b, dns_rootname, ISC_FALSE, NULL);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews check_result(result, "dns_name_fromtext()");
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews isc_buffer_init(&b, tdomain, sizeof(tdomain) - 1);
88674be66567d3c7db91e717cd5972655e2e2488Mark Andrews result = dns_name_totext(domain, ISC_FALSE, &b);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt check_result(result, "dns_name_totext()");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_buffer_usedregion(&b, &r);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt tdomain[r.length] = 0;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt output = isc_mem_allocate(mctx,
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt strlen(tdomain) + strlen("signedkey") + 1);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt if (output == NULL)
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt check_result(ISC_R_FAILURE, "isc_mem_allocate()");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt strcpy(output, tdomain);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt strcat(output, "signedkey");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt db = NULL;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt result = dns_db_create(mctx, "rbt", domain, ISC_FALSE,
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt dns_rdataclass_in, 0, NULL, &db);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt check_result(result, "dns_db_create()");
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley result = dns_db_load(db, argv[0]);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt check_result(result, "dns_db_load()");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt version = NULL;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt dns_db_newversion(db, &version);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt node = NULL;
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley result = dns_db_findnode(db, domain, ISC_FALSE, &node);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein check_result(result, "dns_db_findnode()");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_rdataset_init(&rdataset);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_rdataset_init(&sigrdataset);
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley result = dns_db_findrdataset(db, node, version, dns_rdatatype_key, 0,
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt 0, &rdataset, &sigrdataset);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley check_result(result, "dns_db_findrdataset()");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley loadkeys(domain, &rdataset);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt result = dns_rdataset_first(&sigrdataset);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley check_result(result, "dns_rdataset_first()");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley do {
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_rdataset_current(&sigrdataset, &sigrdata);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley result = dns_rdata_tostruct(&sigrdata, &sig, mctx);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt check_result(result, "dns_rdata_tostruct");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley key = findkey(&sig);
f8727bd90366af835f551da1b5e1fdfcd2d3d01fBrian Wellington result = dns_dnssec_verify(domain, &rdataset, key,
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley ISC_TRUE, mctx, &sigrdata);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington check_result(result, "dns_dnssec_verify");
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington dns_rdata_freestruct(&sig);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington result = dns_rdataset_next(&sigrdataset);
51e0ad287f1b345f0c3316f0633aab14d0e8bb65Brian Wellington } while (result == ISC_R_SUCCESS);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt for (keynode = ISC_LIST_HEAD(keylist);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley keynode != NULL;
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley keynode = ISC_LIST_NEXT(keynode, link))
71954c957132c35ddf5f9e4dcc98c057b265b6d8Brian Wellington if (!keynode->verified)
71954c957132c35ddf5f9e4dcc98c057b265b6d8Brian Wellington fatal("Not all zone keys self signed the key set");
71954c957132c35ddf5f9e4dcc98c057b265b6d8Brian Wellington
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt result = dns_rdataset_first(&sigrdataset);
71954c957132c35ddf5f9e4dcc98c057b265b6d8Brian Wellington check_result(result, "dns_rdataset_first()");
d119051ef75d5a88d28c13fb0a7c6d6757a4e9b5Brian Wellington dns_rdataset_current(&sigrdataset, &sigrdata);
d119051ef75d5a88d28c13fb0a7c6d6757a4e9b5Brian Wellington result = dns_rdata_tostruct(&sigrdata, &sig, mctx);
d119051ef75d5a88d28c13fb0a7c6d6757a4e9b5Brian Wellington check_result(result, "dns_rdata_tostruct()");
d119051ef75d5a88d28c13fb0a7c6d6757a4e9b5Brian Wellington
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt dns_rdataset_disassociate(&sigrdataset);
d119051ef75d5a88d28c13fb0a7c6d6757a4e9b5Brian Wellington
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley argc -= 1;
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley argv += 1;
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley
108490a7f8529aff50a0ac7897580b59a73d9845David Lawrence dns_rdatalist_init(&sigrdatalist);
108490a7f8529aff50a0ac7897580b59a73d9845David Lawrence sigrdatalist.rdclass = rdataset.rdclass;
108490a7f8529aff50a0ac7897580b59a73d9845David Lawrence sigrdatalist.type = dns_rdatatype_sig;
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley sigrdatalist.covers = dns_rdatatype_key;
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt sigrdatalist.ttl = rdataset.ttl;
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley for (i = 0; i < argc; i++) {
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley isc_uint16_t id;
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley int alg;
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley char *namestr = NULL;
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_buffer_init(&b, argv[i], strlen(argv[i]));
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt isc_buffer_add(&b, strlen(argv[i]));
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt result = dst_key_parsefilename(&b, mctx, &namestr, &id, &alg,
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt NULL);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt if (result != ISC_R_SUCCESS)
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt usage();
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt key = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = dst_key_fromfile(namestr, id, alg, DST_TYPE_PRIVATE,
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley mctx, &key);
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley check_result (result, "dst_key_fromfile()");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley isc_mem_put(mctx, namestr, strlen(namestr) + 1);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley rdata = isc_mem_get(mctx, sizeof(dns_rdata_t));
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley if (rdata == NULL)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley check_result(ISC_R_NOMEMORY, "isc_mem_get()");
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt data = isc_mem_get(mctx, BUFSIZE);
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley if (data == NULL)
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews check_result(ISC_R_NOMEMORY, "isc_mem_get()");
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews isc_buffer_init(&b, data, BUFSIZE);
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews result = dns_dnssec_sign(domain, &rdataset, key,
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews &sig.timesigned, &sig.timeexpire,
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews mctx, &b, rdata);
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews check_result (result, "dns_dnssec_sign()");
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews ISC_LIST_APPEND(sigrdatalist.rdata, rdata, link);
0415ca35ada2cac6a86127eaca64f3a997aea121Evan Hunt dst_key_free(key);
8fd925169f3d690f6c50c17d711adc9695407528Mark Andrews }
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley dns_rdataset_init(&newsigrdataset);
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley result = dns_rdatalist_tordataset(&sigrdatalist, &newsigrdataset);
f8727bd90366af835f551da1b5e1fdfcd2d3d01fBrian Wellington check_result (result, "dns_rdatalist_tordataset()");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_db_addrdataset(db, node, version, 0, &newsigrdataset, 0, NULL);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews check_result (result, "dns_db_addrdataset()");
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_db_detachnode(db, &node);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley dns_db_closeversion(db, &version, ISC_TRUE);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley result = dns_db_dump(db, version, output);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley check_result(result, "dns_db_dump()");
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews dns_rdataset_disassociate(&rdataset);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews dns_rdataset_disassociate(&newsigrdataset);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews dns_rdata_freestruct(&sig);
77771185071bf74d53378f1a3099a04d2af5153eBrian Wellington
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley while (!ISC_LIST_EMPTY(sigrdatalist.rdata)) {
77771185071bf74d53378f1a3099a04d2af5153eBrian Wellington rdata = ISC_LIST_HEAD(sigrdatalist.rdata);
77771185071bf74d53378f1a3099a04d2af5153eBrian Wellington ISC_LIST_UNLINK(sigrdatalist.rdata, rdata, link);
77771185071bf74d53378f1a3099a04d2af5153eBrian Wellington isc_mem_put(mctx, rdata->data, BUFSIZE);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley isc_mem_put(mctx, rdata, sizeof *rdata);
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley }
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley dns_db_detach(&db);
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley while (!ISC_LIST_EMPTY(keylist)) {
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley keynode = ISC_LIST_HEAD(keylist);
663841abe0bb1cc8040e552597ef721c35b799e5Brian Wellington ISC_LIST_UNLINK(keylist, keynode, link);
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley dst_key_free(keynode->key);
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley isc_mem_put(mctx, keynode, sizeof(keynode_t));
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley }
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley if (log != NULL)
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley isc_log_destroy(&log);
ee80f4506479e189ca1320eb87ac89188c5a7848Mark Andrews
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley isc_mem_free(mctx, output);
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley isc_mem_destroy(&mctx);
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley return (0);
663841abe0bb1cc8040e552597ef721c35b799e5Brian Wellington}
663841abe0bb1cc8040e552597ef721c35b799e5Brian Wellington