Coverage Report

Created: 2025-06-13 06:56

/src/openssl/ssl/ssl_sess.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2005 Nokia. All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#if defined(__TANDEM) && defined(_SPT_MODEL_)
12
# include <spthread.h>
13
# include <spt_extensions.h> /* timeval */
14
#endif
15
#include <stdio.h>
16
#include <openssl/rand.h>
17
#include <openssl/engine.h>
18
#include "internal/refcount.h"
19
#include "internal/cryptlib.h"
20
#include "internal/ssl_unwrap.h"
21
#include "ssl_local.h"
22
#include "statem/statem_local.h"
23
24
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
25
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
26
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
27
28
DEFINE_STACK_OF(SSL_SESSION)
29
30
__owur static ossl_inline int sess_timedout(OSSL_TIME t, SSL_SESSION *ss)
31
56
{
32
56
    return ossl_time_compare(t, ss->calc_timeout) > 0;
33
56
}
34
35
/*
36
 * Returns -1/0/+1 as other XXXcmp-type functions
37
 * Takes calculated timeout into consideration
38
 */
39
__owur static ossl_inline int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b)
40
0
{
41
0
    return ossl_time_compare(a->calc_timeout, b->calc_timeout);
42
0
}
43
44
/*
45
 * Calculates effective timeout
46
 * Locking must be done by the caller of this function
47
 */
48
void ssl_session_calculate_timeout(SSL_SESSION *ss)
49
9.36k
{
50
9.36k
    ss->calc_timeout = ossl_time_add(ss->time, ss->timeout);
51
9.36k
}
52
53
/*
54
 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
55
 * unlike in earlier protocol versions, the session ticket may not have been
56
 * sent yet even though a handshake has finished. The session ticket data could
57
 * come in sometime later...or even change if multiple session ticket messages
58
 * are sent from the server. The preferred way for applications to obtain
59
 * a resumable session is to use SSL_CTX_sess_set_new_cb().
60
 */
61
62
SSL_SESSION *SSL_get_session(const SSL *ssl)
63
/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
64
0
{
65
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
66
67
0
    if (sc == NULL)
68
0
        return NULL;
69
70
0
    return sc->session;
71
0
}
72
73
SSL_SESSION *SSL_get1_session(SSL *ssl)
74
/* variant of SSL_get_session: caller really gets something */
75
0
{
76
0
    SSL_SESSION *sess;
77
78
    /*
79
     * Need to lock this all up rather than just use CRYPTO_add so that
80
     * somebody doesn't free ssl->session between when we check it's non-null
81
     * and when we up the reference count.
82
     */
83
0
    if (!CRYPTO_THREAD_read_lock(ssl->lock))
84
0
        return NULL;
85
0
    sess = SSL_get_session(ssl);
86
0
    if (sess != NULL && !SSL_SESSION_up_ref(sess))
87
0
        sess = NULL;
88
0
    CRYPTO_THREAD_unlock(ssl->lock);
89
0
    return sess;
90
0
}
91
92
int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
93
0
{
94
0
    return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
95
0
}
96
97
void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
98
0
{
99
0
    return CRYPTO_get_ex_data(&s->ex_data, idx);
100
0
}
101
102
SSL_SESSION *SSL_SESSION_new(void)
103
4.68k
{
104
4.68k
    SSL_SESSION *ss;
105
106
4.68k
    if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
107
0
        return NULL;
108
109
4.68k
    ss = OPENSSL_zalloc(sizeof(*ss));
110
4.68k
    if (ss == NULL)
111
0
        return NULL;
112
113
4.68k
    ss->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_UNSPECIFIED;
114
4.68k
    ss->verify_result = 1;      /* avoid 0 (= X509_V_OK) just in case */
115
   /* 5 minute timeout by default */
116
4.68k
    ss->timeout = ossl_seconds2time(60 * 5 + 4);
117
4.68k
    ss->time = ossl_time_now();
118
4.68k
    ssl_session_calculate_timeout(ss);
119
4.68k
    if (!CRYPTO_NEW_REF(&ss->references, 1)) {
120
0
        OPENSSL_free(ss);
121
0
        return NULL;
122
0
    }
123
124
4.68k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
125
0
        CRYPTO_FREE_REF(&ss->references);
126
0
        OPENSSL_free(ss);
127
0
        return NULL;
128
0
    }
129
4.68k
    return ss;
130
4.68k
}
131
132
/*
133
 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
134
 * ticket == 0 then no ticket information is duplicated, otherwise it is.
135
 */
136
static SSL_SESSION *ssl_session_dup_intern(const SSL_SESSION *src, int ticket)
137
0
{
138
0
    SSL_SESSION *dest;
139
140
0
    dest = OPENSSL_malloc(sizeof(*dest));
141
0
    if (dest == NULL)
142
0
        return NULL;
143
144
    /*
145
     * src is logically read-only but the prev/next pointers are not, they are
146
     * part of the session cache and can be modified concurrently.
147
     */
148
0
    memcpy(dest, src, offsetof(SSL_SESSION, prev));
149
150
    /*
151
     * Set the various pointers to NULL so that we can call SSL_SESSION_free in
152
     * the case of an error whilst halfway through constructing dest
153
     */
154
0
#ifndef OPENSSL_NO_PSK
155
0
    dest->psk_identity_hint = NULL;
156
0
    dest->psk_identity = NULL;
157
0
#endif
158
0
    dest->ext.hostname = NULL;
159
0
    dest->ext.tick = NULL;
160
0
    dest->ext.alpn_selected = NULL;
161
0
#ifndef OPENSSL_NO_SRP
162
0
    dest->srp_username = NULL;
163
0
#endif
164
0
    dest->peer_chain = NULL;
165
0
    dest->peer = NULL;
166
0
    dest->peer_rpk = NULL;
167
0
    dest->ticket_appdata = NULL;
168
0
    memset(&dest->ex_data, 0, sizeof(dest->ex_data));
169
170
    /* As the copy is not in the cache, we remove the associated pointers */
171
0
    dest->prev = NULL;
172
0
    dest->next = NULL;
173
0
    dest->owner = NULL;
174
175
0
    if (!CRYPTO_NEW_REF(&dest->references, 1)) {
176
0
        OPENSSL_free(dest);
177
0
        return NULL;
178
0
    }
179
180
0
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) {
181
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
182
0
        goto err;
183
0
    }
