xref: /openbsd-src/sbin/unwind/libunbound/util/edns.c (revision 7037e34cdfd270b3989fb1829c7cd3439048bd3a)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * util/edns.c - handle base EDNS options.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2018, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file contains functions for base EDNS options.
40ae8c6e27Sflorian  */
41ae8c6e27Sflorian 
42ae8c6e27Sflorian #include "config.h"
437a211805Sflorian #include "util/edns.h"
44e97c6e54Ssthen #include "util/config_file.h"
45ae8c6e27Sflorian #include "util/netevent.h"
46f4f0f0ceSflorian #include "util/net_help.h"
47ae8c6e27Sflorian #include "util/regional.h"
48d500c338Sflorian #include "util/rfc_1982.h"
49d500c338Sflorian #include "util/siphash.h"
50ae8c6e27Sflorian #include "util/data/msgparse.h"
51ae8c6e27Sflorian #include "util/data/msgreply.h"
52d500c338Sflorian #include "sldns/sbuffer.h"
53ae8c6e27Sflorian 
54853e076fSflorian struct edns_strings* edns_strings_create(void)
55f4f0f0ceSflorian {
56853e076fSflorian 	struct edns_strings* edns_strings = calloc(1,
57853e076fSflorian 		sizeof(struct edns_strings));
58853e076fSflorian 	if(!edns_strings)
59f4f0f0ceSflorian 		return NULL;
60853e076fSflorian 	if(!(edns_strings->region = regional_create())) {
61853e076fSflorian 		edns_strings_delete(edns_strings);
62f4f0f0ceSflorian 		return NULL;
63f4f0f0ceSflorian 	}
64853e076fSflorian 	return edns_strings;
65f4f0f0ceSflorian }
66f4f0f0ceSflorian 
67853e076fSflorian void edns_strings_delete(struct edns_strings* edns_strings)
68f4f0f0ceSflorian {
69853e076fSflorian 	if(!edns_strings)
70f4f0f0ceSflorian 		return;
71853e076fSflorian 	regional_destroy(edns_strings->region);
72853e076fSflorian 	free(edns_strings);
73f4f0f0ceSflorian }
74f4f0f0ceSflorian 
75f4f0f0ceSflorian static int
76853e076fSflorian edns_strings_client_insert(struct edns_strings* edns_strings,
77f4f0f0ceSflorian 	struct sockaddr_storage* addr, socklen_t addrlen, int net,
78853e076fSflorian 	const char* string)
79f4f0f0ceSflorian {
80853e076fSflorian 	struct edns_string_addr* esa = regional_alloc_zero(edns_strings->region,
81853e076fSflorian 		sizeof(struct edns_string_addr));
82853e076fSflorian 	if(!esa)
83f4f0f0ceSflorian 		return 0;
84853e076fSflorian 	esa->string_len = strlen(string);
85853e076fSflorian 	esa->string = regional_alloc_init(edns_strings->region, string,
86853e076fSflorian 		esa->string_len);
87853e076fSflorian 	if(!esa->string)
88853e076fSflorian 		return 0;
89853e076fSflorian 	if(!addr_tree_insert(&edns_strings->client_strings, &esa->node, addr,
90853e076fSflorian 		addrlen, net)) {
91853e076fSflorian 		verbose(VERB_QUERY, "duplicate EDNS client string ignored.");
92f4f0f0ceSflorian 	}
93f4f0f0ceSflorian 	return 1;
94f4f0f0ceSflorian }
95f4f0f0ceSflorian 
96853e076fSflorian int edns_strings_apply_cfg(struct edns_strings* edns_strings,
97f4f0f0ceSflorian 	struct config_file* config)
98f4f0f0ceSflorian {
99f4f0f0ceSflorian 	struct config_str2list* c;
100853e076fSflorian 	regional_free_all(edns_strings->region);
101853e076fSflorian 	addr_tree_init(&edns_strings->client_strings);
102f4f0f0ceSflorian 
103853e076fSflorian 	for(c=config->edns_client_strings; c; c=c->next) {
104f4f0f0ceSflorian 		struct sockaddr_storage addr;
105f4f0f0ceSflorian 		socklen_t addrlen;
106f4f0f0ceSflorian 		int net;
107f4f0f0ceSflorian 		log_assert(c->str && c->str2);
108f4f0f0ceSflorian 
109f4f0f0ceSflorian 		if(!netblockstrtoaddr(c->str, UNBOUND_DNS_PORT, &addr, &addrlen,
110f4f0f0ceSflorian 			&net)) {
111853e076fSflorian 			log_err("cannot parse EDNS client string IP netblock: "
112853e076fSflorian 				"%s", c->str);
113f4f0f0ceSflorian 			return 0;
114f4f0f0ceSflorian 		}
115853e076fSflorian 		if(!edns_strings_client_insert(edns_strings, &addr, addrlen,
116853e076fSflorian 			net, c->str2)) {
117853e076fSflorian 			log_err("out of memory while adding EDNS strings");
118f4f0f0ceSflorian 			return 0;
119f4f0f0ceSflorian 		}
120f4f0f0ceSflorian 	}
121853e076fSflorian 	edns_strings->client_string_opcode = config->edns_client_string_opcode;
122f4f0f0ceSflorian 
123853e076fSflorian 	addr_tree_init_parents(&edns_strings->client_strings);
124f4f0f0ceSflorian 	return 1;
125f4f0f0ceSflorian }
126f4f0f0ceSflorian 
127853e076fSflorian struct edns_string_addr*
128853e076fSflorian edns_string_addr_lookup(rbtree_type* tree, struct sockaddr_storage* addr,
129f4f0f0ceSflorian 	socklen_t addrlen)
130f4f0f0ceSflorian {
131853e076fSflorian 	return (struct edns_string_addr*)addr_tree_lookup(tree, addr, addrlen);
132f4f0f0ceSflorian }
133f4f0f0ceSflorian 
134d500c338Sflorian uint8_t*
135d500c338Sflorian edns_cookie_server_hash(const uint8_t* in, const uint8_t* secret, int v4,
136d500c338Sflorian 	uint8_t* hash)
137d500c338Sflorian {
138d500c338Sflorian 	v4?siphash(in, 20, secret, hash, 8):siphash(in, 32, secret, hash, 8);
139d500c338Sflorian 	return hash;
140d500c338Sflorian }
141d500c338Sflorian 
142d500c338Sflorian void
143d500c338Sflorian edns_cookie_server_write(uint8_t* buf, const uint8_t* secret, int v4,
144d500c338Sflorian 	uint32_t timestamp)
145d500c338Sflorian {
146d500c338Sflorian 	uint8_t hash[8];
147d500c338Sflorian 	buf[ 8] = 1;   /* Version */
148d500c338Sflorian 	buf[ 9] = 0;   /* Reserved */
149d500c338Sflorian 	buf[10] = 0;   /* Reserved */
150d500c338Sflorian 	buf[11] = 0;   /* Reserved */
151d500c338Sflorian 	sldns_write_uint32(buf + 12, timestamp);
152d500c338Sflorian 	(void)edns_cookie_server_hash(buf, secret, v4, hash);
153d500c338Sflorian 	memcpy(buf + 16, hash, 8);
154d500c338Sflorian }
155d500c338Sflorian 
156d500c338Sflorian enum edns_cookie_val_status
157d500c338Sflorian edns_cookie_server_validate(const uint8_t* cookie, size_t cookie_len,
158d500c338Sflorian 	const uint8_t* secret, size_t secret_len, int v4,
159d500c338Sflorian 	const uint8_t* hash_input, uint32_t now)
160d500c338Sflorian {
161d500c338Sflorian 	uint8_t hash[8];
162d500c338Sflorian 	uint32_t timestamp;
163d500c338Sflorian 	uint32_t subt_1982 = 0; /* Initialize for the compiler; unused value */
164d500c338Sflorian 	int comp_1982;
165d500c338Sflorian 	if(cookie_len != 24)
166d500c338Sflorian 		/* RFC9018 cookies are 24 bytes long */
167d500c338Sflorian 		return COOKIE_STATUS_CLIENT_ONLY;
168d500c338Sflorian 	if(secret_len != 16 ||  /* RFC9018 cookies have 16 byte secrets */
169d500c338Sflorian 		cookie[8] != 1) /* RFC9018 cookies are cookie version 1 */
170d500c338Sflorian 		return COOKIE_STATUS_INVALID;
171d500c338Sflorian 	timestamp = sldns_read_uint32(cookie + 12);
172d500c338Sflorian 	if((comp_1982 = compare_1982(now, timestamp)) > 0
173d500c338Sflorian 		&& (subt_1982 = subtract_1982(timestamp, now)) > 3600)
174d500c338Sflorian 		/* Cookie is older than 1 hour (see RFC9018 Section 4.3.) */
175d500c338Sflorian 		return COOKIE_STATUS_EXPIRED;
176d500c338Sflorian 	if(comp_1982 <= 0 && subtract_1982(now, timestamp) > 300)
177d500c338Sflorian 		/* Cookie time is more than 5 minutes in the future.
178d500c338Sflorian 		 * (see RFC9018 Section 4.3.) */
179d500c338Sflorian 		return COOKIE_STATUS_FUTURE;
180d500c338Sflorian 	if(memcmp(edns_cookie_server_hash(hash_input, secret, v4, hash),
181d500c338Sflorian 		cookie + 16, 8) != 0)
182d500c338Sflorian 		/* Hashes do not match */
183d500c338Sflorian 		return COOKIE_STATUS_INVALID;
184d500c338Sflorian 	if(comp_1982 > 0 && subt_1982 > 1800)
185d500c338Sflorian 		/* Valid cookie but older than 30 minutes, so create a new one
186d500c338Sflorian 		 * anyway */
187d500c338Sflorian 		return COOKIE_STATUS_VALID_RENEW;
188d500c338Sflorian 	return COOKIE_STATUS_VALID;
189d500c338Sflorian }
190*7037e34cSflorian 
191*7037e34cSflorian struct cookie_secrets*
192*7037e34cSflorian cookie_secrets_create(void)
193*7037e34cSflorian {
194*7037e34cSflorian 	struct cookie_secrets* cookie_secrets = calloc(1,
195*7037e34cSflorian 		sizeof(*cookie_secrets));
196*7037e34cSflorian 	if(!cookie_secrets)
197*7037e34cSflorian 		return NULL;
198*7037e34cSflorian 	lock_basic_init(&cookie_secrets->lock);
199*7037e34cSflorian 	lock_protect(&cookie_secrets->lock, &cookie_secrets->cookie_count,
200*7037e34cSflorian 		sizeof(cookie_secrets->cookie_count));
201*7037e34cSflorian 	lock_protect(&cookie_secrets->lock, cookie_secrets->cookie_secrets,
202*7037e34cSflorian 		sizeof(cookie_secret_type)*UNBOUND_COOKIE_HISTORY_SIZE);
203*7037e34cSflorian 	return cookie_secrets;
204*7037e34cSflorian }
205*7037e34cSflorian 
206*7037e34cSflorian void
207*7037e34cSflorian cookie_secrets_delete(struct cookie_secrets* cookie_secrets)
208*7037e34cSflorian {
209*7037e34cSflorian 	if(!cookie_secrets)
210*7037e34cSflorian 		return;
211*7037e34cSflorian 	lock_basic_destroy(&cookie_secrets->lock);
212*7037e34cSflorian 	explicit_bzero(cookie_secrets->cookie_secrets,
213*7037e34cSflorian 		sizeof(cookie_secret_type)*UNBOUND_COOKIE_HISTORY_SIZE);
214*7037e34cSflorian 	free(cookie_secrets);
215*7037e34cSflorian }
216*7037e34cSflorian 
217*7037e34cSflorian /** Read the cookie secret file */
218*7037e34cSflorian static int
219*7037e34cSflorian cookie_secret_file_read(struct cookie_secrets* cookie_secrets,
220*7037e34cSflorian 	char* cookie_secret_file)
221*7037e34cSflorian {
222*7037e34cSflorian 	char secret[UNBOUND_COOKIE_SECRET_SIZE * 2 + 2/*'\n' and '\0'*/];
223*7037e34cSflorian 	FILE* f;
224*7037e34cSflorian 	int corrupt = 0;
225*7037e34cSflorian 	size_t count;
226*7037e34cSflorian 
227*7037e34cSflorian 	log_assert(cookie_secret_file != NULL);
228*7037e34cSflorian 	cookie_secrets->cookie_count = 0;
229*7037e34cSflorian 	f = fopen(cookie_secret_file, "r");
230*7037e34cSflorian 	/* a non-existing cookie file is not an error */
231*7037e34cSflorian 	if( f == NULL ) {
232*7037e34cSflorian 		if(errno != EPERM) {
233*7037e34cSflorian 			log_err("Could not read cookie-secret-file '%s': %s",
234*7037e34cSflorian 				cookie_secret_file, strerror(errno));
235*7037e34cSflorian 			return 0;
236*7037e34cSflorian 		}
237*7037e34cSflorian 		return 1;
238*7037e34cSflorian 	}
239*7037e34cSflorian 	/* cookie secret file exists and is readable */
240*7037e34cSflorian 	for( count = 0; count < UNBOUND_COOKIE_HISTORY_SIZE; count++ ) {
241*7037e34cSflorian 		size_t secret_len = 0;
242*7037e34cSflorian 		ssize_t decoded_len = 0;
243*7037e34cSflorian 		if( fgets(secret, sizeof(secret), f) == NULL ) { break; }
244*7037e34cSflorian 		secret_len = strlen(secret);
245*7037e34cSflorian 		if( secret_len == 0 ) { break; }
246*7037e34cSflorian 		log_assert( secret_len <= sizeof(secret) );
247*7037e34cSflorian 		secret_len = secret[secret_len - 1] == '\n' ? secret_len - 1 : secret_len;
248*7037e34cSflorian 		if( secret_len != UNBOUND_COOKIE_SECRET_SIZE * 2 ) { corrupt++; break; }
249*7037e34cSflorian 		/* needed for `hex_pton`; stripping potential `\n` */
250*7037e34cSflorian 		secret[secret_len] = '\0';
251*7037e34cSflorian 		decoded_len = hex_pton(secret, cookie_secrets->cookie_secrets[count].cookie_secret,
252*7037e34cSflorian 		                       UNBOUND_COOKIE_SECRET_SIZE);
253*7037e34cSflorian 		if( decoded_len != UNBOUND_COOKIE_SECRET_SIZE ) { corrupt++; break; }
254*7037e34cSflorian 		cookie_secrets->cookie_count++;
255*7037e34cSflorian 	}
256*7037e34cSflorian 	fclose(f);
257*7037e34cSflorian 	return corrupt == 0;
258*7037e34cSflorian }
259*7037e34cSflorian 
260*7037e34cSflorian int
261*7037e34cSflorian cookie_secrets_apply_cfg(struct cookie_secrets* cookie_secrets,
262*7037e34cSflorian 	char* cookie_secret_file)
263*7037e34cSflorian {
264*7037e34cSflorian 	if(!cookie_secrets) {
265*7037e34cSflorian 		if(!cookie_secret_file || !cookie_secret_file[0])
266*7037e34cSflorian 			return 1; /* There is nothing to read anyway */
267*7037e34cSflorian 		log_err("Could not read cookie secrets, no structure alloced");
268*7037e34cSflorian 		return 0;
269*7037e34cSflorian 	}
270*7037e34cSflorian 	if(!cookie_secret_file_read(cookie_secrets, cookie_secret_file))
271*7037e34cSflorian 		return 0;
272*7037e34cSflorian 	return 1;
273*7037e34cSflorian }
274*7037e34cSflorian 
275*7037e34cSflorian enum edns_cookie_val_status
276*7037e34cSflorian cookie_secrets_server_validate(const uint8_t* cookie, size_t cookie_len,
277*7037e34cSflorian 	struct cookie_secrets* cookie_secrets, int v4,
278*7037e34cSflorian 	const uint8_t* hash_input, uint32_t now)
279*7037e34cSflorian {
280*7037e34cSflorian 	size_t i;
281*7037e34cSflorian 	enum edns_cookie_val_status cookie_val_status,
282*7037e34cSflorian 		last = COOKIE_STATUS_INVALID;
283*7037e34cSflorian 	if(!cookie_secrets)
284*7037e34cSflorian 		return COOKIE_STATUS_INVALID; /* There are no cookie secrets.*/
285*7037e34cSflorian 	lock_basic_lock(&cookie_secrets->lock);
286*7037e34cSflorian 	if(cookie_secrets->cookie_count == 0) {
287*7037e34cSflorian 		lock_basic_unlock(&cookie_secrets->lock);
288*7037e34cSflorian 		return COOKIE_STATUS_INVALID; /* There are no cookie secrets.*/
289*7037e34cSflorian 	}
290*7037e34cSflorian 	for(i=0; i<cookie_secrets->cookie_count; i++) {
291*7037e34cSflorian 		cookie_val_status = edns_cookie_server_validate(cookie,
292*7037e34cSflorian 			cookie_len,
293*7037e34cSflorian 			cookie_secrets->cookie_secrets[i].cookie_secret,
294*7037e34cSflorian 			UNBOUND_COOKIE_SECRET_SIZE, v4, hash_input, now);
295*7037e34cSflorian 		if(cookie_val_status == COOKIE_STATUS_VALID ||
296*7037e34cSflorian 			cookie_val_status == COOKIE_STATUS_VALID_RENEW) {
297*7037e34cSflorian 			lock_basic_unlock(&cookie_secrets->lock);
298*7037e34cSflorian 			/* For staging cookies, write a fresh cookie. */
299*7037e34cSflorian 			if(i != 0)
300*7037e34cSflorian 				return COOKIE_STATUS_VALID_RENEW;
301*7037e34cSflorian 			return cookie_val_status;
302*7037e34cSflorian 		}
303*7037e34cSflorian 		if(last == COOKIE_STATUS_INVALID)
304*7037e34cSflorian 			last = cookie_val_status; /* Store more interesting
305*7037e34cSflorian 				failure to return. */
306*7037e34cSflorian 	}
307*7037e34cSflorian 	lock_basic_unlock(&cookie_secrets->lock);
308*7037e34cSflorian 	return last;
309*7037e34cSflorian }
310*7037e34cSflorian 
311*7037e34cSflorian void add_cookie_secret(struct cookie_secrets* cookie_secrets,
312*7037e34cSflorian 	uint8_t* secret, size_t secret_len)
313*7037e34cSflorian {
314*7037e34cSflorian 	log_assert(secret_len == UNBOUND_COOKIE_SECRET_SIZE);
315*7037e34cSflorian 	(void)secret_len;
316*7037e34cSflorian 	if(!cookie_secrets)
317*7037e34cSflorian 		return;
318*7037e34cSflorian 
319*7037e34cSflorian 	/* New cookie secret becomes the staging secret (position 1)
320*7037e34cSflorian 	 * unless there is no active cookie yet, then it becomes the active
321*7037e34cSflorian 	 * secret.  If the UNBOUND_COOKIE_HISTORY_SIZE > 2 then all staging cookies
322*7037e34cSflorian 	 * are moved one position down.
323*7037e34cSflorian 	 */
324*7037e34cSflorian 	if(cookie_secrets->cookie_count == 0) {
325*7037e34cSflorian 		memcpy( cookie_secrets->cookie_secrets->cookie_secret
326*7037e34cSflorian 		       , secret, UNBOUND_COOKIE_SECRET_SIZE);
327*7037e34cSflorian 		cookie_secrets->cookie_count = 1;
328*7037e34cSflorian 		explicit_bzero(secret, UNBOUND_COOKIE_SECRET_SIZE);
329*7037e34cSflorian 		return;
330*7037e34cSflorian 	}
331*7037e34cSflorian #if UNBOUND_COOKIE_HISTORY_SIZE > 2
332*7037e34cSflorian 	memmove( &cookie_secrets->cookie_secrets[2], &cookie_secrets->cookie_secrets[1]
333*7037e34cSflorian 	       , sizeof(struct cookie_secret) * (UNBOUND_COOKIE_HISTORY_SIZE - 2));
334*7037e34cSflorian #endif
335*7037e34cSflorian 	memcpy( cookie_secrets->cookie_secrets[1].cookie_secret
336*7037e34cSflorian 	      , secret, UNBOUND_COOKIE_SECRET_SIZE);
337*7037e34cSflorian 	cookie_secrets->cookie_count = cookie_secrets->cookie_count     < UNBOUND_COOKIE_HISTORY_SIZE
338*7037e34cSflorian 	                  ? cookie_secrets->cookie_count + 1 : UNBOUND_COOKIE_HISTORY_SIZE;
339*7037e34cSflorian 	explicit_bzero(secret, UNBOUND_COOKIE_SECRET_SIZE);
340*7037e34cSflorian }
341*7037e34cSflorian 
342*7037e34cSflorian void activate_cookie_secret(struct cookie_secrets* cookie_secrets)
343*7037e34cSflorian {
344*7037e34cSflorian 	uint8_t active_secret[UNBOUND_COOKIE_SECRET_SIZE];
345*7037e34cSflorian 	if(!cookie_secrets)
346*7037e34cSflorian 		return;
347*7037e34cSflorian 	/* The staging secret becomes the active secret.
348*7037e34cSflorian 	 * The active secret becomes a staging secret.
349*7037e34cSflorian 	 * If the UNBOUND_COOKIE_HISTORY_SIZE > 2 then all staging secrets are moved
350*7037e34cSflorian 	 * one position up and the previously active secret becomes the last
351*7037e34cSflorian 	 * staging secret.
352*7037e34cSflorian 	 */
353*7037e34cSflorian 	if(cookie_secrets->cookie_count < 2)
354*7037e34cSflorian 		return;
355*7037e34cSflorian 	memcpy( active_secret, cookie_secrets->cookie_secrets[0].cookie_secret
356*7037e34cSflorian 	      , UNBOUND_COOKIE_SECRET_SIZE);
357*7037e34cSflorian 	memmove( &cookie_secrets->cookie_secrets[0], &cookie_secrets->cookie_secrets[1]
358*7037e34cSflorian 	       , sizeof(struct cookie_secret) * (UNBOUND_COOKIE_HISTORY_SIZE - 1));
359*7037e34cSflorian 	memcpy( cookie_secrets->cookie_secrets[cookie_secrets->cookie_count - 1].cookie_secret
360*7037e34cSflorian 	      , active_secret, UNBOUND_COOKIE_SECRET_SIZE);
361*7037e34cSflorian 	explicit_bzero(active_secret, UNBOUND_COOKIE_SECRET_SIZE);
362*7037e34cSflorian }
363*7037e34cSflorian 
364*7037e34cSflorian void drop_cookie_secret(struct cookie_secrets* cookie_secrets)
365*7037e34cSflorian {
366*7037e34cSflorian 	if(!cookie_secrets)
367*7037e34cSflorian 		return;
368*7037e34cSflorian 	/* Drops a staging cookie secret. If there are more than one, it will
369*7037e34cSflorian 	 * drop the last staging secret. */
370*7037e34cSflorian 	if(cookie_secrets->cookie_count < 2)
371*7037e34cSflorian 		return;
372*7037e34cSflorian 	explicit_bzero( cookie_secrets->cookie_secrets[cookie_secrets->cookie_count - 1].cookie_secret
373*7037e34cSflorian 	              , UNBOUND_COOKIE_SECRET_SIZE);
374*7037e34cSflorian 	cookie_secrets->cookie_count -= 1;
375*7037e34cSflorian }
376