Coverage Report

Created: 2023-03-26 08:33

/src/gnutls/lib/tls13/anti_replay.c
Line
Count
Source (jump to first uncovered line)
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
0
#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
0
{
54
0
  *anti_replay = gnutls_calloc(1, sizeof(struct gnutls_anti_replay_st));
55
0
  if (!*anti_replay)
56
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
57
58
0
  (*anti_replay)->window = DEFAULT_WINDOW_MS;
59
60
0
  gnutls_gettime(&(*anti_replay)->start_time);
61
62
0
  return 0;
63
0
}
64
65
/**
66
 * gnutls_anti_replay_set_window:
67
 * @anti_replay: is a #gnutls_anti_replay_t type.
68
 * @window: is the time window recording ClientHello, in milliseconds
69
 *
70
 * Sets the time window used for ClientHello recording.  In order to
71
 * protect against replay attacks, the server records ClientHello
72
 * messages within this time period from the last update, and
73
 * considers it a replay when a ClientHello outside of the period; if
74
 * a ClientHello arrives within this period, the server checks the
75
 * database and detects duplicates.
76
 *
77
 * For the details of the algorithm, see RFC 8446, section 8.2.
78
 *
79
 * Since: 3.6.5
80
 */
81
void
82
gnutls_anti_replay_set_window(gnutls_anti_replay_t anti_replay,
83
            unsigned int window)
84
0
{
85
0
  anti_replay->window = window;
86
0
}
87
88
/**
89
 * gnutls_anti_replay_deinit:
90
 * @anti_replay: is a #gnutls_anti_replay type
91
 *
92
 * This function will deinitialize all resources occupied by the given
93
 * anti-replay context.
94
 *
95
 * Since: 3.6.5
96
 **/
97
void gnutls_anti_replay_deinit(gnutls_anti_replay_t anti_replay)
98
0
{
99
0
  gnutls_free(anti_replay);
100
0
}
101
102
/**
103
 * gnutls_anti_replay_enable:
104
 * @session: is a #gnutls_session_t type.
105
 * @anti_replay: is a #gnutls_anti_replay_t type.
106
 *
107
 * Request that the server should use anti-replay mechanism.
108
 *
109
 * Since: 3.6.5
110
 **/
111
void
112
gnutls_anti_replay_enable(gnutls_session_t session,
113
        gnutls_anti_replay_t anti_replay)
114
0
{
115
0
  if (unlikely(session->security_parameters.entity != GNUTLS_SERVER)) {
116
0
    gnutls_assert();
117
0
    return;
118
0
  }
119
120
0
  session->internals.anti_replay = anti_replay;
121
0
}
122
123
int
124
_gnutls_anti_replay_check(gnutls_anti_replay_t anti_replay,
125
        uint32_t client_ticket_age,
126
        struct timespec *ticket_creation_time,
127
        gnutls_datum_t * id)
