Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/state.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2002-2016 Free Software Foundation, Inc.
3
 * Copyright (C) 2014-2016 Nikos Mavrogiannopoulos
4
 * Copyright (C) 2015-2018 Red Hat, Inc.
5
 *
6
 * Author: Nikos Mavrogiannopoulos
7
 *
8
 * This file is part of GnuTLS.
9
 *
10
 * The GnuTLS is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public License
12
 * as published by the Free Software Foundation; either version 2.1 of
13
 * the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
22
 *
23
 */
24
25
/* Functions to manipulate the session (gnutls_int.h), and some other stuff
26
 * are included here. The file's name is traditionally gnutls_state even if the
27
 * state has been renamed to session.
28
 */
29
30
#include "gnutls_int.h"
31
#include "errors.h"
32
#include "auth.h"
33
#include "num.h"
34
#include "datum.h"
35
#include "db.h"
36
#include "record.h"
37
#include "handshake.h"
38
#include "dh.h"
39
#include "buffers.h"
40
#include "mbuffers.h"
41
#include "state.h"
42
#include "constate.h"
43
#include "auth/cert.h"
44
#include "auth/anon.h"
45
#include "auth/psk.h"
46
#include "algorithms.h"
47
#include "hello_ext.h"
48
#include "system.h"
49
#include "random.h"
50
#include "fips.h"
51
#include <intprops.h>
52
#include <gnutls/dtls.h>
53
#include "dtls.h"
54
#include "tls13/session_ticket.h"
55
#include "ext/cert_types.h"
56
#include "locks.h"
57
#include "kx.h"
58
59
/* to be used by supplemental data support to disable TLS1.3
60
 * when supplemental data have been globally registered */
61
unsigned _gnutls_disable_tls13 = 0;
62
63
/* These should really be static, but src/tests.c calls them.  Make
64
   them public functions?  */
65
void _gnutls_rsa_pms_set_version(gnutls_session_t session, unsigned char major,
66
         unsigned char minor);
67
68
/**
69
 * gnutls_cipher_get:
70
 * @session: is a #gnutls_session_t type.
71
 *
72
 * Get the currently used cipher.
73
 *
74
 * Returns: the currently used cipher, a #gnutls_cipher_algorithm_t
75
 *   type.
76
 **/
77
gnutls_cipher_algorithm_t gnutls_cipher_get(gnutls_session_t session)
78
0
{
79
0
  record_parameters_st *record_params;
80
0
  int ret;
81
82
0
  ret = _gnutls_epoch_get(session, EPOCH_READ_CURRENT, &record_params);
83
0
  if (ret < 0)
84
0
    return gnutls_assert_val(GNUTLS_CIPHER_NULL);
85
86
0
  return record_params->cipher->id;
87
0
}
88
89
/**
90
 * gnutls_early_cipher_get:
91
 * @session: is a #gnutls_session_t type.
92
 *
93
 * Get the cipher algorithm used for encrypting early data.
94
 *
95
 * Returns: the cipher used for early data, a
96
 *   #gnutls_cipher_algorithm_t type.
97
 *
98
 * Since: 3.7.2
99
 **/
100
gnutls_cipher_algorithm_t gnutls_early_cipher_get(gnutls_session_t session)
101
0
{
102
0
  const cipher_entry_st *ce;
103
104
0
  if (!(session->internals.hsk_flags & HSK_EARLY_DATA_IN_FLIGHT)) {
105
0
    return gnutls_assert_val(GNUTLS_CIPHER_UNKNOWN);
106
0
  }
107
108
0
  if (unlikely(session->internals.resumed_security_parameters.cs ==
109
0
         NULL)) {
110
0
    return gnutls_assert_val(GNUTLS_CIPHER_UNKNOWN);
111
0
  }
112
113
0
  ce = cipher_to_entry(session->internals.resumed_security_parameters.cs
114
0
             ->block_algorithm);
115
0
  if (unlikely(ce == NULL)) {
116
0
    return gnutls_assert_val(GNUTLS_CIPHER_UNKNOWN);
117
0
  }
118
119
0
  return ce->id;
120
0
}
121
122
/**
123
 * gnutls_certificate_type_get:
124
 * @session: is a #gnutls_session_t type.
125
 *
126
 * This function returns the type of the certificate that is negotiated
127
 * for this side to send to the peer. The certificate type is by default
128
 * X.509, unless an alternative certificate type is enabled by
129
 * gnutls_init() and negotiated during the session.
130
 *
131
 * Resumed sessions will return the certificate type that was negotiated
132
 * and used in the original session.
133
 *
134
 * As of version 3.6.4 it is recommended to use
135
 * gnutls_certificate_type_get2() which is more fine-grained.
136
 *
137
 * Returns: the currently used #gnutls_certificate_type_t certificate
138
 *   type as negotiated for 'our' side of the connection.
139
 **/
140
gnutls_certificate_type_t gnutls_certificate_type_get(gnutls_session_t session)
141
0
{
142
0
  return gnutls_certificate_type_get2(session, GNUTLS_CTYPE_OURS);
143
0
}
144
145
/**
146
 * gnutls_certificate_type_get2:
147
 * @session: is a #gnutls_session_t type.
148
 * @target: is a #gnutls_ctype_target_t type.
149
 *
150
 * This function returns the type of the certificate that a side
151
 * is negotiated to use.  The certificate type is by default X.509,
152
 * unless an alternative certificate type is enabled by gnutls_init() and
153
 * negotiated during the session.
154
 *
155
 * The @target parameter specifies whether to request the negotiated
156
 * certificate type for the client (%GNUTLS_CTYPE_CLIENT),
157
 * or for the server (%GNUTLS_CTYPE_SERVER). Additionally, in P2P mode
158
 * connection set up where you don't know in advance who will be client
159
 * and who will be server you can use the flag (%GNUTLS_CTYPE_OURS) and
160
 * (%GNUTLS_CTYPE_PEERS) to retrieve the corresponding certificate types.
161
 *
162
 * Resumed sessions will return the certificate type that was negotiated
163
 * and used in the original session. That is, this function can be used
164
 * to reliably determine the type of the certificate returned by
165
 * gnutls_certificate_get_peers().
166
 *
167
 * Returns: the currently used #gnutls_certificate_type_t certificate
168
 *   type for the client or the server.
169
 *
170
 * Since: 3.6.4
171
 **/
172
gnutls_certificate_type_t
173
gnutls_certificate_type_get2(gnutls_session_t session,
174
           gnutls_ctype_target_t target)
175
0
{
176
  /* We want to inline this function so therefore
177
   * we've defined it in gnutls_int.h */
178
0
  return get_certificate_type(session, target);
179
0
}
180
181
/**
182
 * gnutls_kx_get:
183
 * @session: is a #gnutls_session_t type.
184
 *
185
 * Get the currently used key exchange algorithm.
186
 *
187
 * This function will return %GNUTLS_KX_ECDHE_RSA, or %GNUTLS_KX_DHE_RSA
188
 * under TLS 1.3, to indicate an elliptic curve DH key exchange or
189
 * a finite field one. The precise group used is available
190
 * by calling gnutls_group_get() instead.
191
 *
192
 * Returns: the key exchange algorithm used in the last handshake, a
193
 *   #gnutls_kx_algorithm_t value.
194
 **/
195
gnutls_kx_algorithm_t gnutls_kx_get(gnutls_session_t session)
196
0
{
197
0
  if (session->security_parameters.cs == 0)
198
0
    return 0;
199
200
0
  if (session->security_parameters.cs->kx_algorithm == 0) { /* TLS 1.3 */
201
0
    const version_entry_st *ver = get_version(session);
202
0
    const gnutls_group_entry_st *group = get_group(session);
203
204
0
    if (ver->tls13_sem) {
205
0
      if (session->internals.hsk_flags & HSK_PSK_SELECTED) {
206
0
        if (group) {
207
0
          if (group->pk == GNUTLS_PK_DH)
208
0
            return GNUTLS_KX_DHE_PSK;
209
0
          else
210
0
            return GNUTLS_KX_ECDHE_PSK;
211
0
        } else {
212
0
          return GNUTLS_KX_PSK;
213
0
        }
214
0
      } else if (group) {
215
0
        if (group->pk == GNUTLS_PK_DH)
216
0
          return GNUTLS_KX_DHE_RSA;
217
0
        else
218
0
          return GNUTLS_KX_ECDHE_RSA;
219
0
      }
220
0
    }
221
0
  }
222
223
0
  return session->security_parameters.cs->kx_algorithm;
224
0
}
225
226
/**
227
 * gnutls_mac_get:
228
 * @session: is a #gnutls_session_t type.
229
 *
230
 * Get the currently used MAC algorithm.
231
 *
232
 * Returns: the currently used mac algorithm, a
233
 *   #gnutls_mac_algorithm_t value.
234
 **/
235
gnutls_mac_algorithm_t gnutls_mac_get(gnutls_session_t session)
236
0
{
237
0
  record_parameters_st *record_params;
238
0
  int ret;
239
240
0
  ret = _gnutls_epoch_get(session, EPOCH_READ_CURRENT, &record_params);
241
0
  if (ret < 0)
242
0
    return gnutls_assert_val(GNUTLS_MAC_NULL);
243
244
0
  return record_params->mac->id;
245
0
}
246
247
/**
248
 * gnutls_compression_get:
249
 * @session: is a #gnutls_session_t type.
250
 *
251
 * Get the currently used compression algorithm.
252
 *
253
 * Returns: the currently used compression method, a
254
 *   #gnutls_compression_method_t value.
255
 **/
