2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
6 * Licensed under the OpenSSL license (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
13 #include "../ssl_locl.h"
14 #include "statem_locl.h"
15 #include "internal/constant_time_locl.h"
16 #include <openssl/buffer.h>
17 #include <openssl/rand.h>
18 #include <openssl/objects.h>
19 #include <openssl/evp.h>
20 #include <openssl/hmac.h>
21 #include <openssl/x509.h>
22 #include <openssl/dh.h>
23 #include <openssl/bn.h>
24 #include <openssl/md5.h>
26 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
);
29 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
30 * handshake state transitions when a TLSv1.3 server is reading messages from
31 * the client. The message type that the client has sent is provided in |mt|.
32 * The current state is in |s->statem.hand_state|.
34 * Return values are 1 for success (transition allowed) and 0 on error
35 * (transition not allowed)
37 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
39 OSSL_STATEM
*st
= &s
->statem
;
42 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
43 * not negotiated TLSv1.3 yet, so that case is handled by
44 * ossl_statem_server_read_transition()
46 switch (st
->hand_state
) {
50 case TLS_ST_EARLY_DATA
:
51 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
52 if (mt
== SSL3_MT_CLIENT_HELLO
) {
53 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
57 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
58 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
59 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
66 case TLS_ST_SR_END_OF_EARLY_DATA
:
67 case TLS_ST_SW_FINISHED
:
68 if (s
->s3
->tmp
.cert_request
) {
69 if (mt
== SSL3_MT_CERTIFICATE
) {
70 st
->hand_state
= TLS_ST_SR_CERT
;
74 if (mt
== SSL3_MT_FINISHED
) {
75 st
->hand_state
= TLS_ST_SR_FINISHED
;
82 if (s
->session
->peer
== NULL
) {
83 if (mt
== SSL3_MT_FINISHED
) {
84 st
->hand_state
= TLS_ST_SR_FINISHED
;
88 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
89 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
95 case TLS_ST_SR_CERT_VRFY
:
96 if (mt
== SSL3_MT_FINISHED
) {
97 st
->hand_state
= TLS_ST_SR_FINISHED
;
104 * Its never ok to start processing handshake messages in the middle of
105 * early data (i.e. before we've received the end of early data alert)
107 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
109 if (mt
== SSL3_MT_KEY_UPDATE
) {
110 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
116 /* No valid transition found */
121 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
122 * handshake state transitions when the server is reading messages from the
123 * client. The message type that the client has sent is provided in |mt|. The
124 * current state is in |s->statem.hand_state|.
126 * Return values are 1 for success (transition allowed) and 0 on error
127 * (transition not allowed)
129 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
131 OSSL_STATEM
*st
= &s
->statem
;
133 if (SSL_IS_TLS13(s
)) {
134 if (!ossl_statem_server13_read_transition(s
, mt
))
139 switch (st
->hand_state
) {
145 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
146 if (mt
== SSL3_MT_CLIENT_HELLO
) {
147 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
152 case TLS_ST_SW_SRVR_DONE
:
154 * If we get a CKE message after a ServerDone then either
155 * 1) We didn't request a Certificate
157 * 2) If we did request one then
158 * a) We allow no Certificate to be returned
160 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
161 * list if we requested a certificate)
163 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
164 if (s
->s3
->tmp
.cert_request
) {
165 if (s
->version
== SSL3_VERSION
) {
166 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
167 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
169 * This isn't an unexpected message as such - we're just
170 * not going to accept it because we require a client
173 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
174 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
175 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
178 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
182 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
185 } else if (s
->s3
->tmp
.cert_request
) {
186 if (mt
== SSL3_MT_CERTIFICATE
) {
187 st
->hand_state
= TLS_ST_SR_CERT
;
194 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
195 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
200 case TLS_ST_SR_KEY_EXCH
:
202 * We should only process a CertificateVerify message if we have
203 * received a Certificate from the client. If so then |s->session->peer|
204 * will be non NULL. In some instances a CertificateVerify message is
205 * not required even if the peer has sent a Certificate (e.g. such as in
206 * the case of static DH). In that case |st->no_cert_verify| should be
209 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
210 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
212 * For the ECDH ciphersuites when the client sends its ECDH
213 * pub key in a certificate, the CertificateVerify message is
214 * not sent. Also for GOST ciphersuites when the client uses
215 * its key from the certificate for key exchange.
217 st
->hand_state
= TLS_ST_SR_CHANGE
;
221 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
222 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
228 case TLS_ST_SR_CERT_VRFY
:
229 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
230 st
->hand_state
= TLS_ST_SR_CHANGE
;
235 case TLS_ST_SR_CHANGE
:
236 #ifndef OPENSSL_NO_NEXTPROTONEG
237 if (s
->s3
->npn_seen
) {
238 if (mt
== SSL3_MT_NEXT_PROTO
) {
239 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
244 if (mt
== SSL3_MT_FINISHED
) {
245 st
->hand_state
= TLS_ST_SR_FINISHED
;
248 #ifndef OPENSSL_NO_NEXTPROTONEG
253 #ifndef OPENSSL_NO_NEXTPROTONEG
254 case TLS_ST_SR_NEXT_PROTO
:
255 if (mt
== SSL3_MT_FINISHED
) {
256 st
->hand_state
= TLS_ST_SR_FINISHED
;
262 case TLS_ST_SW_FINISHED
:
263 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
264 st
->hand_state
= TLS_ST_SR_CHANGE
;
271 /* No valid transition found */
272 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
,
273 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
274 SSL_R_UNEXPECTED_MESSAGE
);
279 * Should we send a ServerKeyExchange message?
281 * Valid return values are:
285 static int send_server_key_exchange(SSL
*s
)
287 unsigned long alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
290 * only send a ServerKeyExchange if DH or fortezza but we have a
291 * sign only certificate PSK: may send PSK identity hints For
292 * ECC ciphersuites, we send a serverKeyExchange message only if
293 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
294 * the server certificate contains the server's public key for
297 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
299 * PSK: send ServerKeyExchange if PSK identity hint if
302 #ifndef OPENSSL_NO_PSK
303 /* Only send SKE if we have identity hint for plain PSK */
304 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
305 && s
->cert
->psk_identity_hint
)
306 /* For other PSK always send SKE */
307 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
309 #ifndef OPENSSL_NO_SRP
310 /* SRP: send ServerKeyExchange */
311 || (alg_k
& SSL_kSRP
)
321 * Should we send a CertificateRequest message?
323 * Valid return values are:
327 static int send_certificate_request(SSL
*s
)
330 /* don't request cert unless asked for it: */
331 s
->verify_mode
& SSL_VERIFY_PEER
333 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
334 * during re-negotiation:
336 && (s
->s3
->tmp
.finish_md_len
== 0 ||
337 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
339 * never request cert in anonymous ciphersuites (see
340 * section "Certificate request" in SSL 3 drafts and in
343 && (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
345 * ... except when the application insists on
346 * verification (against the specs, but statem_clnt.c accepts
349 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
350 /* don't request certificate for SRP auth */
351 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
353 * With normal PSK Certificates and Certificate Requests
356 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
364 * ossl_statem_server13_write_transition() works out what handshake state to
365 * move to next when a TLSv1.3 server is writing messages to be sent to the
368 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
370 OSSL_STATEM
*st
= &s
->statem
;
373 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
374 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
377 switch (st
->hand_state
) {
379 /* Shouldn't happen */
380 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
381 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION
,
382 ERR_R_INTERNAL_ERROR
);
383 return WRITE_TRAN_ERROR
;
386 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
387 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
388 return WRITE_TRAN_CONTINUE
;
390 /* Try to read from the client instead */
391 return WRITE_TRAN_FINISHED
;
393 case TLS_ST_SR_CLNT_HELLO
:
394 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
395 return WRITE_TRAN_CONTINUE
;
397 case TLS_ST_SW_SRVR_HELLO
:
398 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
399 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
400 st
->hand_state
= TLS_ST_SW_CHANGE
;
401 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
402 st
->hand_state
= TLS_ST_EARLY_DATA
;
404 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
405 return WRITE_TRAN_CONTINUE
;
407 case TLS_ST_SW_CHANGE
:
408 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
409 st
->hand_state
= TLS_ST_EARLY_DATA
;
411 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
412 return WRITE_TRAN_CONTINUE
;
414 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
416 st
->hand_state
= TLS_ST_SW_FINISHED
;
417 else if (send_certificate_request(s
))
418 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
420 st
->hand_state
= TLS_ST_SW_CERT
;
422 return WRITE_TRAN_CONTINUE
;
424 case TLS_ST_SW_CERT_REQ
:
425 st
->hand_state
= TLS_ST_SW_CERT
;
426 return WRITE_TRAN_CONTINUE
;
429 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
430 return WRITE_TRAN_CONTINUE
;
432 case TLS_ST_SW_CERT_VRFY
:
433 st
->hand_state
= TLS_ST_SW_FINISHED
;
434 return WRITE_TRAN_CONTINUE
;
436 case TLS_ST_SW_FINISHED
:
437 st
->hand_state
= TLS_ST_EARLY_DATA
;
438 return WRITE_TRAN_CONTINUE
;
440 case TLS_ST_EARLY_DATA
:
441 return WRITE_TRAN_FINISHED
;
443 case TLS_ST_SR_FINISHED
:
445 * Technically we have finished the handshake at this point, but we're
446 * going to remain "in_init" for now and write out the session ticket
448 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
449 * and give the application the opportunity to delay sending the
452 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
453 return WRITE_TRAN_CONTINUE
;
455 case TLS_ST_SR_KEY_UPDATE
:
456 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
457 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
458 return WRITE_TRAN_CONTINUE
;
462 case TLS_ST_SW_KEY_UPDATE
:
463 case TLS_ST_SW_SESSION_TICKET
:
464 st
->hand_state
= TLS_ST_OK
;
465 return WRITE_TRAN_CONTINUE
;
470 * ossl_statem_server_write_transition() works out what handshake state to move
471 * to next when the server is writing messages to be sent to the client.
473 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
475 OSSL_STATEM
*st
= &s
->statem
;
478 * Note that before the ClientHello we don't know what version we are going
479 * to negotiate yet, so we don't take this branch until later
483 return ossl_statem_server13_write_transition(s
);
485 switch (st
->hand_state
) {
487 /* Shouldn't happen */
488 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
489 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION
,
490 ERR_R_INTERNAL_ERROR
);
491 return WRITE_TRAN_ERROR
;
494 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
495 /* We must be trying to renegotiate */
496 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
497 st
->request_state
= TLS_ST_BEFORE
;
498 return WRITE_TRAN_CONTINUE
;
500 /* Must be an incoming ClientHello */
501 if (!tls_setup_handshake(s
)) {
502 /* SSLfatal() already called */
503 return WRITE_TRAN_ERROR
;
508 /* Just go straight to trying to read from the client */
509 return WRITE_TRAN_FINISHED
;
511 case TLS_ST_SW_HELLO_REQ
:
512 st
->hand_state
= TLS_ST_OK
;
513 return WRITE_TRAN_CONTINUE
;
515 case TLS_ST_SR_CLNT_HELLO
:
516 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
517 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
))
518 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
520 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
521 return WRITE_TRAN_CONTINUE
;
523 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
524 return WRITE_TRAN_FINISHED
;
526 case TLS_ST_SW_SRVR_HELLO
:
528 if (s
->ext
.ticket_expected
)
529 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
531 st
->hand_state
= TLS_ST_SW_CHANGE
;
533 /* Check if it is anon DH or anon ECDH, */
534 /* normal PSK or SRP */
535 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
&
536 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
537 st
->hand_state
= TLS_ST_SW_CERT
;
538 } else if (send_server_key_exchange(s
)) {
539 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
540 } else if (send_certificate_request(s
)) {
541 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
543 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
546 return WRITE_TRAN_CONTINUE
;
549 if (s
->ext
.status_expected
) {
550 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
551 return WRITE_TRAN_CONTINUE
;
555 case TLS_ST_SW_CERT_STATUS
:
556 if (send_server_key_exchange(s
)) {
557 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
558 return WRITE_TRAN_CONTINUE
;
562 case TLS_ST_SW_KEY_EXCH
:
563 if (send_certificate_request(s
)) {
564 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
565 return WRITE_TRAN_CONTINUE
;
569 case TLS_ST_SW_CERT_REQ
:
570 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
571 return WRITE_TRAN_CONTINUE
;
573 case TLS_ST_SW_SRVR_DONE
:
574 return WRITE_TRAN_FINISHED
;
576 case TLS_ST_SR_FINISHED
:
578 st
->hand_state
= TLS_ST_OK
;
579 return WRITE_TRAN_CONTINUE
;
580 } else if (s
->ext
.ticket_expected
) {
581 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
583 st
->hand_state
= TLS_ST_SW_CHANGE
;
585 return WRITE_TRAN_CONTINUE
;
587 case TLS_ST_SW_SESSION_TICKET
:
588 st
->hand_state
= TLS_ST_SW_CHANGE
;
589 return WRITE_TRAN_CONTINUE
;
591 case TLS_ST_SW_CHANGE
:
592 st
->hand_state
= TLS_ST_SW_FINISHED
;
593 return WRITE_TRAN_CONTINUE
;
595 case TLS_ST_SW_FINISHED
:
597 return WRITE_TRAN_FINISHED
;
599 st
->hand_state
= TLS_ST_OK
;
600 return WRITE_TRAN_CONTINUE
;
605 * Perform any pre work that needs to be done prior to sending a message from
606 * the server to the client.
608 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
610 OSSL_STATEM
*st
= &s
->statem
;
612 switch (st
->hand_state
) {
614 /* No pre work to be done */
617 case TLS_ST_SW_HELLO_REQ
:
620 dtls1_clear_sent_buffer(s
);
623 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
625 if (SSL_IS_DTLS(s
)) {
626 dtls1_clear_sent_buffer(s
);
627 /* We don't buffer this message so don't use the timer */
632 case TLS_ST_SW_SRVR_HELLO
:
633 if (SSL_IS_DTLS(s
)) {
635 * Messages we write from now on should be buffered and
636 * retransmitted if necessary, so we need to use the timer now
642 case TLS_ST_SW_SRVR_DONE
:
643 #ifndef OPENSSL_NO_SCTP
644 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
645 /* Calls SSLfatal() as required */
646 return dtls_wait_for_dry(s
);
649 return WORK_FINISHED_CONTINUE
;
651 case TLS_ST_SW_SESSION_TICKET
:
652 if (SSL_IS_TLS13(s
)) {
654 * Actually this is the end of the handshake, but we're going
655 * straight into writing the session ticket out. So we finish off
656 * the handshake, but keep the various buffers active.
658 * Calls SSLfatal as required.
660 return tls_finish_handshake(s
, wst
, 0);
661 } if (SSL_IS_DTLS(s
)) {
663 * We're into the last flight. We don't retransmit the last flight
664 * unless we need to, so we don't use the timer
670 case TLS_ST_SW_CHANGE
:
673 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
674 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
675 /* SSLfatal() already called */
678 if (SSL_IS_DTLS(s
)) {
680 * We're into the last flight. We don't retransmit the last flight
681 * unless we need to, so we don't use the timer. This might have
682 * already been set to 0 if we sent a NewSessionTicket message,
683 * but we'll set it again here in case we didn't.
687 return WORK_FINISHED_CONTINUE
;
689 case TLS_ST_EARLY_DATA
:
690 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
)
691 return WORK_FINISHED_CONTINUE
;
695 /* Calls SSLfatal() as required */
696 return tls_finish_handshake(s
, wst
, 1);
699 return WORK_FINISHED_CONTINUE
;
703 * Perform any work that needs to be done after sending a message from the
704 * server to the client.
706 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
708 OSSL_STATEM
*st
= &s
->statem
;
712 switch (st
->hand_state
) {
714 /* No post work to be done */
717 case TLS_ST_SW_HELLO_REQ
:
718 if (statem_flush(s
) != 1)
720 if (!ssl3_init_finished_mac(s
)) {
721 /* SSLfatal() already called */
726 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
727 if (statem_flush(s
) != 1)
729 /* HelloVerifyRequest resets Finished MAC */
730 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
731 /* SSLfatal() already called */
735 * The next message should be another ClientHello which we need to
736 * treat like it was the first packet
741 case TLS_ST_SW_SRVR_HELLO
:
742 if (SSL_IS_TLS13(s
) && s
->hello_retry_request
== SSL_HRR_PENDING
) {
743 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
744 && statem_flush(s
) != 1)
748 #ifndef OPENSSL_NO_SCTP
749 if (SSL_IS_DTLS(s
) && s
->hit
) {
750 unsigned char sctpauthkey
[64];
751 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
754 * Add new shared key for SCTP-Auth, will be ignored if no
757 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
758 sizeof(DTLS1_SCTP_AUTH_LABEL
));
760 if (SSL_export_keying_material(s
, sctpauthkey
,
761 sizeof(sctpauthkey
), labelbuffer
,
762 sizeof(labelbuffer
), NULL
, 0,
764 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
765 SSL_F_OSSL_STATEM_SERVER_POST_WORK
,
766 ERR_R_INTERNAL_ERROR
);
770 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
771 sizeof(sctpauthkey
), sctpauthkey
);
775 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
776 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
780 case TLS_ST_SW_CHANGE
:
781 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
782 if (!statem_flush(s
))
787 * TODO(TLS1.3): This actually causes a problem. We don't yet know
788 * whether the next record we are going to receive is an unencrypted
789 * alert, or an encrypted handshake message. We're going to need
790 * something clever in the record layer for this.
792 if (SSL_IS_TLS13(s
)) {
793 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
794 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
795 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
796 /* SSLfatal() already called */
800 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
801 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
802 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
803 /* SSLfatal() already called */
809 #ifndef OPENSSL_NO_SCTP
810 if (SSL_IS_DTLS(s
) && !s
->hit
) {
812 * Change to new shared key of SCTP-Auth, will be ignored if
815 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
819 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
820 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
822 /* SSLfatal() already called */
827 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
830 case TLS_ST_SW_SRVR_DONE
:
831 if (statem_flush(s
) != 1)
835 case TLS_ST_SW_FINISHED
:
836 if (statem_flush(s
) != 1)
838 #ifndef OPENSSL_NO_SCTP
839 if (SSL_IS_DTLS(s
) && s
->hit
) {
841 * Change to new shared key of SCTP-Auth, will be ignored if
844 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
848 if (SSL_IS_TLS13(s
)) {
849 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
850 s
->master_secret
, s
->handshake_secret
, 0,
851 &s
->session
->master_key_length
)
852 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
853 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
854 /* SSLfatal() already called */
859 case TLS_ST_SW_KEY_UPDATE
:
860 if (statem_flush(s
) != 1)
862 if (!tls13_update_key(s
, 1)) {
863 /* SSLfatal() already called */
868 case TLS_ST_SW_SESSION_TICKET
:
869 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1)
874 return WORK_FINISHED_CONTINUE
;
878 * Get the message construction function and message type for sending from the
881 * Valid return values are:
885 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
886 confunc_f
*confunc
, int *mt
)
888 OSSL_STATEM
*st
= &s
->statem
;
890 switch (st
->hand_state
) {
892 /* Shouldn't happen */
893 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
894 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE
,
895 SSL_R_BAD_HANDSHAKE_STATE
);
898 case TLS_ST_SW_CHANGE
:
900 *confunc
= dtls_construct_change_cipher_spec
;
902 *confunc
= tls_construct_change_cipher_spec
;
903 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
906 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
907 *confunc
= dtls_construct_hello_verify_request
;
908 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
911 case TLS_ST_SW_HELLO_REQ
:
912 /* No construction function needed */
914 *mt
= SSL3_MT_HELLO_REQUEST
;
917 case TLS_ST_SW_SRVR_HELLO
:
918 *confunc
= tls_construct_server_hello
;
919 *mt
= SSL3_MT_SERVER_HELLO
;
923 *confunc
= tls_construct_server_certificate
;
924 *mt
= SSL3_MT_CERTIFICATE
;
927 case TLS_ST_SW_CERT_VRFY
:
928 *confunc
= tls_construct_cert_verify
;
929 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
933 case TLS_ST_SW_KEY_EXCH
:
934 *confunc
= tls_construct_server_key_exchange
;
935 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
938 case TLS_ST_SW_CERT_REQ
:
939 *confunc
= tls_construct_certificate_request
;
940 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
943 case TLS_ST_SW_SRVR_DONE
:
944 *confunc
= tls_construct_server_done
;
945 *mt
= SSL3_MT_SERVER_DONE
;
948 case TLS_ST_SW_SESSION_TICKET
:
949 *confunc
= tls_construct_new_session_ticket
;
950 *mt
= SSL3_MT_NEWSESSION_TICKET
;
953 case TLS_ST_SW_CERT_STATUS
:
954 *confunc
= tls_construct_cert_status
;
955 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
958 case TLS_ST_SW_FINISHED
:
959 *confunc
= tls_construct_finished
;
960 *mt
= SSL3_MT_FINISHED
;
963 case TLS_ST_EARLY_DATA
:
968 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
969 *confunc
= tls_construct_encrypted_extensions
;
970 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
973 case TLS_ST_SW_KEY_UPDATE
:
974 *confunc
= tls_construct_key_update
;
975 *mt
= SSL3_MT_KEY_UPDATE
;
983 * Maximum size (excluding the Handshake header) of a ClientHello message,
984 * calculated as follows:
986 * 2 + # client_version
987 * 32 + # only valid length for random
988 * 1 + # length of session_id
989 * 32 + # maximum size for session_id
990 * 2 + # length of cipher suites
991 * 2^16-2 + # maximum length of cipher suites array
992 * 1 + # length of compression_methods
993 * 2^8-1 + # maximum length of compression methods
994 * 2 + # length of extensions
995 * 2^16-1 # maximum length of extensions
997 #define CLIENT_HELLO_MAX_LENGTH 131396
999 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1000 #define NEXT_PROTO_MAX_LENGTH 514
1003 * Returns the maximum allowed length for the current message that we are
1004 * reading. Excludes the message header.
1006 size_t ossl_statem_server_max_message_size(SSL
*s
)
1008 OSSL_STATEM
*st
= &s
->statem
;
1010 switch (st
->hand_state
) {
1012 /* Shouldn't happen */
1015 case TLS_ST_SR_CLNT_HELLO
:
1016 return CLIENT_HELLO_MAX_LENGTH
;
1018 case TLS_ST_SR_END_OF_EARLY_DATA
:
1019 return END_OF_EARLY_DATA_MAX_LENGTH
;
1021 case TLS_ST_SR_CERT
:
1022 return s
->max_cert_list
;
1024 case TLS_ST_SR_KEY_EXCH
:
1025 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1027 case TLS_ST_SR_CERT_VRFY
:
1028 return SSL3_RT_MAX_PLAIN_LENGTH
;
1030 #ifndef OPENSSL_NO_NEXTPROTONEG
1031 case TLS_ST_SR_NEXT_PROTO
:
1032 return NEXT_PROTO_MAX_LENGTH
;
1035 case TLS_ST_SR_CHANGE
:
1036 return CCS_MAX_LENGTH
;
1038 case TLS_ST_SR_FINISHED
:
1039 return FINISHED_MAX_LENGTH
;
1041 case TLS_ST_SR_KEY_UPDATE
:
1042 return KEY_UPDATE_MAX_LENGTH
;
1047 * Process a message that the server has received from the client.
1049 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1051 OSSL_STATEM
*st
= &s
->statem
;
1053 switch (st
->hand_state
) {
1055 /* Shouldn't happen */
1056 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1057 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE
,
1058 ERR_R_INTERNAL_ERROR
);
1059 return MSG_PROCESS_ERROR
;
1061 case TLS_ST_SR_CLNT_HELLO
:
1062 return tls_process_client_hello(s
, pkt
);
1064 case TLS_ST_SR_END_OF_EARLY_DATA
:
1065 return tls_process_end_of_early_data(s
, pkt
);
1067 case TLS_ST_SR_CERT
:
1068 return tls_process_client_certificate(s
, pkt
);
1070 case TLS_ST_SR_KEY_EXCH
:
1071 return tls_process_client_key_exchange(s
, pkt
);
1073 case TLS_ST_SR_CERT_VRFY
:
1074 return tls_process_cert_verify(s
, pkt
);
1076 #ifndef OPENSSL_NO_NEXTPROTONEG
1077 case TLS_ST_SR_NEXT_PROTO
:
1078 return tls_process_next_proto(s
, pkt
);
1081 case TLS_ST_SR_CHANGE
:
1082 return tls_process_change_cipher_spec(s
, pkt
);
1084 case TLS_ST_SR_FINISHED
:
1085 return tls_process_finished(s
, pkt
);
1087 case TLS_ST_SR_KEY_UPDATE
:
1088 return tls_process_key_update(s
, pkt
);
1094 * Perform any further processing required following the receipt of a message
1097 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1099 OSSL_STATEM
*st
= &s
->statem
;
1101 switch (st
->hand_state
) {
1103 /* Shouldn't happen */
1104 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1105 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE
,
1106 ERR_R_INTERNAL_ERROR
);
1109 case TLS_ST_SR_CLNT_HELLO
:
1110 return tls_post_process_client_hello(s
, wst
);
1112 case TLS_ST_SR_KEY_EXCH
:
1113 return tls_post_process_client_key_exchange(s
, wst
);
1115 return WORK_FINISHED_CONTINUE
;
1118 #ifndef OPENSSL_NO_SRP
1119 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1120 static int ssl_check_srp_ext_ClientHello(SSL
*s
)
1123 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1125 if ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1126 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1127 if (s
->srp_ctx
.login
== NULL
) {
1129 * RFC 5054 says SHOULD reject, we do so if There is no srp
1132 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1133 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1134 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1137 ret
= SSL_srp_server_param_with_username(s
, &al
);
1140 if (ret
== SSL3_AL_FATAL
) {
1141 SSLfatal(s
, al
, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1142 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1143 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1144 : SSL_R_CLIENTHELLO_TLSEXT
);
1153 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1156 /* Always use DTLS 1.0 version: see RFC 6347 */
1157 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1158 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1164 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1166 unsigned int cookie_leni
;
1167 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1168 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1169 &cookie_leni
) == 0 ||
1170 cookie_leni
> 255) {
1171 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1172 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1175 s
->d1
->cookie_len
= cookie_leni
;
1177 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1178 s
->d1
->cookie_len
)) {
1179 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1180 ERR_R_INTERNAL_ERROR
);
1187 #ifndef OPENSSL_NO_EC
1189 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1190 * SecureTransport using the TLS extension block in |hello|.
1191 * Safari, since 10.6, sends exactly these extensions, in this order:
1195 * signature_algorithms (for TLSv1.2 only)
1197 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1198 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1199 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1200 * 10.8..10.8.3 (which don't work).
1202 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1204 static const unsigned char kSafariExtensionsBlock
[] = {
1205 0x00, 0x0a, /* elliptic_curves extension */
1206 0x00, 0x08, /* 8 bytes */
1207 0x00, 0x06, /* 6 bytes of curve ids */
1208 0x00, 0x17, /* P-256 */
1209 0x00, 0x18, /* P-384 */
1210 0x00, 0x19, /* P-521 */
1212 0x00, 0x0b, /* ec_point_formats */
1213 0x00, 0x02, /* 2 bytes */
1214 0x01, /* 1 point format */
1215 0x00, /* uncompressed */
1216 /* The following is only present in TLS 1.2 */
1217 0x00, 0x0d, /* signature_algorithms */
1218 0x00, 0x0c, /* 12 bytes */
1219 0x00, 0x0a, /* 10 bytes */
1220 0x05, 0x01, /* SHA-384/RSA */
1221 0x04, 0x01, /* SHA-256/RSA */
1222 0x02, 0x01, /* SHA-1/RSA */
1223 0x04, 0x03, /* SHA-256/ECDSA */
1224 0x02, 0x03, /* SHA-1/ECDSA */
1226 /* Length of the common prefix (first two extensions). */
1227 static const size_t kSafariCommonExtensionsLength
= 18;
1232 tmppkt
= hello
->extensions
;
1234 if (!PACKET_forward(&tmppkt
, 2)
1235 || !PACKET_get_net_2(&tmppkt
, &type
)
1236 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1240 if (type
!= TLSEXT_TYPE_server_name
)
1243 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1244 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1246 s
->s3
->is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1249 #endif /* !OPENSSL_NO_EC */
1251 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1253 /* |cookie| will only be initialized for DTLS. */
1254 PACKET session_id
, compression
, extensions
, cookie
;
1255 static const unsigned char null_compression
= 0;
1256 CLIENTHELLO_MSG
*clienthello
;
1258 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1259 if (clienthello
== NULL
) {
1260 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1261 ERR_R_INTERNAL_ERROR
);
1264 /* Check if this is actually an unexpected renegotiation ClientHello */
1265 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1266 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
)) {
1267 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1275 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1277 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1278 PACKET_null_init(&cookie
);
1280 if (clienthello
->isv2
) {
1283 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1284 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1285 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1286 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1291 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1292 * header is sent directly on the wire, not wrapped as a TLS
1293 * record. Our record layer just processes the message length and passes
1294 * the rest right through. Its format is:
1296 * 0-1 msg_length - decoded by the record layer
1297 * 2 msg_type - s->init_msg points here
1299 * 5-6 cipher_spec_length
1300 * 7-8 session_id_length
1301 * 9-10 challenge_length
1305 if (!PACKET_get_1(pkt
, &mt
)
1306 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1308 * Should never happen. We should have tested this in the record
1309 * layer in order to have determined that this is a SSLv2 record
1310 * in the first place
1312 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1313 ERR_R_INTERNAL_ERROR
);
1318 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1319 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1320 SSL_R_LENGTH_TOO_SHORT
);
1324 /* Parse the message and load client random. */
1325 if (clienthello
->isv2
) {
1327 * Handle an SSLv2 backwards compatible ClientHello
1328 * Note, this is only for SSLv3+ using the backward compatible format.
1329 * Real SSLv2 is not supported, and is rejected below.
1331 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1334 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1335 || !PACKET_get_net_2(pkt
, &session_id_len
)
1336 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1337 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1338 SSL_R_RECORD_LENGTH_MISMATCH
);
1342 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1343 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1344 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1348 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1350 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1351 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1352 /* No extensions. */
1353 || PACKET_remaining(pkt
) != 0) {
1354 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1355 SSL_R_RECORD_LENGTH_MISMATCH
);
1358 clienthello
->session_id_len
= session_id_len
;
1360 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1361 * here rather than sizeof(clienthello->random) because that is the limit
1362 * for SSLv3 and it is fixed. It won't change even if
1363 * sizeof(clienthello->random) does.
1365 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1366 ? SSL3_RANDOM_SIZE
: challenge_len
;
1367 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1368 if (!PACKET_copy_bytes(&challenge
,
1369 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1370 challenge_len
, challenge_len
)
1371 /* Advertise only null compression. */
1372 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1373 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1374 ERR_R_INTERNAL_ERROR
);
1378 PACKET_null_init(&clienthello
->extensions
);
1380 /* Regular ClientHello. */
1381 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1382 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1383 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1384 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1385 &clienthello
->session_id_len
)) {
1386 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1387 SSL_R_LENGTH_MISMATCH
);
1391 if (SSL_IS_DTLS(s
)) {
1392 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1393 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1394 SSL_R_LENGTH_MISMATCH
);
1397 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1398 DTLS1_COOKIE_LENGTH
,
1399 &clienthello
->dtls_cookie_len
)) {
1400 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1401 SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1405 * If we require cookies and this ClientHello doesn't contain one,
1406 * just return since we do not want to allocate any memory yet.
1407 * So check cookie length...
1409 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1410 if (clienthello
->dtls_cookie_len
== 0)
1411 return MSG_PROCESS_FINISHED_READING
;
1415 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1416 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1417 SSL_R_LENGTH_MISMATCH
);
1421 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1422 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1423 SSL_R_LENGTH_MISMATCH
);
1427 /* Could be empty. */
1428 if (PACKET_remaining(pkt
) == 0) {
1429 PACKET_null_init(&clienthello
->extensions
);
1431 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1432 || PACKET_remaining(pkt
) != 0) {
1433 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1434 SSL_R_LENGTH_MISMATCH
);
1440 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1441 MAX_COMPRESSIONS_SIZE
,
1442 &clienthello
->compressions_len
)) {
1443 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1444 ERR_R_INTERNAL_ERROR
);
1448 /* Preserve the raw extensions PACKET for later use */
1449 extensions
= clienthello
->extensions
;
1450 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1451 &clienthello
->pre_proc_exts
,
1452 &clienthello
->pre_proc_exts_len
, 1)) {
1453 /* SSLfatal already been called */
1456 s
->clienthello
= clienthello
;
1458 return MSG_PROCESS_CONTINUE_PROCESSING
;
1461 if (clienthello
!= NULL
)
1462 OPENSSL_free(clienthello
->pre_proc_exts
);
1463 OPENSSL_free(clienthello
);
1465 return MSG_PROCESS_ERROR
;
1468 static int tls_early_post_process_client_hello(SSL
*s
)
1471 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1475 #ifndef OPENSSL_NO_COMP
1476 SSL_COMP
*comp
= NULL
;
1478 const SSL_CIPHER
*c
;
1479 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1480 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1481 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1482 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1484 /* Finished parsing the ClientHello, now we can start processing it */
1485 /* Give the ClientHello callback a crack at things */
1486 if (s
->ctx
->client_hello_cb
!= NULL
) {
1487 /* A failure in the ClientHello callback terminates the connection. */
1488 switch (s
->ctx
->client_hello_cb(s
, &al
, s
->ctx
->client_hello_cb_arg
)) {
1489 case SSL_CLIENT_HELLO_SUCCESS
:
1491 case SSL_CLIENT_HELLO_RETRY
:
1492 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1494 case SSL_CLIENT_HELLO_ERROR
:
1497 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1498 SSL_R_CALLBACK_FAILED
);
1503 /* Set up the client_random */
1504 memcpy(s
->s3
->client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1506 /* Choose the version */
1508 if (clienthello
->isv2
) {
1509 if (clienthello
->legacy_version
== SSL2_VERSION
1510 || (clienthello
->legacy_version
& 0xff00)
1511 != (SSL3_VERSION_MAJOR
<< 8)) {
1513 * This is real SSLv2 or something completely unknown. We don't
1516 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1517 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1518 SSL_R_UNKNOWN_PROTOCOL
);
1522 s
->client_version
= clienthello
->legacy_version
;
1525 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1526 * versions are potentially compatible. Version negotiation comes later.
1528 if (!SSL_IS_DTLS(s
)) {
1529 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1530 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1531 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1532 protverr
= SSL_R_VERSION_TOO_LOW
;
1538 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1539 /* like ssl3_get_record, send alert using remote version number */
1540 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1542 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1543 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1547 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1548 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1549 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1550 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1551 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1555 if (SSL_IS_DTLS(s
)) {
1556 /* Empty cookie was already handled above by returning early. */
1557 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1558 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1559 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1560 clienthello
->dtls_cookie_len
) == 0) {
1561 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1562 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1563 SSL_R_COOKIE_MISMATCH
);
1565 /* else cookie verification succeeded */
1567 /* default verification */
1568 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1569 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1570 s
->d1
->cookie_len
) != 0) {
1571 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1572 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1573 SSL_R_COOKIE_MISMATCH
);
1576 s
->d1
->cookie_verified
= 1;
1578 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1579 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1580 if (protverr
!= 0) {
1581 s
->version
= s
->client_version
;
1582 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1583 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1591 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1592 clienthello
->isv2
) ||
1593 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1594 clienthello
->isv2
, 1)) {
1595 /* SSLfatal() already called */
1599 s
->s3
->send_connection_binding
= 0;
1600 /* Check what signalling cipher-suite values were received. */
1601 if (scsvs
!= NULL
) {
1602 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1603 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1604 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1605 if (s
->renegotiate
) {
1606 /* SCSV is fatal if renegotiating */
1607 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1608 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1609 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1612 s
->s3
->send_connection_binding
= 1;
1613 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1614 !ssl_check_version_downgrade(s
)) {
1616 * This SCSV indicates that the client previously tried
1617 * a higher version. We should fail if the current version
1618 * is an unexpected downgrade, as that indicates that the first
1619 * connection may have been tampered with in order to trigger
1620 * an insecure downgrade.
1622 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1623 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1624 SSL_R_INAPPROPRIATE_FALLBACK
);
1630 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1631 if (SSL_IS_TLS13(s
)) {
1632 const SSL_CIPHER
*cipher
=
1633 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(s
));
1635 if (cipher
== NULL
) {
1636 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1637 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1638 SSL_R_NO_SHARED_CIPHER
);
1641 if (s
->hello_retry_request
== SSL_HRR_PENDING
1642 && (s
->s3
->tmp
.new_cipher
== NULL
1643 || s
->s3
->tmp
.new_cipher
->id
!= cipher
->id
)) {
1645 * A previous HRR picked a different ciphersuite to the one we
1646 * just selected. Something must have changed.
1648 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1649 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1653 s
->s3
->tmp
.new_cipher
= cipher
;
1656 /* We need to do this before getting the session */
1657 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1658 SSL_EXT_CLIENT_HELLO
,
1659 clienthello
->pre_proc_exts
, NULL
, 0)) {
1660 /* SSLfatal() already called */
1665 * We don't allow resumption in a backwards compatible ClientHello.
1666 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1668 * Versions before 0.9.7 always allow clients to resume sessions in
1669 * renegotiation. 0.9.7 and later allow this by default, but optionally
1670 * ignore resumption requests with flag
1671 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1672 * than a change to default behavior so that applications relying on
1673 * this for security won't even compile against older library versions).
1674 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1675 * request renegotiation but not a new session (s->new_session remains
1676 * unset): for servers, this essentially just means that the
1677 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1680 if (clienthello
->isv2
||
1682 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1683 if (!ssl_get_new_session(s
, 1)) {
1684 /* SSLfatal() already called */
1688 i
= ssl_get_prev_session(s
, clienthello
);
1690 /* previous session */
1692 } else if (i
== -1) {
1693 /* SSLfatal() already called */
1697 if (!ssl_get_new_session(s
, 1)) {
1698 /* SSLfatal() already called */
1704 if (SSL_IS_TLS13(s
)) {
1705 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1706 s
->clienthello
->session_id_len
);
1707 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1711 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1712 * ciphersuite compatibility with the session as part of resumption.
1714 if (!SSL_IS_TLS13(s
) && s
->hit
) {
1716 id
= s
->session
->cipher
->id
;
1719 fprintf(stderr
, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers
));
1721 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1722 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1724 fprintf(stderr
, "client [%2d of %2d]:%s\n",
1725 i
, sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1734 * we need to have the cipher in the cipher list if we are asked
1737 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1738 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1739 SSL_R_REQUIRED_CIPHER_MISSING
);
1744 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1745 if (clienthello
->compressions
[loop
] == 0)
1749 if (loop
>= clienthello
->compressions_len
) {
1751 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1752 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1753 SSL_R_NO_COMPRESSION_SPECIFIED
);
1757 #ifndef OPENSSL_NO_EC
1758 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1759 ssl_check_for_safari(s
, clienthello
);
1760 #endif /* !OPENSSL_NO_EC */
1762 /* TLS extensions */
1763 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1764 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1765 /* SSLfatal() already called */
1770 * Check if we want to use external pre-shared secret for this handshake
1771 * for not reused session only. We need to generate server_random before
1772 * calling tls_session_secret_cb in order to allow SessionTicket
1773 * processing to use it in key derivation.
1777 pos
= s
->s3
->server_random
;
1778 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1779 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1780 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1781 ERR_R_INTERNAL_ERROR
);
1787 && s
->version
>= TLS1_VERSION
1790 && s
->ext
.session_secret_cb
) {
1791 const SSL_CIPHER
*pref_cipher
= NULL
;
1793 * s->session->master_key_length is a size_t, but this is an int for
1794 * backwards compat reasons
1796 int master_key_length
;
1798 master_key_length
= sizeof(s
->session
->master_key
);
1799 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1800 &master_key_length
, ciphers
,
1802 s
->ext
.session_secret_cb_arg
)
1803 && master_key_length
> 0) {
1804 s
->session
->master_key_length
= master_key_length
;
1806 s
->session
->ciphers
= ciphers
;
1807 s
->session
->verify_result
= X509_V_OK
;
1811 /* check if some cipher was preferred by call back */
1812 if (pref_cipher
== NULL
)
1813 pref_cipher
= ssl3_choose_cipher(s
, s
->session
->ciphers
,
1814 SSL_get_ciphers(s
));
1815 if (pref_cipher
== NULL
) {
1816 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1817 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1818 SSL_R_NO_SHARED_CIPHER
);
1822 s
->session
->cipher
= pref_cipher
;
1823 sk_SSL_CIPHER_free(s
->cipher_list
);
1824 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1825 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1826 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1831 * Worst case, we will use the NULL compression, but if we have other
1832 * options, we will now look for them. We have complen-1 compression
1833 * algorithms from the client, starting at q.
1835 s
->s3
->tmp
.new_compression
= NULL
;
1836 if (SSL_IS_TLS13(s
)) {
1838 * We already checked above that the NULL compression method appears in
1839 * the list. Now we check there aren't any others (which is illegal in
1840 * a TLSv1.3 ClientHello.
1842 if (clienthello
->compressions_len
!= 1) {
1843 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1844 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1845 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1849 #ifndef OPENSSL_NO_COMP
1850 /* This only happens if we have a cache hit */
1851 else if (s
->session
->compress_meth
!= 0) {
1852 int m
, comp_id
= s
->session
->compress_meth
;
1854 /* Perform sanity checks on resumed compression algorithm */
1855 /* Can't disable compression */
1856 if (!ssl_allow_compression(s
)) {
1857 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1858 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1859 SSL_R_INCONSISTENT_COMPRESSION
);
1862 /* Look for resumed compression method */
1863 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
1864 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1865 if (comp_id
== comp
->id
) {
1866 s
->s3
->tmp
.new_compression
= comp
;
1870 if (s
->s3
->tmp
.new_compression
== NULL
) {
1871 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1872 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1873 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1876 /* Look for resumed method in compression list */
1877 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
1878 if (clienthello
->compressions
[k
] == comp_id
)
1881 if (k
>= clienthello
->compressions_len
) {
1882 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1883 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1884 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
1887 } else if (s
->hit
) {
1889 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
1890 /* See if we have a match */
1891 int m
, nn
, v
, done
= 0;
1894 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1895 for (m
= 0; m
< nn
; m
++) {
1896 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1898 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
1899 if (v
== clienthello
->compressions
[o
]) {
1908 s
->s3
->tmp
.new_compression
= comp
;
1914 * If compression is disabled we'd better not try to resume a session
1915 * using compression.
1917 if (s
->session
->compress_meth
!= 0) {
1918 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1919 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1920 SSL_R_INCONSISTENT_COMPRESSION
);
1926 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1929 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
1930 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1931 s
->session
->ciphers
= ciphers
;
1932 if (ciphers
== NULL
) {
1933 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1934 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1935 ERR_R_INTERNAL_ERROR
);
1942 #ifdef OPENSSL_NO_COMP
1943 s
->session
->compress_meth
= 0;
1945 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
1947 if (!tls1_set_server_sigalgs(s
)) {
1948 /* SSLfatal() already called */
1953 sk_SSL_CIPHER_free(ciphers
);
1954 sk_SSL_CIPHER_free(scsvs
);
1955 OPENSSL_free(clienthello
->pre_proc_exts
);
1956 OPENSSL_free(s
->clienthello
);
1957 s
->clienthello
= NULL
;
1960 sk_SSL_CIPHER_free(ciphers
);
1961 sk_SSL_CIPHER_free(scsvs
);
1962 OPENSSL_free(clienthello
->pre_proc_exts
);
1963 OPENSSL_free(s
->clienthello
);
1964 s
->clienthello
= NULL
;
1970 * Call the status request callback if needed. Upon success, returns 1.
1971 * Upon failure, returns 0.
1973 static int tls_handle_status_request(SSL
*s
)
1975 s
->ext
.status_expected
= 0;
1978 * If status request then ask callback what to do. Note: this must be
1979 * called after servername callbacks in case the certificate has changed,
1980 * and must be called after the cipher has been chosen because this may
1981 * influence which certificate is sent
1983 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
1984 && s
->ctx
->ext
.status_cb
!= NULL
) {
1987 /* If no certificate can't return certificate status */
1988 if (s
->s3
->tmp
.cert
!= NULL
) {
1990 * Set current certificate to one we will use so SSL_get_certificate
1991 * et al can pick it up.
1993 s
->cert
->key
= s
->s3
->tmp
.cert
;
1994 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
1996 /* We don't want to send a status request response */
1997 case SSL_TLSEXT_ERR_NOACK
:
1998 s
->ext
.status_expected
= 0;
2000 /* status request response should be sent */
2001 case SSL_TLSEXT_ERR_OK
:
2002 if (s
->ext
.ocsp
.resp
)
2003 s
->ext
.status_expected
= 1;
2005 /* something bad happened */
2006 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2008 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2009 SSL_F_TLS_HANDLE_STATUS_REQUEST
,
2010 SSL_R_CLIENTHELLO_TLSEXT
);
2020 * Call the alpn_select callback if needed. Upon success, returns 1.
2021 * Upon failure, returns 0.
2023 int tls_handle_alpn(SSL
*s
)
2025 const unsigned char *selected
= NULL
;
2026 unsigned char selected_len
= 0;
2028 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
->alpn_proposed
!= NULL
) {
2029 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
2030 s
->s3
->alpn_proposed
,
2031 (unsigned int)s
->s3
->alpn_proposed_len
,
2032 s
->ctx
->ext
.alpn_select_cb_arg
);
2034 if (r
== SSL_TLSEXT_ERR_OK
) {
2035 OPENSSL_free(s
->s3
->alpn_selected
);
2036 s
->s3
->alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2037 if (s
->s3
->alpn_selected
== NULL
) {
2038 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_HANDLE_ALPN
,
2039 ERR_R_INTERNAL_ERROR
);
2042 s
->s3
->alpn_selected_len
= selected_len
;
2043 #ifndef OPENSSL_NO_NEXTPROTONEG
2044 /* ALPN takes precedence over NPN. */
2045 s
->s3
->npn_seen
= 0;
2048 /* Check ALPN is consistent with session */
2049 if (s
->session
->ext
.alpn_selected
== NULL
2050 || selected_len
!= s
->session
->ext
.alpn_selected_len
2051 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2052 selected_len
) != 0) {
2053 /* Not consistent so can't be used for early_data */
2054 s
->ext
.early_data_ok
= 0;
2057 /* If a new session update it with the new ALPN value */
2058 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2060 if (s
->session
->ext
.alpn_selected
== NULL
) {
2061 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2062 SSL_F_TLS_HANDLE_ALPN
,
2063 ERR_R_INTERNAL_ERROR
);
2066 s
->session
->ext
.alpn_selected_len
= selected_len
;
2071 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2072 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
, SSL_F_TLS_HANDLE_ALPN
,
2073 SSL_R_NO_APPLICATION_PROTOCOL
);
2077 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2082 /* Check ALPN is consistent with session */
2083 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2084 /* Not consistent so can't be used for early_data */
2085 s
->ext
.early_data_ok
= 0;
2091 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
2093 const SSL_CIPHER
*cipher
;
2095 if (wst
== WORK_MORE_A
) {
2096 int rv
= tls_early_post_process_client_hello(s
);
2098 /* SSLfatal() was already called */
2105 if (wst
== WORK_MORE_B
) {
2106 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2107 /* Let cert callback update server certificates if required */
2108 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2109 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
2111 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2112 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2113 SSL_R_CERT_CB_ERROR
);
2117 s
->rwstate
= SSL_X509_LOOKUP
;
2120 s
->rwstate
= SSL_NOTHING
;
2123 /* In TLSv1.3 we selected the ciphersuite before resumption */
2124 if (!SSL_IS_TLS13(s
)) {
2126 ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
2128 if (cipher
== NULL
) {
2129 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2130 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2131 SSL_R_NO_SHARED_CIPHER
);
2134 s
->s3
->tmp
.new_cipher
= cipher
;
2137 if (!tls_choose_sigalg(s
, 1)) {
2138 /* SSLfatal already called */
2141 /* check whether we should disable session resumption */
2142 if (s
->not_resumable_session_cb
!= NULL
)
2143 s
->session
->not_resumable
=
2144 s
->not_resumable_session_cb(s
,
2145 ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
2146 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2147 if (s
->session
->not_resumable
)
2148 /* do not send a session ticket */
2149 s
->ext
.ticket_expected
= 0;
2152 /* Session-id reuse */
2153 s
->s3
->tmp
.new_cipher
= s
->session
->cipher
;
2157 * we now have the following setup.
2159 * cipher_list - our preferred list of ciphers
2160 * ciphers - the clients preferred list of ciphers
2161 * compression - basically ignored right now
2162 * ssl version is set - sslv3
2163 * s->session - The ssl session has been setup.
2164 * s->hit - session reuse flag
2165 * s->s3->tmp.new_cipher- the new cipher to use.
2169 * Call status_request callback if needed. Has to be done after the
2170 * certificate callbacks etc above.
2172 if (!tls_handle_status_request(s
)) {
2173 /* SSLfatal() already called */
2177 * Call alpn_select callback if needed. Has to be done after SNI and
2178 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2179 * we already did this because cipher negotiation happens earlier, and
2180 * we must handle ALPN before we decide whether to accept early_data.
2182 if (!SSL_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2183 /* SSLfatal() already called */
2189 #ifndef OPENSSL_NO_SRP
2190 if (wst
== WORK_MORE_C
) {
2192 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2194 * callback indicates further work to be done
2196 s
->rwstate
= SSL_X509_LOOKUP
;
2200 /* SSLfatal() already called */
2206 return WORK_FINISHED_STOP
;
2211 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2216 unsigned char *session_id
;
2217 int usetls13
= SSL_IS_TLS13(s
) || s
->hello_retry_request
== SSL_HRR_PENDING
;
2219 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2220 if (!WPACKET_put_bytes_u16(pkt
, version
)
2222 * Random stuff. Filling of the server_random takes place in
2223 * tls_process_client_hello()
2225 || !WPACKET_memcpy(pkt
,
2226 s
->hello_retry_request
== SSL_HRR_PENDING
2227 ? hrrrandom
: s
->s3
->server_random
,
2228 SSL3_RANDOM_SIZE
)) {
2229 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2230 ERR_R_INTERNAL_ERROR
);
2235 * There are several cases for the session ID to send
2236 * back in the server hello:
2237 * - For session reuse from the session cache,
2238 * we send back the old session ID.
2239 * - If stateless session reuse (using a session ticket)
2240 * is successful, we send back the client's "session ID"
2241 * (which doesn't actually identify the session).
2242 * - If it is a new session, we send back the new
2244 * - However, if we want the new session to be single-use,
2245 * we send back a 0-length session ID.
2246 * - In TLSv1.3 we echo back the session id sent to us by the client
2248 * s->hit is non-zero in either case of session reuse,
2249 * so the following won't overwrite an ID that we're supposed
2252 if (s
->session
->not_resumable
||
2253 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2255 s
->session
->session_id_length
= 0;
2258 sl
= s
->tmp_session_id_len
;
2259 session_id
= s
->tmp_session_id
;
2261 sl
= s
->session
->session_id_length
;
2262 session_id
= s
->session
->session_id
;
2265 if (sl
> sizeof(s
->session
->session_id
)) {
2266 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2267 ERR_R_INTERNAL_ERROR
);
2271 /* set up the compression method */
2272 #ifdef OPENSSL_NO_COMP
2275 if (usetls13
|| s
->s3
->tmp
.new_compression
== NULL
)
2278 compm
= s
->s3
->tmp
.new_compression
->id
;
2281 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2282 || !s
->method
->put_cipher_by_char(s
->s3
->tmp
.new_cipher
, pkt
, &len
)
2283 || !WPACKET_put_bytes_u8(pkt
, compm
)
2284 || !tls_construct_extensions(s
, pkt
,
2285 s
->hello_retry_request
2287 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2289 ? SSL_EXT_TLS1_3_SERVER_HELLO
2290 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2292 /* SSLfatal() already called */
2296 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2297 /* Ditch the session. We'll create a new one next time around */
2298 SSL_SESSION_free(s
->session
);
2303 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2304 * a synthetic message_hash in place of ClientHello1.
2306 if (!create_synthetic_message_hash(s
)) {
2307 /* SSLfatal() already called */
2310 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2311 && !ssl3_digest_cached_records(s
, 0)) {
2312 /* SSLfatal() already called */;
2319 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2321 if (!s
->s3
->tmp
.cert_request
) {
2322 if (!ssl3_digest_cached_records(s
, 0)) {
2323 /* SSLfatal() already called */
2330 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2332 #ifndef OPENSSL_NO_DH
2333 EVP_PKEY
*pkdh
= NULL
;
2335 #ifndef OPENSSL_NO_EC
2336 unsigned char *encodedPoint
= NULL
;
2337 size_t encodedlen
= 0;
2340 const SIGALG_LOOKUP
*lu
= s
->s3
->tmp
.sigalg
;
2344 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2345 EVP_PKEY_CTX
*pctx
= NULL
;
2346 size_t paramlen
, paramoffset
;
2348 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2349 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2350 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2354 if (md_ctx
== NULL
) {
2355 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2356 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2360 type
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2362 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2363 #ifndef OPENSSL_NO_PSK
2364 /* Plain PSK or RSAPSK nothing to do */
2365 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2367 #endif /* !OPENSSL_NO_PSK */
2368 #ifndef OPENSSL_NO_DH
2369 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2370 CERT
*cert
= s
->cert
;
2372 EVP_PKEY
*pkdhp
= NULL
;
2375 if (s
->cert
->dh_tmp_auto
) {
2376 DH
*dhp
= ssl_get_auto_dh(s
);
2377 pkdh
= EVP_PKEY_new();
2378 if (pkdh
== NULL
|| dhp
== NULL
) {
2380 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2381 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2382 ERR_R_INTERNAL_ERROR
);
2385 EVP_PKEY_assign_DH(pkdh
, dhp
);
2388 pkdhp
= cert
->dh_tmp
;
2390 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2391 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2392 pkdh
= ssl_dh_to_pkey(dhp
);
2394 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2395 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2396 ERR_R_INTERNAL_ERROR
);
2401 if (pkdhp
== NULL
) {
2402 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2403 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2404 SSL_R_MISSING_TMP_DH_KEY
);
2407 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2408 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2409 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2410 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2411 SSL_R_DH_KEY_TOO_SMALL
);
2414 if (s
->s3
->tmp
.pkey
!= NULL
) {
2415 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2416 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2417 ERR_R_INTERNAL_ERROR
);
2421 s
->s3
->tmp
.pkey
= ssl_generate_pkey(pkdhp
);
2422 if (s
->s3
->tmp
.pkey
== NULL
) {
2423 /* SSLfatal() already called */
2427 dh
= EVP_PKEY_get0_DH(s
->s3
->tmp
.pkey
);
2429 EVP_PKEY_free(pkdh
);
2432 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2433 DH_get0_key(dh
, &r
[2], NULL
);
2436 #ifndef OPENSSL_NO_EC
2437 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2439 if (s
->s3
->tmp
.pkey
!= NULL
) {
2440 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2441 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2442 ERR_R_INTERNAL_ERROR
);
2446 /* Get NID of appropriate shared curve */
2447 curve_id
= tls1_shared_group(s
, -2);
2448 if (curve_id
== 0) {
2449 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2450 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2451 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2454 s
->s3
->tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2455 /* Generate a new key for this curve */
2456 if (s
->s3
->tmp
.pkey
== NULL
) {
2457 /* SSLfatal() already called */
2461 /* Encode the public key. */
2462 encodedlen
= EVP_PKEY_get1_tls_encodedpoint(s
->s3
->tmp
.pkey
,
2464 if (encodedlen
== 0) {
2465 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2466 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2471 * We'll generate the serverKeyExchange message explicitly so we
2472 * can set these to NULLs
2479 #endif /* !OPENSSL_NO_EC */
2480 #ifndef OPENSSL_NO_SRP
2481 if (type
& SSL_kSRP
) {
2482 if ((s
->srp_ctx
.N
== NULL
) ||
2483 (s
->srp_ctx
.g
== NULL
) ||
2484 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2485 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2486 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2487 SSL_R_MISSING_SRP_PARAM
);
2490 r
[0] = s
->srp_ctx
.N
;
2491 r
[1] = s
->srp_ctx
.g
;
2492 r
[2] = s
->srp_ctx
.s
;
2493 r
[3] = s
->srp_ctx
.B
;
2497 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2498 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2499 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2503 if (((s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2504 || ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2506 } else if (lu
== NULL
) {
2507 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
2508 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2512 #ifndef OPENSSL_NO_PSK
2513 if (type
& SSL_PSK
) {
2514 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2515 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2518 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2519 * checked this when we set the identity hint - but just in case
2521 if (len
> PSK_MAX_IDENTITY_LEN
2522 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2524 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2525 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2526 ERR_R_INTERNAL_ERROR
);
2532 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2533 unsigned char *binval
;
2536 #ifndef OPENSSL_NO_SRP
2537 if ((i
== 2) && (type
& SSL_kSRP
)) {
2538 res
= WPACKET_start_sub_packet_u8(pkt
);
2541 res
= WPACKET_start_sub_packet_u16(pkt
);
2544 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2545 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2546 ERR_R_INTERNAL_ERROR
);
2550 #ifndef OPENSSL_NO_DH
2552 * for interoperability with some versions of the Microsoft TLS
2553 * stack, we need to zero pad the DHE pub key to the same length
2556 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2557 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2560 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2561 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2562 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2563 ERR_R_INTERNAL_ERROR
);
2566 memset(binval
, 0, len
);
2570 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2571 || !WPACKET_close(pkt
)) {
2572 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2573 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2574 ERR_R_INTERNAL_ERROR
);
2578 BN_bn2bin(r
[i
], binval
);
2581 #ifndef OPENSSL_NO_EC
2582 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2584 * We only support named (not generic) curves. In this situation, the
2585 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2586 * [1 byte length of encoded point], followed by the actual encoded
2589 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2590 || !WPACKET_put_bytes_u8(pkt
, 0)
2591 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2592 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2593 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2594 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2595 ERR_R_INTERNAL_ERROR
);
2598 OPENSSL_free(encodedPoint
);
2599 encodedPoint
= NULL
;
2605 EVP_PKEY
*pkey
= s
->s3
->tmp
.cert
->privatekey
;
2607 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2608 size_t siglen
, tbslen
;
2611 if (pkey
== NULL
|| !tls1_lookup_md(lu
, &md
)) {
2612 /* Should never happen */
2613 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2614 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2615 ERR_R_INTERNAL_ERROR
);
2619 * n is the length of the params, they start at &(d[4]) and p
2620 * points to the space at the end.
2623 /* Get length of the parameters we have written above */
2624 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2625 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2626 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2627 ERR_R_INTERNAL_ERROR
);
2630 /* send signature algorithm */
2631 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2632 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2633 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2634 ERR_R_INTERNAL_ERROR
);
2638 * Create the signature. We don't know the actual length of the sig
2639 * until after we've created it, so we reserve enough bytes for it
2640 * up front, and then properly allocate them in the WPACKET
2643 siglen
= EVP_PKEY_size(pkey
);
2644 if (!WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2645 || EVP_DigestSignInit(md_ctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
2646 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2647 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2648 ERR_R_INTERNAL_ERROR
);
2651 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2652 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2653 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2654 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2655 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2660 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2661 s
->init_buf
->data
+ paramoffset
,
2664 /* SSLfatal() already called */
2667 rv
= EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
);
2669 if (rv
<= 0 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2670 || sigbytes1
!= sigbytes2
) {
2671 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2672 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2673 ERR_R_INTERNAL_ERROR
);
2678 EVP_MD_CTX_free(md_ctx
);
2681 #ifndef OPENSSL_NO_DH
2682 EVP_PKEY_free(pkdh
);
2684 #ifndef OPENSSL_NO_EC
2685 OPENSSL_free(encodedPoint
);
2687 EVP_MD_CTX_free(md_ctx
);
2691 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2693 if (SSL_IS_TLS13(s
)) {
2694 /* TODO(TLS1.3) for now send empty request context */
2695 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2696 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2697 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2698 ERR_R_INTERNAL_ERROR
);
2702 if (!tls_construct_extensions(s
, pkt
,
2703 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2705 /* SSLfatal() already called */
2711 /* get the list of acceptable cert types */
2712 if (!WPACKET_start_sub_packet_u8(pkt
)
2713 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2714 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2715 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2719 if (SSL_USE_SIGALGS(s
)) {
2720 const uint16_t *psigs
;
2721 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2723 if (!WPACKET_start_sub_packet_u16(pkt
)
2724 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2725 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2726 || !WPACKET_close(pkt
)) {
2727 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2728 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2729 ERR_R_INTERNAL_ERROR
);
2734 if (!construct_ca_names(s
, pkt
)) {
2735 /* SSLfatal() already called */
2740 s
->s3
->tmp
.cert_request
= 1;
2744 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
)
2746 #ifndef OPENSSL_NO_PSK
2747 unsigned char psk
[PSK_MAX_PSK_LEN
];
2749 PACKET psk_identity
;
2751 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2752 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2753 SSL_R_LENGTH_MISMATCH
);
2756 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2757 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2758 SSL_R_DATA_LENGTH_TOO_LONG
);
2761 if (s
->psk_server_callback
== NULL
) {
2762 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2763 SSL_R_PSK_NO_SERVER_CB
);
2767 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2768 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2769 ERR_R_INTERNAL_ERROR
);
2773 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2776 if (psklen
> PSK_MAX_PSK_LEN
) {
2777 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2778 ERR_R_INTERNAL_ERROR
);
2780 } else if (psklen
== 0) {
2782 * PSK related to the given identity not found
2784 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
2785 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2786 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2790 OPENSSL_free(s
->s3
->tmp
.psk
);
2791 s
->s3
->tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2792 OPENSSL_cleanse(psk
, psklen
);
2794 if (s
->s3
->tmp
.psk
== NULL
) {
2795 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2796 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2800 s
->s3
->tmp
.psklen
= psklen
;
2804 /* Should never happen */
2805 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2806 ERR_R_INTERNAL_ERROR
);
2811 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
)
2813 #ifndef OPENSSL_NO_RSA
2814 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
2816 unsigned char decrypt_good
, version_good
;
2817 size_t j
, padding_len
;
2818 PACKET enc_premaster
;
2820 unsigned char *rsa_decrypt
= NULL
;
2823 rsa
= EVP_PKEY_get0_RSA(s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
);
2825 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2826 SSL_R_MISSING_RSA_CERTIFICATE
);
2830 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2831 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2832 enc_premaster
= *pkt
;
2834 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2835 || PACKET_remaining(pkt
) != 0) {
2836 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2837 SSL_R_LENGTH_MISMATCH
);
2843 * We want to be sure that the plaintext buffer size makes it safe to
2844 * iterate over the entire size of a premaster secret
2845 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2846 * their ciphertext cannot accommodate a premaster secret anyway.
2848 if (RSA_size(rsa
) < SSL_MAX_MASTER_KEY_LENGTH
) {
2849 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2850 RSA_R_KEY_SIZE_TOO_SMALL
);
2854 rsa_decrypt
= OPENSSL_malloc(RSA_size(rsa
));
2855 if (rsa_decrypt
== NULL
) {
2856 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2857 ERR_R_MALLOC_FAILURE
);
2862 * We must not leak whether a decryption failure occurs because of
2863 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2864 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2865 * generates a random premaster secret for the case that the decrypt
2866 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2869 if (ssl_randbytes(s
, rand_premaster_secret
,
2870 sizeof(rand_premaster_secret
)) <= 0) {
2871 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2872 ERR_R_INTERNAL_ERROR
);
2877 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2878 * the timing-sensitive code below.
2880 /* TODO(size_t): Convert this function */
2881 decrypt_len
= (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster
),
2882 PACKET_data(&enc_premaster
),
2883 rsa_decrypt
, rsa
, RSA_NO_PADDING
);
2884 if (decrypt_len
< 0) {
2885 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2886 ERR_R_INTERNAL_ERROR
);
2890 /* Check the padding. See RFC 3447, section 7.2.2. */
2893 * The smallest padded premaster is 11 bytes of overhead. Small keys
2894 * are publicly invalid, so this may return immediately. This ensures
2895 * PS is at least 8 bytes.
2897 if (decrypt_len
< 11 + SSL_MAX_MASTER_KEY_LENGTH
) {
2898 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2899 SSL_R_DECRYPTION_FAILED
);
2903 padding_len
= decrypt_len
- SSL_MAX_MASTER_KEY_LENGTH
;
2904 decrypt_good
= constant_time_eq_int_8(rsa_decrypt
[0], 0) &
2905 constant_time_eq_int_8(rsa_decrypt
[1], 2);
2906 for (j
= 2; j
< padding_len
- 1; j
++) {
2907 decrypt_good
&= ~constant_time_is_zero_8(rsa_decrypt
[j
]);
2909 decrypt_good
&= constant_time_is_zero_8(rsa_decrypt
[padding_len
- 1]);
2912 * If the version in the decrypted pre-master secret is correct then
2913 * version_good will be 0xff, otherwise it'll be zero. The
2914 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2915 * (http://eprint.iacr.org/2003/052/) exploits the version number
2916 * check as a "bad version oracle". Thus version checks are done in
2917 * constant time and are treated like any other decryption error.
2920 constant_time_eq_8(rsa_decrypt
[padding_len
],
2921 (unsigned)(s
->client_version
>> 8));
2923 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
2924 (unsigned)(s
->client_version
& 0xff));
2927 * The premaster secret must contain the same version number as the
2928 * ClientHello to detect version rollback attacks (strangely, the
2929 * protocol does not offer such protection for DH ciphersuites).
2930 * However, buggy clients exist that send the negotiated protocol
2931 * version instead if the server does not support the requested
2932 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2935 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) {
2936 unsigned char workaround_good
;
2937 workaround_good
= constant_time_eq_8(rsa_decrypt
[padding_len
],
2938 (unsigned)(s
->version
>> 8));
2940 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
2941 (unsigned)(s
->version
& 0xff));
2942 version_good
|= workaround_good
;
2946 * Both decryption and version must be good for decrypt_good to
2947 * remain non-zero (0xff).
2949 decrypt_good
&= version_good
;
2952 * Now copy rand_premaster_secret over from p using
2953 * decrypt_good_mask. If decryption failed, then p does not
2954 * contain valid plaintext, however, a check above guarantees
2955 * it is still sufficiently large to read from.
2957 for (j
= 0; j
< sizeof(rand_premaster_secret
); j
++) {
2958 rsa_decrypt
[padding_len
+ j
] =
2959 constant_time_select_8(decrypt_good
,
2960 rsa_decrypt
[padding_len
+ j
],
2961 rand_premaster_secret
[j
]);
2964 if (!ssl_generate_master_secret(s
, rsa_decrypt
+ padding_len
,
2965 sizeof(rand_premaster_secret
), 0)) {
2966 /* SSLfatal() already called */
2972 OPENSSL_free(rsa_decrypt
);
2975 /* Should never happen */
2976 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2977 ERR_R_INTERNAL_ERROR
);
2982 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
)
2984 #ifndef OPENSSL_NO_DH
2985 EVP_PKEY
*skey
= NULL
;
2989 const unsigned char *data
;
2990 EVP_PKEY
*ckey
= NULL
;
2993 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
2994 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
2995 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2998 skey
= s
->s3
->tmp
.pkey
;
3000 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3001 SSL_R_MISSING_TMP_DH_KEY
);
3005 if (PACKET_remaining(pkt
) == 0L) {
3006 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3007 SSL_R_MISSING_TMP_DH_KEY
);
3010 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3011 /* We already checked we have enough data */
3012 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3013 ERR_R_INTERNAL_ERROR
);
3016 ckey
= EVP_PKEY_new();
3017 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3018 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3022 cdh
= EVP_PKEY_get0_DH(ckey
);
3023 pub_key
= BN_bin2bn(data
, i
, NULL
);
3025 if (pub_key
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
3026 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3027 ERR_R_INTERNAL_ERROR
);
3028 if (pub_key
!= NULL
)
3033 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3034 /* SSLfatal() already called */
3039 EVP_PKEY_free(s
->s3
->tmp
.pkey
);
3040 s
->s3
->tmp
.pkey
= NULL
;
3042 EVP_PKEY_free(ckey
);
3045 /* Should never happen */
3046 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3047 ERR_R_INTERNAL_ERROR
);
3052 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
)
3054 #ifndef OPENSSL_NO_EC
3055 EVP_PKEY
*skey
= s
->s3
->tmp
.pkey
;
3056 EVP_PKEY
*ckey
= NULL
;
3059 if (PACKET_remaining(pkt
) == 0L) {
3060 /* We don't support ECDH client auth */
3061 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3062 SSL_R_MISSING_TMP_ECDH_KEY
);
3066 const unsigned char *data
;
3069 * Get client's public key from encoded point in the
3070 * ClientKeyExchange message.
3073 /* Get encoded point length */
3074 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3075 || PACKET_remaining(pkt
) != 0) {
3076 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3077 SSL_R_LENGTH_MISMATCH
);
3080 ckey
= EVP_PKEY_new();
3081 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3082 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3086 if (EVP_PKEY_set1_tls_encodedpoint(ckey
, data
, i
) == 0) {
3087 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3093 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3094 /* SSLfatal() already called */
3099 EVP_PKEY_free(s
->s3
->tmp
.pkey
);
3100 s
->s3
->tmp
.pkey
= NULL
;
3102 EVP_PKEY_free(ckey
);
3106 /* Should never happen */
3107 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3108 ERR_R_INTERNAL_ERROR
);
3113 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
)
3115 #ifndef OPENSSL_NO_SRP
3117 const unsigned char *data
;
3119 if (!PACKET_get_net_2(pkt
, &i
)
3120 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3121 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3122 SSL_R_BAD_SRP_A_LENGTH
);
3125 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3126 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3130 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3131 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS_PROCESS_CKE_SRP
,
3132 SSL_R_BAD_SRP_PARAMETERS
);
3135 OPENSSL_free(s
->session
->srp_username
);
3136 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3137 if (s
->session
->srp_username
== NULL
) {
3138 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3139 ERR_R_MALLOC_FAILURE
);
3143 if (!srp_generate_server_master_secret(s
)) {
3144 /* SSLfatal() already called */
3150 /* Should never happen */
3151 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3152 ERR_R_INTERNAL_ERROR
);
3157 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
)
3159 #ifndef OPENSSL_NO_GOST
3160 EVP_PKEY_CTX
*pkey_ctx
;
3161 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3162 unsigned char premaster_secret
[32];
3163 const unsigned char *start
;
3164 size_t outlen
= 32, inlen
;
3165 unsigned long alg_a
;
3168 size_t sess_key_len
;
3169 const unsigned char *data
;
3172 /* Get our certificate private key */
3173 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
3174 if (alg_a
& SSL_aGOST12
) {
3176 * New GOST ciphersuites have SSL_aGOST01 bit too
3178 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3180 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3183 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3185 } else if (alg_a
& SSL_aGOST01
) {
3186 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3189 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
3190 if (pkey_ctx
== NULL
) {
3191 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3192 ERR_R_MALLOC_FAILURE
);
3195 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3196 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3197 ERR_R_INTERNAL_ERROR
);
3201 * If client certificate is present and is of the same type, maybe
3202 * use it for key exchange. Don't mind errors from
3203 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3204 * client certificate for authorization only.
3206 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3207 if (client_pub_pkey
) {
3208 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3211 /* Decrypt session key */
3212 sess_key_len
= PACKET_remaining(pkt
);
3213 if (!PACKET_get_bytes(pkt
, &data
, sess_key_len
)) {
3214 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3215 ERR_R_INTERNAL_ERROR
);
3218 /* TODO(size_t): Convert this function */
3219 if (ASN1_get_object((const unsigned char **)&data
, &Tlen
, &Ttag
,
3220 &Tclass
, (long)sess_key_len
) != V_ASN1_CONSTRUCTED
3221 || Ttag
!= V_ASN1_SEQUENCE
|| Tclass
!= V_ASN1_UNIVERSAL
) {
3222 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3223 SSL_R_DECRYPTION_FAILED
);
3228 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3230 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3231 SSL_R_DECRYPTION_FAILED
);
3234 /* Generate master secret */
3235 if (!ssl_generate_master_secret(s
, premaster_secret
,
3236 sizeof(premaster_secret
), 0)) {
3237 /* SSLfatal() already called */
3240 /* Check if pubkey from client certificate was used */
3241 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3243 s
->statem
.no_cert_verify
= 1;
3247 EVP_PKEY_CTX_free(pkey_ctx
);
3250 /* Should never happen */
3251 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3252 ERR_R_INTERNAL_ERROR
);
3257 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3259 unsigned long alg_k
;
3261 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3263 /* For PSK parse and retrieve identity, obtain PSK key */
3264 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3265 /* SSLfatal() already called */
3269 if (alg_k
& SSL_kPSK
) {
3270 /* Identity extracted earlier: should be nothing left */
3271 if (PACKET_remaining(pkt
) != 0) {
3272 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3273 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3274 SSL_R_LENGTH_MISMATCH
);
3277 /* PSK handled by ssl_generate_master_secret */
3278 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3279 /* SSLfatal() already called */
3282 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3283 if (!tls_process_cke_rsa(s
, pkt
)) {
3284 /* SSLfatal() already called */
3287 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3288 if (!tls_process_cke_dhe(s
, pkt
)) {
3289 /* SSLfatal() already called */
3292 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3293 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3294 /* SSLfatal() already called */
3297 } else if (alg_k
& SSL_kSRP
) {
3298 if (!tls_process_cke_srp(s
, pkt
)) {
3299 /* SSLfatal() already called */
3302 } else if (alg_k
& SSL_kGOST
) {
3303 if (!tls_process_cke_gost(s
, pkt
)) {
3304 /* SSLfatal() already called */
3308 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3309 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3310 SSL_R_UNKNOWN_CIPHER_TYPE
);
3314 return MSG_PROCESS_CONTINUE_PROCESSING
;
3316 #ifndef OPENSSL_NO_PSK
3317 OPENSSL_clear_free(s
->s3
->tmp
.psk
, s
->s3
->tmp
.psklen
);
3318 s
->s3
->tmp
.psk
= NULL
;
3320 return MSG_PROCESS_ERROR
;
3323 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3325 #ifndef OPENSSL_NO_SCTP
3326 if (wst
== WORK_MORE_A
) {
3327 if (SSL_IS_DTLS(s
)) {
3328 unsigned char sctpauthkey
[64];
3329 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3331 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3334 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3335 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3337 if (SSL_export_keying_material(s
, sctpauthkey
,
3338 sizeof(sctpauthkey
), labelbuffer
,
3339 sizeof(labelbuffer
), NULL
, 0,
3341 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3342 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3343 ERR_R_INTERNAL_ERROR
);
3347 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3348 sizeof(sctpauthkey
), sctpauthkey
);
3353 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3355 * No certificate verify or no peer certificate so we no longer need
3356 * the handshake_buffer
3358 if (!ssl3_digest_cached_records(s
, 0)) {
3359 /* SSLfatal() already called */
3362 return WORK_FINISHED_CONTINUE
;
3364 if (!s
->s3
->handshake_buffer
) {
3365 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3366 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3367 ERR_R_INTERNAL_ERROR
);
3371 * For sigalgs freeze the handshake buffer. If we support
3372 * extms we've done this already so this is a no-op
3374 if (!ssl3_digest_cached_records(s
, 1)) {
3375 /* SSLfatal() already called */
3380 return WORK_FINISHED_CONTINUE
;
3383 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3386 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3388 unsigned long l
, llen
;
3389 const unsigned char *certstart
, *certbytes
;
3390 STACK_OF(X509
) *sk
= NULL
;
3391 PACKET spkt
, context
;
3394 if ((sk
= sk_X509_new_null()) == NULL
) {
3395 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3396 ERR_R_MALLOC_FAILURE
);
3400 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3401 if ((SSL_IS_TLS13(s
) && !PACKET_get_length_prefixed_1(pkt
, &context
))
3402 || !PACKET_get_net_3(pkt
, &llen
)
3403 || !PACKET_get_sub_packet(pkt
, &spkt
, llen
)
3404 || PACKET_remaining(pkt
) != 0) {
3405 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3406 SSL_R_LENGTH_MISMATCH
);
3410 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3411 if (!PACKET_get_net_3(&spkt
, &l
)
3412 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3413 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3414 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3415 SSL_R_CERT_LENGTH_MISMATCH
);
3419 certstart
= certbytes
;
3420 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, l
);
3422 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3423 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3426 if (certbytes
!= (certstart
+ l
)) {
3427 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3428 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3429 SSL_R_CERT_LENGTH_MISMATCH
);
3433 if (SSL_IS_TLS13(s
)) {
3434 RAW_EXTENSION
*rawexts
= NULL
;
3437 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3438 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3439 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3443 if (!tls_collect_extensions(s
, &extensions
,
3444 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3445 NULL
, chainidx
== 0)
3446 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3447 rawexts
, x
, chainidx
,
3448 PACKET_remaining(&spkt
) == 0)) {
3449 OPENSSL_free(rawexts
);
3452 OPENSSL_free(rawexts
);
3455 if (!sk_X509_push(sk
, x
)) {
3456 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3457 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3458 ERR_R_MALLOC_FAILURE
);
3464 if (sk_X509_num(sk
) <= 0) {
3465 /* TLS does not mind 0 certs returned */
3466 if (s
->version
== SSL3_VERSION
) {
3467 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3468 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3469 SSL_R_NO_CERTIFICATES_RETURNED
);
3472 /* Fail for TLS only if we required a certificate */
3473 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3474 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3475 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3476 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3477 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3480 /* No client certificate so digest cached records */
3481 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3482 /* SSLfatal() already called */
3487 i
= ssl_verify_cert_chain(s
, sk
);
3489 SSLfatal(s
, ssl_verify_alarm_type(s
->verify_result
),
3490 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3491 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3495 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3496 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3499 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3501 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3502 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3503 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3508 X509_free(s
->session
->peer
);
3509 s
->session
->peer
= sk_X509_shift(sk
);
3510 s
->session
->verify_result
= s
->verify_result
;
3512 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3513 s
->session
->peer_chain
= sk
;
3516 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3519 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3520 /* SSLfatal() already called */
3525 * Inconsistency alert: cert_chain does *not* include the peer's own
3526 * certificate, while we do include it in statem_clnt.c
3530 /* Save the current hash state for when we receive the CertificateVerify */
3532 && !ssl_handshake_hash(s
, s
->cert_verify_hash
,
3533 sizeof(s
->cert_verify_hash
),
3534 &s
->cert_verify_hash_len
)) {
3535 /* SSLfatal() already called */
3539 ret
= MSG_PROCESS_CONTINUE_READING
;
3543 sk_X509_pop_free(sk
, X509_free
);
3547 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3549 CERT_PKEY
*cpk
= s
->s3
->tmp
.cert
;
3552 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3553 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3558 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3559 * for the server Certificate message
3561 if (SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3562 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3563 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3566 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3567 /* SSLfatal() already called */
3574 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
3576 unsigned char *senc
= NULL
;
3577 EVP_CIPHER_CTX
*ctx
= NULL
;
3578 HMAC_CTX
*hctx
= NULL
;
3579 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3580 const unsigned char *const_p
;
3581 int len
, slen_full
, slen
, lenfinal
;
3584 SSL_CTX
*tctx
= s
->session_ctx
;
3585 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3586 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3588 size_t macoffset
, macendoffset
;
3590 unsigned char age_add_c
[sizeof(uint32_t)];
3594 if (SSL_IS_TLS13(s
)) {
3595 if (ssl_randbytes(s
, age_add_u
.age_add_c
, sizeof(age_add_u
)) <= 0) {
3596 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3597 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3598 ERR_R_INTERNAL_ERROR
);
3601 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
3603 * ticket_nonce is set to a single 0 byte because we only ever send a
3604 * single ticket per connection. IMPORTANT: If we ever support multiple
3605 * tickets per connection then this will need to be changed.
3607 OPENSSL_free(s
->session
->ext
.tick_nonce
);
3608 s
->session
->ext
.tick_nonce
= OPENSSL_zalloc(sizeof(char));
3609 if (s
->session
->ext
.tick_nonce
== NULL
) {
3610 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3611 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3612 ERR_R_MALLOC_FAILURE
);
3615 s
->session
->ext
.tick_nonce_len
= 1;
3616 s
->session
->time
= (long)time(NULL
);
3617 if (s
->s3
->alpn_selected
!= NULL
) {
3618 OPENSSL_free(s
->session
->ext
.alpn_selected
);
3619 s
->session
->ext
.alpn_selected
=
3620 OPENSSL_memdup(s
->s3
->alpn_selected
, s
->s3
->alpn_selected_len
);
3621 if (s
->session
->ext
.alpn_selected
== NULL
) {
3622 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3623 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3624 ERR_R_MALLOC_FAILURE
);
3627 s
->session
->ext
.alpn_selected_len
= s
->s3
->alpn_selected_len
;
3629 s
->session
->ext
.max_early_data
= s
->max_early_data
;
3632 /* get session encoding length */
3633 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3635 * Some length values are 16 bits, so forget it if session is too
3638 if (slen_full
== 0 || slen_full
> 0xFF00) {
3639 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3640 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3643 senc
= OPENSSL_malloc(slen_full
);
3645 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3646 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
3650 ctx
= EVP_CIPHER_CTX_new();
3651 hctx
= HMAC_CTX_new();
3652 if (ctx
== NULL
|| hctx
== NULL
) {
3653 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3654 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
3659 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3660 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3661 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3666 * create a fresh copy (not shared with other threads) to clean up
3669 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3671 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3672 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3675 sess
->session_id_length
= 0; /* ID is irrelevant for the ticket */
3677 slen
= i2d_SSL_SESSION(sess
, NULL
);
3678 if (slen
== 0 || slen
> slen_full
) {
3679 /* shouldn't ever happen */
3680 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3681 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3682 SSL_SESSION_free(sess
);
3686 if (!i2d_SSL_SESSION(sess
, &p
)) {
3687 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3688 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3689 SSL_SESSION_free(sess
);
3692 SSL_SESSION_free(sess
);
3695 * Initialize HMAC and cipher contexts. If callback present it does
3696 * all the work otherwise use generated values from parent ctx.
3698 if (tctx
->ext
.ticket_key_cb
) {
3699 /* if 0 is returned, write an empty ticket */
3700 int ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3705 /* Put timeout and length */
3706 if (!WPACKET_put_bytes_u32(pkt
, 0)
3707 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3708 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3709 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3710 ERR_R_INTERNAL_ERROR
);
3714 EVP_CIPHER_CTX_free(ctx
);
3715 HMAC_CTX_free(hctx
);
3719 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3720 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3721 SSL_R_CALLBACK_FAILED
);
3724 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
3726 const EVP_CIPHER
*cipher
= EVP_aes_256_cbc();
3728 iv_len
= EVP_CIPHER_iv_length(cipher
);
3729 if (ssl_randbytes(s
, iv
, iv_len
) <= 0
3730 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3731 tctx
->ext
.tick_aes_key
, iv
)
3732 || !HMAC_Init_ex(hctx
, tctx
->ext
.tick_hmac_key
,
3733 sizeof(tctx
->ext
.tick_hmac_key
),
3734 EVP_sha256(), NULL
)) {
3735 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3736 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3737 ERR_R_INTERNAL_ERROR
);
3740 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3741 sizeof(tctx
->ext
.tick_key_name
));
3745 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3746 * unspecified for resumed session (for simplicity).
3747 * In TLSv1.3 we reset the "time" field above, and always specify the
3750 if (!WPACKET_put_bytes_u32(pkt
,
3751 (s
->hit
&& !SSL_IS_TLS13(s
))
3752 ? 0 : s
->session
->timeout
)
3754 && (!WPACKET_put_bytes_u32(pkt
, age_add_u
.age_add
)
3755 || !WPACKET_sub_memcpy_u8(pkt
, s
->session
->ext
.tick_nonce
,
3756 s
->session
->ext
.tick_nonce_len
)))
3757 /* Now the actual ticket data */
3758 || !WPACKET_start_sub_packet_u16(pkt
)
3759 || !WPACKET_get_total_written(pkt
, &macoffset
)
3760 /* Output key name */
3761 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3763 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3764 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3766 /* Encrypt session data */
3767 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3768 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3769 || encdata1
!= encdata2
3770 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3771 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3772 || encdata1
+ len
!= encdata2
3773 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3774 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3775 || !HMAC_Update(hctx
,
3776 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3777 macendoffset
- macoffset
)
3778 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3779 || !HMAC_Final(hctx
, macdata1
, &hlen
)
3780 || hlen
> EVP_MAX_MD_SIZE
3781 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3782 || macdata1
!= macdata2
3783 || !WPACKET_close(pkt
)) {
3784 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3785 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3789 && !tls_construct_extensions(s
, pkt
,
3790 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
3792 /* SSLfatal() already called */
3795 EVP_CIPHER_CTX_free(ctx
);
3796 HMAC_CTX_free(hctx
);
3802 EVP_CIPHER_CTX_free(ctx
);
3803 HMAC_CTX_free(hctx
);
3808 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3809 * create a separate message. Returns 1 on success or 0 on failure.
3811 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
3813 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
3814 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
3815 s
->ext
.ocsp
.resp_len
)) {
3816 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
,
3817 ERR_R_INTERNAL_ERROR
);
3824 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
3826 if (!tls_construct_cert_status_body(s
, pkt
)) {
3827 /* SSLfatal() already called */
3834 #ifndef OPENSSL_NO_NEXTPROTONEG
3836 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3837 * It sets the next_proto member in s if found
3839 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
3841 PACKET next_proto
, padding
;
3842 size_t next_proto_len
;
3845 * The payload looks like:
3847 * uint8 proto[proto_len];
3848 * uint8 padding_len;
3849 * uint8 padding[padding_len];
3851 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
3852 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
3853 || PACKET_remaining(pkt
) > 0) {
3854 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
3855 SSL_R_LENGTH_MISMATCH
);
3856 return MSG_PROCESS_ERROR
;
3859 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
3861 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
3862 ERR_R_INTERNAL_ERROR
);
3863 return MSG_PROCESS_ERROR
;
3866 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
3868 return MSG_PROCESS_CONTINUE_READING
;
3872 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
3874 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
3876 /* SSLfatal() already called */
3883 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL
*s
, PACKET
*pkt
)
3885 if (PACKET_remaining(pkt
) != 0) {
3886 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
3887 SSL_R_LENGTH_MISMATCH
);
3888 return MSG_PROCESS_ERROR
;
3891 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
3892 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
3893 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
3894 ERR_R_INTERNAL_ERROR
);
3895 return MSG_PROCESS_ERROR
;
3899 * EndOfEarlyData signals a key change so the end of the message must be on
3900 * a record boundary.
3902 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
3903 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
3904 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
3905 SSL_R_NOT_ON_RECORD_BOUNDARY
);
3906 return MSG_PROCESS_ERROR
;
3909 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
3910 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
3911 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
3912 /* SSLfatal() already called */
3913 return MSG_PROCESS_ERROR
;
3916 return MSG_PROCESS_CONTINUE_READING
;