blob: 206f0b1abdff4d96a524270f39a0c3a2636d8c54 [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 *
430 * session_id: points at the session ID in the ClientHello. This code will
431 * read past the end of this in order to parse out the session ticket
432 * extension, if any.
433 * len: the length of the session ID.
434 * limit: a pointer to the first byte after the ClientHello.
435 *
436 * Returns:
437 * -1: error
438 * 0: a session may have been found.
439 *
440 * Side effects:
441 * - If a session is found then s->session is pointed at it (after freeing an
442 * existing session if need be) and s->verify_result is set from the session.
443 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
444 * if the server should issue a new session ticket (to 0 otherwise).
445 */
446int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
447 const unsigned char *limit)
448 {
449 /* This is used only by servers. */
450
451 SSL_SESSION *ret=NULL;
452 int fatal = 0;
453 int try_session_cache = 1;
454#ifndef OPENSSL_NO_TLSEXT
455 int r;
456#endif
457
458 if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
459 goto err;
460
461 if (len == 0)
462 try_session_cache = 0;
463
464#ifndef OPENSSL_NO_TLSEXT
465 r = tls1_process_ticket(s, session_id, len, limit, &ret); /* sets s->tlsext_ticket_expected */
466 switch (r)
467 {
468 case -1: /* Error during processing */
469 fatal = 1;
470 goto err;
471 case 0: /* No ticket found */
472 case 1: /* Zero length ticket found */
473 break; /* Ok to carry on processing session id. */
474 case 2: /* Ticket found but not decrypted. */
475 case 3: /* Ticket decrypted, *ret has been set. */
476 try_session_cache = 0;
477 break;
478 default:
479 abort();
480 }
481#endif
482
483 if (try_session_cache &&
484 ret == NULL &&
485 !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
486 {
487 SSL_SESSION data;
488 data.ssl_version=s->version;
489 data.session_id_length=len;
490 if (len == 0)
491 return 0;
492 memcpy(data.session_id,session_id,len);
493 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
494 ret=lh_SSL_SESSION_retrieve(s->session_ctx->sessions,&data);
495 if (ret != NULL)
496 {
497 /* don't allow other threads to steal it: */
498 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
499 }
500 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
501 if (ret == NULL)
502 s->session_ctx->stats.sess_miss++;
503 }
504
505 if (try_session_cache &&
506 ret == NULL &&
507 s->session_ctx->get_session_cb != NULL)
508 {
509 int copy=1;
510
511 if ((ret=s->session_ctx->get_session_cb(s,session_id,len,&copy)))
512 {
Adam Langleyb2ce0582014-06-20 12:00:00 -0700513 if (ret == SSL_magic_pending_session_ptr())
514 {
515 /* This is a magic value which indicates that
516 * the callback needs to unwind the stack and
517 * figure out the session asynchronously. */
518 return PENDING_SESSION;
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520 s->session_ctx->stats.sess_cb_hit++;
521
522 /* Increment reference count now if the session callback
523 * asks us to do so (note that if the session structures
524 * returned by the callback are shared between threads,
525 * it must handle the reference count itself [i.e. copy == 0],
526 * or things won't be thread-safe). */
527 if (copy)
528 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
529
530 /* Add the externally cached session to the internal
531 * cache as well if and only if we are supposed to. */
532 if(!(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
533 /* The following should not return 1, otherwise,
534 * things are very strange */
535 SSL_CTX_add_session(s->session_ctx,ret);
536 }
537 }
538
539 if (ret == NULL)
540 goto err;
541
542 /* Now ret is non-NULL and we own one of its reference counts. */
543
544 if (ret->sid_ctx_length != s->sid_ctx_length
545 || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
546 {
547 /* We have the session requested by the client, but we don't
548 * want to use it in this context. */
549 goto err; /* treat like cache miss */
550 }
551
552 if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
553 {
554 /* We can't be sure if this session is being used out of
555 * context, which is especially important for SSL_VERIFY_PEER.
556 * The application should have used SSL[_CTX]_set_session_id_context.
557 *
558 * For this error case, we generate an error instead of treating
559 * the event like a cache miss (otherwise it would be easy for
560 * applications to effectively disable the session cache by
561 * accident without anyone noticing).
562 */
563
564 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
565 fatal = 1;
566 goto err;
567 }
568
569 if (ret->cipher == NULL)
570 {
571 unsigned char buf[5],*p;
572 unsigned long l;
573
574 p=buf;
575 l=ret->cipher_id;
576 l2n(l,p);
577 if ((ret->ssl_version>>8) >= SSL3_VERSION_MAJOR)
578 ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
579 else
580 ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
581 if (ret->cipher == NULL)
582 goto err;
583 }
584
585 if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
586 {
587 s->session_ctx->stats.sess_timeout++;
588 if (try_session_cache)
589 {
590 /* session was from the cache, so remove it */
591 SSL_CTX_remove_session(s->session_ctx,ret);
592 }
593 goto err;
594 }
595
596 s->session_ctx->stats.sess_hit++;
597
598 if (s->session != NULL)
599 SSL_SESSION_free(s->session);
600 s->session=ret;
601 s->verify_result = s->session->verify_result;
602 return 1;
603
604 err:
605 if (ret != NULL)
606 {
607 SSL_SESSION_free(ret);
608#ifndef OPENSSL_NO_TLSEXT
609 if (!try_session_cache)
610 {
611 /* The session was from a ticket, so we should
612 * issue a ticket for the new session */
613 s->tlsext_ticket_expected = 1;
614 }
615#endif
616 }
617 if (fatal)
618 return -1;
619 else
620 return 0;
621 }
622
623int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
624 {
625 int ret=0;
626 SSL_SESSION *s;
627
628 /* add just 1 reference count for the SSL_CTX's session cache
629 * even though it has two ways of access: each session is in a
630 * doubly linked list and an lhash */
631 CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
632 /* if session c is in already in cache, we take back the increment later */
633
634 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
635 if (!lh_SSL_SESSION_insert(ctx->sessions,&s,c)) {
636 return 0;
637 }
638
639 /* s != NULL iff we already had a session with the given PID.
640 * In this case, s == c should hold (then we did not really modify
641 * ctx->sessions), or we're in trouble. */
642 if (s != NULL && s != c)
643 {
644 /* We *are* in trouble ... */
645 SSL_SESSION_list_remove(ctx,s);
646 SSL_SESSION_free(s);
647 /* ... so pretend the other session did not exist in cache
648 * (we cannot handle two SSL_SESSION structures with identical
649 * session ID in the same cache, which could happen e.g. when
650 * two threads concurrently obtain the same session from an external
651 * cache) */
652 s = NULL;
653 }
654
655 /* Put at the head of the queue unless it is already in the cache */
656 if (s == NULL)
657 SSL_SESSION_list_add(ctx,c);
658
659 if (s != NULL)
660 {
661 /* existing cache entry -- decrement previously incremented reference
662 * count because it already takes into account the cache */
663
664 SSL_SESSION_free(s); /* s == c */
665 ret=0;
666 }
667 else
668 {
669 /* new cache entry -- remove old ones if cache has become too large */
670
671 ret=1;
672
673 if (SSL_CTX_sess_get_cache_size(ctx) > 0)
674 {
675 while (SSL_CTX_sess_number(ctx) >
676 SSL_CTX_sess_get_cache_size(ctx))
677 {
678 if (!remove_session_lock(ctx,
679 ctx->session_cache_tail, 0))
680 break;
681 else
682 ctx->stats.sess_cache_full++;
683 }
684 }
685 }
686 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
687 return(ret);
688 }
689
690int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
691{
692 return remove_session_lock(ctx, c, 1);
693}
694
695static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
696 {
697 SSL_SESSION *r;
698 int ret=0;
699
700 if ((c != NULL) && (c->session_id_length != 0))
701 {
702 if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
703 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
704 {
705 ret=1;
706 r=lh_SSL_SESSION_delete(ctx->sessions,c);
707 SSL_SESSION_list_remove(ctx,c);
708 }
709
710 if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
711
712 if (ret)
713 {
714 r->not_resumable=1;
715 if (ctx->remove_session_cb != NULL)
716 ctx->remove_session_cb(ctx,r);
717 SSL_SESSION_free(r);
718 }
719 }
720 else
721 ret=0;
722 return(ret);
723 }
724
725void SSL_SESSION_free(SSL_SESSION *ss)
726 {
727 int i;
728
729 if(ss == NULL)
730 return;
731
732 i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
733#ifdef REF_PRINT
734 REF_PRINT("SSL_SESSION",ss);
735#endif
736 if (i > 0) return;
737#ifdef REF_CHECK
738 if (i < 0)
739 {
740 fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
741 abort(); /* ok */
742 }
743#endif
744
745 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
746
747 OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
748 OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
749 OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
750 if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
751 if (ss->peer != NULL) X509_free(ss->peer);
752 if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
753#ifndef OPENSSL_NO_TLSEXT
754 if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
755 if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
756#ifndef OPENSSL_NO_EC
757 ss->tlsext_ecpointformatlist_length = 0;
758 if (ss->tlsext_ecpointformatlist != NULL) OPENSSL_free(ss->tlsext_ecpointformatlist);
759 ss->tlsext_ellipticcurvelist_length = 0;
760 if (ss->tlsext_ellipticcurvelist != NULL) OPENSSL_free(ss->tlsext_ellipticcurvelist);
761#endif /* OPENSSL_NO_EC */
762 if (ss->audit_proof != NULL) OPENSSL_free(ss->audit_proof);
763 ss->audit_proof_length = 0;
764#endif
765#ifndef OPENSSL_NO_PSK
766 if (ss->psk_identity_hint != NULL)
767 OPENSSL_free(ss->psk_identity_hint);
768 if (ss->psk_identity != NULL)
769 OPENSSL_free(ss->psk_identity);
770#endif
771 OPENSSL_cleanse(ss,sizeof(*ss));
772 OPENSSL_free(ss);
773 }
774
775int SSL_set_session(SSL *s, SSL_SESSION *session)
776 {
777 int ret=0;
778 const SSL_METHOD *meth;
779
780 if (session != NULL)
781 {
782 meth=s->ctx->method->get_ssl_method(session->ssl_version);
783 if (meth == NULL)
784 meth=s->method->get_ssl_method(session->ssl_version);
785 if (meth == NULL)
786 {
787 OPENSSL_PUT_ERROR(SSL, SSL_set_session, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
788 return(0);
789 }
790
791 if (meth != s->method)
792 {
793 if (!SSL_set_ssl_method(s,meth))
794 return(0);
795 }
796
797 /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
798 CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
799 if (s->session != NULL)
800 SSL_SESSION_free(s->session);
801 s->session=session;
802 s->verify_result = s->session->verify_result;
803 /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
804 ret=1;
805 }
806 else
807 {
808 if (s->session != NULL)
809 {
810 SSL_SESSION_free(s->session);
811 s->session=NULL;
812 }
813
814 meth=s->ctx->method;
815 if (meth != s->method)
816 {
817 if (!SSL_set_ssl_method(s,meth))
818 return(0);
819 }
820 ret=1;
821 }
822 return(ret);
823 }
824
825long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
826 {
827 if (s == NULL) return(0);
828 s->timeout=t;
829 return(1);
830 }
831
832long SSL_SESSION_get_timeout(const SSL_SESSION *s)
833 {
834 if (s == NULL) return(0);
835 return(s->timeout);
836 }
837
838long SSL_SESSION_get_time(const SSL_SESSION *s)
839 {
840 if (s == NULL) return(0);
841 return(s->time);
842 }
843
844long SSL_SESSION_set_time(SSL_SESSION *s, long t)
845 {
846 if (s == NULL) return(0);
847 s->time=t;
848 return(t);
849 }
850
851X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
852 {
853 return s->peer;
854 }
855
856int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
857 unsigned int sid_ctx_len)
858 {
859 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
860 {
861 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
862 return 0;
863 }
864 s->sid_ctx_length=sid_ctx_len;
865 memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
866
867 return 1;
868 }
869
870#ifndef OPENSSL_NO_TLSEXT
871unsigned char *SSL_SESSION_get_tlsext_authz_server_audit_proof(SSL_SESSION *s, size_t *proof_length)
872 {
873 if (s->audit_proof != NULL)
874 *proof_length = s->audit_proof_length;
875 return s->audit_proof;
876 }
877#endif
878
879long SSL_CTX_set_timeout(SSL_CTX *s, long t)
880 {
881 long l;
882 if (s == NULL) return(0);
883 l=s->session_timeout;
884 s->session_timeout=t;
885 return(l);
886 }
887
888long SSL_CTX_get_timeout(const SSL_CTX *s)
889 {
890 if (s == NULL) return(0);
891 return(s->session_timeout);
892 }
893
894#ifndef OPENSSL_NO_TLSEXT
895int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
896 STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
897 {
898 if (s == NULL) return(0);
899 s->tls_session_secret_cb = tls_session_secret_cb;
900 s->tls_session_secret_cb_arg = arg;
901 return(1);
902 }
903
904int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
905 void *arg)
906 {
907 if (s == NULL) return(0);
908 s->tls_session_ticket_ext_cb = cb;
909 s->tls_session_ticket_ext_cb_arg = arg;
910 return(1);
911 }
912
913int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
914 {
915 if (s->version >= TLS1_VERSION)
916 {
917 if (s->tlsext_session_ticket)
918 {
919 OPENSSL_free(s->tlsext_session_ticket);
920 s->tlsext_session_ticket = NULL;
921 }
922
923 s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
924 if (!s->tlsext_session_ticket)
925 {
926 OPENSSL_PUT_ERROR(SSL, SSL_set_session_ticket_ext, ERR_R_MALLOC_FAILURE);
927 return 0;
928 }
929
930 if (ext_data)
931 {
932 s->tlsext_session_ticket->length = ext_len;
933 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
934 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
935 }
936 else
937 {
938 s->tlsext_session_ticket->length = 0;
939 s->tlsext_session_ticket->data = NULL;
940 }
941
942 return 1;
943 }
944
945 return 0;
946 }
947#endif /* OPENSSL_NO_TLSEXT */
948
949typedef struct timeout_param_st
950 {
951 SSL_CTX *ctx;
952 long time;
953 LHASH_OF(SSL_SESSION) *cache;
954 } TIMEOUT_PARAM;
955
956static void timeout_doall_arg(SSL_SESSION *sess, void *void_param)
957 {
958 TIMEOUT_PARAM *param = void_param;
959
960 if ((param->time == 0) || (param->time > (sess->time+sess->timeout))) /* timeout */
961 {
962 /* The reason we don't call SSL_CTX_remove_session() is to
963 * save on locking overhead */
964 (void)lh_SSL_SESSION_delete(param->cache,sess);
965 SSL_SESSION_list_remove(param->ctx,sess);
966 sess->not_resumable=1;
967 if (param->ctx->remove_session_cb != NULL)
968 param->ctx->remove_session_cb(param->ctx,sess);
969 SSL_SESSION_free(sess);
970 }
971 }
972
973void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
974 {
975 TIMEOUT_PARAM tp;
976
977 tp.ctx=s;
978 tp.cache=s->sessions;
979 if (tp.cache == NULL) return;
980 tp.time=t;
981 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
982 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
983 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
984 }
985
986int ssl_clear_bad_session(SSL *s)
987 {
988 if ( (s->session != NULL) &&
989 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
990 !(SSL_in_init(s) || SSL_in_before(s)))
991 {
992 SSL_CTX_remove_session(s->ctx,s->session);
993 return(1);
994 }
995 else
996 return(0);
997 }
998
999/* locked by SSL_CTX in the calling function */
1000static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1001 {
1002 if ((s->next == NULL) || (s->prev == NULL)) return;
1003
1004 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
1005 { /* last element in list */
1006 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
1007 { /* only one element in list */
1008 ctx->session_cache_head=NULL;
1009 ctx->session_cache_tail=NULL;
1010 }
1011 else
1012 {
1013 ctx->session_cache_tail=s->prev;
1014 s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
1015 }
1016 }
1017 else
1018 {
1019 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
1020 { /* first element in list */
1021 ctx->session_cache_head=s->next;
1022 s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1023 }
1024 else
1025 { /* middle of list */
1026 s->next->prev=s->prev;
1027 s->prev->next=s->next;
1028 }
1029 }
1030 s->prev=s->next=NULL;
1031 }
1032
1033static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1034 {
1035 if ((s->next != NULL) && (s->prev != NULL))
1036 SSL_SESSION_list_remove(ctx,s);
1037
1038 if (ctx->session_cache_head == NULL)
1039 {
1040 ctx->session_cache_head=s;
1041 ctx->session_cache_tail=s;
1042 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1043 s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
1044 }
1045 else
1046 {
1047 s->next=ctx->session_cache_head;
1048 s->next->prev=s;
1049 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1050 ctx->session_cache_head=s;
1051 }
1052 }
1053
1054void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1055 int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
1056 {
1057 ctx->new_session_cb=cb;
1058 }
1059
1060int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1061 {
1062 return ctx->new_session_cb;
1063 }
1064
1065void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1066 void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
1067 {
1068 ctx->remove_session_cb=cb;
1069 }
1070
1071void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
1072 {
1073 return ctx->remove_session_cb;
1074 }
1075
1076void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1077 SSL_SESSION *(*cb)(struct ssl_st *ssl,
1078 unsigned char *data,int len,int *copy))
1079 {
1080 ctx->get_session_cb=cb;
1081 }
1082
1083SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
1084 unsigned char *data,int len,int *copy)
1085 {
1086 return ctx->get_session_cb;
1087 }
1088
1089void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1090 void (*cb)(const SSL *ssl,int type,int val))
1091 {
1092 ctx->info_callback=cb;
1093 }
1094
1095void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
1096 {
1097 return ctx->info_callback;
1098 }
1099
1100void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1101 int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1102 {
1103 ctx->client_cert_cb=cb;
1104 }
1105
1106int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
1107 {
1108 return ctx->client_cert_cb;
1109 }
1110
1111void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1112 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1113 {
1114 ctx->app_gen_cookie_cb=cb;
1115 }
1116
1117void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1118 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
1119 {
1120 ctx->app_verify_cookie_cb=cb;
1121 }
1122
Adam Langley1258b6a2014-06-20 12:00:00 -07001123void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
1124 void (*cb)(SSL *ssl, EVP_PKEY **pkey))
1125 {
1126 ctx->channel_id_cb=cb;
1127 }
1128
1129void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL * ssl, EVP_PKEY **pkey)
1130 {
1131 return ctx->channel_id_cb;
1132 }
1133
Adam Langley95c29f32014-06-20 12:00:00 -07001134IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)