1238106Sdes/*
2238106Sdes * validator/autotrust.h - RFC5011 trust anchor management for unbound.
3238106Sdes *
4238106Sdes * Copyright (c) 2009, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * Contains autotrust definitions.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef VALIDATOR_AUTOTRUST_H
43238106Sdes#define VALIDATOR_AUTOTRUST_H
44238106Sdes#include "util/rbtree.h"
45238106Sdes#include "util/data/packed_rrset.h"
46238106Sdesstruct val_anchors;
47238106Sdesstruct trust_anchor;
48238106Sdesstruct ub_packed_rrset_key;
49238106Sdesstruct module_env;
50238106Sdesstruct val_env;
51269257Sdesstruct sldns_buffer;
52238106Sdes
53238106Sdes/** Autotrust anchor states */
54238106Sdestypedef enum {
55238106Sdes	AUTR_STATE_START   = 0,
56238106Sdes	AUTR_STATE_ADDPEND = 1,
57238106Sdes	AUTR_STATE_VALID   = 2,
58238106Sdes	AUTR_STATE_MISSING = 3,
59238106Sdes	AUTR_STATE_REVOKED = 4,
60238106Sdes	AUTR_STATE_REMOVED = 5
61238106Sdes} autr_state_t;
62238106Sdes
63238106Sdes/**
64238106Sdes * Autotrust metadata for one trust anchor key.
65238106Sdes */
66238106Sdesstruct autr_ta {
67238106Sdes	/** next key */
68238106Sdes	struct autr_ta* next;
69238106Sdes	/** the RR */
70269257Sdes	uint8_t* rr;
71269257Sdes	/** length of rr */
72269257Sdes	size_t rr_len, dname_len;
73238106Sdes	/** last update of key state (new pending count keeps date the same) */
74238106Sdes	time_t last_change;
75238106Sdes	/** 5011 state */
76238106Sdes	autr_state_t s;
77238106Sdes	/** pending count */
78238106Sdes	uint8_t pending_count;
79238106Sdes	/** fresh TA was seen */
80238106Sdes	uint8_t fetched;
81238106Sdes	/** revoked TA was seen */
82238106Sdes	uint8_t revoked;
83238106Sdes};
84238106Sdes
85238106Sdes/**
86238106Sdes * Autotrust metadata for a trust point.
87238106Sdes * This is part of the struct trust_anchor data.
88238106Sdes */
89238106Sdesstruct autr_point_data {
90238106Sdes	/** file to store the trust point in. chrootdir already applied. */
91238106Sdes	char* file;
92238106Sdes	/** rbtree node for probe sort, key is struct trust_anchor */
93238106Sdes	rbnode_t pnode;
94238106Sdes
95238106Sdes	/** the keys */
96238106Sdes	struct autr_ta* keys;
97238106Sdes
98238106Sdes	/** last queried DNSKEY set
99238106Sdes	 * Not all failures are captured in this entry.
100238106Sdes	 * If the validator did not even start (e.g. timeout or localservfail),
101238106Sdes	 * then the last_queried and query_failed values are not updated.
102238106Sdes	 */
103238106Sdes	time_t last_queried;
104238106Sdes	/** last successful DNSKEY set */
105238106Sdes	time_t last_success;
106238106Sdes	/** next probe time */
107238106Sdes	time_t next_probe_time;
108238106Sdes
109238106Sdes	/** when to query if !failed */
110269257Sdes	time_t query_interval;
111238106Sdes	/** when to retry if failed */
112269257Sdes	time_t retry_time;
113238106Sdes
114238106Sdes	/**
115238106Sdes	 * How many times did it fail. diagnostic only (has no effect).
116238106Sdes	 * Only updated if there was a dnskey rrset that failed to verify.
117238106Sdes	 */
118238106Sdes	uint8_t query_failed;
119238106Sdes	/** true if the trust point has been revoked */
120238106Sdes	uint8_t revoked;
121238106Sdes};
122238106Sdes
123238106Sdes/**
124238106Sdes * Autotrust global metadata.
125238106Sdes */
126238106Sdesstruct autr_global_data {
127238106Sdes	/** rbtree of autotrust anchors sorted by next probe time.
128238106Sdes	 * When time is equal, sorted by anchor class, name. */
129238106Sdes	rbtree_t probe;
130238106Sdes};
131238106Sdes
132238106Sdes/**
133238106Sdes * Create new global 5011 data structure.
134238106Sdes * @return new structure or NULL on malloc failure.
135238106Sdes */
136238106Sdesstruct autr_global_data* autr_global_create(void);
137238106Sdes
138238106Sdes/**
139238106Sdes * Delete global 5011 data structure.
140238106Sdes * @param global: global autotrust state to delete.
141238106Sdes */
142238106Sdesvoid autr_global_delete(struct autr_global_data* global);
143238106Sdes
144238106Sdes/**
145238106Sdes * See if autotrust anchors are configured and how many.
146238106Sdes * @param anchors: the trust anchors structure.
147238106Sdes * @return number of autotrust trust anchors
148238106Sdes */
149238106Sdessize_t autr_get_num_anchors(struct val_anchors* anchors);
150238106Sdes
151238106Sdes/**
152238106Sdes * Process probe timer.  Add new probes if needed.
153238106Sdes * @param env: module environment with time, with anchors and with the mesh.
154238106Sdes * @return time of next probe (in seconds from now).
155238106Sdes * 	If 0, then there is no next probe anymore (trust points deleted).
156238106Sdes */
157269257Sdestime_t autr_probe_timer(struct module_env* env);
158238106Sdes
159238106Sdes/** probe tree compare function */
160238106Sdesint probetree_cmp(const void* x, const void* y);
161238106Sdes
162238106Sdes/**
163238106Sdes * Read autotrust file.
164238106Sdes * @param anchors: the anchors structure.
165238106Sdes * @param nm: name of the file (copied).
166238106Sdes * @return false on failure.
167238106Sdes */
168238106Sdesint autr_read_file(struct val_anchors* anchors, const char* nm);
169238106Sdes
170238106Sdes/**
171238106Sdes * Write autotrust file.
172238106Sdes * @param env: environment with scratch space.
173238106Sdes * @param tp: trust point to write.
174238106Sdes */
175238106Sdesvoid autr_write_file(struct module_env* env, struct trust_anchor* tp);
176238106Sdes
177238106Sdes/**
178238106Sdes * Delete autr anchor, deletes the autr data but does not do
179238106Sdes * unlinking from trees, caller does that.
180238106Sdes * @param tp: trust point to delete.
181238106Sdes */
182238106Sdesvoid autr_point_delete(struct trust_anchor* tp);
183238106Sdes
184238106Sdes/**
185238106Sdes * Perform autotrust processing.
186238106Sdes * @param env: qstate environment with the anchors structure.
187238106Sdes * @param ve: validator environment for verification of rrsigs.
188238106Sdes * @param tp: trust anchor to process.
189238106Sdes * @param dnskey_rrset: DNSKEY rrset probed (can be NULL if bad prime result).
190238106Sdes * 	allocated in a region. Has not been validated yet.
191238106Sdes * @return false if trust anchor was revoked completely.
192238106Sdes * 	Otherwise logs errors to log, does not change return value.
193238106Sdes * 	On errors, likely the trust point has been unchanged.
194238106Sdes */
195238106Sdesint autr_process_prime(struct module_env* env, struct val_env* ve,
196238106Sdes	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset);
197238106Sdes
198238106Sdes/**
199238106Sdes * Debug printout of rfc5011 tracked anchors
200238106Sdes * @param anchors: all the anchors.
201238106Sdes */
202238106Sdesvoid autr_debug_print(struct val_anchors* anchors);
203238106Sdes
204238106Sdes/** callback for query answer to 5011 probe */
205269257Sdesvoid probe_answer_cb(void* arg, int rcode, struct sldns_buffer* buf,
206238106Sdes	enum sec_status sec, char* errinf);
207238106Sdes
208238106Sdes#endif /* VALIDATOR_AUTOTRUST_H */
209