256
gnutls_compression_method_t gnutls_compression_get(gnutls_session_t session)
257
0
{
258
0
  return GNUTLS_COMP_NULL;
259
0
}
260
261
/**
262
 * gnutls_prf_hash_get:
263
 * @session: is a #gnutls_session_t type.
264
 *
265
 * Get the currently used hash algorithm. In TLS 1.3, the hash
266
 * algorithm is used for both the key derivation function and
267
 * handshake message authentication code. In TLS 1.2, it matches the
268
 * hash algorithm used for PRF.
269
 *
270
 * Returns: the currently used hash algorithm, a
271
 *    #gnutls_digest_algorithm_t value.
272
 *
273
 * Since: 3.6.13
274
 **/
275
gnutls_digest_algorithm_t gnutls_prf_hash_get(const gnutls_session_t session)
276
0
{
277
0
  if (session->security_parameters.prf == NULL)
278
0
    return gnutls_assert_val(GNUTLS_DIG_UNKNOWN);
279
280
0
  if (session->security_parameters.prf->id >= GNUTLS_MAC_AEAD)
281
0
    return gnutls_assert_val(GNUTLS_DIG_UNKNOWN);
282
283
0
  return (gnutls_digest_algorithm_t)session->security_parameters.prf->id;
284
0
}
285
286
/**
287
 * gnutls_early_prf_hash_get:
288
 * @session: is a #gnutls_session_t type.
289
 *
290
 * Get the hash algorithm used as a PRF to derive keys for encrypting
291
 * early data in TLS 1.3.
292
 *
293
 * Returns: the hash algorithm used for early data, a
294
 *    #gnutls_digest_algorithm_t value.
295
 *
296
 * Since: 3.7.2
297
 **/
298
gnutls_digest_algorithm_t
299
gnutls_early_prf_hash_get(const gnutls_session_t session)
300
0
{
301
0
  if (!(session->internals.hsk_flags & HSK_EARLY_DATA_IN_FLIGHT)) {
302
0
    return gnutls_assert_val(GNUTLS_DIG_UNKNOWN);
303
0
  }
304
305
0
  if (unlikely(session->internals.resumed_security_parameters.prf ==
306
0
         NULL)) {
307
0
    return gnutls_assert_val(GNUTLS_DIG_UNKNOWN);
308
0
  }
309
310
0
  if (unlikely(session->internals.resumed_security_parameters.prf->id >=
311
0
         GNUTLS_MAC_AEAD)) {
312
0
    return gnutls_assert_val(GNUTLS_DIG_UNKNOWN);
313
0
  }
314
315
0
  return (gnutls_digest_algorithm_t)
316
0
    session->internals.resumed_security_parameters.prf->id;
317
0
}
318
319
/**
320
 * gnutls_ciphersuite_get:
321
 * @session: is a #gnutls_session_t type.
322
 *
323
 * Get the canonical name of negotiated TLS ciphersuite.  The names
324
 * returned by this function match the IANA registry, with one
325
 * exception:
326
 *
327
 *   TLS_DHE_DSS_RC4_128_SHA { 0x00, 0x66 }
328
 *
329
 * which is reserved for compatibility.
330
 *
331
 * To get a detailed description of the current ciphersuite, it is
332
 * recommended to use gnutls_session_get_desc().
333
 *
334
 * Returns: a string that contains the canonical name of a TLS ciphersuite,
335
 *     or %NULL if the handshake is not completed.
336
 *
337
 * Since: 3.7.4
338
 **/
339
const char *gnutls_ciphersuite_get(gnutls_session_t session)
340
0
{
341
0
  if (unlikely(session->internals.handshake_in_progress)) {
342
0
    return NULL;
343
0
  }
344
0
  return session->security_parameters.cs->canonical_name;
345
0
}
346
347
void reset_binders(gnutls_session_t session)
348
0
{
349
0
  _gnutls_free_temp_key_datum(&session->key.binders[0].psk);
350
0
  _gnutls_free_temp_key_datum(&session->key.binders[1].psk);
351
0
  memset(session->key.binders, 0, sizeof(session->key.binders));
352
0
}
353
354
/* Check whether certificate credentials of type @cert_type are set
355
 * for the current session.
356
 */
357
static bool _gnutls_has_cert_credentials(gnutls_session_t session,
358
           gnutls_certificate_type_t cert_type)
359
0
{
360
0
  unsigned i;
361
0
  unsigned cert_found = 0;
362
0
  gnutls_certificate_credentials_t cred;
363
364
  /* First, check for certificate credentials. If we have no certificate
365
   * credentials set then we don't support certificates at all.
366
   */
367
0
  cred = (gnutls_certificate_credentials_t)_gnutls_get_cred(
368
0
    session, GNUTLS_CRD_CERTIFICATE);
369
370
0
  if (cred == NULL)
371
0
    return false;
372
373
  /* There are credentials initialized. Now check whether we can find
374
   * pre-set certificates of the required type, but only if we don't
375
   * use the callback functions.
376
   */
377
0
  if (cred->get_cert_callback3 == NULL) {
378
0
    for (i = 0; i < cred->ncerts; i++) {
379
0
      if (cred->certs[i].cert_list[0].type == cert_type) {
380
0
        cert_found = 1;
381
0
        break;
382
0
      }
383
0
    }
384
385
0
    if (cert_found == 0) {
386
      /* No matching certificate found. */
387
0
      return false;
388
0
    }
389
0
  }
390
391
0
  return true; // OK
392
0
}
393
394
/* Check if the given certificate type is supported.
395
 * This means that it is enabled by the priority functions,
396
 * and in some cases a matching certificate exists. A check for
397
 * the latter can be toggled via the parameter @check_credentials.
398
 */
399
bool _gnutls_session_is_cert_type_supported(gnutls_session_t session,
400
              gnutls_certificate_type_t cert_type,
401
              bool check_credentials,
402
              gnutls_ctype_target_t target)
403
0
{
404
0
  unsigned i;
405
0
  priority_st *ctype_priorities;
406
407
  // Check whether this cert type is enabled by the application
408
0
  if (!is_cert_type_enabled(session, cert_type))
409
0
    return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE);
410
411
  // Perform a credentials check if requested
412
0
  if (check_credentials) {
413
0
    if (!_gnutls_has_cert_credentials(session, cert_type))
414
0
      return gnutls_assert_val(
415
0
        GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE);
416
0
  }
417
418
  /* So far so good. We have the required credentials (if needed).
419
   * Now check whether we are allowed to use them according to our
420
   * priorities.
421
   */
422
  // Which certificate type should we query?
423
0
  switch (target) {
424
0
  case GNUTLS_CTYPE_CLIENT:
425
0
    ctype_priorities =
426
0
      &(session->internals.priorities->client_ctype);
427
0
    break;
428
0
  case GNUTLS_CTYPE_SERVER:
429
0
    ctype_priorities =
430
0
      &(session->internals.priorities->server_ctype);
431
0
    break;
432
0
  default:
433
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
434
0
  }
435
436
  // No explicit priorities set, and default ctype is asked
437
0
  if (ctype_priorities->num_priorities == 0 &&
438
0
      cert_type == DEFAULT_CERT_TYPE)
439
0
    return 0;
440
441
  /* Now lets find out whether our cert type is in our priority
442
   * list, i.e. set of allowed cert types.
443
   */
444
0
  for (i = 0; i < ctype_priorities->num_priorities; i++) {
445
0
    if (ctype_priorities->priorities[i] == cert_type)
446
0
      return 0;
447
0
  }
448
449
0
  return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE;
450
0
}
451
452
static void deinit_keys(gnutls_session_t session)
453
0
{
454
0
  const version_entry_st *vers = get_version(session);
455
456
0
  if (vers == NULL)
457
0
    return;
458
459
0
  gnutls_pk_params_release(&session->key.kshare.ecdhx_params);
460
0
  gnutls_pk_params_release(&session->key.kshare.ecdh_params);
461
0
  gnutls_pk_params_release(&session->key.kshare.dh_params);
462
463
0
  if (!vers->tls13_sem && session->key.binders[0].prf == NULL) {
464
0
    gnutls_pk_params_release(&session->key.proto.tls12.ecdh.params);
465
0
    gnutls_pk_params_release(&session->key.proto.tls12.dh.params);
466
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.ecdh.x);
467
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.ecdh.y);
468
0
    _gnutls_free_temp_key_datum(&session->key.proto.tls12.ecdh.raw);
469
470
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.dh.client_Y);
471
472
    /* SRP */
473
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_p);
474
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_g);
475
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_key);
476
477
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.u);
478
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.a);
479
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.x);
480
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.A);
481
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.B);
482
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.b);
483
0
  } else {
484
0
    gnutls_memset(session->key.proto.tls13.temp_secret, 0,
485
0
            sizeof(session->key.proto.tls13.temp_secret));
486
0
  }
487
488
0
  reset_binders(session);
489
0
  _gnutls_free_temp_key_datum(&session->key.key);
490
0
}
491
492
/* An internal version of _gnutls_handshake_internal_state_clear(),
493
 * it will not attempt to deallocate, only initialize */
