keytable.c revision ea14aa24aa9f90cadb49cbaf492237b6cf2b04a7
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Copyright (C) 2000 Internet Software Consortium.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Permission to use, copy, modify, and distribute this software for any
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * purpose with or without fee is hereby granted, provided that the above
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * copyright notice and this permission notice appear in all copies.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <isc/string.h> /* Required for HP/UX (and others?) */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /* Unlocked. */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt unsigned int magic;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /* Locked by lock. */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /* Locked by rwlock. */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt#define VALID_KEYTABLE(kt) ISC_MAGIC_VALID(kt, KEYTABLE_MAGIC)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt unsigned int magic;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt#define VALID_KEYNODE(kn) ISC_MAGIC_VALID(kn, KEYNODE_MAGIC)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_mem_put(mctx, keynode, sizeof(dns_keynode_t));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntdns_keytable_create(isc_mem_t *mctx, dns_keytable_t **keytablep) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Create a keytable.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_create(mctx, free_keynode, mctx, &keytable->table);
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt "isc_mutex_init() failed: %s",
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = isc_rwlock_init(&keytable->rwlock, 0, 0);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt "isc_rwlock_init() failed: %s",
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntdns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Attach *targetp to source.
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Detach *keytablep from its keytable.
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(keytablep != NULL && VALID_KEYTABLE(*keytablep));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (keytable->references == 0 && keytable->active_nodes == 0)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_mem_put(keytable->mctx, keytable, sizeof *keytable);
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_add(dns_keytable_t *keytable, dst_key_t **keyp) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Add '*keyp' to 'keytable'.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_name_fromtext(dns_fixedname_name(&fname), &buffer,
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt knode = isc_mem_get(keytable->mctx, sizeof *knode);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt result = dns_rbt_addnode(keytable->table, dns_fixedname_name(&fname),
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mem_put(keytable->mctx, knode, sizeof *knode);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntdns_keytable_findkeynode(dns_keytable_t *keytable, dns_name_t *name,
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Search for a key named 'name', matching 'algorithm' and 'tag' in
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * 'keytable'.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_findname(keytable->table, name, 0, NULL, &data);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt for (knode = data; knode != NULL; knode = knode->next) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (algorithm == (dns_secalg_t)dst_key_alg(knode->key)
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_finddeepestmatch(dns_keytable_t *keytable, dns_name_t *name,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Search for the deepest match in 'keytable'.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_findname(keytable->table, name, 0, foundname, &data);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_detachkeynode(dns_keytable_t *keytable, dns_keynode_t **keynodep)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Give back a keynode found via dns_keytable_findkeynode().
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep));
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Huntdns_keytable_issecuredomain(dns_keytable_t *keytable, dns_name_t *name,
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt * Is 'name' at or beneath a trusted key?
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_findname(keytable->table, name, 0, NULL, &data);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Get the DST key associated with keynode.
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt * Get the next keynode in the list.