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