494
static void handshake_internal_state_clear1(gnutls_session_t session)
495
0
{
496
  /* by default no selected certificate */
497
0
  session->internals.adv_version_major = 0;
498
0
  session->internals.adv_version_minor = 0;
499
0
  session->internals.direction = 0;
500
501
  /* use out of band data for the last
502
   * handshake messages received.
503
   */
504
0
  session->internals.last_handshake_in = -1;
505
0
  session->internals.last_handshake_out = -1;
506
507
0
  session->internals.resumable = true;
508
509
0
  session->internals.handshake_suspicious_loops = 0;
510
0
  session->internals.dtls.hsk_read_seq = 0;
511
0
  session->internals.dtls.hsk_write_seq = 0;
512
513
0
  session->internals.cand_ec_group = 0;
514
0
  session->internals.cand_dh_group = 0;
515
516
0
  session->internals.hrr_cs[0] = CS_INVALID_MAJOR;
517
0
  session->internals.hrr_cs[1] = CS_INVALID_MINOR;
518
0
}
519
520
/* This function will clear all the variables in internals
521
 * structure within the session, which depend on the current handshake.
522
 * This is used to allow further handshakes.
523
 */
524
void _gnutls_handshake_internal_state_clear(gnutls_session_t session)
525
0
{
526
0
  handshake_internal_state_clear1(session);
527
528
0
  _gnutls_handshake_hash_buffers_clear(session);
529
0
  deinit_keys(session);
530
531
0
  _gnutls_epoch_gc(session);
532
533
0
  session->internals.handshake_abs_timeout.tv_sec = 0;
534
0
  session->internals.handshake_abs_timeout.tv_nsec = 0;
535
0
  session->internals.handshake_in_progress = 0;
536
537
0
  session->internals.tfo.connect_addrlen = 0;
538
0
  session->internals.tfo.connect_only = 0;
539
0
  session->internals.early_data_received = 0;
540
0
  session->internals.session_ticket_renew = 0;
541
0
}
542
543
/**
544
 * gnutls_init:
545
 * @session: is a pointer to a #gnutls_session_t type.
546
 * @flags: indicate if this session is to be used for server or client.
547
 *
548
 * This function initializes the provided session. Every session must
549
 * be initialized before use, and after successful initialization and
550
 * use must be deinitialized by calling gnutls_deinit().
551
 *
552
 * @flags can be any combination of flags from %gnutls_init_flags_t.
553
 *
554
 * Note that since version 3.1.2 this function enables some common
555
 * TLS extensions such as session tickets and OCSP certificate status
556
 * request in client side by default. To prevent that use the %GNUTLS_NO_DEFAULT_EXTENSIONS
557
 * flag.
558
 *
559
 * Note that it is never mandatory to use gnutls_deinit() after this
560
 * function fails.  Since gnutls 3.8.0, it is safe to unconditionally
561
 * use gnutls_deinit() even after failure regardless of whether the
562
 * memory was initialized prior to gnutls_init(); however, clients
563
 * wanting to be portable to older versions of the library should
564
 * either skip deinitialization on failure, or pre-initialize the
565
 * memory passed in to gnutls_init() to all zeroes via memset() or
566
 * similar.
567
 *
568
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
569
 **/
570
int gnutls_init(gnutls_session_t *session, unsigned int flags)
571
0
{
572
0
  int ret;
573
574
0
  *session = NULL;
575
0
  FAIL_IF_LIB_ERROR;
576
577
0
  *session = gnutls_calloc(1, sizeof(struct gnutls_session_int));
578
0
  if (*session == NULL)
579
0
    return GNUTLS_E_MEMORY_ERROR;
580
581
0
  ret = gnutls_mutex_init(&(*session)->internals.post_negotiation_lock);
582
0
  if (ret < 0) {
583
0
    gnutls_assert();
584
0
    gnutls_free(*session);
585
0
    return ret;
586
0
  }
587
588
0
  ret = gnutls_mutex_init(&(*session)->internals.epoch_lock);
589
0
  if (ret < 0) {
590
0
    gnutls_assert();
591
0
    gnutls_mutex_deinit(
592
0
      &(*session)->internals.post_negotiation_lock);
593
0
    gnutls_free(*session);
594
0
    return ret;
595
0
  }
596
597
0
  ret = _gnutls_epoch_setup_next(*session, 1, NULL);
598
0
  if (ret < 0) {
599
0
    gnutls_mutex_deinit(
600
0
      &(*session)->internals.post_negotiation_lock);
601
0
    gnutls_mutex_deinit(&(*session)->internals.epoch_lock);
602
0
    gnutls_free(*session);
603
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
604
0
  }
605
0
  _gnutls_epoch_bump(*session);
606
607
0
  (*session)->security_parameters.entity =
608
0
    (flags & GNUTLS_SERVER ? GNUTLS_SERVER : GNUTLS_CLIENT);
609
610
  /* the default certificate type for TLS */
611
0
  (*session)->security_parameters.client_ctype = DEFAULT_CERT_TYPE;
612
0
  (*session)->security_parameters.server_ctype = DEFAULT_CERT_TYPE;
613
614
  /* Initialize buffers */
615
0
  _gnutls_buffer_init(&(*session)->internals.handshake_hash_buffer);
616
0
  _gnutls_buffer_init(&(*session)->internals.post_handshake_hash_buffer);
617
0
  _gnutls_buffer_init(&(*session)->internals.hb_remote_data);
618
0
  _gnutls_buffer_init(&(*session)->internals.hb_local_data);
619
0
  _gnutls_buffer_init(&(*session)->internals.record_presend_buffer);
620
0
  _gnutls_buffer_init(&(*session)->internals.record_key_update_buffer);
621
0
  _gnutls_buffer_init(&(*session)->internals.reauth_buffer);
622
623
0
  _mbuffer_head_init(&(*session)->internals.record_buffer);
624
0
  _mbuffer_head_init(&(*session)->internals.record_send_buffer);
625
0
  _mbuffer_head_init(&(*session)->internals.record_recv_buffer);
626
0
  _mbuffer_head_init(&(*session)->internals.early_data_recv_buffer);
627
0
  _gnutls_buffer_init(&(*session)->internals.early_data_presend_buffer);
628
629
0
  _mbuffer_head_init(&(*session)->internals.handshake_send_buffer);
630
0
  _gnutls_handshake_recv_buffer_init(*session);
631
632
0
  (*session)->internals.expire_time = DEFAULT_EXPIRE_TIME;
633
634
  /* Ticket key rotation - set the default X to 3 times the ticket expire time */
635
0
  (*session)->key.totp.last_result = 0;
636
637
0
  gnutls_handshake_set_max_packet_length((*session),
638
0
                 MAX_HANDSHAKE_PACKET_SIZE);
639
640
  /* set the socket pointers to -1;
641
   */
642
0
  (*session)->internals.transport_recv_ptr = (gnutls_transport_ptr_t)-1;
643
0
  (*session)->internals.transport_send_ptr = (gnutls_transport_ptr_t)-1;
644
645
  /* set the default maximum record size for TLS
646
   */
647
0
  (*session)->security_parameters.max_record_recv_size =
648
0
    DEFAULT_MAX_RECORD_SIZE;
649
0
  (*session)->security_parameters.max_record_send_size =
650
0
    DEFAULT_MAX_RECORD_SIZE;
651
0
  (*session)->security_parameters.max_user_record_recv_size =
652
0
    DEFAULT_MAX_RECORD_SIZE;
653
0
  (*session)->security_parameters.max_user_record_send_size =
654
0
    DEFAULT_MAX_RECORD_SIZE;
655
656
  /* set the default early data size for TLS
657
   */
658
0
  if ((*session)->security_parameters.entity == GNUTLS_SERVER) {
659
0
    (*session)->security_parameters.max_early_data_size =
660
0
      DEFAULT_MAX_EARLY_DATA_SIZE;
661
0
  } else {
662
0
    (*session)->security_parameters.max_early_data_size =
663
0
      UINT32_MAX;
664
0
  }
665
666
  /* Everything else not initialized here is initialized as NULL
667
   * or 0. This is why calloc is used. However, we want to
668
   * ensure that certain portions of data are initialized at
669
   * runtime before being used. Mark such regions with a
670
   * valgrind client request as undefined.
671
   */
672
0
  _gnutls_memory_mark_undefined(
673
0
    (*session)->security_parameters.master_secret,
674
0
    GNUTLS_MASTER_SIZE);
675
0
  _gnutls_memory_mark_undefined(
676
0
    (*session)->security_parameters.client_random,
677
0
    GNUTLS_RANDOM_SIZE);
678
0
  _gnutls_memory_mark_undefined(
679
0
    (*session)->security_parameters.server_random,
680
0
    GNUTLS_RANDOM_SIZE);
681
0
  _gnutls_memory_mark_undefined((*session)->key.session_ticket_key,
682
0
              TICKET_MASTER_KEY_SIZE);
683
0
  _gnutls_memory_mark_undefined((*session)->key.previous_ticket_key,
684
0
              TICKET_MASTER_KEY_SIZE);
685
0
  _gnutls_memory_mark_undefined((*session)->key.initial_stek,
686
0
              TICKET_MASTER_KEY_SIZE);
687
688
0
  handshake_internal_state_clear1(*session);
689
690
0
#ifdef MSG_NOSIGNAL
691
0
  if (flags & GNUTLS_NO_SIGNAL)
692
0
    gnutls_transport_set_vec_push_function(*session,
693
0
                   system_writev_nosignal);
694
0
  else
695
0
#endif
696
0
    gnutls_transport_set_vec_push_function(*session, system_writev);
697
0
  (*session)->internals.pull_timeout_func = gnutls_system_recv_timeout;
698
0
  (*session)->internals.pull_func = system_read;
699
0
  (*session)->internals.errno_func = system_errno;
700
701
0
  (*session)->internals.saved_username = NULL;
702
0
  (*session)->internals.saved_username_size = -1;
703
704
  /* heartbeat timeouts */
705
0
  (*session)->internals.hb_retrans_timeout_ms = 1000;
706
0
  (*session)->internals.hb_total_timeout_ms = 60000;
707
708
0
  if (flags & GNUTLS_DATAGRAM) {
709
0
    (*session)->internals.dtls.mtu = DTLS_DEFAULT_MTU;
710
0
    (*session)->internals.transport = GNUTLS_DGRAM;
711
712
0
    gnutls_dtls_set_timeouts(*session, DTLS_RETRANS_TIMEOUT, 60000);
713
0
  } else {
714
0
    (*session)->internals.transport = GNUTLS_STREAM;
715
0
  }
716
717
  /* Enable useful extensions */
718
0
  if ((flags & GNUTLS_CLIENT) &&
719
0
      !(flags & GNUTLS_NO_DEFAULT_EXTENSIONS)) {
720
0
#ifdef ENABLE_OCSP
721
0
    if (!(flags & GNUTLS_NO_STATUS_REQUEST))
722
0
      gnutls_ocsp_status_request_enable_client(*session, NULL,
723
0
                 0, NULL);
724
0
#endif
725
0
  }
726
727
  /* session tickets in server side are enabled by setting a key */
728
0
  if (flags & GNUTLS_SERVER)
729
0
    flags |= GNUTLS_NO_TICKETS;
730
731
0
  (*session)->internals.flags = flags;
732
733
0
  if (_gnutls_disable_tls13 != 0)
734
0
    (*session)->internals.flags |= INT_FLAG_NO_TLS13;
735
736
  /* Install the default keylog function */
737
0
  gnutls_session_set_keylog_function(*session, _gnutls_nss_keylog_func);
738
739
0
  return 0;
740
0
}
741
742
/**
743
 * gnutls_deinit:
744
 * @session: is a #gnutls_session_t type.
745
 *
746
 * This function clears all buffers associated with the @session.
747
 * This function will also remove session data from the session
748
 * database if the session was terminated abnormally.
749
 **/
