xref: /freebsd/contrib/unbound/util/edns.c (revision e5b786625f7f82a1fa91e41823332459ea5550f9)
1 /*
2  * util/edns.c - handle base EDNS options.
3  *
4  * Copyright (c) 2018, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions for base EDNS options.
40  */
41 
42 #include "config.h"
43 #include "util/edns.h"
44 #include "util/config_file.h"
45 #include "util/netevent.h"
46 #include "util/net_help.h"
47 #include "util/regional.h"
48 #include "util/rfc_1982.h"
49 #include "util/siphash.h"
50 #include "util/data/msgparse.h"
51 #include "util/data/msgreply.h"
52 #include "sldns/sbuffer.h"
53 
54 #if 0
55 /* XXX: remove me */
56 #include "edns.h"
57 #endif
58 
59 struct edns_strings* edns_strings_create(void)
60 {
61 	struct edns_strings* edns_strings = calloc(1,
62 		sizeof(struct edns_strings));
63 	if(!edns_strings)
64 		return NULL;
65 	if(!(edns_strings->region = regional_create())) {
66 		edns_strings_delete(edns_strings);
67 		return NULL;
68 	}
69 	return edns_strings;
70 }
71 
72 void edns_strings_delete(struct edns_strings* edns_strings)
73 {
74 	if(!edns_strings)
75 		return;
76 	regional_destroy(edns_strings->region);
77 	free(edns_strings);
78 }
79 
80 static int
81 edns_strings_client_insert(struct edns_strings* edns_strings,
82 	struct sockaddr_storage* addr, socklen_t addrlen, int net,
83 	const char* string)
84 {
85 	struct edns_string_addr* esa = regional_alloc_zero(edns_strings->region,
86 		sizeof(struct edns_string_addr));
87 	if(!esa)
88 		return 0;
89 	esa->string_len = strlen(string);
90 	esa->string = regional_alloc_init(edns_strings->region, string,
91 		esa->string_len);
92 	if(!esa->string)
93 		return 0;
94 	if(!addr_tree_insert(&edns_strings->client_strings, &esa->node, addr,
95 		addrlen, net)) {
96 		verbose(VERB_QUERY, "duplicate EDNS client string ignored.");
97 	}
98 	return 1;
99 }
100 
101 int edns_strings_apply_cfg(struct edns_strings* edns_strings,
102 	struct config_file* config)
103 {
104 	struct config_str2list* c;
105 	regional_free_all(edns_strings->region);
106 	addr_tree_init(&edns_strings->client_strings);
107 
108 	for(c=config->edns_client_strings; c; c=c->next) {
109 		struct sockaddr_storage addr;
110 		socklen_t addrlen;
111 		int net;
112 		log_assert(c->str && c->str2);
113 
114 		if(!netblockstrtoaddr(c->str, UNBOUND_DNS_PORT, &addr, &addrlen,
115 			&net)) {
116 			log_err("cannot parse EDNS client string IP netblock: "
117 				"%s", c->str);
118 			return 0;
119 		}
120 		if(!edns_strings_client_insert(edns_strings, &addr, addrlen,
121 			net, c->str2)) {
122 			log_err("out of memory while adding EDNS strings");
123 			return 0;
124 		}
125 	}
126 	edns_strings->client_string_opcode = config->edns_client_string_opcode;
127 
128 	addr_tree_init_parents(&edns_strings->client_strings);
129 	return 1;
130 }
131 
132 struct edns_string_addr*
133 edns_string_addr_lookup(rbtree_type* tree, struct sockaddr_storage* addr,
134 	socklen_t addrlen)
135 {
136 	return (struct edns_string_addr*)addr_tree_lookup(tree, addr, addrlen);
137 }
138 
139 uint8_t*
140 edns_cookie_server_hash(const uint8_t* in, const uint8_t* secret, int v4,
141 	uint8_t* hash)
142 {
143 	v4?siphash(in, 20, secret, hash, 8):siphash(in, 32, secret, hash, 8);
144 	return hash;
145 }
146 
147 void
148 edns_cookie_server_write(uint8_t* buf, const uint8_t* secret, int v4,
149 	uint32_t timestamp)
150 {
151 	uint8_t hash[8];
152 	buf[ 8] = 1;   /* Version */
153 	buf[ 9] = 0;   /* Reserved */
154 	buf[10] = 0;   /* Reserved */
155 	buf[11] = 0;   /* Reserved */
156 	sldns_write_uint32(buf + 12, timestamp);
157 	(void)edns_cookie_server_hash(buf, secret, v4, hash);
158 	memcpy(buf + 16, hash, 8);
159 }
160 
161 enum edns_cookie_val_status
162 edns_cookie_server_validate(const uint8_t* cookie, size_t cookie_len,
163 	const uint8_t* secret, size_t secret_len, int v4,
164 	const uint8_t* hash_input, uint32_t now)
165 {
166 	uint8_t hash[8];
167 	uint32_t timestamp;
168 	uint32_t subt_1982 = 0; /* Initialize for the compiler; unused value */
169 	int comp_1982;
170 	if(cookie_len != 24)
171 		/* RFC9018 cookies are 24 bytes long */
172 		return COOKIE_STATUS_CLIENT_ONLY;
173 	if(secret_len != 16 ||  /* RFC9018 cookies have 16 byte secrets */
174 		cookie[8] != 1) /* RFC9018 cookies are cookie version 1 */
175 		return COOKIE_STATUS_INVALID;
176 	timestamp = sldns_read_uint32(cookie + 12);
177 	if((comp_1982 = compare_1982(now, timestamp)) > 0
178 		&& (subt_1982 = subtract_1982(timestamp, now)) > 3600)
179 		/* Cookie is older than 1 hour (see RFC9018 Section 4.3.) */
180 		return COOKIE_STATUS_EXPIRED;
181 	if(comp_1982 <= 0 && subtract_1982(now, timestamp) > 300)
182 		/* Cookie time is more than 5 minutes in the future.
183 		 * (see RFC9018 Section 4.3.) */
184 		return COOKIE_STATUS_FUTURE;
185 	if(memcmp(edns_cookie_server_hash(hash_input, secret, v4, hash),
186 		cookie + 16, 8) != 0)
187 		/* Hashes do not match */
188 		return COOKIE_STATUS_INVALID;
189 	if(comp_1982 > 0 && subt_1982 > 1800)
190 		/* Valid cookie but older than 30 minutes, so create a new one
191 		 * anyway */
192 		return COOKIE_STATUS_VALID_RENEW;
193 	return COOKIE_STATUS_VALID;
194 }
195