blob: 2f8ca7f5a73e8dd6bee2c12c61c5caa2e94aac7d [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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400137#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700138
Adam Langley95c29f32014-06-20 12:00:00 -0700139#include <openssl/err.h>
140#include <openssl/lhash.h>
141#include <openssl/mem.h>
142#include <openssl/rand.h>
143
David Benjamin2ee94aa2015-04-07 22:38:30 -0400144#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700145
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. */
Brian Smithefed2212015-01-28 16:20:02 -0800149static const char g_pending_session_magic = 0;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700150
Adam Langley95c29f32014-06-20 12:00:00 -0700151static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
David Benjaminb18f0242015-03-10 18:30:08 -0400152static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700153static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155SSL_SESSION *SSL_magic_pending_session_ptr(void) {
156 return (SSL_SESSION *)&g_pending_session_magic;
Adam Langley95c29f32014-06-20 12:00:00 -0700157}
158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159SSL_SESSION *SSL_get_session(const SSL *ssl)
160{
161 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
162 return ssl->session;
163}
Adam Langley95c29f32014-06-20 12:00:00 -0700164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165SSL_SESSION *SSL_get1_session(SSL *ssl) {
166 /* variant of SSL_get_session: caller really gets something */
David Benjamin23a8ca12015-02-09 03:35:43 -0500167 return SSL_SESSION_up_ref(ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800168}
Adam Langley8eaaa862014-08-11 17:18:25 -0700169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
171 CRYPTO_EX_dup *dup_func,
172 CRYPTO_EX_free *free_func) {
173 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
174 new_func, dup_func, free_func);
175}
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) {
178 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
179}
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) {
182 return CRYPTO_get_ex_data(&s->ex_data, idx);
183}
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langleyfcf25832014-12-18 17:42:32 -0800185SSL_SESSION *SSL_SESSION_new(void) {
186 SSL_SESSION *ss;
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langleyfcf25832014-12-18 17:42:32 -0800188 ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
189 if (ss == NULL) {
190 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
191 return 0;
192 }
193 memset(ss, 0, sizeof(SSL_SESSION));
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
196 ss->references = 1;
197 ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
198 ss->time = (unsigned long)time(NULL);
199 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
200 return ss;
201}
202
203const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) {
204 if (len) {
205 *len = s->session_id_length;
206 }
207 return s->session_id;
208}
209
210/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space.
211 * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random
212 * gunk repeatedly until we have no conflict is going to complete in one
213 * iteration pretty much "most" of the time (btw: understatement). So, if it
214 * takes us 10 iterations and we still can't avoid a conflict - well that's a
215 * reasonable point to call it quits. Either the RAND code is broken or someone
216 * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our
217 * server. How you might store that many sessions is perhaps a more interesting
218 * question ... */
219static int def_generate_session_id(const SSL *ssl, uint8_t *id,
220 unsigned int *id_len) {
221 static const unsigned kMaxAttempts = 10;
222 unsigned int retry = 0;
223 do {
224 if (!RAND_bytes(id, *id_len)) {
225 return 0;
226 }
227 } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
228 (++retry < kMaxAttempts));
229
230 if (retry < kMaxAttempts) {
231 return 1;
232 }
233
234 /* else - woops a session_id match */
235 /* XXX We should also check the external cache -- but the probability of a
236 * collision is negligible, and we could not prevent the concurrent creation
237 * of sessions with identical IDs since we currently don't have means to
238 * atomically check whether a session ID already exists and make a
239 * reservation for it if it does not (this problem applies to the internal
240 * cache as well). */
241 return 0;
242}
243
244int ssl_get_new_session(SSL *s, int session) {
245 /* This gets used by clients and servers. */
246
247 unsigned int tmp;
248 SSL_SESSION *ss = NULL;
249 GEN_SESSION_CB cb = def_generate_session_id;
250
251 if (s->mode & SSL_MODE_NO_SESSION_CREATION) {
252 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
253 SSL_R_SESSION_MAY_NOT_BE_CREATED);
254 return 0;
255 }
256
257 ss = SSL_SESSION_new();
258 if (ss == NULL) {
259 return 0;
260 }
261
262 /* If the context has a default timeout, use it over the default. */
263 if (s->initial_ctx->session_timeout != 0) {
264 ss->timeout = s->initial_ctx->session_timeout;
265 }
266
267 if (s->session != NULL) {
268 SSL_SESSION_free(s->session);
269 s->session = NULL;
270 }
271
272 if (session) {
273 if (s->version == SSL3_VERSION || s->version == TLS1_VERSION ||
274 s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION ||
275 s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) {
276 ss->ssl_version = s->version;
277 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
278 } else {
279 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
280 SSL_R_UNSUPPORTED_SSL_VERSION);
281 SSL_SESSION_free(ss);
282 return 0;
283 }
284
285 /* If RFC4507 ticket use empty session ID */
286 if (s->tlsext_ticket_expected) {
287 ss->session_id_length = 0;
288 goto sess_id_done;
289 }
290
291 /* Choose which callback will set the session ID */
292 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
293 if (s->generate_session_id) {
294 cb = s->generate_session_id;
295 } else if (s->initial_ctx->generate_session_id) {
296 cb = s->initial_ctx->generate_session_id;
297 }
298 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
299
300 /* Choose a session ID */
301 tmp = ss->session_id_length;
302 if (!cb(s, ss->session_id, &tmp)) {
303 /* The callback failed */
304 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
305 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
306 SSL_SESSION_free(ss);
307 return 0;
308 }
309
310 /* Don't allow the callback to set the session length to zero. nor set it
311 * higher than it was. */
312 if (!tmp || tmp > ss->session_id_length) {
313 /* The callback set an illegal length */
314 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
315 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
316 SSL_SESSION_free(ss);
317 return 0;
318 }
319
320 ss->session_id_length = tmp;
321 /* Finally, check for a conflict */
322 if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) {
323 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
324 SSL_R_SSL_SESSION_ID_CONFLICT);
325 SSL_SESSION_free(ss);
326 return 0;
327 }
328
329 sess_id_done:
330 if (s->tlsext_hostname) {
331 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
332 if (ss->tlsext_hostname == NULL) {
333 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
334 SSL_SESSION_free(ss);
335 return 0;
336 }
337 }
338 } else {
339 ss->session_id_length = 0;
340 }
341
342 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
343 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
344 SSL_SESSION_free(ss);
345 return 0;
346 }
347
348 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
349 ss->sid_ctx_length = s->sid_ctx_length;
350 s->session = ss;
351 ss->ssl_version = s->version;
352 ss->verify_result = X509_V_OK;
353
354 return 1;
355}
Adam Langley95c29f32014-06-20 12:00:00 -0700356
357/* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
358 * connection. It is only called by servers.
359 *
Adam Langleydc9b1412014-06-20 12:00:00 -0700360 * ctx: contains the early callback context, which is the result of a
361 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -0700362 *
363 * Returns:
364 * -1: error
365 * 0: a session may have been found.
366 *
367 * Side effects:
368 * - If a session is found then s->session is pointed at it (after freeing an
369 * existing session if need be) and s->verify_result is set from the session.
370 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 * if the server should issue a new session ticket (to 0 otherwise). */
372int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx) {
373 /* This is used only by servers. */
374 SSL_SESSION *ret = NULL;
375 int fatal = 0;
376 int try_session_cache = 1;
377 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
380 goto err;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 if (ctx->session_id_len == 0) {
384 try_session_cache = 0;
385 }
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
388 switch (r) {
389 case -1: /* Error during processing */
390 fatal = 1;
391 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 case 0: /* No ticket found */
394 case 1: /* Zero length ticket found */
395 break; /* Ok to carry on processing session id. */
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 case 2: /* Ticket found but not decrypted. */
398 case 3: /* Ticket decrypted, *ret has been set. */
399 try_session_cache = 0;
400 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 default:
403 abort();
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (try_session_cache && ret == NULL &&
407 !(s->initial_ctx->session_cache_mode &
408 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
409 SSL_SESSION data;
410 data.ssl_version = s->version;
411 data.session_id_length = ctx->session_id_len;
412 if (ctx->session_id_len == 0) {
413 return 0;
414 }
415 memcpy(data.session_id, ctx->session_id, ctx->session_id_len);
416 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
David Benjamin33639842015-02-09 03:34:47 -0500417 ret = SSL_SESSION_up_ref(lh_SSL_SESSION_retrieve(s->initial_ctx->sessions,
418 &data));
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 if (try_session_cache && ret == NULL &&
423 s->initial_ctx->get_session_cb != NULL) {
424 int copy = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id,
427 ctx->session_id_len, &copy);
428 if (ret != NULL) {
429 if (ret == SSL_magic_pending_session_ptr()) {
430 /* This is a magic value which indicates that the callback needs to
431 * unwind the stack and figure out the session asynchronously. */
432 return PENDING_SESSION;
433 }
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 /* Increment reference count now if the session callback asks us to do so
436 * (note that if the session structures returned by the callback are
437 * shared between threads, it must handle the reference count itself
438 * [i.e. copy == 0], or things won't be thread-safe). */
439 if (copy) {
David Benjamin33639842015-02-09 03:34:47 -0500440 SSL_SESSION_up_ref(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 }
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 /* Add the externally cached session to the internal cache as well if and
444 * only if we are supposed to. */
445 if (!(s->initial_ctx->session_cache_mode &
446 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
447 /* The following should not return 1, otherwise, things are very
448 * strange */
449 SSL_CTX_add_session(s->initial_ctx, ret);
450 }
451 }
452 }
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 if (ret == NULL) {
455 goto err;
456 }
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 /* Now ret is non-NULL and we own one of its reference counts. */
Adam Langley95c29f32014-06-20 12:00:00 -0700459
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 if (ret->sid_ctx_length != s->sid_ctx_length ||
461 memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
462 /* We have the session requested by the client, but we don't want to use it
463 * in this context. */
464 goto err; /* treat like cache miss */
465 }
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
468 /* We can't be sure if this session is being used out of context, which is
469 * especially important for SSL_VERIFY_PEER. The application should have
470 * used SSL[_CTX]_set_session_id_context.
471 *
472 * For this error case, we generate an error instead of treating the event
473 * like a cache miss (otherwise it would be easy for applications to
474 * effectively disable the session cache by accident without anyone
475 * noticing). */
476 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session,
477 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
478 fatal = 1;
479 goto err;
480 }
Adam Langley95c29f32014-06-20 12:00:00 -0700481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 if (ret->timeout < (long)(time(NULL) - ret->time)) {
483 /* timeout */
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 if (try_session_cache) {
485 /* session was from the cache, so remove it */
486 SSL_CTX_remove_session(s->initial_ctx, ret);
487 }
488 goto err;
489 }
Adam Langley95c29f32014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 if (s->session != NULL) {
492 SSL_SESSION_free(s->session);
493 }
494 s->session = ret;
495 s->verify_result = s->session->verify_result;
496 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498err:
499 if (ret != NULL) {
500 SSL_SESSION_free(ret);
501 if (!try_session_cache) {
502 /* The session was from a ticket, so we should
503 * issue a ticket for the new session */
504 s->tlsext_ticket_expected = 1;
505 }
506 }
507 if (fatal) {
508 return -1;
509 }
510 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700511}
512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
514 int ret = 0;
515 SSL_SESSION *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 /* add just 1 reference count for the SSL_CTX's session cache even though it
518 * has two ways of access: each session is in a doubly linked list and an
519 * lhash */
David Benjamin33639842015-02-09 03:34:47 -0500520 SSL_SESSION_up_ref(c);
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 /* if session c is in already in cache, we take back the increment later */
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
524 if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
525 return 0;
526 }
Adam Langley95c29f32014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* s != NULL iff we already had a session with the given PID. In this case, s
529 * == c should hold (then we did not really modify ctx->sessions), or we're
530 * in trouble. */
531 if (s != NULL && s != c) {
532 /* We *are* in trouble ... */
533 SSL_SESSION_list_remove(ctx, s);
534 SSL_SESSION_free(s);
535 /* ... so pretend the other session did not exist in cache (we cannot
536 * handle two SSL_SESSION structures with identical session ID in the same
537 * cache, which could happen e.g. when two threads concurrently obtain the
538 * same session from an external cache) */
539 s = NULL;
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 /* Put at the head of the queue unless it is already in the cache */
543 if (s == NULL) {
544 SSL_SESSION_list_add(ctx, c);
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 if (s != NULL) {
548 /* existing cache entry -- decrement previously incremented reference count
549 * because it already takes into account the cache */
550 SSL_SESSION_free(s); /* s == c */
551 ret = 0;
552 } else {
553 /* new cache entry -- remove old ones if cache has become too large */
554 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
557 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
558 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
559 break;
560 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 }
562 }
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
566 return ret;
567}
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleyfcf25832014-12-18 17:42:32 -0800569int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
570 return remove_session_lock(ctx, c, 1);
571}
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
574 SSL_SESSION *r;
575 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 if (c != NULL && c->session_id_length != 0) {
578 if (lock) {
579 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
580 }
581 r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
582 if (r == c) {
583 ret = 1;
584 r = lh_SSL_SESSION_delete(ctx->sessions, c);
585 SSL_SESSION_list_remove(ctx, c);
586 }
Adam Langley95c29f32014-06-20 12:00:00 -0700587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 if (lock) {
589 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 if (ret) {
593 r->not_resumable = 1;
594 if (ctx->remove_session_cb != NULL) {
595 ctx->remove_session_cb(ctx, r);
596 }
597 SSL_SESSION_free(r);
598 }
599 }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 return ret;
602}
Adam Langley95c29f32014-06-20 12:00:00 -0700603
David Benjamin33639842015-02-09 03:34:47 -0500604SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
605 if (session) {
606 CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
607 }
608 return session;
609}
610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611void SSL_SESSION_free(SSL_SESSION *ss) {
612 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 if (ss == NULL) {
615 return;
616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
619 if (i > 0) {
620 return;
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
626 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
627 if (ss->sess_cert != NULL) {
628 ssl_sess_cert_free(ss->sess_cert);
629 }
630 if (ss->peer != NULL) {
631 X509_free(ss->peer);
632 }
633 if (ss->tlsext_hostname != NULL) {
634 OPENSSL_free(ss->tlsext_hostname);
635 }
636 if (ss->tlsext_tick != NULL) {
637 OPENSSL_free(ss->tlsext_tick);
638 }
639 if (ss->tlsext_signed_cert_timestamp_list != NULL) {
640 OPENSSL_free(ss->tlsext_signed_cert_timestamp_list);
641 }
642 if (ss->ocsp_response != NULL) {
643 OPENSSL_free(ss->ocsp_response);
644 }
645 if (ss->psk_identity != NULL) {
646 OPENSSL_free(ss->psk_identity);
647 }
648 OPENSSL_cleanse(ss, sizeof(*ss));
649 OPENSSL_free(ss);
650}
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652int SSL_set_session(SSL *s, SSL_SESSION *session) {
653 if (s->session == session) {
654 return 1;
655 }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 if (s->session != NULL) {
658 SSL_SESSION_free(s->session);
659 }
660 s->session = session;
661 if (session != NULL) {
David Benjamin33639842015-02-09 03:34:47 -0500662 SSL_SESSION_up_ref(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 s->verify_result = session->verify_result;
664 }
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 return 1;
667}
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langleyfcf25832014-12-18 17:42:32 -0800669long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
670 if (s == NULL) {
671 return 0;
672 }
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 s->timeout = t;
675 return 1;
676}
Adam Langley95c29f32014-06-20 12:00:00 -0700677
Adam Langleyfcf25832014-12-18 17:42:32 -0800678long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
679 if (s == NULL) {
680 return 0;
681 }
Adam Langley95c29f32014-06-20 12:00:00 -0700682
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 return s->timeout;
684}
685
686long SSL_SESSION_get_time(const SSL_SESSION *s) {
687 if (s == NULL) {
688 return 0;
689 }
690
691 return s->time;
692}
693
694long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
695 if (s == NULL) {
696 return 0;
697 }
698
699 s->time = t;
700 return t;
701}
702
703X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
704
705int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
706 unsigned int sid_ctx_len) {
707 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
708 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context,
709 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
710 return 0;
711 }
712
713 s->sid_ctx_length = sid_ctx_len;
714 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
715
716 return 1;
717}
718
719long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
720 long l;
721 if (s == NULL) {
722 return 0;
723 }
724
725 l = s->session_timeout;
726 s->session_timeout = t;
727 return l;
728}
729
730long SSL_CTX_get_timeout(const SSL_CTX *s) {
731 if (s == NULL) {
732 return 0;
733 }
734
735 return s->session_timeout;
736}
737
738typedef struct timeout_param_st {
739 SSL_CTX *ctx;
740 long time;
741 LHASH_OF(SSL_SESSION) * cache;
742} TIMEOUT_PARAM;
743
744static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
745 TIMEOUT_PARAM *param = void_param;
746
747 if (param->time == 0 ||
748 param->time > (sess->time + sess->timeout)) {
749 /* timeout */
750 /* The reason we don't call SSL_CTX_remove_session() is to
751 * save on locking overhead */
Adam Langleya307dfd2015-01-09 15:42:58 -0800752 (void) lh_SSL_SESSION_delete(param->cache, sess);
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 SSL_SESSION_list_remove(param->ctx, sess);
754 sess->not_resumable = 1;
755 if (param->ctx->remove_session_cb != NULL) {
756 param->ctx->remove_session_cb(param->ctx, sess);
757 }
758 SSL_SESSION_free(sess);
759 }
760}
761
762void SSL_CTX_flush_sessions(SSL_CTX *s, long t) {
763 TIMEOUT_PARAM tp;
764
765 tp.ctx = s;
766 tp.cache = s->sessions;
767 if (tp.cache == NULL) {
768 return;
769 }
770 tp.time = t;
771 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
772 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
773 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
774}
775
776int ssl_clear_bad_session(SSL *s) {
777 if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
778 !SSL_in_init(s)) {
779 SSL_CTX_remove_session(s->ctx, s->session);
780 return 1;
781 }
782
783 return 0;
784}
Adam Langley95c29f32014-06-20 12:00:00 -0700785
786/* locked by SSL_CTX in the calling function */
Adam Langleyfcf25832014-12-18 17:42:32 -0800787static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
788 if (s->next == NULL || s->prev == NULL) {
789 return;
790 }
Adam Langley95c29f32014-06-20 12:00:00 -0700791
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
793 /* last element in list */
794 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
795 /* only one element in list */
796 ctx->session_cache_head = NULL;
797 ctx->session_cache_tail = NULL;
798 } else {
799 ctx->session_cache_tail = s->prev;
800 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
801 }
802 } else {
803 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
804 /* first element in list */
805 ctx->session_cache_head = s->next;
806 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
807 } else { /* middle of list */
808 s->next->prev = s->prev;
809 s->prev->next = s->next;
810 }
811 }
812 s->prev = s->next = NULL;
813}
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
816 if (s->next != NULL && s->prev != NULL) {
817 SSL_SESSION_list_remove(ctx, s);
818 }
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 if (ctx->session_cache_head == NULL) {
821 ctx->session_cache_head = s;
822 ctx->session_cache_tail = s;
823 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
824 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
825 } else {
826 s->next = ctx->session_cache_head;
827 s->next->prev = s;
828 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
829 ctx->session_cache_head = s;
830 }
831}
Adam Langley95c29f32014-06-20 12:00:00 -0700832
833void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
835 ctx->new_session_cb = cb;
836}
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
839 return ctx->new_session_cb;
840}
Adam Langley95c29f32014-06-20 12:00:00 -0700841
842void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
844 ctx->remove_session_cb = cb;
845}
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
848 SSL_SESSION *sess) {
849 return ctx->remove_session_cb;
850}
Adam Langley95c29f32014-06-20 12:00:00 -0700851
852void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 SSL_SESSION *(*cb)(struct ssl_st *ssl,
854 uint8_t *data, int len,
855 int *copy)) {
856 ctx->get_session_cb = cb;
857}
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
860 int len, int *copy) {
861 return ctx->get_session_cb;
862}
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864void SSL_CTX_set_info_callback(SSL_CTX *ctx,
865 void (*cb)(const SSL *ssl, int type, int val)) {
866 ctx->info_callback = cb;
867}
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
870 int val) {
871 return ctx->info_callback;
872}
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
875 EVP_PKEY **pkey)) {
876 ctx->client_cert_cb = cb;
877}
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
880 EVP_PKEY **pkey) {
881 return ctx->client_cert_cb;
882}
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langley1258b6a2014-06-20 12:00:00 -0700884void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
886 ctx->channel_id_cb = cb;
887}
Adam Langley1258b6a2014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
890 return ctx->channel_id_cb;
891}
Adam Langley1258b6a2014-06-20 12:00:00 -0700892
Adam Langley95c29f32014-06-20 12:00:00 -0700893IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)