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 int tls_construct_encrypted_extensions(SSL_CONNECTION
*s
, WPACKET
*pkt
);
49 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
50 * handshake state transitions when a TLSv1.3 server is reading messages from
51 * the client. The message type that the client has sent is provided in |mt|.
52 * The current state is in |s->statem.hand_state|.
54 * Return values are 1 for success (transition allowed) and 0 on error
55 * (transition not allowed)
57 static int ossl_statem_server13_read_transition(SSL_CONNECTION
*s
, int mt
)
59 OSSL_STATEM
*st
= &s
->statem
;
62 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
63 * not negotiated TLSv1.3 yet, so that case is handled by
64 * ossl_statem_server_read_transition()
66 switch (st
->hand_state
) {
70 case TLS_ST_EARLY_DATA
:
71 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
72 if (mt
== SSL3_MT_CLIENT_HELLO
) {
73 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
77 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
78 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
79 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
86 case TLS_ST_SR_END_OF_EARLY_DATA
:
87 case TLS_ST_SW_FINISHED
:
88 if (s
->s3
.tmp
.cert_request
) {
89 if (mt
== SSL3_MT_CERTIFICATE
) {
90 st
->hand_state
= TLS_ST_SR_CERT
;
94 if (mt
== SSL3_MT_FINISHED
) {
95 st
->hand_state
= TLS_ST_SR_FINISHED
;
102 if (s
->session
->peer
== NULL
) {
103 if (mt
== SSL3_MT_FINISHED
) {
104 st
->hand_state
= TLS_ST_SR_FINISHED
;
108 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
109 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
115 case TLS_ST_SR_CERT_VRFY
:
116 if (mt
== SSL3_MT_FINISHED
) {
117 st
->hand_state
= TLS_ST_SR_FINISHED
;
124 * Its never ok to start processing handshake messages in the middle of
125 * early data (i.e. before we've received the end of early data alert)
127 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
130 if (mt
== SSL3_MT_CERTIFICATE
131 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
132 st
->hand_state
= TLS_ST_SR_CERT
;
136 if (mt
== SSL3_MT_KEY_UPDATE
) {
137 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
143 /* No valid transition found */
148 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
149 * handshake state transitions when the server is reading messages from the
150 * client. The message type that the client has sent is provided in |mt|. The
151 * current state is in |s->statem.hand_state|.
153 * Return values are 1 for success (transition allowed) and 0 on error
154 * (transition not allowed)
156 int ossl_statem_server_read_transition(SSL_CONNECTION
*s
, int mt
)
158 OSSL_STATEM
*st
= &s
->statem
;
160 if (SSL_CONNECTION_IS_TLS13(s
)) {
161 if (!ossl_statem_server13_read_transition(s
, mt
))
166 switch (st
->hand_state
) {
172 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
173 if (mt
== SSL3_MT_CLIENT_HELLO
) {
174 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
179 case TLS_ST_SW_SRVR_DONE
:
181 * If we get a CKE message after a ServerDone then either
182 * 1) We didn't request a Certificate
184 * 2) If we did request one then
185 * a) We allow no Certificate to be returned
187 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
188 * list if we requested a certificate)
190 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
191 if (s
->s3
.tmp
.cert_request
) {
192 if (s
->version
== SSL3_VERSION
) {
193 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
194 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
196 * This isn't an unexpected message as such - we're just
197 * not going to accept it because we require a client
200 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
201 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
204 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
208 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
211 } else if (s
->s3
.tmp
.cert_request
) {
212 if (mt
== SSL3_MT_CERTIFICATE
) {
213 st
->hand_state
= TLS_ST_SR_CERT
;
220 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
221 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
226 case TLS_ST_SR_KEY_EXCH
:
228 * We should only process a CertificateVerify message if we have
229 * received a Certificate from the client. If so then |s->session->peer|
230 * will be non NULL. In some instances a CertificateVerify message is
231 * not required even if the peer has sent a Certificate (e.g. such as in
232 * the case of static DH). In that case |st->no_cert_verify| should be
235 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
236 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
238 * For the ECDH ciphersuites when the client sends its ECDH
239 * pub key in a certificate, the CertificateVerify message is
240 * not sent. Also for GOST ciphersuites when the client uses
241 * its key from the certificate for key exchange.
243 st
->hand_state
= TLS_ST_SR_CHANGE
;
247 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
248 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
254 case TLS_ST_SR_CERT_VRFY
:
255 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
256 st
->hand_state
= TLS_ST_SR_CHANGE
;
261 case TLS_ST_SR_CHANGE
:
262 #ifndef OPENSSL_NO_NEXTPROTONEG
263 if (s
->s3
.npn_seen
) {
264 if (mt
== SSL3_MT_NEXT_PROTO
) {
265 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
270 if (mt
== SSL3_MT_FINISHED
) {
271 st
->hand_state
= TLS_ST_SR_FINISHED
;
274 #ifndef OPENSSL_NO_NEXTPROTONEG
279 #ifndef OPENSSL_NO_NEXTPROTONEG
280 case TLS_ST_SR_NEXT_PROTO
:
281 if (mt
== SSL3_MT_FINISHED
) {
282 st
->hand_state
= TLS_ST_SR_FINISHED
;
288 case TLS_ST_SW_FINISHED
:
289 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
290 st
->hand_state
= TLS_ST_SR_CHANGE
;
297 /* No valid transition found */
298 if (SSL_CONNECTION_IS_DTLS(s
) && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
302 * CCS messages don't have a message sequence number so this is probably
303 * because of an out-of-order CCS. We'll just drop it.
306 s
->rwstate
= SSL_READING
;
307 rbio
= SSL_get_rbio(SSL_CONNECTION_GET_SSL(s
));
308 BIO_clear_retry_flags(rbio
);
309 BIO_set_retry_read(rbio
);
312 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
317 * Should we send a ServerKeyExchange message?
319 * Valid return values are:
323 static int send_server_key_exchange(SSL_CONNECTION
*s
)
325 unsigned long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
328 * only send a ServerKeyExchange if DH or fortezza but we have a
329 * sign only certificate PSK: may send PSK identity hints For
330 * ECC ciphersuites, we send a serverKeyExchange message only if
331 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
332 * the server certificate contains the server's public key for
335 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
337 * PSK: send ServerKeyExchange if PSK identity hint if
340 #ifndef OPENSSL_NO_PSK
341 /* Only send SKE if we have identity hint for plain PSK */
342 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
343 && s
->cert
->psk_identity_hint
)
344 /* For other PSK always send SKE */
345 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
347 #ifndef OPENSSL_NO_SRP
348 /* SRP: send ServerKeyExchange */
349 || (alg_k
& SSL_kSRP
)
359 * Should we send a CertificateRequest message?
361 * Valid return values are:
365 int send_certificate_request(SSL_CONNECTION
*s
)
368 /* don't request cert unless asked for it: */
369 s
->verify_mode
& SSL_VERIFY_PEER
371 * don't request if post-handshake-only unless doing
372 * post-handshake in TLSv1.3:
374 && (!SSL_CONNECTION_IS_TLS13(s
)
375 || !(s
->verify_mode
& SSL_VERIFY_POST_HANDSHAKE
)
376 || s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
)
378 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
381 && (s
->certreqs_sent
< 1 ||
382 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
384 * never request cert in anonymous ciphersuites (see
385 * section "Certificate request" in SSL 3 drafts and in
388 && (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
390 * ... except when the application insists on
391 * verification (against the specs, but statem_clnt.c accepts
394 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
395 /* don't request certificate for SRP auth */
396 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
398 * With normal PSK Certificates and Certificate Requests
401 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
409 * ossl_statem_server13_write_transition() works out what handshake state to
410 * move to next when a TLSv1.3 server is writing messages to be sent to the
413 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL_CONNECTION
*s
)
415 OSSL_STATEM
*st
= &s
->statem
;
418 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
419 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
422 switch (st
->hand_state
) {
424 /* Shouldn't happen */
425 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
426 return WRITE_TRAN_ERROR
;
429 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
430 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
431 return WRITE_TRAN_CONTINUE
;
433 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
434 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
435 return WRITE_TRAN_CONTINUE
;
437 if (s
->ext
.extra_tickets_expected
> 0) {
438 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
439 return WRITE_TRAN_CONTINUE
;
441 /* Try to read from the client instead */
442 return WRITE_TRAN_FINISHED
;
444 case TLS_ST_SR_CLNT_HELLO
:
445 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
446 return WRITE_TRAN_CONTINUE
;
448 case TLS_ST_SW_SRVR_HELLO
:
449 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
450 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
451 st
->hand_state
= TLS_ST_SW_CHANGE
;
452 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
453 st
->hand_state
= TLS_ST_EARLY_DATA
;
455 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
456 return WRITE_TRAN_CONTINUE
;
458 case TLS_ST_SW_CHANGE
:
459 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
460 st
->hand_state
= TLS_ST_EARLY_DATA
;
462 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
463 return WRITE_TRAN_CONTINUE
;
465 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
467 st
->hand_state
= TLS_ST_SW_FINISHED
;
468 else if (send_certificate_request(s
))
469 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
471 st
->hand_state
= TLS_ST_SW_CERT
;
473 return WRITE_TRAN_CONTINUE
;
475 case TLS_ST_SW_CERT_REQ
:
476 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
477 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
478 st
->hand_state
= TLS_ST_OK
;
480 st
->hand_state
= TLS_ST_SW_CERT
;
482 return WRITE_TRAN_CONTINUE
;
485 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
486 return WRITE_TRAN_CONTINUE
;
488 case TLS_ST_SW_CERT_VRFY
:
489 st
->hand_state
= TLS_ST_SW_FINISHED
;
490 return WRITE_TRAN_CONTINUE
;
492 case TLS_ST_SW_FINISHED
:
493 st
->hand_state
= TLS_ST_EARLY_DATA
;
494 return WRITE_TRAN_CONTINUE
;
496 case TLS_ST_EARLY_DATA
:
497 return WRITE_TRAN_FINISHED
;
499 case TLS_ST_SR_FINISHED
:
501 * Technically we have finished the handshake at this point, but we're
502 * going to remain "in_init" for now and write out any session tickets
505 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
506 s
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
;
507 } else if (!s
->ext
.ticket_expected
) {
509 * If we're not going to renew the ticket then we just finish the
510 * handshake at this point.
512 st
->hand_state
= TLS_ST_OK
;
513 return WRITE_TRAN_CONTINUE
;
515 if (s
->num_tickets
> s
->sent_tickets
)
516 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
518 st
->hand_state
= TLS_ST_OK
;
519 return WRITE_TRAN_CONTINUE
;
521 case TLS_ST_SR_KEY_UPDATE
:
522 case TLS_ST_SW_KEY_UPDATE
:
523 st
->hand_state
= TLS_ST_OK
;
524 return WRITE_TRAN_CONTINUE
;
526 case TLS_ST_SW_SESSION_TICKET
:
527 /* In a resumption we only ever send a maximum of one new ticket.
528 * Following an initial handshake we send the number of tickets we have
529 * been configured for.
531 if (!SSL_IS_FIRST_HANDSHAKE(s
) && s
->ext
.extra_tickets_expected
> 0) {
532 return WRITE_TRAN_CONTINUE
;
533 } else if (s
->hit
|| s
->num_tickets
<= s
->sent_tickets
) {
534 /* We've written enough tickets out. */
535 st
->hand_state
= TLS_ST_OK
;
537 return WRITE_TRAN_CONTINUE
;
542 * ossl_statem_server_write_transition() works out what handshake state to move
543 * to next when the server is writing messages to be sent to the client.
545 WRITE_TRAN
ossl_statem_server_write_transition(SSL_CONNECTION
*s
)
547 OSSL_STATEM
*st
= &s
->statem
;
550 * Note that before the ClientHello we don't know what version we are going
551 * to negotiate yet, so we don't take this branch until later
554 if (SSL_CONNECTION_IS_TLS13(s
))
555 return ossl_statem_server13_write_transition(s
);
557 switch (st
->hand_state
) {
559 /* Shouldn't happen */
560 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
561 return WRITE_TRAN_ERROR
;
564 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
565 /* We must be trying to renegotiate */
566 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
567 st
->request_state
= TLS_ST_BEFORE
;
568 return WRITE_TRAN_CONTINUE
;
570 /* Must be an incoming ClientHello */
571 if (!tls_setup_handshake(s
)) {
572 /* SSLfatal() already called */
573 return WRITE_TRAN_ERROR
;
578 /* Just go straight to trying to read from the client */
579 return WRITE_TRAN_FINISHED
;
581 case TLS_ST_SW_HELLO_REQ
:
582 st
->hand_state
= TLS_ST_OK
;
583 return WRITE_TRAN_CONTINUE
;
585 case TLS_ST_SR_CLNT_HELLO
:
586 if (SSL_CONNECTION_IS_DTLS(s
) && !s
->d1
->cookie_verified
587 && (SSL_get_options(SSL_CONNECTION_GET_SSL(s
)) & SSL_OP_COOKIE_EXCHANGE
)) {
588 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
589 } else if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
590 /* We must have rejected the renegotiation */
591 st
->hand_state
= TLS_ST_OK
;
592 return WRITE_TRAN_CONTINUE
;
594 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
596 return WRITE_TRAN_CONTINUE
;
598 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
599 return WRITE_TRAN_FINISHED
;
601 case TLS_ST_SW_SRVR_HELLO
:
603 if (s
->ext
.ticket_expected
)
604 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
606 st
->hand_state
= TLS_ST_SW_CHANGE
;
608 /* Check if it is anon DH or anon ECDH, */
609 /* normal PSK or SRP */
610 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
&
611 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
612 st
->hand_state
= TLS_ST_SW_CERT
;
613 } else if (send_server_key_exchange(s
)) {
614 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
615 } else if (send_certificate_request(s
)) {
616 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
618 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
621 return WRITE_TRAN_CONTINUE
;
624 if (s
->ext
.status_expected
) {
625 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
626 return WRITE_TRAN_CONTINUE
;
630 case TLS_ST_SW_CERT_STATUS
:
631 if (send_server_key_exchange(s
)) {
632 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
633 return WRITE_TRAN_CONTINUE
;
637 case TLS_ST_SW_KEY_EXCH
:
638 if (send_certificate_request(s
)) {
639 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
640 return WRITE_TRAN_CONTINUE
;
644 case TLS_ST_SW_CERT_REQ
:
645 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
646 return WRITE_TRAN_CONTINUE
;
648 case TLS_ST_SW_SRVR_DONE
:
649 return WRITE_TRAN_FINISHED
;
651 case TLS_ST_SR_FINISHED
:
653 st
->hand_state
= TLS_ST_OK
;
654 return WRITE_TRAN_CONTINUE
;
655 } else if (s
->ext
.ticket_expected
) {
656 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
658 st
->hand_state
= TLS_ST_SW_CHANGE
;
660 return WRITE_TRAN_CONTINUE
;
662 case TLS_ST_SW_SESSION_TICKET
:
663 st
->hand_state
= TLS_ST_SW_CHANGE
;
664 return WRITE_TRAN_CONTINUE
;
666 case TLS_ST_SW_CHANGE
:
667 st
->hand_state
= TLS_ST_SW_FINISHED
;
668 return WRITE_TRAN_CONTINUE
;
670 case TLS_ST_SW_FINISHED
:
672 return WRITE_TRAN_FINISHED
;
674 st
->hand_state
= TLS_ST_OK
;
675 return WRITE_TRAN_CONTINUE
;
680 * Perform any pre work that needs to be done prior to sending a message from
681 * the server to the client.
683 WORK_STATE
ossl_statem_server_pre_work(SSL_CONNECTION
*s
, WORK_STATE wst
)
685 OSSL_STATEM
*st
= &s
->statem
;
686 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
688 switch (st
->hand_state
) {
690 /* No pre work to be done */
693 case TLS_ST_SW_HELLO_REQ
:
695 if (SSL_CONNECTION_IS_DTLS(s
))
696 dtls1_clear_sent_buffer(s
);
699 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
701 if (SSL_CONNECTION_IS_DTLS(s
)) {
702 dtls1_clear_sent_buffer(s
);
703 /* We don't buffer this message so don't use the timer */
708 case TLS_ST_SW_SRVR_HELLO
:
709 if (SSL_CONNECTION_IS_DTLS(s
)) {
711 * Messages we write from now on should be buffered and
712 * retransmitted if necessary, so we need to use the timer now
718 case TLS_ST_SW_SRVR_DONE
:
719 #ifndef OPENSSL_NO_SCTP
720 if (SSL_CONNECTION_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(ssl
))) {
721 /* Calls SSLfatal() as required */
722 return dtls_wait_for_dry(s
);
725 return WORK_FINISHED_CONTINUE
;
727 case TLS_ST_SW_SESSION_TICKET
:
728 if (SSL_CONNECTION_IS_TLS13(s
) && s
->sent_tickets
== 0
729 && s
->ext
.extra_tickets_expected
== 0) {
731 * Actually this is the end of the handshake, but we're going
732 * straight into writing the session ticket out. So we finish off
733 * the handshake, but keep the various buffers active.
735 * Calls SSLfatal as required.
737 return tls_finish_handshake(s
, wst
, 0, 0);
739 if (SSL_CONNECTION_IS_DTLS(s
)) {
741 * We're into the last flight. We don't retransmit the last flight
742 * unless we need to, so we don't use the timer
748 case TLS_ST_SW_CHANGE
:
749 if (SSL_CONNECTION_IS_TLS13(s
))
751 /* Writes to s->session are only safe for initial handshakes */
752 if (s
->session
->cipher
== NULL
) {
753 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
754 } else if (s
->session
->cipher
!= s
->s3
.tmp
.new_cipher
) {
755 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
758 if (!ssl
->method
->ssl3_enc
->setup_key_block(s
)) {
759 /* SSLfatal() already called */
762 if (SSL_CONNECTION_IS_DTLS(s
)) {
764 * We're into the last flight. We don't retransmit the last flight
765 * unless we need to, so we don't use the timer. This might have
766 * already been set to 0 if we sent a NewSessionTicket message,
767 * but we'll set it again here in case we didn't.
771 return WORK_FINISHED_CONTINUE
;
773 case TLS_ST_EARLY_DATA
:
774 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
775 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0)
776 return WORK_FINISHED_CONTINUE
;
780 /* Calls SSLfatal() as required */
781 return tls_finish_handshake(s
, wst
, 1, 1);
784 return WORK_FINISHED_CONTINUE
;
787 static ossl_inline
int conn_is_closed(void)
789 switch (get_last_sys_error()) {
794 #if defined(ECONNRESET)
798 #if defined(WSAECONNRESET)
808 * Perform any work that needs to be done after sending a message from the
809 * server to the client.
811 WORK_STATE
ossl_statem_server_post_work(SSL_CONNECTION
*s
, WORK_STATE wst
)
813 OSSL_STATEM
*st
= &s
->statem
;
814 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
818 switch (st
->hand_state
) {
820 /* No post work to be done */
823 case TLS_ST_SW_HELLO_REQ
:
824 if (statem_flush(s
) != 1)
826 if (!ssl3_init_finished_mac(s
)) {
827 /* SSLfatal() already called */
832 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
833 if (statem_flush(s
) != 1)
835 /* HelloVerifyRequest resets Finished MAC */
836 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
837 /* SSLfatal() already called */
841 * The next message should be another ClientHello which we need to
842 * treat like it was the first packet
847 case TLS_ST_SW_SRVR_HELLO
:
848 if (SSL_CONNECTION_IS_TLS13(s
)
849 && s
->hello_retry_request
== SSL_HRR_PENDING
) {
850 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
851 && statem_flush(s
) != 1)
855 #ifndef OPENSSL_NO_SCTP
856 if (SSL_CONNECTION_IS_DTLS(s
) && s
->hit
) {
857 unsigned char sctpauthkey
[64];
858 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
862 * Add new shared key for SCTP-Auth, will be ignored if no
865 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
866 sizeof(DTLS1_SCTP_AUTH_LABEL
));
868 /* Don't include the terminating zero. */
869 labellen
= sizeof(labelbuffer
) - 1;
870 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
873 if (SSL_export_keying_material(ssl
, sctpauthkey
,
874 sizeof(sctpauthkey
), labelbuffer
,
877 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
881 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
882 sizeof(sctpauthkey
), sctpauthkey
);
885 if (!SSL_CONNECTION_IS_TLS13(s
)
886 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
887 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
891 case TLS_ST_SW_CHANGE
:
892 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
893 if (!statem_flush(s
))
898 if (SSL_CONNECTION_IS_TLS13(s
)) {
899 if (!ssl
->method
->ssl3_enc
->setup_key_block(s
)
900 || !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
901 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
902 /* SSLfatal() already called */
906 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
907 && !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
908 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
909 /* SSLfatal() already called */
913 * We don't yet know whether the next record we are going to receive
914 * is an unencrypted alert, an encrypted alert, or an encrypted
915 * handshake message. We temporarily tolerate unencrypted alerts.
917 if (s
->rlayer
.rrlmethod
->set_plain_alerts
!= NULL
)
918 s
->rlayer
.rrlmethod
->set_plain_alerts(s
->rlayer
.rrl
, 1);
922 #ifndef OPENSSL_NO_SCTP
923 if (SSL_CONNECTION_IS_DTLS(s
) && !s
->hit
) {
925 * Change to new shared key of SCTP-Auth, will be ignored if
928 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
932 if (!ssl
->method
->ssl3_enc
->change_cipher_state(s
,
933 SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
934 /* SSLfatal() already called */
938 if (SSL_CONNECTION_IS_DTLS(s
))
939 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
942 case TLS_ST_SW_SRVR_DONE
:
943 if (statem_flush(s
) != 1)
947 case TLS_ST_SW_FINISHED
:
948 if (statem_flush(s
) != 1)
950 #ifndef OPENSSL_NO_SCTP
951 if (SSL_CONNECTION_IS_DTLS(s
) && s
->hit
) {
953 * Change to new shared key of SCTP-Auth, will be ignored if
956 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
960 if (SSL_CONNECTION_IS_TLS13(s
)) {
961 /* TLS 1.3 gets the secret size from the handshake md */
963 if (!ssl
->method
->ssl3_enc
->generate_master_secret(s
,
964 s
->master_secret
, s
->handshake_secret
, 0,
966 || !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
967 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
968 /* SSLfatal() already called */
973 case TLS_ST_SW_CERT_REQ
:
974 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
975 if (statem_flush(s
) != 1)
980 case TLS_ST_SW_KEY_UPDATE
:
981 if (statem_flush(s
) != 1)
983 if (!tls13_update_key(s
, 1)) {
984 /* SSLfatal() already called */
989 case TLS_ST_SW_SESSION_TICKET
:
991 if (SSL_CONNECTION_IS_TLS13(s
) && statem_flush(s
) != 1) {
992 if (SSL_get_error(ssl
, 0) == SSL_ERROR_SYSCALL
993 && conn_is_closed()) {
995 * We ignore connection closed errors in TLSv1.3 when sending a
996 * NewSessionTicket and behave as if we were successful. This is
997 * so that we are still able to read data sent to us by a client
998 * that closes soon after the end of the handshake without
999 * waiting to read our post-handshake NewSessionTickets.
1001 s
->rwstate
= SSL_NOTHING
;
1010 return WORK_FINISHED_CONTINUE
;
1014 * Get the message construction function and message type for sending from the
1017 * Valid return values are:
1021 int ossl_statem_server_construct_message(SSL_CONNECTION
*s
,
1022 confunc_f
*confunc
, int *mt
)
1024 OSSL_STATEM
*st
= &s
->statem
;
1026 switch (st
->hand_state
) {
1028 /* Shouldn't happen */
1029 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
1032 case TLS_ST_SW_CHANGE
:
1033 if (SSL_CONNECTION_IS_DTLS(s
))
1034 *confunc
= dtls_construct_change_cipher_spec
;
1036 *confunc
= tls_construct_change_cipher_spec
;
1037 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1040 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1041 *confunc
= dtls_construct_hello_verify_request
;
1042 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1045 case TLS_ST_SW_HELLO_REQ
:
1046 /* No construction function needed */
1048 *mt
= SSL3_MT_HELLO_REQUEST
;
1051 case TLS_ST_SW_SRVR_HELLO
:
1052 *confunc
= tls_construct_server_hello
;
1053 *mt
= SSL3_MT_SERVER_HELLO
;
1056 case TLS_ST_SW_CERT
:
1057 *confunc
= tls_construct_server_certificate
;
1058 *mt
= SSL3_MT_CERTIFICATE
;
1061 case TLS_ST_SW_CERT_VRFY
:
1062 *confunc
= tls_construct_cert_verify
;
1063 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1067 case TLS_ST_SW_KEY_EXCH
:
1068 *confunc
= tls_construct_server_key_exchange
;
1069 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1072 case TLS_ST_SW_CERT_REQ
:
1073 *confunc
= tls_construct_certificate_request
;
1074 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1077 case TLS_ST_SW_SRVR_DONE
:
1078 *confunc
= tls_construct_server_done
;
1079 *mt
= SSL3_MT_SERVER_DONE
;
1082 case TLS_ST_SW_SESSION_TICKET
:
1083 *confunc
= tls_construct_new_session_ticket
;
1084 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1087 case TLS_ST_SW_CERT_STATUS
:
1088 *confunc
= tls_construct_cert_status
;
1089 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1092 case TLS_ST_SW_FINISHED
:
1093 *confunc
= tls_construct_finished
;
1094 *mt
= SSL3_MT_FINISHED
;
1097 case TLS_ST_EARLY_DATA
:
1099 *mt
= SSL3_MT_DUMMY
;
1102 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1103 *confunc
= tls_construct_encrypted_extensions
;
1104 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1107 case TLS_ST_SW_KEY_UPDATE
:
1108 *confunc
= tls_construct_key_update
;
1109 *mt
= SSL3_MT_KEY_UPDATE
;
1117 * Maximum size (excluding the Handshake header) of a ClientHello message,
1118 * calculated as follows:
1120 * 2 + # client_version
1121 * 32 + # only valid length for random
1122 * 1 + # length of session_id
1123 * 32 + # maximum size for session_id
1124 * 2 + # length of cipher suites
1125 * 2^16-2 + # maximum length of cipher suites array
1126 * 1 + # length of compression_methods
1127 * 2^8-1 + # maximum length of compression methods
1128 * 2 + # length of extensions
1129 * 2^16-1 # maximum length of extensions
1131 #define CLIENT_HELLO_MAX_LENGTH 131396
1133 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1134 #define NEXT_PROTO_MAX_LENGTH 514
1137 * Returns the maximum allowed length for the current message that we are
1138 * reading. Excludes the message header.
1140 size_t ossl_statem_server_max_message_size(SSL_CONNECTION
*s
)
1142 OSSL_STATEM
*st
= &s
->statem
;
1144 switch (st
->hand_state
) {
1146 /* Shouldn't happen */
1149 case TLS_ST_SR_CLNT_HELLO
:
1150 return CLIENT_HELLO_MAX_LENGTH
;
1152 case TLS_ST_SR_END_OF_EARLY_DATA
:
1153 return END_OF_EARLY_DATA_MAX_LENGTH
;
1155 case TLS_ST_SR_CERT
:
1156 return s
->max_cert_list
;
1158 case TLS_ST_SR_KEY_EXCH
:
1159 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1161 case TLS_ST_SR_CERT_VRFY
:
1162 return SSL3_RT_MAX_PLAIN_LENGTH
;
1164 #ifndef OPENSSL_NO_NEXTPROTONEG
1165 case TLS_ST_SR_NEXT_PROTO
:
1166 return NEXT_PROTO_MAX_LENGTH
;
1169 case TLS_ST_SR_CHANGE
:
1170 return CCS_MAX_LENGTH
;
1172 case TLS_ST_SR_FINISHED
:
1173 return FINISHED_MAX_LENGTH
;
1175 case TLS_ST_SR_KEY_UPDATE
:
1176 return KEY_UPDATE_MAX_LENGTH
;
1181 * Process a message that the server has received from the client.
1183 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL_CONNECTION
*s
,
1186 OSSL_STATEM
*st
= &s
->statem
;
1188 switch (st
->hand_state
) {
1190 /* Shouldn't happen */
1191 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1192 return MSG_PROCESS_ERROR
;
1194 case TLS_ST_SR_CLNT_HELLO
:
1195 return tls_process_client_hello(s
, pkt
);
1197 case TLS_ST_SR_END_OF_EARLY_DATA
:
1198 return tls_process_end_of_early_data(s
, pkt
);
1200 case TLS_ST_SR_CERT
:
1201 return tls_process_client_certificate(s
, pkt
);
1203 case TLS_ST_SR_KEY_EXCH
:
1204 return tls_process_client_key_exchange(s
, pkt
);
1206 case TLS_ST_SR_CERT_VRFY
:
1207 return tls_process_cert_verify(s
, pkt
);
1209 #ifndef OPENSSL_NO_NEXTPROTONEG
1210 case TLS_ST_SR_NEXT_PROTO
:
1211 return tls_process_next_proto(s
, pkt
);
1214 case TLS_ST_SR_CHANGE
:
1215 return tls_process_change_cipher_spec(s
, pkt
);
1217 case TLS_ST_SR_FINISHED
:
1218 return tls_process_finished(s
, pkt
);
1220 case TLS_ST_SR_KEY_UPDATE
:
1221 return tls_process_key_update(s
, pkt
);
1227 * Perform any further processing required following the receipt of a message
1230 WORK_STATE
ossl_statem_server_post_process_message(SSL_CONNECTION
*s
,
1233 OSSL_STATEM
*st
= &s
->statem
;
1235 switch (st
->hand_state
) {
1237 /* Shouldn't happen */
1238 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1241 case TLS_ST_SR_CLNT_HELLO
:
1242 return tls_post_process_client_hello(s
, wst
);
1244 case TLS_ST_SR_KEY_EXCH
:
1245 return tls_post_process_client_key_exchange(s
, wst
);
1249 #ifndef OPENSSL_NO_SRP
1250 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1251 static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION
*s
)
1254 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1256 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1257 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1258 if (s
->srp_ctx
.login
== NULL
) {
1260 * RFC 5054 says SHOULD reject, we do so if There is no srp
1263 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1264 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1267 ret
= ssl_srp_server_param_with_username_intern(s
, &al
);
1270 if (ret
== SSL3_AL_FATAL
) {
1272 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1273 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1274 : SSL_R_CLIENTHELLO_TLSEXT
);
1283 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1286 /* Always use DTLS 1.0 version: see RFC 6347 */
1287 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1288 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1294 int dtls_construct_hello_verify_request(SSL_CONNECTION
*s
, WPACKET
*pkt
)
1296 unsigned int cookie_leni
;
1297 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1299 if (sctx
->app_gen_cookie_cb
== NULL
1300 || sctx
->app_gen_cookie_cb(SSL_CONNECTION_GET_SSL(s
), s
->d1
->cookie
,
1302 || cookie_leni
> DTLS1_COOKIE_LENGTH
) {
1303 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1306 s
->d1
->cookie_len
= cookie_leni
;
1308 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1309 s
->d1
->cookie_len
)) {
1310 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
1318 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1319 * SecureTransport using the TLS extension block in |hello|.
1320 * Safari, since 10.6, sends exactly these extensions, in this order:
1324 * signature_algorithms (for TLSv1.2 only)
1326 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1327 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1328 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1329 * 10.8..10.8.3 (which don't work).
1331 static void ssl_check_for_safari(SSL_CONNECTION
*s
,
1332 const CLIENTHELLO_MSG
*hello
)
1334 static const unsigned char kSafariExtensionsBlock
[] = {
1335 0x00, 0x0a, /* elliptic_curves extension */
1336 0x00, 0x08, /* 8 bytes */
1337 0x00, 0x06, /* 6 bytes of curve ids */
1338 0x00, 0x17, /* P-256 */
1339 0x00, 0x18, /* P-384 */
1340 0x00, 0x19, /* P-521 */
1342 0x00, 0x0b, /* ec_point_formats */
1343 0x00, 0x02, /* 2 bytes */
1344 0x01, /* 1 point format */
1345 0x00, /* uncompressed */
1346 /* The following is only present in TLS 1.2 */
1347 0x00, 0x0d, /* signature_algorithms */
1348 0x00, 0x0c, /* 12 bytes */
1349 0x00, 0x0a, /* 10 bytes */
1350 0x05, 0x01, /* SHA-384/RSA */
1351 0x04, 0x01, /* SHA-256/RSA */
1352 0x02, 0x01, /* SHA-1/RSA */
1353 0x04, 0x03, /* SHA-256/ECDSA */
1354 0x02, 0x03, /* SHA-1/ECDSA */
1356 /* Length of the common prefix (first two extensions). */
1357 static const size_t kSafariCommonExtensionsLength
= 18;
1362 tmppkt
= hello
->extensions
;
1364 if (!PACKET_forward(&tmppkt
, 2)
1365 || !PACKET_get_net_2(&tmppkt
, &type
)
1366 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1370 if (type
!= TLSEXT_TYPE_server_name
)
1373 ext_len
= TLS1_get_client_version(
1374 SSL_CONNECTION_GET_SSL(s
)) >= TLS1_2_VERSION
?
1375 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1377 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1381 #define RENEG_OPTIONS_OK(options) \
1382 ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
1383 && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
1385 MSG_PROCESS_RETURN
tls_process_client_hello(SSL_CONNECTION
*s
, PACKET
*pkt
)
1387 /* |cookie| will only be initialized for DTLS. */
1388 PACKET session_id
, compression
, extensions
, cookie
;
1389 static const unsigned char null_compression
= 0;
1390 CLIENTHELLO_MSG
*clienthello
= NULL
;
1392 /* Check if this is actually an unexpected renegotiation ClientHello */
1393 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1394 if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s
))) {
1395 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1398 if (!RENEG_OPTIONS_OK(s
->options
)
1399 || (!s
->s3
.send_connection_binding
1401 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1402 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1403 return MSG_PROCESS_FINISHED_READING
;
1409 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1410 if (clienthello
== NULL
) {
1411 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1416 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1418 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1419 PACKET_null_init(&cookie
);
1421 if (clienthello
->isv2
) {
1424 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1425 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1426 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
1431 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1432 * header is sent directly on the wire, not wrapped as a TLS
1433 * record. Our record layer just processes the message length and passes
1434 * the rest right through. Its format is:
1436 * 0-1 msg_length - decoded by the record layer
1437 * 2 msg_type - s->init_msg points here
1439 * 5-6 cipher_spec_length
1440 * 7-8 session_id_length
1441 * 9-10 challenge_length
1445 if (!PACKET_get_1(pkt
, &mt
)
1446 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1448 * Should never happen. We should have tested this in the record
1449 * layer in order to have determined that this is a SSLv2 record
1450 * in the first place
1452 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1457 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1458 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_TOO_SHORT
);
1462 /* Parse the message and load client random. */
1463 if (clienthello
->isv2
) {
1465 * Handle an SSLv2 backwards compatible ClientHello
1466 * Note, this is only for SSLv3+ using the backward compatible format.
1467 * Real SSLv2 is not supported, and is rejected below.
1469 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1472 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1473 || !PACKET_get_net_2(pkt
, &session_id_len
)
1474 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1475 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RECORD_LENGTH_MISMATCH
);
1479 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1480 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_LENGTH_MISMATCH
);
1484 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1486 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1487 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1488 /* No extensions. */
1489 || PACKET_remaining(pkt
) != 0) {
1490 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RECORD_LENGTH_MISMATCH
);
1493 clienthello
->session_id_len
= session_id_len
;
1495 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1496 * here rather than sizeof(clienthello->random) because that is the limit
1497 * for SSLv3 and it is fixed. It won't change even if
1498 * sizeof(clienthello->random) does.
1500 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1501 ? SSL3_RANDOM_SIZE
: challenge_len
;
1502 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1503 if (!PACKET_copy_bytes(&challenge
,
1504 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1505 challenge_len
, challenge_len
)
1506 /* Advertise only null compression. */
1507 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1508 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1512 PACKET_null_init(&clienthello
->extensions
);
1514 /* Regular ClientHello. */
1515 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1516 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1517 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1518 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1519 &clienthello
->session_id_len
)) {
1520 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1524 if (SSL_CONNECTION_IS_DTLS(s
)) {
1525 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1526 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1529 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1530 DTLS1_COOKIE_LENGTH
,
1531 &clienthello
->dtls_cookie_len
)) {
1532 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1536 * If we require cookies and this ClientHello doesn't contain one,
1537 * just return since we do not want to allocate any memory yet.
1538 * So check cookie length...
1540 if (SSL_get_options(SSL_CONNECTION_GET_SSL(s
)) & SSL_OP_COOKIE_EXCHANGE
) {
1541 if (clienthello
->dtls_cookie_len
== 0) {
1542 OPENSSL_free(clienthello
);
1543 return MSG_PROCESS_FINISHED_READING
;
1548 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1549 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1553 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1554 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1558 /* Could be empty. */
1559 if (PACKET_remaining(pkt
) == 0) {
1560 PACKET_null_init(&clienthello
->extensions
);
1562 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1563 || PACKET_remaining(pkt
) != 0) {
1564 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1570 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1571 MAX_COMPRESSIONS_SIZE
,
1572 &clienthello
->compressions_len
)) {
1573 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1577 /* Preserve the raw extensions PACKET for later use */
1578 extensions
= clienthello
->extensions
;
1579 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1580 &clienthello
->pre_proc_exts
,
1581 &clienthello
->pre_proc_exts_len
, 1)) {
1582 /* SSLfatal already been called */
1585 s
->clienthello
= clienthello
;
1587 return MSG_PROCESS_CONTINUE_PROCESSING
;
1590 if (clienthello
!= NULL
)
1591 OPENSSL_free(clienthello
->pre_proc_exts
);
1592 OPENSSL_free(clienthello
);
1594 return MSG_PROCESS_ERROR
;
1597 static int tls_early_post_process_client_hello(SSL_CONNECTION
*s
)
1600 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1604 #ifndef OPENSSL_NO_COMP
1605 SSL_COMP
*comp
= NULL
;
1607 const SSL_CIPHER
*c
;
1608 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1609 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1610 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1611 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1612 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1613 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1615 /* Finished parsing the ClientHello, now we can start processing it */
1616 /* Give the ClientHello callback a crack at things */
1617 if (sctx
->client_hello_cb
!= NULL
) {
1618 /* A failure in the ClientHello callback terminates the connection. */
1619 switch (sctx
->client_hello_cb(ssl
, &al
, sctx
->client_hello_cb_arg
)) {
1620 case SSL_CLIENT_HELLO_SUCCESS
:
1622 case SSL_CLIENT_HELLO_RETRY
:
1623 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1625 case SSL_CLIENT_HELLO_ERROR
:
1627 SSLfatal(s
, al
, SSL_R_CALLBACK_FAILED
);
1632 /* Set up the client_random */
1633 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1635 /* Choose the version */
1637 if (clienthello
->isv2
) {
1638 if (clienthello
->legacy_version
== SSL2_VERSION
1639 || (clienthello
->legacy_version
& 0xff00)
1640 != (SSL3_VERSION_MAJOR
<< 8)) {
1642 * This is real SSLv2 or something completely unknown. We don't
1645 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_UNKNOWN_PROTOCOL
);
1649 s
->client_version
= clienthello
->legacy_version
;
1652 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1653 * versions are potentially compatible. Version negotiation comes later.
1655 if (!SSL_CONNECTION_IS_DTLS(s
)) {
1656 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1657 } else if (ssl
->method
->version
!= DTLS_ANY_VERSION
&&
1658 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1659 protverr
= SSL_R_VERSION_TOO_LOW
;
1665 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1666 /* like ssl3_get_record, send alert using remote version number */
1667 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1669 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, protverr
);
1673 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1674 if (SSL_CONNECTION_IS_TLS13(s
)
1675 && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1676 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
1680 if (SSL_CONNECTION_IS_DTLS(s
)) {
1681 /* Empty cookie was already handled above by returning early. */
1682 if (SSL_get_options(ssl
) & SSL_OP_COOKIE_EXCHANGE
) {
1683 if (sctx
->app_verify_cookie_cb
!= NULL
) {
1684 if (sctx
->app_verify_cookie_cb(ssl
, clienthello
->dtls_cookie
,
1685 clienthello
->dtls_cookie_len
) == 0) {
1686 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1687 SSL_R_COOKIE_MISMATCH
);
1689 /* else cookie verification succeeded */
1691 /* default verification */
1692 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1693 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1694 s
->d1
->cookie_len
) != 0) {
1695 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_COOKIE_MISMATCH
);
1698 s
->d1
->cookie_verified
= 1;
1700 if (ssl
->method
->version
== DTLS_ANY_VERSION
) {
1701 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1702 if (protverr
!= 0) {
1703 s
->version
= s
->client_version
;
1704 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, protverr
);
1712 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1713 clienthello
->isv2
) ||
1714 !ossl_bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
,
1715 &scsvs
, clienthello
->isv2
, 1)) {
1716 /* SSLfatal() already called */
1720 s
->s3
.send_connection_binding
= 0;
1721 /* Check what signalling cipher-suite values were received. */
1722 if (scsvs
!= NULL
) {
1723 for (i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1724 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1725 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1726 if (s
->renegotiate
) {
1727 /* SCSV is fatal if renegotiating */
1728 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1729 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1732 s
->s3
.send_connection_binding
= 1;
1733 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1734 !ssl_check_version_downgrade(s
)) {
1736 * This SCSV indicates that the client previously tried
1737 * a higher version. We should fail if the current version
1738 * is an unexpected downgrade, as that indicates that the first
1739 * connection may have been tampered with in order to trigger
1740 * an insecure downgrade.
1742 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1743 SSL_R_INAPPROPRIATE_FALLBACK
);
1749 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1750 if (SSL_CONNECTION_IS_TLS13(s
)) {
1751 const SSL_CIPHER
*cipher
=
1752 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(ssl
));
1754 if (cipher
== NULL
) {
1755 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_SHARED_CIPHER
);
1758 if (s
->hello_retry_request
== SSL_HRR_PENDING
1759 && (s
->s3
.tmp
.new_cipher
== NULL
1760 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1762 * A previous HRR picked a different ciphersuite to the one we
1763 * just selected. Something must have changed.
1765 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_CIPHER
);
1768 s
->s3
.tmp
.new_cipher
= cipher
;
1771 /* We need to do this before getting the session */
1772 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1773 SSL_EXT_CLIENT_HELLO
,
1774 clienthello
->pre_proc_exts
, NULL
, 0)) {
1775 /* SSLfatal() already called */
1780 * We don't allow resumption in a backwards compatible ClientHello.
1781 * In TLS1.1+, session_id MUST be empty.
1783 * Versions before 0.9.7 always allow clients to resume sessions in
1784 * renegotiation. 0.9.7 and later allow this by default, but optionally
1785 * ignore resumption requests with flag
1786 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1787 * than a change to default behavior so that applications relying on
1788 * this for security won't even compile against older library versions).
1789 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1790 * request renegotiation but not a new session (s->new_session remains
1791 * unset): for servers, this essentially just means that the
1792 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1795 if (clienthello
->isv2
||
1797 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1798 if (!ssl_get_new_session(s
, 1)) {
1799 /* SSLfatal() already called */
1803 i
= ssl_get_prev_session(s
, clienthello
);
1805 /* previous session */
1807 } else if (i
== -1) {
1808 /* SSLfatal() already called */
1812 if (!ssl_get_new_session(s
, 1)) {
1813 /* SSLfatal() already called */
1819 if (SSL_CONNECTION_IS_TLS13(s
)) {
1820 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1821 s
->clienthello
->session_id_len
);
1822 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1826 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1827 * ciphersuite compatibility with the session as part of resumption.
1829 if (!SSL_CONNECTION_IS_TLS13(s
) && s
->hit
) {
1831 id
= s
->session
->cipher
->id
;
1833 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1834 BIO_printf(trc_out
, "client sent %d ciphers\n",
1835 sk_SSL_CIPHER_num(ciphers
));
1837 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1838 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1839 if (trc_out
!= NULL
)
1840 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1841 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1849 * we need to have the cipher in the cipher list if we are asked
1852 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1853 SSL_R_REQUIRED_CIPHER_MISSING
);
1854 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1857 OSSL_TRACE_END(TLS_CIPHER
);
1860 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1861 if (clienthello
->compressions
[loop
] == 0)
1865 if (loop
>= clienthello
->compressions_len
) {
1867 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1871 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1872 ssl_check_for_safari(s
, clienthello
);
1874 /* TLS extensions */
1875 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1876 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1877 /* SSLfatal() already called */
1882 * Check if we want to use external pre-shared secret for this handshake
1883 * for not reused session only. We need to generate server_random before
1884 * calling tls_session_secret_cb in order to allow SessionTicket
1885 * processing to use it in key derivation.
1889 pos
= s
->s3
.server_random
;
1890 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1891 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1897 && s
->version
>= TLS1_VERSION
1898 && !SSL_CONNECTION_IS_TLS13(s
)
1899 && !SSL_CONNECTION_IS_DTLS(s
)
1900 && s
->ext
.session_secret_cb
!= NULL
) {
1901 const SSL_CIPHER
*pref_cipher
= NULL
;
1903 * s->session->master_key_length is a size_t, but this is an int for
1904 * backwards compat reasons
1906 int master_key_length
;
1908 master_key_length
= sizeof(s
->session
->master_key
);
1909 if (s
->ext
.session_secret_cb(ssl
, s
->session
->master_key
,
1910 &master_key_length
, ciphers
,
1912 s
->ext
.session_secret_cb_arg
)
1913 && master_key_length
> 0) {
1914 s
->session
->master_key_length
= master_key_length
;
1916 s
->peer_ciphers
= ciphers
;
1917 s
->session
->verify_result
= X509_V_OK
;
1921 /* check if some cipher was preferred by call back */
1922 if (pref_cipher
== NULL
)
1923 pref_cipher
= ssl3_choose_cipher(s
, s
->peer_ciphers
,
1924 SSL_get_ciphers(ssl
));
1925 if (pref_cipher
== NULL
) {
1926 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_SHARED_CIPHER
);
1930 s
->session
->cipher
= pref_cipher
;
1931 sk_SSL_CIPHER_free(s
->cipher_list
);
1932 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1933 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1934 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1939 * Worst case, we will use the NULL compression, but if we have other
1940 * options, we will now look for them. We have complen-1 compression
1941 * algorithms from the client, starting at q.
1943 s
->s3
.tmp
.new_compression
= NULL
;
1944 if (SSL_CONNECTION_IS_TLS13(s
)) {
1946 * We already checked above that the NULL compression method appears in
1947 * the list. Now we check there aren't any others (which is illegal in
1948 * a TLSv1.3 ClientHello.
1950 if (clienthello
->compressions_len
!= 1) {
1951 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1952 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1956 #ifndef OPENSSL_NO_COMP
1957 /* This only happens if we have a cache hit */
1958 else if (s
->session
->compress_meth
!= 0) {
1959 int m
, comp_id
= s
->session
->compress_meth
;
1961 /* Perform sanity checks on resumed compression algorithm */
1962 /* Can't disable compression */
1963 if (!ssl_allow_compression(s
)) {
1964 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1965 SSL_R_INCONSISTENT_COMPRESSION
);
1968 /* Look for resumed compression method */
1969 for (m
= 0; m
< sk_SSL_COMP_num(sctx
->comp_methods
); m
++) {
1970 comp
= sk_SSL_COMP_value(sctx
->comp_methods
, m
);
1971 if (comp_id
== comp
->id
) {
1972 s
->s3
.tmp
.new_compression
= comp
;
1976 if (s
->s3
.tmp
.new_compression
== NULL
) {
1977 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1978 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1981 /* Look for resumed method in compression list */
1982 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
1983 if (clienthello
->compressions
[k
] == comp_id
)
1986 if (k
>= clienthello
->compressions_len
) {
1987 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1988 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
1991 } else if (s
->hit
) {
1993 } else if (ssl_allow_compression(s
) && sctx
->comp_methods
) {
1994 /* See if we have a match */
1995 int m
, nn
, v
, done
= 0;
1998 nn
= sk_SSL_COMP_num(sctx
->comp_methods
);
1999 for (m
= 0; m
< nn
; m
++) {
2000 comp
= sk_SSL_COMP_value(sctx
->comp_methods
, m
);
2002 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2003 if (v
== clienthello
->compressions
[o
]) {
2012 s
->s3
.tmp
.new_compression
= comp
;
2018 * If compression is disabled we'd better not try to resume a session
2019 * using compression.
2021 if (s
->session
->compress_meth
!= 0) {
2022 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_INCONSISTENT_COMPRESSION
);
2028 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2031 if (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
)) {
2032 sk_SSL_CIPHER_free(s
->peer_ciphers
);
2033 s
->peer_ciphers
= ciphers
;
2034 if (ciphers
== NULL
) {
2035 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2042 #ifdef OPENSSL_NO_COMP
2043 s
->session
->compress_meth
= 0;
2045 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2047 if (!tls1_set_server_sigalgs(s
)) {
2048 /* SSLfatal() already called */
2053 sk_SSL_CIPHER_free(ciphers
);
2054 sk_SSL_CIPHER_free(scsvs
);
2055 OPENSSL_free(clienthello
->pre_proc_exts
);
2056 OPENSSL_free(s
->clienthello
);
2057 s
->clienthello
= NULL
;
2060 sk_SSL_CIPHER_free(ciphers
);
2061 sk_SSL_CIPHER_free(scsvs
);
2062 OPENSSL_free(clienthello
->pre_proc_exts
);
2063 OPENSSL_free(s
->clienthello
);
2064 s
->clienthello
= NULL
;
2070 * Call the status request callback if needed. Upon success, returns 1.
2071 * Upon failure, returns 0.
2073 static int tls_handle_status_request(SSL_CONNECTION
*s
)
2075 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2077 s
->ext
.status_expected
= 0;
2080 * If status request then ask callback what to do. Note: this must be
2081 * called after servername callbacks in case the certificate has changed,
2082 * and must be called after the cipher has been chosen because this may
2083 * influence which certificate is sent
2085 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& sctx
!= NULL
2086 && sctx
->ext
.status_cb
!= NULL
) {
2089 /* If no certificate can't return certificate status */
2090 if (s
->s3
.tmp
.cert
!= NULL
) {
2092 * Set current certificate to one we will use so SSL_get_certificate
2093 * et al can pick it up.
2095 s
->cert
->key
= s
->s3
.tmp
.cert
;
2096 ret
= sctx
->ext
.status_cb(SSL_CONNECTION_GET_SSL(s
),
2097 sctx
->ext
.status_arg
);
2099 /* We don't want to send a status request response */
2100 case SSL_TLSEXT_ERR_NOACK
:
2101 s
->ext
.status_expected
= 0;
2103 /* status request response should be sent */
2104 case SSL_TLSEXT_ERR_OK
:
2105 if (s
->ext
.ocsp
.resp
)
2106 s
->ext
.status_expected
= 1;
2108 /* something bad happened */
2109 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2111 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CLIENTHELLO_TLSEXT
);
2121 * Call the alpn_select callback if needed. Upon success, returns 1.
2122 * Upon failure, returns 0.
2124 int tls_handle_alpn(SSL_CONNECTION
*s
)
2126 const unsigned char *selected
= NULL
;
2127 unsigned char selected_len
= 0;
2128 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2130 if (sctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2131 int r
= sctx
->ext
.alpn_select_cb(SSL_CONNECTION_GET_SSL(s
),
2132 &selected
, &selected_len
,
2133 s
->s3
.alpn_proposed
,
2134 (unsigned int)s
->s3
.alpn_proposed_len
,
2135 sctx
->ext
.alpn_select_cb_arg
);
2137 if (r
== SSL_TLSEXT_ERR_OK
) {
2138 OPENSSL_free(s
->s3
.alpn_selected
);
2139 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2140 if (s
->s3
.alpn_selected
== NULL
) {
2141 s
->s3
.alpn_selected_len
= 0;
2142 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2145 s
->s3
.alpn_selected_len
= selected_len
;
2146 #ifndef OPENSSL_NO_NEXTPROTONEG
2147 /* ALPN takes precedence over NPN. */
2151 /* Check ALPN is consistent with session */
2152 if (s
->session
->ext
.alpn_selected
== NULL
2153 || selected_len
!= s
->session
->ext
.alpn_selected_len
2154 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2155 selected_len
) != 0) {
2156 /* Not consistent so can't be used for early_data */
2157 s
->ext
.early_data_ok
= 0;
2161 * This is a new session and so alpn_selected should have
2162 * been initialised to NULL. We should update it with the
2165 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2166 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2167 ERR_R_INTERNAL_ERROR
);
2170 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2172 if (s
->session
->ext
.alpn_selected
== NULL
) {
2173 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2174 ERR_R_INTERNAL_ERROR
);
2177 s
->session
->ext
.alpn_selected_len
= selected_len
;
2182 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2183 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
,
2184 SSL_R_NO_APPLICATION_PROTOCOL
);
2188 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2193 /* Check ALPN is consistent with session */
2194 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2195 /* Not consistent so can't be used for early_data */
2196 s
->ext
.early_data_ok
= 0;
2202 WORK_STATE
tls_post_process_client_hello(SSL_CONNECTION
*s
, WORK_STATE wst
)
2204 const SSL_CIPHER
*cipher
;
2205 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2207 if (wst
== WORK_MORE_A
) {
2208 int rv
= tls_early_post_process_client_hello(s
);
2210 /* SSLfatal() was already called */
2217 if (wst
== WORK_MORE_B
) {
2218 if (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
)) {
2219 /* Let cert callback update server certificates if required */
2220 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2221 int rv
= s
->cert
->cert_cb(ssl
, s
->cert
->cert_cb_arg
);
2223 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CERT_CB_ERROR
);
2227 s
->rwstate
= SSL_X509_LOOKUP
;
2230 s
->rwstate
= SSL_NOTHING
;
2233 /* In TLSv1.3 we selected the ciphersuite before resumption */
2234 if (!SSL_CONNECTION_IS_TLS13(s
)) {
2236 ssl3_choose_cipher(s
, s
->peer_ciphers
,
2237 SSL_get_ciphers(ssl
));
2239 if (cipher
== NULL
) {
2240 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2241 SSL_R_NO_SHARED_CIPHER
);
2244 s
->s3
.tmp
.new_cipher
= cipher
;
2247 if (!tls_choose_sigalg(s
, 1)) {
2248 /* SSLfatal already called */
2251 /* check whether we should disable session resumption */
2252 if (s
->not_resumable_session_cb
!= NULL
)
2253 s
->session
->not_resumable
=
2254 s
->not_resumable_session_cb(ssl
,
2255 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2256 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2257 if (s
->session
->not_resumable
)
2258 /* do not send a session ticket */
2259 s
->ext
.ticket_expected
= 0;
2262 /* Session-id reuse */
2263 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2267 * we now have the following setup.
2269 * cipher_list - our preferred list of ciphers
2270 * ciphers - the clients preferred list of ciphers
2271 * compression - basically ignored right now
2272 * ssl version is set - sslv3
2273 * s->session - The ssl session has been setup.
2274 * s->hit - session reuse flag
2275 * s->s3.tmp.new_cipher - the new cipher to use.
2279 * Call status_request callback if needed. Has to be done after the
2280 * certificate callbacks etc above.
2282 if (!tls_handle_status_request(s
)) {
2283 /* SSLfatal() already called */
2287 * Call alpn_select callback if needed. Has to be done after SNI and
2288 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2289 * we already did this because cipher negotiation happens earlier, and
2290 * we must handle ALPN before we decide whether to accept early_data.
2292 if (!SSL_CONNECTION_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2293 /* SSLfatal() already called */
2299 #ifndef OPENSSL_NO_SRP
2300 if (wst
== WORK_MORE_C
) {
2302 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2304 * callback indicates further work to be done
2306 s
->rwstate
= SSL_X509_LOOKUP
;
2310 /* SSLfatal() already called */
2316 return WORK_FINISHED_STOP
;
2321 int tls_construct_server_hello(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2326 unsigned char *session_id
;
2327 int usetls13
= SSL_CONNECTION_IS_TLS13(s
)
2328 || s
->hello_retry_request
== SSL_HRR_PENDING
;
2330 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2331 if (!WPACKET_put_bytes_u16(pkt
, version
)
2333 * Random stuff. Filling of the server_random takes place in
2334 * tls_process_client_hello()
2336 || !WPACKET_memcpy(pkt
,
2337 s
->hello_retry_request
== SSL_HRR_PENDING
2338 ? hrrrandom
: s
->s3
.server_random
,
2339 SSL3_RANDOM_SIZE
)) {
2340 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2345 * There are several cases for the session ID to send
2346 * back in the server hello:
2347 * - For session reuse from the session cache,
2348 * we send back the old session ID.
2349 * - If stateless session reuse (using a session ticket)
2350 * is successful, we send back the client's "session ID"
2351 * (which doesn't actually identify the session).
2352 * - If it is a new session, we send back the new
2354 * - However, if we want the new session to be single-use,
2355 * we send back a 0-length session ID.
2356 * - In TLSv1.3 we echo back the session id sent to us by the client
2358 * s->hit is non-zero in either case of session reuse,
2359 * so the following won't overwrite an ID that we're supposed
2362 if (s
->session
->not_resumable
||
2363 (!(SSL_CONNECTION_GET_CTX(s
)->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2365 s
->session
->session_id_length
= 0;
2368 sl
= s
->tmp_session_id_len
;
2369 session_id
= s
->tmp_session_id
;
2371 sl
= s
->session
->session_id_length
;
2372 session_id
= s
->session
->session_id
;
2375 if (sl
> sizeof(s
->session
->session_id
)) {
2376 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2380 /* set up the compression method */
2381 #ifdef OPENSSL_NO_COMP
2384 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2387 compm
= s
->s3
.tmp
.new_compression
->id
;
2390 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2391 || !SSL_CONNECTION_GET_SSL(s
)->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
,
2393 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2394 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2398 if (!tls_construct_extensions(s
, pkt
,
2399 s
->hello_retry_request
== SSL_HRR_PENDING
2400 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2401 : (SSL_CONNECTION_IS_TLS13(s
)
2402 ? SSL_EXT_TLS1_3_SERVER_HELLO
2403 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2405 /* SSLfatal() already called */
2409 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2410 /* Ditch the session. We'll create a new one next time around */
2411 SSL_SESSION_free(s
->session
);
2416 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2417 * a synthetic message_hash in place of ClientHello1.
2419 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2420 /* SSLfatal() already called */
2423 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2424 && !ssl3_digest_cached_records(s
, 0)) {
2425 /* SSLfatal() already called */;
2432 int tls_construct_server_done(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2434 if (!s
->s3
.tmp
.cert_request
) {
2435 if (!ssl3_digest_cached_records(s
, 0)) {
2436 /* SSLfatal() already called */
2443 int tls_construct_server_key_exchange(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2445 EVP_PKEY
*pkdh
= NULL
;
2446 unsigned char *encodedPoint
= NULL
;
2447 size_t encodedlen
= 0;
2449 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2453 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2454 EVP_PKEY_CTX
*pctx
= NULL
;
2455 size_t paramlen
, paramoffset
;
2456 int freer
= 0, ret
= 0;
2457 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2459 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2460 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2464 if (md_ctx
== NULL
) {
2465 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2469 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2471 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2472 #ifndef OPENSSL_NO_PSK
2473 /* Plain PSK or RSAPSK nothing to do */
2474 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2476 #endif /* !OPENSSL_NO_PSK */
2477 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2478 CERT
*cert
= s
->cert
;
2479 EVP_PKEY
*pkdhp
= NULL
;
2481 if (s
->cert
->dh_tmp_auto
) {
2482 pkdh
= ssl_get_auto_dh(s
);
2484 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2489 pkdhp
= cert
->dh_tmp
;
2491 #if !defined(OPENSSL_NO_DEPRECATED_3_0)
2492 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2493 pkdh
= ssl_dh_to_pkey(s
->cert
->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s
),
2496 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2502 if (pkdhp
== NULL
) {
2503 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
2506 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2507 EVP_PKEY_get_security_bits(pkdhp
), 0, pkdhp
)) {
2508 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_DH_KEY_TOO_SMALL
);
2511 if (s
->s3
.tmp
.pkey
!= NULL
) {
2512 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2516 s
->s3
.tmp
.pkey
= ssl_generate_pkey(s
, pkdhp
);
2517 if (s
->s3
.tmp
.pkey
== NULL
) {
2518 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2522 EVP_PKEY_free(pkdh
);
2525 /* These BIGNUMs need to be freed when we're finished */
2527 if (!EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
, OSSL_PKEY_PARAM_FFC_P
,
2529 || !EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
, OSSL_PKEY_PARAM_FFC_G
,
2531 || !EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
,
2532 OSSL_PKEY_PARAM_PUB_KEY
, &r
[2])) {
2533 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2536 } else if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2538 if (s
->s3
.tmp
.pkey
!= NULL
) {
2539 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2543 /* Get NID of appropriate shared curve */
2544 curve_id
= tls1_shared_group(s
, -2);
2545 if (curve_id
== 0) {
2546 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2547 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2550 /* Cache the group used in the SSL_SESSION */
2551 s
->session
->kex_group
= curve_id
;
2552 /* Generate a new key for this curve */
2553 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2554 if (s
->s3
.tmp
.pkey
== NULL
) {
2555 /* SSLfatal() already called */
2559 /* Encode the public key. */
2560 encodedlen
= EVP_PKEY_get1_encoded_public_key(s
->s3
.tmp
.pkey
,
2562 if (encodedlen
== 0) {
2563 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
2568 * We'll generate the serverKeyExchange message explicitly so we
2569 * can set these to NULLs
2576 #ifndef OPENSSL_NO_SRP
2577 if (type
& SSL_kSRP
) {
2578 if ((s
->srp_ctx
.N
== NULL
) ||
2579 (s
->srp_ctx
.g
== NULL
) ||
2580 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2581 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_SRP_PARAM
);
2584 r
[0] = s
->srp_ctx
.N
;
2585 r
[1] = s
->srp_ctx
.g
;
2586 r
[2] = s
->srp_ctx
.s
;
2587 r
[3] = s
->srp_ctx
.B
;
2591 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2595 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2596 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2598 } else if (lu
== NULL
) {
2599 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_INTERNAL_ERROR
);
2603 #ifndef OPENSSL_NO_PSK
2604 if (type
& SSL_PSK
) {
2605 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2606 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2609 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2610 * checked this when we set the identity hint - but just in case
2612 if (len
> PSK_MAX_IDENTITY_LEN
2613 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2615 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2621 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2622 unsigned char *binval
;
2625 #ifndef OPENSSL_NO_SRP
2626 if ((i
== 2) && (type
& SSL_kSRP
)) {
2627 res
= WPACKET_start_sub_packet_u8(pkt
);
2630 res
= WPACKET_start_sub_packet_u16(pkt
);
2633 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2638 * for interoperability with some versions of the Microsoft TLS
2639 * stack, we need to zero pad the DHE pub key to the same length
2642 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2643 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2646 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2647 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2650 memset(binval
, 0, len
);
2654 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2655 || !WPACKET_close(pkt
)) {
2656 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2660 BN_bn2bin(r
[i
], binval
);
2663 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2665 * We only support named (not generic) curves. In this situation, the
2666 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2667 * [1 byte length of encoded point], followed by the actual encoded
2670 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2671 || !WPACKET_put_bytes_u8(pkt
, 0)
2672 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2673 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2674 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2677 OPENSSL_free(encodedPoint
);
2678 encodedPoint
= NULL
;
2683 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2685 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2686 size_t siglen
= 0, tbslen
;
2688 if (pkey
== NULL
|| !tls1_lookup_md(sctx
, lu
, &md
)) {
2689 /* Should never happen */
2690 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2693 /* Get length of the parameters we have written above */
2694 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2695 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2698 /* send signature algorithm */
2699 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2700 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2704 if (EVP_DigestSignInit_ex(md_ctx
, &pctx
,
2705 md
== NULL
? NULL
: EVP_MD_get0_name(md
),
2706 sctx
->libctx
, sctx
->propq
, pkey
,
2708 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2711 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2712 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2713 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2714 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2718 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2719 s
->init_buf
->data
+ paramoffset
,
2722 /* SSLfatal() already called */
2726 if (EVP_DigestSign(md_ctx
, NULL
, &siglen
, tbs
, tbslen
) <=0
2727 || !WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2728 || EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
) <= 0
2729 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2730 || sigbytes1
!= sigbytes2
) {
2732 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2740 EVP_PKEY_free(pkdh
);
2741 OPENSSL_free(encodedPoint
);
2742 EVP_MD_CTX_free(md_ctx
);
2752 int tls_construct_certificate_request(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2754 if (SSL_CONNECTION_IS_TLS13(s
)) {
2755 /* Send random context when doing post-handshake auth */
2756 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2757 OPENSSL_free(s
->pha_context
);
2758 s
->pha_context_len
= 32;
2759 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
) {
2760 s
->pha_context_len
= 0;
2761 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2764 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
2765 s
->pha_context
, s
->pha_context_len
, 0) <= 0
2766 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
,
2767 s
->pha_context_len
)) {
2768 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2771 /* reset the handshake hash back to just after the ClientFinished */
2772 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2773 /* SSLfatal() already called */
2777 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2778 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2783 if (!tls_construct_extensions(s
, pkt
,
2784 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2786 /* SSLfatal() already called */
2792 /* get the list of acceptable cert types */
2793 if (!WPACKET_start_sub_packet_u8(pkt
)
2794 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2795 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2799 if (SSL_USE_SIGALGS(s
)) {
2800 const uint16_t *psigs
;
2801 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2803 if (!WPACKET_start_sub_packet_u16(pkt
)
2804 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2805 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2806 || !WPACKET_close(pkt
)) {
2807 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2812 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2813 /* SSLfatal() already called */
2819 s
->s3
.tmp
.cert_request
= 1;
2823 static int tls_process_cke_psk_preamble(SSL_CONNECTION
*s
, PACKET
*pkt
)
2825 #ifndef OPENSSL_NO_PSK
2826 unsigned char psk
[PSK_MAX_PSK_LEN
];
2828 PACKET psk_identity
;
2830 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2831 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2834 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2835 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DATA_LENGTH_TOO_LONG
);
2838 if (s
->psk_server_callback
== NULL
) {
2839 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_PSK_NO_SERVER_CB
);
2843 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2844 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2848 psklen
= s
->psk_server_callback(SSL_CONNECTION_GET_SSL(s
),
2849 s
->session
->psk_identity
,
2852 if (psklen
> PSK_MAX_PSK_LEN
) {
2853 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2855 } else if (psklen
== 0) {
2857 * PSK related to the given identity not found
2859 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
, SSL_R_PSK_IDENTITY_NOT_FOUND
);
2863 OPENSSL_free(s
->s3
.tmp
.psk
);
2864 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2865 OPENSSL_cleanse(psk
, psklen
);
2867 if (s
->s3
.tmp
.psk
== NULL
) {
2868 s
->s3
.tmp
.psklen
= 0;
2869 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2873 s
->s3
.tmp
.psklen
= psklen
;
2877 /* Should never happen */
2878 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2883 static int tls_process_cke_rsa(SSL_CONNECTION
*s
, PACKET
*pkt
)
2886 PACKET enc_premaster
;
2887 EVP_PKEY
*rsa
= NULL
;
2888 unsigned char *rsa_decrypt
= NULL
;
2890 EVP_PKEY_CTX
*ctx
= NULL
;
2891 OSSL_PARAM params
[3], *p
= params
;
2892 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2894 rsa
= s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
;
2896 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_RSA_CERTIFICATE
);
2900 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2901 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2902 enc_premaster
= *pkt
;
2904 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2905 || PACKET_remaining(pkt
) != 0) {
2906 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2911 outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
2912 rsa_decrypt
= OPENSSL_malloc(outlen
);
2913 if (rsa_decrypt
== NULL
) {
2914 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2918 ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, rsa
, sctx
->propq
);
2920 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2925 * We must not leak whether a decryption failure occurs because of
2926 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2927 * section 7.4.7.1). We use the special padding type
2928 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
2929 * RSA, check the padding and check that the client version is as expected
2930 * in the premaster secret. If any of that fails then the function appears
2931 * to return successfully but with a random result. The call below could
2932 * still fail if the input is publicly invalid.
2933 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2935 if (EVP_PKEY_decrypt_init(ctx
) <= 0
2936 || EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_WITH_TLS_PADDING
) <= 0) {
2937 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2941 *p
++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
,
2942 (unsigned int *)&s
->client_version
);
2943 if ((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) != 0)
2944 *p
++ = OSSL_PARAM_construct_uint(
2945 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
,
2946 (unsigned int *)&s
->version
);
2947 *p
++ = OSSL_PARAM_construct_end();
2949 if (!EVP_PKEY_CTX_set_params(ctx
, params
)
2950 || EVP_PKEY_decrypt(ctx
, rsa_decrypt
, &outlen
,
2951 PACKET_data(&enc_premaster
),
2952 PACKET_remaining(&enc_premaster
)) <= 0) {
2953 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2958 * This test should never fail (otherwise we should have failed above) but
2959 * we double check anyway.
2961 if (outlen
!= SSL_MAX_MASTER_KEY_LENGTH
) {
2962 OPENSSL_cleanse(rsa_decrypt
, SSL_MAX_MASTER_KEY_LENGTH
);
2963 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2967 /* Also cleanses rsa_decrypt (on success or failure) */
2968 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
2969 SSL_MAX_MASTER_KEY_LENGTH
, 0)) {
2970 /* SSLfatal() already called */
2976 OPENSSL_free(rsa_decrypt
);
2977 EVP_PKEY_CTX_free(ctx
);
2981 static int tls_process_cke_dhe(SSL_CONNECTION
*s
, PACKET
*pkt
)
2983 EVP_PKEY
*skey
= NULL
;
2985 const unsigned char *data
;
2986 EVP_PKEY
*ckey
= NULL
;
2989 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
2990 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2993 skey
= s
->s3
.tmp
.pkey
;
2995 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
2999 if (PACKET_remaining(pkt
) == 0L) {
3000 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
3003 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3004 /* We already checked we have enough data */
3005 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3008 ckey
= EVP_PKEY_new();
3009 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3010 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
3014 if (!EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
)) {
3015 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3019 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3020 /* SSLfatal() already called */
3025 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3026 s
->s3
.tmp
.pkey
= NULL
;
3028 EVP_PKEY_free(ckey
);
3032 static int tls_process_cke_ecdhe(SSL_CONNECTION
*s
, PACKET
*pkt
)
3034 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3035 EVP_PKEY
*ckey
= NULL
;
3038 if (PACKET_remaining(pkt
) == 0L) {
3039 /* We don't support ECDH client auth */
3040 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_MISSING_TMP_ECDH_KEY
);
3044 const unsigned char *data
;
3047 * Get client's public key from encoded point in the
3048 * ClientKeyExchange message.
3051 /* Get encoded point length */
3052 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3053 || PACKET_remaining(pkt
) != 0) {
3054 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3058 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_ECDH_KEY
);
3062 ckey
= EVP_PKEY_new();
3063 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3064 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
3068 if (EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
) <= 0) {
3069 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
3074 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3075 /* SSLfatal() already called */
3080 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3081 s
->s3
.tmp
.pkey
= NULL
;
3083 EVP_PKEY_free(ckey
);
3088 static int tls_process_cke_srp(SSL_CONNECTION
*s
, PACKET
*pkt
)
3090 #ifndef OPENSSL_NO_SRP
3092 const unsigned char *data
;
3094 if (!PACKET_get_net_2(pkt
, &i
)
3095 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3096 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_SRP_A_LENGTH
);
3099 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3100 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BN_LIB
);
3103 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3104 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_SRP_PARAMETERS
);
3107 OPENSSL_free(s
->session
->srp_username
);
3108 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3109 if (s
->session
->srp_username
== NULL
) {
3110 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3114 if (!srp_generate_server_master_secret(s
)) {
3115 /* SSLfatal() already called */
3121 /* Should never happen */
3122 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3127 static int tls_process_cke_gost(SSL_CONNECTION
*s
, PACKET
*pkt
)
3129 #ifndef OPENSSL_NO_GOST
3130 EVP_PKEY_CTX
*pkey_ctx
;
3131 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3132 unsigned char premaster_secret
[32];
3133 const unsigned char *start
;
3134 size_t outlen
= 32, inlen
;
3135 unsigned long alg_a
;
3136 GOST_KX_MESSAGE
*pKX
= NULL
;
3137 const unsigned char *ptr
;
3139 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3141 /* Get our certificate private key */
3142 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3143 if (alg_a
& SSL_aGOST12
) {
3145 * New GOST ciphersuites have SSL_aGOST01 bit too
3147 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3149 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3152 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3154 } else if (alg_a
& SSL_aGOST01
) {
3155 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3158 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, pk
, sctx
->propq
);
3159 if (pkey_ctx
== NULL
) {
3160 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3163 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3164 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3168 * If client certificate is present and is of the same type, maybe
3169 * use it for key exchange. Don't mind errors from
3170 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3171 * client certificate for authorization only.
3173 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3174 if (client_pub_pkey
) {
3175 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3179 ptr
= PACKET_data(pkt
);
3180 /* Some implementations provide extra data in the opaqueBlob
3181 * We have nothing to do with this blob so we just skip it */
3182 pKX
= d2i_GOST_KX_MESSAGE(NULL
, &ptr
, PACKET_remaining(pkt
));
3184 || pKX
->kxBlob
== NULL
3185 || ASN1_TYPE_get(pKX
->kxBlob
) != V_ASN1_SEQUENCE
) {
3186 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3190 if (!PACKET_forward(pkt
, ptr
- PACKET_data(pkt
))) {
3191 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_DECRYPTION_FAILED
);
3195 if (PACKET_remaining(pkt
) != 0) {
3196 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_DECRYPTION_FAILED
);
3200 inlen
= pKX
->kxBlob
->value
.sequence
->length
;
3201 start
= pKX
->kxBlob
->value
.sequence
->data
;
3203 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3205 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3208 /* Generate master secret */
3209 if (!ssl_generate_master_secret(s
, premaster_secret
,
3210 sizeof(premaster_secret
), 0)) {
3211 /* SSLfatal() already called */
3214 /* Check if pubkey from client certificate was used */
3215 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3217 s
->statem
.no_cert_verify
= 1;
3221 EVP_PKEY_CTX_free(pkey_ctx
);
3222 GOST_KX_MESSAGE_free(pKX
);
3225 /* Should never happen */
3226 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3231 static int tls_process_cke_gost18(SSL_CONNECTION
*s
, PACKET
*pkt
)
3233 #ifndef OPENSSL_NO_GOST
3234 unsigned char rnd_dgst
[32];
3235 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3236 EVP_PKEY
*pk
= NULL
;
3237 unsigned char premaster_secret
[32];
3238 const unsigned char *start
= NULL
;
3239 size_t outlen
= 32, inlen
= 0;
3241 int cipher_nid
= ossl_gost18_cke_cipher_nid(s
);
3242 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3244 if (cipher_nid
== NID_undef
) {
3245 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3249 if (ossl_gost_ukm(s
, rnd_dgst
) <= 0) {
3250 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3254 /* Get our certificate private key */
3255 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
!= NULL
?
3256 s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
:
3257 s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3259 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
3263 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, pk
, sctx
->propq
);
3264 if (pkey_ctx
== NULL
) {
3265 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3268 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3269 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3273 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3274 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3275 EVP_PKEY_CTRL_SET_IV
, 32, rnd_dgst
) <= 0) {
3276 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3280 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3281 EVP_PKEY_CTRL_CIPHER
, cipher_nid
, NULL
) <= 0) {
3282 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3285 inlen
= PACKET_remaining(pkt
);
3286 start
= PACKET_data(pkt
);
3288 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3289 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3292 /* Generate master secret */
3293 if (!ssl_generate_master_secret(s
, premaster_secret
,
3294 sizeof(premaster_secret
), 0)) {
3295 /* SSLfatal() already called */
3301 EVP_PKEY_CTX_free(pkey_ctx
);
3304 /* Should never happen */
3305 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3310 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL_CONNECTION
*s
,
3313 unsigned long alg_k
;
3315 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3317 /* For PSK parse and retrieve identity, obtain PSK key */
3318 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3319 /* SSLfatal() already called */
3323 if (alg_k
& SSL_kPSK
) {
3324 /* Identity extracted earlier: should be nothing left */
3325 if (PACKET_remaining(pkt
) != 0) {
3326 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3329 /* PSK handled by ssl_generate_master_secret */
3330 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3331 /* SSLfatal() already called */
3334 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3335 if (!tls_process_cke_rsa(s
, pkt
)) {
3336 /* SSLfatal() already called */
3339 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3340 if (!tls_process_cke_dhe(s
, pkt
)) {
3341 /* SSLfatal() already called */
3344 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3345 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3346 /* SSLfatal() already called */
3349 } else if (alg_k
& SSL_kSRP
) {
3350 if (!tls_process_cke_srp(s
, pkt
)) {
3351 /* SSLfatal() already called */
3354 } else if (alg_k
& SSL_kGOST
) {
3355 if (!tls_process_cke_gost(s
, pkt
)) {
3356 /* SSLfatal() already called */
3359 } else if (alg_k
& SSL_kGOST18
) {
3360 if (!tls_process_cke_gost18(s
, pkt
)) {
3361 /* SSLfatal() already called */
3365 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNKNOWN_CIPHER_TYPE
);
3369 return MSG_PROCESS_CONTINUE_PROCESSING
;
3371 #ifndef OPENSSL_NO_PSK
3372 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3373 s
->s3
.tmp
.psk
= NULL
;
3374 s
->s3
.tmp
.psklen
= 0;
3376 return MSG_PROCESS_ERROR
;
3379 WORK_STATE
tls_post_process_client_key_exchange(SSL_CONNECTION
*s
,
3382 #ifndef OPENSSL_NO_SCTP
3383 if (wst
== WORK_MORE_A
) {
3384 if (SSL_CONNECTION_IS_DTLS(s
)) {
3385 unsigned char sctpauthkey
[64];
3386 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3389 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3392 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3393 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3395 /* Don't include the terminating zero. */
3396 labellen
= sizeof(labelbuffer
) - 1;
3397 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3400 if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s
),
3402 sizeof(sctpauthkey
), labelbuffer
,
3405 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3409 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3410 sizeof(sctpauthkey
), sctpauthkey
);
3415 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3417 * No certificate verify or no peer certificate so we no longer need
3418 * the handshake_buffer
3420 if (!ssl3_digest_cached_records(s
, 0)) {
3421 /* SSLfatal() already called */
3424 return WORK_FINISHED_CONTINUE
;
3426 if (!s
->s3
.handshake_buffer
) {
3427 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3431 * For sigalgs freeze the handshake buffer. If we support
3432 * extms we've done this already so this is a no-op
3434 if (!ssl3_digest_cached_records(s
, 1)) {
3435 /* SSLfatal() already called */
3440 return WORK_FINISHED_CONTINUE
;
3443 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL_CONNECTION
*s
,
3447 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3450 const unsigned char *certstart
, *certbytes
;
3451 STACK_OF(X509
) *sk
= NULL
;
3452 PACKET spkt
, context
;
3454 SSL_SESSION
*new_sess
= NULL
;
3455 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3458 * To get this far we must have read encrypted data from the client. We no
3459 * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3
3461 if (s
->rlayer
.rrlmethod
->set_plain_alerts
!= NULL
)
3462 s
->rlayer
.rrlmethod
->set_plain_alerts(s
->rlayer
.rrl
, 0);
3464 if ((sk
= sk_X509_new_null()) == NULL
) {
3465 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3469 if (SSL_CONNECTION_IS_TLS13(s
)
3470 && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3471 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3472 || (s
->pha_context
!= NULL
3473 && !PACKET_equal(&context
, s
->pha_context
,
3474 s
->pha_context_len
)))) {
3475 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
3479 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3480 || PACKET_remaining(pkt
) != 0) {
3481 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3485 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3486 if (!PACKET_get_net_3(&spkt
, &l
)
3487 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3488 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
3492 certstart
= certbytes
;
3493 x
= X509_new_ex(sctx
->libctx
, sctx
->propq
);
3495 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_MALLOC_FAILURE
);
3498 if (d2i_X509(&x
, (const unsigned char **)&certbytes
, l
) == NULL
) {
3499 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_ASN1_LIB
);
3503 if (certbytes
!= (certstart
+ l
)) {
3504 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
3508 if (SSL_CONNECTION_IS_TLS13(s
)) {
3509 RAW_EXTENSION
*rawexts
= NULL
;
3512 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3513 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
3516 if (!tls_collect_extensions(s
, &extensions
,
3517 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3518 NULL
, chainidx
== 0)
3519 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3520 rawexts
, x
, chainidx
,
3521 PACKET_remaining(&spkt
) == 0)) {
3522 OPENSSL_free(rawexts
);
3525 OPENSSL_free(rawexts
);
3528 if (!sk_X509_push(sk
, x
)) {
3529 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3535 if (sk_X509_num(sk
) <= 0) {
3536 /* TLS does not mind 0 certs returned */
3537 if (s
->version
== SSL3_VERSION
) {
3538 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3539 SSL_R_NO_CERTIFICATES_RETURNED
);
3542 /* Fail for TLS only if we required a certificate */
3543 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3544 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3545 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3546 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3549 /* No client certificate so digest cached records */
3550 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3551 /* SSLfatal() already called */
3556 i
= ssl_verify_cert_chain(s
, sk
);
3558 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3559 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3562 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3564 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3565 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3571 * Sessions must be immutable once they go into the session cache. Otherwise
3572 * we can get multi-thread problems. Therefore we don't "update" sessions,
3573 * we replace them with a duplicate. Here, we need to do this every time
3574 * a new certificate is received via post-handshake authentication, as the
3575 * session may have already gone into the session cache.
3578 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3579 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3580 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3584 SSL_SESSION_free(s
->session
);
3585 s
->session
= new_sess
;
3588 X509_free(s
->session
->peer
);
3589 s
->session
->peer
= sk_X509_num(sk
) == 0 ? NULL
: sk_X509_shift(sk
);
3590 s
->session
->verify_result
= s
->verify_result
;
3592 OSSL_STACK_OF_X509_free(s
->session
->peer_chain
);
3593 s
->session
->peer_chain
= sk
;
3597 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3600 if (SSL_CONNECTION_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3601 /* SSLfatal() already called */
3606 * Inconsistency alert: cert_chain does *not* include the peer's own
3607 * certificate, while we do include it in statem_clnt.c
3610 /* Save the current hash state for when we receive the CertificateVerify */
3611 if (SSL_CONNECTION_IS_TLS13(s
)) {
3612 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3613 sizeof(s
->cert_verify_hash
),
3614 &s
->cert_verify_hash_len
)) {
3615 /* SSLfatal() already called */
3619 /* Resend session tickets */
3620 s
->sent_tickets
= 0;
3623 ret
= MSG_PROCESS_CONTINUE_READING
;
3627 OSSL_STACK_OF_X509_free(sk
);
3631 int tls_construct_server_certificate(SSL_CONNECTION
*s
, WPACKET
*pkt
)
3633 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3636 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3641 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3642 * for the server Certificate message
3644 if (SSL_CONNECTION_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3645 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3648 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3649 /* SSLfatal() already called */
3656 static int create_ticket_prequel(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3657 uint32_t age_add
, unsigned char *tick_nonce
)
3659 uint32_t timeout
= (uint32_t)ossl_time2seconds(s
->session
->timeout
);
3662 * Ticket lifetime hint:
3663 * In TLSv1.3 we reset the "time" field above, and always specify the
3664 * timeout, limited to a 1 week period per RFC8446.
3665 * For TLSv1.2 this is advisory only and we leave this unspecified for
3666 * resumed session (for simplicity).
3668 #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
3670 if (SSL_CONNECTION_IS_TLS13(s
)) {
3671 if (ossl_time_compare(s
->session
->timeout
,
3672 ossl_seconds2time(ONE_WEEK_SEC
)) > 0)
3673 timeout
= ONE_WEEK_SEC
;
3677 if (!WPACKET_put_bytes_u32(pkt
, timeout
)) {
3678 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3682 if (SSL_CONNECTION_IS_TLS13(s
)) {
3683 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3684 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3685 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3690 /* Start the sub-packet for the actual ticket data */
3691 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3692 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3699 static int construct_stateless_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3701 unsigned char *tick_nonce
)
3703 unsigned char *senc
= NULL
;
3704 EVP_CIPHER_CTX
*ctx
= NULL
;
3705 SSL_HMAC
*hctx
= NULL
;
3706 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3707 const unsigned char *const_p
;
3708 int len
, slen_full
, slen
, lenfinal
;
3711 SSL_CTX
*tctx
= s
->session_ctx
;
3712 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3713 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3715 size_t macoffset
, macendoffset
;
3716 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
3717 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3719 /* get session encoding length */
3720 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3722 * Some length values are 16 bits, so forget it if session is too
3725 if (slen_full
== 0 || slen_full
> 0xFF00) {
3726 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3729 senc
= OPENSSL_malloc(slen_full
);
3731 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3735 ctx
= EVP_CIPHER_CTX_new();
3736 hctx
= ssl_hmac_new(tctx
);
3737 if (ctx
== NULL
|| hctx
== NULL
) {
3738 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3743 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3744 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3749 * create a fresh copy (not shared with other threads) to clean up
3752 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3754 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3758 slen
= i2d_SSL_SESSION(sess
, NULL
);
3759 if (slen
== 0 || slen
> slen_full
) {
3760 /* shouldn't ever happen */
3761 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3762 SSL_SESSION_free(sess
);
3766 if (!i2d_SSL_SESSION(sess
, &p
)) {
3767 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3768 SSL_SESSION_free(sess
);
3771 SSL_SESSION_free(sess
);
3774 * Initialize HMAC and cipher contexts. If callback present it does
3775 * all the work otherwise use generated values from parent ctx.
3777 #ifndef OPENSSL_NO_DEPRECATED_3_0
3778 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
|| tctx
->ext
.ticket_key_cb
!= NULL
)
3780 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3785 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3786 ret
= tctx
->ext
.ticket_key_evp_cb(ssl
, key_name
, iv
, ctx
,
3787 ssl_hmac_get0_EVP_MAC_CTX(hctx
),
3789 #ifndef OPENSSL_NO_DEPRECATED_3_0
3790 else if (tctx
->ext
.ticket_key_cb
!= NULL
)
3791 /* if 0 is returned, write an empty ticket */
3792 ret
= tctx
->ext
.ticket_key_cb(ssl
, key_name
, iv
, ctx
,
3793 ssl_hmac_get0_HMAC_CTX(hctx
), 1);
3798 /* Put timeout and length */
3799 if (!WPACKET_put_bytes_u32(pkt
, 0)
3800 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3801 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3805 EVP_CIPHER_CTX_free(ctx
);
3806 ssl_hmac_free(hctx
);
3810 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CALLBACK_FAILED
);
3813 iv_len
= EVP_CIPHER_CTX_get_iv_length(ctx
);
3815 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3819 EVP_CIPHER
*cipher
= EVP_CIPHER_fetch(sctx
->libctx
, "AES-256-CBC",
3822 if (cipher
== NULL
) {
3823 /* Error is already recorded */
3824 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
3828 iv_len
= EVP_CIPHER_get_iv_length(cipher
);
3830 || RAND_bytes_ex(sctx
->libctx
, iv
, iv_len
, 0) <= 0
3831 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3832 tctx
->ext
.secure
->tick_aes_key
, iv
)
3833 || !ssl_hmac_init(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
3834 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
3836 EVP_CIPHER_free(cipher
);
3837 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3840 EVP_CIPHER_free(cipher
);
3841 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3842 sizeof(tctx
->ext
.tick_key_name
));
3845 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3846 /* SSLfatal() already called */
3850 if (!WPACKET_get_total_written(pkt
, &macoffset
)
3851 /* Output key name */
3852 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3854 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3855 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3857 /* Encrypt session data */
3858 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3859 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3860 || encdata1
!= encdata2
3861 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3862 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3863 || encdata1
+ len
!= encdata2
3864 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3865 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3866 || !ssl_hmac_update(hctx
,
3867 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3868 macendoffset
- macoffset
)
3869 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3870 || !ssl_hmac_final(hctx
, macdata1
, &hlen
, EVP_MAX_MD_SIZE
)
3871 || hlen
> EVP_MAX_MD_SIZE
3872 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3873 || macdata1
!= macdata2
) {
3874 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3878 /* Close the sub-packet created by create_ticket_prequel() */
3879 if (!WPACKET_close(pkt
)) {
3880 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3887 EVP_CIPHER_CTX_free(ctx
);
3888 ssl_hmac_free(hctx
);
3892 static int construct_stateful_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3894 unsigned char *tick_nonce
)
3896 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3897 /* SSLfatal() already called */
3901 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
3902 s
->session
->session_id_length
)
3903 || !WPACKET_close(pkt
)) {
3904 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3911 int tls_construct_new_session_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
)
3913 SSL_CTX
*tctx
= s
->session_ctx
;
3914 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
3916 unsigned char age_add_c
[sizeof(uint32_t)];
3920 age_add_u
.age_add
= 0;
3922 if (SSL_CONNECTION_IS_TLS13(s
)) {
3925 static const unsigned char nonce_label
[] = "resumption";
3926 const EVP_MD
*md
= ssl_handshake_md(s
);
3927 int hashleni
= EVP_MD_get_size(md
);
3929 /* Ensure cast to size_t is safe */
3930 if (!ossl_assert(hashleni
>= 0)) {
3931 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3934 hashlen
= (size_t)hashleni
;
3937 * If we already sent one NewSessionTicket, or we resumed then
3938 * s->session may already be in a cache and so we must not modify it.
3939 * Instead we need to take a copy of it and modify that.
3941 if (s
->sent_tickets
!= 0 || s
->hit
) {
3942 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
3944 if (new_sess
== NULL
) {
3945 /* SSLfatal already called */
3949 SSL_SESSION_free(s
->session
);
3950 s
->session
= new_sess
;
3953 if (!ssl_generate_session_id(s
, s
->session
)) {
3954 /* SSLfatal() already called */
3957 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
3958 age_add_u
.age_add_c
, sizeof(age_add_u
), 0) <= 0) {
3959 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3962 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
3964 nonce
= s
->next_ticket_nonce
;
3965 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
3966 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
3970 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
3972 sizeof(nonce_label
) - 1,
3975 s
->session
->master_key
,
3977 /* SSLfatal() already called */
3980 s
->session
->master_key_length
= hashlen
;
3982 s
->session
->time
= ossl_time_now();
3983 ssl_session_calculate_timeout(s
->session
);
3984 if (s
->s3
.alpn_selected
!= NULL
) {
3985 OPENSSL_free(s
->session
->ext
.alpn_selected
);
3986 s
->session
->ext
.alpn_selected
=
3987 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
3988 if (s
->session
->ext
.alpn_selected
== NULL
) {
3989 s
->session
->ext
.alpn_selected_len
= 0;
3990 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3993 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
3995 s
->session
->ext
.max_early_data
= s
->max_early_data
;
3998 if (tctx
->generate_ticket_cb
!= NULL
&&
3999 tctx
->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s
),
4000 tctx
->ticket_cb_data
) == 0) {
4001 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4005 * If we are using anti-replay protection then we behave as if
4006 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4007 * is no point in using full stateless tickets.
4009 if (SSL_CONNECTION_IS_TLS13(s
)
4010 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4011 || (s
->max_early_data
> 0
4012 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4013 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4014 /* SSLfatal() already called */
4017 } else if (!construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4019 /* SSLfatal() already called */
4023 if (SSL_CONNECTION_IS_TLS13(s
)) {
4024 if (!tls_construct_extensions(s
, pkt
,
4025 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4027 /* SSLfatal() already called */
4031 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4032 * gets reset to 0 if we send more tickets following a post-handshake
4033 * auth, but |next_ticket_nonce| does not. If we're sending extra
4034 * tickets, decrement the count of pending extra tickets.
4037 s
->next_ticket_nonce
++;
4038 if (s
->ext
.extra_tickets_expected
> 0)
4039 s
->ext
.extra_tickets_expected
--;
4040 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4049 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4050 * create a separate message. Returns 1 on success or 0 on failure.
4052 int tls_construct_cert_status_body(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4054 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4055 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4056 s
->ext
.ocsp
.resp_len
)) {
4057 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4064 int tls_construct_cert_status(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4066 if (!tls_construct_cert_status_body(s
, pkt
)) {
4067 /* SSLfatal() already called */
4074 #ifndef OPENSSL_NO_NEXTPROTONEG
4076 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4077 * It sets the next_proto member in s if found
4079 MSG_PROCESS_RETURN
tls_process_next_proto(SSL_CONNECTION
*s
, PACKET
*pkt
)
4081 PACKET next_proto
, padding
;
4082 size_t next_proto_len
;
4085 * The payload looks like:
4087 * uint8 proto[proto_len];
4088 * uint8 padding_len;
4089 * uint8 padding[padding_len];
4091 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4092 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4093 || PACKET_remaining(pkt
) > 0) {
4094 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
4095 return MSG_PROCESS_ERROR
;
4098 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4100 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4101 return MSG_PROCESS_ERROR
;
4104 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4106 return MSG_PROCESS_CONTINUE_READING
;
4110 static int tls_construct_encrypted_extensions(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4112 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4114 /* SSLfatal() already called */
4121 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL_CONNECTION
*s
, PACKET
*pkt
)
4123 if (PACKET_remaining(pkt
) != 0) {
4124 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
4125 return MSG_PROCESS_ERROR
;
4128 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4129 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4130 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4131 return MSG_PROCESS_ERROR
;
4135 * EndOfEarlyData signals a key change so the end of the message must be on
4136 * a record boundary.
4138 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4139 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
4140 return MSG_PROCESS_ERROR
;
4143 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4144 if (!SSL_CONNECTION_GET_SSL(s
)->method
->ssl3_enc
->change_cipher_state(s
,
4145 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4146 /* SSLfatal() already called */
4147 return MSG_PROCESS_ERROR
;
4150 return MSG_PROCESS_CONTINUE_READING
;