Coverage Report

Created: 2024-07-23 07:36

/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
0
  gnutls_pk_params_release(&session->key.kshare.kem_params);
463
464
0
  if (!vers->tls13_sem && session->key.binders[0].prf == NULL) {
465
0
    gnutls_pk_params_release(&session->key.proto.tls12.ecdh.params);
466
0
    gnutls_pk_params_release(&session->key.proto.tls12.dh.params);
467
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.ecdh.x);
468
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.ecdh.y);
469
0
    _gnutls_free_temp_key_datum(&session->key.proto.tls12.ecdh.raw);
470
471
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.dh.client_Y);
472
473
    /* SRP */
474
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_p);
475
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_g);
476
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_key);
477
478
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.u);
479
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.a);
480
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.x);
481
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.A);
482
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.B);
483
0
    zrelease_temp_mpi_key(&session->key.proto.tls12.srp.b);
484
0
  } else {
485
0
    gnutls_memset(session->key.proto.tls13.temp_secret, 0,
486
0
            sizeof(session->key.proto.tls13.temp_secret));
487
0
  }
488
489
0
  reset_binders(session);
490
0
  _gnutls_free_temp_key_datum(&session->key.key);
491
0
}
492
493
/* An internal version of _gnutls_handshake_internal_state_clear(),
494
 * it will not attempt to deallocate, only initialize */
495
static void handshake_internal_state_clear1(gnutls_session_t session)
496
0
{
497
  /* by default no selected certificate */
498
0
  session->internals.adv_version_major = 0;
499
0
  session->internals.adv_version_minor = 0;
500
0
  session->internals.direction = 0;
501
502
  /* use out of band data for the last
503
   * handshake messages received.
504
   */
505
0
  session->internals.last_handshake_in = -1;
506
0
  session->internals.last_handshake_out = -1;
507
508
0
  session->internals.resumable = true;
509
510
0
  session->internals.handshake_suspicious_loops = 0;
511
0
  session->internals.dtls.hsk_read_seq = 0;
512
0
  session->internals.dtls.hsk_write_seq = 0;
513
514
0
  session->internals.cand_ec_group = 0;
515
0
  session->internals.cand_dh_group = 0;
516
517
0
  session->internals.hrr_cs[0] = CS_INVALID_MAJOR;
518
0
  session->internals.hrr_cs[1] = CS_INVALID_MINOR;
519
0
}
520
521
/* This function will clear all the variables in internals
522
 * structure within the session, which depend on the current handshake.
523
 * This is used to allow further handshakes.
524
 */
525
void _gnutls_handshake_internal_state_clear(gnutls_session_t session)
526
0
{
527
0
  handshake_internal_state_clear1(session);
528
529
0
  _gnutls_handshake_hash_buffers_clear(session);
530
0
  deinit_keys(session);
531
532
0
  _gnutls_epoch_gc(session);
533
534
0
  session->internals.handshake_abs_timeout.tv_sec = 0;
535
0
  session->internals.handshake_abs_timeout.tv_nsec = 0;
536
0
  session->internals.handshake_in_progress = 0;
537
538
0
  session->internals.tfo.connect_addrlen = 0;
539
0
  session->internals.tfo.connect_only = 0;
540
0
  session->internals.early_data_received = 0;
541
0
  session->internals.session_ticket_renew = 0;
542
0
}
543
544
/**
545
 * gnutls_init:
546
 * @session: is a pointer to a #gnutls_session_t type.
547
 * @flags: indicate if this session is to be used for server or client.
548
 *
549
 * This function initializes the provided session. Every session must
550
 * be initialized before use, and after successful initialization and
551
 * use must be deinitialized by calling gnutls_deinit().
552
 *
553
 * @flags can be any combination of flags from %gnutls_init_flags_t.
554
 *
555
 * Note that since version 3.1.2 this function enables some common
556
 * TLS extensions such as session tickets and OCSP certificate status
557
 * request in client side by default. To prevent that use the %GNUTLS_NO_DEFAULT_EXTENSIONS
558
 * flag.
559
 *
560
 * Note that it is never mandatory to use gnutls_deinit() after this
561
 * function fails.  Since gnutls 3.8.0, it is safe to unconditionally
562
 * use gnutls_deinit() even after failure regardless of whether the
563
 * memory was initialized prior to gnutls_init(); however, clients
564
 * wanting to be portable to older versions of the library should
565
 * either skip deinitialization on failure, or pre-initialize the
566
 * memory passed in to gnutls_init() to all zeroes via memset() or
567
 * similar.
568
 *
569
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
570
 **/