128
0
{
129
0
  struct timespec now;
130
0
  time_t window;
131
0
  uint32_t server_ticket_age, diff;
132
0
  gnutls_datum_t key = { NULL, 0 };
133
0
  gnutls_datum_t entry = { NULL, 0 };
134
0
  unsigned char key_buffer[MAX_HASH_SIZE + 12];
135
0
  unsigned char entry_buffer[12]; /* magic + timestamp + expire_time */
136
0
  unsigned char *p;
137
0
  int ret;
138
139
0
  if (unlikely(id->size > MAX_HASH_SIZE))
140
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
141
142
0
  gnutls_gettime(&now);
143
0
  server_ticket_age = timespec_sub_ms(&now, ticket_creation_time);
144
145
  /* It shouldn't be possible that the server's view of ticket
146
   * age is smaller than the client's view.
147
   */
148
0
  if (unlikely(server_ticket_age < client_ticket_age))
149
0
    return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
150
151
  /* If ticket is created before recording has started, discard
152
   * reject early data.
153
   */
154
0
  if (_gnutls_timespec_cmp(ticket_creation_time,
155
0
         &anti_replay->start_time) < 0) {
156
0
    _gnutls_handshake_log
157
0
        ("anti_replay: ticket is created before recording has started\n");
158
0
    return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
159
0
  }
160
161
  /* If certain amount of time (window) has elapsed, rollover
162
   * the recording.
163
   */
164
0
  diff = timespec_sub_ms(&now, &anti_replay->start_time);
165
0
  if (diff > anti_replay->window)
166
0
    gnutls_gettime(&anti_replay->start_time);
167
168
  /* If expected_arrival_time is out of window, reject early
169
   * data.
170
   */
171
0
  if (server_ticket_age - client_ticket_age > anti_replay->window) {
172
0
    _gnutls_handshake_log
173
0
        ("anti_replay: server ticket age: %u, client ticket age: %u\n",
174
0
         server_ticket_age, client_ticket_age);
175
0
    return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
176
0
  }
177
178
  /* Check if the ClientHello is stored in the database.
179
   */
180
0
  if (!anti_replay->db_add_func)
181
0
    return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
182
183
  /* Create a key for database lookup, prefixing window start
184
   * time to ID.  Note that this shouldn't clash with session ID
185
   * used in TLS 1.2, because such IDs are 32 octets, while here
186
   * the key becomes 44+ octets.
187
   */
188
0
  p = key_buffer;
189
0
  _gnutls_write_uint32((uint64_t) anti_replay->start_time.tv_sec >> 32,
190
0
           p);
191
0
  p += 4;
192
0
  _gnutls_write_uint32(anti_replay->start_time.tv_sec & 0xFFFFFFFF, p);
193
0
  p += 4;
194
0
  _gnutls_write_uint32(anti_replay->start_time.tv_nsec, p);
195
0
  p += 4;
196
0
  memcpy(p, id->data, id->size);
197
0
  p += id->size;
198
0
  key.data = key_buffer;
199
0
  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
0
  p = entry_buffer;
206
0
  _gnutls_write_uint32(PACKED_SESSION_MAGIC, p);
207
0
  p += 4;
208
0
  _gnutls_write_uint32(now.tv_sec, p);
209
0
  p += 4;
210
0
  window = anti_replay->window / 1000;
211
0
  _gnutls_write_uint32(window, p);
212
0
  p += 4;
213
0
  entry.data = entry_buffer;
214
0
  entry.size = p - entry_buffer;
215
216
0
  ret = anti_replay->db_add_func(anti_replay->db_ptr,
217
0
               (uint64_t) now.tv_sec +
218
0
               (uint64_t) window, &key, &entry);
219
0
  if (ret < 0) {
220
0
    _gnutls_handshake_log
221
0
        ("anti_replay: duplicate ClientHello found\n");
222
0
    return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
223
0
  }
224
225
0
  return 0;
226
0
}
227
228
/**
229
 * gnutls_anti_replay_set_ptr:
230
 * @anti_replay: is a #gnutls_anti_replay_t type.
231
 * @ptr: is the pointer
232
 *
233
 * Sets the pointer that will be provided to db add function
234
 * as the first argument.
235
 **/
236
void gnutls_anti_replay_set_ptr(gnutls_anti_replay_t anti_replay, void *ptr)
237
0
{
238
0
  anti_replay->db_ptr = ptr;
239
0
}
240
241
/**
242
 * gnutls_anti_replay_set_add_function:
243
 * @anti_replay: is a #gnutls_anti_replay_t type.
244
 * @add_func: is the function.
245
 *
246
 * Sets the function that will be used to store an entry if it is not
247
 * already present in the resumed sessions database.  This function returns 0
248
 * if the entry is successfully stored, and a negative error code
249
 * otherwise.  In particular, if the entry is found in the database,
250
 * it returns %GNUTLS_E_DB_ENTRY_EXISTS.
251
 *
252
 * The arguments to the @add_func are:
253
 *  - %ptr: the pointer set with gnutls_anti_replay_set_ptr()
254
 *  - %exp_time: the expiration time of the entry
255
 *  - %key: a pointer to the key
256
 *  - %data: a pointer to data to store
257
 *
258
 * The data set by this function can be examined using
259
 * gnutls_db_check_entry_expire_time() and gnutls_db_check_entry_time().
260
 *
261
 * Since: 3.6.5
262
 **/
263
void
264
gnutls_anti_replay_set_add_function(gnutls_anti_replay_t anti_replay,
265
            gnutls_db_add_func add_func)
266
0
{
267
0
  anti_replay->db_add_func = add_func;
268
0
}