blob: 97dc10f2b2cbb353ce3feee34ee7495a27976cf3 [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. */
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);
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 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 */
167 SSL_SESSION *sess;
168 /* Need to lock this all up rather than just use CRYPTO_add so that
169 * somebody doesn't free ssl->session between when we check it's
170 * non-null and when we up the reference count. */
171 CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
172 sess = ssl->session;
173 if (sess) {
174 sess->references++;
175 }
176 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 return sess;
179}
Adam Langley8eaaa862014-08-11 17:18:25 -0700180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
182 CRYPTO_EX_dup *dup_func,
183 CRYPTO_EX_free *free_func) {
184 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
185 new_func, dup_func, free_func);
186}
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langleyfcf25832014-12-18 17:42:32 -0800188int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) {
189 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
190}
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) {
193 return CRYPTO_get_ex_data(&s->ex_data, idx);
194}
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196SSL_SESSION *SSL_SESSION_new(void) {
197 SSL_SESSION *ss;
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
200 if (ss == NULL) {
201 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
202 return 0;
203 }
204 memset(ss, 0, sizeof(SSL_SESSION));
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
207 ss->references = 1;
208 ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
209 ss->time = (unsigned long)time(NULL);
210 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
211 return ss;
212}
213
214const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) {
215 if (len) {
216 *len = s->session_id_length;
217 }
218 return s->session_id;
219}
220
221/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space.
222 * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random
223 * gunk repeatedly until we have no conflict is going to complete in one
224 * iteration pretty much "most" of the time (btw: understatement). So, if it
225 * takes us 10 iterations and we still can't avoid a conflict - well that's a
226 * reasonable point to call it quits. Either the RAND code is broken or someone
227 * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our
228 * server. How you might store that many sessions is perhaps a more interesting
229 * question ... */
230static int def_generate_session_id(const SSL *ssl, uint8_t *id,
231 unsigned int *id_len) {
232 static const unsigned kMaxAttempts = 10;
233 unsigned int retry = 0;
234 do {
235 if (!RAND_bytes(id, *id_len)) {
236 return 0;
237 }
238 } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
239 (++retry < kMaxAttempts));
240
241 if (retry < kMaxAttempts) {
242 return 1;
243 }
244
245 /* else - woops a session_id match */
246 /* XXX We should also check the external cache -- but the probability of a
247 * collision is negligible, and we could not prevent the concurrent creation
248 * of sessions with identical IDs since we currently don't have means to
249 * atomically check whether a session ID already exists and make a
250 * reservation for it if it does not (this problem applies to the internal
251 * cache as well). */
252 return 0;
253}
254
255int ssl_get_new_session(SSL *s, int session) {
256 /* This gets used by clients and servers. */
257
258 unsigned int tmp;
259 SSL_SESSION *ss = NULL;
260 GEN_SESSION_CB cb = def_generate_session_id;
261
262 if (s->mode & SSL_MODE_NO_SESSION_CREATION) {
263 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
264 SSL_R_SESSION_MAY_NOT_BE_CREATED);
265 return 0;
266 }
267
268 ss = SSL_SESSION_new();
269 if (ss == NULL) {
270 return 0;
271 }
272
273 /* If the context has a default timeout, use it over the default. */
274 if (s->initial_ctx->session_timeout != 0) {
275 ss->timeout = s->initial_ctx->session_timeout;
276 }
277
278 if (s->session != NULL) {
279 SSL_SESSION_free(s->session);
280 s->session = NULL;
281 }
282
283 if (session) {
284 if (s->version == SSL3_VERSION || s->version == TLS1_VERSION ||
285 s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION ||
286 s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) {
287 ss->ssl_version = s->version;
288 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
289 } else {
290 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
291 SSL_R_UNSUPPORTED_SSL_VERSION);
292 SSL_SESSION_free(ss);
293 return 0;
294 }
295
296 /* If RFC4507 ticket use empty session ID */
297 if (s->tlsext_ticket_expected) {
298 ss->session_id_length = 0;
299 goto sess_id_done;
300 }
301
302 /* Choose which callback will set the session ID */
303 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
304 if (s->generate_session_id) {
305 cb = s->generate_session_id;
306 } else if (s->initial_ctx->generate_session_id) {
307 cb = s->initial_ctx->generate_session_id;
308 }
309 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
310
311 /* Choose a session ID */
312 tmp = ss->session_id_length;
313 if (!cb(s, ss->session_id, &tmp)) {
314 /* The callback failed */
315 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
316 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
317 SSL_SESSION_free(ss);
318 return 0;
319 }
320
321 /* Don't allow the callback to set the session length to zero. nor set it
322 * higher than it was. */
323 if (!tmp || tmp > ss->session_id_length) {
324 /* The callback set an illegal length */
325 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
326 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
327 SSL_SESSION_free(ss);
328 return 0;
329 }
330
331 ss->session_id_length = tmp;
332 /* Finally, check for a conflict */
333 if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) {
334 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
335 SSL_R_SSL_SESSION_ID_CONFLICT);
336 SSL_SESSION_free(ss);
337 return 0;
338 }
339
340 sess_id_done:
341 if (s->tlsext_hostname) {
342 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
343 if (ss->tlsext_hostname == NULL) {
344 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
345 SSL_SESSION_free(ss);
346 return 0;
347 }
348 }
349 } else {
350 ss->session_id_length = 0;
351 }
352
353 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
354 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
355 SSL_SESSION_free(ss);
356 return 0;
357 }
358
359 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
360 ss->sid_ctx_length = s->sid_ctx_length;
361 s->session = ss;
362 ss->ssl_version = s->version;
363 ss->verify_result = X509_V_OK;
364
365 return 1;
366}
Adam Langley95c29f32014-06-20 12:00:00 -0700367
368/* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
369 * connection. It is only called by servers.
370 *
Adam Langleydc9b1412014-06-20 12:00:00 -0700371 * ctx: contains the early callback context, which is the result of a
372 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -0700373 *
374 * Returns:
375 * -1: error
376 * 0: a session may have been found.
377 *
378 * Side effects:
379 * - If a session is found then s->session is pointed at it (after freeing an
380 * existing session if need be) and s->verify_result is set from the session.
381 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 * if the server should issue a new session ticket (to 0 otherwise). */
383int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx) {
384 /* This is used only by servers. */
385 SSL_SESSION *ret = NULL;
386 int fatal = 0;
387 int try_session_cache = 1;
388 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
391 goto err;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 if (ctx->session_id_len == 0) {
395 try_session_cache = 0;
396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
399 switch (r) {
400 case -1: /* Error during processing */
401 fatal = 1;
402 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 case 0: /* No ticket found */
405 case 1: /* Zero length ticket found */
406 break; /* Ok to carry on processing session id. */
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 case 2: /* Ticket found but not decrypted. */
409 case 3: /* Ticket decrypted, *ret has been set. */
410 try_session_cache = 0;
411 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 default:
414 abort();
415 }
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 if (try_session_cache && ret == NULL &&
418 !(s->initial_ctx->session_cache_mode &
419 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
420 SSL_SESSION data;
421 data.ssl_version = s->version;
422 data.session_id_length = ctx->session_id_len;
423 if (ctx->session_id_len == 0) {
424 return 0;
425 }
426 memcpy(data.session_id, ctx->session_id, ctx->session_id_len);
427 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
David Benjamin33639842015-02-09 03:34:47 -0500428 ret = SSL_SESSION_up_ref(lh_SSL_SESSION_retrieve(s->initial_ctx->sessions,
429 &data));
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
431 if (ret == NULL) {
432 s->initial_ctx->stats.sess_miss++;
433 }
434 }
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 if (try_session_cache && ret == NULL &&
437 s->initial_ctx->get_session_cb != NULL) {
438 int copy = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id,
441 ctx->session_id_len, &copy);
442 if (ret != NULL) {
443 if (ret == SSL_magic_pending_session_ptr()) {
444 /* This is a magic value which indicates that the callback needs to
445 * unwind the stack and figure out the session asynchronously. */
446 return PENDING_SESSION;
447 }
448 s->initial_ctx->stats.sess_cb_hit++;
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 /* Increment reference count now if the session callback asks us to do so
451 * (note that if the session structures returned by the callback are
452 * shared between threads, it must handle the reference count itself
453 * [i.e. copy == 0], or things won't be thread-safe). */
454 if (copy) {
David Benjamin33639842015-02-09 03:34:47 -0500455 SSL_SESSION_up_ref(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 }
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 /* Add the externally cached session to the internal cache as well if and
459 * only if we are supposed to. */
460 if (!(s->initial_ctx->session_cache_mode &
461 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
462 /* The following should not return 1, otherwise, things are very
463 * strange */
464 SSL_CTX_add_session(s->initial_ctx, ret);
465 }
466 }
467 }
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 if (ret == NULL) {
470 goto err;
471 }
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 /* Now ret is non-NULL and we own one of its reference counts. */
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 if (ret->sid_ctx_length != s->sid_ctx_length ||
476 memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
477 /* We have the session requested by the client, but we don't want to use it
478 * in this context. */
479 goto err; /* treat like cache miss */
480 }
Adam Langley95c29f32014-06-20 12:00:00 -0700481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
483 /* We can't be sure if this session is being used out of context, which is
484 * especially important for SSL_VERIFY_PEER. The application should have
485 * used SSL[_CTX]_set_session_id_context.
486 *
487 * For this error case, we generate an error instead of treating the event
488 * like a cache miss (otherwise it would be easy for applications to
489 * effectively disable the session cache by accident without anyone
490 * noticing). */
491 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session,
492 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
493 fatal = 1;
494 goto err;
495 }
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 if (ret->timeout < (long)(time(NULL) - ret->time)) {
498 /* timeout */
499 s->initial_ctx->stats.sess_timeout++;
500 if (try_session_cache) {
501 /* session was from the cache, so remove it */
502 SSL_CTX_remove_session(s->initial_ctx, ret);
503 }
504 goto err;
505 }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 s->initial_ctx->stats.sess_hit++;
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 if (s->session != NULL) {
510 SSL_SESSION_free(s->session);
511 }
512 s->session = ret;
513 s->verify_result = s->session->verify_result;
514 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516err:
517 if (ret != NULL) {
518 SSL_SESSION_free(ret);
519 if (!try_session_cache) {
520 /* The session was from a ticket, so we should
521 * issue a ticket for the new session */
522 s->tlsext_ticket_expected = 1;
523 }
524 }
525 if (fatal) {
526 return -1;
527 }
528 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700529}
530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
532 int ret = 0;
533 SSL_SESSION *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* add just 1 reference count for the SSL_CTX's session cache even though it
536 * has two ways of access: each session is in a doubly linked list and an
537 * lhash */
David Benjamin33639842015-02-09 03:34:47 -0500538 SSL_SESSION_up_ref(c);
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 /* if session c is in already in cache, we take back the increment later */
Adam Langley95c29f32014-06-20 12:00:00 -0700540
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
542 if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
543 return 0;
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 /* s != NULL iff we already had a session with the given PID. In this case, s
547 * == c should hold (then we did not really modify ctx->sessions), or we're
548 * in trouble. */
549 if (s != NULL && s != c) {
550 /* We *are* in trouble ... */
551 SSL_SESSION_list_remove(ctx, s);
552 SSL_SESSION_free(s);
553 /* ... so pretend the other session did not exist in cache (we cannot
554 * handle two SSL_SESSION structures with identical session ID in the same
555 * cache, which could happen e.g. when two threads concurrently obtain the
556 * same session from an external cache) */
557 s = NULL;
558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 /* Put at the head of the queue unless it is already in the cache */
561 if (s == NULL) {
562 SSL_SESSION_list_add(ctx, c);
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 if (s != NULL) {
566 /* existing cache entry -- decrement previously incremented reference count
567 * because it already takes into account the cache */
568 SSL_SESSION_free(s); /* s == c */
569 ret = 0;
570 } else {
571 /* new cache entry -- remove old ones if cache has become too large */
572 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
575 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
576 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
577 break;
578 }
579 ctx->stats.sess_cache_full++;
580 }
581 }
582 }
Adam Langley95c29f32014-06-20 12:00:00 -0700583
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
585 return ret;
586}
Adam Langley95c29f32014-06-20 12:00:00 -0700587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
589 return remove_session_lock(ctx, c, 1);
590}
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
593 SSL_SESSION *r;
594 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (c != NULL && c->session_id_length != 0) {
597 if (lock) {
598 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
599 }
600 r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
601 if (r == c) {
602 ret = 1;
603 r = lh_SSL_SESSION_delete(ctx->sessions, c);
604 SSL_SESSION_list_remove(ctx, c);
605 }
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 if (lock) {
608 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 if (ret) {
612 r->not_resumable = 1;
613 if (ctx->remove_session_cb != NULL) {
614 ctx->remove_session_cb(ctx, r);
615 }
616 SSL_SESSION_free(r);
617 }
618 }
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 return ret;
621}
Adam Langley95c29f32014-06-20 12:00:00 -0700622
David Benjamin33639842015-02-09 03:34:47 -0500623SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
624 if (session) {
625 CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
626 }
627 return session;
628}
629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630void SSL_SESSION_free(SSL_SESSION *ss) {
631 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 if (ss == NULL) {
634 return;
635 }
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
638 if (i > 0) {
639 return;
640 }
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
645 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
646 if (ss->sess_cert != NULL) {
647 ssl_sess_cert_free(ss->sess_cert);
648 }
649 if (ss->peer != NULL) {
650 X509_free(ss->peer);
651 }
652 if (ss->tlsext_hostname != NULL) {
653 OPENSSL_free(ss->tlsext_hostname);
654 }
655 if (ss->tlsext_tick != NULL) {
656 OPENSSL_free(ss->tlsext_tick);
657 }
658 if (ss->tlsext_signed_cert_timestamp_list != NULL) {
659 OPENSSL_free(ss->tlsext_signed_cert_timestamp_list);
660 }
661 if (ss->ocsp_response != NULL) {
662 OPENSSL_free(ss->ocsp_response);
663 }
664 if (ss->psk_identity != NULL) {
665 OPENSSL_free(ss->psk_identity);
666 }
667 OPENSSL_cleanse(ss, sizeof(*ss));
668 OPENSSL_free(ss);
669}
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671int SSL_set_session(SSL *s, SSL_SESSION *session) {
672 if (s->session == session) {
673 return 1;
674 }
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (s->session != NULL) {
677 SSL_SESSION_free(s->session);
678 }
679 s->session = session;
680 if (session != NULL) {
David Benjamin33639842015-02-09 03:34:47 -0500681 SSL_SESSION_up_ref(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 s->verify_result = session->verify_result;
683 }
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 return 1;
686}
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langleyfcf25832014-12-18 17:42:32 -0800688long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
689 if (s == NULL) {
690 return 0;
691 }
Adam Langley95c29f32014-06-20 12:00:00 -0700692
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 s->timeout = t;
694 return 1;
695}
Adam Langley95c29f32014-06-20 12:00:00 -0700696
Adam Langleyfcf25832014-12-18 17:42:32 -0800697long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
698 if (s == NULL) {
699 return 0;
700 }
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 return s->timeout;
703}
704
705long SSL_SESSION_get_time(const SSL_SESSION *s) {
706 if (s == NULL) {
707 return 0;
708 }
709
710 return s->time;
711}
712
713long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
714 if (s == NULL) {
715 return 0;
716 }
717
718 s->time = t;
719 return t;
720}
721
722X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
723
724int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
725 unsigned int sid_ctx_len) {
726 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
727 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context,
728 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
729 return 0;
730 }
731
732 s->sid_ctx_length = sid_ctx_len;
733 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
734
735 return 1;
736}
737
738long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
739 long l;
740 if (s == NULL) {
741 return 0;
742 }
743
744 l = s->session_timeout;
745 s->session_timeout = t;
746 return l;
747}
748
749long SSL_CTX_get_timeout(const SSL_CTX *s) {
750 if (s == NULL) {
751 return 0;
752 }
753
754 return s->session_timeout;
755}
756
757typedef struct timeout_param_st {
758 SSL_CTX *ctx;
759 long time;
760 LHASH_OF(SSL_SESSION) * cache;
761} TIMEOUT_PARAM;
762
763static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
764 TIMEOUT_PARAM *param = void_param;
765
766 if (param->time == 0 ||
767 param->time > (sess->time + sess->timeout)) {
768 /* timeout */
769 /* The reason we don't call SSL_CTX_remove_session() is to
770 * save on locking overhead */
Adam Langleya307dfd2015-01-09 15:42:58 -0800771 (void) lh_SSL_SESSION_delete(param->cache, sess);
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 SSL_SESSION_list_remove(param->ctx, sess);
773 sess->not_resumable = 1;
774 if (param->ctx->remove_session_cb != NULL) {
775 param->ctx->remove_session_cb(param->ctx, sess);
776 }
777 SSL_SESSION_free(sess);
778 }
779}
780
781void SSL_CTX_flush_sessions(SSL_CTX *s, long t) {
782 TIMEOUT_PARAM tp;
783
784 tp.ctx = s;
785 tp.cache = s->sessions;
786 if (tp.cache == NULL) {
787 return;
788 }
789 tp.time = t;
790 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
791 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
792 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
793}
794
795int ssl_clear_bad_session(SSL *s) {
796 if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
797 !SSL_in_init(s)) {
798 SSL_CTX_remove_session(s->ctx, s->session);
799 return 1;
800 }
801
802 return 0;
803}
Adam Langley95c29f32014-06-20 12:00:00 -0700804
805/* locked by SSL_CTX in the calling function */
Adam Langleyfcf25832014-12-18 17:42:32 -0800806static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
807 if (s->next == NULL || s->prev == NULL) {
808 return;
809 }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
812 /* last element in list */
813 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
814 /* only one element in list */
815 ctx->session_cache_head = NULL;
816 ctx->session_cache_tail = NULL;
817 } else {
818 ctx->session_cache_tail = s->prev;
819 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
820 }
821 } else {
822 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
823 /* first element in list */
824 ctx->session_cache_head = s->next;
825 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
826 } else { /* middle of list */
827 s->next->prev = s->prev;
828 s->prev->next = s->next;
829 }
830 }
831 s->prev = s->next = NULL;
832}
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyfcf25832014-12-18 17:42:32 -0800834static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
835 if (s->next != NULL && s->prev != NULL) {
836 SSL_SESSION_list_remove(ctx, s);
837 }
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (ctx->session_cache_head == NULL) {
840 ctx->session_cache_head = s;
841 ctx->session_cache_tail = s;
842 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
843 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
844 } else {
845 s->next = ctx->session_cache_head;
846 s->next->prev = s;
847 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
848 ctx->session_cache_head = s;
849 }
850}
Adam Langley95c29f32014-06-20 12:00:00 -0700851
852void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
854 ctx->new_session_cb = cb;
855}
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
858 return ctx->new_session_cb;
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
861void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
863 ctx->remove_session_cb = cb;
864}
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
867 SSL_SESSION *sess) {
868 return ctx->remove_session_cb;
869}
Adam Langley95c29f32014-06-20 12:00:00 -0700870
871void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 SSL_SESSION *(*cb)(struct ssl_st *ssl,
873 uint8_t *data, int len,
874 int *copy)) {
875 ctx->get_session_cb = cb;
876}
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
879 int len, int *copy) {
880 return ctx->get_session_cb;
881}
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883void SSL_CTX_set_info_callback(SSL_CTX *ctx,
884 void (*cb)(const SSL *ssl, int type, int val)) {
885 ctx->info_callback = cb;
886}
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
889 int val) {
890 return ctx->info_callback;
891}
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
894 EVP_PKEY **pkey)) {
895 ctx->client_cert_cb = cb;
896}
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
899 EVP_PKEY **pkey) {
900 return ctx->client_cert_cb;
901}
Adam Langley95c29f32014-06-20 12:00:00 -0700902
903void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 int (*cb)(SSL *ssl, uint8_t *cookie,
905 size_t *cookie_len)) {
906 ctx->app_gen_cookie_cb = cb;
907}
Adam Langley95c29f32014-06-20 12:00:00 -0700908
909void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 int (*cb)(SSL *ssl, const uint8_t *cookie,
911 size_t cookie_len)) {
912 ctx->app_verify_cookie_cb = cb;
913}
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langley1258b6a2014-06-20 12:00:00 -0700915void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
917 ctx->channel_id_cb = cb;
918}
Adam Langley1258b6a2014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
921 return ctx->channel_id_cb;
922}
Adam Langley1258b6a2014-06-20 12:00:00 -0700923
Adam Langley95c29f32014-06-20 12:00:00 -0700924IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)