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