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