Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/hello_ext.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2018 Free Software Foundation, Inc.
3
 * Copyright (C) 2015-2018 Red Hat, Inc.
4
 *
5
 * Author: Nikos Mavrogiannopoulos, Simon Josefsson
6
 *
7
 * This file is part of GnuTLS.
8
 *
9
 * The GnuTLS is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; either version 2.1 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
21
 *
22
 */
23
24
/* Functions that relate to the TLS hello extension parsing.
25
 * Hello extensions are packets appended in the TLS hello packet, and
26
 * allow for extra functionality.
27
 */
28
29
#include "gnutls_int.h"
30
#include "hello_ext.h"
31
#include "errors.h"
32
#include "ext/max_record.h"
33
#include "ext/server_name.h"
34
#include "ext/srp.h"
35
#include "ext/heartbeat.h"
36
#include "ext/session_ticket.h"
37
#include "ext/safe_renegotiation.h"
38
#include "ext/signature.h"
39
#include "ext/safe_renegotiation.h"
40
#include "ext/supported_groups.h"
41
#include "ext/ec_point_formats.h"
42
#include "ext/status_request.h"
43
#include "ext/ext_master_secret.h"
44
#include "ext/supported_versions.h"
45
#include "ext/post_handshake.h"
46
#include "ext/srtp.h"
47
#include "ext/alpn.h"
48
#include "ext/dumbfw.h"
49
#include "ext/key_share.h"
50
#include "ext/pre_shared_key.h"
51
#include "ext/psk_ke_modes.h"
52
#include "ext/etm.h"
53
#include "ext/cookie.h"
54
#include "ext/early_data.h"
55
#include "ext/record_size_limit.h"
56
#include "extv.h"
57
#include "num.h"
58
#include "ext/client_cert_type.h"
59
#include "ext/server_cert_type.h"
60
#include "ext/compress_certificate.h"
61
#include "intprops.h"
62
63
static void unset_ext_data(gnutls_session_t session,
64
         const struct hello_ext_entry_st *, unsigned idx);
65
66
static void unset_resumed_ext_data(gnutls_session_t session,
67
           const struct hello_ext_entry_st *,
68
           unsigned idx);
69
70
static hello_ext_entry_st const *extfunc[MAX_EXT_TYPES + 1] = {
71
  [GNUTLS_EXTENSION_EXT_MASTER_SECRET] = &ext_mod_ext_master_secret,
72
  [GNUTLS_EXTENSION_SUPPORTED_VERSIONS] = &ext_mod_supported_versions,
73
  [GNUTLS_EXTENSION_POST_HANDSHAKE] = &ext_mod_post_handshake,
74
  [GNUTLS_EXTENSION_ETM] = &ext_mod_etm,
75
#ifdef ENABLE_OCSP
76
  [GNUTLS_EXTENSION_STATUS_REQUEST] = &ext_mod_status_request,
77
#endif
78
  [GNUTLS_EXTENSION_SERVER_NAME] = &ext_mod_server_name,
79
  [GNUTLS_EXTENSION_SAFE_RENEGOTIATION] = &ext_mod_sr,
80
#ifdef ENABLE_SRP
81
  [GNUTLS_EXTENSION_SRP] = &ext_mod_srp,
82
#endif
83
#ifdef ENABLE_HEARTBEAT
84
  [GNUTLS_EXTENSION_HEARTBEAT] = &ext_mod_heartbeat,
85
#endif
86
  [GNUTLS_EXTENSION_SESSION_TICKET] = &ext_mod_session_ticket,
87
  [GNUTLS_EXTENSION_CLIENT_CERT_TYPE] = &ext_mod_client_cert_type,
88
  [GNUTLS_EXTENSION_SERVER_CERT_TYPE] = &ext_mod_server_cert_type,
89
  [GNUTLS_EXTENSION_SUPPORTED_GROUPS] = &ext_mod_supported_groups,
90
  [GNUTLS_EXTENSION_SUPPORTED_EC_POINT_FORMATS] =
91
    &ext_mod_supported_ec_point_formats,
92
  [GNUTLS_EXTENSION_SIGNATURE_ALGORITHMS] = &ext_mod_sig,
93
  [GNUTLS_EXTENSION_KEY_SHARE] = &ext_mod_key_share,
94
  [GNUTLS_EXTENSION_COOKIE] = &ext_mod_cookie,
95
  [GNUTLS_EXTENSION_EARLY_DATA] = &ext_mod_early_data,
96
#ifdef ENABLE_DTLS_SRTP
97
  [GNUTLS_EXTENSION_SRTP] = &ext_mod_srtp,
98
#endif
99
#ifdef ENABLE_ALPN
100
  [GNUTLS_EXTENSION_ALPN] = &ext_mod_alpn,
101
#endif
102
  [GNUTLS_EXTENSION_RECORD_SIZE_LIMIT] = &ext_mod_record_size_limit,
103
  [GNUTLS_EXTENSION_MAX_RECORD_SIZE] = &ext_mod_max_record_size,
104
  [GNUTLS_EXTENSION_PSK_KE_MODES] = &ext_mod_psk_ke_modes,
105
  [GNUTLS_EXTENSION_COMPRESS_CERTIFICATE] = &ext_mod_compress_certificate,
106
  [GNUTLS_EXTENSION_PRE_SHARED_KEY] = &ext_mod_pre_shared_key,
107
  /* This must be the last extension registered.
108
   */
109
  [GNUTLS_EXTENSION_DUMBFW] = &ext_mod_dumbfw,
110
};
111
112
static const hello_ext_entry_st *gid_to_ext_entry(gnutls_session_t session,
113
              extensions_t id)
