1 /* ssl/statem/statem_srvr.c */
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 or fortezza 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
|SSL_kECDHE
)
327 * PSK: send ServerKeyExchange if PSK identity hint if
330 #ifndef OPENSSL_NO_PSK
331 /* Only send SKE if we have identity hint for plain PSK */
332 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
333 && s
->cert
->psk_identity_hint
)
334 /* For other PSK always send SKE */
335 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
337 #ifndef OPENSSL_NO_SRP
338 /* SRP: send ServerKeyExchange */
339 || (alg_k
& SSL_kSRP
)
349 * Should we send a CertificateRequest message?
351 * Valid return values are:
355 static int send_certificate_request(SSL
*s
)
358 /* don't request cert unless asked for it: */
359 s
->verify_mode
& SSL_VERIFY_PEER
361 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
362 * during re-negotiation:
364 && ((s
->session
->peer
== NULL
) ||
365 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
367 * never request cert in anonymous ciphersuites (see
368 * section "Certificate request" in SSL 3 drafts and in
371 && (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
373 * ... except when the application insists on
374 * verification (against the specs, but s3_clnt.c accepts
377 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
378 /* don't request certificate for SRP auth */
379 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
381 * With normal PSK Certificates and Certificate Requests
384 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
392 * server_write_transition() works out what handshake state to move to next
393 * when the server is writing messages to be sent to the client.
395 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
397 OSSL_STATEM
*st
= &s
->statem
;
399 switch(st
->hand_state
) {
401 /* Just go straight to trying to read from the client */;
402 return WRITE_TRAN_FINISHED
;
405 /* We must be trying to renegotiate */
406 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
407 return WRITE_TRAN_CONTINUE
;
409 case TLS_ST_SW_HELLO_REQ
:
410 st
->hand_state
= TLS_ST_OK
;
411 ossl_statem_set_in_init(s
, 0);
412 return WRITE_TRAN_CONTINUE
;
414 case TLS_ST_SR_CLNT_HELLO
:
415 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
416 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
))
417 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
419 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
420 return WRITE_TRAN_CONTINUE
;
422 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
423 return WRITE_TRAN_FINISHED
;
425 case TLS_ST_SW_SRVR_HELLO
:
427 if (s
->tlsext_ticket_expected
)
428 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
430 st
->hand_state
= TLS_ST_SW_CHANGE
;
432 /* Check if it is anon DH or anon ECDH, */
433 /* normal PSK or SRP */
434 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
&
435 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
436 st
->hand_state
= TLS_ST_SW_CERT
;
437 } else if (send_server_key_exchange(s
)) {
438 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
439 } else if (send_certificate_request(s
)) {
440 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
442 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
445 return WRITE_TRAN_CONTINUE
;
448 if (s
->tlsext_status_expected
) {
449 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
450 return WRITE_TRAN_CONTINUE
;
454 case TLS_ST_SW_CERT_STATUS
:
455 if (send_server_key_exchange(s
)) {
456 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
457 return WRITE_TRAN_CONTINUE
;
461 case TLS_ST_SW_KEY_EXCH
:
462 if (send_certificate_request(s
)) {
463 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
464 return WRITE_TRAN_CONTINUE
;
468 case TLS_ST_SW_CERT_REQ
:
469 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
470 return WRITE_TRAN_CONTINUE
;
472 case TLS_ST_SW_SRVR_DONE
:
473 return WRITE_TRAN_FINISHED
;
475 case TLS_ST_SR_FINISHED
:
477 st
->hand_state
= TLS_ST_OK
;
478 ossl_statem_set_in_init(s
, 0);
479 return WRITE_TRAN_CONTINUE
;
480 } else if (s
->tlsext_ticket_expected
) {
481 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
483 st
->hand_state
= TLS_ST_SW_CHANGE
;
485 return WRITE_TRAN_CONTINUE
;
487 case TLS_ST_SW_SESSION_TICKET
:
488 st
->hand_state
= TLS_ST_SW_CHANGE
;
489 return WRITE_TRAN_CONTINUE
;
491 case TLS_ST_SW_CHANGE
:
492 st
->hand_state
= TLS_ST_SW_FINISHED
;
493 return WRITE_TRAN_CONTINUE
;
495 case TLS_ST_SW_FINISHED
:
497 return WRITE_TRAN_FINISHED
;
499 st
->hand_state
= TLS_ST_OK
;
500 ossl_statem_set_in_init(s
, 0);
501 return WRITE_TRAN_CONTINUE
;
504 /* Shouldn't happen */
505 return WRITE_TRAN_ERROR
;
510 * Perform any pre work that needs to be done prior to sending a message from
511 * the server to the client.
513 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
515 OSSL_STATEM
*st
= &s
->statem
;
517 switch(st
->hand_state
) {
518 case TLS_ST_SW_HELLO_REQ
:
521 dtls1_clear_record_buffer(s
);
524 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
526 if (SSL_IS_DTLS(s
)) {
527 dtls1_clear_record_buffer(s
);
528 /* We don't buffer this message so don't use the timer */
533 case TLS_ST_SW_SRVR_HELLO
:
534 if (SSL_IS_DTLS(s
)) {
536 * Messages we write from now on should be bufferred and
537 * retransmitted if necessary, so we need to use the timer now
543 case TLS_ST_SW_SRVR_DONE
:
544 #ifndef OPENSSL_NO_SCTP
545 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
)))
546 return dtls_wait_for_dry(s
);
548 return WORK_FINISHED_CONTINUE
;
550 case TLS_ST_SW_SESSION_TICKET
:
551 if (SSL_IS_DTLS(s
)) {
553 * We're into the last flight. We don't retransmit the last flight
554 * unless we need to, so we don't use the timer
560 case TLS_ST_SW_CHANGE
:
561 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
562 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
563 ossl_statem_set_error(s
);
566 if (SSL_IS_DTLS(s
)) {
568 * We're into the last flight. We don't retransmit the last flight
569 * unless we need to, so we don't use the timer. This might have
570 * already been set to 0 if we sent a NewSessionTicket message,
571 * but we'll set it again here in case we didn't.
575 return WORK_FINISHED_CONTINUE
;
578 return tls_finish_handshake(s
, wst
);
581 /* No pre work to be done */
585 return WORK_FINISHED_CONTINUE
;
589 * Perform any work that needs to be done after sending a message from the
590 * server to the client.
592 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
594 OSSL_STATEM
*st
= &s
->statem
;
598 switch(st
->hand_state
) {
599 case TLS_ST_SW_HELLO_REQ
:
600 if (statem_flush(s
) != 1)
602 ssl3_init_finished_mac(s
);
605 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
606 if (statem_flush(s
) != 1)
608 /* HelloVerifyRequest resets Finished MAC */
609 if (s
->version
!= DTLS1_BAD_VER
)
610 ssl3_init_finished_mac(s
);
612 * The next message should be another ClientHello which we need to
613 * treat like it was the first packet
618 case TLS_ST_SW_SRVR_HELLO
:
619 #ifndef OPENSSL_NO_SCTP
620 if (SSL_IS_DTLS(s
) && s
->hit
) {
621 unsigned char sctpauthkey
[64];
622 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
625 * Add new shared key for SCTP-Auth, will be ignored if no
628 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
629 sizeof(DTLS1_SCTP_AUTH_LABEL
));
631 if (SSL_export_keying_material(s
, sctpauthkey
,
632 sizeof(sctpauthkey
), labelbuffer
,
633 sizeof(labelbuffer
), NULL
, 0, 0) <= 0) {
634 ossl_statem_set_error(s
);
638 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
639 sizeof(sctpauthkey
), sctpauthkey
);
644 case TLS_ST_SW_CHANGE
:
645 #ifndef OPENSSL_NO_SCTP
646 if (SSL_IS_DTLS(s
) && !s
->hit
) {
648 * Change to new shared key of SCTP-Auth, will be ignored if
651 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
655 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
656 SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
657 ossl_statem_set_error(s
);
662 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
665 case TLS_ST_SW_SRVR_DONE
:
666 if (statem_flush(s
) != 1)
670 case TLS_ST_SW_FINISHED
:
671 if (statem_flush(s
) != 1)
673 #ifndef OPENSSL_NO_SCTP
674 if (SSL_IS_DTLS(s
) && s
->hit
) {
676 * Change to new shared key of SCTP-Auth, will be ignored if
679 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
686 /* No post work to be done */
690 return WORK_FINISHED_CONTINUE
;
694 * Construct a message to be sent from the server to the client.
696 * Valid return values are:
700 int ossl_statem_server_construct_message(SSL
*s
)
702 OSSL_STATEM
*st
= &s
->statem
;
704 switch(st
->hand_state
) {
705 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
706 return dtls_construct_hello_verify_request(s
);
708 case TLS_ST_SW_HELLO_REQ
:
709 return tls_construct_hello_request(s
);
711 case TLS_ST_SW_SRVR_HELLO
:
712 return tls_construct_server_hello(s
);
715 return tls_construct_server_certificate(s
);
717 case TLS_ST_SW_KEY_EXCH
:
718 return tls_construct_server_key_exchange(s
);
720 case TLS_ST_SW_CERT_REQ
:
721 return tls_construct_certificate_request(s
);
723 case TLS_ST_SW_SRVR_DONE
:
724 return tls_construct_server_done(s
);
726 case TLS_ST_SW_SESSION_TICKET
:
727 return tls_construct_new_session_ticket(s
);
729 case TLS_ST_SW_CERT_STATUS
:
730 return tls_construct_cert_status(s
);
732 case TLS_ST_SW_CHANGE
:
734 return dtls_construct_change_cipher_spec(s
);
736 return tls_construct_change_cipher_spec(s
);
738 case TLS_ST_SW_FINISHED
:
739 return tls_construct_finished(s
,
741 ssl3_enc
->server_finished_label
,
743 ssl3_enc
->server_finished_label_len
);
746 /* Shouldn't happen */
753 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
754 #define NEXT_PROTO_MAX_LENGTH 514
757 * Returns the maximum allowed length for the current message that we are
758 * reading. Excludes the message header.
760 unsigned long ossl_statem_server_max_message_size(SSL
*s
)
762 OSSL_STATEM
*st
= &s
->statem
;
764 switch(st
->hand_state
) {
765 case TLS_ST_SR_CLNT_HELLO
:
766 return SSL3_RT_MAX_PLAIN_LENGTH
;
769 return s
->max_cert_list
;
771 case TLS_ST_SR_KEY_EXCH
:
772 return CLIENT_KEY_EXCH_MAX_LENGTH
;
774 case TLS_ST_SR_CERT_VRFY
:
775 return SSL3_RT_MAX_PLAIN_LENGTH
;
777 #ifndef OPENSSL_NO_NEXTPROTONEG
778 case TLS_ST_SR_NEXT_PROTO
:
779 return NEXT_PROTO_MAX_LENGTH
;
782 case TLS_ST_SR_CHANGE
:
783 return CCS_MAX_LENGTH
;
785 case TLS_ST_SR_FINISHED
:
786 return FINISHED_MAX_LENGTH
;
789 /* Shouldn't happen */
797 * Process a message that the server has received from the client.
799 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
801 OSSL_STATEM
*st
= &s
->statem
;
803 switch(st
->hand_state
) {
804 case TLS_ST_SR_CLNT_HELLO
:
805 return tls_process_client_hello(s
, pkt
);
808 return tls_process_client_certificate(s
, pkt
);
810 case TLS_ST_SR_KEY_EXCH
:
811 return tls_process_client_key_exchange(s
, pkt
);
813 case TLS_ST_SR_CERT_VRFY
:
814 return tls_process_cert_verify(s
, pkt
);
816 #ifndef OPENSSL_NO_NEXTPROTONEG
817 case TLS_ST_SR_NEXT_PROTO
:
818 return tls_process_next_proto(s
, pkt
);
821 case TLS_ST_SR_CHANGE
:
822 return tls_process_change_cipher_spec(s
, pkt
);
824 case TLS_ST_SR_FINISHED
:
825 return tls_process_finished(s
, pkt
);
828 /* Shouldn't happen */
832 return MSG_PROCESS_ERROR
;
836 * Perform any further processing required following the receipt of a message
839 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
841 OSSL_STATEM
*st
= &s
->statem
;
843 switch(st
->hand_state
) {
844 case TLS_ST_SR_CLNT_HELLO
:
845 return tls_post_process_client_hello(s
, wst
);
847 case TLS_ST_SR_KEY_EXCH
:
848 return tls_post_process_client_key_exchange(s
, wst
);
850 case TLS_ST_SR_CERT_VRFY
:
851 #ifndef OPENSSL_NO_SCTP
852 if ( /* Is this SCTP? */
853 BIO_dgram_is_sctp(SSL_get_wbio(s
))
854 /* Are we renegotiating? */
856 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s
))) {
857 s
->s3
->in_read_app_data
= 2;
858 s
->rwstate
= SSL_READING
;
859 BIO_clear_retry_flags(SSL_get_rbio(s
));
860 BIO_set_retry_read(SSL_get_rbio(s
));
861 ossl_statem_set_sctp_read_sock(s
, 1);
864 ossl_statem_set_sctp_read_sock(s
, 0);
867 return WORK_FINISHED_CONTINUE
;
873 /* Shouldn't happen */
877 #ifndef OPENSSL_NO_SRP
878 static int ssl_check_srp_ext_ClientHello(SSL
*s
, int *al
)
880 int ret
= SSL_ERROR_NONE
;
882 *al
= SSL_AD_UNRECOGNIZED_NAME
;
884 if ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
885 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
886 if (s
->srp_ctx
.login
== NULL
) {
888 * RFC 5054 says SHOULD reject, we do so if There is no srp
892 *al
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
894 ret
= SSL_srp_server_param_with_username(s
, al
);
901 int tls_construct_hello_request(SSL
*s
)
903 if (!ssl_set_handshake_header(s
, SSL3_MT_HELLO_REQUEST
, 0)) {
904 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_REQUEST
, ERR_R_INTERNAL_ERROR
);
905 ossl_statem_set_error(s
);
912 unsigned int dtls_raw_hello_verify_request(unsigned char *buf
,
913 unsigned char *cookie
,
914 unsigned char cookie_len
)
916 unsigned int msg_len
;
920 /* Always use DTLS 1.0 version: see RFC 6347 */
921 *(p
++) = DTLS1_VERSION
>> 8;
922 *(p
++) = DTLS1_VERSION
& 0xFF;
924 *(p
++) = (unsigned char)cookie_len
;
925 memcpy(p
, cookie
, cookie_len
);
932 int dtls_construct_hello_verify_request(SSL
*s
)
937 buf
= (unsigned char *)s
->init_buf
->data
;
939 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
940 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
941 &(s
->d1
->cookie_len
)) == 0 ||
942 s
->d1
->cookie_len
> 255) {
943 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
944 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
945 ossl_statem_set_error(s
);
949 len
= dtls_raw_hello_verify_request(&buf
[DTLS1_HM_HEADER_LENGTH
],
950 s
->d1
->cookie
, s
->d1
->cookie_len
);
952 dtls1_set_message_header(s
, buf
, DTLS1_MT_HELLO_VERIFY_REQUEST
, len
, 0,
954 len
+= DTLS1_HM_HEADER_LENGTH
;
956 /* number of bytes to write */
963 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
965 int i
, al
= SSL_AD_INTERNAL_ERROR
;
966 unsigned int j
, complen
= 0;
969 #ifndef OPENSSL_NO_COMP
970 SSL_COMP
*comp
= NULL
;
972 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
974 /* |cookie| will only be initialized for DTLS. */
975 PACKET session_id
, cipher_suites
, compression
, extensions
, cookie
;
978 is_v2_record
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
980 PACKET_null_init(&cookie
);
981 /* First lets get s->client_version set correctly */
983 unsigned int version
;
986 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
987 * header is sent directly on the wire, not wrapped as a TLS
988 * record. Our record layer just processes the message length and passes
989 * the rest right through. Its format is:
991 * 0-1 msg_length - decoded by the record layer
992 * 2 msg_type - s->init_msg points here
994 * 5-6 cipher_spec_length
995 * 7-8 session_id_length
996 * 9-10 challenge_length
1000 if (!PACKET_get_1(pkt
, &mt
)
1001 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1003 * Should never happen. We should have tested this in the record
1004 * layer in order to have determined that this is a SSLv2 record
1005 * in the first place
1007 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1011 if (!PACKET_get_net_2(pkt
, &version
)) {
1012 /* No protocol version supplied! */
1013 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNKNOWN_PROTOCOL
);
1016 if (version
== 0x0002) {
1017 /* This is real SSLv2. We don't support it. */
1018 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNKNOWN_PROTOCOL
);
1020 } else if ((version
& 0xff00) == (SSL3_VERSION_MAJOR
<< 8)) {
1022 s
->client_version
= version
;
1024 /* No idea what protocol this is */
1025 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNKNOWN_PROTOCOL
);
1030 * use version from inside client hello, not from record header (may
1031 * differ: see RFC 2246, Appendix E, second paragraph)
1033 if(!PACKET_get_net_2(pkt
, (unsigned int *)&s
->client_version
)) {
1034 al
= SSL_AD_DECODE_ERROR
;
1035 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
1040 /* Do SSL/TLS version negotiation if applicable */
1041 if (!SSL_IS_DTLS(s
)) {
1042 if (s
->version
!= TLS_ANY_VERSION
) {
1043 if (s
->client_version
>= s
->version
) {
1046 } else if (s
->client_version
>= SSL3_VERSION
) {
1047 switch(s
->client_version
) {
1049 case TLS1_2_VERSION
:
1050 if(!(s
->options
& SSL_OP_NO_TLSv1_2
)) {
1051 s
->version
= TLS1_2_VERSION
;
1052 s
->method
= TLSv1_2_server_method();
1056 /* Deliberately fall through */
1057 case TLS1_1_VERSION
:
1058 if(!(s
->options
& SSL_OP_NO_TLSv1_1
)) {
1059 s
->version
= TLS1_1_VERSION
;
1060 s
->method
= TLSv1_1_server_method();
1064 /* Deliberately fall through */
1066 if(!(s
->options
& SSL_OP_NO_TLSv1
)) {
1067 s
->version
= TLS1_VERSION
;
1068 s
->method
= TLSv1_server_method();
1072 /* Deliberately fall through */
1074 #ifndef OPENSSL_NO_SSL3
1075 if(!(s
->options
& SSL_OP_NO_SSLv3
)) {
1076 s
->version
= SSL3_VERSION
;
1077 s
->method
= SSLv3_server_method();
1086 } else if (s
->client_version
<= s
->version
1087 || s
->method
->version
== DTLS_ANY_VERSION
) {
1089 * For DTLS we just check versions are potentially compatible. Version
1090 * negotiation comes later.
1096 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNKNOWN_PROTOCOL
);
1097 if ((!s
->enc_write_ctx
&& !s
->write_hash
)) {
1099 * similar to ssl3_get_record, send alert using remote version
1102 s
->version
= s
->client_version
;
1104 al
= SSL_AD_PROTOCOL_VERSION
;
1108 /* Parse the message and load client random. */
1111 * Handle an SSLv2 backwards compatible ClientHello
1112 * Note, this is only for SSLv3+ using the backward compatible format.
1113 * Real SSLv2 is not supported, and is rejected above.
1115 unsigned int cipher_len
, session_id_len
, challenge_len
;
1118 if (!PACKET_get_net_2(pkt
, &cipher_len
)
1119 || !PACKET_get_net_2(pkt
, &session_id_len
)
1120 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1121 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1122 SSL_R_RECORD_LENGTH_MISMATCH
);
1123 al
= SSL_AD_DECODE_ERROR
;
1127 if (!PACKET_get_sub_packet(pkt
, &cipher_suites
, cipher_len
)
1128 || !PACKET_get_sub_packet(pkt
, &session_id
, session_id_len
)
1129 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1130 /* No extensions. */
1131 || PACKET_remaining(pkt
) != 0) {
1132 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1133 SSL_R_RECORD_LENGTH_MISMATCH
);
1134 al
= SSL_AD_DECODE_ERROR
;
1138 /* Load the client random */
1139 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
? SSL3_RANDOM_SIZE
:
1141 memset(s
->s3
->client_random
, 0, SSL3_RANDOM_SIZE
);
1142 if (!PACKET_copy_bytes(&challenge
,
1143 s
->s3
->client_random
+ SSL3_RANDOM_SIZE
-
1144 challenge_len
, challenge_len
)) {
1145 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1146 al
= SSL_AD_INTERNAL_ERROR
;
1150 PACKET_null_init(&compression
);
1151 PACKET_null_init(&extensions
);
1153 /* Regular ClientHello. */
1154 if (!PACKET_copy_bytes(pkt
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
)
1155 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)) {
1156 al
= SSL_AD_DECODE_ERROR
;
1157 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1161 if (SSL_IS_DTLS(s
)) {
1162 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1163 al
= SSL_AD_DECODE_ERROR
;
1164 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1168 * If we require cookies and this ClientHello doesn't contain one,
1169 * just return since we do not want to allocate any memory yet.
1170 * So check cookie length...
1172 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1173 if (PACKET_remaining(&cookie
) == 0)
1178 if (!PACKET_get_length_prefixed_2(pkt
, &cipher_suites
)
1179 || !PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1180 al
= SSL_AD_DECODE_ERROR
;
1181 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1184 /* Could be empty. */
1191 * We don't allow resumption in a backwards compatible ClientHello.
1192 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1194 * Versions before 0.9.7 always allow clients to resume sessions in
1195 * renegotiation. 0.9.7 and later allow this by default, but optionally
1196 * ignore resumption requests with flag
1197 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1198 * than a change to default behavior so that applications relying on
1199 * this for security won't even compile against older library versions).
1200 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1201 * request renegotiation but not a new session (s->new_session remains
1202 * unset): for servers, this essentially just means that the
1203 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1208 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1209 if (!ssl_get_new_session(s
, 1))
1212 i
= ssl_get_prev_session(s
, &extensions
, &session_id
);
1214 * Only resume if the session's version matches the negotiated
1216 * RFC 5246 does not provide much useful advice on resumption
1217 * with a different protocol version. It doesn't forbid it but
1218 * the sanity of such behaviour would be questionable.
1219 * In practice, clients do not accept a version mismatch and
1220 * will abort the handshake with an error.
1222 if (i
== 1 && s
->version
== s
->session
->ssl_version
) {
1223 /* previous session */
1225 } else if (i
== -1) {
1229 if (!ssl_get_new_session(s
, 1))
1234 if (SSL_IS_DTLS(s
)) {
1235 /* Empty cookie was already handled above by returning early. */
1236 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1237 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1238 if (s
->ctx
->app_verify_cookie_cb(s
, PACKET_data(&cookie
),
1239 PACKET_remaining(&cookie
)) == 0) {
1240 al
= SSL_AD_HANDSHAKE_FAILURE
;
1241 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1242 SSL_R_COOKIE_MISMATCH
);
1244 /* else cookie verification succeeded */
1246 /* default verification */
1247 } else if (!PACKET_equal(&cookie
, s
->d1
->cookie
,
1248 s
->d1
->cookie_len
)) {
1249 al
= SSL_AD_HANDSHAKE_FAILURE
;
1250 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_COOKIE_MISMATCH
);
1253 s
->d1
->cookie_verified
= 1;
1255 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1256 /* Select version to use */
1257 if (s
->client_version
<= DTLS1_2_VERSION
&&
1258 !(s
->options
& SSL_OP_NO_DTLSv1_2
)) {
1259 s
->version
= DTLS1_2_VERSION
;
1260 s
->method
= DTLSv1_2_server_method();
1261 } else if (tls1_suiteb(s
)) {
1262 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1263 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
1264 s
->version
= s
->client_version
;
1265 al
= SSL_AD_PROTOCOL_VERSION
;
1267 } else if (s
->client_version
<= DTLS1_VERSION
&&
1268 !(s
->options
& SSL_OP_NO_DTLSv1
)) {
1269 s
->version
= DTLS1_VERSION
;
1270 s
->method
= DTLSv1_server_method();
1272 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1273 SSL_R_WRONG_VERSION_NUMBER
);
1274 s
->version
= s
->client_version
;
1275 al
= SSL_AD_PROTOCOL_VERSION
;
1278 s
->session
->ssl_version
= s
->version
;
1282 if (ssl_bytes_to_cipher_list(s
, &cipher_suites
, &(ciphers
),
1283 is_v2_record
, &al
) == NULL
) {
1287 /* If it is a hit, check that the cipher is in the list */
1290 id
= s
->session
->cipher
->id
;
1293 fprintf(stderr
, "client sent %d ciphers\n",
1294 sk_SSL_CIPHER_num(ciphers
));
1296 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1297 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1299 fprintf(stderr
, "client [%2d of %2d]:%s\n",
1300 i
, sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1309 * we need to have the cipher in the cipher list if we are asked
1312 al
= SSL_AD_ILLEGAL_PARAMETER
;
1313 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1314 SSL_R_REQUIRED_CIPHER_MISSING
);
1319 complen
= PACKET_remaining(&compression
);
1320 for (j
= 0; j
< complen
; j
++) {
1321 if (PACKET_data(&compression
)[j
] == 0)
1327 al
= SSL_AD_DECODE_ERROR
;
1328 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1332 /* TLS extensions */
1333 if (s
->version
>= SSL3_VERSION
) {
1334 if (!ssl_parse_clienthello_tlsext(s
, &extensions
)) {
1335 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_PARSE_TLSEXT
);
1341 * Check if we want to use external pre-shared secret for this handshake
1342 * for not reused session only. We need to generate server_random before
1343 * calling tls_session_secret_cb in order to allow SessionTicket
1344 * processing to use it in key derivation.
1348 pos
= s
->s3
->server_random
;
1349 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
) <= 0) {
1354 if (!s
->hit
&& s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
) {
1355 SSL_CIPHER
*pref_cipher
= NULL
;
1357 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
1358 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
1359 &s
->session
->master_key_length
, ciphers
,
1361 s
->tls_session_secret_cb_arg
)) {
1363 s
->session
->ciphers
= ciphers
;
1364 s
->session
->verify_result
= X509_V_OK
;
1368 /* check if some cipher was preferred by call back */
1370 pref_cipher
? pref_cipher
: ssl3_choose_cipher(s
,
1375 if (pref_cipher
== NULL
) {
1376 al
= SSL_AD_HANDSHAKE_FAILURE
;
1377 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_NO_SHARED_CIPHER
);
1381 s
->session
->cipher
= pref_cipher
;
1382 sk_SSL_CIPHER_free(s
->cipher_list
);
1383 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1384 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1385 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1390 * Worst case, we will use the NULL compression, but if we have other
1391 * options, we will now look for them. We have complen-1 compression
1392 * algorithms from the client, starting at q.
1394 s
->s3
->tmp
.new_compression
= NULL
;
1395 #ifndef OPENSSL_NO_COMP
1396 /* This only happens if we have a cache hit */
1397 if (s
->session
->compress_meth
!= 0) {
1398 int m
, comp_id
= s
->session
->compress_meth
;
1400 /* Perform sanity checks on resumed compression algorithm */
1401 /* Can't disable compression */
1402 if (!ssl_allow_compression(s
)) {
1403 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1404 SSL_R_INCONSISTENT_COMPRESSION
);
1407 /* Look for resumed compression method */
1408 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
1409 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1410 if (comp_id
== comp
->id
) {
1411 s
->s3
->tmp
.new_compression
= comp
;
1415 if (s
->s3
->tmp
.new_compression
== NULL
) {
1416 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1417 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1420 /* Look for resumed method in compression list */
1421 for (k
= 0; k
< complen
; k
++) {
1422 if (PACKET_data(&compression
)[k
] == comp_id
)
1426 al
= SSL_AD_ILLEGAL_PARAMETER
;
1427 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1428 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING
);
1433 else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
1434 /* See if we have a match */
1435 int m
, nn
, v
, done
= 0;
1438 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1439 for (m
= 0; m
< nn
; m
++) {
1440 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1442 for (o
= 0; o
< complen
; o
++) {
1443 if (v
== PACKET_data(&compression
)[o
]) {
1452 s
->s3
->tmp
.new_compression
= comp
;
1458 * If compression is disabled we'd better not try to resume a session
1459 * using compression.
1461 if (s
->session
->compress_meth
!= 0) {
1462 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1468 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1472 #ifdef OPENSSL_NO_COMP
1473 s
->session
->compress_meth
= 0;
1475 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
1477 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1478 s
->session
->ciphers
= ciphers
;
1479 if (ciphers
== NULL
) {
1480 al
= SSL_AD_INTERNAL_ERROR
;
1481 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1485 if (!tls1_set_server_sigalgs(s
)) {
1486 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1491 sk_SSL_CIPHER_free(ciphers
);
1492 return MSG_PROCESS_CONTINUE_PROCESSING
;
1494 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1496 ossl_statem_set_error(s
);
1498 sk_SSL_CIPHER_free(ciphers
);
1499 return MSG_PROCESS_ERROR
;
1503 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
1505 int al
= SSL_AD_HANDSHAKE_FAILURE
;
1508 if (wst
== WORK_MORE_A
) {
1510 /* Let cert callback update server certificates if required */
1511 if (s
->cert
->cert_cb
) {
1512 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
1514 al
= SSL_AD_INTERNAL_ERROR
;
1515 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
, SSL_R_CERT_CB_ERROR
);
1519 s
->rwstate
= SSL_X509_LOOKUP
;
1522 s
->rwstate
= SSL_NOTHING
;
1524 cipher
= ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
1526 if (cipher
== NULL
) {
1527 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
, SSL_R_NO_SHARED_CIPHER
);
1530 s
->s3
->tmp
.new_cipher
= cipher
;
1531 /* check whether we should disable session resumption */
1532 if (s
->not_resumable_session_cb
!= NULL
)
1533 s
->session
->not_resumable
= s
->not_resumable_session_cb(s
,
1534 ((cipher
->algorithm_mkey
& (SSL_kDHE
| SSL_kECDHE
)) != 0));
1535 if (s
->session
->not_resumable
)
1536 /* do not send a session ticket */
1537 s
->tlsext_ticket_expected
= 0;
1539 /* Session-id reuse */
1540 s
->s3
->tmp
.new_cipher
= s
->session
->cipher
;
1543 if (!(s
->verify_mode
& SSL_VERIFY_PEER
)) {
1544 if (!ssl3_digest_cached_records(s
, 0)) {
1545 al
= SSL_AD_INTERNAL_ERROR
;
1551 * we now have the following setup.
1553 * cipher_list - our prefered list of ciphers
1554 * ciphers - the clients prefered list of ciphers
1555 * compression - basically ignored right now
1556 * ssl version is set - sslv3
1557 * s->session - The ssl session has been setup.
1558 * s->hit - session reuse flag
1559 * s->s3->tmp.new_cipher- the new cipher to use.
1562 /* Handles TLS extensions that we couldn't check earlier */
1563 if (s
->version
>= SSL3_VERSION
) {
1564 if (ssl_check_clienthello_tlsext_late(s
) <= 0) {
1565 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
1566 SSL_R_CLIENTHELLO_TLSEXT
);
1573 #ifndef OPENSSL_NO_SRP
1574 if (wst
== WORK_MORE_B
) {
1576 if ((ret
= ssl_check_srp_ext_ClientHello(s
, &al
)) < 0) {
1578 * callback indicates further work to be done
1580 s
->rwstate
= SSL_X509_LOOKUP
;
1583 if (ret
!= SSL_ERROR_NONE
) {
1585 * This is not really an error but the only means to for
1586 * a client to detect whether srp is supported.
1588 if (al
!= TLS1_AD_UNKNOWN_PSK_IDENTITY
)
1589 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
1590 SSL_R_CLIENTHELLO_TLSEXT
);
1597 return WORK_FINISHED_STOP
;
1599 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1600 ossl_statem_set_error(s
);
1604 int tls_construct_server_hello(SSL
*s
)
1607 unsigned char *p
, *d
;
1612 buf
= (unsigned char *)s
->init_buf
->data
;
1614 /* Do the message type and length last */
1615 d
= p
= ssl_handshake_start(s
);
1617 *(p
++) = s
->version
>> 8;
1618 *(p
++) = s
->version
& 0xff;
1621 * Random stuff. Filling of the server_random takes place in
1622 * tls_process_client_hello()
1624 memcpy(p
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
1625 p
+= SSL3_RANDOM_SIZE
;
1628 * There are several cases for the session ID to send
1629 * back in the server hello:
1630 * - For session reuse from the session cache,
1631 * we send back the old session ID.
1632 * - If stateless session reuse (using a session ticket)
1633 * is successful, we send back the client's "session ID"
1634 * (which doesn't actually identify the session).
1635 * - If it is a new session, we send back the new
1637 * - However, if we want the new session to be single-use,
1638 * we send back a 0-length session ID.
1639 * s->hit is non-zero in either case of session reuse,
1640 * so the following won't overwrite an ID that we're supposed
1643 if (s
->session
->not_resumable
||
1644 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
1646 s
->session
->session_id_length
= 0;
1648 sl
= s
->session
->session_id_length
;
1649 if (sl
> (int)sizeof(s
->session
->session_id
)) {
1650 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1651 ossl_statem_set_error(s
);
1655 memcpy(p
, s
->session
->session_id
, sl
);
1658 /* put the cipher */
1659 i
= ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
, p
);
1662 /* put the compression method */
1663 #ifdef OPENSSL_NO_COMP
1666 if (s
->s3
->tmp
.new_compression
== NULL
)
1669 *(p
++) = s
->s3
->tmp
.new_compression
->id
;
1672 if (ssl_prepare_serverhello_tlsext(s
) <= 0) {
1673 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, SSL_R_SERVERHELLO_TLSEXT
);
1674 ossl_statem_set_error(s
);
1678 ssl_add_serverhello_tlsext(s
, p
, buf
+ SSL3_RT_MAX_PLAIN_LENGTH
,
1680 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1681 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1682 ossl_statem_set_error(s
);
1688 if (!ssl_set_handshake_header(s
, SSL3_MT_SERVER_HELLO
, l
)) {
1689 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1690 ossl_statem_set_error(s
);
1697 int tls_construct_server_done(SSL
*s
)
1699 if (!ssl_set_handshake_header(s
, SSL3_MT_SERVER_DONE
, 0)) {
1700 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_DONE
, ERR_R_INTERNAL_ERROR
);
1701 ossl_statem_set_error(s
);
1705 if (!s
->s3
->tmp
.cert_request
) {
1706 if (!ssl3_digest_cached_records(s
, 0)) {
1707 ossl_statem_set_error(s
);
1714 int tls_construct_server_key_exchange(SSL
*s
)
1716 #ifndef OPENSSL_NO_DH
1717 DH
*dh
= NULL
, *dhp
;
1719 #ifndef OPENSSL_NO_EC
1720 unsigned char *encodedPoint
= NULL
;
1725 const EVP_MD
*md
= NULL
;
1726 unsigned char *p
, *d
;
1733 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
1735 if (md_ctx
== NULL
) {
1736 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1737 al
= SSL_AD_INTERNAL_ERROR
;
1741 type
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1745 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
1747 #ifndef OPENSSL_NO_PSK
1748 if (type
& SSL_PSK
) {
1750 * reserve size for record length and PSK identity hint
1753 if (s
->cert
->psk_identity_hint
)
1754 n
+= strlen(s
->cert
->psk_identity_hint
);
1756 /* Plain PSK or RSAPSK nothing to do */
1757 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
1759 #endif /* !OPENSSL_NO_PSK */
1760 #ifndef OPENSSL_NO_DH
1761 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
1762 CERT
*cert
= s
->cert
;
1764 if (s
->cert
->dh_tmp_auto
) {
1765 dhp
= ssl_get_auto_dh(s
);
1767 al
= SSL_AD_INTERNAL_ERROR
;
1768 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1769 ERR_R_INTERNAL_ERROR
);
1774 if ((dhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
))
1775 dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
1777 al
= SSL_AD_HANDSHAKE_FAILURE
;
1778 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1779 SSL_R_MISSING_TMP_DH_KEY
);
1782 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
1783 DH_security_bits(dhp
), 0, dhp
)) {
1784 al
= SSL_AD_HANDSHAKE_FAILURE
;
1785 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1786 SSL_R_DH_KEY_TOO_SMALL
);
1789 if (s
->s3
->tmp
.dh
!= NULL
) {
1790 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1791 ERR_R_INTERNAL_ERROR
);
1795 if (s
->cert
->dh_tmp_auto
)
1797 else if ((dh
= DHparams_dup(dhp
)) == NULL
) {
1798 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1803 if ((dhp
->pub_key
== NULL
||
1804 dhp
->priv_key
== NULL
||
1805 (s
->options
& SSL_OP_SINGLE_DH_USE
))) {
1806 if (!DH_generate_key(dh
)) {
1807 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1811 dh
->pub_key
= BN_dup(dhp
->pub_key
);
1812 dh
->priv_key
= BN_dup(dhp
->priv_key
);
1813 if ((dh
->pub_key
== NULL
) || (dh
->priv_key
== NULL
)) {
1814 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1823 #ifndef OPENSSL_NO_EC
1824 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
1827 if (s
->s3
->tmp
.pkey
!= NULL
) {
1828 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1829 ERR_R_INTERNAL_ERROR
);
1833 /* Get NID of appropriate shared curve */
1834 nid
= tls1_shared_curve(s
, -2);
1835 curve_id
= tls1_ec_nid2curve_id(nid
);
1836 if (curve_id
== 0) {
1837 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1838 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1841 s
->s3
->tmp
.pkey
= ssl_generate_pkey(NULL
, nid
);
1842 /* Generate a new key for this curve */
1843 if (s
->s3
->tmp
.pkey
== NULL
) {
1844 al
= SSL_AD_INTERNAL_ERROR
;
1845 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
1849 /* Encode the public key. */
1850 encodedlen
= EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(s
->s3
->tmp
.pkey
),
1851 POINT_CONVERSION_UNCOMPRESSED
,
1852 &encodedPoint
, NULL
);
1854 if (encodedlen
== 0) {
1855 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1860 * We only support named (not generic) curves in ECDH ephemeral key
1861 * exchanges. In this situation, we need four additional bytes to
1862 * encode the entire ServerECDHParams structure.
1864 n
+= 4 + encodedlen
;
1867 * We'll generate the serverKeyExchange message explicitly so we
1868 * can set these to NULLs
1875 #endif /* !OPENSSL_NO_EC */
1876 #ifndef OPENSSL_NO_SRP
1877 if (type
& SSL_kSRP
) {
1878 if ((s
->srp_ctx
.N
== NULL
) ||
1879 (s
->srp_ctx
.g
== NULL
) ||
1880 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
1881 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1882 SSL_R_MISSING_SRP_PARAM
);
1885 r
[0] = s
->srp_ctx
.N
;
1886 r
[1] = s
->srp_ctx
.g
;
1887 r
[2] = s
->srp_ctx
.s
;
1888 r
[3] = s
->srp_ctx
.B
;
1892 al
= SSL_AD_HANDSHAKE_FAILURE
;
1893 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1894 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1897 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
1898 nr
[i
] = BN_num_bytes(r
[i
]);
1899 #ifndef OPENSSL_NO_SRP
1900 if ((i
== 2) && (type
& SSL_kSRP
))
1907 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
|SSL_aSRP
))
1908 && !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) {
1909 if ((pkey
= ssl_get_sign_pkey(s
, s
->s3
->tmp
.new_cipher
, &md
))
1911 al
= SSL_AD_DECODE_ERROR
;
1914 kn
= EVP_PKEY_size(pkey
);
1920 if (!BUF_MEM_grow_clean(buf
, n
+ SSL_HM_HEADER_LENGTH(s
) + kn
)) {
1921 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_LIB_BUF
);
1924 d
= p
= ssl_handshake_start(s
);
1926 #ifndef OPENSSL_NO_PSK
1927 if (type
& SSL_PSK
) {
1928 /* copy PSK identity hint */
1929 if (s
->cert
->psk_identity_hint
) {
1930 s2n(strlen(s
->cert
->psk_identity_hint
), p
);
1931 strncpy((char *)p
, s
->cert
->psk_identity_hint
,
1932 strlen(s
->cert
->psk_identity_hint
));
1933 p
+= strlen(s
->cert
->psk_identity_hint
);
1940 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
1941 #ifndef OPENSSL_NO_SRP
1942 if ((i
== 2) && (type
& SSL_kSRP
)) {
1952 #ifndef OPENSSL_NO_EC
1953 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
1955 * XXX: For now, we only support named (not generic) curves. In
1956 * this situation, the serverKeyExchange message has: [1 byte
1957 * CurveType], [2 byte CurveName] [1 byte length of encoded
1958 * point], followed by the actual encoded point itself
1960 *p
= NAMED_CURVE_TYPE
;
1968 memcpy(p
, encodedPoint
, encodedlen
);
1969 OPENSSL_free(encodedPoint
);
1970 encodedPoint
= NULL
;
1978 * n is the length of the params, they start at &(d[4]) and p
1979 * points to the space at the end.
1982 /* send signature algorithm */
1983 if (SSL_USE_SIGALGS(s
)) {
1984 if (!tls12_get_sigandhash(p
, pkey
, md
)) {
1985 /* Should never happen */
1986 al
= SSL_AD_INTERNAL_ERROR
;
1987 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
1988 ERR_R_INTERNAL_ERROR
);
1994 fprintf(stderr
, "Using hash %s\n", EVP_MD_name(md
));
1996 if (EVP_SignInit_ex(md_ctx
, md
, NULL
) <= 0
1997 || EVP_SignUpdate(md_ctx
, &(s
->s3
->client_random
[0]),
1998 SSL3_RANDOM_SIZE
) <= 0
1999 || EVP_SignUpdate(md_ctx
, &(s
->s3
->server_random
[0]),
2000 SSL3_RANDOM_SIZE
) <= 0
2001 || EVP_SignUpdate(md_ctx
, d
, n
) <= 0
2002 || EVP_SignFinal(md_ctx
, &(p
[2]),
2003 (unsigned int *)&i
, pkey
) <= 0) {
2004 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_LIB_EVP
);
2005 al
= SSL_AD_INTERNAL_ERROR
;
2010 if (SSL_USE_SIGALGS(s
))
2013 /* Is this error check actually needed? */
2014 al
= SSL_AD_HANDSHAKE_FAILURE
;
2015 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2016 SSL_R_UNKNOWN_PKEY_TYPE
);
2021 if (!ssl_set_handshake_header(s
, SSL3_MT_SERVER_KEY_EXCHANGE
, n
)) {
2022 al
= SSL_AD_HANDSHAKE_FAILURE
;
2023 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2027 EVP_MD_CTX_free(md_ctx
);
2030 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2032 #ifndef OPENSSL_NO_EC
2033 OPENSSL_free(encodedPoint
);
2035 EVP_MD_CTX_free(md_ctx
);
2036 ossl_statem_set_error(s
);
2040 int tls_construct_certificate_request(SSL
*s
)
2042 unsigned char *p
, *d
;
2043 int i
, j
, nl
, off
, n
;
2044 STACK_OF(X509_NAME
) *sk
= NULL
;
2050 d
= p
= ssl_handshake_start(s
);
2052 /* get the list of acceptable cert types */
2054 n
= ssl3_get_req_cert_type(s
, p
);
2059 if (SSL_USE_SIGALGS(s
)) {
2060 const unsigned char *psigs
;
2061 unsigned char *etmp
= p
;
2062 nl
= tls12_get_psigalgs(s
, &psigs
);
2063 /* Skip over length for now */
2065 nl
= tls12_copy_sigalgs(s
, p
, psigs
, nl
);
2066 /* Now fill in length */
2076 sk
= SSL_get_client_CA_list(s
);
2079 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
2080 name
= sk_X509_NAME_value(sk
, i
);
2081 j
= i2d_X509_NAME(name
, NULL
);
2082 if (!BUF_MEM_grow_clean
2083 (buf
, SSL_HM_HEADER_LENGTH(s
) + n
+ j
+ 2)) {
2084 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2088 p
= ssl_handshake_start(s
) + n
;
2090 i2d_X509_NAME(name
, &p
);
2095 /* else no CA names */
2096 p
= ssl_handshake_start(s
) + off
;
2099 if (!ssl_set_handshake_header(s
, SSL3_MT_CERTIFICATE_REQUEST
, n
)) {
2100 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2104 s
->s3
->tmp
.cert_request
= 1;
2108 ossl_statem_set_error(s
);
2112 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
2116 unsigned long alg_k
;
2117 #ifndef OPENSSL_NO_RSA
2119 EVP_PKEY
*pkey
= NULL
;
2121 #ifndef OPENSSL_NO_DH
2125 #ifndef OPENSSL_NO_EC
2126 EVP_PKEY
*ckey
= NULL
;
2128 PACKET enc_premaster
;
2129 unsigned char *data
, *rsa_decrypt
= NULL
;
2131 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2133 #ifndef OPENSSL_NO_PSK
2134 /* For PSK parse and retrieve identity, obtain PSK key */
2135 if (alg_k
& SSL_PSK
) {
2136 unsigned char psk
[PSK_MAX_PSK_LEN
];
2138 PACKET psk_identity
;
2140 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2141 al
= SSL_AD_DECODE_ERROR
;
2142 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
2145 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2146 al
= SSL_AD_DECODE_ERROR
;
2147 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2148 SSL_R_DATA_LENGTH_TOO_LONG
);
2151 if (s
->psk_server_callback
== NULL
) {
2152 al
= SSL_AD_INTERNAL_ERROR
;
2153 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2154 SSL_R_PSK_NO_SERVER_CB
);
2158 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2159 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2160 al
= SSL_AD_INTERNAL_ERROR
;
2164 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2167 if (psklen
> PSK_MAX_PSK_LEN
) {
2168 al
= SSL_AD_INTERNAL_ERROR
;
2169 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2171 } else if (psklen
== 0) {
2173 * PSK related to the given identity not found
2175 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2176 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2177 al
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
2181 OPENSSL_free(s
->s3
->tmp
.psk
);
2182 s
->s3
->tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2183 OPENSSL_cleanse(psk
, psklen
);
2185 if (s
->s3
->tmp
.psk
== NULL
) {
2186 al
= SSL_AD_INTERNAL_ERROR
;
2187 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2191 s
->s3
->tmp
.psklen
= psklen
;
2193 if (alg_k
& SSL_kPSK
) {
2194 /* Identity extracted earlier: should be nothing left */
2195 if (PACKET_remaining(pkt
) != 0) {
2196 al
= SSL_AD_HANDSHAKE_FAILURE
;
2197 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
2200 /* PSK handled by ssl_generate_master_secret */
2201 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
2202 al
= SSL_AD_INTERNAL_ERROR
;
2203 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2208 #ifndef OPENSSL_NO_RSA
2209 if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
2210 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
2212 unsigned char decrypt_good
, version_good
;
2215 /* FIX THIS UP EAY EAY EAY EAY */
2216 pkey
= s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
2217 if ((pkey
== NULL
) ||
2218 (pkey
->type
!= EVP_PKEY_RSA
) || (pkey
->pkey
.rsa
== NULL
)) {
2219 al
= SSL_AD_HANDSHAKE_FAILURE
;
2220 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2221 SSL_R_MISSING_RSA_CERTIFICATE
);
2224 rsa
= pkey
->pkey
.rsa
;
2226 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2227 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2228 enc_premaster
= *pkt
;
2231 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2232 || PACKET_remaining(pkt
) != 0) {
2233 /* Try SSLv3 behaviour for TLS. */
2234 if (s
->options
& SSL_OP_TLS_D5_BUG
) {
2235 enc_premaster
= orig
;
2237 al
= SSL_AD_DECODE_ERROR
;
2238 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2239 SSL_R_LENGTH_MISMATCH
);
2246 * We want to be sure that the plaintext buffer size makes it safe to
2247 * iterate over the entire size of a premaster secret
2248 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2249 * their ciphertext cannot accommodate a premaster secret anyway.
2251 if (RSA_size(rsa
) < SSL_MAX_MASTER_KEY_LENGTH
) {
2252 al
= SSL_AD_INTERNAL_ERROR
;
2253 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2254 RSA_R_KEY_SIZE_TOO_SMALL
);
2258 rsa_decrypt
= OPENSSL_malloc(RSA_size(rsa
));
2259 if (rsa_decrypt
== NULL
) {
2260 al
= SSL_AD_INTERNAL_ERROR
;
2261 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2266 * We must not leak whether a decryption failure occurs because of
2267 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2268 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2269 * generates a random premaster secret for the case that the decrypt
2270 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2273 if (RAND_bytes(rand_premaster_secret
,
2274 sizeof(rand_premaster_secret
)) <= 0) {
2278 decrypt_len
= RSA_private_decrypt(PACKET_remaining(&enc_premaster
),
2279 PACKET_data(&enc_premaster
),
2280 rsa_decrypt
, rsa
, RSA_PKCS1_PADDING
);
2284 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2285 * be 0xff if so and zero otherwise.
2288 constant_time_eq_int_8(decrypt_len
, SSL_MAX_MASTER_KEY_LENGTH
);
2291 * If the version in the decrypted pre-master secret is correct then
2292 * version_good will be 0xff, otherwise it'll be zero. The
2293 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2294 * (http://eprint.iacr.org/2003/052/) exploits the version number
2295 * check as a "bad version oracle". Thus version checks are done in
2296 * constant time and are treated like any other decryption error.
2299 constant_time_eq_8(rsa_decrypt
[0],
2300 (unsigned)(s
->client_version
>> 8));
2302 constant_time_eq_8(rsa_decrypt
[1],
2303 (unsigned)(s
->client_version
& 0xff));
2306 * The premaster secret must contain the same version number as the
2307 * ClientHello to detect version rollback attacks (strangely, the
2308 * protocol does not offer such protection for DH ciphersuites).
2309 * However, buggy clients exist that send the negotiated protocol
2310 * version instead if the server does not support the requested
2311 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2314 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) {
2315 unsigned char workaround_good
;
2317 constant_time_eq_8(rsa_decrypt
[0], (unsigned)(s
->version
>> 8));
2319 constant_time_eq_8(rsa_decrypt
[1],
2320 (unsigned)(s
->version
& 0xff));
2321 version_good
|= workaround_good
;
2325 * Both decryption and version must be good for decrypt_good to
2326 * remain non-zero (0xff).
2328 decrypt_good
&= version_good
;
2331 * Now copy rand_premaster_secret over from p using
2332 * decrypt_good_mask. If decryption failed, then p does not
2333 * contain valid plaintext, however, a check above guarantees
2334 * it is still sufficiently large to read from.
2336 for (j
= 0; j
< sizeof(rand_premaster_secret
); j
++) {
2338 constant_time_select_8(decrypt_good
, rsa_decrypt
[j
],
2339 rand_premaster_secret
[j
]);
2342 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
2343 sizeof(rand_premaster_secret
), 0)) {
2344 al
= SSL_AD_INTERNAL_ERROR
;
2345 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2348 OPENSSL_free(rsa_decrypt
);
2352 #ifndef OPENSSL_NO_DH
2353 if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2354 unsigned char shared
[(OPENSSL_DH_MAX_MODULUS_BITS
+ 7) / 8];
2356 if (!PACKET_get_net_2(pkt
, &i
)) {
2357 if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2358 al
= SSL_AD_HANDSHAKE_FAILURE
;
2359 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2360 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2365 if (PACKET_remaining(pkt
) != i
) {
2366 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2367 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2370 if (s
->s3
->tmp
.dh
== NULL
) {
2371 al
= SSL_AD_HANDSHAKE_FAILURE
;
2372 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2373 SSL_R_MISSING_TMP_DH_KEY
);
2376 dh_srvr
= s
->s3
->tmp
.dh
;
2378 if (PACKET_remaining(pkt
) == 0L) {
2379 al
= SSL_AD_HANDSHAKE_FAILURE
;
2380 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2381 SSL_R_MISSING_TMP_DH_KEY
);
2384 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
2385 /* We already checked we have enough data */
2386 al
= SSL_AD_INTERNAL_ERROR
;
2387 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2388 ERR_R_INTERNAL_ERROR
);
2391 pub
= BN_bin2bn(data
, i
, NULL
);
2393 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, SSL_R_BN_LIB
);
2397 i
= DH_compute_key(shared
, pub
, dh_srvr
);
2400 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2405 DH_free(s
->s3
->tmp
.dh
);
2406 s
->s3
->tmp
.dh
= NULL
;
2409 if (!ssl_generate_master_secret(s
, shared
, i
, 0)) {
2410 al
= SSL_AD_INTERNAL_ERROR
;
2411 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2417 #ifndef OPENSSL_NO_EC
2418 if (alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
| SSL_kECDHEPSK
)) {
2419 EVP_PKEY
*skey
= NULL
;
2421 /* Let's get server private key and group information */
2422 if (alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) {
2423 /* use the certificate */
2424 skey
= s
->cert
->pkeys
[SSL_PKEY_ECC
].privatekey
;
2427 * use the ephermeral values we saved when generating the
2428 * ServerKeyExchange msg.
2430 skey
= s
->s3
->tmp
.pkey
;
2433 if (PACKET_remaining(pkt
) == 0L) {
2434 /* We don't support ECDH client auth */
2435 al
= SSL_AD_HANDSHAKE_FAILURE
;
2436 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2437 SSL_R_MISSING_TMP_ECDH_KEY
);
2441 * Get client's public key from encoded point in the
2442 * ClientKeyExchange message.
2445 /* Get encoded point length */
2446 if (!PACKET_get_1(pkt
, &i
)) {
2447 al
= SSL_AD_DECODE_ERROR
;
2448 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2449 SSL_R_LENGTH_MISMATCH
);
2452 if (!PACKET_get_bytes(pkt
, &data
, i
)
2453 || PACKET_remaining(pkt
) != 0) {
2454 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2457 ckey
= EVP_PKEY_new();
2458 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
2459 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
2462 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(ckey
), data
, i
,
2464 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2469 if (ssl_derive(s
, skey
, ckey
) == 0) {
2470 al
= SSL_AD_INTERNAL_ERROR
;
2471 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2475 EVP_PKEY_free(ckey
);
2478 return MSG_PROCESS_CONTINUE_PROCESSING
;
2481 #ifndef OPENSSL_NO_SRP
2482 if (alg_k
& SSL_kSRP
) {
2483 if (!PACKET_get_net_2(pkt
, &i
)
2484 || !PACKET_get_bytes(pkt
, &data
, i
)) {
2485 al
= SSL_AD_DECODE_ERROR
;
2486 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, SSL_R_BAD_SRP_A_LENGTH
);
2489 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
2490 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_BN_LIB
);
2493 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0
2494 || BN_is_zero(s
->srp_ctx
.A
)) {
2495 al
= SSL_AD_ILLEGAL_PARAMETER
;
2496 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2497 SSL_R_BAD_SRP_PARAMETERS
);
2500 OPENSSL_free(s
->session
->srp_username
);
2501 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
2502 if (s
->session
->srp_username
== NULL
) {
2503 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2507 if (!srp_generate_server_master_secret(s
)) {
2508 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2512 #endif /* OPENSSL_NO_SRP */
2513 #ifndef OPENSSL_NO_GOST
2514 if (alg_k
& SSL_kGOST
) {
2515 EVP_PKEY_CTX
*pkey_ctx
;
2516 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
2517 unsigned char premaster_secret
[32], *start
;
2518 size_t outlen
= 32, inlen
;
2519 unsigned long alg_a
;
2524 /* Get our certificate private key */
2525 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2526 if (alg_a
& SSL_aGOST12
) {
2528 * New GOST ciphersuites have SSL_aGOST01 bit too
2530 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
2532 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
2535 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2537 } else if (alg_a
& SSL_aGOST01
) {
2538 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2541 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
2542 if (pkey_ctx
== NULL
) {
2543 al
= SSL_AD_INTERNAL_ERROR
;
2544 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2547 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
2548 al
= SSL_AD_INTERNAL_ERROR
;
2549 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2553 * If client certificate is present and is of the same type, maybe
2554 * use it for key exchange. Don't mind errors from
2555 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2556 * client certificate for authorization only.
2558 client_pub_pkey
= X509_get_pubkey(s
->session
->peer
);
2559 if (client_pub_pkey
) {
2560 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
2563 /* Decrypt session key */
2564 sess_key_len
= PACKET_remaining(pkt
);
2565 if (!PACKET_get_bytes(pkt
, &data
, sess_key_len
)) {
2566 al
= SSL_AD_INTERNAL_ERROR
;
2567 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2570 if (ASN1_get_object ((const unsigned char **)&data
, &Tlen
, &Ttag
,
2571 &Tclass
, sess_key_len
) != V_ASN1_CONSTRUCTED
2572 || Ttag
!= V_ASN1_SEQUENCE
2573 || Tclass
!= V_ASN1_UNIVERSAL
) {
2574 al
= SSL_AD_DECODE_ERROR
;
2575 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2576 SSL_R_DECRYPTION_FAILED
);
2581 if (EVP_PKEY_decrypt
2582 (pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
2583 al
= SSL_AD_DECODE_ERROR
;
2584 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
2585 SSL_R_DECRYPTION_FAILED
);
2588 /* Generate master secret */
2589 if (!ssl_generate_master_secret(s
, premaster_secret
,
2590 sizeof(premaster_secret
), 0)) {
2591 al
= SSL_AD_INTERNAL_ERROR
;
2592 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2595 /* Check if pubkey from client certificate was used */
2596 if (EVP_PKEY_CTX_ctrl
2597 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
2598 s
->statem
.no_cert_verify
= 1;
2600 EVP_PKEY_free(client_pub_pkey
);
2601 EVP_PKEY_CTX_free(pkey_ctx
);
2602 return MSG_PROCESS_CONTINUE_PROCESSING
;
2604 EVP_PKEY_free(client_pub_pkey
);
2605 EVP_PKEY_CTX_free(pkey_ctx
);
2610 al
= SSL_AD_HANDSHAKE_FAILURE
;
2611 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, SSL_R_UNKNOWN_CIPHER_TYPE
);
2615 return MSG_PROCESS_CONTINUE_PROCESSING
;
2617 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2618 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2621 #ifndef OPENSSL_NO_EC
2622 EVP_PKEY_free(ckey
);
2623 OPENSSL_free(rsa_decrypt
);
2625 #ifndef OPENSSL_NO_PSK
2626 OPENSSL_clear_free(s
->s3
->tmp
.psk
, s
->s3
->tmp
.psklen
);
2627 s
->s3
->tmp
.psk
= NULL
;
2629 ossl_statem_set_error(s
);
2630 return MSG_PROCESS_ERROR
;
2633 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
2635 #ifndef OPENSSL_NO_SCTP
2636 if (wst
== WORK_MORE_A
) {
2637 if (SSL_IS_DTLS(s
)) {
2638 unsigned char sctpauthkey
[64];
2639 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
2641 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2644 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
2645 sizeof(DTLS1_SCTP_AUTH_LABEL
));
2647 if (SSL_export_keying_material(s
, sctpauthkey
,
2648 sizeof(sctpauthkey
), labelbuffer
,
2649 sizeof(labelbuffer
), NULL
, 0, 0) <= 0) {
2650 ossl_statem_set_error(s
);
2654 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
2655 sizeof(sctpauthkey
), sctpauthkey
);
2660 if ((wst
== WORK_MORE_B
)
2662 && BIO_dgram_is_sctp(SSL_get_wbio(s
))
2663 /* Are we renegotiating? */
2665 /* Are we going to skip the CertificateVerify? */
2666 && (s
->session
->peer
== NULL
|| s
->statem
.no_cert_verify
)
2667 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s
))) {
2668 s
->s3
->in_read_app_data
= 2;
2669 s
->rwstate
= SSL_READING
;
2670 BIO_clear_retry_flags(SSL_get_rbio(s
));
2671 BIO_set_retry_read(SSL_get_rbio(s
));
2672 ossl_statem_set_sctp_read_sock(s
, 1);
2675 ossl_statem_set_sctp_read_sock(s
, 0);
2679 if (s
->statem
.no_cert_verify
) {
2680 /* No certificate verify so we no longer need the handshake_buffer */
2681 BIO_free(s
->s3
->handshake_buffer
);
2682 s
->s3
->handshake_buffer
= NULL
;
2683 return WORK_FINISHED_CONTINUE
;
2685 if (!s
->session
->peer
) {
2686 /* No peer certificate so we no longer need the handshake_buffer */
2687 BIO_free(s
->s3
->handshake_buffer
);
2688 return WORK_FINISHED_CONTINUE
;
2690 if (!s
->s3
->handshake_buffer
) {
2691 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
2692 ERR_R_INTERNAL_ERROR
);
2693 ossl_statem_set_error(s
);
2697 * For sigalgs freeze the handshake buffer. If we support
2698 * extms we've done this already so this is a no-op
2700 if (!ssl3_digest_cached_records(s
, 1)) {
2701 ossl_statem_set_error(s
);
2706 return WORK_FINISHED_CONTINUE
;
2709 MSG_PROCESS_RETURN
tls_process_cert_verify(SSL
*s
, PACKET
*pkt
)
2711 EVP_PKEY
*pkey
= NULL
;
2712 unsigned char *sig
, *data
;
2713 int al
, ret
= MSG_PROCESS_ERROR
;
2717 const EVP_MD
*md
= NULL
;
2721 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
2724 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
2725 al
= SSL_AD_INTERNAL_ERROR
;
2729 peer
= s
->session
->peer
;
2730 pkey
= X509_get_pubkey(peer
);
2731 type
= X509_certificate_type(peer
, pkey
);
2733 if (!(type
& EVP_PKT_SIGN
)) {
2734 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
,
2735 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
2736 al
= SSL_AD_ILLEGAL_PARAMETER
;
2740 /* Check for broken implementations of GOST ciphersuites */
2742 * If key is GOST and n is exactly 64, it is bare signature without
2743 * length field (CryptoPro implementations at least till CSP 4.0)
2745 #ifndef OPENSSL_NO_GOST
2746 if (PACKET_remaining(pkt
) == 64 && pkey
->type
== NID_id_GostR3410_2001
) {
2751 if (SSL_USE_SIGALGS(s
)) {
2754 if (!PACKET_get_bytes(pkt
, &sig
, 2)) {
2755 al
= SSL_AD_DECODE_ERROR
;
2758 rv
= tls12_check_peer_sigalg(&md
, s
, sig
, pkey
);
2760 al
= SSL_AD_INTERNAL_ERROR
;
2762 } else if (rv
== 0) {
2763 al
= SSL_AD_DECODE_ERROR
;
2767 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
2770 /* Use default digest for this key type */
2771 int idx
= ssl_cert_type(NULL
, pkey
);
2773 md
= s
->s3
->tmp
.md
[idx
];
2775 al
= SSL_AD_INTERNAL_ERROR
;
2780 if (!PACKET_get_net_2(pkt
, &len
)) {
2781 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
2782 al
= SSL_AD_DECODE_ERROR
;
2786 j
= EVP_PKEY_size(pkey
);
2787 if (((int)len
> j
) || ((int)PACKET_remaining(pkt
) > j
)
2788 || (PACKET_remaining(pkt
) == 0)) {
2789 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_WRONG_SIGNATURE_SIZE
);
2790 al
= SSL_AD_DECODE_ERROR
;
2793 if (!PACKET_get_bytes(pkt
, &data
, len
)) {
2794 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
2795 al
= SSL_AD_DECODE_ERROR
;
2799 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
2800 if (hdatalen
<= 0) {
2801 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
2802 al
= SSL_AD_INTERNAL_ERROR
;
2806 fprintf(stderr
, "Using client verify alg %s\n", EVP_MD_name(md
));
2808 if (!EVP_VerifyInit_ex(mctx
, md
, NULL
)
2809 || !EVP_VerifyUpdate(mctx
, hdata
, hdatalen
)) {
2810 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
2811 al
= SSL_AD_INTERNAL_ERROR
;
2815 #ifndef OPENSSL_NO_GOST
2816 if (pkey
->type
== NID_id_GostR3410_2001
2817 || pkey
->type
== NID_id_GostR3410_2012_256
2818 || pkey
->type
== NID_id_GostR3410_2012_512
) {
2819 BUF_reverse(data
, NULL
, len
);
2823 if (s
->version
== SSL3_VERSION
2824 && !EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
2825 s
->session
->master_key_length
,
2826 s
->session
->master_key
)) {
2827 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
2828 al
= SSL_AD_INTERNAL_ERROR
;
2832 if (EVP_VerifyFinal(mctx
, data
, len
, pkey
) <= 0) {
2833 al
= SSL_AD_DECRYPT_ERROR
;
2834 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_BAD_SIGNATURE
);
2838 ret
= MSG_PROCESS_CONTINUE_PROCESSING
;
2841 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2842 ossl_statem_set_error(s
);
2844 BIO_free(s
->s3
->handshake_buffer
);
2845 s
->s3
->handshake_buffer
= NULL
;
2846 EVP_MD_CTX_free(mctx
);
2847 EVP_PKEY_free(pkey
);
2851 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
2853 int i
, al
= SSL_AD_INTERNAL_ERROR
, ret
= MSG_PROCESS_ERROR
;
2855 unsigned long l
, llen
;
2856 const unsigned char *certstart
;
2857 unsigned char *certbytes
;
2858 STACK_OF(X509
) *sk
= NULL
;
2861 if ((sk
= sk_X509_new_null()) == NULL
) {
2862 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
2866 if (!PACKET_get_net_3(pkt
, &llen
)
2867 || !PACKET_get_sub_packet(pkt
, &spkt
, llen
)
2868 || PACKET_remaining(pkt
) != 0) {
2869 al
= SSL_AD_DECODE_ERROR
;
2870 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
2874 while (PACKET_remaining(&spkt
) > 0) {
2875 if (!PACKET_get_net_3(&spkt
, &l
)
2876 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
2877 al
= SSL_AD_DECODE_ERROR
;
2878 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
2879 SSL_R_CERT_LENGTH_MISMATCH
);
2883 certstart
= certbytes
;
2884 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, l
);
2886 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
2889 if (certbytes
!= (certstart
+ l
)) {
2890 al
= SSL_AD_DECODE_ERROR
;
2891 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
2892 SSL_R_CERT_LENGTH_MISMATCH
);
2895 if (!sk_X509_push(sk
, x
)) {
2896 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
2902 if (sk_X509_num(sk
) <= 0) {
2903 /* TLS does not mind 0 certs returned */
2904 if (s
->version
== SSL3_VERSION
) {
2905 al
= SSL_AD_HANDSHAKE_FAILURE
;
2906 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
2907 SSL_R_NO_CERTIFICATES_RETURNED
);
2910 /* Fail for TLS only if we required a certificate */
2911 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2912 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2913 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
2914 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2915 al
= SSL_AD_HANDSHAKE_FAILURE
;
2918 /* No client certificate so digest cached records */
2919 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
2924 i
= ssl_verify_cert_chain(s
, sk
);
2926 al
= ssl_verify_alarm_type(s
->verify_result
);
2927 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
2928 SSL_R_CERTIFICATE_VERIFY_FAILED
);
2932 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
2933 al
= SSL_AD_HANDSHAKE_FAILURE
;
2936 pkey
= X509_get_pubkey(sk_X509_value(sk
, 0));
2938 al
= SSL3_AD_HANDSHAKE_FAILURE
;
2939 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
2940 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
2943 EVP_PKEY_free(pkey
);
2946 X509_free(s
->session
->peer
);
2947 s
->session
->peer
= sk_X509_shift(sk
);
2948 s
->session
->verify_result
= s
->verify_result
;
2950 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
2951 s
->session
->peer_chain
= sk
;
2953 * Inconsistency alert: cert_chain does *not* include the peer's own
2954 * certificate, while we do include it in s3_clnt.c
2957 ret
= MSG_PROCESS_CONTINUE_READING
;
2961 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2962 ossl_statem_set_error(s
);
2965 sk_X509_pop_free(sk
, X509_free
);
2969 int tls_construct_server_certificate(SSL
*s
)
2973 cpk
= ssl_get_server_send_pkey(s
);
2975 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
2976 ossl_statem_set_error(s
);
2980 if (!ssl3_output_cert_chain(s
, cpk
)) {
2981 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
2982 ossl_statem_set_error(s
);
2989 int tls_construct_new_session_ticket(SSL
*s
)
2991 unsigned char *senc
= NULL
;
2993 HMAC_CTX
*hctx
= NULL
;
2994 unsigned char *p
, *macstart
;
2995 const unsigned char *const_p
;
2996 int len
, slen_full
, slen
;
2999 SSL_CTX
*tctx
= s
->initial_ctx
;
3000 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3001 unsigned char key_name
[16];
3003 /* get session encoding length */
3004 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3006 * Some length values are 16 bits, so forget it if session is too
3009 if (slen_full
== 0 || slen_full
> 0xFF00) {
3010 ossl_statem_set_error(s
);
3013 senc
= OPENSSL_malloc(slen_full
);
3015 ossl_statem_set_error(s
);
3019 EVP_CIPHER_CTX_init(&ctx
);
3020 hctx
= HMAC_CTX_new();
3023 if (!i2d_SSL_SESSION(s
->session
, &p
))
3027 * create a fresh copy (not shared with other threads) to clean up
3030 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3033 sess
->session_id_length
= 0; /* ID is irrelevant for the ticket */
3035 slen
= i2d_SSL_SESSION(sess
, NULL
);
3036 if (slen
== 0 || slen
> slen_full
) { /* shouldn't ever happen */
3037 SSL_SESSION_free(sess
);
3041 if (!i2d_SSL_SESSION(sess
, &p
)) {
3042 SSL_SESSION_free(sess
);
3045 SSL_SESSION_free(sess
);
3048 * Grow buffer if need be: the length calculation is as
3049 * follows handshake_header_length +
3050 * 4 (ticket lifetime hint) + 2 (ticket length) +
3051 * 16 (key name) + max_iv_len (iv length) +
3052 * session_length + max_enc_block_size (max encrypted session
3053 * length) + max_md_size (HMAC).
3055 if (!BUF_MEM_grow(s
->init_buf
,
3056 SSL_HM_HEADER_LENGTH(s
) + 22 + EVP_MAX_IV_LENGTH
+
3057 EVP_MAX_BLOCK_LENGTH
+ EVP_MAX_MD_SIZE
+ slen
))
3060 p
= ssl_handshake_start(s
);
3062 * Initialize HMAC and cipher contexts. If callback present it does
3063 * all the work otherwise use generated values from parent ctx.
3065 if (tctx
->tlsext_ticket_key_cb
) {
3066 if (tctx
->tlsext_ticket_key_cb(s
, key_name
, iv
, &ctx
, hctx
, 1) < 0)
3069 if (RAND_bytes(iv
, 16) <= 0)
3071 if (!EVP_EncryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
3072 tctx
->tlsext_tick_aes_key
, iv
))
3074 if (!HMAC_Init_ex(hctx
, tctx
->tlsext_tick_hmac_key
, 16,
3075 EVP_sha256(), NULL
))
3077 memcpy(key_name
, tctx
->tlsext_tick_key_name
, 16);
3081 * Ticket lifetime hint (advisory only): We leave this unspecified
3082 * for resumed session (for simplicity), and guess that tickets for
3083 * new sessions will live as long as their sessions.
3085 l2n(s
->hit
? 0 : s
->session
->timeout
, p
);
3087 /* Skip ticket length for now */
3089 /* Output key name */
3091 memcpy(p
, key_name
, 16);
3094 memcpy(p
, iv
, EVP_CIPHER_CTX_iv_length(&ctx
));
3095 p
+= EVP_CIPHER_CTX_iv_length(&ctx
);
3096 /* Encrypt session data */
3097 if (!EVP_EncryptUpdate(&ctx
, p
, &len
, senc
, slen
))
3100 if (!EVP_EncryptFinal(&ctx
, p
, &len
))
3104 if (!HMAC_Update(hctx
, macstart
, p
- macstart
))
3106 if (!HMAC_Final(hctx
, p
, &hlen
))
3109 EVP_CIPHER_CTX_cleanup(&ctx
);
3110 HMAC_CTX_free(hctx
);
3113 /* Now write out lengths: p points to end of data written */
3115 len
= p
- ssl_handshake_start(s
);
3116 /* Skip ticket lifetime hint */
3117 p
= ssl_handshake_start(s
) + 4;
3119 if (!ssl_set_handshake_header(s
, SSL3_MT_NEWSESSION_TICKET
, len
))
3126 EVP_CIPHER_CTX_cleanup(&ctx
);
3127 HMAC_CTX_free(hctx
);
3128 ossl_statem_set_error(s
);
3132 int tls_construct_cert_status(SSL
*s
)
3136 * Grow buffer if need be: the length calculation is as
3137 * follows 1 (message type) + 3 (message length) +
3138 * 1 (ocsp response type) + 3 (ocsp response length)
3141 if (!BUF_MEM_grow(s
->init_buf
, 8 + s
->tlsext_ocsp_resplen
)) {
3142 ossl_statem_set_error(s
);
3146 p
= (unsigned char *)s
->init_buf
->data
;
3149 *(p
++) = SSL3_MT_CERTIFICATE_STATUS
;
3150 /* message length */
3151 l2n3(s
->tlsext_ocsp_resplen
+ 4, p
);
3153 *(p
++) = s
->tlsext_status_type
;
3154 /* length of OCSP response */
3155 l2n3(s
->tlsext_ocsp_resplen
, p
);
3156 /* actual response */
3157 memcpy(p
, s
->tlsext_ocsp_resp
, s
->tlsext_ocsp_resplen
);
3158 /* number of bytes to write */
3159 s
->init_num
= 8 + s
->tlsext_ocsp_resplen
;
3165 #ifndef OPENSSL_NO_NEXTPROTONEG
3167 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3168 * It sets the next_proto member in s if found
3170 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
3172 PACKET next_proto
, padding
;
3173 size_t next_proto_len
;
3176 * The payload looks like:
3178 * uint8 proto[proto_len];
3179 * uint8 padding_len;
3180 * uint8 padding[padding_len];
3182 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
3183 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
3184 || PACKET_remaining(pkt
) > 0) {
3185 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO
, SSL_R_LENGTH_MISMATCH
);
3189 if (!PACKET_memdup(&next_proto
, &s
->next_proto_negotiated
,
3191 s
->next_proto_negotiated_len
= 0;
3195 s
->next_proto_negotiated_len
= (unsigned char)next_proto_len
;
3197 return MSG_PROCESS_CONTINUE_READING
;
3199 ossl_statem_set_error(s
);
3200 return MSG_PROCESS_ERROR
;
3204 #define SSLV2_CIPHER_LEN 3
3206 STACK_OF(SSL_CIPHER
) *ssl_bytes_to_cipher_list(SSL
*s
,
3207 PACKET
*cipher_suites
,
3208 STACK_OF(SSL_CIPHER
) **skp
,
3209 int sslv2format
, int *al
3212 const SSL_CIPHER
*c
;
3213 STACK_OF(SSL_CIPHER
) *sk
;
3215 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
3216 unsigned char cipher
[SSLV2_CIPHER_LEN
];
3218 s
->s3
->send_connection_binding
= 0;
3220 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
3222 if (PACKET_remaining(cipher_suites
) == 0) {
3223 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
, SSL_R_NO_CIPHERS_SPECIFIED
);
3224 *al
= SSL_AD_ILLEGAL_PARAMETER
;
3228 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
3229 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
,
3230 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
3231 *al
= SSL_AD_DECODE_ERROR
;
3235 if ((skp
== NULL
) || (*skp
== NULL
)) {
3236 sk
= sk_SSL_CIPHER_new_null(); /* change perhaps later */
3238 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
, ERR_R_MALLOC_FAILURE
);
3239 *al
= SSL_AD_INTERNAL_ERROR
;
3244 sk_SSL_CIPHER_zero(sk
);
3247 if (!PACKET_memdup(cipher_suites
, &s
->s3
->tmp
.ciphers_raw
,
3248 &s
->s3
->tmp
.ciphers_rawlen
)) {
3249 *al
= SSL_AD_INTERNAL_ERROR
;
3253 while (PACKET_copy_bytes(cipher_suites
, cipher
, n
)) {
3255 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
3256 * first byte set to zero, while true SSLv2 ciphers have a non-zero
3257 * first byte. We don't support any true SSLv2 ciphers, so skip them.
3259 if (sslv2format
&& cipher
[0] != '\0')
3262 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3263 if ((cipher
[n
- 2] == ((SSL3_CK_SCSV
>> 8) & 0xff)) &&
3264 (cipher
[n
- 1] == (SSL3_CK_SCSV
& 0xff))) {
3265 /* SCSV fatal if renegotiating */
3266 if (s
->renegotiate
) {
3267 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
,
3268 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
3269 *al
= SSL_AD_HANDSHAKE_FAILURE
;
3272 s
->s3
->send_connection_binding
= 1;
3273 #ifdef OPENSSL_RI_DEBUG
3274 fprintf(stderr
, "SCSV received by server\n");
3279 /* Check for TLS_FALLBACK_SCSV */
3280 if ((cipher
[n
- 2] == ((SSL3_CK_FALLBACK_SCSV
>> 8) & 0xff)) &&
3281 (cipher
[n
- 1] == (SSL3_CK_FALLBACK_SCSV
& 0xff))) {
3283 * The SCSV indicates that the client previously tried a higher
3284 * version. Fail if the current version is an unexpected
3287 if (!SSL_ctrl(s
, SSL_CTRL_CHECK_PROTO_VERSION
, 0, NULL
)) {
3288 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
,
3289 SSL_R_INAPPROPRIATE_FALLBACK
);
3290 *al
= SSL_AD_INAPPROPRIATE_FALLBACK
;
3296 /* For SSLv2-compat, ignore leading 0-byte. */
3297 c
= ssl_get_cipher_by_char(s
, sslv2format
? &cipher
[1] : cipher
);
3299 if (!sk_SSL_CIPHER_push(sk
, c
)) {
3300 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
, ERR_R_MALLOC_FAILURE
);
3301 *al
= SSL_AD_INTERNAL_ERROR
;
3306 if (PACKET_remaining(cipher_suites
) > 0) {
3307 *al
= SSL_AD_INTERNAL_ERROR
;
3308 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
, ERR_R_INTERNAL_ERROR
);
3316 if ((skp
== NULL
) || (*skp
== NULL
))
3317 sk_SSL_CIPHER_free(sk
);