1 /* $OpenBSD: ssl_clnt.c,v 1.11 2017/03/10 16:03:27 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
155 #include "ssl_locl.h"
157 #include <openssl/bn.h>
158 #include <openssl/buffer.h>
159 #include <openssl/curve25519.h>
160 #include <openssl/dh.h>
161 #include <openssl/evp.h>
162 #include <openssl/md5.h>
163 #include <openssl/objects.h>
165 #ifndef OPENSSL_NO_ENGINE
166 #include <openssl/engine.h>
168 #ifndef OPENSSL_NO_GOST
169 #include <openssl/gost.h>
172 #include "bytestring.h"
174 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
);
179 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
181 int new_state
, state
, skip
= 0;
186 if (s
->internal
->info_callback
!= NULL
)
187 cb
= s
->internal
->info_callback
;
188 else if (s
->ctx
->internal
->info_callback
!= NULL
)
189 cb
= s
->ctx
->internal
->info_callback
;
191 s
->internal
->in_handshake
++;
192 if (!SSL_in_init(s
) || SSL_in_before(s
))
196 state
= s
->internal
->state
;
198 switch (s
->internal
->state
) {
199 case SSL_ST_RENEGOTIATE
:
200 s
->internal
->renegotiate
= 1;
201 s
->internal
->state
= SSL_ST_CONNECT
;
202 s
->ctx
->internal
->stats
.sess_connect_renegotiate
++;
206 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
207 case SSL_ST_OK
|SSL_ST_CONNECT
:
211 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
213 if ((s
->version
& 0xff00 ) != 0x0300) {
214 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
219 /* s->version=SSL3_VERSION; */
220 s
->internal
->type
= SSL_ST_CONNECT
;
222 if (!ssl3_setup_init_buffer(s
)) {
226 if (!ssl3_setup_buffers(s
)) {
230 if (!ssl_init_wbio_buffer(s
, 0)) {
235 /* don't push the buffering BIO quite yet */
237 if (!tls1_init_finished_mac(s
)) {
242 s
->internal
->state
= SSL3_ST_CW_CLNT_HELLO_A
;
243 s
->ctx
->internal
->stats
.sess_connect
++;
244 s
->internal
->init_num
= 0;
247 case SSL3_ST_CW_CLNT_HELLO_A
:
248 case SSL3_ST_CW_CLNT_HELLO_B
:
250 s
->internal
->shutdown
= 0;
251 ret
= ssl3_client_hello(s
);
254 s
->internal
->state
= SSL3_ST_CR_SRVR_HELLO_A
;
255 s
->internal
->init_num
= 0;
257 /* turn on buffering for the next lot of output */
258 if (s
->bbio
!= s
->wbio
)
259 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
263 case SSL3_ST_CR_SRVR_HELLO_A
:
264 case SSL3_ST_CR_SRVR_HELLO_B
:
265 ret
= ssl3_get_server_hello(s
);
269 if (s
->internal
->hit
) {
270 s
->internal
->state
= SSL3_ST_CR_FINISHED_A
;
271 if (s
->internal
->tlsext_ticket_expected
) {
272 /* receive renewed session ticket */
273 s
->internal
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
276 s
->internal
->state
= SSL3_ST_CR_CERT_A
;
277 s
->internal
->init_num
= 0;
280 case SSL3_ST_CR_CERT_A
:
281 case SSL3_ST_CR_CERT_B
:
282 ret
= ssl3_check_finished(s
);
286 s
->internal
->hit
= 1;
287 if (s
->internal
->tlsext_ticket_expected
)
288 s
->internal
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
290 s
->internal
->state
= SSL3_ST_CR_FINISHED_A
;
291 s
->internal
->init_num
= 0;
294 /* Check if it is anon DH/ECDH. */
295 if (!(S3I(s
)->tmp
.new_cipher
->algorithm_auth
&
297 ret
= ssl3_get_server_certificate(s
);
300 if (s
->internal
->tlsext_status_expected
)
301 s
->internal
->state
= SSL3_ST_CR_CERT_STATUS_A
;
303 s
->internal
->state
= SSL3_ST_CR_KEY_EXCH_A
;
306 s
->internal
->state
= SSL3_ST_CR_KEY_EXCH_A
;
308 s
->internal
->init_num
= 0;
311 case SSL3_ST_CR_KEY_EXCH_A
:
312 case SSL3_ST_CR_KEY_EXCH_B
:
313 ret
= ssl3_get_server_key_exchange(s
);
316 s
->internal
->state
= SSL3_ST_CR_CERT_REQ_A
;
317 s
->internal
->init_num
= 0;
320 * At this point we check that we have the
321 * required stuff from the server.
323 if (!ssl3_check_cert_and_algorithm(s
)) {
329 case SSL3_ST_CR_CERT_REQ_A
:
330 case SSL3_ST_CR_CERT_REQ_B
:
331 ret
= ssl3_get_certificate_request(s
);
334 s
->internal
->state
= SSL3_ST_CR_SRVR_DONE_A
;
335 s
->internal
->init_num
= 0;
338 case SSL3_ST_CR_SRVR_DONE_A
:
339 case SSL3_ST_CR_SRVR_DONE_B
:
340 ret
= ssl3_get_server_done(s
);
343 if (S3I(s
)->tmp
.cert_req
)
344 s
->internal
->state
= SSL3_ST_CW_CERT_A
;
346 s
->internal
->state
= SSL3_ST_CW_KEY_EXCH_A
;
347 s
->internal
->init_num
= 0;
351 case SSL3_ST_CW_CERT_A
:
352 case SSL3_ST_CW_CERT_B
:
353 case SSL3_ST_CW_CERT_C
:
354 case SSL3_ST_CW_CERT_D
:
355 ret
= ssl3_send_client_certificate(s
);
358 s
->internal
->state
= SSL3_ST_CW_KEY_EXCH_A
;
359 s
->internal
->init_num
= 0;
362 case SSL3_ST_CW_KEY_EXCH_A
:
363 case SSL3_ST_CW_KEY_EXCH_B
:
364 ret
= ssl3_send_client_key_exchange(s
);
368 * EAY EAY EAY need to check for DH fix cert
372 * For TLS, cert_req is set to 2, so a cert chain
373 * of nothing is sent, but no verify packet is sent
376 * XXX: For now, we do not support client
377 * authentication in ECDH cipher suites with
378 * ECDH (rather than ECDSA) certificates.
379 * We need to skip the certificate verify
380 * message when client's ECDH public key is sent
381 * inside the client certificate.
383 if (S3I(s
)->tmp
.cert_req
== 1) {
384 s
->internal
->state
= SSL3_ST_CW_CERT_VRFY_A
;
386 s
->internal
->state
= SSL3_ST_CW_CHANGE_A
;
387 S3I(s
)->change_cipher_spec
= 0;
389 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
390 s
->internal
->state
= SSL3_ST_CW_CHANGE_A
;
391 S3I(s
)->change_cipher_spec
= 0;
394 s
->internal
->init_num
= 0;
397 case SSL3_ST_CW_CERT_VRFY_A
:
398 case SSL3_ST_CW_CERT_VRFY_B
:
399 ret
= ssl3_send_client_verify(s
);
402 s
->internal
->state
= SSL3_ST_CW_CHANGE_A
;
403 s
->internal
->init_num
= 0;
404 S3I(s
)->change_cipher_spec
= 0;
407 case SSL3_ST_CW_CHANGE_A
:
408 case SSL3_ST_CW_CHANGE_B
:
409 ret
= ssl3_send_change_cipher_spec(s
,
410 SSL3_ST_CW_CHANGE_A
, SSL3_ST_CW_CHANGE_B
);
414 if (S3I(s
)->next_proto_neg_seen
)
415 s
->internal
->state
= SSL3_ST_CW_NEXT_PROTO_A
;
417 s
->internal
->state
= SSL3_ST_CW_FINISHED_A
;
418 s
->internal
->init_num
= 0;
420 s
->session
->cipher
= S3I(s
)->tmp
.new_cipher
;
421 if (!tls1_setup_key_block(s
)) {
426 if (!tls1_change_cipher_state(s
,
427 SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
434 case SSL3_ST_CW_NEXT_PROTO_A
:
435 case SSL3_ST_CW_NEXT_PROTO_B
:
436 ret
= ssl3_send_next_proto(s
);
439 s
->internal
->state
= SSL3_ST_CW_FINISHED_A
;
442 case SSL3_ST_CW_FINISHED_A
:
443 case SSL3_ST_CW_FINISHED_B
:
444 ret
= ssl3_send_finished(s
, SSL3_ST_CW_FINISHED_A
,
445 SSL3_ST_CW_FINISHED_B
,
446 TLS_MD_CLIENT_FINISH_CONST
,
447 TLS_MD_CLIENT_FINISH_CONST_SIZE
);
450 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
451 s
->internal
->state
= SSL3_ST_CW_FLUSH
;
454 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
455 if (s
->internal
->hit
) {
456 S3I(s
)->tmp
.next_state
= SSL_ST_OK
;
458 SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
459 s
->internal
->state
= SSL_ST_OK
;
460 s
->s3
->flags
|=SSL3_FLAGS_POP_BUFFER
;
461 S3I(s
)->delay_buf_pop_ret
= 0;
464 /* Allow NewSessionTicket if ticket expected */
465 if (s
->internal
->tlsext_ticket_expected
)
466 S3I(s
)->tmp
.next_state
=
467 SSL3_ST_CR_SESSION_TICKET_A
;
470 S3I(s
)->tmp
.next_state
= SSL3_ST_CR_FINISHED_A
;
472 s
->internal
->init_num
= 0;
475 case SSL3_ST_CR_SESSION_TICKET_A
:
476 case SSL3_ST_CR_SESSION_TICKET_B
:
477 ret
= ssl3_get_new_session_ticket(s
);
480 s
->internal
->state
= SSL3_ST_CR_FINISHED_A
;
481 s
->internal
->init_num
= 0;
484 case SSL3_ST_CR_CERT_STATUS_A
:
485 case SSL3_ST_CR_CERT_STATUS_B
:
486 ret
= ssl3_get_cert_status(s
);
489 s
->internal
->state
= SSL3_ST_CR_KEY_EXCH_A
;
490 s
->internal
->init_num
= 0;
493 case SSL3_ST_CR_FINISHED_A
:
494 case SSL3_ST_CR_FINISHED_B
:
495 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
496 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
497 SSL3_ST_CR_FINISHED_B
);
501 if (s
->internal
->hit
)
502 s
->internal
->state
= SSL3_ST_CW_CHANGE_A
;
504 s
->internal
->state
= SSL_ST_OK
;
505 s
->internal
->init_num
= 0;
508 case SSL3_ST_CW_FLUSH
:
509 s
->internal
->rwstate
= SSL_WRITING
;
510 if (BIO_flush(s
->wbio
) <= 0) {
514 s
->internal
->rwstate
= SSL_NOTHING
;
515 s
->internal
->state
= S3I(s
)->tmp
.next_state
;
519 /* clean a few things up */
520 tls1_cleanup_key_block(s
);
522 BUF_MEM_free(s
->internal
->init_buf
);
523 s
->internal
->init_buf
= NULL
;
526 * If we are not 'joining' the last two packets,
527 * remove the buffering now
529 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
530 ssl_free_wbio_buffer(s
);
531 /* else do it later in ssl3_write */
533 s
->internal
->init_num
= 0;
534 s
->internal
->renegotiate
= 0;
535 s
->internal
->new_session
= 0;
537 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
538 if (s
->internal
->hit
)
539 s
->ctx
->internal
->stats
.sess_hit
++;
543 s
->internal
->handshake_func
= ssl3_connect
;
544 s
->ctx
->internal
->stats
.sess_connect_good
++;
547 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
553 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
559 /* did we do anything */
560 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
561 if (s
->internal
->debug
) {
562 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
566 if ((cb
!= NULL
) && (s
->internal
->state
!= state
)) {
567 new_state
= s
->internal
->state
;
568 s
->internal
->state
= state
;
569 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
570 s
->internal
->state
= new_state
;
577 s
->internal
->in_handshake
--;
579 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
585 ssl3_client_hello(SSL
*s
)
587 unsigned char *bufend
, *p
, *d
;
588 uint16_t max_version
;
592 bufend
= (unsigned char *)s
->internal
->init_buf
->data
+ SSL3_RT_MAX_PLAIN_LENGTH
;
594 if (s
->internal
->state
== SSL3_ST_CW_CLNT_HELLO_A
) {
595 SSL_SESSION
*sess
= s
->session
;
597 if (ssl_supported_version_range(s
, NULL
, &max_version
) != 1) {
598 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
601 s
->client_version
= s
->version
= max_version
;
603 if ((sess
== NULL
) ||
604 (sess
->ssl_version
!= s
->version
) ||
605 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
606 (sess
->internal
->not_resumable
)) {
607 if (!ssl_get_new_session(s
, 0))
610 /* else use the pre-loaded session */
613 * If a DTLS ClientHello message is being resent after a
614 * HelloVerifyRequest, we must retain the original client
617 if (!SSL_IS_DTLS(s
) || D1I(s
)->send_cookie
== 0)
618 arc4random_buf(s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
620 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_CLIENT_HELLO
);
623 * Version indicates the negotiated version: for example from
624 * an SSLv2/v3 compatible client hello). The client_version
625 * field is the maximum version we permit and it is also
626 * used in RSA encrypted premaster secrets. Some servers can
627 * choke if we initially report a higher version then
628 * renegotiate to a lower one in the premaster secret. This
629 * didn't happen with TLS 1.0 as most servers supported it
630 * but it can with TLS 1.1 or later if the server only supports
633 * Possible scenario with previous logic:
634 * 1. Client hello indicates TLS 1.2
635 * 2. Server hello says TLS 1.0
636 * 3. RSA encrypted premaster secret uses 1.2.
637 * 4. Handhaked proceeds using TLS 1.0.
638 * 5. Server sends hello request to renegotiate.
639 * 6. Client hello indicates TLS v1.0 as we now
640 * know that is maximum server supports.
641 * 7. Server chokes on RSA encrypted premaster secret
642 * containing version 1.0.
644 * For interoperability it should be OK to always use the
645 * maximum version we support in client hello and then rely
646 * on the checking of version to ensure the servers isn't
647 * being inconsistent: for example initially negotiating with
648 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
649 * client_version in client hello and not resetting it to
650 * the negotiated version.
653 *(p
++) = s
->client_version
>> 8;
654 *(p
++) = s
->client_version
& 0xff;
657 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
658 p
+= SSL3_RANDOM_SIZE
;
661 if (s
->internal
->new_session
)
664 i
= s
->session
->session_id_length
;
667 if (i
> (int)sizeof(s
->session
->session_id
)) {
668 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
671 memcpy(p
, s
->session
->session_id
, i
);
676 if (SSL_IS_DTLS(s
)) {
677 if (D1I(s
)->cookie_len
> sizeof(D1I(s
)->cookie
)) {
678 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
681 *(p
++) = D1I(s
)->cookie_len
;
682 memcpy(p
, D1I(s
)->cookie
, D1I(s
)->cookie_len
);
683 p
+= D1I(s
)->cookie_len
;
686 /* Ciphers supported */
687 if (!ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &p
[2],
688 bufend
- &p
[2], &outlen
))
691 SSLerror(s
, SSL_R_NO_CIPHERS_AVAILABLE
);
697 /* add in (no) COMPRESSION */
699 *(p
++) = 0; /* Add the NULL method */
702 if ((p
= ssl_add_clienthello_tlsext(s
, p
, bufend
)) == NULL
) {
703 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
707 ssl3_handshake_msg_finish(s
, p
- d
);
709 s
->internal
->state
= SSL3_ST_CW_CLNT_HELLO_B
;
712 /* SSL3_ST_CW_CLNT_HELLO_B */
713 return (ssl3_handshake_write(s
));
720 ssl3_get_server_hello(SSL
*s
)
722 CBS cbs
, server_random
, session_id
;
723 uint16_t server_version
, cipher_suite
;
724 uint16_t min_version
, max_version
;
725 uint8_t compression_method
;
726 STACK_OF(SSL_CIPHER
) *sk
;
727 const SSL_CIPHER
*cipher
;
728 const SSL_METHOD
*method
;
735 s
->internal
->first_packet
= 1;
736 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_HELLO_A
,
737 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, /* ?? */ &ok
);
740 s
->internal
->first_packet
= 0;
745 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
747 if (SSL_IS_DTLS(s
)) {
748 if (S3I(s
)->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
749 if (D1I(s
)->send_cookie
== 0) {
750 S3I(s
)->tmp
.reuse_message
= 1;
753 /* Already sent a cookie. */
754 al
= SSL_AD_UNEXPECTED_MESSAGE
;
755 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
761 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
762 al
= SSL_AD_UNEXPECTED_MESSAGE
;
763 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
767 if (!CBS_get_u16(&cbs
, &server_version
))
770 if (ssl_supported_version_range(s
, &min_version
, &max_version
) != 1) {
771 SSLerror(s
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
775 if (server_version
< min_version
|| server_version
> max_version
) {
776 SSLerror(s
, SSL_R_WRONG_SSL_VERSION
);
777 s
->version
= (s
->version
& 0xff00) | (server_version
& 0xff);
778 al
= SSL_AD_PROTOCOL_VERSION
;
781 s
->version
= server_version
;
783 if ((method
= tls1_get_client_method(server_version
)) == NULL
)
784 method
= dtls1_get_client_method(server_version
);
785 if (method
== NULL
) {
786 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
792 if (!CBS_get_bytes(&cbs
, &server_random
, SSL3_RANDOM_SIZE
))
794 if (!CBS_write_bytes(&server_random
, s
->s3
->server_random
,
795 sizeof(s
->s3
->server_random
), NULL
))
799 if (!CBS_get_u8_length_prefixed(&cbs
, &session_id
))
802 if ((CBS_len(&session_id
) > sizeof(s
->session
->session_id
)) ||
803 (CBS_len(&session_id
) > SSL3_SESSION_ID_SIZE
)) {
804 al
= SSL_AD_ILLEGAL_PARAMETER
;
805 SSLerror(s
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
810 if (!CBS_get_u16(&cbs
, &cipher_suite
))
814 * Check if we want to resume the session based on external
817 if (s
->internal
->tls_session_secret_cb
) {
818 SSL_CIPHER
*pref_cipher
= NULL
;
819 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
820 if (s
->internal
->tls_session_secret_cb(s
, s
->session
->master_key
,
821 &s
->session
->master_key_length
, NULL
, &pref_cipher
,
822 s
->internal
->tls_session_secret_cb_arg
)) {
823 s
->session
->cipher
= pref_cipher
? pref_cipher
:
824 ssl3_get_cipher_by_value(cipher_suite
);
825 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
829 if (s
->session
->session_id_length
!= 0 &&
830 CBS_mem_equal(&session_id
, s
->session
->session_id
,
831 s
->session
->session_id_length
)) {
832 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
||
833 timingsafe_memcmp(s
->session
->sid_ctx
,
834 s
->sid_ctx
, s
->sid_ctx_length
) != 0) {
835 /* actually a client application bug */
836 al
= SSL_AD_ILLEGAL_PARAMETER
;
837 SSLerror(s
, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
840 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
841 s
->internal
->hit
= 1;
843 /* a miss or crap from the other end */
845 /* If we were trying for session-id reuse, make a new
846 * SSL_SESSION so we don't stuff up other people */
847 s
->internal
->hit
= 0;
848 if (s
->session
->session_id_length
> 0) {
849 if (!ssl_get_new_session(s
, 0)) {
850 al
= SSL_AD_INTERNAL_ERROR
;
856 * XXX - improve the handling for the case where there is a
857 * zero length session identifier.
859 if (!CBS_write_bytes(&session_id
, s
->session
->session_id
,
860 sizeof(s
->session
->session_id
), &outlen
))
862 s
->session
->session_id_length
= outlen
;
864 s
->session
->ssl_version
= s
->version
;
867 if ((cipher
= ssl3_get_cipher_by_value(cipher_suite
)) == NULL
) {
868 al
= SSL_AD_ILLEGAL_PARAMETER
;
869 SSLerror(s
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
873 /* TLS v1.2 only ciphersuites require v1.2 or later. */
874 if ((cipher
->algorithm_ssl
& SSL_TLSV1_2
) &&
875 (TLS1_get_version(s
) < TLS1_2_VERSION
)) {
876 al
= SSL_AD_ILLEGAL_PARAMETER
;
877 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
881 sk
= ssl_get_ciphers_by_id(s
);
882 i
= sk_SSL_CIPHER_find(sk
, cipher
);
884 /* we did not say we would use this cipher */
885 al
= SSL_AD_ILLEGAL_PARAMETER
;
886 SSLerror(s
, SSL_R_WRONG_CIPHER_RETURNED
);
891 * Depending on the session caching (internal/external), the cipher
892 * and/or cipher_id values may not be set. Make sure that
893 * cipher_id is set and use it for comparison.
895 if (s
->session
->cipher
)
896 s
->session
->cipher_id
= s
->session
->cipher
->id
;
897 if (s
->internal
->hit
&& (s
->session
->cipher_id
!= cipher
->id
)) {
898 al
= SSL_AD_ILLEGAL_PARAMETER
;
899 SSLerror(s
, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
902 S3I(s
)->tmp
.new_cipher
= cipher
;
904 if (!tls1_handshake_hash_init(s
))
908 * Don't digest cached records if no sigalgs: we may need them for
909 * client authentication.
911 alg_k
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
912 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) &&
913 !tls1_digest_cached_records(s
)) {
914 al
= SSL_AD_INTERNAL_ERROR
;
918 if (!CBS_get_u8(&cbs
, &compression_method
))
921 if (compression_method
!= 0) {
922 al
= SSL_AD_ILLEGAL_PARAMETER
;
923 SSLerror(s
, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
927 /* TLS extensions. */
928 p
= (unsigned char *)CBS_data(&cbs
);
929 if (!ssl_parse_serverhello_tlsext(s
, &p
, CBS_len(&cbs
), &al
)) {
930 /* 'al' set by ssl_parse_serverhello_tlsext */
931 SSLerror(s
, SSL_R_PARSE_TLSEXT
);
934 if (ssl_check_serverhello_tlsext(s
) <= 0) {
935 SSLerror(s
, SSL_R_SERVERHELLO_TLSEXT
);
939 /* See if any data remains... */
940 if (p
- CBS_data(&cbs
) != CBS_len(&cbs
))
946 /* wrong packet length */
947 al
= SSL_AD_DECODE_ERROR
;
948 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
950 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
956 ssl3_get_server_certificate(SSL
*s
)
958 int al
, i
, ok
, ret
= -1;
962 const unsigned char *q
;
963 STACK_OF(X509
) *sk
= NULL
;
965 EVP_PKEY
*pkey
= NULL
;
967 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
968 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
973 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
974 S3I(s
)->tmp
.reuse_message
= 1;
978 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
979 al
= SSL_AD_UNEXPECTED_MESSAGE
;
980 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
985 if ((sk
= sk_X509_new_null()) == NULL
) {
986 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
993 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
994 if (CBS_len(&cbs
) < 3)
997 if (!CBS_get_u24_length_prefixed(&cbs
, &cert_list
) ||
998 CBS_len(&cbs
) != 0) {
999 al
= SSL_AD_DECODE_ERROR
;
1000 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1004 while (CBS_len(&cert_list
) > 0) {
1007 if (CBS_len(&cert_list
) < 3)
1009 if (!CBS_get_u24_length_prefixed(&cert_list
, &cert
)) {
1010 al
= SSL_AD_DECODE_ERROR
;
1011 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1015 q
= CBS_data(&cert
);
1016 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
1018 al
= SSL_AD_BAD_CERTIFICATE
;
1019 SSLerror(s
, ERR_R_ASN1_LIB
);
1022 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
1023 al
= SSL_AD_DECODE_ERROR
;
1024 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
1027 if (!sk_X509_push(sk
, x
)) {
1028 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1034 i
= ssl_verify_cert_chain(s
, sk
);
1035 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)) {
1036 al
= ssl_verify_alarm_type(s
->verify_result
);
1037 SSLerror(s
, SSL_R_CERTIFICATE_VERIFY_FAILED
);
1041 ERR_clear_error(); /* but we keep s->verify_result */
1043 sc
= ssl_sess_cert_new();
1046 ssl_sess_cert_free(SSI(s
)->sess_cert
);
1047 SSI(s
)->sess_cert
= sc
;
1049 sc
->cert_chain
= sk
;
1051 * Inconsistency alert: cert_chain does include the peer's
1052 * certificate, which we don't include in s3_srvr.c
1054 x
= sk_X509_value(sk
, 0);
1056 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1058 pkey
= X509_get_pubkey(x
);
1060 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1063 SSLerror(s
, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1067 i
= ssl_cert_type(x
, pkey
);
1071 SSLerror(s
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1075 sc
->peer_cert_type
= i
;
1076 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1078 * Why would the following ever happen?
1079 * We just created sc a couple of lines ago.
1081 X509_free(sc
->peer_pkeys
[i
].x509
);
1082 sc
->peer_pkeys
[i
].x509
= x
;
1083 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1085 X509_free(s
->session
->peer
);
1086 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1087 s
->session
->peer
= x
;
1088 s
->session
->verify_result
= s
->verify_result
;
1095 /* wrong packet length */
1096 al
= SSL_AD_DECODE_ERROR
;
1097 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1099 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1102 EVP_PKEY_free(pkey
);
1104 sk_X509_pop_free(sk
, X509_free
);
1110 ssl3_get_server_kex_dhe(SSL
*s
, EVP_PKEY
**pkey
, unsigned char **pp
, long *nn
)
1112 CBS cbs
, dhp
, dhg
, dhpk
;
1113 BN_CTX
*bn_ctx
= NULL
;
1114 SESS_CERT
*sc
= NULL
;
1119 alg_a
= S3I(s
)->tmp
.new_cipher
->algorithm_auth
;
1120 sc
= SSI(s
)->sess_cert
;
1125 CBS_init(&cbs
, *pp
, *nn
);
1127 if ((dh
= DH_new()) == NULL
) {
1128 SSLerror(s
, ERR_R_DH_LIB
);
1132 if (!CBS_get_u16_length_prefixed(&cbs
, &dhp
))
1134 if ((dh
->p
= BN_bin2bn(CBS_data(&dhp
), CBS_len(&dhp
), NULL
)) == NULL
) {
1135 SSLerror(s
, ERR_R_BN_LIB
);
1139 if (!CBS_get_u16_length_prefixed(&cbs
, &dhg
))
1141 if ((dh
->g
= BN_bin2bn(CBS_data(&dhg
), CBS_len(&dhg
), NULL
)) == NULL
) {
1142 SSLerror(s
, ERR_R_BN_LIB
);
1146 if (!CBS_get_u16_length_prefixed(&cbs
, &dhpk
))
1148 if ((dh
->pub_key
= BN_bin2bn(CBS_data(&dhpk
), CBS_len(&dhpk
),
1150 SSLerror(s
, ERR_R_BN_LIB
);
1155 * Check the strength of the DH key just constructed.
1156 * Discard keys weaker than 1024 bits.
1158 if (DH_size(dh
) < 1024 / 8) {
1159 SSLerror(s
, SSL_R_BAD_DH_P_LENGTH
);
1163 if (alg_a
& SSL_aRSA
)
1164 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1165 else if (alg_a
& SSL_aDSS
)
1166 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].x509
);
1168 /* XXX - Anonymous DH, so no certificate or pkey. */
1171 sc
->peer_dh_tmp
= dh
;
1173 *nn
= CBS_len(&cbs
);
1174 *pp
= (unsigned char *)CBS_data(&cbs
);
1179 al
= SSL_AD_DECODE_ERROR
;
1180 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1181 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1185 BN_CTX_free(bn_ctx
);
1191 ssl3_get_server_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1193 const EC_GROUP
*group
;
1194 EC_GROUP
*ngroup
= NULL
;
1195 EC_POINT
*point
= NULL
;
1196 BN_CTX
*bn_ctx
= NULL
;
1197 EC_KEY
*ecdh
= NULL
;
1201 * Extract the server's ephemeral ECDH public key.
1204 if ((ecdh
= EC_KEY_new()) == NULL
) {
1205 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1209 if ((ngroup
= EC_GROUP_new_by_curve_name(nid
)) == NULL
) {
1210 SSLerror(s
, ERR_R_EC_LIB
);
1213 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1214 SSLerror(s
, ERR_R_EC_LIB
);
1218 group
= EC_KEY_get0_group(ecdh
);
1220 if ((point
= EC_POINT_new(group
)) == NULL
||
1221 (bn_ctx
= BN_CTX_new()) == NULL
) {
1222 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1226 if (EC_POINT_oct2point(group
, point
, CBS_data(public),
1227 CBS_len(public), bn_ctx
) == 0) {
1228 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1229 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1233 EC_KEY_set_public_key(ecdh
, point
);
1234 sc
->peer_ecdh_tmp
= ecdh
;
1240 BN_CTX_free(bn_ctx
);
1241 EC_GROUP_free(ngroup
);
1242 EC_POINT_free(point
);
1249 ssl3_get_server_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, int nid
, CBS
*public)
1253 if (nid
!= NID_X25519
) {
1254 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1258 if (CBS_len(public) != X25519_KEY_LENGTH
) {
1259 SSLerror(s
, SSL_R_BAD_ECPOINT
);
1260 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1264 if (!CBS_stow(public, &sc
->peer_x25519_tmp
, &outlen
)) {
1265 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1276 ssl3_get_server_kex_ecdhe(SSL
*s
, EVP_PKEY
**pkey
, unsigned char **pp
, long *nn
)
1286 alg_a
= S3I(s
)->tmp
.new_cipher
->algorithm_auth
;
1287 sc
= SSI(s
)->sess_cert
;
1292 CBS_init(&cbs
, *pp
, *nn
);
1294 /* Only named curves are supported. */
1295 if (!CBS_get_u8(&cbs
, &curve_type
) ||
1296 curve_type
!= NAMED_CURVE_TYPE
||
1297 !CBS_get_u16(&cbs
, &curve_id
)) {
1298 al
= SSL_AD_DECODE_ERROR
;
1299 SSLerror(s
, SSL_R_LENGTH_TOO_SHORT
);
1304 * Check that the curve is one of our preferences - if it is not,
1305 * the server has sent us an invalid curve.
1307 if (tls1_check_curve(s
, curve_id
) != 1) {
1308 al
= SSL_AD_DECODE_ERROR
;
1309 SSLerror(s
, SSL_R_WRONG_CURVE
);
1313 if ((nid
= tls1_ec_curve_id2nid(curve_id
)) == 0) {
1314 al
= SSL_AD_INTERNAL_ERROR
;
1315 SSLerror(s
, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1319 if (!CBS_get_u8_length_prefixed(&cbs
, &public))
1322 if (nid
== NID_X25519
) {
1323 if (ssl3_get_server_kex_ecdhe_ecx(s
, sc
, nid
, &public) != 1)
1326 if (ssl3_get_server_kex_ecdhe_ecp(s
, sc
, nid
, &public) != 1)
1331 * The ECC/TLS specification does not mention the use of DSA to sign
1332 * ECParameters in the server key exchange message. We do support RSA
1335 if (alg_a
& SSL_aRSA
)
1336 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1337 else if (alg_a
& SSL_aECDSA
)
1338 *pkey
= X509_get_pubkey(sc
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1340 /* XXX - Anonymous ECDH, so no certificate or pkey. */
1343 *nn
= CBS_len(&cbs
);
1344 *pp
= (unsigned char *)CBS_data(&cbs
);
1349 al
= SSL_AD_DECODE_ERROR
;
1350 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1353 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1360 ssl3_get_server_key_exchange(SSL
*s
)
1362 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
*2];
1364 unsigned char *param
, *p
;
1365 int al
, i
, j
, param_len
, ok
;
1366 long n
, alg_k
, alg_a
;
1367 EVP_PKEY
*pkey
= NULL
;
1368 const EVP_MD
*md
= NULL
;
1371 alg_k
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
1372 alg_a
= S3I(s
)->tmp
.new_cipher
->algorithm_auth
;
1375 * Use same message size as in ssl3_get_certificate_request()
1376 * as ServerKeyExchange message may be skipped.
1378 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_KEY_EXCH_A
,
1379 SSL3_ST_CR_KEY_EXCH_B
, -1, s
->internal
->max_cert_list
, &ok
);
1383 EVP_MD_CTX_init(&md_ctx
);
1385 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1387 * Do not skip server key exchange if this cipher suite uses
1390 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
1391 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1392 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1396 S3I(s
)->tmp
.reuse_message
= 1;
1397 EVP_MD_CTX_cleanup(&md_ctx
);
1401 if (SSI(s
)->sess_cert
!= NULL
) {
1402 DH_free(SSI(s
)->sess_cert
->peer_dh_tmp
);
1403 SSI(s
)->sess_cert
->peer_dh_tmp
= NULL
;
1405 EC_KEY_free(SSI(s
)->sess_cert
->peer_ecdh_tmp
);
1406 SSI(s
)->sess_cert
->peer_ecdh_tmp
= NULL
;
1408 free(SSI(s
)->sess_cert
->peer_x25519_tmp
);
1409 SSI(s
)->sess_cert
->peer_x25519_tmp
= NULL
;
1411 SSI(s
)->sess_cert
= ssl_sess_cert_new();
1412 if (SSI(s
)->sess_cert
== NULL
)
1416 param
= p
= (unsigned char *)s
->internal
->init_msg
;
1419 if (alg_k
& SSL_kDHE
) {
1420 if (ssl3_get_server_kex_dhe(s
, &pkey
, &p
, &n
) != 1)
1422 } else if (alg_k
& SSL_kECDHE
) {
1423 if (ssl3_get_server_kex_ecdhe(s
, &pkey
, &p
, &n
) != 1)
1425 } else if (alg_k
!= 0) {
1426 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1427 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
1431 param_len
= param_len
- n
;
1433 /* if it was signed, check the signature */
1435 if (SSL_USE_SIGALGS(s
)) {
1436 int sigalg
= tls12_get_sigid(pkey
);
1437 /* Should never happen */
1439 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1443 * Check key type is consistent
1448 if (sigalg
!= (int)p
[1]) {
1449 SSLerror(s
, SSL_R_WRONG_SIGNATURE_TYPE
);
1450 al
= SSL_AD_DECODE_ERROR
;
1453 md
= tls12_get_hash(p
[0]);
1455 SSLerror(s
, SSL_R_UNKNOWN_DIGEST
);
1456 al
= SSL_AD_DECODE_ERROR
;
1468 j
= EVP_PKEY_size(pkey
);
1470 if (i
!= n
|| n
> j
) {
1471 /* wrong packet length */
1472 al
= SSL_AD_DECODE_ERROR
;
1473 SSLerror(s
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1477 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1480 if (!EVP_DigestInit_ex(&md_ctx
, EVP_md5_sha1(), NULL
)) {
1481 al
= SSL_AD_INTERNAL_ERROR
;
1484 EVP_DigestUpdate(&md_ctx
, s
->s3
->client_random
,
1486 EVP_DigestUpdate(&md_ctx
, s
->s3
->server_random
,
1488 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1489 EVP_DigestFinal_ex(&md_ctx
, q
, (unsigned int *)&i
);
1492 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
,
1493 p
, n
, pkey
->pkey
.rsa
);
1495 al
= SSL_AD_DECRYPT_ERROR
;
1496 SSLerror(s
, SSL_R_BAD_RSA_DECRYPT
);
1501 al
= SSL_AD_DECRYPT_ERROR
;
1502 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1506 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
1507 EVP_VerifyUpdate(&md_ctx
, s
->s3
->client_random
,
1509 EVP_VerifyUpdate(&md_ctx
, s
->s3
->server_random
,
1511 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1512 if (EVP_VerifyFinal(&md_ctx
, p
,(int)n
, pkey
) <= 0) {
1514 al
= SSL_AD_DECRYPT_ERROR
;
1515 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
1520 /* aNULL does not need public keys. */
1521 if (!(alg_a
& SSL_aNULL
)) {
1522 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1525 /* still data left over */
1527 al
= SSL_AD_DECODE_ERROR
;
1528 SSLerror(s
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1533 EVP_PKEY_free(pkey
);
1534 EVP_MD_CTX_cleanup(&md_ctx
);
1539 /* wrong packet length */
1540 al
= SSL_AD_DECODE_ERROR
;
1541 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1544 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1547 EVP_PKEY_free(pkey
);
1549 EVP_MD_CTX_cleanup(&md_ctx
);
1555 ssl3_get_certificate_request(SSL
*s
)
1560 CBS cert_request
, ctypes
, rdn_list
;
1561 X509_NAME
*xn
= NULL
;
1562 const unsigned char *q
;
1563 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1565 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_REQ_A
,
1566 SSL3_ST_CR_CERT_REQ_B
, -1, s
->internal
->max_cert_list
, &ok
);
1571 S3I(s
)->tmp
.cert_req
= 0;
1573 if (S3I(s
)->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1574 S3I(s
)->tmp
.reuse_message
= 1;
1576 * If we get here we don't need any cached handshake records
1577 * as we wont be doing client auth.
1579 if (S3I(s
)->handshake_buffer
) {
1580 if (!tls1_digest_cached_records(s
))
1586 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1587 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1588 SSLerror(s
, SSL_R_WRONG_MESSAGE_TYPE
);
1592 /* TLS does not like anon-DH with client cert */
1593 if (S3I(s
)->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
1594 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1595 SSLerror(s
, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1601 CBS_init(&cert_request
, s
->internal
->init_msg
, n
);
1603 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1604 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1608 /* get the certificate types */
1609 if (!CBS_get_u8(&cert_request
, &ctype_num
))
1612 if (ctype_num
> SSL3_CT_NUMBER
)
1613 ctype_num
= SSL3_CT_NUMBER
;
1614 if (!CBS_get_bytes(&cert_request
, &ctypes
, ctype_num
) ||
1615 !CBS_write_bytes(&ctypes
, (uint8_t *)S3I(s
)->tmp
.ctype
,
1616 sizeof(S3I(s
)->tmp
.ctype
), NULL
)) {
1617 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1621 if (SSL_USE_SIGALGS(s
)) {
1624 if (CBS_len(&cert_request
) < 2) {
1625 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1629 /* Check we have enough room for signature algorithms and
1630 * following length value.
1632 if (!CBS_get_u16_length_prefixed(&cert_request
, &sigalgs
)) {
1633 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1634 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1637 if ((CBS_len(&sigalgs
) & 1) ||
1638 !tls1_process_sigalgs(s
, CBS_data(&sigalgs
),
1639 CBS_len(&sigalgs
))) {
1640 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1641 SSLerror(s
, SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
1646 /* get the CA RDNs */
1647 if (CBS_len(&cert_request
) < 2) {
1648 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1652 if (!CBS_get_u16_length_prefixed(&cert_request
, &rdn_list
) ||
1653 CBS_len(&cert_request
) != 0) {
1654 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1655 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1659 while (CBS_len(&rdn_list
) > 0) {
1662 if (CBS_len(&rdn_list
) < 2) {
1663 SSLerror(s
, SSL_R_DATA_LENGTH_TOO_LONG
);
1667 if (!CBS_get_u16_length_prefixed(&rdn_list
, &rdn
)) {
1668 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1669 SSLerror(s
, SSL_R_CA_DN_TOO_LONG
);
1674 if ((xn
= d2i_X509_NAME(NULL
, &q
, CBS_len(&rdn
))) == NULL
) {
1675 ssl3_send_alert(s
, SSL3_AL_FATAL
,
1676 SSL_AD_DECODE_ERROR
);
1677 SSLerror(s
, ERR_R_ASN1_LIB
);
1681 if (q
!= CBS_data(&rdn
) + CBS_len(&rdn
)) {
1682 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1683 SSLerror(s
, SSL_R_CA_DN_LENGTH_MISMATCH
);
1686 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1687 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1690 xn
= NULL
; /* avoid free in err block */
1693 /* we should setup a certificate to return.... */
1694 S3I(s
)->tmp
.cert_req
= 1;
1695 S3I(s
)->tmp
.ctype_num
= ctype_num
;
1696 sk_X509_NAME_pop_free(S3I(s
)->tmp
.ca_names
, X509_NAME_free
);
1697 S3I(s
)->tmp
.ca_names
= ca_sk
;
1703 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1707 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1712 ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1714 return (X509_NAME_cmp(*a
, *b
));
1718 ssl3_get_new_session_ticket(SSL
*s
)
1720 int ok
, al
, ret
= 0;
1721 uint32_t lifetime_hint
;
1723 CBS cbs
, session_ticket
;
1725 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SESSION_TICKET_A
,
1726 SSL3_ST_CR_SESSION_TICKET_B
, -1, 16384, &ok
);
1730 if (S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) {
1731 S3I(s
)->tmp
.reuse_message
= 1;
1734 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
) {
1735 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1736 SSLerror(s
, SSL_R_BAD_MESSAGE_TYPE
);
1741 al
= SSL_AD_DECODE_ERROR
;
1742 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1746 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
1747 if (!CBS_get_u32(&cbs
, &lifetime_hint
) ||
1748 #if UINT32_MAX > LONG_MAX
1749 lifetime_hint
> LONG_MAX
||
1751 !CBS_get_u16_length_prefixed(&cbs
, &session_ticket
) ||
1752 CBS_len(&cbs
) != 0) {
1753 al
= SSL_AD_DECODE_ERROR
;
1754 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1757 s
->session
->tlsext_tick_lifetime_hint
= (long)lifetime_hint
;
1759 if (!CBS_stow(&session_ticket
, &s
->session
->tlsext_tick
,
1760 &s
->session
->tlsext_ticklen
)) {
1761 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1766 * There are two ways to detect a resumed ticket sesion.
1767 * One is to set an appropriate session ID and then the server
1768 * must return a match in ServerHello. This allows the normal
1769 * client session ID matching to work and we know much
1770 * earlier that the ticket has been accepted.
1772 * The other way is to set zero length session ID when the
1773 * ticket is presented and rely on the handshake to determine
1774 * session resumption.
1776 * We choose the former approach because this fits in with
1777 * assumptions elsewhere in OpenSSL. The session ID is set
1778 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1781 EVP_Digest(CBS_data(&session_ticket
), CBS_len(&session_ticket
),
1782 s
->session
->session_id
, &s
->session
->session_id_length
,
1783 EVP_sha256(), NULL
);
1787 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1793 ssl3_get_cert_status(SSL
*s
)
1795 CBS cert_status
, response
;
1799 uint8_t status_type
;
1801 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_STATUS_A
,
1802 SSL3_ST_CR_CERT_STATUS_B
, SSL3_MT_CERTIFICATE_STATUS
,
1809 /* need at least status type + length */
1810 al
= SSL_AD_DECODE_ERROR
;
1811 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1815 CBS_init(&cert_status
, s
->internal
->init_msg
, n
);
1816 if (!CBS_get_u8(&cert_status
, &status_type
) ||
1817 CBS_len(&cert_status
) < 3) {
1818 /* need at least status type + length */
1819 al
= SSL_AD_DECODE_ERROR
;
1820 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1824 if (status_type
!= TLSEXT_STATUSTYPE_ocsp
) {
1825 al
= SSL_AD_DECODE_ERROR
;
1826 SSLerror(s
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
1830 if (!CBS_get_u24_length_prefixed(&cert_status
, &response
) ||
1831 CBS_len(&cert_status
) != 0) {
1832 al
= SSL_AD_DECODE_ERROR
;
1833 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1837 if (!CBS_stow(&response
, &s
->internal
->tlsext_ocsp_resp
,
1838 &stow_len
) || stow_len
> INT_MAX
) {
1839 s
->internal
->tlsext_ocsp_resplen
= 0;
1840 al
= SSL_AD_INTERNAL_ERROR
;
1841 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1844 s
->internal
->tlsext_ocsp_resplen
= (int)stow_len
;
1846 if (s
->ctx
->internal
->tlsext_status_cb
) {
1848 ret
= s
->ctx
->internal
->tlsext_status_cb(s
,
1849 s
->ctx
->internal
->tlsext_status_arg
);
1851 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
1852 SSLerror(s
, SSL_R_INVALID_STATUS_RESPONSE
);
1856 al
= SSL_AD_INTERNAL_ERROR
;
1857 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1863 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1868 ssl3_get_server_done(SSL
*s
)
1873 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_SRVR_DONE_A
,
1874 SSL3_ST_CR_SRVR_DONE_B
, SSL3_MT_SERVER_DONE
,
1875 30, /* should be very small, like 0 :-) */ &ok
);
1880 /* should contain no data */
1881 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1882 SSLerror(s
, SSL_R_LENGTH_MISMATCH
);
1890 ssl3_send_client_kex_rsa(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1892 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
];
1893 unsigned char *enc_pms
= NULL
;
1894 EVP_PKEY
*pkey
= NULL
;
1900 * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1.
1903 pkey
= X509_get_pubkey(sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1904 if (pkey
== NULL
|| pkey
->type
!= EVP_PKEY_RSA
||
1905 pkey
->pkey
.rsa
== NULL
) {
1906 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1910 pms
[0] = s
->client_version
>> 8;
1911 pms
[1] = s
->client_version
& 0xff;
1912 arc4random_buf(&pms
[2], sizeof(pms
) - 2);
1914 if ((enc_pms
= malloc(RSA_size(pkey
->pkey
.rsa
))) == NULL
) {
1915 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1919 enc_len
= RSA_public_encrypt(sizeof(pms
), pms
, enc_pms
, pkey
->pkey
.rsa
,
1922 SSLerror(s
, SSL_R_BAD_RSA_ENCRYPT
);
1926 if (!CBB_add_u16_length_prefixed(cbb
, &epms
))
1928 if (!CBB_add_bytes(&epms
, enc_pms
, enc_len
))
1930 if (!CBB_flush(cbb
))
1933 s
->session
->master_key_length
=
1934 tls1_generate_master_secret(s
,
1935 s
->session
->master_key
, pms
, sizeof(pms
));
1940 explicit_bzero(pms
, sizeof(pms
));
1941 EVP_PKEY_free(pkey
);
1948 ssl3_send_client_kex_dhe(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
1950 DH
*dh_srvr
= NULL
, *dh_clnt
= NULL
;
1951 unsigned char *key
= NULL
;
1952 int key_size
= 0, key_len
;
1953 unsigned char *data
;
1957 /* Ensure that we have an ephemeral key for DHE. */
1958 if (sess_cert
->peer_dh_tmp
== NULL
) {
1959 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
1960 SSLerror(s
, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
1963 dh_srvr
= sess_cert
->peer_dh_tmp
;
1965 /* Generate a new random key. */
1966 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
1967 SSLerror(s
, ERR_R_DH_LIB
);
1970 if (!DH_generate_key(dh_clnt
)) {
1971 SSLerror(s
, ERR_R_DH_LIB
);
1974 key_size
= DH_size(dh_clnt
);
1975 if ((key
= malloc(key_size
)) == NULL
) {
1976 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1979 key_len
= DH_compute_key(key
, dh_srvr
->pub_key
, dh_clnt
);
1981 SSLerror(s
, ERR_R_DH_LIB
);
1985 /* Generate master key from the result. */
1986 s
->session
->master_key_length
=
1987 tls1_generate_master_secret(s
,
1988 s
->session
->master_key
, key
, key_len
);
1990 if (!CBB_add_u16_length_prefixed(cbb
, &dh_Yc
))
1992 if (!CBB_add_space(&dh_Yc
, &data
, BN_num_bytes(dh_clnt
->pub_key
)))
1994 BN_bn2bin(dh_clnt
->pub_key
, data
);
1995 if (!CBB_flush(cbb
))
2003 explicit_bzero(key
, key_size
);
2010 ssl3_send_client_kex_ecdhe_ecp(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2012 const EC_GROUP
*group
= NULL
;
2013 const EC_POINT
*point
= NULL
;
2014 EC_KEY
*ecdh
= NULL
;
2015 BN_CTX
*bn_ctx
= NULL
;
2016 unsigned char *key
= NULL
;
2017 unsigned char *data
;
2019 int key_size
= 0, key_len
;
2023 if ((group
= EC_KEY_get0_group(sc
->peer_ecdh_tmp
)) == NULL
||
2024 (point
= EC_KEY_get0_public_key(sc
->peer_ecdh_tmp
)) == NULL
) {
2025 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2029 if ((ecdh
= EC_KEY_new()) == NULL
) {
2030 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2034 if (!EC_KEY_set_group(ecdh
, group
)) {
2035 SSLerror(s
, ERR_R_EC_LIB
);
2039 /* Generate a new ECDH key pair. */
2040 if (!(EC_KEY_generate_key(ecdh
))) {
2041 SSLerror(s
, ERR_R_ECDH_LIB
);
2044 if ((key_size
= ECDH_size(ecdh
)) <= 0) {
2045 SSLerror(s
, ERR_R_ECDH_LIB
);
2048 if ((key
= malloc(key_size
)) == NULL
) {
2049 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2051 key_len
= ECDH_compute_key(key
, key_size
, point
, ecdh
, NULL
);
2053 SSLerror(s
, ERR_R_ECDH_LIB
);
2057 /* Generate master key from the result. */
2058 s
->session
->master_key_length
=
2059 tls1_generate_master_secret(s
,
2060 s
->session
->master_key
, key
, key_len
);
2062 encoded_len
= EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2063 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
2064 if (encoded_len
== 0) {
2065 SSLerror(s
, ERR_R_ECDH_LIB
);
2069 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
2070 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2074 /* Encode the public key. */
2075 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2077 if (!CBB_add_space(&ecpoint
, &data
, encoded_len
))
2079 if (EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
2080 POINT_CONVERSION_UNCOMPRESSED
, data
, encoded_len
,
2083 if (!CBB_flush(cbb
))
2090 explicit_bzero(key
, key_size
);
2093 BN_CTX_free(bn_ctx
);
2100 ssl3_send_client_kex_ecdhe_ecx(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2102 uint8_t *public_key
= NULL
, *private_key
= NULL
, *shared_key
= NULL
;
2106 /* Generate X25519 key pair and derive shared key. */
2107 if ((public_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2109 if ((private_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2111 if ((shared_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2113 X25519_keypair(public_key
, private_key
);
2114 if (!X25519(shared_key
, private_key
, sc
->peer_x25519_tmp
))
2117 /* Serialize the public key. */
2118 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
2120 if (!CBB_add_bytes(&ecpoint
, public_key
, X25519_KEY_LENGTH
))
2122 if (!CBB_flush(cbb
))
2125 /* Generate master key from the result. */
2126 s
->session
->master_key_length
=
2127 tls1_generate_master_secret(s
,
2128 s
->session
->master_key
, shared_key
, X25519_KEY_LENGTH
);
2133 if (private_key
!= NULL
)
2134 explicit_bzero(private_key
, X25519_KEY_LENGTH
);
2135 if (shared_key
!= NULL
)
2136 explicit_bzero(shared_key
, X25519_KEY_LENGTH
);
2146 ssl3_send_client_kex_ecdhe(SSL
*s
, SESS_CERT
*sc
, CBB
*cbb
)
2148 if (sc
->peer_x25519_tmp
!= NULL
) {
2149 if (ssl3_send_client_kex_ecdhe_ecx(s
, sc
, cbb
) != 1)
2151 } else if (sc
->peer_ecdh_tmp
!= NULL
) {
2152 if (ssl3_send_client_kex_ecdhe_ecp(s
, sc
, cbb
) != 1)
2155 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2156 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2167 ssl3_send_client_kex_gost(SSL
*s
, SESS_CERT
*sess_cert
, CBB
*cbb
)
2169 unsigned char premaster_secret
[32], shared_ukm
[32], tmp
[256];
2170 EVP_PKEY
*pub_key
= NULL
;
2171 EVP_PKEY_CTX
*pkey_ctx
;
2174 unsigned int md_len
;
2175 EVP_MD_CTX
*ukm_hash
;
2180 /* Get server sertificate PKEY and create ctx from it */
2181 peer_cert
= sess_cert
->peer_pkeys
[SSL_PKEY_GOST01
].x509
;
2182 if (peer_cert
== NULL
) {
2183 SSLerror(s
, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2187 pub_key
= X509_get_pubkey(peer_cert
);
2188 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
, NULL
);
2191 * If we have send a certificate, and certificate key parameters match
2192 * those of server certificate, use certificate key for key exchange.
2193 * Otherwise, generate ephemeral key pair.
2195 EVP_PKEY_encrypt_init(pkey_ctx
);
2197 /* Generate session key. */
2198 arc4random_buf(premaster_secret
, 32);
2201 * If we have client certificate, use its secret as peer key.
2203 if (S3I(s
)->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2204 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2205 s
->cert
->key
->privatekey
) <=0) {
2207 * If there was an error - just ignore it.
2208 * Ephemeral key would be used.
2215 * Compute shared IV and store it in algorithm-specific context data.
2217 ukm_hash
= EVP_MD_CTX_create();
2218 if (ukm_hash
== NULL
) {
2219 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2223 if (ssl_get_algorithm2(s
) & SSL_HANDSHAKE_MAC_GOST94
)
2224 nid
= NID_id_GostR3411_94
;
2226 nid
= NID_id_tc26_gost3411_2012_256
;
2227 if (!EVP_DigestInit(ukm_hash
, EVP_get_digestbynid(nid
)))
2229 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2230 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2231 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2232 EVP_MD_CTX_destroy(ukm_hash
);
2233 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
2234 EVP_PKEY_CTRL_SET_IV
, 8, shared_ukm
) < 0) {
2235 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2240 * Make GOST keytransport blob message, encapsulate it into sequence.
2243 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, premaster_secret
,
2245 SSLerror(s
, SSL_R_LIBRARY_BUG
);
2249 if (!CBB_add_asn1(cbb
, &gostblob
, CBS_ASN1_SEQUENCE
))
2251 if (!CBB_add_bytes(&gostblob
, tmp
, msglen
))
2253 if (!CBB_flush(cbb
))
2256 /* Check if pubkey from client certificate was used. */
2257 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
2259 /* Set flag "skip certificate verify". */
2260 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2262 EVP_PKEY_CTX_free(pkey_ctx
);
2263 s
->session
->master_key_length
=
2264 tls1_generate_master_secret(s
,
2265 s
->session
->master_key
, premaster_secret
, 32);
2270 explicit_bzero(premaster_secret
, sizeof(premaster_secret
));
2271 EVP_PKEY_free(pub_key
);
2277 ssl3_send_client_key_exchange(SSL
*s
)
2279 SESS_CERT
*sess_cert
;
2280 unsigned long alg_k
;
2283 memset(&cbb
, 0, sizeof(cbb
));
2285 if (s
->internal
->state
== SSL3_ST_CW_KEY_EXCH_A
) {
2286 alg_k
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
2288 if ((sess_cert
= SSI(s
)->sess_cert
) == NULL
) {
2289 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2290 SSL_AD_UNEXPECTED_MESSAGE
);
2291 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2295 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &kex
,
2296 SSL3_MT_CLIENT_KEY_EXCHANGE
))
2299 if (alg_k
& SSL_kRSA
) {
2300 if (ssl3_send_client_kex_rsa(s
, sess_cert
, &kex
) != 1)
2302 } else if (alg_k
& SSL_kDHE
) {
2303 if (ssl3_send_client_kex_dhe(s
, sess_cert
, &kex
) != 1)
2305 } else if (alg_k
& SSL_kECDHE
) {
2306 if (ssl3_send_client_kex_ecdhe(s
, sess_cert
, &kex
) != 1)
2308 } else if (alg_k
& SSL_kGOST
) {
2309 if (ssl3_send_client_kex_gost(s
, sess_cert
, &kex
) != 1)
2312 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2313 SSL_AD_HANDSHAKE_FAILURE
);
2314 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2318 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2321 s
->internal
->state
= SSL3_ST_CW_KEY_EXCH_B
;
2324 /* SSL3_ST_CW_KEY_EXCH_B */
2325 return (ssl3_handshake_write(s
));
2334 ssl3_send_client_verify(SSL
*s
)
2337 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
2339 EVP_PKEY_CTX
*pctx
= NULL
;
2345 EVP_MD_CTX_init(&mctx
);
2347 if (s
->internal
->state
== SSL3_ST_CW_CERT_VRFY_A
) {
2348 p
= ssl3_handshake_msg_start(s
, SSL3_MT_CERTIFICATE_VERIFY
);
2351 * Create context from key and test if sha1 is allowed as
2354 pkey
= s
->cert
->key
->privatekey
;
2355 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2356 EVP_PKEY_sign_init(pctx
);
2358 /* XXX - is this needed? */
2359 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) <= 0)
2362 if (!SSL_USE_SIGALGS(s
)) {
2363 if (S3I(s
)->handshake_buffer
) {
2364 if (!tls1_digest_cached_records(s
))
2367 if (!tls1_handshake_hash_value(s
, data
, sizeof(data
),
2373 * For TLS v1.2 send signature algorithm and signature
2374 * using agreed digest and cached handshake records.
2376 if (SSL_USE_SIGALGS(s
)) {
2379 const EVP_MD
*md
= s
->cert
->key
->digest
;
2380 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
,
2382 if (hdatalen
<= 0 ||
2383 !tls12_get_sigandhash(p
, pkey
, md
)) {
2384 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2388 if (!EVP_SignInit_ex(&mctx
, md
, NULL
) ||
2389 !EVP_SignUpdate(&mctx
, hdata
, hdatalen
) ||
2390 !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
2391 SSLerror(s
, ERR_R_EVP_LIB
);
2396 if (!tls1_digest_cached_records(s
))
2398 } else if (pkey
->type
== EVP_PKEY_RSA
) {
2399 if (RSA_sign(NID_md5_sha1
, data
,
2400 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, &(p
[2]),
2401 &u
, pkey
->pkey
.rsa
) <= 0 ) {
2402 SSLerror(s
, ERR_R_RSA_LIB
);
2407 } else if (pkey
->type
== EVP_PKEY_DSA
) {
2408 if (!DSA_sign(pkey
->save_type
,
2409 &(data
[MD5_DIGEST_LENGTH
]),
2410 SHA_DIGEST_LENGTH
, &(p
[2]),
2411 (unsigned int *)&j
, pkey
->pkey
.dsa
)) {
2412 SSLerror(s
, ERR_R_DSA_LIB
);
2417 } else if (pkey
->type
== EVP_PKEY_EC
) {
2418 if (!ECDSA_sign(pkey
->save_type
,
2419 &(data
[MD5_DIGEST_LENGTH
]),
2420 SHA_DIGEST_LENGTH
, &(p
[2]),
2421 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
2422 SSLerror(s
, ERR_R_ECDSA_LIB
);
2427 #ifndef OPENSSL_NO_GOST
2428 } else if (pkey
->type
== NID_id_GostR3410_94
||
2429 pkey
->type
== NID_id_GostR3410_2001
) {
2430 unsigned char signbuf
[128];
2437 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2438 if (hdatalen
<= 0) {
2439 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2442 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2443 !(md
= EVP_get_digestbynid(nid
))) {
2444 SSLerror(s
, ERR_R_EVP_LIB
);
2447 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2448 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2449 !EVP_DigestFinal(&mctx
, signbuf
, &u
) ||
2450 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2451 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
2452 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2453 GOST_SIG_FORMAT_RS_LE
,
2455 (EVP_PKEY_sign(pctx
, &(p
[2]), &sigsize
,
2456 signbuf
, u
) <= 0)) {
2457 SSLerror(s
, ERR_R_EVP_LIB
);
2460 if (!tls1_digest_cached_records(s
))
2467 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2471 s
->internal
->state
= SSL3_ST_CW_CERT_VRFY_B
;
2473 ssl3_handshake_msg_finish(s
, n
);
2476 EVP_MD_CTX_cleanup(&mctx
);
2477 EVP_PKEY_CTX_free(pctx
);
2479 return (ssl3_handshake_write(s
));
2482 EVP_MD_CTX_cleanup(&mctx
);
2483 EVP_PKEY_CTX_free(pctx
);
2488 ssl3_send_client_certificate(SSL
*s
)
2490 EVP_PKEY
*pkey
= NULL
;
2492 CBB cbb
, client_cert
;
2495 memset(&cbb
, 0, sizeof(cbb
));
2497 if (s
->internal
->state
== SSL3_ST_CW_CERT_A
) {
2498 if ((s
->cert
== NULL
) || (s
->cert
->key
->x509
== NULL
) ||
2499 (s
->cert
->key
->privatekey
== NULL
))
2500 s
->internal
->state
= SSL3_ST_CW_CERT_B
;
2502 s
->internal
->state
= SSL3_ST_CW_CERT_C
;
2505 /* We need to get a client cert */
2506 if (s
->internal
->state
== SSL3_ST_CW_CERT_B
) {
2508 * If we get an error, we need to
2509 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2510 * We then get retied later
2512 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2514 s
->internal
->rwstate
= SSL_X509_LOOKUP
;
2517 s
->internal
->rwstate
= SSL_NOTHING
;
2518 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2519 s
->internal
->state
= SSL3_ST_CW_CERT_B
;
2520 if (!SSL_use_certificate(s
, x509
) ||
2521 !SSL_use_PrivateKey(s
, pkey
))
2523 } else if (i
== 1) {
2525 SSLerror(s
, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2529 EVP_PKEY_free(pkey
);
2531 S3I(s
)->tmp
.cert_req
= 2;
2533 /* Ok, we have a cert */
2534 s
->internal
->state
= SSL3_ST_CW_CERT_C
;
2537 if (s
->internal
->state
== SSL3_ST_CW_CERT_C
) {
2538 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &client_cert
,
2539 SSL3_MT_CERTIFICATE
))
2541 if (!ssl3_output_cert_chain(s
, &client_cert
,
2542 (S3I(s
)->tmp
.cert_req
== 2) ? NULL
: s
->cert
->key
->x509
))
2544 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2547 s
->internal
->state
= SSL3_ST_CW_CERT_D
;
2550 /* SSL3_ST_CW_CERT_D */
2551 return (ssl3_handshake_write(s
));
2559 #define has_bits(i,m) (((i)&(m)) == (m))
2562 ssl3_check_cert_and_algorithm(SSL
*s
)
2566 EVP_PKEY
*pkey
= NULL
;
2570 alg_k
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
2571 alg_a
= S3I(s
)->tmp
.new_cipher
->algorithm_auth
;
2573 /* We don't have a certificate. */
2574 if (alg_a
& SSL_aNULL
)
2577 sc
= SSI(s
)->sess_cert
;
2579 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2582 dh
= SSI(s
)->sess_cert
->peer_dh_tmp
;
2584 /* This is the passed certificate. */
2586 idx
= sc
->peer_cert_type
;
2587 if (idx
== SSL_PKEY_ECC
) {
2588 if (ssl_check_srvr_ecc_cert_and_alg(
2589 sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
2591 SSLerror(s
, SSL_R_BAD_ECC_CERT
);
2597 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
2598 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
2599 EVP_PKEY_free(pkey
);
2601 /* Check that we have a certificate if we require one. */
2602 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
|EVP_PKT_SIGN
)) {
2603 SSLerror(s
, SSL_R_MISSING_RSA_SIGNING_CERT
);
2605 } else if ((alg_a
& SSL_aDSS
) &&
2606 !has_bits(i
, EVP_PK_DSA
|EVP_PKT_SIGN
)) {
2607 SSLerror(s
, SSL_R_MISSING_DSA_SIGNING_CERT
);
2610 if ((alg_k
& SSL_kRSA
) &&
2611 !has_bits(i
, EVP_PK_RSA
|EVP_PKT_ENC
)) {
2612 SSLerror(s
, SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2615 if ((alg_k
& SSL_kDHE
) &&
2616 !(has_bits(i
, EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
))) {
2617 SSLerror(s
, SSL_R_MISSING_DH_KEY
);
2623 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2629 ssl3_send_next_proto(SSL
*s
)
2631 CBB cbb
, nextproto
, npn
, padding
;
2635 memset(&cbb
, 0, sizeof(cbb
));
2637 if (s
->internal
->state
== SSL3_ST_CW_NEXT_PROTO_A
) {
2638 pad_len
= 32 - ((s
->internal
->next_proto_negotiated_len
+ 2) % 32);
2640 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &nextproto
,
2641 SSL3_MT_NEXT_PROTO
))
2643 if (!CBB_add_u8_length_prefixed(&nextproto
, &npn
))
2645 if (!CBB_add_bytes(&npn
, s
->internal
->next_proto_negotiated
,
2646 s
->internal
->next_proto_negotiated_len
))
2648 if (!CBB_add_u8_length_prefixed(&nextproto
, &padding
))
2650 if (!CBB_add_space(&padding
, &pad
, pad_len
))
2652 memset(pad
, 0, pad_len
);
2653 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2656 s
->internal
->state
= SSL3_ST_CW_NEXT_PROTO_B
;
2659 return (ssl3_handshake_write(s
));
2668 * Check to see if handshake is full or resumed. Usually this is just a
2669 * case of checking to see if a cache hit has occurred. In the case of
2670 * session tickets we have to check the next message to be sure.
2674 ssl3_check_finished(SSL
*s
)
2679 /* If we have no ticket it cannot be a resumed session. */
2680 if (!s
->session
->tlsext_tick
)
2682 /* this function is called when we really expect a Certificate
2683 * message, so permit appropriate message length */
2684 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_CR_CERT_A
,
2685 SSL3_ST_CR_CERT_B
, -1, s
->internal
->max_cert_list
, &ok
);
2688 S3I(s
)->tmp
.reuse_message
= 1;
2689 if ((S3I(s
)->tmp
.message_type
== SSL3_MT_FINISHED
) ||
2690 (S3I(s
)->tmp
.message_type
== SSL3_MT_NEWSESSION_TICKET
))
2697 ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
2701 #ifndef OPENSSL_NO_ENGINE
2702 if (s
->ctx
->internal
->client_cert_engine
) {
2703 i
= ENGINE_load_ssl_client_cert(
2704 s
->ctx
->internal
->client_cert_engine
, s
,
2705 SSL_get_client_CA_list(s
), px509
, ppkey
, NULL
, NULL
, NULL
);
2710 if (s
->ctx
->internal
->client_cert_cb
)
2711 i
= s
->ctx
->internal
->client_cert_cb(s
, px509
, ppkey
);