114
0
{
115
0
  unsigned i;
116
117
0
  assert(id < MAX_EXT_TYPES);
118
119
0
  for (i = 0; i < session->internals.rexts_size; i++) {
120
0
    if (session->internals.rexts[i].gid == id) {
121
0
      return &session->internals.rexts[i];
122
0
    }
123
0
  }
124
125
0
  return extfunc[id];
126
0
}
127
128
static const hello_ext_entry_st *
129
tls_id_to_ext_entry(gnutls_session_t session, uint16_t tls_id,
130
        gnutls_ext_parse_type_t parse_point)
131
0
{
132
0
  unsigned i;
133
0
  const hello_ext_entry_st *e;
134
135
0
  for (i = 0; i < session->internals.rexts_size; i++) {
136
0
    if (session->internals.rexts[i].tls_id == tls_id) {
137
0
      e = &session->internals.rexts[i];
138
0
      goto done;
139
0
    }
140
0
  }
141
142
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
143
0
    if (!extfunc[i])
144
0
      continue;
145
146
0
    if (extfunc[i]->tls_id == tls_id) {
147
0
      e = extfunc[i];
148
0
      goto done;
149
0
    }
150
0
  }
151
152
0
  return NULL;
153
0
done:
154
0
  if (parse_point == GNUTLS_EXT_ANY ||
155
0
      (IS_SERVER(session) && e->server_parse_point == parse_point) ||
156
0
      (!IS_SERVER(session) && e->client_parse_point == parse_point)) {
157
0
    return e;
158
0
  } else {
159
0
    return NULL;
160
0
  }
161
0
}
162
163
/**
164
 * gnutls_ext_get_name:
165
 * @ext: is a TLS extension numeric ID
166
 *
167
 * Convert a TLS extension numeric ID to a printable string.
168
 *
169
 * Returns: a pointer to a string that contains the name of the
170
 *   specified cipher, or %NULL.
171
 **/
172
const char *gnutls_ext_get_name(unsigned int ext)
173
0
{
174
0
  size_t i;
175
176
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
177
0
    if (!extfunc[i])
178
0
      continue;
179
180
0
    if (extfunc[i]->tls_id == ext)
181
0
      return extfunc[i]->name;
182
0
  }
183
184
0
  return NULL;
185
0
}
186
187
/* Returns %GNUTLS_EXTENSION_INVALID on error
188
 */
189
static unsigned tls_id_to_gid(gnutls_session_t session, unsigned tls_id)
190
0
{
191
0
  unsigned i;
192
193
0
  for (i = 0; i < session->internals.rexts_size; i++) {
194
0
    if (session->internals.rexts[i].tls_id == tls_id)
195
0
      return session->internals.rexts[i].gid;
196
0
  }
197
198
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
199
0
    if (!extfunc[i])
200
0
      continue;
201
202
0
    if (extfunc[i]->tls_id == tls_id)
203
0
      return extfunc[i]->gid;
204
0
  }
205
206
0
  return GNUTLS_EXTENSION_INVALID;
207
0
}
208
209
typedef struct hello_ext_ctx_st {
210
  gnutls_session_t session;
211
  gnutls_ext_flags_t msg;
212
  gnutls_ext_parse_type_t parse_point;
213
  const hello_ext_entry_st *ext; /* used during send */
214
  unsigned seen_pre_shared_key;
215
} hello_ext_ctx_st;
216
217
static int hello_ext_parse(void *_ctx, unsigned tls_id, const uint8_t *data,
218
         unsigned data_size)
219
0
{
220
0
  hello_ext_ctx_st *ctx = _ctx;
221
0
  gnutls_session_t session = ctx->session;
222
0
  const hello_ext_entry_st *ext;
223
0
  int ret;
224
225
0
  if (tls_id == PRE_SHARED_KEY_TLS_ID) {
226
0
    ctx->seen_pre_shared_key = 1;
227
0
  } else if (ctx->seen_pre_shared_key &&
228
0
       session->security_parameters.entity == GNUTLS_SERVER) {
229
    /* the pre-shared key extension must always be the last one,
230
     * draft-ietf-tls-tls13-28: 4.2.11 */
231
0
    return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
232
0
  }
233
234
0
  ext = tls_id_to_ext_entry(session, tls_id, ctx->parse_point);
235
0
  if (ext == NULL || ext->recv_func == NULL) {
236
0
    goto ignore;
237
0
  }
238
239
  /* we do not hard fail when extensions defined for TLS are used for
240
   * DTLS and vice-versa. They may extend their role in the future. */
241
0
  if (IS_DTLS(session)) {
242
0
    if (!(ext->validity & GNUTLS_EXT_FLAG_DTLS)) {
243
0
      gnutls_assert();
244
0
      goto ignore;
245
0
    }
246
0
  } else {
247
0
    if (!(ext->validity & GNUTLS_EXT_FLAG_TLS)) {
248
0
      gnutls_assert();
249
0
      goto ignore;
250
0
    }
251
0
  }
252
253
0
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
254
0
    if (!(ext->validity & GNUTLS_EXT_FLAG_IGNORE_CLIENT_REQUEST) &&
255
0
        !_gnutls_hello_ext_is_present(session, ext->gid)) {
256
0
      _gnutls_debug_log(
257
0
        "EXT[%p]: Received unexpected extension '%s/%d'\n",
258
0
        session, ext->name, (int)tls_id);
259
0
      return gnutls_assert_val(
260
0
        GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);
261
0
    }
262
0
  }
263
264
0
  if ((ext->validity & ctx->msg) == 0) {
265
0
    _gnutls_debug_log(
266
0
      "EXT[%p]: Received unexpected extension (%s/%d) for '%s'\n",
267
0
      session, ext->name, (int)tls_id,
268
0
      ext_msg_validity_to_str(ctx->msg));
269
0
    return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);
