1b7579f77SDag-Erling Smørgrav /* 2b7579f77SDag-Erling Smørgrav * validator/val_anchor.h - validator trust anchor storage. 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2417d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2517d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2617d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2717d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2817d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2917d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3017d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3117d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3217d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3317d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav 36b7579f77SDag-Erling Smørgrav /** 37b7579f77SDag-Erling Smørgrav * \file 38b7579f77SDag-Erling Smørgrav * 39b7579f77SDag-Erling Smørgrav * This file contains storage for the trust anchors for the validator. 40b7579f77SDag-Erling Smørgrav */ 41b7579f77SDag-Erling Smørgrav 42b7579f77SDag-Erling Smørgrav #ifndef VALIDATOR_VAL_ANCHOR_H 43b7579f77SDag-Erling Smørgrav #define VALIDATOR_VAL_ANCHOR_H 44b7579f77SDag-Erling Smørgrav #include "util/rbtree.h" 45b7579f77SDag-Erling Smørgrav #include "util/locks.h" 46b7579f77SDag-Erling Smørgrav struct trust_anchor; 47b7579f77SDag-Erling Smørgrav struct config_file; 48b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key; 49b7579f77SDag-Erling Smørgrav struct autr_point_data; 50b7579f77SDag-Erling Smørgrav struct autr_global_data; 5117d15b25SDag-Erling Smørgrav struct sldns_buffer; 52b7579f77SDag-Erling Smørgrav 53b7579f77SDag-Erling Smørgrav /** 54b7579f77SDag-Erling Smørgrav * Trust anchor store. 55b7579f77SDag-Erling Smørgrav * The tree must be locked, while no other locks (from trustanchors) are held. 56b7579f77SDag-Erling Smørgrav * And then an anchor searched for. Which can be locked or deleted. Then 57b7579f77SDag-Erling Smørgrav * the tree can be unlocked again. This means you have to release the lock 58b7579f77SDag-Erling Smørgrav * on a trust anchor and look it up again to delete it. 59b7579f77SDag-Erling Smørgrav */ 60b7579f77SDag-Erling Smørgrav struct val_anchors { 61*be771a7bSCy Schubert /** lock on trees. It is locked in order after stubs. */ 623005e0a3SDag-Erling Smørgrav lock_basic_type lock; 63b7579f77SDag-Erling Smørgrav /** 64b7579f77SDag-Erling Smørgrav * Anchors are store in this tree. Sort order is chosen, so that 65b7579f77SDag-Erling Smørgrav * dnames are in nsec-like order. A lookup on class, name will return 66b7579f77SDag-Erling Smørgrav * an exact match of the closest match, with the ancestor needed. 67b7579f77SDag-Erling Smørgrav * contents of type trust_anchor. 68b7579f77SDag-Erling Smørgrav */ 693005e0a3SDag-Erling Smørgrav rbtree_type* tree; 70b7579f77SDag-Erling Smørgrav /** Autotrust global data, anchors sorted by next probe time */ 71b7579f77SDag-Erling Smørgrav struct autr_global_data* autr; 72b7579f77SDag-Erling Smørgrav }; 73b7579f77SDag-Erling Smørgrav 74b7579f77SDag-Erling Smørgrav /** 75b7579f77SDag-Erling Smørgrav * Trust anchor key 76b7579f77SDag-Erling Smørgrav */ 77b7579f77SDag-Erling Smørgrav struct ta_key { 78b7579f77SDag-Erling Smørgrav /** next in list */ 79b7579f77SDag-Erling Smørgrav struct ta_key* next; 80b7579f77SDag-Erling Smørgrav /** rdata, in wireformat of the key RR. starts with rdlength. */ 81b7579f77SDag-Erling Smørgrav uint8_t* data; 82b7579f77SDag-Erling Smørgrav /** length of the rdata (including rdlength). */ 83b7579f77SDag-Erling Smørgrav size_t len; 84b7579f77SDag-Erling Smørgrav /** DNS type (host format) of the key, DS or DNSKEY */ 85b7579f77SDag-Erling Smørgrav uint16_t type; 86b7579f77SDag-Erling Smørgrav }; 87b7579f77SDag-Erling Smørgrav 88b7579f77SDag-Erling Smørgrav /** 89b7579f77SDag-Erling Smørgrav * A trust anchor in the trust anchor store. 90b7579f77SDag-Erling Smørgrav * Unique by name, class. 91b7579f77SDag-Erling Smørgrav */ 92b7579f77SDag-Erling Smørgrav struct trust_anchor { 93b7579f77SDag-Erling Smørgrav /** rbtree node, key is this structure */ 943005e0a3SDag-Erling Smørgrav rbnode_type node; 95b7579f77SDag-Erling Smørgrav /** lock on the entire anchor and its keys; for autotrust changes */ 963005e0a3SDag-Erling Smørgrav lock_basic_type lock; 97b7579f77SDag-Erling Smørgrav /** name of this trust anchor */ 98b7579f77SDag-Erling Smørgrav uint8_t* name; 99b7579f77SDag-Erling Smørgrav /** length of name */ 100b7579f77SDag-Erling Smørgrav size_t namelen; 101b7579f77SDag-Erling Smørgrav /** number of labels in name of rrset */ 102b7579f77SDag-Erling Smørgrav int namelabs; 103b7579f77SDag-Erling Smørgrav /** the ancestor in the trustanchor tree */ 104b7579f77SDag-Erling Smørgrav struct trust_anchor* parent; 105b7579f77SDag-Erling Smørgrav /** 106b7579f77SDag-Erling Smørgrav * List of DS or DNSKEY rrs that form the trust anchor. 107b7579f77SDag-Erling Smørgrav */ 108b7579f77SDag-Erling Smørgrav struct ta_key* keylist; 109b7579f77SDag-Erling Smørgrav /** Autotrust anchor point data, or NULL */ 110b7579f77SDag-Erling Smørgrav struct autr_point_data* autr; 111b7579f77SDag-Erling Smørgrav /** number of DSs in the keylist */ 112b7579f77SDag-Erling Smørgrav size_t numDS; 113b7579f77SDag-Erling Smørgrav /** number of DNSKEYs in the keylist */ 114b7579f77SDag-Erling Smørgrav size_t numDNSKEY; 115b7579f77SDag-Erling Smørgrav /** the DS RRset */ 116b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key* ds_rrset; 117b7579f77SDag-Erling Smørgrav /** The DNSKEY RRset */ 118b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key* dnskey_rrset; 119b7579f77SDag-Erling Smørgrav /** class of the trust anchor */ 120b7579f77SDag-Erling Smørgrav uint16_t dclass; 121b7579f77SDag-Erling Smørgrav }; 122b7579f77SDag-Erling Smørgrav 123b7579f77SDag-Erling Smørgrav /** 124b7579f77SDag-Erling Smørgrav * Create trust anchor storage 125b7579f77SDag-Erling Smørgrav * @return new storage or NULL on error. 126b7579f77SDag-Erling Smørgrav */ 127b7579f77SDag-Erling Smørgrav struct val_anchors* anchors_create(void); 128b7579f77SDag-Erling Smørgrav 129b7579f77SDag-Erling Smørgrav /** 130b7579f77SDag-Erling Smørgrav * Delete trust anchor storage. 131b7579f77SDag-Erling Smørgrav * @param anchors: to delete. 132b7579f77SDag-Erling Smørgrav */ 133b7579f77SDag-Erling Smørgrav void anchors_delete(struct val_anchors* anchors); 134b7579f77SDag-Erling Smørgrav 135b7579f77SDag-Erling Smørgrav /** 136b7579f77SDag-Erling Smørgrav * Process trust anchor config. 137b7579f77SDag-Erling Smørgrav * @param anchors: struct anchor storage 138b7579f77SDag-Erling Smørgrav * @param cfg: config options. 139b7579f77SDag-Erling Smørgrav * @return 0 on error. 140b7579f77SDag-Erling Smørgrav */ 141b7579f77SDag-Erling Smørgrav int anchors_apply_cfg(struct val_anchors* anchors, struct config_file* cfg); 142b7579f77SDag-Erling Smørgrav 143b7579f77SDag-Erling Smørgrav /** 144b7579f77SDag-Erling Smørgrav * Recalculate parent pointers. The caller must hold the lock on the 145b7579f77SDag-Erling Smørgrav * anchors structure (say after removing an item from the rbtree). 146b7579f77SDag-Erling Smørgrav * Caller must not hold any locks on trust anchors. 147b7579f77SDag-Erling Smørgrav * After the call is complete the parent pointers are updated and an item 148b7579f77SDag-Erling Smørgrav * just removed is no longer referenced in parent pointers. 149b7579f77SDag-Erling Smørgrav * @param anchors: the structure to update. 150b7579f77SDag-Erling Smørgrav */ 151b7579f77SDag-Erling Smørgrav void anchors_init_parents_locked(struct val_anchors* anchors); 152b7579f77SDag-Erling Smørgrav 153b7579f77SDag-Erling Smørgrav /** 154b7579f77SDag-Erling Smørgrav * Given a qname/qclass combination, find the trust anchor closest above it. 155b7579f77SDag-Erling Smørgrav * Or return NULL if none exists. 156b7579f77SDag-Erling Smørgrav * 157b7579f77SDag-Erling Smørgrav * @param anchors: struct anchor storage 158b7579f77SDag-Erling Smørgrav * @param qname: query name, uncompressed wireformat. 159b7579f77SDag-Erling Smørgrav * @param qname_len: length of qname. 160b7579f77SDag-Erling Smørgrav * @param qclass: class to query for. 161b7579f77SDag-Erling Smørgrav * @return the trust anchor or NULL if none is found. The anchor is locked. 162b7579f77SDag-Erling Smørgrav */ 163b7579f77SDag-Erling Smørgrav struct trust_anchor* anchors_lookup(struct val_anchors* anchors, 164b7579f77SDag-Erling Smørgrav uint8_t* qname, size_t qname_len, uint16_t qclass); 165b7579f77SDag-Erling Smørgrav 166b7579f77SDag-Erling Smørgrav /** 167b7579f77SDag-Erling Smørgrav * Find a trust anchor. Exact matching. 168b7579f77SDag-Erling Smørgrav * @param anchors: anchor storage. 169b7579f77SDag-Erling Smørgrav * @param name: name of trust anchor (wireformat) 170b7579f77SDag-Erling Smørgrav * @param namelabs: labels in name 171b7579f77SDag-Erling Smørgrav * @param namelen: length of name 172b7579f77SDag-Erling Smørgrav * @param dclass: class of trust anchor 173b7579f77SDag-Erling Smørgrav * @return NULL if not found. The anchor is locked. 174b7579f77SDag-Erling Smørgrav */ 175b7579f77SDag-Erling Smørgrav struct trust_anchor* anchor_find(struct val_anchors* anchors, 176b7579f77SDag-Erling Smørgrav uint8_t* name, int namelabs, size_t namelen, uint16_t dclass); 177b7579f77SDag-Erling Smørgrav 178b7579f77SDag-Erling Smørgrav /** 179b7579f77SDag-Erling Smørgrav * Store one string as trust anchor RR. 180b7579f77SDag-Erling Smørgrav * @param anchors: anchor storage. 181b7579f77SDag-Erling Smørgrav * @param buffer: parsing buffer, to generate the RR wireformat in. 182b7579f77SDag-Erling Smørgrav * @param str: string. 183b7579f77SDag-Erling Smørgrav * @return NULL on error. 184b7579f77SDag-Erling Smørgrav */ 185b7579f77SDag-Erling Smørgrav struct trust_anchor* anchor_store_str(struct val_anchors* anchors, 18617d15b25SDag-Erling Smørgrav struct sldns_buffer* buffer, const char* str); 187b7579f77SDag-Erling Smørgrav 188b7579f77SDag-Erling Smørgrav /** 189b7579f77SDag-Erling Smørgrav * Get memory in use by the trust anchor storage 190b7579f77SDag-Erling Smørgrav * @param anchors: anchor storage. 191b7579f77SDag-Erling Smørgrav * @return memory in use in bytes. 192b7579f77SDag-Erling Smørgrav */ 193b7579f77SDag-Erling Smørgrav size_t anchors_get_mem(struct val_anchors* anchors); 194b7579f77SDag-Erling Smørgrav 195b7579f77SDag-Erling Smørgrav /** compare two trust anchors */ 196b7579f77SDag-Erling Smørgrav int anchor_cmp(const void* k1, const void* k2); 197b7579f77SDag-Erling Smørgrav 198b7579f77SDag-Erling Smørgrav /** 199b7579f77SDag-Erling Smørgrav * Add insecure point trust anchor. For external use (locks and init_parents) 200b7579f77SDag-Erling Smørgrav * @param anchors: anchor storage. 201b7579f77SDag-Erling Smørgrav * @param c: class. 202b7579f77SDag-Erling Smørgrav * @param nm: name of insecure trust point. 203b7579f77SDag-Erling Smørgrav * @return false on alloc failure. 204b7579f77SDag-Erling Smørgrav */ 205b7579f77SDag-Erling Smørgrav int anchors_add_insecure(struct val_anchors* anchors, uint16_t c, uint8_t* nm); 206b7579f77SDag-Erling Smørgrav 207b7579f77SDag-Erling Smørgrav /** 208b7579f77SDag-Erling Smørgrav * Delete insecure point trust anchor. Does not remove if no such point. 209b7579f77SDag-Erling Smørgrav * For external use (locks and init_parents) 210b7579f77SDag-Erling Smørgrav * @param anchors: anchor storage. 211b7579f77SDag-Erling Smørgrav * @param c: class. 212b7579f77SDag-Erling Smørgrav * @param nm: name of insecure trust point. 213b7579f77SDag-Erling Smørgrav */ 214b7579f77SDag-Erling Smørgrav void anchors_delete_insecure(struct val_anchors* anchors, uint16_t c, 215b7579f77SDag-Erling Smørgrav uint8_t* nm); 216b7579f77SDag-Erling Smørgrav 21765b390aaSDag-Erling Smørgrav /** 21865b390aaSDag-Erling Smørgrav * Get a list of keytags for the trust anchor. Zero tags for insecure points. 21965b390aaSDag-Erling Smørgrav * @param ta: trust anchor (locked by caller). 22065b390aaSDag-Erling Smørgrav * @param list: array of uint16_t. 22165b390aaSDag-Erling Smørgrav * @param num: length of array. 22265b390aaSDag-Erling Smørgrav * @return number of keytags filled into array. If total number of keytags is 22365b390aaSDag-Erling Smørgrav * bigger than the array, it is truncated at num. On errors, less keytags 22465b390aaSDag-Erling Smørgrav * are filled in. The array is sorted. 22565b390aaSDag-Erling Smørgrav */ 22665b390aaSDag-Erling Smørgrav size_t anchor_list_keytags(struct trust_anchor* ta, uint16_t* list, size_t num); 22765b390aaSDag-Erling Smørgrav 2280fb34990SDag-Erling Smørgrav /** 2290fb34990SDag-Erling Smørgrav * Check if there is a trust anchor for given zone with this keytag. 2300fb34990SDag-Erling Smørgrav * 2310fb34990SDag-Erling Smørgrav * @param anchors: anchor storage 2320fb34990SDag-Erling Smørgrav * @param name: name of trust anchor (wireformat) 2330fb34990SDag-Erling Smørgrav * @param namelabs: labels in name 2340fb34990SDag-Erling Smørgrav * @param namelen: length of name 2350fb34990SDag-Erling Smørgrav * @param dclass: class of trust anchor 2360fb34990SDag-Erling Smørgrav * @param keytag: keytag 2370fb34990SDag-Erling Smørgrav * @return 1 if there is a trust anchor in the trustachor store for this zone 2380fb34990SDag-Erling Smørgrav * and keytag, else 0. 2390fb34990SDag-Erling Smørgrav */ 2400fb34990SDag-Erling Smørgrav int anchor_has_keytag(struct val_anchors* anchors, uint8_t* name, int namelabs, 2410fb34990SDag-Erling Smørgrav size_t namelen, uint16_t dclass, uint16_t keytag); 2420fb34990SDag-Erling Smørgrav 243103ba509SCy Schubert /** 244103ba509SCy Schubert * Find an anchor that is not an insecure point, if any, or there are no 245103ba509SCy Schubert * DNSSEC verification anchors if none. 246103ba509SCy Schubert * @param anchors: anchor storage 247103ba509SCy Schubert * @return trust anchor or NULL. It is locked. 248103ba509SCy Schubert */ 249103ba509SCy Schubert struct trust_anchor* anchors_find_any_noninsecure(struct val_anchors* anchors); 250103ba509SCy Schubert 251*be771a7bSCy Schubert /** 252*be771a7bSCy Schubert * Swap internal tree with preallocated entries. 253*be771a7bSCy Schubert * @param anchors: anchor storage. 254*be771a7bSCy Schubert * @param data: the data structure used to take elements from. This contains 255*be771a7bSCy Schubert * the old elements on return. 256*be771a7bSCy Schubert */ 257*be771a7bSCy Schubert void anchors_swap_tree(struct val_anchors* anchors, struct val_anchors* data); 258*be771a7bSCy Schubert 259b7579f77SDag-Erling Smørgrav #endif /* VALIDATOR_VAL_ANCHOR_H */ 260