1 /* $OpenBSD: ssl_clnt.c,v 1.34 2018/09/05 16:58: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"
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 if (s
->internal
->hit
) {
514 S3I(s
)->hs
.next_state
= SSL_ST_OK
;
516 /* Allow NewSessionTicket if ticket expected */
517 if (s
->internal
->tlsext_ticket_expected
)
518 S3I(s
)->hs
.next_state
=
519 SSL3_ST_CR_SESSION_TICKET_A
;
521 S3I(s
)->hs
.next_state
=
522 SSL3_ST_CR_FINISHED_A
;
524 s
->internal
->init_num
= 0;
527 case SSL3_ST_CR_SESSION_TICKET_A
:
528 case SSL3_ST_CR_SESSION_TICKET_B
:
529 ret
= ssl3_get_new_session_ticket(s
);
532 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
533 s
->internal
->init_num
= 0;
536 case SSL3_ST_CR_CERT_STATUS_A
:
537 case SSL3_ST_CR_CERT_STATUS_B
:
538 ret
= ssl3_get_cert_status(s
);
541 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
542 s
->internal
->init_num
= 0;
545 case SSL3_ST_CR_FINISHED_A
:
546 case SSL3_ST_CR_FINISHED_B
:
548 D1I(s
)->change_cipher_spec_ok
= 1;
550 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
551 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
552 SSL3_ST_CR_FINISHED_B
);
558 if (s
->internal
->hit
)
559 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
561 S3I(s
)->hs
.state
= SSL_ST_OK
;
562 s
->internal
->init_num
= 0;
565 case SSL3_ST_CW_FLUSH
:
566 s
->internal
->rwstate
= SSL_WRITING
;
567 if (BIO_flush(s
->wbio
) <= 0) {
568 if (SSL_IS_DTLS(s
)) {
569 /* If the write error was fatal, stop trying */
570 if (!BIO_should_retry(s
->wbio
)) {
571 s
->internal
->rwstate
= SSL_NOTHING
;
572 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
578 s
->internal
->rwstate
= SSL_NOTHING
;
579 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
583 /* clean a few things up */
584 tls1_cleanup_key_block(s
);
586 if (!SSL_IS_DTLS(s
)) {
587 BUF_MEM_free(s
->internal
->init_buf
);
588 s
->internal
->init_buf
= NULL
;
591 ssl_free_wbio_buffer(s
);
593 s
->internal
->init_num
= 0;
594 s
->internal
->renegotiate
= 0;
595 s
->internal
->new_session
= 0;
597 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
598 if (s
->internal
->hit
)
599 s
->ctx
->internal
->stats
.sess_hit
++;
603 s
->internal
->handshake_func
= ssl3_connect
;
604 s
->ctx
->internal
->stats
.sess_connect_good
++;
607 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
609 if (SSL_IS_DTLS(s
)) {
610 /* done with handshaking */
611 D1I(s
)->handshake_read_seq
= 0;
612 D1I(s
)->next_handshake_write_seq
= 0;
619 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
625 /* did we do anything */
626 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
627 if (s
->internal
->debug
) {
628 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
632 if ((cb
!= NULL
) && (S3I(s
)->hs
.state
!= state
)) {
633 new_state
= S3I(s
)->hs
.state
;
634 S3I(s
)->hs
.state
= state
;
635 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
636 S3I(s
)->hs
.state
= new_state
;
643 s
->internal
->in_handshake
--;
645 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
651 ssl3_send_client_hello(SSL
*s
)
653 CBB cbb
, client_hello
, session_id
, cookie
, cipher_suites
;
654 CBB compression_methods
;
655 uint16_t max_version
;
658 memset(&cbb
, 0, sizeof(cbb
));
660 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CLNT_HELLO_A
) {
661 SSL_SESSION
*sess
= s
->session
;
663 if (ssl_supported_version_range(s
, NULL
, &max_version
) != 1) {
664 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
667 s
->client_version
= s
->version
= max_version
;
670 sess
->ssl_version
!= s
->version
||
671 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
672 sess
->internal
->not_resumable
) {
673 if (!ssl_get_new_session(s
, 0))
676 /* else use the pre-loaded session */
679 * If a DTLS ClientHello message is being resent after a
680 * HelloVerifyRequest, we must retain the original client
683 if (!SSL_IS_DTLS(s
) || D1I(s
)->send_cookie
== 0)
684 arc4random_buf(s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
686 if (!ssl3_handshake_msg_start(s
, &cbb
, &client_hello
,
687 SSL3_MT_CLIENT_HELLO
))
691 * Version indicates the negotiated version: for example from
692 * an SSLv2/v3 compatible client hello). The client_version
693 * field is the maximum version we permit and it is also
694 * used in RSA encrypted premaster secrets. Some servers can
695 * choke if we initially report a higher version then
696 * renegotiate to a lower one in the premaster secret. This
697 * didn't happen with TLS 1.0 as most servers supported it
698 * but it can with TLS 1.1 or later if the server only supports
701 * Possible scenario with previous logic:
702 * 1. Client hello indicates TLS 1.2
703 * 2. Server hello says TLS 1.0
704 * 3. RSA encrypted premaster secret uses 1.2.
705 * 4. Handhaked proceeds using TLS 1.0.
706 * 5. Server sends hello request to renegotiate.
707 * 6. Client hello indicates TLS v1.0 as we now
708 * know that is maximum server supports.
709 * 7. Server chokes on RSA encrypted premaster secret
710 * containing version 1.0.
712 * For interoperability it should be OK to always use the
713 * maximum version we support in client hello and then rely
714 * on the checking of version to ensure the servers isn't
715 * being inconsistent: for example initially negotiating with
716 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
717 * client_version in client hello and not resetting it to
718 * the negotiated version.
720 if (!CBB_add_u16(&client_hello
, s
->client_version
))
724 if (!CBB_add_bytes(&client_hello
, s
->s3
->client_random
,
725 sizeof(s
->s3
->client_random
)))
729 if (!CBB_add_u8_length_prefixed(&client_hello
, &session_id
))
731 if (!s
->internal
->new_session
&&
732 s
->session
->session_id_length
> 0) {
733 sl
= s
->session
->session_id_length
;
734 if (sl
> sizeof(s
->session
->session_id
)) {
735 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
738 if (!CBB_add_bytes(&session_id
,
739 s
->session
->session_id
, sl
))
744 if (SSL_IS_DTLS(s
)) {
745 if (D1I(s
)->cookie_len
> sizeof(D1I(s
)->cookie
)) {
746 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
749 if (!CBB_add_u8_length_prefixed(&client_hello
, &cookie
))
751 if (!CBB_add_bytes(&cookie
, D1I(s
)->cookie
,
756 /* Ciphers supported */
757 if (!CBB_add_u16_length_prefixed(&client_hello
, &cipher_suites
))
759 if (!ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
),
761 SSLerror(s
, SSL_R_NO_CIPHERS_AVAILABLE
);
765 /* Add in compression methods (null) */
766 if (!CBB_add_u8_length_prefixed(&client_hello
,
767 &compression_methods
))
769 if (!CBB_add_u8(&compression_methods
, 0))
773 if (!tlsext_clienthello_build(s
, &client_hello
)) {
774 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
778 if (!ssl3_handshake_msg_finish(s
, &cbb
))
781 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_B
;
784 /* SSL3_ST_CW_CLNT_HELLO_B */
785 return (ssl3_handshake_write(s
));
794 ssl3_get_server_hello(SSL
*s
)
796 CBS cbs
, server_random
, session_id
;
797 uint16_t server_version
, cipher_suite
;
798 uint16_t min_version
, max_version
;
799 uint8_t compression_method
;
800 STACK_OF(SSL_CIPHER
) *sk
;
801 const SSL_CIPHER
*cipher
;
802 const SSL_METHOD
*method
;
808 s
->internal
->first_packet
= 1;
809 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_HELLO_A
,
810 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, /* ?? */ &ok
);
813 s
->internal
->first_packet
= 0;
818 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
820 if (SSL_IS_DTLS(s
)) {
821 if (S3I(s
)->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
822 if (D1I(s
)->send_cookie
== 0) {
823 S3I(s
)->tmp
.reuse_message
= 1;
826 /* Already sent a cookie. */
827 al
= SSL_AD_UNEXPECTED_MESSAGE
;
828 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
834 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
835 al
= SSL_AD_UNEXPECTED_MESSAGE
;
836 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
840 if (!CBS_get_u16(&cbs
, &server_version
))
843 if (ssl_supported_version_range(s
, &min_version
, &max_version
) != 1) {
844 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
848 if (server_version
< min_version
|| server_version
> max_version
) {
849 SSLerror(s
, SSL_R_WRONG_SSL_VERSION
);
850 s
->version
= (s
->version
& 0xff00) | (server_version
& 0xff);
851 al
= SSL_AD_PROTOCOL_VERSION
;
854 s
->version
= server_version
;
856 if ((method
= tls1_get_client_method(server_version
)) == NULL
)
857 method
= dtls1_get_client_method(server_version
);
858 if (method
== NULL
) {
859 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
865 if (!CBS_get_bytes(&cbs
, &server_random
, SSL3_RANDOM_SIZE
))
867 if (!CBS_write_bytes(&server_random
, s
->s3
->server_random
,
868 sizeof(s
->s3
->server_random
), NULL
))
872 if (!CBS_get_u8_length_prefixed(&cbs
, &session_id
))
875 if ((CBS_len(&session_id
) > sizeof(s
->session
->session_id
)) ||
876 (CBS_len(&session_id
) > SSL3_SESSION_ID_SIZE
)) {
877 al
= SSL_AD_ILLEGAL_PARAMETER
;
878 SSLerror(s
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
883 if (!CBS_get_u16(&cbs
, &cipher_suite
))
887 * Check if we want to resume the session based on external
890 if (s
->internal
->tls_session_secret_cb
) {
891 SSL_CIPHER
*pref_cipher
= NULL
;
892 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
893 if (s
->internal
->tls_session_secret_cb(s
, s
->session
->master_key
,
894 &s
->session
->master_key_length
, NULL
, &pref_cipher
,
895 s
->internal
->tls_session_secret_cb_arg
)) {
896 s
->session
->cipher
= pref_cipher
? pref_cipher
:
897 ssl3_get_cipher_by_value(cipher_suite
);
898 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
902 if (s
->session
->session_id_length
!= 0 &&
903 CBS_mem_equal(&session_id
, s
->session
->session_id
,
904 s
->session
->session_id_length
)) {
905 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
||
906 timingsafe_memcmp(s
->session
->sid_ctx
,
907 s
->sid_ctx
, s
->sid_ctx_length
) != 0) {
908 /* actually a client application bug */
909 al
= SSL_AD_ILLEGAL_PARAMETER
;
910 SSLerror(s
, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
913 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
914 s
->internal
->hit
= 1;
916 /* a miss or crap from the other end */
918 /* If we were trying for session-id reuse, make a new
919 * SSL_SESSION so we don't stuff up other people */
920 s
->internal
->hit
= 0;
921 if (s
->session
->session_id_length
> 0) {
922 if (!ssl_get_new_session(s
, 0)) {
923 al
= SSL_AD_INTERNAL_ERROR
;
929 * XXX - improve the handling for the case where there is a
930 * zero length session identifier.
932 if (!CBS_write_bytes(&session_id
, s
->session
->session_id
,
933 sizeof(s
->session
->session_id
), &outlen
))
935 s
->session
->session_id_length
= outlen
;
937 s
->session
->ssl_version
= s
->version
;
940 if ((cipher
= ssl3_get_cipher_by_value(cipher_suite
)) == NULL
) {
941 al
= SSL_AD_ILLEGAL_PARAMETER
;
942 SSLerror(s
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
946 /* TLS v1.2 only ciphersuites require v1.2 or later. */
947 if ((cipher
->algorithm_ssl
& SSL_TLSV1_2
) &&
948 (TLS1_get_version(s
) < TLS1_2_VERSION
)) {
949 al
= SSL_AD_ILLEGAL_PARAMETER
;
950 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
954 sk
= ssl_get_ciphers_by_id(s
);
955 i
= sk_SSL_CIPHER_find(sk
, cipher
);
957 /* we did not say we would use this cipher */
958 al
= SSL_AD_ILLEGAL_PARAMETER
;
959 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
964 * Depending on the session caching (internal/external), the cipher
965 * and/or cipher_id values may not be set. Make sure that
966 * cipher_id is set and use it for comparison.
968 if (s
->session
->cipher
)
969 s
->session
->cipher_id
= s
->session
->cipher
->id
;
970 if (s
->internal
->hit
&& (s
->session
->cipher_id
!= cipher
->id
)) {
971 al
= SSL_AD_ILLEGAL_PARAMETER
;
972 SSLerror(s
, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
975 S3I(s
)->hs
.new_cipher
= cipher
;
977 if (!tls1_handshake_hash_init(s
))
981 * Don't digest cached records if no sigalgs: we may need them for
982 * client authentication.
984 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
985 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) &&
986 !tls1_digest_cached_records(s
)) {
987 al
= SSL_AD_INTERNAL_ERROR
;
991 if (!CBS_get_u8(&cbs
, &compression_method
))
994 if (compression_method
!= 0) {
995 al
= SSL_AD_ILLEGAL_PARAMETER
;
996 SSLerror(s
, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1000 if (!tlsext_serverhello_parse(s
, &cbs
, &al
)) {
1001 SSLerror(s
, SSL_R_PARSE_TLSEXT
);
1006 * Determine if we need to see RI. Strictly speaking if we want to
1007 * avoid an attack we should *always* see RI even on initial server
1008 * hello because the client doesn't see any renegotiation during an
1009 * attack. However this would mean we could not connect to any server
1010 * which doesn't support RI so for the immediate future tolerate RI
1011 * absence on initial connect only.
1013 if (!S3I(s
)->renegotiate_seen
&&
1014 !(s
->internal
->options
& SSL_OP_LEGACY_SERVER_CONNECT
)) {
1015 al
= SSL_AD_HANDSHAKE_FAILURE
;
1016 SSLerror(s
, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
1020 if (ssl_check_serverhello_tlsext(s
) <= 0) {
1021 SSLerror(s
, SSL_R_SERVERHELLO_TLSEXT
);
1028 /* wrong packet length */
1029 al
= SSL_AD_DECODE_ERROR
;
1030 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1032 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1038 ssl3_get_server_certificate(SSL
*s
)
1040 int al
, i
, ok
, ret
= -1;
1044 const unsigned char *q
;
1045 STACK_OF(X509
) *sk
= NULL
;
1047 EVP_PKEY
*pkey
= NULL
;
1049 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
1050 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
1055 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
1056 S3I(s
)->tmp
.reuse_message
= 1;
1060 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
1061 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1062 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
1067 if ((sk
= sk_X509_new_null()) == NULL
) {
1068 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1075 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1076 if (CBS_len(&cbs
) < 3)
1079 if (!CBS_get_u24_length_prefixed(&cbs
, &cert_list
) ||
1080 CBS_len(&cbs
) != 0) {
1081 al
= SSL_AD_DECODE_ERROR
;
1082 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1086 while (CBS_len(&cert_list
) > 0) {
1089 if (CBS_len(&cert_list
) < 3)
1091 if (!CBS_get_u24_length_prefixed(&cert_list
, &cert
)) {
1092 al
= SSL_AD_DECODE_ERROR
;
1093 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1097 q
= CBS_data(&cert
);
1098 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
1100 al
= SSL_AD_BAD_CERTIFICATE
;
1101 SSLerror(s
, ERR_R_ASN1_LIB
);
1104 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
1105 al
= SSL_AD_DECODE_ERROR
;
1106 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1109 if (!sk_X509_push(sk
, x
)) {
1110 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1116 i
= ssl_verify_cert_chain(s
, sk
);
1117 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)) {
1118 al
= ssl_verify_alarm_type(s
->verify_result
);
1119 SSLerror(s
, SSL_R_CERTIFICATE_VERIFY_FAILED
);
1123 ERR_clear_error(); /* but we keep s->verify_result */
1125 sc
= ssl_sess_cert_new();
1128 ssl_sess_cert_free(SSI(s
)->sess_cert
);
1129 SSI(s
)->sess_cert
= sc
;
1131 sc
->cert_chain
= sk
;
1133 * Inconsistency alert: cert_chain does include the peer's
1134 * certificate, which we don't include in s3_srvr.c
1136 x
= sk_X509_value(sk
, 0);
1138 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1140 pkey
= X509_get_pubkey(x
);
1142 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1145 SSLerror(s
, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1149 i
= ssl_cert_type(x
, pkey
);
1153 SSLerror(s
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1157 sc
->peer_cert_type
= i
;
1158 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1160 * Why would the following ever happen?
1161 * We just created sc a couple of lines ago.
1163 X509_free(sc
->peer_pkeys
[i
].x509
);
1164 sc
->peer_pkeys
[i
].x509
= x
;
1165 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1167 X509_free(s
->session
->peer
);
1168 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1169 s
->session
->peer
= x
;
1170 s
->session
->verify_result
= s
->verify_result
;
1177 /* wrong packet length */
1178 al
= SSL_AD_DECODE_ERROR
;
1179 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1181 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1184 EVP_PKEY_free(pkey
);
1186 sk_X509_pop_free(sk
, X509_free
);
1192 ssl3_get_server_kex_dhe(SSL
*s
, EVP_PKEY
**pkey
, CBS
*cbs
)
1195 BN_CTX
*bn_ctx
= NULL
;
1196 SESS_CERT
*sc
= NULL
;
1201 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1202 sc
= SSI(s
)->sess_cert
;
1204 if ((dh
= DH_new()) == NULL
) {
1205 SSLerror(s
, ERR_R_DH_LIB
);
1209 if (!CBS_get_u16_length_prefixed(cbs
, &dhp
))
1211 if ((dh
->p
= BN_bin2bn(CBS_data(&dhp
), CBS_len(&dhp
), NULL
)) == NULL
) {
1212 SSLerror(s
, ERR_R_BN_LIB
);
1216 if (!CBS_get_u16_length_prefixed(cbs
, &dhg
))
1218 if ((dh
->g
= BN_bin2bn(CBS_data(&dhg
), CBS_len(&dhg
), NULL
)) == NULL
) {
1219 SSLerror(s
, ERR_R_BN_LIB
);
1223 if (!CBS_get_u16_length_prefixed(cbs
, &dhpk
))
1225 if ((dh
->pub_key
= BN_bin2bn(CBS_data(&dhpk
), CBS_len(&dhpk
),
1227 SSLerror(s
, ERR_R_BN_LIB
);
1232 * Check the strength of the DH key just constructed.
1233 * Discard keys weaker than 1024 bits.
1235 if (DH_size(dh
) < 1024 / 8) {
1236 SSLerror(s
, SSL_R_BAD_DH_P_LENGTH
);
1240 if (alg_a
& SSL_aRSA
)
1241 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1243 /* XXX - Anonymous DH, so no certificate or pkey. */
1246 sc
->peer_dh_tmp
= dh
;
1251 al
= SSL_AD_DECODE_ERROR
;
1252 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1253 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1257 BN_CTX_free(bn_ctx
);
1263 ssl3_get_server_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1265 const EC_GROUP
*group
;
1266 EC_GROUP
*ngroup
= NULL
;
1267 EC_POINT
*point
= NULL
;
1268 BN_CTX
*bn_ctx
= NULL
;
1269 EC_KEY
*ecdh
= NULL
;
1273 * Extract the server's ephemeral ECDH public key.
1276 if ((ecdh
= EC_KEY_new()) == NULL
) {
1277 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1281 if ((ngroup
= EC_GROUP_new_by_curve_name(nid
)) == NULL
) {
1282 SSLerror(s
, ERR_R_EC_LIB
);
1285 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1286 SSLerror(s
, ERR_R_EC_LIB
);
1290 group
= EC_KEY_get0_group(ecdh
);
1292 if ((point
= EC_POINT_new(group
)) == NULL
||
1293 (bn_ctx
= BN_CTX_new()) == NULL
) {
1294 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1298 if (EC_POINT_oct2point(group
, point
, CBS_data(public),
1299 CBS_len(public), bn_ctx
) == 0) {
1300 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1301 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1305 EC_KEY_set_public_key(ecdh
, point
);
1306 sc
->peer_ecdh_tmp
= ecdh
;
1312 BN_CTX_free(bn_ctx
);
1313 EC_GROUP_free(ngroup
);
1314 EC_POINT_free(point
);
1321 ssl3_get_server_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1325 if (nid
!= NID_X25519
) {
1326 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1330 if (CBS_len(public) != X25519_KEY_LENGTH
) {
1331 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1332 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1336 if (!CBS_stow(public, &sc
->peer_x25519_tmp
, &outlen
)) {
1337 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1348 ssl3_get_server_kex_ecdhe(SSL
*s
, EVP_PKEY
**pkey
, CBS
*cbs
)
1358 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1359 sc
= SSI(s
)->sess_cert
;
1361 /* Only named curves are supported. */
1362 if (!CBS_get_u8(cbs
, &curve_type
) ||
1363 curve_type
!= NAMED_CURVE_TYPE
||
1364 !CBS_get_u16(cbs
, &curve_id
)) {
1365 al
= SSL_AD_DECODE_ERROR
;
1366 SSLerror(s
, SSL_R_LENGTH_TOO_SHORT
);
1371 * Check that the curve is one of our preferences - if it is not,
1372 * the server has sent us an invalid curve.
1374 if (tls1_check_curve(s
, curve_id
) != 1) {
1375 al
= SSL_AD_DECODE_ERROR
;
1376 SSLerror(s
, SSL_R_WRONG_CURVE
);
1380 if ((nid
= tls1_ec_curve_id2nid(curve_id
)) == 0) {
1381 al
= SSL_AD_INTERNAL_ERROR
;
1382 SSLerror(s
, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1386 if (!CBS_get_u8_length_prefixed(cbs
, &public))
1389 if (nid
== NID_X25519
) {
1390 if (ssl3_get_server_kex_ecdhe_ecx(s
, sc
, nid
, &public) != 1)
1393 if (ssl3_get_server_kex_ecdhe_ecp(s
, sc
, nid
, &public) != 1)
1398 * The ECC/TLS specification does not mention the use of DSA to sign
1399 * ECParameters in the server key exchange message. We do support RSA
1402 if (alg_a
& SSL_aRSA
)
1403 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1404 else if (alg_a
& SSL_aECDSA
)
1405 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1407 /* XXX - Anonymous ECDH, so no certificate or pkey. */
1413 al
= SSL_AD_DECODE_ERROR
;
1414 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1417 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1424 ssl3_get_server_key_exchange(SSL
*s
)
1427 const EVP_MD
*md
= NULL
;
1428 EVP_PKEY
*pkey
= NULL
;
1430 const unsigned char *param
;
1431 uint8_t hash_id
, sig_id
;
1432 long n
, alg_k
, alg_a
;
1436 EVP_MD_CTX_init(&md_ctx
);
1438 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
1439 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1442 * Use same message size as in ssl3_get_certificate_request()
1443 * as ServerKeyExchange message may be skipped.
1445 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_KEY_EXCH_A
,
1446 SSL3_ST_CR_KEY_EXCH_B
, -1, s
->internal
->max_cert_list
, &ok
);
1453 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1455 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1457 * Do not skip server key exchange if this cipher suite uses
1460 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
1461 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1462 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1466 S3I(s
)->tmp
.reuse_message
= 1;
1467 EVP_MD_CTX_cleanup(&md_ctx
);
1471 if (SSI(s
)->sess_cert
!= NULL
) {
1472 DH_free(SSI(s
)->sess_cert
->peer_dh_tmp
);
1473 SSI(s
)->sess_cert
->peer_dh_tmp
= NULL
;
1475 EC_KEY_free(SSI(s
)->sess_cert
->peer_ecdh_tmp
);
1476 SSI(s
)->sess_cert
->peer_ecdh_tmp
= NULL
;
1478 free(SSI(s
)->sess_cert
->peer_x25519_tmp
);
1479 SSI(s
)->sess_cert
->peer_x25519_tmp
= NULL
;
1481 SSI(s
)->sess_cert
= ssl_sess_cert_new();
1482 if (SSI(s
)->sess_cert
== NULL
)
1486 param
= CBS_data(&cbs
);
1487 param_len
= CBS_len(&cbs
);
1489 if (alg_k
& SSL_kDHE
) {
1490 if (ssl3_get_server_kex_dhe(s
, &pkey
, &cbs
) != 1)
1492 } else if (alg_k
& SSL_kECDHE
) {
1493 if (ssl3_get_server_kex_ecdhe(s
, &pkey
, &cbs
) != 1)
1495 } else if (alg_k
!= 0) {
1496 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1497 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1501 param_len
-= CBS_len(&cbs
);
1503 /* if it was signed, check the signature */
1505 if (SSL_USE_SIGALGS(s
)) {
1506 if (!CBS_get_u8(&cbs
, &hash_id
))
1508 if (!CBS_get_u8(&cbs
, &sig_id
))
1511 if ((md
= tls12_get_hash(hash_id
)) == NULL
) {
1512 SSLerror(s
, SSL_R_UNKNOWN_DIGEST
);
1513 al
= SSL_AD_DECODE_ERROR
;
1517 /* Check key type is consistent with signature. */
1518 if ((sigalg
= tls12_get_sigid(pkey
)) == -1) {
1519 /* Should never happen */
1520 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1523 if (sigalg
!= sig_id
) {
1524 SSLerror(s
, SSL_R_WRONG_SIGNATURE_TYPE
);
1525 al
= SSL_AD_DECODE_ERROR
;
1528 } else if (pkey
->type
== EVP_PKEY_RSA
) {
1529 md
= EVP_md5_sha1();
1534 if (!CBS_get_u16_length_prefixed(&cbs
, &signature
))
1536 if (CBS_len(&signature
) > EVP_PKEY_size(pkey
)) {
1537 al
= SSL_AD_DECODE_ERROR
;
1538 SSLerror(s
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1542 if (!EVP_VerifyInit_ex(&md_ctx
, md
, NULL
))
1544 if (!EVP_VerifyUpdate(&md_ctx
, s
->s3
->client_random
,
1547 if (!EVP_VerifyUpdate(&md_ctx
, s
->s3
->server_random
,
1550 if (!EVP_VerifyUpdate(&md_ctx
, param
, param_len
))
1552 if (EVP_VerifyFinal(&md_ctx
, CBS_data(&signature
),
1553 CBS_len(&signature
), pkey
) <= 0) {
1554 al
= SSL_AD_DECRYPT_ERROR
;
1555 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1559 /* aNULL does not need public keys. */
1560 if (!(alg_a
& SSL_aNULL
)) {
1561 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1566 if (CBS_len(&cbs
) != 0) {
1567 al
= SSL_AD_DECODE_ERROR
;
1568 SSLerror(s
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1572 EVP_PKEY_free(pkey
);
1573 EVP_MD_CTX_cleanup(&md_ctx
);
1578 al
= SSL_AD_DECODE_ERROR
;
1579 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1582 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1585 EVP_PKEY_free(pkey
);
1586 EVP_MD_CTX_cleanup(&md_ctx
);
1592 ssl3_get_certificate_request(SSL
*s
)
1597 CBS cert_request
, ctypes
, rdn_list
;
1598 X509_NAME
*xn
= NULL
;
1599 const unsigned char *q
;
1600 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1602 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_REQ_A
,
1603 SSL3_ST_CR_CERT_REQ_B
, -1, s
->internal
->max_cert_list
, &ok
);
1608 S3I(s
)->tmp
.cert_req
= 0;
1610 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1611 S3I(s
)->tmp
.reuse_message
= 1;
1613 * If we get here we don't need any cached handshake records
1614 * as we wont be doing client auth.
1616 if (S3I(s
)->handshake_buffer
) {
1617 if (!tls1_digest_cached_records(s
))
1623 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1624 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1625 SSLerror(s
, SSL_R_WRONG_MESSAGE_TYPE
);
1629 /* TLS does not like anon-DH with client cert */
1630 if (S3I(s
)->hs
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
1631 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1632 SSLerror(s
, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1638 CBS_init(&cert_request
, s
->internal
->init_msg
, n
);
1640 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1641 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1645 /* get the certificate types */
1646 if (!CBS_get_u8(&cert_request
, &ctype_num
))
1649 if (ctype_num
> SSL3_CT_NUMBER
)
1650 ctype_num
= SSL3_CT_NUMBER
;
1651 if (!CBS_get_bytes(&cert_request
, &ctypes
, ctype_num
) ||
1652 !CBS_write_bytes(&ctypes
, (uint8_t *)S3I(s
)->tmp
.ctype
,
1653 sizeof(S3I(s
)->tmp
.ctype
), NULL
)) {
1654 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1658 if (SSL_USE_SIGALGS(s
)) {
1661 if (CBS_len(&cert_request
) < 2) {
1662 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1666 /* Check we have enough room for signature algorithms and
1667 * following length value.
1669 if (!CBS_get_u16_length_prefixed(&cert_request
, &sigalgs
)) {
1670 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1671 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1674 if (!tls1_process_sigalgs(s
, &sigalgs
)) {
1675 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1676 SSLerror(s
, SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
1681 /* get the CA RDNs */
1682 if (CBS_len(&cert_request
) < 2) {
1683 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1687 if (!CBS_get_u16_length_prefixed(&cert_request
, &rdn_list
) ||
1688 CBS_len(&cert_request
) != 0) {
1689 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1690 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1694 while (CBS_len(&rdn_list
) > 0) {
1697 if (CBS_len(&rdn_list
) < 2) {
1698 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1702 if (!CBS_get_u16_length_prefixed(&rdn_list
, &rdn
)) {
1703 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1704 SSLerror(s
, SSL_R_CA_DN_TOO_LONG
);
1709 if ((xn
= d2i_X509_NAME(NULL
, &q
, CBS_len(&rdn
))) == NULL
) {
1710 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1711 SSL_AD_DECODE_ERROR
);
1712 SSLerror(s
, ERR_R_ASN1_LIB
);
1716 if (q
!= CBS_data(&rdn
) + CBS_len(&rdn
)) {
1717 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1718 SSLerror(s
, SSL_R_CA_DN_LENGTH_MISMATCH
);
1721 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1722 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1725 xn
= NULL
; /* avoid free in err block */
1728 /* we should setup a certificate to return.... */
1729 S3I(s
)->tmp
.cert_req
= 1;
1730 S3I(s
)->tmp
.ctype_num
= ctype_num
;
1731 sk_X509_NAME_pop_free(S3I(s
)->tmp
.ca_names
, X509_NAME_free
);
1732 S3I(s
)->tmp
.ca_names
= ca_sk
;
1738 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1742 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1747 ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1749 return (X509_NAME_cmp(*a
, *b
));
1753 ssl3_get_new_session_ticket(SSL
*s
)
1755 int ok
, al
, ret
= 0;
1756 uint32_t lifetime_hint
;
1758 CBS cbs
, session_ticket
;
1760 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SESSION_TICKET_A
,
1761 SSL3_ST_CR_SESSION_TICKET_B
, -1, 16384, &ok
);
1765 if (S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) {
1766 S3I(s
)->tmp
.reuse_message
= 1;
1769 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
) {
1770 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1771 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
1776 al
= SSL_AD_DECODE_ERROR
;
1777 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1781 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1782 if (!CBS_get_u32(&cbs
, &lifetime_hint
) ||
1783 #if UINT32_MAX > LONG_MAX
1784 lifetime_hint
> LONG_MAX
||
1786 !CBS_get_u16_length_prefixed(&cbs
, &session_ticket
) ||
1787 CBS_len(&cbs
) != 0) {
1788 al
= SSL_AD_DECODE_ERROR
;
1789 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1792 s
->session
->tlsext_tick_lifetime_hint
= (long)lifetime_hint
;
1794 if (!CBS_stow(&session_ticket
, &s
->session
->tlsext_tick
,
1795 &s
->session
->tlsext_ticklen
)) {
1796 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1801 * There are two ways to detect a resumed ticket sesion.
1802 * One is to set an appropriate session ID and then the server
1803 * must return a match in ServerHello. This allows the normal
1804 * client session ID matching to work and we know much
1805 * earlier that the ticket has been accepted.
1807 * The other way is to set zero length session ID when the
1808 * ticket is presented and rely on the handshake to determine
1809 * session resumption.
1811 * We choose the former approach because this fits in with
1812 * assumptions elsewhere in OpenSSL. The session ID is set
1813 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1816 EVP_Digest(CBS_data(&session_ticket
), CBS_len(&session_ticket
),
1817 s
->session
->session_id
, &s
->session
->session_id_length
,
1818 EVP_sha256(), NULL
);
1822 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1828 ssl3_get_cert_status(SSL
*s
)
1830 CBS cert_status
, response
;
1834 uint8_t status_type
;
1836 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_STATUS_A
,
1837 SSL3_ST_CR_CERT_STATUS_B
, SSL3_MT_CERTIFICATE_STATUS
,
1844 /* need at least status type + length */
1845 al
= SSL_AD_DECODE_ERROR
;
1846 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1850 CBS_init(&cert_status
, s
->internal
->init_msg
, n
);
1851 if (!CBS_get_u8(&cert_status
, &status_type
) ||
1852 CBS_len(&cert_status
) < 3) {
1853 /* need at least status type + length */
1854 al
= SSL_AD_DECODE_ERROR
;
1855 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1859 if (status_type
!= TLSEXT_STATUSTYPE_ocsp
) {
1860 al
= SSL_AD_DECODE_ERROR
;
1861 SSLerror(s
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
1865 if (!CBS_get_u24_length_prefixed(&cert_status
, &response
) ||
1866 CBS_len(&cert_status
) != 0) {
1867 al
= SSL_AD_DECODE_ERROR
;
1868 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1872 if (!CBS_stow(&response
, &s
->internal
->tlsext_ocsp_resp
,
1873 &stow_len
) || stow_len
> INT_MAX
) {
1874 s
->internal
->tlsext_ocsp_resplen
= 0;
1875 al
= SSL_AD_INTERNAL_ERROR
;
1876 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1879 s
->internal
->tlsext_ocsp_resplen
= (int)stow_len
;
1881 if (s
->ctx
->internal
->tlsext_status_cb
) {
1883 ret
= s
->ctx
->internal
->tlsext_status_cb(s
,
1884 s
->ctx
->internal
->tlsext_status_arg
);
1886 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
1887 SSLerror(s
, SSL_R_INVALID_STATUS_RESPONSE
);
1891 al
= SSL_AD_INTERNAL_ERROR
;
1892 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1898 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1903 ssl3_get_server_done(SSL
*s
)
1908 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_DONE_A
,
1909 SSL3_ST_CR_SRVR_DONE_B
, SSL3_MT_SERVER_DONE
,
1910 30, /* should be very small, like 0 :-) */ &ok
);
1915 /* should contain no data */
1916 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1917 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1925 ssl3_send_client_kex_rsa(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1927 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
];
1928 unsigned char *enc_pms
= NULL
;
1929 EVP_PKEY
*pkey
= NULL
;
1935 * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1.
1938 pkey
= X509_get_pubkey(sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1939 if (pkey
== NULL
|| pkey
->type
!= EVP_PKEY_RSA
||
1940 pkey
->pkey
.rsa
== NULL
) {
1941 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1945 pms
[0] = s
->client_version
>> 8;
1946 pms
[1] = s
->client_version
& 0xff;
1947 arc4random_buf(&pms
[2], sizeof(pms
) - 2);
1949 if ((enc_pms
= malloc(RSA_size(pkey
->pkey
.rsa
))) == NULL
) {
1950 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1954 enc_len
= RSA_public_encrypt(sizeof(pms
), pms
, enc_pms
, pkey
->pkey
.rsa
,
1957 SSLerror(s
, SSL_R_BAD_RSA_ENCRYPT
);
1961 if (!CBB_add_u16_length_prefixed(cbb
, &epms
))
1963 if (!CBB_add_bytes(&epms
, enc_pms
, enc_len
))
1965 if (!CBB_flush(cbb
))
1968 s
->session
->master_key_length
=
1969 tls1_generate_master_secret(s
,
1970 s
->session
->master_key
, pms
, sizeof(pms
));
1975 explicit_bzero(pms
, sizeof(pms
));
1976 EVP_PKEY_free(pkey
);
1983 ssl3_send_client_kex_dhe(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1985 DH
*dh_srvr
= NULL
, *dh_clnt
= NULL
;
1986 unsigned char *key
= NULL
;
1987 int key_size
= 0, key_len
;
1988 unsigned char *data
;
1992 /* Ensure that we have an ephemeral key for DHE. */
1993 if (sess_cert
->peer_dh_tmp
== NULL
) {
1994 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
1995 SSLerror(s
, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
1998 dh_srvr
= sess_cert
->peer_dh_tmp
;
2000 /* Generate a new random key. */
2001 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
2002 SSLerror(s
, ERR_R_DH_LIB
);
2005 if (!DH_generate_key(dh_clnt
)) {
2006 SSLerror(s
, ERR_R_DH_LIB
);
2009 if ((key_size
= DH_size(dh_clnt
)) <= 0) {
2010 SSLerror(s
, ERR_R_DH_LIB
);
2013 if ((key
= malloc(key_size
)) == NULL
) {
2014 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2017 if ((key_len
= DH_compute_key(key
, dh_srvr
->pub_key
, dh_clnt
)) <= 0) {
2018 SSLerror(s
, ERR_R_DH_LIB
);
2022 /* Generate master key from the result. */
2023 s
->session
->master_key_length
=
2024 tls1_generate_master_secret(s
,
2025 s
->session
->master_key
, key
, key_len
);
2027 if (!CBB_add_u16_length_prefixed(cbb
, &dh_Yc
))
2029 if (!CBB_add_space(&dh_Yc
, &data
, BN_num_bytes(dh_clnt
->pub_key
)))
2031 BN_bn2bin(dh_clnt
->pub_key
, data
);
2032 if (!CBB_flush(cbb
))
2039 freezero(key
, key_size
);
2045 ssl3_send_client_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2047 const EC_GROUP
*group
= NULL
;
2048 const EC_POINT
*point
= NULL
;
2049 EC_KEY
*ecdh
= NULL
;
2050 BN_CTX
*bn_ctx
= NULL
;
2051 unsigned char *key
= NULL
;
2052 unsigned char *data
;
2054 int key_size
= 0, key_len
;
2058 if ((group
= EC_KEY_get0_group(sc
->peer_ecdh_tmp
)) == NULL
||
2059 (point
= EC_KEY_get0_public_key(sc
->peer_ecdh_tmp
)) == NULL
) {
2060 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2064 if ((ecdh
= EC_KEY_new()) == NULL
) {
2065 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2069 if (!EC_KEY_set_group(ecdh
, group
)) {
2070 SSLerror(s
, ERR_R_EC_LIB
);
2074 /* Generate a new ECDH key pair. */
2075 if (!EC_KEY_generate_key(ecdh
)) {
2076 SSLerror(s
, ERR_R_ECDH_LIB
);
2079 if ((key_size
= ECDH_size(ecdh
)) <= 0) {
2080 SSLerror(s
, ERR_R_ECDH_LIB
);
2083 if ((key
= malloc(key_size
)) == NULL
) {
2084 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2087 key_len
= ECDH_compute_key(key
, key_size
, point
, ecdh
, NULL
);
2089 SSLerror(s
, ERR_R_ECDH_LIB
);
2093 /* Generate master key from the result. */
2094 s
->session
->master_key_length
=
2095 tls1_generate_master_secret(s
,
2096 s
->session
->master_key
, key
, key_len
);
2098 encoded_len
= EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2099 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
2100 if (encoded_len
== 0) {
2101 SSLerror(s
, ERR_R_ECDH_LIB
);
2105 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
2106 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2110 /* Encode the public key. */
2111 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2113 if (!CBB_add_space(&ecpoint
, &data
, encoded_len
))
2115 if (EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2116 POINT_CONVERSION_UNCOMPRESSED
, data
, encoded_len
,
2119 if (!CBB_flush(cbb
))
2125 freezero(key
, key_size
);
2127 BN_CTX_free(bn_ctx
);
2134 ssl3_send_client_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2136 uint8_t *public_key
= NULL
, *private_key
= NULL
, *shared_key
= NULL
;
2140 /* Generate X25519 key pair and derive shared key. */
2141 if ((public_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2143 if ((private_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2145 if ((shared_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2147 X25519_keypair(public_key
, private_key
);
2148 if (!X25519(shared_key
, private_key
, sc
->peer_x25519_tmp
))
2151 /* Serialize the public key. */
2152 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2154 if (!CBB_add_bytes(&ecpoint
, public_key
, X25519_KEY_LENGTH
))
2156 if (!CBB_flush(cbb
))
2159 /* Generate master key from the result. */
2160 s
->session
->master_key_length
=
2161 tls1_generate_master_secret(s
,
2162 s
->session
->master_key
, shared_key
, X25519_KEY_LENGTH
);
2168 freezero(private_key
, X25519_KEY_LENGTH
);
2169 freezero(shared_key
, X25519_KEY_LENGTH
);
2175 ssl3_send_client_kex_ecdhe(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2177 if (sc
->peer_x25519_tmp
!= NULL
) {
2178 if (ssl3_send_client_kex_ecdhe_ecx(s
, sc
, cbb
) != 1)
2180 } else if (sc
->peer_ecdh_tmp
!= NULL
) {
2181 if (ssl3_send_client_kex_ecdhe_ecp(s
, sc
, cbb
) != 1)
2184 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2185 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2196 ssl3_send_client_kex_gost(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
2198 unsigned char premaster_secret
[32], shared_ukm
[32], tmp
[256];
2199 EVP_PKEY
*pub_key
= NULL
;
2200 EVP_PKEY_CTX
*pkey_ctx
;
2203 unsigned int md_len
;
2204 EVP_MD_CTX
*ukm_hash
;
2209 /* Get server sertificate PKEY and create ctx from it */
2210 peer_cert
= sess_cert
->peer_pkeys
[SSL_PKEY_GOST01
].x509
;
2211 if (peer_cert
== NULL
) {
2212 SSLerror(s
, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2216 pub_key
= X509_get_pubkey(peer_cert
);
2217 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
, NULL
);
2220 * If we have send a certificate, and certificate key parameters match
2221 * those of server certificate, use certificate key for key exchange.
2222 * Otherwise, generate ephemeral key pair.
2224 EVP_PKEY_encrypt_init(pkey_ctx
);
2226 /* Generate session key. */
2227 arc4random_buf(premaster_secret
, 32);
2230 * If we have client certificate, use its secret as peer key.
2232 if (S3I(s
)->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2233 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2234 s
->cert
->key
->privatekey
) <=0) {
2236 * If there was an error - just ignore it.
2237 * Ephemeral key would be used.
2244 * Compute shared IV and store it in algorithm-specific context data.
2246 ukm_hash
= EVP_MD_CTX_new();
2247 if (ukm_hash
== NULL
) {
2248 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2252 if (ssl_get_algorithm2(s
) & SSL_HANDSHAKE_MAC_GOST94
)
2253 nid
= NID_id_GostR3411_94
;
2255 nid
= NID_id_tc26_gost3411_2012_256
;
2256 if (!EVP_DigestInit(ukm_hash
, EVP_get_digestbynid(nid
)))
2258 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2259 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2260 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2261 EVP_MD_CTX_free(ukm_hash
);
2262 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
2263 EVP_PKEY_CTRL_SET_IV
, 8, shared_ukm
) < 0) {
2264 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2269 * Make GOST keytransport blob message, encapsulate it into sequence.
2272 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, premaster_secret
,
2274 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2278 if (!CBB_add_asn1(cbb
, &gostblob
, CBS_ASN1_SEQUENCE
))
2280 if (!CBB_add_bytes(&gostblob
, tmp
, msglen
))
2282 if (!CBB_flush(cbb
))
2285 /* Check if pubkey from client certificate was used. */
2286 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
2288 /* Set flag "skip certificate verify". */
2289 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2291 EVP_PKEY_CTX_free(pkey_ctx
);
2292 s
->session
->master_key_length
=
2293 tls1_generate_master_secret(s
,
2294 s
->session
->master_key
, premaster_secret
, 32);
2299 explicit_bzero(premaster_secret
, sizeof(premaster_secret
));
2300 EVP_PKEY_free(pub_key
);
2306 ssl3_send_client_key_exchange(SSL
*s
)
2308 SESS_CERT
*sess_cert
;
2309 unsigned long alg_k
;
2312 memset(&cbb
, 0, sizeof(cbb
));
2314 if (S3I(s
)->hs
.state
== SSL3_ST_CW_KEY_EXCH_A
) {
2315 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2317 if ((sess_cert
= SSI(s
)->sess_cert
) == NULL
) {
2318 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2319 SSL_AD_UNEXPECTED_MESSAGE
);
2320 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2324 if (!ssl3_handshake_msg_start(s
, &cbb
, &kex
,
2325 SSL3_MT_CLIENT_KEY_EXCHANGE
))
2328 if (alg_k
& SSL_kRSA
) {
2329 if (ssl3_send_client_kex_rsa(s
, sess_cert
, &kex
) != 1)
2331 } else if (alg_k
& SSL_kDHE
) {
2332 if (ssl3_send_client_kex_dhe(s
, sess_cert
, &kex
) != 1)
2334 } else if (alg_k
& SSL_kECDHE
) {
2335 if (ssl3_send_client_kex_ecdhe(s
, sess_cert
, &kex
) != 1)
2337 } else if (alg_k
& SSL_kGOST
) {
2338 if (ssl3_send_client_kex_gost(s
, sess_cert
, &kex
) != 1)
2341 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2342 SSL_AD_HANDSHAKE_FAILURE
);
2343 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2347 if (!ssl3_handshake_msg_finish(s
, &cbb
))
2350 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_B
;
2353 /* SSL3_ST_CW_KEY_EXCH_B */
2354 return (ssl3_handshake_write(s
));
2363 ssl3_send_client_verify(SSL
*s
)
2365 CBB cbb
, cert_verify
, cbb_signature
;
2366 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
2367 unsigned char *signature
= NULL
;
2368 unsigned int signature_len
;
2369 EVP_PKEY_CTX
*pctx
= NULL
;
2376 EVP_MD_CTX_init(&mctx
);
2378 memset(&cbb
, 0, sizeof(cbb
));
2380 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_VRFY_A
) {
2381 if (!ssl3_handshake_msg_start(s
, &cbb
, &cert_verify
,
2382 SSL3_MT_CERTIFICATE_VERIFY
))
2386 * Create context from key and test if sha1 is allowed as
2389 pkey
= s
->cert
->key
->privatekey
;
2390 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2391 EVP_PKEY_sign_init(pctx
);
2393 /* XXX - is this needed? */
2394 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) <= 0)
2397 if ((signature
= calloc(1, EVP_PKEY_size(pkey
))) == NULL
)
2400 if (!SSL_USE_SIGALGS(s
)) {
2401 if (S3I(s
)->handshake_buffer
) {
2402 if (!tls1_digest_cached_records(s
))
2405 if (!tls1_handshake_hash_value(s
, data
, sizeof(data
),
2411 * For TLS v1.2 send signature algorithm and signature
2412 * using agreed digest and cached handshake records.
2414 if (SSL_USE_SIGALGS(s
)) {
2415 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
,
2417 md
= s
->cert
->key
->digest
;
2418 if (hdatalen
<= 0 ||
2419 !tls12_get_hashandsig(&cert_verify
, pkey
, md
)) {
2420 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2423 if (!EVP_SignInit_ex(&mctx
, md
, NULL
) ||
2424 !EVP_SignUpdate(&mctx
, hdata
, hdatalen
) ||
2425 !EVP_SignFinal(&mctx
, signature
, &signature_len
,
2427 SSLerror(s
, ERR_R_EVP_LIB
);
2430 if (!tls1_digest_cached_records(s
))
2432 } else if (pkey
->type
== EVP_PKEY_RSA
) {
2433 if (RSA_sign(NID_md5_sha1
, data
,
2434 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, signature
,
2435 &signature_len
, pkey
->pkey
.rsa
) <= 0 ) {
2436 SSLerror(s
, ERR_R_RSA_LIB
);
2439 } else if (pkey
->type
== EVP_PKEY_EC
) {
2440 if (!ECDSA_sign(pkey
->save_type
,
2441 &data
[MD5_DIGEST_LENGTH
], SHA_DIGEST_LENGTH
,
2442 signature
, &signature_len
, pkey
->pkey
.ec
)) {
2443 SSLerror(s
, ERR_R_ECDSA_LIB
);
2446 #ifndef OPENSSL_NO_GOST
2447 } else if (pkey
->type
== NID_id_GostR3410_94
||
2448 pkey
->type
== NID_id_GostR3410_2001
) {
2449 unsigned char signbuf
[128];
2454 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2455 if (hdatalen
<= 0) {
2456 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2459 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2460 !(md
= EVP_get_digestbynid(nid
))) {
2461 SSLerror(s
, ERR_R_EVP_LIB
);
2464 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2465 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2466 !EVP_DigestFinal(&mctx
, signbuf
, &u
) ||
2467 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2468 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
2469 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2470 GOST_SIG_FORMAT_RS_LE
, NULL
) <= 0) ||
2471 (EVP_PKEY_sign(pctx
, signature
, &sigsize
,
2472 signbuf
, u
) <= 0)) {
2473 SSLerror(s
, ERR_R_EVP_LIB
);
2476 if (sigsize
> UINT_MAX
)
2478 signature_len
= sigsize
;
2479 if (!tls1_digest_cached_records(s
))
2483 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2487 if (!CBB_add_u16_length_prefixed(&cert_verify
, &cbb_signature
))
2489 if (!CBB_add_bytes(&cbb_signature
, signature
, signature_len
))
2492 if (!ssl3_handshake_msg_finish(s
, &cbb
))
2495 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_VRFY_B
;
2498 EVP_MD_CTX_cleanup(&mctx
);
2499 EVP_PKEY_CTX_free(pctx
);
2502 return (ssl3_handshake_write(s
));
2506 EVP_MD_CTX_cleanup(&mctx
);
2507 EVP_PKEY_CTX_free(pctx
);
2514 ssl3_send_client_certificate(SSL
*s
)
2516 EVP_PKEY
*pkey
= NULL
;
2518 CBB cbb
, client_cert
;
2521 memset(&cbb
, 0, sizeof(cbb
));
2523 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_A
) {
2524 if ((s
->cert
== NULL
) || (s
->cert
->key
->x509
== NULL
) ||
2525 (s
->cert
->key
->privatekey
== NULL
))
2526 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_B
;
2528 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_C
;
2531 /* We need to get a client cert */
2532 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_B
) {
2534 * If we get an error, we need to
2535 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2536 * We then get retied later
2538 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2540 s
->internal
->rwstate
= SSL_X509_LOOKUP
;
2543 s
->internal
->rwstate
= SSL_NOTHING
;
2544 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2545 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_B
;
2546 if (!SSL_use_certificate(s
, x509
) ||
2547 !SSL_use_PrivateKey(s
, pkey
))
2549 } else if (i
== 1) {
2551 SSLerror(s
, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2555 EVP_PKEY_free(pkey
);
2557 S3I(s
)->tmp
.cert_req
= 2;
2559 /* Ok, we have a cert */
2560 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_C
;
2563 if (S3I(s
)->hs
.state
== SSL3_ST_CW_CERT_C
) {
2564 if (!ssl3_handshake_msg_start(s
, &cbb
, &client_cert
,
2565 SSL3_MT_CERTIFICATE
))
2567 if (!ssl3_output_cert_chain(s
, &client_cert
,
2568 (S3I(s
)->tmp
.cert_req
== 2) ? NULL
: s
->cert
->key
->x509
))
2570 if (!ssl3_handshake_msg_finish(s
, &cbb
))
2573 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_D
;
2576 /* SSL3_ST_CW_CERT_D */
2577 return (ssl3_handshake_write(s
));
2585 #define has_bits(i,m) (((i)&(m)) == (m))
2588 ssl3_check_cert_and_algorithm(SSL
*s
)
2592 EVP_PKEY
*pkey
= NULL
;
2596 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2597 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
2599 /* We don't have a certificate. */
2600 if (alg_a
& SSL_aNULL
)
2603 sc
= SSI(s
)->sess_cert
;
2605 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2608 dh
= SSI(s
)->sess_cert
->peer_dh_tmp
;
2610 /* This is the passed certificate. */
2612 idx
= sc
->peer_cert_type
;
2613 if (idx
== SSL_PKEY_ECC
) {
2614 if (ssl_check_srvr_ecc_cert_and_alg(
2615 sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
2617 SSLerror(s
, SSL_R_BAD_ECC_CERT
);
2623 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
2624 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
2625 EVP_PKEY_free(pkey
);
2627 /* Check that we have a certificate if we require one. */
2628 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_SIGN
)) {
2629 SSLerror(s
, SSL_R_MISSING_RSA_SIGNING_CERT
);
2632 if ((alg_k
& SSL_kRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_ENC
)) {
2633 SSLerror(s
, SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2636 if ((alg_k
& SSL_kDHE
) &&
2637 !(has_bits(i
, EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
))) {
2638 SSLerror(s
, SSL_R_MISSING_DH_KEY
);
2644 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2650 * Check to see if handshake is full or resumed. Usually this is just a
2651 * case of checking to see if a cache hit has occurred. In the case of
2652 * session tickets we have to check the next message to be sure.
2656 ssl3_check_finished(SSL
*s
)
2661 /* If we have no ticket it cannot be a resumed session. */
2662 if (!s
->session
->tlsext_tick
)
2664 /* this function is called when we really expect a Certificate
2665 * message, so permit appropriate message length */
2666 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
2667 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
2670 S3I(s
)->tmp
.reuse_message
= 1;
2671 if ((S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) ||
2672 (S3I(s
)->tmp
.message_type
== SSL3_MT_NEWSESSION_TICKET
))
2679 ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
2683 #ifndef OPENSSL_NO_ENGINE
2684 if (s
->ctx
->internal
->client_cert_engine
) {
2685 i
= ENGINE_load_ssl_client_cert(
2686 s
->ctx
->internal
->client_cert_engine
, s
,
2687 SSL_get_client_CA_list(s
), px509
, ppkey
, NULL
, NULL
, NULL
);
2692 if (s
->ctx
->internal
->client_cert_cb
)
2693 i
= s
->ctx
->internal
->client_cert_cb(s
, px509
, ppkey
);