270
0
  }
271
272
0
  if (session->security_parameters.entity == GNUTLS_SERVER) {
273
0
    ret = _gnutls_hello_ext_save(session, ext->gid, 1);
274
0
    if (ret == 0)
275
0
      return gnutls_assert_val(
276
0
        GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);
277
0
  }
278
279
0
  _gnutls_handshake_log("EXT[%p]: Parsing extension '%s/%d' (%d bytes)\n",
280
0
            session, ext->name, (int)tls_id, data_size);
281
282
0
  _gnutls_ext_set_msg(session, ctx->msg);
283
0
  if ((ret = ext->recv_func(session, data, data_size)) < 0) {
284
0
    gnutls_assert();
285
0
    return ret;
286
0
  }
287
288
0
  return 0;
289
290
0
ignore:
291
0
  if (ext) {
292
0
    _gnutls_handshake_log("EXT[%p]: Ignoring extension '%s/%d'\n",
293
0
              session, ext->name, (int)tls_id);
294
0
  }
295
0
  return 0;
296
0
}
297
298
int _gnutls_parse_hello_extensions(gnutls_session_t session,
299
           gnutls_ext_flags_t msg,
300
           gnutls_ext_parse_type_t parse_point,
301
           const uint8_t *data, int data_size)
302
0
{
303
0
  int ret;
304
0
  hello_ext_ctx_st ctx;
305
306
0
  msg &= GNUTLS_EXT_FLAG_SET_ONLY_FLAGS_MASK;
307
308
0
  ctx.session = session;
309
0
  ctx.msg = msg;
310
0
  ctx.parse_point = parse_point;
311
0
  ctx.seen_pre_shared_key = 0;
312
313
0
  ret = _gnutls_extv_parse(&ctx, hello_ext_parse, data, data_size);
314
0
  if (ret < 0)
315
0
    return gnutls_assert_val(ret);
316
317
0
  return 0;
318
0
}
319
320
static int hello_ext_send(void *_ctx, gnutls_buffer_st *buf)
321
0
{
322
0
  hello_ext_ctx_st *ctx = _ctx;
323
0
  int ret;
324
0
  const hello_ext_entry_st *p = ctx->ext;
325
0
  gnutls_session_t session = ctx->session;
326
0
  int appended;
327
0
  size_t size_prev;
328
329
0
  if (unlikely(p->send_func == NULL))
330
0
    return 0;
331
332
0
  if (ctx->parse_point != GNUTLS_EXT_ANY &&
333
0
      ((IS_SERVER(session) && p->server_parse_point != ctx->parse_point) ||
334
0
       (!IS_SERVER(session) &&
335
0
        p->client_parse_point != ctx->parse_point))) {
336
0
    return 0;
337
0
  }
338
339
0
  if (IS_DTLS(session)) {
340
0
    if (!(p->validity & GNUTLS_EXT_FLAG_DTLS)) {
341
0
      gnutls_assert();
342
0
      goto skip;
343
0
    }
344
0
  } else {
345
0
    if (!(p->validity & GNUTLS_EXT_FLAG_TLS)) {
346
0
      gnutls_assert();
347
0
      goto skip;
348
0
    }
349
0
  }
350
351
0
  if ((ctx->msg & p->validity) == 0) {
352
0
    goto skip;
353
0
  } else {
354
0
    _gnutls_handshake_log(
355
0
      "EXT[%p]: Preparing extension (%s/%d) for '%s'\n",
356
0
      session, p->name, (int)p->tls_id,
357
0
      ext_msg_validity_to_str(ctx->msg));
358
0
  }
359
360
  /* ensure we don't send something twice (i.e, overridden extensions in
361
   * client), and ensure we are sending only what we received in server. */
362
0
  ret = _gnutls_hello_ext_is_present(session, p->gid);
363
364
0
  if (session->security_parameters.entity == GNUTLS_SERVER) {
365
    /* if client didn't advertise and the override flag is not present */
366
0
    if (!(p->validity & GNUTLS_EXT_FLAG_IGNORE_CLIENT_REQUEST) &&
367
0
        ret == 0)
368
0
      return 0;
369
0
  } else {
370
0
    if (ret != 0) /* already sent */
371
0
      return 0;
372
0
  }
373
374
0
  size_prev = buf->length;
375
376
0
  _gnutls_ext_set_msg(session, ctx->msg);
377
0
  ret = p->send_func(session, buf);
378
0
  if (ret < 0 && ret != GNUTLS_E_INT_RET_0) {
379
0
    return gnutls_assert_val(ret);
380
0
  }
381
382
0
  appended = buf->length - size_prev;
383
384
  /* add this extension to the extension list, to know which extensions
385
   * to expect.
386
   */
387
0
  if ((appended > 0 || ret == GNUTLS_E_INT_RET_0) &&
388
0
      session->security_parameters.entity == GNUTLS_CLIENT) {
389
0
    _gnutls_hello_ext_save(session, p->gid, 0);
390
0
  }
391
392
0
  return ret;
393
394
0
skip:
395
0
  _gnutls_handshake_log(
396
0
    "EXT[%p]: Not sending extension (%s/%d) for '%s'\n", session,
397
0
    p->name, (int)p->tls_id, ext_msg_validity_to_str(ctx->msg));
398
0
  return 0;
399
0
}
400
401
static inline void swap_exts(extensions_t *exts, size_t index1, size_t index2)
402
0
{
403
0
  extensions_t temp = exts[index1];
404
0
  exts[index1] = exts[index2];
405
0
  exts[index2] = temp;
406
0
}
407
408
static int shuffle_exts(extensions_t *exts, size_t size)
409
0
{
410
0
  uint8_t permutation[MAX_EXT_TYPES];
411
0
  size_t i;
412
0
  int ret;
413
414
0
  assert(size <= MAX_EXT_TYPES);
415
416
  /* Generate random permutation, assuming MAX_EXT_TYPES <
417
   * UINT8_MAX.
418
   */
419
0
  ret = gnutls_rnd(GNUTLS_RND_RANDOM, permutation, size);
420
0
  if (ret < 0)
421
0
    return gnutls_assert_val(ret);
422
423
0
  for (i = size - 1; i > 0; i--) {
424
0
    extensions_t j = permutation[i] % (i + 1);
425
0
    swap_exts(exts, i, j);
426
0
  }
427
428
0
  return 0;
429
0
}
430
431
int _gnutls_gen_hello_extensions(gnutls_session_t session,
432
         gnutls_buffer_st *buf, gnutls_ext_flags_t msg,
433
         gnutls_ext_parse_type_t parse_point)
