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