blob: f3f280cf231947fe0e616f10eac8cbc2ddd87301 [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"
David Benjamin546f1a52015-04-15 16:46:09 -0400145#include "../crypto/internal.h"
146
Adam Langley95c29f32014-06-20 12:00:00 -0700147
Adam Langleyb2ce0582014-06-20 12:00:00 -0700148/* The address of this is a magic value, a pointer to which is returned by
149 * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
150 * that it needs to asynchronously fetch session information. */
Brian Smithefed2212015-01-28 16:20:02 -0800151static const char g_pending_session_magic = 0;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700152
David Benjamin9f33fc62015-04-15 17:29:53 -0400153static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
154
Adam Langley95c29f32014-06-20 12:00:00 -0700155static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
David Benjaminb18f0242015-03-10 18:30:08 -0400156static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700157static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159SSL_SESSION *SSL_magic_pending_session_ptr(void) {
160 return (SSL_SESSION *)&g_pending_session_magic;
Adam Langley95c29f32014-06-20 12:00:00 -0700161}
162
Adam Langleyfcf25832014-12-18 17:42:32 -0800163SSL_SESSION *SSL_get_session(const SSL *ssl)
164{
165 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
166 return ssl->session;
167}
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langleyfcf25832014-12-18 17:42:32 -0800169SSL_SESSION *SSL_get1_session(SSL *ssl) {
170 /* variant of SSL_get_session: caller really gets something */
David Benjamin23a8ca12015-02-09 03:35:43 -0500171 return SSL_SESSION_up_ref(ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172}
Adam Langley8eaaa862014-08-11 17:18:25 -0700173
Adam Langleyfcf25832014-12-18 17:42:32 -0800174int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
175 CRYPTO_EX_dup *dup_func,
176 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400177 int index;
178 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
179 dup_func, free_func)) {
180 return -1;
181 }
182 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -0800183}
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langleyfcf25832014-12-18 17:42:32 -0800185int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) {
186 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
187}
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) {
190 return CRYPTO_get_ex_data(&s->ex_data, idx);
191}
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langleyfcf25832014-12-18 17:42:32 -0800193SSL_SESSION *SSL_SESSION_new(void) {
194 SSL_SESSION *ss;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
197 if (ss == NULL) {
198 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
199 return 0;
200 }
201 memset(ss, 0, sizeof(SSL_SESSION));
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
204 ss->references = 1;
205 ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
206 ss->time = (unsigned long)time(NULL);
David Benjamin9f33fc62015-04-15 17:29:53 -0400207 CRYPTO_new_ex_data(&g_ex_data_class, ss, &ss->ex_data);
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 return ss;
209}
210
211const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) {
212 if (len) {
213 *len = s->session_id_length;
214 }
215 return s->session_id;
216}
217
218/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space.
219 * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random
220 * gunk repeatedly until we have no conflict is going to complete in one
221 * iteration pretty much "most" of the time (btw: understatement). So, if it
222 * takes us 10 iterations and we still can't avoid a conflict - well that's a
223 * reasonable point to call it quits. Either the RAND code is broken or someone
224 * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our
225 * server. How you might store that many sessions is perhaps a more interesting
226 * question ... */
227static int def_generate_session_id(const SSL *ssl, uint8_t *id,
228 unsigned int *id_len) {
229 static const unsigned kMaxAttempts = 10;
230 unsigned int retry = 0;
231 do {
232 if (!RAND_bytes(id, *id_len)) {
233 return 0;
234 }
235 } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
236 (++retry < kMaxAttempts));
237
238 if (retry < kMaxAttempts) {
239 return 1;
240 }
241
242 /* else - woops a session_id match */
243 /* XXX We should also check the external cache -- but the probability of a
244 * collision is negligible, and we could not prevent the concurrent creation
245 * of sessions with identical IDs since we currently don't have means to
246 * atomically check whether a session ID already exists and make a
247 * reservation for it if it does not (this problem applies to the internal
248 * cache as well). */
249 return 0;
250}
251
252int ssl_get_new_session(SSL *s, int session) {
253 /* This gets used by clients and servers. */
254
255 unsigned int tmp;
256 SSL_SESSION *ss = NULL;
257 GEN_SESSION_CB cb = def_generate_session_id;
258
259 if (s->mode & SSL_MODE_NO_SESSION_CREATION) {
260 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
261 SSL_R_SESSION_MAY_NOT_BE_CREATED);
262 return 0;
263 }
264
265 ss = SSL_SESSION_new();
266 if (ss == NULL) {
267 return 0;
268 }
269
270 /* If the context has a default timeout, use it over the default. */
271 if (s->initial_ctx->session_timeout != 0) {
272 ss->timeout = s->initial_ctx->session_timeout;
273 }
274
David Benjamin2755a3e2015-04-22 16:17:58 -0400275 SSL_SESSION_free(s->session);
276 s->session = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800277
278 if (session) {
279 if (s->version == SSL3_VERSION || s->version == TLS1_VERSION ||
280 s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION ||
281 s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) {
282 ss->ssl_version = s->version;
283 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
284 } else {
285 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
286 SSL_R_UNSUPPORTED_SSL_VERSION);
287 SSL_SESSION_free(ss);
288 return 0;
289 }
290
291 /* If RFC4507 ticket use empty session ID */
292 if (s->tlsext_ticket_expected) {
293 ss->session_id_length = 0;
294 goto sess_id_done;
295 }
296
297 /* Choose which callback will set the session ID */
298 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
299 if (s->generate_session_id) {
300 cb = s->generate_session_id;
301 } else if (s->initial_ctx->generate_session_id) {
302 cb = s->initial_ctx->generate_session_id;
303 }
304 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
305
306 /* Choose a session ID */
307 tmp = ss->session_id_length;
308 if (!cb(s, ss->session_id, &tmp)) {
309 /* The callback failed */
310 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
311 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
312 SSL_SESSION_free(ss);
313 return 0;
314 }
315
316 /* Don't allow the callback to set the session length to zero. nor set it
317 * higher than it was. */
318 if (!tmp || tmp > ss->session_id_length) {
319 /* The callback set an illegal length */
320 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
321 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
322 SSL_SESSION_free(ss);
323 return 0;
324 }
325
326 ss->session_id_length = tmp;
327 /* Finally, check for a conflict */
328 if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) {
329 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
330 SSL_R_SSL_SESSION_ID_CONFLICT);
331 SSL_SESSION_free(ss);
332 return 0;
333 }
334
335 sess_id_done:
336 if (s->tlsext_hostname) {
337 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
338 if (ss->tlsext_hostname == NULL) {
339 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
340 SSL_SESSION_free(ss);
341 return 0;
342 }
343 }
344 } else {
345 ss->session_id_length = 0;
346 }
347
348 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
349 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
350 SSL_SESSION_free(ss);
351 return 0;
352 }
353
354 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
355 ss->sid_ctx_length = s->sid_ctx_length;
356 s->session = ss;
357 ss->ssl_version = s->version;
358 ss->verify_result = X509_V_OK;
359
360 return 1;
361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
363/* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
364 * connection. It is only called by servers.
365 *
Adam Langleydc9b1412014-06-20 12:00:00 -0700366 * ctx: contains the early callback context, which is the result of a
367 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -0700368 *
369 * Returns:
370 * -1: error
371 * 0: a session may have been found.
372 *
373 * Side effects:
374 * - If a session is found then s->session is pointed at it (after freeing an
375 * existing session if need be) and s->verify_result is set from the session.
376 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 * if the server should issue a new session ticket (to 0 otherwise). */
378int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx) {
379 /* This is used only by servers. */
380 SSL_SESSION *ret = NULL;
381 int fatal = 0;
382 int try_session_cache = 1;
383 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
386 goto err;
387 }
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 if (ctx->session_id_len == 0) {
390 try_session_cache = 0;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
394 switch (r) {
395 case -1: /* Error during processing */
396 fatal = 1;
397 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 case 0: /* No ticket found */
400 case 1: /* Zero length ticket found */
401 break; /* Ok to carry on processing session id. */
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 case 2: /* Ticket found but not decrypted. */
404 case 3: /* Ticket decrypted, *ret has been set. */
405 try_session_cache = 0;
406 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 default:
409 abort();
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 if (try_session_cache && ret == NULL &&
413 !(s->initial_ctx->session_cache_mode &
414 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
415 SSL_SESSION data;
416 data.ssl_version = s->version;
417 data.session_id_length = ctx->session_id_len;
418 if (ctx->session_id_len == 0) {
419 return 0;
420 }
421 memcpy(data.session_id, ctx->session_id, ctx->session_id_len);
422 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
David Benjamin33639842015-02-09 03:34:47 -0500423 ret = SSL_SESSION_up_ref(lh_SSL_SESSION_retrieve(s->initial_ctx->sessions,
424 &data));
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 }
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 if (try_session_cache && ret == NULL &&
429 s->initial_ctx->get_session_cb != NULL) {
430 int copy = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id,
433 ctx->session_id_len, &copy);
434 if (ret != NULL) {
435 if (ret == SSL_magic_pending_session_ptr()) {
436 /* This is a magic value which indicates that the callback needs to
437 * unwind the stack and figure out the session asynchronously. */
438 return PENDING_SESSION;
439 }
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 /* Increment reference count now if the session callback asks us to do so
442 * (note that if the session structures returned by the callback are
443 * shared between threads, it must handle the reference count itself
444 * [i.e. copy == 0], or things won't be thread-safe). */
445 if (copy) {
David Benjamin33639842015-02-09 03:34:47 -0500446 SSL_SESSION_up_ref(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 }
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 /* Add the externally cached session to the internal cache as well if and
450 * only if we are supposed to. */
451 if (!(s->initial_ctx->session_cache_mode &
452 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
453 /* The following should not return 1, otherwise, things are very
454 * strange */
455 SSL_CTX_add_session(s->initial_ctx, ret);
456 }
457 }
458 }
Adam Langley95c29f32014-06-20 12:00:00 -0700459
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 if (ret == NULL) {
461 goto err;
462 }
Adam Langley95c29f32014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 /* Now ret is non-NULL and we own one of its reference counts. */
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 if (ret->sid_ctx_length != s->sid_ctx_length ||
467 memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
468 /* We have the session requested by the client, but we don't want to use it
469 * in this context. */
470 goto err; /* treat like cache miss */
471 }
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
474 /* We can't be sure if this session is being used out of context, which is
475 * especially important for SSL_VERIFY_PEER. The application should have
476 * used SSL[_CTX]_set_session_id_context.
477 *
478 * For this error case, we generate an error instead of treating the event
479 * like a cache miss (otherwise it would be easy for applications to
480 * effectively disable the session cache by accident without anyone
481 * noticing). */
482 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session,
483 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
484 fatal = 1;
485 goto err;
486 }
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 if (ret->timeout < (long)(time(NULL) - ret->time)) {
489 /* timeout */
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 if (try_session_cache) {
491 /* session was from the cache, so remove it */
492 SSL_CTX_remove_session(s->initial_ctx, ret);
493 }
494 goto err;
495 }
Adam Langley95c29f32014-06-20 12:00:00 -0700496
David Benjamin2755a3e2015-04-22 16:17:58 -0400497 SSL_SESSION_free(s->session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 s->session = ret;
499 s->verify_result = s->session->verify_result;
500 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700501
Adam Langleyfcf25832014-12-18 17:42:32 -0800502err:
503 if (ret != NULL) {
504 SSL_SESSION_free(ret);
505 if (!try_session_cache) {
506 /* The session was from a ticket, so we should
507 * issue a ticket for the new session */
508 s->tlsext_ticket_expected = 1;
509 }
510 }
511 if (fatal) {
512 return -1;
513 }
514 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700515}
516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
518 int ret = 0;
519 SSL_SESSION *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 /* add just 1 reference count for the SSL_CTX's session cache even though it
522 * has two ways of access: each session is in a doubly linked list and an
523 * lhash */
David Benjamin33639842015-02-09 03:34:47 -0500524 SSL_SESSION_up_ref(c);
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 /* if session c is in already in cache, we take back the increment later */
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
528 if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
529 return 0;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 /* s != NULL iff we already had a session with the given PID. In this case, s
533 * == c should hold (then we did not really modify ctx->sessions), or we're
534 * in trouble. */
535 if (s != NULL && s != c) {
536 /* We *are* in trouble ... */
537 SSL_SESSION_list_remove(ctx, s);
538 SSL_SESSION_free(s);
539 /* ... so pretend the other session did not exist in cache (we cannot
540 * handle two SSL_SESSION structures with identical session ID in the same
541 * cache, which could happen e.g. when two threads concurrently obtain the
542 * same session from an external cache) */
543 s = NULL;
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 /* Put at the head of the queue unless it is already in the cache */
547 if (s == NULL) {
548 SSL_SESSION_list_add(ctx, c);
549 }
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 if (s != NULL) {
552 /* existing cache entry -- decrement previously incremented reference count
553 * because it already takes into account the cache */
554 SSL_SESSION_free(s); /* s == c */
555 ret = 0;
556 } else {
557 /* new cache entry -- remove old ones if cache has become too large */
558 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
561 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
562 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
563 break;
564 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 }
566 }
567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
570 return ret;
571}
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
574 return remove_session_lock(ctx, c, 1);
575}
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
578 SSL_SESSION *r;
579 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 if (c != NULL && c->session_id_length != 0) {
582 if (lock) {
583 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
584 }
585 r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
586 if (r == c) {
587 ret = 1;
588 r = lh_SSL_SESSION_delete(ctx->sessions, c);
589 SSL_SESSION_list_remove(ctx, c);
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 if (lock) {
593 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (ret) {
597 r->not_resumable = 1;
598 if (ctx->remove_session_cb != NULL) {
599 ctx->remove_session_cb(ctx, r);
600 }
601 SSL_SESSION_free(r);
602 }
603 }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 return ret;
606}
Adam Langley95c29f32014-06-20 12:00:00 -0700607
David Benjamin33639842015-02-09 03:34:47 -0500608SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
609 if (session) {
610 CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
611 }
612 return session;
613}
614
David Benjamin4fcc2e22015-04-22 12:58:16 -0400615void SSL_SESSION_free(SSL_SESSION *session) {
616 if (session == NULL ||
617 CRYPTO_add(&session->references, -1, CRYPTO_LOCK_SSL_SESSION) > 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 return;
619 }
Adam Langley95c29f32014-06-20 12:00:00 -0700620
David Benjamin4fcc2e22015-04-22 12:58:16 -0400621 CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700622
David Benjamin4fcc2e22015-04-22 12:58:16 -0400623 OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
624 OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
David Benjamin2755a3e2015-04-22 16:17:58 -0400625 ssl_sess_cert_free(session->sess_cert);
626 X509_free(session->peer);
627 OPENSSL_free(session->tlsext_hostname);
628 OPENSSL_free(session->tlsext_tick);
629 OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
630 OPENSSL_free(session->ocsp_response);
631 OPENSSL_free(session->psk_identity);
David Benjamin4fcc2e22015-04-22 12:58:16 -0400632 OPENSSL_cleanse(session, sizeof(*session));
633 OPENSSL_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800634}
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636int SSL_set_session(SSL *s, SSL_SESSION *session) {
637 if (s->session == session) {
638 return 1;
639 }
Adam Langley95c29f32014-06-20 12:00:00 -0700640
David Benjamin2755a3e2015-04-22 16:17:58 -0400641 SSL_SESSION_free(s->session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 s->session = session;
643 if (session != NULL) {
David Benjamin33639842015-02-09 03:34:47 -0500644 SSL_SESSION_up_ref(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 s->verify_result = session->verify_result;
646 }
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 return 1;
649}
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
652 if (s == NULL) {
653 return 0;
654 }
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 s->timeout = t;
657 return 1;
658}
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
661 if (s == NULL) {
662 return 0;
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 return s->timeout;
666}
667
668long SSL_SESSION_get_time(const SSL_SESSION *s) {
669 if (s == NULL) {
670 return 0;
671 }
672
673 return s->time;
674}
675
676long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
677 if (s == NULL) {
678 return 0;
679 }
680
681 s->time = t;
682 return t;
683}
684
685X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
686
687int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
688 unsigned int sid_ctx_len) {
689 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
690 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context,
691 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
692 return 0;
693 }
694
695 s->sid_ctx_length = sid_ctx_len;
696 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
697
698 return 1;
699}
700
701long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
702 long l;
703 if (s == NULL) {
704 return 0;
705 }
706
707 l = s->session_timeout;
708 s->session_timeout = t;
709 return l;
710}
711
712long SSL_CTX_get_timeout(const SSL_CTX *s) {
713 if (s == NULL) {
714 return 0;
715 }
716
717 return s->session_timeout;
718}
719
720typedef struct timeout_param_st {
721 SSL_CTX *ctx;
722 long time;
723 LHASH_OF(SSL_SESSION) * cache;
724} TIMEOUT_PARAM;
725
726static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
727 TIMEOUT_PARAM *param = void_param;
728
729 if (param->time == 0 ||
730 param->time > (sess->time + sess->timeout)) {
731 /* timeout */
732 /* The reason we don't call SSL_CTX_remove_session() is to
733 * save on locking overhead */
Adam Langleya307dfd2015-01-09 15:42:58 -0800734 (void) lh_SSL_SESSION_delete(param->cache, sess);
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 SSL_SESSION_list_remove(param->ctx, sess);
736 sess->not_resumable = 1;
737 if (param->ctx->remove_session_cb != NULL) {
738 param->ctx->remove_session_cb(param->ctx, sess);
739 }
740 SSL_SESSION_free(sess);
741 }
742}
743
744void SSL_CTX_flush_sessions(SSL_CTX *s, long t) {
745 TIMEOUT_PARAM tp;
746
747 tp.ctx = s;
748 tp.cache = s->sessions;
749 if (tp.cache == NULL) {
750 return;
751 }
752 tp.time = t;
753 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
754 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
755 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
756}
757
758int ssl_clear_bad_session(SSL *s) {
759 if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
760 !SSL_in_init(s)) {
761 SSL_CTX_remove_session(s->ctx, s->session);
762 return 1;
763 }
764
765 return 0;
766}
Adam Langley95c29f32014-06-20 12:00:00 -0700767
768/* locked by SSL_CTX in the calling function */
Adam Langleyfcf25832014-12-18 17:42:32 -0800769static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
770 if (s->next == NULL || s->prev == NULL) {
771 return;
772 }
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
775 /* last element in list */
776 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
777 /* only one element in list */
778 ctx->session_cache_head = NULL;
779 ctx->session_cache_tail = NULL;
780 } else {
781 ctx->session_cache_tail = s->prev;
782 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
783 }
784 } else {
785 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
786 /* first element in list */
787 ctx->session_cache_head = s->next;
788 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
789 } else { /* middle of list */
790 s->next->prev = s->prev;
791 s->prev->next = s->next;
792 }
793 }
794 s->prev = s->next = NULL;
795}
Adam Langley95c29f32014-06-20 12:00:00 -0700796
Adam Langleyfcf25832014-12-18 17:42:32 -0800797static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
798 if (s->next != NULL && s->prev != NULL) {
799 SSL_SESSION_list_remove(ctx, s);
800 }
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 if (ctx->session_cache_head == NULL) {
803 ctx->session_cache_head = s;
804 ctx->session_cache_tail = s;
805 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
806 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
807 } else {
808 s->next = ctx->session_cache_head;
809 s->next->prev = s;
810 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
811 ctx->session_cache_head = s;
812 }
813}
Adam Langley95c29f32014-06-20 12:00:00 -0700814
815void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
817 ctx->new_session_cb = cb;
818}
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
821 return ctx->new_session_cb;
822}
Adam Langley95c29f32014-06-20 12:00:00 -0700823
824void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
826 ctx->remove_session_cb = cb;
827}
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
830 SSL_SESSION *sess) {
831 return ctx->remove_session_cb;
832}
Adam Langley95c29f32014-06-20 12:00:00 -0700833
834void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 SSL_SESSION *(*cb)(struct ssl_st *ssl,
836 uint8_t *data, int len,
837 int *copy)) {
838 ctx->get_session_cb = cb;
839}
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
842 int len, int *copy) {
843 return ctx->get_session_cb;
844}
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846void SSL_CTX_set_info_callback(SSL_CTX *ctx,
847 void (*cb)(const SSL *ssl, int type, int val)) {
848 ctx->info_callback = cb;
849}
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
852 int val) {
853 return ctx->info_callback;
854}
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
857 EVP_PKEY **pkey)) {
858 ctx->client_cert_cb = cb;
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
862 EVP_PKEY **pkey) {
863 return ctx->client_cert_cb;
864}
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langley1258b6a2014-06-20 12:00:00 -0700866void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
868 ctx->channel_id_cb = cb;
869}
Adam Langley1258b6a2014-06-20 12:00:00 -0700870
Adam Langleyfcf25832014-12-18 17:42:32 -0800871void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
872 return ctx->channel_id_cb;
873}
Adam Langley1258b6a2014-06-20 12:00:00 -0700874
Adam Langley95c29f32014-06-20 12:00:00 -0700875IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)