ssl_lib.c revision 1.226
1/* $OpenBSD: ssl_lib.c,v 1.226 2020/09/13 16:49:05 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-2007 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 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * ECC cipher suite support in OpenSSL originally developed by
114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115 */
116/* ====================================================================
117 * Copyright 2005 Nokia. All rights reserved.
118 *
119 * The portions of the attached software ("Contribution") is developed by
120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121 * license.
122 *
123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125 * support (see RFC 4279) to OpenSSL.
126 *
127 * No patent licenses or other rights except those expressly stated in
128 * the OpenSSL open source license shall be deemed granted or received
129 * expressly, by implication, estoppel, or otherwise.
130 *
131 * No assurances are provided by Nokia that the Contribution does not
132 * infringe the patent or other intellectual property rights of any third
133 * party or that the license provides you with all the necessary rights
134 * to make use of the Contribution.
135 *
136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140 * OTHERWISE.
141 */
142
143#include <arpa/inet.h>
144#include <sys/socket.h>
145#include <netinet/in.h>
146
147#include <stdio.h>
148
149#include "ssl_locl.h"
150
151#include <openssl/bn.h>
152#include <openssl/dh.h>
153#include <openssl/lhash.h>
154#include <openssl/objects.h>
155#include <openssl/ocsp.h>
156#include <openssl/x509v3.h>
157
158#ifndef OPENSSL_NO_ENGINE
159#include <openssl/engine.h>
160#endif
161
162#include "bytestring.h"
163#include "ssl_sigalgs.h"
164
165const char *SSL_version_str = OPENSSL_VERSION_TEXT;
166
167int
168SSL_clear(SSL *s)
169{
170	if (s->method == NULL) {
171		SSLerror(s, SSL_R_NO_METHOD_SPECIFIED);
172		return (0);
173	}
174
175	if (ssl_clear_bad_session(s)) {
176		SSL_SESSION_free(s->session);
177		s->session = NULL;
178	}
179
180	s->error = 0;
181	s->internal->hit = 0;
182	s->internal->shutdown = 0;
183
184	if (s->internal->renegotiate) {
185		SSLerror(s, ERR_R_INTERNAL_ERROR);
186		return (0);
187	}
188
189	s->internal->type = 0;
190
191	s->version = s->method->internal->version;
192	s->client_version = s->version;
193	s->internal->rwstate = SSL_NOTHING;
194	s->internal->rstate = SSL_ST_READ_HEADER;
195
196	tls13_ctx_free(s->internal->tls13);
197	s->internal->tls13 = NULL;
198
199	BUF_MEM_free(s->internal->init_buf);
200	s->internal->init_buf = NULL;
201
202	ssl_clear_cipher_state(s);
203
204	s->internal->first_packet = 0;
205
206	/*
207	 * Check to see if we were changed into a different method, if
208	 * so, revert back if we are not doing session-id reuse.
209	 */
210	if (!s->internal->in_handshake && (s->session == NULL) &&
211	    (s->method != s->ctx->method)) {
212		s->method->internal->ssl_free(s);
213		s->method = s->ctx->method;
214		if (!s->method->internal->ssl_new(s))
215			return (0);
216	} else
217		s->method->internal->ssl_clear(s);
218
219	S3I(s)->hs.state = SSL_ST_BEFORE|((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT);
220
221	return (1);
222}
223
224/* Used to change an SSL_CTXs default SSL method type */
225int
226SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
227{
228	STACK_OF(SSL_CIPHER) *ciphers;
229
230	ctx->method = meth;
231
232	ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
233	    ctx->internal->cipher_list_tls13, SSL_DEFAULT_CIPHER_LIST);
234	if (ciphers == NULL || sk_SSL_CIPHER_num(ciphers) <= 0) {
235		SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
236		return (0);
237	}
238	return (1);
239}
240
241SSL *
242SSL_new(SSL_CTX *ctx)
243{
244	SSL *s;
245
246	if (ctx == NULL) {
247		SSLerrorx(SSL_R_NULL_SSL_CTX);
248		return (NULL);
249	}
250	if (ctx->method == NULL) {
251		SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
252		return (NULL);
253	}
254
255	if ((s = calloc(1, sizeof(*s))) == NULL)
256		goto err;
257	if ((s->internal = calloc(1, sizeof(*s->internal))) == NULL)
258		goto err;
259
260	s->internal->min_version = ctx->internal->min_version;
261	s->internal->max_version = ctx->internal->max_version;
262
263	s->internal->options = ctx->internal->options;
264	s->internal->mode = ctx->internal->mode;
265	s->internal->max_cert_list = ctx->internal->max_cert_list;
266
267	if ((s->cert = ssl_cert_dup(ctx->internal->cert)) == NULL)
268		goto err;
269
270	s->internal->read_ahead = ctx->internal->read_ahead;
271	s->internal->msg_callback = ctx->internal->msg_callback;
272	s->internal->msg_callback_arg = ctx->internal->msg_callback_arg;
273	s->verify_mode = ctx->verify_mode;
274	s->sid_ctx_length = ctx->sid_ctx_length;
275	OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
276	memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
277	s->internal->verify_callback = ctx->internal->default_verify_callback;
278	s->internal->generate_session_id = ctx->internal->generate_session_id;
279
280	s->param = X509_VERIFY_PARAM_new();
281	if (!s->param)
282		goto err;
283	X509_VERIFY_PARAM_inherit(s->param, ctx->param);
284	s->internal->quiet_shutdown = ctx->internal->quiet_shutdown;
285	s->max_send_fragment = ctx->internal->max_send_fragment;
286
287	CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
288	s->ctx = ctx;
289	s->internal->tlsext_debug_cb = 0;
290	s->internal->tlsext_debug_arg = NULL;
291	s->internal->tlsext_ticket_expected = 0;
292	s->tlsext_status_type = -1;
293	s->internal->tlsext_status_expected = 0;
294	s->internal->tlsext_ocsp_ids = NULL;
295	s->internal->tlsext_ocsp_exts = NULL;
296	s->internal->tlsext_ocsp_resp = NULL;
297	s->internal->tlsext_ocsp_resp_len = 0;
298	CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
299	s->initial_ctx = ctx;
300
301	if (ctx->internal->tlsext_ecpointformatlist != NULL) {
302		s->internal->tlsext_ecpointformatlist =
303		    calloc(ctx->internal->tlsext_ecpointformatlist_length,
304			sizeof(ctx->internal->tlsext_ecpointformatlist[0]));
305		if (s->internal->tlsext_ecpointformatlist == NULL)
306			goto err;
307		memcpy(s->internal->tlsext_ecpointformatlist,
308		    ctx->internal->tlsext_ecpointformatlist,
309		    ctx->internal->tlsext_ecpointformatlist_length *
310		    sizeof(ctx->internal->tlsext_ecpointformatlist[0]));
311		s->internal->tlsext_ecpointformatlist_length =
312		    ctx->internal->tlsext_ecpointformatlist_length;
313	}
314	if (ctx->internal->tlsext_supportedgroups != NULL) {
315		s->internal->tlsext_supportedgroups =
316		    calloc(ctx->internal->tlsext_supportedgroups_length,
317			sizeof(ctx->internal->tlsext_supportedgroups[0]));
318		if (s->internal->tlsext_supportedgroups == NULL)
319			goto err;
320		memcpy(s->internal->tlsext_supportedgroups,
321		    ctx->internal->tlsext_supportedgroups,
322		    ctx->internal->tlsext_supportedgroups_length *
323		    sizeof(ctx->internal->tlsext_supportedgroups[0]));
324		s->internal->tlsext_supportedgroups_length =
325		    ctx->internal->tlsext_supportedgroups_length;
326	}
327
328	if (s->ctx->internal->alpn_client_proto_list != NULL) {
329		s->internal->alpn_client_proto_list =
330		    malloc(s->ctx->internal->alpn_client_proto_list_len);
331		if (s->internal->alpn_client_proto_list == NULL)
332			goto err;
333		memcpy(s->internal->alpn_client_proto_list,
334		    s->ctx->internal->alpn_client_proto_list,
335		    s->ctx->internal->alpn_client_proto_list_len);
336		s->internal->alpn_client_proto_list_len =
337		    s->ctx->internal->alpn_client_proto_list_len;
338	}
339
340	s->verify_result = X509_V_OK;
341
342	s->method = ctx->method;
343
344	if (!s->method->internal->ssl_new(s))
345		goto err;
346
347	if ((s->internal->rl = tls12_record_layer_new()) == NULL)
348		goto err;
349
350	s->references = 1;
351	s->server = (ctx->method->internal->ssl_accept == ssl_undefined_function) ? 0 : 1;
352
353	SSL_clear(s);
354
355	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->internal->ex_data);
356
357	return (s);
358
359 err:
360	SSL_free(s);
361	SSLerrorx(ERR_R_MALLOC_FAILURE);
362	return (NULL);
363}
364
365int
366SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
367    unsigned int sid_ctx_len)
368{
369	if (sid_ctx_len > sizeof ctx->sid_ctx) {
370		SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
371		return (0);
372	}
373	ctx->sid_ctx_length = sid_ctx_len;
374	memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
375
376	return (1);
377}
378
379int
380SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
381    unsigned int sid_ctx_len)
382{
383	if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
384		SSLerror(ssl, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
385		return (0);
386	}
387	ssl->sid_ctx_length = sid_ctx_len;
388	memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
389
390	return (1);
391}
392
393int
394SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
395{
396	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
397	ctx->internal->generate_session_id = cb;
398	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
399	return (1);
400}
401
402int
403SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
404{
405	CRYPTO_w_lock(CRYPTO_LOCK_SSL);
406	ssl->internal->generate_session_id = cb;
407	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
408	return (1);
409}
410
411int
412SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
413    unsigned int id_len)
414{
415	/*
416	 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp
417	 * shows how we can "construct" a session to give us the desired
418	 * check - ie. to find if there's a session in the hash table
419	 * that would conflict with any new session built out of this
420	 * id/id_len and the ssl_version in use by this SSL.
421	 */
422	SSL_SESSION r, *p;
423
424	if (id_len > sizeof r.session_id)
425		return (0);
426
427	r.ssl_version = ssl->version;
428	r.session_id_length = id_len;
429	memcpy(r.session_id, id, id_len);
430
431	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
432	p = lh_SSL_SESSION_retrieve(ssl->ctx->internal->sessions, &r);
433	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
434	return (p != NULL);
435}
436
437int
438SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
439{
440	return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
441}
442
443int
444SSL_set_purpose(SSL *s, int purpose)
445{
446	return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
447}
448
449int
450SSL_CTX_set_trust(SSL_CTX *s, int trust)
451{
452	return (X509_VERIFY_PARAM_set_trust(s->param, trust));
453}
454
455int
456SSL_set_trust(SSL *s, int trust)
457{
458	return (X509_VERIFY_PARAM_set_trust(s->param, trust));
459}
460
461int
462SSL_set1_host(SSL *s, const char *hostname)
463{
464	struct in_addr ina;
465	struct in6_addr in6a;
466
467	if (hostname != NULL && *hostname != '\0' &&
468	    (inet_pton(AF_INET, hostname, &ina) == 1 ||
469	    inet_pton(AF_INET6, hostname, &in6a) == 1))
470		return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
471	else
472		return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
473}
474
475X509_VERIFY_PARAM *
476SSL_CTX_get0_param(SSL_CTX *ctx)
477{
478	return (ctx->param);
479}
480
481int
482SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
483{
484	return (X509_VERIFY_PARAM_set1(ctx->param, vpm));
485}
486
487X509_VERIFY_PARAM *
488SSL_get0_param(SSL *ssl)
489{
490	return (ssl->param);
491}
492
493int
494SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
495{
496	return (X509_VERIFY_PARAM_set1(ssl->param, vpm));
497}
498
499void
500SSL_free(SSL *s)
501{
502	int	i;
503
504	if (s == NULL)
505		return;
506
507	i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
508	if (i > 0)
509		return;
510
511	X509_VERIFY_PARAM_free(s->param);
512
513	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->internal->ex_data);
514
515	if (s->bbio != NULL) {
516		/* If the buffering BIO is in place, pop it off */
517		if (s->bbio == s->wbio) {
518			s->wbio = BIO_pop(s->wbio);
519		}
520		BIO_free(s->bbio);
521		s->bbio = NULL;
522	}
523
524	if (s->rbio != s->wbio)
525		BIO_free_all(s->rbio);
526	BIO_free_all(s->wbio);
527
528	tls13_ctx_free(s->internal->tls13);
529
530	BUF_MEM_free(s->internal->init_buf);
531
532	sk_SSL_CIPHER_free(s->cipher_list);
533	sk_SSL_CIPHER_free(s->internal->cipher_list_tls13);
534
535	/* Make the next call work :-) */
536	if (s->session != NULL) {
537		ssl_clear_bad_session(s);
538		SSL_SESSION_free(s->session);
539	}
540
541	ssl_clear_cipher_state(s);
542
543	ssl_cert_free(s->cert);
544
545	free(s->tlsext_hostname);
546	SSL_CTX_free(s->initial_ctx);
547
548	free(s->internal->tlsext_ecpointformatlist);
549	free(s->internal->tlsext_supportedgroups);
550
551	sk_X509_EXTENSION_pop_free(s->internal->tlsext_ocsp_exts,
552	    X509_EXTENSION_free);
553	sk_OCSP_RESPID_pop_free(s->internal->tlsext_ocsp_ids, OCSP_RESPID_free);
554	free(s->internal->tlsext_ocsp_resp);
555
556	sk_X509_NAME_pop_free(s->internal->client_CA, X509_NAME_free);
557
558	if (s->method != NULL)
559		s->method->internal->ssl_free(s);
560
561	SSL_CTX_free(s->ctx);
562
563	free(s->internal->alpn_client_proto_list);
564
565#ifndef OPENSSL_NO_SRTP
566	sk_SRTP_PROTECTION_PROFILE_free(s->internal->srtp_profiles);
567#endif
568
569	tls12_record_layer_free(s->internal->rl);
570
571	free(s->internal);
572	free(s);
573}
574
575int
576SSL_up_ref(SSL *s)
577{
578	int refs = CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL);
579	return (refs > 1) ? 1 : 0;
580}
581
582void
583SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
584{
585	/* If the output buffering BIO is still in place, remove it */
586	if (s->bbio != NULL) {
587		if (s->wbio == s->bbio) {
588			s->wbio = s->wbio->next_bio;
589			s->bbio->next_bio = NULL;
590		}
591	}
592
593	if (s->rbio != rbio && s->rbio != s->wbio)
594		BIO_free_all(s->rbio);
595	if (s->wbio != wbio)
596		BIO_free_all(s->wbio);
597	s->rbio = rbio;
598	s->wbio = wbio;
599}
600
601BIO *
602SSL_get_rbio(const SSL *s)
603{
604	return (s->rbio);
605}
606
607BIO *
608SSL_get_wbio(const SSL *s)
609{
610	return (s->wbio);
611}
612
613int
614SSL_get_fd(const SSL *s)
615{
616	return (SSL_get_rfd(s));
617}
618
619int
620SSL_get_rfd(const SSL *s)
621{
622	int	 ret = -1;
623	BIO	*b, *r;
624
625	b = SSL_get_rbio(s);
626	r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
627	if (r != NULL)
628		BIO_get_fd(r, &ret);
629	return (ret);
630}
631
632int
633SSL_get_wfd(const SSL *s)
634{
635	int	 ret = -1;
636	BIO	*b, *r;
637
638	b = SSL_get_wbio(s);
639	r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
640	if (r != NULL)
641		BIO_get_fd(r, &ret);
642	return (ret);
643}
644
645int
646SSL_set_fd(SSL *s, int fd)
647{
648	int	 ret = 0;
649	BIO	*bio = NULL;
650
651	bio = BIO_new(BIO_s_socket());
652
653	if (bio == NULL) {
654		SSLerror(s, ERR_R_BUF_LIB);
655		goto err;
656	}
657	BIO_set_fd(bio, fd, BIO_NOCLOSE);
658	SSL_set_bio(s, bio, bio);
659	ret = 1;
660err:
661	return (ret);
662}
663
664int
665SSL_set_wfd(SSL *s, int fd)
666{
667	int	 ret = 0;
668	BIO	*bio = NULL;
669
670	if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
671	    || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
672		bio = BIO_new(BIO_s_socket());
673
674		if (bio == NULL) {
675			SSLerror(s, ERR_R_BUF_LIB);
676			goto err;
677		}
678		BIO_set_fd(bio, fd, BIO_NOCLOSE);
679		SSL_set_bio(s, SSL_get_rbio(s), bio);
680	} else
681		SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
682	ret = 1;
683err:
684	return (ret);
685}
686
687int
688SSL_set_rfd(SSL *s, int fd)
689{
690	int	 ret = 0;
691	BIO	*bio = NULL;
692
693	if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
694	    || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
695		bio = BIO_new(BIO_s_socket());
696
697		if (bio == NULL) {
698			SSLerror(s, ERR_R_BUF_LIB);
699			goto err;
700		}
701		BIO_set_fd(bio, fd, BIO_NOCLOSE);
702		SSL_set_bio(s, bio, SSL_get_wbio(s));
703	} else
704		SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
705	ret = 1;
706err:
707	return (ret);
708}
709
710
711/* return length of latest Finished message we sent, copy to 'buf' */
712size_t
713SSL_get_finished(const SSL *s, void *buf, size_t count)
714{
715	size_t	ret;
716
717	ret = S3I(s)->tmp.finish_md_len;
718	if (count > ret)
719		count = ret;
720	memcpy(buf, S3I(s)->tmp.finish_md, count);
721	return (ret);
722}
723
724/* return length of latest Finished message we expected, copy to 'buf' */
725size_t
726SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
727{
728	size_t	ret;
729
730	ret = S3I(s)->tmp.peer_finish_md_len;
731	if (count > ret)
732		count = ret;
733	memcpy(buf, S3I(s)->tmp.peer_finish_md, count);
734	return (ret);
735}
736
737
738int
739SSL_get_verify_mode(const SSL *s)
740{
741	return (s->verify_mode);
742}
743
744int
745SSL_get_verify_depth(const SSL *s)
746{
747	return (X509_VERIFY_PARAM_get_depth(s->param));
748}
749
750int
751(*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *)
752{
753	return (s->internal->verify_callback);
754}
755
756int
757SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
758{
759	return (ctx->verify_mode);
760}
761
762int
763SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
764{
765	return (X509_VERIFY_PARAM_get_depth(ctx->param));
766}
767
768int
769(*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *)
770{
771	return (ctx->internal->default_verify_callback);
772}
773
774void
775SSL_set_verify(SSL *s, int mode,
776    int (*callback)(int ok, X509_STORE_CTX *ctx))
777{
778	s->verify_mode = mode;
779	if (callback != NULL)
780		s->internal->verify_callback = callback;
781}
782
783void
784SSL_set_verify_depth(SSL *s, int depth)
785{
786	X509_VERIFY_PARAM_set_depth(s->param, depth);
787}
788
789void
790SSL_set_read_ahead(SSL *s, int yes)
791{
792	s->internal->read_ahead = yes;
793}
794
795int
796SSL_get_read_ahead(const SSL *s)
797{
798	return (s->internal->read_ahead);
799}
800
801int
802SSL_pending(const SSL *s)
803{
804	return (s->method->internal->ssl_pending(s));
805}
806
807X509 *
808SSL_get_peer_certificate(const SSL *s)
809{
810	X509	*r;
811
812	if ((s == NULL) || (s->session == NULL))
813		r = NULL;
814	else
815		r = s->session->peer;
816
817	if (r == NULL)
818		return (r);
819
820	CRYPTO_add(&r->references, 1, CRYPTO_LOCK_X509);
821
822	return (r);
823}
824
825STACK_OF(X509) *
826SSL_get_peer_cert_chain(const SSL *s)
827{
828	STACK_OF(X509)	*r;
829
830	if ((s == NULL) || (s->session == NULL) ||
831	    (SSI(s)->sess_cert == NULL))
832		r = NULL;
833	else
834		r = SSI(s)->sess_cert->cert_chain;
835
836	/*
837	 * If we are a client, cert_chain includes the peer's own
838	 * certificate;
839	 * if we are a server, it does not.
840	 */
841	return (r);
842}
843
844/*
845 * Now in theory, since the calling process own 't' it should be safe to
846 * modify.  We need to be able to read f without being hassled
847 */
848int
849SSL_copy_session_id(SSL *t, const SSL *f)
850{
851	CERT	*tmp;
852
853	/* Do we need to do SSL locking? */
854	if (!SSL_set_session(t, SSL_get_session(f)))
855		return 0;
856
857	/* What if we are set up for one protocol but want to talk another? */
858	if (t->method != f->method) {
859		t->method->internal->ssl_free(t);
860		t->method = f->method;
861		if (!t->method->internal->ssl_new(t))
862			return 0;
863	}
864
865	tmp = t->cert;
866	if (f->cert != NULL) {
867		CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
868		t->cert = f->cert;
869	} else
870		t->cert = NULL;
871	ssl_cert_free(tmp);
872
873	if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length))
874		return 0;
875
876	return 1;
877}
878
879/* Fix this so it checks all the valid key/cert options */
880int
881SSL_CTX_check_private_key(const SSL_CTX *ctx)
882{
883	if ((ctx == NULL) || (ctx->internal->cert == NULL) ||
884	    (ctx->internal->cert->key->x509 == NULL)) {
885		SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED);
886		return (0);
887	}
888	if (ctx->internal->cert->key->privatekey == NULL) {
889		SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED);
890		return (0);
891	}
892	return (X509_check_private_key(ctx->internal->cert->key->x509,
893	    ctx->internal->cert->key->privatekey));
894}
895
896/* Fix this function so that it takes an optional type parameter */
897int
898SSL_check_private_key(const SSL *ssl)
899{
900	if (ssl == NULL) {
901		SSLerrorx(ERR_R_PASSED_NULL_PARAMETER);
902		return (0);
903	}
904	if (ssl->cert == NULL) {
905		SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
906		return (0);
907	}
908	if (ssl->cert->key->x509 == NULL) {
909		SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
910		return (0);
911	}
912	if (ssl->cert->key->privatekey == NULL) {
913		SSLerror(ssl, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
914		return (0);
915	}
916	return (X509_check_private_key(ssl->cert->key->x509,
917	    ssl->cert->key->privatekey));
918}
919
920int
921SSL_accept(SSL *s)
922{
923	if (s->internal->handshake_func == NULL)
924		SSL_set_accept_state(s); /* Not properly initialized yet */
925
926	return (s->method->internal->ssl_accept(s));
927}
928
929int
930SSL_connect(SSL *s)
931{
932	if (s->internal->handshake_func == NULL)
933		SSL_set_connect_state(s); /* Not properly initialized yet */
934
935	return (s->method->internal->ssl_connect(s));
936}
937
938int
939SSL_is_server(const SSL *s)
940{
941	return s->server;
942}
943
944static long
945ssl_get_default_timeout()
946{
947	/*
948	 * 2 hours, the 24 hours mentioned in the TLSv1 spec
949	 * is way too long for http, the cache would over fill.
950	 */
951	return (2 * 60 * 60);
952}
953
954long
955SSL_get_default_timeout(const SSL *s)
956{
957	return (ssl_get_default_timeout());
958}
959
960int
961SSL_read(SSL *s, void *buf, int num)
962{
963	if (s->internal->handshake_func == NULL) {
964		SSLerror(s, SSL_R_UNINITIALIZED);
965		return (-1);
966	}
967
968	if (s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) {
969		s->internal->rwstate = SSL_NOTHING;
970		return (0);
971	}
972	return ssl3_read(s, buf, num);
973}
974
975int
976SSL_peek(SSL *s, void *buf, int num)
977{
978	if (s->internal->handshake_func == NULL) {
979		SSLerror(s, SSL_R_UNINITIALIZED);
980		return (-1);
981	}
982
983	if (s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) {
984		return (0);
985	}
986	return ssl3_peek(s, buf, num);
987}
988
989int
990SSL_write(SSL *s, const void *buf, int num)
991{
992	if (s->internal->handshake_func == NULL) {
993		SSLerror(s, SSL_R_UNINITIALIZED);
994		return (-1);
995	}
996
997	if (s->internal->shutdown & SSL_SENT_SHUTDOWN) {
998		s->internal->rwstate = SSL_NOTHING;
999		SSLerror(s, SSL_R_PROTOCOL_IS_SHUTDOWN);
1000		return (-1);
1001	}
1002	return ssl3_write(s, buf, num);
1003}
1004
1005int
1006SSL_shutdown(SSL *s)
1007{
1008	/*
1009	 * Note that this function behaves differently from what one might
1010	 * expect.  Return values are 0 for no success (yet),
1011	 * 1 for success; but calling it once is usually not enough,
1012	 * even if blocking I/O is used (see ssl3_shutdown).
1013	 */
1014
1015	if (s->internal->handshake_func == NULL) {
1016		SSLerror(s, SSL_R_UNINITIALIZED);
1017		return (-1);
1018	}
1019
1020	if (s != NULL && !SSL_in_init(s))
1021		return (s->method->internal->ssl_shutdown(s));
1022
1023	return (1);
1024}
1025
1026int
1027SSL_renegotiate(SSL *s)
1028{
1029	if (s->internal->renegotiate == 0)
1030		s->internal->renegotiate = 1;
1031
1032	s->internal->new_session = 1;
1033
1034	return (s->method->internal->ssl_renegotiate(s));
1035}
1036
1037int
1038SSL_renegotiate_abbreviated(SSL *s)
1039{
1040	if (s->internal->renegotiate == 0)
1041		s->internal->renegotiate = 1;
1042
1043	s->internal->new_session = 0;
1044
1045	return (s->method->internal->ssl_renegotiate(s));
1046}
1047
1048int
1049SSL_renegotiate_pending(SSL *s)
1050{
1051	/*
1052	 * Becomes true when negotiation is requested;
1053	 * false again once a handshake has finished.
1054	 */
1055	return (s->internal->renegotiate != 0);
1056}
1057
1058long
1059SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
1060{
1061	long	l;
1062
1063	switch (cmd) {
1064	case SSL_CTRL_GET_READ_AHEAD:
1065		return (s->internal->read_ahead);
1066	case SSL_CTRL_SET_READ_AHEAD:
1067		l = s->internal->read_ahead;
1068		s->internal->read_ahead = larg;
1069		return (l);
1070
1071	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1072		s->internal->msg_callback_arg = parg;
1073		return (1);
1074
1075	case SSL_CTRL_OPTIONS:
1076		return (s->internal->options|=larg);
1077	case SSL_CTRL_CLEAR_OPTIONS:
1078		return (s->internal->options&=~larg);
1079	case SSL_CTRL_MODE:
1080		return (s->internal->mode|=larg);
1081	case SSL_CTRL_CLEAR_MODE:
1082		return (s->internal->mode &=~larg);
1083	case SSL_CTRL_GET_MAX_CERT_LIST:
1084		return (s->internal->max_cert_list);
1085	case SSL_CTRL_SET_MAX_CERT_LIST:
1086		l = s->internal->max_cert_list;
1087		s->internal->max_cert_list = larg;
1088		return (l);
1089	case SSL_CTRL_SET_MTU:
1090#ifndef OPENSSL_NO_DTLS1
1091		if (larg < (long)dtls1_min_mtu())
1092			return (0);
1093#endif
1094		if (SSL_IS_DTLS(s)) {
1095			D1I(s)->mtu = larg;
1096			return (larg);
1097		}
1098		return (0);
1099	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1100		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1101			return (0);
1102		s->max_send_fragment = larg;
1103		return (1);
1104	case SSL_CTRL_GET_RI_SUPPORT:
1105		if (s->s3)
1106			return (S3I(s)->send_connection_binding);
1107		else return (0);
1108	default:
1109		if (SSL_IS_DTLS(s))
1110			return dtls1_ctrl(s, cmd, larg, parg);
1111		return ssl3_ctrl(s, cmd, larg, parg);
1112	}
1113}
1114
1115long
1116SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1117{
1118	switch (cmd) {
1119	case SSL_CTRL_SET_MSG_CALLBACK:
1120		s->internal->msg_callback = (void (*)(int write_p, int version,
1121		    int content_type, const void *buf, size_t len,
1122		    SSL *ssl, void *arg))(fp);
1123		return (1);
1124
1125	default:
1126		return (ssl3_callback_ctrl(s, cmd, fp));
1127	}
1128}
1129
1130struct lhash_st_SSL_SESSION *
1131SSL_CTX_sessions(SSL_CTX *ctx)
1132{
1133	return (ctx->internal->sessions);
1134}
1135
1136long
1137SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1138{
1139	long	l;
1140
1141	switch (cmd) {
1142	case SSL_CTRL_GET_READ_AHEAD:
1143		return (ctx->internal->read_ahead);
1144	case SSL_CTRL_SET_READ_AHEAD:
1145		l = ctx->internal->read_ahead;
1146		ctx->internal->read_ahead = larg;
1147		return (l);
1148
1149	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1150		ctx->internal->msg_callback_arg = parg;
1151		return (1);
1152
1153	case SSL_CTRL_GET_MAX_CERT_LIST:
1154		return (ctx->internal->max_cert_list);
1155	case SSL_CTRL_SET_MAX_CERT_LIST:
1156		l = ctx->internal->max_cert_list;
1157		ctx->internal->max_cert_list = larg;
1158		return (l);
1159
1160	case SSL_CTRL_SET_SESS_CACHE_SIZE:
1161		l = ctx->internal->session_cache_size;
1162		ctx->internal->session_cache_size = larg;
1163		return (l);
1164	case SSL_CTRL_GET_SESS_CACHE_SIZE:
1165		return (ctx->internal->session_cache_size);
1166	case SSL_CTRL_SET_SESS_CACHE_MODE:
1167		l = ctx->internal->session_cache_mode;
1168		ctx->internal->session_cache_mode = larg;
1169		return (l);
1170	case SSL_CTRL_GET_SESS_CACHE_MODE:
1171		return (ctx->internal->session_cache_mode);
1172
1173	case SSL_CTRL_SESS_NUMBER:
1174		return (lh_SSL_SESSION_num_items(ctx->internal->sessions));
1175	case SSL_CTRL_SESS_CONNECT:
1176		return (ctx->internal->stats.sess_connect);
1177	case SSL_CTRL_SESS_CONNECT_GOOD:
1178		return (ctx->internal->stats.sess_connect_good);
1179	case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1180		return (ctx->internal->stats.sess_connect_renegotiate);
1181	case SSL_CTRL_SESS_ACCEPT:
1182		return (ctx->internal->stats.sess_accept);
1183	case SSL_CTRL_SESS_ACCEPT_GOOD:
1184		return (ctx->internal->stats.sess_accept_good);
1185	case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1186		return (ctx->internal->stats.sess_accept_renegotiate);
1187	case SSL_CTRL_SESS_HIT:
1188		return (ctx->internal->stats.sess_hit);
1189	case SSL_CTRL_SESS_CB_HIT:
1190		return (ctx->internal->stats.sess_cb_hit);
1191	case SSL_CTRL_SESS_MISSES:
1192		return (ctx->internal->stats.sess_miss);
1193	case SSL_CTRL_SESS_TIMEOUTS:
1194		return (ctx->internal->stats.sess_timeout);
1195	case SSL_CTRL_SESS_CACHE_FULL:
1196		return (ctx->internal->stats.sess_cache_full);
1197	case SSL_CTRL_OPTIONS:
1198		return (ctx->internal->options|=larg);
1199	case SSL_CTRL_CLEAR_OPTIONS:
1200		return (ctx->internal->options&=~larg);
1201	case SSL_CTRL_MODE:
1202		return (ctx->internal->mode|=larg);
1203	case SSL_CTRL_CLEAR_MODE:
1204		return (ctx->internal->mode&=~larg);
1205	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1206		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1207			return (0);
1208		ctx->internal->max_send_fragment = larg;
1209		return (1);
1210	default:
1211		return (ssl3_ctx_ctrl(ctx, cmd, larg, parg));
1212	}
1213}
1214
1215long
1216SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1217{
1218	switch (cmd) {
1219	case SSL_CTRL_SET_MSG_CALLBACK:
1220		ctx->internal->msg_callback = (void (*)(int write_p, int version,
1221		    int content_type, const void *buf, size_t len, SSL *ssl,
1222		    void *arg))(fp);
1223		return (1);
1224
1225	default:
1226		return (ssl3_ctx_callback_ctrl(ctx, cmd, fp));
1227	}
1228}
1229
1230int
1231ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1232{
1233	long	l;
1234
1235	l = a->id - b->id;
1236	if (l == 0L)
1237		return (0);
1238	else
1239		return ((l > 0) ? 1:-1);
1240}
1241
1242STACK_OF(SSL_CIPHER) *
1243SSL_get_ciphers(const SSL *s)
1244{
1245	if (s == NULL)
1246		return (NULL);
1247	if (s->cipher_list != NULL)
1248		return (s->cipher_list);
1249
1250	return (s->ctx->cipher_list);
1251}
1252
1253STACK_OF(SSL_CIPHER) *
1254SSL_get_client_ciphers(const SSL *s)
1255{
1256	if (s == NULL || s->session == NULL || !s->server)
1257		return NULL;
1258	return s->session->ciphers;
1259}
1260
1261STACK_OF(SSL_CIPHER) *
1262SSL_get1_supported_ciphers(SSL *s)
1263{
1264	STACK_OF(SSL_CIPHER) *supported_ciphers = NULL, *ciphers;
1265	const SSL_CIPHER *cipher;
1266	uint16_t min_vers, max_vers;
1267	int i;
1268
1269	if (s == NULL)
1270		return NULL;
1271	if (!ssl_supported_version_range(s, &min_vers, &max_vers))
1272		return NULL;
1273	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1274		return NULL;
1275	if ((supported_ciphers = sk_SSL_CIPHER_new_null()) == NULL)
1276		return NULL;
1277
1278	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1279		if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL)
1280			goto err;
1281		if (!ssl_cipher_allowed_in_version_range(cipher, min_vers,
1282		    max_vers))
1283			continue;
1284		if (!sk_SSL_CIPHER_push(supported_ciphers, cipher))
1285			goto err;
1286	}
1287
1288	if (sk_SSL_CIPHER_num(supported_ciphers) > 0)
1289		return supported_ciphers;
1290
1291 err:
1292	sk_SSL_CIPHER_free(supported_ciphers);
1293	return NULL;
1294}
1295
1296/* See if we have any ECC cipher suites. */
1297int
1298ssl_has_ecc_ciphers(SSL *s)
1299{
1300	STACK_OF(SSL_CIPHER) *ciphers;
1301	unsigned long alg_k, alg_a;
1302	SSL_CIPHER *cipher;
1303	int i;
1304
1305	if (s->version == DTLS1_VERSION)
1306		return 0;
1307	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1308		return 0;
1309
1310	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1311		cipher = sk_SSL_CIPHER_value(ciphers, i);
1312
1313		alg_k = cipher->algorithm_mkey;
1314		alg_a = cipher->algorithm_auth;
1315
1316		if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1317			return 1;
1318	}
1319
1320	return 0;
1321}
1322
1323/* The old interface to get the same thing as SSL_get_ciphers(). */
1324const char *
1325SSL_get_cipher_list(const SSL *s, int n)
1326{
1327	STACK_OF(SSL_CIPHER) *ciphers;
1328	const SSL_CIPHER *cipher;
1329
1330	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1331		return (NULL);
1332	if ((cipher = sk_SSL_CIPHER_value(ciphers, n)) == NULL)
1333		return (NULL);
1334
1335	return (cipher->name);
1336}
1337
1338STACK_OF(SSL_CIPHER) *
1339SSL_CTX_get_ciphers(const SSL_CTX *ctx)
1340{
1341	return ctx->cipher_list;
1342}
1343
1344/* Specify the ciphers to be used by default by the SSL_CTX. */
1345int
1346SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1347{
1348	STACK_OF(SSL_CIPHER) *ciphers;
1349
1350	/*
1351	 * ssl_create_cipher_list may return an empty stack if it was unable to
1352	 * find a cipher matching the given rule string (for example if the
1353	 * rule string specifies a cipher which has been disabled). This is not
1354	 * an error as far as ssl_create_cipher_list is concerned, and hence
1355	 * ctx->cipher_list has been updated.
1356	 */
1357	ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1358	    ctx->internal->cipher_list_tls13, str);
1359	if (ciphers == NULL) {
1360		return (0);
1361	} else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1362		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1363		return (0);
1364	}
1365	return (1);
1366}
1367
1368int
1369SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1370{
1371	if (!ssl_parse_ciphersuites(&ctx->internal->cipher_list_tls13, str)) {
1372		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1373		return 0;
1374	}
1375	if (!ssl_merge_cipherlists(ctx->cipher_list,
1376	    ctx->internal->cipher_list_tls13, &ctx->cipher_list))
1377		return 0;
1378
1379	return 1;
1380}
1381
1382/* Specify the ciphers to be used by the SSL. */
1383int
1384SSL_set_cipher_list(SSL *s, const char *str)
1385{
1386	STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13;
1387
1388	if ((ciphers_tls13 = s->internal->cipher_list_tls13) == NULL)
1389		ciphers_tls13 = s->ctx->internal->cipher_list_tls13;
1390
1391	/* See comment in SSL_CTX_set_cipher_list. */
1392	ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1393	    ciphers_tls13, str);
1394	if (ciphers == NULL) {
1395		return (0);
1396	} else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1397		SSLerror(s, SSL_R_NO_CIPHER_MATCH);
1398		return (0);
1399	}
1400	return (1);
1401}
1402
1403int
1404SSL_set_ciphersuites(SSL *s, const char *str)
1405{
1406	STACK_OF(SSL_CIPHER) *ciphers;
1407
1408	if ((ciphers = s->cipher_list) == NULL)
1409		ciphers = s->ctx->cipher_list;
1410
1411	if (!ssl_parse_ciphersuites(&s->internal->cipher_list_tls13, str)) {
1412		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1413		return (0);
1414	}
1415	if (!ssl_merge_cipherlists(ciphers, s->internal->cipher_list_tls13,
1416	    &s->cipher_list))
1417		return 0;
1418
1419	return 1;
1420}
1421
1422char *
1423SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1424{
1425	STACK_OF(SSL_CIPHER) *ciphers;
1426	const SSL_CIPHER *cipher;
1427	size_t curlen = 0;
1428	char *end;
1429	int i;
1430
1431	if (s->session == NULL || s->session->ciphers == NULL || len < 2)
1432		return (NULL);
1433
1434	ciphers = s->session->ciphers;
1435	if (sk_SSL_CIPHER_num(ciphers) == 0)
1436		return (NULL);
1437
1438	buf[0] = '\0';
1439	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1440		cipher = sk_SSL_CIPHER_value(ciphers, i);
1441		end = buf + curlen;
1442		if (strlcat(buf, cipher->name, len) >= len ||
1443		    (curlen = strlcat(buf, ":", len)) >= len) {
1444			/* remove truncated cipher from list */
1445			*end = '\0';
1446			break;
1447		}
1448	}
1449	/* remove trailing colon */
1450	if ((end = strrchr(buf, ':')) != NULL)
1451		*end = '\0';
1452	return (buf);
1453}
1454
1455/*
1456 * Return a servername extension value if provided in Client Hello, or NULL.
1457 * So far, only host_name types are defined (RFC 3546).
1458 */
1459const char *
1460SSL_get_servername(const SSL *s, const int type)
1461{
1462	if (type != TLSEXT_NAMETYPE_host_name)
1463		return (NULL);
1464
1465	return (s->session && !s->tlsext_hostname ?
1466	    s->session->tlsext_hostname :
1467	    s->tlsext_hostname);
1468}
1469
1470int
1471SSL_get_servername_type(const SSL *s)
1472{
1473	if (s->session &&
1474	    (!s->tlsext_hostname ?
1475	    s->session->tlsext_hostname : s->tlsext_hostname))
1476		return (TLSEXT_NAMETYPE_host_name);
1477	return (-1);
1478}
1479
1480/*
1481 * SSL_select_next_proto implements standard protocol selection. It is
1482 * expected that this function is called from the callback set by
1483 * SSL_CTX_set_alpn_select_cb.
1484 *
1485 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1486 * strings. The length byte itself is not included in the length. A byte
1487 * string of length 0 is invalid. No byte string may be truncated.
1488 *
1489 * It returns either:
1490 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1491 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1492 */
1493int
1494SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1495    const unsigned char *server, unsigned int server_len,
1496    const unsigned char *client, unsigned int client_len)
1497{
1498	unsigned int		 i, j;
1499	const unsigned char	*result;
1500	int			 status = OPENSSL_NPN_UNSUPPORTED;
1501
1502	/*
1503	 * For each protocol in server preference order,
1504	 * see if we support it.
1505	 */
1506	for (i = 0; i < server_len; ) {
1507		for (j = 0; j < client_len; ) {
1508			if (server[i] == client[j] &&
1509			    memcmp(&server[i + 1],
1510			    &client[j + 1], server[i]) == 0) {
1511				/* We found a match */
1512				result = &server[i];
1513				status = OPENSSL_NPN_NEGOTIATED;
1514				goto found;
1515			}
1516			j += client[j];
1517			j++;
1518		}
1519		i += server[i];
1520		i++;
1521	}
1522
1523	/* There's no overlap between our protocols and the server's list. */
1524	result = client;
1525	status = OPENSSL_NPN_NO_OVERLAP;
1526
1527found:
1528	*out = (unsigned char *) result + 1;
1529	*outlen = result[0];
1530	return (status);
1531}
1532
1533/* SSL_get0_next_proto_negotiated is deprecated. */
1534void
1535SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1536    unsigned int *len)
1537{
1538	*data = NULL;
1539	*len = 0;
1540}
1541
1542/* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1543void
1544SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl,
1545    const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1546{
1547}
1548
1549/* SSL_CTX_set_next_proto_select_cb is deprecated. */
1550void
1551SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s,
1552    unsigned char **out, unsigned char *outlen, const unsigned char *in,
1553    unsigned int inlen, void *arg), void *arg)
1554{
1555}
1556
1557/*
1558 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1559 * protocols, which must be in wire-format (i.e. a series of non-empty,
1560 * 8-bit length-prefixed strings). Returns 0 on success.
1561 */
1562int
1563SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
1564    unsigned int protos_len)
1565{
1566	int failed = 1;
1567
1568	if (protos == NULL || protos_len == 0)
1569		goto err;
1570
1571	free(ctx->internal->alpn_client_proto_list);
1572	ctx->internal->alpn_client_proto_list = NULL;
1573	ctx->internal->alpn_client_proto_list_len = 0;
1574
1575	if ((ctx->internal->alpn_client_proto_list = malloc(protos_len))
1576	    == NULL)
1577		goto err;
1578	ctx->internal->alpn_client_proto_list_len = protos_len;
1579
1580	memcpy(ctx->internal->alpn_client_proto_list, protos, protos_len);
1581
1582	failed = 0;
1583
1584 err:
1585	/* NOTE: Return values are the reverse of what you expect. */
1586	return (failed);
1587}
1588
1589/*
1590 * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1591 * protocols, which must be in wire-format (i.e. a series of non-empty,
1592 * 8-bit length-prefixed strings). Returns 0 on success.
1593 */
1594int
1595SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
1596    unsigned int protos_len)
1597{
1598	int failed = 1;
1599
1600	if (protos == NULL || protos_len == 0)
1601		goto err;
1602
1603	free(ssl->internal->alpn_client_proto_list);
1604	ssl->internal->alpn_client_proto_list = NULL;
1605	ssl->internal->alpn_client_proto_list_len = 0;
1606
1607	if ((ssl->internal->alpn_client_proto_list = malloc(protos_len))
1608	    == NULL)
1609		goto err;
1610	ssl->internal->alpn_client_proto_list_len = protos_len;
1611
1612	memcpy(ssl->internal->alpn_client_proto_list, protos, protos_len);
1613
1614	failed = 0;
1615
1616 err:
1617	/* NOTE: Return values are the reverse of what you expect. */
1618	return (failed);
1619}
1620
1621/*
1622 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1623 * ClientHello processing in order to select an ALPN protocol from the
1624 * client's list of offered protocols.
1625 */
1626void
1627SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1628    int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen,
1629    const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1630{
1631	ctx->internal->alpn_select_cb = cb;
1632	ctx->internal->alpn_select_cb_arg = arg;
1633}
1634
1635/*
1636 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1637 * it sets data to point to len bytes of protocol name (not including the
1638 * leading length-prefix byte). If the server didn't respond with* a negotiated
1639 * protocol then len will be zero.
1640 */
1641void
1642SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1643    unsigned int *len)
1644{
1645	*data = NULL;
1646	*len = 0;
1647
1648	*data = ssl->s3->internal->alpn_selected;
1649	*len = ssl->s3->internal->alpn_selected_len;
1650}
1651
1652int
1653SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1654    const char *label, size_t llen, const unsigned char *p, size_t plen,
1655    int use_context)
1656{
1657	return (tls1_export_keying_material(s, out, olen,
1658	    label, llen, p, plen, use_context));
1659}
1660
1661static unsigned long
1662ssl_session_hash(const SSL_SESSION *a)
1663{
1664	unsigned long	l;
1665
1666	l = (unsigned long)
1667	    ((unsigned int) a->session_id[0]     )|
1668	    ((unsigned int) a->session_id[1]<< 8L)|
1669	    ((unsigned long)a->session_id[2]<<16L)|
1670	    ((unsigned long)a->session_id[3]<<24L);
1671	return (l);
1672}
1673
1674/*
1675 * NB: If this function (or indeed the hash function which uses a sort of
1676 * coarser function than this one) is changed, ensure
1677 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1678 * able to construct an SSL_SESSION that will collide with any existing session
1679 * with a matching session ID.
1680 */
1681static int
1682ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1683{
1684	if (a->ssl_version != b->ssl_version)
1685		return (1);
1686	if (a->session_id_length != b->session_id_length)
1687		return (1);
1688	if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0)
1689		return (1);
1690	return (0);
1691}
1692
1693/*
1694 * These wrapper functions should remain rather than redeclaring
1695 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1696 * variable. The reason is that the functions aren't static, they're exposed via
1697 * ssl.h.
1698 */
1699static unsigned long
1700ssl_session_LHASH_HASH(const void *arg)
1701{
1702	const SSL_SESSION *a = arg;
1703
1704	return ssl_session_hash(a);
1705}
1706
1707static int
1708ssl_session_LHASH_COMP(const void *arg1, const void *arg2)
1709{
1710	const SSL_SESSION *a = arg1;
1711	const SSL_SESSION *b = arg2;
1712
1713	return ssl_session_cmp(a, b);
1714}
1715
1716SSL_CTX *
1717SSL_CTX_new(const SSL_METHOD *meth)
1718{
1719	SSL_CTX	*ret;
1720
1721	if (!OPENSSL_init_ssl(0, NULL)) {
1722		SSLerrorx(SSL_R_LIBRARY_BUG);
1723		return (NULL);
1724	}
1725
1726	if (meth == NULL) {
1727		SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED);
1728		return (NULL);
1729	}
1730
1731	if ((ret = calloc(1, sizeof(*ret))) == NULL) {
1732		SSLerrorx(ERR_R_MALLOC_FAILURE);
1733		return (NULL);
1734	}
1735	if ((ret->internal = calloc(1, sizeof(*ret->internal))) == NULL) {
1736		free(ret);
1737		SSLerrorx(ERR_R_MALLOC_FAILURE);
1738		return (NULL);
1739	}
1740
1741	if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1742		SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1743		goto err;
1744	}
1745
1746	ret->method = meth;
1747	ret->internal->min_version = meth->internal->min_version;
1748	ret->internal->max_version = meth->internal->max_version;
1749	ret->internal->mode = SSL_MODE_AUTO_RETRY;
1750
1751	ret->cert_store = NULL;
1752	ret->internal->session_cache_mode = SSL_SESS_CACHE_SERVER;
1753	ret->internal->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1754	ret->internal->session_cache_head = NULL;
1755	ret->internal->session_cache_tail = NULL;
1756
1757	/* We take the system default */
1758	ret->session_timeout = ssl_get_default_timeout();
1759
1760	ret->internal->new_session_cb = 0;
1761	ret->internal->remove_session_cb = 0;
1762	ret->internal->get_session_cb = 0;
1763	ret->internal->generate_session_id = 0;
1764
1765	memset((char *)&ret->internal->stats, 0, sizeof(ret->internal->stats));
1766
1767	ret->references = 1;
1768	ret->internal->quiet_shutdown = 0;
1769
1770	ret->internal->info_callback = NULL;
1771
1772	ret->internal->app_verify_callback = 0;
1773	ret->internal->app_verify_arg = NULL;
1774
1775	ret->internal->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1776	ret->internal->read_ahead = 0;
1777	ret->internal->msg_callback = 0;
1778	ret->internal->msg_callback_arg = NULL;
1779	ret->verify_mode = SSL_VERIFY_NONE;
1780	ret->sid_ctx_length = 0;
1781	ret->internal->default_verify_callback = NULL;
1782
1783	if ((ret->internal->cert = ssl_cert_new()) == NULL)
1784		goto err;
1785
1786	ret->default_passwd_callback = 0;
1787	ret->default_passwd_callback_userdata = NULL;
1788	ret->internal->client_cert_cb = 0;
1789	ret->internal->app_gen_cookie_cb = 0;
1790	ret->internal->app_verify_cookie_cb = 0;
1791
1792	ret->internal->sessions = lh_SSL_SESSION_new();
1793	if (ret->internal->sessions == NULL)
1794		goto err;
1795	ret->cert_store = X509_STORE_new();
1796	if (ret->cert_store == NULL)
1797		goto err;
1798
1799	ssl_create_cipher_list(ret->method, &ret->cipher_list,
1800	    NULL, SSL_DEFAULT_CIPHER_LIST);
1801	if (ret->cipher_list == NULL ||
1802	    sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
1803		SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS);
1804		goto err2;
1805	}
1806
1807	ret->param = X509_VERIFY_PARAM_new();
1808	if (!ret->param)
1809		goto err;
1810
1811	if ((ret->internal->client_CA = sk_X509_NAME_new_null()) == NULL)
1812		goto err;
1813
1814	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->internal->ex_data);
1815
1816	ret->extra_certs = NULL;
1817
1818	ret->internal->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1819
1820	ret->internal->tlsext_servername_callback = 0;
1821	ret->internal->tlsext_servername_arg = NULL;
1822
1823	/* Setup RFC4507 ticket keys */
1824	arc4random_buf(ret->internal->tlsext_tick_key_name, 16);
1825	arc4random_buf(ret->internal->tlsext_tick_hmac_key, 16);
1826	arc4random_buf(ret->internal->tlsext_tick_aes_key, 16);
1827
1828	ret->internal->tlsext_status_cb = 0;
1829	ret->internal->tlsext_status_arg = NULL;
1830
1831#ifndef OPENSSL_NO_ENGINE
1832	ret->internal->client_cert_engine = NULL;
1833#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
1834#define eng_strx(x)	#x
1835#define eng_str(x)	eng_strx(x)
1836	/* Use specific client engine automatically... ignore errors */
1837	{
1838		ENGINE *eng;
1839		eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1840		if (!eng) {
1841			ERR_clear_error();
1842			ENGINE_load_builtin_engines();
1843			eng = ENGINE_by_id(eng_str(
1844			    OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1845		}
1846		if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
1847			ERR_clear_error();
1848	}
1849#endif
1850#endif
1851	/*
1852	 * Default is to connect to non-RI servers. When RI is more widely
1853	 * deployed might change this.
1854	 */
1855	ret->internal->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1856
1857	return (ret);
1858err:
1859	SSLerrorx(ERR_R_MALLOC_FAILURE);
1860err2:
1861	SSL_CTX_free(ret);
1862	return (NULL);
1863}
1864
1865void
1866SSL_CTX_free(SSL_CTX *ctx)
1867{
1868	int	i;
1869
1870	if (ctx == NULL)
1871		return;
1872
1873	i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX);
1874	if (i > 0)
1875		return;
1876
1877	X509_VERIFY_PARAM_free(ctx->param);
1878
1879	/*
1880	 * Free internal session cache. However: the remove_cb() may reference
1881	 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1882	 * after the sessions were flushed.
1883	 * As the ex_data handling routines might also touch the session cache,
1884	 * the most secure solution seems to be: empty (flush) the cache, then
1885	 * free ex_data, then finally free the cache.
1886	 * (See ticket [openssl.org #212].)
1887	 */
1888	if (ctx->internal->sessions != NULL)
1889		SSL_CTX_flush_sessions(ctx, 0);
1890
1891	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->internal->ex_data);
1892
1893	lh_SSL_SESSION_free(ctx->internal->sessions);
1894
1895	X509_STORE_free(ctx->cert_store);
1896	sk_SSL_CIPHER_free(ctx->cipher_list);
1897	sk_SSL_CIPHER_free(ctx->internal->cipher_list_tls13);
1898	ssl_cert_free(ctx->internal->cert);
1899	sk_X509_NAME_pop_free(ctx->internal->client_CA, X509_NAME_free);
1900	sk_X509_pop_free(ctx->extra_certs, X509_free);
1901
1902#ifndef OPENSSL_NO_SRTP
1903	if (ctx->internal->srtp_profiles)
1904		sk_SRTP_PROTECTION_PROFILE_free(ctx->internal->srtp_profiles);
1905#endif
1906
1907#ifndef OPENSSL_NO_ENGINE
1908	ENGINE_finish(ctx->internal->client_cert_engine);
1909#endif
1910
1911	free(ctx->internal->tlsext_ecpointformatlist);
1912	free(ctx->internal->tlsext_supportedgroups);
1913
1914	free(ctx->internal->alpn_client_proto_list);
1915
1916	free(ctx->internal);
1917	free(ctx);
1918}
1919
1920int
1921SSL_CTX_up_ref(SSL_CTX *ctx)
1922{
1923	int refs = CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
1924	return ((refs > 1) ? 1 : 0);
1925}
1926
1927pem_password_cb *
1928SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
1929{
1930	return (ctx->default_passwd_callback);
1931}
1932
1933void
1934SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
1935{
1936	ctx->default_passwd_callback = cb;
1937}
1938
1939void *
1940SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
1941{
1942	return ctx->default_passwd_callback_userdata;
1943}
1944
1945void
1946SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
1947{
1948	ctx->default_passwd_callback_userdata = u;
1949}
1950
1951void
1952SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1953    int (*cb)(X509_STORE_CTX *, void *), void *arg)
1954{
1955	ctx->internal->app_verify_callback = cb;
1956	ctx->internal->app_verify_arg = arg;
1957}
1958
1959void
1960SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *))
1961{
1962	ctx->verify_mode = mode;
1963	ctx->internal->default_verify_callback = cb;
1964}
1965
1966void
1967SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
1968{
1969	X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1970}
1971
1972static int
1973ssl_cert_can_sign(X509 *x)
1974{
1975	/* This call populates extension flags (ex_flags). */
1976	X509_check_purpose(x, -1, 0);
1977
1978	/* Key usage, if present, must allow signing. */
1979	return ((x->ex_flags & EXFLAG_KUSAGE) == 0 ||
1980	    (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE));
1981}
1982
1983void
1984ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
1985{
1986	unsigned long mask_a, mask_k;
1987	CERT_PKEY *cpk;
1988
1989	if (c == NULL)
1990		return;
1991
1992	mask_a = SSL_aNULL | SSL_aTLS1_3;
1993	mask_k = SSL_kECDHE | SSL_kTLS1_3;
1994
1995	if (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto != 0)
1996		mask_k |= SSL_kDHE;
1997
1998	cpk = &(c->pkeys[SSL_PKEY_ECC]);
1999	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2000		if (ssl_cert_can_sign(cpk->x509))
2001			mask_a |= SSL_aECDSA;
2002	}
2003
2004	cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2005	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2006		mask_k |= SSL_kGOST;
2007		mask_a |= SSL_aGOST01;
2008	}
2009
2010	cpk = &(c->pkeys[SSL_PKEY_RSA]);
2011	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2012		mask_a |= SSL_aRSA;
2013		mask_k |= SSL_kRSA;
2014	}
2015
2016	c->mask_k = mask_k;
2017	c->mask_a = mask_a;
2018	c->valid = 1;
2019}
2020
2021/* See if this handshake is using an ECC cipher suite. */
2022int
2023ssl_using_ecc_cipher(SSL *s)
2024{
2025	unsigned long alg_a, alg_k;
2026
2027	alg_a = S3I(s)->hs.new_cipher->algorithm_auth;
2028	alg_k = S3I(s)->hs.new_cipher->algorithm_mkey;
2029
2030	return SSI(s)->tlsext_ecpointformatlist != NULL &&
2031	    SSI(s)->tlsext_ecpointformatlist_length > 0 &&
2032	    ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2033}
2034
2035int
2036ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2037{
2038	const SSL_CIPHER	*cs = S3I(s)->hs.new_cipher;
2039	unsigned long		 alg_a;
2040
2041	alg_a = cs->algorithm_auth;
2042
2043	if (alg_a & SSL_aECDSA) {
2044		/* This call populates extension flags (ex_flags). */
2045		X509_check_purpose(x, -1, 0);
2046
2047		/* Key usage, if present, must allow signing. */
2048		if ((x->ex_flags & EXFLAG_KUSAGE) &&
2049		    ((x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) == 0)) {
2050			SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2051			return (0);
2052		}
2053	}
2054
2055	return (1);
2056}
2057
2058CERT_PKEY *
2059ssl_get_server_send_pkey(const SSL *s)
2060{
2061	unsigned long	 alg_a;
2062	CERT		*c;
2063	int		 i;
2064
2065	c = s->cert;
2066	ssl_set_cert_masks(c, S3I(s)->hs.new_cipher);
2067
2068	alg_a = S3I(s)->hs.new_cipher->algorithm_auth;
2069
2070	if (alg_a & SSL_aECDSA) {
2071		i = SSL_PKEY_ECC;
2072	} else if (alg_a & SSL_aRSA) {
2073		i = SSL_PKEY_RSA;
2074	} else if (alg_a & SSL_aGOST01) {
2075		i = SSL_PKEY_GOST01;
2076	} else { /* if (alg_a & SSL_aNULL) */
2077		SSLerror(s, ERR_R_INTERNAL_ERROR);
2078		return (NULL);
2079	}
2080
2081	return (c->pkeys + i);
2082}
2083
2084EVP_PKEY *
2085ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd,
2086    const struct ssl_sigalg **sap)
2087{
2088	const struct ssl_sigalg *sigalg = NULL;
2089	EVP_PKEY *pkey = NULL;
2090	unsigned long	 alg_a;
2091	CERT		*c;
2092	int		 idx = -1;
2093
2094	alg_a = cipher->algorithm_auth;
2095	c = s->cert;
2096
2097	if (alg_a & SSL_aRSA) {
2098		idx = SSL_PKEY_RSA;
2099	} else if ((alg_a & SSL_aECDSA) &&
2100	    (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2101		idx = SSL_PKEY_ECC;
2102	if (idx == -1) {
2103		SSLerror(s, ERR_R_INTERNAL_ERROR);
2104		return (NULL);
2105	}
2106
2107	pkey = c->pkeys[idx].privatekey;
2108	if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) {
2109		SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2110		return (NULL);
2111	}
2112	*pmd = sigalg->md();
2113	*sap = sigalg;
2114
2115	return (pkey);
2116}
2117
2118DH *
2119ssl_get_auto_dh(SSL *s)
2120{
2121	CERT_PKEY *cpk;
2122	int keylen;
2123	DH *dhp;
2124
2125	if (s->cert->dh_tmp_auto == 2) {
2126		keylen = 1024;
2127	} else if (S3I(s)->hs.new_cipher->algorithm_auth & SSL_aNULL) {
2128		keylen = 1024;
2129		if (S3I(s)->hs.new_cipher->strength_bits == 256)
2130			keylen = 3072;
2131	} else {
2132		if ((cpk = ssl_get_server_send_pkey(s)) == NULL)
2133			return (NULL);
2134		if (cpk->privatekey == NULL || cpk->privatekey->pkey.dh == NULL)
2135			return (NULL);
2136		keylen = EVP_PKEY_bits(cpk->privatekey);
2137	}
2138
2139	if ((dhp = DH_new()) == NULL)
2140		return (NULL);
2141
2142	dhp->g = BN_new();
2143	if (dhp->g != NULL)
2144		BN_set_word(dhp->g, 2);
2145
2146	if (keylen >= 8192)
2147		dhp->p = get_rfc3526_prime_8192(NULL);
2148	else if (keylen >= 4096)
2149		dhp->p = get_rfc3526_prime_4096(NULL);
2150	else if (keylen >= 3072)
2151		dhp->p = get_rfc3526_prime_3072(NULL);
2152	else if (keylen >= 2048)
2153		dhp->p = get_rfc3526_prime_2048(NULL);
2154	else if (keylen >= 1536)
2155		dhp->p = get_rfc3526_prime_1536(NULL);
2156	else
2157		dhp->p = get_rfc2409_prime_1024(NULL);
2158
2159	if (dhp->p == NULL || dhp->g == NULL) {
2160		DH_free(dhp);
2161		return (NULL);
2162	}
2163	return (dhp);
2164}
2165
2166void
2167ssl_update_cache(SSL *s, int mode)
2168{
2169	int	i;
2170
2171	/*
2172	 * If the session_id_length is 0, we are not supposed to cache it,
2173	 * and it would be rather hard to do anyway :-)
2174	 */
2175	if (s->session->session_id_length == 0)
2176		return;
2177
2178	i = s->session_ctx->internal->session_cache_mode;
2179	if ((i & mode) && (!s->internal->hit) && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2180	    || SSL_CTX_add_session(s->session_ctx, s->session))
2181	    && (s->session_ctx->internal->new_session_cb != NULL)) {
2182		CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2183		if (!s->session_ctx->internal->new_session_cb(s, s->session))
2184			SSL_SESSION_free(s->session);
2185	}
2186
2187	/* auto flush every 255 connections */
2188	if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2189	    ((i & mode) == mode)) {
2190		if ((((mode & SSL_SESS_CACHE_CLIENT) ?
2191		    s->session_ctx->internal->stats.sess_connect_good :
2192		    s->session_ctx->internal->stats.sess_accept_good) & 0xff) == 0xff) {
2193			SSL_CTX_flush_sessions(s->session_ctx, time(NULL));
2194		}
2195	}
2196}
2197
2198const SSL_METHOD *
2199SSL_get_ssl_method(SSL *s)
2200{
2201	return (s->method);
2202}
2203
2204int
2205SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2206{
2207	int	conn = -1;
2208	int	ret = 1;
2209
2210	if (s->method != meth) {
2211		if (s->internal->handshake_func != NULL)
2212			conn = (s->internal->handshake_func == s->method->internal->ssl_connect);
2213
2214		if (s->method->internal->version == meth->internal->version)
2215			s->method = meth;
2216		else {
2217			s->method->internal->ssl_free(s);
2218			s->method = meth;
2219			ret = s->method->internal->ssl_new(s);
2220		}
2221
2222		if (conn == 1)
2223			s->internal->handshake_func = meth->internal->ssl_connect;
2224		else if (conn == 0)
2225			s->internal->handshake_func = meth->internal->ssl_accept;
2226	}
2227	return (ret);
2228}
2229
2230int
2231SSL_get_error(const SSL *s, int i)
2232{
2233	int		 reason;
2234	unsigned long	 l;
2235	BIO		*bio;
2236
2237	if (i > 0)
2238		return (SSL_ERROR_NONE);
2239
2240	/* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2241	 * etc, where we do encode the error */
2242	if ((l = ERR_peek_error()) != 0) {
2243		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2244			return (SSL_ERROR_SYSCALL);
2245		else
2246			return (SSL_ERROR_SSL);
2247	}
2248
2249	if ((i < 0) && SSL_want_read(s)) {
2250		bio = SSL_get_rbio(s);
2251		if (BIO_should_read(bio)) {
2252			return (SSL_ERROR_WANT_READ);
2253		} else if (BIO_should_write(bio)) {
2254			/*
2255			 * This one doesn't make too much sense...  We never
2256			 * try to write to the rbio, and an application
2257			 * program where rbio and wbio are separate couldn't
2258			 * even know what it should wait for.  However if we
2259			 * ever set s->internal->rwstate incorrectly (so that we have
2260			 * SSL_want_read(s) instead of SSL_want_write(s))
2261			 * and rbio and wbio *are* the same, this test works
2262			 * around that bug; so it might be safer to keep it.
2263			 */
2264			return (SSL_ERROR_WANT_WRITE);
2265		} else if (BIO_should_io_special(bio)) {
2266			reason = BIO_get_retry_reason(bio);
2267			if (reason == BIO_RR_CONNECT)
2268				return (SSL_ERROR_WANT_CONNECT);
2269			else if (reason == BIO_RR_ACCEPT)
2270				return (SSL_ERROR_WANT_ACCEPT);
2271			else
2272				return (SSL_ERROR_SYSCALL); /* unknown */
2273		}
2274	}
2275
2276	if ((i < 0) && SSL_want_write(s)) {
2277		bio = SSL_get_wbio(s);
2278		if (BIO_should_write(bio)) {
2279			return (SSL_ERROR_WANT_WRITE);
2280		} else if (BIO_should_read(bio)) {
2281			/*
2282			 * See above (SSL_want_read(s) with
2283			 * BIO_should_write(bio))
2284			 */
2285			return (SSL_ERROR_WANT_READ);
2286		} else if (BIO_should_io_special(bio)) {
2287			reason = BIO_get_retry_reason(bio);
2288			if (reason == BIO_RR_CONNECT)
2289				return (SSL_ERROR_WANT_CONNECT);
2290			else if (reason == BIO_RR_ACCEPT)
2291				return (SSL_ERROR_WANT_ACCEPT);
2292			else
2293				return (SSL_ERROR_SYSCALL);
2294		}
2295	}
2296	if ((i < 0) && SSL_want_x509_lookup(s)) {
2297		return (SSL_ERROR_WANT_X509_LOOKUP);
2298	}
2299
2300	if (i == 0) {
2301		if ((s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2302		    (S3I(s)->warn_alert == SSL_AD_CLOSE_NOTIFY))
2303		return (SSL_ERROR_ZERO_RETURN);
2304	}
2305	return (SSL_ERROR_SYSCALL);
2306}
2307
2308int
2309SSL_do_handshake(SSL *s)
2310{
2311	int	ret = 1;
2312
2313	if (s->internal->handshake_func == NULL) {
2314		SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET);
2315		return (-1);
2316	}
2317
2318	s->method->internal->ssl_renegotiate_check(s);
2319
2320	if (SSL_in_init(s) || SSL_in_before(s)) {
2321		ret = s->internal->handshake_func(s);
2322	}
2323	return (ret);
2324}
2325
2326/*
2327 * For the next 2 functions, SSL_clear() sets shutdown and so
2328 * one of these calls will reset it
2329 */
2330void
2331SSL_set_accept_state(SSL *s)
2332{
2333	s->server = 1;
2334	s->internal->shutdown = 0;
2335	S3I(s)->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE;
2336	s->internal->handshake_func = s->method->internal->ssl_accept;
2337	ssl_clear_cipher_state(s);
2338}
2339
2340void
2341SSL_set_connect_state(SSL *s)
2342{
2343	s->server = 0;
2344	s->internal->shutdown = 0;
2345	S3I(s)->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE;
2346	s->internal->handshake_func = s->method->internal->ssl_connect;
2347	ssl_clear_cipher_state(s);
2348}
2349
2350int
2351ssl_undefined_function(SSL *s)
2352{
2353	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2354	return (0);
2355}
2356
2357int
2358ssl_undefined_void_function(void)
2359{
2360	SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2361	return (0);
2362}
2363
2364int
2365ssl_undefined_const_function(const SSL *s)
2366{
2367	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2368	return (0);
2369}
2370
2371const char *
2372ssl_version_string(int ver)
2373{
2374	switch (ver) {
2375	case DTLS1_VERSION:
2376		return (SSL_TXT_DTLS1);
2377	case TLS1_VERSION:
2378		return (SSL_TXT_TLSV1);
2379	case TLS1_1_VERSION:
2380		return (SSL_TXT_TLSV1_1);
2381	case TLS1_2_VERSION:
2382		return (SSL_TXT_TLSV1_2);
2383	case TLS1_3_VERSION:
2384		return (SSL_TXT_TLSV1_3);
2385	default:
2386		return ("unknown");
2387	}
2388}
2389
2390const char *
2391SSL_get_version(const SSL *s)
2392{
2393	return ssl_version_string(s->version);
2394}
2395
2396SSL *
2397SSL_dup(SSL *s)
2398{
2399	STACK_OF(X509_NAME) *sk;
2400	X509_NAME *xn;
2401	SSL *ret;
2402	int i;
2403
2404	if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2405		goto err;
2406
2407	ret->version = s->version;
2408	ret->internal->type = s->internal->type;
2409	ret->method = s->method;
2410
2411	if (s->session != NULL) {
2412		if (!SSL_copy_session_id(ret, s))
2413			goto err;
2414	} else {
2415		/*
2416		 * No session has been established yet, so we have to expect
2417		 * that s->cert or ret->cert will be changed later --
2418		 * they should not both point to the same object,
2419		 * and thus we can't use SSL_copy_session_id.
2420		 */
2421
2422		ret->method->internal->ssl_free(ret);
2423		ret->method = s->method;
2424		ret->method->internal->ssl_new(ret);
2425
2426		ssl_cert_free(ret->cert);
2427		if ((ret->cert = ssl_cert_dup(s->cert)) == NULL)
2428			goto err;
2429
2430		if (!SSL_set_session_id_context(ret, s->sid_ctx,
2431		    s->sid_ctx_length))
2432			goto err;
2433	}
2434
2435	ret->internal->options = s->internal->options;
2436	ret->internal->mode = s->internal->mode;
2437	SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2438	SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2439	ret->internal->msg_callback = s->internal->msg_callback;
2440	ret->internal->msg_callback_arg = s->internal->msg_callback_arg;
2441	SSL_set_verify(ret, SSL_get_verify_mode(s),
2442	SSL_get_verify_callback(s));
2443	SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2444	ret->internal->generate_session_id = s->internal->generate_session_id;
2445
2446	SSL_set_info_callback(ret, SSL_get_info_callback(s));
2447
2448	ret->internal->debug = s->internal->debug;
2449
2450	/* copy app data, a little dangerous perhaps */
2451	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL,
2452	    &ret->internal->ex_data, &s->internal->ex_data))
2453		goto err;
2454
2455	/* setup rbio, and wbio */
2456	if (s->rbio != NULL) {
2457		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2458			goto err;
2459	}
2460	if (s->wbio != NULL) {
2461		if (s->wbio != s->rbio) {
2462			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2463				goto err;
2464		} else
2465			ret->wbio = ret->rbio;
2466	}
2467	ret->internal->rwstate = s->internal->rwstate;
2468	ret->internal->in_handshake = s->internal->in_handshake;
2469	ret->internal->handshake_func = s->internal->handshake_func;
2470	ret->server = s->server;
2471	ret->internal->renegotiate = s->internal->renegotiate;
2472	ret->internal->new_session = s->internal->new_session;
2473	ret->internal->quiet_shutdown = s->internal->quiet_shutdown;
2474	ret->internal->shutdown = s->internal->shutdown;
2475	/* SSL_dup does not really work at any state, though */
2476	S3I(ret)->hs.state = S3I(s)->hs.state;
2477	ret->internal->rstate = s->internal->rstate;
2478
2479	/*
2480	 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2481	 * ret->init_off
2482	 */
2483	ret->internal->init_num = 0;
2484
2485	ret->internal->hit = s->internal->hit;
2486
2487	X509_VERIFY_PARAM_inherit(ret->param, s->param);
2488
2489	if (s->cipher_list != NULL) {
2490		if ((ret->cipher_list =
2491		    sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2492			goto err;
2493	}
2494	if (s->internal->cipher_list_tls13 != NULL) {
2495		if ((ret->internal->cipher_list_tls13 =
2496		    sk_SSL_CIPHER_dup(s->internal->cipher_list_tls13)) == NULL)
2497			goto err;
2498	}
2499
2500	/* Dup the client_CA list */
2501	if (s->internal->client_CA != NULL) {
2502		if ((sk = sk_X509_NAME_dup(s->internal->client_CA)) == NULL) goto err;
2503			ret->internal->client_CA = sk;
2504		for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2505			xn = sk_X509_NAME_value(sk, i);
2506			if (sk_X509_NAME_set(sk, i,
2507			    X509_NAME_dup(xn)) == NULL) {
2508				X509_NAME_free(xn);
2509				goto err;
2510			}
2511		}
2512	}
2513
2514	return ret;
2515 err:
2516	SSL_free(ret);
2517	return NULL;
2518}
2519
2520void
2521ssl_clear_cipher_state(SSL *s)
2522{
2523	ssl_clear_cipher_read_state(s);
2524	ssl_clear_cipher_write_state(s);
2525}
2526
2527void
2528ssl_clear_cipher_read_state(SSL *s)
2529{
2530	EVP_CIPHER_CTX_free(s->enc_read_ctx);
2531	s->enc_read_ctx = NULL;
2532	EVP_MD_CTX_free(s->read_hash);
2533	s->read_hash = NULL;
2534
2535	tls12_record_layer_clear_read_state(s->internal->rl);
2536	tls12_record_layer_set_read_seq_num(s->internal->rl,
2537	    S3I(s)->read_sequence);
2538
2539	if (s->internal->aead_read_ctx != NULL) {
2540		EVP_AEAD_CTX_cleanup(&s->internal->aead_read_ctx->ctx);
2541		free(s->internal->aead_read_ctx);
2542		s->internal->aead_read_ctx = NULL;
2543	}
2544}
2545
2546void
2547ssl_clear_cipher_write_state(SSL *s)
2548{
2549	EVP_CIPHER_CTX_free(s->internal->enc_write_ctx);
2550	s->internal->enc_write_ctx = NULL;
2551	EVP_MD_CTX_free(s->internal->write_hash);
2552	s->internal->write_hash = NULL;
2553
2554	tls12_record_layer_clear_write_state(s->internal->rl);
2555	tls12_record_layer_set_write_seq_num(s->internal->rl,
2556	    S3I(s)->write_sequence);
2557
2558	if (s->internal->aead_write_ctx != NULL) {
2559		EVP_AEAD_CTX_cleanup(&s->internal->aead_write_ctx->ctx);
2560		free(s->internal->aead_write_ctx);
2561		s->internal->aead_write_ctx = NULL;
2562	}
2563}
2564
2565/* Fix this function so that it takes an optional type parameter */
2566X509 *
2567SSL_get_certificate(const SSL *s)
2568{
2569	return (s->cert->key->x509);
2570}
2571
2572/* Fix this function so that it takes an optional type parameter */
2573EVP_PKEY *
2574SSL_get_privatekey(const SSL *s)
2575{
2576	return (s->cert->key->privatekey);
2577}
2578
2579const SSL_CIPHER *
2580SSL_get_current_cipher(const SSL *s)
2581{
2582	if ((s->session != NULL) && (s->session->cipher != NULL))
2583		return (s->session->cipher);
2584	return (NULL);
2585}
2586const void *
2587SSL_get_current_compression(SSL *s)
2588{
2589	return (NULL);
2590}
2591
2592const void *
2593SSL_get_current_expansion(SSL *s)
2594{
2595	return (NULL);
2596}
2597
2598size_t
2599SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out)
2600{
2601	size_t len = sizeof(s->s3->client_random);
2602
2603	if (out == NULL)
2604		return len;
2605
2606	if (len > max_out)
2607		len = max_out;
2608
2609	memcpy(out, s->s3->client_random, len);
2610
2611	return len;
2612}
2613
2614size_t
2615SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out)
2616{
2617	size_t len = sizeof(s->s3->server_random);
2618
2619	if (out == NULL)
2620		return len;
2621
2622	if (len > max_out)
2623		len = max_out;
2624
2625	memcpy(out, s->s3->server_random, len);
2626
2627	return len;
2628}
2629
2630int
2631ssl_init_wbio_buffer(SSL *s, int push)
2632{
2633	BIO	*bbio;
2634
2635	if (s->bbio == NULL) {
2636		bbio = BIO_new(BIO_f_buffer());
2637		if (bbio == NULL)
2638			return (0);
2639		s->bbio = bbio;
2640	} else {
2641		bbio = s->bbio;
2642		if (s->bbio == s->wbio)
2643			s->wbio = BIO_pop(s->wbio);
2644	}
2645	(void)BIO_reset(bbio);
2646/*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2647	if (!BIO_set_read_buffer_size(bbio, 1)) {
2648		SSLerror(s, ERR_R_BUF_LIB);
2649		return (0);
2650	}
2651	if (push) {
2652		if (s->wbio != bbio)
2653			s->wbio = BIO_push(bbio, s->wbio);
2654	} else {
2655		if (s->wbio == bbio)
2656			s->wbio = BIO_pop(bbio);
2657	}
2658	return (1);
2659}
2660
2661void
2662ssl_free_wbio_buffer(SSL *s)
2663{
2664	if (s == NULL)
2665		return;
2666
2667	if (s->bbio == NULL)
2668		return;
2669
2670	if (s->bbio == s->wbio) {
2671		/* remove buffering */
2672		s->wbio = BIO_pop(s->wbio);
2673	}
2674	BIO_free(s->bbio);
2675	s->bbio = NULL;
2676}
2677
2678void
2679SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
2680{
2681	ctx->internal->quiet_shutdown = mode;
2682}
2683
2684int
2685SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2686{
2687	return (ctx->internal->quiet_shutdown);
2688}
2689
2690void
2691SSL_set_quiet_shutdown(SSL *s, int mode)
2692{
2693	s->internal->quiet_shutdown = mode;
2694}
2695
2696int
2697SSL_get_quiet_shutdown(const SSL *s)
2698{
2699	return (s->internal->quiet_shutdown);
2700}
2701
2702void
2703SSL_set_shutdown(SSL *s, int mode)
2704{
2705	s->internal->shutdown = mode;
2706}
2707
2708int
2709SSL_get_shutdown(const SSL *s)
2710{
2711	return (s->internal->shutdown);
2712}
2713
2714int
2715SSL_version(const SSL *s)
2716{
2717	return (s->version);
2718}
2719
2720SSL_CTX *
2721SSL_get_SSL_CTX(const SSL *ssl)
2722{
2723	return (ssl->ctx);
2724}
2725
2726SSL_CTX *
2727SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2728{
2729	if (ssl->ctx == ctx)
2730		return (ssl->ctx);
2731	if (ctx == NULL)
2732		ctx = ssl->initial_ctx;
2733
2734	ssl_cert_free(ssl->cert);
2735	ssl->cert = ssl_cert_dup(ctx->internal->cert);
2736
2737	CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2738	SSL_CTX_free(ssl->ctx); /* decrement reference count */
2739	ssl->ctx = ctx;
2740	return (ssl->ctx);
2741}
2742
2743int
2744SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2745{
2746	return (X509_STORE_set_default_paths(ctx->cert_store));
2747}
2748
2749int
2750SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2751    const char *CApath)
2752{
2753	return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
2754}
2755
2756int
2757SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
2758{
2759	return (X509_STORE_load_mem(ctx->cert_store, buf, len));
2760}
2761
2762void
2763SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val))
2764{
2765	ssl->internal->info_callback = cb;
2766}
2767
2768void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val)
2769{
2770	return (ssl->internal->info_callback);
2771}
2772
2773int
2774SSL_state(const SSL *ssl)
2775{
2776	return (S3I(ssl)->hs.state);
2777}
2778
2779void
2780SSL_set_state(SSL *ssl, int state)
2781{
2782	S3I(ssl)->hs.state = state;
2783}
2784
2785void
2786SSL_set_verify_result(SSL *ssl, long arg)
2787{
2788	ssl->verify_result = arg;
2789}
2790
2791long
2792SSL_get_verify_result(const SSL *ssl)
2793{
2794	return (ssl->verify_result);
2795}
2796
2797int
2798SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2799    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
2800{
2801	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2802	    new_func, dup_func, free_func));
2803}
2804
2805int
2806SSL_set_ex_data(SSL *s, int idx, void *arg)
2807{
2808	return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
2809}
2810
2811void *
2812SSL_get_ex_data(const SSL *s, int idx)
2813{
2814	return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
2815}
2816
2817int
2818SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2819    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
2820{
2821	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2822	    new_func, dup_func, free_func));
2823}
2824
2825int
2826SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
2827{
2828	return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
2829}
2830
2831void *
2832SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
2833{
2834	return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
2835}
2836
2837int
2838ssl_ok(SSL *s)
2839{
2840	return (1);
2841}
2842
2843X509_STORE *
2844SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2845{
2846	return (ctx->cert_store);
2847}
2848
2849void
2850SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
2851{
2852	X509_STORE_free(ctx->cert_store);
2853	ctx->cert_store = store;
2854}
2855
2856X509 *
2857SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2858{
2859	if (ctx->internal->cert == NULL)
2860		return NULL;
2861
2862	return ctx->internal->cert->key->x509;
2863}
2864
2865int
2866SSL_want(const SSL *s)
2867{
2868	return (s->internal->rwstate);
2869}
2870
2871void
2872SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export,
2873    int keylength))
2874{
2875	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2876}
2877
2878void
2879SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2880    int keylength))
2881{
2882	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2883}
2884
2885void
2886SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export,
2887    int keylength))
2888{
2889	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2890}
2891
2892void
2893SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2894    int keylength))
2895{
2896	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2897}
2898
2899void
2900SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl,
2901    int is_export, int keylength))
2902{
2903	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
2904	    (void (*)(void))ecdh);
2905}
2906
2907void
2908SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2909    int keylength))
2910{
2911	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2912}
2913
2914
2915void
2916SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version,
2917    int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2918{
2919	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK,
2920	    (void (*)(void))cb);
2921}
2922
2923void
2924SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version,
2925    int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2926{
2927	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2928}
2929
2930void
2931SSL_set_debug(SSL *s, int debug)
2932{
2933	s->internal->debug = debug;
2934}
2935
2936int
2937SSL_cache_hit(SSL *s)
2938{
2939	return (s->internal->hit);
2940}
2941
2942int
2943SSL_CTX_get_min_proto_version(SSL_CTX *ctx)
2944{
2945	return ctx->internal->min_version;
2946}
2947
2948int
2949SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
2950{
2951	return ssl_version_set_min(ctx->method, version,
2952	    ctx->internal->max_version, &ctx->internal->min_version);
2953}
2954
2955int
2956SSL_CTX_get_max_proto_version(SSL_CTX *ctx)
2957{
2958	return ctx->internal->max_version;
2959}
2960
2961int
2962SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
2963{
2964	return ssl_version_set_max(ctx->method, version,
2965	    ctx->internal->min_version, &ctx->internal->max_version);
2966}
2967
2968int
2969SSL_get_min_proto_version(SSL *ssl)
2970{
2971	return ssl->internal->min_version;
2972}
2973
2974int
2975SSL_set_min_proto_version(SSL *ssl, uint16_t version)
2976{
2977	return ssl_version_set_min(ssl->method, version,
2978	    ssl->internal->max_version, &ssl->internal->min_version);
2979}
2980int
2981SSL_get_max_proto_version(SSL *ssl)
2982{
2983	return ssl->internal->max_version;
2984}
2985
2986int
2987SSL_set_max_proto_version(SSL *ssl, uint16_t version)
2988{
2989	return ssl_version_set_max(ssl->method, version,
2990	    ssl->internal->min_version, &ssl->internal->max_version);
2991}
2992
2993static int
2994ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
2995{
2996	SSL_CIPHER const *a = a_;
2997	SSL_CIPHER const *b = b_;
2998	return ssl_cipher_id_cmp(a, b);
2999}
3000
3001SSL_CIPHER *
3002OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base, int num)
3003{
3004	return (SSL_CIPHER *)OBJ_bsearch_(key, base, num, sizeof(SSL_CIPHER),
3005	    ssl_cipher_id_cmp_BSEARCH_CMP_FN);
3006}
3007