xref: /openbsd/lib/libssl/ssl_sess.c (revision f4fe6251)
1 /* $OpenBSD: ssl_sess.c,v 1.128 2024/07/22 14:47:15 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 /* ====================================================================
112  * Copyright 2005 Nokia. All rights reserved.
113  *
114  * The portions of the attached software ("Contribution") is developed by
115  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116  * license.
117  *
118  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120  * support (see RFC 4279) to OpenSSL.
121  *
122  * No patent licenses or other rights except those expressly stated in
123  * the OpenSSL open source license shall be deemed granted or received
124  * expressly, by implication, estoppel, or otherwise.
125  *
126  * No assurances are provided by Nokia that the Contribution does not
127  * infringe the patent or other intellectual property rights of any third
128  * party or that the license provides you with all the necessary rights
129  * to make use of the Contribution.
130  *
131  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135  * OTHERWISE.
136  */
137 
138 #include <openssl/lhash.h>
139 #include <openssl/opensslconf.h>
140 
141 #include "ssl_local.h"
142 
143 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
144 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
145 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
146 
147 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
148 SSL_SESSION *
SSL_get_session(const SSL * ssl)149 SSL_get_session(const SSL *ssl)
150 {
151 	return (ssl->session);
152 }
153 LSSL_ALIAS(SSL_get_session);
154 
155 /* variant of SSL_get_session: caller really gets something */
156 SSL_SESSION *
SSL_get1_session(SSL * ssl)157 SSL_get1_session(SSL *ssl)
158 {
159 	SSL_SESSION *sess;
160 
161 	/*
162 	 * Need to lock this all up rather than just use CRYPTO_add so that
163 	 * somebody doesn't free ssl->session between when we check it's
164 	 * non-null and when we up the reference count.
165 	 */
166 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
167 	sess = ssl->session;
168 	if (sess)
169 		sess->references++;
170 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
171 
172 	return (sess);
173 }
174 LSSL_ALIAS(SSL_get1_session);
175 
176 int
SSL_SESSION_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)177 SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
178     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
179 {
180 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION,
181 	    argl, argp, new_func, dup_func, free_func);
182 }
183 LSSL_ALIAS(SSL_SESSION_get_ex_new_index);
184 
185 int
SSL_SESSION_set_ex_data(SSL_SESSION * s,int idx,void * arg)186 SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
187 {
188 	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
189 }
190 LSSL_ALIAS(SSL_SESSION_set_ex_data);
191 
192 void *
SSL_SESSION_get_ex_data(const SSL_SESSION * s,int idx)193 SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
194 {
195 	return (CRYPTO_get_ex_data(&s->ex_data, idx));
196 }
197 LSSL_ALIAS(SSL_SESSION_get_ex_data);
198 
199 uint32_t
SSL_SESSION_get_max_early_data(const SSL_SESSION * s)200 SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
201 {
202 	return 0;
203 }
204 LSSL_ALIAS(SSL_SESSION_get_max_early_data);
205 
206 int
SSL_SESSION_set_max_early_data(SSL_SESSION * s,uint32_t max_early_data)207 SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
208 {
209 	return 1;
210 }
211 LSSL_ALIAS(SSL_SESSION_set_max_early_data);
212 
213 SSL_SESSION *
SSL_SESSION_new(void)214 SSL_SESSION_new(void)
215 {
216 	SSL_SESSION *ss;
217 
218 	if (!OPENSSL_init_ssl(0, NULL)) {
219 		SSLerrorx(SSL_R_LIBRARY_BUG);
220 		return(NULL);
221 	}
222 
223 	if ((ss = calloc(1, sizeof(*ss))) == NULL) {
224 		SSLerrorx(ERR_R_MALLOC_FAILURE);
225 		return (NULL);
226 	}
227 
228 	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
229 	ss->references = 1;
230 	ss->timeout = 60 * 5 + 4; /* 5 minutes 4 seconds timeout by default */
231 	ss->time = time(NULL);
232 	ss->prev = NULL;
233 	ss->next = NULL;
234 	ss->tlsext_hostname = NULL;
235 
236 	ss->peer_cert_type = -1;
237 
238 	ss->tlsext_ecpointformatlist_length = 0;
239 	ss->tlsext_ecpointformatlist = NULL;
240 	ss->tlsext_supportedgroups_length = 0;
241 	ss->tlsext_supportedgroups = NULL;
242 
243 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
244 
245 	return (ss);
246 }
247 LSSL_ALIAS(SSL_SESSION_new);
248 
249 SSL_SESSION *
ssl_session_dup(SSL_SESSION * sess,int include_ticket)250 ssl_session_dup(SSL_SESSION *sess, int include_ticket)
251 {
252 	SSL_SESSION *copy;
253 	CBS cbs;
254 
255 	if ((copy = calloc(1, sizeof(*copy))) == NULL) {
256 		SSLerrorx(ERR_R_MALLOC_FAILURE);
257 		goto err;
258 	}
259 
260 	copy->ssl_version = sess->ssl_version;
261 
262 	CBS_init(&cbs, sess->master_key, sess->master_key_length);
263 	if (!CBS_write_bytes(&cbs, copy->master_key, sizeof(copy->master_key),
264 	    &copy->master_key_length))
265 		goto err;
266 
267 	CBS_init(&cbs, sess->session_id, sess->session_id_length);
268 	if (!CBS_write_bytes(&cbs, copy->session_id, sizeof(copy->session_id),
269 	    &copy->session_id_length))
270 		goto err;
271 
272 	CBS_init(&cbs, sess->sid_ctx, sess->sid_ctx_length);
273 	if (!CBS_write_bytes(&cbs, copy->sid_ctx, sizeof(copy->sid_ctx),
274 	    &copy->sid_ctx_length))
275 		goto err;
276 
277 	if (sess->peer_cert != NULL) {
278 		if (!X509_up_ref(sess->peer_cert))
279 			goto err;
280 		copy->peer_cert = sess->peer_cert;
281 	}
282 	copy->peer_cert_type = sess->peer_cert_type;
283 
284 	copy->verify_result = sess->verify_result;
285 
286 	copy->timeout = sess->timeout;
287 	copy->time = sess->time;
288 	copy->references = 1;
289 
290 	copy->cipher_value = sess->cipher_value;
291 
292 	if (sess->tlsext_hostname != NULL) {
293 		copy->tlsext_hostname = strdup(sess->tlsext_hostname);
294 		if (copy->tlsext_hostname == NULL)
295 			goto err;
296 	}
297 
298 	if (include_ticket) {
299 		CBS_init(&cbs, sess->tlsext_tick, sess->tlsext_ticklen);
300 		if (!CBS_stow(&cbs, &copy->tlsext_tick, &copy->tlsext_ticklen))
301 			goto err;
302 		copy->tlsext_tick_lifetime_hint =
303 		    sess->tlsext_tick_lifetime_hint;
304 
305 		/*
306 		 * XXX - copy sess->resumption_master_secret and all other
307 		 * TLSv1.3 info here.
308 		 */
309 	}
310 
311 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, copy,
312 	    &copy->ex_data))
313 		goto err;
314 
315 	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, &copy->ex_data,
316 	    &sess->ex_data))
317 		goto err;
318 
319 	/* Omit prev/next: the new session gets its own slot in the cache. */
320 
321 	copy->not_resumable = sess->not_resumable;
322 
323 	CBS_init(&cbs, sess->tlsext_ecpointformatlist,
324 	    sess->tlsext_ecpointformatlist_length);
325 	if (!CBS_stow(&cbs, &copy->tlsext_ecpointformatlist,
326 	    &copy->tlsext_ecpointformatlist_length))
327 		goto err;
328 
329 	if (sess->tlsext_supportedgroups != NULL) {
330 		if ((copy->tlsext_supportedgroups = calloc(sizeof(uint16_t),
331 		    sess->tlsext_supportedgroups_length)) == NULL)
332 			goto err;
333 		memcpy(copy->tlsext_supportedgroups,
334 		    sess->tlsext_supportedgroups,
335 		    sizeof(uint16_t) * sess->tlsext_supportedgroups_length);
336 		copy->tlsext_supportedgroups_length =
337 		    sess->tlsext_supportedgroups_length;
338 	}
339 
340 	return copy;
341 
342  err:
343 	SSL_SESSION_free(copy);
344 
345 	return NULL;
346 }
347 
348 const unsigned char *
SSL_SESSION_get_id(const SSL_SESSION * ss,unsigned int * len)349 SSL_SESSION_get_id(const SSL_SESSION *ss, unsigned int *len)
350 {
351 	if (len != NULL)
352 		*len = (unsigned int)ss->session_id_length;
353 	return ss->session_id;
354 }
355 LSSL_ALIAS(SSL_SESSION_get_id);
356 
357 const unsigned char *
SSL_SESSION_get0_id_context(const SSL_SESSION * ss,unsigned int * len)358 SSL_SESSION_get0_id_context(const SSL_SESSION *ss, unsigned int *len)
359 {
360 	if (len != NULL)
361 		*len = (unsigned int)ss->sid_ctx_length;
362 	return ss->sid_ctx;
363 }
364 LSSL_ALIAS(SSL_SESSION_get0_id_context);
365 
366 unsigned int
SSL_SESSION_get_compress_id(const SSL_SESSION * ss)367 SSL_SESSION_get_compress_id(const SSL_SESSION *ss)
368 {
369 	return 0;
370 }
371 LSSL_ALIAS(SSL_SESSION_get_compress_id);
372 
373 unsigned long
SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION * s)374 SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
375 {
376 	return s->tlsext_tick_lifetime_hint;
377 }
378 LSSL_ALIAS(SSL_SESSION_get_ticket_lifetime_hint);
379 
380 int
SSL_SESSION_has_ticket(const SSL_SESSION * s)381 SSL_SESSION_has_ticket(const SSL_SESSION *s)
382 {
383 	return (s->tlsext_ticklen > 0) ? 1 : 0;
384 }
385 LSSL_ALIAS(SSL_SESSION_has_ticket);
386 
387 /*
388  * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
389  * the ID with random gunk repeatedly until we have no conflict is going to
390  * complete in one iteration pretty much "most" of the time (btw:
391  * understatement). So, if it takes us 10 iterations and we still can't avoid
392  * a conflict - well that's a reasonable point to call it quits. Either the
393  * arc4random code is broken or someone is trying to open roughly very close to
394  * 2^128 (or 2^256) SSL sessions to our server. How you might store that many
395  * sessions is perhaps a more interesting question...
396  */
397 
398 #define MAX_SESS_ID_ATTEMPTS 10
399 
400 static int
def_generate_session_id(const SSL * ssl,unsigned char * id,unsigned int * id_len)401 def_generate_session_id(const SSL *ssl, unsigned char *id, unsigned int *id_len)
402 {
403 	unsigned int retry = 0;
404 
405 	do {
406 		arc4random_buf(id, *id_len);
407 	} while (SSL_has_matching_session_id(ssl, id, *id_len) &&
408 	    (++retry < MAX_SESS_ID_ATTEMPTS));
409 
410 	if (retry < MAX_SESS_ID_ATTEMPTS)
411 		return 1;
412 
413 	/* else - woops a session_id match */
414 	/* XXX We should also check the external cache --
415 	 * but the probability of a collision is negligible, and
416 	 * we could not prevent the concurrent creation of sessions
417 	 * with identical IDs since we currently don't have means
418 	 * to atomically check whether a session ID already exists
419 	 * and make a reservation for it if it does not
420 	 * (this problem applies to the internal cache as well).
421 	 */
422 	return 0;
423 }
424 
425 int
ssl_get_new_session(SSL * s,int session)426 ssl_get_new_session(SSL *s, int session)
427 {
428 	unsigned int tmp;
429 	SSL_SESSION *ss = NULL;
430 	GEN_SESSION_CB cb = def_generate_session_id;
431 
432 	/* This gets used by clients and servers. */
433 
434 	if ((ss = SSL_SESSION_new()) == NULL)
435 		return (0);
436 
437 	/* If the context has a default timeout, use it */
438 	if (s->session_ctx->session_timeout == 0)
439 		ss->timeout = SSL_get_default_timeout(s);
440 	else
441 		ss->timeout = s->session_ctx->session_timeout;
442 
443 	if (s->session != NULL) {
444 		SSL_SESSION_free(s->session);
445 		s->session = NULL;
446 	}
447 
448 	if (session) {
449 		switch (s->version) {
450 		case TLS1_VERSION:
451 		case TLS1_1_VERSION:
452 		case TLS1_2_VERSION:
453 		case DTLS1_VERSION:
454 		case DTLS1_2_VERSION:
455 			ss->ssl_version = s->version;
456 			ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
457 			break;
458 		default:
459 			SSLerror(s, SSL_R_UNSUPPORTED_SSL_VERSION);
460 			SSL_SESSION_free(ss);
461 			return (0);
462 		}
463 
464 		/* If RFC4507 ticket use empty session ID. */
465 		if (s->tlsext_ticket_expected) {
466 			ss->session_id_length = 0;
467 			goto sess_id_done;
468 		}
469 
470 		/* Choose which callback will set the session ID. */
471 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
472 		if (s->generate_session_id)
473 			cb = s->generate_session_id;
474 		else if (s->session_ctx->generate_session_id)
475 			cb = s->session_ctx->generate_session_id;
476 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
477 
478 		/* Choose a session ID. */
479 		tmp = ss->session_id_length;
480 		if (!cb(s, ss->session_id, &tmp)) {
481 			/* The callback failed */
482 			SSLerror(s, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
483 			SSL_SESSION_free(ss);
484 			return (0);
485 		}
486 
487 		/*
488 		 * Don't allow the callback to set the session length to zero.
489 		 * nor set it higher than it was.
490 		 */
491 		if (tmp == 0 || tmp > ss->session_id_length) {
492 			/* The callback set an illegal length */
493 			SSLerror(s, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
494 			SSL_SESSION_free(ss);
495 			return (0);
496 		}
497 		ss->session_id_length = tmp;
498 
499 		/* Finally, check for a conflict. */
500 		if (SSL_has_matching_session_id(s, ss->session_id,
501 		    ss->session_id_length)) {
502 			SSLerror(s, SSL_R_SSL_SESSION_ID_CONFLICT);
503 			SSL_SESSION_free(ss);
504 			return (0);
505 		}
506 
507  sess_id_done:
508 		if (s->tlsext_hostname) {
509 			ss->tlsext_hostname = strdup(s->tlsext_hostname);
510 			if (ss->tlsext_hostname == NULL) {
511 				SSLerror(s, ERR_R_INTERNAL_ERROR);
512 				SSL_SESSION_free(ss);
513 				return 0;
514 			}
515 		}
516 	} else {
517 		ss->session_id_length = 0;
518 	}
519 
520 	if (s->sid_ctx_length > sizeof ss->sid_ctx) {
521 		SSLerror(s, ERR_R_INTERNAL_ERROR);
522 		SSL_SESSION_free(ss);
523 		return 0;
524 	}
525 
526 	memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
527 	ss->sid_ctx_length = s->sid_ctx_length;
528 	s->session = ss;
529 	ss->ssl_version = s->version;
530 	ss->verify_result = X509_V_OK;
531 
532 	return (1);
533 }
534 
535 static SSL_SESSION *
ssl_session_from_cache(SSL * s,CBS * session_id)536 ssl_session_from_cache(SSL *s, CBS *session_id)
537 {
538 	SSL_SESSION *sess;
539 	SSL_SESSION data;
540 
541 	if ((s->session_ctx->session_cache_mode &
542 	     SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
543 		return NULL;
544 
545 	memset(&data, 0, sizeof(data));
546 
547 	data.ssl_version = s->version;
548 
549 	if (!CBS_write_bytes(session_id, data.session_id,
550 	    sizeof(data.session_id), &data.session_id_length))
551 		return NULL;
552 
553 	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
554 	sess = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
555 	if (sess != NULL)
556 		CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
557 	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
558 
559 	if (sess == NULL)
560 		s->session_ctx->stats.sess_miss++;
561 
562 	return sess;
563 }
564 
565 static SSL_SESSION *
ssl_session_from_callback(SSL * s,CBS * session_id)566 ssl_session_from_callback(SSL *s, CBS *session_id)
567 {
568 	SSL_SESSION *sess;
569 	int copy;
570 
571 	if (s->session_ctx->get_session_cb == NULL)
572 		return NULL;
573 
574 	copy = 1;
575 	if ((sess = s->session_ctx->get_session_cb(s,
576 	    CBS_data(session_id), CBS_len(session_id), &copy)) == NULL)
577 		return NULL;
578 	/*
579 	 * The copy handler may have set copy == 0 to indicate that the session
580 	 * structures are shared between threads and that it handles the
581 	 * reference count itself. If it didn't set copy to zero, we must
582 	 * increment the reference count.
583 	 */
584 	if (copy)
585 		CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
586 
587 	s->session_ctx->stats.sess_cb_hit++;
588 
589 	/* Add the externally cached session to the internal cache as well. */
590 	if (!(s->session_ctx->session_cache_mode &
591 	    SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
592 		/*
593 		 * The following should not return 1,
594 		 * otherwise, things are very strange.
595 		 */
596 		SSL_CTX_add_session(s->session_ctx, sess);
597 	}
598 
599 	return sess;
600 }
601 
602 static SSL_SESSION *
ssl_session_by_id(SSL * s,CBS * session_id)603 ssl_session_by_id(SSL *s, CBS *session_id)
604 {
605 	SSL_SESSION *sess;
606 
607 	if (CBS_len(session_id) == 0)
608 		return NULL;
609 
610 	if ((sess = ssl_session_from_cache(s, session_id)) == NULL)
611 		sess = ssl_session_from_callback(s, session_id);
612 
613 	return sess;
614 }
615 
616 /*
617  * ssl_get_prev_session attempts to find an SSL_SESSION to be used to resume
618  * this connection. It is only called by servers.
619  *
620  *   session_id: points at the session ID in the ClientHello. This code will
621  *       read past the end of this in order to parse out the session ticket
622  *       extension, if any.
623  *   ext_block: a CBS for the ClientHello extensions block.
624  *   alert: alert that the caller should send in case of failure.
625  *
626  * Returns:
627  *   -1: error
628  *    0: a session may have been found.
629  *
630  * Side effects:
631  *   - If a session is found then s->session is pointed at it (after freeing
632  *     an existing session if need be) and s->verify_result is set from the
633  *     session.
634  *   - For both new and resumed sessions, s->tlsext_ticket_expected
635  *     indicates whether the server should issue a new session ticket or not.
636  */
637 int
ssl_get_prev_session(SSL * s,CBS * session_id,CBS * ext_block,int * alert)638 ssl_get_prev_session(SSL *s, CBS *session_id, CBS *ext_block, int *alert)
639 {
640 	SSL_SESSION *sess = NULL;
641 	int alert_desc = SSL_AD_INTERNAL_ERROR, fatal = 0;
642 	int ticket_decrypted = 0;
643 
644 	/* This is used only by servers. */
645 
646 	if (CBS_len(session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH)
647 		goto err;
648 
649 	/* Sets s->tlsext_ticket_expected. */
650 	switch (tls1_process_ticket(s, ext_block, &alert_desc, &sess)) {
651 	case TLS1_TICKET_FATAL_ERROR:
652 		fatal = 1;
653 		goto err;
654 	case TLS1_TICKET_NONE:
655 	case TLS1_TICKET_EMPTY:
656 		if ((sess = ssl_session_by_id(s, session_id)) == NULL)
657 			goto err;
658 		break;
659 	case TLS1_TICKET_NOT_DECRYPTED:
660 		goto err;
661 	case TLS1_TICKET_DECRYPTED:
662 		ticket_decrypted = 1;
663 
664 		/*
665 		 * The session ID is used by some clients to detect that the
666 		 * ticket has been accepted so we copy it into sess.
667 		 */
668 		if (!CBS_write_bytes(session_id, sess->session_id,
669 		    sizeof(sess->session_id), &sess->session_id_length)) {
670 			fatal = 1;
671 			goto err;
672 		}
673 		break;
674 	default:
675 		SSLerror(s, ERR_R_INTERNAL_ERROR);
676 		fatal = 1;
677 		goto err;
678 	}
679 
680 	/* Now sess is non-NULL and we own one of its reference counts. */
681 
682 	if (sess->sid_ctx_length != s->sid_ctx_length ||
683 	    timingsafe_memcmp(sess->sid_ctx, s->sid_ctx,
684 	    sess->sid_ctx_length) != 0) {
685 		/*
686 		 * We have the session requested by the client, but we don't
687 		 * want to use it in this context. Treat it like a cache miss.
688 		 */
689 		goto err;
690 	}
691 
692 	if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
693 		/*
694 		 * We can't be sure if this session is being used out of
695 		 * context, which is especially important for SSL_VERIFY_PEER.
696 		 * The application should have used
697 		 * SSL[_CTX]_set_session_id_context.
698 		 *
699 		 * For this error case, we generate an error instead of treating
700 		 * the event like a cache miss (otherwise it would be easy for
701 		 * applications to effectively disable the session cache by
702 		 * accident without anyone noticing).
703 		 */
704 		SSLerror(s, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
705 		fatal = 1;
706 		goto err;
707 	}
708 
709 	if (sess->timeout < (time(NULL) - sess->time)) {
710 		s->session_ctx->stats.sess_timeout++;
711 		if (!ticket_decrypted) {
712 			/* The session was from the cache, so remove it. */
713 			SSL_CTX_remove_session(s->session_ctx, sess);
714 		}
715 		goto err;
716 	}
717 
718 	s->session_ctx->stats.sess_hit++;
719 
720 	SSL_SESSION_free(s->session);
721 	s->session = sess;
722 	s->verify_result = s->session->verify_result;
723 
724 	return 1;
725 
726  err:
727 	SSL_SESSION_free(sess);
728 	if (ticket_decrypted) {
729 		/*
730 		 * The session was from a ticket. Issue a ticket for the new
731 		 * session.
732 		 */
733 		s->tlsext_ticket_expected = 1;
734 	}
735 	if (fatal) {
736 		*alert = alert_desc;
737 		return -1;
738 	}
739 	return 0;
740 }
741 
742 int
SSL_CTX_add_session(SSL_CTX * ctx,SSL_SESSION * c)743 SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
744 {
745 	int ret = 0;
746 	SSL_SESSION *s;
747 
748 	/*
749 	 * Add just 1 reference count for the SSL_CTX's session cache
750 	 * even though it has two ways of access: each session is in a
751 	 * doubly linked list and an lhash.
752 	 */
753 	CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION);
754 
755 	/*
756 	 * If session c is in already in cache, we take back the increment
757 	 * later.
758 	 */
759 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
760 	s = lh_SSL_SESSION_insert(ctx->sessions, c);
761 
762 	/*
763 	 * s != NULL iff we already had a session with the given PID.
764 	 * In this case, s == c should hold (then we did not really modify
765 	 * ctx->sessions), or we're in trouble.
766 	 */
767 	if (s != NULL && s != c) {
768 		/* We *are* in trouble ... */
769 		SSL_SESSION_list_remove(ctx, s);
770 		SSL_SESSION_free(s);
771 		/*
772 		 * ... so pretend the other session did not exist in cache
773 		 * (we cannot handle two SSL_SESSION structures with identical
774 		 * session ID in the same cache, which could happen e.g. when
775 		 * two threads concurrently obtain the same session from an
776 		 * external cache).
777 		 */
778 		s = NULL;
779 	}
780 
781 	/* Put at the head of the queue unless it is already in the cache */
782 	if (s == NULL)
783 		SSL_SESSION_list_add(ctx, c);
784 
785 	if (s != NULL) {
786 		/*
787 		 * existing cache entry -- decrement previously incremented
788 		 * reference count because it already takes into account the
789 		 * cache.
790 		 */
791 		SSL_SESSION_free(s); /* s == c */
792 		ret = 0;
793 	} else {
794 		/*
795 		 * New cache entry -- remove old ones if cache has become
796 		 * too large.
797 		 */
798 
799 		ret = 1;
800 
801 		if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
802 			while (SSL_CTX_sess_number(ctx) >
803 			    SSL_CTX_sess_get_cache_size(ctx)) {
804 				if (!remove_session_lock(ctx,
805 				    ctx->session_cache_tail, 0))
806 					break;
807 				else
808 					ctx->stats.sess_cache_full++;
809 			}
810 		}
811 	}
812 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
813 	return (ret);
814 }
815 LSSL_ALIAS(SSL_CTX_add_session);
816 
817 int
SSL_CTX_remove_session(SSL_CTX * ctx,SSL_SESSION * c)818 SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
819 {
820 	return remove_session_lock(ctx, c, 1);
821 }
822 LSSL_ALIAS(SSL_CTX_remove_session);
823 
824 static int
remove_session_lock(SSL_CTX * ctx,SSL_SESSION * c,int lck)825 remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
826 {
827 	SSL_SESSION *r;
828 	int ret = 0;
829 
830 	if (c == NULL || c->session_id_length == 0)
831 		return 0;
832 
833 	if (lck)
834 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
835 	if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
836 		ret = 1;
837 		r = lh_SSL_SESSION_delete(ctx->sessions, c);
838 		SSL_SESSION_list_remove(ctx, c);
839 	}
840 	if (lck)
841 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
842 
843 	if (ret) {
844 		r->not_resumable = 1;
845 		if (ctx->remove_session_cb != NULL)
846 			ctx->remove_session_cb(ctx, r);
847 		SSL_SESSION_free(r);
848 	}
849 
850 	return ret;
851 }
852 
853 void
SSL_SESSION_free(SSL_SESSION * ss)854 SSL_SESSION_free(SSL_SESSION *ss)
855 {
856 	int i;
857 
858 	if (ss == NULL)
859 		return;
860 
861 	i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
862 	if (i > 0)
863 		return;
864 
865 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
866 
867 	explicit_bzero(ss->master_key, sizeof ss->master_key);
868 	explicit_bzero(ss->session_id, sizeof ss->session_id);
869 
870 	X509_free(ss->peer_cert);
871 
872 	free(ss->tlsext_hostname);
873 	free(ss->tlsext_tick);
874 	free(ss->tlsext_ecpointformatlist);
875 	free(ss->tlsext_supportedgroups);
876 
877 	tls13_secret_cleanup(&ss->resumption_master_secret);
878 
879 	freezero(ss, sizeof(*ss));
880 }
881 LSSL_ALIAS(SSL_SESSION_free);
882 
883 int
SSL_SESSION_up_ref(SSL_SESSION * ss)884 SSL_SESSION_up_ref(SSL_SESSION *ss)
885 {
886 	return CRYPTO_add(&ss->references, 1, CRYPTO_LOCK_SSL_SESSION) > 1;
887 }
888 LSSL_ALIAS(SSL_SESSION_up_ref);
889 
890 int
SSL_set_session(SSL * s,SSL_SESSION * session)891 SSL_set_session(SSL *s, SSL_SESSION *session)
892 {
893 	const SSL_METHOD *method;
894 
895 	if (session == NULL) {
896 		SSL_SESSION_free(s->session);
897 		s->session = NULL;
898 
899 		return SSL_set_ssl_method(s, s->ctx->method);
900 	}
901 
902 	if ((method = ssl_get_method(session->ssl_version)) == NULL) {
903 		SSLerror(s, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
904 		return (0);
905 	}
906 
907 	if (!SSL_set_ssl_method(s, method))
908 		return (0);
909 
910 	CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
911 	SSL_SESSION_free(s->session);
912 	s->session = session;
913 	s->verify_result = s->session->verify_result;
914 
915 	return (1);
916 }
917 LSSL_ALIAS(SSL_set_session);
918 
919 size_t
SSL_SESSION_get_master_key(const SSL_SESSION * ss,unsigned char * out,size_t max_out)920 SSL_SESSION_get_master_key(const SSL_SESSION *ss, unsigned char *out,
921     size_t max_out)
922 {
923 	size_t len = ss->master_key_length;
924 
925 	if (out == NULL)
926 		return len;
927 
928 	if (len > max_out)
929 		len = max_out;
930 
931 	memcpy(out, ss->master_key, len);
932 
933 	return len;
934 }
935 LSSL_ALIAS(SSL_SESSION_get_master_key);
936 
937 long
SSL_SESSION_set_timeout(SSL_SESSION * s,long t)938 SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
939 {
940 	if (s == NULL)
941 		return (0);
942 	s->timeout = t;
943 	return (1);
944 }
945 LSSL_ALIAS(SSL_SESSION_set_timeout);
946 
947 long
SSL_SESSION_get_timeout(const SSL_SESSION * s)948 SSL_SESSION_get_timeout(const SSL_SESSION *s)
949 {
950 	if (s == NULL)
951 		return (0);
952 	return (s->timeout);
953 }
954 LSSL_ALIAS(SSL_SESSION_get_timeout);
955 
956 /* XXX 2038 */
957 long
SSL_SESSION_get_time(const SSL_SESSION * s)958 SSL_SESSION_get_time(const SSL_SESSION *s)
959 {
960 	if (s == NULL)
961 		return (0);
962 	return (s->time);
963 }
964 LSSL_ALIAS(SSL_SESSION_get_time);
965 
966 /* XXX 2038 */
967 long
SSL_SESSION_set_time(SSL_SESSION * s,long t)968 SSL_SESSION_set_time(SSL_SESSION *s, long t)
969 {
970 	if (s == NULL)
971 		return (0);
972 	s->time = t;
973 	return (t);
974 }
975 LSSL_ALIAS(SSL_SESSION_set_time);
976 
977 int
SSL_SESSION_get_protocol_version(const SSL_SESSION * s)978 SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
979 {
980 	return s->ssl_version;
981 }
982 LSSL_ALIAS(SSL_SESSION_get_protocol_version);
983 
984 const SSL_CIPHER *
SSL_SESSION_get0_cipher(const SSL_SESSION * s)985 SSL_SESSION_get0_cipher(const SSL_SESSION *s)
986 {
987 	return ssl3_get_cipher_by_value(s->cipher_value);
988 }
989 LSSL_ALIAS(SSL_SESSION_get0_cipher);
990 
991 X509 *
SSL_SESSION_get0_peer(SSL_SESSION * s)992 SSL_SESSION_get0_peer(SSL_SESSION *s)
993 {
994 	return s->peer_cert;
995 }
996 LSSL_ALIAS(SSL_SESSION_get0_peer);
997 
998 int
SSL_SESSION_set1_id(SSL_SESSION * s,const unsigned char * sid,unsigned int sid_len)999 SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
1000     unsigned int sid_len)
1001 {
1002 	if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1003 		SSLerrorx(SSL_R_SSL_SESSION_ID_TOO_LONG);
1004 		return 0;
1005 	}
1006 	s->session_id_length = sid_len;
1007 	memmove(s->session_id, sid, sid_len);
1008 	return 1;
1009 }
1010 LSSL_ALIAS(SSL_SESSION_set1_id);
1011 
1012 int
SSL_SESSION_set1_id_context(SSL_SESSION * s,const unsigned char * sid_ctx,unsigned int sid_ctx_len)1013 SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1014     unsigned int sid_ctx_len)
1015 {
1016 	if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1017 		SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1018 		return 0;
1019 	}
1020 	s->sid_ctx_length = sid_ctx_len;
1021 	memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1022 
1023 	return 1;
1024 }
1025 LSSL_ALIAS(SSL_SESSION_set1_id_context);
1026 
1027 int
SSL_SESSION_is_resumable(const SSL_SESSION * s)1028 SSL_SESSION_is_resumable(const SSL_SESSION *s)
1029 {
1030 	return 0;
1031 }
1032 LSSL_ALIAS(SSL_SESSION_is_resumable);
1033 
1034 long
SSL_CTX_set_timeout(SSL_CTX * s,long t)1035 SSL_CTX_set_timeout(SSL_CTX *s, long t)
1036 {
1037 	long l;
1038 
1039 	if (s == NULL)
1040 		return (0);
1041 	l = s->session_timeout;
1042 	s->session_timeout = t;
1043 
1044 	return (l);
1045 }
1046 LSSL_ALIAS(SSL_CTX_set_timeout);
1047 
1048 long
SSL_CTX_get_timeout(const SSL_CTX * s)1049 SSL_CTX_get_timeout(const SSL_CTX *s)
1050 {
1051 	if (s == NULL)
1052 		return (0);
1053 	return (s->session_timeout);
1054 }
1055 LSSL_ALIAS(SSL_CTX_get_timeout);
1056 
1057 int
SSL_set_session_secret_cb(SSL * s,int (* tls_session_secret_cb)(SSL * s,void * secret,int * secret_len,STACK_OF (SSL_CIPHER)* peer_ciphers,SSL_CIPHER ** cipher,void * arg),void * arg)1058 SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s,
1059     void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers,
1060     SSL_CIPHER **cipher, void *arg), void *arg)
1061 {
1062 	if (s == NULL)
1063 		return (0);
1064 	s->tls_session_secret_cb = tls_session_secret_cb;
1065 	s->tls_session_secret_cb_arg = arg;
1066 	return (1);
1067 }
1068 LSSL_ALIAS(SSL_set_session_secret_cb);
1069 
1070 int
SSL_set_session_ticket_ext_cb(SSL * s,tls_session_ticket_ext_cb_fn cb,void * arg)1071 SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1072     void *arg)
1073 {
1074 	if (s == NULL)
1075 		return (0);
1076 	s->tls_session_ticket_ext_cb = cb;
1077 	s->tls_session_ticket_ext_cb_arg = arg;
1078 	return (1);
1079 }
1080 LSSL_ALIAS(SSL_set_session_ticket_ext_cb);
1081 
1082 int
SSL_set_session_ticket_ext(SSL * s,void * ext_data,int ext_len)1083 SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1084 {
1085 	if (s->version >= TLS1_VERSION) {
1086 		free(s->tlsext_session_ticket);
1087 		s->tlsext_session_ticket =
1088 		    malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1089 		if (!s->tlsext_session_ticket) {
1090 			SSLerror(s, ERR_R_MALLOC_FAILURE);
1091 			return 0;
1092 		}
1093 
1094 		if (ext_data) {
1095 			s->tlsext_session_ticket->length = ext_len;
1096 			s->tlsext_session_ticket->data =
1097 			    s->tlsext_session_ticket + 1;
1098 			memcpy(s->tlsext_session_ticket->data,
1099 			    ext_data, ext_len);
1100 		} else {
1101 			s->tlsext_session_ticket->length = 0;
1102 			s->tlsext_session_ticket->data = NULL;
1103 		}
1104 
1105 		return 1;
1106 	}
1107 
1108 	return 0;
1109 }
1110 LSSL_ALIAS(SSL_set_session_ticket_ext);
1111 
1112 typedef struct timeout_param_st {
1113 	SSL_CTX *ctx;
1114 	long time;
1115 	struct lhash_st_SSL_SESSION *cache;
1116 } TIMEOUT_PARAM;
1117 
1118 static void
timeout_doall_arg(SSL_SESSION * s,TIMEOUT_PARAM * p)1119 timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p)
1120 {
1121 	if ((p->time == 0) || (p->time > (s->time + s->timeout))) {
1122 		/* timeout */
1123 		/* The reason we don't call SSL_CTX_remove_session() is to
1124 		 * save on locking overhead */
1125 		(void)lh_SSL_SESSION_delete(p->cache, s);
1126 		SSL_SESSION_list_remove(p->ctx, s);
1127 		s->not_resumable = 1;
1128 		if (p->ctx->remove_session_cb != NULL)
1129 			p->ctx->remove_session_cb(p->ctx, s);
1130 		SSL_SESSION_free(s);
1131 	}
1132 }
1133 
1134 static void
timeout_LHASH_DOALL_ARG(void * arg1,void * arg2)1135 timeout_LHASH_DOALL_ARG(void *arg1, void *arg2)
1136 {
1137 	SSL_SESSION *a = arg1;
1138 	TIMEOUT_PARAM *b = arg2;
1139 
1140 	timeout_doall_arg(a, b);
1141 }
1142 
1143 /* XXX 2038 */
1144 void
SSL_CTX_flush_sessions(SSL_CTX * s,long t)1145 SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1146 {
1147 	TIMEOUT_PARAM tp;
1148 
1149 	tp.ctx = s;
1150 	tp.cache = s->sessions;
1151 	if (tp.cache == NULL)
1152 		return;
1153 	tp.time = t;
1154 
1155 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
1156 	lh_SSL_SESSION_doall_arg(tp.cache, timeout_LHASH_DOALL_ARG,
1157 	    TIMEOUT_PARAM, &tp);
1158 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
1159 }
1160 LSSL_ALIAS(SSL_CTX_flush_sessions);
1161 
1162 int
ssl_clear_bad_session(SSL * s)1163 ssl_clear_bad_session(SSL *s)
1164 {
1165 	if ((s->session != NULL) && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1166 	    !(SSL_in_init(s) || SSL_in_before(s))) {
1167 		SSL_CTX_remove_session(s->ctx, s->session);
1168 		return (1);
1169 	} else
1170 		return (0);
1171 }
1172 
1173 /* locked by SSL_CTX in the calling function */
1174 static void
SSL_SESSION_list_remove(SSL_CTX * ctx,SSL_SESSION * s)1175 SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1176 {
1177 	if (s->next == NULL || s->prev == NULL)
1178 		return;
1179 
1180 	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1181 		/* last element in list */
1182 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1183 			/* only one element in list */
1184 			ctx->session_cache_head = NULL;
1185 			ctx->session_cache_tail = NULL;
1186 		} else {
1187 			ctx->session_cache_tail = s->prev;
1188 			s->prev->next =
1189 			    (SSL_SESSION *)&(ctx->session_cache_tail);
1190 		}
1191 	} else {
1192 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1193 			/* first element in list */
1194 			ctx->session_cache_head = s->next;
1195 			s->next->prev =
1196 			    (SSL_SESSION *)&(ctx->session_cache_head);
1197 		} else {
1198 			/* middle of list */
1199 			s->next->prev = s->prev;
1200 			s->prev->next = s->next;
1201 		}
1202 	}
1203 	s->prev = s->next = NULL;
1204 }
1205 
1206 static void
SSL_SESSION_list_add(SSL_CTX * ctx,SSL_SESSION * s)1207 SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1208 {
1209 	if (s->next != NULL && s->prev != NULL)
1210 		SSL_SESSION_list_remove(ctx, s);
1211 
1212 	if (ctx->session_cache_head == NULL) {
1213 		ctx->session_cache_head = s;
1214 		ctx->session_cache_tail = s;
1215 		s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1216 		s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1217 	} else {
1218 		s->next = ctx->session_cache_head;
1219 		s->next->prev = s;
1220 		s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1221 		ctx->session_cache_head = s;
1222 	}
1223 }
1224 
1225 void
SSL_CTX_sess_set_new_cb(SSL_CTX * ctx,int (* cb)(struct ssl_st * ssl,SSL_SESSION * sess))1226 SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1227     int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
1228 	ctx->new_session_cb = cb;
1229 }
1230 LSSL_ALIAS(SSL_CTX_sess_set_new_cb);
1231 
1232 int
SSL_CTX_sess_get_new_cb(SSL_CTX * ctx)1233 (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1234 {
1235 	return ctx->new_session_cb;
1236 }
1237 LSSL_ALIAS(SSL_CTX_sess_get_new_cb);
1238 
1239 void
SSL_CTX_sess_set_remove_cb(SSL_CTX * ctx,void (* cb)(SSL_CTX * ctx,SSL_SESSION * sess))1240 SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1241     void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess))
1242 {
1243 	ctx->remove_session_cb = cb;
1244 }
1245 LSSL_ALIAS(SSL_CTX_sess_set_remove_cb);
1246 
1247 void
SSL_CTX_sess_get_remove_cb(SSL_CTX * ctx)1248 (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx, SSL_SESSION *sess)
1249 {
1250 	return ctx->remove_session_cb;
1251 }
1252 LSSL_ALIAS(SSL_CTX_sess_get_remove_cb);
1253 
1254 void
SSL_CTX_sess_set_get_cb(SSL_CTX * ctx,SSL_SESSION * (* cb)(struct ssl_st * ssl,const unsigned char * data,int len,int * copy))1255 SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, SSL_SESSION *(*cb)(struct ssl_st *ssl,
1256     const unsigned char *data, int len, int *copy))
1257 {
1258 	ctx->get_session_cb = cb;
1259 }
1260 LSSL_ALIAS(SSL_CTX_sess_set_get_cb);
1261 
1262 SSL_SESSION *
SSL_CTX_sess_get_get_cb(SSL_CTX * ctx)1263 (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, const unsigned char *data,
1264     int len, int *copy)
1265 {
1266 	return ctx->get_session_cb;
1267 }
1268 LSSL_ALIAS(SSL_CTX_sess_get_get_cb);
1269 
1270 void
SSL_CTX_set_info_callback(SSL_CTX * ctx,void (* cb)(const SSL * ssl,int type,int val))1271 SSL_CTX_set_info_callback(SSL_CTX *ctx,
1272     void (*cb)(const SSL *ssl, int type, int val))
1273 {
1274 	ctx->info_callback = cb;
1275 }
1276 LSSL_ALIAS(SSL_CTX_set_info_callback);
1277 
1278 void
SSL_CTX_get_info_callback(SSL_CTX * ctx)1279 (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, int val)
1280 {
1281 	return ctx->info_callback;
1282 }
1283 LSSL_ALIAS(SSL_CTX_get_info_callback);
1284 
1285 void
SSL_CTX_set_client_cert_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,X509 ** x509,EVP_PKEY ** pkey))1286 SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1287     int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1288 {
1289 	ctx->client_cert_cb = cb;
1290 }
1291 LSSL_ALIAS(SSL_CTX_set_client_cert_cb);
1292 
1293 int
SSL_CTX_get_client_cert_cb(SSL_CTX * ctx)1294 (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509,
1295     EVP_PKEY **pkey)
1296 {
1297 	return ctx->client_cert_cb;
1298 }
1299 LSSL_ALIAS(SSL_CTX_get_client_cert_cb);
1300 
1301 void
SSL_CTX_set_cookie_generate_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,unsigned char * cookie,unsigned int * cookie_len))1302 SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1303     int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1304 {
1305 	ctx->app_gen_cookie_cb = cb;
1306 }
1307 LSSL_ALIAS(SSL_CTX_set_cookie_generate_cb);
1308 
1309 void
SSL_CTX_set_cookie_verify_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,const unsigned char * cookie,unsigned int cookie_len))1310 SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1311     int (*cb)(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len))
1312 {
1313 	ctx->app_verify_cookie_cb = cb;
1314 }
1315 LSSL_ALIAS(SSL_CTX_set_cookie_verify_cb);
1316 
1317 int
PEM_write_SSL_SESSION(FILE * fp,SSL_SESSION * x)1318 PEM_write_SSL_SESSION(FILE *fp, SSL_SESSION *x)
1319 {
1320 	return PEM_ASN1_write((i2d_of_void *)i2d_SSL_SESSION,
1321 	    PEM_STRING_SSL_SESSION, fp, x, NULL, NULL, 0, NULL, NULL);
1322 }
1323 LSSL_ALIAS(PEM_write_SSL_SESSION);
1324 
1325 SSL_SESSION *
PEM_read_SSL_SESSION(FILE * fp,SSL_SESSION ** x,pem_password_cb * cb,void * u)1326 PEM_read_SSL_SESSION(FILE *fp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1327 {
1328 	return PEM_ASN1_read((d2i_of_void *)d2i_SSL_SESSION,
1329 	    PEM_STRING_SSL_SESSION, fp, (void **)x, cb, u);
1330 }
1331 LSSL_ALIAS(PEM_read_SSL_SESSION);
1332 
1333 SSL_SESSION *
PEM_read_bio_SSL_SESSION(BIO * bp,SSL_SESSION ** x,pem_password_cb * cb,void * u)1334 PEM_read_bio_SSL_SESSION(BIO *bp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1335 {
1336 	return PEM_ASN1_read_bio((d2i_of_void *)d2i_SSL_SESSION,
1337 	    PEM_STRING_SSL_SESSION, bp, (void **)x, cb, u);
1338 }
1339 LSSL_ALIAS(PEM_read_bio_SSL_SESSION);
1340 
1341 int
PEM_write_bio_SSL_SESSION(BIO * bp,SSL_SESSION * x)1342 PEM_write_bio_SSL_SESSION(BIO *bp, SSL_SESSION *x)
1343 {
1344 	return PEM_ASN1_write_bio((i2d_of_void *)i2d_SSL_SESSION,
1345 	    PEM_STRING_SSL_SESSION, bp, x, NULL, NULL, 0, NULL, NULL);
1346 }
1347 LSSL_ALIAS(PEM_write_bio_SSL_SESSION);
1348