xref: /freebsd/contrib/unbound/validator/autotrust.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * validator/autotrust.c - RFC5011 trust anchor management for unbound.
3  *
4  * Copyright (c) 2009, 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  * Contains autotrust implementation. The implementation was taken from
40  * the autotrust daemon (BSD licensed), written by Matthijs Mekking.
41  * It was modified to fit into unbound. The state table process is the same.
42  */
43 #include "config.h"
44 #include "validator/autotrust.h"
45 #include "validator/val_anchor.h"
46 #include "validator/val_utils.h"
47 #include "validator/val_sigcrypt.h"
48 #include "util/data/dname.h"
49 #include "util/data/packed_rrset.h"
50 #include "util/log.h"
51 #include "util/module.h"
52 #include "util/net_help.h"
53 #include "util/config_file.h"
54 #include "util/regional.h"
55 #include "util/random.h"
56 #include "util/data/msgparse.h"
57 #include "services/mesh.h"
58 #include "services/cache/rrset.h"
59 #include "validator/val_kcache.h"
60 #include "sldns/sbuffer.h"
61 #include "sldns/wire2str.h"
62 #include "sldns/str2wire.h"
63 #include "sldns/keyraw.h"
64 #include "sldns/rrdef.h"
65 #include <stdarg.h>
66 #include <ctype.h>
67 
68 /** number of times a key must be seen before it can become valid */
69 #define MIN_PENDINGCOUNT 2
70 
71 /** Event: Revoked */
72 static void do_revoked(struct module_env* env, struct autr_ta* anchor, int* c);
73 
74 struct autr_global_data* autr_global_create(void)
75 {
76 	struct autr_global_data* global;
77 	global = (struct autr_global_data*)malloc(sizeof(*global));
78 	if(!global)
79 		return NULL;
80 	rbtree_init(&global->probe, &probetree_cmp);
81 	return global;
82 }
83 
84 void autr_global_delete(struct autr_global_data* global)
85 {
86 	if(!global)
87 		return;
88 	/* elements deleted by parent */
89 	free(global);
90 }
91 
92 int probetree_cmp(const void* x, const void* y)
93 {
94 	struct trust_anchor* a = (struct trust_anchor*)x;
95 	struct trust_anchor* b = (struct trust_anchor*)y;
96 	log_assert(a->autr && b->autr);
97 	if(a->autr->next_probe_time < b->autr->next_probe_time)
98 		return -1;
99 	if(a->autr->next_probe_time > b->autr->next_probe_time)
100 		return 1;
101 	/* time is equal, sort on trust point identity */
102 	return anchor_cmp(x, y);
103 }
104 
105 size_t
106 autr_get_num_anchors(struct val_anchors* anchors)
107 {
108 	size_t res = 0;
109 	if(!anchors)
110 		return 0;
111 	lock_basic_lock(&anchors->lock);
112 	if(anchors->autr)
113 		res = anchors->autr->probe.count;
114 	lock_basic_unlock(&anchors->lock);
115 	return res;
116 }
117 
118 /** Position in string */
119 static int
120 position_in_string(char *str, const char* sub)
121 {
122 	char* pos = strstr(str, sub);
123 	if(pos)
124 		return (int)(pos-str)+(int)strlen(sub);
125 	return -1;
126 }
127 
128 /** Debug routine to print pretty key information */
129 static void
130 verbose_key(struct autr_ta* ta, enum verbosity_value level,
131 	const char* format, ...) ATTR_FORMAT(printf, 3, 4);
132 
133 /**
134  * Implementation of debug pretty key print
135  * @param ta: trust anchor key with DNSKEY data.
136  * @param level: verbosity level to print at.
137  * @param format: printf style format string.
138  */
139 static void
140 verbose_key(struct autr_ta* ta, enum verbosity_value level,
141 	const char* format, ...)
142 {
143 	va_list args;
144 	va_start(args, format);
145 	if(verbosity >= level) {
146 		char* str = sldns_wire2str_dname(ta->rr, ta->dname_len);
147 		int keytag = (int)sldns_calc_keytag_raw(sldns_wirerr_get_rdata(
148 			ta->rr, ta->rr_len, ta->dname_len),
149 			sldns_wirerr_get_rdatalen(ta->rr, ta->rr_len,
150 			ta->dname_len));
151 		char msg[MAXSYSLOGMSGLEN];
152 		vsnprintf(msg, sizeof(msg), format, args);
153 		verbose(level, "%s key %d %s", str?str:"??", keytag, msg);
154 		free(str);
155 	}
156 	va_end(args);
157 }
158 
159 /**
160  * Parse comments
161  * @param str: to parse
162  * @param ta: trust key autotrust metadata
163  * @return false on failure.
164  */
165 static int
166 parse_comments(char* str, struct autr_ta* ta)
167 {
168         int len = (int)strlen(str), pos = 0, timestamp = 0;
169         char* comment = (char*) malloc(sizeof(char)*len+1);
170         char* comments = comment;
171 	if(!comment) {
172 		log_err("malloc failure in parse");
173                 return 0;
174 	}
175 	/* skip over whitespace and data at start of line */
176         while (*str != '\0' && *str != ';')
177                 str++;
178         if (*str == ';')
179                 str++;
180         /* copy comments */
181         while (*str != '\0')
182         {
183                 *comments = *str;
184                 comments++;
185                 str++;
186         }
187         *comments = '\0';
188 
189         comments = comment;
190 
191         /* read state */
192         pos = position_in_string(comments, "state=");
193         if (pos >= (int) strlen(comments))
194         {
195 		log_err("parse error");
196                 free(comment);
197                 return 0;
198         }
199         if (pos <= 0)
200                 ta->s = AUTR_STATE_VALID;
201         else
202         {
203                 int s = (int) comments[pos] - '0';
204                 switch(s)
205                 {
206                         case AUTR_STATE_START:
207                         case AUTR_STATE_ADDPEND:
208                         case AUTR_STATE_VALID:
209                         case AUTR_STATE_MISSING:
210                         case AUTR_STATE_REVOKED:
211                         case AUTR_STATE_REMOVED:
212                                 ta->s = s;
213                                 break;
214                         default:
215 				verbose_key(ta, VERB_OPS, "has undefined "
216 					"state, considered NewKey");
217                                 ta->s = AUTR_STATE_START;
218                                 break;
219                 }
220         }
221         /* read pending count */
222         pos = position_in_string(comments, "count=");
223         if (pos >= (int) strlen(comments))
224         {
225 		log_err("parse error");
226                 free(comment);
227                 return 0;
228         }
229         if (pos <= 0)
230                 ta->pending_count = 0;
231         else
232         {
233                 comments += pos;
234                 ta->pending_count = (uint8_t)atoi(comments);
235         }
236 
237         /* read last change */
238         pos = position_in_string(comments, "lastchange=");
239         if (pos >= (int) strlen(comments))
240         {
241 		log_err("parse error");
242                 free(comment);
243                 return 0;
244         }
245         if (pos >= 0)
246         {
247                 comments += pos;
248                 timestamp = atoi(comments);
249         }
250         if (pos < 0 || !timestamp)
251 		ta->last_change = 0;
252         else
253                 ta->last_change = (time_t)timestamp;
254 
255         free(comment);
256         return 1;
257 }
258 
259 /** Check if a line contains data (besides comments) */
260 static int
261 str_contains_data(char* str, char comment)
262 {
263         while (*str != '\0') {
264                 if (*str == comment || *str == '\n')
265                         return 0;
266                 if (*str != ' ' && *str != '\t')
267                         return 1;
268                 str++;
269         }
270         return 0;
271 }
272 
273 /** Get DNSKEY flags
274  * rdata without rdatalen in front of it. */
275 static int
276 dnskey_flags(uint16_t t, uint8_t* rdata, size_t len)
277 {
278 	uint16_t f;
279 	if(t != LDNS_RR_TYPE_DNSKEY)
280 		return 0;
281 	if(len < 2)
282 		return 0;
283 	memmove(&f, rdata, 2);
284 	f = ntohs(f);
285 	return (int)f;
286 }
287 
288 /** Check if KSK DNSKEY.
289  * pass rdata without rdatalen in front of it */
290 static int
291 rr_is_dnskey_sep(uint16_t t, uint8_t* rdata, size_t len)
292 {
293 	return (dnskey_flags(t, rdata, len)&DNSKEY_BIT_SEP);
294 }
295 
296 /** Check if TA is KSK DNSKEY */
297 static int
298 ta_is_dnskey_sep(struct autr_ta* ta)
299 {
300 	return (dnskey_flags(
301 		sldns_wirerr_get_type(ta->rr, ta->rr_len, ta->dname_len),
302 		sldns_wirerr_get_rdata(ta->rr, ta->rr_len, ta->dname_len),
303 		sldns_wirerr_get_rdatalen(ta->rr, ta->rr_len, ta->dname_len)
304 		) & DNSKEY_BIT_SEP);
305 }
306 
307 /** Check if REVOKED DNSKEY
308  * pass rdata without rdatalen in front of it */
309 static int
310 rr_is_dnskey_revoked(uint16_t t, uint8_t* rdata, size_t len)
311 {
312 	return (dnskey_flags(t, rdata, len)&LDNS_KEY_REVOKE_KEY);
313 }
314 
315 /** create ta */
316 static struct autr_ta*
317 autr_ta_create(uint8_t* rr, size_t rr_len, size_t dname_len)
318 {
319 	struct autr_ta* ta = (struct autr_ta*)calloc(1, sizeof(*ta));
320 	if(!ta) {
321 		free(rr);
322 		return NULL;
323 	}
324 	ta->rr = rr;
325 	ta->rr_len = rr_len;
326 	ta->dname_len = dname_len;
327 	return ta;
328 }
329 
330 /** create tp */
331 static struct trust_anchor*
332 autr_tp_create(struct val_anchors* anchors, uint8_t* own, size_t own_len,
333 	uint16_t dc)
334 {
335 	struct trust_anchor* tp = (struct trust_anchor*)calloc(1, sizeof(*tp));
336 	if(!tp) return NULL;
337 	tp->name = memdup(own, own_len);
338 	if(!tp->name) {
339 		free(tp);
340 		return NULL;
341 	}
342 	tp->namelen = own_len;
343 	tp->namelabs = dname_count_labels(tp->name);
344 	tp->node.key = tp;
345 	tp->dclass = dc;
346 	tp->autr = (struct autr_point_data*)calloc(1, sizeof(*tp->autr));
347 	if(!tp->autr) {
348 		free(tp->name);
349 		free(tp);
350 		return NULL;
351 	}
352 	tp->autr->pnode.key = tp;
353 
354 	lock_basic_lock(&anchors->lock);
355 	if(!rbtree_insert(anchors->tree, &tp->node)) {
356 		lock_basic_unlock(&anchors->lock);
357 		log_err("trust anchor presented twice");
358 		free(tp->name);
359 		free(tp->autr);
360 		free(tp);
361 		return NULL;
362 	}
363 	if(!rbtree_insert(&anchors->autr->probe, &tp->autr->pnode)) {
364 		(void)rbtree_delete(anchors->tree, tp);
365 		lock_basic_unlock(&anchors->lock);
366 		log_err("trust anchor in probetree twice");
367 		free(tp->name);
368 		free(tp->autr);
369 		free(tp);
370 		return NULL;
371 	}
372 	lock_basic_init(&tp->lock);
373 	lock_protect(&tp->lock, tp, sizeof(*tp));
374 	lock_protect(&tp->lock, tp->autr, sizeof(*tp->autr));
375 	lock_basic_unlock(&anchors->lock);
376 	return tp;
377 }
378 
379 /** delete assembled rrsets */
380 static void
381 autr_rrset_delete(struct ub_packed_rrset_key* r)
382 {
383 	if(r) {
384 		free(r->rk.dname);
385 		free(r->entry.data);
386 		free(r);
387 	}
388 }
389 
390 void autr_point_delete(struct trust_anchor* tp)
391 {
392 	if(!tp)
393 		return;
394 	lock_unprotect(&tp->lock, tp);
395 	lock_unprotect(&tp->lock, tp->autr);
396 	lock_basic_destroy(&tp->lock);
397 	autr_rrset_delete(tp->ds_rrset);
398 	autr_rrset_delete(tp->dnskey_rrset);
399 	if(tp->autr) {
400 		struct autr_ta* p = tp->autr->keys, *np;
401 		while(p) {
402 			np = p->next;
403 			free(p->rr);
404 			free(p);
405 			p = np;
406 		}
407 		free(tp->autr->file);
408 		free(tp->autr);
409 	}
410 	free(tp->name);
411 	free(tp);
412 }
413 
414 /** find or add a new trust point for autotrust */
415 static struct trust_anchor*
416 find_add_tp(struct val_anchors* anchors, uint8_t* rr, size_t rr_len,
417 	size_t dname_len)
418 {
419 	struct trust_anchor* tp;
420 	tp = anchor_find(anchors, rr, dname_count_labels(rr), dname_len,
421 		sldns_wirerr_get_class(rr, rr_len, dname_len));
422 	if(tp) {
423 		if(!tp->autr) {
424 			log_err("anchor cannot be with and without autotrust");
425 			lock_basic_unlock(&tp->lock);
426 			return NULL;
427 		}
428 		return tp;
429 	}
430 	tp = autr_tp_create(anchors, rr, dname_len, sldns_wirerr_get_class(rr,
431 		rr_len, dname_len));
432 	if(!tp)
433 		return NULL;
434 	lock_basic_lock(&tp->lock);
435 	return tp;
436 }
437 
438 /** Add trust anchor from RR */
439 static struct autr_ta*
440 add_trustanchor_frm_rr(struct val_anchors* anchors, uint8_t* rr, size_t rr_len,
441         size_t dname_len, struct trust_anchor** tp)
442 {
443 	struct autr_ta* ta = autr_ta_create(rr, rr_len, dname_len);
444 	if(!ta)
445 		return NULL;
446 	*tp = find_add_tp(anchors, rr, rr_len, dname_len);
447 	if(!*tp) {
448 		free(ta->rr);
449 		free(ta);
450 		return NULL;
451 	}
452 	/* add ta to tp */
453 	ta->next = (*tp)->autr->keys;
454 	(*tp)->autr->keys = ta;
455 	lock_basic_unlock(&(*tp)->lock);
456 	return ta;
457 }
458 
459 /**
460  * Add new trust anchor from a string in file.
461  * @param anchors: all anchors
462  * @param str: string with anchor and comments, if any comments.
463  * @param tp: trust point returned.
464  * @param origin: what to use for @
465  * @param origin_len: length of origin
466  * @param prev: previous rr name
467  * @param prev_len: length of prev
468  * @param skip: if true, the result is NULL, but not an error, skip it.
469  * @return new key in trust point.
470  */
471 static struct autr_ta*
472 add_trustanchor_frm_str(struct val_anchors* anchors, char* str,
473 	struct trust_anchor** tp, uint8_t* origin, size_t origin_len,
474 	uint8_t** prev, size_t* prev_len, int* skip)
475 {
476 	uint8_t rr[LDNS_RR_BUF_SIZE];
477 	size_t rr_len = sizeof(rr), dname_len;
478 	uint8_t* drr;
479 	int lstatus;
480         if (!str_contains_data(str, ';')) {
481 		*skip = 1;
482                 return NULL; /* empty line */
483 	}
484 	if(0 != (lstatus = sldns_str2wire_rr_buf(str, rr, &rr_len, &dname_len,
485 		0, origin, origin_len, *prev, *prev_len)))
486 	{
487 		log_err("ldns error while converting string to RR at%d: %s: %s",
488 			LDNS_WIREPARSE_OFFSET(lstatus),
489 			sldns_get_errorstr_parse(lstatus), str);
490 		return NULL;
491 	}
492 	free(*prev);
493 	*prev = memdup(rr, dname_len);
494 	*prev_len = dname_len;
495 	if(!*prev) {
496 		log_err("malloc failure in add_trustanchor");
497 		return NULL;
498 	}
499 	if(sldns_wirerr_get_type(rr, rr_len, dname_len)!=LDNS_RR_TYPE_DNSKEY &&
500 		sldns_wirerr_get_type(rr, rr_len, dname_len)!=LDNS_RR_TYPE_DS) {
501 		*skip = 1;
502 		return NULL; /* only DS and DNSKEY allowed */
503 	}
504 	drr = memdup(rr, rr_len);
505 	if(!drr) {
506 		log_err("malloc failure in add trustanchor");
507 		return NULL;
508 	}
509 	return add_trustanchor_frm_rr(anchors, drr, rr_len, dname_len, tp);
510 }
511 
512 /**
513  * Load single anchor
514  * @param anchors: all points.
515  * @param str: comments line
516  * @param fname: filename
517  * @param origin: the $ORIGIN.
518  * @param origin_len: length of origin
519  * @param prev: passed to ldns.
520  * @param prev_len: length of prev
521  * @param skip: if true, the result is NULL, but not an error, skip it.
522  * @return false on failure, otherwise the tp read.
523  */
524 static struct trust_anchor*
525 load_trustanchor(struct val_anchors* anchors, char* str, const char* fname,
526 	uint8_t* origin, size_t origin_len, uint8_t** prev, size_t* prev_len,
527 	int* skip)
528 {
529 	struct autr_ta* ta = NULL;
530 	struct trust_anchor* tp = NULL;
531 
532 	ta = add_trustanchor_frm_str(anchors, str, &tp, origin, origin_len,
533 		prev, prev_len, skip);
534 	if(!ta)
535 		return NULL;
536 	lock_basic_lock(&tp->lock);
537 	if(!parse_comments(str, ta)) {
538 		lock_basic_unlock(&tp->lock);
539 		return NULL;
540 	}
541 	if(!tp->autr->file) {
542 		tp->autr->file = strdup(fname);
543 		if(!tp->autr->file) {
544 			lock_basic_unlock(&tp->lock);
545 			log_err("malloc failure");
546 			return NULL;
547 		}
548 	}
549 	lock_basic_unlock(&tp->lock);
550         return tp;
551 }
552 
553 /** iterator for DSes from keylist. return true if a next element exists */
554 static int
555 assemble_iterate_ds(struct autr_ta** list, uint8_t** rr, size_t* rr_len,
556 	size_t* dname_len)
557 {
558 	while(*list) {
559 		if(sldns_wirerr_get_type((*list)->rr, (*list)->rr_len,
560 			(*list)->dname_len) == LDNS_RR_TYPE_DS) {
561 			*rr = (*list)->rr;
562 			*rr_len = (*list)->rr_len;
563 			*dname_len = (*list)->dname_len;
564 			*list = (*list)->next;
565 			return 1;
566 		}
567 		*list = (*list)->next;
568 	}
569 	return 0;
570 }
571 
572 /** iterator for DNSKEYs from keylist. return true if a next element exists */
573 static int
574 assemble_iterate_dnskey(struct autr_ta** list, uint8_t** rr, size_t* rr_len,
575 	size_t* dname_len)
576 {
577 	while(*list) {
578 		if(sldns_wirerr_get_type((*list)->rr, (*list)->rr_len,
579 		   (*list)->dname_len) != LDNS_RR_TYPE_DS &&
580 			((*list)->s == AUTR_STATE_VALID ||
581 			 (*list)->s == AUTR_STATE_MISSING)) {
582 			*rr = (*list)->rr;
583 			*rr_len = (*list)->rr_len;
584 			*dname_len = (*list)->dname_len;
585 			*list = (*list)->next;
586 			return 1;
587 		}
588 		*list = (*list)->next;
589 	}
590 	return 0;
591 }
592 
593 /** see if iterator-list has any elements in it, or it is empty */
594 static int
595 assemble_iterate_hasfirst(int iter(struct autr_ta**, uint8_t**, size_t*,
596 	size_t*), struct autr_ta* list)
597 {
598 	uint8_t* rr = NULL;
599 	size_t rr_len = 0, dname_len = 0;
600 	return iter(&list, &rr, &rr_len, &dname_len);
601 }
602 
603 /** number of elements in iterator list */
604 static size_t
605 assemble_iterate_count(int iter(struct autr_ta**, uint8_t**, size_t*,
606 	size_t*), struct autr_ta* list)
607 {
608 	uint8_t* rr = NULL;
609 	size_t i = 0, rr_len = 0, dname_len = 0;
610 	while(iter(&list, &rr, &rr_len, &dname_len)) {
611 		i++;
612 	}
613 	return i;
614 }
615 
616 /**
617  * Create a ub_packed_rrset_key allocated on the heap.
618  * It therefore does not have the correct ID value, and cannot be used
619  * inside the cache.  It can be used in storage outside of the cache.
620  * Keys for the cache have to be obtained from alloc.h .
621  * @param iter: iterator over the elements in the list.  It filters elements.
622  * @param list: the list.
623  * @return key allocated or NULL on failure.
624  */
625 static struct ub_packed_rrset_key*
626 ub_packed_rrset_heap_key(int iter(struct autr_ta**, uint8_t**, size_t*,
627 	size_t*), struct autr_ta* list)
628 {
629 	uint8_t* rr = NULL;
630 	size_t rr_len = 0, dname_len = 0;
631 	struct ub_packed_rrset_key* k;
632 	if(!iter(&list, &rr, &rr_len, &dname_len))
633 		return NULL;
634 	k = (struct ub_packed_rrset_key*)calloc(1, sizeof(*k));
635 	if(!k)
636 		return NULL;
637 	k->rk.type = htons(sldns_wirerr_get_type(rr, rr_len, dname_len));
638 	k->rk.rrset_class = htons(sldns_wirerr_get_class(rr, rr_len, dname_len));
639 	k->rk.dname_len = dname_len;
640 	k->rk.dname = memdup(rr, dname_len);
641 	if(!k->rk.dname) {
642 		free(k);
643 		return NULL;
644 	}
645 	return k;
646 }
647 
648 /**
649  * Create packed_rrset data on the heap.
650  * @param iter: iterator over the elements in the list.  It filters elements.
651  * @param list: the list.
652  * @return data allocated or NULL on failure.
653  */
654 static struct packed_rrset_data*
655 packed_rrset_heap_data(int iter(struct autr_ta**, uint8_t**, size_t*,
656 	size_t*), struct autr_ta* list)
657 {
658 	uint8_t* rr = NULL;
659 	size_t rr_len = 0, dname_len = 0;
660 	struct packed_rrset_data* data;
661 	size_t count=0, rrsig_count=0, len=0, i, total;
662 	uint8_t* nextrdata;
663 	struct autr_ta* list_i;
664 	time_t ttl = 0;
665 
666 	list_i = list;
667 	while(iter(&list_i, &rr, &rr_len, &dname_len)) {
668 		if(sldns_wirerr_get_type(rr, rr_len, dname_len) ==
669 			LDNS_RR_TYPE_RRSIG)
670 			rrsig_count++;
671 		else	count++;
672 		/* sizeof the rdlength + rdatalen */
673 		len += 2 + sldns_wirerr_get_rdatalen(rr, rr_len, dname_len);
674 		ttl = (time_t)sldns_wirerr_get_ttl(rr, rr_len, dname_len);
675 	}
676 	if(count == 0 && rrsig_count == 0)
677 		return NULL;
678 
679 	/* allocate */
680 	total = count + rrsig_count;
681 	len += sizeof(*data) + total*(sizeof(size_t) + sizeof(time_t) +
682 		sizeof(uint8_t*));
683 	data = (struct packed_rrset_data*)calloc(1, len);
684 	if(!data)
685 		return NULL;
686 
687 	/* fill it */
688 	data->ttl = ttl;
689 	data->count = count;
690 	data->rrsig_count = rrsig_count;
691 	data->rr_len = (size_t*)((uint8_t*)data +
692 		sizeof(struct packed_rrset_data));
693 	data->rr_data = (uint8_t**)&(data->rr_len[total]);
694 	data->rr_ttl = (time_t*)&(data->rr_data[total]);
695 	nextrdata = (uint8_t*)&(data->rr_ttl[total]);
696 
697 	/* fill out len, ttl, fields */
698 	list_i = list;
699 	i = 0;
700 	while(iter(&list_i, &rr, &rr_len, &dname_len)) {
701 		data->rr_ttl[i] = (time_t)sldns_wirerr_get_ttl(rr, rr_len,
702 			dname_len);
703 		if(data->rr_ttl[i] < data->ttl)
704 			data->ttl = data->rr_ttl[i];
705 		data->rr_len[i] = 2 /* the rdlength */ +
706 			sldns_wirerr_get_rdatalen(rr, rr_len, dname_len);
707 		i++;
708 	}
709 
710 	/* fixup rest of ptrs */
711 	for(i=0; i<total; i++) {
712 		data->rr_data[i] = nextrdata;
713 		nextrdata += data->rr_len[i];
714 	}
715 
716 	/* copy data in there */
717 	list_i = list;
718 	i = 0;
719 	while(iter(&list_i, &rr, &rr_len, &dname_len)) {
720 		log_assert(data->rr_data[i]);
721 		memmove(data->rr_data[i],
722 			sldns_wirerr_get_rdatawl(rr, rr_len, dname_len),
723 			data->rr_len[i]);
724 		i++;
725 	}
726 
727 	if(data->rrsig_count && data->count == 0) {
728 		data->count = data->rrsig_count; /* rrset type is RRSIG */
729 		data->rrsig_count = 0;
730 	}
731 	return data;
732 }
733 
734 /**
735  * Assemble the trust anchors into DS and DNSKEY packed rrsets.
736  * Uses only VALID and MISSING DNSKEYs.
737  * Read the sldns_rrs and builds packed rrsets
738  * @param tp: the trust point. Must be locked.
739  * @return false on malloc failure.
740  */
741 static int
742 autr_assemble(struct trust_anchor* tp)
743 {
744 	struct ub_packed_rrset_key* ubds=NULL, *ubdnskey=NULL;
745 
746 	/* make packed rrset keys - malloced with no ID number, they
747 	 * are not in the cache */
748 	/* make packed rrset data (if there is a key) */
749 	if(assemble_iterate_hasfirst(assemble_iterate_ds, tp->autr->keys)) {
750 		ubds = ub_packed_rrset_heap_key(
751 			assemble_iterate_ds, tp->autr->keys);
752 		if(!ubds)
753 			goto error_cleanup;
754 		ubds->entry.data = packed_rrset_heap_data(
755 			assemble_iterate_ds, tp->autr->keys);
756 		if(!ubds->entry.data)
757 			goto error_cleanup;
758 	}
759 
760 	/* make packed DNSKEY data */
761 	if(assemble_iterate_hasfirst(assemble_iterate_dnskey, tp->autr->keys)) {
762 		ubdnskey = ub_packed_rrset_heap_key(
763 			assemble_iterate_dnskey, tp->autr->keys);
764 		if(!ubdnskey)
765 			goto error_cleanup;
766 		ubdnskey->entry.data = packed_rrset_heap_data(
767 			assemble_iterate_dnskey, tp->autr->keys);
768 		if(!ubdnskey->entry.data) {
769 		error_cleanup:
770 			autr_rrset_delete(ubds);
771 			autr_rrset_delete(ubdnskey);
772 			return 0;
773 		}
774 	}
775 
776 	/* we have prepared the new keys so nothing can go wrong any more.
777 	 * And we are sure we cannot be left without trustanchor after
778 	 * any errors. Put in the new keys and remove old ones. */
779 
780 	/* free the old data */
781 	autr_rrset_delete(tp->ds_rrset);
782 	autr_rrset_delete(tp->dnskey_rrset);
783 
784 	/* assign the data to replace the old */
785 	tp->ds_rrset = ubds;
786 	tp->dnskey_rrset = ubdnskey;
787 	tp->numDS = assemble_iterate_count(assemble_iterate_ds,
788 		tp->autr->keys);
789 	tp->numDNSKEY = assemble_iterate_count(assemble_iterate_dnskey,
790 		tp->autr->keys);
791 	return 1;
792 }
793 
794 /** parse integer */
795 static unsigned int
796 parse_int(char* line, int* ret)
797 {
798 	char *e;
799 	unsigned int x = (unsigned int)strtol(line, &e, 10);
800 	if(line == e) {
801 		*ret = -1; /* parse error */
802 		return 0;
803 	}
804 	*ret = 1; /* matched */
805 	return x;
806 }
807 
808 /** parse id sequence for anchor */
809 static struct trust_anchor*
810 parse_id(struct val_anchors* anchors, char* line)
811 {
812 	struct trust_anchor *tp;
813 	int r;
814 	uint16_t dclass;
815 	uint8_t* dname;
816 	size_t dname_len;
817 	/* read the owner name */
818 	char* next = strchr(line, ' ');
819 	if(!next)
820 		return NULL;
821 	next[0] = 0;
822 	dname = sldns_str2wire_dname(line, &dname_len);
823 	if(!dname)
824 		return NULL;
825 
826 	/* read the class */
827 	dclass = parse_int(next+1, &r);
828 	if(r == -1) {
829 		free(dname);
830 		return NULL;
831 	}
832 
833 	/* find the trust point */
834 	tp = autr_tp_create(anchors, dname, dname_len, dclass);
835 	free(dname);
836 	return tp;
837 }
838 
839 /**
840  * Parse variable from trustanchor header
841  * @param line: to parse
842  * @param anchors: the anchor is added to this, if "id:" is seen.
843  * @param anchor: the anchor as result value or previously returned anchor
844  * 	value to read the variable lines into.
845  * @return: 0 no match, -1 failed syntax error, +1 success line read.
846  * 	+2 revoked trust anchor file.
847  */
848 static int
849 parse_var_line(char* line, struct val_anchors* anchors,
850 	struct trust_anchor** anchor)
851 {
852 	struct trust_anchor* tp = *anchor;
853 	int r = 0;
854 	if(strncmp(line, ";;id: ", 6) == 0) {
855 		*anchor = parse_id(anchors, line+6);
856 		if(!*anchor) return -1;
857 		else return 1;
858 	} else if(strncmp(line, ";;REVOKED", 9) == 0) {
859 		if(tp) {
860 			log_err("REVOKED statement must be at start of file");
861 			return -1;
862 		}
863 		return 2;
864 	} else if(strncmp(line, ";;last_queried: ", 16) == 0) {
865 		if(!tp) return -1;
866 		lock_basic_lock(&tp->lock);
867 		tp->autr->last_queried = (time_t)parse_int(line+16, &r);
868 		lock_basic_unlock(&tp->lock);
869 	} else if(strncmp(line, ";;last_success: ", 16) == 0) {
870 		if(!tp) return -1;
871 		lock_basic_lock(&tp->lock);
872 		tp->autr->last_success = (time_t)parse_int(line+16, &r);
873 		lock_basic_unlock(&tp->lock);
874 	} else if(strncmp(line, ";;next_probe_time: ", 19) == 0) {
875 		if(!tp) return -1;
876 		lock_basic_lock(&anchors->lock);
877 		lock_basic_lock(&tp->lock);
878 		(void)rbtree_delete(&anchors->autr->probe, tp);
879 		tp->autr->next_probe_time = (time_t)parse_int(line+19, &r);
880 		(void)rbtree_insert(&anchors->autr->probe, &tp->autr->pnode);
881 		lock_basic_unlock(&tp->lock);
882 		lock_basic_unlock(&anchors->lock);
883 	} else if(strncmp(line, ";;query_failed: ", 16) == 0) {
884 		if(!tp) return -1;
885 		lock_basic_lock(&tp->lock);
886 		tp->autr->query_failed = (uint8_t)parse_int(line+16, &r);
887 		lock_basic_unlock(&tp->lock);
888 	} else if(strncmp(line, ";;query_interval: ", 18) == 0) {
889 		if(!tp) return -1;
890 		lock_basic_lock(&tp->lock);
891 		tp->autr->query_interval = (time_t)parse_int(line+18, &r);
892 		lock_basic_unlock(&tp->lock);
893 	} else if(strncmp(line, ";;retry_time: ", 14) == 0) {
894 		if(!tp) return -1;
895 		lock_basic_lock(&tp->lock);
896 		tp->autr->retry_time = (time_t)parse_int(line+14, &r);
897 		lock_basic_unlock(&tp->lock);
898 	}
899 	return r;
900 }
901 
902 /** handle origin lines */
903 static int
904 handle_origin(char* line, uint8_t** origin, size_t* origin_len)
905 {
906 	size_t len = 0;
907 	while(isspace((unsigned char)*line))
908 		line++;
909 	if(strncmp(line, "$ORIGIN", 7) != 0)
910 		return 0;
911 	free(*origin);
912 	line += 7;
913 	while(isspace((unsigned char)*line))
914 		line++;
915 	*origin = sldns_str2wire_dname(line, &len);
916 	*origin_len = len;
917 	if(!*origin)
918 		log_warn("malloc failure or parse error in $ORIGIN");
919 	return 1;
920 }
921 
922 /** Read one line and put multiline RRs onto one line string */
923 static int
924 read_multiline(char* buf, size_t len, FILE* in, int* linenr)
925 {
926 	char* pos = buf;
927 	size_t left = len;
928 	int depth = 0;
929 	buf[len-1] = 0;
930 	while(left > 0 && fgets(pos, (int)left, in) != NULL) {
931 		size_t i, poslen = strlen(pos);
932 		(*linenr)++;
933 
934 		/* check what the new depth is after the line */
935 		/* this routine cannot handle braces inside quotes,
936 		   say for TXT records, but this routine only has to read keys */
937 		for(i=0; i<poslen; i++) {
938 			if(pos[i] == '(') {
939 				depth++;
940 			} else if(pos[i] == ')') {
941 				if(depth == 0) {
942 					log_err("mismatch: too many ')'");
943 					return -1;
944 				}
945 				depth--;
946 			} else if(pos[i] == ';') {
947 				break;
948 			}
949 		}
950 
951 		/* normal oneline or last line: keeps newline and comments */
952 		if(depth == 0) {
953 			return 1;
954 		}
955 
956 		/* more lines expected, snip off comments and newline */
957 		if(poslen>0)
958 			pos[poslen-1] = 0; /* strip newline */
959 		if(strchr(pos, ';'))
960 			strchr(pos, ';')[0] = 0; /* strip comments */
961 
962 		/* move to paste other lines behind this one */
963 		poslen = strlen(pos);
964 		pos += poslen;
965 		left -= poslen;
966 		/* the newline is changed into a space */
967 		if(left <= 2 /* space and eos */) {
968 			log_err("line too long");
969 			return -1;
970 		}
971 		pos[0] = ' ';
972 		pos[1] = 0;
973 		pos += 1;
974 		left -= 1;
975 	}
976 	if(depth != 0) {
977 		log_err("mismatch: too many '('");
978 		return -1;
979 	}
980 	if(pos != buf)
981 		return 1;
982 	return 0;
983 }
984 
985 int autr_read_file(struct val_anchors* anchors, const char* nm)
986 {
987         /* the file descriptor */
988         FILE* fd;
989         /* keep track of line numbers */
990         int line_nr = 0;
991         /* single line */
992         char line[10240];
993 	/* trust point being read */
994 	struct trust_anchor *tp = NULL, *tp2;
995 	int r;
996 	/* for $ORIGIN parsing */
997 	uint8_t *origin=NULL, *prev=NULL;
998 	size_t origin_len=0, prev_len=0;
999 
1000         if (!(fd = fopen(nm, "r"))) {
1001                 log_err("unable to open %s for reading: %s",
1002 			nm, strerror(errno));
1003                 return 0;
1004         }
1005         verbose(VERB_ALGO, "reading autotrust anchor file %s", nm);
1006         while ( (r=read_multiline(line, sizeof(line), fd, &line_nr)) != 0) {
1007 		if(r == -1 || (r = parse_var_line(line, anchors, &tp)) == -1) {
1008 			log_err("could not parse auto-trust-anchor-file "
1009 				"%s line %d", nm, line_nr);
1010 			fclose(fd);
1011 			free(origin);
1012 			free(prev);
1013 			return 0;
1014 		} else if(r == 1) {
1015 			continue;
1016 		} else if(r == 2) {
1017 			log_warn("trust anchor %s has been revoked", nm);
1018 			fclose(fd);
1019 			free(origin);
1020 			free(prev);
1021 			return 1;
1022 		}
1023         	if (!str_contains_data(line, ';'))
1024                 	continue; /* empty lines allowed */
1025  		if(handle_origin(line, &origin, &origin_len))
1026 			continue;
1027 		r = 0;
1028                 if(!(tp2=load_trustanchor(anchors, line, nm, origin,
1029 			origin_len, &prev, &prev_len, &r))) {
1030 			if(!r) log_err("failed to load trust anchor from %s "
1031 				"at line %i, skipping", nm, line_nr);
1032                         /* try to do the rest */
1033 			continue;
1034                 }
1035 		if(tp && tp != tp2) {
1036 			log_err("file %s has mismatching data inside: "
1037 				"the file may only contain keys for one name, "
1038 				"remove keys for other domain names", nm);
1039         		fclose(fd);
1040 			free(origin);
1041 			free(prev);
1042 			return 0;
1043 		}
1044 		tp = tp2;
1045         }
1046         fclose(fd);
1047 	free(origin);
1048 	free(prev);
1049 	if(!tp) {
1050 		log_err("failed to read %s", nm);
1051 		return 0;
1052 	}
1053 
1054 	/* now assemble the data into DNSKEY and DS packed rrsets */
1055 	lock_basic_lock(&tp->lock);
1056 	if(!autr_assemble(tp)) {
1057 		lock_basic_unlock(&tp->lock);
1058 		log_err("malloc failure assembling %s", nm);
1059 		return 0;
1060 	}
1061 	lock_basic_unlock(&tp->lock);
1062 	return 1;
1063 }
1064 
1065 /** string for a trustanchor state */
1066 static const char*
1067 trustanchor_state2str(autr_state_type s)
1068 {
1069         switch (s) {
1070                 case AUTR_STATE_START:       return "  START  ";
1071                 case AUTR_STATE_ADDPEND:     return " ADDPEND ";
1072                 case AUTR_STATE_VALID:       return "  VALID  ";
1073                 case AUTR_STATE_MISSING:     return " MISSING ";
1074                 case AUTR_STATE_REVOKED:     return " REVOKED ";
1075                 case AUTR_STATE_REMOVED:     return " REMOVED ";
1076         }
1077         return " UNKNOWN ";
1078 }
1079 
1080 /** ctime r for autotrust */
1081 static char* autr_ctime_r(time_t* t, char* s)
1082 {
1083 	ctime_r(t, s);
1084 #ifdef USE_WINSOCK
1085 	if(strlen(s) > 10 && s[7]==' ' && s[8]=='0')
1086 		s[8]=' '; /* fix error in windows ctime */
1087 #endif
1088 	return s;
1089 }
1090 
1091 /** print ID to file */
1092 static int
1093 print_id(FILE* out, char* fname, uint8_t* nm, size_t nmlen, uint16_t dclass)
1094 {
1095 	char* s = sldns_wire2str_dname(nm, nmlen);
1096 	if(!s) {
1097 		log_err("malloc failure in write to %s", fname);
1098 		return 0;
1099 	}
1100 	if(fprintf(out, ";;id: %s %d\n", s, (int)dclass) < 0) {
1101 		log_err("could not write to %s: %s", fname, strerror(errno));
1102 		free(s);
1103 		return 0;
1104 	}
1105 	free(s);
1106 	return 1;
1107 }
1108 
1109 static int
1110 autr_write_contents(FILE* out, char* fn, struct trust_anchor* tp)
1111 {
1112 	char tmi[32];
1113 	struct autr_ta* ta;
1114 	char* str;
1115 
1116 	/* write pretty header */
1117 	if(fprintf(out, "; autotrust trust anchor file\n") < 0) {
1118 		log_err("could not write to %s: %s", fn, strerror(errno));
1119 		return 0;
1120 	}
1121 	if(tp->autr->revoked) {
1122 		if(fprintf(out, ";;REVOKED\n") < 0 ||
1123 		   fprintf(out, "; The zone has all keys revoked, and is\n"
1124 			"; considered as if it has no trust anchors.\n"
1125 			"; the remainder of the file is the last probe.\n"
1126 			"; to restart the trust anchor, overwrite this file.\n"
1127 			"; with one containing valid DNSKEYs or DSes.\n") < 0) {
1128 		   log_err("could not write to %s: %s", fn, strerror(errno));
1129 		   return 0;
1130 		}
1131 	}
1132 	if(!print_id(out, fn, tp->name, tp->namelen, tp->dclass)) {
1133 		return 0;
1134 	}
1135 	if(fprintf(out, ";;last_queried: %u ;;%s",
1136 		(unsigned int)tp->autr->last_queried,
1137 		autr_ctime_r(&(tp->autr->last_queried), tmi)) < 0 ||
1138 	   fprintf(out, ";;last_success: %u ;;%s",
1139 		(unsigned int)tp->autr->last_success,
1140 		autr_ctime_r(&(tp->autr->last_success), tmi)) < 0 ||
1141 	   fprintf(out, ";;next_probe_time: %u ;;%s",
1142 		(unsigned int)tp->autr->next_probe_time,
1143 		autr_ctime_r(&(tp->autr->next_probe_time), tmi)) < 0 ||
1144 	   fprintf(out, ";;query_failed: %d\n", (int)tp->autr->query_failed)<0
1145 	   || fprintf(out, ";;query_interval: %d\n",
1146 	   (int)tp->autr->query_interval) < 0 ||
1147 	   fprintf(out, ";;retry_time: %d\n", (int)tp->autr->retry_time) < 0) {
1148 		log_err("could not write to %s: %s", fn, strerror(errno));
1149 		return 0;
1150 	}
1151 
1152 	/* write anchors */
1153 	for(ta=tp->autr->keys; ta; ta=ta->next) {
1154 		/* by default do not store START and REMOVED keys */
1155 		if(ta->s == AUTR_STATE_START)
1156 			continue;
1157 		if(ta->s == AUTR_STATE_REMOVED)
1158 			continue;
1159 		/* only store keys */
1160 		if(sldns_wirerr_get_type(ta->rr, ta->rr_len, ta->dname_len)
1161 			!= LDNS_RR_TYPE_DNSKEY)
1162 			continue;
1163 		str = sldns_wire2str_rr(ta->rr, ta->rr_len);
1164 		if(!str || !str[0]) {
1165 			free(str);
1166 			log_err("malloc failure writing %s", fn);
1167 			return 0;
1168 		}
1169 		str[strlen(str)-1] = 0; /* remove newline */
1170 		if(fprintf(out, "%s ;;state=%d [%s] ;;count=%d "
1171 			";;lastchange=%u ;;%s", str, (int)ta->s,
1172 			trustanchor_state2str(ta->s), (int)ta->pending_count,
1173 			(unsigned int)ta->last_change,
1174 			autr_ctime_r(&(ta->last_change), tmi)) < 0) {
1175 		   log_err("could not write to %s: %s", fn, strerror(errno));
1176 		   free(str);
1177 		   return 0;
1178 		}
1179 		free(str);
1180 	}
1181 	return 1;
1182 }
1183 
1184 void autr_write_file(struct module_env* env, struct trust_anchor* tp)
1185 {
1186 	FILE* out;
1187 	char* fname = tp->autr->file;
1188 #ifndef S_SPLINT_S
1189 	long long llvalue;
1190 #endif
1191 	char tempf[2048];
1192 	log_assert(tp->autr);
1193 	if(!env) {
1194 		log_err("autr_write_file: Module environment is NULL.");
1195 		return;
1196 	}
1197 	/* unique name with pid number, thread number, and struct pointer
1198 	 * (the pointer uniquifies for multiple libunbound contexts) */
1199 #ifndef S_SPLINT_S
1200 #if defined(SIZE_MAX) && defined(UINT32_MAX) && (UINT32_MAX == SIZE_MAX || INT32_MAX == SIZE_MAX)
1201 	/* avoid warning about upcast on 32bit systems */
1202 	llvalue = (unsigned long)tp;
1203 #else
1204 	llvalue = (unsigned long long)tp;
1205 #endif
1206 	snprintf(tempf, sizeof(tempf), "%s.%d-%d-" ARG_LL "x", fname, (int)getpid(),
1207 		env->worker?*(int*)env->worker:0, llvalue);
1208 #endif /* S_SPLINT_S */
1209 	verbose(VERB_ALGO, "autotrust: write to disk: %s", tempf);
1210 	out = fopen(tempf, "w");
1211 	if(!out) {
1212 		fatal_exit("could not open autotrust file for writing, %s: %s",
1213 			tempf, strerror(errno));
1214 		return;
1215 	}
1216 	if(!autr_write_contents(out, tempf, tp)) {
1217 		/* failed to write contents (completely) */
1218 		fclose(out);
1219 		unlink(tempf);
1220 		fatal_exit("could not completely write: %s", fname);
1221 		return;
1222 	}
1223 	if(fflush(out) != 0)
1224 		log_err("could not fflush(%s): %s", fname, strerror(errno));
1225 #ifdef HAVE_FSYNC
1226 	if(fsync(fileno(out)) != 0)
1227 		log_err("could not fsync(%s): %s", fname, strerror(errno));
1228 #else
1229 	FlushFileBuffers((HANDLE)_get_osfhandle(_fileno(out)));
1230 #endif
1231 	if(fclose(out) != 0) {
1232 		fatal_exit("could not complete write: %s: %s",
1233 			fname, strerror(errno));
1234 		unlink(tempf);
1235 		return;
1236 	}
1237 	/* success; overwrite actual file */
1238 	verbose(VERB_ALGO, "autotrust: replaced %s", fname);
1239 #ifdef UB_ON_WINDOWS
1240 	(void)unlink(fname); /* windows does not replace file with rename() */
1241 #endif
1242 	if(rename(tempf, fname) < 0) {
1243 		fatal_exit("rename(%s to %s): %s", tempf, fname, strerror(errno));
1244 	}
1245 }
1246 
1247 /**
1248  * Verify if dnskey works for trust point
1249  * @param env: environment (with time) for verification
1250  * @param ve: validator environment (with options) for verification.
1251  * @param tp: trust point to verify with
1252  * @param rrset: DNSKEY rrset to verify.
1253  * @param qstate: qstate with region.
1254  * @return false on failure, true if verification successful.
1255  */
1256 static int
1257 verify_dnskey(struct module_env* env, struct val_env* ve,
1258         struct trust_anchor* tp, struct ub_packed_rrset_key* rrset,
1259 	struct module_qstate* qstate)
1260 {
1261 	char* reason = NULL;
1262 	uint8_t sigalg[ALGO_NEEDS_MAX+1];
1263 	int downprot = env->cfg->harden_algo_downgrade;
1264 	enum sec_status sec = val_verify_DNSKEY_with_TA(env, ve, rrset,
1265 		tp->ds_rrset, tp->dnskey_rrset, downprot?sigalg:NULL, &reason,
1266 		NULL, qstate);
1267 	/* sigalg is ignored, it returns algorithms signalled to exist, but
1268 	 * in 5011 there are no other rrsets to check.  if downprot is
1269 	 * enabled, then it checks that the DNSKEY is signed with all
1270 	 * algorithms available in the trust store. */
1271 	verbose(VERB_ALGO, "autotrust: validate DNSKEY with anchor: %s",
1272 		sec_status_to_string(sec));
1273 	return sec == sec_status_secure;
1274 }
1275 
1276 static int32_t
1277 rrsig_get_expiry(uint8_t* d, size_t len)
1278 {
1279 	/* rrsig: 2(rdlen), 2(type) 1(alg) 1(v) 4(origttl), then 4(expi), (4)incep) */
1280 	if(len < 2+8+4)
1281 		return 0;
1282 	return sldns_read_uint32(d+2+8);
1283 }
1284 
1285 /** Find minimum expiration interval from signatures */
1286 static time_t
1287 min_expiry(struct module_env* env, struct packed_rrset_data* dd)
1288 {
1289 	size_t i;
1290 	int32_t t, r = 15 * 24 * 3600; /* 15 days max */
1291 	for(i=dd->count; i<dd->count+dd->rrsig_count; i++) {
1292 		t = rrsig_get_expiry(dd->rr_data[i], dd->rr_len[i]);
1293 		if((int32_t)t - (int32_t)*env->now > 0) {
1294 			t -= (int32_t)*env->now;
1295 			if(t < r)
1296 				r = t;
1297 		}
1298 	}
1299 	return (time_t)r;
1300 }
1301 
1302 /** Is rr self-signed revoked key */
1303 static int
1304 rr_is_selfsigned_revoked(struct module_env* env, struct val_env* ve,
1305 	struct ub_packed_rrset_key* dnskey_rrset, size_t i,
1306 	struct module_qstate* qstate)
1307 {
1308 	enum sec_status sec;
1309 	char* reason = NULL;
1310 	verbose(VERB_ALGO, "seen REVOKE flag, check self-signed, rr %d",
1311 		(int)i);
1312 	/* no algorithm downgrade protection necessary, if it is selfsigned
1313 	 * revoked it can be removed. */
1314 	sec = dnskey_verify_rrset(env, ve, dnskey_rrset, dnskey_rrset, i,
1315 		&reason, NULL, LDNS_SECTION_ANSWER, qstate);
1316 	return (sec == sec_status_secure);
1317 }
1318 
1319 /** Set fetched value */
1320 static void
1321 seen_trustanchor(struct autr_ta* ta, uint8_t seen)
1322 {
1323 	ta->fetched = seen;
1324 	if(ta->pending_count < 250) /* no numerical overflow, please */
1325 		ta->pending_count++;
1326 }
1327 
1328 /** set revoked value */
1329 static void
1330 seen_revoked_trustanchor(struct autr_ta* ta, uint8_t revoked)
1331 {
1332 	ta->revoked = revoked;
1333 }
1334 
1335 /** revoke a trust anchor */
1336 static void
1337 revoke_dnskey(struct autr_ta* ta, int off)
1338 {
1339 	uint16_t flags;
1340 	uint8_t* data;
1341 	if(sldns_wirerr_get_type(ta->rr, ta->rr_len, ta->dname_len) !=
1342 		LDNS_RR_TYPE_DNSKEY)
1343 		return;
1344 	if(sldns_wirerr_get_rdatalen(ta->rr, ta->rr_len, ta->dname_len) < 2)
1345 		return;
1346 	data = sldns_wirerr_get_rdata(ta->rr, ta->rr_len, ta->dname_len);
1347 	flags = sldns_read_uint16(data);
1348 	if (off && (flags&LDNS_KEY_REVOKE_KEY))
1349 		flags ^= LDNS_KEY_REVOKE_KEY; /* flip */
1350 	else
1351 		flags |= LDNS_KEY_REVOKE_KEY;
1352 	sldns_write_uint16(data, flags);
1353 }
1354 
1355 /** Compare two RRs skipping the REVOKED bit. Pass rdata(no len) */
1356 static int
1357 dnskey_compare_skip_revbit(uint8_t* a, size_t a_len, uint8_t* b, size_t b_len)
1358 {
1359 	size_t i;
1360 	if(a_len != b_len)
1361 		return -1;
1362 	/* compare RRs RDATA byte for byte. */
1363 	for(i = 0; i < a_len; i++)
1364 	{
1365 		uint8_t rdf1, rdf2;
1366 		rdf1 = a[i];
1367 		rdf2 = b[i];
1368 		if(i==1) {
1369 			/* this is the second part of the flags field */
1370 			rdf1 |= LDNS_KEY_REVOKE_KEY;
1371 			rdf2 |= LDNS_KEY_REVOKE_KEY;
1372 		}
1373 		if (rdf1 < rdf2)	return -1;
1374 		else if (rdf1 > rdf2)	return 1;
1375         }
1376 	return 0;
1377 }
1378 
1379 
1380 /** compare trust anchor with rdata, 0 if equal. Pass rdata(no len) */
1381 static int
1382 ta_compare(struct autr_ta* a, uint16_t t, uint8_t* b, size_t b_len)
1383 {
1384 	if(!a) return -1;
1385 	else if(!b) return -1;
1386 	else if(sldns_wirerr_get_type(a->rr, a->rr_len, a->dname_len) != t)
1387 		return (int)sldns_wirerr_get_type(a->rr, a->rr_len,
1388 			a->dname_len) - (int)t;
1389 	else if(t == LDNS_RR_TYPE_DNSKEY) {
1390 		return dnskey_compare_skip_revbit(
1391 			sldns_wirerr_get_rdata(a->rr, a->rr_len, a->dname_len),
1392 			sldns_wirerr_get_rdatalen(a->rr, a->rr_len,
1393 			a->dname_len), b, b_len);
1394 	}
1395 	else if(t == LDNS_RR_TYPE_DS) {
1396 		if(sldns_wirerr_get_rdatalen(a->rr, a->rr_len, a->dname_len) !=
1397 			b_len)
1398 			return -1;
1399 		return memcmp(sldns_wirerr_get_rdata(a->rr,
1400 			a->rr_len, a->dname_len), b, b_len);
1401 	}
1402 	return -1;
1403 }
1404 
1405 /**
1406  * Find key
1407  * @param tp: to search in
1408  * @param t: rr type of the rdata.
1409  * @param rdata: to look for  (no rdatalen in it)
1410  * @param rdata_len: length of rdata
1411  * @param result: returns NULL or the ta key looked for.
1412  * @return false on malloc failure during search. if true examine result.
1413  */
1414 static int
1415 find_key(struct trust_anchor* tp, uint16_t t, uint8_t* rdata, size_t rdata_len,
1416 	struct autr_ta** result)
1417 {
1418 	struct autr_ta* ta;
1419 	if(!tp || !rdata) {
1420 		*result = NULL;
1421 		return 0;
1422 	}
1423 	for(ta=tp->autr->keys; ta; ta=ta->next) {
1424 		if(ta_compare(ta, t, rdata, rdata_len) == 0) {
1425 			*result = ta;
1426 			return 1;
1427 		}
1428 	}
1429 	*result = NULL;
1430 	return 1;
1431 }
1432 
1433 /** add key and clone RR and tp already locked. rdata without rdlen. */
1434 static struct autr_ta*
1435 add_key(struct trust_anchor* tp, uint32_t ttl, uint8_t* rdata, size_t rdata_len)
1436 {
1437 	struct autr_ta* ta;
1438 	uint8_t* rr;
1439 	size_t rr_len, dname_len;
1440 	uint16_t rrtype = htons(LDNS_RR_TYPE_DNSKEY);
1441 	uint16_t rrclass = htons(LDNS_RR_CLASS_IN);
1442 	uint16_t rdlen = htons(rdata_len);
1443 	dname_len = tp->namelen;
1444 	ttl = htonl(ttl);
1445 	rr_len = dname_len + 10 /* type,class,ttl,rdatalen */ + rdata_len;
1446 	rr = (uint8_t*)malloc(rr_len);
1447 	if(!rr) return NULL;
1448 	memmove(rr, tp->name, tp->namelen);
1449 	memmove(rr+dname_len, &rrtype, 2);
1450 	memmove(rr+dname_len+2, &rrclass, 2);
1451 	memmove(rr+dname_len+4, &ttl, 4);
1452 	memmove(rr+dname_len+8, &rdlen, 2);
1453 	memmove(rr+dname_len+10, rdata, rdata_len);
1454 	ta = autr_ta_create(rr, rr_len, dname_len);
1455 	if(!ta) {
1456 		/* rr freed in autr_ta_create */
1457 		return NULL;
1458 	}
1459 	/* link in, tp already locked */
1460 	ta->next = tp->autr->keys;
1461 	tp->autr->keys = ta;
1462 	return ta;
1463 }
1464 
1465 /** get TTL from DNSKEY rrset */
1466 static time_t
1467 key_ttl(struct ub_packed_rrset_key* k)
1468 {
1469 	struct packed_rrset_data* d = (struct packed_rrset_data*)k->entry.data;
1470 	return d->ttl;
1471 }
1472 
1473 /** update the time values for the trustpoint */
1474 static void
1475 set_tp_times(struct trust_anchor* tp, time_t rrsig_exp_interval,
1476 	time_t origttl, int* changed)
1477 {
1478 	time_t x, qi = tp->autr->query_interval, rt = tp->autr->retry_time;
1479 
1480 	/* x = MIN(15days, ttl/2, expire/2) */
1481 	x = 15 * 24 * 3600;
1482 	if(origttl/2 < x)
1483 		x = origttl/2;
1484 	if(rrsig_exp_interval/2 < x)
1485 		x = rrsig_exp_interval/2;
1486 	/* MAX(1hr, x) */
1487 	if(!autr_permit_small_holddown) {
1488 		if(x < 3600)
1489 			tp->autr->query_interval = 3600;
1490 		else	tp->autr->query_interval = x;
1491 	}	else    tp->autr->query_interval = x;
1492 
1493 	/* x= MIN(1day, ttl/10, expire/10) */
1494 	x = 24 * 3600;
1495 	if(origttl/10 < x)
1496 		x = origttl/10;
1497 	if(rrsig_exp_interval/10 < x)
1498 		x = rrsig_exp_interval/10;
1499 	/* MAX(1hr, x) */
1500 	if(!autr_permit_small_holddown) {
1501 		if(x < 3600)
1502 			tp->autr->retry_time = 3600;
1503 		else	tp->autr->retry_time = x;
1504 	}	else    tp->autr->retry_time = x;
1505 
1506 	if(qi != tp->autr->query_interval || rt != tp->autr->retry_time) {
1507 		*changed = 1;
1508 		verbose(VERB_ALGO, "orig_ttl is %d", (int)origttl);
1509 		verbose(VERB_ALGO, "rrsig_exp_interval is %d",
1510 			(int)rrsig_exp_interval);
1511 		verbose(VERB_ALGO, "query_interval: %d, retry_time: %d",
1512 			(int)tp->autr->query_interval,
1513 			(int)tp->autr->retry_time);
1514 	}
1515 }
1516 
1517 /** init events to zero */
1518 static void
1519 init_events(struct trust_anchor* tp)
1520 {
1521 	struct autr_ta* ta;
1522 	for(ta=tp->autr->keys; ta; ta=ta->next) {
1523 		ta->fetched = 0;
1524 	}
1525 }
1526 
1527 /** check for revoked keys without trusting any other information */
1528 static void
1529 check_contains_revoked(struct module_env* env, struct val_env* ve,
1530 	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset,
1531 	int* changed, struct module_qstate* qstate)
1532 {
1533 	struct packed_rrset_data* dd = (struct packed_rrset_data*)
1534 		dnskey_rrset->entry.data;
1535 	size_t i;
1536 	log_assert(ntohs(dnskey_rrset->rk.type) == LDNS_RR_TYPE_DNSKEY);
1537 	for(i=0; i<dd->count; i++) {
1538 		struct autr_ta* ta = NULL;
1539 		if(!rr_is_dnskey_sep(ntohs(dnskey_rrset->rk.type),
1540 			dd->rr_data[i]+2, dd->rr_len[i]-2) ||
1541 			!rr_is_dnskey_revoked(ntohs(dnskey_rrset->rk.type),
1542 			dd->rr_data[i]+2, dd->rr_len[i]-2))
1543 			continue; /* not a revoked KSK */
1544 		if(!find_key(tp, ntohs(dnskey_rrset->rk.type),
1545 			dd->rr_data[i]+2, dd->rr_len[i]-2, &ta)) {
1546 			log_err("malloc failure");
1547 			continue; /* malloc fail in compare*/
1548 		}
1549 		if(!ta)
1550 			continue; /* key not found */
1551 		if(rr_is_selfsigned_revoked(env, ve, dnskey_rrset, i, qstate)) {
1552 			/* checked if there is an rrsig signed by this key. */
1553 			/* same keytag, but stored can be revoked already, so
1554 			 * compare keytags, with +0 or +128(REVOKE flag) */
1555 			log_assert(dnskey_calc_keytag(dnskey_rrset, i)-128 ==
1556 				sldns_calc_keytag_raw(sldns_wirerr_get_rdata(
1557 				ta->rr, ta->rr_len, ta->dname_len),
1558 				sldns_wirerr_get_rdatalen(ta->rr, ta->rr_len,
1559 				ta->dname_len)) ||
1560 				dnskey_calc_keytag(dnskey_rrset, i) ==
1561 				sldns_calc_keytag_raw(sldns_wirerr_get_rdata(
1562 				ta->rr, ta->rr_len, ta->dname_len),
1563 				sldns_wirerr_get_rdatalen(ta->rr, ta->rr_len,
1564 				ta->dname_len))); /* checks conversion*/
1565 			verbose_key(ta, VERB_ALGO, "is self-signed revoked");
1566 			if(!ta->revoked)
1567 				*changed = 1;
1568 			seen_revoked_trustanchor(ta, 1);
1569 			do_revoked(env, ta, changed);
1570 		}
1571 	}
1572 }
1573 
1574 /** See if a DNSKEY is verified by one of the DSes */
1575 static int
1576 key_matches_a_ds(struct module_env* env, struct val_env* ve,
1577 	struct ub_packed_rrset_key* dnskey_rrset, size_t key_idx,
1578 	struct ub_packed_rrset_key* ds_rrset)
1579 {
1580 	struct packed_rrset_data* dd = (struct packed_rrset_data*)
1581 	                ds_rrset->entry.data;
1582 	size_t ds_idx, num = dd->count;
1583 	int d = val_favorite_ds_algo(ds_rrset);
1584 	char* reason = "";
1585 	for(ds_idx=0; ds_idx<num; ds_idx++) {
1586 		if(!ds_digest_algo_is_supported(ds_rrset, ds_idx) ||
1587 			!ds_key_algo_is_supported(ds_rrset, ds_idx) ||
1588 			!dnskey_size_is_supported(dnskey_rrset, key_idx) ||
1589 			ds_get_digest_algo(ds_rrset, ds_idx) != d)
1590 			continue;
1591 		if(ds_get_key_algo(ds_rrset, ds_idx)
1592 		   != dnskey_get_algo(dnskey_rrset, key_idx)
1593 		   || dnskey_calc_keytag(dnskey_rrset, key_idx)
1594 		   != ds_get_keytag(ds_rrset, ds_idx)) {
1595 			continue;
1596 		}
1597 		if(!ds_digest_match_dnskey(env, dnskey_rrset, key_idx,
1598 			ds_rrset, ds_idx)) {
1599 			verbose(VERB_ALGO, "DS match attempt failed");
1600 			continue;
1601 		}
1602 		/* match of hash is sufficient for bootstrap of trust point */
1603 		(void)reason;
1604 		(void)ve;
1605 		return 1;
1606 		/* no need to check RRSIG, DS hash already matched with source
1607 		if(dnskey_verify_rrset(env, ve, dnskey_rrset,
1608 			dnskey_rrset, key_idx, &reason) == sec_status_secure) {
1609 			return 1;
1610 		} else {
1611 			verbose(VERB_ALGO, "DS match failed because the key "
1612 				"does not verify the keyset: %s", reason);
1613 		}
1614 		*/
1615 	}
1616 	return 0;
1617 }
1618 
1619 /** Set update events */
1620 static int
1621 update_events(struct module_env* env, struct val_env* ve,
1622 	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset,
1623 	int* changed)
1624 {
1625 	struct packed_rrset_data* dd = (struct packed_rrset_data*)
1626 		dnskey_rrset->entry.data;
1627 	size_t i;
1628 	log_assert(ntohs(dnskey_rrset->rk.type) == LDNS_RR_TYPE_DNSKEY);
1629 	init_events(tp);
1630 	for(i=0; i<dd->count; i++) {
1631 		struct autr_ta* ta = NULL;
1632 		if(!rr_is_dnskey_sep(ntohs(dnskey_rrset->rk.type),
1633 			dd->rr_data[i]+2, dd->rr_len[i]-2))
1634 			continue;
1635 		if(rr_is_dnskey_revoked(ntohs(dnskey_rrset->rk.type),
1636 			dd->rr_data[i]+2, dd->rr_len[i]-2)) {
1637 			/* self-signed revoked keys already detected before,
1638 			 * other revoked keys are not 'added' again */
1639 			continue;
1640 		}
1641 		/* is a key of this type supported?. Note rr_list and
1642 		 * packed_rrset are in the same order. */
1643 		if(!dnskey_algo_is_supported(dnskey_rrset, i) ||
1644 			!dnskey_size_is_supported(dnskey_rrset, i)) {
1645 			/* skip unknown algorithm key, it is useless to us */
1646 			log_nametypeclass(VERB_DETAIL, "trust point has "
1647 				"unsupported algorithm at",
1648 				tp->name, LDNS_RR_TYPE_DNSKEY, tp->dclass);
1649 			continue;
1650 		}
1651 
1652 		/* is it new? if revocation bit set, find the unrevoked key */
1653 		if(!find_key(tp, ntohs(dnskey_rrset->rk.type),
1654 			dd->rr_data[i]+2, dd->rr_len[i]-2, &ta)) {
1655 			return 0;
1656 		}
1657 		if(!ta) {
1658 			ta = add_key(tp, (uint32_t)dd->rr_ttl[i],
1659 				dd->rr_data[i]+2, dd->rr_len[i]-2);
1660 			*changed = 1;
1661 			/* first time seen, do we have DSes? if match: VALID */
1662 			if(ta && tp->ds_rrset && key_matches_a_ds(env, ve,
1663 				dnskey_rrset, i, tp->ds_rrset)) {
1664 				verbose_key(ta, VERB_ALGO, "verified by DS");
1665 				ta->s = AUTR_STATE_VALID;
1666 			}
1667 		}
1668 		if(!ta) {
1669 			return 0;
1670 		}
1671 		seen_trustanchor(ta, 1);
1672 		verbose_key(ta, VERB_ALGO, "in DNS response");
1673 	}
1674 	set_tp_times(tp, min_expiry(env, dd), key_ttl(dnskey_rrset), changed);
1675 	return 1;
1676 }
1677 
1678 /**
1679  * Check if the holddown time has already exceeded
1680  * setting: add-holddown: add holddown timer
1681  * setting: del-holddown: del holddown timer
1682  * @param env: environment with current time
1683  * @param ta: trust anchor to check for.
1684  * @param holddown: the timer value
1685  * @return number of seconds the holddown has passed.
1686  */
1687 static time_t
1688 check_holddown(struct module_env* env, struct autr_ta* ta,
1689 	unsigned int holddown)
1690 {
1691         time_t elapsed;
1692 	if(*env->now < ta->last_change) {
1693 		log_warn("time goes backwards. delaying key holddown");
1694 		return 0;
1695 	}
1696 	elapsed = *env->now - ta->last_change;
1697         if (elapsed > (time_t)holddown) {
1698                 return elapsed-(time_t)holddown;
1699         }
1700 	verbose_key(ta, VERB_ALGO, "holddown time " ARG_LL "d seconds to go",
1701 		(long long) ((time_t)holddown-elapsed));
1702         return 0;
1703 }
1704 
1705 
1706 /** Set last_change to now */
1707 static void
1708 reset_holddown(struct module_env* env, struct autr_ta* ta, int* changed)
1709 {
1710 	ta->last_change = *env->now;
1711 	*changed = 1;
1712 }
1713 
1714 /** Set the state for this trust anchor */
1715 static void
1716 set_trustanchor_state(struct module_env* env, struct autr_ta* ta, int* changed,
1717 	autr_state_type s)
1718 {
1719 	verbose_key(ta, VERB_ALGO, "update: %s to %s",
1720 		trustanchor_state2str(ta->s), trustanchor_state2str(s));
1721 	ta->s = s;
1722 	reset_holddown(env, ta, changed);
1723 }
1724 
1725 
1726 /** Event: NewKey */
1727 static void
1728 do_newkey(struct module_env* env, struct autr_ta* anchor, int* c)
1729 {
1730 	if (anchor->s == AUTR_STATE_START)
1731 		set_trustanchor_state(env, anchor, c, AUTR_STATE_ADDPEND);
1732 }
1733 
1734 /** Event: AddTime */
1735 static void
1736 do_addtime(struct module_env* env, struct autr_ta* anchor, int* c)
1737 {
1738 	/* This not according to RFC, this is 30 days, but the RFC demands
1739 	 * MAX(30days, TTL expire time of first DNSKEY set with this key),
1740 	 * The value may be too small if a very large TTL was used. */
1741 	time_t exceeded = check_holddown(env, anchor, env->cfg->add_holddown);
1742 	if (exceeded && anchor->s == AUTR_STATE_ADDPEND) {
1743 		verbose_key(anchor, VERB_ALGO, "add-holddown time exceeded "
1744 			ARG_LL "d seconds ago, and pending-count %d",
1745 			(long long)exceeded, anchor->pending_count);
1746 		if(anchor->pending_count >= MIN_PENDINGCOUNT) {
1747 			set_trustanchor_state(env, anchor, c, AUTR_STATE_VALID);
1748 			anchor->pending_count = 0;
1749 			return;
1750 		}
1751 		verbose_key(anchor, VERB_ALGO, "add-holddown time sanity check "
1752 			"failed (pending count: %d)", anchor->pending_count);
1753 	}
1754 }
1755 
1756 /** Event: RemTime */
1757 static void
1758 do_remtime(struct module_env* env, struct autr_ta* anchor, int* c)
1759 {
1760 	time_t exceeded = check_holddown(env, anchor, env->cfg->del_holddown);
1761 	if(exceeded && anchor->s == AUTR_STATE_REVOKED) {
1762 		verbose_key(anchor, VERB_ALGO, "del-holddown time exceeded "
1763 			ARG_LL "d seconds ago", (long long)exceeded);
1764 		set_trustanchor_state(env, anchor, c, AUTR_STATE_REMOVED);
1765 	}
1766 }
1767 
1768 /** Event: KeyRem */
1769 static void
1770 do_keyrem(struct module_env* env, struct autr_ta* anchor, int* c)
1771 {
1772 	if(anchor->s == AUTR_STATE_ADDPEND) {
1773 		set_trustanchor_state(env, anchor, c, AUTR_STATE_START);
1774 		anchor->pending_count = 0;
1775 	} else if(anchor->s == AUTR_STATE_VALID)
1776 		set_trustanchor_state(env, anchor, c, AUTR_STATE_MISSING);
1777 }
1778 
1779 /** Event: KeyPres */
1780 static void
1781 do_keypres(struct module_env* env, struct autr_ta* anchor, int* c)
1782 {
1783 	if(anchor->s == AUTR_STATE_MISSING)
1784 		set_trustanchor_state(env, anchor, c, AUTR_STATE_VALID);
1785 }
1786 
1787 /* Event: Revoked */
1788 static void
1789 do_revoked(struct module_env* env, struct autr_ta* anchor, int* c)
1790 {
1791 	if(anchor->s == AUTR_STATE_VALID || anchor->s == AUTR_STATE_MISSING) {
1792                 set_trustanchor_state(env, anchor, c, AUTR_STATE_REVOKED);
1793 		verbose_key(anchor, VERB_ALGO, "old id, prior to revocation");
1794                 revoke_dnskey(anchor, 0);
1795 		verbose_key(anchor, VERB_ALGO, "new id, after revocation");
1796 	}
1797 }
1798 
1799 /** Do statestable transition matrix for anchor */
1800 static void
1801 anchor_state_update(struct module_env* env, struct autr_ta* anchor, int* c)
1802 {
1803 	log_assert(anchor);
1804 	switch(anchor->s) {
1805 	/* START */
1806 	case AUTR_STATE_START:
1807 		/* NewKey: ADDPEND */
1808 		if (anchor->fetched)
1809 			do_newkey(env, anchor, c);
1810 		break;
1811 	/* ADDPEND */
1812 	case AUTR_STATE_ADDPEND:
1813 		/* KeyRem: START */
1814 		if (!anchor->fetched)
1815 			do_keyrem(env, anchor, c);
1816 		/* AddTime: VALID */
1817 		else	do_addtime(env, anchor, c);
1818 		break;
1819 	/* VALID */
1820 	case AUTR_STATE_VALID:
1821 		/* RevBit: REVOKED */
1822 		if (anchor->revoked)
1823 			do_revoked(env, anchor, c);
1824 		/* KeyRem: MISSING */
1825 		else if (!anchor->fetched)
1826 			do_keyrem(env, anchor, c);
1827 		else if(!anchor->last_change) {
1828 			verbose_key(anchor, VERB_ALGO, "first seen");
1829 			reset_holddown(env, anchor, c);
1830 		}
1831 		break;
1832 	/* MISSING */
1833 	case AUTR_STATE_MISSING:
1834 		/* RevBit: REVOKED */
1835 		if (anchor->revoked)
1836 			do_revoked(env, anchor, c);
1837 		/* KeyPres */
1838 		else if (anchor->fetched)
1839 			do_keypres(env, anchor, c);
1840 		break;
1841 	/* REVOKED */
1842 	case AUTR_STATE_REVOKED:
1843 		if (anchor->fetched)
1844 			reset_holddown(env, anchor, c);
1845 		/* RemTime: REMOVED */
1846 		else	do_remtime(env, anchor, c);
1847 		break;
1848 	/* REMOVED */
1849 	case AUTR_STATE_REMOVED:
1850 	default:
1851 		break;
1852 	}
1853 }
1854 
1855 /** if ZSK init then trust KSKs */
1856 static int
1857 init_zsk_to_ksk(struct module_env* env, struct trust_anchor* tp, int* changed)
1858 {
1859 	/* search for VALID ZSKs */
1860 	struct autr_ta* anchor;
1861 	int validzsk = 0;
1862 	int validksk = 0;
1863 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1864 		/* last_change test makes sure it was manually configured */
1865 		if(sldns_wirerr_get_type(anchor->rr, anchor->rr_len,
1866 			anchor->dname_len) == LDNS_RR_TYPE_DNSKEY &&
1867 			anchor->last_change == 0 &&
1868 			!ta_is_dnskey_sep(anchor) &&
1869 			anchor->s == AUTR_STATE_VALID)
1870                         validzsk++;
1871 	}
1872 	if(validzsk == 0)
1873 		return 0;
1874 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1875                 if (ta_is_dnskey_sep(anchor) &&
1876 			anchor->s == AUTR_STATE_ADDPEND) {
1877 			verbose_key(anchor, VERB_ALGO, "trust KSK from "
1878 				"ZSK(config)");
1879 			set_trustanchor_state(env, anchor, changed,
1880 				AUTR_STATE_VALID);
1881 			validksk++;
1882 		}
1883 	}
1884 	return validksk;
1885 }
1886 
1887 /** Remove missing trustanchors so the list does not grow forever */
1888 static void
1889 remove_missing_trustanchors(struct module_env* env, struct trust_anchor* tp,
1890 	int* changed)
1891 {
1892 	struct autr_ta* anchor;
1893 	time_t exceeded;
1894 	int valid = 0;
1895 	/* see if we have anchors that are valid */
1896 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1897 		/* Only do KSKs */
1898                 if (!ta_is_dnskey_sep(anchor))
1899                         continue;
1900                 if (anchor->s == AUTR_STATE_VALID)
1901                         valid++;
1902 	}
1903 	/* if there are no SEP Valid anchors, see if we started out with
1904 	 * a ZSK (last-change=0) anchor, which is VALID and there are KSKs
1905 	 * now that can be made valid.  Do this immediately because there
1906 	 * is no guarantee that the ZSKs get announced long enough.  Usually
1907 	 * this is immediately after init with a ZSK trusted, unless the domain
1908 	 * was not advertising any KSKs at all.  In which case we perfectly
1909 	 * track the zero number of KSKs. */
1910 	if(valid == 0) {
1911 		valid = init_zsk_to_ksk(env, tp, changed);
1912 		if(valid == 0)
1913 			return;
1914 	}
1915 
1916 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1917 		/* ignore ZSKs if newly added */
1918 		if(anchor->s == AUTR_STATE_START)
1919 			continue;
1920 		/* remove ZSKs if a KSK is present */
1921                 if (!ta_is_dnskey_sep(anchor)) {
1922 			if(valid > 0) {
1923 				verbose_key(anchor, VERB_ALGO, "remove ZSK "
1924 					"[%d key(s) VALID]", valid);
1925 				set_trustanchor_state(env, anchor, changed,
1926 					AUTR_STATE_REMOVED);
1927 			}
1928                         continue;
1929 		}
1930                 /* Only do MISSING keys */
1931                 if (anchor->s != AUTR_STATE_MISSING)
1932                         continue;
1933 		if(env->cfg->keep_missing == 0)
1934 			continue; /* keep forever */
1935 
1936 		exceeded = check_holddown(env, anchor, env->cfg->keep_missing);
1937 		/* If keep_missing has exceeded and we still have more than
1938 		 * one valid KSK: remove missing trust anchor */
1939                 if (exceeded && valid > 0) {
1940 			verbose_key(anchor, VERB_ALGO, "keep-missing time "
1941 				"exceeded " ARG_LL "d seconds ago, [%d key(s) VALID]",
1942 				(long long)exceeded, valid);
1943 			set_trustanchor_state(env, anchor, changed,
1944 				AUTR_STATE_REMOVED);
1945 		}
1946 	}
1947 }
1948 
1949 /** Do the statetable from RFC5011 transition matrix */
1950 static int
1951 do_statetable(struct module_env* env, struct trust_anchor* tp, int* changed)
1952 {
1953 	struct autr_ta* anchor;
1954 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1955 		/* Only do KSKs */
1956 		if(!ta_is_dnskey_sep(anchor))
1957 			continue;
1958 		anchor_state_update(env, anchor, changed);
1959 	}
1960 	remove_missing_trustanchors(env, tp, changed);
1961 	return 1;
1962 }
1963 
1964 /** See if time alone makes ADDPEND to VALID transition */
1965 static void
1966 autr_holddown_exceed(struct module_env* env, struct trust_anchor* tp, int* c)
1967 {
1968 	struct autr_ta* anchor;
1969 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1970 		if(ta_is_dnskey_sep(anchor) &&
1971 			anchor->s == AUTR_STATE_ADDPEND)
1972 			do_addtime(env, anchor, c);
1973 	}
1974 }
1975 
1976 /** cleanup key list */
1977 static void
1978 autr_cleanup_keys(struct trust_anchor* tp)
1979 {
1980 	struct autr_ta* p, **prevp;
1981 	prevp = &tp->autr->keys;
1982 	p = tp->autr->keys;
1983 	while(p) {
1984 		/* do we want to remove this key? */
1985 		if(p->s == AUTR_STATE_START || p->s == AUTR_STATE_REMOVED ||
1986 			sldns_wirerr_get_type(p->rr, p->rr_len, p->dname_len)
1987 			!= LDNS_RR_TYPE_DNSKEY) {
1988 			struct autr_ta* np = p->next;
1989 			/* remove */
1990 			free(p->rr);
1991 			free(p);
1992 			/* snip and go to next item */
1993 			*prevp = np;
1994 			p = np;
1995 			continue;
1996 		}
1997 		/* remove pending counts if no longer pending */
1998 		if(p->s != AUTR_STATE_ADDPEND)
1999 			p->pending_count = 0;
2000 		prevp = &p->next;
2001 		p = p->next;
2002 	}
2003 }
2004 
2005 /** calculate next probe time */
2006 static time_t
2007 calc_next_probe(struct module_env* env, time_t wait)
2008 {
2009 	/* make it random, 90-100% */
2010 	time_t rnd, rest;
2011 	if(!autr_permit_small_holddown) {
2012 		if(wait < 3600)
2013 			wait = 3600;
2014 	} else {
2015 		if(wait == 0) wait = 1;
2016 	}
2017 	rnd = wait/10;
2018 	rest = wait-rnd;
2019 	rnd = (time_t)ub_random_max(env->rnd, (long int)rnd);
2020 	return (time_t)(*env->now + rest + rnd);
2021 }
2022 
2023 /** what is first probe time (anchors must be locked) */
2024 static time_t
2025 wait_probe_time(struct val_anchors* anchors)
2026 {
2027 	rbnode_type* t = rbtree_first(&anchors->autr->probe);
2028 	if(t != RBTREE_NULL)
2029 		return ((struct trust_anchor*)t->key)->autr->next_probe_time;
2030 	return 0;
2031 }
2032 
2033 /** reset worker timer */
2034 static void
2035 reset_worker_timer(struct module_env* env)
2036 {
2037 	struct timeval tv;
2038 #ifndef S_SPLINT_S
2039 	time_t next = (time_t)wait_probe_time(env->anchors);
2040 	/* in case this is libunbound, no timer */
2041 	if(!env->probe_timer)
2042 		return;
2043 	if(next > *env->now)
2044 		tv.tv_sec = (time_t)(next - *env->now);
2045 	else	tv.tv_sec = 0;
2046 #endif
2047 	tv.tv_usec = 0;
2048 	comm_timer_set(env->probe_timer, &tv);
2049 	verbose(VERB_ALGO, "scheduled next probe in " ARG_LL "d sec", (long long)tv.tv_sec);
2050 }
2051 
2052 /** set next probe for trust anchor */
2053 static int
2054 set_next_probe(struct module_env* env, struct trust_anchor* tp,
2055 	struct ub_packed_rrset_key* dnskey_rrset)
2056 {
2057 	struct trust_anchor key, *tp2;
2058 	time_t mold, mnew;
2059 	/* use memory allocated in rrset for temporary name storage */
2060 	key.node.key = &key;
2061 	key.name = dnskey_rrset->rk.dname;
2062 	key.namelen = dnskey_rrset->rk.dname_len;
2063 	key.namelabs = dname_count_labels(key.name);
2064 	key.dclass = tp->dclass;
2065 	lock_basic_unlock(&tp->lock);
2066 
2067 	/* fetch tp again and lock anchors, so that we can modify the trees */
2068 	lock_basic_lock(&env->anchors->lock);
2069 	tp2 = (struct trust_anchor*)rbtree_search(env->anchors->tree, &key);
2070 	if(!tp2) {
2071 		verbose(VERB_ALGO, "trustpoint was deleted in set_next_probe");
2072 		lock_basic_unlock(&env->anchors->lock);
2073 		return 0;
2074 	}
2075 	log_assert(tp == tp2);
2076 	lock_basic_lock(&tp->lock);
2077 
2078 	/* schedule */
2079 	mold = wait_probe_time(env->anchors);
2080 	(void)rbtree_delete(&env->anchors->autr->probe, tp);
2081 	tp->autr->next_probe_time = calc_next_probe(env,
2082 		tp->autr->query_interval);
2083 	(void)rbtree_insert(&env->anchors->autr->probe, &tp->autr->pnode);
2084 	mnew = wait_probe_time(env->anchors);
2085 
2086 	lock_basic_unlock(&env->anchors->lock);
2087 	verbose(VERB_ALGO, "next probe set in %d seconds",
2088 		(int)tp->autr->next_probe_time - (int)*env->now);
2089 	if(mold != mnew) {
2090 		reset_worker_timer(env);
2091 	}
2092 	return 1;
2093 }
2094 
2095 /** Revoke and Delete a trust point */
2096 static void
2097 autr_tp_remove(struct module_env* env, struct trust_anchor* tp,
2098 	struct ub_packed_rrset_key* dnskey_rrset)
2099 {
2100 	struct trust_anchor* del_tp;
2101 	struct trust_anchor key;
2102 	struct autr_point_data pd;
2103 	time_t mold, mnew;
2104 
2105 	log_nametypeclass(VERB_OPS, "trust point was revoked",
2106 		tp->name, LDNS_RR_TYPE_DNSKEY, tp->dclass);
2107 	tp->autr->revoked = 1;
2108 
2109 	/* use space allocated for dnskey_rrset to save name of anchor */
2110 	memset(&key, 0, sizeof(key));
2111 	memset(&pd, 0, sizeof(pd));
2112 	key.autr = &pd;
2113 	key.node.key = &key;
2114 	pd.pnode.key = &key;
2115 	pd.next_probe_time = tp->autr->next_probe_time;
2116 	key.name = dnskey_rrset->rk.dname;
2117 	key.namelen = tp->namelen;
2118 	key.namelabs = tp->namelabs;
2119 	key.dclass = tp->dclass;
2120 
2121 	/* unlock */
2122 	lock_basic_unlock(&tp->lock);
2123 
2124 	/* take from tree. It could be deleted by someone else,hence (void). */
2125 	lock_basic_lock(&env->anchors->lock);
2126 	del_tp = (struct trust_anchor*)rbtree_delete(env->anchors->tree, &key);
2127 	mold = wait_probe_time(env->anchors);
2128 	(void)rbtree_delete(&env->anchors->autr->probe, &key);
2129 	mnew = wait_probe_time(env->anchors);
2130 	anchors_init_parents_locked(env->anchors);
2131 	lock_basic_unlock(&env->anchors->lock);
2132 
2133 	/* if !del_tp then the trust point is no longer present in the tree,
2134 	 * it was deleted by someone else, who will write the zonefile and
2135 	 * clean up the structure */
2136 	if(del_tp) {
2137 		/* save on disk */
2138 		del_tp->autr->next_probe_time = 0; /* no more probing for it */
2139 		autr_write_file(env, del_tp);
2140 
2141 		/* delete */
2142 		autr_point_delete(del_tp);
2143 	}
2144 	if(mold != mnew) {
2145 		reset_worker_timer(env);
2146 	}
2147 }
2148 
2149 int autr_process_prime(struct module_env* env, struct val_env* ve,
2150 	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset,
2151 	struct module_qstate* qstate)
2152 {
2153 	int changed = 0;
2154 	log_assert(tp && tp->autr);
2155 	/* autotrust update trust anchors */
2156 	/* the tp is locked, and stays locked unless it is deleted */
2157 
2158 	/* we could just catch the anchor here while another thread
2159 	 * is busy deleting it. Just unlock and let the other do its job */
2160 	if(tp->autr->revoked) {
2161 		log_nametypeclass(VERB_ALGO, "autotrust not processed, "
2162 			"trust point revoked", tp->name,
2163 			LDNS_RR_TYPE_DNSKEY, tp->dclass);
2164 		lock_basic_unlock(&tp->lock);
2165 		return 0; /* it is revoked */
2166 	}
2167 
2168 	/* query_dnskeys(): */
2169 	tp->autr->last_queried = *env->now;
2170 
2171 	log_nametypeclass(VERB_ALGO, "autotrust process for",
2172 		tp->name, LDNS_RR_TYPE_DNSKEY, tp->dclass);
2173 	/* see if time alone makes some keys valid */
2174 	autr_holddown_exceed(env, tp, &changed);
2175 	if(changed) {
2176 		verbose(VERB_ALGO, "autotrust: morekeys, reassemble");
2177 		if(!autr_assemble(tp)) {
2178 			log_err("malloc failure assembling autotrust keys");
2179 			return 1; /* unchanged */
2180 		}
2181 	}
2182 	/* did we get any data? */
2183 	if(!dnskey_rrset) {
2184 		verbose(VERB_ALGO, "autotrust: no dnskey rrset");
2185 		/* no update of query_failed, because then we would have
2186 		 * to write to disk. But we cannot because we maybe are
2187 		 * still 'initializing' with DS records, that we cannot write
2188 		 * in the full format (which only contains KSKs). */
2189 		return 1; /* trust point exists */
2190 	}
2191 	/* check for revoked keys to remove immediately */
2192 	check_contains_revoked(env, ve, tp, dnskey_rrset, &changed, qstate);
2193 	if(changed) {
2194 		verbose(VERB_ALGO, "autotrust: revokedkeys, reassemble");
2195 		if(!autr_assemble(tp)) {
2196 			log_err("malloc failure assembling autotrust keys");
2197 			return 1; /* unchanged */
2198 		}
2199 		if(!tp->ds_rrset && !tp->dnskey_rrset) {
2200 			/* no more keys, all are revoked */
2201 			/* this is a success for this probe attempt */
2202 			tp->autr->last_success = *env->now;
2203 			autr_tp_remove(env, tp, dnskey_rrset);
2204 			return 0; /* trust point removed */
2205 		}
2206 	}
2207 	/* verify the dnskey rrset and see if it is valid. */
2208 	if(!verify_dnskey(env, ve, tp, dnskey_rrset, qstate)) {
2209 		verbose(VERB_ALGO, "autotrust: dnskey did not verify.");
2210 		/* only increase failure count if this is not the first prime,
2211 		 * this means there was a previous successful probe */
2212 		if(tp->autr->last_success) {
2213 			tp->autr->query_failed += 1;
2214 			autr_write_file(env, tp);
2215 		}
2216 		return 1; /* trust point exists */
2217 	}
2218 
2219 	tp->autr->last_success = *env->now;
2220 	tp->autr->query_failed = 0;
2221 
2222 	/* Add new trust anchors to the data structure
2223 	 * - note which trust anchors are seen this probe.
2224 	 * Set trustpoint query_interval and retry_time.
2225 	 * - find minimum rrsig expiration interval
2226 	 */
2227 	if(!update_events(env, ve, tp, dnskey_rrset, &changed)) {
2228 		log_err("malloc failure in autotrust update_events. "
2229 			"trust point unchanged.");
2230 		return 1; /* trust point unchanged, so exists */
2231 	}
2232 
2233 	/* - for every SEP key do the 5011 statetable.
2234 	 * - remove missing trustanchors (if veryold and we have new anchors).
2235 	 */
2236 	if(!do_statetable(env, tp, &changed)) {
2237 		log_err("malloc failure in autotrust do_statetable. "
2238 			"trust point unchanged.");
2239 		return 1; /* trust point unchanged, so exists */
2240 	}
2241 
2242 	autr_cleanup_keys(tp);
2243 	if(!set_next_probe(env, tp, dnskey_rrset))
2244 		return 0; /* trust point does not exist */
2245 	autr_write_file(env, tp);
2246 	if(changed) {
2247 		verbose(VERB_ALGO, "autotrust: changed, reassemble");
2248 		if(!autr_assemble(tp)) {
2249 			log_err("malloc failure assembling autotrust keys");
2250 			return 1; /* unchanged */
2251 		}
2252 		if(!tp->ds_rrset && !tp->dnskey_rrset) {
2253 			/* no more keys, all are revoked */
2254 			autr_tp_remove(env, tp, dnskey_rrset);
2255 			return 0; /* trust point removed */
2256 		}
2257 	} else verbose(VERB_ALGO, "autotrust: no changes");
2258 
2259 	return 1; /* trust point exists */
2260 }
2261 
2262 /** debug print a trust anchor key */
2263 static void
2264 autr_debug_print_ta(struct autr_ta* ta)
2265 {
2266 	char buf[32];
2267 	char* str = sldns_wire2str_rr(ta->rr, ta->rr_len);
2268 	if(!str) {
2269 		log_info("out of memory in debug_print_ta");
2270 		return;
2271 	}
2272 	if(str[0]) str[strlen(str)-1]=0; /* remove newline */
2273 	(void)autr_ctime_r(&ta->last_change, buf);
2274 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2275 	log_info("[%s] %s ;;state:%d ;;pending_count:%d%s%s last:%s",
2276 		trustanchor_state2str(ta->s), str, ta->s, ta->pending_count,
2277 		ta->fetched?" fetched":"", ta->revoked?" revoked":"", buf);
2278 	free(str);
2279 }
2280 
2281 /** debug print a trust point */
2282 static void
2283 autr_debug_print_tp(struct trust_anchor* tp)
2284 {
2285 	struct autr_ta* ta;
2286 	char buf[257];
2287 	if(!tp->autr)
2288 		return;
2289 	dname_str(tp->name, buf);
2290 	log_info("trust point %s : %d", buf, (int)tp->dclass);
2291 	log_info("assembled %d DS and %d DNSKEYs",
2292 		(int)tp->numDS, (int)tp->numDNSKEY);
2293 	if(tp->ds_rrset) {
2294 		log_packed_rrset(NO_VERBOSE, "DS:", tp->ds_rrset);
2295 	}
2296 	if(tp->dnskey_rrset) {
2297 		log_packed_rrset(NO_VERBOSE, "DNSKEY:", tp->dnskey_rrset);
2298 	}
2299 	log_info("file %s", tp->autr->file);
2300 	(void)autr_ctime_r(&tp->autr->last_queried, buf);
2301 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2302 	log_info("last_queried: %u %s", (unsigned)tp->autr->last_queried, buf);
2303 	(void)autr_ctime_r(&tp->autr->last_success, buf);
2304 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2305 	log_info("last_success: %u %s", (unsigned)tp->autr->last_success, buf);
2306 	(void)autr_ctime_r(&tp->autr->next_probe_time, buf);
2307 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2308 	log_info("next_probe_time: %u %s", (unsigned)tp->autr->next_probe_time,
2309 		buf);
2310 	log_info("query_interval: %u", (unsigned)tp->autr->query_interval);
2311 	log_info("retry_time: %u", (unsigned)tp->autr->retry_time);
2312 	log_info("query_failed: %u", (unsigned)tp->autr->query_failed);
2313 
2314 	for(ta=tp->autr->keys; ta; ta=ta->next) {
2315 		autr_debug_print_ta(ta);
2316 	}
2317 }
2318 
2319 void
2320 autr_debug_print(struct val_anchors* anchors)
2321 {
2322 	struct trust_anchor* tp;
2323 	lock_basic_lock(&anchors->lock);
2324 	RBTREE_FOR(tp, struct trust_anchor*, anchors->tree) {
2325 		lock_basic_lock(&tp->lock);
2326 		autr_debug_print_tp(tp);
2327 		lock_basic_unlock(&tp->lock);
2328 	}
2329 	lock_basic_unlock(&anchors->lock);
2330 }
2331 
2332 void probe_answer_cb(void* arg, int ATTR_UNUSED(rcode),
2333 	sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(sec),
2334 	char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
2335 {
2336 	/* retry was set before the query was done,
2337 	 * re-querytime is set when query succeeded, but that may not
2338 	 * have reset this timer because the query could have been
2339 	 * handled by another thread. In that case, this callback would
2340 	 * get called after the original timeout is done.
2341 	 * By not resetting the timer, it may probe more often, but not
2342 	 * less often.
2343 	 * Unless the new lookup resulted in smaller TTLs and thus smaller
2344 	 * timeout values. In that case one old TTL could be mistakenly done.
2345 	 */
2346 	struct module_env* env = (struct module_env*)arg;
2347 	verbose(VERB_ALGO, "autotrust probe answer cb");
2348 	reset_worker_timer(env);
2349 }
2350 
2351 /** probe a trust anchor DNSKEY and unlocks tp */
2352 static void
2353 probe_anchor(struct module_env* env, struct trust_anchor* tp)
2354 {
2355 	struct query_info qinfo;
2356 	uint16_t qflags = BIT_RD;
2357 	struct edns_data edns;
2358 	sldns_buffer* buf = env->scratch_buffer;
2359 	qinfo.qname = regional_alloc_init(env->scratch, tp->name, tp->namelen);
2360 	if(!qinfo.qname) {
2361 		log_err("out of memory making 5011 probe");
2362 		return;
2363 	}
2364 	qinfo.qname_len = tp->namelen;
2365 	qinfo.qtype = LDNS_RR_TYPE_DNSKEY;
2366 	qinfo.qclass = tp->dclass;
2367 	qinfo.local_alias = NULL;
2368 	log_query_info(VERB_ALGO, "autotrust probe", &qinfo);
2369 	verbose(VERB_ALGO, "retry probe set in %d seconds",
2370 		(int)tp->autr->next_probe_time - (int)*env->now);
2371 	edns.edns_present = 1;
2372 	edns.ext_rcode = 0;
2373 	edns.edns_version = 0;
2374 	edns.bits = EDNS_DO;
2375 	edns.opt_list_in = NULL;
2376 	edns.opt_list_out = NULL;
2377 	edns.opt_list_inplace_cb_out = NULL;
2378 	edns.padding_block_size = 0;
2379 	edns.cookie_present = 0;
2380 	edns.cookie_valid = 0;
2381 	if(sldns_buffer_capacity(buf) < 65535)
2382 		edns.udp_size = (uint16_t)sldns_buffer_capacity(buf);
2383 	else	edns.udp_size = 65535;
2384 
2385 	/* can't hold the lock while mesh_run is processing */
2386 	lock_basic_unlock(&tp->lock);
2387 
2388 	/* delete the DNSKEY from rrset and key cache so an active probe
2389 	 * is done. First the rrset so another thread does not use it
2390 	 * to recreate the key entry in a race condition. */
2391 	rrset_cache_remove(env->rrset_cache, qinfo.qname, qinfo.qname_len,
2392 		qinfo.qtype, qinfo.qclass, 0);
2393 	key_cache_remove(env->key_cache, qinfo.qname, qinfo.qname_len,
2394 		qinfo.qclass);
2395 
2396 	if(!mesh_new_callback(env->mesh, &qinfo, qflags, &edns, buf, 0,
2397 		&probe_answer_cb, env, 0)) {
2398 		log_err("out of memory making 5011 probe");
2399 	}
2400 }
2401 
2402 /** fetch first to-probe trust-anchor and lock it and set retrytime */
2403 static struct trust_anchor*
2404 todo_probe(struct module_env* env, time_t* next)
2405 {
2406 	struct trust_anchor* tp;
2407 	rbnode_type* el;
2408 	/* get first one */
2409 	lock_basic_lock(&env->anchors->lock);
2410 	if( (el=rbtree_first(&env->anchors->autr->probe)) == RBTREE_NULL) {
2411 		/* in case of revoked anchors */
2412 		lock_basic_unlock(&env->anchors->lock);
2413 		/* signal that there are no anchors to probe */
2414 		*next = 0;
2415 		return NULL;
2416 	}
2417 	tp = (struct trust_anchor*)el->key;
2418 	lock_basic_lock(&tp->lock);
2419 
2420 	/* is it eligible? */
2421 	if((time_t)tp->autr->next_probe_time > *env->now) {
2422 		/* no more to probe */
2423 		*next = (time_t)tp->autr->next_probe_time - *env->now;
2424 		lock_basic_unlock(&tp->lock);
2425 		lock_basic_unlock(&env->anchors->lock);
2426 		return NULL;
2427 	}
2428 
2429 	/* reset its next probe time */
2430 	(void)rbtree_delete(&env->anchors->autr->probe, tp);
2431 	tp->autr->next_probe_time = calc_next_probe(env, tp->autr->retry_time);
2432 	(void)rbtree_insert(&env->anchors->autr->probe, &tp->autr->pnode);
2433 	lock_basic_unlock(&env->anchors->lock);
2434 
2435 	return tp;
2436 }
2437 
2438 time_t
2439 autr_probe_timer(struct module_env* env)
2440 {
2441 	struct trust_anchor* tp;
2442 	time_t next_probe = 3600;
2443 	int num = 0;
2444 	if(autr_permit_small_holddown) next_probe = 1;
2445 	verbose(VERB_ALGO, "autotrust probe timer callback");
2446 	/* while there are still anchors to probe */
2447 	while( (tp = todo_probe(env, &next_probe)) ) {
2448 		/* make a probe for this anchor */
2449 		probe_anchor(env, tp);
2450 		num++;
2451 	}
2452 	regional_free_all(env->scratch);
2453 	if(next_probe == 0)
2454 		return 0; /* no trust points to probe */
2455 	verbose(VERB_ALGO, "autotrust probe timer %d callbacks done", num);
2456 	return next_probe;
2457 }
2458