2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
23 /* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
51 #include "../ssl_locl.h"
52 #include "statem_locl.h"
53 #include "internal/constant_time_locl.h"
54 #include <openssl/buffer.h>
55 #include <openssl/rand.h>
56 #include <openssl/objects.h>
57 #include <openssl/evp.h>
58 #include <openssl/hmac.h>
59 #include <openssl/x509.h>
60 #include <openssl/dh.h>
61 #include <openssl/bn.h>
62 #include <openssl/md5.h>
64 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
);
65 static int tls_construct_hello_retry_request(SSL
*s
, WPACKET
*pkt
);
68 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
69 * handshake state transitions when a TLSv1.3 server is reading messages from
70 * the client. The message type that the client has sent is provided in |mt|.
71 * The current state is in |s->statem.hand_state|.
73 * Return values are 1 for success (transition allowed) and 0 on error
74 * (transition not allowed)
76 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
78 OSSL_STATEM
*st
= &s
->statem
;
81 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
82 * not negotiated TLSv1.3 yet, so that case is handled by
83 * ossl_statem_server_read_transition()
85 switch (st
->hand_state
) {
89 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
90 if (mt
== SSL3_MT_CLIENT_HELLO
) {
91 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
96 case TLS_ST_EARLY_DATA
:
97 if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
98 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
99 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
106 case TLS_ST_SR_END_OF_EARLY_DATA
:
107 case TLS_ST_SW_FINISHED
:
108 if (s
->s3
->tmp
.cert_request
) {
109 if (mt
== SSL3_MT_CERTIFICATE
) {
110 st
->hand_state
= TLS_ST_SR_CERT
;
114 if (mt
== SSL3_MT_FINISHED
) {
115 st
->hand_state
= TLS_ST_SR_FINISHED
;
122 if (s
->session
->peer
== NULL
) {
123 if (mt
== SSL3_MT_FINISHED
) {
124 st
->hand_state
= TLS_ST_SR_FINISHED
;
128 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
129 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
135 case TLS_ST_SR_CERT_VRFY
:
136 if (mt
== SSL3_MT_FINISHED
) {
137 st
->hand_state
= TLS_ST_SR_FINISHED
;
144 * Its never ok to start processing handshake messages in the middle of
145 * early data (i.e. before we've received the end of early data alert)
147 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
149 if (mt
== SSL3_MT_KEY_UPDATE
) {
150 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
156 /* No valid transition found */
161 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
162 * handshake state transitions when the server is reading messages from the
163 * client. The message type that the client has sent is provided in |mt|. The
164 * current state is in |s->statem.hand_state|.
166 * Return values are 1 for success (transition allowed) and 0 on error
167 * (transition not allowed)
169 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
171 OSSL_STATEM
*st
= &s
->statem
;
173 if (SSL_IS_TLS13(s
)) {
174 if (!ossl_statem_server13_read_transition(s
, mt
))
179 switch (st
->hand_state
) {
185 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
186 if (mt
== SSL3_MT_CLIENT_HELLO
) {
187 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
192 case TLS_ST_SW_SRVR_DONE
:
194 * If we get a CKE message after a ServerDone then either
195 * 1) We didn't request a Certificate
197 * 2) If we did request one then
198 * a) We allow no Certificate to be returned
200 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
201 * list if we requested a certificate)
203 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
204 if (s
->s3
->tmp
.cert_request
) {
205 if (s
->version
== SSL3_VERSION
) {
206 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
207 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
209 * This isn't an unexpected message as such - we're just
210 * not going to accept it because we require a client
213 ssl3_send_alert(s
, SSL3_AL_FATAL
,
214 SSL3_AD_HANDSHAKE_FAILURE
);
215 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
216 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
219 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
223 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
226 } else if (s
->s3
->tmp
.cert_request
) {
227 if (mt
== SSL3_MT_CERTIFICATE
) {
228 st
->hand_state
= TLS_ST_SR_CERT
;
235 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
236 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
241 case TLS_ST_SR_KEY_EXCH
:
243 * We should only process a CertificateVerify message if we have
244 * received a Certificate from the client. If so then |s->session->peer|
245 * will be non NULL. In some instances a CertificateVerify message is
246 * not required even if the peer has sent a Certificate (e.g. such as in
247 * the case of static DH). In that case |st->no_cert_verify| should be
250 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
251 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
253 * For the ECDH ciphersuites when the client sends its ECDH
254 * pub key in a certificate, the CertificateVerify message is
255 * not sent. Also for GOST ciphersuites when the client uses
256 * its key from the certificate for key exchange.
258 st
->hand_state
= TLS_ST_SR_CHANGE
;
262 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
263 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
269 case TLS_ST_SR_CERT_VRFY
:
270 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
271 st
->hand_state
= TLS_ST_SR_CHANGE
;
276 case TLS_ST_SR_CHANGE
:
277 #ifndef OPENSSL_NO_NEXTPROTONEG
278 if (s
->s3
->npn_seen
) {
279 if (mt
== SSL3_MT_NEXT_PROTO
) {
280 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
285 if (mt
== SSL3_MT_FINISHED
) {
286 st
->hand_state
= TLS_ST_SR_FINISHED
;
289 #ifndef OPENSSL_NO_NEXTPROTONEG
294 #ifndef OPENSSL_NO_NEXTPROTONEG
295 case TLS_ST_SR_NEXT_PROTO
:
296 if (mt
== SSL3_MT_FINISHED
) {
297 st
->hand_state
= TLS_ST_SR_FINISHED
;
303 case TLS_ST_SW_FINISHED
:
304 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
305 st
->hand_state
= TLS_ST_SR_CHANGE
;
312 /* No valid transition found */
313 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL3_AD_UNEXPECTED_MESSAGE
);
314 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
, SSL_R_UNEXPECTED_MESSAGE
);
319 * Should we send a ServerKeyExchange message?
321 * Valid return values are:
325 static int send_server_key_exchange(SSL
*s
)
327 unsigned long alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
330 * only send a ServerKeyExchange if DH or fortezza but we have a
331 * sign only certificate PSK: may send PSK identity hints For
332 * ECC ciphersuites, we send a serverKeyExchange message only if
333 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
334 * the server certificate contains the server's public key for
337 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
339 * PSK: send ServerKeyExchange if PSK identity hint if
342 #ifndef OPENSSL_NO_PSK
343 /* Only send SKE if we have identity hint for plain PSK */
344 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
345 && s
->cert
->psk_identity_hint
)
346 /* For other PSK always send SKE */
347 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
349 #ifndef OPENSSL_NO_SRP
350 /* SRP: send ServerKeyExchange */
351 || (alg_k
& SSL_kSRP
)
361 * Should we send a CertificateRequest message?
363 * Valid return values are:
367 static int send_certificate_request(SSL
*s
)
370 /* don't request cert unless asked for it: */
371 s
->verify_mode
& SSL_VERIFY_PEER
373 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
374 * during re-negotiation:
376 && (s
->s3
->tmp
.finish_md_len
== 0 ||
377 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
379 * never request cert in anonymous ciphersuites (see
380 * section "Certificate request" in SSL 3 drafts and in
383 && (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
385 * ... except when the application insists on
386 * verification (against the specs, but statem_clnt.c accepts
389 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
390 /* don't request certificate for SRP auth */
391 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
393 * With normal PSK Certificates and Certificate Requests
396 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
404 * ossl_statem_server13_write_transition() works out what handshake state to
405 * move to next when a TLSv1.3 server is writing messages to be sent to the
408 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
410 OSSL_STATEM
*st
= &s
->statem
;
413 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
414 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
417 switch (st
->hand_state
) {
419 /* Shouldn't happen */
420 return WRITE_TRAN_ERROR
;
423 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
424 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
425 return WRITE_TRAN_CONTINUE
;
427 /* Try to read from the client instead */
428 return WRITE_TRAN_FINISHED
;
430 case TLS_ST_SR_CLNT_HELLO
:
431 if (s
->hello_retry_request
)
432 st
->hand_state
= TLS_ST_SW_HELLO_RETRY_REQUEST
;
434 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
435 return WRITE_TRAN_CONTINUE
;
437 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
438 return WRITE_TRAN_FINISHED
;
440 case TLS_ST_SW_SRVR_HELLO
:
441 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
442 return WRITE_TRAN_CONTINUE
;
444 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
446 st
->hand_state
= TLS_ST_SW_FINISHED
;
447 else if (send_certificate_request(s
))
448 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
450 st
->hand_state
= TLS_ST_SW_CERT
;
452 return WRITE_TRAN_CONTINUE
;
454 case TLS_ST_SW_CERT_REQ
:
455 st
->hand_state
= TLS_ST_SW_CERT
;
456 return WRITE_TRAN_CONTINUE
;
459 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
460 return WRITE_TRAN_CONTINUE
;
462 case TLS_ST_SW_CERT_VRFY
:
463 st
->hand_state
= TLS_ST_SW_FINISHED
;
464 return WRITE_TRAN_CONTINUE
;
466 case TLS_ST_SW_FINISHED
:
467 st
->hand_state
= TLS_ST_EARLY_DATA
;
468 return WRITE_TRAN_CONTINUE
;
470 case TLS_ST_EARLY_DATA
:
471 return WRITE_TRAN_FINISHED
;
473 case TLS_ST_SR_FINISHED
:
475 * Technically we have finished the handshake at this point, but we're
476 * going to remain "in_init" for now and write out the session ticket
478 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
479 * and give the application the opportunity to delay sending the
482 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
483 return WRITE_TRAN_CONTINUE
;
485 case TLS_ST_SR_KEY_UPDATE
:
486 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
487 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
488 return WRITE_TRAN_CONTINUE
;
492 case TLS_ST_SW_KEY_UPDATE
:
493 case TLS_ST_SW_SESSION_TICKET
:
494 st
->hand_state
= TLS_ST_OK
;
495 return WRITE_TRAN_CONTINUE
;
500 * ossl_statem_server_write_transition() works out what handshake state to move
501 * to next when the server is writing messages to be sent to the client.
503 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
505 OSSL_STATEM
*st
= &s
->statem
;
508 * Note that before the ClientHello we don't know what version we are going
509 * to negotiate yet, so we don't take this branch until later
513 return ossl_statem_server13_write_transition(s
);
515 switch (st
->hand_state
) {
517 /* Shouldn't happen */
518 return WRITE_TRAN_ERROR
;
521 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
522 /* We must be trying to renegotiate */
523 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
524 st
->request_state
= TLS_ST_BEFORE
;
525 return WRITE_TRAN_CONTINUE
;
527 /* Must be an incoming ClientHello */
528 if (!tls_setup_handshake(s
)) {
529 ossl_statem_set_error(s
);
530 return WRITE_TRAN_ERROR
;
535 /* Just go straight to trying to read from the client */
536 return WRITE_TRAN_FINISHED
;
538 case TLS_ST_SW_HELLO_REQ
:
539 st
->hand_state
= TLS_ST_OK
;
540 return WRITE_TRAN_CONTINUE
;
542 case TLS_ST_SR_CLNT_HELLO
:
543 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
544 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
))
545 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
547 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
548 return WRITE_TRAN_CONTINUE
;
550 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
551 return WRITE_TRAN_FINISHED
;
553 case TLS_ST_SW_SRVR_HELLO
:
555 if (s
->ext
.ticket_expected
)
556 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
558 st
->hand_state
= TLS_ST_SW_CHANGE
;
560 /* Check if it is anon DH or anon ECDH, */
561 /* normal PSK or SRP */
562 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
&
563 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
564 st
->hand_state
= TLS_ST_SW_CERT
;
565 } else if (send_server_key_exchange(s
)) {
566 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
567 } else if (send_certificate_request(s
)) {
568 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
570 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
573 return WRITE_TRAN_CONTINUE
;
576 if (s
->ext
.status_expected
) {
577 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
578 return WRITE_TRAN_CONTINUE
;
582 case TLS_ST_SW_CERT_STATUS
:
583 if (send_server_key_exchange(s
)) {
584 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
585 return WRITE_TRAN_CONTINUE
;
589 case TLS_ST_SW_KEY_EXCH
:
590 if (send_certificate_request(s
)) {
591 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
592 return WRITE_TRAN_CONTINUE
;
596 case TLS_ST_SW_CERT_REQ
:
597 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
598 return WRITE_TRAN_CONTINUE
;
600 case TLS_ST_SW_SRVR_DONE
:
601 return WRITE_TRAN_FINISHED
;
603 case TLS_ST_SR_FINISHED
:
605 st
->hand_state
= TLS_ST_OK
;
606 return WRITE_TRAN_CONTINUE
;
607 } else if (s
->ext
.ticket_expected
) {
608 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
610 st
->hand_state
= TLS_ST_SW_CHANGE
;
612 return WRITE_TRAN_CONTINUE
;
614 case TLS_ST_SW_SESSION_TICKET
:
615 st
->hand_state
= TLS_ST_SW_CHANGE
;
616 return WRITE_TRAN_CONTINUE
;
618 case TLS_ST_SW_CHANGE
:
619 st
->hand_state
= TLS_ST_SW_FINISHED
;
620 return WRITE_TRAN_CONTINUE
;
622 case TLS_ST_SW_FINISHED
:
624 return WRITE_TRAN_FINISHED
;
626 st
->hand_state
= TLS_ST_OK
;
627 return WRITE_TRAN_CONTINUE
;
632 * Perform any pre work that needs to be done prior to sending a message from
633 * the server to the client.
635 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
637 OSSL_STATEM
*st
= &s
->statem
;
639 switch (st
->hand_state
) {
641 /* No pre work to be done */
644 case TLS_ST_SW_HELLO_REQ
:
647 dtls1_clear_sent_buffer(s
);
650 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
652 if (SSL_IS_DTLS(s
)) {
653 dtls1_clear_sent_buffer(s
);
654 /* We don't buffer this message so don't use the timer */
659 case TLS_ST_SW_SRVR_HELLO
:
660 if (SSL_IS_DTLS(s
)) {
662 * Messages we write from now on should be buffered and
663 * retransmitted if necessary, so we need to use the timer now
669 case TLS_ST_SW_SRVR_DONE
:
670 #ifndef OPENSSL_NO_SCTP
671 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
)))
672 return dtls_wait_for_dry(s
);
674 return WORK_FINISHED_CONTINUE
;
676 case TLS_ST_SW_SESSION_TICKET
:
677 if (SSL_IS_TLS13(s
)) {
679 * Actually this is the end of the handshake, but we're going
680 * straight into writing the session ticket out. So we finish off
681 * the handshake, but keep the various buffers active.
683 return tls_finish_handshake(s
, wst
, 0);
684 } if (SSL_IS_DTLS(s
)) {
686 * We're into the last flight. We don't retransmit the last flight
687 * unless we need to, so we don't use the timer
693 case TLS_ST_SW_CHANGE
:
694 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
695 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
696 ossl_statem_set_error(s
);
699 if (SSL_IS_DTLS(s
)) {
701 * We're into the last flight. We don't retransmit the last flight
702 * unless we need to, so we don't use the timer. This might have
703 * already been set to 0 if we sent a NewSessionTicket message,
704 * but we'll set it again here in case we didn't.
708 return WORK_FINISHED_CONTINUE
;
710 case TLS_ST_EARLY_DATA
:
711 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
)
712 return WORK_FINISHED_CONTINUE
;
716 return tls_finish_handshake(s
, wst
, 1);
719 return WORK_FINISHED_CONTINUE
;
723 * Perform any work that needs to be done after sending a message from the
724 * server to the client.
726 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
728 OSSL_STATEM
*st
= &s
->statem
;
732 switch (st
->hand_state
) {
734 /* No post work to be done */
737 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
738 if (statem_flush(s
) != 1)
742 case TLS_ST_SW_HELLO_REQ
:
743 if (statem_flush(s
) != 1)
745 if (!ssl3_init_finished_mac(s
)) {
746 ossl_statem_set_error(s
);
751 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
752 if (statem_flush(s
) != 1)
754 /* HelloVerifyRequest resets Finished MAC */
755 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
756 ossl_statem_set_error(s
);
760 * The next message should be another ClientHello which we need to
761 * treat like it was the first packet
766 case TLS_ST_SW_SRVR_HELLO
:
767 #ifndef OPENSSL_NO_SCTP
768 if (SSL_IS_DTLS(s
) && s
->hit
) {
769 unsigned char sctpauthkey
[64];
770 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
773 * Add new shared key for SCTP-Auth, will be ignored if no
776 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
777 sizeof(DTLS1_SCTP_AUTH_LABEL
));
779 if (SSL_export_keying_material(s
, sctpauthkey
,
780 sizeof(sctpauthkey
), labelbuffer
,
781 sizeof(labelbuffer
), NULL
, 0,
783 ossl_statem_set_error(s
);
787 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
788 sizeof(sctpauthkey
), sctpauthkey
);
792 * TODO(TLS1.3): This actually causes a problem. We don't yet know
793 * whether the next record we are going to receive is an unencrypted
794 * alert, or an encrypted handshake message. We're going to need
795 * something clever in the record layer for this.
797 if (SSL_IS_TLS13(s
)) {
798 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
799 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
800 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
803 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
804 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
805 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
))
810 case TLS_ST_SW_CHANGE
:
811 #ifndef OPENSSL_NO_SCTP
812 if (SSL_IS_DTLS(s
) && !s
->hit
) {
814 * Change to new shared key of SCTP-Auth, will be ignored if
817 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
821 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
822 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
824 ossl_statem_set_error(s
);
829 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
832 case TLS_ST_SW_SRVR_DONE
:
833 if (statem_flush(s
) != 1)
837 case TLS_ST_SW_FINISHED
:
838 if (statem_flush(s
) != 1)
840 #ifndef OPENSSL_NO_SCTP
841 if (SSL_IS_DTLS(s
) && s
->hit
) {
843 * Change to new shared key of SCTP-Auth, will be ignored if
846 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
850 if (SSL_IS_TLS13(s
)) {
851 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
852 s
->master_secret
, s
->handshake_secret
, 0,
853 &s
->session
->master_key_length
)
854 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
855 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
860 case TLS_ST_SW_KEY_UPDATE
:
861 if (statem_flush(s
) != 1)
863 if (!tls13_update_key(s
, 1))
867 case TLS_ST_SW_SESSION_TICKET
:
868 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1)
873 return WORK_FINISHED_CONTINUE
;
877 * Get the message construction function and message type for sending from the
880 * Valid return values are:
884 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
885 confunc_f
*confunc
, int *mt
)
887 OSSL_STATEM
*st
= &s
->statem
;
889 switch (st
->hand_state
) {
891 /* Shouldn't happen */
894 case TLS_ST_SW_CHANGE
:
896 *confunc
= dtls_construct_change_cipher_spec
;
898 *confunc
= tls_construct_change_cipher_spec
;
899 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
902 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
903 *confunc
= dtls_construct_hello_verify_request
;
904 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
907 case TLS_ST_SW_HELLO_REQ
:
908 /* No construction function needed */
910 *mt
= SSL3_MT_HELLO_REQUEST
;
913 case TLS_ST_SW_SRVR_HELLO
:
914 *confunc
= tls_construct_server_hello
;
915 *mt
= SSL3_MT_SERVER_HELLO
;
919 *confunc
= tls_construct_server_certificate
;
920 *mt
= SSL3_MT_CERTIFICATE
;
923 case TLS_ST_SW_CERT_VRFY
:
924 *confunc
= tls_construct_cert_verify
;
925 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
929 case TLS_ST_SW_KEY_EXCH
:
930 *confunc
= tls_construct_server_key_exchange
;
931 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
934 case TLS_ST_SW_CERT_REQ
:
935 *confunc
= tls_construct_certificate_request
;
936 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
939 case TLS_ST_SW_SRVR_DONE
:
940 *confunc
= tls_construct_server_done
;
941 *mt
= SSL3_MT_SERVER_DONE
;
944 case TLS_ST_SW_SESSION_TICKET
:
945 *confunc
= tls_construct_new_session_ticket
;
946 *mt
= SSL3_MT_NEWSESSION_TICKET
;
949 case TLS_ST_SW_CERT_STATUS
:
950 *confunc
= tls_construct_cert_status
;
951 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
954 case TLS_ST_SW_FINISHED
:
955 *confunc
= tls_construct_finished
;
956 *mt
= SSL3_MT_FINISHED
;
959 case TLS_ST_EARLY_DATA
:
964 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
965 *confunc
= tls_construct_encrypted_extensions
;
966 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
969 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
970 *confunc
= tls_construct_hello_retry_request
;
971 *mt
= SSL3_MT_HELLO_RETRY_REQUEST
;
974 case TLS_ST_SW_KEY_UPDATE
:
975 *confunc
= tls_construct_key_update
;
976 *mt
= SSL3_MT_KEY_UPDATE
;
984 * Maximum size (excluding the Handshake header) of a ClientHello message,
985 * calculated as follows:
987 * 2 + # client_version
988 * 32 + # only valid length for random
989 * 1 + # length of session_id
990 * 32 + # maximum size for session_id
991 * 2 + # length of cipher suites
992 * 2^16-2 + # maximum length of cipher suites array
993 * 1 + # length of compression_methods
994 * 2^8-1 + # maximum length of compression methods
995 * 2 + # length of extensions
996 * 2^16-1 # maximum length of extensions
998 #define CLIENT_HELLO_MAX_LENGTH 131396
1000 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1001 #define NEXT_PROTO_MAX_LENGTH 514
1004 * Returns the maximum allowed length for the current message that we are
1005 * reading. Excludes the message header.
1007 size_t ossl_statem_server_max_message_size(SSL
*s
)
1009 OSSL_STATEM
*st
= &s
->statem
;
1011 switch (st
->hand_state
) {
1013 /* Shouldn't happen */
1016 case TLS_ST_SR_CLNT_HELLO
:
1017 return CLIENT_HELLO_MAX_LENGTH
;
1019 case TLS_ST_SR_END_OF_EARLY_DATA
:
1020 return END_OF_EARLY_DATA_MAX_LENGTH
;
1022 case TLS_ST_SR_CERT
:
1023 return s
->max_cert_list
;
1025 case TLS_ST_SR_KEY_EXCH
:
1026 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1028 case TLS_ST_SR_CERT_VRFY
:
1029 return SSL3_RT_MAX_PLAIN_LENGTH
;
1031 #ifndef OPENSSL_NO_NEXTPROTONEG
1032 case TLS_ST_SR_NEXT_PROTO
:
1033 return NEXT_PROTO_MAX_LENGTH
;
1036 case TLS_ST_SR_CHANGE
:
1037 return CCS_MAX_LENGTH
;
1039 case TLS_ST_SR_FINISHED
:
1040 return FINISHED_MAX_LENGTH
;
1042 case TLS_ST_SR_KEY_UPDATE
:
1043 return KEY_UPDATE_MAX_LENGTH
;
1048 * Process a message that the server has received from the client.
1050 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1052 OSSL_STATEM
*st
= &s
->statem
;
1054 switch (st
->hand_state
) {
1056 /* Shouldn't happen */
1057 return MSG_PROCESS_ERROR
;
1059 case TLS_ST_SR_CLNT_HELLO
:
1060 return tls_process_client_hello(s
, pkt
);
1062 case TLS_ST_SR_END_OF_EARLY_DATA
:
1063 return tls_process_end_of_early_data(s
, pkt
);
1065 case TLS_ST_SR_CERT
:
1066 return tls_process_client_certificate(s
, pkt
);
1068 case TLS_ST_SR_KEY_EXCH
:
1069 return tls_process_client_key_exchange(s
, pkt
);
1071 case TLS_ST_SR_CERT_VRFY
:
1072 return tls_process_cert_verify(s
, pkt
);
1074 #ifndef OPENSSL_NO_NEXTPROTONEG
1075 case TLS_ST_SR_NEXT_PROTO
:
1076 return tls_process_next_proto(s
, pkt
);
1079 case TLS_ST_SR_CHANGE
:
1080 return tls_process_change_cipher_spec(s
, pkt
);
1082 case TLS_ST_SR_FINISHED
:
1083 return tls_process_finished(s
, pkt
);
1085 case TLS_ST_SR_KEY_UPDATE
:
1086 return tls_process_key_update(s
, pkt
);
1092 * Perform any further processing required following the receipt of a message
1095 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1097 OSSL_STATEM
*st
= &s
->statem
;
1099 switch (st
->hand_state
) {
1101 /* Shouldn't happen */
1104 case TLS_ST_SR_CLNT_HELLO
:
1105 return tls_post_process_client_hello(s
, wst
);
1107 case TLS_ST_SR_KEY_EXCH
:
1108 return tls_post_process_client_key_exchange(s
, wst
);
1110 return WORK_FINISHED_CONTINUE
;
1113 #ifndef OPENSSL_NO_SRP
1114 static int ssl_check_srp_ext_ClientHello(SSL
*s
, int *al
)
1116 int ret
= SSL_ERROR_NONE
;
1118 *al
= SSL_AD_UNRECOGNIZED_NAME
;
1120 if ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1121 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1122 if (s
->srp_ctx
.login
== NULL
) {
1124 * RFC 5054 says SHOULD reject, we do so if There is no srp
1127 ret
= SSL3_AL_FATAL
;
1128 *al
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
1130 ret
= SSL_srp_server_param_with_username(s
, al
);
1137 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1140 /* Always use DTLS 1.0 version: see RFC 6347 */
1141 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1142 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1148 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1150 unsigned int cookie_leni
;
1151 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1152 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1153 &cookie_leni
) == 0 ||
1154 cookie_leni
> 255) {
1155 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1156 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1159 s
->d1
->cookie_len
= cookie_leni
;
1161 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1162 s
->d1
->cookie_len
)) {
1163 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
, ERR_R_INTERNAL_ERROR
);
1170 #ifndef OPENSSL_NO_EC
1172 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1173 * SecureTransport using the TLS extension block in |hello|.
1174 * Safari, since 10.6, sends exactly these extensions, in this order:
1178 * signature_algorithms (for TLSv1.2 only)
1180 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1181 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1182 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1183 * 10.8..10.8.3 (which don't work).
1185 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1187 static const unsigned char kSafariExtensionsBlock
[] = {
1188 0x00, 0x0a, /* elliptic_curves extension */
1189 0x00, 0x08, /* 8 bytes */
1190 0x00, 0x06, /* 6 bytes of curve ids */
1191 0x00, 0x17, /* P-256 */
1192 0x00, 0x18, /* P-384 */
1193 0x00, 0x19, /* P-521 */
1195 0x00, 0x0b, /* ec_point_formats */
1196 0x00, 0x02, /* 2 bytes */
1197 0x01, /* 1 point format */
1198 0x00, /* uncompressed */
1199 /* The following is only present in TLS 1.2 */
1200 0x00, 0x0d, /* signature_algorithms */
1201 0x00, 0x0c, /* 12 bytes */
1202 0x00, 0x0a, /* 10 bytes */
1203 0x05, 0x01, /* SHA-384/RSA */
1204 0x04, 0x01, /* SHA-256/RSA */
1205 0x02, 0x01, /* SHA-1/RSA */
1206 0x04, 0x03, /* SHA-256/ECDSA */
1207 0x02, 0x03, /* SHA-1/ECDSA */
1209 /* Length of the common prefix (first two extensions). */
1210 static const size_t kSafariCommonExtensionsLength
= 18;
1215 tmppkt
= hello
->extensions
;
1217 if (!PACKET_forward(&tmppkt
, 2)
1218 || !PACKET_get_net_2(&tmppkt
, &type
)
1219 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1223 if (type
!= TLSEXT_TYPE_server_name
)
1226 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1227 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1229 s
->s3
->is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1232 #endif /* !OPENSSL_NO_EC */
1234 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1236 int al
= SSL_AD_INTERNAL_ERROR
;
1237 /* |cookie| will only be initialized for DTLS. */
1238 PACKET session_id
, compression
, extensions
, cookie
;
1239 static const unsigned char null_compression
= 0;
1240 CLIENTHELLO_MSG
*clienthello
;
1242 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1243 if (clienthello
== NULL
) {
1244 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1247 /* Check if this is actually an unexpected renegotiation ClientHello */
1248 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1249 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
)) {
1250 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1258 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1260 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1261 PACKET_null_init(&cookie
);
1263 if (clienthello
->isv2
) {
1266 if (!SSL_IS_FIRST_HANDSHAKE(s
) || s
->hello_retry_request
) {
1267 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1268 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1273 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1274 * header is sent directly on the wire, not wrapped as a TLS
1275 * record. Our record layer just processes the message length and passes
1276 * the rest right through. Its format is:
1278 * 0-1 msg_length - decoded by the record layer
1279 * 2 msg_type - s->init_msg points here
1281 * 5-6 cipher_spec_length
1282 * 7-8 session_id_length
1283 * 9-10 challenge_length
1287 if (!PACKET_get_1(pkt
, &mt
)
1288 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1290 * Should never happen. We should have tested this in the record
1291 * layer in order to have determined that this is a SSLv2 record
1292 * in the first place
1294 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1299 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1300 al
= SSL_AD_DECODE_ERROR
;
1301 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
1305 /* Parse the message and load client random. */
1306 if (clienthello
->isv2
) {
1308 * Handle an SSLv2 backwards compatible ClientHello
1309 * Note, this is only for SSLv3+ using the backward compatible format.
1310 * Real SSLv2 is not supported, and is rejected below.
1312 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1315 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1316 || !PACKET_get_net_2(pkt
, &session_id_len
)
1317 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1318 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1319 SSL_R_RECORD_LENGTH_MISMATCH
);
1320 al
= SSL_AD_DECODE_ERROR
;
1324 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1325 al
= SSL_AD_ILLEGAL_PARAMETER
;
1326 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1330 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1332 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1333 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1334 /* No extensions. */
1335 || PACKET_remaining(pkt
) != 0) {
1336 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1337 SSL_R_RECORD_LENGTH_MISMATCH
);
1338 al
= SSL_AD_DECODE_ERROR
;
1341 clienthello
->session_id_len
= session_id_len
;
1343 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1344 * here rather than sizeof(clienthello->random) because that is the limit
1345 * for SSLv3 and it is fixed. It won't change even if
1346 * sizeof(clienthello->random) does.
1348 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1349 ? SSL3_RANDOM_SIZE
: challenge_len
;
1350 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1351 if (!PACKET_copy_bytes(&challenge
,
1352 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1353 challenge_len
, challenge_len
)
1354 /* Advertise only null compression. */
1355 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1356 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1357 al
= SSL_AD_INTERNAL_ERROR
;
1361 PACKET_null_init(&clienthello
->extensions
);
1363 /* Regular ClientHello. */
1364 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1365 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1366 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1367 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1368 &clienthello
->session_id_len
)) {
1369 al
= SSL_AD_DECODE_ERROR
;
1370 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1374 if (SSL_IS_DTLS(s
)) {
1375 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1376 al
= SSL_AD_DECODE_ERROR
;
1377 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1380 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1381 DTLS1_COOKIE_LENGTH
,
1382 &clienthello
->dtls_cookie_len
)) {
1383 al
= SSL_AD_INTERNAL_ERROR
;
1384 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1388 * If we require cookies and this ClientHello doesn't contain one,
1389 * just return since we do not want to allocate any memory yet.
1390 * So check cookie length...
1392 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1393 if (clienthello
->dtls_cookie_len
== 0)
1398 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1399 al
= SSL_AD_DECODE_ERROR
;
1400 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1404 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1405 al
= SSL_AD_DECODE_ERROR
;
1406 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1410 /* Could be empty. */
1411 if (PACKET_remaining(pkt
) == 0) {
1412 PACKET_null_init(&clienthello
->extensions
);
1414 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1415 || PACKET_remaining(pkt
) != 0) {
1416 al
= SSL_AD_DECODE_ERROR
;
1417 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1423 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1424 MAX_COMPRESSIONS_SIZE
,
1425 &clienthello
->compressions_len
)) {
1426 al
= SSL_AD_INTERNAL_ERROR
;
1427 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1431 /* Preserve the raw extensions PACKET for later use */
1432 extensions
= clienthello
->extensions
;
1433 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1434 &clienthello
->pre_proc_exts
, &al
,
1435 &clienthello
->pre_proc_exts_len
, 1)) {
1436 /* SSLerr already been called */
1439 s
->clienthello
= clienthello
;
1441 return MSG_PROCESS_CONTINUE_PROCESSING
;
1443 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1445 ossl_statem_set_error(s
);
1447 if (clienthello
!= NULL
)
1448 OPENSSL_free(clienthello
->pre_proc_exts
);
1449 OPENSSL_free(clienthello
);
1451 return MSG_PROCESS_ERROR
;
1454 static int tls_early_post_process_client_hello(SSL
*s
, int *pal
)
1457 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1461 #ifndef OPENSSL_NO_COMP
1462 SSL_COMP
*comp
= NULL
;
1464 const SSL_CIPHER
*c
;
1465 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1466 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1467 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1468 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1470 /* Finished parsing the ClientHello, now we can start processing it */
1471 /* Give the early callback a crack at things */
1472 if (s
->ctx
->early_cb
!= NULL
) {
1474 /* A failure in the early callback terminates the connection. */
1475 code
= s
->ctx
->early_cb(s
, &al
, s
->ctx
->early_cb_arg
);
1479 s
->rwstate
= SSL_EARLY_WORK
;
1484 /* Set up the client_random */
1485 memcpy(s
->s3
->client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1487 /* Choose the version */
1489 if (clienthello
->isv2
) {
1490 if (clienthello
->legacy_version
== SSL2_VERSION
1491 || (clienthello
->legacy_version
& 0xff00)
1492 != (SSL3_VERSION_MAJOR
<< 8)) {
1494 * This is real SSLv2 or something complete unknown. We don't
1497 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_UNKNOWN_PROTOCOL
);
1501 s
->client_version
= clienthello
->legacy_version
;
1504 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1505 * versions are potentially compatible. Version negotiation comes later.
1507 if (!SSL_IS_DTLS(s
)) {
1508 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1509 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1510 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1511 protverr
= SSL_R_VERSION_TOO_LOW
;
1517 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1518 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1519 /* like ssl3_get_record, send alert using remote version number */
1520 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1522 al
= SSL_AD_PROTOCOL_VERSION
;
1526 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1527 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1528 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1529 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1530 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1534 if (SSL_IS_DTLS(s
)) {
1535 /* Empty cookie was already handled above by returning early. */
1536 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1537 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1538 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1539 clienthello
->dtls_cookie_len
) == 0) {
1540 al
= SSL_AD_HANDSHAKE_FAILURE
;
1541 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1542 SSL_R_COOKIE_MISMATCH
);
1544 /* else cookie verification succeeded */
1546 /* default verification */
1547 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1548 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1549 s
->d1
->cookie_len
) != 0) {
1550 al
= SSL_AD_HANDSHAKE_FAILURE
;
1551 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_COOKIE_MISMATCH
);
1554 s
->d1
->cookie_verified
= 1;
1556 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1557 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1558 if (protverr
!= 0) {
1559 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1560 s
->version
= s
->client_version
;
1561 al
= SSL_AD_PROTOCOL_VERSION
;
1569 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1570 clienthello
->isv2
, &al
) ||
1571 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1572 clienthello
->isv2
, &al
)) {
1576 s
->s3
->send_connection_binding
= 0;
1577 /* Check what signalling cipher-suite values were received. */
1578 if (scsvs
!= NULL
) {
1579 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1580 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1581 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1582 if (s
->renegotiate
) {
1583 /* SCSV is fatal if renegotiating */
1584 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1585 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1586 al
= SSL_AD_HANDSHAKE_FAILURE
;
1589 s
->s3
->send_connection_binding
= 1;
1590 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1591 !ssl_check_version_downgrade(s
)) {
1593 * This SCSV indicates that the client previously tried
1594 * a higher version. We should fail if the current version
1595 * is an unexpected downgrade, as that indicates that the first
1596 * connection may have been tampered with in order to trigger
1597 * an insecure downgrade.
1599 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1600 SSL_R_INAPPROPRIATE_FALLBACK
);
1601 al
= SSL_AD_INAPPROPRIATE_FALLBACK
;
1607 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1608 if (SSL_IS_TLS13(s
)) {
1609 const SSL_CIPHER
*cipher
=
1610 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(s
));
1612 if (cipher
== NULL
) {
1613 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1614 SSL_R_NO_SHARED_CIPHER
);
1615 al
= SSL_AD_HANDSHAKE_FAILURE
;
1618 if (s
->hello_retry_request
&& s
->s3
->tmp
.new_cipher
!= NULL
1619 && s
->s3
->tmp
.new_cipher
->id
!= cipher
->id
) {
1621 * A previous HRR picked a different ciphersuite to the one we
1622 * just selected. Something must have changed.
1624 al
= SSL_AD_ILLEGAL_PARAMETER
;
1625 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_BAD_CIPHER
);
1628 s
->s3
->tmp
.new_cipher
= cipher
;
1631 /* We need to do this before getting the session */
1632 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1633 SSL_EXT_CLIENT_HELLO
,
1634 clienthello
->pre_proc_exts
, NULL
, 0, &al
)) {
1635 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1640 * We don't allow resumption in a backwards compatible ClientHello.
1641 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1643 * Versions before 0.9.7 always allow clients to resume sessions in
1644 * renegotiation. 0.9.7 and later allow this by default, but optionally
1645 * ignore resumption requests with flag
1646 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1647 * than a change to default behavior so that applications relying on
1648 * this for security won't even compile against older library versions).
1649 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1650 * request renegotiation but not a new session (s->new_session remains
1651 * unset): for servers, this essentially just means that the
1652 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1655 if (clienthello
->isv2
||
1657 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1658 if (!ssl_get_new_session(s
, 1))
1661 i
= ssl_get_prev_session(s
, clienthello
, &al
);
1663 /* previous session */
1665 } else if (i
== -1) {
1669 if (!ssl_get_new_session(s
, 1))
1675 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1676 * ciphersuite compatibility with the session as part of resumption.
1678 if (!SSL_IS_TLS13(s
) && s
->hit
) {
1680 id
= s
->session
->cipher
->id
;
1683 fprintf(stderr
, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers
));
1685 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1686 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1688 fprintf(stderr
, "client [%2d of %2d]:%s\n",
1689 i
, sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1698 * we need to have the cipher in the cipher list if we are asked
1701 al
= SSL_AD_ILLEGAL_PARAMETER
;
1702 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1703 SSL_R_REQUIRED_CIPHER_MISSING
);
1708 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1709 if (clienthello
->compressions
[loop
] == 0)
1713 if (loop
>= clienthello
->compressions_len
) {
1715 al
= SSL_AD_DECODE_ERROR
;
1716 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1720 #ifndef OPENSSL_NO_EC
1721 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1722 ssl_check_for_safari(s
, clienthello
);
1723 #endif /* !OPENSSL_NO_EC */
1725 /* TLS extensions */
1726 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1727 clienthello
->pre_proc_exts
, NULL
, 0, &al
, 1)) {
1728 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_PARSE_TLSEXT
);
1733 * Check if we want to use external pre-shared secret for this handshake
1734 * for not reused session only. We need to generate server_random before
1735 * calling tls_session_secret_cb in order to allow SessionTicket
1736 * processing to use it in key derivation.
1740 pos
= s
->s3
->server_random
;
1741 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1747 && s
->version
>= TLS1_VERSION
1750 && s
->ext
.session_secret_cb
) {
1751 const SSL_CIPHER
*pref_cipher
= NULL
;
1753 * s->session->master_key_length is a size_t, but this is an int for
1754 * backwards compat reasons
1756 int master_key_length
;
1758 master_key_length
= sizeof(s
->session
->master_key
);
1759 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1760 &master_key_length
, ciphers
,
1762 s
->ext
.session_secret_cb_arg
)
1763 && master_key_length
> 0) {
1764 s
->session
->master_key_length
= master_key_length
;
1766 s
->session
->ciphers
= ciphers
;
1767 s
->session
->verify_result
= X509_V_OK
;
1771 /* check if some cipher was preferred by call back */
1772 if (pref_cipher
== NULL
)
1773 pref_cipher
= ssl3_choose_cipher(s
, s
->session
->ciphers
,
1774 SSL_get_ciphers(s
));
1775 if (pref_cipher
== NULL
) {
1776 al
= SSL_AD_HANDSHAKE_FAILURE
;
1777 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_NO_SHARED_CIPHER
);
1781 s
->session
->cipher
= pref_cipher
;
1782 sk_SSL_CIPHER_free(s
->cipher_list
);
1783 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1784 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1785 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1790 * Worst case, we will use the NULL compression, but if we have other
1791 * options, we will now look for them. We have complen-1 compression
1792 * algorithms from the client, starting at q.
1794 s
->s3
->tmp
.new_compression
= NULL
;
1795 if (SSL_IS_TLS13(s
)) {
1797 * We already checked above that the NULL compression method appears in
1798 * the list. Now we check there aren't any others (which is illegal in
1799 * a TLSv1.3 ClientHello.
1801 if (clienthello
->compressions_len
!= 1) {
1802 al
= SSL_AD_ILLEGAL_PARAMETER
;
1803 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1804 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1808 #ifndef OPENSSL_NO_COMP
1809 /* This only happens if we have a cache hit */
1810 else if (s
->session
->compress_meth
!= 0) {
1811 int m
, comp_id
= s
->session
->compress_meth
;
1813 /* Perform sanity checks on resumed compression algorithm */
1814 /* Can't disable compression */
1815 if (!ssl_allow_compression(s
)) {
1816 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1817 SSL_R_INCONSISTENT_COMPRESSION
);
1820 /* Look for resumed compression method */
1821 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
1822 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1823 if (comp_id
== comp
->id
) {
1824 s
->s3
->tmp
.new_compression
= comp
;
1828 if (s
->s3
->tmp
.new_compression
== NULL
) {
1829 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1830 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1833 /* Look for resumed method in compression list */
1834 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
1835 if (clienthello
->compressions
[k
] == comp_id
)
1838 if (k
>= clienthello
->compressions_len
) {
1839 al
= SSL_AD_ILLEGAL_PARAMETER
;
1840 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1841 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
1844 } else if (s
->hit
) {
1846 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
1847 /* See if we have a match */
1848 int m
, nn
, v
, done
= 0;
1851 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1852 for (m
= 0; m
< nn
; m
++) {
1853 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1855 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
1856 if (v
== clienthello
->compressions
[o
]) {
1865 s
->s3
->tmp
.new_compression
= comp
;
1871 * If compression is disabled we'd better not try to resume a session
1872 * using compression.
1874 if (s
->session
->compress_meth
!= 0) {
1875 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1881 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1884 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
1885 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1886 s
->session
->ciphers
= ciphers
;
1887 if (ciphers
== NULL
) {
1888 al
= SSL_AD_INTERNAL_ERROR
;
1889 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1896 #ifdef OPENSSL_NO_COMP
1897 s
->session
->compress_meth
= 0;
1899 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
1901 if (!tls1_set_server_sigalgs(s
)) {
1902 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1907 sk_SSL_CIPHER_free(ciphers
);
1908 sk_SSL_CIPHER_free(scsvs
);
1909 OPENSSL_free(clienthello
->pre_proc_exts
);
1910 OPENSSL_free(s
->clienthello
);
1911 s
->clienthello
= NULL
;
1914 ossl_statem_set_error(s
);
1917 sk_SSL_CIPHER_free(ciphers
);
1918 sk_SSL_CIPHER_free(scsvs
);
1919 OPENSSL_free(clienthello
->pre_proc_exts
);
1920 OPENSSL_free(s
->clienthello
);
1921 s
->clienthello
= NULL
;
1927 * Call the status request callback if needed. Upon success, returns 1.
1928 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
1930 static int tls_handle_status_request(SSL
*s
, int *al
)
1932 s
->ext
.status_expected
= 0;
1935 * If status request then ask callback what to do. Note: this must be
1936 * called after servername callbacks in case the certificate has changed,
1937 * and must be called after the cipher has been chosen because this may
1938 * influence which certificate is sent
1940 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
1941 && s
->ctx
->ext
.status_cb
!= NULL
) {
1944 /* If no certificate can't return certificate status */
1945 if (s
->s3
->tmp
.cert
!= NULL
) {
1947 * Set current certificate to one we will use so SSL_get_certificate
1948 * et al can pick it up.
1950 s
->cert
->key
= s
->s3
->tmp
.cert
;
1951 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
1953 /* We don't want to send a status request response */
1954 case SSL_TLSEXT_ERR_NOACK
:
1955 s
->ext
.status_expected
= 0;
1957 /* status request response should be sent */
1958 case SSL_TLSEXT_ERR_OK
:
1959 if (s
->ext
.ocsp
.resp
)
1960 s
->ext
.status_expected
= 1;
1962 /* something bad happened */
1963 case SSL_TLSEXT_ERR_ALERT_FATAL
:
1965 *al
= SSL_AD_INTERNAL_ERROR
;
1974 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
1976 int al
= SSL_AD_HANDSHAKE_FAILURE
;
1977 const SSL_CIPHER
*cipher
;
1979 if (wst
== WORK_MORE_A
) {
1980 int rv
= tls_early_post_process_client_hello(s
, &al
);
1982 /* SSLErr() was already called */
1989 if (wst
== WORK_MORE_B
) {
1990 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
1991 /* Let cert callback update server certificates if required */
1992 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
1993 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
1995 al
= SSL_AD_INTERNAL_ERROR
;
1996 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
1997 SSL_R_CERT_CB_ERROR
);
2001 s
->rwstate
= SSL_X509_LOOKUP
;
2004 s
->rwstate
= SSL_NOTHING
;
2007 /* In TLSv1.3 we selected the ciphersuite before resumption */
2008 if (!SSL_IS_TLS13(s
)) {
2010 ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
2012 if (cipher
== NULL
) {
2013 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2014 SSL_R_NO_SHARED_CIPHER
);
2017 s
->s3
->tmp
.new_cipher
= cipher
;
2020 if (!tls_choose_sigalg(s
, &al
))
2022 /* check whether we should disable session resumption */
2023 if (s
->not_resumable_session_cb
!= NULL
)
2024 s
->session
->not_resumable
=
2025 s
->not_resumable_session_cb(s
, ((cipher
->algorithm_mkey
2026 & (SSL_kDHE
| SSL_kECDHE
))
2028 if (s
->session
->not_resumable
)
2029 /* do not send a session ticket */
2030 s
->ext
.ticket_expected
= 0;
2033 /* Session-id reuse */
2034 s
->s3
->tmp
.new_cipher
= s
->session
->cipher
;
2038 * we now have the following setup.
2040 * cipher_list - our preferred list of ciphers
2041 * ciphers - the clients preferred list of ciphers
2042 * compression - basically ignored right now
2043 * ssl version is set - sslv3
2044 * s->session - The ssl session has been setup.
2045 * s->hit - session reuse flag
2046 * s->s3->tmp.new_cipher- the new cipher to use.
2050 * Call status_request callback if needed. Has to be done after the
2051 * certificate callbacks etc above.
2053 if (!tls_handle_status_request(s
, &al
)) {
2054 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2055 SSL_R_CLIENTHELLO_TLSEXT
);
2061 #ifndef OPENSSL_NO_SRP
2062 if (wst
== WORK_MORE_C
) {
2064 if ((ret
= ssl_check_srp_ext_ClientHello(s
, &al
)) < 0) {
2066 * callback indicates further work to be done
2068 s
->rwstate
= SSL_X509_LOOKUP
;
2071 if (ret
!= SSL_ERROR_NONE
) {
2073 * This is not really an error but the only means to for
2074 * a client to detect whether srp is supported.
2076 if (al
!= TLS1_AD_UNKNOWN_PSK_IDENTITY
)
2077 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2078 SSL_R_CLIENTHELLO_TLSEXT
);
2080 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2081 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2087 return WORK_FINISHED_STOP
;
2089 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2090 ossl_statem_set_error(s
);
2094 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2096 int compm
, al
= SSL_AD_INTERNAL_ERROR
;
2100 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
2101 version
= SSL_IS_TLS13(s
) ? TLS1_3_VERSION_DRAFT
: s
->version
;
2102 if (!WPACKET_put_bytes_u16(pkt
, version
)
2104 * Random stuff. Filling of the server_random takes place in
2105 * tls_process_client_hello()
2107 || !WPACKET_memcpy(pkt
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
)) {
2108 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
2113 * There are several cases for the session ID to send
2114 * back in the server hello:
2115 * - For session reuse from the session cache,
2116 * we send back the old session ID.
2117 * - If stateless session reuse (using a session ticket)
2118 * is successful, we send back the client's "session ID"
2119 * (which doesn't actually identify the session).
2120 * - If it is a new session, we send back the new
2122 * - However, if we want the new session to be single-use,
2123 * we send back a 0-length session ID.
2124 * s->hit is non-zero in either case of session reuse,
2125 * so the following won't overwrite an ID that we're supposed
2128 if (s
->session
->not_resumable
||
2129 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2131 s
->session
->session_id_length
= 0;
2133 sl
= s
->session
->session_id_length
;
2134 if (sl
> sizeof(s
->session
->session_id
)) {
2135 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
2139 /* set up the compression method */
2140 #ifdef OPENSSL_NO_COMP
2143 if (s
->s3
->tmp
.new_compression
== NULL
)
2146 compm
= s
->s3
->tmp
.new_compression
->id
;
2149 if ((!SSL_IS_TLS13(s
)
2150 && !WPACKET_sub_memcpy_u8(pkt
, s
->session
->session_id
, sl
))
2151 || !s
->method
->put_cipher_by_char(s
->s3
->tmp
.new_cipher
, pkt
, &len
)
2152 || (!SSL_IS_TLS13(s
)
2153 && !WPACKET_put_bytes_u8(pkt
, compm
))
2154 || !tls_construct_extensions(s
, pkt
,
2156 ? SSL_EXT_TLS1_3_SERVER_HELLO
2157 : SSL_EXT_TLS1_2_SERVER_HELLO
,
2159 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
2163 if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2164 && !ssl3_digest_cached_records(s
, 0)) {
2165 al
= SSL_AD_INTERNAL_ERROR
;
2171 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2175 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2177 if (!s
->s3
->tmp
.cert_request
) {
2178 if (!ssl3_digest_cached_records(s
, 0)) {
2179 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2186 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2188 #ifndef OPENSSL_NO_DH
2189 EVP_PKEY
*pkdh
= NULL
;
2191 #ifndef OPENSSL_NO_EC
2192 unsigned char *encodedPoint
= NULL
;
2193 size_t encodedlen
= 0;
2196 const SIGALG_LOOKUP
*lu
= s
->s3
->tmp
.sigalg
;
2197 int al
= SSL_AD_INTERNAL_ERROR
, i
;
2200 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2201 EVP_PKEY_CTX
*pctx
= NULL
;
2202 size_t paramlen
, paramoffset
;
2204 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2205 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2209 if (md_ctx
== NULL
) {
2210 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2214 type
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2216 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2217 #ifndef OPENSSL_NO_PSK
2218 /* Plain PSK or RSAPSK nothing to do */
2219 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2221 #endif /* !OPENSSL_NO_PSK */
2222 #ifndef OPENSSL_NO_DH
2223 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2224 CERT
*cert
= s
->cert
;
2226 EVP_PKEY
*pkdhp
= NULL
;
2229 if (s
->cert
->dh_tmp_auto
) {
2230 DH
*dhp
= ssl_get_auto_dh(s
);
2231 pkdh
= EVP_PKEY_new();
2232 if (pkdh
== NULL
|| dhp
== NULL
) {
2234 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2235 ERR_R_INTERNAL_ERROR
);
2238 EVP_PKEY_assign_DH(pkdh
, dhp
);
2241 pkdhp
= cert
->dh_tmp
;
2243 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2244 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2245 pkdh
= ssl_dh_to_pkey(dhp
);
2247 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2248 ERR_R_INTERNAL_ERROR
);
2253 if (pkdhp
== NULL
) {
2254 al
= SSL_AD_INTERNAL_ERROR
;
2255 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2256 SSL_R_MISSING_TMP_DH_KEY
);
2259 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2260 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2261 al
= SSL_AD_HANDSHAKE_FAILURE
;
2262 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2263 SSL_R_DH_KEY_TOO_SMALL
);
2266 if (s
->s3
->tmp
.pkey
!= NULL
) {
2267 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2268 ERR_R_INTERNAL_ERROR
);
2272 s
->s3
->tmp
.pkey
= ssl_generate_pkey(pkdhp
);
2274 if (s
->s3
->tmp
.pkey
== NULL
) {
2275 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
2279 dh
= EVP_PKEY_get0_DH(s
->s3
->tmp
.pkey
);
2281 EVP_PKEY_free(pkdh
);
2284 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2285 DH_get0_key(dh
, &r
[2], NULL
);
2288 #ifndef OPENSSL_NO_EC
2289 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2292 if (s
->s3
->tmp
.pkey
!= NULL
) {
2293 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2294 ERR_R_INTERNAL_ERROR
);
2298 /* Get NID of appropriate shared curve */
2299 nid
= tls1_shared_group(s
, -2);
2300 curve_id
= tls1_ec_nid2curve_id(nid
);
2301 if (curve_id
== 0) {
2302 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2303 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2306 s
->s3
->tmp
.pkey
= ssl_generate_pkey_curve(curve_id
);
2307 /* Generate a new key for this curve */
2308 if (s
->s3
->tmp
.pkey
== NULL
) {
2309 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
2313 /* Encode the public key. */
2314 encodedlen
= EVP_PKEY_get1_tls_encodedpoint(s
->s3
->tmp
.pkey
,
2316 if (encodedlen
== 0) {
2317 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2322 * We'll generate the serverKeyExchange message explicitly so we
2323 * can set these to NULLs
2330 #endif /* !OPENSSL_NO_EC */
2331 #ifndef OPENSSL_NO_SRP
2332 if (type
& SSL_kSRP
) {
2333 if ((s
->srp_ctx
.N
== NULL
) ||
2334 (s
->srp_ctx
.g
== NULL
) ||
2335 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2336 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2337 SSL_R_MISSING_SRP_PARAM
);
2340 r
[0] = s
->srp_ctx
.N
;
2341 r
[1] = s
->srp_ctx
.g
;
2342 r
[2] = s
->srp_ctx
.s
;
2343 r
[3] = s
->srp_ctx
.B
;
2347 al
= SSL_AD_INTERNAL_ERROR
;
2348 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2349 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2353 if (((s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2354 || ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2356 } else if (lu
== NULL
) {
2357 al
= SSL_AD_DECODE_ERROR
;
2361 #ifndef OPENSSL_NO_PSK
2362 if (type
& SSL_PSK
) {
2363 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2364 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2367 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2368 * checked this when we set the identity hint - but just in case
2370 if (len
> PSK_MAX_IDENTITY_LEN
2371 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2373 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2374 ERR_R_INTERNAL_ERROR
);
2380 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2381 unsigned char *binval
;
2384 #ifndef OPENSSL_NO_SRP
2385 if ((i
== 2) && (type
& SSL_kSRP
)) {
2386 res
= WPACKET_start_sub_packet_u8(pkt
);
2389 res
= WPACKET_start_sub_packet_u16(pkt
);
2392 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2393 ERR_R_INTERNAL_ERROR
);
2397 #ifndef OPENSSL_NO_DH
2399 * for interoperability with some versions of the Microsoft TLS
2400 * stack, we need to zero pad the DHE pub key to the same length
2403 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2404 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2407 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2408 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2409 ERR_R_INTERNAL_ERROR
);
2412 memset(binval
, 0, len
);
2416 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2417 || !WPACKET_close(pkt
)) {
2418 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2419 ERR_R_INTERNAL_ERROR
);
2423 BN_bn2bin(r
[i
], binval
);
2426 #ifndef OPENSSL_NO_EC
2427 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2429 * We only support named (not generic) curves. In this situation, the
2430 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2431 * [1 byte length of encoded point], followed by the actual encoded
2434 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2435 || !WPACKET_put_bytes_u8(pkt
, 0)
2436 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2437 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2438 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2439 ERR_R_INTERNAL_ERROR
);
2442 OPENSSL_free(encodedPoint
);
2443 encodedPoint
= NULL
;
2449 EVP_PKEY
*pkey
= s
->s3
->tmp
.cert
->privatekey
;
2450 const EVP_MD
*md
= ssl_md(lu
->hash_idx
);
2451 unsigned char *sigbytes1
, *sigbytes2
;
2454 if (pkey
== NULL
|| md
== NULL
) {
2455 /* Should never happen */
2456 al
= SSL_AD_INTERNAL_ERROR
;
2457 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2458 ERR_R_INTERNAL_ERROR
);
2462 * n is the length of the params, they start at &(d[4]) and p
2463 * points to the space at the end.
2466 /* Get length of the parameters we have written above */
2467 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2468 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2469 ERR_R_INTERNAL_ERROR
);
2472 /* send signature algorithm */
2473 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
))
2476 * Create the signature. We don't know the actual length of the sig
2477 * until after we've created it, so we reserve enough bytes for it
2478 * up front, and then properly allocate them in the WPACKET
2481 siglen
= EVP_PKEY_size(pkey
);
2482 if (!WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2483 || EVP_DigestSignInit(md_ctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
2484 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2485 ERR_R_INTERNAL_ERROR
);
2488 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2489 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2490 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2491 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2496 if (EVP_DigestSignUpdate(md_ctx
, &(s
->s3
->client_random
[0]),
2497 SSL3_RANDOM_SIZE
) <= 0
2498 || EVP_DigestSignUpdate(md_ctx
, &(s
->s3
->server_random
[0]),
2499 SSL3_RANDOM_SIZE
) <= 0
2500 || EVP_DigestSignUpdate(md_ctx
,
2501 s
->init_buf
->data
+ paramoffset
,
2503 || EVP_DigestSignFinal(md_ctx
, sigbytes1
, &siglen
) <= 0
2504 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2505 || sigbytes1
!= sigbytes2
) {
2506 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2507 ERR_R_INTERNAL_ERROR
);
2512 EVP_MD_CTX_free(md_ctx
);
2515 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2517 #ifndef OPENSSL_NO_DH
2518 EVP_PKEY_free(pkdh
);
2520 #ifndef OPENSSL_NO_EC
2521 OPENSSL_free(encodedPoint
);
2523 EVP_MD_CTX_free(md_ctx
);
2527 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2529 int al
= SSL_AD_INTERNAL_ERROR
;
2531 if (SSL_IS_TLS13(s
)) {
2532 /* TODO(TLS1.3) for now send empty request context */
2533 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2534 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2535 ERR_R_INTERNAL_ERROR
);
2539 if (!tls_construct_extensions(s
, pkt
,
2540 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2542 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2543 ERR_R_INTERNAL_ERROR
);
2549 /* get the list of acceptable cert types */
2550 if (!WPACKET_start_sub_packet_u8(pkt
)
2551 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2552 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2556 if (SSL_USE_SIGALGS(s
)) {
2557 const uint16_t *psigs
;
2558 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2560 if (!WPACKET_start_sub_packet_u16(pkt
)
2561 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2562 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2563 || !WPACKET_close(pkt
)) {
2564 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2565 ERR_R_INTERNAL_ERROR
);
2570 if (!construct_ca_names(s
, pkt
)) {
2571 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2576 s
->s3
->tmp
.cert_request
= 1;
2579 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2583 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
, int *al
)
2585 #ifndef OPENSSL_NO_PSK
2586 unsigned char psk
[PSK_MAX_PSK_LEN
];
2588 PACKET psk_identity
;
2590 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2591 *al
= SSL_AD_DECODE_ERROR
;
2592 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, SSL_R_LENGTH_MISMATCH
);
2595 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2596 *al
= SSL_AD_DECODE_ERROR
;
2597 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, SSL_R_DATA_LENGTH_TOO_LONG
);
2600 if (s
->psk_server_callback
== NULL
) {
2601 *al
= SSL_AD_INTERNAL_ERROR
;
2602 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, SSL_R_PSK_NO_SERVER_CB
);
2606 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2607 *al
= SSL_AD_INTERNAL_ERROR
;
2608 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_INTERNAL_ERROR
);
2612 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2615 if (psklen
> PSK_MAX_PSK_LEN
) {
2616 *al
= SSL_AD_INTERNAL_ERROR
;
2617 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_INTERNAL_ERROR
);
2619 } else if (psklen
== 0) {
2621 * PSK related to the given identity not found
2623 *al
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
2624 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2625 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2629 OPENSSL_free(s
->s3
->tmp
.psk
);
2630 s
->s3
->tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2631 OPENSSL_cleanse(psk
, psklen
);
2633 if (s
->s3
->tmp
.psk
== NULL
) {
2634 *al
= SSL_AD_INTERNAL_ERROR
;
2635 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2639 s
->s3
->tmp
.psklen
= psklen
;
2643 /* Should never happen */
2644 *al
= SSL_AD_INTERNAL_ERROR
;
2645 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_INTERNAL_ERROR
);
2650 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
, int *al
)
2652 #ifndef OPENSSL_NO_RSA
2653 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
2655 unsigned char decrypt_good
, version_good
;
2656 size_t j
, padding_len
;
2657 PACKET enc_premaster
;
2659 unsigned char *rsa_decrypt
= NULL
;
2662 rsa
= EVP_PKEY_get0_RSA(s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
);
2664 *al
= SSL_AD_INTERNAL_ERROR
;
2665 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, SSL_R_MISSING_RSA_CERTIFICATE
);
2669 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2670 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2671 enc_premaster
= *pkt
;
2673 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2674 || PACKET_remaining(pkt
) != 0) {
2675 *al
= SSL_AD_DECODE_ERROR
;
2676 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, SSL_R_LENGTH_MISMATCH
);
2682 * We want to be sure that the plaintext buffer size makes it safe to
2683 * iterate over the entire size of a premaster secret
2684 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2685 * their ciphertext cannot accommodate a premaster secret anyway.
2687 if (RSA_size(rsa
) < SSL_MAX_MASTER_KEY_LENGTH
) {
2688 *al
= SSL_AD_INTERNAL_ERROR
;
2689 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, RSA_R_KEY_SIZE_TOO_SMALL
);
2693 rsa_decrypt
= OPENSSL_malloc(RSA_size(rsa
));
2694 if (rsa_decrypt
== NULL
) {
2695 *al
= SSL_AD_INTERNAL_ERROR
;
2696 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, ERR_R_MALLOC_FAILURE
);
2701 * We must not leak whether a decryption failure occurs because of
2702 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2703 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2704 * generates a random premaster secret for the case that the decrypt
2705 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2708 if (RAND_bytes(rand_premaster_secret
, sizeof(rand_premaster_secret
)) <= 0)
2712 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2713 * the timing-sensitive code below.
2715 /* TODO(size_t): Convert this function */
2716 decrypt_len
= (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster
),
2717 PACKET_data(&enc_premaster
),
2718 rsa_decrypt
, rsa
, RSA_NO_PADDING
);
2719 if (decrypt_len
< 0)
2722 /* Check the padding. See RFC 3447, section 7.2.2. */
2725 * The smallest padded premaster is 11 bytes of overhead. Small keys
2726 * are publicly invalid, so this may return immediately. This ensures
2727 * PS is at least 8 bytes.
2729 if (decrypt_len
< 11 + SSL_MAX_MASTER_KEY_LENGTH
) {
2730 *al
= SSL_AD_DECRYPT_ERROR
;
2731 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, SSL_R_DECRYPTION_FAILED
);
2735 padding_len
= decrypt_len
- SSL_MAX_MASTER_KEY_LENGTH
;
2736 decrypt_good
= constant_time_eq_int_8(rsa_decrypt
[0], 0) &
2737 constant_time_eq_int_8(rsa_decrypt
[1], 2);
2738 for (j
= 2; j
< padding_len
- 1; j
++) {
2739 decrypt_good
&= ~constant_time_is_zero_8(rsa_decrypt
[j
]);
2741 decrypt_good
&= constant_time_is_zero_8(rsa_decrypt
[padding_len
- 1]);
2744 * If the version in the decrypted pre-master secret is correct then
2745 * version_good will be 0xff, otherwise it'll be zero. The
2746 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2747 * (http://eprint.iacr.org/2003/052/) exploits the version number
2748 * check as a "bad version oracle". Thus version checks are done in
2749 * constant time and are treated like any other decryption error.
2752 constant_time_eq_8(rsa_decrypt
[padding_len
],
2753 (unsigned)(s
->client_version
>> 8));
2755 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
2756 (unsigned)(s
->client_version
& 0xff));
2759 * The premaster secret must contain the same version number as the
2760 * ClientHello to detect version rollback attacks (strangely, the
2761 * protocol does not offer such protection for DH ciphersuites).
2762 * However, buggy clients exist that send the negotiated protocol
2763 * version instead if the server does not support the requested
2764 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2767 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) {
2768 unsigned char workaround_good
;
2769 workaround_good
= constant_time_eq_8(rsa_decrypt
[padding_len
],
2770 (unsigned)(s
->version
>> 8));
2772 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
2773 (unsigned)(s
->version
& 0xff));
2774 version_good
|= workaround_good
;
2778 * Both decryption and version must be good for decrypt_good to
2779 * remain non-zero (0xff).
2781 decrypt_good
&= version_good
;
2784 * Now copy rand_premaster_secret over from p using
2785 * decrypt_good_mask. If decryption failed, then p does not
2786 * contain valid plaintext, however, a check above guarantees
2787 * it is still sufficiently large to read from.
2789 for (j
= 0; j
< sizeof(rand_premaster_secret
); j
++) {
2790 rsa_decrypt
[padding_len
+ j
] =
2791 constant_time_select_8(decrypt_good
,
2792 rsa_decrypt
[padding_len
+ j
],
2793 rand_premaster_secret
[j
]);
2796 if (!ssl_generate_master_secret(s
, rsa_decrypt
+ padding_len
,
2797 sizeof(rand_premaster_secret
), 0)) {
2798 *al
= SSL_AD_INTERNAL_ERROR
;
2799 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, ERR_R_INTERNAL_ERROR
);
2805 OPENSSL_free(rsa_decrypt
);
2808 /* Should never happen */
2809 *al
= SSL_AD_INTERNAL_ERROR
;
2810 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, ERR_R_INTERNAL_ERROR
);
2815 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
, int *al
)
2817 #ifndef OPENSSL_NO_DH
2818 EVP_PKEY
*skey
= NULL
;
2822 const unsigned char *data
;
2823 EVP_PKEY
*ckey
= NULL
;
2826 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
2827 *al
= SSL_AD_DECODE_ERROR
;
2828 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
,
2829 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2832 skey
= s
->s3
->tmp
.pkey
;
2834 *al
= SSL_AD_INTERNAL_ERROR
;
2835 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, SSL_R_MISSING_TMP_DH_KEY
);
2839 if (PACKET_remaining(pkt
) == 0L) {
2840 *al
= SSL_AD_DECODE_ERROR
;
2841 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, SSL_R_MISSING_TMP_DH_KEY
);
2844 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
2845 /* We already checked we have enough data */
2846 *al
= SSL_AD_INTERNAL_ERROR
;
2847 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2850 ckey
= EVP_PKEY_new();
2851 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
2852 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, SSL_R_BN_LIB
);
2855 cdh
= EVP_PKEY_get0_DH(ckey
);
2856 pub_key
= BN_bin2bn(data
, i
, NULL
);
2858 if (pub_key
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
2859 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2860 if (pub_key
!= NULL
)
2865 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
2866 *al
= SSL_AD_INTERNAL_ERROR
;
2867 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2872 EVP_PKEY_free(s
->s3
->tmp
.pkey
);
2873 s
->s3
->tmp
.pkey
= NULL
;
2875 EVP_PKEY_free(ckey
);
2878 /* Should never happen */
2879 *al
= SSL_AD_INTERNAL_ERROR
;
2880 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2885 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
, int *al
)
2887 #ifndef OPENSSL_NO_EC
2888 EVP_PKEY
*skey
= s
->s3
->tmp
.pkey
;
2889 EVP_PKEY
*ckey
= NULL
;
2892 if (PACKET_remaining(pkt
) == 0L) {
2893 /* We don't support ECDH client auth */
2894 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2895 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, SSL_R_MISSING_TMP_ECDH_KEY
);
2899 const unsigned char *data
;
2902 * Get client's public key from encoded point in the
2903 * ClientKeyExchange message.
2906 /* Get encoded point length */
2907 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
2908 || PACKET_remaining(pkt
) != 0) {
2909 *al
= SSL_AD_DECODE_ERROR
;
2910 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, SSL_R_LENGTH_MISMATCH
);
2913 ckey
= EVP_PKEY_new();
2914 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
2915 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_EVP_LIB
);
2918 if (EVP_PKEY_set1_tls_encodedpoint(ckey
, data
, i
) == 0) {
2919 *al
= SSL_AD_ILLEGAL_PARAMETER
;
2920 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_EC_LIB
);
2925 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
2926 *al
= SSL_AD_INTERNAL_ERROR
;
2927 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_INTERNAL_ERROR
);
2932 EVP_PKEY_free(s
->s3
->tmp
.pkey
);
2933 s
->s3
->tmp
.pkey
= NULL
;
2935 EVP_PKEY_free(ckey
);
2939 /* Should never happen */
2940 *al
= SSL_AD_INTERNAL_ERROR
;
2941 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_INTERNAL_ERROR
);
2946 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
, int *al
)
2948 #ifndef OPENSSL_NO_SRP
2950 const unsigned char *data
;
2952 if (!PACKET_get_net_2(pkt
, &i
)
2953 || !PACKET_get_bytes(pkt
, &data
, i
)) {
2954 *al
= SSL_AD_DECODE_ERROR
;
2955 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, SSL_R_BAD_SRP_A_LENGTH
);
2958 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
2959 *al
= SSL_AD_INTERNAL_ERROR
;
2960 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_BN_LIB
);
2963 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
2964 *al
= SSL_AD_ILLEGAL_PARAMETER
;
2965 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, SSL_R_BAD_SRP_PARAMETERS
);
2968 OPENSSL_free(s
->session
->srp_username
);
2969 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
2970 if (s
->session
->srp_username
== NULL
) {
2971 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_MALLOC_FAILURE
);
2975 if (!srp_generate_server_master_secret(s
)) {
2976 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_INTERNAL_ERROR
);
2982 /* Should never happen */
2983 *al
= SSL_AD_INTERNAL_ERROR
;
2984 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_INTERNAL_ERROR
);
2989 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
, int *al
)
2991 #ifndef OPENSSL_NO_GOST
2992 EVP_PKEY_CTX
*pkey_ctx
;
2993 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
2994 unsigned char premaster_secret
[32];
2995 const unsigned char *start
;
2996 size_t outlen
= 32, inlen
;
2997 unsigned long alg_a
;
3000 size_t sess_key_len
;
3001 const unsigned char *data
;
3004 /* Get our certificate private key */
3005 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
3006 if (alg_a
& SSL_aGOST12
) {
3008 * New GOST ciphersuites have SSL_aGOST01 bit too
3010 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3012 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3015 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3017 } else if (alg_a
& SSL_aGOST01
) {
3018 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3021 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
3022 if (pkey_ctx
== NULL
) {
3023 *al
= SSL_AD_INTERNAL_ERROR
;
3024 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_MALLOC_FAILURE
);
3027 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3028 *al
= SSL_AD_INTERNAL_ERROR
;
3029 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3033 * If client certificate is present and is of the same type, maybe
3034 * use it for key exchange. Don't mind errors from
3035 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3036 * client certificate for authorization only.
3038 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3039 if (client_pub_pkey
) {
3040 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3043 /* Decrypt session key */
3044 sess_key_len
= PACKET_remaining(pkt
);
3045 if (!PACKET_get_bytes(pkt
, &data
, sess_key_len
)) {
3046 *al
= SSL_AD_INTERNAL_ERROR
;
3047 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3050 /* TODO(size_t): Convert this function */
3051 if (ASN1_get_object((const unsigned char **)&data
, &Tlen
, &Ttag
,
3052 &Tclass
, (long)sess_key_len
) != V_ASN1_CONSTRUCTED
3053 || Ttag
!= V_ASN1_SEQUENCE
|| Tclass
!= V_ASN1_UNIVERSAL
) {
3054 *al
= SSL_AD_DECODE_ERROR
;
3055 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, SSL_R_DECRYPTION_FAILED
);
3060 if (EVP_PKEY_decrypt
3061 (pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3062 *al
= SSL_AD_DECODE_ERROR
;
3063 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, SSL_R_DECRYPTION_FAILED
);
3066 /* Generate master secret */
3067 if (!ssl_generate_master_secret(s
, premaster_secret
,
3068 sizeof(premaster_secret
), 0)) {
3069 *al
= SSL_AD_INTERNAL_ERROR
;
3070 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3073 /* Check if pubkey from client certificate was used */
3074 if (EVP_PKEY_CTX_ctrl
3075 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
3076 s
->statem
.no_cert_verify
= 1;
3080 EVP_PKEY_CTX_free(pkey_ctx
);
3083 /* Should never happen */
3084 *al
= SSL_AD_INTERNAL_ERROR
;
3085 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3090 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3093 unsigned long alg_k
;
3095 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3097 /* For PSK parse and retrieve identity, obtain PSK key */
3098 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
, &al
))
3101 if (alg_k
& SSL_kPSK
) {
3102 /* Identity extracted earlier: should be nothing left */
3103 if (PACKET_remaining(pkt
) != 0) {
3104 al
= SSL_AD_DECODE_ERROR
;
3105 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3106 SSL_R_LENGTH_MISMATCH
);
3109 /* PSK handled by ssl_generate_master_secret */
3110 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3111 al
= SSL_AD_INTERNAL_ERROR
;
3112 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3115 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3116 if (!tls_process_cke_rsa(s
, pkt
, &al
))
3118 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3119 if (!tls_process_cke_dhe(s
, pkt
, &al
))
3121 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3122 if (!tls_process_cke_ecdhe(s
, pkt
, &al
))
3124 } else if (alg_k
& SSL_kSRP
) {
3125 if (!tls_process_cke_srp(s
, pkt
, &al
))
3127 } else if (alg_k
& SSL_kGOST
) {
3128 if (!tls_process_cke_gost(s
, pkt
, &al
))
3131 al
= SSL_AD_INTERNAL_ERROR
;
3132 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3133 SSL_R_UNKNOWN_CIPHER_TYPE
);
3137 return MSG_PROCESS_CONTINUE_PROCESSING
;
3140 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3141 #ifndef OPENSSL_NO_PSK
3142 OPENSSL_clear_free(s
->s3
->tmp
.psk
, s
->s3
->tmp
.psklen
);
3143 s
->s3
->tmp
.psk
= NULL
;
3145 ossl_statem_set_error(s
);
3146 return MSG_PROCESS_ERROR
;
3149 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3151 #ifndef OPENSSL_NO_SCTP
3152 if (wst
== WORK_MORE_A
) {
3153 if (SSL_IS_DTLS(s
)) {
3154 unsigned char sctpauthkey
[64];
3155 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3157 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3160 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3161 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3163 if (SSL_export_keying_material(s
, sctpauthkey
,
3164 sizeof(sctpauthkey
), labelbuffer
,
3165 sizeof(labelbuffer
), NULL
, 0,
3167 ossl_statem_set_error(s
);
3171 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3172 sizeof(sctpauthkey
), sctpauthkey
);
3177 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3179 * No certificate verify or no peer certificate so we no longer need
3180 * the handshake_buffer
3182 if (!ssl3_digest_cached_records(s
, 0)) {
3183 ossl_statem_set_error(s
);
3186 return WORK_FINISHED_CONTINUE
;
3188 if (!s
->s3
->handshake_buffer
) {
3189 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3190 ERR_R_INTERNAL_ERROR
);
3191 ossl_statem_set_error(s
);
3195 * For sigalgs freeze the handshake buffer. If we support
3196 * extms we've done this already so this is a no-op
3198 if (!ssl3_digest_cached_records(s
, 1)) {
3199 ossl_statem_set_error(s
);
3204 return WORK_FINISHED_CONTINUE
;
3207 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3209 int i
, al
= SSL_AD_INTERNAL_ERROR
, ret
= MSG_PROCESS_ERROR
;
3211 unsigned long l
, llen
;
3212 const unsigned char *certstart
, *certbytes
;
3213 STACK_OF(X509
) *sk
= NULL
;
3214 PACKET spkt
, context
;
3217 if ((sk
= sk_X509_new_null()) == NULL
) {
3218 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3222 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3223 if ((SSL_IS_TLS13(s
) && !PACKET_get_length_prefixed_1(pkt
, &context
))
3224 || !PACKET_get_net_3(pkt
, &llen
)
3225 || !PACKET_get_sub_packet(pkt
, &spkt
, llen
)
3226 || PACKET_remaining(pkt
) != 0) {
3227 al
= SSL_AD_DECODE_ERROR
;
3228 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
3232 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3233 if (!PACKET_get_net_3(&spkt
, &l
)
3234 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3235 al
= SSL_AD_DECODE_ERROR
;
3236 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3237 SSL_R_CERT_LENGTH_MISMATCH
);
3241 certstart
= certbytes
;
3242 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, l
);
3244 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3247 if (certbytes
!= (certstart
+ l
)) {
3248 al
= SSL_AD_DECODE_ERROR
;
3249 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3250 SSL_R_CERT_LENGTH_MISMATCH
);
3254 if (SSL_IS_TLS13(s
)) {
3255 RAW_EXTENSION
*rawexts
= NULL
;
3258 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3259 al
= SSL_AD_DECODE_ERROR
;
3260 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, SSL_R_BAD_LENGTH
);
3263 if (!tls_collect_extensions(s
, &extensions
,
3264 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3265 &al
, NULL
, chainidx
== 0)
3266 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3267 rawexts
, x
, chainidx
, &al
,
3268 PACKET_remaining(&spkt
) == 0)) {
3269 OPENSSL_free(rawexts
);
3272 OPENSSL_free(rawexts
);
3275 if (!sk_X509_push(sk
, x
)) {
3276 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3282 if (sk_X509_num(sk
) <= 0) {
3283 /* TLS does not mind 0 certs returned */
3284 if (s
->version
== SSL3_VERSION
) {
3285 al
= SSL_AD_HANDSHAKE_FAILURE
;
3286 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3287 SSL_R_NO_CERTIFICATES_RETURNED
);
3290 /* Fail for TLS only if we required a certificate */
3291 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3292 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3293 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3294 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3295 al
= SSL_AD_CERTIFICATE_REQUIRED
;
3298 /* No client certificate so digest cached records */
3299 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3304 i
= ssl_verify_cert_chain(s
, sk
);
3306 al
= ssl_verify_alarm_type(s
->verify_result
);
3307 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3308 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3312 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3313 al
= SSL_AD_HANDSHAKE_FAILURE
;
3316 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3318 al
= SSL3_AD_HANDSHAKE_FAILURE
;
3319 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3320 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3325 X509_free(s
->session
->peer
);
3326 s
->session
->peer
= sk_X509_shift(sk
);
3327 s
->session
->verify_result
= s
->verify_result
;
3329 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3330 s
->session
->peer_chain
= sk
;
3333 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3336 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3337 al
= SSL_AD_INTERNAL_ERROR
;
3338 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3343 * Inconsistency alert: cert_chain does *not* include the peer's own
3344 * certificate, while we do include it in statem_clnt.c
3348 /* Save the current hash state for when we receive the CertificateVerify */
3350 && !ssl_handshake_hash(s
, s
->cert_verify_hash
,
3351 sizeof(s
->cert_verify_hash
),
3352 &s
->cert_verify_hash_len
)) {
3353 al
= SSL_AD_INTERNAL_ERROR
;
3354 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3358 ret
= MSG_PROCESS_CONTINUE_READING
;
3362 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3363 ossl_statem_set_error(s
);
3366 sk_X509_pop_free(sk
, X509_free
);
3370 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3372 CERT_PKEY
*cpk
= s
->s3
->tmp
.cert
;
3373 int al
= SSL_AD_INTERNAL_ERROR
;
3376 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3381 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3382 * for the server Certificate message
3384 if ((SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0))
3385 || !ssl3_output_cert_chain(s
, pkt
, cpk
, &al
)) {
3386 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3387 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3394 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
3396 unsigned char *senc
= NULL
;
3397 EVP_CIPHER_CTX
*ctx
= NULL
;
3398 HMAC_CTX
*hctx
= NULL
;
3399 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3400 const unsigned char *const_p
;
3401 int len
, slen_full
, slen
, lenfinal
;
3404 SSL_CTX
*tctx
= s
->session_ctx
;
3405 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3406 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3407 int iv_len
, al
= SSL_AD_INTERNAL_ERROR
;
3408 size_t macoffset
, macendoffset
;
3410 unsigned char age_add_c
[sizeof(uint32_t)];
3414 if (SSL_IS_TLS13(s
)) {
3415 if (RAND_bytes(age_add_u
.age_add_c
, sizeof(age_add_u
)) <= 0)
3417 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
3418 s
->session
->time
= (long)time(NULL
);
3419 if (s
->s3
->alpn_selected
!= NULL
) {
3420 OPENSSL_free(s
->session
->ext
.alpn_selected
);
3421 s
->session
->ext
.alpn_selected
=
3422 OPENSSL_memdup(s
->s3
->alpn_selected
, s
->s3
->alpn_selected_len
);
3423 if (s
->session
->ext
.alpn_selected
== NULL
) {
3424 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3425 ERR_R_MALLOC_FAILURE
);
3428 s
->session
->ext
.alpn_selected_len
= s
->s3
->alpn_selected_len
;
3430 s
->session
->ext
.max_early_data
= s
->max_early_data
;
3433 /* get session encoding length */
3434 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3436 * Some length values are 16 bits, so forget it if session is too
3439 if (slen_full
== 0 || slen_full
> 0xFF00) {
3440 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3443 senc
= OPENSSL_malloc(slen_full
);
3445 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
3449 ctx
= EVP_CIPHER_CTX_new();
3450 hctx
= HMAC_CTX_new();
3451 if (ctx
== NULL
|| hctx
== NULL
) {
3452 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
3457 if (!i2d_SSL_SESSION(s
->session
, &p
))
3461 * create a fresh copy (not shared with other threads) to clean up
3464 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3467 sess
->session_id_length
= 0; /* ID is irrelevant for the ticket */
3469 slen
= i2d_SSL_SESSION(sess
, NULL
);
3470 if (slen
== 0 || slen
> slen_full
) { /* shouldn't ever happen */
3471 SSL_SESSION_free(sess
);
3475 if (!i2d_SSL_SESSION(sess
, &p
)) {
3476 SSL_SESSION_free(sess
);
3479 SSL_SESSION_free(sess
);
3482 * Initialize HMAC and cipher contexts. If callback present it does
3483 * all the work otherwise use generated values from parent ctx.
3485 if (tctx
->ext
.ticket_key_cb
) {
3486 /* if 0 is returned, write an empty ticket */
3487 int ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3492 /* Put timeout and length */
3493 if (!WPACKET_put_bytes_u32(pkt
, 0)
3494 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3495 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3496 ERR_R_INTERNAL_ERROR
);
3500 EVP_CIPHER_CTX_free(ctx
);
3501 HMAC_CTX_free(hctx
);
3506 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
3508 const EVP_CIPHER
*cipher
= EVP_aes_256_cbc();
3510 iv_len
= EVP_CIPHER_iv_length(cipher
);
3511 if (RAND_bytes(iv
, iv_len
) <= 0)
3513 if (!EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3514 tctx
->ext
.tick_aes_key
, iv
))
3516 if (!HMAC_Init_ex(hctx
, tctx
->ext
.tick_hmac_key
,
3517 sizeof(tctx
->ext
.tick_hmac_key
),
3518 EVP_sha256(), NULL
))
3520 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3521 sizeof(tctx
->ext
.tick_key_name
));
3525 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3526 * unspecified for resumed session (for simplicity).
3527 * In TLSv1.3 we reset the "time" field above, and always specify the
3530 if (!WPACKET_put_bytes_u32(pkt
,
3531 (s
->hit
&& !SSL_IS_TLS13(s
))
3532 ? 0 : s
->session
->timeout
)
3534 && !WPACKET_put_bytes_u32(pkt
, age_add_u
.age_add
))
3535 /* Now the actual ticket data */
3536 || !WPACKET_start_sub_packet_u16(pkt
)
3537 || !WPACKET_get_total_written(pkt
, &macoffset
)
3538 /* Output key name */
3539 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3541 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3542 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3544 /* Encrypt session data */
3545 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3546 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3547 || encdata1
!= encdata2
3548 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3549 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3550 || encdata1
+ len
!= encdata2
3551 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3552 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3553 || !HMAC_Update(hctx
,
3554 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3555 macendoffset
- macoffset
)
3556 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3557 || !HMAC_Final(hctx
, macdata1
, &hlen
)
3558 || hlen
> EVP_MAX_MD_SIZE
3559 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3560 || macdata1
!= macdata2
3561 || !WPACKET_close(pkt
)
3563 && !tls_construct_extensions(s
, pkt
,
3564 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
3566 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3569 EVP_CIPHER_CTX_free(ctx
);
3570 HMAC_CTX_free(hctx
);
3575 ossl_statem_set_error(s
);
3577 EVP_CIPHER_CTX_free(ctx
);
3578 HMAC_CTX_free(hctx
);
3579 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3584 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3585 * create a separate message. Returns 1 on success or 0 on failure.
3587 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
3589 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
3590 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
3591 s
->ext
.ocsp
.resp_len
)) {
3592 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
, ERR_R_INTERNAL_ERROR
);
3599 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
3601 if (!tls_construct_cert_status_body(s
, pkt
)) {
3602 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3609 #ifndef OPENSSL_NO_NEXTPROTONEG
3611 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3612 * It sets the next_proto member in s if found
3614 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
3616 PACKET next_proto
, padding
;
3617 size_t next_proto_len
;
3618 int al
= SSL_AD_INTERNAL_ERROR
;
3621 * The payload looks like:
3623 * uint8 proto[proto_len];
3624 * uint8 padding_len;
3625 * uint8 padding[padding_len];
3627 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
3628 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
3629 || PACKET_remaining(pkt
) > 0) {
3630 al
= SSL_AD_DECODE_ERROR
;
3631 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO
, SSL_R_LENGTH_MISMATCH
);
3635 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
3640 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
3642 return MSG_PROCESS_CONTINUE_READING
;
3644 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3645 ossl_statem_set_error(s
);
3646 return MSG_PROCESS_ERROR
;
3650 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
3654 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
3656 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3657 SSLerr(SSL_F_TLS_CONSTRUCT_ENCRYPTED_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
3664 static int tls_construct_hello_retry_request(SSL
*s
, WPACKET
*pkt
)
3666 int al
= SSL_AD_INTERNAL_ERROR
;
3670 * TODO(TLS1.3): Remove the DRAFT version before release
3671 * (should be s->version)
3673 if (!WPACKET_put_bytes_u16(pkt
, TLS1_3_VERSION_DRAFT
)
3674 || !s
->method
->put_cipher_by_char(s
->s3
->tmp
.new_cipher
, pkt
, &len
)
3675 || !tls_construct_extensions(s
, pkt
,
3676 SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
,
3678 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST
, ERR_R_INTERNAL_ERROR
);
3682 /* Ditch the session. We'll create a new one next time around */
3683 SSL_SESSION_free(s
->session
);
3688 * Re-initialise the Transcript Hash. We're going to prepopulate it with
3689 * a synthetic message_hash in place of ClientHello1.
3691 if (!create_synthetic_message_hash(s
))
3696 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3700 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL
*s
, PACKET
*pkt
)
3702 int al
= SSL_AD_INTERNAL_ERROR
;
3704 if (PACKET_remaining(pkt
) != 0) {
3705 al
= SSL_AD_DECODE_ERROR
;
3706 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
, SSL_R_LENGTH_MISMATCH
);
3707 ossl_statem_set_error(s
);
3708 return MSG_PROCESS_ERROR
;
3711 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
3712 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
3713 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
, ERR_R_INTERNAL_ERROR
);
3718 * EndOfEarlyData signals a key change so the end of the message must be on
3719 * a record boundary.
3721 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
3722 al
= SSL_AD_UNEXPECTED_MESSAGE
;
3723 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
3724 SSL_R_NOT_ON_RECORD_BOUNDARY
);
3728 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
3729 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
3730 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
3731 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
, ERR_R_INTERNAL_ERROR
);
3735 return MSG_PROCESS_CONTINUE_READING
;
3737 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3738 ossl_statem_set_error(s
);
3739 return MSG_PROCESS_ERROR
;