Coverage Report

Created: 2026-05-16 06:55

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