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