ssl_sess.c revision 1.22
1/* ssl/ssl_sess.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58/* ==================================================================== 59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111/* ==================================================================== 112 * Copyright 2005 Nokia. All rights reserved. 113 * 114 * The portions of the attached software ("Contribution") is developed by 115 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 116 * license. 117 * 118 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 119 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 120 * support (see RFC 4279) to OpenSSL. 121 * 122 * No patent licenses or other rights except those expressly stated in 123 * the OpenSSL open source license shall be deemed granted or received 124 * expressly, by implication, estoppel, or otherwise. 125 * 126 * No assurances are provided by Nokia that the Contribution does not 127 * infringe the patent or other intellectual property rights of any third 128 * party or that the license provides you with all the necessary rights 129 * to make use of the Contribution. 130 * 131 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 132 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 133 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 134 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 135 * OTHERWISE. 136 */ 137 138#include <stdio.h> 139#include <openssl/lhash.h> 140#include <openssl/rand.h> 141#ifndef OPENSSL_NO_ENGINE 142#include <openssl/engine.h> 143#endif 144#include "ssl_locl.h" 145 146static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 147static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); 148static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); 149 150SSL_SESSION 151*SSL_get_session(const SSL *ssl) 152/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ 153{ 154 return (ssl->session); 155} 156 157SSL_SESSION 158*SSL_get1_session(SSL *ssl) 159/* variant of SSL_get_session: caller really gets something */ 160{ 161 SSL_SESSION *sess; 162 /* Need to lock this all up rather than just use CRYPTO_add so that 163 * somebody doesn't free ssl->session between when we check it's 164 * non-null and when we up the reference count. */ 165 CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION); 166 sess = ssl->session; 167 if (sess) 168 sess->references++; 169 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION); 170 return (sess); 171} 172 173int 174SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 175 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 176{ 177 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp, 178 new_func, dup_func, free_func); 179} 180 181int 182SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) 183{ 184 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 185} 186 187void 188*SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) 189{ 190 return (CRYPTO_get_ex_data(&s->ex_data, idx)); 191} 192 193SSL_SESSION 194*SSL_SESSION_new(void) 195{ 196 SSL_SESSION *ss; 197 198 ss = (SSL_SESSION *)malloc(sizeof(SSL_SESSION)); 199 if (ss == NULL) { 200 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE); 201 return (0); 202 } 203 memset(ss, 0, sizeof(SSL_SESSION)); 204 205 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ 206 ss->references = 1; 207 ss->timeout=60*5+4; /* 5 minute timeout by default */ 208 ss->time = (unsigned long)time(NULL); 209 ss->prev = NULL; 210 ss->next = NULL; 211 ss->compress_meth = 0; 212#ifndef OPENSSL_NO_TLSEXT 213 ss->tlsext_hostname = NULL; 214 215#ifndef OPENSSL_NO_EC 216 ss->tlsext_ecpointformatlist_length = 0; 217 ss->tlsext_ecpointformatlist = NULL; 218 ss->tlsext_ellipticcurvelist_length = 0; 219 ss->tlsext_ellipticcurvelist = NULL; 220#endif 221#endif 222 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); 223#ifndef OPENSSL_NO_PSK 224 ss->psk_identity_hint = NULL; 225 ss->psk_identity = NULL; 226#endif 227#ifndef OPENSSL_NO_SRP 228 ss->srp_username = NULL; 229#endif 230 return (ss); 231} 232 233const unsigned char 234*SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) 235{ 236 if (len) 237 *len = s->session_id_length; 238 return s->session_id; 239} 240 241unsigned int 242SSL_SESSION_get_compress_id(const SSL_SESSION *s) 243{ 244 return s->compress_meth; 245} 246 247/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1 248 * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly 249 * until we have no conflict is going to complete in one iteration pretty much 250 * "most" of the time (btw: understatement). So, if it takes us 10 iterations 251 * and we still can't avoid a conflict - well that's a reasonable point to call 252 * it quits. Either the RAND code is broken or someone is trying to open roughly 253 * very close to 2^128 (or 2^256) SSL sessions to our server. How you might 254 * store that many sessions is perhaps a more interesting question ... */ 255 256#define MAX_SESS_ID_ATTEMPTS 10 257static int 258def_generate_session_id(const SSL *ssl, unsigned char *id, 259 unsigned int *id_len) 260{ 261 unsigned int retry = 0; 262 do 263 if (RAND_pseudo_bytes(id, *id_len) <= 0) 264 return 0; 265 while (SSL_has_matching_session_id(ssl, id, *id_len) && 266 (++retry < MAX_SESS_ID_ATTEMPTS)); 267 if (retry < MAX_SESS_ID_ATTEMPTS) 268 return 1; 269 /* else - woops a session_id match */ 270 /* XXX We should also check the external cache -- 271 * but the probability of a collision is negligible, and 272 * we could not prevent the concurrent creation of sessions 273 * with identical IDs since we currently don't have means 274 * to atomically check whether a session ID already exists 275 * and make a reservation for it if it does not 276 * (this problem applies to the internal cache as well). 277 */ 278 return 0; 279} 280 281int 282ssl_get_new_session(SSL *s, int session) 283{ 284 /* This gets used by clients and servers. */ 285 286 unsigned int tmp; 287 SSL_SESSION *ss = NULL; 288 GEN_SESSION_CB cb = def_generate_session_id; 289 290 if ((ss = SSL_SESSION_new()) == NULL) return (0); 291 292 /* If the context has a default timeout, use it */ 293 if (s->session_ctx->session_timeout == 0) 294 ss->timeout = SSL_get_default_timeout(s); 295 else 296 ss->timeout = s->session_ctx->session_timeout; 297 298 if (s->session != NULL) { 299 SSL_SESSION_free(s->session); 300 s->session = NULL; 301 } 302 303 if (session) { 304 switch (s->version) { 305 case SSL3_VERSION: 306 case TLS1_VERSION: 307 case TLS1_1_VERSION: 308 case TLS1_2_VERSION: 309 case DTLS1_BAD_VER: 310 case DTLS1_VERSION: 311 ss->ssl_version = s->version; 312 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 313 break; 314 default: 315 SSLerr(SSL_F_SSL_GET_NEW_SESSION, 316 SSL_R_UNSUPPORTED_SSL_VERSION); 317 SSL_SESSION_free(ss); 318 return (0); 319 } 320#ifndef OPENSSL_NO_TLSEXT 321 /* If RFC4507 ticket use empty session ID */ 322 if (s->tlsext_ticket_expected) { 323 ss->session_id_length = 0; 324 goto sess_id_done; 325 } 326#endif 327 /* Choose which callback will set the session ID */ 328 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 329 if (s->generate_session_id) 330 cb = s->generate_session_id; 331 else if (s->session_ctx->generate_session_id) 332 cb = s->session_ctx->generate_session_id; 333 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 334 /* Choose a session ID */ 335 tmp = ss->session_id_length; 336 if (!cb(s, ss->session_id, &tmp)) { 337 /* The callback failed */ 338 SSLerr(SSL_F_SSL_GET_NEW_SESSION, 339 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); 340 SSL_SESSION_free(ss); 341 return (0); 342 } 343 /* Don't allow the callback to set the session length to zero. 344 * nor set it higher than it was. */ 345 if (!tmp || (tmp > ss->session_id_length)) { 346 /* The callback set an illegal length */ 347 SSLerr(SSL_F_SSL_GET_NEW_SESSION, 348 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); 349 SSL_SESSION_free(ss); 350 return (0); 351 } 352 ss->session_id_length = tmp; 353 /* Finally, check for a conflict */ 354 if (SSL_has_matching_session_id(s, ss->session_id, 355 ss->session_id_length)) { 356 SSLerr(SSL_F_SSL_GET_NEW_SESSION, 357 SSL_R_SSL_SESSION_ID_CONFLICT); 358 SSL_SESSION_free(ss); 359 return (0); 360 } 361#ifndef OPENSSL_NO_TLSEXT 362 sess_id_done: 363 if (s->tlsext_hostname) { 364 ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname); 365 if (ss->tlsext_hostname == NULL) { 366 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); 367 SSL_SESSION_free(ss); 368 return 0; 369 } 370 } 371#ifndef OPENSSL_NO_EC 372 if (s->tlsext_ecpointformatlist) { 373 if (ss->tlsext_ecpointformatlist != NULL) 374 free(ss->tlsext_ecpointformatlist); 375 if ((ss->tlsext_ecpointformatlist = malloc(s->tlsext_ecpointformatlist_length)) == NULL) { 376 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_MALLOC_FAILURE); 377 SSL_SESSION_free(ss); 378 return 0; 379 } 380 ss->tlsext_ecpointformatlist_length = s->tlsext_ecpointformatlist_length; 381 memcpy(ss->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length); 382 } 383 if (s->tlsext_ellipticcurvelist) { 384 if (ss->tlsext_ellipticcurvelist != NULL) 385 free(ss->tlsext_ellipticcurvelist); 386 if ((ss->tlsext_ellipticcurvelist = malloc(s->tlsext_ellipticcurvelist_length)) == NULL) { 387 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_MALLOC_FAILURE); 388 SSL_SESSION_free(ss); 389 return 0; 390 } 391 ss->tlsext_ellipticcurvelist_length = s->tlsext_ellipticcurvelist_length; 392 memcpy(ss->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length); 393 } 394#endif 395#endif 396 } else { 397 ss->session_id_length = 0; 398 } 399 400 if (s->sid_ctx_length > sizeof ss->sid_ctx) { 401 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); 402 SSL_SESSION_free(ss); 403 return 0; 404 } 405 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); 406 ss->sid_ctx_length = s->sid_ctx_length; 407 s->session = ss; 408 ss->ssl_version = s->version; 409 ss->verify_result = X509_V_OK; 410 411 return (1); 412} 413 414/* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this 415 * connection. It is only called by servers. 416 * 417 * session_id: points at the session ID in the ClientHello. This code will 418 * read past the end of this in order to parse out the session ticket 419 * extension, if any. 420 * len: the length of the session ID. 421 * limit: a pointer to the first byte after the ClientHello. 422 * 423 * Returns: 424 * -1: error 425 * 0: a session may have been found. 426 * 427 * Side effects: 428 * - If a session is found then s->session is pointed at it (after freeing an 429 * existing session if need be) and s->verify_result is set from the session. 430 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1 431 * if the server should issue a new session ticket (to 0 otherwise). 432 */ 433int 434ssl_get_prev_session(SSL *s, unsigned char *session_id, int len, 435 const unsigned char *limit) 436{ 437 /* This is used only by servers. */ 438 439 SSL_SESSION *ret = NULL; 440 int fatal = 0; 441 int try_session_cache = 1; 442#ifndef OPENSSL_NO_TLSEXT 443 int r; 444#endif 445 446 if (len > SSL_MAX_SSL_SESSION_ID_LENGTH) 447 goto err; 448 449 if (len == 0) 450 try_session_cache = 0; 451 452#ifndef OPENSSL_NO_TLSEXT 453 r = tls1_process_ticket(s, session_id, len, limit, &ret); /* sets s->tlsext_ticket_expected */ 454 switch (r) { 455 case -1: /* Error during processing */ 456 fatal = 1; 457 goto err; 458 case 0: /* No ticket found */ 459 case 1: /* Zero length ticket found */ 460 break; /* Ok to carry on processing session id. */ 461 case 2: /* Ticket found but not decrypted. */ 462 case 3: /* Ticket decrypted, *ret has been set. */ 463 try_session_cache = 0; 464 break; 465 default: 466 abort(); 467 } 468#endif 469 470 if (try_session_cache && 471 ret == NULL && 472 !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { 473 SSL_SESSION data; 474 data.ssl_version = s->version; 475 data.session_id_length = len; 476 if (len == 0) 477 return 0; 478 memcpy(data.session_id, session_id, len); 479 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 480 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); 481 if (ret != NULL) { 482 /* don't allow other threads to steal it: */ 483 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION); 484 } 485 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 486 if (ret == NULL) 487 s->session_ctx->stats.sess_miss++; 488 } 489 490 if (try_session_cache && 491 ret == NULL && 492 s->session_ctx->get_session_cb != NULL) { 493 int copy = 1; 494 495 if ((ret = s->session_ctx->get_session_cb(s, session_id, len, ©))) { 496 s->session_ctx->stats.sess_cb_hit++; 497 498 /* Increment reference count now if the session callback 499 * asks us to do so (note that if the session structures 500 * returned by the callback are shared between threads, 501 * it must handle the reference count itself [i.e. copy == 0], 502 * or things won't be thread-safe). */ 503 if (copy) 504 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION); 505 506 /* Add the externally cached session to the internal 507 * cache as well if and only if we are supposed to. */ 508 if (!(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE)) 509 /* The following should not return 1, otherwise, 510 * things are very strange */ 511 SSL_CTX_add_session(s->session_ctx, ret); 512 } 513 } 514 515 if (ret == NULL) 516 goto err; 517 518 /* Now ret is non-NULL and we own one of its reference counts. */ 519 520 if (ret->sid_ctx_length != s->sid_ctx_length 521 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { 522 /* We have the session requested by the client, but we don't 523 * want to use it in this context. */ 524 goto err; /* treat like cache miss */ 525 } 526 527 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { 528 /* We can't be sure if this session is being used out of 529 * context, which is especially important for SSL_VERIFY_PEER. 530 * The application should have used SSL[_CTX]_set_session_id_context. 531 * 532 * For this error case, we generate an error instead of treating 533 * the event like a cache miss (otherwise it would be easy for 534 * applications to effectively disable the session cache by 535 * accident without anyone noticing). 536 */ 537 538 SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 539 fatal = 1; 540 goto err; 541 } 542 543 if (ret->cipher == NULL) { 544 unsigned char buf[5], *p; 545 unsigned long l; 546 547 p = buf; 548 l = ret->cipher_id; 549 l2n(l, p); 550 if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR) 551 ret->cipher = ssl_get_cipher_by_char(s, &(buf[2])); 552 else 553 ret->cipher = ssl_get_cipher_by_char(s, &(buf[1])); 554 if (ret->cipher == NULL) 555 goto err; 556 } 557 558 if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */ 559 { 560 s->session_ctx->stats.sess_timeout++; 561 if (try_session_cache) { 562 /* session was from the cache, so remove it */ 563 SSL_CTX_remove_session(s->session_ctx, ret); 564 } 565 goto err; 566 } 567 568 s->session_ctx->stats.sess_hit++; 569 570 if (s->session != NULL) 571 SSL_SESSION_free(s->session); 572 s->session = ret; 573 s->verify_result = s->session->verify_result; 574 return 1; 575 576 err: 577 if (ret != NULL) { 578 SSL_SESSION_free(ret); 579#ifndef OPENSSL_NO_TLSEXT 580 if (!try_session_cache) { 581 /* The session was from a ticket, so we should 582 * issue a ticket for the new session */ 583 s->tlsext_ticket_expected = 1; 584 } 585#endif 586 } 587 if (fatal) 588 return -1; 589 else 590 return 0; 591} 592 593int 594SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) 595{ 596 int ret = 0; 597 SSL_SESSION *s; 598 599 /* add just 1 reference count for the SSL_CTX's session cache 600 * even though it has two ways of access: each session is in a 601 * doubly linked list and an lhash */ 602 CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION); 603 /* if session c is in already in cache, we take back the increment later */ 604 605 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 606 s = lh_SSL_SESSION_insert(ctx->sessions, c); 607 608 /* s != NULL iff we already had a session with the given PID. 609 * In this case, s == c should hold (then we did not really modify 610 * ctx->sessions), or we're in trouble. */ 611 if (s != NULL && s != c) { 612 /* We *are* in trouble ... */ 613 SSL_SESSION_list_remove(ctx, s); 614 SSL_SESSION_free(s); 615 /* ... so pretend the other session did not exist in cache 616 * (we cannot handle two SSL_SESSION structures with identical 617 * session ID in the same cache, which could happen e.g. when 618 * two threads concurrently obtain the same session from an external 619 * cache) */ 620 s = NULL; 621 } 622 623 /* Put at the head of the queue unless it is already in the cache */ 624 if (s == NULL) 625 SSL_SESSION_list_add(ctx, c); 626 627 if (s != NULL) { 628 /* existing cache entry -- decrement previously incremented reference 629 * count because it already takes into account the cache */ 630 631 SSL_SESSION_free(s); /* s == c */ 632 ret = 0; 633 } else { 634 /* new cache entry -- remove old ones if cache has become too large */ 635 636 ret = 1; 637 638 if (SSL_CTX_sess_get_cache_size(ctx) > 0) { 639 while (SSL_CTX_sess_number(ctx) > 640 SSL_CTX_sess_get_cache_size(ctx)) { 641 if (!remove_session_lock(ctx, 642 ctx->session_cache_tail, 0)) 643 break; 644 else 645 ctx->stats.sess_cache_full++; 646 } 647 } 648 } 649 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 650 return (ret); 651} 652 653int 654SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) 655{ 656 return remove_session_lock(ctx, c, 1); 657} 658 659static int 660remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) 661{ 662 SSL_SESSION *r; 663 int ret = 0; 664 665 if ((c != NULL) && (c->session_id_length != 0)) { 666 if (lck) 667 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 668 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) { 669 ret = 1; 670 r = lh_SSL_SESSION_delete(ctx->sessions, c); 671 SSL_SESSION_list_remove(ctx, c); 672 } 673 674 if (lck) 675 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 676 677 if (ret) { 678 r->not_resumable = 1; 679 if (ctx->remove_session_cb != NULL) 680 ctx->remove_session_cb(ctx, r); 681 SSL_SESSION_free(r); 682 } 683 } else 684 ret = 0; 685 return (ret); 686} 687 688void 689SSL_SESSION_free(SSL_SESSION *ss) 690{ 691 int i; 692 693 if (ss == NULL) 694 return; 695 696 i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION); 697 if (i > 0) 698 return; 699 700 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); 701 702 OPENSSL_cleanse(ss->key_arg, sizeof ss->key_arg); 703 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key); 704 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id); 705 if (ss->sess_cert != NULL) 706 ssl_sess_cert_free(ss->sess_cert); 707 if (ss->peer != NULL) 708 X509_free(ss->peer); 709 if (ss->ciphers != NULL) 710 sk_SSL_CIPHER_free(ss->ciphers); 711#ifndef OPENSSL_NO_TLSEXT 712 if (ss->tlsext_hostname != NULL) 713 free(ss->tlsext_hostname); 714 if (ss->tlsext_tick != NULL) 715 free(ss->tlsext_tick); 716#ifndef OPENSSL_NO_EC 717 ss->tlsext_ecpointformatlist_length = 0; 718 if (ss->tlsext_ecpointformatlist != NULL) 719 free(ss->tlsext_ecpointformatlist); 720 ss->tlsext_ellipticcurvelist_length = 0; 721 if (ss->tlsext_ellipticcurvelist != NULL) 722 free(ss->tlsext_ellipticcurvelist); 723#endif /* OPENSSL_NO_EC */ 724#endif 725#ifndef OPENSSL_NO_PSK 726 if (ss->psk_identity_hint != NULL) 727 free(ss->psk_identity_hint); 728 if (ss->psk_identity != NULL) 729 free(ss->psk_identity); 730#endif 731#ifndef OPENSSL_NO_SRP 732 if (ss->srp_username != NULL) 733 free(ss->srp_username); 734#endif 735 OPENSSL_cleanse(ss, sizeof(*ss)); 736 free(ss); 737} 738 739int 740SSL_set_session(SSL *s, SSL_SESSION *session) 741{ 742 int ret = 0; 743 const SSL_METHOD *meth; 744 745 if (session != NULL) { 746 meth = s->ctx->method->get_ssl_method(session->ssl_version); 747 if (meth == NULL) 748 meth = s->method->get_ssl_method(session->ssl_version); 749 if (meth == NULL) { 750 SSLerr(SSL_F_SSL_SET_SESSION, SSL_R_UNABLE_TO_FIND_SSL_METHOD); 751 return (0); 752 } 753 754 if (meth != s->method) { 755 if (!SSL_set_ssl_method(s, meth)) 756 return (0); 757 } 758 759#ifndef OPENSSL_NO_KRB5 760 if (s->kssl_ctx && !s->kssl_ctx->client_princ && 761 session->krb5_client_princ_len > 0) { 762 s->kssl_ctx->client_princ = (char *)malloc(session->krb5_client_princ_len + 1); 763 memcpy(s->kssl_ctx->client_princ, session->krb5_client_princ, 764 session->krb5_client_princ_len); 765 s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0'; 766 } 767#endif /* OPENSSL_NO_KRB5 */ 768 769 /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/ 770 CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION); 771 if (s->session != NULL) 772 SSL_SESSION_free(s->session); 773 s->session = session; 774 s->verify_result = s->session->verify_result; 775 /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/ 776 ret = 1; 777 } else { 778 if (s->session != NULL) { 779 SSL_SESSION_free(s->session); 780 s->session = NULL; 781 } 782 783 meth = s->ctx->method; 784 if (meth != s->method) { 785 if (!SSL_set_ssl_method(s, meth)) 786 return (0); 787 } 788 ret = 1; 789 } 790 return (ret); 791} 792 793long 794SSL_SESSION_set_timeout(SSL_SESSION *s, long t) 795{ 796 if (s == NULL) 797 return (0); 798 s->timeout = t; 799 return (1); 800} 801 802long 803SSL_SESSION_get_timeout(const SSL_SESSION *s) 804{ 805 if (s == NULL) 806 return (0); 807 return (s->timeout); 808} 809 810long 811SSL_SESSION_get_time(const SSL_SESSION *s) 812{ 813 if (s == NULL) 814 return (0); 815 return (s->time); 816} 817 818long 819SSL_SESSION_set_time(SSL_SESSION *s, long t) 820{ 821 if (s == NULL) 822 return (0); 823 s->time = t; 824 return (t); 825} 826 827X509 828*SSL_SESSION_get0_peer(SSL_SESSION *s) 829{ 830 return s->peer; 831} 832 833int 834SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, 835 unsigned int sid_ctx_len) 836{ 837 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 838 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 839 return 0; 840 } 841 s->sid_ctx_length = sid_ctx_len; 842 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); 843 844 return 1; 845} 846 847long 848SSL_CTX_set_timeout(SSL_CTX *s, long t) 849{ 850 long l; 851 if (s == NULL) 852 return (0); 853 l = s->session_timeout; 854 s->session_timeout = t; 855 return (l); 856} 857 858long 859SSL_CTX_get_timeout(const SSL_CTX *s) 860{ 861 if (s == NULL) 862 return (0); 863 return (s->session_timeout); 864} 865 866#ifndef OPENSSL_NO_TLSEXT 867int 868SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len, 869 STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg) 870{ 871 if (s == NULL) 872 return (0); 873 s->tls_session_secret_cb = tls_session_secret_cb; 874 s->tls_session_secret_cb_arg = arg; 875 return (1); 876} 877 878int 879SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, 880 void *arg) 881{ 882 if (s == NULL) 883 return (0); 884 s->tls_session_ticket_ext_cb = cb; 885 s->tls_session_ticket_ext_cb_arg = arg; 886 return (1); 887} 888 889int 890SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) 891{ 892 if (s->version >= TLS1_VERSION) { 893 if (s->tlsext_session_ticket) { 894 free(s->tlsext_session_ticket); 895 s->tlsext_session_ticket = NULL; 896 } 897 898 s->tlsext_session_ticket = malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); 899 if (!s->tlsext_session_ticket) { 900 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE); 901 return 0; 902 } 903 904 if (ext_data) { 905 s->tlsext_session_ticket->length = ext_len; 906 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1; 907 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len); 908 } else { 909 s->tlsext_session_ticket->length = 0; 910 s->tlsext_session_ticket->data = NULL; 911 } 912 913 return 1; 914 } 915 916 return 0; 917} 918#endif /* OPENSSL_NO_TLSEXT */ 919 920typedef struct timeout_param_st { 921 SSL_CTX *ctx; 922 long time; 923 LHASH_OF(SSL_SESSION) *cache; 924} TIMEOUT_PARAM; 925 926static void 927timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p) 928{ 929 if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */ 930 { 931 /* The reason we don't call SSL_CTX_remove_session() is to 932 * save on locking overhead */ 933 (void)lh_SSL_SESSION_delete(p->cache, s); 934 SSL_SESSION_list_remove(p->ctx, s); 935 s->not_resumable = 1; 936 if (p->ctx->remove_session_cb != NULL) 937 p->ctx->remove_session_cb(p->ctx, s); 938 SSL_SESSION_free(s); 939 } 940} 941 942static 943IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION, TIMEOUT_PARAM) 944 945void 946SSL_CTX_flush_sessions(SSL_CTX *s, long t) 947{ 948 unsigned long i; 949 TIMEOUT_PARAM tp; 950 951 tp.ctx = s; 952 tp.cache = s->sessions; 953 if (tp.cache == NULL) 954 return; 955 tp.time = t; 956 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 957 i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load; 958 CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0; 959 lh_SSL_SESSION_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), 960 TIMEOUT_PARAM, &tp); 961 CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i; 962 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 963} 964 965int 966ssl_clear_bad_session(SSL *s) 967{ 968 if ((s->session != NULL) && 969 !(s->shutdown & SSL_SENT_SHUTDOWN) && 970 !(SSL_in_init(s) || SSL_in_before(s))) { 971 SSL_CTX_remove_session(s->ctx, s->session); 972 return (1); 973 } else 974 return (0); 975} 976 977/* locked by SSL_CTX in the calling function */ 978static void 979SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) 980{ 981 if ((s->next == NULL) 982 || (s->prev == NULL)) return; 983 984 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) 985 { /* last element in list */ 986 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) 987 { /* only one element in list */ 988 ctx->session_cache_head = NULL; 989 ctx->session_cache_tail = NULL; 990 } else { 991 ctx->session_cache_tail = s->prev; 992 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); 993 } 994 } else { 995 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) 996 { /* first element in list */ 997 ctx->session_cache_head = s->next; 998 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); 999 } else 1000 { /* middle of list */ 1001 s->next->prev = s->prev; 1002 s->prev->next = s->next; 1003 } 1004 } 1005 s->prev = s->next = NULL; 1006} 1007 1008static void 1009SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) 1010{ 1011 if ((s->next != NULL) && (s->prev != NULL)) 1012 SSL_SESSION_list_remove(ctx, s); 1013 1014 if (ctx->session_cache_head == NULL) { 1015 ctx->session_cache_head = s; 1016 ctx->session_cache_tail = s; 1017 s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1018 s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1019 } else { 1020 s->next = ctx->session_cache_head; 1021 s->next->prev = s; 1022 s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1023 ctx->session_cache_head = s; 1024 } 1025} 1026 1027void 1028SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, 1029 int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) { 1030 ctx->new_session_cb = cb; 1031} 1032 1033int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) 1034{ 1035 return ctx->new_session_cb; 1036} 1037 1038void 1039SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, 1040 void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) 1041{ 1042 ctx->remove_session_cb = cb; 1043} 1044 1045void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx, SSL_SESSION *sess) 1046{ 1047 return ctx->remove_session_cb; 1048} 1049 1050void 1051SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, 1052 SSL_SESSION *(*cb)(struct ssl_st *ssl, 1053unsigned char *data, int len, int *copy)) 1054{ 1055 ctx->get_session_cb = cb; 1056} 1057 1058SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, 1059 unsigned char *data, int len, int *copy) 1060{ 1061 return ctx->get_session_cb; 1062} 1063 1064void 1065SSL_CTX_set_info_callback(SSL_CTX *ctx, 1066 void (*cb)(const SSL *ssl, int type, int val)) 1067{ 1068 ctx->info_callback = cb; 1069} 1070 1071void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, int val) 1072{ 1073 return ctx->info_callback; 1074} 1075 1076void 1077SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, 1078 int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey)) 1079{ 1080 ctx->client_cert_cb = cb; 1081} 1082 1083int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey) 1084{ 1085 return ctx->client_cert_cb; 1086} 1087 1088#ifndef OPENSSL_NO_ENGINE 1089int 1090SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e) 1091{ 1092 if (!ENGINE_init(e)) { 1093 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB); 1094 return 0; 1095 } 1096 if (!ENGINE_get_ssl_client_cert_function(e)) { 1097 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, SSL_R_NO_CLIENT_CERT_METHOD); 1098 ENGINE_finish(e); 1099 return 0; 1100 } 1101 ctx->client_cert_engine = e; 1102 return 1; 1103} 1104#endif 1105 1106void 1107SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, 1108 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)) 1109{ 1110 ctx->app_gen_cookie_cb = cb; 1111} 1112 1113void 1114SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, 1115 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len)) 1116{ 1117 ctx->app_verify_cookie_cb = cb; 1118} 1119 1120IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) 1121