184
185
0
    if (src->peer != NULL) {
186
0
        if (!X509_up_ref(src->peer)) {
187
0
            ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
188
0
            goto err;
189
0
        }
190
0
        dest->peer = src->peer;
191
0
    }
192
193
0
    if (src->peer_chain != NULL) {
194
0
        dest->peer_chain = X509_chain_up_ref(src->peer_chain);
195
0
        if (dest->peer_chain == NULL) {
196
0
            ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
197
0
            goto err;
198
0
        }
199
0
    }
200
201
0
    if (src->peer_rpk != NULL) {
202
0
        if (!EVP_PKEY_up_ref(src->peer_rpk))
203
0
            goto err;
204
0
        dest->peer_rpk = src->peer_rpk;
205
0
    }
206
207
0
#ifndef OPENSSL_NO_PSK
208
0
    if (src->psk_identity_hint) {
209
0
        dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
210
0
        if (dest->psk_identity_hint == NULL)
211
0
            goto err;
212
0
    }
213
0
    if (src->psk_identity) {
214
0
        dest->psk_identity = OPENSSL_strdup(src->psk_identity);
215
0
        if (dest->psk_identity == NULL)
216
0
            goto err;
217
0
    }
218
0
#endif
219
220
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
221
0
                            &dest->ex_data, &src->ex_data)) {
222
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
223
0
        goto err;
224
0
    }
225
226
0
    if (src->ext.hostname) {
227
0
        dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
228
0
        if (dest->ext.hostname == NULL)
229
0
            goto err;
230
0
    }
231
232
0
    if (ticket != 0 && src->ext.tick != NULL) {
233
0
        dest->ext.tick =
234
0
            OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
235
0
        if (dest->ext.tick == NULL)
236
0
            goto err;
237
0
    } else {
238
0
        dest->ext.tick_lifetime_hint = 0;
239
0
        dest->ext.ticklen = 0;
240
0
    }
241
242
0
    if (src->ext.alpn_selected != NULL) {
243
0
        dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
244
0
                                                 src->ext.alpn_selected_len);
245
0
        if (dest->ext.alpn_selected == NULL)
246
0
            goto err;
247
0
    }
248
249
0
#ifndef OPENSSL_NO_SRP
250
0
    if (src->srp_username) {
251
0
        dest->srp_username = OPENSSL_strdup(src->srp_username);
252
0
        if (dest->srp_username == NULL)
253
0
            goto err;
254
0
    }
255
0
#endif
256
257
0
    if (src->ticket_appdata != NULL) {
258
0
        dest->ticket_appdata =
259
0
            OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
260
0
        if (dest->ticket_appdata == NULL)
261
0
            goto err;
262
0
    }
263
264
0
    return dest;
265
0
 err:
266
0
    SSL_SESSION_free(dest);
267
0
    return NULL;
268
0
}
269
270
SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src)
271
0
{
272
0
    return ssl_session_dup_intern(src, 1);
273
0
}
274
275
/*
276
 * Used internally when duplicating a session which might be already shared.
277
 * We will have resumed the original session. Subsequently we might have marked
278
 * it as non-resumable (e.g. in another thread) - but this copy should be ok to
279
 * resume from.
280
 */
281
SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
282
0
{
283
0
    SSL_SESSION *sess = ssl_session_dup_intern(src, ticket);
284
285
0
    if (sess != NULL)
286
0
        sess->not_resumable = 0;
287
288
0
    return sess;
289
0
}
290
291
const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
292
0
{
293
0
    if (len)
294
0
        *len = (unsigned int)s->session_id_length;
295
0
    return s->session_id;
296
0
}
297
298
const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
299
                                                unsigned int *len)
300
0
{
301
0
    if (len != NULL)
302
0
        *len = (unsigned int)s->sid_ctx_length;
303
0
    return s->sid_ctx;
304
0
}
305
306
unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
307
0
{
308
0
    return s->compress_meth;
309
0
}
310
311
/*
312
 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
313
 * the ID with random junk repeatedly until we have no conflict is going to
314
 * complete in one iteration pretty much "most" of the time (btw:
315
 * understatement). So, if it takes us 10 iterations and we still can't avoid
316
 * a conflict - well that's a reasonable point to call it quits. Either the
317
 * RAND code is broken or someone is trying to open roughly very close to
318
 * 2^256 SSL sessions to our server. How you might store that many sessions
319
 * is perhaps a more interesting question ...
320
 */
321
322
3.31k
#define MAX_SESS_ID_ATTEMPTS 10
323
static int def_generate_session_id(SSL *ssl, unsigned char *id,
324
                                   unsigned int *id_len)
325
3.31k
{
326
3.31k
    unsigned int retry = 0;
327
3.31k
    do {
328
3.31k
        if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0)
329
0
            return 0;
330
3.31k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
331
3.31k
        if (retry > 0) {
332
0
            id[0]++;
333
0
        }
334
3.31k
#endif
335
3.31k
    } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
336
3.31k
           (++retry < MAX_SESS_ID_ATTEMPTS)) ;
337
3.31k
    if (retry < MAX_SESS_ID_ATTEMPTS)
338
3.31k
        return 1;
339
    /* else - woops a session_id match */
340
    /*
341
     * XXX We should also check the external cache -- but the probability of
342
     * a collision is negligible, and we could not prevent the concurrent
343
     * creation of sessions with identical IDs since we currently don't have
344
     * means to atomically check whether a session ID already exists and make
345
     * a reservation for it if it does not (this problem applies to the
346
     * internal cache as well).
347
     */
348
0
    return 0;
349
3.31k
}
350
351
int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss)
352
3.53k
{
353
3.53k
    unsigned int tmp;
354
3.53k
    GEN_SESSION_CB cb = def_generate_session_id;
355
3.53k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
356
357
3.53k
    switch (s->version) {
358
558
    case SSL3_VERSION:
359
670
    case TLS1_VERSION:
360
855
    case TLS1_1_VERSION:
361
3.53k
    case TLS1_2_VERSION:
362
3.53k
    case TLS1_3_VERSION:
363
3.53k
    case DTLS1_BAD_VER:
364
3.53k
    case DTLS1_VERSION:
365
3.53k
    case DTLS1_2_VERSION:
366
3.53k
        ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
367
3.53k
        break;
368
0
    default:
369
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION);
370
0
        return 0;
371
3.53k
    }
