Coverage Report

Created: 2025-12-14 06:48

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