571
int gnutls_init(gnutls_session_t *session, unsigned int flags)
572
0
{
573
0
  int ret;
574
575
0
  *session = NULL;
576
0
  FAIL_IF_LIB_ERROR;
577
578
0
  *session = gnutls_calloc(1, sizeof(struct gnutls_session_int));
579
0
  if (*session == NULL)
580
0
    return GNUTLS_E_MEMORY_ERROR;
581
582
0
  ret = gnutls_mutex_init(&(*session)->internals.post_negotiation_lock);
583
0
  if (ret < 0) {
584
0
    gnutls_assert();
585
0
    gnutls_free(*session);
586
0
    return ret;
587
0
  }
588
589
0
  ret = gnutls_mutex_init(&(*session)->internals.epoch_lock);
590
0
  if (ret < 0) {
591
0
    gnutls_assert();
592
0
    gnutls_mutex_deinit(
593
0
      &(*session)->internals.post_negotiation_lock);
594
0
    gnutls_free(*session);
595
0
    return ret;
596
0
  }
597
598
0
  ret = _gnutls_epoch_setup_next(*session, 1, NULL);
599
0
  if (ret < 0) {
600
0
    gnutls_mutex_deinit(
601
0
      &(*session)->internals.post_negotiation_lock);
602
0
    gnutls_mutex_deinit(&(*session)->internals.epoch_lock);
603
0
    gnutls_free(*session);
604
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
605
0
  }
606
0
  _gnutls_epoch_bump(*session);
607
608
0
  (*session)->security_parameters.entity =
609
0
    (flags & GNUTLS_SERVER ? GNUTLS_SERVER : GNUTLS_CLIENT);
610
611
  /* the default certificate type for TLS */
612
0
  (*session)->security_parameters.client_ctype = DEFAULT_CERT_TYPE;
613
0
  (*session)->security_parameters.server_ctype = DEFAULT_CERT_TYPE;
614
615
  /* Initialize buffers */
616
0
  _gnutls_buffer_init(&(*session)->internals.handshake_hash_buffer);
617
0
  _gnutls_buffer_init(&(*session)->internals.post_handshake_hash_buffer);
618
0
  _gnutls_buffer_init(&(*session)->internals.hb_remote_data);
619
0
  _gnutls_buffer_init(&(*session)->internals.hb_local_data);
620
0
  _gnutls_buffer_init(&(*session)->internals.record_presend_buffer);
621
0
  _gnutls_buffer_init(&(*session)->internals.record_key_update_buffer);
622
0
  _gnutls_buffer_init(&(*session)->internals.reauth_buffer);
623
624
0
  _mbuffer_head_init(&(*session)->internals.record_buffer);
625
0
  _mbuffer_head_init(&(*session)->internals.record_send_buffer);
626
0
  _mbuffer_head_init(&(*session)->internals.record_recv_buffer);
627
0
  _mbuffer_head_init(&(*session)->internals.early_data_recv_buffer);
628
0
  _gnutls_buffer_init(&(*session)->internals.early_data_presend_buffer);
629
630
0
  _mbuffer_head_init(&(*session)->internals.handshake_send_buffer);
631
0
  _gnutls_handshake_recv_buffer_init(*session);
632
633
0
  (*session)->internals.expire_time = DEFAULT_EXPIRE_TIME;
634
635
  /* Ticket key rotation - set the default X to 3 times the ticket expire time */
636
0
  (*session)->key.totp.last_result = 0;
637
638
0
  gnutls_handshake_set_max_packet_length((*session),
639
0
                 MAX_HANDSHAKE_PACKET_SIZE);
640
641
  /* set the socket pointers to -1;
642
   */
643
0
  (*session)->internals.transport_recv_ptr = (gnutls_transport_ptr_t)-1;
644
0
  (*session)->internals.transport_send_ptr = (gnutls_transport_ptr_t)-1;
645
646
  /* set the default maximum record size for TLS
647
   */
648
0
  (*session)->security_parameters.max_record_recv_size =
649
0
    DEFAULT_MAX_RECORD_SIZE;
650
0
  (*session)->security_parameters.max_record_send_size =
651
0
    DEFAULT_MAX_RECORD_SIZE;
652
0
  (*session)->security_parameters.max_user_record_recv_size =
653
0
    DEFAULT_MAX_RECORD_SIZE;
654
0
  (*session)->security_parameters.max_user_record_send_size =
655
0
    DEFAULT_MAX_RECORD_SIZE;
656
657
  /* set the default early data size for TLS
658
   */
659
0
  if ((*session)->security_parameters.entity == GNUTLS_SERVER) {
660
0
    (*session)->security_parameters.max_early_data_size =
661
0
      DEFAULT_MAX_EARLY_DATA_SIZE;
662
0
  } else {
663
0
    (*session)->security_parameters.max_early_data_size =
664
0
      UINT32_MAX;
665
0
  }
666
667
  /* Everything else not initialized here is initialized as NULL
668
   * or 0. This is why calloc is used. However, we want to
669
   * ensure that certain portions of data are initialized at
670
   * runtime before being used. Mark such regions with a
671
   * valgrind client request as undefined.
672
   */
673
0
  _gnutls_memory_mark_undefined(
674
0
    (*session)->security_parameters.master_secret,
675
0
    GNUTLS_MASTER_SIZE);
676
0
  _gnutls_memory_mark_undefined(
677
0
    (*session)->security_parameters.client_random,
678
0
    GNUTLS_RANDOM_SIZE);
679
0
  _gnutls_memory_mark_undefined(
680
0
    (*session)->security_parameters.server_random,
681
0
    GNUTLS_RANDOM_SIZE);
682
0
  _gnutls_memory_mark_undefined((*session)->key.session_ticket_key,
683
0
              TICKET_MASTER_KEY_SIZE);
684
0
  _gnutls_memory_mark_undefined((*session)->key.previous_ticket_key,
685
0
              TICKET_MASTER_KEY_SIZE);
686
0
  _gnutls_memory_mark_undefined((*session)->key.initial_stek,
687
0
              TICKET_MASTER_KEY_SIZE);
688
689
0
  handshake_internal_state_clear1(*session);
690
691
0
#ifdef MSG_NOSIGNAL
692
0
  if (flags & GNUTLS_NO_SIGNAL)
693
0
    gnutls_transport_set_vec_push_function(*session,
694
0
                   system_writev_nosignal);
695
0
  else
696
0
#endif
697
0
    gnutls_transport_set_vec_push_function(*session, system_writev);
698
0
  (*session)->internals.pull_timeout_func = gnutls_system_recv_timeout;
699
0
  (*session)->internals.pull_func = system_read;
700
0
  (*session)->internals.errno_func = system_errno;
701
702
0
  (*session)->internals.saved_username = NULL;
703
0
  (*session)->internals.saved_username_size = -1;
704
705
  /* heartbeat timeouts */
706
0
  (*session)->internals.hb_retrans_timeout_ms = 1000;
707
0
  (*session)->internals.hb_total_timeout_ms = 60000;
708
709
0
  if (flags & GNUTLS_DATAGRAM) {
710
0
    (*session)->internals.dtls.mtu = DTLS_DEFAULT_MTU;
711
0
    (*session)->internals.transport = GNUTLS_DGRAM;
712
713
0
    gnutls_dtls_set_timeouts(*session, DTLS_RETRANS_TIMEOUT, 60000);
714
0
  } else {
715
0
    (*session)->internals.transport = GNUTLS_STREAM;
716
0
  }
717
718
  /* Enable useful extensions */
719
0
  if ((flags & GNUTLS_CLIENT) &&
720
0
      !(flags & GNUTLS_NO_DEFAULT_EXTENSIONS)) {
721
0
#ifdef ENABLE_OCSP
722
0
    if (!(flags & GNUTLS_NO_STATUS_REQUEST))
723
0
      gnutls_ocsp_status_request_enable_client(*session, NULL,
724
0
                 0, NULL);
725
0
#endif
726
0
  }
727
728
  /* session tickets in server side are enabled by setting a key */
729
0
  if (flags & GNUTLS_SERVER)
730
0
    flags |= GNUTLS_NO_TICKETS;
731
732
0
  (*session)->internals.flags = flags;
733
734
0
  if (_gnutls_disable_tls13 != 0)
735
0
    (*session)->internals.flags |= INT_FLAG_NO_TLS13;
736
737
  /* Install the default keylog function */
738
0
  gnutls_session_set_keylog_function(*session, _gnutls_nss_keylog_func);
739
740
0
  return 0;
741
0
}
742
743
/**
744
 * gnutls_deinit:
745
 * @session: is a #gnutls_session_t type.
746
 *
747
 * This function clears all buffers associated with the @session.
748
 * This function will also remove session data from the session
749
 * database if the session was terminated abnormally.
750
 **/