372
373
    /*-
374
     * If RFC5077 ticket, use empty session ID (as server).
375
     * Note that:
376
     * (a) ssl_get_prev_session() does lookahead into the
377
     *     ClientHello extensions to find the session ticket.
378
     *     When ssl_get_prev_session() fails, statem_srvr.c calls
379
     *     ssl_get_new_session() in tls_process_client_hello().
380
     *     At that point, it has not yet parsed the extensions,
381
     *     however, because of the lookahead, it already knows
382
     *     whether a ticket is expected or not.
383
     *
384
     * (b) statem_clnt.c calls ssl_get_new_session() before parsing
385
     *     ServerHello extensions, and before recording the session
386
     *     ID received from the server, so this block is a noop.
387
     */
388
3.53k
    if (s->ext.ticket_expected) {
389
224
        ss->session_id_length = 0;
390
224
        return 1;
391
224
    }
392
393
    /* Choose which callback will set the session ID */
394
3.31k
    if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s)->lock))
395
0
        return 0;
396
3.31k
    if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) {
397
0
        CRYPTO_THREAD_unlock(ssl->lock);
398
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
399
0
                 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
400
0
        return 0;
401
0
    }
402
3.31k
    if (s->generate_session_id)
403
0
        cb = s->generate_session_id;
404
3.31k
    else if (s->session_ctx->generate_session_id)
405
0
        cb = s->session_ctx->generate_session_id;
406
3.31k
    CRYPTO_THREAD_unlock(s->session_ctx->lock);
407
3.31k
    CRYPTO_THREAD_unlock(ssl->lock);
408
    /* Choose a session ID */
409
3.31k
    memset(ss->session_id, 0, ss->session_id_length);
410
3.31k
    tmp = (int)ss->session_id_length;
411
3.31k
    if (!cb(ssl, ss->session_id, &tmp)) {
412
        /* The callback failed */
413
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
414
0
                 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
415
0
        return 0;
416
0
    }
417
    /*
418
     * Don't allow the callback to set the session length to zero. nor
419
     * set it higher than it was.
420
     */
421
3.31k
    if (tmp == 0 || tmp > ss->session_id_length) {
422
        /* The callback set an illegal length */
423
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
424
0
                 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
425
0
        return 0;
426
0
    }
427
3.31k
    ss->session_id_length = tmp;
428
    /* Finally, check for a conflict */
429
3.31k
    if (SSL_has_matching_session_id(ssl, ss->session_id,
430
3.31k
                                    (unsigned int)ss->session_id_length)) {
431
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT);
432
0
        return 0;
433
0
    }
434
435
3.31k
    return 1;
436
3.31k
}
437
438
int ssl_get_new_session(SSL_CONNECTION *s, int session)
439
4.53k
{
440
    /* This gets used by clients and servers. */
441
442
4.53k
    SSL_SESSION *ss = NULL;
443
444
4.53k
    if ((ss = SSL_SESSION_new()) == NULL) {
445
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
446
0
        return 0;
447
0
    }
448
449
    /* If the context has a default timeout, use it */
450
4.53k
    if (ossl_time_is_zero(s->session_ctx->session_timeout))
451
0
        ss->timeout = SSL_CONNECTION_GET_SSL(s)->method->get_timeout();
452
4.53k
    else
453
4.53k
        ss->timeout = s->session_ctx->session_timeout;
454
4.53k
    ssl_session_calculate_timeout(ss);
455
456
4.53k
    SSL_SESSION_free(s->session);
457
4.53k
    s->session = NULL;
458
459
4.53k
    if (session) {
460
4.53k
        if (SSL_CONNECTION_IS_TLS13(s)) {
461
            /*
462
             * We generate the session id while constructing the
463
             * NewSessionTicket in TLSv1.3.
464
             */
465
995
            ss->session_id_length = 0;
466
3.53k
        } else if (!ssl_generate_session_id(s, ss)) {
467
            /* SSLfatal() already called */
468
0
            SSL_SESSION_free(ss);
469
0
            return 0;
470
0
        }
471
472
4.53k
    } else {
473
0
        ss->session_id_length = 0;
474
0
    }
475
476
4.53k
    if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
477
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
478
0
        SSL_SESSION_free(ss);
479
0
        return 0;
480
0
    }
481
4.53k
    memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
482
4.53k
    ss->sid_ctx_length = s->sid_ctx_length;
483
4.53k
    s->session = ss;
484
4.53k
    ss->ssl_version = s->version;
485
4.53k
    ss->verify_result = X509_V_OK;
486
487
    /* If client supports extended master secret set it in session */
488
4.53k
    if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
489
138
        ss->flags |= SSL_SESS_FLAG_EXTMS;
490
491
4.53k
    return 1;
492
4.53k
}
493
494
SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s,
495
                                  const unsigned char *sess_id,
496
                                  size_t sess_id_len)
497
37
{
498
37
    SSL_SESSION *ret = NULL;
499
500
37
    if ((s->session_ctx->session_cache_mode
501
37
         & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
502
37
        SSL_SESSION data;
503
504
37
        data.ssl_version = s->version;
505
37
        if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
506
0
            return NULL;
507
508
37
        memcpy(data.session_id, sess_id, sess_id_len);
509
37
        data.session_id_length = sess_id_len;
510
511
37
        if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock))
512
0
            return NULL;
513
37
        ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
514
37
        if (ret != NULL) {
515
            /* don't allow other threads to steal it: */
516
0
            if (!SSL_SESSION_up_ref(ret)) {
517
0
                CRYPTO_THREAD_unlock(s->session_ctx->lock);
518
0
                return NULL;
519
0
            }
520
0
        }
521
37
        CRYPTO_THREAD_unlock(s->session_ctx->lock);
522
37
        if (ret == NULL)
523
37
            ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
524
37
    }
525
526
37
    if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
527
0
        int copy = 1;
528
529
0
        ret = s->session_ctx->get_session_cb(SSL_CONNECTION_GET_USER_SSL(s),
530
0
                                             sess_id, sess_id_len, &copy);