434
0
{
435
0
  int pos, ret;
436
0
  size_t i;
437
0
  hello_ext_ctx_st ctx;
438
  /* To shuffle extension sending order */
439
0
  extensions_t indices[MAX_EXT_TYPES];
440
441
0
  msg &= GNUTLS_EXT_FLAG_SET_ONLY_FLAGS_MASK;
442
443
0
  ctx.session = session;
444
0
  ctx.msg = msg;
445
0
  ctx.parse_point = parse_point;
446
447
0
  ret = _gnutls_extv_append_init(buf);
448
0
  if (ret < 0)
449
0
    return gnutls_assert_val(ret);
450
451
0
  pos = ret;
452
0
  _gnutls_ext_set_extensions_offset(session, pos);
453
454
0
  for (i = 0; i < session->internals.rexts_size; i++) {
455
0
    ctx.ext = &session->internals.rexts[i];
456
0
    ret = _gnutls_extv_append(buf,
457
0
            session->internals.rexts[i].tls_id,
458
0
            &ctx, hello_ext_send);
459
0
    if (ret < 0)
460
0
      return gnutls_assert_val(ret);
461
462
0
    if (ret > 0)
463
0
      _gnutls_handshake_log(
464
0
        "EXT[%p]: Sending extension %s/%d (%d bytes)\n",
465
0
        session, ctx.ext->name, (int)ctx.ext->tls_id,
466
0
        ret - 4);
467
0
  }
468
469
  /* Initializing extensions array */
470
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
471
0
    indices[i] = i;
472
0
  }
473
474
0
  if (!session->internals.priorities->no_shuffle_extensions) {
475
    /* Ordering padding and pre_shared_key as last extensions */
476
0
    swap_exts(indices, MAX_EXT_TYPES - 2, GNUTLS_EXTENSION_DUMBFW);
477
0
    swap_exts(indices, MAX_EXT_TYPES - 1,
478
0
        GNUTLS_EXTENSION_PRE_SHARED_KEY);
479
480
0
    ret = shuffle_exts(indices, MAX_EXT_TYPES - 2);
481
0
    if (ret < 0)
482
0
      return gnutls_assert_val(ret);
483
0
  }
484
485
  /* hello_ext_send() ensures we don't send duplicates, in case
486
   * of overridden extensions */
487
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
488
0
    size_t ii = indices[i];
489
0
    if (!extfunc[ii])
490
0
      continue;
491
492
0
    ctx.ext = extfunc[ii];
493
0
    ret = _gnutls_extv_append(buf, extfunc[ii]->tls_id, &ctx,
494
0
            hello_ext_send);
495
0
    if (ret < 0)
496
0
      return gnutls_assert_val(ret);
497
498
0
    if (ret > 0)
499
0
      _gnutls_handshake_log(
500
0
        "EXT[%p]: Sending extension %s/%d (%d bytes)\n",
501
0
        session, ctx.ext->name, (int)ctx.ext->tls_id,
502
0
        ret - 4);
503
0
  }
504
505
0
  ret = _gnutls_extv_append_final(buf, pos, !(msg & GNUTLS_EXT_FLAG_EE));
506
0
  if (ret < 0)
507
0
    return gnutls_assert_val(ret);
508
509
0
  return 0;
510
0
}
511
512
/* Global deinit and init of global extensions */
513
int _gnutls_hello_ext_init(void)
514
2
{
515
2
  return GNUTLS_E_SUCCESS;
516
2
}
517
518
void _gnutls_hello_ext_deinit(void)
519
0
{
520
0
  unsigned i;
521
522
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
523
0
    if (!extfunc[i])
524
0
      continue;
525
526
0
    if (extfunc[i]->free_struct != 0) {
527
0
      gnutls_free(((hello_ext_entry_st *)extfunc[i])->name);
528
0
      gnutls_free(extfunc[i]);
529
0
    }
530
0
  }
531
0
}
532
533
/* Packing of extension data (for use in resumption) */
534
static int pack_extension(gnutls_session_t session,
535
        const hello_ext_entry_st *extp,
536
        gnutls_buffer_st *packed)