751
void gnutls_deinit(gnutls_session_t session)
752
0
{
753
0
  unsigned int i;
754
755
0
  if (session == NULL)
756
0
    return;
757
758
  /* remove auth info firstly */
759
0
  _gnutls_free_auth_info(session);
760
761
0
  _gnutls_handshake_internal_state_clear(session);
762
0
  _gnutls_handshake_io_buffer_clear(session);
763
0
  _gnutls_hello_ext_priv_deinit(session);
764
765
0
  for (i = 0; i < MAX_EPOCH_INDEX; i++)
766
0
    if (session->record_parameters[i] != NULL) {
767
0
      _gnutls_epoch_free(session,
768
0
             session->record_parameters[i]);
769
0
      session->record_parameters[i] = NULL;
770
0
    }
771
772
0
  _gnutls_buffer_clear(&session->internals.handshake_hash_buffer);
773
0
  _gnutls_buffer_clear(&session->internals.post_handshake_hash_buffer);
774
0
  _gnutls_buffer_clear(&session->internals.hb_remote_data);
775
0
  _gnutls_buffer_clear(&session->internals.hb_local_data);
776
0
  _gnutls_buffer_clear(&session->internals.record_presend_buffer);
777
0
  _gnutls_buffer_clear(&session->internals.record_key_update_buffer);
778
0
  _gnutls_buffer_clear(&session->internals.reauth_buffer);
779
780
0
  _mbuffer_head_clear(&session->internals.record_buffer);
781
0
  _mbuffer_head_clear(&session->internals.record_recv_buffer);
782
0
  _mbuffer_head_clear(&session->internals.record_send_buffer);
783
784
0
  _mbuffer_head_clear(&session->internals.early_data_recv_buffer);
785
0
  _gnutls_buffer_clear(&session->internals.early_data_presend_buffer);
786
787
0
  _gnutls_free_datum(&session->internals.resumption_data);
788
0
  _gnutls_free_datum(&session->internals.dtls.dcookie);
789
790
0
  for (i = 0; i < session->internals.rexts_size; i++)
791
0
    gnutls_free(session->internals.rexts[i].name);
792
0
  gnutls_free(session->internals.rexts);
793
0
  gnutls_free(session->internals.post_handshake_cr_context.data);
794
795
0
  gnutls_free(session->internals.saved_username);
796
0
  gnutls_free(session->internals.rsup);
797
798
0
  gnutls_credentials_clear(session);
799
0
  _gnutls_selected_certs_deinit(session);
800
801
  /* destroy any session ticket we may have received */
802
0
  tls13_ticket_deinit(&session->internals.tls13_ticket);
803
804
  /* we rely on priorities' internal reference counting */
805
0
  gnutls_priority_deinit(session->internals.priorities);
806
807
  /* overwrite any temp TLS1.3 keys */
808
0
  gnutls_memset(&session->key.proto, 0, sizeof(session->key.proto));
809
810
  /* clear session ticket keys */
811
0
  _gnutls_memory_mark_defined(session->key.session_ticket_key,
812
0
            TICKET_MASTER_KEY_SIZE);
813
0
  gnutls_memset(&session->key.session_ticket_key, 0,
814
0
          TICKET_MASTER_KEY_SIZE);
815
0
  _gnutls_memory_mark_undefined(session->key.session_ticket_key,
816
0
              TICKET_MASTER_KEY_SIZE);
817
818
0
  _gnutls_memory_mark_defined(session->key.previous_ticket_key,
819
0
            TICKET_MASTER_KEY_SIZE);
820
0
  gnutls_memset(&session->key.previous_ticket_key, 0,
821
0
          TICKET_MASTER_KEY_SIZE);
822
0
  _gnutls_memory_mark_undefined(session->key.previous_ticket_key,
823
0
              TICKET_MASTER_KEY_SIZE);
824
825
0
  _gnutls_memory_mark_defined(session->key.initial_stek,
826
0
            TICKET_MASTER_KEY_SIZE);
827
0
  gnutls_memset(&session->key.initial_stek, 0, TICKET_MASTER_KEY_SIZE);
828
0
  _gnutls_memory_mark_undefined(session->key.initial_stek,
829
0
              TICKET_MASTER_KEY_SIZE);
830
831
0
  gnutls_mutex_deinit(&session->internals.post_negotiation_lock);
832
0
  gnutls_mutex_deinit(&session->internals.epoch_lock);
833
834
0
  gnutls_free(session);
835
0
}
836
837
int _gnutls_dh_set_peer_public(gnutls_session_t session, bigint_t public)
838
0
{
839
0
  dh_info_st *dh;
840
0
  int ret;
841
842
0
  switch (gnutls_auth_get_type(session)) {
843
0
  case GNUTLS_CRD_ANON: {
844
0
    anon_auth_info_t info;
845
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON);
846
0
    if (info == NULL)
847
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
848
849
0
    dh = &info->dh;
850
0
    break;
851
0
  }
852
0
  case GNUTLS_CRD_PSK: {
853
0
    psk_auth_info_t info;
854
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
855
0
    if (info == NULL)
856
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
857
858
0
    dh = &info->dh;
859
0
    break;
860
0
  }
861
0
  case GNUTLS_CRD_CERTIFICATE: {
862
0
    cert_auth_info_t info;
863
864
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
865
0
    if (info == NULL)
866
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
867
868
0
    dh = &info->dh;
869
0
    break;
870
0
  }
871
0
  default:
872
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
873
0
  }
874
875
0
  if (dh->public_key.data)
876
0
    _gnutls_free_datum(&dh->public_key);
877
878
0
  ret = _gnutls_mpi_dprint_lz(public, &dh->public_key);
879
0
  if (ret < 0) {
880
0
    gnutls_assert();
881
0
    return ret;
882
0
  }
883
884
0
  return 0;
885
0
}
886
887
int _gnutls_dh_set_secret_bits(gnutls_session_t session, unsigned bits)
888
0
{
889
0
  switch (gnutls_auth_get_type(session)) {
890
0
  case GNUTLS_CRD_ANON: {
891
0
    anon_auth_info_t info;
892
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON);
893
0
    if (info == NULL)
894
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
895
0
    info->dh.secret_bits = bits;
896
0
    break;
897
0
  }
898
0
  case GNUTLS_CRD_PSK: {
899
0
    psk_auth_info_t info;
900
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
901
0
    if (info == NULL)
902
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
903
0
    info->dh.secret_bits = bits;
904
0
    break;
905
0
  }
906
0
  case GNUTLS_CRD_CERTIFICATE: {
907
0
    cert_auth_info_t info;
908
909
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
910
0
    if (info == NULL)
911
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
912
913
0
    info->dh.secret_bits = bits;
914
0
    break;
915
0
  default:
916
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
917
0
  }
918
0
  }
919
920
0
  return 0;
921
0
}
922
923
/* Sets the prime and the generator in the auth info structure.
924
 */
925
int _gnutls_dh_save_group(gnutls_session_t session, bigint_t gen,
926
        bigint_t prime)
927
0
{
928
0
  dh_info_st *dh;
929
0
  int ret;
930
931
0
  switch (gnutls_auth_get_type(session)) {
932
0
  case GNUTLS_CRD_ANON: {
933
0
    anon_auth_info_t info;
934
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON);
935
0
    if (info == NULL)
936
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
937
938
0
    dh = &info->dh;
939
0
    break;
940
0
  }
941
0
  case GNUTLS_CRD_PSK: {
942
0
    psk_auth_info_t info;
943
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
944
0
    if (info == NULL)
945
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
946
947
0
    dh = &info->dh;
948
0
    break;
949
0
  }
950
0
  case GNUTLS_CRD_CERTIFICATE: {
951
0
    cert_auth_info_t info;
952
953
0
    info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
954
0
    if (info == NULL)
955
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
956
957
0
    dh = &info->dh;
958
0
    break;
959
0
  }
960
0
  default:
961
0
    return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
962
0
  }
963
964
0
  if (dh->prime.data)
965
0
    _gnutls_free_datum(&dh->prime);
966
967
0
  if (dh->generator.data)
968
0
    _gnutls_free_datum(&dh->generator);
969
970
  /* prime
971
   */
972
0
  ret = _gnutls_mpi_dprint_lz(prime, &dh->prime);
973
0
  if (ret < 0) {
974
0
    gnutls_assert();
975
0
    return ret;
976
0
  }
977
978
  /* generator
979
   */
980
0
  ret = _gnutls_mpi_dprint_lz(gen, &dh->generator);
