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