537
0
{
538
0
  int ret;
539
0
  int size_offset;
540
0
  int cur_size;
541
0
  gnutls_ext_priv_data_t data;
542
0
  int rval = 0;
543
544
0
  ret = _gnutls_hello_ext_get_priv(session, extp->gid, &data);
545
0
  if (ret >= 0 && extp->pack_func != NULL) {
546
0
    BUFFER_APPEND_NUM(packed, extp->gid);
547
548
0
    size_offset = packed->length;
549
0
    BUFFER_APPEND_NUM(packed, 0);
550
551
0
    cur_size = packed->length;
552
553
0
    ret = extp->pack_func(data, packed);
554
0
    if (ret < 0) {
555
0
      gnutls_assert();
556
0
      return ret;
557
0
    }
558
559
0
    rval = 1;
560
    /* write the actual size */
561
0
    _gnutls_write_uint32(packed->length - cur_size,
562
0
             packed->data + size_offset);
563
0
  }
564
565
0
  return rval;
566
0
}
567
568
int _gnutls_hello_ext_pack(gnutls_session_t session, gnutls_buffer_st *packed)
569
0
{
570
0
  unsigned int i;
571
0
  int ret;
572
0
  int total_exts_pos;
573
0
  int n_exts = 0;
574
0
  const struct hello_ext_entry_st *ext;
575
576
0
  total_exts_pos = packed->length;
577
0
  BUFFER_APPEND_NUM(packed, 0);
578
579
0
  for (i = 0; i <= GNUTLS_EXTENSION_MAX_VALUE; i++) {
580
0
    if (session->internals.used_exts & ((ext_track_t)1 << i)) {
581
0
      ext = gid_to_ext_entry(session, i);
582
0
      if (ext == NULL)
583
0
        continue;
584
585
0
      ret = pack_extension(session, ext, packed);
586
0
      if (ret < 0)
587
0
        return gnutls_assert_val(ret);
588
589
0
      if (ret > 0)
590
0
        n_exts++;
591
0
    }
592
0
  }
593
594
0
  _gnutls_write_uint32(n_exts, packed->data + total_exts_pos);
595
596
0
  return 0;
597
0
}
598
599
int _gnutls_ext_set_full_client_hello(gnutls_session_t session,
600
              handshake_buffer_st *recv_buf)
601
0
{
602
0
  int ret;
603
0
  gnutls_buffer_st *buf = &session->internals.full_client_hello;
604
605
0
  _gnutls_buffer_clear(buf);
606
607
0
  if ((ret = _gnutls_buffer_append_prefix(buf, 8, recv_buf->htype)) < 0)
608
0
    return gnutls_assert_val(ret);
609
0
  if ((ret = _gnutls_buffer_append_prefix(buf, 24,
610
0
            recv_buf->data.length)) < 0)
611
0
    return gnutls_assert_val(ret);
612
0
  if ((ret = _gnutls_buffer_append_data(buf, recv_buf->data.data,
613
0
                recv_buf->data.length)) < 0)
614
0
    return gnutls_assert_val(ret);
615
616
0
  return 0;
617
0
}
618
619
unsigned _gnutls_ext_get_full_client_hello(gnutls_session_t session,
620
             gnutls_datum_t *d)
621
0
{
622
0
  gnutls_buffer_st *buf = &session->internals.full_client_hello;
623
624
0
  if (!buf->length)
625
0
    return 0;
626
627
0
  d->data = buf->data;
628
0
  d->size = buf->length;
629
630
0
  return 1;
631
0
}
632
633
static void _gnutls_ext_set_resumed_session_data(gnutls_session_t session,
634
             extensions_t id,
635
             gnutls_ext_priv_data_t data)
636
0
{
637
0
  const struct hello_ext_entry_st *ext;
638
639
  /* If this happens we need to increase the max */
640
0
  assert(id < MAX_EXT_TYPES);
641
642
0
  ext = gid_to_ext_entry(session, id);
643
0
  assert(ext != NULL);
644
645
0
  if (session->internals.ext_data[id].resumed_set != 0)
646
0
    unset_resumed_ext_data(session, ext, id);
647
648
0
  session->internals.ext_data[id].resumed_priv = data;
649
0
  session->internals.ext_data[id].resumed_set = 1;
650
0
  return;
651
0
}
652
653
int _gnutls_hello_ext_unpack(gnutls_session_t session, gnutls_buffer_st *packed)
654
0
{
655
0
  int i, ret;
656
0
  gnutls_ext_priv_data_t data;
657
0
  int max_exts = 0;
658
0
  extensions_t id;
659
0
  int size_for_id, cur_pos;
660
0
  const struct hello_ext_entry_st *ext;
661
662
0
  BUFFER_POP_NUM(packed, max_exts);
663
0
  for (i = 0; i < max_exts; i++) {
664
0
    BUFFER_POP_NUM(packed, id);
665
0
    BUFFER_POP_NUM(packed, size_for_id);
666
667
0
    cur_pos = packed->length;
668
669
0
    ext = gid_to_ext_entry(session, id);
670
0
    if (ext == NULL || ext->unpack_func == NULL) {
671
0
      gnutls_assert();
672
0
      return GNUTLS_E_PARSING_ERROR;
673
0
    }
674
675
0
    ret = ext->unpack_func(packed, &data);
676
0
    if (ret < 0) {
677
0
      gnutls_assert();
678
0
      return ret;
679
0
    }
680
681
    /* verify that unpack read the correct bytes */
682
0
    cur_pos = cur_pos - packed->length;
683
0
    if (cur_pos /* read length */ != size_for_id) {
684
0
      gnutls_assert();
685
0
      return GNUTLS_E_PARSING_ERROR;
686
0
    }
687
688
0
    _gnutls_ext_set_resumed_session_data(session, id, data);
689
0
  }
690
691
0
  return 0;
692
693
0
error:
694
0
  return ret;
695
0
}
696
697
static void unset_ext_data(gnutls_session_t session,
698
         const struct hello_ext_entry_st *ext, unsigned idx)