981
0
  if (ret < 0) {
982
0
    gnutls_assert();
983
0
    _gnutls_free_datum(&dh->prime);
984
0
    return ret;
985
0
  }
986
987
0
  return 0;
988
0
}
989
990
/**
991
 * gnutls_certificate_send_x509_rdn_sequence:
992
 * @session: a #gnutls_session_t type.
993
 * @status: is 0 or 1
994
 *
995
 * If status is non zero, this function will order gnutls not to send
996
 * the rdnSequence in the certificate request message. That is the
997
 * server will not advertise its trusted CAs to the peer. If status
998
 * is zero then the default behaviour will take effect, which is to
999
 * advertise the server's trusted CAs.
1000
 *
1001
 * This function has no effect in clients, and in authentication
1002
 * methods other than certificate with X.509 certificates.
1003
 **/
1004
void gnutls_certificate_send_x509_rdn_sequence(gnutls_session_t session,
1005
                 int status)
1006
0
{
1007
0
  session->internals.ignore_rdn_sequence = status;
1008
0
}
1009
1010
/*-
1011
 * _gnutls_record_set_default_version - Used to set the default version for the first record packet
1012
 * @session: is a #gnutls_session_t type.
1013
 * @major: is a tls major version
1014
 * @minor: is a tls minor version
1015
 *
1016
 * This function sets the default version that we will use in the first
1017
 * record packet (client hello). This function is only useful to people
1018
 * that know TLS internals and want to debug other implementations.
1019
 -*/
1020
void _gnutls_record_set_default_version(gnutls_session_t session,
1021
          unsigned char major,
1022
          unsigned char minor)
1023
0
{
1024
0
  session->internals.default_record_version[0] = major;
1025
0
  session->internals.default_record_version[1] = minor;
1026
0
}
1027
1028
/*-
1029
 * _gnutls_hello_set_default_version - Used to set the default version for the first record packet
1030
 * @session: is a #gnutls_session_t type.
1031
 * @major: is a tls major version
1032
 * @minor: is a tls minor version
1033
 *
1034
 * This function sets the default version that we will use in the first
1035
 * record packet (client hello). This function is only useful to people
1036
 * that know TLS internals and want to debug other implementations.
1037
 -*/
1038
void _gnutls_hello_set_default_version(gnutls_session_t session,
1039
               unsigned char major, unsigned char minor)
1040
0
{
1041
0
  session->internals.default_hello_version[0] = major;
1042
0
  session->internals.default_hello_version[1] = minor;
1043
0
}
1044
1045
/**
1046
 * gnutls_handshake_set_private_extensions:
1047
 * @session: is a #gnutls_session_t type.
1048
 * @allow: is an integer (0 or 1)
1049
 *
1050
 * This function will enable or disable the use of private cipher
1051
 * suites (the ones that start with 0xFF).  By default or if @allow
1052
 * is 0 then these cipher suites will not be advertised nor used.
1053
 *
1054
 * Currently GnuTLS does not include such cipher-suites or
1055
 * compression algorithms.
1056
 *
1057
 * Enabling the private ciphersuites when talking to other than
1058
 * gnutls servers and clients may cause interoperability problems.
1059
 **/
1060
void gnutls_handshake_set_private_extensions(gnutls_session_t session,
1061
               int allow)
1062
0
{
1063
  /* we have no private extensions */
1064
0
  return;
1065
0
}
1066
1067
/**
1068
 * gnutls_session_is_resumed:
1069
 * @session: is a #gnutls_session_t type.
1070
 *
1071
 * Checks whether session is resumed or not. This is functional
1072
 * for both server and client side.
1073
 *
1074
 * Returns: non zero if this session is resumed, or a zero if this is
1075
 *   a new session.
1076
 **/
1077
int gnutls_session_is_resumed(gnutls_session_t session)
1078
0
{
1079
0
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
1080
0
    const version_entry_st *ver = get_version(session);
1081
0
    if (ver && ver->tls13_sem) {
1082
0
      return session->internals.resumed;
1083
0
    }
1084
1085
0
    if (session->security_parameters.session_id_size > 0 &&
1086
0
        session->security_parameters.session_id_size ==
1087
0
          session->internals.resumed_security_parameters
1088
0
            .session_id_size &&
1089
0
        memcmp(session->security_parameters.session_id,
1090
0
         session->internals.resumed_security_parameters
1091
0
           .session_id,
1092
0
         session->security_parameters.session_id_size) == 0)
1093
0
      return 1;
1094
0
  } else {
1095
0
    if (session->internals.resumed)
1096
0
      return 1;
1097
0
  }
1098
1099
0
  return 0;
1100
0
}
1101
1102
/**
1103
 * gnutls_session_resumption_requested:
1104
 * @session: is a #gnutls_session_t type.
1105
 *
1106
 * Check whether the client has asked for session resumption.
1107
 * This function is valid only on server side.
1108
 *
1109
 * Returns: non zero if session resumption was asked, or a zero if not.
1110
 **/
1111
int gnutls_session_resumption_requested(gnutls_session_t session)
1112
0
{
1113
0
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
1114
0
    return 0;
1115
0
  } else {
1116
0
    return session->internals.resumption_requested;
1117
0
  }
1118
0
}
1119
1120
/*-
1121
 * _gnutls_session_is_psk - Used to check whether this session uses PSK kx
1122
 * @session: is a #gnutls_session_t type.
1123
 *
1124
 * This function will return non zero if this session uses a PSK key
1125
 * exchange algorithm.
1126
 -*/
1127
int _gnutls_session_is_psk(gnutls_session_t session)
1128
0
{
1129
0
  gnutls_kx_algorithm_t kx;
1130
1131
0
  kx = session->security_parameters.cs->kx_algorithm;
1132
0
  if (kx == GNUTLS_KX_PSK || kx == GNUTLS_KX_DHE_PSK ||
1133
0
      kx == GNUTLS_KX_RSA_PSK)
1134
0
    return 1;
1135
1136
0
  return 0;
1137
0
}
1138
1139
/*-
1140
 * _gnutls_session_is_ecc - Used to check whether this session uses ECC kx
1141
 * @session: is a #gnutls_session_t type.
1142
 *
1143
 * This function will return non zero if this session uses an elliptic
1144
 * curves key exchange exchange algorithm.
1145
 -*/
1146
int _gnutls_session_is_ecc(gnutls_session_t session)
1147
0
{
1148
0
  gnutls_kx_algorithm_t kx;
1149
1150
  /* We get the key exchange algorithm through the ciphersuite because
1151
   * the negotiated key exchange might not have been set yet.
1152
   */
1153
0
  kx = session->security_parameters.cs->kx_algorithm;
1154
1155
0
  return _gnutls_kx_is_ecc(kx);
1156
0
}
1157
1158
/**
1159
 * gnutls_session_get_ptr:
1160
 * @session: is a #gnutls_session_t type.
1161
 *
1162
 * Get user pointer for session.  Useful in callbacks.  This is the
1163
 *   pointer set with gnutls_session_set_ptr().
1164
 *
1165
 * Returns: the user given pointer from the session structure, or
1166
 *   %NULL if it was never set.
1167
 **/