750
void gnutls_deinit(gnutls_session_t session)
751
0
{
752
0
  unsigned int i;
753
754
0
  if (session == NULL)
755
0
    return;
756
757
  /* remove auth info firstly */
758
0
  _gnutls_free_auth_info(session);
759
760
0
  _gnutls_handshake_internal_state_clear(session);
761
0
  _gnutls_handshake_io_buffer_clear(session);
762
0
  _gnutls_hello_ext_priv_deinit(session);
763
764
0
  for (i = 0; i < MAX_EPOCH_INDEX; i++)
765
0
    if (session->record_parameters[i] != NULL) {
766
0
      _gnutls_epoch_free(session,
767
0
             session->record_parameters[i]);
768
0
      session->record_parameters[i] = NULL;
769
0
    }
770
771
0
  _gnutls_buffer_clear(&session->internals.handshake_hash_buffer);
772
0
  _gnutls_buffer_clear(&session->internals.post_handshake_hash_buffer);
773
0
  _gnutls_buffer_clear(&session->internals.hb_remote_data);
774
0
  _gnutls_buffer_clear(&session->internals.hb_local_data);
775
0
  _gnutls_buffer_clear(&session->internals.record_presend_buffer);
776
0
  _gnutls_buffer_clear(&session->internals.record_key_update_buffer);
777
0
  _gnutls_buffer_clear(&session->internals.reauth_buffer);
778
779
0
  _mbuffer_head_clear(&session->internals.record_buffer);
780
0
  _mbuffer_head_clear(&session->internals.record_recv_buffer);
781
0
  _mbuffer_head_clear(&session->internals.record_send_buffer);
782
783
0
  _mbuffer_head_clear(&session->internals.early_data_recv_buffer);
784
0
  _gnutls_buffer_clear(&session->internals.early_data_presend_buffer);
785
786
0
  _gnutls_free_datum(&session->internals.resumption_data);
787
0
  _gnutls_free_datum(&session->internals.dtls.dcookie);
788
789
0
  for (i = 0; i < session->internals.rexts_size; i++)
790
0
    gnutls_free(session->internals.rexts[i].name);
791
0
  gnutls_free(session->internals.rexts);
792
0
  gnutls_free(session->internals.post_handshake_cr_context.data);
793
794
0
  gnutls_free(session->internals.saved_username);
795
0
  gnutls_free(session->internals.rsup);
796
797
0
  gnutls_credentials_clear(session);
798
0
  _gnutls_selected_certs_deinit(session);
799
800
  /* destroy any session ticket we may have received */
801
0
  tls13_ticket_deinit(&session->internals.tls13_ticket);
802
803
  /* we rely on priorities' internal reference counting */
804
0
  gnutls_priority_deinit(session->internals.priorities);
805
806
  /* overwrite any temp TLS1.3 keys */
807
0
  gnutls_memset(&session->key.proto, 0, sizeof(session->key.proto));
808
809
  /* clear session ticket keys */
810
0
  _gnutls_memory_mark_defined(session->key.session_ticket_key,
811
0
            TICKET_MASTER_KEY_SIZE);
812
0
  gnutls_memset(&session->key.session_ticket_key, 0,
813
0
          TICKET_MASTER_KEY_SIZE);
814
0
  _gnutls_memory_mark_undefined(session->key.session_ticket_key,
815
0
              TICKET_MASTER_KEY_SIZE);
816
817
0
  _gnutls_memory_mark_defined(session->key.previous_ticket_key,
818
0
            TICKET_MASTER_KEY_SIZE);
819
0
  gnutls_memset(&session->key.previous_ticket_key, 0,
820
0
          TICKET_MASTER_KEY_SIZE);
821
0
  _gnutls_memory_mark_undefined(session->key.previous_ticket_key,
822
0
              TICKET_MASTER_KEY_SIZE);
823
824
0
  _gnutls_memory_mark_defined(session->key.initial_stek,
825
0
            TICKET_MASTER_KEY_SIZE);
826
0
  gnutls_memset(&session->key.initial_stek, 0, TICKET_MASTER_KEY_SIZE);
827
0
  _gnutls_memory_mark_undefined(session->key.initial_stek,
828
0
              TICKET_MASTER_KEY_SIZE);
829
830
0
  gnutls_mutex_deinit(&session->internals.post_negotiation_lock);
831
0
  gnutls_mutex_deinit(&session->internals.epoch_lock);
832
833
0
  gnutls_free(session);
834
0
}
835
836
int _gnutls_dh_set_peer_public(gnutls_session_t session, bigint_t public)
837
0
{
838
0
  dh_info_st *dh;
839
0
  int ret;
840
841
0
  switch (gnutls_auth_get_type(session)) {
842
0
  case GNUTLS_CRD_ANON: {
843
0
    anon_auth_info_t info;
844
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON);
845
0
    if (info == NULL)
846
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
847
848
0
    dh = &info->dh;
849
0
    break;
850
0
  }
851
0
  case GNUTLS_CRD_PSK: {
852
0
    psk_auth_info_t info;
853
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
854
0
    if (info == NULL)
855
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
856
857
0
    dh = &info->dh;
858
0
    break;
859
0
  }
860
0
  case GNUTLS_CRD_CERTIFICATE: {
861
0
    cert_auth_info_t info;
862
863
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
864
0
    if (info == NULL)
865
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
866
867
0
    dh = &info->dh;
868
0
    break;
869
0
  }
870
0
  default:
871
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
872
0
  }
873
874
0
  if (dh->public_key.data)
875
0
    _gnutls_free_datum(&dh->public_key);
876
877
0
  ret = _gnutls_mpi_dprint_lz(public, &dh->public_key);
878
0
  if (ret < 0) {
879
0
    gnutls_assert();
880
0
    return ret;
881
0
  }
882
883
0
  return 0;
884
0
}
885
886
int _gnutls_dh_set_secret_bits(gnutls_session_t session, unsigned bits)
887
0
{
888
0
  switch (gnutls_auth_get_type(session)) {
889
0
  case GNUTLS_CRD_ANON: {
890
0
    anon_auth_info_t info;
891
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON);
892
0
    if (info == NULL)
893
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
894
0
    info->dh.secret_bits = bits;
895
0
    break;
896
0
  }
897
0
  case GNUTLS_CRD_PSK: {
898
0
    psk_auth_info_t info;
899
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
900
0
    if (info == NULL)
901
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
902
0
    info->dh.secret_bits = bits;
903
0
    break;
904
0
  }
905
0
  case GNUTLS_CRD_CERTIFICATE: {
906
0
    cert_auth_info_t info;
907
908
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
909
0
    if (info == NULL)
910
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
911
912
0
    info->dh.secret_bits = bits;
913
0
    break;
914
0
  default:
915
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
916
0
  }
917
0
  }
918
919
0
  return 0;
920
0
}
921
922
/* Sets the prime and the generator in the auth info structure.
923
 */
924
int _gnutls_dh_save_group(gnutls_session_t session, bigint_t gen,
925
        bigint_t prime)
