blob: 75e211018c2afd5731065119c23f6fd52fb43b0c [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;
Adam Langley95c29f32014-06-20 12:00:00 -0700216 ss->tlsext_hostname = NULL;
217#ifndef OPENSSL_NO_EC
218 ss->tlsext_ecpointformatlist_length = 0;
219 ss->tlsext_ecpointformatlist = NULL;
220 ss->tlsext_ellipticcurvelist_length = 0;
221 ss->tlsext_ellipticcurvelist = NULL;
222#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700223 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700224 ss->psk_identity_hint=NULL;
225 ss->psk_identity=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700226 return(ss);
227 }
228
229const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
230 {
231 if(len)
232 *len = s->session_id_length;
233 return s->session_id;
234 }
235
Adam Langley95c29f32014-06-20 12:00:00 -0700236/* 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
Adam Langley8eaaa862014-08-11 17:18:25 -0700277 if (s->mode & SSL_MODE_NO_SESSION_CREATION)
278 {
279 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SESSION_MAY_NOT_BE_CREATED);
280 return 0;
281 }
282
Adam Langley95c29f32014-06-20 12:00:00 -0700283 if ((ss=SSL_SESSION_new()) == NULL) return(0);
284
285 /* If the context has a default timeout, use it */
286 if (s->session_ctx->session_timeout == 0)
287 ss->timeout=SSL_get_default_timeout(s);
288 else
289 ss->timeout=s->session_ctx->session_timeout;
290
291 if (s->session != NULL)
292 {
293 SSL_SESSION_free(s->session);
294 s->session=NULL;
295 }
296
297 if (session)
298 {
299 if (s->version == SSL2_VERSION)
300 {
301 ss->ssl_version=SSL2_VERSION;
302 ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
303 }
304 else if (s->version == SSL3_VERSION)
305 {
306 ss->ssl_version=SSL3_VERSION;
307 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
308 }
309 else if (s->version == TLS1_VERSION)
310 {
311 ss->ssl_version=TLS1_VERSION;
312 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
313 }
314 else if (s->version == TLS1_1_VERSION)
315 {
316 ss->ssl_version=TLS1_1_VERSION;
317 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
318 }
319 else if (s->version == TLS1_2_VERSION)
320 {
321 ss->ssl_version=TLS1_2_VERSION;
322 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
323 }
324 else if (s->version == DTLS1_BAD_VER)
325 {
326 ss->ssl_version=DTLS1_BAD_VER;
327 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
328 }
329 else if (s->version == DTLS1_VERSION)
330 {
331 ss->ssl_version=DTLS1_VERSION;
332 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
333 }
334 else if (s->version == DTLS1_2_VERSION)
335 {
336 ss->ssl_version=DTLS1_2_VERSION;
337 ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
338 }
339 else
340 {
341 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_UNSUPPORTED_SSL_VERSION);
342 SSL_SESSION_free(ss);
343 return(0);
344 }
Adam Langley95c29f32014-06-20 12:00:00 -0700345 /* If RFC4507 ticket use empty session ID */
346 if (s->tlsext_ticket_expected)
347 {
348 ss->session_id_length = 0;
349 goto sess_id_done;
350 }
Adam Langley95c29f32014-06-20 12:00:00 -0700351 /* Choose which callback will set the session ID */
352 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
353 if(s->generate_session_id)
354 cb = s->generate_session_id;
355 else if(s->session_ctx->generate_session_id)
356 cb = s->session_ctx->generate_session_id;
357 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
358 /* Choose a session ID */
359 tmp = ss->session_id_length;
360 if(!cb(s, ss->session_id, &tmp))
361 {
362 /* The callback failed */
363 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
364 SSL_SESSION_free(ss);
365 return(0);
366 }
367 /* Don't allow the callback to set the session length to zero.
368 * nor set it higher than it was. */
369 if(!tmp || (tmp > ss->session_id_length))
370 {
371 /* The callback set an illegal length */
372 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
373 SSL_SESSION_free(ss);
374 return(0);
375 }
376 /* If the session length was shrunk and we're SSLv2, pad it */
377 if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
378 memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
379 else
380 ss->session_id_length = tmp;
381 /* Finally, check for a conflict */
382 if(SSL_has_matching_session_id(s, ss->session_id,
383 ss->session_id_length))
384 {
385 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CONFLICT);
386 SSL_SESSION_free(ss);
387 return(0);
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389 sess_id_done:
390 if (s->tlsext_hostname) {
391 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
392 if (ss->tlsext_hostname == NULL) {
393 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
394 SSL_SESSION_free(ss);
395 return 0;
396 }
397 }
Adam Langley0289c732014-06-20 12:00:00 -0700398 if (s->psk_identity_hint)
399 {
400 ss->psk_identity_hint = BUF_strdup(s->psk_identity_hint);
401 if (ss->psk_identity_hint == NULL)
402 {
403 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_MALLOC_FAILURE);
404 SSL_SESSION_free(ss);
405 return 0;
406 }
407 }
Adam Langley95c29f32014-06-20 12:00:00 -0700408 }
409 else
410 {
411 ss->session_id_length=0;
412 }
413
414 if (s->sid_ctx_length > sizeof ss->sid_ctx)
415 {
416 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
417 SSL_SESSION_free(ss);
418 return 0;
419 }
420 memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
421 ss->sid_ctx_length=s->sid_ctx_length;
422 s->session=ss;
423 ss->ssl_version=s->version;
424 ss->verify_result = X509_V_OK;
425
426 return(1);
427 }
428
429/* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
430 * connection. It is only called by servers.
431 *
Adam Langleydc9b1412014-06-20 12:00:00 -0700432 * ctx: contains the early callback context, which is the result of a
433 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -0700434 *
435 * Returns:
436 * -1: error
437 * 0: a session may have been found.
438 *
439 * Side effects:
440 * - If a session is found then s->session is pointed at it (after freeing an
441 * existing session if need be) and s->verify_result is set from the session.
442 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
443 * if the server should issue a new session ticket (to 0 otherwise).
444 */
Adam Langleydc9b1412014-06-20 12:00:00 -0700445int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx)
Adam Langley95c29f32014-06-20 12:00:00 -0700446 {
447 /* This is used only by servers. */
448
449 SSL_SESSION *ret=NULL;
450 int fatal = 0;
451 int try_session_cache = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700452 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700453
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
Adam Langleydc9b1412014-06-20 12:00:00 -0700460 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
Adam Langley95c29f32014-06-20 12:00:00 -0700461 switch (r)
462 {
463 case -1: /* Error during processing */
464 fatal = 1;
465 goto err;
466 case 0: /* No ticket found */
467 case 1: /* Zero length ticket found */
468 break; /* Ok to carry on processing session id. */
469 case 2: /* Ticket found but not decrypted. */
470 case 3: /* Ticket decrypted, *ret has been set. */
471 try_session_cache = 0;
472 break;
473 default:
474 abort();
475 }
Adam Langley95c29f32014-06-20 12:00:00 -0700476
477 if (try_session_cache &&
478 ret == NULL &&
479 !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
480 {
481 SSL_SESSION data;
482 data.ssl_version=s->version;
Adam Langleydc9b1412014-06-20 12:00:00 -0700483 data.session_id_length=ctx->session_id_len;
484 if (ctx->session_id_len == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700485 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700486 memcpy(data.session_id,ctx->session_id,ctx->session_id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700487 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
488 ret=lh_SSL_SESSION_retrieve(s->session_ctx->sessions,&data);
489 if (ret != NULL)
490 {
491 /* don't allow other threads to steal it: */
492 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
493 }
494 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
495 if (ret == NULL)
496 s->session_ctx->stats.sess_miss++;
497 }
498
499 if (try_session_cache &&
500 ret == NULL &&
501 s->session_ctx->get_session_cb != NULL)
502 {
503 int copy=1;
504
Adam Langleydc9b1412014-06-20 12:00:00 -0700505 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 -0700506 {
Adam Langleyb2ce0582014-06-20 12:00:00 -0700507 if (ret == SSL_magic_pending_session_ptr())
508 {
509 /* This is a magic value which indicates that
510 * the callback needs to unwind the stack and
511 * figure out the session asynchronously. */
512 return PENDING_SESSION;
513 }
Adam Langley95c29f32014-06-20 12:00:00 -0700514 s->session_ctx->stats.sess_cb_hit++;
515
516 /* Increment reference count now if the session callback
517 * asks us to do so (note that if the session structures
518 * returned by the callback are shared between threads,
519 * it must handle the reference count itself [i.e. copy == 0],
520 * or things won't be thread-safe). */
521 if (copy)
522 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
523
524 /* Add the externally cached session to the internal
525 * cache as well if and only if we are supposed to. */
526 if(!(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
527 /* The following should not return 1, otherwise,
528 * things are very strange */
529 SSL_CTX_add_session(s->session_ctx,ret);
530 }
531 }
532
533 if (ret == NULL)
534 goto err;
535
536 /* Now ret is non-NULL and we own one of its reference counts. */
537
538 if (ret->sid_ctx_length != s->sid_ctx_length
539 || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
540 {
541 /* We have the session requested by the client, but we don't
542 * want to use it in this context. */
543 goto err; /* treat like cache miss */
544 }
545
546 if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
547 {
548 /* We can't be sure if this session is being used out of
549 * context, which is especially important for SSL_VERIFY_PEER.
550 * The application should have used SSL[_CTX]_set_session_id_context.
551 *
552 * For this error case, we generate an error instead of treating
553 * the event like a cache miss (otherwise it would be easy for
554 * applications to effectively disable the session cache by
555 * accident without anyone noticing).
556 */
557
558 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
559 fatal = 1;
560 goto err;
561 }
562
563 if (ret->cipher == NULL)
564 {
David Benjamin39482a12014-07-20 13:30:15 -0400565 /* The cipher id has a leading 0x03 to be removed (and then put
566 * back for the binary search) as a remnant of SSLv2 support. */
567 ret->cipher = ssl3_get_cipher_by_value(ret->cipher_id & 0xffff);
Adam Langley95c29f32014-06-20 12:00:00 -0700568 if (ret->cipher == NULL)
569 goto err;
570 }
571
572 if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
573 {
574 s->session_ctx->stats.sess_timeout++;
575 if (try_session_cache)
576 {
577 /* session was from the cache, so remove it */
578 SSL_CTX_remove_session(s->session_ctx,ret);
579 }
580 goto err;
581 }
582
583 s->session_ctx->stats.sess_hit++;
584
585 if (s->session != NULL)
586 SSL_SESSION_free(s->session);
587 s->session=ret;
588 s->verify_result = s->session->verify_result;
589 return 1;
590
591 err:
592 if (ret != NULL)
593 {
594 SSL_SESSION_free(ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700595 if (!try_session_cache)
596 {
597 /* The session was from a ticket, so we should
598 * issue a ticket for the new session */
599 s->tlsext_ticket_expected = 1;
600 }
Adam Langley95c29f32014-06-20 12:00:00 -0700601 }
602 if (fatal)
603 return -1;
604 else
605 return 0;
606 }
607
608int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
609 {
610 int ret=0;
611 SSL_SESSION *s;
612
613 /* add just 1 reference count for the SSL_CTX's session cache
614 * even though it has two ways of access: each session is in a
615 * doubly linked list and an lhash */
616 CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
617 /* if session c is in already in cache, we take back the increment later */
618
619 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
620 if (!lh_SSL_SESSION_insert(ctx->sessions,&s,c)) {
621 return 0;
622 }
623
624 /* s != NULL iff we already had a session with the given PID.
625 * In this case, s == c should hold (then we did not really modify
626 * ctx->sessions), or we're in trouble. */
627 if (s != NULL && s != c)
628 {
629 /* We *are* in trouble ... */
630 SSL_SESSION_list_remove(ctx,s);
631 SSL_SESSION_free(s);
632 /* ... so pretend the other session did not exist in cache
633 * (we cannot handle two SSL_SESSION structures with identical
634 * session ID in the same cache, which could happen e.g. when
635 * two threads concurrently obtain the same session from an external
636 * cache) */
637 s = NULL;
638 }
639
640 /* Put at the head of the queue unless it is already in the cache */
641 if (s == NULL)
642 SSL_SESSION_list_add(ctx,c);
643
644 if (s != NULL)
645 {
646 /* existing cache entry -- decrement previously incremented reference
647 * count because it already takes into account the cache */
648
649 SSL_SESSION_free(s); /* s == c */
650 ret=0;
651 }
652 else
653 {
654 /* new cache entry -- remove old ones if cache has become too large */
655
656 ret=1;
657
658 if (SSL_CTX_sess_get_cache_size(ctx) > 0)
659 {
660 while (SSL_CTX_sess_number(ctx) >
661 SSL_CTX_sess_get_cache_size(ctx))
662 {
663 if (!remove_session_lock(ctx,
664 ctx->session_cache_tail, 0))
665 break;
666 else
667 ctx->stats.sess_cache_full++;
668 }
669 }
670 }
671 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
672 return(ret);
673 }
674
675int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
676{
677 return remove_session_lock(ctx, c, 1);
678}
679
680static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
681 {
682 SSL_SESSION *r;
683 int ret=0;
684
685 if ((c != NULL) && (c->session_id_length != 0))
686 {
687 if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
688 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
689 {
690 ret=1;
691 r=lh_SSL_SESSION_delete(ctx->sessions,c);
692 SSL_SESSION_list_remove(ctx,c);
693 }
694
695 if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
696
697 if (ret)
698 {
699 r->not_resumable=1;
700 if (ctx->remove_session_cb != NULL)
701 ctx->remove_session_cb(ctx,r);
702 SSL_SESSION_free(r);
703 }
704 }
705 else
706 ret=0;
707 return(ret);
708 }
709
710void SSL_SESSION_free(SSL_SESSION *ss)
711 {
712 int i;
713
714 if(ss == NULL)
715 return;
716
717 i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
718#ifdef REF_PRINT
719 REF_PRINT("SSL_SESSION",ss);
720#endif
721 if (i > 0) return;
722#ifdef REF_CHECK
723 if (i < 0)
724 {
725 fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
726 abort(); /* ok */
727 }
728#endif
729
730 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
731
732 OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
733 OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
734 OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
735 if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
736 if (ss->peer != NULL) X509_free(ss->peer);
737 if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -0700738 if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
739 if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
740#ifndef OPENSSL_NO_EC
741 ss->tlsext_ecpointformatlist_length = 0;
742 if (ss->tlsext_ecpointformatlist != NULL) OPENSSL_free(ss->tlsext_ecpointformatlist);
743 ss->tlsext_ellipticcurvelist_length = 0;
744 if (ss->tlsext_ellipticcurvelist != NULL) OPENSSL_free(ss->tlsext_ellipticcurvelist);
745#endif /* OPENSSL_NO_EC */
Adam Langley95c29f32014-06-20 12:00:00 -0700746 if (ss->psk_identity_hint != NULL)
747 OPENSSL_free(ss->psk_identity_hint);
748 if (ss->psk_identity != NULL)
749 OPENSSL_free(ss->psk_identity);
Adam Langley95c29f32014-06-20 12:00:00 -0700750 OPENSSL_cleanse(ss,sizeof(*ss));
751 OPENSSL_free(ss);
752 }
753
754int SSL_set_session(SSL *s, SSL_SESSION *session)
755 {
756 int ret=0;
757 const SSL_METHOD *meth;
758
759 if (session != NULL)
760 {
761 meth=s->ctx->method->get_ssl_method(session->ssl_version);
762 if (meth == NULL)
763 meth=s->method->get_ssl_method(session->ssl_version);
764 if (meth == NULL)
765 {
766 OPENSSL_PUT_ERROR(SSL, SSL_set_session, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
767 return(0);
768 }
769
770 if (meth != s->method)
771 {
772 if (!SSL_set_ssl_method(s,meth))
773 return(0);
774 }
775
776 /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
777 CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
778 if (s->session != NULL)
779 SSL_SESSION_free(s->session);
780 s->session=session;
781 s->verify_result = s->session->verify_result;
782 /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
783 ret=1;
784 }
785 else
786 {
787 if (s->session != NULL)
788 {
789 SSL_SESSION_free(s->session);
790 s->session=NULL;
791 }
792
793 meth=s->ctx->method;
794 if (meth != s->method)
795 {
796 if (!SSL_set_ssl_method(s,meth))
797 return(0);
798 }
799 ret=1;
800 }
801 return(ret);
802 }
803
804long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
805 {
806 if (s == NULL) return(0);
807 s->timeout=t;
808 return(1);
809 }
810
811long SSL_SESSION_get_timeout(const SSL_SESSION *s)
812 {
813 if (s == NULL) return(0);
814 return(s->timeout);
815 }
816
817long SSL_SESSION_get_time(const SSL_SESSION *s)
818 {
819 if (s == NULL) return(0);
820 return(s->time);
821 }
822
823long SSL_SESSION_set_time(SSL_SESSION *s, long t)
824 {
825 if (s == NULL) return(0);
826 s->time=t;
827 return(t);
828 }
829
830X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
831 {
832 return s->peer;
833 }
834
835int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
836 unsigned int sid_ctx_len)
837 {
838 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
839 {
840 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
841 return 0;
842 }
843 s->sid_ctx_length=sid_ctx_len;
844 memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
845
846 return 1;
847 }
848
Adam Langley95c29f32014-06-20 12:00:00 -0700849long SSL_CTX_set_timeout(SSL_CTX *s, long t)
850 {
851 long l;
852 if (s == NULL) return(0);
853 l=s->session_timeout;
854 s->session_timeout=t;
855 return(l);
856 }
857
858long SSL_CTX_get_timeout(const SSL_CTX *s)
859 {
860 if (s == NULL) return(0);
861 return(s->session_timeout);
862 }
863
Adam Langley95c29f32014-06-20 12:00:00 -0700864int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
865 STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
866 {
867 if (s == NULL) return(0);
868 s->tls_session_secret_cb = tls_session_secret_cb;
869 s->tls_session_secret_cb_arg = arg;
870 return(1);
871 }
872
873int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
874 void *arg)
875 {
876 if (s == NULL) return(0);
877 s->tls_session_ticket_ext_cb = cb;
878 s->tls_session_ticket_ext_cb_arg = arg;
879 return(1);
880 }
881
882int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
883 {
884 if (s->version >= TLS1_VERSION)
885 {
886 if (s->tlsext_session_ticket)
887 {
888 OPENSSL_free(s->tlsext_session_ticket);
889 s->tlsext_session_ticket = NULL;
890 }
891
892 s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
893 if (!s->tlsext_session_ticket)
894 {
895 OPENSSL_PUT_ERROR(SSL, SSL_set_session_ticket_ext, ERR_R_MALLOC_FAILURE);
896 return 0;
897 }
898
899 if (ext_data)
900 {
901 s->tlsext_session_ticket->length = ext_len;
902 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
903 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
904 }
905 else
906 {
907 s->tlsext_session_ticket->length = 0;
908 s->tlsext_session_ticket->data = NULL;
909 }
910
911 return 1;
912 }
913
914 return 0;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916
917typedef struct timeout_param_st
918 {
919 SSL_CTX *ctx;
920 long time;
921 LHASH_OF(SSL_SESSION) *cache;
922 } TIMEOUT_PARAM;
923
924static void timeout_doall_arg(SSL_SESSION *sess, void *void_param)
925 {
926 TIMEOUT_PARAM *param = void_param;
927
928 if ((param->time == 0) || (param->time > (sess->time+sess->timeout))) /* timeout */
929 {
930 /* The reason we don't call SSL_CTX_remove_session() is to
931 * save on locking overhead */
932 (void)lh_SSL_SESSION_delete(param->cache,sess);
933 SSL_SESSION_list_remove(param->ctx,sess);
934 sess->not_resumable=1;
935 if (param->ctx->remove_session_cb != NULL)
936 param->ctx->remove_session_cb(param->ctx,sess);
937 SSL_SESSION_free(sess);
938 }
939 }
940
941void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
942 {
943 TIMEOUT_PARAM tp;
944
945 tp.ctx=s;
946 tp.cache=s->sessions;
947 if (tp.cache == NULL) return;
948 tp.time=t;
949 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
950 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
951 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
952 }
953
954int ssl_clear_bad_session(SSL *s)
955 {
956 if ( (s->session != NULL) &&
957 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
958 !(SSL_in_init(s) || SSL_in_before(s)))
959 {
960 SSL_CTX_remove_session(s->ctx,s->session);
961 return(1);
962 }
963 else
964 return(0);
965 }
966
967/* locked by SSL_CTX in the calling function */
968static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
969 {
970 if ((s->next == NULL) || (s->prev == NULL)) return;
971
972 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
973 { /* last element in list */
974 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
975 { /* only one element in list */
976 ctx->session_cache_head=NULL;
977 ctx->session_cache_tail=NULL;
978 }
979 else
980 {
981 ctx->session_cache_tail=s->prev;
982 s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
983 }
984 }
985 else
986 {
987 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
988 { /* first element in list */
989 ctx->session_cache_head=s->next;
990 s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
991 }
992 else
993 { /* middle of list */
994 s->next->prev=s->prev;
995 s->prev->next=s->next;
996 }
997 }
998 s->prev=s->next=NULL;
999 }
1000
1001static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1002 {
1003 if ((s->next != NULL) && (s->prev != NULL))
1004 SSL_SESSION_list_remove(ctx,s);
1005
1006 if (ctx->session_cache_head == NULL)
1007 {
1008 ctx->session_cache_head=s;
1009 ctx->session_cache_tail=s;
1010 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1011 s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
1012 }
1013 else
1014 {
1015 s->next=ctx->session_cache_head;
1016 s->next->prev=s;
1017 s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
1018 ctx->session_cache_head=s;
1019 }
1020 }
1021
1022void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1023 int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
1024 {
1025 ctx->new_session_cb=cb;
1026 }
1027
1028int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1029 {
1030 return ctx->new_session_cb;
1031 }
1032
1033void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1034 void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
1035 {
1036 ctx->remove_session_cb=cb;
1037 }
1038
1039void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
1040 {
1041 return ctx->remove_session_cb;
1042 }
1043
1044void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1045 SSL_SESSION *(*cb)(struct ssl_st *ssl,
1046 unsigned char *data,int len,int *copy))
1047 {
1048 ctx->get_session_cb=cb;
1049 }
1050
1051SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
1052 unsigned char *data,int len,int *copy)
1053 {
1054 return ctx->get_session_cb;
1055 }
1056
1057void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1058 void (*cb)(const SSL *ssl,int type,int val))
1059 {
1060 ctx->info_callback=cb;
1061 }
1062
1063void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
1064 {
1065 return ctx->info_callback;
1066 }
1067
1068void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1069 int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1070 {
1071 ctx->client_cert_cb=cb;
1072 }
1073
1074int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
1075 {
1076 return ctx->client_cert_cb;
1077 }
1078
1079void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1080 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1081 {
1082 ctx->app_gen_cookie_cb=cb;
1083 }
1084
David Benjamin22f9bcc2014-07-13 12:29:21 -04001085/* TODO(davidben): |cookie| should be a const pointer. */
Adam Langley95c29f32014-06-20 12:00:00 -07001086void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1087 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
1088 {
1089 ctx->app_verify_cookie_cb=cb;
1090 }
1091
Adam Langley1258b6a2014-06-20 12:00:00 -07001092void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
1093 void (*cb)(SSL *ssl, EVP_PKEY **pkey))
1094 {
1095 ctx->channel_id_cb=cb;
1096 }
1097
1098void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL * ssl, EVP_PKEY **pkey)
1099 {
1100 return ctx->channel_id_cb;
1101 }
1102
Adam Langley95c29f32014-06-20 12:00:00 -07001103IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)