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