Coverage Report

Created: 2018-08-29 13:53

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