blob: 923a99231f22365b970ac5008cf42dacc155be4e [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
Adam Langleyb2ce0582014-06-20 12:00:00 -0700146/* 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. */
149static char g_pending_session_magic;
150
Adam Langley95c29f32014-06-20 12:00:00 -0700151static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
152static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
153static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
154
Adam Langleyb2ce0582014-06-20 12:00:00 -0700155SSL_SESSION *SSL_magic_pending_session_ptr()
156 {
157 return (SSL_SESSION*) &g_pending_session_magic;
158 }
159
Adam Langley95c29f32014-06-20 12:00:00 -0700160SSL_SESSION *SSL_get_session(const SSL *ssl)
161/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
162 {
163 return(ssl->session);
164 }
165
166SSL_SESSION *SSL_get1_session(SSL *ssl)
167/* variant of SSL_get_session: caller really gets something */
168 {
169 SSL_SESSION *sess;
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);
174 sess = ssl->session;
175 if(sess)
176 sess->references++;
177 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
178 return(sess);
179 }
180
181int 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)
183 {
184 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
185 new_func, dup_func, free_func);
186 }
187
188int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
189 {
190 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
191 }
192
193void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
194 {
195 return(CRYPTO_get_ex_data(&s->ex_data,idx));
196 }
197
198SSL_SESSION *SSL_SESSION_new(void)
199 {
200 SSL_SESSION *ss;
201
202 ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
203 if (ss == NULL)
204 {
205 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
206 return(0);
207 }
208 memset(ss,0,sizeof(SSL_SESSION));
209
210 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
211 ss->references=1;
212 ss->timeout=60*5+4; /* 5 minute timeout by default */
213 ss->time=(unsigned long)time(NULL);
214 ss->prev=NULL;
215 ss->next=NULL;
216 ss->compress_meth=0;
217#ifndef OPENSSL_NO_TLSEXT
218 ss->tlsext_hostname = NULL;
219#ifndef OPENSSL_NO_EC
220 ss->tlsext_ecpointformatlist_length = 0;
221 ss->tlsext_ecpointformatlist = NULL;
222 ss->tlsext_ellipticcurvelist_length = 0;
223 ss->tlsext_ellipticcurvelist = NULL;
224#endif
225#endif
226 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
227#ifndef OPENSSL_NO_PSK
228 ss->psk_identity_hint=NULL;
229 ss->psk_identity=NULL;
230#endif
231 return(ss);
232 }
233
234const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
235 {
236 if(len)
237 *len = s->session_id_length;
238 return s->session_id;
239 }
240
241unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
242 {
243 return s->compress_meth;
244 }
245
246/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
247 * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
248 * until we have no conflict is going to complete in one iteration pretty much
249 * "most" of the time (btw: understatement). So, if it takes us 10 iterations
250 * and we still can't avoid a conflict - well that's a reasonable point to call
251 * it quits. Either the RAND code is broken or someone is trying to open roughly
252 * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
253 * store that many sessions is perhaps a more interesting question ... */
254
255#define MAX_SESS_ID_ATTEMPTS 10
256static int def_generate_session_id(const SSL *ssl, unsigned char *id,
257 unsigned int *id_len)
258{
259 unsigned int retry = 0;
260 do
261 if (RAND_pseudo_bytes(id, *id_len) <= 0)
262 return 0;
263 while(SSL_has_matching_session_id(ssl, id, *id_len) &&
264 (++retry < MAX_SESS_ID_ATTEMPTS));
265 if(retry < MAX_SESS_ID_ATTEMPTS)
266 return 1;
267 /* else - woops a session_id match */
268 /* XXX We should also check the external cache --
269 * but the probability of a collision is negligible, and
270 * we could not prevent the concurrent creation of sessions
271 * with identical IDs since we currently don't have means
272 * to atomically check whether a session ID already exists
273 * and make a reservation for it if it does not
274 * (this problem applies to the internal cache as well).
275 */
276 return 0;
277}
278
279int ssl_get_new_session(SSL *s, int session)
280 {
281 /* This gets used by clients and servers. */
282
283 unsigned int tmp;
284 SSL_SESSION *ss=NULL;
285 GEN_SESSION_CB cb = def_generate_session_id;
286
287 if ((ss=SSL_SESSION_new()) == NULL) return(0);
288
289 /* If the context has a default timeout, use it */
290 if (s->session_ctx->session_timeout == 0)
291 ss->timeout=SSL_get_default_timeout(s);
292 else
293 ss->timeout=s->session_ctx->session_timeout;
294
295 if (s->session != NULL)
296 {
297 SSL_SESSION_free(s->session);
298 s->session=NULL;
299 }
300
301 if (session)
302 {
303 if (s->version == SSL2_VERSION)
304 {
305 ss->ssl_version=SSL2_VERSION;
306 ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
307 }
308 else if (s->version == SSL3_VERSION)
309 {
310 ss->ssl_version=SSL3_VERSION;
311 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
312 }
313 else if (s->version == TLS1_VERSION)
314 {
315 ss->ssl_version=TLS1_VERSION;
316 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
317 }
318 else if (s->version == TLS1_1_VERSION)
319 {
320 ss->ssl_version=TLS1_1_VERSION;
321 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
322 }
323 else if (s->version == TLS1_2_VERSION)
324 {
325 ss->ssl_version=TLS1_2_VERSION;
326 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
327 }
328 else if (s->version == DTLS1_BAD_VER)
329 {
330 ss->ssl_version=DTLS1_BAD_VER;
331 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
332 }
333 else if (s->version == DTLS1_VERSION)
334 {
335 ss->ssl_version=DTLS1_VERSION;
336 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
337 }
338 else if (s->version == DTLS1_2_VERSION)
339 {
340 ss->ssl_version=DTLS1_2_VERSION;
341 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
342 }
343 else
344 {
345 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_UNSUPPORTED_SSL_VERSION);
346 SSL_SESSION_free(ss);
347 return(0);
348 }
349#ifndef OPENSSL_NO_TLSEXT
350 /* If RFC4507 ticket use empty session ID */
351 if (s->tlsext_ticket_expected)
352 {
353 ss->session_id_length = 0;
354 goto sess_id_done;
355 }
356#endif
357 /* Choose which callback will set the session ID */
358 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
359 if(s->generate_session_id)
360 cb = s->generate_session_id;
361 else if(s->session_ctx->generate_session_id)
362 cb = s->session_ctx->generate_session_id;
363 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
364 /* Choose a session ID */
365 tmp = ss->session_id_length;
366 if(!cb(s, ss->session_id, &tmp))
367 {
368 /* The callback failed */
369 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
370 SSL_SESSION_free(ss);
371 return(0);
372 }
373 /* Don't allow the callback to set the session length to zero.
374 * nor set it higher than it was. */
375 if(!tmp || (tmp > ss->session_id_length))
376 {
377 /* The callback set an illegal length */
378 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
379 SSL_SESSION_free(ss);
380 return(0);
381 }
382 /* If the session length was shrunk and we're SSLv2, pad it */
383 if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
384 memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
385 else
386 ss->session_id_length = tmp;
387 /* Finally, check for a conflict */
388 if(SSL_has_matching_session_id(s, ss->session_id,
389 ss->session_id_length))
390 {
391 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CONFLICT);
392 SSL_SESSION_free(ss);
393 return(0);
394 }
395#ifndef OPENSSL_NO_TLSEXT
396 sess_id_done:
397 if (s->tlsext_hostname) {
398 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
399 if (ss->tlsext_hostname == NULL) {
400 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
401 SSL_SESSION_free(ss);
402 return 0;
403 }
404 }
405#endif
406 }
407 else
408 {
409 ss->session_id_length=0;
410 }
411
412 if (s->sid_ctx_length > sizeof ss->sid_ctx)
413 {
414 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
415 SSL_SESSION_free(ss);
416 return 0;
417 }
418 memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
419 ss->sid_ctx_length=s->sid_ctx_length;
420 s->session=ss;
421 ss->ssl_version=s->version;
422 ss->verify_result = X509_V_OK;
423
424 return(1);
425 }
426
427/* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
428 * connection. It is only called by servers.
429 *
Adam Langleydc9b1412014-06-20 12:00:00 -0700430 * ctx: contains the early callback context, which is the result of a
431 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -0700432 *
433 * Returns:
434 * -1: error
435 * 0: a session may have been found.
436 *
437 * Side effects:
438 * - If a session is found then s->session is pointed at it (after freeing an
439 * existing session if need be) and s->verify_result is set from the session.
440 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
441 * if the server should issue a new session ticket (to 0 otherwise).
442 */
Adam Langleydc9b1412014-06-20 12:00:00 -0700443int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx)
Adam Langley95c29f32014-06-20 12:00:00 -0700444 {
445 /* This is used only by servers. */
446
447 SSL_SESSION *ret=NULL;
448 int fatal = 0;
449 int try_session_cache = 1;
450#ifndef OPENSSL_NO_TLSEXT
451 int r;
452#endif
453
Adam Langleydc9b1412014-06-20 12:00:00 -0700454 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH)
Adam Langley95c29f32014-06-20 12:00:00 -0700455 goto err;
456
Adam Langleydc9b1412014-06-20 12:00:00 -0700457 if (ctx->session_id_len == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700458 try_session_cache = 0;
459
460#ifndef OPENSSL_NO_TLSEXT
Adam Langleydc9b1412014-06-20 12:00:00 -0700461 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
Adam Langley95c29f32014-06-20 12:00:00 -0700462 switch (r)
463 {
464 case -1: /* Error during processing */
465 fatal = 1;
466 goto err;
467 case 0: /* No ticket found */
468 case 1: /* Zero length ticket found */
469 break; /* Ok to carry on processing session id. */
470 case 2: /* Ticket found but not decrypted. */
471 case 3: /* Ticket decrypted, *ret has been set. */
472 try_session_cache = 0;
473 break;
474 default:
475 abort();
476 }
477#endif
478
479 if (try_session_cache &&
480 ret == NULL &&
481 !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
482 {
483 SSL_SESSION data;
484 data.ssl_version=s->version;
Adam Langleydc9b1412014-06-20 12:00:00 -0700485 data.session_id_length=ctx->session_id_len;
486 if (ctx->session_id_len == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700487 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700488 memcpy(data.session_id,ctx->session_id,ctx->session_id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700489 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
490 ret=lh_SSL_SESSION_retrieve(s->session_ctx->sessions,&data);
491 if (ret != NULL)
492 {
493 /* don't allow other threads to steal it: */
494 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
495 }
496 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
497 if (ret == NULL)
498 s->session_ctx->stats.sess_miss++;
499 }
500
501 if (try_session_cache &&
502 ret == NULL &&
503 s->session_ctx->get_session_cb != NULL)
504 {
505 int copy=1;
506
Adam Langleydc9b1412014-06-20 12:00:00 -0700507 if ((ret=s->session_ctx->get_session_cb(s,(unsigned char *) ctx->session_id,ctx->session_id_len,&copy)))
Adam Langley95c29f32014-06-20 12:00:00 -0700508 {
Adam Langleyb2ce0582014-06-20 12:00:00 -0700509 if (ret == SSL_magic_pending_session_ptr())
510 {
511 /* This is a magic value which indicates that
512 * the callback needs to unwind the stack and
513 * figure out the session asynchronously. */
514 return PENDING_SESSION;
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516 s->session_ctx->stats.sess_cb_hit++;
517
518 /* Increment reference count now if the session callback
519 * asks us to do so (note that if the session structures
520 * returned by the callback are shared between threads,
521 * it must handle the reference count itself [i.e. copy == 0],
522 * or things won't be thread-safe). */
523 if (copy)
524 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
525
526 /* Add the externally cached session to the internal
527 * cache as well if and only if we are supposed to. */
528 if(!(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
529 /* The following should not return 1, otherwise,
530 * things are very strange */
531 SSL_CTX_add_session(s->session_ctx,ret);
532 }
533 }
534
535 if (ret == NULL)
536 goto err;
537
538 /* Now ret is non-NULL and we own one of its reference counts. */
539
540 if (ret->sid_ctx_length != s->sid_ctx_length
541 || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
542 {
543 /* We have the session requested by the client, but we don't
544 * want to use it in this context. */
545 goto err; /* treat like cache miss */
546 }
547
548 if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
549 {
550 /* We can't be sure if this session is being used out of
551 * context, which is especially important for SSL_VERIFY_PEER.
552 * The application should have used SSL[_CTX]_set_session_id_context.
553 *
554 * For this error case, we generate an error instead of treating
555 * the event like a cache miss (otherwise it would be easy for
556 * applications to effectively disable the session cache by
557 * accident without anyone noticing).
558 */
559
560 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
561 fatal = 1;
562 goto err;
563 }
564
565 if (ret->cipher == NULL)
566 {
567 unsigned char buf[5],*p;
568 unsigned long l;
569
570 p=buf;
571 l=ret->cipher_id;
572 l2n(l,p);
573 if ((ret->ssl_version>>8) >= SSL3_VERSION_MAJOR)
574 ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
575 else
576 ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
577 if (ret->cipher == NULL)
578 goto err;
579 }
580
581 if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
582 {
583 s->session_ctx->stats.sess_timeout++;
584 if (try_session_cache)
585 {
586 /* session was from the cache, so remove it */
587 SSL_CTX_remove_session(s->session_ctx,ret);
588 }
589 goto err;
590 }
591
592 s->session_ctx->stats.sess_hit++;
593
594 if (s->session != NULL)
595 SSL_SESSION_free(s->session);
596 s->session=ret;
597 s->verify_result = s->session->verify_result;
598 return 1;
599
600 err:
601 if (ret != NULL)
602 {
603 SSL_SESSION_free(ret);
604#ifndef OPENSSL_NO_TLSEXT
605 if (!try_session_cache)
606 {
607 /* The session was from a ticket, so we should
608 * issue a ticket for the new session */
609 s->tlsext_ticket_expected = 1;
610 }
611#endif
612 }
613 if (fatal)
614 return -1;
615 else
616 return 0;
617 }
618
619int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
620 {
621 int ret=0;
622 SSL_SESSION *s;
623
624 /* add just 1 reference count for the SSL_CTX's session cache
625 * even though it has two ways of access: each session is in a
626 * doubly linked list and an lhash */
627 CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
628 /* if session c is in already in cache, we take back the increment later */
629
630 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
631 if (!lh_SSL_SESSION_insert(ctx->sessions,&s,c)) {
632 return 0;
633 }
634
635 /* s != NULL iff we already had a session with the given PID.
636 * In this case, s == c should hold (then we did not really modify
637 * ctx->sessions), or we're in trouble. */
638 if (s != NULL && s != c)
639 {
640 /* We *are* in trouble ... */
641 SSL_SESSION_list_remove(ctx,s);
642 SSL_SESSION_free(s);
643 /* ... so pretend the other session did not exist in cache
644 * (we cannot handle two SSL_SESSION structures with identical
645 * session ID in the same cache, which could happen e.g. when
646 * two threads concurrently obtain the same session from an external
647 * cache) */
648 s = NULL;
649 }
650
651 /* Put at the head of the queue unless it is already in the cache */
652 if (s == NULL)
653 SSL_SESSION_list_add(ctx,c);
654
655 if (s != NULL)
656 {
657 /* existing cache entry -- decrement previously incremented reference
658 * count because it already takes into account the cache */
659
660 SSL_SESSION_free(s); /* s == c */
661 ret=0;
662 }
663 else
664 {
665 /* new cache entry -- remove old ones if cache has become too large */
666
667 ret=1;
668
669 if (SSL_CTX_sess_get_cache_size(ctx) > 0)
670 {
671 while (SSL_CTX_sess_number(ctx) >
672 SSL_CTX_sess_get_cache_size(ctx))
673 {
674 if (!remove_session_lock(ctx,
675 ctx->session_cache_tail, 0))
676 break;
677 else
678 ctx->stats.sess_cache_full++;
679 }
680 }
681 }
682 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
683 return(ret);
684 }
685
686int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
687{
688 return remove_session_lock(ctx, c, 1);
689}
690
691static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
692 {
693 SSL_SESSION *r;
694 int ret=0;
695
696 if ((c != NULL) && (c->session_id_length != 0))
697 {
698 if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
699 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
700 {
701 ret=1;
702 r=lh_SSL_SESSION_delete(ctx->sessions,c);
703 SSL_SESSION_list_remove(ctx,c);
704 }
705
706 if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
707
708 if (ret)
709 {
710 r->not_resumable=1;
711 if (ctx->remove_session_cb != NULL)
712 ctx->remove_session_cb(ctx,r);
713 SSL_SESSION_free(r);
714 }
715 }
716 else
717 ret=0;
718 return(ret);
719 }
720
721void SSL_SESSION_free(SSL_SESSION *ss)
722 {
723 int i;
724
725 if(ss == NULL)
726 return;
727
728 i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
729#ifdef REF_PRINT
730 REF_PRINT("SSL_SESSION",ss);
731#endif
732 if (i > 0) return;
733#ifdef REF_CHECK
734 if (i < 0)
735 {
736 fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
737 abort(); /* ok */
738 }
739#endif
740
741 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
742
743 OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
744 OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
745 OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
746 if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
747 if (ss->peer != NULL) X509_free(ss->peer);
748 if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
749#ifndef OPENSSL_NO_TLSEXT
750 if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
751 if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
752#ifndef OPENSSL_NO_EC
753 ss->tlsext_ecpointformatlist_length = 0;
754 if (ss->tlsext_ecpointformatlist != NULL) OPENSSL_free(ss->tlsext_ecpointformatlist);
755 ss->tlsext_ellipticcurvelist_length = 0;
756 if (ss->tlsext_ellipticcurvelist != NULL) OPENSSL_free(ss->tlsext_ellipticcurvelist);
757#endif /* OPENSSL_NO_EC */
758 if (ss->audit_proof != NULL) OPENSSL_free(ss->audit_proof);
759 ss->audit_proof_length = 0;
760#endif
761#ifndef OPENSSL_NO_PSK
762 if (ss->psk_identity_hint != NULL)
763 OPENSSL_free(ss->psk_identity_hint);
764 if (ss->psk_identity != NULL)
765 OPENSSL_free(ss->psk_identity);
766#endif
767 OPENSSL_cleanse(ss,sizeof(*ss));
768 OPENSSL_free(ss);
769 }
770
771int SSL_set_session(SSL *s, SSL_SESSION *session)
772 {
773 int ret=0;
774 const SSL_METHOD *meth;
775
776 if (session != NULL)
777 {
778 meth=s->ctx->method->get_ssl_method(session->ssl_version);
779 if (meth == NULL)
780 meth=s->method->get_ssl_method(session->ssl_version);
781 if (meth == NULL)
782 {
783 OPENSSL_PUT_ERROR(SSL, SSL_set_session, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
784 return(0);
785 }
786
787 if (meth != s->method)
788 {
789 if (!SSL_set_ssl_method(s,meth))
790 return(0);
791 }
792
793 /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
794 CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
795 if (s->session != NULL)
796 SSL_SESSION_free(s->session);
797 s->session=session;
798 s->verify_result = s->session->verify_result;
799 /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
800 ret=1;
801 }
802 else
803 {
804 if (s->session != NULL)
805 {
806 SSL_SESSION_free(s->session);
807 s->session=NULL;
808 }
809
810 meth=s->ctx->method;
811 if (meth != s->method)
812 {
813 if (!SSL_set_ssl_method(s,meth))
814 return(0);
815 }
816 ret=1;
817 }
818 return(ret);
819 }
820
821long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
822 {
823 if (s == NULL) return(0);
824 s->timeout=t;
825 return(1);
826 }
827
828long SSL_SESSION_get_timeout(const SSL_SESSION *s)
829 {
830 if (s == NULL) return(0);
831 return(s->timeout);
832 }
833
834long SSL_SESSION_get_time(const SSL_SESSION *s)
835 {
836 if (s == NULL) return(0);
837 return(s->time);
838 }
839
840long SSL_SESSION_set_time(SSL_SESSION *s, long t)
841 {
842 if (s == NULL) return(0);
843 s->time=t;
844 return(t);
845 }
846
847X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
848 {
849 return s->peer;
850 }
851
852int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
853 unsigned int sid_ctx_len)
854 {
855 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
856 {
857 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
858 return 0;
859 }
860 s->sid_ctx_length=sid_ctx_len;
861 memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
862
863 return 1;
864 }
865
866#ifndef OPENSSL_NO_TLSEXT
867unsigned char *SSL_SESSION_get_tlsext_authz_server_audit_proof(SSL_SESSION *s, size_t *proof_length)
868 {
869 if (s->audit_proof != NULL)
870 *proof_length = s->audit_proof_length;
871 return s->audit_proof;
872 }
873#endif
874
875long SSL_CTX_set_timeout(SSL_CTX *s, long t)
876 {
877 long l;
878 if (s == NULL) return(0);
879 l=s->session_timeout;
880 s->session_timeout=t;
881 return(l);
882 }
883
884long SSL_CTX_get_timeout(const SSL_CTX *s)
885 {
886 if (s == NULL) return(0);
887 return(s->session_timeout);
888 }
889
890#ifndef OPENSSL_NO_TLSEXT
891int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
892 STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
893 {
894 if (s == NULL) return(0);
895 s->tls_session_secret_cb = tls_session_secret_cb;
896 s->tls_session_secret_cb_arg = arg;
897 return(1);
898 }
899
900int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
901 void *arg)
902 {
903 if (s == NULL) return(0);
904 s->tls_session_ticket_ext_cb = cb;
905 s->tls_session_ticket_ext_cb_arg = arg;
906 return(1);
907 }
908
909int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
910 {
911 if (s->version >= TLS1_VERSION)
912 {
913 if (s->tlsext_session_ticket)
914 {
915 OPENSSL_free(s->tlsext_session_ticket);
916 s->tlsext_session_ticket = NULL;
917 }
918
919 s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
920 if (!s->tlsext_session_ticket)
921 {
922 OPENSSL_PUT_ERROR(SSL, SSL_set_session_ticket_ext, ERR_R_MALLOC_FAILURE);
923 return 0;
924 }
925
926 if (ext_data)
927 {
928 s->tlsext_session_ticket->length = ext_len;
929 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
930 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
931 }
932 else
933 {
934 s->tlsext_session_ticket->length = 0;
935 s->tlsext_session_ticket->data = NULL;
936 }
937
938 return 1;
939 }
940
941 return 0;
942 }
943#endif /* OPENSSL_NO_TLSEXT */
944
945typedef struct timeout_param_st
946 {
947 SSL_CTX *ctx;
948 long time;
949 LHASH_OF(SSL_SESSION) *cache;
950 } TIMEOUT_PARAM;
951
952static void timeout_doall_arg(SSL_SESSION *sess, void *void_param)
953 {
954 TIMEOUT_PARAM *param = void_param;
955
956 if ((param->time == 0) || (param->time > (sess->time+sess->timeout))) /* timeout */
957 {
958 /* The reason we don't call SSL_CTX_remove_session() is to
959 * save on locking overhead */
960 (void)lh_SSL_SESSION_delete(param->cache,sess);
961 SSL_SESSION_list_remove(param->ctx,sess);
962 sess->not_resumable=1;
963 if (param->ctx->remove_session_cb != NULL)
964 param->ctx->remove_session_cb(param->ctx,sess);
965 SSL_SESSION_free(sess);
966 }
967 }
968
969void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
970 {
971 TIMEOUT_PARAM tp;
972
973 tp.ctx=s;
974 tp.cache=s->sessions;
975 if (tp.cache == NULL) return;
976 tp.time=t;
977 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
978 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
979 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
980 }
981
982int ssl_clear_bad_session(SSL *s)
983 {
984 if ( (s->session != NULL) &&
985 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
986 !(SSL_in_init(s) || SSL_in_before(s)))
987 {
988 SSL_CTX_remove_session(s->ctx,s->session);
989 return(1);
990 }
991 else
992 return(0);
993 }
994
995/* locked by SSL_CTX in the calling function */
996static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
997 {
998 if ((s->next == NULL) || (s->prev == NULL)) return;
999
1000 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
1001 { /* last element in list */
1002 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
1003 { /* only one element in list */
1004 ctx->session_cache_head=NULL;
1005 ctx->session_cache_tail=NULL;
1006 }
1007 else
1008 {
1009 ctx->session_cache_tail=s->prev;
1010 s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
1011 }
1012 }
1013 else
1014 {
1015 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
1016 { /* first element in list */
1017 ctx->session_cache_head=s->next;
1018 s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1019 }
1020 else
1021 { /* middle of list */
1022 s->next->prev=s->prev;
1023 s->prev->next=s->next;
1024 }
1025 }
1026 s->prev=s->next=NULL;
1027 }
1028
1029static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1030 {
1031 if ((s->next != NULL) && (s->prev != NULL))
1032 SSL_SESSION_list_remove(ctx,s);
1033
1034 if (ctx->session_cache_head == NULL)
1035 {
1036 ctx->session_cache_head=s;
1037 ctx->session_cache_tail=s;
1038 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1039 s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
1040 }
1041 else
1042 {
1043 s->next=ctx->session_cache_head;
1044 s->next->prev=s;
1045 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1046 ctx->session_cache_head=s;
1047 }
1048 }
1049
1050void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1051 int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
1052 {
1053 ctx->new_session_cb=cb;
1054 }
1055
1056int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1057 {
1058 return ctx->new_session_cb;
1059 }
1060
1061void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1062 void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
1063 {
1064 ctx->remove_session_cb=cb;
1065 }
1066
1067void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
1068 {
1069 return ctx->remove_session_cb;
1070 }
1071
1072void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1073 SSL_SESSION *(*cb)(struct ssl_st *ssl,
1074 unsigned char *data,int len,int *copy))
1075 {
1076 ctx->get_session_cb=cb;
1077 }
1078
1079SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
1080 unsigned char *data,int len,int *copy)
1081 {
1082 return ctx->get_session_cb;
1083 }
1084
1085void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1086 void (*cb)(const SSL *ssl,int type,int val))
1087 {
1088 ctx->info_callback=cb;
1089 }
1090
1091void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
1092 {
1093 return ctx->info_callback;
1094 }
1095
1096void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1097 int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1098 {
1099 ctx->client_cert_cb=cb;
1100 }
1101
1102int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
1103 {
1104 return ctx->client_cert_cb;
1105 }
1106
1107void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1108 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1109 {
1110 ctx->app_gen_cookie_cb=cb;
1111 }
1112
1113void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1114 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
1115 {
1116 ctx->app_verify_cookie_cb=cb;
1117 }
1118
Adam Langley1258b6a2014-06-20 12:00:00 -07001119void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
1120 void (*cb)(SSL *ssl, EVP_PKEY **pkey))
1121 {
1122 ctx->channel_id_cb=cb;
1123 }
1124
1125void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL * ssl, EVP_PKEY **pkey)
1126 {
1127 return ctx->channel_id_cb;
1128 }
1129
Adam Langley95c29f32014-06-20 12:00:00 -07001130IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)