Coverage Report

Created: 2026-11-16 07:12

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