531
532
0
        if (ret != NULL) {
533
0
            if (ret->not_resumable) {
534
                /* If its not resumable then ignore this session */
535
0
                if (!copy)
536
0
                    SSL_SESSION_free(ret);
537
0
                return NULL;
538
0
            }
539
0
            ssl_tsan_counter(s->session_ctx,
540
0
                             &s->session_ctx->stats.sess_cb_hit);
541
542
            /*
543
             * Increment reference count now if the session callback asks us
544
             * to do so (note that if the session structures returned by the
545
             * callback are shared between threads, it must handle the
546
             * reference count itself [i.e. copy == 0], or things won't be
547
             * thread-safe).
548
             */
549
0
            if (copy && !SSL_SESSION_up_ref(ret))
550
0
                return NULL;
551
552
            /*
553
             * Add the externally cached session to the internal cache as
554
             * well if and only if we are supposed to.
555
             */
556
0
            if ((s->session_ctx->session_cache_mode &
557
0
                 SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
558
                /*
559
                 * Either return value of SSL_CTX_add_session should not
560
                 * interrupt the session resumption process. The return
561
                 * value is intentionally ignored.
562
                 */
563
0
                (void)SSL_CTX_add_session(s->session_ctx, ret);
564
0
            }
565
0
        }
566
0
    }
567
568
37
    return ret;
569
37
}
570
571
/*-
572
 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
573
 * connection. It is only called by servers.
574
 *
575
 *   hello: The parsed ClientHello data
576
 *
577
 * Returns:
578
 *   -1: fatal error
579
 *    0: no session found
580
 *    1: a session may have been found.
581
 *
582
 * Side effects:
583
 *   - If a session is found then s->session is pointed at it (after freeing an
584
 *     existing session if need be) and s->verify_result is set from the session.
585
 *   - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
586
 *     if the server should issue a new session ticket (to 0 otherwise).
587
 */
588
int ssl_get_prev_session(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello)
589
3.07k
{
590
    /* This is used only by servers. */
591
592
3.07k
    SSL_SESSION *ret = NULL;
593
3.07k
    int fatal = 0;
594
3.07k
    int try_session_cache = 0;
595
3.07k
    SSL_TICKET_STATUS r;
596
597
3.07k
    if (SSL_CONNECTION_IS_TLS13(s)) {
598
1.09k
        SSL_SESSION_free(s->session);
599
1.09k
        s->session = NULL;
600
        /*
601
         * By default we will send a new ticket. This can be overridden in the
602
         * ticket processing.
603
         */
604
1.09k
        s->ext.ticket_expected = 1;
605
1.09k
        if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
606
1.09k
                                 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
607
1.09k
                                 NULL, 0)
608
1.09k
                || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
609
1.07k
                                        hello->pre_proc_exts, NULL, 0))
610
96
            return -1;
611
612
        /* If we resumed, s->session will now be set */
613
995
        ret = s->session;
614
1.98k
    } else {
615
        /* sets s->ext.ticket_expected */
616
1.98k
        r = tls_get_ticket_from_client(s, hello, &ret);
617
1.98k
        switch (r) {
618
0
        case SSL_TICKET_FATAL_ERR_MALLOC:
619
0
        case SSL_TICKET_FATAL_ERR_OTHER:
620
0
            fatal = 1;
621
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
622
0
            goto err;
623
1.71k
        case SSL_TICKET_NONE:
624
1.82k
        case SSL_TICKET_EMPTY:
625
1.82k
            if (hello->session_id_len > 0) {
626
37
                try_session_cache = 1;
627
37
                ret = lookup_sess_in_cache(s, hello->session_id,
628
37
                                           hello->session_id_len);
629
37
            }
630
1.82k
            break;
631
106
        case SSL_TICKET_NO_DECRYPT:
632
164
        case SSL_TICKET_SUCCESS:
633
164
        case SSL_TICKET_SUCCESS_RENEW:
634
164
            break;
635
1.98k
        }
636
1.98k
    }
637
638
2.98k
    if (ret == NULL)
639
2.92k
        goto err;
640
641
    /* Now ret is non-NULL and we own one of its reference counts. */
642
643
    /* Check TLS version consistency */
644
60
    if (ret->ssl_version != s->version)
645
3
        goto err;
646
647
57
    if (ret->sid_ctx_length != s->sid_ctx_length
648
57
        || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
649
        /*
650
         * We have the session requested by the client, but we don't want to
651
         * use it in this context.
652
         */
653
1
        goto err;               /* treat like cache miss */
654
1
    }
655
656
56
    if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
657
        /*
658
         * We can't be sure if this session is being used out of context,
659
         * which is especially important for SSL_VERIFY_PEER. The application
660
         * should have used SSL[_CTX]_set_session_id_context. For this error
661
         * case, we generate an error instead of treating the event like a
662
         * cache miss (otherwise it would be easy for applications to
663
         * effectively disable the session cache by accident without anyone
664
         * noticing).
665
         */
666
667
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
668
0
                 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
669
0
        fatal = 1;
670
0
        goto err;
671
0
    }
672
673
56
    if (sess_timedout(ossl_time_now(), ret)) {
674
2
        ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout);
675
2
        if (try_session_cache) {
676
            /* session was from the cache, so remove it */
677
0
            SSL_CTX_remove_session(s->session_ctx, ret);
678
0
        }
679
2
        goto err;
680
2
    }
681
682
    /* Check extended master secret extension consistency */
683
54
    if (ret->flags & SSL_SESS_FLAG_EXTMS) {
684
        /* If old session includes extms, but new does not: abort handshake */
685
44
        if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
686
2
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS);
687
2
            fatal = 1;
688
2
            goto err;
689
2
        }
690
44
    } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
691
        /* If new session includes extms, but old does not: do not resume */
692
6
        goto err;
693
6
    }
694
695
46
    if (!SSL_CONNECTION_IS_TLS13(s)) {
696
        /* We already did this for TLS1.3 */
697
46
        SSL_SESSION_free(s->session);
698
46
        s->session = ret;
699
46
    }
700
701
46
    ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit);
702
46
    s->verify_result = s->session->verify_result;
703
46
    return 1;
704
705
2.93k
 err:
706
2.93k
    if (ret != NULL) {
707
14
        SSL_SESSION_free(ret);
708
        /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
709
14
        if (SSL_CONNECTION_IS_TLS13(s))
710
2
            s->session = NULL;
711
712
14
        if (!try_session_cache) {
713
            /*
714
             * The session was from a ticket, so we should issue a ticket for
715
             * the new session
716
             */
717
14
            s->ext.ticket_expected = 1;
718
14
        }
719
14
    }
720
2.93k
    if (fatal)
721
2
        return -1;
722
723
2.93k
    return 0;