926
0
{
927
0
  dh_info_st *dh;
928
0
  int ret;
929
930
0
  switch (gnutls_auth_get_type(session)) {
931
0
  case GNUTLS_CRD_ANON: {
932
0
    anon_auth_info_t info;
933
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON);
934
0
    if (info == NULL)
935
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
936
937
0
    dh = &info->dh;
938
0
    break;
939
0
  }
940
0
  case GNUTLS_CRD_PSK: {
941
0
    psk_auth_info_t info;
942
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
943
0
    if (info == NULL)
944
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
945
946
0
    dh = &info->dh;
947
0
    break;
948
0
  }
949
0
  case GNUTLS_CRD_CERTIFICATE: {
950
0
    cert_auth_info_t info;
951
952
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
953
0
    if (info == NULL)
954
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
955
956
0
    dh = &info->dh;
957
0
    break;
958
0
  }
959
0
  default:
960
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
961
0
  }
962
963
0
  if (dh->prime.data)
964
0
    _gnutls_free_datum(&dh->prime);
965
966
0
  if (dh->generator.data)
967
0
    _gnutls_free_datum(&dh->generator);
968
969
  /* prime
970
   */
971
0
  ret = _gnutls_mpi_dprint_lz(prime, &dh->prime);
972
0
  if (ret < 0) {
973
0
    gnutls_assert();
974
0
    return ret;
975
0
  }
976
977
  /* generator
978
   */
979
0
  ret = _gnutls_mpi_dprint_lz(gen, &dh->generator);
980
0
  if (ret < 0) {
981
0
    gnutls_assert();
982
0
    _gnutls_free_datum(&dh->prime);
983
0
    return ret;
984
0
  }
985
986
0
  return 0;
987
0
}
988
989
/**
990
 * gnutls_certificate_send_x509_rdn_sequence:
991
 * @session: a #gnutls_session_t type.
992
 * @status: is 0 or 1
993
 *
994
 * If status is non zero, this function will order gnutls not to send
995
 * the rdnSequence in the certificate request message. That is the
996
 * server will not advertise its trusted CAs to the peer. If status
997
 * is zero then the default behaviour will take effect, which is to
998
 * advertise the server's trusted CAs.
999
 *
1000
 * This function has no effect in clients, and in authentication
1001
 * methods other than certificate with X.509 certificates.
1002
 **/
1003
void gnutls_certificate_send_x509_rdn_sequence(gnutls_session_t session,
1004
                 int status)
1005
0
{
1006
0
  session->internals.ignore_rdn_sequence = status;
1007
0
}
1008
1009
/*-
1010
 * _gnutls_record_set_default_version - Used to set the default version for the first record packet
1011
 * @session: is a #gnutls_session_t type.
1012
 * @major: is a tls major version
1013
 * @minor: is a tls minor version
1014
 *
1015
 * This function sets the default version that we will use in the first
1016
 * record packet (client hello). This function is only useful to people
1017
 * that know TLS internals and want to debug other implementations.
1018
 -*/
1019
void _gnutls_record_set_default_version(gnutls_session_t session,
1020
          unsigned char major,
1021
          unsigned char minor)
1022
0
{
1023
0
  session->internals.default_record_version[0] = major;
1024
0
  session->internals.default_record_version[1] = minor;
1025
0
}
1026
1027
/*-
1028
 * _gnutls_hello_set_default_version - Used to set the default version for the first record packet
1029
 * @session: is a #gnutls_session_t type.
1030
 * @major: is a tls major version
1031
 * @minor: is a tls minor version
1032
 *
1033
 * This function sets the default version that we will use in the first
1034
 * record packet (client hello). This function is only useful to people
1035
 * that know TLS internals and want to debug other implementations.
1036
 -*/
1037
void _gnutls_hello_set_default_version(gnutls_session_t session,
1038
               unsigned char major, unsigned char minor)
1039
0
{
1040
0
  session->internals.default_hello_version[0] = major;
1041
0
  session->internals.default_hello_version[1] = minor;
1042
0
}
1043
1044
/**
1045
 * gnutls_handshake_set_private_extensions:
1046
 * @session: is a #gnutls_session_t type.
1047
 * @allow: is an integer (0 or 1)
1048
 *
1049
 * This function will enable or disable the use of private cipher
1050
 * suites (the ones that start with 0xFF).  By default or if @allow
1051
 * is 0 then these cipher suites will not be advertised nor used.
1052
 *
1053
 * Currently GnuTLS does not include such cipher-suites or
1054
 * compression algorithms.
1055
 *
1056
 * Enabling the private ciphersuites when talking to other than
1057
 * gnutls servers and clients may cause interoperability problems.
1058
 **/
1059
void gnutls_handshake_set_private_extensions(gnutls_session_t session,
1060
               int allow)
1061
0
{
1062
  /* we have no private extensions */
1063
0
  return;
1064
0
}
1065
1066
/**
1067
 * gnutls_session_is_resumed:
1068
 * @session: is a #gnutls_session_t type.
1069
 *
1070
 * Checks whether session is resumed or not. This is functional
1071
 * for both server and client side.
1072
 *
1073
 * Returns: non zero if this session is resumed, or a zero if this is
1074
 *   a new session.
1075
 **/
1076
int gnutls_session_is_resumed(gnutls_session_t session)
1077
0
{
1078
0
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
1079
0
    const version_entry_st *ver = get_version(session);
1080
0
    if (ver && ver->tls13_sem) {
1081
0
      return session->internals.resumed;
1082
0
    }
1083
1084
0
    if (session->security_parameters.session_id_size > 0 &&
1085
0
        session->security_parameters.session_id_size ==
1086
0
          session->internals.resumed_security_parameters
1087
0
            .session_id_size &&
1088
0
        memcmp(session->security_parameters.session_id,
1089
0
         session->internals.resumed_security_parameters
1090
0
           .session_id,
1091
0
         session->security_parameters.session_id_size) == 0)
1092
0
      return 1;
1093
0
  } else {
1094
0
    if (session->internals.resumed)
1095
0
      return 1;
1096
0
  }
1097
1098
0
  return 0;
1099
0
}
1100
1101
/**
1102
 * gnutls_session_resumption_requested:
1103
 * @session: is a #gnutls_session_t type.
1104
 *
1105
 * Check whether the client has asked for session resumption.
1106
 * This function is valid only on server side.
1107
 *
1108
 * Returns: non zero if session resumption was asked, or a zero if not.
1109
 **/
1110
int gnutls_session_resumption_requested(gnutls_session_t session)
1111
0
{
1112
0
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
1113
0
    return 0;
1114
0
  } else {
1115
0
    return session->internals.resumption_requested;
1116
0
  }
1117
0
}
1118
1119
/*-
1120
 * _gnutls_session_is_psk - Used to check whether this session uses PSK kx
1121
 * @session: is a #gnutls_session_t type.
1122
 *
1123
 * This function will return non zero if this session uses a PSK key
1124
 * exchange algorithm.
1125
 -*/
1126
int _gnutls_session_is_psk(gnutls_session_t session)
1127
0
{
1128
0
  gnutls_kx_algorithm_t kx;
1129
1130
0
  kx = session->security_parameters.cs->kx_algorithm;
1131
0
  if (kx == GNUTLS_KX_PSK || kx == GNUTLS_KX_DHE_PSK ||
1132
0
      kx == GNUTLS_KX_RSA_PSK)
1133
0
    return 1;
1134
1135
0
  return 0;
1136
0
}
1137
1138
/*-
1139
 * _gnutls_session_is_ecc - Used to check whether this session uses ECC kx
1140
 * @session: is a #gnutls_session_t type.
1141
 *
1142
 * This function will return non zero if this session uses an elliptic
1143
 * curves key exchange exchange algorithm.
1144
 -*/
1145
int _gnutls_session_is_ecc(gnutls_session_t session)
1146
0
{
1147
0
  gnutls_kx_algorithm_t kx;
1148
1149
  /* We get the key exchange algorithm through the ciphersuite because
1150
   * the negotiated key exchange might not have been set yet.
1151
   */
1152
0
  kx = session->security_parameters.cs->kx_algorithm;
1153
1154
0
  return _gnutls_kx_is_ecc(kx);
1155
0
}
1156
1157
/**
1158
 * gnutls_session_get_ptr:
1159
 * @session: is a #gnutls_session_t type.
1160
 *
1161
 * Get user pointer for session.  Useful in callbacks.  This is the
1162
 *   pointer set with gnutls_session_set_ptr().
1163
 *
1164
 * Returns: the user given pointer from the session structure, or
1165
 *   %NULL if it was never set.
1166
 **/
1167
void *gnutls_session_get_ptr(gnutls_session_t session)
1168
0
{
1169
0
  return session->internals.user_ptr;
1170
0
}
1171
1172
/**
1173
 * gnutls_session_set_ptr:
1174
 * @session: is a #gnutls_session_t type.
1175
 * @ptr: is the user pointer
1176
 *
1177
 * This function will set (associate) the user given pointer @ptr to
1178
 * the session structure.  This pointer can be accessed with
1179
 * gnutls_session_get_ptr().
1180
 **/
