2 * Copyright 1995-2020 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/bn.h>
24 #include <openssl/md5.h>
25 #include <openssl/trace.h>
26 #include <openssl/core_names.h>
27 #include <openssl/asn1t.h>
29 #define TICKET_NONCE_SIZE 8
33 ASN1_TYPE
*opaqueBlob
;
36 DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE
)
38 ASN1_SEQUENCE(GOST_KX_MESSAGE
) = {
39 ASN1_SIMPLE(GOST_KX_MESSAGE
, kxBlob
, ASN1_ANY
),
40 ASN1_OPT(GOST_KX_MESSAGE
, opaqueBlob
, ASN1_ANY
),
41 } ASN1_SEQUENCE_END(GOST_KX_MESSAGE
)
43 IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE
)
45 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
);
48 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
49 * handshake state transitions when a TLSv1.3 server is reading messages from
50 * the client. The message type that the client has sent is provided in |mt|.
51 * The current state is in |s->statem.hand_state|.
53 * Return values are 1 for success (transition allowed) and 0 on error
54 * (transition not allowed)
56 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
58 OSSL_STATEM
*st
= &s
->statem
;
61 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
62 * not negotiated TLSv1.3 yet, so that case is handled by
63 * ossl_statem_server_read_transition()
65 switch (st
->hand_state
) {
69 case TLS_ST_EARLY_DATA
:
70 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
71 if (mt
== SSL3_MT_CLIENT_HELLO
) {
72 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
76 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
77 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
78 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
85 case TLS_ST_SR_END_OF_EARLY_DATA
:
86 case TLS_ST_SW_FINISHED
:
87 if (s
->s3
.tmp
.cert_request
) {
88 if (mt
== SSL3_MT_CERTIFICATE
) {
89 st
->hand_state
= TLS_ST_SR_CERT
;
93 if (mt
== SSL3_MT_FINISHED
) {
94 st
->hand_state
= TLS_ST_SR_FINISHED
;
101 if (s
->session
->peer
== NULL
) {
102 if (mt
== SSL3_MT_FINISHED
) {
103 st
->hand_state
= TLS_ST_SR_FINISHED
;
107 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
108 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
114 case TLS_ST_SR_CERT_VRFY
:
115 if (mt
== SSL3_MT_FINISHED
) {
116 st
->hand_state
= TLS_ST_SR_FINISHED
;
123 * Its never ok to start processing handshake messages in the middle of
124 * early data (i.e. before we've received the end of early data alert)
126 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
129 if (mt
== SSL3_MT_CERTIFICATE
130 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
131 st
->hand_state
= TLS_ST_SR_CERT
;
135 if (mt
== SSL3_MT_KEY_UPDATE
) {
136 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
142 /* No valid transition found */
147 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
148 * handshake state transitions when the server is reading messages from the
149 * client. The message type that the client has sent is provided in |mt|. The
150 * current state is in |s->statem.hand_state|.
152 * Return values are 1 for success (transition allowed) and 0 on error
153 * (transition not allowed)
155 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
157 OSSL_STATEM
*st
= &s
->statem
;
159 if (SSL_IS_TLS13(s
)) {
160 if (!ossl_statem_server13_read_transition(s
, mt
))
165 switch (st
->hand_state
) {
171 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
172 if (mt
== SSL3_MT_CLIENT_HELLO
) {
173 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
178 case TLS_ST_SW_SRVR_DONE
:
180 * If we get a CKE message after a ServerDone then either
181 * 1) We didn't request a Certificate
183 * 2) If we did request one then
184 * a) We allow no Certificate to be returned
186 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
187 * list if we requested a certificate)
189 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
190 if (s
->s3
.tmp
.cert_request
) {
191 if (s
->version
== SSL3_VERSION
) {
192 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
193 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
195 * This isn't an unexpected message as such - we're just
196 * not going to accept it because we require a client
199 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
200 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
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_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(s
);
308 BIO_clear_retry_flags(rbio
);
309 BIO_set_retry_read(rbio
);
312 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
,
313 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
314 SSL_R_UNEXPECTED_MESSAGE
);
319 * Should we send a ServerKeyExchange message?
321 * Valid return values are:
325 static int send_server_key_exchange(SSL
*s
)
327 unsigned long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
330 * only send a ServerKeyExchange if DH or fortezza but we have a
331 * sign only certificate PSK: may send PSK identity hints For
332 * ECC ciphersuites, we send a serverKeyExchange message only if
333 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
334 * the server certificate contains the server's public key for
337 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
339 * PSK: send ServerKeyExchange if PSK identity hint if
342 #ifndef OPENSSL_NO_PSK
343 /* Only send SKE if we have identity hint for plain PSK */
344 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
345 && s
->cert
->psk_identity_hint
)
346 /* For other PSK always send SKE */
347 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
349 #ifndef OPENSSL_NO_SRP
350 /* SRP: send ServerKeyExchange */
351 || (alg_k
& SSL_kSRP
)
361 * Should we send a CertificateRequest message?
363 * Valid return values are:
367 int send_certificate_request(SSL
*s
)
370 /* don't request cert unless asked for it: */
371 s
->verify_mode
& SSL_VERIFY_PEER
373 * don't request if post-handshake-only unless doing
374 * post-handshake in TLSv1.3:
376 && (!SSL_IS_TLS13(s
) || !(s
->verify_mode
& SSL_VERIFY_POST_HANDSHAKE
)
377 || s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
)
379 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
382 && (s
->certreqs_sent
< 1 ||
383 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
385 * never request cert in anonymous ciphersuites (see
386 * section "Certificate request" in SSL 3 drafts and in
389 && (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
391 * ... except when the application insists on
392 * verification (against the specs, but statem_clnt.c accepts
395 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
396 /* don't request certificate for SRP auth */
397 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
399 * With normal PSK Certificates and Certificate Requests
402 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
410 * ossl_statem_server13_write_transition() works out what handshake state to
411 * move to next when a TLSv1.3 server is writing messages to be sent to the
414 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
416 OSSL_STATEM
*st
= &s
->statem
;
419 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
420 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
423 switch (st
->hand_state
) {
425 /* Shouldn't happen */
426 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
427 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION
,
428 ERR_R_INTERNAL_ERROR
);
429 return WRITE_TRAN_ERROR
;
432 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
433 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
434 return WRITE_TRAN_CONTINUE
;
436 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
437 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
438 return WRITE_TRAN_CONTINUE
;
440 if (s
->ext
.extra_tickets_expected
> 0) {
441 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
442 return WRITE_TRAN_CONTINUE
;
444 /* Try to read from the client instead */
445 return WRITE_TRAN_FINISHED
;
447 case TLS_ST_SR_CLNT_HELLO
:
448 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
449 return WRITE_TRAN_CONTINUE
;
451 case TLS_ST_SW_SRVR_HELLO
:
452 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
453 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
454 st
->hand_state
= TLS_ST_SW_CHANGE
;
455 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
456 st
->hand_state
= TLS_ST_EARLY_DATA
;
458 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
459 return WRITE_TRAN_CONTINUE
;
461 case TLS_ST_SW_CHANGE
:
462 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
463 st
->hand_state
= TLS_ST_EARLY_DATA
;
465 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
466 return WRITE_TRAN_CONTINUE
;
468 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
470 st
->hand_state
= TLS_ST_SW_FINISHED
;
471 else if (send_certificate_request(s
))
472 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
474 st
->hand_state
= TLS_ST_SW_CERT
;
476 return WRITE_TRAN_CONTINUE
;
478 case TLS_ST_SW_CERT_REQ
:
479 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
480 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
481 st
->hand_state
= TLS_ST_OK
;
483 st
->hand_state
= TLS_ST_SW_CERT
;
485 return WRITE_TRAN_CONTINUE
;
488 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
489 return WRITE_TRAN_CONTINUE
;
491 case TLS_ST_SW_CERT_VRFY
:
492 st
->hand_state
= TLS_ST_SW_FINISHED
;
493 return WRITE_TRAN_CONTINUE
;
495 case TLS_ST_SW_FINISHED
:
496 st
->hand_state
= TLS_ST_EARLY_DATA
;
497 return WRITE_TRAN_CONTINUE
;
499 case TLS_ST_EARLY_DATA
:
500 return WRITE_TRAN_FINISHED
;
502 case TLS_ST_SR_FINISHED
:
504 * Technically we have finished the handshake at this point, but we're
505 * going to remain "in_init" for now and write out any session tickets
508 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
509 s
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
;
510 } else if (!s
->ext
.ticket_expected
) {
512 * If we're not going to renew the ticket then we just finish the
513 * handshake at this point.
515 st
->hand_state
= TLS_ST_OK
;
516 return WRITE_TRAN_CONTINUE
;
518 if (s
->num_tickets
> s
->sent_tickets
)
519 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
521 st
->hand_state
= TLS_ST_OK
;
522 return WRITE_TRAN_CONTINUE
;
524 case TLS_ST_SR_KEY_UPDATE
:
525 case TLS_ST_SW_KEY_UPDATE
:
526 st
->hand_state
= TLS_ST_OK
;
527 return WRITE_TRAN_CONTINUE
;
529 case TLS_ST_SW_SESSION_TICKET
:
530 /* In a resumption we only ever send a maximum of one new ticket.
531 * Following an initial handshake we send the number of tickets we have
532 * been configured for.
534 if (!SSL_IS_FIRST_HANDSHAKE(s
) && s
->ext
.extra_tickets_expected
> 0) {
535 return WRITE_TRAN_CONTINUE
;
536 } else if (s
->hit
|| s
->num_tickets
<= s
->sent_tickets
) {
537 /* We've written enough tickets out. */
538 st
->hand_state
= TLS_ST_OK
;
540 return WRITE_TRAN_CONTINUE
;
545 * ossl_statem_server_write_transition() works out what handshake state to move
546 * to next when the server is writing messages to be sent to the client.
548 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
550 OSSL_STATEM
*st
= &s
->statem
;
553 * Note that before the ClientHello we don't know what version we are going
554 * to negotiate yet, so we don't take this branch until later
558 return ossl_statem_server13_write_transition(s
);
560 switch (st
->hand_state
) {
562 /* Shouldn't happen */
563 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
564 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION
,
565 ERR_R_INTERNAL_ERROR
);
566 return WRITE_TRAN_ERROR
;
569 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
570 /* We must be trying to renegotiate */
571 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
572 st
->request_state
= TLS_ST_BEFORE
;
573 return WRITE_TRAN_CONTINUE
;
575 /* Must be an incoming ClientHello */
576 if (!tls_setup_handshake(s
)) {
577 /* SSLfatal() already called */
578 return WRITE_TRAN_ERROR
;
583 /* Just go straight to trying to read from the client */
584 return WRITE_TRAN_FINISHED
;
586 case TLS_ST_SW_HELLO_REQ
:
587 st
->hand_state
= TLS_ST_OK
;
588 return WRITE_TRAN_CONTINUE
;
590 case TLS_ST_SR_CLNT_HELLO
:
591 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
592 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
)) {
593 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
594 } else if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
595 /* We must have rejected the renegotiation */
596 st
->hand_state
= TLS_ST_OK
;
597 return WRITE_TRAN_CONTINUE
;
599 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
601 return WRITE_TRAN_CONTINUE
;
603 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
604 return WRITE_TRAN_FINISHED
;
606 case TLS_ST_SW_SRVR_HELLO
:
608 if (s
->ext
.ticket_expected
)
609 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
611 st
->hand_state
= TLS_ST_SW_CHANGE
;
613 /* Check if it is anon DH or anon ECDH, */
614 /* normal PSK or SRP */
615 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
&
616 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
617 st
->hand_state
= TLS_ST_SW_CERT
;
618 } else if (send_server_key_exchange(s
)) {
619 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
620 } else if (send_certificate_request(s
)) {
621 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
623 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
626 return WRITE_TRAN_CONTINUE
;
629 if (s
->ext
.status_expected
) {
630 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
631 return WRITE_TRAN_CONTINUE
;
635 case TLS_ST_SW_CERT_STATUS
:
636 if (send_server_key_exchange(s
)) {
637 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
638 return WRITE_TRAN_CONTINUE
;
642 case TLS_ST_SW_KEY_EXCH
:
643 if (send_certificate_request(s
)) {
644 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
645 return WRITE_TRAN_CONTINUE
;
649 case TLS_ST_SW_CERT_REQ
:
650 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
651 return WRITE_TRAN_CONTINUE
;
653 case TLS_ST_SW_SRVR_DONE
:
654 return WRITE_TRAN_FINISHED
;
656 case TLS_ST_SR_FINISHED
:
658 st
->hand_state
= TLS_ST_OK
;
659 return WRITE_TRAN_CONTINUE
;
660 } else if (s
->ext
.ticket_expected
) {
661 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
663 st
->hand_state
= TLS_ST_SW_CHANGE
;
665 return WRITE_TRAN_CONTINUE
;
667 case TLS_ST_SW_SESSION_TICKET
:
668 st
->hand_state
= TLS_ST_SW_CHANGE
;
669 return WRITE_TRAN_CONTINUE
;
671 case TLS_ST_SW_CHANGE
:
672 st
->hand_state
= TLS_ST_SW_FINISHED
;
673 return WRITE_TRAN_CONTINUE
;
675 case TLS_ST_SW_FINISHED
:
677 return WRITE_TRAN_FINISHED
;
679 st
->hand_state
= TLS_ST_OK
;
680 return WRITE_TRAN_CONTINUE
;
685 * Perform any pre work that needs to be done prior to sending a message from
686 * the server to the client.
688 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
690 OSSL_STATEM
*st
= &s
->statem
;
692 switch (st
->hand_state
) {
694 /* No pre work to be done */
697 case TLS_ST_SW_HELLO_REQ
:
700 dtls1_clear_sent_buffer(s
);
703 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
705 if (SSL_IS_DTLS(s
)) {
706 dtls1_clear_sent_buffer(s
);
707 /* We don't buffer this message so don't use the timer */
712 case TLS_ST_SW_SRVR_HELLO
:
713 if (SSL_IS_DTLS(s
)) {
715 * Messages we write from now on should be buffered and
716 * retransmitted if necessary, so we need to use the timer now
722 case TLS_ST_SW_SRVR_DONE
:
723 #ifndef OPENSSL_NO_SCTP
724 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
725 /* Calls SSLfatal() as required */
726 return dtls_wait_for_dry(s
);
729 return WORK_FINISHED_CONTINUE
;
731 case TLS_ST_SW_SESSION_TICKET
:
732 if (SSL_IS_TLS13(s
) && s
->sent_tickets
== 0
733 && s
->ext
.extra_tickets_expected
== 0) {
735 * Actually this is the end of the handshake, but we're going
736 * straight into writing the session ticket out. So we finish off
737 * the handshake, but keep the various buffers active.
739 * Calls SSLfatal as required.
741 return tls_finish_handshake(s
, wst
, 0, 0);
743 if (SSL_IS_DTLS(s
)) {
745 * We're into the last flight. We don't retransmit the last flight
746 * unless we need to, so we don't use the timer
752 case TLS_ST_SW_CHANGE
:
755 /* Writes to s->session are only safe for initial handshakes */
756 if (s
->session
->cipher
== NULL
) {
757 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
758 } else if (s
->session
->cipher
!= s
->s3
.tmp
.new_cipher
) {
759 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
760 SSL_F_OSSL_STATEM_SERVER_PRE_WORK
,
761 ERR_R_INTERNAL_ERROR
);
764 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
765 /* SSLfatal() already called */
768 if (SSL_IS_DTLS(s
)) {
770 * We're into the last flight. We don't retransmit the last flight
771 * unless we need to, so we don't use the timer. This might have
772 * already been set to 0 if we sent a NewSessionTicket message,
773 * but we'll set it again here in case we didn't.
777 return WORK_FINISHED_CONTINUE
;
779 case TLS_ST_EARLY_DATA
:
780 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
781 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0)
782 return WORK_FINISHED_CONTINUE
;
786 /* Calls SSLfatal() as required */
787 return tls_finish_handshake(s
, wst
, 1, 1);
790 return WORK_FINISHED_CONTINUE
;
793 static ossl_inline
int conn_is_closed(void)
795 switch (get_last_sys_error()) {
800 #if defined(ECONNRESET)
804 #if defined(WSAECONNRESET)
814 * Perform any work that needs to be done after sending a message from the
815 * server to the client.
817 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
819 OSSL_STATEM
*st
= &s
->statem
;
823 switch (st
->hand_state
) {
825 /* No post work to be done */
828 case TLS_ST_SW_HELLO_REQ
:
829 if (statem_flush(s
) != 1)
831 if (!ssl3_init_finished_mac(s
)) {
832 /* SSLfatal() already called */
837 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
838 if (statem_flush(s
) != 1)
840 /* HelloVerifyRequest resets Finished MAC */
841 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
842 /* SSLfatal() already called */
846 * The next message should be another ClientHello which we need to
847 * treat like it was the first packet
852 case TLS_ST_SW_SRVR_HELLO
:
853 if (SSL_IS_TLS13(s
) && s
->hello_retry_request
== SSL_HRR_PENDING
) {
854 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
855 && statem_flush(s
) != 1)
859 #ifndef OPENSSL_NO_SCTP
860 if (SSL_IS_DTLS(s
) && s
->hit
) {
861 unsigned char sctpauthkey
[64];
862 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
866 * Add new shared key for SCTP-Auth, will be ignored if no
869 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
870 sizeof(DTLS1_SCTP_AUTH_LABEL
));
872 /* Don't include the terminating zero. */
873 labellen
= sizeof(labelbuffer
) - 1;
874 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
877 if (SSL_export_keying_material(s
, sctpauthkey
,
878 sizeof(sctpauthkey
), labelbuffer
,
881 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
882 SSL_F_OSSL_STATEM_SERVER_POST_WORK
,
883 ERR_R_INTERNAL_ERROR
);
887 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
888 sizeof(sctpauthkey
), sctpauthkey
);
892 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
893 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
897 case TLS_ST_SW_CHANGE
:
898 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
899 if (!statem_flush(s
))
904 if (SSL_IS_TLS13(s
)) {
905 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
906 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
907 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
908 /* SSLfatal() already called */
912 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
913 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
914 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
915 /* SSLfatal() already called */
919 * We don't yet know whether the next record we are going to receive
920 * is an unencrypted alert, an encrypted alert, or an encrypted
921 * handshake message. We temporarily tolerate unencrypted alerts.
923 s
->statem
.enc_read_state
= ENC_READ_STATE_ALLOW_PLAIN_ALERTS
;
927 #ifndef OPENSSL_NO_SCTP
928 if (SSL_IS_DTLS(s
) && !s
->hit
) {
930 * Change to new shared key of SCTP-Auth, will be ignored if
933 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
937 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
938 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
940 /* SSLfatal() already called */
945 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
948 case TLS_ST_SW_SRVR_DONE
:
949 if (statem_flush(s
) != 1)
953 case TLS_ST_SW_FINISHED
:
954 if (statem_flush(s
) != 1)
956 #ifndef OPENSSL_NO_SCTP
957 if (SSL_IS_DTLS(s
) && s
->hit
) {
959 * Change to new shared key of SCTP-Auth, will be ignored if
962 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
966 if (SSL_IS_TLS13(s
)) {
967 /* TLS 1.3 gets the secret size from the handshake md */
969 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
970 s
->master_secret
, s
->handshake_secret
, 0,
972 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
973 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
974 /* SSLfatal() already called */
979 case TLS_ST_SW_CERT_REQ
:
980 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
981 if (statem_flush(s
) != 1)
986 case TLS_ST_SW_KEY_UPDATE
:
987 if (statem_flush(s
) != 1)
989 if (!tls13_update_key(s
, 1)) {
990 /* SSLfatal() already called */
995 case TLS_ST_SW_SESSION_TICKET
:
997 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1) {
998 if (SSL_get_error(s
, 0) == SSL_ERROR_SYSCALL
999 && conn_is_closed()) {
1001 * We ignore connection closed errors in TLSv1.3 when sending a
1002 * NewSessionTicket and behave as if we were successful. This is
1003 * so that we are still able to read data sent to us by a client
1004 * that closes soon after the end of the handshake without
1005 * waiting to read our post-handshake NewSessionTickets.
1007 s
->rwstate
= SSL_NOTHING
;
1016 return WORK_FINISHED_CONTINUE
;
1020 * Get the message construction function and message type for sending from the
1023 * Valid return values are:
1027 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
1028 confunc_f
*confunc
, int *mt
)
1030 OSSL_STATEM
*st
= &s
->statem
;
1032 switch (st
->hand_state
) {
1034 /* Shouldn't happen */
1035 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1036 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE
,
1037 SSL_R_BAD_HANDSHAKE_STATE
);
1040 case TLS_ST_SW_CHANGE
:
1042 *confunc
= dtls_construct_change_cipher_spec
;
1044 *confunc
= tls_construct_change_cipher_spec
;
1045 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1048 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1049 *confunc
= dtls_construct_hello_verify_request
;
1050 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1053 case TLS_ST_SW_HELLO_REQ
:
1054 /* No construction function needed */
1056 *mt
= SSL3_MT_HELLO_REQUEST
;
1059 case TLS_ST_SW_SRVR_HELLO
:
1060 *confunc
= tls_construct_server_hello
;
1061 *mt
= SSL3_MT_SERVER_HELLO
;
1064 case TLS_ST_SW_CERT
:
1065 *confunc
= tls_construct_server_certificate
;
1066 *mt
= SSL3_MT_CERTIFICATE
;
1069 case TLS_ST_SW_CERT_VRFY
:
1070 *confunc
= tls_construct_cert_verify
;
1071 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1075 case TLS_ST_SW_KEY_EXCH
:
1076 *confunc
= tls_construct_server_key_exchange
;
1077 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1080 case TLS_ST_SW_CERT_REQ
:
1081 *confunc
= tls_construct_certificate_request
;
1082 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1085 case TLS_ST_SW_SRVR_DONE
:
1086 *confunc
= tls_construct_server_done
;
1087 *mt
= SSL3_MT_SERVER_DONE
;
1090 case TLS_ST_SW_SESSION_TICKET
:
1091 *confunc
= tls_construct_new_session_ticket
;
1092 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1095 case TLS_ST_SW_CERT_STATUS
:
1096 *confunc
= tls_construct_cert_status
;
1097 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1100 case TLS_ST_SW_FINISHED
:
1101 *confunc
= tls_construct_finished
;
1102 *mt
= SSL3_MT_FINISHED
;
1105 case TLS_ST_EARLY_DATA
:
1107 *mt
= SSL3_MT_DUMMY
;
1110 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1111 *confunc
= tls_construct_encrypted_extensions
;
1112 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1115 case TLS_ST_SW_KEY_UPDATE
:
1116 *confunc
= tls_construct_key_update
;
1117 *mt
= SSL3_MT_KEY_UPDATE
;
1125 * Maximum size (excluding the Handshake header) of a ClientHello message,
1126 * calculated as follows:
1128 * 2 + # client_version
1129 * 32 + # only valid length for random
1130 * 1 + # length of session_id
1131 * 32 + # maximum size for session_id
1132 * 2 + # length of cipher suites
1133 * 2^16-2 + # maximum length of cipher suites array
1134 * 1 + # length of compression_methods
1135 * 2^8-1 + # maximum length of compression methods
1136 * 2 + # length of extensions
1137 * 2^16-1 # maximum length of extensions
1139 #define CLIENT_HELLO_MAX_LENGTH 131396
1141 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1142 #define NEXT_PROTO_MAX_LENGTH 514
1145 * Returns the maximum allowed length for the current message that we are
1146 * reading. Excludes the message header.
1148 size_t ossl_statem_server_max_message_size(SSL
*s
)
1150 OSSL_STATEM
*st
= &s
->statem
;
1152 switch (st
->hand_state
) {
1154 /* Shouldn't happen */
1157 case TLS_ST_SR_CLNT_HELLO
:
1158 return CLIENT_HELLO_MAX_LENGTH
;
1160 case TLS_ST_SR_END_OF_EARLY_DATA
:
1161 return END_OF_EARLY_DATA_MAX_LENGTH
;
1163 case TLS_ST_SR_CERT
:
1164 return s
->max_cert_list
;
1166 case TLS_ST_SR_KEY_EXCH
:
1167 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1169 case TLS_ST_SR_CERT_VRFY
:
1170 return SSL3_RT_MAX_PLAIN_LENGTH
;
1172 #ifndef OPENSSL_NO_NEXTPROTONEG
1173 case TLS_ST_SR_NEXT_PROTO
:
1174 return NEXT_PROTO_MAX_LENGTH
;
1177 case TLS_ST_SR_CHANGE
:
1178 return CCS_MAX_LENGTH
;
1180 case TLS_ST_SR_FINISHED
:
1181 return FINISHED_MAX_LENGTH
;
1183 case TLS_ST_SR_KEY_UPDATE
:
1184 return KEY_UPDATE_MAX_LENGTH
;
1189 * Process a message that the server has received from the client.
1191 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1193 OSSL_STATEM
*st
= &s
->statem
;
1195 switch (st
->hand_state
) {
1197 /* Shouldn't happen */
1198 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1199 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE
,
1200 ERR_R_INTERNAL_ERROR
);
1201 return MSG_PROCESS_ERROR
;
1203 case TLS_ST_SR_CLNT_HELLO
:
1204 return tls_process_client_hello(s
, pkt
);
1206 case TLS_ST_SR_END_OF_EARLY_DATA
:
1207 return tls_process_end_of_early_data(s
, pkt
);
1209 case TLS_ST_SR_CERT
:
1210 return tls_process_client_certificate(s
, pkt
);
1212 case TLS_ST_SR_KEY_EXCH
:
1213 return tls_process_client_key_exchange(s
, pkt
);
1215 case TLS_ST_SR_CERT_VRFY
:
1216 return tls_process_cert_verify(s
, pkt
);
1218 #ifndef OPENSSL_NO_NEXTPROTONEG
1219 case TLS_ST_SR_NEXT_PROTO
:
1220 return tls_process_next_proto(s
, pkt
);
1223 case TLS_ST_SR_CHANGE
:
1224 return tls_process_change_cipher_spec(s
, pkt
);
1226 case TLS_ST_SR_FINISHED
:
1227 return tls_process_finished(s
, pkt
);
1229 case TLS_ST_SR_KEY_UPDATE
:
1230 return tls_process_key_update(s
, pkt
);
1236 * Perform any further processing required following the receipt of a message
1239 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1241 OSSL_STATEM
*st
= &s
->statem
;
1243 switch (st
->hand_state
) {
1245 /* Shouldn't happen */
1246 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1247 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE
,
1248 ERR_R_INTERNAL_ERROR
);
1251 case TLS_ST_SR_CLNT_HELLO
:
1252 return tls_post_process_client_hello(s
, wst
);
1254 case TLS_ST_SR_KEY_EXCH
:
1255 return tls_post_process_client_key_exchange(s
, wst
);
1259 #ifndef OPENSSL_NO_SRP
1260 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1261 static int ssl_check_srp_ext_ClientHello(SSL
*s
)
1264 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1266 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1267 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1268 if (s
->srp_ctx
.login
== NULL
) {
1270 * RFC 5054 says SHOULD reject, we do so if There is no srp
1273 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1274 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1275 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1278 ret
= SSL_srp_server_param_with_username(s
, &al
);
1281 if (ret
== SSL3_AL_FATAL
) {
1282 SSLfatal(s
, al
, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1283 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1284 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1285 : SSL_R_CLIENTHELLO_TLSEXT
);
1294 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1297 /* Always use DTLS 1.0 version: see RFC 6347 */
1298 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1299 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1305 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1307 unsigned int cookie_leni
;
1308 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1309 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1310 &cookie_leni
) == 0 ||
1311 cookie_leni
> DTLS1_COOKIE_LENGTH
) {
1312 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1313 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1316 s
->d1
->cookie_len
= cookie_leni
;
1318 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1319 s
->d1
->cookie_len
)) {
1320 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1321 ERR_R_INTERNAL_ERROR
);
1328 #ifndef OPENSSL_NO_EC
1330 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1331 * SecureTransport using the TLS extension block in |hello|.
1332 * Safari, since 10.6, sends exactly these extensions, in this order:
1336 * signature_algorithms (for TLSv1.2 only)
1338 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1339 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1340 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1341 * 10.8..10.8.3 (which don't work).
1343 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1345 static const unsigned char kSafariExtensionsBlock
[] = {
1346 0x00, 0x0a, /* elliptic_curves extension */
1347 0x00, 0x08, /* 8 bytes */
1348 0x00, 0x06, /* 6 bytes of curve ids */
1349 0x00, 0x17, /* P-256 */
1350 0x00, 0x18, /* P-384 */
1351 0x00, 0x19, /* P-521 */
1353 0x00, 0x0b, /* ec_point_formats */
1354 0x00, 0x02, /* 2 bytes */
1355 0x01, /* 1 point format */
1356 0x00, /* uncompressed */
1357 /* The following is only present in TLS 1.2 */
1358 0x00, 0x0d, /* signature_algorithms */
1359 0x00, 0x0c, /* 12 bytes */
1360 0x00, 0x0a, /* 10 bytes */
1361 0x05, 0x01, /* SHA-384/RSA */
1362 0x04, 0x01, /* SHA-256/RSA */
1363 0x02, 0x01, /* SHA-1/RSA */
1364 0x04, 0x03, /* SHA-256/ECDSA */
1365 0x02, 0x03, /* SHA-1/ECDSA */
1367 /* Length of the common prefix (first two extensions). */
1368 static const size_t kSafariCommonExtensionsLength
= 18;
1373 tmppkt
= hello
->extensions
;
1375 if (!PACKET_forward(&tmppkt
, 2)
1376 || !PACKET_get_net_2(&tmppkt
, &type
)
1377 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1381 if (type
!= TLSEXT_TYPE_server_name
)
1384 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1385 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1387 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1390 #endif /* !OPENSSL_NO_EC */
1392 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1394 /* |cookie| will only be initialized for DTLS. */
1395 PACKET session_id
, compression
, extensions
, cookie
;
1396 static const unsigned char null_compression
= 0;
1397 CLIENTHELLO_MSG
*clienthello
= NULL
;
1399 /* Check if this is actually an unexpected renegotiation ClientHello */
1400 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1401 if (!ossl_assert(!SSL_IS_TLS13(s
))) {
1402 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1403 ERR_R_INTERNAL_ERROR
);
1406 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
) != 0
1407 || (!s
->s3
.send_connection_binding
1409 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1410 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1411 return MSG_PROCESS_FINISHED_READING
;
1417 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1418 if (clienthello
== NULL
) {
1419 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1420 ERR_R_INTERNAL_ERROR
);
1425 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1427 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1428 PACKET_null_init(&cookie
);
1430 if (clienthello
->isv2
) {
1433 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1434 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1435 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1436 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1441 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1442 * header is sent directly on the wire, not wrapped as a TLS
1443 * record. Our record layer just processes the message length and passes
1444 * the rest right through. Its format is:
1446 * 0-1 msg_length - decoded by the record layer
1447 * 2 msg_type - s->init_msg points here
1449 * 5-6 cipher_spec_length
1450 * 7-8 session_id_length
1451 * 9-10 challenge_length
1455 if (!PACKET_get_1(pkt
, &mt
)
1456 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1458 * Should never happen. We should have tested this in the record
1459 * layer in order to have determined that this is a SSLv2 record
1460 * in the first place
1462 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1463 ERR_R_INTERNAL_ERROR
);
1468 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1469 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1470 SSL_R_LENGTH_TOO_SHORT
);
1474 /* Parse the message and load client random. */
1475 if (clienthello
->isv2
) {
1477 * Handle an SSLv2 backwards compatible ClientHello
1478 * Note, this is only for SSLv3+ using the backward compatible format.
1479 * Real SSLv2 is not supported, and is rejected below.
1481 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1484 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1485 || !PACKET_get_net_2(pkt
, &session_id_len
)
1486 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1487 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1488 SSL_R_RECORD_LENGTH_MISMATCH
);
1492 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1493 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1494 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1498 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1500 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1501 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1502 /* No extensions. */
1503 || PACKET_remaining(pkt
) != 0) {
1504 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1505 SSL_R_RECORD_LENGTH_MISMATCH
);
1508 clienthello
->session_id_len
= session_id_len
;
1510 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1511 * here rather than sizeof(clienthello->random) because that is the limit
1512 * for SSLv3 and it is fixed. It won't change even if
1513 * sizeof(clienthello->random) does.
1515 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1516 ? SSL3_RANDOM_SIZE
: challenge_len
;
1517 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1518 if (!PACKET_copy_bytes(&challenge
,
1519 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1520 challenge_len
, challenge_len
)
1521 /* Advertise only null compression. */
1522 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1523 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1524 ERR_R_INTERNAL_ERROR
);
1528 PACKET_null_init(&clienthello
->extensions
);
1530 /* Regular ClientHello. */
1531 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1532 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1533 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1534 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1535 &clienthello
->session_id_len
)) {
1536 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1537 SSL_R_LENGTH_MISMATCH
);
1541 if (SSL_IS_DTLS(s
)) {
1542 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1543 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1544 SSL_R_LENGTH_MISMATCH
);
1547 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1548 DTLS1_COOKIE_LENGTH
,
1549 &clienthello
->dtls_cookie_len
)) {
1550 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1551 SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1555 * If we require cookies and this ClientHello doesn't contain one,
1556 * just return since we do not want to allocate any memory yet.
1557 * So check cookie length...
1559 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1560 if (clienthello
->dtls_cookie_len
== 0) {
1561 OPENSSL_free(clienthello
);
1562 return MSG_PROCESS_FINISHED_READING
;
1567 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1568 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1569 SSL_R_LENGTH_MISMATCH
);
1573 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1574 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1575 SSL_R_LENGTH_MISMATCH
);
1579 /* Could be empty. */
1580 if (PACKET_remaining(pkt
) == 0) {
1581 PACKET_null_init(&clienthello
->extensions
);
1583 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1584 || PACKET_remaining(pkt
) != 0) {
1585 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1586 SSL_R_LENGTH_MISMATCH
);
1592 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1593 MAX_COMPRESSIONS_SIZE
,
1594 &clienthello
->compressions_len
)) {
1595 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1596 ERR_R_INTERNAL_ERROR
);
1600 /* Preserve the raw extensions PACKET for later use */
1601 extensions
= clienthello
->extensions
;
1602 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1603 &clienthello
->pre_proc_exts
,
1604 &clienthello
->pre_proc_exts_len
, 1)) {
1605 /* SSLfatal already been called */
1608 s
->clienthello
= clienthello
;
1610 return MSG_PROCESS_CONTINUE_PROCESSING
;
1613 if (clienthello
!= NULL
)
1614 OPENSSL_free(clienthello
->pre_proc_exts
);
1615 OPENSSL_free(clienthello
);
1617 return MSG_PROCESS_ERROR
;
1620 static int tls_early_post_process_client_hello(SSL
*s
)
1623 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1627 #ifndef OPENSSL_NO_COMP
1628 SSL_COMP
*comp
= NULL
;
1630 const SSL_CIPHER
*c
;
1631 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1632 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1633 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1634 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1636 /* Finished parsing the ClientHello, now we can start processing it */
1637 /* Give the ClientHello callback a crack at things */
1638 if (s
->ctx
->client_hello_cb
!= NULL
) {
1639 /* A failure in the ClientHello callback terminates the connection. */
1640 switch (s
->ctx
->client_hello_cb(s
, &al
, s
->ctx
->client_hello_cb_arg
)) {
1641 case SSL_CLIENT_HELLO_SUCCESS
:
1643 case SSL_CLIENT_HELLO_RETRY
:
1644 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1646 case SSL_CLIENT_HELLO_ERROR
:
1649 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1650 SSL_R_CALLBACK_FAILED
);
1655 /* Set up the client_random */
1656 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1658 /* Choose the version */
1660 if (clienthello
->isv2
) {
1661 if (clienthello
->legacy_version
== SSL2_VERSION
1662 || (clienthello
->legacy_version
& 0xff00)
1663 != (SSL3_VERSION_MAJOR
<< 8)) {
1665 * This is real SSLv2 or something completely unknown. We don't
1668 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1669 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1670 SSL_R_UNKNOWN_PROTOCOL
);
1674 s
->client_version
= clienthello
->legacy_version
;
1677 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1678 * versions are potentially compatible. Version negotiation comes later.
1680 if (!SSL_IS_DTLS(s
)) {
1681 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1682 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1683 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1684 protverr
= SSL_R_VERSION_TOO_LOW
;
1690 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1691 /* like ssl3_get_record, send alert using remote version number */
1692 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1694 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1695 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1699 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1700 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1701 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1702 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1703 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1707 if (SSL_IS_DTLS(s
)) {
1708 /* Empty cookie was already handled above by returning early. */
1709 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1710 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1711 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1712 clienthello
->dtls_cookie_len
) == 0) {
1713 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1714 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1715 SSL_R_COOKIE_MISMATCH
);
1717 /* else cookie verification succeeded */
1719 /* default verification */
1720 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1721 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1722 s
->d1
->cookie_len
) != 0) {
1723 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1724 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1725 SSL_R_COOKIE_MISMATCH
);
1728 s
->d1
->cookie_verified
= 1;
1730 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1731 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1732 if (protverr
!= 0) {
1733 s
->version
= s
->client_version
;
1734 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1735 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1743 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1744 clienthello
->isv2
) ||
1745 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1746 clienthello
->isv2
, 1)) {
1747 /* SSLfatal() already called */
1751 s
->s3
.send_connection_binding
= 0;
1752 /* Check what signalling cipher-suite values were received. */
1753 if (scsvs
!= NULL
) {
1754 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1755 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1756 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1757 if (s
->renegotiate
) {
1758 /* SCSV is fatal if renegotiating */
1759 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1760 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1761 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1764 s
->s3
.send_connection_binding
= 1;
1765 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1766 !ssl_check_version_downgrade(s
)) {
1768 * This SCSV indicates that the client previously tried
1769 * a higher version. We should fail if the current version
1770 * is an unexpected downgrade, as that indicates that the first
1771 * connection may have been tampered with in order to trigger
1772 * an insecure downgrade.
1774 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1775 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1776 SSL_R_INAPPROPRIATE_FALLBACK
);
1782 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1783 if (SSL_IS_TLS13(s
)) {
1784 const SSL_CIPHER
*cipher
=
1785 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(s
));
1787 if (cipher
== NULL
) {
1788 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1789 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1790 SSL_R_NO_SHARED_CIPHER
);
1793 if (s
->hello_retry_request
== SSL_HRR_PENDING
1794 && (s
->s3
.tmp
.new_cipher
== NULL
1795 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1797 * A previous HRR picked a different ciphersuite to the one we
1798 * just selected. Something must have changed.
1800 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1801 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1805 s
->s3
.tmp
.new_cipher
= cipher
;
1808 /* We need to do this before getting the session */
1809 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1810 SSL_EXT_CLIENT_HELLO
,
1811 clienthello
->pre_proc_exts
, NULL
, 0)) {
1812 /* SSLfatal() already called */
1817 * We don't allow resumption in a backwards compatible ClientHello.
1818 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1820 * Versions before 0.9.7 always allow clients to resume sessions in
1821 * renegotiation. 0.9.7 and later allow this by default, but optionally
1822 * ignore resumption requests with flag
1823 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1824 * than a change to default behavior so that applications relying on
1825 * this for security won't even compile against older library versions).
1826 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1827 * request renegotiation but not a new session (s->new_session remains
1828 * unset): for servers, this essentially just means that the
1829 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1832 if (clienthello
->isv2
||
1834 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1835 if (!ssl_get_new_session(s
, 1)) {
1836 /* SSLfatal() already called */
1840 i
= ssl_get_prev_session(s
, clienthello
);
1842 /* previous session */
1844 } else if (i
== -1) {
1845 /* SSLfatal() already called */
1849 if (!ssl_get_new_session(s
, 1)) {
1850 /* SSLfatal() already called */
1856 if (SSL_IS_TLS13(s
)) {
1857 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1858 s
->clienthello
->session_id_len
);
1859 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1863 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1864 * ciphersuite compatibility with the session as part of resumption.
1866 if (!SSL_IS_TLS13(s
) && s
->hit
) {
1868 id
= s
->session
->cipher
->id
;
1870 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1871 BIO_printf(trc_out
, "client sent %d ciphers\n",
1872 sk_SSL_CIPHER_num(ciphers
));
1874 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1875 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1876 if (trc_out
!= NULL
)
1877 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1878 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1886 * we need to have the cipher in the cipher list if we are asked
1889 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1890 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1891 SSL_R_REQUIRED_CIPHER_MISSING
);
1892 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1895 OSSL_TRACE_END(TLS_CIPHER
);
1898 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1899 if (clienthello
->compressions
[loop
] == 0)
1903 if (loop
>= clienthello
->compressions_len
) {
1905 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1906 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1907 SSL_R_NO_COMPRESSION_SPECIFIED
);
1911 #ifndef OPENSSL_NO_EC
1912 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1913 ssl_check_for_safari(s
, clienthello
);
1914 #endif /* !OPENSSL_NO_EC */
1916 /* TLS extensions */
1917 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1918 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1919 /* SSLfatal() already called */
1924 * Check if we want to use external pre-shared secret for this handshake
1925 * for not reused session only. We need to generate server_random before
1926 * calling tls_session_secret_cb in order to allow SessionTicket
1927 * processing to use it in key derivation.
1931 pos
= s
->s3
.server_random
;
1932 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1933 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1934 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1935 ERR_R_INTERNAL_ERROR
);
1941 && s
->version
>= TLS1_VERSION
1944 && s
->ext
.session_secret_cb
) {
1945 const SSL_CIPHER
*pref_cipher
= NULL
;
1947 * s->session->master_key_length is a size_t, but this is an int for
1948 * backwards compat reasons
1950 int master_key_length
;
1952 master_key_length
= sizeof(s
->session
->master_key
);
1953 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1954 &master_key_length
, ciphers
,
1956 s
->ext
.session_secret_cb_arg
)
1957 && master_key_length
> 0) {
1958 s
->session
->master_key_length
= master_key_length
;
1960 s
->peer_ciphers
= ciphers
;
1961 s
->session
->verify_result
= X509_V_OK
;
1965 /* check if some cipher was preferred by call back */
1966 if (pref_cipher
== NULL
)
1967 pref_cipher
= ssl3_choose_cipher(s
, s
->peer_ciphers
,
1968 SSL_get_ciphers(s
));
1969 if (pref_cipher
== NULL
) {
1970 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1971 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1972 SSL_R_NO_SHARED_CIPHER
);
1976 s
->session
->cipher
= pref_cipher
;
1977 sk_SSL_CIPHER_free(s
->cipher_list
);
1978 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1979 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1980 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1985 * Worst case, we will use the NULL compression, but if we have other
1986 * options, we will now look for them. We have complen-1 compression
1987 * algorithms from the client, starting at q.
1989 s
->s3
.tmp
.new_compression
= NULL
;
1990 if (SSL_IS_TLS13(s
)) {
1992 * We already checked above that the NULL compression method appears in
1993 * the list. Now we check there aren't any others (which is illegal in
1994 * a TLSv1.3 ClientHello.
1996 if (clienthello
->compressions_len
!= 1) {
1997 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1998 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1999 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
2003 #ifndef OPENSSL_NO_COMP
2004 /* This only happens if we have a cache hit */
2005 else if (s
->session
->compress_meth
!= 0) {
2006 int m
, comp_id
= s
->session
->compress_meth
;
2008 /* Perform sanity checks on resumed compression algorithm */
2009 /* Can't disable compression */
2010 if (!ssl_allow_compression(s
)) {
2011 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2012 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2013 SSL_R_INCONSISTENT_COMPRESSION
);
2016 /* Look for resumed compression method */
2017 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
2018 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2019 if (comp_id
== comp
->id
) {
2020 s
->s3
.tmp
.new_compression
= comp
;
2024 if (s
->s3
.tmp
.new_compression
== NULL
) {
2025 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2026 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2027 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
2030 /* Look for resumed method in compression list */
2031 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
2032 if (clienthello
->compressions
[k
] == comp_id
)
2035 if (k
>= clienthello
->compressions_len
) {
2036 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2037 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2038 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
2041 } else if (s
->hit
) {
2043 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
2044 /* See if we have a match */
2045 int m
, nn
, v
, done
= 0;
2048 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
2049 for (m
= 0; m
< nn
; m
++) {
2050 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2052 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2053 if (v
== clienthello
->compressions
[o
]) {
2062 s
->s3
.tmp
.new_compression
= comp
;
2068 * If compression is disabled we'd better not try to resume a session
2069 * using compression.
2071 if (s
->session
->compress_meth
!= 0) {
2072 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2073 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2074 SSL_R_INCONSISTENT_COMPRESSION
);
2080 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2083 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2084 sk_SSL_CIPHER_free(s
->peer_ciphers
);
2085 s
->peer_ciphers
= ciphers
;
2086 if (ciphers
== NULL
) {
2087 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2088 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2089 ERR_R_INTERNAL_ERROR
);
2096 #ifdef OPENSSL_NO_COMP
2097 s
->session
->compress_meth
= 0;
2099 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2101 if (!tls1_set_server_sigalgs(s
)) {
2102 /* SSLfatal() already called */
2107 sk_SSL_CIPHER_free(ciphers
);
2108 sk_SSL_CIPHER_free(scsvs
);
2109 OPENSSL_free(clienthello
->pre_proc_exts
);
2110 OPENSSL_free(s
->clienthello
);
2111 s
->clienthello
= NULL
;
2114 sk_SSL_CIPHER_free(ciphers
);
2115 sk_SSL_CIPHER_free(scsvs
);
2116 OPENSSL_free(clienthello
->pre_proc_exts
);
2117 OPENSSL_free(s
->clienthello
);
2118 s
->clienthello
= NULL
;
2124 * Call the status request callback if needed. Upon success, returns 1.
2125 * Upon failure, returns 0.
2127 static int tls_handle_status_request(SSL
*s
)
2129 s
->ext
.status_expected
= 0;
2132 * If status request then ask callback what to do. Note: this must be
2133 * called after servername callbacks in case the certificate has changed,
2134 * and must be called after the cipher has been chosen because this may
2135 * influence which certificate is sent
2137 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
2138 && s
->ctx
->ext
.status_cb
!= NULL
) {
2141 /* If no certificate can't return certificate status */
2142 if (s
->s3
.tmp
.cert
!= NULL
) {
2144 * Set current certificate to one we will use so SSL_get_certificate
2145 * et al can pick it up.
2147 s
->cert
->key
= s
->s3
.tmp
.cert
;
2148 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
2150 /* We don't want to send a status request response */
2151 case SSL_TLSEXT_ERR_NOACK
:
2152 s
->ext
.status_expected
= 0;
2154 /* status request response should be sent */
2155 case SSL_TLSEXT_ERR_OK
:
2156 if (s
->ext
.ocsp
.resp
)
2157 s
->ext
.status_expected
= 1;
2159 /* something bad happened */
2160 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2162 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2163 SSL_F_TLS_HANDLE_STATUS_REQUEST
,
2164 SSL_R_CLIENTHELLO_TLSEXT
);
2174 * Call the alpn_select callback if needed. Upon success, returns 1.
2175 * Upon failure, returns 0.
2177 int tls_handle_alpn(SSL
*s
)
2179 const unsigned char *selected
= NULL
;
2180 unsigned char selected_len
= 0;
2182 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2183 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
2184 s
->s3
.alpn_proposed
,
2185 (unsigned int)s
->s3
.alpn_proposed_len
,
2186 s
->ctx
->ext
.alpn_select_cb_arg
);
2188 if (r
== SSL_TLSEXT_ERR_OK
) {
2189 OPENSSL_free(s
->s3
.alpn_selected
);
2190 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2191 if (s
->s3
.alpn_selected
== NULL
) {
2192 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_HANDLE_ALPN
,
2193 ERR_R_INTERNAL_ERROR
);
2196 s
->s3
.alpn_selected_len
= selected_len
;
2197 #ifndef OPENSSL_NO_NEXTPROTONEG
2198 /* ALPN takes precedence over NPN. */
2202 /* Check ALPN is consistent with session */
2203 if (s
->session
->ext
.alpn_selected
== NULL
2204 || selected_len
!= s
->session
->ext
.alpn_selected_len
2205 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2206 selected_len
) != 0) {
2207 /* Not consistent so can't be used for early_data */
2208 s
->ext
.early_data_ok
= 0;
2212 * This is a new session and so alpn_selected should have
2213 * been initialised to NULL. We should update it with the
2216 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2217 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2218 SSL_F_TLS_HANDLE_ALPN
,
2219 ERR_R_INTERNAL_ERROR
);
2222 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2224 if (s
->session
->ext
.alpn_selected
== NULL
) {
2225 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2226 SSL_F_TLS_HANDLE_ALPN
,
2227 ERR_R_INTERNAL_ERROR
);
2230 s
->session
->ext
.alpn_selected_len
= selected_len
;
2235 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2236 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
, SSL_F_TLS_HANDLE_ALPN
,
2237 SSL_R_NO_APPLICATION_PROTOCOL
);
2241 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2246 /* Check ALPN is consistent with session */
2247 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2248 /* Not consistent so can't be used for early_data */
2249 s
->ext
.early_data_ok
= 0;
2255 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
2257 const SSL_CIPHER
*cipher
;
2259 if (wst
== WORK_MORE_A
) {
2260 int rv
= tls_early_post_process_client_hello(s
);
2262 /* SSLfatal() was already called */
2269 if (wst
== WORK_MORE_B
) {
2270 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2271 /* Let cert callback update server certificates if required */
2272 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2273 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
2275 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2276 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2277 SSL_R_CERT_CB_ERROR
);
2281 s
->rwstate
= SSL_X509_LOOKUP
;
2284 s
->rwstate
= SSL_NOTHING
;
2287 /* In TLSv1.3 we selected the ciphersuite before resumption */
2288 if (!SSL_IS_TLS13(s
)) {
2290 ssl3_choose_cipher(s
, s
->peer_ciphers
, SSL_get_ciphers(s
));
2292 if (cipher
== NULL
) {
2293 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2294 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2295 SSL_R_NO_SHARED_CIPHER
);
2298 s
->s3
.tmp
.new_cipher
= cipher
;
2301 if (!tls_choose_sigalg(s
, 1)) {
2302 /* SSLfatal already called */
2305 /* check whether we should disable session resumption */
2306 if (s
->not_resumable_session_cb
!= NULL
)
2307 s
->session
->not_resumable
=
2308 s
->not_resumable_session_cb(s
,
2309 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2310 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2311 if (s
->session
->not_resumable
)
2312 /* do not send a session ticket */
2313 s
->ext
.ticket_expected
= 0;
2316 /* Session-id reuse */
2317 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2321 * we now have the following setup.
2323 * cipher_list - our preferred list of ciphers
2324 * ciphers - the clients preferred list of ciphers
2325 * compression - basically ignored right now
2326 * ssl version is set - sslv3
2327 * s->session - The ssl session has been setup.
2328 * s->hit - session reuse flag
2329 * s->s3.tmp.new_cipher - the new cipher to use.
2333 * Call status_request callback if needed. Has to be done after the
2334 * certificate callbacks etc above.
2336 if (!tls_handle_status_request(s
)) {
2337 /* SSLfatal() already called */
2341 * Call alpn_select callback if needed. Has to be done after SNI and
2342 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2343 * we already did this because cipher negotiation happens earlier, and
2344 * we must handle ALPN before we decide whether to accept early_data.
2346 if (!SSL_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2347 /* SSLfatal() already called */
2353 #ifndef OPENSSL_NO_SRP
2354 if (wst
== WORK_MORE_C
) {
2356 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2358 * callback indicates further work to be done
2360 s
->rwstate
= SSL_X509_LOOKUP
;
2364 /* SSLfatal() already called */
2370 return WORK_FINISHED_STOP
;
2375 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2380 unsigned char *session_id
;
2381 int usetls13
= SSL_IS_TLS13(s
) || s
->hello_retry_request
== SSL_HRR_PENDING
;
2383 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2384 if (!WPACKET_put_bytes_u16(pkt
, version
)
2386 * Random stuff. Filling of the server_random takes place in
2387 * tls_process_client_hello()
2389 || !WPACKET_memcpy(pkt
,
2390 s
->hello_retry_request
== SSL_HRR_PENDING
2391 ? hrrrandom
: s
->s3
.server_random
,
2392 SSL3_RANDOM_SIZE
)) {
2393 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2394 ERR_R_INTERNAL_ERROR
);
2399 * There are several cases for the session ID to send
2400 * back in the server hello:
2401 * - For session reuse from the session cache,
2402 * we send back the old session ID.
2403 * - If stateless session reuse (using a session ticket)
2404 * is successful, we send back the client's "session ID"
2405 * (which doesn't actually identify the session).
2406 * - If it is a new session, we send back the new
2408 * - However, if we want the new session to be single-use,
2409 * we send back a 0-length session ID.
2410 * - In TLSv1.3 we echo back the session id sent to us by the client
2412 * s->hit is non-zero in either case of session reuse,
2413 * so the following won't overwrite an ID that we're supposed
2416 if (s
->session
->not_resumable
||
2417 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2419 s
->session
->session_id_length
= 0;
2422 sl
= s
->tmp_session_id_len
;
2423 session_id
= s
->tmp_session_id
;
2425 sl
= s
->session
->session_id_length
;
2426 session_id
= s
->session
->session_id
;
2429 if (sl
> sizeof(s
->session
->session_id
)) {
2430 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2431 ERR_R_INTERNAL_ERROR
);
2435 /* set up the compression method */
2436 #ifdef OPENSSL_NO_COMP
2439 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2442 compm
= s
->s3
.tmp
.new_compression
->id
;
2445 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2446 || !s
->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
, pkt
, &len
)
2447 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2448 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2449 ERR_R_INTERNAL_ERROR
);
2453 if (!tls_construct_extensions(s
, pkt
,
2454 s
->hello_retry_request
== SSL_HRR_PENDING
2455 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2457 ? SSL_EXT_TLS1_3_SERVER_HELLO
2458 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2460 /* SSLfatal() already called */
2464 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2465 /* Ditch the session. We'll create a new one next time around */
2466 SSL_SESSION_free(s
->session
);
2471 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2472 * a synthetic message_hash in place of ClientHello1.
2474 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2475 /* SSLfatal() already called */
2478 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2479 && !ssl3_digest_cached_records(s
, 0)) {
2480 /* SSLfatal() already called */;
2487 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2489 if (!s
->s3
.tmp
.cert_request
) {
2490 if (!ssl3_digest_cached_records(s
, 0)) {
2491 /* SSLfatal() already called */
2498 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2500 #ifndef OPENSSL_NO_DH
2501 EVP_PKEY
*pkdh
= NULL
;
2503 #ifndef OPENSSL_NO_EC
2504 unsigned char *encodedPoint
= NULL
;
2505 size_t encodedlen
= 0;
2508 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2512 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2513 EVP_PKEY_CTX
*pctx
= NULL
;
2514 size_t paramlen
, paramoffset
;
2516 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2517 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2518 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2522 if (md_ctx
== NULL
) {
2523 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2524 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2528 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2530 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2531 #ifndef OPENSSL_NO_PSK
2532 /* Plain PSK or RSAPSK nothing to do */
2533 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2535 #endif /* !OPENSSL_NO_PSK */
2536 #ifndef OPENSSL_NO_DH
2537 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2538 CERT
*cert
= s
->cert
;
2540 EVP_PKEY
*pkdhp
= NULL
;
2543 if (s
->cert
->dh_tmp_auto
) {
2544 DH
*dhp
= ssl_get_auto_dh(s
);
2545 pkdh
= EVP_PKEY_new();
2546 if (pkdh
== NULL
|| dhp
== NULL
) {
2548 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2549 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2550 ERR_R_INTERNAL_ERROR
);
2553 EVP_PKEY_assign_DH(pkdh
, dhp
);
2556 pkdhp
= cert
->dh_tmp
;
2558 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2559 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2560 pkdh
= ssl_dh_to_pkey(dhp
);
2562 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2563 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2564 ERR_R_INTERNAL_ERROR
);
2569 if (pkdhp
== NULL
) {
2570 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2571 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2572 SSL_R_MISSING_TMP_DH_KEY
);
2575 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2576 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2577 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2578 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2579 SSL_R_DH_KEY_TOO_SMALL
);
2582 if (s
->s3
.tmp
.pkey
!= NULL
) {
2583 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2584 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2585 ERR_R_INTERNAL_ERROR
);
2589 s
->s3
.tmp
.pkey
= ssl_generate_pkey(s
, pkdhp
);
2590 if (s
->s3
.tmp
.pkey
== NULL
) {
2591 /* SSLfatal() already called */
2595 dh
= EVP_PKEY_get0_DH(s
->s3
.tmp
.pkey
);
2597 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2598 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2599 ERR_R_INTERNAL_ERROR
);
2603 EVP_PKEY_free(pkdh
);
2606 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2607 DH_get0_key(dh
, &r
[2], NULL
);
2610 #ifndef OPENSSL_NO_EC
2611 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2613 if (s
->s3
.tmp
.pkey
!= NULL
) {
2614 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2615 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2616 ERR_R_INTERNAL_ERROR
);
2620 /* Get NID of appropriate shared curve */
2621 curve_id
= tls1_shared_group(s
, -2);
2622 if (curve_id
== 0) {
2623 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2624 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2625 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2628 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2629 /* Generate a new key for this curve */
2630 if (s
->s3
.tmp
.pkey
== NULL
) {
2631 /* SSLfatal() already called */
2635 /* Encode the public key. */
2636 encodedlen
= EVP_PKEY_get1_encoded_public_key(s
->s3
.tmp
.pkey
,
2638 if (encodedlen
== 0) {
2639 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2640 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2645 * We'll generate the serverKeyExchange message explicitly so we
2646 * can set these to NULLs
2653 #endif /* !OPENSSL_NO_EC */
2654 #ifndef OPENSSL_NO_SRP
2655 if (type
& SSL_kSRP
) {
2656 if ((s
->srp_ctx
.N
== NULL
) ||
2657 (s
->srp_ctx
.g
== NULL
) ||
2658 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2659 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2660 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2661 SSL_R_MISSING_SRP_PARAM
);
2664 r
[0] = s
->srp_ctx
.N
;
2665 r
[1] = s
->srp_ctx
.g
;
2666 r
[2] = s
->srp_ctx
.s
;
2667 r
[3] = s
->srp_ctx
.B
;
2671 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2672 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2673 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2677 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2678 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2680 } else if (lu
== NULL
) {
2681 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
2682 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2686 #ifndef OPENSSL_NO_PSK
2687 if (type
& SSL_PSK
) {
2688 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2689 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2692 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2693 * checked this when we set the identity hint - but just in case
2695 if (len
> PSK_MAX_IDENTITY_LEN
2696 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2698 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2699 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2700 ERR_R_INTERNAL_ERROR
);
2706 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2707 unsigned char *binval
;
2710 #ifndef OPENSSL_NO_SRP
2711 if ((i
== 2) && (type
& SSL_kSRP
)) {
2712 res
= WPACKET_start_sub_packet_u8(pkt
);
2715 res
= WPACKET_start_sub_packet_u16(pkt
);
2718 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2719 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2720 ERR_R_INTERNAL_ERROR
);
2724 #ifndef OPENSSL_NO_DH
2726 * for interoperability with some versions of the Microsoft TLS
2727 * stack, we need to zero pad the DHE pub key to the same length
2730 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2731 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2734 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2735 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2736 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2737 ERR_R_INTERNAL_ERROR
);
2740 memset(binval
, 0, len
);
2744 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2745 || !WPACKET_close(pkt
)) {
2746 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2747 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2748 ERR_R_INTERNAL_ERROR
);
2752 BN_bn2bin(r
[i
], binval
);
2755 #ifndef OPENSSL_NO_EC
2756 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2758 * We only support named (not generic) curves. In this situation, the
2759 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2760 * [1 byte length of encoded point], followed by the actual encoded
2763 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2764 || !WPACKET_put_bytes_u8(pkt
, 0)
2765 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2766 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2767 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2768 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2769 ERR_R_INTERNAL_ERROR
);
2772 OPENSSL_free(encodedPoint
);
2773 encodedPoint
= NULL
;
2779 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2781 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2782 size_t siglen
= 0, tbslen
;
2784 if (pkey
== NULL
|| !tls1_lookup_md(s
->ctx
, lu
, &md
)) {
2785 /* Should never happen */
2786 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2787 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2788 ERR_R_INTERNAL_ERROR
);
2791 /* Get length of the parameters we have written above */
2792 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2793 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2794 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2795 ERR_R_INTERNAL_ERROR
);
2798 /* send signature algorithm */
2799 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2800 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2801 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2802 ERR_R_INTERNAL_ERROR
);
2806 if (EVP_DigestSignInit_ex(md_ctx
, &pctx
,
2807 md
== NULL
? NULL
: EVP_MD_name(md
),
2808 s
->ctx
->libctx
, s
->ctx
->propq
, pkey
) <= 0) {
2809 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2810 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2811 ERR_R_INTERNAL_ERROR
);
2814 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2815 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2816 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2817 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2818 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2823 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2824 s
->init_buf
->data
+ paramoffset
,
2827 /* SSLfatal() already called */
2831 if (EVP_DigestSign(md_ctx
, NULL
, &siglen
, tbs
, tbslen
) <=0
2832 || !WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2833 || EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
) <= 0
2834 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2835 || sigbytes1
!= sigbytes2
) {
2837 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2838 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2839 ERR_R_INTERNAL_ERROR
);
2845 EVP_MD_CTX_free(md_ctx
);
2848 #ifndef OPENSSL_NO_DH
2849 EVP_PKEY_free(pkdh
);
2851 #ifndef OPENSSL_NO_EC
2852 OPENSSL_free(encodedPoint
);
2854 EVP_MD_CTX_free(md_ctx
);
2858 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2860 if (SSL_IS_TLS13(s
)) {
2861 /* Send random context when doing post-handshake auth */
2862 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2863 OPENSSL_free(s
->pha_context
);
2864 s
->pha_context_len
= 32;
2865 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
2866 || RAND_bytes_ex(s
->ctx
->libctx
, s
->pha_context
,
2867 s
->pha_context_len
) <= 0
2868 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
, s
->pha_context_len
)) {
2869 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2870 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2871 ERR_R_INTERNAL_ERROR
);
2874 /* reset the handshake hash back to just after the ClientFinished */
2875 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2876 /* SSLfatal() already called */
2880 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2881 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2882 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2883 ERR_R_INTERNAL_ERROR
);
2888 if (!tls_construct_extensions(s
, pkt
,
2889 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2891 /* SSLfatal() already called */
2897 /* get the list of acceptable cert types */
2898 if (!WPACKET_start_sub_packet_u8(pkt
)
2899 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2900 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2901 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2905 if (SSL_USE_SIGALGS(s
)) {
2906 const uint16_t *psigs
;
2907 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2909 if (!WPACKET_start_sub_packet_u16(pkt
)
2910 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2911 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2912 || !WPACKET_close(pkt
)) {
2913 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2914 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2915 ERR_R_INTERNAL_ERROR
);
2920 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2921 /* SSLfatal() already called */
2927 s
->s3
.tmp
.cert_request
= 1;
2931 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
)
2933 #ifndef OPENSSL_NO_PSK
2934 unsigned char psk
[PSK_MAX_PSK_LEN
];
2936 PACKET psk_identity
;
2938 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2939 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2940 SSL_R_LENGTH_MISMATCH
);
2943 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2944 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2945 SSL_R_DATA_LENGTH_TOO_LONG
);
2948 if (s
->psk_server_callback
== NULL
) {
2949 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2950 SSL_R_PSK_NO_SERVER_CB
);
2954 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2955 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2956 ERR_R_INTERNAL_ERROR
);
2960 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2963 if (psklen
> PSK_MAX_PSK_LEN
) {
2964 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2965 ERR_R_INTERNAL_ERROR
);
2967 } else if (psklen
== 0) {
2969 * PSK related to the given identity not found
2971 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
2972 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2973 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2977 OPENSSL_free(s
->s3
.tmp
.psk
);
2978 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2979 OPENSSL_cleanse(psk
, psklen
);
2981 if (s
->s3
.tmp
.psk
== NULL
) {
2982 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2983 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2987 s
->s3
.tmp
.psklen
= psklen
;
2991 /* Should never happen */
2992 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2993 ERR_R_INTERNAL_ERROR
);
2998 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
)
3000 #ifndef OPENSSL_NO_RSA
3002 PACKET enc_premaster
;
3003 EVP_PKEY
*rsa
= NULL
;
3004 unsigned char *rsa_decrypt
= NULL
;
3006 EVP_PKEY_CTX
*ctx
= NULL
;
3007 OSSL_PARAM params
[3], *p
= params
;
3009 rsa
= s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
;
3011 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3012 SSL_R_MISSING_RSA_CERTIFICATE
);
3016 /* SSLv3 and pre-standard DTLS omit the length bytes. */
3017 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
3018 enc_premaster
= *pkt
;
3020 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
3021 || PACKET_remaining(pkt
) != 0) {
3022 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3023 SSL_R_LENGTH_MISMATCH
);
3028 outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
3029 rsa_decrypt
= OPENSSL_malloc(outlen
);
3030 if (rsa_decrypt
== NULL
) {
3031 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3032 ERR_R_MALLOC_FAILURE
);
3036 ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, rsa
, s
->ctx
->propq
);
3038 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3039 ERR_R_MALLOC_FAILURE
);
3044 * We must not leak whether a decryption failure occurs because of
3045 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
3046 * section 7.4.7.1). We use the special padding type
3047 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automaticaly decrypt the
3048 * RSA, check the padding and check that the client version is as expected
3049 * in the premaster secret. If any of that fails then the function appears
3050 * to return successfully but with a random result. The call below could
3051 * still fail if the input is publicly invalid.
3052 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
3054 if (EVP_PKEY_decrypt_init(ctx
) <= 0
3055 || EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_WITH_TLS_PADDING
) <= 0) {
3056 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3057 SSL_R_DECRYPTION_FAILED
);
3061 *p
++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
,
3062 (unsigned int *)&s
->client_version
);
3063 if ((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) != 0)
3064 *p
++ = OSSL_PARAM_construct_uint(
3065 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
,
3066 (unsigned int *)&s
->version
);
3067 *p
++ = OSSL_PARAM_construct_end();
3069 if (!EVP_PKEY_CTX_set_params(ctx
, params
)
3070 || EVP_PKEY_decrypt(ctx
, rsa_decrypt
, &outlen
,
3071 PACKET_data(&enc_premaster
),
3072 PACKET_remaining(&enc_premaster
)) <= 0) {
3073 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3074 SSL_R_DECRYPTION_FAILED
);
3079 * This test should never fail (otherwise we should have failed above) but
3080 * we double check anyway.
3082 if (outlen
!= SSL_MAX_MASTER_KEY_LENGTH
) {
3083 OPENSSL_cleanse(rsa_decrypt
, SSL_MAX_MASTER_KEY_LENGTH
);
3084 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3085 SSL_R_DECRYPTION_FAILED
);
3089 /* Also cleanses rsa_decrypt (on success or failure) */
3090 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
3091 SSL_MAX_MASTER_KEY_LENGTH
, 0)) {
3092 /* SSLfatal() already called */
3098 OPENSSL_free(rsa_decrypt
);
3099 EVP_PKEY_CTX_free(ctx
);
3102 /* Should never happen */
3103 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3104 ERR_R_INTERNAL_ERROR
);
3109 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
)
3111 #ifndef OPENSSL_NO_DH
3112 EVP_PKEY
*skey
= NULL
;
3116 const unsigned char *data
;
3117 EVP_PKEY
*ckey
= NULL
;
3120 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
3121 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3122 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
3125 skey
= s
->s3
.tmp
.pkey
;
3127 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3128 SSL_R_MISSING_TMP_DH_KEY
);
3132 if (PACKET_remaining(pkt
) == 0L) {
3133 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3134 SSL_R_MISSING_TMP_DH_KEY
);
3137 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3138 /* We already checked we have enough data */
3139 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3140 ERR_R_INTERNAL_ERROR
);
3143 ckey
= EVP_PKEY_new();
3144 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3145 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3146 SSL_R_COPY_PARAMETERS_FAILED
);
3150 cdh
= EVP_PKEY_get0_DH(ckey
);
3151 pub_key
= BN_bin2bn(data
, i
, NULL
);
3152 if (pub_key
== NULL
|| cdh
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
3153 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3154 ERR_R_INTERNAL_ERROR
);
3159 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3160 /* SSLfatal() already called */
3165 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3166 s
->s3
.tmp
.pkey
= NULL
;
3168 EVP_PKEY_free(ckey
);
3171 /* Should never happen */
3172 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3173 ERR_R_INTERNAL_ERROR
);
3178 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
)
3180 #ifndef OPENSSL_NO_EC
3181 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3182 EVP_PKEY
*ckey
= NULL
;
3185 if (PACKET_remaining(pkt
) == 0L) {
3186 /* We don't support ECDH client auth */
3187 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3188 SSL_R_MISSING_TMP_ECDH_KEY
);
3192 const unsigned char *data
;
3195 * Get client's public key from encoded point in the
3196 * ClientKeyExchange message.
3199 /* Get encoded point length */
3200 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3201 || PACKET_remaining(pkt
) != 0) {
3202 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3203 SSL_R_LENGTH_MISMATCH
);
3207 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3208 SSL_R_MISSING_TMP_ECDH_KEY
);
3212 ckey
= EVP_PKEY_new();
3213 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3214 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3215 SSL_R_COPY_PARAMETERS_FAILED
);
3219 if (EVP_PKEY_set1_encoded_public_key(ckey
, data
, i
) <= 0) {
3220 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3226 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3227 /* SSLfatal() already called */
3232 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3233 s
->s3
.tmp
.pkey
= NULL
;
3235 EVP_PKEY_free(ckey
);
3239 /* Should never happen */
3240 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3241 ERR_R_INTERNAL_ERROR
);
3246 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
)
3248 #ifndef OPENSSL_NO_SRP
3250 const unsigned char *data
;
3252 if (!PACKET_get_net_2(pkt
, &i
)
3253 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3254 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3255 SSL_R_BAD_SRP_A_LENGTH
);
3258 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3259 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3263 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3264 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS_PROCESS_CKE_SRP
,
3265 SSL_R_BAD_SRP_PARAMETERS
);
3268 OPENSSL_free(s
->session
->srp_username
);
3269 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3270 if (s
->session
->srp_username
== NULL
) {
3271 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3272 ERR_R_MALLOC_FAILURE
);
3276 if (!srp_generate_server_master_secret(s
)) {
3277 /* SSLfatal() already called */
3283 /* Should never happen */
3284 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3285 ERR_R_INTERNAL_ERROR
);
3290 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
)
3292 #ifndef OPENSSL_NO_GOST
3293 EVP_PKEY_CTX
*pkey_ctx
;
3294 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3295 unsigned char premaster_secret
[32];
3296 const unsigned char *start
;
3297 size_t outlen
= 32, inlen
;
3298 unsigned long alg_a
;
3299 GOST_KX_MESSAGE
*pKX
= NULL
;
3300 const unsigned char *ptr
;
3303 /* Get our certificate private key */
3304 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3305 if (alg_a
& SSL_aGOST12
) {
3307 * New GOST ciphersuites have SSL_aGOST01 bit too
3309 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3311 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3314 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3316 } else if (alg_a
& SSL_aGOST01
) {
3317 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3320 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, pk
, s
->ctx
->propq
);
3321 if (pkey_ctx
== NULL
) {
3322 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3323 ERR_R_MALLOC_FAILURE
);
3326 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3327 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3328 ERR_R_INTERNAL_ERROR
);
3332 * If client certificate is present and is of the same type, maybe
3333 * use it for key exchange. Don't mind errors from
3334 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3335 * client certificate for authorization only.
3337 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3338 if (client_pub_pkey
) {
3339 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3343 ptr
= PACKET_data(pkt
);
3344 /* Some implementations provide extra data in the opaqueBlob
3345 * We have nothing to do with this blob so we just skip it */
3346 pKX
= d2i_GOST_KX_MESSAGE(NULL
, &ptr
, PACKET_remaining(pkt
));
3348 || pKX
->kxBlob
== NULL
3349 || ASN1_TYPE_get(pKX
->kxBlob
) != V_ASN1_SEQUENCE
) {
3350 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3351 SSL_R_DECRYPTION_FAILED
);
3355 if (!PACKET_forward(pkt
, ptr
- PACKET_data(pkt
))) {
3356 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3357 SSL_R_DECRYPTION_FAILED
);
3361 if (PACKET_remaining(pkt
) != 0) {
3362 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3363 SSL_R_DECRYPTION_FAILED
);
3367 inlen
= pKX
->kxBlob
->value
.sequence
->length
;
3368 start
= pKX
->kxBlob
->value
.sequence
->data
;
3370 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3372 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3373 SSL_R_DECRYPTION_FAILED
);
3376 /* Generate master secret */
3377 if (!ssl_generate_master_secret(s
, premaster_secret
,
3378 sizeof(premaster_secret
), 0)) {
3379 /* SSLfatal() already called */
3382 /* Check if pubkey from client certificate was used */
3383 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3385 s
->statem
.no_cert_verify
= 1;
3389 EVP_PKEY_CTX_free(pkey_ctx
);
3390 GOST_KX_MESSAGE_free(pKX
);
3393 /* Should never happen */
3394 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3395 ERR_R_INTERNAL_ERROR
);
3400 static int tls_process_cke_gost18(SSL
*s
, PACKET
*pkt
)
3402 #ifndef OPENSSL_NO_GOST
3403 unsigned char rnd_dgst
[32];
3404 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3405 EVP_PKEY
*pk
= NULL
;
3406 unsigned char premaster_secret
[32];
3407 const unsigned char *start
= NULL
;
3408 size_t outlen
= 32, inlen
= 0;
3410 int cipher_nid
= gost18_cke_cipher_nid(s
);
3412 if (cipher_nid
== NID_undef
) {
3413 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CKE_GOST18
,
3414 ERR_R_INTERNAL_ERROR
);
3418 if (gost_ukm(s
, rnd_dgst
) <= 0) {
3419 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CKE_GOST18
,
3420 ERR_R_INTERNAL_ERROR
);
3424 /* Get our certificate private key */
3425 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
!= NULL
?
3426 s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
:
3427 s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3429 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3430 SSL_R_BAD_HANDSHAKE_STATE
);
3434 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, pk
, s
->ctx
->propq
);
3435 if (pkey_ctx
== NULL
) {
3436 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3437 ERR_R_MALLOC_FAILURE
);
3440 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3441 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3442 ERR_R_INTERNAL_ERROR
);
3446 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3447 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3448 EVP_PKEY_CTRL_SET_IV
, 32, rnd_dgst
) < 0) {
3449 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3454 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3455 EVP_PKEY_CTRL_CIPHER
, cipher_nid
, NULL
) < 0) {
3456 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3460 inlen
= PACKET_remaining(pkt
);
3461 start
= PACKET_data(pkt
);
3463 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3464 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3465 SSL_R_DECRYPTION_FAILED
);
3468 /* Generate master secret */
3469 if (!ssl_generate_master_secret(s
, premaster_secret
,
3470 sizeof(premaster_secret
), 0)) {
3471 /* SSLfatal() already called */
3477 EVP_PKEY_CTX_free(pkey_ctx
);
3480 /* Should never happen */
3481 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3482 ERR_R_INTERNAL_ERROR
);
3487 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3489 unsigned long alg_k
;
3491 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3493 /* For PSK parse and retrieve identity, obtain PSK key */
3494 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3495 /* SSLfatal() already called */
3499 if (alg_k
& SSL_kPSK
) {
3500 /* Identity extracted earlier: should be nothing left */
3501 if (PACKET_remaining(pkt
) != 0) {
3502 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3503 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3504 SSL_R_LENGTH_MISMATCH
);
3507 /* PSK handled by ssl_generate_master_secret */
3508 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3509 /* SSLfatal() already called */
3512 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3513 if (!tls_process_cke_rsa(s
, pkt
)) {
3514 /* SSLfatal() already called */
3517 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3518 if (!tls_process_cke_dhe(s
, pkt
)) {
3519 /* SSLfatal() already called */
3522 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3523 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3524 /* SSLfatal() already called */
3527 } else if (alg_k
& SSL_kSRP
) {
3528 if (!tls_process_cke_srp(s
, pkt
)) {
3529 /* SSLfatal() already called */
3532 } else if (alg_k
& SSL_kGOST
) {
3533 if (!tls_process_cke_gost(s
, pkt
)) {
3534 /* SSLfatal() already called */
3537 } else if (alg_k
& SSL_kGOST18
) {
3538 if (!tls_process_cke_gost18(s
, pkt
)) {
3539 /* SSLfatal() already called */
3543 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3544 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3545 SSL_R_UNKNOWN_CIPHER_TYPE
);
3549 return MSG_PROCESS_CONTINUE_PROCESSING
;
3551 #ifndef OPENSSL_NO_PSK
3552 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3553 s
->s3
.tmp
.psk
= NULL
;
3555 return MSG_PROCESS_ERROR
;
3558 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3560 #ifndef OPENSSL_NO_SCTP
3561 if (wst
== WORK_MORE_A
) {
3562 if (SSL_IS_DTLS(s
)) {
3563 unsigned char sctpauthkey
[64];
3564 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3567 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3570 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3571 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3573 /* Don't include the terminating zero. */
3574 labellen
= sizeof(labelbuffer
) - 1;
3575 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3578 if (SSL_export_keying_material(s
, sctpauthkey
,
3579 sizeof(sctpauthkey
), labelbuffer
,
3582 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3583 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3584 ERR_R_INTERNAL_ERROR
);
3588 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3589 sizeof(sctpauthkey
), sctpauthkey
);
3594 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3596 * No certificate verify or no peer certificate so we no longer need
3597 * the handshake_buffer
3599 if (!ssl3_digest_cached_records(s
, 0)) {
3600 /* SSLfatal() already called */
3603 return WORK_FINISHED_CONTINUE
;
3605 if (!s
->s3
.handshake_buffer
) {
3606 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3607 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3608 ERR_R_INTERNAL_ERROR
);
3612 * For sigalgs freeze the handshake buffer. If we support
3613 * extms we've done this already so this is a no-op
3615 if (!ssl3_digest_cached_records(s
, 1)) {
3616 /* SSLfatal() already called */
3621 return WORK_FINISHED_CONTINUE
;
3624 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3627 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3630 const unsigned char *certstart
, *certbytes
;
3631 STACK_OF(X509
) *sk
= NULL
;
3632 PACKET spkt
, context
;
3634 SSL_SESSION
*new_sess
= NULL
;
3637 * To get this far we must have read encrypted data from the client. We no
3638 * longer tolerate unencrypted alerts. This value is ignored if less than
3641 s
->statem
.enc_read_state
= ENC_READ_STATE_VALID
;
3643 if ((sk
= sk_X509_new_null()) == NULL
) {
3644 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3645 ERR_R_MALLOC_FAILURE
);
3649 if (SSL_IS_TLS13(s
) && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3650 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3651 || (s
->pha_context
!= NULL
&&
3652 !PACKET_equal(&context
, s
->pha_context
, s
->pha_context_len
)))) {
3653 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3654 SSL_R_INVALID_CONTEXT
);
3658 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3659 || PACKET_remaining(pkt
) != 0) {
3660 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3661 SSL_R_LENGTH_MISMATCH
);
3665 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3666 if (!PACKET_get_net_3(&spkt
, &l
)
3667 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3668 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3669 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3670 SSL_R_CERT_LENGTH_MISMATCH
);
3674 certstart
= certbytes
;
3675 x
= X509_new_ex(s
->ctx
->libctx
, s
->ctx
->propq
);
3677 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3678 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3681 if (d2i_X509(&x
, (const unsigned char **)&certbytes
, l
) == NULL
) {
3682 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3683 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3687 if (certbytes
!= (certstart
+ l
)) {
3688 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3689 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3690 SSL_R_CERT_LENGTH_MISMATCH
);
3694 if (SSL_IS_TLS13(s
)) {
3695 RAW_EXTENSION
*rawexts
= NULL
;
3698 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3699 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3700 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3704 if (!tls_collect_extensions(s
, &extensions
,
3705 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3706 NULL
, chainidx
== 0)
3707 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3708 rawexts
, x
, chainidx
,
3709 PACKET_remaining(&spkt
) == 0)) {
3710 OPENSSL_free(rawexts
);
3713 OPENSSL_free(rawexts
);
3716 if (!sk_X509_push(sk
, x
)) {
3717 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3718 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3719 ERR_R_MALLOC_FAILURE
);
3725 if (sk_X509_num(sk
) <= 0) {
3726 /* TLS does not mind 0 certs returned */
3727 if (s
->version
== SSL3_VERSION
) {
3728 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3729 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3730 SSL_R_NO_CERTIFICATES_RETURNED
);
3733 /* Fail for TLS only if we required a certificate */
3734 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3735 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3736 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3737 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3738 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3741 /* No client certificate so digest cached records */
3742 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3743 /* SSLfatal() already called */
3748 i
= ssl_verify_cert_chain(s
, sk
);
3750 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3751 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3752 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3756 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3757 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3760 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3762 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3763 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3764 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3770 * Sessions must be immutable once they go into the session cache. Otherwise
3771 * we can get multi-thread problems. Therefore we don't "update" sessions,
3772 * we replace them with a duplicate. Here, we need to do this every time
3773 * a new certificate is received via post-handshake authentication, as the
3774 * session may have already gone into the session cache.
3777 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3778 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3779 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3780 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3781 ERR_R_MALLOC_FAILURE
);
3785 SSL_SESSION_free(s
->session
);
3786 s
->session
= new_sess
;
3789 X509_free(s
->session
->peer
);
3790 s
->session
->peer
= sk_X509_shift(sk
);
3791 s
->session
->verify_result
= s
->verify_result
;
3793 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3794 s
->session
->peer_chain
= sk
;
3797 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3800 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3801 /* SSLfatal() already called */
3806 * Inconsistency alert: cert_chain does *not* include the peer's own
3807 * certificate, while we do include it in statem_clnt.c
3811 /* Save the current hash state for when we receive the CertificateVerify */
3812 if (SSL_IS_TLS13(s
)) {
3813 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3814 sizeof(s
->cert_verify_hash
),
3815 &s
->cert_verify_hash_len
)) {
3816 /* SSLfatal() already called */
3820 /* Resend session tickets */
3821 s
->sent_tickets
= 0;
3824 ret
= MSG_PROCESS_CONTINUE_READING
;
3828 sk_X509_pop_free(sk
, X509_free
);
3832 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3834 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3837 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3838 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3843 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3844 * for the server Certificate message
3846 if (SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3847 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3848 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3851 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3852 /* SSLfatal() already called */
3859 static int create_ticket_prequel(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3860 unsigned char *tick_nonce
)
3863 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3864 * unspecified for resumed session (for simplicity).
3865 * In TLSv1.3 we reset the "time" field above, and always specify the
3868 if (!WPACKET_put_bytes_u32(pkt
,
3869 (s
->hit
&& !SSL_IS_TLS13(s
))
3870 ? 0 : s
->session
->timeout
)) {
3871 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3872 ERR_R_INTERNAL_ERROR
);
3876 if (SSL_IS_TLS13(s
)) {
3877 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3878 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3879 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3880 ERR_R_INTERNAL_ERROR
);
3885 /* Start the sub-packet for the actual ticket data */
3886 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3887 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3888 ERR_R_INTERNAL_ERROR
);
3895 static int construct_stateless_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3896 unsigned char *tick_nonce
)
3898 unsigned char *senc
= NULL
;
3899 EVP_CIPHER_CTX
*ctx
= NULL
;
3900 SSL_HMAC
*hctx
= NULL
;
3901 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3902 const unsigned char *const_p
;
3903 int len
, slen_full
, slen
, lenfinal
;
3906 SSL_CTX
*tctx
= s
->session_ctx
;
3907 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3908 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3910 size_t macoffset
, macendoffset
;
3912 /* get session encoding length */
3913 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3915 * Some length values are 16 bits, so forget it if session is too
3918 if (slen_full
== 0 || slen_full
> 0xFF00) {
3919 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3920 ERR_R_INTERNAL_ERROR
);
3923 senc
= OPENSSL_malloc(slen_full
);
3925 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3926 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_MALLOC_FAILURE
);
3930 ctx
= EVP_CIPHER_CTX_new();
3931 hctx
= ssl_hmac_new(tctx
);
3932 if (ctx
== NULL
|| hctx
== NULL
) {
3933 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3934 ERR_R_MALLOC_FAILURE
);
3939 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3940 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3941 ERR_R_INTERNAL_ERROR
);
3946 * create a fresh copy (not shared with other threads) to clean up
3949 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3951 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3952 ERR_R_INTERNAL_ERROR
);
3956 slen
= i2d_SSL_SESSION(sess
, NULL
);
3957 if (slen
== 0 || slen
> slen_full
) {
3958 /* shouldn't ever happen */
3959 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3960 ERR_R_INTERNAL_ERROR
);
3961 SSL_SESSION_free(sess
);
3965 if (!i2d_SSL_SESSION(sess
, &p
)) {
3966 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3967 ERR_R_INTERNAL_ERROR
);
3968 SSL_SESSION_free(sess
);
3971 SSL_SESSION_free(sess
);
3974 * Initialize HMAC and cipher contexts. If callback present it does
3975 * all the work otherwise use generated values from parent ctx.
3977 #ifndef OPENSSL_NO_DEPRECATED_3_0
3978 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
|| tctx
->ext
.ticket_key_cb
!= NULL
)
3980 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3985 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3986 ret
= tctx
->ext
.ticket_key_evp_cb(s
, key_name
, iv
, ctx
,
3987 ssl_hmac_get0_EVP_MAC_CTX(hctx
),
3989 #ifndef OPENSSL_NO_DEPRECATED_3_0
3990 else if (tctx
->ext
.ticket_key_cb
!= NULL
)
3991 /* if 0 is returned, write an empty ticket */
3992 ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3993 ssl_hmac_get0_HMAC_CTX(hctx
), 1);
3998 /* Put timeout and length */
3999 if (!WPACKET_put_bytes_u32(pkt
, 0)
4000 || !WPACKET_put_bytes_u16(pkt
, 0)) {
4001 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4002 SSL_F_CONSTRUCT_STATELESS_TICKET
,
4003 ERR_R_INTERNAL_ERROR
);
4007 EVP_CIPHER_CTX_free(ctx
);
4008 ssl_hmac_free(hctx
);
4012 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4013 SSL_R_CALLBACK_FAILED
);
4016 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
4018 EVP_CIPHER
*cipher
= EVP_CIPHER_fetch(s
->ctx
->libctx
, "AES-256-CBC",
4021 if (cipher
== NULL
) {
4022 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4023 SSL_R_ALGORITHM_FETCH_FAILED
);
4027 iv_len
= EVP_CIPHER_iv_length(cipher
);
4028 if (RAND_bytes_ex(s
->ctx
->libctx
, iv
, iv_len
) <= 0
4029 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
4030 tctx
->ext
.secure
->tick_aes_key
, iv
)
4031 || !ssl_hmac_init(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
4032 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
4034 EVP_CIPHER_free(cipher
);
4035 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4036 ERR_R_INTERNAL_ERROR
);
4039 EVP_CIPHER_free(cipher
);
4040 memcpy(key_name
, tctx
->ext
.tick_key_name
,
4041 sizeof(tctx
->ext
.tick_key_name
));
4044 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
4045 /* SSLfatal() already called */
4049 if (!WPACKET_get_total_written(pkt
, &macoffset
)
4050 /* Output key name */
4051 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
4053 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
4054 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
4056 /* Encrypt session data */
4057 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
4058 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
4059 || encdata1
!= encdata2
4060 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
4061 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
4062 || encdata1
+ len
!= encdata2
4063 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
4064 || !WPACKET_get_total_written(pkt
, &macendoffset
)
4065 || !ssl_hmac_update(hctx
,
4066 (unsigned char *)s
->init_buf
->data
+ macoffset
,
4067 macendoffset
- macoffset
)
4068 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
4069 || !ssl_hmac_final(hctx
, macdata1
, &hlen
, EVP_MAX_MD_SIZE
)
4070 || hlen
> EVP_MAX_MD_SIZE
4071 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
4072 || macdata1
!= macdata2
) {
4073 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4074 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_INTERNAL_ERROR
);
4078 /* Close the sub-packet created by create_ticket_prequel() */
4079 if (!WPACKET_close(pkt
)) {
4080 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4081 ERR_R_INTERNAL_ERROR
);
4088 EVP_CIPHER_CTX_free(ctx
);
4089 ssl_hmac_free(hctx
);
4093 static int construct_stateful_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
4094 unsigned char *tick_nonce
)
4096 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
4097 /* SSLfatal() already called */
4101 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
4102 s
->session
->session_id_length
)
4103 || !WPACKET_close(pkt
)) {
4104 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATEFUL_TICKET
,
4105 ERR_R_INTERNAL_ERROR
);
4112 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
4114 SSL_CTX
*tctx
= s
->session_ctx
;
4115 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
4117 unsigned char age_add_c
[sizeof(uint32_t)];
4121 age_add_u
.age_add
= 0;
4123 if (SSL_IS_TLS13(s
)) {
4126 static const unsigned char nonce_label
[] = "resumption";
4127 const EVP_MD
*md
= ssl_handshake_md(s
);
4128 int hashleni
= EVP_MD_size(md
);
4130 /* Ensure cast to size_t is safe */
4131 if (!ossl_assert(hashleni
>= 0)) {
4132 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4133 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4134 ERR_R_INTERNAL_ERROR
);
4137 hashlen
= (size_t)hashleni
;
4140 * If we already sent one NewSessionTicket, or we resumed then
4141 * s->session may already be in a cache and so we must not modify it.
4142 * Instead we need to take a copy of it and modify that.
4144 if (s
->sent_tickets
!= 0 || s
->hit
) {
4145 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
4147 if (new_sess
== NULL
) {
4148 /* SSLfatal already called */
4152 SSL_SESSION_free(s
->session
);
4153 s
->session
= new_sess
;
4156 if (!ssl_generate_session_id(s
, s
->session
)) {
4157 /* SSLfatal() already called */
4160 if (RAND_bytes_ex(s
->ctx
->libctx
, age_add_u
.age_add_c
,
4161 sizeof(age_add_u
)) <= 0) {
4162 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4163 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4164 ERR_R_INTERNAL_ERROR
);
4167 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
4169 nonce
= s
->next_ticket_nonce
;
4170 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
4171 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
4175 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
4177 sizeof(nonce_label
) - 1,
4180 s
->session
->master_key
,
4182 /* SSLfatal() already called */
4185 s
->session
->master_key_length
= hashlen
;
4187 s
->session
->time
= (long)time(NULL
);
4188 if (s
->s3
.alpn_selected
!= NULL
) {
4189 OPENSSL_free(s
->session
->ext
.alpn_selected
);
4190 s
->session
->ext
.alpn_selected
=
4191 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
4192 if (s
->session
->ext
.alpn_selected
== NULL
) {
4193 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4194 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4195 ERR_R_MALLOC_FAILURE
);
4198 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
4200 s
->session
->ext
.max_early_data
= s
->max_early_data
;
4203 if (tctx
->generate_ticket_cb
!= NULL
&&
4204 tctx
->generate_ticket_cb(s
, tctx
->ticket_cb_data
) == 0)
4208 * If we are using anti-replay protection then we behave as if
4209 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4210 * is no point in using full stateless tickets.
4213 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4214 || (s
->max_early_data
> 0
4215 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4216 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4217 /* SSLfatal() already called */
4220 } else if (!construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4222 /* SSLfatal() already called */
4226 if (SSL_IS_TLS13(s
)) {
4227 if (!tls_construct_extensions(s
, pkt
,
4228 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4230 /* SSLfatal() already called */
4234 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4235 * gets reset to 0 if we send more tickets following a post-handshake
4236 * auth, but |next_ticket_nonce| does not. If we're sending extra
4237 * tickets, decrement the count of pending extra tickets.
4240 s
->next_ticket_nonce
++;
4241 if (s
->ext
.extra_tickets_expected
> 0)
4242 s
->ext
.extra_tickets_expected
--;
4243 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4252 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4253 * create a separate message. Returns 1 on success or 0 on failure.
4255 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
4257 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4258 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4259 s
->ext
.ocsp
.resp_len
)) {
4260 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
,
4261 ERR_R_INTERNAL_ERROR
);
4268 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
4270 if (!tls_construct_cert_status_body(s
, pkt
)) {
4271 /* SSLfatal() already called */
4278 #ifndef OPENSSL_NO_NEXTPROTONEG
4280 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4281 * It sets the next_proto member in s if found
4283 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
4285 PACKET next_proto
, padding
;
4286 size_t next_proto_len
;
4289 * The payload looks like:
4291 * uint8 proto[proto_len];
4292 * uint8 padding_len;
4293 * uint8 padding[padding_len];
4295 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4296 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4297 || PACKET_remaining(pkt
) > 0) {
4298 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4299 SSL_R_LENGTH_MISMATCH
);
4300 return MSG_PROCESS_ERROR
;
4303 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4305 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4306 ERR_R_INTERNAL_ERROR
);
4307 return MSG_PROCESS_ERROR
;
4310 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4312 return MSG_PROCESS_CONTINUE_READING
;
4316 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
4318 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4320 /* SSLfatal() already called */
4327 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL
*s
, PACKET
*pkt
)
4329 if (PACKET_remaining(pkt
) != 0) {
4330 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4331 SSL_R_LENGTH_MISMATCH
);
4332 return MSG_PROCESS_ERROR
;
4335 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4336 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4337 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4338 ERR_R_INTERNAL_ERROR
);
4339 return MSG_PROCESS_ERROR
;
4343 * EndOfEarlyData signals a key change so the end of the message must be on
4344 * a record boundary.
4346 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4347 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
4348 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4349 SSL_R_NOT_ON_RECORD_BOUNDARY
);
4350 return MSG_PROCESS_ERROR
;
4353 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4354 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
4355 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4356 /* SSLfatal() already called */
4357 return MSG_PROCESS_ERROR
;
4360 return MSG_PROCESS_CONTINUE_READING
;