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