1181
void gnutls_session_set_ptr(gnutls_session_t session, void *ptr)
1182
0
{
1183
0
  session->internals.user_ptr = ptr;
1184
0
}
1185
1186
/**
1187
 * gnutls_session_set_verify_function:
1188
 * @session: is a #gnutls_session_t type.
1189
 * @func: is the callback function
1190
 *
1191
 * This function sets a callback to be called when peer's certificate
1192
 * has been received in order to verify it on receipt rather than
1193
 * doing after the handshake is completed. This overrides any callback
1194
 * set using gnutls_certificate_set_verify_function().
1195
 *
1196
 * The callback's function prototype is:
1197
 * int (*callback)(gnutls_session_t);
1198
 *
1199
 * If the callback function is provided then gnutls will call it, in the
1200
 * handshake, just after the certificate message has been received.
1201
 * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
1202
 * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
1203
 * can be used.
1204
 *
1205
 * The callback function should return 0 for the handshake to continue
1206
 * or non-zero to terminate.
1207
 *
1208
 * Since: 3.4.6
1209
 **/
1210
void gnutls_session_set_verify_function(
1211
  gnutls_session_t session, gnutls_certificate_verify_function *func)
1212
0
{
1213
0
  session->internals.verify_callback = func;
1214
0
}
1215
1216
/**
1217
 * gnutls_record_get_direction:
1218
 * @session: is a #gnutls_session_t type.
1219
 *
1220
 * This function is useful to determine whether a GnuTLS function was interrupted
1221
 * while sending or receiving, so that select() or poll() may be called appropriately.
1222
 *
1223
 * It provides information about the internals of the record
1224
 * protocol and is only useful if a prior gnutls function call,
1225
 * e.g.  gnutls_handshake(), was interrupted and returned
1226
 * %GNUTLS_E_INTERRUPTED or %GNUTLS_E_AGAIN. After such an interrupt
1227
 * applications may call select() or poll() before restoring the
1228
 * interrupted GnuTLS function.
1229
 *
1230
 * This function's output is unreliable if you are using the same
1231
 * @session in different threads for sending and receiving.
1232
 *
1233
 * Returns: 0 if interrupted while trying to read data, or 1 while trying to write data.
1234
 **/
1235
int gnutls_record_get_direction(gnutls_session_t session)
1236
0
{
1237
0
  return session->internals.direction;
1238
0
}
1239
1240
/*-
1241
 * _gnutls_rsa_pms_set_version - Sets a version to be used at the RSA PMS
1242
 * @session: is a #gnutls_session_t type.
1243
 * @major: is the major version to use
1244
 * @minor: is the minor version to use
1245
 *
1246
 * This function will set the given version number to be used at the
1247
 * RSA PMS secret. This is only useful to clients, which want to
1248
 * test server's capabilities.
1249
 -*/
1250
void _gnutls_rsa_pms_set_version(gnutls_session_t session, unsigned char major,
1251
         unsigned char minor)
1252
0
{
1253
0
  session->internals.rsa_pms_version[0] = major;
1254
0
  session->internals.rsa_pms_version[1] = minor;
1255
0
}
1256
1257
void _gnutls_session_client_cert_type_set(gnutls_session_t session,
1258
            gnutls_certificate_type_t ct)
1259
0
{
1260
0
  _gnutls_handshake_log(
1261
0
    "HSK[%p]: Selected client certificate type %s (%d)\n", session,
1262
0
    gnutls_certificate_type_get_name(ct), ct);
1263
0
  session->security_parameters.client_ctype = ct;
1264
0
}
1265
1266
void _gnutls_session_server_cert_type_set(gnutls_session_t session,
1267
            gnutls_certificate_type_t ct)
1268
0
{
1269
0
  _gnutls_handshake_log(
1270
0
    "HSK[%p]: Selected server certificate type %s (%d)\n", session,
1271
0
    gnutls_certificate_type_get_name(ct), ct);
1272
0
  session->security_parameters.server_ctype = ct;
1273
0
}
1274
1275
/**
1276
 * gnutls_handshake_set_post_client_hello_function:
1277
 * @session: is a #gnutls_session_t type.
1278
 * @func: is the function to be called
1279
 *
1280
 * This function will set a callback to be called after the client
1281
 * hello has been received (callback valid in server side only). This
1282
 * allows the server to adjust settings based on received extensions.
1283
 *
1284
 * Those settings could be ciphersuites, requesting certificate, or
1285
 * anything else except for version negotiation (this is done before
1286
 * the hello message is parsed).
1287
 *
1288
 * This callback must return 0 on success or a gnutls error code to
1289
 * terminate the handshake.
1290
 *
1291
 * Since GnuTLS 3.3.5 the callback is
1292
 * allowed to return %GNUTLS_E_AGAIN or %GNUTLS_E_INTERRUPTED to
1293
 * put the handshake on hold. In that case gnutls_handshake()
1294
 * will return %GNUTLS_E_INTERRUPTED and can be resumed when needed.
1295
 *
1296
 * Warning: You should not use this function to terminate the
1297
 * handshake based on client input unless you know what you are
1298
 * doing. Before the handshake is finished there is no way to know if
1299
 * there is a man-in-the-middle attack being performed.
1300
 **/
1301
void gnutls_handshake_set_post_client_hello_function(
1302
  gnutls_session_t session, gnutls_handshake_simple_hook_func func)
1303
0
{
1304
0
  session->internals.user_hello_func = func;
1305
0
}
1306
1307
/**
1308
 * gnutls_session_enable_compatibility_mode:
1309
 * @session: is a #gnutls_session_t type.
1310
 *
1311
 * This function can be used to disable certain (security) features in
1312
 * TLS in order to maintain maximum compatibility with buggy
1313
 * clients. Because several trade-offs with security are enabled,
1314
 * if required they will be reported through the audit subsystem.
1315
 *
1316
 * Normally only servers that require maximum compatibility with
1317
 * everything out there, need to call this function.
1318
 *
1319
 * Note that this function must be called after any call to gnutls_priority
1320
 * functions.
1321
 *
1322
 * Since: 2.1.4
1323
 **/
1324
void gnutls_session_enable_compatibility_mode(gnutls_session_t session)
1325
0
{
1326
0
  ENABLE_COMPAT(&session->internals);
1327
0
}
1328
1329
/**
1330
 * gnutls_session_channel_binding:
1331
 * @session: is a #gnutls_session_t type.
1332
 * @cbtype: an #gnutls_channel_binding_t enumeration type
1333
 * @cb: output buffer array with data
1334
 *
1335
 * Extract given channel binding data of the @cbtype (e.g.,
1336
 * %GNUTLS_CB_TLS_UNIQUE) type.
1337
 *
1338
 * Returns: %GNUTLS_E_SUCCESS on success,
1339
 * %GNUTLS_E_UNIMPLEMENTED_FEATURE if the @cbtype is unsupported,
1340
 * %GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE if the data is not
1341
 * currently available, or an error code.
1342
 *
1343
 * Since: 2.12.0
1344
 **/
1345
int gnutls_session_channel_binding(gnutls_session_t session,
1346
           gnutls_channel_binding_t cbtype,
1347
           gnutls_datum_t *cb)
1348
0
{
1349
0
  if (!session->internals.initial_negotiation_completed)
1350
0
    return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1351
1352
0
  if (cbtype == GNUTLS_CB_TLS_UNIQUE) {
1353
0
    const version_entry_st *ver = get_version(session);
1354
0
    if (unlikely(ver == NULL || ver->tls13_sem))
1355
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1356
1357
0
    cb->size = session->internals.cb_tls_unique_len;
1358
0
    cb->data = gnutls_malloc(cb->size);
1359
0
    if (cb->data == NULL)
1360
0
      return GNUTLS_E_MEMORY_ERROR;
1361
1362
0
    memcpy(cb->data, session->internals.cb_tls_unique, cb->size);
1363
1364
0
    return 0;
1365
0
  }
1366
1367
0
  if (cbtype == GNUTLS_CB_TLS_SERVER_END_POINT) {
1368
0
    const gnutls_datum_t *ders;
1369
0
    unsigned int num_certs = 1;
1370
0
    int ret;
1371
0
    size_t rlen;
1372
0
    gnutls_x509_crt_t cert;
1373
0
    gnutls_digest_algorithm_t algo;
1374
1375
    /* Only X509 certificates are supported for this binding type */
1376
0
    ret = gnutls_certificate_type_get(session);
1377
0
    if (ret != GNUTLS_CRT_X509)
1378
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1379
1380
0
    if (session->security_parameters.entity == GNUTLS_CLIENT)
1381
0
      ders = gnutls_certificate_get_peers(session,
1382
0
                  &num_certs);
1383
0
    else
1384
0
      ders = gnutls_certificate_get_ours(session);
1385
1386
    /* Previous check indicated we have x509 but you never know */
1387
0
    if (!ders || num_certs == 0)
1388
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1389
1390
0
    ret = gnutls_x509_crt_list_import(&cert, &num_certs, ders,
1391
0
              GNUTLS_X509_FMT_DER, 0);
1392
    /* Again, this is not supposed to happen (normally) */
1393
0
    if (ret < 0 || num_certs == 0)
1394
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1395
1396
    /* Obtain signature algorithm used by certificate */
1397
0
    ret = gnutls_x509_crt_get_signature_algorithm(cert);
1398
0
    if (ret < 0 || ret == GNUTLS_SIGN_UNKNOWN)
1399
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1400
1401
    /* obtain hash function from signature and normalize it */
1402
0
    algo = gnutls_sign_get_hash_algorithm(ret);
1403
0
    switch (algo) {
1404
0
    case GNUTLS_DIG_MD5:
1405
0
    case GNUTLS_DIG_SHA1:
1406
0
      algo = GNUTLS_DIG_SHA256;
1407
0
      break;
1408
0
    case GNUTLS_DIG_UNKNOWN:
1409
0
    case GNUTLS_DIG_NULL:
1410
0
    case GNUTLS_DIG_MD5_SHA1:
1411
      /* double hashing not supported either */
1412
0
      gnutls_x509_crt_deinit(cert);
1413
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1414
0
    default:
1415
0
      break;
1416
0
    }
1417
1418
    /* preallocate 512 bits buffer as maximum supported digest */
1419
0
    rlen = MAX_HASH_SIZE;
1420
0
    cb->data = gnutls_malloc(rlen);
1421
0
    if (cb->data == NULL) {
1422
0
      gnutls_x509_crt_deinit(cert);
1423
0
      return GNUTLS_E_MEMORY_ERROR;
1424
0
    }
1425
1426
0
    ret = gnutls_x509_crt_get_fingerprint(cert, algo, cb->data,
1427
0
                  &rlen);
1428
0
    if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1429
0
      cb->data = gnutls_realloc_fast(cb->data, cb->size);
1430
0
      if (cb->data == NULL) {
1431
0
        gnutls_x509_crt_deinit(cert);
1432
0
        return GNUTLS_E_MEMORY_ERROR;
1433
0
      }
1434
0
      ret = gnutls_x509_crt_get_fingerprint(cert, algo,
1435
0
                    cb->data, &rlen);
1436
0
    }
1437
0
    cb->size = rlen;
1438
0
    gnutls_x509_crt_deinit(cert);
1439
0
    return ret;
1440
0
  }
