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 s
->statem
.enc_read_state
= ENC_READ_STATE_ALLOW_PLAIN_ALERTS
;
921 #ifndef OPENSSL_NO_SCTP
922 if (SSL_CONNECTION_IS_DTLS(s
) && !s
->hit
) {
924 * Change to new shared key of SCTP-Auth, will be ignored if
927 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
931 if (!ssl
->method
->ssl3_enc
->change_cipher_state(s
,
932 SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
933 /* SSLfatal() already called */
937 if (SSL_CONNECTION_IS_DTLS(s
))
938 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
941 case TLS_ST_SW_SRVR_DONE
:
942 if (statem_flush(s
) != 1)
946 case TLS_ST_SW_FINISHED
:
947 if (statem_flush(s
) != 1)
949 #ifndef OPENSSL_NO_SCTP
950 if (SSL_CONNECTION_IS_DTLS(s
) && s
->hit
) {
952 * Change to new shared key of SCTP-Auth, will be ignored if
955 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
959 if (SSL_CONNECTION_IS_TLS13(s
)) {
960 /* TLS 1.3 gets the secret size from the handshake md */
962 if (!ssl
->method
->ssl3_enc
->generate_master_secret(s
,
963 s
->master_secret
, s
->handshake_secret
, 0,
965 || !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
966 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
967 /* SSLfatal() already called */
972 case TLS_ST_SW_CERT_REQ
:
973 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
974 if (statem_flush(s
) != 1)
979 case TLS_ST_SW_KEY_UPDATE
:
980 if (statem_flush(s
) != 1)
982 if (!tls13_update_key(s
, 1)) {
983 /* SSLfatal() already called */
988 case TLS_ST_SW_SESSION_TICKET
:
990 if (SSL_CONNECTION_IS_TLS13(s
) && statem_flush(s
) != 1) {
991 if (SSL_get_error(ssl
, 0) == SSL_ERROR_SYSCALL
992 && conn_is_closed()) {
994 * We ignore connection closed errors in TLSv1.3 when sending a
995 * NewSessionTicket and behave as if we were successful. This is
996 * so that we are still able to read data sent to us by a client
997 * that closes soon after the end of the handshake without
998 * waiting to read our post-handshake NewSessionTickets.
1000 s
->rwstate
= SSL_NOTHING
;
1009 return WORK_FINISHED_CONTINUE
;
1013 * Get the message construction function and message type for sending from the
1016 * Valid return values are:
1020 int ossl_statem_server_construct_message(SSL_CONNECTION
*s
,
1021 confunc_f
*confunc
, int *mt
)
1023 OSSL_STATEM
*st
= &s
->statem
;
1025 switch (st
->hand_state
) {
1027 /* Shouldn't happen */
1028 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
1031 case TLS_ST_SW_CHANGE
:
1032 if (SSL_CONNECTION_IS_DTLS(s
))
1033 *confunc
= dtls_construct_change_cipher_spec
;
1035 *confunc
= tls_construct_change_cipher_spec
;
1036 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1039 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1040 *confunc
= dtls_construct_hello_verify_request
;
1041 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1044 case TLS_ST_SW_HELLO_REQ
:
1045 /* No construction function needed */
1047 *mt
= SSL3_MT_HELLO_REQUEST
;
1050 case TLS_ST_SW_SRVR_HELLO
:
1051 *confunc
= tls_construct_server_hello
;
1052 *mt
= SSL3_MT_SERVER_HELLO
;
1055 case TLS_ST_SW_CERT
:
1056 *confunc
= tls_construct_server_certificate
;
1057 *mt
= SSL3_MT_CERTIFICATE
;
1060 case TLS_ST_SW_CERT_VRFY
:
1061 *confunc
= tls_construct_cert_verify
;
1062 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1066 case TLS_ST_SW_KEY_EXCH
:
1067 *confunc
= tls_construct_server_key_exchange
;
1068 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1071 case TLS_ST_SW_CERT_REQ
:
1072 *confunc
= tls_construct_certificate_request
;
1073 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1076 case TLS_ST_SW_SRVR_DONE
:
1077 *confunc
= tls_construct_server_done
;
1078 *mt
= SSL3_MT_SERVER_DONE
;
1081 case TLS_ST_SW_SESSION_TICKET
:
1082 *confunc
= tls_construct_new_session_ticket
;
1083 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1086 case TLS_ST_SW_CERT_STATUS
:
1087 *confunc
= tls_construct_cert_status
;
1088 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1091 case TLS_ST_SW_FINISHED
:
1092 *confunc
= tls_construct_finished
;
1093 *mt
= SSL3_MT_FINISHED
;
1096 case TLS_ST_EARLY_DATA
:
1098 *mt
= SSL3_MT_DUMMY
;
1101 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1102 *confunc
= tls_construct_encrypted_extensions
;
1103 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1106 case TLS_ST_SW_KEY_UPDATE
:
1107 *confunc
= tls_construct_key_update
;
1108 *mt
= SSL3_MT_KEY_UPDATE
;
1116 * Maximum size (excluding the Handshake header) of a ClientHello message,
1117 * calculated as follows:
1119 * 2 + # client_version
1120 * 32 + # only valid length for random
1121 * 1 + # length of session_id
1122 * 32 + # maximum size for session_id
1123 * 2 + # length of cipher suites
1124 * 2^16-2 + # maximum length of cipher suites array
1125 * 1 + # length of compression_methods
1126 * 2^8-1 + # maximum length of compression methods
1127 * 2 + # length of extensions
1128 * 2^16-1 # maximum length of extensions
1130 #define CLIENT_HELLO_MAX_LENGTH 131396
1132 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1133 #define NEXT_PROTO_MAX_LENGTH 514
1136 * Returns the maximum allowed length for the current message that we are
1137 * reading. Excludes the message header.
1139 size_t ossl_statem_server_max_message_size(SSL_CONNECTION
*s
)
1141 OSSL_STATEM
*st
= &s
->statem
;
1143 switch (st
->hand_state
) {
1145 /* Shouldn't happen */
1148 case TLS_ST_SR_CLNT_HELLO
:
1149 return CLIENT_HELLO_MAX_LENGTH
;
1151 case TLS_ST_SR_END_OF_EARLY_DATA
:
1152 return END_OF_EARLY_DATA_MAX_LENGTH
;
1154 case TLS_ST_SR_CERT
:
1155 return s
->max_cert_list
;
1157 case TLS_ST_SR_KEY_EXCH
:
1158 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1160 case TLS_ST_SR_CERT_VRFY
:
1161 return SSL3_RT_MAX_PLAIN_LENGTH
;
1163 #ifndef OPENSSL_NO_NEXTPROTONEG
1164 case TLS_ST_SR_NEXT_PROTO
:
1165 return NEXT_PROTO_MAX_LENGTH
;
1168 case TLS_ST_SR_CHANGE
:
1169 return CCS_MAX_LENGTH
;
1171 case TLS_ST_SR_FINISHED
:
1172 return FINISHED_MAX_LENGTH
;
1174 case TLS_ST_SR_KEY_UPDATE
:
1175 return KEY_UPDATE_MAX_LENGTH
;
1180 * Process a message that the server has received from the client.
1182 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL_CONNECTION
*s
,
1185 OSSL_STATEM
*st
= &s
->statem
;
1187 switch (st
->hand_state
) {
1189 /* Shouldn't happen */
1190 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1191 return MSG_PROCESS_ERROR
;
1193 case TLS_ST_SR_CLNT_HELLO
:
1194 return tls_process_client_hello(s
, pkt
);
1196 case TLS_ST_SR_END_OF_EARLY_DATA
:
1197 return tls_process_end_of_early_data(s
, pkt
);
1199 case TLS_ST_SR_CERT
:
1200 return tls_process_client_certificate(s
, pkt
);
1202 case TLS_ST_SR_KEY_EXCH
:
1203 return tls_process_client_key_exchange(s
, pkt
);
1205 case TLS_ST_SR_CERT_VRFY
:
1206 return tls_process_cert_verify(s
, pkt
);
1208 #ifndef OPENSSL_NO_NEXTPROTONEG
1209 case TLS_ST_SR_NEXT_PROTO
:
1210 return tls_process_next_proto(s
, pkt
);
1213 case TLS_ST_SR_CHANGE
:
1214 return tls_process_change_cipher_spec(s
, pkt
);
1216 case TLS_ST_SR_FINISHED
:
1217 return tls_process_finished(s
, pkt
);
1219 case TLS_ST_SR_KEY_UPDATE
:
1220 return tls_process_key_update(s
, pkt
);
1226 * Perform any further processing required following the receipt of a message
1229 WORK_STATE
ossl_statem_server_post_process_message(SSL_CONNECTION
*s
,
1232 OSSL_STATEM
*st
= &s
->statem
;
1234 switch (st
->hand_state
) {
1236 /* Shouldn't happen */
1237 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1240 case TLS_ST_SR_CLNT_HELLO
:
1241 return tls_post_process_client_hello(s
, wst
);
1243 case TLS_ST_SR_KEY_EXCH
:
1244 return tls_post_process_client_key_exchange(s
, wst
);
1248 #ifndef OPENSSL_NO_SRP
1249 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1250 static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION
*s
)
1253 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1255 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1256 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1257 if (s
->srp_ctx
.login
== NULL
) {
1259 * RFC 5054 says SHOULD reject, we do so if There is no srp
1262 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1263 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1266 ret
= ssl_srp_server_param_with_username_intern(s
, &al
);
1269 if (ret
== SSL3_AL_FATAL
) {
1271 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1272 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1273 : SSL_R_CLIENTHELLO_TLSEXT
);
1282 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1285 /* Always use DTLS 1.0 version: see RFC 6347 */
1286 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1287 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1293 int dtls_construct_hello_verify_request(SSL_CONNECTION
*s
, WPACKET
*pkt
)
1295 unsigned int cookie_leni
;
1296 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1298 if (sctx
->app_gen_cookie_cb
== NULL
1299 || sctx
->app_gen_cookie_cb(SSL_CONNECTION_GET_SSL(s
), s
->d1
->cookie
,
1301 || cookie_leni
> DTLS1_COOKIE_LENGTH
) {
1302 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1305 s
->d1
->cookie_len
= cookie_leni
;
1307 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1308 s
->d1
->cookie_len
)) {
1309 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
1317 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1318 * SecureTransport using the TLS extension block in |hello|.
1319 * Safari, since 10.6, sends exactly these extensions, in this order:
1323 * signature_algorithms (for TLSv1.2 only)
1325 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1326 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1327 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1328 * 10.8..10.8.3 (which don't work).
1330 static void ssl_check_for_safari(SSL_CONNECTION
*s
,
1331 const CLIENTHELLO_MSG
*hello
)
1333 static const unsigned char kSafariExtensionsBlock
[] = {
1334 0x00, 0x0a, /* elliptic_curves extension */
1335 0x00, 0x08, /* 8 bytes */
1336 0x00, 0x06, /* 6 bytes of curve ids */
1337 0x00, 0x17, /* P-256 */
1338 0x00, 0x18, /* P-384 */
1339 0x00, 0x19, /* P-521 */
1341 0x00, 0x0b, /* ec_point_formats */
1342 0x00, 0x02, /* 2 bytes */
1343 0x01, /* 1 point format */
1344 0x00, /* uncompressed */
1345 /* The following is only present in TLS 1.2 */
1346 0x00, 0x0d, /* signature_algorithms */
1347 0x00, 0x0c, /* 12 bytes */
1348 0x00, 0x0a, /* 10 bytes */
1349 0x05, 0x01, /* SHA-384/RSA */
1350 0x04, 0x01, /* SHA-256/RSA */
1351 0x02, 0x01, /* SHA-1/RSA */
1352 0x04, 0x03, /* SHA-256/ECDSA */
1353 0x02, 0x03, /* SHA-1/ECDSA */
1355 /* Length of the common prefix (first two extensions). */
1356 static const size_t kSafariCommonExtensionsLength
= 18;
1361 tmppkt
= hello
->extensions
;
1363 if (!PACKET_forward(&tmppkt
, 2)
1364 || !PACKET_get_net_2(&tmppkt
, &type
)
1365 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1369 if (type
!= TLSEXT_TYPE_server_name
)
1372 ext_len
= TLS1_get_client_version(
1373 SSL_CONNECTION_GET_SSL(s
)) >= TLS1_2_VERSION
?
1374 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1376 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1380 #define RENEG_OPTIONS_OK(options) \
1381 ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
1382 && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
1384 MSG_PROCESS_RETURN
tls_process_client_hello(SSL_CONNECTION
*s
, PACKET
*pkt
)
1386 /* |cookie| will only be initialized for DTLS. */
1387 PACKET session_id
, compression
, extensions
, cookie
;
1388 static const unsigned char null_compression
= 0;
1389 CLIENTHELLO_MSG
*clienthello
= NULL
;
1391 /* Check if this is actually an unexpected renegotiation ClientHello */
1392 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1393 if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s
))) {
1394 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1397 if (!RENEG_OPTIONS_OK(s
->options
)
1398 || (!s
->s3
.send_connection_binding
1400 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1401 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1402 return MSG_PROCESS_FINISHED_READING
;
1408 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1409 if (clienthello
== NULL
) {
1410 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1415 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1417 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1418 PACKET_null_init(&cookie
);
1420 if (clienthello
->isv2
) {
1423 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1424 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1425 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
1430 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1431 * header is sent directly on the wire, not wrapped as a TLS
1432 * record. Our record layer just processes the message length and passes
1433 * the rest right through. Its format is:
1435 * 0-1 msg_length - decoded by the record layer
1436 * 2 msg_type - s->init_msg points here
1438 * 5-6 cipher_spec_length
1439 * 7-8 session_id_length
1440 * 9-10 challenge_length
1444 if (!PACKET_get_1(pkt
, &mt
)
1445 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1447 * Should never happen. We should have tested this in the record
1448 * layer in order to have determined that this is a SSLv2 record
1449 * in the first place
1451 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1456 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1457 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_TOO_SHORT
);
1461 /* Parse the message and load client random. */
1462 if (clienthello
->isv2
) {
1464 * Handle an SSLv2 backwards compatible ClientHello
1465 * Note, this is only for SSLv3+ using the backward compatible format.
1466 * Real SSLv2 is not supported, and is rejected below.
1468 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1471 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1472 || !PACKET_get_net_2(pkt
, &session_id_len
)
1473 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1474 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RECORD_LENGTH_MISMATCH
);
1478 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1479 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_LENGTH_MISMATCH
);
1483 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1485 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1486 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1487 /* No extensions. */
1488 || PACKET_remaining(pkt
) != 0) {
1489 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RECORD_LENGTH_MISMATCH
);
1492 clienthello
->session_id_len
= session_id_len
;
1494 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1495 * here rather than sizeof(clienthello->random) because that is the limit
1496 * for SSLv3 and it is fixed. It won't change even if
1497 * sizeof(clienthello->random) does.
1499 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1500 ? SSL3_RANDOM_SIZE
: challenge_len
;
1501 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1502 if (!PACKET_copy_bytes(&challenge
,
1503 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1504 challenge_len
, challenge_len
)
1505 /* Advertise only null compression. */
1506 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1507 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1511 PACKET_null_init(&clienthello
->extensions
);
1513 /* Regular ClientHello. */
1514 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1515 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1516 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1517 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1518 &clienthello
->session_id_len
)) {
1519 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1523 if (SSL_CONNECTION_IS_DTLS(s
)) {
1524 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1525 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1528 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1529 DTLS1_COOKIE_LENGTH
,
1530 &clienthello
->dtls_cookie_len
)) {
1531 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1535 * If we require cookies and this ClientHello doesn't contain one,
1536 * just return since we do not want to allocate any memory yet.
1537 * So check cookie length...
1539 if (SSL_get_options(SSL_CONNECTION_GET_SSL(s
)) & SSL_OP_COOKIE_EXCHANGE
) {
1540 if (clienthello
->dtls_cookie_len
== 0) {
1541 OPENSSL_free(clienthello
);
1542 return MSG_PROCESS_FINISHED_READING
;
1547 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1548 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1552 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1553 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1557 /* Could be empty. */
1558 if (PACKET_remaining(pkt
) == 0) {
1559 PACKET_null_init(&clienthello
->extensions
);
1561 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1562 || PACKET_remaining(pkt
) != 0) {
1563 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1569 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1570 MAX_COMPRESSIONS_SIZE
,
1571 &clienthello
->compressions_len
)) {
1572 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1576 /* Preserve the raw extensions PACKET for later use */
1577 extensions
= clienthello
->extensions
;
1578 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1579 &clienthello
->pre_proc_exts
,
1580 &clienthello
->pre_proc_exts_len
, 1)) {
1581 /* SSLfatal already been called */
1584 s
->clienthello
= clienthello
;
1586 return MSG_PROCESS_CONTINUE_PROCESSING
;
1589 if (clienthello
!= NULL
)
1590 OPENSSL_free(clienthello
->pre_proc_exts
);
1591 OPENSSL_free(clienthello
);
1593 return MSG_PROCESS_ERROR
;
1596 static int tls_early_post_process_client_hello(SSL_CONNECTION
*s
)
1599 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1603 #ifndef OPENSSL_NO_COMP
1604 SSL_COMP
*comp
= NULL
;
1606 const SSL_CIPHER
*c
;
1607 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1608 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1609 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1610 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1611 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1612 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1614 /* Finished parsing the ClientHello, now we can start processing it */
1615 /* Give the ClientHello callback a crack at things */
1616 if (sctx
->client_hello_cb
!= NULL
) {
1617 /* A failure in the ClientHello callback terminates the connection. */
1618 switch (sctx
->client_hello_cb(ssl
, &al
, sctx
->client_hello_cb_arg
)) {
1619 case SSL_CLIENT_HELLO_SUCCESS
:
1621 case SSL_CLIENT_HELLO_RETRY
:
1622 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1624 case SSL_CLIENT_HELLO_ERROR
:
1626 SSLfatal(s
, al
, SSL_R_CALLBACK_FAILED
);
1631 /* Set up the client_random */
1632 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1634 /* Choose the version */
1636 if (clienthello
->isv2
) {
1637 if (clienthello
->legacy_version
== SSL2_VERSION
1638 || (clienthello
->legacy_version
& 0xff00)
1639 != (SSL3_VERSION_MAJOR
<< 8)) {
1641 * This is real SSLv2 or something completely unknown. We don't
1644 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_UNKNOWN_PROTOCOL
);
1648 s
->client_version
= clienthello
->legacy_version
;
1651 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1652 * versions are potentially compatible. Version negotiation comes later.
1654 if (!SSL_CONNECTION_IS_DTLS(s
)) {
1655 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1656 } else if (ssl
->method
->version
!= DTLS_ANY_VERSION
&&
1657 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1658 protverr
= SSL_R_VERSION_TOO_LOW
;
1664 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1665 /* like ssl3_get_record, send alert using remote version number */
1666 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1668 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, protverr
);
1672 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1673 if (SSL_CONNECTION_IS_TLS13(s
)
1674 && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1675 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
1679 if (SSL_CONNECTION_IS_DTLS(s
)) {
1680 /* Empty cookie was already handled above by returning early. */
1681 if (SSL_get_options(ssl
) & SSL_OP_COOKIE_EXCHANGE
) {
1682 if (sctx
->app_verify_cookie_cb
!= NULL
) {
1683 if (sctx
->app_verify_cookie_cb(ssl
, clienthello
->dtls_cookie
,
1684 clienthello
->dtls_cookie_len
) == 0) {
1685 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1686 SSL_R_COOKIE_MISMATCH
);
1688 /* else cookie verification succeeded */
1690 /* default verification */
1691 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1692 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1693 s
->d1
->cookie_len
) != 0) {
1694 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_COOKIE_MISMATCH
);
1697 s
->d1
->cookie_verified
= 1;
1699 if (ssl
->method
->version
== DTLS_ANY_VERSION
) {
1700 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1701 if (protverr
!= 0) {
1702 s
->version
= s
->client_version
;
1703 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, protverr
);
1711 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1712 clienthello
->isv2
) ||
1713 !ossl_bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
,
1714 &scsvs
, clienthello
->isv2
, 1)) {
1715 /* SSLfatal() already called */
1719 s
->s3
.send_connection_binding
= 0;
1720 /* Check what signalling cipher-suite values were received. */
1721 if (scsvs
!= NULL
) {
1722 for (i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1723 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1724 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1725 if (s
->renegotiate
) {
1726 /* SCSV is fatal if renegotiating */
1727 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1728 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1731 s
->s3
.send_connection_binding
= 1;
1732 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1733 !ssl_check_version_downgrade(s
)) {
1735 * This SCSV indicates that the client previously tried
1736 * a higher version. We should fail if the current version
1737 * is an unexpected downgrade, as that indicates that the first
1738 * connection may have been tampered with in order to trigger
1739 * an insecure downgrade.
1741 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1742 SSL_R_INAPPROPRIATE_FALLBACK
);
1748 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1749 if (SSL_CONNECTION_IS_TLS13(s
)) {
1750 const SSL_CIPHER
*cipher
=
1751 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(ssl
));
1753 if (cipher
== NULL
) {
1754 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_SHARED_CIPHER
);
1757 if (s
->hello_retry_request
== SSL_HRR_PENDING
1758 && (s
->s3
.tmp
.new_cipher
== NULL
1759 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1761 * A previous HRR picked a different ciphersuite to the one we
1762 * just selected. Something must have changed.
1764 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_CIPHER
);
1767 s
->s3
.tmp
.new_cipher
= cipher
;
1770 /* We need to do this before getting the session */
1771 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1772 SSL_EXT_CLIENT_HELLO
,
1773 clienthello
->pre_proc_exts
, NULL
, 0)) {
1774 /* SSLfatal() already called */
1779 * We don't allow resumption in a backwards compatible ClientHello.
1780 * In TLS1.1+, session_id MUST be empty.
1782 * Versions before 0.9.7 always allow clients to resume sessions in
1783 * renegotiation. 0.9.7 and later allow this by default, but optionally
1784 * ignore resumption requests with flag
1785 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1786 * than a change to default behavior so that applications relying on
1787 * this for security won't even compile against older library versions).
1788 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1789 * request renegotiation but not a new session (s->new_session remains
1790 * unset): for servers, this essentially just means that the
1791 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1794 if (clienthello
->isv2
||
1796 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1797 if (!ssl_get_new_session(s
, 1)) {
1798 /* SSLfatal() already called */
1802 i
= ssl_get_prev_session(s
, clienthello
);
1804 /* previous session */
1806 } else if (i
== -1) {
1807 /* SSLfatal() already called */
1811 if (!ssl_get_new_session(s
, 1)) {
1812 /* SSLfatal() already called */
1818 if (SSL_CONNECTION_IS_TLS13(s
)) {
1819 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1820 s
->clienthello
->session_id_len
);
1821 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1825 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1826 * ciphersuite compatibility with the session as part of resumption.
1828 if (!SSL_CONNECTION_IS_TLS13(s
) && s
->hit
) {
1830 id
= s
->session
->cipher
->id
;
1832 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1833 BIO_printf(trc_out
, "client sent %d ciphers\n",
1834 sk_SSL_CIPHER_num(ciphers
));
1836 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1837 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1838 if (trc_out
!= NULL
)
1839 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1840 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1848 * we need to have the cipher in the cipher list if we are asked
1851 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1852 SSL_R_REQUIRED_CIPHER_MISSING
);
1853 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1856 OSSL_TRACE_END(TLS_CIPHER
);
1859 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1860 if (clienthello
->compressions
[loop
] == 0)
1864 if (loop
>= clienthello
->compressions_len
) {
1866 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1870 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1871 ssl_check_for_safari(s
, clienthello
);
1873 /* TLS extensions */
1874 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1875 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1876 /* SSLfatal() already called */
1881 * Check if we want to use external pre-shared secret for this handshake
1882 * for not reused session only. We need to generate server_random before
1883 * calling tls_session_secret_cb in order to allow SessionTicket
1884 * processing to use it in key derivation.
1888 pos
= s
->s3
.server_random
;
1889 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1890 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1896 && s
->version
>= TLS1_VERSION
1897 && !SSL_CONNECTION_IS_TLS13(s
)
1898 && !SSL_CONNECTION_IS_DTLS(s
)
1899 && s
->ext
.session_secret_cb
!= NULL
) {
1900 const SSL_CIPHER
*pref_cipher
= NULL
;
1902 * s->session->master_key_length is a size_t, but this is an int for
1903 * backwards compat reasons
1905 int master_key_length
;
1907 master_key_length
= sizeof(s
->session
->master_key
);
1908 if (s
->ext
.session_secret_cb(ssl
, s
->session
->master_key
,
1909 &master_key_length
, ciphers
,
1911 s
->ext
.session_secret_cb_arg
)
1912 && master_key_length
> 0) {
1913 s
->session
->master_key_length
= master_key_length
;
1915 s
->peer_ciphers
= ciphers
;
1916 s
->session
->verify_result
= X509_V_OK
;
1920 /* check if some cipher was preferred by call back */
1921 if (pref_cipher
== NULL
)
1922 pref_cipher
= ssl3_choose_cipher(s
, s
->peer_ciphers
,
1923 SSL_get_ciphers(ssl
));
1924 if (pref_cipher
== NULL
) {
1925 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_SHARED_CIPHER
);
1929 s
->session
->cipher
= pref_cipher
;
1930 sk_SSL_CIPHER_free(s
->cipher_list
);
1931 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1932 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1933 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1938 * Worst case, we will use the NULL compression, but if we have other
1939 * options, we will now look for them. We have complen-1 compression
1940 * algorithms from the client, starting at q.
1942 s
->s3
.tmp
.new_compression
= NULL
;
1943 if (SSL_CONNECTION_IS_TLS13(s
)) {
1945 * We already checked above that the NULL compression method appears in
1946 * the list. Now we check there aren't any others (which is illegal in
1947 * a TLSv1.3 ClientHello.
1949 if (clienthello
->compressions_len
!= 1) {
1950 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1951 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1955 #ifndef OPENSSL_NO_COMP
1956 /* This only happens if we have a cache hit */
1957 else if (s
->session
->compress_meth
!= 0) {
1958 int m
, comp_id
= s
->session
->compress_meth
;
1960 /* Perform sanity checks on resumed compression algorithm */
1961 /* Can't disable compression */
1962 if (!ssl_allow_compression(s
)) {
1963 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1964 SSL_R_INCONSISTENT_COMPRESSION
);
1967 /* Look for resumed compression method */
1968 for (m
= 0; m
< sk_SSL_COMP_num(sctx
->comp_methods
); m
++) {
1969 comp
= sk_SSL_COMP_value(sctx
->comp_methods
, m
);
1970 if (comp_id
== comp
->id
) {
1971 s
->s3
.tmp
.new_compression
= comp
;
1975 if (s
->s3
.tmp
.new_compression
== NULL
) {
1976 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1977 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1980 /* Look for resumed method in compression list */
1981 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
1982 if (clienthello
->compressions
[k
] == comp_id
)
1985 if (k
>= clienthello
->compressions_len
) {
1986 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1987 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
1990 } else if (s
->hit
) {
1992 } else if (ssl_allow_compression(s
) && sctx
->comp_methods
) {
1993 /* See if we have a match */
1994 int m
, nn
, v
, done
= 0;
1997 nn
= sk_SSL_COMP_num(sctx
->comp_methods
);
1998 for (m
= 0; m
< nn
; m
++) {
1999 comp
= sk_SSL_COMP_value(sctx
->comp_methods
, m
);
2001 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2002 if (v
== clienthello
->compressions
[o
]) {
2011 s
->s3
.tmp
.new_compression
= comp
;
2017 * If compression is disabled we'd better not try to resume a session
2018 * using compression.
2020 if (s
->session
->compress_meth
!= 0) {
2021 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_INCONSISTENT_COMPRESSION
);
2027 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2030 if (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
)) {
2031 sk_SSL_CIPHER_free(s
->peer_ciphers
);
2032 s
->peer_ciphers
= ciphers
;
2033 if (ciphers
== NULL
) {
2034 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2041 #ifdef OPENSSL_NO_COMP
2042 s
->session
->compress_meth
= 0;
2044 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2046 if (!tls1_set_server_sigalgs(s
)) {
2047 /* SSLfatal() already called */
2052 sk_SSL_CIPHER_free(ciphers
);
2053 sk_SSL_CIPHER_free(scsvs
);
2054 OPENSSL_free(clienthello
->pre_proc_exts
);
2055 OPENSSL_free(s
->clienthello
);
2056 s
->clienthello
= NULL
;
2059 sk_SSL_CIPHER_free(ciphers
);
2060 sk_SSL_CIPHER_free(scsvs
);
2061 OPENSSL_free(clienthello
->pre_proc_exts
);
2062 OPENSSL_free(s
->clienthello
);
2063 s
->clienthello
= NULL
;
2069 * Call the status request callback if needed. Upon success, returns 1.
2070 * Upon failure, returns 0.
2072 static int tls_handle_status_request(SSL_CONNECTION
*s
)
2074 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2076 s
->ext
.status_expected
= 0;
2079 * If status request then ask callback what to do. Note: this must be
2080 * called after servername callbacks in case the certificate has changed,
2081 * and must be called after the cipher has been chosen because this may
2082 * influence which certificate is sent
2084 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& sctx
!= NULL
2085 && sctx
->ext
.status_cb
!= NULL
) {
2088 /* If no certificate can't return certificate status */
2089 if (s
->s3
.tmp
.cert
!= NULL
) {
2091 * Set current certificate to one we will use so SSL_get_certificate
2092 * et al can pick it up.
2094 s
->cert
->key
= s
->s3
.tmp
.cert
;
2095 ret
= sctx
->ext
.status_cb(SSL_CONNECTION_GET_SSL(s
),
2096 sctx
->ext
.status_arg
);
2098 /* We don't want to send a status request response */
2099 case SSL_TLSEXT_ERR_NOACK
:
2100 s
->ext
.status_expected
= 0;
2102 /* status request response should be sent */
2103 case SSL_TLSEXT_ERR_OK
:
2104 if (s
->ext
.ocsp
.resp
)
2105 s
->ext
.status_expected
= 1;
2107 /* something bad happened */
2108 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2110 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CLIENTHELLO_TLSEXT
);
2120 * Call the alpn_select callback if needed. Upon success, returns 1.
2121 * Upon failure, returns 0.
2123 int tls_handle_alpn(SSL_CONNECTION
*s
)
2125 const unsigned char *selected
= NULL
;
2126 unsigned char selected_len
= 0;
2127 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2129 if (sctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2130 int r
= sctx
->ext
.alpn_select_cb(SSL_CONNECTION_GET_SSL(s
),
2131 &selected
, &selected_len
,
2132 s
->s3
.alpn_proposed
,
2133 (unsigned int)s
->s3
.alpn_proposed_len
,
2134 sctx
->ext
.alpn_select_cb_arg
);
2136 if (r
== SSL_TLSEXT_ERR_OK
) {
2137 OPENSSL_free(s
->s3
.alpn_selected
);
2138 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2139 if (s
->s3
.alpn_selected
== NULL
) {
2140 s
->s3
.alpn_selected_len
= 0;
2141 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2144 s
->s3
.alpn_selected_len
= selected_len
;
2145 #ifndef OPENSSL_NO_NEXTPROTONEG
2146 /* ALPN takes precedence over NPN. */
2150 /* Check ALPN is consistent with session */
2151 if (s
->session
->ext
.alpn_selected
== NULL
2152 || selected_len
!= s
->session
->ext
.alpn_selected_len
2153 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2154 selected_len
) != 0) {
2155 /* Not consistent so can't be used for early_data */
2156 s
->ext
.early_data_ok
= 0;
2160 * This is a new session and so alpn_selected should have
2161 * been initialised to NULL. We should update it with the
2164 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2165 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2166 ERR_R_INTERNAL_ERROR
);
2169 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2171 if (s
->session
->ext
.alpn_selected
== NULL
) {
2172 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2173 ERR_R_INTERNAL_ERROR
);
2176 s
->session
->ext
.alpn_selected_len
= selected_len
;
2181 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2182 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
,
2183 SSL_R_NO_APPLICATION_PROTOCOL
);
2187 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2192 /* Check ALPN is consistent with session */
2193 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2194 /* Not consistent so can't be used for early_data */
2195 s
->ext
.early_data_ok
= 0;
2201 WORK_STATE
tls_post_process_client_hello(SSL_CONNECTION
*s
, WORK_STATE wst
)
2203 const SSL_CIPHER
*cipher
;
2204 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2206 if (wst
== WORK_MORE_A
) {
2207 int rv
= tls_early_post_process_client_hello(s
);
2209 /* SSLfatal() was already called */
2216 if (wst
== WORK_MORE_B
) {
2217 if (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
)) {
2218 /* Let cert callback update server certificates if required */
2219 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2220 int rv
= s
->cert
->cert_cb(ssl
, s
->cert
->cert_cb_arg
);
2222 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CERT_CB_ERROR
);
2226 s
->rwstate
= SSL_X509_LOOKUP
;
2229 s
->rwstate
= SSL_NOTHING
;
2232 /* In TLSv1.3 we selected the ciphersuite before resumption */
2233 if (!SSL_CONNECTION_IS_TLS13(s
)) {
2235 ssl3_choose_cipher(s
, s
->peer_ciphers
,
2236 SSL_get_ciphers(ssl
));
2238 if (cipher
== NULL
) {
2239 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2240 SSL_R_NO_SHARED_CIPHER
);
2243 s
->s3
.tmp
.new_cipher
= cipher
;
2246 if (!tls_choose_sigalg(s
, 1)) {
2247 /* SSLfatal already called */
2250 /* check whether we should disable session resumption */
2251 if (s
->not_resumable_session_cb
!= NULL
)
2252 s
->session
->not_resumable
=
2253 s
->not_resumable_session_cb(ssl
,
2254 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2255 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2256 if (s
->session
->not_resumable
)
2257 /* do not send a session ticket */
2258 s
->ext
.ticket_expected
= 0;
2261 /* Session-id reuse */
2262 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2266 * we now have the following setup.
2268 * cipher_list - our preferred list of ciphers
2269 * ciphers - the clients preferred list of ciphers
2270 * compression - basically ignored right now
2271 * ssl version is set - sslv3
2272 * s->session - The ssl session has been setup.
2273 * s->hit - session reuse flag
2274 * s->s3.tmp.new_cipher - the new cipher to use.
2278 * Call status_request callback if needed. Has to be done after the
2279 * certificate callbacks etc above.
2281 if (!tls_handle_status_request(s
)) {
2282 /* SSLfatal() already called */
2286 * Call alpn_select callback if needed. Has to be done after SNI and
2287 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2288 * we already did this because cipher negotiation happens earlier, and
2289 * we must handle ALPN before we decide whether to accept early_data.
2291 if (!SSL_CONNECTION_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2292 /* SSLfatal() already called */
2298 #ifndef OPENSSL_NO_SRP
2299 if (wst
== WORK_MORE_C
) {
2301 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2303 * callback indicates further work to be done
2305 s
->rwstate
= SSL_X509_LOOKUP
;
2309 /* SSLfatal() already called */
2315 return WORK_FINISHED_STOP
;
2320 int tls_construct_server_hello(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2325 unsigned char *session_id
;
2326 int usetls13
= SSL_CONNECTION_IS_TLS13(s
)
2327 || s
->hello_retry_request
== SSL_HRR_PENDING
;
2329 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2330 if (!WPACKET_put_bytes_u16(pkt
, version
)
2332 * Random stuff. Filling of the server_random takes place in
2333 * tls_process_client_hello()
2335 || !WPACKET_memcpy(pkt
,
2336 s
->hello_retry_request
== SSL_HRR_PENDING
2337 ? hrrrandom
: s
->s3
.server_random
,
2338 SSL3_RANDOM_SIZE
)) {
2339 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2344 * There are several cases for the session ID to send
2345 * back in the server hello:
2346 * - For session reuse from the session cache,
2347 * we send back the old session ID.
2348 * - If stateless session reuse (using a session ticket)
2349 * is successful, we send back the client's "session ID"
2350 * (which doesn't actually identify the session).
2351 * - If it is a new session, we send back the new
2353 * - However, if we want the new session to be single-use,
2354 * we send back a 0-length session ID.
2355 * - In TLSv1.3 we echo back the session id sent to us by the client
2357 * s->hit is non-zero in either case of session reuse,
2358 * so the following won't overwrite an ID that we're supposed
2361 if (s
->session
->not_resumable
||
2362 (!(SSL_CONNECTION_GET_CTX(s
)->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2364 s
->session
->session_id_length
= 0;
2367 sl
= s
->tmp_session_id_len
;
2368 session_id
= s
->tmp_session_id
;
2370 sl
= s
->session
->session_id_length
;
2371 session_id
= s
->session
->session_id
;
2374 if (sl
> sizeof(s
->session
->session_id
)) {
2375 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2379 /* set up the compression method */
2380 #ifdef OPENSSL_NO_COMP
2383 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2386 compm
= s
->s3
.tmp
.new_compression
->id
;
2389 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2390 || !SSL_CONNECTION_GET_SSL(s
)->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
,
2392 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2393 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2397 if (!tls_construct_extensions(s
, pkt
,
2398 s
->hello_retry_request
== SSL_HRR_PENDING
2399 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2400 : (SSL_CONNECTION_IS_TLS13(s
)
2401 ? SSL_EXT_TLS1_3_SERVER_HELLO
2402 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2404 /* SSLfatal() already called */
2408 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2409 /* Ditch the session. We'll create a new one next time around */
2410 SSL_SESSION_free(s
->session
);
2415 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2416 * a synthetic message_hash in place of ClientHello1.
2418 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2419 /* SSLfatal() already called */
2422 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2423 && !ssl3_digest_cached_records(s
, 0)) {
2424 /* SSLfatal() already called */;
2431 int tls_construct_server_done(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2433 if (!s
->s3
.tmp
.cert_request
) {
2434 if (!ssl3_digest_cached_records(s
, 0)) {
2435 /* SSLfatal() already called */
2442 int tls_construct_server_key_exchange(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2444 EVP_PKEY
*pkdh
= NULL
;
2445 unsigned char *encodedPoint
= NULL
;
2446 size_t encodedlen
= 0;
2448 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2452 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2453 EVP_PKEY_CTX
*pctx
= NULL
;
2454 size_t paramlen
, paramoffset
;
2455 int freer
= 0, ret
= 0;
2456 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2458 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2459 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2463 if (md_ctx
== NULL
) {
2464 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2468 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2470 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2471 #ifndef OPENSSL_NO_PSK
2472 /* Plain PSK or RSAPSK nothing to do */
2473 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2475 #endif /* !OPENSSL_NO_PSK */
2476 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2477 CERT
*cert
= s
->cert
;
2478 EVP_PKEY
*pkdhp
= NULL
;
2480 if (s
->cert
->dh_tmp_auto
) {
2481 pkdh
= ssl_get_auto_dh(s
);
2483 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2488 pkdhp
= cert
->dh_tmp
;
2490 #if !defined(OPENSSL_NO_DEPRECATED_3_0)
2491 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2492 pkdh
= ssl_dh_to_pkey(s
->cert
->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s
),
2495 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2501 if (pkdhp
== NULL
) {
2502 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
2505 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2506 EVP_PKEY_get_security_bits(pkdhp
), 0, pkdhp
)) {
2507 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_DH_KEY_TOO_SMALL
);
2510 if (s
->s3
.tmp
.pkey
!= NULL
) {
2511 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2515 s
->s3
.tmp
.pkey
= ssl_generate_pkey(s
, pkdhp
);
2516 if (s
->s3
.tmp
.pkey
== NULL
) {
2517 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2521 EVP_PKEY_free(pkdh
);
2524 /* These BIGNUMs need to be freed when we're finished */
2526 if (!EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
, OSSL_PKEY_PARAM_FFC_P
,
2528 || !EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
, OSSL_PKEY_PARAM_FFC_G
,
2530 || !EVP_PKEY_get_bn_param(s
->s3
.tmp
.pkey
,
2531 OSSL_PKEY_PARAM_PUB_KEY
, &r
[2])) {
2532 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2535 } else if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2537 if (s
->s3
.tmp
.pkey
!= NULL
) {
2538 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2542 /* Get NID of appropriate shared curve */
2543 curve_id
= tls1_shared_group(s
, -2);
2544 if (curve_id
== 0) {
2545 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2546 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2549 /* Cache the group used in the SSL_SESSION */
2550 s
->session
->kex_group
= curve_id
;
2551 /* Generate a new key for this curve */
2552 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2553 if (s
->s3
.tmp
.pkey
== NULL
) {
2554 /* SSLfatal() already called */
2558 /* Encode the public key. */
2559 encodedlen
= EVP_PKEY_get1_encoded_public_key(s
->s3
.tmp
.pkey
,
2561 if (encodedlen
== 0) {
2562 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
2567 * We'll generate the serverKeyExchange message explicitly so we
2568 * can set these to NULLs
2575 #ifndef OPENSSL_NO_SRP
2576 if (type
& SSL_kSRP
) {
2577 if ((s
->srp_ctx
.N
== NULL
) ||
2578 (s
->srp_ctx
.g
== NULL
) ||
2579 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2580 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_SRP_PARAM
);
2583 r
[0] = s
->srp_ctx
.N
;
2584 r
[1] = s
->srp_ctx
.g
;
2585 r
[2] = s
->srp_ctx
.s
;
2586 r
[3] = s
->srp_ctx
.B
;
2590 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2594 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2595 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2597 } else if (lu
== NULL
) {
2598 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_INTERNAL_ERROR
);
2602 #ifndef OPENSSL_NO_PSK
2603 if (type
& SSL_PSK
) {
2604 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2605 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2608 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2609 * checked this when we set the identity hint - but just in case
2611 if (len
> PSK_MAX_IDENTITY_LEN
2612 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2614 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2620 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2621 unsigned char *binval
;
2624 #ifndef OPENSSL_NO_SRP
2625 if ((i
== 2) && (type
& SSL_kSRP
)) {
2626 res
= WPACKET_start_sub_packet_u8(pkt
);
2629 res
= WPACKET_start_sub_packet_u16(pkt
);
2632 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2637 * for interoperability with some versions of the Microsoft TLS
2638 * stack, we need to zero pad the DHE pub key to the same length
2641 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2642 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2645 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2646 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2649 memset(binval
, 0, len
);
2653 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2654 || !WPACKET_close(pkt
)) {
2655 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2659 BN_bn2bin(r
[i
], binval
);
2662 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2664 * We only support named (not generic) curves. In this situation, the
2665 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2666 * [1 byte length of encoded point], followed by the actual encoded
2669 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2670 || !WPACKET_put_bytes_u8(pkt
, 0)
2671 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2672 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2673 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2676 OPENSSL_free(encodedPoint
);
2677 encodedPoint
= NULL
;
2682 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2684 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2685 size_t siglen
= 0, tbslen
;
2687 if (pkey
== NULL
|| !tls1_lookup_md(sctx
, lu
, &md
)) {
2688 /* Should never happen */
2689 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2692 /* Get length of the parameters we have written above */
2693 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2694 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2697 /* send signature algorithm */
2698 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2699 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2703 if (EVP_DigestSignInit_ex(md_ctx
, &pctx
,
2704 md
== NULL
? NULL
: EVP_MD_get0_name(md
),
2705 sctx
->libctx
, sctx
->propq
, pkey
,
2707 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2710 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2711 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2712 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2713 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2717 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2718 s
->init_buf
->data
+ paramoffset
,
2721 /* SSLfatal() already called */
2725 if (EVP_DigestSign(md_ctx
, NULL
, &siglen
, tbs
, tbslen
) <=0
2726 || !WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2727 || EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
) <= 0
2728 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2729 || sigbytes1
!= sigbytes2
) {
2731 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2739 EVP_PKEY_free(pkdh
);
2740 OPENSSL_free(encodedPoint
);
2741 EVP_MD_CTX_free(md_ctx
);
2751 int tls_construct_certificate_request(SSL_CONNECTION
*s
, WPACKET
*pkt
)
2753 if (SSL_CONNECTION_IS_TLS13(s
)) {
2754 /* Send random context when doing post-handshake auth */
2755 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2756 OPENSSL_free(s
->pha_context
);
2757 s
->pha_context_len
= 32;
2758 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
) {
2759 s
->pha_context_len
= 0;
2760 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2763 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
2764 s
->pha_context
, s
->pha_context_len
, 0) <= 0
2765 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
,
2766 s
->pha_context_len
)) {
2767 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2770 /* reset the handshake hash back to just after the ClientFinished */
2771 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2772 /* SSLfatal() already called */
2776 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2777 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2782 if (!tls_construct_extensions(s
, pkt
,
2783 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2785 /* SSLfatal() already called */
2791 /* get the list of acceptable cert types */
2792 if (!WPACKET_start_sub_packet_u8(pkt
)
2793 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2794 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2798 if (SSL_USE_SIGALGS(s
)) {
2799 const uint16_t *psigs
;
2800 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2802 if (!WPACKET_start_sub_packet_u16(pkt
)
2803 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2804 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2805 || !WPACKET_close(pkt
)) {
2806 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2811 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2812 /* SSLfatal() already called */
2818 s
->s3
.tmp
.cert_request
= 1;
2822 static int tls_process_cke_psk_preamble(SSL_CONNECTION
*s
, PACKET
*pkt
)
2824 #ifndef OPENSSL_NO_PSK
2825 unsigned char psk
[PSK_MAX_PSK_LEN
];
2827 PACKET psk_identity
;
2829 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2830 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2833 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2834 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DATA_LENGTH_TOO_LONG
);
2837 if (s
->psk_server_callback
== NULL
) {
2838 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_PSK_NO_SERVER_CB
);
2842 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2843 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2847 psklen
= s
->psk_server_callback(SSL_CONNECTION_GET_SSL(s
),
2848 s
->session
->psk_identity
,
2851 if (psklen
> PSK_MAX_PSK_LEN
) {
2852 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2854 } else if (psklen
== 0) {
2856 * PSK related to the given identity not found
2858 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
, SSL_R_PSK_IDENTITY_NOT_FOUND
);
2862 OPENSSL_free(s
->s3
.tmp
.psk
);
2863 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2864 OPENSSL_cleanse(psk
, psklen
);
2866 if (s
->s3
.tmp
.psk
== NULL
) {
2867 s
->s3
.tmp
.psklen
= 0;
2868 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2872 s
->s3
.tmp
.psklen
= psklen
;
2876 /* Should never happen */
2877 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2882 static int tls_process_cke_rsa(SSL_CONNECTION
*s
, PACKET
*pkt
)
2885 PACKET enc_premaster
;
2886 EVP_PKEY
*rsa
= NULL
;
2887 unsigned char *rsa_decrypt
= NULL
;
2889 EVP_PKEY_CTX
*ctx
= NULL
;
2890 OSSL_PARAM params
[3], *p
= params
;
2891 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
2893 rsa
= s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
;
2895 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_RSA_CERTIFICATE
);
2899 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2900 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2901 enc_premaster
= *pkt
;
2903 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2904 || PACKET_remaining(pkt
) != 0) {
2905 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2910 outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
2911 rsa_decrypt
= OPENSSL_malloc(outlen
);
2912 if (rsa_decrypt
== NULL
) {
2913 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2917 ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, rsa
, sctx
->propq
);
2919 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2924 * We must not leak whether a decryption failure occurs because of
2925 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2926 * section 7.4.7.1). We use the special padding type
2927 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
2928 * RSA, check the padding and check that the client version is as expected
2929 * in the premaster secret. If any of that fails then the function appears
2930 * to return successfully but with a random result. The call below could
2931 * still fail if the input is publicly invalid.
2932 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2934 if (EVP_PKEY_decrypt_init(ctx
) <= 0
2935 || EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_WITH_TLS_PADDING
) <= 0) {
2936 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2940 *p
++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
,
2941 (unsigned int *)&s
->client_version
);
2942 if ((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) != 0)
2943 *p
++ = OSSL_PARAM_construct_uint(
2944 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
,
2945 (unsigned int *)&s
->version
);
2946 *p
++ = OSSL_PARAM_construct_end();
2948 if (!EVP_PKEY_CTX_set_params(ctx
, params
)
2949 || EVP_PKEY_decrypt(ctx
, rsa_decrypt
, &outlen
,
2950 PACKET_data(&enc_premaster
),
2951 PACKET_remaining(&enc_premaster
)) <= 0) {
2952 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2957 * This test should never fail (otherwise we should have failed above) but
2958 * we double check anyway.
2960 if (outlen
!= SSL_MAX_MASTER_KEY_LENGTH
) {
2961 OPENSSL_cleanse(rsa_decrypt
, SSL_MAX_MASTER_KEY_LENGTH
);
2962 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DECRYPTION_FAILED
);
2966 /* Also cleanses rsa_decrypt (on success or failure) */
2967 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
2968 SSL_MAX_MASTER_KEY_LENGTH
, 0)) {
2969 /* SSLfatal() already called */
2975 OPENSSL_free(rsa_decrypt
);
2976 EVP_PKEY_CTX_free(ctx
);
2980 static int tls_process_cke_dhe(SSL_CONNECTION
*s
, PACKET
*pkt
)
2982 EVP_PKEY
*skey
= NULL
;
2984 const unsigned char *data
;
2985 EVP_PKEY
*ckey
= NULL
;
2988 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
2989 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2992 skey
= s
->s3
.tmp
.pkey
;
2994 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
2998 if (PACKET_remaining(pkt
) == 0L) {
2999 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_MISSING_TMP_DH_KEY
);
3002 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3003 /* We already checked we have enough data */
3004 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3007 ckey
= EVP_PKEY_new();
3008 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3009 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
3013 if (!EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
)) {
3014 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3018 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3019 /* SSLfatal() already called */
3024 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3025 s
->s3
.tmp
.pkey
= NULL
;
3027 EVP_PKEY_free(ckey
);
3031 static int tls_process_cke_ecdhe(SSL_CONNECTION
*s
, PACKET
*pkt
)
3033 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3034 EVP_PKEY
*ckey
= NULL
;
3037 if (PACKET_remaining(pkt
) == 0L) {
3038 /* We don't support ECDH client auth */
3039 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_MISSING_TMP_ECDH_KEY
);
3043 const unsigned char *data
;
3046 * Get client's public key from encoded point in the
3047 * ClientKeyExchange message.
3050 /* Get encoded point length */
3051 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3052 || PACKET_remaining(pkt
) != 0) {
3053 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3057 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_MISSING_TMP_ECDH_KEY
);
3061 ckey
= EVP_PKEY_new();
3062 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3063 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
3067 if (EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
) <= 0) {
3068 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
3073 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3074 /* SSLfatal() already called */
3079 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3080 s
->s3
.tmp
.pkey
= NULL
;
3082 EVP_PKEY_free(ckey
);
3087 static int tls_process_cke_srp(SSL_CONNECTION
*s
, PACKET
*pkt
)
3089 #ifndef OPENSSL_NO_SRP
3091 const unsigned char *data
;
3093 if (!PACKET_get_net_2(pkt
, &i
)
3094 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3095 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_SRP_A_LENGTH
);
3098 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3099 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BN_LIB
);
3102 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3103 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_SRP_PARAMETERS
);
3106 OPENSSL_free(s
->session
->srp_username
);
3107 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3108 if (s
->session
->srp_username
== NULL
) {
3109 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3113 if (!srp_generate_server_master_secret(s
)) {
3114 /* SSLfatal() already called */
3120 /* Should never happen */
3121 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3126 static int tls_process_cke_gost(SSL_CONNECTION
*s
, PACKET
*pkt
)
3128 #ifndef OPENSSL_NO_GOST
3129 EVP_PKEY_CTX
*pkey_ctx
;
3130 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3131 unsigned char premaster_secret
[32];
3132 const unsigned char *start
;
3133 size_t outlen
= 32, inlen
;
3134 unsigned long alg_a
;
3135 GOST_KX_MESSAGE
*pKX
= NULL
;
3136 const unsigned char *ptr
;
3138 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3140 /* Get our certificate private key */
3141 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3142 if (alg_a
& SSL_aGOST12
) {
3144 * New GOST ciphersuites have SSL_aGOST01 bit too
3146 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3148 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3151 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3153 } else if (alg_a
& SSL_aGOST01
) {
3154 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3157 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, pk
, sctx
->propq
);
3158 if (pkey_ctx
== NULL
) {
3159 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3162 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3163 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3167 * If client certificate is present and is of the same type, maybe
3168 * use it for key exchange. Don't mind errors from
3169 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3170 * client certificate for authorization only.
3172 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3173 if (client_pub_pkey
) {
3174 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3178 ptr
= PACKET_data(pkt
);
3179 /* Some implementations provide extra data in the opaqueBlob
3180 * We have nothing to do with this blob so we just skip it */
3181 pKX
= d2i_GOST_KX_MESSAGE(NULL
, &ptr
, PACKET_remaining(pkt
));
3183 || pKX
->kxBlob
== NULL
3184 || ASN1_TYPE_get(pKX
->kxBlob
) != V_ASN1_SEQUENCE
) {
3185 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3189 if (!PACKET_forward(pkt
, ptr
- PACKET_data(pkt
))) {
3190 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_DECRYPTION_FAILED
);
3194 if (PACKET_remaining(pkt
) != 0) {
3195 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_DECRYPTION_FAILED
);
3199 inlen
= pKX
->kxBlob
->value
.sequence
->length
;
3200 start
= pKX
->kxBlob
->value
.sequence
->data
;
3202 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3204 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3207 /* Generate master secret */
3208 if (!ssl_generate_master_secret(s
, premaster_secret
,
3209 sizeof(premaster_secret
), 0)) {
3210 /* SSLfatal() already called */
3213 /* Check if pubkey from client certificate was used */
3214 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3216 s
->statem
.no_cert_verify
= 1;
3220 EVP_PKEY_CTX_free(pkey_ctx
);
3221 GOST_KX_MESSAGE_free(pKX
);
3224 /* Should never happen */
3225 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3230 static int tls_process_cke_gost18(SSL_CONNECTION
*s
, PACKET
*pkt
)
3232 #ifndef OPENSSL_NO_GOST
3233 unsigned char rnd_dgst
[32];
3234 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3235 EVP_PKEY
*pk
= NULL
;
3236 unsigned char premaster_secret
[32];
3237 const unsigned char *start
= NULL
;
3238 size_t outlen
= 32, inlen
= 0;
3240 int cipher_nid
= ossl_gost18_cke_cipher_nid(s
);
3241 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3243 if (cipher_nid
== NID_undef
) {
3244 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3248 if (ossl_gost_ukm(s
, rnd_dgst
) <= 0) {
3249 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3253 /* Get our certificate private key */
3254 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
!= NULL
?
3255 s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
:
3256 s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3258 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
3262 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(sctx
->libctx
, pk
, sctx
->propq
);
3263 if (pkey_ctx
== NULL
) {
3264 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3267 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3268 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3272 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3273 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3274 EVP_PKEY_CTRL_SET_IV
, 32, rnd_dgst
) <= 0) {
3275 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3279 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3280 EVP_PKEY_CTRL_CIPHER
, cipher_nid
, NULL
) <= 0) {
3281 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3284 inlen
= PACKET_remaining(pkt
);
3285 start
= PACKET_data(pkt
);
3287 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3288 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_DECRYPTION_FAILED
);
3291 /* Generate master secret */
3292 if (!ssl_generate_master_secret(s
, premaster_secret
,
3293 sizeof(premaster_secret
), 0)) {
3294 /* SSLfatal() already called */
3300 EVP_PKEY_CTX_free(pkey_ctx
);
3303 /* Should never happen */
3304 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3309 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL_CONNECTION
*s
,
3312 unsigned long alg_k
;
3314 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3316 /* For PSK parse and retrieve identity, obtain PSK key */
3317 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3318 /* SSLfatal() already called */
3322 if (alg_k
& SSL_kPSK
) {
3323 /* Identity extracted earlier: should be nothing left */
3324 if (PACKET_remaining(pkt
) != 0) {
3325 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3328 /* PSK handled by ssl_generate_master_secret */
3329 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3330 /* SSLfatal() already called */
3333 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3334 if (!tls_process_cke_rsa(s
, pkt
)) {
3335 /* SSLfatal() already called */
3338 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3339 if (!tls_process_cke_dhe(s
, pkt
)) {
3340 /* SSLfatal() already called */
3343 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3344 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3345 /* SSLfatal() already called */
3348 } else if (alg_k
& SSL_kSRP
) {
3349 if (!tls_process_cke_srp(s
, pkt
)) {
3350 /* SSLfatal() already called */
3353 } else if (alg_k
& SSL_kGOST
) {
3354 if (!tls_process_cke_gost(s
, pkt
)) {
3355 /* SSLfatal() already called */
3358 } else if (alg_k
& SSL_kGOST18
) {
3359 if (!tls_process_cke_gost18(s
, pkt
)) {
3360 /* SSLfatal() already called */
3364 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNKNOWN_CIPHER_TYPE
);
3368 return MSG_PROCESS_CONTINUE_PROCESSING
;
3370 #ifndef OPENSSL_NO_PSK
3371 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3372 s
->s3
.tmp
.psk
= NULL
;
3373 s
->s3
.tmp
.psklen
= 0;
3375 return MSG_PROCESS_ERROR
;
3378 WORK_STATE
tls_post_process_client_key_exchange(SSL_CONNECTION
*s
,
3381 #ifndef OPENSSL_NO_SCTP
3382 if (wst
== WORK_MORE_A
) {
3383 if (SSL_CONNECTION_IS_DTLS(s
)) {
3384 unsigned char sctpauthkey
[64];
3385 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3388 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3391 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3392 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3394 /* Don't include the terminating zero. */
3395 labellen
= sizeof(labelbuffer
) - 1;
3396 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3399 if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s
),
3401 sizeof(sctpauthkey
), labelbuffer
,
3404 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3408 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3409 sizeof(sctpauthkey
), sctpauthkey
);
3414 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3416 * No certificate verify or no peer certificate so we no longer need
3417 * the handshake_buffer
3419 if (!ssl3_digest_cached_records(s
, 0)) {
3420 /* SSLfatal() already called */
3423 return WORK_FINISHED_CONTINUE
;
3425 if (!s
->s3
.handshake_buffer
) {
3426 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3430 * For sigalgs freeze the handshake buffer. If we support
3431 * extms we've done this already so this is a no-op
3433 if (!ssl3_digest_cached_records(s
, 1)) {
3434 /* SSLfatal() already called */
3439 return WORK_FINISHED_CONTINUE
;
3442 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL_CONNECTION
*s
,
3446 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3449 const unsigned char *certstart
, *certbytes
;
3450 STACK_OF(X509
) *sk
= NULL
;
3451 PACKET spkt
, context
;
3453 SSL_SESSION
*new_sess
= NULL
;
3454 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3457 * To get this far we must have read encrypted data from the client. We no
3458 * longer tolerate unencrypted alerts. This value is ignored if less than
3461 s
->statem
.enc_read_state
= ENC_READ_STATE_VALID
;
3463 if ((sk
= sk_X509_new_null()) == NULL
) {
3464 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3468 if (SSL_CONNECTION_IS_TLS13(s
)
3469 && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3470 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3471 || (s
->pha_context
!= NULL
3472 && !PACKET_equal(&context
, s
->pha_context
,
3473 s
->pha_context_len
)))) {
3474 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
3478 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3479 || PACKET_remaining(pkt
) != 0) {
3480 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3484 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3485 if (!PACKET_get_net_3(&spkt
, &l
)
3486 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3487 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
3491 certstart
= certbytes
;
3492 x
= X509_new_ex(sctx
->libctx
, sctx
->propq
);
3494 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_MALLOC_FAILURE
);
3497 if (d2i_X509(&x
, (const unsigned char **)&certbytes
, l
) == NULL
) {
3498 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_ASN1_LIB
);
3502 if (certbytes
!= (certstart
+ l
)) {
3503 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
3507 if (SSL_CONNECTION_IS_TLS13(s
)) {
3508 RAW_EXTENSION
*rawexts
= NULL
;
3511 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3512 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
3515 if (!tls_collect_extensions(s
, &extensions
,
3516 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3517 NULL
, chainidx
== 0)
3518 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3519 rawexts
, x
, chainidx
,
3520 PACKET_remaining(&spkt
) == 0)) {
3521 OPENSSL_free(rawexts
);
3524 OPENSSL_free(rawexts
);
3527 if (!sk_X509_push(sk
, x
)) {
3528 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3534 if (sk_X509_num(sk
) <= 0) {
3535 /* TLS does not mind 0 certs returned */
3536 if (s
->version
== SSL3_VERSION
) {
3537 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3538 SSL_R_NO_CERTIFICATES_RETURNED
);
3541 /* Fail for TLS only if we required a certificate */
3542 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3543 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3544 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3545 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3548 /* No client certificate so digest cached records */
3549 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3550 /* SSLfatal() already called */
3555 i
= ssl_verify_cert_chain(s
, sk
);
3557 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3558 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3561 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3563 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3564 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3570 * Sessions must be immutable once they go into the session cache. Otherwise
3571 * we can get multi-thread problems. Therefore we don't "update" sessions,
3572 * we replace them with a duplicate. Here, we need to do this every time
3573 * a new certificate is received via post-handshake authentication, as the
3574 * session may have already gone into the session cache.
3577 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3578 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3579 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3583 SSL_SESSION_free(s
->session
);
3584 s
->session
= new_sess
;
3587 X509_free(s
->session
->peer
);
3588 s
->session
->peer
= sk_X509_shift(sk
);
3589 s
->session
->verify_result
= s
->verify_result
;
3591 OSSL_STACK_OF_X509_free(s
->session
->peer_chain
);
3592 s
->session
->peer_chain
= sk
;
3596 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3599 if (SSL_CONNECTION_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3600 /* SSLfatal() already called */
3605 * Inconsistency alert: cert_chain does *not* include the peer's own
3606 * certificate, while we do include it in statem_clnt.c
3609 /* Save the current hash state for when we receive the CertificateVerify */
3610 if (SSL_CONNECTION_IS_TLS13(s
)) {
3611 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3612 sizeof(s
->cert_verify_hash
),
3613 &s
->cert_verify_hash_len
)) {
3614 /* SSLfatal() already called */
3618 /* Resend session tickets */
3619 s
->sent_tickets
= 0;
3622 ret
= MSG_PROCESS_CONTINUE_READING
;
3626 OSSL_STACK_OF_X509_free(sk
);
3630 int tls_construct_server_certificate(SSL_CONNECTION
*s
, WPACKET
*pkt
)
3632 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3635 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3640 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3641 * for the server Certificate message
3643 if (SSL_CONNECTION_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3644 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3647 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3648 /* SSLfatal() already called */
3655 static int create_ticket_prequel(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3656 uint32_t age_add
, unsigned char *tick_nonce
)
3658 uint32_t timeout
= (uint32_t)s
->session
->timeout
;
3661 * Ticket lifetime hint:
3662 * In TLSv1.3 we reset the "time" field above, and always specify the
3663 * timeout, limited to a 1 week period per RFC8446.
3664 * For TLSv1.2 this is advisory only and we leave this unspecified for
3665 * resumed session (for simplicity).
3667 #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
3669 if (SSL_CONNECTION_IS_TLS13(s
)) {
3670 if (s
->session
->timeout
> ONE_WEEK_SEC
)
3671 timeout
= ONE_WEEK_SEC
;
3675 if (!WPACKET_put_bytes_u32(pkt
, timeout
)) {
3676 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3680 if (SSL_CONNECTION_IS_TLS13(s
)) {
3681 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3682 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3683 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3688 /* Start the sub-packet for the actual ticket data */
3689 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3690 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3697 static int construct_stateless_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3699 unsigned char *tick_nonce
)
3701 unsigned char *senc
= NULL
;
3702 EVP_CIPHER_CTX
*ctx
= NULL
;
3703 SSL_HMAC
*hctx
= NULL
;
3704 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3705 const unsigned char *const_p
;
3706 int len
, slen_full
, slen
, lenfinal
;
3709 SSL_CTX
*tctx
= s
->session_ctx
;
3710 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3711 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3713 size_t macoffset
, macendoffset
;
3714 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
3715 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
3717 /* get session encoding length */
3718 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3720 * Some length values are 16 bits, so forget it if session is too
3723 if (slen_full
== 0 || slen_full
> 0xFF00) {
3724 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3727 senc
= OPENSSL_malloc(slen_full
);
3729 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3733 ctx
= EVP_CIPHER_CTX_new();
3734 hctx
= ssl_hmac_new(tctx
);
3735 if (ctx
== NULL
|| hctx
== NULL
) {
3736 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3741 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3742 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3747 * create a fresh copy (not shared with other threads) to clean up
3750 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3752 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3756 slen
= i2d_SSL_SESSION(sess
, NULL
);
3757 if (slen
== 0 || slen
> slen_full
) {
3758 /* shouldn't ever happen */
3759 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3760 SSL_SESSION_free(sess
);
3764 if (!i2d_SSL_SESSION(sess
, &p
)) {
3765 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3766 SSL_SESSION_free(sess
);
3769 SSL_SESSION_free(sess
);
3772 * Initialize HMAC and cipher contexts. If callback present it does
3773 * all the work otherwise use generated values from parent ctx.
3775 #ifndef OPENSSL_NO_DEPRECATED_3_0
3776 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
|| tctx
->ext
.ticket_key_cb
!= NULL
)
3778 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3783 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3784 ret
= tctx
->ext
.ticket_key_evp_cb(ssl
, key_name
, iv
, ctx
,
3785 ssl_hmac_get0_EVP_MAC_CTX(hctx
),
3787 #ifndef OPENSSL_NO_DEPRECATED_3_0
3788 else if (tctx
->ext
.ticket_key_cb
!= NULL
)
3789 /* if 0 is returned, write an empty ticket */
3790 ret
= tctx
->ext
.ticket_key_cb(ssl
, key_name
, iv
, ctx
,
3791 ssl_hmac_get0_HMAC_CTX(hctx
), 1);
3796 /* Put timeout and length */
3797 if (!WPACKET_put_bytes_u32(pkt
, 0)
3798 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3799 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3803 EVP_CIPHER_CTX_free(ctx
);
3804 ssl_hmac_free(hctx
);
3808 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CALLBACK_FAILED
);
3811 iv_len
= EVP_CIPHER_CTX_get_iv_length(ctx
);
3813 EVP_CIPHER
*cipher
= EVP_CIPHER_fetch(sctx
->libctx
, "AES-256-CBC",
3816 if (cipher
== NULL
) {
3817 /* Error is already recorded */
3818 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
3822 iv_len
= EVP_CIPHER_get_iv_length(cipher
);
3824 || RAND_bytes_ex(sctx
->libctx
, iv
, iv_len
, 0) <= 0
3825 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3826 tctx
->ext
.secure
->tick_aes_key
, iv
)
3827 || !ssl_hmac_init(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
3828 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
3830 EVP_CIPHER_free(cipher
);
3831 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3834 EVP_CIPHER_free(cipher
);
3835 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3836 sizeof(tctx
->ext
.tick_key_name
));
3839 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3840 /* SSLfatal() already called */
3844 if (!WPACKET_get_total_written(pkt
, &macoffset
)
3845 /* Output key name */
3846 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3848 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3849 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3851 /* Encrypt session data */
3852 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3853 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3854 || encdata1
!= encdata2
3855 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3856 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3857 || encdata1
+ len
!= encdata2
3858 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3859 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3860 || !ssl_hmac_update(hctx
,
3861 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3862 macendoffset
- macoffset
)
3863 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3864 || !ssl_hmac_final(hctx
, macdata1
, &hlen
, EVP_MAX_MD_SIZE
)
3865 || hlen
> EVP_MAX_MD_SIZE
3866 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3867 || macdata1
!= macdata2
) {
3868 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3872 /* Close the sub-packet created by create_ticket_prequel() */
3873 if (!WPACKET_close(pkt
)) {
3874 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3881 EVP_CIPHER_CTX_free(ctx
);
3882 ssl_hmac_free(hctx
);
3886 static int construct_stateful_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
,
3888 unsigned char *tick_nonce
)
3890 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3891 /* SSLfatal() already called */
3895 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
3896 s
->session
->session_id_length
)
3897 || !WPACKET_close(pkt
)) {
3898 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3905 int tls_construct_new_session_ticket(SSL_CONNECTION
*s
, WPACKET
*pkt
)
3907 SSL_CTX
*tctx
= s
->session_ctx
;
3908 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
3910 unsigned char age_add_c
[sizeof(uint32_t)];
3914 age_add_u
.age_add
= 0;
3916 if (SSL_CONNECTION_IS_TLS13(s
)) {
3919 static const unsigned char nonce_label
[] = "resumption";
3920 const EVP_MD
*md
= ssl_handshake_md(s
);
3921 int hashleni
= EVP_MD_get_size(md
);
3923 /* Ensure cast to size_t is safe */
3924 if (!ossl_assert(hashleni
>= 0)) {
3925 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3928 hashlen
= (size_t)hashleni
;
3931 * If we already sent one NewSessionTicket, or we resumed then
3932 * s->session may already be in a cache and so we must not modify it.
3933 * Instead we need to take a copy of it and modify that.
3935 if (s
->sent_tickets
!= 0 || s
->hit
) {
3936 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
3938 if (new_sess
== NULL
) {
3939 /* SSLfatal already called */
3943 SSL_SESSION_free(s
->session
);
3944 s
->session
= new_sess
;
3947 if (!ssl_generate_session_id(s
, s
->session
)) {
3948 /* SSLfatal() already called */
3951 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
3952 age_add_u
.age_add_c
, sizeof(age_add_u
), 0) <= 0) {
3953 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3956 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
3958 nonce
= s
->next_ticket_nonce
;
3959 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
3960 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
3964 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
3966 sizeof(nonce_label
) - 1,
3969 s
->session
->master_key
,
3971 /* SSLfatal() already called */
3974 s
->session
->master_key_length
= hashlen
;
3976 s
->session
->time
= time(NULL
);
3977 ssl_session_calculate_timeout(s
->session
);
3978 if (s
->s3
.alpn_selected
!= NULL
) {
3979 OPENSSL_free(s
->session
->ext
.alpn_selected
);
3980 s
->session
->ext
.alpn_selected
=
3981 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
3982 if (s
->session
->ext
.alpn_selected
== NULL
) {
3983 s
->session
->ext
.alpn_selected_len
= 0;
3984 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3987 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
3989 s
->session
->ext
.max_early_data
= s
->max_early_data
;
3992 if (tctx
->generate_ticket_cb
!= NULL
&&
3993 tctx
->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s
),
3994 tctx
->ticket_cb_data
) == 0) {
3995 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3999 * If we are using anti-replay protection then we behave as if
4000 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4001 * is no point in using full stateless tickets.
4003 if (SSL_CONNECTION_IS_TLS13(s
)
4004 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4005 || (s
->max_early_data
> 0
4006 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4007 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4008 /* SSLfatal() already called */
4011 } else if (!construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4013 /* SSLfatal() already called */
4017 if (SSL_CONNECTION_IS_TLS13(s
)) {
4018 if (!tls_construct_extensions(s
, pkt
,
4019 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4021 /* SSLfatal() already called */
4025 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4026 * gets reset to 0 if we send more tickets following a post-handshake
4027 * auth, but |next_ticket_nonce| does not. If we're sending extra
4028 * tickets, decrement the count of pending extra tickets.
4031 s
->next_ticket_nonce
++;
4032 if (s
->ext
.extra_tickets_expected
> 0)
4033 s
->ext
.extra_tickets_expected
--;
4034 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4043 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4044 * create a separate message. Returns 1 on success or 0 on failure.
4046 int tls_construct_cert_status_body(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4048 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4049 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4050 s
->ext
.ocsp
.resp_len
)) {
4051 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4058 int tls_construct_cert_status(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4060 if (!tls_construct_cert_status_body(s
, pkt
)) {
4061 /* SSLfatal() already called */
4068 #ifndef OPENSSL_NO_NEXTPROTONEG
4070 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4071 * It sets the next_proto member in s if found
4073 MSG_PROCESS_RETURN
tls_process_next_proto(SSL_CONNECTION
*s
, PACKET
*pkt
)
4075 PACKET next_proto
, padding
;
4076 size_t next_proto_len
;
4079 * The payload looks like:
4081 * uint8 proto[proto_len];
4082 * uint8 padding_len;
4083 * uint8 padding[padding_len];
4085 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4086 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4087 || PACKET_remaining(pkt
) > 0) {
4088 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
4089 return MSG_PROCESS_ERROR
;
4092 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4094 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4095 return MSG_PROCESS_ERROR
;
4098 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4100 return MSG_PROCESS_CONTINUE_READING
;
4104 static int tls_construct_encrypted_extensions(SSL_CONNECTION
*s
, WPACKET
*pkt
)
4106 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4108 /* SSLfatal() already called */
4115 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL_CONNECTION
*s
, PACKET
*pkt
)
4117 if (PACKET_remaining(pkt
) != 0) {
4118 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
4119 return MSG_PROCESS_ERROR
;
4122 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4123 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4124 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
4125 return MSG_PROCESS_ERROR
;
4129 * EndOfEarlyData signals a key change so the end of the message must be on
4130 * a record boundary.
4132 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4133 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
4134 return MSG_PROCESS_ERROR
;
4137 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4138 if (!SSL_CONNECTION_GET_SSL(s
)->method
->ssl3_enc
->change_cipher_state(s
,
4139 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4140 /* SSLfatal() already called */
4141 return MSG_PROCESS_ERROR
;
4144 return MSG_PROCESS_CONTINUE_READING
;