699
0
{
700
0
  if (session->internals.ext_data[idx].set == 0)
701
0
    return;
702
703
0
  if (ext && ext->deinit_func &&
704
0
      session->internals.ext_data[idx].priv != NULL)
705
0
    ext->deinit_func(session->internals.ext_data[idx].priv);
706
0
  session->internals.ext_data[idx].set = 0;
707
0
}
708
709
void _gnutls_hello_ext_unset_priv(gnutls_session_t session, extensions_t id)
710
0
{
711
0
  const struct hello_ext_entry_st *ext;
712
713
0
  ext = gid_to_ext_entry(session, id);
714
0
  if (ext)
715
0
    unset_ext_data(session, ext, id);
716
0
}
717
718
static void unset_resumed_ext_data(gnutls_session_t session,
719
           const struct hello_ext_entry_st *ext,
720
           unsigned idx)
721
0
{
722
0
  if (session->internals.ext_data[idx].resumed_set == 0)
723
0
    return;
724
725
0
  if (ext && ext->deinit_func &&
726
0
      session->internals.ext_data[idx].resumed_priv) {
727
0
    ext->deinit_func(session->internals.ext_data[idx].resumed_priv);
728
0
  }
729
0
  session->internals.ext_data[idx].resumed_set = 0;
730
0
}
731
732
/* Deinitializes all data that are associated with TLS extensions.
733
 */
734
void _gnutls_hello_ext_priv_deinit(gnutls_session_t session)
735
0
{
736
0
  unsigned int i;
737
0
  const struct hello_ext_entry_st *ext;
738
739
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
740
0
    if (!session->internals.ext_data[i].set &&
741
0
        !session->internals.ext_data[i].resumed_set)
742
0
      continue;
743
744
0
    ext = gid_to_ext_entry(session, i);
745
0
    if (ext) {
746
0
      unset_ext_data(session, ext, i);
747
0
      unset_resumed_ext_data(session, ext, i);
748
0
    }
749
0
  }
750
0
}
751
752
/* This function allows an extension to store data in the current session
753
 * and retrieve them later on. We use functions instead of a pointer to a
754
 * private pointer, to allow API additions by individual extensions.
755
 */
756
void _gnutls_hello_ext_set_priv(gnutls_session_t session, extensions_t id,
757
        gnutls_ext_priv_data_t data)
758
0
{
759
0
  const struct hello_ext_entry_st *ext;
760
761
0
  assert(id < MAX_EXT_TYPES);
762
763
0
  ext = gid_to_ext_entry(session, id);
764
0
  assert(ext != NULL);
765
766
0
  if (session->internals.ext_data[id].set != 0) {
767
0
    unset_ext_data(session, ext, id);
768
0
  }
769
0
  session->internals.ext_data[id].priv = data;
770
0
  session->internals.ext_data[id].set = 1;
771
772
0
  return;
773
0
}
774
775
int _gnutls_hello_ext_get_priv(gnutls_session_t session, extensions_t id,
776
             gnutls_ext_priv_data_t *data)
777
0
{
778
0
  if (session->internals.ext_data[id].set != 0) {
779
0
    *data = session->internals.ext_data[id].priv;
780
0
    return 0;
781
0
  }
782
783
0
  return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
784
0
}
785
786
int _gnutls_hello_ext_get_resumed_priv(gnutls_session_t session,
787
               extensions_t id,
788
               gnutls_ext_priv_data_t *data)
789
0
{
790
0
  if (session->internals.ext_data[id].resumed_set != 0) {
791
0
    *data = session->internals.ext_data[id].resumed_priv;
792
0
    return 0;
793
0
  }
794
795
0
  return GNUTLS_E_INVALID_REQUEST;
796
0
}
797
798
/**
799
 * gnutls_ext_register:
800
 * @name: the name of the extension to register
801
 * @id: the numeric TLS id of the extension
802
 * @parse_point: the parse type of the extension (see gnutls_ext_parse_type_t)
803
 * @recv_func: a function to receive the data
804
 * @send_func: a function to send the data
805
 * @deinit_func: a function deinitialize any private data
806
 * @pack_func: a function which serializes the extension's private data (used on session packing for resumption)
807
 * @unpack_func: a function which will deserialize the extension's private data
808
 *
809
 * This function will register a new extension type. The extension will remain
810
 * registered until gnutls_global_deinit() is called. If the extension type
811
 * is already registered then %GNUTLS_E_ALREADY_REGISTERED will be returned.
812
 *
813
 * Each registered extension can store temporary data into the gnutls_session_t
814
 * structure using gnutls_ext_set_data(), and they can be retrieved using
815
 * gnutls_ext_get_data().
816
 *
817
 * Any extensions registered with this function are valid for the client
818
 * and TLS1.2 server hello (or encrypted extensions for TLS1.3).
819
 *
820
 * This function is not thread safe.
821
 *
822
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
823
 *
824
 * Since: 3.4.0
825
 **/
826
int gnutls_ext_register(const char *name, int id,
827
      gnutls_ext_parse_type_t parse_point,
828
      gnutls_ext_recv_func recv_func,
829
      gnutls_ext_send_func send_func,
830
      gnutls_ext_deinit_data_func deinit_func,
831
      gnutls_ext_pack_func pack_func,
832
      gnutls_ext_unpack_func unpack_func)
