18987SN/A/*
28987SN/A * validator/val_anchor.h - validator trust anchor storage.
38987SN/A *
48987SN/A * Copyright (c) 2007, NLnet Labs. All rights reserved.
58987SN/A *
68987SN/A * This software is open source.
78987SN/A *
88987SN/A * Redistribution and use in source and binary forms, with or without
98987SN/A * modification, are permitted provided that the following conditions
108987SN/A * are met:
118987SN/A *
128987SN/A * Redistributions of source code must retain the above copyright notice,
138987SN/A * this list of conditions and the following disclaimer.
148987SN/A *
158987SN/A * Redistributions in binary form must reproduce the above copyright notice,
168987SN/A * this list of conditions and the following disclaimer in the documentation
178987SN/A * and/or other materials provided with the distribution.
188987SN/A *
198987SN/A * Neither the name of the NLNET LABS nor the names of its contributors may
208987SN/A * be used to endorse or promote products derived from this software without
218987SN/A * specific prior written permission.
228987SN/A *
238987SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
248987SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
258987SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
268987SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
278987SN/A * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
288987SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
298987SN/A * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
308987SN/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
318987SN/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
328987SN/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
338987SN/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
348987SN/A */
358987SN/A
368987SN/A/**
378987SN/A * \file
388987SN/A *
398987SN/A * This file contains storage for the trust anchors for the validator.
408987SN/A */
418987SN/A
428987SN/A#ifndef VALIDATOR_VAL_ANCHOR_H
438987SN/A#define VALIDATOR_VAL_ANCHOR_H
448987SN/A#include "util/rbtree.h"
458987SN/A#include "util/locks.h"
468987SN/Astruct trust_anchor;
478987SN/Astruct config_file;
488987SN/Astruct ub_packed_rrset_key;
498987SN/Astruct autr_point_data;
508987SN/Astruct autr_global_data;
518987SN/Astruct sldns_buffer;
528987SN/A
538987SN/A/**
548987SN/A * Trust anchor store.
558987SN/A * The tree must be locked, while no other locks (from trustanchors) are held.
568987SN/A * And then an anchor searched for.  Which can be locked or deleted.  Then
578987SN/A * the tree can be unlocked again.  This means you have to release the lock
588987SN/A * on a trust anchor and look it up again to delete it.
598987SN/A */
608987SN/Astruct val_anchors {
618987SN/A	/** lock on trees */
628987SN/A	lock_basic_type lock;
638987SN/A	/**
648987SN/A	 * Anchors are store in this tree. Sort order is chosen, so that
658987SN/A	 * dnames are in nsec-like order. A lookup on class, name will return
668987SN/A	 * an exact match of the closest match, with the ancestor needed.
678987SN/A	 * contents of type trust_anchor.
688987SN/A	 */
698987SN/A	rbtree_type* tree;
708987SN/A	/** Autotrust global data, anchors sorted by next probe time */
718987SN/A	struct autr_global_data* autr;
728987SN/A};
738987SN/A
748987SN/A/**
758987SN/A * Trust anchor key
768987SN/A */
778987SN/Astruct ta_key {
788987SN/A	/** next in list */
798987SN/A	struct ta_key* next;
808987SN/A	/** rdata, in wireformat of the key RR. starts with rdlength. */
818987SN/A	uint8_t* data;
828987SN/A	/** length of the rdata (including rdlength). */
838987SN/A	size_t len;
848987SN/A	/** DNS type (host format) of the key, DS or DNSKEY */
858987SN/A	uint16_t type;
868987SN/A};
878987SN/A
888987SN/A/**
898987SN/A * A trust anchor in the trust anchor store.
908987SN/A * Unique by name, class.
918987SN/A */
928987SN/Astruct trust_anchor {
938987SN/A	/** rbtree node, key is this structure */
948987SN/A	rbnode_type node;
958987SN/A	/** lock on the entire anchor and its keys; for autotrust changes */
968987SN/A	lock_basic_type lock;
978987SN/A	/** name of this trust anchor */
988987SN/A	uint8_t* name;
998987SN/A	/** length of name */
1008987SN/A	size_t namelen;
1018987SN/A	/** number of labels in name of rrset */
1028987SN/A	int namelabs;
1038987SN/A	/** the ancestor in the trustanchor tree */
1048987SN/A	struct trust_anchor* parent;
1058987SN/A	/**
1068987SN/A	 * List of DS or DNSKEY rrs that form the trust anchor.
1078987SN/A	 */
1088987SN/A	struct ta_key* keylist;
1098987SN/A	/** Autotrust anchor point data, or NULL */
1108987SN/A	struct autr_point_data* autr;
1118987SN/A	/** number of DSs in the keylist */
1128987SN/A	size_t numDS;
1138987SN/A	/** number of DNSKEYs in the keylist */
1148987SN/A	size_t numDNSKEY;
1158987SN/A	/** the DS RRset */
1168987SN/A	struct ub_packed_rrset_key* ds_rrset;
1178987SN/A	/** The DNSKEY RRset */
1188987SN/A	struct ub_packed_rrset_key* dnskey_rrset;
1198987SN/A	/** class of the trust anchor */
1208987SN/A	uint16_t dclass;
1218987SN/A};
1228987SN/A
1238987SN/A/**
1248987SN/A * Create trust anchor storage
1258987SN/A * @return new storage or NULL on error.
1268987SN/A */
1278987SN/Astruct val_anchors* anchors_create(void);
1288987SN/A
1298987SN/A/**
1308987SN/A * Delete trust anchor storage.
1318987SN/A * @param anchors: to delete.
1328987SN/A */
1338987SN/Avoid anchors_delete(struct val_anchors* anchors);
1348987SN/A
1358987SN/A/**
1368987SN/A * Process trust anchor config.
1378987SN/A * @param anchors: struct anchor storage
1388987SN/A * @param cfg: config options.
139 * @return 0 on error.
140 */
141int anchors_apply_cfg(struct val_anchors* anchors, struct config_file* cfg);
142
143/**
144 * Recalculate parent pointers.  The caller must hold the lock on the
145 * anchors structure (say after removing an item from the rbtree).
146 * Caller must not hold any locks on trust anchors.
147 * After the call is complete the parent pointers are updated and an item
148 * just removed is no longer referenced in parent pointers.
149 * @param anchors: the structure to update.
150 */
151void anchors_init_parents_locked(struct val_anchors* anchors);
152
153/**
154 * Given a qname/qclass combination, find the trust anchor closest above it.
155 * Or return NULL if none exists.
156 *
157 * @param anchors: struct anchor storage
158 * @param qname: query name, uncompressed wireformat.
159 * @param qname_len: length of qname.
160 * @param qclass: class to query for.
161 * @return the trust anchor or NULL if none is found. The anchor is locked.
162 */
163struct trust_anchor* anchors_lookup(struct val_anchors* anchors,
164	uint8_t* qname, size_t qname_len, uint16_t qclass);
165
166/**
167 * Find a trust anchor. Exact matching.
168 * @param anchors: anchor storage.
169 * @param name: name of trust anchor (wireformat)
170 * @param namelabs: labels in name
171 * @param namelen: length of name
172 * @param dclass: class of trust anchor
173 * @return NULL if not found. The anchor is locked.
174 */
175struct trust_anchor* anchor_find(struct val_anchors* anchors,
176	uint8_t* name, int namelabs, size_t namelen, uint16_t dclass);
177
178/**
179 * Store one string as trust anchor RR.
180 * @param anchors: anchor storage.
181 * @param buffer: parsing buffer, to generate the RR wireformat in.
182 * @param str: string.
183 * @return NULL on error.
184 */
185struct trust_anchor* anchor_store_str(struct val_anchors* anchors,
186	struct sldns_buffer* buffer, const char* str);
187
188/**
189 * Get memory in use by the trust anchor storage
190 * @param anchors: anchor storage.
191 * @return memory in use in bytes.
192 */
193size_t anchors_get_mem(struct val_anchors* anchors);
194
195/** compare two trust anchors */
196int anchor_cmp(const void* k1, const void* k2);
197
198/**
199 * Add insecure point trust anchor.  For external use (locks and init_parents)
200 * @param anchors: anchor storage.
201 * @param c: class.
202 * @param nm: name of insecure trust point.
203 * @return false on alloc failure.
204 */
205int anchors_add_insecure(struct val_anchors* anchors, uint16_t c, uint8_t* nm);
206
207/**
208 * Delete insecure point trust anchor.  Does not remove if no such point.
209 * For external use (locks and init_parents)
210 * @param anchors: anchor storage.
211 * @param c: class.
212 * @param nm: name of insecure trust point.
213 */
214void anchors_delete_insecure(struct val_anchors* anchors, uint16_t c,
215	uint8_t* nm);
216
217/**
218 * Get a list of keytags for the trust anchor.  Zero tags for insecure points.
219 * @param ta: trust anchor (locked by caller).
220 * @param list: array of uint16_t.
221 * @param num: length of array.
222 * @return number of keytags filled into array.  If total number of keytags is
223 * bigger than the array, it is truncated at num.  On errors, less keytags
224 * are filled in.  The array is sorted.
225 */
226size_t anchor_list_keytags(struct trust_anchor* ta, uint16_t* list, size_t num);
227
228/**
229 * Check if there is a trust anchor for given zone with this keytag.
230 *
231 * @param anchors: anchor storage
232 * @param name: name of trust anchor (wireformat)
233 * @param namelabs: labels in name
234 * @param namelen: length of name
235 * @param dclass: class of trust anchor
236 * @param keytag: keytag
237 * @return 1 if there is a trust anchor in the trustachor store for this zone
238 * and keytag, else 0.
239 */
240int anchor_has_keytag(struct val_anchors* anchors, uint8_t* name, int namelabs,
241	size_t namelen, uint16_t dclass, uint16_t keytag);
242
243/**
244 * Find an anchor that is not an insecure point, if any, or there are no
245 * DNSSEC verification anchors if none.
246 * @param anchors: anchor storage
247 * @return trust anchor or NULL. It is locked.
248 */
249struct trust_anchor* anchors_find_any_noninsecure(struct val_anchors* anchors);
250
251#endif /* VALIDATOR_VAL_ANCHOR_H */
252