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