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