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