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