2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 02:28:55 +00:00
bind/lib/dns/tsig.c

1890 lines
47 KiB
C
Raw Normal View History

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*! \file */
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <isc/buffer.h>
#include <isc/mem.h>
#include <isc/refcount.h>
#include <isc/result.h>
#include <isc/serial.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/util.h>
#include <dns/fixedname.h>
#include <dns/keyvalues.h>
#include <dns/log.h>
#include <dns/message.h>
2000-07-21 20:53:59 +00:00
#include <dns/rbt.h>
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#include <dns/tsig.h>
#include "tsig_p.h"
2020-02-13 14:44:37 -08:00
#define TSIG_MAGIC ISC_MAGIC('T', 'S', 'I', 'G')
#define VALID_TSIG_KEY(x) ISC_MAGIC_VALID(x, TSIG_MAGIC)
#ifndef DNS_TSIG_MAXGENERATEDKEYS
#define DNS_TSIG_MAXGENERATEDKEYS 4096
#endif /* ifndef DNS_TSIG_MAXGENERATEDKEYS */
#define is_response(msg) ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
2000-08-14 18:13:11 +00:00
2000-08-14 22:00:00 +00:00
#define BADTIMELEN 6
static unsigned char hmacmd5_ndata[] = "\010hmac-md5\007sig-alg\003reg\003int";
static unsigned char hmacmd5_offsets[] = { 0, 9, 17, 21, 25 };
2000-08-14 22:00:00 +00:00
2020-02-13 14:44:37 -08:00
static dns_name_t const hmacmd5 = DNS_NAME_INITABSOLUTE(hmacmd5_ndata,
hmacmd5_offsets);
const dns_name_t *dns_tsig_hmacmd5_name = &hmacmd5;
2020-02-13 14:44:37 -08:00
static unsigned char gsstsig_ndata[] = "\010gss-tsig";
static unsigned char gsstsig_offsets[] = { 0, 9 };
static dns_name_t const gsstsig = DNS_NAME_INITABSOLUTE(gsstsig_ndata,
gsstsig_offsets);
const dns_name_t *dns_tsig_gssapi_name = &gsstsig;
2000-08-17 02:08:27 +00:00
2020-02-13 14:44:37 -08:00
static unsigned char hmacsha1_ndata[] = "\011hmac-sha1";
static unsigned char hmacsha1_offsets[] = { 0, 10 };
static dns_name_t const hmacsha1 = DNS_NAME_INITABSOLUTE(hmacsha1_ndata,
hmacsha1_offsets);
const dns_name_t *dns_tsig_hmacsha1_name = &hmacsha1;
2020-02-13 14:44:37 -08:00
static unsigned char hmacsha224_ndata[] = "\013hmac-sha224";
static unsigned char hmacsha224_offsets[] = { 0, 12 };
static dns_name_t const hmacsha224 = DNS_NAME_INITABSOLUTE(hmacsha224_ndata,
hmacsha224_offsets);
const dns_name_t *dns_tsig_hmacsha224_name = &hmacsha224;
2020-02-13 14:44:37 -08:00
static unsigned char hmacsha256_ndata[] = "\013hmac-sha256";
static unsigned char hmacsha256_offsets[] = { 0, 12 };
static dns_name_t const hmacsha256 = DNS_NAME_INITABSOLUTE(hmacsha256_ndata,
hmacsha256_offsets);
const dns_name_t *dns_tsig_hmacsha256_name = &hmacsha256;
2020-02-13 14:44:37 -08:00
static unsigned char hmacsha384_ndata[] = "\013hmac-sha384";
static unsigned char hmacsha384_offsets[] = { 0, 12 };
static dns_name_t const hmacsha384 = DNS_NAME_INITABSOLUTE(hmacsha384_ndata,
hmacsha384_offsets);
const dns_name_t *dns_tsig_hmacsha384_name = &hmacsha384;
2020-02-13 14:44:37 -08:00
static unsigned char hmacsha512_ndata[] = "\013hmac-sha512";
static unsigned char hmacsha512_offsets[] = { 0, 12 };
static dns_name_t const hmacsha512 = DNS_NAME_INITABSOLUTE(hmacsha512_ndata,
hmacsha512_offsets);
const dns_name_t *dns_tsig_hmacsha512_name = &hmacsha512;
static const struct {
const dns_name_t *name;
2020-02-13 14:44:37 -08:00
unsigned int dstalg;
} known_algs[] = { { &hmacmd5, DST_ALG_HMACMD5 },
{ &gsstsig, DST_ALG_GSSAPI },
{ &hmacsha1, DST_ALG_HMACSHA1 },
{ &hmacsha224, DST_ALG_HMACSHA224 },
{ &hmacsha256, DST_ALG_HMACSHA256 },
{ &hmacsha384, DST_ALG_HMACSHA384 },
{ &hmacsha512, DST_ALG_HMACSHA512 } };
2020-02-14 08:14:03 +01:00
static isc_result_t
tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg);
2020-02-14 08:14:03 +01:00
static void
tsig_log(dns_tsigkey_t *key, int level, const char *fmt, ...)
ISC_FORMAT_PRINTF(3, 4);
2020-02-14 08:14:03 +01:00
static void
cleanup_ring(dns_tsig_keyring_t *ring);
static void
tsigkey_free(dns_tsigkey_t *key);
bool
2020-02-13 14:44:37 -08:00
dns__tsig_algvalid(unsigned int alg) {
return (alg == DST_ALG_HMACMD5 || alg == DST_ALG_HMACSHA1 ||
alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
alg == DST_ALG_HMACSHA384 || alg == DST_ALG_HMACSHA512);
}
static void
2020-02-13 14:44:37 -08:00
tsig_log(dns_tsigkey_t *key, int level, const char *fmt, ...) {
va_list ap;
2020-02-13 14:44:37 -08:00
char message[4096];
char namestr[DNS_NAME_FORMATSIZE];
char creatorstr[DNS_NAME_FORMATSIZE];
if (!isc_log_wouldlog(dns_lctx, level)) {
return;
}
if (key != NULL) {
dns_name_format(&key->name, namestr, sizeof(namestr));
} else {
strlcpy(namestr, "<null>", sizeof(namestr));
}
if (key != NULL && key->generated && key->creator != NULL) {
dns_name_format(key->creator, creatorstr, sizeof(creatorstr));
} else {
strlcpy(creatorstr, "<null>", sizeof(creatorstr));
}
va_start(ap, fmt);
vsnprintf(message, sizeof(message), fmt, ap);
va_end(ap);
if (key != NULL && key->generated) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DNSSEC,
DNS_LOGMODULE_TSIG, level,
"tsig key '%s' (%s): %s", namestr, creatorstr,
message);
} else {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DNSSEC,
DNS_LOGMODULE_TSIG, level, "tsig key '%s': %s",
namestr, message);
}
}
static void
2020-02-13 14:44:37 -08:00
remove_fromring(dns_tsigkey_t *tkey) {
if (tkey->generated) {
ISC_LIST_UNLINK(tkey->ring->lru, tkey, link);
tkey->ring->generated--;
}
(void)dns_rbt_deletename(tkey->ring->keys, &tkey->name, false);
}
static void
2020-02-13 14:44:37 -08:00
adjust_lru(dns_tsigkey_t *tkey) {
if (tkey->generated) {
RWLOCK(&tkey->ring->lock, isc_rwlocktype_write);
/*
* We may have been removed from the LRU list between
* removing the read lock and acquiring the write lock.
*/
2020-02-13 14:44:37 -08:00
if (ISC_LINK_LINKED(tkey, link) && tkey->ring->lru.tail != tkey)
{
ISC_LIST_UNLINK(tkey->ring->lru, tkey, link);
ISC_LIST_APPEND(tkey->ring->lru, tkey, link);
}
RWUNLOCK(&tkey->ring->lock, isc_rwlocktype_write);
}
}
/*
* A supplemental routine just to add a key to ring. Note that reference
* counter should be counted separately because we may be adding the key
* as part of creation of the key, in which case the reference counter was
* already initialized. Also note we don't need RWLOCK for the reference
* counter: it's protected by a separate lock.
*/
static isc_result_t
keyring_add(dns_tsig_keyring_t *ring, const dns_name_t *name,
2020-02-13 14:44:37 -08:00
dns_tsigkey_t *tkey) {
isc_result_t result;
RWLOCK(&ring->lock, isc_rwlocktype_write);
ring->writecount++;
/*
* Do on the fly cleaning. Find some nodes we might not
* want around any more.
*/
if (ring->writecount > 10) {
cleanup_ring(ring);
ring->writecount = 0;
}
result = dns_rbt_addname(ring->keys, name, tkey);
if (result == ISC_R_SUCCESS) {
if (tkey->generated) {
/*
* Add the new key to the LRU list and remove the
* least recently used key if there are too many
* keys on the list.
*/
ISC_LIST_APPEND(ring->lru, tkey, link);
if (ring->generated++ > ring->maxgenerated) {
remove_fromring(ISC_LIST_HEAD(ring->lru));
}
}
tkey->ring = ring;
}
RWUNLOCK(&ring->lock, isc_rwlocktype_write);
return (result);
}
isc_result_t
dns_tsigkey_createfromkey(const dns_name_t *name, const dns_name_t *algorithm,
dst_key_t *dstkey, bool generated, bool restored,
const dns_name_t *creator, isc_stdtime_t inception,
2000-08-17 02:08:27 +00:00
isc_stdtime_t expire, isc_mem_t *mctx,
2020-02-13 14:44:37 -08:00
dns_tsig_keyring_t *ring, dns_tsigkey_t **key) {
dns_tsigkey_t *tkey = NULL;
2020-02-13 14:44:37 -08:00
isc_result_t ret;
unsigned int refs = 0;
unsigned int dstalg = 0;
REQUIRE(key == NULL || *key == NULL);
REQUIRE(name != NULL);
REQUIRE(algorithm != NULL);
REQUIRE(mctx != NULL);
REQUIRE(key != NULL || ring != NULL);
tkey = isc_mem_get(mctx, sizeof(dns_tsigkey_t));
*tkey = (dns_tsigkey_t){
.generated = generated,
.restored = restored,
.ring = ring,
.inception = inception,
.expire = expire,
.name = DNS_NAME_INITEMPTY,
.link = ISC_LINK_INITIALIZER,
};
dns_name_dup(name, mctx, &tkey->name);
(void)dns_name_downcase(&tkey->name, &tkey->name, NULL);
/* Check against known algorithm names */
dstalg = dns__tsig_algfromname(algorithm);
if (dstalg != 0) {
/*
* 'algorithm' must be set to a static pointer
* so that dns__tsig_algallocated() can compare them.
*/
tkey->algorithm = dns__tsig_algnamefromname(algorithm);
if (dstkey != NULL && dst_key_alg(dstkey) != dstalg) {
ret = DNS_R_BADALG;
goto cleanup_name;
}
} else {
dns_name_t *tmpname = NULL;
if (dstkey != NULL) {
ret = DNS_R_BADALG;
2000-08-14 18:13:11 +00:00
goto cleanup_name;
}
tmpname = isc_mem_get(mctx, sizeof(dns_name_t));
dns_name_init(tmpname, NULL);
dns_name_dup(algorithm, mctx, tmpname);
(void)dns_name_downcase(tmpname, tmpname, NULL);
tkey->algorithm = tmpname;
2000-08-14 18:13:11 +00:00
}
if (creator != NULL) {
tkey->creator = isc_mem_get(mctx, sizeof(dns_name_t));
dns_name_init(tkey->creator, NULL);
dns_name_dup(creator, mctx, tkey->creator);
}
if (dstkey != NULL) {
dst_key_attach(dstkey, &tkey->key);
}
if (key != NULL) {
refs = 1;
}
if (ring != NULL) {
refs++;
}
isc_refcount_init(&tkey->refs, refs);
isc_mem_attach(mctx, &tkey->mctx);
tkey->magic = TSIG_MAGIC;
if (ring != NULL) {
ret = keyring_add(ring, name, tkey);
if (ret != ISC_R_SUCCESS) {
goto cleanup_refs;
}
}
/*
* Ignore this if it's a GSS key, since the key size is meaningless.
*/
if (dstkey != NULL && dst_key_size(dstkey) < 64 &&
2022-11-02 19:33:14 +01:00
dstalg != DST_ALG_GSSAPI)
{
char namestr[DNS_NAME_FORMATSIZE];
dns_name_format(name, namestr, sizeof(namestr));
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DNSSEC,
DNS_LOGMODULE_TSIG, ISC_LOG_INFO,
"the key '%s' is too short to be secure",
namestr);
}
if (key != NULL) {
*key = tkey;
}
if (tkey->restored) {
tsig_log(tkey, ISC_LOG_DEBUG(3), "restored from file");
} else if (tkey->generated) {
tsig_log(tkey, ISC_LOG_DEBUG(3), "generated");
} else {
tsig_log(tkey, ISC_LOG_DEBUG(3), "statically configured");
}
return (ISC_R_SUCCESS);
cleanup_refs:
tkey->magic = 0;
while (refs-- > 0) {
isc_refcount_decrement0(&tkey->refs);
}
isc_refcount_destroy(&tkey->refs);
if (tkey->key != NULL) {
dst_key_free(&tkey->key);
}
if (tkey->creator != NULL) {
dns_name_free(tkey->creator, mctx);
isc_mem_put(mctx, tkey->creator, sizeof(dns_name_t));
}
if (dns__tsig_algallocated(tkey->algorithm)) {
dns_name_t *tmpname = UNCONST(tkey->algorithm);
if (dns_name_dynamic(tmpname)) {
dns_name_free(tmpname, mctx);
}
isc_mem_put(mctx, tmpname, sizeof(dns_name_t));
2000-08-14 18:13:11 +00:00
}
cleanup_name:
dns_name_free(&tkey->name, mctx);
2000-07-28 00:01:59 +00:00
isc_mem_put(mctx, tkey, sizeof(dns_tsigkey_t));
return (ret);
}
/*
* Find a few nodes to destroy if possible.
*/
static void
2020-02-13 14:44:37 -08:00
cleanup_ring(dns_tsig_keyring_t *ring) {
isc_result_t result;
dns_rbtnodechain_t chain;
2020-02-13 14:44:37 -08:00
dns_name_t foundname;
dns_fixedname_t fixedorigin;
dns_name_t *origin;
isc_stdtime_t now = isc_stdtime_now();
2020-02-13 14:44:37 -08:00
dns_rbtnode_t *node;
dns_tsigkey_t *tkey;
/*
* Start up a new iterator each time.
*/
dns_name_init(&foundname, NULL);
origin = dns_fixedname_initname(&fixedorigin);
again:
dns_rbtnodechain_init(&chain);
result = dns_rbtnodechain_first(&chain, ring->keys, &foundname, origin);
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
dns_rbtnodechain_invalidate(&chain);
return;
}
for (;;) {
node = NULL;
dns_rbtnodechain_current(&chain, &foundname, origin, &node);
tkey = node->data;
if (tkey != NULL) {
if (tkey->generated &&
isc_refcount_current(&tkey->refs) == 1 &&
tkey->inception != tkey->expire &&
2020-02-13 14:44:37 -08:00
tkey->expire < now)
{
tsig_log(tkey, 2, "tsig expire: deleting");
/* delete the key */
dns_rbtnodechain_invalidate(&chain);
remove_fromring(tkey);
goto again;
}
}
result = dns_rbtnodechain_next(&chain, &foundname, origin);
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
dns_rbtnodechain_invalidate(&chain);
return;
}
}
}
static void
2020-02-13 14:44:37 -08:00
destroyring(dns_tsig_keyring_t *ring) {
isc_refcount_destroy(&ring->references);
dns_rbt_destroy(&ring->keys);
isc_rwlock_destroy(&ring->lock);
isc_mem_putanddetach(&ring->mctx, ring, sizeof(dns_tsig_keyring_t));
}
/*
* Look up the DST_ALG_ constant for a given name.
*/
unsigned int
2020-02-13 14:44:37 -08:00
dns__tsig_algfromname(const dns_name_t *algorithm) {
int i;
int n = sizeof(known_algs) / sizeof(*known_algs);
for (i = 0; i < n; ++i) {
const dns_name_t *name = known_algs[i].name;
if (algorithm == name || dns_name_equal(algorithm, name)) {
return (known_algs[i].dstalg);
}
}
return (0);
}
/*
* Convert an algorithm name into a pointer to the
* corresponding pre-defined dns_name_t structure.
*/
const dns_name_t *
2020-02-13 14:44:37 -08:00
dns__tsig_algnamefromname(const dns_name_t *algorithm) {
int i;
int n = sizeof(known_algs) / sizeof(*known_algs);
for (i = 0; i < n; ++i) {
const dns_name_t *name = known_algs[i].name;
if (algorithm == name || dns_name_equal(algorithm, name)) {
return (name);
}
}
return (NULL);
}
/*
* Test whether the passed algorithm is NOT a pointer to one of the
* pre-defined known algorithms (and therefore one that has been
* dynamically allocated).
*
* This will return an incorrect result if passed a dynamically allocated
* dns_name_t that happens to match one of the pre-defined names.
*/
bool
2020-02-13 14:44:37 -08:00
dns__tsig_algallocated(const dns_name_t *algorithm) {
int i;
int n = sizeof(known_algs) / sizeof(*known_algs);
for (i = 0; i < n; ++i) {
const dns_name_t *name = known_algs[i].name;
if (algorithm == name) {
return (false);
}
}
return (true);
}
static isc_result_t
2020-02-13 14:44:37 -08:00
restore_key(dns_tsig_keyring_t *ring, isc_stdtime_t now, FILE *fp) {
dst_key_t *dstkey = NULL;
char namestr[1024];
char creatorstr[1024];
char algorithmstr[1024];
char keystr[4096];
unsigned int inception, expire;
int n;
isc_buffer_t b;
dns_name_t *name, *creator, *algorithm;
dns_fixedname_t fname, fcreator, falgorithm;
2020-02-13 14:44:37 -08:00
isc_result_t result;
unsigned int dstalg;
n = fscanf(fp, "%1023s %1023s %u %u %1023s %4095s\n", namestr,
creatorstr, &inception, &expire, algorithmstr, keystr);
if (n == EOF) {
return (ISC_R_NOMORE);
}
if (n != 6) {
return (ISC_R_FAILURE);
}
if (isc_serial_lt(expire, now)) {
return (DNS_R_EXPIRED);
}
name = dns_fixedname_initname(&fname);
isc_buffer_init(&b, namestr, strlen(namestr));
isc_buffer_add(&b, strlen(namestr));
result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
if (result != ISC_R_SUCCESS) {
return (result);
}
creator = dns_fixedname_initname(&fcreator);
isc_buffer_init(&b, creatorstr, strlen(creatorstr));
isc_buffer_add(&b, strlen(creatorstr));
result = dns_name_fromtext(creator, &b, dns_rootname, 0, NULL);
if (result != ISC_R_SUCCESS) {
return (result);
}
algorithm = dns_fixedname_initname(&falgorithm);
isc_buffer_init(&b, algorithmstr, strlen(algorithmstr));
isc_buffer_add(&b, strlen(algorithmstr));
result = dns_name_fromtext(algorithm, &b, dns_rootname, 0, NULL);
if (result != ISC_R_SUCCESS) {
return (result);
}
dstalg = dns__tsig_algfromname(algorithm);
if (dstalg == 0) {
return (DNS_R_BADALG);
}
result = dst_key_restore(name, dstalg, DNS_KEYOWNER_ENTITY,
DNS_KEYPROTO_DNSSEC, dns_rdataclass_in,
ring->mctx, keystr, &dstkey);
if (result != ISC_R_SUCCESS) {
return (result);
}
result = dns_tsigkey_createfromkey(name, algorithm, dstkey, true, true,
creator, inception, expire,
ring->mctx, ring, NULL);
if (dstkey != NULL) {
dst_key_free(&dstkey);
}
return (result);
}
static void
2020-02-13 14:44:37 -08:00
dump_key(dns_tsigkey_t *tkey, FILE *fp) {
char *buffer = NULL;
int length = 0;
char namestr[DNS_NAME_FORMATSIZE];
char creatorstr[DNS_NAME_FORMATSIZE];
char algorithmstr[DNS_NAME_FORMATSIZE];
isc_result_t result;
REQUIRE(tkey != NULL);
REQUIRE(fp != NULL);
dns_name_format(&tkey->name, namestr, sizeof(namestr));
dns_name_format(tkey->creator, creatorstr, sizeof(creatorstr));
dns_name_format(tkey->algorithm, algorithmstr, sizeof(algorithmstr));
result = dst_key_dump(tkey->key, tkey->mctx, &buffer, &length);
if (result == ISC_R_SUCCESS) {
fprintf(fp, "%s %s %u %u %s %.*s\n", namestr, creatorstr,
tkey->inception, tkey->expire, algorithmstr, length,
buffer);
}
if (buffer != NULL) {
isc_mem_put(tkey->mctx, buffer, length);
}
}
isc_result_t
2020-02-13 14:44:37 -08:00
dns_tsigkeyring_dumpanddetach(dns_tsig_keyring_t **ringp, FILE *fp) {
isc_result_t result;
dns_rbtnodechain_t chain;
dns_name_t foundname;
dns_fixedname_t fixedorigin;
dns_name_t *origin;
isc_stdtime_t now = isc_stdtime_now();
2020-02-13 14:44:37 -08:00
dns_rbtnode_t *node;
dns_tsigkey_t *tkey;
dns_tsig_keyring_t *ring;
REQUIRE(ringp != NULL && *ringp != NULL);
ring = *ringp;
*ringp = NULL;
2019-05-20 16:55:42 +02:00
if (isc_refcount_decrement(&ring->references) > 1) {
return (DNS_R_CONTINUE);
2019-05-20 16:55:42 +02:00
}
dns_name_init(&foundname, NULL);
origin = dns_fixedname_initname(&fixedorigin);
dns_rbtnodechain_init(&chain);
result = dns_rbtnodechain_first(&chain, ring->keys, &foundname, origin);
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
dns_rbtnodechain_invalidate(&chain);
goto destroy;
}
for (;;) {
node = NULL;
dns_rbtnodechain_current(&chain, &foundname, origin, &node);
tkey = node->data;
if (tkey != NULL && tkey->generated && tkey->expire >= now) {
dump_key(tkey, fp);
}
result = dns_rbtnodechain_next(&chain, &foundname, origin);
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
dns_rbtnodechain_invalidate(&chain);
if (result == ISC_R_NOMORE) {
result = ISC_R_SUCCESS;
}
goto destroy;
}
}
destroy:
destroyring(ring);
return (result);
}
const dns_name_t *
2020-02-13 14:44:37 -08:00
dns_tsigkey_identity(const dns_tsigkey_t *tsigkey) {
REQUIRE(tsigkey == NULL || VALID_TSIG_KEY(tsigkey));
if (tsigkey == NULL) {
return (NULL);
}
if (tsigkey->generated) {
return (tsigkey->creator);
} else {
return (&tsigkey->name);
}
}
2000-08-17 02:08:27 +00:00
isc_result_t
dns_tsigkey_create(const dns_name_t *name, const dns_name_t *algorithm,
unsigned char *secret, int length, bool generated,
bool restored, const dns_name_t *creator,
isc_stdtime_t inception, isc_stdtime_t expire,
isc_mem_t *mctx, dns_tsig_keyring_t *ring,
dns_tsigkey_t **key) {
2020-02-13 14:44:37 -08:00
dst_key_t *dstkey = NULL;
2000-08-17 02:08:27 +00:00
isc_result_t result;
unsigned int dstalg = 0;
2000-08-17 02:08:27 +00:00
REQUIRE(length >= 0);
if (length > 0) {
2000-08-17 02:08:27 +00:00
REQUIRE(secret != NULL);
}
2000-08-17 02:08:27 +00:00
dstalg = dns__tsig_algfromname(algorithm);
if (dns__tsig_algvalid(dstalg)) {
if (secret != NULL) {
isc_buffer_t b;
isc_buffer_init(&b, secret, length);
isc_buffer_add(&b, length);
result = dst_key_frombuffer(
name, dstalg, DNS_KEYOWNER_ENTITY,
DNS_KEYPROTO_DNSSEC, dns_rdataclass_in, &b,
mctx, &dstkey);
if (result != ISC_R_SUCCESS) {
return (result);
}
}
} else if (length > 0) {
return (DNS_R_BADALG);
}
2000-08-17 02:08:27 +00:00
result = dns_tsigkey_createfromkey(name, algorithm, dstkey, generated,
restored, creator, inception, expire,
mctx, ring, key);
if (dstkey != NULL) {
2000-08-17 02:08:27 +00:00
dst_key_free(&dstkey);
}
2000-08-17 02:08:27 +00:00
return (result);
}
void
2020-02-13 14:44:37 -08:00
dns_tsigkey_attach(dns_tsigkey_t *source, dns_tsigkey_t **targetp) {
REQUIRE(VALID_TSIG_KEY(source));
REQUIRE(targetp != NULL && *targetp == NULL);
isc_refcount_increment(&source->refs);
*targetp = source;
}
static void
2020-02-13 14:44:37 -08:00
tsigkey_free(dns_tsigkey_t *key) {
REQUIRE(VALID_TSIG_KEY(key));
key->magic = 0;
dns_name_free(&key->name, key->mctx);
if (dns__tsig_algallocated(key->algorithm)) {
dns_name_t *name = UNCONST(key->algorithm);
dns_name_free(name, key->mctx);
isc_mem_put(key->mctx, name, sizeof(dns_name_t));
2000-08-14 18:13:11 +00:00
}
if (key->key != NULL) {
dst_key_free(&key->key);
}
if (key->creator != NULL) {
dns_name_free(key->creator, key->mctx);
isc_mem_put(key->mctx, key->creator, sizeof(dns_name_t));
}
isc_mem_putanddetach(&key->mctx, key, sizeof(dns_tsigkey_t));
}
void
2020-02-13 14:44:37 -08:00
dns_tsigkey_detach(dns_tsigkey_t **keyp) {
REQUIRE(keyp != NULL && VALID_TSIG_KEY(*keyp));
dns_tsigkey_t *key = *keyp;
*keyp = NULL;
if (isc_refcount_decrement(&key->refs) == 1) {
isc_refcount_destroy(&key->refs);
2000-07-21 20:53:59 +00:00
tsigkey_free(key);
}
}
void
2020-02-13 14:44:37 -08:00
dns_tsigkey_setdeleted(dns_tsigkey_t *key) {
2000-07-21 20:53:59 +00:00
REQUIRE(VALID_TSIG_KEY(key));
REQUIRE(key->ring != NULL);
RWLOCK(&key->ring->lock, isc_rwlocktype_write);
remove_fromring(key);
2000-07-21 20:53:59 +00:00
RWUNLOCK(&key->ring->lock, isc_rwlocktype_write);
}
isc_result_t
2020-02-13 14:44:37 -08:00
dns_tsig_sign(dns_message_t *msg) {
dns_tsigkey_t *key = NULL;
dns_rdata_any_tsig_t tsig, querytsig;
2020-02-13 14:44:37 -08:00
unsigned char data[128];
isc_buffer_t databuf, sigbuf;
isc_buffer_t *dynbuf = NULL;
dns_name_t *owner = NULL;
2020-02-13 14:44:37 -08:00
dns_rdata_t *rdata = NULL;
dns_rdatalist_t *datalist = NULL;
dns_rdataset_t *dataset = NULL;
isc_region_t r;
isc_stdtime_t now;
isc_mem_t *mctx;
dst_context_t *ctx = NULL;
isc_result_t ret;
unsigned char badtimedata[BADTIMELEN];
unsigned int sigsize = 0;
bool response;
REQUIRE(msg != NULL);
key = dns_message_gettsigkey(msg);
REQUIRE(VALID_TSIG_KEY(key));
1999-09-10 15:42:57 +00:00
2019-01-30 11:12:49 +01:00
/*
* If this is a response, there should be a TSIG in the query with the
* the exception if this is a TKEY request (see RFC 3645, Section 2.2).
*/
response = is_response(msg);
2019-01-30 11:12:49 +01:00
if (response && msg->querytsig == NULL) {
if (msg->tkey != 1) {
return (DNS_R_EXPECTEDTSIG);
}
}
1999-09-10 02:42:12 +00:00
mctx = msg->mctx;
2000-05-22 23:17:22 +00:00
tsig.mctx = mctx;
tsig.common.rdclass = dns_rdataclass_any;
tsig.common.rdtype = dns_rdatatype_tsig;
ISC_LINK_INIT(&tsig.common, link);
dns_name_init(&tsig.algorithm, NULL);
2000-08-14 18:13:11 +00:00
dns_name_clone(key->algorithm, &tsig.algorithm);
if (msg->fuzzing) {
now = msg->fuzztime;
} else {
now = isc_stdtime_now();
}
tsig.timesigned = now + msg->timeadjust;
2000-05-22 23:17:22 +00:00
tsig.fudge = DNS_TSIG_FUDGE;
2000-05-22 23:17:22 +00:00
tsig.originalid = msg->id;
isc_buffer_init(&databuf, data, sizeof(data));
if (response) {
2000-05-22 23:17:22 +00:00
tsig.error = msg->querytsigstatus;
} else {
2000-05-22 23:17:22 +00:00
tsig.error = dns_rcode_noerror;
}
2000-05-22 23:17:22 +00:00
if (tsig.error != dns_tsigerror_badtime) {
tsig.otherlen = 0;
tsig.other = NULL;
2000-08-14 22:00:00 +00:00
} else {
isc_buffer_t otherbuf;
2000-08-14 22:00:00 +00:00
tsig.otherlen = BADTIMELEN;
tsig.other = badtimedata;
isc_buffer_init(&otherbuf, tsig.other, tsig.otherlen);
isc_buffer_putuint48(&otherbuf, tsig.timesigned);
}
2000-08-14 22:00:00 +00:00
if ((key->key != NULL) && (tsig.error != dns_tsigerror_badsig) &&
2020-02-13 14:44:37 -08:00
(tsig.error != dns_tsigerror_badkey))
{
unsigned char header[DNS_MESSAGE_HEADERLEN];
2020-02-13 14:44:37 -08:00
isc_buffer_t headerbuf;
uint16_t digestbits;
bool querytsig_ok = false;
2017-06-28 09:11:49 -07:00
/*
* If it is a response, we assume that the request MAC
* has validated at this point. This is why we include a
* MAC length > 0 in the reply.
*/
ret = dst_context_create(key->key, mctx, DNS_LOGCATEGORY_DNSSEC,
true, 0, &ctx);
if (ret != ISC_R_SUCCESS) {
2000-08-14 22:00:00 +00:00
return (ret);
}
1999-12-17 21:09:34 +00:00
/*
* If this is a response, and if there was a TSIG in
* the query, digest the request's MAC.
*
* (Note: querytsig should be non-NULL for all
* responses except TKEY responses. Those may be signed
* with the newly-negotiated TSIG key even if the query
* wasn't signed.)
*/
if (response && msg->querytsig != NULL) {
dns_rdata_t querytsigrdata = DNS_RDATA_INIT;
INSIST(msg->verified_sig);
ret = dns_rdataset_first(msg->querytsig);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
dns_rdataset_current(msg->querytsig, &querytsigrdata);
ret = dns_rdata_tostruct(&querytsigrdata, &querytsig,
NULL);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
isc_buffer_putuint16(&databuf, querytsig.siglen);
if (isc_buffer_availablelength(&databuf) <
2022-11-02 19:33:14 +01:00
querytsig.siglen)
{
ret = ISC_R_NOSPACE;
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
isc_buffer_putmem(&databuf, querytsig.signature,
querytsig.siglen);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
2019-02-13 17:21:16 +11:00
querytsig_ok = true;
1999-12-17 21:09:34 +00:00
}
/*
* Digest the header.
*/
isc_buffer_init(&headerbuf, header, sizeof(header));
dns_message_renderheader(msg, &headerbuf);
isc_buffer_usedregion(&headerbuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
/*
* Digest the remainder of the message.
*/
isc_buffer_usedregion(msg->buffer, &r);
isc_region_consume(&r, DNS_MESSAGE_HEADERLEN);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
if (msg->tcp_continuation == 0) {
/*
* Digest the name, class, ttl, alg.
*/
1999-09-10 14:56:36 +00:00
dns_name_toregion(&key->name, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
isc_buffer_clear(&databuf);
isc_buffer_putuint16(&databuf, dns_rdataclass_any);
isc_buffer_putuint32(&databuf, 0); /* ttl */
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
2000-05-22 23:17:22 +00:00
dns_name_toregion(&tsig.algorithm, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
}
/* Digest the timesigned and fudge */
isc_buffer_clear(&databuf);
2019-02-13 17:21:16 +11:00
if (tsig.error == dns_tsigerror_badtime && querytsig_ok) {
tsig.timesigned = querytsig.timesigned;
2011-08-29 04:02:54 +00:00
}
isc_buffer_putuint48(&databuf, tsig.timesigned);
2000-05-22 23:17:22 +00:00
isc_buffer_putuint16(&databuf, tsig.fudge);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
if (msg->tcp_continuation == 0) {
/*
* Digest the error and other data length.
*/
1999-09-10 14:56:36 +00:00
isc_buffer_clear(&databuf);
2000-05-22 23:17:22 +00:00
isc_buffer_putuint16(&databuf, tsig.error);
isc_buffer_putuint16(&databuf, tsig.otherlen);
1999-09-10 14:56:36 +00:00
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
/*
2011-11-02 19:41:02 +00:00
* Digest other data.
*/
2000-05-22 23:17:22 +00:00
if (tsig.otherlen > 0) {
r.length = tsig.otherlen;
r.base = tsig.other;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
}
}
2000-05-17 22:48:10 +00:00
ret = dst_key_sigsize(key->key, &sigsize);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
tsig.signature = isc_mem_get(mctx, sigsize);
2000-08-17 02:08:27 +00:00
isc_buffer_init(&sigbuf, tsig.signature, sigsize);
2000-06-02 18:59:33 +00:00
ret = dst_context_sign(ctx, &sigbuf);
if (ret != ISC_R_SUCCESS) {
goto cleanup_signature;
}
2000-06-02 18:59:33 +00:00
dst_context_destroy(&ctx);
digestbits = dst_key_getbits(key->key);
if (digestbits != 0) {
2019-02-13 17:21:16 +11:00
unsigned int bytes = (digestbits + 7) / 8;
if (querytsig_ok && bytes < querytsig.siglen) {
bytes = querytsig.siglen;
}
if (bytes > isc_buffer_usedlength(&sigbuf)) {
bytes = isc_buffer_usedlength(&sigbuf);
}
tsig.siglen = bytes;
} else {
tsig.siglen = isc_buffer_usedlength(&sigbuf);
}
2000-08-14 22:00:00 +00:00
} else {
2000-05-22 23:17:22 +00:00
tsig.siglen = 0;
tsig.signature = NULL;
}
dns_message_gettemprdata(msg, &rdata);
isc_buffer_allocate(msg->mctx, &dynbuf, 512);
1999-09-10 02:42:12 +00:00
ret = dns_rdata_fromstruct(rdata, dns_rdataclass_any,
2000-05-22 23:17:22 +00:00
dns_rdatatype_tsig, &tsig, dynbuf);
if (ret != ISC_R_SUCCESS) {
goto cleanup_dynbuf;
}
dns_message_takebuffer(msg, &dynbuf);
2000-05-22 23:17:22 +00:00
if (tsig.signature != NULL) {
2000-08-17 02:08:27 +00:00
isc_mem_put(mctx, tsig.signature, sigsize);
2000-05-22 23:17:22 +00:00
tsig.signature = NULL;
}
dns_message_gettempname(msg, &owner);
dns_name_copy(&key->name, owner);
dns_message_gettemprdatalist(msg, &datalist);
dns_message_gettemprdataset(msg, &dataset);
datalist->rdclass = dns_rdataclass_any;
datalist->type = dns_rdatatype_tsig;
ISC_LIST_APPEND(datalist->rdata, rdata, link);
dns_rdatalist_tordataset(datalist, dataset);
msg->tsig = dataset;
msg->tsigname = owner;
/* Windows does not like the tsig name being compressed. */
msg->tsigname->attributes.nocompress = true;
return (ISC_R_SUCCESS);
cleanup_dynbuf:
isc_buffer_free(&dynbuf);
dns_message_puttemprdata(msg, &rdata);
cleanup_signature:
if (tsig.signature != NULL) {
isc_mem_put(mctx, tsig.signature, sigsize);
}
cleanup_context:
if (ctx != NULL) {
dst_context_destroy(&ctx);
}
return (ret);
}
isc_result_t
2000-01-21 20:18:41 +00:00
dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
2020-02-13 14:44:37 -08:00
dns_tsig_keyring_t *ring1, dns_tsig_keyring_t *ring2) {
dns_rdata_any_tsig_t tsig, querytsig;
2020-02-13 14:44:37 -08:00
isc_region_t r, source_r, header_r, sig_r;
isc_buffer_t databuf;
unsigned char data[32];
dns_name_t *keyname;
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_stdtime_t now;
isc_result_t ret;
dns_tsigkey_t *tsigkey;
dst_key_t *key = NULL;
unsigned char header[DNS_MESSAGE_HEADERLEN];
dst_context_t *ctx = NULL;
isc_mem_t *mctx;
uint16_t addcount, id;
unsigned int siglen;
unsigned int alg;
bool response;
REQUIRE(source != NULL);
REQUIRE(DNS_MESSAGE_VALID(msg));
tsigkey = dns_message_gettsigkey(msg);
response = is_response(msg);
REQUIRE(tsigkey == NULL || VALID_TSIG_KEY(tsigkey));
1999-09-10 15:42:57 +00:00
msg->verify_attempted = 1;
msg->verified_sig = 0;
msg->tsigstatus = dns_tsigerror_badsig;
if (msg->tcp_continuation) {
if (tsigkey == NULL || msg->querytsig == NULL) {
return (DNS_R_UNEXPECTEDTSIG);
}
return (tsig_verify_tcp(source, msg));
}
2000-01-21 20:18:41 +00:00
/*
* There should be a TSIG record...
*/
if (msg->tsig == NULL) {
1999-09-10 15:42:57 +00:00
return (DNS_R_EXPECTEDTSIG);
}
1999-09-10 15:42:57 +00:00
/*
* If this is a response and there's no key or query TSIG, there
* shouldn't be one on the response.
*/
if (response && (tsigkey == NULL || msg->querytsig == NULL)) {
1999-09-10 15:42:57 +00:00
return (DNS_R_UNEXPECTEDTSIG);
}
mctx = msg->mctx;
/*
* If we're here, we know the message is well formed and contains a
* TSIG record.
*/
keyname = msg->tsigname;
ret = dns_rdataset_first(msg->tsig);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
dns_rdataset_current(msg->tsig, &rdata);
ret = dns_rdata_tostruct(&rdata, &tsig, NULL);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
dns_rdata_reset(&rdata);
if (response) {
ret = dns_rdataset_first(msg->querytsig);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
dns_rdataset_current(msg->querytsig, &rdata);
ret = dns_rdata_tostruct(&rdata, &querytsig, NULL);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
}
/*
* Do the key name and algorithm match that of the query?
*/
if (response &&
(!dns_name_equal(keyname, &tsigkey->name) ||
2020-02-13 14:44:37 -08:00
!dns_name_equal(&tsig.algorithm, &querytsig.algorithm)))
{
msg->tsigstatus = dns_tsigerror_badkey;
tsig_log(msg->tsigkey, 2,
"key name and algorithm do not match");
return (DNS_R_TSIGVERIFYFAILURE);
}
/*
* Get the current time.
*/
if (msg->fuzzing) {
now = msg->fuzztime;
} else {
now = isc_stdtime_now();
}
/*
* Find dns_tsigkey_t based on keyname.
*/
if (tsigkey == NULL) {
2000-01-21 20:18:41 +00:00
ret = ISC_R_NOTFOUND;
if (ring1 != NULL) {
2000-01-21 20:18:41 +00:00
ret = dns_tsigkey_find(&tsigkey, keyname,
&tsig.algorithm, ring1);
}
if (ret == ISC_R_NOTFOUND && ring2 != NULL) {
2000-01-21 20:18:41 +00:00
ret = dns_tsigkey_find(&tsigkey, keyname,
&tsig.algorithm, ring2);
}
1999-11-03 16:52:28 +00:00
if (ret != ISC_R_SUCCESS) {
msg->tsigstatus = dns_tsigerror_badkey;
ret = dns_tsigkey_create(
keyname, &tsig.algorithm, NULL, 0, false, false,
NULL, now, now, mctx, NULL, &msg->tsigkey);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
tsig_log(msg->tsigkey, 2, "unknown key");
1999-11-03 16:52:28 +00:00
return (DNS_R_TSIGVERIFYFAILURE);
}
2000-05-27 00:24:07 +00:00
msg->tsigkey = tsigkey;
}
key = tsigkey->key;
/*
* Check digest length.
*/
alg = dst_key_alg(key);
ret = dst_key_sigsize(key, &siglen);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
if (dns__tsig_algvalid(alg)) {
if (tsig.siglen > siglen) {
2014-09-15 18:18:12 -07:00
tsig_log(msg->tsigkey, 2, "signature length too big");
return (DNS_R_FORMERR);
}
if (tsig.siglen > 0 &&
2022-11-02 19:33:14 +01:00
(tsig.siglen < 10 || tsig.siglen < ((siglen + 1) / 2)))
{
tsig_log(msg->tsigkey, 2,
"signature length below minimum");
return (DNS_R_FORMERR);
}
}
if (tsig.siglen > 0) {
uint16_t addcount_n;
sig_r.base = tsig.signature;
sig_r.length = tsig.siglen;
ret = dst_context_create(key, mctx, DNS_LOGCATEGORY_DNSSEC,
false, 0, &ctx);
if (ret != ISC_R_SUCCESS) {
2000-08-14 22:00:00 +00:00
return (ret);
}
2017-06-28 09:11:49 -07:00
if (response) {
isc_buffer_init(&databuf, data, sizeof(data));
isc_buffer_putuint16(&databuf, querytsig.siglen);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
if (querytsig.siglen > 0) {
r.length = querytsig.siglen;
r.base = querytsig.signature;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
}
}
/*
* Extract the header.
*/
isc_buffer_usedregion(source, &r);
memmove(header, r.base, DNS_MESSAGE_HEADERLEN);
1999-09-10 14:56:36 +00:00
isc_region_consume(&r, DNS_MESSAGE_HEADERLEN);
/*
* Decrement the additional field counter.
*/
memmove(&addcount, &header[DNS_MESSAGE_HEADERLEN - 2], 2);
addcount_n = ntohs(addcount);
addcount = htons((uint16_t)(addcount_n - 1));
memmove(&header[DNS_MESSAGE_HEADERLEN - 2], &addcount, 2);
/*
* Put in the original id.
*/
id = htons(tsig.originalid);
memmove(&header[0], &id, 2);
/*
* Digest the modified header.
*/
header_r.base = (unsigned char *)header;
header_r.length = DNS_MESSAGE_HEADERLEN;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &header_r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
/*
* Digest all non-TSIG records.
*/
isc_buffer_usedregion(source, &source_r);
r.base = source_r.base + DNS_MESSAGE_HEADERLEN;
r.length = msg->sigstart - DNS_MESSAGE_HEADERLEN;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
/*
* Digest the key name.
*/
dns_name_toregion(&tsigkey->name, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
isc_buffer_init(&databuf, data, sizeof(data));
isc_buffer_putuint16(&databuf, tsig.common.rdclass);
isc_buffer_putuint32(&databuf, msg->tsig->ttl);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
/*
* Digest the key algorithm.
*/
2000-08-14 18:13:11 +00:00
dns_name_toregion(tsigkey->algorithm, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
isc_buffer_clear(&databuf);
isc_buffer_putuint48(&databuf, tsig.timesigned);
isc_buffer_putuint16(&databuf, tsig.fudge);
isc_buffer_putuint16(&databuf, tsig.error);
isc_buffer_putuint16(&databuf, tsig.otherlen);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
if (tsig.otherlen > 0) {
r.base = tsig.other;
r.length = tsig.otherlen;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
}
2000-06-02 18:59:33 +00:00
ret = dst_context_verify(ctx, &sig_r);
if (ret == DST_R_VERIFYFAILURE) {
2000-06-06 23:44:13 +00:00
ret = DNS_R_TSIGVERIFYFAILURE;
tsig_log(msg->tsigkey, 2,
"signature failed to verify(1)");
2000-06-06 23:44:13 +00:00
goto cleanup_context;
} else if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
msg->verified_sig = 1;
2020-03-25 17:46:26 +11:00
} else if (!response || (tsig.error != dns_tsigerror_badsig &&
tsig.error != dns_tsigerror_badkey))
2020-02-13 14:44:37 -08:00
{
tsig_log(msg->tsigkey, 2, "signature was empty");
return (DNS_R_TSIGVERIFYFAILURE);
}
2017-06-28 09:11:49 -07:00
/*
* Here at this point, the MAC has been verified. Even if any of
* the following code returns a TSIG error, the reply will be
* signed and WILL always include the request MAC in the digest
* computation.
*/
/*
* Is the time ok?
*/
if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {
msg->tsigstatus = dns_tsigerror_badtime;
tsig_log(msg->tsigkey, 2, "signature has expired");
ret = DNS_R_CLOCKSKEW;
goto cleanup_context;
} else if (now + msg->timeadjust < tsig.timesigned - tsig.fudge) {
msg->tsigstatus = dns_tsigerror_badtime;
tsig_log(msg->tsigkey, 2, "signature is in the future");
ret = DNS_R_CLOCKSKEW;
goto cleanup_context;
}
if (dns__tsig_algvalid(alg)) {
uint16_t digestbits = dst_key_getbits(key);
2017-06-28 09:11:49 -07:00
if (tsig.siglen > 0 && digestbits != 0 &&
2022-11-02 19:33:14 +01:00
tsig.siglen < ((digestbits + 7) / 8))
{
2017-06-28 09:11:49 -07:00
msg->tsigstatus = dns_tsigerror_badtrunc;
tsig_log(msg->tsigkey, 2,
"truncated signature length too small");
ret = DNS_R_TSIGVERIFYFAILURE;
goto cleanup_context;
}
2020-02-13 14:44:37 -08:00
if (tsig.siglen > 0 && digestbits == 0 && tsig.siglen < siglen)
{
2017-06-28 09:11:49 -07:00
msg->tsigstatus = dns_tsigerror_badtrunc;
tsig_log(msg->tsigkey, 2, "signature length too small");
ret = DNS_R_TSIGVERIFYFAILURE;
goto cleanup_context;
}
}
2020-03-25 17:46:26 +11:00
if (response && tsig.error != dns_rcode_noerror) {
msg->tsigstatus = tsig.error;
if (tsig.error == dns_tsigerror_badtime) {
ret = DNS_R_CLOCKSKEW;
} else {
ret = DNS_R_TSIGERRORSET;
}
goto cleanup_context;
}
msg->tsigstatus = dns_rcode_noerror;
ret = ISC_R_SUCCESS;
cleanup_context:
if (ctx != NULL) {
2000-06-02 18:59:33 +00:00
dst_context_destroy(&ctx);
}
return (ret);
}
2000-01-21 20:18:41 +00:00
static isc_result_t
2020-02-13 14:44:37 -08:00
tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
dns_rdata_any_tsig_t tsig, querytsig;
2020-02-13 14:44:37 -08:00
isc_region_t r, source_r, header_r, sig_r;
isc_buffer_t databuf;
unsigned char data[32];
dns_name_t *keyname;
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_stdtime_t now;
isc_result_t ret;
dns_tsigkey_t *tsigkey;
dst_key_t *key = NULL;
unsigned char header[DNS_MESSAGE_HEADERLEN];
uint16_t addcount, id;
bool has_tsig = false;
isc_mem_t *mctx;
unsigned int siglen;
unsigned int alg;
1999-09-10 14:56:36 +00:00
REQUIRE(source != NULL);
REQUIRE(msg != NULL);
REQUIRE(dns_message_gettsigkey(msg) != NULL);
1999-09-10 14:56:36 +00:00
REQUIRE(msg->tcp_continuation == 1);
REQUIRE(msg->querytsig != NULL);
1999-09-10 14:56:36 +00:00
msg->verified_sig = 0;
msg->tsigstatus = dns_tsigerror_badsig;
if (!is_response(msg)) {
return (DNS_R_EXPECTEDRESPONSE);
}
2000-06-02 18:59:33 +00:00
mctx = msg->mctx;
tsigkey = dns_message_gettsigkey(msg);
2017-06-28 09:11:49 -07:00
key = tsigkey->key;
/*
* Extract and parse the previous TSIG
*/
ret = dns_rdataset_first(msg->querytsig);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
dns_rdataset_current(msg->querytsig, &rdata);
ret = dns_rdata_tostruct(&rdata, &querytsig, NULL);
if (ret != ISC_R_SUCCESS) {
return (ret);
}
dns_rdata_reset(&rdata);
/*
* If there is a TSIG in this message, do some checks.
*/
if (msg->tsig != NULL) {
has_tsig = true;
1999-09-10 14:56:36 +00:00
keyname = msg->tsigname;
ret = dns_rdataset_first(msg->tsig);
if (ret != ISC_R_SUCCESS) {
goto cleanup_querystruct;
}
dns_rdataset_current(msg->tsig, &rdata);
ret = dns_rdata_tostruct(&rdata, &tsig, NULL);
if (ret != ISC_R_SUCCESS) {
goto cleanup_querystruct;
}
/*
* Do the key name and algorithm match that of the query?
*/
if (!dns_name_equal(keyname, &tsigkey->name) ||
2020-02-13 14:44:37 -08:00
!dns_name_equal(&tsig.algorithm, &querytsig.algorithm))
{
1999-09-10 14:56:36 +00:00
msg->tsigstatus = dns_tsigerror_badkey;
ret = DNS_R_TSIGVERIFYFAILURE;
tsig_log(msg->tsigkey, 2,
"key name and algorithm do not match");
goto cleanup_querystruct;
1999-09-10 14:56:36 +00:00
}
/*
2017-06-28 09:11:49 -07:00
* Check digest length.
*/
2017-06-28 09:11:49 -07:00
alg = dst_key_alg(key);
ret = dst_key_sigsize(key, &siglen);
if (ret != ISC_R_SUCCESS) {
goto cleanup_querystruct;
}
if (dns__tsig_algvalid(alg)) {
2017-06-28 09:11:49 -07:00
if (tsig.siglen > siglen) {
tsig_log(tsigkey, 2,
"signature length too big");
ret = DNS_R_FORMERR;
goto cleanup_querystruct;
}
if (tsig.siglen > 0 &&
(tsig.siglen < 10 ||
2022-11-02 19:33:14 +01:00
tsig.siglen < ((siglen + 1) / 2)))
{
2017-06-28 09:11:49 -07:00
tsig_log(tsigkey, 2,
"signature length below minimum");
ret = DNS_R_FORMERR;
goto cleanup_querystruct;
}
1999-09-10 14:56:36 +00:00
}
}
if (msg->tsigctx == NULL) {
ret = dst_context_create(key, mctx, DNS_LOGCATEGORY_DNSSEC,
false, 0, &msg->tsigctx);
if (ret != ISC_R_SUCCESS) {
goto cleanup_querystruct;
}
1999-09-10 14:56:36 +00:00
/*
* Digest the length of the query signature
*/
isc_buffer_init(&databuf, data, sizeof(data));
isc_buffer_putuint16(&databuf, querytsig.siglen);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(msg->tsigctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
/*
* Digest the data of the query signature
*/
if (querytsig.siglen > 0) {
r.length = querytsig.siglen;
r.base = querytsig.signature;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(msg->tsigctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
}
}
/*
* Extract the header.
*/
isc_buffer_usedregion(source, &r);
memmove(header, r.base, DNS_MESSAGE_HEADERLEN);
1999-09-10 14:56:36 +00:00
isc_region_consume(&r, DNS_MESSAGE_HEADERLEN);
/*
* Decrement the additional field counter if necessary.
*/
1999-09-10 14:56:36 +00:00
if (has_tsig) {
uint16_t addcount_n;
memmove(&addcount, &header[DNS_MESSAGE_HEADERLEN - 2], 2);
addcount_n = ntohs(addcount);
addcount = htons((uint16_t)(addcount_n - 1));
memmove(&header[DNS_MESSAGE_HEADERLEN - 2], &addcount, 2);
1999-09-10 14:56:36 +00:00
/*
* Put in the original id.
*
* XXX Can TCP transfers be forwarded? How would that
* work?
*/
id = htons(tsig.originalid);
memmove(&header[0], &id, 2);
}
1999-09-10 14:56:36 +00:00
/*
* Digest the modified header.
*/
header_r.base = (unsigned char *)header;
1999-09-10 14:56:36 +00:00
header_r.length = DNS_MESSAGE_HEADERLEN;
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(msg->tsigctx, &header_r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
/*
* Digest all non-TSIG records.
*/
isc_buffer_usedregion(source, &source_r);
1999-09-10 14:56:36 +00:00
r.base = source_r.base + DNS_MESSAGE_HEADERLEN;
if (has_tsig) {
r.length = msg->sigstart - DNS_MESSAGE_HEADERLEN;
} else {
1999-09-10 14:56:36 +00:00
r.length = source_r.length - DNS_MESSAGE_HEADERLEN;
}
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(msg->tsigctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
/*
* Digest the time signed and fudge.
*/
1999-09-10 14:56:36 +00:00
if (has_tsig) {
isc_buffer_init(&databuf, data, sizeof(data));
isc_buffer_putuint48(&databuf, tsig.timesigned);
isc_buffer_putuint16(&databuf, tsig.fudge);
isc_buffer_usedregion(&databuf, &r);
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(msg->tsigctx, &r);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
sig_r.base = tsig.signature;
sig_r.length = tsig.siglen;
if (tsig.siglen == 0) {
if (tsig.error != dns_rcode_noerror) {
msg->tsigstatus = tsig.error;
if (tsig.error == dns_tsigerror_badtime) {
ret = DNS_R_CLOCKSKEW;
} else {
ret = DNS_R_TSIGERRORSET;
}
} else {
tsig_log(msg->tsigkey, 2, "signature is empty");
ret = DNS_R_TSIGVERIFYFAILURE;
}
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
2000-06-02 18:59:33 +00:00
ret = dst_context_verify(msg->tsigctx, &sig_r);
if (ret == DST_R_VERIFYFAILURE) {
tsig_log(msg->tsigkey, 2,
"signature failed to verify(2)");
2000-06-02 18:59:33 +00:00
ret = DNS_R_TSIGVERIFYFAILURE;
goto cleanup_context;
} else if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
goto cleanup_context;
}
msg->verified_sig = 1;
1999-09-10 14:56:36 +00:00
2017-06-28 09:11:49 -07:00
/*
* Here at this point, the MAC has been verified. Even
* if any of the following code returns a TSIG error,
* the reply will be signed and WILL always include the
* request MAC in the digest computation.
*/
/*
* Is the time ok?
*/
if (msg->fuzzing) {
now = msg->fuzztime;
} else {
now = isc_stdtime_now();
}
2017-06-28 09:11:49 -07:00
if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {
msg->tsigstatus = dns_tsigerror_badtime;
tsig_log(msg->tsigkey, 2, "signature has expired");
ret = DNS_R_CLOCKSKEW;
goto cleanup_context;
2020-02-13 14:44:37 -08:00
} else if (now + msg->timeadjust < tsig.timesigned - tsig.fudge)
{
2017-06-28 09:11:49 -07:00
msg->tsigstatus = dns_tsigerror_badtime;
tsig_log(msg->tsigkey, 2, "signature is in the future");
2017-06-28 09:11:49 -07:00
ret = DNS_R_CLOCKSKEW;
goto cleanup_context;
}
alg = dst_key_alg(key);
ret = dst_key_sigsize(key, &siglen);
if (ret != ISC_R_SUCCESS) {
2017-06-28 09:11:49 -07:00
goto cleanup_context;
}
if (dns__tsig_algvalid(alg)) {
uint16_t digestbits = dst_key_getbits(key);
2017-06-28 09:11:49 -07:00
if (tsig.siglen > 0 && digestbits != 0 &&
2022-11-02 19:33:14 +01:00
tsig.siglen < ((digestbits + 7) / 8))
{
2017-06-28 09:11:49 -07:00
msg->tsigstatus = dns_tsigerror_badtrunc;
tsig_log(msg->tsigkey, 2,
"truncated signature length "
"too small");
ret = DNS_R_TSIGVERIFYFAILURE;
goto cleanup_context;
}
if (tsig.siglen > 0 && digestbits == 0 &&
2022-11-02 19:33:14 +01:00
tsig.siglen < siglen)
{
2017-06-28 09:11:49 -07:00
msg->tsigstatus = dns_tsigerror_badtrunc;
tsig_log(msg->tsigkey, 2,
"signature length too small");
ret = DNS_R_TSIGVERIFYFAILURE;
goto cleanup_context;
}
}
if (tsig.error != dns_rcode_noerror) {
msg->tsigstatus = tsig.error;
if (tsig.error == dns_tsigerror_badtime) {
ret = DNS_R_CLOCKSKEW;
} else {
ret = DNS_R_TSIGERRORSET;
}
goto cleanup_context;
}
1999-09-10 14:56:36 +00:00
}
msg->tsigstatus = dns_rcode_noerror;
ret = ISC_R_SUCCESS;
2000-06-02 18:59:33 +00:00
cleanup_context:
/*
* Except in error conditions, don't destroy the DST context
* for unsigned messages; it is a running sum till the next
* TSIG signed message.
*/
if ((ret != ISC_R_SUCCESS || has_tsig) && msg->tsigctx != NULL) {
dst_context_destroy(&msg->tsigctx);
}
cleanup_querystruct:
dns_rdata_freestruct(&querytsig);
2000-06-02 18:59:33 +00:00
return (ret);
1999-09-10 14:56:36 +00:00
}
isc_result_t
dns_tsigkey_find(dns_tsigkey_t **tsigkey, const dns_name_t *name,
2020-02-13 14:44:37 -08:00
const dns_name_t *algorithm, dns_tsig_keyring_t *ring) {
1999-10-08 18:37:24 +00:00
dns_tsigkey_t *key;
isc_stdtime_t now = isc_stdtime_now();
2020-02-13 14:44:37 -08:00
isc_result_t result;
REQUIRE(tsigkey != NULL);
REQUIRE(*tsigkey == NULL);
REQUIRE(name != NULL);
2000-01-21 20:18:41 +00:00
REQUIRE(ring != NULL);
RWLOCK(&ring->lock, isc_rwlocktype_write);
cleanup_ring(ring);
RWUNLOCK(&ring->lock, isc_rwlocktype_write);
RWLOCK(&ring->lock, isc_rwlocktype_read);
2000-07-21 20:53:59 +00:00
key = NULL;
result = dns_rbt_findname(ring->keys, name, 0, NULL, (void *)&key);
if (result == DNS_R_PARTIALMATCH || result == ISC_R_NOTFOUND) {
RWUNLOCK(&ring->lock, isc_rwlocktype_read);
return (ISC_R_NOTFOUND);
}
2000-08-14 18:13:11 +00:00
if (algorithm != NULL && !dns_name_equal(key->algorithm, algorithm)) {
2000-07-21 20:53:59 +00:00
RWUNLOCK(&ring->lock, isc_rwlocktype_read);
return (ISC_R_NOTFOUND);
}
if (key->inception != key->expire && isc_serial_lt(key->expire, now)) {
2000-07-21 20:53:59 +00:00
/*
* The key has expired.
*/
RWUNLOCK(&ring->lock, isc_rwlocktype_read);
RWLOCK(&ring->lock, isc_rwlocktype_write);
remove_fromring(key);
2000-07-21 20:53:59 +00:00
RWUNLOCK(&ring->lock, isc_rwlocktype_write);
return (ISC_R_NOTFOUND);
}
#if 0
/*
* MPAXXX We really should look at the inception time.
*/
if (key->inception != key->expire &&
isc_serial_lt(key->inception, now)) {
RWUNLOCK(&ring->lock, isc_rwlocktype_read);
adjust_lru(key);
return (ISC_R_NOTFOUND);
}
#endif /* if 0 */
isc_refcount_increment(&key->refs);
RWUNLOCK(&ring->lock, isc_rwlocktype_read);
adjust_lru(key);
2000-07-21 20:53:59 +00:00
*tsigkey = key;
return (ISC_R_SUCCESS);
}
static void
2020-02-13 14:44:37 -08:00
free_tsignode(void *node, void *_unused) {
2000-07-21 20:53:59 +00:00
dns_tsigkey_t *key;
REQUIRE(node != NULL);
UNUSED(_unused);
2000-07-21 20:53:59 +00:00
key = node;
if (key->generated) {
if (ISC_LINK_LINKED(key, link)) {
ISC_LIST_UNLINK(key->ring->lru, key, link);
}
}
2000-07-21 20:53:59 +00:00
dns_tsigkey_detach(&key);
}
isc_result_t
2020-02-13 14:44:37 -08:00
dns_tsigkeyring_create(isc_mem_t *mctx, dns_tsig_keyring_t **ringp) {
isc_result_t result;
2000-07-21 20:53:59 +00:00
dns_tsig_keyring_t *ring;
REQUIRE(mctx != NULL);
2000-07-21 20:53:59 +00:00
REQUIRE(ringp != NULL);
REQUIRE(*ringp == NULL);
2000-07-21 20:53:59 +00:00
ring = isc_mem_get(mctx, sizeof(dns_tsig_keyring_t));
Add the reader-writer synchronization with modified C-RW-WP This changes the internal isc_rwlock implementation to: Irina Calciu, Dave Dice, Yossi Lev, Victor Luchangco, Virendra J. Marathe, and Nir Shavit. 2013. NUMA-aware reader-writer locks. SIGPLAN Not. 48, 8 (August 2013), 157–166. DOI:https://doi.org/10.1145/2517327.24425 (The full article available from: http://mcg.cs.tau.ac.il/papers/ppopp2013-rwlocks.pdf) The implementation is based on the The Writer-Preference Lock (C-RW-WP) variant (see the 3.4 section of the paper for the rationale). The implemented algorithm has been modified for simplicity and for usage patterns in rbtdb.c. The changes compared to the original algorithm: * We haven't implemented the cohort locks because that would require a knowledge of NUMA nodes, instead a simple atomic_bool is used as synchronization point for writer lock. * The per-thread reader counters are not being used - this would require the internal thread id (isc_tid_v) to be always initialized, even in the utilities; the change has a slight performance penalty, so we might revisit this change in the future. However, this change also saves a lot of memory, because cache-line aligned counters were used, so on 32-core machine, the rwlock would be 4096+ bytes big. * The readers use a writer_barrier that will raise after a while when readers lock can't be acquired to prevent readers starvation. * Separate ingress and egress readers counters queues to reduce both inter and intra-thread contention.
2021-03-24 17:52:56 +01:00
isc_rwlock_init(&ring->lock);
2000-07-21 20:53:59 +00:00
ring->keys = NULL;
result = dns_rbt_create(mctx, free_tsignode, NULL, &ring->keys);
if (result != ISC_R_SUCCESS) {
isc_rwlock_destroy(&ring->lock);
isc_mem_put(mctx, ring, sizeof(dns_tsig_keyring_t));
return (result);
}
ring->writecount = 0;
ring->mctx = NULL;
ring->generated = 0;
ring->maxgenerated = DNS_TSIG_MAXGENERATEDKEYS;
ISC_LIST_INIT(ring->lru);
isc_mem_attach(mctx, &ring->mctx);
2019-05-20 16:55:42 +02:00
isc_refcount_init(&ring->references, 1);
2000-07-21 20:53:59 +00:00
*ringp = ring;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_tsigkeyring_add(dns_tsig_keyring_t *ring, const dns_name_t *name,
2020-02-13 14:44:37 -08:00
dns_tsigkey_t *tkey) {
isc_result_t result;
REQUIRE(VALID_TSIG_KEY(tkey));
REQUIRE(tkey->ring == NULL);
REQUIRE(name != NULL);
result = keyring_add(ring, name, tkey);
if (result == ISC_R_SUCCESS) {
isc_refcount_increment(&tkey->refs);
}
return (result);
}
void
2020-02-13 14:44:37 -08:00
dns_tsigkeyring_attach(dns_tsig_keyring_t *source,
dns_tsig_keyring_t **target) {
REQUIRE(source != NULL);
REQUIRE(target != NULL && *target == NULL);
2019-05-20 16:55:42 +02:00
isc_refcount_increment(&source->references);
*target = source;
}
void
2020-02-13 14:44:37 -08:00
dns_tsigkeyring_detach(dns_tsig_keyring_t **ringp) {
2000-07-21 20:53:59 +00:00
dns_tsig_keyring_t *ring;
2000-01-21 22:51:48 +00:00
2000-07-21 20:53:59 +00:00
REQUIRE(ringp != NULL);
REQUIRE(*ringp != NULL);
2000-01-21 20:18:41 +00:00
2000-07-21 20:53:59 +00:00
ring = *ringp;
*ringp = NULL;
2000-01-21 20:18:41 +00:00
2019-05-20 16:55:42 +02:00
if (isc_refcount_decrement(&ring->references) == 1) {
destroyring(ring);
2019-05-20 16:55:42 +02:00
}
}
void
2020-02-13 14:44:37 -08:00
dns_keyring_restore(dns_tsig_keyring_t *ring, FILE *fp) {
isc_stdtime_t now = isc_stdtime_now();
2020-02-13 14:44:37 -08:00
isc_result_t result;
do {
result = restore_key(ring, now, fp);
if (result == ISC_R_NOMORE) {
return;
}
if (result == DNS_R_BADALG || result == DNS_R_EXPIRED) {
result = ISC_R_SUCCESS;
}
} while (result == ISC_R_SUCCESS);
}