ssl_sess.c revision 1.124
1/* $OpenBSD: ssl_sess.c,v 1.124 2024/01/24 14:05:10 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
143static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
144static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
145static 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 */
148SSL_SESSION *
149SSL_get_session(const SSL *ssl)
150{
151	return (ssl->session);
152}
153LSSL_ALIAS(SSL_get_session);
154
155/* variant of SSL_get_session: caller really gets something */
156SSL_SESSION *
157SSL_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}
174LSSL_ALIAS(SSL_get1_session);
175
176int
177SSL_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}
183LSSL_ALIAS(SSL_SESSION_get_ex_new_index);
184
185int
186SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
187{
188	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
189}
190LSSL_ALIAS(SSL_SESSION_set_ex_data);
191
192void *
193SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
194{
195	return (CRYPTO_get_ex_data(&s->ex_data, idx));
196}
197LSSL_ALIAS(SSL_SESSION_get_ex_data);
198
199uint32_t
200SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
201{
202	return 0;
203}
204LSSL_ALIAS(SSL_SESSION_get_max_early_data);
205
206int
207SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
208{
209	return 1;
210}
211LSSL_ALIAS(SSL_SESSION_set_max_early_data);
212
213SSL_SESSION *
214SSL_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}
247LSSL_ALIAS(SSL_SESSION_new);
248
249SSL_SESSION *
250ssl_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
354const unsigned char *
355SSL_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}
361LSSL_ALIAS(SSL_SESSION_get_id);
362
363const unsigned char *
364SSL_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}
370LSSL_ALIAS(SSL_SESSION_get0_id_context);
371
372unsigned int
373SSL_SESSION_get_compress_id(const SSL_SESSION *ss)
374{
375	return 0;
376}
377LSSL_ALIAS(SSL_SESSION_get_compress_id);
378
379unsigned long
380SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
381{
382	return s->tlsext_tick_lifetime_hint;
383}
384LSSL_ALIAS(SSL_SESSION_get_ticket_lifetime_hint);
385
386int
387SSL_SESSION_has_ticket(const SSL_SESSION *s)
388{
389	return (s->tlsext_ticklen > 0) ? 1 : 0;
390}
391LSSL_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
406static int
407def_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
431int
432ssl_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
541static SSL_SESSION *
542ssl_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
571static SSL_SESSION *
572ssl_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
608static SSL_SESSION *
609ssl_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 */
643int
644ssl_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
754int
755SSL_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}
827LSSL_ALIAS(SSL_CTX_add_session);
828
829int
830SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
831{
832	return remove_session_lock(ctx, c, 1);
833}
834LSSL_ALIAS(SSL_CTX_remove_session);
835
836static int
837remove_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
865void
866SSL_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}
895LSSL_ALIAS(SSL_SESSION_free);
896
897int
898SSL_SESSION_up_ref(SSL_SESSION *ss)
899{
900	int refs = CRYPTO_add(&ss->references, 1, CRYPTO_LOCK_SSL_SESSION);
901	return (refs > 1) ? 1 : 0;
902}
903LSSL_ALIAS(SSL_SESSION_up_ref);
904
905int
906SSL_set_session(SSL *s, SSL_SESSION *session)
907{
908	const SSL_METHOD *method;
909
910	if (session == NULL) {
911		SSL_SESSION_free(s->session);
912		s->session = NULL;
913
914		return SSL_set_ssl_method(s, s->ctx->method);
915	}
916
917	if ((method = ssl_get_method(session->ssl_version)) == NULL) {
918		SSLerror(s, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
919		return (0);
920	}
921
922	if (!SSL_set_ssl_method(s, method))
923		return (0);
924
925	CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
926	SSL_SESSION_free(s->session);
927	s->session = session;
928	s->verify_result = s->session->verify_result;
929
930	return (1);
931}
932LSSL_ALIAS(SSL_set_session);
933
934size_t
935SSL_SESSION_get_master_key(const SSL_SESSION *ss, unsigned char *out,
936    size_t max_out)
937{
938	size_t len = ss->master_key_length;
939
940	if (out == NULL)
941		return len;
942
943	if (len > max_out)
944		len = max_out;
945
946	memcpy(out, ss->master_key, len);
947
948	return len;
949}
950LSSL_ALIAS(SSL_SESSION_get_master_key);
951
952long
953SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
954{
955	if (s == NULL)
956		return (0);
957	s->timeout = t;
958	return (1);
959}
960LSSL_ALIAS(SSL_SESSION_set_timeout);
961
962long
963SSL_SESSION_get_timeout(const SSL_SESSION *s)
964{
965	if (s == NULL)
966		return (0);
967	return (s->timeout);
968}
969LSSL_ALIAS(SSL_SESSION_get_timeout);
970
971/* XXX 2038 */
972long
973SSL_SESSION_get_time(const SSL_SESSION *s)
974{
975	if (s == NULL)
976		return (0);
977	return (s->time);
978}
979LSSL_ALIAS(SSL_SESSION_get_time);
980
981/* XXX 2038 */
982long
983SSL_SESSION_set_time(SSL_SESSION *s, long t)
984{
985	if (s == NULL)
986		return (0);
987	s->time = t;
988	return (t);
989}
990LSSL_ALIAS(SSL_SESSION_set_time);
991
992int
993SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
994{
995	return s->ssl_version;
996}
997LSSL_ALIAS(SSL_SESSION_get_protocol_version);
998
999const SSL_CIPHER *
1000SSL_SESSION_get0_cipher(const SSL_SESSION *s)
1001{
1002	return s->cipher;
1003}
1004LSSL_ALIAS(SSL_SESSION_get0_cipher);
1005
1006X509 *
1007SSL_SESSION_get0_peer(SSL_SESSION *s)
1008{
1009	return s->peer_cert;
1010}
1011LSSL_ALIAS(SSL_SESSION_get0_peer);
1012
1013int
1014SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
1015    unsigned int sid_len)
1016{
1017	if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1018		SSLerrorx(SSL_R_SSL_SESSION_ID_TOO_LONG);
1019		return 0;
1020	}
1021	s->session_id_length = sid_len;
1022	memmove(s->session_id, sid, sid_len);
1023	return 1;
1024}
1025LSSL_ALIAS(SSL_SESSION_set1_id);
1026
1027int
1028SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1029    unsigned int sid_ctx_len)
1030{
1031	if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1032		SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1033		return 0;
1034	}
1035	s->sid_ctx_length = sid_ctx_len;
1036	memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1037
1038	return 1;
1039}
1040LSSL_ALIAS(SSL_SESSION_set1_id_context);
1041
1042int
1043SSL_SESSION_is_resumable(const SSL_SESSION *s)
1044{
1045	return 0;
1046}
1047LSSL_ALIAS(SSL_SESSION_is_resumable);
1048
1049long
1050SSL_CTX_set_timeout(SSL_CTX *s, long t)
1051{
1052	long l;
1053
1054	if (s == NULL)
1055		return (0);
1056	l = s->session_timeout;
1057	s->session_timeout = t;
1058
1059	return (l);
1060}
1061LSSL_ALIAS(SSL_CTX_set_timeout);
1062
1063long
1064SSL_CTX_get_timeout(const SSL_CTX *s)
1065{
1066	if (s == NULL)
1067		return (0);
1068	return (s->session_timeout);
1069}
1070LSSL_ALIAS(SSL_CTX_get_timeout);
1071
1072int
1073SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s,
1074    void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers,
1075    SSL_CIPHER **cipher, void *arg), void *arg)
1076{
1077	if (s == NULL)
1078		return (0);
1079	s->tls_session_secret_cb = tls_session_secret_cb;
1080	s->tls_session_secret_cb_arg = arg;
1081	return (1);
1082}
1083LSSL_ALIAS(SSL_set_session_secret_cb);
1084
1085int
1086SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1087    void *arg)
1088{
1089	if (s == NULL)
1090		return (0);
1091	s->tls_session_ticket_ext_cb = cb;
1092	s->tls_session_ticket_ext_cb_arg = arg;
1093	return (1);
1094}
1095LSSL_ALIAS(SSL_set_session_ticket_ext_cb);
1096
1097int
1098SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1099{
1100	if (s->version >= TLS1_VERSION) {
1101		free(s->tlsext_session_ticket);
1102		s->tlsext_session_ticket =
1103		    malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1104		if (!s->tlsext_session_ticket) {
1105			SSLerror(s, ERR_R_MALLOC_FAILURE);
1106			return 0;
1107		}
1108
1109		if (ext_data) {
1110			s->tlsext_session_ticket->length = ext_len;
1111			s->tlsext_session_ticket->data =
1112			    s->tlsext_session_ticket + 1;
1113			memcpy(s->tlsext_session_ticket->data,
1114			    ext_data, ext_len);
1115		} else {
1116			s->tlsext_session_ticket->length = 0;
1117			s->tlsext_session_ticket->data = NULL;
1118		}
1119
1120		return 1;
1121	}
1122
1123	return 0;
1124}
1125LSSL_ALIAS(SSL_set_session_ticket_ext);
1126
1127typedef struct timeout_param_st {
1128	SSL_CTX *ctx;
1129	long time;
1130	struct lhash_st_SSL_SESSION *cache;
1131} TIMEOUT_PARAM;
1132
1133static void
1134timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p)
1135{
1136	if ((p->time == 0) || (p->time > (s->time + s->timeout))) {
1137		/* timeout */
1138		/* The reason we don't call SSL_CTX_remove_session() is to
1139		 * save on locking overhead */
1140		(void)lh_SSL_SESSION_delete(p->cache, s);
1141		SSL_SESSION_list_remove(p->ctx, s);
1142		s->not_resumable = 1;
1143		if (p->ctx->remove_session_cb != NULL)
1144			p->ctx->remove_session_cb(p->ctx, s);
1145		SSL_SESSION_free(s);
1146	}
1147}
1148
1149static void
1150timeout_LHASH_DOALL_ARG(void *arg1, void *arg2)
1151{
1152	SSL_SESSION *a = arg1;
1153	TIMEOUT_PARAM *b = arg2;
1154
1155	timeout_doall_arg(a, b);
1156}
1157
1158/* XXX 2038 */
1159void
1160SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1161{
1162	TIMEOUT_PARAM tp;
1163
1164	tp.ctx = s;
1165	tp.cache = s->sessions;
1166	if (tp.cache == NULL)
1167		return;
1168	tp.time = t;
1169
1170	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
1171	lh_SSL_SESSION_doall_arg(tp.cache, timeout_LHASH_DOALL_ARG,
1172	    TIMEOUT_PARAM, &tp);
1173	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
1174}
1175LSSL_ALIAS(SSL_CTX_flush_sessions);
1176
1177int
1178ssl_clear_bad_session(SSL *s)
1179{
1180	if ((s->session != NULL) && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1181	    !(SSL_in_init(s) || SSL_in_before(s))) {
1182		SSL_CTX_remove_session(s->ctx, s->session);
1183		return (1);
1184	} else
1185		return (0);
1186}
1187
1188/* locked by SSL_CTX in the calling function */
1189static void
1190SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1191{
1192	if (s->next == NULL || s->prev == NULL)
1193		return;
1194
1195	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1196		/* last element in list */
1197		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1198			/* only one element in list */
1199			ctx->session_cache_head = NULL;
1200			ctx->session_cache_tail = NULL;
1201		} else {
1202			ctx->session_cache_tail = s->prev;
1203			s->prev->next =
1204			    (SSL_SESSION *)&(ctx->session_cache_tail);
1205		}
1206	} else {
1207		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1208			/* first element in list */
1209			ctx->session_cache_head = s->next;
1210			s->next->prev =
1211			    (SSL_SESSION *)&(ctx->session_cache_head);
1212		} else {
1213			/* middle of list */
1214			s->next->prev = s->prev;
1215			s->prev->next = s->next;
1216		}
1217	}
1218	s->prev = s->next = NULL;
1219}
1220
1221static void
1222SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1223{
1224	if (s->next != NULL && s->prev != NULL)
1225		SSL_SESSION_list_remove(ctx, s);
1226
1227	if (ctx->session_cache_head == NULL) {
1228		ctx->session_cache_head = s;
1229		ctx->session_cache_tail = s;
1230		s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1231		s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1232	} else {
1233		s->next = ctx->session_cache_head;
1234		s->next->prev = s;
1235		s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1236		ctx->session_cache_head = s;
1237	}
1238}
1239
1240void
1241SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1242    int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
1243	ctx->new_session_cb = cb;
1244}
1245LSSL_ALIAS(SSL_CTX_sess_set_new_cb);
1246
1247int
1248(*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1249{
1250	return ctx->new_session_cb;
1251}
1252LSSL_ALIAS(SSL_CTX_sess_get_new_cb);
1253
1254void
1255SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1256    void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess))
1257{
1258	ctx->remove_session_cb = cb;
1259}
1260LSSL_ALIAS(SSL_CTX_sess_set_remove_cb);
1261
1262void
1263(*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx, SSL_SESSION *sess)
1264{
1265	return ctx->remove_session_cb;
1266}
1267LSSL_ALIAS(SSL_CTX_sess_get_remove_cb);
1268
1269void
1270SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, SSL_SESSION *(*cb)(struct ssl_st *ssl,
1271    const unsigned char *data, int len, int *copy))
1272{
1273	ctx->get_session_cb = cb;
1274}
1275LSSL_ALIAS(SSL_CTX_sess_set_get_cb);
1276
1277SSL_SESSION *
1278(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, const unsigned char *data,
1279    int len, int *copy)
1280{
1281	return ctx->get_session_cb;
1282}
1283LSSL_ALIAS(SSL_CTX_sess_get_get_cb);
1284
1285void
1286SSL_CTX_set_info_callback(SSL_CTX *ctx,
1287    void (*cb)(const SSL *ssl, int type, int val))
1288{
1289	ctx->info_callback = cb;
1290}
1291LSSL_ALIAS(SSL_CTX_set_info_callback);
1292
1293void
1294(*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, int val)
1295{
1296	return ctx->info_callback;
1297}
1298LSSL_ALIAS(SSL_CTX_get_info_callback);
1299
1300void
1301SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1302    int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1303{
1304	ctx->client_cert_cb = cb;
1305}
1306LSSL_ALIAS(SSL_CTX_set_client_cert_cb);
1307
1308int
1309(*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509,
1310    EVP_PKEY **pkey)
1311{
1312	return ctx->client_cert_cb;
1313}
1314LSSL_ALIAS(SSL_CTX_get_client_cert_cb);
1315
1316void
1317SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1318    int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1319{
1320	ctx->app_gen_cookie_cb = cb;
1321}
1322LSSL_ALIAS(SSL_CTX_set_cookie_generate_cb);
1323
1324void
1325SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1326    int (*cb)(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len))
1327{
1328	ctx->app_verify_cookie_cb = cb;
1329}
1330LSSL_ALIAS(SSL_CTX_set_cookie_verify_cb);
1331
1332int
1333PEM_write_SSL_SESSION(FILE *fp, SSL_SESSION *x)
1334{
1335	return PEM_ASN1_write((i2d_of_void *)i2d_SSL_SESSION,
1336	    PEM_STRING_SSL_SESSION, fp, x, NULL, NULL, 0, NULL, NULL);
1337}
1338LSSL_ALIAS(PEM_write_SSL_SESSION);
1339
1340SSL_SESSION *
1341PEM_read_SSL_SESSION(FILE *fp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1342{
1343	return PEM_ASN1_read((d2i_of_void *)d2i_SSL_SESSION,
1344	    PEM_STRING_SSL_SESSION, fp, (void **)x, cb, u);
1345}
1346LSSL_ALIAS(PEM_read_SSL_SESSION);
1347
1348SSL_SESSION *
1349PEM_read_bio_SSL_SESSION(BIO *bp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1350{
1351	return PEM_ASN1_read_bio((d2i_of_void *)d2i_SSL_SESSION,
1352	    PEM_STRING_SSL_SESSION, bp, (void **)x, cb, u);
1353}
1354LSSL_ALIAS(PEM_read_bio_SSL_SESSION);
1355
1356int
1357PEM_write_bio_SSL_SESSION(BIO *bp, SSL_SESSION *x)
1358{
1359	return PEM_ASN1_write_bio((i2d_of_void *)i2d_SSL_SESSION,
1360	    PEM_STRING_SSL_SESSION, bp, x, NULL, NULL, 0, NULL, NULL);
1361}
1362LSSL_ALIAS(PEM_write_bio_SSL_SESSION);
1363