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