1 /* $OpenBSD: ssl_lib.c,v 1.170 2017/08/30 16:24:21 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
145 #include "ssl_locl.h"
147 #include <openssl/bn.h>
148 #include <openssl/dh.h>
149 #include <openssl/lhash.h>
150 #include <openssl/objects.h>
151 #include <openssl/ocsp.h>
152 #include <openssl/x509v3.h>
154 #ifndef OPENSSL_NO_ENGINE
155 #include <openssl/engine.h>
158 #include "bytestring.h"
160 const char *SSL_version_str
= OPENSSL_VERSION_TEXT
;
165 if (s
->method
== NULL
) {
166 SSLerror(s
, SSL_R_NO_METHOD_SPECIFIED
);
170 if (ssl_clear_bad_session(s
)) {
171 SSL_SESSION_free(s
->session
);
176 s
->internal
->hit
= 0;
177 s
->internal
->shutdown
= 0;
179 if (s
->internal
->renegotiate
) {
180 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
184 s
->internal
->type
= 0;
186 s
->version
= s
->method
->internal
->version
;
187 s
->client_version
= s
->version
;
188 s
->internal
->rwstate
= SSL_NOTHING
;
189 s
->internal
->rstate
= SSL_ST_READ_HEADER
;
191 BUF_MEM_free(s
->internal
->init_buf
);
192 s
->internal
->init_buf
= NULL
;
194 ssl_clear_cipher_ctx(s
);
195 ssl_clear_hash_ctx(&s
->read_hash
);
196 ssl_clear_hash_ctx(&s
->internal
->write_hash
);
198 s
->internal
->first_packet
= 0;
201 * Check to see if we were changed into a different method, if
202 * so, revert back if we are not doing session-id reuse.
204 if (!s
->internal
->in_handshake
&& (s
->session
== NULL
) &&
205 (s
->method
!= s
->ctx
->method
)) {
206 s
->method
->internal
->ssl_free(s
);
207 s
->method
= s
->ctx
->method
;
208 if (!s
->method
->internal
->ssl_new(s
))
211 s
->method
->internal
->ssl_clear(s
);
213 S3I(s
)->hs
.state
= SSL_ST_BEFORE
|((s
->server
) ? SSL_ST_ACCEPT
: SSL_ST_CONNECT
);
218 /* Used to change an SSL_CTXs default SSL method type */
220 SSL_CTX_set_ssl_version(SSL_CTX
*ctx
, const SSL_METHOD
*meth
)
222 STACK_OF(SSL_CIPHER
) *sk
;
226 sk
= ssl_create_cipher_list(ctx
->method
, &(ctx
->cipher_list
),
227 &(ctx
->internal
->cipher_list_by_id
), SSL_DEFAULT_CIPHER_LIST
);
228 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= 0)) {
229 SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
236 SSL_new(SSL_CTX
*ctx
)
241 SSLerrorx(SSL_R_NULL_SSL_CTX
);
244 if (ctx
->method
== NULL
) {
245 SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION
);
249 if ((s
= calloc(1, sizeof(*s
))) == NULL
) {
250 SSLerrorx(ERR_R_MALLOC_FAILURE
);
253 if ((s
->internal
= calloc(1, sizeof(*s
->internal
))) == NULL
) {
255 SSLerrorx(ERR_R_MALLOC_FAILURE
);
259 s
->internal
->min_version
= ctx
->internal
->min_version
;
260 s
->internal
->max_version
= ctx
->internal
->max_version
;
262 s
->internal
->options
= ctx
->internal
->options
;
263 s
->internal
->mode
= ctx
->internal
->mode
;
264 s
->internal
->max_cert_list
= ctx
->internal
->max_cert_list
;
266 if (ctx
->internal
->cert
!= NULL
) {
268 * Earlier library versions used to copy the pointer to
269 * the CERT, not its contents; only when setting new
270 * parameters for the per-SSL copy, ssl_cert_new would be
271 * called (and the direct reference to the per-SSL_CTX
272 * settings would be lost, but those still were indirectly
273 * accessed for various purposes, and for that reason they
274 * used to be known as s->ctx->default_cert).
275 * Now we don't look at the SSL_CTX's CERT after having
276 * duplicated it once.
278 s
->cert
= ssl_cert_dup(ctx
->internal
->cert
);
282 s
->cert
=NULL
; /* Cannot really happen (see SSL_CTX_new) */
284 s
->internal
->read_ahead
= ctx
->internal
->read_ahead
;
285 s
->internal
->msg_callback
= ctx
->internal
->msg_callback
;
286 s
->internal
->msg_callback_arg
= ctx
->internal
->msg_callback_arg
;
287 s
->verify_mode
= ctx
->verify_mode
;
288 s
->sid_ctx_length
= ctx
->sid_ctx_length
;
289 OPENSSL_assert(s
->sid_ctx_length
<= sizeof s
->sid_ctx
);
290 memcpy(&s
->sid_ctx
, &ctx
->sid_ctx
, sizeof(s
->sid_ctx
));
291 s
->internal
->verify_callback
= ctx
->internal
->default_verify_callback
;
292 s
->internal
->generate_session_id
= ctx
->internal
->generate_session_id
;
294 s
->param
= X509_VERIFY_PARAM_new();
297 X509_VERIFY_PARAM_inherit(s
->param
, ctx
->param
);
298 s
->internal
->quiet_shutdown
= ctx
->internal
->quiet_shutdown
;
299 s
->max_send_fragment
= ctx
->internal
->max_send_fragment
;
301 CRYPTO_add(&ctx
->references
, 1, CRYPTO_LOCK_SSL_CTX
);
303 s
->internal
->tlsext_debug_cb
= 0;
304 s
->internal
->tlsext_debug_arg
= NULL
;
305 s
->internal
->tlsext_ticket_expected
= 0;
306 s
->tlsext_status_type
= -1;
307 s
->internal
->tlsext_status_expected
= 0;
308 s
->internal
->tlsext_ocsp_ids
= NULL
;
309 s
->internal
->tlsext_ocsp_exts
= NULL
;
310 s
->internal
->tlsext_ocsp_resp
= NULL
;
311 s
->internal
->tlsext_ocsp_resplen
= -1;
312 CRYPTO_add(&ctx
->references
, 1, CRYPTO_LOCK_SSL_CTX
);
313 s
->initial_ctx
= ctx
;
315 if (ctx
->internal
->tlsext_ecpointformatlist
!= NULL
) {
316 s
->internal
->tlsext_ecpointformatlist
=
317 calloc(ctx
->internal
->tlsext_ecpointformatlist_length
,
318 sizeof(ctx
->internal
->tlsext_ecpointformatlist
[0]));
319 if (s
->internal
->tlsext_ecpointformatlist
== NULL
)
321 memcpy(s
->internal
->tlsext_ecpointformatlist
,
322 ctx
->internal
->tlsext_ecpointformatlist
,
323 ctx
->internal
->tlsext_ecpointformatlist_length
*
324 sizeof(ctx
->internal
->tlsext_ecpointformatlist
[0]));
325 s
->internal
->tlsext_ecpointformatlist_length
=
326 ctx
->internal
->tlsext_ecpointformatlist_length
;
328 if (ctx
->internal
->tlsext_supportedgroups
!= NULL
) {
329 s
->internal
->tlsext_supportedgroups
=
330 calloc(ctx
->internal
->tlsext_supportedgroups_length
,
331 sizeof(ctx
->internal
->tlsext_supportedgroups
));
332 if (s
->internal
->tlsext_supportedgroups
== NULL
)
334 memcpy(s
->internal
->tlsext_supportedgroups
,
335 ctx
->internal
->tlsext_supportedgroups
,
336 ctx
->internal
->tlsext_supportedgroups_length
*
337 sizeof(ctx
->internal
->tlsext_supportedgroups
[0]));
338 s
->internal
->tlsext_supportedgroups_length
=
339 ctx
->internal
->tlsext_supportedgroups_length
;
342 if (s
->ctx
->internal
->alpn_client_proto_list
!= NULL
) {
343 s
->internal
->alpn_client_proto_list
=
344 malloc(s
->ctx
->internal
->alpn_client_proto_list_len
);
345 if (s
->internal
->alpn_client_proto_list
== NULL
)
347 memcpy(s
->internal
->alpn_client_proto_list
,
348 s
->ctx
->internal
->alpn_client_proto_list
,
349 s
->ctx
->internal
->alpn_client_proto_list_len
);
350 s
->internal
->alpn_client_proto_list_len
=
351 s
->ctx
->internal
->alpn_client_proto_list_len
;
354 s
->verify_result
= X509_V_OK
;
356 s
->method
= ctx
->method
;
358 if (!s
->method
->internal
->ssl_new(s
))
362 s
->server
= (ctx
->method
->internal
->ssl_accept
== ssl_undefined_function
) ? 0 : 1;
366 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->internal
->ex_data
);
372 SSLerrorx(ERR_R_MALLOC_FAILURE
);
377 SSL_CTX_set_session_id_context(SSL_CTX
*ctx
, const unsigned char *sid_ctx
,
378 unsigned int sid_ctx_len
)
380 if (sid_ctx_len
> sizeof ctx
->sid_ctx
) {
381 SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
384 ctx
->sid_ctx_length
= sid_ctx_len
;
385 memcpy(ctx
->sid_ctx
, sid_ctx
, sid_ctx_len
);
391 SSL_set_session_id_context(SSL
*ssl
, const unsigned char *sid_ctx
,
392 unsigned int sid_ctx_len
)
394 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
395 SSLerror(ssl
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
398 ssl
->sid_ctx_length
= sid_ctx_len
;
399 memcpy(ssl
->sid_ctx
, sid_ctx
, sid_ctx_len
);
405 SSL_CTX_set_generate_session_id(SSL_CTX
*ctx
, GEN_SESSION_CB cb
)
407 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX
);
408 ctx
->internal
->generate_session_id
= cb
;
409 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX
);
414 SSL_set_generate_session_id(SSL
*ssl
, GEN_SESSION_CB cb
)
416 CRYPTO_w_lock(CRYPTO_LOCK_SSL
);
417 ssl
->internal
->generate_session_id
= cb
;
418 CRYPTO_w_unlock(CRYPTO_LOCK_SSL
);
423 SSL_has_matching_session_id(const SSL
*ssl
, const unsigned char *id
,
427 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp
428 * shows how we can "construct" a session to give us the desired
429 * check - ie. to find if there's a session in the hash table
430 * that would conflict with any new session built out of this
431 * id/id_len and the ssl_version in use by this SSL.
435 if (id_len
> sizeof r
.session_id
)
438 r
.ssl_version
= ssl
->version
;
439 r
.session_id_length
= id_len
;
440 memcpy(r
.session_id
, id
, id_len
);
442 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX
);
443 p
= lh_SSL_SESSION_retrieve(ssl
->ctx
->internal
->sessions
, &r
);
444 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX
);
449 SSL_CTX_set_purpose(SSL_CTX
*s
, int purpose
)
451 return (X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
));
455 SSL_set_purpose(SSL
*s
, int purpose
)
457 return (X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
));
461 SSL_CTX_set_trust(SSL_CTX
*s
, int trust
)
463 return (X509_VERIFY_PARAM_set_trust(s
->param
, trust
));
467 SSL_set_trust(SSL
*s
, int trust
)
469 return (X509_VERIFY_PARAM_set_trust(s
->param
, trust
));
473 SSL_CTX_set1_param(SSL_CTX
*ctx
, X509_VERIFY_PARAM
*vpm
)
475 return (X509_VERIFY_PARAM_set1(ctx
->param
, vpm
));
479 SSL_set1_param(SSL
*ssl
, X509_VERIFY_PARAM
*vpm
)
481 return (X509_VERIFY_PARAM_set1(ssl
->param
, vpm
));
492 i
= CRYPTO_add(&s
->references
, -1, CRYPTO_LOCK_SSL
);
496 X509_VERIFY_PARAM_free(s
->param
);
498 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->internal
->ex_data
);
500 if (s
->bbio
!= NULL
) {
501 /* If the buffering BIO is in place, pop it off */
502 if (s
->bbio
== s
->wbio
) {
503 s
->wbio
= BIO_pop(s
->wbio
);
509 if (s
->rbio
!= s
->wbio
)
510 BIO_free_all(s
->rbio
);
511 BIO_free_all(s
->wbio
);
513 BUF_MEM_free(s
->internal
->init_buf
);
515 /* add extra stuff */
516 sk_SSL_CIPHER_free(s
->cipher_list
);
517 sk_SSL_CIPHER_free(s
->internal
->cipher_list_by_id
);
519 /* Make the next call work :-) */
520 if (s
->session
!= NULL
) {
521 ssl_clear_bad_session(s
);
522 SSL_SESSION_free(s
->session
);
525 ssl_clear_cipher_ctx(s
);
526 ssl_clear_hash_ctx(&s
->read_hash
);
527 ssl_clear_hash_ctx(&s
->internal
->write_hash
);
529 ssl_cert_free(s
->cert
);
531 free(s
->tlsext_hostname
);
532 SSL_CTX_free(s
->initial_ctx
);
534 free(s
->internal
->tlsext_ecpointformatlist
);
535 free(s
->internal
->tlsext_supportedgroups
);
537 sk_X509_EXTENSION_pop_free(s
->internal
->tlsext_ocsp_exts
,
538 X509_EXTENSION_free
);
539 sk_OCSP_RESPID_pop_free(s
->internal
->tlsext_ocsp_ids
, OCSP_RESPID_free
);
540 free(s
->internal
->tlsext_ocsp_resp
);
542 sk_X509_NAME_pop_free(s
->internal
->client_CA
, X509_NAME_free
);
544 if (s
->method
!= NULL
)
545 s
->method
->internal
->ssl_free(s
);
547 SSL_CTX_free(s
->ctx
);
549 free(s
->internal
->alpn_client_proto_list
);
551 #ifndef OPENSSL_NO_SRTP
552 sk_SRTP_PROTECTION_PROFILE_free(s
->internal
->srtp_profiles
);
560 SSL_set_bio(SSL
*s
, BIO
*rbio
, BIO
*wbio
)
562 /* If the output buffering BIO is still in place, remove it */
563 if (s
->bbio
!= NULL
) {
564 if (s
->wbio
== s
->bbio
) {
565 s
->wbio
= s
->wbio
->next_bio
;
566 s
->bbio
->next_bio
= NULL
;
570 if (s
->rbio
!= rbio
&& s
->rbio
!= s
->wbio
)
571 BIO_free_all(s
->rbio
);
573 BIO_free_all(s
->wbio
);
579 SSL_get_rbio(const SSL
*s
)
585 SSL_get_wbio(const SSL
*s
)
591 SSL_get_fd(const SSL
*s
)
593 return (SSL_get_rfd(s
));
597 SSL_get_rfd(const SSL
*s
)
603 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
610 SSL_get_wfd(const SSL
*s
)
616 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
623 SSL_set_fd(SSL
*s
, int fd
)
628 bio
= BIO_new(BIO_s_socket());
631 SSLerror(s
, ERR_R_BUF_LIB
);
634 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
635 SSL_set_bio(s
, bio
, bio
);
642 SSL_set_wfd(SSL
*s
, int fd
)
647 if ((s
->rbio
== NULL
) || (BIO_method_type(s
->rbio
) != BIO_TYPE_SOCKET
)
648 || ((int)BIO_get_fd(s
->rbio
, NULL
) != fd
)) {
649 bio
= BIO_new(BIO_s_socket());
652 SSLerror(s
, ERR_R_BUF_LIB
);
655 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
656 SSL_set_bio(s
, SSL_get_rbio(s
), bio
);
658 SSL_set_bio(s
, SSL_get_rbio(s
), SSL_get_rbio(s
));
665 SSL_set_rfd(SSL
*s
, int fd
)
670 if ((s
->wbio
== NULL
) || (BIO_method_type(s
->wbio
) != BIO_TYPE_SOCKET
)
671 || ((int)BIO_get_fd(s
->wbio
, NULL
) != fd
)) {
672 bio
= BIO_new(BIO_s_socket());
675 SSLerror(s
, ERR_R_BUF_LIB
);
678 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
679 SSL_set_bio(s
, bio
, SSL_get_wbio(s
));
681 SSL_set_bio(s
, SSL_get_wbio(s
), SSL_get_wbio(s
));
688 /* return length of latest Finished message we sent, copy to 'buf' */
690 SSL_get_finished(const SSL
*s
, void *buf
, size_t count
)
695 ret
= S3I(s
)->tmp
.finish_md_len
;
698 memcpy(buf
, S3I(s
)->tmp
.finish_md
, count
);
703 /* return length of latest Finished message we expected, copy to 'buf' */
705 SSL_get_peer_finished(const SSL
*s
, void *buf
, size_t count
)
710 ret
= S3I(s
)->tmp
.peer_finish_md_len
;
713 memcpy(buf
, S3I(s
)->tmp
.peer_finish_md
, count
);
720 SSL_get_verify_mode(const SSL
*s
)
722 return (s
->verify_mode
);
726 SSL_get_verify_depth(const SSL
*s
)
728 return (X509_VERIFY_PARAM_get_depth(s
->param
));
732 (*SSL_get_verify_callback(const SSL
*s
))(int, X509_STORE_CTX
*)
734 return (s
->internal
->verify_callback
);
738 SSL_CTX_get_verify_mode(const SSL_CTX
*ctx
)
740 return (ctx
->verify_mode
);
744 SSL_CTX_get_verify_depth(const SSL_CTX
*ctx
)
746 return (X509_VERIFY_PARAM_get_depth(ctx
->param
));
749 int (*SSL_CTX_get_verify_callback(const SSL_CTX
*ctx
))(int, X509_STORE_CTX
*)
751 return (ctx
->internal
->default_verify_callback
);
755 SSL_set_verify(SSL
*s
, int mode
,
756 int (*callback
)(int ok
, X509_STORE_CTX
*ctx
))
758 s
->verify_mode
= mode
;
759 if (callback
!= NULL
)
760 s
->internal
->verify_callback
= callback
;
764 SSL_set_verify_depth(SSL
*s
, int depth
)
766 X509_VERIFY_PARAM_set_depth(s
->param
, depth
);
770 SSL_set_read_ahead(SSL
*s
, int yes
)
772 s
->internal
->read_ahead
= yes
;
776 SSL_get_read_ahead(const SSL
*s
)
778 return (s
->internal
->read_ahead
);
782 SSL_pending(const SSL
*s
)
785 * SSL_pending cannot work properly if read-ahead is enabled
786 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
787 * and it is impossible to fix since SSL_pending cannot report
788 * errors that may be observed while scanning the new data.
789 * (Note that SSL_pending() is often used as a boolean value,
790 * so we'd better not return -1.)
792 return (s
->method
->internal
->ssl_pending(s
));
796 SSL_get_peer_certificate(const SSL
*s
)
800 if ((s
== NULL
) || (s
->session
== NULL
))
803 r
= s
->session
->peer
;
808 CRYPTO_add(&r
->references
, 1, CRYPTO_LOCK_X509
);
814 SSL_get_peer_cert_chain(const SSL
*s
)
818 if ((s
== NULL
) || (s
->session
== NULL
) ||
819 (SSI(s
)->sess_cert
== NULL
))
822 r
= SSI(s
)->sess_cert
->cert_chain
;
825 * If we are a client, cert_chain includes the peer's own
827 * if we are a server, it does not.
833 * Now in theory, since the calling process own 't' it should be safe to
834 * modify. We need to be able to read f without being hassled
837 SSL_copy_session_id(SSL
*t
, const SSL
*f
)
841 /* Do we need to to SSL locking? */
842 SSL_set_session(t
, SSL_get_session(f
));
845 * What if we are setup as SSLv2 but want to talk SSLv3 or
848 if (t
->method
!= f
->method
) {
849 t
->method
->internal
->ssl_free(t
); /* cleanup current */
850 t
->method
= f
->method
; /* change method */
851 t
->method
->internal
->ssl_new(t
); /* setup new */
855 if (f
->cert
!= NULL
) {
856 CRYPTO_add(&f
->cert
->references
, 1, CRYPTO_LOCK_SSL_CERT
);
861 SSL_set_session_id_context(t
, f
->sid_ctx
, f
->sid_ctx_length
);
864 /* Fix this so it checks all the valid key/cert options */
866 SSL_CTX_check_private_key(const SSL_CTX
*ctx
)
868 if ((ctx
== NULL
) || (ctx
->internal
->cert
== NULL
) ||
869 (ctx
->internal
->cert
->key
->x509
== NULL
)) {
870 SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED
);
873 if (ctx
->internal
->cert
->key
->privatekey
== NULL
) {
874 SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
877 return (X509_check_private_key(ctx
->internal
->cert
->key
->x509
,
878 ctx
->internal
->cert
->key
->privatekey
));
881 /* Fix this function so that it takes an optional type parameter */
883 SSL_check_private_key(const SSL
*ssl
)
886 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
889 if (ssl
->cert
== NULL
) {
890 SSLerror(ssl
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
893 if (ssl
->cert
->key
->x509
== NULL
) {
894 SSLerror(ssl
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
897 if (ssl
->cert
->key
->privatekey
== NULL
) {
898 SSLerror(ssl
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
901 return (X509_check_private_key(ssl
->cert
->key
->x509
,
902 ssl
->cert
->key
->privatekey
));
908 if (s
->internal
->handshake_func
== NULL
)
909 SSL_set_accept_state(s
); /* Not properly initialized yet */
911 return (s
->method
->internal
->ssl_accept(s
));
917 if (s
->internal
->handshake_func
== NULL
)
918 SSL_set_connect_state(s
); /* Not properly initialized yet */
920 return (s
->method
->internal
->ssl_connect(s
));
924 SSL_get_default_timeout(const SSL
*s
)
926 return (s
->method
->internal
->get_timeout());
930 SSL_read(SSL
*s
, void *buf
, int num
)
932 if (s
->internal
->handshake_func
== NULL
) {
933 SSLerror(s
, SSL_R_UNINITIALIZED
);
937 if (s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
938 s
->internal
->rwstate
= SSL_NOTHING
;
941 return (s
->method
->internal
->ssl_read(s
, buf
, num
));
945 SSL_peek(SSL
*s
, void *buf
, int num
)
947 if (s
->internal
->handshake_func
== NULL
) {
948 SSLerror(s
, SSL_R_UNINITIALIZED
);
952 if (s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
955 return (s
->method
->internal
->ssl_peek(s
, buf
, num
));
959 SSL_write(SSL
*s
, const void *buf
, int num
)
961 if (s
->internal
->handshake_func
== NULL
) {
962 SSLerror(s
, SSL_R_UNINITIALIZED
);
966 if (s
->internal
->shutdown
& SSL_SENT_SHUTDOWN
) {
967 s
->internal
->rwstate
= SSL_NOTHING
;
968 SSLerror(s
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
971 return (s
->method
->internal
->ssl_write(s
, buf
, num
));
978 * Note that this function behaves differently from what one might
979 * expect. Return values are 0 for no success (yet),
980 * 1 for success; but calling it once is usually not enough,
981 * even if blocking I/O is used (see ssl3_shutdown).
984 if (s
->internal
->handshake_func
== NULL
) {
985 SSLerror(s
, SSL_R_UNINITIALIZED
);
989 if ((s
!= NULL
) && !SSL_in_init(s
))
990 return (s
->method
->internal
->ssl_shutdown(s
));
996 SSL_renegotiate(SSL
*s
)
998 if (s
->internal
->renegotiate
== 0)
999 s
->internal
->renegotiate
= 1;
1001 s
->internal
->new_session
= 1;
1003 return (s
->method
->internal
->ssl_renegotiate(s
));
1007 SSL_renegotiate_abbreviated(SSL
*s
)
1009 if (s
->internal
->renegotiate
== 0)
1010 s
->internal
->renegotiate
= 1;
1012 s
->internal
->new_session
= 0;
1014 return (s
->method
->internal
->ssl_renegotiate(s
));
1018 SSL_renegotiate_pending(SSL
*s
)
1021 * Becomes true when negotiation is requested;
1022 * false again once a handshake has finished.
1024 return (s
->internal
->renegotiate
!= 0);
1028 SSL_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
1033 case SSL_CTRL_GET_READ_AHEAD
:
1034 return (s
->internal
->read_ahead
);
1035 case SSL_CTRL_SET_READ_AHEAD
:
1036 l
= s
->internal
->read_ahead
;
1037 s
->internal
->read_ahead
= larg
;
1040 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1041 s
->internal
->msg_callback_arg
= parg
;
1044 case SSL_CTRL_OPTIONS
:
1045 return (s
->internal
->options
|=larg
);
1046 case SSL_CTRL_CLEAR_OPTIONS
:
1047 return (s
->internal
->options
&=~larg
);
1049 return (s
->internal
->mode
|=larg
);
1050 case SSL_CTRL_CLEAR_MODE
:
1051 return (s
->internal
->mode
&=~larg
);
1052 case SSL_CTRL_GET_MAX_CERT_LIST
:
1053 return (s
->internal
->max_cert_list
);
1054 case SSL_CTRL_SET_MAX_CERT_LIST
:
1055 l
= s
->internal
->max_cert_list
;
1056 s
->internal
->max_cert_list
= larg
;
1058 case SSL_CTRL_SET_MTU
:
1059 #ifndef OPENSSL_NO_DTLS1
1060 if (larg
< (long)dtls1_min_mtu())
1063 if (SSL_IS_DTLS(s
)) {
1068 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1069 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
1071 s
->max_send_fragment
= larg
;
1073 case SSL_CTRL_GET_RI_SUPPORT
:
1075 return (S3I(s
)->send_connection_binding
);
1079 return dtls1_ctrl(s
, cmd
, larg
, parg
);
1080 return ssl3_ctrl(s
, cmd
, larg
, parg
);
1085 SSL_callback_ctrl(SSL
*s
, int cmd
, void (*fp
)(void))
1088 case SSL_CTRL_SET_MSG_CALLBACK
:
1089 s
->internal
->msg_callback
= (void (*)(int write_p
, int version
,
1090 int content_type
, const void *buf
, size_t len
,
1091 SSL
*ssl
, void *arg
))(fp
);
1095 return (ssl3_callback_ctrl(s
, cmd
, fp
));
1099 struct lhash_st_SSL_SESSION
*
1100 SSL_CTX_sessions(SSL_CTX
*ctx
)
1102 return (ctx
->internal
->sessions
);
1106 SSL_CTX_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
1111 case SSL_CTRL_GET_READ_AHEAD
:
1112 return (ctx
->internal
->read_ahead
);
1113 case SSL_CTRL_SET_READ_AHEAD
:
1114 l
= ctx
->internal
->read_ahead
;
1115 ctx
->internal
->read_ahead
= larg
;
1118 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1119 ctx
->internal
->msg_callback_arg
= parg
;
1122 case SSL_CTRL_GET_MAX_CERT_LIST
:
1123 return (ctx
->internal
->max_cert_list
);
1124 case SSL_CTRL_SET_MAX_CERT_LIST
:
1125 l
= ctx
->internal
->max_cert_list
;
1126 ctx
->internal
->max_cert_list
= larg
;
1129 case SSL_CTRL_SET_SESS_CACHE_SIZE
:
1130 l
= ctx
->internal
->session_cache_size
;
1131 ctx
->internal
->session_cache_size
= larg
;
1133 case SSL_CTRL_GET_SESS_CACHE_SIZE
:
1134 return (ctx
->internal
->session_cache_size
);
1135 case SSL_CTRL_SET_SESS_CACHE_MODE
:
1136 l
= ctx
->internal
->session_cache_mode
;
1137 ctx
->internal
->session_cache_mode
= larg
;
1139 case SSL_CTRL_GET_SESS_CACHE_MODE
:
1140 return (ctx
->internal
->session_cache_mode
);
1142 case SSL_CTRL_SESS_NUMBER
:
1143 return (lh_SSL_SESSION_num_items(ctx
->internal
->sessions
));
1144 case SSL_CTRL_SESS_CONNECT
:
1145 return (ctx
->internal
->stats
.sess_connect
);
1146 case SSL_CTRL_SESS_CONNECT_GOOD
:
1147 return (ctx
->internal
->stats
.sess_connect_good
);
1148 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE
:
1149 return (ctx
->internal
->stats
.sess_connect_renegotiate
);
1150 case SSL_CTRL_SESS_ACCEPT
:
1151 return (ctx
->internal
->stats
.sess_accept
);
1152 case SSL_CTRL_SESS_ACCEPT_GOOD
:
1153 return (ctx
->internal
->stats
.sess_accept_good
);
1154 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE
:
1155 return (ctx
->internal
->stats
.sess_accept_renegotiate
);
1156 case SSL_CTRL_SESS_HIT
:
1157 return (ctx
->internal
->stats
.sess_hit
);
1158 case SSL_CTRL_SESS_CB_HIT
:
1159 return (ctx
->internal
->stats
.sess_cb_hit
);
1160 case SSL_CTRL_SESS_MISSES
:
1161 return (ctx
->internal
->stats
.sess_miss
);
1162 case SSL_CTRL_SESS_TIMEOUTS
:
1163 return (ctx
->internal
->stats
.sess_timeout
);
1164 case SSL_CTRL_SESS_CACHE_FULL
:
1165 return (ctx
->internal
->stats
.sess_cache_full
);
1166 case SSL_CTRL_OPTIONS
:
1167 return (ctx
->internal
->options
|=larg
);
1168 case SSL_CTRL_CLEAR_OPTIONS
:
1169 return (ctx
->internal
->options
&=~larg
);
1171 return (ctx
->internal
->mode
|=larg
);
1172 case SSL_CTRL_CLEAR_MODE
:
1173 return (ctx
->internal
->mode
&=~larg
);
1174 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1175 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
1177 ctx
->internal
->max_send_fragment
= larg
;
1180 return (ssl3_ctx_ctrl(ctx
, cmd
, larg
, parg
));
1185 SSL_CTX_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
)(void))
1188 case SSL_CTRL_SET_MSG_CALLBACK
:
1189 ctx
->internal
->msg_callback
= (void (*)(int write_p
, int version
,
1190 int content_type
, const void *buf
, size_t len
, SSL
*ssl
,
1195 return (ssl3_ctx_callback_ctrl(ctx
, cmd
, fp
));
1200 ssl_cipher_id_cmp(const SSL_CIPHER
*a
, const SSL_CIPHER
*b
)
1208 return ((l
> 0) ? 1:-1);
1212 ssl_cipher_ptr_id_cmp(const SSL_CIPHER
* const *ap
,
1213 const SSL_CIPHER
* const *bp
)
1217 l
= (*ap
)->id
- (*bp
)->id
;
1221 return ((l
> 0) ? 1:-1);
1225 * Return a STACK of the ciphers available for the SSL and in order of
1228 STACK_OF(SSL_CIPHER
) *
1229 SSL_get_ciphers(const SSL
*s
)
1232 if (s
->cipher_list
!= NULL
) {
1233 return (s
->cipher_list
);
1234 } else if ((s
->ctx
!= NULL
) && (s
->ctx
->cipher_list
!= NULL
)) {
1235 return (s
->ctx
->cipher_list
);
1242 * Return a STACK of the ciphers available for the SSL and in order of
1245 STACK_OF(SSL_CIPHER
) *
1246 ssl_get_ciphers_by_id(SSL
*s
)
1249 if (s
->internal
->cipher_list_by_id
!= NULL
) {
1250 return (s
->internal
->cipher_list_by_id
);
1251 } else if ((s
->ctx
!= NULL
) &&
1252 (s
->ctx
->internal
->cipher_list_by_id
!= NULL
)) {
1253 return (s
->ctx
->internal
->cipher_list_by_id
);
1259 /* See if we have any ECC cipher suites. */
1261 ssl_has_ecc_ciphers(SSL
*s
)
1263 STACK_OF(SSL_CIPHER
) *ciphers
;
1264 unsigned long alg_k
, alg_a
;
1268 if (s
->version
== DTLS1_VERSION
)
1270 if ((ciphers
= SSL_get_ciphers(s
)) == NULL
)
1273 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1274 cipher
= sk_SSL_CIPHER_value(ciphers
, i
);
1276 alg_k
= cipher
->algorithm_mkey
;
1277 alg_a
= cipher
->algorithm_auth
;
1279 if ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
))
1286 /* The old interface to get the same thing as SSL_get_ciphers(). */
1288 SSL_get_cipher_list(const SSL
*s
, int n
)
1291 STACK_OF(SSL_CIPHER
) *sk
;
1295 sk
= SSL_get_ciphers(s
);
1296 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= n
))
1298 c
= sk_SSL_CIPHER_value(sk
, n
);
1304 /* Specify the ciphers to be used by default by the SSL_CTX. */
1306 SSL_CTX_set_cipher_list(SSL_CTX
*ctx
, const char *str
)
1308 STACK_OF(SSL_CIPHER
) *sk
;
1310 sk
= ssl_create_cipher_list(ctx
->method
, &ctx
->cipher_list
,
1311 &ctx
->internal
->cipher_list_by_id
, str
);
1313 * ssl_create_cipher_list may return an empty stack if it
1314 * was unable to find a cipher matching the given rule string
1315 * (for example if the rule string specifies a cipher which
1316 * has been disabled). This is not an error as far as
1317 * ssl_create_cipher_list is concerned, and hence
1318 * ctx->cipher_list and ctx->internal->cipher_list_by_id has been
1323 else if (sk_SSL_CIPHER_num(sk
) == 0) {
1324 SSLerrorx(SSL_R_NO_CIPHER_MATCH
);
1330 /* Specify the ciphers to be used by the SSL. */
1332 SSL_set_cipher_list(SSL
*s
, const char *str
)
1334 STACK_OF(SSL_CIPHER
) *sk
;
1336 sk
= ssl_create_cipher_list(s
->ctx
->method
, &s
->cipher_list
,
1337 &s
->internal
->cipher_list_by_id
, str
);
1338 /* see comment in SSL_CTX_set_cipher_list */
1341 else if (sk_SSL_CIPHER_num(sk
) == 0) {
1342 SSLerror(s
, SSL_R_NO_CIPHER_MATCH
);
1348 /* works well for SSLv2, not so good for SSLv3 */
1350 SSL_get_shared_ciphers(const SSL
*s
, char *buf
, int len
)
1353 STACK_OF(SSL_CIPHER
) *sk
;
1358 if (s
->session
== NULL
|| s
->session
->ciphers
== NULL
|| len
< 2)
1361 sk
= s
->session
->ciphers
;
1362 if (sk_SSL_CIPHER_num(sk
) == 0)
1366 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); i
++) {
1367 c
= sk_SSL_CIPHER_value(sk
, i
);
1369 if (strlcat(buf
, c
->name
, len
) >= len
||
1370 (curlen
= strlcat(buf
, ":", len
)) >= len
) {
1371 /* remove truncated cipher from list */
1376 /* remove trailing colon */
1377 if ((end
= strrchr(buf
, ':')) != NULL
)
1383 ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
, unsigned char *p
,
1384 size_t maxlen
, size_t *outlen
)
1396 if (!CBB_init_fixed(&cbb
, p
, maxlen
))
1399 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); i
++) {
1400 cipher
= sk_SSL_CIPHER_value(sk
, i
);
1402 /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */
1403 if ((cipher
->algorithm_ssl
& SSL_TLSV1_2
) &&
1404 (TLS1_get_client_version(s
) < TLS1_2_VERSION
))
1407 if (!CBB_add_u16(&cbb
, ssl3_cipher_get_value(cipher
)))
1413 /* Add SCSV if there are other ciphers and we're not renegotiating. */
1414 if (ciphers
> 0 && !s
->internal
->renegotiate
) {
1415 if (!CBB_add_u16(&cbb
, SSL3_CK_SCSV
& SSL3_CK_VALUE_MASK
))
1419 if (!CBB_finish(&cbb
, NULL
, outlen
))
1430 STACK_OF(SSL_CIPHER
) *
1431 ssl_bytes_to_cipher_list(SSL
*s
, const unsigned char *p
, int num
)
1434 const SSL_CIPHER
*c
;
1435 STACK_OF(SSL_CIPHER
) *sk
= NULL
;
1436 unsigned long cipher_id
;
1437 uint16_t cipher_value
, max_version
;
1440 S3I(s
)->send_connection_binding
= 0;
1443 * RFC 5246 section 7.4.1.2 defines the interval as [2,2^16-2].
1445 if (num
< 2 || num
> 0x10000 - 2) {
1446 SSLerror(s
, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
1450 if ((sk
= sk_SSL_CIPHER_new_null()) == NULL
) {
1451 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1455 CBS_init(&cbs
, p
, num
);
1456 while (CBS_len(&cbs
) > 0) {
1457 if (!CBS_get_u16(&cbs
, &cipher_value
)) {
1458 SSLerror(s
, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
1462 cipher_id
= SSL3_CK_ID
| cipher_value
;
1464 if (s
->s3
!= NULL
&& cipher_id
== SSL3_CK_SCSV
) {
1466 * TLS_EMPTY_RENEGOTIATION_INFO_SCSV is fatal if
1469 if (s
->internal
->renegotiate
) {
1470 SSLerror(s
, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1471 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1472 SSL_AD_HANDSHAKE_FAILURE
);
1476 S3I(s
)->send_connection_binding
= 1;
1480 if (cipher_id
== SSL3_CK_FALLBACK_SCSV
) {
1482 * TLS_FALLBACK_SCSV indicates that the client
1483 * previously tried a higher protocol version.
1484 * Fail if the current version is an unexpected
1487 max_version
= ssl_max_server_version(s
);
1488 if (max_version
== 0 || s
->version
< max_version
) {
1489 SSLerror(s
, SSL_R_INAPPROPRIATE_FALLBACK
);
1491 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1492 SSL_AD_INAPPROPRIATE_FALLBACK
);
1498 if ((c
= ssl3_get_cipher_by_value(cipher_value
)) != NULL
) {
1499 if (!sk_SSL_CIPHER_push(sk
, c
)) {
1500 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1509 sk_SSL_CIPHER_free(sk
);
1516 * Return a servername extension value if provided in Client Hello, or NULL.
1517 * So far, only host_name types are defined (RFC 3546).
1520 SSL_get_servername(const SSL
*s
, const int type
)
1522 if (type
!= TLSEXT_NAMETYPE_host_name
)
1525 return (s
->session
&& !s
->tlsext_hostname
?
1526 s
->session
->tlsext_hostname
:
1527 s
->tlsext_hostname
);
1531 SSL_get_servername_type(const SSL
*s
)
1534 (!s
->tlsext_hostname
?
1535 s
->session
->tlsext_hostname
: s
->tlsext_hostname
))
1536 return (TLSEXT_NAMETYPE_host_name
);
1541 * SSL_select_next_proto implements standard protocol selection. It is
1542 * expected that this function is called from the callback set by
1543 * SSL_CTX_set_alpn_select_cb.
1545 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1546 * strings. The length byte itself is not included in the length. A byte
1547 * string of length 0 is invalid. No byte string may be truncated.
1549 * It returns either:
1550 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1551 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1554 SSL_select_next_proto(unsigned char **out
, unsigned char *outlen
,
1555 const unsigned char *server
, unsigned int server_len
,
1556 const unsigned char *client
, unsigned int client_len
)
1559 const unsigned char *result
;
1560 int status
= OPENSSL_NPN_UNSUPPORTED
;
1563 * For each protocol in server preference order,
1564 * see if we support it.
1566 for (i
= 0; i
< server_len
; ) {
1567 for (j
= 0; j
< client_len
; ) {
1568 if (server
[i
] == client
[j
] &&
1569 memcmp(&server
[i
+ 1],
1570 &client
[j
+ 1], server
[i
]) == 0) {
1571 /* We found a match */
1572 result
= &server
[i
];
1573 status
= OPENSSL_NPN_NEGOTIATED
;
1583 /* There's no overlap between our protocols and the server's list. */
1585 status
= OPENSSL_NPN_NO_OVERLAP
;
1588 *out
= (unsigned char *) result
+ 1;
1589 *outlen
= result
[0];
1593 /* SSL_get0_next_proto_negotiated is deprecated. */
1595 SSL_get0_next_proto_negotiated(const SSL
*s
, const unsigned char **data
,
1602 /* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1604 SSL_CTX_set_next_protos_advertised_cb(SSL_CTX
*ctx
, int (*cb
) (SSL
*ssl
,
1605 const unsigned char **out
, unsigned int *outlen
, void *arg
), void *arg
)
1609 /* SSL_CTX_set_next_proto_select_cb is deprecated. */
1611 SSL_CTX_set_next_proto_select_cb(SSL_CTX
*ctx
, int (*cb
) (SSL
*s
,
1612 unsigned char **out
, unsigned char *outlen
, const unsigned char *in
,
1613 unsigned int inlen
, void *arg
), void *arg
)
1618 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1619 * protocols, which must be in wire-format (i.e. a series of non-empty,
1620 * 8-bit length-prefixed strings). Returns 0 on success.
1623 SSL_CTX_set_alpn_protos(SSL_CTX
*ctx
, const unsigned char *protos
,
1624 unsigned int protos_len
)
1628 if (protos
== NULL
|| protos_len
== 0)
1631 free(ctx
->internal
->alpn_client_proto_list
);
1632 ctx
->internal
->alpn_client_proto_list
= NULL
;
1633 ctx
->internal
->alpn_client_proto_list_len
= 0;
1635 if ((ctx
->internal
->alpn_client_proto_list
= malloc(protos_len
))
1638 ctx
->internal
->alpn_client_proto_list_len
= protos_len
;
1640 memcpy(ctx
->internal
->alpn_client_proto_list
, protos
, protos_len
);
1645 /* NOTE: Return values are the reverse of what you expect. */
1650 * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1651 * protocols, which must be in wire-format (i.e. a series of non-empty,
1652 * 8-bit length-prefixed strings). Returns 0 on success.
1655 SSL_set_alpn_protos(SSL
*ssl
, const unsigned char *protos
,
1656 unsigned int protos_len
)
1660 if (protos
== NULL
|| protos_len
== 0)
1663 free(ssl
->internal
->alpn_client_proto_list
);
1664 ssl
->internal
->alpn_client_proto_list
= NULL
;
1665 ssl
->internal
->alpn_client_proto_list_len
= 0;
1667 if ((ssl
->internal
->alpn_client_proto_list
= malloc(protos_len
))
1670 ssl
->internal
->alpn_client_proto_list_len
= protos_len
;
1672 memcpy(ssl
->internal
->alpn_client_proto_list
, protos
, protos_len
);
1677 /* NOTE: Return values are the reverse of what you expect. */
1682 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1683 * ClientHello processing in order to select an ALPN protocol from the
1684 * client's list of offered protocols.
1687 SSL_CTX_set_alpn_select_cb(SSL_CTX
* ctx
,
1688 int (*cb
) (SSL
*ssl
, const unsigned char **out
, unsigned char *outlen
,
1689 const unsigned char *in
, unsigned int inlen
, void *arg
), void *arg
)
1691 ctx
->internal
->alpn_select_cb
= cb
;
1692 ctx
->internal
->alpn_select_cb_arg
= arg
;
1696 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1697 * it sets data to point to len bytes of protocol name (not including the
1698 * leading length-prefix byte). If the server didn't respond with* a negotiated
1699 * protocol then len will be zero.
1702 SSL_get0_alpn_selected(const SSL
*ssl
, const unsigned char **data
,
1708 if (ssl
->s3
!= NULL
) {
1709 *data
= ssl
->s3
->internal
->alpn_selected
;
1710 *len
= ssl
->s3
->internal
->alpn_selected_len
;
1715 SSL_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
1716 const char *label
, size_t llen
, const unsigned char *p
, size_t plen
,
1719 return (tls1_export_keying_material(s
, out
, olen
,
1720 label
, llen
, p
, plen
, use_context
));
1723 static unsigned long
1724 ssl_session_hash(const SSL_SESSION
*a
)
1729 ((unsigned int) a
->session_id
[0] )|
1730 ((unsigned int) a
->session_id
[1]<< 8L)|
1731 ((unsigned long)a
->session_id
[2]<<16L)|
1732 ((unsigned long)a
->session_id
[3]<<24L);
1737 * NB: If this function (or indeed the hash function which uses a sort of
1738 * coarser function than this one) is changed, ensure
1739 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1740 * able to construct an SSL_SESSION that will collide with any existing session
1741 * with a matching session ID.
1744 ssl_session_cmp(const SSL_SESSION
*a
, const SSL_SESSION
*b
)
1746 if (a
->ssl_version
!= b
->ssl_version
)
1748 if (a
->session_id_length
!= b
->session_id_length
)
1750 if (timingsafe_memcmp(a
->session_id
, b
->session_id
, a
->session_id_length
) != 0)
1756 * These wrapper functions should remain rather than redeclaring
1757 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1758 * variable. The reason is that the functions aren't static, they're exposed via
1761 static unsigned long
1762 ssl_session_LHASH_HASH(const void *arg
)
1764 const SSL_SESSION
*a
= arg
;
1766 return ssl_session_hash(a
);
1770 ssl_session_LHASH_COMP(const void *arg1
, const void *arg2
)
1772 const SSL_SESSION
*a
= arg1
;
1773 const SSL_SESSION
*b
= arg2
;
1775 return ssl_session_cmp(a
, b
);
1779 SSL_CTX_new(const SSL_METHOD
*meth
)
1784 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED
);
1788 if ((ret
= calloc(1, sizeof(*ret
))) == NULL
) {
1789 SSLerrorx(ERR_R_MALLOC_FAILURE
);
1792 if ((ret
->internal
= calloc(1, sizeof(*ret
->internal
))) == NULL
) {
1794 SSLerrorx(ERR_R_MALLOC_FAILURE
);
1798 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1799 SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS
);
1804 ret
->internal
->min_version
= meth
->internal
->min_version
;
1805 ret
->internal
->max_version
= meth
->internal
->max_version
;
1807 ret
->cert_store
= NULL
;
1808 ret
->internal
->session_cache_mode
= SSL_SESS_CACHE_SERVER
;
1809 ret
->internal
->session_cache_size
= SSL_SESSION_CACHE_MAX_SIZE_DEFAULT
;
1810 ret
->internal
->session_cache_head
= NULL
;
1811 ret
->internal
->session_cache_tail
= NULL
;
1813 /* We take the system default */
1814 ret
->session_timeout
= meth
->internal
->get_timeout();
1816 ret
->internal
->new_session_cb
= 0;
1817 ret
->internal
->remove_session_cb
= 0;
1818 ret
->internal
->get_session_cb
= 0;
1819 ret
->internal
->generate_session_id
= 0;
1821 memset((char *)&ret
->internal
->stats
, 0, sizeof(ret
->internal
->stats
));
1823 ret
->references
= 1;
1824 ret
->internal
->quiet_shutdown
= 0;
1826 ret
->internal
->info_callback
= NULL
;
1828 ret
->internal
->app_verify_callback
= 0;
1829 ret
->internal
->app_verify_arg
= NULL
;
1831 ret
->internal
->max_cert_list
= SSL_MAX_CERT_LIST_DEFAULT
;
1832 ret
->internal
->read_ahead
= 0;
1833 ret
->internal
->msg_callback
= 0;
1834 ret
->internal
->msg_callback_arg
= NULL
;
1835 ret
->verify_mode
= SSL_VERIFY_NONE
;
1836 ret
->sid_ctx_length
= 0;
1837 ret
->internal
->default_verify_callback
= NULL
;
1838 if ((ret
->internal
->cert
= ssl_cert_new()) == NULL
)
1841 ret
->default_passwd_callback
= 0;
1842 ret
->default_passwd_callback_userdata
= NULL
;
1843 ret
->internal
->client_cert_cb
= 0;
1844 ret
->internal
->app_gen_cookie_cb
= 0;
1845 ret
->internal
->app_verify_cookie_cb
= 0;
1847 ret
->internal
->sessions
= lh_SSL_SESSION_new();
1848 if (ret
->internal
->sessions
== NULL
)
1850 ret
->cert_store
= X509_STORE_new();
1851 if (ret
->cert_store
== NULL
)
1854 ssl_create_cipher_list(ret
->method
, &ret
->cipher_list
,
1855 &ret
->internal
->cipher_list_by_id
, SSL_DEFAULT_CIPHER_LIST
);
1856 if (ret
->cipher_list
== NULL
||
1857 sk_SSL_CIPHER_num(ret
->cipher_list
) <= 0) {
1858 SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS
);
1862 ret
->param
= X509_VERIFY_PARAM_new();
1866 if ((ret
->internal
->client_CA
= sk_X509_NAME_new_null()) == NULL
)
1869 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ret
, &ret
->internal
->ex_data
);
1871 ret
->extra_certs
= NULL
;
1873 ret
->internal
->max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
1875 ret
->internal
->tlsext_servername_callback
= 0;
1876 ret
->internal
->tlsext_servername_arg
= NULL
;
1878 /* Setup RFC4507 ticket keys */
1879 arc4random_buf(ret
->internal
->tlsext_tick_key_name
, 16);
1880 arc4random_buf(ret
->internal
->tlsext_tick_hmac_key
, 16);
1881 arc4random_buf(ret
->internal
->tlsext_tick_aes_key
, 16);
1883 ret
->internal
->tlsext_status_cb
= 0;
1884 ret
->internal
->tlsext_status_arg
= NULL
;
1886 #ifndef OPENSSL_NO_ENGINE
1887 ret
->internal
->client_cert_engine
= NULL
;
1888 #ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
1889 #define eng_strx(x) #x
1890 #define eng_str(x) eng_strx(x)
1891 /* Use specific client engine automatically... ignore errors */
1894 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
1897 ENGINE_load_builtin_engines();
1898 eng
= ENGINE_by_id(eng_str(
1899 OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
1901 if (!eng
|| !SSL_CTX_set_client_cert_engine(ret
, eng
))
1907 * Default is to connect to non-RI servers. When RI is more widely
1908 * deployed might change this.
1910 ret
->internal
->options
|= SSL_OP_LEGACY_SERVER_CONNECT
;
1914 SSLerrorx(ERR_R_MALLOC_FAILURE
);
1921 SSL_CTX_free(SSL_CTX
*ctx
)
1928 i
= CRYPTO_add(&ctx
->references
, -1, CRYPTO_LOCK_SSL_CTX
);
1932 X509_VERIFY_PARAM_free(ctx
->param
);
1935 * Free internal session cache. However: the remove_cb() may reference
1936 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1937 * after the sessions were flushed.
1938 * As the ex_data handling routines might also touch the session cache,
1939 * the most secure solution seems to be: empty (flush) the cache, then
1940 * free ex_data, then finally free the cache.
1941 * (See ticket [openssl.org #212].)
1943 if (ctx
->internal
->sessions
!= NULL
)
1944 SSL_CTX_flush_sessions(ctx
, 0);
1946 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ctx
, &ctx
->internal
->ex_data
);
1948 lh_SSL_SESSION_free(ctx
->internal
->sessions
);
1950 X509_STORE_free(ctx
->cert_store
);
1951 sk_SSL_CIPHER_free(ctx
->cipher_list
);
1952 sk_SSL_CIPHER_free(ctx
->internal
->cipher_list_by_id
);
1953 ssl_cert_free(ctx
->internal
->cert
);
1954 sk_X509_NAME_pop_free(ctx
->internal
->client_CA
, X509_NAME_free
);
1955 sk_X509_pop_free(ctx
->extra_certs
, X509_free
);
1957 #ifndef OPENSSL_NO_SRTP
1958 if (ctx
->internal
->srtp_profiles
)
1959 sk_SRTP_PROTECTION_PROFILE_free(ctx
->internal
->srtp_profiles
);
1962 #ifndef OPENSSL_NO_ENGINE
1963 if (ctx
->internal
->client_cert_engine
)
1964 ENGINE_finish(ctx
->internal
->client_cert_engine
);
1967 free(ctx
->internal
->tlsext_ecpointformatlist
);
1968 free(ctx
->internal
->tlsext_supportedgroups
);
1970 free(ctx
->internal
->alpn_client_proto_list
);
1972 free(ctx
->internal
);
1977 SSL_CTX_set_default_passwd_cb(SSL_CTX
*ctx
, pem_password_cb
*cb
)
1979 ctx
->default_passwd_callback
= cb
;
1983 SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX
*ctx
, void *u
)
1985 ctx
->default_passwd_callback_userdata
= u
;
1989 SSL_CTX_set_cert_verify_callback(SSL_CTX
*ctx
, int (*cb
)(X509_STORE_CTX
*,
1992 ctx
->internal
->app_verify_callback
= cb
;
1993 ctx
->internal
->app_verify_arg
= arg
;
1997 SSL_CTX_set_verify(SSL_CTX
*ctx
, int mode
, int (*cb
)(int, X509_STORE_CTX
*))
1999 ctx
->verify_mode
= mode
;
2000 ctx
->internal
->default_verify_callback
= cb
;
2004 SSL_CTX_set_verify_depth(SSL_CTX
*ctx
, int depth
)
2006 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
2010 ssl_set_cert_masks(CERT
*c
, const SSL_CIPHER
*cipher
)
2012 int rsa_enc
, rsa_sign
, dh_tmp
;
2014 unsigned long mask_k
, mask_a
;
2021 dh_tmp
= (c
->dh_tmp
!= NULL
|| c
->dh_tmp_cb
!= NULL
||
2022 c
->dh_tmp_auto
!= 0);
2024 cpk
= &(c
->pkeys
[SSL_PKEY_RSA_ENC
]);
2025 rsa_enc
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
2026 cpk
= &(c
->pkeys
[SSL_PKEY_RSA_SIGN
]);
2027 rsa_sign
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
2028 cpk
= &(c
->pkeys
[SSL_PKEY_ECC
]);
2029 have_ecc_cert
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
2034 cpk
= &(c
->pkeys
[SSL_PKEY_GOST01
]);
2035 if (cpk
->x509
!= NULL
&& cpk
->privatekey
!=NULL
) {
2036 mask_k
|= SSL_kGOST
;
2037 mask_a
|= SSL_aGOST01
;
2046 if (rsa_enc
|| rsa_sign
)
2049 mask_a
|= SSL_aNULL
;
2052 * An ECC certificate may be usable for ECDH and/or
2053 * ECDSA cipher suites depending on the key usage extension.
2055 if (have_ecc_cert
) {
2056 x
= (c
->pkeys
[SSL_PKEY_ECC
]).x509
;
2058 /* This call populates extension flags (ex_flags). */
2059 X509_check_purpose(x
, -1, 0);
2061 /* Key usage, if present, must allow signing. */
2062 if ((x
->ex_flags
& EXFLAG_KUSAGE
) == 0 ||
2063 (x
->ex_kusage
& X509v3_KU_DIGITAL_SIGNATURE
))
2064 mask_a
|= SSL_aECDSA
;
2067 mask_k
|= SSL_kECDHE
;
2074 /* See if this handshake is using an ECC cipher suite. */
2076 ssl_using_ecc_cipher(SSL
*s
)
2078 unsigned long alg_a
, alg_k
;
2080 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
2081 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2083 return SSI(s
)->tlsext_ecpointformatlist
!= NULL
&&
2084 SSI(s
)->tlsext_ecpointformatlist_length
> 0 &&
2085 ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
));
2089 ssl_check_srvr_ecc_cert_and_alg(X509
*x
, SSL
*s
)
2091 const SSL_CIPHER
*cs
= S3I(s
)->hs
.new_cipher
;
2092 unsigned long alg_a
;
2094 alg_a
= cs
->algorithm_auth
;
2096 if (alg_a
& SSL_aECDSA
) {
2097 /* This call populates extension flags (ex_flags). */
2098 X509_check_purpose(x
, -1, 0);
2100 /* Key usage, if present, must allow signing. */
2101 if ((x
->ex_flags
& EXFLAG_KUSAGE
) &&
2102 ((x
->ex_kusage
& X509v3_KU_DIGITAL_SIGNATURE
) == 0)) {
2103 SSLerror(s
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
2112 ssl_get_server_send_pkey(const SSL
*s
)
2114 unsigned long alg_a
;
2119 ssl_set_cert_masks(c
, S3I(s
)->hs
.new_cipher
);
2121 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
2123 if (alg_a
& SSL_aECDSA
) {
2125 } else if (alg_a
& SSL_aRSA
) {
2126 if (c
->pkeys
[SSL_PKEY_RSA_ENC
].x509
== NULL
)
2127 i
= SSL_PKEY_RSA_SIGN
;
2129 i
= SSL_PKEY_RSA_ENC
;
2130 } else if (alg_a
& SSL_aGOST01
) {
2131 i
= SSL_PKEY_GOST01
;
2132 } else { /* if (alg_a & SSL_aNULL) */
2133 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2137 return (c
->pkeys
+ i
);
2141 ssl_get_server_send_cert(const SSL
*s
)
2145 cpk
= ssl_get_server_send_pkey(s
);
2152 ssl_get_sign_pkey(SSL
*s
, const SSL_CIPHER
*cipher
, const EVP_MD
**pmd
)
2154 unsigned long alg_a
;
2158 alg_a
= cipher
->algorithm_auth
;
2161 if (alg_a
& SSL_aRSA
) {
2162 if (c
->pkeys
[SSL_PKEY_RSA_SIGN
].privatekey
!= NULL
)
2163 idx
= SSL_PKEY_RSA_SIGN
;
2164 else if (c
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
!= NULL
)
2165 idx
= SSL_PKEY_RSA_ENC
;
2166 } else if ((alg_a
& SSL_aECDSA
) &&
2167 (c
->pkeys
[SSL_PKEY_ECC
].privatekey
!= NULL
))
2170 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2174 *pmd
= c
->pkeys
[idx
].digest
;
2175 return (c
->pkeys
[idx
].privatekey
);
2179 ssl_get_auto_dh(SSL
*s
)
2185 if (s
->cert
->dh_tmp_auto
== 2) {
2187 } else if (S3I(s
)->hs
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
2189 if (S3I(s
)->hs
.new_cipher
->strength_bits
== 256)
2192 if ((cpk
= ssl_get_server_send_pkey(s
)) == NULL
)
2194 if (cpk
->privatekey
== NULL
|| cpk
->privatekey
->pkey
.dh
== NULL
)
2196 keylen
= EVP_PKEY_bits(cpk
->privatekey
);
2199 if ((dhp
= DH_new()) == NULL
)
2204 BN_set_word(dhp
->g
, 2);
2207 dhp
->p
= get_rfc3526_prime_8192(NULL
);
2208 else if (keylen
>= 4096)
2209 dhp
->p
= get_rfc3526_prime_4096(NULL
);
2210 else if (keylen
>= 3072)
2211 dhp
->p
= get_rfc3526_prime_3072(NULL
);
2212 else if (keylen
>= 2048)
2213 dhp
->p
= get_rfc3526_prime_2048(NULL
);
2214 else if (keylen
>= 1536)
2215 dhp
->p
= get_rfc3526_prime_1536(NULL
);
2217 dhp
->p
= get_rfc2409_prime_1024(NULL
);
2219 if (dhp
->p
== NULL
|| dhp
->g
== NULL
) {
2227 ssl_update_cache(SSL
*s
, int mode
)
2232 * If the session_id_length is 0, we are not supposed to cache it,
2233 * and it would be rather hard to do anyway :-)
2235 if (s
->session
->session_id_length
== 0)
2238 i
= s
->session_ctx
->internal
->session_cache_mode
;
2239 if ((i
& mode
) && (!s
->internal
->hit
) && ((i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
)
2240 || SSL_CTX_add_session(s
->session_ctx
, s
->session
))
2241 && (s
->session_ctx
->internal
->new_session_cb
!= NULL
)) {
2242 CRYPTO_add(&s
->session
->references
, 1, CRYPTO_LOCK_SSL_SESSION
);
2243 if (!s
->session_ctx
->internal
->new_session_cb(s
, s
->session
))
2244 SSL_SESSION_free(s
->session
);
2247 /* auto flush every 255 connections */
2248 if ((!(i
& SSL_SESS_CACHE_NO_AUTO_CLEAR
)) &&
2249 ((i
& mode
) == mode
)) {
2250 if ((((mode
& SSL_SESS_CACHE_CLIENT
) ?
2251 s
->session_ctx
->internal
->stats
.sess_connect_good
:
2252 s
->session_ctx
->internal
->stats
.sess_accept_good
) & 0xff) == 0xff) {
2253 SSL_CTX_flush_sessions(s
->session_ctx
, time(NULL
));
2259 SSL_get_ssl_method(SSL
*s
)
2265 SSL_set_ssl_method(SSL
*s
, const SSL_METHOD
*meth
)
2270 if (s
->method
!= meth
) {
2271 if (s
->internal
->handshake_func
!= NULL
)
2272 conn
= (s
->internal
->handshake_func
== s
->method
->internal
->ssl_connect
);
2274 if (s
->method
->internal
->version
== meth
->internal
->version
)
2277 s
->method
->internal
->ssl_free(s
);
2279 ret
= s
->method
->internal
->ssl_new(s
);
2283 s
->internal
->handshake_func
= meth
->internal
->ssl_connect
;
2285 s
->internal
->handshake_func
= meth
->internal
->ssl_accept
;
2291 SSL_get_error(const SSL
*s
, int i
)
2298 return (SSL_ERROR_NONE
);
2300 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2301 * etc, where we do encode the error */
2302 if ((l
= ERR_peek_error()) != 0) {
2303 if (ERR_GET_LIB(l
) == ERR_LIB_SYS
)
2304 return (SSL_ERROR_SYSCALL
);
2306 return (SSL_ERROR_SSL
);
2309 if ((i
< 0) && SSL_want_read(s
)) {
2310 bio
= SSL_get_rbio(s
);
2311 if (BIO_should_read(bio
)) {
2312 return (SSL_ERROR_WANT_READ
);
2313 } else if (BIO_should_write(bio
)) {
2315 * This one doesn't make too much sense... We never
2316 * try to write to the rbio, and an application
2317 * program where rbio and wbio are separate couldn't
2318 * even know what it should wait for. However if we
2319 * ever set s->internal->rwstate incorrectly (so that we have
2320 * SSL_want_read(s) instead of SSL_want_write(s))
2321 * and rbio and wbio *are* the same, this test works
2322 * around that bug; so it might be safer to keep it.
2324 return (SSL_ERROR_WANT_WRITE
);
2325 } else if (BIO_should_io_special(bio
)) {
2326 reason
= BIO_get_retry_reason(bio
);
2327 if (reason
== BIO_RR_CONNECT
)
2328 return (SSL_ERROR_WANT_CONNECT
);
2329 else if (reason
== BIO_RR_ACCEPT
)
2330 return (SSL_ERROR_WANT_ACCEPT
);
2332 return (SSL_ERROR_SYSCALL
); /* unknown */
2336 if ((i
< 0) && SSL_want_write(s
)) {
2337 bio
= SSL_get_wbio(s
);
2338 if (BIO_should_write(bio
)) {
2339 return (SSL_ERROR_WANT_WRITE
);
2340 } else if (BIO_should_read(bio
)) {
2342 * See above (SSL_want_read(s) with
2343 * BIO_should_write(bio))
2345 return (SSL_ERROR_WANT_READ
);
2346 } else if (BIO_should_io_special(bio
)) {
2347 reason
= BIO_get_retry_reason(bio
);
2348 if (reason
== BIO_RR_CONNECT
)
2349 return (SSL_ERROR_WANT_CONNECT
);
2350 else if (reason
== BIO_RR_ACCEPT
)
2351 return (SSL_ERROR_WANT_ACCEPT
);
2353 return (SSL_ERROR_SYSCALL
);
2356 if ((i
< 0) && SSL_want_x509_lookup(s
)) {
2357 return (SSL_ERROR_WANT_X509_LOOKUP
);
2361 if ((s
->internal
->shutdown
& SSL_RECEIVED_SHUTDOWN
) &&
2362 (S3I(s
)->warn_alert
== SSL_AD_CLOSE_NOTIFY
))
2363 return (SSL_ERROR_ZERO_RETURN
);
2365 return (SSL_ERROR_SYSCALL
);
2369 SSL_do_handshake(SSL
*s
)
2373 if (s
->internal
->handshake_func
== NULL
) {
2374 SSLerror(s
, SSL_R_CONNECTION_TYPE_NOT_SET
);
2378 s
->method
->internal
->ssl_renegotiate_check(s
);
2380 if (SSL_in_init(s
) || SSL_in_before(s
)) {
2381 ret
= s
->internal
->handshake_func(s
);
2387 * For the next 2 functions, SSL_clear() sets shutdown and so
2388 * one of these calls will reset it
2391 SSL_set_accept_state(SSL
*s
)
2394 s
->internal
->shutdown
= 0;
2395 S3I(s
)->hs
.state
= SSL_ST_ACCEPT
|SSL_ST_BEFORE
;
2396 s
->internal
->handshake_func
= s
->method
->internal
->ssl_accept
;
2397 /* clear the current cipher */
2398 ssl_clear_cipher_ctx(s
);
2399 ssl_clear_hash_ctx(&s
->read_hash
);
2400 ssl_clear_hash_ctx(&s
->internal
->write_hash
);
2404 SSL_set_connect_state(SSL
*s
)
2407 s
->internal
->shutdown
= 0;
2408 S3I(s
)->hs
.state
= SSL_ST_CONNECT
|SSL_ST_BEFORE
;
2409 s
->internal
->handshake_func
= s
->method
->internal
->ssl_connect
;
2410 /* clear the current cipher */
2411 ssl_clear_cipher_ctx(s
);
2412 ssl_clear_hash_ctx(&s
->read_hash
);
2413 ssl_clear_hash_ctx(&s
->internal
->write_hash
);
2417 ssl_undefined_function(SSL
*s
)
2419 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2424 ssl_undefined_void_function(void)
2426 SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2431 ssl_undefined_const_function(const SSL
*s
)
2433 SSLerror(s
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2438 ssl_version_string(int ver
)
2442 return (SSL_TXT_DTLS1
);
2444 return (SSL_TXT_TLSV1
);
2445 case TLS1_1_VERSION
:
2446 return (SSL_TXT_TLSV1_1
);
2447 case TLS1_2_VERSION
:
2448 return (SSL_TXT_TLSV1_2
);
2455 SSL_get_version(const SSL
*s
)
2457 return ssl_version_string(s
->version
);
2463 STACK_OF(X509_NAME
) *sk
;
2468 if ((ret
= SSL_new(SSL_get_SSL_CTX(s
))) == NULL
)
2471 ret
->version
= s
->version
;
2472 ret
->internal
->type
= s
->internal
->type
;
2473 ret
->method
= s
->method
;
2475 if (s
->session
!= NULL
) {
2476 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2477 SSL_copy_session_id(ret
, s
);
2480 * No session has been established yet, so we have to expect
2481 * that s->cert or ret->cert will be changed later --
2482 * they should not both point to the same object,
2483 * and thus we can't use SSL_copy_session_id.
2486 ret
->method
->internal
->ssl_free(ret
);
2487 ret
->method
= s
->method
;
2488 ret
->method
->internal
->ssl_new(ret
);
2490 if (s
->cert
!= NULL
) {
2491 ssl_cert_free(ret
->cert
);
2492 ret
->cert
= ssl_cert_dup(s
->cert
);
2493 if (ret
->cert
== NULL
)
2497 SSL_set_session_id_context(ret
,
2498 s
->sid_ctx
, s
->sid_ctx_length
);
2501 ret
->internal
->options
= s
->internal
->options
;
2502 ret
->internal
->mode
= s
->internal
->mode
;
2503 SSL_set_max_cert_list(ret
, SSL_get_max_cert_list(s
));
2504 SSL_set_read_ahead(ret
, SSL_get_read_ahead(s
));
2505 ret
->internal
->msg_callback
= s
->internal
->msg_callback
;
2506 ret
->internal
->msg_callback_arg
= s
->internal
->msg_callback_arg
;
2507 SSL_set_verify(ret
, SSL_get_verify_mode(s
),
2508 SSL_get_verify_callback(s
));
2509 SSL_set_verify_depth(ret
, SSL_get_verify_depth(s
));
2510 ret
->internal
->generate_session_id
= s
->internal
->generate_session_id
;
2512 SSL_set_info_callback(ret
, SSL_get_info_callback(s
));
2514 ret
->internal
->debug
= s
->internal
->debug
;
2516 /* copy app data, a little dangerous perhaps */
2517 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL
,
2518 &ret
->internal
->ex_data
, &s
->internal
->ex_data
))
2521 /* setup rbio, and wbio */
2522 if (s
->rbio
!= NULL
) {
2523 if (!BIO_dup_state(s
->rbio
,(char *)&ret
->rbio
))
2526 if (s
->wbio
!= NULL
) {
2527 if (s
->wbio
!= s
->rbio
) {
2528 if (!BIO_dup_state(s
->wbio
,(char *)&ret
->wbio
))
2531 ret
->wbio
= ret
->rbio
;
2533 ret
->internal
->rwstate
= s
->internal
->rwstate
;
2534 ret
->internal
->in_handshake
= s
->internal
->in_handshake
;
2535 ret
->internal
->handshake_func
= s
->internal
->handshake_func
;
2536 ret
->server
= s
->server
;
2537 ret
->internal
->renegotiate
= s
->internal
->renegotiate
;
2538 ret
->internal
->new_session
= s
->internal
->new_session
;
2539 ret
->internal
->quiet_shutdown
= s
->internal
->quiet_shutdown
;
2540 ret
->internal
->shutdown
= s
->internal
->shutdown
;
2541 /* SSL_dup does not really work at any state, though */
2542 S3I(ret
)->hs
.state
= S3I(s
)->hs
.state
;
2543 ret
->internal
->rstate
= s
->internal
->rstate
;
2546 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2549 ret
->internal
->init_num
= 0;
2551 ret
->internal
->hit
= s
->internal
->hit
;
2553 X509_VERIFY_PARAM_inherit(ret
->param
, s
->param
);
2555 /* dup the cipher_list and cipher_list_by_id stacks */
2556 if (s
->cipher_list
!= NULL
) {
2557 if ((ret
->cipher_list
=
2558 sk_SSL_CIPHER_dup(s
->cipher_list
)) == NULL
)
2561 if (s
->internal
->cipher_list_by_id
!= NULL
) {
2562 if ((ret
->internal
->cipher_list_by_id
=
2563 sk_SSL_CIPHER_dup(s
->internal
->cipher_list_by_id
)) == NULL
)
2567 /* Dup the client_CA list */
2568 if (s
->internal
->client_CA
!= NULL
) {
2569 if ((sk
= sk_X509_NAME_dup(s
->internal
->client_CA
)) == NULL
) goto err
;
2570 ret
->internal
->client_CA
= sk
;
2571 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
2572 xn
= sk_X509_NAME_value(sk
, i
);
2573 if (sk_X509_NAME_set(sk
, i
,
2574 X509_NAME_dup(xn
)) == NULL
) {
2591 ssl_clear_cipher_ctx(SSL
*s
)
2593 EVP_CIPHER_CTX_free(s
->enc_read_ctx
);
2594 s
->enc_read_ctx
= NULL
;
2595 EVP_CIPHER_CTX_free(s
->internal
->enc_write_ctx
);
2596 s
->internal
->enc_write_ctx
= NULL
;
2598 if (s
->internal
->aead_read_ctx
!= NULL
) {
2599 EVP_AEAD_CTX_cleanup(&s
->internal
->aead_read_ctx
->ctx
);
2600 free(s
->internal
->aead_read_ctx
);
2601 s
->internal
->aead_read_ctx
= NULL
;
2603 if (s
->internal
->aead_write_ctx
!= NULL
) {
2604 EVP_AEAD_CTX_cleanup(&s
->internal
->aead_write_ctx
->ctx
);
2605 free(s
->internal
->aead_write_ctx
);
2606 s
->internal
->aead_write_ctx
= NULL
;
2611 /* Fix this function so that it takes an optional type parameter */
2613 SSL_get_certificate(const SSL
*s
)
2615 if (s
->cert
!= NULL
)
2616 return (s
->cert
->key
->x509
);
2621 /* Fix this function so that it takes an optional type parameter */
2623 SSL_get_privatekey(SSL
*s
)
2625 if (s
->cert
!= NULL
)
2626 return (s
->cert
->key
->privatekey
);
2632 SSL_get_current_cipher(const SSL
*s
)
2634 if ((s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
))
2635 return (s
->session
->cipher
);
2639 SSL_get_current_compression(SSL
*s
)
2645 SSL_get_current_expansion(SSL
*s
)
2651 ssl_init_wbio_buffer(SSL
*s
, int push
)
2655 if (s
->bbio
== NULL
) {
2656 bbio
= BIO_new(BIO_f_buffer());
2662 if (s
->bbio
== s
->wbio
)
2663 s
->wbio
= BIO_pop(s
->wbio
);
2665 (void)BIO_reset(bbio
);
2666 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2667 if (!BIO_set_read_buffer_size(bbio
, 1)) {
2668 SSLerror(s
, ERR_R_BUF_LIB
);
2672 if (s
->wbio
!= bbio
)
2673 s
->wbio
= BIO_push(bbio
, s
->wbio
);
2675 if (s
->wbio
== bbio
)
2676 s
->wbio
= BIO_pop(bbio
);
2682 ssl_free_wbio_buffer(SSL
*s
)
2687 if (s
->bbio
== NULL
)
2690 if (s
->bbio
== s
->wbio
) {
2691 /* remove buffering */
2692 s
->wbio
= BIO_pop(s
->wbio
);
2699 SSL_CTX_set_quiet_shutdown(SSL_CTX
*ctx
, int mode
)
2701 ctx
->internal
->quiet_shutdown
= mode
;
2705 SSL_CTX_get_quiet_shutdown(const SSL_CTX
*ctx
)
2707 return (ctx
->internal
->quiet_shutdown
);
2711 SSL_set_quiet_shutdown(SSL
*s
, int mode
)
2713 s
->internal
->quiet_shutdown
= mode
;
2717 SSL_get_quiet_shutdown(const SSL
*s
)
2719 return (s
->internal
->quiet_shutdown
);
2723 SSL_set_shutdown(SSL
*s
, int mode
)
2725 s
->internal
->shutdown
= mode
;
2729 SSL_get_shutdown(const SSL
*s
)
2731 return (s
->internal
->shutdown
);
2735 SSL_version(const SSL
*s
)
2737 return (s
->version
);
2741 SSL_get_SSL_CTX(const SSL
*ssl
)
2747 SSL_set_SSL_CTX(SSL
*ssl
, SSL_CTX
* ctx
)
2749 CERT
*ocert
= ssl
->cert
;
2751 if (ssl
->ctx
== ctx
)
2754 ctx
= ssl
->initial_ctx
;
2755 ssl
->cert
= ssl_cert_dup(ctx
->internal
->cert
);
2756 if (ocert
!= NULL
) {
2758 /* Copy negotiated digests from original certificate. */
2759 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
2760 ssl
->cert
->pkeys
[i
].digest
= ocert
->pkeys
[i
].digest
;
2761 ssl_cert_free(ocert
);
2763 CRYPTO_add(&ctx
->references
, 1, CRYPTO_LOCK_SSL_CTX
);
2764 SSL_CTX_free(ssl
->ctx
); /* decrement reference count */
2770 SSL_CTX_set_default_verify_paths(SSL_CTX
*ctx
)
2772 return (X509_STORE_set_default_paths(ctx
->cert_store
));
2776 SSL_CTX_load_verify_locations(SSL_CTX
*ctx
, const char *CAfile
,
2779 return (X509_STORE_load_locations(ctx
->cert_store
, CAfile
, CApath
));
2783 SSL_CTX_load_verify_mem(SSL_CTX
*ctx
, void *buf
, int len
)
2785 return (X509_STORE_load_mem(ctx
->cert_store
, buf
, len
));
2789 SSL_set_info_callback(SSL
*ssl
, void (*cb
)(const SSL
*ssl
, int type
, int val
))
2791 ssl
->internal
->info_callback
= cb
;
2794 void (*SSL_get_info_callback(const SSL
*ssl
))(const SSL
*ssl
, int type
, int val
)
2796 return (ssl
->internal
->info_callback
);
2800 SSL_state(const SSL
*ssl
)
2802 return (S3I(ssl
)->hs
.state
);
2806 SSL_set_state(SSL
*ssl
, int state
)
2808 S3I(ssl
)->hs
.state
= state
;
2812 SSL_set_verify_result(SSL
*ssl
, long arg
)
2814 ssl
->verify_result
= arg
;
2818 SSL_get_verify_result(const SSL
*ssl
)
2820 return (ssl
->verify_result
);
2824 SSL_get_ex_new_index(long argl
, void *argp
, CRYPTO_EX_new
*new_func
,
2825 CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
2827 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL
, argl
, argp
,
2828 new_func
, dup_func
, free_func
));
2832 SSL_set_ex_data(SSL
*s
, int idx
, void *arg
)
2834 return (CRYPTO_set_ex_data(&s
->internal
->ex_data
, idx
, arg
));
2838 SSL_get_ex_data(const SSL
*s
, int idx
)
2840 return (CRYPTO_get_ex_data(&s
->internal
->ex_data
, idx
));
2844 SSL_CTX_get_ex_new_index(long argl
, void *argp
, CRYPTO_EX_new
*new_func
,
2845 CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
2847 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX
, argl
, argp
,
2848 new_func
, dup_func
, free_func
));
2852 SSL_CTX_set_ex_data(SSL_CTX
*s
, int idx
, void *arg
)
2854 return (CRYPTO_set_ex_data(&s
->internal
->ex_data
, idx
, arg
));
2858 SSL_CTX_get_ex_data(const SSL_CTX
*s
, int idx
)
2860 return (CRYPTO_get_ex_data(&s
->internal
->ex_data
, idx
));
2870 SSL_CTX_get_cert_store(const SSL_CTX
*ctx
)
2872 return (ctx
->cert_store
);
2876 SSL_CTX_set_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
2878 X509_STORE_free(ctx
->cert_store
);
2879 ctx
->cert_store
= store
;
2883 SSL_want(const SSL
*s
)
2885 return (s
->internal
->rwstate
);
2889 SSL_CTX_set_tmp_rsa_callback(SSL_CTX
*ctx
, RSA
*(*cb
)(SSL
*ssl
, int is_export
,
2892 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_RSA_CB
,(void (*)(void))cb
);
2896 SSL_set_tmp_rsa_callback(SSL
*ssl
, RSA
*(*cb
)(SSL
*ssl
, int is_export
,
2899 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_RSA_CB
,(void (*)(void))cb
);
2903 SSL_CTX_set_tmp_dh_callback(SSL_CTX
*ctx
, DH
*(*dh
)(SSL
*ssl
, int is_export
,
2906 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_DH_CB
,(void (*)(void))dh
);
2910 SSL_set_tmp_dh_callback(SSL
*ssl
, DH
*(*dh
)(SSL
*ssl
, int is_export
,
2913 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_DH_CB
,(void (*)(void))dh
);
2917 SSL_CTX_set_tmp_ecdh_callback(SSL_CTX
*ctx
, EC_KEY
*(*ecdh
)(SSL
*ssl
,
2918 int is_export
, int keylength
))
2920 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_ECDH_CB
,
2921 (void (*)(void))ecdh
);
2925 SSL_set_tmp_ecdh_callback(SSL
*ssl
, EC_KEY
*(*ecdh
)(SSL
*ssl
, int is_export
,
2928 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_ECDH_CB
,(void (*)(void))ecdh
);
2933 SSL_CTX_set_msg_callback(SSL_CTX
*ctx
, void (*cb
)(int write_p
, int version
,
2934 int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))
2936 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_MSG_CALLBACK
,
2937 (void (*)(void))cb
);
2941 SSL_set_msg_callback(SSL
*ssl
, void (*cb
)(int write_p
, int version
,
2942 int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))
2944 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
2948 ssl_clear_hash_ctx(EVP_MD_CTX
**hash
)
2951 EVP_MD_CTX_destroy(*hash
);
2956 SSL_set_debug(SSL
*s
, int debug
)
2958 s
->internal
->debug
= debug
;
2962 SSL_cache_hit(SSL
*s
)
2964 return (s
->internal
->hit
);
2968 SSL_CTX_set_min_proto_version(SSL_CTX
*ctx
, uint16_t version
)
2970 return ssl_version_set_min(ctx
->method
, version
,
2971 ctx
->internal
->max_version
, &ctx
->internal
->min_version
);
2975 SSL_CTX_set_max_proto_version(SSL_CTX
*ctx
, uint16_t version
)
2977 return ssl_version_set_max(ctx
->method
, version
,
2978 ctx
->internal
->min_version
, &ctx
->internal
->max_version
);
2982 SSL_set_min_proto_version(SSL
*ssl
, uint16_t version
)
2984 return ssl_version_set_min(ssl
->method
, version
,
2985 ssl
->internal
->max_version
, &ssl
->internal
->min_version
);
2989 SSL_set_max_proto_version(SSL
*ssl
, uint16_t version
)
2991 return ssl_version_set_max(ssl
->method
, version
,
2992 ssl
->internal
->min_version
, &ssl
->internal
->max_version
);
2996 ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_
, const void *b_
)
2998 SSL_CIPHER
const *a
= a_
;
2999 SSL_CIPHER
const *b
= b_
;
3000 return ssl_cipher_id_cmp(a
, b
);
3004 OBJ_bsearch_ssl_cipher_id(SSL_CIPHER
*key
, SSL_CIPHER
const *base
, int num
)
3006 return (SSL_CIPHER
*)OBJ_bsearch_(key
, base
, num
, sizeof(SSL_CIPHER
),
3007 ssl_cipher_id_cmp_BSEARCH_CMP_FN
);