Coverage Report

Created: 2026-08-17 07:16

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