833
0
{
834
0
  hello_ext_entry_st *tmp_mod;
835
0
  unsigned i;
836
0
  unsigned gid = GNUTLS_EXTENSION_MAX + 1;
837
838
0
  for (i = 0; i < MAX_EXT_TYPES; i++) {
839
0
    if (!extfunc[i])
840
0
      continue;
841
842
0
    if (extfunc[i]->tls_id == id)
843
0
      return gnutls_assert_val(GNUTLS_E_ALREADY_REGISTERED);
844
845
0
    if (extfunc[i]->gid >= gid)
846
0
      gid = extfunc[i]->gid + 1;
847
0
  }
848
849
0
  assert(gid < sizeof(extfunc) / sizeof(extfunc[0]));
850
0
  if (gid > GNUTLS_EXTENSION_MAX_VALUE)
851
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
852
853
0
  tmp_mod = gnutls_calloc(1, sizeof(*tmp_mod));
854
0
  if (tmp_mod == NULL)
855
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
856
857
0
  tmp_mod->name = gnutls_strdup(name);
858
0
  tmp_mod->free_struct = 1;
859
0
  tmp_mod->tls_id = id;
860
0
  tmp_mod->gid = gid;
861
0
  tmp_mod->client_parse_point = parse_point;
862
0
  tmp_mod->server_parse_point = parse_point;
863
0
  tmp_mod->recv_func = recv_func;
864
0
  tmp_mod->send_func = send_func;
865
0
  tmp_mod->deinit_func = deinit_func;
866
0
  tmp_mod->pack_func = pack_func;
867
0
  tmp_mod->unpack_func = unpack_func;
868
0
  tmp_mod->validity = GNUTLS_EXT_FLAG_CLIENT_HELLO |
869
0
          GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO |
870
0
          GNUTLS_EXT_FLAG_EE | GNUTLS_EXT_FLAG_DTLS |
871
0
          GNUTLS_EXT_FLAG_TLS;
872
873
0
  assert(extfunc[gid] == NULL);
874
0
  extfunc[gid] = tmp_mod;
875
876
0
  return 0;
877
0
}
878
879
#define VALIDITY_MASK                                                        \
880
0
  (GNUTLS_EXT_FLAG_CLIENT_HELLO | GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO | \
881
0
   GNUTLS_EXT_FLAG_TLS13_SERVER_HELLO | GNUTLS_EXT_FLAG_EE |           \
882
0
   GNUTLS_EXT_FLAG_HRR)
883
884
/**
885
 * gnutls_session_ext_register:
886
 * @session: the session for which this extension will be set
887
 * @name: the name of the extension to register
888
 * @id: the numeric id of the extension
889
 * @parse_point: the parse type of the extension (see gnutls_ext_parse_type_t)
890
 * @recv_func: a function to receive the data
891
 * @send_func: a function to send the data
892
 * @deinit_func: a function deinitialize any private data
893
 * @pack_func: a function which serializes the extension's private data (used on session packing for resumption)
894
 * @unpack_func: a function which will deserialize the extension's private data
895
 * @flags: must be zero or flags from %gnutls_ext_flags_t
896
 *
897
 * This function will register a new extension type. The extension will be
898
 * only usable within the registered session. If the extension type
899
 * is already registered then %GNUTLS_E_ALREADY_REGISTERED will be returned,
900
 * unless the flag %GNUTLS_EXT_FLAG_OVERRIDE_INTERNAL is specified. The latter
901
 * flag when specified can be used to override certain extensions introduced
902
 * after 3.6.0. It is expected to be used by applications which handle
903
 * custom extensions that are not currently supported in GnuTLS, but direct
904
 * support for them may be added in the future.
905
 *
906
 * Each registered extension can store temporary data into the gnutls_session_t
907
 * structure using gnutls_ext_set_data(), and they can be retrieved using
908
 * gnutls_ext_get_data().
909
 *
910
 * The validity of the extension registered can be given by the appropriate flags
911
 * of %gnutls_ext_flags_t. If no validity is given, then the registered extension
912
 * will be valid for client and TLS1.2 server hello (or encrypted extensions for TLS1.3).
913
 *
914
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
915
 *
916
 * Since: 3.5.5
917
 **/
918
int gnutls_session_ext_register(gnutls_session_t session, const char *name,
919
        int id, gnutls_ext_parse_type_t parse_point,
920
        gnutls_ext_recv_func recv_func,
921
        gnutls_ext_send_func send_func,
922
        gnutls_ext_deinit_data_func deinit_func,
923
        gnutls_ext_pack_func pack_func,
924
        gnutls_ext_unpack_func unpack_func,
925
        unsigned flags)
