Coverage Report

Created: 2023-04-12 06:22

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