1 /* 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright 2005 Nokia. All rights reserved. 4 * 5 * Licensed under the Apache License 2.0 (the "License"). You may not use 6 * this file except in compliance with the License. You can obtain a copy 7 * in the file LICENSE in the source distribution or at 8 * https://www.openssl.org/source/license.html 9 */ 10 11 #if defined(__TANDEM) && defined(_SPT_MODEL_) 12 # include <spthread.h> 13 # include <spt_extensions.h> /* timeval */ 14 #endif 15 #include <stdio.h> 16 #include <openssl/rand.h> 17 #include <openssl/engine.h> 18 #include "internal/refcount.h" 19 #include "internal/cryptlib.h" 20 #include "ssl_local.h" 21 #include "statem/statem_local.h" 22 23 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 24 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); 25 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); 26 27 DEFINE_STACK_OF(SSL_SESSION) 28 29 __owur static int sess_timedout(time_t t, SSL_SESSION *ss) 30 { 31 /* if timeout overflowed, it can never timeout! */ 32 if (ss->timeout_ovf) 33 return 0; 34 return t > ss->calc_timeout; 35 } 36 37 /* 38 * Returns -1/0/+1 as other XXXcmp-type functions 39 * Takes overflow of calculated timeout into consideration 40 */ 41 __owur static int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b) 42 { 43 /* if only one overflowed, then it is greater */ 44 if (a->timeout_ovf && !b->timeout_ovf) 45 return 1; 46 if (!a->timeout_ovf && b->timeout_ovf) 47 return -1; 48 /* No overflow, or both overflowed, so straight compare is safe */ 49 if (a->calc_timeout < b->calc_timeout) 50 return -1; 51 if (a->calc_timeout > b->calc_timeout) 52 return 1; 53 return 0; 54 } 55 56 /* 57 * Calculates effective timeout, saving overflow state 58 * Locking must be done by the caller of this function 59 */ 60 void ssl_session_calculate_timeout(SSL_SESSION *ss) 61 { 62 /* Force positive timeout */ 63 if (ss->timeout < 0) 64 ss->timeout = 0; 65 ss->calc_timeout = ss->time + ss->timeout; 66 /* 67 * |timeout| is always zero or positive, so the check for 68 * overflow only needs to consider if |time| is positive 69 */ 70 ss->timeout_ovf = ss->time > 0 && ss->calc_timeout < ss->time; 71 /* 72 * N.B. Realistic overflow can only occur in our lifetimes on a 73 * 32-bit machine in January 2038. 74 * However, There are no controls to limit the |timeout| 75 * value, except to keep it positive. 76 */ 77 } 78 79 /* 80 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because, 81 * unlike in earlier protocol versions, the session ticket may not have been 82 * sent yet even though a handshake has finished. The session ticket data could 83 * come in sometime later...or even change if multiple session ticket messages 84 * are sent from the server. The preferred way for applications to obtain 85 * a resumable session is to use SSL_CTX_sess_set_new_cb(). 86 */ 87 88 SSL_SESSION *SSL_get_session(const SSL *ssl) 89 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ 90 { 91 return ssl->session; 92 } 93 94 SSL_SESSION *SSL_get1_session(SSL *ssl) 95 /* variant of SSL_get_session: caller really gets something */ 96 { 97 SSL_SESSION *sess; 98 /* 99 * Need to lock this all up rather than just use CRYPTO_add so that 100 * somebody doesn't free ssl->session between when we check it's non-null 101 * and when we up the reference count. 102 */ 103 if (!CRYPTO_THREAD_read_lock(ssl->lock)) 104 return NULL; 105 sess = ssl->session; 106 if (sess) 107 SSL_SESSION_up_ref(sess); 108 CRYPTO_THREAD_unlock(ssl->lock); 109 return sess; 110 } 111 112 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) 113 { 114 return CRYPTO_set_ex_data(&s->ex_data, idx, arg); 115 } 116 117 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) 118 { 119 return CRYPTO_get_ex_data(&s->ex_data, idx); 120 } 121 122 SSL_SESSION *SSL_SESSION_new(void) 123 { 124 SSL_SESSION *ss; 125 126 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL)) 127 return NULL; 128 129 ss = OPENSSL_zalloc(sizeof(*ss)); 130 if (ss == NULL) { 131 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 132 return NULL; 133 } 134 135 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ 136 ss->references = 1; 137 ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */ 138 ss->time = time(NULL); 139 ssl_session_calculate_timeout(ss); 140 ss->lock = CRYPTO_THREAD_lock_new(); 141 if (ss->lock == NULL) { 142 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 143 OPENSSL_free(ss); 144 return NULL; 145 } 146 147 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) { 148 CRYPTO_THREAD_lock_free(ss->lock); 149 OPENSSL_free(ss); 150 return NULL; 151 } 152 return ss; 153 } 154 155 /* 156 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If 157 * ticket == 0 then no ticket information is duplicated, otherwise it is. 158 */ 159 static SSL_SESSION *ssl_session_dup_intern(const SSL_SESSION *src, int ticket) 160 { 161 SSL_SESSION *dest; 162 163 dest = OPENSSL_malloc(sizeof(*dest)); 164 if (dest == NULL) { 165 goto err; 166 } 167 memcpy(dest, src, sizeof(*dest)); 168 169 /* 170 * Set the various pointers to NULL so that we can call SSL_SESSION_free in 171 * the case of an error whilst halfway through constructing dest 172 */ 173 #ifndef OPENSSL_NO_PSK 174 dest->psk_identity_hint = NULL; 175 dest->psk_identity = NULL; 176 #endif 177 dest->ext.hostname = NULL; 178 dest->ext.tick = NULL; 179 dest->ext.alpn_selected = NULL; 180 #ifndef OPENSSL_NO_SRP 181 dest->srp_username = NULL; 182 #endif 183 dest->peer_chain = NULL; 184 dest->peer = NULL; 185 dest->ticket_appdata = NULL; 186 memset(&dest->ex_data, 0, sizeof(dest->ex_data)); 187 188 /* As the copy is not in the cache, we remove the associated pointers */ 189 dest->prev = NULL; 190 dest->next = NULL; 191 dest->owner = NULL; 192 193 dest->references = 1; 194 195 dest->lock = CRYPTO_THREAD_lock_new(); 196 if (dest->lock == NULL) { 197 OPENSSL_free(dest); 198 dest = NULL; 199 goto err; 200 } 201 202 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) 203 goto err; 204 205 if (src->peer != NULL) { 206 if (!X509_up_ref(src->peer)) 207 goto err; 208 dest->peer = src->peer; 209 } 210 211 if (src->peer_chain != NULL) { 212 dest->peer_chain = X509_chain_up_ref(src->peer_chain); 213 if (dest->peer_chain == NULL) 214 goto err; 215 } 216 #ifndef OPENSSL_NO_PSK 217 if (src->psk_identity_hint) { 218 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint); 219 if (dest->psk_identity_hint == NULL) { 220 goto err; 221 } 222 } 223 if (src->psk_identity) { 224 dest->psk_identity = OPENSSL_strdup(src->psk_identity); 225 if (dest->psk_identity == NULL) { 226 goto err; 227 } 228 } 229 #endif 230 231 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, 232 &dest->ex_data, &src->ex_data)) { 233 goto err; 234 } 235 236 if (src->ext.hostname) { 237 dest->ext.hostname = OPENSSL_strdup(src->ext.hostname); 238 if (dest->ext.hostname == NULL) { 239 goto err; 240 } 241 } 242 243 if (ticket != 0 && src->ext.tick != NULL) { 244 dest->ext.tick = 245 OPENSSL_memdup(src->ext.tick, src->ext.ticklen); 246 if (dest->ext.tick == NULL) 247 goto err; 248 } else { 249 dest->ext.tick_lifetime_hint = 0; 250 dest->ext.ticklen = 0; 251 } 252 253 if (src->ext.alpn_selected != NULL) { 254 dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected, 255 src->ext.alpn_selected_len); 256 if (dest->ext.alpn_selected == NULL) 257 goto err; 258 } 259 260 #ifndef OPENSSL_NO_SRP 261 if (src->srp_username) { 262 dest->srp_username = OPENSSL_strdup(src->srp_username); 263 if (dest->srp_username == NULL) { 264 goto err; 265 } 266 } 267 #endif 268 269 if (src->ticket_appdata != NULL) { 270 dest->ticket_appdata = 271 OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len); 272 if (dest->ticket_appdata == NULL) 273 goto err; 274 } 275 276 return dest; 277 err: 278 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 279 SSL_SESSION_free(dest); 280 return NULL; 281 } 282 283 SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src) 284 { 285 return ssl_session_dup_intern(src, 1); 286 } 287 288 /* 289 * Used internally when duplicating a session which might be already shared. 290 * We will have resumed the original session. Subsequently we might have marked 291 * it as non-resumable (e.g. in another thread) - but this copy should be ok to 292 * resume from. 293 */ 294 SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket) 295 { 296 SSL_SESSION *sess = ssl_session_dup_intern(src, ticket); 297 298 if (sess != NULL) 299 sess->not_resumable = 0; 300 301 return sess; 302 } 303 304 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) 305 { 306 if (len) 307 *len = (unsigned int)s->session_id_length; 308 return s->session_id; 309 } 310 const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s, 311 unsigned int *len) 312 { 313 if (len != NULL) 314 *len = (unsigned int)s->sid_ctx_length; 315 return s->sid_ctx; 316 } 317 318 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s) 319 { 320 return s->compress_meth; 321 } 322 323 /* 324 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling 325 * the ID with random junk repeatedly until we have no conflict is going to 326 * complete in one iteration pretty much "most" of the time (btw: 327 * understatement). So, if it takes us 10 iterations and we still can't avoid 328 * a conflict - well that's a reasonable point to call it quits. Either the 329 * RAND code is broken or someone is trying to open roughly very close to 330 * 2^256 SSL sessions to our server. How you might store that many sessions 331 * is perhaps a more interesting question ... 332 */ 333 334 #define MAX_SESS_ID_ATTEMPTS 10 335 static int def_generate_session_id(SSL *ssl, unsigned char *id, 336 unsigned int *id_len) 337 { 338 unsigned int retry = 0; 339 do 340 if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0) 341 return 0; 342 while (SSL_has_matching_session_id(ssl, id, *id_len) && 343 (++retry < MAX_SESS_ID_ATTEMPTS)) ; 344 if (retry < MAX_SESS_ID_ATTEMPTS) 345 return 1; 346 /* else - woops a session_id match */ 347 /* 348 * XXX We should also check the external cache -- but the probability of 349 * a collision is negligible, and we could not prevent the concurrent 350 * creation of sessions with identical IDs since we currently don't have 351 * means to atomically check whether a session ID already exists and make 352 * a reservation for it if it does not (this problem applies to the 353 * internal cache as well). 354 */ 355 return 0; 356 } 357 358 int ssl_generate_session_id(SSL *s, SSL_SESSION *ss) 359 { 360 unsigned int tmp; 361 GEN_SESSION_CB cb = def_generate_session_id; 362 363 switch (s->version) { 364 case SSL3_VERSION: 365 case TLS1_VERSION: 366 case TLS1_1_VERSION: 367 case TLS1_2_VERSION: 368 case TLS1_3_VERSION: 369 case DTLS1_BAD_VER: 370 case DTLS1_VERSION: 371 case DTLS1_2_VERSION: 372 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 373 break; 374 default: 375 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION); 376 return 0; 377 } 378 379 /*- 380 * If RFC5077 ticket, use empty session ID (as server). 381 * Note that: 382 * (a) ssl_get_prev_session() does lookahead into the 383 * ClientHello extensions to find the session ticket. 384 * When ssl_get_prev_session() fails, statem_srvr.c calls 385 * ssl_get_new_session() in tls_process_client_hello(). 386 * At that point, it has not yet parsed the extensions, 387 * however, because of the lookahead, it already knows 388 * whether a ticket is expected or not. 389 * 390 * (b) statem_clnt.c calls ssl_get_new_session() before parsing 391 * ServerHello extensions, and before recording the session 392 * ID received from the server, so this block is a noop. 393 */ 394 if (s->ext.ticket_expected) { 395 ss->session_id_length = 0; 396 return 1; 397 } 398 399 /* Choose which callback will set the session ID */ 400 if (!CRYPTO_THREAD_read_lock(s->lock)) 401 return 0; 402 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) { 403 CRYPTO_THREAD_unlock(s->lock); 404 SSLfatal(s, SSL_AD_INTERNAL_ERROR, 405 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 406 return 0; 407 } 408 if (s->generate_session_id) 409 cb = s->generate_session_id; 410 else if (s->session_ctx->generate_session_id) 411 cb = s->session_ctx->generate_session_id; 412 CRYPTO_THREAD_unlock(s->session_ctx->lock); 413 CRYPTO_THREAD_unlock(s->lock); 414 /* Choose a session ID */ 415 memset(ss->session_id, 0, ss->session_id_length); 416 tmp = (int)ss->session_id_length; 417 if (!cb(s, ss->session_id, &tmp)) { 418 /* The callback failed */ 419 SSLfatal(s, SSL_AD_INTERNAL_ERROR, 420 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); 421 return 0; 422 } 423 /* 424 * Don't allow the callback to set the session length to zero. nor 425 * set it higher than it was. 426 */ 427 if (tmp == 0 || tmp > ss->session_id_length) { 428 /* The callback set an illegal length */ 429 SSLfatal(s, SSL_AD_INTERNAL_ERROR, 430 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); 431 return 0; 432 } 433 ss->session_id_length = tmp; 434 /* Finally, check for a conflict */ 435 if (SSL_has_matching_session_id(s, ss->session_id, 436 (unsigned int)ss->session_id_length)) { 437 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT); 438 return 0; 439 } 440 441 return 1; 442 } 443 444 int ssl_get_new_session(SSL *s, int session) 445 { 446 /* This gets used by clients and servers. */ 447 448 SSL_SESSION *ss = NULL; 449 450 if ((ss = SSL_SESSION_new()) == NULL) { 451 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); 452 return 0; 453 } 454 455 /* If the context has a default timeout, use it */ 456 if (s->session_ctx->session_timeout == 0) 457 ss->timeout = SSL_get_default_timeout(s); 458 else 459 ss->timeout = s->session_ctx->session_timeout; 460 ssl_session_calculate_timeout(ss); 461 462 SSL_SESSION_free(s->session); 463 s->session = NULL; 464 465 if (session) { 466 if (SSL_IS_TLS13(s)) { 467 /* 468 * We generate the session id while constructing the 469 * NewSessionTicket in TLSv1.3. 470 */ 471 ss->session_id_length = 0; 472 } else if (!ssl_generate_session_id(s, ss)) { 473 /* SSLfatal() already called */ 474 SSL_SESSION_free(ss); 475 return 0; 476 } 477 478 } else { 479 ss->session_id_length = 0; 480 } 481 482 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) { 483 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 484 SSL_SESSION_free(ss); 485 return 0; 486 } 487 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); 488 ss->sid_ctx_length = s->sid_ctx_length; 489 s->session = ss; 490 ss->ssl_version = s->version; 491 ss->verify_result = X509_V_OK; 492 493 /* If client supports extended master secret set it in session */ 494 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) 495 ss->flags |= SSL_SESS_FLAG_EXTMS; 496 497 return 1; 498 } 499 500 SSL_SESSION *lookup_sess_in_cache(SSL *s, const unsigned char *sess_id, 501 size_t sess_id_len) 502 { 503 SSL_SESSION *ret = NULL; 504 505 if ((s->session_ctx->session_cache_mode 506 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) { 507 SSL_SESSION data; 508 509 data.ssl_version = s->version; 510 if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH)) 511 return NULL; 512 513 memcpy(data.session_id, sess_id, sess_id_len); 514 data.session_id_length = sess_id_len; 515 516 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) 517 return NULL; 518 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); 519 if (ret != NULL) { 520 /* don't allow other threads to steal it: */ 521 SSL_SESSION_up_ref(ret); 522 } 523 CRYPTO_THREAD_unlock(s->session_ctx->lock); 524 if (ret == NULL) 525 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss); 526 } 527 528 if (ret == NULL && s->session_ctx->get_session_cb != NULL) { 529 int copy = 1; 530 531 ret = s->session_ctx->get_session_cb(s, sess_id, sess_id_len, ©); 532 533 if (ret != NULL) { 534 if (ret->not_resumable) { 535 /* If its not resumable then ignore this session */ 536 if (!copy) 537 SSL_SESSION_free(ret); 538 return NULL; 539 } 540 ssl_tsan_counter(s->session_ctx, 541 &s->session_ctx->stats.sess_cb_hit); 542 543 /* 544 * Increment reference count now if the session callback asks us 545 * to do so (note that if the session structures returned by the 546 * callback are shared between threads, it must handle the 547 * reference count itself [i.e. copy == 0], or things won't be 548 * thread-safe). 549 */ 550 if (copy) 551 SSL_SESSION_up_ref(ret); 552 553 /* 554 * Add the externally cached session to the internal cache as 555 * well if and only if we are supposed to. 556 */ 557 if ((s->session_ctx->session_cache_mode & 558 SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) { 559 /* 560 * Either return value of SSL_CTX_add_session should not 561 * interrupt the session resumption process. The return 562 * value is intentionally ignored. 563 */ 564 (void)SSL_CTX_add_session(s->session_ctx, ret); 565 } 566 } 567 } 568 569 return ret; 570 } 571 572 /*- 573 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this 574 * connection. It is only called by servers. 575 * 576 * hello: The parsed ClientHello data 577 * 578 * Returns: 579 * -1: fatal error 580 * 0: no session found 581 * 1: a session may have been found. 582 * 583 * Side effects: 584 * - If a session is found then s->session is pointed at it (after freeing an 585 * existing session if need be) and s->verify_result is set from the session. 586 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1 587 * if the server should issue a new session ticket (to 0 otherwise). 588 */ 589 int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello) 590 { 591 /* This is used only by servers. */ 592 593 SSL_SESSION *ret = NULL; 594 int fatal = 0; 595 int try_session_cache = 0; 596 SSL_TICKET_STATUS r; 597 598 if (SSL_IS_TLS13(s)) { 599 /* 600 * By default we will send a new ticket. This can be overridden in the 601 * ticket processing. 602 */ 603 s->ext.ticket_expected = 1; 604 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes, 605 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts, 606 NULL, 0) 607 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO, 608 hello->pre_proc_exts, NULL, 0)) 609 return -1; 610 611 ret = s->session; 612 } else { 613 /* sets s->ext.ticket_expected */ 614 r = tls_get_ticket_from_client(s, hello, &ret); 615 switch (r) { 616 case SSL_TICKET_FATAL_ERR_MALLOC: 617 case SSL_TICKET_FATAL_ERR_OTHER: 618 fatal = 1; 619 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 620 goto err; 621 case SSL_TICKET_NONE: 622 case SSL_TICKET_EMPTY: 623 if (hello->session_id_len > 0) { 624 try_session_cache = 1; 625 ret = lookup_sess_in_cache(s, hello->session_id, 626 hello->session_id_len); 627 } 628 break; 629 case SSL_TICKET_NO_DECRYPT: 630 case SSL_TICKET_SUCCESS: 631 case SSL_TICKET_SUCCESS_RENEW: 632 break; 633 } 634 } 635 636 if (ret == NULL) 637 goto err; 638 639 /* Now ret is non-NULL and we own one of its reference counts. */ 640 641 /* Check TLS version consistency */ 642 if (ret->ssl_version != s->version) 643 goto err; 644 645 if (ret->sid_ctx_length != s->sid_ctx_length 646 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { 647 /* 648 * We have the session requested by the client, but we don't want to 649 * use it in this context. 650 */ 651 goto err; /* treat like cache miss */ 652 } 653 654 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { 655 /* 656 * We can't be sure if this session is being used out of context, 657 * which is especially important for SSL_VERIFY_PEER. The application 658 * should have used SSL[_CTX]_set_session_id_context. For this error 659 * case, we generate an error instead of treating the event like a 660 * cache miss (otherwise it would be easy for applications to 661 * effectively disable the session cache by accident without anyone 662 * noticing). 663 */ 664 665 SSLfatal(s, SSL_AD_INTERNAL_ERROR, 666 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 667 fatal = 1; 668 goto err; 669 } 670 671 if (sess_timedout(time(NULL), ret)) { 672 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout); 673 if (try_session_cache) { 674 /* session was from the cache, so remove it */ 675 SSL_CTX_remove_session(s->session_ctx, ret); 676 } 677 goto err; 678 } 679 680 /* Check extended master secret extension consistency */ 681 if (ret->flags & SSL_SESS_FLAG_EXTMS) { 682 /* If old session includes extms, but new does not: abort handshake */ 683 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) { 684 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS); 685 fatal = 1; 686 goto err; 687 } 688 } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) { 689 /* If new session includes extms, but old does not: do not resume */ 690 goto err; 691 } 692 693 if (!SSL_IS_TLS13(s)) { 694 /* We already did this for TLS1.3 */ 695 SSL_SESSION_free(s->session); 696 s->session = ret; 697 } 698 699 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit); 700 s->verify_result = s->session->verify_result; 701 return 1; 702 703 err: 704 if (ret != NULL) { 705 SSL_SESSION_free(ret); 706 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */ 707 if (SSL_IS_TLS13(s)) 708 s->session = NULL; 709 710 if (!try_session_cache) { 711 /* 712 * The session was from a ticket, so we should issue a ticket for 713 * the new session 714 */ 715 s->ext.ticket_expected = 1; 716 } 717 } 718 if (fatal) 719 return -1; 720 721 return 0; 722 } 723 724 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) 725 { 726 int ret = 0; 727 SSL_SESSION *s; 728 729 /* 730 * add just 1 reference count for the SSL_CTX's session cache even though 731 * it has two ways of access: each session is in a doubly linked list and 732 * an lhash 733 */ 734 SSL_SESSION_up_ref(c); 735 /* 736 * if session c is in already in cache, we take back the increment later 737 */ 738 739 if (!CRYPTO_THREAD_write_lock(ctx->lock)) { 740 SSL_SESSION_free(c); 741 return 0; 742 } 743 s = lh_SSL_SESSION_insert(ctx->sessions, c); 744 745 /* 746 * s != NULL iff we already had a session with the given PID. In this 747 * case, s == c should hold (then we did not really modify 748 * ctx->sessions), or we're in trouble. 749 */ 750 if (s != NULL && s != c) { 751 /* We *are* in trouble ... */ 752 SSL_SESSION_list_remove(ctx, s); 753 SSL_SESSION_free(s); 754 /* 755 * ... so pretend the other session did not exist in cache (we cannot 756 * handle two SSL_SESSION structures with identical session ID in the 757 * same cache, which could happen e.g. when two threads concurrently 758 * obtain the same session from an external cache) 759 */ 760 s = NULL; 761 } else if (s == NULL && 762 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) { 763 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */ 764 765 /* 766 * ... so take back the extra reference and also don't add 767 * the session to the SSL_SESSION_list at this time 768 */ 769 s = c; 770 } 771 772 /* Adjust last used time, and add back into the cache at the appropriate spot */ 773 if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) { 774 c->time = time(NULL); 775 ssl_session_calculate_timeout(c); 776 } 777 778 if (s == NULL) { 779 /* 780 * new cache entry -- remove old ones if cache has become too large 781 * delete cache entry *before* add, so we don't remove the one we're adding! 782 */ 783 784 ret = 1; 785 786 if (SSL_CTX_sess_get_cache_size(ctx) > 0) { 787 while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) { 788 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) 789 break; 790 else 791 ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full); 792 } 793 } 794 } 795 796 SSL_SESSION_list_add(ctx, c); 797 798 if (s != NULL) { 799 /* 800 * existing cache entry -- decrement previously incremented reference 801 * count because it already takes into account the cache 802 */ 803 804 SSL_SESSION_free(s); /* s == c */ 805 ret = 0; 806 } 807 CRYPTO_THREAD_unlock(ctx->lock); 808 return ret; 809 } 810 811 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) 812 { 813 return remove_session_lock(ctx, c, 1); 814 } 815 816 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) 817 { 818 SSL_SESSION *r; 819 int ret = 0; 820 821 if ((c != NULL) && (c->session_id_length != 0)) { 822 if (lck) { 823 if (!CRYPTO_THREAD_write_lock(ctx->lock)) 824 return 0; 825 } 826 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) { 827 ret = 1; 828 r = lh_SSL_SESSION_delete(ctx->sessions, r); 829 SSL_SESSION_list_remove(ctx, r); 830 } 831 c->not_resumable = 1; 832 833 if (lck) 834 CRYPTO_THREAD_unlock(ctx->lock); 835 836 if (ctx->remove_session_cb != NULL) 837 ctx->remove_session_cb(ctx, c); 838 839 if (ret) 840 SSL_SESSION_free(r); 841 } 842 return ret; 843 } 844 845 void SSL_SESSION_free(SSL_SESSION *ss) 846 { 847 int i; 848 849 if (ss == NULL) 850 return; 851 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock); 852 REF_PRINT_COUNT("SSL_SESSION", ss); 853 if (i > 0) 854 return; 855 REF_ASSERT_ISNT(i < 0); 856 857 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); 858 859 OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key)); 860 OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id)); 861 X509_free(ss->peer); 862 sk_X509_pop_free(ss->peer_chain, X509_free); 863 OPENSSL_free(ss->ext.hostname); 864 OPENSSL_free(ss->ext.tick); 865 #ifndef OPENSSL_NO_PSK 866 OPENSSL_free(ss->psk_identity_hint); 867 OPENSSL_free(ss->psk_identity); 868 #endif 869 #ifndef OPENSSL_NO_SRP 870 OPENSSL_free(ss->srp_username); 871 #endif 872 OPENSSL_free(ss->ext.alpn_selected); 873 OPENSSL_free(ss->ticket_appdata); 874 CRYPTO_THREAD_lock_free(ss->lock); 875 OPENSSL_clear_free(ss, sizeof(*ss)); 876 } 877 878 int SSL_SESSION_up_ref(SSL_SESSION *ss) 879 { 880 int i; 881 882 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0) 883 return 0; 884 885 REF_PRINT_COUNT("SSL_SESSION", ss); 886 REF_ASSERT_ISNT(i < 2); 887 return ((i > 1) ? 1 : 0); 888 } 889 890 int SSL_set_session(SSL *s, SSL_SESSION *session) 891 { 892 ssl_clear_bad_session(s); 893 if (s->ctx->method != s->method) { 894 if (!SSL_set_ssl_method(s, s->ctx->method)) 895 return 0; 896 } 897 898 if (session != NULL) { 899 SSL_SESSION_up_ref(session); 900 s->verify_result = session->verify_result; 901 } 902 SSL_SESSION_free(s->session); 903 s->session = session; 904 905 return 1; 906 } 907 908 int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, 909 unsigned int sid_len) 910 { 911 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { 912 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG); 913 return 0; 914 } 915 s->session_id_length = sid_len; 916 if (sid != s->session_id) 917 memcpy(s->session_id, sid, sid_len); 918 return 1; 919 } 920 921 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) 922 { 923 time_t new_timeout = (time_t)t; 924 925 if (s == NULL || t < 0) 926 return 0; 927 if (s->owner != NULL) { 928 if (!CRYPTO_THREAD_write_lock(s->owner->lock)) 929 return 0; 930 s->timeout = new_timeout; 931 ssl_session_calculate_timeout(s); 932 SSL_SESSION_list_add(s->owner, s); 933 CRYPTO_THREAD_unlock(s->owner->lock); 934 } else { 935 s->timeout = new_timeout; 936 ssl_session_calculate_timeout(s); 937 } 938 return 1; 939 } 940 941 long SSL_SESSION_get_timeout(const SSL_SESSION *s) 942 { 943 if (s == NULL) 944 return 0; 945 return (long)s->timeout; 946 } 947 948 long SSL_SESSION_get_time(const SSL_SESSION *s) 949 { 950 if (s == NULL) 951 return 0; 952 return (long)s->time; 953 } 954 955 long SSL_SESSION_set_time(SSL_SESSION *s, long t) 956 { 957 time_t new_time = (time_t)t; 958 959 if (s == NULL) 960 return 0; 961 if (s->owner != NULL) { 962 if (!CRYPTO_THREAD_write_lock(s->owner->lock)) 963 return 0; 964 s->time = new_time; 965 ssl_session_calculate_timeout(s); 966 SSL_SESSION_list_add(s->owner, s); 967 CRYPTO_THREAD_unlock(s->owner->lock); 968 } else { 969 s->time = new_time; 970 ssl_session_calculate_timeout(s); 971 } 972 return t; 973 } 974 975 int SSL_SESSION_get_protocol_version(const SSL_SESSION *s) 976 { 977 return s->ssl_version; 978 } 979 980 int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version) 981 { 982 s->ssl_version = version; 983 return 1; 984 } 985 986 const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s) 987 { 988 return s->cipher; 989 } 990 991 int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher) 992 { 993 s->cipher = cipher; 994 return 1; 995 } 996 997 const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s) 998 { 999 return s->ext.hostname; 1000 } 1001 1002 int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname) 1003 { 1004 OPENSSL_free(s->ext.hostname); 1005 if (hostname == NULL) { 1006 s->ext.hostname = NULL; 1007 return 1; 1008 } 1009 s->ext.hostname = OPENSSL_strdup(hostname); 1010 1011 return s->ext.hostname != NULL; 1012 } 1013 1014 int SSL_SESSION_has_ticket(const SSL_SESSION *s) 1015 { 1016 return (s->ext.ticklen > 0) ? 1 : 0; 1017 } 1018 1019 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) 1020 { 1021 return s->ext.tick_lifetime_hint; 1022 } 1023 1024 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick, 1025 size_t *len) 1026 { 1027 *len = s->ext.ticklen; 1028 if (tick != NULL) 1029 *tick = s->ext.tick; 1030 } 1031 1032 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s) 1033 { 1034 return s->ext.max_early_data; 1035 } 1036 1037 int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data) 1038 { 1039 s->ext.max_early_data = max_early_data; 1040 1041 return 1; 1042 } 1043 1044 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s, 1045 const unsigned char **alpn, 1046 size_t *len) 1047 { 1048 *alpn = s->ext.alpn_selected; 1049 *len = s->ext.alpn_selected_len; 1050 } 1051 1052 int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn, 1053 size_t len) 1054 { 1055 OPENSSL_free(s->ext.alpn_selected); 1056 if (alpn == NULL || len == 0) { 1057 s->ext.alpn_selected = NULL; 1058 s->ext.alpn_selected_len = 0; 1059 return 1; 1060 } 1061 s->ext.alpn_selected = OPENSSL_memdup(alpn, len); 1062 if (s->ext.alpn_selected == NULL) { 1063 s->ext.alpn_selected_len = 0; 1064 return 0; 1065 } 1066 s->ext.alpn_selected_len = len; 1067 1068 return 1; 1069 } 1070 1071 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) 1072 { 1073 return s->peer; 1074 } 1075 1076 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, 1077 unsigned int sid_ctx_len) 1078 { 1079 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 1080 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 1081 return 0; 1082 } 1083 s->sid_ctx_length = sid_ctx_len; 1084 if (sid_ctx != s->sid_ctx) 1085 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); 1086 1087 return 1; 1088 } 1089 1090 int SSL_SESSION_is_resumable(const SSL_SESSION *s) 1091 { 1092 /* 1093 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a 1094 * session ID. 1095 */ 1096 return !s->not_resumable 1097 && (s->session_id_length > 0 || s->ext.ticklen > 0); 1098 } 1099 1100 long SSL_CTX_set_timeout(SSL_CTX *s, long t) 1101 { 1102 long l; 1103 if (s == NULL) 1104 return 0; 1105 l = s->session_timeout; 1106 s->session_timeout = t; 1107 return l; 1108 } 1109 1110 long SSL_CTX_get_timeout(const SSL_CTX *s) 1111 { 1112 if (s == NULL) 1113 return 0; 1114 return s->session_timeout; 1115 } 1116 1117 int SSL_set_session_secret_cb(SSL *s, 1118 tls_session_secret_cb_fn tls_session_secret_cb, 1119 void *arg) 1120 { 1121 if (s == NULL) 1122 return 0; 1123 s->ext.session_secret_cb = tls_session_secret_cb; 1124 s->ext.session_secret_cb_arg = arg; 1125 return 1; 1126 } 1127 1128 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, 1129 void *arg) 1130 { 1131 if (s == NULL) 1132 return 0; 1133 s->ext.session_ticket_cb = cb; 1134 s->ext.session_ticket_cb_arg = arg; 1135 return 1; 1136 } 1137 1138 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) 1139 { 1140 if (s->version >= TLS1_VERSION) { 1141 OPENSSL_free(s->ext.session_ticket); 1142 s->ext.session_ticket = NULL; 1143 s->ext.session_ticket = 1144 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); 1145 if (s->ext.session_ticket == NULL) { 1146 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 1147 return 0; 1148 } 1149 1150 if (ext_data != NULL) { 1151 s->ext.session_ticket->length = ext_len; 1152 s->ext.session_ticket->data = s->ext.session_ticket + 1; 1153 memcpy(s->ext.session_ticket->data, ext_data, ext_len); 1154 } else { 1155 s->ext.session_ticket->length = 0; 1156 s->ext.session_ticket->data = NULL; 1157 } 1158 1159 return 1; 1160 } 1161 1162 return 0; 1163 } 1164 1165 void SSL_CTX_flush_sessions(SSL_CTX *s, long t) 1166 { 1167 STACK_OF(SSL_SESSION) *sk; 1168 SSL_SESSION *current; 1169 unsigned long i; 1170 1171 if (!CRYPTO_THREAD_write_lock(s->lock)) 1172 return; 1173 1174 sk = sk_SSL_SESSION_new_null(); 1175 i = lh_SSL_SESSION_get_down_load(s->sessions); 1176 lh_SSL_SESSION_set_down_load(s->sessions, 0); 1177 1178 /* 1179 * Iterate over the list from the back (oldest), and stop 1180 * when a session can no longer be removed. 1181 * Add the session to a temporary list to be freed outside 1182 * the SSL_CTX lock. 1183 * But still do the remove_session_cb() within the lock. 1184 */ 1185 while (s->session_cache_tail != NULL) { 1186 current = s->session_cache_tail; 1187 if (t == 0 || sess_timedout((time_t)t, current)) { 1188 lh_SSL_SESSION_delete(s->sessions, current); 1189 SSL_SESSION_list_remove(s, current); 1190 current->not_resumable = 1; 1191 if (s->remove_session_cb != NULL) 1192 s->remove_session_cb(s, current); 1193 /* 1194 * Throw the session on a stack, it's entirely plausible 1195 * that while freeing outside the critical section, the 1196 * session could be re-added, so avoid using the next/prev 1197 * pointers. If the stack failed to create, or the session 1198 * couldn't be put on the stack, just free it here 1199 */ 1200 if (sk == NULL || !sk_SSL_SESSION_push(sk, current)) 1201 SSL_SESSION_free(current); 1202 } else { 1203 break; 1204 } 1205 } 1206 1207 lh_SSL_SESSION_set_down_load(s->sessions, i); 1208 CRYPTO_THREAD_unlock(s->lock); 1209 1210 sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free); 1211 } 1212 1213 int ssl_clear_bad_session(SSL *s) 1214 { 1215 if ((s->session != NULL) && 1216 !(s->shutdown & SSL_SENT_SHUTDOWN) && 1217 !(SSL_in_init(s) || SSL_in_before(s))) { 1218 SSL_CTX_remove_session(s->session_ctx, s->session); 1219 return 1; 1220 } else 1221 return 0; 1222 } 1223 1224 /* locked by SSL_CTX in the calling function */ 1225 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) 1226 { 1227 if ((s->next == NULL) || (s->prev == NULL)) 1228 return; 1229 1230 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) { 1231 /* last element in list */ 1232 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { 1233 /* only one element in list */ 1234 ctx->session_cache_head = NULL; 1235 ctx->session_cache_tail = NULL; 1236 } else { 1237 ctx->session_cache_tail = s->prev; 1238 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1239 } 1240 } else { 1241 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { 1242 /* first element in list */ 1243 ctx->session_cache_head = s->next; 1244 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1245 } else { 1246 /* middle of list */ 1247 s->next->prev = s->prev; 1248 s->prev->next = s->next; 1249 } 1250 } 1251 s->prev = s->next = NULL; 1252 s->owner = NULL; 1253 } 1254 1255 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) 1256 { 1257 SSL_SESSION *next; 1258 1259 if ((s->next != NULL) && (s->prev != NULL)) 1260 SSL_SESSION_list_remove(ctx, s); 1261 1262 if (ctx->session_cache_head == NULL) { 1263 ctx->session_cache_head = s; 1264 ctx->session_cache_tail = s; 1265 s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1266 s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1267 } else { 1268 if (timeoutcmp(s, ctx->session_cache_head) >= 0) { 1269 /* 1270 * if we timeout after (or the same time as) the first 1271 * session, put us first - usual case 1272 */ 1273 s->next = ctx->session_cache_head; 1274 s->next->prev = s; 1275 s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1276 ctx->session_cache_head = s; 1277 } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) { 1278 /* if we timeout before the last session, put us last */ 1279 s->prev = ctx->session_cache_tail; 1280 s->prev->next = s; 1281 s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1282 ctx->session_cache_tail = s; 1283 } else { 1284 /* 1285 * we timeout somewhere in-between - if there is only 1286 * one session in the cache it will be caught above 1287 */ 1288 next = ctx->session_cache_head->next; 1289 while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) { 1290 if (timeoutcmp(s, next) >= 0) { 1291 s->next = next; 1292 s->prev = next->prev; 1293 next->prev->next = s; 1294 next->prev = s; 1295 break; 1296 } 1297 next = next->next; 1298 } 1299 } 1300 } 1301 s->owner = ctx; 1302 } 1303 1304 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, 1305 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess)) 1306 { 1307 ctx->new_session_cb = cb; 1308 } 1309 1310 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) { 1311 return ctx->new_session_cb; 1312 } 1313 1314 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, 1315 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess)) 1316 { 1317 ctx->remove_session_cb = cb; 1318 } 1319 1320 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx, 1321 SSL_SESSION *sess) { 1322 return ctx->remove_session_cb; 1323 } 1324 1325 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, 1326 SSL_SESSION *(*cb) (struct ssl_st *ssl, 1327 const unsigned char *data, 1328 int len, int *copy)) 1329 { 1330 ctx->get_session_cb = cb; 1331 } 1332 1333 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl, 1334 const unsigned char 1335 *data, int len, 1336 int *copy) { 1337 return ctx->get_session_cb; 1338 } 1339 1340 void SSL_CTX_set_info_callback(SSL_CTX *ctx, 1341 void (*cb) (const SSL *ssl, int type, int val)) 1342 { 1343 ctx->info_callback = cb; 1344 } 1345 1346 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, 1347 int val) { 1348 return ctx->info_callback; 1349 } 1350 1351 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, 1352 int (*cb) (SSL *ssl, X509 **x509, 1353 EVP_PKEY **pkey)) 1354 { 1355 ctx->client_cert_cb = cb; 1356 } 1357 1358 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, 1359 EVP_PKEY **pkey) { 1360 return ctx->client_cert_cb; 1361 } 1362 1363 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, 1364 int (*cb) (SSL *ssl, 1365 unsigned char *cookie, 1366 unsigned int *cookie_len)) 1367 { 1368 ctx->app_gen_cookie_cb = cb; 1369 } 1370 1371 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, 1372 int (*cb) (SSL *ssl, 1373 const unsigned char *cookie, 1374 unsigned int cookie_len)) 1375 { 1376 ctx->app_verify_cookie_cb = cb; 1377 } 1378 1379 int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len) 1380 { 1381 OPENSSL_free(ss->ticket_appdata); 1382 ss->ticket_appdata_len = 0; 1383 if (data == NULL || len == 0) { 1384 ss->ticket_appdata = NULL; 1385 return 1; 1386 } 1387 ss->ticket_appdata = OPENSSL_memdup(data, len); 1388 if (ss->ticket_appdata != NULL) { 1389 ss->ticket_appdata_len = len; 1390 return 1; 1391 } 1392 return 0; 1393 } 1394 1395 int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len) 1396 { 1397 *data = ss->ticket_appdata; 1398 *len = ss->ticket_appdata_len; 1399 return 1; 1400 } 1401 1402 void SSL_CTX_set_stateless_cookie_generate_cb( 1403 SSL_CTX *ctx, 1404 int (*cb) (SSL *ssl, 1405 unsigned char *cookie, 1406 size_t *cookie_len)) 1407 { 1408 ctx->gen_stateless_cookie_cb = cb; 1409 } 1410 1411 void SSL_CTX_set_stateless_cookie_verify_cb( 1412 SSL_CTX *ctx, 1413 int (*cb) (SSL *ssl, 1414 const unsigned char *cookie, 1415 size_t cookie_len)) 1416 { 1417 ctx->verify_stateless_cookie_cb = cb; 1418 } 1419 1420 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) 1421