Coverage Report

Created: 2025-03-18 06:55

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