1 /* $OpenBSD: ssl_clnt.c,v 1.23 2018/02/08 11:30:30 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"
173 #include "ssl_tlsext.h"
175 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
);
180 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
182 int new_state
, state
, skip
= 0;
187 if (s
->internal
->info_callback
!= NULL
)
188 cb
= s
->internal
->info_callback
;
189 else if (s
->ctx
->internal
->info_callback
!= NULL
)
190 cb
= s
->ctx
->internal
->info_callback
;
192 s
->internal
->in_handshake
++;
193 if (!SSL_in_init(s
) || SSL_in_before(s
))
197 state
= S3I(s
)->hs
.state
;
199 switch (S3I(s
)->hs
.state
) {
200 case SSL_ST_RENEGOTIATE
:
201 s
->internal
->renegotiate
= 1;
202 S3I(s
)->hs
.state
= SSL_ST_CONNECT
;
203 s
->ctx
->internal
->stats
.sess_connect_renegotiate
++;
207 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
208 case SSL_ST_OK
|SSL_ST_CONNECT
:
212 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
214 if (SSL_IS_DTLS(s
)) {
215 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00)) {
216 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
221 if ((s
->version
& 0xff00) != 0x0300) {
222 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
228 /* s->version=SSL3_VERSION; */
229 s
->internal
->type
= SSL_ST_CONNECT
;
231 if (!ssl3_setup_init_buffer(s
)) {
235 if (!ssl3_setup_buffers(s
)) {
239 if (!ssl_init_wbio_buffer(s
, 0)) {
244 /* don't push the buffering BIO quite yet */
246 if (!SSL_IS_DTLS(s
)) {
247 if (!tls1_init_finished_mac(s
)) {
253 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_A
;
254 s
->ctx
->internal
->stats
.sess_connect
++;
255 s
->internal
->init_num
= 0;
257 if (SSL_IS_DTLS(s
)) {
258 /* mark client_random uninitialized */
259 memset(s
->s3
->client_random
, 0,
260 sizeof(s
->s3
->client_random
));
261 D1I(s
)->send_cookie
= 0;
262 s
->internal
->hit
= 0;
266 case SSL3_ST_CW_CLNT_HELLO_A
:
267 case SSL3_ST_CW_CLNT_HELLO_B
:
268 s
->internal
->shutdown
= 0;
270 if (SSL_IS_DTLS(s
)) {
271 /* every DTLS ClientHello resets Finished MAC */
272 if (!tls1_init_finished_mac(s
)) {
277 dtls1_start_timer(s
);
280 ret
= ssl3_send_client_hello(s
);
284 if (SSL_IS_DTLS(s
) && D1I(s
)->send_cookie
) {
285 S3I(s
)->hs
.state
= SSL3_ST_CW_FLUSH
;
286 S3I(s
)->hs
.next_state
= SSL3_ST_CR_SRVR_HELLO_A
;
288 S3I(s
)->hs
.state
= SSL3_ST_CR_SRVR_HELLO_A
;
290 s
->internal
->init_num
= 0;
292 /* turn on buffering for the next lot of output */
293 if (s
->bbio
!= s
->wbio
)
294 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
298 case SSL3_ST_CR_SRVR_HELLO_A
:
299 case SSL3_ST_CR_SRVR_HELLO_B
:
300 ret
= ssl3_get_server_hello(s
);
304 if (s
->internal
->hit
) {
305 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
306 if (!SSL_IS_DTLS(s
)) {
307 if (s
->internal
->tlsext_ticket_expected
) {
308 /* receive renewed session ticket */
309 S3I(s
)->hs
.state
= SSL3_ST_CR_SESSION_TICKET_A
;
312 } else if (SSL_IS_DTLS(s
)) {
313 S3I(s
)->hs
.state
= DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A
;
315 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_A
;
317 s
->internal
->init_num
= 0;
320 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A
:
321 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B
:
322 ret
= dtls1_get_hello_verify(s
);
326 if (D1I(s
)->send_cookie
) /* start again, with a cookie */
327 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_A
;
329 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_A
;
330 s
->internal
->init_num
= 0;
333 case SSL3_ST_CR_CERT_A
:
334 case SSL3_ST_CR_CERT_B
:
335 ret
= ssl3_check_finished(s
);
339 s
->internal
->hit
= 1;
340 if (s
->internal
->tlsext_ticket_expected
)
341 S3I(s
)->hs
.state
= SSL3_ST_CR_SESSION_TICKET_A
;
343 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
344 s
->internal
->init_num
= 0;
347 /* Check if it is anon DH/ECDH. */
348 if (!(S3I(s
)->hs
.new_cipher
->algorithm_auth
&
350 ret
= ssl3_get_server_certificate(s
);
353 if (s
->internal
->tlsext_status_expected
)
354 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_STATUS_A
;
356 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
359 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
361 s
->internal
->init_num
= 0;
364 case SSL3_ST_CR_KEY_EXCH_A
:
365 case SSL3_ST_CR_KEY_EXCH_B
:
366 ret
= ssl3_get_server_key_exchange(s
);
369 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_REQ_A
;
370 s
->internal
->init_num
= 0;
373 * At this point we check that we have the
374 * required stuff from the server.
376 if (!ssl3_check_cert_and_algorithm(s
)) {
382 case SSL3_ST_CR_CERT_REQ_A
:
383 case SSL3_ST_CR_CERT_REQ_B
:
384 ret
= ssl3_get_certificate_request(s
);
387 S3I(s
)->hs
.state
= SSL3_ST_CR_SRVR_DONE_A
;
388 s
->internal
->init_num
= 0;
391 case SSL3_ST_CR_SRVR_DONE_A
:
392 case SSL3_ST_CR_SRVR_DONE_B
:
393 ret
= ssl3_get_server_done(s
);
398 if (S3I(s
)->tmp
.cert_req
)
399 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_A
;
401 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_A
;
402 s
->internal
->init_num
= 0;
406 case SSL3_ST_CW_CERT_A
:
407 case SSL3_ST_CW_CERT_B
:
408 case SSL3_ST_CW_CERT_C
:
409 case SSL3_ST_CW_CERT_D
:
411 dtls1_start_timer(s
);
412 ret
= ssl3_send_client_certificate(s
);
415 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_A
;
416 s
->internal
->init_num
= 0;
419 case SSL3_ST_CW_KEY_EXCH_A
:
420 case SSL3_ST_CW_KEY_EXCH_B
:
422 dtls1_start_timer(s
);
423 ret
= ssl3_send_client_key_exchange(s
);
427 * EAY EAY EAY need to check for DH fix cert
431 * For TLS, cert_req is set to 2, so a cert chain
432 * of nothing is sent, but no verify packet is sent
435 * XXX: For now, we do not support client
436 * authentication in ECDH cipher suites with
437 * ECDH (rather than ECDSA) certificates.
438 * We need to skip the certificate verify
439 * message when client's ECDH public key is sent
440 * inside the client certificate.
442 if (S3I(s
)->tmp
.cert_req
== 1) {
443 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_VRFY_A
;
445 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
446 S3I(s
)->change_cipher_spec
= 0;
448 if (!SSL_IS_DTLS(s
)) {
449 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
450 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
451 S3I(s
)->change_cipher_spec
= 0;
455 s
->internal
->init_num
= 0;
458 case SSL3_ST_CW_CERT_VRFY_A
:
459 case SSL3_ST_CW_CERT_VRFY_B
:
461 dtls1_start_timer(s
);
462 ret
= ssl3_send_client_verify(s
);
465 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
466 s
->internal
->init_num
= 0;
467 S3I(s
)->change_cipher_spec
= 0;
470 case SSL3_ST_CW_CHANGE_A
:
471 case SSL3_ST_CW_CHANGE_B
:
472 if (SSL_IS_DTLS(s
) && !s
->internal
->hit
)
473 dtls1_start_timer(s
);
474 ret
= ssl3_send_change_cipher_spec(s
,
475 SSL3_ST_CW_CHANGE_A
, SSL3_ST_CW_CHANGE_B
);
479 S3I(s
)->hs
.state
= SSL3_ST_CW_FINISHED_A
;
480 s
->internal
->init_num
= 0;
482 s
->session
->cipher
= S3I(s
)->hs
.new_cipher
;
483 if (!tls1_setup_key_block(s
)) {
488 if (!tls1_change_cipher_state(s
,
489 SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
495 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
499 case SSL3_ST_CW_FINISHED_A
:
500 case SSL3_ST_CW_FINISHED_B
:
501 if (SSL_IS_DTLS(s
) && !s
->internal
->hit
)
502 dtls1_start_timer(s
);
503 ret
= ssl3_send_finished(s
, SSL3_ST_CW_FINISHED_A
,
504 SSL3_ST_CW_FINISHED_B
, TLS_MD_CLIENT_FINISH_CONST
,
505 TLS_MD_CLIENT_FINISH_CONST_SIZE
);
509 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
510 S3I(s
)->hs
.state
= SSL3_ST_CW_FLUSH
;
513 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
514 if (s
->internal
->hit
) {
515 S3I(s
)->hs
.next_state
= SSL_ST_OK
;
517 SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
518 S3I(s
)->hs
.state
= SSL_ST_OK
;
519 s
->s3
->flags
|= SSL3_FLAGS_POP_BUFFER
;
520 S3I(s
)->delay_buf_pop_ret
= 0;
523 /* Allow NewSessionTicket if ticket expected */
524 if (s
->internal
->tlsext_ticket_expected
)
525 S3I(s
)->hs
.next_state
=
526 SSL3_ST_CR_SESSION_TICKET_A
;
528 S3I(s
)->hs
.next_state
=
529 SSL3_ST_CR_FINISHED_A
;
531 s
->internal
->init_num
= 0;
534 case SSL3_ST_CR_SESSION_TICKET_A
:
535 case SSL3_ST_CR_SESSION_TICKET_B
:
536 ret
= ssl3_get_new_session_ticket(s
);
539 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
540 s
->internal
->init_num
= 0;
543 case SSL3_ST_CR_CERT_STATUS_A
:
544 case SSL3_ST_CR_CERT_STATUS_B
:
545 ret
= ssl3_get_cert_status(s
);
548 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
549 s
->internal
->init_num
= 0;
552 case SSL3_ST_CR_FINISHED_A
:
553 case SSL3_ST_CR_FINISHED_B
:
555 D1I(s
)->change_cipher_spec_ok
= 1;
557 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
558 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
559 SSL3_ST_CR_FINISHED_B
);
565 if (s
->internal
->hit
)
566 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
568 S3I(s
)->hs
.state
= SSL_ST_OK
;
569 s
->internal
->init_num
= 0;
572 case SSL3_ST_CW_FLUSH
:
573 s
->internal
->rwstate
= SSL_WRITING
;
574 if (BIO_flush(s
->wbio
) <= 0) {
575 if (SSL_IS_DTLS(s
)) {
576 /* If the write error was fatal, stop trying */
577 if (!BIO_should_retry(s
->wbio
)) {
578 s
->internal
->rwstate
= SSL_NOTHING
;
579 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
585 s
->internal
->rwstate
= SSL_NOTHING
;
586 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
590 /* clean a few things up */
591 tls1_cleanup_key_block(s
);
593 if (!SSL_IS_DTLS(s
)) {
594 BUF_MEM_free(s
->internal
->init_buf
);
595 s
->internal
->init_buf
= NULL
;
599 * If we are not 'joining' the last two packets,
600 * remove the buffering now
602 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
603 ssl_free_wbio_buffer(s
);
604 /* else do it later in ssl3_write */
606 s
->internal
->init_num
= 0;
607 s
->internal
->renegotiate
= 0;
608 s
->internal
->new_session
= 0;
610 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
611 if (s
->internal
->hit
)
612 s
->ctx
->internal
->stats
.sess_hit
++;
616 s
->internal
->handshake_func
= ssl3_connect
;
617 s
->ctx
->internal
->stats
.sess_connect_good
++;
620 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
622 if (SSL_IS_DTLS(s
)) {
623 /* done with handshaking */
624 D1I(s
)->handshake_read_seq
= 0;
625 D1I(s
)->next_handshake_write_seq
= 0;
632 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
638 /* did we do anything */
639 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
640 if (s
->internal
->debug
) {
641 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
645 if ((cb
!= NULL
) && (S3I(s
)->hs
.state
!= state
)) {
646 new_state
= S3I(s
)->hs
.state
;
647 S3I(s
)->hs
.state
= state
;
648 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
649 S3I(s
)->hs
.state
= new_state
;
656 s
->internal
->in_handshake
--;
658 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
664 ssl3_send_client_hello(SSL
*s
)
666 CBB cbb
, client_hello
, session_id
, cookie
, cipher_suites
;
667 CBB compression_methods
;
668 uint16_t max_version
;
671 memset(&cbb
, 0, sizeof(cbb
));
673 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CLNT_HELLO_A
) {
674 SSL_SESSION
*sess
= s
->session
;
676 if (ssl_supported_version_range(s
, NULL
, &max_version
) != 1) {
677 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
680 s
->client_version
= s
->version
= max_version
;
683 sess
->ssl_version
!= s
->version
||
684 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
685 sess
->internal
->not_resumable
) {
686 if (!ssl_get_new_session(s
, 0))
689 /* else use the pre-loaded session */
692 * If a DTLS ClientHello message is being resent after a
693 * HelloVerifyRequest, we must retain the original client
696 if (!SSL_IS_DTLS(s
) || D1I(s
)->send_cookie
== 0)
697 arc4random_buf(s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
699 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &client_hello
,
700 SSL3_MT_CLIENT_HELLO
))
704 * Version indicates the negotiated version: for example from
705 * an SSLv2/v3 compatible client hello). The client_version
706 * field is the maximum version we permit and it is also
707 * used in RSA encrypted premaster secrets. Some servers can
708 * choke if we initially report a higher version then
709 * renegotiate to a lower one in the premaster secret. This
710 * didn't happen with TLS 1.0 as most servers supported it
711 * but it can with TLS 1.1 or later if the server only supports
714 * Possible scenario with previous logic:
715 * 1. Client hello indicates TLS 1.2
716 * 2. Server hello says TLS 1.0
717 * 3. RSA encrypted premaster secret uses 1.2.
718 * 4. Handhaked proceeds using TLS 1.0.
719 * 5. Server sends hello request to renegotiate.
720 * 6. Client hello indicates TLS v1.0 as we now
721 * know that is maximum server supports.
722 * 7. Server chokes on RSA encrypted premaster secret
723 * containing version 1.0.
725 * For interoperability it should be OK to always use the
726 * maximum version we support in client hello and then rely
727 * on the checking of version to ensure the servers isn't
728 * being inconsistent: for example initially negotiating with
729 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
730 * client_version in client hello and not resetting it to
731 * the negotiated version.
733 if (!CBB_add_u16(&client_hello
, s
->client_version
))
737 if (!CBB_add_bytes(&client_hello
, s
->s3
->client_random
,
738 sizeof(s
->s3
->client_random
)))
742 if (!CBB_add_u8_length_prefixed(&client_hello
, &session_id
))
744 if (!s
->internal
->new_session
&&
745 s
->session
->session_id_length
> 0) {
746 sl
= s
->session
->session_id_length
;
747 if (sl
> sizeof(s
->session
->session_id
)) {
748 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
751 if (!CBB_add_bytes(&session_id
,
752 s
->session
->session_id
, sl
))
757 if (SSL_IS_DTLS(s
)) {
758 if (D1I(s
)->cookie_len
> sizeof(D1I(s
)->cookie
)) {
759 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
762 if (!CBB_add_u8_length_prefixed(&client_hello
, &cookie
))
764 if (!CBB_add_bytes(&cookie
, D1I(s
)->cookie
,
769 /* Ciphers supported */
770 if (!CBB_add_u16_length_prefixed(&client_hello
, &cipher_suites
))
772 if (!ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
),
774 SSLerror(s
, SSL_R_NO_CIPHERS_AVAILABLE
);
778 /* Add in compression methods (null) */
779 if (!CBB_add_u8_length_prefixed(&client_hello
,
780 &compression_methods
))
782 if (!CBB_add_u8(&compression_methods
, 0))
786 if (!tlsext_clienthello_build(s
, &client_hello
)) {
787 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
791 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
794 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_B
;
797 /* SSL3_ST_CW_CLNT_HELLO_B */
798 return (ssl3_handshake_write(s
));
807 ssl3_get_server_hello(SSL
*s
)
809 CBS cbs
, server_random
, session_id
;
810 uint16_t server_version
, cipher_suite
;
811 uint16_t min_version
, max_version
;
812 uint8_t compression_method
;
813 STACK_OF(SSL_CIPHER
) *sk
;
814 const SSL_CIPHER
*cipher
;
815 const SSL_METHOD
*method
;
821 s
->internal
->first_packet
= 1;
822 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_HELLO_A
,
823 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, /* ?? */ &ok
);
826 s
->internal
->first_packet
= 0;
831 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
833 if (SSL_IS_DTLS(s
)) {
834 if (S3I(s
)->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
835 if (D1I(s
)->send_cookie
== 0) {
836 S3I(s
)->tmp
.reuse_message
= 1;
839 /* Already sent a cookie. */
840 al
= SSL_AD_UNEXPECTED_MESSAGE
;
841 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
847 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
848 al
= SSL_AD_UNEXPECTED_MESSAGE
;
849 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
853 if (!CBS_get_u16(&cbs
, &server_version
))
856 if (ssl_supported_version_range(s
, &min_version
, &max_version
) != 1) {
857 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
861 if (server_version
< min_version
|| server_version
> max_version
) {
862 SSLerror(s
, SSL_R_WRONG_SSL_VERSION
);
863 s
->version
= (s
->version
& 0xff00) | (server_version
& 0xff);
864 al
= SSL_AD_PROTOCOL_VERSION
;
867 s
->version
= server_version
;
869 if ((method
= tls1_get_client_method(server_version
)) == NULL
)
870 method
= dtls1_get_client_method(server_version
);
871 if (method
== NULL
) {
872 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
878 if (!CBS_get_bytes(&cbs
, &server_random
, SSL3_RANDOM_SIZE
))
880 if (!CBS_write_bytes(&server_random
, s
->s3
->server_random
,
881 sizeof(s
->s3
->server_random
), NULL
))
885 if (!CBS_get_u8_length_prefixed(&cbs
, &session_id
))
888 if ((CBS_len(&session_id
) > sizeof(s
->session
->session_id
)) ||
889 (CBS_len(&session_id
) > SSL3_SESSION_ID_SIZE
)) {
890 al
= SSL_AD_ILLEGAL_PARAMETER
;
891 SSLerror(s
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
896 if (!CBS_get_u16(&cbs
, &cipher_suite
))
900 * Check if we want to resume the session based on external
903 if (s
->internal
->tls_session_secret_cb
) {
904 SSL_CIPHER
*pref_cipher
= NULL
;
905 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
906 if (s
->internal
->tls_session_secret_cb(s
, s
->session
->master_key
,
907 &s
->session
->master_key_length
, NULL
, &pref_cipher
,
908 s
->internal
->tls_session_secret_cb_arg
)) {
909 s
->session
->cipher
= pref_cipher
? pref_cipher
:
910 ssl3_get_cipher_by_value(cipher_suite
);
911 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
915 if (s
->session
->session_id_length
!= 0 &&
916 CBS_mem_equal(&session_id
, s
->session
->session_id
,
917 s
->session
->session_id_length
)) {
918 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
||
919 timingsafe_memcmp(s
->session
->sid_ctx
,
920 s
->sid_ctx
, s
->sid_ctx_length
) != 0) {
921 /* actually a client application bug */
922 al
= SSL_AD_ILLEGAL_PARAMETER
;
923 SSLerror(s
, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
926 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
927 s
->internal
->hit
= 1;
929 /* a miss or crap from the other end */
931 /* If we were trying for session-id reuse, make a new
932 * SSL_SESSION so we don't stuff up other people */
933 s
->internal
->hit
= 0;
934 if (s
->session
->session_id_length
> 0) {
935 if (!ssl_get_new_session(s
, 0)) {
936 al
= SSL_AD_INTERNAL_ERROR
;
942 * XXX - improve the handling for the case where there is a
943 * zero length session identifier.
945 if (!CBS_write_bytes(&session_id
, s
->session
->session_id
,
946 sizeof(s
->session
->session_id
), &outlen
))
948 s
->session
->session_id_length
= outlen
;
950 s
->session
->ssl_version
= s
->version
;
953 if ((cipher
= ssl3_get_cipher_by_value(cipher_suite
)) == NULL
) {
954 al
= SSL_AD_ILLEGAL_PARAMETER
;
955 SSLerror(s
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
959 /* TLS v1.2 only ciphersuites require v1.2 or later. */
960 if ((cipher
->algorithm_ssl
& SSL_TLSV1_2
) &&
961 (TLS1_get_version(s
) < TLS1_2_VERSION
)) {
962 al
= SSL_AD_ILLEGAL_PARAMETER
;
963 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
967 sk
= ssl_get_ciphers_by_id(s
);
968 i
= sk_SSL_CIPHER_find(sk
, cipher
);
970 /* we did not say we would use this cipher */
971 al
= SSL_AD_ILLEGAL_PARAMETER
;
972 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
977 * Depending on the session caching (internal/external), the cipher
978 * and/or cipher_id values may not be set. Make sure that
979 * cipher_id is set and use it for comparison.
981 if (s
->session
->cipher
)
982 s
->session
->cipher_id
= s
->session
->cipher
->id
;
983 if (s
->internal
->hit
&& (s
->session
->cipher_id
!= cipher
->id
)) {
984 al
= SSL_AD_ILLEGAL_PARAMETER
;
985 SSLerror(s
, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
988 S3I(s
)->hs
.new_cipher
= cipher
;
990 if (!tls1_handshake_hash_init(s
))
994 * Don't digest cached records if no sigalgs: we may need them for
995 * client authentication.
997 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
998 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) &&
999 !tls1_digest_cached_records(s
)) {
1000 al
= SSL_AD_INTERNAL_ERROR
;
1004 if (!CBS_get_u8(&cbs
, &compression_method
))
1007 if (compression_method
!= 0) {
1008 al
= SSL_AD_ILLEGAL_PARAMETER
;
1009 SSLerror(s
, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1013 if (!tlsext_serverhello_parse(s
, &cbs
, &al
)) {
1014 SSLerror(s
, SSL_R_PARSE_TLSEXT
);
1019 * Determine if we need to see RI. Strictly speaking if we want to
1020 * avoid an attack we should *always* see RI even on initial server
1021 * hello because the client doesn't see any renegotiation during an
1022 * attack. However this would mean we could not connect to any server
1023 * which doesn't support RI so for the immediate future tolerate RI
1024 * absence on initial connect only.
1026 if (!S3I(s
)->renegotiate_seen
&&
1027 !(s
->internal
->options
& SSL_OP_LEGACY_SERVER_CONNECT
)) {
1028 al
= SSL_AD_HANDSHAKE_FAILURE
;
1029 SSLerror(s
, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
1033 if (ssl_check_serverhello_tlsext(s
) <= 0) {
1034 SSLerror(s
, SSL_R_SERVERHELLO_TLSEXT
);
1041 /* wrong packet length */
1042 al
= SSL_AD_DECODE_ERROR
;
1043 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1045 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1051 ssl3_get_server_certificate(SSL
*s
)
1053 int al
, i
, ok
, ret
= -1;
1057 const unsigned char *q
;
1058 STACK_OF(X509
) *sk
= NULL
;
1060 EVP_PKEY
*pkey
= NULL
;
1062 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
1063 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
1068 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
1069 S3I(s
)->tmp
.reuse_message
= 1;
1073 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
1074 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1075 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
1080 if ((sk
= sk_X509_new_null()) == NULL
) {
1081 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1088 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1089 if (CBS_len(&cbs
) < 3)
1092 if (!CBS_get_u24_length_prefixed(&cbs
, &cert_list
) ||
1093 CBS_len(&cbs
) != 0) {
1094 al
= SSL_AD_DECODE_ERROR
;
1095 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1099 while (CBS_len(&cert_list
) > 0) {
1102 if (CBS_len(&cert_list
) < 3)
1104 if (!CBS_get_u24_length_prefixed(&cert_list
, &cert
)) {
1105 al
= SSL_AD_DECODE_ERROR
;
1106 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1110 q
= CBS_data(&cert
);
1111 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
1113 al
= SSL_AD_BAD_CERTIFICATE
;
1114 SSLerror(s
, ERR_R_ASN1_LIB
);
1117 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
1118 al
= SSL_AD_DECODE_ERROR
;
1119 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1122 if (!sk_X509_push(sk
, x
)) {
1123 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1129 i
= ssl_verify_cert_chain(s
, sk
);
1130 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)) {
1131 al
= ssl_verify_alarm_type(s
->verify_result
);
1132 SSLerror(s
, SSL_R_CERTIFICATE_VERIFY_FAILED
);
1136 ERR_clear_error(); /* but we keep s->verify_result */
1138 sc
= ssl_sess_cert_new();
1141 ssl_sess_cert_free(SSI(s
)->sess_cert
);
1142 SSI(s
)->sess_cert
= sc
;
1144 sc
->cert_chain
= sk
;
1146 * Inconsistency alert: cert_chain does include the peer's
1147 * certificate, which we don't include in s3_srvr.c
1149 x
= sk_X509_value(sk
, 0);
1151 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1153 pkey
= X509_get_pubkey(x
);
1155 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1158 SSLerror(s
, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1162 i
= ssl_cert_type(x
, pkey
);
1166 SSLerror(s
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1170 sc
->peer_cert_type
= i
;
1171 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1173 * Why would the following ever happen?
1174 * We just created sc a couple of lines ago.
1176 X509_free(sc
->peer_pkeys
[i
].x509
);
1177 sc
->peer_pkeys
[i
].x509
= x
;
1178 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1180 X509_free(s
->session
->peer
);
1181 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1182 s
->session
->peer
= x
;
1183 s
->session
->verify_result
= s
->verify_result
;
1190 /* wrong packet length */
1191 al
= SSL_AD_DECODE_ERROR
;
1192 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1194 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1197 EVP_PKEY_free(pkey
);
1199 sk_X509_pop_free(sk
, X509_free
);
1205 ssl3_get_server_kex_dhe(SSL
*s
, EVP_PKEY
**pkey
, unsigned char **pp
, long *nn
)
1207 CBS cbs
, dhp
, dhg
, dhpk
;
1208 BN_CTX
*bn_ctx
= NULL
;
1209 SESS_CERT
*sc
= NULL
;
1214 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1215 sc
= SSI(s
)->sess_cert
;
1220 CBS_init(&cbs
, *pp
, *nn
);
1222 if ((dh
= DH_new()) == NULL
) {
1223 SSLerror(s
, ERR_R_DH_LIB
);
1227 if (!CBS_get_u16_length_prefixed(&cbs
, &dhp
))
1229 if ((dh
->p
= BN_bin2bn(CBS_data(&dhp
), CBS_len(&dhp
), NULL
)) == NULL
) {
1230 SSLerror(s
, ERR_R_BN_LIB
);
1234 if (!CBS_get_u16_length_prefixed(&cbs
, &dhg
))
1236 if ((dh
->g
= BN_bin2bn(CBS_data(&dhg
), CBS_len(&dhg
), NULL
)) == NULL
) {
1237 SSLerror(s
, ERR_R_BN_LIB
);
1241 if (!CBS_get_u16_length_prefixed(&cbs
, &dhpk
))
1243 if ((dh
->pub_key
= BN_bin2bn(CBS_data(&dhpk
), CBS_len(&dhpk
),
1245 SSLerror(s
, ERR_R_BN_LIB
);
1250 * Check the strength of the DH key just constructed.
1251 * Discard keys weaker than 1024 bits.
1253 if (DH_size(dh
) < 1024 / 8) {
1254 SSLerror(s
, SSL_R_BAD_DH_P_LENGTH
);
1258 if (alg_a
& SSL_aRSA
)
1259 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1261 /* XXX - Anonymous DH, so no certificate or pkey. */
1264 sc
->peer_dh_tmp
= dh
;
1266 *nn
= CBS_len(&cbs
);
1267 *pp
= (unsigned char *)CBS_data(&cbs
);
1272 al
= SSL_AD_DECODE_ERROR
;
1273 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1274 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1278 BN_CTX_free(bn_ctx
);
1284 ssl3_get_server_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1286 const EC_GROUP
*group
;
1287 EC_GROUP
*ngroup
= NULL
;
1288 EC_POINT
*point
= NULL
;
1289 BN_CTX
*bn_ctx
= NULL
;
1290 EC_KEY
*ecdh
= NULL
;
1294 * Extract the server's ephemeral ECDH public key.
1297 if ((ecdh
= EC_KEY_new()) == NULL
) {
1298 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1302 if ((ngroup
= EC_GROUP_new_by_curve_name(nid
)) == NULL
) {
1303 SSLerror(s
, ERR_R_EC_LIB
);
1306 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1307 SSLerror(s
, ERR_R_EC_LIB
);
1311 group
= EC_KEY_get0_group(ecdh
);
1313 if ((point
= EC_POINT_new(group
)) == NULL
||
1314 (bn_ctx
= BN_CTX_new()) == NULL
) {
1315 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1319 if (EC_POINT_oct2point(group
, point
, CBS_data(public),
1320 CBS_len(public), bn_ctx
) == 0) {
1321 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1322 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1326 EC_KEY_set_public_key(ecdh
, point
);
1327 sc
->peer_ecdh_tmp
= ecdh
;
1333 BN_CTX_free(bn_ctx
);
1334 EC_GROUP_free(ngroup
);
1335 EC_POINT_free(point
);
1342 ssl3_get_server_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1346 if (nid
!= NID_X25519
) {
1347 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1351 if (CBS_len(public) != X25519_KEY_LENGTH
) {
1352 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1353 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1357 if (!CBS_stow(public, &sc
->peer_x25519_tmp
, &outlen
)) {
1358 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1369 ssl3_get_server_kex_ecdhe(SSL
*s
, EVP_PKEY
**pkey
, unsigned char **pp
, long *nn
)
1379 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1380 sc
= SSI(s
)->sess_cert
;
1385 CBS_init(&cbs
, *pp
, *nn
);
1387 /* Only named curves are supported. */
1388 if (!CBS_get_u8(&cbs
, &curve_type
) ||
1389 curve_type
!= NAMED_CURVE_TYPE
||
1390 !CBS_get_u16(&cbs
, &curve_id
)) {
1391 al
= SSL_AD_DECODE_ERROR
;
1392 SSLerror(s
, SSL_R_LENGTH_TOO_SHORT
);
1397 * Check that the curve is one of our preferences - if it is not,
1398 * the server has sent us an invalid curve.
1400 if (tls1_check_curve(s
, curve_id
) != 1) {
1401 al
= SSL_AD_DECODE_ERROR
;
1402 SSLerror(s
, SSL_R_WRONG_CURVE
);
1406 if ((nid
= tls1_ec_curve_id2nid(curve_id
)) == 0) {
1407 al
= SSL_AD_INTERNAL_ERROR
;
1408 SSLerror(s
, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1412 if (!CBS_get_u8_length_prefixed(&cbs
, &public))
1415 if (nid
== NID_X25519
) {
1416 if (ssl3_get_server_kex_ecdhe_ecx(s
, sc
, nid
, &public) != 1)
1419 if (ssl3_get_server_kex_ecdhe_ecp(s
, sc
, nid
, &public) != 1)
1424 * The ECC/TLS specification does not mention the use of DSA to sign
1425 * ECParameters in the server key exchange message. We do support RSA
1428 if (alg_a
& SSL_aRSA
)
1429 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1430 else if (alg_a
& SSL_aECDSA
)
1431 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1433 /* XXX - Anonymous ECDH, so no certificate or pkey. */
1436 *nn
= CBS_len(&cbs
);
1437 *pp
= (unsigned char *)CBS_data(&cbs
);
1442 al
= SSL_AD_DECODE_ERROR
;
1443 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1446 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1453 ssl3_get_server_key_exchange(SSL
*s
)
1455 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
*2];
1457 unsigned char *param
, *p
;
1458 int al
, i
, j
, param_len
, ok
;
1459 long n
, alg_k
, alg_a
;
1460 EVP_PKEY
*pkey
= NULL
;
1461 const EVP_MD
*md
= NULL
;
1464 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
1465 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1468 * Use same message size as in ssl3_get_certificate_request()
1469 * as ServerKeyExchange message may be skipped.
1471 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_KEY_EXCH_A
,
1472 SSL3_ST_CR_KEY_EXCH_B
, -1, s
->internal
->max_cert_list
, &ok
);
1476 EVP_MD_CTX_init(&md_ctx
);
1478 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1480 * Do not skip server key exchange if this cipher suite uses
1483 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
1484 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1485 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1489 S3I(s
)->tmp
.reuse_message
= 1;
1490 EVP_MD_CTX_cleanup(&md_ctx
);
1494 if (SSI(s
)->sess_cert
!= NULL
) {
1495 DH_free(SSI(s
)->sess_cert
->peer_dh_tmp
);
1496 SSI(s
)->sess_cert
->peer_dh_tmp
= NULL
;
1498 EC_KEY_free(SSI(s
)->sess_cert
->peer_ecdh_tmp
);
1499 SSI(s
)->sess_cert
->peer_ecdh_tmp
= NULL
;
1501 free(SSI(s
)->sess_cert
->peer_x25519_tmp
);
1502 SSI(s
)->sess_cert
->peer_x25519_tmp
= NULL
;
1504 SSI(s
)->sess_cert
= ssl_sess_cert_new();
1505 if (SSI(s
)->sess_cert
== NULL
)
1509 param
= p
= (unsigned char *)s
->internal
->init_msg
;
1512 if (alg_k
& SSL_kDHE
) {
1513 if (ssl3_get_server_kex_dhe(s
, &pkey
, &p
, &n
) != 1)
1515 } else if (alg_k
& SSL_kECDHE
) {
1516 if (ssl3_get_server_kex_ecdhe(s
, &pkey
, &p
, &n
) != 1)
1518 } else if (alg_k
!= 0) {
1519 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1520 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1524 param_len
= param_len
- n
;
1526 /* if it was signed, check the signature */
1528 if (SSL_USE_SIGALGS(s
)) {
1529 int sigalg
= tls12_get_sigid(pkey
);
1530 /* Should never happen */
1532 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1536 * Check key type is consistent
1541 if (sigalg
!= (int)p
[1]) {
1542 SSLerror(s
, SSL_R_WRONG_SIGNATURE_TYPE
);
1543 al
= SSL_AD_DECODE_ERROR
;
1546 md
= tls12_get_hash(p
[0]);
1548 SSLerror(s
, SSL_R_UNKNOWN_DIGEST
);
1549 al
= SSL_AD_DECODE_ERROR
;
1561 j
= EVP_PKEY_size(pkey
);
1563 if (i
!= n
|| n
> j
) {
1564 /* wrong packet length */
1565 al
= SSL_AD_DECODE_ERROR
;
1566 SSLerror(s
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1570 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1573 if (!EVP_DigestInit_ex(&md_ctx
, EVP_md5_sha1(), NULL
)) {
1574 al
= SSL_AD_INTERNAL_ERROR
;
1577 EVP_DigestUpdate(&md_ctx
, s
->s3
->client_random
,
1579 EVP_DigestUpdate(&md_ctx
, s
->s3
->server_random
,
1581 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1582 EVP_DigestFinal_ex(&md_ctx
, q
, (unsigned int *)&i
);
1585 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
,
1586 p
, n
, pkey
->pkey
.rsa
);
1588 al
= SSL_AD_DECRYPT_ERROR
;
1589 SSLerror(s
, SSL_R_BAD_RSA_DECRYPT
);
1594 al
= SSL_AD_DECRYPT_ERROR
;
1595 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1599 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
1600 EVP_VerifyUpdate(&md_ctx
, s
->s3
->client_random
,
1602 EVP_VerifyUpdate(&md_ctx
, s
->s3
->server_random
,
1604 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1605 if (EVP_VerifyFinal(&md_ctx
, p
,(int)n
, pkey
) <= 0) {
1607 al
= SSL_AD_DECRYPT_ERROR
;
1608 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1613 /* aNULL does not need public keys. */
1614 if (!(alg_a
& SSL_aNULL
)) {
1615 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1618 /* still data left over */
1620 al
= SSL_AD_DECODE_ERROR
;
1621 SSLerror(s
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1626 EVP_PKEY_free(pkey
);
1627 EVP_MD_CTX_cleanup(&md_ctx
);
1632 /* wrong packet length */
1633 al
= SSL_AD_DECODE_ERROR
;
1634 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1637 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1640 EVP_PKEY_free(pkey
);
1642 EVP_MD_CTX_cleanup(&md_ctx
);
1648 ssl3_get_certificate_request(SSL
*s
)
1653 CBS cert_request
, ctypes
, rdn_list
;
1654 X509_NAME
*xn
= NULL
;
1655 const unsigned char *q
;
1656 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1658 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_REQ_A
,
1659 SSL3_ST_CR_CERT_REQ_B
, -1, s
->internal
->max_cert_list
, &ok
);
1664 S3I(s
)->tmp
.cert_req
= 0;
1666 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1667 S3I(s
)->tmp
.reuse_message
= 1;
1669 * If we get here we don't need any cached handshake records
1670 * as we wont be doing client auth.
1672 if (S3I(s
)->handshake_buffer
) {
1673 if (!tls1_digest_cached_records(s
))
1679 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1680 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1681 SSLerror(s
, SSL_R_WRONG_MESSAGE_TYPE
);
1685 /* TLS does not like anon-DH with client cert */
1686 if (S3I(s
)->hs
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
1687 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1688 SSLerror(s
, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1694 CBS_init(&cert_request
, s
->internal
->init_msg
, n
);
1696 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1697 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1701 /* get the certificate types */
1702 if (!CBS_get_u8(&cert_request
, &ctype_num
))
1705 if (ctype_num
> SSL3_CT_NUMBER
)
1706 ctype_num
= SSL3_CT_NUMBER
;
1707 if (!CBS_get_bytes(&cert_request
, &ctypes
, ctype_num
) ||
1708 !CBS_write_bytes(&ctypes
, (uint8_t *)S3I(s
)->tmp
.ctype
,
1709 sizeof(S3I(s
)->tmp
.ctype
), NULL
)) {
1710 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1714 if (SSL_USE_SIGALGS(s
)) {
1717 if (CBS_len(&cert_request
) < 2) {
1718 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1722 /* Check we have enough room for signature algorithms and
1723 * following length value.
1725 if (!CBS_get_u16_length_prefixed(&cert_request
, &sigalgs
)) {
1726 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1727 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1730 if (!tls1_process_sigalgs(s
, &sigalgs
)) {
1731 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1732 SSLerror(s
, SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
1737 /* get the CA RDNs */
1738 if (CBS_len(&cert_request
) < 2) {
1739 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1743 if (!CBS_get_u16_length_prefixed(&cert_request
, &rdn_list
) ||
1744 CBS_len(&cert_request
) != 0) {
1745 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1746 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1750 while (CBS_len(&rdn_list
) > 0) {
1753 if (CBS_len(&rdn_list
) < 2) {
1754 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1758 if (!CBS_get_u16_length_prefixed(&rdn_list
, &rdn
)) {
1759 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1760 SSLerror(s
, SSL_R_CA_DN_TOO_LONG
);
1765 if ((xn
= d2i_X509_NAME(NULL
, &q
, CBS_len(&rdn
))) == NULL
) {
1766 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1767 SSL_AD_DECODE_ERROR
);
1768 SSLerror(s
, ERR_R_ASN1_LIB
);
1772 if (q
!= CBS_data(&rdn
) + CBS_len(&rdn
)) {
1773 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1774 SSLerror(s
, SSL_R_CA_DN_LENGTH_MISMATCH
);
1777 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1778 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1781 xn
= NULL
; /* avoid free in err block */
1784 /* we should setup a certificate to return.... */
1785 S3I(s
)->tmp
.cert_req
= 1;
1786 S3I(s
)->tmp
.ctype_num
= ctype_num
;
1787 sk_X509_NAME_pop_free(S3I(s
)->tmp
.ca_names
, X509_NAME_free
);
1788 S3I(s
)->tmp
.ca_names
= ca_sk
;
1794 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1798 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1803 ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1805 return (X509_NAME_cmp(*a
, *b
));
1809 ssl3_get_new_session_ticket(SSL
*s
)
1811 int ok
, al
, ret
= 0;
1812 uint32_t lifetime_hint
;
1814 CBS cbs
, session_ticket
;
1816 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SESSION_TICKET_A
,
1817 SSL3_ST_CR_SESSION_TICKET_B
, -1, 16384, &ok
);
1821 if (S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) {
1822 S3I(s
)->tmp
.reuse_message
= 1;
1825 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
) {
1826 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1827 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
1832 al
= SSL_AD_DECODE_ERROR
;
1833 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1837 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1838 if (!CBS_get_u32(&cbs
, &lifetime_hint
) ||
1839 #if UINT32_MAX > LONG_MAX
1840 lifetime_hint
> LONG_MAX
||
1842 !CBS_get_u16_length_prefixed(&cbs
, &session_ticket
) ||
1843 CBS_len(&cbs
) != 0) {
1844 al
= SSL_AD_DECODE_ERROR
;
1845 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1848 s
->session
->tlsext_tick_lifetime_hint
= (long)lifetime_hint
;
1850 if (!CBS_stow(&session_ticket
, &s
->session
->tlsext_tick
,
1851 &s
->session
->tlsext_ticklen
)) {
1852 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1857 * There are two ways to detect a resumed ticket sesion.
1858 * One is to set an appropriate session ID and then the server
1859 * must return a match in ServerHello. This allows the normal
1860 * client session ID matching to work and we know much
1861 * earlier that the ticket has been accepted.
1863 * The other way is to set zero length session ID when the
1864 * ticket is presented and rely on the handshake to determine
1865 * session resumption.
1867 * We choose the former approach because this fits in with
1868 * assumptions elsewhere in OpenSSL. The session ID is set
1869 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1872 EVP_Digest(CBS_data(&session_ticket
), CBS_len(&session_ticket
),
1873 s
->session
->session_id
, &s
->session
->session_id_length
,
1874 EVP_sha256(), NULL
);
1878 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1884 ssl3_get_cert_status(SSL
*s
)
1886 CBS cert_status
, response
;
1890 uint8_t status_type
;
1892 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_STATUS_A
,
1893 SSL3_ST_CR_CERT_STATUS_B
, SSL3_MT_CERTIFICATE_STATUS
,
1900 /* need at least status type + length */
1901 al
= SSL_AD_DECODE_ERROR
;
1902 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1906 CBS_init(&cert_status
, s
->internal
->init_msg
, n
);
1907 if (!CBS_get_u8(&cert_status
, &status_type
) ||
1908 CBS_len(&cert_status
) < 3) {
1909 /* need at least status type + length */
1910 al
= SSL_AD_DECODE_ERROR
;
1911 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1915 if (status_type
!= TLSEXT_STATUSTYPE_ocsp
) {
1916 al
= SSL_AD_DECODE_ERROR
;
1917 SSLerror(s
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
1921 if (!CBS_get_u24_length_prefixed(&cert_status
, &response
) ||
1922 CBS_len(&cert_status
) != 0) {
1923 al
= SSL_AD_DECODE_ERROR
;
1924 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1928 if (!CBS_stow(&response
, &s
->internal
->tlsext_ocsp_resp
,
1929 &stow_len
) || stow_len
> INT_MAX
) {
1930 s
->internal
->tlsext_ocsp_resplen
= 0;
1931 al
= SSL_AD_INTERNAL_ERROR
;
1932 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1935 s
->internal
->tlsext_ocsp_resplen
= (int)stow_len
;
1937 if (s
->ctx
->internal
->tlsext_status_cb
) {
1939 ret
= s
->ctx
->internal
->tlsext_status_cb(s
,
1940 s
->ctx
->internal
->tlsext_status_arg
);
1942 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
1943 SSLerror(s
, SSL_R_INVALID_STATUS_RESPONSE
);
1947 al
= SSL_AD_INTERNAL_ERROR
;
1948 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1954 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1959 ssl3_get_server_done(SSL
*s
)
1964 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_DONE_A
,
1965 SSL3_ST_CR_SRVR_DONE_B
, SSL3_MT_SERVER_DONE
,
1966 30, /* should be very small, like 0 :-) */ &ok
);
1971 /* should contain no data */
1972 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1973 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1981 ssl3_send_client_kex_rsa(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1983 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
];
1984 unsigned char *enc_pms
= NULL
;
1985 EVP_PKEY
*pkey
= NULL
;
1991 * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1.
1994 pkey
= X509_get_pubkey(sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1995 if (pkey
== NULL
|| pkey
->type
!= EVP_PKEY_RSA
||
1996 pkey
->pkey
.rsa
== NULL
) {
1997 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2001 pms
[0] = s
->client_version
>> 8;
2002 pms
[1] = s
->client_version
& 0xff;
2003 arc4random_buf(&pms
[2], sizeof(pms
) - 2);
2005 if ((enc_pms
= malloc(RSA_size(pkey
->pkey
.rsa
))) == NULL
) {
2006 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2010 enc_len
= RSA_public_encrypt(sizeof(pms
), pms
, enc_pms
, pkey
->pkey
.rsa
,
2013 SSLerror(s
, SSL_R_BAD_RSA_ENCRYPT
);
2017 if (!CBB_add_u16_length_prefixed(cbb
, &epms
))
2019 if (!CBB_add_bytes(&epms
, enc_pms
, enc_len
))
2021 if (!CBB_flush(cbb
))
2024 s
->session
->master_key_length
=
2025 tls1_generate_master_secret(s
,
2026 s
->session
->master_key
, pms
, sizeof(pms
));
2031 explicit_bzero(pms
, sizeof(pms
));
2032 EVP_PKEY_free(pkey
);
2039 ssl3_send_client_kex_dhe(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
2041 DH
*dh_srvr
= NULL
, *dh_clnt
= NULL
;
2042 unsigned char *key
= NULL
;
2043 int key_size
= 0, key_len
;
2044 unsigned char *data
;
2048 /* Ensure that we have an ephemeral key for DHE. */
2049 if (sess_cert
->peer_dh_tmp
== NULL
) {
2050 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2051 SSLerror(s
, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
2054 dh_srvr
= sess_cert
->peer_dh_tmp
;
2056 /* Generate a new random key. */
2057 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
2058 SSLerror(s
, ERR_R_DH_LIB
);
2061 if (!DH_generate_key(dh_clnt
)) {
2062 SSLerror(s
, ERR_R_DH_LIB
);
2065 key_size
= DH_size(dh_clnt
);
2066 if ((key
= malloc(key_size
)) == NULL
) {
2067 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2070 key_len
= DH_compute_key(key
, dh_srvr
->pub_key
, dh_clnt
);
2072 SSLerror(s
, ERR_R_DH_LIB
);
2076 /* Generate master key from the result. */
2077 s
->session
->master_key_length
=
2078 tls1_generate_master_secret(s
,
2079 s
->session
->master_key
, key
, key_len
);
2081 if (!CBB_add_u16_length_prefixed(cbb
, &dh_Yc
))
2083 if (!CBB_add_space(&dh_Yc
, &data
, BN_num_bytes(dh_clnt
->pub_key
)))
2085 BN_bn2bin(dh_clnt
->pub_key
, data
);
2086 if (!CBB_flush(cbb
))
2093 freezero(key
, key_size
);
2099 ssl3_send_client_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2101 const EC_GROUP
*group
= NULL
;
2102 const EC_POINT
*point
= NULL
;
2103 EC_KEY
*ecdh
= NULL
;
2104 BN_CTX
*bn_ctx
= NULL
;
2105 unsigned char *key
= NULL
;
2106 unsigned char *data
;
2108 int key_size
= 0, key_len
;
2112 if ((group
= EC_KEY_get0_group(sc
->peer_ecdh_tmp
)) == NULL
||
2113 (point
= EC_KEY_get0_public_key(sc
->peer_ecdh_tmp
)) == NULL
) {
2114 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2118 if ((ecdh
= EC_KEY_new()) == NULL
) {
2119 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2123 if (!EC_KEY_set_group(ecdh
, group
)) {
2124 SSLerror(s
, ERR_R_EC_LIB
);
2128 /* Generate a new ECDH key pair. */
2129 if (!(EC_KEY_generate_key(ecdh
))) {
2130 SSLerror(s
, ERR_R_ECDH_LIB
);
2133 if ((key_size
= ECDH_size(ecdh
)) <= 0) {
2134 SSLerror(s
, ERR_R_ECDH_LIB
);
2137 if ((key
= malloc(key_size
)) == NULL
) {
2138 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2140 key_len
= ECDH_compute_key(key
, key_size
, point
, ecdh
, NULL
);
2142 SSLerror(s
, ERR_R_ECDH_LIB
);
2146 /* Generate master key from the result. */
2147 s
->session
->master_key_length
=
2148 tls1_generate_master_secret(s
,
2149 s
->session
->master_key
, key
, key_len
);
2151 encoded_len
= EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2152 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
2153 if (encoded_len
== 0) {
2154 SSLerror(s
, ERR_R_ECDH_LIB
);
2158 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
2159 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2163 /* Encode the public key. */
2164 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2166 if (!CBB_add_space(&ecpoint
, &data
, encoded_len
))
2168 if (EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2169 POINT_CONVERSION_UNCOMPRESSED
, data
, encoded_len
,
2172 if (!CBB_flush(cbb
))
2178 freezero(key
, key_size
);
2180 BN_CTX_free(bn_ctx
);
2187 ssl3_send_client_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2189 uint8_t *public_key
= NULL
, *private_key
= NULL
, *shared_key
= NULL
;
2193 /* Generate X25519 key pair and derive shared key. */
2194 if ((public_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2196 if ((private_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2198 if ((shared_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2200 X25519_keypair(public_key
, private_key
);
2201 if (!X25519(shared_key
, private_key
, sc
->peer_x25519_tmp
))
2204 /* Serialize the public key. */
2205 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2207 if (!CBB_add_bytes(&ecpoint
, public_key
, X25519_KEY_LENGTH
))
2209 if (!CBB_flush(cbb
))
2212 /* Generate master key from the result. */
2213 s
->session
->master_key_length
=
2214 tls1_generate_master_secret(s
,
2215 s
->session
->master_key
, shared_key
, X25519_KEY_LENGTH
);
2221 freezero(private_key
, X25519_KEY_LENGTH
);
2222 freezero(shared_key
, X25519_KEY_LENGTH
);
2228 ssl3_send_client_kex_ecdhe(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2230 if (sc
->peer_x25519_tmp
!= NULL
) {
2231 if (ssl3_send_client_kex_ecdhe_ecx(s
, sc
, cbb
) != 1)
2233 } else if (sc
->peer_ecdh_tmp
!= NULL
) {
2234 if (ssl3_send_client_kex_ecdhe_ecp(s
, sc
, cbb
) != 1)
2237 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2238 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2249 ssl3_send_client_kex_gost(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
2251 unsigned char premaster_secret
[32], shared_ukm
[32], tmp
[256];
2252 EVP_PKEY
*pub_key
= NULL
;
2253 EVP_PKEY_CTX
*pkey_ctx
;
2256 unsigned int md_len
;
2257 EVP_MD_CTX
*ukm_hash
;
2262 /* Get server sertificate PKEY and create ctx from it */
2263 peer_cert
= sess_cert
->peer_pkeys
[SSL_PKEY_GOST01
].x509
;
2264 if (peer_cert
== NULL
) {
2265 SSLerror(s
, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2269 pub_key
= X509_get_pubkey(peer_cert
);
2270 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
, NULL
);
2273 * If we have send a certificate, and certificate key parameters match
2274 * those of server certificate, use certificate key for key exchange.
2275 * Otherwise, generate ephemeral key pair.
2277 EVP_PKEY_encrypt_init(pkey_ctx
);
2279 /* Generate session key. */
2280 arc4random_buf(premaster_secret
, 32);
2283 * If we have client certificate, use its secret as peer key.
2285 if (S3I(s
)->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2286 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2287 s
->cert
->key
->privatekey
) <=0) {
2289 * If there was an error - just ignore it.
2290 * Ephemeral key would be used.
2297 * Compute shared IV and store it in algorithm-specific context data.
2299 ukm_hash
= EVP_MD_CTX_create();
2300 if (ukm_hash
== NULL
) {
2301 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2305 if (ssl_get_algorithm2(s
) & SSL_HANDSHAKE_MAC_GOST94
)
2306 nid
= NID_id_GostR3411_94
;
2308 nid
= NID_id_tc26_gost3411_2012_256
;
2309 if (!EVP_DigestInit(ukm_hash
, EVP_get_digestbynid(nid
)))
2311 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2312 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2313 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2314 EVP_MD_CTX_destroy(ukm_hash
);
2315 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
2316 EVP_PKEY_CTRL_SET_IV
, 8, shared_ukm
) < 0) {
2317 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2322 * Make GOST keytransport blob message, encapsulate it into sequence.
2325 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, premaster_secret
,
2327 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2331 if (!CBB_add_asn1(cbb
, &gostblob
, CBS_ASN1_SEQUENCE
))
2333 if (!CBB_add_bytes(&gostblob
, tmp
, msglen
))
2335 if (!CBB_flush(cbb
))
2338 /* Check if pubkey from client certificate was used. */
2339 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
2341 /* Set flag "skip certificate verify". */
2342 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2344 EVP_PKEY_CTX_free(pkey_ctx
);
2345 s
->session
->master_key_length
=
2346 tls1_generate_master_secret(s
,
2347 s
->session
->master_key
, premaster_secret
, 32);
2352 explicit_bzero(premaster_secret
, sizeof(premaster_secret
));
2353 EVP_PKEY_free(pub_key
);
2359 ssl3_send_client_key_exchange(SSL
*s
)
2361 SESS_CERT
*sess_cert
;
2362 unsigned long alg_k
;
2365 memset(&cbb
, 0, sizeof(cbb
));
2367 if (S3I(s
)->hs
.state
== SSL3_ST_CW_KEY_EXCH_A
) {
2368 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2370 if ((sess_cert
= SSI(s
)->sess_cert
) == NULL
) {
2371 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2372 SSL_AD_UNEXPECTED_MESSAGE
);
2373 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2377 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &kex
,
2378 SSL3_MT_CLIENT_KEY_EXCHANGE
))
2381 if (alg_k
& SSL_kRSA
) {
2382 if (ssl3_send_client_kex_rsa(s
, sess_cert
, &kex
) != 1)
2384 } else if (alg_k
& SSL_kDHE
) {
2385 if (ssl3_send_client_kex_dhe(s
, sess_cert
, &kex
) != 1)
2387 } else if (alg_k
& SSL_kECDHE
) {
2388 if (ssl3_send_client_kex_ecdhe(s
, sess_cert
, &kex
) != 1)
2390 } else if (alg_k
& SSL_kGOST
) {
2391 if (ssl3_send_client_kex_gost(s
, sess_cert
, &kex
) != 1)
2394 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2395 SSL_AD_HANDSHAKE_FAILURE
);
2396 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2400 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2403 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_B
;
2406 /* SSL3_ST_CW_KEY_EXCH_B */
2407 return (ssl3_handshake_write(s
));
2416 ssl3_send_client_verify(SSL
*s
)
2419 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
2421 EVP_PKEY_CTX
*pctx
= NULL
;
2427 EVP_MD_CTX_init(&mctx
);
2429 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_VRFY_A
) {
2430 p
= ssl3_handshake_msg_start(s
, SSL3_MT_CERTIFICATE_VERIFY
);
2433 * Create context from key and test if sha1 is allowed as
2436 pkey
= s
->cert
->key
->privatekey
;
2437 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2438 EVP_PKEY_sign_init(pctx
);
2440 /* XXX - is this needed? */
2441 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) <= 0)
2444 if (!SSL_USE_SIGALGS(s
)) {
2445 if (S3I(s
)->handshake_buffer
) {
2446 if (!tls1_digest_cached_records(s
))
2449 if (!tls1_handshake_hash_value(s
, data
, sizeof(data
),
2455 * For TLS v1.2 send signature algorithm and signature
2456 * using agreed digest and cached handshake records.
2458 if (SSL_USE_SIGALGS(s
)) {
2461 const EVP_MD
*md
= s
->cert
->key
->digest
;
2462 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
,
2464 if (hdatalen
<= 0 ||
2465 !tls12_get_sigandhash(p
, pkey
, md
)) {
2466 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2470 if (!EVP_SignInit_ex(&mctx
, md
, NULL
) ||
2471 !EVP_SignUpdate(&mctx
, hdata
, hdatalen
) ||
2472 !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
2473 SSLerror(s
, ERR_R_EVP_LIB
);
2478 if (!tls1_digest_cached_records(s
))
2480 } else if (pkey
->type
== EVP_PKEY_RSA
) {
2481 if (RSA_sign(NID_md5_sha1
, data
,
2482 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, &(p
[2]),
2483 &u
, pkey
->pkey
.rsa
) <= 0 ) {
2484 SSLerror(s
, ERR_R_RSA_LIB
);
2489 } else if (pkey
->type
== EVP_PKEY_EC
) {
2490 if (!ECDSA_sign(pkey
->save_type
,
2491 &(data
[MD5_DIGEST_LENGTH
]),
2492 SHA_DIGEST_LENGTH
, &(p
[2]),
2493 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
2494 SSLerror(s
, ERR_R_ECDSA_LIB
);
2499 #ifndef OPENSSL_NO_GOST
2500 } else if (pkey
->type
== NID_id_GostR3410_94
||
2501 pkey
->type
== NID_id_GostR3410_2001
) {
2502 unsigned char signbuf
[128];
2509 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2510 if (hdatalen
<= 0) {
2511 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2514 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2515 !(md
= EVP_get_digestbynid(nid
))) {
2516 SSLerror(s
, ERR_R_EVP_LIB
);
2519 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2520 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2521 !EVP_DigestFinal(&mctx
, signbuf
, &u
) ||
2522 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2523 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
2524 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2525 GOST_SIG_FORMAT_RS_LE
,
2527 (EVP_PKEY_sign(pctx
, &(p
[2]), &sigsize
,
2528 signbuf
, u
) <= 0)) {
2529 SSLerror(s
, ERR_R_EVP_LIB
);
2532 if (!tls1_digest_cached_records(s
))
2539 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2543 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_VRFY_B
;
2545 ssl3_handshake_msg_finish(s
, n
);
2548 EVP_MD_CTX_cleanup(&mctx
);
2549 EVP_PKEY_CTX_free(pctx
);
2551 return (ssl3_handshake_write(s
));
2554 EVP_MD_CTX_cleanup(&mctx
);
2555 EVP_PKEY_CTX_free(pctx
);
2560 ssl3_send_client_certificate(SSL
*s
)
2562 EVP_PKEY
*pkey
= NULL
;
2564 CBB cbb
, client_cert
;
2567 memset(&cbb
, 0, sizeof(cbb
));
2569 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_A
) {
2570 if ((s
->cert
== NULL
) || (s
->cert
->key
->x509
== NULL
) ||
2571 (s
->cert
->key
->privatekey
== NULL
))
2572 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_B
;
2574 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_C
;
2577 /* We need to get a client cert */
2578 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_B
) {
2580 * If we get an error, we need to
2581 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2582 * We then get retied later
2584 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2586 s
->internal
->rwstate
= SSL_X509_LOOKUP
;
2589 s
->internal
->rwstate
= SSL_NOTHING
;
2590 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2591 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_B
;
2592 if (!SSL_use_certificate(s
, x509
) ||
2593 !SSL_use_PrivateKey(s
, pkey
))
2595 } else if (i
== 1) {
2597 SSLerror(s
, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2601 EVP_PKEY_free(pkey
);
2603 S3I(s
)->tmp
.cert_req
= 2;
2605 /* Ok, we have a cert */
2606 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_C
;
2609 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_C
) {
2610 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &client_cert
,
2611 SSL3_MT_CERTIFICATE
))
2613 if (!ssl3_output_cert_chain(s
, &client_cert
,
2614 (S3I(s
)->tmp
.cert_req
== 2) ? NULL
: s
->cert
->key
->x509
))
2616 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2619 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_D
;
2622 /* SSL3_ST_CW_CERT_D */
2623 return (ssl3_handshake_write(s
));
2631 #define has_bits(i,m) (((i)&(m)) == (m))
2634 ssl3_check_cert_and_algorithm(SSL
*s
)
2638 EVP_PKEY
*pkey
= NULL
;
2642 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2643 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
2645 /* We don't have a certificate. */
2646 if (alg_a
& SSL_aNULL
)
2649 sc
= SSI(s
)->sess_cert
;
2651 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2654 dh
= SSI(s
)->sess_cert
->peer_dh_tmp
;
2656 /* This is the passed certificate. */
2658 idx
= sc
->peer_cert_type
;
2659 if (idx
== SSL_PKEY_ECC
) {
2660 if (ssl_check_srvr_ecc_cert_and_alg(
2661 sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
2663 SSLerror(s
, SSL_R_BAD_ECC_CERT
);
2669 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
2670 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
2671 EVP_PKEY_free(pkey
);
2673 /* Check that we have a certificate if we require one. */
2674 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_SIGN
)) {
2675 SSLerror(s
, SSL_R_MISSING_RSA_SIGNING_CERT
);
2678 if ((alg_k
& SSL_kRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_ENC
)) {
2679 SSLerror(s
, SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2682 if ((alg_k
& SSL_kDHE
) &&
2683 !(has_bits(i
, EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
))) {
2684 SSLerror(s
, SSL_R_MISSING_DH_KEY
);
2690 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2696 * Check to see if handshake is full or resumed. Usually this is just a
2697 * case of checking to see if a cache hit has occurred. In the case of
2698 * session tickets we have to check the next message to be sure.
2702 ssl3_check_finished(SSL
*s
)
2707 /* If we have no ticket it cannot be a resumed session. */
2708 if (!s
->session
->tlsext_tick
)
2710 /* this function is called when we really expect a Certificate
2711 * message, so permit appropriate message length */
2712 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
2713 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
2716 S3I(s
)->tmp
.reuse_message
= 1;
2717 if ((S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) ||
2718 (S3I(s
)->tmp
.message_type
== SSL3_MT_NEWSESSION_TICKET
))
2725 ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
2729 #ifndef OPENSSL_NO_ENGINE
2730 if (s
->ctx
->internal
->client_cert_engine
) {
2731 i
= ENGINE_load_ssl_client_cert(
2732 s
->ctx
->internal
->client_cert_engine
, s
,
2733 SSL_get_client_CA_list(s
), px509
, ppkey
, NULL
, NULL
, NULL
);
2738 if (s
->ctx
->internal
->client_cert_cb
)
2739 i
= s
->ctx
->internal
->client_cert_cb(s
, px509
, ppkey
);