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