blob: c5069d8597fe13d700f70982d428f17cffcfb274 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
146/* The address of this is a magic value, a pointer to which is returned by
147 * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
148 * that it needs to asynchronously fetch session information. */
149static const char g_pending_session_magic = 0;
150
151static 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
155SSL_SESSION *SSL_magic_pending_session_ptr(void) {
156 return (SSL_SESSION *)&g_pending_session_magic;
157}
158
159SSL_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}
164
165SSL_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);
177
178 return sess;
179}
180
181int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
182 CRYPTO_EX_dup *dup_func,
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}
187
188int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) {
189 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
190}
191
192void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) {
193 return CRYPTO_get_ex_data(&s->ex_data, idx);
194}
195
196SSL_SESSION *SSL_SESSION_new(void) {
197 SSL_SESSION *ss;
198
199 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));
205
206 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}
367
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 *
371 * ctx: contains the early callback context, which is the result of a
372 * shallow parse of the ClientHello.
373 *
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
382 * 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;
389
390 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
391 goto err;
392 }
393
394 if (ctx->session_id_len == 0) {
395 try_session_cache = 0;
396 }
397
398 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;
403
404 case 0: /* No ticket found */
405 case 1: /* Zero length ticket found */
406 break; /* Ok to carry on processing session id. */
407
408 case 2: /* Ticket found but not decrypted. */
409 case 3: /* Ticket decrypted, *ret has been set. */
410 try_session_cache = 0;
411 break;
412
413 default:
414 abort();
415 }
416
417 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);
428 ret = lh_SSL_SESSION_retrieve(s->initial_ctx->sessions, &data);
429 if (ret != NULL) {
430 /* don't allow other threads to steal it: */
431 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION);
432 }
433 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
434 if (ret == NULL) {
435 s->initial_ctx->stats.sess_miss++;
436 }
437 }
438
439 if (try_session_cache && ret == NULL &&
440 s->initial_ctx->get_session_cb != NULL) {
441 int copy = 1;
442
443 ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id,
444 ctx->session_id_len, &copy);
445 if (ret != NULL) {
446 if (ret == SSL_magic_pending_session_ptr()) {
447 /* This is a magic value which indicates that the callback needs to
448 * unwind the stack and figure out the session asynchronously. */
449 return PENDING_SESSION;
450 }
451 s->initial_ctx->stats.sess_cb_hit++;
452
453 /* Increment reference count now if the session callback asks us to do so
454 * (note that if the session structures returned by the callback are
455 * shared between threads, it must handle the reference count itself
456 * [i.e. copy == 0], or things won't be thread-safe). */
457 if (copy) {
458 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION);
459 }
460
461 /* Add the externally cached session to the internal cache as well if and
462 * only if we are supposed to. */
463 if (!(s->initial_ctx->session_cache_mode &
464 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
465 /* The following should not return 1, otherwise, things are very
466 * strange */
467 SSL_CTX_add_session(s->initial_ctx, ret);
468 }
469 }
470 }
471
472 if (ret == NULL) {
473 goto err;
474 }
475
476 /* Now ret is non-NULL and we own one of its reference counts. */
477
478 if (ret->sid_ctx_length != s->sid_ctx_length ||
479 memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
480 /* We have the session requested by the client, but we don't want to use it
481 * in this context. */
482 goto err; /* treat like cache miss */
483 }
484
485 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
486 /* We can't be sure if this session is being used out of context, which is
487 * especially important for SSL_VERIFY_PEER. The application should have
488 * used SSL[_CTX]_set_session_id_context.
489 *
490 * For this error case, we generate an error instead of treating the event
491 * like a cache miss (otherwise it would be easy for applications to
492 * effectively disable the session cache by accident without anyone
493 * noticing). */
494 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session,
495 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
496 fatal = 1;
497 goto err;
498 }
499
500 if (ret->timeout < (long)(time(NULL) - ret->time)) {
501 /* timeout */
502 s->initial_ctx->stats.sess_timeout++;
503 if (try_session_cache) {
504 /* session was from the cache, so remove it */
505 SSL_CTX_remove_session(s->initial_ctx, ret);
506 }
507 goto err;
508 }
509
510 s->initial_ctx->stats.sess_hit++;
511
512 if (s->session != NULL) {
513 SSL_SESSION_free(s->session);
514 }
515 s->session = ret;
516 s->verify_result = s->session->verify_result;
517 return 1;
518
519err:
520 if (ret != NULL) {
521 SSL_SESSION_free(ret);
522 if (!try_session_cache) {
523 /* The session was from a ticket, so we should
524 * issue a ticket for the new session */
525 s->tlsext_ticket_expected = 1;
526 }
527 }
528 if (fatal) {
529 return -1;
530 }
531 return 0;
532}
533
534int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
535 int ret = 0;
536 SSL_SESSION *s;
537
538 /* add just 1 reference count for the SSL_CTX's session cache even though it
539 * has two ways of access: each session is in a doubly linked list and an
540 * lhash */
541 CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION);
542 /* if session c is in already in cache, we take back the increment later */
543
544 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
545 if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
546 return 0;
547 }
548
549 /* s != NULL iff we already had a session with the given PID. In this case, s
550 * == c should hold (then we did not really modify ctx->sessions), or we're
551 * in trouble. */
552 if (s != NULL && s != c) {
553 /* We *are* in trouble ... */
554 SSL_SESSION_list_remove(ctx, s);
555 SSL_SESSION_free(s);
556 /* ... so pretend the other session did not exist in cache (we cannot
557 * handle two SSL_SESSION structures with identical session ID in the same
558 * cache, which could happen e.g. when two threads concurrently obtain the
559 * same session from an external cache) */
560 s = NULL;
561 }
562
563 /* Put at the head of the queue unless it is already in the cache */
564 if (s == NULL) {
565 SSL_SESSION_list_add(ctx, c);
566 }
567
568 if (s != NULL) {
569 /* existing cache entry -- decrement previously incremented reference count
570 * because it already takes into account the cache */
571 SSL_SESSION_free(s); /* s == c */
572 ret = 0;
573 } else {
574 /* new cache entry -- remove old ones if cache has become too large */
575 ret = 1;
576
577 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
578 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
579 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
580 break;
581 }
582 ctx->stats.sess_cache_full++;
583 }
584 }
585 }
586
587 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
588 return ret;
589}
590
591int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
592 return remove_session_lock(ctx, c, 1);
593}
594
595static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
596 SSL_SESSION *r;
597 int ret = 0;
598
599 if (c != NULL && c->session_id_length != 0) {
600 if (lock) {
601 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
602 }
603 r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
604 if (r == c) {
605 ret = 1;
606 r = lh_SSL_SESSION_delete(ctx->sessions, c);
607 SSL_SESSION_list_remove(ctx, c);
608 }
609
610 if (lock) {
611 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
612 }
613
614 if (ret) {
615 r->not_resumable = 1;
616 if (ctx->remove_session_cb != NULL) {
617 ctx->remove_session_cb(ctx, r);
618 }
619 SSL_SESSION_free(r);
620 }
621 }
622
623 return ret;
624}
625
626void SSL_SESSION_free(SSL_SESSION *ss) {
627 int i;
628
629 if (ss == NULL) {
630 return;
631 }
632
633 i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
634 if (i > 0) {
635 return;
636 }
637
638 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
639
640 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
641 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
642 if (ss->sess_cert != NULL) {
643 ssl_sess_cert_free(ss->sess_cert);
644 }
645 if (ss->peer != NULL) {
646 X509_free(ss->peer);
647 }
648 if (ss->tlsext_hostname != NULL) {
649 OPENSSL_free(ss->tlsext_hostname);
650 }
651 if (ss->tlsext_tick != NULL) {
652 OPENSSL_free(ss->tlsext_tick);
653 }
654 if (ss->tlsext_signed_cert_timestamp_list != NULL) {
655 OPENSSL_free(ss->tlsext_signed_cert_timestamp_list);
656 }
657 if (ss->ocsp_response != NULL) {
658 OPENSSL_free(ss->ocsp_response);
659 }
660 if (ss->psk_identity != NULL) {
661 OPENSSL_free(ss->psk_identity);
662 }
663 OPENSSL_cleanse(ss, sizeof(*ss));
664 OPENSSL_free(ss);
665}
666
667int SSL_set_session(SSL *s, SSL_SESSION *session) {
668 if (s->session == session) {
669 return 1;
670 }
671
672 if (s->session != NULL) {
673 SSL_SESSION_free(s->session);
674 }
675 s->session = session;
676 if (session != NULL) {
677 CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
678 s->verify_result = session->verify_result;
679 }
680
681 return 1;
682}
683
684long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
685 if (s == NULL) {
686 return 0;
687 }
688
689 s->timeout = t;
690 return 1;
691}
692
693long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
694 if (s == NULL) {
695 return 0;
696 }
697
698 return s->timeout;
699}
700
701long SSL_SESSION_get_time(const SSL_SESSION *s) {
702 if (s == NULL) {
703 return 0;
704 }
705
706 return s->time;
707}
708
709long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
710 if (s == NULL) {
711 return 0;
712 }
713
714 s->time = t;
715 return t;
716}
717
718X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
719
720int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
721 unsigned int sid_ctx_len) {
722 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
723 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context,
724 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
725 return 0;
726 }
727
728 s->sid_ctx_length = sid_ctx_len;
729 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
730
731 return 1;
732}
733
734long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
735 long l;
736 if (s == NULL) {
737 return 0;
738 }
739
740 l = s->session_timeout;
741 s->session_timeout = t;
742 return l;
743}
744
745long SSL_CTX_get_timeout(const SSL_CTX *s) {
746 if (s == NULL) {
747 return 0;
748 }
749
750 return s->session_timeout;
751}
752
753typedef struct timeout_param_st {
754 SSL_CTX *ctx;
755 long time;
756 LHASH_OF(SSL_SESSION) * cache;
757} TIMEOUT_PARAM;
758
759static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
760 TIMEOUT_PARAM *param = void_param;
761
762 if (param->time == 0 ||
763 param->time > (sess->time + sess->timeout)) {
764 /* timeout */
765 /* The reason we don't call SSL_CTX_remove_session() is to
766 * save on locking overhead */
767 (void) lh_SSL_SESSION_delete(param->cache, sess);
768 SSL_SESSION_list_remove(param->ctx, sess);
769 sess->not_resumable = 1;
770 if (param->ctx->remove_session_cb != NULL) {
771 param->ctx->remove_session_cb(param->ctx, sess);
772 }
773 SSL_SESSION_free(sess);
774 }
775}
776
777void SSL_CTX_flush_sessions(SSL_CTX *s, long t) {
778 TIMEOUT_PARAM tp;
779
780 tp.ctx = s;
781 tp.cache = s->sessions;
782 if (tp.cache == NULL) {
783 return;
784 }
785 tp.time = t;
786 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
787 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
788 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
789}
790
791int ssl_clear_bad_session(SSL *s) {
792 if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
793 !SSL_in_init(s)) {
794 SSL_CTX_remove_session(s->ctx, s->session);
795 return 1;
796 }
797
798 return 0;
799}
800
801/* locked by SSL_CTX in the calling function */
802static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
803 if (s->next == NULL || s->prev == NULL) {
804 return;
805 }
806
807 if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
808 /* last element in list */
809 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
810 /* only one element in list */
811 ctx->session_cache_head = NULL;
812 ctx->session_cache_tail = NULL;
813 } else {
814 ctx->session_cache_tail = s->prev;
815 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
816 }
817 } else {
818 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
819 /* first element in list */
820 ctx->session_cache_head = s->next;
821 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
822 } else { /* middle of list */
823 s->next->prev = s->prev;
824 s->prev->next = s->next;
825 }
826 }
827 s->prev = s->next = NULL;
828}
829
830static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
831 if (s->next != NULL && s->prev != NULL) {
832 SSL_SESSION_list_remove(ctx, s);
833 }
834
835 if (ctx->session_cache_head == NULL) {
836 ctx->session_cache_head = s;
837 ctx->session_cache_tail = s;
838 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
839 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
840 } else {
841 s->next = ctx->session_cache_head;
842 s->next->prev = s;
843 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
844 ctx->session_cache_head = s;
845 }
846}
847
848void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
849 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
850 ctx->new_session_cb = cb;
851}
852
853int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
854 return ctx->new_session_cb;
855}
856
857void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
858 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
859 ctx->remove_session_cb = cb;
860}
861
862void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
863 SSL_SESSION *sess) {
864 return ctx->remove_session_cb;
865}
866
867void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
868 SSL_SESSION *(*cb)(struct ssl_st *ssl,
869 uint8_t *data, int len,
870 int *copy)) {
871 ctx->get_session_cb = cb;
872}
873
874SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
875 int len, int *copy) {
876 return ctx->get_session_cb;
877}
878
879void SSL_CTX_set_info_callback(SSL_CTX *ctx,
880 void (*cb)(const SSL *ssl, int type, int val)) {
881 ctx->info_callback = cb;
882}
883
884void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
885 int val) {
886 return ctx->info_callback;
887}
888
889void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
890 EVP_PKEY **pkey)) {
891 ctx->client_cert_cb = cb;
892}
893
894int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
895 EVP_PKEY **pkey) {
896 return ctx->client_cert_cb;
897}
898
899void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
900 int (*cb)(SSL *ssl, uint8_t *cookie,
901 size_t *cookie_len)) {
902 ctx->app_gen_cookie_cb = cb;
903}
904
905void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
906 int (*cb)(SSL *ssl, const uint8_t *cookie,
907 size_t cookie_len)) {
908 ctx->app_verify_cookie_cb = cb;
909}
910
911void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
912 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
913 ctx->channel_id_cb = cb;
914}
915
916void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
917 return ctx->channel_id_cb;
918}
919
920IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)