724
2.93k
}
725
726
int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
727
472
{
728
472
    int ret = 0;
729
472
    SSL_SESSION *s;
730
731
    /*
732
     * add just 1 reference count for the SSL_CTX's session cache even though
733
     * it has two ways of access: each session is in a doubly linked list and
734
     * an lhash
735
     */
736
472
    if (!SSL_SESSION_up_ref(c))
737
0
        return 0;
738
    /*
739
     * if session c is in already in cache, we take back the increment later
740
     */
741
742
472
    if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
743
0
        SSL_SESSION_free(c);
744
0
        return 0;
745
0
    }
746
472
    s = lh_SSL_SESSION_insert(ctx->sessions, c);
747
748
    /*
749
     * s != NULL iff we already had a session with the given PID. In this
750
     * case, s == c should hold (then we did not really modify
751
     * ctx->sessions), or we're in trouble.
752
     */
753
472
    if (s != NULL && s != c) {
754
        /* We *are* in trouble ... */
755
0
        SSL_SESSION_list_remove(ctx, s);
756
0
        SSL_SESSION_free(s);
757
        /*
758
         * ... so pretend the other session did not exist in cache (we cannot
759
         * handle two SSL_SESSION structures with identical session ID in the
760
         * same cache, which could happen e.g. when two threads concurrently
761
         * obtain the same session from an external cache)
762
         */
763
0
        s = NULL;
764
472
    } else if (s == NULL &&
765
472
               lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
766
        /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
767
768
        /*
769
         * ... so take back the extra reference and also don't add
770
         * the session to the SSL_SESSION_list at this time
771
         */
772
0
        s = c;
773
0
    }
774
775
    /* Adjust last used time, and add back into the cache at the appropriate spot */
776
472
    if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) {
777
0
        c->time = ossl_time_now();
778
0
        ssl_session_calculate_timeout(c);
779
0
    }
780
781
472
    if (s == NULL) {
782
        /*
783
         * new cache entry -- remove old ones if cache has become too large
784
         * delete cache entry *before* add, so we don't remove the one we're adding!
785
         */
786
787
472
        ret = 1;
788
789
472
        if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
790
472
            while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) {
791
0
                if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
792
0
                    break;
793
0
                else
794
0
                    ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full);
795
0
            }
796
472
        }
797
472
    }
798
799
472
    SSL_SESSION_list_add(ctx, c);
800
801
472
    if (s != NULL) {
802
        /*
803
         * existing cache entry -- decrement previously incremented reference
804
         * count because it already takes into account the cache
805
         */
806
807
0
        SSL_SESSION_free(s);    /* s == c */
808
0
        ret = 0;
809
0
    }
810
472
    CRYPTO_THREAD_unlock(ctx->lock);
811
472
    return ret;
812
472
}
813
814
int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
815
2.83k
{
816
2.83k
    return remove_session_lock(ctx, c, 1);
817
2.83k
}
818
819
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
820
2.83k
{
821
2.83k
    SSL_SESSION *r;
822
2.83k
    int ret = 0;
823
824
2.83k
    if ((c != NULL) && (c->session_id_length != 0)) {
825
2.15k
        if (lck) {
826
2.15k
            if (!CRYPTO_THREAD_write_lock(ctx->lock))
827
0
                return 0;
828
2.15k
        }
829
2.15k
        if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
830
403
            ret = 1;
831
403
            r = lh_SSL_SESSION_delete(ctx->sessions, r);
832
403
            SSL_SESSION_list_remove(ctx, r);
833
403
        }
834
2.15k
        c->not_resumable = 1;
835
836
2.15k
        if (lck)
837
2.15k
            CRYPTO_THREAD_unlock(ctx->lock);
838
839
2.15k
        if (ctx->remove_session_cb != NULL)
840
0
            ctx->remove_session_cb(ctx, c);
841
842
2.15k
        if (ret)
843
403
            SSL_SESSION_free(r);
844
2.15k
    }
845
2.83k
    return ret;
846
2.83k
}
847
848
void SSL_SESSION_free(SSL_SESSION *ss)
849
27.1k
{
850
27.1k
    int i;
851
852
27.1k
    if (ss == NULL)
853
21.9k
        return;
854
5.15k
    CRYPTO_DOWN_REF(&ss->references, &i);
855
5.15k
    REF_PRINT_COUNT("SSL_SESSION", i, ss);
856
5.15k
    if (i > 0)
857
472
        return;
858
4.68k
    REF_ASSERT_ISNT(i < 0);
859
860
4.68k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
861
862
4.68k
    OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
863
4.68k
    OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
864
4.68k
    X509_free(ss->peer);
865
4.68k
    EVP_PKEY_free(ss->peer_rpk);
866
4.68k
    OSSL_STACK_OF_X509_free(ss->peer_chain);
867
4.68k
    OPENSSL_free(ss->ext.hostname);
868
4.68k
    OPENSSL_free(ss->ext.tick);
869
4.68k
#ifndef OPENSSL_NO_PSK
870
4.68k
    OPENSSL_free(ss->psk_identity_hint);
871
4.68k
    OPENSSL_free(ss->psk_identity);
872
4.68k
#endif
873
4.68k
#ifndef OPENSSL_NO_SRP
874
4.68k
    OPENSSL_free(ss->srp_username);
875
4.68k
#endif
876
4.68k
    OPENSSL_free(ss->ext.alpn_selected);
877
4.68k
    OPENSSL_free(ss->ticket_appdata);
878
4.68k
    CRYPTO_FREE_REF(&ss->references);
879
4.68k
    OPENSSL_clear_free(ss, sizeof(*ss));
880
4.68k
}
881
882
int SSL_SESSION_up_ref(SSL_SESSION *ss)
883
472
{
884
472
    int i;
885
886
472
    if (CRYPTO_UP_REF(&ss->references, &i) <= 0)
887
0
        return 0;
888
889
472
    REF_PRINT_COUNT("SSL_SESSION", i, ss);
890
472
    REF_ASSERT_ISNT(i < 2);
891
472
    return ((i > 1) ? 1 : 0);
892
472
}
893
894
int SSL_set_session(SSL *s, SSL_SESSION *session)
895
0
{
896
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
897
898
0
    if (sc == NULL)
899
0
        return 0;
900
901
0
    if (session != NULL && !SSL_SESSION_up_ref(session))
902
0
        return 0;
903
904
0
    ssl_clear_bad_session(sc);
905
0
    if (s->defltmeth != s->method) {
906
0
        if (!SSL_set_ssl_method(s, s->defltmeth)) {
907
0
            SSL_SESSION_free(session);
908
0
            return 0;
909
0
        }
910
0
    }
911
912
0
    if (session != NULL)
913
0
        sc->verify_result = session->verify_result;
914
915
0
    SSL_SESSION_free(sc->session);
916
0
    sc->session = session;
917
918
0
    return 1;
919
0
}
920
921
int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
922
                        unsigned int sid_len)
