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