1 /* $OpenBSD: ssl_srvr.c,v 1.22 2017/08/12 21:47:59 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
153 #include "ssl_locl.h"
155 #include <openssl/bn.h>
156 #include <openssl/buffer.h>
157 #include <openssl/curve25519.h>
158 #include <openssl/evp.h>
159 #include <openssl/dh.h>
160 #ifndef OPENSSL_NO_GOST
161 #include <openssl/gost.h>
163 #include <openssl/hmac.h>
164 #include <openssl/md5.h>
165 #include <openssl/objects.h>
166 #include <openssl/x509.h>
168 #include "bytestring.h"
174 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
176 int new_state
, state
, skip
= 0;
181 if (s
->internal
->info_callback
!= NULL
)
182 cb
= s
->internal
->info_callback
;
183 else if (s
->ctx
->internal
->info_callback
!= NULL
)
184 cb
= s
->ctx
->internal
->info_callback
;
186 /* init things to blank */
187 s
->internal
->in_handshake
++;
188 if (!SSL_in_init(s
) || SSL_in_before(s
))
191 if (s
->cert
== NULL
) {
192 SSLerror(s
, SSL_R_NO_CERTIFICATE_SET
);
198 state
= S3I(s
)->hs
.state
;
200 switch (S3I(s
)->hs
.state
) {
201 case SSL_ST_RENEGOTIATE
:
202 s
->internal
->renegotiate
= 1;
203 /* S3I(s)->hs.state=SSL_ST_ACCEPT; */
207 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
208 case SSL_ST_OK
|SSL_ST_ACCEPT
:
212 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
214 if ((s
->version
>> 8) != 3) {
215 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
219 s
->internal
->type
= SSL_ST_ACCEPT
;
221 if (!ssl3_setup_init_buffer(s
)) {
225 if (!ssl3_setup_buffers(s
)) {
230 s
->internal
->init_num
= 0;
232 if (S3I(s
)->hs
.state
!= SSL_ST_RENEGOTIATE
) {
234 * Ok, we now need to push on a buffering BIO
235 * so that the output is sent in a way that
238 if (!ssl_init_wbio_buffer(s
, 1)) {
243 if (!tls1_init_finished_mac(s
)) {
248 S3I(s
)->hs
.state
= SSL3_ST_SR_CLNT_HELLO_A
;
249 s
->ctx
->internal
->stats
.sess_accept
++;
250 } else if (!S3I(s
)->send_connection_binding
) {
252 * Server attempting to renegotiate with
253 * client that doesn't support secure
256 SSLerror(s
, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
257 ssl3_send_alert(s
, SSL3_AL_FATAL
,
258 SSL_AD_HANDSHAKE_FAILURE
);
263 * S3I(s)->hs.state == SSL_ST_RENEGOTIATE,
264 * we will just send a HelloRequest
266 s
->ctx
->internal
->stats
.sess_accept_renegotiate
++;
267 S3I(s
)->hs
.state
= SSL3_ST_SW_HELLO_REQ_A
;
271 case SSL3_ST_SW_HELLO_REQ_A
:
272 case SSL3_ST_SW_HELLO_REQ_B
:
274 s
->internal
->shutdown
= 0;
275 ret
= ssl3_send_hello_request(s
);
278 S3I(s
)->hs
.next_state
= SSL3_ST_SW_HELLO_REQ_C
;
279 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
280 s
->internal
->init_num
= 0;
282 if (!tls1_init_finished_mac(s
)) {
288 case SSL3_ST_SW_HELLO_REQ_C
:
289 S3I(s
)->hs
.state
= SSL_ST_OK
;
292 case SSL3_ST_SR_CLNT_HELLO_A
:
293 case SSL3_ST_SR_CLNT_HELLO_B
:
294 case SSL3_ST_SR_CLNT_HELLO_C
:
296 s
->internal
->shutdown
= 0;
297 if (s
->internal
->rwstate
!= SSL_X509_LOOKUP
) {
298 ret
= ssl3_get_client_hello(s
);
303 s
->internal
->renegotiate
= 2;
304 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_HELLO_A
;
305 s
->internal
->init_num
= 0;
308 case SSL3_ST_SW_SRVR_HELLO_A
:
309 case SSL3_ST_SW_SRVR_HELLO_B
:
310 ret
= ssl3_send_server_hello(s
);
313 if (s
->internal
->hit
) {
314 if (s
->internal
->tlsext_ticket_expected
)
315 S3I(s
)->hs
.state
= SSL3_ST_SW_SESSION_TICKET_A
;
317 S3I(s
)->hs
.state
= SSL3_ST_SW_CHANGE_A
;
320 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_A
;
321 s
->internal
->init_num
= 0;
324 case SSL3_ST_SW_CERT_A
:
325 case SSL3_ST_SW_CERT_B
:
326 /* Check if it is anon DH or anon ECDH. */
327 if (!(S3I(s
)->hs
.new_cipher
->algorithm_auth
&
329 ret
= ssl3_send_server_certificate(s
);
332 if (s
->internal
->tlsext_status_expected
)
333 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_STATUS_A
;
335 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_A
;
338 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_A
;
340 s
->internal
->init_num
= 0;
343 case SSL3_ST_SW_KEY_EXCH_A
:
344 case SSL3_ST_SW_KEY_EXCH_B
:
345 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
348 * Only send if using a DH key exchange.
350 * For ECC ciphersuites, we send a ServerKeyExchange
351 * message only if the cipher suite is ECDHE. In other
352 * cases, the server certificate contains the server's
353 * public key for key exchange.
355 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
356 ret
= ssl3_send_server_key_exchange(s
);
362 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_REQ_A
;
363 s
->internal
->init_num
= 0;
366 case SSL3_ST_SW_CERT_REQ_A
:
367 case SSL3_ST_SW_CERT_REQ_B
:
369 * Determine whether or not we need to request a
372 * Do not request a certificate if:
374 * - We did not ask for it (SSL_VERIFY_PEER is unset).
376 * - SSL_VERIFY_CLIENT_ONCE is set and we are
379 * - We are using an anonymous ciphersuites
380 * (see section "Certificate request" in SSL 3 drafts
381 * and in RFC 2246) ... except when the application
382 * insists on verification (against the specs, but
383 * s3_clnt.c accepts this for SSL 3).
385 if (!(s
->verify_mode
& SSL_VERIFY_PEER
) ||
386 ((s
->session
->peer
!= NULL
) &&
387 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
388 ((S3I(s
)->hs
.new_cipher
->algorithm_auth
&
389 SSL_aNULL
) && !(s
->verify_mode
&
390 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))) {
391 /* No cert request */
393 S3I(s
)->tmp
.cert_request
= 0;
394 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_DONE_A
;
395 if (S3I(s
)->handshake_buffer
) {
396 if (!tls1_digest_cached_records(s
)) {
402 S3I(s
)->tmp
.cert_request
= 1;
403 ret
= ssl3_send_certificate_request(s
);
406 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_DONE_A
;
407 s
->internal
->init_num
= 0;
411 case SSL3_ST_SW_SRVR_DONE_A
:
412 case SSL3_ST_SW_SRVR_DONE_B
:
413 ret
= ssl3_send_server_done(s
);
416 S3I(s
)->hs
.next_state
= SSL3_ST_SR_CERT_A
;
417 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
418 s
->internal
->init_num
= 0;
421 case SSL3_ST_SW_FLUSH
:
424 * This code originally checked to see if
425 * any data was pending using BIO_CTRL_INFO
426 * and then flushed. This caused problems
427 * as documented in PR#1939. The proposed
428 * fix doesn't completely resolve this issue
429 * as buggy implementations of BIO_CTRL_PENDING
430 * still exist. So instead we just flush
434 s
->internal
->rwstate
= SSL_WRITING
;
435 if (BIO_flush(s
->wbio
) <= 0) {
439 s
->internal
->rwstate
= SSL_NOTHING
;
441 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
444 case SSL3_ST_SR_CERT_A
:
445 case SSL3_ST_SR_CERT_B
:
446 if (S3I(s
)->tmp
.cert_request
) {
447 ret
= ssl3_get_client_certificate(s
);
451 s
->internal
->init_num
= 0;
452 S3I(s
)->hs
.state
= SSL3_ST_SR_KEY_EXCH_A
;
455 case SSL3_ST_SR_KEY_EXCH_A
:
456 case SSL3_ST_SR_KEY_EXCH_B
:
457 ret
= ssl3_get_client_key_exchange(s
);
460 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
463 * For the ECDH ciphersuites when
464 * the client sends its ECDH pub key in
465 * a certificate, the CertificateVerify
466 * message is not sent.
467 * Also for GOST ciphersuites when
468 * the client uses its key from the certificate
471 S3I(s
)->hs
.state
= SSL3_ST_SR_FINISHED_A
;
472 s
->internal
->init_num
= 0;
473 } else if (SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) {
474 S3I(s
)->hs
.state
= SSL3_ST_SR_CERT_VRFY_A
;
475 s
->internal
->init_num
= 0;
476 if (!s
->session
->peer
)
479 * For sigalgs freeze the handshake buffer
480 * at this point and digest cached records.
482 if (!S3I(s
)->handshake_buffer
) {
483 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
487 s
->s3
->flags
|= TLS1_FLAGS_KEEP_HANDSHAKE
;
488 if (!tls1_digest_cached_records(s
)) {
493 S3I(s
)->hs
.state
= SSL3_ST_SR_CERT_VRFY_A
;
494 s
->internal
->init_num
= 0;
497 * We need to get hashes here so if there is
498 * a client cert, it can be verified.
500 if (S3I(s
)->handshake_buffer
) {
501 if (!tls1_digest_cached_records(s
)) {
506 if (!tls1_handshake_hash_value(s
,
507 S3I(s
)->tmp
.cert_verify_md
,
508 sizeof(S3I(s
)->tmp
.cert_verify_md
),
516 case SSL3_ST_SR_CERT_VRFY_A
:
517 case SSL3_ST_SR_CERT_VRFY_B
:
518 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
520 /* we should decide if we expected this one */
521 ret
= ssl3_get_cert_verify(s
);
525 S3I(s
)->hs
.state
= SSL3_ST_SR_FINISHED_A
;
526 s
->internal
->init_num
= 0;
529 case SSL3_ST_SR_FINISHED_A
:
530 case SSL3_ST_SR_FINISHED_B
:
531 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
532 ret
= ssl3_get_finished(s
, SSL3_ST_SR_FINISHED_A
,
533 SSL3_ST_SR_FINISHED_B
);
536 if (s
->internal
->hit
)
537 S3I(s
)->hs
.state
= SSL_ST_OK
;
538 else if (s
->internal
->tlsext_ticket_expected
)
539 S3I(s
)->hs
.state
= SSL3_ST_SW_SESSION_TICKET_A
;
541 S3I(s
)->hs
.state
= SSL3_ST_SW_CHANGE_A
;
542 s
->internal
->init_num
= 0;
545 case SSL3_ST_SW_SESSION_TICKET_A
:
546 case SSL3_ST_SW_SESSION_TICKET_B
:
547 ret
= ssl3_send_newsession_ticket(s
);
550 S3I(s
)->hs
.state
= SSL3_ST_SW_CHANGE_A
;
551 s
->internal
->init_num
= 0;
554 case SSL3_ST_SW_CERT_STATUS_A
:
555 case SSL3_ST_SW_CERT_STATUS_B
:
556 ret
= ssl3_send_cert_status(s
);
559 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_A
;
560 s
->internal
->init_num
= 0;
564 case SSL3_ST_SW_CHANGE_A
:
565 case SSL3_ST_SW_CHANGE_B
:
567 s
->session
->cipher
= S3I(s
)->hs
.new_cipher
;
568 if (!tls1_setup_key_block(s
)) {
573 ret
= ssl3_send_change_cipher_spec(s
,
574 SSL3_ST_SW_CHANGE_A
, SSL3_ST_SW_CHANGE_B
);
578 S3I(s
)->hs
.state
= SSL3_ST_SW_FINISHED_A
;
579 s
->internal
->init_num
= 0;
581 if (!tls1_change_cipher_state(
582 s
, SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
589 case SSL3_ST_SW_FINISHED_A
:
590 case SSL3_ST_SW_FINISHED_B
:
591 ret
= ssl3_send_finished(s
,
592 SSL3_ST_SW_FINISHED_A
, SSL3_ST_SW_FINISHED_B
,
593 TLS_MD_SERVER_FINISH_CONST
,
594 TLS_MD_SERVER_FINISH_CONST_SIZE
);
597 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
598 if (s
->internal
->hit
)
599 S3I(s
)->hs
.next_state
= SSL3_ST_SR_FINISHED_A
;
601 S3I(s
)->hs
.next_state
= SSL_ST_OK
;
602 s
->internal
->init_num
= 0;
606 /* clean a few things up */
607 tls1_cleanup_key_block(s
);
609 BUF_MEM_free(s
->internal
->init_buf
);
610 s
->internal
->init_buf
= NULL
;
612 /* remove buffering on output */
613 ssl_free_wbio_buffer(s
);
615 s
->internal
->init_num
= 0;
617 /* skipped if we just sent a HelloRequest */
618 if (s
->internal
->renegotiate
== 2) {
619 s
->internal
->renegotiate
= 0;
620 s
->internal
->new_session
= 0;
622 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
624 s
->ctx
->internal
->stats
.sess_accept_good
++;
626 s
->internal
->handshake_func
= ssl3_accept
;
629 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
637 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
643 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
644 if (s
->internal
->debug
) {
645 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
650 if ((cb
!= NULL
) && (S3I(s
)->hs
.state
!= state
)) {
651 new_state
= S3I(s
)->hs
.state
;
652 S3I(s
)->hs
.state
= state
;
653 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
654 S3I(s
)->hs
.state
= new_state
;
660 /* BIO_flush(s->wbio); */
662 s
->internal
->in_handshake
--;
664 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
669 ssl3_send_hello_request(SSL
*s
)
673 memset(&cbb
, 0, sizeof(cbb
));
675 if (S3I(s
)->hs
.state
== SSL3_ST_SW_HELLO_REQ_A
) {
676 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &hello
,
677 SSL3_MT_HELLO_REQUEST
))
679 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
682 S3I(s
)->hs
.state
= SSL3_ST_SW_HELLO_REQ_B
;
685 /* SSL3_ST_SW_HELLO_REQ_B */
686 return (ssl3_handshake_write(s
));
695 ssl3_get_client_hello(SSL
*s
)
697 CBS cbs
, client_random
, session_id
, cookie
, cipher_suites
;
698 CBS compression_methods
;
699 uint16_t client_version
;
702 int i
, j
, ok
, al
, ret
= -1, cookie_valid
= 0;
705 unsigned char *p
, *d
;
707 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
709 const SSL_METHOD
*method
;
710 uint16_t shared_version
;
714 * We do this so that we will respond with our native type.
715 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
716 * This down switching should be handled by a different method.
717 * If we are SSLv3, we will respond with SSLv3, even if prompted with
720 if (S3I(s
)->hs
.state
== SSL3_ST_SR_CLNT_HELLO_A
) {
721 S3I(s
)->hs
.state
= SSL3_ST_SR_CLNT_HELLO_B
;
724 s
->internal
->first_packet
= 1;
725 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_CLNT_HELLO_B
,
726 SSL3_ST_SR_CLNT_HELLO_C
, SSL3_MT_CLIENT_HELLO
,
727 SSL3_RT_MAX_PLAIN_LENGTH
, &ok
);
730 s
->internal
->first_packet
= 0;
735 d
= p
= (unsigned char *)s
->internal
->init_msg
;
738 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
741 * Use version from inside client hello, not from record header.
742 * (may differ: see RFC 2246, Appendix E, second paragraph)
744 if (!CBS_get_u16(&cbs
, &client_version
))
747 if (ssl_max_shared_version(s
, client_version
, &shared_version
) != 1) {
748 SSLerror(s
, SSL_R_WRONG_VERSION_NUMBER
);
749 if ((s
->client_version
>> 8) == SSL3_VERSION_MAJOR
&&
750 !s
->internal
->enc_write_ctx
&& !s
->internal
->write_hash
) {
752 * Similar to ssl3_get_record, send alert using remote
755 s
->version
= s
->client_version
;
757 al
= SSL_AD_PROTOCOL_VERSION
;
760 s
->client_version
= client_version
;
761 s
->version
= shared_version
;
763 if ((method
= tls1_get_server_method(shared_version
)) == NULL
)
764 method
= dtls1_get_server_method(shared_version
);
765 if (method
== NULL
) {
766 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
771 if (!CBS_get_bytes(&cbs
, &client_random
, SSL3_RANDOM_SIZE
))
773 if (!CBS_get_u8_length_prefixed(&cbs
, &session_id
))
777 * If we require cookies (DTLS) and this ClientHello doesn't
778 * contain one, just return since we do not want to
779 * allocate any memory yet. So check cookie length...
781 if (SSL_IS_DTLS(s
)) {
782 if (!CBS_get_u8_length_prefixed(&cbs
, &cookie
))
784 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
785 if (CBS_len(&cookie
) == 0)
790 if (!CBS_write_bytes(&client_random
, s
->s3
->client_random
,
791 sizeof(s
->s3
->client_random
), NULL
))
794 s
->internal
->hit
= 0;
797 * Versions before 0.9.7 always allow clients to resume sessions in
798 * renegotiation. 0.9.7 and later allow this by default, but optionally
799 * ignore resumption requests with flag
800 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag
801 * rather than a change to default behavior so that applications
802 * relying on this for security won't even compile against older
805 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated()
806 * to request renegotiation but not a new session (s->internal->new_session
807 * remains unset): for servers, this essentially just means that the
808 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
811 if ((s
->internal
->new_session
&& (s
->internal
->options
&
812 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
813 if (!ssl_get_new_session(s
, 1))
816 /* XXX - pass CBS through instead... */
817 i
= ssl_get_prev_session(s
,
818 (unsigned char *)CBS_data(&session_id
),
819 CBS_len(&session_id
), end
);
820 if (i
== 1) { /* previous session */
821 s
->internal
->hit
= 1;
826 if (!ssl_get_new_session(s
, 1))
831 if (SSL_IS_DTLS(s
)) {
833 * The ClientHello may contain a cookie even if the HelloVerify
834 * message has not been sent - make sure that it does not cause
837 if (CBS_len(&cookie
) > sizeof(D1I(s
)->rcvd_cookie
)) {
838 al
= SSL_AD_DECODE_ERROR
;
839 SSLerror(s
, SSL_R_COOKIE_MISMATCH
);
843 /* Verify the cookie if appropriate option is set. */
844 if ((SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) &&
845 CBS_len(&cookie
) > 0) {
848 /* XXX - rcvd_cookie seems to only be used here... */
849 if (!CBS_write_bytes(&cookie
, D1I(s
)->rcvd_cookie
,
850 sizeof(D1I(s
)->rcvd_cookie
), &cookie_len
))
853 if (s
->ctx
->internal
->app_verify_cookie_cb
!= NULL
) {
854 if (s
->ctx
->internal
->app_verify_cookie_cb(s
,
855 D1I(s
)->rcvd_cookie
, cookie_len
) == 0) {
856 al
= SSL_AD_HANDSHAKE_FAILURE
;
857 SSLerror(s
, SSL_R_COOKIE_MISMATCH
);
860 /* else cookie verification succeeded */
861 /* XXX - can d1->cookie_len > sizeof(rcvd_cookie) ? */
862 } else if (timingsafe_memcmp(D1I(s
)->rcvd_cookie
,
863 D1I(s
)->cookie
, D1I(s
)->cookie_len
) != 0) {
864 /* default verification */
865 al
= SSL_AD_HANDSHAKE_FAILURE
;
866 SSLerror(s
, SSL_R_COOKIE_MISMATCH
);
873 if (!CBS_get_u16_length_prefixed(&cbs
, &cipher_suites
))
876 /* XXX - This logic seems wrong... */
877 if (CBS_len(&cipher_suites
) == 0 && CBS_len(&session_id
) != 0) {
878 /* we need a cipher if we are not resuming a session */
879 al
= SSL_AD_ILLEGAL_PARAMETER
;
880 SSLerror(s
, SSL_R_NO_CIPHERS_SPECIFIED
);
884 if (CBS_len(&cipher_suites
) > 0) {
885 if ((ciphers
= ssl_bytes_to_cipher_list(s
,
886 CBS_data(&cipher_suites
), CBS_len(&cipher_suites
))) == NULL
)
890 /* If it is a hit, check that the cipher is in the list */
891 if (s
->internal
->hit
&& CBS_len(&cipher_suites
) > 0) {
893 id
= s
->session
->cipher
->id
;
895 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
896 c
= sk_SSL_CIPHER_value(ciphers
, i
);
904 * We need to have the cipher in the cipher
905 * list if we are asked to reuse it
907 al
= SSL_AD_ILLEGAL_PARAMETER
;
908 SSLerror(s
, SSL_R_REQUIRED_CIPHER_MISSING
);
913 if (!CBS_get_u8_length_prefixed(&cbs
, &compression_methods
))
917 while (CBS_len(&compression_methods
) > 0) {
918 if (!CBS_get_u8(&compression_methods
, &comp_method
))
920 if (comp_method
== 0)
923 if (comp_null
== 0) {
924 al
= SSL_AD_DECODE_ERROR
;
925 SSLerror(s
, SSL_R_NO_COMPRESSION_SPECIFIED
);
929 p
= (unsigned char *)CBS_data(&cbs
);
932 if (!ssl_parse_clienthello_tlsext(s
, &p
, d
, n
, &al
)) {
933 /* 'al' set by ssl_parse_clienthello_tlsext */
934 SSLerror(s
, SSL_R_PARSE_TLSEXT
);
937 if (ssl_check_clienthello_tlsext_early(s
) <= 0) {
938 SSLerror(s
, SSL_R_CLIENTHELLO_TLSEXT
);
943 * Check if we want to use external pre-shared secret for this
944 * handshake for not reused session only. We need to generate
945 * server_random before calling tls_session_secret_cb in order to allow
946 * SessionTicket processing to use it in key derivation.
948 arc4random_buf(s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
950 if (!s
->internal
->hit
&& s
->internal
->tls_session_secret_cb
) {
951 SSL_CIPHER
*pref_cipher
= NULL
;
953 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
954 if (s
->internal
->tls_session_secret_cb(s
, s
->session
->master_key
,
955 &s
->session
->master_key_length
, ciphers
, &pref_cipher
,
956 s
->internal
->tls_session_secret_cb_arg
)) {
957 s
->internal
->hit
= 1;
958 s
->session
->ciphers
= ciphers
;
959 s
->session
->verify_result
= X509_V_OK
;
963 /* check if some cipher was preferred by call back */
964 pref_cipher
= pref_cipher
? pref_cipher
:
965 ssl3_choose_cipher(s
, s
->session
->ciphers
,
967 if (pref_cipher
== NULL
) {
968 al
= SSL_AD_HANDSHAKE_FAILURE
;
969 SSLerror(s
, SSL_R_NO_SHARED_CIPHER
);
973 s
->session
->cipher
= pref_cipher
;
975 sk_SSL_CIPHER_free(s
->cipher_list
);
976 sk_SSL_CIPHER_free(s
->internal
->cipher_list_by_id
);
978 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
979 s
->internal
->cipher_list_by_id
=
980 sk_SSL_CIPHER_dup(s
->session
->ciphers
);
985 * Given s->session->ciphers and SSL_get_ciphers, we must
989 if (!s
->internal
->hit
) {
990 sk_SSL_CIPHER_free(s
->session
->ciphers
);
991 s
->session
->ciphers
= ciphers
;
992 if (ciphers
== NULL
) {
993 al
= SSL_AD_ILLEGAL_PARAMETER
;
994 SSLerror(s
, SSL_R_NO_CIPHERS_PASSED
);
998 c
= ssl3_choose_cipher(s
, s
->session
->ciphers
,
1002 al
= SSL_AD_HANDSHAKE_FAILURE
;
1003 SSLerror(s
, SSL_R_NO_SHARED_CIPHER
);
1006 S3I(s
)->hs
.new_cipher
= c
;
1008 S3I(s
)->hs
.new_cipher
= s
->session
->cipher
;
1011 if (!tls1_handshake_hash_init(s
))
1014 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
1015 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) ||
1016 !(s
->verify_mode
& SSL_VERIFY_PEER
)) {
1017 if (!tls1_digest_cached_records(s
)) {
1018 al
= SSL_AD_INTERNAL_ERROR
;
1024 * We now have the following setup.
1026 * cipher_list - our prefered list of ciphers
1027 * ciphers - the clients prefered list of ciphers
1028 * compression - basically ignored right now
1029 * ssl version is set - sslv3
1030 * s->session - The ssl session has been setup.
1031 * s->internal->hit - session reuse flag
1032 * s->hs.new_cipher - the new cipher to use.
1035 /* Handles TLS extensions that we couldn't check earlier */
1036 if (ssl_check_clienthello_tlsext_late(s
) <= 0) {
1037 SSLerror(s
, SSL_R_CLIENTHELLO_TLSEXT
);
1041 ret
= cookie_valid
? 2 : 1;
1045 al
= SSL_AD_DECODE_ERROR
;
1046 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1048 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1051 sk_SSL_CIPHER_free(ciphers
);
1057 ssl3_send_server_hello(SSL
*s
)
1059 unsigned char *bufend
;
1060 unsigned char *p
, *d
;
1061 CBB cbb
, session_id
;
1065 memset(&cbb
, 0, sizeof(cbb
));
1067 bufend
= (unsigned char *)s
->internal
->init_buf
->data
+ SSL3_RT_MAX_PLAIN_LENGTH
;
1069 if (S3I(s
)->hs
.state
== SSL3_ST_SW_SRVR_HELLO_A
) {
1070 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_SERVER_HELLO
);
1072 if (!CBB_init_fixed(&cbb
, p
, bufend
- p
))
1075 if (!CBB_add_u16(&cbb
, s
->version
))
1077 if (!CBB_add_bytes(&cbb
, s
->s3
->server_random
,
1078 sizeof(s
->s3
->server_random
)))
1082 * There are several cases for the session ID to send
1083 * back in the server hello:
1085 * - For session reuse from the session cache,
1086 * we send back the old session ID.
1087 * - If stateless session reuse (using a session ticket)
1088 * is successful, we send back the client's "session ID"
1089 * (which doesn't actually identify the session).
1090 * - If it is a new session, we send back the new
1092 * - However, if we want the new session to be single-use,
1093 * we send back a 0-length session ID.
1095 * s->internal->hit is non-zero in either case of session reuse,
1096 * so the following won't overwrite an ID that we're supposed
1099 if (!(s
->ctx
->internal
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
1100 && !s
->internal
->hit
)
1101 s
->session
->session_id_length
= 0;
1103 sl
= s
->session
->session_id_length
;
1104 if (sl
> (int)sizeof(s
->session
->session_id
)) {
1105 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1109 if (!CBB_add_u8_length_prefixed(&cbb
, &session_id
))
1111 if (!CBB_add_bytes(&session_id
, s
->session
->session_id
, sl
))
1115 if (!CBB_add_u16(&cbb
,
1116 ssl3_cipher_get_value(S3I(s
)->hs
.new_cipher
)))
1119 /* Compression method. */
1120 if (!CBB_add_u8(&cbb
, 0))
1123 if (!CBB_finish(&cbb
, NULL
, &outlen
))
1126 if ((p
= ssl_add_serverhello_tlsext(s
, p
+ outlen
,
1128 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1132 ssl3_handshake_msg_finish(s
, p
- d
);
1135 /* SSL3_ST_SW_SRVR_HELLO_B */
1136 return (ssl3_handshake_write(s
));
1145 ssl3_send_server_done(SSL
*s
)
1149 memset(&cbb
, 0, sizeof(cbb
));
1151 if (S3I(s
)->hs
.state
== SSL3_ST_SW_SRVR_DONE_A
) {
1152 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &done
,
1153 SSL3_MT_SERVER_DONE
))
1155 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
1158 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_DONE_B
;
1161 /* SSL3_ST_SW_SRVR_DONE_B */
1162 return (ssl3_handshake_write(s
));
1171 ssl3_send_server_kex_dhe(SSL
*s
, CBB
*cbb
)
1173 CBB dh_p
, dh_g
, dh_Ys
;
1174 DH
*dh
= NULL
, *dhp
;
1175 unsigned char *data
;
1178 if (s
->cert
->dh_tmp_auto
!= 0) {
1179 if ((dhp
= ssl_get_auto_dh(s
)) == NULL
) {
1180 al
= SSL_AD_INTERNAL_ERROR
;
1181 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1185 dhp
= s
->cert
->dh_tmp
;
1187 if (dhp
== NULL
&& s
->cert
->dh_tmp_cb
!= NULL
)
1188 dhp
= s
->cert
->dh_tmp_cb(s
, 0,
1189 SSL_C_PKEYLENGTH(S3I(s
)->hs
.new_cipher
));
1192 al
= SSL_AD_HANDSHAKE_FAILURE
;
1193 SSLerror(s
, SSL_R_MISSING_TMP_DH_KEY
);
1197 if (S3I(s
)->tmp
.dh
!= NULL
) {
1198 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1202 if (s
->cert
->dh_tmp_auto
!= 0) {
1204 } else if ((dh
= DHparams_dup(dhp
)) == NULL
) {
1205 SSLerror(s
, ERR_R_DH_LIB
);
1208 S3I(s
)->tmp
.dh
= dh
;
1209 if (!DH_generate_key(dh
)) {
1210 SSLerror(s
, ERR_R_DH_LIB
);
1215 * Serialize the DH parameters and public key.
1217 if (!CBB_add_u16_length_prefixed(cbb
, &dh_p
))
1219 if (!CBB_add_space(&dh_p
, &data
, BN_num_bytes(dh
->p
)))
1221 BN_bn2bin(dh
->p
, data
);
1223 if (!CBB_add_u16_length_prefixed(cbb
, &dh_g
))
1225 if (!CBB_add_space(&dh_g
, &data
, BN_num_bytes(dh
->g
)))
1227 BN_bn2bin(dh
->g
, data
);
1229 if (!CBB_add_u16_length_prefixed(cbb
, &dh_Ys
))
1231 if (!CBB_add_space(&dh_Ys
, &data
, BN_num_bytes(dh
->pub_key
)))
1233 BN_bn2bin(dh
->pub_key
, data
);
1235 if (!CBB_flush(cbb
))
1241 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1247 ssl3_send_server_kex_ecdhe_ecp(SSL
*s
, int nid
, CBB
*cbb
)
1249 const EC_GROUP
*group
;
1250 const EC_POINT
*pubkey
;
1251 unsigned char *data
;
1252 int encoded_len
= 0;
1254 BN_CTX
*bn_ctx
= NULL
;
1260 * Only named curves are supported in ECDH ephemeral key exchanges.
1261 * For supported named curves, curve_id is non-zero.
1263 if ((curve_id
= tls1_ec_nid2curve_id(nid
)) == 0) {
1264 SSLerror(s
, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1268 if (S3I(s
)->tmp
.ecdh
!= NULL
) {
1269 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1273 if ((S3I(s
)->tmp
.ecdh
= EC_KEY_new_by_curve_name(nid
)) == NULL
) {
1274 al
= SSL_AD_HANDSHAKE_FAILURE
;
1275 SSLerror(s
, SSL_R_MISSING_TMP_ECDH_KEY
);
1278 ecdh
= S3I(s
)->tmp
.ecdh
;
1280 if (!EC_KEY_generate_key(ecdh
)) {
1281 SSLerror(s
, ERR_R_ECDH_LIB
);
1284 if ((group
= EC_KEY_get0_group(ecdh
)) == NULL
||
1285 (pubkey
= EC_KEY_get0_public_key(ecdh
)) == NULL
||
1286 EC_KEY_get0_private_key(ecdh
) == NULL
) {
1287 SSLerror(s
, ERR_R_ECDH_LIB
);
1292 * Encode the public key.
1294 encoded_len
= EC_POINT_point2oct(group
, pubkey
,
1295 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
1296 if (encoded_len
== 0) {
1297 SSLerror(s
, ERR_R_ECDH_LIB
);
1300 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
1301 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1306 * Only named curves are supported in ECDH ephemeral key exchanges.
1307 * In this case the ServerKeyExchange message has:
1308 * [1 byte CurveType], [2 byte CurveName]
1309 * [1 byte length of encoded point], followed by
1310 * the actual encoded point itself.
1312 if (!CBB_add_u8(cbb
, NAMED_CURVE_TYPE
))
1314 if (!CBB_add_u16(cbb
, curve_id
))
1316 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
1318 if (!CBB_add_space(&ecpoint
, &data
, encoded_len
))
1320 if (EC_POINT_point2oct(group
, pubkey
, POINT_CONVERSION_UNCOMPRESSED
,
1321 data
, encoded_len
, bn_ctx
) == 0) {
1322 SSLerror(s
, ERR_R_ECDH_LIB
);
1325 if (!CBB_flush(cbb
))
1328 BN_CTX_free(bn_ctx
);
1333 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1335 BN_CTX_free(bn_ctx
);
1341 ssl3_send_server_kex_ecdhe_ecx(SSL
*s
, int nid
, CBB
*cbb
)
1343 uint8_t *public_key
= NULL
;
1348 /* Generate an X25519 key pair. */
1349 if (S3I(s
)->tmp
.x25519
!= NULL
) {
1350 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1353 if ((S3I(s
)->tmp
.x25519
= malloc(X25519_KEY_LENGTH
)) == NULL
)
1355 if ((public_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
1357 X25519_keypair(public_key
, S3I(s
)->tmp
.x25519
);
1359 /* Serialize public key. */
1360 if ((curve_id
= tls1_ec_nid2curve_id(nid
)) == 0) {
1361 SSLerror(s
, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1365 if (!CBB_add_u8(cbb
, NAMED_CURVE_TYPE
))
1367 if (!CBB_add_u16(cbb
, curve_id
))
1369 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
1371 if (!CBB_add_bytes(&ecpoint
, public_key
, X25519_KEY_LENGTH
))
1373 if (!CBB_flush(cbb
))
1385 ssl3_send_server_kex_ecdhe(SSL
*s
, CBB
*cbb
)
1389 nid
= tls1_get_shared_curve(s
);
1391 if (nid
== NID_X25519
)
1392 return ssl3_send_server_kex_ecdhe_ecx(s
, nid
, cbb
);
1394 return ssl3_send_server_kex_ecdhe_ecp(s
, nid
, cbb
);
1398 ssl3_send_server_key_exchange(SSL
*s
)
1401 unsigned char *params
= NULL
;
1404 unsigned char md_buf
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
1407 const EVP_MD
*md
= NULL
;
1408 unsigned char *p
, *d
;
1409 int al
, i
, j
, n
, kn
;
1414 memset(&cbb
, 0, sizeof(cbb
));
1416 EVP_MD_CTX_init(&md_ctx
);
1417 if (S3I(s
)->hs
.state
== SSL3_ST_SW_KEY_EXCH_A
) {
1418 type
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
1420 buf
= s
->internal
->init_buf
;
1422 if (!CBB_init(&cbb
, 0))
1425 if (type
& SSL_kDHE
) {
1426 if (ssl3_send_server_kex_dhe(s
, &cbb
) != 1)
1428 } else if (type
& SSL_kECDHE
) {
1429 if (ssl3_send_server_kex_ecdhe(s
, &cbb
) != 1)
1432 al
= SSL_AD_HANDSHAKE_FAILURE
;
1433 SSLerror(s
, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1437 if (!CBB_finish(&cbb
, ¶ms
, ¶ms_len
))
1440 if (!(S3I(s
)->hs
.new_cipher
->algorithm_auth
& SSL_aNULL
)) {
1441 if ((pkey
= ssl_get_sign_pkey(
1442 s
, S3I(s
)->hs
.new_cipher
, &md
)) == NULL
) {
1443 al
= SSL_AD_DECODE_ERROR
;
1446 kn
= EVP_PKEY_size(pkey
);
1452 if (!BUF_MEM_grow_clean(buf
, ssl3_handshake_msg_hdr_len(s
) +
1454 SSLerror(s
, ERR_LIB_BUF
);
1458 d
= p
= ssl3_handshake_msg_start(s
,
1459 SSL3_MT_SERVER_KEY_EXCHANGE
);
1461 memcpy(p
, params
, params_len
);
1472 * n is the length of the params, they start at &(d[4])
1473 * and p points to the space at the end.
1475 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1478 if (!EVP_DigestInit_ex(&md_ctx
, EVP_md5_sha1(),
1481 EVP_DigestUpdate(&md_ctx
, s
->s3
->client_random
,
1483 EVP_DigestUpdate(&md_ctx
, s
->s3
->server_random
,
1485 EVP_DigestUpdate(&md_ctx
, d
, n
);
1486 EVP_DigestFinal_ex(&md_ctx
, q
,
1487 (unsigned int *)&i
);
1490 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
1491 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
1492 SSLerror(s
, ERR_R_RSA_LIB
);
1498 /* Send signature algorithm. */
1499 if (SSL_USE_SIGALGS(s
)) {
1500 if (!tls12_get_sigandhash(p
, pkey
, md
)) {
1501 /* Should never happen */
1502 al
= SSL_AD_INTERNAL_ERROR
;
1503 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1508 EVP_SignInit_ex(&md_ctx
, md
, NULL
);
1509 EVP_SignUpdate(&md_ctx
,
1510 s
->s3
->client_random
,
1512 EVP_SignUpdate(&md_ctx
,
1513 s
->s3
->server_random
,
1515 EVP_SignUpdate(&md_ctx
, d
, n
);
1516 if (!EVP_SignFinal(&md_ctx
, &p
[2],
1517 (unsigned int *)&i
, pkey
)) {
1518 SSLerror(s
, ERR_R_EVP_LIB
);
1523 if (SSL_USE_SIGALGS(s
))
1526 /* Is this error check actually needed? */
1527 al
= SSL_AD_HANDSHAKE_FAILURE
;
1528 SSLerror(s
, SSL_R_UNKNOWN_PKEY_TYPE
);
1533 ssl3_handshake_msg_finish(s
, n
);
1536 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_B
;
1538 EVP_MD_CTX_cleanup(&md_ctx
);
1540 return (ssl3_handshake_write(s
));
1543 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1546 EVP_MD_CTX_cleanup(&md_ctx
);
1553 ssl3_send_certificate_request(SSL
*s
)
1555 CBB cbb
, cert_request
, cert_types
, sigalgs
, cert_auth
, dn
;
1556 STACK_OF(X509_NAME
) *sk
= NULL
;
1561 * Certificate Request - RFC 5246 section 7.4.4.
1564 memset(&cbb
, 0, sizeof(cbb
));
1566 if (S3I(s
)->hs
.state
== SSL3_ST_SW_CERT_REQ_A
) {
1567 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &cert_request
,
1568 SSL3_MT_CERTIFICATE_REQUEST
))
1571 if (!CBB_add_u8_length_prefixed(&cert_request
, &cert_types
))
1573 if (!ssl3_get_req_cert_types(s
, &cert_types
))
1576 if (SSL_USE_SIGALGS(s
)) {
1577 unsigned char *sigalgs_data
;
1580 tls12_get_req_sig_algs(s
, &sigalgs_data
, &sigalgs_len
);
1582 if (!CBB_add_u16_length_prefixed(&cert_request
, &sigalgs
))
1584 if (!CBB_add_bytes(&sigalgs
, sigalgs_data
, sigalgs_len
))
1588 if (!CBB_add_u16_length_prefixed(&cert_request
, &cert_auth
))
1591 sk
= SSL_get_client_CA_list(s
);
1592 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
1593 unsigned char *name_data
;
1596 name
= sk_X509_NAME_value(sk
, i
);
1597 name_len
= i2d_X509_NAME(name
, NULL
);
1599 if (!CBB_add_u16_length_prefixed(&cert_auth
, &dn
))
1601 if (!CBB_add_space(&dn
, &name_data
, name_len
))
1603 if (i2d_X509_NAME(name
, &name_data
) != name_len
)
1607 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
1610 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_REQ_B
;
1613 /* SSL3_ST_SW_CERT_REQ_B */
1614 return (ssl3_handshake_write(s
));
1623 ssl3_get_client_kex_rsa(SSL
*s
, unsigned char *p
, long n
)
1625 unsigned char fakekey
[SSL_MAX_MASTER_KEY_LENGTH
];
1628 EVP_PKEY
*pkey
= NULL
;
1633 arc4random_buf(fakekey
, sizeof(fakekey
));
1634 fakekey
[0] = s
->client_version
>> 8;
1635 fakekey
[1] = s
->client_version
& 0xff;
1637 pkey
= s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1638 if ((pkey
== NULL
) || (pkey
->type
!= EVP_PKEY_RSA
) ||
1639 (pkey
->pkey
.rsa
== NULL
)) {
1640 al
= SSL_AD_HANDSHAKE_FAILURE
;
1641 SSLerror(s
, SSL_R_MISSING_RSA_CERTIFICATE
);
1644 rsa
= pkey
->pkey
.rsa
;
1650 SSLerror(s
, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1655 i
= RSA_private_decrypt((int)n
, p
, p
, rsa
, RSA_PKCS1_PADDING
);
1661 if (i
!= SSL_MAX_MASTER_KEY_LENGTH
) {
1662 al
= SSL_AD_DECODE_ERROR
;
1663 /* SSLerror(s, SSL_R_BAD_RSA_DECRYPT); */
1666 if (p
- d
+ 2 > n
) /* needed in the SSL3 case */
1668 if ((al
== -1) && !((p
[0] == (s
->client_version
>> 8)) &&
1669 (p
[1] == (s
->client_version
& 0xff)))) {
1671 * The premaster secret must contain the same version
1672 * number as the ClientHello to detect version rollback
1673 * attacks (strangely, the protocol does not offer such
1674 * protection for DH ciphersuites).
1675 * However, buggy clients exist that send the negotiated
1676 * protocol version instead if the server does not
1677 * support the requested protocol version.
1678 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1681 if (!((s
->internal
->options
& SSL_OP_TLS_ROLLBACK_BUG
) &&
1682 (p
[0] == (s
->version
>> 8)) &&
1683 (p
[1] == (s
->version
& 0xff)))) {
1684 al
= SSL_AD_DECODE_ERROR
;
1685 /* SSLerror(s, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1688 * The Klima-Pokorny-Rosa extension of
1689 * Bleichenbacher's attack
1690 * (http://eprint.iacr.org/2003/052/) exploits
1691 * the version number check as a "bad version
1692 * oracle" -- an alert would reveal that the
1693 * plaintext corresponding to some ciphertext
1694 * made up by the adversary is properly
1695 * formatted except that the version number is
1697 * To avoid such attacks, we should treat this
1698 * just like any other decryption error.
1705 * Some decryption failure -- use random value instead
1706 * as countermeasure against Bleichenbacher's attack
1707 * on PKCS #1 v1.5 RSA padding (see RFC 2246,
1710 i
= SSL_MAX_MASTER_KEY_LENGTH
;
1714 s
->session
->master_key_length
=
1715 tls1_generate_master_secret(s
,
1716 s
->session
->master_key
, p
, i
);
1718 explicit_bzero(p
, i
);
1722 al
= SSL_AD_DECODE_ERROR
;
1723 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1725 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1731 ssl3_get_client_kex_dhe(SSL
*s
, unsigned char *p
, long n
)
1741 CBS_init(&cbs
, p
, n
);
1743 if (!CBS_get_u16_length_prefixed(&cbs
, &dh_Yc
))
1746 if (CBS_len(&cbs
) != 0)
1749 if (S3I(s
)->tmp
.dh
== NULL
) {
1750 al
= SSL_AD_HANDSHAKE_FAILURE
;
1751 SSLerror(s
, SSL_R_MISSING_TMP_DH_KEY
);
1754 dh
= S3I(s
)->tmp
.dh
;
1756 if ((bn
= BN_bin2bn(CBS_data(&dh_Yc
), CBS_len(&dh_Yc
), NULL
)) == NULL
) {
1757 SSLerror(s
, SSL_R_BN_LIB
);
1761 key_size
= DH_compute_key(p
, bn
, dh
);
1762 if (key_size
<= 0) {
1763 SSLerror(s
, ERR_R_DH_LIB
);
1768 s
->session
->master_key_length
=
1769 tls1_generate_master_secret(
1770 s
, s
->session
->master_key
, p
, key_size
);
1772 explicit_bzero(p
, key_size
);
1774 DH_free(S3I(s
)->tmp
.dh
);
1775 S3I(s
)->tmp
.dh
= NULL
;
1782 al
= SSL_AD_DECODE_ERROR
;
1783 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1785 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1791 ssl3_get_client_kex_ecdhe_ecp(SSL
*s
, unsigned char *p
, long n
)
1793 EC_KEY
*srvr_ecdh
= NULL
;
1794 EVP_PKEY
*clnt_pub_pkey
= NULL
;
1795 EC_POINT
*clnt_ecpoint
= NULL
;
1796 BN_CTX
*bn_ctx
= NULL
;
1802 const EC_GROUP
*group
;
1803 const BIGNUM
*priv_key
;
1805 /* Initialize structures for server's ECDH key pair. */
1806 if ((srvr_ecdh
= EC_KEY_new()) == NULL
) {
1807 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1812 * Use the ephemeral values we saved when
1813 * generating the ServerKeyExchange message.
1815 tkey
= S3I(s
)->tmp
.ecdh
;
1817 group
= EC_KEY_get0_group(tkey
);
1818 priv_key
= EC_KEY_get0_private_key(tkey
);
1820 if (!EC_KEY_set_group(srvr_ecdh
, group
) ||
1821 !EC_KEY_set_private_key(srvr_ecdh
, priv_key
)) {
1822 SSLerror(s
, ERR_R_EC_LIB
);
1826 /* Let's get client's public key */
1827 if ((clnt_ecpoint
= EC_POINT_new(group
)) == NULL
) {
1828 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1833 /* Client Publickey was in Client Certificate */
1834 if (((clnt_pub_pkey
= X509_get_pubkey(
1835 s
->session
->peer
)) == NULL
) ||
1836 (clnt_pub_pkey
->type
!= EVP_PKEY_EC
)) {
1838 * XXX: For now, we do not support client
1839 * authentication using ECDH certificates
1840 * so this branch (n == 0L) of the code is
1841 * never executed. When that support is
1842 * added, we ought to ensure the key
1843 * received in the certificate is
1844 * authorized for key agreement.
1845 * ECDH_compute_key implicitly checks that
1846 * the two ECDH shares are for the same
1849 al
= SSL_AD_HANDSHAKE_FAILURE
;
1850 SSLerror(s
, SSL_R_UNABLE_TO_DECODE_ECDH_CERTS
);
1854 if (EC_POINT_copy(clnt_ecpoint
,
1855 EC_KEY_get0_public_key(clnt_pub_pkey
->pkey
.ec
))
1857 SSLerror(s
, ERR_R_EC_LIB
);
1860 ret
= 2; /* Skip certificate verify processing */
1863 * Get client's public key from encoded point
1864 * in the ClientKeyExchange message.
1866 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
1867 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1871 /* Get encoded point length */
1876 SSLerror(s
, ERR_R_EC_LIB
);
1879 if (EC_POINT_oct2point(group
,
1880 clnt_ecpoint
, p
, i
, bn_ctx
) == 0) {
1881 SSLerror(s
, ERR_R_EC_LIB
);
1885 * p is pointing to somewhere in the buffer
1886 * currently, so set it to the start.
1888 p
= (unsigned char *)s
->internal
->init_buf
->data
;
1891 /* Compute the shared pre-master secret */
1892 key_size
= ECDH_size(srvr_ecdh
);
1893 if (key_size
<= 0) {
1894 SSLerror(s
, ERR_R_ECDH_LIB
);
1897 i
= ECDH_compute_key(p
, key_size
, clnt_ecpoint
, srvr_ecdh
,
1900 SSLerror(s
, ERR_R_ECDH_LIB
);
1904 EVP_PKEY_free(clnt_pub_pkey
);
1905 EC_POINT_free(clnt_ecpoint
);
1906 EC_KEY_free(srvr_ecdh
);
1907 BN_CTX_free(bn_ctx
);
1908 EC_KEY_free(S3I(s
)->tmp
.ecdh
);
1909 S3I(s
)->tmp
.ecdh
= NULL
;
1911 /* Compute the master secret */
1912 s
->session
->master_key_length
=
1913 tls1_generate_master_secret(
1914 s
, s
->session
->master_key
, p
, i
);
1916 explicit_bzero(p
, i
);
1920 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1922 EVP_PKEY_free(clnt_pub_pkey
);
1923 EC_POINT_free(clnt_ecpoint
);
1924 EC_KEY_free(srvr_ecdh
);
1925 BN_CTX_free(bn_ctx
);
1930 ssl3_get_client_kex_ecdhe_ecx(SSL
*s
, unsigned char *p
, long n
)
1932 uint8_t *shared_key
= NULL
;
1939 CBS_init(&cbs
, p
, n
);
1940 if (!CBS_get_u8_length_prefixed(&cbs
, &ecpoint
))
1942 if (CBS_len(&ecpoint
) != X25519_KEY_LENGTH
)
1945 if ((shared_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
1947 if (!X25519(shared_key
, S3I(s
)->tmp
.x25519
, CBS_data(&ecpoint
)))
1950 freezero(S3I(s
)->tmp
.x25519
, X25519_KEY_LENGTH
);
1951 S3I(s
)->tmp
.x25519
= NULL
;
1953 s
->session
->master_key_length
=
1954 tls1_generate_master_secret(
1955 s
, s
->session
->master_key
, shared_key
, X25519_KEY_LENGTH
);
1960 freezero(shared_key
, X25519_KEY_LENGTH
);
1966 ssl3_get_client_kex_ecdhe(SSL
*s
, unsigned char *p
, long n
)
1968 if (S3I(s
)->tmp
.x25519
!= NULL
)
1969 return ssl3_get_client_kex_ecdhe_ecx(s
, p
, n
);
1971 return ssl3_get_client_kex_ecdhe_ecp(s
, p
, n
);
1975 ssl3_get_client_kex_gost(SSL
*s
, unsigned char *p
, long n
)
1978 EVP_PKEY_CTX
*pkey_ctx
;
1979 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
1980 unsigned char premaster_secret
[32], *start
;
1981 size_t outlen
= 32, inlen
;
1982 unsigned long alg_a
;
1988 /* Get our certificate private key*/
1989 alg_a
= S3I(s
)->hs
.new_cipher
->algorithm_auth
;
1990 if (alg_a
& SSL_aGOST01
)
1991 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
1993 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
1994 EVP_PKEY_decrypt_init(pkey_ctx
);
1996 * If client certificate is present and is of the same type,
1997 * maybe use it for key exchange.
1998 * Don't mind errors from EVP_PKEY_derive_set_peer, because
1999 * it is completely valid to use a client certificate for
2000 * authorization only.
2002 client_pub_pkey
= X509_get_pubkey(s
->session
->peer
);
2003 if (client_pub_pkey
) {
2004 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2005 client_pub_pkey
) <= 0)
2010 /* Decrypt session key */
2011 if (ASN1_get_object((const unsigned char **)&p
, &Tlen
, &Ttag
,
2012 &Tclass
, n
) != V_ASN1_CONSTRUCTED
||
2013 Ttag
!= V_ASN1_SEQUENCE
|| Tclass
!= V_ASN1_UNIVERSAL
) {
2014 SSLerror(s
, SSL_R_DECRYPTION_FAILED
);
2019 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
,
2020 start
, inlen
) <=0) {
2021 SSLerror(s
, SSL_R_DECRYPTION_FAILED
);
2024 /* Generate master secret */
2025 s
->session
->master_key_length
=
2026 tls1_generate_master_secret(
2027 s
, s
->session
->master_key
, premaster_secret
, 32);
2028 /* Check if pubkey from client certificate was used */
2029 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1,
2030 EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
2035 EVP_PKEY_free(client_pub_pkey
);
2036 EVP_PKEY_CTX_free(pkey_ctx
);
2043 al
= SSL_AD_DECODE_ERROR
;
2044 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
2045 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2051 ssl3_get_client_key_exchange(SSL
*s
)
2053 unsigned long alg_k
;
2058 /* 2048 maxlen is a guess. How long a key does that permit? */
2059 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_KEY_EXCH_A
,
2060 SSL3_ST_SR_KEY_EXCH_B
, SSL3_MT_CLIENT_KEY_EXCHANGE
, 2048, &ok
);
2064 p
= (unsigned char *)s
->internal
->init_msg
;
2066 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
2068 if (alg_k
& SSL_kRSA
) {
2069 if (ssl3_get_client_kex_rsa(s
, p
, n
) != 1)
2071 } else if (alg_k
& SSL_kDHE
) {
2072 if (ssl3_get_client_kex_dhe(s
, p
, n
) != 1)
2074 } else if (alg_k
& SSL_kECDHE
) {
2075 if (ssl3_get_client_kex_ecdhe(s
, p
, n
) != 1)
2077 } else if (alg_k
& SSL_kGOST
) {
2078 if (ssl3_get_client_kex_gost(s
, p
, n
) != 1)
2081 al
= SSL_AD_HANDSHAKE_FAILURE
;
2082 SSLerror(s
, SSL_R_UNKNOWN_CIPHER_TYPE
);
2089 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2095 ssl3_get_cert_verify(SSL
*s
)
2097 EVP_PKEY
*pkey
= NULL
;
2099 int al
, ok
, ret
= 0;
2103 const EVP_MD
*md
= NULL
;
2105 EVP_MD_CTX_init(&mctx
);
2107 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_CERT_VRFY_A
,
2108 SSL3_ST_SR_CERT_VRFY_B
, -1, SSL3_RT_MAX_PLAIN_LENGTH
, &ok
);
2112 if (s
->session
->peer
!= NULL
) {
2113 peer
= s
->session
->peer
;
2114 pkey
= X509_get_pubkey(peer
);
2115 type
= X509_certificate_type(peer
, pkey
);
2121 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
) {
2122 S3I(s
)->tmp
.reuse_message
= 1;
2124 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2125 SSLerror(s
, SSL_R_MISSING_VERIFY_MESSAGE
);
2133 SSLerror(s
, SSL_R_NO_CLIENT_CERT_RECEIVED
);
2134 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2138 if (!(type
& EVP_PKT_SIGN
)) {
2139 SSLerror(s
, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
2140 al
= SSL_AD_ILLEGAL_PARAMETER
;
2144 if (S3I(s
)->change_cipher_spec
) {
2145 SSLerror(s
, SSL_R_CCS_RECEIVED_EARLY
);
2146 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2150 /* we now have a signature that we need to verify */
2151 p
= (unsigned char *)s
->internal
->init_msg
;
2153 * Check for broken implementations of GOST ciphersuites.
2155 * If key is GOST and n is exactly 64, it is a bare
2156 * signature without length field.
2158 if (n
== 64 && (pkey
->type
== NID_id_GostR3410_94
||
2159 pkey
->type
== NID_id_GostR3410_2001
) ) {
2162 if (SSL_USE_SIGALGS(s
)) {
2163 int sigalg
= tls12_get_sigid(pkey
);
2164 /* Should never happen */
2166 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2167 al
= SSL_AD_INTERNAL_ERROR
;
2172 /* Check key type is consistent with signature */
2173 if (sigalg
!= (int)p
[1]) {
2174 SSLerror(s
, SSL_R_WRONG_SIGNATURE_TYPE
);
2175 al
= SSL_AD_DECODE_ERROR
;
2178 md
= tls12_get_hash(p
[0]);
2180 SSLerror(s
, SSL_R_UNKNOWN_DIGEST
);
2181 al
= SSL_AD_DECODE_ERROR
;
2194 j
= EVP_PKEY_size(pkey
);
2195 if ((i
> j
) || (n
> j
) || (n
<= 0)) {
2196 SSLerror(s
, SSL_R_WRONG_SIGNATURE_SIZE
);
2197 al
= SSL_AD_DECODE_ERROR
;
2201 if (SSL_USE_SIGALGS(s
)) {
2204 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2205 if (hdatalen
<= 0) {
2206 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2207 al
= SSL_AD_INTERNAL_ERROR
;
2210 if (!EVP_VerifyInit_ex(&mctx
, md
, NULL
) ||
2211 !EVP_VerifyUpdate(&mctx
, hdata
, hdatalen
)) {
2212 SSLerror(s
, ERR_R_EVP_LIB
);
2213 al
= SSL_AD_INTERNAL_ERROR
;
2217 if (EVP_VerifyFinal(&mctx
, p
, i
, pkey
) <= 0) {
2218 al
= SSL_AD_DECRYPT_ERROR
;
2219 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
2223 if (pkey
->type
== EVP_PKEY_RSA
) {
2224 i
= RSA_verify(NID_md5_sha1
, S3I(s
)->tmp
.cert_verify_md
,
2225 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, p
, i
,
2228 al
= SSL_AD_DECRYPT_ERROR
;
2229 SSLerror(s
, SSL_R_BAD_RSA_DECRYPT
);
2233 al
= SSL_AD_DECRYPT_ERROR
;
2234 SSLerror(s
, SSL_R_BAD_RSA_SIGNATURE
);
2238 if (pkey
->type
== EVP_PKEY_EC
) {
2239 j
= ECDSA_verify(pkey
->save_type
,
2240 &(S3I(s
)->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
2241 SHA_DIGEST_LENGTH
, p
, i
, pkey
->pkey
.ec
);
2244 al
= SSL_AD_DECRYPT_ERROR
;
2245 SSLerror(s
, SSL_R_BAD_ECDSA_SIGNATURE
);
2249 #ifndef OPENSSL_NO_GOST
2250 if (pkey
->type
== NID_id_GostR3410_94
||
2251 pkey
->type
== NID_id_GostR3410_2001
) {
2254 unsigned char signature
[128];
2255 unsigned int siglen
= sizeof(signature
);
2259 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2260 if (hdatalen
<= 0) {
2261 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2262 al
= SSL_AD_INTERNAL_ERROR
;
2265 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2266 !(md
= EVP_get_digestbynid(nid
))) {
2267 SSLerror(s
, ERR_R_EVP_LIB
);
2268 al
= SSL_AD_INTERNAL_ERROR
;
2271 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2273 SSLerror(s
, ERR_R_EVP_LIB
);
2274 al
= SSL_AD_INTERNAL_ERROR
;
2277 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2278 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2279 !EVP_DigestFinal(&mctx
, signature
, &siglen
) ||
2280 (EVP_PKEY_verify_init(pctx
) <= 0) ||
2281 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2282 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_VERIFY
,
2283 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2284 GOST_SIG_FORMAT_RS_LE
,
2286 SSLerror(s
, ERR_R_EVP_LIB
);
2287 al
= SSL_AD_INTERNAL_ERROR
;
2288 EVP_PKEY_CTX_free(pctx
);
2292 if (EVP_PKEY_verify(pctx
, p
, i
, signature
, siglen
) <= 0) {
2293 al
= SSL_AD_DECRYPT_ERROR
;
2294 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
2295 EVP_PKEY_CTX_free(pctx
);
2299 EVP_PKEY_CTX_free(pctx
);
2303 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2304 al
= SSL_AD_UNSUPPORTED_CERTIFICATE
;
2312 al
= SSL_AD_DECODE_ERROR
;
2313 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
2315 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2318 if (S3I(s
)->handshake_buffer
) {
2319 BIO_free(S3I(s
)->handshake_buffer
);
2320 S3I(s
)->handshake_buffer
= NULL
;
2321 s
->s3
->flags
&= ~TLS1_FLAGS_KEEP_HANDSHAKE
;
2323 EVP_MD_CTX_cleanup(&mctx
);
2324 EVP_PKEY_free(pkey
);
2329 ssl3_get_client_certificate(SSL
*s
)
2331 CBS cbs
, client_certs
;
2332 int i
, ok
, al
, ret
= -1;
2335 const unsigned char *q
;
2336 STACK_OF(X509
) *sk
= NULL
;
2338 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_CERT_A
, SSL3_ST_SR_CERT_B
,
2339 -1, s
->internal
->max_cert_list
, &ok
);
2344 if (S3I(s
)->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
2345 if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2346 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2347 SSLerror(s
, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2348 al
= SSL_AD_HANDSHAKE_FAILURE
;
2352 * If tls asked for a client cert,
2353 * the client must return a 0 list.
2355 if (S3I(s
)->tmp
.cert_request
) {
2356 SSLerror(s
, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
2358 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2361 S3I(s
)->tmp
.reuse_message
= 1;
2365 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
2366 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2367 SSLerror(s
, SSL_R_WRONG_MESSAGE_TYPE
);
2374 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
2376 if ((sk
= sk_X509_new_null()) == NULL
) {
2377 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2381 if (!CBS_get_u24_length_prefixed(&cbs
, &client_certs
) ||
2385 while (CBS_len(&client_certs
) > 0) {
2388 if (!CBS_get_u24_length_prefixed(&client_certs
, &cert
)) {
2389 al
= SSL_AD_DECODE_ERROR
;
2390 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
2394 q
= CBS_data(&cert
);
2395 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
2397 SSLerror(s
, ERR_R_ASN1_LIB
);
2400 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
2401 al
= SSL_AD_DECODE_ERROR
;
2402 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
2405 if (!sk_X509_push(sk
, x
)) {
2406 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2412 if (sk_X509_num(sk
) <= 0) {
2414 * TLS does not mind 0 certs returned.
2415 * Fail for TLS only if we required a certificate.
2417 if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2418 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2419 SSLerror(s
, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2420 al
= SSL_AD_HANDSHAKE_FAILURE
;
2423 /* No client certificate so digest cached records */
2424 if (S3I(s
)->handshake_buffer
&& !tls1_digest_cached_records(s
)) {
2425 al
= SSL_AD_INTERNAL_ERROR
;
2429 i
= ssl_verify_cert_chain(s
, sk
);
2431 al
= ssl_verify_alarm_type(s
->verify_result
);
2432 SSLerror(s
, SSL_R_NO_CERTIFICATE_RETURNED
);
2437 X509_free(s
->session
->peer
);
2438 s
->session
->peer
= sk_X509_shift(sk
);
2439 s
->session
->verify_result
= s
->verify_result
;
2442 * With the current implementation, sess_cert will always be NULL
2443 * when we arrive here
2445 if (SSI(s
)->sess_cert
== NULL
) {
2446 SSI(s
)->sess_cert
= ssl_sess_cert_new();
2447 if (SSI(s
)->sess_cert
== NULL
) {
2448 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2452 sk_X509_pop_free(SSI(s
)->sess_cert
->cert_chain
, X509_free
);
2453 SSI(s
)->sess_cert
->cert_chain
= sk
;
2456 * Inconsistency alert: cert_chain does *not* include the
2457 * peer's own certificate, while we do include it in s3_clnt.c
2465 al
= SSL_AD_DECODE_ERROR
;
2466 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
2468 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2472 sk_X509_pop_free(sk
, X509_free
);
2478 ssl3_send_server_certificate(SSL
*s
)
2480 CBB cbb
, server_cert
;
2484 * Server Certificate - RFC 5246, section 7.4.2.
2487 memset(&cbb
, 0, sizeof(cbb
));
2489 if (S3I(s
)->hs
.state
== SSL3_ST_SW_CERT_A
) {
2490 if ((x
= ssl_get_server_send_cert(s
)) == NULL
) {
2491 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2495 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &server_cert
,
2496 SSL3_MT_CERTIFICATE
))
2498 if (!ssl3_output_cert_chain(s
, &server_cert
, x
))
2500 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2503 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_B
;
2506 /* SSL3_ST_SW_CERT_B */
2507 return (ssl3_handshake_write(s
));
2515 /* send a new session ticket (not necessarily for a new session) */
2517 ssl3_send_newsession_ticket(SSL
*s
)
2519 unsigned char *d
, *p
, *macstart
;
2520 unsigned char *senc
= NULL
;
2521 const unsigned char *const_p
;
2522 int len
, slen_full
, slen
;
2527 SSL_CTX
*tctx
= s
->initial_ctx
;
2528 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2529 unsigned char key_name
[16];
2531 if (S3I(s
)->hs
.state
== SSL3_ST_SW_SESSION_TICKET_A
) {
2532 /* get session encoding length */
2533 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
2535 * Some length values are 16 bits, so forget it if session is
2538 if (slen_full
> 0xFF00)
2540 senc
= malloc(slen_full
);
2544 i2d_SSL_SESSION(s
->session
, &p
);
2547 * Create a fresh copy (not shared with other threads) to
2551 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
2555 /* ID is irrelevant for the ticket */
2556 sess
->session_id_length
= 0;
2558 slen
= i2d_SSL_SESSION(sess
, NULL
);
2559 if (slen
> slen_full
) {
2560 /* shouldn't ever happen */
2564 i2d_SSL_SESSION(sess
, &p
);
2565 SSL_SESSION_free(sess
);
2568 * Grow buffer if need be: the length calculation is as
2569 * follows 1 (size of message name) + 3 (message length
2570 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2571 * 16 (key name) + max_iv_len (iv length) +
2572 * session_length + max_enc_block_size (max encrypted session
2573 * length) + max_md_size (HMAC).
2575 if (!BUF_MEM_grow(s
->internal
->init_buf
, ssl3_handshake_msg_hdr_len(s
) +
2576 22 + EVP_MAX_IV_LENGTH
+ EVP_MAX_BLOCK_LENGTH
+
2577 EVP_MAX_MD_SIZE
+ slen
))
2580 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_NEWSESSION_TICKET
);
2582 EVP_CIPHER_CTX_init(&ctx
);
2583 HMAC_CTX_init(&hctx
);
2586 * Initialize HMAC and cipher contexts. If callback present
2587 * it does all the work otherwise use generated values
2590 if (tctx
->internal
->tlsext_ticket_key_cb
) {
2591 if (tctx
->internal
->tlsext_ticket_key_cb(s
,
2592 key_name
, iv
, &ctx
, &hctx
, 1) < 0) {
2593 EVP_CIPHER_CTX_cleanup(&ctx
);
2597 arc4random_buf(iv
, 16);
2598 EVP_EncryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
2599 tctx
->internal
->tlsext_tick_aes_key
, iv
);
2600 HMAC_Init_ex(&hctx
, tctx
->internal
->tlsext_tick_hmac_key
,
2601 16, tlsext_tick_md(), NULL
);
2602 memcpy(key_name
, tctx
->internal
->tlsext_tick_key_name
, 16);
2606 * Ticket lifetime hint (advisory only):
2607 * We leave this unspecified for resumed session
2608 * (for simplicity), and guess that tickets for new
2609 * sessions will live as long as their sessions.
2611 l2n(s
->internal
->hit
? 0 : s
->session
->timeout
, p
);
2613 /* Skip ticket length for now */
2615 /* Output key name */
2617 memcpy(p
, key_name
, 16);
2620 memcpy(p
, iv
, EVP_CIPHER_CTX_iv_length(&ctx
));
2621 p
+= EVP_CIPHER_CTX_iv_length(&ctx
);
2622 /* Encrypt session data */
2623 EVP_EncryptUpdate(&ctx
, p
, &len
, senc
, slen
);
2625 EVP_EncryptFinal_ex(&ctx
, p
, &len
);
2627 EVP_CIPHER_CTX_cleanup(&ctx
);
2629 HMAC_Update(&hctx
, macstart
, p
- macstart
);
2630 HMAC_Final(&hctx
, p
, &hlen
);
2631 HMAC_CTX_cleanup(&hctx
);
2634 /* Now write out lengths: p points to end of data written */
2638 /* Skip ticket lifetime hint. */
2640 s2n(len
- 6, p
); /* Message length */
2642 ssl3_handshake_msg_finish(s
, len
);
2644 S3I(s
)->hs
.state
= SSL3_ST_SW_SESSION_TICKET_B
;
2646 freezero(senc
, slen_full
);
2649 /* SSL3_ST_SW_SESSION_TICKET_B */
2650 return (ssl3_handshake_write(s
));
2653 freezero(senc
, slen_full
);
2659 ssl3_send_cert_status(SSL
*s
)
2661 CBB cbb
, certstatus
, ocspresp
;
2663 memset(&cbb
, 0, sizeof(cbb
));
2665 if (S3I(s
)->hs
.state
== SSL3_ST_SW_CERT_STATUS_A
) {
2666 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &certstatus
,
2667 SSL3_MT_CERTIFICATE_STATUS
))
2669 if (!CBB_add_u8(&certstatus
, s
->tlsext_status_type
))
2671 if (!CBB_add_u24_length_prefixed(&certstatus
, &ocspresp
))
2673 if (!CBB_add_bytes(&ocspresp
, s
->internal
->tlsext_ocsp_resp
,
2674 s
->internal
->tlsext_ocsp_resplen
))
2676 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2679 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_STATUS_B
;
2682 /* SSL3_ST_SW_CERT_STATUS_B */
2683 return (ssl3_handshake_write(s
));