923
0
{
924
0
    if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
925
0
      ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG);
926
0
      return 0;
927
0
    }
928
0
    s->session_id_length = sid_len;
929
0
    if (sid != s->session_id && sid_len > 0)
930
0
        memcpy(s->session_id, sid, sid_len);
931
932
0
    return 1;
933
0
}
934
935
long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
936
0
{
937
0
    OSSL_TIME new_timeout = ossl_seconds2time(t);
938
939
0
    if (s == NULL || t < 0)
940
0
        return 0;
941
0
    if (s->owner != NULL) {
942
0
        if (!CRYPTO_THREAD_write_lock(s->owner->lock))
943
0
            return 0;
944
0
        s->timeout = new_timeout;
945
0
        ssl_session_calculate_timeout(s);
946
0
        SSL_SESSION_list_add(s->owner, s);
947
0
        CRYPTO_THREAD_unlock(s->owner->lock);
948
0
    } else {
949
0
        s->timeout = new_timeout;
950
0
        ssl_session_calculate_timeout(s);
951
0
    }
952
0
    return 1;
953
0
}
954
955
long SSL_SESSION_get_timeout(const SSL_SESSION *s)
956
0
{
957
0
    if (s == NULL)
958
0
        return 0;
959
0
    return (long)ossl_time_to_time_t(s->timeout);
960
0
}
961
962
#ifndef OPENSSL_NO_DEPRECATED_3_4
963
long SSL_SESSION_get_time(const SSL_SESSION *s)
964
0
{
965
0
    return (long) SSL_SESSION_get_time_ex(s);
966
0
}
967
#endif
968
969
time_t SSL_SESSION_get_time_ex(const SSL_SESSION *s)
970
0
{
971
0
    if (s == NULL)
972
0
        return 0;
973
0
    return ossl_time_to_time_t(s->time);
974
0
}
975
976
time_t SSL_SESSION_set_time_ex(SSL_SESSION *s, time_t t)
977
0
{
978
0
    OSSL_TIME new_time = ossl_time_from_time_t(t);
979
980
0
    if (s == NULL)
981
0
        return 0;
982
0
    if (s->owner != NULL) {
983
0
        if (!CRYPTO_THREAD_write_lock(s->owner->lock))
984
0
            return 0;
985
0
        s->time = new_time;
986
0
        ssl_session_calculate_timeout(s);
987
0
        SSL_SESSION_list_add(s->owner, s);
988
0
        CRYPTO_THREAD_unlock(s->owner->lock);
989
0
    } else {
990
0
        s->time = new_time;
991
0
        ssl_session_calculate_timeout(s);
992
0
    }
993
0
    return t;
994
0
}
995
996
#ifndef OPENSSL_NO_DEPRECATED_3_4
997
long SSL_SESSION_set_time(SSL_SESSION *s, long t)
998
0
{
999
0
    return (long) SSL_SESSION_set_time_ex(s, (time_t) t);
1000
0
}
1001
#endif
1002
1003
int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
1004
0
{
1005
0
    return s->ssl_version;
1006
0
}
1007
1008
int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
1009
0
{
1010
0
    s->ssl_version = version;
1011
0
    return 1;
1012
0
}
1013
1014
const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
1015
0
{
1016
0
    return s->cipher;
1017
0
}
1018
1019
int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
1020
0
{
1021
0
    s->cipher = cipher;
1022
0
    return 1;
1023
0
}
1024
1025
const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
1026
0
{
1027
0
    return s->ext.hostname;
1028
0
}
1029
1030
int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
1031
0
{
1032
0
    OPENSSL_free(s->ext.hostname);
1033
0
    if (hostname == NULL) {
1034
0
        s->ext.hostname = NULL;
1035
0
        return 1;
1036
0
    }
1037
0
    s->ext.hostname = OPENSSL_strdup(hostname);
1038
1039
0
    return s->ext.hostname != NULL;
1040
0
}
1041
1042
int SSL_SESSION_has_ticket(const SSL_SESSION *s)
1043
0
{
1044
0
    return (s->ext.ticklen > 0) ? 1 : 0;
1045
0
}
1046
1047
unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
1048
0
{
1049
0
    return s->ext.tick_lifetime_hint;
1050
0
}
1051
1052
void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
1053
                             size_t *len)
1054
0
{
1055
0
    *len = s->ext.ticklen;
1056
0
    if (tick != NULL)
1057
0
        *tick = s->ext.tick;
1058
0
}
1059
1060
uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
1061
0
{
1062
0
    return s->ext.max_early_data;
1063
0
}
1064
1065
int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
1066
0
{
1067
0
    s->ext.max_early_data = max_early_data;
1068
1069
0
    return 1;
1070
0
}
1071
1072
void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
1073
                                    const unsigned char **alpn,
1074
                                    size_t *len)
1075
0
{
1076
0
    *alpn = s->ext.alpn_selected;
1077
0
    *len = s->ext.alpn_selected_len;
1078
0
}
1079
1080
int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
1081
                                   size_t len)
1082
0
{
1083
0
    OPENSSL_free(s->ext.alpn_selected);
1084
0
    if (alpn == NULL || len == 0) {
1085
0
        s->ext.alpn_selected = NULL;
1086
0
        s->ext.alpn_selected_len = 0;
1087
0
        return 1;
1088
0
    }
1089
0
    s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
1090
0
    if (s->ext.alpn_selected == NULL) {
1091
0
        s->ext.alpn_selected_len = 0;
1092
0
        return 0;
1093
0
    }
1094
0
    s->ext.alpn_selected_len = len;
1095
1096
0
    return 1;
1097
0
}
1098
1099
X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
1100
0
{
1101
0
    return s->peer;
1102
0
}
1103
1104
EVP_PKEY *SSL_SESSION_get0_peer_rpk(SSL_SESSION *s)
1105
0
{
1106
0
    return s->peer_rpk;
1107
0
}
1108
1109
int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1110
                                unsigned int sid_ctx_len)
