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.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
152 #include "ssl_locl.h"
153 #include <openssl/buffer.h>
154 #include <openssl/rand.h>
155 #include <openssl/objects.h>
156 #include <openssl/evp.h>
157 #include <openssl/md5.h>
158 #ifndef OPENSSL_NO_DH
159 # include <openssl/dh.h>
161 #include <openssl/bn.h>
162 #ifndef OPENSSL_NO_ENGINE
163 # include <openssl/engine.h>
166 static int ssl_set_version(SSL
*s
);
167 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
);
168 #ifndef OPENSSL_NO_TLSEXT
169 static int ssl3_check_finished(SSL
*s
);
171 static int ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
,
173 int (*put_cb
) (const SSL_CIPHER
*,
177 int ssl3_connect(SSL
*s
)
180 unsigned long Time
= (unsigned long)time(NULL
);
181 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
183 int new_state
, state
, skip
= 0;
185 RAND_add(&Time
, sizeof(Time
), 0);
189 if (s
->info_callback
!= NULL
)
190 cb
= s
->info_callback
;
191 else if (s
->ctx
->info_callback
!= NULL
)
192 cb
= s
->ctx
->info_callback
;
195 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
200 #ifndef OPENSSL_NO_HEARTBEATS
202 * If we're awaiting a HeartbeatResponse, pretend we already got and
203 * don't await it anymore, because Heartbeats don't make sense during
206 if (s
->tlsext_hb_pending
) {
207 s
->tlsext_hb_pending
= 0;
216 case SSL_ST_RENEGOTIATE
:
218 s
->state
= SSL_ST_CONNECT
;
219 s
->ctx
->stats
.sess_connect_renegotiate
++;
223 case SSL_ST_BEFORE
| SSL_ST_CONNECT
:
224 case SSL_ST_OK
| SSL_ST_CONNECT
:
228 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
230 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
231 && s
->version
!= TLS_ANY_VERSION
) {
232 SSLerr(SSL_F_SSL3_CONNECT
, ERR_R_INTERNAL_ERROR
);
233 s
->state
= SSL_ST_ERR
;
238 if (s
->version
!= TLS_ANY_VERSION
&&
239 !ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
240 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_VERSION_TOO_LOW
);
244 /* s->version=SSL3_VERSION; */
245 s
->type
= SSL_ST_CONNECT
;
247 if (s
->init_buf
== NULL
) {
248 if ((buf
= BUF_MEM_new()) == NULL
) {
250 s
->state
= SSL_ST_ERR
;
253 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
255 s
->state
= SSL_ST_ERR
;
262 if (!ssl3_setup_buffers(s
)) {
267 /* setup buffing BIO */
268 if (!ssl_init_wbio_buffer(s
, 0)) {
270 s
->state
= SSL_ST_ERR
;
274 /* don't push the buffering BIO quite yet */
276 ssl3_init_finished_mac(s
);
278 s
->state
= SSL3_ST_CW_CLNT_HELLO_A
;
279 s
->ctx
->stats
.sess_connect
++;
281 s
->s3
->flags
&= ~SSL3_FLAGS_CCS_OK
;
283 * Should have been reset by ssl3_get_finished, too.
285 s
->s3
->change_cipher_spec
= 0;
288 case SSL3_ST_CW_CLNT_HELLO_A
:
289 case SSL3_ST_CW_CLNT_HELLO_B
:
292 ret
= ssl3_client_hello(s
);
295 s
->state
= SSL3_ST_CR_SRVR_HELLO_A
;
298 /* turn on buffering for the next lot of output */
299 if (s
->bbio
!= s
->wbio
)
300 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
304 case SSL3_ST_CR_SRVR_HELLO_A
:
305 case SSL3_ST_CR_SRVR_HELLO_B
:
306 ret
= ssl3_get_server_hello(s
);
311 s
->state
= SSL3_ST_CR_FINISHED_A
;
312 #ifndef OPENSSL_NO_TLSEXT
313 if (s
->tlsext_ticket_expected
) {
314 /* receive renewed session ticket */
315 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
319 s
->state
= SSL3_ST_CR_CERT_A
;
323 case SSL3_ST_CR_CERT_A
:
324 case SSL3_ST_CR_CERT_B
:
325 #ifndef OPENSSL_NO_TLSEXT
326 /* Noop (ret = 0) for everything but EAP-FAST. */
327 ret
= ssl3_check_finished(s
);
332 s
->state
= SSL3_ST_CR_FINISHED_A
;
337 /* Check if it is anon DH/ECDH, SRP auth */
341 new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
))
342 && !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
)) {
343 ret
= ssl3_get_server_certificate(s
);
346 #ifndef OPENSSL_NO_TLSEXT
347 if (s
->tlsext_status_expected
)
348 s
->state
= SSL3_ST_CR_CERT_STATUS_A
;
350 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
353 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
359 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
364 case SSL3_ST_CR_KEY_EXCH_A
:
365 case SSL3_ST_CR_KEY_EXCH_B
:
366 ret
= ssl3_get_key_exchange(s
);
369 s
->state
= SSL3_ST_CR_CERT_REQ_A
;
373 * at this point we check that we have the required stuff from
376 if (!ssl3_check_cert_and_algorithm(s
)) {
378 s
->state
= SSL_ST_ERR
;
383 case SSL3_ST_CR_CERT_REQ_A
:
384 case SSL3_ST_CR_CERT_REQ_B
:
385 ret
= ssl3_get_certificate_request(s
);
388 s
->state
= SSL3_ST_CR_SRVR_DONE_A
;
392 case SSL3_ST_CR_SRVR_DONE_A
:
393 case SSL3_ST_CR_SRVR_DONE_B
:
394 ret
= ssl3_get_server_done(s
);
397 #ifndef OPENSSL_NO_SRP
398 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
399 if ((ret
= SRP_Calc_A_param(s
)) <= 0) {
400 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_SRP_A_CALC
);
401 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
402 s
->state
= SSL_ST_ERR
;
407 if (s
->s3
->tmp
.cert_req
)
408 s
->state
= SSL3_ST_CW_CERT_A
;
410 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
415 case SSL3_ST_CW_CERT_A
:
416 case SSL3_ST_CW_CERT_B
:
417 case SSL3_ST_CW_CERT_C
:
418 case SSL3_ST_CW_CERT_D
:
419 ret
= ssl3_send_client_certificate(s
);
422 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
426 case SSL3_ST_CW_KEY_EXCH_A
:
427 case SSL3_ST_CW_KEY_EXCH_B
:
428 ret
= ssl3_send_client_key_exchange(s
);
432 * EAY EAY EAY need to check for DH fix cert sent back
435 * For TLS, cert_req is set to 2, so a cert chain of nothing is
436 * sent, but no verify packet is sent
439 * XXX: For now, we do not support client authentication in ECDH
440 * cipher suites with ECDH (rather than ECDSA) certificates. We
441 * need to skip the certificate verify message when client's
442 * ECDH public key is sent inside the client certificate.
444 if (s
->s3
->tmp
.cert_req
== 1) {
445 s
->state
= SSL3_ST_CW_CERT_VRFY_A
;
447 s
->state
= SSL3_ST_CW_CHANGE_A
;
449 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
450 s
->state
= SSL3_ST_CW_CHANGE_A
;
456 case SSL3_ST_CW_CERT_VRFY_A
:
457 case SSL3_ST_CW_CERT_VRFY_B
:
458 ret
= ssl3_send_client_verify(s
);
461 s
->state
= SSL3_ST_CW_CHANGE_A
;
465 case SSL3_ST_CW_CHANGE_A
:
466 case SSL3_ST_CW_CHANGE_B
:
467 ret
= ssl3_send_change_cipher_spec(s
,
469 SSL3_ST_CW_CHANGE_B
);
473 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
474 s
->state
= SSL3_ST_CW_FINISHED_A
;
476 if (s
->s3
->next_proto_neg_seen
)
477 s
->state
= SSL3_ST_CW_NEXT_PROTO_A
;
479 s
->state
= SSL3_ST_CW_FINISHED_A
;
483 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
484 #ifdef OPENSSL_NO_COMP
485 s
->session
->compress_meth
= 0;
487 if (s
->s3
->tmp
.new_compression
== NULL
)
488 s
->session
->compress_meth
= 0;
490 s
->session
->compress_meth
= s
->s3
->tmp
.new_compression
->id
;
492 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
494 s
->state
= SSL_ST_ERR
;
498 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
499 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
502 s
->state
= SSL_ST_ERR
;
508 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
509 case SSL3_ST_CW_NEXT_PROTO_A
:
510 case SSL3_ST_CW_NEXT_PROTO_B
:
511 ret
= ssl3_send_next_proto(s
);
514 s
->state
= SSL3_ST_CW_FINISHED_A
;
518 case SSL3_ST_CW_FINISHED_A
:
519 case SSL3_ST_CW_FINISHED_B
:
520 ret
= ssl3_send_finished(s
,
521 SSL3_ST_CW_FINISHED_A
,
522 SSL3_ST_CW_FINISHED_B
,
524 ssl3_enc
->client_finished_label
,
526 ssl3_enc
->client_finished_label_len
);
529 s
->state
= SSL3_ST_CW_FLUSH
;
532 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
534 s
->s3
->tmp
.next_state
= SSL_ST_OK
;
535 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
536 s
->state
= SSL_ST_OK
;
537 s
->s3
->flags
|= SSL3_FLAGS_POP_BUFFER
;
538 s
->s3
->delay_buf_pop_ret
= 0;
541 #ifndef OPENSSL_NO_TLSEXT
543 * Allow NewSessionTicket if ticket expected
545 if (s
->tlsext_ticket_expected
)
546 s
->s3
->tmp
.next_state
= SSL3_ST_CR_SESSION_TICKET_A
;
550 s
->s3
->tmp
.next_state
= SSL3_ST_CR_FINISHED_A
;
555 #ifndef OPENSSL_NO_TLSEXT
556 case SSL3_ST_CR_SESSION_TICKET_A
:
557 case SSL3_ST_CR_SESSION_TICKET_B
:
558 ret
= ssl3_get_new_session_ticket(s
);
561 s
->state
= SSL3_ST_CR_FINISHED_A
;
565 case SSL3_ST_CR_CERT_STATUS_A
:
566 case SSL3_ST_CR_CERT_STATUS_B
:
567 ret
= ssl3_get_cert_status(s
);
570 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
575 case SSL3_ST_CR_FINISHED_A
:
576 case SSL3_ST_CR_FINISHED_B
:
577 if (!s
->s3
->change_cipher_spec
)
578 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
579 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
580 SSL3_ST_CR_FINISHED_B
);
585 s
->state
= SSL3_ST_CW_CHANGE_A
;
587 s
->state
= SSL_ST_OK
;
591 case SSL3_ST_CW_FLUSH
:
592 s
->rwstate
= SSL_WRITING
;
593 if (BIO_flush(s
->wbio
) <= 0) {
597 s
->rwstate
= SSL_NOTHING
;
598 s
->state
= s
->s3
->tmp
.next_state
;
602 /* clean a few things up */
603 ssl3_cleanup_key_block(s
);
604 BUF_MEM_free(s
->init_buf
);
608 * If we are not 'joining' the last two packets, remove the
611 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
612 ssl_free_wbio_buffer(s
);
613 /* else do it later in ssl3_write */
619 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
621 s
->ctx
->stats
.sess_hit
++;
625 s
->handshake_func
= ssl3_connect
;
626 s
->ctx
->stats
.sess_connect_good
++;
629 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
636 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_UNKNOWN_STATE
);
642 /* did we do anything */
643 if (!s
->s3
->tmp
.reuse_message
&& !skip
) {
645 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
649 if ((cb
!= NULL
) && (s
->state
!= state
)) {
650 new_state
= s
->state
;
652 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
653 s
->state
= new_state
;
662 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
667 * Work out what version we should be using for the initial ClientHello if
668 * the version is currently set to (D)TLS_ANY_VERSION.
669 * Returns 1 on success
672 static int ssl_set_version(SSL
*s
)
674 unsigned long mask
, options
= s
->options
;
676 if (s
->method
->version
== TLS_ANY_VERSION
) {
678 * SSL_OP_NO_X disables all protocols above X *if* there are
679 * some protocols below X enabled. This is required in order
680 * to maintain "version capability" vector contiguous. So
681 * that if application wants to disable TLS1.0 in favour of
682 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
683 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
685 mask
= SSL_OP_NO_TLSv1_1
| SSL_OP_NO_TLSv1
686 #if !defined(OPENSSL_NO_SSL3)
690 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
691 if (options
& SSL_OP_NO_TLSv1_2
) {
692 if ((options
& mask
) != mask
) {
693 s
->version
= TLS1_1_VERSION
;
695 SSLerr(SSL_F_SSL_SET_VERSION
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
699 s
->version
= TLS1_2_VERSION
;
702 if ((options
& mask
) == mask
) {
703 SSLerr(SSL_F_SSL_SET_VERSION
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
706 s
->version
= TLS1_1_VERSION
;
709 mask
&= ~SSL_OP_NO_TLSv1_1
;
710 if ((options
& SSL_OP_NO_TLSv1_1
) && (options
& mask
) != mask
)
711 s
->version
= TLS1_VERSION
;
712 mask
&= ~SSL_OP_NO_TLSv1
;
713 #if !defined(OPENSSL_NO_SSL3)
714 if ((options
& SSL_OP_NO_TLSv1
) && (options
& mask
) != mask
)
715 s
->version
= SSL3_VERSION
;
718 if (s
->version
!= TLS1_2_VERSION
&& tls1_suiteb(s
)) {
719 SSLerr(SSL_F_SSL_SET_VERSION
,
720 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE
);
724 if (s
->version
== SSL3_VERSION
&& FIPS_mode()) {
725 SSLerr(SSL_F_SSL_SET_VERSION
, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE
);
729 } else if (s
->method
->version
== DTLS_ANY_VERSION
) {
730 /* Determine which DTLS version to use */
731 /* If DTLS 1.2 disabled correct the version number */
732 if (options
& SSL_OP_NO_DTLSv1_2
) {
733 if (tls1_suiteb(s
)) {
734 SSLerr(SSL_F_SSL_SET_VERSION
,
735 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
739 * Disabling all versions is silly: return an error.
741 if (options
& SSL_OP_NO_DTLSv1
) {
742 SSLerr(SSL_F_SSL_SET_VERSION
, SSL_R_WRONG_SSL_VERSION
);
746 * Update method so we don't use any DTLS 1.2 features.
748 s
->method
= DTLSv1_client_method();
749 s
->version
= DTLS1_VERSION
;
752 * We only support one version: update method
754 if (options
& SSL_OP_NO_DTLSv1
)
755 s
->method
= DTLSv1_2_client_method();
756 s
->version
= DTLS1_2_VERSION
;
760 s
->client_version
= s
->version
;
765 int ssl3_client_hello(SSL
*s
)
768 unsigned char *p
, *d
;
772 #ifndef OPENSSL_NO_COMP
777 buf
= (unsigned char *)s
->init_buf
->data
;
778 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
) {
779 SSL_SESSION
*sess
= s
->session
;
781 /* Work out what SSL/TLS/DTLS version to use */
782 if (ssl_set_version(s
) == 0)
785 if ((sess
== NULL
) || (sess
->ssl_version
!= s
->version
) ||
786 #ifdef OPENSSL_NO_TLSEXT
787 !sess
->session_id_length
||
790 * In the case of EAP-FAST, we can have a pre-shared
791 * "ticket" without a session ID.
793 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
795 (sess
->not_resumable
)) {
796 if (!ssl_get_new_session(s
, 0))
799 /* else use the pre-loaded session */
801 p
= s
->s3
->client_random
;
804 * for DTLS if client_random is initialized, reuse it, we are
805 * required to use same upon reply to HelloVerify
807 if (SSL_IS_DTLS(s
)) {
810 for (idx
= 0; idx
< sizeof(s
->s3
->client_random
); idx
++) {
819 if (i
&& ssl_fill_hello_random(s
, 0, p
,
820 sizeof(s
->s3
->client_random
)) <= 0)
823 /* Do the message type and length last */
824 d
= p
= ssl_handshake_start(s
);
827 * version indicates the negotiated version: for example from
828 * an SSLv2/v3 compatible client hello). The client_version
829 * field is the maximum version we permit and it is also
830 * used in RSA encrypted premaster secrets. Some servers can
831 * choke if we initially report a higher version then
832 * renegotiate to a lower one in the premaster secret. This
833 * didn't happen with TLS 1.0 as most servers supported it
834 * but it can with TLS 1.1 or later if the server only supports
837 * Possible scenario with previous logic:
838 * 1. Client hello indicates TLS 1.2
839 * 2. Server hello says TLS 1.0
840 * 3. RSA encrypted premaster secret uses 1.2.
841 * 4. Handhaked proceeds using TLS 1.0.
842 * 5. Server sends hello request to renegotiate.
843 * 6. Client hello indicates TLS v1.0 as we now
844 * know that is maximum server supports.
845 * 7. Server chokes on RSA encrypted premaster secret
846 * containing version 1.0.
848 * For interoperability it should be OK to always use the
849 * maximum version we support in client hello and then rely
850 * on the checking of version to ensure the servers isn't
851 * being inconsistent: for example initially negotiating with
852 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
853 * client_version in client hello and not resetting it to
854 * the negotiated version.
856 *(p
++) = s
->client_version
>> 8;
857 *(p
++) = s
->client_version
& 0xff;
860 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
861 p
+= SSL3_RANDOM_SIZE
;
867 i
= s
->session
->session_id_length
;
870 if (i
> (int)sizeof(s
->session
->session_id
)) {
871 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
874 memcpy(p
, s
->session
->session_id
, i
);
878 /* cookie stuff for DTLS */
879 if (SSL_IS_DTLS(s
)) {
880 if (s
->d1
->cookie_len
> sizeof(s
->d1
->cookie
)) {
881 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
884 *(p
++) = s
->d1
->cookie_len
;
885 memcpy(p
, s
->d1
->cookie
, s
->d1
->cookie_len
);
886 p
+= s
->d1
->cookie_len
;
889 /* Ciphers supported */
890 i
= ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &(p
[2]), 0);
892 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_NO_CIPHERS_AVAILABLE
);
895 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
897 * Some servers hang if client hello > 256 bytes as hack workaround
898 * chop number of supported ciphers to keep it well below this if we
901 if (TLS1_get_version(s
) >= TLS1_2_VERSION
902 && i
> OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
)
903 i
= OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
& ~1;
909 #ifdef OPENSSL_NO_COMP
913 if (!ssl_allow_compression(s
) || !s
->ctx
->comp_methods
)
916 j
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
918 for (i
= 0; i
< j
; i
++) {
919 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, i
);
923 *(p
++) = 0; /* Add the NULL method */
925 #ifndef OPENSSL_NO_TLSEXT
927 if (ssl_prepare_clienthello_tlsext(s
) <= 0) {
928 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
932 ssl_add_clienthello_tlsext(s
, p
, buf
+ SSL3_RT_MAX_PLAIN_LENGTH
,
934 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
935 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
941 if (!ssl_set_handshake_header(s
, SSL3_MT_CLIENT_HELLO
, l
)) {
942 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
943 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
946 s
->state
= SSL3_ST_CW_CLNT_HELLO_B
;
949 /* SSL3_ST_CW_CLNT_HELLO_B */
950 return ssl_do_write(s
);
952 s
->state
= SSL_ST_ERR
;
956 int ssl3_get_server_hello(SSL
*s
)
958 STACK_OF(SSL_CIPHER
) *sk
;
961 unsigned char *p
, *d
;
962 int i
, al
= SSL_AD_INTERNAL_ERROR
, ok
;
965 #ifndef OPENSSL_NO_COMP
969 * Hello verify request and/or server hello version may not match so set
970 * first packet if we're negotiating version.
975 n
= s
->method
->ssl_get_message(s
,
976 SSL3_ST_CR_SRVR_HELLO_A
,
977 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, &ok
);
982 if (SSL_IS_DTLS(s
)) {
984 if (s
->s3
->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
985 if (s
->d1
->send_cookie
== 0) {
986 s
->s3
->tmp
.reuse_message
= 1;
988 } else { /* already sent a cookie */
990 al
= SSL_AD_UNEXPECTED_MESSAGE
;
991 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
997 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
998 al
= SSL_AD_UNEXPECTED_MESSAGE
;
999 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
1003 d
= p
= (unsigned char *)s
->init_msg
;
1005 if (s
->method
->version
== TLS_ANY_VERSION
) {
1006 int sversion
= (p
[0] << 8) | p
[1];
1008 #if TLS_MAX_VERSION != TLS1_2_VERSION
1009 #error Code needs updating for new TLS version
1011 #ifndef OPENSSL_NO_SSL3
1012 if ((sversion
== SSL3_VERSION
) && !(s
->options
& SSL_OP_NO_SSLv3
)) {
1014 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1015 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE
);
1016 al
= SSL_AD_PROTOCOL_VERSION
;
1019 s
->method
= SSLv3_client_method();
1022 if ((sversion
== TLS1_VERSION
) && !(s
->options
& SSL_OP_NO_TLSv1
)) {
1023 s
->method
= TLSv1_client_method();
1024 } else if ((sversion
== TLS1_1_VERSION
) &&
1025 !(s
->options
& SSL_OP_NO_TLSv1_1
)) {
1026 s
->method
= TLSv1_1_client_method();
1027 } else if ((sversion
== TLS1_2_VERSION
) &&
1028 !(s
->options
& SSL_OP_NO_TLSv1_2
)) {
1029 s
->method
= TLSv1_2_client_method();
1031 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_UNSUPPORTED_PROTOCOL
);
1032 al
= SSL_AD_PROTOCOL_VERSION
;
1035 s
->session
->ssl_version
= s
->version
= s
->method
->version
;
1037 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
1038 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_VERSION_TOO_LOW
);
1039 al
= SSL_AD_PROTOCOL_VERSION
;
1042 } else if (s
->method
->version
== DTLS_ANY_VERSION
) {
1043 /* Work out correct protocol version to use */
1044 int hversion
= (p
[0] << 8) | p
[1];
1045 int options
= s
->options
;
1046 if (hversion
== DTLS1_2_VERSION
&& !(options
& SSL_OP_NO_DTLSv1_2
))
1047 s
->method
= DTLSv1_2_client_method();
1048 else if (tls1_suiteb(s
)) {
1049 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1050 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
1051 s
->version
= hversion
;
1052 al
= SSL_AD_PROTOCOL_VERSION
;
1054 } else if (hversion
== DTLS1_VERSION
&& !(options
& SSL_OP_NO_DTLSv1
))
1055 s
->method
= DTLSv1_client_method();
1057 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
1058 s
->version
= hversion
;
1059 al
= SSL_AD_PROTOCOL_VERSION
;
1062 s
->version
= s
->method
->version
;
1063 } else if ((p
[0] != (s
->version
>> 8)) || (p
[1] != (s
->version
& 0xff))) {
1064 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
1065 s
->version
= (s
->version
& 0xff00) | p
[1];
1066 al
= SSL_AD_PROTOCOL_VERSION
;
1071 /* load the server hello data */
1072 /* load the server random */
1073 memcpy(s
->s3
->server_random
, p
, SSL3_RANDOM_SIZE
);
1074 p
+= SSL3_RANDOM_SIZE
;
1078 /* get the session-id */
1081 if ((j
> sizeof s
->session
->session_id
) || (j
> SSL3_SESSION_ID_SIZE
)) {
1082 al
= SSL_AD_ILLEGAL_PARAMETER
;
1083 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
1086 #ifndef OPENSSL_NO_TLSEXT
1088 * Check if we can resume the session based on external pre-shared secret.
1089 * EAP-FAST (RFC 4851) supports two types of session resumption.
1090 * Resumption based on server-side state works with session IDs.
1091 * Resumption based on pre-shared Protected Access Credentials (PACs)
1092 * works by overriding the SessionTicket extension at the application
1093 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1094 * servers would honour the session ID.) Therefore, the session ID alone
1095 * is not a reliable indicator of session resumption, so we first check if
1096 * we can resume, and later peek at the next handshake message to see if the
1097 * server wants to resume.
1099 if (s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
&&
1100 s
->session
->tlsext_tick
) {
1101 SSL_CIPHER
*pref_cipher
= NULL
;
1102 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
1103 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
1104 &s
->session
->master_key_length
,
1106 s
->tls_session_secret_cb_arg
)) {
1107 s
->session
->cipher
= pref_cipher
?
1108 pref_cipher
: ssl_get_cipher_by_char(s
, p
+ j
);
1110 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1111 al
= SSL_AD_INTERNAL_ERROR
;
1115 #endif /* OPENSSL_NO_TLSEXT */
1117 if (j
!= 0 && j
== s
->session
->session_id_length
1118 && memcmp(p
, s
->session
->session_id
, j
) == 0) {
1119 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
1120 || memcmp(s
->session
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
)) {
1121 /* actually a client application bug */
1122 al
= SSL_AD_ILLEGAL_PARAMETER
;
1123 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1124 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
1130 * If we were trying for session-id reuse but the server
1131 * didn't echo the ID, make a new SSL_SESSION.
1132 * In the case of EAP-FAST and PAC, we do not send a session ID,
1133 * so the PAC-based session secret is always preserved. It'll be
1134 * overwritten if the server refuses resumption.
1136 if (s
->session
->session_id_length
> 0) {
1137 if (!ssl_get_new_session(s
, 0)) {
1141 s
->session
->session_id_length
= j
;
1142 memcpy(s
->session
->session_id
, p
, j
); /* j could be 0 */
1145 c
= ssl_get_cipher_by_char(s
, p
);
1147 /* unknown cipher */
1148 al
= SSL_AD_ILLEGAL_PARAMETER
;
1149 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
1152 /* Set version disabled mask now we know version */
1153 if (!SSL_USE_TLS1_2_CIPHERS(s
))
1154 ct
->mask_ssl
= SSL_TLSV1_2
;
1158 * If it is a disabled cipher we didn't send it in client hello, so
1161 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_CHECK
)) {
1162 al
= SSL_AD_ILLEGAL_PARAMETER
;
1163 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
1166 p
+= ssl_put_cipher_by_char(s
, NULL
, NULL
);
1168 sk
= ssl_get_ciphers_by_id(s
);
1169 i
= sk_SSL_CIPHER_find(sk
, c
);
1171 /* we did not say we would use this cipher */
1172 al
= SSL_AD_ILLEGAL_PARAMETER
;
1173 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
1178 * Depending on the session caching (internal/external), the cipher
1179 * and/or cipher_id values may not be set. Make sure that cipher_id is
1180 * set and use it for comparison.
1182 if (s
->session
->cipher
)
1183 s
->session
->cipher_id
= s
->session
->cipher
->id
;
1184 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
1185 al
= SSL_AD_ILLEGAL_PARAMETER
;
1186 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1187 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
1190 s
->s3
->tmp
.new_cipher
= c
;
1192 * Don't digest cached records if no sigalgs: we may need them for client
1195 if (!SSL_USE_SIGALGS(s
) && !ssl3_digest_cached_records(s
))
1197 /* lets get the compression algorithm */
1199 #ifdef OPENSSL_NO_COMP
1201 al
= SSL_AD_ILLEGAL_PARAMETER
;
1202 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1203 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1207 * If compression is disabled we'd better not try to resume a session
1208 * using compression.
1210 if (s
->session
->compress_meth
!= 0) {
1211 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1216 if (s
->hit
&& j
!= s
->session
->compress_meth
) {
1217 al
= SSL_AD_ILLEGAL_PARAMETER
;
1218 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1219 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED
);
1224 else if (!ssl_allow_compression(s
)) {
1225 al
= SSL_AD_ILLEGAL_PARAMETER
;
1226 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_COMPRESSION_DISABLED
);
1229 comp
= ssl3_comp_find(s
->ctx
->comp_methods
, j
);
1231 if ((j
!= 0) && (comp
== NULL
)) {
1232 al
= SSL_AD_ILLEGAL_PARAMETER
;
1233 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1234 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1237 s
->s3
->tmp
.new_compression
= comp
;
1241 #ifndef OPENSSL_NO_TLSEXT
1242 /* TLS extensions */
1243 if (!ssl_parse_serverhello_tlsext(s
, &p
, d
, n
)) {
1244 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_PARSE_TLSEXT
);
1250 /* wrong packet length */
1251 al
= SSL_AD_DECODE_ERROR
;
1252 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_PACKET_LENGTH
);
1258 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1260 s
->state
= SSL_ST_ERR
;
1264 int ssl3_get_server_certificate(SSL
*s
)
1266 int al
, i
, ok
, ret
= -1, exp_idx
;
1267 unsigned long n
, nc
, llen
, l
;
1269 const unsigned char *q
, *p
;
1271 STACK_OF(X509
) *sk
= NULL
;
1273 EVP_PKEY
*pkey
= NULL
;
1275 n
= s
->method
->ssl_get_message(s
,
1278 -1, s
->max_cert_list
, &ok
);
1283 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
1284 s
->s3
->tmp
.reuse_message
= 1;
1288 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
1289 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1290 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_BAD_MESSAGE_TYPE
);
1293 p
= d
= (unsigned char *)s
->init_msg
;
1295 if ((sk
= sk_X509_new_null()) == NULL
) {
1296 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1301 if (llen
+ 3 != n
) {
1302 al
= SSL_AD_DECODE_ERROR
;
1303 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
1306 for (nc
= 0; nc
< llen
;) {
1308 if ((l
+ nc
+ 3) > llen
) {
1309 al
= SSL_AD_DECODE_ERROR
;
1310 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1311 SSL_R_CERT_LENGTH_MISMATCH
);
1316 x
= d2i_X509(NULL
, &q
, l
);
1318 al
= SSL_AD_BAD_CERTIFICATE
;
1319 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_ASN1_LIB
);
1323 al
= SSL_AD_DECODE_ERROR
;
1324 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1325 SSL_R_CERT_LENGTH_MISMATCH
);
1328 if (!sk_X509_push(sk
, x
)) {
1329 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1337 i
= ssl_verify_cert_chain(s
, sk
);
1338 if (s
->verify_mode
!= SSL_VERIFY_NONE
&& i
<= 0) {
1339 al
= ssl_verify_alarm_type(s
->verify_result
);
1340 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1341 SSL_R_CERTIFICATE_VERIFY_FAILED
);
1344 ERR_clear_error(); /* but we keep s->verify_result */
1346 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, i
);
1347 al
= SSL_AD_HANDSHAKE_FAILURE
;
1351 sc
= ssl_sess_cert_new();
1355 ssl_sess_cert_free(s
->session
->sess_cert
);
1356 s
->session
->sess_cert
= sc
;
1358 sc
->cert_chain
= sk
;
1360 * Inconsistency alert: cert_chain does include the peer's certificate,
1361 * which we don't include in s3_srvr.c
1363 x
= sk_X509_value(sk
, 0);
1366 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1369 pkey
= X509_get_pubkey(x
);
1371 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1374 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1375 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1379 i
= ssl_cert_type(x
, pkey
);
1383 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1384 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1388 exp_idx
= ssl_cipher_get_cert_index(s
->s3
->tmp
.new_cipher
);
1389 if (exp_idx
>= 0 && i
!= exp_idx
) {
1391 al
= SSL_AD_ILLEGAL_PARAMETER
;
1392 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1393 SSL_R_WRONG_CERTIFICATE_TYPE
);
1396 sc
->peer_cert_type
= i
;
1397 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1399 * Why would the following ever happen? We just created sc a couple
1402 X509_free(sc
->peer_pkeys
[i
].x509
);
1403 sc
->peer_pkeys
[i
].x509
= x
;
1404 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1406 X509_free(s
->session
->peer
);
1407 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1408 s
->session
->peer
= x
;
1409 s
->session
->verify_result
= s
->verify_result
;
1416 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1418 s
->state
= SSL_ST_ERR
;
1420 EVP_PKEY_free(pkey
);
1422 sk_X509_pop_free(sk
, X509_free
);
1426 int ssl3_get_key_exchange(SSL
*s
)
1428 #ifndef OPENSSL_NO_RSA
1429 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
* 2];
1432 unsigned char *param
, *p
;
1434 long i
, param_len
, n
, alg_k
, alg_a
;
1435 EVP_PKEY
*pkey
= NULL
;
1436 const EVP_MD
*md
= NULL
;
1437 #ifndef OPENSSL_NO_RSA
1440 #ifndef OPENSSL_NO_DH
1443 #ifndef OPENSSL_NO_EC
1444 EC_KEY
*ecdh
= NULL
;
1445 BN_CTX
*bn_ctx
= NULL
;
1446 EC_POINT
*srvr_ecpoint
= NULL
;
1448 int encoded_pt_len
= 0;
1451 EVP_MD_CTX_init(&md_ctx
);
1454 * use same message size as in ssl3_get_certificate_request() as
1455 * ServerKeyExchange message may be skipped
1457 n
= s
->method
->ssl_get_message(s
,
1458 SSL3_ST_CR_KEY_EXCH_A
,
1459 SSL3_ST_CR_KEY_EXCH_B
,
1460 -1, s
->max_cert_list
, &ok
);
1464 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1466 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1468 * Can't skip server key exchange if this is an ephemeral
1471 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)) {
1472 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1473 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1476 #ifndef OPENSSL_NO_PSK
1478 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1479 * identity hint is sent. Set session->sess_cert anyway to avoid
1482 if (alg_k
& SSL_kPSK
) {
1483 s
->session
->sess_cert
= ssl_sess_cert_new();
1484 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1485 s
->ctx
->psk_identity_hint
= NULL
;
1488 s
->s3
->tmp
.reuse_message
= 1;
1492 param
= p
= (unsigned char *)s
->init_msg
;
1493 if (s
->session
->sess_cert
!= NULL
) {
1494 #ifndef OPENSSL_NO_RSA
1495 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
1496 s
->session
->sess_cert
->peer_rsa_tmp
= NULL
;
1498 #ifndef OPENSSL_NO_DH
1499 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
1500 s
->session
->sess_cert
->peer_dh_tmp
= NULL
;
1502 #ifndef OPENSSL_NO_EC
1503 EC_KEY_free(s
->session
->sess_cert
->peer_ecdh_tmp
);
1504 s
->session
->sess_cert
->peer_ecdh_tmp
= NULL
;
1507 s
->session
->sess_cert
= ssl_sess_cert_new();
1510 /* Total length of the parameters including the length prefix */
1513 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1515 al
= SSL_AD_DECODE_ERROR
;
1517 #ifndef OPENSSL_NO_PSK
1518 if (alg_k
& SSL_kPSK
) {
1519 char tmp_id_hint
[PSK_MAX_IDENTITY_LEN
+ 1];
1522 if (param_len
> n
) {
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1529 * Store PSK identity hint for later use, hint is used in
1530 * ssl3_send_client_key_exchange. Assume that the maximum length of
1531 * a PSK identity hint can be as long as the maximum length of a PSK
1534 if (i
> PSK_MAX_IDENTITY_LEN
) {
1535 al
= SSL_AD_HANDSHAKE_FAILURE
;
1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_DATA_LENGTH_TOO_LONG
);
1539 if (i
> n
- param_len
) {
1540 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1541 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH
);
1547 * If received PSK identity hint contains NULL characters, the hint
1548 * is truncated from the first NULL. p may not be ending with NULL,
1549 * so create a NULL-terminated string.
1551 memcpy(tmp_id_hint
, p
, i
);
1552 memset(tmp_id_hint
+ i
, 0, PSK_MAX_IDENTITY_LEN
+ 1 - i
);
1553 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1554 s
->ctx
->psk_identity_hint
= BUF_strdup(tmp_id_hint
);
1555 if (s
->ctx
->psk_identity_hint
== NULL
) {
1556 al
= SSL_AD_HANDSHAKE_FAILURE
;
1557 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1564 #endif /* !OPENSSL_NO_PSK */
1565 #ifndef OPENSSL_NO_SRP
1566 if (alg_k
& SSL_kSRP
) {
1568 if (param_len
> n
) {
1569 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1574 if (i
> n
- param_len
) {
1575 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_N_LENGTH
);
1580 if ((s
->srp_ctx
.N
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1581 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1586 if (2 > n
- param_len
) {
1587 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1594 if (i
> n
- param_len
) {
1595 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_G_LENGTH
);
1600 if ((s
->srp_ctx
.g
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1601 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1606 if (1 > n
- param_len
) {
1607 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1612 i
= (unsigned int)(p
[0]);
1615 if (i
> n
- param_len
) {
1616 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_S_LENGTH
);
1621 if ((s
->srp_ctx
.s
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1622 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1627 if (2 > n
- param_len
) {
1628 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1635 if (i
> n
- param_len
) {
1636 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_B_LENGTH
);
1641 if ((s
->srp_ctx
.B
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1642 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1648 if (!srp_verify_server_param(s
, &al
)) {
1649 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_PARAMETERS
);
1653 /* We must check if there is a certificate */
1654 # ifndef OPENSSL_NO_RSA
1655 if (alg_a
& SSL_aRSA
)
1657 X509_get_pubkey(s
->session
->
1658 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1662 # ifndef OPENSSL_NO_DSA
1663 else if (alg_a
& SSL_aDSS
)
1665 X509_get_pubkey(s
->session
->
1666 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1670 #endif /* !OPENSSL_NO_SRP */
1671 #ifndef OPENSSL_NO_RSA
1672 if (alg_k
& SSL_kRSA
) {
1673 /* Temporary RSA keys only allowed in export ciphersuites */
1674 if (!SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)) {
1675 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1679 if ((rsa
= RSA_new()) == NULL
) {
1680 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1685 if (param_len
> n
) {
1686 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1691 if (i
> n
- param_len
) {
1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_MODULUS_LENGTH
);
1697 if ((rsa
->n
= BN_bin2bn(p
, i
, rsa
->n
)) == NULL
) {
1698 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1703 if (2 > n
- param_len
) {
1704 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1711 if (i
> n
- param_len
) {
1712 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_E_LENGTH
);
1717 if ((rsa
->e
= BN_bin2bn(p
, i
, rsa
->e
)) == NULL
) {
1718 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1724 /* this should be because we are using an export cipher */
1725 if (alg_a
& SSL_aRSA
)
1727 X509_get_pubkey(s
->session
->
1728 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1730 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1733 s
->session
->sess_cert
->peer_rsa_tmp
= rsa
;
1736 #else /* OPENSSL_NO_RSA */
1739 #ifndef OPENSSL_NO_DH
1740 else if (alg_k
& SSL_kDHE
) {
1741 if ((dh
= DH_new()) == NULL
) {
1742 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1747 if (param_len
> n
) {
1748 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1753 if (i
> n
- param_len
) {
1754 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_P_LENGTH
);
1759 if ((dh
->p
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1765 if (2 > n
- param_len
) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1773 if (i
> n
- param_len
) {
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_G_LENGTH
);
1779 if ((dh
->g
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1780 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1785 if (2 > n
- param_len
) {
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1793 if (i
> n
- param_len
) {
1794 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1799 if ((dh
->pub_key
= BN_bin2bn(p
, i
, NULL
)) == NULL
) {
1800 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1806 if (!ssl_security(s
, SSL_SECOP_TMP_DH
, DH_security_bits(dh
), 0, dh
)) {
1807 al
= SSL_AD_HANDSHAKE_FAILURE
;
1808 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_DH_KEY_TOO_SMALL
);
1811 # ifndef OPENSSL_NO_RSA
1812 if (alg_a
& SSL_aRSA
)
1814 X509_get_pubkey(s
->session
->
1815 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1819 # ifndef OPENSSL_NO_DSA
1820 else if (alg_a
& SSL_aDSS
)
1822 X509_get_pubkey(s
->session
->
1823 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1826 /* else anonymous DH, so no certificate or pkey. */
1828 s
->session
->sess_cert
->peer_dh_tmp
= dh
;
1831 #endif /* !OPENSSL_NO_DH */
1833 #ifndef OPENSSL_NO_EC
1834 else if (alg_k
& SSL_kECDHE
) {
1836 const EC_GROUP
*group
;
1838 if ((ecdh
= EC_KEY_new()) == NULL
) {
1839 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1844 * Extract elliptic curve parameters and the server's ephemeral ECDH
1845 * public key. Keep accumulating lengths of various components in
1846 * param_len and make sure it never exceeds n.
1850 * XXX: For now we only support named (not generic) curves and the
1851 * ECParameters in this case is just three bytes. We also need one
1852 * byte for the length of the encoded point
1855 if (param_len
> n
) {
1856 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1860 * Check curve is one of our preferences, if not server has sent an
1861 * invalid curve. ECParameters is 3 bytes.
1863 if (!tls1_check_curve(s
, p
, 3)) {
1864 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_CURVE
);
1868 if ((curve_nid
= tls1_ec_curve_id2nid(*(p
+ 2))) == 0) {
1869 al
= SSL_AD_INTERNAL_ERROR
;
1870 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1871 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1875 ngroup
= EC_GROUP_new_by_curve_name(curve_nid
);
1876 if (ngroup
== NULL
) {
1877 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1880 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1881 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1884 EC_GROUP_free(ngroup
);
1886 group
= EC_KEY_get0_group(ecdh
);
1888 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1889 (EC_GROUP_get_degree(group
) > 163)) {
1890 al
= SSL_AD_EXPORT_RESTRICTION
;
1891 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1892 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1898 /* Next, get the encoded ECPoint */
1899 if (((srvr_ecpoint
= EC_POINT_new(group
)) == NULL
) ||
1900 ((bn_ctx
= BN_CTX_new()) == NULL
)) {
1901 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1905 encoded_pt_len
= *p
; /* length of encoded point */
1908 if ((encoded_pt_len
> n
- param_len
) ||
1909 (EC_POINT_oct2point(group
, srvr_ecpoint
,
1910 p
, encoded_pt_len
, bn_ctx
) == 0)) {
1911 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_ECPOINT
);
1914 param_len
+= encoded_pt_len
;
1917 p
+= encoded_pt_len
;
1920 * The ECC/TLS specification does not mention the use of DSA to sign
1921 * ECParameters in the server key exchange message. We do support RSA
1925 # ifndef OPENSSL_NO_RSA
1926 else if (alg_a
& SSL_aRSA
)
1928 X509_get_pubkey(s
->session
->
1929 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1931 # ifndef OPENSSL_NO_EC
1932 else if (alg_a
& SSL_aECDSA
)
1934 X509_get_pubkey(s
->session
->
1935 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1937 /* else anonymous ECDH, so no certificate or pkey. */
1938 EC_KEY_set_public_key(ecdh
, srvr_ecpoint
);
1939 s
->session
->sess_cert
->peer_ecdh_tmp
= ecdh
;
1941 BN_CTX_free(bn_ctx
);
1943 EC_POINT_free(srvr_ecpoint
);
1944 srvr_ecpoint
= NULL
;
1946 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1947 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1950 #endif /* !OPENSSL_NO_EC */
1952 /* p points to the next byte, there are 'n' bytes left */
1954 /* if it was signed, check the signature */
1956 if (SSL_USE_SIGALGS(s
)) {
1959 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1962 rv
= tls12_check_peer_sigalg(&md
, s
, p
, pkey
);
1969 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
1977 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1982 j
= EVP_PKEY_size(pkey
);
1985 * Check signature length. If n is 0 then signature is empty
1987 if ((i
!= n
) || (n
> j
) || (n
<= 0)) {
1988 /* wrong packet length */
1989 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1992 #ifndef OPENSSL_NO_RSA
1993 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1999 for (num
= 2; num
> 0; num
--) {
2000 EVP_MD_CTX_set_flags(&md_ctx
, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
2001 EVP_DigestInit_ex(&md_ctx
, (num
== 2)
2002 ? s
->ctx
->md5
: s
->ctx
->sha1
, NULL
);
2003 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
2005 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
2007 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
2008 EVP_DigestFinal_ex(&md_ctx
, q
, &size
);
2012 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
, pkey
->pkey
.rsa
);
2014 al
= SSL_AD_DECRYPT_ERROR
;
2015 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_DECRYPT
);
2020 al
= SSL_AD_DECRYPT_ERROR
;
2021 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
2027 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
2028 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
2030 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
2032 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
2033 if (EVP_VerifyFinal(&md_ctx
, p
, (int)n
, pkey
) <= 0) {
2035 al
= SSL_AD_DECRYPT_ERROR
;
2036 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
2041 /* aNULL, aSRP or kPSK do not need public keys */
2042 if (!(alg_a
& (SSL_aNULL
| SSL_aSRP
)) && !(alg_k
& SSL_kPSK
)) {
2043 /* Might be wrong key type, check it */
2044 if (ssl3_check_cert_and_algorithm(s
))
2045 /* Otherwise this shouldn't happen */
2046 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2049 /* still data left over */
2051 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
2055 EVP_PKEY_free(pkey
);
2056 EVP_MD_CTX_cleanup(&md_ctx
);
2059 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2061 EVP_PKEY_free(pkey
);
2062 #ifndef OPENSSL_NO_RSA
2065 #ifndef OPENSSL_NO_DH
2068 #ifndef OPENSSL_NO_EC
2069 BN_CTX_free(bn_ctx
);
2070 EC_POINT_free(srvr_ecpoint
);
2073 EVP_MD_CTX_cleanup(&md_ctx
);
2074 s
->state
= SSL_ST_ERR
;
2078 int ssl3_get_certificate_request(SSL
*s
)
2081 unsigned long n
, nc
, l
;
2082 unsigned int llen
, ctype_num
, i
;
2083 X509_NAME
*xn
= NULL
;
2084 const unsigned char *p
, *q
;
2086 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
2088 n
= s
->method
->ssl_get_message(s
,
2089 SSL3_ST_CR_CERT_REQ_A
,
2090 SSL3_ST_CR_CERT_REQ_B
,
2091 -1, s
->max_cert_list
, &ok
);
2096 s
->s3
->tmp
.cert_req
= 0;
2098 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
2099 s
->s3
->tmp
.reuse_message
= 1;
2101 * If we get here we don't need any cached handshake records as we
2102 * wont be doing client auth.
2104 if (s
->s3
->handshake_buffer
) {
2105 if (!ssl3_digest_cached_records(s
))
2111 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
2112 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2113 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_WRONG_MESSAGE_TYPE
);
2117 /* TLS does not like anon-DH with client cert */
2118 if (s
->version
> SSL3_VERSION
) {
2119 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
2120 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2121 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2122 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
2127 p
= d
= (unsigned char *)s
->init_msg
;
2129 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
2130 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2134 /* get the certificate types */
2136 OPENSSL_free(s
->cert
->ctypes
);
2137 s
->cert
->ctypes
= NULL
;
2138 if (ctype_num
> SSL3_CT_NUMBER
) {
2139 /* If we exceed static buffer copy all to cert structure */
2140 s
->cert
->ctypes
= OPENSSL_malloc(ctype_num
);
2141 if (s
->cert
->ctypes
== NULL
) {
2142 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2145 memcpy(s
->cert
->ctypes
, p
, ctype_num
);
2146 s
->cert
->ctype_num
= (size_t)ctype_num
;
2147 ctype_num
= SSL3_CT_NUMBER
;
2149 for (i
= 0; i
< ctype_num
; i
++)
2150 s
->s3
->tmp
.ctype
[i
] = p
[i
];
2152 if (SSL_USE_SIGALGS(s
)) {
2155 * Check we have enough room for signature algorithms and following
2158 if ((unsigned long)(p
- d
+ llen
+ 2) > n
) {
2159 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2160 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2161 SSL_R_DATA_LENGTH_TOO_LONG
);
2164 /* Clear certificate digests and validity flags */
2165 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
2166 s
->cert
->pkeys
[i
].digest
= NULL
;
2167 s
->cert
->pkeys
[i
].valid_flags
= 0;
2169 if ((llen
& 1) || !tls1_save_sigalgs(s
, p
, llen
)) {
2170 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2171 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2172 SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
2175 if (!tls1_process_sigalgs(s
)) {
2176 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2177 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2183 /* get the CA RDNs */
2186 if ((unsigned long)(p
- d
+ llen
) != n
) {
2187 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2188 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_LENGTH_MISMATCH
);
2192 for (nc
= 0; nc
< llen
;) {
2194 if ((l
+ nc
+ 2) > llen
) {
2195 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2196 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_CA_DN_TOO_LONG
);
2202 if ((xn
= d2i_X509_NAME(NULL
, &q
, l
)) == NULL
) {
2203 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2204 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_ASN1_LIB
);
2209 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2210 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2211 SSL_R_CA_DN_LENGTH_MISMATCH
);
2214 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
2215 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2223 /* we should setup a certificate to return.... */
2224 s
->s3
->tmp
.cert_req
= 1;
2225 s
->s3
->tmp
.ctype_num
= ctype_num
;
2226 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
2227 s
->s3
->tmp
.ca_names
= ca_sk
;
2233 s
->state
= SSL_ST_ERR
;
2235 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
2239 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
2241 return (X509_NAME_cmp(*a
, *b
));
2244 #ifndef OPENSSL_NO_TLSEXT
2245 int ssl3_get_new_session_ticket(SSL
*s
)
2247 int ok
, al
, ret
= 0, ticklen
;
2249 const unsigned char *p
;
2252 n
= s
->method
->ssl_get_message(s
,
2253 SSL3_ST_CR_SESSION_TICKET_A
,
2254 SSL3_ST_CR_SESSION_TICKET_B
,
2255 SSL3_MT_NEWSESSION_TICKET
, 16384, &ok
);
2261 /* need at least ticket_lifetime_hint + ticket length */
2262 al
= SSL_AD_DECODE_ERROR
;
2263 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
2267 p
= d
= (unsigned char *)s
->init_msg
;
2268 n2l(p
, s
->session
->tlsext_tick_lifetime_hint
);
2270 /* ticket_lifetime_hint + ticket_length + ticket */
2271 if (ticklen
+ 6 != n
) {
2272 al
= SSL_AD_DECODE_ERROR
;
2273 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
2276 OPENSSL_free(s
->session
->tlsext_tick
);
2277 s
->session
->tlsext_ticklen
= 0;
2278 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
2279 if (!s
->session
->tlsext_tick
) {
2280 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
2283 memcpy(s
->session
->tlsext_tick
, p
, ticklen
);
2284 s
->session
->tlsext_ticklen
= ticklen
;
2286 * There are two ways to detect a resumed ticket session. One is to set
2287 * an appropriate session ID and then the server must return a match in
2288 * ServerHello. This allows the normal client session ID matching to work
2289 * and we know much earlier that the ticket has been accepted. The
2290 * other way is to set zero length session ID when the ticket is
2291 * presented and rely on the handshake to determine session resumption.
2292 * We choose the former approach because this fits in with assumptions
2293 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2294 * SHA256 is disabled) hash of the ticket.
2296 EVP_Digest(p
, ticklen
,
2297 s
->session
->session_id
, &s
->session
->session_id_length
,
2298 EVP_sha256(), NULL
);
2302 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2304 s
->state
= SSL_ST_ERR
;
2308 int ssl3_get_cert_status(SSL
*s
)
2311 unsigned long resplen
, n
;
2312 const unsigned char *p
;
2314 n
= s
->method
->ssl_get_message(s
,
2315 SSL3_ST_CR_CERT_STATUS_A
,
2316 SSL3_ST_CR_CERT_STATUS_B
,
2317 SSL3_MT_CERTIFICATE_STATUS
, 16384, &ok
);
2322 /* need at least status type + length */
2323 al
= SSL_AD_DECODE_ERROR
;
2324 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2327 p
= (unsigned char *)s
->init_msg
;
2328 if (*p
++ != TLSEXT_STATUSTYPE_ocsp
) {
2329 al
= SSL_AD_DECODE_ERROR
;
2330 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
2334 if (resplen
+ 4 != n
) {
2335 al
= SSL_AD_DECODE_ERROR
;
2336 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2339 OPENSSL_free(s
->tlsext_ocsp_resp
);
2340 s
->tlsext_ocsp_resp
= BUF_memdup(p
, resplen
);
2341 if (!s
->tlsext_ocsp_resp
) {
2342 al
= SSL_AD_INTERNAL_ERROR
;
2343 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
2346 s
->tlsext_ocsp_resplen
= resplen
;
2347 if (s
->ctx
->tlsext_status_cb
) {
2349 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2351 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
2352 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_INVALID_STATUS_RESPONSE
);
2356 al
= SSL_AD_INTERNAL_ERROR
;
2357 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
2363 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2364 s
->state
= SSL_ST_ERR
;
2369 int ssl3_get_server_done(SSL
*s
)
2374 /* Second to last param should be very small, like 0 :-) */
2375 n
= s
->method
->ssl_get_message(s
,
2376 SSL3_ST_CR_SRVR_DONE_A
,
2377 SSL3_ST_CR_SRVR_DONE_B
,
2378 SSL3_MT_SERVER_DONE
, 30, &ok
);
2383 /* should contain no data */
2384 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2385 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
, SSL_R_LENGTH_MISMATCH
);
2386 s
->state
= SSL_ST_ERR
;
2393 int ssl3_send_client_key_exchange(SSL
*s
)
2397 unsigned long alg_k
;
2398 #ifndef OPENSSL_NO_RSA
2400 EVP_PKEY
*pkey
= NULL
;
2402 #ifndef OPENSSL_NO_EC
2403 EC_KEY
*clnt_ecdh
= NULL
;
2404 const EC_POINT
*srvr_ecpoint
= NULL
;
2405 EVP_PKEY
*srvr_pub_pkey
= NULL
;
2406 unsigned char *encodedPoint
= NULL
;
2407 int encoded_pt_len
= 0;
2408 BN_CTX
*bn_ctx
= NULL
;
2410 unsigned char *pms
= NULL
;
2413 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
) {
2414 p
= ssl_handshake_start(s
);
2416 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2418 /* Fool emacs indentation */
2421 #ifndef OPENSSL_NO_RSA
2422 else if (alg_k
& SSL_kRSA
) {
2424 pmslen
= SSL_MAX_MASTER_KEY_LENGTH
;
2425 pms
= OPENSSL_malloc(pmslen
);
2429 if (s
->session
->sess_cert
== NULL
) {
2431 * We should always have a server certificate with SSL_kRSA.
2433 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2434 ERR_R_INTERNAL_ERROR
);
2438 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
2439 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
2442 X509_get_pubkey(s
->session
->
2443 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].
2445 if ((pkey
== NULL
) || (pkey
->type
!= EVP_PKEY_RSA
)
2446 || (pkey
->pkey
.rsa
== NULL
)) {
2447 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2448 ERR_R_INTERNAL_ERROR
);
2451 rsa
= pkey
->pkey
.rsa
;
2452 EVP_PKEY_free(pkey
);
2455 pms
[0] = s
->client_version
>> 8;
2456 pms
[1] = s
->client_version
& 0xff;
2457 if (RAND_bytes(pms
+ 2, pmslen
- 2) <= 0)
2461 /* Fix buf for TLS and beyond */
2462 if (s
->version
> SSL3_VERSION
)
2464 n
= RSA_public_encrypt(pmslen
, pms
, p
, rsa
, RSA_PKCS1_PADDING
);
2466 if (s
->options
& SSL_OP_PKCS1_CHECK_1
)
2468 if (s
->options
& SSL_OP_PKCS1_CHECK_2
)
2472 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2473 SSL_R_BAD_RSA_ENCRYPT
);
2477 /* Fix buf for TLS and beyond */
2478 if (s
->version
> SSL3_VERSION
) {
2484 #ifndef OPENSSL_NO_DH
2485 else if (alg_k
& (SSL_kDHE
| SSL_kDHr
| SSL_kDHd
)) {
2486 DH
*dh_srvr
, *dh_clnt
;
2487 SESS_CERT
*scert
= s
->session
->sess_cert
;
2489 if (scert
== NULL
) {
2490 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2491 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2492 SSL_R_UNEXPECTED_MESSAGE
);
2496 if (scert
->peer_dh_tmp
!= NULL
)
2497 dh_srvr
= scert
->peer_dh_tmp
;
2499 /* we get them from the cert */
2500 int idx
= scert
->peer_cert_type
;
2501 EVP_PKEY
*spkey
= NULL
;
2504 spkey
= X509_get_pubkey(scert
->peer_pkeys
[idx
].x509
);
2506 dh_srvr
= EVP_PKEY_get1_DH(spkey
);
2507 EVP_PKEY_free(spkey
);
2509 if (dh_srvr
== NULL
) {
2510 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2511 ERR_R_INTERNAL_ERROR
);
2515 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
2516 /* Use client certificate key */
2517 EVP_PKEY
*clkey
= s
->cert
->key
->privatekey
;
2520 dh_clnt
= EVP_PKEY_get1_DH(clkey
);
2521 if (dh_clnt
== NULL
) {
2522 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2523 ERR_R_INTERNAL_ERROR
);
2527 /* generate a new random key */
2528 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
2529 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2532 if (!DH_generate_key(dh_clnt
)) {
2533 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2539 pmslen
= DH_size(dh_clnt
);
2540 pms
= OPENSSL_malloc(pmslen
);
2545 * use the 'p' output buffer for the DH key, but make sure to
2546 * clear it out afterwards
2549 n
= DH_compute_key(pms
, dh_srvr
->pub_key
, dh_clnt
);
2550 if (scert
->peer_dh_tmp
== NULL
)
2554 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2560 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
)
2563 /* send off the data */
2564 n
= BN_num_bytes(dh_clnt
->pub_key
);
2566 BN_bn2bin(dh_clnt
->pub_key
, p
);
2572 /* perhaps clean things up a bit EAY EAY EAY EAY */
2576 #ifndef OPENSSL_NO_EC
2577 else if (alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
)) {
2578 const EC_GROUP
*srvr_group
= NULL
;
2580 int ecdh_clnt_cert
= 0;
2583 if (s
->session
->sess_cert
== NULL
) {
2584 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2586 SSL_R_UNEXPECTED_MESSAGE
);
2591 * Did we send out the client's ECDH share for use in premaster
2592 * computation as part of client certificate? If so, set
2593 * ecdh_clnt_cert to 1.
2595 if ((alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) && (s
->cert
!= NULL
)) {
2597 * XXX: For now, we do not support client
2598 * authentication using ECDH certificates.
2599 * To add such support, one needs to add
2600 * code that checks for appropriate
2601 * conditions and sets ecdh_clnt_cert to 1.
2602 * For example, the cert have an ECC
2603 * key on the same curve as the server's
2604 * and the key should be authorized for
2607 * One also needs to add code in ssl3_connect
2608 * to skip sending the certificate verify
2611 * if ((s->cert->key->privatekey != NULL) &&
2612 * (s->cert->key->privatekey->type ==
2613 * EVP_PKEY_EC) && ...)
2614 * ecdh_clnt_cert = 1;
2618 if (s
->session
->sess_cert
->peer_ecdh_tmp
!= NULL
) {
2619 tkey
= s
->session
->sess_cert
->peer_ecdh_tmp
;
2621 /* Get the Server Public Key from Cert */
2623 X509_get_pubkey(s
->session
->
2624 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
2625 if ((srvr_pub_pkey
== NULL
)
2626 || (srvr_pub_pkey
->type
!= EVP_PKEY_EC
)
2627 || (srvr_pub_pkey
->pkey
.ec
== NULL
)) {
2628 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2629 ERR_R_INTERNAL_ERROR
);
2633 tkey
= srvr_pub_pkey
->pkey
.ec
;
2636 srvr_group
= EC_KEY_get0_group(tkey
);
2637 srvr_ecpoint
= EC_KEY_get0_public_key(tkey
);
2639 if ((srvr_group
== NULL
) || (srvr_ecpoint
== NULL
)) {
2640 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2641 ERR_R_INTERNAL_ERROR
);
2645 if ((clnt_ecdh
= EC_KEY_new()) == NULL
) {
2646 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2647 ERR_R_MALLOC_FAILURE
);
2651 if (!EC_KEY_set_group(clnt_ecdh
, srvr_group
)) {
2652 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2655 if (ecdh_clnt_cert
) {
2657 * Reuse key info from our certificate We only need our
2658 * private key to perform the ECDH computation.
2660 const BIGNUM
*priv_key
;
2661 tkey
= s
->cert
->key
->privatekey
->pkey
.ec
;
2662 priv_key
= EC_KEY_get0_private_key(tkey
);
2663 if (priv_key
== NULL
) {
2664 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2665 ERR_R_MALLOC_FAILURE
);
2668 if (!EC_KEY_set_private_key(clnt_ecdh
, priv_key
)) {
2669 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2673 /* Generate a new ECDH key pair */
2674 if (!(EC_KEY_generate_key(clnt_ecdh
))) {
2675 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2682 * use the 'p' output buffer for the ECDH key, but make sure to
2683 * clear it out afterwards
2686 field_size
= EC_GROUP_get_degree(srvr_group
);
2687 if (field_size
<= 0) {
2688 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2691 pmslen
= (field_size
+ 7) / 8;
2692 pms
= OPENSSL_malloc(pmslen
);
2695 n
= ECDH_compute_key(pms
, pmslen
, srvr_ecpoint
, clnt_ecdh
, NULL
);
2696 if (n
<= 0 || pmslen
!= (size_t)n
) {
2697 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2701 if (ecdh_clnt_cert
) {
2702 /* Send empty client key exch message */
2706 * First check the size of encoding and allocate memory
2710 EC_POINT_point2oct(srvr_group
,
2711 EC_KEY_get0_public_key(clnt_ecdh
),
2712 POINT_CONVERSION_UNCOMPRESSED
,
2715 encodedPoint
= (unsigned char *)
2716 OPENSSL_malloc(encoded_pt_len
* sizeof(unsigned char));
2717 bn_ctx
= BN_CTX_new();
2718 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
)) {
2719 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2720 ERR_R_MALLOC_FAILURE
);
2724 /* Encode the public key */
2725 n
= EC_POINT_point2oct(srvr_group
,
2726 EC_KEY_get0_public_key(clnt_ecdh
),
2727 POINT_CONVERSION_UNCOMPRESSED
,
2728 encodedPoint
, encoded_pt_len
, bn_ctx
);
2730 *p
= n
; /* length of encoded point */
2731 /* Encoded point will be copied here */
2733 /* copy the point */
2734 memcpy(p
, encodedPoint
, n
);
2735 /* increment n to account for length field */
2739 /* Free allocated memory */
2740 BN_CTX_free(bn_ctx
);
2741 OPENSSL_free(encodedPoint
);
2742 EC_KEY_free(clnt_ecdh
);
2743 EVP_PKEY_free(srvr_pub_pkey
);
2745 #endif /* !OPENSSL_NO_EC */
2746 else if (alg_k
& SSL_kGOST
) {
2747 /* GOST key exchange message creation */
2748 EVP_PKEY_CTX
*pkey_ctx
;
2751 unsigned int md_len
;
2753 unsigned char shared_ukm
[32], tmp
[256];
2754 EVP_MD_CTX
*ukm_hash
;
2758 pms
= OPENSSL_malloc(pmslen
);
2763 * Get server sertificate PKEY and create ctx from it
2767 sess_cert
->peer_pkeys
[(keytype
= SSL_PKEY_GOST01
)].x509
;
2771 sess_cert
->peer_pkeys
[(keytype
= SSL_PKEY_GOST94
)].x509
;
2773 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2774 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2778 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
=
2779 X509_get_pubkey(peer_cert
), NULL
);
2781 * If we have send a certificate, and certificate key
2783 * * parameters match those of server certificate, use
2784 * certificate key for key exchange
2787 /* Otherwise, generate ephemeral key pair */
2789 EVP_PKEY_encrypt_init(pkey_ctx
);
2790 /* Generate session key */
2791 if (RAND_bytes(pms
, pmslen
) <= 0) {
2792 EVP_PKEY_CTX_free(pkey_ctx
);
2793 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2794 ERR_R_INTERNAL_ERROR
);
2798 * If we have client certificate, use its secret as peer key
2800 if (s
->s3
->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2801 if (EVP_PKEY_derive_set_peer
2802 (pkey_ctx
, s
->cert
->key
->privatekey
) <= 0) {
2804 * If there was an error - just ignore it. Ephemeral key
2811 * Compute shared IV and store it in algorithm-specific context
2814 ukm_hash
= EVP_MD_CTX_create();
2815 EVP_DigestInit(ukm_hash
,
2816 EVP_get_digestbynid(NID_id_GostR3411_94
));
2817 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
,
2819 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
,
2821 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2822 EVP_MD_CTX_destroy(ukm_hash
);
2823 if (EVP_PKEY_CTX_ctrl
2824 (pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
, EVP_PKEY_CTRL_SET_IV
, 8,
2826 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2830 /* Make GOST keytransport blob message */
2832 * Encapsulate it into sequence
2834 *(p
++) = V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
;
2836 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, pms
, pmslen
) < 0) {
2837 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2841 if (msglen
>= 0x80) {
2843 *(p
++) = msglen
& 0xff;
2846 *(p
++) = msglen
& 0xff;
2849 memcpy(p
, tmp
, msglen
);
2850 /* Check if pubkey from client certificate was used */
2851 if (EVP_PKEY_CTX_ctrl
2852 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0) {
2853 /* Set flag "skip certificate verify" */
2854 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2856 EVP_PKEY_CTX_free(pkey_ctx
);
2857 EVP_PKEY_free(pub_key
);
2860 #ifndef OPENSSL_NO_SRP
2861 else if (alg_k
& SSL_kSRP
) {
2862 if (s
->srp_ctx
.A
!= NULL
) {
2863 /* send off the data */
2864 n
= BN_num_bytes(s
->srp_ctx
.A
);
2866 BN_bn2bin(s
->srp_ctx
.A
, p
);
2869 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2870 ERR_R_INTERNAL_ERROR
);
2873 OPENSSL_free(s
->session
->srp_username
);
2874 s
->session
->srp_username
= BUF_strdup(s
->srp_ctx
.login
);
2875 if (s
->session
->srp_username
== NULL
) {
2876 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2877 ERR_R_MALLOC_FAILURE
);
2882 #ifndef OPENSSL_NO_PSK
2883 else if (alg_k
& SSL_kPSK
) {
2885 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2886 * \0-terminated identity. The last byte is for us for simulating
2889 char identity
[PSK_MAX_IDENTITY_LEN
+ 2];
2890 size_t identity_len
;
2891 unsigned char *t
= NULL
;
2892 unsigned int psk_len
= 0;
2896 if (s
->psk_client_callback
== NULL
) {
2897 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2898 SSL_R_PSK_NO_CLIENT_CB
);
2902 memset(identity
, 0, sizeof(identity
));
2903 /* Allocate maximum size buffer */
2904 pmslen
= PSK_MAX_PSK_LEN
* 2 + 4;
2905 pms
= OPENSSL_malloc(pmslen
);
2909 psk_len
= s
->psk_client_callback(s
, s
->ctx
->psk_identity_hint
,
2910 identity
, sizeof(identity
) - 1,
2912 if (psk_len
> PSK_MAX_PSK_LEN
) {
2913 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2914 ERR_R_INTERNAL_ERROR
);
2916 } else if (psk_len
== 0) {
2917 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2918 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2921 /* Change pmslen to real length */
2922 pmslen
= 2 + psk_len
+ 2 + psk_len
;
2923 identity
[PSK_MAX_IDENTITY_LEN
+ 1] = '\0';
2924 identity_len
= strlen(identity
);
2925 if (identity_len
> PSK_MAX_IDENTITY_LEN
) {
2926 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2927 ERR_R_INTERNAL_ERROR
);
2930 /* create PSK pre_master_secret */
2932 memmove(pms
+ psk_len
+ 4, pms
, psk_len
);
2934 memset(t
, 0, psk_len
);
2938 OPENSSL_free(s
->session
->psk_identity_hint
);
2939 s
->session
->psk_identity_hint
=
2940 BUF_strdup(s
->ctx
->psk_identity_hint
);
2941 if (s
->ctx
->psk_identity_hint
!= NULL
2942 && s
->session
->psk_identity_hint
== NULL
) {
2943 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2944 ERR_R_MALLOC_FAILURE
);
2948 OPENSSL_free(s
->session
->psk_identity
);
2949 s
->session
->psk_identity
= BUF_strdup(identity
);
2950 if (s
->session
->psk_identity
== NULL
) {
2951 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2952 ERR_R_MALLOC_FAILURE
);
2956 s2n(identity_len
, p
);
2957 memcpy(p
, identity
, identity_len
);
2958 n
= 2 + identity_len
;
2961 OPENSSL_cleanse(identity
, sizeof(identity
));
2963 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2969 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2970 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2974 if (!ssl_set_handshake_header(s
, SSL3_MT_CLIENT_KEY_EXCHANGE
, n
)) {
2975 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2976 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2980 s
->state
= SSL3_ST_CW_KEY_EXCH_B
;
2983 /* SSL3_ST_CW_KEY_EXCH_B */
2984 n
= ssl_do_write(s
);
2985 #ifndef OPENSSL_NO_SRP
2987 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
2989 * If everything written generate master key: no need to save PMS as
2990 * SRP_generate_client_master_secret generates it internally.
2993 if ((s
->session
->master_key_length
=
2994 SRP_generate_client_master_secret(s
,
2995 s
->session
->master_key
)) <
2997 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2998 ERR_R_INTERNAL_ERROR
);
3004 /* If we haven't written everything save PMS */
3007 s
->cert
->pmslen
= pmslen
;
3009 /* If we don't have a PMS restore */
3012 pmslen
= s
->cert
->pmslen
;
3015 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3016 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
3019 s
->session
->master_key_length
=
3020 s
->method
->ssl3_enc
->generate_master_secret(s
,
3022 session
->master_key
,
3024 OPENSSL_clear_free(pms
, pmslen
);
3025 s
->cert
->pms
= NULL
;
3026 if (s
->session
->master_key_length
< 0) {
3027 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3028 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3034 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3035 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
3037 OPENSSL_clear_free(pms
, pmslen
);
3038 s
->cert
->pms
= NULL
;
3039 #ifndef OPENSSL_NO_EC
3040 BN_CTX_free(bn_ctx
);
3041 OPENSSL_free(encodedPoint
);
3042 EC_KEY_free(clnt_ecdh
);
3043 EVP_PKEY_free(srvr_pub_pkey
);
3045 s
->state
= SSL_ST_ERR
;
3049 int ssl3_send_client_verify(SSL
*s
)
3052 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
3054 EVP_PKEY_CTX
*pctx
= NULL
;
3060 EVP_MD_CTX_init(&mctx
);
3062 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
) {
3063 p
= ssl_handshake_start(s
);
3064 pkey
= s
->cert
->key
->privatekey
;
3065 /* Create context from key and test if sha1 is allowed as digest */
3066 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
3067 EVP_PKEY_sign_init(pctx
);
3068 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) > 0) {
3069 if (!SSL_USE_SIGALGS(s
))
3070 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3073 [MD5_DIGEST_LENGTH
]));
3078 * For TLS v1.2 send signature algorithm and signature using agreed
3079 * digest and cached handshake records.
3081 if (SSL_USE_SIGALGS(s
)) {
3084 const EVP_MD
*md
= s
->cert
->key
->digest
;
3085 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
3086 if (hdatalen
<= 0 || !tls12_get_sigandhash(p
, pkey
, md
)) {
3087 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3092 fprintf(stderr
, "Using TLS 1.2 with client alg %s\n",
3095 if (!EVP_SignInit_ex(&mctx
, md
, NULL
)
3096 || !EVP_SignUpdate(&mctx
, hdata
, hdatalen
)
3097 || !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
3098 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_EVP_LIB
);
3104 * For extended master secret we've already digested cached
3107 if (s
->session
->flags
& SSL_SESS_FLAG_EXTMS
) {
3108 BIO_free(s
->s3
->handshake_buffer
);
3109 s
->s3
->handshake_buffer
= NULL
;
3110 s
->s3
->flags
&= ~TLS1_FLAGS_KEEP_HANDSHAKE
;
3111 } else if (!ssl3_digest_cached_records(s
))
3114 #ifndef OPENSSL_NO_RSA
3115 if (pkey
->type
== EVP_PKEY_RSA
) {
3116 s
->method
->ssl3_enc
->cert_verify_mac(s
, NID_md5
, &(data
[0]));
3117 if (RSA_sign(NID_md5_sha1
, data
,
3118 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
,
3119 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
3120 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_RSA_LIB
);
3127 #ifndef OPENSSL_NO_DSA
3128 if (pkey
->type
== EVP_PKEY_DSA
) {
3129 if (!DSA_sign(pkey
->save_type
,
3130 &(data
[MD5_DIGEST_LENGTH
]),
3131 SHA_DIGEST_LENGTH
, &(p
[2]),
3132 (unsigned int *)&j
, pkey
->pkey
.dsa
)) {
3133 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_DSA_LIB
);
3140 #ifndef OPENSSL_NO_EC
3141 if (pkey
->type
== EVP_PKEY_EC
) {
3142 if (!ECDSA_sign(pkey
->save_type
,
3143 &(data
[MD5_DIGEST_LENGTH
]),
3144 SHA_DIGEST_LENGTH
, &(p
[2]),
3145 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
3146 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_ECDSA_LIB
);
3153 if (pkey
->type
== NID_id_GostR3410_94
3154 || pkey
->type
== NID_id_GostR3410_2001
) {
3155 unsigned char signbuf
[64];
3157 size_t sigsize
= 64;
3158 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3159 NID_id_GostR3411_94
, data
);
3160 if (EVP_PKEY_sign(pctx
, signbuf
, &sigsize
, data
, 32) <= 0) {
3161 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3164 for (i
= 63, j
= 0; i
>= 0; j
++, i
--) {
3165 p
[2 + j
] = signbuf
[i
];
3170 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3173 if (!ssl_set_handshake_header(s
, SSL3_MT_CERTIFICATE_VERIFY
, n
)) {
3174 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3177 s
->state
= SSL3_ST_CW_CERT_VRFY_B
;
3179 EVP_MD_CTX_cleanup(&mctx
);
3180 EVP_PKEY_CTX_free(pctx
);
3181 return ssl_do_write(s
);
3183 EVP_MD_CTX_cleanup(&mctx
);
3184 EVP_PKEY_CTX_free(pctx
);
3185 s
->state
= SSL_ST_ERR
;
3190 * Check a certificate can be used for client authentication. Currently check
3191 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3192 * certificates can be used and optionally checks suitability for Suite B.
3194 static int ssl3_check_client_certificate(SSL
*s
)
3196 unsigned long alg_k
;
3197 if (!s
->cert
|| !s
->cert
->key
->x509
|| !s
->cert
->key
->privatekey
)
3199 /* If no suitable signature algorithm can't use certificate */
3200 if (SSL_USE_SIGALGS(s
) && !s
->cert
->key
->digest
)
3203 * If strict mode check suitability of chain before using it. This also
3204 * adjusts suite B digest if necessary.
3206 if (s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
&&
3207 !tls1_check_chain(s
, NULL
, NULL
, NULL
, -2))
3209 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3210 /* See if we can use client certificate for fixed DH */
3211 if (alg_k
& (SSL_kDHr
| SSL_kDHd
)) {
3212 SESS_CERT
*scert
= s
->session
->sess_cert
;
3213 int i
= scert
->peer_cert_type
;
3214 EVP_PKEY
*clkey
= NULL
, *spkey
= NULL
;
3215 clkey
= s
->cert
->key
->privatekey
;
3216 /* If client key not DH assume it can be used */
3217 if (EVP_PKEY_id(clkey
) != EVP_PKEY_DH
)
3220 spkey
= X509_get_pubkey(scert
->peer_pkeys
[i
].x509
);
3222 /* Compare server and client parameters */
3223 i
= EVP_PKEY_cmp_parameters(clkey
, spkey
);
3224 EVP_PKEY_free(spkey
);
3228 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
3233 int ssl3_send_client_certificate(SSL
*s
)
3236 EVP_PKEY
*pkey
= NULL
;
3239 if (s
->state
== SSL3_ST_CW_CERT_A
) {
3240 /* Let cert callback update client certificates if required */
3241 if (s
->cert
->cert_cb
) {
3242 i
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
3244 s
->rwstate
= SSL_X509_LOOKUP
;
3248 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3249 s
->state
= SSL_ST_ERR
;
3252 s
->rwstate
= SSL_NOTHING
;
3254 if (ssl3_check_client_certificate(s
))
3255 s
->state
= SSL3_ST_CW_CERT_C
;
3257 s
->state
= SSL3_ST_CW_CERT_B
;
3260 /* We need to get a client cert */
3261 if (s
->state
== SSL3_ST_CW_CERT_B
) {
3263 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3264 * return(-1); We then get retied later
3267 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
3269 s
->rwstate
= SSL_X509_LOOKUP
;
3272 s
->rwstate
= SSL_NOTHING
;
3273 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
3274 s
->state
= SSL3_ST_CW_CERT_B
;
3275 if (!SSL_use_certificate(s
, x509
) || !SSL_use_PrivateKey(s
, pkey
))
3277 } else if (i
== 1) {
3279 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,
3280 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
3284 EVP_PKEY_free(pkey
);
3285 if (i
&& !ssl3_check_client_certificate(s
))
3288 if (s
->version
== SSL3_VERSION
) {
3289 s
->s3
->tmp
.cert_req
= 0;
3290 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_CERTIFICATE
);
3293 s
->s3
->tmp
.cert_req
= 2;
3294 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
)) {
3295 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3296 s
->state
= SSL_ST_ERR
;
3302 /* Ok, we have a cert */
3303 s
->state
= SSL3_ST_CW_CERT_C
;
3306 if (s
->state
== SSL3_ST_CW_CERT_C
) {
3307 s
->state
= SSL3_ST_CW_CERT_D
;
3308 if (!ssl3_output_cert_chain(s
,
3309 (s
->s3
->tmp
.cert_req
==
3310 2) ? NULL
: s
->cert
->key
)) {
3311 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3312 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3313 s
->state
= SSL_ST_ERR
;
3317 /* SSL3_ST_CW_CERT_D */
3318 return ssl_do_write(s
);
3321 #define has_bits(i,m) (((i)&(m)) == (m))
3323 int ssl3_check_cert_and_algorithm(SSL
*s
)
3327 EVP_PKEY
*pkey
= NULL
;
3329 #ifndef OPENSSL_NO_RSA
3332 #ifndef OPENSSL_NO_DH
3336 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3337 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
3339 /* we don't have a certificate */
3340 if ((alg_a
& SSL_aNULL
) || (alg_k
& SSL_kPSK
))
3343 sc
= s
->session
->sess_cert
;
3345 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
3348 #ifndef OPENSSL_NO_RSA
3349 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
3351 #ifndef OPENSSL_NO_DH
3352 dh
= s
->session
->sess_cert
->peer_dh_tmp
;
3355 /* This is the passed certificate */
3357 idx
= sc
->peer_cert_type
;
3358 #ifndef OPENSSL_NO_EC
3359 if (idx
== SSL_PKEY_ECC
) {
3360 if (ssl_check_srvr_ecc_cert_and_alg(sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
3362 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_BAD_ECC_CERT
);
3367 } else if (alg_a
& SSL_aECDSA
) {
3368 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3369 SSL_R_MISSING_ECDSA_SIGNING_CERT
);
3371 } else if (alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) {
3372 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_MISSING_ECDH_CERT
);
3376 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
3377 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
3378 EVP_PKEY_free(pkey
);
3380 /* Check that we have a certificate if we require one */
3381 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
| EVP_PKT_SIGN
)) {
3382 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3383 SSL_R_MISSING_RSA_SIGNING_CERT
);
3386 #ifndef OPENSSL_NO_DSA
3387 else if ((alg_a
& SSL_aDSS
) && !has_bits(i
, EVP_PK_DSA
| EVP_PKT_SIGN
)) {
3388 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3389 SSL_R_MISSING_DSA_SIGNING_CERT
);
3393 #ifndef OPENSSL_NO_RSA
3394 if ((alg_k
& SSL_kRSA
) &&
3395 !(has_bits(i
, EVP_PK_RSA
| EVP_PKT_ENC
) || (rsa
!= NULL
))) {
3396 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3397 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
3401 #ifndef OPENSSL_NO_DH
3402 if ((alg_k
& SSL_kDHE
) &&
3403 !(has_bits(i
, EVP_PK_DH
| EVP_PKT_EXCH
) || (dh
!= NULL
))) {
3404 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_MISSING_DH_KEY
);
3406 } else if ((alg_k
& SSL_kDHr
) && !SSL_USE_SIGALGS(s
) &&
3407 !has_bits(i
, EVP_PK_DH
| EVP_PKS_RSA
)) {
3408 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3409 SSL_R_MISSING_DH_RSA_CERT
);
3412 # ifndef OPENSSL_NO_DSA
3413 else if ((alg_k
& SSL_kDHd
) && !SSL_USE_SIGALGS(s
) &&
3414 !has_bits(i
, EVP_PK_DH
| EVP_PKS_DSA
)) {
3415 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3416 SSL_R_MISSING_DH_DSA_CERT
);
3422 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && !has_bits(i
, EVP_PKT_EXP
)) {
3423 #ifndef OPENSSL_NO_RSA
3424 if (alg_k
& SSL_kRSA
) {
3426 || RSA_size(rsa
) * 8 >
3427 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3428 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3429 SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
3434 #ifndef OPENSSL_NO_DH
3435 if (alg_k
& (SSL_kDHE
| SSL_kDHr
| SSL_kDHd
)) {
3437 || DH_size(dh
) * 8 >
3438 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3439 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3440 SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
3446 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3447 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
3453 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3458 #ifndef OPENSSL_NO_TLSEXT
3460 * Normally, we can tell if the server is resuming the session from
3461 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3462 * message after the ServerHello to determine if the server is resuming.
3463 * Therefore, we allow EAP-FAST to peek ahead.
3464 * ssl3_check_finished returns 1 if we are resuming from an external
3465 * pre-shared secret, we have a "ticket" and the next server handshake message
3466 * is Finished; and 0 otherwise. It returns -1 upon an error.
3468 static int ssl3_check_finished(SSL
*s
)
3472 if (s
->version
< TLS1_VERSION
|| !s
->tls_session_secret_cb
||
3473 !s
->session
->tlsext_tick
)
3476 /* Need to permit this temporarily, in case the next message is Finished. */
3477 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
3479 * This function is called when we might get a Certificate message instead,
3480 * so permit appropriate message length.
3481 * We ignore the return value as we're only interested in the message type
3482 * and not its length.
3484 s
->method
->ssl_get_message(s
,
3487 -1, s
->max_cert_list
, &ok
);
3488 s
->s3
->flags
&= ~SSL3_FLAGS_CCS_OK
;
3493 s
->s3
->tmp
.reuse_message
= 1;
3495 if (s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
)
3498 /* If we're not done, then the CCS arrived early and we should bail. */
3499 if (s
->s3
->change_cipher_spec
) {
3500 SSLerr(SSL_F_SSL3_CHECK_FINISHED
, SSL_R_CCS_RECEIVED_EARLY
);
3501 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
3508 # ifndef OPENSSL_NO_NEXTPROTONEG
3509 int ssl3_send_next_proto(SSL
*s
)
3511 unsigned int len
, padding_len
;
3514 if (s
->state
== SSL3_ST_CW_NEXT_PROTO_A
) {
3515 len
= s
->next_proto_negotiated_len
;
3516 padding_len
= 32 - ((len
+ 2) % 32);
3517 d
= (unsigned char *)s
->init_buf
->data
;
3519 memcpy(d
+ 5, s
->next_proto_negotiated
, len
);
3520 d
[5 + len
] = padding_len
;
3521 memset(d
+ 6 + len
, 0, padding_len
);
3522 *(d
++) = SSL3_MT_NEXT_PROTO
;
3523 l2n3(2 + len
+ padding_len
, d
);
3524 s
->state
= SSL3_ST_CW_NEXT_PROTO_B
;
3525 s
->init_num
= 4 + 2 + len
+ padding_len
;
3529 return ssl3_do_write(s
, SSL3_RT_HANDSHAKE
);
3534 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
3537 #ifndef OPENSSL_NO_ENGINE
3538 if (s
->ctx
->client_cert_engine
) {
3539 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
3540 SSL_get_client_CA_list(s
),
3541 px509
, ppkey
, NULL
, NULL
, NULL
);
3546 if (s
->ctx
->client_cert_cb
)
3547 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);
3551 int ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
,
3553 int (*put_cb
) (const SSL_CIPHER
*,
3559 int empty_reneg_info_scsv
= !s
->renegotiate
;
3560 /* Set disabled masks for this session */
3561 ssl_set_client_disabled(s
);
3567 put_cb
= s
->method
->put_cipher_by_char
;
3569 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); i
++) {
3570 c
= sk_SSL_CIPHER_value(sk
, i
);
3571 /* Skip disabled ciphers */
3572 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_SUPPORTED
))
3574 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3575 if (c
->id
== SSL3_CK_SCSV
) {
3576 if (!empty_reneg_info_scsv
)
3579 empty_reneg_info_scsv
= 0;
3586 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3590 if (empty_reneg_info_scsv
) {
3591 static SSL_CIPHER scsv
= {
3592 0, NULL
, SSL3_CK_SCSV
, 0, 0, 0, 0, 0, 0, 0, 0, 0
3594 j
= put_cb(&scsv
, p
);
3596 #ifdef OPENSSL_RI_DEBUG
3598 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3601 if (s
->mode
& SSL_MODE_SEND_FALLBACK_SCSV
) {
3602 static SSL_CIPHER scsv
= {
3603 0, NULL
, SSL3_CK_FALLBACK_SCSV
, 0, 0, 0, 0, 0, 0, 0, 0, 0
3605 j
= put_cb(&scsv
, p
);