summaryrefslogtreecommitdiff
path: root/lib/tls13/anti_replay.c
blob: a99266928ced17d57a7e99de43d60ab53ca5fecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * Author: Daiki Ueno
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

#include "gnutls_int.h"
#include "db.h"
#include "system.h"
#include "tls13/anti_replay.h"

/* The default time window in milliseconds; RFC8446 suggests the order
 * of ten seconds is sufficient for the clients on the Internet. */
#define DEFAULT_WINDOW_MS 10000

struct gnutls_anti_replay_st {
	uint32_t window;
	struct timespec start_time;
	gnutls_db_add_func db_add_func;
	void *db_ptr;
};

/**
 * gnutls_anti_replay_init:
 * @anti_replay: is a pointer to #gnutls_anti_replay_t type
 *
 * This function will allocate and initialize the @anti_replay context
 * to be usable for detect replay attacks. The context can then be
 * attached to a @gnutls_session_t with
 * gnutls_anti_replay_enable().
 *
 * Returns: Zero or a negative error code on error.
 *
 * Since: 3.6.5
 **/
int
gnutls_anti_replay_init(gnutls_anti_replay_t *anti_replay)
{
	*anti_replay = gnutls_calloc(1, sizeof(struct gnutls_anti_replay_st));
	if (!*anti_replay)
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

	(*anti_replay)->window = DEFAULT_WINDOW_MS;

	gnutls_gettime(&(*anti_replay)->start_time);

	return 0;
}

/**
 * gnutls_anti_replay_set_window:
 * @anti_replay: is a #gnutls_anti_replay_t type.
 * @window: is the time window recording ClientHello, in milliseconds
 *
 * Sets the time window used for ClientHello recording.  In order to
 * protect against replay attacks, the server records ClientHello
 * messages within this time period from the last update, and
 * considers it a replay when a ClientHello outside of the period; if
 * a ClientHello arrives within this period, the server checks the
 * database and detects duplicates.
 *
 * For the details of the algorithm, see RFC 8446, section 8.2.
 *
 * Since: 3.6.5
 */
void
gnutls_anti_replay_set_window(gnutls_anti_replay_t anti_replay,
			      unsigned int window)
{
	anti_replay->window = window;
}

/**
 * gnutls_anti_replay_deinit:
 * @anti_replay: is a #gnutls_anti_replay type
 *
 * This function will deinitialize all resources occupied by the given
 * anti-replay context.
 *
 * Since: 3.6.5
 **/
void
gnutls_anti_replay_deinit(gnutls_anti_replay_t anti_replay)
{
	gnutls_free(anti_replay);
}

/**
 * gnutls_anti_replay_enable:
 * @session: is a #gnutls_session_t type.
 * @anti_replay: is a #gnutls_anti_replay_t type.
 *
 * Request that the server should use anti-replay mechanism.
 *
 * Since: 3.6.5
 **/
