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