1111
0
{
1112
0
    if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1113
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1114
0
        return 0;
1115
0
    }
1116
0
    s->sid_ctx_length = sid_ctx_len;
1117
0
    if (sid_ctx != s->sid_ctx)
1118
0
        memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1119
1120
0
    return 1;
1121
0
}
1122
1123
int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1124
0
{
1125
    /*
1126
     * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1127
     * session ID.
1128
     */
1129
0
    return !s->not_resumable
1130
0
           && (s->session_id_length > 0 || s->ext.ticklen > 0);
1131
0
}
1132
1133
long SSL_CTX_set_timeout(SSL_CTX *s, long t)
1134
0
{
1135
0
    long l;
1136
1137
0
    if (s == NULL)
1138
0
        return 0;
1139
0
    l = (long)ossl_time2seconds(s->session_timeout);
1140
0
    s->session_timeout = ossl_seconds2time(t);
1141
0
    return l;
1142
0
}
1143
1144
long SSL_CTX_get_timeout(const SSL_CTX *s)
1145
0
{
1146
0
    if (s == NULL)
1147
0
        return 0;
1148
0
    return (long)ossl_time2seconds(s->session_timeout);
1149
0
}
1150
1151
int SSL_set_session_secret_cb(SSL *s,
1152
                              tls_session_secret_cb_fn tls_session_secret_cb,
1153
                              void *arg)
1154
0
{
1155
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1156
1157
0
    if (sc == NULL)
1158
0
        return 0;
1159
1160
0
    sc->ext.session_secret_cb = tls_session_secret_cb;
1161
0
    sc->ext.session_secret_cb_arg = arg;
1162
0
    return 1;
1163
0
}
1164
1165
int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1166
                                  void *arg)
1167
0
{
1168
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1169
1170
0
    if (sc == NULL)
1171
0
        return 0;
1172
1173
0
    sc->ext.session_ticket_cb = cb;
1174
0
    sc->ext.session_ticket_cb_arg = arg;
1175
0
    return 1;
1176
0
}
1177
1178
int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1179
0
{
1180
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1181
1182
0
    if (sc == NULL)
1183
0
        return 0;
1184
1185
0
    if (sc->version >= TLS1_VERSION) {
1186
0
        OPENSSL_free(sc->ext.session_ticket);
1187
0
        sc->ext.session_ticket = NULL;
1188
0
        sc->ext.session_ticket =
1189
0
            OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1190
0
        if (sc->ext.session_ticket == NULL)
1191
0
            return 0;
1192
1193
0
        if (ext_data != NULL) {
1194
0
            sc->ext.session_ticket->length = ext_len;
1195
0
            sc->ext.session_ticket->data = sc->ext.session_ticket + 1;
1196
0
            memcpy(sc->ext.session_ticket->data, ext_data, ext_len);
1197
0
        } else {
1198
0
            sc->ext.session_ticket->length = 0;
1199
0
            sc->ext.session_ticket->data = NULL;
1200
0
        }
1201
1202
0
        return 1;
1203
0
    }
1204
1205
0
    return 0;
1206
0
}
1207
1208
#ifndef OPENSSL_NO_DEPRECATED_3_4
1209
void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1210
0
{
1211
0
    SSL_CTX_flush_sessions_ex(s, (time_t) t);
1212
0
}
1213
#endif
1214
1215
void SSL_CTX_flush_sessions_ex(SSL_CTX *s, time_t t)
1216
5.42k
{
1217
5.42k
    STACK_OF(SSL_SESSION) *sk;
1218
5.42k
    SSL_SESSION *current;
1219
5.42k
    unsigned long i;
1220
5.42k
    const OSSL_TIME timeout = ossl_time_from_time_t(t);
1221
1222
5.42k
    if (!CRYPTO_THREAD_write_lock(s->lock))
1223
0
        return;
1224
1225
5.42k
    sk = sk_SSL_SESSION_new_null();
1226
5.42k
    i = lh_SSL_SESSION_get_down_load(s->sessions);
1227
5.42k
    lh_SSL_SESSION_set_down_load(s->sessions, 0);
1228
1229
    /*
1230
     * Iterate over the list from the back (oldest), and stop
1231
     * when a session can no longer be removed.
1232
     * Add the session to a temporary list to be freed outside
1233
     * the SSL_CTX lock.
1234
     * But still do the remove_session_cb() within the lock.
1235
     */
1236
5.49k
    while (s->session_cache_tail != NULL) {
1237
69
        current = s->session_cache_tail;
1238
69
        if (t == 0 || sess_timedout(timeout, current)) {
1239
69
            lh_SSL_SESSION_delete(s->sessions, current);
1240
69
            SSL_SESSION_list_remove(s, current);
1241
69
            current->not_resumable = 1;
1242
69
            if (s->remove_session_cb != NULL)
1243
0
                s->remove_session_cb(s, current);
1244
            /*
1245
             * Throw the session on a stack, it's entirely plausible
1246
             * that while freeing outside the critical section, the
1247
             * session could be re-added, so avoid using the next/prev
1248
             * pointers. If the stack failed to create, or the session
1249
             * couldn't be put on the stack, just free it here
1250
             */
1251
69
            if (sk == NULL || !sk_SSL_SESSION_push(sk, current))
1252
0
                SSL_SESSION_free(current);
1253
69
        } else {
1254
0
            break;
1255
0
        }
1256
69
    }
1257
1258
5.42k
    lh_SSL_SESSION_set_down_load(s->sessions, i);
1259
5.42k
    CRYPTO_THREAD_unlock(s->lock);
1260
1261
5.42k
    sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free);
1262
5.42k
}
1263
1264
int ssl_clear_bad_session(SSL_CONNECTION *s)
1265
15.3k
{
1266
15.3k
    if ((s->session != NULL) &&
1267
15.3k
        !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1268
15.3k
        !(SSL_in_init(SSL_CONNECTION_GET_SSL(s))
1269
4.45k
          || SSL_in_before(SSL_CONNECTION_GET_SSL(s)))) {
1270
168
        SSL_CTX_remove_session(s->session_ctx, s->session);
1271
168
        return 1;
1272
168
    } else
1273
15.1k
        return 0;
1274
15.3k
}
1275
1276
/* locked by SSL_CTX in the calling function */
1277
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1278
472
{
1279
472
    if ((s->next == NULL) || (s->prev == NULL))
1280
0
        return;
1281
1282
472
    if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1283
        /* last element in list */
1284
472
        if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1285
            /* only one element in list */
1286
472
            ctx->session_cache_head = NULL;
1287
472
            ctx->session_cache_tail = NULL;
1288
472
        } else {
1289
0
            ctx->session_cache_tail = s->prev;
1290
0
            s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1291
0
        }
