tkey.c revision 352e28b5eedc250149352990c3a8f14d5af0267f
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson/*
19c7b1a0293498a3e36692c59646ed6e15ffc8d0Tinderbox User * Copyright (C) 1999, 2000 Internet Software Consortium.
c651f15b30f1dae5cc2f00878fb5da5b3a35a468Mark Andrews *
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson * 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
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson *
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson */
2eeb74d1cf5355dd98f6d507a10086e16bb08c4bTinderbox User
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt/*
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * $Id: tkey.c,v 1.51 2000/09/20 00:01:26 bwelling Exp $
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt */
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt#include <config.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt#include <isc/buffer.h>
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt#include <isc/entropy.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/md5.h>
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson#include <isc/mem.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/string.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/util.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/dnssec.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/keyvalues.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/log.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/message.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/name.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/rdata.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/rdatalist.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/rdataset.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/rdatastruct.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/result.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <dns/tkey.h>
c1a883f2e04d94e99c433b1f6cfd0c0338f4ed85Mark Andrews#include <dns/tsig.h>
938440694b33cd752e9e4b71a526368b4811c177Tinderbox User
19c7b1a0293498a3e36692c59646ed6e15ffc8d0Tinderbox User#define TKEY_RANDOM_AMOUNT 16
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define RETERR(x) do { \
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein result = (x); \
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (result != ISC_R_SUCCESS) \
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein goto failure; \
f5d30e2864e048a42c4dc1134993ae7efdb5d6c3Mark Andrews } while (0)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define TKEYTRACE(m) isc_log_write(dns_lctx, \
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein DNS_LOGCATEGORY_RESOLVER, \
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein DNS_LOGMODULE_RESOLVER, \
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ISC_LOG_DEBUG(3), \
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson "tkey: %s", (m))
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic void
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeintkey_log(const char *fmt, ...) {
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt va_list ap;
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein va_start(ap, fmt);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein DNS_LOGMODULE_REQUEST, ISC_LOG_DEBUG(4), fmt, ap);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein va_end(ap);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
821350367e2c7313c02eb275e8e05d5193b47cfdJeremy C. Reedisc_result_t
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeindns_tkeyctx_create(isc_mem_t *mctx, isc_entropy_t *ectx, dns_tkeyctx_t **tctxp)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein{
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_tkeyctx_t *tctx;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein REQUIRE(mctx != NULL);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt REQUIRE(ectx != NULL);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein REQUIRE(tctxp != NULL && *tctxp == NULL);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews tctx = isc_mem_get(mctx, sizeof(dns_tkeyctx_t));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (tctx == NULL)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_NOMEMORY);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein tctx->mctx = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_mem_attach(mctx, &tctx->mctx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein tctx->ectx = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_entropy_attach(ectx, &tctx->ectx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein tctx->dhkey = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein tctx->domain = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein *tctxp = tctx;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_SUCCESS);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinvoid
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeindns_tkeyctx_destroy(dns_tkeyctx_t **tctxp) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_mem_t *mctx;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_tkeyctx_t *tctx;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein REQUIRE(tctxp != NULL && *tctxp != NULL);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein tctx = *tctxp;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein mctx = tctx->mctx;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (tctx->dhkey != NULL)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dst_key_free(&tctx->dhkey);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (tctx->domain != NULL) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_name_free(tctx->domain, mctx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_mem_put(mctx, tctx->domain, sizeof(dns_name_t));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein }
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_entropy_detach(&tctx->ectx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_mem_put(mctx, tctx, sizeof(dns_tkeyctx_t));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_mem_detach(&mctx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein *tctxp = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic isc_result_t
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinadd_rdata_to_list(dns_message_t *msg, dns_name_t *name, dns_rdata_t *rdata,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_uint32_t ttl, dns_namelist_t *namelist)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein{
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_result_t result;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_region_t r, newr;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdata_t *newrdata = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_name_t *newname = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdatalist_t *newlist = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdataset_t *newset = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_t *tmprdatabuf = NULL, *tmpnamebuf = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(dns_message_gettemprdata(msg, &newrdata));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdata_toregion(rdata, &r);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(isc_buffer_allocate(msg->mctx, &tmprdatabuf, r.length));
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt isc_buffer_availableregion(tmprdatabuf, &newr);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt memcpy(newr.base, r.base, r.length);
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews dns_rdata_fromregion(newrdata, rdata->rdclass, rdata->type, &newr);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_takebuffer(msg, &tmprdatabuf);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_name_toregion(name, &r);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(dns_message_gettempname(msg, &newname));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_name_init(newname, NULL);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(isc_buffer_allocate(msg->mctx, &tmpnamebuf, r.length));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_availableregion(tmpnamebuf, &newr);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein memcpy(newr.base, r.base, r.length);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_name_fromregion(newname, &newr);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_takebuffer(msg, &tmpnamebuf);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(dns_message_gettemprdatalist(msg, &newlist));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein newlist->rdclass = newrdata->rdclass;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein newlist->type = newrdata->type;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein newlist->covers = 0;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein newlist->ttl = ttl;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ISC_LIST_INIT(newlist->rdata);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt ISC_LIST_APPEND(newlist->rdata, newrdata, link);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(dns_message_gettemprdataset(msg, &newset));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdataset_init(newset);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RETERR(dns_rdatalist_tordataset(newlist, newset));
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ISC_LIST_INIT(newname->list);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ISC_LIST_APPEND(newname->list, newset, link);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt ISC_LIST_APPEND(*namelist, newname, link);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_SUCCESS);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein failure:
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (newrdata != NULL)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_puttemprdata(msg, &newrdata);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (newname != NULL)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_puttempname(msg, &newname);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (newlist != NULL)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_puttemprdatalist(msg, &newlist);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (newset != NULL) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdataset_disassociate(newset);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_puttemprdataset(msg, &newset);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein }
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (result);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic isc_result_t
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Huntcompute_secret(isc_buffer_t *shared, isc_region_t *queryrandomness,
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt isc_region_t *serverrandomness, isc_buffer_t *secret)
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews{
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_t md5ctx;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_region_t r, r2;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned char digests[32];
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int i;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_usedregion(shared, &r);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /*
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * MD5 ( query data | DH value ).
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_init(&md5ctx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_update(&md5ctx, queryrandomness->base, queryrandomness->length);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_update(&md5ctx, r.base, r.length);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_final(&md5ctx, digests);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /*
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * MD5 ( server data | DH value ).
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_init(&md5ctx);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_update(&md5ctx, serverrandomness->base,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein serverrandomness->length);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_md5_update(&md5ctx, r.base, r.length);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt isc_md5_final(&md5ctx, &digests[ISC_MD5_DIGESTLENGTH]);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /*
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * XOR ( DH value, MD5-1 | MD5-2).
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein */
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt isc_buffer_availableregion(secret, &r);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_usedregion(shared, &r2);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (r.length < sizeof(digests) || r.length < r2.length)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_NOSPACE);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (r2.length > sizeof(digests)) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein memcpy(r.base, r2.base, r2.length);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein for (i = 0; i < sizeof(digests); i++)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein r.base[i] ^= digests[i];
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_add(secret, r2.length);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein }
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein else {
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt memcpy(r.base, digests, sizeof(digests));
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt for (i = 0; i < r2.length; i++)
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt r.base[i] ^= r2.base[i];
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews isc_buffer_add(secret, sizeof(digests));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein }
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_SUCCESS);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic isc_result_t
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinprocess_dhtkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdata_tkey_t *tkeyin, dns_tkeyctx_t *tctx,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdata_tkey_t *tkeyout,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_tsig_keyring_t *ring, dns_namelist_t *namelist)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein{
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_result_t result = ISC_R_SUCCESS;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_name_t *keyname, ourname;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdataset_t *keyset;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdata_t keyrdata, ourkeyrdata;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_boolean_t found_key = ISC_FALSE, found_incompatible = ISC_FALSE;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dst_key_t *pubkey = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_t ourkeybuf, *shared = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_region_t r, r2, ourkeyr;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_uint32_t ourttl;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned char keydata[DST_KEY_MAXSIZE];
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int sharedsize;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_buffer_t secret;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned char *randomdata = NULL, secretdata[256];
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein isc_stdtime_t now;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /*
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * Look for a DH KEY record that will work with ours.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein result = dns_message_firstname(msg, DNS_SECTION_ADDITIONAL);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein while (result == ISC_R_SUCCESS) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein keyname = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_message_currentname(msg, DNS_SECTION_ADDITIONAL, &keyname);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein keyset = NULL;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein result = dns_message_findtype(keyname, dns_rdatatype_key, 0,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein &keyset);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (result == ISC_R_SUCCESS) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein result = dns_rdataset_first(keyset);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein while (result == ISC_R_SUCCESS) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein dns_rdataset_current(keyset, &keyrdata);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein pubkey = NULL;
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt result = dns_dnssec_keyfromrdata(keyname,
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt &keyrdata,
msg->mctx,
&pubkey);
if (result != ISC_R_SUCCESS) {
result = dns_rdataset_next(keyset);
continue;
}
if (dst_key_alg(pubkey) == DNS_KEYALG_DH) {
if (dst_key_paramcompare(pubkey,
tctx->dhkey))
{
found_key = ISC_TRUE;
goto got_key;
}
else
found_incompatible = ISC_TRUE;
}
dst_key_free(&pubkey);
result = dns_rdataset_next(keyset);
}
}
result = dns_message_nextname(msg, DNS_SECTION_ADDITIONAL);
}
got_key:
if (!found_key) {
if (found_incompatible) {
tkey_log("process_dhtkey: found an incompatible key");
tkeyout->error = dns_tsigerror_badkey;
return (ISC_R_SUCCESS);
} else
tkey_log("process_dhtkey: failed to find a key");
return (DNS_R_FORMERR);
}
RETERR(add_rdata_to_list(msg, keyname, &keyrdata, keyset->ttl,
namelist));
isc_buffer_init(&ourkeybuf, keydata, sizeof(keydata));
RETERR(dst_key_todns(tctx->dhkey, &ourkeybuf));
isc_buffer_usedregion(&ourkeybuf, &ourkeyr);
dns_rdata_fromregion(&ourkeyrdata, dns_rdataclass_any,
dns_rdatatype_key, &ourkeyr);
dns_name_init(&ourname, NULL);
dns_name_clone(dst_key_name(tctx->dhkey), &ourname);
ourttl = 0;
#if 0
/*
* Not sure how to do this without a view...
*/
db = NULL;
result = dns_dbtable_find(client->view->dbtable, &ourname, 0, &db);
if (result == ISC_R_SUCCESS) {
dns_rdataset_t set;
dns_fixedname_t foundname;
dns_rdataset_init(&set);
dns_fixedname_init(&foundname);
result = dns_db_find(db, &ourname, NULL, dns_rdatatype_key,
DNS_DBFIND_NOWILD, 0, NULL,
dns_fixedname_name(&foundname),
&set, NULL);
if (result == ISC_R_SUCCESS) {
ourttl = set.ttl;
dns_rdataset_disassociate(&set);
}
}
#endif
RETERR(add_rdata_to_list(msg, &ourname, &ourkeyrdata, ourttl,
namelist));
RETERR(dst_key_secretsize(tctx->dhkey, &sharedsize));
RETERR(isc_buffer_allocate(msg->mctx, &shared, sharedsize));
result = dst_key_computesecret(pubkey, tctx->dhkey, shared);
if (result != ISC_R_SUCCESS) {
tkey_log("process_dhtkey: failed to compute shared secret: %s",
isc_result_totext(result));
goto failure;
}
isc_buffer_init(&secret, secretdata, sizeof(secretdata));
randomdata = isc_mem_get(tkeyout->mctx, TKEY_RANDOM_AMOUNT);
if (randomdata == NULL) {
result = ISC_R_NOMEMORY;
goto failure;
}
result = isc_entropy_getdata(tctx->ectx, randomdata,
TKEY_RANDOM_AMOUNT, NULL, 0);
if (result != ISC_R_SUCCESS) {
tkey_log("process_dhtkey: failed to obtain entropy: %s",
isc_result_totext(result));
goto failure;
}
r.base = randomdata;
r.length = TKEY_RANDOM_AMOUNT;
r2.base = tkeyin->key;
r2.length = tkeyin->keylen;
RETERR(compute_secret(shared, &r2, &r, &secret));
dst_key_free(&pubkey);
isc_buffer_usedregion(&secret, &r);
result = dns_tsigkey_create(name, &tkeyin->algorithm, r.base, r.length,
ISC_TRUE, signer, tkeyin->inception,
tkeyin->expire, msg->mctx, ring, NULL);
isc_buffer_free(&shared);
shared = NULL;
if (result == ISC_R_NOTFOUND) {
tkeyout->error = dns_tsigerror_badalg;
return (ISC_R_SUCCESS);
}
if (result != ISC_R_SUCCESS)
goto failure;
/* This key is good for a long time */
isc_stdtime_get(&now);
tkeyout->inception = tkeyin->inception;
tkeyout->expire = tkeyin->expire;
tkeyout->key = randomdata;
tkeyout->keylen = TKEY_RANDOM_AMOUNT;
return (ISC_R_SUCCESS);
failure:
if (!ISC_LIST_EMPTY(*namelist)) {
dns_name_t *tname = ISC_LIST_HEAD(*namelist);
while (tname != NULL) {
dns_name_t *next = ISC_LIST_NEXT(tname, link);
dns_rdataset_t *tset;
ISC_LIST_UNLINK(*namelist, tname, link);
tset = ISC_LIST_HEAD(tname->list);
dns_rdataset_disassociate(tset);
dns_message_puttemprdataset(msg, &tset);
dns_message_puttempname(msg, &tname);
tname = next;
}
}
if (shared != NULL)
isc_buffer_free(&shared);
return (result);
}
static isc_result_t
process_deletetkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
dns_rdata_tkey_t *tkeyin,
dns_rdata_tkey_t *tkeyout,
dns_tsig_keyring_t *ring,
dns_namelist_t *namelist)
{
isc_result_t result;
dns_tsigkey_t *tsigkey = NULL;
dns_name_t *identity;
UNUSED(msg);
UNUSED(tkeyout);
UNUSED(namelist);
result = dns_tsigkey_find(&tsigkey, name, &tkeyin->algorithm, ring);
if (result != ISC_R_SUCCESS)
tkeyout->error = dns_tsigerror_badname;
/*
* Only allow a delete if the identity that created the key is the
* same as the identity that signed the message.
*/
identity = dns_tsigkey_identity(tsigkey);
if (identity == NULL || !dns_name_equal(identity, signer))
return (DNS_R_REFUSED);
/*
* Set the key to be deleted when no references are left. If the key
* was not generated with TKEY and is in the config file, it may be
* reloaded later.
*/
dns_tsigkey_setdeleted(tsigkey);
/* Release the reference */
dns_tsigkey_detach(&tsigkey);
return (ISC_R_SUCCESS);
}
isc_result_t
dns_tkey_processquery(dns_message_t *msg, dns_tkeyctx_t *tctx,
dns_tsig_keyring_t *ring)
{
isc_result_t result = ISC_R_SUCCESS;
dns_rdata_tkey_t tkeyin, tkeyout;
dns_name_t *qname, *name, *keyname, tempkeyname, signer;
dns_rdataset_t *tkeyset;
dns_rdata_t tkeyrdata, *rdata = NULL;
isc_buffer_t *dynbuf = NULL;
dns_namelist_t namelist;
isc_boolean_t freealg = ISC_FALSE;
REQUIRE(msg != NULL);
REQUIRE(tctx != NULL);
REQUIRE(ring != NULL);
/*
* Need to do this to determine if this should be freed later.
*/
memset(&tkeyin, 0, sizeof(dns_rdata_tkey_t));
/*
* Interpret the question section.
*/
result = dns_message_firstname(msg, DNS_SECTION_QUESTION);
INSIST(result == ISC_R_SUCCESS);
qname = NULL;
dns_message_currentname(msg, DNS_SECTION_QUESTION, &qname);
/*
* Look for a TKEY record that matches the question.
*/
tkeyset = NULL;
name = NULL;
result = dns_message_findname(msg, DNS_SECTION_ADDITIONAL, qname,
dns_rdatatype_tkey, 0, &name, &tkeyset);
if (result != ISC_R_SUCCESS) {
result = DNS_R_FORMERR;
tkey_log("dns_tkey_processquery: couldn't find a TKEY "
"matching the question");
goto failure;
}
result = dns_rdataset_first(tkeyset);
if (result != ISC_R_SUCCESS) {
result = DNS_R_FORMERR;
goto failure;
}
dns_rdataset_current(tkeyset, &tkeyrdata);
RETERR(dns_rdata_tostruct(&tkeyrdata, &tkeyin, msg->mctx));
if (tkeyin.error != dns_rcode_noerror) {
result = DNS_R_FORMERR;
goto failure;
}
/*
* Before we go any farther, verify that the message was signed.
* GSSAPI TKEY doesn't require a signature, the rest do.
*/
dns_name_init(&signer, NULL);
result = dns_message_signer(msg, &signer);
if (result != ISC_R_SUCCESS &&
(tkeyin.mode != DNS_TKEYMODE_GSSAPI || result != ISC_R_NOTFOUND))
{
tkey_log("dns_tkey_processquery: query was not properly "
"signed - rejecting");
result = DNS_R_FORMERR;
goto failure;
}
ISC_LIST_INIT(namelist);
tkeyout.common.rdclass = tkeyin.common.rdclass;
tkeyout.common.rdtype = tkeyin.common.rdtype;
ISC_LINK_INIT(&tkeyout.common, link);
tkeyout.mctx = msg->mctx;
dns_name_init(&tkeyout.algorithm, NULL);
RETERR(dns_name_dup(&tkeyin.algorithm, msg->mctx, &tkeyout.algorithm));
freealg = ISC_TRUE;
tkeyout.inception = tkeyout.expire = 0;
tkeyout.mode = tkeyin.mode;
tkeyout.error = 0;
tkeyout.keylen = tkeyout.otherlen = 0;
tkeyout.key = tkeyout.other = NULL;
/*
* A delete operation must have a fully specified key name. If this
* is not a delete, we do the following:
* if (qname != ".")
* keyname = qname + defaultdomain
* else
* keyname = <random hex> + defaultdomain
*/
if (tkeyin.mode != DNS_TKEYMODE_DELETE) {
dns_name_t prefix;
isc_buffer_t *buf = NULL;
unsigned char tdata[64];
dns_tsigkey_t *tsigkey = NULL;
dns_name_init(&tempkeyname, NULL);
keyname = &tempkeyname;
dns_name_init(&prefix, NULL);
RETERR(isc_buffer_allocate(msg->mctx, &buf, 256));
if (!dns_name_equal(qname, dns_rootname)) {
unsigned int n = dns_name_countlabels(qname);
dns_name_getlabelsequence(qname, 0, n - 1, &prefix);
}
else {
static char hexdigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
unsigned char randomtext[32];
isc_buffer_t b, b2;
int i;
result = isc_entropy_getdata(tctx->ectx,
randomtext,
sizeof(randomtext),
NULL, 0);
if (result != ISC_R_SUCCESS) {
dns_message_takebuffer(msg, &buf);
goto failure;
}
for (i = sizeof(randomtext) - 2; i >= 0; i -= 2) {
unsigned char val = randomtext[i/2];
randomtext[i] = hexdigits[val >> 4];
randomtext[i+1] = hexdigits[val & 0xF];
}
isc_buffer_init(&b, randomtext, sizeof(randomtext));
isc_buffer_init(&b2, tdata, sizeof(tdata));
isc_buffer_add(&b, sizeof(randomtext));
result = dns_name_fromtext(&prefix, &b, NULL,
ISC_FALSE, &b2);
if (result != ISC_R_SUCCESS) {
dns_message_takebuffer(msg, &buf);
goto failure;
}
}
result = dns_name_concatenate(&prefix, tctx->domain,
keyname, buf);
dns_message_takebuffer(msg, &buf);
if (result != ISC_R_SUCCESS)
goto failure;
result = dns_tsigkey_find(&tsigkey, keyname, NULL, ring);
if (result == ISC_R_SUCCESS) {
tkeyout.error = dns_tsigerror_badname;
dns_tsigkey_detach(&tsigkey);
goto failure_with_tkey;
}
else if (result != ISC_R_NOTFOUND)
goto failure;
}
else
keyname = qname;
if (!dns_name_equal(&tkeyin.algorithm, DNS_TSIG_HMACMD5_NAME)) {
tkey_log("dns_tkey_processquery: tkey modes other than "
"hmac-md5 are not supported");
tkeyout.error = dns_tsigerror_badkey;
goto failure_with_tkey;
}
switch (tkeyin.mode) {
case DNS_TKEYMODE_DIFFIEHELLMAN:
RETERR(process_dhtkey(msg, &signer, keyname, &tkeyin,
tctx, &tkeyout, ring,
&namelist));
tkeyout.error = dns_rcode_noerror;
break;
case DNS_TKEYMODE_DELETE:
RETERR(process_deletetkey(msg, &signer, keyname,
&tkeyin, &tkeyout,
ring, &namelist));
tkeyout.error = dns_rcode_noerror;
break;
case DNS_TKEYMODE_SERVERASSIGNED:
case DNS_TKEYMODE_GSSAPI:
case DNS_TKEYMODE_RESOLVERASSIGNED:
result = DNS_R_NOTIMP;
goto failure;
default:
tkeyout.error = dns_tsigerror_badmode;
}
failure_with_tkey:
dns_rdata_freestruct(&tkeyin);
RETERR(dns_message_gettemprdata(msg, &rdata));
RETERR(isc_buffer_allocate(msg->mctx, &dynbuf, 128));
result = dns_rdata_fromstruct(rdata, tkeyout.common.rdclass,
tkeyout.common.rdtype, &tkeyout, dynbuf);
dns_name_free(&tkeyout.algorithm, msg->mctx);
if (tkeyout.key != NULL)
isc_mem_put(msg->mctx, tkeyout.key, tkeyout.keylen);
if (tkeyout.other != NULL)
isc_mem_put(msg->mctx, tkeyout.other, tkeyout.otherlen);
if (result != ISC_R_SUCCESS)
goto failure;
RETERR(add_rdata_to_list(msg, keyname, rdata, 0, &namelist));
isc_buffer_free(&dynbuf);
RETERR(dns_message_reply(msg, ISC_TRUE));
name = ISC_LIST_HEAD(namelist);
while (name != NULL) {
dns_name_t *next = ISC_LIST_NEXT(name, link);
ISC_LIST_UNLINK(namelist, name, link);
dns_message_addname(msg, name, DNS_SECTION_ANSWER);
name = next;
}
return (ISC_R_SUCCESS);
failure:
if (freealg)
dns_name_free(&tkeyout.algorithm, msg->mctx);
if (tkeyin.common.rdtype == dns_rdatatype_tkey)
dns_rdata_freestruct(&tkeyin);
if (rdata != NULL)
dns_message_puttemprdata(msg, &rdata);
if (dynbuf != NULL)
isc_buffer_free(&dynbuf);
return (result);
}
static isc_result_t
buildquery(dns_message_t *msg, dns_name_t *name,
dns_rdata_tkey_t *tkey)
{
dns_name_t *qname = NULL, *aname = NULL;
dns_rdataset_t *question = NULL, *tkeyset = NULL;
dns_rdatalist_t *tkeylist = NULL;
dns_rdata_t *rdata = NULL;
isc_buffer_t *dynbuf = NULL;
isc_result_t result;
REQUIRE(msg != NULL);
REQUIRE(name != NULL);
REQUIRE(tkey != NULL);
RETERR(dns_message_gettempname(msg, &qname));
RETERR(dns_message_gettempname(msg, &aname));
RETERR(dns_message_gettemprdataset(msg, &question));
dns_rdataset_init(question);
dns_rdataset_makequestion(question, dns_rdataclass_any,
dns_rdatatype_tkey);
RETERR(isc_buffer_allocate(msg->mctx, &dynbuf, 512));
RETERR(dns_message_gettemprdata(msg, &rdata));
RETERR(dns_rdata_fromstruct(rdata, dns_rdataclass_any,
dns_rdatatype_tkey, tkey, dynbuf));
dns_message_takebuffer(msg, &dynbuf);
RETERR(dns_message_gettemprdatalist(msg, &tkeylist));
tkeylist->rdclass = dns_rdataclass_any;
tkeylist->type = dns_rdatatype_tkey;
tkeylist->covers = 0;
tkeylist->ttl = 0;
ISC_LIST_INIT(tkeylist->rdata);
ISC_LIST_APPEND(tkeylist->rdata, rdata, link);
RETERR(dns_message_gettemprdataset(msg, &tkeyset));
dns_rdataset_init(tkeyset);
RETERR(dns_rdatalist_tordataset(tkeylist, tkeyset));
dns_name_init(qname, NULL);
dns_name_clone(name, qname);
dns_name_init(aname, NULL);
dns_name_clone(name, aname);
ISC_LIST_APPEND(qname->list, question, link);
ISC_LIST_APPEND(aname->list, tkeyset, link);
dns_message_addname(msg, qname, DNS_SECTION_QUESTION);
dns_message_addname(msg, aname, DNS_SECTION_ADDITIONAL);
return (ISC_R_SUCCESS);
failure:
if (qname != NULL)
dns_message_puttempname(msg, &qname);
if (aname != NULL)
dns_message_puttempname(msg, &aname);
if (question != NULL) {
dns_rdataset_disassociate(question);
dns_message_puttemprdataset(msg, &question);
}
if (dynbuf != NULL)
isc_buffer_free(&dynbuf);
return (result);
}
isc_result_t
dns_tkey_builddhquery(dns_message_t *msg, dst_key_t *key, dns_name_t *name,
dns_name_t *algorithm, isc_buffer_t *nonce,
isc_uint32_t lifetime)
{
dns_rdata_tkey_t tkey;
dns_rdata_t *rdata = NULL;
isc_buffer_t *dynbuf = NULL;
isc_region_t r;
dns_name_t keyname;
dns_namelist_t namelist;
isc_result_t result;
isc_stdtime_t now;
REQUIRE(msg != NULL);
REQUIRE(key != NULL);
REQUIRE(dst_key_alg(key) == DNS_KEYALG_DH);
REQUIRE(dst_key_isprivate(key));
REQUIRE(name != NULL);
REQUIRE(algorithm != NULL);
tkey.common.rdclass = dns_rdataclass_any;
tkey.common.rdtype = dns_rdatatype_tkey;
ISC_LINK_INIT(&tkey.common, link);
tkey.mctx = msg->mctx;
dns_name_init(&tkey.algorithm, NULL);
dns_name_clone(algorithm, &tkey.algorithm);
isc_stdtime_get(&now);
tkey.inception = now;
tkey.expire = now + lifetime;
tkey.mode = DNS_TKEYMODE_DIFFIEHELLMAN;
if (nonce != NULL)
isc_buffer_region(nonce, &r);
else {
r.base = isc_mem_get(msg->mctx, 0);
r.length = 0;
}
tkey.error = 0;
tkey.key = r.base;
tkey.keylen = r.length;
tkey.other = NULL;
tkey.otherlen = 0;
RETERR(buildquery(msg, name, &tkey));
if (nonce == NULL)
isc_mem_put(msg->mctx, r.base, 0);
RETERR(dns_message_gettemprdata(msg, &rdata));
RETERR(isc_buffer_allocate(msg->mctx, &dynbuf, 1024));
RETERR(dst_key_todns(key, dynbuf));
isc_buffer_usedregion(dynbuf, &r);
dns_rdata_fromregion(rdata, dns_rdataclass_any,
dns_rdatatype_key, &r);
dns_message_takebuffer(msg, &dynbuf);
dns_name_init(&keyname, NULL);
dns_name_clone(dst_key_name(key), &keyname);
ISC_LIST_INIT(namelist);
RETERR(add_rdata_to_list(msg, &keyname, rdata, 0, &namelist));
dns_message_addname(msg, ISC_LIST_HEAD(namelist),
DNS_SECTION_ADDITIONAL);
return (ISC_R_SUCCESS);
failure:
if (dynbuf != NULL)
isc_buffer_free(&dynbuf);
return (result);
}
isc_result_t
dns_tkey_builddeletequery(dns_message_t *msg, dns_tsigkey_t *key) {
dns_rdata_tkey_t tkey;
REQUIRE(msg != NULL);
REQUIRE(key != NULL);
tkey.common.rdclass = dns_rdataclass_any;
tkey.common.rdtype = dns_rdatatype_tkey;
ISC_LINK_INIT(&tkey.common, link);
tkey.mctx = msg->mctx;
dns_name_init(&tkey.algorithm, NULL);
dns_name_clone(key->algorithm, &tkey.algorithm);
tkey.inception = tkey.expire = 0;
tkey.mode = DNS_TKEYMODE_DELETE;
tkey.error = 0;
tkey.keylen = tkey.otherlen = 0;
tkey.key = tkey.other = NULL;
return (buildquery(msg, &key->name, &tkey));
}
static isc_result_t
find_tkey(dns_message_t *msg, dns_name_t **name, dns_rdata_t *rdata,
int section)
{
dns_rdataset_t *tkeyset;
isc_result_t result;
result = dns_message_firstname(msg, section);
while (result == ISC_R_SUCCESS) {
*name = NULL;
dns_message_currentname(msg, section, name);
tkeyset = NULL;
result = dns_message_findtype(*name, dns_rdatatype_tkey, 0,
&tkeyset);
if (result == ISC_R_SUCCESS) {
result = dns_rdataset_first(tkeyset);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdataset_current(tkeyset, rdata);
return (ISC_R_SUCCESS);
}
result = dns_message_nextname(msg, section);
}
if (result == ISC_R_NOMORE)
return (ISC_R_NOTFOUND);
return (result);
}
isc_result_t
dns_tkey_processdhresponse(dns_message_t *qmsg, dns_message_t *rmsg,
dst_key_t *key, isc_buffer_t *nonce,
dns_tsigkey_t **outkey, dns_tsig_keyring_t *ring)
{
dns_rdata_t qtkeyrdata, rtkeyrdata;
dns_name_t keyname, *tkeyname, *theirkeyname, *ourkeyname, *tempname;
dns_rdataset_t *theirkeyset = NULL, *ourkeyset = NULL;
dns_rdata_t theirkeyrdata;
dst_key_t *theirkey;
dns_rdata_tkey_t qtkey, rtkey;
unsigned char secretdata[256];
unsigned int sharedsize;
isc_buffer_t *shared = NULL, secret;
isc_region_t r, r2;
isc_result_t result;
isc_boolean_t freertkey = ISC_FALSE;
REQUIRE(qmsg != NULL);
REQUIRE(rmsg != NULL);
REQUIRE(key != NULL);
REQUIRE(dst_key_alg(key) == DNS_KEYALG_DH);
REQUIRE(dst_key_isprivate(key));
if (outkey != NULL)
REQUIRE(*outkey == NULL);
REQUIRE(ring != NULL);
if (rmsg->rcode != dns_rcode_noerror)
return (ISC_RESULTCLASS_DNSRCODE + rmsg->rcode);
RETERR(find_tkey(rmsg, &tkeyname, &rtkeyrdata, DNS_SECTION_ANSWER));
RETERR(dns_rdata_tostruct(&rtkeyrdata, &rtkey, rmsg->mctx));
freertkey = ISC_TRUE;
RETERR(find_tkey(qmsg, &tempname, &qtkeyrdata,
DNS_SECTION_ADDITIONAL));
RETERR(dns_rdata_tostruct(&qtkeyrdata, &qtkey, qmsg->mctx));
if (rtkey.error != dns_rcode_noerror ||
rtkey.mode != DNS_TKEYMODE_DIFFIEHELLMAN ||
rtkey.mode != qtkey.mode ||
!dns_name_equal(&rtkey.algorithm, &qtkey.algorithm) ||
rmsg->rcode != dns_rcode_noerror)
{
tkey_log("dns_tkey_processdhresponse: tkey mode invalid "
"or error set");
result = DNS_R_INVALIDTKEY;
dns_rdata_freestruct(&qtkey);
goto failure;
}
dns_rdata_freestruct(&qtkey);
dns_name_init(&keyname, NULL);
dns_name_clone(dst_key_name(key), &keyname);
ourkeyname = NULL;
ourkeyset = NULL;
RETERR(dns_message_findname(rmsg, DNS_SECTION_ANSWER, &keyname,
dns_rdatatype_key, 0, &ourkeyname,
&ourkeyset));
result = dns_message_firstname(rmsg, DNS_SECTION_ANSWER);
while (result == ISC_R_SUCCESS) {
theirkeyname = NULL;
dns_message_currentname(rmsg, DNS_SECTION_ANSWER,
&theirkeyname);
if (dns_name_equal(theirkeyname, ourkeyname))
goto next;
theirkeyset = NULL;
result = dns_message_findtype(theirkeyname, dns_rdatatype_key,
0, &theirkeyset);
if (result == ISC_R_SUCCESS) {
RETERR(dns_rdataset_first(theirkeyset));
break;
}
next:
result = dns_message_nextname(rmsg, DNS_SECTION_ANSWER);
}
if (theirkeyset == NULL) {
tkey_log("dns_tkey_processdhresponse: failed to find server "
"key");
result = DNS_R_INVALIDTKEY;
result = ISC_R_NOTFOUND;
goto failure;
}
dns_rdataset_current(theirkeyset, &theirkeyrdata);
theirkey = NULL;
RETERR(dns_dnssec_keyfromrdata(theirkeyname, &theirkeyrdata,
rmsg->mctx, &theirkey));
RETERR(dst_key_secretsize(key, &sharedsize));
RETERR(isc_buffer_allocate(rmsg->mctx, &shared, sharedsize));
RETERR(dst_key_computesecret(theirkey, key, shared));
isc_buffer_init(&secret, secretdata, sizeof(secretdata));
r.base = rtkey.key;
r.length = rtkey.keylen;
if (nonce != NULL)
isc_buffer_region(nonce, &r2);
else {
r2.base = isc_mem_get(rmsg->mctx, 0);
r2.length = 0;
}
RETERR(compute_secret(shared, &r2, &r, &secret));
if (nonce == NULL)
isc_mem_put(rmsg->mctx, r2.base, 0);
isc_buffer_usedregion(&secret, &r);
result = dns_tsigkey_create(tkeyname, &rtkey.algorithm,
r.base, r.length, ISC_TRUE,
NULL, rtkey.inception, rtkey.expire,
rmsg->mctx, ring, outkey);
isc_buffer_free(&shared);
dns_rdata_freestruct(&rtkey);
dst_key_free(&theirkey);
return (result);
failure:
if (shared != NULL)
isc_buffer_free(&shared);
if (theirkey != NULL)
dst_key_free(&theirkey);
if (freertkey)
dns_rdata_freestruct(&rtkey);
return (result);
}
isc_result_t
dns_tkey_processdeleteresponse(dns_message_t *qmsg, dns_message_t *rmsg,
dns_tsig_keyring_t *ring)
{
dns_rdata_t qtkeyrdata, rtkeyrdata;
dns_name_t *tkeyname, *tempname;
dns_rdata_tkey_t qtkey, rtkey;
dns_tsigkey_t *tsigkey = NULL;
isc_result_t result;
REQUIRE(qmsg != NULL);
REQUIRE(rmsg != NULL);
if (rmsg->rcode != dns_rcode_noerror)
return(ISC_RESULTCLASS_DNSRCODE + rmsg->rcode);
RETERR(find_tkey(rmsg, &tkeyname, &rtkeyrdata, DNS_SECTION_ANSWER));
RETERR(dns_rdata_tostruct(&rtkeyrdata, &rtkey, rmsg->mctx));
RETERR(find_tkey(qmsg, &tempname, &qtkeyrdata,
DNS_SECTION_ADDITIONAL));
RETERR(dns_rdata_tostruct(&qtkeyrdata, &qtkey, qmsg->mctx));
if (rtkey.error != dns_rcode_noerror ||
rtkey.mode != DNS_TKEYMODE_DELETE ||
rtkey.mode != qtkey.mode ||
!dns_name_equal(&rtkey.algorithm, &qtkey.algorithm) ||
rmsg->rcode != dns_rcode_noerror)
{
tkey_log("dns_tkey_processdeleteresponse: tkey mode invalid "
"or error set");
result = DNS_R_INVALIDTKEY;
dns_rdata_freestruct(&qtkey);
dns_rdata_freestruct(&rtkey);
goto failure;
}
dns_rdata_freestruct(&qtkey);
RETERR(dns_tsigkey_find(&tsigkey, tkeyname, &rtkey.algorithm, ring));
dns_rdata_freestruct(&rtkey);
/*
* Mark the key as deleted.
*/
dns_tsigkey_setdeleted(tsigkey);
/*
* Release the reference.
*/
dns_tsigkey_detach(&tsigkey);
failure:
return (result);
}