2 * Copyright 1995-2018 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 Apache License 2.0 (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_local.h"
14 #include "statem_local.h"
15 #include "internal/constant_time.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/buffer.h>
18 #include <openssl/rand.h>
19 #include <openssl/objects.h>
20 #include <openssl/evp.h>
21 #include <openssl/hmac.h>
22 #include <openssl/x509.h>
23 #include <openssl/dh.h>
24 #include <openssl/bn.h>
25 #include <openssl/md5.h>
26 #include <openssl/trace.h>
27 #include <openssl/core_names.h>
28 #include <openssl/asn1t.h>
30 #define TICKET_NONCE_SIZE 8
34 ASN1_TYPE
*opaqueBlob
;
37 DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE
)
39 ASN1_SEQUENCE(GOST_KX_MESSAGE
) = {
40 ASN1_SIMPLE(GOST_KX_MESSAGE
, kxBlob
, ASN1_ANY
),
41 ASN1_OPT(GOST_KX_MESSAGE
, opaqueBlob
, ASN1_ANY
),
42 } ASN1_SEQUENCE_END(GOST_KX_MESSAGE
)
44 IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE
)
46 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
);
49 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
50 * handshake state transitions when a TLSv1.3 server is reading messages from
51 * the client. The message type that the client has sent is provided in |mt|.
52 * The current state is in |s->statem.hand_state|.
54 * Return values are 1 for success (transition allowed) and 0 on error
55 * (transition not allowed)
57 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
59 OSSL_STATEM
*st
= &s
->statem
;
62 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
63 * not negotiated TLSv1.3 yet, so that case is handled by
64 * ossl_statem_server_read_transition()
66 switch (st
->hand_state
) {
70 case TLS_ST_EARLY_DATA
:
71 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
72 if (mt
== SSL3_MT_CLIENT_HELLO
) {
73 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
77 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
78 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
79 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
86 case TLS_ST_SR_END_OF_EARLY_DATA
:
87 case TLS_ST_SW_FINISHED
:
88 if (s
->s3
.tmp
.cert_request
) {
89 if (mt
== SSL3_MT_CERTIFICATE
) {
90 st
->hand_state
= TLS_ST_SR_CERT
;
94 if (mt
== SSL3_MT_FINISHED
) {
95 st
->hand_state
= TLS_ST_SR_FINISHED
;
102 if (s
->session
->peer
== NULL
) {
103 if (mt
== SSL3_MT_FINISHED
) {
104 st
->hand_state
= TLS_ST_SR_FINISHED
;
108 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
109 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
115 case TLS_ST_SR_CERT_VRFY
:
116 if (mt
== SSL3_MT_FINISHED
) {
117 st
->hand_state
= TLS_ST_SR_FINISHED
;
124 * Its never ok to start processing handshake messages in the middle of
125 * early data (i.e. before we've received the end of early data alert)
127 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
130 if (mt
== SSL3_MT_CERTIFICATE
131 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
132 st
->hand_state
= TLS_ST_SR_CERT
;
136 if (mt
== SSL3_MT_KEY_UPDATE
) {
137 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
143 /* No valid transition found */
148 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
149 * handshake state transitions when the server is reading messages from the
150 * client. The message type that the client has sent is provided in |mt|. The
151 * current state is in |s->statem.hand_state|.
153 * Return values are 1 for success (transition allowed) and 0 on error
154 * (transition not allowed)
156 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
158 OSSL_STATEM
*st
= &s
->statem
;
160 if (SSL_IS_TLS13(s
)) {
161 if (!ossl_statem_server13_read_transition(s
, mt
))
166 switch (st
->hand_state
) {
172 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
173 if (mt
== SSL3_MT_CLIENT_HELLO
) {
174 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
179 case TLS_ST_SW_SRVR_DONE
:
181 * If we get a CKE message after a ServerDone then either
182 * 1) We didn't request a Certificate
184 * 2) If we did request one then
185 * a) We allow no Certificate to be returned
187 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
188 * list if we requested a certificate)
190 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
191 if (s
->s3
.tmp
.cert_request
) {
192 if (s
->version
== SSL3_VERSION
) {
193 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
194 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
196 * This isn't an unexpected message as such - we're just
197 * not going to accept it because we require a client
200 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
201 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
202 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
205 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
209 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
212 } else if (s
->s3
.tmp
.cert_request
) {
213 if (mt
== SSL3_MT_CERTIFICATE
) {
214 st
->hand_state
= TLS_ST_SR_CERT
;
221 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
222 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
227 case TLS_ST_SR_KEY_EXCH
:
229 * We should only process a CertificateVerify message if we have
230 * received a Certificate from the client. If so then |s->session->peer|
231 * will be non NULL. In some instances a CertificateVerify message is
232 * not required even if the peer has sent a Certificate (e.g. such as in
233 * the case of static DH). In that case |st->no_cert_verify| should be
236 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
237 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
239 * For the ECDH ciphersuites when the client sends its ECDH
240 * pub key in a certificate, the CertificateVerify message is
241 * not sent. Also for GOST ciphersuites when the client uses
242 * its key from the certificate for key exchange.
244 st
->hand_state
= TLS_ST_SR_CHANGE
;
248 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
249 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
255 case TLS_ST_SR_CERT_VRFY
:
256 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
257 st
->hand_state
= TLS_ST_SR_CHANGE
;
262 case TLS_ST_SR_CHANGE
:
263 #ifndef OPENSSL_NO_NEXTPROTONEG
264 if (s
->s3
.npn_seen
) {
265 if (mt
== SSL3_MT_NEXT_PROTO
) {
266 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
271 if (mt
== SSL3_MT_FINISHED
) {
272 st
->hand_state
= TLS_ST_SR_FINISHED
;
275 #ifndef OPENSSL_NO_NEXTPROTONEG
280 #ifndef OPENSSL_NO_NEXTPROTONEG
281 case TLS_ST_SR_NEXT_PROTO
:
282 if (mt
== SSL3_MT_FINISHED
) {
283 st
->hand_state
= TLS_ST_SR_FINISHED
;
289 case TLS_ST_SW_FINISHED
:
290 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
291 st
->hand_state
= TLS_ST_SR_CHANGE
;
298 /* No valid transition found */
299 if (SSL_IS_DTLS(s
) && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
303 * CCS messages don't have a message sequence number so this is probably
304 * because of an out-of-order CCS. We'll just drop it.
307 s
->rwstate
= SSL_READING
;
308 rbio
= SSL_get_rbio(s
);
309 BIO_clear_retry_flags(rbio
);
310 BIO_set_retry_read(rbio
);
313 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
,
314 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
315 SSL_R_UNEXPECTED_MESSAGE
);
320 * Should we send a ServerKeyExchange message?
322 * Valid return values are:
326 static int send_server_key_exchange(SSL
*s
)
328 unsigned long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
331 * only send a ServerKeyExchange if DH or fortezza but we have a
332 * sign only certificate PSK: may send PSK identity hints For
333 * ECC ciphersuites, we send a serverKeyExchange message only if
334 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
335 * the server certificate contains the server's public key for
338 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
340 * PSK: send ServerKeyExchange if PSK identity hint if
343 #ifndef OPENSSL_NO_PSK
344 /* Only send SKE if we have identity hint for plain PSK */
345 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
346 && s
->cert
->psk_identity_hint
)
347 /* For other PSK always send SKE */
348 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
350 #ifndef OPENSSL_NO_SRP
351 /* SRP: send ServerKeyExchange */
352 || (alg_k
& SSL_kSRP
)
362 * Should we send a CertificateRequest message?
364 * Valid return values are:
368 int send_certificate_request(SSL
*s
)
371 /* don't request cert unless asked for it: */
372 s
->verify_mode
& SSL_VERIFY_PEER
374 * don't request if post-handshake-only unless doing
375 * post-handshake in TLSv1.3:
377 && (!SSL_IS_TLS13(s
) || !(s
->verify_mode
& SSL_VERIFY_POST_HANDSHAKE
)
378 || s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
)
380 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
383 && (s
->certreqs_sent
< 1 ||
384 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
386 * never request cert in anonymous ciphersuites (see
387 * section "Certificate request" in SSL 3 drafts and in
390 && (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
392 * ... except when the application insists on
393 * verification (against the specs, but statem_clnt.c accepts
396 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
397 /* don't request certificate for SRP auth */
398 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
400 * With normal PSK Certificates and Certificate Requests
403 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
411 * ossl_statem_server13_write_transition() works out what handshake state to
412 * move to next when a TLSv1.3 server is writing messages to be sent to the
415 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
417 OSSL_STATEM
*st
= &s
->statem
;
420 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
421 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
424 switch (st
->hand_state
) {
426 /* Shouldn't happen */
427 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
428 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION
,
429 ERR_R_INTERNAL_ERROR
);
430 return WRITE_TRAN_ERROR
;
433 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
434 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
435 return WRITE_TRAN_CONTINUE
;
437 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
438 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
439 return WRITE_TRAN_CONTINUE
;
441 /* Try to read from the client instead */
442 return WRITE_TRAN_FINISHED
;
444 case TLS_ST_SR_CLNT_HELLO
:
445 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
446 return WRITE_TRAN_CONTINUE
;
448 case TLS_ST_SW_SRVR_HELLO
:
449 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
450 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
451 st
->hand_state
= TLS_ST_SW_CHANGE
;
452 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
453 st
->hand_state
= TLS_ST_EARLY_DATA
;
455 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
456 return WRITE_TRAN_CONTINUE
;
458 case TLS_ST_SW_CHANGE
:
459 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
460 st
->hand_state
= TLS_ST_EARLY_DATA
;
462 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
463 return WRITE_TRAN_CONTINUE
;
465 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
467 st
->hand_state
= TLS_ST_SW_FINISHED
;
468 else if (send_certificate_request(s
))
469 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
471 st
->hand_state
= TLS_ST_SW_CERT
;
473 return WRITE_TRAN_CONTINUE
;
475 case TLS_ST_SW_CERT_REQ
:
476 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
477 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
478 st
->hand_state
= TLS_ST_OK
;
480 st
->hand_state
= TLS_ST_SW_CERT
;
482 return WRITE_TRAN_CONTINUE
;
485 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
486 return WRITE_TRAN_CONTINUE
;
488 case TLS_ST_SW_CERT_VRFY
:
489 st
->hand_state
= TLS_ST_SW_FINISHED
;
490 return WRITE_TRAN_CONTINUE
;
492 case TLS_ST_SW_FINISHED
:
493 st
->hand_state
= TLS_ST_EARLY_DATA
;
494 return WRITE_TRAN_CONTINUE
;
496 case TLS_ST_EARLY_DATA
:
497 return WRITE_TRAN_FINISHED
;
499 case TLS_ST_SR_FINISHED
:
501 * Technically we have finished the handshake at this point, but we're
502 * going to remain "in_init" for now and write out any session tickets
505 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
506 s
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
;
507 } else if (!s
->ext
.ticket_expected
) {
509 * If we're not going to renew the ticket then we just finish the
510 * handshake at this point.
512 st
->hand_state
= TLS_ST_OK
;
513 return WRITE_TRAN_CONTINUE
;
515 if (s
->num_tickets
> s
->sent_tickets
)
516 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
518 st
->hand_state
= TLS_ST_OK
;
519 return WRITE_TRAN_CONTINUE
;
521 case TLS_ST_SR_KEY_UPDATE
:
522 case TLS_ST_SW_KEY_UPDATE
:
523 st
->hand_state
= TLS_ST_OK
;
524 return WRITE_TRAN_CONTINUE
;
526 case TLS_ST_SW_SESSION_TICKET
:
527 /* In a resumption we only ever send a maximum of one new ticket.
528 * Following an initial handshake we send the number of tickets we have
529 * been configured for.
531 if (s
->hit
|| s
->num_tickets
<= s
->sent_tickets
) {
532 /* We've written enough tickets out. */
533 st
->hand_state
= TLS_ST_OK
;
535 return WRITE_TRAN_CONTINUE
;
540 * ossl_statem_server_write_transition() works out what handshake state to move
541 * to next when the server is writing messages to be sent to the client.
543 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
545 OSSL_STATEM
*st
= &s
->statem
;
548 * Note that before the ClientHello we don't know what version we are going
549 * to negotiate yet, so we don't take this branch until later
553 return ossl_statem_server13_write_transition(s
);
555 switch (st
->hand_state
) {
557 /* Shouldn't happen */
558 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
559 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION
,
560 ERR_R_INTERNAL_ERROR
);
561 return WRITE_TRAN_ERROR
;
564 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
565 /* We must be trying to renegotiate */
566 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
567 st
->request_state
= TLS_ST_BEFORE
;
568 return WRITE_TRAN_CONTINUE
;
570 /* Must be an incoming ClientHello */
571 if (!tls_setup_handshake(s
)) {
572 /* SSLfatal() already called */
573 return WRITE_TRAN_ERROR
;
578 /* Just go straight to trying to read from the client */
579 return WRITE_TRAN_FINISHED
;
581 case TLS_ST_SW_HELLO_REQ
:
582 st
->hand_state
= TLS_ST_OK
;
583 return WRITE_TRAN_CONTINUE
;
585 case TLS_ST_SR_CLNT_HELLO
:
586 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
587 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
)) {
588 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
589 } else if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
590 /* We must have rejected the renegotiation */
591 st
->hand_state
= TLS_ST_OK
;
592 return WRITE_TRAN_CONTINUE
;
594 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
596 return WRITE_TRAN_CONTINUE
;
598 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
599 return WRITE_TRAN_FINISHED
;
601 case TLS_ST_SW_SRVR_HELLO
:
603 if (s
->ext
.ticket_expected
)
604 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
606 st
->hand_state
= TLS_ST_SW_CHANGE
;
608 /* Check if it is anon DH or anon ECDH, */
609 /* normal PSK or SRP */
610 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
&
611 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
612 st
->hand_state
= TLS_ST_SW_CERT
;
613 } else if (send_server_key_exchange(s
)) {
614 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
615 } else if (send_certificate_request(s
)) {
616 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
618 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
621 return WRITE_TRAN_CONTINUE
;
624 if (s
->ext
.status_expected
) {
625 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
626 return WRITE_TRAN_CONTINUE
;
630 case TLS_ST_SW_CERT_STATUS
:
631 if (send_server_key_exchange(s
)) {
632 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
633 return WRITE_TRAN_CONTINUE
;
637 case TLS_ST_SW_KEY_EXCH
:
638 if (send_certificate_request(s
)) {
639 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
640 return WRITE_TRAN_CONTINUE
;
644 case TLS_ST_SW_CERT_REQ
:
645 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
646 return WRITE_TRAN_CONTINUE
;
648 case TLS_ST_SW_SRVR_DONE
:
649 return WRITE_TRAN_FINISHED
;
651 case TLS_ST_SR_FINISHED
:
653 st
->hand_state
= TLS_ST_OK
;
654 return WRITE_TRAN_CONTINUE
;
655 } else if (s
->ext
.ticket_expected
) {
656 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
658 st
->hand_state
= TLS_ST_SW_CHANGE
;
660 return WRITE_TRAN_CONTINUE
;
662 case TLS_ST_SW_SESSION_TICKET
:
663 st
->hand_state
= TLS_ST_SW_CHANGE
;
664 return WRITE_TRAN_CONTINUE
;
666 case TLS_ST_SW_CHANGE
:
667 st
->hand_state
= TLS_ST_SW_FINISHED
;
668 return WRITE_TRAN_CONTINUE
;
670 case TLS_ST_SW_FINISHED
:
672 return WRITE_TRAN_FINISHED
;
674 st
->hand_state
= TLS_ST_OK
;
675 return WRITE_TRAN_CONTINUE
;
680 * Perform any pre work that needs to be done prior to sending a message from
681 * the server to the client.
683 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
685 OSSL_STATEM
*st
= &s
->statem
;
687 switch (st
->hand_state
) {
689 /* No pre work to be done */
692 case TLS_ST_SW_HELLO_REQ
:
695 dtls1_clear_sent_buffer(s
);
698 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
700 if (SSL_IS_DTLS(s
)) {
701 dtls1_clear_sent_buffer(s
);
702 /* We don't buffer this message so don't use the timer */
707 case TLS_ST_SW_SRVR_HELLO
:
708 if (SSL_IS_DTLS(s
)) {
710 * Messages we write from now on should be buffered and
711 * retransmitted if necessary, so we need to use the timer now
717 case TLS_ST_SW_SRVR_DONE
:
718 #ifndef OPENSSL_NO_SCTP
719 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
720 /* Calls SSLfatal() as required */
721 return dtls_wait_for_dry(s
);
724 return WORK_FINISHED_CONTINUE
;
726 case TLS_ST_SW_SESSION_TICKET
:
727 if (SSL_IS_TLS13(s
) && s
->sent_tickets
== 0) {
729 * Actually this is the end of the handshake, but we're going
730 * straight into writing the session ticket out. So we finish off
731 * the handshake, but keep the various buffers active.
733 * Calls SSLfatal as required.
735 return tls_finish_handshake(s
, wst
, 0, 0);
736 } if (SSL_IS_DTLS(s
)) {
738 * We're into the last flight. We don't retransmit the last flight
739 * unless we need to, so we don't use the timer
745 case TLS_ST_SW_CHANGE
:
748 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
749 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
750 /* SSLfatal() already called */
753 if (SSL_IS_DTLS(s
)) {
755 * We're into the last flight. We don't retransmit the last flight
756 * unless we need to, so we don't use the timer. This might have
757 * already been set to 0 if we sent a NewSessionTicket message,
758 * but we'll set it again here in case we didn't.
762 return WORK_FINISHED_CONTINUE
;
764 case TLS_ST_EARLY_DATA
:
765 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
766 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0)
767 return WORK_FINISHED_CONTINUE
;
771 /* Calls SSLfatal() as required */
772 return tls_finish_handshake(s
, wst
, 1, 1);
775 return WORK_FINISHED_CONTINUE
;
778 static ossl_inline
int conn_is_closed(void)
780 switch (get_last_sys_error()) {
785 #if defined(ECONNRESET)
789 #if defined(WSAECONNRESET)
799 * Perform any work that needs to be done after sending a message from the
800 * server to the client.
802 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
804 OSSL_STATEM
*st
= &s
->statem
;
808 switch (st
->hand_state
) {
810 /* No post work to be done */
813 case TLS_ST_SW_HELLO_REQ
:
814 if (statem_flush(s
) != 1)
816 if (!ssl3_init_finished_mac(s
)) {
817 /* SSLfatal() already called */
822 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
823 if (statem_flush(s
) != 1)
825 /* HelloVerifyRequest resets Finished MAC */
826 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
827 /* SSLfatal() already called */
831 * The next message should be another ClientHello which we need to
832 * treat like it was the first packet
837 case TLS_ST_SW_SRVR_HELLO
:
838 if (SSL_IS_TLS13(s
) && s
->hello_retry_request
== SSL_HRR_PENDING
) {
839 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
840 && statem_flush(s
) != 1)
844 #ifndef OPENSSL_NO_SCTP
845 if (SSL_IS_DTLS(s
) && s
->hit
) {
846 unsigned char sctpauthkey
[64];
847 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
851 * Add new shared key for SCTP-Auth, will be ignored if no
854 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
855 sizeof(DTLS1_SCTP_AUTH_LABEL
));
857 /* Don't include the terminating zero. */
858 labellen
= sizeof(labelbuffer
) - 1;
859 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
862 if (SSL_export_keying_material(s
, sctpauthkey
,
863 sizeof(sctpauthkey
), labelbuffer
,
866 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
867 SSL_F_OSSL_STATEM_SERVER_POST_WORK
,
868 ERR_R_INTERNAL_ERROR
);
872 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
873 sizeof(sctpauthkey
), sctpauthkey
);
877 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
878 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
882 case TLS_ST_SW_CHANGE
:
883 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
884 if (!statem_flush(s
))
889 if (SSL_IS_TLS13(s
)) {
890 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
891 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
892 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
893 /* SSLfatal() already called */
897 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
898 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
899 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
900 /* SSLfatal() already called */
904 * We don't yet know whether the next record we are going to receive
905 * is an unencrypted alert, an encrypted alert, or an encrypted
906 * handshake message. We temporarily tolerate unencrypted alerts.
908 s
->statem
.enc_read_state
= ENC_READ_STATE_ALLOW_PLAIN_ALERTS
;
912 #ifndef OPENSSL_NO_SCTP
913 if (SSL_IS_DTLS(s
) && !s
->hit
) {
915 * Change to new shared key of SCTP-Auth, will be ignored if
918 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
922 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
923 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
925 /* SSLfatal() already called */
930 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
933 case TLS_ST_SW_SRVR_DONE
:
934 if (statem_flush(s
) != 1)
938 case TLS_ST_SW_FINISHED
:
939 if (statem_flush(s
) != 1)
941 #ifndef OPENSSL_NO_SCTP
942 if (SSL_IS_DTLS(s
) && s
->hit
) {
944 * Change to new shared key of SCTP-Auth, will be ignored if
947 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
951 if (SSL_IS_TLS13(s
)) {
952 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
953 s
->master_secret
, s
->handshake_secret
, 0,
954 &s
->session
->master_key_length
)
955 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
956 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
957 /* SSLfatal() already called */
962 case TLS_ST_SW_CERT_REQ
:
963 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
964 if (statem_flush(s
) != 1)
969 case TLS_ST_SW_KEY_UPDATE
:
970 if (statem_flush(s
) != 1)
972 if (!tls13_update_key(s
, 1)) {
973 /* SSLfatal() already called */
978 case TLS_ST_SW_SESSION_TICKET
:
980 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1) {
981 if (SSL_get_error(s
, 0) == SSL_ERROR_SYSCALL
982 && conn_is_closed()) {
984 * We ignore connection closed errors in TLSv1.3 when sending a
985 * NewSessionTicket and behave as if we were successful. This is
986 * so that we are still able to read data sent to us by a client
987 * that closes soon after the end of the handshake without
988 * waiting to read our post-handshake NewSessionTickets.
990 s
->rwstate
= SSL_NOTHING
;
999 return WORK_FINISHED_CONTINUE
;
1003 * Get the message construction function and message type for sending from the
1006 * Valid return values are:
1010 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
1011 confunc_f
*confunc
, int *mt
)
1013 OSSL_STATEM
*st
= &s
->statem
;
1015 switch (st
->hand_state
) {
1017 /* Shouldn't happen */
1018 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1019 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE
,
1020 SSL_R_BAD_HANDSHAKE_STATE
);
1023 case TLS_ST_SW_CHANGE
:
1025 *confunc
= dtls_construct_change_cipher_spec
;
1027 *confunc
= tls_construct_change_cipher_spec
;
1028 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1031 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1032 *confunc
= dtls_construct_hello_verify_request
;
1033 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1036 case TLS_ST_SW_HELLO_REQ
:
1037 /* No construction function needed */
1039 *mt
= SSL3_MT_HELLO_REQUEST
;
1042 case TLS_ST_SW_SRVR_HELLO
:
1043 *confunc
= tls_construct_server_hello
;
1044 *mt
= SSL3_MT_SERVER_HELLO
;
1047 case TLS_ST_SW_CERT
:
1048 *confunc
= tls_construct_server_certificate
;
1049 *mt
= SSL3_MT_CERTIFICATE
;
1052 case TLS_ST_SW_CERT_VRFY
:
1053 *confunc
= tls_construct_cert_verify
;
1054 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1058 case TLS_ST_SW_KEY_EXCH
:
1059 *confunc
= tls_construct_server_key_exchange
;
1060 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1063 case TLS_ST_SW_CERT_REQ
:
1064 *confunc
= tls_construct_certificate_request
;
1065 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1068 case TLS_ST_SW_SRVR_DONE
:
1069 *confunc
= tls_construct_server_done
;
1070 *mt
= SSL3_MT_SERVER_DONE
;
1073 case TLS_ST_SW_SESSION_TICKET
:
1074 *confunc
= tls_construct_new_session_ticket
;
1075 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1078 case TLS_ST_SW_CERT_STATUS
:
1079 *confunc
= tls_construct_cert_status
;
1080 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1083 case TLS_ST_SW_FINISHED
:
1084 *confunc
= tls_construct_finished
;
1085 *mt
= SSL3_MT_FINISHED
;
1088 case TLS_ST_EARLY_DATA
:
1090 *mt
= SSL3_MT_DUMMY
;
1093 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1094 *confunc
= tls_construct_encrypted_extensions
;
1095 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1098 case TLS_ST_SW_KEY_UPDATE
:
1099 *confunc
= tls_construct_key_update
;
1100 *mt
= SSL3_MT_KEY_UPDATE
;
1108 * Maximum size (excluding the Handshake header) of a ClientHello message,
1109 * calculated as follows:
1111 * 2 + # client_version
1112 * 32 + # only valid length for random
1113 * 1 + # length of session_id
1114 * 32 + # maximum size for session_id
1115 * 2 + # length of cipher suites
1116 * 2^16-2 + # maximum length of cipher suites array
1117 * 1 + # length of compression_methods
1118 * 2^8-1 + # maximum length of compression methods
1119 * 2 + # length of extensions
1120 * 2^16-1 # maximum length of extensions
1122 #define CLIENT_HELLO_MAX_LENGTH 131396
1124 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1125 #define NEXT_PROTO_MAX_LENGTH 514
1128 * Returns the maximum allowed length for the current message that we are
1129 * reading. Excludes the message header.
1131 size_t ossl_statem_server_max_message_size(SSL
*s
)
1133 OSSL_STATEM
*st
= &s
->statem
;
1135 switch (st
->hand_state
) {
1137 /* Shouldn't happen */
1140 case TLS_ST_SR_CLNT_HELLO
:
1141 return CLIENT_HELLO_MAX_LENGTH
;
1143 case TLS_ST_SR_END_OF_EARLY_DATA
:
1144 return END_OF_EARLY_DATA_MAX_LENGTH
;
1146 case TLS_ST_SR_CERT
:
1147 return s
->max_cert_list
;
1149 case TLS_ST_SR_KEY_EXCH
:
1150 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1152 case TLS_ST_SR_CERT_VRFY
:
1153 return SSL3_RT_MAX_PLAIN_LENGTH
;
1155 #ifndef OPENSSL_NO_NEXTPROTONEG
1156 case TLS_ST_SR_NEXT_PROTO
:
1157 return NEXT_PROTO_MAX_LENGTH
;
1160 case TLS_ST_SR_CHANGE
:
1161 return CCS_MAX_LENGTH
;
1163 case TLS_ST_SR_FINISHED
:
1164 return FINISHED_MAX_LENGTH
;
1166 case TLS_ST_SR_KEY_UPDATE
:
1167 return KEY_UPDATE_MAX_LENGTH
;
1172 * Process a message that the server has received from the client.
1174 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1176 OSSL_STATEM
*st
= &s
->statem
;
1178 switch (st
->hand_state
) {
1180 /* Shouldn't happen */
1181 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1182 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE
,
1183 ERR_R_INTERNAL_ERROR
);
1184 return MSG_PROCESS_ERROR
;
1186 case TLS_ST_SR_CLNT_HELLO
:
1187 return tls_process_client_hello(s
, pkt
);
1189 case TLS_ST_SR_END_OF_EARLY_DATA
:
1190 return tls_process_end_of_early_data(s
, pkt
);
1192 case TLS_ST_SR_CERT
:
1193 return tls_process_client_certificate(s
, pkt
);
1195 case TLS_ST_SR_KEY_EXCH
:
1196 return tls_process_client_key_exchange(s
, pkt
);
1198 case TLS_ST_SR_CERT_VRFY
:
1199 return tls_process_cert_verify(s
, pkt
);
1201 #ifndef OPENSSL_NO_NEXTPROTONEG
1202 case TLS_ST_SR_NEXT_PROTO
:
1203 return tls_process_next_proto(s
, pkt
);
1206 case TLS_ST_SR_CHANGE
:
1207 return tls_process_change_cipher_spec(s
, pkt
);
1209 case TLS_ST_SR_FINISHED
:
1210 return tls_process_finished(s
, pkt
);
1212 case TLS_ST_SR_KEY_UPDATE
:
1213 return tls_process_key_update(s
, pkt
);
1219 * Perform any further processing required following the receipt of a message
1222 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1224 OSSL_STATEM
*st
= &s
->statem
;
1226 switch (st
->hand_state
) {
1228 /* Shouldn't happen */
1229 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1230 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE
,
1231 ERR_R_INTERNAL_ERROR
);
1234 case TLS_ST_SR_CLNT_HELLO
:
1235 return tls_post_process_client_hello(s
, wst
);
1237 case TLS_ST_SR_KEY_EXCH
:
1238 return tls_post_process_client_key_exchange(s
, wst
);
1242 #ifndef OPENSSL_NO_SRP
1243 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1244 static int ssl_check_srp_ext_ClientHello(SSL
*s
)
1247 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1249 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1250 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1251 if (s
->srp_ctx
.login
== NULL
) {
1253 * RFC 5054 says SHOULD reject, we do so if There is no srp
1256 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1257 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1258 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1261 ret
= SSL_srp_server_param_with_username(s
, &al
);
1264 if (ret
== SSL3_AL_FATAL
) {
1265 SSLfatal(s
, al
, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1266 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1267 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1268 : SSL_R_CLIENTHELLO_TLSEXT
);
1277 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1280 /* Always use DTLS 1.0 version: see RFC 6347 */
1281 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1282 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1288 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1290 unsigned int cookie_leni
;
1291 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1292 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1293 &cookie_leni
) == 0 ||
1294 cookie_leni
> 255) {
1295 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1296 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1299 s
->d1
->cookie_len
= cookie_leni
;
1301 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1302 s
->d1
->cookie_len
)) {
1303 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1304 ERR_R_INTERNAL_ERROR
);
1311 #ifndef OPENSSL_NO_EC
1313 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1314 * SecureTransport using the TLS extension block in |hello|.
1315 * Safari, since 10.6, sends exactly these extensions, in this order:
1319 * signature_algorithms (for TLSv1.2 only)
1321 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1322 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1323 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1324 * 10.8..10.8.3 (which don't work).
1326 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1328 static const unsigned char kSafariExtensionsBlock
[] = {
1329 0x00, 0x0a, /* elliptic_curves extension */
1330 0x00, 0x08, /* 8 bytes */
1331 0x00, 0x06, /* 6 bytes of curve ids */
1332 0x00, 0x17, /* P-256 */
1333 0x00, 0x18, /* P-384 */
1334 0x00, 0x19, /* P-521 */
1336 0x00, 0x0b, /* ec_point_formats */
1337 0x00, 0x02, /* 2 bytes */
1338 0x01, /* 1 point format */
1339 0x00, /* uncompressed */
1340 /* The following is only present in TLS 1.2 */
1341 0x00, 0x0d, /* signature_algorithms */
1342 0x00, 0x0c, /* 12 bytes */
1343 0x00, 0x0a, /* 10 bytes */
1344 0x05, 0x01, /* SHA-384/RSA */
1345 0x04, 0x01, /* SHA-256/RSA */
1346 0x02, 0x01, /* SHA-1/RSA */
1347 0x04, 0x03, /* SHA-256/ECDSA */
1348 0x02, 0x03, /* SHA-1/ECDSA */
1350 /* Length of the common prefix (first two extensions). */
1351 static const size_t kSafariCommonExtensionsLength
= 18;
1356 tmppkt
= hello
->extensions
;
1358 if (!PACKET_forward(&tmppkt
, 2)
1359 || !PACKET_get_net_2(&tmppkt
, &type
)
1360 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1364 if (type
!= TLSEXT_TYPE_server_name
)
1367 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1368 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1370 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1373 #endif /* !OPENSSL_NO_EC */
1375 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1377 /* |cookie| will only be initialized for DTLS. */
1378 PACKET session_id
, compression
, extensions
, cookie
;
1379 static const unsigned char null_compression
= 0;
1380 CLIENTHELLO_MSG
*clienthello
= NULL
;
1382 /* Check if this is actually an unexpected renegotiation ClientHello */
1383 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1384 if (!ossl_assert(!SSL_IS_TLS13(s
))) {
1385 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1386 ERR_R_INTERNAL_ERROR
);
1389 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
) != 0
1390 || (!s
->s3
.send_connection_binding
1392 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1393 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1394 return MSG_PROCESS_FINISHED_READING
;
1400 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1401 if (clienthello
== NULL
) {
1402 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1403 ERR_R_INTERNAL_ERROR
);
1408 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1410 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1411 PACKET_null_init(&cookie
);
1413 if (clienthello
->isv2
) {
1416 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1417 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1418 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1419 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1424 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1425 * header is sent directly on the wire, not wrapped as a TLS
1426 * record. Our record layer just processes the message length and passes
1427 * the rest right through. Its format is:
1429 * 0-1 msg_length - decoded by the record layer
1430 * 2 msg_type - s->init_msg points here
1432 * 5-6 cipher_spec_length
1433 * 7-8 session_id_length
1434 * 9-10 challenge_length
1438 if (!PACKET_get_1(pkt
, &mt
)
1439 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1441 * Should never happen. We should have tested this in the record
1442 * layer in order to have determined that this is a SSLv2 record
1443 * in the first place
1445 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1446 ERR_R_INTERNAL_ERROR
);
1451 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1452 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1453 SSL_R_LENGTH_TOO_SHORT
);
1457 /* Parse the message and load client random. */
1458 if (clienthello
->isv2
) {
1460 * Handle an SSLv2 backwards compatible ClientHello
1461 * Note, this is only for SSLv3+ using the backward compatible format.
1462 * Real SSLv2 is not supported, and is rejected below.
1464 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1467 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1468 || !PACKET_get_net_2(pkt
, &session_id_len
)
1469 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1470 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1471 SSL_R_RECORD_LENGTH_MISMATCH
);
1475 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1476 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1477 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1481 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1483 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1484 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1485 /* No extensions. */
1486 || PACKET_remaining(pkt
) != 0) {
1487 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1488 SSL_R_RECORD_LENGTH_MISMATCH
);
1491 clienthello
->session_id_len
= session_id_len
;
1493 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1494 * here rather than sizeof(clienthello->random) because that is the limit
1495 * for SSLv3 and it is fixed. It won't change even if
1496 * sizeof(clienthello->random) does.
1498 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1499 ? SSL3_RANDOM_SIZE
: challenge_len
;
1500 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1501 if (!PACKET_copy_bytes(&challenge
,
1502 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1503 challenge_len
, challenge_len
)
1504 /* Advertise only null compression. */
1505 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1506 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1507 ERR_R_INTERNAL_ERROR
);
1511 PACKET_null_init(&clienthello
->extensions
);
1513 /* Regular ClientHello. */
1514 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1515 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1516 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1517 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1518 &clienthello
->session_id_len
)) {
1519 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1520 SSL_R_LENGTH_MISMATCH
);
1524 if (SSL_IS_DTLS(s
)) {
1525 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1526 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1527 SSL_R_LENGTH_MISMATCH
);
1530 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1531 DTLS1_COOKIE_LENGTH
,
1532 &clienthello
->dtls_cookie_len
)) {
1533 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1534 SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1538 * If we require cookies and this ClientHello doesn't contain one,
1539 * just return since we do not want to allocate any memory yet.
1540 * So check cookie length...
1542 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1543 if (clienthello
->dtls_cookie_len
== 0) {
1544 OPENSSL_free(clienthello
);
1545 return MSG_PROCESS_FINISHED_READING
;
1550 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1551 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1552 SSL_R_LENGTH_MISMATCH
);
1556 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1557 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1558 SSL_R_LENGTH_MISMATCH
);
1562 /* Could be empty. */
1563 if (PACKET_remaining(pkt
) == 0) {
1564 PACKET_null_init(&clienthello
->extensions
);
1566 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1567 || PACKET_remaining(pkt
) != 0) {
1568 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1569 SSL_R_LENGTH_MISMATCH
);
1575 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1576 MAX_COMPRESSIONS_SIZE
,
1577 &clienthello
->compressions_len
)) {
1578 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1579 ERR_R_INTERNAL_ERROR
);
1583 /* Preserve the raw extensions PACKET for later use */
1584 extensions
= clienthello
->extensions
;
1585 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1586 &clienthello
->pre_proc_exts
,
1587 &clienthello
->pre_proc_exts_len
, 1)) {
1588 /* SSLfatal already been called */
1591 s
->clienthello
= clienthello
;
1593 return MSG_PROCESS_CONTINUE_PROCESSING
;
1596 if (clienthello
!= NULL
)
1597 OPENSSL_free(clienthello
->pre_proc_exts
);
1598 OPENSSL_free(clienthello
);
1600 return MSG_PROCESS_ERROR
;
1603 static int tls_early_post_process_client_hello(SSL
*s
)
1606 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1610 #ifndef OPENSSL_NO_COMP
1611 SSL_COMP
*comp
= NULL
;
1613 const SSL_CIPHER
*c
;
1614 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1615 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1616 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1617 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1619 /* Finished parsing the ClientHello, now we can start processing it */
1620 /* Give the ClientHello callback a crack at things */
1621 if (s
->ctx
->client_hello_cb
!= NULL
) {
1622 /* A failure in the ClientHello callback terminates the connection. */
1623 switch (s
->ctx
->client_hello_cb(s
, &al
, s
->ctx
->client_hello_cb_arg
)) {
1624 case SSL_CLIENT_HELLO_SUCCESS
:
1626 case SSL_CLIENT_HELLO_RETRY
:
1627 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1629 case SSL_CLIENT_HELLO_ERROR
:
1632 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1633 SSL_R_CALLBACK_FAILED
);
1638 /* Set up the client_random */
1639 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1641 /* Choose the version */
1643 if (clienthello
->isv2
) {
1644 if (clienthello
->legacy_version
== SSL2_VERSION
1645 || (clienthello
->legacy_version
& 0xff00)
1646 != (SSL3_VERSION_MAJOR
<< 8)) {
1648 * This is real SSLv2 or something completely unknown. We don't
1651 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1652 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1653 SSL_R_UNKNOWN_PROTOCOL
);
1657 s
->client_version
= clienthello
->legacy_version
;
1660 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1661 * versions are potentially compatible. Version negotiation comes later.
1663 if (!SSL_IS_DTLS(s
)) {
1664 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1665 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1666 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1667 protverr
= SSL_R_VERSION_TOO_LOW
;
1673 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1674 /* like ssl3_get_record, send alert using remote version number */
1675 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1677 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1678 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1682 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1683 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1684 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1685 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1686 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1690 if (SSL_IS_DTLS(s
)) {
1691 /* Empty cookie was already handled above by returning early. */
1692 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1693 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1694 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1695 clienthello
->dtls_cookie_len
) == 0) {
1696 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1697 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1698 SSL_R_COOKIE_MISMATCH
);
1700 /* else cookie verification succeeded */
1702 /* default verification */
1703 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1704 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1705 s
->d1
->cookie_len
) != 0) {
1706 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1707 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1708 SSL_R_COOKIE_MISMATCH
);
1711 s
->d1
->cookie_verified
= 1;
1713 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1714 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1715 if (protverr
!= 0) {
1716 s
->version
= s
->client_version
;
1717 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1718 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1726 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1727 clienthello
->isv2
) ||
1728 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1729 clienthello
->isv2
, 1)) {
1730 /* SSLfatal() already called */
1734 s
->s3
.send_connection_binding
= 0;
1735 /* Check what signalling cipher-suite values were received. */
1736 if (scsvs
!= NULL
) {
1737 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1738 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1739 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1740 if (s
->renegotiate
) {
1741 /* SCSV is fatal if renegotiating */
1742 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1743 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1744 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1747 s
->s3
.send_connection_binding
= 1;
1748 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1749 !ssl_check_version_downgrade(s
)) {
1751 * This SCSV indicates that the client previously tried
1752 * a higher version. We should fail if the current version
1753 * is an unexpected downgrade, as that indicates that the first
1754 * connection may have been tampered with in order to trigger
1755 * an insecure downgrade.
1757 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1758 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1759 SSL_R_INAPPROPRIATE_FALLBACK
);
1765 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1766 if (SSL_IS_TLS13(s
)) {
1767 const SSL_CIPHER
*cipher
=
1768 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(s
));
1770 if (cipher
== NULL
) {
1771 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1772 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1773 SSL_R_NO_SHARED_CIPHER
);
1776 if (s
->hello_retry_request
== SSL_HRR_PENDING
1777 && (s
->s3
.tmp
.new_cipher
== NULL
1778 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1780 * A previous HRR picked a different ciphersuite to the one we
1781 * just selected. Something must have changed.
1783 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1784 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1788 s
->s3
.tmp
.new_cipher
= cipher
;
1791 /* We need to do this before getting the session */
1792 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1793 SSL_EXT_CLIENT_HELLO
,
1794 clienthello
->pre_proc_exts
, NULL
, 0)) {
1795 /* SSLfatal() already called */
1800 * We don't allow resumption in a backwards compatible ClientHello.
1801 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1803 * Versions before 0.9.7 always allow clients to resume sessions in
1804 * renegotiation. 0.9.7 and later allow this by default, but optionally
1805 * ignore resumption requests with flag
1806 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1807 * than a change to default behavior so that applications relying on
1808 * this for security won't even compile against older library versions).
1809 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1810 * request renegotiation but not a new session (s->new_session remains
1811 * unset): for servers, this essentially just means that the
1812 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1815 if (clienthello
->isv2
||
1817 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1818 if (!ssl_get_new_session(s
, 1)) {
1819 /* SSLfatal() already called */
1823 i
= ssl_get_prev_session(s
, clienthello
);
1825 /* previous session */
1827 } else if (i
== -1) {
1828 /* SSLfatal() already called */
1832 if (!ssl_get_new_session(s
, 1)) {
1833 /* SSLfatal() already called */
1839 if (SSL_IS_TLS13(s
)) {
1840 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1841 s
->clienthello
->session_id_len
);
1842 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1846 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1847 * ciphersuite compatibility with the session as part of resumption.
1849 if (!SSL_IS_TLS13(s
) && s
->hit
) {
1851 id
= s
->session
->cipher
->id
;
1853 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1854 BIO_printf(trc_out
, "client sent %d ciphers\n",
1855 sk_SSL_CIPHER_num(ciphers
));
1857 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1858 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1859 if (trc_out
!= NULL
)
1860 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1861 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1869 * we need to have the cipher in the cipher list if we are asked
1872 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1873 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1874 SSL_R_REQUIRED_CIPHER_MISSING
);
1875 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1878 OSSL_TRACE_END(TLS_CIPHER
);
1881 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1882 if (clienthello
->compressions
[loop
] == 0)
1886 if (loop
>= clienthello
->compressions_len
) {
1888 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1889 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1890 SSL_R_NO_COMPRESSION_SPECIFIED
);
1894 #ifndef OPENSSL_NO_EC
1895 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1896 ssl_check_for_safari(s
, clienthello
);
1897 #endif /* !OPENSSL_NO_EC */
1899 /* TLS extensions */
1900 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1901 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1902 /* SSLfatal() already called */
1907 * Check if we want to use external pre-shared secret for this handshake
1908 * for not reused session only. We need to generate server_random before
1909 * calling tls_session_secret_cb in order to allow SessionTicket
1910 * processing to use it in key derivation.
1914 pos
= s
->s3
.server_random
;
1915 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1916 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1917 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1918 ERR_R_INTERNAL_ERROR
);
1924 && s
->version
>= TLS1_VERSION
1927 && s
->ext
.session_secret_cb
) {
1928 const SSL_CIPHER
*pref_cipher
= NULL
;
1930 * s->session->master_key_length is a size_t, but this is an int for
1931 * backwards compat reasons
1933 int master_key_length
;
1935 master_key_length
= sizeof(s
->session
->master_key
);
1936 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1937 &master_key_length
, ciphers
,
1939 s
->ext
.session_secret_cb_arg
)
1940 && master_key_length
> 0) {
1941 s
->session
->master_key_length
= master_key_length
;
1943 s
->peer_ciphers
= ciphers
;
1944 s
->session
->verify_result
= X509_V_OK
;
1948 /* check if some cipher was preferred by call back */
1949 if (pref_cipher
== NULL
)
1950 pref_cipher
= ssl3_choose_cipher(s
, s
->peer_ciphers
,
1951 SSL_get_ciphers(s
));
1952 if (pref_cipher
== NULL
) {
1953 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1954 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1955 SSL_R_NO_SHARED_CIPHER
);
1959 s
->session
->cipher
= pref_cipher
;
1960 sk_SSL_CIPHER_free(s
->cipher_list
);
1961 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1962 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1963 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1968 * Worst case, we will use the NULL compression, but if we have other
1969 * options, we will now look for them. We have complen-1 compression
1970 * algorithms from the client, starting at q.
1972 s
->s3
.tmp
.new_compression
= NULL
;
1973 if (SSL_IS_TLS13(s
)) {
1975 * We already checked above that the NULL compression method appears in
1976 * the list. Now we check there aren't any others (which is illegal in
1977 * a TLSv1.3 ClientHello.
1979 if (clienthello
->compressions_len
!= 1) {
1980 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1981 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1982 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1986 #ifndef OPENSSL_NO_COMP
1987 /* This only happens if we have a cache hit */
1988 else if (s
->session
->compress_meth
!= 0) {
1989 int m
, comp_id
= s
->session
->compress_meth
;
1991 /* Perform sanity checks on resumed compression algorithm */
1992 /* Can't disable compression */
1993 if (!ssl_allow_compression(s
)) {
1994 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1995 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1996 SSL_R_INCONSISTENT_COMPRESSION
);
1999 /* Look for resumed compression method */
2000 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
2001 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2002 if (comp_id
== comp
->id
) {
2003 s
->s3
.tmp
.new_compression
= comp
;
2007 if (s
->s3
.tmp
.new_compression
== NULL
) {
2008 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2009 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2010 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
2013 /* Look for resumed method in compression list */
2014 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
2015 if (clienthello
->compressions
[k
] == comp_id
)
2018 if (k
>= clienthello
->compressions_len
) {
2019 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2020 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2021 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
2024 } else if (s
->hit
) {
2026 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
2027 /* See if we have a match */
2028 int m
, nn
, v
, done
= 0;
2031 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
2032 for (m
= 0; m
< nn
; m
++) {
2033 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2035 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2036 if (v
== clienthello
->compressions
[o
]) {
2045 s
->s3
.tmp
.new_compression
= comp
;
2051 * If compression is disabled we'd better not try to resume a session
2052 * using compression.
2054 if (s
->session
->compress_meth
!= 0) {
2055 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2056 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2057 SSL_R_INCONSISTENT_COMPRESSION
);
2063 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2066 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2067 sk_SSL_CIPHER_free(s
->peer_ciphers
);
2068 s
->peer_ciphers
= ciphers
;
2069 if (ciphers
== NULL
) {
2070 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2071 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2072 ERR_R_INTERNAL_ERROR
);
2079 #ifdef OPENSSL_NO_COMP
2080 s
->session
->compress_meth
= 0;
2082 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2084 if (!tls1_set_server_sigalgs(s
)) {
2085 /* SSLfatal() already called */
2090 sk_SSL_CIPHER_free(ciphers
);
2091 sk_SSL_CIPHER_free(scsvs
);
2092 OPENSSL_free(clienthello
->pre_proc_exts
);
2093 OPENSSL_free(s
->clienthello
);
2094 s
->clienthello
= NULL
;
2097 sk_SSL_CIPHER_free(ciphers
);
2098 sk_SSL_CIPHER_free(scsvs
);
2099 OPENSSL_free(clienthello
->pre_proc_exts
);
2100 OPENSSL_free(s
->clienthello
);
2101 s
->clienthello
= NULL
;
2107 * Call the status request callback if needed. Upon success, returns 1.
2108 * Upon failure, returns 0.
2110 static int tls_handle_status_request(SSL
*s
)
2112 s
->ext
.status_expected
= 0;
2115 * If status request then ask callback what to do. Note: this must be
2116 * called after servername callbacks in case the certificate has changed,
2117 * and must be called after the cipher has been chosen because this may
2118 * influence which certificate is sent
2120 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
2121 && s
->ctx
->ext
.status_cb
!= NULL
) {
2124 /* If no certificate can't return certificate status */
2125 if (s
->s3
.tmp
.cert
!= NULL
) {
2127 * Set current certificate to one we will use so SSL_get_certificate
2128 * et al can pick it up.
2130 s
->cert
->key
= s
->s3
.tmp
.cert
;
2131 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
2133 /* We don't want to send a status request response */
2134 case SSL_TLSEXT_ERR_NOACK
:
2135 s
->ext
.status_expected
= 0;
2137 /* status request response should be sent */
2138 case SSL_TLSEXT_ERR_OK
:
2139 if (s
->ext
.ocsp
.resp
)
2140 s
->ext
.status_expected
= 1;
2142 /* something bad happened */
2143 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2145 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2146 SSL_F_TLS_HANDLE_STATUS_REQUEST
,
2147 SSL_R_CLIENTHELLO_TLSEXT
);
2157 * Call the alpn_select callback if needed. Upon success, returns 1.
2158 * Upon failure, returns 0.
2160 int tls_handle_alpn(SSL
*s
)
2162 const unsigned char *selected
= NULL
;
2163 unsigned char selected_len
= 0;
2165 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2166 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
2167 s
->s3
.alpn_proposed
,
2168 (unsigned int)s
->s3
.alpn_proposed_len
,
2169 s
->ctx
->ext
.alpn_select_cb_arg
);
2171 if (r
== SSL_TLSEXT_ERR_OK
) {
2172 OPENSSL_free(s
->s3
.alpn_selected
);
2173 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2174 if (s
->s3
.alpn_selected
== NULL
) {
2175 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_HANDLE_ALPN
,
2176 ERR_R_INTERNAL_ERROR
);
2179 s
->s3
.alpn_selected_len
= selected_len
;
2180 #ifndef OPENSSL_NO_NEXTPROTONEG
2181 /* ALPN takes precedence over NPN. */
2185 /* Check ALPN is consistent with session */
2186 if (s
->session
->ext
.alpn_selected
== NULL
2187 || selected_len
!= s
->session
->ext
.alpn_selected_len
2188 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2189 selected_len
) != 0) {
2190 /* Not consistent so can't be used for early_data */
2191 s
->ext
.early_data_ok
= 0;
2195 * This is a new session and so alpn_selected should have
2196 * been initialised to NULL. We should update it with the
2199 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2200 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2201 SSL_F_TLS_HANDLE_ALPN
,
2202 ERR_R_INTERNAL_ERROR
);
2205 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2207 if (s
->session
->ext
.alpn_selected
== NULL
) {
2208 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2209 SSL_F_TLS_HANDLE_ALPN
,
2210 ERR_R_INTERNAL_ERROR
);
2213 s
->session
->ext
.alpn_selected_len
= selected_len
;
2218 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2219 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
, SSL_F_TLS_HANDLE_ALPN
,
2220 SSL_R_NO_APPLICATION_PROTOCOL
);
2224 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2229 /* Check ALPN is consistent with session */
2230 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2231 /* Not consistent so can't be used for early_data */
2232 s
->ext
.early_data_ok
= 0;
2238 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
2240 const SSL_CIPHER
*cipher
;
2242 if (wst
== WORK_MORE_A
) {
2243 int rv
= tls_early_post_process_client_hello(s
);
2245 /* SSLfatal() was already called */
2252 if (wst
== WORK_MORE_B
) {
2253 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2254 /* Let cert callback update server certificates if required */
2255 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2256 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
2258 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2259 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2260 SSL_R_CERT_CB_ERROR
);
2264 s
->rwstate
= SSL_X509_LOOKUP
;
2267 s
->rwstate
= SSL_NOTHING
;
2270 /* In TLSv1.3 we selected the ciphersuite before resumption */
2271 if (!SSL_IS_TLS13(s
)) {
2273 ssl3_choose_cipher(s
, s
->peer_ciphers
, SSL_get_ciphers(s
));
2275 if (cipher
== NULL
) {
2276 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2277 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2278 SSL_R_NO_SHARED_CIPHER
);
2281 s
->s3
.tmp
.new_cipher
= cipher
;
2284 if (!tls_choose_sigalg(s
, 1)) {
2285 /* SSLfatal already called */
2288 /* check whether we should disable session resumption */
2289 if (s
->not_resumable_session_cb
!= NULL
)
2290 s
->session
->not_resumable
=
2291 s
->not_resumable_session_cb(s
,
2292 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2293 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2294 if (s
->session
->not_resumable
)
2295 /* do not send a session ticket */
2296 s
->ext
.ticket_expected
= 0;
2299 /* Session-id reuse */
2300 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2304 * we now have the following setup.
2306 * cipher_list - our preferred list of ciphers
2307 * ciphers - the clients preferred list of ciphers
2308 * compression - basically ignored right now
2309 * ssl version is set - sslv3
2310 * s->session - The ssl session has been setup.
2311 * s->hit - session reuse flag
2312 * s->s3.tmp.new_cipher - the new cipher to use.
2316 * Call status_request callback if needed. Has to be done after the
2317 * certificate callbacks etc above.
2319 if (!tls_handle_status_request(s
)) {
2320 /* SSLfatal() already called */
2324 * Call alpn_select callback if needed. Has to be done after SNI and
2325 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2326 * we already did this because cipher negotiation happens earlier, and
2327 * we must handle ALPN before we decide whether to accept early_data.
2329 if (!SSL_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2330 /* SSLfatal() already called */
2336 #ifndef OPENSSL_NO_SRP
2337 if (wst
== WORK_MORE_C
) {
2339 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2341 * callback indicates further work to be done
2343 s
->rwstate
= SSL_X509_LOOKUP
;
2347 /* SSLfatal() already called */
2353 return WORK_FINISHED_STOP
;
2358 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2363 unsigned char *session_id
;
2364 int usetls13
= SSL_IS_TLS13(s
) || s
->hello_retry_request
== SSL_HRR_PENDING
;
2366 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2367 if (!WPACKET_put_bytes_u16(pkt
, version
)
2369 * Random stuff. Filling of the server_random takes place in
2370 * tls_process_client_hello()
2372 || !WPACKET_memcpy(pkt
,
2373 s
->hello_retry_request
== SSL_HRR_PENDING
2374 ? hrrrandom
: s
->s3
.server_random
,
2375 SSL3_RANDOM_SIZE
)) {
2376 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2377 ERR_R_INTERNAL_ERROR
);
2382 * There are several cases for the session ID to send
2383 * back in the server hello:
2384 * - For session reuse from the session cache,
2385 * we send back the old session ID.
2386 * - If stateless session reuse (using a session ticket)
2387 * is successful, we send back the client's "session ID"
2388 * (which doesn't actually identify the session).
2389 * - If it is a new session, we send back the new
2391 * - However, if we want the new session to be single-use,
2392 * we send back a 0-length session ID.
2393 * - In TLSv1.3 we echo back the session id sent to us by the client
2395 * s->hit is non-zero in either case of session reuse,
2396 * so the following won't overwrite an ID that we're supposed
2399 if (s
->session
->not_resumable
||
2400 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2402 s
->session
->session_id_length
= 0;
2405 sl
= s
->tmp_session_id_len
;
2406 session_id
= s
->tmp_session_id
;
2408 sl
= s
->session
->session_id_length
;
2409 session_id
= s
->session
->session_id
;
2412 if (sl
> sizeof(s
->session
->session_id
)) {
2413 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2414 ERR_R_INTERNAL_ERROR
);
2418 /* set up the compression method */
2419 #ifdef OPENSSL_NO_COMP
2422 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2425 compm
= s
->s3
.tmp
.new_compression
->id
;
2428 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2429 || !s
->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
, pkt
, &len
)
2430 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2431 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2432 ERR_R_INTERNAL_ERROR
);
2436 if (!tls_construct_extensions(s
, pkt
,
2437 s
->hello_retry_request
== SSL_HRR_PENDING
2438 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2440 ? SSL_EXT_TLS1_3_SERVER_HELLO
2441 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2443 /* SSLfatal() already called */
2447 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2448 /* Ditch the session. We'll create a new one next time around */
2449 SSL_SESSION_free(s
->session
);
2454 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2455 * a synthetic message_hash in place of ClientHello1.
2457 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2458 /* SSLfatal() already called */
2461 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2462 && !ssl3_digest_cached_records(s
, 0)) {
2463 /* SSLfatal() already called */;
2470 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2472 if (!s
->s3
.tmp
.cert_request
) {
2473 if (!ssl3_digest_cached_records(s
, 0)) {
2474 /* SSLfatal() already called */
2481 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2483 #ifndef OPENSSL_NO_DH
2484 EVP_PKEY
*pkdh
= NULL
;
2486 #ifndef OPENSSL_NO_EC
2487 unsigned char *encodedPoint
= NULL
;
2488 size_t encodedlen
= 0;
2491 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2495 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2496 EVP_PKEY_CTX
*pctx
= NULL
;
2497 size_t paramlen
, paramoffset
;
2499 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2500 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2501 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2505 if (md_ctx
== NULL
) {
2506 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2507 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2511 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2513 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2514 #ifndef OPENSSL_NO_PSK
2515 /* Plain PSK or RSAPSK nothing to do */
2516 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2518 #endif /* !OPENSSL_NO_PSK */
2519 #ifndef OPENSSL_NO_DH
2520 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2521 CERT
*cert
= s
->cert
;
2523 EVP_PKEY
*pkdhp
= NULL
;
2526 if (s
->cert
->dh_tmp_auto
) {
2527 DH
*dhp
= ssl_get_auto_dh(s
);
2528 pkdh
= EVP_PKEY_new();
2529 if (pkdh
== NULL
|| dhp
== NULL
) {
2531 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2532 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2533 ERR_R_INTERNAL_ERROR
);
2536 EVP_PKEY_assign_DH(pkdh
, dhp
);
2539 pkdhp
= cert
->dh_tmp
;
2541 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2542 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2543 pkdh
= ssl_dh_to_pkey(dhp
);
2545 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2546 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2547 ERR_R_INTERNAL_ERROR
);
2552 if (pkdhp
== NULL
) {
2553 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2554 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2555 SSL_R_MISSING_TMP_DH_KEY
);
2558 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2559 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2560 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2561 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2562 SSL_R_DH_KEY_TOO_SMALL
);
2565 if (s
->s3
.tmp
.pkey
!= NULL
) {
2566 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2567 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2568 ERR_R_INTERNAL_ERROR
);
2572 s
->s3
.tmp
.pkey
= ssl_generate_pkey(pkdhp
);
2573 if (s
->s3
.tmp
.pkey
== NULL
) {
2574 /* SSLfatal() already called */
2578 dh
= EVP_PKEY_get0_DH(s
->s3
.tmp
.pkey
);
2580 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2581 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2582 ERR_R_INTERNAL_ERROR
);
2586 EVP_PKEY_free(pkdh
);
2589 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2590 DH_get0_key(dh
, &r
[2], NULL
);
2593 #ifndef OPENSSL_NO_EC
2594 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2596 if (s
->s3
.tmp
.pkey
!= NULL
) {
2597 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2598 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2599 ERR_R_INTERNAL_ERROR
);
2603 /* Get NID of appropriate shared curve */
2604 curve_id
= tls1_shared_group(s
, -2);
2605 if (curve_id
== 0) {
2606 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2607 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2608 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2611 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2612 /* Generate a new key for this curve */
2613 if (s
->s3
.tmp
.pkey
== NULL
) {
2614 /* SSLfatal() already called */
2618 /* Encode the public key. */
2619 encodedlen
= EVP_PKEY_get1_tls_encodedpoint(s
->s3
.tmp
.pkey
,
2621 if (encodedlen
== 0) {
2622 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2623 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2628 * We'll generate the serverKeyExchange message explicitly so we
2629 * can set these to NULLs
2636 #endif /* !OPENSSL_NO_EC */
2637 #ifndef OPENSSL_NO_SRP
2638 if (type
& SSL_kSRP
) {
2639 if ((s
->srp_ctx
.N
== NULL
) ||
2640 (s
->srp_ctx
.g
== NULL
) ||
2641 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2642 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2643 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2644 SSL_R_MISSING_SRP_PARAM
);
2647 r
[0] = s
->srp_ctx
.N
;
2648 r
[1] = s
->srp_ctx
.g
;
2649 r
[2] = s
->srp_ctx
.s
;
2650 r
[3] = s
->srp_ctx
.B
;
2654 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2655 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2656 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2660 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2661 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2663 } else if (lu
== NULL
) {
2664 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
2665 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2669 #ifndef OPENSSL_NO_PSK
2670 if (type
& SSL_PSK
) {
2671 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2672 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2675 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2676 * checked this when we set the identity hint - but just in case
2678 if (len
> PSK_MAX_IDENTITY_LEN
2679 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2681 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2682 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2683 ERR_R_INTERNAL_ERROR
);
2689 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2690 unsigned char *binval
;
2693 #ifndef OPENSSL_NO_SRP
2694 if ((i
== 2) && (type
& SSL_kSRP
)) {
2695 res
= WPACKET_start_sub_packet_u8(pkt
);
2698 res
= WPACKET_start_sub_packet_u16(pkt
);
2701 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2702 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2703 ERR_R_INTERNAL_ERROR
);
2707 #ifndef OPENSSL_NO_DH
2709 * for interoperability with some versions of the Microsoft TLS
2710 * stack, we need to zero pad the DHE pub key to the same length
2713 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2714 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2717 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2718 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2719 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2720 ERR_R_INTERNAL_ERROR
);
2723 memset(binval
, 0, len
);
2727 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2728 || !WPACKET_close(pkt
)) {
2729 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2730 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2731 ERR_R_INTERNAL_ERROR
);
2735 BN_bn2bin(r
[i
], binval
);
2738 #ifndef OPENSSL_NO_EC
2739 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2741 * We only support named (not generic) curves. In this situation, the
2742 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2743 * [1 byte length of encoded point], followed by the actual encoded
2746 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2747 || !WPACKET_put_bytes_u8(pkt
, 0)
2748 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2749 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2750 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2751 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2752 ERR_R_INTERNAL_ERROR
);
2755 OPENSSL_free(encodedPoint
);
2756 encodedPoint
= NULL
;
2762 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2764 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2765 size_t siglen
, tbslen
;
2768 if (pkey
== NULL
|| !tls1_lookup_md(lu
, &md
)) {
2769 /* Should never happen */
2770 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2771 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2772 ERR_R_INTERNAL_ERROR
);
2775 /* Get length of the parameters we have written above */
2776 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2777 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2778 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2779 ERR_R_INTERNAL_ERROR
);
2782 /* send signature algorithm */
2783 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2784 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2785 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2786 ERR_R_INTERNAL_ERROR
);
2790 * Create the signature. We don't know the actual length of the sig
2791 * until after we've created it, so we reserve enough bytes for it
2792 * up front, and then properly allocate them in the WPACKET
2795 siglen
= EVP_PKEY_size(pkey
);
2796 if (!WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2797 || EVP_DigestSignInit(md_ctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
2798 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2799 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2800 ERR_R_INTERNAL_ERROR
);
2803 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2804 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2805 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2806 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2807 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2812 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2813 s
->init_buf
->data
+ paramoffset
,
2816 /* SSLfatal() already called */
2819 rv
= EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
);
2821 if (rv
<= 0 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2822 || sigbytes1
!= sigbytes2
) {
2823 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2824 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2825 ERR_R_INTERNAL_ERROR
);
2830 EVP_MD_CTX_free(md_ctx
);
2833 #ifndef OPENSSL_NO_DH
2834 EVP_PKEY_free(pkdh
);
2836 #ifndef OPENSSL_NO_EC
2837 OPENSSL_free(encodedPoint
);
2839 EVP_MD_CTX_free(md_ctx
);
2843 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2845 if (SSL_IS_TLS13(s
)) {
2846 /* Send random context when doing post-handshake auth */
2847 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2848 OPENSSL_free(s
->pha_context
);
2849 s
->pha_context_len
= 32;
2850 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
2851 || RAND_bytes(s
->pha_context
, s
->pha_context_len
) <= 0
2852 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
, s
->pha_context_len
)) {
2853 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2854 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2855 ERR_R_INTERNAL_ERROR
);
2858 /* reset the handshake hash back to just after the ClientFinished */
2859 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2860 /* SSLfatal() already called */
2864 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2865 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2866 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2867 ERR_R_INTERNAL_ERROR
);
2872 if (!tls_construct_extensions(s
, pkt
,
2873 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2875 /* SSLfatal() already called */
2881 /* get the list of acceptable cert types */
2882 if (!WPACKET_start_sub_packet_u8(pkt
)
2883 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2884 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2885 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2889 if (SSL_USE_SIGALGS(s
)) {
2890 const uint16_t *psigs
;
2891 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2893 if (!WPACKET_start_sub_packet_u16(pkt
)
2894 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2895 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2896 || !WPACKET_close(pkt
)) {
2897 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2898 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2899 ERR_R_INTERNAL_ERROR
);
2904 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2905 /* SSLfatal() already called */
2911 s
->s3
.tmp
.cert_request
= 1;
2915 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
)
2917 #ifndef OPENSSL_NO_PSK
2918 unsigned char psk
[PSK_MAX_PSK_LEN
];
2920 PACKET psk_identity
;
2922 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2923 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2924 SSL_R_LENGTH_MISMATCH
);
2927 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2928 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2929 SSL_R_DATA_LENGTH_TOO_LONG
);
2932 if (s
->psk_server_callback
== NULL
) {
2933 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2934 SSL_R_PSK_NO_SERVER_CB
);
2938 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2939 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2940 ERR_R_INTERNAL_ERROR
);
2944 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2947 if (psklen
> PSK_MAX_PSK_LEN
) {
2948 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2949 ERR_R_INTERNAL_ERROR
);
2951 } else if (psklen
== 0) {
2953 * PSK related to the given identity not found
2955 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
2956 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2957 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2961 OPENSSL_free(s
->s3
.tmp
.psk
);
2962 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2963 OPENSSL_cleanse(psk
, psklen
);
2965 if (s
->s3
.tmp
.psk
== NULL
) {
2966 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2967 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2971 s
->s3
.tmp
.psklen
= psklen
;
2975 /* Should never happen */
2976 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2977 ERR_R_INTERNAL_ERROR
);
2982 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
)
2984 #ifndef OPENSSL_NO_RSA
2986 PACKET enc_premaster
;
2987 EVP_PKEY
*rsa
= NULL
;
2988 unsigned char *rsa_decrypt
= NULL
;
2990 EVP_PKEY_CTX
*ctx
= NULL
;
2991 OSSL_PARAM params
[3], *p
= params
;
2993 rsa
= s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
;
2995 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2996 SSL_R_MISSING_RSA_CERTIFICATE
);
3000 /* SSLv3 and pre-standard DTLS omit the length bytes. */
3001 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
3002 enc_premaster
= *pkt
;
3004 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
3005 || PACKET_remaining(pkt
) != 0) {
3006 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3007 SSL_R_LENGTH_MISMATCH
);
3013 * We want to be sure that the plaintext buffer size makes it safe to
3014 * iterate over the entire size of a premaster secret
3015 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
3016 * their ciphertext cannot accommodate a premaster secret anyway.
3018 if (EVP_PKEY_size(rsa
) < RSA_PKCS1_PADDING_SIZE
3019 + SSL_MAX_MASTER_KEY_LENGTH
) {
3020 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3021 RSA_R_KEY_SIZE_TOO_SMALL
);
3025 outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
3026 rsa_decrypt
= OPENSSL_malloc(outlen
);
3027 if (rsa_decrypt
== NULL
) {
3028 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3029 ERR_R_MALLOC_FAILURE
);
3033 ctx
= EVP_PKEY_CTX_new(rsa
, NULL
);
3035 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3036 ERR_R_MALLOC_FAILURE
);
3041 * We must not leak whether a decryption failure occurs because of
3042 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
3043 * section 7.4.7.1). We use the special padding type
3044 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automaticaly decrypt the
3045 * RSA, check the padding and check that the client version is as expected
3046 * in the premaster secret. If any of that fails then the function appears
3047 * to return successfully but with a random result. The call below could
3048 * still fail if the input is publicly invalid.
3049 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
3051 if (EVP_PKEY_decrypt_init(ctx
) <= 0
3052 || EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_WITH_TLS_PADDING
) <= 0) {
3053 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3054 SSL_R_DECRYPTION_FAILED
);
3058 *p
++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
,
3059 (unsigned int *)&s
->client_version
);
3060 if ((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) != 0)
3061 *p
++ = OSSL_PARAM_construct_uint(
3062 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
,
3063 (unsigned int *)&s
->version
);
3064 *p
++ = OSSL_PARAM_construct_end();
3066 if (!EVP_PKEY_CTX_set_params(ctx
, params
)
3067 || EVP_PKEY_decrypt(ctx
, rsa_decrypt
, &outlen
,
3068 PACKET_data(&enc_premaster
),
3069 PACKET_remaining(&enc_premaster
)) <= 0) {
3070 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3071 SSL_R_DECRYPTION_FAILED
);
3076 * This test should never fail (otherwise we should have failed above) but
3077 * we double check anyway.
3079 if (outlen
!= SSL_MAX_MASTER_KEY_LENGTH
) {
3080 OPENSSL_cleanse(rsa_decrypt
, SSL_MAX_MASTER_KEY_LENGTH
);
3081 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3082 SSL_R_DECRYPTION_FAILED
);
3086 /* Also cleanses rsa_decrypt (on success or failure) */
3087 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
3088 SSL_MAX_MASTER_KEY_LENGTH
, 0)) {
3089 /* SSLfatal() already called */
3095 OPENSSL_free(rsa_decrypt
);
3096 EVP_PKEY_CTX_free(ctx
);
3099 /* Should never happen */
3100 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3101 ERR_R_INTERNAL_ERROR
);
3106 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
)
3108 #ifndef OPENSSL_NO_DH
3109 EVP_PKEY
*skey
= NULL
;
3113 const unsigned char *data
;
3114 EVP_PKEY
*ckey
= NULL
;
3117 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
3118 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3119 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
3122 skey
= s
->s3
.tmp
.pkey
;
3124 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3125 SSL_R_MISSING_TMP_DH_KEY
);
3129 if (PACKET_remaining(pkt
) == 0L) {
3130 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3131 SSL_R_MISSING_TMP_DH_KEY
);
3134 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3135 /* We already checked we have enough data */
3136 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3137 ERR_R_INTERNAL_ERROR
);
3140 ckey
= EVP_PKEY_new();
3141 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3142 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3147 cdh
= EVP_PKEY_get0_DH(ckey
);
3148 pub_key
= BN_bin2bn(data
, i
, NULL
);
3149 if (pub_key
== NULL
|| cdh
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
3150 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3151 ERR_R_INTERNAL_ERROR
);
3156 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3157 /* SSLfatal() already called */
3162 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3163 s
->s3
.tmp
.pkey
= NULL
;
3165 EVP_PKEY_free(ckey
);
3168 /* Should never happen */
3169 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3170 ERR_R_INTERNAL_ERROR
);
3175 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
)
3177 #ifndef OPENSSL_NO_EC
3178 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3179 EVP_PKEY
*ckey
= NULL
;
3182 if (PACKET_remaining(pkt
) == 0L) {
3183 /* We don't support ECDH client auth */
3184 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3185 SSL_R_MISSING_TMP_ECDH_KEY
);
3189 const unsigned char *data
;
3192 * Get client's public key from encoded point in the
3193 * ClientKeyExchange message.
3196 /* Get encoded point length */
3197 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3198 || PACKET_remaining(pkt
) != 0) {
3199 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3200 SSL_R_LENGTH_MISMATCH
);
3204 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3205 SSL_R_MISSING_TMP_ECDH_KEY
);
3209 ckey
= EVP_PKEY_new();
3210 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3211 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3215 if (EVP_PKEY_set1_tls_encodedpoint(ckey
, data
, i
) == 0) {
3216 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3222 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3223 /* SSLfatal() already called */
3228 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3229 s
->s3
.tmp
.pkey
= NULL
;
3231 EVP_PKEY_free(ckey
);
3235 /* Should never happen */
3236 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3237 ERR_R_INTERNAL_ERROR
);
3242 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
)
3244 #ifndef OPENSSL_NO_SRP
3246 const unsigned char *data
;
3248 if (!PACKET_get_net_2(pkt
, &i
)
3249 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3250 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3251 SSL_R_BAD_SRP_A_LENGTH
);
3254 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3255 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3259 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3260 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS_PROCESS_CKE_SRP
,
3261 SSL_R_BAD_SRP_PARAMETERS
);
3264 OPENSSL_free(s
->session
->srp_username
);
3265 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3266 if (s
->session
->srp_username
== NULL
) {
3267 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3268 ERR_R_MALLOC_FAILURE
);
3272 if (!srp_generate_server_master_secret(s
)) {
3273 /* SSLfatal() already called */
3279 /* Should never happen */
3280 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3281 ERR_R_INTERNAL_ERROR
);
3286 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
)
3288 #ifndef OPENSSL_NO_GOST
3289 EVP_PKEY_CTX
*pkey_ctx
;
3290 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3291 unsigned char premaster_secret
[32];
3292 const unsigned char *start
;
3293 size_t outlen
= 32, inlen
;
3294 unsigned long alg_a
;
3295 GOST_KX_MESSAGE
*pKX
= NULL
;
3296 const unsigned char *ptr
;
3299 /* Get our certificate private key */
3300 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3301 if (alg_a
& SSL_aGOST12
) {
3303 * New GOST ciphersuites have SSL_aGOST01 bit too
3305 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3307 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3310 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3312 } else if (alg_a
& SSL_aGOST01
) {
3313 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3316 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
3317 if (pkey_ctx
== NULL
) {
3318 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3319 ERR_R_MALLOC_FAILURE
);
3322 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3323 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3324 ERR_R_INTERNAL_ERROR
);
3328 * If client certificate is present and is of the same type, maybe
3329 * use it for key exchange. Don't mind errors from
3330 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3331 * client certificate for authorization only.
3333 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3334 if (client_pub_pkey
) {
3335 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3339 ptr
= PACKET_data(pkt
);
3340 /* Some implementations provide extra data in the opaqueBlob
3341 * We have nothing to do with this blob so we just skip it */
3342 pKX
= d2i_GOST_KX_MESSAGE(NULL
, &ptr
, PACKET_remaining(pkt
));
3344 || pKX
->kxBlob
== NULL
3345 || ASN1_TYPE_get(pKX
->kxBlob
) != V_ASN1_SEQUENCE
) {
3346 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3347 SSL_R_DECRYPTION_FAILED
);
3351 if (!PACKET_forward(pkt
, ptr
- PACKET_data(pkt
))) {
3352 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3353 SSL_R_DECRYPTION_FAILED
);
3357 if (PACKET_remaining(pkt
) != 0) {
3358 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3359 SSL_R_DECRYPTION_FAILED
);
3363 inlen
= pKX
->kxBlob
->value
.sequence
->length
;
3364 start
= pKX
->kxBlob
->value
.sequence
->data
;
3366 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3368 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3369 SSL_R_DECRYPTION_FAILED
);
3372 /* Generate master secret */
3373 if (!ssl_generate_master_secret(s
, premaster_secret
,
3374 sizeof(premaster_secret
), 0)) {
3375 /* SSLfatal() already called */
3378 /* Check if pubkey from client certificate was used */
3379 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3381 s
->statem
.no_cert_verify
= 1;
3385 EVP_PKEY_CTX_free(pkey_ctx
);
3386 GOST_KX_MESSAGE_free(pKX
);
3389 /* Should never happen */
3390 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3391 ERR_R_INTERNAL_ERROR
);
3396 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3398 unsigned long alg_k
;
3400 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3402 /* For PSK parse and retrieve identity, obtain PSK key */
3403 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3404 /* SSLfatal() already called */
3408 if (alg_k
& SSL_kPSK
) {
3409 /* Identity extracted earlier: should be nothing left */
3410 if (PACKET_remaining(pkt
) != 0) {
3411 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3412 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3413 SSL_R_LENGTH_MISMATCH
);
3416 /* PSK handled by ssl_generate_master_secret */
3417 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3418 /* SSLfatal() already called */
3421 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3422 if (!tls_process_cke_rsa(s
, pkt
)) {
3423 /* SSLfatal() already called */
3426 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3427 if (!tls_process_cke_dhe(s
, pkt
)) {
3428 /* SSLfatal() already called */
3431 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3432 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3433 /* SSLfatal() already called */
3436 } else if (alg_k
& SSL_kSRP
) {
3437 if (!tls_process_cke_srp(s
, pkt
)) {
3438 /* SSLfatal() already called */
3441 } else if (alg_k
& SSL_kGOST
) {
3442 if (!tls_process_cke_gost(s
, pkt
)) {
3443 /* SSLfatal() already called */
3447 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3448 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3449 SSL_R_UNKNOWN_CIPHER_TYPE
);
3453 return MSG_PROCESS_CONTINUE_PROCESSING
;
3455 #ifndef OPENSSL_NO_PSK
3456 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3457 s
->s3
.tmp
.psk
= NULL
;
3459 return MSG_PROCESS_ERROR
;
3462 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3464 #ifndef OPENSSL_NO_SCTP
3465 if (wst
== WORK_MORE_A
) {
3466 if (SSL_IS_DTLS(s
)) {
3467 unsigned char sctpauthkey
[64];
3468 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3471 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3474 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3475 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3477 /* Don't include the terminating zero. */
3478 labellen
= sizeof(labelbuffer
) - 1;
3479 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3482 if (SSL_export_keying_material(s
, sctpauthkey
,
3483 sizeof(sctpauthkey
), labelbuffer
,
3486 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3487 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3488 ERR_R_INTERNAL_ERROR
);
3492 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3493 sizeof(sctpauthkey
), sctpauthkey
);
3498 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3500 * No certificate verify or no peer certificate so we no longer need
3501 * the handshake_buffer
3503 if (!ssl3_digest_cached_records(s
, 0)) {
3504 /* SSLfatal() already called */
3507 return WORK_FINISHED_CONTINUE
;
3509 if (!s
->s3
.handshake_buffer
) {
3510 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3511 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3512 ERR_R_INTERNAL_ERROR
);
3516 * For sigalgs freeze the handshake buffer. If we support
3517 * extms we've done this already so this is a no-op
3519 if (!ssl3_digest_cached_records(s
, 1)) {
3520 /* SSLfatal() already called */
3525 return WORK_FINISHED_CONTINUE
;
3528 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3531 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3534 const unsigned char *certstart
, *certbytes
;
3535 STACK_OF(X509
) *sk
= NULL
;
3536 PACKET spkt
, context
;
3538 SSL_SESSION
*new_sess
= NULL
;
3541 * To get this far we must have read encrypted data from the client. We no
3542 * longer tolerate unencrypted alerts. This value is ignored if less than
3545 s
->statem
.enc_read_state
= ENC_READ_STATE_VALID
;
3547 if ((sk
= sk_X509_new_null()) == NULL
) {
3548 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3549 ERR_R_MALLOC_FAILURE
);
3553 if (SSL_IS_TLS13(s
) && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3554 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3555 || (s
->pha_context
!= NULL
&&
3556 !PACKET_equal(&context
, s
->pha_context
, s
->pha_context_len
)))) {
3557 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3558 SSL_R_INVALID_CONTEXT
);
3562 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3563 || PACKET_remaining(pkt
) != 0) {
3564 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3565 SSL_R_LENGTH_MISMATCH
);
3569 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3570 if (!PACKET_get_net_3(&spkt
, &l
)
3571 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3572 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3573 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3574 SSL_R_CERT_LENGTH_MISMATCH
);
3578 certstart
= certbytes
;
3579 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, l
);
3581 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3582 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3585 if (certbytes
!= (certstart
+ l
)) {
3586 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3587 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3588 SSL_R_CERT_LENGTH_MISMATCH
);
3592 if (SSL_IS_TLS13(s
)) {
3593 RAW_EXTENSION
*rawexts
= NULL
;
3596 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3597 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3598 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3602 if (!tls_collect_extensions(s
, &extensions
,
3603 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3604 NULL
, chainidx
== 0)
3605 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3606 rawexts
, x
, chainidx
,
3607 PACKET_remaining(&spkt
) == 0)) {
3608 OPENSSL_free(rawexts
);
3611 OPENSSL_free(rawexts
);
3614 if (!sk_X509_push(sk
, x
)) {
3615 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3616 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3617 ERR_R_MALLOC_FAILURE
);
3623 if (sk_X509_num(sk
) <= 0) {
3624 /* TLS does not mind 0 certs returned */
3625 if (s
->version
== SSL3_VERSION
) {
3626 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3627 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3628 SSL_R_NO_CERTIFICATES_RETURNED
);
3631 /* Fail for TLS only if we required a certificate */
3632 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3633 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3634 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3635 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3636 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3639 /* No client certificate so digest cached records */
3640 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3641 /* SSLfatal() already called */
3646 i
= ssl_verify_cert_chain(s
, sk
);
3648 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3649 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3650 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3654 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3655 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3658 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3660 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3661 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3662 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3668 * Sessions must be immutable once they go into the session cache. Otherwise
3669 * we can get multi-thread problems. Therefore we don't "update" sessions,
3670 * we replace them with a duplicate. Here, we need to do this every time
3671 * a new certificate is received via post-handshake authentication, as the
3672 * session may have already gone into the session cache.
3675 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3676 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3677 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3678 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3679 ERR_R_MALLOC_FAILURE
);
3683 SSL_SESSION_free(s
->session
);
3684 s
->session
= new_sess
;
3687 X509_free(s
->session
->peer
);
3688 s
->session
->peer
= sk_X509_shift(sk
);
3689 s
->session
->verify_result
= s
->verify_result
;
3691 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3692 s
->session
->peer_chain
= sk
;
3695 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3698 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3699 /* SSLfatal() already called */
3704 * Inconsistency alert: cert_chain does *not* include the peer's own
3705 * certificate, while we do include it in statem_clnt.c
3709 /* Save the current hash state for when we receive the CertificateVerify */
3710 if (SSL_IS_TLS13(s
)) {
3711 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3712 sizeof(s
->cert_verify_hash
),
3713 &s
->cert_verify_hash_len
)) {
3714 /* SSLfatal() already called */
3718 /* Resend session tickets */
3719 s
->sent_tickets
= 0;
3722 ret
= MSG_PROCESS_CONTINUE_READING
;
3726 sk_X509_pop_free(sk
, X509_free
);
3730 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3732 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3735 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3736 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3741 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3742 * for the server Certificate message
3744 if (SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3745 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3746 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3749 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3750 /* SSLfatal() already called */
3757 static int create_ticket_prequel(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3758 unsigned char *tick_nonce
)
3761 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3762 * unspecified for resumed session (for simplicity).
3763 * In TLSv1.3 we reset the "time" field above, and always specify the
3766 if (!WPACKET_put_bytes_u32(pkt
,
3767 (s
->hit
&& !SSL_IS_TLS13(s
))
3768 ? 0 : s
->session
->timeout
)) {
3769 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3770 ERR_R_INTERNAL_ERROR
);
3774 if (SSL_IS_TLS13(s
)) {
3775 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3776 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3777 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3778 ERR_R_INTERNAL_ERROR
);
3783 /* Start the sub-packet for the actual ticket data */
3784 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3785 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3786 ERR_R_INTERNAL_ERROR
);
3793 static int construct_stateless_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3794 unsigned char *tick_nonce
)
3796 unsigned char *senc
= NULL
;
3797 EVP_CIPHER_CTX
*ctx
= NULL
;
3798 HMAC_CTX
*hctx
= NULL
;
3799 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3800 const unsigned char *const_p
;
3801 int len
, slen_full
, slen
, lenfinal
;
3804 SSL_CTX
*tctx
= s
->session_ctx
;
3805 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3806 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3808 size_t macoffset
, macendoffset
;
3810 /* get session encoding length */
3811 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3813 * Some length values are 16 bits, so forget it if session is too
3816 if (slen_full
== 0 || slen_full
> 0xFF00) {
3817 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3818 ERR_R_INTERNAL_ERROR
);
3821 senc
= OPENSSL_malloc(slen_full
);
3823 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3824 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_MALLOC_FAILURE
);
3828 ctx
= EVP_CIPHER_CTX_new();
3829 hctx
= HMAC_CTX_new();
3830 if (ctx
== NULL
|| hctx
== NULL
) {
3831 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3832 ERR_R_MALLOC_FAILURE
);
3837 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3838 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3839 ERR_R_INTERNAL_ERROR
);
3844 * create a fresh copy (not shared with other threads) to clean up
3847 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3849 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3850 ERR_R_INTERNAL_ERROR
);
3854 slen
= i2d_SSL_SESSION(sess
, NULL
);
3855 if (slen
== 0 || slen
> slen_full
) {
3856 /* shouldn't ever happen */
3857 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3858 ERR_R_INTERNAL_ERROR
);
3859 SSL_SESSION_free(sess
);
3863 if (!i2d_SSL_SESSION(sess
, &p
)) {
3864 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3865 ERR_R_INTERNAL_ERROR
);
3866 SSL_SESSION_free(sess
);
3869 SSL_SESSION_free(sess
);
3872 * Initialize HMAC and cipher contexts. If callback present it does
3873 * all the work otherwise use generated values from parent ctx.
3875 if (tctx
->ext
.ticket_key_cb
) {
3876 /* if 0 is returned, write an empty ticket */
3877 int ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3882 /* Put timeout and length */
3883 if (!WPACKET_put_bytes_u32(pkt
, 0)
3884 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3885 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3886 SSL_F_CONSTRUCT_STATELESS_TICKET
,
3887 ERR_R_INTERNAL_ERROR
);
3891 EVP_CIPHER_CTX_free(ctx
);
3892 HMAC_CTX_free(hctx
);
3896 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3897 SSL_R_CALLBACK_FAILED
);
3900 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
3902 const EVP_CIPHER
*cipher
= EVP_aes_256_cbc();
3904 iv_len
= EVP_CIPHER_iv_length(cipher
);
3905 if (RAND_bytes(iv
, iv_len
) <= 0
3906 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3907 tctx
->ext
.secure
->tick_aes_key
, iv
)
3908 || !HMAC_Init_ex(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
3909 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
3910 EVP_sha256(), NULL
)) {
3911 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3912 ERR_R_INTERNAL_ERROR
);
3915 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3916 sizeof(tctx
->ext
.tick_key_name
));
3919 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3920 /* SSLfatal() already called */
3924 if (!WPACKET_get_total_written(pkt
, &macoffset
)
3925 /* Output key name */
3926 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3928 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3929 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3931 /* Encrypt session data */
3932 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3933 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3934 || encdata1
!= encdata2
3935 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3936 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3937 || encdata1
+ len
!= encdata2
3938 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3939 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3940 || !HMAC_Update(hctx
,
3941 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3942 macendoffset
- macoffset
)
3943 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3944 || !HMAC_Final(hctx
, macdata1
, &hlen
)
3945 || hlen
> EVP_MAX_MD_SIZE
3946 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3947 || macdata1
!= macdata2
) {
3948 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3949 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_INTERNAL_ERROR
);
3953 /* Close the sub-packet created by create_ticket_prequel() */
3954 if (!WPACKET_close(pkt
)) {
3955 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3956 ERR_R_INTERNAL_ERROR
);
3963 EVP_CIPHER_CTX_free(ctx
);
3964 HMAC_CTX_free(hctx
);
3968 static int construct_stateful_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3969 unsigned char *tick_nonce
)
3971 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3972 /* SSLfatal() already called */
3976 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
3977 s
->session
->session_id_length
)
3978 || !WPACKET_close(pkt
)) {
3979 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATEFUL_TICKET
,
3980 ERR_R_INTERNAL_ERROR
);
3987 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
3989 SSL_CTX
*tctx
= s
->session_ctx
;
3990 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
3992 unsigned char age_add_c
[sizeof(uint32_t)];
3996 age_add_u
.age_add
= 0;
3998 if (SSL_IS_TLS13(s
)) {
4001 static const unsigned char nonce_label
[] = "resumption";
4002 const EVP_MD
*md
= ssl_handshake_md(s
);
4003 int hashleni
= EVP_MD_size(md
);
4005 /* Ensure cast to size_t is safe */
4006 if (!ossl_assert(hashleni
>= 0)) {
4007 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4008 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4009 ERR_R_INTERNAL_ERROR
);
4012 hashlen
= (size_t)hashleni
;
4015 * If we already sent one NewSessionTicket, or we resumed then
4016 * s->session may already be in a cache and so we must not modify it.
4017 * Instead we need to take a copy of it and modify that.
4019 if (s
->sent_tickets
!= 0 || s
->hit
) {
4020 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
4022 if (new_sess
== NULL
) {
4023 /* SSLfatal already called */
4027 SSL_SESSION_free(s
->session
);
4028 s
->session
= new_sess
;
4031 if (!ssl_generate_session_id(s
, s
->session
)) {
4032 /* SSLfatal() already called */
4035 if (RAND_bytes(age_add_u
.age_add_c
, sizeof(age_add_u
)) <= 0) {
4036 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4037 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4038 ERR_R_INTERNAL_ERROR
);
4041 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
4043 nonce
= s
->next_ticket_nonce
;
4044 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
4045 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
4049 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
4051 sizeof(nonce_label
) - 1,
4054 s
->session
->master_key
,
4056 /* SSLfatal() already called */
4059 s
->session
->master_key_length
= hashlen
;
4061 s
->session
->time
= (long)time(NULL
);
4062 if (s
->s3
.alpn_selected
!= NULL
) {
4063 OPENSSL_free(s
->session
->ext
.alpn_selected
);
4064 s
->session
->ext
.alpn_selected
=
4065 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
4066 if (s
->session
->ext
.alpn_selected
== NULL
) {
4067 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4068 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4069 ERR_R_MALLOC_FAILURE
);
4072 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
4074 s
->session
->ext
.max_early_data
= s
->max_early_data
;
4077 if (tctx
->generate_ticket_cb
!= NULL
&&
4078 tctx
->generate_ticket_cb(s
, tctx
->ticket_cb_data
) == 0)
4082 * If we are using anti-replay protection then we behave as if
4083 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4084 * is no point in using full stateless tickets.
4087 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4088 || (s
->max_early_data
> 0
4089 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4090 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4091 /* SSLfatal() already called */
4094 } else if (!construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4096 /* SSLfatal() already called */
4100 if (SSL_IS_TLS13(s
)) {
4101 if (!tls_construct_extensions(s
, pkt
,
4102 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4104 /* SSLfatal() already called */
4108 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4109 * gets reset to 0 if we send more tickets following a post-handshake
4110 * auth, but |next_ticket_nonce| does not.
4113 s
->next_ticket_nonce
++;
4114 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4123 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4124 * create a separate message. Returns 1 on success or 0 on failure.
4126 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
4128 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4129 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4130 s
->ext
.ocsp
.resp_len
)) {
4131 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
,
4132 ERR_R_INTERNAL_ERROR
);
4139 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
4141 if (!tls_construct_cert_status_body(s
, pkt
)) {
4142 /* SSLfatal() already called */
4149 #ifndef OPENSSL_NO_NEXTPROTONEG
4151 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4152 * It sets the next_proto member in s if found
4154 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
4156 PACKET next_proto
, padding
;
4157 size_t next_proto_len
;
4160 * The payload looks like:
4162 * uint8 proto[proto_len];
4163 * uint8 padding_len;
4164 * uint8 padding[padding_len];
4166 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4167 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4168 || PACKET_remaining(pkt
) > 0) {
4169 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4170 SSL_R_LENGTH_MISMATCH
);
4171 return MSG_PROCESS_ERROR
;
4174 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4176 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4177 ERR_R_INTERNAL_ERROR
);
4178 return MSG_PROCESS_ERROR
;
4181 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4183 return MSG_PROCESS_CONTINUE_READING
;
4187 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
4189 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4191 /* SSLfatal() already called */
4198 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL
*s
, PACKET
*pkt
)
4200 if (PACKET_remaining(pkt
) != 0) {
4201 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4202 SSL_R_LENGTH_MISMATCH
);
4203 return MSG_PROCESS_ERROR
;
4206 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4207 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4208 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4209 ERR_R_INTERNAL_ERROR
);
4210 return MSG_PROCESS_ERROR
;
4214 * EndOfEarlyData signals a key change so the end of the message must be on
4215 * a record boundary.
4217 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4218 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
4219 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4220 SSL_R_NOT_ON_RECORD_BOUNDARY
);
4221 return MSG_PROCESS_ERROR
;
4224 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4225 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
4226 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4227 /* SSLfatal() already called */
4228 return MSG_PROCESS_ERROR
;
4231 return MSG_PROCESS_CONTINUE_READING
;