1168
void *gnutls_session_get_ptr(gnutls_session_t session)
1169
0
{
1170
0
  return session->internals.user_ptr;
1171
0
}
1172
1173
/**
1174
 * gnutls_session_set_ptr:
1175
 * @session: is a #gnutls_session_t type.
1176
 * @ptr: is the user pointer
1177
 *
1178
 * This function will set (associate) the user given pointer @ptr to
1179
 * the session structure.  This pointer can be accessed with
1180
 * gnutls_session_get_ptr().
1181
 **/
1182
void gnutls_session_set_ptr(gnutls_session_t session, void *ptr)
1183
0
{
1184
0
  session->internals.user_ptr = ptr;
1185
0
}
1186
1187
/**
1188
 * gnutls_session_set_verify_function:
1189
 * @session: is a #gnutls_session_t type.
1190
 * @func: is the callback function
1191
 *
1192
 * This function sets a callback to be called when peer's certificate
1193
 * has been received in order to verify it on receipt rather than
1194
 * doing after the handshake is completed. This overrides any callback
1195
 * set using gnutls_certificate_set_verify_function().
1196
 *
1197
 * The callback's function prototype is:
1198
 * int (*callback)(gnutls_session_t);
1199
 *
1200
 * If the callback function is provided then gnutls will call it, in the
1201
 * handshake, just after the certificate message has been received.
1202
 * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
1203
 * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
1204
 * can be used.
1205
 *
1206
 * The callback function should return 0 for the handshake to continue
1207
 * or non-zero to terminate.
1208
 *
1209
 * Since: 3.4.6
1210
 **/
1211
void gnutls_session_set_verify_function(
1212
  gnutls_session_t session, gnutls_certificate_verify_function *func)
1213
0
{
1214
0
  session->internals.verify_callback = func;
1215
0
}
1216
1217
/**
1218
 * gnutls_record_get_direction:
1219
 * @session: is a #gnutls_session_t type.
1220
 *
1221
 * This function is useful to determine whether a GnuTLS function was interrupted
1222
 * while sending or receiving, so that select() or poll() may be called appropriately.
1223
 *
1224
 * It provides information about the internals of the record
1225
 * protocol and is only useful if a prior gnutls function call,
1226
 * e.g.  gnutls_handshake(), was interrupted and returned
1227
 * %GNUTLS_E_INTERRUPTED or %GNUTLS_E_AGAIN. After such an interrupt
1228
 * applications may call select() or poll() before restoring the
1229
 * interrupted GnuTLS function.
1230
 *
1231
 * This function's output is unreliable if you are using the same
1232
 * @session in different threads for sending and receiving.
1233
 *
1234
 * Returns: 0 if interrupted while trying to read data, or 1 while trying to write data.
1235
 **/
1236
int gnutls_record_get_direction(gnutls_session_t session)
1237
0
{
1238
0
  return session->internals.direction;
1239
0
}
1240
1241
/*-
1242
 * _gnutls_rsa_pms_set_version - Sets a version to be used at the RSA PMS
1243
 * @session: is a #gnutls_session_t type.
1244
 * @major: is the major version to use
1245
 * @minor: is the minor version to use
1246
 *
1247
 * This function will set the given version number to be used at the
1248
 * RSA PMS secret. This is only useful to clients, which want to
1249
 * test server's capabilities.
1250
 -*/
1251
void _gnutls_rsa_pms_set_version(gnutls_session_t session, unsigned char major,
1252
         unsigned char minor)
1253
0
{
1254
0
  session->internals.rsa_pms_version[0] = major;
1255
0
  session->internals.rsa_pms_version[1] = minor;
1256
0
}
1257
1258
void _gnutls_session_client_cert_type_set(gnutls_session_t session,
1259
            gnutls_certificate_type_t ct)
1260
0
{
1261
0
  _gnutls_handshake_log(
1262
0
    "HSK[%p]: Selected client certificate type %s (%d)\n", session,
1263
0
    gnutls_certificate_type_get_name(ct), ct);
1264
0
  session->security_parameters.client_ctype = ct;
1265
0
}
1266
1267
void _gnutls_session_server_cert_type_set(gnutls_session_t session,
1268
            gnutls_certificate_type_t ct)
1269
0
{
1270
0
  _gnutls_handshake_log(
1271
0
    "HSK[%p]: Selected server certificate type %s (%d)\n", session,
1272
0
    gnutls_certificate_type_get_name(ct), ct);
1273
0
  session->security_parameters.server_ctype = ct;
1274
0
}
1275
1276
/**
1277
 * gnutls_handshake_set_post_client_hello_function:
1278
 * @session: is a #gnutls_session_t type.
1279
 * @func: is the function to be called
1280
 *
1281
 * This function will set a callback to be called after the client
1282
 * hello has been received (callback valid in server side only). This
1283
 * allows the server to adjust settings based on received extensions.
1284
 *
1285
 * Those settings could be ciphersuites, requesting certificate, or
1286
 * anything else except for version negotiation (this is done before
1287
 * the hello message is parsed).
1288
 *
1289
 * This callback must return 0 on success or a gnutls error code to
1290
 * terminate the handshake.
1291
 *
1292
 * Since GnuTLS 3.3.5 the callback is
1293
 * allowed to return %GNUTLS_E_AGAIN or %GNUTLS_E_INTERRUPTED to
1294
 * put the handshake on hold. In that case gnutls_handshake()
1295
 * will return %GNUTLS_E_INTERRUPTED and can be resumed when needed.
1296
 *
1297
 * Warning: You should not use this function to terminate the
1298
 * handshake based on client input unless you know what you are
1299
 * doing. Before the handshake is finished there is no way to know if
1300
 * there is a man-in-the-middle attack being performed.
1301
 **/
1302
void gnutls_handshake_set_post_client_hello_function(
1303
  gnutls_session_t session, gnutls_handshake_simple_hook_func func)
1304
0
{
1305
0
  session->internals.user_hello_func = func;
1306
0
}
1307
1308
/**
1309
 * gnutls_session_enable_compatibility_mode:
1310
 * @session: is a #gnutls_session_t type.
1311
 *
1312
 * This function can be used to disable certain (security) features in
1313
 * TLS in order to maintain maximum compatibility with buggy
1314
 * clients. Because several trade-offs with security are enabled,
1315
 * if required they will be reported through the audit subsystem.
1316
 *
1317
 * Normally only servers that require maximum compatibility with
1318
 * everything out there, need to call this function.
1319
 *
1320
 * Note that this function must be called after any call to gnutls_priority
1321
 * functions.
1322
 *
1323
 * Since: 2.1.4
1324
 **/
1325
void gnutls_session_enable_compatibility_mode(gnutls_session_t session)
1326
0
{
1327
0
  ENABLE_COMPAT(&session->internals);
1328
0
}
1329
1330
/**
1331
 * gnutls_session_channel_binding:
1332
 * @session: is a #gnutls_session_t type.
1333
 * @cbtype: an #gnutls_channel_binding_t enumeration type
1334
 * @cb: output buffer array with data
1335
 *
1336
 * Extract given channel binding data of the @cbtype (e.g.,
1337
 * %GNUTLS_CB_TLS_UNIQUE) type.
1338
 *
1339
 * Returns: %GNUTLS_E_SUCCESS on success,
1340
 * %GNUTLS_E_UNIMPLEMENTED_FEATURE if the @cbtype is unsupported,
1341
 * %GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE if the data is not
1342
 * currently available, or an error code.
1343
 *
1344
 * Since: 2.12.0
1345
 **/
