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>
30 DEFINE_STACK_OF(SSL_COMP
)
31 DEFINE_STACK_OF_CONST(SSL_CIPHER
)
33 #define TICKET_NONCE_SIZE 8
37 ASN1_TYPE
*opaqueBlob
;
40 DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE
)
42 ASN1_SEQUENCE(GOST_KX_MESSAGE
) = {
43 ASN1_SIMPLE(GOST_KX_MESSAGE
, kxBlob
, ASN1_ANY
),
44 ASN1_OPT(GOST_KX_MESSAGE
, opaqueBlob
, ASN1_ANY
),
45 } ASN1_SEQUENCE_END(GOST_KX_MESSAGE
)
47 IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE
)
49 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
);
52 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
53 * handshake state transitions when a TLSv1.3 server is reading messages from
54 * the client. The message type that the client has sent is provided in |mt|.
55 * The current state is in |s->statem.hand_state|.
57 * Return values are 1 for success (transition allowed) and 0 on error
58 * (transition not allowed)
60 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
62 OSSL_STATEM
*st
= &s
->statem
;
65 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
66 * not negotiated TLSv1.3 yet, so that case is handled by
67 * ossl_statem_server_read_transition()
69 switch (st
->hand_state
) {
73 case TLS_ST_EARLY_DATA
:
74 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
75 if (mt
== SSL3_MT_CLIENT_HELLO
) {
76 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
80 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
81 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
82 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
89 case TLS_ST_SR_END_OF_EARLY_DATA
:
90 case TLS_ST_SW_FINISHED
:
91 if (s
->s3
.tmp
.cert_request
) {
92 if (mt
== SSL3_MT_CERTIFICATE
) {
93 st
->hand_state
= TLS_ST_SR_CERT
;
97 if (mt
== SSL3_MT_FINISHED
) {
98 st
->hand_state
= TLS_ST_SR_FINISHED
;
105 if (s
->session
->peer
== NULL
) {
106 if (mt
== SSL3_MT_FINISHED
) {
107 st
->hand_state
= TLS_ST_SR_FINISHED
;
111 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
112 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
118 case TLS_ST_SR_CERT_VRFY
:
119 if (mt
== SSL3_MT_FINISHED
) {
120 st
->hand_state
= TLS_ST_SR_FINISHED
;
127 * Its never ok to start processing handshake messages in the middle of
128 * early data (i.e. before we've received the end of early data alert)
130 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
133 if (mt
== SSL3_MT_CERTIFICATE
134 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
135 st
->hand_state
= TLS_ST_SR_CERT
;
139 if (mt
== SSL3_MT_KEY_UPDATE
) {
140 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
146 /* No valid transition found */
151 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
152 * handshake state transitions when the server is reading messages from the
153 * client. The message type that the client has sent is provided in |mt|. The
154 * current state is in |s->statem.hand_state|.
156 * Return values are 1 for success (transition allowed) and 0 on error
157 * (transition not allowed)
159 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
161 OSSL_STATEM
*st
= &s
->statem
;
163 if (SSL_IS_TLS13(s
)) {
164 if (!ossl_statem_server13_read_transition(s
, mt
))
169 switch (st
->hand_state
) {
175 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
176 if (mt
== SSL3_MT_CLIENT_HELLO
) {
177 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
182 case TLS_ST_SW_SRVR_DONE
:
184 * If we get a CKE message after a ServerDone then either
185 * 1) We didn't request a Certificate
187 * 2) If we did request one then
188 * a) We allow no Certificate to be returned
190 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
191 * list if we requested a certificate)
193 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
194 if (s
->s3
.tmp
.cert_request
) {
195 if (s
->version
== SSL3_VERSION
) {
196 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
197 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
199 * This isn't an unexpected message as such - we're just
200 * not going to accept it because we require a client
203 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
204 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
205 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
208 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
212 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
215 } else if (s
->s3
.tmp
.cert_request
) {
216 if (mt
== SSL3_MT_CERTIFICATE
) {
217 st
->hand_state
= TLS_ST_SR_CERT
;
224 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
225 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
230 case TLS_ST_SR_KEY_EXCH
:
232 * We should only process a CertificateVerify message if we have
233 * received a Certificate from the client. If so then |s->session->peer|
234 * will be non NULL. In some instances a CertificateVerify message is
235 * not required even if the peer has sent a Certificate (e.g. such as in
236 * the case of static DH). In that case |st->no_cert_verify| should be
239 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
240 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
242 * For the ECDH ciphersuites when the client sends its ECDH
243 * pub key in a certificate, the CertificateVerify message is
244 * not sent. Also for GOST ciphersuites when the client uses
245 * its key from the certificate for key exchange.
247 st
->hand_state
= TLS_ST_SR_CHANGE
;
251 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
252 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
258 case TLS_ST_SR_CERT_VRFY
:
259 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
260 st
->hand_state
= TLS_ST_SR_CHANGE
;
265 case TLS_ST_SR_CHANGE
:
266 #ifndef OPENSSL_NO_NEXTPROTONEG
267 if (s
->s3
.npn_seen
) {
268 if (mt
== SSL3_MT_NEXT_PROTO
) {
269 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
274 if (mt
== SSL3_MT_FINISHED
) {
275 st
->hand_state
= TLS_ST_SR_FINISHED
;
278 #ifndef OPENSSL_NO_NEXTPROTONEG
283 #ifndef OPENSSL_NO_NEXTPROTONEG
284 case TLS_ST_SR_NEXT_PROTO
:
285 if (mt
== SSL3_MT_FINISHED
) {
286 st
->hand_state
= TLS_ST_SR_FINISHED
;
292 case TLS_ST_SW_FINISHED
:
293 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
294 st
->hand_state
= TLS_ST_SR_CHANGE
;
301 /* No valid transition found */
302 if (SSL_IS_DTLS(s
) && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
306 * CCS messages don't have a message sequence number so this is probably
307 * because of an out-of-order CCS. We'll just drop it.
310 s
->rwstate
= SSL_READING
;
311 rbio
= SSL_get_rbio(s
);
312 BIO_clear_retry_flags(rbio
);
313 BIO_set_retry_read(rbio
);
316 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
,
317 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
318 SSL_R_UNEXPECTED_MESSAGE
);
323 * Should we send a ServerKeyExchange message?
325 * Valid return values are:
329 static int send_server_key_exchange(SSL
*s
)
331 unsigned long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
334 * only send a ServerKeyExchange if DH or fortezza but we have a
335 * sign only certificate PSK: may send PSK identity hints For
336 * ECC ciphersuites, we send a serverKeyExchange message only if
337 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
338 * the server certificate contains the server's public key for
341 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
343 * PSK: send ServerKeyExchange if PSK identity hint if
346 #ifndef OPENSSL_NO_PSK
347 /* Only send SKE if we have identity hint for plain PSK */
348 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
349 && s
->cert
->psk_identity_hint
)
350 /* For other PSK always send SKE */
351 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
353 #ifndef OPENSSL_NO_SRP
354 /* SRP: send ServerKeyExchange */
355 || (alg_k
& SSL_kSRP
)
365 * Should we send a CertificateRequest message?
367 * Valid return values are:
371 int send_certificate_request(SSL
*s
)
374 /* don't request cert unless asked for it: */
375 s
->verify_mode
& SSL_VERIFY_PEER
377 * don't request if post-handshake-only unless doing
378 * post-handshake in TLSv1.3:
380 && (!SSL_IS_TLS13(s
) || !(s
->verify_mode
& SSL_VERIFY_POST_HANDSHAKE
)
381 || s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
)
383 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
386 && (s
->certreqs_sent
< 1 ||
387 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
389 * never request cert in anonymous ciphersuites (see
390 * section "Certificate request" in SSL 3 drafts and in
393 && (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
395 * ... except when the application insists on
396 * verification (against the specs, but statem_clnt.c accepts
399 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
400 /* don't request certificate for SRP auth */
401 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
403 * With normal PSK Certificates and Certificate Requests
406 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
414 * ossl_statem_server13_write_transition() works out what handshake state to
415 * move to next when a TLSv1.3 server is writing messages to be sent to the
418 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
420 OSSL_STATEM
*st
= &s
->statem
;
423 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
424 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
427 switch (st
->hand_state
) {
429 /* Shouldn't happen */
430 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
431 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION
,
432 ERR_R_INTERNAL_ERROR
);
433 return WRITE_TRAN_ERROR
;
436 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
437 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
438 return WRITE_TRAN_CONTINUE
;
440 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
441 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
442 return WRITE_TRAN_CONTINUE
;
444 if (s
->ext
.extra_tickets_expected
> 0) {
445 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
446 return WRITE_TRAN_CONTINUE
;
448 /* Try to read from the client instead */
449 return WRITE_TRAN_FINISHED
;
451 case TLS_ST_SR_CLNT_HELLO
:
452 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
453 return WRITE_TRAN_CONTINUE
;
455 case TLS_ST_SW_SRVR_HELLO
:
456 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
457 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
458 st
->hand_state
= TLS_ST_SW_CHANGE
;
459 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
460 st
->hand_state
= TLS_ST_EARLY_DATA
;
462 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
463 return WRITE_TRAN_CONTINUE
;
465 case TLS_ST_SW_CHANGE
:
466 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
467 st
->hand_state
= TLS_ST_EARLY_DATA
;
469 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
470 return WRITE_TRAN_CONTINUE
;
472 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
474 st
->hand_state
= TLS_ST_SW_FINISHED
;
475 else if (send_certificate_request(s
))
476 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
478 st
->hand_state
= TLS_ST_SW_CERT
;
480 return WRITE_TRAN_CONTINUE
;
482 case TLS_ST_SW_CERT_REQ
:
483 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
484 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
485 st
->hand_state
= TLS_ST_OK
;
487 st
->hand_state
= TLS_ST_SW_CERT
;
489 return WRITE_TRAN_CONTINUE
;
492 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
493 return WRITE_TRAN_CONTINUE
;
495 case TLS_ST_SW_CERT_VRFY
:
496 st
->hand_state
= TLS_ST_SW_FINISHED
;
497 return WRITE_TRAN_CONTINUE
;
499 case TLS_ST_SW_FINISHED
:
500 st
->hand_state
= TLS_ST_EARLY_DATA
;
501 return WRITE_TRAN_CONTINUE
;
503 case TLS_ST_EARLY_DATA
:
504 return WRITE_TRAN_FINISHED
;
506 case TLS_ST_SR_FINISHED
:
508 * Technically we have finished the handshake at this point, but we're
509 * going to remain "in_init" for now and write out any session tickets
512 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
513 s
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
;
514 } else if (!s
->ext
.ticket_expected
) {
516 * If we're not going to renew the ticket then we just finish the
517 * handshake at this point.
519 st
->hand_state
= TLS_ST_OK
;
520 return WRITE_TRAN_CONTINUE
;
522 if (s
->num_tickets
> s
->sent_tickets
)
523 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
525 st
->hand_state
= TLS_ST_OK
;
526 return WRITE_TRAN_CONTINUE
;
528 case TLS_ST_SR_KEY_UPDATE
:
529 case TLS_ST_SW_KEY_UPDATE
:
530 st
->hand_state
= TLS_ST_OK
;
531 return WRITE_TRAN_CONTINUE
;
533 case TLS_ST_SW_SESSION_TICKET
:
534 /* In a resumption we only ever send a maximum of one new ticket.
535 * Following an initial handshake we send the number of tickets we have
536 * been configured for.
538 if (!SSL_IS_FIRST_HANDSHAKE(s
) && s
->ext
.extra_tickets_expected
> 0) {
539 return WRITE_TRAN_CONTINUE
;
540 } else if (s
->hit
|| s
->num_tickets
<= s
->sent_tickets
) {
541 /* We've written enough tickets out. */
542 st
->hand_state
= TLS_ST_OK
;
544 return WRITE_TRAN_CONTINUE
;
549 * ossl_statem_server_write_transition() works out what handshake state to move
550 * to next when the server is writing messages to be sent to the client.
552 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
554 OSSL_STATEM
*st
= &s
->statem
;
557 * Note that before the ClientHello we don't know what version we are going
558 * to negotiate yet, so we don't take this branch until later
562 return ossl_statem_server13_write_transition(s
);
564 switch (st
->hand_state
) {
566 /* Shouldn't happen */
567 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
568 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION
,
569 ERR_R_INTERNAL_ERROR
);
570 return WRITE_TRAN_ERROR
;
573 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
574 /* We must be trying to renegotiate */
575 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
576 st
->request_state
= TLS_ST_BEFORE
;
577 return WRITE_TRAN_CONTINUE
;
579 /* Must be an incoming ClientHello */
580 if (!tls_setup_handshake(s
)) {
581 /* SSLfatal() already called */
582 return WRITE_TRAN_ERROR
;
587 /* Just go straight to trying to read from the client */
588 return WRITE_TRAN_FINISHED
;
590 case TLS_ST_SW_HELLO_REQ
:
591 st
->hand_state
= TLS_ST_OK
;
592 return WRITE_TRAN_CONTINUE
;
594 case TLS_ST_SR_CLNT_HELLO
:
595 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
596 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
)) {
597 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
598 } else if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
599 /* We must have rejected the renegotiation */
600 st
->hand_state
= TLS_ST_OK
;
601 return WRITE_TRAN_CONTINUE
;
603 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
605 return WRITE_TRAN_CONTINUE
;
607 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
608 return WRITE_TRAN_FINISHED
;
610 case TLS_ST_SW_SRVR_HELLO
:
612 if (s
->ext
.ticket_expected
)
613 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
615 st
->hand_state
= TLS_ST_SW_CHANGE
;
617 /* Check if it is anon DH or anon ECDH, */
618 /* normal PSK or SRP */
619 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
&
620 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
621 st
->hand_state
= TLS_ST_SW_CERT
;
622 } else if (send_server_key_exchange(s
)) {
623 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
624 } else if (send_certificate_request(s
)) {
625 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
627 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
630 return WRITE_TRAN_CONTINUE
;
633 if (s
->ext
.status_expected
) {
634 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
635 return WRITE_TRAN_CONTINUE
;
639 case TLS_ST_SW_CERT_STATUS
:
640 if (send_server_key_exchange(s
)) {
641 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
642 return WRITE_TRAN_CONTINUE
;
646 case TLS_ST_SW_KEY_EXCH
:
647 if (send_certificate_request(s
)) {
648 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
649 return WRITE_TRAN_CONTINUE
;
653 case TLS_ST_SW_CERT_REQ
:
654 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
655 return WRITE_TRAN_CONTINUE
;
657 case TLS_ST_SW_SRVR_DONE
:
658 return WRITE_TRAN_FINISHED
;
660 case TLS_ST_SR_FINISHED
:
662 st
->hand_state
= TLS_ST_OK
;
663 return WRITE_TRAN_CONTINUE
;
664 } else if (s
->ext
.ticket_expected
) {
665 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
667 st
->hand_state
= TLS_ST_SW_CHANGE
;
669 return WRITE_TRAN_CONTINUE
;
671 case TLS_ST_SW_SESSION_TICKET
:
672 st
->hand_state
= TLS_ST_SW_CHANGE
;
673 return WRITE_TRAN_CONTINUE
;
675 case TLS_ST_SW_CHANGE
:
676 st
->hand_state
= TLS_ST_SW_FINISHED
;
677 return WRITE_TRAN_CONTINUE
;
679 case TLS_ST_SW_FINISHED
:
681 return WRITE_TRAN_FINISHED
;
683 st
->hand_state
= TLS_ST_OK
;
684 return WRITE_TRAN_CONTINUE
;
689 * Perform any pre work that needs to be done prior to sending a message from
690 * the server to the client.
692 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
694 OSSL_STATEM
*st
= &s
->statem
;
696 switch (st
->hand_state
) {
698 /* No pre work to be done */
701 case TLS_ST_SW_HELLO_REQ
:
704 dtls1_clear_sent_buffer(s
);
707 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
709 if (SSL_IS_DTLS(s
)) {
710 dtls1_clear_sent_buffer(s
);
711 /* We don't buffer this message so don't use the timer */
716 case TLS_ST_SW_SRVR_HELLO
:
717 if (SSL_IS_DTLS(s
)) {
719 * Messages we write from now on should be buffered and
720 * retransmitted if necessary, so we need to use the timer now
726 case TLS_ST_SW_SRVR_DONE
:
727 #ifndef OPENSSL_NO_SCTP
728 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
729 /* Calls SSLfatal() as required */
730 return dtls_wait_for_dry(s
);
733 return WORK_FINISHED_CONTINUE
;
735 case TLS_ST_SW_SESSION_TICKET
:
736 if (SSL_IS_TLS13(s
) && s
->sent_tickets
== 0
737 && s
->ext
.extra_tickets_expected
== 0) {
739 * Actually this is the end of the handshake, but we're going
740 * straight into writing the session ticket out. So we finish off
741 * the handshake, but keep the various buffers active.
743 * Calls SSLfatal as required.
745 return tls_finish_handshake(s
, wst
, 0, 0);
747 if (SSL_IS_DTLS(s
)) {
749 * We're into the last flight. We don't retransmit the last flight
750 * unless we need to, so we don't use the timer
756 case TLS_ST_SW_CHANGE
:
759 /* Writes to s->session are only safe for initial handshakes */
760 if (s
->session
->cipher
== NULL
) {
761 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
762 } else if (s
->session
->cipher
!= s
->s3
.tmp
.new_cipher
) {
763 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
764 SSL_F_OSSL_STATEM_SERVER_PRE_WORK
,
765 ERR_R_INTERNAL_ERROR
);
768 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
769 /* SSLfatal() already called */
772 if (SSL_IS_DTLS(s
)) {
774 * We're into the last flight. We don't retransmit the last flight
775 * unless we need to, so we don't use the timer. This might have
776 * already been set to 0 if we sent a NewSessionTicket message,
777 * but we'll set it again here in case we didn't.
781 return WORK_FINISHED_CONTINUE
;
783 case TLS_ST_EARLY_DATA
:
784 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
785 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0)
786 return WORK_FINISHED_CONTINUE
;
790 /* Calls SSLfatal() as required */
791 return tls_finish_handshake(s
, wst
, 1, 1);
794 return WORK_FINISHED_CONTINUE
;
797 static ossl_inline
int conn_is_closed(void)
799 switch (get_last_sys_error()) {
804 #if defined(ECONNRESET)
808 #if defined(WSAECONNRESET)
818 * Perform any work that needs to be done after sending a message from the
819 * server to the client.
821 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
823 OSSL_STATEM
*st
= &s
->statem
;
827 switch (st
->hand_state
) {
829 /* No post work to be done */
832 case TLS_ST_SW_HELLO_REQ
:
833 if (statem_flush(s
) != 1)
835 if (!ssl3_init_finished_mac(s
)) {
836 /* SSLfatal() already called */
841 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
842 if (statem_flush(s
) != 1)
844 /* HelloVerifyRequest resets Finished MAC */
845 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
846 /* SSLfatal() already called */
850 * The next message should be another ClientHello which we need to
851 * treat like it was the first packet
856 case TLS_ST_SW_SRVR_HELLO
:
857 if (SSL_IS_TLS13(s
) && s
->hello_retry_request
== SSL_HRR_PENDING
) {
858 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
859 && statem_flush(s
) != 1)
863 #ifndef OPENSSL_NO_SCTP
864 if (SSL_IS_DTLS(s
) && s
->hit
) {
865 unsigned char sctpauthkey
[64];
866 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
870 * Add new shared key for SCTP-Auth, will be ignored if no
873 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
874 sizeof(DTLS1_SCTP_AUTH_LABEL
));
876 /* Don't include the terminating zero. */
877 labellen
= sizeof(labelbuffer
) - 1;
878 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
881 if (SSL_export_keying_material(s
, sctpauthkey
,
882 sizeof(sctpauthkey
), labelbuffer
,
885 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
886 SSL_F_OSSL_STATEM_SERVER_POST_WORK
,
887 ERR_R_INTERNAL_ERROR
);
891 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
892 sizeof(sctpauthkey
), sctpauthkey
);
896 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
897 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
901 case TLS_ST_SW_CHANGE
:
902 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
903 if (!statem_flush(s
))
908 if (SSL_IS_TLS13(s
)) {
909 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
910 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
911 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
912 /* SSLfatal() already called */
916 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
917 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
918 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
919 /* SSLfatal() already called */
923 * We don't yet know whether the next record we are going to receive
924 * is an unencrypted alert, an encrypted alert, or an encrypted
925 * handshake message. We temporarily tolerate unencrypted alerts.
927 s
->statem
.enc_read_state
= ENC_READ_STATE_ALLOW_PLAIN_ALERTS
;
931 #ifndef OPENSSL_NO_SCTP
932 if (SSL_IS_DTLS(s
) && !s
->hit
) {
934 * Change to new shared key of SCTP-Auth, will be ignored if
937 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
941 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
942 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
944 /* SSLfatal() already called */
949 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
952 case TLS_ST_SW_SRVR_DONE
:
953 if (statem_flush(s
) != 1)
957 case TLS_ST_SW_FINISHED
:
958 if (statem_flush(s
) != 1)
960 #ifndef OPENSSL_NO_SCTP
961 if (SSL_IS_DTLS(s
) && s
->hit
) {
963 * Change to new shared key of SCTP-Auth, will be ignored if
966 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
970 if (SSL_IS_TLS13(s
)) {
971 /* TLS 1.3 gets the secret size from the handshake md */
973 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
974 s
->master_secret
, s
->handshake_secret
, 0,
976 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
977 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
978 /* SSLfatal() already called */
983 case TLS_ST_SW_CERT_REQ
:
984 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
985 if (statem_flush(s
) != 1)
990 case TLS_ST_SW_KEY_UPDATE
:
991 if (statem_flush(s
) != 1)
993 if (!tls13_update_key(s
, 1)) {
994 /* SSLfatal() already called */
999 case TLS_ST_SW_SESSION_TICKET
:
1001 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1) {
1002 if (SSL_get_error(s
, 0) == SSL_ERROR_SYSCALL
1003 && conn_is_closed()) {
1005 * We ignore connection closed errors in TLSv1.3 when sending a
1006 * NewSessionTicket and behave as if we were successful. This is
1007 * so that we are still able to read data sent to us by a client
1008 * that closes soon after the end of the handshake without
1009 * waiting to read our post-handshake NewSessionTickets.
1011 s
->rwstate
= SSL_NOTHING
;
1020 return WORK_FINISHED_CONTINUE
;
1024 * Get the message construction function and message type for sending from the
1027 * Valid return values are:
1031 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
1032 confunc_f
*confunc
, int *mt
)
1034 OSSL_STATEM
*st
= &s
->statem
;
1036 switch (st
->hand_state
) {
1038 /* Shouldn't happen */
1039 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1040 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE
,
1041 SSL_R_BAD_HANDSHAKE_STATE
);
1044 case TLS_ST_SW_CHANGE
:
1046 *confunc
= dtls_construct_change_cipher_spec
;
1048 *confunc
= tls_construct_change_cipher_spec
;
1049 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1052 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1053 *confunc
= dtls_construct_hello_verify_request
;
1054 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1057 case TLS_ST_SW_HELLO_REQ
:
1058 /* No construction function needed */
1060 *mt
= SSL3_MT_HELLO_REQUEST
;
1063 case TLS_ST_SW_SRVR_HELLO
:
1064 *confunc
= tls_construct_server_hello
;
1065 *mt
= SSL3_MT_SERVER_HELLO
;
1068 case TLS_ST_SW_CERT
:
1069 *confunc
= tls_construct_server_certificate
;
1070 *mt
= SSL3_MT_CERTIFICATE
;
1073 case TLS_ST_SW_CERT_VRFY
:
1074 *confunc
= tls_construct_cert_verify
;
1075 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1079 case TLS_ST_SW_KEY_EXCH
:
1080 *confunc
= tls_construct_server_key_exchange
;
1081 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1084 case TLS_ST_SW_CERT_REQ
:
1085 *confunc
= tls_construct_certificate_request
;
1086 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1089 case TLS_ST_SW_SRVR_DONE
:
1090 *confunc
= tls_construct_server_done
;
1091 *mt
= SSL3_MT_SERVER_DONE
;
1094 case TLS_ST_SW_SESSION_TICKET
:
1095 *confunc
= tls_construct_new_session_ticket
;
1096 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1099 case TLS_ST_SW_CERT_STATUS
:
1100 *confunc
= tls_construct_cert_status
;
1101 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1104 case TLS_ST_SW_FINISHED
:
1105 *confunc
= tls_construct_finished
;
1106 *mt
= SSL3_MT_FINISHED
;
1109 case TLS_ST_EARLY_DATA
:
1111 *mt
= SSL3_MT_DUMMY
;
1114 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1115 *confunc
= tls_construct_encrypted_extensions
;
1116 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1119 case TLS_ST_SW_KEY_UPDATE
:
1120 *confunc
= tls_construct_key_update
;
1121 *mt
= SSL3_MT_KEY_UPDATE
;
1129 * Maximum size (excluding the Handshake header) of a ClientHello message,
1130 * calculated as follows:
1132 * 2 + # client_version
1133 * 32 + # only valid length for random
1134 * 1 + # length of session_id
1135 * 32 + # maximum size for session_id
1136 * 2 + # length of cipher suites
1137 * 2^16-2 + # maximum length of cipher suites array
1138 * 1 + # length of compression_methods
1139 * 2^8-1 + # maximum length of compression methods
1140 * 2 + # length of extensions
1141 * 2^16-1 # maximum length of extensions
1143 #define CLIENT_HELLO_MAX_LENGTH 131396
1145 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1146 #define NEXT_PROTO_MAX_LENGTH 514
1149 * Returns the maximum allowed length for the current message that we are
1150 * reading. Excludes the message header.
1152 size_t ossl_statem_server_max_message_size(SSL
*s
)
1154 OSSL_STATEM
*st
= &s
->statem
;
1156 switch (st
->hand_state
) {
1158 /* Shouldn't happen */
1161 case TLS_ST_SR_CLNT_HELLO
:
1162 return CLIENT_HELLO_MAX_LENGTH
;
1164 case TLS_ST_SR_END_OF_EARLY_DATA
:
1165 return END_OF_EARLY_DATA_MAX_LENGTH
;
1167 case TLS_ST_SR_CERT
:
1168 return s
->max_cert_list
;
1170 case TLS_ST_SR_KEY_EXCH
:
1171 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1173 case TLS_ST_SR_CERT_VRFY
:
1174 return SSL3_RT_MAX_PLAIN_LENGTH
;
1176 #ifndef OPENSSL_NO_NEXTPROTONEG
1177 case TLS_ST_SR_NEXT_PROTO
:
1178 return NEXT_PROTO_MAX_LENGTH
;
1181 case TLS_ST_SR_CHANGE
:
1182 return CCS_MAX_LENGTH
;
1184 case TLS_ST_SR_FINISHED
:
1185 return FINISHED_MAX_LENGTH
;
1187 case TLS_ST_SR_KEY_UPDATE
:
1188 return KEY_UPDATE_MAX_LENGTH
;
1193 * Process a message that the server has received from the client.
1195 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1197 OSSL_STATEM
*st
= &s
->statem
;
1199 switch (st
->hand_state
) {
1201 /* Shouldn't happen */
1202 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1203 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE
,
1204 ERR_R_INTERNAL_ERROR
);
1205 return MSG_PROCESS_ERROR
;
1207 case TLS_ST_SR_CLNT_HELLO
:
1208 return tls_process_client_hello(s
, pkt
);
1210 case TLS_ST_SR_END_OF_EARLY_DATA
:
1211 return tls_process_end_of_early_data(s
, pkt
);
1213 case TLS_ST_SR_CERT
:
1214 return tls_process_client_certificate(s
, pkt
);
1216 case TLS_ST_SR_KEY_EXCH
:
1217 return tls_process_client_key_exchange(s
, pkt
);
1219 case TLS_ST_SR_CERT_VRFY
:
1220 return tls_process_cert_verify(s
, pkt
);
1222 #ifndef OPENSSL_NO_NEXTPROTONEG
1223 case TLS_ST_SR_NEXT_PROTO
:
1224 return tls_process_next_proto(s
, pkt
);
1227 case TLS_ST_SR_CHANGE
:
1228 return tls_process_change_cipher_spec(s
, pkt
);
1230 case TLS_ST_SR_FINISHED
:
1231 return tls_process_finished(s
, pkt
);
1233 case TLS_ST_SR_KEY_UPDATE
:
1234 return tls_process_key_update(s
, pkt
);
1240 * Perform any further processing required following the receipt of a message
1243 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1245 OSSL_STATEM
*st
= &s
->statem
;
1247 switch (st
->hand_state
) {
1249 /* Shouldn't happen */
1250 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1251 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE
,
1252 ERR_R_INTERNAL_ERROR
);
1255 case TLS_ST_SR_CLNT_HELLO
:
1256 return tls_post_process_client_hello(s
, wst
);
1258 case TLS_ST_SR_KEY_EXCH
:
1259 return tls_post_process_client_key_exchange(s
, wst
);
1263 #ifndef OPENSSL_NO_SRP
1264 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1265 static int ssl_check_srp_ext_ClientHello(SSL
*s
)
1268 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1270 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1271 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1272 if (s
->srp_ctx
.login
== NULL
) {
1274 * RFC 5054 says SHOULD reject, we do so if There is no srp
1277 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1278 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1279 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1282 ret
= SSL_srp_server_param_with_username(s
, &al
);
1285 if (ret
== SSL3_AL_FATAL
) {
1286 SSLfatal(s
, al
, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1287 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1288 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1289 : SSL_R_CLIENTHELLO_TLSEXT
);
1298 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1301 /* Always use DTLS 1.0 version: see RFC 6347 */
1302 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1303 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1309 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1311 unsigned int cookie_leni
;
1312 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1313 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1314 &cookie_leni
) == 0 ||
1315 cookie_leni
> DTLS1_COOKIE_LENGTH
) {
1316 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1317 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1320 s
->d1
->cookie_len
= cookie_leni
;
1322 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1323 s
->d1
->cookie_len
)) {
1324 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1325 ERR_R_INTERNAL_ERROR
);
1332 #ifndef OPENSSL_NO_EC
1334 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1335 * SecureTransport using the TLS extension block in |hello|.
1336 * Safari, since 10.6, sends exactly these extensions, in this order:
1340 * signature_algorithms (for TLSv1.2 only)
1342 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1343 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1344 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1345 * 10.8..10.8.3 (which don't work).
1347 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1349 static const unsigned char kSafariExtensionsBlock
[] = {
1350 0x00, 0x0a, /* elliptic_curves extension */
1351 0x00, 0x08, /* 8 bytes */
1352 0x00, 0x06, /* 6 bytes of curve ids */
1353 0x00, 0x17, /* P-256 */
1354 0x00, 0x18, /* P-384 */
1355 0x00, 0x19, /* P-521 */
1357 0x00, 0x0b, /* ec_point_formats */
1358 0x00, 0x02, /* 2 bytes */
1359 0x01, /* 1 point format */
1360 0x00, /* uncompressed */
1361 /* The following is only present in TLS 1.2 */
1362 0x00, 0x0d, /* signature_algorithms */
1363 0x00, 0x0c, /* 12 bytes */
1364 0x00, 0x0a, /* 10 bytes */
1365 0x05, 0x01, /* SHA-384/RSA */
1366 0x04, 0x01, /* SHA-256/RSA */
1367 0x02, 0x01, /* SHA-1/RSA */
1368 0x04, 0x03, /* SHA-256/ECDSA */
1369 0x02, 0x03, /* SHA-1/ECDSA */
1371 /* Length of the common prefix (first two extensions). */
1372 static const size_t kSafariCommonExtensionsLength
= 18;
1377 tmppkt
= hello
->extensions
;
1379 if (!PACKET_forward(&tmppkt
, 2)
1380 || !PACKET_get_net_2(&tmppkt
, &type
)
1381 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1385 if (type
!= TLSEXT_TYPE_server_name
)
1388 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1389 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1391 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1394 #endif /* !OPENSSL_NO_EC */
1396 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1398 /* |cookie| will only be initialized for DTLS. */
1399 PACKET session_id
, compression
, extensions
, cookie
;
1400 static const unsigned char null_compression
= 0;
1401 CLIENTHELLO_MSG
*clienthello
= NULL
;
1403 /* Check if this is actually an unexpected renegotiation ClientHello */
1404 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1405 if (!ossl_assert(!SSL_IS_TLS13(s
))) {
1406 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1407 ERR_R_INTERNAL_ERROR
);
1410 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
) != 0
1411 || (!s
->s3
.send_connection_binding
1413 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1414 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1415 return MSG_PROCESS_FINISHED_READING
;
1421 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1422 if (clienthello
== NULL
) {
1423 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1424 ERR_R_INTERNAL_ERROR
);
1429 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1431 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1432 PACKET_null_init(&cookie
);
1434 if (clienthello
->isv2
) {
1437 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1438 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1439 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1440 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1445 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1446 * header is sent directly on the wire, not wrapped as a TLS
1447 * record. Our record layer just processes the message length and passes
1448 * the rest right through. Its format is:
1450 * 0-1 msg_length - decoded by the record layer
1451 * 2 msg_type - s->init_msg points here
1453 * 5-6 cipher_spec_length
1454 * 7-8 session_id_length
1455 * 9-10 challenge_length
1459 if (!PACKET_get_1(pkt
, &mt
)
1460 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1462 * Should never happen. We should have tested this in the record
1463 * layer in order to have determined that this is a SSLv2 record
1464 * in the first place
1466 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1467 ERR_R_INTERNAL_ERROR
);
1472 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1473 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1474 SSL_R_LENGTH_TOO_SHORT
);
1478 /* Parse the message and load client random. */
1479 if (clienthello
->isv2
) {
1481 * Handle an SSLv2 backwards compatible ClientHello
1482 * Note, this is only for SSLv3+ using the backward compatible format.
1483 * Real SSLv2 is not supported, and is rejected below.
1485 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1488 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1489 || !PACKET_get_net_2(pkt
, &session_id_len
)
1490 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1491 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1492 SSL_R_RECORD_LENGTH_MISMATCH
);
1496 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1497 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1498 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1502 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1504 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1505 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1506 /* No extensions. */
1507 || PACKET_remaining(pkt
) != 0) {
1508 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1509 SSL_R_RECORD_LENGTH_MISMATCH
);
1512 clienthello
->session_id_len
= session_id_len
;
1514 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1515 * here rather than sizeof(clienthello->random) because that is the limit
1516 * for SSLv3 and it is fixed. It won't change even if
1517 * sizeof(clienthello->random) does.
1519 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1520 ? SSL3_RANDOM_SIZE
: challenge_len
;
1521 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1522 if (!PACKET_copy_bytes(&challenge
,
1523 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1524 challenge_len
, challenge_len
)
1525 /* Advertise only null compression. */
1526 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1527 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1528 ERR_R_INTERNAL_ERROR
);
1532 PACKET_null_init(&clienthello
->extensions
);
1534 /* Regular ClientHello. */
1535 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1536 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1537 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1538 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1539 &clienthello
->session_id_len
)) {
1540 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1541 SSL_R_LENGTH_MISMATCH
);
1545 if (SSL_IS_DTLS(s
)) {
1546 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1547 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1548 SSL_R_LENGTH_MISMATCH
);
1551 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1552 DTLS1_COOKIE_LENGTH
,
1553 &clienthello
->dtls_cookie_len
)) {
1554 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1555 SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1559 * If we require cookies and this ClientHello doesn't contain one,
1560 * just return since we do not want to allocate any memory yet.
1561 * So check cookie length...
1563 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1564 if (clienthello
->dtls_cookie_len
== 0) {
1565 OPENSSL_free(clienthello
);
1566 return MSG_PROCESS_FINISHED_READING
;
1571 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1572 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1573 SSL_R_LENGTH_MISMATCH
);
1577 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1578 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1579 SSL_R_LENGTH_MISMATCH
);
1583 /* Could be empty. */
1584 if (PACKET_remaining(pkt
) == 0) {
1585 PACKET_null_init(&clienthello
->extensions
);
1587 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1588 || PACKET_remaining(pkt
) != 0) {
1589 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1590 SSL_R_LENGTH_MISMATCH
);
1596 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1597 MAX_COMPRESSIONS_SIZE
,
1598 &clienthello
->compressions_len
)) {
1599 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1600 ERR_R_INTERNAL_ERROR
);
1604 /* Preserve the raw extensions PACKET for later use */
1605 extensions
= clienthello
->extensions
;
1606 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1607 &clienthello
->pre_proc_exts
,
1608 &clienthello
->pre_proc_exts_len
, 1)) {
1609 /* SSLfatal already been called */
1612 s
->clienthello
= clienthello
;
1614 return MSG_PROCESS_CONTINUE_PROCESSING
;
1617 if (clienthello
!= NULL
)
1618 OPENSSL_free(clienthello
->pre_proc_exts
);
1619 OPENSSL_free(clienthello
);
1621 return MSG_PROCESS_ERROR
;
1624 static int tls_early_post_process_client_hello(SSL
*s
)
1627 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1631 #ifndef OPENSSL_NO_COMP
1632 SSL_COMP
*comp
= NULL
;
1634 const SSL_CIPHER
*c
;
1635 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1636 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1637 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1638 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1640 /* Finished parsing the ClientHello, now we can start processing it */
1641 /* Give the ClientHello callback a crack at things */
1642 if (s
->ctx
->client_hello_cb
!= NULL
) {
1643 /* A failure in the ClientHello callback terminates the connection. */
1644 switch (s
->ctx
->client_hello_cb(s
, &al
, s
->ctx
->client_hello_cb_arg
)) {
1645 case SSL_CLIENT_HELLO_SUCCESS
:
1647 case SSL_CLIENT_HELLO_RETRY
:
1648 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1650 case SSL_CLIENT_HELLO_ERROR
:
1653 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1654 SSL_R_CALLBACK_FAILED
);
1659 /* Set up the client_random */
1660 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1662 /* Choose the version */
1664 if (clienthello
->isv2
) {
1665 if (clienthello
->legacy_version
== SSL2_VERSION
1666 || (clienthello
->legacy_version
& 0xff00)
1667 != (SSL3_VERSION_MAJOR
<< 8)) {
1669 * This is real SSLv2 or something completely unknown. We don't
1672 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1673 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1674 SSL_R_UNKNOWN_PROTOCOL
);
1678 s
->client_version
= clienthello
->legacy_version
;
1681 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1682 * versions are potentially compatible. Version negotiation comes later.
1684 if (!SSL_IS_DTLS(s
)) {
1685 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1686 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1687 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1688 protverr
= SSL_R_VERSION_TOO_LOW
;
1694 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1695 /* like ssl3_get_record, send alert using remote version number */
1696 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1698 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1699 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1703 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1704 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1705 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1706 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1707 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1711 if (SSL_IS_DTLS(s
)) {
1712 /* Empty cookie was already handled above by returning early. */
1713 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1714 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1715 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1716 clienthello
->dtls_cookie_len
) == 0) {
1717 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1718 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1719 SSL_R_COOKIE_MISMATCH
);
1721 /* else cookie verification succeeded */
1723 /* default verification */
1724 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1725 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1726 s
->d1
->cookie_len
) != 0) {
1727 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1728 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1729 SSL_R_COOKIE_MISMATCH
);
1732 s
->d1
->cookie_verified
= 1;
1734 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1735 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1736 if (protverr
!= 0) {
1737 s
->version
= s
->client_version
;
1738 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1739 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1747 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1748 clienthello
->isv2
) ||
1749 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1750 clienthello
->isv2
, 1)) {
1751 /* SSLfatal() already called */
1755 s
->s3
.send_connection_binding
= 0;
1756 /* Check what signalling cipher-suite values were received. */
1757 if (scsvs
!= NULL
) {
1758 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1759 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1760 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1761 if (s
->renegotiate
) {
1762 /* SCSV is fatal if renegotiating */
1763 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1764 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1765 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1768 s
->s3
.send_connection_binding
= 1;
1769 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1770 !ssl_check_version_downgrade(s
)) {
1772 * This SCSV indicates that the client previously tried
1773 * a higher version. We should fail if the current version
1774 * is an unexpected downgrade, as that indicates that the first
1775 * connection may have been tampered with in order to trigger
1776 * an insecure downgrade.
1778 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1779 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1780 SSL_R_INAPPROPRIATE_FALLBACK
);
1786 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1787 if (SSL_IS_TLS13(s
)) {
1788 const SSL_CIPHER
*cipher
=
1789 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(s
));
1791 if (cipher
== NULL
) {
1792 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1793 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1794 SSL_R_NO_SHARED_CIPHER
);
1797 if (s
->hello_retry_request
== SSL_HRR_PENDING
1798 && (s
->s3
.tmp
.new_cipher
== NULL
1799 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1801 * A previous HRR picked a different ciphersuite to the one we
1802 * just selected. Something must have changed.
1804 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1805 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1809 s
->s3
.tmp
.new_cipher
= cipher
;
1812 /* We need to do this before getting the session */
1813 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1814 SSL_EXT_CLIENT_HELLO
,
1815 clienthello
->pre_proc_exts
, NULL
, 0)) {
1816 /* SSLfatal() already called */
1821 * We don't allow resumption in a backwards compatible ClientHello.
1822 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1824 * Versions before 0.9.7 always allow clients to resume sessions in
1825 * renegotiation. 0.9.7 and later allow this by default, but optionally
1826 * ignore resumption requests with flag
1827 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1828 * than a change to default behavior so that applications relying on
1829 * this for security won't even compile against older library versions).
1830 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1831 * request renegotiation but not a new session (s->new_session remains
1832 * unset): for servers, this essentially just means that the
1833 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1836 if (clienthello
->isv2
||
1838 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1839 if (!ssl_get_new_session(s
, 1)) {
1840 /* SSLfatal() already called */
1844 i
= ssl_get_prev_session(s
, clienthello
);
1846 /* previous session */
1848 } else if (i
== -1) {
1849 /* SSLfatal() already called */
1853 if (!ssl_get_new_session(s
, 1)) {
1854 /* SSLfatal() already called */
1860 if (SSL_IS_TLS13(s
)) {
1861 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1862 s
->clienthello
->session_id_len
);
1863 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1867 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1868 * ciphersuite compatibility with the session as part of resumption.
1870 if (!SSL_IS_TLS13(s
) && s
->hit
) {
1872 id
= s
->session
->cipher
->id
;
1874 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1875 BIO_printf(trc_out
, "client sent %d ciphers\n",
1876 sk_SSL_CIPHER_num(ciphers
));
1878 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1879 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1880 if (trc_out
!= NULL
)
1881 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1882 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1890 * we need to have the cipher in the cipher list if we are asked
1893 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1894 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1895 SSL_R_REQUIRED_CIPHER_MISSING
);
1896 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1899 OSSL_TRACE_END(TLS_CIPHER
);
1902 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1903 if (clienthello
->compressions
[loop
] == 0)
1907 if (loop
>= clienthello
->compressions_len
) {
1909 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1910 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1911 SSL_R_NO_COMPRESSION_SPECIFIED
);
1915 #ifndef OPENSSL_NO_EC
1916 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1917 ssl_check_for_safari(s
, clienthello
);
1918 #endif /* !OPENSSL_NO_EC */
1920 /* TLS extensions */
1921 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1922 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1923 /* SSLfatal() already called */
1928 * Check if we want to use external pre-shared secret for this handshake
1929 * for not reused session only. We need to generate server_random before
1930 * calling tls_session_secret_cb in order to allow SessionTicket
1931 * processing to use it in key derivation.
1935 pos
= s
->s3
.server_random
;
1936 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1937 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1938 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1939 ERR_R_INTERNAL_ERROR
);
1945 && s
->version
>= TLS1_VERSION
1948 && s
->ext
.session_secret_cb
) {
1949 const SSL_CIPHER
*pref_cipher
= NULL
;
1951 * s->session->master_key_length is a size_t, but this is an int for
1952 * backwards compat reasons
1954 int master_key_length
;
1956 master_key_length
= sizeof(s
->session
->master_key
);
1957 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1958 &master_key_length
, ciphers
,
1960 s
->ext
.session_secret_cb_arg
)
1961 && master_key_length
> 0) {
1962 s
->session
->master_key_length
= master_key_length
;
1964 s
->peer_ciphers
= ciphers
;
1965 s
->session
->verify_result
= X509_V_OK
;
1969 /* check if some cipher was preferred by call back */
1970 if (pref_cipher
== NULL
)
1971 pref_cipher
= ssl3_choose_cipher(s
, s
->peer_ciphers
,
1972 SSL_get_ciphers(s
));
1973 if (pref_cipher
== NULL
) {
1974 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1975 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1976 SSL_R_NO_SHARED_CIPHER
);
1980 s
->session
->cipher
= pref_cipher
;
1981 sk_SSL_CIPHER_free(s
->cipher_list
);
1982 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1983 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1984 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->peer_ciphers
);
1989 * Worst case, we will use the NULL compression, but if we have other
1990 * options, we will now look for them. We have complen-1 compression
1991 * algorithms from the client, starting at q.
1993 s
->s3
.tmp
.new_compression
= NULL
;
1994 if (SSL_IS_TLS13(s
)) {
1996 * We already checked above that the NULL compression method appears in
1997 * the list. Now we check there aren't any others (which is illegal in
1998 * a TLSv1.3 ClientHello.
2000 if (clienthello
->compressions_len
!= 1) {
2001 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2002 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2003 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
2007 #ifndef OPENSSL_NO_COMP
2008 /* This only happens if we have a cache hit */
2009 else if (s
->session
->compress_meth
!= 0) {
2010 int m
, comp_id
= s
->session
->compress_meth
;
2012 /* Perform sanity checks on resumed compression algorithm */
2013 /* Can't disable compression */
2014 if (!ssl_allow_compression(s
)) {
2015 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2016 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2017 SSL_R_INCONSISTENT_COMPRESSION
);
2020 /* Look for resumed compression method */
2021 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
2022 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2023 if (comp_id
== comp
->id
) {
2024 s
->s3
.tmp
.new_compression
= comp
;
2028 if (s
->s3
.tmp
.new_compression
== NULL
) {
2029 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2030 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2031 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
2034 /* Look for resumed method in compression list */
2035 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
2036 if (clienthello
->compressions
[k
] == comp_id
)
2039 if (k
>= clienthello
->compressions_len
) {
2040 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2041 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2042 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
2045 } else if (s
->hit
) {
2047 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
2048 /* See if we have a match */
2049 int m
, nn
, v
, done
= 0;
2052 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
2053 for (m
= 0; m
< nn
; m
++) {
2054 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2056 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2057 if (v
== clienthello
->compressions
[o
]) {
2066 s
->s3
.tmp
.new_compression
= comp
;
2072 * If compression is disabled we'd better not try to resume a session
2073 * using compression.
2075 if (s
->session
->compress_meth
!= 0) {
2076 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2077 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2078 SSL_R_INCONSISTENT_COMPRESSION
);
2084 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2087 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2088 sk_SSL_CIPHER_free(s
->peer_ciphers
);
2089 s
->peer_ciphers
= ciphers
;
2090 if (ciphers
== NULL
) {
2091 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2092 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2093 ERR_R_INTERNAL_ERROR
);
2100 #ifdef OPENSSL_NO_COMP
2101 s
->session
->compress_meth
= 0;
2103 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2105 if (!tls1_set_server_sigalgs(s
)) {
2106 /* SSLfatal() already called */
2111 sk_SSL_CIPHER_free(ciphers
);
2112 sk_SSL_CIPHER_free(scsvs
);
2113 OPENSSL_free(clienthello
->pre_proc_exts
);
2114 OPENSSL_free(s
->clienthello
);
2115 s
->clienthello
= NULL
;
2118 sk_SSL_CIPHER_free(ciphers
);
2119 sk_SSL_CIPHER_free(scsvs
);
2120 OPENSSL_free(clienthello
->pre_proc_exts
);
2121 OPENSSL_free(s
->clienthello
);
2122 s
->clienthello
= NULL
;
2128 * Call the status request callback if needed. Upon success, returns 1.
2129 * Upon failure, returns 0.
2131 static int tls_handle_status_request(SSL
*s
)
2133 s
->ext
.status_expected
= 0;
2136 * If status request then ask callback what to do. Note: this must be
2137 * called after servername callbacks in case the certificate has changed,
2138 * and must be called after the cipher has been chosen because this may
2139 * influence which certificate is sent
2141 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
2142 && s
->ctx
->ext
.status_cb
!= NULL
) {
2145 /* If no certificate can't return certificate status */
2146 if (s
->s3
.tmp
.cert
!= NULL
) {
2148 * Set current certificate to one we will use so SSL_get_certificate
2149 * et al can pick it up.
2151 s
->cert
->key
= s
->s3
.tmp
.cert
;
2152 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
2154 /* We don't want to send a status request response */
2155 case SSL_TLSEXT_ERR_NOACK
:
2156 s
->ext
.status_expected
= 0;
2158 /* status request response should be sent */
2159 case SSL_TLSEXT_ERR_OK
:
2160 if (s
->ext
.ocsp
.resp
)
2161 s
->ext
.status_expected
= 1;
2163 /* something bad happened */
2164 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2166 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2167 SSL_F_TLS_HANDLE_STATUS_REQUEST
,
2168 SSL_R_CLIENTHELLO_TLSEXT
);
2178 * Call the alpn_select callback if needed. Upon success, returns 1.
2179 * Upon failure, returns 0.
2181 int tls_handle_alpn(SSL
*s
)
2183 const unsigned char *selected
= NULL
;
2184 unsigned char selected_len
= 0;
2186 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2187 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
2188 s
->s3
.alpn_proposed
,
2189 (unsigned int)s
->s3
.alpn_proposed_len
,
2190 s
->ctx
->ext
.alpn_select_cb_arg
);
2192 if (r
== SSL_TLSEXT_ERR_OK
) {
2193 OPENSSL_free(s
->s3
.alpn_selected
);
2194 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2195 if (s
->s3
.alpn_selected
== NULL
) {
2196 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_HANDLE_ALPN
,
2197 ERR_R_INTERNAL_ERROR
);
2200 s
->s3
.alpn_selected_len
= selected_len
;
2201 #ifndef OPENSSL_NO_NEXTPROTONEG
2202 /* ALPN takes precedence over NPN. */
2206 /* Check ALPN is consistent with session */
2207 if (s
->session
->ext
.alpn_selected
== NULL
2208 || selected_len
!= s
->session
->ext
.alpn_selected_len
2209 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2210 selected_len
) != 0) {
2211 /* Not consistent so can't be used for early_data */
2212 s
->ext
.early_data_ok
= 0;
2216 * This is a new session and so alpn_selected should have
2217 * been initialised to NULL. We should update it with the
2220 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2221 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2222 SSL_F_TLS_HANDLE_ALPN
,
2223 ERR_R_INTERNAL_ERROR
);
2226 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2228 if (s
->session
->ext
.alpn_selected
== NULL
) {
2229 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2230 SSL_F_TLS_HANDLE_ALPN
,
2231 ERR_R_INTERNAL_ERROR
);
2234 s
->session
->ext
.alpn_selected_len
= selected_len
;
2239 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2240 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
, SSL_F_TLS_HANDLE_ALPN
,
2241 SSL_R_NO_APPLICATION_PROTOCOL
);
2245 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2250 /* Check ALPN is consistent with session */
2251 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2252 /* Not consistent so can't be used for early_data */
2253 s
->ext
.early_data_ok
= 0;
2259 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
2261 const SSL_CIPHER
*cipher
;
2263 if (wst
== WORK_MORE_A
) {
2264 int rv
= tls_early_post_process_client_hello(s
);
2266 /* SSLfatal() was already called */
2273 if (wst
== WORK_MORE_B
) {
2274 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2275 /* Let cert callback update server certificates if required */
2276 if (!s
->hit
&& s
->cert
->cert_cb
!= NULL
) {
2277 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
2279 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2280 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2281 SSL_R_CERT_CB_ERROR
);
2285 s
->rwstate
= SSL_X509_LOOKUP
;
2288 s
->rwstate
= SSL_NOTHING
;
2291 /* In TLSv1.3 we selected the ciphersuite before resumption */
2292 if (!SSL_IS_TLS13(s
)) {
2294 ssl3_choose_cipher(s
, s
->peer_ciphers
, SSL_get_ciphers(s
));
2296 if (cipher
== NULL
) {
2297 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2298 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2299 SSL_R_NO_SHARED_CIPHER
);
2302 s
->s3
.tmp
.new_cipher
= cipher
;
2305 if (!tls_choose_sigalg(s
, 1)) {
2306 /* SSLfatal already called */
2309 /* check whether we should disable session resumption */
2310 if (s
->not_resumable_session_cb
!= NULL
)
2311 s
->session
->not_resumable
=
2312 s
->not_resumable_session_cb(s
,
2313 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2314 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2315 if (s
->session
->not_resumable
)
2316 /* do not send a session ticket */
2317 s
->ext
.ticket_expected
= 0;
2320 /* Session-id reuse */
2321 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2325 * we now have the following setup.
2327 * cipher_list - our preferred list of ciphers
2328 * ciphers - the clients preferred list of ciphers
2329 * compression - basically ignored right now
2330 * ssl version is set - sslv3
2331 * s->session - The ssl session has been setup.
2332 * s->hit - session reuse flag
2333 * s->s3.tmp.new_cipher - the new cipher to use.
2337 * Call status_request callback if needed. Has to be done after the
2338 * certificate callbacks etc above.
2340 if (!tls_handle_status_request(s
)) {
2341 /* SSLfatal() already called */
2345 * Call alpn_select callback if needed. Has to be done after SNI and
2346 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2347 * we already did this because cipher negotiation happens earlier, and
2348 * we must handle ALPN before we decide whether to accept early_data.
2350 if (!SSL_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2351 /* SSLfatal() already called */
2357 #ifndef OPENSSL_NO_SRP
2358 if (wst
== WORK_MORE_C
) {
2360 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2362 * callback indicates further work to be done
2364 s
->rwstate
= SSL_X509_LOOKUP
;
2368 /* SSLfatal() already called */
2374 return WORK_FINISHED_STOP
;
2379 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2384 unsigned char *session_id
;
2385 int usetls13
= SSL_IS_TLS13(s
) || s
->hello_retry_request
== SSL_HRR_PENDING
;
2387 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2388 if (!WPACKET_put_bytes_u16(pkt
, version
)
2390 * Random stuff. Filling of the server_random takes place in
2391 * tls_process_client_hello()
2393 || !WPACKET_memcpy(pkt
,
2394 s
->hello_retry_request
== SSL_HRR_PENDING
2395 ? hrrrandom
: s
->s3
.server_random
,
2396 SSL3_RANDOM_SIZE
)) {
2397 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2398 ERR_R_INTERNAL_ERROR
);
2403 * There are several cases for the session ID to send
2404 * back in the server hello:
2405 * - For session reuse from the session cache,
2406 * we send back the old session ID.
2407 * - If stateless session reuse (using a session ticket)
2408 * is successful, we send back the client's "session ID"
2409 * (which doesn't actually identify the session).
2410 * - If it is a new session, we send back the new
2412 * - However, if we want the new session to be single-use,
2413 * we send back a 0-length session ID.
2414 * - In TLSv1.3 we echo back the session id sent to us by the client
2416 * s->hit is non-zero in either case of session reuse,
2417 * so the following won't overwrite an ID that we're supposed
2420 if (s
->session
->not_resumable
||
2421 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2423 s
->session
->session_id_length
= 0;
2426 sl
= s
->tmp_session_id_len
;
2427 session_id
= s
->tmp_session_id
;
2429 sl
= s
->session
->session_id_length
;
2430 session_id
= s
->session
->session_id
;
2433 if (sl
> sizeof(s
->session
->session_id
)) {
2434 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2435 ERR_R_INTERNAL_ERROR
);
2439 /* set up the compression method */
2440 #ifdef OPENSSL_NO_COMP
2443 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2446 compm
= s
->s3
.tmp
.new_compression
->id
;
2449 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2450 || !s
->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
, pkt
, &len
)
2451 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2452 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2453 ERR_R_INTERNAL_ERROR
);
2457 if (!tls_construct_extensions(s
, pkt
,
2458 s
->hello_retry_request
== SSL_HRR_PENDING
2459 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2461 ? SSL_EXT_TLS1_3_SERVER_HELLO
2462 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2464 /* SSLfatal() already called */
2468 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2469 /* Ditch the session. We'll create a new one next time around */
2470 SSL_SESSION_free(s
->session
);
2475 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2476 * a synthetic message_hash in place of ClientHello1.
2478 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2479 /* SSLfatal() already called */
2482 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2483 && !ssl3_digest_cached_records(s
, 0)) {
2484 /* SSLfatal() already called */;
2491 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2493 if (!s
->s3
.tmp
.cert_request
) {
2494 if (!ssl3_digest_cached_records(s
, 0)) {
2495 /* SSLfatal() already called */
2502 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2504 #ifndef OPENSSL_NO_DH
2505 EVP_PKEY
*pkdh
= NULL
;
2507 #ifndef OPENSSL_NO_EC
2508 unsigned char *encodedPoint
= NULL
;
2509 size_t encodedlen
= 0;
2512 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2516 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2517 EVP_PKEY_CTX
*pctx
= NULL
;
2518 size_t paramlen
, paramoffset
;
2520 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2521 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2522 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2526 if (md_ctx
== NULL
) {
2527 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2528 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2532 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2534 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2535 #ifndef OPENSSL_NO_PSK
2536 /* Plain PSK or RSAPSK nothing to do */
2537 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2539 #endif /* !OPENSSL_NO_PSK */
2540 #ifndef OPENSSL_NO_DH
2541 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2542 CERT
*cert
= s
->cert
;
2544 EVP_PKEY
*pkdhp
= NULL
;
2547 if (s
->cert
->dh_tmp_auto
) {
2548 DH
*dhp
= ssl_get_auto_dh(s
);
2549 pkdh
= EVP_PKEY_new();
2550 if (pkdh
== NULL
|| dhp
== NULL
) {
2552 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2553 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2554 ERR_R_INTERNAL_ERROR
);
2557 EVP_PKEY_assign_DH(pkdh
, dhp
);
2560 pkdhp
= cert
->dh_tmp
;
2562 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2563 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2564 pkdh
= ssl_dh_to_pkey(dhp
);
2566 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2567 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2568 ERR_R_INTERNAL_ERROR
);
2573 if (pkdhp
== NULL
) {
2574 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2575 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2576 SSL_R_MISSING_TMP_DH_KEY
);
2579 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2580 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2581 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2582 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2583 SSL_R_DH_KEY_TOO_SMALL
);
2586 if (s
->s3
.tmp
.pkey
!= NULL
) {
2587 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2588 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2589 ERR_R_INTERNAL_ERROR
);
2593 s
->s3
.tmp
.pkey
= ssl_generate_pkey(s
, pkdhp
);
2594 if (s
->s3
.tmp
.pkey
== NULL
) {
2595 /* SSLfatal() already called */
2599 dh
= EVP_PKEY_get0_DH(s
->s3
.tmp
.pkey
);
2601 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2602 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2603 ERR_R_INTERNAL_ERROR
);
2607 EVP_PKEY_free(pkdh
);
2610 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2611 DH_get0_key(dh
, &r
[2], NULL
);
2614 #ifndef OPENSSL_NO_EC
2615 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2617 if (s
->s3
.tmp
.pkey
!= NULL
) {
2618 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2619 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2620 ERR_R_INTERNAL_ERROR
);
2624 /* Get NID of appropriate shared curve */
2625 curve_id
= tls1_shared_group(s
, -2);
2626 if (curve_id
== 0) {
2627 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2628 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2629 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2632 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2633 /* Generate a new key for this curve */
2634 if (s
->s3
.tmp
.pkey
== NULL
) {
2635 /* SSLfatal() already called */
2639 /* Encode the public key. */
2640 encodedlen
= EVP_PKEY_get1_tls_encodedpoint(s
->s3
.tmp
.pkey
,
2642 if (encodedlen
== 0) {
2643 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2644 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2649 * We'll generate the serverKeyExchange message explicitly so we
2650 * can set these to NULLs
2657 #endif /* !OPENSSL_NO_EC */
2658 #ifndef OPENSSL_NO_SRP
2659 if (type
& SSL_kSRP
) {
2660 if ((s
->srp_ctx
.N
== NULL
) ||
2661 (s
->srp_ctx
.g
== NULL
) ||
2662 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2663 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2664 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2665 SSL_R_MISSING_SRP_PARAM
);
2668 r
[0] = s
->srp_ctx
.N
;
2669 r
[1] = s
->srp_ctx
.g
;
2670 r
[2] = s
->srp_ctx
.s
;
2671 r
[3] = s
->srp_ctx
.B
;
2675 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2676 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2677 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2681 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2682 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2684 } else if (lu
== NULL
) {
2685 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
2686 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2690 #ifndef OPENSSL_NO_PSK
2691 if (type
& SSL_PSK
) {
2692 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2693 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2696 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2697 * checked this when we set the identity hint - but just in case
2699 if (len
> PSK_MAX_IDENTITY_LEN
2700 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2702 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2703 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2704 ERR_R_INTERNAL_ERROR
);
2710 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2711 unsigned char *binval
;
2714 #ifndef OPENSSL_NO_SRP
2715 if ((i
== 2) && (type
& SSL_kSRP
)) {
2716 res
= WPACKET_start_sub_packet_u8(pkt
);
2719 res
= WPACKET_start_sub_packet_u16(pkt
);
2722 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2723 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2724 ERR_R_INTERNAL_ERROR
);
2728 #ifndef OPENSSL_NO_DH
2730 * for interoperability with some versions of the Microsoft TLS
2731 * stack, we need to zero pad the DHE pub key to the same length
2734 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2735 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2738 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2739 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2740 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2741 ERR_R_INTERNAL_ERROR
);
2744 memset(binval
, 0, len
);
2748 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2749 || !WPACKET_close(pkt
)) {
2750 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2751 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2752 ERR_R_INTERNAL_ERROR
);
2756 BN_bn2bin(r
[i
], binval
);
2759 #ifndef OPENSSL_NO_EC
2760 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2762 * We only support named (not generic) curves. In this situation, the
2763 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2764 * [1 byte length of encoded point], followed by the actual encoded
2767 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2768 || !WPACKET_put_bytes_u8(pkt
, 0)
2769 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2770 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2771 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2772 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2773 ERR_R_INTERNAL_ERROR
);
2776 OPENSSL_free(encodedPoint
);
2777 encodedPoint
= NULL
;
2783 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2785 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2786 size_t siglen
= 0, tbslen
;
2788 if (pkey
== NULL
|| !tls1_lookup_md(s
->ctx
, lu
, &md
)) {
2789 /* Should never happen */
2790 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2791 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2792 ERR_R_INTERNAL_ERROR
);
2795 /* Get length of the parameters we have written above */
2796 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2797 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2798 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2799 ERR_R_INTERNAL_ERROR
);
2802 /* send signature algorithm */
2803 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2804 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2805 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2806 ERR_R_INTERNAL_ERROR
);
2810 if (EVP_DigestSignInit_with_libctx(md_ctx
, &pctx
,
2811 md
== NULL
? NULL
: EVP_MD_name(md
),
2812 s
->ctx
->libctx
, s
->ctx
->propq
,
2814 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2815 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2816 ERR_R_INTERNAL_ERROR
);
2819 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2820 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2821 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2822 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2823 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2828 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2829 s
->init_buf
->data
+ paramoffset
,
2832 /* SSLfatal() already called */
2836 if (EVP_DigestSign(md_ctx
, NULL
, &siglen
, tbs
, tbslen
) <=0
2837 || !WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2838 || EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
) <= 0
2839 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2840 || sigbytes1
!= sigbytes2
) {
2842 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2843 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2844 ERR_R_INTERNAL_ERROR
);
2850 EVP_MD_CTX_free(md_ctx
);
2853 #ifndef OPENSSL_NO_DH
2854 EVP_PKEY_free(pkdh
);
2856 #ifndef OPENSSL_NO_EC
2857 OPENSSL_free(encodedPoint
);
2859 EVP_MD_CTX_free(md_ctx
);
2863 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2865 if (SSL_IS_TLS13(s
)) {
2866 /* Send random context when doing post-handshake auth */
2867 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2868 OPENSSL_free(s
->pha_context
);
2869 s
->pha_context_len
= 32;
2870 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
2871 || RAND_bytes_ex(s
->ctx
->libctx
, s
->pha_context
,
2872 s
->pha_context_len
) <= 0
2873 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
, s
->pha_context_len
)) {
2874 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2875 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2876 ERR_R_INTERNAL_ERROR
);
2879 /* reset the handshake hash back to just after the ClientFinished */
2880 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2881 /* SSLfatal() already called */
2885 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2886 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2887 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2888 ERR_R_INTERNAL_ERROR
);
2893 if (!tls_construct_extensions(s
, pkt
,
2894 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2896 /* SSLfatal() already called */
2902 /* get the list of acceptable cert types */
2903 if (!WPACKET_start_sub_packet_u8(pkt
)
2904 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2905 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2906 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2910 if (SSL_USE_SIGALGS(s
)) {
2911 const uint16_t *psigs
;
2912 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2914 if (!WPACKET_start_sub_packet_u16(pkt
)
2915 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2916 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2917 || !WPACKET_close(pkt
)) {
2918 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2919 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2920 ERR_R_INTERNAL_ERROR
);
2925 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2926 /* SSLfatal() already called */
2932 s
->s3
.tmp
.cert_request
= 1;
2936 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
)
2938 #ifndef OPENSSL_NO_PSK
2939 unsigned char psk
[PSK_MAX_PSK_LEN
];
2941 PACKET psk_identity
;
2943 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2944 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2945 SSL_R_LENGTH_MISMATCH
);
2948 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2949 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2950 SSL_R_DATA_LENGTH_TOO_LONG
);
2953 if (s
->psk_server_callback
== NULL
) {
2954 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2955 SSL_R_PSK_NO_SERVER_CB
);
2959 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2960 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2961 ERR_R_INTERNAL_ERROR
);
2965 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2968 if (psklen
> PSK_MAX_PSK_LEN
) {
2969 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2970 ERR_R_INTERNAL_ERROR
);
2972 } else if (psklen
== 0) {
2974 * PSK related to the given identity not found
2976 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
2977 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2978 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2982 OPENSSL_free(s
->s3
.tmp
.psk
);
2983 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2984 OPENSSL_cleanse(psk
, psklen
);
2986 if (s
->s3
.tmp
.psk
== NULL
) {
2987 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2988 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2992 s
->s3
.tmp
.psklen
= psklen
;
2996 /* Should never happen */
2997 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2998 ERR_R_INTERNAL_ERROR
);
3003 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
)
3005 #ifndef OPENSSL_NO_RSA
3007 PACKET enc_premaster
;
3008 EVP_PKEY
*rsa
= NULL
;
3009 unsigned char *rsa_decrypt
= NULL
;
3011 EVP_PKEY_CTX
*ctx
= NULL
;
3012 OSSL_PARAM params
[3], *p
= params
;
3014 rsa
= s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
;
3016 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3017 SSL_R_MISSING_RSA_CERTIFICATE
);
3021 /* SSLv3 and pre-standard DTLS omit the length bytes. */
3022 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
3023 enc_premaster
= *pkt
;
3025 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
3026 || PACKET_remaining(pkt
) != 0) {
3027 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3028 SSL_R_LENGTH_MISMATCH
);
3033 outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
3034 rsa_decrypt
= OPENSSL_malloc(outlen
);
3035 if (rsa_decrypt
== NULL
) {
3036 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3037 ERR_R_MALLOC_FAILURE
);
3041 ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, rsa
, s
->ctx
->propq
);
3043 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3044 ERR_R_MALLOC_FAILURE
);
3049 * We must not leak whether a decryption failure occurs because of
3050 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
3051 * section 7.4.7.1). We use the special padding type
3052 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automaticaly decrypt the
3053 * RSA, check the padding and check that the client version is as expected
3054 * in the premaster secret. If any of that fails then the function appears
3055 * to return successfully but with a random result. The call below could
3056 * still fail if the input is publicly invalid.
3057 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
3059 if (EVP_PKEY_decrypt_init(ctx
) <= 0
3060 || EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_WITH_TLS_PADDING
) <= 0) {
3061 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3062 SSL_R_DECRYPTION_FAILED
);
3066 *p
++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
,
3067 (unsigned int *)&s
->client_version
);
3068 if ((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) != 0)
3069 *p
++ = OSSL_PARAM_construct_uint(
3070 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
,
3071 (unsigned int *)&s
->version
);
3072 *p
++ = OSSL_PARAM_construct_end();
3074 if (!EVP_PKEY_CTX_set_params(ctx
, params
)
3075 || EVP_PKEY_decrypt(ctx
, rsa_decrypt
, &outlen
,
3076 PACKET_data(&enc_premaster
),
3077 PACKET_remaining(&enc_premaster
)) <= 0) {
3078 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3079 SSL_R_DECRYPTION_FAILED
);
3084 * This test should never fail (otherwise we should have failed above) but
3085 * we double check anyway.
3087 if (outlen
!= SSL_MAX_MASTER_KEY_LENGTH
) {
3088 OPENSSL_cleanse(rsa_decrypt
, SSL_MAX_MASTER_KEY_LENGTH
);
3089 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3090 SSL_R_DECRYPTION_FAILED
);
3094 /* Also cleanses rsa_decrypt (on success or failure) */
3095 if (!ssl_generate_master_secret(s
, rsa_decrypt
,
3096 SSL_MAX_MASTER_KEY_LENGTH
, 0)) {
3097 /* SSLfatal() already called */
3103 OPENSSL_free(rsa_decrypt
);
3104 EVP_PKEY_CTX_free(ctx
);
3107 /* Should never happen */
3108 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3109 ERR_R_INTERNAL_ERROR
);
3114 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
)
3116 #ifndef OPENSSL_NO_DH
3117 EVP_PKEY
*skey
= NULL
;
3121 const unsigned char *data
;
3122 EVP_PKEY
*ckey
= NULL
;
3125 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
3126 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3127 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
3130 skey
= s
->s3
.tmp
.pkey
;
3132 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3133 SSL_R_MISSING_TMP_DH_KEY
);
3137 if (PACKET_remaining(pkt
) == 0L) {
3138 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3139 SSL_R_MISSING_TMP_DH_KEY
);
3142 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3143 /* We already checked we have enough data */
3144 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3145 ERR_R_INTERNAL_ERROR
);
3148 ckey
= EVP_PKEY_new();
3149 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3150 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3151 SSL_R_COPY_PARAMETERS_FAILED
);
3155 cdh
= EVP_PKEY_get0_DH(ckey
);
3156 pub_key
= BN_bin2bn(data
, i
, NULL
);
3157 if (pub_key
== NULL
|| cdh
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
3158 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3159 ERR_R_INTERNAL_ERROR
);
3164 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3165 /* SSLfatal() already called */
3170 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3171 s
->s3
.tmp
.pkey
= NULL
;
3173 EVP_PKEY_free(ckey
);
3176 /* Should never happen */
3177 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3178 ERR_R_INTERNAL_ERROR
);
3183 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
)
3185 #ifndef OPENSSL_NO_EC
3186 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3187 EVP_PKEY
*ckey
= NULL
;
3190 if (PACKET_remaining(pkt
) == 0L) {
3191 /* We don't support ECDH client auth */
3192 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3193 SSL_R_MISSING_TMP_ECDH_KEY
);
3197 const unsigned char *data
;
3200 * Get client's public key from encoded point in the
3201 * ClientKeyExchange message.
3204 /* Get encoded point length */
3205 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3206 || PACKET_remaining(pkt
) != 0) {
3207 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3208 SSL_R_LENGTH_MISMATCH
);
3212 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3213 SSL_R_MISSING_TMP_ECDH_KEY
);
3217 ckey
= EVP_PKEY_new();
3218 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3219 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3220 SSL_R_COPY_PARAMETERS_FAILED
);
3224 if (EVP_PKEY_set1_tls_encodedpoint(ckey
, data
, i
) == 0) {
3225 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3231 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3232 /* SSLfatal() already called */
3237 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3238 s
->s3
.tmp
.pkey
= NULL
;
3240 EVP_PKEY_free(ckey
);
3244 /* Should never happen */
3245 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3246 ERR_R_INTERNAL_ERROR
);
3251 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
)
3253 #ifndef OPENSSL_NO_SRP
3255 const unsigned char *data
;
3257 if (!PACKET_get_net_2(pkt
, &i
)
3258 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3259 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3260 SSL_R_BAD_SRP_A_LENGTH
);
3263 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3264 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3268 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3269 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS_PROCESS_CKE_SRP
,
3270 SSL_R_BAD_SRP_PARAMETERS
);
3273 OPENSSL_free(s
->session
->srp_username
);
3274 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3275 if (s
->session
->srp_username
== NULL
) {
3276 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3277 ERR_R_MALLOC_FAILURE
);
3281 if (!srp_generate_server_master_secret(s
)) {
3282 /* SSLfatal() already called */
3288 /* Should never happen */
3289 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3290 ERR_R_INTERNAL_ERROR
);
3295 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
)
3297 #ifndef OPENSSL_NO_GOST
3298 EVP_PKEY_CTX
*pkey_ctx
;
3299 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3300 unsigned char premaster_secret
[32];
3301 const unsigned char *start
;
3302 size_t outlen
= 32, inlen
;
3303 unsigned long alg_a
;
3304 GOST_KX_MESSAGE
*pKX
= NULL
;
3305 const unsigned char *ptr
;
3308 /* Get our certificate private key */
3309 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3310 if (alg_a
& SSL_aGOST12
) {
3312 * New GOST ciphersuites have SSL_aGOST01 bit too
3314 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3316 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3319 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3321 } else if (alg_a
& SSL_aGOST01
) {
3322 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3325 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, pk
, s
->ctx
->propq
);
3326 if (pkey_ctx
== NULL
) {
3327 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3328 ERR_R_MALLOC_FAILURE
);
3331 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3332 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3333 ERR_R_INTERNAL_ERROR
);
3337 * If client certificate is present and is of the same type, maybe
3338 * use it for key exchange. Don't mind errors from
3339 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3340 * client certificate for authorization only.
3342 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3343 if (client_pub_pkey
) {
3344 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3348 ptr
= PACKET_data(pkt
);
3349 /* Some implementations provide extra data in the opaqueBlob
3350 * We have nothing to do with this blob so we just skip it */
3351 pKX
= d2i_GOST_KX_MESSAGE(NULL
, &ptr
, PACKET_remaining(pkt
));
3353 || pKX
->kxBlob
== NULL
3354 || ASN1_TYPE_get(pKX
->kxBlob
) != V_ASN1_SEQUENCE
) {
3355 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3356 SSL_R_DECRYPTION_FAILED
);
3360 if (!PACKET_forward(pkt
, ptr
- PACKET_data(pkt
))) {
3361 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3362 SSL_R_DECRYPTION_FAILED
);
3366 if (PACKET_remaining(pkt
) != 0) {
3367 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3368 SSL_R_DECRYPTION_FAILED
);
3372 inlen
= pKX
->kxBlob
->value
.sequence
->length
;
3373 start
= pKX
->kxBlob
->value
.sequence
->data
;
3375 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3377 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3378 SSL_R_DECRYPTION_FAILED
);
3381 /* Generate master secret */
3382 if (!ssl_generate_master_secret(s
, premaster_secret
,
3383 sizeof(premaster_secret
), 0)) {
3384 /* SSLfatal() already called */
3387 /* Check if pubkey from client certificate was used */
3388 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3390 s
->statem
.no_cert_verify
= 1;
3394 EVP_PKEY_CTX_free(pkey_ctx
);
3395 GOST_KX_MESSAGE_free(pKX
);
3398 /* Should never happen */
3399 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3400 ERR_R_INTERNAL_ERROR
);
3405 static int tls_process_cke_gost18(SSL
*s
, PACKET
*pkt
)
3407 #ifndef OPENSSL_NO_GOST
3408 unsigned char rnd_dgst
[32];
3409 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3410 EVP_PKEY
*pk
= NULL
;
3411 unsigned char premaster_secret
[32];
3412 const unsigned char *start
= NULL
;
3413 size_t outlen
= 32, inlen
= 0;
3415 int cipher_nid
= gost18_cke_cipher_nid(s
);
3417 if (cipher_nid
== NID_undef
) {
3418 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CKE_GOST18
,
3419 ERR_R_INTERNAL_ERROR
);
3423 if (gost_ukm(s
, rnd_dgst
) <= 0) {
3424 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CKE_GOST18
,
3425 ERR_R_INTERNAL_ERROR
);
3429 /* Get our certificate private key */
3430 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
!= NULL
?
3431 s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
:
3432 s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3434 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3435 SSL_R_BAD_HANDSHAKE_STATE
);
3439 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, pk
, s
->ctx
->propq
);
3440 if (pkey_ctx
== NULL
) {
3441 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3442 ERR_R_MALLOC_FAILURE
);
3445 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3446 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3447 ERR_R_INTERNAL_ERROR
);
3451 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3452 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3453 EVP_PKEY_CTRL_SET_IV
, 32, rnd_dgst
) < 0) {
3454 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3459 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_DECRYPT
,
3460 EVP_PKEY_CTRL_CIPHER
, cipher_nid
, NULL
) < 0) {
3461 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3465 inlen
= PACKET_remaining(pkt
);
3466 start
= PACKET_data(pkt
);
3468 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3469 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3470 SSL_R_DECRYPTION_FAILED
);
3473 /* Generate master secret */
3474 if (!ssl_generate_master_secret(s
, premaster_secret
,
3475 sizeof(premaster_secret
), 0)) {
3476 /* SSLfatal() already called */
3482 EVP_PKEY_CTX_free(pkey_ctx
);
3485 /* Should never happen */
3486 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST18
,
3487 ERR_R_INTERNAL_ERROR
);
3492 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3494 unsigned long alg_k
;
3496 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3498 /* For PSK parse and retrieve identity, obtain PSK key */
3499 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3500 /* SSLfatal() already called */
3504 if (alg_k
& SSL_kPSK
) {
3505 /* Identity extracted earlier: should be nothing left */
3506 if (PACKET_remaining(pkt
) != 0) {
3507 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3508 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3509 SSL_R_LENGTH_MISMATCH
);
3512 /* PSK handled by ssl_generate_master_secret */
3513 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3514 /* SSLfatal() already called */
3517 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3518 if (!tls_process_cke_rsa(s
, pkt
)) {
3519 /* SSLfatal() already called */
3522 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3523 if (!tls_process_cke_dhe(s
, pkt
)) {
3524 /* SSLfatal() already called */
3527 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3528 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3529 /* SSLfatal() already called */
3532 } else if (alg_k
& SSL_kSRP
) {
3533 if (!tls_process_cke_srp(s
, pkt
)) {
3534 /* SSLfatal() already called */
3537 } else if (alg_k
& SSL_kGOST
) {
3538 if (!tls_process_cke_gost(s
, pkt
)) {
3539 /* SSLfatal() already called */
3542 } else if (alg_k
& SSL_kGOST18
) {
3543 if (!tls_process_cke_gost18(s
, pkt
)) {
3544 /* SSLfatal() already called */
3548 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3549 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3550 SSL_R_UNKNOWN_CIPHER_TYPE
);
3554 return MSG_PROCESS_CONTINUE_PROCESSING
;
3556 #ifndef OPENSSL_NO_PSK
3557 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3558 s
->s3
.tmp
.psk
= NULL
;
3560 return MSG_PROCESS_ERROR
;
3563 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3565 #ifndef OPENSSL_NO_SCTP
3566 if (wst
== WORK_MORE_A
) {
3567 if (SSL_IS_DTLS(s
)) {
3568 unsigned char sctpauthkey
[64];
3569 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3572 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3575 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3576 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3578 /* Don't include the terminating zero. */
3579 labellen
= sizeof(labelbuffer
) - 1;
3580 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3583 if (SSL_export_keying_material(s
, sctpauthkey
,
3584 sizeof(sctpauthkey
), labelbuffer
,
3587 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3588 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3589 ERR_R_INTERNAL_ERROR
);
3593 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3594 sizeof(sctpauthkey
), sctpauthkey
);
3599 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3601 * No certificate verify or no peer certificate so we no longer need
3602 * the handshake_buffer
3604 if (!ssl3_digest_cached_records(s
, 0)) {
3605 /* SSLfatal() already called */
3608 return WORK_FINISHED_CONTINUE
;
3610 if (!s
->s3
.handshake_buffer
) {
3611 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3612 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3613 ERR_R_INTERNAL_ERROR
);
3617 * For sigalgs freeze the handshake buffer. If we support
3618 * extms we've done this already so this is a no-op
3620 if (!ssl3_digest_cached_records(s
, 1)) {
3621 /* SSLfatal() already called */
3626 return WORK_FINISHED_CONTINUE
;
3629 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3632 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3635 const unsigned char *certstart
, *certbytes
;
3636 STACK_OF(X509
) *sk
= NULL
;
3637 PACKET spkt
, context
;
3639 SSL_SESSION
*new_sess
= NULL
;
3642 * To get this far we must have read encrypted data from the client. We no
3643 * longer tolerate unencrypted alerts. This value is ignored if less than
3646 s
->statem
.enc_read_state
= ENC_READ_STATE_VALID
;
3648 if ((sk
= sk_X509_new_null()) == NULL
) {
3649 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3650 ERR_R_MALLOC_FAILURE
);
3654 if (SSL_IS_TLS13(s
) && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3655 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3656 || (s
->pha_context
!= NULL
&&
3657 !PACKET_equal(&context
, s
->pha_context
, s
->pha_context_len
)))) {
3658 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3659 SSL_R_INVALID_CONTEXT
);
3663 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3664 || PACKET_remaining(pkt
) != 0) {
3665 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3666 SSL_R_LENGTH_MISMATCH
);
3670 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3671 if (!PACKET_get_net_3(&spkt
, &l
)
3672 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3673 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3674 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3675 SSL_R_CERT_LENGTH_MISMATCH
);
3679 certstart
= certbytes
;
3680 x
= X509_new_with_libctx(s
->ctx
->libctx
, s
->ctx
->propq
);
3682 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3683 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3686 if (d2i_X509(&x
, (const unsigned char **)&certbytes
, l
) == NULL
) {
3687 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3688 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3692 if (certbytes
!= (certstart
+ l
)) {
3693 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3694 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3695 SSL_R_CERT_LENGTH_MISMATCH
);
3699 if (SSL_IS_TLS13(s
)) {
3700 RAW_EXTENSION
*rawexts
= NULL
;
3703 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3704 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3705 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3709 if (!tls_collect_extensions(s
, &extensions
,
3710 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3711 NULL
, chainidx
== 0)
3712 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3713 rawexts
, x
, chainidx
,
3714 PACKET_remaining(&spkt
) == 0)) {
3715 OPENSSL_free(rawexts
);
3718 OPENSSL_free(rawexts
);
3721 if (!sk_X509_push(sk
, x
)) {
3722 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3723 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3724 ERR_R_MALLOC_FAILURE
);
3730 if (sk_X509_num(sk
) <= 0) {
3731 /* TLS does not mind 0 certs returned */
3732 if (s
->version
== SSL3_VERSION
) {
3733 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3734 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3735 SSL_R_NO_CERTIFICATES_RETURNED
);
3738 /* Fail for TLS only if we required a certificate */
3739 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3740 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3741 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3742 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3743 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3746 /* No client certificate so digest cached records */
3747 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3748 /* SSLfatal() already called */
3753 i
= ssl_verify_cert_chain(s
, sk
);
3755 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3756 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3757 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3761 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3762 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3765 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3767 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3768 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3769 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3775 * Sessions must be immutable once they go into the session cache. Otherwise
3776 * we can get multi-thread problems. Therefore we don't "update" sessions,
3777 * we replace them with a duplicate. Here, we need to do this every time
3778 * a new certificate is received via post-handshake authentication, as the
3779 * session may have already gone into the session cache.
3782 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3783 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3784 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3785 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3786 ERR_R_MALLOC_FAILURE
);
3790 SSL_SESSION_free(s
->session
);
3791 s
->session
= new_sess
;
3794 X509_free(s
->session
->peer
);
3795 s
->session
->peer
= sk_X509_shift(sk
);
3796 s
->session
->verify_result
= s
->verify_result
;
3798 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3799 s
->session
->peer_chain
= sk
;
3802 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3805 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3806 /* SSLfatal() already called */
3811 * Inconsistency alert: cert_chain does *not* include the peer's own
3812 * certificate, while we do include it in statem_clnt.c
3816 /* Save the current hash state for when we receive the CertificateVerify */
3817 if (SSL_IS_TLS13(s
)) {
3818 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3819 sizeof(s
->cert_verify_hash
),
3820 &s
->cert_verify_hash_len
)) {
3821 /* SSLfatal() already called */
3825 /* Resend session tickets */
3826 s
->sent_tickets
= 0;
3829 ret
= MSG_PROCESS_CONTINUE_READING
;
3833 sk_X509_pop_free(sk
, X509_free
);
3837 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3839 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3842 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3843 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3848 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3849 * for the server Certificate message
3851 if (SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3852 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3853 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3856 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3857 /* SSLfatal() already called */
3864 static int create_ticket_prequel(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3865 unsigned char *tick_nonce
)
3868 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3869 * unspecified for resumed session (for simplicity).
3870 * In TLSv1.3 we reset the "time" field above, and always specify the
3873 if (!WPACKET_put_bytes_u32(pkt
,
3874 (s
->hit
&& !SSL_IS_TLS13(s
))
3875 ? 0 : s
->session
->timeout
)) {
3876 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3877 ERR_R_INTERNAL_ERROR
);
3881 if (SSL_IS_TLS13(s
)) {
3882 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3883 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3884 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3885 ERR_R_INTERNAL_ERROR
);
3890 /* Start the sub-packet for the actual ticket data */
3891 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3892 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3893 ERR_R_INTERNAL_ERROR
);
3900 static int construct_stateless_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3901 unsigned char *tick_nonce
)
3903 unsigned char *senc
= NULL
;
3904 EVP_CIPHER_CTX
*ctx
= NULL
;
3905 SSL_HMAC
*hctx
= NULL
;
3906 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3907 const unsigned char *const_p
;
3908 int len
, slen_full
, slen
, lenfinal
;
3911 SSL_CTX
*tctx
= s
->session_ctx
;
3912 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3913 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3915 size_t macoffset
, macendoffset
;
3917 /* get session encoding length */
3918 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3920 * Some length values are 16 bits, so forget it if session is too
3923 if (slen_full
== 0 || slen_full
> 0xFF00) {
3924 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3925 ERR_R_INTERNAL_ERROR
);
3928 senc
= OPENSSL_malloc(slen_full
);
3930 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3931 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_MALLOC_FAILURE
);
3935 ctx
= EVP_CIPHER_CTX_new();
3936 hctx
= ssl_hmac_new(tctx
);
3937 if (ctx
== NULL
|| hctx
== NULL
) {
3938 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3939 ERR_R_MALLOC_FAILURE
);
3944 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3945 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3946 ERR_R_INTERNAL_ERROR
);
3951 * create a fresh copy (not shared with other threads) to clean up
3954 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3956 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3957 ERR_R_INTERNAL_ERROR
);
3961 slen
= i2d_SSL_SESSION(sess
, NULL
);
3962 if (slen
== 0 || slen
> slen_full
) {
3963 /* shouldn't ever happen */
3964 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3965 ERR_R_INTERNAL_ERROR
);
3966 SSL_SESSION_free(sess
);
3970 if (!i2d_SSL_SESSION(sess
, &p
)) {
3971 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3972 ERR_R_INTERNAL_ERROR
);
3973 SSL_SESSION_free(sess
);
3976 SSL_SESSION_free(sess
);
3979 * Initialize HMAC and cipher contexts. If callback present it does
3980 * all the work otherwise use generated values from parent ctx.
3982 #ifndef OPENSSL_NO_DEPRECATED_3_0
3983 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
|| tctx
->ext
.ticket_key_cb
!= NULL
)
3985 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3990 if (tctx
->ext
.ticket_key_evp_cb
!= NULL
)
3991 ret
= tctx
->ext
.ticket_key_evp_cb(s
, key_name
, iv
, ctx
,
3992 ssl_hmac_get0_EVP_MAC_CTX(hctx
),
3994 #ifndef OPENSSL_NO_DEPRECATED_3_0
3995 else if (tctx
->ext
.ticket_key_cb
!= NULL
)
3996 /* if 0 is returned, write an empty ticket */
3997 ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3998 ssl_hmac_get0_HMAC_CTX(hctx
), 1);
4003 /* Put timeout and length */
4004 if (!WPACKET_put_bytes_u32(pkt
, 0)
4005 || !WPACKET_put_bytes_u16(pkt
, 0)) {
4006 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4007 SSL_F_CONSTRUCT_STATELESS_TICKET
,
4008 ERR_R_INTERNAL_ERROR
);
4012 EVP_CIPHER_CTX_free(ctx
);
4013 ssl_hmac_free(hctx
);
4017 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4018 SSL_R_CALLBACK_FAILED
);
4021 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
4023 EVP_CIPHER
*cipher
= EVP_CIPHER_fetch(s
->ctx
->libctx
, "AES-256-CBC",
4026 if (cipher
== NULL
) {
4027 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4028 SSL_R_ALGORITHM_FETCH_FAILED
);
4032 iv_len
= EVP_CIPHER_iv_length(cipher
);
4033 if (RAND_bytes_ex(s
->ctx
->libctx
, iv
, iv_len
) <= 0
4034 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
4035 tctx
->ext
.secure
->tick_aes_key
, iv
)
4036 || !ssl_hmac_init(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
4037 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
4039 EVP_CIPHER_free(cipher
);
4040 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4041 ERR_R_INTERNAL_ERROR
);
4044 EVP_CIPHER_free(cipher
);
4045 memcpy(key_name
, tctx
->ext
.tick_key_name
,
4046 sizeof(tctx
->ext
.tick_key_name
));
4049 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
4050 /* SSLfatal() already called */
4054 if (!WPACKET_get_total_written(pkt
, &macoffset
)
4055 /* Output key name */
4056 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
4058 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
4059 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
4061 /* Encrypt session data */
4062 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
4063 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
4064 || encdata1
!= encdata2
4065 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
4066 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
4067 || encdata1
+ len
!= encdata2
4068 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
4069 || !WPACKET_get_total_written(pkt
, &macendoffset
)
4070 || !ssl_hmac_update(hctx
,
4071 (unsigned char *)s
->init_buf
->data
+ macoffset
,
4072 macendoffset
- macoffset
)
4073 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
4074 || !ssl_hmac_final(hctx
, macdata1
, &hlen
, EVP_MAX_MD_SIZE
)
4075 || hlen
> EVP_MAX_MD_SIZE
4076 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
4077 || macdata1
!= macdata2
) {
4078 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4079 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_INTERNAL_ERROR
);
4083 /* Close the sub-packet created by create_ticket_prequel() */
4084 if (!WPACKET_close(pkt
)) {
4085 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4086 ERR_R_INTERNAL_ERROR
);
4093 EVP_CIPHER_CTX_free(ctx
);
4094 ssl_hmac_free(hctx
);
4098 static int construct_stateful_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
4099 unsigned char *tick_nonce
)
4101 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
4102 /* SSLfatal() already called */
4106 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
4107 s
->session
->session_id_length
)
4108 || !WPACKET_close(pkt
)) {
4109 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATEFUL_TICKET
,
4110 ERR_R_INTERNAL_ERROR
);
4117 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
4119 SSL_CTX
*tctx
= s
->session_ctx
;
4120 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
4122 unsigned char age_add_c
[sizeof(uint32_t)];
4126 age_add_u
.age_add
= 0;
4128 if (SSL_IS_TLS13(s
)) {
4131 static const unsigned char nonce_label
[] = "resumption";
4132 const EVP_MD
*md
= ssl_handshake_md(s
);
4133 int hashleni
= EVP_MD_size(md
);
4135 /* Ensure cast to size_t is safe */
4136 if (!ossl_assert(hashleni
>= 0)) {
4137 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4138 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4139 ERR_R_INTERNAL_ERROR
);
4142 hashlen
= (size_t)hashleni
;
4145 * If we already sent one NewSessionTicket, or we resumed then
4146 * s->session may already be in a cache and so we must not modify it.
4147 * Instead we need to take a copy of it and modify that.
4149 if (s
->sent_tickets
!= 0 || s
->hit
) {
4150 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
4152 if (new_sess
== NULL
) {
4153 /* SSLfatal already called */
4157 SSL_SESSION_free(s
->session
);
4158 s
->session
= new_sess
;
4161 if (!ssl_generate_session_id(s
, s
->session
)) {
4162 /* SSLfatal() already called */
4165 if (RAND_bytes_ex(s
->ctx
->libctx
, age_add_u
.age_add_c
,
4166 sizeof(age_add_u
)) <= 0) {
4167 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4168 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4169 ERR_R_INTERNAL_ERROR
);
4172 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
4174 nonce
= s
->next_ticket_nonce
;
4175 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
4176 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
4180 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
4182 sizeof(nonce_label
) - 1,
4185 s
->session
->master_key
,
4187 /* SSLfatal() already called */
4190 s
->session
->master_key_length
= hashlen
;
4192 s
->session
->time
= (long)time(NULL
);
4193 if (s
->s3
.alpn_selected
!= NULL
) {
4194 OPENSSL_free(s
->session
->ext
.alpn_selected
);
4195 s
->session
->ext
.alpn_selected
=
4196 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
4197 if (s
->session
->ext
.alpn_selected
== NULL
) {
4198 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4199 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4200 ERR_R_MALLOC_FAILURE
);
4203 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
4205 s
->session
->ext
.max_early_data
= s
->max_early_data
;
4208 if (tctx
->generate_ticket_cb
!= NULL
&&
4209 tctx
->generate_ticket_cb(s
, tctx
->ticket_cb_data
) == 0)
4213 * If we are using anti-replay protection then we behave as if
4214 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4215 * is no point in using full stateless tickets.
4218 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4219 || (s
->max_early_data
> 0
4220 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4221 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4222 /* SSLfatal() already called */
4225 } else if (!construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4227 /* SSLfatal() already called */
4231 if (SSL_IS_TLS13(s
)) {
4232 if (!tls_construct_extensions(s
, pkt
,
4233 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4235 /* SSLfatal() already called */
4239 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4240 * gets reset to 0 if we send more tickets following a post-handshake
4241 * auth, but |next_ticket_nonce| does not. If we're sending extra
4242 * tickets, decrement the count of pending extra tickets.
4245 s
->next_ticket_nonce
++;
4246 if (s
->ext
.extra_tickets_expected
> 0)
4247 s
->ext
.extra_tickets_expected
--;
4248 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4257 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4258 * create a separate message. Returns 1 on success or 0 on failure.
4260 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
4262 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4263 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4264 s
->ext
.ocsp
.resp_len
)) {
4265 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
,
4266 ERR_R_INTERNAL_ERROR
);
4273 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
4275 if (!tls_construct_cert_status_body(s
, pkt
)) {
4276 /* SSLfatal() already called */
4283 #ifndef OPENSSL_NO_NEXTPROTONEG
4285 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4286 * It sets the next_proto member in s if found
4288 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
4290 PACKET next_proto
, padding
;
4291 size_t next_proto_len
;
4294 * The payload looks like:
4296 * uint8 proto[proto_len];
4297 * uint8 padding_len;
4298 * uint8 padding[padding_len];
4300 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4301 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4302 || PACKET_remaining(pkt
) > 0) {
4303 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4304 SSL_R_LENGTH_MISMATCH
);
4305 return MSG_PROCESS_ERROR
;
4308 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4310 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4311 ERR_R_INTERNAL_ERROR
);
4312 return MSG_PROCESS_ERROR
;
4315 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4317 return MSG_PROCESS_CONTINUE_READING
;
4321 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
4323 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4325 /* SSLfatal() already called */
4332 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL
*s
, PACKET
*pkt
)
4334 if (PACKET_remaining(pkt
) != 0) {
4335 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4336 SSL_R_LENGTH_MISMATCH
);
4337 return MSG_PROCESS_ERROR
;
4340 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4341 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4342 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4343 ERR_R_INTERNAL_ERROR
);
4344 return MSG_PROCESS_ERROR
;
4348 * EndOfEarlyData signals a key change so the end of the message must be on
4349 * a record boundary.
4351 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4352 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
4353 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4354 SSL_R_NOT_ON_RECORD_BOUNDARY
);
4355 return MSG_PROCESS_ERROR
;
4358 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4359 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
4360 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4361 /* SSLfatal() already called */
4362 return MSG_PROCESS_ERROR
;
4365 return MSG_PROCESS_CONTINUE_READING
;