blob: c6913fc59cfd9b3816b395f6c4bde4ff75cc44f0 [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 */
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);
420 if (ret == NULL) {
421 s->initial_ctx->stats.sess_miss++;
422 }
423 }
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 if (try_session_cache && ret == NULL &&
426 s->initial_ctx->get_session_cb != NULL) {
427 int copy = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id,
430 ctx->session_id_len, &copy);
431 if (ret != NULL) {
432 if (ret == SSL_magic_pending_session_ptr()) {
433 /* This is a magic value which indicates that the callback needs to
434 * unwind the stack and figure out the session asynchronously. */
435 return PENDING_SESSION;
436 }
437 s->initial_ctx->stats.sess_cb_hit++;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 /* Increment reference count now if the session callback asks us to do so
440 * (note that if the session structures returned by the callback are
441 * shared between threads, it must handle the reference count itself
442 * [i.e. copy == 0], or things won't be thread-safe). */
443 if (copy) {
David Benjamin33639842015-02-09 03:34:47 -0500444 SSL_SESSION_up_ref(ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 }
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 /* Add the externally cached session to the internal cache as well if and
448 * only if we are supposed to. */
449 if (!(s->initial_ctx->session_cache_mode &
450 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
451 /* The following should not return 1, otherwise, things are very
452 * strange */
453 SSL_CTX_add_session(s->initial_ctx, ret);
454 }
455 }
456 }
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 if (ret == NULL) {
459 goto err;
460 }
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 /* Now ret is non-NULL and we own one of its reference counts. */
Adam Langley95c29f32014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 if (ret->sid_ctx_length != s->sid_ctx_length ||
465 memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
466 /* We have the session requested by the client, but we don't want to use it
467 * in this context. */
468 goto err; /* treat like cache miss */
469 }
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
472 /* We can't be sure if this session is being used out of context, which is
473 * especially important for SSL_VERIFY_PEER. The application should have
474 * used SSL[_CTX]_set_session_id_context.
475 *
476 * For this error case, we generate an error instead of treating the event
477 * like a cache miss (otherwise it would be easy for applications to
478 * effectively disable the session cache by accident without anyone
479 * noticing). */
480 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session,
481 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
482 fatal = 1;
483 goto err;
484 }
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 if (ret->timeout < (long)(time(NULL) - ret->time)) {
487 /* timeout */
488 s->initial_ctx->stats.sess_timeout++;
489 if (try_session_cache) {
490 /* session was from the cache, so remove it */
491 SSL_CTX_remove_session(s->initial_ctx, ret);
492 }
493 goto err;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 s->initial_ctx->stats.sess_hit++;
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 if (s->session != NULL) {
499 SSL_SESSION_free(s->session);
500 }
501 s->session = ret;
502 s->verify_result = s->session->verify_result;
503 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505err:
506 if (ret != NULL) {
507 SSL_SESSION_free(ret);
508 if (!try_session_cache) {
509 /* The session was from a ticket, so we should
510 * issue a ticket for the new session */
511 s->tlsext_ticket_expected = 1;
512 }
513 }
514 if (fatal) {
515 return -1;
516 }
517 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700518}
519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
521 int ret = 0;
522 SSL_SESSION *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 /* add just 1 reference count for the SSL_CTX's session cache even though it
525 * has two ways of access: each session is in a doubly linked list and an
526 * lhash */
David Benjamin33639842015-02-09 03:34:47 -0500527 SSL_SESSION_up_ref(c);
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* if session c is in already in cache, we take back the increment later */
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
531 if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
532 return 0;
533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* s != NULL iff we already had a session with the given PID. In this case, s
536 * == c should hold (then we did not really modify ctx->sessions), or we're
537 * in trouble. */
538 if (s != NULL && s != c) {
539 /* We *are* in trouble ... */
540 SSL_SESSION_list_remove(ctx, s);
541 SSL_SESSION_free(s);
542 /* ... so pretend the other session did not exist in cache (we cannot
543 * handle two SSL_SESSION structures with identical session ID in the same
544 * cache, which could happen e.g. when two threads concurrently obtain the
545 * same session from an external cache) */
546 s = NULL;
547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 /* Put at the head of the queue unless it is already in the cache */
550 if (s == NULL) {
551 SSL_SESSION_list_add(ctx, c);
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 if (s != NULL) {
555 /* existing cache entry -- decrement previously incremented reference count
556 * because it already takes into account the cache */
557 SSL_SESSION_free(s); /* s == c */
558 ret = 0;
559 } else {
560 /* new cache entry -- remove old ones if cache has become too large */
561 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
564 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
565 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
566 break;
567 }
568 ctx->stats.sess_cache_full++;
569 }
570 }
571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
574 return ret;
575}
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
578 return remove_session_lock(ctx, c, 1);
579}
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
582 SSL_SESSION *r;
583 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 if (c != NULL && c->session_id_length != 0) {
586 if (lock) {
587 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
588 }
589 r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
590 if (r == c) {
591 ret = 1;
592 r = lh_SSL_SESSION_delete(ctx->sessions, c);
593 SSL_SESSION_list_remove(ctx, c);
594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (lock) {
597 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
598 }
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 if (ret) {
601 r->not_resumable = 1;
602 if (ctx->remove_session_cb != NULL) {
603 ctx->remove_session_cb(ctx, r);
604 }
605 SSL_SESSION_free(r);
606 }
607 }
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 return ret;
610}
Adam Langley95c29f32014-06-20 12:00:00 -0700611
David Benjamin33639842015-02-09 03:34:47 -0500612SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
613 if (session) {
614 CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
615 }
616 return session;
617}
618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619void SSL_SESSION_free(SSL_SESSION *ss) {
620 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 if (ss == NULL) {
623 return;
624 }
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
627 if (i > 0) {
628 return;
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
634 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
635 if (ss->sess_cert != NULL) {
636 ssl_sess_cert_free(ss->sess_cert);
637 }
638 if (ss->peer != NULL) {
639 X509_free(ss->peer);
640 }
641 if (ss->tlsext_hostname != NULL) {
642 OPENSSL_free(ss->tlsext_hostname);
643 }
644 if (ss->tlsext_tick != NULL) {
645 OPENSSL_free(ss->tlsext_tick);
646 }
647 if (ss->tlsext_signed_cert_timestamp_list != NULL) {
648 OPENSSL_free(ss->tlsext_signed_cert_timestamp_list);
649 }
650 if (ss->ocsp_response != NULL) {
651 OPENSSL_free(ss->ocsp_response);
652 }
653 if (ss->psk_identity != NULL) {
654 OPENSSL_free(ss->psk_identity);
655 }
656 OPENSSL_cleanse(ss, sizeof(*ss));
657 OPENSSL_free(ss);
658}
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660int SSL_set_session(SSL *s, SSL_SESSION *session) {
661 if (s->session == session) {
662 return 1;
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 if (s->session != NULL) {
666 SSL_SESSION_free(s->session);
667 }
668 s->session = session;
669 if (session != NULL) {
David Benjamin33639842015-02-09 03:34:47 -0500670 SSL_SESSION_up_ref(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 s->verify_result = session->verify_result;
672 }
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 return 1;
675}
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langleyfcf25832014-12-18 17:42:32 -0800677long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
678 if (s == NULL) {
679 return 0;
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 s->timeout = t;
683 return 1;
684}
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langleyfcf25832014-12-18 17:42:32 -0800686long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
687 if (s == NULL) {
688 return 0;
689 }
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 return s->timeout;
692}
693
694long SSL_SESSION_get_time(const SSL_SESSION *s) {
695 if (s == NULL) {
696 return 0;
697 }
698
699 return s->time;
700}
701
702long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
703 if (s == NULL) {
704 return 0;
705 }
706
707 s->time = t;
708 return t;
709}
710
711X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
712
713int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
714 unsigned int sid_ctx_len) {
715 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
716 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context,
717 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
718 return 0;
719 }
720
721 s->sid_ctx_length = sid_ctx_len;
722 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
723
724 return 1;
725}
726
727long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
728 long l;
729 if (s == NULL) {
730 return 0;
731 }
732
733 l = s->session_timeout;
734 s->session_timeout = t;
735 return l;
736}
737
738long SSL_CTX_get_timeout(const SSL_CTX *s) {
739 if (s == NULL) {
740 return 0;
741 }
742
743 return s->session_timeout;
744}
745
746typedef struct timeout_param_st {
747 SSL_CTX *ctx;
748 long time;
749 LHASH_OF(SSL_SESSION) * cache;
750} TIMEOUT_PARAM;
751
752static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
753 TIMEOUT_PARAM *param = void_param;
754
755 if (param->time == 0 ||
756 param->time > (sess->time + sess->timeout)) {
757 /* timeout */
758 /* The reason we don't call SSL_CTX_remove_session() is to
759 * save on locking overhead */
Adam Langleya307dfd2015-01-09 15:42:58 -0800760 (void) lh_SSL_SESSION_delete(param->cache, sess);
Adam Langleyfcf25832014-12-18 17:42:32 -0800761 SSL_SESSION_list_remove(param->ctx, sess);
762 sess->not_resumable = 1;
763 if (param->ctx->remove_session_cb != NULL) {
764 param->ctx->remove_session_cb(param->ctx, sess);
765 }
766 SSL_SESSION_free(sess);
767 }
768}
769
770void SSL_CTX_flush_sessions(SSL_CTX *s, long t) {
771 TIMEOUT_PARAM tp;
772
773 tp.ctx = s;
774 tp.cache = s->sessions;
775 if (tp.cache == NULL) {
776 return;
777 }
778 tp.time = t;
779 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
780 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
781 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
782}
783
784int ssl_clear_bad_session(SSL *s) {
785 if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
786 !SSL_in_init(s)) {
787 SSL_CTX_remove_session(s->ctx, s->session);
788 return 1;
789 }
790
791 return 0;
792}
Adam Langley95c29f32014-06-20 12:00:00 -0700793
794/* locked by SSL_CTX in the calling function */
Adam Langleyfcf25832014-12-18 17:42:32 -0800795static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
796 if (s->next == NULL || s->prev == NULL) {
797 return;
798 }
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
801 /* last element in list */
802 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
803 /* only one element in list */
804 ctx->session_cache_head = NULL;
805 ctx->session_cache_tail = NULL;
806 } else {
807 ctx->session_cache_tail = s->prev;
808 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
809 }
810 } else {
811 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
812 /* first element in list */
813 ctx->session_cache_head = s->next;
814 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
815 } else { /* middle of list */
816 s->next->prev = s->prev;
817 s->prev->next = s->next;
818 }
819 }
820 s->prev = s->next = NULL;
821}
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Adam Langleyfcf25832014-12-18 17:42:32 -0800823static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
824 if (s->next != NULL && s->prev != NULL) {
825 SSL_SESSION_list_remove(ctx, s);
826 }
Adam Langley95c29f32014-06-20 12:00:00 -0700827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 if (ctx->session_cache_head == NULL) {
829 ctx->session_cache_head = s;
830 ctx->session_cache_tail = s;
831 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
832 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
833 } else {
834 s->next = ctx->session_cache_head;
835 s->next->prev = s;
836 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
837 ctx->session_cache_head = s;
838 }
839}
Adam Langley95c29f32014-06-20 12:00:00 -0700840
841void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800842 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
843 ctx->new_session_cb = cb;
844}
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
847 return ctx->new_session_cb;
848}
Adam Langley95c29f32014-06-20 12:00:00 -0700849
850void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
852 ctx->remove_session_cb = cb;
853}
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
856 SSL_SESSION *sess) {
857 return ctx->remove_session_cb;
858}
Adam Langley95c29f32014-06-20 12:00:00 -0700859
860void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 SSL_SESSION *(*cb)(struct ssl_st *ssl,
862 uint8_t *data, int len,
863 int *copy)) {
864 ctx->get_session_cb = cb;
865}
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
868 int len, int *copy) {
869 return ctx->get_session_cb;
870}
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872void SSL_CTX_set_info_callback(SSL_CTX *ctx,
873 void (*cb)(const SSL *ssl, int type, int val)) {
874 ctx->info_callback = cb;
875}
Adam Langley95c29f32014-06-20 12:00:00 -0700876
Adam Langleyfcf25832014-12-18 17:42:32 -0800877void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
878 int val) {
879 return ctx->info_callback;
880}
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
883 EVP_PKEY **pkey)) {
884 ctx->client_cert_cb = cb;
885}
Adam Langley95c29f32014-06-20 12:00:00 -0700886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
888 EVP_PKEY **pkey) {
889 return ctx->client_cert_cb;
890}
Adam Langley95c29f32014-06-20 12:00:00 -0700891
892void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 int (*cb)(SSL *ssl, uint8_t *cookie,
894 size_t *cookie_len)) {
895 ctx->app_gen_cookie_cb = cb;
896}
Adam Langley95c29f32014-06-20 12:00:00 -0700897
898void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 int (*cb)(SSL *ssl, const uint8_t *cookie,
900 size_t cookie_len)) {
901 ctx->app_verify_cookie_cb = cb;
902}
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langley1258b6a2014-06-20 12:00:00 -0700904void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
906 ctx->channel_id_cb = cb;
907}
Adam Langley1258b6a2014-06-20 12:00:00 -0700908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
910 return ctx->channel_id_cb;
911}
Adam Langley1258b6a2014-06-20 12:00:00 -0700912
Adam Langley95c29f32014-06-20 12:00:00 -0700913IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)