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