926
0
{
927
0
  hello_ext_entry_st tmp_mod;
928
0
  hello_ext_entry_st *exts;
929
0
  unsigned i;
930
0
  unsigned gid = GNUTLS_EXTENSION_MAX + 1;
931
932
  /* reject handling any extensions which modify the TLS handshake
933
   * in any way, or are mapped to an exported API. */
934
0
  for (i = 0; i < GNUTLS_EXTENSION_MAX; i++) {
935
0
    if (!extfunc[i])
936
0
      continue;
937
938
0
    if (extfunc[i]->tls_id == id) {
939
0
      if (!(flags & GNUTLS_EXT_FLAG_OVERRIDE_INTERNAL)) {
940
0
        return gnutls_assert_val(
941
0
          GNUTLS_E_ALREADY_REGISTERED);
942
0
      } else if (extfunc[i]->cannot_be_overriden) {
943
0
        return gnutls_assert_val(
944
0
          GNUTLS_E_ALREADY_REGISTERED);
945
0
      }
946
0
      break;
947
0
    }
948
949
0
    if (extfunc[i]->gid >= gid)
950
0
      gid = extfunc[i]->gid + 1;
951
0
  }
952
953
0
  for (i = 0; i < session->internals.rexts_size; i++) {
954
0
    if (session->internals.rexts[i].tls_id == id) {
955
0
      return gnutls_assert_val(GNUTLS_E_ALREADY_REGISTERED);
956
0
    }
957
958
0
    if (session->internals.rexts[i].gid >= gid)
959
0
      gid = session->internals.rexts[i].gid + 1;
960
0
  }
961
962
0
  if (gid > GNUTLS_EXTENSION_MAX_VALUE)
963
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
964
965
0
  memset(&tmp_mod, 0, sizeof(hello_ext_entry_st));
966
0
  tmp_mod.name = gnutls_strdup(name);
967
0
  tmp_mod.free_struct = 1;
968
0
  tmp_mod.tls_id = id;
969
0
  tmp_mod.gid = gid;
970
0
  tmp_mod.client_parse_point = parse_point;
971
0
  tmp_mod.server_parse_point = parse_point;
972
0
  tmp_mod.recv_func = recv_func;
973
0
  tmp_mod.send_func = send_func;
974
0
  tmp_mod.deinit_func = deinit_func;
975
0
  tmp_mod.pack_func = pack_func;
976
0
  tmp_mod.unpack_func = unpack_func;
977
0
  tmp_mod.validity = flags;
978
979
0
  if ((tmp_mod.validity & VALIDITY_MASK) == 0) {
980
0
    tmp_mod.validity = GNUTLS_EXT_FLAG_CLIENT_HELLO |
981
0
           GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO |
982
0
           GNUTLS_EXT_FLAG_EE;
983
0
  }
984
985
0
  if ((tmp_mod.validity & (GNUTLS_EXT_FLAG_DTLS | GNUTLS_EXT_FLAG_TLS)) ==
986
0
      0) {
987
0
    if (IS_DTLS(session))
988
0
      tmp_mod.validity |= GNUTLS_EXT_FLAG_DTLS;
989
0
    else
990
0
      tmp_mod.validity |= GNUTLS_EXT_FLAG_TLS;
991
0
  }
992
993
0
  if (unlikely(INT_ADD_OVERFLOW(session->internals.rexts_size, 1))) {
994
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
995
0
  }
996
997
0
  exts = _gnutls_reallocarray(session->internals.rexts,
998
0
            session->internals.rexts_size + 1,
999
0
            sizeof(*exts));
1000
0
  if (exts == NULL) {
1001
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1002
0
  }
1003
1004
0
  session->internals.rexts = exts;
1005
1006
0
  memcpy(&session->internals.rexts[session->internals.rexts_size],
1007
0
         &tmp_mod, sizeof(hello_ext_entry_st));
1008
0
  session->internals.rexts_size++;
1009
1010
0
  return 0;
1011
0
}
1012
1013
/**
1014
 * gnutls_ext_set_data:
1015
 * @session: a #gnutls_session_t opaque pointer
1016
 * @tls_id: the numeric id of the extension
1017
 * @data: the private data to set
1018
 *
1019
 * This function allows an extension handler to store data in the current session
1020
 * and retrieve them later on. The set data will be deallocated using
1021
 * the gnutls_ext_deinit_data_func.
1022
 *
1023
 * Since: 3.4.0
1024
 **/
1025
void gnutls_ext_set_data(gnutls_session_t session, unsigned tls_id,
1026
       gnutls_ext_priv_data_t data)
1027
0
{
1028
0
  unsigned id = tls_id_to_gid(session, tls_id);
1029
0
  if (id == GNUTLS_EXTENSION_INVALID)
1030
0
    return;
1031
1032
0
  _gnutls_hello_ext_set_priv(session, id, data);
1033
0
}
1034
1035
/**
1036
 * gnutls_ext_get_data:
1037
 * @session: a #gnutls_session_t opaque pointer
1038
 * @tls_id: the numeric id of the extension
1039
 * @data: a pointer to the private data to retrieve
1040
 *
1041
 * This function retrieves any data previously stored with gnutls_ext_set_data().
1042
 *
1043
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
1044
 *
1045
 * Since: 3.4.0
1046
 **/
1047
int gnutls_ext_get_data(gnutls_session_t session, unsigned tls_id,
1048
      gnutls_ext_priv_data_t *data)
1049
0
{
1050
0
  unsigned id = tls_id_to_gid(session, tls_id);
1051
0
  if (id == GNUTLS_EXTENSION_INVALID)
1052
0
    return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
1053
1054
0
  return _gnutls_hello_ext_get_priv(session, id, data);
1055
0
}
1056
1057
/**
1058
 * gnutls_ext_get_current_msg:
1059
 * @session: a #gnutls_session_t opaque pointer
1060
 *
1061
 * This function allows an extension handler to obtain the message
1062
 * this extension is being called from. The returned value is a single
1063
 * entry of the %gnutls_ext_flags_t enumeration. That is, if an
1064
 * extension was registered with the %GNUTLS_EXT_FLAG_HRR and
1065
 * %GNUTLS_EXT_FLAG_EE flags, the value when called during parsing of the
1066
 * encrypted extensions message will be %GNUTLS_EXT_FLAG_EE.
1067
 *
1068
 * If not called under an extension handler, its value is undefined.
1069
 *
1070
 * Since: 3.6.3
1071
 **/
1072
unsigned gnutls_ext_get_current_msg(gnutls_session_t session)
1073
0
{
1074
0
  return _gnutls_ext_get_msg(session);
1075
0
}
1076
1077
/**
1078
 * gnutls_ext_get_name2:
1079
 * @session: a #gnutls_session_t opaque pointer
1080
 * @tls_id: is a TLS extension numeric ID
1081
 * @parse_point: the parse type of the extension
1082
 *
1083
 * Convert a TLS extension numeric ID to a printable string.
1084
 *
1085
 * Returns: a pointer to a string that contains the name of the
1086
 *   specified cipher, or %NULL.
1087
 **/
1088
const char *gnutls_ext_get_name2(gnutls_session_t session, unsigned int tls_id,
1089
         gnutls_ext_parse_type_t parse_point)
1090
0
{
1091
0
  const struct hello_ext_entry_st *ext;
1092
1093
0
  ext = tls_id_to_ext_entry(session, tls_id, parse_point);
1094
0
  if (ext)
1095
0
    return ext->name;
1096
1097
0
  return NULL;
1098
0
}