]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_sess.c
Make the PACKET/WPACKET code available to both libcrypto and libssl
[thirdparty/openssl.git] / ssl / ssl_sess.c
CommitLineData
846e33c7 1/*
b0edda11 2 * Copyright 1995-2018 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"
d02b48c6 16#include "ssl_locl.h"
1053a6e2 17#include "statem/statem_locl.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
MC
73 if (ss == NULL) {
74 SSLerr(SSL_F_SSL_SESSION_NEW, 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) {
84 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
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
110 dest = OPENSSL_malloc(sizeof(*src));
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:
98ece4ee
MC
221 SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
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
16cfc2c9 262 if (RAND_bytes(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:
f63a17d6
MC
297 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
298 SSL_R_UNSUPPORTED_SSL_VERSION);
a84e5c9a
TS
299 return 0;
300 }
301
302 /*-
303 * If RFC5077 ticket, use empty session ID (as server).
304 * Note that:
305 * (a) ssl_get_prev_session() does lookahead into the
306 * ClientHello extensions to find the session ticket.
307 * When ssl_get_prev_session() fails, statem_srvr.c calls
308 * ssl_get_new_session() in tls_process_client_hello().
309 * At that point, it has not yet parsed the extensions,
310 * however, because of the lookahead, it already knows
311 * whether a ticket is expected or not.
312 *
313 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
314 * ServerHello extensions, and before recording the session
315 * ID received from the server, so this block is a noop.
316 */
317 if (s->ext.ticket_expected) {
318 ss->session_id_length = 0;
319 return 1;
320 }
321
322 /* Choose which callback will set the session ID */
323 CRYPTO_THREAD_read_lock(s->lock);
324 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
325 if (s->generate_session_id)
326 cb = s->generate_session_id;
327 else if (s->session_ctx->generate_session_id)
328 cb = s->session_ctx->generate_session_id;
329 CRYPTO_THREAD_unlock(s->session_ctx->lock);
330 CRYPTO_THREAD_unlock(s->lock);
331 /* Choose a session ID */
332 memset(ss->session_id, 0, ss->session_id_length);
333 tmp = (int)ss->session_id_length;
334 if (!cb(s, ss->session_id, &tmp)) {
335 /* The callback failed */
f63a17d6
MC
336 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
337 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
a84e5c9a
TS
338 return 0;
339 }
340 /*
341 * Don't allow the callback to set the session length to zero. nor
342 * set it higher than it was.
343 */
344 if (tmp == 0 || tmp > ss->session_id_length) {
345 /* The callback set an illegal length */
f63a17d6
MC
346 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
347 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
a84e5c9a
TS
348 return 0;
349 }
350 ss->session_id_length = tmp;
351 /* Finally, check for a conflict */
352 if (SSL_has_matching_session_id(s, ss->session_id,
353 (unsigned int)ss->session_id_length)) {
f63a17d6
MC
354 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
355 SSL_R_SSL_SESSION_ID_CONFLICT);
a84e5c9a
TS
356 return 0;
357 }
358
359 return 1;
360}
361
6b691a5c 362int ssl_get_new_session(SSL *s, int session)
0f113f3e
MC
363{
364 /* This gets used by clients and servers. */
365
0f113f3e 366 SSL_SESSION *ss = NULL;
0f113f3e 367
f63a17d6
MC
368 if ((ss = SSL_SESSION_new()) == NULL) {
369 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_NEW_SESSION,
370 ERR_R_MALLOC_FAILURE);
a84e5c9a 371 return 0;
f63a17d6 372 }
0f113f3e
MC
373
374 /* If the context has a default timeout, use it */
375 if (s->session_ctx->session_timeout == 0)
376 ss->timeout = SSL_get_default_timeout(s);
377 else
378 ss->timeout = s->session_ctx->session_timeout;
379
62adbcee
RS
380 SSL_SESSION_free(s->session);
381 s->session = NULL;
0f113f3e
MC
382
383 if (session) {
32305f88
MC
384 if (SSL_IS_TLS13(s)) {
385 /*
386 * We generate the session id while constructing the
387 * NewSessionTicket in TLSv1.3.
388 */
389 ss->session_id_length = 0;
390 } else if (!ssl_generate_session_id(s, ss)) {
f63a17d6 391 /* SSLfatal() already called */
0f113f3e 392 SSL_SESSION_free(ss);
a84e5c9a 393 return 0;
0f113f3e 394 }
e481f9b9 395
0f113f3e
MC
396 } else {
397 ss->session_id_length = 0;
398 }
399
cbe29648 400 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
f63a17d6
MC
401 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_NEW_SESSION,
402 ERR_R_INTERNAL_ERROR);
0f113f3e
MC
403 SSL_SESSION_free(ss);
404 return 0;
405 }
406 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
407 ss->sid_ctx_length = s->sid_ctx_length;
408 s->session = ss;
409 ss->ssl_version = s->version;
410 ss->verify_result = X509_V_OK;
411
e7f0d921 412 /* If client supports extended master secret set it in session */
555cbb32 413 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
e7f0d921
DSH
414 ss->flags |= SSL_SESS_FLAG_EXTMS;
415
a84e5c9a 416 return 1;
0f113f3e 417}
d02b48c6 418
6cc0b3c2
MC
419SSL_SESSION *lookup_sess_in_cache(SSL *s, const unsigned char *sess_id,
420 size_t sess_id_len)
421{
422 SSL_SESSION *ret = NULL;
6cc0b3c2
MC
423
424 if ((s->session_ctx->session_cache_mode
425 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
426 SSL_SESSION data;
427
428 data.ssl_version = s->version;
429 if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
430 return NULL;
431
432 memcpy(data.session_id, sess_id, sess_id_len);
433 data.session_id_length = sess_id_len;
434
435 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
436 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
437 if (ret != NULL) {
438 /* don't allow other threads to steal it: */
439 SSL_SESSION_up_ref(ret);
440 }
441 CRYPTO_THREAD_unlock(s->session_ctx->lock);
442 if (ret == NULL)
9ef9088c 443 tsan_counter(&s->session_ctx->stats.sess_miss);
6cc0b3c2
MC
444 }
445
446 if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
447 int copy = 1;
448
449 ret = s->session_ctx->get_session_cb(s, sess_id, sess_id_len, &copy);
450
451 if (ret != NULL) {
9ef9088c 452 tsan_counter(&s->session_ctx->stats.sess_cb_hit);
6cc0b3c2
MC
453
454 /*
455 * Increment reference count now if the session callback asks us
456 * to do so (note that if the session structures returned by the
457 * callback are shared between threads, it must handle the
458 * reference count itself [i.e. copy == 0], or things won't be
459 * thread-safe).
460 */
461 if (copy)
462 SSL_SESSION_up_ref(ret);
463
464 /*
465 * Add the externally cached session to the internal cache as
466 * well if and only if we are supposed to.
467 */
468 if ((s->session_ctx->session_cache_mode &
469 SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
470 /*
471 * Either return value of SSL_CTX_add_session should not
472 * interrupt the session resumption process. The return
473 * value is intentionally ignored.
474 */
475 (void)SSL_CTX_add_session(s->session_ctx, ret);
476 }
477 }
478 }
479
480 return ret;
481}
482
3a83462d
MC
483/*-
484 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
c519e89f
BM
485 * connection. It is only called by servers.
486 *
de7d61d5 487 * hello: The parsed ClientHello data
c519e89f
BM
488 *
489 * Returns:
1053a6e2
MC
490 * -1: fatal error
491 * 0: no session found
492 * 1: a session may have been found.
c519e89f
BM
493 *
494 * Side effects:
495 * - If a session is found then s->session is pointed at it (after freeing an
496 * existing session if need be) and s->verify_result is set from the session.
aff8c126 497 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
c519e89f
BM
498 * if the server should issue a new session ticket (to 0 otherwise).
499 */
f63a17d6 500int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello)
0f113f3e
MC
501{
502 /* This is used only by servers. */
b56bce4f 503
0f113f3e 504 SSL_SESSION *ret = NULL;
9ef9088c 505 int fatal = 0;
1053a6e2 506 int try_session_cache = 0;
61fb5923 507 SSL_TICKET_STATUS r;
d02b48c6 508
1053a6e2 509 if (SSL_IS_TLS13(s)) {
61fb5923
MC
510 /*
511 * By default we will send a new ticket. This can be overridden in the
512 * ticket processing.
513 */
514 s->ext.ticket_expected = 1;
fe874d27
MC
515 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
516 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
f63a17d6 517 NULL, 0)
fe874d27 518 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
f63a17d6 519 hello->pre_proc_exts, NULL, 0))
1053a6e2
MC
520 return -1;
521
522 ret = s->session;
523 } else {
524 /* sets s->ext.ticket_expected */
525 r = tls_get_ticket_from_client(s, hello, &ret);
526 switch (r) {
df0fed9a
TS
527 case SSL_TICKET_FATAL_ERR_MALLOC:
528 case SSL_TICKET_FATAL_ERR_OTHER:
1053a6e2 529 fatal = 1;
f63a17d6
MC
530 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_PREV_SESSION,
531 ERR_R_INTERNAL_ERROR);
1053a6e2 532 goto err;
df0fed9a
TS
533 case SSL_TICKET_NONE:
534 case SSL_TICKET_EMPTY:
6cc0b3c2 535 if (hello->session_id_len > 0) {
0afca811 536 try_session_cache = 1;
6cc0b3c2
MC
537 ret = lookup_sess_in_cache(s, hello->session_id,
538 hello->session_id_len);
539 }
61c32649 540 break;
df0fed9a
TS
541 case SSL_TICKET_NO_DECRYPT:
542 case SSL_TICKET_SUCCESS:
543 case SSL_TICKET_SUCCESS_RENEW:
1053a6e2 544 break;
1053a6e2 545 }
0f113f3e 546 }
c519e89f 547
0f113f3e
MC
548 if (ret == NULL)
549 goto err;
550
551 /* Now ret is non-NULL and we own one of its reference counts. */
552
128ae276
MC
553 /* Check TLS version consistency */
554 if (ret->ssl_version != s->version)
555 goto err;
556
0f113f3e
MC
557 if (ret->sid_ctx_length != s->sid_ctx_length
558 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
559 /*
560 * We have the session requested by the client, but we don't want to
561 * use it in this context.
562 */
563 goto err; /* treat like cache miss */
564 }
565
566 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
567 /*
568 * We can't be sure if this session is being used out of context,
569 * which is especially important for SSL_VERIFY_PEER. The application
570 * should have used SSL[_CTX]_set_session_id_context. For this error
571 * case, we generate an error instead of treating the event like a
572 * cache miss (otherwise it would be easy for applications to
573 * effectively disable the session cache by accident without anyone
574 * noticing).
575 */
576
f63a17d6
MC
577 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_PREV_SESSION,
578 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
0f113f3e
MC
579 fatal = 1;
580 goto err;
581 }
582
0f113f3e 583 if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
9ef9088c 584 tsan_counter(&s->session_ctx->stats.sess_timeout);
0f113f3e
MC
585 if (try_session_cache) {
586 /* session was from the cache, so remove it */
587 SSL_CTX_remove_session(s->session_ctx, ret);
588 }
589 goto err;
590 }
591
e7f0d921
DSH
592 /* Check extended master secret extension consistency */
593 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
594 /* If old session includes extms, but new does not: abort handshake */
555cbb32 595 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
f63a17d6
MC
596 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL_GET_PREV_SESSION,
597 SSL_R_INCONSISTENT_EXTMS);
e7f0d921
DSH
598 fatal = 1;
599 goto err;
600 }
555cbb32 601 } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
e7f0d921
DSH
602 /* If new session includes extms, but old does not: do not resume */
603 goto err;
604 }
605
1053a6e2
MC
606 if (!SSL_IS_TLS13(s)) {
607 /* We already did this for TLS1.3 */
608 SSL_SESSION_free(s->session);
609 s->session = ret;
610 }
0f113f3e 611
9ef9088c 612 tsan_counter(&s->session_ctx->stats.sess_hit);
0f113f3e
MC
613 s->verify_result = s->session->verify_result;
614 return 1;
8876bc05
BM
615
616 err:
0f113f3e
MC
617 if (ret != NULL) {
618 SSL_SESSION_free(ret);
1f5b44e9 619 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
128ae276
MC
620 if (SSL_IS_TLS13(s))
621 s->session = NULL;
e481f9b9 622
0f113f3e
MC
623 if (!try_session_cache) {
624 /*
625 * The session was from a ticket, so we should issue a ticket for
626 * the new session
627 */
aff8c126 628 s->ext.ticket_expected = 1;
0f113f3e 629 }
0f113f3e 630 }
f63a17d6 631 if (fatal)
0f113f3e 632 return -1;
40f805ad
MC
633
634 return 0;
0f113f3e 635}
d02b48c6 636
6b691a5c 637int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
0f113f3e 638{
9ef9088c 639 int ret = 0;
0f113f3e
MC
640 SSL_SESSION *s;
641
642 /*
643 * add just 1 reference count for the SSL_CTX's session cache even though
644 * it has two ways of access: each session is in a doubly linked list and
645 * an lhash
646 */
16203f7b 647 SSL_SESSION_up_ref(c);
0f113f3e
MC
648 /*
649 * if session c is in already in cache, we take back the increment later
650 */
651
16203f7b 652 CRYPTO_THREAD_write_lock(ctx->lock);
0f113f3e
MC
653 s = lh_SSL_SESSION_insert(ctx->sessions, c);
654
655 /*
656 * s != NULL iff we already had a session with the given PID. In this
657 * case, s == c should hold (then we did not really modify
658 * ctx->sessions), or we're in trouble.
659 */
660 if (s != NULL && s != c) {
661 /* We *are* in trouble ... */
662 SSL_SESSION_list_remove(ctx, s);
663 SSL_SESSION_free(s);
664 /*
665 * ... so pretend the other session did not exist in cache (we cannot
666 * handle two SSL_SESSION structures with identical session ID in the
667 * same cache, which could happen e.g. when two threads concurrently
668 * obtain the same session from an external cache)
669 */
670 s = NULL;
38088ce9
BE
671 } else if (s == NULL &&
672 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
673 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
674
675 /*
676 * ... so take back the extra reference and also don't add
677 * the session to the SSL_SESSION_list at this time
678 */
679 s = c;
0f113f3e
MC
680 }
681
682 /* Put at the head of the queue unless it is already in the cache */
683 if (s == NULL)
684 SSL_SESSION_list_add(ctx, c);
685
686 if (s != NULL) {
687 /*
688 * existing cache entry -- decrement previously incremented reference
689 * count because it already takes into account the cache
690 */
691
692 SSL_SESSION_free(s); /* s == c */
693 ret = 0;
694 } else {
695 /*
696 * new cache entry -- remove old ones if cache has become too large
697 */
698
699 ret = 1;
700
701 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
a230b26e 702 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
0f113f3e
MC
703 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
704 break;
705 else
9ef9088c 706 tsan_counter(&ctx->stats.sess_cache_full);
0f113f3e
MC
707 }
708 }
709 }
16203f7b
AG
710 CRYPTO_THREAD_unlock(ctx->lock);
711 return ret;
0f113f3e 712}
d02b48c6 713
6b691a5c 714int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
801294f8 715{
0f113f3e 716 return remove_session_lock(ctx, c, 1);
801294f8
DSH
717}
718
0fda2e37 719static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
0f113f3e
MC
720{
721 SSL_SESSION *r;
722 int ret = 0;
723
724 if ((c != NULL) && (c->session_id_length != 0)) {
725 if (lck)
16203f7b 726 CRYPTO_THREAD_write_lock(ctx->lock);
66d7de16 727 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
0f113f3e 728 ret = 1;
66d7de16
MC
729 r = lh_SSL_SESSION_delete(ctx->sessions, r);
730 SSL_SESSION_list_remove(ctx, r);
0f113f3e 731 }
e4612d02 732 c->not_resumable = 1;
0f113f3e
MC
733
734 if (lck)
16203f7b 735 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e 736
e4612d02
MC
737 if (ctx->remove_session_cb != NULL)
738 ctx->remove_session_cb(ctx, c);
c0a58e03
MH
739
740 if (ret)
741 SSL_SESSION_free(r);
0f113f3e
MC
742 } else
743 ret = 0;
26a7d938 744 return ret;
0f113f3e 745}
d02b48c6 746
6b691a5c 747void SSL_SESSION_free(SSL_SESSION *ss)
0f113f3e
MC
748{
749 int i;
d02b48c6 750
e6e9170d
RS
751 if (ss == NULL)
752 return;
2f545ae4 753 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
f3f1cf84 754 REF_PRINT_COUNT("SSL_SESSION", ss);
0f113f3e
MC
755 if (i > 0)
756 return;
f3f1cf84 757 REF_ASSERT_ISNT(i < 0);
d02b48c6 758
0f113f3e 759 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
58964a49 760
cbe29648
RS
761 OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
762 OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
222561fe 763 X509_free(ss->peer);
c34b0f99 764 sk_X509_pop_free(ss->peer_chain, X509_free);
aff8c126
RS
765 OPENSSL_free(ss->ext.hostname);
766 OPENSSL_free(ss->ext.tick);
ddac1974 767#ifndef OPENSSL_NO_PSK
25aaa98a
RS
768 OPENSSL_free(ss->psk_identity_hint);
769 OPENSSL_free(ss->psk_identity);
edc032b5
BL
770#endif
771#ifndef OPENSSL_NO_SRP
25aaa98a 772 OPENSSL_free(ss->srp_username);
ed3883d2 773#endif
f6370040 774 OPENSSL_free(ss->ext.alpn_selected);
df0fed9a 775 OPENSSL_free(ss->ticket_appdata);
16203f7b 776 CRYPTO_THREAD_lock_free(ss->lock);
4b45c6e5 777 OPENSSL_clear_free(ss, sizeof(*ss));
0f113f3e 778}
d02b48c6 779
16203f7b
AG
780int SSL_SESSION_up_ref(SSL_SESSION *ss)
781{
782 int i;
783
2f545ae4 784 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
16203f7b
AG
785 return 0;
786
787 REF_PRINT_COUNT("SSL_SESSION", ss);
788 REF_ASSERT_ISNT(i < 2);
789 return ((i > 1) ? 1 : 0);
790}
791
6b691a5c 792int SSL_set_session(SSL *s, SSL_SESSION *session)
0f113f3e 793{
e70656cf
MC
794 ssl_clear_bad_session(s);
795 if (s->ctx->method != s->method) {
796 if (!SSL_set_ssl_method(s, s->ctx->method))
797 return 0;
798 }
0f113f3e 799
e70656cf 800 if (session != NULL) {
16203f7b 801 SSL_SESSION_up_ref(session);
e70656cf 802 s->verify_result = session->verify_result;
0f113f3e 803 }
e70656cf
MC
804 SSL_SESSION_free(s->session);
805 s->session = session;
806
807 return 1;
0f113f3e 808}
d02b48c6 809
fddfc0af
RG
810int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
811 unsigned int sid_len)
812{
813 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
814 SSLerr(SSL_F_SSL_SESSION_SET1_ID,
815 SSL_R_SSL_SESSION_ID_TOO_LONG);
816 return 0;
817 }
818 s->session_id_length = sid_len;
6aad9393
RG
819 if (sid != s->session_id)
820 memcpy(s->session_id, sid, sid_len);
fddfc0af
RG
821 return 1;
822}
823
6b691a5c 824long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
0f113f3e
MC
825{
826 if (s == NULL)
26a7d938 827 return 0;
0f113f3e 828 s->timeout = t;
208fb891 829 return 1;
0f113f3e 830}
d02b48c6 831
0821bcd4 832long SSL_SESSION_get_timeout(const SSL_SESSION *s)
0f113f3e
MC
833{
834 if (s == NULL)
26a7d938
K
835 return 0;
836 return s->timeout;
0f113f3e 837}
d02b48c6 838
0821bcd4 839long SSL_SESSION_get_time(const SSL_SESSION *s)
0f113f3e
MC
840{
841 if (s == NULL)
26a7d938
K
842 return 0;
843 return s->time;
0f113f3e 844}
d02b48c6 845
6b691a5c 846long SSL_SESSION_set_time(SSL_SESSION *s, long t)
0f113f3e
MC
847{
848 if (s == NULL)
26a7d938 849 return 0;
0f113f3e 850 s->time = t;
26a7d938 851 return t;
0f113f3e 852}
d02b48c6 853
bd01f649
TS
854int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
855{
bd01f649
TS
856 return s->ssl_version;
857}
858
5a43d511
MC
859int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
860{
861 s->ssl_version = version;
862 return 1;
863}
864
e9281323
RS
865const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
866{
867 return s->cipher;
868}
1a993d1d
MC
869
870int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
871{
872 s->cipher = cipher;
873 return 1;
874}
e9281323 875
4b6b8487
LC
876const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
877{
aff8c126 878 return s->ext.hostname;
4b6b8487
LC
879}
880
67738645
MC
881int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
882{
883 OPENSSL_free(s->ext.hostname);
884 if (hostname == NULL) {
885 s->ext.hostname = NULL;
886 return 1;
887 }
888 s->ext.hostname = OPENSSL_strdup(hostname);
889
890 return s->ext.hostname != NULL;
891}
892
f2baac27
MC
893int SSL_SESSION_has_ticket(const SSL_SESSION *s)
894{
aff8c126 895 return (s->ext.ticklen > 0) ? 1 : 0;
f2baac27
MC
896}
897
898unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
899{
aff8c126 900 return s->ext.tick_lifetime_hint;
f2baac27
MC
901}
902
48593cb1 903void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
a230b26e 904 size_t *len)
b7c9187b 905{
aff8c126 906 *len = s->ext.ticklen;
61986d32 907 if (tick != NULL)
aff8c126 908 *tick = s->ext.tick;
b7c9187b
MC
909}
910
fcc47578
MC
911uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
912{
913 return s->ext.max_early_data;
914}
915
98e1d934
MC
916int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
917{
918 s->ext.max_early_data = max_early_data;
919
920 return 1;
921}
922
67738645
MC
923void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
924 const unsigned char **alpn,
925 size_t *len)
926{
927 *alpn = s->ext.alpn_selected;
928 *len = s->ext.alpn_selected_len;
929}
930
931int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
932 size_t len)
933{
934 OPENSSL_free(s->ext.alpn_selected);
935 if (alpn == NULL || len == 0) {
936 s->ext.alpn_selected = NULL;
937 s->ext.alpn_selected_len = 0;
938 return 1;
939 }
940 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
941 if (s->ext.alpn_selected == NULL) {
942 s->ext.alpn_selected_len = 0;
943 return 0;
944 }
945 s->ext.alpn_selected_len = len;
946
947 return 1;
948}
949
08557cf2 950X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
0f113f3e
MC
951{
952 return s->peer;
953}
954
955int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
956 unsigned int sid_ctx_len)
957{
958 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
959 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
960 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
961 return 0;
962 }
963 s->sid_ctx_length = sid_ctx_len;
6aad9393
RG
964 if (sid_ctx != s->sid_ctx)
965 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
0f113f3e
MC
966
967 return 1;
968}
08557cf2 969
e586eac8
MC
970int SSL_SESSION_is_resumable(const SSL_SESSION *s)
971{
972 /*
973 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
974 * session ID.
975 */
976 return !s->not_resumable
977 && (s->session_id_length > 0 || s->ext.ticklen > 0);
978}
979
6b691a5c 980long SSL_CTX_set_timeout(SSL_CTX *s, long t)
0f113f3e
MC
981{
982 long l;
983 if (s == NULL)
26a7d938 984 return 0;
0f113f3e
MC
985 l = s->session_timeout;
986 s->session_timeout = t;
26a7d938 987 return l;
0f113f3e 988}
413c4f45 989
0821bcd4 990long SSL_CTX_get_timeout(const SSL_CTX *s)
0f113f3e
MC
991{
992 if (s == NULL)
26a7d938
K
993 return 0;
994 return s->session_timeout;
0f113f3e 995}
413c4f45 996
0f113f3e 997int SSL_set_session_secret_cb(SSL *s,
aff8c126 998 tls_session_secret_cb_fn tls_session_secret_cb,
0f113f3e
MC
999 void *arg)
1000{
1001 if (s == NULL)
26a7d938 1002 return 0;
aff8c126
RS
1003 s->ext.session_secret_cb = tls_session_secret_cb;
1004 s->ext.session_secret_cb_arg = arg;
208fb891 1005 return 1;
0f113f3e 1006}
12bf56c0
DSH
1007
1008int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
0f113f3e
MC
1009 void *arg)
1010{
1011 if (s == NULL)
26a7d938 1012 return 0;
aff8c126
RS
1013 s->ext.session_ticket_cb = cb;
1014 s->ext.session_ticket_cb_arg = arg;
208fb891 1015 return 1;
0f113f3e 1016}
12bf56c0
DSH
1017
1018int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
0f113f3e
MC
1019{
1020 if (s->version >= TLS1_VERSION) {
aff8c126
RS
1021 OPENSSL_free(s->ext.session_ticket);
1022 s->ext.session_ticket = NULL;
1023 s->ext.session_ticket =
0f113f3e 1024 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
aff8c126 1025 if (s->ext.session_ticket == NULL) {
0f113f3e
MC
1026 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1027 return 0;
1028 }
1029
1ed327f7 1030 if (ext_data != NULL) {
aff8c126
RS
1031 s->ext.session_ticket->length = ext_len;
1032 s->ext.session_ticket->data = s->ext.session_ticket + 1;
1033 memcpy(s->ext.session_ticket->data, ext_data, ext_len);
0f113f3e 1034 } else {
aff8c126
RS
1035 s->ext.session_ticket->length = 0;
1036 s->ext.session_ticket->data = NULL;
0f113f3e
MC
1037 }
1038
1039 return 1;
1040 }
1041
1042 return 0;
1043}
0f113f3e
MC
1044
1045typedef struct timeout_param_st {
1046 SSL_CTX *ctx;
1047 long time;
1048 LHASH_OF(SSL_SESSION) *cache;
1049} TIMEOUT_PARAM;
d02b48c6 1050
2a056de8 1051static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p)
0f113f3e
MC
1052{
1053 if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */
1054 /*
1055 * The reason we don't call SSL_CTX_remove_session() is to save on
1056 * locking overhead
1057 */
1058 (void)lh_SSL_SESSION_delete(p->cache, s);
1059 SSL_SESSION_list_remove(p->ctx, s);
1060 s->not_resumable = 1;
1061 if (p->ctx->remove_session_cb != NULL)
1062 p->ctx->remove_session_cb(p->ctx, s);
1063 SSL_SESSION_free(s);
1064 }
1065}
d02b48c6 1066
2a056de8 1067IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM);
3c914840 1068
6b691a5c 1069void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
0f113f3e
MC
1070{
1071 unsigned long i;
1072 TIMEOUT_PARAM tp;
1073
1074 tp.ctx = s;
1075 tp.cache = s->sessions;
1076 if (tp.cache == NULL)
1077 return;
1078 tp.time = t;
16203f7b 1079 CRYPTO_THREAD_write_lock(s->lock);
739a1eb1
RS
1080 i = lh_SSL_SESSION_get_down_load(s->sessions);
1081 lh_SSL_SESSION_set_down_load(s->sessions, 0);
2a056de8 1082 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
739a1eb1 1083 lh_SSL_SESSION_set_down_load(s->sessions, i);
16203f7b 1084 CRYPTO_THREAD_unlock(s->lock);
0f113f3e 1085}
d02b48c6 1086
6b691a5c 1087int ssl_clear_bad_session(SSL *s)
0f113f3e
MC
1088{
1089 if ((s->session != NULL) &&
1090 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1091 !(SSL_in_init(s) || SSL_in_before(s))) {
e2bb9b9b 1092 SSL_CTX_remove_session(s->session_ctx, s->session);
208fb891 1093 return 1;
0f113f3e 1094 } else
26a7d938 1095 return 0;
0f113f3e 1096}
58964a49
RE
1097
1098/* locked by SSL_CTX in the calling function */
6b691a5c 1099static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
0f113f3e
MC
1100{
1101 if ((s->next == NULL) || (s->prev == NULL))
1102 return;
1103
1104 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1105 /* last element in list */
1106 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1107 /* only one element in list */
1108 ctx->session_cache_head = NULL;
1109 ctx->session_cache_tail = NULL;
1110 } else {
1111 ctx->session_cache_tail = s->prev;
1112 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1113 }
1114 } else {
1115 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1116 /* first element in list */
1117 ctx->session_cache_head = s->next;
1118 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1119 } else {
1120 /* middle of list */
1121 s->next->prev = s->prev;
1122 s->prev->next = s->next;
1123 }
1124 }
1125 s->prev = s->next = NULL;
1126}
58964a49 1127
6b691a5c 1128static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
0f113f3e
MC
1129{
1130 if ((s->next != NULL) && (s->prev != NULL))
1131 SSL_SESSION_list_remove(ctx, s);
1132
1133 if (ctx->session_cache_head == NULL) {
1134 ctx->session_cache_head = s;
1135 ctx->session_cache_tail = s;
1136 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1137 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1138 } else {
1139 s->next = ctx->session_cache_head;
1140 s->next->prev = s;
1141 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1142 ctx->session_cache_head = s;
1143 }
1144}
58964a49 1145
7806f3dd 1146void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
a230b26e 1147 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
0f113f3e
MC
1148{
1149 ctx->new_session_cb = cb;
1150}
7806f3dd 1151
0f113f3e
MC
1152int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1153 return ctx->new_session_cb;
1154}
7806f3dd
NL
1155
1156void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
0f113f3e
MC
1157 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1158{
1159 ctx->remove_session_cb = cb;
1160}
7806f3dd 1161
0f113f3e
MC
1162void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1163 SSL_SESSION *sess) {
1164 return ctx->remove_session_cb;
1165}
7806f3dd
NL
1166
1167void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
0f113f3e 1168 SSL_SESSION *(*cb) (struct ssl_st *ssl,
b6981744
EK
1169 const unsigned char *data,
1170 int len, int *copy))
0f113f3e
MC
1171{
1172 ctx->get_session_cb = cb;
1173}
1174
1175SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
a230b26e
EK
1176 const unsigned char
1177 *data, int len,
1178 int *copy) {
0f113f3e
MC
1179 return ctx->get_session_cb;
1180}
1181
1182void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1183 void (*cb) (const SSL *ssl, int type, int val))
1184{
1185 ctx->info_callback = cb;
1186}
1187
1188void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1189 int val) {
1190 return ctx->info_callback;
1191}
7806f3dd
NL
1192
1193void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
0f113f3e
MC
1194 int (*cb) (SSL *ssl, X509 **x509,
1195 EVP_PKEY **pkey))
1196{
1197 ctx->client_cert_cb = cb;
1198}
7806f3dd 1199
0f113f3e
MC
1200int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1201 EVP_PKEY **pkey) {
1202 return ctx->client_cert_cb;
1203}
7806f3dd 1204
368888bc
DSH
1205#ifndef OPENSSL_NO_ENGINE
1206int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
0f113f3e
MC
1207{
1208 if (!ENGINE_init(e)) {
1209 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
1210 return 0;
1211 }
1212 if (!ENGINE_get_ssl_client_cert_function(e)) {
1213 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE,
1214 SSL_R_NO_CLIENT_CERT_METHOD);
1215 ENGINE_finish(e);
1216 return 0;
1217 }
1218 ctx->client_cert_engine = e;
1219 return 1;
1220}
368888bc
DSH
1221#endif
1222
7806f3dd 1223void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
0f113f3e
MC
1224 int (*cb) (SSL *ssl,
1225 unsigned char *cookie,
1226 unsigned int *cookie_len))
1227{
1228 ctx->app_gen_cookie_cb = cb;
1229}
7806f3dd
NL
1230
1231void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
a230b26e
EK
1232 int (*cb) (SSL *ssl,
1233 const unsigned char *cookie,
0f113f3e
MC
1234 unsigned int cookie_len))
1235{
1236 ctx->app_verify_cookie_cb = cb;
1237}
7806f3dd 1238
df0fed9a
TS
1239int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1240{
1241 OPENSSL_free(ss->ticket_appdata);
1242 ss->ticket_appdata_len = 0;
1243 if (data == NULL || len == 0) {
1244 ss->ticket_appdata = NULL;
1245 return 1;
1246 }
1247 ss->ticket_appdata = OPENSSL_memdup(data, len);
1248 if (ss->ticket_appdata != NULL) {
1249 ss->ticket_appdata_len = len;
1250 return 1;
1251 }
1252 return 0;
1253}
1254
1255int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1256{
1257 *data = ss->ticket_appdata;
1258 *len = ss->ticket_appdata_len;
1259 return 1;
1260}
1261
3fa2812f
BS
1262void SSL_CTX_set_stateless_cookie_generate_cb(
1263 SSL_CTX *ctx,
1264 int (*cb) (SSL *ssl,
1265 unsigned char *cookie,
1266 size_t *cookie_len))
1267{
1268 ctx->gen_stateless_cookie_cb = cb;
1269}
1270
1271void SSL_CTX_set_stateless_cookie_verify_cb(
1272 SSL_CTX *ctx,
1273 int (*cb) (SSL *ssl,
1274 const unsigned char *cookie,
1275 size_t cookie_len))
1276{
1277 ctx->verify_stateless_cookie_cb = cb;
1278}
1279
a230b26e 1280IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)