1 /*
2  * Copyright (C) 2018 Red Hat, Inc.
3  *
4  * Author: Daiki Ueno
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>
20  *
21  */
22 
23 #include "gnutls_int.h"
24 #include "db.h"
25 #include "system.h"
26 #include "tls13/anti_replay.h"
27 
28 /* The default time window in milliseconds; RFC8446 suggests the order
29  * of ten seconds is sufficient for the clients on the Internet. */
30 #define DEFAULT_WINDOW_MS 10000
31 
32 struct gnutls_anti_replay_st {
33 	uint32_t window;
34 	struct timespec start_time;
35 	gnutls_db_add_func db_add_func;
36 	void *db_ptr;
37 };
38 
39 /**
40  * gnutls_anti_replay_init:
41  * @anti_replay: is a pointer to #gnutls_anti_replay_t type
42  *
43  * This function will allocate and initialize the @anti_replay context
44  * to be usable for detect replay attacks. The context can then be
45  * attached to a @gnutls_session_t with
46  * gnutls_anti_replay_enable().
47  *
48  * Returns: Zero or a negative error code on error.
49  *
50  * Since: 3.6.5
51  **/
52 int
gnutls_anti_replay_init(gnutls_anti_replay_t * anti_replay)53 gnutls_anti_replay_init(gnutls_anti_replay_t *anti_replay)
54 {
55 	*anti_replay = gnutls_calloc(1, sizeof(struct gnutls_anti_replay_st));
56 	if (!*anti_replay)
57 		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
58 
59 	(*anti_replay)->window = DEFAULT_WINDOW_MS;
60 
61 	gnutls_gettime(&(*anti_replay)->start_time);
62 
63 	return 0;
64 }
65 
66 /**
67  * gnutls_anti_replay_set_window:
68  * @anti_replay: is a #gnutls_anti_replay_t type.
69  * @window: is the time window recording ClientHello, in milliseconds
70  *
71  * Sets the time window used for ClientHello recording.  In order to
72  * protect against replay attacks, the server records ClientHello
73  * messages within this time period from the last update, and
74  * considers it a replay when a ClientHello outside of the period; if
75  * a ClientHello arrives within this period, the server checks the
76  * database and detects duplicates.
77  *
78  * For the details of the algorithm, see RFC 8446, section 8.2.
79  *
80  * Since: 3.6.5
81  */
82 void
gnutls_anti_replay_set_window(gnutls_anti_replay_t anti_replay,unsigned int window)83 gnutls_anti_replay_set_window(gnutls_anti_replay_t anti_replay,
84 			      unsigned int window)
85 {
86 	anti_replay->window = window;
87 }
88 
89 /**
90  * gnutls_anti_replay_deinit:
91  * @anti_replay: is a #gnutls_anti_replay type
92  *
93  * This function will deinitialize all resources occupied by the given
94  * anti-replay context.
95  *
96  * Since: 3.6.5
97  **/
98 void
gnutls_anti_replay_deinit(gnutls_anti_replay_t anti_replay)99 gnutls_anti_replay_deinit(gnutls_anti_replay_t anti_replay)
100 {
101 	gnutls_free(anti_replay);
102 }
103 
104 /**
105  * gnutls_anti_replay_enable:
106  * @session: is a #gnutls_session_t type.
107  * @anti_replay: is a #gnutls_anti_replay_t type.
108  *
109  * Request that the server should use anti-replay mechanism.
110  *
111  * Since: 3.6.5
112  **/
113 void
gnutls_anti_replay_enable(gnutls_session_t session,gnutls_anti_replay_t anti_replay)114 gnutls_anti_replay_enable(gnutls_session_t session,
115 			  gnutls_anti_replay_t anti_replay)
116 {
117 	if (unlikely(session->security_parameters.entity != GNUTLS_SERVER)) {
118 		gnutls_assert();
119 		return;
120 	}
121 
122 	session->internals.anti_replay = anti_replay;
123 }
124 
125 int
_gnutls_anti_replay_check(gnutls_anti_replay_t anti_replay,uint32_t client_ticket_age,struct timespec * ticket_creation_time,gnutls_datum_t * id)126 _gnutls_anti_replay_check(gnutls_anti_replay_t anti_replay,
127 			  uint32_t client_ticket_age,
128 			  struct timespec *ticket_creation_time,
129 			  gnutls_datum_t *id)
130 {
131 	struct timespec now;
132 	time_t window;
133 	uint32_t server_ticket_age, diff;
134 	gnutls_datum_t key = { NULL, 0 };
135 	gnutls_datum_t entry = { NULL, 0 };
136 	unsigned char key_buffer[MAX_HASH_SIZE + 12];
137 	unsigned char entry_buffer[12]; /* magic + timestamp + expire_time */
138 	unsigned char *p;
139 	int ret;
140 
141 	if (unlikely(id->size > MAX_HASH_SIZE))
142 		return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
143 
144 	gnutls_gettime(&now);
145 	server_ticket_age = timespec_sub_ms(&now, ticket_creation_time);
146 
147 	/* It shouldn't be possible that the server's view of ticket
148 	 * age is smaller than the client's view.
149 	 */
150 	if (unlikely(server_ticket_age < client_ticket_age))
151 		return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
152 
153 	/* If ticket is created before recording has started, discard
154 	 * reject early data.
155 	 */
156 	if (_gnutls_timespec_cmp(ticket_creation_time,
157 				 &anti_replay->start_time) < 0) {
158 		_gnutls_handshake_log("anti_replay: ticket is created before recording has started\n");
159 		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
160 	}
161 
162 	/* If certain amount of time (window) has elapsed, rollover
163 	 * the recording.
164 	 */
165 	diff = timespec_sub_ms(&now, &anti_replay->start_time);
166 	if (diff > anti_replay->window)
167 		gnutls_gettime(&anti_replay->start_time);
168 
169 	/* If expected_arrival_time is out of window, reject early
170 	 * data.
171 	 */
172 	if (server_ticket_age - client_ticket_age > anti_replay->window) {
173 		_gnutls_handshake_log("anti_replay: server ticket age: %u, client ticket age: %u\n",
174 				      server_ticket_age,
175 				      client_ticket_age);
176 		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
177 	}
178 
179 	/* Check if the ClientHello is stored in the database.
180 	 */
181 	if (!anti_replay->db_add_func)
182 		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
183 
184 	/* Create a key for database lookup, prefixing window start
185 	 * time to ID.  Note that this shouldn't clash with session ID
186 	 * used in TLS 1.2, because such IDs are 32 octets, while here
187 	 * the key becomes 44+ octets.
188 	 */
189 	p = key_buffer;
190 	_gnutls_write_uint32((uint64_t) anti_replay->start_time.tv_sec >> 32, p);
191 	p += 4;
192 	_gnutls_write_uint32(anti_replay->start_time.tv_sec & 0xFFFFFFFF, p);
193 	p += 4;
194 	_gnutls_write_uint32(anti_replay->start_time.tv_nsec, p);
195 	p += 4;
196 	memcpy(p, id->data, id->size);
197 	p += id->size;
198 	key.data = key_buffer;
199 	key.size = p - key_buffer;
200 
201 	/* Create an entry to be stored on database if the lookup
202 	 * failed.  This is formatted so that
203 	 * gnutls_db_check_entry_expire_time() work.
204 	 */
205 	p = entry_buffer;
206 	_gnutls_write_uint32(PACKED_SESSION_MAGIC, p);
207 	p += 4;
208 	_gnutls_write_uint32(now.tv_sec, p);
209 	p += 4;
210 	window = anti_replay->window / 1000;
211 	_gnutls_write_uint32(window, p);
212 	p += 4;
213 	entry.data = entry_buffer;
214 	entry.size = p - entry_buffer;
215 
216 	ret = anti_replay->db_add_func(anti_replay->db_ptr,
217 				       (uint64_t)now.tv_sec+(uint64_t)window, &key, &entry);
218 	if (ret < 0) {
219 		_gnutls_handshake_log("anti_replay: duplicate ClientHello found\n");
220 		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
221 	}
222 
223 	return 0;
224 }
225 
226 /**
227  * gnutls_anti_replay_set_ptr:
228  * @anti_replay: is a #gnutls_anti_replay_t type.
229  * @ptr: is the pointer
230  *
231  * Sets the pointer that will be provided to db add function
232  * as the first argument.
233  **/
gnutls_anti_replay_set_ptr(gnutls_anti_replay_t anti_replay,void * ptr)234 void gnutls_anti_replay_set_ptr(gnutls_anti_replay_t anti_replay, void *ptr)
235 {
236 	anti_replay->db_ptr = ptr;
237 }
238 
239 /**
240  * gnutls_anti_replay_set_add_function:
241  * @anti_replay: is a #gnutls_anti_replay_t type.
242  * @add_func: is the function.
243  *
244  * Sets the function that will be used to store an entry if it is not
245  * already present in the resumed sessions database.  This function returns 0
246  * if the entry is successfully stored, and a negative error code
247  * otherwise.  In particular, if the entry is found in the database,
248  * it returns %GNUTLS_E_DB_ENTRY_EXISTS.
249  *
250  * The arguments to the @add_func are:
251  *  - %ptr: the pointer set with gnutls_anti_replay_set_ptr()
252  *  - %exp_time: the expiration time of the entry
253  *  - %key: a pointer to the key
254  *  - %data: a pointer to data to store
255  *
256  * The data set by this function can be examined using
257  * gnutls_db_check_entry_expire_time() and gnutls_db_check_entry_time().
258  *
259  * Since: 3.6.5
260  **/
261 void
gnutls_anti_replay_set_add_function(gnutls_anti_replay_t anti_replay,gnutls_db_add_func add_func)262 gnutls_anti_replay_set_add_function(gnutls_anti_replay_t anti_replay,
263 				    gnutls_db_add_func add_func)
264 {
265 	anti_replay->db_add_func = add_func;
266 }
267