1346
int gnutls_session_channel_binding(gnutls_session_t session,
1347
           gnutls_channel_binding_t cbtype,
1348
           gnutls_datum_t *cb)
1349
0
{
1350
0
  if (!session->internals.initial_negotiation_completed)
1351
0
    return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1352
1353
0
  if (cbtype == GNUTLS_CB_TLS_UNIQUE) {
1354
0
    const version_entry_st *ver = get_version(session);
1355
0
    if (unlikely(ver == NULL || ver->tls13_sem))
1356
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1357
1358
0
    cb->size = session->internals.cb_tls_unique_len;
1359
0
    cb->data = gnutls_malloc(cb->size);
1360
0
    if (cb->data == NULL)
1361
0
      return GNUTLS_E_MEMORY_ERROR;
1362
1363
0
    memcpy(cb->data, session->internals.cb_tls_unique, cb->size);
1364
1365
0
    return 0;
1366
0
  }
1367
1368
0
  if (cbtype == GNUTLS_CB_TLS_SERVER_END_POINT) {
1369
0
    const gnutls_datum_t *ders;
1370
0
    unsigned int num_certs = 1;
1371
0
    int ret;
1372
0
    size_t rlen;
1373
0
    gnutls_x509_crt_t cert;
1374
0
    gnutls_digest_algorithm_t algo;
1375
1376
    /* Only X509 certificates are supported for this binding type */
1377
0
    ret = gnutls_certificate_type_get(session);
1378
0
    if (ret != GNUTLS_CRT_X509)
1379
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1380
1381
0
    if (session->security_parameters.entity == GNUTLS_CLIENT)
1382
0
      ders = gnutls_certificate_get_peers(session,
1383
0
                  &num_certs);
1384
0
    else
1385
0
      ders = gnutls_certificate_get_ours(session);
1386
1387
    /* Previous check indicated we have x509 but you never know */
1388
0
    if (!ders || num_certs == 0)
1389
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1390
1391
0
    ret = gnutls_x509_crt_list_import(&cert, &num_certs, ders,
1392
0
              GNUTLS_X509_FMT_DER, 0);
1393
    /* Again, this is not supposed to happen (normally) */
1394
0
    if (ret < 0 || num_certs == 0)
1395
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1396
1397
    /* Obtain signature algorithm used by certificate */
1398
0
    ret = gnutls_x509_crt_get_signature_algorithm(cert);
1399
0
    if (ret < 0 || ret == GNUTLS_SIGN_UNKNOWN)
1400
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1401
1402
    /* obtain hash function from signature and normalize it */
1403
0
    algo = gnutls_sign_get_hash_algorithm(ret);
1404
0
    switch (algo) {
1405
0
    case GNUTLS_DIG_MD5:
1406
0
    case GNUTLS_DIG_SHA1:
1407
0
      algo = GNUTLS_DIG_SHA256;
1408
0
      break;
1409
0
    case GNUTLS_DIG_UNKNOWN:
1410
0
    case GNUTLS_DIG_NULL:
1411
0
    case GNUTLS_DIG_MD5_SHA1:
1412
      /* double hashing not supported either */
1413
0
      gnutls_x509_crt_deinit(cert);
1414
0
      return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1415
0
    default:
1416
0
      break;
1417
0
    }
1418
1419
    /* preallocate 512 bits buffer as maximum supported digest */
1420
0
    rlen = MAX_HASH_SIZE;
1421
0
    cb->data = gnutls_malloc(rlen);
1422
0
    if (cb->data == NULL) {
1423
0
      gnutls_x509_crt_deinit(cert);
1424
0
      return GNUTLS_E_MEMORY_ERROR;
1425
0
    }
1426
1427
0
    ret = gnutls_x509_crt_get_fingerprint(cert, algo, cb->data,
1428
0
                  &rlen);
1429
0
    if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1430
0
      cb->data = gnutls_realloc_fast(cb->data, cb->size);
1431
0
      if (cb->data == NULL) {
1432
0
        gnutls_x509_crt_deinit(cert);
1433
0
        return GNUTLS_E_MEMORY_ERROR;
1434
0
      }
1435
0
      ret = gnutls_x509_crt_get_fingerprint(cert, algo,
1436
0
                    cb->data, &rlen);
1437
0
    }
1438
0
    cb->size = rlen;
1439
0
    gnutls_x509_crt_deinit(cert);
1440
0
    return ret;
1441
0
  }
1442
1443
0
  if (cbtype == GNUTLS_CB_TLS_EXPORTER) {
1444
0
#define RFC5705_LABEL_DATA "EXPORTER-Channel-Binding"
1445
0
#define RFC5705_LABEL_LEN 24
1446
0
#define EXPORTER_CTX_DATA ""
1447
0
#define EXPORTER_CTX_LEN 0
1448
1449
0
    const version_entry_st *ver = get_version(session);
1450
0
    if (unlikely(ver == NULL)) {
1451
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1452
0
    }
1453
1454
    /* "tls-exporter" channel binding is defined only when
1455
     * the TLS handshake results in unique master secrets,
1456
     * i.e., either TLS 1.3, or TLS 1.2 with extended
1457
     * master secret negotiated.
1458
     */
1459
0
    if (!ver->tls13_sem &&
1460
0
        gnutls_session_ext_master_secret_status(session) == 0) {
1461
0
      return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE;
1462
0
    }
1463
1464
0
    cb->size = 32;
1465
0
    cb->data = gnutls_malloc(cb->size);
1466
0
    if (cb->data == NULL)
1467
0
      return GNUTLS_E_MEMORY_ERROR;
1468
1469
0
    return gnutls_prf_rfc5705(session, RFC5705_LABEL_LEN,
1470
0
            RFC5705_LABEL_DATA, EXPORTER_CTX_LEN,
1471
0
            EXPORTER_CTX_DATA, cb->size,
1472
0
            (char *)cb->data);
1473
0
  }
1474
1475
0
  return GNUTLS_E_UNIMPLEMENTED_FEATURE;
1476
0
}
1477
1478
/**
1479
 * gnutls_ecc_curve_get:
1480
 * @session: is a #gnutls_session_t type.
1481
 *
1482
 * Returns the currently used elliptic curve for key exchange. Only valid
1483
 * when using an elliptic curve ciphersuite.
1484
 *
1485
 * Returns: the currently used curve, a #gnutls_ecc_curve_t
1486
 *   type.
1487
 *
1488
 * Since: 3.0
1489
 **/
1490
gnutls_ecc_curve_t gnutls_ecc_curve_get(gnutls_session_t session)
1491
0
{
1492
0
  const gnutls_group_entry_st *e;
1493
1494
0
  e = get_group(session);
1495
0
  if (e == NULL || e->curve == 0)
1496
0
    return 0;
1497
0
  return e->curve;
1498
0
}
1499
1500
/**
1501
 * gnutls_group_get:
1502
 * @session: is a #gnutls_session_t type.
1503
 *
1504
 * Returns the currently used group for key exchange. Only valid
1505
 * when using an elliptic curve or DH ciphersuite.
1506
 *
1507
 * Returns: the currently used group, a #gnutls_group_t
1508
 *   type.
1509
 *
1510
 * Since: 3.6.0
1511
 **/