1441
1442
0
  if (cbtype == GNUTLS_CB_TLS_EXPORTER) {
1443
0
#define RFC5705_LABEL_DATA "EXPORTER-Channel-Binding"
1444
0
#define RFC5705_LABEL_LEN 24
1445
0
#define EXPORTER_CTX_DATA ""
1446
0
#define EXPORTER_CTX_LEN 0
1447
1448
0
    const version_entry_st *ver = get_version(session);
1449
0
    if (unlikely(ver == NULL)) {
1450
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1451
0
    }
1452
1453
    /* "tls-exporter" channel binding is defined only when
1454
     * the TLS handshake results in unique master secrets,
1455
     * i.e., either TLS 1.3, or TLS 1.2 with extended
1456
     * master secret negotiated.
1457
     */
1458
0
    if (!ver->tls13_sem &&
1459
0
        gnutls_session_ext_master_secret_status(session) == 0) {
1460
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1461
0
    }
1462
1463
0
    cb->size = 32;
1464
0
    cb->data = gnutls_malloc(cb->size);
1465
0
    if (cb->data == NULL)
1466
0
      return GNUTLS_E_MEMORY_ERROR;
1467
1468
0
    return gnutls_prf_rfc5705(session, RFC5705_LABEL_LEN,
1469
0
            RFC5705_LABEL_DATA, EXPORTER_CTX_LEN,
1470
0
            EXPORTER_CTX_DATA, cb->size,
1471
0
            (char *)cb->data);
1472
0
  }
1473
1474
0
  return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1475
0
}
1476
1477
/**
1478
 * gnutls_ecc_curve_get:
1479
 * @session: is a #gnutls_session_t type.
1480
 *
1481
 * Returns the currently used elliptic curve for key exchange. Only valid
1482
 * when using an elliptic curve ciphersuite.
1483
 *
1484
 * Returns: the currently used curve, a #gnutls_ecc_curve_t
1485
 *   type.
1486
 *
1487
 * Since: 3.0
1488
 **/
1489
gnutls_ecc_curve_t gnutls_ecc_curve_get(gnutls_session_t session)
1490
0
{
1491
0
  const gnutls_group_entry_st *e;
1492
1493
0
  e = get_group(session);
1494
0
  if (e == NULL || e->curve == 0)
1495
0
    return 0;
1496
0
  return e->curve;
1497
0
}
1498
1499
/**
1500
 * gnutls_group_get:
1501
 * @session: is a #gnutls_session_t type.
1502
 *
1503
 * Returns the currently used group for key exchange. Only valid
1504
 * when using an elliptic curve or DH ciphersuite.
1505
 *
1506
 * Returns: the currently used group, a #gnutls_group_t
1507
 *   type.
1508
 *
1509
 * Since: 3.6.0
1510
 **/
1511
gnutls_group_t gnutls_group_get(gnutls_session_t session)
1512
0
{
1513
0
  const gnutls_group_entry_st *e;
1514
1515
0
  e = get_group(session);
1516
0
  if (e == NULL)
1517
0
    return 0;
1518
0
  return e->id;
1519
0
}
1520
1521
/**
1522
 * gnutls_protocol_get_version:
1523
 * @session: is a #gnutls_session_t type.
1524
 *
1525
 * Get TLS version, a #gnutls_protocol_t value.
1526
 *
1527
 * Returns: The version of the currently used protocol.
1528
 **/
1529
gnutls_protocol_t gnutls_protocol_get_version(gnutls_session_t session)
1530
0
{
1531
0
  return get_num_version(session);
1532
0
}
1533
1534
/**
1535
 * gnutls_session_get_random:
1536
 * @session: is a #gnutls_session_t type.
1537
 * @client: the client part of the random
1538
 * @server: the server part of the random
1539
 *
1540
 * This function returns pointers to the client and server
1541
 * random fields used in the TLS handshake. The pointers are
1542
 * not to be modified or deallocated.
1543
 *
1544
 * If a client random value has not yet been established, the output
1545
 * will be garbage.
1546
 *
1547
 * Since: 3.0
1548
 **/
1549
void gnutls_session_get_random(gnutls_session_t session, gnutls_datum_t *client,
1550
             gnutls_datum_t *server)
1551
0
{
1552
0
  if (client) {
1553
0
    client->data = session->security_parameters.client_random;
1554
0
    client->size =
1555
0
      sizeof(session->security_parameters.client_random);
1556
0
  }
1557
1558
0
  if (server) {
1559
0
    server->data = session->security_parameters.server_random;
1560
0
    server->size =
1561
0
      sizeof(session->security_parameters.server_random);
1562
0
  }
1563
0
}
1564
1565
/**
1566
 * gnutls_session_get_master_secret:
1567
 * @session: is a #gnutls_session_t type.
1568
 * @secret: the session's master secret
1569
 *
1570
 * This function returns pointers to the master secret
1571
 * used in the TLS session. The pointers are not to be modified or deallocated.
1572
 *
1573
 * This function is only applicable under TLS 1.2 or earlier versions.
1574
 *
1575
 * Since: 3.5.0
1576
 **/
1577
void gnutls_session_get_master_secret(gnutls_session_t session,
1578
              gnutls_datum_t *secret)
1579
0
{
1580
0
  secret->data = session->security_parameters.master_secret;
1581
0
  secret->size = sizeof(session->security_parameters.master_secret);
1582
0
}
1583
1584
unsigned int timespec_sub_ms(struct timespec *a, struct timespec *b)
1585
0
{
1586
0
  time_t dsecs;
1587
1588
0
  dsecs = a->tv_sec - b->tv_sec;
1589
0
  if (!INT_MULTIPLY_OVERFLOW(dsecs, 1000)) {
1590
0
    return (dsecs * 1000 +
1591
0
      (a->tv_nsec - b->tv_nsec) / (1000 * 1000));
1592
0
  } else {
1593
0
    return UINT_MAX;
1594
0
  }
1595
0
}
1596
1597
/**
1598
 * gnutls_handshake_set_random:
1599
 * @session: is a #gnutls_session_t type.
1600
 * @random: a random value of 32-bytes
1601
 *
1602
 * This function will explicitly set the server or client hello 
1603
 * random value in the subsequent TLS handshake. The random value 
1604
 * should be a 32-byte value.
1605
 *
1606
 * Note that this function should not normally be used as gnutls
1607
 * will select automatically a random value for the handshake.
1608
 *
1609
 * This function should not be used when resuming a session.
1610
 *
1611
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1612
 *
1613
 * Since 3.1.9
1614
 **/
1615
int gnutls_handshake_set_random(gnutls_session_t session,
1616
        const gnutls_datum_t *random)
1617
0
{
1618
0
  if (random->size != GNUTLS_RANDOM_SIZE)
1619
0
    return GNUTLS_E_INVALID_REQUEST;
1620
1621
0
  session->internals.sc_random_set = 1;
1622
0
  if (session->security_parameters.entity == GNUTLS_CLIENT)
1623
0
    memcpy(session->internals.resumed_security_parameters
1624
0
             .client_random,
1625
0
           random->data, random->size);
1626
0
  else
1627
0
    memcpy(session->internals.resumed_security_parameters
1628
0
             .server_random,
1629
0
           random->data, random->size);
1630
1631
0
  return 0;
1632
0
}
1633
1634
/**
1635
 * gnutls_handshake_set_hook_function:
1636
 * @session: is a #gnutls_session_t type
1637
 * @htype: the %gnutls_handshake_description_t of the message to hook at
1638
 * @when: %GNUTLS_HOOK_* depending on when the hook function should be called
1639
 * @func: is the function to be called
1640
 *
1641
 * This function will set a callback to be called after or before the specified
1642
 * handshake message has been received or generated. This is a
1643
 * generalization of gnutls_handshake_set_post_client_hello_function().
1644
 *
1645
 * To call the hook function prior to the message being generated or processed
1646
 * use %GNUTLS_HOOK_PRE as @when parameter, %GNUTLS_HOOK_POST to call
1647
 * after, and %GNUTLS_HOOK_BOTH for both cases.
1648
 *
1649
 * This callback must return 0 on success or a gnutls error code to
1650
 * terminate the handshake.
1651
 *
1652
 * To hook at all handshake messages use an @htype of %GNUTLS_HANDSHAKE_ANY.
1653
 *
1654
 * Warning: You should not use this function to terminate the
1655
 * handshake based on client input unless you know what you are
1656
 * doing. Before the handshake is finished there is no way to know if
1657
 * there is a man-in-the-middle attack being performed.
1658
 **/
