1 /* $OpenBSD: ssl_srvr.c,v 1.11.4.1 2017/04/29 23:41:32 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
= s
->internal
->state
;
200 switch (s
->internal
->state
) {
201 case SSL_ST_RENEGOTIATE
:
202 s
->internal
->renegotiate
= 1;
203 /* s->internal->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 (s
->internal
->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 s
->internal
->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 * s->internal->state == SSL_ST_RENEGOTIATE,
264 * we will just send a HelloRequest
266 s
->ctx
->internal
->stats
.sess_accept_renegotiate
++;
267 s
->internal
->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
)->tmp
.next_state
= SSL3_ST_SW_HELLO_REQ_C
;
279 s
->internal
->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 s
->internal
->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 s
->internal
->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 s
->internal
->state
= SSL3_ST_SW_SESSION_TICKET_A
;
317 s
->internal
->state
= SSL3_ST_SW_CHANGE_A
;
320 s
->internal
->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
)->tmp
.new_cipher
->algorithm_auth
&
329 ret
= ssl3_send_server_certificate(s
);
332 if (s
->internal
->tlsext_status_expected
)
333 s
->internal
->state
= SSL3_ST_SW_CERT_STATUS_A
;
335 s
->internal
->state
= SSL3_ST_SW_KEY_EXCH_A
;
338 s
->internal
->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
)->tmp
.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 s
->internal
->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
)->tmp
.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 s
->internal
->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 s
->internal
->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
)->tmp
.next_state
= SSL3_ST_SR_CERT_A
;
417 s
->internal
->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 s
->internal
->state
= S3I(s
)->tmp
.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 s
->internal
->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
)->tmp
.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 if (S3I(s
)->next_proto_neg_seen
)
472 s
->internal
->state
= SSL3_ST_SR_NEXT_PROTO_A
;
474 s
->internal
->state
= SSL3_ST_SR_FINISHED_A
;
475 s
->internal
->init_num
= 0;
476 } else if (SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) {
477 s
->internal
->state
= SSL3_ST_SR_CERT_VRFY_A
;
478 s
->internal
->init_num
= 0;
479 if (!s
->session
->peer
)
482 * For sigalgs freeze the handshake buffer
483 * at this point and digest cached records.
485 if (!S3I(s
)->handshake_buffer
) {
486 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
490 s
->s3
->flags
|= TLS1_FLAGS_KEEP_HANDSHAKE
;
491 if (!tls1_digest_cached_records(s
)) {
496 s
->internal
->state
= SSL3_ST_SR_CERT_VRFY_A
;
497 s
->internal
->init_num
= 0;
500 * We need to get hashes here so if there is
501 * a client cert, it can be verified.
503 if (S3I(s
)->handshake_buffer
) {
504 if (!tls1_digest_cached_records(s
)) {
509 if (!tls1_handshake_hash_value(s
,
510 S3I(s
)->tmp
.cert_verify_md
,
511 sizeof(S3I(s
)->tmp
.cert_verify_md
),
519 case SSL3_ST_SR_CERT_VRFY_A
:
520 case SSL3_ST_SR_CERT_VRFY_B
:
521 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
523 /* we should decide if we expected this one */
524 ret
= ssl3_get_cert_verify(s
);
528 if (S3I(s
)->next_proto_neg_seen
)
529 s
->internal
->state
= SSL3_ST_SR_NEXT_PROTO_A
;
531 s
->internal
->state
= SSL3_ST_SR_FINISHED_A
;
532 s
->internal
->init_num
= 0;
535 case SSL3_ST_SR_NEXT_PROTO_A
:
536 case SSL3_ST_SR_NEXT_PROTO_B
:
537 ret
= ssl3_get_next_proto(s
);
540 s
->internal
->init_num
= 0;
541 s
->internal
->state
= SSL3_ST_SR_FINISHED_A
;
544 case SSL3_ST_SR_FINISHED_A
:
545 case SSL3_ST_SR_FINISHED_B
:
546 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
547 ret
= ssl3_get_finished(s
, SSL3_ST_SR_FINISHED_A
,
548 SSL3_ST_SR_FINISHED_B
);
551 if (s
->internal
->hit
)
552 s
->internal
->state
= SSL_ST_OK
;
553 else if (s
->internal
->tlsext_ticket_expected
)
554 s
->internal
->state
= SSL3_ST_SW_SESSION_TICKET_A
;
556 s
->internal
->state
= SSL3_ST_SW_CHANGE_A
;
557 s
->internal
->init_num
= 0;
560 case SSL3_ST_SW_SESSION_TICKET_A
:
561 case SSL3_ST_SW_SESSION_TICKET_B
:
562 ret
= ssl3_send_newsession_ticket(s
);
565 s
->internal
->state
= SSL3_ST_SW_CHANGE_A
;
566 s
->internal
->init_num
= 0;
569 case SSL3_ST_SW_CERT_STATUS_A
:
570 case SSL3_ST_SW_CERT_STATUS_B
:
571 ret
= ssl3_send_cert_status(s
);
574 s
->internal
->state
= SSL3_ST_SW_KEY_EXCH_A
;
575 s
->internal
->init_num
= 0;
579 case SSL3_ST_SW_CHANGE_A
:
580 case SSL3_ST_SW_CHANGE_B
:
582 s
->session
->cipher
= S3I(s
)->tmp
.new_cipher
;
583 if (!tls1_setup_key_block(s
)) {
588 ret
= ssl3_send_change_cipher_spec(s
,
589 SSL3_ST_SW_CHANGE_A
, SSL3_ST_SW_CHANGE_B
);
593 s
->internal
->state
= SSL3_ST_SW_FINISHED_A
;
594 s
->internal
->init_num
= 0;
596 if (!tls1_change_cipher_state(
597 s
, SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
604 case SSL3_ST_SW_FINISHED_A
:
605 case SSL3_ST_SW_FINISHED_B
:
606 ret
= ssl3_send_finished(s
,
607 SSL3_ST_SW_FINISHED_A
, SSL3_ST_SW_FINISHED_B
,
608 TLS_MD_SERVER_FINISH_CONST
,
609 TLS_MD_SERVER_FINISH_CONST_SIZE
);
612 s
->internal
->state
= SSL3_ST_SW_FLUSH
;
613 if (s
->internal
->hit
) {
614 if (S3I(s
)->next_proto_neg_seen
) {
615 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
616 S3I(s
)->tmp
.next_state
=
617 SSL3_ST_SR_NEXT_PROTO_A
;
619 S3I(s
)->tmp
.next_state
=
620 SSL3_ST_SR_FINISHED_A
;
622 S3I(s
)->tmp
.next_state
= SSL_ST_OK
;
623 s
->internal
->init_num
= 0;
627 /* clean a few things up */
628 tls1_cleanup_key_block(s
);
630 BUF_MEM_free(s
->internal
->init_buf
);
631 s
->internal
->init_buf
= NULL
;
633 /* remove buffering on output */
634 ssl_free_wbio_buffer(s
);
636 s
->internal
->init_num
= 0;
638 /* skipped if we just sent a HelloRequest */
639 if (s
->internal
->renegotiate
== 2) {
640 s
->internal
->renegotiate
= 0;
641 s
->internal
->new_session
= 0;
643 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
645 s
->ctx
->internal
->stats
.sess_accept_good
++;
647 s
->internal
->handshake_func
= ssl3_accept
;
650 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
658 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
664 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
665 if (s
->internal
->debug
) {
666 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
671 if ((cb
!= NULL
) && (s
->internal
->state
!= state
)) {
672 new_state
= s
->internal
->state
;
673 s
->internal
->state
= state
;
674 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
675 s
->internal
->state
= new_state
;
681 /* BIO_flush(s->wbio); */
683 s
->internal
->in_handshake
--;
685 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
690 ssl3_send_hello_request(SSL
*s
)
694 memset(&cbb
, 0, sizeof(cbb
));
696 if (s
->internal
->state
== SSL3_ST_SW_HELLO_REQ_A
) {
697 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &hello
,
698 SSL3_MT_HELLO_REQUEST
))
700 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
703 s
->internal
->state
= SSL3_ST_SW_HELLO_REQ_B
;
706 /* SSL3_ST_SW_HELLO_REQ_B */
707 return (ssl3_handshake_write(s
));
716 ssl3_get_client_hello(SSL
*s
)
718 CBS cbs
, client_random
, session_id
, cookie
, cipher_suites
;
719 CBS compression_methods
;
720 uint16_t client_version
;
723 int i
, j
, ok
, al
, ret
= -1, cookie_valid
= 0;
726 unsigned char *p
, *d
;
728 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
730 const SSL_METHOD
*method
;
731 uint16_t shared_version
;
735 * We do this so that we will respond with our native type.
736 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
737 * This down switching should be handled by a different method.
738 * If we are SSLv3, we will respond with SSLv3, even if prompted with
741 if (s
->internal
->state
== SSL3_ST_SR_CLNT_HELLO_A
) {
742 s
->internal
->state
= SSL3_ST_SR_CLNT_HELLO_B
;
745 s
->internal
->first_packet
= 1;
746 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_CLNT_HELLO_B
,
747 SSL3_ST_SR_CLNT_HELLO_C
, SSL3_MT_CLIENT_HELLO
,
748 SSL3_RT_MAX_PLAIN_LENGTH
, &ok
);
751 s
->internal
->first_packet
= 0;
756 d
= p
= (unsigned char *)s
->internal
->init_msg
;
759 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
762 * Use version from inside client hello, not from record header.
763 * (may differ: see RFC 2246, Appendix E, second paragraph)
765 if (!CBS_get_u16(&cbs
, &client_version
))
768 if (ssl_max_shared_version(s
, client_version
, &shared_version
) != 1) {
769 SSLerror(s
, SSL_R_WRONG_VERSION_NUMBER
);
770 if ((s
->client_version
>> 8) == SSL3_VERSION_MAJOR
&&
771 !s
->internal
->enc_write_ctx
&& !s
->internal
->write_hash
) {
773 * Similar to ssl3_get_record, send alert using remote
776 s
->version
= s
->client_version
;
778 al
= SSL_AD_PROTOCOL_VERSION
;
781 s
->client_version
= client_version
;
782 s
->version
= shared_version
;
784 if ((method
= tls1_get_server_method(shared_version
)) == NULL
)
785 method
= dtls1_get_server_method(shared_version
);
786 if (method
== NULL
) {
787 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
792 if (!CBS_get_bytes(&cbs
, &client_random
, SSL3_RANDOM_SIZE
))
794 if (!CBS_get_u8_length_prefixed(&cbs
, &session_id
))
798 * If we require cookies (DTLS) and this ClientHello doesn't
799 * contain one, just return since we do not want to
800 * allocate any memory yet. So check cookie length...
802 if (SSL_IS_DTLS(s
)) {
803 if (!CBS_get_u8_length_prefixed(&cbs
, &cookie
))
805 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
806 if (CBS_len(&cookie
) == 0)
811 if (!CBS_write_bytes(&client_random
, s
->s3
->client_random
,
812 sizeof(s
->s3
->client_random
), NULL
))
815 s
->internal
->hit
= 0;
818 * Versions before 0.9.7 always allow clients to resume sessions in
819 * renegotiation. 0.9.7 and later allow this by default, but optionally
820 * ignore resumption requests with flag
821 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag
822 * rather than a change to default behavior so that applications
823 * relying on this for security won't even compile against older
826 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated()
827 * to request renegotiation but not a new session (s->internal->new_session
828 * remains unset): for servers, this essentially just means that the
829 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
832 if ((s
->internal
->new_session
&& (s
->internal
->options
&
833 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
834 if (!ssl_get_new_session(s
, 1))
837 /* XXX - pass CBS through instead... */
838 i
= ssl_get_prev_session(s
,
839 (unsigned char *)CBS_data(&session_id
),
840 CBS_len(&session_id
), end
);
841 if (i
== 1) { /* previous session */
842 s
->internal
->hit
= 1;
847 if (!ssl_get_new_session(s
, 1))
852 if (SSL_IS_DTLS(s
)) {
854 * The ClientHello may contain a cookie even if the HelloVerify
855 * message has not been sent - make sure that it does not cause
858 if (CBS_len(&cookie
) > sizeof(D1I(s
)->rcvd_cookie
)) {
859 al
= SSL_AD_DECODE_ERROR
;
860 SSLerror(s
, SSL_R_COOKIE_MISMATCH
);
864 /* Verify the cookie if appropriate option is set. */
865 if ((SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) &&
866 CBS_len(&cookie
) > 0) {
869 /* XXX - rcvd_cookie seems to only be used here... */
870 if (!CBS_write_bytes(&cookie
, D1I(s
)->rcvd_cookie
,
871 sizeof(D1I(s
)->rcvd_cookie
), &cookie_len
))
874 if (s
->ctx
->internal
->app_verify_cookie_cb
!= NULL
) {
875 if (s
->ctx
->internal
->app_verify_cookie_cb(s
,
876 D1I(s
)->rcvd_cookie
, cookie_len
) == 0) {
877 al
= SSL_AD_HANDSHAKE_FAILURE
;
878 SSLerror(s
, SSL_R_COOKIE_MISMATCH
);
881 /* else cookie verification succeeded */
882 /* XXX - can d1->cookie_len > sizeof(rcvd_cookie) ? */
883 } else if (timingsafe_memcmp(D1I(s
)->rcvd_cookie
,
884 D1I(s
)->cookie
, D1I(s
)->cookie_len
) != 0) {
885 /* default verification */
886 al
= SSL_AD_HANDSHAKE_FAILURE
;
887 SSLerror(s
, SSL_R_COOKIE_MISMATCH
);
894 if (!CBS_get_u16_length_prefixed(&cbs
, &cipher_suites
))
897 /* XXX - This logic seems wrong... */
898 if (CBS_len(&cipher_suites
) == 0 && CBS_len(&session_id
) != 0) {
899 /* we need a cipher if we are not resuming a session */
900 al
= SSL_AD_ILLEGAL_PARAMETER
;
901 SSLerror(s
, SSL_R_NO_CIPHERS_SPECIFIED
);
905 if (CBS_len(&cipher_suites
) > 0) {
906 if ((ciphers
= ssl_bytes_to_cipher_list(s
,
907 CBS_data(&cipher_suites
), CBS_len(&cipher_suites
))) == NULL
)
911 /* If it is a hit, check that the cipher is in the list */
912 if (s
->internal
->hit
&& CBS_len(&cipher_suites
) > 0) {
914 id
= s
->session
->cipher
->id
;
916 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
917 c
= sk_SSL_CIPHER_value(ciphers
, i
);
925 * We need to have the cipher in the cipher
926 * list if we are asked to reuse it
928 al
= SSL_AD_ILLEGAL_PARAMETER
;
929 SSLerror(s
, SSL_R_REQUIRED_CIPHER_MISSING
);
934 if (!CBS_get_u8_length_prefixed(&cbs
, &compression_methods
))
938 while (CBS_len(&compression_methods
) > 0) {
939 if (!CBS_get_u8(&compression_methods
, &comp_method
))
941 if (comp_method
== 0)
944 if (comp_null
== 0) {
945 al
= SSL_AD_DECODE_ERROR
;
946 SSLerror(s
, SSL_R_NO_COMPRESSION_SPECIFIED
);
950 p
= (unsigned char *)CBS_data(&cbs
);
953 if (!ssl_parse_clienthello_tlsext(s
, &p
, d
, n
, &al
)) {
954 /* 'al' set by ssl_parse_clienthello_tlsext */
955 SSLerror(s
, SSL_R_PARSE_TLSEXT
);
958 if (ssl_check_clienthello_tlsext_early(s
) <= 0) {
959 SSLerror(s
, SSL_R_CLIENTHELLO_TLSEXT
);
964 * Check if we want to use external pre-shared secret for this
965 * handshake for not reused session only. We need to generate
966 * server_random before calling tls_session_secret_cb in order to allow
967 * SessionTicket processing to use it in key derivation.
969 arc4random_buf(s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
971 if (!s
->internal
->hit
&& s
->internal
->tls_session_secret_cb
) {
972 SSL_CIPHER
*pref_cipher
= NULL
;
974 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
975 if (s
->internal
->tls_session_secret_cb(s
, s
->session
->master_key
,
976 &s
->session
->master_key_length
, ciphers
, &pref_cipher
,
977 s
->internal
->tls_session_secret_cb_arg
)) {
978 s
->internal
->hit
= 1;
979 s
->session
->ciphers
= ciphers
;
980 s
->session
->verify_result
= X509_V_OK
;
984 /* check if some cipher was preferred by call back */
985 pref_cipher
= pref_cipher
? pref_cipher
:
986 ssl3_choose_cipher(s
, s
->session
->ciphers
,
988 if (pref_cipher
== NULL
) {
989 al
= SSL_AD_HANDSHAKE_FAILURE
;
990 SSLerror(s
, SSL_R_NO_SHARED_CIPHER
);
994 s
->session
->cipher
= pref_cipher
;
996 sk_SSL_CIPHER_free(s
->cipher_list
);
997 sk_SSL_CIPHER_free(s
->internal
->cipher_list_by_id
);
999 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1000 s
->internal
->cipher_list_by_id
=
1001 sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1006 * Given s->session->ciphers and SSL_get_ciphers, we must
1010 if (!s
->internal
->hit
) {
1011 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1012 s
->session
->ciphers
= ciphers
;
1013 if (ciphers
== NULL
) {
1014 al
= SSL_AD_ILLEGAL_PARAMETER
;
1015 SSLerror(s
, SSL_R_NO_CIPHERS_PASSED
);
1019 c
= ssl3_choose_cipher(s
, s
->session
->ciphers
,
1020 SSL_get_ciphers(s
));
1023 al
= SSL_AD_HANDSHAKE_FAILURE
;
1024 SSLerror(s
, SSL_R_NO_SHARED_CIPHER
);
1027 S3I(s
)->tmp
.new_cipher
= c
;
1029 S3I(s
)->tmp
.new_cipher
= s
->session
->cipher
;
1032 if (!tls1_handshake_hash_init(s
))
1035 alg_k
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
1036 if (!(SSL_USE_SIGALGS(s
) || (alg_k
& SSL_kGOST
)) ||
1037 !(s
->verify_mode
& SSL_VERIFY_PEER
)) {
1038 if (!tls1_digest_cached_records(s
)) {
1039 al
= SSL_AD_INTERNAL_ERROR
;
1045 * We now have the following setup.
1047 * cipher_list - our prefered list of ciphers
1048 * ciphers - the clients prefered list of ciphers
1049 * compression - basically ignored right now
1050 * ssl version is set - sslv3
1051 * s->session - The ssl session has been setup.
1052 * s->internal->hit - session reuse flag
1053 * s->tmp.new_cipher - the new cipher to use.
1056 /* Handles TLS extensions that we couldn't check earlier */
1057 if (ssl_check_clienthello_tlsext_late(s
) <= 0) {
1058 SSLerror(s
, SSL_R_CLIENTHELLO_TLSEXT
);
1062 ret
= cookie_valid
? 2 : 1;
1066 al
= SSL_AD_DECODE_ERROR
;
1067 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1069 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1072 sk_SSL_CIPHER_free(ciphers
);
1078 ssl3_send_server_hello(SSL
*s
)
1080 unsigned char *bufend
;
1081 unsigned char *p
, *d
;
1082 CBB cbb
, session_id
;
1086 memset(&cbb
, 0, sizeof(cbb
));
1088 bufend
= (unsigned char *)s
->internal
->init_buf
->data
+ SSL3_RT_MAX_PLAIN_LENGTH
;
1090 if (s
->internal
->state
== SSL3_ST_SW_SRVR_HELLO_A
) {
1091 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_SERVER_HELLO
);
1093 if (!CBB_init_fixed(&cbb
, p
, bufend
- p
))
1096 if (!CBB_add_u16(&cbb
, s
->version
))
1098 if (!CBB_add_bytes(&cbb
, s
->s3
->server_random
,
1099 sizeof(s
->s3
->server_random
)))
1103 * There are several cases for the session ID to send
1104 * back in the server hello:
1106 * - For session reuse from the session cache,
1107 * we send back the old session ID.
1108 * - If stateless session reuse (using a session ticket)
1109 * is successful, we send back the client's "session ID"
1110 * (which doesn't actually identify the session).
1111 * - If it is a new session, we send back the new
1113 * - However, if we want the new session to be single-use,
1114 * we send back a 0-length session ID.
1116 * s->internal->hit is non-zero in either case of session reuse,
1117 * so the following won't overwrite an ID that we're supposed
1120 if (!(s
->ctx
->internal
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
1121 && !s
->internal
->hit
)
1122 s
->session
->session_id_length
= 0;
1124 sl
= s
->session
->session_id_length
;
1125 if (sl
> (int)sizeof(s
->session
->session_id
)) {
1126 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1130 if (!CBB_add_u8_length_prefixed(&cbb
, &session_id
))
1132 if (!CBB_add_bytes(&session_id
, s
->session
->session_id
, sl
))
1136 if (!CBB_add_u16(&cbb
,
1137 ssl3_cipher_get_value(S3I(s
)->tmp
.new_cipher
)))
1140 /* Compression method. */
1141 if (!CBB_add_u8(&cbb
, 0))
1144 if (!CBB_finish(&cbb
, NULL
, &outlen
))
1147 if ((p
= ssl_add_serverhello_tlsext(s
, p
+ outlen
,
1149 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1153 ssl3_handshake_msg_finish(s
, p
- d
);
1156 /* SSL3_ST_SW_SRVR_HELLO_B */
1157 return (ssl3_handshake_write(s
));
1166 ssl3_send_server_done(SSL
*s
)
1170 memset(&cbb
, 0, sizeof(cbb
));
1172 if (s
->internal
->state
== SSL3_ST_SW_SRVR_DONE_A
) {
1173 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &done
,
1174 SSL3_MT_SERVER_DONE
))
1176 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
1179 s
->internal
->state
= SSL3_ST_SW_SRVR_DONE_B
;
1182 /* SSL3_ST_SW_SRVR_DONE_B */
1183 return (ssl3_handshake_write(s
));
1192 ssl3_send_server_kex_dhe(SSL
*s
, CBB
*cbb
)
1194 CBB dh_p
, dh_g
, dh_Ys
;
1195 DH
*dh
= NULL
, *dhp
;
1196 unsigned char *data
;
1199 if (s
->cert
->dh_tmp_auto
!= 0) {
1200 if ((dhp
= ssl_get_auto_dh(s
)) == NULL
) {
1201 al
= SSL_AD_INTERNAL_ERROR
;
1202 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1206 dhp
= s
->cert
->dh_tmp
;
1208 if (dhp
== NULL
&& s
->cert
->dh_tmp_cb
!= NULL
)
1209 dhp
= s
->cert
->dh_tmp_cb(s
, 0,
1210 SSL_C_PKEYLENGTH(S3I(s
)->tmp
.new_cipher
));
1213 al
= SSL_AD_HANDSHAKE_FAILURE
;
1214 SSLerror(s
, SSL_R_MISSING_TMP_DH_KEY
);
1218 if (S3I(s
)->tmp
.dh
!= NULL
) {
1219 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1223 if (s
->cert
->dh_tmp_auto
!= 0) {
1225 } else if ((dh
= DHparams_dup(dhp
)) == NULL
) {
1226 SSLerror(s
, ERR_R_DH_LIB
);
1229 S3I(s
)->tmp
.dh
= dh
;
1230 if (!DH_generate_key(dh
)) {
1231 SSLerror(s
, ERR_R_DH_LIB
);
1236 * Serialize the DH parameters and public key.
1238 if (!CBB_add_u16_length_prefixed(cbb
, &dh_p
))
1240 if (!CBB_add_space(&dh_p
, &data
, BN_num_bytes(dh
->p
)))
1242 BN_bn2bin(dh
->p
, data
);
1244 if (!CBB_add_u16_length_prefixed(cbb
, &dh_g
))
1246 if (!CBB_add_space(&dh_g
, &data
, BN_num_bytes(dh
->g
)))
1248 BN_bn2bin(dh
->g
, data
);
1250 if (!CBB_add_u16_length_prefixed(cbb
, &dh_Ys
))
1252 if (!CBB_add_space(&dh_Ys
, &data
, BN_num_bytes(dh
->pub_key
)))
1254 BN_bn2bin(dh
->pub_key
, data
);
1256 if (!CBB_flush(cbb
))
1262 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1268 ssl3_send_server_kex_ecdhe_ecp(SSL
*s
, int nid
, CBB
*cbb
)
1271 unsigned char *data
;
1272 EC_KEY
*ecdh
= NULL
, *ecdhp
;
1273 const EC_GROUP
*group
;
1274 unsigned char *encodedPoint
= NULL
;
1277 BN_CTX
*bn_ctx
= NULL
;
1280 ecdhp
= s
->cert
->ecdh_tmp
;
1281 if (s
->cert
->ecdh_tmp_auto
!= 0) {
1282 if (nid
!= NID_undef
)
1283 ecdhp
= EC_KEY_new_by_curve_name(nid
);
1284 } else if (ecdhp
== NULL
&& s
->cert
->ecdh_tmp_cb
!= NULL
) {
1285 ecdhp
= s
->cert
->ecdh_tmp_cb(s
, 0,
1286 SSL_C_PKEYLENGTH(S3I(s
)->tmp
.new_cipher
));
1288 if (ecdhp
== NULL
) {
1289 al
= SSL_AD_HANDSHAKE_FAILURE
;
1290 SSLerror(s
, SSL_R_MISSING_TMP_ECDH_KEY
);
1294 if (S3I(s
)->tmp
.ecdh
!= NULL
) {
1295 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1299 /* Duplicate the ECDH structure. */
1300 if (s
->cert
->ecdh_tmp_auto
!= 0) {
1302 } else if ((ecdh
= EC_KEY_dup(ecdhp
)) == NULL
) {
1303 SSLerror(s
, ERR_R_ECDH_LIB
);
1306 S3I(s
)->tmp
.ecdh
= ecdh
;
1308 if ((EC_KEY_get0_public_key(ecdh
) == NULL
) ||
1309 (EC_KEY_get0_private_key(ecdh
) == NULL
) ||
1310 (s
->internal
->options
& SSL_OP_SINGLE_ECDH_USE
)) {
1311 if (!EC_KEY_generate_key(ecdh
)) {
1312 SSLerror(s
, ERR_R_ECDH_LIB
);
1317 if (((group
= EC_KEY_get0_group(ecdh
)) == NULL
) ||
1318 (EC_KEY_get0_public_key(ecdh
) == NULL
) ||
1319 (EC_KEY_get0_private_key(ecdh
) == NULL
)) {
1320 SSLerror(s
, ERR_R_ECDH_LIB
);
1325 * Only named curves are supported in ECDH ephemeral key exchanges.
1326 * For supported named curves, curve_id is non-zero.
1328 if ((curve_id
= tls1_ec_nid2curve_id(
1329 EC_GROUP_get_curve_name(group
))) == 0) {
1330 SSLerror(s
, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1335 * Encode the public key. First check the size of encoding and
1336 * allocate memory accordingly.
1338 encodedlen
= EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
1339 POINT_CONVERSION_UNCOMPRESSED
, NULL
, 0, NULL
);
1341 encodedPoint
= malloc(encodedlen
);
1343 bn_ctx
= BN_CTX_new();
1344 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
)) {
1345 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1349 encodedlen
= EC_POINT_point2oct(group
, EC_KEY_get0_public_key(ecdh
),
1350 POINT_CONVERSION_UNCOMPRESSED
, encodedPoint
, encodedlen
, bn_ctx
);
1352 if (encodedlen
== 0) {
1353 SSLerror(s
, ERR_R_ECDH_LIB
);
1357 BN_CTX_free(bn_ctx
);
1361 * Only named curves are supported in ECDH ephemeral key exchanges.
1362 * In this case the ServerKeyExchange message has:
1363 * [1 byte CurveType], [2 byte CurveName]
1364 * [1 byte length of encoded point], followed by
1365 * the actual encoded point itself.
1367 if (!CBB_add_u8(cbb
, NAMED_CURVE_TYPE
))
1369 if (!CBB_add_u16(cbb
, curve_id
))
1371 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
1373 if (!CBB_add_space(&ecpoint
, &data
, encodedlen
))
1376 memcpy(data
, encodedPoint
, encodedlen
);
1379 encodedPoint
= NULL
;
1381 if (!CBB_flush(cbb
))
1387 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1390 BN_CTX_free(bn_ctx
);
1396 ssl3_send_server_kex_ecdhe_ecx(SSL
*s
, int nid
, CBB
*cbb
)
1398 uint8_t *public_key
= NULL
;
1403 /* Generate an X25519 key pair. */
1404 if (S3I(s
)->tmp
.x25519
!= NULL
) {
1405 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1408 if ((S3I(s
)->tmp
.x25519
= malloc(X25519_KEY_LENGTH
)) == NULL
)
1410 if ((public_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
1412 X25519_keypair(public_key
, S3I(s
)->tmp
.x25519
);
1414 /* Serialize public key. */
1415 if ((curve_id
= tls1_ec_nid2curve_id(nid
)) == 0) {
1416 SSLerror(s
, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1420 if (!CBB_add_u8(cbb
, NAMED_CURVE_TYPE
))
1422 if (!CBB_add_u16(cbb
, curve_id
))
1424 if (!CBB_add_u8_length_prefixed(cbb
, &ecpoint
))
1426 if (!CBB_add_bytes(&ecpoint
, public_key
, X25519_KEY_LENGTH
))
1428 if (!CBB_flush(cbb
))
1440 ssl3_send_server_kex_ecdhe(SSL
*s
, CBB
*cbb
)
1444 nid
= tls1_get_shared_curve(s
);
1446 if (s
->cert
->ecdh_tmp_auto
!= 0 && nid
== NID_X25519
)
1447 return ssl3_send_server_kex_ecdhe_ecx(s
, nid
, cbb
);
1449 return ssl3_send_server_kex_ecdhe_ecp(s
, nid
, cbb
);
1453 ssl3_send_server_key_exchange(SSL
*s
)
1456 unsigned char *params
= NULL
;
1459 unsigned char md_buf
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
1462 const EVP_MD
*md
= NULL
;
1463 unsigned char *p
, *d
;
1464 int al
, i
, j
, n
, kn
;
1469 memset(&cbb
, 0, sizeof(cbb
));
1471 EVP_MD_CTX_init(&md_ctx
);
1472 if (s
->internal
->state
== SSL3_ST_SW_KEY_EXCH_A
) {
1473 type
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
1475 buf
= s
->internal
->init_buf
;
1477 if (!CBB_init(&cbb
, 0))
1480 if (type
& SSL_kDHE
) {
1481 if (ssl3_send_server_kex_dhe(s
, &cbb
) != 1)
1483 } else if (type
& SSL_kECDHE
) {
1484 if (ssl3_send_server_kex_ecdhe(s
, &cbb
) != 1)
1487 al
= SSL_AD_HANDSHAKE_FAILURE
;
1488 SSLerror(s
, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1492 if (!CBB_finish(&cbb
, ¶ms
, ¶ms_len
))
1495 if (!(S3I(s
)->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)) {
1496 if ((pkey
= ssl_get_sign_pkey(
1497 s
, S3I(s
)->tmp
.new_cipher
, &md
)) == NULL
) {
1498 al
= SSL_AD_DECODE_ERROR
;
1501 kn
= EVP_PKEY_size(pkey
);
1507 if (!BUF_MEM_grow_clean(buf
, ssl3_handshake_msg_hdr_len(s
) +
1509 SSLerror(s
, ERR_LIB_BUF
);
1513 d
= p
= ssl3_handshake_msg_start(s
,
1514 SSL3_MT_SERVER_KEY_EXCHANGE
);
1516 memcpy(p
, params
, params_len
);
1527 * n is the length of the params, they start at &(d[4])
1528 * and p points to the space at the end.
1530 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1533 if (!EVP_DigestInit_ex(&md_ctx
, EVP_md5_sha1(),
1536 EVP_DigestUpdate(&md_ctx
, s
->s3
->client_random
,
1538 EVP_DigestUpdate(&md_ctx
, s
->s3
->server_random
,
1540 EVP_DigestUpdate(&md_ctx
, d
, n
);
1541 EVP_DigestFinal_ex(&md_ctx
, q
,
1542 (unsigned int *)&i
);
1545 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
1546 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
1547 SSLerror(s
, ERR_R_RSA_LIB
);
1553 /* Send signature algorithm. */
1554 if (SSL_USE_SIGALGS(s
)) {
1555 if (!tls12_get_sigandhash(p
, pkey
, md
)) {
1556 /* Should never happen */
1557 al
= SSL_AD_INTERNAL_ERROR
;
1558 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
1563 EVP_SignInit_ex(&md_ctx
, md
, NULL
);
1564 EVP_SignUpdate(&md_ctx
,
1565 s
->s3
->client_random
,
1567 EVP_SignUpdate(&md_ctx
,
1568 s
->s3
->server_random
,
1570 EVP_SignUpdate(&md_ctx
, d
, n
);
1571 if (!EVP_SignFinal(&md_ctx
, &p
[2],
1572 (unsigned int *)&i
, pkey
)) {
1573 SSLerror(s
, ERR_R_EVP_LIB
);
1578 if (SSL_USE_SIGALGS(s
))
1581 /* Is this error check actually needed? */
1582 al
= SSL_AD_HANDSHAKE_FAILURE
;
1583 SSLerror(s
, SSL_R_UNKNOWN_PKEY_TYPE
);
1588 ssl3_handshake_msg_finish(s
, n
);
1591 s
->internal
->state
= SSL3_ST_SW_KEY_EXCH_B
;
1593 EVP_MD_CTX_cleanup(&md_ctx
);
1595 return (ssl3_handshake_write(s
));
1598 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1601 EVP_MD_CTX_cleanup(&md_ctx
);
1608 ssl3_send_certificate_request(SSL
*s
)
1610 unsigned char *p
, *d
;
1611 int i
, j
, nl
, off
, n
;
1612 STACK_OF(X509_NAME
) *sk
= NULL
;
1616 if (s
->internal
->state
== SSL3_ST_SW_CERT_REQ_A
) {
1617 buf
= s
->internal
->init_buf
;
1619 d
= p
= ssl3_handshake_msg_start(s
,
1620 SSL3_MT_CERTIFICATE_REQUEST
);
1622 /* get the list of acceptable cert types */
1624 n
= ssl3_get_req_cert_type(s
, p
);
1629 if (SSL_USE_SIGALGS(s
)) {
1630 nl
= tls12_get_req_sig_algs(s
, p
+ 2);
1640 sk
= SSL_get_client_CA_list(s
);
1643 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
1644 name
= sk_X509_NAME_value(sk
, i
);
1645 j
= i2d_X509_NAME(name
, NULL
);
1646 if (!BUF_MEM_grow_clean(buf
,
1647 ssl3_handshake_msg_hdr_len(s
) + n
+ j
1649 SSLerror(s
, ERR_R_BUF_LIB
);
1652 p
= ssl3_handshake_msg_start(s
,
1653 SSL3_MT_CERTIFICATE_REQUEST
) + n
;
1655 i2d_X509_NAME(name
, &p
);
1660 /* else no CA names */
1661 p
= ssl3_handshake_msg_start(s
,
1662 SSL3_MT_CERTIFICATE_REQUEST
) + off
;
1665 ssl3_handshake_msg_finish(s
, n
);
1667 s
->internal
->state
= SSL3_ST_SW_CERT_REQ_B
;
1670 /* SSL3_ST_SW_CERT_REQ_B */
1671 return (ssl3_handshake_write(s
));
1677 ssl3_get_client_kex_rsa(SSL
*s
, unsigned char *p
, long n
)
1679 unsigned char fakekey
[SSL_MAX_MASTER_KEY_LENGTH
];
1682 EVP_PKEY
*pkey
= NULL
;
1687 arc4random_buf(fakekey
, sizeof(fakekey
));
1688 fakekey
[0] = s
->client_version
>> 8;
1689 fakekey
[1] = s
->client_version
& 0xff;
1691 pkey
= s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1692 if ((pkey
== NULL
) || (pkey
->type
!= EVP_PKEY_RSA
) ||
1693 (pkey
->pkey
.rsa
== NULL
)) {
1694 al
= SSL_AD_HANDSHAKE_FAILURE
;
1695 SSLerror(s
, SSL_R_MISSING_RSA_CERTIFICATE
);
1698 rsa
= pkey
->pkey
.rsa
;
1704 SSLerror(s
, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1709 i
= RSA_private_decrypt((int)n
, p
, p
, rsa
, RSA_PKCS1_PADDING
);
1715 if (i
!= SSL_MAX_MASTER_KEY_LENGTH
) {
1716 al
= SSL_AD_DECODE_ERROR
;
1717 /* SSLerror(s, SSL_R_BAD_RSA_DECRYPT); */
1720 if (p
- d
+ 2 > n
) /* needed in the SSL3 case */
1722 if ((al
== -1) && !((p
[0] == (s
->client_version
>> 8)) &&
1723 (p
[1] == (s
->client_version
& 0xff)))) {
1725 * The premaster secret must contain the same version
1726 * number as the ClientHello to detect version rollback
1727 * attacks (strangely, the protocol does not offer such
1728 * protection for DH ciphersuites).
1729 * However, buggy clients exist that send the negotiated
1730 * protocol version instead if the server does not
1731 * support the requested protocol version.
1732 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1735 if (!((s
->internal
->options
& SSL_OP_TLS_ROLLBACK_BUG
) &&
1736 (p
[0] == (s
->version
>> 8)) &&
1737 (p
[1] == (s
->version
& 0xff)))) {
1738 al
= SSL_AD_DECODE_ERROR
;
1739 /* SSLerror(s, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1742 * The Klima-Pokorny-Rosa extension of
1743 * Bleichenbacher's attack
1744 * (http://eprint.iacr.org/2003/052/) exploits
1745 * the version number check as a "bad version
1746 * oracle" -- an alert would reveal that the
1747 * plaintext corresponding to some ciphertext
1748 * made up by the adversary is properly
1749 * formatted except that the version number is
1751 * To avoid such attacks, we should treat this
1752 * just like any other decryption error.
1759 * Some decryption failure -- use random value instead
1760 * as countermeasure against Bleichenbacher's attack
1761 * on PKCS #1 v1.5 RSA padding (see RFC 2246,
1764 i
= SSL_MAX_MASTER_KEY_LENGTH
;
1768 s
->session
->master_key_length
=
1769 tls1_generate_master_secret(s
,
1770 s
->session
->master_key
, p
, i
);
1772 explicit_bzero(p
, i
);
1776 al
= SSL_AD_DECODE_ERROR
;
1777 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1779 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1785 ssl3_get_client_kex_dhe(SSL
*s
, unsigned char *p
, long n
)
1795 CBS_init(&cbs
, p
, n
);
1797 if (!CBS_get_u16_length_prefixed(&cbs
, &dh_Yc
))
1800 if (CBS_len(&cbs
) != 0)
1803 if (S3I(s
)->tmp
.dh
== NULL
) {
1804 al
= SSL_AD_HANDSHAKE_FAILURE
;
1805 SSLerror(s
, SSL_R_MISSING_TMP_DH_KEY
);
1808 dh
= S3I(s
)->tmp
.dh
;
1810 if ((bn
= BN_bin2bn(CBS_data(&dh_Yc
), CBS_len(&dh_Yc
), NULL
)) == NULL
) {
1811 SSLerror(s
, SSL_R_BN_LIB
);
1815 key_size
= DH_compute_key(p
, bn
, dh
);
1816 if (key_size
<= 0) {
1817 SSLerror(s
, ERR_R_DH_LIB
);
1822 s
->session
->master_key_length
=
1823 tls1_generate_master_secret(
1824 s
, s
->session
->master_key
, p
, key_size
);
1826 explicit_bzero(p
, key_size
);
1828 DH_free(S3I(s
)->tmp
.dh
);
1829 S3I(s
)->tmp
.dh
= NULL
;
1836 al
= SSL_AD_DECODE_ERROR
;
1837 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
1839 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1845 ssl3_get_client_kex_ecdhe_ecp(SSL
*s
, unsigned char *p
, long n
)
1847 EC_KEY
*srvr_ecdh
= NULL
;
1848 EVP_PKEY
*clnt_pub_pkey
= NULL
;
1849 EC_POINT
*clnt_ecpoint
= NULL
;
1850 BN_CTX
*bn_ctx
= NULL
;
1856 const EC_GROUP
*group
;
1857 const BIGNUM
*priv_key
;
1859 /* Initialize structures for server's ECDH key pair. */
1860 if ((srvr_ecdh
= EC_KEY_new()) == NULL
) {
1861 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1866 * Use the ephemeral values we saved when
1867 * generating the ServerKeyExchange message.
1869 tkey
= S3I(s
)->tmp
.ecdh
;
1871 group
= EC_KEY_get0_group(tkey
);
1872 priv_key
= EC_KEY_get0_private_key(tkey
);
1874 if (!EC_KEY_set_group(srvr_ecdh
, group
) ||
1875 !EC_KEY_set_private_key(srvr_ecdh
, priv_key
)) {
1876 SSLerror(s
, ERR_R_EC_LIB
);
1880 /* Let's get client's public key */
1881 if ((clnt_ecpoint
= EC_POINT_new(group
)) == NULL
) {
1882 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1887 /* Client Publickey was in Client Certificate */
1888 if (((clnt_pub_pkey
= X509_get_pubkey(
1889 s
->session
->peer
)) == NULL
) ||
1890 (clnt_pub_pkey
->type
!= EVP_PKEY_EC
)) {
1892 * XXX: For now, we do not support client
1893 * authentication using ECDH certificates
1894 * so this branch (n == 0L) of the code is
1895 * never executed. When that support is
1896 * added, we ought to ensure the key
1897 * received in the certificate is
1898 * authorized for key agreement.
1899 * ECDH_compute_key implicitly checks that
1900 * the two ECDH shares are for the same
1903 al
= SSL_AD_HANDSHAKE_FAILURE
;
1904 SSLerror(s
, SSL_R_UNABLE_TO_DECODE_ECDH_CERTS
);
1908 if (EC_POINT_copy(clnt_ecpoint
,
1909 EC_KEY_get0_public_key(clnt_pub_pkey
->pkey
.ec
))
1911 SSLerror(s
, ERR_R_EC_LIB
);
1914 ret
= 2; /* Skip certificate verify processing */
1917 * Get client's public key from encoded point
1918 * in the ClientKeyExchange message.
1920 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
1921 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
1925 /* Get encoded point length */
1930 SSLerror(s
, ERR_R_EC_LIB
);
1933 if (EC_POINT_oct2point(group
,
1934 clnt_ecpoint
, p
, i
, bn_ctx
) == 0) {
1935 SSLerror(s
, ERR_R_EC_LIB
);
1939 * p is pointing to somewhere in the buffer
1940 * currently, so set it to the start.
1942 p
= (unsigned char *)s
->internal
->init_buf
->data
;
1945 /* Compute the shared pre-master secret */
1946 key_size
= ECDH_size(srvr_ecdh
);
1947 if (key_size
<= 0) {
1948 SSLerror(s
, ERR_R_ECDH_LIB
);
1951 i
= ECDH_compute_key(p
, key_size
, clnt_ecpoint
, srvr_ecdh
,
1954 SSLerror(s
, ERR_R_ECDH_LIB
);
1958 EVP_PKEY_free(clnt_pub_pkey
);
1959 EC_POINT_free(clnt_ecpoint
);
1960 EC_KEY_free(srvr_ecdh
);
1961 BN_CTX_free(bn_ctx
);
1962 EC_KEY_free(S3I(s
)->tmp
.ecdh
);
1963 S3I(s
)->tmp
.ecdh
= NULL
;
1965 /* Compute the master secret */
1966 s
->session
->master_key_length
=
1967 tls1_generate_master_secret(
1968 s
, s
->session
->master_key
, p
, i
);
1970 explicit_bzero(p
, i
);
1974 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1976 EVP_PKEY_free(clnt_pub_pkey
);
1977 EC_POINT_free(clnt_ecpoint
);
1978 EC_KEY_free(srvr_ecdh
);
1979 BN_CTX_free(bn_ctx
);
1984 ssl3_get_client_kex_ecdhe_ecx(SSL
*s
, unsigned char *p
, long n
)
1986 uint8_t *shared_key
= NULL
;
1993 CBS_init(&cbs
, p
, n
);
1994 if (!CBS_get_u8_length_prefixed(&cbs
, &ecpoint
))
1996 if (CBS_len(&ecpoint
) != X25519_KEY_LENGTH
)
1999 if ((shared_key
= malloc(X25519_KEY_LENGTH
)) == NULL
)
2001 if (!X25519(shared_key
, S3I(s
)->tmp
.x25519
, CBS_data(&ecpoint
)))
2004 explicit_bzero(S3I(s
)->tmp
.x25519
, X25519_KEY_LENGTH
);
2005 free(S3I(s
)->tmp
.x25519
);
2006 S3I(s
)->tmp
.x25519
= NULL
;
2008 s
->session
->master_key_length
=
2009 tls1_generate_master_secret(
2010 s
, s
->session
->master_key
, shared_key
, X25519_KEY_LENGTH
);
2015 if (shared_key
!= NULL
)
2016 explicit_bzero(shared_key
, X25519_KEY_LENGTH
);
2023 ssl3_get_client_kex_ecdhe(SSL
*s
, unsigned char *p
, long n
)
2025 if (S3I(s
)->tmp
.x25519
!= NULL
)
2026 return ssl3_get_client_kex_ecdhe_ecx(s
, p
, n
);
2028 return ssl3_get_client_kex_ecdhe_ecp(s
, p
, n
);
2032 ssl3_get_client_kex_gost(SSL
*s
, unsigned char *p
, long n
)
2035 EVP_PKEY_CTX
*pkey_ctx
;
2036 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
2037 unsigned char premaster_secret
[32], *start
;
2038 size_t outlen
= 32, inlen
;
2039 unsigned long alg_a
;
2045 /* Get our certificate private key*/
2046 alg_a
= S3I(s
)->tmp
.new_cipher
->algorithm_auth
;
2047 if (alg_a
& SSL_aGOST01
)
2048 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2050 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
2051 EVP_PKEY_decrypt_init(pkey_ctx
);
2053 * If client certificate is present and is of the same type,
2054 * maybe use it for key exchange.
2055 * Don't mind errors from EVP_PKEY_derive_set_peer, because
2056 * it is completely valid to use a client certificate for
2057 * authorization only.
2059 client_pub_pkey
= X509_get_pubkey(s
->session
->peer
);
2060 if (client_pub_pkey
) {
2061 if (EVP_PKEY_derive_set_peer(pkey_ctx
,
2062 client_pub_pkey
) <= 0)
2067 /* Decrypt session key */
2068 if (ASN1_get_object((const unsigned char **)&p
, &Tlen
, &Ttag
,
2069 &Tclass
, n
) != V_ASN1_CONSTRUCTED
||
2070 Ttag
!= V_ASN1_SEQUENCE
|| Tclass
!= V_ASN1_UNIVERSAL
) {
2071 SSLerror(s
, SSL_R_DECRYPTION_FAILED
);
2076 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
,
2077 start
, inlen
) <=0) {
2078 SSLerror(s
, SSL_R_DECRYPTION_FAILED
);
2081 /* Generate master secret */
2082 s
->session
->master_key_length
=
2083 tls1_generate_master_secret(
2084 s
, s
->session
->master_key
, premaster_secret
, 32);
2085 /* Check if pubkey from client certificate was used */
2086 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1,
2087 EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
2092 EVP_PKEY_free(client_pub_pkey
);
2093 EVP_PKEY_CTX_free(pkey_ctx
);
2100 al
= SSL_AD_DECODE_ERROR
;
2101 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
2102 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2108 ssl3_get_client_key_exchange(SSL
*s
)
2110 unsigned long alg_k
;
2115 /* 2048 maxlen is a guess. How long a key does that permit? */
2116 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_KEY_EXCH_A
,
2117 SSL3_ST_SR_KEY_EXCH_B
, SSL3_MT_CLIENT_KEY_EXCHANGE
, 2048, &ok
);
2121 p
= (unsigned char *)s
->internal
->init_msg
;
2123 alg_k
= S3I(s
)->tmp
.new_cipher
->algorithm_mkey
;
2125 if (alg_k
& SSL_kRSA
) {
2126 if (ssl3_get_client_kex_rsa(s
, p
, n
) != 1)
2128 } else if (alg_k
& SSL_kDHE
) {
2129 if (ssl3_get_client_kex_dhe(s
, p
, n
) != 1)
2131 } else if (alg_k
& SSL_kECDHE
) {
2132 if (ssl3_get_client_kex_ecdhe(s
, p
, n
) != 1)
2134 } else if (alg_k
& SSL_kGOST
) {
2135 if (ssl3_get_client_kex_gost(s
, p
, n
) != 1)
2138 al
= SSL_AD_HANDSHAKE_FAILURE
;
2139 SSLerror(s
, SSL_R_UNKNOWN_CIPHER_TYPE
);
2146 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2152 ssl3_get_cert_verify(SSL
*s
)
2154 EVP_PKEY
*pkey
= NULL
;
2156 int al
, ok
, ret
= 0;
2160 const EVP_MD
*md
= NULL
;
2162 EVP_MD_CTX_init(&mctx
);
2164 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_CERT_VRFY_A
,
2165 SSL3_ST_SR_CERT_VRFY_B
, -1, SSL3_RT_MAX_PLAIN_LENGTH
, &ok
);
2169 if (s
->session
->peer
!= NULL
) {
2170 peer
= s
->session
->peer
;
2171 pkey
= X509_get_pubkey(peer
);
2172 type
= X509_certificate_type(peer
, pkey
);
2178 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
) {
2179 S3I(s
)->tmp
.reuse_message
= 1;
2181 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2182 SSLerror(s
, SSL_R_MISSING_VERIFY_MESSAGE
);
2190 SSLerror(s
, SSL_R_NO_CLIENT_CERT_RECEIVED
);
2191 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2195 if (!(type
& EVP_PKT_SIGN
)) {
2196 SSLerror(s
, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
2197 al
= SSL_AD_ILLEGAL_PARAMETER
;
2201 if (S3I(s
)->change_cipher_spec
) {
2202 SSLerror(s
, SSL_R_CCS_RECEIVED_EARLY
);
2203 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2207 /* we now have a signature that we need to verify */
2208 p
= (unsigned char *)s
->internal
->init_msg
;
2210 * Check for broken implementations of GOST ciphersuites.
2212 * If key is GOST and n is exactly 64, it is a bare
2213 * signature without length field.
2215 if (n
== 64 && (pkey
->type
== NID_id_GostR3410_94
||
2216 pkey
->type
== NID_id_GostR3410_2001
) ) {
2219 if (SSL_USE_SIGALGS(s
)) {
2220 int sigalg
= tls12_get_sigid(pkey
);
2221 /* Should never happen */
2223 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2224 al
= SSL_AD_INTERNAL_ERROR
;
2229 /* Check key type is consistent with signature */
2230 if (sigalg
!= (int)p
[1]) {
2231 SSLerror(s
, SSL_R_WRONG_SIGNATURE_TYPE
);
2232 al
= SSL_AD_DECODE_ERROR
;
2235 md
= tls12_get_hash(p
[0]);
2237 SSLerror(s
, SSL_R_UNKNOWN_DIGEST
);
2238 al
= SSL_AD_DECODE_ERROR
;
2251 j
= EVP_PKEY_size(pkey
);
2252 if ((i
> j
) || (n
> j
) || (n
<= 0)) {
2253 SSLerror(s
, SSL_R_WRONG_SIGNATURE_SIZE
);
2254 al
= SSL_AD_DECODE_ERROR
;
2258 if (SSL_USE_SIGALGS(s
)) {
2261 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2262 if (hdatalen
<= 0) {
2263 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2264 al
= SSL_AD_INTERNAL_ERROR
;
2267 if (!EVP_VerifyInit_ex(&mctx
, md
, NULL
) ||
2268 !EVP_VerifyUpdate(&mctx
, hdata
, hdatalen
)) {
2269 SSLerror(s
, ERR_R_EVP_LIB
);
2270 al
= SSL_AD_INTERNAL_ERROR
;
2274 if (EVP_VerifyFinal(&mctx
, p
, i
, pkey
) <= 0) {
2275 al
= SSL_AD_DECRYPT_ERROR
;
2276 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
2280 if (pkey
->type
== EVP_PKEY_RSA
) {
2281 i
= RSA_verify(NID_md5_sha1
, S3I(s
)->tmp
.cert_verify_md
,
2282 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, p
, i
,
2285 al
= SSL_AD_DECRYPT_ERROR
;
2286 SSLerror(s
, SSL_R_BAD_RSA_DECRYPT
);
2290 al
= SSL_AD_DECRYPT_ERROR
;
2291 SSLerror(s
, SSL_R_BAD_RSA_SIGNATURE
);
2295 if (pkey
->type
== EVP_PKEY_DSA
) {
2296 j
= DSA_verify(pkey
->save_type
,
2297 &(S3I(s
)->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
2298 SHA_DIGEST_LENGTH
, p
, i
, pkey
->pkey
.dsa
);
2301 al
= SSL_AD_DECRYPT_ERROR
;
2302 SSLerror(s
, SSL_R_BAD_DSA_SIGNATURE
);
2306 if (pkey
->type
== EVP_PKEY_EC
) {
2307 j
= ECDSA_verify(pkey
->save_type
,
2308 &(S3I(s
)->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
2309 SHA_DIGEST_LENGTH
, p
, i
, pkey
->pkey
.ec
);
2312 al
= SSL_AD_DECRYPT_ERROR
;
2313 SSLerror(s
, SSL_R_BAD_ECDSA_SIGNATURE
);
2317 #ifndef OPENSSL_NO_GOST
2318 if (pkey
->type
== NID_id_GostR3410_94
||
2319 pkey
->type
== NID_id_GostR3410_2001
) {
2322 unsigned char signature
[128];
2323 unsigned int siglen
= sizeof(signature
);
2327 hdatalen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &hdata
);
2328 if (hdatalen
<= 0) {
2329 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2330 al
= SSL_AD_INTERNAL_ERROR
;
2333 if (!EVP_PKEY_get_default_digest_nid(pkey
, &nid
) ||
2334 !(md
= EVP_get_digestbynid(nid
))) {
2335 SSLerror(s
, ERR_R_EVP_LIB
);
2336 al
= SSL_AD_INTERNAL_ERROR
;
2339 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2341 SSLerror(s
, ERR_R_EVP_LIB
);
2342 al
= SSL_AD_INTERNAL_ERROR
;
2345 if (!EVP_DigestInit_ex(&mctx
, md
, NULL
) ||
2346 !EVP_DigestUpdate(&mctx
, hdata
, hdatalen
) ||
2347 !EVP_DigestFinal(&mctx
, signature
, &siglen
) ||
2348 (EVP_PKEY_verify_init(pctx
) <= 0) ||
2349 (EVP_PKEY_CTX_set_signature_md(pctx
, md
) <= 0) ||
2350 (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_VERIFY
,
2351 EVP_PKEY_CTRL_GOST_SIG_FORMAT
,
2352 GOST_SIG_FORMAT_RS_LE
,
2354 SSLerror(s
, ERR_R_EVP_LIB
);
2355 al
= SSL_AD_INTERNAL_ERROR
;
2356 EVP_PKEY_CTX_free(pctx
);
2360 if (EVP_PKEY_verify(pctx
, p
, i
, signature
, siglen
) <= 0) {
2361 al
= SSL_AD_DECRYPT_ERROR
;
2362 SSLerror(s
, SSL_R_BAD_SIGNATURE
);
2363 EVP_PKEY_CTX_free(pctx
);
2367 EVP_PKEY_CTX_free(pctx
);
2371 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2372 al
= SSL_AD_UNSUPPORTED_CERTIFICATE
;
2380 al
= SSL_AD_DECODE_ERROR
;
2381 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
2383 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2386 if (S3I(s
)->handshake_buffer
) {
2387 BIO_free(S3I(s
)->handshake_buffer
);
2388 S3I(s
)->handshake_buffer
= NULL
;
2389 s
->s3
->flags
&= ~TLS1_FLAGS_KEEP_HANDSHAKE
;
2391 EVP_MD_CTX_cleanup(&mctx
);
2392 EVP_PKEY_free(pkey
);
2397 ssl3_get_client_certificate(SSL
*s
)
2399 CBS cbs
, client_certs
;
2400 int i
, ok
, al
, ret
= -1;
2403 const unsigned char *q
;
2404 STACK_OF(X509
) *sk
= NULL
;
2406 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_CERT_A
, SSL3_ST_SR_CERT_B
,
2407 -1, s
->internal
->max_cert_list
, &ok
);
2412 if (S3I(s
)->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
2413 if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2414 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2415 SSLerror(s
, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2416 al
= SSL_AD_HANDSHAKE_FAILURE
;
2420 * If tls asked for a client cert,
2421 * the client must return a 0 list.
2423 if (S3I(s
)->tmp
.cert_request
) {
2424 SSLerror(s
, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
2426 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2429 S3I(s
)->tmp
.reuse_message
= 1;
2433 if (S3I(s
)->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
2434 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2435 SSLerror(s
, SSL_R_WRONG_MESSAGE_TYPE
);
2442 CBS_init(&cbs
, s
->internal
->init_msg
, n
);
2444 if ((sk
= sk_X509_new_null()) == NULL
) {
2445 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2449 if (!CBS_get_u24_length_prefixed(&cbs
, &client_certs
) ||
2453 while (CBS_len(&client_certs
) > 0) {
2456 if (!CBS_get_u24_length_prefixed(&client_certs
, &cert
)) {
2457 al
= SSL_AD_DECODE_ERROR
;
2458 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
2462 q
= CBS_data(&cert
);
2463 x
= d2i_X509(NULL
, &q
, CBS_len(&cert
));
2465 SSLerror(s
, ERR_R_ASN1_LIB
);
2468 if (q
!= CBS_data(&cert
) + CBS_len(&cert
)) {
2469 al
= SSL_AD_DECODE_ERROR
;
2470 SSLerror(s
, SSL_R_CERT_LENGTH_MISMATCH
);
2473 if (!sk_X509_push(sk
, x
)) {
2474 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2480 if (sk_X509_num(sk
) <= 0) {
2482 * TLS does not mind 0 certs returned.
2483 * Fail for TLS only if we required a certificate.
2485 if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2486 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2487 SSLerror(s
, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2488 al
= SSL_AD_HANDSHAKE_FAILURE
;
2491 /* No client certificate so digest cached records */
2492 if (S3I(s
)->handshake_buffer
&& !tls1_digest_cached_records(s
)) {
2493 al
= SSL_AD_INTERNAL_ERROR
;
2497 i
= ssl_verify_cert_chain(s
, sk
);
2499 al
= ssl_verify_alarm_type(s
->verify_result
);
2500 SSLerror(s
, SSL_R_NO_CERTIFICATE_RETURNED
);
2505 X509_free(s
->session
->peer
);
2506 s
->session
->peer
= sk_X509_shift(sk
);
2507 s
->session
->verify_result
= s
->verify_result
;
2510 * With the current implementation, sess_cert will always be NULL
2511 * when we arrive here
2513 if (SSI(s
)->sess_cert
== NULL
) {
2514 SSI(s
)->sess_cert
= ssl_sess_cert_new();
2515 if (SSI(s
)->sess_cert
== NULL
) {
2516 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2520 sk_X509_pop_free(SSI(s
)->sess_cert
->cert_chain
, X509_free
);
2521 SSI(s
)->sess_cert
->cert_chain
= sk
;
2524 * Inconsistency alert: cert_chain does *not* include the
2525 * peer's own certificate, while we do include it in s3_clnt.c
2533 al
= SSL_AD_DECODE_ERROR
;
2534 SSLerror(s
, SSL_R_BAD_PACKET_LENGTH
);
2536 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2540 sk_X509_pop_free(sk
, X509_free
);
2546 ssl3_send_server_certificate(SSL
*s
)
2548 CBB cbb
, server_cert
;
2552 * Server Certificate - RFC 5246, section 7.4.2.
2555 memset(&cbb
, 0, sizeof(cbb
));
2557 if (s
->internal
->state
== SSL3_ST_SW_CERT_A
) {
2558 if ((x
= ssl_get_server_send_cert(s
)) == NULL
) {
2559 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
2563 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &server_cert
,
2564 SSL3_MT_CERTIFICATE
))
2566 if (!ssl3_output_cert_chain(s
, &server_cert
, x
))
2568 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2571 s
->internal
->state
= SSL3_ST_SW_CERT_B
;
2574 /* SSL3_ST_SW_CERT_B */
2575 return (ssl3_handshake_write(s
));
2583 /* send a new session ticket (not necessarily for a new session) */
2585 ssl3_send_newsession_ticket(SSL
*s
)
2587 unsigned char *d
, *p
, *macstart
;
2588 unsigned char *senc
= NULL
;
2589 const unsigned char *const_p
;
2590 int len
, slen_full
, slen
;
2595 SSL_CTX
*tctx
= s
->initial_ctx
;
2596 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2597 unsigned char key_name
[16];
2599 if (s
->internal
->state
== SSL3_ST_SW_SESSION_TICKET_A
) {
2600 /* get session encoding length */
2601 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
2603 * Some length values are 16 bits, so forget it if session is
2606 if (slen_full
> 0xFF00)
2608 senc
= malloc(slen_full
);
2612 i2d_SSL_SESSION(s
->session
, &p
);
2615 * Create a fresh copy (not shared with other threads) to
2619 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
2623 /* ID is irrelevant for the ticket */
2624 sess
->session_id_length
= 0;
2626 slen
= i2d_SSL_SESSION(sess
, NULL
);
2627 if (slen
> slen_full
) {
2628 /* shouldn't ever happen */
2632 i2d_SSL_SESSION(sess
, &p
);
2633 SSL_SESSION_free(sess
);
2636 * Grow buffer if need be: the length calculation is as
2637 * follows 1 (size of message name) + 3 (message length
2638 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2639 * 16 (key name) + max_iv_len (iv length) +
2640 * session_length + max_enc_block_size (max encrypted session
2641 * length) + max_md_size (HMAC).
2643 if (!BUF_MEM_grow(s
->internal
->init_buf
, ssl3_handshake_msg_hdr_len(s
) +
2644 22 + EVP_MAX_IV_LENGTH
+ EVP_MAX_BLOCK_LENGTH
+
2645 EVP_MAX_MD_SIZE
+ slen
))
2648 d
= p
= ssl3_handshake_msg_start(s
, SSL3_MT_NEWSESSION_TICKET
);
2650 EVP_CIPHER_CTX_init(&ctx
);
2651 HMAC_CTX_init(&hctx
);
2654 * Initialize HMAC and cipher contexts. If callback present
2655 * it does all the work otherwise use generated values
2658 if (tctx
->internal
->tlsext_ticket_key_cb
) {
2659 if (tctx
->internal
->tlsext_ticket_key_cb(s
,
2660 key_name
, iv
, &ctx
, &hctx
, 1) < 0) {
2661 EVP_CIPHER_CTX_cleanup(&ctx
);
2665 arc4random_buf(iv
, 16);
2666 EVP_EncryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
2667 tctx
->internal
->tlsext_tick_aes_key
, iv
);
2668 HMAC_Init_ex(&hctx
, tctx
->internal
->tlsext_tick_hmac_key
,
2669 16, tlsext_tick_md(), NULL
);
2670 memcpy(key_name
, tctx
->internal
->tlsext_tick_key_name
, 16);
2674 * Ticket lifetime hint (advisory only):
2675 * We leave this unspecified for resumed session
2676 * (for simplicity), and guess that tickets for new
2677 * sessions will live as long as their sessions.
2679 l2n(s
->internal
->hit
? 0 : s
->session
->timeout
, p
);
2681 /* Skip ticket length for now */
2683 /* Output key name */
2685 memcpy(p
, key_name
, 16);
2688 memcpy(p
, iv
, EVP_CIPHER_CTX_iv_length(&ctx
));
2689 p
+= EVP_CIPHER_CTX_iv_length(&ctx
);
2690 /* Encrypt session data */
2691 EVP_EncryptUpdate(&ctx
, p
, &len
, senc
, slen
);
2693 EVP_EncryptFinal_ex(&ctx
, p
, &len
);
2695 EVP_CIPHER_CTX_cleanup(&ctx
);
2697 HMAC_Update(&hctx
, macstart
, p
- macstart
);
2698 HMAC_Final(&hctx
, p
, &hlen
);
2699 HMAC_CTX_cleanup(&hctx
);
2702 /* Now write out lengths: p points to end of data written */
2706 /* Skip ticket lifetime hint. */
2708 s2n(len
- 6, p
); /* Message length */
2710 ssl3_handshake_msg_finish(s
, len
);
2712 s
->internal
->state
= SSL3_ST_SW_SESSION_TICKET_B
;
2714 explicit_bzero(senc
, slen_full
);
2718 /* SSL3_ST_SW_SESSION_TICKET_B */
2719 return (ssl3_handshake_write(s
));
2723 explicit_bzero(senc
, slen_full
);
2730 ssl3_send_cert_status(SSL
*s
)
2732 CBB cbb
, certstatus
, ocspresp
;
2734 memset(&cbb
, 0, sizeof(cbb
));
2736 if (s
->internal
->state
== SSL3_ST_SW_CERT_STATUS_A
) {
2737 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &certstatus
,
2738 SSL3_MT_CERTIFICATE_STATUS
))
2740 if (!CBB_add_u8(&certstatus
, s
->tlsext_status_type
))
2742 if (!CBB_add_u24_length_prefixed(&certstatus
, &ocspresp
))
2744 if (!CBB_add_bytes(&ocspresp
, s
->internal
->tlsext_ocsp_resp
,
2745 s
->internal
->tlsext_ocsp_resplen
))
2747 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
2750 s
->internal
->state
= SSL3_ST_SW_CERT_STATUS_B
;
2753 /* SSL3_ST_SW_CERT_STATUS_B */
2754 return (ssl3_handshake_write(s
));
2763 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
2764 * It sets the next_proto member in s if found
2767 ssl3_get_next_proto(SSL
*s
)
2769 CBS cbs
, proto
, padding
;
2775 * Clients cannot send a NextProtocol message if we didn't see the
2776 * extension in their ClientHello
2778 if (!S3I(s
)->next_proto_neg_seen
) {
2779 SSLerror(s
, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION
);
2783 /* 514 maxlen is enough for the payload format below */
2784 n
= s
->method
->internal
->ssl_get_message(s
, SSL3_ST_SR_NEXT_PROTO_A
,
2785 SSL3_ST_SR_NEXT_PROTO_B
, SSL3_MT_NEXT_PROTO
, 514, &ok
);
2790 * s->internal->state doesn't reflect whether ChangeCipherSpec has been received
2791 * in this handshake, but S3I(s)->change_cipher_spec does (will be reset
2792 * by ssl3_get_finished).
2794 if (!S3I(s
)->change_cipher_spec
) {
2795 SSLerror(s
, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS
);
2801 /* The body must be > 1 bytes long */
2803 CBS_init(&cbs
, s
->internal
->init_msg
, s
->internal
->init_num
);
2806 * The payload looks like:
2808 * uint8 proto[proto_len];
2809 * uint8 padding_len;
2810 * uint8 padding[padding_len];
2812 if (!CBS_get_u8_length_prefixed(&cbs
, &proto
) ||
2813 !CBS_get_u8_length_prefixed(&cbs
, &padding
) ||
2818 * XXX We should not NULL it, but this matches old behavior of not
2819 * freeing before malloc.
2821 s
->internal
->next_proto_negotiated
= NULL
;
2822 s
->internal
->next_proto_negotiated_len
= 0;
2824 if (!CBS_stow(&proto
, &s
->internal
->next_proto_negotiated
, &len
)) {
2825 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
2828 s
->internal
->next_proto_negotiated_len
= (uint8_t)len
;