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