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