1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) cryptsoft.com). This product includes software written by Tim 107 * Hudson (tjh (at) 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 #include <string.h> 138 139 #include <openssl/err.h> 140 #include <openssl/lhash.h> 141 #include <openssl/mem.h> 142 #include <openssl/rand.h> 143 144 #include "internal.h" 145 #include "../crypto/internal.h" 146 147 148 /* The address of this is a magic value, a pointer to which is returned by 149 * SSL_magic_pending_session_ptr(). It allows a session callback to indicate 150 * that it needs to asynchronously fetch session information. */ 151 static const char g_pending_session_magic = 0; 152 153 static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; 154 155 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 156 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); 157 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); 158 159 SSL_SESSION *SSL_magic_pending_session_ptr(void) { 160 return (SSL_SESSION *)&g_pending_session_magic; 161 } 162 163 SSL_SESSION *SSL_get_session(const SSL *ssl) 164 { 165 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ 166 return ssl->session; 167 } 168 169 SSL_SESSION *SSL_get1_session(SSL *ssl) { 170 /* variant of SSL_get_session: caller really gets something */ 171 return SSL_SESSION_up_ref(ssl->session); 172 } 173 174 int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 175 CRYPTO_EX_dup *dup_func, 176 CRYPTO_EX_free *free_func) { 177 int index; 178 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func, 179 dup_func, free_func)) { 180 return -1; 181 } 182 return index; 183 } 184 185 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) { 186 return CRYPTO_set_ex_data(&s->ex_data, idx, arg); 187 } 188 189 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) { 190 return CRYPTO_get_ex_data(&s->ex_data, idx); 191 } 192 193 SSL_SESSION *SSL_SESSION_new(void) { 194 SSL_SESSION *ss; 195 196 ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)); 197 if (ss == NULL) { 198 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE); 199 return 0; 200 } 201 memset(ss, 0, sizeof(SSL_SESSION)); 202 203 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ 204 ss->references = 1; 205 ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT; 206 ss->time = (unsigned long)time(NULL); 207 CRYPTO_new_ex_data(&g_ex_data_class, ss, &ss->ex_data); 208 return ss; 209 } 210 211 const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) { 212 if (len) { 213 *len = s->session_id_length; 214 } 215 return s->session_id; 216 } 217 218 /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. 219 * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random 220 * gunk repeatedly until we have no conflict is going to complete in one 221 * iteration pretty much "most" of the time (btw: understatement). So, if it 222 * takes us 10 iterations and we still can't avoid a conflict - well that's a 223 * reasonable point to call it quits. Either the RAND code is broken or someone 224 * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our 225 * server. How you might store that many sessions is perhaps a more interesting 226 * question ... */ 227 static int def_generate_session_id(const SSL *ssl, uint8_t *id, 228 unsigned int *id_len) { 229 static const unsigned kMaxAttempts = 10; 230 unsigned int retry = 0; 231 do { 232 if (!RAND_bytes(id, *id_len)) { 233 return 0; 234 } 235 } while (SSL_has_matching_session_id(ssl, id, *id_len) && 236 (++retry < kMaxAttempts)); 237 238 if (retry < kMaxAttempts) { 239 return 1; 240 } 241 242 /* else - woops a session_id match */ 243 /* XXX We should also check the external cache -- but the probability of a 244 * collision is negligible, and we could not prevent the concurrent creation 245 * of sessions with identical IDs since we currently don't have means to 246 * atomically check whether a session ID already exists and make a 247 * reservation for it if it does not (this problem applies to the internal 248 * cache as well). */ 249 return 0; 250 } 251 252 int ssl_get_new_session(SSL *s, int session) { 253 /* This gets used by clients and servers. */ 254 255 unsigned int tmp; 256 SSL_SESSION *ss = NULL; 257 GEN_SESSION_CB cb = def_generate_session_id; 258 259 if (s->mode & SSL_MODE_NO_SESSION_CREATION) { 260 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, 261 SSL_R_SESSION_MAY_NOT_BE_CREATED); 262 return 0; 263 } 264 265 ss = SSL_SESSION_new(); 266 if (ss == NULL) { 267 return 0; 268 } 269 270 /* If the context has a default timeout, use it over the default. */ 271 if (s->initial_ctx->session_timeout != 0) { 272 ss->timeout = s->initial_ctx->session_timeout; 273 } 274 275 SSL_SESSION_free(s->session); 276 s->session = NULL; 277 278 if (session) { 279 if (s->version == SSL3_VERSION || s->version == TLS1_VERSION || 280 s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION || 281 s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) { 282 ss->ssl_version = s->version; 283 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 284 } else { 285 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, 286 SSL_R_UNSUPPORTED_SSL_VERSION); 287 SSL_SESSION_free(ss); 288 return 0; 289 } 290 291 /* If RFC4507 ticket use empty session ID */ 292 if (s->tlsext_ticket_expected) { 293 ss->session_id_length = 0; 294 goto sess_id_done; 295 } 296 297 /* Choose which callback will set the session ID */ 298 if (s->generate_session_id) { 299 cb = s->generate_session_id; 300 } else if (s->initial_ctx->generate_session_id) { 301 cb = s->initial_ctx->generate_session_id; 302 } 303 304 /* Choose a session ID */ 305 tmp = ss->session_id_length; 306 if (!cb(s, ss->session_id, &tmp)) { 307 /* The callback failed */ 308 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, 309 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); 310 SSL_SESSION_free(ss); 311 return 0; 312 } 313 314 /* Don't allow the callback to set the session length to zero. nor set it 315 * higher than it was. */ 316 if (!tmp || tmp > ss->session_id_length) { 317 /* The callback set an illegal length */ 318 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, 319 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); 320 SSL_SESSION_free(ss); 321 return 0; 322 } 323 324 ss->session_id_length = tmp; 325 /* Finally, check for a conflict */ 326 if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) { 327 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, 328 SSL_R_SSL_SESSION_ID_CONFLICT); 329 SSL_SESSION_free(ss); 330 return 0; 331 } 332 333 sess_id_done: 334 if (s->tlsext_hostname) { 335 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname); 336 if (ss->tlsext_hostname == NULL) { 337 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR); 338 SSL_SESSION_free(ss); 339 return 0; 340 } 341 } 342 } else { 343 ss->session_id_length = 0; 344 } 345 346 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) { 347 OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR); 348 SSL_SESSION_free(ss); 349 return 0; 350 } 351 352 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); 353 ss->sid_ctx_length = s->sid_ctx_length; 354 s->session = ss; 355 ss->ssl_version = s->version; 356 ss->verify_result = X509_V_OK; 357 358 return 1; 359 } 360 361 /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this 362 * connection. It is only called by servers. 363 * 364 * ctx: contains the early callback context, which is the result of a 365 * shallow parse of the ClientHello. 366 * 367 * Returns: 368 * -1: error 369 * 0: a session may have been found. 370 * 371 * Side effects: 372 * - If a session is found then s->session is pointed at it (after freeing an 373 * existing session if need be) and s->verify_result is set from the session. 374 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1 375 * if the server should issue a new session ticket (to 0 otherwise). */ 376 int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx) { 377 /* This is used only by servers. */ 378 SSL_SESSION *ret = NULL; 379 int fatal = 0; 380 int try_session_cache = 1; 381 int r; 382 383 if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { 384 goto err; 385 } 386 387 if (ctx->session_id_len == 0) { 388 try_session_cache = 0; 389 } 390 391 r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */ 392 switch (r) { 393 case -1: /* Error during processing */ 394 fatal = 1; 395 goto err; 396 397 case 0: /* No ticket found */ 398 case 1: /* Zero length ticket found */ 399 break; /* Ok to carry on processing session id. */ 400 401 case 2: /* Ticket found but not decrypted. */ 402 case 3: /* Ticket decrypted, *ret has been set. */ 403 try_session_cache = 0; 404 break; 405 406 default: 407 abort(); 408 } 409 410 if (try_session_cache && ret == NULL && 411 !(s->initial_ctx->session_cache_mode & 412 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { 413 SSL_SESSION data; 414 data.ssl_version = s->version; 415 data.session_id_length = ctx->session_id_len; 416 if (ctx->session_id_len == 0) { 417 return 0; 418 } 419 memcpy(data.session_id, ctx->session_id, ctx->session_id_len); 420 421 CRYPTO_MUTEX_lock_read(&s->initial_ctx->lock); 422 ret = lh_SSL_SESSION_retrieve(s->initial_ctx->sessions, &data); 423 CRYPTO_MUTEX_unlock(&s->initial_ctx->lock); 424 425 if (ret != NULL) { 426 SSL_SESSION_up_ref(ret); 427 } 428 } 429 430 if (try_session_cache && ret == NULL && 431 s->initial_ctx->get_session_cb != NULL) { 432 int copy = 1; 433 434 ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id, 435 ctx->session_id_len, ©); 436 if (ret != NULL) { 437 if (ret == SSL_magic_pending_session_ptr()) { 438 /* This is a magic value which indicates that the callback needs to 439 * unwind the stack and figure out the session asynchronously. */ 440 return PENDING_SESSION; 441 } 442 443 /* Increment reference count now if the session callback asks us to do so 444 * (note that if the session structures returned by the callback are 445 * shared between threads, it must handle the reference count itself 446 * [i.e. copy == 0], or things won't be thread-safe). */ 447 if (copy) { 448 SSL_SESSION_up_ref(ret); 449 } 450 451 /* Add the externally cached session to the internal cache as well if and 452 * only if we are supposed to. */ 453 if (!(s->initial_ctx->session_cache_mode & 454 SSL_SESS_CACHE_NO_INTERNAL_STORE)) { 455 /* The following should not return 1, otherwise, things are very 456 * strange */ 457 SSL_CTX_add_session(s->initial_ctx, ret); 458 } 459 } 460 } 461 462 if (ret == NULL) { 463 goto err; 464 } 465 466 /* Now ret is non-NULL and we own one of its reference counts. */ 467 468 if (ret->sid_ctx_length != s->sid_ctx_length || 469 memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { 470 /* We have the session requested by the client, but we don't want to use it 471 * in this context. */ 472 goto err; /* treat like cache miss */ 473 } 474 475 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { 476 /* We can't be sure if this session is being used out of context, which is 477 * especially important for SSL_VERIFY_PEER. The application should have 478 * used SSL[_CTX]_set_session_id_context. 479 * 480 * For this error case, we generate an error instead of treating the event 481 * like a cache miss (otherwise it would be easy for applications to 482 * effectively disable the session cache by accident without anyone 483 * noticing). */ 484 OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, 485 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 486 fatal = 1; 487 goto err; 488 } 489 490 if (ret->timeout < (long)(time(NULL) - ret->time)) { 491 /* timeout */ 492 if (try_session_cache) { 493 /* session was from the cache, so remove it */ 494 SSL_CTX_remove_session(s->initial_ctx, ret); 495 } 496 goto err; 497 } 498 499 SSL_SESSION_free(s->session); 500 s->session = ret; 501 s->verify_result = s->session->verify_result; 502 return 1; 503 504 err: 505 if (ret != NULL) { 506 SSL_SESSION_free(ret); 507 if (!try_session_cache) { 508 /* The session was from a ticket, so we should 509 * issue a ticket for the new session */ 510 s->tlsext_ticket_expected = 1; 511 } 512 } 513 if (fatal) { 514 return -1; 515 } 516 return 0; 517 } 518 519 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) { 520 int ret = 0; 521 SSL_SESSION *s; 522 523 /* add just 1 reference count for the SSL_CTX's session cache even though it 524 * has two ways of access: each session is in a doubly linked list and an 525 * lhash */ 526 SSL_SESSION_up_ref(c); 527 /* if session c is in already in cache, we take back the increment later */ 528 529 CRYPTO_MUTEX_lock_write(&ctx->lock); 530 if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) { 531 CRYPTO_MUTEX_unlock(&ctx->lock); 532 return 0; 533 } 534 535 /* 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 } 548 549 /* 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 } 553 554 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; 562 563 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 } 569 } 570 } 571 572 CRYPTO_MUTEX_unlock(&ctx->lock); 573 return ret; 574 } 575 576 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) { 577 return remove_session_lock(ctx, c, 1); 578 } 579 580 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) { 581 SSL_SESSION *r; 582 int ret = 0; 583 584 if (c != NULL && c->session_id_length != 0) { 585 if (lock) { 586 CRYPTO_MUTEX_lock_write(&ctx->lock); 587 } 588 r = lh_SSL_SESSION_retrieve(ctx->sessions, c); 589 if (r == c) { 590 ret = 1; 591 r = lh_SSL_SESSION_delete(ctx->sessions, c); 592 SSL_SESSION_list_remove(ctx, c); 593 } 594 595 if (lock) { 596 CRYPTO_MUTEX_unlock(&ctx->lock); 597 } 598 599 if (ret) { 600 r->not_resumable = 1; 601 if (ctx->remove_session_cb != NULL) { 602 ctx->remove_session_cb(ctx, r); 603 } 604 SSL_SESSION_free(r); 605 } 606 } 607 608 return ret; 609 } 610 611 SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) { 612 if (session) { 613 CRYPTO_refcount_inc(&session->references); 614 } 615 return session; 616 } 617 618 void SSL_SESSION_free(SSL_SESSION *session) { 619 if (session == NULL || 620 !CRYPTO_refcount_dec_and_test_zero(&session->references)) { 621 return; 622 } 623 624 CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data); 625 626 OPENSSL_cleanse(session->master_key, sizeof(session->master_key)); 627 OPENSSL_cleanse(session->session_id, sizeof(session->session_id)); 628 ssl_sess_cert_free(session->sess_cert); 629 X509_free(session->peer); 630 OPENSSL_free(session->tlsext_hostname); 631 OPENSSL_free(session->tlsext_tick); 632 OPENSSL_free(session->tlsext_signed_cert_timestamp_list); 633 OPENSSL_free(session->ocsp_response); 634 OPENSSL_free(session->psk_identity); 635 OPENSSL_cleanse(session, sizeof(*session)); 636 OPENSSL_free(session); 637 } 638 639 int SSL_set_session(SSL *s, SSL_SESSION *session) { 640 if (s->session == session) { 641 return 1; 642 } 643 644 SSL_SESSION_free(s->session); 645 s->session = session; 646 if (session != NULL) { 647 SSL_SESSION_up_ref(session); 648 s->verify_result = session->verify_result; 649 } 650 651 return 1; 652 } 653 654 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) { 655 if (s == NULL) { 656 return 0; 657 } 658 659 s->timeout = t; 660 return 1; 661 } 662 663 long SSL_SESSION_get_timeout(const SSL_SESSION *s) { 664 if (s == NULL) { 665 return 0; 666 } 667 668 return s->timeout; 669 } 670 671 long SSL_SESSION_get_time(const SSL_SESSION *s) { 672 if (s == NULL) { 673 return 0; 674 } 675 676 return s->time; 677 } 678 679 long SSL_SESSION_set_time(SSL_SESSION *s, long t) { 680 if (s == NULL) { 681 return 0; 682 } 683 684 s->time = t; 685 return t; 686 } 687 688 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; } 689 690 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx, 691 unsigned int sid_ctx_len) { 692 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 693 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, 694 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 695 return 0; 696 } 697 698 s->sid_ctx_length = sid_ctx_len; 699 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); 700 701 return 1; 702 } 703 704 long SSL_CTX_set_timeout(SSL_CTX *s, long t) { 705 long l; 706 if (s == NULL) { 707 return 0; 708 } 709 710 l = s->session_timeout; 711 s->session_timeout = t; 712 return l; 713 } 714 715 long SSL_CTX_get_timeout(const SSL_CTX *s) { 716 if (s == NULL) { 717 return 0; 718 } 719 720 return s->session_timeout; 721 } 722 723 typedef struct timeout_param_st { 724 SSL_CTX *ctx; 725 long time; 726 LHASH_OF(SSL_SESSION) *cache; 727 } TIMEOUT_PARAM; 728 729 static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) { 730 TIMEOUT_PARAM *param = void_param; 731 732 if (param->time == 0 || 733 param->time > (sess->time + sess->timeout)) { 734 /* timeout */ 735 /* The reason we don't call SSL_CTX_remove_session() is to 736 * save on locking overhead */ 737 (void) lh_SSL_SESSION_delete(param->cache, sess); 738 SSL_SESSION_list_remove(param->ctx, sess); 739 sess->not_resumable = 1; 740 if (param->ctx->remove_session_cb != NULL) { 741 param->ctx->remove_session_cb(param->ctx, sess); 742 } 743 SSL_SESSION_free(sess); 744 } 745 } 746 747 void SSL_CTX_flush_sessions(SSL_CTX *ctx, long t) { 748 TIMEOUT_PARAM tp; 749 750 tp.ctx = ctx; 751 tp.cache = ctx->sessions; 752 if (tp.cache == NULL) { 753 return; 754 } 755 tp.time = t; 756 CRYPTO_MUTEX_lock_write(&ctx->lock); 757 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp); 758 CRYPTO_MUTEX_unlock(&ctx->lock); 759 } 760 761 int ssl_clear_bad_session(SSL *s) { 762 if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) && 763 !SSL_in_init(s)) { 764 SSL_CTX_remove_session(s->ctx, s->session); 765 return 1; 766 } 767 768 return 0; 769 } 770 771 /* locked by SSL_CTX in the calling function */ 772 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) { 773 if (s->next == NULL || s->prev == NULL) { 774 return; 775 } 776 777 if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) { 778 /* last element in list */ 779 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) { 780 /* only one element in list */ 781 ctx->session_cache_head = NULL; 782 ctx->session_cache_tail = NULL; 783 } else { 784 ctx->session_cache_tail = s->prev; 785 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); 786 } 787 } else { 788 if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) { 789 /* first element in list */ 790 ctx->session_cache_head = s->next; 791 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); 792 } else { /* middle of list */ 793 s->next->prev = s->prev; 794 s->prev->next = s->next; 795 } 796 } 797 s->prev = s->next = NULL; 798 } 799 800 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) { 801 if (s->next != NULL && s->prev != NULL) { 802 SSL_SESSION_list_remove(ctx, s); 803 } 804 805 if (ctx->session_cache_head == NULL) { 806 ctx->session_cache_head = s; 807 ctx->session_cache_tail = s; 808 s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 809 s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 810 } else { 811 s->next = ctx->session_cache_head; 812 s->next->prev = s; 813 s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 814 ctx->session_cache_head = s; 815 } 816 } 817 818 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, 819 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) { 820 ctx->new_session_cb = cb; 821 } 822 823 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) { 824 return ctx->new_session_cb; 825 } 826 827 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, 828 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) { 829 ctx->remove_session_cb = cb; 830 } 831 832 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx, 833 SSL_SESSION *sess) { 834 return ctx->remove_session_cb; 835 } 836 837 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, 838 SSL_SESSION *(*cb)(struct ssl_st *ssl, 839 uint8_t *data, int len, 840 int *copy)) { 841 ctx->get_session_cb = cb; 842 } 843 844 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data, 845 int len, int *copy) { 846 return ctx->get_session_cb; 847 } 848 849 void SSL_CTX_set_info_callback(SSL_CTX *ctx, 850 void (*cb)(const SSL *ssl, int type, int val)) { 851 ctx->info_callback = cb; 852 } 853 854 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, 855 int val) { 856 return ctx->info_callback; 857 } 858 859 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509, 860 EVP_PKEY **pkey)) { 861 ctx->client_cert_cb = cb; 862 } 863 864 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509, 865 EVP_PKEY **pkey) { 866 return ctx->client_cert_cb; 867 } 868 869 void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx, 870 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) { 871 ctx->channel_id_cb = cb; 872 } 873 874 void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) { 875 return ctx->channel_id_cb; 876 } 877 878 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) 879