2 * Copyright 1995-2022 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/x509.h>
22 #include <openssl/dh.h>
23 #include <openssl/rsa.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 CON_FUNC_RETURN
tls_construct_encrypted_extensions(SSL_CONNECTION
*s
,
50 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
51 * handshake state transitions when a TLSv1.3 server is reading messages from
52 * the client. The message type that the client has sent is provided in |mt|.
53 * The current state is in |s->statem.hand_state|.
55 * Return values are 1 for success (transition allowed) and 0 on error
56 * (transition not allowed)
58 static int ossl_statem_server13_read_transition(SSL_CONNECTION
*s
, int mt
)
60 OSSL_STATEM
*st
= &s
->statem
;
63 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
64 * not negotiated TLSv1.3 yet, so that case is handled by
65 * ossl_statem_server_read_transition()
67 switch (st
->hand_state
) {
71 case TLS_ST_EARLY_DATA
:
72 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
73 if (mt
== SSL3_MT_CLIENT_HELLO
) {
74 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
78 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
79 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
80 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
87 case TLS_ST_SR_END_OF_EARLY_DATA
:
88 case TLS_ST_SW_FINISHED
:
89 if (s
->s3
.tmp
.cert_request
) {
90 if (mt
== SSL3_MT_CERTIFICATE
) {
91 st
->hand_state
= TLS_ST_SR_CERT
;
95 if (mt
== SSL3_MT_FINISHED
) {
96 st
->hand_state
= TLS_ST_SR_FINISHED
;
103 if (s
->session
->peer
== NULL
) {
104 if (mt
== SSL3_MT_FINISHED
) {
105 st
->hand_state
= TLS_ST_SR_FINISHED
;
109 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
110 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
116 case TLS_ST_SR_CERT_VRFY
:
117 if (mt
== SSL3_MT_FINISHED
) {
118 st
->hand_state
= TLS_ST_SR_FINISHED
;
125 * Its never ok to start processing handshake messages in the middle of
126 * early data (i.e. before we've received the end of early data alert)
128 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
131 if (mt
== SSL3_MT_CERTIFICATE
132 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
133 st
->hand_state
= TLS_ST_SR_CERT
;
137 if (mt
== SSL3_MT_KEY_UPDATE
) {
138 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
144 /* No valid transition found */
149 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
150 * handshake state transitions when the server is reading messages from the
151 * client. The message type that the client has sent is provided in |mt|. The
152 * current state is in |s->statem.hand_state|.
154 * Return values are 1 for success (transition allowed) and 0 on error
155 * (transition not allowed)
157 int ossl_statem_server_read_transition(SSL_CONNECTION
*s
, int mt
)
159 OSSL_STATEM
*st
= &s
->statem
;
161 if (SSL_CONNECTION_IS_TLS13(s
)) {
162 if (!ossl_statem_server13_read_transition(s
, mt
))
167 switch (st
->hand_state
) {
173 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
174 if (mt
== SSL3_MT_CLIENT_HELLO
) {
175 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
180 case TLS_ST_SW_SRVR_DONE
:
182 * If we get a CKE message after a ServerDone then either
183 * 1) We didn't request a Certificate
185 * 2) If we did request one then
186 * a) We allow no Certificate to be returned
188 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
189 * list if we requested a certificate)
191 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
192 if (s
->s3
.tmp
.cert_request
) {
193 if (s
->version
== SSL3_VERSION
) {
194 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
195 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
197 * This isn't an unexpected message as such - we're just
198 * not going to accept it because we require a client
201 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
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_CONNECTION_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(SSL_CONNECTION_GET_SSL(s
));
309 BIO_clear_retry_flags(rbio
);
310 BIO_set_retry_read(rbio
);
313 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
318 * Should we send a ServerKeyExchange message?
320 * Valid return values are:
324 static int send_server_key_exchange(SSL_CONNECTION
*s
)
326 unsigned long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
329 * only send a ServerKeyExchange if DH or fortezza but we have a
330 * sign only certificate PSK: may send PSK identity hints For
331 * ECC ciphersuites, we send a serverKeyExchange message only if
332 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
333 * the server certificate contains the server's public key for
336 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
338 * PSK: send ServerKeyExchange if PSK identity hint if
341 #ifndef OPENSSL_NO_PSK
342 /* Only send SKE if we have identity hint for plain PSK */
343 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
344 && s
->cert
->psk_identity_hint
)
345 /* For other PSK always send SKE */
346 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
348 #ifndef OPENSSL_NO_SRP
349 /* SRP: send ServerKeyExchange */
350 || (alg_k
& SSL_kSRP
)
360 * Should we send a CertificateRequest message?
362 * Valid return values are:
366 int send_certificate_request(SSL_CONNECTION
*s
)
369 /* don't request cert unless asked for it: */
370 s
->verify_mode
& SSL_VERIFY_PEER
372 * don't request if post-handshake-only unless doing
373 * post-handshake in TLSv1.3:
375 && (!SSL_CONNECTION_IS_TLS13(s
)
376 || !(s
->verify_mode
& SSL_VERIFY_POST_HANDSHAKE
)
377 || s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
)
379 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
382 && (s
->certreqs_sent
< 1 ||
383 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
385 * never request cert in anonymous ciphersuites (see
386 * section "Certificate request" in SSL 3 drafts and in
389 && (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
391 * ... except when the application insists on
392 * verification (against the specs, but statem_clnt.c accepts
395 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
396 /* don't request certificate for SRP auth */
397 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
399 * With normal PSK Certificates and Certificate Requests
402 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
410 * ossl_statem_server13_write_transition() works out what handshake state to
411 * move to next when a TLSv1.3 server is writing messages to be sent to the
414 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL_CONNECTION
*s
)
416 OSSL_STATEM
*st
= &s
->statem
;
419 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
420 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
423 switch (st
->hand_state
) {
425 /* Shouldn't happen */
426 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
427 return WRITE_TRAN_ERROR
;
430 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
431 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
432 return WRITE_TRAN_CONTINUE
;
434 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
435 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
436 return WRITE_TRAN_CONTINUE
;
438 if (s
->ext
.extra_tickets_expected
> 0) {
439 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
440 return WRITE_TRAN_CONTINUE
;
442 /* Try to read from the client instead */
443 return WRITE_TRAN_FINISHED
;
445 case TLS_ST_SR_CLNT_HELLO
:
446 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
447 return WRITE_TRAN_CONTINUE
;
449 case TLS_ST_SW_SRVR_HELLO
:
450 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
451 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
452 st
->hand_state
= TLS_ST_SW_CHANGE
;
453 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
454 st
->hand_state
= TLS_ST_EARLY_DATA
;
456 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
457 return WRITE_TRAN_CONTINUE
;
459 case TLS_ST_SW_CHANGE
:
460 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
461 st
->hand_state
= TLS_ST_EARLY_DATA
;
463 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
464 return WRITE_TRAN_CONTINUE
;
466 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
468 st
->hand_state
= TLS_ST_SW_FINISHED
;
469 else if (send_certificate_request(s
))
470 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
472 st
->hand_state
= TLS_ST_SW_CERT
;
474 return WRITE_TRAN_CONTINUE
;
476 case TLS_ST_SW_CERT_REQ
:
477 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
478 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
479 st
->hand_state
= TLS_ST_OK
;
481 st
->hand_state
= TLS_ST_SW_CERT
;
483 return WRITE_TRAN_CONTINUE
;
486 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
487 return WRITE_TRAN_CONTINUE
;
489 case TLS_ST_SW_CERT_VRFY
:
490 st
->hand_state
= TLS_ST_SW_FINISHED
;
491 return WRITE_TRAN_CONTINUE
;
493 case TLS_ST_SW_FINISHED
:
494 st
->hand_state
= TLS_ST_EARLY_DATA
;
495 return WRITE_TRAN_CONTINUE
;
497 case TLS_ST_EARLY_DATA
:
498 return WRITE_TRAN_FINISHED
;
500 case TLS_ST_SR_FINISHED
:
502 * Technically we have finished the handshake at this point, but we're
503 * going to remain "in_init" for now and write out any session tickets
506 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
507 s
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
;
508 } else if (!s
->ext
.ticket_expected
) {
510 * If we're not going to renew the ticket then we just finish the
511 * handshake at this point.
513 st
->hand_state
= TLS_ST_OK
;
514 return WRITE_TRAN_CONTINUE
;
516 if (s
->num_tickets
> s
->sent_tickets
)
517 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
519 st
->hand_state
= TLS_ST_OK
;
520 return WRITE_TRAN_CONTINUE
;
522 case TLS_ST_SR_KEY_UPDATE
:
523 case TLS_ST_SW_KEY_UPDATE
:
524 st
->hand_state
= TLS_ST_OK
;
525 return WRITE_TRAN_CONTINUE
;
527 case TLS_ST_SW_SESSION_TICKET
:
528 /* In a resumption we only ever send a maximum of one new ticket.
529 * Following an initial handshake we send the number of tickets we have
530 * been configured for.
532 if (!SSL_IS_FIRST_HANDSHAKE(s
) && s
->ext
.extra_tickets_expected
> 0) {
533 return WRITE_TRAN_CONTINUE
;
534 } else if (s
->hit
|| s
->num_tickets
<= s
->sent_tickets
) {
535 /* We've written enough tickets out. */
536 st
->hand_state
= TLS_ST_OK
;
538 return WRITE_TRAN_CONTINUE
;
543 * ossl_statem_server_write_transition() works out what handshake state to move
544 * to next when the server is writing messages to be sent to the client.
546 WRITE_TRAN
ossl_statem_server_write_transition(SSL_CONNECTION
*s
)
548 OSSL_STATEM
*st
= &s
->statem
;
551 * Note that before the ClientHello we don't know what version we are going
552 * to negotiate yet, so we don't take this branch until later
555 if (SSL_CONNECTION_IS_TLS13(s
))
556 return ossl_statem_server13_write_transition(s
);
558 switch (st
->hand_state
) {
560 /* Shouldn't happen */
561 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
562 return WRITE_TRAN_ERROR
;
565 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
566 /* We must be trying to renegotiate */
567 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
568 st
->request_state
= TLS_ST_BEFORE
;
569 return WRITE_TRAN_CONTINUE
;
571 /* Must be an incoming ClientHello */
572 if (!tls_setup_handshake(s
)) {
573 /* SSLfatal() already called */
574 return WRITE_TRAN_ERROR
;
579 /* Just go straight to trying to read from the client */
580 return WRITE_TRAN_FINISHED
;
582 case TLS_ST_SW_HELLO_REQ
:
583 st
->hand_state
= TLS_ST_OK
;
584 return WRITE_TRAN_CONTINUE
;
586 case TLS_ST_SR_CLNT_HELLO
:
587 if (SSL_CONNECTION_IS_DTLS(s
) && !s
->d1
->cookie_verified
588 && (SSL_get_options(SSL_CONNECTION_GET_SSL(s
)) & SSL_OP_COOKIE_EXCHANGE
)) {
589 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
590 } else if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
591 /* We must have rejected the renegotiation */
592 st
->hand_state
= TLS_ST_OK
;
593 return WRITE_TRAN_CONTINUE
;
595 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
597 return WRITE_TRAN_CONTINUE
;
599 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
600 return WRITE_TRAN_FINISHED
;
602 case TLS_ST_SW_SRVR_HELLO
:
604 if (s
->ext
.ticket_expected
)
605 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
607 st
->hand_state
= TLS_ST_SW_CHANGE
;
609 /* Check if it is anon DH or anon ECDH, */
610 /* normal PSK or SRP */
611 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
&
612 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
613 st
->hand_state
= TLS_ST_SW_CERT
;
614 } else if (send_server_key_exchange(s
)) {
615 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
616 } else if (send_certificate_request(s
)) {
617 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
619 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
622 return WRITE_TRAN_CONTINUE
;
625 if (s
->ext
.status_expected
) {
626 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
627 return WRITE_TRAN_CONTINUE
;
631 case TLS_ST_SW_CERT_STATUS
:
632 if (send_server_key_exchange(s
)) {
633 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
634 return WRITE_TRAN_CONTINUE
;
638 case TLS_ST_SW_KEY_EXCH
:
639 if (send_certificate_request(s
)) {
640 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
641 return WRITE_TRAN_CONTINUE
;
645 case TLS_ST_SW_CERT_REQ
:
646 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
647 return WRITE_TRAN_CONTINUE
;
649 case TLS_ST_SW_SRVR_DONE
:
650 return WRITE_TRAN_FINISHED
;
652 case TLS_ST_SR_FINISHED
:
654 st
->hand_state
= TLS_ST_OK
;
655 return WRITE_TRAN_CONTINUE
;
656 } else if (s
->ext
.ticket_expected
) {
657 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
659 st
->hand_state
= TLS_ST_SW_CHANGE
;
661 return WRITE_TRAN_CONTINUE
;
663 case TLS_ST_SW_SESSION_TICKET
:
664 st
->hand_state
= TLS_ST_SW_CHANGE
;
665 return WRITE_TRAN_CONTINUE
;
667 case TLS_ST_SW_CHANGE
:
668 st
->hand_state
= TLS_ST_SW_FINISHED
;
669 return WRITE_TRAN_CONTINUE
;
671 case TLS_ST_SW_FINISHED
:
673 return WRITE_TRAN_FINISHED
;
675 st
->hand_state
= TLS_ST_OK
;
676 return WRITE_TRAN_CONTINUE
;
681 * Perform any pre work that needs to be done prior to sending a message from
682 * the server to the client.
684 WORK_STATE
ossl_statem_server_pre_work(SSL_CONNECTION
*s
, WORK_STATE wst
)
686 OSSL_STATEM
*st
= &s
->statem
;
687 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
689 switch (st
->hand_state
) {
691 /* No pre work to be done */
694 case TLS_ST_SW_HELLO_REQ
:
696 if (SSL_CONNECTION_IS_DTLS(s
))
697 dtls1_clear_sent_buffer(s
);
700 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
702 if (SSL_CONNECTION_IS_DTLS(s
)) {
703 dtls1_clear_sent_buffer(s
);
704 /* We don't buffer this message so don't use the timer */
709 case TLS_ST_SW_SRVR_HELLO
:
710 if (SSL_CONNECTION_IS_DTLS(s
)) {
712 * Messages we write from now on should be buffered and
713 * retransmitted if necessary, so we need to use the timer now
719 case TLS_ST_SW_SRVR_DONE
:
720 #ifndef OPENSSL_NO_SCTP
721 if (SSL_CONNECTION_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(ssl
))) {
722 /* Calls SSLfatal() as required */
723 return dtls_wait_for_dry(s
);
726 return WORK_FINISHED_CONTINUE
;
728 case TLS_ST_SW_SESSION_TICKET
:
729 if (SSL_CONNECTION_IS_TLS13(s
) && s
->sent_tickets
== 0
730 && s
->ext
.extra_tickets_expected
== 0) {
732 * Actually this is the end of the handshake, but we're going
733 * straight into writing the session ticket out. So we finish off
734 * the handshake, but keep the various buffers active.
736 * Calls SSLfatal as required.
738 return tls_finish_handshake(s
, wst
, 0, 0);
740 if (SSL_CONNECTION_IS_DTLS(s
)) {
742 * We're into the last flight. We don't retransmit the last flight
743 * unless we need to, so we don't use the timer
749 case TLS_ST_SW_CHANGE
:
750 if (SSL_CONNECTION_IS_TLS13(s
))
752 /* Writes to s->session are only safe for initial handshakes */
753 if (s
->session
->cipher
== NULL
) {
754 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
755 } else if (s
->session
->cipher
!= s
->s3
.tmp
.new_cipher
) {
756 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
759 if (!ssl
->method
->ssl3_enc
->setup_key_block(s
)) {
760 /* SSLfatal() already called */
763 if (SSL_CONNECTION_IS_DTLS(s
)) {
765 * We're into the last flight. We don't retransmit the last flight
766 * unless we need to, so we don't use the timer. This might have
767 * already been set to 0 if we sent a NewSessionTicket message,
768 * but we'll set it again here in case we didn't.
772 return WORK_FINISHED_CONTINUE
;
774 case TLS_ST_EARLY_DATA
:
775 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
776 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0)
777 return WORK_FINISHED_CONTINUE
;
781 /* Calls SSLfatal() as required */
782 return tls_finish_handshake(s
, wst
, 1, 1);
785 return WORK_FINISHED_CONTINUE
;
788 static ossl_inline
int conn_is_closed(void)
790 switch (get_last_sys_error()) {
795 #if defined(ECONNRESET)
799 #if defined(WSAECONNRESET)
809 * Perform any work that needs to be done after sending a message from the
810 * server to the client.
812 WORK_STATE
ossl_statem_server_post_work(SSL_CONNECTION
*s
, WORK_STATE wst
)
814 OSSL_STATEM
*st
= &s
->statem
;
815 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
819 switch (st
->hand_state
) {
821 /* No post work to be done */
824 case TLS_ST_SW_HELLO_REQ
:
825 if (statem_flush(s
) != 1)
827 if (!ssl3_init_finished_mac(s
)) {
828 /* SSLfatal() already called */
833 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
834 if (statem_flush(s
) != 1)
836 /* HelloVerifyRequest resets Finished MAC */
837 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
838 /* SSLfatal() already called */
842 * The next message should be another ClientHello which we need to
843 * treat like it was the first packet
848 case TLS_ST_SW_SRVR_HELLO
:
849 if (SSL_CONNECTION_IS_TLS13(s
)
850 && s
->hello_retry_request
== SSL_HRR_PENDING
) {
851 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
852 && statem_flush(s
) != 1)
856 #ifndef OPENSSL_NO_SCTP
857 if (SSL_CONNECTION_IS_DTLS(s
) && s
->hit
) {
858 unsigned char sctpauthkey
[64];
859 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
863 * Add new shared key for SCTP-Auth, will be ignored if no
866 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
867 sizeof(DTLS1_SCTP_AUTH_LABEL
));
869 /* Don't include the terminating zero. */
870 labellen
= sizeof(labelbuffer
) - 1;
871 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
874 if (SSL_export_keying_material(ssl
, sctpauthkey
,
875 sizeof(sctpauthkey
), labelbuffer
,
878 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
882 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
883 sizeof(sctpauthkey
), sctpauthkey
);
886 if (!SSL_CONNECTION_IS_TLS13(s
)
887 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
888 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
892 case TLS_ST_SW_CHANGE
:
893 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
894 if (!statem_flush(s
))
899 if (SSL_CONNECTION_IS_TLS13(s
)) {
900 if (!ssl
->method
->ssl3_enc
->setup_key_block(s
)
901 || !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
902 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
903 /* SSLfatal() already called */
907 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
908 && !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
909 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
910 /* SSLfatal() already called */
914 * We don't yet know whether the next record we are going to receive
915 * is an unencrypted alert, an encrypted alert, or an encrypted
916 * handshake message. We temporarily tolerate unencrypted alerts.
918 if (s
->rlayer
.rrlmethod
->set_plain_alerts
!= NULL
)
919 s
->rlayer
.rrlmethod
->set_plain_alerts(s
->rlayer
.rrl
, 1);
923 #ifndef OPENSSL_NO_SCTP
924 if (SSL_CONNECTION_IS_DTLS(s
) && !s
->hit
) {
926 * Change to new shared key of SCTP-Auth, will be ignored if
929 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
933 if (!ssl
->method
->ssl3_enc
->change_cipher_state(s
,
934 SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
935 /* SSLfatal() already called */
939 if (SSL_CONNECTION_IS_DTLS(s
))
940 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
943 case TLS_ST_SW_SRVR_DONE
:
944 if (statem_flush(s
) != 1)
948 case TLS_ST_SW_FINISHED
:
949 if (statem_flush(s
) != 1)
951 #ifndef OPENSSL_NO_SCTP
952 if (SSL_CONNECTION_IS_DTLS(s
) && s
->hit
) {
954 * Change to new shared key of SCTP-Auth, will be ignored if
957 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
961 if (SSL_CONNECTION_IS_TLS13(s
)) {
962 /* TLS 1.3 gets the secret size from the handshake md */
964 if (!ssl
->method
->ssl3_enc
->generate_master_secret(s
,
965 s
->master_secret
, s
->handshake_secret
, 0,
967 || !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
968 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
969 /* SSLfatal() already called */
974 case TLS_ST_SW_CERT_REQ
:
975 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
976 if (statem_flush(s
) != 1)
981 case TLS_ST_SW_KEY_UPDATE
:
982 if (statem_flush(s
) != 1)
984 if (!tls13_update_key(s
, 1)) {
985 /* SSLfatal() already called */
990 case TLS_ST_SW_SESSION_TICKET
:
992 if (SSL_CONNECTION_IS_TLS13(s
) && statem_flush(s
) != 1) {
993 if (SSL_get_error(ssl
, 0) == SSL_ERROR_SYSCALL
994 && conn_is_closed()) {
996 * We ignore connection closed errors in TLSv1.3 when sending a
997 * NewSessionTicket and behave as if we were successful. This is
998 * so that we are still able to read data sent to us by a client
999 * that closes soon after the end of the handshake without
1000 * waiting to read our post-handshake NewSessionTickets.
1002 s
->rwstate
= SSL_NOTHING
;
1011 return WORK_FINISHED_CONTINUE
;
1015 * Get the message construction function and message type for sending from the
1018 * Valid return values are:
1022 int ossl_statem_server_construct_message(SSL_CONNECTION
*s
,
1023 confunc_f
*confunc
, int *mt
)
1025 OSSL_STATEM
*st
= &s
->statem
;
1027 switch (st
->hand_state
) {
1029 /* Shouldn't happen */
1030 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
1033 case TLS_ST_SW_CHANGE
:
1034 if (SSL_CONNECTION_IS_DTLS(s
))
1035 *confunc
= dtls_construct_change_cipher_spec
;
1037 *confunc
= tls_construct_change_cipher_spec
;
1038 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1041 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1042 *confunc
= dtls_construct_hello_verify_request
;
1043 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1046 case TLS_ST_SW_HELLO_REQ
:
1047 /* No construction function needed */
1049 *mt
= SSL3_MT_HELLO_REQUEST
;
1052 case TLS_ST_SW_SRVR_HELLO
:
1053 *confunc
= tls_construct_server_hello
;
1054 *mt
= SSL3_MT_SERVER_HELLO
;
1057 case TLS_ST_SW_CERT
:
1058 *confunc
= tls_construct_server_certificate
;
1059 *mt
= SSL3_MT_CERTIFICATE
;
1062 case TLS_ST_SW_CERT_VRFY
:
1063 *confunc
= tls_construct_cert_verify
;
1064 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1068 case TLS_ST_SW_KEY_EXCH
:
1069 *confunc
= tls_construct_server_key_exchange
;
1070 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1073 case TLS_ST_SW_CERT_REQ
:
1074 *confunc
= tls_construct_certificate_request
;
1075 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1078 case TLS_ST_SW_SRVR_DONE
:
1079 *confunc
= tls_construct_server_done
;
1080 *mt
= SSL3_MT_SERVER_DONE
;
1083 case TLS_ST_SW_SESSION_TICKET
:
1084 *confunc
= tls_construct_new_session_ticket
;
1085 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1088 case TLS_ST_SW_CERT_STATUS
:
1089 *confunc
= tls_construct_cert_status
;
1090 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1093 case TLS_ST_SW_FINISHED
:
1094 *confunc
= tls_construct_finished
;
1095 *mt
= SSL3_MT_FINISHED
;
1098 case TLS_ST_EARLY_DATA
:
1100 *mt
= SSL3_MT_DUMMY
;
1103 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1104 *confunc
= tls_construct_encrypted_extensions
;
1105 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1108 case TLS_ST_SW_KEY_UPDATE
:
1109 *confunc
= tls_construct_key_update
;
1110 *mt
= SSL3_MT_KEY_UPDATE
;
1118 * Maximum size (excluding the Handshake header) of a ClientHello message,
1119 * calculated as follows:
1121 * 2 + # client_version
1122 * 32 + # only valid length for random
1123 * 1 + # length of session_id
1124 * 32 + # maximum size for session_id
1125 * 2 + # length of cipher suites
1126 * 2^16-2 + # maximum length of cipher suites array
1127 * 1 + # length of compression_methods
1128 * 2^8-1 + # maximum length of compression methods
1129 * 2 + # length of extensions
1130 * 2^16-1 # maximum length of extensions
1132 #define CLIENT_HELLO_MAX_LENGTH 131396
1134 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1135 #define NEXT_PROTO_MAX_LENGTH 514
1138 * Returns the maximum allowed length for the current message that we are
1139 * reading. Excludes the message header.
1141 size_t ossl_statem_server_max_message_size(SSL_CONNECTION
*s
)
1143 OSSL_STATEM
*st
= &s
->statem
;
1145 switch (st
->hand_state
) {
1147 /* Shouldn't happen */
1150 case TLS_ST_SR_CLNT_HELLO
:
1151 return CLIENT_HELLO_MAX_LENGTH
;
1153 case TLS_ST_SR_END_OF_EARLY_DATA
:
1154 return END_OF_EARLY_DATA_MAX_LENGTH
;
1156 case TLS_ST_SR_CERT
:
1157 return s
->max_cert_list
;
1159 case TLS_ST_SR_KEY_EXCH
:
1160 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1162 case TLS_ST_SR_CERT_VRFY
:
1163 return SSL3_RT_MAX_PLAIN_LENGTH
;
1165 #ifndef OPENSSL_NO_NEXTPROTONEG
1166 case TLS_ST_SR_NEXT_PROTO
:
1167 return NEXT_PROTO_MAX_LENGTH
;
1170 case TLS_ST_SR_CHANGE
:
1171 return CCS_MAX_LENGTH
;
1173 case TLS_ST_SR_FINISHED
:
1174 return FINISHED_MAX_LENGTH
;
1176 case TLS_ST_SR_KEY_UPDATE
:
1177 return KEY_UPDATE_MAX_LENGTH
;
1182 * Process a message that the server has received from the client.
1184 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL_CONNECTION
*s
,
1187 OSSL_STATEM
*st
= &s
->statem
;
1189 switch (st
->hand_state
) {
1191 /* Shouldn't happen */
1192 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1193 return MSG_PROCESS_ERROR
;
1195 case TLS_ST_SR_CLNT_HELLO
:
1196 return tls_process_client_hello(s
, pkt
);
1198 case TLS_ST_SR_END_OF_EARLY_DATA
:
1199 return tls_process_end_of_early_data(s
, pkt
);
1201 case TLS_ST_SR_CERT
:
1202 return tls_process_client_certificate(s
, pkt
);
1204 case TLS_ST_SR_KEY_EXCH
:
1205 return tls_process_client_key_exchange(s
, pkt
);
1207 case TLS_ST_SR_CERT_VRFY
:
1208 return tls_process_cert_verify(s
, pkt
);
1210 #ifndef OPENSSL_NO_NEXTPROTONEG
1211 case TLS_ST_SR_NEXT_PROTO
:
1212 return tls_process_next_proto(s
, pkt
);
1215 case TLS_ST_SR_CHANGE
:
1216 return tls_process_change_cipher_spec(s
, pkt
);
1218 case TLS_ST_SR_FINISHED
:
1219 return tls_process_finished(s
, pkt
);
1221 case TLS_ST_SR_KEY_UPDATE
:
1222 return tls_process_key_update(s
, pkt
);
1228 * Perform any further processing required following the receipt of a message
1231 WORK_STATE
ossl_statem_server_post_process_message(SSL_CONNECTION
*s
,
1234 OSSL_STATEM
*st
= &s
->statem
;
1236 switch (st
->hand_state
) {
1238 /* Shouldn't happen */
1239 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1242 case TLS_ST_SR_CLNT_HELLO
:
1243 return tls_post_process_client_hello(s
, wst
);
1245 case TLS_ST_SR_KEY_EXCH
:
1246 return tls_post_process_client_key_exchange(s
, wst
);
1250 #ifndef OPENSSL_NO_SRP
1251 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1252 static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION
*s
)
1255 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1257 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1258 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1259 if (s
->srp_ctx
.login
== NULL
) {
1261 * RFC 5054 says SHOULD reject, we do so if There is no srp
1264 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1265 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1268 ret
= ssl_srp_server_param_with_username_intern(s
, &al
);
1271 if (ret
== SSL3_AL_FATAL
) {
1273 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1274 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1275 : SSL_R_CLIENTHELLO_TLSEXT
);
1284 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1287 /* Always use DTLS 1.0 version: see RFC 6347 */
1288 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1289 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1295 CON_FUNC_RETURN
dtls_construct_hello_verify_request(SSL_CONNECTION
*s
,
1298 unsigned int cookie_leni
;
1299 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1301 if (sctx
->app_gen_cookie_cb
== NULL
1302 || sctx
->app_gen_cookie_cb(SSL_CONNECTION_GET_SSL(s
), s
->d1
->cookie
,
1304 || cookie_leni
> DTLS1_COOKIE_LENGTH
) {
1305 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1306 return CON_FUNC_ERROR
;
1308 s
->d1
->cookie_len
= cookie_leni
;
1310 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1311 s
->d1
->cookie_len
)) {
1312 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
1313 return CON_FUNC_ERROR
;
1316 return CON_FUNC_SUCCESS
;
1320 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1321 * SecureTransport using the TLS extension block in |hello|.
1322 * Safari, since 10.6, sends exactly these extensions, in this order:
1326 * signature_algorithms (for TLSv1.2 only)
1328 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1329 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1330 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1331 * 10.8..10.8.3 (which don't work).
1333 static void ssl_check_for_safari(SSL_CONNECTION
*s
,
1334 const CLIENTHELLO_MSG
*hello
)
1336 static const unsigned char kSafariExtensionsBlock
[] = {
1337 0x00, 0x0a, /* elliptic_curves extension */
1338 0x00, 0x08, /* 8 bytes */
1339 0x00, 0x06, /* 6 bytes of curve ids */
1340 0x00, 0x17, /* P-256 */
1341 0x00, 0x18, /* P-384 */
1342 0x00, 0x19, /* P-521 */
1344 0x00, 0x0b, /* ec_point_formats */
1345 0x00, 0x02, /* 2 bytes */
1346 0x01, /* 1 point format */
1347 0x00, /* uncompressed */
1348 /* The following is only present in TLS 1.2 */
1349 0x00, 0x0d, /* signature_algorithms */
1350 0x00, 0x0c, /* 12 bytes */
1351 0x00, 0x0a, /* 10 bytes */
1352 0x05, 0x01, /* SHA-384/RSA */
1353 0x04, 0x01, /* SHA-256/RSA */
1354 0x02, 0x01, /* SHA-1/RSA */
1355 0x04, 0x03, /* SHA-256/ECDSA */
1356 0x02, 0x03, /* SHA-1/ECDSA */
1358 /* Length of the common prefix (first two extensions). */
1359 static const size_t kSafariCommonExtensionsLength
= 18;
1364 tmppkt
= hello
->extensions
;
1366 if (!PACKET_forward(&tmppkt
, 2)
1367 || !PACKET_get_net_2(&tmppkt
, &type
)
1368 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1372 if (type
!= TLSEXT_TYPE_server_name
)
1375 ext_len
= TLS1_get_client_version(
1376 SSL_CONNECTION_GET_SSL(s
)) >= TLS1_2_VERSION
?
1377 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1379 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1383 #define RENEG_OPTIONS_OK(options) \
1384 ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
1385 && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
1387 MSG_PROCESS_RETURN
tls_process_client_hello(SSL_CONNECTION
*s
, PACKET
*pkt
)
1389 /* |cookie| will only be initialized for DTLS. */
1390 PACKET session_id
, compression
, extensions
, cookie
;
1391 static const unsigned char null_compression
= 0;
1392 CLIENTHELLO_MSG
*clienthello
= NULL
;
1394 /* Check if this is actually an unexpected renegotiation ClientHello */
1395 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1396 if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s
))) {
1397 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1400 if (!RENEG_OPTIONS_OK(s
->options
)
1401 || (!s
->s3
.send_connection_binding
1403 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1404 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1405 return MSG_PROCESS_FINISHED_READING
;
1411 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1412 if (clienthello
== NULL
) {
1413 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1418 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1420 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1421 PACKET_null_init(&cookie
);
1423 if (clienthello
->isv2
) {
1426 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1427 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1428 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
1433 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1434 * header is sent directly on the wire, not wrapped as a TLS
1435 * record. Our record layer just processes the message length and passes
1436 * the rest right through. Its format is:
1438 * 0-1 msg_length - decoded by the record layer
1439 * 2 msg_type - s->init_msg points here
1441 * 5-6 cipher_spec_length
1442 * 7-8 session_id_length
1443 * 9-10 challenge_length
1447 if (!PACKET_get_1(pkt
, &mt
)
1448 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1450 * Should never happen. We should have tested this in the record
1451 * layer in order to have determined that this is a SSLv2 record
1452 * in the first place
1454 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1459 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1460 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_TOO_SHORT
);
1464 /* Parse the message and load client random. */
1465 if (clienthello
->isv2
) {
1467 * Handle an SSLv2 backwards compatible ClientHello
1468 * Note, this is only for SSLv3+ using the backward compatible format.
1469 * Real SSLv2 is not supported, and is rejected below.
1471 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1474 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1475 || !PACKET_get_net_2(pkt
, &session_id_len
)
1476 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1477 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RECORD_LENGTH_MISMATCH
);
1481 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1482 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_LENGTH_MISMATCH
);
1486 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1488 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1489 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1490 /* No extensions. */
1491 || PACKET_remaining(pkt
) != 0) {
1492 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RECORD_LENGTH_MISMATCH
);
1495 clienthello
->session_id_len
= session_id_len
;
1497 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1498 * here rather than sizeof(clienthello->random) because that is the limit
1499 * for SSLv3 and it is fixed. It won't change even if
1500 * sizeof(clienthello->random) does.
1502 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1503 ? SSL3_RANDOM_SIZE
: challenge_len
;
1504 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1505 if (!PACKET_copy_bytes(&challenge
,
1506 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1507 challenge_len
, challenge_len
)
1508 /* Advertise only null compression. */
1509 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1510 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1514 PACKET_null_init(&clienthello
->extensions
);
1516 /* Regular ClientHello. */
1517 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1518 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1519 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1520 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1521 &clienthello
->session_id_len
)) {
1522 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1526 if (SSL_CONNECTION_IS_DTLS(s
)) {
1527 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1528 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1531 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1532 DTLS1_COOKIE_LENGTH
,
1533 &clienthello
->dtls_cookie_len
)) {
1534 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, 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(SSL_CONNECTION_GET_SSL(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_R_LENGTH_MISMATCH
);
1555 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1556 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1560 /* Could be empty. */
1561 if (PACKET_remaining(pkt
) == 0) {
1562 PACKET_null_init(&clienthello
->extensions
);
1564 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1565 || PACKET_remaining(pkt
) != 0) {
1566 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1572 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1573 MAX_COMPRESSIONS_SIZE
,
1574 &clienthello
->compressions_len
)) {
1575 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1579 /* Preserve the raw extensions PACKET for later use */
1580 extensions
= clienthello
->extensions
;
1581 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1582 &clienthello
->pre_proc_exts
,
1583 &clienthello
->pre_proc_exts_len
, 1)) {
1584 /* SSLfatal already been called */
1587 s
->clienthello
= clienthello
;
1589 return MSG_PROCESS_CONTINUE_PROCESSING
;
1592 if (clienthello
!= NULL
)
1593 OPENSSL_free(clienthello
->pre_proc_exts
);
1594 OPENSSL_free(clienthello
);
1596 return MSG_PROCESS_ERROR
;
1599 static int tls_early_post_process_client_hello(SSL_CONNECTION
*s
)
1602 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1606 #ifndef OPENSSL_NO_COMP
1607 SSL_COMP
*comp
= NULL
;
1609 const SSL_CIPHER
*c
;
1610 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1611 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1612 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1613 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1614 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1615 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1617 /* Finished parsing the ClientHello, now we can start processing it */
1618 /* Give the ClientHello callback a crack at things */
1619 if (sctx
->client_hello_cb
!= NULL
) {
1620 /* A failure in the ClientHello callback terminates the connection. */
1621 switch (sctx
->client_hello_cb(ssl
, &al
, sctx
->client_hello_cb_arg
)) {
1622 case SSL_CLIENT_HELLO_SUCCESS
:
1624 case SSL_CLIENT_HELLO_RETRY
:
1625 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1627 case SSL_CLIENT_HELLO_ERROR
:
1629 SSLfatal(s
, al
, SSL_R_CALLBACK_FAILED
);
1634 /* Set up the client_random */
1635 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1637 /* Choose the version */
1639 if (clienthello
->isv2
) {
1640 if (clienthello
->legacy_version
== SSL2_VERSION
1641 || (clienthello
->legacy_version
& 0xff00)
1642 != (SSL3_VERSION_MAJOR
<< 8)) {
1644 * This is real SSLv2 or something completely unknown. We don't
1647 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_UNKNOWN_PROTOCOL
);
1651 s
->client_version
= clienthello
->legacy_version
;
1654 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1655 * versions are potentially compatible. Version negotiation comes later.
1657 if (!SSL_CONNECTION_IS_DTLS(s
)) {
1658 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1659 } else if (ssl
->method
->version
!= DTLS_ANY_VERSION
&&
1660 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1661 protverr
= SSL_R_VERSION_TOO_LOW
;
1667 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1668 /* like ssl3_get_record, send alert using remote version number */
1669 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1671 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, protverr
);
1675 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1676 if (SSL_CONNECTION_IS_TLS13(s
)
1677 && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1678 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
1682 if (SSL_CONNECTION_IS_DTLS(s
)) {
1683 /* Empty cookie was already handled above by returning early. */
1684 if (SSL_get_options(ssl
) & SSL_OP_COOKIE_EXCHANGE
) {
1685 if (sctx
->app_verify_cookie_cb
!= NULL
) {
1686 if (sctx
->app_verify_cookie_cb(ssl
, clienthello
->dtls_cookie
,
1687 clienthello
->dtls_cookie_len
) == 0) {
1688 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1689 SSL_R_COOKIE_MISMATCH
);
1691 /* else cookie verification succeeded */
1693 /* default verification */
1694 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1695 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1696 s
->d1
->cookie_len
) != 0) {
1697 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_COOKIE_MISMATCH
);
1700 s
->d1
->cookie_verified
= 1;
1702 if (ssl
->method
->version
== DTLS_ANY_VERSION
) {
1703 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1704 if (protverr
!= 0) {
1705 s
->version
= s
->client_version
;
1706 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, protverr
);
1714 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1715 clienthello
->isv2
) ||
1716 !ossl_bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
,
1717 &scsvs
, clienthello
->isv2
, 1)) {
1718 /* SSLfatal() already called */
1722 s
->s3
.send_connection_binding
= 0;
1723 /* Check what signalling cipher-suite values were received. */
1724 if (scsvs
!= NULL
) {
1725 for (i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1726 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1727 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1728 if (s
->renegotiate
) {
1729 /* SCSV is fatal if renegotiating */
1730 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1731 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1734 s
->s3
.send_connection_binding
= 1;
1735 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1736 !ssl_check_version_downgrade(s
)) {
1738 * This SCSV indicates that the client previously tried
1739 * a higher version. We should fail if the current version
1740 * is an unexpected downgrade, as that indicates that the first
1741 * connection may have been tampered with in order to trigger
1742 * an insecure downgrade.
1744 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1745 SSL_R_INAPPROPRIATE_FALLBACK
);
1751 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1752 if (SSL_CONNECTION_IS_TLS13(s
)) {
1753 const SSL_CIPHER
*cipher
=
1754 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(ssl
));
1756 if (cipher
== NULL
) {
1757 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_SHARED_CIPHER
);
1760 if (s
->hello_retry_request
== SSL_HRR_PENDING
1761 && (s
->s3
.tmp
.new_cipher
== NULL
1762 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1764 * A previous HRR picked a different ciphersuite to the one we
1765 * just selected. Something must have changed.
1767 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_CIPHER
);
1770 s
->s3
.tmp
.new_cipher
= cipher
;
1773 /* We need to do this before getting the session */
1774 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1775 SSL_EXT_CLIENT_HELLO
,
1776 clienthello
->pre_proc_exts
, NULL
, 0)) {
1777 /* SSLfatal() already called */
1782 * We don't allow resumption in a backwards compatible ClientHello.
1783 * In TLS1.1+, session_id MUST be empty.
1785 * Versions before 0.9.7 always allow clients to resume sessions in
1786 * renegotiation. 0.9.7 and later allow this by default, but optionally
1787 * ignore resumption requests with flag
1788 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1789 * than a change to default behavior so that applications relying on
1790 * this for security won't even compile against older library versions).
1791 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1792 * request renegotiation but not a new session (s->new_session remains
1793 * unset): for servers, this essentially just means that the
1794 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1797 if (clienthello
->isv2
||
1799 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1800 if (!ssl_get_new_session(s
, 1)) {
1801 /* SSLfatal() already called */
1805 i
= ssl_get_prev_session(s
, clienthello
);
1807 /* previous session */
1809 } else if (i
== -1) {
1810 /* SSLfatal() already called */
1814 if (!ssl_get_new_session(s
, 1)) {
1815 /* SSLfatal() already called */
1821 if (SSL_CONNECTION_IS_TLS13(s
)) {
1822 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1823 s
->clienthello
->session_id_len
);
1824 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1828 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1829 * ciphersuite compatibility with the session as part of resumption.
1831 if (!SSL_CONNECTION_IS_TLS13(s
) && s
->hit
) {
1833 id
= s
->session
->cipher
->id
;
1835 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1836 BIO_printf(trc_out
, "client sent %d ciphers\n",
1837 sk_SSL_CIPHER_num(ciphers
));
1839 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1840 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1841 if (trc_out
!= NULL
)
1842 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1843 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1851 * we need to have the cipher in the cipher list if we are asked
1854 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1855 SSL_R_REQUIRED_CIPHER_MISSING
);
1856 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1859 OSSL_TRACE_END(TLS_CIPHER
);
1862 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1863 if (clienthello
->compressions
[loop
] == 0)
1867 if (loop
>= clienthello
->compressions_len
) {
1869 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1873 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1874 ssl_check_for_safari(s
, clienthello
);
1876 /* TLS extensions */
1877 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1878 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1879 /* SSLfatal() already called */
1884 * Check if we want to use external pre-shared secret for this handshake
1885 * for not reused session only. We need to generate server_random before
1886 * calling tls_session_secret_cb in order to allow SessionTicket
1887 * processing to use it in key derivation.
1891 pos
= s
->s3
.server_random
;
1892 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1893 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1899 && s
->version
>= TLS1_VERSION
1900 && !SSL_CONNECTION_IS_TLS13(s
)
1901 && !SSL_CONNECTION_IS_DTLS(s
)
1902 && s
->ext
.session_secret_cb
!= NULL
) {
1903 const SSL_CIPHER
*pref_cipher
= NULL
;
1905 * s->session->master_key_length is a size_t, but this is an int for
1906 * backwards compat reasons
1908 int master_key_length
;
1910 master_key_length
= sizeof(s
->session
->master_key
);
1911 if (s
->ext
.session_secret_cb(ssl
, s
->session
->master_key
,
1912 &master_key_length
, ciphers
,
1914 s
->ext
.session_secret_cb_arg
)
1915 && master_key_length
> 0) {
1916 s
->session
->master_key_length
= master_key_length
;
1918 s
->peer_ciphers
= ciphers
;
1919 s
->session
->verify_result
= X509_V_OK
;
1923 /* check if some cipher was preferred by call back */
1924 if (pref_cipher
== NULL
)
1925 pref_cipher
= ssl3_choose_cipher(s
, s
->peer_ciphers
,
1926 SSL_get_ciphers(ssl
));
1927 if (pref_cipher
== NULL
) {
1928 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_SHARED_CIPHER
);
1932 s
->session
->cipher
= pref_cipher
;
1933 sk_SSL_CIPHER_free(s
->cipher_list
);
1934 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1935 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1936 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1941 * Worst case, we will use the NULL compression, but if we have other
1942 * options, we will now look for them. We have complen-1 compression
1943 * algorithms from the client, starting at q.
1945 s
->s3
.tmp
.new_compression
= NULL
;
1946 if (SSL_CONNECTION_IS_TLS13(s
)) {
1948 * We already checked above that the NULL compression method appears in
1949 * the list. Now we check there aren't any others (which is illegal in
1950 * a TLSv1.3 ClientHello.
1952 if (clienthello
->compressions_len
!= 1) {
1953 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1954 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1958 #ifndef OPENSSL_NO_COMP
1959 /* This only happens if we have a cache hit */
1960 else if (s
->session
->compress_meth
!= 0) {
1961 int m
, comp_id
= s
->session
->compress_meth
;
1963 /* Perform sanity checks on resumed compression algorithm */
1964 /* Can't disable compression */
1965 if (!ssl_allow_compression(s
)) {
1966 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1967 SSL_R_INCONSISTENT_COMPRESSION
);
1970 /* Look for resumed compression method */
1971 for (m
= 0; m
< sk_SSL_COMP_num(sctx
->comp_methods
); m
++) {
1972 comp
= sk_SSL_COMP_value(sctx
->comp_methods
, m
);
1973 if (comp_id
== comp
->id
) {
1974 s
->s3
.tmp
.new_compression
= comp
;
1978 if (s
->s3
.tmp
.new_compression
== NULL
) {
1979 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1980 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1983 /* Look for resumed method in compression list */
1984 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
1985 if (clienthello
->compressions
[k
] == comp_id
)
1988 if (k
>= clienthello
->compressions_len
) {
1989 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1990 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
1993 } else if (s
->hit
) {
1995 } else if (ssl_allow_compression(s
) && sctx
->comp_methods
) {
1996 /* See if we have a match */
1997 int m
, nn
, v
, done
= 0;
2000 nn
= sk_SSL_COMP_num(sctx
->comp_methods
);
2001 for (m
= 0; m
< nn
; m
++) {
2002 comp
= sk_SSL_COMP_value(sctx
->comp_methods
, m
);
2004 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2005 if (v
== clienthello
->compressions
[o
]) {
2014 s
->s3
.tmp
.new_compression
= comp
;
2020 * If compression is disabled we'd better not try to resume a session
2021 * using compression.
2023 if (s
->session
->compress_meth
!= 0) {
2024 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_INCONSISTENT_COMPRESSION
);
2030 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2033 if (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
)) {
2034 sk_SSL_CIPHER_free(s
->peer_ciphers
);
2035 s
->peer_ciphers
= ciphers
;
2036 if (ciphers
== NULL
) {
2037 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2044 #ifdef OPENSSL_NO_COMP
2045 s
->session
->compress_meth
= 0;
2047 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2049 if (!tls1_set_server_sigalgs(s
)) {
2050 /* SSLfatal() already called */
2055 sk_SSL_CIPHER_free(ciphers
);
2056 sk_SSL_CIPHER_free(scsvs
);
2057 OPENSSL_free(clienthello
->pre_proc_exts
);
2058 OPENSSL_free(s
->clienthello
);
2059 s
->clienthello
= NULL
;
2062 sk_SSL_CIPHER_free(ciphers
);
2063 sk_SSL_CIPHER_free(scsvs
);
2064 OPENSSL_free(clienthello
->pre_proc_exts
);
2065 OPENSSL_free(s
->clienthello
);
2066 s
->clienthello
= NULL
;
2072 * Call the status request callback if needed. Upon success, returns 1.
2073 * Upon failure, returns 0.
2075 static int tls_handle_status_request(SSL_CONNECTION
*s
)
2077 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2079 s
->ext
.status_expected
= 0;
2082 * If status request then ask callback what to do. Note: this must be
2083 * called after servername callbacks in case the certificate has changed,
2084 * and must be called after the cipher has been chosen because this may
2085 * influence which certificate is sent
2087 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& sctx
!= NULL
2088 && sctx
->ext
.status_cb
!= NULL
) {
2091 /* If no certificate can't return certificate status */
2092 if (s
->s3
.tmp
.cert
!= NULL
) {
2094 * Set current certificate to one we will use so SSL_get_certificate
2095 * et al can pick it up.
2097 s
->cert
->key
= s
->s3
.tmp
.cert
;
2098 ret
= sctx
->ext
.status_cb(SSL_CONNECTION_GET_SSL(s
),
2099 sctx
->ext
.status_arg
);
2101 /* We don't want to send a status request response */
2102 case SSL_TLSEXT_ERR_NOACK
:
2103 s
->ext
.status_expected
= 0;
2105 /* status request response should be sent */
2106 case SSL_TLSEXT_ERR_OK
:
2107 if (s
->ext
.ocsp
.resp
)
2108 s
->ext
.status_expected
= 1;
2110 /* something bad happened */
2111 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2113 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CLIENTHELLO_TLSEXT
);
2123 * Call the alpn_select callback if needed. Upon success, returns 1.
2124 * Upon failure, returns 0.
2126 int tls_handle_alpn(SSL_CONNECTION
*s
)
2128 const unsigned char *selected
= NULL
;
2129 unsigned char selected_len
= 0;
2130 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2132 if (sctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2133 int r
= sctx
->ext
.alpn_select_cb(SSL_CONNECTION_GET_SSL(s
),
2134 &selected
, &selected_len
,
2135 s
->s3
.alpn_proposed
,
2136 (unsigned int)s
->s3
.alpn_proposed_len
,
2137 sctx
->ext
.alpn_select_cb_arg
);
2139 if (r
== SSL_TLSEXT_ERR_OK
) {
2140 OPENSSL_free(s
->s3
.alpn_selected
);
2141 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2142 if (s
->s3
.alpn_selected
== NULL
) {
2143 s
->s3
.alpn_selected_len
= 0;
2144 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2147 s
->s3
.alpn_selected_len
= selected_len
;
2148 #ifndef OPENSSL_NO_NEXTPROTONEG
2149 /* ALPN takes precedence over NPN. */
2153 /* Check ALPN is consistent with session */
2154 if (s
->session
->ext
.alpn_selected
== NULL
2155 || selected_len
!= s
->session
->ext
.alpn_selected_len
2156 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2157 selected_len
) != 0) {
2158 /* Not consistent so can't be used for early_data */
2159 s
->ext
.early_data_ok
= 0;
2163 * This is a new session and so alpn_selected should have
2164 * been initialised to NULL. We should update it with the
2167 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2168 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2169 ERR_R_INTERNAL_ERROR
);
2172 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2174 if (s
->session
->ext
.alpn_selected
== NULL
) {
2175 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2176 ERR_R_INTERNAL_ERROR
);
2179 s
->session
->ext
.alpn_selected_len
= selected_len
;
2184 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2185 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
,
2186 SSL_R_NO_APPLICATION_PROTOCOL
);
2190 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2195 /* Check ALPN is consistent with session */
2196 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2197 /* Not consistent so can't be used for early_data */
2198 s
->ext
.early_data_ok
= 0;
2204 WORK_STATE
tls_post_process_client_hello(SSL_CONNECTION
*s
, WORK_STATE wst
)
2206 const SSL_CIPHER
*cipher
;
2207 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2209 if (wst
== WORK_MORE_A
) {
2210 int rv
= tls_early_post_process_client_hello(s
);
2212 /* SSLfatal() was already called */
2219 if (wst
== WORK_MORE_B
) {
2220 if (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
)) {
2221 /* Let cert callback update server certificates if required */
2222 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2223 int rv
= s
->cert
->cert_cb(ssl
, s
->cert
->cert_cb_arg
);
2225 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CERT_CB_ERROR
);
2229 s
->rwstate
= SSL_X509_LOOKUP
;
2232 s
->rwstate
= SSL_NOTHING
;
2235 /* In TLSv1.3 we selected the ciphersuite before resumption */
2236 if (!SSL_CONNECTION_IS_TLS13(s
)) {
2238 ssl3_choose_cipher(s
, s
->peer_ciphers
,
2239 SSL_get_ciphers(ssl
));
2241 if (cipher
== NULL
) {
2242 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2243 SSL_R_NO_SHARED_CIPHER
);
2246 s
->s3
.tmp
.new_cipher
= cipher
;
2249 if (!tls_choose_sigalg(s
, 1)) {
2250 /* SSLfatal already called */
2253 /* check whether we should disable session resumption */
2254 if (s
->not_resumable_session_cb
!= NULL
)
2255 s
->session
->not_resumable
=
2256 s
->not_resumable_session_cb(ssl
,
2257 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2258 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2259 if (s
->session
->not_resumable
)
2260 /* do not send a session ticket */
2261 s
->ext
.ticket_expected
= 0;
2264 /* Session-id reuse */
2265 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2269 * we now have the following setup.
2271 * cipher_list - our preferred list of ciphers
2272 * ciphers - the clients preferred list of ciphers
2273 * compression - basically ignored right now
2274 * ssl version is set - sslv3
2275 * s->session - The ssl session has been setup.
2276 * s->hit - session reuse flag
2277 * s->s3.tmp.new_cipher - the new cipher to use.
2281 * Call status_request callback if needed. Has to be done after the
2282 * certificate callbacks etc above.
2284 if (!tls_handle_status_request(s
)) {
2285 /* SSLfatal() already called */
2289 * Call alpn_select callback if needed. Has to be done after SNI and
2290 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2291 * we already did this because cipher negotiation happens earlier, and
2292 * we must handle ALPN before we decide whether to accept early_data.
2294 if (!SSL_CONNECTION_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2295 /* SSLfatal() already called */
2301 #ifndef OPENSSL_NO_SRP
2302 if (wst
== WORK_MORE_C
) {
2304 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2306 * callback indicates further work to be done
2308 s
->rwstate
= SSL_X509_LOOKUP
;
2312 /* SSLfatal() already called */
2318 return WORK_FINISHED_STOP
;
2323 CON_FUNC_RETURN
tls_construct_server_hello(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2328 unsigned char *session_id
;
2329 int usetls13
= SSL_CONNECTION_IS_TLS13(s
)
2330 || s
->hello_retry_request
== SSL_HRR_PENDING
;
2332 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2333 if (!WPACKET_put_bytes_u16(pkt
, version
)
2335 * Random stuff. Filling of the server_random takes place in
2336 * tls_process_client_hello()
2338 || !WPACKET_memcpy(pkt
,
2339 s
->hello_retry_request
== SSL_HRR_PENDING
2340 ? hrrrandom
: s
->s3
.server_random
,
2341 SSL3_RANDOM_SIZE
)) {
2342 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2343 return CON_FUNC_ERROR
;
2347 * There are several cases for the session ID to send
2348 * back in the server hello:
2349 * - For session reuse from the session cache,
2350 * we send back the old session ID.
2351 * - If stateless session reuse (using a session ticket)
2352 * is successful, we send back the client's "session ID"
2353 * (which doesn't actually identify the session).
2354 * - If it is a new session, we send back the new
2356 * - However, if we want the new session to be single-use,
2357 * we send back a 0-length session ID.
2358 * - In TLSv1.3 we echo back the session id sent to us by the client
2360 * s->hit is non-zero in either case of session reuse,
2361 * so the following won't overwrite an ID that we're supposed
2364 if (s
->session
->not_resumable
||
2365 (!(SSL_CONNECTION_GET_CTX(s
)->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2367 s
->session
->session_id_length
= 0;
2370 sl
= s
->tmp_session_id_len
;
2371 session_id
= s
->tmp_session_id
;
2373 sl
= s
->session
->session_id_length
;
2374 session_id
= s
->session
->session_id
;
2377 if (sl
> sizeof(s
->session
->session_id
)) {
2378 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2379 return CON_FUNC_ERROR
;
2382 /* set up the compression method */
2383 #ifdef OPENSSL_NO_COMP
2386 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2389 compm
= s
->s3
.tmp
.new_compression
->id
;
2392 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2393 || !SSL_CONNECTION_GET_SSL(s
)->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
,
2395 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2396 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2397 return CON_FUNC_ERROR
;
2400 if (!tls_construct_extensions(s
, pkt
,
2401 s
->hello_retry_request
== SSL_HRR_PENDING
2402 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2403 : (SSL_CONNECTION_IS_TLS13(s
)
2404 ? SSL_EXT_TLS1_3_SERVER_HELLO
2405 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2407 /* SSLfatal() already called */
2408 return CON_FUNC_ERROR
;
2411 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2412 /* Ditch the session. We'll create a new one next time around */
2413 SSL_SESSION_free(s
->session
);
2418 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2419 * a synthetic message_hash in place of ClientHello1.
2421 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2422 /* SSLfatal() already called */
2423 return CON_FUNC_ERROR
;
2425 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2426 && !ssl3_digest_cached_records(s
, 0)) {
2427 /* SSLfatal() already called */;
2428 return CON_FUNC_ERROR
;
2431 return CON_FUNC_SUCCESS
;
2434 CON_FUNC_RETURN
tls_construct_server_done(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2436 if (!s
->s3
.tmp
.cert_request
) {
2437 if (!ssl3_digest_cached_records(s
, 0)) {
2438 /* SSLfatal() already called */
2439 return CON_FUNC_ERROR
;
2442 return CON_FUNC_SUCCESS
;
2445 CON_FUNC_RETURN
tls_construct_server_key_exchange(SSL_CONNECTION
*s
,
2448 EVP_PKEY
*pkdh
= NULL
;
2449 unsigned char *encodedPoint
= NULL
;
2450 size_t encodedlen
= 0;
2452 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2456 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2457 EVP_PKEY_CTX
*pctx
= NULL
;
2458 size_t paramlen
, paramoffset
;
2460 CON_FUNC_RETURN ret
= CON_FUNC_ERROR
;
2461 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2463 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2464 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2468 if (md_ctx
== NULL
) {
2469 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2473 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2475 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2476 #ifndef OPENSSL_NO_PSK
2477 /* Plain PSK or RSAPSK nothing to do */
2478 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2480 #endif /* !OPENSSL_NO_PSK */
2481 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2482 CERT
*cert
= s
->cert
;
2483 EVP_PKEY
*pkdhp
= NULL
;
2485 if (s
->cert
->dh_tmp_auto
) {
2486 pkdh
= ssl_get_auto_dh(s
);
2488 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2493 pkdhp
= cert
->dh_tmp
;
2495 #if !defined(OPENSSL_NO_DEPRECATED_3_0)
2496 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2497 pkdh
= ssl_dh_to_pkey(s
->cert
->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s
),
2500 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2506 if (pkdhp
== NULL
) {
2507 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
2510 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2511 EVP_PKEY_get_security_bits(pkdhp
), 0, pkdhp
)) {
2512 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_DH_KEY_TOO_SMALL
);
2515 if (s
->s3
.tmp
.pkey
!= NULL
) {
2516 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2520 s
->s3
.tmp
.pkey
= ssl_generate_pkey(s
, pkdhp
);
2521 if (s
->s3
.tmp
.pkey
== NULL
) {
2522 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2526 EVP_PKEY_free(pkdh
);
2529 /* These BIGNUMs need to be freed when we're finished */
2531 if (!EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
, OSSL_PKEY_PARAM_FFC_P
,
2533 || !EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
, OSSL_PKEY_PARAM_FFC_G
,
2535 || !EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
,
2536 OSSL_PKEY_PARAM_PUB_KEY
, &r
[2])) {
2537 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2540 } else if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2542 if (s
->s3
.tmp
.pkey
!= NULL
) {
2543 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2547 /* Get NID of appropriate shared curve */
2548 curve_id
= tls1_shared_group(s
, -2);
2549 if (curve_id
== 0) {
2550 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2551 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2554 /* Cache the group used in the SSL_SESSION */
2555 s
->session
->kex_group
= curve_id
;
2556 /* Generate a new key for this curve */
2557 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2558 if (s
->s3
.tmp
.pkey
== NULL
) {
2559 /* SSLfatal() already called */
2563 /* Encode the public key. */
2564 encodedlen
= EVP_PKEY_get1_encoded_public_key(s
->s3
.tmp
.pkey
,
2566 if (encodedlen
== 0) {
2567 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
2572 * We'll generate the serverKeyExchange message explicitly so we
2573 * can set these to NULLs
2580 #ifndef OPENSSL_NO_SRP
2581 if (type
& SSL_kSRP
) {
2582 if ((s
->srp_ctx
.N
== NULL
) ||
2583 (s
->srp_ctx
.g
== NULL
) ||
2584 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2585 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_SRP_PARAM
);
2588 r
[0] = s
->srp_ctx
.N
;
2589 r
[1] = s
->srp_ctx
.g
;
2590 r
[2] = s
->srp_ctx
.s
;
2591 r
[3] = s
->srp_ctx
.B
;
2595 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2599 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2600 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2602 } else if (lu
== NULL
) {
2603 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_INTERNAL_ERROR
);
2607 #ifndef OPENSSL_NO_PSK
2608 if (type
& SSL_PSK
) {
2609 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2610 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2613 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2614 * checked this when we set the identity hint - but just in case
2616 if (len
> PSK_MAX_IDENTITY_LEN
2617 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2619 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2625 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2626 unsigned char *binval
;
2629 #ifndef OPENSSL_NO_SRP
2630 if ((i
== 2) && (type
& SSL_kSRP
)) {
2631 res
= WPACKET_start_sub_packet_u8(pkt
);
2634 res
= WPACKET_start_sub_packet_u16(pkt
);
2637 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2642 * for interoperability with some versions of the Microsoft TLS
2643 * stack, we need to zero pad the DHE pub key to the same length
2646 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2647 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2650 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2651 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2654 memset(binval
, 0, len
);
2658 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2659 || !WPACKET_close(pkt
)) {
2660 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2664 BN_bn2bin(r
[i
], binval
);
2667 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2669 * We only support named (not generic) curves. In this situation, the
2670 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2671 * [1 byte length of encoded point], followed by the actual encoded
2674 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2675 || !WPACKET_put_bytes_u8(pkt
, 0)
2676 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2677 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2678 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2681 OPENSSL_free(encodedPoint
);
2682 encodedPoint
= NULL
;
2687 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2689 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2690 size_t siglen
= 0, tbslen
;
2692 if (pkey
== NULL
|| !tls1_lookup_md(sctx
, lu
, &md
)) {
2693 /* Should never happen */
2694 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2697 /* Get length of the parameters we have written above */
2698 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2699 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2702 /* send signature algorithm */
2703 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2704 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2708 if (EVP_DigestSignInit_ex(md_ctx
, &pctx
,
2709 md
== NULL
? NULL
: EVP_MD_get0_name(md
),
2710 sctx
->libctx
, sctx
->propq
, pkey
,
2712 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2715 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2716 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2717 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2718 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2722 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2723 s
->init_buf
->data
+ paramoffset
,
2726 /* SSLfatal() already called */
2730 if (EVP_DigestSign(md_ctx
, NULL
, &siglen
, tbs
, tbslen
) <=0
2731 || !WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2732 || EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
) <= 0
2733 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2734 || sigbytes1
!= sigbytes2
) {
2736 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2742 ret
= CON_FUNC_SUCCESS
;
2744 EVP_PKEY_free(pkdh
);
2745 OPENSSL_free(encodedPoint
);
2746 EVP_MD_CTX_free(md_ctx
);
2756 CON_FUNC_RETURN
tls_construct_certificate_request(SSL_CONNECTION
*s
,
2759 if (SSL_CONNECTION_IS_TLS13(s
)) {
2760 /* Send random context when doing post-handshake auth */
2761 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2762 OPENSSL_free(s
->pha_context
);
2763 s
->pha_context_len
= 32;
2764 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
) {
2765 s
->pha_context_len
= 0;
2766 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2767 return CON_FUNC_ERROR
;
2769 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
2770 s
->pha_context
, s
->pha_context_len
, 0) <= 0
2771 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
,
2772 s
->pha_context_len
)) {
2773 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2774 return CON_FUNC_ERROR
;
2776 /* reset the handshake hash back to just after the ClientFinished */
2777 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2778 /* SSLfatal() already called */
2779 return CON_FUNC_ERROR
;
2782 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2783 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2784 return CON_FUNC_ERROR
;
2788 if (!tls_construct_extensions(s
, pkt
,
2789 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2791 /* SSLfatal() already called */
2792 return CON_FUNC_ERROR
;
2797 /* get the list of acceptable cert types */
2798 if (!WPACKET_start_sub_packet_u8(pkt
)
2799 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2800 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2801 return CON_FUNC_ERROR
;
2804 if (SSL_USE_SIGALGS(s
)) {
2805 const uint16_t *psigs
;
2806 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2808 if (!WPACKET_start_sub_packet_u16(pkt
)
2809 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2810 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2811 || !WPACKET_close(pkt
)) {
2812 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2813 return CON_FUNC_ERROR
;
2817 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2818 /* SSLfatal() already called */
2819 return CON_FUNC_ERROR
;
2824 s
->s3
.tmp
.cert_request
= 1;
2825 return CON_FUNC_SUCCESS
;
2828 static int tls_process_cke_psk_preamble(SSL_CONNECTION
*s
, PACKET
*pkt
)
2830 #ifndef OPENSSL_NO_PSK
2831 unsigned char psk
[PSK_MAX_PSK_LEN
];
2833 PACKET psk_identity
;
2835 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2836 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2839 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2840 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DATA_LENGTH_TOO_LONG
);
2843 if (s
->psk_server_callback
== NULL
) {
2844 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_PSK_NO_SERVER_CB
);
2848 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2849 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2853 psklen
= s
->psk_server_callback(SSL_CONNECTION_GET_SSL(s
),
2854 s
->session
->psk_identity
,
2857 if (psklen
> PSK_MAX_PSK_LEN
) {
2858 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2860 } else if (psklen
== 0) {
2862 * PSK related to the given identity not found
2864 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
, SSL_R_PSK_IDENTITY_NOT_FOUND
);
2868 OPENSSL_free(s
->s3
.tmp
.psk
);
2869 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2870 OPENSSL_cleanse(psk
, psklen
);
2872 if (s
->s3
.tmp
.psk
== NULL
) {
2873 s
->s3
.tmp
.psklen
= 0;
2874 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2878 s
->s3
.tmp
.psklen
= psklen
;
2882 /* Should never happen */
2883 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2888 static int tls_process_cke_rsa(SSL_CONNECTION
*s
, PACKET
*pkt
)
2891 PACKET enc_premaster
;
2892 EVP_PKEY
*rsa
= NULL
;
2893 unsigned char *rsa_decrypt
= NULL
;
2895 EVP_PKEY_CTX
*ctx
= NULL
;
2896 OSSL_PARAM params
[3], *p
= params
;
2897 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2899 rsa
= s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
;
2901 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_RSA_CERTIFICATE
);
2905 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2906 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2907 enc_premaster
= *pkt
;
2909 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2910 || PACKET_remaining(pkt
) != 0) {
2911 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2916 outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
2917 rsa_decrypt
= OPENSSL_malloc(outlen
);
2918 if (rsa_decrypt
== NULL
) {
2919 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2923 ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, rsa
, sctx
->propq
);
2925 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2930 * We must not leak whether a decryption failure occurs because of
2931 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2932 * section 7.4.7.1). We use the special padding type
2933 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
2934 * RSA, check the padding and check that the client version is as expected
2935 * in the premaster secret. If any of that fails then the function appears
2936 * to return successfully but with a random result. The call below could
2937 * still fail if the input is publicly invalid.
2938 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2940 if (EVP_PKEY_decrypt_init(ctx
) <= 0
2941 || EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_WITH_TLS_PADDING
) <= 0) {
2942 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2946 *p
++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
,
2947 (unsigned int *)&s
->client_version
);
2948 if ((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) != 0)
2949 *p
++ = OSSL_PARAM_construct_uint(
2950 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
,
2951 (unsigned int *)&s
->version
);
2952 *p
++ = OSSL_PARAM_construct_end();
2954 if (!EVP_PKEY_CTX_set_params(ctx
, params
)
2955 || EVP_PKEY_decrypt(ctx
, rsa_decrypt
, &outlen
,
2956 PACKET_data(&enc_premaster
),
2957 PACKET_remaining(&enc_premaster
)) <= 0) {
2958 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2963 * This test should never fail (otherwise we should have failed above) but
2964 * we double check anyway.
2966 if (outlen
!= SSL_MAX_MASTER_KEY_LENGTH
) {
2967 OPENSSL_cleanse(rsa_decrypt
, SSL_MAX_MASTER_KEY_LENGTH
);
2968 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2972 /* Also cleanses rsa_decrypt (on success or failure) */
2973 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
2974 SSL_MAX_MASTER_KEY_LENGTH
, 0)) {
2975 /* SSLfatal() already called */
2981 OPENSSL_free(rsa_decrypt
);
2982 EVP_PKEY_CTX_free(ctx
);
2986 static int tls_process_cke_dhe(SSL_CONNECTION
*s
, PACKET
*pkt
)
2988 EVP_PKEY
*skey
= NULL
;
2990 const unsigned char *data
;
2991 EVP_PKEY
*ckey
= NULL
;
2994 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
2995 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2998 skey
= s
->s3
.tmp
.pkey
;
3000 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
3004 if (PACKET_remaining(pkt
) == 0L) {
3005 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
3008 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3009 /* We already checked we have enough data */
3010 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3013 ckey
= EVP_PKEY_new();
3014 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3015 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
3019 if (!EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
)) {
3020 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3024 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3025 /* SSLfatal() already called */
3030 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3031 s
->s3
.tmp
.pkey
= NULL
;
3033 EVP_PKEY_free(ckey
);
3037 static int tls_process_cke_ecdhe(SSL_CONNECTION
*s
, PACKET
*pkt
)
3039 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3040 EVP_PKEY
*ckey
= NULL
;
3043 if (PACKET_remaining(pkt
) == 0L) {
3044 /* We don't support ECDH client auth */
3045 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_MISSING_TMP_ECDH_KEY
);
3049 const unsigned char *data
;
3052 * Get client's public key from encoded point in the
3053 * ClientKeyExchange message.
3056 /* Get encoded point length */
3057 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3058 || PACKET_remaining(pkt
) != 0) {
3059 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3063 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_ECDH_KEY
);
3067 ckey
= EVP_PKEY_new();
3068 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3069 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
3073 if (EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
) <= 0) {
3074 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
3079 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3080 /* SSLfatal() already called */
3085 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3086 s
->s3
.tmp
.pkey
= NULL
;
3088 EVP_PKEY_free(ckey
);
3093 static int tls_process_cke_srp(SSL_CONNECTION
*s
, PACKET
*pkt
)
3095 #ifndef OPENSSL_NO_SRP
3097 const unsigned char *data
;
3099 if (!PACKET_get_net_2(pkt
, &i
)
3100 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3101 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_SRP_A_LENGTH
);
3104 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3105 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BN_LIB
);
3108 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3109 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_SRP_PARAMETERS
);
3112 OPENSSL_free(s
->session
->srp_username
);
3113 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3114 if (s
->session
->srp_username
== NULL
) {
3115 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3119 if (!srp_generate_server_master_secret(s
)) {
3120 /* SSLfatal() already called */
3126 /* Should never happen */
3127 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3132 static int tls_process_cke_gost(SSL_CONNECTION
*s
, PACKET
*pkt
)
3134 #ifndef OPENSSL_NO_GOST
3135 EVP_PKEY_CTX
*pkey_ctx
;
3136 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3137 unsigned char premaster_secret
[32];
3138 const unsigned char *start
;
3139 size_t outlen
= 32, inlen
;
3140 unsigned long alg_a
;
3141 GOST_KX_MESSAGE
*pKX
= NULL
;
3142 const unsigned char *ptr
;
3144 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3146 /* Get our certificate private key */
3147 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3148 if (alg_a
& SSL_aGOST12
) {
3150 * New GOST ciphersuites have SSL_aGOST01 bit too
3152 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3154 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3157 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3159 } else if (alg_a
& SSL_aGOST01
) {
3160 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3163 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, pk
, sctx
->propq
);
3164 if (pkey_ctx
== NULL
) {
3165 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3168 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3169 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3173 * If client certificate is present and is of the same type, maybe
3174 * use it for key exchange. Don't mind errors from
3175 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3176 * client certificate for authorization only.
3178 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3179 if (client_pub_pkey
) {
3180 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3184 ptr
= PACKET_data(pkt
);
3185 /* Some implementations provide extra data in the opaqueBlob
3186 * We have nothing to do with this blob so we just skip it */
3187 pKX
= d2i_GOST_KX_MESSAGE(NULL
, &ptr
, PACKET_remaining(pkt
));
3189 || pKX
->kxBlob
== NULL
3190 || ASN1_TYPE_get(pKX
->kxBlob
) != V_ASN1_SEQUENCE
) {
3191 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3195 if (!PACKET_forward(pkt
, ptr
- PACKET_data(pkt
))) {
3196 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_DECRYPTION_FAILED
);
3200 if (PACKET_remaining(pkt
) != 0) {
3201 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_DECRYPTION_FAILED
);
3205 inlen
= pKX
->kxBlob
->value
.sequence
->length
;
3206 start
= pKX
->kxBlob
->value
.sequence
->data
;
3208 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3210 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3213 /* Generate master secret */
3214 if (!ssl_generate_master_secret(s
, premaster_secret
,
3215 sizeof(premaster_secret
), 0)) {
3216 /* SSLfatal() already called */
3219 /* Check if pubkey from client certificate was used */
3220 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3222 s
->statem
.no_cert_verify
= 1;
3226 EVP_PKEY_CTX_free(pkey_ctx
);
3227 GOST_KX_MESSAGE_free(pKX
);
3230 /* Should never happen */
3231 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3236 static int tls_process_cke_gost18(SSL_CONNECTION
*s
, PACKET
*pkt
)
3238 #ifndef OPENSSL_NO_GOST
3239 unsigned char rnd_dgst
[32];
3240 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3241 EVP_PKEY
*pk
= NULL
;
3242 unsigned char premaster_secret
[32];
3243 const unsigned char *start
= NULL
;
3244 size_t outlen
= 32, inlen
= 0;
3246 int cipher_nid
= ossl_gost18_cke_cipher_nid(s
);
3247 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3249 if (cipher_nid
== NID_undef
) {
3250 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3254 if (ossl_gost_ukm(s
, rnd_dgst
) <= 0) {
3255 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3259 /* Get our certificate private key */
3260 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
!= NULL
?
3261 s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
:
3262 s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3264 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
3268 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, pk
, sctx
->propq
);
3269 if (pkey_ctx
== NULL
) {
3270 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3273 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3274 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3278 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3279 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3280 EVP_PKEY_CTRL_SET_IV
, 32, rnd_dgst
) <= 0) {
3281 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3285 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3286 EVP_PKEY_CTRL_CIPHER
, cipher_nid
, NULL
) <= 0) {
3287 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3290 inlen
= PACKET_remaining(pkt
);
3291 start
= PACKET_data(pkt
);
3293 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3294 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3297 /* Generate master secret */
3298 if (!ssl_generate_master_secret(s
, premaster_secret
,
3299 sizeof(premaster_secret
), 0)) {
3300 /* SSLfatal() already called */
3306 EVP_PKEY_CTX_free(pkey_ctx
);
3309 /* Should never happen */
3310 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3315 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL_CONNECTION
*s
,
3318 unsigned long alg_k
;
3320 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3322 /* For PSK parse and retrieve identity, obtain PSK key */
3323 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3324 /* SSLfatal() already called */
3328 if (alg_k
& SSL_kPSK
) {
3329 /* Identity extracted earlier: should be nothing left */
3330 if (PACKET_remaining(pkt
) != 0) {
3331 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3334 /* PSK handled by ssl_generate_master_secret */
3335 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3336 /* SSLfatal() already called */
3339 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3340 if (!tls_process_cke_rsa(s
, pkt
)) {
3341 /* SSLfatal() already called */
3344 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3345 if (!tls_process_cke_dhe(s
, pkt
)) {
3346 /* SSLfatal() already called */
3349 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3350 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3351 /* SSLfatal() already called */
3354 } else if (alg_k
& SSL_kSRP
) {
3355 if (!tls_process_cke_srp(s
, pkt
)) {
3356 /* SSLfatal() already called */
3359 } else if (alg_k
& SSL_kGOST
) {
3360 if (!tls_process_cke_gost(s
, pkt
)) {
3361 /* SSLfatal() already called */
3364 } else if (alg_k
& SSL_kGOST18
) {
3365 if (!tls_process_cke_gost18(s
, pkt
)) {
3366 /* SSLfatal() already called */
3370 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNKNOWN_CIPHER_TYPE
);
3374 return MSG_PROCESS_CONTINUE_PROCESSING
;
3376 #ifndef OPENSSL_NO_PSK
3377 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3378 s
->s3
.tmp
.psk
= NULL
;
3379 s
->s3
.tmp
.psklen
= 0;
3381 return MSG_PROCESS_ERROR
;
3384 WORK_STATE
tls_post_process_client_key_exchange(SSL_CONNECTION
*s
,
3387 #ifndef OPENSSL_NO_SCTP
3388 if (wst
== WORK_MORE_A
) {
3389 if (SSL_CONNECTION_IS_DTLS(s
)) {
3390 unsigned char sctpauthkey
[64];
3391 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3394 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3397 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3398 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3400 /* Don't include the terminating zero. */
3401 labellen
= sizeof(labelbuffer
) - 1;
3402 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3405 if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s
),
3407 sizeof(sctpauthkey
), labelbuffer
,
3410 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3414 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3415 sizeof(sctpauthkey
), sctpauthkey
);
3420 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3422 * No certificate verify or no peer certificate so we no longer need
3423 * the handshake_buffer
3425 if (!ssl3_digest_cached_records(s
, 0)) {
3426 /* SSLfatal() already called */
3429 return WORK_FINISHED_CONTINUE
;
3431 if (!s
->s3
.handshake_buffer
) {
3432 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3436 * For sigalgs freeze the handshake buffer. If we support
3437 * extms we've done this already so this is a no-op
3439 if (!ssl3_digest_cached_records(s
, 1)) {
3440 /* SSLfatal() already called */
3445 return WORK_FINISHED_CONTINUE
;
3448 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL_CONNECTION
*s
,
3452 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3455 const unsigned char *certstart
, *certbytes
;
3456 STACK_OF(X509
) *sk
= NULL
;
3457 PACKET spkt
, context
;
3459 SSL_SESSION
*new_sess
= NULL
;
3460 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3463 * To get this far we must have read encrypted data from the client. We no
3464 * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3
3466 if (s
->rlayer
.rrlmethod
->set_plain_alerts
!= NULL
)
3467 s
->rlayer
.rrlmethod
->set_plain_alerts(s
->rlayer
.rrl
, 0);
3469 if ((sk
= sk_X509_new_null()) == NULL
) {
3470 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3474 if (SSL_CONNECTION_IS_TLS13(s
)
3475 && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3476 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3477 || (s
->pha_context
!= NULL
3478 && !PACKET_equal(&context
, s
->pha_context
,
3479 s
->pha_context_len
)))) {
3480 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
3484 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3485 || PACKET_remaining(pkt
) != 0) {
3486 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3490 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3491 if (!PACKET_get_net_3(&spkt
, &l
)
3492 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3493 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
3497 certstart
= certbytes
;
3498 x
= X509_new_ex(sctx
->libctx
, sctx
->propq
);
3500 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_MALLOC_FAILURE
);
3503 if (d2i_X509(&x
, (const unsigned char **)&certbytes
, l
) == NULL
) {
3504 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_ASN1_LIB
);
3508 if (certbytes
!= (certstart
+ l
)) {
3509 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
3513 if (SSL_CONNECTION_IS_TLS13(s
)) {
3514 RAW_EXTENSION
*rawexts
= NULL
;
3517 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3518 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
3521 if (!tls_collect_extensions(s
, &extensions
,
3522 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3523 NULL
, chainidx
== 0)
3524 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3525 rawexts
, x
, chainidx
,
3526 PACKET_remaining(&spkt
) == 0)) {
3527 OPENSSL_free(rawexts
);
3530 OPENSSL_free(rawexts
);
3533 if (!sk_X509_push(sk
, x
)) {
3534 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3540 if (sk_X509_num(sk
) <= 0) {
3541 /* TLS does not mind 0 certs returned */
3542 if (s
->version
== SSL3_VERSION
) {
3543 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3544 SSL_R_NO_CERTIFICATES_RETURNED
);
3547 /* Fail for TLS only if we required a certificate */
3548 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3549 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3550 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3551 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3554 /* No client certificate so digest cached records */
3555 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3556 /* SSLfatal() already called */
3561 i
= ssl_verify_cert_chain(s
, sk
);
3563 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3564 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3567 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3569 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3570 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3576 * Sessions must be immutable once they go into the session cache. Otherwise
3577 * we can get multi-thread problems. Therefore we don't "update" sessions,
3578 * we replace them with a duplicate. Here, we need to do this every time
3579 * a new certificate is received via post-handshake authentication, as the
3580 * session may have already gone into the session cache.
3583 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3584 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3585 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3589 SSL_SESSION_free(s
->session
);
3590 s
->session
= new_sess
;
3593 X509_free(s
->session
->peer
);
3594 s
->session
->peer
= sk_X509_num(sk
) == 0 ? NULL
: sk_X509_shift(sk
);
3595 s
->session
->verify_result
= s
->verify_result
;
3597 OSSL_STACK_OF_X509_free(s
->session
->peer_chain
);
3598 s
->session
->peer_chain
= sk
;
3602 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3605 if (SSL_CONNECTION_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3606 /* SSLfatal() already called */
3611 * Inconsistency alert: cert_chain does *not* include the peer's own
3612 * certificate, while we do include it in statem_clnt.c
3615 /* Save the current hash state for when we receive the CertificateVerify */
3616 if (SSL_CONNECTION_IS_TLS13(s
)) {
3617 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3618 sizeof(s
->cert_verify_hash
),
3619 &s
->cert_verify_hash_len
)) {
3620 /* SSLfatal() already called */
3624 /* Resend session tickets */
3625 s
->sent_tickets
= 0;
3628 ret
= MSG_PROCESS_CONTINUE_READING
;
3632 OSSL_STACK_OF_X509_free(sk
);
3636 CON_FUNC_RETURN
tls_construct_server_certificate(SSL_CONNECTION
*s
, WPACKET
*pkt
)
3638 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3641 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3642 return CON_FUNC_ERROR
;
3646 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3647 * for the server Certificate message
3649 if (SSL_CONNECTION_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3650 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3651 return CON_FUNC_ERROR
;
3653 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3654 /* SSLfatal() already called */
3655 return CON_FUNC_ERROR
;
3658 return CON_FUNC_SUCCESS
;
3661 static int create_ticket_prequel(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3662 uint32_t age_add
, unsigned char *tick_nonce
)
3664 uint32_t timeout
= (uint32_t)ossl_time2seconds(s
->session
->timeout
);
3667 * Ticket lifetime hint:
3668 * In TLSv1.3 we reset the "time" field above, and always specify the
3669 * timeout, limited to a 1 week period per RFC8446.
3670 * For TLSv1.2 this is advisory only and we leave this unspecified for
3671 * resumed session (for simplicity).
3673 #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
3675 if (SSL_CONNECTION_IS_TLS13(s
)) {
3676 if (ossl_time_compare(s
->session
->timeout
,
3677 ossl_seconds2time(ONE_WEEK_SEC
)) > 0)
3678 timeout
= ONE_WEEK_SEC
;
3682 if (!WPACKET_put_bytes_u32(pkt
, timeout
)) {
3683 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3687 if (SSL_CONNECTION_IS_TLS13(s
)) {
3688 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3689 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3690 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3695 /* Start the sub-packet for the actual ticket data */
3696 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3697 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3704 static CON_FUNC_RETURN
construct_stateless_ticket(SSL_CONNECTION
*s
,
3707 unsigned char *tick_nonce
)
3709 unsigned char *senc
= NULL
;
3710 EVP_CIPHER_CTX
*ctx
= NULL
;
3711 SSL_HMAC
*hctx
= NULL
;
3712 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3713 const unsigned char *const_p
;
3714 int len
, slen_full
, slen
, lenfinal
;
3717 SSL_CTX
*tctx
= s
->session_ctx
;
3718 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3719 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3721 CON_FUNC_RETURN ok
= CON_FUNC_ERROR
;
3722 size_t macoffset
, macendoffset
;
3723 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
3724 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3726 /* get session encoding length */
3727 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3729 * Some length values are 16 bits, so forget it if session is too
3732 if (slen_full
== 0 || slen_full
> 0xFF00) {
3733 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3736 senc
= OPENSSL_malloc(slen_full
);
3738 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3742 ctx
= EVP_CIPHER_CTX_new();
3743 hctx
= ssl_hmac_new(tctx
);
3744 if (ctx
== NULL
|| hctx
== NULL
) {
3745 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3750 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3751 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3756 * create a fresh copy (not shared with other threads) to clean up
3759 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3761 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3765 slen
= i2d_SSL_SESSION(sess
, NULL
);
3766 if (slen
== 0 || slen
> slen_full
) {
3767 /* shouldn't ever happen */
3768 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3769 SSL_SESSION_free(sess
);
3773 if (!i2d_SSL_SESSION(sess
, &p
)) {
3774 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3775 SSL_SESSION_free(sess
);
3778 SSL_SESSION_free(sess
);
3781 * Initialize HMAC and cipher contexts. If callback present it does
3782 * all the work otherwise use generated values from parent ctx.
3784 #ifndef OPENSSL_NO_DEPRECATED_3_0
3785 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
|| tctx
->ext
.ticket_key_cb
!= NULL
)
3787 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3792 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3793 ret
= tctx
->ext
.ticket_key_evp_cb(ssl
, key_name
, iv
, ctx
,
3794 ssl_hmac_get0_EVP_MAC_CTX(hctx
),
3796 #ifndef OPENSSL_NO_DEPRECATED_3_0
3797 else if (tctx
->ext
.ticket_key_cb
!= NULL
)
3798 /* if 0 is returned, write an empty ticket */
3799 ret
= tctx
->ext
.ticket_key_cb(ssl
, key_name
, iv
, ctx
,
3800 ssl_hmac_get0_HMAC_CTX(hctx
), 1);
3805 * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0
3806 * length ticket is not allowed so we abort construction of the
3809 if (SSL_CONNECTION_IS_TLS13(s
)) {
3810 ok
= CON_FUNC_DONT_SEND
;
3813 /* Put timeout and length */
3814 if (!WPACKET_put_bytes_u32(pkt
, 0)
3815 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3816 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3820 EVP_CIPHER_CTX_free(ctx
);
3821 ssl_hmac_free(hctx
);
3822 return CON_FUNC_SUCCESS
;
3825 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CALLBACK_FAILED
);
3828 iv_len
= EVP_CIPHER_CTX_get_iv_length(ctx
);
3830 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3834 EVP_CIPHER
*cipher
= EVP_CIPHER_fetch(sctx
->libctx
, "AES-256-CBC",
3837 if (cipher
== NULL
) {
3838 /* Error is already recorded */
3839 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
3843 iv_len
= EVP_CIPHER_get_iv_length(cipher
);
3845 || RAND_bytes_ex(sctx
->libctx
, iv
, iv_len
, 0) <= 0
3846 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3847 tctx
->ext
.secure
->tick_aes_key
, iv
)
3848 || !ssl_hmac_init(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
3849 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
3851 EVP_CIPHER_free(cipher
);
3852 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3855 EVP_CIPHER_free(cipher
);
3856 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3857 sizeof(tctx
->ext
.tick_key_name
));
3860 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3861 /* SSLfatal() already called */
3865 if (!WPACKET_get_total_written(pkt
, &macoffset
)
3866 /* Output key name */
3867 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3869 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3870 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3872 /* Encrypt session data */
3873 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3874 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3875 || encdata1
!= encdata2
3876 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3877 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3878 || encdata1
+ len
!= encdata2
3879 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3880 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3881 || !ssl_hmac_update(hctx
,
3882 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3883 macendoffset
- macoffset
)
3884 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3885 || !ssl_hmac_final(hctx
, macdata1
, &hlen
, EVP_MAX_MD_SIZE
)
3886 || hlen
> EVP_MAX_MD_SIZE
3887 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3888 || macdata1
!= macdata2
) {
3889 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3893 /* Close the sub-packet created by create_ticket_prequel() */
3894 if (!WPACKET_close(pkt
)) {
3895 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3899 ok
= CON_FUNC_SUCCESS
;
3902 EVP_CIPHER_CTX_free(ctx
);
3903 ssl_hmac_free(hctx
);
3907 static int construct_stateful_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3909 unsigned char *tick_nonce
)
3911 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3912 /* SSLfatal() already called */
3916 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
3917 s
->session
->session_id_length
)
3918 || !WPACKET_close(pkt
)) {
3919 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3926 static void tls_update_ticket_counts(SSL_CONNECTION
*s
)
3929 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
3930 * gets reset to 0 if we send more tickets following a post-handshake
3931 * auth, but |next_ticket_nonce| does not. If we're sending extra
3932 * tickets, decrement the count of pending extra tickets.
3935 s
->next_ticket_nonce
++;
3936 if (s
->ext
.extra_tickets_expected
> 0)
3937 s
->ext
.extra_tickets_expected
--;
3940 CON_FUNC_RETURN
tls_construct_new_session_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
)
3942 SSL_CTX
*tctx
= s
->session_ctx
;
3943 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
3945 unsigned char age_add_c
[sizeof(uint32_t)];
3948 CON_FUNC_RETURN ret
= CON_FUNC_ERROR
;
3950 age_add_u
.age_add
= 0;
3952 if (SSL_CONNECTION_IS_TLS13(s
)) {
3955 static const unsigned char nonce_label
[] = "resumption";
3956 const EVP_MD
*md
= ssl_handshake_md(s
);
3957 int hashleni
= EVP_MD_get_size(md
);
3959 /* Ensure cast to size_t is safe */
3960 if (!ossl_assert(hashleni
>= 0)) {
3961 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3964 hashlen
= (size_t)hashleni
;
3967 * If we already sent one NewSessionTicket, or we resumed then
3968 * s->session may already be in a cache and so we must not modify it.
3969 * Instead we need to take a copy of it and modify that.
3971 if (s
->sent_tickets
!= 0 || s
->hit
) {
3972 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
3974 if (new_sess
== NULL
) {
3975 /* SSLfatal already called */
3979 SSL_SESSION_free(s
->session
);
3980 s
->session
= new_sess
;
3983 if (!ssl_generate_session_id(s
, s
->session
)) {
3984 /* SSLfatal() already called */
3987 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
3988 age_add_u
.age_add_c
, sizeof(age_add_u
), 0) <= 0) {
3989 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3992 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
3994 nonce
= s
->next_ticket_nonce
;
3995 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
3996 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
4000 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
4002 sizeof(nonce_label
) - 1,
4005 s
->session
->master_key
,
4007 /* SSLfatal() already called */
4010 s
->session
->master_key_length
= hashlen
;
4012 s
->session
->time
= ossl_time_now();
4013 ssl_session_calculate_timeout(s
->session
);
4014 if (s
->s3
.alpn_selected
!= NULL
) {
4015 OPENSSL_free(s
->session
->ext
.alpn_selected
);
4016 s
->session
->ext
.alpn_selected
=
4017 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
4018 if (s
->session
->ext
.alpn_selected
== NULL
) {
4019 s
->session
->ext
.alpn_selected_len
= 0;
4020 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
4023 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
4025 s
->session
->ext
.max_early_data
= s
->max_early_data
;
4028 if (tctx
->generate_ticket_cb
!= NULL
&&
4029 tctx
->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s
),
4030 tctx
->ticket_cb_data
) == 0) {
4031 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4035 * If we are using anti-replay protection then we behave as if
4036 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4037 * is no point in using full stateless tickets.
4039 if (SSL_CONNECTION_IS_TLS13(s
)
4040 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4041 || (s
->max_early_data
> 0
4042 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4043 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4044 /* SSLfatal() already called */
4048 CON_FUNC_RETURN tmpret
;
4050 tmpret
= construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4052 if (tmpret
!= CON_FUNC_SUCCESS
) {
4053 if (tmpret
== CON_FUNC_DONT_SEND
) {
4054 /* Non-fatal. Abort construction but continue */
4055 ret
= CON_FUNC_DONT_SEND
;
4056 /* We count this as a success so update the counts anwyay */
4057 tls_update_ticket_counts(s
);
4059 /* else SSLfatal() already called */
4064 if (SSL_CONNECTION_IS_TLS13(s
)) {
4065 if (!tls_construct_extensions(s
, pkt
,
4066 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4068 /* SSLfatal() already called */
4071 tls_update_ticket_counts(s
);
4072 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4075 ret
= CON_FUNC_SUCCESS
;
4081 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4082 * create a separate message. Returns 1 on success or 0 on failure.
4084 int tls_construct_cert_status_body(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4086 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4087 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4088 s
->ext
.ocsp
.resp_len
)) {
4089 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4096 CON_FUNC_RETURN
tls_construct_cert_status(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4098 if (!tls_construct_cert_status_body(s
, pkt
)) {
4099 /* SSLfatal() already called */
4100 return CON_FUNC_ERROR
;
4103 return CON_FUNC_SUCCESS
;
4106 #ifndef OPENSSL_NO_NEXTPROTONEG
4108 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4109 * It sets the next_proto member in s if found
4111 MSG_PROCESS_RETURN
tls_process_next_proto(SSL_CONNECTION
*s
, PACKET
*pkt
)
4113 PACKET next_proto
, padding
;
4114 size_t next_proto_len
;
4117 * The payload looks like:
4119 * uint8 proto[proto_len];
4120 * uint8 padding_len;
4121 * uint8 padding[padding_len];
4123 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4124 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4125 || PACKET_remaining(pkt
) > 0) {
4126 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
4127 return MSG_PROCESS_ERROR
;
4130 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4132 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4133 return MSG_PROCESS_ERROR
;
4136 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4138 return MSG_PROCESS_CONTINUE_READING
;
4142 static CON_FUNC_RETURN
tls_construct_encrypted_extensions(SSL_CONNECTION
*s
,
4145 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4147 /* SSLfatal() already called */
4148 return CON_FUNC_ERROR
;
4151 return CON_FUNC_SUCCESS
;
4154 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL_CONNECTION
*s
, PACKET
*pkt
)
4156 if (PACKET_remaining(pkt
) != 0) {
4157 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
4158 return MSG_PROCESS_ERROR
;
4161 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4162 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4163 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4164 return MSG_PROCESS_ERROR
;
4168 * EndOfEarlyData signals a key change so the end of the message must be on
4169 * a record boundary.
4171 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4172 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
4173 return MSG_PROCESS_ERROR
;
4176 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4177 if (!SSL_CONNECTION_GET_SSL(s
)->method
->ssl3_enc
->change_cipher_state(s
,
4178 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4179 /* SSLfatal() already called */
4180 return MSG_PROCESS_ERROR
;
4183 return MSG_PROCESS_CONTINUE_READING
;