VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/ssl/ssl_sess.c@ 109052

Last change on this file since 109052 was 109052, checked in by vboxsync, 3 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette