1 /* ssl/statem/statem_srvr.c -*- mode:C; c-file-style: "eay" -*- */
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"
154 #include "statem_locl.h"
155 #include "internal/constant_time_locl.h"
156 #include <openssl/buffer.h>
157 #include <openssl/rand.h>
158 #include <openssl/objects.h>
159 #include <openssl/evp.h>
160 #include <openssl/hmac.h>
161 #include <openssl/x509.h>
162 #ifndef OPENSSL_NO_DH
163 # include <openssl/dh.h>
165 #include <openssl/bn.h>
166 #include <openssl/md5.h>
168 static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
169 PACKET *cipher_suites,
170 STACK_OF(SSL_CIPHER) **skp,
171 int sslv2format, int *al);
174 * server_read_transition() encapsulates the logic for the allowed handshake
175 * state transitions when the server is reading messages from the client. The
176 * message type that the client has sent is provided in |mt|. The current state
177 * is in |s->statem.hand_state|.
179 * Valid return values are:
180 * 1: Success (transition allowed)
181 * 0: Error (transition not allowed)
183 int ossl_statem_server_read_transition(SSL *s, int mt)
185 OSSL_STATEM *st = &s->statem;
187 switch(st->hand_state) {
189 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
190 if (mt == SSL3_MT_CLIENT_HELLO) {
191 st->hand_state = TLS_ST_SR_CLNT_HELLO;
196 case TLS_ST_SW_SRVR_DONE:
198 * If we get a CKE message after a ServerDone then either
199 * 1) We didn't request a Certificate
201 * 2) If we did request one then
202 * a) We allow no Certificate to be returned
204 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
205 * list if we requested a certificate)
207 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE
208 && (!s->s3->tmp.cert_request
209 || (!((s->verify_mode & SSL_VERIFY_PEER) &&
210 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
211 && (s->version == SSL3_VERSION)))) {
212 st->hand_state = TLS_ST_SR_KEY_EXCH;
214 } else if (s->s3->tmp.cert_request) {
215 if (mt == SSL3_MT_CERTIFICATE) {
216 st->hand_state = TLS_ST_SR_CERT;
223 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
224 st->hand_state = TLS_ST_SR_KEY_EXCH;
229 case TLS_ST_SR_KEY_EXCH:
231 * We should only process a CertificateVerify message if we have
232 * received a Certificate from the client. If so then |s->session->peer|
233 * will be non NULL. In some instances a CertificateVerify message is
234 * not required even if the peer has sent a Certificate (e.g. such as in
235 * the case of static DH). In that case |st->no_cert_verify| should be
238 if (s->session->peer == NULL || st->no_cert_verify) {
239 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
241 * For the ECDH ciphersuites when the client sends its ECDH
242 * pub key in a certificate, the CertificateVerify message is
243 * not sent. Also for GOST ciphersuites when the client uses
244 * its key from the certificate for key exchange.
246 st->hand_state = TLS_ST_SR_CHANGE;
250 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
251 st->hand_state = TLS_ST_SR_CERT_VRFY;
257 case TLS_ST_SR_CERT_VRFY:
258 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
259 st->hand_state = TLS_ST_SR_CHANGE;
264 case TLS_ST_SR_CHANGE:
265 #ifndef OPENSSL_NO_NEXTPROTONEG
266 if (s->s3->next_proto_neg_seen) {
267 if (mt == SSL3_MT_NEXT_PROTO) {
268 st->hand_state = TLS_ST_SR_NEXT_PROTO;
273 if (mt == SSL3_MT_FINISHED) {
274 st->hand_state = TLS_ST_SR_FINISHED;
277 #ifndef OPENSSL_NO_NEXTPROTONEG
282 #ifndef OPENSSL_NO_NEXTPROTONEG
283 case TLS_ST_SR_NEXT_PROTO:
284 if (mt == SSL3_MT_FINISHED) {
285 st->hand_state = TLS_ST_SR_FINISHED;
291 case TLS_ST_SW_FINISHED:
292 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
293 st->hand_state = TLS_ST_SR_CHANGE;
302 /* No valid transition found */
307 * Should we send a ServerKeyExchange message?
309 * Valid return values are:
313 static int send_server_key_exchange(SSL *s)
315 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
318 * only send a ServerKeyExchange if DH, fortezza or RSA but we have a
319 * sign only certificate PSK: may send PSK identity hints For
320 * ECC ciphersuites, we send a serverKeyExchange message only if
321 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
322 * the server certificate contains the server's public key for
325 if ( (alg_k & SSL_kDHE)
326 || (alg_k & SSL_kECDHE)
327 || ((alg_k & SSL_kRSA)
328 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
329 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
330 && EVP_PKEY_size(s->cert->pkeys
331 [SSL_PKEY_RSA_ENC].privatekey) *
332 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
337 * PSK: send ServerKeyExchange if PSK identity hint if
340 #ifndef OPENSSL_NO_PSK
341 /* Only send SKE if we have identity hint for plain PSK */
342 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
343 && s->cert->psk_identity_hint)
344 /* For other PSK always send SKE */
345 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
347 #ifndef OPENSSL_NO_SRP
348 /* SRP: send ServerKeyExchange */
349 || (alg_k & SSL_kSRP)
359 * Should we send a CertificateRequest message?
361 * Valid return values are:
365 static int send_certificate_request(SSL *s)
368 /* don't request cert unless asked for it: */
369 s->verify_mode & SSL_VERIFY_PEER
371 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
372 * during re-negotiation:
374 && ((s->session->peer == NULL) ||
375 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
377 * never request cert in anonymous ciphersuites (see
378 * section "Certificate request" in SSL 3 drafts and in
381 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
383 * ... except when the application insists on
384 * verification (against the specs, but s3_clnt.c accepts
387 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
388 /* don't request certificate for SRP auth */
389 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
391 * With normal PSK Certificates and Certificate Requests
394 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
402 * server_write_transition() works out what handshake state to move to next
403 * when the server is writing messages to be sent to the client.
405 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
407 OSSL_STATEM *st = &s->statem;
409 switch(st->hand_state) {
411 /* Just go straight to trying to read from the client */;
412 return WRITE_TRAN_FINISHED;
415 /* We must be trying to renegotiate */
416 st->hand_state = TLS_ST_SW_HELLO_REQ;
417 return WRITE_TRAN_CONTINUE;
419 case TLS_ST_SW_HELLO_REQ:
420 st->hand_state = TLS_ST_OK;
421 ossl_statem_set_in_init(s, 0);
422 return WRITE_TRAN_CONTINUE;
424 case TLS_ST_SR_CLNT_HELLO:
425 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
426 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
427 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
429 st->hand_state = TLS_ST_SW_SRVR_HELLO;
430 return WRITE_TRAN_CONTINUE;
432 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
433 return WRITE_TRAN_FINISHED;
435 case TLS_ST_SW_SRVR_HELLO:
437 if (s->tlsext_ticket_expected)
438 st->hand_state = TLS_ST_SW_SESSION_TICKET;
440 st->hand_state = TLS_ST_SW_CHANGE;
442 /* Check if it is anon DH or anon ECDH, */
443 /* normal PSK or SRP */
444 if (!(s->s3->tmp.new_cipher->algorithm_auth &
445 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
446 st->hand_state = TLS_ST_SW_CERT;
447 } else if (send_server_key_exchange(s)) {
448 st->hand_state = TLS_ST_SW_KEY_EXCH;
449 } else if (send_certificate_request(s)) {
450 st->hand_state = TLS_ST_SW_CERT_REQ;
452 st->hand_state = TLS_ST_SW_SRVR_DONE;
455 return WRITE_TRAN_CONTINUE;
458 if (s->tlsext_status_expected) {
459 st->hand_state = TLS_ST_SW_CERT_STATUS;
460 return WRITE_TRAN_CONTINUE;
464 case TLS_ST_SW_CERT_STATUS:
465 if (send_server_key_exchange(s)) {
466 st->hand_state = TLS_ST_SW_KEY_EXCH;
467 return WRITE_TRAN_CONTINUE;
471 case TLS_ST_SW_KEY_EXCH:
472 if (send_certificate_request(s)) {
473 st->hand_state = TLS_ST_SW_CERT_REQ;
474 return WRITE_TRAN_CONTINUE;
478 case TLS_ST_SW_CERT_REQ:
479 st->hand_state = TLS_ST_SW_SRVR_DONE;
480 return WRITE_TRAN_CONTINUE;
482 case TLS_ST_SW_SRVR_DONE:
483 return WRITE_TRAN_FINISHED;
485 case TLS_ST_SR_FINISHED:
487 st->hand_state = TLS_ST_OK;
488 ossl_statem_set_in_init(s, 0);
489 return WRITE_TRAN_CONTINUE;
490 } else if (s->tlsext_ticket_expected) {
491 st->hand_state = TLS_ST_SW_SESSION_TICKET;
493 st->hand_state = TLS_ST_SW_CHANGE;
495 return WRITE_TRAN_CONTINUE;
497 case TLS_ST_SW_SESSION_TICKET:
498 st->hand_state = TLS_ST_SW_CHANGE;
499 return WRITE_TRAN_CONTINUE;
501 case TLS_ST_SW_CHANGE:
502 st->hand_state = TLS_ST_SW_FINISHED;
503 return WRITE_TRAN_CONTINUE;
505 case TLS_ST_SW_FINISHED:
507 return WRITE_TRAN_FINISHED;
509 st->hand_state = TLS_ST_OK;
510 ossl_statem_set_in_init(s, 0);
511 return WRITE_TRAN_CONTINUE;
514 /* Shouldn't happen */
515 return WRITE_TRAN_ERROR;
520 * Perform any pre work that needs to be done prior to sending a message from
521 * the server to the client.
523 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
525 OSSL_STATEM *st = &s->statem;
527 switch(st->hand_state) {
528 case TLS_ST_SW_HELLO_REQ:
531 dtls1_clear_record_buffer(s);
534 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
536 if (SSL_IS_DTLS(s)) {
537 dtls1_clear_record_buffer(s);
538 /* We don't buffer this message so don't use the timer */
543 case TLS_ST_SW_SRVR_HELLO:
544 if (SSL_IS_DTLS(s)) {
546 * Messages we write from now on should be bufferred and
547 * retransmitted if necessary, so we need to use the timer now
553 case TLS_ST_SW_SRVR_DONE:
554 #ifndef OPENSSL_NO_SCTP
555 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
556 return dtls_wait_for_dry(s);
558 return WORK_FINISHED_CONTINUE;
560 case TLS_ST_SW_SESSION_TICKET:
561 if (SSL_IS_DTLS(s)) {
563 * We're into the last flight. We don't retransmit the last flight
564 * unless we need to, so we don't use the timer
570 case TLS_ST_SW_CHANGE:
571 s->session->cipher = s->s3->tmp.new_cipher;
572 if (!s->method->ssl3_enc->setup_key_block(s)) {
573 ossl_statem_set_error(s);
576 if (SSL_IS_DTLS(s)) {
578 * We're into the last flight. We don't retransmit the last flight
579 * unless we need to, so we don't use the timer. This might have
580 * already been set to 0 if we sent a NewSessionTicket message,
581 * but we'll set it again here in case we didn't.
585 return WORK_FINISHED_CONTINUE;
588 return tls_finish_handshake(s, wst);
591 /* No pre work to be done */
595 return WORK_FINISHED_CONTINUE;
599 * Perform any work that needs to be done after sending a message from the
600 * server to the client.
602 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
604 OSSL_STATEM *st = &s->statem;
608 switch(st->hand_state) {
609 case TLS_ST_SW_HELLO_REQ:
610 if (statem_flush(s) != 1)
612 ssl3_init_finished_mac(s);
615 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
616 if (statem_flush(s) != 1)
618 /* HelloVerifyRequest resets Finished MAC */
619 if (s->version != DTLS1_BAD_VER)
620 ssl3_init_finished_mac(s);
622 * The next message should be another ClientHello which we need to
623 * treat like it was the first packet
628 case TLS_ST_SW_SRVR_HELLO:
629 #ifndef OPENSSL_NO_SCTP
630 if (SSL_IS_DTLS(s) && s->hit) {
631 unsigned char sctpauthkey[64];
632 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
635 * Add new shared key for SCTP-Auth, will be ignored if no
638 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
639 sizeof(DTLS1_SCTP_AUTH_LABEL));
641 if (SSL_export_keying_material(s, sctpauthkey,
642 sizeof(sctpauthkey), labelbuffer,
643 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
644 ossl_statem_set_error(s);
648 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
649 sizeof(sctpauthkey), sctpauthkey);
654 case TLS_ST_SW_CHANGE:
655 #ifndef OPENSSL_NO_SCTP
656 if (SSL_IS_DTLS(s) && !s->hit) {
658 * Change to new shared key of SCTP-Auth, will be ignored if
661 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
665 if (!s->method->ssl3_enc->change_cipher_state(s,
666 SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
667 ossl_statem_set_error(s);
672 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
675 case TLS_ST_SW_SRVR_DONE:
676 if (statem_flush(s) != 1)
680 case TLS_ST_SW_FINISHED:
681 if (statem_flush(s) != 1)
683 #ifndef OPENSSL_NO_SCTP
684 if (SSL_IS_DTLS(s) && s->hit) {
686 * Change to new shared key of SCTP-Auth, will be ignored if
689 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
696 /* No post work to be done */
700 return WORK_FINISHED_CONTINUE;
704 * Construct a message to be sent from the server to the client.
706 * Valid return values are:
710 int ossl_statem_server_construct_message(SSL *s)
712 OSSL_STATEM *st = &s->statem;
714 switch(st->hand_state) {
715 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
716 return dtls_construct_hello_verify_request(s);
718 case TLS_ST_SW_HELLO_REQ:
719 return tls_construct_hello_request(s);
721 case TLS_ST_SW_SRVR_HELLO:
722 return tls_construct_server_hello(s);
725 return tls_construct_server_certificate(s);
727 case TLS_ST_SW_KEY_EXCH:
728 return tls_construct_server_key_exchange(s);
730 case TLS_ST_SW_CERT_REQ:
731 return tls_construct_certificate_request(s);
733 case TLS_ST_SW_SRVR_DONE:
734 return tls_construct_server_done(s);
736 case TLS_ST_SW_SESSION_TICKET:
737 return tls_construct_new_session_ticket(s);
739 case TLS_ST_SW_CERT_STATUS:
740 return tls_construct_cert_status(s);
742 case TLS_ST_SW_CHANGE:
744 return dtls_construct_change_cipher_spec(s);
746 return tls_construct_change_cipher_spec(s);
748 case TLS_ST_SW_FINISHED:
749 return tls_construct_finished(s,
751 ssl3_enc->server_finished_label,
753 ssl3_enc->server_finished_label_len);
756 /* Shouldn't happen */
763 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
764 #define NEXT_PROTO_MAX_LENGTH 514
767 * Returns the maximum allowed length for the current message that we are
768 * reading. Excludes the message header.
770 unsigned long ossl_statem_server_max_message_size(SSL *s)
772 OSSL_STATEM *st = &s->statem;
774 switch(st->hand_state) {
775 case TLS_ST_SR_CLNT_HELLO:
776 return SSL3_RT_MAX_PLAIN_LENGTH;
779 return s->max_cert_list;
781 case TLS_ST_SR_KEY_EXCH:
782 return CLIENT_KEY_EXCH_MAX_LENGTH;
784 case TLS_ST_SR_CERT_VRFY:
785 return SSL3_RT_MAX_PLAIN_LENGTH;
787 #ifndef OPENSSL_NO_NEXTPROTONEG
788 case TLS_ST_SR_NEXT_PROTO:
789 return NEXT_PROTO_MAX_LENGTH;
792 case TLS_ST_SR_CHANGE:
793 return CCS_MAX_LENGTH;
795 case TLS_ST_SR_FINISHED:
796 return FINISHED_MAX_LENGTH;
799 /* Shouldn't happen */
807 * Process a message that the server has received from the client.
809 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
811 OSSL_STATEM *st = &s->statem;
813 switch(st->hand_state) {
814 case TLS_ST_SR_CLNT_HELLO:
815 return tls_process_client_hello(s, pkt);
818 return tls_process_client_certificate(s, pkt);
820 case TLS_ST_SR_KEY_EXCH:
821 return tls_process_client_key_exchange(s, pkt);
823 case TLS_ST_SR_CERT_VRFY:
824 return tls_process_cert_verify(s, pkt);
826 #ifndef OPENSSL_NO_NEXTPROTONEG
827 case TLS_ST_SR_NEXT_PROTO:
828 return tls_process_next_proto(s, pkt);
831 case TLS_ST_SR_CHANGE:
832 return tls_process_change_cipher_spec(s, pkt);
834 case TLS_ST_SR_FINISHED:
835 return tls_process_finished(s, pkt);
838 /* Shouldn't happen */
842 return MSG_PROCESS_ERROR;
846 * Perform any further processing required following the receipt of a message
849 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
851 OSSL_STATEM *st = &s->statem;
853 switch(st->hand_state) {
854 case TLS_ST_SR_CLNT_HELLO:
855 return tls_post_process_client_hello(s, wst);
857 case TLS_ST_SR_KEY_EXCH:
858 return tls_post_process_client_key_exchange(s, wst);
860 case TLS_ST_SR_CERT_VRFY:
861 #ifndef OPENSSL_NO_SCTP
862 if ( /* Is this SCTP? */
863 BIO_dgram_is_sctp(SSL_get_wbio(s))
864 /* Are we renegotiating? */
866 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
867 s->s3->in_read_app_data = 2;
868 s->rwstate = SSL_READING;
869 BIO_clear_retry_flags(SSL_get_rbio(s));
870 BIO_set_retry_read(SSL_get_rbio(s));
871 ossl_statem_set_sctp_read_sock(s, 1);
874 ossl_statem_set_sctp_read_sock(s, 0);
877 return WORK_FINISHED_CONTINUE;
883 /* Shouldn't happen */
887 #ifndef OPENSSL_NO_SRP
888 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
890 int ret = SSL_ERROR_NONE;
892 *al = SSL_AD_UNRECOGNIZED_NAME;
894 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
895 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
896 if (s->srp_ctx.login == NULL) {
898 * RFC 5054 says SHOULD reject, we do so if There is no srp
902 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
904 ret = SSL_srp_server_param_with_username(s, al);
911 int tls_construct_hello_request(SSL *s)
913 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
914 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
915 ossl_statem_set_error(s);
922 unsigned int dtls_raw_hello_verify_request(unsigned char *buf,
923 unsigned char *cookie,
924 unsigned char cookie_len)
926 unsigned int msg_len;
930 /* Always use DTLS 1.0 version: see RFC 6347 */
931 *(p++) = DTLS1_VERSION >> 8;
932 *(p++) = DTLS1_VERSION & 0xFF;
934 *(p++) = (unsigned char)cookie_len;
935 memcpy(p, cookie, cookie_len);
942 int dtls_construct_hello_verify_request(SSL *s)
947 buf = (unsigned char *)s->init_buf->data;
949 if (s->ctx->app_gen_cookie_cb == NULL ||
950 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
951 &(s->d1->cookie_len)) == 0 ||
952 s->d1->cookie_len > 255) {
953 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
954 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
955 ossl_statem_set_error(s);
959 len = dtls_raw_hello_verify_request(&buf[DTLS1_HM_HEADER_LENGTH],
960 s->d1->cookie, s->d1->cookie_len);
962 dtls1_set_message_header(s, buf, DTLS1_MT_HELLO_VERIFY_REQUEST, len, 0,
964 len += DTLS1_HM_HEADER_LENGTH;
966 /* number of bytes to write */
973 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
975 int i, al = SSL_AD_INTERNAL_ERROR;
976 unsigned int j, complen = 0;
979 #ifndef OPENSSL_NO_COMP
980 SSL_COMP *comp = NULL;
982 STACK_OF(SSL_CIPHER) *ciphers = NULL;
984 /* |cookie| will only be initialized for DTLS. */
985 PACKET session_id, cipher_suites, compression, extensions, cookie;
988 is_v2_record = RECORD_LAYER_is_sslv2_record(&s->rlayer);
990 PACKET_null_init(&cookie);
991 /* First lets get s->client_version set correctly */
993 unsigned int version;
996 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
997 * header is sent directly on the wire, not wrapped as a TLS
998 * record. Our record layer just processes the message length and passes
999 * the rest right through. Its format is:
1001 * 0-1 msg_length - decoded by the record layer
1002 * 2 msg_type - s->init_msg points here
1004 * 5-6 cipher_spec_length
1005 * 7-8 session_id_length
1006 * 9-10 challenge_length
1010 if (!PACKET_get_1(pkt, &mt)
1011 || mt != SSL2_MT_CLIENT_HELLO) {
1013 * Should never happen. We should have tested this in the record
1014 * layer in order to have determined that this is a SSLv2 record
1015 * in the first place
1017 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1021 if (!PACKET_get_net_2(pkt, &version)) {
1022 /* No protocol version supplied! */
1023 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1026 if (version == 0x0002) {
1027 /* This is real SSLv2. We don't support it. */
1028 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1030 } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {
1032 s->client_version = version;
1034 /* No idea what protocol this is */
1035 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1040 * use version from inside client hello, not from record header (may
1041 * differ: see RFC 2246, Appendix E, second paragraph)
1043 if(!PACKET_get_net_2(pkt, (unsigned int *)&s->client_version)) {
1044 al = SSL_AD_DECODE_ERROR;
1045 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1050 /* Do SSL/TLS version negotiation if applicable */
1051 if (!SSL_IS_DTLS(s)) {
1052 if (s->version != TLS_ANY_VERSION) {
1053 if (s->client_version >= s->version) {
1056 } else if (s->client_version >= SSL3_VERSION) {
1057 switch(s->client_version) {
1059 case TLS1_2_VERSION:
1060 if(!(s->options & SSL_OP_NO_TLSv1_2)) {
1061 s->version = TLS1_2_VERSION;
1062 s->method = TLSv1_2_server_method();
1066 /* Deliberately fall through */
1067 case TLS1_1_VERSION:
1068 if(!(s->options & SSL_OP_NO_TLSv1_1)) {
1069 s->version = TLS1_1_VERSION;
1070 s->method = TLSv1_1_server_method();
1074 /* Deliberately fall through */
1076 if(!(s->options & SSL_OP_NO_TLSv1)) {
1077 s->version = TLS1_VERSION;
1078 s->method = TLSv1_server_method();
1082 /* Deliberately fall through */
1084 #ifndef OPENSSL_NO_SSL3
1085 if(!(s->options & SSL_OP_NO_SSLv3)) {
1086 s->version = SSL3_VERSION;
1087 s->method = SSLv3_server_method();
1096 } else if (s->client_version <= s->version
1097 || s->method->version == DTLS_ANY_VERSION) {
1099 * For DTLS we just check versions are potentially compatible. Version
1100 * negotiation comes later.
1106 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1107 if ((!s->enc_write_ctx && !s->write_hash)) {
1109 * similar to ssl3_get_record, send alert using remote version
1112 s->version = s->client_version;
1114 al = SSL_AD_PROTOCOL_VERSION;
1118 /* Parse the message and load client random. */
1121 * Handle an SSLv2 backwards compatible ClientHello
1122 * Note, this is only for SSLv3+ using the backward compatible format.
1123 * Real SSLv2 is not supported, and is rejected above.
1125 unsigned int cipher_len, session_id_len, challenge_len;
1128 if (!PACKET_get_net_2(pkt, &cipher_len)
1129 || !PACKET_get_net_2(pkt, &session_id_len)
1130 || !PACKET_get_net_2(pkt, &challenge_len)) {
1131 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1132 SSL_R_RECORD_LENGTH_MISMATCH);
1133 al = SSL_AD_DECODE_ERROR;
1137 if (!PACKET_get_sub_packet(pkt, &cipher_suites, cipher_len)
1138 || !PACKET_get_sub_packet(pkt, &session_id, session_id_len)
1139 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1140 /* No extensions. */
1141 || PACKET_remaining(pkt) != 0) {
1142 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1143 SSL_R_RECORD_LENGTH_MISMATCH);
1144 al = SSL_AD_DECODE_ERROR;
1148 /* Load the client random */
1149 challenge_len = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE :
1151 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1152 if (!PACKET_copy_bytes(&challenge,
1153 s->s3->client_random + SSL3_RANDOM_SIZE -
1154 challenge_len, challenge_len)) {
1155 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1156 al = SSL_AD_INTERNAL_ERROR;
1160 PACKET_null_init(&compression);
1161 PACKET_null_init(&extensions);
1163 /* Regular ClientHello. */
1164 if (!PACKET_copy_bytes(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
1165 || !PACKET_get_length_prefixed_1(pkt, &session_id)) {
1166 al = SSL_AD_DECODE_ERROR;
1167 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1171 if (SSL_IS_DTLS(s)) {
1172 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1173 al = SSL_AD_DECODE_ERROR;
1174 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1178 * If we require cookies and this ClientHello doesn't contain one,
1179 * just return since we do not want to allocate any memory yet.
1180 * So check cookie length...
1182 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1183 if (PACKET_remaining(&cookie) == 0)
1188 if (!PACKET_get_length_prefixed_2(pkt, &cipher_suites)
1189 || !PACKET_get_length_prefixed_1(pkt, &compression)) {
1190 al = SSL_AD_DECODE_ERROR;
1191 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1194 /* Could be empty. */
1201 * We don't allow resumption in a backwards compatible ClientHello.
1202 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1204 * Versions before 0.9.7 always allow clients to resume sessions in
1205 * renegotiation. 0.9.7 and later allow this by default, but optionally
1206 * ignore resumption requests with flag
1207 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1208 * than a change to default behavior so that applications relying on
1209 * this for security won't even compile against older library versions).
1210 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1211 * request renegotiation but not a new session (s->new_session remains
1212 * unset): for servers, this essentially just means that the
1213 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1218 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1219 if (!ssl_get_new_session(s, 1))
1222 i = ssl_get_prev_session(s, &extensions, &session_id);
1224 * Only resume if the session's version matches the negotiated
1226 * RFC 5246 does not provide much useful advice on resumption
1227 * with a different protocol version. It doesn't forbid it but
1228 * the sanity of such behaviour would be questionable.
1229 * In practice, clients do not accept a version mismatch and
1230 * will abort the handshake with an error.
1232 if (i == 1 && s->version == s->session->ssl_version) {
1233 /* previous session */
1235 } else if (i == -1) {
1239 if (!ssl_get_new_session(s, 1))
1244 if (SSL_IS_DTLS(s)) {
1245 /* Empty cookie was already handled above by returning early. */
1246 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1247 if (s->ctx->app_verify_cookie_cb != NULL) {
1248 if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookie),
1249 PACKET_remaining(&cookie)) == 0) {
1250 al = SSL_AD_HANDSHAKE_FAILURE;
1251 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1252 SSL_R_COOKIE_MISMATCH);
1254 /* else cookie verification succeeded */
1256 /* default verification */
1257 } else if (!PACKET_equal(&cookie, s->d1->cookie,
1258 s->d1->cookie_len)) {
1259 al = SSL_AD_HANDSHAKE_FAILURE;
1260 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1263 s->d1->cookie_verified = 1;
1265 if (s->method->version == DTLS_ANY_VERSION) {
1266 /* Select version to use */
1267 if (s->client_version <= DTLS1_2_VERSION &&
1268 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1269 s->version = DTLS1_2_VERSION;
1270 s->method = DTLSv1_2_server_method();
1271 } else if (tls1_suiteb(s)) {
1272 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1273 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1274 s->version = s->client_version;
1275 al = SSL_AD_PROTOCOL_VERSION;
1277 } else if (s->client_version <= DTLS1_VERSION &&
1278 !(s->options & SSL_OP_NO_DTLSv1)) {
1279 s->version = DTLS1_VERSION;
1280 s->method = DTLSv1_server_method();
1282 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1283 SSL_R_WRONG_VERSION_NUMBER);
1284 s->version = s->client_version;
1285 al = SSL_AD_PROTOCOL_VERSION;
1288 s->session->ssl_version = s->version;
1292 if (ssl_bytes_to_cipher_list(s, &cipher_suites, &(ciphers),
1293 is_v2_record, &al) == NULL) {
1297 /* If it is a hit, check that the cipher is in the list */
1300 id = s->session->cipher->id;
1303 fprintf(stderr, "client sent %d ciphers\n",
1304 sk_SSL_CIPHER_num(ciphers));
1306 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1307 c = sk_SSL_CIPHER_value(ciphers, i);
1309 fprintf(stderr, "client [%2d of %2d]:%s\n",
1310 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1319 * we need to have the cipher in the cipher list if we are asked
1322 al = SSL_AD_ILLEGAL_PARAMETER;
1323 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1324 SSL_R_REQUIRED_CIPHER_MISSING);
1329 complen = PACKET_remaining(&compression);
1330 for (j = 0; j < complen; j++) {
1331 if (PACKET_data(&compression)[j] == 0)
1337 al = SSL_AD_DECODE_ERROR;
1338 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1342 /* TLS extensions */
1343 if (s->version >= SSL3_VERSION) {
1344 if (!ssl_parse_clienthello_tlsext(s, &extensions)) {
1345 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1351 * Check if we want to use external pre-shared secret for this handshake
1352 * for not reused session only. We need to generate server_random before
1353 * calling tls_session_secret_cb in order to allow SessionTicket
1354 * processing to use it in key derivation.
1358 pos = s->s3->server_random;
1359 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1364 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1365 SSL_CIPHER *pref_cipher = NULL;
1367 s->session->master_key_length = sizeof(s->session->master_key);
1368 if (s->tls_session_secret_cb(s, s->session->master_key,
1369 &s->session->master_key_length, ciphers,
1371 s->tls_session_secret_cb_arg)) {
1373 s->session->ciphers = ciphers;
1374 s->session->verify_result = X509_V_OK;
1378 /* check if some cipher was preferred by call back */
1380 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1385 if (pref_cipher == NULL) {
1386 al = SSL_AD_HANDSHAKE_FAILURE;
1387 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1391 s->session->cipher = pref_cipher;
1392 sk_SSL_CIPHER_free(s->cipher_list);
1393 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1394 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1395 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1400 * Worst case, we will use the NULL compression, but if we have other
1401 * options, we will now look for them. We have complen-1 compression
1402 * algorithms from the client, starting at q.
1404 s->s3->tmp.new_compression = NULL;
1405 #ifndef OPENSSL_NO_COMP
1406 /* This only happens if we have a cache hit */
1407 if (s->session->compress_meth != 0) {
1408 int m, comp_id = s->session->compress_meth;
1410 /* Perform sanity checks on resumed compression algorithm */
1411 /* Can't disable compression */
1412 if (!ssl_allow_compression(s)) {
1413 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1414 SSL_R_INCONSISTENT_COMPRESSION);
1417 /* Look for resumed compression method */
1418 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1419 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1420 if (comp_id == comp->id) {
1421 s->s3->tmp.new_compression = comp;
1425 if (s->s3->tmp.new_compression == NULL) {
1426 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1427 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1430 /* Look for resumed method in compression list */
1431 for (k = 0; k < complen; k++) {
1432 if (PACKET_data(&compression)[k] == comp_id)
1436 al = SSL_AD_ILLEGAL_PARAMETER;
1437 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1438 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1443 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1444 /* See if we have a match */
1445 int m, nn, v, done = 0;
1448 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1449 for (m = 0; m < nn; m++) {
1450 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1452 for (o = 0; o < complen; o++) {
1453 if (v == PACKET_data(&compression)[o]) {
1462 s->s3->tmp.new_compression = comp;
1468 * If compression is disabled we'd better not try to resume a session
1469 * using compression.
1471 if (s->session->compress_meth != 0) {
1472 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1478 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1482 #ifdef OPENSSL_NO_COMP
1483 s->session->compress_meth = 0;
1485 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1487 sk_SSL_CIPHER_free(s->session->ciphers);
1488 s->session->ciphers = ciphers;
1489 if (ciphers == NULL) {
1490 al = SSL_AD_INTERNAL_ERROR;
1491 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1495 if (!tls1_set_server_sigalgs(s)) {
1496 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1501 sk_SSL_CIPHER_free(ciphers);
1502 return MSG_PROCESS_CONTINUE_PROCESSING;
1504 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1506 ossl_statem_set_error(s);
1508 sk_SSL_CIPHER_free(ciphers);
1509 return MSG_PROCESS_ERROR;
1513 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1515 int al = SSL_AD_HANDSHAKE_FAILURE;
1518 if (wst == WORK_MORE_A) {
1520 /* Let cert callback update server certificates if required */
1521 if (s->cert->cert_cb) {
1522 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1524 al = SSL_AD_INTERNAL_ERROR;
1525 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1529 s->rwstate = SSL_X509_LOOKUP;
1532 s->rwstate = SSL_NOTHING;
1534 cipher = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1536 if (cipher == NULL) {
1537 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1540 s->s3->tmp.new_cipher = cipher;
1541 /* check whether we should disable session resumption */
1542 if (s->not_resumable_session_cb != NULL)
1543 s->session->not_resumable = s->not_resumable_session_cb(s,
1544 ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0));
1545 if (s->session->not_resumable)
1546 /* do not send a session ticket */
1547 s->tlsext_ticket_expected = 0;
1549 /* Session-id reuse */
1550 s->s3->tmp.new_cipher = s->session->cipher;
1553 if (!(s->verify_mode & SSL_VERIFY_PEER)) {
1554 if (!ssl3_digest_cached_records(s, 0)) {
1555 al = SSL_AD_INTERNAL_ERROR;
1561 * we now have the following setup.
1563 * cipher_list - our prefered list of ciphers
1564 * ciphers - the clients prefered list of ciphers
1565 * compression - basically ignored right now
1566 * ssl version is set - sslv3
1567 * s->session - The ssl session has been setup.
1568 * s->hit - session reuse flag
1569 * s->s3->tmp.new_cipher- the new cipher to use.
1572 /* Handles TLS extensions that we couldn't check earlier */
1573 if (s->version >= SSL3_VERSION) {
1574 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1575 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1576 SSL_R_CLIENTHELLO_TLSEXT);
1583 #ifndef OPENSSL_NO_SRP
1584 if (wst == WORK_MORE_B) {
1586 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1588 * callback indicates further work to be done
1590 s->rwstate = SSL_X509_LOOKUP;
1593 if (ret != SSL_ERROR_NONE) {
1595 * This is not really an error but the only means to for
1596 * a client to detect whether srp is supported.
1598 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1599 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1600 SSL_R_CLIENTHELLO_TLSEXT);
1607 return WORK_FINISHED_STOP;
1609 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1610 ossl_statem_set_error(s);
1614 int tls_construct_server_hello(SSL *s)
1617 unsigned char *p, *d;
1622 buf = (unsigned char *)s->init_buf->data;
1624 /* Do the message type and length last */
1625 d = p = ssl_handshake_start(s);
1627 *(p++) = s->version >> 8;
1628 *(p++) = s->version & 0xff;
1631 * Random stuff. Filling of the server_random takes place in
1632 * tls_process_client_hello()
1634 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1635 p += SSL3_RANDOM_SIZE;
1638 * There are several cases for the session ID to send
1639 * back in the server hello:
1640 * - For session reuse from the session cache,
1641 * we send back the old session ID.
1642 * - If stateless session reuse (using a session ticket)
1643 * is successful, we send back the client's "session ID"
1644 * (which doesn't actually identify the session).
1645 * - If it is a new session, we send back the new
1647 * - However, if we want the new session to be single-use,
1648 * we send back a 0-length session ID.
1649 * s->hit is non-zero in either case of session reuse,
1650 * so the following won't overwrite an ID that we're supposed
1653 if (s->session->not_resumable ||
1654 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1656 s->session->session_id_length = 0;
1658 sl = s->session->session_id_length;
1659 if (sl > (int)sizeof(s->session->session_id)) {
1660 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1661 ossl_statem_set_error(s);
1665 memcpy(p, s->session->session_id, sl);
1668 /* put the cipher */
1669 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1672 /* put the compression method */
1673 #ifdef OPENSSL_NO_COMP
1676 if (s->s3->tmp.new_compression == NULL)
1679 *(p++) = s->s3->tmp.new_compression->id;
1682 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1683 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1684 ossl_statem_set_error(s);
1688 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1690 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1691 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1692 ossl_statem_set_error(s);
1698 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1699 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1700 ossl_statem_set_error(s);
1707 int tls_construct_server_done(SSL *s)
1709 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1710 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1711 ossl_statem_set_error(s);
1715 if (!s->s3->tmp.cert_request) {
1716 if (!ssl3_digest_cached_records(s, 0)) {
1717 ossl_statem_set_error(s);
1724 int tls_construct_server_key_exchange(SSL *s)
1726 #ifndef OPENSSL_NO_RSA
1729 #ifndef OPENSSL_NO_DH
1730 DH *dh = NULL, *dhp;
1732 #ifndef OPENSSL_NO_EC
1733 EC_KEY *ecdh = NULL, *ecdhp;
1734 unsigned char *encodedPoint = NULL;
1737 BN_CTX *bn_ctx = NULL;
1740 const EVP_MD *md = NULL;
1741 unsigned char *p, *d;
1751 EVP_MD_CTX_init(&md_ctx);
1753 type = s->s3->tmp.new_cipher->algorithm_mkey;
1758 r[0] = r[1] = r[2] = r[3] = NULL;
1760 #ifndef OPENSSL_NO_PSK
1761 if (type & SSL_PSK) {
1763 * reserve size for record length and PSK identity hint
1766 if (s->cert->psk_identity_hint)
1767 n += strlen(s->cert->psk_identity_hint);
1769 /* Plain PSK or RSAPSK nothing to do */
1770 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
1772 #endif /* !OPENSSL_NO_PSK */
1773 #ifndef OPENSSL_NO_RSA
1774 if (type & SSL_kRSA) {
1775 rsa = cert->rsa_tmp;
1776 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1777 rsa = s->cert->rsa_tmp_cb(s,
1778 SSL_C_IS_EXPORT(s->s3->
1780 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1783 al = SSL_AD_HANDSHAKE_FAILURE;
1784 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1785 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1789 cert->rsa_tmp = rsa;
1792 al = SSL_AD_HANDSHAKE_FAILURE;
1793 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1794 SSL_R_MISSING_TMP_RSA_KEY);
1799 s->s3->tmp.use_rsa_tmp = 1;
1802 #ifndef OPENSSL_NO_DH
1803 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
1804 if (s->cert->dh_tmp_auto) {
1805 dhp = ssl_get_auto_dh(s);
1807 al = SSL_AD_INTERNAL_ERROR;
1808 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1809 ERR_R_INTERNAL_ERROR);
1814 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1815 dhp = s->cert->dh_tmp_cb(s,
1816 SSL_C_IS_EXPORT(s->s3->
1818 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1821 al = SSL_AD_HANDSHAKE_FAILURE;
1822 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1823 SSL_R_MISSING_TMP_DH_KEY);
1826 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1827 DH_security_bits(dhp), 0, dhp)) {
1828 al = SSL_AD_HANDSHAKE_FAILURE;
1829 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1830 SSL_R_DH_KEY_TOO_SMALL);
1833 if (s->s3->tmp.dh != NULL) {
1834 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1835 ERR_R_INTERNAL_ERROR);
1839 if (s->cert->dh_tmp_auto)
1841 else if ((dh = DHparams_dup(dhp)) == NULL) {
1842 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1847 if ((dhp->pub_key == NULL ||
1848 dhp->priv_key == NULL ||
1849 (s->options & SSL_OP_SINGLE_DH_USE))) {
1850 if (!DH_generate_key(dh)) {
1851 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1855 dh->pub_key = BN_dup(dhp->pub_key);
1856 dh->priv_key = BN_dup(dhp->priv_key);
1857 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1858 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1867 #ifndef OPENSSL_NO_EC
1868 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1869 const EC_GROUP *group;
1872 if (s->cert->ecdh_tmp_auto) {
1873 /* Get NID of appropriate shared curve */
1874 int nid = tls1_shared_curve(s, -2);
1875 if (nid != NID_undef)
1876 ecdhp = EC_KEY_new_by_curve_name(nid);
1878 if (ecdhp == NULL) {
1879 al = SSL_AD_HANDSHAKE_FAILURE;
1880 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1881 SSL_R_MISSING_TMP_ECDH_KEY);
1885 if (s->s3->tmp.ecdh != NULL) {
1886 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1887 ERR_R_INTERNAL_ERROR);
1891 /* Duplicate the ECDH structure. */
1892 if (ecdhp == NULL) {
1893 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1896 if (s->cert->ecdh_tmp_auto)
1898 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1899 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1903 s->s3->tmp.ecdh = ecdh;
1904 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1905 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1906 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1907 if (!EC_KEY_generate_key(ecdh)) {
1908 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1914 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1915 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1916 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1917 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1921 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1922 (EC_GROUP_get_degree(group) > 163)) {
1923 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1924 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1929 * XXX: For now, we only support ephemeral ECDH keys over named
1930 * (not generic) curves. For supported named curves, curve_id is
1934 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1936 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1937 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1942 * Encode the public key. First check the size of encoding and
1943 * allocate memory accordingly.
1945 encodedlen = EC_POINT_point2oct(group,
1946 EC_KEY_get0_public_key(ecdh),
1947 POINT_CONVERSION_UNCOMPRESSED,
1950 encodedPoint = (unsigned char *)
1951 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1952 bn_ctx = BN_CTX_new();
1953 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1954 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1955 ERR_R_MALLOC_FAILURE);
1959 encodedlen = EC_POINT_point2oct(group,
1960 EC_KEY_get0_public_key(ecdh),
1961 POINT_CONVERSION_UNCOMPRESSED,
1962 encodedPoint, encodedlen, bn_ctx);
1964 if (encodedlen == 0) {
1965 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1969 BN_CTX_free(bn_ctx);
1973 * XXX: For now, we only support named (not generic) curves in
1974 * ECDH ephemeral key exchanges. In this situation, we need four
1975 * additional bytes to encode the entire ServerECDHParams
1978 n += 4 + encodedlen;
1981 * We'll generate the serverKeyExchange message explicitly so we
1982 * can set these to NULLs
1989 #endif /* !OPENSSL_NO_EC */
1990 #ifndef OPENSSL_NO_SRP
1991 if (type & SSL_kSRP) {
1992 if ((s->srp_ctx.N == NULL) ||
1993 (s->srp_ctx.g == NULL) ||
1994 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1995 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1996 SSL_R_MISSING_SRP_PARAM);
1999 r[0] = s->srp_ctx.N;
2000 r[1] = s->srp_ctx.g;
2001 r[2] = s->srp_ctx.s;
2002 r[3] = s->srp_ctx.B;
2006 al = SSL_AD_HANDSHAKE_FAILURE;
2007 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2008 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2011 for (i = 0; i < 4 && r[i] != NULL; i++) {
2012 nr[i] = BN_num_bytes(r[i]);
2013 #ifndef OPENSSL_NO_SRP
2014 if ((i == 2) && (type & SSL_kSRP))
2021 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP))
2022 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
2023 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
2025 al = SSL_AD_DECODE_ERROR;
2028 kn = EVP_PKEY_size(pkey);
2034 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
2035 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
2038 d = p = ssl_handshake_start(s);
2040 #ifndef OPENSSL_NO_PSK
2041 if (type & SSL_PSK) {
2042 /* copy PSK identity hint */
2043 if (s->cert->psk_identity_hint) {
2044 s2n(strlen(s->cert->psk_identity_hint), p);
2045 strncpy((char *)p, s->cert->psk_identity_hint,
2046 strlen(s->cert->psk_identity_hint));
2047 p += strlen(s->cert->psk_identity_hint);
2054 for (i = 0; i < 4 && r[i] != NULL; i++) {
2055 #ifndef OPENSSL_NO_SRP
2056 if ((i == 2) && (type & SSL_kSRP)) {
2066 #ifndef OPENSSL_NO_EC
2067 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2069 * XXX: For now, we only support named (not generic) curves. In
2070 * this situation, the serverKeyExchange message has: [1 byte
2071 * CurveType], [2 byte CurveName] [1 byte length of encoded
2072 * point], followed by the actual encoded point itself
2074 *p = NAMED_CURVE_TYPE;
2082 memcpy(p, encodedPoint, encodedlen);
2083 OPENSSL_free(encodedPoint);
2084 encodedPoint = NULL;
2092 * n is the length of the params, they start at &(d[4]) and p
2093 * points to the space at the end.
2096 /* send signature algorithm */
2097 if (SSL_USE_SIGALGS(s)) {
2098 if (!tls12_get_sigandhash(p, pkey, md)) {
2099 /* Should never happen */
2100 al = SSL_AD_INTERNAL_ERROR;
2101 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2102 ERR_R_INTERNAL_ERROR);
2108 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2110 if (EVP_SignInit_ex(&md_ctx, md, NULL) <= 0
2111 || EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2112 SSL3_RANDOM_SIZE) <= 0
2113 || EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2114 SSL3_RANDOM_SIZE) <= 0
2115 || EVP_SignUpdate(&md_ctx, d, n) <= 0
2116 || EVP_SignFinal(&md_ctx, &(p[2]),
2117 (unsigned int *)&i, pkey) <= 0) {
2118 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2119 al = SSL_AD_INTERNAL_ERROR;
2124 if (SSL_USE_SIGALGS(s))
2127 /* Is this error check actually needed? */
2128 al = SSL_AD_HANDSHAKE_FAILURE;
2129 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2130 SSL_R_UNKNOWN_PKEY_TYPE);
2135 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
2136 al = SSL_AD_HANDSHAKE_FAILURE;
2137 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2141 EVP_MD_CTX_cleanup(&md_ctx);
2144 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2146 #ifndef OPENSSL_NO_EC
2147 OPENSSL_free(encodedPoint);
2148 BN_CTX_free(bn_ctx);
2150 EVP_MD_CTX_cleanup(&md_ctx);
2151 ossl_statem_set_error(s);
2155 int tls_construct_certificate_request(SSL *s)
2157 unsigned char *p, *d;
2158 int i, j, nl, off, n;
2159 STACK_OF(X509_NAME) *sk = NULL;
2165 d = p = ssl_handshake_start(s);
2167 /* get the list of acceptable cert types */
2169 n = ssl3_get_req_cert_type(s, p);
2174 if (SSL_USE_SIGALGS(s)) {
2175 const unsigned char *psigs;
2176 unsigned char *etmp = p;
2177 nl = tls12_get_psigalgs(s, &psigs);
2178 /* Skip over length for now */
2180 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2181 /* Now fill in length */
2191 sk = SSL_get_client_CA_list(s);
2194 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2195 name = sk_X509_NAME_value(sk, i);
2196 j = i2d_X509_NAME(name, NULL);
2197 if (!BUF_MEM_grow_clean
2198 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2199 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2203 p = ssl_handshake_start(s) + n;
2205 i2d_X509_NAME(name, &p);
2210 /* else no CA names */
2211 p = ssl_handshake_start(s) + off;
2214 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2215 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2219 s->s3->tmp.cert_request = 1;
2223 ossl_statem_set_error(s);
2227 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
2231 unsigned long alg_k;
2232 #ifndef OPENSSL_NO_RSA
2234 EVP_PKEY *pkey = NULL;
2236 #ifndef OPENSSL_NO_DH
2238 DH *dh_srvr, *dh_clnt = NULL;
2240 #ifndef OPENSSL_NO_EC
2241 EC_KEY *srvr_ecdh = NULL;
2242 EVP_PKEY *clnt_pub_pkey = NULL;
2243 EC_POINT *clnt_ecpoint = NULL;
2244 BN_CTX *bn_ctx = NULL;
2246 PACKET enc_premaster;
2247 unsigned char *data, *rsa_decrypt = NULL;
2249 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2251 #ifndef OPENSSL_NO_PSK
2252 /* For PSK parse and retrieve identity, obtain PSK key */
2253 if (alg_k & SSL_PSK) {
2254 unsigned char psk[PSK_MAX_PSK_LEN];
2256 PACKET psk_identity;
2258 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2259 al = SSL_AD_DECODE_ERROR;
2260 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2263 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2264 al = SSL_AD_DECODE_ERROR;
2265 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2266 SSL_R_DATA_LENGTH_TOO_LONG);
2269 if (s->psk_server_callback == NULL) {
2270 al = SSL_AD_INTERNAL_ERROR;
2271 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2272 SSL_R_PSK_NO_SERVER_CB);
2276 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2277 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2278 al = SSL_AD_INTERNAL_ERROR;
2282 psklen = s->psk_server_callback(s, s->session->psk_identity,
2285 if (psklen > PSK_MAX_PSK_LEN) {
2286 al = SSL_AD_INTERNAL_ERROR;
2287 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2289 } else if (psklen == 0) {
2291 * PSK related to the given identity not found
2293 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2294 SSL_R_PSK_IDENTITY_NOT_FOUND);
2295 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2299 OPENSSL_free(s->s3->tmp.psk);
2300 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2301 OPENSSL_cleanse(psk, psklen);
2303 if (s->s3->tmp.psk == NULL) {
2304 al = SSL_AD_INTERNAL_ERROR;
2305 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2309 s->s3->tmp.psklen = psklen;
2311 if (alg_k & SSL_kPSK) {
2312 /* Identity extracted earlier: should be nothing left */
2313 if (PACKET_remaining(pkt) != 0) {
2314 al = SSL_AD_HANDSHAKE_FAILURE;
2315 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2318 /* PSK handled by ssl_generate_master_secret */
2319 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2320 al = SSL_AD_INTERNAL_ERROR;
2321 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2326 #ifndef OPENSSL_NO_RSA
2327 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2328 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2330 unsigned char decrypt_good, version_good;
2333 /* FIX THIS UP EAY EAY EAY EAY */
2334 if (s->s3->tmp.use_rsa_tmp) {
2335 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2336 rsa = s->cert->rsa_tmp;
2338 * Don't do a callback because rsa_tmp should be sent already
2341 al = SSL_AD_HANDSHAKE_FAILURE;
2342 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2343 SSL_R_MISSING_TMP_RSA_PKEY);
2348 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2349 if ((pkey == NULL) ||
2350 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2351 al = SSL_AD_HANDSHAKE_FAILURE;
2352 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2353 SSL_R_MISSING_RSA_CERTIFICATE);
2356 rsa = pkey->pkey.rsa;
2359 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2360 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2361 enc_premaster = *pkt;
2364 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2365 || PACKET_remaining(pkt) != 0) {
2366 /* Try SSLv3 behaviour for TLS. */
2367 if (s->options & SSL_OP_TLS_D5_BUG) {
2368 enc_premaster = orig;
2370 al = SSL_AD_DECODE_ERROR;
2371 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2372 SSL_R_LENGTH_MISMATCH);
2379 * We want to be sure that the plaintext buffer size makes it safe to
2380 * iterate over the entire size of a premaster secret
2381 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2382 * their ciphertext cannot accommodate a premaster secret anyway.
2384 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2385 al = SSL_AD_INTERNAL_ERROR;
2386 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2387 RSA_R_KEY_SIZE_TOO_SMALL);
2391 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2392 if (rsa_decrypt == NULL) {
2393 al = SSL_AD_INTERNAL_ERROR;
2394 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2399 * We must not leak whether a decryption failure occurs because of
2400 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2401 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2402 * generates a random premaster secret for the case that the decrypt
2403 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2406 if (RAND_bytes(rand_premaster_secret,
2407 sizeof(rand_premaster_secret)) <= 0) {
2411 decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),
2412 PACKET_data(&enc_premaster),
2413 rsa_decrypt, rsa, RSA_PKCS1_PADDING);
2417 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2418 * be 0xff if so and zero otherwise.
2421 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2424 * If the version in the decrypted pre-master secret is correct then
2425 * version_good will be 0xff, otherwise it'll be zero. The
2426 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2427 * (http://eprint.iacr.org/2003/052/) exploits the version number
2428 * check as a "bad version oracle". Thus version checks are done in
2429 * constant time and are treated like any other decryption error.
2432 constant_time_eq_8(rsa_decrypt[0],
2433 (unsigned)(s->client_version >> 8));
2435 constant_time_eq_8(rsa_decrypt[1],
2436 (unsigned)(s->client_version & 0xff));
2439 * The premaster secret must contain the same version number as the
2440 * ClientHello to detect version rollback attacks (strangely, the
2441 * protocol does not offer such protection for DH ciphersuites).
2442 * However, buggy clients exist that send the negotiated protocol
2443 * version instead if the server does not support the requested
2444 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2447 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2448 unsigned char workaround_good;
2450 constant_time_eq_8(rsa_decrypt[0], (unsigned)(s->version >> 8));
2452 constant_time_eq_8(rsa_decrypt[1],
2453 (unsigned)(s->version & 0xff));
2454 version_good |= workaround_good;
2458 * Both decryption and version must be good for decrypt_good to
2459 * remain non-zero (0xff).
2461 decrypt_good &= version_good;
2464 * Now copy rand_premaster_secret over from p using
2465 * decrypt_good_mask. If decryption failed, then p does not
2466 * contain valid plaintext, however, a check above guarantees
2467 * it is still sufficiently large to read from.
2469 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2471 constant_time_select_8(decrypt_good, rsa_decrypt[j],
2472 rand_premaster_secret[j]);
2475 if (!ssl_generate_master_secret(s, rsa_decrypt,
2476 sizeof(rand_premaster_secret), 0)) {
2477 al = SSL_AD_INTERNAL_ERROR;
2478 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2481 OPENSSL_free(rsa_decrypt);
2485 #ifndef OPENSSL_NO_DH
2486 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2488 EVP_PKEY *skey = NULL;
2489 PACKET bookmark = *pkt;
2490 unsigned char shared[(OPENSSL_DH_MAX_MODULUS_BITS + 7) / 8];
2492 if (!PACKET_get_net_2(pkt, &i)) {
2493 if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2494 al = SSL_AD_HANDSHAKE_FAILURE;
2495 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2496 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2501 if (PACKET_remaining(pkt) != i) {
2502 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2503 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2504 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2508 i = PACKET_remaining(pkt);
2511 if (alg_k & SSL_kDHr)
2512 idx = SSL_PKEY_DH_RSA;
2513 else if (alg_k & SSL_kDHd)
2514 idx = SSL_PKEY_DH_DSA;
2516 skey = s->cert->pkeys[idx].privatekey;
2517 if ((skey == NULL) ||
2518 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2519 al = SSL_AD_HANDSHAKE_FAILURE;
2520 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2521 SSL_R_MISSING_RSA_CERTIFICATE);
2524 dh_srvr = skey->pkey.dh;
2525 } else if (s->s3->tmp.dh == NULL) {
2526 al = SSL_AD_HANDSHAKE_FAILURE;
2527 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2528 SSL_R_MISSING_TMP_DH_KEY);
2531 dh_srvr = s->s3->tmp.dh;
2533 if (PACKET_remaining(pkt) == 0L) {
2534 /* Get pubkey from cert */
2535 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2537 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2538 dh_clnt = EVP_PKEY_get1_DH(clkey);
2540 if (dh_clnt == NULL) {
2541 al = SSL_AD_HANDSHAKE_FAILURE;
2542 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2543 SSL_R_MISSING_TMP_DH_KEY);
2546 EVP_PKEY_free(clkey);
2547 pub = dh_clnt->pub_key;
2549 if (!PACKET_get_bytes(pkt, &data, i)) {
2550 /* We already checked we have enough data */
2551 al = SSL_AD_INTERNAL_ERROR;
2552 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2553 ERR_R_INTERNAL_ERROR);
2556 pub = BN_bin2bn(data, i, NULL);
2559 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2563 i = DH_compute_key(shared, pub, dh_srvr);
2566 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2571 DH_free(s->s3->tmp.dh);
2572 s->s3->tmp.dh = NULL;
2578 if (!ssl_generate_master_secret(s, shared, i, 0)) {
2579 al = SSL_AD_INTERNAL_ERROR;
2580 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2584 s->statem.no_cert_verify = 1;
2585 return MSG_PROCESS_CONTINUE_PROCESSING;
2590 #ifndef OPENSSL_NO_EC
2591 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2594 const EC_GROUP *group;
2595 const BIGNUM *priv_key;
2596 unsigned char *shared;
2598 /* initialize structures for server's ECDH key pair */
2599 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2600 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2604 /* Let's get server private key and group information */
2605 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2606 /* use the certificate */
2607 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2610 * use the ephermeral values we saved when generating the
2611 * ServerKeyExchange msg.
2613 tkey = s->s3->tmp.ecdh;
2616 group = EC_KEY_get0_group(tkey);
2617 priv_key = EC_KEY_get0_private_key(tkey);
2619 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2620 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2621 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2625 /* Let's get client's public key */
2626 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2627 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2631 if (PACKET_remaining(pkt) == 0L) {
2632 /* Client Publickey was in Client Certificate */
2634 if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2635 al = SSL_AD_HANDSHAKE_FAILURE;
2636 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2637 SSL_R_MISSING_TMP_ECDH_KEY);
2640 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2641 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2643 * XXX: For now, we do not support client authentication
2644 * using ECDH certificates so this branch (n == 0L) of the
2645 * code is never executed. When that support is added, we
2646 * ought to ensure the key received in the certificate is
2647 * authorized for key agreement. ECDH_compute_key implicitly
2648 * checks that the two ECDH shares are for the same group.
2650 al = SSL_AD_HANDSHAKE_FAILURE;
2651 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2652 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2656 if (EC_POINT_copy(clnt_ecpoint,
2657 EC_KEY_get0_public_key(clnt_pub_pkey->
2659 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2662 s->statem.no_cert_verify = 1;
2665 * Get client's public key from encoded point in the
2666 * ClientKeyExchange message.
2668 if ((bn_ctx = BN_CTX_new()) == NULL) {
2669 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2670 ERR_R_MALLOC_FAILURE);
2674 /* Get encoded point length */
2675 if (!PACKET_get_1(pkt, &i)) {
2676 al = SSL_AD_DECODE_ERROR;
2677 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2678 SSL_R_LENGTH_MISMATCH);
2681 if (!PACKET_get_bytes(pkt, &data, i)
2682 || PACKET_remaining(pkt) != 0) {
2683 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2686 if (EC_POINT_oct2point(group, clnt_ecpoint, data, i, bn_ctx) == 0) {
2687 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2692 /* Compute the shared pre-master secret */
2693 field_size = EC_GROUP_get_degree(group);
2694 if (field_size <= 0) {
2695 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2698 shared = OPENSSL_malloc((field_size + 7) / 8);
2699 if (shared == NULL) {
2700 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2703 i = ECDH_compute_key(shared, (field_size + 7) / 8, clnt_ecpoint,
2706 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2707 OPENSSL_free(shared);
2711 EVP_PKEY_free(clnt_pub_pkey);
2712 EC_POINT_free(clnt_ecpoint);
2713 EC_KEY_free(srvr_ecdh);
2714 BN_CTX_free(bn_ctx);
2715 EC_KEY_free(s->s3->tmp.ecdh);
2716 s->s3->tmp.ecdh = NULL;
2718 if (!ssl_generate_master_secret(s, shared, i, 1)) {
2719 al = SSL_AD_INTERNAL_ERROR;
2720 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2723 return MSG_PROCESS_CONTINUE_PROCESSING;
2726 #ifndef OPENSSL_NO_SRP
2727 if (alg_k & SSL_kSRP) {
2728 if (!PACKET_get_net_2(pkt, &i)
2729 || !PACKET_get_bytes(pkt, &data, i)) {
2730 al = SSL_AD_DECODE_ERROR;
2731 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BAD_SRP_A_LENGTH);
2734 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2735 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2738 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2739 || BN_is_zero(s->srp_ctx.A)) {
2740 al = SSL_AD_ILLEGAL_PARAMETER;
2741 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2742 SSL_R_BAD_SRP_PARAMETERS);
2745 OPENSSL_free(s->session->srp_username);
2746 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2747 if (s->session->srp_username == NULL) {
2748 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2752 if (!srp_generate_server_master_secret(s)) {
2753 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2757 #endif /* OPENSSL_NO_SRP */
2758 #ifndef OPENSSL_NO_GOST
2759 if (alg_k & SSL_kGOST) {
2760 EVP_PKEY_CTX *pkey_ctx;
2761 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2762 unsigned char premaster_secret[32], *start;
2763 size_t outlen = 32, inlen;
2764 unsigned long alg_a;
2769 /* Get our certificate private key */
2770 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2771 if (alg_a & SSL_aGOST12) {
2773 * New GOST ciphersuites have SSL_aGOST01 bit too
2775 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2777 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2780 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2782 } else if (alg_a & SSL_aGOST01) {
2783 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2786 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2787 if (pkey_ctx == NULL) {
2788 al = SSL_AD_INTERNAL_ERROR;
2789 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2792 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2793 al = SSL_AD_INTERNAL_ERROR;
2794 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2798 * If client certificate is present and is of the same type, maybe
2799 * use it for key exchange. Don't mind errors from
2800 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2801 * client certificate for authorization only.
2803 client_pub_pkey = X509_get_pubkey(s->session->peer);
2804 if (client_pub_pkey) {
2805 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2808 /* Decrypt session key */
2809 sess_key_len = PACKET_remaining(pkt);
2810 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2811 al = SSL_AD_INTERNAL_ERROR;
2812 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2815 if (ASN1_get_object ((const unsigned char **)&data, &Tlen, &Ttag,
2816 &Tclass, sess_key_len) != V_ASN1_CONSTRUCTED
2817 || Ttag != V_ASN1_SEQUENCE
2818 || Tclass != V_ASN1_UNIVERSAL) {
2819 al = SSL_AD_DECODE_ERROR;
2820 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2821 SSL_R_DECRYPTION_FAILED);
2826 if (EVP_PKEY_decrypt
2827 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2828 al = SSL_AD_DECODE_ERROR;
2829 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2830 SSL_R_DECRYPTION_FAILED);
2833 /* Generate master secret */
2834 if (!ssl_generate_master_secret(s, premaster_secret,
2835 sizeof(premaster_secret), 0)) {
2836 al = SSL_AD_INTERNAL_ERROR;
2837 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2840 /* Check if pubkey from client certificate was used */
2841 if (EVP_PKEY_CTX_ctrl
2842 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2843 s->statem.no_cert_verify = 1;
2845 EVP_PKEY_free(client_pub_pkey);
2846 EVP_PKEY_CTX_free(pkey_ctx);
2847 return MSG_PROCESS_CONTINUE_PROCESSING;
2849 EVP_PKEY_free(client_pub_pkey);
2850 EVP_PKEY_CTX_free(pkey_ctx);
2855 al = SSL_AD_HANDSHAKE_FAILURE;
2856 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2860 return MSG_PROCESS_CONTINUE_PROCESSING;
2862 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2863 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2866 #ifndef OPENSSL_NO_EC
2867 EVP_PKEY_free(clnt_pub_pkey);
2868 EC_POINT_free(clnt_ecpoint);
2869 EC_KEY_free(srvr_ecdh);
2870 BN_CTX_free(bn_ctx);
2871 OPENSSL_free(rsa_decrypt);
2873 #ifndef OPENSSL_NO_PSK
2874 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2875 s->s3->tmp.psk = NULL;
2877 ossl_statem_set_error(s);
2878 return MSG_PROCESS_ERROR;
2881 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
2883 #ifndef OPENSSL_NO_SCTP
2884 if (wst == WORK_MORE_A) {
2885 if (SSL_IS_DTLS(s)) {
2886 unsigned char sctpauthkey[64];
2887 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2889 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2892 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2893 sizeof(DTLS1_SCTP_AUTH_LABEL));
2895 if (SSL_export_keying_material(s, sctpauthkey,
2896 sizeof(sctpauthkey), labelbuffer,
2897 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
2898 ossl_statem_set_error(s);
2902 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2903 sizeof(sctpauthkey), sctpauthkey);
2908 if ((wst == WORK_MORE_B)
2910 && BIO_dgram_is_sctp(SSL_get_wbio(s))
2911 /* Are we renegotiating? */
2913 /* Are we going to skip the CertificateVerify? */
2914 && (s->session->peer == NULL || s->statem.no_cert_verify)
2915 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
2916 s->s3->in_read_app_data = 2;
2917 s->rwstate = SSL_READING;
2918 BIO_clear_retry_flags(SSL_get_rbio(s));
2919 BIO_set_retry_read(SSL_get_rbio(s));
2920 ossl_statem_set_sctp_read_sock(s, 1);
2923 ossl_statem_set_sctp_read_sock(s, 0);
2927 if (s->statem.no_cert_verify) {
2928 /* No certificate verify so we no longer need the handshake_buffer */
2929 BIO_free(s->s3->handshake_buffer);
2930 s->s3->handshake_buffer = NULL;
2931 return WORK_FINISHED_CONTINUE;
2933 if (!s->session->peer) {
2934 /* No peer certificate so we no longer need the handshake_buffer */
2935 BIO_free(s->s3->handshake_buffer);
2936 return WORK_FINISHED_CONTINUE;
2938 if (!s->s3->handshake_buffer) {
2939 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
2940 ERR_R_INTERNAL_ERROR);
2941 ossl_statem_set_error(s);
2945 * For sigalgs freeze the handshake buffer. If we support
2946 * extms we've done this already so this is a no-op
2948 if (!ssl3_digest_cached_records(s, 1)) {
2949 ossl_statem_set_error(s);
2954 return WORK_FINISHED_CONTINUE;
2957 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
2959 EVP_PKEY *pkey = NULL;
2960 unsigned char *sig, *data;
2961 int al, ret = MSG_PROCESS_ERROR;
2965 const EVP_MD *md = NULL;
2970 EVP_MD_CTX_init(&mctx);
2972 peer = s->session->peer;
2973 pkey = X509_get_pubkey(peer);
2974 type = X509_certificate_type(peer, pkey);
2976 if (!(type & EVP_PKT_SIGN)) {
2977 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
2978 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2979 al = SSL_AD_ILLEGAL_PARAMETER;
2983 /* Check for broken implementations of GOST ciphersuites */
2985 * If key is GOST and n is exactly 64, it is bare signature without
2986 * length field (CryptoPro implementations at least till CSP 4.0)
2988 #ifndef OPENSSL_NO_GOST
2989 if (PACKET_remaining(pkt) == 64 && pkey->type == NID_id_GostR3410_2001) {
2994 if (SSL_USE_SIGALGS(s)) {
2997 if (!PACKET_get_bytes(pkt, &sig, 2)) {
2998 al = SSL_AD_DECODE_ERROR;
3001 rv = tls12_check_peer_sigalg(&md, s, sig, pkey);
3003 al = SSL_AD_INTERNAL_ERROR;
3005 } else if (rv == 0) {
3006 al = SSL_AD_DECODE_ERROR;
3010 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
3013 /* Use default digest for this key type */
3014 int idx = ssl_cert_type(NULL, pkey);
3016 md = s->s3->tmp.md[idx];
3018 al = SSL_AD_INTERNAL_ERROR;
3023 if (!PACKET_get_net_2(pkt, &len)) {
3024 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
3025 al = SSL_AD_DECODE_ERROR;
3029 j = EVP_PKEY_size(pkey);
3030 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
3031 || (PACKET_remaining(pkt) == 0)) {
3032 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
3033 al = SSL_AD_DECODE_ERROR;
3036 if (!PACKET_get_bytes(pkt, &data, len)) {
3037 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
3038 al = SSL_AD_DECODE_ERROR;
3042 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3043 if (hdatalen <= 0) {
3044 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3045 al = SSL_AD_INTERNAL_ERROR;
3049 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
3051 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3052 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3053 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
3054 al = SSL_AD_INTERNAL_ERROR;
3058 #ifndef OPENSSL_NO_GOST
3059 if (pkey->type == NID_id_GostR3410_2001
3060 || pkey->type == NID_id_GostR3410_2012_256
3061 || pkey->type == NID_id_GostR3410_2012_512) {
3062 BUF_reverse(data, NULL, len);
3066 if (s->version == SSL3_VERSION
3067 && !EVP_MD_CTX_ctrl(&mctx, EVP_CTRL_SSL3_MASTER_SECRET,
3068 s->session->master_key_length,
3069 s->session->master_key)) {
3070 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
3071 al = SSL_AD_INTERNAL_ERROR;
3075 if (EVP_VerifyFinal(&mctx, data, len, pkey) <= 0) {
3076 al = SSL_AD_DECRYPT_ERROR;
3077 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3081 ret = MSG_PROCESS_CONTINUE_PROCESSING;
3084 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3085 ossl_statem_set_error(s);
3087 BIO_free(s->s3->handshake_buffer);
3088 s->s3->handshake_buffer = NULL;
3089 EVP_MD_CTX_cleanup(&mctx);
3090 EVP_PKEY_free(pkey);
3094 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3096 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3098 unsigned long l, llen;
3099 const unsigned char *certstart;
3100 unsigned char *certbytes;
3101 STACK_OF(X509) *sk = NULL;
3104 if ((sk = sk_X509_new_null()) == NULL) {
3105 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3109 if (!PACKET_get_net_3(pkt, &llen)
3110 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3111 || PACKET_remaining(pkt) != 0) {
3112 al = SSL_AD_DECODE_ERROR;
3113 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3117 while (PACKET_remaining(&spkt) > 0) {
3118 if (!PACKET_get_net_3(&spkt, &l)
3119 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3120 al = SSL_AD_DECODE_ERROR;
3121 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3122 SSL_R_CERT_LENGTH_MISMATCH);
3126 certstart = certbytes;
3127 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3129 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3132 if (certbytes != (certstart + l)) {
3133 al = SSL_AD_DECODE_ERROR;
3134 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3135 SSL_R_CERT_LENGTH_MISMATCH);
3138 if (!sk_X509_push(sk, x)) {
3139 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3145 if (sk_X509_num(sk) <= 0) {
3146 /* TLS does not mind 0 certs returned */
3147 if (s->version == SSL3_VERSION) {
3148 al = SSL_AD_HANDSHAKE_FAILURE;
3149 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3150 SSL_R_NO_CERTIFICATES_RETURNED);
3153 /* Fail for TLS only if we required a certificate */
3154 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3155 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3156 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3157 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3158 al = SSL_AD_HANDSHAKE_FAILURE;
3161 /* No client certificate so digest cached records */
3162 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3167 i = ssl_verify_cert_chain(s, sk);
3169 al = ssl_verify_alarm_type(s->verify_result);
3170 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3171 SSL_R_CERTIFICATE_VERIFY_FAILED);
3175 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3176 al = SSL_AD_HANDSHAKE_FAILURE;
3179 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3181 al = SSL3_AD_HANDSHAKE_FAILURE;
3182 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3183 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3186 EVP_PKEY_free(pkey);
3189 X509_free(s->session->peer);
3190 s->session->peer = sk_X509_shift(sk);
3191 s->session->verify_result = s->verify_result;
3193 sk_X509_pop_free(s->session->peer_chain, X509_free);
3194 s->session->peer_chain = sk;
3196 * Inconsistency alert: cert_chain does *not* include the peer's own
3197 * certificate, while we do include it in s3_clnt.c
3200 ret = MSG_PROCESS_CONTINUE_READING;
3204 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3205 ossl_statem_set_error(s);
3208 sk_X509_pop_free(sk, X509_free);
3212 int tls_construct_server_certificate(SSL *s)
3216 cpk = ssl_get_server_send_pkey(s);
3218 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3219 ossl_statem_set_error(s);
3223 if (!ssl3_output_cert_chain(s, cpk)) {
3224 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3225 ossl_statem_set_error(s);
3232 int tls_construct_new_session_ticket(SSL *s)
3234 unsigned char *senc = NULL;
3237 unsigned char *p, *macstart;
3238 const unsigned char *const_p;
3239 int len, slen_full, slen;
3242 SSL_CTX *tctx = s->initial_ctx;
3243 unsigned char iv[EVP_MAX_IV_LENGTH];
3244 unsigned char key_name[16];
3246 /* get session encoding length */
3247 slen_full = i2d_SSL_SESSION(s->session, NULL);
3249 * Some length values are 16 bits, so forget it if session is too
3252 if (slen_full == 0 || slen_full > 0xFF00) {
3253 ossl_statem_set_error(s);
3256 senc = OPENSSL_malloc(slen_full);
3258 ossl_statem_set_error(s);
3262 EVP_CIPHER_CTX_init(&ctx);
3263 HMAC_CTX_init(&hctx);
3266 if (!i2d_SSL_SESSION(s->session, &p))
3270 * create a fresh copy (not shared with other threads) to clean up
3273 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3276 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3278 slen = i2d_SSL_SESSION(sess, NULL);
3279 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3280 SSL_SESSION_free(sess);
3284 if (!i2d_SSL_SESSION(sess, &p)) {
3285 SSL_SESSION_free(sess);
3288 SSL_SESSION_free(sess);
3291 * Grow buffer if need be: the length calculation is as
3292 * follows handshake_header_length +
3293 * 4 (ticket lifetime hint) + 2 (ticket length) +
3294 * 16 (key name) + max_iv_len (iv length) +
3295 * session_length + max_enc_block_size (max encrypted session
3296 * length) + max_md_size (HMAC).
3298 if (!BUF_MEM_grow(s->init_buf,
3299 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3300 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3303 p = ssl_handshake_start(s);
3305 * Initialize HMAC and cipher contexts. If callback present it does
3306 * all the work otherwise use generated values from parent ctx.
3308 if (tctx->tlsext_ticket_key_cb) {
3309 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3313 if (RAND_bytes(iv, 16) <= 0)
3315 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3316 tctx->tlsext_tick_aes_key, iv))
3318 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3319 EVP_sha256(), NULL))
3321 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3325 * Ticket lifetime hint (advisory only): We leave this unspecified
3326 * for resumed session (for simplicity), and guess that tickets for
3327 * new sessions will live as long as their sessions.
3329 l2n(s->hit ? 0 : s->session->timeout, p);
3331 /* Skip ticket length for now */
3333 /* Output key name */
3335 memcpy(p, key_name, 16);
3338 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3339 p += EVP_CIPHER_CTX_iv_length(&ctx);
3340 /* Encrypt session data */
3341 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3344 if (!EVP_EncryptFinal(&ctx, p, &len))
3348 if (!HMAC_Update(&hctx, macstart, p - macstart))
3350 if (!HMAC_Final(&hctx, p, &hlen))
3353 EVP_CIPHER_CTX_cleanup(&ctx);
3354 HMAC_CTX_cleanup(&hctx);
3357 /* Now write out lengths: p points to end of data written */
3359 len = p - ssl_handshake_start(s);
3360 /* Skip ticket lifetime hint */
3361 p = ssl_handshake_start(s) + 4;
3363 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3370 EVP_CIPHER_CTX_cleanup(&ctx);
3371 HMAC_CTX_cleanup(&hctx);
3372 ossl_statem_set_error(s);
3376 int tls_construct_cert_status(SSL *s)
3380 * Grow buffer if need be: the length calculation is as
3381 * follows 1 (message type) + 3 (message length) +
3382 * 1 (ocsp response type) + 3 (ocsp response length)
3385 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3386 ossl_statem_set_error(s);
3390 p = (unsigned char *)s->init_buf->data;
3393 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3394 /* message length */
3395 l2n3(s->tlsext_ocsp_resplen + 4, p);
3397 *(p++) = s->tlsext_status_type;
3398 /* length of OCSP response */
3399 l2n3(s->tlsext_ocsp_resplen, p);
3400 /* actual response */
3401 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3402 /* number of bytes to write */
3403 s->init_num = 8 + s->tlsext_ocsp_resplen;
3409 #ifndef OPENSSL_NO_NEXTPROTONEG
3411 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3412 * It sets the next_proto member in s if found
3414 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3416 PACKET next_proto, padding;
3417 size_t next_proto_len;
3420 * The payload looks like:
3422 * uint8 proto[proto_len];
3423 * uint8 padding_len;
3424 * uint8 padding[padding_len];
3426 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3427 || !PACKET_get_length_prefixed_1(pkt, &padding)
3428 || PACKET_remaining(pkt) > 0) {
3429 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3433 if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated,
3435 s->next_proto_negotiated_len = 0;
3439 s->next_proto_negotiated_len = (unsigned char)next_proto_len;
3441 return MSG_PROCESS_CONTINUE_READING;
3443 ossl_statem_set_error(s);
3444 return MSG_PROCESS_ERROR;
3448 #define SSLV2_CIPHER_LEN 3
3450 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
3451 PACKET *cipher_suites,
3452 STACK_OF(SSL_CIPHER) **skp,
3453 int sslv2format, int *al
3456 const SSL_CIPHER *c;
3457 STACK_OF(SSL_CIPHER) *sk;
3459 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
3460 unsigned char cipher[SSLV2_CIPHER_LEN];
3462 s->s3->send_connection_binding = 0;
3464 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
3466 if (PACKET_remaining(cipher_suites) == 0) {
3467 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
3468 *al = SSL_AD_ILLEGAL_PARAMETER;
3472 if (PACKET_remaining(cipher_suites) % n != 0) {
3473 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3474 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3475 *al = SSL_AD_DECODE_ERROR;
3479 if ((skp == NULL) || (*skp == NULL)) {
3480 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3482 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3483 *al = SSL_AD_INTERNAL_ERROR;
3488 sk_SSL_CIPHER_zero(sk);
3491 if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
3492 &s->s3->tmp.ciphers_rawlen)) {
3493 *al = SSL_AD_INTERNAL_ERROR;
3497 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
3499 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
3500 * first byte set to zero, while true SSLv2 ciphers have a non-zero
3501 * first byte. We don't support any true SSLv2 ciphers, so skip them.
3503 if (sslv2format && cipher[0] != '\0')
3506 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3507 if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3508 (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3509 /* SCSV fatal if renegotiating */
3510 if (s->renegotiate) {
3511 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3512 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3513 *al = SSL_AD_HANDSHAKE_FAILURE;
3516 s->s3->send_connection_binding = 1;
3517 #ifdef OPENSSL_RI_DEBUG
3518 fprintf(stderr, "SCSV received by server\n");
3523 /* Check for TLS_FALLBACK_SCSV */
3524 if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3525 (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3527 * The SCSV indicates that the client previously tried a higher
3528 * version. Fail if the current version is an unexpected
3531 if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
3532 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3533 SSL_R_INAPPROPRIATE_FALLBACK);
3534 *al = SSL_AD_INAPPROPRIATE_FALLBACK;
3540 /* For SSLv2-compat, ignore leading 0-byte. */
3541 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher);
3543 if (!sk_SSL_CIPHER_push(sk, c)) {
3544 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3545 *al = SSL_AD_INTERNAL_ERROR;
3550 if (PACKET_remaining(cipher_suites) > 0) {
3551 *al = SSL_AD_INTERNAL_ERROR;
3552 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);
3560 if ((skp == NULL) || (*skp == NULL))
3561 sk_SSL_CIPHER_free(sk);