1292
472
    } else {
1293
0
        if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1294
            /* first element in list */
1295
0
            ctx->session_cache_head = s->next;
1296
0
            s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1297
0
        } else {
1298
            /* middle of list */
1299
0
            s->next->prev = s->prev;
1300
0
            s->prev->next = s->next;
1301
0
        }
1302
0
    }
1303
472
    s->prev = s->next = NULL;
1304
472
    s->owner = NULL;
1305
472
}
1306
1307
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1308
472
{
1309
472
    SSL_SESSION *next;
1310
1311
472
    if ((s->next != NULL) && (s->prev != NULL))
1312
0
        SSL_SESSION_list_remove(ctx, s);
1313
1314
472
    if (ctx->session_cache_head == NULL) {
1315
472
        ctx->session_cache_head = s;
1316
472
        ctx->session_cache_tail = s;
1317
472
        s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1318
472
        s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1319
472
    } else {
1320
0
        if (timeoutcmp(s, ctx->session_cache_head) >= 0) {
1321
            /*
1322
             * if we timeout after (or the same time as) the first
1323
             * session, put us first - usual case
1324
             */
1325
0
            s->next = ctx->session_cache_head;
1326
0
            s->next->prev = s;
1327
0
            s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1328
0
            ctx->session_cache_head = s;
1329
0
        } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) {
1330
            /* if we timeout before the last session, put us last */
1331
0
            s->prev = ctx->session_cache_tail;
1332
0
            s->prev->next = s;
1333
0
            s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1334
0
            ctx->session_cache_tail = s;
1335
0
        } else {
1336
            /*
1337
             * we timeout somewhere in-between - if there is only
1338
             * one session in the cache it will be caught above
1339
             */
1340
0
            next = ctx->session_cache_head->next;
1341
0
            while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) {
1342
0
                if (timeoutcmp(s, next) >= 0) {
1343
0
                    s->next = next;
1344
0
                    s->prev = next->prev;
1345
0
                    next->prev->next = s;
1346
0
                    next->prev = s;
1347
0
                    break;
1348
0
                }
1349
0
                next = next->next;
1350
0
            }
1351
0
        }
1352
0
    }
1353
472
    s->owner = ctx;
1354
472
}
1355
1356
void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1357
                             int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
1358
0
{
1359
0
    ctx->new_session_cb = cb;
1360
0
}
1361
1362
0
int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1363
0
    return ctx->new_session_cb;
1364
0
}
1365
1366
void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1367
                                void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1368
0
{
1369
0
    ctx->remove_session_cb = cb;
1370
0
}
1371
1372
void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1373
0
                                                  SSL_SESSION *sess) {
1374
0
    return ctx->remove_session_cb;
1375
0
}
1376
1377
void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1378
                             SSL_SESSION *(*cb) (SSL *ssl,
1379
                                                 const unsigned char *data,
1380
                                                 int len, int *copy))
1381
0
{
1382
0
    ctx->get_session_cb = cb;
1383
0
}
1384
1385
SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1386
                                                       const unsigned char
1387
                                                       *data, int len,
1388
0
                                                       int *copy) {
1389
0
    return ctx->get_session_cb;
1390
0
}
1391
1392
void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1393
                               void (*cb) (const SSL *ssl, int type, int val))
1394
0
{
1395
0
    ctx->info_callback = cb;
1396
0
}
1397
1398
void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1399
0
                                                 int val) {
1400
0
    return ctx->info_callback;
1401
0
}
1402
1403
void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1404
                                int (*cb) (SSL *ssl, X509 **x509,
1405
                                           EVP_PKEY **pkey))
1406
0
{
1407
0
    ctx->client_cert_cb = cb;
1408
0
}
1409
1410
int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1411
0
                                                 EVP_PKEY **pkey) {
1412
0
    return ctx->client_cert_cb;
1413
0
}
1414
1415
void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1416
                                    int (*cb) (SSL *ssl,
1417
                                               unsigned char *cookie,
1418
                                               unsigned int *cookie_len))
1419
0
{
1420
0
    ctx->app_gen_cookie_cb = cb;
1421
0
}
1422
1423
void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1424
                                  int (*cb) (SSL *ssl,
1425
                                             const unsigned char *cookie,
1426
                                             unsigned int cookie_len))
1427
0
{
1428
0
    ctx->app_verify_cookie_cb = cb;
1429
0
}
1430
1431
int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1432
0
{
1433
0
    OPENSSL_free(ss->ticket_appdata);
1434
0
    ss->ticket_appdata_len = 0;
1435
0
    if (data == NULL || len == 0) {
1436
0
        ss->ticket_appdata = NULL;
1437
0
        return 1;
1438
0
    }
1439
0
    ss->ticket_appdata = OPENSSL_memdup(data, len);
1440
0
    if (ss->ticket_appdata != NULL) {
1441
0
        ss->ticket_appdata_len = len;
1442
0
        return 1;
1443
0
    }
1444
0
    return 0;
1445
0
}
1446
1447
int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1448
0
{
1449
0
    *data = ss->ticket_appdata;
1450
0
    *len = ss->ticket_appdata_len;
1451
0
    return 1;
1452
0
}
1453
1454
void SSL_CTX_set_stateless_cookie_generate_cb(
1455
    SSL_CTX *ctx,
1456
    int (*cb) (SSL *ssl,
1457
               unsigned char *cookie,
1458
               size_t *cookie_len))
1459
0
{
1460
0
    ctx->gen_stateless_cookie_cb = cb;
1461
0
}
1462
1463
void SSL_CTX_set_stateless_cookie_verify_cb(
1464
    SSL_CTX *ctx,
1465
    int (*cb) (SSL *ssl,
1466
               const unsigned char *cookie,
1467
               size_t cookie_len))
1468
0
{
1469
0
    ctx->verify_stateless_cookie_cb = cb;
1470
0
}
1471
1472
IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)