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()
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=60*5+4; /* 5 minute timeout by default */
213 ss->time=(unsigned long)time(NULL);
216 ss->tlsext_hostname = NULL;
217 #ifndef OPENSSL_NO_EC
218 ss->tlsext_ecpointformatlist_length = 0;
219 ss->tlsext_ecpointformatlist = NULL;
220 ss->tlsext_ellipticcurvelist_length = 0;
221 ss->tlsext_ellipticcurvelist = NULL;
223 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
224 ss->psk_identity_hint=NULL;
225 ss->psk_identity=NULL;
229 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
232 *len = s->session_id_length;
233 return s->session_id;
236 /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
237 * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
238 * until we have no conflict is going to complete in one iteration pretty much
239 * "most" of the time (btw: understatement). So, if it takes us 10 iterations
240 * and we still can't avoid a conflict - well that's a reasonable point to call
241 * it quits. Either the RAND code is broken or someone is trying to open roughly
242 * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
243 * store that many sessions is perhaps a more interesting question ... */
245 #define MAX_SESS_ID_ATTEMPTS 10
246 static int def_generate_session_id(const SSL *ssl, unsigned char *id,
247 unsigned int *id_len)
249 unsigned int retry = 0;
251 if (RAND_pseudo_bytes(id, *id_len) <= 0)
253 while(SSL_has_matching_session_id(ssl, id, *id_len) &&
254 (++retry < MAX_SESS_ID_ATTEMPTS));
255 if(retry < MAX_SESS_ID_ATTEMPTS)
257 /* else - woops a session_id match */
258 /* XXX We should also check the external cache --
259 * but the probability of a collision is negligible, and
260 * we could not prevent the concurrent creation of sessions
261 * with identical IDs since we currently don't have means
262 * to atomically check whether a session ID already exists
263 * and make a reservation for it if it does not
264 * (this problem applies to the internal cache as well).
269 int ssl_get_new_session(SSL *s, int session)
271 /* This gets used by clients and servers. */
274 SSL_SESSION *ss=NULL;
275 GEN_SESSION_CB cb = def_generate_session_id;
277 if ((ss=SSL_SESSION_new()) == NULL) return(0);
279 /* If the context has a default timeout, use it */
280 if (s->session_ctx->session_timeout == 0)
281 ss->timeout=SSL_get_default_timeout(s);
283 ss->timeout=s->session_ctx->session_timeout;
285 if (s->session != NULL)
287 SSL_SESSION_free(s->session);
293 if (s->version == SSL2_VERSION)
295 ss->ssl_version=SSL2_VERSION;
296 ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
298 else if (s->version == SSL3_VERSION)
300 ss->ssl_version=SSL3_VERSION;
301 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
303 else if (s->version == TLS1_VERSION)
305 ss->ssl_version=TLS1_VERSION;
306 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
308 else if (s->version == TLS1_1_VERSION)
310 ss->ssl_version=TLS1_1_VERSION;
311 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
313 else if (s->version == TLS1_2_VERSION)
315 ss->ssl_version=TLS1_2_VERSION;
316 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
318 else if (s->version == DTLS1_BAD_VER)
320 ss->ssl_version=DTLS1_BAD_VER;
321 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
323 else if (s->version == DTLS1_VERSION)
325 ss->ssl_version=DTLS1_VERSION;
326 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
328 else if (s->version == DTLS1_2_VERSION)
330 ss->ssl_version=DTLS1_2_VERSION;
331 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
335 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_UNSUPPORTED_SSL_VERSION);
336 SSL_SESSION_free(ss);
339 /* If RFC4507 ticket use empty session ID */
340 if (s->tlsext_ticket_expected)
342 ss->session_id_length = 0;
345 /* Choose which callback will set the session ID */
346 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
347 if(s->generate_session_id)
348 cb = s->generate_session_id;
349 else if(s->session_ctx->generate_session_id)
350 cb = s->session_ctx->generate_session_id;
351 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
352 /* Choose a session ID */
353 tmp = ss->session_id_length;
354 if(!cb(s, ss->session_id, &tmp))
356 /* The callback failed */
357 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
358 SSL_SESSION_free(ss);
361 /* Don't allow the callback to set the session length to zero.
362 * nor set it higher than it was. */
363 if(!tmp || (tmp > ss->session_id_length))
365 /* The callback set an illegal length */
366 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
367 SSL_SESSION_free(ss);
370 /* If the session length was shrunk and we're SSLv2, pad it */
371 if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
372 memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
374 ss->session_id_length = tmp;
375 /* Finally, check for a conflict */
376 if(SSL_has_matching_session_id(s, ss->session_id,
377 ss->session_id_length))
379 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CONFLICT);
380 SSL_SESSION_free(ss);
384 if (s->tlsext_hostname) {
385 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
386 if (ss->tlsext_hostname == NULL) {
387 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
388 SSL_SESSION_free(ss);
392 if (s->psk_identity_hint)
394 ss->psk_identity_hint = BUF_strdup(s->psk_identity_hint);
395 if (ss->psk_identity_hint == NULL)
397 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_MALLOC_FAILURE);
398 SSL_SESSION_free(ss);
405 ss->session_id_length=0;
408 if (s->sid_ctx_length > sizeof ss->sid_ctx)
410 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
411 SSL_SESSION_free(ss);
414 memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
415 ss->sid_ctx_length=s->sid_ctx_length;
417 ss->ssl_version=s->version;
418 ss->verify_result = X509_V_OK;
423 /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
424 * connection. It is only called by servers.
426 * ctx: contains the early callback context, which is the result of a
427 * shallow parse of the ClientHello.
431 * 0: a session may have been found.
434 * - If a session is found then s->session is pointed at it (after freeing an
435 * existing session if need be) and s->verify_result is set from the session.
436 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
437 * if the server should issue a new session ticket (to 0 otherwise).
439 int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx)
441 /* This is used only by servers. */
443 SSL_SESSION *ret=NULL;
445 int try_session_cache = 1;
448 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH)
451 if (ctx->session_id_len == 0)
452 try_session_cache = 0;
454 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
457 case -1: /* Error during processing */
460 case 0: /* No ticket found */
461 case 1: /* Zero length ticket found */
462 break; /* Ok to carry on processing session id. */
463 case 2: /* Ticket found but not decrypted. */
464 case 3: /* Ticket decrypted, *ret has been set. */
465 try_session_cache = 0;
471 if (try_session_cache &&
473 !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
476 data.ssl_version=s->version;
477 data.session_id_length=ctx->session_id_len;
478 if (ctx->session_id_len == 0)
480 memcpy(data.session_id,ctx->session_id,ctx->session_id_len);
481 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
482 ret=lh_SSL_SESSION_retrieve(s->session_ctx->sessions,&data);
485 /* don't allow other threads to steal it: */
486 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
488 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
490 s->session_ctx->stats.sess_miss++;
493 if (try_session_cache &&
495 s->session_ctx->get_session_cb != NULL)
499 if ((ret=s->session_ctx->get_session_cb(s,(unsigned char *) ctx->session_id,ctx->session_id_len,©)))
501 if (ret == SSL_magic_pending_session_ptr())
503 /* This is a magic value which indicates that
504 * the callback needs to unwind the stack and
505 * figure out the session asynchronously. */
506 return PENDING_SESSION;
508 s->session_ctx->stats.sess_cb_hit++;
510 /* Increment reference count now if the session callback
511 * asks us to do so (note that if the session structures
512 * returned by the callback are shared between threads,
513 * it must handle the reference count itself [i.e. copy == 0],
514 * or things won't be thread-safe). */
516 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
518 /* Add the externally cached session to the internal
519 * cache as well if and only if we are supposed to. */
520 if(!(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
521 /* The following should not return 1, otherwise,
522 * things are very strange */
523 SSL_CTX_add_session(s->session_ctx,ret);
530 /* Now ret is non-NULL and we own one of its reference counts. */
532 if (ret->sid_ctx_length != s->sid_ctx_length
533 || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
535 /* We have the session requested by the client, but we don't
536 * want to use it in this context. */
537 goto err; /* treat like cache miss */
540 if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
542 /* We can't be sure if this session is being used out of
543 * context, which is especially important for SSL_VERIFY_PEER.
544 * The application should have used SSL[_CTX]_set_session_id_context.
546 * For this error case, we generate an error instead of treating
547 * the event like a cache miss (otherwise it would be easy for
548 * applications to effectively disable the session cache by
549 * accident without anyone noticing).
552 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
557 if (ret->cipher == NULL)
559 /* The cipher id has a leading 0x03 to be removed (and then put
560 * back for the binary search) as a remnant of SSLv2 support. */
561 ret->cipher = ssl3_get_cipher_by_value(ret->cipher_id & 0xffff);
562 if (ret->cipher == NULL)
566 if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
568 s->session_ctx->stats.sess_timeout++;
569 if (try_session_cache)
571 /* session was from the cache, so remove it */
572 SSL_CTX_remove_session(s->session_ctx,ret);
577 s->session_ctx->stats.sess_hit++;
579 if (s->session != NULL)
580 SSL_SESSION_free(s->session);
582 s->verify_result = s->session->verify_result;
588 SSL_SESSION_free(ret);
589 if (!try_session_cache)
591 /* The session was from a ticket, so we should
592 * issue a ticket for the new session */
593 s->tlsext_ticket_expected = 1;
602 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
607 /* add just 1 reference count for the SSL_CTX's session cache
608 * even though it has two ways of access: each session is in a
609 * doubly linked list and an lhash */
610 CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
611 /* if session c is in already in cache, we take back the increment later */
613 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
614 if (!lh_SSL_SESSION_insert(ctx->sessions,&s,c)) {
618 /* s != NULL iff we already had a session with the given PID.
619 * In this case, s == c should hold (then we did not really modify
620 * ctx->sessions), or we're in trouble. */
621 if (s != NULL && s != c)
623 /* We *are* in trouble ... */
624 SSL_SESSION_list_remove(ctx,s);
626 /* ... so pretend the other session did not exist in cache
627 * (we cannot handle two SSL_SESSION structures with identical
628 * session ID in the same cache, which could happen e.g. when
629 * two threads concurrently obtain the same session from an external
634 /* Put at the head of the queue unless it is already in the cache */
636 SSL_SESSION_list_add(ctx,c);
640 /* existing cache entry -- decrement previously incremented reference
641 * count because it already takes into account the cache */
643 SSL_SESSION_free(s); /* s == c */
648 /* new cache entry -- remove old ones if cache has become too large */
652 if (SSL_CTX_sess_get_cache_size(ctx) > 0)
654 while (SSL_CTX_sess_number(ctx) >
655 SSL_CTX_sess_get_cache_size(ctx))
657 if (!remove_session_lock(ctx,
658 ctx->session_cache_tail, 0))
661 ctx->stats.sess_cache_full++;
665 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
669 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
671 return remove_session_lock(ctx, c, 1);
674 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
679 if ((c != NULL) && (c->session_id_length != 0))
681 if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
682 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
685 r=lh_SSL_SESSION_delete(ctx->sessions,c);
686 SSL_SESSION_list_remove(ctx,c);
689 if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
694 if (ctx->remove_session_cb != NULL)
695 ctx->remove_session_cb(ctx,r);
704 void SSL_SESSION_free(SSL_SESSION *ss)
711 i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
713 REF_PRINT("SSL_SESSION",ss);
719 fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
724 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
726 OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
727 OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
728 OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
729 if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
730 if (ss->peer != NULL) X509_free(ss->peer);
731 if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
732 if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
733 if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
734 #ifndef OPENSSL_NO_EC
735 ss->tlsext_ecpointformatlist_length = 0;
736 if (ss->tlsext_ecpointformatlist != NULL) OPENSSL_free(ss->tlsext_ecpointformatlist);
737 ss->tlsext_ellipticcurvelist_length = 0;
738 if (ss->tlsext_ellipticcurvelist != NULL) OPENSSL_free(ss->tlsext_ellipticcurvelist);
739 #endif /* OPENSSL_NO_EC */
740 if (ss->psk_identity_hint != NULL)
741 OPENSSL_free(ss->psk_identity_hint);
742 if (ss->psk_identity != NULL)
743 OPENSSL_free(ss->psk_identity);
744 OPENSSL_cleanse(ss,sizeof(*ss));
748 int SSL_set_session(SSL *s, SSL_SESSION *session)
751 const SSL_METHOD *meth;
755 meth=s->ctx->method->get_ssl_method(session->ssl_version);
757 meth=s->method->get_ssl_method(session->ssl_version);
760 OPENSSL_PUT_ERROR(SSL, SSL_set_session, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
764 if (meth != s->method)
766 if (!SSL_set_ssl_method(s,meth))
770 /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
771 CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
772 if (s->session != NULL)
773 SSL_SESSION_free(s->session);
775 s->verify_result = s->session->verify_result;
776 /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
781 if (s->session != NULL)
783 SSL_SESSION_free(s->session);
788 if (meth != s->method)
790 if (!SSL_set_ssl_method(s,meth))
798 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
800 if (s == NULL) return(0);
805 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
807 if (s == NULL) return(0);
811 long SSL_SESSION_get_time(const SSL_SESSION *s)
813 if (s == NULL) return(0);
817 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
819 if (s == NULL) return(0);
824 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
829 int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
830 unsigned int sid_ctx_len)
832 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
834 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
837 s->sid_ctx_length=sid_ctx_len;
838 memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
843 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
846 if (s == NULL) return(0);
847 l=s->session_timeout;
848 s->session_timeout=t;
852 long SSL_CTX_get_timeout(const SSL_CTX *s)
854 if (s == NULL) return(0);
855 return(s->session_timeout);
858 int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
859 STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
861 if (s == NULL) return(0);
862 s->tls_session_secret_cb = tls_session_secret_cb;
863 s->tls_session_secret_cb_arg = arg;
867 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
870 if (s == NULL) return(0);
871 s->tls_session_ticket_ext_cb = cb;
872 s->tls_session_ticket_ext_cb_arg = arg;
876 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
878 if (s->version >= TLS1_VERSION)
880 if (s->tlsext_session_ticket)
882 OPENSSL_free(s->tlsext_session_ticket);
883 s->tlsext_session_ticket = NULL;
886 s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
887 if (!s->tlsext_session_ticket)
889 OPENSSL_PUT_ERROR(SSL, SSL_set_session_ticket_ext, ERR_R_MALLOC_FAILURE);
895 s->tlsext_session_ticket->length = ext_len;
896 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
897 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
901 s->tlsext_session_ticket->length = 0;
902 s->tlsext_session_ticket->data = NULL;
911 typedef struct timeout_param_st
915 LHASH_OF(SSL_SESSION) *cache;
918 static void timeout_doall_arg(SSL_SESSION *sess, void *void_param)
920 TIMEOUT_PARAM *param = void_param;
922 if ((param->time == 0) || (param->time > (sess->time+sess->timeout))) /* timeout */
924 /* The reason we don't call SSL_CTX_remove_session() is to
925 * save on locking overhead */
926 (void)lh_SSL_SESSION_delete(param->cache,sess);
927 SSL_SESSION_list_remove(param->ctx,sess);
928 sess->not_resumable=1;
929 if (param->ctx->remove_session_cb != NULL)
930 param->ctx->remove_session_cb(param->ctx,sess);
931 SSL_SESSION_free(sess);
935 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
940 tp.cache=s->sessions;
941 if (tp.cache == NULL) return;
943 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
944 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
945 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
948 int ssl_clear_bad_session(SSL *s)
950 if ( (s->session != NULL) &&
951 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
952 !(SSL_in_init(s) || SSL_in_before(s)))
954 SSL_CTX_remove_session(s->ctx,s->session);
961 /* locked by SSL_CTX in the calling function */
962 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
964 if ((s->next == NULL) || (s->prev == NULL)) return;
966 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
967 { /* last element in list */
968 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
969 { /* only one element in list */
970 ctx->session_cache_head=NULL;
971 ctx->session_cache_tail=NULL;
975 ctx->session_cache_tail=s->prev;
976 s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
981 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
982 { /* first element in list */
983 ctx->session_cache_head=s->next;
984 s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
987 { /* middle of list */
988 s->next->prev=s->prev;
989 s->prev->next=s->next;
992 s->prev=s->next=NULL;
995 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
997 if ((s->next != NULL) && (s->prev != NULL))
998 SSL_SESSION_list_remove(ctx,s);
1000 if (ctx->session_cache_head == NULL)
1002 ctx->session_cache_head=s;
1003 ctx->session_cache_tail=s;
1004 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1005 s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
1009 s->next=ctx->session_cache_head;
1011 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1012 ctx->session_cache_head=s;
1016 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1017 int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
1019 ctx->new_session_cb=cb;
1022 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1024 return ctx->new_session_cb;
1027 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1028 void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
1030 ctx->remove_session_cb=cb;
1033 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
1035 return ctx->remove_session_cb;
1038 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1039 SSL_SESSION *(*cb)(struct ssl_st *ssl,
1040 unsigned char *data,int len,int *copy))
1042 ctx->get_session_cb=cb;
1045 SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
1046 unsigned char *data,int len,int *copy)
1048 return ctx->get_session_cb;
1051 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1052 void (*cb)(const SSL *ssl,int type,int val))
1054 ctx->info_callback=cb;
1057 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
1059 return ctx->info_callback;
1062 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1063 int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1065 ctx->client_cert_cb=cb;
1068 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
1070 return ctx->client_cert_cb;
1073 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1074 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1076 ctx->app_gen_cookie_cb=cb;
1079 /* TODO(davidben): |cookie| should be a const pointer. */
1080 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1081 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
1083 ctx->app_verify_cookie_cb=cb;
1086 void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
1087 void (*cb)(SSL *ssl, EVP_PKEY **pkey))
1089 ctx->channel_id_cb=cb;
1092 void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL * ssl, EVP_PKEY **pkey)
1094 return ctx->channel_id_cb;
1097 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)