10SN/A/*
214799Slana * validator/val_anchor.h - validator trust anchor storage.
30SN/A *
40SN/A * Copyright (c) 2007, NLnet Labs. All rights reserved.
50SN/A *
60SN/A * This software is open source.
72362SN/A *
80SN/A * Redistribution and use in source and binary forms, with or without
92362SN/A * modification, are permitted provided that the following conditions
100SN/A * are met:
110SN/A *
120SN/A * Redistributions of source code must retain the above copyright notice,
130SN/A * this list of conditions and the following disclaimer.
140SN/A *
150SN/A * Redistributions in binary form must reproduce the above copyright notice,
160SN/A * this list of conditions and the following disclaimer in the documentation
170SN/A * and/or other materials provided with the distribution.
180SN/A *
190SN/A * Neither the name of the NLNET LABS nor the names of its contributors may
200SN/A * be used to endorse or promote products derived from this software without
212362SN/A * specific prior written permission.
222362SN/A *
232362SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
240SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
250SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
260SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
270SN/A * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
280SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2914799Slana * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
300SN/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
310SN/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
320SN/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
330SN/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
340SN/A */
350SN/A
360SN/A/**
370SN/A * \file
383516SN/A *
393516SN/A * This file contains storage for the trust anchors for the validator.
403516SN/A */
413516SN/A
423516SN/A#ifndef VALIDATOR_VAL_ANCHOR_H
433516SN/A#define VALIDATOR_VAL_ANCHOR_H
443516SN/A#include "util/rbtree.h"
453516SN/A#include "util/locks.h"
463516SN/Astruct trust_anchor;
473516SN/Astruct config_file;
483516SN/Astruct ub_packed_rrset_key;
493516SN/Astruct autr_point_data;
503516SN/Astruct autr_global_data;
513516SN/Astruct sldns_buffer;
523516SN/A
533516SN/A/**
540SN/A * Trust anchor store.
550SN/A * The tree must be locked, while no other locks (from trustanchors) are held.
563516SN/A * And then an anchor searched for.  Which can be locked or deleted.  Then
573516SN/A * the tree can be unlocked again.  This means you have to release the lock
583516SN/A * on a trust anchor and look it up again to delete it.
590SN/A */
600SN/Astruct val_anchors {
615976SN/A	/** lock on trees */
623516SN/A	lock_basic_t lock;
633516SN/A	/**
643516SN/A	 * Anchors are store in this tree. Sort order is chosen, so that
653516SN/A	 * dnames are in nsec-like order. A lookup on class, name will return
663516SN/A	 * an exact match of the closest match, with the ancestor needed.
673516SN/A	 * contents of type trust_anchor.
683516SN/A	 */
693516SN/A	rbtree_t* tree;
703516SN/A	/** The DLV trust anchor (if one is configured, else NULL) */
713516SN/A	struct trust_anchor* dlv_anchor;
723516SN/A	/** Autotrust global data, anchors sorted by next probe time */
733516SN/A	struct autr_global_data* autr;
743516SN/A};
753516SN/A
763516SN/A/**
773516SN/A * Trust anchor key
783516SN/A */
790SN/Astruct ta_key {
800SN/A	/** next in list */
813516SN/A	struct ta_key* next;
820SN/A	/** rdata, in wireformat of the key RR. starts with rdlength. */
830SN/A	uint8_t* data;
843516SN/A	/** length of the rdata (including rdlength). */
853516SN/A	size_t len;
863516SN/A	/** DNS type (host format) of the key, DS or DNSKEY */
873516SN/A	uint16_t type;
883516SN/A};
893516SN/A
903516SN/A/**
913516SN/A * A trust anchor in the trust anchor store.
923516SN/A * Unique by name, class.
933516SN/A */
943516SN/Astruct trust_anchor {
953516SN/A	/** rbtree node, key is this structure */
963516SN/A	rbnode_t node;
973516SN/A	/** lock on the entire anchor and its keys; for autotrust changes */
983516SN/A	lock_basic_t lock;
993516SN/A	/** name of this trust anchor */
1003516SN/A	uint8_t* name;
1013516SN/A	/** length of name */
1023516SN/A	size_t namelen;
1033516SN/A	/** number of labels in name of rrset */
1040SN/A	int namelabs;
1050SN/A	/** the ancestor in the trustanchor tree */
1060SN/A	struct trust_anchor* parent;
1073516SN/A	/**
1083516SN/A	 * List of DS or DNSKEY rrs that form the trust anchor.
1093516SN/A	 */
1103516SN/A	struct ta_key* keylist;
1113516SN/A	/** Autotrust anchor point data, or NULL */
1123516SN/A	struct autr_point_data* autr;
1133516SN/A	/** number of DSs in the keylist */
1143516SN/A	size_t numDS;
1153516SN/A	/** number of DNSKEYs in the keylist */
1163516SN/A	size_t numDNSKEY;
1173516SN/A	/** the DS RRset */
1183516SN/A	struct ub_packed_rrset_key* ds_rrset;
1193516SN/A	/** The DNSKEY RRset */
1203516SN/A	struct ub_packed_rrset_key* dnskey_rrset;
1210SN/A	/** class of the trust anchor */
1220SN/A	uint16_t dclass;
1233516SN/A};
1243516SN/A
1253516SN/A/**
1263516SN/A * Create trust anchor storage
1273516SN/A * @return new storage or NULL on error.
1283516SN/A */
1293516SN/Astruct val_anchors* anchors_create(void);
1307491SN/A
1317491SN/A/**
1323516SN/A * Delete trust anchor storage.
1333516SN/A * @param anchors: to delete.
1343516SN/A */
1353516SN/Avoid anchors_delete(struct val_anchors* anchors);
1363516SN/A
1373516SN/A/**
1383516SN/A * Process trust anchor config.
1393516SN/A * @param anchors: struct anchor storage
1403516SN/A * @param cfg: config options.
1413516SN/A * @return 0 on error.
1423516SN/A */
1433516SN/Aint anchors_apply_cfg(struct val_anchors* anchors, struct config_file* cfg);
1443516SN/A
1453516SN/A/**
1463516SN/A * Recalculate parent pointers.  The caller must hold the lock on the
1473516SN/A * anchors structure (say after removing an item from the rbtree).
1480SN/A * Caller must not hold any locks on trust anchors.
1490SN/A * After the call is complete the parent pointers are updated and an item
1503516SN/A * just removed is no longer referenced in parent pointers.
1513516SN/A * @param anchors: the structure to update.
1520SN/A */
1530SN/Avoid anchors_init_parents_locked(struct val_anchors* anchors);
1540SN/A
1553516SN/A/**
1563516SN/A * Given a qname/qclass combination, find the trust anchor closest above it.
1570SN/A * Or return NULL if none exists.
1580SN/A *
1590SN/A * @param anchors: struct anchor storage
1600SN/A * @param qname: query name, uncompressed wireformat.
1610SN/A * @param qname_len: length of qname.
1620SN/A * @param qclass: class to query for.
1630SN/A * @return the trust anchor or NULL if none is found. The anchor is locked.
1640SN/A */
1655976SN/Astruct trust_anchor* anchors_lookup(struct val_anchors* anchors,
1660SN/A	uint8_t* qname, size_t qname_len, uint16_t qclass);
1670SN/A
1680SN/A/**
1690SN/A * Find a trust anchor. Exact matching.
1703516SN/A * @param anchors: anchor storage.
171 * @param name: name of trust anchor (wireformat)
172 * @param namelabs: labels in name
173 * @param namelen: length of name
174 * @param dclass: class of trust anchor
175 * @return NULL if not found. The anchor is locked.
176 */
177struct trust_anchor* anchor_find(struct val_anchors* anchors,
178	uint8_t* name, int namelabs, size_t namelen, uint16_t dclass);
179
180/**
181 * Store one string as trust anchor RR.
182 * @param anchors: anchor storage.
183 * @param buffer: parsing buffer, to generate the RR wireformat in.
184 * @param str: string.
185 * @return NULL on error.
186 */
187struct trust_anchor* anchor_store_str(struct val_anchors* anchors,
188	struct sldns_buffer* buffer, const char* str);
189
190/**
191 * Get memory in use by the trust anchor storage
192 * @param anchors: anchor storage.
193 * @return memory in use in bytes.
194 */
195size_t anchors_get_mem(struct val_anchors* anchors);
196
197/** compare two trust anchors */
198int anchor_cmp(const void* k1, const void* k2);
199
200/**
201 * Add insecure point trust anchor.  For external use (locks and init_parents)
202 * @param anchors: anchor storage.
203 * @param c: class.
204 * @param nm: name of insecure trust point.
205 * @return false on alloc failure.
206 */
207int anchors_add_insecure(struct val_anchors* anchors, uint16_t c, uint8_t* nm);
208
209/**
210 * Delete insecure point trust anchor.  Does not remove if no such point.
211 * For external use (locks and init_parents)
212 * @param anchors: anchor storage.
213 * @param c: class.
214 * @param nm: name of insecure trust point.
215 */
216void anchors_delete_insecure(struct val_anchors* anchors, uint16_t c,
217	uint8_t* nm);
218
219#endif /* VALIDATOR_VAL_ANCHOR_H */
220