Coverage Report

Created: 2026-09-12 06:55

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