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