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 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 inline 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 inline 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_mkey & SSL_PSK)) {
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 enum WRITE_TRAN 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 enum WORK_STATE server_pre_work(SSL *s, enum 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 enum WORK_STATE server_post_work(SSL *s, enum 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 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
639 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 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 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 enum MSG_PROCESS_RETURN 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 enum WORK_STATE server_post_process_message(SSL *s, enum 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 statem_set_sctp_read_sock(s, 1);
874 ossl_ossl_statem_set_sctp_read_sock(s, 0);
877 return WORK_FINISHED_CONTINUE;
880 case TLS_ST_SR_FINISHED:
882 return tls_finish_handshake(s, wst);
884 return WORK_FINISHED_STOP;
889 /* Shouldn't happen */
893 #ifndef OPENSSL_NO_SRP
894 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
896 int ret = SSL_ERROR_NONE;
898 *al = SSL_AD_UNRECOGNIZED_NAME;
900 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
901 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
902 if (s->srp_ctx.login == NULL) {
904 * RFC 5054 says SHOULD reject, we do so if There is no srp
908 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
910 ret = SSL_srp_server_param_with_username(s, al);
917 int tls_construct_hello_request(SSL *s)
919 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
920 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
921 ossl_statem_set_error(s);
928 unsigned int dtls_raw_hello_verify_request(unsigned char *buf,
929 unsigned char *cookie,
930 unsigned char cookie_len)
932 unsigned int msg_len;
936 /* Always use DTLS 1.0 version: see RFC 6347 */
937 *(p++) = DTLS1_VERSION >> 8;
938 *(p++) = DTLS1_VERSION & 0xFF;
940 *(p++) = (unsigned char)cookie_len;
941 memcpy(p, cookie, cookie_len);
948 int dtls_construct_hello_verify_request(SSL *s)
953 buf = (unsigned char *)s->init_buf->data;
955 if (s->ctx->app_gen_cookie_cb == NULL ||
956 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
957 &(s->d1->cookie_len)) == 0 ||
958 s->d1->cookie_len > 255) {
959 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
960 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
961 ossl_statem_set_error(s);
965 len = dtls_raw_hello_verify_request(&buf[DTLS1_HM_HEADER_LENGTH],
966 s->d1->cookie, s->d1->cookie_len);
968 dtls1_set_message_header(s, buf, DTLS1_MT_HELLO_VERIFY_REQUEST, len, 0,
970 len += DTLS1_HM_HEADER_LENGTH;
972 /* number of bytes to write */
979 enum MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
981 int i, al = SSL_AD_INTERNAL_ERROR;
982 unsigned int j, complen = 0;
985 #ifndef OPENSSL_NO_COMP
986 SSL_COMP *comp = NULL;
988 STACK_OF(SSL_CIPHER) *ciphers = NULL;
990 /* |cookie| will only be initialized for DTLS. */
991 PACKET session_id, cipher_suites, compression, extensions, cookie;
994 is_v2_record = RECORD_LAYER_is_sslv2_record(&s->rlayer);
996 PACKET_null_init(&cookie);
997 /* First lets get s->client_version set correctly */
999 unsigned int version;
1002 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1003 * header is sent directly on the wire, not wrapped as a TLS
1004 * record. Our record layer just processes the message length and passes
1005 * the rest right through. Its format is:
1007 * 0-1 msg_length - decoded by the record layer
1008 * 2 msg_type - s->init_msg points here
1010 * 5-6 cipher_spec_length
1011 * 7-8 session_id_length
1012 * 9-10 challenge_length
1016 if (!PACKET_get_1(pkt, &mt)
1017 || mt != SSL2_MT_CLIENT_HELLO) {
1019 * Should never happen. We should have tested this in the record
1020 * layer in order to have determined that this is a SSLv2 record
1021 * in the first place
1023 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1027 if (!PACKET_get_net_2(pkt, &version)) {
1028 /* No protocol version supplied! */
1029 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1032 if (version == 0x0002) {
1033 /* This is real SSLv2. We don't support it. */
1034 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1036 } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {
1038 s->client_version = version;
1040 /* No idea what protocol this is */
1041 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1046 * use version from inside client hello, not from record header (may
1047 * differ: see RFC 2246, Appendix E, second paragraph)
1049 if(!PACKET_get_net_2(pkt, (unsigned int *)&s->client_version)) {
1050 al = SSL_AD_DECODE_ERROR;
1051 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1056 /* Do SSL/TLS version negotiation if applicable */
1057 if (!SSL_IS_DTLS(s)) {
1058 if (s->version != TLS_ANY_VERSION) {
1059 if (s->client_version >= s->version) {
1062 } else if (s->client_version >= SSL3_VERSION) {
1063 switch(s->client_version) {
1065 case TLS1_2_VERSION:
1066 if(!(s->options & SSL_OP_NO_TLSv1_2)) {
1067 s->version = TLS1_2_VERSION;
1068 s->method = TLSv1_2_server_method();
1072 /* Deliberately fall through */
1073 case TLS1_1_VERSION:
1074 if(!(s->options & SSL_OP_NO_TLSv1_1)) {
1075 s->version = TLS1_1_VERSION;
1076 s->method = TLSv1_1_server_method();
1080 /* Deliberately fall through */
1082 if(!(s->options & SSL_OP_NO_TLSv1)) {
1083 s->version = TLS1_VERSION;
1084 s->method = TLSv1_server_method();
1088 /* Deliberately fall through */
1090 #ifndef OPENSSL_NO_SSL3
1091 if(!(s->options & SSL_OP_NO_SSLv3)) {
1092 s->version = SSL3_VERSION;
1093 s->method = SSLv3_server_method();
1102 } else if (s->client_version <= s->version
1103 || s->method->version == DTLS_ANY_VERSION) {
1105 * For DTLS we just check versions are potentially compatible. Version
1106 * negotiation comes later.
1112 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1113 if ((!s->enc_write_ctx && !s->write_hash)) {
1115 * similar to ssl3_get_record, send alert using remote version
1118 s->version = s->client_version;
1120 al = SSL_AD_PROTOCOL_VERSION;
1124 /* Parse the message and load client random. */
1127 * Handle an SSLv2 backwards compatible ClientHello
1128 * Note, this is only for SSLv3+ using the backward compatible format.
1129 * Real SSLv2 is not supported, and is rejected above.
1131 unsigned int cipher_len, session_id_len, challenge_len;
1134 if (!PACKET_get_net_2(pkt, &cipher_len)
1135 || !PACKET_get_net_2(pkt, &session_id_len)
1136 || !PACKET_get_net_2(pkt, &challenge_len)) {
1137 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1138 SSL_R_RECORD_LENGTH_MISMATCH);
1139 al = SSL_AD_DECODE_ERROR;
1143 if (!PACKET_get_sub_packet(pkt, &cipher_suites, cipher_len)
1144 || !PACKET_get_sub_packet(pkt, &session_id, session_id_len)
1145 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1146 /* No extensions. */
1147 || PACKET_remaining(pkt) != 0) {
1148 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1149 SSL_R_RECORD_LENGTH_MISMATCH);
1150 al = SSL_AD_DECODE_ERROR;
1154 /* Load the client random */
1155 challenge_len = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE :
1157 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1158 if (!PACKET_copy_bytes(&challenge,
1159 s->s3->client_random + SSL3_RANDOM_SIZE -
1160 challenge_len, challenge_len)) {
1161 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1162 al = SSL_AD_INTERNAL_ERROR;
1166 PACKET_null_init(&compression);
1167 PACKET_null_init(&extensions);
1169 /* Regular ClientHello. */
1170 if (!PACKET_copy_bytes(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
1171 || !PACKET_get_length_prefixed_1(pkt, &session_id)) {
1172 al = SSL_AD_DECODE_ERROR;
1173 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1177 if (SSL_IS_DTLS(s)) {
1178 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1179 al = SSL_AD_DECODE_ERROR;
1180 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1184 * If we require cookies and this ClientHello doesn't contain one,
1185 * just return since we do not want to allocate any memory yet.
1186 * So check cookie length...
1188 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1189 if (PACKET_remaining(&cookie) == 0)
1194 if (!PACKET_get_length_prefixed_2(pkt, &cipher_suites)
1195 || !PACKET_get_length_prefixed_1(pkt, &compression)) {
1196 al = SSL_AD_DECODE_ERROR;
1197 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1200 /* Could be empty. */
1207 * We don't allow resumption in a backwards compatible ClientHello.
1208 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1210 * Versions before 0.9.7 always allow clients to resume sessions in
1211 * renegotiation. 0.9.7 and later allow this by default, but optionally
1212 * ignore resumption requests with flag
1213 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1214 * than a change to default behavior so that applications relying on
1215 * this for security won't even compile against older library versions).
1216 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1217 * request renegotiation but not a new session (s->new_session remains
1218 * unset): for servers, this essentially just means that the
1219 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1224 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1225 if (!ssl_get_new_session(s, 1))
1228 i = ssl_get_prev_session(s, &extensions, &session_id);
1230 * Only resume if the session's version matches the negotiated
1232 * RFC 5246 does not provide much useful advice on resumption
1233 * with a different protocol version. It doesn't forbid it but
1234 * the sanity of such behaviour would be questionable.
1235 * In practice, clients do not accept a version mismatch and
1236 * will abort the handshake with an error.
1238 if (i == 1 && s->version == s->session->ssl_version) {
1239 /* previous session */
1241 } else if (i == -1) {
1245 if (!ssl_get_new_session(s, 1))
1250 if (SSL_IS_DTLS(s)) {
1251 /* Empty cookie was already handled above by returning early. */
1252 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1253 if (s->ctx->app_verify_cookie_cb != NULL) {
1254 if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookie),
1255 PACKET_remaining(&cookie)) == 0) {
1256 al = SSL_AD_HANDSHAKE_FAILURE;
1257 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1258 SSL_R_COOKIE_MISMATCH);
1260 /* else cookie verification succeeded */
1262 /* default verification */
1263 } else if (!PACKET_equal(&cookie, s->d1->cookie,
1264 s->d1->cookie_len)) {
1265 al = SSL_AD_HANDSHAKE_FAILURE;
1266 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1269 s->d1->cookie_verified = 1;
1271 if (s->method->version == DTLS_ANY_VERSION) {
1272 /* Select version to use */
1273 if (s->client_version <= DTLS1_2_VERSION &&
1274 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1275 s->version = DTLS1_2_VERSION;
1276 s->method = DTLSv1_2_server_method();
1277 } else if (tls1_suiteb(s)) {
1278 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1279 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1280 s->version = s->client_version;
1281 al = SSL_AD_PROTOCOL_VERSION;
1283 } else if (s->client_version <= DTLS1_VERSION &&
1284 !(s->options & SSL_OP_NO_DTLSv1)) {
1285 s->version = DTLS1_VERSION;
1286 s->method = DTLSv1_server_method();
1288 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1289 SSL_R_WRONG_VERSION_NUMBER);
1290 s->version = s->client_version;
1291 al = SSL_AD_PROTOCOL_VERSION;
1294 s->session->ssl_version = s->version;
1298 if (ssl_bytes_to_cipher_list(s, &cipher_suites, &(ciphers),
1299 is_v2_record, &al) == NULL) {
1303 /* If it is a hit, check that the cipher is in the list */
1306 id = s->session->cipher->id;
1309 fprintf(stderr, "client sent %d ciphers\n",
1310 sk_SSL_CIPHER_num(ciphers));
1312 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1313 c = sk_SSL_CIPHER_value(ciphers, i);
1315 fprintf(stderr, "client [%2d of %2d]:%s\n",
1316 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1325 * we need to have the cipher in the cipher list if we are asked
1328 al = SSL_AD_ILLEGAL_PARAMETER;
1329 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1330 SSL_R_REQUIRED_CIPHER_MISSING);
1335 complen = PACKET_remaining(&compression);
1336 for (j = 0; j < complen; j++) {
1337 if (PACKET_data(&compression)[j] == 0)
1343 al = SSL_AD_DECODE_ERROR;
1344 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1348 /* TLS extensions */
1349 if (s->version >= SSL3_VERSION) {
1350 if (!ssl_parse_clienthello_tlsext(s, &extensions)) {
1351 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1357 * Check if we want to use external pre-shared secret for this handshake
1358 * for not reused session only. We need to generate server_random before
1359 * calling tls_session_secret_cb in order to allow SessionTicket
1360 * processing to use it in key derivation.
1364 pos = s->s3->server_random;
1365 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1370 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1371 SSL_CIPHER *pref_cipher = NULL;
1373 s->session->master_key_length = sizeof(s->session->master_key);
1374 if (s->tls_session_secret_cb(s, s->session->master_key,
1375 &s->session->master_key_length, ciphers,
1377 s->tls_session_secret_cb_arg)) {
1379 s->session->ciphers = ciphers;
1380 s->session->verify_result = X509_V_OK;
1384 /* check if some cipher was preferred by call back */
1386 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1391 if (pref_cipher == NULL) {
1392 al = SSL_AD_HANDSHAKE_FAILURE;
1393 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1397 s->session->cipher = pref_cipher;
1398 sk_SSL_CIPHER_free(s->cipher_list);
1399 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1400 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1401 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1406 * Worst case, we will use the NULL compression, but if we have other
1407 * options, we will now look for them. We have complen-1 compression
1408 * algorithms from the client, starting at q.
1410 s->s3->tmp.new_compression = NULL;
1411 #ifndef OPENSSL_NO_COMP
1412 /* This only happens if we have a cache hit */
1413 if (s->session->compress_meth != 0) {
1414 int m, comp_id = s->session->compress_meth;
1416 /* Perform sanity checks on resumed compression algorithm */
1417 /* Can't disable compression */
1418 if (!ssl_allow_compression(s)) {
1419 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1420 SSL_R_INCONSISTENT_COMPRESSION);
1423 /* Look for resumed compression method */
1424 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1425 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1426 if (comp_id == comp->id) {
1427 s->s3->tmp.new_compression = comp;
1431 if (s->s3->tmp.new_compression == NULL) {
1432 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1433 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1436 /* Look for resumed method in compression list */
1437 for (k = 0; k < complen; k++) {
1438 if (PACKET_data(&compression)[k] == comp_id)
1442 al = SSL_AD_ILLEGAL_PARAMETER;
1443 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1444 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1449 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1450 /* See if we have a match */
1451 int m, nn, v, done = 0;
1454 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1455 for (m = 0; m < nn; m++) {
1456 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1458 for (o = 0; o < complen; o++) {
1459 if (v == PACKET_data(&compression)[o]) {
1468 s->s3->tmp.new_compression = comp;
1474 * If compression is disabled we'd better not try to resume a session
1475 * using compression.
1477 if (s->session->compress_meth != 0) {
1478 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1484 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1488 #ifdef OPENSSL_NO_COMP
1489 s->session->compress_meth = 0;
1491 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1493 sk_SSL_CIPHER_free(s->session->ciphers);
1494 s->session->ciphers = ciphers;
1495 if (ciphers == NULL) {
1496 al = SSL_AD_INTERNAL_ERROR;
1497 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1501 if (!tls1_set_server_sigalgs(s)) {
1502 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1507 sk_SSL_CIPHER_free(ciphers);
1508 return MSG_PROCESS_CONTINUE_PROCESSING;
1510 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1512 ossl_statem_set_error(s);
1514 sk_SSL_CIPHER_free(ciphers);
1515 return MSG_PROCESS_ERROR;
1519 enum WORK_STATE tls_post_process_client_hello(SSL *s, enum WORK_STATE wst)
1524 if (wst == WORK_MORE_A) {
1526 /* Let cert callback update server certificates if required */
1527 if (s->cert->cert_cb) {
1528 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1530 al = SSL_AD_INTERNAL_ERROR;
1531 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1535 s->rwstate = SSL_X509_LOOKUP;
1538 s->rwstate = SSL_NOTHING;
1540 cipher = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1542 if (cipher == NULL) {
1543 al = SSL_AD_HANDSHAKE_FAILURE;
1544 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1547 s->s3->tmp.new_cipher = cipher;
1548 /* check whether we should disable session resumption */
1549 if (s->not_resumable_session_cb != NULL)
1550 s->session->not_resumable = s->not_resumable_session_cb(s,
1551 ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0));
1552 if (s->session->not_resumable)
1553 /* do not send a session ticket */
1554 s->tlsext_ticket_expected = 0;
1556 /* Session-id reuse */
1557 s->s3->tmp.new_cipher = s->session->cipher;
1560 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1561 if (!ssl3_digest_cached_records(s, 0))
1566 * we now have the following setup.
1568 * cipher_list - our prefered list of ciphers
1569 * ciphers - the clients prefered list of ciphers
1570 * compression - basically ignored right now
1571 * ssl version is set - sslv3
1572 * s->session - The ssl session has been setup.
1573 * s->hit - session reuse flag
1574 * s->s3->tmp.new_cipher- the new cipher to use.
1577 /* Handles TLS extensions that we couldn't check earlier */
1578 if (s->version >= SSL3_VERSION) {
1579 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1580 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1587 #ifndef OPENSSL_NO_SRP
1588 if (wst == WORK_MORE_B) {
1590 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1592 * callback indicates further work to be done
1594 s->rwstate = SSL_X509_LOOKUP;
1597 if (ret != SSL_ERROR_NONE) {
1599 * This is not really an error but the only means to for
1600 * a client to detect whether srp is supported.
1602 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1603 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1604 SSL_R_CLIENTHELLO_TLSEXT);
1611 return WORK_FINISHED_STOP;
1613 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1614 ossl_statem_set_error(s);
1618 int tls_construct_server_hello(SSL *s)
1621 unsigned char *p, *d;
1626 buf = (unsigned char *)s->init_buf->data;
1628 /* Do the message type and length last */
1629 d = p = ssl_handshake_start(s);
1631 *(p++) = s->version >> 8;
1632 *(p++) = s->version & 0xff;
1635 * Random stuff. Filling of the server_random takes place in
1636 * tls_process_client_hello()
1638 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1639 p += SSL3_RANDOM_SIZE;
1642 * There are several cases for the session ID to send
1643 * back in the server hello:
1644 * - For session reuse from the session cache,
1645 * we send back the old session ID.
1646 * - If stateless session reuse (using a session ticket)
1647 * is successful, we send back the client's "session ID"
1648 * (which doesn't actually identify the session).
1649 * - If it is a new session, we send back the new
1651 * - However, if we want the new session to be single-use,
1652 * we send back a 0-length session ID.
1653 * s->hit is non-zero in either case of session reuse,
1654 * so the following won't overwrite an ID that we're supposed
1657 if (s->session->not_resumable ||
1658 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1660 s->session->session_id_length = 0;
1662 sl = s->session->session_id_length;
1663 if (sl > (int)sizeof(s->session->session_id)) {
1664 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1665 ossl_statem_set_error(s);
1669 memcpy(p, s->session->session_id, sl);
1672 /* put the cipher */
1673 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1676 /* put the compression method */
1677 #ifdef OPENSSL_NO_COMP
1680 if (s->s3->tmp.new_compression == NULL)
1683 *(p++) = s->s3->tmp.new_compression->id;
1686 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1687 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1688 ossl_statem_set_error(s);
1692 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1694 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1695 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1696 ossl_statem_set_error(s);
1702 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1703 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1704 ossl_statem_set_error(s);
1711 int tls_construct_server_done(SSL *s)
1713 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1714 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1715 ossl_statem_set_error(s);
1719 if (!s->s3->tmp.cert_request) {
1720 if (!ssl3_digest_cached_records(s, 0)) {
1721 ossl_statem_set_error(s);
1728 int tls_construct_server_key_exchange(SSL *s)
1730 #ifndef OPENSSL_NO_RSA
1734 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1737 #ifndef OPENSSL_NO_DH
1738 DH *dh = NULL, *dhp;
1740 #ifndef OPENSSL_NO_EC
1741 EC_KEY *ecdh = NULL, *ecdhp;
1742 unsigned char *encodedPoint = NULL;
1745 BN_CTX *bn_ctx = NULL;
1748 const EVP_MD *md = NULL;
1749 unsigned char *p, *d;
1759 EVP_MD_CTX_init(&md_ctx);
1761 type = s->s3->tmp.new_cipher->algorithm_mkey;
1766 r[0] = r[1] = r[2] = r[3] = NULL;
1768 #ifndef OPENSSL_NO_PSK
1769 if (type & SSL_PSK) {
1771 * reserve size for record length and PSK identity hint
1774 if (s->cert->psk_identity_hint)
1775 n += strlen(s->cert->psk_identity_hint);
1777 /* Plain PSK or RSAPSK nothing to do */
1778 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
1780 #endif /* !OPENSSL_NO_PSK */
1781 #ifndef OPENSSL_NO_RSA
1782 if (type & SSL_kRSA) {
1783 rsa = cert->rsa_tmp;
1784 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1785 rsa = s->cert->rsa_tmp_cb(s,
1786 SSL_C_IS_EXPORT(s->s3->
1788 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1791 al = SSL_AD_HANDSHAKE_FAILURE;
1792 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1793 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1797 cert->rsa_tmp = rsa;
1800 al = SSL_AD_HANDSHAKE_FAILURE;
1801 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1802 SSL_R_MISSING_TMP_RSA_KEY);
1807 s->s3->tmp.use_rsa_tmp = 1;
1810 #ifndef OPENSSL_NO_DH
1811 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
1812 if (s->cert->dh_tmp_auto) {
1813 dhp = ssl_get_auto_dh(s);
1815 al = SSL_AD_INTERNAL_ERROR;
1816 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1817 ERR_R_INTERNAL_ERROR);
1822 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1823 dhp = s->cert->dh_tmp_cb(s,
1824 SSL_C_IS_EXPORT(s->s3->
1826 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1829 al = SSL_AD_HANDSHAKE_FAILURE;
1830 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1831 SSL_R_MISSING_TMP_DH_KEY);
1834 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1835 DH_security_bits(dhp), 0, dhp)) {
1836 al = SSL_AD_HANDSHAKE_FAILURE;
1837 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1838 SSL_R_DH_KEY_TOO_SMALL);
1841 if (s->s3->tmp.dh != NULL) {
1842 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1843 ERR_R_INTERNAL_ERROR);
1847 if (s->cert->dh_tmp_auto)
1849 else if ((dh = DHparams_dup(dhp)) == NULL) {
1850 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1855 if ((dhp->pub_key == NULL ||
1856 dhp->priv_key == NULL ||
1857 (s->options & SSL_OP_SINGLE_DH_USE))) {
1858 if (!DH_generate_key(dh)) {
1859 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1863 dh->pub_key = BN_dup(dhp->pub_key);
1864 dh->priv_key = BN_dup(dhp->priv_key);
1865 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1866 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1875 #ifndef OPENSSL_NO_EC
1876 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1877 const EC_GROUP *group;
1879 ecdhp = cert->ecdh_tmp;
1880 if (s->cert->ecdh_tmp_auto) {
1881 /* Get NID of appropriate shared curve */
1882 int nid = tls1_shared_curve(s, -2);
1883 if (nid != NID_undef)
1884 ecdhp = EC_KEY_new_by_curve_name(nid);
1885 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1886 ecdhp = s->cert->ecdh_tmp_cb(s,
1887 SSL_C_IS_EXPORT(s->s3->
1889 SSL_C_EXPORT_PKEYLENGTH(s->
1890 s3->tmp.new_cipher));
1892 if (ecdhp == NULL) {
1893 al = SSL_AD_HANDSHAKE_FAILURE;
1894 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1895 SSL_R_MISSING_TMP_ECDH_KEY);
1899 if (s->s3->tmp.ecdh != NULL) {
1900 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1901 ERR_R_INTERNAL_ERROR);
1905 /* Duplicate the ECDH structure. */
1906 if (ecdhp == NULL) {
1907 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1910 if (s->cert->ecdh_tmp_auto)
1912 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1913 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1917 s->s3->tmp.ecdh = ecdh;
1918 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1919 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1920 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1921 if (!EC_KEY_generate_key(ecdh)) {
1922 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1928 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1929 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1930 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1931 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1935 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1936 (EC_GROUP_get_degree(group) > 163)) {
1937 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1938 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1943 * XXX: For now, we only support ephemeral ECDH keys over named
1944 * (not generic) curves. For supported named curves, curve_id is
1948 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1950 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1951 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1956 * Encode the public key. First check the size of encoding and
1957 * allocate memory accordingly.
1959 encodedlen = EC_POINT_point2oct(group,
1960 EC_KEY_get0_public_key(ecdh),
1961 POINT_CONVERSION_UNCOMPRESSED,
1964 encodedPoint = (unsigned char *)
1965 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1966 bn_ctx = BN_CTX_new();
1967 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1968 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1969 ERR_R_MALLOC_FAILURE);
1973 encodedlen = EC_POINT_point2oct(group,
1974 EC_KEY_get0_public_key(ecdh),
1975 POINT_CONVERSION_UNCOMPRESSED,
1976 encodedPoint, encodedlen, bn_ctx);
1978 if (encodedlen == 0) {
1979 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1983 BN_CTX_free(bn_ctx);
1987 * XXX: For now, we only support named (not generic) curves in
1988 * ECDH ephemeral key exchanges. In this situation, we need four
1989 * additional bytes to encode the entire ServerECDHParams
1992 n += 4 + encodedlen;
1995 * We'll generate the serverKeyExchange message explicitly so we
1996 * can set these to NULLs
2003 #endif /* !OPENSSL_NO_EC */
2004 #ifndef OPENSSL_NO_SRP
2005 if (type & SSL_kSRP) {
2006 if ((s->srp_ctx.N == NULL) ||
2007 (s->srp_ctx.g == NULL) ||
2008 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2009 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2010 SSL_R_MISSING_SRP_PARAM);
2013 r[0] = s->srp_ctx.N;
2014 r[1] = s->srp_ctx.g;
2015 r[2] = s->srp_ctx.s;
2016 r[3] = s->srp_ctx.B;
2020 al = SSL_AD_HANDSHAKE_FAILURE;
2021 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2022 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2025 for (i = 0; i < 4 && r[i] != NULL; i++) {
2026 nr[i] = BN_num_bytes(r[i]);
2027 #ifndef OPENSSL_NO_SRP
2028 if ((i == 2) && (type & SSL_kSRP))
2035 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP))
2036 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
2037 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
2039 al = SSL_AD_DECODE_ERROR;
2042 kn = EVP_PKEY_size(pkey);
2048 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
2049 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
2052 d = p = ssl_handshake_start(s);
2054 #ifndef OPENSSL_NO_PSK
2055 if (type & SSL_PSK) {
2056 /* copy PSK identity hint */
2057 if (s->cert->psk_identity_hint) {
2058 s2n(strlen(s->cert->psk_identity_hint), p);
2059 strncpy((char *)p, s->cert->psk_identity_hint,
2060 strlen(s->cert->psk_identity_hint));
2061 p += strlen(s->cert->psk_identity_hint);
2068 for (i = 0; i < 4 && r[i] != NULL; i++) {
2069 #ifndef OPENSSL_NO_SRP
2070 if ((i == 2) && (type & SSL_kSRP)) {
2080 #ifndef OPENSSL_NO_EC
2081 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2083 * XXX: For now, we only support named (not generic) curves. In
2084 * this situation, the serverKeyExchange message has: [1 byte
2085 * CurveType], [2 byte CurveName] [1 byte length of encoded
2086 * point], followed by the actual encoded point itself
2088 *p = NAMED_CURVE_TYPE;
2096 memcpy(p, encodedPoint, encodedlen);
2097 OPENSSL_free(encodedPoint);
2098 encodedPoint = NULL;
2106 * n is the length of the params, they start at &(d[4]) and p
2107 * points to the space at the end.
2109 #ifndef OPENSSL_NO_RSA
2110 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
2113 for (num = 2; num > 0; num--) {
2114 EVP_MD_CTX_set_flags(&md_ctx,
2115 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2116 EVP_DigestInit_ex(&md_ctx, (num == 2)
2117 ? s->ctx->md5 : s->ctx->sha1, NULL);
2118 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2120 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2122 EVP_DigestUpdate(&md_ctx, d, n);
2123 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
2127 if (RSA_sign(NID_md5_sha1, md_buf, j,
2128 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2129 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2137 /* send signature algorithm */
2138 if (SSL_USE_SIGALGS(s)) {
2139 if (!tls12_get_sigandhash(p, pkey, md)) {
2140 /* Should never happen */
2141 al = SSL_AD_INTERNAL_ERROR;
2142 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2143 ERR_R_INTERNAL_ERROR);
2149 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2151 EVP_SignInit_ex(&md_ctx, md, NULL);
2152 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2154 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2156 EVP_SignUpdate(&md_ctx, d, n);
2157 if (!EVP_SignFinal(&md_ctx, &(p[2]),
2158 (unsigned int *)&i, pkey)) {
2159 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2164 if (SSL_USE_SIGALGS(s))
2167 /* Is this error check actually needed? */
2168 al = SSL_AD_HANDSHAKE_FAILURE;
2169 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2170 SSL_R_UNKNOWN_PKEY_TYPE);
2175 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
2176 al = SSL_AD_HANDSHAKE_FAILURE;
2177 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2181 EVP_MD_CTX_cleanup(&md_ctx);
2184 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2186 #ifndef OPENSSL_NO_EC
2187 OPENSSL_free(encodedPoint);
2188 BN_CTX_free(bn_ctx);
2190 EVP_MD_CTX_cleanup(&md_ctx);
2191 ossl_statem_set_error(s);
2195 int tls_construct_certificate_request(SSL *s)
2197 unsigned char *p, *d;
2198 int i, j, nl, off, n;
2199 STACK_OF(X509_NAME) *sk = NULL;
2205 d = p = ssl_handshake_start(s);
2207 /* get the list of acceptable cert types */
2209 n = ssl3_get_req_cert_type(s, p);
2214 if (SSL_USE_SIGALGS(s)) {
2215 const unsigned char *psigs;
2216 unsigned char *etmp = p;
2217 nl = tls12_get_psigalgs(s, &psigs);
2218 /* Skip over length for now */
2220 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2221 /* Now fill in length */
2231 sk = SSL_get_client_CA_list(s);
2234 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2235 name = sk_X509_NAME_value(sk, i);
2236 j = i2d_X509_NAME(name, NULL);
2237 if (!BUF_MEM_grow_clean
2238 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2239 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2243 p = ssl_handshake_start(s) + n;
2245 i2d_X509_NAME(name, &p);
2250 /* else no CA names */
2251 p = ssl_handshake_start(s) + off;
2254 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2255 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2259 s->s3->tmp.cert_request = 1;
2263 ossl_statem_set_error(s);
2267 enum MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
2271 unsigned long alg_k;
2272 #ifndef OPENSSL_NO_RSA
2274 EVP_PKEY *pkey = NULL;
2276 #ifndef OPENSSL_NO_DH
2278 DH *dh_srvr, *dh_clnt = NULL;
2280 #ifndef OPENSSL_NO_EC
2281 EC_KEY *srvr_ecdh = NULL;
2282 EVP_PKEY *clnt_pub_pkey = NULL;
2283 EC_POINT *clnt_ecpoint = NULL;
2284 BN_CTX *bn_ctx = NULL;
2286 PACKET enc_premaster;
2287 unsigned char *data, *rsa_decrypt = NULL;
2289 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2291 #ifndef OPENSSL_NO_PSK
2292 /* For PSK parse and retrieve identity, obtain PSK key */
2293 if (alg_k & SSL_PSK) {
2294 unsigned char psk[PSK_MAX_PSK_LEN];
2296 PACKET psk_identity;
2298 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2299 al = SSL_AD_DECODE_ERROR;
2300 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2303 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2304 al = SSL_AD_DECODE_ERROR;
2305 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2306 SSL_R_DATA_LENGTH_TOO_LONG);
2309 if (s->psk_server_callback == NULL) {
2310 al = SSL_AD_INTERNAL_ERROR;
2311 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2312 SSL_R_PSK_NO_SERVER_CB);
2316 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2317 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2318 al = SSL_AD_INTERNAL_ERROR;
2322 psklen = s->psk_server_callback(s, s->session->psk_identity,
2325 if (psklen > PSK_MAX_PSK_LEN) {
2326 al = SSL_AD_INTERNAL_ERROR;
2327 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2329 } else if (psklen == 0) {
2331 * PSK related to the given identity not found
2333 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2334 SSL_R_PSK_IDENTITY_NOT_FOUND);
2335 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2339 OPENSSL_free(s->s3->tmp.psk);
2340 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2341 OPENSSL_cleanse(psk, psklen);
2343 if (s->s3->tmp.psk == NULL) {
2344 al = SSL_AD_INTERNAL_ERROR;
2345 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2349 s->s3->tmp.psklen = psklen;
2351 if (alg_k & SSL_kPSK) {
2352 /* Identity extracted earlier: should be nothing left */
2353 if (PACKET_remaining(pkt) != 0) {
2354 al = SSL_AD_HANDSHAKE_FAILURE;
2355 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2358 /* PSK handled by ssl_generate_master_secret */
2359 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2360 al = SSL_AD_INTERNAL_ERROR;
2361 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2366 #ifndef OPENSSL_NO_RSA
2367 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2368 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2370 unsigned char decrypt_good, version_good;
2373 /* FIX THIS UP EAY EAY EAY EAY */
2374 if (s->s3->tmp.use_rsa_tmp) {
2375 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2376 rsa = s->cert->rsa_tmp;
2378 * Don't do a callback because rsa_tmp should be sent already
2381 al = SSL_AD_HANDSHAKE_FAILURE;
2382 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2383 SSL_R_MISSING_TMP_RSA_PKEY);
2388 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2389 if ((pkey == NULL) ||
2390 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2391 al = SSL_AD_HANDSHAKE_FAILURE;
2392 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2393 SSL_R_MISSING_RSA_CERTIFICATE);
2396 rsa = pkey->pkey.rsa;
2399 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2400 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2401 enc_premaster = *pkt;
2404 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2405 || PACKET_remaining(pkt) != 0) {
2406 /* Try SSLv3 behaviour for TLS. */
2407 if (s->options & SSL_OP_TLS_D5_BUG) {
2408 enc_premaster = orig;
2410 al = SSL_AD_DECODE_ERROR;
2411 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2412 SSL_R_LENGTH_MISMATCH);
2419 * We want to be sure that the plaintext buffer size makes it safe to
2420 * iterate over the entire size of a premaster secret
2421 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2422 * their ciphertext cannot accommodate a premaster secret anyway.
2424 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2425 al = SSL_AD_INTERNAL_ERROR;
2426 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2427 RSA_R_KEY_SIZE_TOO_SMALL);
2431 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2432 if (rsa_decrypt == NULL) {
2433 al = SSL_AD_INTERNAL_ERROR;
2434 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2439 * We must not leak whether a decryption failure occurs because of
2440 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2441 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2442 * generates a random premaster secret for the case that the decrypt
2443 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2446 if (RAND_bytes(rand_premaster_secret,
2447 sizeof(rand_premaster_secret)) <= 0) {
2451 decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),
2452 PACKET_data(&enc_premaster),
2453 rsa_decrypt, rsa, RSA_PKCS1_PADDING);
2457 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2458 * be 0xff if so and zero otherwise.
2461 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2464 * If the version in the decrypted pre-master secret is correct then
2465 * version_good will be 0xff, otherwise it'll be zero. The
2466 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2467 * (http://eprint.iacr.org/2003/052/) exploits the version number
2468 * check as a "bad version oracle". Thus version checks are done in
2469 * constant time and are treated like any other decryption error.
2472 constant_time_eq_8(rsa_decrypt[0],
2473 (unsigned)(s->client_version >> 8));
2475 constant_time_eq_8(rsa_decrypt[1],
2476 (unsigned)(s->client_version & 0xff));
2479 * The premaster secret must contain the same version number as the
2480 * ClientHello to detect version rollback attacks (strangely, the
2481 * protocol does not offer such protection for DH ciphersuites).
2482 * However, buggy clients exist that send the negotiated protocol
2483 * version instead if the server does not support the requested
2484 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2487 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2488 unsigned char workaround_good;
2490 constant_time_eq_8(rsa_decrypt[0], (unsigned)(s->version >> 8));
2492 constant_time_eq_8(rsa_decrypt[1],
2493 (unsigned)(s->version & 0xff));
2494 version_good |= workaround_good;
2498 * Both decryption and version must be good for decrypt_good to
2499 * remain non-zero (0xff).
2501 decrypt_good &= version_good;
2504 * Now copy rand_premaster_secret over from p using
2505 * decrypt_good_mask. If decryption failed, then p does not
2506 * contain valid plaintext, however, a check above guarantees
2507 * it is still sufficiently large to read from.
2509 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2511 constant_time_select_8(decrypt_good, rsa_decrypt[j],
2512 rand_premaster_secret[j]);
2515 if (!ssl_generate_master_secret(s, rsa_decrypt,
2516 sizeof(rand_premaster_secret), 0)) {
2517 al = SSL_AD_INTERNAL_ERROR;
2518 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2521 OPENSSL_free(rsa_decrypt);
2525 #ifndef OPENSSL_NO_DH
2526 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2528 EVP_PKEY *skey = NULL;
2529 PACKET bookmark = *pkt;
2530 unsigned char shared[(OPENSSL_DH_MAX_MODULUS_BITS + 7) / 8];
2532 if (!PACKET_get_net_2(pkt, &i)) {
2533 if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2534 al = SSL_AD_HANDSHAKE_FAILURE;
2535 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2536 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2541 if (PACKET_remaining(pkt) != i) {
2542 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2543 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2544 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2548 i = PACKET_remaining(pkt);
2551 if (alg_k & SSL_kDHr)
2552 idx = SSL_PKEY_DH_RSA;
2553 else if (alg_k & SSL_kDHd)
2554 idx = SSL_PKEY_DH_DSA;
2556 skey = s->cert->pkeys[idx].privatekey;
2557 if ((skey == NULL) ||
2558 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2559 al = SSL_AD_HANDSHAKE_FAILURE;
2560 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2561 SSL_R_MISSING_RSA_CERTIFICATE);
2564 dh_srvr = skey->pkey.dh;
2565 } else if (s->s3->tmp.dh == NULL) {
2566 al = SSL_AD_HANDSHAKE_FAILURE;
2567 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2568 SSL_R_MISSING_TMP_DH_KEY);
2571 dh_srvr = s->s3->tmp.dh;
2573 if (PACKET_remaining(pkt) == 0L) {
2574 /* Get pubkey from cert */
2575 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2577 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2578 dh_clnt = EVP_PKEY_get1_DH(clkey);
2580 if (dh_clnt == NULL) {
2581 al = SSL_AD_HANDSHAKE_FAILURE;
2582 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2583 SSL_R_MISSING_TMP_DH_KEY);
2586 EVP_PKEY_free(clkey);
2587 pub = dh_clnt->pub_key;
2589 if (!PACKET_get_bytes(pkt, &data, i)) {
2590 /* We already checked we have enough data */
2591 al = SSL_AD_INTERNAL_ERROR;
2592 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2593 ERR_R_INTERNAL_ERROR);
2596 pub = BN_bin2bn(data, i, NULL);
2599 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2603 i = DH_compute_key(shared, pub, dh_srvr);
2606 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2611 DH_free(s->s3->tmp.dh);
2612 s->s3->tmp.dh = NULL;
2618 if (!ssl_generate_master_secret(s, shared, i, 0)) {
2619 al = SSL_AD_INTERNAL_ERROR;
2620 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2624 s->statem.no_cert_verify = 1;
2625 return MSG_PROCESS_CONTINUE_PROCESSING;
2630 #ifndef OPENSSL_NO_EC
2631 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2634 const EC_GROUP *group;
2635 const BIGNUM *priv_key;
2636 unsigned char *shared;
2638 /* initialize structures for server's ECDH key pair */
2639 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2640 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2644 /* Let's get server private key and group information */
2645 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2646 /* use the certificate */
2647 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2650 * use the ephermeral values we saved when generating the
2651 * ServerKeyExchange msg.
2653 tkey = s->s3->tmp.ecdh;
2656 group = EC_KEY_get0_group(tkey);
2657 priv_key = EC_KEY_get0_private_key(tkey);
2659 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2660 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2661 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2665 /* Let's get client's public key */
2666 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2667 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2671 if (PACKET_remaining(pkt) == 0L) {
2672 /* Client Publickey was in Client Certificate */
2674 if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2675 al = SSL_AD_HANDSHAKE_FAILURE;
2676 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2677 SSL_R_MISSING_TMP_ECDH_KEY);
2680 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2681 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2683 * XXX: For now, we do not support client authentication
2684 * using ECDH certificates so this branch (n == 0L) of the
2685 * code is never executed. When that support is added, we
2686 * ought to ensure the key received in the certificate is
2687 * authorized for key agreement. ECDH_compute_key implicitly
2688 * checks that the two ECDH shares are for the same group.
2690 al = SSL_AD_HANDSHAKE_FAILURE;
2691 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2692 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2696 if (EC_POINT_copy(clnt_ecpoint,
2697 EC_KEY_get0_public_key(clnt_pub_pkey->
2699 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2702 s->statem.no_cert_verify = 1;
2705 * Get client's public key from encoded point in the
2706 * ClientKeyExchange message.
2708 if ((bn_ctx = BN_CTX_new()) == NULL) {
2709 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2710 ERR_R_MALLOC_FAILURE);
2714 /* Get encoded point length */
2715 if (!PACKET_get_1(pkt, &i)) {
2716 al = SSL_AD_DECODE_ERROR;
2717 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2718 SSL_R_LENGTH_MISMATCH);
2721 if (!PACKET_get_bytes(pkt, &data, i)
2722 || PACKET_remaining(pkt) != 0) {
2723 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2726 if (EC_POINT_oct2point(group, clnt_ecpoint, data, i, bn_ctx) == 0) {
2727 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2732 /* Compute the shared pre-master secret */
2733 field_size = EC_GROUP_get_degree(group);
2734 if (field_size <= 0) {
2735 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2738 shared = OPENSSL_malloc((field_size + 7) / 8);
2739 if (shared == NULL) {
2740 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2743 i = ECDH_compute_key(shared, (field_size + 7) / 8, clnt_ecpoint,
2746 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2747 OPENSSL_free(shared);
2751 EVP_PKEY_free(clnt_pub_pkey);
2752 EC_POINT_free(clnt_ecpoint);
2753 EC_KEY_free(srvr_ecdh);
2754 BN_CTX_free(bn_ctx);
2755 EC_KEY_free(s->s3->tmp.ecdh);
2756 s->s3->tmp.ecdh = NULL;
2758 if (!ssl_generate_master_secret(s, shared, i, 1)) {
2759 al = SSL_AD_INTERNAL_ERROR;
2760 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2763 return MSG_PROCESS_CONTINUE_PROCESSING;
2766 #ifndef OPENSSL_NO_SRP
2767 if (alg_k & SSL_kSRP) {
2768 if (!PACKET_get_net_2(pkt, &i)
2769 || !PACKET_get_bytes(pkt, &data, i)) {
2770 al = SSL_AD_DECODE_ERROR;
2771 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BAD_SRP_A_LENGTH);
2774 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2775 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2778 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2779 || BN_is_zero(s->srp_ctx.A)) {
2780 al = SSL_AD_ILLEGAL_PARAMETER;
2781 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2782 SSL_R_BAD_SRP_PARAMETERS);
2785 OPENSSL_free(s->session->srp_username);
2786 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2787 if (s->session->srp_username == NULL) {
2788 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2792 if (!srp_generate_server_master_secret(s)) {
2793 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2797 #endif /* OPENSSL_NO_SRP */
2798 if (alg_k & SSL_kGOST) {
2799 EVP_PKEY_CTX *pkey_ctx;
2800 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2801 unsigned char premaster_secret[32], *start;
2802 size_t outlen = 32, inlen;
2803 unsigned long alg_a;
2808 /* Get our certificate private key */
2809 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2810 if (alg_a & SSL_aGOST01)
2811 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2813 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2814 EVP_PKEY_decrypt_init(pkey_ctx);
2816 * If client certificate is present and is of the same type, maybe
2817 * use it for key exchange. Don't mind errors from
2818 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2819 * client certificate for authorization only.
2821 client_pub_pkey = X509_get_pubkey(s->session->peer);
2822 if (client_pub_pkey) {
2823 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2826 /* Decrypt session key */
2827 sess_key_len = PACKET_remaining(pkt);
2828 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2829 al = SSL_AD_INTERNAL_ERROR;
2830 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2833 if (ASN1_get_object ((const unsigned char **)&data, &Tlen, &Ttag,
2834 &Tclass, sess_key_len) != V_ASN1_CONSTRUCTED
2835 || Ttag != V_ASN1_SEQUENCE
2836 || Tclass != V_ASN1_UNIVERSAL) {
2837 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2838 SSL_R_DECRYPTION_FAILED);
2843 if (EVP_PKEY_decrypt
2844 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2845 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2846 SSL_R_DECRYPTION_FAILED);
2849 /* Generate master secret */
2850 if (!ssl_generate_master_secret(s, premaster_secret,
2851 sizeof(premaster_secret), 0)) {
2852 al = SSL_AD_INTERNAL_ERROR;
2853 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2856 /* Check if pubkey from client certificate was used */
2857 if (EVP_PKEY_CTX_ctrl
2858 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2859 s->statem.no_cert_verify = 1;
2861 EVP_PKEY_free(client_pub_pkey);
2862 EVP_PKEY_CTX_free(pkey_ctx);
2863 return MSG_PROCESS_CONTINUE_PROCESSING;
2865 EVP_PKEY_free(client_pub_pkey);
2866 EVP_PKEY_CTX_free(pkey_ctx);
2869 al = SSL_AD_HANDSHAKE_FAILURE;
2870 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2874 return MSG_PROCESS_CONTINUE_PROCESSING;
2876 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2877 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2880 #ifndef OPENSSL_NO_EC
2881 EVP_PKEY_free(clnt_pub_pkey);
2882 EC_POINT_free(clnt_ecpoint);
2883 EC_KEY_free(srvr_ecdh);
2884 BN_CTX_free(bn_ctx);
2885 OPENSSL_free(rsa_decrypt);
2887 #ifndef OPENSSL_NO_PSK
2888 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2889 s->s3->tmp.psk = NULL;
2891 ossl_statem_set_error(s);
2892 return MSG_PROCESS_ERROR;
2895 enum WORK_STATE tls_post_process_client_key_exchange(SSL *s,
2896 enum WORK_STATE wst)
2898 #ifndef OPENSSL_NO_SCTP
2899 if (wst == WORK_MORE_A) {
2900 if (SSL_IS_DTLS(s)) {
2901 unsigned char sctpauthkey[64];
2902 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2904 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2907 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
2908 DTLS1_SCTP_AUTH_LABEL);
2910 if (SSL_export_keying_material(s, sctpauthkey,
2911 sizeof(sctpauthkey), labelbuffer,
2912 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
2913 ossl_statem_set_error(s);
2917 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2918 sizeof(sctpauthkey), sctpauthkey);
2923 if ((wst == WORK_MORE_B)
2925 && BIO_dgram_is_sctp(SSL_get_wbio(s))
2926 /* Are we renegotiating? */
2928 /* Are we going to skip the CertificateVerify? */
2929 && (s->session->peer == NULL || s->statem.no_cert_verify)
2930 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
2931 s->s3->in_read_app_data = 2;
2932 s->rwstate = SSL_READING;
2933 BIO_clear_retry_flags(SSL_get_rbio(s));
2934 BIO_set_retry_read(SSL_get_rbio(s));
2935 statem_set_sctp_read_sock(s, 1);
2938 ossl_statem_set_sctp_read_sock(s, 0);
2942 if (s->statem.no_cert_verify) {
2943 /* No certificate verify so we no longer need the handshake_buffer */
2944 BIO_free(s->s3->handshake_buffer);
2945 return WORK_FINISHED_CONTINUE;
2946 } else if (SSL_USE_SIGALGS(s)) {
2947 if (!s->session->peer) {
2948 /* No peer certificate so we no longer need the handshake_buffer */
2949 BIO_free(s->s3->handshake_buffer);
2950 return WORK_FINISHED_CONTINUE;
2952 if (!s->s3->handshake_buffer) {
2953 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
2954 ERR_R_INTERNAL_ERROR);
2955 ossl_statem_set_error(s);
2959 * For sigalgs freeze the handshake buffer. If we support
2960 * extms we've done this already so this is a no-op
2962 if (!ssl3_digest_cached_records(s, 1)) {
2963 ossl_statem_set_error(s);
2971 * We need to get hashes here so if there is a client cert,
2972 * it can be verified FIXME - digest processing for
2973 * CertificateVerify should be generalized. But it is next
2976 if (!ssl3_digest_cached_records(s, 0)) {
2977 ossl_statem_set_error(s);
2980 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++) {
2981 if (s->s3->handshake_dgst[dgst_num]) {
2984 s->method->ssl3_enc->cert_verify_mac(s,
2993 EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
2994 if (dgst_size < 0) {
2995 ossl_statem_set_error(s);
2998 offset += dgst_size;
3003 return WORK_FINISHED_CONTINUE;
3006 enum MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
3008 EVP_PKEY *pkey = NULL;
3009 unsigned char *sig, *data;
3010 int al, ret = MSG_PROCESS_ERROR;
3014 const EVP_MD *md = NULL;
3016 EVP_MD_CTX_init(&mctx);
3018 peer = s->session->peer;
3019 pkey = X509_get_pubkey(peer);
3020 type = X509_certificate_type(peer, pkey);
3022 if (!(type & EVP_PKT_SIGN)) {
3023 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
3024 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
3025 al = SSL_AD_ILLEGAL_PARAMETER;
3029 /* Check for broken implementations of GOST ciphersuites */
3031 * If key is GOST and n is exactly 64, it is bare signature without
3034 if (PACKET_remaining(pkt) == 64 && pkey->type == NID_id_GostR3410_2001) {
3037 if (SSL_USE_SIGALGS(s)) {
3040 if (!PACKET_get_bytes(pkt, &sig, 2)) {
3041 al = SSL_AD_DECODE_ERROR;
3044 rv = tls12_check_peer_sigalg(&md, s, sig, pkey);
3046 al = SSL_AD_INTERNAL_ERROR;
3048 } else if (rv == 0) {
3049 al = SSL_AD_DECODE_ERROR;
3053 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
3056 if (!PACKET_get_net_2(pkt, &len)) {
3057 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
3058 al = SSL_AD_DECODE_ERROR;
3062 j = EVP_PKEY_size(pkey);
3063 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
3064 || (PACKET_remaining(pkt) == 0)) {
3065 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
3066 al = SSL_AD_DECODE_ERROR;
3069 if (!PACKET_get_bytes(pkt, &data, len)) {
3070 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
3071 al = SSL_AD_DECODE_ERROR;
3075 if (SSL_USE_SIGALGS(s)) {
3078 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3079 if (hdatalen <= 0) {
3080 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3081 al = SSL_AD_INTERNAL_ERROR;
3085 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3088 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3089 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3090 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
3091 al = SSL_AD_INTERNAL_ERROR;
3095 if (EVP_VerifyFinal(&mctx, data, len, pkey) <= 0) {
3096 al = SSL_AD_DECRYPT_ERROR;
3097 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3101 #ifndef OPENSSL_NO_RSA
3102 if (pkey->type == EVP_PKEY_RSA) {
3103 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3104 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, data, len,
3107 al = SSL_AD_DECRYPT_ERROR;
3108 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3112 al = SSL_AD_DECRYPT_ERROR;
3113 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3118 #ifndef OPENSSL_NO_DSA
3119 if (pkey->type == EVP_PKEY_DSA) {
3120 j = DSA_verify(pkey->save_type,
3121 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3122 SHA_DIGEST_LENGTH, data, len, pkey->pkey.dsa);
3125 al = SSL_AD_DECRYPT_ERROR;
3126 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3131 #ifndef OPENSSL_NO_EC
3132 if (pkey->type == EVP_PKEY_EC) {
3133 j = ECDSA_verify(pkey->save_type,
3134 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3135 SHA_DIGEST_LENGTH, data, len, pkey->pkey.ec);
3138 al = SSL_AD_DECRYPT_ERROR;
3139 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3144 if (pkey->type == NID_id_GostR3410_2001) {
3145 unsigned char signature[64];
3147 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3148 EVP_PKEY_verify_init(pctx);
3150 fprintf(stderr, "GOST signature length is %d", len);
3152 for (idx = 0; idx < 64; idx++) {
3153 signature[63 - idx] = data[idx];
3155 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3157 EVP_PKEY_CTX_free(pctx);
3159 al = SSL_AD_DECRYPT_ERROR;
3160 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3164 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3165 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3169 ret = MSG_PROCESS_CONTINUE_PROCESSING;
3172 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3173 ossl_statem_set_error(s);
3175 BIO_free(s->s3->handshake_buffer);
3176 s->s3->handshake_buffer = NULL;
3177 EVP_MD_CTX_cleanup(&mctx);
3178 EVP_PKEY_free(pkey);
3182 enum MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3184 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3186 unsigned long l, llen;
3187 const unsigned char *certstart;
3188 unsigned char *certbytes;
3189 STACK_OF(X509) *sk = NULL;
3192 if ((sk = sk_X509_new_null()) == NULL) {
3193 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3197 if (!PACKET_get_net_3(pkt, &llen)
3198 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3199 || PACKET_remaining(pkt) != 0) {
3200 al = SSL_AD_DECODE_ERROR;
3201 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3205 while (PACKET_remaining(&spkt) > 0) {
3206 if (!PACKET_get_net_3(&spkt, &l)
3207 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3208 al = SSL_AD_DECODE_ERROR;
3209 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3210 SSL_R_CERT_LENGTH_MISMATCH);
3214 certstart = certbytes;
3215 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3217 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3220 if (certbytes != (certstart + l)) {
3221 al = SSL_AD_DECODE_ERROR;
3222 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3223 SSL_R_CERT_LENGTH_MISMATCH);
3226 if (!sk_X509_push(sk, x)) {
3227 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3233 if (sk_X509_num(sk) <= 0) {
3234 /* TLS does not mind 0 certs returned */
3235 if (s->version == SSL3_VERSION) {
3236 al = SSL_AD_HANDSHAKE_FAILURE;
3237 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3238 SSL_R_NO_CERTIFICATES_RETURNED);
3241 /* Fail for TLS only if we required a certificate */
3242 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3243 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3244 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3245 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3246 al = SSL_AD_HANDSHAKE_FAILURE;
3249 /* No client certificate so digest cached records */
3250 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3255 i = ssl_verify_cert_chain(s, sk);
3257 al = ssl_verify_alarm_type(s->verify_result);
3258 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3259 SSL_R_CERTIFICATE_VERIFY_FAILED);
3263 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3264 al = SSL_AD_HANDSHAKE_FAILURE;
3267 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3269 al = SSL3_AD_HANDSHAKE_FAILURE;
3270 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3271 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3274 EVP_PKEY_free(pkey);
3277 X509_free(s->session->peer);
3278 s->session->peer = sk_X509_shift(sk);
3279 s->session->verify_result = s->verify_result;
3281 sk_X509_pop_free(s->session->peer_chain, X509_free);
3282 s->session->peer_chain = sk;
3284 * Inconsistency alert: cert_chain does *not* include the peer's own
3285 * certificate, while we do include it in s3_clnt.c
3288 ret = MSG_PROCESS_CONTINUE_READING;
3292 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3293 ossl_statem_set_error(s);
3296 sk_X509_pop_free(sk, X509_free);
3300 int tls_construct_server_certificate(SSL *s)
3304 cpk = ssl_get_server_send_pkey(s);
3306 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3307 ossl_statem_set_error(s);
3311 if (!ssl3_output_cert_chain(s, cpk)) {
3312 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3313 ossl_statem_set_error(s);
3320 int tls_construct_new_session_ticket(SSL *s)
3322 unsigned char *senc = NULL;
3325 unsigned char *p, *macstart;
3326 const unsigned char *const_p;
3327 int len, slen_full, slen;
3330 SSL_CTX *tctx = s->initial_ctx;
3331 unsigned char iv[EVP_MAX_IV_LENGTH];
3332 unsigned char key_name[16];
3334 /* get session encoding length */
3335 slen_full = i2d_SSL_SESSION(s->session, NULL);
3337 * Some length values are 16 bits, so forget it if session is too
3340 if (slen_full == 0 || slen_full > 0xFF00) {
3341 ossl_statem_set_error(s);
3344 senc = OPENSSL_malloc(slen_full);
3346 ossl_statem_set_error(s);
3350 EVP_CIPHER_CTX_init(&ctx);
3351 HMAC_CTX_init(&hctx);
3354 if (!i2d_SSL_SESSION(s->session, &p))
3358 * create a fresh copy (not shared with other threads) to clean up
3361 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3364 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3366 slen = i2d_SSL_SESSION(sess, NULL);
3367 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3368 SSL_SESSION_free(sess);
3372 if (!i2d_SSL_SESSION(sess, &p)) {
3373 SSL_SESSION_free(sess);
3376 SSL_SESSION_free(sess);
3379 * Grow buffer if need be: the length calculation is as
3380 * follows handshake_header_length +
3381 * 4 (ticket lifetime hint) + 2 (ticket length) +
3382 * 16 (key name) + max_iv_len (iv length) +
3383 * session_length + max_enc_block_size (max encrypted session
3384 * length) + max_md_size (HMAC).
3386 if (!BUF_MEM_grow(s->init_buf,
3387 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3388 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3391 p = ssl_handshake_start(s);
3393 * Initialize HMAC and cipher contexts. If callback present it does
3394 * all the work otherwise use generated values from parent ctx.
3396 if (tctx->tlsext_ticket_key_cb) {
3397 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3401 if (RAND_bytes(iv, 16) <= 0)
3403 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3404 tctx->tlsext_tick_aes_key, iv))
3406 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3407 EVP_sha256(), NULL))
3409 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3413 * Ticket lifetime hint (advisory only): We leave this unspecified
3414 * for resumed session (for simplicity), and guess that tickets for
3415 * new sessions will live as long as their sessions.
3417 l2n(s->hit ? 0 : s->session->timeout, p);
3419 /* Skip ticket length for now */
3421 /* Output key name */
3423 memcpy(p, key_name, 16);
3426 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3427 p += EVP_CIPHER_CTX_iv_length(&ctx);
3428 /* Encrypt session data */
3429 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3432 if (!EVP_EncryptFinal(&ctx, p, &len))
3436 if (!HMAC_Update(&hctx, macstart, p - macstart))
3438 if (!HMAC_Final(&hctx, p, &hlen))
3441 EVP_CIPHER_CTX_cleanup(&ctx);
3442 HMAC_CTX_cleanup(&hctx);
3445 /* Now write out lengths: p points to end of data written */
3447 len = p - ssl_handshake_start(s);
3448 /* Skip ticket lifetime hint */
3449 p = ssl_handshake_start(s) + 4;
3451 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3458 EVP_CIPHER_CTX_cleanup(&ctx);
3459 HMAC_CTX_cleanup(&hctx);
3460 ossl_statem_set_error(s);
3464 int tls_construct_cert_status(SSL *s)
3468 * Grow buffer if need be: the length calculation is as
3469 * follows 1 (message type) + 3 (message length) +
3470 * 1 (ocsp response type) + 3 (ocsp response length)
3473 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3474 ossl_statem_set_error(s);
3478 p = (unsigned char *)s->init_buf->data;
3481 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3482 /* message length */
3483 l2n3(s->tlsext_ocsp_resplen + 4, p);
3485 *(p++) = s->tlsext_status_type;
3486 /* length of OCSP response */
3487 l2n3(s->tlsext_ocsp_resplen, p);
3488 /* actual response */
3489 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3490 /* number of bytes to write */
3491 s->init_num = 8 + s->tlsext_ocsp_resplen;
3497 #ifndef OPENSSL_NO_NEXTPROTONEG
3499 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3500 * It sets the next_proto member in s if found
3502 enum MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3504 PACKET next_proto, padding;
3505 size_t next_proto_len;
3508 * The payload looks like:
3510 * uint8 proto[proto_len];
3511 * uint8 padding_len;
3512 * uint8 padding[padding_len];
3514 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3515 || !PACKET_get_length_prefixed_1(pkt, &padding)
3516 || PACKET_remaining(pkt) > 0) {
3517 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3521 if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated,
3523 s->next_proto_negotiated_len = 0;
3527 s->next_proto_negotiated_len = (unsigned char)next_proto_len;
3529 return MSG_PROCESS_CONTINUE_READING;
3531 ossl_statem_set_error(s);
3532 return MSG_PROCESS_ERROR;
3536 #define SSLV2_CIPHER_LEN 3
3538 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
3539 PACKET *cipher_suites,
3540 STACK_OF(SSL_CIPHER) **skp,
3541 int sslv2format, int *al
3544 const SSL_CIPHER *c;
3545 STACK_OF(SSL_CIPHER) *sk;
3547 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
3548 unsigned char cipher[SSLV2_CIPHER_LEN];
3550 s->s3->send_connection_binding = 0;
3552 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
3554 if (PACKET_remaining(cipher_suites) == 0) {
3555 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
3556 *al = SSL_AD_ILLEGAL_PARAMETER;
3560 if (PACKET_remaining(cipher_suites) % n != 0) {
3561 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3562 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3563 *al = SSL_AD_DECODE_ERROR;
3567 if ((skp == NULL) || (*skp == NULL)) {
3568 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3570 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3571 *al = SSL_AD_INTERNAL_ERROR;
3576 sk_SSL_CIPHER_zero(sk);
3579 if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
3580 &s->s3->tmp.ciphers_rawlen)) {
3581 *al = SSL_AD_INTERNAL_ERROR;
3585 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
3587 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
3588 * first byte set to zero, while true SSLv2 ciphers have a non-zero
3589 * first byte. We don't support any true SSLv2 ciphers, so skip them.
3591 if (sslv2format && cipher[0] != '\0')
3594 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3595 if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3596 (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3597 /* SCSV fatal if renegotiating */
3598 if (s->renegotiate) {
3599 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3600 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3601 *al = SSL_AD_HANDSHAKE_FAILURE;
3604 s->s3->send_connection_binding = 1;
3605 #ifdef OPENSSL_RI_DEBUG
3606 fprintf(stderr, "SCSV received by server\n");
3611 /* Check for TLS_FALLBACK_SCSV */
3612 if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3613 (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3615 * The SCSV indicates that the client previously tried a higher
3616 * version. Fail if the current version is an unexpected
3619 if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
3620 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3621 SSL_R_INAPPROPRIATE_FALLBACK);
3622 *al = SSL_AD_INAPPROPRIATE_FALLBACK;
3628 /* For SSLv2-compat, ignore leading 0-byte. */
3629 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher);
3631 if (!sk_SSL_CIPHER_push(sk, c)) {
3632 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3633 *al = SSL_AD_INTERNAL_ERROR;
3638 if (PACKET_remaining(cipher_suites) > 0) {
3639 *al = SSL_AD_INTERNAL_ERROR;
3640 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);
3648 if ((skp == NULL) || (*skp == NULL))
3649 sk_SSL_CIPHER_free(sk);