1 /* $OpenBSD: ssl_lib.c,v 1.305 2022/09/10 15:29:33 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
116 /* ====================================================================
117 * Copyright 2005 Nokia. All rights reserved.
119 * The portions of the attached software ("Contribution") is developed by
120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
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.
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.
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.
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
143 #include <arpa/inet.h>
144 #include <sys/socket.h>
145 #include <netinet/in.h>
150 #include <openssl/dh.h>
151 #include <openssl/lhash.h>
152 #include <openssl/objects.h>
153 #include <openssl/ocsp.h>
154 #include <openssl/opensslconf.h>
155 #include <openssl/x509v3.h>
157 #ifndef OPENSSL_NO_ENGINE
158 #include <openssl/engine.h>
161 #include "bytestring.h"
162 #include "dtls_locl.h"
163 #include "ssl_locl.h"
164 #include "ssl_sigalgs.h"
165 #include "ssl_tlsext.h"
167 const char *SSL_version_str
= OPENSSL_VERSION_TEXT
;
172 if (s
->method
== NULL
) {
173 SSLerror(s
, SSL_R_NO_METHOD_SPECIFIED
);
177 if (ssl_clear_bad_session(s
)) {
178 SSL_SESSION_free(s
->session
);
183 s
->internal
->hit
= 0;
184 s
->internal
->shutdown
= 0;
186 if (s
->internal
->renegotiate
) {
187 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
191 s
->version
= s
->method
->version
;
192 s
->client_version
= s
->version
;
193 s
->internal
->rwstate
= SSL_NOTHING
;
194 s
->internal
->rstate
= SSL_ST_READ_HEADER
;
196 tls13_ctx_free(s
->internal
->tls13
);
197 s
->internal
->tls13
= NULL
;
199 ssl3_release_init_buffer(s
);
201 ssl_clear_cipher_state(s
);
203 s
->internal
->first_packet
= 0;
206 * Check to see if we were changed into a different method, if
207 * so, revert back if we are not doing session-id reuse.
209 if (!s
->internal
->in_handshake
&& (s
->session
== NULL
) &&
210 (s
->method
!= s
->ctx
->method
)) {
211 s
->method
->ssl_free(s
);
212 s
->method
= s
->ctx
->method
;
213 if (!s
->method
->ssl_new(s
))
216 s
->method
->ssl_clear(s
);
221 /* Used to change an SSL_CTXs default SSL method type */
223 SSL_CTX_set_ssl_version(SSL_CTX
*ctx
, const SSL_METHOD
*meth
)
225 STACK_OF(SSL_CIPHER
) *ciphers
;
229 ciphers
= ssl_create_cipher_list(ctx
->method
, &ctx
->cipher_list
,
230 ctx
->internal
->cipher_list_tls13
, SSL_DEFAULT_CIPHER_LIST
,
231 ctx
->internal
->cert
);
232 if (ciphers
== NULL
|| sk_SSL_CIPHER_num(ciphers
) <= 0) {
233 SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
240 SSL_new(SSL_CTX
*ctx
)
246 SSLerrorx(SSL_R_NULL_SSL_CTX
);
249 if (ctx
->method
== NULL
) {
250 SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION
);
254 if ((s
= calloc(1, sizeof(*s
))) == NULL
)
256 if ((s
->internal
= calloc(1, sizeof(*s
->internal
))) == NULL
)
259 if ((s
->internal
->rl
= tls12_record_layer_new()) == NULL
)
262 s
->internal
->min_tls_version
= ctx
->internal
->min_tls_version
;
263 s
->internal
->max_tls_version
= ctx
->internal
->max_tls_version
;
264 s
->internal
->min_proto_version
= ctx
->internal
->min_proto_version
;
265 s
->internal
->max_proto_version
= ctx
->internal
->max_proto_version
;
267 s
->internal
->options
= ctx
->internal
->options
;
268 s
->internal
->mode
= ctx
->internal
->mode
;
269 s
->internal
->max_cert_list
= ctx
->internal
->max_cert_list
;
270 s
->internal
->num_tickets
= ctx
->internal
->num_tickets
;
272 if ((s
->cert
= ssl_cert_dup(ctx
->internal
->cert
)) == NULL
)
275 s
->internal
->read_ahead
= ctx
->internal
->read_ahead
;
276 s
->internal
->msg_callback
= ctx
->internal
->msg_callback
;
277 s
->internal
->msg_callback_arg
= ctx
->internal
->msg_callback_arg
;
278 s
->verify_mode
= ctx
->verify_mode
;
279 s
->sid_ctx_length
= ctx
->sid_ctx_length
;
280 OPENSSL_assert(s
->sid_ctx_length
<= sizeof s
->sid_ctx
);
281 memcpy(&s
->sid_ctx
, &ctx
->sid_ctx
, sizeof(s
->sid_ctx
));
282 s
->internal
->verify_callback
= ctx
->internal
->default_verify_callback
;
283 s
->internal
->generate_session_id
= ctx
->internal
->generate_session_id
;
285 s
->param
= X509_VERIFY_PARAM_new();
288 X509_VERIFY_PARAM_inherit(s
->param
, ctx
->param
);
289 s
->internal
->quiet_shutdown
= ctx
->internal
->quiet_shutdown
;
290 s
->max_send_fragment
= ctx
->internal
->max_send_fragment
;
292 CRYPTO_add(&ctx
->references
, 1, CRYPTO_LOCK_SSL_CTX
);
294 s
->internal
->tlsext_debug_cb
= 0;
295 s
->internal
->tlsext_debug_arg
= NULL
;
296 s
->internal
->tlsext_ticket_expected
= 0;
297 s
->tlsext_status_type
= -1;
298 s
->internal
->tlsext_status_expected
= 0;
299 s
->internal
->tlsext_ocsp_ids
= NULL
;
300 s
->internal
->tlsext_ocsp_exts
= NULL
;
301 s
->internal
->tlsext_ocsp_resp
= NULL
;
302 s
->internal
->tlsext_ocsp_resp_len
= 0;
303 CRYPTO_add(&ctx
->references
, 1, CRYPTO_LOCK_SSL_CTX
);
304 s
->initial_ctx
= ctx
;
306 if (ctx
->internal
->tlsext_ecpointformatlist
!= NULL
) {
307 s
->internal
->tlsext_ecpointformatlist
=
308 calloc(ctx
->internal
->tlsext_ecpointformatlist_length
,
309 sizeof(ctx
->internal
->tlsext_ecpointformatlist
[0]));
310 if (s
->internal
->tlsext_ecpointformatlist
== NULL
)
312 memcpy(s
->internal
->tlsext_ecpointformatlist
,
313 ctx
->internal
->tlsext_ecpointformatlist
,
314 ctx
->internal
->tlsext_ecpointformatlist_length
*
315 sizeof(ctx
->internal
->tlsext_ecpointformatlist
[0]));
316 s
->internal
->tlsext_ecpointformatlist_length
=
317 ctx
->internal
->tlsext_ecpointformatlist_length
;
319 if (ctx
->internal
->tlsext_supportedgroups
!= NULL
) {
320 s
->internal
->tlsext_supportedgroups
=
321 calloc(ctx
->internal
->tlsext_supportedgroups_length
,
322 sizeof(ctx
->internal
->tlsext_supportedgroups
[0]));
323 if (s
->internal
->tlsext_supportedgroups
== NULL
)
325 memcpy(s
->internal
->tlsext_supportedgroups
,
326 ctx
->internal
->tlsext_supportedgroups
,
327 ctx
->internal
->tlsext_supportedgroups_length
*
328 sizeof(ctx
->internal
->tlsext_supportedgroups
[0]));
329 s
->internal
->tlsext_supportedgroups_length
=
330 ctx
->internal
->tlsext_supportedgroups_length
;
333 CBS_init(&cbs
, ctx
->internal
->alpn_client_proto_list
,
334 ctx
->internal
->alpn_client_proto_list_len
);
335 if (!CBS_stow(&cbs
, &s
->internal
->alpn_client_proto_list
,
336 &s
->internal
->alpn_client_proto_list_len
))
339 s
->verify_result
= X509_V_OK
;
341 s
->method
= ctx
->method
;
342 s
->quic_method
= ctx
->quic_method
;
344 if (!s
->method
->ssl_new(s
))
348 s
->server
= ctx
->method
->server
;
352 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->internal
->ex_data
);
358 SSLerrorx(ERR_R_MALLOC_FAILURE
);
363 SSL_CTX_set_session_id_context(SSL_CTX
*ctx
, const unsigned char *sid_ctx
,
364 unsigned int sid_ctx_len
)
366 if (sid_ctx_len
> sizeof ctx
->sid_ctx
) {
367 SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
370 ctx
->sid_ctx_length
= sid_ctx_len
;
371 memcpy(ctx
->sid_ctx
, sid_ctx
, sid_ctx_len
);
377 SSL_set_session_id_context(SSL
*ssl
, const unsigned char *sid_ctx
,
378 unsigned int sid_ctx_len
)
380 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
381 SSLerror(ssl
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
384 ssl
->sid_ctx_length
= sid_ctx_len
;
385 memcpy(ssl
->sid_ctx
, sid_ctx
, sid_ctx_len
);
391 SSL_CTX_set_generate_session_id(SSL_CTX
*ctx
, GEN_SESSION_CB cb
)
393 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX
);
394 ctx
->internal
->generate_session_id
= cb
;
395 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX
);
400 SSL_set_generate_session_id(SSL
*ssl
, GEN_SESSION_CB cb
)
402 CRYPTO_w_lock(CRYPTO_LOCK_SSL
);
403 ssl
->internal
->generate_session_id
= cb
;
404 CRYPTO_w_unlock(CRYPTO_LOCK_SSL
);
409 SSL_has_matching_session_id(const SSL
*ssl
, const unsigned char *id
,
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.
421 if (id_len
> sizeof r
.session_id
)
424 r
.ssl_version
= ssl
->version
;
425 r
.session_id_length
= id_len
;
426 memcpy(r
.session_id
, id
, id_len
);
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
);
435 SSL_CTX_set_purpose(SSL_CTX
*s
, int purpose
)
437 return (X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
));
441 SSL_set_purpose(SSL
*s
, int purpose
)
443 return (X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
));
447 SSL_CTX_set_trust(SSL_CTX
*s
, int trust
)
449 return (X509_VERIFY_PARAM_set_trust(s
->param
, trust
));
453 SSL_set_trust(SSL
*s
, int trust
)
455 return (X509_VERIFY_PARAM_set_trust(s
->param
, trust
));
459 SSL_set1_host(SSL
*s
, const char *hostname
)
462 struct in6_addr in6a
;
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
);
469 return X509_VERIFY_PARAM_set1_host(s
->param
, hostname
, 0);
473 SSL_set_hostflags(SSL
*s
, unsigned int flags
)
475 X509_VERIFY_PARAM_set_hostflags(s
->param
, flags
);
479 SSL_get0_peername(SSL
*s
)
481 return X509_VERIFY_PARAM_get0_peername(s
->param
);
485 SSL_CTX_get0_param(SSL_CTX
*ctx
)
491 SSL_CTX_set1_param(SSL_CTX
*ctx
, X509_VERIFY_PARAM
*vpm
)
493 return (X509_VERIFY_PARAM_set1(ctx
->param
, vpm
));
497 SSL_get0_param(SSL
*ssl
)
503 SSL_set1_param(SSL
*ssl
, X509_VERIFY_PARAM
*vpm
)
505 return (X509_VERIFY_PARAM_set1(ssl
->param
, vpm
));
516 i
= CRYPTO_add(&s
->references
, -1, CRYPTO_LOCK_SSL
);
520 X509_VERIFY_PARAM_free(s
->param
);
522 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->internal
->ex_data
);
524 if (s
->bbio
!= NULL
) {
525 /* If the buffering BIO is in place, pop it off */
526 if (s
->bbio
== s
->wbio
) {
527 s
->wbio
= BIO_pop(s
->wbio
);
533 if (s
->rbio
!= s
->wbio
)
534 BIO_free_all(s
->rbio
);
535 BIO_free_all(s
->wbio
);
537 tls13_ctx_free(s
->internal
->tls13
);
539 ssl3_release_init_buffer(s
);
541 sk_SSL_CIPHER_free(s
->cipher_list
);
542 sk_SSL_CIPHER_free(s
->internal
->cipher_list_tls13
);
544 /* Make the next call work :-) */
545 if (s
->session
!= NULL
) {
546 ssl_clear_bad_session(s
);
547 SSL_SESSION_free(s
->session
);
550 ssl_clear_cipher_state(s
);
552 ssl_cert_free(s
->cert
);
554 free(s
->tlsext_hostname
);
555 SSL_CTX_free(s
->initial_ctx
);
557 free(s
->internal
->tlsext_ecpointformatlist
);
558 free(s
->internal
->tlsext_supportedgroups
);
560 sk_X509_EXTENSION_pop_free(s
->internal
->tlsext_ocsp_exts
,
561 X509_EXTENSION_free
);
562 sk_OCSP_RESPID_pop_free(s
->internal
->tlsext_ocsp_ids
, OCSP_RESPID_free
);
563 free(s
->internal
->tlsext_ocsp_resp
);
565 sk_X509_NAME_pop_free(s
->internal
->client_CA
, X509_NAME_free
);
567 if (s
->method
!= NULL
)
568 s
->method
->ssl_free(s
);
570 SSL_CTX_free(s
->ctx
);
572 free(s
->internal
->alpn_client_proto_list
);
574 free(s
->internal
->quic_transport_params
);
576 #ifndef OPENSSL_NO_SRTP
577 sk_SRTP_PROTECTION_PROFILE_free(s
->internal
->srtp_profiles
);
580 tls12_record_layer_free(s
->internal
->rl
);
589 int refs
= CRYPTO_add(&s
->references
, 1, CRYPTO_LOCK_SSL
);
590 return (refs
> 1) ? 1 : 0;
594 SSL_set_bio(SSL
*s
, BIO
*rbio
, BIO
*wbio
)
596 /* If the output buffering BIO is still in place, remove it */
597 if (s
->bbio
!= NULL
) {
598 if (s
->wbio
== s
->bbio
) {
599 s
->wbio
= BIO_next(s
->wbio
);
600 BIO_set_next(s
->bbio
, NULL
);
604 if (s
->rbio
!= rbio
&& s
->rbio
!= s
->wbio
)
605 BIO_free_all(s
->rbio
);
607 BIO_free_all(s
->wbio
);
613 SSL_get_rbio(const SSL
*s
)
619 SSL_set0_rbio(SSL
*s
, BIO
*rbio
)
621 BIO_free_all(s
->rbio
);
626 SSL_get_wbio(const SSL
*s
)
632 SSL_get_fd(const SSL
*s
)
634 return (SSL_get_rfd(s
));
638 SSL_get_rfd(const SSL
*s
)
644 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
651 SSL_get_wfd(const SSL
*s
)
657 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
664 SSL_set_fd(SSL
*s
, int fd
)
669 bio
= BIO_new(BIO_s_socket());
672 SSLerror(s
, ERR_R_BUF_LIB
);
675 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
676 SSL_set_bio(s
, bio
, bio
);
683 SSL_set_wfd(SSL
*s
, int fd
)
688 if ((s
->rbio
== NULL
) || (BIO_method_type(s
->rbio
) != BIO_TYPE_SOCKET
)
689 || ((int)BIO_get_fd(s
->rbio
, NULL
) != fd
)) {
690 bio
= BIO_new(BIO_s_socket());
693 SSLerror(s
, ERR_R_BUF_LIB
);
696 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
697 SSL_set_bio(s
, SSL_get_rbio(s
), bio
);
699 SSL_set_bio(s
, SSL_get_rbio(s
), SSL_get_rbio(s
));
706 SSL_set_rfd(SSL
*s
, int fd
)
711 if ((s
->wbio
== NULL
) || (BIO_method_type(s
->wbio
) != BIO_TYPE_SOCKET
)
712 || ((int)BIO_get_fd(s
->wbio
, NULL
) != fd
)) {
713 bio
= BIO_new(BIO_s_socket());
716 SSLerror(s
, ERR_R_BUF_LIB
);
719 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
720 SSL_set_bio(s
, bio
, SSL_get_wbio(s
));
722 SSL_set_bio(s
, SSL_get_wbio(s
), SSL_get_wbio(s
));
729 /* return length of latest Finished message we sent, copy to 'buf' */
731 SSL_get_finished(const SSL
*s
, void *buf
, size_t count
)
735 ret
= s
->s3
->hs
.finished_len
;
738 memcpy(buf
, s
->s3
->hs
.finished
, count
);
742 /* return length of latest Finished message we expected, copy to 'buf' */
744 SSL_get_peer_finished(const SSL
*s
, void *buf
, size_t count
)
748 ret
= s
->s3
->hs
.peer_finished_len
;
751 memcpy(buf
, s
->s3
->hs
.peer_finished
, count
);
757 SSL_get_verify_mode(const SSL
*s
)
759 return (s
->verify_mode
);
763 SSL_get_verify_depth(const SSL
*s
)
765 return (X509_VERIFY_PARAM_get_depth(s
->param
));
769 (*SSL_get_verify_callback(const SSL
*s
))(int, X509_STORE_CTX
*)
771 return (s
->internal
->verify_callback
);
775 SSL_CTX_set_keylog_callback(SSL_CTX
*ctx
, SSL_CTX_keylog_cb_func cb
)
777 ctx
->internal
->keylog_callback
= cb
;
780 SSL_CTX_keylog_cb_func
781 SSL_CTX_get_keylog_callback(const SSL_CTX
*ctx
)
783 return (ctx
->internal
->keylog_callback
);
787 SSL_set_num_tickets(SSL
*s
, size_t num_tickets
)
789 s
->internal
->num_tickets
= num_tickets
;
795 SSL_get_num_tickets(const SSL
*s
)
797 return s
->internal
->num_tickets
;
801 SSL_CTX_set_num_tickets(SSL_CTX
*ctx
, size_t num_tickets
)
803 ctx
->internal
->num_tickets
= num_tickets
;
809 SSL_CTX_get_num_tickets(const SSL_CTX
*ctx
)
811 return ctx
->internal
->num_tickets
;
815 SSL_CTX_get_verify_mode(const SSL_CTX
*ctx
)
817 return (ctx
->verify_mode
);
821 SSL_CTX_get_verify_depth(const SSL_CTX
*ctx
)
823 return (X509_VERIFY_PARAM_get_depth(ctx
->param
));
827 (*SSL_CTX_get_verify_callback(const SSL_CTX
*ctx
))(int, X509_STORE_CTX
*)
829 return (ctx
->internal
->default_verify_callback
);
833 SSL_set_verify(SSL
*s
, int mode
,
834 int (*callback
)(int ok
, X509_STORE_CTX
*ctx
))
836 s
->verify_mode
= mode
;
837 if (callback
!= NULL
)
838 s
->internal
->verify_callback
= callback
;
842 SSL_set_verify_depth(SSL
*s
, int depth
)
844 X509_VERIFY_PARAM_set_depth(s
->param
, depth
);
848 SSL_set_read_ahead(SSL
*s
, int yes
)
850 s
->internal
->read_ahead
= yes
;
854 SSL_get_read_ahead(const SSL
*s
)
856 return (s
->internal
->read_ahead
);
860 SSL_pending(const SSL
*s
)
862 return (s
->method
->ssl_pending(s
));
866 SSL_get_peer_certificate(const SSL
*s
)
870 if (s
== NULL
|| s
->session
== NULL
)
873 if ((cert
= s
->session
->peer_cert
) == NULL
)
882 SSL_get_peer_cert_chain(const SSL
*s
)
888 * Achtung! Due to API inconsistency, a client includes the peer's leaf
889 * certificate in the peer certificate chain, while a server does not.
892 return s
->s3
->hs
.peer_certs
;
894 return s
->s3
->hs
.peer_certs_no_leaf
;
898 SSL_get0_verified_chain(const SSL
*s
)
900 return s
->internal
->verified_chain
;
904 * Now in theory, since the calling process own 't' it should be safe to
905 * modify. We need to be able to read f without being hassled
908 SSL_copy_session_id(SSL
*t
, const SSL
*f
)
912 /* Do we need to do SSL locking? */
913 if (!SSL_set_session(t
, SSL_get_session(f
)))
916 /* What if we are set up for one protocol but want to talk another? */
917 if (t
->method
!= f
->method
) {
918 t
->method
->ssl_free(t
);
919 t
->method
= f
->method
;
920 if (!t
->method
->ssl_new(t
))
925 if (f
->cert
!= NULL
) {
926 CRYPTO_add(&f
->cert
->references
, 1, CRYPTO_LOCK_SSL_CERT
);
932 if (!SSL_set_session_id_context(t
, f
->sid_ctx
, f
->sid_ctx_length
))
938 /* Fix this so it checks all the valid key/cert options */
940 SSL_CTX_check_private_key(const SSL_CTX
*ctx
)
942 if ((ctx
== NULL
) || (ctx
->internal
->cert
== NULL
) ||
943 (ctx
->internal
->cert
->key
->x509
== NULL
)) {
944 SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED
);
947 if (ctx
->internal
->cert
->key
->privatekey
== NULL
) {
948 SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
951 return (X509_check_private_key(ctx
->internal
->cert
->key
->x509
,
952 ctx
->internal
->cert
->key
->privatekey
));
955 /* Fix this function so that it takes an optional type parameter */
957 SSL_check_private_key(const SSL
*ssl
)
960 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
963 if (ssl
->cert
== NULL
) {
964 SSLerror(ssl
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
967 if (ssl
->cert
->key
->x509
== NULL
) {
968 SSLerror(ssl
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
971 if (ssl
->cert
->key
->privatekey
== NULL
) {
972 SSLerror(ssl
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
975 return (X509_check_private_key(ssl
->cert
->key
->x509
,
976 ssl
->cert
->key
->privatekey
));
982 if (s
->internal
->handshake_func
== NULL
)
983 SSL_set_accept_state(s
); /* Not properly initialized yet */
985 return (s
->method
->ssl_accept(s
));
991 if (s
->internal
->handshake_func
== NULL
)
992 SSL_set_connect_state(s
); /* Not properly initialized yet */
994 return (s
->method
->ssl_connect(s
));
998 SSL_is_dtls(const SSL
*s
)
1000 return s
->method
->dtls
;
1004 SSL_is_server(const SSL
*s
)
1010 ssl_get_default_timeout()
1013 * 2 hours, the 24 hours mentioned in the TLSv1 spec
1014 * is way too long for http, the cache would over fill.
1016 return (2 * 60 * 60);
1020 SSL_get_default_timeout(const SSL
*s
)
1022 return (ssl_get_default_timeout());
1026 SSL_read(SSL
*s
, void *buf
, int num
)
1029 SSLerror(s
, SSL_R_BAD_LENGTH
);
1033 if (SSL_is_quic(s
)) {
1034 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1038 if (s
->internal
->handshake_func
== NULL
) {
1039 SSLerror(s
, SSL_R_UNINITIALIZED
);
1043 if (s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
1044 s
->internal
->rwstate
= SSL_NOTHING
;
1047 return ssl3_read(s
, buf
, num
);
1051 SSL_read_ex(SSL
*s
, void *buf
, size_t num
, size_t *bytes_read
)
1055 /* We simply don't bother supporting enormous reads */
1056 if (num
> INT_MAX
) {
1057 SSLerror(s
, SSL_R_BAD_LENGTH
);
1061 ret
= SSL_read(s
, buf
, (int)num
);
1070 SSL_peek(SSL
*s
, void *buf
, int num
)
1073 SSLerror(s
, SSL_R_BAD_LENGTH
);
1077 if (SSL_is_quic(s
)) {
1078 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1082 if (s
->internal
->handshake_func
== NULL
) {
1083 SSLerror(s
, SSL_R_UNINITIALIZED
);
1087 if (s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
1090 return ssl3_peek(s
, buf
, num
);
1094 SSL_peek_ex(SSL
*s
, void *buf
, size_t num
, size_t *bytes_peeked
)
1098 /* We simply don't bother supporting enormous peeks */
1099 if (num
> INT_MAX
) {
1100 SSLerror(s
, SSL_R_BAD_LENGTH
);
1104 ret
= SSL_peek(s
, buf
, (int)num
);
1107 *bytes_peeked
= ret
;
1113 SSL_write(SSL
*s
, const void *buf
, int num
)
1116 SSLerror(s
, SSL_R_BAD_LENGTH
);
1120 if (SSL_is_quic(s
)) {
1121 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1125 if (s
->internal
->handshake_func
== NULL
) {
1126 SSLerror(s
, SSL_R_UNINITIALIZED
);
1130 if (s
->internal
->shutdown
& SSL_SENT_SHUTDOWN
) {
1131 s
->internal
->rwstate
= SSL_NOTHING
;
1132 SSLerror(s
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
1135 return ssl3_write(s
, buf
, num
);
1139 SSL_write_ex(SSL
*s
, const void *buf
, size_t num
, size_t *bytes_written
)
1143 /* We simply don't bother supporting enormous writes */
1144 if (num
> INT_MAX
) {
1145 SSLerror(s
, SSL_R_BAD_LENGTH
);
1150 /* This API is special */
1155 ret
= SSL_write(s
, buf
, (int)num
);
1158 *bytes_written
= ret
;
1164 SSL_CTX_get_max_early_data(const SSL_CTX
*ctx
)
1170 SSL_CTX_set_max_early_data(SSL_CTX
*ctx
, uint32_t max_early_data
)
1176 SSL_get_max_early_data(const SSL
*s
)
1182 SSL_set_max_early_data(SSL
*s
, uint32_t max_early_data
)
1188 SSL_get_early_data_status(const SSL
*s
)
1190 return SSL_EARLY_DATA_REJECTED
;
1194 SSL_read_early_data(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
1199 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1200 return SSL_READ_EARLY_DATA_ERROR
;
1203 return SSL_READ_EARLY_DATA_FINISH
;
1207 SSL_write_early_data(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
1210 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1215 SSL_shutdown(SSL
*s
)
1218 * Note that this function behaves differently from what one might
1219 * expect. Return values are 0 for no success (yet),
1220 * 1 for success; but calling it once is usually not enough,
1221 * even if blocking I/O is used (see ssl3_shutdown).
1224 if (s
->internal
->handshake_func
== NULL
) {
1225 SSLerror(s
, SSL_R_UNINITIALIZED
);
1229 if (s
!= NULL
&& !SSL_in_init(s
))
1230 return (s
->method
->ssl_shutdown(s
));
1236 SSL_renegotiate(SSL
*s
)
1238 if (s
->internal
->renegotiate
== 0)
1239 s
->internal
->renegotiate
= 1;
1241 s
->internal
->new_session
= 1;
1243 return (s
->method
->ssl_renegotiate(s
));
1247 SSL_renegotiate_abbreviated(SSL
*s
)
1249 if (s
->internal
->renegotiate
== 0)
1250 s
->internal
->renegotiate
= 1;
1252 s
->internal
->new_session
= 0;
1254 return (s
->method
->ssl_renegotiate(s
));
1258 SSL_renegotiate_pending(SSL
*s
)
1261 * Becomes true when negotiation is requested;
1262 * false again once a handshake has finished.
1264 return (s
->internal
->renegotiate
!= 0);
1268 SSL_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
1273 case SSL_CTRL_GET_READ_AHEAD
:
1274 return (s
->internal
->read_ahead
);
1275 case SSL_CTRL_SET_READ_AHEAD
:
1276 l
= s
->internal
->read_ahead
;
1277 s
->internal
->read_ahead
= larg
;
1280 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1281 s
->internal
->msg_callback_arg
= parg
;
1284 case SSL_CTRL_OPTIONS
:
1285 return (s
->internal
->options
|=larg
);
1286 case SSL_CTRL_CLEAR_OPTIONS
:
1287 return (s
->internal
->options
&=~larg
);
1289 return (s
->internal
->mode
|=larg
);
1290 case SSL_CTRL_CLEAR_MODE
:
1291 return (s
->internal
->mode
&=~larg
);
1292 case SSL_CTRL_GET_MAX_CERT_LIST
:
1293 return (s
->internal
->max_cert_list
);
1294 case SSL_CTRL_SET_MAX_CERT_LIST
:
1295 l
= s
->internal
->max_cert_list
;
1296 s
->internal
->max_cert_list
= larg
;
1298 case SSL_CTRL_SET_MTU
:
1299 #ifndef OPENSSL_NO_DTLS1
1300 if (larg
< (long)dtls1_min_mtu())
1303 if (SSL_is_dtls(s
)) {
1308 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1309 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
1311 s
->max_send_fragment
= larg
;
1313 case SSL_CTRL_GET_RI_SUPPORT
:
1315 return (s
->s3
->send_connection_binding
);
1319 return dtls1_ctrl(s
, cmd
, larg
, parg
);
1320 return ssl3_ctrl(s
, cmd
, larg
, parg
);
1325 SSL_callback_ctrl(SSL
*s
, int cmd
, void (*fp
)(void))
1328 case SSL_CTRL_SET_MSG_CALLBACK
:
1329 s
->internal
->msg_callback
= (ssl_msg_callback_fn
*)(fp
);
1333 return (ssl3_callback_ctrl(s
, cmd
, fp
));
1337 struct lhash_st_SSL_SESSION
*
1338 SSL_CTX_sessions(SSL_CTX
*ctx
)
1340 return (ctx
->internal
->sessions
);
1344 SSL_CTX_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
1349 case SSL_CTRL_GET_READ_AHEAD
:
1350 return (ctx
->internal
->read_ahead
);
1351 case SSL_CTRL_SET_READ_AHEAD
:
1352 l
= ctx
->internal
->read_ahead
;
1353 ctx
->internal
->read_ahead
= larg
;
1356 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1357 ctx
->internal
->msg_callback_arg
= parg
;
1360 case SSL_CTRL_GET_MAX_CERT_LIST
:
1361 return (ctx
->internal
->max_cert_list
);
1362 case SSL_CTRL_SET_MAX_CERT_LIST
:
1363 l
= ctx
->internal
->max_cert_list
;
1364 ctx
->internal
->max_cert_list
= larg
;
1367 case SSL_CTRL_SET_SESS_CACHE_SIZE
:
1368 l
= ctx
->internal
->session_cache_size
;
1369 ctx
->internal
->session_cache_size
= larg
;
1371 case SSL_CTRL_GET_SESS_CACHE_SIZE
:
1372 return (ctx
->internal
->session_cache_size
);
1373 case SSL_CTRL_SET_SESS_CACHE_MODE
:
1374 l
= ctx
->internal
->session_cache_mode
;
1375 ctx
->internal
->session_cache_mode
= larg
;
1377 case SSL_CTRL_GET_SESS_CACHE_MODE
:
1378 return (ctx
->internal
->session_cache_mode
);
1380 case SSL_CTRL_SESS_NUMBER
:
1381 return (lh_SSL_SESSION_num_items(ctx
->internal
->sessions
));
1382 case SSL_CTRL_SESS_CONNECT
:
1383 return (ctx
->internal
->stats
.sess_connect
);
1384 case SSL_CTRL_SESS_CONNECT_GOOD
:
1385 return (ctx
->internal
->stats
.sess_connect_good
);
1386 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE
:
1387 return (ctx
->internal
->stats
.sess_connect_renegotiate
);
1388 case SSL_CTRL_SESS_ACCEPT
:
1389 return (ctx
->internal
->stats
.sess_accept
);
1390 case SSL_CTRL_SESS_ACCEPT_GOOD
:
1391 return (ctx
->internal
->stats
.sess_accept_good
);
1392 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE
:
1393 return (ctx
->internal
->stats
.sess_accept_renegotiate
);
1394 case SSL_CTRL_SESS_HIT
:
1395 return (ctx
->internal
->stats
.sess_hit
);
1396 case SSL_CTRL_SESS_CB_HIT
:
1397 return (ctx
->internal
->stats
.sess_cb_hit
);
1398 case SSL_CTRL_SESS_MISSES
:
1399 return (ctx
->internal
->stats
.sess_miss
);
1400 case SSL_CTRL_SESS_TIMEOUTS
:
1401 return (ctx
->internal
->stats
.sess_timeout
);
1402 case SSL_CTRL_SESS_CACHE_FULL
:
1403 return (ctx
->internal
->stats
.sess_cache_full
);
1404 case SSL_CTRL_OPTIONS
:
1405 return (ctx
->internal
->options
|=larg
);
1406 case SSL_CTRL_CLEAR_OPTIONS
:
1407 return (ctx
->internal
->options
&=~larg
);
1409 return (ctx
->internal
->mode
|=larg
);
1410 case SSL_CTRL_CLEAR_MODE
:
1411 return (ctx
->internal
->mode
&=~larg
);
1412 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1413 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
1415 ctx
->internal
->max_send_fragment
= larg
;
1418 return (ssl3_ctx_ctrl(ctx
, cmd
, larg
, parg
));
1423 SSL_CTX_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
)(void))
1426 case SSL_CTRL_SET_MSG_CALLBACK
:
1427 ctx
->internal
->msg_callback
= (ssl_msg_callback_fn
*)fp
;
1431 return (ssl3_ctx_callback_ctrl(ctx
, cmd
, fp
));
1436 ssl_cipher_id_cmp(const SSL_CIPHER
*a
, const SSL_CIPHER
*b
)
1444 return ((l
> 0) ? 1:-1);
1447 STACK_OF(SSL_CIPHER
) *
1448 SSL_get_ciphers(const SSL
*s
)
1452 if (s
->cipher_list
!= NULL
)
1453 return (s
->cipher_list
);
1455 return (s
->ctx
->cipher_list
);
1458 STACK_OF(SSL_CIPHER
) *
1459 SSL_get_client_ciphers(const SSL
*s
)
1461 if (s
== NULL
|| s
->session
== NULL
|| !s
->server
)
1463 return s
->session
->ciphers
;
1466 STACK_OF(SSL_CIPHER
) *
1467 SSL_get1_supported_ciphers(SSL
*s
)
1469 STACK_OF(SSL_CIPHER
) *supported_ciphers
= NULL
, *ciphers
;
1471 uint16_t min_vers
, max_vers
;
1476 if (!ssl_supported_tls_version_range(s
, &min_vers
, &max_vers
))
1478 if ((ciphers
= SSL_get_ciphers(s
)) == NULL
)
1480 if ((supported_ciphers
= sk_SSL_CIPHER_new_null()) == NULL
)
1483 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1484 if ((cipher
= sk_SSL_CIPHER_value(ciphers
, i
)) == NULL
)
1486 if (!ssl_cipher_allowed_in_tls_version_range(cipher
, min_vers
,
1489 if (!ssl_security_supported_cipher(s
, cipher
))
1491 if (!sk_SSL_CIPHER_push(supported_ciphers
, cipher
))
1495 if (sk_SSL_CIPHER_num(supported_ciphers
) > 0)
1496 return supported_ciphers
;
1499 sk_SSL_CIPHER_free(supported_ciphers
);
1503 /* See if we have any ECC cipher suites. */
1505 ssl_has_ecc_ciphers(SSL
*s
)
1507 STACK_OF(SSL_CIPHER
) *ciphers
;
1508 unsigned long alg_k
, alg_a
;
1512 if ((ciphers
= SSL_get_ciphers(s
)) == NULL
)
1515 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1516 cipher
= sk_SSL_CIPHER_value(ciphers
, i
);
1518 alg_k
= cipher
->algorithm_mkey
;
1519 alg_a
= cipher
->algorithm_auth
;
1521 if ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
))
1528 /* The old interface to get the same thing as SSL_get_ciphers(). */
1530 SSL_get_cipher_list(const SSL
*s
, int n
)
1532 STACK_OF(SSL_CIPHER
) *ciphers
;
1533 const SSL_CIPHER
*cipher
;
1535 if ((ciphers
= SSL_get_ciphers(s
)) == NULL
)
1537 if ((cipher
= sk_SSL_CIPHER_value(ciphers
, n
)) == NULL
)
1540 return (cipher
->name
);
1543 STACK_OF(SSL_CIPHER
) *
1544 SSL_CTX_get_ciphers(const SSL_CTX
*ctx
)
1548 return ctx
->cipher_list
;
1551 /* Specify the ciphers to be used by default by the SSL_CTX. */
1553 SSL_CTX_set_cipher_list(SSL_CTX
*ctx
, const char *str
)
1555 STACK_OF(SSL_CIPHER
) *ciphers
;
1558 * ssl_create_cipher_list may return an empty stack if it was unable to
1559 * find a cipher matching the given rule string (for example if the
1560 * rule string specifies a cipher which has been disabled). This is not
1561 * an error as far as ssl_create_cipher_list is concerned, and hence
1562 * ctx->cipher_list has been updated.
1564 ciphers
= ssl_create_cipher_list(ctx
->method
, &ctx
->cipher_list
,
1565 ctx
->internal
->cipher_list_tls13
, str
, ctx
->internal
->cert
);
1566 if (ciphers
== NULL
) {
1568 } else if (sk_SSL_CIPHER_num(ciphers
) == 0) {
1569 SSLerrorx(SSL_R_NO_CIPHER_MATCH
);
1576 SSL_CTX_set_ciphersuites(SSL_CTX
*ctx
, const char *str
)
1578 if (!ssl_parse_ciphersuites(&ctx
->internal
->cipher_list_tls13
, str
)) {
1579 SSLerrorx(SSL_R_NO_CIPHER_MATCH
);
1582 if (!ssl_merge_cipherlists(ctx
->cipher_list
,
1583 ctx
->internal
->cipher_list_tls13
, &ctx
->cipher_list
))
1589 /* Specify the ciphers to be used by the SSL. */
1591 SSL_set_cipher_list(SSL
*s
, const char *str
)
1593 STACK_OF(SSL_CIPHER
) *ciphers
, *ciphers_tls13
;
1595 if ((ciphers_tls13
= s
->internal
->cipher_list_tls13
) == NULL
)
1596 ciphers_tls13
= s
->ctx
->internal
->cipher_list_tls13
;
1598 /* See comment in SSL_CTX_set_cipher_list. */
1599 ciphers
= ssl_create_cipher_list(s
->ctx
->method
, &s
->cipher_list
,
1600 ciphers_tls13
, str
, s
->cert
);
1601 if (ciphers
== NULL
) {
1603 } else if (sk_SSL_CIPHER_num(ciphers
) == 0) {
1604 SSLerror(s
, SSL_R_NO_CIPHER_MATCH
);
1611 SSL_set_ciphersuites(SSL
*s
, const char *str
)
1613 STACK_OF(SSL_CIPHER
) *ciphers
;
1615 if ((ciphers
= s
->cipher_list
) == NULL
)
1616 ciphers
= s
->ctx
->cipher_list
;
1618 if (!ssl_parse_ciphersuites(&s
->internal
->cipher_list_tls13
, str
)) {
1619 SSLerrorx(SSL_R_NO_CIPHER_MATCH
);
1622 if (!ssl_merge_cipherlists(ciphers
, s
->internal
->cipher_list_tls13
,
1630 SSL_get_shared_ciphers(const SSL
*s
, char *buf
, int len
)
1632 STACK_OF(SSL_CIPHER
) *client_ciphers
, *server_ciphers
;
1633 const SSL_CIPHER
*cipher
;
1638 if (!s
->server
|| s
->session
== NULL
|| len
< 2)
1641 if ((client_ciphers
= s
->session
->ciphers
) == NULL
)
1643 if ((server_ciphers
= SSL_get_ciphers(s
)) == NULL
)
1645 if (sk_SSL_CIPHER_num(client_ciphers
) == 0 ||
1646 sk_SSL_CIPHER_num(server_ciphers
) == 0)
1650 for (i
= 0; i
< sk_SSL_CIPHER_num(client_ciphers
); i
++) {
1651 cipher
= sk_SSL_CIPHER_value(client_ciphers
, i
);
1653 if (sk_SSL_CIPHER_find(server_ciphers
, cipher
) < 0)
1657 if (strlcat(buf
, cipher
->name
, len
) >= len
||
1658 (curlen
= strlcat(buf
, ":", len
)) >= len
) {
1659 /* remove truncated cipher from list */
1664 /* remove trailing colon */
1665 if ((end
= strrchr(buf
, ':')) != NULL
)
1671 * Return a servername extension value if provided in Client Hello, or NULL.
1672 * So far, only host_name types are defined (RFC 3546).
1675 SSL_get_servername(const SSL
*s
, const int type
)
1677 if (type
!= TLSEXT_NAMETYPE_host_name
)
1680 return (s
->session
&& !s
->tlsext_hostname
?
1681 s
->session
->tlsext_hostname
:
1682 s
->tlsext_hostname
);
1686 SSL_get_servername_type(const SSL
*s
)
1689 (!s
->tlsext_hostname
?
1690 s
->session
->tlsext_hostname
: s
->tlsext_hostname
))
1691 return (TLSEXT_NAMETYPE_host_name
);
1696 * SSL_select_next_proto implements standard protocol selection. It is
1697 * expected that this function is called from the callback set by
1698 * SSL_CTX_set_alpn_select_cb.
1700 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1701 * strings. The length byte itself is not included in the length. A byte
1702 * string of length 0 is invalid. No byte string may be truncated.
1704 * It returns either:
1705 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1706 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1709 SSL_select_next_proto(unsigned char **out
, unsigned char *outlen
,
1710 const unsigned char *server
, unsigned int server_len
,
1711 const unsigned char *client
, unsigned int client_len
)
1714 const unsigned char *result
;
1715 int status
= OPENSSL_NPN_UNSUPPORTED
;
1718 * For each protocol in server preference order,
1719 * see if we support it.
1721 for (i
= 0; i
< server_len
; ) {
1722 for (j
= 0; j
< client_len
; ) {
1723 if (server
[i
] == client
[j
] &&
1724 memcmp(&server
[i
+ 1],
1725 &client
[j
+ 1], server
[i
]) == 0) {
1726 /* We found a match */
1727 result
= &server
[i
];
1728 status
= OPENSSL_NPN_NEGOTIATED
;
1738 /* There's no overlap between our protocols and the server's list. */
1740 status
= OPENSSL_NPN_NO_OVERLAP
;
1743 *out
= (unsigned char *) result
+ 1;
1744 *outlen
= result
[0];
1748 /* SSL_get0_next_proto_negotiated is deprecated. */
1750 SSL_get0_next_proto_negotiated(const SSL
*s
, const unsigned char **data
,
1757 /* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1759 SSL_CTX_set_next_protos_advertised_cb(SSL_CTX
*ctx
, int (*cb
) (SSL
*ssl
,
1760 const unsigned char **out
, unsigned int *outlen
, void *arg
), void *arg
)
1764 /* SSL_CTX_set_next_proto_select_cb is deprecated. */
1766 SSL_CTX_set_next_proto_select_cb(SSL_CTX
*ctx
, int (*cb
) (SSL
*s
,
1767 unsigned char **out
, unsigned char *outlen
, const unsigned char *in
,
1768 unsigned int inlen
, void *arg
), void *arg
)
1773 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1774 * protocols, which must be in wire-format (i.e. a series of non-empty,
1775 * 8-bit length-prefixed strings). Returns 0 on success.
1778 SSL_CTX_set_alpn_protos(SSL_CTX
*ctx
, const unsigned char *protos
,
1779 unsigned int protos_len
)
1787 CBS_init(&cbs
, protos
, protos_len
);
1789 if (protos_len
> 0) {
1790 if (!tlsext_alpn_check_format(&cbs
))
1794 if (!CBS_stow(&cbs
, &ctx
->internal
->alpn_client_proto_list
,
1795 &ctx
->internal
->alpn_client_proto_list_len
))
1801 /* NOTE: Return values are the reverse of what you expect. */
1806 * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1807 * protocols, which must be in wire-format (i.e. a series of non-empty,
1808 * 8-bit length-prefixed strings). Returns 0 on success.
1811 SSL_set_alpn_protos(SSL
*ssl
, const unsigned char *protos
,
1812 unsigned int protos_len
)
1820 CBS_init(&cbs
, protos
, protos_len
);
1822 if (protos_len
> 0) {
1823 if (!tlsext_alpn_check_format(&cbs
))
1827 if (!CBS_stow(&cbs
, &ssl
->internal
->alpn_client_proto_list
,
1828 &ssl
->internal
->alpn_client_proto_list_len
))
1834 /* NOTE: Return values are the reverse of what you expect. */
1839 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1840 * ClientHello processing in order to select an ALPN protocol from the
1841 * client's list of offered protocols.
1844 SSL_CTX_set_alpn_select_cb(SSL_CTX
* ctx
,
1845 int (*cb
) (SSL
*ssl
, const unsigned char **out
, unsigned char *outlen
,
1846 const unsigned char *in
, unsigned int inlen
, void *arg
), void *arg
)
1848 ctx
->internal
->alpn_select_cb
= cb
;
1849 ctx
->internal
->alpn_select_cb_arg
= arg
;
1853 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1854 * it sets data to point to len bytes of protocol name (not including the
1855 * leading length-prefix byte). If the server didn't respond with* a negotiated
1856 * protocol then len will be zero.
1859 SSL_get0_alpn_selected(const SSL
*ssl
, const unsigned char **data
,
1862 *data
= ssl
->s3
->alpn_selected
;
1863 *len
= ssl
->s3
->alpn_selected_len
;
1867 SSL_set_psk_use_session_callback(SSL
*s
, SSL_psk_use_session_cb_func cb
)
1873 SSL_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
1874 const char *label
, size_t llen
, const unsigned char *p
, size_t plen
,
1877 if (s
->internal
->tls13
!= NULL
&& s
->version
== TLS1_3_VERSION
) {
1882 return tls13_exporter(s
->internal
->tls13
, label
, llen
, p
, plen
,
1886 return (tls1_export_keying_material(s
, out
, olen
, label
, llen
, p
, plen
,
1890 static unsigned long
1891 ssl_session_hash(const SSL_SESSION
*a
)
1896 ((unsigned int) a
->session_id
[0] )|
1897 ((unsigned int) a
->session_id
[1]<< 8L)|
1898 ((unsigned long)a
->session_id
[2]<<16L)|
1899 ((unsigned long)a
->session_id
[3]<<24L);
1904 * NB: If this function (or indeed the hash function which uses a sort of
1905 * coarser function than this one) is changed, ensure
1906 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1907 * able to construct an SSL_SESSION that will collide with any existing session
1908 * with a matching session ID.
1911 ssl_session_cmp(const SSL_SESSION
*a
, const SSL_SESSION
*b
)
1913 if (a
->ssl_version
!= b
->ssl_version
)
1915 if (a
->session_id_length
!= b
->session_id_length
)
1917 if (timingsafe_memcmp(a
->session_id
, b
->session_id
, a
->session_id_length
) != 0)
1923 * These wrapper functions should remain rather than redeclaring
1924 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1925 * variable. The reason is that the functions aren't static, they're exposed via
1928 static unsigned long
1929 ssl_session_LHASH_HASH(const void *arg
)
1931 const SSL_SESSION
*a
= arg
;
1933 return ssl_session_hash(a
);
1937 ssl_session_LHASH_COMP(const void *arg1
, const void *arg2
)
1939 const SSL_SESSION
*a
= arg1
;
1940 const SSL_SESSION
*b
= arg2
;
1942 return ssl_session_cmp(a
, b
);
1946 SSL_CTX_new(const SSL_METHOD
*meth
)
1950 if (!OPENSSL_init_ssl(0, NULL
)) {
1951 SSLerrorx(SSL_R_LIBRARY_BUG
);
1956 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED
);
1960 if ((ret
= calloc(1, sizeof(*ret
))) == NULL
) {
1961 SSLerrorx(ERR_R_MALLOC_FAILURE
);
1964 if ((ret
->internal
= calloc(1, sizeof(*ret
->internal
))) == NULL
) {
1966 SSLerrorx(ERR_R_MALLOC_FAILURE
);
1970 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1971 SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS
);
1976 ret
->internal
->min_tls_version
= meth
->min_tls_version
;
1977 ret
->internal
->max_tls_version
= meth
->max_tls_version
;
1978 ret
->internal
->min_proto_version
= 0;
1979 ret
->internal
->max_proto_version
= 0;
1980 ret
->internal
->mode
= SSL_MODE_AUTO_RETRY
;
1982 ret
->cert_store
= NULL
;
1983 ret
->internal
->session_cache_mode
= SSL_SESS_CACHE_SERVER
;
1984 ret
->internal
->session_cache_size
= SSL_SESSION_CACHE_MAX_SIZE_DEFAULT
;
1985 ret
->internal
->session_cache_head
= NULL
;
1986 ret
->internal
->session_cache_tail
= NULL
;
1988 /* We take the system default */
1989 ret
->session_timeout
= ssl_get_default_timeout();
1991 ret
->internal
->new_session_cb
= 0;
1992 ret
->internal
->remove_session_cb
= 0;
1993 ret
->internal
->get_session_cb
= 0;
1994 ret
->internal
->generate_session_id
= 0;
1996 memset((char *)&ret
->internal
->stats
, 0, sizeof(ret
->internal
->stats
));
1998 ret
->references
= 1;
1999 ret
->internal
->quiet_shutdown
= 0;
2001 ret
->internal
->info_callback
= NULL
;
2003 ret
->internal
->app_verify_callback
= 0;
2004 ret
->internal
->app_verify_arg
= NULL
;
2006 ret
->internal
->max_cert_list
= SSL_MAX_CERT_LIST_DEFAULT
;
2007 ret
->internal
->read_ahead
= 0;
2008 ret
->internal
->msg_callback
= 0;
2009 ret
->internal
->msg_callback_arg
= NULL
;
2010 ret
->verify_mode
= SSL_VERIFY_NONE
;
2011 ret
->sid_ctx_length
= 0;
2012 ret
->internal
->default_verify_callback
= NULL
;
2014 if ((ret
->internal
->cert
= ssl_cert_new()) == NULL
)
2017 ret
->default_passwd_callback
= 0;
2018 ret
->default_passwd_callback_userdata
= NULL
;
2019 ret
->internal
->client_cert_cb
= 0;
2020 ret
->internal
->app_gen_cookie_cb
= 0;
2021 ret
->internal
->app_verify_cookie_cb
= 0;
2023 ret
->internal
->sessions
= lh_SSL_SESSION_new();
2024 if (ret
->internal
->sessions
== NULL
)
2026 ret
->cert_store
= X509_STORE_new();
2027 if (ret
->cert_store
== NULL
)
2030 ssl_create_cipher_list(ret
->method
, &ret
->cipher_list
,
2031 NULL
, SSL_DEFAULT_CIPHER_LIST
, ret
->internal
->cert
);
2032 if (ret
->cipher_list
== NULL
||
2033 sk_SSL_CIPHER_num(ret
->cipher_list
) <= 0) {
2034 SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS
);
2038 ret
->param
= X509_VERIFY_PARAM_new();
2042 if ((ret
->internal
->client_CA
= sk_X509_NAME_new_null()) == NULL
)
2045 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ret
, &ret
->internal
->ex_data
);
2047 ret
->extra_certs
= NULL
;
2049 ret
->internal
->max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
2051 ret
->internal
->tlsext_servername_callback
= 0;
2052 ret
->internal
->tlsext_servername_arg
= NULL
;
2054 /* Setup RFC4507 ticket keys */
2055 arc4random_buf(ret
->internal
->tlsext_tick_key_name
, 16);
2056 arc4random_buf(ret
->internal
->tlsext_tick_hmac_key
, 16);
2057 arc4random_buf(ret
->internal
->tlsext_tick_aes_key
, 16);
2059 ret
->internal
->tlsext_status_cb
= 0;
2060 ret
->internal
->tlsext_status_arg
= NULL
;
2062 #ifndef OPENSSL_NO_ENGINE
2063 ret
->internal
->client_cert_engine
= NULL
;
2064 #ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2065 #define eng_strx(x) #x
2066 #define eng_str(x) eng_strx(x)
2067 /* Use specific client engine automatically... ignore errors */
2070 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
2073 ENGINE_load_builtin_engines();
2074 eng
= ENGINE_by_id(eng_str(
2075 OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
2077 if (!eng
|| !SSL_CTX_set_client_cert_engine(ret
, eng
))
2083 * Default is to connect to non-RI servers. When RI is more widely
2084 * deployed might change this.
2086 ret
->internal
->options
|= SSL_OP_LEGACY_SERVER_CONNECT
;
2090 SSLerrorx(ERR_R_MALLOC_FAILURE
);
2097 SSL_CTX_free(SSL_CTX
*ctx
)
2104 i
= CRYPTO_add(&ctx
->references
, -1, CRYPTO_LOCK_SSL_CTX
);
2108 X509_VERIFY_PARAM_free(ctx
->param
);
2111 * Free internal session cache. However: the remove_cb() may reference
2112 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2113 * after the sessions were flushed.
2114 * As the ex_data handling routines might also touch the session cache,
2115 * the most secure solution seems to be: empty (flush) the cache, then
2116 * free ex_data, then finally free the cache.
2117 * (See ticket [openssl.org #212].)
2119 if (ctx
->internal
->sessions
!= NULL
)
2120 SSL_CTX_flush_sessions(ctx
, 0);
2122 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ctx
, &ctx
->internal
->ex_data
);
2124 lh_SSL_SESSION_free(ctx
->internal
->sessions
);
2126 X509_STORE_free(ctx
->cert_store
);
2127 sk_SSL_CIPHER_free(ctx
->cipher_list
);
2128 sk_SSL_CIPHER_free(ctx
->internal
->cipher_list_tls13
);
2129 ssl_cert_free(ctx
->internal
->cert
);
2130 sk_X509_NAME_pop_free(ctx
->internal
->client_CA
, X509_NAME_free
);
2131 sk_X509_pop_free(ctx
->extra_certs
, X509_free
);
2133 #ifndef OPENSSL_NO_SRTP
2134 if (ctx
->internal
->srtp_profiles
)
2135 sk_SRTP_PROTECTION_PROFILE_free(ctx
->internal
->srtp_profiles
);
2138 #ifndef OPENSSL_NO_ENGINE
2139 ENGINE_finish(ctx
->internal
->client_cert_engine
);
2142 free(ctx
->internal
->tlsext_ecpointformatlist
);
2143 free(ctx
->internal
->tlsext_supportedgroups
);
2145 free(ctx
->internal
->alpn_client_proto_list
);
2147 free(ctx
->internal
);
2152 SSL_CTX_up_ref(SSL_CTX
*ctx
)
2154 int refs
= CRYPTO_add(&ctx
->references
, 1, CRYPTO_LOCK_SSL_CTX
);
2155 return ((refs
> 1) ? 1 : 0);
2159 SSL_CTX_get_default_passwd_cb(SSL_CTX
*ctx
)
2161 return (ctx
->default_passwd_callback
);
2165 SSL_CTX_set_default_passwd_cb(SSL_CTX
*ctx
, pem_password_cb
*cb
)
2167 ctx
->default_passwd_callback
= cb
;
2171 SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX
*ctx
)
2173 return ctx
->default_passwd_callback_userdata
;
2177 SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX
*ctx
, void *u
)
2179 ctx
->default_passwd_callback_userdata
= u
;
2183 SSL_CTX_set_cert_verify_callback(SSL_CTX
*ctx
,
2184 int (*cb
)(X509_STORE_CTX
*, void *), void *arg
)
2186 ctx
->internal
->app_verify_callback
= cb
;
2187 ctx
->internal
->app_verify_arg
= arg
;
2191 SSL_CTX_set_verify(SSL_CTX
*ctx
, int mode
, int (*cb
)(int, X509_STORE_CTX
*))
2193 ctx
->verify_mode
= mode
;
2194 ctx
->internal
->default_verify_callback
= cb
;
2198 SSL_CTX_set_verify_depth(SSL_CTX
*ctx
, int depth
)
2200 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
2204 ssl_set_cert_masks(SSL_CERT
*c
, const SSL_CIPHER
*cipher
)
2206 unsigned long mask_a
, mask_k
;
2212 mask_a
= SSL_aNULL
| SSL_aTLS1_3
;
2213 mask_k
= SSL_kECDHE
| SSL_kTLS1_3
;
2215 if (c
->dhe_params
!= NULL
|| c
->dhe_params_cb
!= NULL
||
2216 c
->dhe_params_auto
!= 0)
2219 cpk
= &(c
->pkeys
[SSL_PKEY_ECC
]);
2220 if (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
) {
2221 /* Key usage, if present, must allow signing. */
2222 if (X509_get_key_usage(cpk
->x509
) & X509v3_KU_DIGITAL_SIGNATURE
)
2223 mask_a
|= SSL_aECDSA
;
2226 cpk
= &(c
->pkeys
[SSL_PKEY_GOST01
]);
2227 if (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
) {
2228 mask_k
|= SSL_kGOST
;
2229 mask_a
|= SSL_aGOST01
;
2232 cpk
= &(c
->pkeys
[SSL_PKEY_RSA
]);
2233 if (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
) {
2243 /* See if this handshake is using an ECC cipher suite. */
2245 ssl_using_ecc_cipher(SSL
*s
)
2247 unsigned long alg_a
, alg_k
;
2249 alg_a
= s
->s3
->hs
.cipher
->algorithm_auth
;
2250 alg_k
= s
->s3
->hs
.cipher
->algorithm_mkey
;
2252 return s
->session
->tlsext_ecpointformatlist
!= NULL
&&
2253 s
->session
->tlsext_ecpointformatlist_length
> 0 &&
2254 ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
));
2258 ssl_check_srvr_ecc_cert_and_alg(SSL
*s
, X509
*x
)
2260 const SSL_CIPHER
*cs
= s
->s3
->hs
.cipher
;
2261 unsigned long alg_a
;
2263 alg_a
= cs
->algorithm_auth
;
2265 if (alg_a
& SSL_aECDSA
) {
2266 /* Key usage, if present, must allow signing. */
2267 if (!(X509_get_key_usage(x
) & X509v3_KU_DIGITAL_SIGNATURE
)) {
2268 SSLerror(s
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
2277 ssl_get_server_send_pkey(const SSL
*s
)
2279 unsigned long alg_a
;
2284 ssl_set_cert_masks(c
, s
->s3
->hs
.cipher
);
2286 alg_a
= s
->s3
->hs
.cipher
->algorithm_auth
;
2288 if (alg_a
& SSL_aECDSA
) {
2290 } else if (alg_a
& SSL_aRSA
) {
2292 } else if (alg_a
& SSL_aGOST01
) {
2293 i
= SSL_PKEY_GOST01
;
2294 } else { /* if (alg_a & SSL_aNULL) */
2295 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2299 return (c
->pkeys
+ i
);
2303 ssl_get_sign_pkey(SSL
*s
, const SSL_CIPHER
*cipher
, const EVP_MD
**pmd
,
2304 const struct ssl_sigalg
**sap
)
2306 const struct ssl_sigalg
*sigalg
= NULL
;
2307 EVP_PKEY
*pkey
= NULL
;
2308 unsigned long alg_a
;
2312 alg_a
= cipher
->algorithm_auth
;
2315 if (alg_a
& SSL_aRSA
) {
2317 } else if ((alg_a
& SSL_aECDSA
) &&
2318 (c
->pkeys
[SSL_PKEY_ECC
].privatekey
!= NULL
))
2321 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2325 pkey
= c
->pkeys
[idx
].privatekey
;
2326 if ((sigalg
= ssl_sigalg_select(s
, pkey
)) == NULL
) {
2327 SSLerror(s
, SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
2330 *pmd
= sigalg
->md();
2337 ssl_dhe_params_auto_key_bits(SSL
*s
)
2342 if (s
->cert
->dhe_params_auto
== 2) {
2344 } else if (s
->s3
->hs
.cipher
->algorithm_auth
& SSL_aNULL
) {
2346 if (s
->s3
->hs
.cipher
->strength_bits
== 256)
2349 if ((cpk
= ssl_get_server_send_pkey(s
)) == NULL
)
2351 if (cpk
->privatekey
== NULL
||
2352 EVP_PKEY_get0_RSA(cpk
->privatekey
) == NULL
)
2354 if ((key_bits
= EVP_PKEY_bits(cpk
->privatekey
)) <= 0)
2362 ssl_should_update_external_cache(SSL
*s
, int mode
)
2366 cache_mode
= s
->session_ctx
->internal
->session_cache_mode
;
2368 /* Don't cache if mode says not to */
2369 if ((cache_mode
& mode
) == 0)
2372 /* if it is not already cached, cache it */
2373 if (!s
->internal
->hit
)
2376 /* If it's TLS 1.3, do it to match OpenSSL */
2377 if (s
->s3
->hs
.negotiated_tls_version
>= TLS1_3_VERSION
)
2384 ssl_should_update_internal_cache(SSL
*s
, int mode
)
2388 cache_mode
= s
->session_ctx
->internal
->session_cache_mode
;
2390 /* Don't cache if mode says not to */
2391 if ((cache_mode
& mode
) == 0)
2394 /* If it is already cached, don't cache it again */
2395 if (s
->internal
->hit
)
2398 if ((cache_mode
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) != 0)
2401 /* If we are lesser than TLS 1.3, Cache it. */
2402 if (s
->s3
->hs
.negotiated_tls_version
< TLS1_3_VERSION
)
2405 /* Below this we consider TLS 1.3 or later */
2407 /* If it's not a server, add it? OpenSSL does this. */
2411 /* XXX if we support early data / PSK need to add */
2414 * If we have the remove session callback, we will want
2415 * to know about this even if it's a stateless ticket
2416 * from 1.3 so we can know when it is removed.
2418 if (s
->session_ctx
->internal
->remove_session_cb
!= NULL
)
2421 /* If we have set OP_NO_TICKET, cache it. */
2422 if ((s
->internal
->options
& SSL_OP_NO_TICKET
) != 0)
2425 /* Otherwise do not cache */
2430 ssl_update_cache(SSL
*s
, int mode
)
2432 int cache_mode
, do_callback
;
2434 if (s
->session
->session_id_length
== 0)
2437 cache_mode
= s
->session_ctx
->internal
->session_cache_mode
;
2438 do_callback
= ssl_should_update_external_cache(s
, mode
);
2440 if (ssl_should_update_internal_cache(s
, mode
)) {
2442 * XXX should we fail if the add to the internal cache
2443 * fails? OpenSSL doesn't care..
2445 (void) SSL_CTX_add_session(s
->session_ctx
, s
->session
);
2449 * Update the "external cache" by calling the new session
2450 * callback if present, even with TLS 1.3 without early data
2451 * "because some application just want to know about the
2452 * creation of a session and aren't doing a full cache".
2453 * Apparently, if they are doing a full cache, they'll have
2454 * some fun, but we endeavour to give application writers the
2455 * same glorious experience they expect from OpenSSL which
2458 if (do_callback
&& s
->session_ctx
->internal
->new_session_cb
!= NULL
) {
2459 CRYPTO_add(&s
->session
->references
, 1, CRYPTO_LOCK_SSL_SESSION
);
2460 if (!s
->session_ctx
->internal
->new_session_cb(s
, s
->session
))
2461 SSL_SESSION_free(s
->session
);
2464 /* Auto flush every 255 connections. */
2465 if (!(cache_mode
& SSL_SESS_CACHE_NO_AUTO_CLEAR
) &&
2466 (cache_mode
& mode
) != 0) {
2468 if (mode
& SSL_SESS_CACHE_CLIENT
)
2469 connections
= s
->session_ctx
->internal
->stats
.sess_connect_good
;
2471 connections
= s
->session_ctx
->internal
->stats
.sess_accept_good
;
2472 if ((connections
& 0xff) == 0xff)
2473 SSL_CTX_flush_sessions(s
->session_ctx
, time(NULL
));
2478 SSL_get_ssl_method(SSL
*s
)
2484 SSL_set_ssl_method(SSL
*s
, const SSL_METHOD
*method
)
2486 int (*handshake_func
)(SSL
*) = NULL
;
2489 if (s
->method
== method
)
2492 if (s
->internal
->handshake_func
== s
->method
->ssl_connect
)
2493 handshake_func
= method
->ssl_connect
;
2494 else if (s
->internal
->handshake_func
== s
->method
->ssl_accept
)
2495 handshake_func
= method
->ssl_accept
;
2497 if (s
->method
->version
== method
->version
) {
2500 s
->method
->ssl_free(s
);
2502 ret
= s
->method
->ssl_new(s
);
2504 s
->internal
->handshake_func
= handshake_func
;
2510 SSL_get_error(const SSL
*s
, int i
)
2517 return (SSL_ERROR_NONE
);
2520 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2521 * etc, where we do encode the error.
2523 if ((l
= ERR_peek_error()) != 0) {
2524 if (ERR_GET_LIB(l
) == ERR_LIB_SYS
)
2525 return (SSL_ERROR_SYSCALL
);
2527 return (SSL_ERROR_SSL
);
2530 if (SSL_want_read(s
)) {
2531 bio
= SSL_get_rbio(s
);
2532 if (BIO_should_read(bio
)) {
2533 return (SSL_ERROR_WANT_READ
);
2534 } else if (BIO_should_write(bio
)) {
2536 * This one doesn't make too much sense... We never
2537 * try to write to the rbio, and an application
2538 * program where rbio and wbio are separate couldn't
2539 * even know what it should wait for. However if we
2540 * ever set s->internal->rwstate incorrectly (so that we have
2541 * SSL_want_read(s) instead of SSL_want_write(s))
2542 * and rbio and wbio *are* the same, this test works
2543 * around that bug; so it might be safer to keep it.
2545 return (SSL_ERROR_WANT_WRITE
);
2546 } else if (BIO_should_io_special(bio
)) {
2547 reason
= BIO_get_retry_reason(bio
);
2548 if (reason
== BIO_RR_CONNECT
)
2549 return (SSL_ERROR_WANT_CONNECT
);
2550 else if (reason
== BIO_RR_ACCEPT
)
2551 return (SSL_ERROR_WANT_ACCEPT
);
2553 return (SSL_ERROR_SYSCALL
); /* unknown */
2557 if (SSL_want_write(s
)) {
2558 bio
= SSL_get_wbio(s
);
2559 if (BIO_should_write(bio
)) {
2560 return (SSL_ERROR_WANT_WRITE
);
2561 } else if (BIO_should_read(bio
)) {
2563 * See above (SSL_want_read(s) with
2564 * BIO_should_write(bio))
2566 return (SSL_ERROR_WANT_READ
);
2567 } else if (BIO_should_io_special(bio
)) {
2568 reason
= BIO_get_retry_reason(bio
);
2569 if (reason
== BIO_RR_CONNECT
)
2570 return (SSL_ERROR_WANT_CONNECT
);
2571 else if (reason
== BIO_RR_ACCEPT
)
2572 return (SSL_ERROR_WANT_ACCEPT
);
2574 return (SSL_ERROR_SYSCALL
);
2578 if (SSL_want_x509_lookup(s
))
2579 return (SSL_ERROR_WANT_X509_LOOKUP
);
2581 if ((s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) &&
2582 (s
->s3
->warn_alert
== SSL_AD_CLOSE_NOTIFY
))
2583 return (SSL_ERROR_ZERO_RETURN
);
2585 return (SSL_ERROR_SYSCALL
);
2589 SSL_CTX_set_quic_method(SSL_CTX
*ctx
, const SSL_QUIC_METHOD
*quic_method
)
2591 if (ctx
->method
->dtls
)
2594 ctx
->quic_method
= quic_method
;
2600 SSL_set_quic_method(SSL
*ssl
, const SSL_QUIC_METHOD
*quic_method
)
2602 if (ssl
->method
->dtls
)
2605 ssl
->quic_method
= quic_method
;
2611 SSL_quic_max_handshake_flight_len(const SSL
*ssl
,
2612 enum ssl_encryption_level_t level
)
2616 /* Limit flights to 16K when there are no large certificate messages. */
2620 case ssl_encryption_initial
:
2623 case ssl_encryption_early_data
:
2624 /* QUIC does not send EndOfEarlyData. */
2627 case ssl_encryption_handshake
:
2630 * Servers may receive Certificate message if configured
2631 * to request client certificates.
2633 if ((SSL_get_verify_mode(ssl
) & SSL_VERIFY_PEER
) != 0 &&
2634 ssl
->internal
->max_cert_list
> flight_len
)
2635 flight_len
= ssl
->internal
->max_cert_list
;
2638 * Clients may receive both Certificate message and a
2639 * CertificateRequest message.
2641 if (ssl
->internal
->max_cert_list
* 2 > flight_len
)
2642 flight_len
= ssl
->internal
->max_cert_list
* 2;
2645 case ssl_encryption_application
:
2647 * Note there is not actually a bound on the number of
2648 * NewSessionTickets one may send in a row. This level may need
2649 * more involved flow control.
2657 enum ssl_encryption_level_t
2658 SSL_quic_read_level(const SSL
*ssl
)
2660 return ssl
->s3
->hs
.tls13
.quic_read_level
;
2663 enum ssl_encryption_level_t
2664 SSL_quic_write_level(const SSL
*ssl
)
2666 return ssl
->s3
->hs
.tls13
.quic_write_level
;
2670 SSL_provide_quic_data(SSL
*ssl
, enum ssl_encryption_level_t level
,
2671 const uint8_t *data
, size_t len
)
2673 if (!SSL_is_quic(ssl
)) {
2674 SSLerror(ssl
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2678 if (level
!= SSL_quic_read_level(ssl
)) {
2679 SSLerror(ssl
, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED
);
2683 if (ssl
->s3
->hs
.tls13
.quic_read_buffer
== NULL
) {
2684 ssl
->s3
->hs
.tls13
.quic_read_buffer
= tls_buffer_new(0);
2685 if (ssl
->s3
->hs
.tls13
.quic_read_buffer
== NULL
) {
2686 SSLerror(ssl
, ERR_R_MALLOC_FAILURE
);
2691 /* XXX - note that this does not currently downsize. */
2692 tls_buffer_set_capacity_limit(ssl
->s3
->hs
.tls13
.quic_read_buffer
,
2693 SSL_quic_max_handshake_flight_len(ssl
, level
));
2696 * XXX - an append that fails due to exceeding capacity should set
2697 * SSL_R_EXCESSIVE_MESSAGE_SIZE.
2699 return tls_buffer_append(ssl
->s3
->hs
.tls13
.quic_read_buffer
, data
, len
);
2703 SSL_process_quic_post_handshake(SSL
*ssl
)
2705 /* XXX - this needs to run PHH received. */
2710 SSL_do_handshake(SSL
*s
)
2712 if (s
->internal
->handshake_func
== NULL
) {
2713 SSLerror(s
, SSL_R_CONNECTION_TYPE_NOT_SET
);
2717 s
->method
->ssl_renegotiate_check(s
);
2719 if (!SSL_in_init(s
) && !SSL_in_before(s
))
2722 return s
->internal
->handshake_func(s
);
2726 * For the next 2 functions, SSL_clear() sets shutdown and so
2727 * one of these calls will reset it
2730 SSL_set_accept_state(SSL
*s
)
2733 s
->internal
->shutdown
= 0;
2734 s
->s3
->hs
.state
= SSL_ST_ACCEPT
|SSL_ST_BEFORE
;
2735 s
->internal
->handshake_func
= s
->method
->ssl_accept
;
2736 ssl_clear_cipher_state(s
);
2740 SSL_set_connect_state(SSL
*s
)
2743 s
->internal
->shutdown
= 0;
2744 s
->s3
->hs
.state
= SSL_ST_CONNECT
|SSL_ST_BEFORE
;
2745 s
->internal
->handshake_func
= s
->method
->ssl_connect
;
2746 ssl_clear_cipher_state(s
);
2750 ssl_undefined_function(SSL
*s
)
2752 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2757 ssl_undefined_void_function(void)
2759 SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2764 ssl_undefined_const_function(const SSL
*s
)
2766 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2771 ssl_version_string(int ver
)
2775 return (SSL_TXT_TLSV1
);
2776 case TLS1_1_VERSION
:
2777 return (SSL_TXT_TLSV1_1
);
2778 case TLS1_2_VERSION
:
2779 return (SSL_TXT_TLSV1_2
);
2780 case TLS1_3_VERSION
:
2781 return (SSL_TXT_TLSV1_3
);
2783 return (SSL_TXT_DTLS1
);
2784 case DTLS1_2_VERSION
:
2785 return (SSL_TXT_DTLS1_2
);
2792 SSL_get_version(const SSL
*s
)
2794 return ssl_version_string(s
->version
);
2800 STACK_OF(X509_NAME
) *sk
;
2805 if ((ret
= SSL_new(SSL_get_SSL_CTX(s
))) == NULL
)
2808 ret
->version
= s
->version
;
2809 ret
->method
= s
->method
;
2811 if (s
->session
!= NULL
) {
2812 if (!SSL_copy_session_id(ret
, s
))
2816 * No session has been established yet, so we have to expect
2817 * that s->cert or ret->cert will be changed later --
2818 * they should not both point to the same object,
2819 * and thus we can't use SSL_copy_session_id.
2822 ret
->method
->ssl_free(ret
);
2823 ret
->method
= s
->method
;
2824 ret
->method
->ssl_new(ret
);
2826 ssl_cert_free(ret
->cert
);
2827 if ((ret
->cert
= ssl_cert_dup(s
->cert
)) == NULL
)
2830 if (!SSL_set_session_id_context(ret
, s
->sid_ctx
,
2835 ret
->internal
->options
= s
->internal
->options
;
2836 ret
->internal
->mode
= s
->internal
->mode
;
2837 SSL_set_max_cert_list(ret
, SSL_get_max_cert_list(s
));
2838 SSL_set_read_ahead(ret
, SSL_get_read_ahead(s
));
2839 ret
->internal
->msg_callback
= s
->internal
->msg_callback
;
2840 ret
->internal
->msg_callback_arg
= s
->internal
->msg_callback_arg
;
2841 SSL_set_verify(ret
, SSL_get_verify_mode(s
),
2842 SSL_get_verify_callback(s
));
2843 SSL_set_verify_depth(ret
, SSL_get_verify_depth(s
));
2844 ret
->internal
->generate_session_id
= s
->internal
->generate_session_id
;
2846 SSL_set_info_callback(ret
, SSL_get_info_callback(s
));
2848 ret
->internal
->debug
= s
->internal
->debug
;
2850 /* copy app data, a little dangerous perhaps */
2851 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL
,
2852 &ret
->internal
->ex_data
, &s
->internal
->ex_data
))
2855 /* setup rbio, and wbio */
2856 if (s
->rbio
!= NULL
) {
2857 if (!BIO_dup_state(s
->rbio
,(char *)&ret
->rbio
))
2860 if (s
->wbio
!= NULL
) {
2861 if (s
->wbio
!= s
->rbio
) {
2862 if (!BIO_dup_state(s
->wbio
,(char *)&ret
->wbio
))
2865 ret
->wbio
= ret
->rbio
;
2867 ret
->internal
->rwstate
= s
->internal
->rwstate
;
2868 ret
->internal
->in_handshake
= s
->internal
->in_handshake
;
2869 ret
->internal
->handshake_func
= s
->internal
->handshake_func
;
2870 ret
->server
= s
->server
;
2871 ret
->internal
->renegotiate
= s
->internal
->renegotiate
;
2872 ret
->internal
->new_session
= s
->internal
->new_session
;
2873 ret
->internal
->quiet_shutdown
= s
->internal
->quiet_shutdown
;
2874 ret
->internal
->shutdown
= s
->internal
->shutdown
;
2875 /* SSL_dup does not really work at any state, though */
2876 ret
->s3
->hs
.state
= s
->s3
->hs
.state
;
2877 ret
->internal
->rstate
= s
->internal
->rstate
;
2880 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2883 ret
->internal
->init_num
= 0;
2885 ret
->internal
->hit
= s
->internal
->hit
;
2887 X509_VERIFY_PARAM_inherit(ret
->param
, s
->param
);
2889 if (s
->cipher_list
!= NULL
) {
2890 if ((ret
->cipher_list
=
2891 sk_SSL_CIPHER_dup(s
->cipher_list
)) == NULL
)
2894 if (s
->internal
->cipher_list_tls13
!= NULL
) {
2895 if ((ret
->internal
->cipher_list_tls13
=
2896 sk_SSL_CIPHER_dup(s
->internal
->cipher_list_tls13
)) == NULL
)
2900 /* Dup the client_CA list */
2901 if (s
->internal
->client_CA
!= NULL
) {
2902 if ((sk
= sk_X509_NAME_dup(s
->internal
->client_CA
)) == NULL
) goto err
;
2903 ret
->internal
->client_CA
= sk
;
2904 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
2905 xn
= sk_X509_NAME_value(sk
, i
);
2906 if (sk_X509_NAME_set(sk
, i
,
2907 X509_NAME_dup(xn
)) == NULL
) {
2921 ssl_clear_cipher_state(SSL
*s
)
2923 tls12_record_layer_clear_read_state(s
->internal
->rl
);
2924 tls12_record_layer_clear_write_state(s
->internal
->rl
);
2928 ssl_info_callback(const SSL
*s
, int type
, int value
)
2930 ssl_info_callback_fn
*cb
;
2932 if ((cb
= s
->internal
->info_callback
) == NULL
)
2933 cb
= s
->ctx
->internal
->info_callback
;
2939 ssl_msg_callback(SSL
*s
, int is_write
, int content_type
,
2940 const void *msg_buf
, size_t msg_len
)
2942 if (s
->internal
->msg_callback
== NULL
)
2945 s
->internal
->msg_callback(is_write
, s
->version
, content_type
,
2946 msg_buf
, msg_len
, s
, s
->internal
->msg_callback_arg
);
2950 ssl_msg_callback_cbs(SSL
*s
, int is_write
, int content_type
, CBS
*cbs
)
2952 ssl_msg_callback(s
, is_write
, content_type
, CBS_data(cbs
), CBS_len(cbs
));
2955 /* Fix this function so that it takes an optional type parameter */
2957 SSL_get_certificate(const SSL
*s
)
2959 return (s
->cert
->key
->x509
);
2962 /* Fix this function so that it takes an optional type parameter */
2964 SSL_get_privatekey(const SSL
*s
)
2966 return (s
->cert
->key
->privatekey
);
2970 SSL_get_current_cipher(const SSL
*s
)
2972 if ((s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
))
2973 return (s
->session
->cipher
);
2977 SSL_get_current_compression(SSL
*s
)
2983 SSL_get_current_expansion(SSL
*s
)
2989 SSL_get_client_random(const SSL
*s
, unsigned char *out
, size_t max_out
)
2991 size_t len
= sizeof(s
->s3
->client_random
);
2999 memcpy(out
, s
->s3
->client_random
, len
);
3005 SSL_get_server_random(const SSL
*s
, unsigned char *out
, size_t max_out
)
3007 size_t len
= sizeof(s
->s3
->server_random
);
3015 memcpy(out
, s
->s3
->server_random
, len
);
3021 ssl_init_wbio_buffer(SSL
*s
, int push
)
3025 if (s
->bbio
== NULL
) {
3026 bbio
= BIO_new(BIO_f_buffer());
3032 if (s
->bbio
== s
->wbio
)
3033 s
->wbio
= BIO_pop(s
->wbio
);
3035 (void)BIO_reset(bbio
);
3036 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3037 if (!BIO_set_read_buffer_size(bbio
, 1)) {
3038 SSLerror(s
, ERR_R_BUF_LIB
);
3042 if (s
->wbio
!= bbio
)
3043 s
->wbio
= BIO_push(bbio
, s
->wbio
);
3045 if (s
->wbio
== bbio
)
3046 s
->wbio
= BIO_pop(bbio
);
3052 ssl_free_wbio_buffer(SSL
*s
)
3057 if (s
->bbio
== NULL
)
3060 if (s
->bbio
== s
->wbio
) {
3061 /* remove buffering */
3062 s
->wbio
= BIO_pop(s
->wbio
);
3069 SSL_CTX_set_quiet_shutdown(SSL_CTX
*ctx
, int mode
)
3071 ctx
->internal
->quiet_shutdown
= mode
;
3075 SSL_CTX_get_quiet_shutdown(const SSL_CTX
*ctx
)
3077 return (ctx
->internal
->quiet_shutdown
);
3081 SSL_set_quiet_shutdown(SSL
*s
, int mode
)
3083 s
->internal
->quiet_shutdown
= mode
;
3087 SSL_get_quiet_shutdown(const SSL
*s
)
3089 return (s
->internal
->quiet_shutdown
);
3093 SSL_set_shutdown(SSL
*s
, int mode
)
3095 s
->internal
->shutdown
= mode
;
3099 SSL_get_shutdown(const SSL
*s
)
3101 return (s
->internal
->shutdown
);
3105 SSL_version(const SSL
*s
)
3107 return (s
->version
);
3111 SSL_get_SSL_CTX(const SSL
*ssl
)
3117 SSL_set_SSL_CTX(SSL
*ssl
, SSL_CTX
* ctx
)
3122 ctx
= ssl
->initial_ctx
;
3123 if (ssl
->ctx
== ctx
)
3126 if ((new_cert
= ssl_cert_dup(ctx
->internal
->cert
)) == NULL
)
3128 ssl_cert_free(ssl
->cert
);
3129 ssl
->cert
= new_cert
;
3131 SSL_CTX_up_ref(ctx
);
3132 SSL_CTX_free(ssl
->ctx
); /* decrement reference count */
3139 SSL_CTX_set_default_verify_paths(SSL_CTX
*ctx
)
3141 return (X509_STORE_set_default_paths(ctx
->cert_store
));
3145 SSL_CTX_load_verify_locations(SSL_CTX
*ctx
, const char *CAfile
,
3148 return (X509_STORE_load_locations(ctx
->cert_store
, CAfile
, CApath
));
3152 SSL_CTX_load_verify_mem(SSL_CTX
*ctx
, void *buf
, int len
)
3154 return (X509_STORE_load_mem(ctx
->cert_store
, buf
, len
));
3158 SSL_set_info_callback(SSL
*ssl
, void (*cb
)(const SSL
*ssl
, int type
, int val
))
3160 ssl
->internal
->info_callback
= cb
;
3163 void (*SSL_get_info_callback(const SSL
*ssl
))(const SSL
*ssl
, int type
, int val
)
3165 return (ssl
->internal
->info_callback
);
3169 SSL_state(const SSL
*ssl
)
3171 return (ssl
->s3
->hs
.state
);
3175 SSL_set_state(SSL
*ssl
, int state
)
3177 ssl
->s3
->hs
.state
= state
;
3181 SSL_set_verify_result(SSL
*ssl
, long arg
)
3183 ssl
->verify_result
= arg
;
3187 SSL_get_verify_result(const SSL
*ssl
)
3189 return (ssl
->verify_result
);
3193 SSL_verify_client_post_handshake(SSL
*ssl
)
3199 SSL_CTX_set_post_handshake_auth(SSL_CTX
*ctx
, int val
)
3205 SSL_set_post_handshake_auth(SSL
*ssl
, int val
)
3211 SSL_get_ex_new_index(long argl
, void *argp
, CRYPTO_EX_new
*new_func
,
3212 CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
3214 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL
, argl
, argp
,
3215 new_func
, dup_func
, free_func
));
3219 SSL_set_ex_data(SSL
*s
, int idx
, void *arg
)
3221 return (CRYPTO_set_ex_data(&s
->internal
->ex_data
, idx
, arg
));
3225 SSL_get_ex_data(const SSL
*s
, int idx
)
3227 return (CRYPTO_get_ex_data(&s
->internal
->ex_data
, idx
));
3231 SSL_CTX_get_ex_new_index(long argl
, void *argp
, CRYPTO_EX_new
*new_func
,
3232 CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
3234 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX
, argl
, argp
,
3235 new_func
, dup_func
, free_func
));
3239 SSL_CTX_set_ex_data(SSL_CTX
*s
, int idx
, void *arg
)
3241 return (CRYPTO_set_ex_data(&s
->internal
->ex_data
, idx
, arg
));
3245 SSL_CTX_get_ex_data(const SSL_CTX
*s
, int idx
)
3247 return (CRYPTO_get_ex_data(&s
->internal
->ex_data
, idx
));
3257 SSL_CTX_get_cert_store(const SSL_CTX
*ctx
)
3259 return (ctx
->cert_store
);
3263 SSL_CTX_set_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
3265 X509_STORE_free(ctx
->cert_store
);
3266 ctx
->cert_store
= store
;
3270 SSL_CTX_get0_certificate(const SSL_CTX
*ctx
)
3272 if (ctx
->internal
->cert
== NULL
)
3275 return ctx
->internal
->cert
->key
->x509
;
3279 SSL_CTX_get0_privatekey(const SSL_CTX
*ctx
)
3281 if (ctx
->internal
->cert
== NULL
)
3284 return ctx
->internal
->cert
->key
->privatekey
;
3288 SSL_want(const SSL
*s
)
3290 return (s
->internal
->rwstate
);
3294 SSL_CTX_set_tmp_rsa_callback(SSL_CTX
*ctx
, RSA
*(*cb
)(SSL
*ssl
, int is_export
,
3297 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_RSA_CB
,(void (*)(void))cb
);
3301 SSL_set_tmp_rsa_callback(SSL
*ssl
, RSA
*(*cb
)(SSL
*ssl
, int is_export
,
3304 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_RSA_CB
,(void (*)(void))cb
);
3308 SSL_CTX_set_tmp_dh_callback(SSL_CTX
*ctx
, DH
*(*dh
)(SSL
*ssl
, int is_export
,
3311 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_DH_CB
,(void (*)(void))dh
);
3315 SSL_set_tmp_dh_callback(SSL
*ssl
, DH
*(*dh
)(SSL
*ssl
, int is_export
,
3318 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_DH_CB
,(void (*)(void))dh
);
3322 SSL_CTX_set_tmp_ecdh_callback(SSL_CTX
*ctx
, EC_KEY
*(*ecdh
)(SSL
*ssl
,
3323 int is_export
, int keylength
))
3325 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_ECDH_CB
,
3326 (void (*)(void))ecdh
);
3330 SSL_set_tmp_ecdh_callback(SSL
*ssl
, EC_KEY
*(*ecdh
)(SSL
*ssl
, int is_export
,
3333 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_ECDH_CB
,(void (*)(void))ecdh
);
3338 SSL_CTX_set_msg_callback(SSL_CTX
*ctx
, void (*cb
)(int write_p
, int version
,
3339 int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))
3341 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_MSG_CALLBACK
,
3342 (void (*)(void))cb
);
3346 SSL_set_msg_callback(SSL
*ssl
, void (*cb
)(int write_p
, int version
,
3347 int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))
3349 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
3353 SSL_set_debug(SSL
*s
, int debug
)
3355 s
->internal
->debug
= debug
;
3359 SSL_cache_hit(SSL
*s
)
3361 return (s
->internal
->hit
);
3365 SSL_CTX_get_min_proto_version(SSL_CTX
*ctx
)
3367 return ctx
->internal
->min_proto_version
;
3371 SSL_CTX_set_min_proto_version(SSL_CTX
*ctx
, uint16_t version
)
3373 return ssl_version_set_min(ctx
->method
, version
,
3374 ctx
->internal
->max_tls_version
, &ctx
->internal
->min_tls_version
,
3375 &ctx
->internal
->min_proto_version
);
3379 SSL_CTX_get_max_proto_version(SSL_CTX
*ctx
)
3381 return ctx
->internal
->max_proto_version
;
3385 SSL_CTX_set_max_proto_version(SSL_CTX
*ctx
, uint16_t version
)
3387 return ssl_version_set_max(ctx
->method
, version
,
3388 ctx
->internal
->min_tls_version
, &ctx
->internal
->max_tls_version
,
3389 &ctx
->internal
->max_proto_version
);
3393 SSL_get_min_proto_version(SSL
*ssl
)
3395 return ssl
->internal
->min_proto_version
;
3399 SSL_set_min_proto_version(SSL
*ssl
, uint16_t version
)
3401 return ssl_version_set_min(ssl
->method
, version
,
3402 ssl
->internal
->max_tls_version
, &ssl
->internal
->min_tls_version
,
3403 &ssl
->internal
->min_proto_version
);
3406 SSL_get_max_proto_version(SSL
*ssl
)
3408 return ssl
->internal
->max_proto_version
;
3412 SSL_set_max_proto_version(SSL
*ssl
, uint16_t version
)
3414 return ssl_version_set_max(ssl
->method
, version
,
3415 ssl
->internal
->min_tls_version
, &ssl
->internal
->max_tls_version
,
3416 &ssl
->internal
->max_proto_version
);
3420 SSL_CTX_get_ssl_method(const SSL_CTX
*ctx
)
3426 SSL_CTX_get_security_level(const SSL_CTX
*ctx
)
3428 return ctx
->internal
->cert
->security_level
;
3432 SSL_CTX_set_security_level(SSL_CTX
*ctx
, int level
)
3434 ctx
->internal
->cert
->security_level
= level
;
3438 SSL_get_security_level(const SSL
*ssl
)
3440 return ssl
->cert
->security_level
;
3444 SSL_set_security_level(SSL
*ssl
, int level
)
3446 ssl
->cert
->security_level
= level
;
3450 SSL_is_quic(const SSL
*ssl
)
3452 return ssl
->quic_method
!= NULL
;
3456 SSL_set_quic_transport_params(SSL
*ssl
, const uint8_t *params
,
3459 freezero(ssl
->internal
->quic_transport_params
,
3460 ssl
->internal
->quic_transport_params_len
);
3461 ssl
->internal
->quic_transport_params
= NULL
;
3462 ssl
->internal
->quic_transport_params_len
= 0;
3464 if ((ssl
->internal
->quic_transport_params
= malloc(params_len
)) == NULL
)
3467 memcpy(ssl
->internal
->quic_transport_params
, params
, params_len
);
3468 ssl
->internal
->quic_transport_params_len
= params_len
;
3474 SSL_get_peer_quic_transport_params(const SSL
*ssl
, const uint8_t **out_params
,
3475 size_t *out_params_len
)
3477 *out_params
= ssl
->s3
->peer_quic_transport_params
;
3478 *out_params_len
= ssl
->s3
->peer_quic_transport_params_len
;
3482 SSL_set_quic_use_legacy_codepoint(SSL
*ssl
, int use_legacy
)
3484 /* Not supported. */
3488 ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_
, const void *b_
)
3490 SSL_CIPHER
const *a
= a_
;
3491 SSL_CIPHER
const *b
= b_
;
3492 return ssl_cipher_id_cmp(a
, b
);
3496 OBJ_bsearch_ssl_cipher_id(SSL_CIPHER
*key
, SSL_CIPHER
const *base
, int num
)
3498 return (SSL_CIPHER
*)OBJ_bsearch_(key
, base
, num
, sizeof(SSL_CIPHER
),
3499 ssl_cipher_id_cmp_BSEARCH_CMP_FN
);