1 /* $OpenBSD: ssl_clnt.c,v 1.17 2017/08/12 21:47:59 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
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/curve25519.h>
160 #include <openssl/dh.h>
161 #include <openssl/evp.h>
162 #include <openssl/md5.h>
163 #include <openssl/objects.h>
165 #ifndef OPENSSL_NO_ENGINE
166 #include <openssl/engine.h>
168 #ifndef OPENSSL_NO_GOST
169 #include <openssl/gost.h>
172 #include "bytestring.h"
174 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
);
179 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
181 int new_state
, state
, skip
= 0;
186 if (s
->internal
->info_callback
!= NULL
)
187 cb
= s
->internal
->info_callback
;
188 else if (s
->ctx
->internal
->info_callback
!= NULL
)
189 cb
= s
->ctx
->internal
->info_callback
;
191 s
->internal
->in_handshake
++;
192 if (!SSL_in_init(s
) || SSL_in_before(s
))
196 state
= S3I(s
)->hs
.state
;
198 switch (S3I(s
)->hs
.state
) {
199 case SSL_ST_RENEGOTIATE
:
200 s
->internal
->renegotiate
= 1;
201 S3I(s
)->hs
.state
= SSL_ST_CONNECT
;
202 s
->ctx
->internal
->stats
.sess_connect_renegotiate
++;
206 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
207 case SSL_ST_OK
|SSL_ST_CONNECT
:
211 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
213 if ((s
->version
& 0xff00 ) != 0x0300) {
214 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
219 /* s->version=SSL3_VERSION; */
220 s
->internal
->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 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_A
;
243 s
->ctx
->internal
->stats
.sess_connect
++;
244 s
->internal
->init_num
= 0;
247 case SSL3_ST_CW_CLNT_HELLO_A
:
248 case SSL3_ST_CW_CLNT_HELLO_B
:
250 s
->internal
->shutdown
= 0;
251 ret
= ssl3_client_hello(s
);
254 S3I(s
)->hs
.state
= SSL3_ST_CR_SRVR_HELLO_A
;
255 s
->internal
->init_num
= 0;
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
);
269 if (s
->internal
->hit
) {
270 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
271 if (s
->internal
->tlsext_ticket_expected
) {
272 /* receive renewed session ticket */
273 S3I(s
)->hs
.state
= SSL3_ST_CR_SESSION_TICKET_A
;
276 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_A
;
277 s
->internal
->init_num
= 0;
280 case SSL3_ST_CR_CERT_A
:
281 case SSL3_ST_CR_CERT_B
:
282 ret
= ssl3_check_finished(s
);
286 s
->internal
->hit
= 1;
287 if (s
->internal
->tlsext_ticket_expected
)
288 S3I(s
)->hs
.state
= SSL3_ST_CR_SESSION_TICKET_A
;
290 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
291 s
->internal
->init_num
= 0;
294 /* Check if it is anon DH/ECDH. */
295 if (!(S3I(s
)->hs
.new_cipher
->algorithm_auth
&
297 ret
= ssl3_get_server_certificate(s
);
300 if (s
->internal
->tlsext_status_expected
)
301 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_STATUS_A
;
303 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
306 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
308 s
->internal
->init_num
= 0;
311 case SSL3_ST_CR_KEY_EXCH_A
:
312 case SSL3_ST_CR_KEY_EXCH_B
:
313 ret
= ssl3_get_server_key_exchange(s
);
316 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_REQ_A
;
317 s
->internal
->init_num
= 0;
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 S3I(s
)->hs
.state
= SSL3_ST_CR_SRVR_DONE_A
;
335 s
->internal
->init_num
= 0;
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 (S3I(s
)->tmp
.cert_req
)
344 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_A
;
346 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_A
;
347 s
->internal
->init_num
= 0;
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 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_A
;
359 s
->internal
->init_num
= 0;
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 (S3I(s
)->tmp
.cert_req
== 1) {
384 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_VRFY_A
;
386 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
387 S3I(s
)->change_cipher_spec
= 0;
389 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
390 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
391 S3I(s
)->change_cipher_spec
= 0;
394 s
->internal
->init_num
= 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 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
403 s
->internal
->init_num
= 0;
404 S3I(s
)->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 S3I(s
)->hs
.state
= SSL3_ST_CW_FINISHED_A
;
415 s
->internal
->init_num
= 0;
417 s
->session
->cipher
= S3I(s
)->hs
.new_cipher
;
418 if (!tls1_setup_key_block(s
)) {
423 if (!tls1_change_cipher_state(s
,
424 SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
431 case SSL3_ST_CW_FINISHED_A
:
432 case SSL3_ST_CW_FINISHED_B
:
433 ret
= ssl3_send_finished(s
, SSL3_ST_CW_FINISHED_A
,
434 SSL3_ST_CW_FINISHED_B
,
435 TLS_MD_CLIENT_FINISH_CONST
,
436 TLS_MD_CLIENT_FINISH_CONST_SIZE
);
439 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
440 S3I(s
)->hs
.state
= SSL3_ST_CW_FLUSH
;
443 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
444 if (s
->internal
->hit
) {
445 S3I(s
)->hs
.next_state
= SSL_ST_OK
;
447 SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
448 S3I(s
)->hs
.state
= SSL_ST_OK
;
449 s
->s3
->flags
|=SSL3_FLAGS_POP_BUFFER
;
450 S3I(s
)->delay_buf_pop_ret
= 0;
453 /* Allow NewSessionTicket if ticket expected */
454 if (s
->internal
->tlsext_ticket_expected
)
455 S3I(s
)->hs
.next_state
=
456 SSL3_ST_CR_SESSION_TICKET_A
;
459 S3I(s
)->hs
.next_state
= SSL3_ST_CR_FINISHED_A
;
461 s
->internal
->init_num
= 0;
464 case SSL3_ST_CR_SESSION_TICKET_A
:
465 case SSL3_ST_CR_SESSION_TICKET_B
:
466 ret
= ssl3_get_new_session_ticket(s
);
469 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
470 s
->internal
->init_num
= 0;
473 case SSL3_ST_CR_CERT_STATUS_A
:
474 case SSL3_ST_CR_CERT_STATUS_B
:
475 ret
= ssl3_get_cert_status(s
);
478 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
479 s
->internal
->init_num
= 0;
482 case SSL3_ST_CR_FINISHED_A
:
483 case SSL3_ST_CR_FINISHED_B
:
484 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
485 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
486 SSL3_ST_CR_FINISHED_B
);
490 if (s
->internal
->hit
)
491 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
493 S3I(s
)->hs
.state
= SSL_ST_OK
;
494 s
->internal
->init_num
= 0;
497 case SSL3_ST_CW_FLUSH
:
498 s
->internal
->rwstate
= SSL_WRITING
;
499 if (BIO_flush(s
->wbio
) <= 0) {
503 s
->internal
->rwstate
= SSL_NOTHING
;
504 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
508 /* clean a few things up */
509 tls1_cleanup_key_block(s
);
511 BUF_MEM_free(s
->internal
->init_buf
);
512 s
->internal
->init_buf
= NULL
;
515 * If we are not 'joining' the last two packets,
516 * remove the buffering now
518 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
519 ssl_free_wbio_buffer(s
);
520 /* else do it later in ssl3_write */
522 s
->internal
->init_num
= 0;
523 s
->internal
->renegotiate
= 0;
524 s
->internal
->new_session
= 0;
526 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
527 if (s
->internal
->hit
)
528 s
->ctx
->internal
->stats
.sess_hit
++;
532 s
->internal
->handshake_func
= ssl3_connect
;
533 s
->ctx
->internal
->stats
.sess_connect_good
++;
536 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
542 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
548 /* did we do anything */
549 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
550 if (s
->internal
->debug
) {
551 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
555 if ((cb
!= NULL
) && (S3I(s
)->hs
.state
!= state
)) {
556 new_state
= S3I(s
)->hs
.state
;
557 S3I(s
)->hs
.state
= state
;
558 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
559 S3I(s
)->hs
.state
= new_state
;
566 s
->internal
->in_handshake
--;
568 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
574 ssl3_client_hello(SSL
*s
)
576 unsigned char *bufend
, *p
, *d
;
577 uint16_t max_version
;
581 bufend
= (unsigned char *)s
->internal
->init_buf
->data
+ SSL3_RT_MAX_PLAIN_LENGTH
;
583 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CLNT_HELLO_A
) {
584 SSL_SESSION
*sess
= s
->session
;
586 if (ssl_supported_version_range(s
, NULL
, &max_version
) != 1) {
587 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
590 s
->client_version
= s
->version
= max_version
;
592 if ((sess
== NULL
) ||
593 (sess
->ssl_version
!= s
->version
) ||
594 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
595 (sess
->internal
->not_resumable
)) {
596 if (!ssl_get_new_session(s
, 0))
599 /* else use the pre-loaded session */
602 * If a DTLS ClientHello message is being resent after a
603 * HelloVerifyRequest, we must retain the original client
606 if (!SSL_IS_DTLS(s
) || D1I(s
)->send_cookie
== 0)
607 arc4random_buf(s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
609 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_CLIENT_HELLO
);
612 * Version indicates the negotiated version: for example from
613 * an SSLv2/v3 compatible client hello). The client_version
614 * field is the maximum version we permit and it is also
615 * used in RSA encrypted premaster secrets. Some servers can
616 * choke if we initially report a higher version then
617 * renegotiate to a lower one in the premaster secret. This
618 * didn't happen with TLS 1.0 as most servers supported it
619 * but it can with TLS 1.1 or later if the server only supports
622 * Possible scenario with previous logic:
623 * 1. Client hello indicates TLS 1.2
624 * 2. Server hello says TLS 1.0
625 * 3. RSA encrypted premaster secret uses 1.2.
626 * 4. Handhaked proceeds using TLS 1.0.
627 * 5. Server sends hello request to renegotiate.
628 * 6. Client hello indicates TLS v1.0 as we now
629 * know that is maximum server supports.
630 * 7. Server chokes on RSA encrypted premaster secret
631 * containing version 1.0.
633 * For interoperability it should be OK to always use the
634 * maximum version we support in client hello and then rely
635 * on the checking of version to ensure the servers isn't
636 * being inconsistent: for example initially negotiating with
637 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
638 * client_version in client hello and not resetting it to
639 * the negotiated version.
642 *(p
++) = s
->client_version
>> 8;
643 *(p
++) = s
->client_version
& 0xff;
646 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
647 p
+= SSL3_RANDOM_SIZE
;
650 if (s
->internal
->new_session
)
653 i
= s
->session
->session_id_length
;
656 if (i
> (int)sizeof(s
->session
->session_id
)) {
657 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
660 memcpy(p
, s
->session
->session_id
, i
);
665 if (SSL_IS_DTLS(s
)) {
666 if (D1I(s
)->cookie_len
> sizeof(D1I(s
)->cookie
)) {
667 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
670 *(p
++) = D1I(s
)->cookie_len
;
671 memcpy(p
, D1I(s
)->cookie
, D1I(s
)->cookie_len
);
672 p
+= D1I(s
)->cookie_len
;
675 /* Ciphers supported */
676 if (!ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &p
[2],
677 bufend
- &p
[2], &outlen
))
680 SSLerror(s
, SSL_R_NO_CIPHERS_AVAILABLE
);
686 /* add in (no) COMPRESSION */
688 *(p
++) = 0; /* Add the NULL method */
691 if ((p
= ssl_add_clienthello_tlsext(s
, p
, bufend
)) == NULL
) {
692 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
696 ssl3_handshake_msg_finish(s
, p
- d
);
698 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_B
;
701 /* SSL3_ST_CW_CLNT_HELLO_B */
702 return (ssl3_handshake_write(s
));
709 ssl3_get_server_hello(SSL
*s
)
711 CBS cbs
, server_random
, session_id
;
712 uint16_t server_version
, cipher_suite
;
713 uint16_t min_version
, max_version
;
714 uint8_t compression_method
;
715 STACK_OF(SSL_CIPHER
) *sk
;
716 const SSL_CIPHER
*cipher
;
717 const SSL_METHOD
*method
;
724 s
->internal
->first_packet
= 1;
725 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_HELLO_A
,
726 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, /* ?? */ &ok
);
729 s
->internal
->first_packet
= 0;
734 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
736 if (SSL_IS_DTLS(s
)) {
737 if (S3I(s
)->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
738 if (D1I(s
)->send_cookie
== 0) {
739 S3I(s
)->tmp
.reuse_message
= 1;
742 /* Already sent a cookie. */
743 al
= SSL_AD_UNEXPECTED_MESSAGE
;
744 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
750 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
751 al
= SSL_AD_UNEXPECTED_MESSAGE
;
752 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
756 if (!CBS_get_u16(&cbs
, &server_version
))
759 if (ssl_supported_version_range(s
, &min_version
, &max_version
) != 1) {
760 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
764 if (server_version
< min_version
|| server_version
> max_version
) {
765 SSLerror(s
, SSL_R_WRONG_SSL_VERSION
);
766 s
->version
= (s
->version
& 0xff00) | (server_version
& 0xff);
767 al
= SSL_AD_PROTOCOL_VERSION
;
770 s
->version
= server_version
;
772 if ((method
= tls1_get_client_method(server_version
)) == NULL
)
773 method
= dtls1_get_client_method(server_version
);
774 if (method
== NULL
) {
775 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
781 if (!CBS_get_bytes(&cbs
, &server_random
, SSL3_RANDOM_SIZE
))
783 if (!CBS_write_bytes(&server_random
, s
->s3
->server_random
,
784 sizeof(s
->s3
->server_random
), NULL
))
788 if (!CBS_get_u8_length_prefixed(&cbs
, &session_id
))
791 if ((CBS_len(&session_id
) > sizeof(s
->session
->session_id
)) ||
792 (CBS_len(&session_id
) > SSL3_SESSION_ID_SIZE
)) {
793 al
= SSL_AD_ILLEGAL_PARAMETER
;
794 SSLerror(s
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
799 if (!CBS_get_u16(&cbs
, &cipher_suite
))
803 * Check if we want to resume the session based on external
806 if (s
->internal
->tls_session_secret_cb
) {
807 SSL_CIPHER
*pref_cipher
= NULL
;
808 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
809 if (s
->internal
->tls_session_secret_cb(s
, s
->session
->master_key
,
810 &s
->session
->master_key_length
, NULL
, &pref_cipher
,
811 s
->internal
->tls_session_secret_cb_arg
)) {
812 s
->session
->cipher
= pref_cipher
? pref_cipher
:
813 ssl3_get_cipher_by_value(cipher_suite
);
814 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
818 if (s
->session
->session_id_length
!= 0 &&
819 CBS_mem_equal(&session_id
, s
->session
->session_id
,
820 s
->session
->session_id_length
)) {
821 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
||
822 timingsafe_memcmp(s
->session
->sid_ctx
,
823 s
->sid_ctx
, s
->sid_ctx_length
) != 0) {
824 /* actually a client application bug */
825 al
= SSL_AD_ILLEGAL_PARAMETER
;
826 SSLerror(s
, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
829 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
830 s
->internal
->hit
= 1;
832 /* a miss or crap from the other end */
834 /* If we were trying for session-id reuse, make a new
835 * SSL_SESSION so we don't stuff up other people */
836 s
->internal
->hit
= 0;
837 if (s
->session
->session_id_length
> 0) {
838 if (!ssl_get_new_session(s
, 0)) {
839 al
= SSL_AD_INTERNAL_ERROR
;
845 * XXX - improve the handling for the case where there is a
846 * zero length session identifier.
848 if (!CBS_write_bytes(&session_id
, s
->session
->session_id
,
849 sizeof(s
->session
->session_id
), &outlen
))
851 s
->session
->session_id_length
= outlen
;
853 s
->session
->ssl_version
= s
->version
;
856 if ((cipher
= ssl3_get_cipher_by_value(cipher_suite
)) == NULL
) {
857 al
= SSL_AD_ILLEGAL_PARAMETER
;
858 SSLerror(s
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
862 /* TLS v1.2 only ciphersuites require v1.2 or later. */
863 if ((cipher
->algorithm_ssl
& SSL_TLSV1_2
) &&
864 (TLS1_get_version(s
) < TLS1_2_VERSION
)) {
865 al
= SSL_AD_ILLEGAL_PARAMETER
;
866 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
870 sk
= ssl_get_ciphers_by_id(s
);
871 i
= sk_SSL_CIPHER_find(sk
, cipher
);
873 /* we did not say we would use this cipher */
874 al
= SSL_AD_ILLEGAL_PARAMETER
;
875 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
880 * Depending on the session caching (internal/external), the cipher
881 * and/or cipher_id values may not be set. Make sure that
882 * cipher_id is set and use it for comparison.
884 if (s
->session
->cipher
)
885 s
->session
->cipher_id
= s
->session
->cipher
->id
;
886 if (s
->internal
->hit
&& (s
->session
->cipher_id
!= cipher
->id
)) {
887 al
= SSL_AD_ILLEGAL_PARAMETER
;
888 SSLerror(s
, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
891 S3I(s
)->hs
.new_cipher
= cipher
;
893 if (!tls1_handshake_hash_init(s
))
897 * Don't digest cached records if no sigalgs: we may need them for
898 * client authentication.
900 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
901 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) &&
902 !tls1_digest_cached_records(s
)) {
903 al
= SSL_AD_INTERNAL_ERROR
;
907 if (!CBS_get_u8(&cbs
, &compression_method
))
910 if (compression_method
!= 0) {
911 al
= SSL_AD_ILLEGAL_PARAMETER
;
912 SSLerror(s
, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
916 /* TLS extensions. */
917 p
= (unsigned char *)CBS_data(&cbs
);
918 if (!ssl_parse_serverhello_tlsext(s
, &p
, CBS_len(&cbs
), &al
)) {
919 /* 'al' set by ssl_parse_serverhello_tlsext */
920 SSLerror(s
, SSL_R_PARSE_TLSEXT
);
923 if (ssl_check_serverhello_tlsext(s
) <= 0) {
924 SSLerror(s
, SSL_R_SERVERHELLO_TLSEXT
);
928 /* See if any data remains... */
929 if (p
- CBS_data(&cbs
) != CBS_len(&cbs
))
935 /* wrong packet length */
936 al
= SSL_AD_DECODE_ERROR
;
937 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
939 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
945 ssl3_get_server_certificate(SSL
*s
)
947 int al
, i
, ok
, ret
= -1;
951 const unsigned char *q
;
952 STACK_OF(X509
) *sk
= NULL
;
954 EVP_PKEY
*pkey
= NULL
;
956 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
957 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
962 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
963 S3I(s
)->tmp
.reuse_message
= 1;
967 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
968 al
= SSL_AD_UNEXPECTED_MESSAGE
;
969 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
974 if ((sk
= sk_X509_new_null()) == NULL
) {
975 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
982 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
983 if (CBS_len(&cbs
) < 3)
986 if (!CBS_get_u24_length_prefixed(&cbs
, &cert_list
) ||
987 CBS_len(&cbs
) != 0) {
988 al
= SSL_AD_DECODE_ERROR
;
989 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
993 while (CBS_len(&cert_list
) > 0) {
996 if (CBS_len(&cert_list
) < 3)
998 if (!CBS_get_u24_length_prefixed(&cert_list
, &cert
)) {
999 al
= SSL_AD_DECODE_ERROR
;
1000 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1004 q
= CBS_data(&cert
);
1005 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
1007 al
= SSL_AD_BAD_CERTIFICATE
;
1008 SSLerror(s
, ERR_R_ASN1_LIB
);
1011 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
1012 al
= SSL_AD_DECODE_ERROR
;
1013 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1016 if (!sk_X509_push(sk
, x
)) {
1017 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1023 i
= ssl_verify_cert_chain(s
, sk
);
1024 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)) {
1025 al
= ssl_verify_alarm_type(s
->verify_result
);
1026 SSLerror(s
, SSL_R_CERTIFICATE_VERIFY_FAILED
);
1030 ERR_clear_error(); /* but we keep s->verify_result */
1032 sc
= ssl_sess_cert_new();
1035 ssl_sess_cert_free(SSI(s
)->sess_cert
);
1036 SSI(s
)->sess_cert
= sc
;
1038 sc
->cert_chain
= sk
;
1040 * Inconsistency alert: cert_chain does include the peer's
1041 * certificate, which we don't include in s3_srvr.c
1043 x
= sk_X509_value(sk
, 0);
1045 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1047 pkey
= X509_get_pubkey(x
);
1049 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1052 SSLerror(s
, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1056 i
= ssl_cert_type(x
, pkey
);
1060 SSLerror(s
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1064 sc
->peer_cert_type
= i
;
1065 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1067 * Why would the following ever happen?
1068 * We just created sc a couple of lines ago.
1070 X509_free(sc
->peer_pkeys
[i
].x509
);
1071 sc
->peer_pkeys
[i
].x509
= x
;
1072 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1074 X509_free(s
->session
->peer
);
1075 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1076 s
->session
->peer
= x
;
1077 s
->session
->verify_result
= s
->verify_result
;
1084 /* wrong packet length */
1085 al
= SSL_AD_DECODE_ERROR
;
1086 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1088 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1091 EVP_PKEY_free(pkey
);
1093 sk_X509_pop_free(sk
, X509_free
);
1099 ssl3_get_server_kex_dhe(SSL
*s
, EVP_PKEY
**pkey
, unsigned char **pp
, long *nn
)
1101 CBS cbs
, dhp
, dhg
, dhpk
;
1102 BN_CTX
*bn_ctx
= NULL
;
1103 SESS_CERT
*sc
= NULL
;
1108 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1109 sc
= SSI(s
)->sess_cert
;
1114 CBS_init(&cbs
, *pp
, *nn
);
1116 if ((dh
= DH_new()) == NULL
) {
1117 SSLerror(s
, ERR_R_DH_LIB
);
1121 if (!CBS_get_u16_length_prefixed(&cbs
, &dhp
))
1123 if ((dh
->p
= BN_bin2bn(CBS_data(&dhp
), CBS_len(&dhp
), NULL
)) == NULL
) {
1124 SSLerror(s
, ERR_R_BN_LIB
);
1128 if (!CBS_get_u16_length_prefixed(&cbs
, &dhg
))
1130 if ((dh
->g
= BN_bin2bn(CBS_data(&dhg
), CBS_len(&dhg
), NULL
)) == NULL
) {
1131 SSLerror(s
, ERR_R_BN_LIB
);
1135 if (!CBS_get_u16_length_prefixed(&cbs
, &dhpk
))
1137 if ((dh
->pub_key
= BN_bin2bn(CBS_data(&dhpk
), CBS_len(&dhpk
),
1139 SSLerror(s
, ERR_R_BN_LIB
);
1144 * Check the strength of the DH key just constructed.
1145 * Discard keys weaker than 1024 bits.
1147 if (DH_size(dh
) < 1024 / 8) {
1148 SSLerror(s
, SSL_R_BAD_DH_P_LENGTH
);
1152 if (alg_a
& SSL_aRSA
)
1153 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1155 /* XXX - Anonymous DH, so no certificate or pkey. */
1158 sc
->peer_dh_tmp
= dh
;
1160 *nn
= CBS_len(&cbs
);
1161 *pp
= (unsigned char *)CBS_data(&cbs
);
1166 al
= SSL_AD_DECODE_ERROR
;
1167 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1168 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1172 BN_CTX_free(bn_ctx
);
1178 ssl3_get_server_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1180 const EC_GROUP
*group
;
1181 EC_GROUP
*ngroup
= NULL
;
1182 EC_POINT
*point
= NULL
;
1183 BN_CTX
*bn_ctx
= NULL
;
1184 EC_KEY
*ecdh
= NULL
;
1188 * Extract the server's ephemeral ECDH public key.
1191 if ((ecdh
= EC_KEY_new()) == NULL
) {
1192 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1196 if ((ngroup
= EC_GROUP_new_by_curve_name(nid
)) == NULL
) {
1197 SSLerror(s
, ERR_R_EC_LIB
);
1200 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1201 SSLerror(s
, ERR_R_EC_LIB
);
1205 group
= EC_KEY_get0_group(ecdh
);
1207 if ((point
= EC_POINT_new(group
)) == NULL
||
1208 (bn_ctx
= BN_CTX_new()) == NULL
) {
1209 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1213 if (EC_POINT_oct2point(group
, point
, CBS_data(public),
1214 CBS_len(public), bn_ctx
) == 0) {
1215 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1216 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1220 EC_KEY_set_public_key(ecdh
, point
);
1221 sc
->peer_ecdh_tmp
= ecdh
;
1227 BN_CTX_free(bn_ctx
);
1228 EC_GROUP_free(ngroup
);
1229 EC_POINT_free(point
);
1236 ssl3_get_server_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1240 if (nid
!= NID_X25519
) {
1241 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1245 if (CBS_len(public) != X25519_KEY_LENGTH
) {
1246 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1247 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1251 if (!CBS_stow(public, &sc
->peer_x25519_tmp
, &outlen
)) {
1252 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1263 ssl3_get_server_kex_ecdhe(SSL
*s
, EVP_PKEY
**pkey
, unsigned char **pp
, long *nn
)
1273 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1274 sc
= SSI(s
)->sess_cert
;
1279 CBS_init(&cbs
, *pp
, *nn
);
1281 /* Only named curves are supported. */
1282 if (!CBS_get_u8(&cbs
, &curve_type
) ||
1283 curve_type
!= NAMED_CURVE_TYPE
||
1284 !CBS_get_u16(&cbs
, &curve_id
)) {
1285 al
= SSL_AD_DECODE_ERROR
;
1286 SSLerror(s
, SSL_R_LENGTH_TOO_SHORT
);
1291 * Check that the curve is one of our preferences - if it is not,
1292 * the server has sent us an invalid curve.
1294 if (tls1_check_curve(s
, curve_id
) != 1) {
1295 al
= SSL_AD_DECODE_ERROR
;
1296 SSLerror(s
, SSL_R_WRONG_CURVE
);
1300 if ((nid
= tls1_ec_curve_id2nid(curve_id
)) == 0) {
1301 al
= SSL_AD_INTERNAL_ERROR
;
1302 SSLerror(s
, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1306 if (!CBS_get_u8_length_prefixed(&cbs
, &public))
1309 if (nid
== NID_X25519
) {
1310 if (ssl3_get_server_kex_ecdhe_ecx(s
, sc
, nid
, &public) != 1)
1313 if (ssl3_get_server_kex_ecdhe_ecp(s
, sc
, nid
, &public) != 1)
1318 * The ECC/TLS specification does not mention the use of DSA to sign
1319 * ECParameters in the server key exchange message. We do support RSA
1322 if (alg_a
& SSL_aRSA
)
1323 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1324 else if (alg_a
& SSL_aECDSA
)
1325 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1327 /* XXX - Anonymous ECDH, so no certificate or pkey. */
1330 *nn
= CBS_len(&cbs
);
1331 *pp
= (unsigned char *)CBS_data(&cbs
);
1336 al
= SSL_AD_DECODE_ERROR
;
1337 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1340 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1347 ssl3_get_server_key_exchange(SSL
*s
)
1349 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
*2];
1351 unsigned char *param
, *p
;
1352 int al
, i
, j
, param_len
, ok
;
1353 long n
, alg_k
, alg_a
;
1354 EVP_PKEY
*pkey
= NULL
;
1355 const EVP_MD
*md
= NULL
;
1358 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
1359 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1362 * Use same message size as in ssl3_get_certificate_request()
1363 * as ServerKeyExchange message may be skipped.
1365 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_KEY_EXCH_A
,
1366 SSL3_ST_CR_KEY_EXCH_B
, -1, s
->internal
->max_cert_list
, &ok
);
1370 EVP_MD_CTX_init(&md_ctx
);
1372 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1374 * Do not skip server key exchange if this cipher suite uses
1377 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
1378 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1379 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1383 S3I(s
)->tmp
.reuse_message
= 1;
1384 EVP_MD_CTX_cleanup(&md_ctx
);
1388 if (SSI(s
)->sess_cert
!= NULL
) {
1389 DH_free(SSI(s
)->sess_cert
->peer_dh_tmp
);
1390 SSI(s
)->sess_cert
->peer_dh_tmp
= NULL
;
1392 EC_KEY_free(SSI(s
)->sess_cert
->peer_ecdh_tmp
);
1393 SSI(s
)->sess_cert
->peer_ecdh_tmp
= NULL
;
1395 free(SSI(s
)->sess_cert
->peer_x25519_tmp
);
1396 SSI(s
)->sess_cert
->peer_x25519_tmp
= NULL
;
1398 SSI(s
)->sess_cert
= ssl_sess_cert_new();
1399 if (SSI(s
)->sess_cert
== NULL
)
1403 param
= p
= (unsigned char *)s
->internal
->init_msg
;
1406 if (alg_k
& SSL_kDHE
) {
1407 if (ssl3_get_server_kex_dhe(s
, &pkey
, &p
, &n
) != 1)
1409 } else if (alg_k
& SSL_kECDHE
) {
1410 if (ssl3_get_server_kex_ecdhe(s
, &pkey
, &p
, &n
) != 1)
1412 } else if (alg_k
!= 0) {
1413 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1414 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1418 param_len
= param_len
- n
;
1420 /* if it was signed, check the signature */
1422 if (SSL_USE_SIGALGS(s
)) {
1423 int sigalg
= tls12_get_sigid(pkey
);
1424 /* Should never happen */
1426 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1430 * Check key type is consistent
1435 if (sigalg
!= (int)p
[1]) {
1436 SSLerror(s
, SSL_R_WRONG_SIGNATURE_TYPE
);
1437 al
= SSL_AD_DECODE_ERROR
;
1440 md
= tls12_get_hash(p
[0]);
1442 SSLerror(s
, SSL_R_UNKNOWN_DIGEST
);
1443 al
= SSL_AD_DECODE_ERROR
;
1455 j
= EVP_PKEY_size(pkey
);
1457 if (i
!= n
|| n
> j
) {
1458 /* wrong packet length */
1459 al
= SSL_AD_DECODE_ERROR
;
1460 SSLerror(s
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1464 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1467 if (!EVP_DigestInit_ex(&md_ctx
, EVP_md5_sha1(), NULL
)) {
1468 al
= SSL_AD_INTERNAL_ERROR
;
1471 EVP_DigestUpdate(&md_ctx
, s
->s3
->client_random
,
1473 EVP_DigestUpdate(&md_ctx
, s
->s3
->server_random
,
1475 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1476 EVP_DigestFinal_ex(&md_ctx
, q
, (unsigned int *)&i
);
1479 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
,
1480 p
, n
, pkey
->pkey
.rsa
);
1482 al
= SSL_AD_DECRYPT_ERROR
;
1483 SSLerror(s
, SSL_R_BAD_RSA_DECRYPT
);
1488 al
= SSL_AD_DECRYPT_ERROR
;
1489 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1493 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
1494 EVP_VerifyUpdate(&md_ctx
, s
->s3
->client_random
,
1496 EVP_VerifyUpdate(&md_ctx
, s
->s3
->server_random
,
1498 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1499 if (EVP_VerifyFinal(&md_ctx
, p
,(int)n
, pkey
) <= 0) {
1501 al
= SSL_AD_DECRYPT_ERROR
;
1502 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1507 /* aNULL does not need public keys. */
1508 if (!(alg_a
& SSL_aNULL
)) {
1509 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1512 /* still data left over */
1514 al
= SSL_AD_DECODE_ERROR
;
1515 SSLerror(s
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1520 EVP_PKEY_free(pkey
);
1521 EVP_MD_CTX_cleanup(&md_ctx
);
1526 /* wrong packet length */
1527 al
= SSL_AD_DECODE_ERROR
;
1528 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1531 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1534 EVP_PKEY_free(pkey
);
1536 EVP_MD_CTX_cleanup(&md_ctx
);
1542 ssl3_get_certificate_request(SSL
*s
)
1547 CBS cert_request
, ctypes
, rdn_list
;
1548 X509_NAME
*xn
= NULL
;
1549 const unsigned char *q
;
1550 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1552 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_REQ_A
,
1553 SSL3_ST_CR_CERT_REQ_B
, -1, s
->internal
->max_cert_list
, &ok
);
1558 S3I(s
)->tmp
.cert_req
= 0;
1560 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1561 S3I(s
)->tmp
.reuse_message
= 1;
1563 * If we get here we don't need any cached handshake records
1564 * as we wont be doing client auth.
1566 if (S3I(s
)->handshake_buffer
) {
1567 if (!tls1_digest_cached_records(s
))
1573 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1574 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1575 SSLerror(s
, SSL_R_WRONG_MESSAGE_TYPE
);
1579 /* TLS does not like anon-DH with client cert */
1580 if (S3I(s
)->hs
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
1581 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1582 SSLerror(s
, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1588 CBS_init(&cert_request
, s
->internal
->init_msg
, n
);
1590 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1591 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1595 /* get the certificate types */
1596 if (!CBS_get_u8(&cert_request
, &ctype_num
))
1599 if (ctype_num
> SSL3_CT_NUMBER
)
1600 ctype_num
= SSL3_CT_NUMBER
;
1601 if (!CBS_get_bytes(&cert_request
, &ctypes
, ctype_num
) ||
1602 !CBS_write_bytes(&ctypes
, (uint8_t *)S3I(s
)->tmp
.ctype
,
1603 sizeof(S3I(s
)->tmp
.ctype
), NULL
)) {
1604 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1608 if (SSL_USE_SIGALGS(s
)) {
1611 if (CBS_len(&cert_request
) < 2) {
1612 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1616 /* Check we have enough room for signature algorithms and
1617 * following length value.
1619 if (!CBS_get_u16_length_prefixed(&cert_request
, &sigalgs
)) {
1620 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1621 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1624 if (!tls1_process_sigalgs(s
, &sigalgs
)) {
1625 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1626 SSLerror(s
, SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
1631 /* get the CA RDNs */
1632 if (CBS_len(&cert_request
) < 2) {
1633 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1637 if (!CBS_get_u16_length_prefixed(&cert_request
, &rdn_list
) ||
1638 CBS_len(&cert_request
) != 0) {
1639 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1640 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1644 while (CBS_len(&rdn_list
) > 0) {
1647 if (CBS_len(&rdn_list
) < 2) {
1648 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1652 if (!CBS_get_u16_length_prefixed(&rdn_list
, &rdn
)) {
1653 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1654 SSLerror(s
, SSL_R_CA_DN_TOO_LONG
);
1659 if ((xn
= d2i_X509_NAME(NULL
, &q
, CBS_len(&rdn
))) == NULL
) {
1660 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1661 SSL_AD_DECODE_ERROR
);
1662 SSLerror(s
, ERR_R_ASN1_LIB
);
1666 if (q
!= CBS_data(&rdn
) + CBS_len(&rdn
)) {
1667 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1668 SSLerror(s
, SSL_R_CA_DN_LENGTH_MISMATCH
);
1671 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1672 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1675 xn
= NULL
; /* avoid free in err block */
1678 /* we should setup a certificate to return.... */
1679 S3I(s
)->tmp
.cert_req
= 1;
1680 S3I(s
)->tmp
.ctype_num
= ctype_num
;
1681 sk_X509_NAME_pop_free(S3I(s
)->tmp
.ca_names
, X509_NAME_free
);
1682 S3I(s
)->tmp
.ca_names
= ca_sk
;
1688 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1692 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1697 ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1699 return (X509_NAME_cmp(*a
, *b
));
1703 ssl3_get_new_session_ticket(SSL
*s
)
1705 int ok
, al
, ret
= 0;
1706 uint32_t lifetime_hint
;
1708 CBS cbs
, session_ticket
;
1710 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SESSION_TICKET_A
,
1711 SSL3_ST_CR_SESSION_TICKET_B
, -1, 16384, &ok
);
1715 if (S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) {
1716 S3I(s
)->tmp
.reuse_message
= 1;
1719 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
) {
1720 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1721 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
1726 al
= SSL_AD_DECODE_ERROR
;
1727 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1731 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1732 if (!CBS_get_u32(&cbs
, &lifetime_hint
) ||
1733 #if UINT32_MAX > LONG_MAX
1734 lifetime_hint
> LONG_MAX
||
1736 !CBS_get_u16_length_prefixed(&cbs
, &session_ticket
) ||
1737 CBS_len(&cbs
) != 0) {
1738 al
= SSL_AD_DECODE_ERROR
;
1739 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1742 s
->session
->tlsext_tick_lifetime_hint
= (long)lifetime_hint
;
1744 if (!CBS_stow(&session_ticket
, &s
->session
->tlsext_tick
,
1745 &s
->session
->tlsext_ticklen
)) {
1746 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1751 * There are two ways to detect a resumed ticket sesion.
1752 * One is to set an appropriate session ID and then the server
1753 * must return a match in ServerHello. This allows the normal
1754 * client session ID matching to work and we know much
1755 * earlier that the ticket has been accepted.
1757 * The other way is to set zero length session ID when the
1758 * ticket is presented and rely on the handshake to determine
1759 * session resumption.
1761 * We choose the former approach because this fits in with
1762 * assumptions elsewhere in OpenSSL. The session ID is set
1763 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1766 EVP_Digest(CBS_data(&session_ticket
), CBS_len(&session_ticket
),
1767 s
->session
->session_id
, &s
->session
->session_id_length
,
1768 EVP_sha256(), NULL
);
1772 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1778 ssl3_get_cert_status(SSL
*s
)
1780 CBS cert_status
, response
;
1784 uint8_t status_type
;
1786 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_STATUS_A
,
1787 SSL3_ST_CR_CERT_STATUS_B
, SSL3_MT_CERTIFICATE_STATUS
,
1794 /* need at least status type + length */
1795 al
= SSL_AD_DECODE_ERROR
;
1796 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1800 CBS_init(&cert_status
, s
->internal
->init_msg
, n
);
1801 if (!CBS_get_u8(&cert_status
, &status_type
) ||
1802 CBS_len(&cert_status
) < 3) {
1803 /* need at least status type + length */
1804 al
= SSL_AD_DECODE_ERROR
;
1805 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1809 if (status_type
!= TLSEXT_STATUSTYPE_ocsp
) {
1810 al
= SSL_AD_DECODE_ERROR
;
1811 SSLerror(s
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
1815 if (!CBS_get_u24_length_prefixed(&cert_status
, &response
) ||
1816 CBS_len(&cert_status
) != 0) {
1817 al
= SSL_AD_DECODE_ERROR
;
1818 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1822 if (!CBS_stow(&response
, &s
->internal
->tlsext_ocsp_resp
,
1823 &stow_len
) || stow_len
> INT_MAX
) {
1824 s
->internal
->tlsext_ocsp_resplen
= 0;
1825 al
= SSL_AD_INTERNAL_ERROR
;
1826 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1829 s
->internal
->tlsext_ocsp_resplen
= (int)stow_len
;
1831 if (s
->ctx
->internal
->tlsext_status_cb
) {
1833 ret
= s
->ctx
->internal
->tlsext_status_cb(s
,
1834 s
->ctx
->internal
->tlsext_status_arg
);
1836 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
1837 SSLerror(s
, SSL_R_INVALID_STATUS_RESPONSE
);
1841 al
= SSL_AD_INTERNAL_ERROR
;
1842 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1848 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1853 ssl3_get_server_done(SSL
*s
)
1858 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_DONE_A
,
1859 SSL3_ST_CR_SRVR_DONE_B
, SSL3_MT_SERVER_DONE
,
1860 30, /* should be very small, like 0 :-) */ &ok
);
1865 /* should contain no data */
1866 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1867 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1875 ssl3_send_client_kex_rsa(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1877 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
];
1878 unsigned char *enc_pms
= NULL
;
1879 EVP_PKEY
*pkey
= NULL
;
1885 * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1.
1888 pkey
= X509_get_pubkey(sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1889 if (pkey
== NULL
|| pkey
->type
!= EVP_PKEY_RSA
||
1890 pkey
->pkey
.rsa
== NULL
) {
1891 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1895 pms
[0] = s
->client_version
>> 8;
1896 pms
[1] = s
->client_version
& 0xff;
1897 arc4random_buf(&pms
[2], sizeof(pms
) - 2);
1899 if ((enc_pms
= malloc(RSA_size(pkey
->pkey
.rsa
))) == NULL
) {
1900 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1904 enc_len
= RSA_public_encrypt(sizeof(pms
), pms
, enc_pms
, pkey
->pkey
.rsa
,
1907 SSLerror(s
, SSL_R_BAD_RSA_ENCRYPT
);
1911 if (!CBB_add_u16_length_prefixed(cbb
, &epms
))
1913 if (!CBB_add_bytes(&epms
, enc_pms
, enc_len
))
1915 if (!CBB_flush(cbb
))
1918 s
->session
->master_key_length
=
1919 tls1_generate_master_secret(s
,
1920 s
->session
->master_key
, pms
, sizeof(pms
));
1925 explicit_bzero(pms
, sizeof(pms
));
1926 EVP_PKEY_free(pkey
);
1933 ssl3_send_client_kex_dhe(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1935 DH
*dh_srvr
= NULL
, *dh_clnt
= NULL
;
1936 unsigned char *key
= NULL
;
1937 int key_size
= 0, key_len
;
1938 unsigned char *data
;
1942 /* Ensure that we have an ephemeral key for DHE. */
1943 if (sess_cert
->peer_dh_tmp
== NULL
) {
1944 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
1945 SSLerror(s
, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
1948 dh_srvr
= sess_cert
->peer_dh_tmp
;
1950 /* Generate a new random key. */
1951 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
1952 SSLerror(s
, ERR_R_DH_LIB
);
1955 if (!DH_generate_key(dh_clnt
)) {
1956 SSLerror(s
, ERR_R_DH_LIB
);
1959 key_size
= DH_size(dh_clnt
);
1960 if ((key
= malloc(key_size
)) == NULL
) {
1961 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1964 key_len
= DH_compute_key(key
, dh_srvr
->pub_key
, dh_clnt
);
1966 SSLerror(s
, ERR_R_DH_LIB
);
1970 /* Generate master key from the result. */
1971 s
->session
->master_key_length
=
1972 tls1_generate_master_secret(s
,
1973 s
->session
->master_key
, key
, key_len
);
1975 if (!CBB_add_u16_length_prefixed(cbb
, &dh_Yc
))
1977 if (!CBB_add_space(&dh_Yc
, &data
, BN_num_bytes(dh_clnt
->pub_key
)))
1979 BN_bn2bin(dh_clnt
->pub_key
, data
);
1980 if (!CBB_flush(cbb
))
1987 freezero(key
, key_size
);
1993 ssl3_send_client_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
1995 const EC_GROUP
*group
= NULL
;
1996 const EC_POINT
*point
= NULL
;
1997 EC_KEY
*ecdh
= NULL
;
1998 BN_CTX
*bn_ctx
= NULL
;
1999 unsigned char *key
= NULL
;
2000 unsigned char *data
;
2002 int key_size
= 0, key_len
;
2006 if ((group
= EC_KEY_get0_group(sc
->peer_ecdh_tmp
)) == NULL
||
2007 (point
= EC_KEY_get0_public_key(sc
->peer_ecdh_tmp
)) == NULL
) {
2008 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2012 if ((ecdh
= EC_KEY_new()) == NULL
) {
2013 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2017 if (!EC_KEY_set_group(ecdh
, group
)) {
2018 SSLerror(s
, ERR_R_EC_LIB
);
2022 /* Generate a new ECDH key pair. */
2023 if (!(EC_KEY_generate_key(ecdh
))) {
2024 SSLerror(s
, ERR_R_ECDH_LIB
);
2027 if ((key_size
= ECDH_size(ecdh
)) <= 0) {
2028 SSLerror(s
, ERR_R_ECDH_LIB
);
2031 if ((key
= malloc(key_size
)) == NULL
) {
2032 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2034 key_len
= ECDH_compute_key(key
, key_size
, point
, ecdh
, NULL
);
2036 SSLerror(s
, ERR_R_ECDH_LIB
);
2040 /* Generate master key from the result. */
2041 s
->session
->master_key_length
=
2042 tls1_generate_master_secret(s
,
2043 s
->session
->master_key
, key
, key_len
);
2045 encoded_len
= EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2046 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
2047 if (encoded_len
== 0) {
2048 SSLerror(s
, ERR_R_ECDH_LIB
);
2052 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
2053 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2057 /* Encode the public key. */
2058 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2060 if (!CBB_add_space(&ecpoint
, &data
, encoded_len
))
2062 if (EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2063 POINT_CONVERSION_UNCOMPRESSED
, data
, encoded_len
,
2066 if (!CBB_flush(cbb
))
2072 freezero(key
, key_size
);
2074 BN_CTX_free(bn_ctx
);
2081 ssl3_send_client_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2083 uint8_t *public_key
= NULL
, *private_key
= NULL
, *shared_key
= NULL
;
2087 /* Generate X25519 key pair and derive shared key. */
2088 if ((public_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2090 if ((private_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2092 if ((shared_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2094 X25519_keypair(public_key
, private_key
);
2095 if (!X25519(shared_key
, private_key
, sc
->peer_x25519_tmp
))
2098 /* Serialize the public key. */
2099 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2101 if (!CBB_add_bytes(&ecpoint
, public_key
, X25519_KEY_LENGTH
))
2103 if (!CBB_flush(cbb
))
2106 /* Generate master key from the result. */
2107 s
->session
->master_key_length
=
2108 tls1_generate_master_secret(s
,
2109 s
->session
->master_key
, shared_key
, X25519_KEY_LENGTH
);
2115 freezero(private_key
, X25519_KEY_LENGTH
);
2116 freezero(shared_key
, X25519_KEY_LENGTH
);
2122 ssl3_send_client_kex_ecdhe(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2124 if (sc
->peer_x25519_tmp
!= NULL
) {
2125 if (ssl3_send_client_kex_ecdhe_ecx(s
, sc
, cbb
) != 1)
2127 } else if (sc
->peer_ecdh_tmp
!= NULL
) {
2128 if (ssl3_send_client_kex_ecdhe_ecp(s
, sc
, cbb
) != 1)
2131 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2132 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2143 ssl3_send_client_kex_gost(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
2145 unsigned char premaster_secret
[32], shared_ukm
[32], tmp
[256];
2146 EVP_PKEY
*pub_key
= NULL
;
2147 EVP_PKEY_CTX
*pkey_ctx
;
2150 unsigned int md_len
;
2151 EVP_MD_CTX
*ukm_hash
;
2156 /* Get server sertificate PKEY and create ctx from it */
2157 peer_cert
= sess_cert
->peer_pkeys
[SSL_PKEY_GOST01
].x509
;
2158 if (peer_cert
== NULL
) {
2159 SSLerror(s
, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2163 pub_key
= X509_get_pubkey(peer_cert
);
2164 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
, NULL
);
2167 * If we have send a certificate, and certificate key parameters match
2168 * those of server certificate, use certificate key for key exchange.
2169 * Otherwise, generate ephemeral key pair.
2171 EVP_PKEY_encrypt_init(pkey_ctx
);
2173 /* Generate session key. */
2174 arc4random_buf(premaster_secret
, 32);
2177 * If we have client certificate, use its secret as peer key.
2179 if (S3I(s
)->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2180 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2181 s
->cert
->key
->privatekey
) <=0) {
2183 * If there was an error - just ignore it.
2184 * Ephemeral key would be used.
2191 * Compute shared IV and store it in algorithm-specific context data.
2193 ukm_hash
= EVP_MD_CTX_create();
2194 if (ukm_hash
== NULL
) {
2195 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2199 if (ssl_get_algorithm2(s
) & SSL_HANDSHAKE_MAC_GOST94
)
2200 nid
= NID_id_GostR3411_94
;
2202 nid
= NID_id_tc26_gost3411_2012_256
;
2203 if (!EVP_DigestInit(ukm_hash
, EVP_get_digestbynid(nid
)))
2205 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2206 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2207 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2208 EVP_MD_CTX_destroy(ukm_hash
);
2209 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
2210 EVP_PKEY_CTRL_SET_IV
, 8, shared_ukm
) < 0) {
2211 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2216 * Make GOST keytransport blob message, encapsulate it into sequence.
2219 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, premaster_secret
,
2221 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2225 if (!CBB_add_asn1(cbb
, &gostblob
, CBS_ASN1_SEQUENCE
))
2227 if (!CBB_add_bytes(&gostblob
, tmp
, msglen
))
2229 if (!CBB_flush(cbb
))
2232 /* Check if pubkey from client certificate was used. */
2233 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
2235 /* Set flag "skip certificate verify". */
2236 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2238 EVP_PKEY_CTX_free(pkey_ctx
);
2239 s
->session
->master_key_length
=
2240 tls1_generate_master_secret(s
,
2241 s
->session
->master_key
, premaster_secret
, 32);
2246 explicit_bzero(premaster_secret
, sizeof(premaster_secret
));
2247 EVP_PKEY_free(pub_key
);
2253 ssl3_send_client_key_exchange(SSL
*s
)
2255 SESS_CERT
*sess_cert
;
2256 unsigned long alg_k
;
2259 memset(&cbb
, 0, sizeof(cbb
));
2261 if (S3I(s
)->hs
.state
== SSL3_ST_CW_KEY_EXCH_A
) {
2262 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2264 if ((sess_cert
= SSI(s
)->sess_cert
) == NULL
) {
2265 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2266 SSL_AD_UNEXPECTED_MESSAGE
);
2267 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2271 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &kex
,
2272 SSL3_MT_CLIENT_KEY_EXCHANGE
))
2275 if (alg_k
& SSL_kRSA
) {
2276 if (ssl3_send_client_kex_rsa(s
, sess_cert
, &kex
) != 1)
2278 } else if (alg_k
& SSL_kDHE
) {
2279 if (ssl3_send_client_kex_dhe(s
, sess_cert
, &kex
) != 1)
2281 } else if (alg_k
& SSL_kECDHE
) {
2282 if (ssl3_send_client_kex_ecdhe(s
, sess_cert
, &kex
) != 1)
2284 } else if (alg_k
& SSL_kGOST
) {
2285 if (ssl3_send_client_kex_gost(s
, sess_cert
, &kex
) != 1)
2288 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2289 SSL_AD_HANDSHAKE_FAILURE
);
2290 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2294 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2297 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_B
;
2300 /* SSL3_ST_CW_KEY_EXCH_B */
2301 return (ssl3_handshake_write(s
));
2310 ssl3_send_client_verify(SSL
*s
)
2313 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
2315 EVP_PKEY_CTX
*pctx
= NULL
;
2321 EVP_MD_CTX_init(&mctx
);
2323 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_VRFY_A
) {
2324 p
= ssl3_handshake_msg_start(s
, SSL3_MT_CERTIFICATE_VERIFY
);
2327 * Create context from key and test if sha1 is allowed as
2330 pkey
= s
->cert
->key
->privatekey
;
2331 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2332 EVP_PKEY_sign_init(pctx
);
2334 /* XXX - is this needed? */
2335 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) <= 0)
2338 if (!SSL_USE_SIGALGS(s
)) {
2339 if (S3I(s
)->handshake_buffer
) {
2340 if (!tls1_digest_cached_records(s
))
2343 if (!tls1_handshake_hash_value(s
, data
, sizeof(data
),
2349 * For TLS v1.2 send signature algorithm and signature
2350 * using agreed digest and cached handshake records.
2352 if (SSL_USE_SIGALGS(s
)) {
2355 const EVP_MD
*md
= s
->cert
->key
->digest
;
2356 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
,
2358 if (hdatalen
<= 0 ||
2359 !tls12_get_sigandhash(p
, pkey
, md
)) {
2360 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2364 if (!EVP_SignInit_ex(&mctx
, md
, NULL
) ||
2365 !EVP_SignUpdate(&mctx
, hdata
, hdatalen
) ||
2366 !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
2367 SSLerror(s
, ERR_R_EVP_LIB
);
2372 if (!tls1_digest_cached_records(s
))
2374 } else if (pkey
->type
== EVP_PKEY_RSA
) {
2375 if (RSA_sign(NID_md5_sha1
, data
,
2376 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, &(p
[2]),
2377 &u
, pkey
->pkey
.rsa
) <= 0 ) {
2378 SSLerror(s
, ERR_R_RSA_LIB
);
2383 } else if (pkey
->type
== EVP_PKEY_EC
) {
2384 if (!ECDSA_sign(pkey
->save_type
,
2385 &(data
[MD5_DIGEST_LENGTH
]),
2386 SHA_DIGEST_LENGTH
, &(p
[2]),
2387 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
2388 SSLerror(s
, ERR_R_ECDSA_LIB
);
2393 #ifndef OPENSSL_NO_GOST
2394 } else if (pkey
->type
== NID_id_GostR3410_94
||
2395 pkey
->type
== NID_id_GostR3410_2001
) {
2396 unsigned char signbuf
[128];
2403 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2404 if (hdatalen
<= 0) {
2405 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2408 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2409 !(md
= EVP_get_digestbynid(nid
))) {
2410 SSLerror(s
, ERR_R_EVP_LIB
);
2413 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2414 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2415 !EVP_DigestFinal(&mctx
, signbuf
, &u
) ||
2416 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2417 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
2418 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2419 GOST_SIG_FORMAT_RS_LE
,
2421 (EVP_PKEY_sign(pctx
, &(p
[2]), &sigsize
,
2422 signbuf
, u
) <= 0)) {
2423 SSLerror(s
, ERR_R_EVP_LIB
);
2426 if (!tls1_digest_cached_records(s
))
2433 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2437 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_VRFY_B
;
2439 ssl3_handshake_msg_finish(s
, n
);
2442 EVP_MD_CTX_cleanup(&mctx
);
2443 EVP_PKEY_CTX_free(pctx
);
2445 return (ssl3_handshake_write(s
));
2448 EVP_MD_CTX_cleanup(&mctx
);
2449 EVP_PKEY_CTX_free(pctx
);
2454 ssl3_send_client_certificate(SSL
*s
)
2456 EVP_PKEY
*pkey
= NULL
;
2458 CBB cbb
, client_cert
;
2461 memset(&cbb
, 0, sizeof(cbb
));
2463 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_A
) {
2464 if ((s
->cert
== NULL
) || (s
->cert
->key
->x509
== NULL
) ||
2465 (s
->cert
->key
->privatekey
== NULL
))
2466 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_B
;
2468 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_C
;
2471 /* We need to get a client cert */
2472 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_B
) {
2474 * If we get an error, we need to
2475 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2476 * We then get retied later
2478 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2480 s
->internal
->rwstate
= SSL_X509_LOOKUP
;
2483 s
->internal
->rwstate
= SSL_NOTHING
;
2484 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2485 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_B
;
2486 if (!SSL_use_certificate(s
, x509
) ||
2487 !SSL_use_PrivateKey(s
, pkey
))
2489 } else if (i
== 1) {
2491 SSLerror(s
, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2495 EVP_PKEY_free(pkey
);
2497 S3I(s
)->tmp
.cert_req
= 2;
2499 /* Ok, we have a cert */
2500 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_C
;
2503 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_C
) {
2504 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &client_cert
,
2505 SSL3_MT_CERTIFICATE
))
2507 if (!ssl3_output_cert_chain(s
, &client_cert
,
2508 (S3I(s
)->tmp
.cert_req
== 2) ? NULL
: s
->cert
->key
->x509
))
2510 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2513 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_D
;
2516 /* SSL3_ST_CW_CERT_D */
2517 return (ssl3_handshake_write(s
));
2525 #define has_bits(i,m) (((i)&(m)) == (m))
2528 ssl3_check_cert_and_algorithm(SSL
*s
)
2532 EVP_PKEY
*pkey
= NULL
;
2536 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2537 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
2539 /* We don't have a certificate. */
2540 if (alg_a
& SSL_aNULL
)
2543 sc
= SSI(s
)->sess_cert
;
2545 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2548 dh
= SSI(s
)->sess_cert
->peer_dh_tmp
;
2550 /* This is the passed certificate. */
2552 idx
= sc
->peer_cert_type
;
2553 if (idx
== SSL_PKEY_ECC
) {
2554 if (ssl_check_srvr_ecc_cert_and_alg(
2555 sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
2557 SSLerror(s
, SSL_R_BAD_ECC_CERT
);
2563 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
2564 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
2565 EVP_PKEY_free(pkey
);
2567 /* Check that we have a certificate if we require one. */
2568 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_SIGN
)) {
2569 SSLerror(s
, SSL_R_MISSING_RSA_SIGNING_CERT
);
2572 if ((alg_k
& SSL_kRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_ENC
)) {
2573 SSLerror(s
, SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2576 if ((alg_k
& SSL_kDHE
) &&
2577 !(has_bits(i
, EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
))) {
2578 SSLerror(s
, SSL_R_MISSING_DH_KEY
);
2584 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
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
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
2607 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
2610 S3I(s
)->tmp
.reuse_message
= 1;
2611 if ((S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) ||
2612 (S3I(s
)->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
->internal
->client_cert_engine
) {
2625 i
= ENGINE_load_ssl_client_cert(
2626 s
->ctx
->internal
->client_cert_engine
, s
,
2627 SSL_get_client_CA_list(s
), px509
, ppkey
, NULL
, NULL
, NULL
);
2632 if (s
->ctx
->internal
->client_cert_cb
)
2633 i
= s
->ctx
->internal
->client_cert_cb(s
, px509
, ppkey
);