Resolved unresolved symbols with no-hw
[openssl.git] / ssl / ssl_sess.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in
69  *    the documentation and/or other materials provided with the
70  *    distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  *    software must display the following acknowledgment:
74  *    "This product includes software developed by the OpenSSL Project
75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  *    endorse or promote products derived from this software without
79  *    prior written permission. For written permission, please contact
80  *    openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  *    nor may "OpenSSL" appear in their names without prior written
84  *    permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com).  This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com).
108  *
109  */
110 /* ====================================================================
111  * Copyright 2005 Nokia. All rights reserved.
112  *
113  * The portions of the attached software ("Contribution") is developed by
114  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
115  * license.
116  *
117  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
118  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
119  * support (see RFC 4279) to OpenSSL.
120  *
121  * No patent licenses or other rights except those expressly stated in
122  * the OpenSSL open source license shall be deemed granted or received
123  * expressly, by implication, estoppel, or otherwise.
124  *
125  * No assurances are provided by Nokia that the Contribution does not
126  * infringe the patent or other intellectual property rights of any third
127  * party or that the license provides you with all the necessary rights
128  * to make use of the Contribution.
129  *
130  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
131  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
132  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
133  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
134  * OTHERWISE.
135  */
136
137 #include <stdio.h>
138 #include <openssl/lhash.h>
139 #include <openssl/rand.h>
140 #ifndef OPENSSL_NO_ENGINE
141 # include <openssl/engine.h>
142 #endif
143 #include "ssl_locl.h"
144
145 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
146 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
147 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
148
149 SSL_SESSION *SSL_get_session(const SSL *ssl)
150 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
151 {
152     return (ssl->session);
153 }
154
155 SSL_SESSION *SSL_get1_session(SSL *ssl)
156 /* variant of SSL_get_session: caller really gets something */
157 {
158     SSL_SESSION *sess;
159     /*
160      * Need to lock this all up rather than just use CRYPTO_add so that
161      * somebody doesn't free ssl->session between when we check it's non-null
162      * and when we up the reference count.
163      */
164     CRYPTO_THREAD_read_lock(ssl->lock);
165     sess = ssl->session;
166     if (sess)
167         SSL_SESSION_up_ref(sess);
168     CRYPTO_THREAD_unlock(ssl->lock);
169     return sess;
170 }
171
172 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
173 {
174     return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
175 }
176
177 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
178 {
179     return (CRYPTO_get_ex_data(&s->ex_data, idx));
180 }
181
182 SSL_SESSION *SSL_SESSION_new(void)
183 {
184     SSL_SESSION *ss;
185
186     ss = OPENSSL_zalloc(sizeof(*ss));
187     if (ss == NULL) {
188         SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
189         return NULL;
190     }
191
192     ss->verify_result = 1;      /* avoid 0 (= X509_V_OK) just in case */
193     ss->references = 1;
194     ss->timeout = 60 * 5 + 4;   /* 5 minute timeout by default */
195     ss->time = (unsigned long)time(NULL);
196     ss->lock = CRYPTO_THREAD_lock_new();
197     if (ss->lock == NULL) {
198         SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
199         OPENSSL_free(ss);
200         return NULL;
201     }
202
203     CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
204
205     return ss;
206 }
207
208 /*
209  * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
210  * ticket == 0 then no ticket information is duplicated, otherwise it is.
211  */
212 SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket)
213 {
214     SSL_SESSION *dest;
215
216     dest = OPENSSL_malloc(sizeof(*src));
217     if (dest == NULL) {
218         goto err;
219     }
220     memcpy(dest, src, sizeof(*dest));
221
222     /*
223      * Set the various pointers to NULL so that we can call SSL_SESSION_free in
224      * the case of an error whilst halfway through constructing dest
225      */
226 #ifndef OPENSSL_NO_PSK
227     dest->psk_identity_hint = NULL;
228     dest->psk_identity = NULL;
229 #endif
230     dest->ciphers = NULL;
231     dest->tlsext_hostname = NULL;
232 #ifndef OPENSSL_NO_EC
233     dest->tlsext_ecpointformatlist = NULL;
234     dest->tlsext_ellipticcurvelist = NULL;
235 #endif
236     dest->tlsext_tick = NULL;
237 #ifndef OPENSSL_NO_SRP
238     dest->srp_username = NULL;
239 #endif
240     memset(&dest->ex_data, 0, sizeof(dest->ex_data));
241
242     /* We deliberately don't copy the prev and next pointers */
243     dest->prev = NULL;
244     dest->next = NULL;
245
246     dest->references = 1;
247
248     dest->lock = CRYPTO_THREAD_lock_new();
249     if (dest->lock == NULL)
250         goto err;
251
252     if (src->peer != NULL)
253         X509_up_ref(src->peer);
254
255     if (src->peer_chain != NULL) {
256         dest->peer_chain = X509_chain_up_ref(src->peer_chain);
257         if (dest->peer_chain == NULL)
258             goto err;
259     }
260
261 #ifndef OPENSSL_NO_PSK
262     if (src->psk_identity_hint) {
263         dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
264         if (dest->psk_identity_hint == NULL) {
265             goto err;
266         }
267     }
268     if (src->psk_identity) {
269         dest->psk_identity = OPENSSL_strdup(src->psk_identity);
270         if (dest->psk_identity == NULL) {
271             goto err;
272         }
273     }
274 #endif
275
276     if(src->ciphers != NULL) {
277         dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers);
278         if (dest->ciphers == NULL)
279             goto err;
280     }
281
282     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
283                                             &dest->ex_data, &src->ex_data)) {
284         goto err;
285     }
286
287     if (src->tlsext_hostname) {
288         dest->tlsext_hostname = OPENSSL_strdup(src->tlsext_hostname);
289         if (dest->tlsext_hostname == NULL) {
290             goto err;
291         }
292     }
293 #ifndef OPENSSL_NO_EC
294     if (src->tlsext_ecpointformatlist) {
295         dest->tlsext_ecpointformatlist =
296             OPENSSL_memdup(src->tlsext_ecpointformatlist,
297                        src->tlsext_ecpointformatlist_length);
298         if (dest->tlsext_ecpointformatlist == NULL)
299             goto err;
300     }
301     if (src->tlsext_ellipticcurvelist) {
302         dest->tlsext_ellipticcurvelist =
303             OPENSSL_memdup(src->tlsext_ellipticcurvelist,
304                        src->tlsext_ellipticcurvelist_length);
305         if (dest->tlsext_ellipticcurvelist == NULL)
306             goto err;
307     }
308 #endif
309
310     if (ticket != 0) {
311         dest->tlsext_tick = OPENSSL_memdup(src->tlsext_tick, src->tlsext_ticklen);
312         if(dest->tlsext_tick == NULL)
313             goto err;
314     } else {
315         dest->tlsext_tick_lifetime_hint = 0;
316         dest->tlsext_ticklen = 0;
317     }
318
319 #ifndef OPENSSL_NO_SRP
320     if (src->srp_username) {
321         dest->srp_username = OPENSSL_strdup(src->srp_username);
322         if (dest->srp_username == NULL) {
323             goto err;
324         }
325     }
326 #endif
327
328     return dest;
329 err:
330     SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
331     SSL_SESSION_free(dest);
332     return NULL;
333 }
334
335 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s,
336                                         unsigned int *len)
337 {
338     if (len)
339         *len = s->session_id_length;
340     return s->session_id;
341 }
342
343 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
344 {
345     return s->compress_meth;
346 }
347
348 /*
349  * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
350  * the ID with random junk repeatedly until we have no conflict is going to
351  * complete in one iteration pretty much "most" of the time (btw:
352  * understatement). So, if it takes us 10 iterations and we still can't avoid
353  * a conflict - well that's a reasonable point to call it quits. Either the
354  * RAND code is broken or someone is trying to open roughly very close to
355  * 2^256 SSL sessions to our server. How you might store that many sessions
356  * is perhaps a more interesting question ...
357  */
358
359 #define MAX_SESS_ID_ATTEMPTS 10
360 static int def_generate_session_id(const SSL *ssl, unsigned char *id,
361                                    unsigned int *id_len)
362 {
363     unsigned int retry = 0;
364     do
365         if (RAND_bytes(id, *id_len) <= 0)
366             return 0;
367     while (SSL_has_matching_session_id(ssl, id, *id_len) &&
368            (++retry < MAX_SESS_ID_ATTEMPTS)) ;
369     if (retry < MAX_SESS_ID_ATTEMPTS)
370         return 1;
371     /* else - woops a session_id match */
372     /*
373      * XXX We should also check the external cache -- but the probability of
374      * a collision is negligible, and we could not prevent the concurrent
375      * creation of sessions with identical IDs since we currently don't have
376      * means to atomically check whether a session ID already exists and make
377      * a reservation for it if it does not (this problem applies to the
378      * internal cache as well).
379      */
380     return 0;
381 }
382
383 int ssl_get_new_session(SSL *s, int session)
384 {
385     /* This gets used by clients and servers. */
386
387     unsigned int tmp;
388     SSL_SESSION *ss = NULL;
389     GEN_SESSION_CB cb = def_generate_session_id;
390
391     if ((ss = SSL_SESSION_new()) == NULL)
392         return (0);
393
394     /* If the context has a default timeout, use it */
395     if (s->session_ctx->session_timeout == 0)
396         ss->timeout = SSL_get_default_timeout(s);
397     else
398         ss->timeout = s->session_ctx->session_timeout;
399
400     SSL_SESSION_free(s->session);
401     s->session = NULL;
402
403     if (session) {
404         if (s->version == SSL3_VERSION) {
405             ss->ssl_version = SSL3_VERSION;
406             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
407         } else if (s->version == TLS1_VERSION) {
408             ss->ssl_version = TLS1_VERSION;
409             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
410         } else if (s->version == TLS1_1_VERSION) {
411             ss->ssl_version = TLS1_1_VERSION;
412             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
413         } else if (s->version == TLS1_2_VERSION) {
414             ss->ssl_version = TLS1_2_VERSION;
415             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
416         } else if (s->version == DTLS1_BAD_VER) {
417             ss->ssl_version = DTLS1_BAD_VER;
418             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
419         } else if (s->version == DTLS1_VERSION) {
420             ss->ssl_version = DTLS1_VERSION;
421             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
422         } else if (s->version == DTLS1_2_VERSION) {
423             ss->ssl_version = DTLS1_2_VERSION;
424             ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
425         } else {
426             SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
427             SSL_SESSION_free(ss);
428             return (0);
429         }
430
431         /*-
432          * If RFC5077 ticket, use empty session ID (as server).
433          * Note that:
434          * (a) ssl_get_prev_session() does lookahead into the
435          *     ClientHello extensions to find the session ticket.
436          *     When ssl_get_prev_session() fails, s3_srvr.c calls
437          *     ssl_get_new_session() in ssl3_get_client_hello().
438          *     At that point, it has not yet parsed the extensions,
439          *     however, because of the lookahead, it already knows
440          *     whether a ticket is expected or not.
441          *
442          * (b) s3_clnt.c calls ssl_get_new_session() before parsing
443          *     ServerHello extensions, and before recording the session
444          *     ID received from the server, so this block is a noop.
445          */
446         if (s->tlsext_ticket_expected) {
447             ss->session_id_length = 0;
448             goto sess_id_done;
449         }
450
451         /* Choose which callback will set the session ID */
452         CRYPTO_THREAD_read_lock(s->lock);
453         CRYPTO_THREAD_read_lock(s->session_ctx->lock);
454         if (s->generate_session_id)
455             cb = s->generate_session_id;
456         else if (s->session_ctx->generate_session_id)
457             cb = s->session_ctx->generate_session_id;
458         CRYPTO_THREAD_unlock(s->session_ctx->lock);
459         CRYPTO_THREAD_unlock(s->lock);
460         /* Choose a session ID */
461         tmp = ss->session_id_length;
462         if (!cb(s, ss->session_id, &tmp)) {
463             /* The callback failed */
464             SSLerr(SSL_F_SSL_GET_NEW_SESSION,
465                    SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
466             SSL_SESSION_free(ss);
467             return (0);
468         }
469         /*
470          * Don't allow the callback to set the session length to zero. nor
471          * set it higher than it was.
472          */
473         if (tmp == 0 || tmp > ss->session_id_length) {
474             /* The callback set an illegal length */
475             SSLerr(SSL_F_SSL_GET_NEW_SESSION,
476                    SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
477             SSL_SESSION_free(ss);
478             return (0);
479         }
480         ss->session_id_length = tmp;
481         /* Finally, check for a conflict */
482         if (SSL_has_matching_session_id(s, ss->session_id,
483                                         ss->session_id_length)) {
484             SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT);
485             SSL_SESSION_free(ss);
486             return (0);
487         }
488
489  sess_id_done:
490         if (s->tlsext_hostname) {
491             ss->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
492             if (ss->tlsext_hostname == NULL) {
493                 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
494                 SSL_SESSION_free(ss);
495                 return 0;
496             }
497         }
498     } else {
499         ss->session_id_length = 0;
500     }
501
502     if (s->sid_ctx_length > sizeof ss->sid_ctx) {
503         SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
504         SSL_SESSION_free(ss);
505         return 0;
506     }
507     memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
508     ss->sid_ctx_length = s->sid_ctx_length;
509     s->session = ss;
510     ss->ssl_version = s->version;
511     ss->verify_result = X509_V_OK;
512
513     /* If client supports extended master secret set it in session */
514     if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
515         ss->flags |= SSL_SESS_FLAG_EXTMS;
516
517     return (1);
518 }
519
520 /*-
521  * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
522  * connection. It is only called by servers.
523  *
524  *   ext: ClientHello extensions (including length prefix)
525  *   session_id: ClientHello session ID.
526  *
527  * Returns:
528  *   -1: error
529  *    0: a session may have been found.
530  *
531  * Side effects:
532  *   - If a session is found then s->session is pointed at it (after freeing an
533  *     existing session if need be) and s->verify_result is set from the session.
534  *   - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
535  *     if the server should issue a new session ticket (to 0 otherwise).
536  */
537 int ssl_get_prev_session(SSL *s, const PACKET *ext, const PACKET *session_id)
538 {
539     /* This is used only by servers. */
540
541     SSL_SESSION *ret = NULL;
542     int fatal = 0;
543     int try_session_cache = 1;
544     int r;
545
546     if (PACKET_remaining(session_id) == 0)
547         try_session_cache = 0;
548
549     /* sets s->tlsext_ticket_expected and extended master secret flag */
550     r = tls_check_serverhello_tlsext_early(s, ext, session_id, &ret);
551     switch (r) {
552     case -1:                   /* Error during processing */
553         fatal = 1;
554         goto err;
555     case 0:                    /* No ticket found */
556     case 1:                    /* Zero length ticket found */
557         break;                  /* Ok to carry on processing session id. */
558     case 2:                    /* Ticket found but not decrypted. */
559     case 3:                    /* Ticket decrypted, *ret has been set. */
560         try_session_cache = 0;
561         break;
562     default:
563         abort();
564     }
565
566     if (try_session_cache &&
567         ret == NULL &&
568         !(s->session_ctx->session_cache_mode &
569           SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
570         SSL_SESSION data;
571         size_t local_len;
572         data.ssl_version = s->version;
573         if (!PACKET_copy_all(session_id, data.session_id,
574                              sizeof(data.session_id),
575                              &local_len)) {
576             goto err;
577         }
578         data.session_id_length = local_len;
579         CRYPTO_THREAD_read_lock(s->session_ctx->lock);
580         ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
581         if (ret != NULL) {
582             /* don't allow other threads to steal it: */
583             SSL_SESSION_up_ref(ret);
584         }
585         CRYPTO_THREAD_unlock(s->session_ctx->lock);
586         if (ret == NULL)
587             s->session_ctx->stats.sess_miss++;
588     }
589
590     if (try_session_cache &&
591         ret == NULL && s->session_ctx->get_session_cb != NULL) {
592         int copy = 1;
593         ret = s->session_ctx->get_session_cb(s, PACKET_data(session_id),
594                                              PACKET_remaining(session_id),
595                                              &copy);
596
597         if (ret != NULL) {
598             s->session_ctx->stats.sess_cb_hit++;
599
600             /*
601              * Increment reference count now if the session callback asks us
602              * to do so (note that if the session structures returned by the
603              * callback are shared between threads, it must handle the
604              * reference count itself [i.e. copy == 0], or things won't be
605              * thread-safe).
606              */
607             if (copy)
608                 SSL_SESSION_up_ref(ret);
609
610             /*
611              * Add the externally cached session to the internal cache as
612              * well if and only if we are supposed to.
613              */
614             if (!
615                 (s->session_ctx->session_cache_mode &
616                  SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
617                 /*
618                  * The following should not return 1, otherwise, things are
619                  * very strange
620                  */
621                 if (SSL_CTX_add_session(s->session_ctx, ret))
622                     goto err;
623             }
624         }
625     }
626
627     if (ret == NULL)
628         goto err;
629
630     /* Now ret is non-NULL and we own one of its reference counts. */
631
632     if (ret->sid_ctx_length != s->sid_ctx_length
633         || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
634         /*
635          * We have the session requested by the client, but we don't want to
636          * use it in this context.
637          */
638         goto err;               /* treat like cache miss */
639     }
640
641     if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
642         /*
643          * We can't be sure if this session is being used out of context,
644          * which is especially important for SSL_VERIFY_PEER. The application
645          * should have used SSL[_CTX]_set_session_id_context. For this error
646          * case, we generate an error instead of treating the event like a
647          * cache miss (otherwise it would be easy for applications to
648          * effectively disable the session cache by accident without anyone
649          * noticing).
650          */
651
652         SSLerr(SSL_F_SSL_GET_PREV_SESSION,
653                SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
654         fatal = 1;
655         goto err;
656     }
657
658     if (ret->cipher == NULL) {
659         unsigned char buf[5], *p;
660         unsigned long l;
661
662         p = buf;
663         l = ret->cipher_id;
664         l2n(l, p);
665         if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR)
666             ret->cipher = ssl_get_cipher_by_char(s, &(buf[2]));
667         else
668             ret->cipher = ssl_get_cipher_by_char(s, &(buf[1]));
669         if (ret->cipher == NULL)
670             goto err;
671     }
672
673     if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
674         s->session_ctx->stats.sess_timeout++;
675         if (try_session_cache) {
676             /* session was from the cache, so remove it */
677             SSL_CTX_remove_session(s->session_ctx, ret);
678         }
679         goto err;
680     }
681
682     /* Check extended master secret extension consistency */
683     if (ret->flags & SSL_SESS_FLAG_EXTMS) {
684         /* If old session includes extms, but new does not: abort handshake */
685         if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
686             SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS);
687             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
688             fatal = 1;
689             goto err;
690         }
691     } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
692         /* If new session includes extms, but old does not: do not resume */
693         goto err;
694     }
695
696     s->session_ctx->stats.sess_hit++;
697
698     SSL_SESSION_free(s->session);
699     s->session = ret;
700     s->verify_result = s->session->verify_result;
701     return 1;
702
703  err:
704     if (ret != NULL) {
705         SSL_SESSION_free(ret);
706
707         if (!try_session_cache) {
708             /*
709              * The session was from a ticket, so we should issue a ticket for
710              * the new session
711              */
712             s->tlsext_ticket_expected = 1;
713         }
714     }
715     if (fatal)
716         return -1;
717     else
718         return 0;
719 }
720
721 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
722 {
723     int ret = 0;
724     SSL_SESSION *s;
725
726     /*
727      * add just 1 reference count for the SSL_CTX's session cache even though
728      * it has two ways of access: each session is in a doubly linked list and
729      * an lhash
730      */
731     SSL_SESSION_up_ref(c);
732     /*
733      * if session c is in already in cache, we take back the increment later
734      */
735
736     CRYPTO_THREAD_write_lock(ctx->lock);
737     s = lh_SSL_SESSION_insert(ctx->sessions, c);
738
739     /*
740      * s != NULL iff we already had a session with the given PID. In this
741      * case, s == c should hold (then we did not really modify
742      * ctx->sessions), or we're in trouble.
743      */
744     if (s != NULL && s != c) {
745         /* We *are* in trouble ... */
746         SSL_SESSION_list_remove(ctx, s);
747         SSL_SESSION_free(s);
748         /*
749          * ... so pretend the other session did not exist in cache (we cannot
750          * handle two SSL_SESSION structures with identical session ID in the
751          * same cache, which could happen e.g. when two threads concurrently
752          * obtain the same session from an external cache)
753          */
754         s = NULL;
755     }
756
757     /* Put at the head of the queue unless it is already in the cache */
758     if (s == NULL)
759         SSL_SESSION_list_add(ctx, c);
760
761     if (s != NULL) {
762         /*
763          * existing cache entry -- decrement previously incremented reference
764          * count because it already takes into account the cache
765          */
766
767         SSL_SESSION_free(s);    /* s == c */
768         ret = 0;
769     } else {
770         /*
771          * new cache entry -- remove old ones if cache has become too large
772          */
773
774         ret = 1;
775
776         if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
777             while (SSL_CTX_sess_number(ctx) >
778                    SSL_CTX_sess_get_cache_size(ctx)) {
779                 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
780                     break;
781                 else
782                     ctx->stats.sess_cache_full++;
783             }
784         }
785     }
786     CRYPTO_THREAD_unlock(ctx->lock);
787     return ret;
788 }
789
790 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
791 {
792     return remove_session_lock(ctx, c, 1);
793 }
794
795 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
796 {
797     SSL_SESSION *r;
798     int ret = 0;
799
800     if ((c != NULL) && (c->session_id_length != 0)) {
801         if (lck)
802             CRYPTO_THREAD_write_lock(ctx->lock);
803         if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
804             ret = 1;
805             r = lh_SSL_SESSION_delete(ctx->sessions, c);
806             SSL_SESSION_list_remove(ctx, c);
807         }
808
809         if (lck)
810             CRYPTO_THREAD_unlock(ctx->lock);
811
812         if (ret) {
813             r->not_resumable = 1;
814             if (ctx->remove_session_cb != NULL)
815                 ctx->remove_session_cb(ctx, r);
816             SSL_SESSION_free(r);
817         }
818     } else
819         ret = 0;
820     return (ret);
821 }
822
823 void SSL_SESSION_free(SSL_SESSION *ss)
824 {
825     int i;
826
827     if (ss == NULL)
828         return;
829
830     CRYPTO_atomic_add(&ss->references, -1, &i, ss->lock);
831     REF_PRINT_COUNT("SSL_SESSION", ss);
832     if (i > 0)
833         return;
834     REF_ASSERT_ISNT(i < 0);
835
836     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
837
838     OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
839     OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
840     X509_free(ss->peer);
841     sk_X509_pop_free(ss->peer_chain, X509_free);
842     sk_SSL_CIPHER_free(ss->ciphers);
843     OPENSSL_free(ss->tlsext_hostname);
844     OPENSSL_free(ss->tlsext_tick);
845 #ifndef OPENSSL_NO_EC
846     ss->tlsext_ecpointformatlist_length = 0;
847     OPENSSL_free(ss->tlsext_ecpointformatlist);
848     ss->tlsext_ellipticcurvelist_length = 0;
849     OPENSSL_free(ss->tlsext_ellipticcurvelist);
850 #endif                         /* OPENSSL_NO_EC */
851 #ifndef OPENSSL_NO_PSK
852     OPENSSL_free(ss->psk_identity_hint);
853     OPENSSL_free(ss->psk_identity);
854 #endif
855 #ifndef OPENSSL_NO_SRP
856     OPENSSL_free(ss->srp_username);
857 #endif
858     CRYPTO_THREAD_lock_free(ss->lock);
859     OPENSSL_clear_free(ss, sizeof(*ss));
860 }
861
862 int SSL_SESSION_up_ref(SSL_SESSION *ss)
863 {
864     int i;
865
866     if (CRYPTO_atomic_add(&ss->references, 1, &i, ss->lock) <= 0)
867         return 0;
868
869     REF_PRINT_COUNT("SSL_SESSION", ss);
870     REF_ASSERT_ISNT(i < 2);
871     return ((i > 1) ? 1 : 0);
872 }
873
874 int SSL_set_session(SSL *s, SSL_SESSION *session)
875 {
876     int ret = 0;
877     const SSL_METHOD *meth;
878
879     if (session != NULL) {
880         meth = s->ctx->method->get_ssl_method(session->ssl_version);
881         if (meth == NULL)
882             meth = s->method->get_ssl_method(session->ssl_version);
883         if (meth == NULL) {
884             SSLerr(SSL_F_SSL_SET_SESSION, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
885             return (0);
886         }
887
888         if (meth != s->method) {
889             if (!SSL_set_ssl_method(s, meth))
890                 return (0);
891         }
892
893         SSL_SESSION_up_ref(session);
894         SSL_SESSION_free(s->session);
895         s->session = session;
896         s->verify_result = s->session->verify_result;
897         ret = 1;
898     } else {
899         SSL_SESSION_free(s->session);
900         s->session = NULL;
901         meth = s->ctx->method;
902         if (meth != s->method) {
903             if (!SSL_set_ssl_method(s, meth))
904                 return (0);
905         }
906         ret = 1;
907     }
908     return (ret);
909 }
910
911 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
912 {
913     if (s == NULL)
914         return (0);
915     s->timeout = t;
916     return (1);
917 }
918
919 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
920 {
921     if (s == NULL)
922         return (0);
923     return (s->timeout);
924 }
925
926 long SSL_SESSION_get_time(const SSL_SESSION *s)
927 {
928     if (s == NULL)
929         return (0);
930     return (s->time);
931 }
932
933 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
934 {
935     if (s == NULL)
936         return (0);
937     s->time = t;
938     return (t);
939 }
940
941 int SSL_SESSION_has_ticket(const SSL_SESSION *s)
942 {
943     return (s->tlsext_ticklen > 0) ? 1 : 0;
944 }
945
946 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
947 {
948     return s->tlsext_tick_lifetime_hint;
949 }
950
951 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, unsigned char **tick,
952                             size_t *len)
953 {
954     *len = s->tlsext_ticklen;
955     if (tick != NULL)
956         *tick = s->tlsext_tick;
957 }
958
959 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
960 {
961     return s->peer;
962 }
963
964 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
965                                 unsigned int sid_ctx_len)
966 {
967     if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
968         SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
969                SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
970         return 0;
971     }
972     s->sid_ctx_length = sid_ctx_len;
973     memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
974
975     return 1;
976 }
977
978 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
979 {
980     long l;
981     if (s == NULL)
982         return (0);
983     l = s->session_timeout;
984     s->session_timeout = t;
985     return (l);
986 }
987
988 long SSL_CTX_get_timeout(const SSL_CTX *s)
989 {
990     if (s == NULL)
991         return (0);
992     return (s->session_timeout);
993 }
994
995 int SSL_set_session_secret_cb(SSL *s,
996                               int (*tls_session_secret_cb) (SSL *s,
997                                                             void *secret,
998                                                             int *secret_len,
999                                                             STACK_OF(SSL_CIPHER)
1000                                                             *peer_ciphers,
1001                                                             const SSL_CIPHER
1002                                                             **cipher,
1003                                                             void *arg),
1004                               void *arg)
1005 {
1006     if (s == NULL)
1007         return (0);
1008     s->tls_session_secret_cb = tls_session_secret_cb;
1009     s->tls_session_secret_cb_arg = arg;
1010     return (1);
1011 }
1012
1013 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1014                                   void *arg)
1015 {
1016     if (s == NULL)
1017         return (0);
1018     s->tls_session_ticket_ext_cb = cb;
1019     s->tls_session_ticket_ext_cb_arg = arg;
1020     return (1);
1021 }
1022
1023 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1024 {
1025     if (s->version >= TLS1_VERSION) {
1026         OPENSSL_free(s->tlsext_session_ticket);
1027         s->tlsext_session_ticket = NULL;
1028         s->tlsext_session_ticket =
1029             OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1030         if (s->tlsext_session_ticket == NULL) {
1031             SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1032             return 0;
1033         }
1034
1035         if (ext_data) {
1036             s->tlsext_session_ticket->length = ext_len;
1037             s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
1038             memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
1039         } else {
1040             s->tlsext_session_ticket->length = 0;
1041             s->tlsext_session_ticket->data = NULL;
1042         }
1043
1044         return 1;
1045     }
1046
1047     return 0;
1048 }
1049
1050 typedef struct timeout_param_st {
1051     SSL_CTX *ctx;
1052     long time;
1053     LHASH_OF(SSL_SESSION) *cache;
1054 } TIMEOUT_PARAM;
1055
1056 static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p)
1057 {
1058     if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */
1059         /*
1060          * The reason we don't call SSL_CTX_remove_session() is to save on
1061          * locking overhead
1062          */
1063         (void)lh_SSL_SESSION_delete(p->cache, s);
1064         SSL_SESSION_list_remove(p->ctx, s);
1065         s->not_resumable = 1;
1066         if (p->ctx->remove_session_cb != NULL)
1067             p->ctx->remove_session_cb(p->ctx, s);
1068         SSL_SESSION_free(s);
1069     }
1070 }
1071
1072 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM);
1073
1074 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1075 {
1076     unsigned long i;
1077     TIMEOUT_PARAM tp;
1078
1079     tp.ctx = s;
1080     tp.cache = s->sessions;
1081     if (tp.cache == NULL)
1082         return;
1083     tp.time = t;
1084     CRYPTO_THREAD_write_lock(s->lock);
1085     i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;
1086     CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;
1087     lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
1088     CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i;
1089     CRYPTO_THREAD_unlock(s->lock);
1090 }
1091
1092 int ssl_clear_bad_session(SSL *s)
1093 {
1094     if ((s->session != NULL) &&
1095         !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1096         !(SSL_in_init(s) || SSL_in_before(s))) {
1097         SSL_CTX_remove_session(s->ctx, s->session);
1098         return (1);
1099     } else
1100         return (0);
1101 }
1102
1103 /* locked by SSL_CTX in the calling function */
1104 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1105 {
1106     if ((s->next == NULL) || (s->prev == NULL))
1107         return;
1108
1109     if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1110         /* last element in list */
1111         if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1112             /* only one element in list */
1113             ctx->session_cache_head = NULL;
1114             ctx->session_cache_tail = NULL;
1115         } else {
1116             ctx->session_cache_tail = s->prev;
1117             s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1118         }
1119     } else {
1120         if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1121             /* first element in list */
1122             ctx->session_cache_head = s->next;
1123             s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1124         } else {
1125             /* middle of list */
1126             s->next->prev = s->prev;
1127             s->prev->next = s->next;
1128         }
1129     }
1130     s->prev = s->next = NULL;
1131 }
1132
1133 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1134 {
1135     if ((s->next != NULL) && (s->prev != NULL))
1136         SSL_SESSION_list_remove(ctx, s);
1137
1138     if (ctx->session_cache_head == NULL) {
1139         ctx->session_cache_head = s;
1140         ctx->session_cache_tail = s;
1141         s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1142         s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1143     } else {
1144         s->next = ctx->session_cache_head;
1145         s->next->prev = s;
1146         s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1147         ctx->session_cache_head = s;
1148     }
1149 }
1150
1151 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1152                              int (*cb) (struct ssl_st *ssl,
1153                                         SSL_SESSION *sess))
1154 {
1155     ctx->new_session_cb = cb;
1156 }
1157
1158 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1159     return ctx->new_session_cb;
1160 }
1161
1162 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1163                                 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1164 {
1165     ctx->remove_session_cb = cb;
1166 }
1167
1168 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1169                                                   SSL_SESSION *sess) {
1170     return ctx->remove_session_cb;
1171 }
1172
1173 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1174                              SSL_SESSION *(*cb) (struct ssl_st *ssl,
1175                                                  const unsigned char *data,
1176                                                  int len, int *copy))
1177 {
1178     ctx->get_session_cb = cb;
1179 }
1180
1181 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1182                                                        const unsigned char *data,
1183                                                        int len, int *copy) {
1184     return ctx->get_session_cb;
1185 }
1186
1187 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1188                                void (*cb) (const SSL *ssl, int type, int val))
1189 {
1190     ctx->info_callback = cb;
1191 }
1192
1193 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1194                                                  int val) {
1195     return ctx->info_callback;
1196 }
1197
1198 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1199                                 int (*cb) (SSL *ssl, X509 **x509,
1200                                            EVP_PKEY **pkey))
1201 {
1202     ctx->client_cert_cb = cb;
1203 }
1204
1205 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1206                                                  EVP_PKEY **pkey) {
1207     return ctx->client_cert_cb;
1208 }
1209
1210 #ifndef OPENSSL_NO_ENGINE
1211 int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
1212 {
1213     if (!ENGINE_init(e)) {
1214         SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
1215         return 0;
1216     }
1217     if (!ENGINE_get_ssl_client_cert_function(e)) {
1218         SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE,
1219                SSL_R_NO_CLIENT_CERT_METHOD);
1220         ENGINE_finish(e);
1221         return 0;
1222     }
1223     ctx->client_cert_engine = e;
1224     return 1;
1225 }
1226 #endif
1227
1228 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1229                                     int (*cb) (SSL *ssl,
1230                                                unsigned char *cookie,
1231                                                unsigned int *cookie_len))
1232 {
1233     ctx->app_gen_cookie_cb = cb;
1234 }
1235
1236 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1237                                   int (*cb) (SSL *ssl, const unsigned char *cookie,
1238                                              unsigned int cookie_len))
1239 {
1240     ctx->app_verify_cookie_cb = cb;
1241 }
1242
1243 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION,
1244                  SSL_SESSION)