1238106Sdes/*
2238106Sdes * validator/val_kentry.h - validator key entry definition.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, 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 * This file contains functions for dealing with validator key entries.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef VALIDATOR_VAL_KENTRY_H
43238106Sdes#define VALIDATOR_VAL_KENTRY_H
44238106Sdesstruct packed_rrset_data;
45238106Sdesstruct regional;
46238106Sdesstruct ub_packed_rrset_key;
47238106Sdes#include "util/storage/lruhash.h"
48238106Sdes
49238106Sdes/**
50238106Sdes * A key entry for the validator.
51238106Sdes * This may or may not be a trusted key.
52238106Sdes * This is what is stored in the key cache.
53238106Sdes * This is the key part for the cache; the key entry key.
54238106Sdes */
55238106Sdesstruct key_entry_key {
56238106Sdes	/** lru hash entry */
57238106Sdes	struct lruhash_entry entry;
58238106Sdes	/** name of the key */
59238106Sdes	uint8_t* name;
60238106Sdes	/** length of name */
61238106Sdes	size_t namelen;
62238106Sdes	/** class of the key, host byteorder */
63238106Sdes	uint16_t key_class;
64238106Sdes};
65238106Sdes
66238106Sdes/**
67238106Sdes * Key entry for the validator.
68238106Sdes * Contains key status.
69238106Sdes * This is the data part for the cache, the key entry data.
70238106Sdes *
71238106Sdes * Can be in three basic states:
72238106Sdes * 	isbad=0:		good key
73238106Sdes * 	isbad=1:		bad key
74238106Sdes * 	isbad=0 && rrset=0:	insecure space.
75238106Sdes */
76238106Sdesstruct key_entry_data {
77238106Sdes	/** the TTL of this entry (absolute time) */
78269257Sdes	time_t ttl;
79238106Sdes	/** the key rrdata. can be NULL to signal keyless name. */
80238106Sdes	struct packed_rrset_data* rrset_data;
81238106Sdes	/** not NULL sometimes to give reason why bogus */
82238106Sdes	char* reason;
83238106Sdes	/** list of algorithms signalled, ends with 0, or NULL */
84238106Sdes	uint8_t* algo;
85238106Sdes	/** DNS RR type of the rrset data (host order) */
86238106Sdes	uint16_t rrset_type;
87238106Sdes	/** if the key is bad: Bogus or malformed */
88238106Sdes	uint8_t isbad;
89238106Sdes};
90238106Sdes
91238106Sdes/** function for lruhash operation */
92238106Sdessize_t key_entry_sizefunc(void* key, void* data);
93238106Sdes
94238106Sdes/** function for lruhash operation */
95238106Sdesint key_entry_compfunc(void* k1, void* k2);
96238106Sdes
97238106Sdes/** function for lruhash operation */
98238106Sdesvoid key_entry_delkeyfunc(void* key, void* userarg);
99238106Sdes
100238106Sdes/** function for lruhash operation */
101238106Sdesvoid key_entry_deldatafunc(void* data, void* userarg);
102238106Sdes
103238106Sdes/** calculate hash for key entry
104238106Sdes * @param kk: key entry. The lruhash entry.hash value is filled in.
105238106Sdes */
106238106Sdesvoid key_entry_hash(struct key_entry_key* kk);
107238106Sdes
108238106Sdes/**
109238106Sdes * Copy a key entry, to be region-allocated.
110238106Sdes * @param kkey: the key entry key (and data pointer) to copy.
111238106Sdes * @param region: where to allocate it
112238106Sdes * @return newly region-allocated entry or NULL on a failure to allocate.
113238106Sdes */
114238106Sdesstruct key_entry_key* key_entry_copy_toregion(struct key_entry_key* kkey,
115238106Sdes	struct regional* region);
116238106Sdes
117238106Sdes/**
118238106Sdes * Copy a key entry, malloced.
119238106Sdes * @param kkey: the key entry key (and data pointer) to copy.
120238106Sdes * @return newly allocated entry or NULL on a failure to allocate memory.
121238106Sdes */
122238106Sdesstruct key_entry_key* key_entry_copy(struct key_entry_key* kkey);
123238106Sdes
124238106Sdes/**
125238106Sdes * See if this is a null entry. Does not do locking.
126238106Sdes * @param kkey: must have data pointer set correctly
127238106Sdes * @return true if it is a NULL rrset entry.
128238106Sdes */
129238106Sdesint key_entry_isnull(struct key_entry_key* kkey);
130238106Sdes
131238106Sdes/**
132238106Sdes * See if this entry is good. Does not do locking.
133238106Sdes * @param kkey: must have data pointer set correctly
134238106Sdes * @return true if it is good.
135238106Sdes */
136238106Sdesint key_entry_isgood(struct key_entry_key* kkey);
137238106Sdes
138238106Sdes/**
139238106Sdes * See if this entry is bad. Does not do locking.
140238106Sdes * @param kkey: must have data pointer set correctly
141238106Sdes * @return true if it is bad.
142238106Sdes */
143238106Sdesint key_entry_isbad(struct key_entry_key* kkey);
144238106Sdes
145238106Sdes/**
146238106Sdes * Set reason why a key is bad.
147238106Sdes * @param kkey: bad key.
148238106Sdes * @param reason: string to attach, you must allocate it.
149238106Sdes *    Not safe to call twice unless you deallocate it yourself.
150238106Sdes */
151238106Sdesvoid key_entry_set_reason(struct key_entry_key* kkey, char* reason);
152238106Sdes
153238106Sdes/**
154238106Sdes * Get reason why a key is bad.
155238106Sdes * @param kkey: bad key
156238106Sdes * @return pointer to string.
157238106Sdes *    String is part of key entry and is deleted with it.
158238106Sdes */
159238106Sdeschar* key_entry_get_reason(struct key_entry_key* kkey);
160238106Sdes
161238106Sdes/**
162238106Sdes * Create a null entry, in the given region.
163238106Sdes * @param region: where to allocate
164238106Sdes * @param name: the key name
165238106Sdes * @param namelen: length of name
166238106Sdes * @param dclass: class of key entry. (host order);
167238106Sdes * @param ttl: what ttl should the key have. relative.
168238106Sdes * @param now: current time (added to ttl).
169238106Sdes * @return new key entry or NULL on alloc failure
170238106Sdes */
171238106Sdesstruct key_entry_key* key_entry_create_null(struct regional* region,
172269257Sdes	uint8_t* name, size_t namelen, uint16_t dclass, time_t ttl,
173269257Sdes	time_t now);
174238106Sdes
175238106Sdes/**
176238106Sdes * Create a key entry from an rrset, in the given region.
177238106Sdes * @param region: where to allocate.
178238106Sdes * @param name: the key name
179238106Sdes * @param namelen: length of name
180238106Sdes * @param dclass: class of key entry. (host order);
181238106Sdes * @param rrset: data for key entry. This is copied to the region.
182238106Sdes * @param sigalg: signalled algorithm list (or NULL).
183238106Sdes * @param now: current time (added to ttl of rrset)
184238106Sdes * @return new key entry or NULL on alloc failure
185238106Sdes */
186238106Sdesstruct key_entry_key* key_entry_create_rrset(struct regional* region,
187238106Sdes        uint8_t* name, size_t namelen, uint16_t dclass,
188269257Sdes	struct ub_packed_rrset_key* rrset, uint8_t* sigalg, time_t now);
189238106Sdes
190238106Sdes/**
191238106Sdes * Create a bad entry, in the given region.
192238106Sdes * @param region: where to allocate
193238106Sdes * @param name: the key name
194238106Sdes * @param namelen: length of name
195238106Sdes * @param dclass: class of key entry. (host order);
196238106Sdes * @param ttl: what ttl should the key have. relative.
197238106Sdes * @param now: current time (added to ttl).
198238106Sdes * @return new key entry or NULL on alloc failure
199238106Sdes */
200238106Sdesstruct key_entry_key* key_entry_create_bad(struct regional* region,
201269257Sdes	uint8_t* name, size_t namelen, uint16_t dclass, time_t ttl,
202269257Sdes	time_t now);
203238106Sdes
204238106Sdes/**
205238106Sdes * Obtain rrset from a key entry, allocated in region.
206238106Sdes * @param kkey: key entry to convert to a rrset.
207238106Sdes * @param region: where to allocate rrset
208238106Sdes * @return rrset copy; if no rrset or alloc error returns NULL.
209238106Sdes */
210238106Sdesstruct ub_packed_rrset_key* key_entry_get_rrset(struct key_entry_key* kkey,
211238106Sdes	struct regional* region);
212238106Sdes
213238106Sdes/**
214238106Sdes * Get keysize of the keyentry.
215238106Sdes * @param kkey: key, must be a good key, with contents.
216238106Sdes * @return size in bits of the key.
217238106Sdes */
218238106Sdessize_t key_entry_keysize(struct key_entry_key* kkey);
219238106Sdes
220238106Sdes#endif /* VALIDATOR_VAL_KENTRY_H */
221