xref: /openbsd/lib/libssl/ssl_sess.c (revision c8993338)
1 /* $OpenBSD: ssl_sess.c,v 1.125 2024/03/27 06:47:52 tb 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 = sess->cipher;
291 	copy->cipher_id = sess->cipher_id;
292 
293 	if (sess->ciphers != NULL) {
294 		if ((copy->ciphers = sk_SSL_CIPHER_dup(sess->ciphers)) == NULL)
295 			goto err;
296 	}
297 
298 	if (sess->tlsext_hostname != NULL) {
299 		copy->tlsext_hostname = strdup(sess->tlsext_hostname);
300 		if (copy->tlsext_hostname == NULL)
301 			goto err;
302 	}
303 
304 	if (include_ticket) {
305 		CBS_init(&cbs, sess->tlsext_tick, sess->tlsext_ticklen);
306 		if (!CBS_stow(&cbs, &copy->tlsext_tick, &copy->tlsext_ticklen))
307 			goto err;
308 		copy->tlsext_tick_lifetime_hint =
309 		    sess->tlsext_tick_lifetime_hint;
310 
311 		/*
312 		 * XXX - copy sess->resumption_master_secret and all other
313 		 * TLSv1.3 info here.
314 		 */
315 	}
316 
317 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, copy,
318 	    &copy->ex_data))
319 		goto err;
320 
321 	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, &copy->ex_data,
322 	    &sess->ex_data))
323 		goto err;
324 
325 	/* Omit prev/next: the new session gets its own slot in the cache. */
326 
327 	copy->not_resumable = sess->not_resumable;
328 
329 	CBS_init(&cbs, sess->tlsext_ecpointformatlist,
330 	    sess->tlsext_ecpointformatlist_length);
331 	if (!CBS_stow(&cbs, &copy->tlsext_ecpointformatlist,
332 	    &copy->tlsext_ecpointformatlist_length))
333 		goto err;
334 
335 	if (sess->tlsext_supportedgroups != NULL) {
336 		if ((copy->tlsext_supportedgroups = calloc(sizeof(uint16_t),
337 		    sess->tlsext_supportedgroups_length)) == NULL)
338 			goto err;
339 		memcpy(copy->tlsext_supportedgroups,
340 		    sess->tlsext_supportedgroups,
341 		    sizeof(uint16_t) * sess->tlsext_supportedgroups_length);
342 		copy->tlsext_supportedgroups_length =
343 		    sess->tlsext_supportedgroups_length;
344 	}
345 
346 	return copy;
347 
348  err:
349 	SSL_SESSION_free(copy);
350 
351 	return NULL;
352 }
353 
354 const unsigned char *
SSL_SESSION_get_id(const SSL_SESSION * ss,unsigned int * len)355 SSL_SESSION_get_id(const SSL_SESSION *ss, unsigned int *len)
356 {
357 	if (len != NULL)
358 		*len = (unsigned int)ss->session_id_length;
359 	return ss->session_id;
360 }
361 LSSL_ALIAS(SSL_SESSION_get_id);
362 
363 const unsigned char *
SSL_SESSION_get0_id_context(const SSL_SESSION * ss,unsigned int * len)364 SSL_SESSION_get0_id_context(const SSL_SESSION *ss, unsigned int *len)
365 {
366 	if (len != NULL)
367 		*len = (unsigned int)ss->sid_ctx_length;
368 	return ss->sid_ctx;
369 }
370 LSSL_ALIAS(SSL_SESSION_get0_id_context);
371 
372 unsigned int
SSL_SESSION_get_compress_id(const SSL_SESSION * ss)373 SSL_SESSION_get_compress_id(const SSL_SESSION *ss)
374 {
375 	return 0;
376 }
377 LSSL_ALIAS(SSL_SESSION_get_compress_id);
378 
379 unsigned long
SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION * s)380 SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
381 {
382 	return s->tlsext_tick_lifetime_hint;
383 }
384 LSSL_ALIAS(SSL_SESSION_get_ticket_lifetime_hint);
385 
386 int
SSL_SESSION_has_ticket(const SSL_SESSION * s)387 SSL_SESSION_has_ticket(const SSL_SESSION *s)
388 {
389 	return (s->tlsext_ticklen > 0) ? 1 : 0;
390 }
391 LSSL_ALIAS(SSL_SESSION_has_ticket);
392 
393 /*
394  * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
395  * the ID with random gunk repeatedly until we have no conflict is going to
396  * complete in one iteration pretty much "most" of the time (btw:
397  * understatement). So, if it takes us 10 iterations and we still can't avoid
398  * a conflict - well that's a reasonable point to call it quits. Either the
399  * arc4random code is broken or someone is trying to open roughly very close to
400  * 2^128 (or 2^256) SSL sessions to our server. How you might store that many
401  * sessions is perhaps a more interesting question...
402  */
403 
404 #define MAX_SESS_ID_ATTEMPTS 10
405 
406 static int
def_generate_session_id(const SSL * ssl,unsigned char * id,unsigned int * id_len)407 def_generate_session_id(const SSL *ssl, unsigned char *id, unsigned int *id_len)
408 {
409 	unsigned int retry = 0;
410 
411 	do {
412 		arc4random_buf(id, *id_len);
413 	} while (SSL_has_matching_session_id(ssl, id, *id_len) &&
414 	    (++retry < MAX_SESS_ID_ATTEMPTS));
415 
416 	if (retry < MAX_SESS_ID_ATTEMPTS)
417 		return 1;
418 
419 	/* else - woops a session_id match */
420 	/* XXX We should also check the external cache --
421 	 * but the probability of a collision is negligible, and
422 	 * we could not prevent the concurrent creation of sessions
423 	 * with identical IDs since we currently don't have means
424 	 * to atomically check whether a session ID already exists
425 	 * and make a reservation for it if it does not
426 	 * (this problem applies to the internal cache as well).
427 	 */
428 	return 0;
429 }
430 
431 int
ssl_get_new_session(SSL * s,int session)432 ssl_get_new_session(SSL *s, int session)
433 {
434 	unsigned int tmp;
435 	SSL_SESSION *ss = NULL;
436 	GEN_SESSION_CB cb = def_generate_session_id;
437 
438 	/* This gets used by clients and servers. */
439 
440 	if ((ss = SSL_SESSION_new()) == NULL)
441 		return (0);
442 
443 	/* If the context has a default timeout, use it */
444 	if (s->session_ctx->session_timeout == 0)
445 		ss->timeout = SSL_get_default_timeout(s);
446 	else
447 		ss->timeout = s->session_ctx->session_timeout;
448 
449 	if (s->session != NULL) {
450 		SSL_SESSION_free(s->session);
451 		s->session = NULL;
452 	}
453 
454 	if (session) {
455 		switch (s->version) {
456 		case TLS1_VERSION:
457 		case TLS1_1_VERSION:
458 		case TLS1_2_VERSION:
459 		case DTLS1_VERSION:
460 		case DTLS1_2_VERSION:
461 			ss->ssl_version = s->version;
462 			ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
463 			break;
464 		default:
465 			SSLerror(s, SSL_R_UNSUPPORTED_SSL_VERSION);
466 			SSL_SESSION_free(ss);
467 			return (0);
468 		}
469 
470 		/* If RFC4507 ticket use empty session ID. */
471 		if (s->tlsext_ticket_expected) {
472 			ss->session_id_length = 0;
473 			goto sess_id_done;
474 		}
475 
476 		/* Choose which callback will set the session ID. */
477 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
478 		if (s->generate_session_id)
479 			cb = s->generate_session_id;
480 		else if (s->session_ctx->generate_session_id)
481 			cb = s->session_ctx->generate_session_id;
482 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
483 
484 		/* Choose a session ID. */
485 		tmp = ss->session_id_length;
486 		if (!cb(s, ss->session_id, &tmp)) {
487 			/* The callback failed */
488 			SSLerror(s, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
489 			SSL_SESSION_free(ss);
490 			return (0);
491 		}
492 
493 		/*
494 		 * Don't allow the callback to set the session length to zero.
495 		 * nor set it higher than it was.
496 		 */
497 		if (tmp == 0 || tmp > ss->session_id_length) {
498 			/* The callback set an illegal length */
499 			SSLerror(s, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
500 			SSL_SESSION_free(ss);
501 			return (0);
502 		}
503 		ss->session_id_length = tmp;
504 
505 		/* Finally, check for a conflict. */
506 		if (SSL_has_matching_session_id(s, ss->session_id,
507 		    ss->session_id_length)) {
508 			SSLerror(s, SSL_R_SSL_SESSION_ID_CONFLICT);
509 			SSL_SESSION_free(ss);
510 			return (0);
511 		}
512 
513  sess_id_done:
514 		if (s->tlsext_hostname) {
515 			ss->tlsext_hostname = strdup(s->tlsext_hostname);
516 			if (ss->tlsext_hostname == NULL) {
517 				SSLerror(s, ERR_R_INTERNAL_ERROR);
518 				SSL_SESSION_free(ss);
519 				return 0;
520 			}
521 		}
522 	} else {
523 		ss->session_id_length = 0;
524 	}
525 
526 	if (s->sid_ctx_length > sizeof ss->sid_ctx) {
527 		SSLerror(s, ERR_R_INTERNAL_ERROR);
528 		SSL_SESSION_free(ss);
529 		return 0;
530 	}
531 
532 	memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
533 	ss->sid_ctx_length = s->sid_ctx_length;
534 	s->session = ss;
535 	ss->ssl_version = s->version;
536 	ss->verify_result = X509_V_OK;
537 
538 	return (1);
539 }
540 
541 static SSL_SESSION *
ssl_session_from_cache(SSL * s,CBS * session_id)542 ssl_session_from_cache(SSL *s, CBS *session_id)
543 {
544 	SSL_SESSION *sess;
545 	SSL_SESSION data;
546 
547 	if ((s->session_ctx->session_cache_mode &
548 	     SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
549 		return NULL;
550 
551 	memset(&data, 0, sizeof(data));
552 
553 	data.ssl_version = s->version;
554 
555 	if (!CBS_write_bytes(session_id, data.session_id,
556 	    sizeof(data.session_id), &data.session_id_length))
557 		return NULL;
558 
559 	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
560 	sess = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
561 	if (sess != NULL)
562 		CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
563 	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
564 
565 	if (sess == NULL)
566 		s->session_ctx->stats.sess_miss++;
567 
568 	return sess;
569 }
570 
571 static SSL_SESSION *
ssl_session_from_callback(SSL * s,CBS * session_id)572 ssl_session_from_callback(SSL *s, CBS *session_id)
573 {
574 	SSL_SESSION *sess;
575 	int copy;
576 
577 	if (s->session_ctx->get_session_cb == NULL)
578 		return NULL;
579 
580 	copy = 1;
581 	if ((sess = s->session_ctx->get_session_cb(s,
582 	    CBS_data(session_id), CBS_len(session_id), &copy)) == NULL)
583 		return NULL;
584 	/*
585 	 * The copy handler may have set copy == 0 to indicate that the session
586 	 * structures are shared between threads and that it handles the
587 	 * reference count itself. If it didn't set copy to zero, we must
588 	 * increment the reference count.
589 	 */
590 	if (copy)
591 		CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
592 
593 	s->session_ctx->stats.sess_cb_hit++;
594 
595 	/* Add the externally cached session to the internal cache as well. */
596 	if (!(s->session_ctx->session_cache_mode &
597 	    SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
598 		/*
599 		 * The following should not return 1,
600 		 * otherwise, things are very strange.
601 		 */
602 		SSL_CTX_add_session(s->session_ctx, sess);
603 	}
604 
605 	return sess;
606 }
607 
608 static SSL_SESSION *
ssl_session_by_id(SSL * s,CBS * session_id)609 ssl_session_by_id(SSL *s, CBS *session_id)
610 {
611 	SSL_SESSION *sess;
612 
613 	if (CBS_len(session_id) == 0)
614 		return NULL;
615 
616 	if ((sess = ssl_session_from_cache(s, session_id)) == NULL)
617 		sess = ssl_session_from_callback(s, session_id);
618 
619 	return sess;
620 }
621 
622 /*
623  * ssl_get_prev_session attempts to find an SSL_SESSION to be used to resume
624  * this connection. It is only called by servers.
625  *
626  *   session_id: points at the session ID in the ClientHello. This code will
627  *       read past the end of this in order to parse out the session ticket
628  *       extension, if any.
629  *   ext_block: a CBS for the ClientHello extensions block.
630  *   alert: alert that the caller should send in case of failure.
631  *
632  * Returns:
633  *   -1: error
634  *    0: a session may have been found.
635  *
636  * Side effects:
637  *   - If a session is found then s->session is pointed at it (after freeing
638  *     an existing session if need be) and s->verify_result is set from the
639  *     session.
640  *   - For both new and resumed sessions, s->tlsext_ticket_expected
641  *     indicates whether the server should issue a new session ticket or not.
642  */
643 int
ssl_get_prev_session(SSL * s,CBS * session_id,CBS * ext_block,int * alert)644 ssl_get_prev_session(SSL *s, CBS *session_id, CBS *ext_block, int *alert)
645 {
646 	SSL_SESSION *sess = NULL;
647 	int alert_desc = SSL_AD_INTERNAL_ERROR, fatal = 0;
648 	int ticket_decrypted = 0;
649 
650 	/* This is used only by servers. */
651 
652 	if (CBS_len(session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH)
653 		goto err;
654 
655 	/* Sets s->tlsext_ticket_expected. */
656 	switch (tls1_process_ticket(s, ext_block, &alert_desc, &sess)) {
657 	case TLS1_TICKET_FATAL_ERROR:
658 		fatal = 1;
659 		goto err;
660 	case TLS1_TICKET_NONE:
661 	case TLS1_TICKET_EMPTY:
662 		if ((sess = ssl_session_by_id(s, session_id)) == NULL)
663 			goto err;
664 		break;
665 	case TLS1_TICKET_NOT_DECRYPTED:
666 		goto err;
667 	case TLS1_TICKET_DECRYPTED:
668 		ticket_decrypted = 1;
669 
670 		/*
671 		 * The session ID is used by some clients to detect that the
672 		 * ticket has been accepted so we copy it into sess.
673 		 */
674 		if (!CBS_write_bytes(session_id, sess->session_id,
675 		    sizeof(sess->session_id), &sess->session_id_length)) {
676 			fatal = 1;
677 			goto err;
678 		}
679 		break;
680 	default:
681 		SSLerror(s, ERR_R_INTERNAL_ERROR);
682 		fatal = 1;
683 		goto err;
684 	}
685 
686 	/* Now sess is non-NULL and we own one of its reference counts. */
687 
688 	if (sess->sid_ctx_length != s->sid_ctx_length ||
689 	    timingsafe_memcmp(sess->sid_ctx, s->sid_ctx,
690 	    sess->sid_ctx_length) != 0) {
691 		/*
692 		 * We have the session requested by the client, but we don't
693 		 * want to use it in this context. Treat it like a cache miss.
694 		 */
695 		goto err;
696 	}
697 
698 	if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
699 		/*
700 		 * We can't be sure if this session is being used out of
701 		 * context, which is especially important for SSL_VERIFY_PEER.
702 		 * The application should have used
703 		 * SSL[_CTX]_set_session_id_context.
704 		 *
705 		 * For this error case, we generate an error instead of treating
706 		 * the event like a cache miss (otherwise it would be easy for
707 		 * applications to effectively disable the session cache by
708 		 * accident without anyone noticing).
709 		 */
710 		SSLerror(s, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
711 		fatal = 1;
712 		goto err;
713 	}
714 
715 	if (sess->cipher == NULL) {
716 		sess->cipher = ssl3_get_cipher_by_id(sess->cipher_id);
717 		if (sess->cipher == NULL)
718 			goto err;
719 	}
720 
721 	if (sess->timeout < (time(NULL) - sess->time)) {
722 		s->session_ctx->stats.sess_timeout++;
723 		if (!ticket_decrypted) {
724 			/* The session was from the cache, so remove it. */
725 			SSL_CTX_remove_session(s->session_ctx, sess);
726 		}
727 		goto err;
728 	}
729 
730 	s->session_ctx->stats.sess_hit++;
731 
732 	SSL_SESSION_free(s->session);
733 	s->session = sess;
734 	s->verify_result = s->session->verify_result;
735 
736 	return 1;
737 
738  err:
739 	SSL_SESSION_free(sess);
740 	if (ticket_decrypted) {
741 		/*
742 		 * The session was from a ticket. Issue a ticket for the new
743 		 * session.
744 		 */
745 		s->tlsext_ticket_expected = 1;
746 	}
747 	if (fatal) {
748 		*alert = alert_desc;
749 		return -1;
750 	}
751 	return 0;
752 }
753 
754 int
SSL_CTX_add_session(SSL_CTX * ctx,SSL_SESSION * c)755 SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
756 {
757 	int ret = 0;
758 	SSL_SESSION *s;
759 
760 	/*
761 	 * Add just 1 reference count for the SSL_CTX's session cache
762 	 * even though it has two ways of access: each session is in a
763 	 * doubly linked list and an lhash.
764 	 */
765 	CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION);
766 
767 	/*
768 	 * If session c is in already in cache, we take back the increment
769 	 * later.
770 	 */
771 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
772 	s = lh_SSL_SESSION_insert(ctx->sessions, c);
773 
774 	/*
775 	 * s != NULL iff we already had a session with the given PID.
776 	 * In this case, s == c should hold (then we did not really modify
777 	 * ctx->sessions), or we're in trouble.
778 	 */
779 	if (s != NULL && s != c) {
780 		/* We *are* in trouble ... */
781 		SSL_SESSION_list_remove(ctx, s);
782 		SSL_SESSION_free(s);
783 		/*
784 		 * ... so pretend the other session did not exist in cache
785 		 * (we cannot handle two SSL_SESSION structures with identical
786 		 * session ID in the same cache, which could happen e.g. when
787 		 * two threads concurrently obtain the same session from an
788 		 * external cache).
789 		 */
790 		s = NULL;
791 	}
792 
793 	/* Put at the head of the queue unless it is already in the cache */
794 	if (s == NULL)
795 		SSL_SESSION_list_add(ctx, c);
796 
797 	if (s != NULL) {
798 		/*
799 		 * existing cache entry -- decrement previously incremented
800 		 * reference count because it already takes into account the
801 		 * cache.
802 		 */
803 		SSL_SESSION_free(s); /* s == c */
804 		ret = 0;
805 	} else {
806 		/*
807 		 * New cache entry -- remove old ones if cache has become
808 		 * too large.
809 		 */
810 
811 		ret = 1;
812 
813 		if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
814 			while (SSL_CTX_sess_number(ctx) >
815 			    SSL_CTX_sess_get_cache_size(ctx)) {
816 				if (!remove_session_lock(ctx,
817 				    ctx->session_cache_tail, 0))
818 					break;
819 				else
820 					ctx->stats.sess_cache_full++;
821 			}
822 		}
823 	}
824 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
825 	return (ret);
826 }
827 LSSL_ALIAS(SSL_CTX_add_session);
828 
829 int
SSL_CTX_remove_session(SSL_CTX * ctx,SSL_SESSION * c)830 SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
831 {
832 	return remove_session_lock(ctx, c, 1);
833 }
834 LSSL_ALIAS(SSL_CTX_remove_session);
835 
836 static int
remove_session_lock(SSL_CTX * ctx,SSL_SESSION * c,int lck)837 remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
838 {
839 	SSL_SESSION *r;
840 	int ret = 0;
841 
842 	if (c == NULL || c->session_id_length == 0)
843 		return 0;
844 
845 	if (lck)
846 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
847 	if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
848 		ret = 1;
849 		r = lh_SSL_SESSION_delete(ctx->sessions, c);
850 		SSL_SESSION_list_remove(ctx, c);
851 	}
852 	if (lck)
853 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
854 
855 	if (ret) {
856 		r->not_resumable = 1;
857 		if (ctx->remove_session_cb != NULL)
858 			ctx->remove_session_cb(ctx, r);
859 		SSL_SESSION_free(r);
860 	}
861 
862 	return ret;
863 }
864 
865 void
SSL_SESSION_free(SSL_SESSION * ss)866 SSL_SESSION_free(SSL_SESSION *ss)
867 {
868 	int i;
869 
870 	if (ss == NULL)
871 		return;
872 
873 	i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
874 	if (i > 0)
875 		return;
876 
877 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
878 
879 	explicit_bzero(ss->master_key, sizeof ss->master_key);
880 	explicit_bzero(ss->session_id, sizeof ss->session_id);
881 
882 	X509_free(ss->peer_cert);
883 
884 	sk_SSL_CIPHER_free(ss->ciphers);
885 
886 	free(ss->tlsext_hostname);
887 	free(ss->tlsext_tick);
888 	free(ss->tlsext_ecpointformatlist);
889 	free(ss->tlsext_supportedgroups);
890 
891 	tls13_secret_cleanup(&ss->resumption_master_secret);
892 
893 	freezero(ss, sizeof(*ss));
894 }
895 LSSL_ALIAS(SSL_SESSION_free);
896 
897 int
SSL_SESSION_up_ref(SSL_SESSION * ss)898 SSL_SESSION_up_ref(SSL_SESSION *ss)
899 {
900 	return CRYPTO_add(&ss->references, 1, CRYPTO_LOCK_SSL_SESSION) > 1;
901 }
902 LSSL_ALIAS(SSL_SESSION_up_ref);
903 
904 int
SSL_set_session(SSL * s,SSL_SESSION * session)905 SSL_set_session(SSL *s, SSL_SESSION *session)
906 {
907 	const SSL_METHOD *method;
908 
909 	if (session == NULL) {
910 		SSL_SESSION_free(s->session);
911 		s->session = NULL;
912 
913 		return SSL_set_ssl_method(s, s->ctx->method);
914 	}
915 
916 	if ((method = ssl_get_method(session->ssl_version)) == NULL) {
917 		SSLerror(s, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
918 		return (0);
919 	}
920 
921 	if (!SSL_set_ssl_method(s, method))
922 		return (0);
923 
924 	CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
925 	SSL_SESSION_free(s->session);
926 	s->session = session;
927 	s->verify_result = s->session->verify_result;
928 
929 	return (1);
930 }
931 LSSL_ALIAS(SSL_set_session);
932 
933 size_t
SSL_SESSION_get_master_key(const SSL_SESSION * ss,unsigned char * out,size_t max_out)934 SSL_SESSION_get_master_key(const SSL_SESSION *ss, unsigned char *out,
935     size_t max_out)
936 {
937 	size_t len = ss->master_key_length;
938 
939 	if (out == NULL)
940 		return len;
941 
942 	if (len > max_out)
943 		len = max_out;
944 
945 	memcpy(out, ss->master_key, len);
946 
947 	return len;
948 }
949 LSSL_ALIAS(SSL_SESSION_get_master_key);
950 
951 long
SSL_SESSION_set_timeout(SSL_SESSION * s,long t)952 SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
953 {
954 	if (s == NULL)
955 		return (0);
956 	s->timeout = t;
957 	return (1);
958 }
959 LSSL_ALIAS(SSL_SESSION_set_timeout);
960 
961 long
SSL_SESSION_get_timeout(const SSL_SESSION * s)962 SSL_SESSION_get_timeout(const SSL_SESSION *s)
963 {
964 	if (s == NULL)
965 		return (0);
966 	return (s->timeout);
967 }
968 LSSL_ALIAS(SSL_SESSION_get_timeout);
969 
970 /* XXX 2038 */
971 long
SSL_SESSION_get_time(const SSL_SESSION * s)972 SSL_SESSION_get_time(const SSL_SESSION *s)
973 {
974 	if (s == NULL)
975 		return (0);
976 	return (s->time);
977 }
978 LSSL_ALIAS(SSL_SESSION_get_time);
979 
980 /* XXX 2038 */
981 long
SSL_SESSION_set_time(SSL_SESSION * s,long t)982 SSL_SESSION_set_time(SSL_SESSION *s, long t)
983 {
984 	if (s == NULL)
985 		return (0);
986 	s->time = t;
987 	return (t);
988 }
989 LSSL_ALIAS(SSL_SESSION_set_time);
990 
991 int
SSL_SESSION_get_protocol_version(const SSL_SESSION * s)992 SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
993 {
994 	return s->ssl_version;
995 }
996 LSSL_ALIAS(SSL_SESSION_get_protocol_version);
997 
998 const SSL_CIPHER *
SSL_SESSION_get0_cipher(const SSL_SESSION * s)999 SSL_SESSION_get0_cipher(const SSL_SESSION *s)
1000 {
1001 	return s->cipher;
1002 }
1003 LSSL_ALIAS(SSL_SESSION_get0_cipher);
1004 
1005 X509 *
SSL_SESSION_get0_peer(SSL_SESSION * s)1006 SSL_SESSION_get0_peer(SSL_SESSION *s)
1007 {
1008 	return s->peer_cert;
1009 }
1010 LSSL_ALIAS(SSL_SESSION_get0_peer);
1011 
1012 int
SSL_SESSION_set1_id(SSL_SESSION * s,const unsigned char * sid,unsigned int sid_len)1013 SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
1014     unsigned int sid_len)
1015 {
1016 	if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1017 		SSLerrorx(SSL_R_SSL_SESSION_ID_TOO_LONG);
1018 		return 0;
1019 	}
1020 	s->session_id_length = sid_len;
1021 	memmove(s->session_id, sid, sid_len);
1022 	return 1;
1023 }
1024 LSSL_ALIAS(SSL_SESSION_set1_id);
1025 
1026 int
SSL_SESSION_set1_id_context(SSL_SESSION * s,const unsigned char * sid_ctx,unsigned int sid_ctx_len)1027 SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1028     unsigned int sid_ctx_len)
1029 {
1030 	if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1031 		SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1032 		return 0;
1033 	}
1034 	s->sid_ctx_length = sid_ctx_len;
1035 	memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1036 
1037 	return 1;
1038 }
1039 LSSL_ALIAS(SSL_SESSION_set1_id_context);
1040 
1041 int
SSL_SESSION_is_resumable(const SSL_SESSION * s)1042 SSL_SESSION_is_resumable(const SSL_SESSION *s)
1043 {
1044 	return 0;
1045 }
1046 LSSL_ALIAS(SSL_SESSION_is_resumable);
1047 
1048 long
SSL_CTX_set_timeout(SSL_CTX * s,long t)1049 SSL_CTX_set_timeout(SSL_CTX *s, long t)
1050 {
1051 	long l;
1052 
1053 	if (s == NULL)
1054 		return (0);
1055 	l = s->session_timeout;
1056 	s->session_timeout = t;
1057 
1058 	return (l);
1059 }
1060 LSSL_ALIAS(SSL_CTX_set_timeout);
1061 
1062 long
SSL_CTX_get_timeout(const SSL_CTX * s)1063 SSL_CTX_get_timeout(const SSL_CTX *s)
1064 {
1065 	if (s == NULL)
1066 		return (0);
1067 	return (s->session_timeout);
1068 }
1069 LSSL_ALIAS(SSL_CTX_get_timeout);
1070 
1071 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)1072 SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s,
1073     void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers,
1074     SSL_CIPHER **cipher, void *arg), void *arg)
1075 {
1076 	if (s == NULL)
1077 		return (0);
1078 	s->tls_session_secret_cb = tls_session_secret_cb;
1079 	s->tls_session_secret_cb_arg = arg;
1080 	return (1);
1081 }
1082 LSSL_ALIAS(SSL_set_session_secret_cb);
1083 
1084 int
SSL_set_session_ticket_ext_cb(SSL * s,tls_session_ticket_ext_cb_fn cb,void * arg)1085 SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1086     void *arg)
1087 {
1088 	if (s == NULL)
1089 		return (0);
1090 	s->tls_session_ticket_ext_cb = cb;
1091 	s->tls_session_ticket_ext_cb_arg = arg;
1092 	return (1);
1093 }
1094 LSSL_ALIAS(SSL_set_session_ticket_ext_cb);
1095 
1096 int
SSL_set_session_ticket_ext(SSL * s,void * ext_data,int ext_len)1097 SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1098 {
1099 	if (s->version >= TLS1_VERSION) {
1100 		free(s->tlsext_session_ticket);
1101 		s->tlsext_session_ticket =
1102 		    malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1103 		if (!s->tlsext_session_ticket) {
1104 			SSLerror(s, ERR_R_MALLOC_FAILURE);
1105 			return 0;
1106 		}
1107 
1108 		if (ext_data) {
1109 			s->tlsext_session_ticket->length = ext_len;
1110 			s->tlsext_session_ticket->data =
1111 			    s->tlsext_session_ticket + 1;
1112 			memcpy(s->tlsext_session_ticket->data,
1113 			    ext_data, ext_len);
1114 		} else {
1115 			s->tlsext_session_ticket->length = 0;
1116 			s->tlsext_session_ticket->data = NULL;
1117 		}
1118 
1119 		return 1;
1120 	}
1121 
1122 	return 0;
1123 }
1124 LSSL_ALIAS(SSL_set_session_ticket_ext);
1125 
1126 typedef struct timeout_param_st {
1127 	SSL_CTX *ctx;
1128 	long time;
1129 	struct lhash_st_SSL_SESSION *cache;
1130 } TIMEOUT_PARAM;
1131 
1132 static void
timeout_doall_arg(SSL_SESSION * s,TIMEOUT_PARAM * p)1133 timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p)
1134 {
1135 	if ((p->time == 0) || (p->time > (s->time + s->timeout))) {
1136 		/* timeout */
1137 		/* The reason we don't call SSL_CTX_remove_session() is to
1138 		 * save on locking overhead */
1139 		(void)lh_SSL_SESSION_delete(p->cache, s);
1140 		SSL_SESSION_list_remove(p->ctx, s);
1141 		s->not_resumable = 1;
1142 		if (p->ctx->remove_session_cb != NULL)
1143 			p->ctx->remove_session_cb(p->ctx, s);
1144 		SSL_SESSION_free(s);
1145 	}
1146 }
1147 
1148 static void
timeout_LHASH_DOALL_ARG(void * arg1,void * arg2)1149 timeout_LHASH_DOALL_ARG(void *arg1, void *arg2)
1150 {
1151 	SSL_SESSION *a = arg1;
1152 	TIMEOUT_PARAM *b = arg2;
1153 
1154 	timeout_doall_arg(a, b);
1155 }
1156 
1157 /* XXX 2038 */
1158 void
SSL_CTX_flush_sessions(SSL_CTX * s,long t)1159 SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1160 {
1161 	TIMEOUT_PARAM tp;
1162 
1163 	tp.ctx = s;
1164 	tp.cache = s->sessions;
1165 	if (tp.cache == NULL)
1166 		return;
1167 	tp.time = t;
1168 
1169 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
1170 	lh_SSL_SESSION_doall_arg(tp.cache, timeout_LHASH_DOALL_ARG,
1171 	    TIMEOUT_PARAM, &tp);
1172 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
1173 }
1174 LSSL_ALIAS(SSL_CTX_flush_sessions);
1175 
1176 int
ssl_clear_bad_session(SSL * s)1177 ssl_clear_bad_session(SSL *s)
1178 {
1179 	if ((s->session != NULL) && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1180 	    !(SSL_in_init(s) || SSL_in_before(s))) {
1181 		SSL_CTX_remove_session(s->ctx, s->session);
1182 		return (1);
1183 	} else
1184 		return (0);
1185 }
1186 
1187 /* locked by SSL_CTX in the calling function */
1188 static void
SSL_SESSION_list_remove(SSL_CTX * ctx,SSL_SESSION * s)1189 SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1190 {
1191 	if (s->next == NULL || s->prev == NULL)
1192 		return;
1193 
1194 	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1195 		/* last element in list */
1196 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1197 			/* only one element in list */
1198 			ctx->session_cache_head = NULL;
1199 			ctx->session_cache_tail = NULL;
1200 		} else {
1201 			ctx->session_cache_tail = s->prev;
1202 			s->prev->next =
1203 			    (SSL_SESSION *)&(ctx->session_cache_tail);
1204 		}
1205 	} else {
1206 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1207 			/* first element in list */
1208 			ctx->session_cache_head = s->next;
1209 			s->next->prev =
1210 			    (SSL_SESSION *)&(ctx->session_cache_head);
1211 		} else {
1212 			/* middle of list */
1213 			s->next->prev = s->prev;
1214 			s->prev->next = s->next;
1215 		}
1216 	}
1217 	s->prev = s->next = NULL;
1218 }
1219 
1220 static void
SSL_SESSION_list_add(SSL_CTX * ctx,SSL_SESSION * s)1221 SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1222 {
1223 	if (s->next != NULL && s->prev != NULL)
1224 		SSL_SESSION_list_remove(ctx, s);
1225 
1226 	if (ctx->session_cache_head == NULL) {
1227 		ctx->session_cache_head = s;
1228 		ctx->session_cache_tail = s;
1229 		s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1230 		s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1231 	} else {
1232 		s->next = ctx->session_cache_head;
1233 		s->next->prev = s;
1234 		s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1235 		ctx->session_cache_head = s;
1236 	}
1237 }
1238 
1239 void
SSL_CTX_sess_set_new_cb(SSL_CTX * ctx,int (* cb)(struct ssl_st * ssl,SSL_SESSION * sess))1240 SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1241     int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
1242 	ctx->new_session_cb = cb;
1243 }
1244 LSSL_ALIAS(SSL_CTX_sess_set_new_cb);
1245 
1246 int
SSL_CTX_sess_get_new_cb(SSL_CTX * ctx)1247 (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1248 {
1249 	return ctx->new_session_cb;
1250 }
1251 LSSL_ALIAS(SSL_CTX_sess_get_new_cb);
1252 
1253 void
SSL_CTX_sess_set_remove_cb(SSL_CTX * ctx,void (* cb)(SSL_CTX * ctx,SSL_SESSION * sess))1254 SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1255     void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess))
1256 {
1257 	ctx->remove_session_cb = cb;
1258 }
1259 LSSL_ALIAS(SSL_CTX_sess_set_remove_cb);
1260 
1261 void
SSL_CTX_sess_get_remove_cb(SSL_CTX * ctx)1262 (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx, SSL_SESSION *sess)
1263 {
1264 	return ctx->remove_session_cb;
1265 }
1266 LSSL_ALIAS(SSL_CTX_sess_get_remove_cb);
1267 
1268 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))1269 SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, SSL_SESSION *(*cb)(struct ssl_st *ssl,
1270     const unsigned char *data, int len, int *copy))
1271 {
1272 	ctx->get_session_cb = cb;
1273 }
1274 LSSL_ALIAS(SSL_CTX_sess_set_get_cb);
1275 
1276 SSL_SESSION *
SSL_CTX_sess_get_get_cb(SSL_CTX * ctx)1277 (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, const unsigned char *data,
1278     int len, int *copy)
1279 {
1280 	return ctx->get_session_cb;
1281 }
1282 LSSL_ALIAS(SSL_CTX_sess_get_get_cb);
1283 
1284 void
SSL_CTX_set_info_callback(SSL_CTX * ctx,void (* cb)(const SSL * ssl,int type,int val))1285 SSL_CTX_set_info_callback(SSL_CTX *ctx,
1286     void (*cb)(const SSL *ssl, int type, int val))
1287 {
1288 	ctx->info_callback = cb;
1289 }
1290 LSSL_ALIAS(SSL_CTX_set_info_callback);
1291 
1292 void
SSL_CTX_get_info_callback(SSL_CTX * ctx)1293 (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, int val)
1294 {
1295 	return ctx->info_callback;
1296 }
1297 LSSL_ALIAS(SSL_CTX_get_info_callback);
1298 
1299 void
SSL_CTX_set_client_cert_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,X509 ** x509,EVP_PKEY ** pkey))1300 SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1301     int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1302 {
1303 	ctx->client_cert_cb = cb;
1304 }
1305 LSSL_ALIAS(SSL_CTX_set_client_cert_cb);
1306 
1307 int
SSL_CTX_get_client_cert_cb(SSL_CTX * ctx)1308 (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509,
1309     EVP_PKEY **pkey)
1310 {
1311 	return ctx->client_cert_cb;
1312 }
1313 LSSL_ALIAS(SSL_CTX_get_client_cert_cb);
1314 
1315 void
SSL_CTX_set_cookie_generate_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,unsigned char * cookie,unsigned int * cookie_len))1316 SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1317     int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1318 {
1319 	ctx->app_gen_cookie_cb = cb;
1320 }
1321 LSSL_ALIAS(SSL_CTX_set_cookie_generate_cb);
1322 
1323 void
SSL_CTX_set_cookie_verify_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,const unsigned char * cookie,unsigned int cookie_len))1324 SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1325     int (*cb)(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len))
1326 {
1327 	ctx->app_verify_cookie_cb = cb;
1328 }
1329 LSSL_ALIAS(SSL_CTX_set_cookie_verify_cb);
1330 
1331 int
PEM_write_SSL_SESSION(FILE * fp,SSL_SESSION * x)1332 PEM_write_SSL_SESSION(FILE *fp, SSL_SESSION *x)
1333 {
1334 	return PEM_ASN1_write((i2d_of_void *)i2d_SSL_SESSION,
1335 	    PEM_STRING_SSL_SESSION, fp, x, NULL, NULL, 0, NULL, NULL);
1336 }
1337 LSSL_ALIAS(PEM_write_SSL_SESSION);
1338 
1339 SSL_SESSION *
PEM_read_SSL_SESSION(FILE * fp,SSL_SESSION ** x,pem_password_cb * cb,void * u)1340 PEM_read_SSL_SESSION(FILE *fp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1341 {
1342 	return PEM_ASN1_read((d2i_of_void *)d2i_SSL_SESSION,
1343 	    PEM_STRING_SSL_SESSION, fp, (void **)x, cb, u);
1344 }
1345 LSSL_ALIAS(PEM_read_SSL_SESSION);
1346 
1347 SSL_SESSION *
PEM_read_bio_SSL_SESSION(BIO * bp,SSL_SESSION ** x,pem_password_cb * cb,void * u)1348 PEM_read_bio_SSL_SESSION(BIO *bp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1349 {
1350 	return PEM_ASN1_read_bio((d2i_of_void *)d2i_SSL_SESSION,
1351 	    PEM_STRING_SSL_SESSION, bp, (void **)x, cb, u);
1352 }
1353 LSSL_ALIAS(PEM_read_bio_SSL_SESSION);
1354 
1355 int
PEM_write_bio_SSL_SESSION(BIO * bp,SSL_SESSION * x)1356 PEM_write_bio_SSL_SESSION(BIO *bp, SSL_SESSION *x)
1357 {
1358 	return PEM_ASN1_write_bio((i2d_of_void *)i2d_SSL_SESSION,
1359 	    PEM_STRING_SSL_SESSION, bp, x, NULL, NULL, 0, NULL, NULL);
1360 }
1361 LSSL_ALIAS(PEM_write_bio_SSL_SESSION);
1362