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