xref: /freebsd/contrib/unbound/validator/val_sigcrypt.h (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1 /*
2  * validator/val_sigcrypt.h - validator signature crypto functions.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains helper functions for the validator module.
40  * The functions help with signature verification and checking, the
41  * bridging between RR wireformat data and crypto calls.
42  */
43 
44 #ifndef VALIDATOR_VAL_SIGCRYPT_H
45 #define VALIDATOR_VAL_SIGCRYPT_H
46 #include "util/data/packed_rrset.h"
47 #include "sldns/pkthdr.h"
48 struct val_env;
49 struct module_env;
50 struct module_qstate;
51 struct ub_packed_rrset_key;
52 struct rbtree_type;
53 struct regional;
54 struct sldns_buffer;
55 
56 /** number of entries in algorithm needs array */
57 #define ALGO_NEEDS_MAX 256
58 
59 /**
60  * Storage for algorithm needs.  DNSKEY algorithms.
61  */
62 struct algo_needs {
63 	/** the algorithms (8-bit) with each a number.
64 	 * 0: not marked.
65 	 * 1: marked 'necessary but not yet fulfilled'
66 	 * 2: marked bogus.
67 	 * Indexed by algorithm number.
68 	 */
69 	uint8_t needs[ALGO_NEEDS_MAX];
70 	/** the number of entries in the array that are unfulfilled */
71 	size_t num;
72 };
73 
74 /**
75  * Initialize algo needs structure, set algos from rrset as needed.
76  * Results are added to an existing need structure.
77  * @param n: struct with storage.
78  * @param dnskey: algos from this struct set as necessary. DNSKEY set.
79  * @param sigalg: adds to signalled algorithm list too.
80  */
81 void algo_needs_init_dnskey_add(struct algo_needs* n,
82 	struct ub_packed_rrset_key* dnskey, uint8_t* sigalg);
83 
84 /**
85  * Initialize algo needs structure from a signalled algo list.
86  * @param n: struct with storage.
87  * @param sigalg: signalled algorithm list, numbers ends with 0.
88  */
89 void algo_needs_init_list(struct algo_needs* n, uint8_t* sigalg);
90 
91 /**
92  * Initialize algo needs structure, set algos from rrset as needed.
93  * @param n: struct with storage.
94  * @param ds: algos from this struct set as necessary. DS set.
95  * @param fav_ds_algo: filter to use only this DS algo.
96  * @param sigalg: list of signalled algos, constructed as output,
97  *	provide size ALGO_NEEDS_MAX+1. list of algonumbers, ends with a zero.
98  */
99 void algo_needs_init_ds(struct algo_needs* n, struct ub_packed_rrset_key* ds,
100 	int fav_ds_algo, uint8_t* sigalg);
101 
102 /**
103  * Mark this algorithm as a success, sec_secure, and see if we are done.
104  * @param n: storage structure processed.
105  * @param algo: the algorithm processed to be secure.
106  * @return if true, processing has finished successfully, we are satisfied.
107  */
108 int algo_needs_set_secure(struct algo_needs* n, uint8_t algo);
109 
110 /**
111  * Mark this algorithm a failure, sec_bogus.  It can later be overridden
112  * by a success for this algorithm (with a different signature).
113  * @param n: storage structure processed.
114  * @param algo: the algorithm processed to be bogus.
115  */
116 void algo_needs_set_bogus(struct algo_needs* n, uint8_t algo);
117 
118 /**
119  * See how many algorithms are missing (not bogus or secure, but not processed)
120  * @param n: storage structure processed.
121  * @return number of algorithms missing after processing.
122  */
123 size_t algo_needs_num_missing(struct algo_needs* n);
124 
125 /**
126  * See which algo is missing.
127  * @param n: struct after processing.
128  * @return if 0 an algorithm was bogus, if a number, this algorithm was
129  *   missing.  So on 0, report why that was bogus, on number report a missing
130  *   algorithm.  There could be multiple missing, this reports the first one.
131  */
132 int algo_needs_missing(struct algo_needs* n);
133 
134 /**
135  * Format error reason for algorithm missing.
136  * @param env: module env with scratch for temp storage of string.
137  * @param alg: DNSKEY-algorithm missing.
138  * @param reason: destination.
139  * @param s: string, appended with 'with algorithm ..'.
140  */
141 void algo_needs_reason(struct module_env* env, int alg, char** reason, char* s);
142 
143 /**
144  * Check if dnskey matches a DS digest
145  * Does not check dnskey-keyid footprint, just the digest.
146  * @param env: module environment. Uses scratch space.
147  * @param dnskey_rrset: DNSKEY rrset.
148  * @param dnskey_idx: index of RR in rrset.
149  * @param ds_rrset: DS rrset
150  * @param ds_idx: index of RR in DS rrset.
151  * @return true if it matches, false on error, not supported or no match.
152  */
153 int ds_digest_match_dnskey(struct module_env* env,
154 	struct ub_packed_rrset_key* dnskey_rrset, size_t dnskey_idx,
155 	struct ub_packed_rrset_key* ds_rrset, size_t ds_idx);
156 
157 /**
158  * Get dnskey keytag, footprint value
159  * @param dnskey_rrset: DNSKEY rrset.
160  * @param dnskey_idx: index of RR in rrset.
161  * @return the keytag or 0 for badly formatted DNSKEYs.
162  */
163 uint16_t dnskey_calc_keytag(struct ub_packed_rrset_key* dnskey_rrset,
164 	size_t dnskey_idx);
165 
166 /**
167  * Get DS keytag, footprint value that matches the DNSKEY keytag it signs.
168  * @param ds_rrset: DS rrset
169  * @param ds_idx: index of RR in DS rrset.
170  * @return the keytag or 0 for badly formatted DSs.
171  */
172 uint16_t ds_get_keytag(struct ub_packed_rrset_key* ds_rrset, size_t ds_idx);
173 
174 /**
175  * See if DNSKEY algorithm is supported
176  * @param dnskey_rrset: DNSKEY rrset.
177  * @param dnskey_idx: index of RR in rrset.
178  * @return true if supported.
179  */
180 int dnskey_algo_is_supported(struct ub_packed_rrset_key* dnskey_rrset,
181 	size_t dnskey_idx);
182 
183 /**
184  * See if DS digest algorithm is supported
185  * @param ds_rrset: DS rrset
186  * @param ds_idx: index of RR in DS rrset.
187  * @return true if supported.
188  */
189 int ds_digest_algo_is_supported(struct ub_packed_rrset_key* ds_rrset,
190 	size_t ds_idx);
191 
192 /**
193  * Get DS RR digest algorithm
194  * @param ds_rrset: DS rrset.
195  * @param ds_idx: which DS.
196  * @return algorithm or 0 if DS too short.
197  */
198 int ds_get_digest_algo(struct ub_packed_rrset_key* ds_rrset, size_t ds_idx);
199 
200 /**
201  * See if DS key algorithm is supported
202  * @param ds_rrset: DS rrset
203  * @param ds_idx: index of RR in DS rrset.
204  * @return true if supported.
205  */
206 int ds_key_algo_is_supported(struct ub_packed_rrset_key* ds_rrset,
207 	size_t ds_idx);
208 
209 /**
210  * Get DS RR key algorithm. This value should match with the DNSKEY algo.
211  * @param k: DS rrset.
212  * @param idx: which DS.
213  * @return algorithm or 0 if DS too short.
214  */
215 int ds_get_key_algo(struct ub_packed_rrset_key* k, size_t idx);
216 
217 /**
218  * Get DNSKEY RR signature algorithm
219  * @param k: DNSKEY rrset.
220  * @param idx: which DNSKEY RR.
221  * @return algorithm or 0 if DNSKEY too short.
222  */
223 int dnskey_get_algo(struct ub_packed_rrset_key* k, size_t idx);
224 
225 /**
226  * Get DNSKEY RR flags
227  * @param k: DNSKEY rrset.
228  * @param idx: which DNSKEY RR.
229  * @return flags or 0 if DNSKEY too short.
230  */
231 uint16_t dnskey_get_flags(struct ub_packed_rrset_key* k, size_t idx);
232 
233 /**
234  * Verify rrset against dnskey rrset.
235  * @param env: module environment, scratch space is used.
236  * @param ve: validator environment, date settings.
237  * @param rrset: to be validated.
238  * @param dnskey: DNSKEY rrset, keyset to try.
239  * @param sigalg: if nonNULL provide downgrade protection otherwise one
240  *   algorithm is enough.
241  * @param reason: if bogus, a string returned, fixed or alloced in scratch.
242  * @param section: section of packet where this rrset comes from.
243  * @param qstate: qstate with region.
244  * @return SECURE if one key in the set verifies one rrsig.
245  *	UNCHECKED on allocation errors, unsupported algorithms, malformed data,
246  *	and BOGUS on verification failures (no keys match any signatures).
247  */
248 enum sec_status dnskeyset_verify_rrset(struct module_env* env,
249 	struct val_env* ve, struct ub_packed_rrset_key* rrset,
250 	struct ub_packed_rrset_key* dnskey, uint8_t* sigalg, char** reason,
251 	sldns_pkt_section section, struct module_qstate* qstate);
252 
253 /**
254  * verify rrset against one specific dnskey (from rrset)
255  * @param env: module environment, scratch space is used.
256  * @param ve: validator environment, date settings.
257  * @param rrset: to be validated.
258  * @param dnskey: DNSKEY rrset, keyset.
259  * @param dnskey_idx: which key from the rrset to try.
260  * @param reason: if bogus, a string returned, fixed or alloced in scratch.
261  * @param section: section of packet where this rrset comes from.
262  * @param qstate: qstate with region.
263  * @return secure if *this* key signs any of the signatures on rrset.
264  *	unchecked on error or and bogus on bad signature.
265  */
266 enum sec_status dnskey_verify_rrset(struct module_env* env,
267 	struct val_env* ve, struct ub_packed_rrset_key* rrset,
268 	struct ub_packed_rrset_key* dnskey, size_t dnskey_idx, char** reason,
269 	sldns_pkt_section section, struct module_qstate* qstate);
270 
271 /**
272  * verify rrset, with dnskey rrset, for a specific rrsig in rrset
273  * @param env: module environment, scratch space is used.
274  * @param ve: validator environment, date settings.
275  * @param now: current time for validation (can be overridden).
276  * @param rrset: to be validated.
277  * @param dnskey: DNSKEY rrset, keyset to try.
278  * @param sig_idx: which signature to try to validate.
279  * @param sortree: reused sorted order. Stored in region. Pass NULL at start,
280  * 	and for a new rrset.
281  * @param reason: if bogus, a string returned, fixed or alloced in scratch.
282  * @param section: section of packet where this rrset comes from.
283  * @param qstate: qstate with region.
284  * @return secure if any key signs *this* signature. bogus if no key signs it,
285  *	or unchecked on error.
286  */
287 enum sec_status dnskeyset_verify_rrset_sig(struct module_env* env,
288 	struct val_env* ve, time_t now, struct ub_packed_rrset_key* rrset,
289 	struct ub_packed_rrset_key* dnskey, size_t sig_idx,
290 	struct rbtree_type** sortree, char** reason, sldns_pkt_section section,
291 	struct module_qstate* qstate);
292 
293 /**
294  * verify rrset, with specific dnskey(from set), for a specific rrsig
295  * @param region: scratch region used for temporary allocation.
296  * @param buf: scratch buffer used for canonicalized rrset data.
297  * @param ve: validator environment, date settings.
298  * @param now: current time for validation (can be overridden).
299  * @param rrset: to be validated.
300  * @param dnskey: DNSKEY rrset, keyset.
301  * @param dnskey_idx: which key from the rrset to try.
302  * @param sig_idx: which signature to try to validate.
303  * @param sortree: pass NULL at start, the sorted rrset order is returned.
304  * 	pass it again for the same rrset.
305  * @param buf_canon: if true, the buffer is already canonical.
306  * 	pass false at start. pass old value only for same rrset and same
307  * 	signature (but perhaps different key) for reuse.
308  * @param reason: if bogus, a string returned, fixed or alloced in scratch.
309  * @param section: section of packet where this rrset comes from.
310  * @param qstate: qstate with region.
311  * @return secure if this key signs this signature. unchecked on error or
312  *	bogus if it did not validate.
313  */
314 enum sec_status dnskey_verify_rrset_sig(struct regional* region,
315 	struct sldns_buffer* buf, struct val_env* ve, time_t now,
316 	struct ub_packed_rrset_key* rrset, struct ub_packed_rrset_key* dnskey,
317 	size_t dnskey_idx, size_t sig_idx,
318 	struct rbtree_type** sortree, int* buf_canon, char** reason,
319 	sldns_pkt_section section, struct module_qstate* qstate);
320 
321 /**
322  * canonical compare for two tree entries
323  */
324 int canonical_tree_compare(const void* k1, const void* k2);
325 
326 /**
327  * Compare two rrsets and see if they are the same, canonicalised.
328  * The rrsets are not altered.
329  * @param region: temporary region.
330  * @param k1: rrset1
331  * @param k2: rrset2
332  * @return true if equal.
333  */
334 int rrset_canonical_equal(struct regional* region,
335 	struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2);
336 
337 #endif /* VALIDATOR_VAL_SIGCRYPT_H */
338