void
gnutls_anti_replay_enable(gnutls_session_t session,
			  gnutls_anti_replay_t anti_replay)
{
	if (unlikely(session->security_parameters.entity != GNUTLS_SERVER)) {
		gnutls_assert();
		return;
	}

	session->internals.anti_replay = anti_replay;
}

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)
{
	struct timespec now;
	time_t window;
	uint32_t server_ticket_age, diff;
	gnutls_datum_t key = { NULL, 0 };
	gnutls_datum_t entry = { NULL, 0 };
	unsigned char key_buffer[MAX_HASH_SIZE + 12];
	unsigned char entry_buffer[12]; /* magic + timestamp + expire_time */
	unsigned char *p;
	int ret;

	if (unlikely(id->size > MAX_HASH_SIZE))
		return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);

	gnutls_gettime(&now);
	server_ticket_age = timespec_sub_ms(&now, ticket_creation_time);

	/* It shouldn't be possible that the server's view of ticket
	 * age is smaller than the client's view.
	 */
	if (unlikely(server_ticket_age < client_ticket_age))
		return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);

	/* If ticket is created before recording has started, discard
	 * reject early data.
	 */
	if (_gnutls_timespec_cmp(ticket_creation_time,
				 &anti_replay->start_time) < 0) {
		_gnutls_handshake_log("anti_replay: ticket is created before recording has started\n");
		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
	}

	/* If certain amount of time (window) has elapsed, rollover
	 * the recording.
	 */
	diff = timespec_sub_ms(&now, &anti_replay->start_time);
	if (diff > anti_replay->window)
		gnutls_gettime(&anti_replay->start_time);

	/* If expected_arrival_time is out of window, reject early
	 * data.
	 */
	if (server_ticket_age - client_ticket_age > anti_replay->window) {
		_gnutls_handshake_log("anti_replay: server ticket age: %u, client ticket age: %u\n",
				      server_ticket_age,
				      client_ticket_age);
		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
	}

	/* Check if the ClientHello is stored in the database.
	 */
	if (!anti_replay->db_add_func)
		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);

	/* Create a key for database lookup, prefixing window start
	 * time to ID.  Note that this shouldn't clash with session ID
	 * used in TLS 1.2, because such IDs are 32 octets, while here
	 * the key becomes 44+ octets.
	 */
	p = key_buffer;
	_gnutls_write_uint32((uint64_t) anti_replay->start_time.tv_sec >> 32, p);
	p += 4;
	_gnutls_write_uint32(anti_replay->start_time.tv_sec & 0xFFFFFFFF, p);
	p += 4;
	_gnutls_write_uint32(anti_replay->start_time.tv_nsec, p);
	p += 4;
	memcpy(p, id->data, id->size);
	p += id->size;
	key.data = key_buffer;
	key.size = p - key_buffer;

	/* Create an entry to be stored on database if the lookup
	 * failed.  This is formatted so that
	 * gnutls_db_check_entry_expire_time() work.
	 */
	p = entry_buffer;
	_gnutls_write_uint32(PACKED_SESSION_MAGIC, p);
	p += 4;
	_gnutls_write_uint32(now.tv_sec, p);
	p += 4;
	window = anti_replay->window / 1000;
	_gnutls_write_uint32(window, p);
	p += 4;
	entry.data = entry_buffer;
	entry.size = p - entry_buffer;

	ret = anti_replay->db_add_func(anti_replay->db_ptr,
				       (uint64_t)now.tv_sec+(uint64_t)window, &key, &entry);
	if (ret < 0) {
		_gnutls_handshake_log("anti_replay: duplicate ClientHello found\n");
		return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
	}

	return 0;
}

/**
 * gnutls_anti_replay_set_ptr:
 * @anti_replay: is a #gnutls_anti_replay_t type.
 * @ptr: is the pointer
 *
 * Sets the pointer that will be provided to db add function
 * as the first argument.
 **/
void gnutls_anti_replay_set_ptr(gnutls_anti_replay_t anti_replay, void *ptr)
{
	anti_replay->db_ptr = ptr;
}

/**
 * gnutls_anti_replay_set_add_function:
 * @anti_replay: is a #gnutls_anti_replay_t type.
 * @add_func: is the function.
 *
 * Sets the function that will be used to store an entry if it is not
 * already present in the resumed sessions database.  This function returns 0
 * if the entry is successfully stored, and a negative error code
 * otherwise.  In particular, if the entry is found in the database,
 * it returns %GNUTLS_E_DB_ENTRY_EXISTS.
 *
 * The arguments to the @add_func are:
 *  - %ptr: the pointer set with gnutls_anti_replay_set_ptr()
 *  - %exp_time: the expiration time of the entry
 *  - %key: a pointer to the key
 *  - %data: a pointer to data to store
 *
 * The data set by this function can be examined using
 * gnutls_db_check_entry_expire_time() and gnutls_db_check_entry_time().
 *
 * Since: 3.6.5
 **/
void
gnutls_anti_replay_set_add_function(gnutls_anti_replay_t anti_replay,
				    gnutls_db_add_func add_func)
{
	anti_replay->db_add_func = add_func;
}