1512
gnutls_group_t gnutls_group_get(gnutls_session_t session)
1513
0
{
1514
0
  const gnutls_group_entry_st *e;
1515
1516
0
  e = get_group(session);
1517
0
  if (e == NULL)
1518
0
    return 0;
1519
0
  return e->id;
1520
0
}
1521
1522
/**
1523
 * gnutls_protocol_get_version:
1524
 * @session: is a #gnutls_session_t type.
1525
 *
1526
 * Get TLS version, a #gnutls_protocol_t value.
1527
 *
1528
 * Returns: The version of the currently used protocol.
1529
 **/
1530
gnutls_protocol_t gnutls_protocol_get_version(gnutls_session_t session)
1531
0
{
1532
0
  return get_num_version(session);
1533
0
}
1534
1535
/**
1536
 * gnutls_session_get_random:
1537
 * @session: is a #gnutls_session_t type.
1538
 * @client: the client part of the random
1539
 * @server: the server part of the random
1540
 *
1541
 * This function returns pointers to the client and server
1542
 * random fields used in the TLS handshake. The pointers are
1543
 * not to be modified or deallocated.
1544
 *
1545
 * If a client random value has not yet been established, the output
1546
 * will be garbage.
1547
 *
1548
 * Since: 3.0
1549
 **/
1550
void gnutls_session_get_random(gnutls_session_t session, gnutls_datum_t *client,
1551
             gnutls_datum_t *server)
1552
0
{
1553
0
  if (client) {
1554
0
    client->data = session->security_parameters.client_random;
1555
0
    client->size =
1556
0
      sizeof(session->security_parameters.client_random);
1557
0
  }
1558
1559
0
  if (server) {
1560
0
    server->data = session->security_parameters.server_random;
1561
0
    server->size =
1562
0
      sizeof(session->security_parameters.server_random);
1563
0
  }
1564
0
}
1565
1566
/**
1567
 * gnutls_session_get_master_secret:
1568
 * @session: is a #gnutls_session_t type.
1569
 * @secret: the session's master secret
1570
 *
1571
 * This function returns pointers to the master secret
1572
 * used in the TLS session. The pointers are not to be modified or deallocated.
1573
 *
1574
 * This function is only applicable under TLS 1.2 or earlier versions.
1575
 *
1576
 * Since: 3.5.0
1577
 **/
1578
void gnutls_session_get_master_secret(gnutls_session_t session,
1579
              gnutls_datum_t *secret)
1580
0
{
1581
0
  secret->data = session->security_parameters.master_secret;
1582
0
  secret->size = sizeof(session->security_parameters.master_secret);
1583
0
}
1584
1585
unsigned int timespec_sub_ms(struct timespec *a, struct timespec *b)
1586
0
{
1587
0
  time_t dsecs;
1588
1589
0
  dsecs = a->tv_sec - b->tv_sec;
1590
0
  if (!INT_MULTIPLY_OVERFLOW(dsecs, 1000)) {
1591
0
    return (dsecs * 1000 +
1592
0
      (a->tv_nsec - b->tv_nsec) / (1000 * 1000));
1593
0
  } else {
1594
0
    return UINT_MAX;
1595
0
  }
1596
0
}
1597
1598
/**
1599
 * gnutls_handshake_set_random:
1600
 * @session: is a #gnutls_session_t type.
1601
 * @random: a random value of 32-bytes
1602
 *
1603
 * This function will explicitly set the server or client hello 
1604
 * random value in the subsequent TLS handshake. The random value 
1605
 * should be a 32-byte value.
1606
 *
1607
 * Note that this function should not normally be used as gnutls
1608
 * will select automatically a random value for the handshake.
1609
 *
1610
 * This function should not be used when resuming a session.
1611
 *
1612
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1613
 *
1614
 * Since 3.1.9
1615
 **/
1616
int gnutls_handshake_set_random(gnutls_session_t session,
1617
        const gnutls_datum_t *random)
1618
0
{
1619
0
  if (random->size != GNUTLS_RANDOM_SIZE)
1620
0
    return GNUTLS_E_INVALID_REQUEST;
1621
1622
0
  session->internals.sc_random_set = 1;
1623
0
  if (session->security_parameters.entity == GNUTLS_CLIENT)
1624
0
    memcpy(session->internals.resumed_security_parameters
1625
0
             .client_random,
1626
0
           random->data, random->size);
1627
0
  else
1628
0
    memcpy(session->internals.resumed_security_parameters
1629
0
             .server_random,
1630
0
           random->data, random->size);
1631
1632
0
  return 0;
1633
0
}
1634
1635
/**
1636
 * gnutls_handshake_set_hook_function:
1637
 * @session: is a #gnutls_session_t type
1638
 * @htype: the %gnutls_handshake_description_t of the message to hook at
1639
 * @when: %GNUTLS_HOOK_* depending on when the hook function should be called
1640
 * @func: is the function to be called
1641
 *
1642
 * This function will set a callback to be called after or before the specified
1643
 * handshake message has been received or generated. This is a
1644
 * generalization of gnutls_handshake_set_post_client_hello_function().
1645
 *
1646
 * To call the hook function prior to the message being generated or processed
1647
 * use %GNUTLS_HOOK_PRE as @when parameter, %GNUTLS_HOOK_POST to call
1648
 * after, and %GNUTLS_HOOK_BOTH for both cases.
1649
 *
1650
 * This callback must return 0 on success or a gnutls error code to
1651
 * terminate the handshake.
1652
 *
1653
 * To hook at all handshake messages use an @htype of %GNUTLS_HANDSHAKE_ANY.
1654
 *
1655
 * Warning: You should not use this function to terminate the
1656
 * handshake based on client input unless you know what you are
1657
 * doing. Before the handshake is finished there is no way to know if
1658
 * there is a man-in-the-middle attack being performed.
1659
 **/
1660
void gnutls_handshake_set_hook_function(gnutls_session_t session,
1661
          unsigned int htype, int when,
1662
          gnutls_handshake_hook_func func)
1663
0
{
1664
0
  session->internals.h_hook = func;
1665
0
  session->internals.h_type = htype;
1666
0
  session->internals.h_post = when;
1667
0
}
1668
1669
/**
1670
 * gnutls_handshake_set_read_function:
1671
 * @session: is #gnutls_session_t type
1672
 * @func: is the function to be called
1673
 *
1674
 * This function will set a callback to be called when a handshake
1675
 * message is being sent.
1676
 *
1677
 * Since: 3.7.0
1678
 */
1679
void gnutls_handshake_set_read_function(gnutls_session_t session,
1680
          gnutls_handshake_read_func func)
1681
0
{
1682
0
  session->internals.h_read_func = func;
1683
0
}
1684
1685
/**
1686
 * gnutls_alert_set_read_function:
1687
 * @session: is #gnutls_session_t type
1688
 * @func: is the function to be called
1689
 *
1690
 * This function will set a callback to be called when an alert
1691
 * message is being sent.
1692
 *
1693
 * Since: 3.7.0
1694
 */
1695
void gnutls_alert_set_read_function(gnutls_session_t session,
1696
            gnutls_alert_read_func func)
