1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
110 /* ====================================================================
111 * Copyright 2005 Nokia. All rights reserved.
113 * The portions of the attached software ("Contribution") is developed by
114 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
117 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
118 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
119 * support (see RFC 4279) to OpenSSL.
121 * No patent licenses or other rights except those expressly stated in
122 * the OpenSSL open source license shall be deemed granted or received
123 * expressly, by implication, estoppel, or otherwise.
125 * No assurances are provided by Nokia that the Contribution does not
126 * infringe the patent or other intellectual property rights of any third
127 * party or that the license provides you with all the necessary rights
128 * to make use of the Contribution.
130 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
131 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
132 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
133 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
138 #include <openssl/engine.h>
139 #include <openssl/err.h>
140 #include <openssl/lhash.h>
141 #include <openssl/mem.h>
142 #include <openssl/rand.h>
144 #include "ssl_locl.h"
146 /* The address of this is a magic value, a pointer to which is returned by
147 * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
148 * that it needs to asynchronously fetch session information. */
149 static char g_pending_session_magic;
151 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
152 static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
153 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
155 SSL_SESSION *SSL_magic_pending_session_ptr(void)
157 return (SSL_SESSION*) &g_pending_session_magic;
160 SSL_SESSION *SSL_get_session(const SSL *ssl)
161 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
163 return(ssl->session);
166 SSL_SESSION *SSL_get1_session(SSL *ssl)
167 /* variant of SSL_get_session: caller really gets something */
170 /* Need to lock this all up rather than just use CRYPTO_add so that
171 * somebody doesn't free ssl->session between when we check it's
172 * non-null and when we up the reference count. */
173 CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
177 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
181 int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
182 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
184 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
185 new_func, dup_func, free_func);
188 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
190 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
193 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
195 return(CRYPTO_get_ex_data(&s->ex_data,idx));
198 SSL_SESSION *SSL_SESSION_new(void)
202 ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
205 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
208 memset(ss,0,sizeof(SSL_SESSION));
210 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
212 ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
213 ss->time=(unsigned long)time(NULL);
214 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
218 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
221 *len = s->session_id_length;
222 return s->session_id;
225 /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
226 * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
227 * until we have no conflict is going to complete in one iteration pretty much
228 * "most" of the time (btw: understatement). So, if it takes us 10 iterations
229 * and we still can't avoid a conflict - well that's a reasonable point to call
230 * it quits. Either the RAND code is broken or someone is trying to open roughly
231 * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
232 * store that many sessions is perhaps a more interesting question ... */
234 #define MAX_SESS_ID_ATTEMPTS 10
235 static int def_generate_session_id(const SSL *ssl, unsigned char *id,
236 unsigned int *id_len)
238 unsigned int retry = 0;
240 if (RAND_pseudo_bytes(id, *id_len) <= 0)
242 while(SSL_has_matching_session_id(ssl, id, *id_len) &&
243 (++retry < MAX_SESS_ID_ATTEMPTS));
244 if(retry < MAX_SESS_ID_ATTEMPTS)
246 /* else - woops a session_id match */
247 /* XXX We should also check the external cache --
248 * but the probability of a collision is negligible, and
249 * we could not prevent the concurrent creation of sessions
250 * with identical IDs since we currently don't have means
251 * to atomically check whether a session ID already exists
252 * and make a reservation for it if it does not
253 * (this problem applies to the internal cache as well).
258 int ssl_get_new_session(SSL *s, int session)
260 /* This gets used by clients and servers. */
263 SSL_SESSION *ss=NULL;
264 GEN_SESSION_CB cb = def_generate_session_id;
266 if (s->mode & SSL_MODE_NO_SESSION_CREATION)
268 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SESSION_MAY_NOT_BE_CREATED);
272 if ((ss=SSL_SESSION_new()) == NULL) return(0);
274 /* If the context has a default timeout, use it over the default. */
275 if (s->initial_ctx->session_timeout != 0)
276 ss->timeout=s->initial_ctx->session_timeout;
278 if (s->session != NULL)
280 SSL_SESSION_free(s->session);
286 if (s->version == SSL2_VERSION)
288 ss->ssl_version=SSL2_VERSION;
289 ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
291 else if (s->version == SSL3_VERSION)
293 ss->ssl_version=SSL3_VERSION;
294 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
296 else if (s->version == TLS1_VERSION)
298 ss->ssl_version=TLS1_VERSION;
299 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
301 else if (s->version == TLS1_1_VERSION)
303 ss->ssl_version=TLS1_1_VERSION;
304 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
306 else if (s->version == TLS1_2_VERSION)
308 ss->ssl_version=TLS1_2_VERSION;
309 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
311 else if (s->version == DTLS1_VERSION)
313 ss->ssl_version=DTLS1_VERSION;
314 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
316 else if (s->version == DTLS1_2_VERSION)
318 ss->ssl_version=DTLS1_2_VERSION;
319 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
323 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_UNSUPPORTED_SSL_VERSION);
324 SSL_SESSION_free(ss);
327 /* If RFC4507 ticket use empty session ID */
328 if (s->tlsext_ticket_expected)
330 ss->session_id_length = 0;
333 /* Choose which callback will set the session ID */
334 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
335 if(s->generate_session_id)
336 cb = s->generate_session_id;
337 else if(s->initial_ctx->generate_session_id)
338 cb = s->initial_ctx->generate_session_id;
339 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
340 /* Choose a session ID */
341 tmp = ss->session_id_length;
342 if(!cb(s, ss->session_id, &tmp))
344 /* The callback failed */
345 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
346 SSL_SESSION_free(ss);
349 /* Don't allow the callback to set the session length to zero.
350 * nor set it higher than it was. */
351 if(!tmp || (tmp > ss->session_id_length))
353 /* The callback set an illegal length */
354 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
355 SSL_SESSION_free(ss);
358 /* If the session length was shrunk and we're SSLv2, pad it */
359 if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
360 memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
362 ss->session_id_length = tmp;
363 /* Finally, check for a conflict */
364 if(SSL_has_matching_session_id(s, ss->session_id,
365 ss->session_id_length))
367 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CONFLICT);
368 SSL_SESSION_free(ss);
372 if (s->tlsext_hostname) {
373 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
374 if (ss->tlsext_hostname == NULL) {
375 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
376 SSL_SESSION_free(ss);
380 if (s->psk_identity_hint)
382 ss->psk_identity_hint = BUF_strdup(s->psk_identity_hint);
383 if (ss->psk_identity_hint == NULL)
385 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_MALLOC_FAILURE);
386 SSL_SESSION_free(ss);
393 ss->session_id_length=0;
396 if (s->sid_ctx_length > sizeof ss->sid_ctx)
398 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
399 SSL_SESSION_free(ss);
402 memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
403 ss->sid_ctx_length=s->sid_ctx_length;
405 ss->ssl_version=s->version;
406 ss->verify_result = X509_V_OK;
411 /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
412 * connection. It is only called by servers.
414 * ctx: contains the early callback context, which is the result of a
415 * shallow parse of the ClientHello.
419 * 0: a session may have been found.
422 * - If a session is found then s->session is pointed at it (after freeing an
423 * existing session if need be) and s->verify_result is set from the session.
424 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
425 * if the server should issue a new session ticket (to 0 otherwise).
427 int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx)
429 /* This is used only by servers. */
431 SSL_SESSION *ret=NULL;
433 int try_session_cache = 1;
436 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH)
439 if (ctx->session_id_len == 0)
440 try_session_cache = 0;
442 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
445 case -1: /* Error during processing */
448 case 0: /* No ticket found */
449 case 1: /* Zero length ticket found */
450 break; /* Ok to carry on processing session id. */
451 case 2: /* Ticket found but not decrypted. */
452 case 3: /* Ticket decrypted, *ret has been set. */
453 try_session_cache = 0;
459 if (try_session_cache &&
461 !(s->initial_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
464 data.ssl_version=s->version;
465 data.session_id_length=ctx->session_id_len;
466 if (ctx->session_id_len == 0)
468 memcpy(data.session_id,ctx->session_id,ctx->session_id_len);
469 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
470 ret=lh_SSL_SESSION_retrieve(s->initial_ctx->sessions,&data);
473 /* don't allow other threads to steal it: */
474 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
476 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
478 s->initial_ctx->stats.sess_miss++;
481 if (try_session_cache &&
483 s->initial_ctx->get_session_cb != NULL)
487 if ((ret=s->initial_ctx->get_session_cb(s,(unsigned char *) ctx->session_id,ctx->session_id_len,©)))
489 if (ret == SSL_magic_pending_session_ptr())
491 /* This is a magic value which indicates that
492 * the callback needs to unwind the stack and
493 * figure out the session asynchronously. */
494 return PENDING_SESSION;
496 s->initial_ctx->stats.sess_cb_hit++;
498 /* Increment reference count now if the session callback
499 * asks us to do so (note that if the session structures
500 * returned by the callback are shared between threads,
501 * it must handle the reference count itself [i.e. copy == 0],
502 * or things won't be thread-safe). */
504 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
506 /* Add the externally cached session to the internal
507 * cache as well if and only if we are supposed to. */
508 if(!(s->initial_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
509 /* The following should not return 1, otherwise,
510 * things are very strange */
511 SSL_CTX_add_session(s->initial_ctx,ret);
518 /* Now ret is non-NULL and we own one of its reference counts. */
520 if (ret->sid_ctx_length != s->sid_ctx_length
521 || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
523 /* We have the session requested by the client, but we don't
524 * want to use it in this context. */
525 goto err; /* treat like cache miss */
528 if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
530 /* We can't be sure if this session is being used out of
531 * context, which is especially important for SSL_VERIFY_PEER.
532 * The application should have used SSL[_CTX]_set_session_id_context.
534 * For this error case, we generate an error instead of treating
535 * the event like a cache miss (otherwise it would be easy for
536 * applications to effectively disable the session cache by
537 * accident without anyone noticing).
540 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
545 if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
547 s->initial_ctx->stats.sess_timeout++;
548 if (try_session_cache)
550 /* session was from the cache, so remove it */
551 SSL_CTX_remove_session(s->initial_ctx,ret);
556 s->initial_ctx->stats.sess_hit++;
558 if (s->session != NULL)
559 SSL_SESSION_free(s->session);
561 s->verify_result = s->session->verify_result;
567 SSL_SESSION_free(ret);
568 if (!try_session_cache)
570 /* The session was from a ticket, so we should
571 * issue a ticket for the new session */
572 s->tlsext_ticket_expected = 1;
581 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
586 /* add just 1 reference count for the SSL_CTX's session cache
587 * even though it has two ways of access: each session is in a
588 * doubly linked list and an lhash */
589 CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
590 /* if session c is in already in cache, we take back the increment later */
592 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
593 if (!lh_SSL_SESSION_insert(ctx->sessions,&s,c)) {
597 /* s != NULL iff we already had a session with the given PID.
598 * In this case, s == c should hold (then we did not really modify
599 * ctx->sessions), or we're in trouble. */
600 if (s != NULL && s != c)
602 /* We *are* in trouble ... */
603 SSL_SESSION_list_remove(ctx,s);
605 /* ... so pretend the other session did not exist in cache
606 * (we cannot handle two SSL_SESSION structures with identical
607 * session ID in the same cache, which could happen e.g. when
608 * two threads concurrently obtain the same session from an external
613 /* Put at the head of the queue unless it is already in the cache */
615 SSL_SESSION_list_add(ctx,c);
619 /* existing cache entry -- decrement previously incremented reference
620 * count because it already takes into account the cache */
622 SSL_SESSION_free(s); /* s == c */
627 /* new cache entry -- remove old ones if cache has become too large */
631 if (SSL_CTX_sess_get_cache_size(ctx) > 0)
633 while (SSL_CTX_sess_number(ctx) >
634 SSL_CTX_sess_get_cache_size(ctx))
636 if (!remove_session_lock(ctx,
637 ctx->session_cache_tail, 0))
640 ctx->stats.sess_cache_full++;
644 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
648 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
650 return remove_session_lock(ctx, c, 1);
653 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
658 if ((c != NULL) && (c->session_id_length != 0))
660 if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
661 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
664 r=lh_SSL_SESSION_delete(ctx->sessions,c);
665 SSL_SESSION_list_remove(ctx,c);
668 if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
673 if (ctx->remove_session_cb != NULL)
674 ctx->remove_session_cb(ctx,r);
683 void SSL_SESSION_free(SSL_SESSION *ss)
690 i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
692 REF_PRINT("SSL_SESSION",ss);
698 fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
703 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
705 OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
706 OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
707 if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
708 if (ss->peer != NULL) X509_free(ss->peer);
709 if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
710 if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
711 if (ss->tlsext_signed_cert_timestamp_list != NULL)
712 OPENSSL_free(ss->tlsext_signed_cert_timestamp_list);
713 if (ss->ocsp_response != NULL)
714 OPENSSL_free(ss->ocsp_response);
715 if (ss->psk_identity_hint != NULL)
716 OPENSSL_free(ss->psk_identity_hint);
717 if (ss->psk_identity != NULL)
718 OPENSSL_free(ss->psk_identity);
719 OPENSSL_cleanse(ss,sizeof(*ss));
723 int SSL_set_session(SSL *s, SSL_SESSION *session)
726 const SSL_METHOD *meth;
730 meth=s->ctx->method->get_ssl_method(session->ssl_version);
732 meth=s->method->get_ssl_method(session->ssl_version);
735 OPENSSL_PUT_ERROR(SSL, SSL_set_session, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
739 if (meth != s->method)
741 if (!SSL_set_ssl_method(s,meth))
745 /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
746 CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
747 if (s->session != NULL)
748 SSL_SESSION_free(s->session);
750 s->verify_result = s->session->verify_result;
751 /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
756 if (s->session != NULL)
758 SSL_SESSION_free(s->session);
763 if (meth != s->method)
765 if (!SSL_set_ssl_method(s,meth))
773 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
775 if (s == NULL) return(0);
780 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
782 if (s == NULL) return(0);
786 long SSL_SESSION_get_time(const SSL_SESSION *s)
788 if (s == NULL) return(0);
792 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
794 if (s == NULL) return(0);
799 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
804 int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
805 unsigned int sid_ctx_len)
807 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
809 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
812 s->sid_ctx_length=sid_ctx_len;
813 memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
818 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
821 if (s == NULL) return(0);
822 l=s->session_timeout;
823 s->session_timeout=t;
827 long SSL_CTX_get_timeout(const SSL_CTX *s)
829 if (s == NULL) return(0);
830 return(s->session_timeout);
833 int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
834 STACK_OF(SSL_CIPHER) *peer_ciphers, const SSL_CIPHER **cipher, void *arg), void *arg)
836 if (s == NULL) return(0);
837 s->tls_session_secret_cb = tls_session_secret_cb;
838 s->tls_session_secret_cb_arg = arg;
842 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
845 if (s == NULL) return(0);
846 s->tls_session_ticket_ext_cb = cb;
847 s->tls_session_ticket_ext_cb_arg = arg;
851 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
853 if (s->version >= TLS1_VERSION)
855 if (s->tlsext_session_ticket)
857 OPENSSL_free(s->tlsext_session_ticket);
858 s->tlsext_session_ticket = NULL;
861 s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
862 if (!s->tlsext_session_ticket)
864 OPENSSL_PUT_ERROR(SSL, SSL_set_session_ticket_ext, ERR_R_MALLOC_FAILURE);
870 s->tlsext_session_ticket->length = ext_len;
871 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
872 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
876 s->tlsext_session_ticket->length = 0;
877 s->tlsext_session_ticket->data = NULL;
886 typedef struct timeout_param_st
890 LHASH_OF(SSL_SESSION) *cache;
893 static void timeout_doall_arg(SSL_SESSION *sess, void *void_param)
895 TIMEOUT_PARAM *param = void_param;
897 if ((param->time == 0) || (param->time > (sess->time+sess->timeout))) /* timeout */
899 /* The reason we don't call SSL_CTX_remove_session() is to
900 * save on locking overhead */
901 (void)lh_SSL_SESSION_delete(param->cache,sess);
902 SSL_SESSION_list_remove(param->ctx,sess);
903 sess->not_resumable=1;
904 if (param->ctx->remove_session_cb != NULL)
905 param->ctx->remove_session_cb(param->ctx,sess);
906 SSL_SESSION_free(sess);
910 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
915 tp.cache=s->sessions;
916 if (tp.cache == NULL) return;
918 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
919 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
920 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
923 int ssl_clear_bad_session(SSL *s)
925 if ( (s->session != NULL) &&
926 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
927 !(SSL_in_init(s) || SSL_in_before(s)))
929 SSL_CTX_remove_session(s->ctx,s->session);
936 /* locked by SSL_CTX in the calling function */
937 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
939 if ((s->next == NULL) || (s->prev == NULL)) return;
941 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
942 { /* last element in list */
943 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
944 { /* only one element in list */
945 ctx->session_cache_head=NULL;
946 ctx->session_cache_tail=NULL;
950 ctx->session_cache_tail=s->prev;
951 s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
956 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
957 { /* first element in list */
958 ctx->session_cache_head=s->next;
959 s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
962 { /* middle of list */
963 s->next->prev=s->prev;
964 s->prev->next=s->next;
967 s->prev=s->next=NULL;
970 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
972 if ((s->next != NULL) && (s->prev != NULL))
973 SSL_SESSION_list_remove(ctx,s);
975 if (ctx->session_cache_head == NULL)
977 ctx->session_cache_head=s;
978 ctx->session_cache_tail=s;
979 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
980 s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
984 s->next=ctx->session_cache_head;
986 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
987 ctx->session_cache_head=s;
991 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
992 int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
994 ctx->new_session_cb=cb;
997 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
999 return ctx->new_session_cb;
1002 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1003 void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
1005 ctx->remove_session_cb=cb;
1008 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
1010 return ctx->remove_session_cb;
1013 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1014 SSL_SESSION *(*cb)(struct ssl_st *ssl,
1015 unsigned char *data,int len,int *copy))
1017 ctx->get_session_cb=cb;
1020 SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
1021 unsigned char *data,int len,int *copy)
1023 return ctx->get_session_cb;
1026 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1027 void (*cb)(const SSL *ssl,int type,int val))
1029 ctx->info_callback=cb;
1032 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
1034 return ctx->info_callback;
1037 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1038 int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1040 ctx->client_cert_cb=cb;
1043 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
1045 return ctx->client_cert_cb;
1048 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1049 int (*cb)(SSL *ssl, uint8_t *cookie, size_t *cookie_len))
1051 ctx->app_gen_cookie_cb=cb;
1054 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1055 int (*cb)(SSL *ssl, const uint8_t *cookie, size_t cookie_len))
1057 ctx->app_verify_cookie_cb=cb;
1060 void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
1061 void (*cb)(SSL *ssl, EVP_PKEY **pkey))
1063 ctx->channel_id_cb=cb;
1066 void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL * ssl, EVP_PKEY **pkey)
1068 return ctx->channel_id_cb;
1071 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)