Coverage Report

Created: 2025-12-31 06:58

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