1 /* $OpenBSD: s3_clnt.c,v 1.137 2016/03/11 07:08:45 mmcc 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.
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
155 #include "ssl_locl.h"
157 #include <openssl/bn.h>
158 #include <openssl/buffer.h>
159 #include <openssl/dh.h>
160 #include <openssl/evp.h>
161 #include <openssl/md5.h>
162 #include <openssl/objects.h>
164 #ifndef OPENSSL_NO_ENGINE
165 #include <openssl/engine.h>
167 #ifndef OPENSSL_NO_GOST
168 #include <openssl/gost.h>
171 #include "bytestring.h"
173 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
);
178 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
180 int new_state
, state
, skip
= 0;
185 if (s
->info_callback
!= NULL
)
186 cb
= s
->info_callback
;
187 else if (s
->ctx
->info_callback
!= NULL
)
188 cb
= s
->ctx
->info_callback
;
191 if (!SSL_in_init(s
) || SSL_in_before(s
))
198 case SSL_ST_RENEGOTIATE
:
200 s
->state
= SSL_ST_CONNECT
;
201 s
->ctx
->stats
.sess_connect_renegotiate
++;
205 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
206 case SSL_ST_OK
|SSL_ST_CONNECT
:
210 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
212 if ((s
->version
& 0xff00 ) != 0x0300) {
213 SSLerr(SSL_F_SSL3_CONNECT
,
214 ERR_R_INTERNAL_ERROR
);
219 /* s->version=SSL3_VERSION; */
220 s
->type
= SSL_ST_CONNECT
;
222 if (!ssl3_setup_init_buffer(s
)) {
226 if (!ssl3_setup_buffers(s
)) {
230 if (!ssl_init_wbio_buffer(s
, 0)) {
235 /* don't push the buffering BIO quite yet */
237 if (!tls1_init_finished_mac(s
)) {
242 s
->state
= SSL3_ST_CW_CLNT_HELLO_A
;
243 s
->ctx
->stats
.sess_connect
++;
247 case SSL3_ST_CW_CLNT_HELLO_A
:
248 case SSL3_ST_CW_CLNT_HELLO_B
:
251 ret
= ssl3_client_hello(s
);
254 s
->state
= SSL3_ST_CR_SRVR_HELLO_A
;
257 /* turn on buffering for the next lot of output */
258 if (s
->bbio
!= s
->wbio
)
259 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
263 case SSL3_ST_CR_SRVR_HELLO_A
:
264 case SSL3_ST_CR_SRVR_HELLO_B
:
265 ret
= ssl3_get_server_hello(s
);
270 s
->state
= SSL3_ST_CR_FINISHED_A
;
271 if (s
->tlsext_ticket_expected
) {
272 /* receive renewed session ticket */
273 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
276 s
->state
= SSL3_ST_CR_CERT_A
;
280 case SSL3_ST_CR_CERT_A
:
281 case SSL3_ST_CR_CERT_B
:
282 ret
= ssl3_check_finished(s
);
287 if (s
->tlsext_ticket_expected
)
288 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
290 s
->state
= SSL3_ST_CR_FINISHED_A
;
294 /* Check if it is anon DH/ECDH. */
295 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
&
297 ret
= ssl3_get_server_certificate(s
);
300 if (s
->tlsext_status_expected
)
301 s
->state
= SSL3_ST_CR_CERT_STATUS_A
;
303 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
306 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
311 case SSL3_ST_CR_KEY_EXCH_A
:
312 case SSL3_ST_CR_KEY_EXCH_B
:
313 ret
= ssl3_get_key_exchange(s
);
316 s
->state
= SSL3_ST_CR_CERT_REQ_A
;
320 * At this point we check that we have the
321 * required stuff from the server.
323 if (!ssl3_check_cert_and_algorithm(s
)) {
329 case SSL3_ST_CR_CERT_REQ_A
:
330 case SSL3_ST_CR_CERT_REQ_B
:
331 ret
= ssl3_get_certificate_request(s
);
334 s
->state
= SSL3_ST_CR_SRVR_DONE_A
;
338 case SSL3_ST_CR_SRVR_DONE_A
:
339 case SSL3_ST_CR_SRVR_DONE_B
:
340 ret
= ssl3_get_server_done(s
);
343 if (s
->s3
->tmp
.cert_req
)
344 s
->state
= SSL3_ST_CW_CERT_A
;
346 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
351 case SSL3_ST_CW_CERT_A
:
352 case SSL3_ST_CW_CERT_B
:
353 case SSL3_ST_CW_CERT_C
:
354 case SSL3_ST_CW_CERT_D
:
355 ret
= ssl3_send_client_certificate(s
);
358 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
362 case SSL3_ST_CW_KEY_EXCH_A
:
363 case SSL3_ST_CW_KEY_EXCH_B
:
364 ret
= ssl3_send_client_key_exchange(s
);
368 * EAY EAY EAY need to check for DH fix cert
372 * For TLS, cert_req is set to 2, so a cert chain
373 * of nothing is sent, but no verify packet is sent
376 * XXX: For now, we do not support client
377 * authentication in ECDH cipher suites with
378 * ECDH (rather than ECDSA) certificates.
379 * We need to skip the certificate verify
380 * message when client's ECDH public key is sent
381 * inside the client certificate.
383 if (s
->s3
->tmp
.cert_req
== 1) {
384 s
->state
= SSL3_ST_CW_CERT_VRFY_A
;
386 s
->state
= SSL3_ST_CW_CHANGE_A
;
387 s
->s3
->change_cipher_spec
= 0;
389 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
390 s
->state
= SSL3_ST_CW_CHANGE_A
;
391 s
->s3
->change_cipher_spec
= 0;
397 case SSL3_ST_CW_CERT_VRFY_A
:
398 case SSL3_ST_CW_CERT_VRFY_B
:
399 ret
= ssl3_send_client_verify(s
);
402 s
->state
= SSL3_ST_CW_CHANGE_A
;
404 s
->s3
->change_cipher_spec
= 0;
407 case SSL3_ST_CW_CHANGE_A
:
408 case SSL3_ST_CW_CHANGE_B
:
409 ret
= ssl3_send_change_cipher_spec(s
,
410 SSL3_ST_CW_CHANGE_A
, SSL3_ST_CW_CHANGE_B
);
414 if (s
->s3
->next_proto_neg_seen
)
415 s
->state
= SSL3_ST_CW_NEXT_PROTO_A
;
417 s
->state
= SSL3_ST_CW_FINISHED_A
;
420 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
421 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
426 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
427 SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
434 case SSL3_ST_CW_NEXT_PROTO_A
:
435 case SSL3_ST_CW_NEXT_PROTO_B
:
436 ret
= ssl3_send_next_proto(s
);
439 s
->state
= SSL3_ST_CW_FINISHED_A
;
442 case SSL3_ST_CW_FINISHED_A
:
443 case SSL3_ST_CW_FINISHED_B
:
444 ret
= ssl3_send_finished(s
, SSL3_ST_CW_FINISHED_A
,
445 SSL3_ST_CW_FINISHED_B
,
446 s
->method
->ssl3_enc
->client_finished_label
,
447 s
->method
->ssl3_enc
->client_finished_label_len
);
450 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
451 s
->state
= SSL3_ST_CW_FLUSH
;
454 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
456 s
->s3
->tmp
.next_state
= SSL_ST_OK
;
458 SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
459 s
->state
= SSL_ST_OK
;
460 s
->s3
->flags
|=SSL3_FLAGS_POP_BUFFER
;
461 s
->s3
->delay_buf_pop_ret
= 0;
464 /* Allow NewSessionTicket if ticket expected */
465 if (s
->tlsext_ticket_expected
)
466 s
->s3
->tmp
.next_state
=
467 SSL3_ST_CR_SESSION_TICKET_A
;
470 s
->s3
->tmp
.next_state
= SSL3_ST_CR_FINISHED_A
;
475 case SSL3_ST_CR_SESSION_TICKET_A
:
476 case SSL3_ST_CR_SESSION_TICKET_B
:
477 ret
= ssl3_get_new_session_ticket(s
);
480 s
->state
= SSL3_ST_CR_FINISHED_A
;
484 case SSL3_ST_CR_CERT_STATUS_A
:
485 case SSL3_ST_CR_CERT_STATUS_B
:
486 ret
= ssl3_get_cert_status(s
);
489 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
493 case SSL3_ST_CR_FINISHED_A
:
494 case SSL3_ST_CR_FINISHED_B
:
495 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
496 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
497 SSL3_ST_CR_FINISHED_B
);
502 s
->state
= SSL3_ST_CW_CHANGE_A
;
504 s
->state
= SSL_ST_OK
;
508 case SSL3_ST_CW_FLUSH
:
509 s
->rwstate
= SSL_WRITING
;
510 if (BIO_flush(s
->wbio
) <= 0) {
514 s
->rwstate
= SSL_NOTHING
;
515 s
->state
= s
->s3
->tmp
.next_state
;
519 /* clean a few things up */
520 tls1_cleanup_key_block(s
);
522 if (s
->init_buf
!= NULL
) {
523 BUF_MEM_free(s
->init_buf
);
528 * If we are not 'joining' the last two packets,
529 * remove the buffering now
531 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
532 ssl_free_wbio_buffer(s
);
533 /* else do it later in ssl3_write */
539 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
541 s
->ctx
->stats
.sess_hit
++;
545 s
->handshake_func
= ssl3_connect
;
546 s
->ctx
->stats
.sess_connect_good
++;
549 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
555 SSLerr(SSL_F_SSL3_CONNECT
,
556 SSL_R_UNKNOWN_STATE
);
562 /* did we do anything */
563 if (!s
->s3
->tmp
.reuse_message
&& !skip
) {
565 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
569 if ((cb
!= NULL
) && (s
->state
!= state
)) {
570 new_state
= s
->state
;
572 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
573 s
->state
= new_state
;
582 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
588 ssl3_client_hello(SSL
*s
)
590 unsigned char *bufend
, *p
, *d
;
593 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
) {
594 SSL_SESSION
*sess
= s
->session
;
596 if ((sess
== NULL
) ||
597 (sess
->ssl_version
!= s
->version
) ||
598 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
599 (sess
->not_resumable
)) {
600 if (!ssl_get_new_session(s
, 0))
603 /* else use the pre-loaded session */
606 * If a DTLS ClientHello message is being resent after a
607 * HelloVerifyRequest, we must retain the original client
610 if (!SSL_IS_DTLS(s
) || s
->d1
->send_cookie
== 0)
611 arc4random_buf(s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
613 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_CLIENT_HELLO
);
616 * Version indicates the negotiated version: for example from
617 * an SSLv2/v3 compatible client hello). The client_version
618 * field is the maximum version we permit and it is also
619 * used in RSA encrypted premaster secrets. Some servers can
620 * choke if we initially report a higher version then
621 * renegotiate to a lower one in the premaster secret. This
622 * didn't happen with TLS 1.0 as most servers supported it
623 * but it can with TLS 1.1 or later if the server only supports
626 * Possible scenario with previous logic:
627 * 1. Client hello indicates TLS 1.2
628 * 2. Server hello says TLS 1.0
629 * 3. RSA encrypted premaster secret uses 1.2.
630 * 4. Handhaked proceeds using TLS 1.0.
631 * 5. Server sends hello request to renegotiate.
632 * 6. Client hello indicates TLS v1.0 as we now
633 * know that is maximum server supports.
634 * 7. Server chokes on RSA encrypted premaster secret
635 * containing version 1.0.
637 * For interoperability it should be OK to always use the
638 * maximum version we support in client hello and then rely
639 * on the checking of version to ensure the servers isn't
640 * being inconsistent: for example initially negotiating with
641 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
642 * client_version in client hello and not resetting it to
643 * the negotiated version.
645 *(p
++) = s
->client_version
>> 8;
646 *(p
++) = s
->client_version
& 0xff;
649 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
650 p
+= SSL3_RANDOM_SIZE
;
656 i
= s
->session
->session_id_length
;
659 if (i
> (int)sizeof(s
->session
->session_id
)) {
660 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,
661 ERR_R_INTERNAL_ERROR
);
664 memcpy(p
, s
->session
->session_id
, i
);
669 if (SSL_IS_DTLS(s
)) {
670 if (s
->d1
->cookie_len
> sizeof(s
->d1
->cookie
)) {
671 SSLerr(SSL_F_DTLS1_CLIENT_HELLO
,
672 ERR_R_INTERNAL_ERROR
);
675 *(p
++) = s
->d1
->cookie_len
;
676 memcpy(p
, s
->d1
->cookie
, s
->d1
->cookie_len
);
677 p
+= s
->d1
->cookie_len
;
680 /* Ciphers supported */
681 i
= ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &p
[2]);
683 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,
684 SSL_R_NO_CIPHERS_AVAILABLE
);
690 /* add in (no) COMPRESSION */
692 *(p
++) = 0; /* Add the NULL method */
695 bufend
= (unsigned char *)s
->init_buf
->data
+
696 SSL3_RT_MAX_PLAIN_LENGTH
;
697 if ((p
= ssl_add_clienthello_tlsext(s
, p
, bufend
)) == NULL
) {
698 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,
699 ERR_R_INTERNAL_ERROR
);
703 ssl3_handshake_msg_finish(s
, p
- d
);
705 s
->state
= SSL3_ST_CW_CLNT_HELLO_B
;
708 /* SSL3_ST_CW_CLNT_HELLO_B */
709 return (ssl3_handshake_write(s
));
716 ssl3_get_server_hello(SSL
*s
)
718 STACK_OF(SSL_CIPHER
) *sk
;
720 unsigned char *p
, *q
, *d
;
723 uint16_t cipher_value
;
727 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_SRVR_HELLO_A
,
728 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, /* ?? */ &ok
);
733 if (SSL_IS_DTLS(s
)) {
734 if (s
->s3
->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
735 if (s
->d1
->send_cookie
== 0) {
736 s
->s3
->tmp
.reuse_message
= 1;
739 /* Already sent a cookie. */
740 al
= SSL_AD_UNEXPECTED_MESSAGE
;
741 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
742 SSL_R_BAD_MESSAGE_TYPE
);
748 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
749 al
= SSL_AD_UNEXPECTED_MESSAGE
;
750 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
751 SSL_R_BAD_MESSAGE_TYPE
);
755 d
= p
= (unsigned char *)s
->init_msg
;
759 if ((p
[0] != (s
->version
>> 8)) || (p
[1] != (s
->version
& 0xff))) {
760 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
761 s
->version
= (s
->version
&0xff00) | p
[1];
762 al
= SSL_AD_PROTOCOL_VERSION
;
767 /* load the server hello data */
769 if (p
+ SSL3_RANDOM_SIZE
+ 1 - d
> n
)
772 /* load the server random */
773 memcpy(s
->s3
->server_random
, p
, SSL3_RANDOM_SIZE
);
774 p
+= SSL3_RANDOM_SIZE
;
776 /* get the session-id */
779 if ((j
> sizeof s
->session
->session_id
) ||
780 (j
> SSL3_SESSION_ID_SIZE
)) {
781 al
= SSL_AD_ILLEGAL_PARAMETER
;
782 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
783 SSL_R_SSL3_SESSION_ID_TOO_LONG
);
787 if (p
+ j
+ 2 - d
> n
)
790 /* Get the cipher value. */
792 n2s(q
, cipher_value
);
795 * Check if we want to resume the session based on external
798 if (s
->tls_session_secret_cb
) {
799 SSL_CIPHER
*pref_cipher
= NULL
;
800 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
801 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
802 &s
->session
->master_key_length
, NULL
, &pref_cipher
,
803 s
->tls_session_secret_cb_arg
)) {
804 s
->session
->cipher
= pref_cipher
? pref_cipher
:
805 ssl3_get_cipher_by_value(cipher_value
);
806 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
810 if (j
!= 0 && j
== s
->session
->session_id_length
&&
811 timingsafe_memcmp(p
, s
->session
->session_id
, j
) == 0) {
812 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
||
813 timingsafe_memcmp(s
->session
->sid_ctx
,
814 s
->sid_ctx
, s
->sid_ctx_length
) != 0) {
815 /* actually a client application bug */
816 al
= SSL_AD_ILLEGAL_PARAMETER
;
817 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
818 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
821 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
824 /* a miss or crap from the other end */
826 /* If we were trying for session-id reuse, make a new
827 * SSL_SESSION so we don't stuff up other people */
829 if (s
->session
->session_id_length
> 0) {
830 if (!ssl_get_new_session(s
, 0)) {
831 al
= SSL_AD_INTERNAL_ERROR
;
835 s
->session
->session_id_length
= j
;
836 memcpy(s
->session
->session_id
, p
, j
); /* j could be 0 */
840 if ((c
= ssl3_get_cipher_by_value(cipher_value
)) == NULL
) {
842 al
= SSL_AD_ILLEGAL_PARAMETER
;
843 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
844 SSL_R_UNKNOWN_CIPHER_RETURNED
);
848 /* TLS v1.2 only ciphersuites require v1.2 or later */
849 if ((c
->algorithm_ssl
& SSL_TLSV1_2
) &&
850 (TLS1_get_version(s
) < TLS1_2_VERSION
)) {
851 al
= SSL_AD_ILLEGAL_PARAMETER
;
852 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
853 SSL_R_WRONG_CIPHER_RETURNED
);
856 p
+= SSL3_CIPHER_VALUE_SIZE
;
858 sk
= ssl_get_ciphers_by_id(s
);
859 i
= sk_SSL_CIPHER_find(sk
, c
);
861 /* we did not say we would use this cipher */
862 al
= SSL_AD_ILLEGAL_PARAMETER
;
863 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
864 SSL_R_WRONG_CIPHER_RETURNED
);
869 * Depending on the session caching (internal/external), the cipher
870 * and/or cipher_id values may not be set. Make sure that
871 * cipher_id is set and use it for comparison.
873 if (s
->session
->cipher
)
874 s
->session
->cipher_id
= s
->session
->cipher
->id
;
875 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
876 al
= SSL_AD_ILLEGAL_PARAMETER
;
877 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
878 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
881 s
->s3
->tmp
.new_cipher
= c
;
883 * Don't digest cached records if no sigalgs: we may need them for
884 * client authentication.
886 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
887 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) &&
888 !tls1_digest_cached_records(s
)) {
889 al
= SSL_AD_INTERNAL_ERROR
;
892 /* lets get the compression algorithm */
897 al
= SSL_AD_ILLEGAL_PARAMETER
;
898 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
899 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
904 if (!ssl_parse_serverhello_tlsext(s
, &p
, d
, n
, &al
)) {
905 /* 'al' set by ssl_parse_serverhello_tlsext */
906 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_PARSE_TLSEXT
);
909 if (ssl_check_serverhello_tlsext(s
) <= 0) {
910 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SERVERHELLO_TLSEXT
);
920 /* wrong packet length */
921 al
= SSL_AD_DECODE_ERROR
;
922 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_PACKET_LENGTH
);
924 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
930 ssl3_get_server_certificate(SSL
*s
)
932 int al
, i
, ok
, ret
= -1;
936 const unsigned char *q
;
937 STACK_OF(X509
) *sk
= NULL
;
939 EVP_PKEY
*pkey
= NULL
;
941 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
942 SSL3_ST_CR_CERT_B
, -1, s
->max_cert_list
, &ok
);
947 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
948 s
->s3
->tmp
.reuse_message
= 1;
952 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
953 al
= SSL_AD_UNEXPECTED_MESSAGE
;
954 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
955 SSL_R_BAD_MESSAGE_TYPE
);
960 if ((sk
= sk_X509_new_null()) == NULL
) {
961 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
962 ERR_R_MALLOC_FAILURE
);
969 CBS_init(&cbs
, s
->init_msg
, n
);
970 if (CBS_len(&cbs
) < 3)
973 if (!CBS_get_u24_length_prefixed(&cbs
, &cert_list
) ||
974 CBS_len(&cbs
) != 0) {
975 al
= SSL_AD_DECODE_ERROR
;
976 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
977 SSL_R_LENGTH_MISMATCH
);
981 while (CBS_len(&cert_list
) > 0) {
984 if (CBS_len(&cert_list
) < 3)
986 if (!CBS_get_u24_length_prefixed(&cert_list
, &cert
)) {
987 al
= SSL_AD_DECODE_ERROR
;
988 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
989 SSL_R_CERT_LENGTH_MISMATCH
);
994 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
996 al
= SSL_AD_BAD_CERTIFICATE
;
997 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1001 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
1002 al
= SSL_AD_DECODE_ERROR
;
1003 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1004 SSL_R_CERT_LENGTH_MISMATCH
);
1007 if (!sk_X509_push(sk
, x
)) {
1008 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1009 ERR_R_MALLOC_FAILURE
);
1015 i
= ssl_verify_cert_chain(s
, sk
);
1016 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)) {
1017 al
= ssl_verify_alarm_type(s
->verify_result
);
1018 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1019 SSL_R_CERTIFICATE_VERIFY_FAILED
);
1023 ERR_clear_error(); /* but we keep s->verify_result */
1025 sc
= ssl_sess_cert_new();
1028 if (s
->session
->sess_cert
)
1029 ssl_sess_cert_free(s
->session
->sess_cert
);
1030 s
->session
->sess_cert
= sc
;
1032 sc
->cert_chain
= sk
;
1034 * Inconsistency alert: cert_chain does include the peer's
1035 * certificate, which we don't include in s3_srvr.c
1037 x
= sk_X509_value(sk
, 0);
1039 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1041 pkey
= X509_get_pubkey(x
);
1043 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1046 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1047 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1051 i
= ssl_cert_type(x
, pkey
);
1055 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1056 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1060 sc
->peer_cert_type
= i
;
1061 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1063 * Why would the following ever happen?
1064 * We just created sc a couple of lines ago.
1066 X509_free(sc
->peer_pkeys
[i
].x509
);
1067 sc
->peer_pkeys
[i
].x509
= x
;
1068 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1070 X509_free(s
->session
->peer
);
1071 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1072 s
->session
->peer
= x
;
1073 s
->session
->verify_result
= s
->verify_result
;
1080 /* wrong packet length */
1081 al
= SSL_AD_DECODE_ERROR
;
1082 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1083 SSL_R_BAD_PACKET_LENGTH
);
1085 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1088 EVP_PKEY_free(pkey
);
1090 sk_X509_pop_free(sk
, X509_free
);
1095 ssl3_get_key_exchange(SSL
*s
)
1097 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
*2];
1099 unsigned char *param
, *p
;
1100 int al
, i
, j
, param_len
, ok
;
1101 long n
, alg_k
, alg_a
;
1102 EVP_PKEY
*pkey
= NULL
;
1103 const EVP_MD
*md
= NULL
;
1106 EC_KEY
*ecdh
= NULL
;
1107 BN_CTX
*bn_ctx
= NULL
;
1108 EC_POINT
*srvr_ecpoint
= NULL
;
1110 int encoded_pt_len
= 0;
1112 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1113 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1116 * Use same message size as in ssl3_get_certificate_request()
1117 * as ServerKeyExchange message may be skipped.
1119 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_KEY_EXCH_A
,
1120 SSL3_ST_CR_KEY_EXCH_B
, -1, s
->max_cert_list
, &ok
);
1124 EVP_MD_CTX_init(&md_ctx
);
1126 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1128 * Do not skip server key exchange if this cipher suite uses
1131 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
1132 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1133 SSL_R_UNEXPECTED_MESSAGE
);
1134 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1138 s
->s3
->tmp
.reuse_message
= 1;
1139 EVP_MD_CTX_cleanup(&md_ctx
);
1143 if (s
->session
->sess_cert
!= NULL
) {
1144 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
1145 s
->session
->sess_cert
->peer_dh_tmp
= NULL
;
1147 EC_KEY_free(s
->session
->sess_cert
->peer_ecdh_tmp
);
1148 s
->session
->sess_cert
->peer_ecdh_tmp
= NULL
;
1150 s
->session
->sess_cert
= ssl_sess_cert_new();
1151 if (s
->session
->sess_cert
== NULL
)
1155 param
= p
= (unsigned char *)s
->init_msg
;
1158 if (alg_k
& SSL_kDHE
) {
1159 if ((dh
= DH_new()) == NULL
) {
1160 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1168 if (param_len
> n
) {
1169 al
= SSL_AD_DECODE_ERROR
;
1170 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1171 SSL_R_BAD_DH_P_LENGTH
);
1174 if (!(dh
->p
= BN_bin2bn(p
, i
, NULL
))) {
1175 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1181 if (param_len
+ 2 > n
)
1185 if (param_len
> n
) {
1186 al
= SSL_AD_DECODE_ERROR
;
1187 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1188 SSL_R_BAD_DH_G_LENGTH
);
1191 if (!(dh
->g
= BN_bin2bn(p
, i
, NULL
))) {
1192 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1198 if (param_len
+ 2 > n
)
1202 if (param_len
> n
) {
1203 al
= SSL_AD_DECODE_ERROR
;
1204 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1205 SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1208 if (!(dh
->pub_key
= BN_bin2bn(p
, i
, NULL
))) {
1209 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1217 * Check the strength of the DH key just constructed.
1218 * Discard keys weaker than 1024 bits.
1221 if (DH_size(dh
) < 1024 / 8) {
1222 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1223 SSL_R_BAD_DH_P_LENGTH
);
1227 if (alg_a
& SSL_aRSA
)
1228 pkey
= X509_get_pubkey(
1229 s
->session
->sess_cert
->peer_pkeys
[
1230 SSL_PKEY_RSA_ENC
].x509
);
1231 else if (alg_a
& SSL_aDSS
)
1232 pkey
= X509_get_pubkey(
1233 s
->session
->sess_cert
->peer_pkeys
[
1234 SSL_PKEY_DSA_SIGN
].x509
);
1235 /* else anonymous DH, so no certificate or pkey. */
1237 s
->session
->sess_cert
->peer_dh_tmp
= dh
;
1239 } else if (alg_k
& SSL_kECDHE
) {
1240 const EC_GROUP
*group
;
1243 if ((ecdh
= EC_KEY_new()) == NULL
) {
1244 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1245 ERR_R_MALLOC_FAILURE
);
1250 * Extract elliptic curve parameters and the
1251 * server's ephemeral ECDH public key.
1252 * Keep accumulating lengths of various components in
1253 * param_len and make sure it never exceeds n.
1257 * XXX: For now we only support named (not generic) curves
1258 * and the ECParameters in this case is just three bytes.
1261 if (param_len
> n
) {
1262 al
= SSL_AD_DECODE_ERROR
;
1263 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1264 SSL_R_LENGTH_TOO_SHORT
);
1269 * Check curve is one of our preferences, if not server has
1270 * sent an invalid curve.
1272 if (tls1_check_curve(s
, p
, param_len
) != 1) {
1273 al
= SSL_AD_DECODE_ERROR
;
1274 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_CURVE
);
1278 if ((curve_nid
= tls1_ec_curve_id2nid(*(p
+ 2))) == 0) {
1279 al
= SSL_AD_INTERNAL_ERROR
;
1280 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1281 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1285 ngroup
= EC_GROUP_new_by_curve_name(curve_nid
);
1286 if (ngroup
== NULL
) {
1287 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1291 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1292 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1296 EC_GROUP_free(ngroup
);
1298 group
= EC_KEY_get0_group(ecdh
);
1302 /* Next, get the encoded ECPoint */
1303 if (((srvr_ecpoint
= EC_POINT_new(group
)) == NULL
) ||
1304 ((bn_ctx
= BN_CTX_new()) == NULL
)) {
1305 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1306 ERR_R_MALLOC_FAILURE
);
1310 if (param_len
+ 1 > n
)
1312 encoded_pt_len
= *p
;
1313 /* length of encoded point */
1315 param_len
+= (1 + encoded_pt_len
);
1316 if ((param_len
> n
) || (EC_POINT_oct2point(group
, srvr_ecpoint
,
1317 p
, encoded_pt_len
, bn_ctx
) == 0)) {
1318 al
= SSL_AD_DECODE_ERROR
;
1319 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1325 p
+= encoded_pt_len
;
1328 * The ECC/TLS specification does not mention the use
1329 * of DSA to sign ECParameters in the server key
1330 * exchange message. We do support RSA and ECDSA.
1332 if (alg_a
& SSL_aRSA
)
1333 pkey
= X509_get_pubkey(
1334 s
->session
->sess_cert
->peer_pkeys
[
1335 SSL_PKEY_RSA_ENC
].x509
);
1336 else if (alg_a
& SSL_aECDSA
)
1337 pkey
= X509_get_pubkey(
1338 s
->session
->sess_cert
->peer_pkeys
[
1339 SSL_PKEY_ECC
].x509
);
1340 /* Else anonymous ECDH, so no certificate or pkey. */
1341 EC_KEY_set_public_key(ecdh
, srvr_ecpoint
);
1342 s
->session
->sess_cert
->peer_ecdh_tmp
= ecdh
;
1344 BN_CTX_free(bn_ctx
);
1346 EC_POINT_free(srvr_ecpoint
);
1347 srvr_ecpoint
= NULL
;
1349 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1350 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1351 SSL_R_UNEXPECTED_MESSAGE
);
1355 /* p points to the next byte, there are 'n' bytes left */
1357 /* if it was signed, check the signature */
1359 if (SSL_USE_SIGALGS(s
)) {
1360 int sigalg
= tls12_get_sigid(pkey
);
1361 /* Should never happen */
1363 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1364 ERR_R_INTERNAL_ERROR
);
1368 * Check key type is consistent
1373 if (sigalg
!= (int)p
[1]) {
1374 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1375 SSL_R_WRONG_SIGNATURE_TYPE
);
1376 al
= SSL_AD_DECODE_ERROR
;
1379 md
= tls12_get_hash(p
[0]);
1381 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1382 SSL_R_UNKNOWN_DIGEST
);
1383 al
= SSL_AD_DECODE_ERROR
;
1395 j
= EVP_PKEY_size(pkey
);
1397 if (i
!= n
|| n
> j
) {
1398 /* wrong packet length */
1399 al
= SSL_AD_DECODE_ERROR
;
1400 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1401 SSL_R_WRONG_SIGNATURE_LENGTH
);
1405 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1410 for (num
= 2; num
> 0; num
--) {
1411 if (!EVP_DigestInit_ex(&md_ctx
,
1412 (num
== 2) ? s
->ctx
->md5
: s
->ctx
->sha1
,
1414 al
= SSL_AD_INTERNAL_ERROR
;
1417 EVP_DigestUpdate(&md_ctx
,
1418 s
->s3
->client_random
,
1420 EVP_DigestUpdate(&md_ctx
,
1421 s
->s3
->server_random
,
1423 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1424 EVP_DigestFinal_ex(&md_ctx
, q
,
1425 (unsigned int *)&i
);
1429 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
,
1430 p
, n
, pkey
->pkey
.rsa
);
1432 al
= SSL_AD_DECRYPT_ERROR
;
1433 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1434 SSL_R_BAD_RSA_DECRYPT
);
1439 al
= SSL_AD_DECRYPT_ERROR
;
1440 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1441 SSL_R_BAD_SIGNATURE
);
1445 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
1446 EVP_VerifyUpdate(&md_ctx
, s
->s3
->client_random
,
1448 EVP_VerifyUpdate(&md_ctx
, s
->s3
->server_random
,
1450 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1451 if (EVP_VerifyFinal(&md_ctx
, p
,(int)n
, pkey
) <= 0) {
1453 al
= SSL_AD_DECRYPT_ERROR
;
1454 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1455 SSL_R_BAD_SIGNATURE
);
1460 /* aNULL does not need public keys. */
1461 if (!(alg_a
& SSL_aNULL
)) {
1462 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1463 ERR_R_INTERNAL_ERROR
);
1466 /* still data left over */
1468 al
= SSL_AD_DECODE_ERROR
;
1469 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1470 SSL_R_EXTRA_DATA_IN_MESSAGE
);
1474 EVP_PKEY_free(pkey
);
1475 EVP_MD_CTX_cleanup(&md_ctx
);
1478 /* wrong packet length */
1479 al
= SSL_AD_DECODE_ERROR
;
1480 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_PACKET_LENGTH
);
1482 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1484 EVP_PKEY_free(pkey
);
1487 BN_CTX_free(bn_ctx
);
1488 EC_POINT_free(srvr_ecpoint
);
1490 EVP_MD_CTX_cleanup(&md_ctx
);
1495 ssl3_get_certificate_request(SSL
*s
)
1500 CBS cert_request
, ctypes
, rdn_list
;
1501 X509_NAME
*xn
= NULL
;
1502 const unsigned char *q
;
1503 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1505 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_CERT_REQ_A
,
1506 SSL3_ST_CR_CERT_REQ_B
, -1, s
->max_cert_list
, &ok
);
1511 s
->s3
->tmp
.cert_req
= 0;
1513 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1514 s
->s3
->tmp
.reuse_message
= 1;
1516 * If we get here we don't need any cached handshake records
1517 * as we wont be doing client auth.
1519 if (s
->s3
->handshake_buffer
) {
1520 if (!tls1_digest_cached_records(s
))
1526 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1527 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1528 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1529 SSL_R_WRONG_MESSAGE_TYPE
);
1533 /* TLS does not like anon-DH with client cert */
1534 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
1535 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1536 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1537 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1543 CBS_init(&cert_request
, s
->init_msg
, n
);
1545 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1546 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1547 ERR_R_MALLOC_FAILURE
);
1551 /* get the certificate types */
1552 if (!CBS_get_u8(&cert_request
, &ctype_num
))
1555 if (ctype_num
> SSL3_CT_NUMBER
)
1556 ctype_num
= SSL3_CT_NUMBER
;
1557 if (!CBS_get_bytes(&cert_request
, &ctypes
, ctype_num
) ||
1558 !CBS_write_bytes(&ctypes
, s
->s3
->tmp
.ctype
,
1559 sizeof(s
->s3
->tmp
.ctype
), NULL
)) {
1560 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1561 SSL_R_DATA_LENGTH_TOO_LONG
);
1565 if (SSL_USE_SIGALGS(s
)) {
1568 if (CBS_len(&cert_request
) < 2) {
1569 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1570 SSL_R_DATA_LENGTH_TOO_LONG
);
1574 /* Check we have enough room for signature algorithms and
1575 * following length value.
1577 if (!CBS_get_u16_length_prefixed(&cert_request
, &sigalgs
)) {
1578 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1579 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1580 SSL_R_DATA_LENGTH_TOO_LONG
);
1583 if ((CBS_len(&sigalgs
) & 1) ||
1584 !tls1_process_sigalgs(s
, CBS_data(&sigalgs
),
1585 CBS_len(&sigalgs
))) {
1586 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1587 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1588 SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
1593 /* get the CA RDNs */
1594 if (CBS_len(&cert_request
) < 2) {
1595 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1596 SSL_R_DATA_LENGTH_TOO_LONG
);
1600 if (!CBS_get_u16_length_prefixed(&cert_request
, &rdn_list
) ||
1601 CBS_len(&cert_request
) != 0) {
1602 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1603 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1604 SSL_R_LENGTH_MISMATCH
);
1608 while (CBS_len(&rdn_list
) > 0) {
1611 if (CBS_len(&rdn_list
) < 2) {
1612 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1613 SSL_R_DATA_LENGTH_TOO_LONG
);
1617 if (!CBS_get_u16_length_prefixed(&rdn_list
, &rdn
)) {
1618 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1619 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1620 SSL_R_CA_DN_TOO_LONG
);
1625 if ((xn
= d2i_X509_NAME(NULL
, &q
, CBS_len(&rdn
))) == NULL
) {
1626 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1627 SSL_AD_DECODE_ERROR
);
1628 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1633 if (q
!= CBS_data(&rdn
) + CBS_len(&rdn
)) {
1634 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1635 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1636 SSL_R_CA_DN_LENGTH_MISMATCH
);
1639 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1640 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1641 ERR_R_MALLOC_FAILURE
);
1644 xn
= NULL
; /* avoid free in err block */
1647 /* we should setup a certificate to return.... */
1648 s
->s3
->tmp
.cert_req
= 1;
1649 s
->s3
->tmp
.ctype_num
= ctype_num
;
1650 if (s
->s3
->tmp
.ca_names
!= NULL
)
1651 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
1652 s
->s3
->tmp
.ca_names
= ca_sk
;
1658 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1659 SSL_R_BAD_PACKET_LENGTH
);
1664 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1669 ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1671 return (X509_NAME_cmp(*a
, *b
));
1675 ssl3_get_new_session_ticket(SSL
*s
)
1677 int ok
, al
, ret
= 0;
1678 uint32_t lifetime_hint
;
1680 CBS cbs
, session_ticket
;
1682 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_SESSION_TICKET_A
,
1683 SSL3_ST_CR_SESSION_TICKET_B
, -1, 16384, &ok
);
1687 if (s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
) {
1688 s
->s3
->tmp
.reuse_message
= 1;
1691 if (s
->s3
->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
) {
1692 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1693 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,
1694 SSL_R_BAD_MESSAGE_TYPE
);
1699 al
= SSL_AD_DECODE_ERROR
;
1700 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,
1701 SSL_R_LENGTH_MISMATCH
);
1705 CBS_init(&cbs
, s
->init_msg
, n
);
1706 if (!CBS_get_u32(&cbs
, &lifetime_hint
) ||
1707 #if UINT32_MAX > LONG_MAX
1708 lifetime_hint
> LONG_MAX
||
1710 !CBS_get_u16_length_prefixed(&cbs
, &session_ticket
) ||
1711 CBS_len(&cbs
) != 0) {
1712 al
= SSL_AD_DECODE_ERROR
;
1713 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,
1714 SSL_R_LENGTH_MISMATCH
);
1717 s
->session
->tlsext_tick_lifetime_hint
= (long)lifetime_hint
;
1719 if (!CBS_stow(&session_ticket
, &s
->session
->tlsext_tick
,
1720 &s
->session
->tlsext_ticklen
)) {
1721 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,
1722 ERR_R_MALLOC_FAILURE
);
1727 * There are two ways to detect a resumed ticket sesion.
1728 * One is to set an appropriate session ID and then the server
1729 * must return a match in ServerHello. This allows the normal
1730 * client session ID matching to work and we know much
1731 * earlier that the ticket has been accepted.
1733 * The other way is to set zero length session ID when the
1734 * ticket is presented and rely on the handshake to determine
1735 * session resumption.
1737 * We choose the former approach because this fits in with
1738 * assumptions elsewhere in OpenSSL. The session ID is set
1739 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1742 EVP_Digest(CBS_data(&session_ticket
), CBS_len(&session_ticket
),
1743 s
->session
->session_id
, &s
->session
->session_id_length
,
1744 EVP_sha256(), NULL
);
1748 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1754 ssl3_get_cert_status(SSL
*s
)
1756 CBS cert_status
, response
;
1760 uint8_t status_type
;
1762 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_CERT_STATUS_A
,
1763 SSL3_ST_CR_CERT_STATUS_B
, SSL3_MT_CERTIFICATE_STATUS
,
1770 /* need at least status type + length */
1771 al
= SSL_AD_DECODE_ERROR
;
1772 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1773 SSL_R_LENGTH_MISMATCH
);
1777 CBS_init(&cert_status
, s
->init_msg
, n
);
1778 if (!CBS_get_u8(&cert_status
, &status_type
) ||
1779 CBS_len(&cert_status
) < 3) {
1780 /* need at least status type + length */
1781 al
= SSL_AD_DECODE_ERROR
;
1782 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1783 SSL_R_LENGTH_MISMATCH
);
1787 if (status_type
!= TLSEXT_STATUSTYPE_ocsp
) {
1788 al
= SSL_AD_DECODE_ERROR
;
1789 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1790 SSL_R_UNSUPPORTED_STATUS_TYPE
);
1794 if (!CBS_get_u24_length_prefixed(&cert_status
, &response
) ||
1795 CBS_len(&cert_status
) != 0) {
1796 al
= SSL_AD_DECODE_ERROR
;
1797 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1798 SSL_R_LENGTH_MISMATCH
);
1802 if (!CBS_stow(&response
, &s
->tlsext_ocsp_resp
,
1803 &stow_len
) || stow_len
> INT_MAX
) {
1804 s
->tlsext_ocsp_resplen
= 0;
1805 al
= SSL_AD_INTERNAL_ERROR
;
1806 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1807 ERR_R_MALLOC_FAILURE
);
1810 s
->tlsext_ocsp_resplen
= (int)stow_len
;
1812 if (s
->ctx
->tlsext_status_cb
) {
1814 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
1816 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
1817 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1818 SSL_R_INVALID_STATUS_RESPONSE
);
1822 al
= SSL_AD_INTERNAL_ERROR
;
1823 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,
1824 ERR_R_MALLOC_FAILURE
);
1830 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1835 ssl3_get_server_done(SSL
*s
)
1840 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_SRVR_DONE_A
,
1841 SSL3_ST_CR_SRVR_DONE_B
, SSL3_MT_SERVER_DONE
,
1842 30, /* should be very small, like 0 :-) */ &ok
);
1847 /* should contain no data */
1848 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1849 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
, SSL_R_LENGTH_MISMATCH
);
1857 ssl3_send_client_kex_rsa(SSL
*s
, SESS_CERT
*sess_cert
, unsigned char *p
,
1860 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
1861 EVP_PKEY
*pkey
= NULL
;
1866 pkey
= X509_get_pubkey(sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1867 if (pkey
== NULL
|| pkey
->type
!= EVP_PKEY_RSA
||
1868 pkey
->pkey
.rsa
== NULL
) {
1869 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1870 ERR_R_INTERNAL_ERROR
);
1874 tmp_buf
[0] = s
->client_version
>> 8;
1875 tmp_buf
[1] = s
->client_version
& 0xff;
1876 arc4random_buf(&tmp_buf
[2], sizeof(tmp_buf
) - 2);
1878 s
->session
->master_key_length
= sizeof(tmp_buf
);
1883 n
= RSA_public_encrypt(sizeof(tmp_buf
), tmp_buf
, p
, pkey
->pkey
.rsa
,
1886 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1887 SSL_R_BAD_RSA_ENCRYPT
);
1894 s
->session
->master_key_length
=
1895 s
->method
->ssl3_enc
->generate_master_secret(s
,
1896 s
->session
->master_key
, tmp_buf
, sizeof(tmp_buf
));
1902 explicit_bzero(tmp_buf
, sizeof(tmp_buf
));
1903 EVP_PKEY_free(pkey
);
1909 ssl3_send_client_kex_dhe(SSL
*s
, SESS_CERT
*sess_cert
, unsigned char *p
,
1912 DH
*dh_srvr
= NULL
, *dh_clnt
= NULL
;
1913 unsigned char *key
= NULL
;
1917 /* Ensure that we have an ephemeral key for DHE. */
1918 if (sess_cert
->peer_dh_tmp
== NULL
) {
1919 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
1920 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1921 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
1924 dh_srvr
= sess_cert
->peer_dh_tmp
;
1926 /* Generate a new random key. */
1927 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
1928 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1931 if (!DH_generate_key(dh_clnt
)) {
1932 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1935 key_size
= DH_size(dh_clnt
);
1936 if ((key
= malloc(key_size
)) == NULL
) {
1937 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1938 ERR_R_MALLOC_FAILURE
);
1941 n
= DH_compute_key(key
, dh_srvr
->pub_key
, dh_clnt
);
1943 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1947 /* Generate master key from the result. */
1948 s
->session
->master_key_length
=
1949 s
->method
->ssl3_enc
->generate_master_secret(s
,
1950 s
->session
->master_key
, key
, n
);
1952 /* Send off the data. */
1953 n
= BN_num_bytes(dh_clnt
->pub_key
);
1955 BN_bn2bin(dh_clnt
->pub_key
, p
);
1964 explicit_bzero(key
, key_size
);
1971 ssl3_send_client_kex_ecdh(SSL
*s
, SESS_CERT
*sess_cert
, unsigned char *p
,
1974 EC_KEY
*tkey
, *clnt_ecdh
= NULL
;
1975 const EC_GROUP
*srvr_group
= NULL
;
1976 const EC_POINT
*srvr_ecpoint
= NULL
;
1977 EVP_PKEY
*srvr_pub_pkey
= NULL
;
1978 BN_CTX
*bn_ctx
= NULL
;
1979 unsigned char *encodedPoint
= NULL
;
1980 unsigned char *key
= NULL
;
1981 unsigned long alg_k
;
1982 int encoded_pt_len
= 0;
1986 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1988 /* Ensure that we have an ephemeral key for ECDHE. */
1989 if ((alg_k
& SSL_kECDHE
) && sess_cert
->peer_ecdh_tmp
== NULL
) {
1990 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
1991 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1992 ERR_R_INTERNAL_ERROR
);
1995 tkey
= sess_cert
->peer_ecdh_tmp
;
1997 if (alg_k
& (SSL_kECDHr
|SSL_kECDHe
)) {
1998 /* Get the Server Public Key from certificate. */
1999 srvr_pub_pkey
= X509_get_pubkey(
2000 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
2001 if (srvr_pub_pkey
!= NULL
&& srvr_pub_pkey
->type
== EVP_PKEY_EC
)
2002 tkey
= srvr_pub_pkey
->pkey
.ec
;
2006 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2007 ERR_R_INTERNAL_ERROR
);
2011 srvr_group
= EC_KEY_get0_group(tkey
);
2012 srvr_ecpoint
= EC_KEY_get0_public_key(tkey
);
2014 if (srvr_group
== NULL
|| srvr_ecpoint
== NULL
) {
2015 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2016 ERR_R_INTERNAL_ERROR
);
2020 if ((clnt_ecdh
= EC_KEY_new()) == NULL
) {
2021 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2022 ERR_R_MALLOC_FAILURE
);
2026 if (!EC_KEY_set_group(clnt_ecdh
, srvr_group
)) {
2027 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2031 /* Generate a new ECDH key pair. */
2032 if (!(EC_KEY_generate_key(clnt_ecdh
))) {
2033 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2036 key_size
= ECDH_size(clnt_ecdh
);
2037 if (key_size
<= 0) {
2038 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2041 if ((key
= malloc(key_size
)) == NULL
) {
2042 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2043 ERR_R_MALLOC_FAILURE
);
2045 n
= ECDH_compute_key(key
, key_size
, srvr_ecpoint
, clnt_ecdh
, NULL
);
2047 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2051 /* Generate master key from the result. */
2052 s
->session
->master_key_length
=
2053 s
->method
->ssl3_enc
->generate_master_secret(s
,
2054 s
->session
->master_key
, key
, n
);
2057 * First check the size of encoding and allocate memory accordingly.
2059 encoded_pt_len
= EC_POINT_point2oct(srvr_group
,
2060 EC_KEY_get0_public_key(clnt_ecdh
),
2061 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
2063 bn_ctx
= BN_CTX_new();
2064 encodedPoint
= malloc(encoded_pt_len
);
2065 if (encodedPoint
== NULL
|| bn_ctx
== NULL
) {
2066 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2067 ERR_R_MALLOC_FAILURE
);
2071 /* Encode the public key */
2072 n
= EC_POINT_point2oct(srvr_group
, EC_KEY_get0_public_key(clnt_ecdh
),
2073 POINT_CONVERSION_UNCOMPRESSED
, encodedPoint
, encoded_pt_len
,
2076 *p
= n
; /* length of encoded point */
2077 /* Encoded point will be copied here */
2080 /* copy the point */
2081 memcpy((unsigned char *)p
, encodedPoint
, n
);
2082 /* increment n to account for length field */
2090 explicit_bzero(key
, key_size
);
2093 BN_CTX_free(bn_ctx
);
2095 EC_KEY_free(clnt_ecdh
);
2096 EVP_PKEY_free(srvr_pub_pkey
);
2102 ssl3_send_client_kex_gost(SSL
*s
, SESS_CERT
*sess_cert
, unsigned char *p
,
2105 unsigned char premaster_secret
[32], shared_ukm
[32], tmp
[256];
2106 EVP_PKEY
*pub_key
= NULL
;
2107 EVP_PKEY_CTX
*pkey_ctx
;
2110 unsigned int md_len
;
2111 EVP_MD_CTX
*ukm_hash
;
2116 /* Get server sertificate PKEY and create ctx from it */
2117 peer_cert
= sess_cert
->peer_pkeys
[SSL_PKEY_GOST01
].x509
;
2118 if (peer_cert
== NULL
) {
2119 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2120 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2124 pub_key
= X509_get_pubkey(peer_cert
);
2125 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
, NULL
);
2128 * If we have send a certificate, and certificate key parameters match
2129 * those of server certificate, use certificate key for key exchange.
2130 * Otherwise, generate ephemeral key pair.
2132 EVP_PKEY_encrypt_init(pkey_ctx
);
2134 /* Generate session key. */
2135 arc4random_buf(premaster_secret
, 32);
2138 * If we have client certificate, use its secret as peer key.
2140 if (s
->s3
->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2141 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2142 s
->cert
->key
->privatekey
) <=0) {
2144 * If there was an error - just ignore it.
2145 * Ephemeral key would be used.
2152 * Compute shared IV and store it in algorithm-specific context data.
2154 ukm_hash
= EVP_MD_CTX_create();
2155 if (ukm_hash
== NULL
) {
2156 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2157 ERR_R_MALLOC_FAILURE
);
2161 if (ssl_get_algorithm2(s
) & SSL_HANDSHAKE_MAC_GOST94
)
2162 nid
= NID_id_GostR3411_94
;
2164 nid
= NID_id_tc26_gost3411_2012_256
;
2165 if (!EVP_DigestInit(ukm_hash
, EVP_get_digestbynid(nid
)))
2167 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2168 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2169 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2170 EVP_MD_CTX_destroy(ukm_hash
);
2171 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
2172 EVP_PKEY_CTRL_SET_IV
, 8, shared_ukm
) < 0) {
2173 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, SSL_R_LIBRARY_BUG
);
2178 * Make GOST keytransport blob message, encapsulate it into sequence.
2180 *(p
++) = V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
;
2182 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, premaster_secret
,
2184 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, SSL_R_LIBRARY_BUG
);
2187 if (msglen
>= 0x80) {
2189 *(p
++) = msglen
& 0xff;
2192 *(p
++) = msglen
& 0xff;
2195 memcpy(p
, tmp
, msglen
);
2197 /* Check if pubkey from client certificate was used. */
2198 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
2200 /* Set flag "skip certificate verify". */
2201 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2203 EVP_PKEY_CTX_free(pkey_ctx
);
2204 s
->session
->master_key_length
=
2205 s
->method
->ssl3_enc
->generate_master_secret(s
,
2206 s
->session
->master_key
, premaster_secret
, 32);
2212 explicit_bzero(premaster_secret
, sizeof(premaster_secret
));
2213 EVP_PKEY_free(pub_key
);
2219 ssl3_send_client_key_exchange(SSL
*s
)
2221 SESS_CERT
*sess_cert
;
2222 unsigned long alg_k
;
2226 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
) {
2227 p
= ssl3_handshake_msg_start(s
, SSL3_MT_CLIENT_KEY_EXCHANGE
);
2229 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2231 if ((sess_cert
= s
->session
->sess_cert
) == NULL
) {
2232 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2233 SSL_AD_UNEXPECTED_MESSAGE
);
2234 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2235 ERR_R_INTERNAL_ERROR
);
2239 if (alg_k
& SSL_kRSA
) {
2240 if (ssl3_send_client_kex_rsa(s
, sess_cert
, p
, &n
) != 1)
2242 } else if (alg_k
& SSL_kDHE
) {
2243 if (ssl3_send_client_kex_dhe(s
, sess_cert
, p
, &n
) != 1)
2245 } else if (alg_k
& (SSL_kECDHE
|SSL_kECDHr
|SSL_kECDHe
)) {
2246 if (ssl3_send_client_kex_ecdh(s
, sess_cert
, p
, &n
) != 1)
2248 } else if (alg_k
& SSL_kGOST
) {
2249 if (ssl3_send_client_kex_gost(s
, sess_cert
, p
, &n
) != 1)
2252 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2253 SSL_AD_HANDSHAKE_FAILURE
);
2254 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2255 ERR_R_INTERNAL_ERROR
);
2259 ssl3_handshake_msg_finish(s
, n
);
2261 s
->state
= SSL3_ST_CW_KEY_EXCH_B
;
2264 /* SSL3_ST_CW_KEY_EXCH_B */
2265 return (ssl3_handshake_write(s
));
2272 ssl3_send_client_verify(SSL
*s
)
2275 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
2277 EVP_PKEY_CTX
*pctx
= NULL
;
2283 EVP_MD_CTX_init(&mctx
);
2285 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
) {
2286 p
= ssl3_handshake_msg_start(s
, SSL3_MT_CERTIFICATE_VERIFY
);
2289 * Create context from key and test if sha1 is allowed as
2292 pkey
= s
->cert
->key
->privatekey
;
2293 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2294 EVP_PKEY_sign_init(pctx
);
2295 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) > 0) {
2296 if (!SSL_USE_SIGALGS(s
))
2297 s
->method
->ssl3_enc
->cert_verify_mac(s
,
2298 NID_sha1
, &(data
[MD5_DIGEST_LENGTH
]));
2303 * For TLS v1.2 send signature algorithm and signature
2304 * using agreed digest and cached handshake records.
2306 if (SSL_USE_SIGALGS(s
)) {
2309 const EVP_MD
*md
= s
->cert
->key
->digest
;
2310 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
,
2312 if (hdatalen
<= 0 ||
2313 !tls12_get_sigandhash(p
, pkey
, md
)) {
2314 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2315 ERR_R_INTERNAL_ERROR
);
2319 if (!EVP_SignInit_ex(&mctx
, md
, NULL
) ||
2320 !EVP_SignUpdate(&mctx
, hdata
, hdatalen
) ||
2321 !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
2322 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2328 if (!tls1_digest_cached_records(s
))
2330 } else if (pkey
->type
== EVP_PKEY_RSA
) {
2331 s
->method
->ssl3_enc
->cert_verify_mac(
2332 s
, NID_md5
, &(data
[0]));
2333 if (RSA_sign(NID_md5_sha1
, data
,
2334 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, &(p
[2]),
2335 &u
, pkey
->pkey
.rsa
) <= 0 ) {
2336 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2342 } else if (pkey
->type
== EVP_PKEY_DSA
) {
2343 if (!DSA_sign(pkey
->save_type
,
2344 &(data
[MD5_DIGEST_LENGTH
]),
2345 SHA_DIGEST_LENGTH
, &(p
[2]),
2346 (unsigned int *)&j
, pkey
->pkey
.dsa
)) {
2347 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2353 } else if (pkey
->type
== EVP_PKEY_EC
) {
2354 if (!ECDSA_sign(pkey
->save_type
,
2355 &(data
[MD5_DIGEST_LENGTH
]),
2356 SHA_DIGEST_LENGTH
, &(p
[2]),
2357 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
2358 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2364 #ifndef OPENSSL_NO_GOST
2365 } else if (pkey
->type
== NID_id_GostR3410_94
||
2366 pkey
->type
== NID_id_GostR3410_2001
) {
2367 unsigned char signbuf
[128];
2374 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
2375 if (hdatalen
<= 0) {
2376 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2377 ERR_R_INTERNAL_ERROR
);
2380 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2381 !(md
= EVP_get_digestbynid(nid
))) {
2382 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2386 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2387 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2388 !EVP_DigestFinal(&mctx
, signbuf
, &u
) ||
2389 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2390 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
2391 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2392 GOST_SIG_FORMAT_RS_LE
,
2394 (EVP_PKEY_sign(pctx
, &(p
[2]), &sigsize
,
2395 signbuf
, u
) <= 0)) {
2396 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2400 if (!tls1_digest_cached_records(s
))
2407 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
2408 ERR_R_INTERNAL_ERROR
);
2412 s
->state
= SSL3_ST_CW_CERT_VRFY_B
;
2414 ssl3_handshake_msg_finish(s
, n
);
2417 EVP_MD_CTX_cleanup(&mctx
);
2418 EVP_PKEY_CTX_free(pctx
);
2420 return (ssl3_handshake_write(s
));
2423 EVP_MD_CTX_cleanup(&mctx
);
2424 EVP_PKEY_CTX_free(pctx
);
2429 ssl3_send_client_certificate(SSL
*s
)
2432 EVP_PKEY
*pkey
= NULL
;
2436 if (s
->state
== SSL3_ST_CW_CERT_A
) {
2437 if ((s
->cert
== NULL
) || (s
->cert
->key
->x509
== NULL
) ||
2438 (s
->cert
->key
->privatekey
== NULL
))
2439 s
->state
= SSL3_ST_CW_CERT_B
;
2441 s
->state
= SSL3_ST_CW_CERT_C
;
2444 /* We need to get a client cert */
2445 if (s
->state
== SSL3_ST_CW_CERT_B
) {
2447 * If we get an error, we need to
2448 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2449 * We then get retied later
2451 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2453 s
->rwstate
= SSL_X509_LOOKUP
;
2456 s
->rwstate
= SSL_NOTHING
;
2457 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2458 s
->state
= SSL3_ST_CW_CERT_B
;
2459 if (!SSL_use_certificate(s
, x509
) ||
2460 !SSL_use_PrivateKey(s
, pkey
))
2462 } else if (i
== 1) {
2464 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,
2465 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2469 EVP_PKEY_free(pkey
);
2471 s
->s3
->tmp
.cert_req
= 2;
2473 /* Ok, we have a cert */
2474 s
->state
= SSL3_ST_CW_CERT_C
;
2477 if (s
->state
== SSL3_ST_CW_CERT_C
) {
2478 s
->state
= SSL3_ST_CW_CERT_D
;
2479 l
= ssl3_output_cert_chain(s
,
2480 (s
->s3
->tmp
.cert_req
== 2) ? NULL
: s
->cert
->key
->x509
);
2481 s
->init_num
= (int)l
;
2484 /* SSL3_ST_CW_CERT_D */
2485 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
2488 #define has_bits(i,m) (((i)&(m)) == (m))
2491 ssl3_check_cert_and_algorithm(SSL
*s
)
2495 EVP_PKEY
*pkey
= NULL
;
2499 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2500 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2502 /* We don't have a certificate. */
2503 if (alg_a
& SSL_aNULL
)
2506 sc
= s
->session
->sess_cert
;
2508 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2509 ERR_R_INTERNAL_ERROR
);
2512 dh
= s
->session
->sess_cert
->peer_dh_tmp
;
2514 /* This is the passed certificate. */
2516 idx
= sc
->peer_cert_type
;
2517 if (idx
== SSL_PKEY_ECC
) {
2518 if (ssl_check_srvr_ecc_cert_and_alg(
2519 sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
2521 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2522 SSL_R_BAD_ECC_CERT
);
2528 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
2529 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
2530 EVP_PKEY_free(pkey
);
2532 /* Check that we have a certificate if we require one. */
2533 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_SIGN
)) {
2534 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2535 SSL_R_MISSING_RSA_SIGNING_CERT
);
2537 } else if ((alg_a
& SSL_aDSS
) &&
2538 !has_bits(i
, EVP_PK_DSA
|EVP_PKT_SIGN
)) {
2539 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2540 SSL_R_MISSING_DSA_SIGNING_CERT
);
2543 if ((alg_k
& SSL_kRSA
) &&
2544 !has_bits(i
, EVP_PK_RSA
|EVP_PKT_ENC
)) {
2545 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2546 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2549 if ((alg_k
& SSL_kDHE
) &&
2550 !(has_bits(i
, EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
))) {
2551 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2552 SSL_R_MISSING_DH_KEY
);
2558 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2564 ssl3_send_next_proto(SSL
*s
)
2566 unsigned int len
, padding_len
;
2567 unsigned char *d
, *p
;
2569 if (s
->state
== SSL3_ST_CW_NEXT_PROTO_A
) {
2570 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_NEXT_PROTO
);
2572 len
= s
->next_proto_negotiated_len
;
2573 padding_len
= 32 - ((len
+ 2) % 32);
2575 memcpy(p
, s
->next_proto_negotiated
, len
);
2577 *(p
++) = padding_len
;
2578 memset(p
, 0, padding_len
);
2581 ssl3_handshake_msg_finish(s
, p
- d
);
2583 s
->state
= SSL3_ST_CW_NEXT_PROTO_B
;
2586 return (ssl3_handshake_write(s
));
2590 * Check to see if handshake is full or resumed. Usually this is just a
2591 * case of checking to see if a cache hit has occurred. In the case of
2592 * session tickets we have to check the next message to be sure.
2596 ssl3_check_finished(SSL
*s
)
2601 /* If we have no ticket it cannot be a resumed session. */
2602 if (!s
->session
->tlsext_tick
)
2604 /* this function is called when we really expect a Certificate
2605 * message, so permit appropriate message length */
2606 n
= s
->method
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
2607 SSL3_ST_CR_CERT_B
, -1, s
->max_cert_list
, &ok
);
2610 s
->s3
->tmp
.reuse_message
= 1;
2611 if ((s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
) ||
2612 (s
->s3
->tmp
.message_type
== SSL3_MT_NEWSESSION_TICKET
))
2619 ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
2623 #ifndef OPENSSL_NO_ENGINE
2624 if (s
->ctx
->client_cert_engine
) {
2625 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
2626 SSL_get_client_CA_list(s
),
2627 px509
, ppkey
, NULL
, NULL
, NULL
);
2632 if (s
->ctx
->client_cert_cb
)
2633 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);