1659
void gnutls_handshake_set_hook_function(gnutls_session_t session,
1660
          unsigned int htype, int when,
1661
          gnutls_handshake_hook_func func)
1662
0
{
1663
0
  session->internals.h_hook = func;
1664
0
  session->internals.h_type = htype;
1665
0
  session->internals.h_post = when;
1666
0
}
1667
1668
/**
1669
 * gnutls_handshake_set_read_function:
1670
 * @session: is #gnutls_session_t type
1671
 * @func: is the function to be called
1672
 *
1673
 * This function will set a callback to be called when a handshake
1674
 * message is being sent.
1675
 *
1676
 * Since: 3.7.0
1677
 */
1678
void gnutls_handshake_set_read_function(gnutls_session_t session,
1679
          gnutls_handshake_read_func func)
1680
0
{
1681
0
  session->internals.h_read_func = func;
1682
0
}
1683
1684
/**
1685
 * gnutls_alert_set_read_function:
1686
 * @session: is #gnutls_session_t type
1687
 * @func: is the function to be called
1688
 *
1689
 * This function will set a callback to be called when an alert
1690
 * message is being sent.
1691
 *
1692
 * Since: 3.7.0
1693
 */
1694
void gnutls_alert_set_read_function(gnutls_session_t session,
1695
            gnutls_alert_read_func func)
1696
0
{
1697
0
  session->internals.alert_read_func = func;
1698
0
}
1699
1700
/**
1701
 * gnutls_record_get_state:
1702
 * @session: is a #gnutls_session_t type
1703
 * @read: if non-zero the read parameters are returned, otherwise the write
1704
 * @mac_key: the key used for MAC (if a MAC is used)
1705
 * @IV: the initialization vector or nonce used
1706
 * @cipher_key: the cipher key
1707
 * @seq_number: A 64-bit sequence number
1708
 *
1709
 * This function will return the parameters of the current record state.
1710
 * These are only useful to be provided to an external off-loading device
1711
 * or subsystem. The returned values should be considered constant
1712
 * and valid for the lifetime of the session.
1713
 *
1714
 * In that case, to sync the state back you must call gnutls_record_set_state().
1715
 *
1716
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1717
 *
1718
 * Since 3.4.0
1719
 **/
1720
int gnutls_record_get_state(gnutls_session_t session, unsigned read,
1721
          gnutls_datum_t *mac_key, gnutls_datum_t *IV,
1722
          gnutls_datum_t *cipher_key,
1723
          unsigned char seq_number[8])
1724
0
{
1725
0
  record_parameters_st *record_params;
1726
0
  record_state_st *record_state;
1727
0
  unsigned int epoch;
1728
0
  int ret;
1729
1730
0
  if (read)
1731
0
    epoch = EPOCH_READ_CURRENT;
1732
0
  else
1733
0
    epoch = EPOCH_WRITE_CURRENT;
1734
1735
0
  ret = _gnutls_epoch_get(session, epoch, &record_params);
1736
0
  if (ret < 0)
1737
0
    return gnutls_assert_val(ret);
1738
1739
0
  if (!record_params->initialized)
1740
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1741
1742
0
  if (read)
1743
0
    record_state = &record_params->read;
1744
0
  else
1745
0
    record_state = &record_params->write;
1746
1747
0
  if (mac_key) {
1748
0
    mac_key->data = record_state->mac_key;
1749
0
    mac_key->size = record_state->mac_key_size;
1750
0
  }
1751
1752
0
  if (IV) {
1753
0
    IV->data = record_state->iv;
1754
0
    IV->size = record_state->iv_size;
1755
0
  }
1756
1757
0
  if (cipher_key) {
1758
0
    cipher_key->data = record_state->key;
1759
0
    cipher_key->size = record_state->key_size;
1760
0
  }
1761
1762
0
  if (seq_number)
1763
0
    _gnutls_write_uint64(record_state->sequence_number, seq_number);
1764
0
  return 0;
1765
0
}
1766
1767
/**
1768
 * gnutls_record_set_state:
1769
 * @session: is a #gnutls_session_t type
1770
 * @read: if non-zero the read parameters are returned, otherwise the write
1771
 * @seq_number: A 64-bit sequence number
1772
 *
1773
 * This function will set the sequence number in the current record state.
1774
 * This function is useful if sending and receiving are offloaded from
1775
 * gnutls. That is, if gnutls_record_get_state() was used.
1776
 *
1777
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1778
 *
1779
 * Since 3.4.0
1780
 **/
1781
int gnutls_record_set_state(gnutls_session_t session, unsigned read,
1782
          const unsigned char seq_number[8])
1783
0
{
1784
0
  record_parameters_st *record_params;
1785
0
  record_state_st *record_state;
1786
0
  int epoch, ret;
1787
1788
0
  if (read)
1789
0
    epoch = EPOCH_READ_CURRENT;
1790
0
  else
1791
0
    epoch = EPOCH_WRITE_CURRENT;
1792
1793
0
  ret = _gnutls_epoch_get(session, epoch, &record_params);
1794
0
  if (ret < 0)
1795
0
    return gnutls_assert_val(ret);
1796
1797
0
  if (!record_params->initialized)
1798
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1799
1800
0
  if (read)
1801
0
    record_state = &record_params->read;
1802
0
  else
1803
0
    record_state = &record_params->write;
1804
1805
0
  record_state->sequence_number = _gnutls_read_uint64(seq_number);
1806
1807
0
  if (IS_DTLS(session)) {
1808
0
    _dtls_reset_window(record_params);
1809
0
  }
1810
1811
0
  return 0;
1812
0
}
1813
1814
/**
1815
 * gnutls_session_get_flags:
1816
 * @session: is a #gnutls_session_t type.
1817
 *
1818
 * This function will return a series (ORed) of flags, applicable
1819
 * for the current session.
1820
 *
1821
 * This replaces individual informational functions such as
1822
 * gnutls_safe_renegotiation_status(), gnutls_session_ext_master_secret_status(),
1823
 * etc.
1824
 *
1825
 * Returns: An ORed sequence of flags (see %gnutls_session_flags_t)
1826
 *
1827
 * Since: 3.5.0
1828
 **/
1829
unsigned gnutls_session_get_flags(gnutls_session_t session)
1830
0
{
1831
0
  unsigned flags = 0;
1832
1833
0
  if (gnutls_safe_renegotiation_status(session))
1834
0
    flags |= GNUTLS_SFLAGS_SAFE_RENEGOTIATION;
1835
0
  if (gnutls_session_ext_master_secret_status(session))
1836
0
    flags |= GNUTLS_SFLAGS_EXT_MASTER_SECRET;
1837
0
  if (gnutls_session_etm_status(session))
1838
0
    flags |= GNUTLS_SFLAGS_ETM;
1839
0
  if (gnutls_heartbeat_allowed(session, GNUTLS_HB_LOCAL_ALLOWED_TO_SEND))
1840
0
    flags |= GNUTLS_SFLAGS_HB_LOCAL_SEND;
1841
0
  if (gnutls_heartbeat_allowed(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND))
1842
0
    flags |= GNUTLS_SFLAGS_HB_PEER_SEND;
1843
0
  if (session->internals.hsk_flags & HSK_FALSE_START_USED)
1844
0
    flags |= GNUTLS_SFLAGS_FALSE_START;
1845
0
  if ((session->internals.hsk_flags & HSK_EARLY_START_USED) &&
1846
0
      (session->internals.flags & GNUTLS_ENABLE_EARLY_START))
1847
0
    flags |= GNUTLS_SFLAGS_EARLY_START;
1848
0
  if (session->internals.hsk_flags & HSK_USED_FFDHE)
1849
0
    flags |= GNUTLS_SFLAGS_RFC7919;
1850
0
  if (session->internals.hsk_flags & HSK_TICKET_RECEIVED)
1851
0
    flags |= GNUTLS_SFLAGS_SESSION_TICKET;
1852
0
  if (session->security_parameters.post_handshake_auth)
1853
0
    flags |= GNUTLS_SFLAGS_POST_HANDSHAKE_AUTH;
1854
0
  if (session->internals.hsk_flags & HSK_EARLY_DATA_ACCEPTED)
1855
0
    flags |= GNUTLS_SFLAGS_EARLY_DATA;
1856
0
  if (session->internals.hsk_flags & HSK_OCSP_REQUESTED)
1857
0
    flags |= GNUTLS_SFLAGS_CLI_REQUESTED_OCSP;
1858
0
  if (session->internals.hsk_flags & HSK_CLIENT_OCSP_REQUESTED)
1859
0
    flags |= GNUTLS_SFLAGS_SERV_REQUESTED_OCSP;
1860
1861
0
  return flags;
1862
0
}