1697
0
{
1698
0
  session->internals.alert_read_func = func;
1699
0
}
1700
1701
/**
1702
 * gnutls_record_get_state:
1703
 * @session: is a #gnutls_session_t type
1704
 * @read: if non-zero the read parameters are returned, otherwise the write
1705
 * @mac_key: the key used for MAC (if a MAC is used)
1706
 * @IV: the initialization vector or nonce used
1707
 * @cipher_key: the cipher key
1708
 * @seq_number: A 64-bit sequence number
1709
 *
1710
 * This function will return the parameters of the current record state.
1711
 * These are only useful to be provided to an external off-loading device
1712
 * or subsystem. The returned values should be considered constant
1713
 * and valid for the lifetime of the session.
1714
 *
1715
 * In that case, to sync the state back you must call gnutls_record_set_state().
1716
 *
1717
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1718
 *
1719
 * Since 3.4.0
1720
 **/
1721
int gnutls_record_get_state(gnutls_session_t session, unsigned read,
1722
          gnutls_datum_t *mac_key, gnutls_datum_t *IV,
1723
          gnutls_datum_t *cipher_key,
1724
          unsigned char seq_number[8])
1725
0
{
1726
0
  record_parameters_st *record_params;
1727
0
  record_state_st *record_state;
1728
0
  unsigned int epoch;
1729
0
  int ret;
1730
1731
0
  if (read)
1732
0
    epoch = EPOCH_READ_CURRENT;
1733
0
  else
1734
0
    epoch = EPOCH_WRITE_CURRENT;
1735
1736
0
  ret = _gnutls_epoch_get(session, epoch, &record_params);
1737
0
  if (ret < 0)
1738
0
    return gnutls_assert_val(ret);
1739
1740
0
  if (!record_params->initialized)
1741
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1742
1743
0
  if (read)
1744
0
    record_state = &record_params->read;
1745
0
  else
1746
0
    record_state = &record_params->write;
1747
1748
0
  if (mac_key) {
1749
0
    mac_key->data = record_state->mac_key;
1750
0
    mac_key->size = record_state->mac_key_size;
1751
0
  }
1752
1753
0
  if (IV) {
1754
0
    IV->data = record_state->iv;
1755
0
    IV->size = record_state->iv_size;
1756
0
  }
1757
1758
0
  if (cipher_key) {
1759
0
    cipher_key->data = record_state->key;
1760
0
    cipher_key->size = record_state->key_size;
1761
0
  }
1762
1763
0
  if (seq_number)
1764
0
    _gnutls_write_uint64(record_state->sequence_number, seq_number);
1765
0
  return 0;
1766
0
}
1767
1768
/**
1769
 * gnutls_record_set_state:
1770
 * @session: is a #gnutls_session_t type
1771
 * @read: if non-zero the read parameters are returned, otherwise the write
1772
 * @seq_number: A 64-bit sequence number
1773
 *
1774
 * This function will set the sequence number in the current record state.
1775
 * This function is useful if sending and receiving are offloaded from
1776
 * gnutls. That is, if gnutls_record_get_state() was used.
1777
 *
1778
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1779
 *
1780
 * Since 3.4.0
1781
 **/
1782
int gnutls_record_set_state(gnutls_session_t session, unsigned read,
1783
          const unsigned char seq_number[8])
1784
0
{
1785
0
  record_parameters_st *record_params;
1786
0
  record_state_st *record_state;
1787
0
  int epoch, ret;
1788
1789
0
  if (read)
1790
0
    epoch = EPOCH_READ_CURRENT;
1791
0
  else
1792
0
    epoch = EPOCH_WRITE_CURRENT;
1793
1794
0
  ret = _gnutls_epoch_get(session, epoch, &record_params);
1795
0
  if (ret < 0)
1796
0
    return gnutls_assert_val(ret);
1797
1798
0
  if (!record_params->initialized)
1799
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1800
1801
0
  if (read)
1802
0
    record_state = &record_params->read;
1803
0
  else
1804
0
    record_state = &record_params->write;
1805
1806
0
  record_state->sequence_number = _gnutls_read_uint64(seq_number);
1807
1808
0
  if (IS_DTLS(session)) {
1809
0
    _dtls_reset_window(record_params);
1810
0
  }
1811
1812
0
  return 0;
1813
0
}
1814
1815
/**
1816
 * gnutls_session_get_flags:
1817
 * @session: is a #gnutls_session_t type.
1818
 *
1819
 * This function will return a series (ORed) of flags, applicable
1820
 * for the current session.
1821
 *
1822
 * This replaces individual informational functions such as
1823
 * gnutls_safe_renegotiation_status(), gnutls_session_ext_master_secret_status(),
1824
 * etc.
1825
 *
1826
 * Returns: An ORed sequence of flags (see %gnutls_session_flags_t)
1827
 *
1828
 * Since: 3.5.0
1829
 **/
1830
unsigned gnutls_session_get_flags(gnutls_session_t session)
1831
0
{
1832
0
  unsigned flags = 0;
1833
1834
0
  if (gnutls_safe_renegotiation_status(session))
1835
0
    flags |= GNUTLS_SFLAGS_SAFE_RENEGOTIATION;
1836
0
  if (gnutls_session_ext_master_secret_status(session))
1837
0
    flags |= GNUTLS_SFLAGS_EXT_MASTER_SECRET;
1838
0
  if (gnutls_session_etm_status(session))
1839
0
    flags |= GNUTLS_SFLAGS_ETM;
1840
0
  if (gnutls_heartbeat_allowed(session, GNUTLS_HB_LOCAL_ALLOWED_TO_SEND))
1841
0
    flags |= GNUTLS_SFLAGS_HB_LOCAL_SEND;
1842
0
  if (gnutls_heartbeat_allowed(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND))
1843
0
    flags |= GNUTLS_SFLAGS_HB_PEER_SEND;
1844
0
  if (session->internals.hsk_flags & HSK_FALSE_START_USED)
1845
0
    flags |= GNUTLS_SFLAGS_FALSE_START;
1846
0
  if ((session->internals.hsk_flags & HSK_EARLY_START_USED) &&
1847
0
      (session->internals.flags & GNUTLS_ENABLE_EARLY_START))
1848
0
    flags |= GNUTLS_SFLAGS_EARLY_START;
1849
0
  if (session->internals.hsk_flags & HSK_USED_FFDHE)
1850
0
    flags |= GNUTLS_SFLAGS_RFC7919;
1851
0
  if (session->internals.hsk_flags & HSK_TICKET_RECEIVED)
1852
0
    flags |= GNUTLS_SFLAGS_SESSION_TICKET;
1853
0
  if (session->security_parameters.post_handshake_auth)
1854
0
    flags |= GNUTLS_SFLAGS_POST_HANDSHAKE_AUTH;
1855
0
  if (session->internals.hsk_flags & HSK_EARLY_DATA_ACCEPTED)
1856
0
    flags |= GNUTLS_SFLAGS_EARLY_DATA;
1857
0
  if (session->internals.hsk_flags & HSK_OCSP_REQUESTED)
1858
0
    flags |= GNUTLS_SFLAGS_CLI_REQUESTED_OCSP;
1859
0
  if (session->internals.hsk_flags & HSK_CLIENT_OCSP_REQUESTED)
1860
0
    flags |= GNUTLS_SFLAGS_SERV_REQUESTED_OCSP;
1861
1862
0
  return flags;
1863
0
}