2 * Copyright 1995-2018 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_locl.h"
14 #include "statem_locl.h"
15 #include "internal/constant_time_locl.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/hmac.h>
22 #include <openssl/x509.h>
23 #include <openssl/dh.h>
24 #include <openssl/bn.h>
25 #include <openssl/md5.h>
26 #include <openssl/trace.h>
28 #define TICKET_NONCE_SIZE 8
30 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
);
33 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
34 * handshake state transitions when a TLSv1.3 server is reading messages from
35 * the client. The message type that the client has sent is provided in |mt|.
36 * The current state is in |s->statem.hand_state|.
38 * Return values are 1 for success (transition allowed) and 0 on error
39 * (transition not allowed)
41 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
43 OSSL_STATEM
*st
= &s
->statem
;
46 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
47 * not negotiated TLSv1.3 yet, so that case is handled by
48 * ossl_statem_server_read_transition()
50 switch (st
->hand_state
) {
54 case TLS_ST_EARLY_DATA
:
55 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
56 if (mt
== SSL3_MT_CLIENT_HELLO
) {
57 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
61 } else if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
62 if (mt
== SSL3_MT_END_OF_EARLY_DATA
) {
63 st
->hand_state
= TLS_ST_SR_END_OF_EARLY_DATA
;
70 case TLS_ST_SR_END_OF_EARLY_DATA
:
71 case TLS_ST_SW_FINISHED
:
72 if (s
->s3
.tmp
.cert_request
) {
73 if (mt
== SSL3_MT_CERTIFICATE
) {
74 st
->hand_state
= TLS_ST_SR_CERT
;
78 if (mt
== SSL3_MT_FINISHED
) {
79 st
->hand_state
= TLS_ST_SR_FINISHED
;
86 if (s
->session
->peer
== NULL
) {
87 if (mt
== SSL3_MT_FINISHED
) {
88 st
->hand_state
= TLS_ST_SR_FINISHED
;
92 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
93 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
99 case TLS_ST_SR_CERT_VRFY
:
100 if (mt
== SSL3_MT_FINISHED
) {
101 st
->hand_state
= TLS_ST_SR_FINISHED
;
108 * Its never ok to start processing handshake messages in the middle of
109 * early data (i.e. before we've received the end of early data alert)
111 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
114 if (mt
== SSL3_MT_CERTIFICATE
115 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
116 st
->hand_state
= TLS_ST_SR_CERT
;
120 if (mt
== SSL3_MT_KEY_UPDATE
) {
121 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
127 /* No valid transition found */
132 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
133 * handshake state transitions when the server is reading messages from the
134 * client. The message type that the client has sent is provided in |mt|. The
135 * current state is in |s->statem.hand_state|.
137 * Return values are 1 for success (transition allowed) and 0 on error
138 * (transition not allowed)
140 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
142 OSSL_STATEM
*st
= &s
->statem
;
144 if (SSL_IS_TLS13(s
)) {
145 if (!ossl_statem_server13_read_transition(s
, mt
))
150 switch (st
->hand_state
) {
156 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
157 if (mt
== SSL3_MT_CLIENT_HELLO
) {
158 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
163 case TLS_ST_SW_SRVR_DONE
:
165 * If we get a CKE message after a ServerDone then either
166 * 1) We didn't request a Certificate
168 * 2) If we did request one then
169 * a) We allow no Certificate to be returned
171 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
172 * list if we requested a certificate)
174 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
175 if (s
->s3
.tmp
.cert_request
) {
176 if (s
->version
== SSL3_VERSION
) {
177 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
178 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
180 * This isn't an unexpected message as such - we're just
181 * not going to accept it because we require a client
184 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
185 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
186 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
189 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
193 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
196 } else if (s
->s3
.tmp
.cert_request
) {
197 if (mt
== SSL3_MT_CERTIFICATE
) {
198 st
->hand_state
= TLS_ST_SR_CERT
;
205 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
206 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
211 case TLS_ST_SR_KEY_EXCH
:
213 * We should only process a CertificateVerify message if we have
214 * received a Certificate from the client. If so then |s->session->peer|
215 * will be non NULL. In some instances a CertificateVerify message is
216 * not required even if the peer has sent a Certificate (e.g. such as in
217 * the case of static DH). In that case |st->no_cert_verify| should be
220 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
221 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
223 * For the ECDH ciphersuites when the client sends its ECDH
224 * pub key in a certificate, the CertificateVerify message is
225 * not sent. Also for GOST ciphersuites when the client uses
226 * its key from the certificate for key exchange.
228 st
->hand_state
= TLS_ST_SR_CHANGE
;
232 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
233 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
239 case TLS_ST_SR_CERT_VRFY
:
240 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
241 st
->hand_state
= TLS_ST_SR_CHANGE
;
246 case TLS_ST_SR_CHANGE
:
247 #ifndef OPENSSL_NO_NEXTPROTONEG
248 if (s
->s3
.npn_seen
) {
249 if (mt
== SSL3_MT_NEXT_PROTO
) {
250 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
255 if (mt
== SSL3_MT_FINISHED
) {
256 st
->hand_state
= TLS_ST_SR_FINISHED
;
259 #ifndef OPENSSL_NO_NEXTPROTONEG
264 #ifndef OPENSSL_NO_NEXTPROTONEG
265 case TLS_ST_SR_NEXT_PROTO
:
266 if (mt
== SSL3_MT_FINISHED
) {
267 st
->hand_state
= TLS_ST_SR_FINISHED
;
273 case TLS_ST_SW_FINISHED
:
274 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
275 st
->hand_state
= TLS_ST_SR_CHANGE
;
282 /* No valid transition found */
283 if (SSL_IS_DTLS(s
) && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
287 * CCS messages don't have a message sequence number so this is probably
288 * because of an out-of-order CCS. We'll just drop it.
291 s
->rwstate
= SSL_READING
;
292 rbio
= SSL_get_rbio(s
);
293 BIO_clear_retry_flags(rbio
);
294 BIO_set_retry_read(rbio
);
297 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
,
298 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
299 SSL_R_UNEXPECTED_MESSAGE
);
304 * Should we send a ServerKeyExchange message?
306 * Valid return values are:
310 static int send_server_key_exchange(SSL
*s
)
312 unsigned long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
315 * only send a ServerKeyExchange if DH or fortezza but we have a
316 * sign only certificate PSK: may send PSK identity hints For
317 * ECC ciphersuites, we send a serverKeyExchange message only if
318 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
319 * the server certificate contains the server's public key for
322 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
324 * PSK: send ServerKeyExchange if PSK identity hint if
327 #ifndef OPENSSL_NO_PSK
328 /* Only send SKE if we have identity hint for plain PSK */
329 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
330 && s
->cert
->psk_identity_hint
)
331 /* For other PSK always send SKE */
332 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
334 #ifndef OPENSSL_NO_SRP
335 /* SRP: send ServerKeyExchange */
336 || (alg_k
& SSL_kSRP
)
346 * Should we send a CertificateRequest message?
348 * Valid return values are:
352 int send_certificate_request(SSL
*s
)
355 /* don't request cert unless asked for it: */
356 s
->verify_mode
& SSL_VERIFY_PEER
358 * don't request if post-handshake-only unless doing
359 * post-handshake in TLSv1.3:
361 && (!SSL_IS_TLS13(s
) || !(s
->verify_mode
& SSL_VERIFY_POST_HANDSHAKE
)
362 || s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
)
364 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
367 && (s
->certreqs_sent
< 1 ||
368 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
370 * never request cert in anonymous ciphersuites (see
371 * section "Certificate request" in SSL 3 drafts and in
374 && (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
376 * ... except when the application insists on
377 * verification (against the specs, but statem_clnt.c accepts
380 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
381 /* don't request certificate for SRP auth */
382 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
384 * With normal PSK Certificates and Certificate Requests
387 && !(s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
395 * ossl_statem_server13_write_transition() works out what handshake state to
396 * move to next when a TLSv1.3 server is writing messages to be sent to the
399 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
401 OSSL_STATEM
*st
= &s
->statem
;
404 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
405 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
408 switch (st
->hand_state
) {
410 /* Shouldn't happen */
411 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
412 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION
,
413 ERR_R_INTERNAL_ERROR
);
414 return WRITE_TRAN_ERROR
;
417 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
418 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
419 return WRITE_TRAN_CONTINUE
;
421 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
422 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
423 return WRITE_TRAN_CONTINUE
;
425 /* Try to read from the client instead */
426 return WRITE_TRAN_FINISHED
;
428 case TLS_ST_SR_CLNT_HELLO
:
429 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
430 return WRITE_TRAN_CONTINUE
;
432 case TLS_ST_SW_SRVR_HELLO
:
433 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
434 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
)
435 st
->hand_state
= TLS_ST_SW_CHANGE
;
436 else if (s
->hello_retry_request
== SSL_HRR_PENDING
)
437 st
->hand_state
= TLS_ST_EARLY_DATA
;
439 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
440 return WRITE_TRAN_CONTINUE
;
442 case TLS_ST_SW_CHANGE
:
443 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
444 st
->hand_state
= TLS_ST_EARLY_DATA
;
446 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
447 return WRITE_TRAN_CONTINUE
;
449 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
451 st
->hand_state
= TLS_ST_SW_FINISHED
;
452 else if (send_certificate_request(s
))
453 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
455 st
->hand_state
= TLS_ST_SW_CERT
;
457 return WRITE_TRAN_CONTINUE
;
459 case TLS_ST_SW_CERT_REQ
:
460 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
461 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
462 st
->hand_state
= TLS_ST_OK
;
464 st
->hand_state
= TLS_ST_SW_CERT
;
466 return WRITE_TRAN_CONTINUE
;
469 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
470 return WRITE_TRAN_CONTINUE
;
472 case TLS_ST_SW_CERT_VRFY
:
473 st
->hand_state
= TLS_ST_SW_FINISHED
;
474 return WRITE_TRAN_CONTINUE
;
476 case TLS_ST_SW_FINISHED
:
477 st
->hand_state
= TLS_ST_EARLY_DATA
;
478 return WRITE_TRAN_CONTINUE
;
480 case TLS_ST_EARLY_DATA
:
481 return WRITE_TRAN_FINISHED
;
483 case TLS_ST_SR_FINISHED
:
485 * Technically we have finished the handshake at this point, but we're
486 * going to remain "in_init" for now and write out any session tickets
489 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
490 s
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
;
491 } else if (!s
->ext
.ticket_expected
) {
493 * If we're not going to renew the ticket then we just finish the
494 * handshake at this point.
496 st
->hand_state
= TLS_ST_OK
;
497 return WRITE_TRAN_CONTINUE
;
499 if (s
->num_tickets
> s
->sent_tickets
)
500 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
502 st
->hand_state
= TLS_ST_OK
;
503 return WRITE_TRAN_CONTINUE
;
505 case TLS_ST_SR_KEY_UPDATE
:
506 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
507 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
508 return WRITE_TRAN_CONTINUE
;
512 case TLS_ST_SW_KEY_UPDATE
:
513 st
->hand_state
= TLS_ST_OK
;
514 return WRITE_TRAN_CONTINUE
;
516 case TLS_ST_SW_SESSION_TICKET
:
517 /* In a resumption we only ever send a maximum of one new ticket.
518 * Following an initial handshake we send the number of tickets we have
519 * been configured for.
521 if (s
->hit
|| s
->num_tickets
<= s
->sent_tickets
) {
522 /* We've written enough tickets out. */
523 st
->hand_state
= TLS_ST_OK
;
525 return WRITE_TRAN_CONTINUE
;
530 * ossl_statem_server_write_transition() works out what handshake state to move
531 * to next when the server is writing messages to be sent to the client.
533 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
535 OSSL_STATEM
*st
= &s
->statem
;
538 * Note that before the ClientHello we don't know what version we are going
539 * to negotiate yet, so we don't take this branch until later
543 return ossl_statem_server13_write_transition(s
);
545 switch (st
->hand_state
) {
547 /* Shouldn't happen */
548 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
549 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION
,
550 ERR_R_INTERNAL_ERROR
);
551 return WRITE_TRAN_ERROR
;
554 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
555 /* We must be trying to renegotiate */
556 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
557 st
->request_state
= TLS_ST_BEFORE
;
558 return WRITE_TRAN_CONTINUE
;
560 /* Must be an incoming ClientHello */
561 if (!tls_setup_handshake(s
)) {
562 /* SSLfatal() already called */
563 return WRITE_TRAN_ERROR
;
568 /* Just go straight to trying to read from the client */
569 return WRITE_TRAN_FINISHED
;
571 case TLS_ST_SW_HELLO_REQ
:
572 st
->hand_state
= TLS_ST_OK
;
573 return WRITE_TRAN_CONTINUE
;
575 case TLS_ST_SR_CLNT_HELLO
:
576 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
577 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
)) {
578 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
579 } else if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
580 /* We must have rejected the renegotiation */
581 st
->hand_state
= TLS_ST_OK
;
582 return WRITE_TRAN_CONTINUE
;
584 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
586 return WRITE_TRAN_CONTINUE
;
588 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
589 return WRITE_TRAN_FINISHED
;
591 case TLS_ST_SW_SRVR_HELLO
:
593 if (s
->ext
.ticket_expected
)
594 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
596 st
->hand_state
= TLS_ST_SW_CHANGE
;
598 /* Check if it is anon DH or anon ECDH, */
599 /* normal PSK or SRP */
600 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
&
601 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
602 st
->hand_state
= TLS_ST_SW_CERT
;
603 } else if (send_server_key_exchange(s
)) {
604 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
605 } else if (send_certificate_request(s
)) {
606 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
608 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
611 return WRITE_TRAN_CONTINUE
;
614 if (s
->ext
.status_expected
) {
615 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
616 return WRITE_TRAN_CONTINUE
;
620 case TLS_ST_SW_CERT_STATUS
:
621 if (send_server_key_exchange(s
)) {
622 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
623 return WRITE_TRAN_CONTINUE
;
627 case TLS_ST_SW_KEY_EXCH
:
628 if (send_certificate_request(s
)) {
629 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
630 return WRITE_TRAN_CONTINUE
;
634 case TLS_ST_SW_CERT_REQ
:
635 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
636 return WRITE_TRAN_CONTINUE
;
638 case TLS_ST_SW_SRVR_DONE
:
639 return WRITE_TRAN_FINISHED
;
641 case TLS_ST_SR_FINISHED
:
643 st
->hand_state
= TLS_ST_OK
;
644 return WRITE_TRAN_CONTINUE
;
645 } else if (s
->ext
.ticket_expected
) {
646 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
648 st
->hand_state
= TLS_ST_SW_CHANGE
;
650 return WRITE_TRAN_CONTINUE
;
652 case TLS_ST_SW_SESSION_TICKET
:
653 st
->hand_state
= TLS_ST_SW_CHANGE
;
654 return WRITE_TRAN_CONTINUE
;
656 case TLS_ST_SW_CHANGE
:
657 st
->hand_state
= TLS_ST_SW_FINISHED
;
658 return WRITE_TRAN_CONTINUE
;
660 case TLS_ST_SW_FINISHED
:
662 return WRITE_TRAN_FINISHED
;
664 st
->hand_state
= TLS_ST_OK
;
665 return WRITE_TRAN_CONTINUE
;
670 * Perform any pre work that needs to be done prior to sending a message from
671 * the server to the client.
673 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
675 OSSL_STATEM
*st
= &s
->statem
;
677 switch (st
->hand_state
) {
679 /* No pre work to be done */
682 case TLS_ST_SW_HELLO_REQ
:
685 dtls1_clear_sent_buffer(s
);
688 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
690 if (SSL_IS_DTLS(s
)) {
691 dtls1_clear_sent_buffer(s
);
692 /* We don't buffer this message so don't use the timer */
697 case TLS_ST_SW_SRVR_HELLO
:
698 if (SSL_IS_DTLS(s
)) {
700 * Messages we write from now on should be buffered and
701 * retransmitted if necessary, so we need to use the timer now
707 case TLS_ST_SW_SRVR_DONE
:
708 #ifndef OPENSSL_NO_SCTP
709 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
710 /* Calls SSLfatal() as required */
711 return dtls_wait_for_dry(s
);
714 return WORK_FINISHED_CONTINUE
;
716 case TLS_ST_SW_SESSION_TICKET
:
717 if (SSL_IS_TLS13(s
) && s
->sent_tickets
== 0) {
719 * Actually this is the end of the handshake, but we're going
720 * straight into writing the session ticket out. So we finish off
721 * the handshake, but keep the various buffers active.
723 * Calls SSLfatal as required.
725 return tls_finish_handshake(s
, wst
, 0, 0);
726 } if (SSL_IS_DTLS(s
)) {
728 * We're into the last flight. We don't retransmit the last flight
729 * unless we need to, so we don't use the timer
735 case TLS_ST_SW_CHANGE
:
738 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
739 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
740 /* SSLfatal() already called */
743 if (SSL_IS_DTLS(s
)) {
745 * We're into the last flight. We don't retransmit the last flight
746 * unless we need to, so we don't use the timer. This might have
747 * already been set to 0 if we sent a NewSessionTicket message,
748 * but we'll set it again here in case we didn't.
752 return WORK_FINISHED_CONTINUE
;
754 case TLS_ST_EARLY_DATA
:
755 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
756 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0)
757 return WORK_FINISHED_CONTINUE
;
761 /* Calls SSLfatal() as required */
762 return tls_finish_handshake(s
, wst
, 1, 1);
765 return WORK_FINISHED_CONTINUE
;
768 static ossl_inline
int conn_is_closed(void)
770 switch (get_last_sys_error()) {
775 #if defined(ECONNRESET)
779 #if defined(WSAECONNRESET)
789 * Perform any work that needs to be done after sending a message from the
790 * server to the client.
792 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
794 OSSL_STATEM
*st
= &s
->statem
;
798 switch (st
->hand_state
) {
800 /* No post work to be done */
803 case TLS_ST_SW_HELLO_REQ
:
804 if (statem_flush(s
) != 1)
806 if (!ssl3_init_finished_mac(s
)) {
807 /* SSLfatal() already called */
812 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
813 if (statem_flush(s
) != 1)
815 /* HelloVerifyRequest resets Finished MAC */
816 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
817 /* SSLfatal() already called */
821 * The next message should be another ClientHello which we need to
822 * treat like it was the first packet
827 case TLS_ST_SW_SRVR_HELLO
:
828 if (SSL_IS_TLS13(s
) && s
->hello_retry_request
== SSL_HRR_PENDING
) {
829 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0
830 && statem_flush(s
) != 1)
834 #ifndef OPENSSL_NO_SCTP
835 if (SSL_IS_DTLS(s
) && s
->hit
) {
836 unsigned char sctpauthkey
[64];
837 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
841 * Add new shared key for SCTP-Auth, will be ignored if no
844 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
845 sizeof(DTLS1_SCTP_AUTH_LABEL
));
847 /* Don't include the terminating zero. */
848 labellen
= sizeof(labelbuffer
) - 1;
849 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
852 if (SSL_export_keying_material(s
, sctpauthkey
,
853 sizeof(sctpauthkey
), labelbuffer
,
856 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
857 SSL_F_OSSL_STATEM_SERVER_POST_WORK
,
858 ERR_R_INTERNAL_ERROR
);
862 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
863 sizeof(sctpauthkey
), sctpauthkey
);
867 || ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
868 && s
->hello_retry_request
!= SSL_HRR_COMPLETE
))
872 case TLS_ST_SW_CHANGE
:
873 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
874 if (!statem_flush(s
))
879 if (SSL_IS_TLS13(s
)) {
880 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
881 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
882 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
883 /* SSLfatal() already called */
887 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
888 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
889 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
)) {
890 /* SSLfatal() already called */
894 * We don't yet know whether the next record we are going to receive
895 * is an unencrypted alert, an encrypted alert, or an encrypted
896 * handshake message. We temporarily tolerate unencrypted alerts.
898 s
->statem
.enc_read_state
= ENC_READ_STATE_ALLOW_PLAIN_ALERTS
;
902 #ifndef OPENSSL_NO_SCTP
903 if (SSL_IS_DTLS(s
) && !s
->hit
) {
905 * Change to new shared key of SCTP-Auth, will be ignored if
908 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
912 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
913 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
915 /* SSLfatal() already called */
920 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
923 case TLS_ST_SW_SRVR_DONE
:
924 if (statem_flush(s
) != 1)
928 case TLS_ST_SW_FINISHED
:
929 if (statem_flush(s
) != 1)
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 (SSL_IS_TLS13(s
)) {
942 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
943 s
->master_secret
, s
->handshake_secret
, 0,
944 &s
->session
->master_key_length
)
945 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
946 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
947 /* SSLfatal() already called */
952 case TLS_ST_SW_CERT_REQ
:
953 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
954 if (statem_flush(s
) != 1)
959 case TLS_ST_SW_KEY_UPDATE
:
960 if (statem_flush(s
) != 1)
962 if (!tls13_update_key(s
, 1)) {
963 /* SSLfatal() already called */
968 case TLS_ST_SW_SESSION_TICKET
:
970 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1) {
971 if (SSL_get_error(s
, 0) == SSL_ERROR_SYSCALL
972 && conn_is_closed()) {
974 * We ignore connection closed errors in TLSv1.3 when sending a
975 * NewSessionTicket and behave as if we were successful. This is
976 * so that we are still able to read data sent to us by a client
977 * that closes soon after the end of the handshake without
978 * waiting to read our post-handshake NewSessionTickets.
980 s
->rwstate
= SSL_NOTHING
;
989 return WORK_FINISHED_CONTINUE
;
993 * Get the message construction function and message type for sending from the
996 * Valid return values are:
1000 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
1001 confunc_f
*confunc
, int *mt
)
1003 OSSL_STATEM
*st
= &s
->statem
;
1005 switch (st
->hand_state
) {
1007 /* Shouldn't happen */
1008 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1009 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE
,
1010 SSL_R_BAD_HANDSHAKE_STATE
);
1013 case TLS_ST_SW_CHANGE
:
1015 *confunc
= dtls_construct_change_cipher_spec
;
1017 *confunc
= tls_construct_change_cipher_spec
;
1018 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1021 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
1022 *confunc
= dtls_construct_hello_verify_request
;
1023 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
1026 case TLS_ST_SW_HELLO_REQ
:
1027 /* No construction function needed */
1029 *mt
= SSL3_MT_HELLO_REQUEST
;
1032 case TLS_ST_SW_SRVR_HELLO
:
1033 *confunc
= tls_construct_server_hello
;
1034 *mt
= SSL3_MT_SERVER_HELLO
;
1037 case TLS_ST_SW_CERT
:
1038 *confunc
= tls_construct_server_certificate
;
1039 *mt
= SSL3_MT_CERTIFICATE
;
1042 case TLS_ST_SW_CERT_VRFY
:
1043 *confunc
= tls_construct_cert_verify
;
1044 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
1048 case TLS_ST_SW_KEY_EXCH
:
1049 *confunc
= tls_construct_server_key_exchange
;
1050 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
1053 case TLS_ST_SW_CERT_REQ
:
1054 *confunc
= tls_construct_certificate_request
;
1055 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
1058 case TLS_ST_SW_SRVR_DONE
:
1059 *confunc
= tls_construct_server_done
;
1060 *mt
= SSL3_MT_SERVER_DONE
;
1063 case TLS_ST_SW_SESSION_TICKET
:
1064 *confunc
= tls_construct_new_session_ticket
;
1065 *mt
= SSL3_MT_NEWSESSION_TICKET
;
1068 case TLS_ST_SW_CERT_STATUS
:
1069 *confunc
= tls_construct_cert_status
;
1070 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
1073 case TLS_ST_SW_FINISHED
:
1074 *confunc
= tls_construct_finished
;
1075 *mt
= SSL3_MT_FINISHED
;
1078 case TLS_ST_EARLY_DATA
:
1080 *mt
= SSL3_MT_DUMMY
;
1083 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
1084 *confunc
= tls_construct_encrypted_extensions
;
1085 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
1088 case TLS_ST_SW_KEY_UPDATE
:
1089 *confunc
= tls_construct_key_update
;
1090 *mt
= SSL3_MT_KEY_UPDATE
;
1098 * Maximum size (excluding the Handshake header) of a ClientHello message,
1099 * calculated as follows:
1101 * 2 + # client_version
1102 * 32 + # only valid length for random
1103 * 1 + # length of session_id
1104 * 32 + # maximum size for session_id
1105 * 2 + # length of cipher suites
1106 * 2^16-2 + # maximum length of cipher suites array
1107 * 1 + # length of compression_methods
1108 * 2^8-1 + # maximum length of compression methods
1109 * 2 + # length of extensions
1110 * 2^16-1 # maximum length of extensions
1112 #define CLIENT_HELLO_MAX_LENGTH 131396
1114 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1115 #define NEXT_PROTO_MAX_LENGTH 514
1118 * Returns the maximum allowed length for the current message that we are
1119 * reading. Excludes the message header.
1121 size_t ossl_statem_server_max_message_size(SSL
*s
)
1123 OSSL_STATEM
*st
= &s
->statem
;
1125 switch (st
->hand_state
) {
1127 /* Shouldn't happen */
1130 case TLS_ST_SR_CLNT_HELLO
:
1131 return CLIENT_HELLO_MAX_LENGTH
;
1133 case TLS_ST_SR_END_OF_EARLY_DATA
:
1134 return END_OF_EARLY_DATA_MAX_LENGTH
;
1136 case TLS_ST_SR_CERT
:
1137 return s
->max_cert_list
;
1139 case TLS_ST_SR_KEY_EXCH
:
1140 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1142 case TLS_ST_SR_CERT_VRFY
:
1143 return SSL3_RT_MAX_PLAIN_LENGTH
;
1145 #ifndef OPENSSL_NO_NEXTPROTONEG
1146 case TLS_ST_SR_NEXT_PROTO
:
1147 return NEXT_PROTO_MAX_LENGTH
;
1150 case TLS_ST_SR_CHANGE
:
1151 return CCS_MAX_LENGTH
;
1153 case TLS_ST_SR_FINISHED
:
1154 return FINISHED_MAX_LENGTH
;
1156 case TLS_ST_SR_KEY_UPDATE
:
1157 return KEY_UPDATE_MAX_LENGTH
;
1162 * Process a message that the server has received from the client.
1164 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1166 OSSL_STATEM
*st
= &s
->statem
;
1168 switch (st
->hand_state
) {
1170 /* Shouldn't happen */
1171 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1172 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE
,
1173 ERR_R_INTERNAL_ERROR
);
1174 return MSG_PROCESS_ERROR
;
1176 case TLS_ST_SR_CLNT_HELLO
:
1177 return tls_process_client_hello(s
, pkt
);
1179 case TLS_ST_SR_END_OF_EARLY_DATA
:
1180 return tls_process_end_of_early_data(s
, pkt
);
1182 case TLS_ST_SR_CERT
:
1183 return tls_process_client_certificate(s
, pkt
);
1185 case TLS_ST_SR_KEY_EXCH
:
1186 return tls_process_client_key_exchange(s
, pkt
);
1188 case TLS_ST_SR_CERT_VRFY
:
1189 return tls_process_cert_verify(s
, pkt
);
1191 #ifndef OPENSSL_NO_NEXTPROTONEG
1192 case TLS_ST_SR_NEXT_PROTO
:
1193 return tls_process_next_proto(s
, pkt
);
1196 case TLS_ST_SR_CHANGE
:
1197 return tls_process_change_cipher_spec(s
, pkt
);
1199 case TLS_ST_SR_FINISHED
:
1200 return tls_process_finished(s
, pkt
);
1202 case TLS_ST_SR_KEY_UPDATE
:
1203 return tls_process_key_update(s
, pkt
);
1209 * Perform any further processing required following the receipt of a message
1212 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1214 OSSL_STATEM
*st
= &s
->statem
;
1216 switch (st
->hand_state
) {
1218 /* Shouldn't happen */
1219 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1220 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE
,
1221 ERR_R_INTERNAL_ERROR
);
1224 case TLS_ST_SR_CLNT_HELLO
:
1225 return tls_post_process_client_hello(s
, wst
);
1227 case TLS_ST_SR_KEY_EXCH
:
1228 return tls_post_process_client_key_exchange(s
, wst
);
1232 #ifndef OPENSSL_NO_SRP
1233 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1234 static int ssl_check_srp_ext_ClientHello(SSL
*s
)
1237 int al
= SSL_AD_UNRECOGNIZED_NAME
;
1239 if ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1240 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1241 if (s
->srp_ctx
.login
== NULL
) {
1243 * RFC 5054 says SHOULD reject, we do so if There is no srp
1246 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
1247 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1248 SSL_R_PSK_IDENTITY_NOT_FOUND
);
1251 ret
= SSL_srp_server_param_with_username(s
, &al
);
1254 if (ret
== SSL3_AL_FATAL
) {
1255 SSLfatal(s
, al
, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO
,
1256 al
== SSL_AD_UNKNOWN_PSK_IDENTITY
1257 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1258 : SSL_R_CLIENTHELLO_TLSEXT
);
1267 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1270 /* Always use DTLS 1.0 version: see RFC 6347 */
1271 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1272 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1278 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1280 unsigned int cookie_leni
;
1281 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1282 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1283 &cookie_leni
) == 0 ||
1284 cookie_leni
> 255) {
1285 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1286 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1289 s
->d1
->cookie_len
= cookie_leni
;
1291 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1292 s
->d1
->cookie_len
)) {
1293 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1294 ERR_R_INTERNAL_ERROR
);
1301 #ifndef OPENSSL_NO_EC
1303 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1304 * SecureTransport using the TLS extension block in |hello|.
1305 * Safari, since 10.6, sends exactly these extensions, in this order:
1309 * signature_algorithms (for TLSv1.2 only)
1311 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1312 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1313 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1314 * 10.8..10.8.3 (which don't work).
1316 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1318 static const unsigned char kSafariExtensionsBlock
[] = {
1319 0x00, 0x0a, /* elliptic_curves extension */
1320 0x00, 0x08, /* 8 bytes */
1321 0x00, 0x06, /* 6 bytes of curve ids */
1322 0x00, 0x17, /* P-256 */
1323 0x00, 0x18, /* P-384 */
1324 0x00, 0x19, /* P-521 */
1326 0x00, 0x0b, /* ec_point_formats */
1327 0x00, 0x02, /* 2 bytes */
1328 0x01, /* 1 point format */
1329 0x00, /* uncompressed */
1330 /* The following is only present in TLS 1.2 */
1331 0x00, 0x0d, /* signature_algorithms */
1332 0x00, 0x0c, /* 12 bytes */
1333 0x00, 0x0a, /* 10 bytes */
1334 0x05, 0x01, /* SHA-384/RSA */
1335 0x04, 0x01, /* SHA-256/RSA */
1336 0x02, 0x01, /* SHA-1/RSA */
1337 0x04, 0x03, /* SHA-256/ECDSA */
1338 0x02, 0x03, /* SHA-1/ECDSA */
1340 /* Length of the common prefix (first two extensions). */
1341 static const size_t kSafariCommonExtensionsLength
= 18;
1346 tmppkt
= hello
->extensions
;
1348 if (!PACKET_forward(&tmppkt
, 2)
1349 || !PACKET_get_net_2(&tmppkt
, &type
)
1350 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1354 if (type
!= TLSEXT_TYPE_server_name
)
1357 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1358 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1360 s
->s3
.is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1363 #endif /* !OPENSSL_NO_EC */
1365 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1367 /* |cookie| will only be initialized for DTLS. */
1368 PACKET session_id
, compression
, extensions
, cookie
;
1369 static const unsigned char null_compression
= 0;
1370 CLIENTHELLO_MSG
*clienthello
= NULL
;
1372 /* Check if this is actually an unexpected renegotiation ClientHello */
1373 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1374 if (!ossl_assert(!SSL_IS_TLS13(s
))) {
1375 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1376 ERR_R_INTERNAL_ERROR
);
1379 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
) != 0
1380 || (!s
->s3
.send_connection_binding
1382 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
) == 0)) {
1383 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1384 return MSG_PROCESS_FINISHED_READING
;
1390 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1391 if (clienthello
== NULL
) {
1392 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1393 ERR_R_INTERNAL_ERROR
);
1398 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1400 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1401 PACKET_null_init(&cookie
);
1403 if (clienthello
->isv2
) {
1406 if (!SSL_IS_FIRST_HANDSHAKE(s
)
1407 || s
->hello_retry_request
!= SSL_HRR_NONE
) {
1408 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1409 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1414 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1415 * header is sent directly on the wire, not wrapped as a TLS
1416 * record. Our record layer just processes the message length and passes
1417 * the rest right through. Its format is:
1419 * 0-1 msg_length - decoded by the record layer
1420 * 2 msg_type - s->init_msg points here
1422 * 5-6 cipher_spec_length
1423 * 7-8 session_id_length
1424 * 9-10 challenge_length
1428 if (!PACKET_get_1(pkt
, &mt
)
1429 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1431 * Should never happen. We should have tested this in the record
1432 * layer in order to have determined that this is a SSLv2 record
1433 * in the first place
1435 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1436 ERR_R_INTERNAL_ERROR
);
1441 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1442 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1443 SSL_R_LENGTH_TOO_SHORT
);
1447 /* Parse the message and load client random. */
1448 if (clienthello
->isv2
) {
1450 * Handle an SSLv2 backwards compatible ClientHello
1451 * Note, this is only for SSLv3+ using the backward compatible format.
1452 * Real SSLv2 is not supported, and is rejected below.
1454 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1457 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1458 || !PACKET_get_net_2(pkt
, &session_id_len
)
1459 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1460 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1461 SSL_R_RECORD_LENGTH_MISMATCH
);
1465 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1466 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1467 SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1471 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1473 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1474 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1475 /* No extensions. */
1476 || PACKET_remaining(pkt
) != 0) {
1477 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1478 SSL_R_RECORD_LENGTH_MISMATCH
);
1481 clienthello
->session_id_len
= session_id_len
;
1483 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1484 * here rather than sizeof(clienthello->random) because that is the limit
1485 * for SSLv3 and it is fixed. It won't change even if
1486 * sizeof(clienthello->random) does.
1488 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1489 ? SSL3_RANDOM_SIZE
: challenge_len
;
1490 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1491 if (!PACKET_copy_bytes(&challenge
,
1492 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1493 challenge_len
, challenge_len
)
1494 /* Advertise only null compression. */
1495 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1496 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1497 ERR_R_INTERNAL_ERROR
);
1501 PACKET_null_init(&clienthello
->extensions
);
1503 /* Regular ClientHello. */
1504 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1505 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1506 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1507 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1508 &clienthello
->session_id_len
)) {
1509 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1510 SSL_R_LENGTH_MISMATCH
);
1514 if (SSL_IS_DTLS(s
)) {
1515 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1516 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1517 SSL_R_LENGTH_MISMATCH
);
1520 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1521 DTLS1_COOKIE_LENGTH
,
1522 &clienthello
->dtls_cookie_len
)) {
1523 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1524 SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1528 * If we require cookies and this ClientHello doesn't contain one,
1529 * just return since we do not want to allocate any memory yet.
1530 * So check cookie length...
1532 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1533 if (clienthello
->dtls_cookie_len
== 0) {
1534 OPENSSL_free(clienthello
);
1535 return MSG_PROCESS_FINISHED_READING
;
1540 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1541 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1542 SSL_R_LENGTH_MISMATCH
);
1546 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1547 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1548 SSL_R_LENGTH_MISMATCH
);
1552 /* Could be empty. */
1553 if (PACKET_remaining(pkt
) == 0) {
1554 PACKET_null_init(&clienthello
->extensions
);
1556 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)
1557 || PACKET_remaining(pkt
) != 0) {
1558 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1559 SSL_R_LENGTH_MISMATCH
);
1565 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1566 MAX_COMPRESSIONS_SIZE
,
1567 &clienthello
->compressions_len
)) {
1568 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1569 ERR_R_INTERNAL_ERROR
);
1573 /* Preserve the raw extensions PACKET for later use */
1574 extensions
= clienthello
->extensions
;
1575 if (!tls_collect_extensions(s
, &extensions
, SSL_EXT_CLIENT_HELLO
,
1576 &clienthello
->pre_proc_exts
,
1577 &clienthello
->pre_proc_exts_len
, 1)) {
1578 /* SSLfatal already been called */
1581 s
->clienthello
= clienthello
;
1583 return MSG_PROCESS_CONTINUE_PROCESSING
;
1586 if (clienthello
!= NULL
)
1587 OPENSSL_free(clienthello
->pre_proc_exts
);
1588 OPENSSL_free(clienthello
);
1590 return MSG_PROCESS_ERROR
;
1593 static int tls_early_post_process_client_hello(SSL
*s
)
1596 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1600 #ifndef OPENSSL_NO_COMP
1601 SSL_COMP
*comp
= NULL
;
1603 const SSL_CIPHER
*c
;
1604 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1605 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1606 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1607 DOWNGRADE dgrd
= DOWNGRADE_NONE
;
1609 /* Finished parsing the ClientHello, now we can start processing it */
1610 /* Give the ClientHello callback a crack at things */
1611 if (s
->ctx
->client_hello_cb
!= NULL
) {
1612 /* A failure in the ClientHello callback terminates the connection. */
1613 switch (s
->ctx
->client_hello_cb(s
, &al
, s
->ctx
->client_hello_cb_arg
)) {
1614 case SSL_CLIENT_HELLO_SUCCESS
:
1616 case SSL_CLIENT_HELLO_RETRY
:
1617 s
->rwstate
= SSL_CLIENT_HELLO_CB
;
1619 case SSL_CLIENT_HELLO_ERROR
:
1622 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1623 SSL_R_CALLBACK_FAILED
);
1628 /* Set up the client_random */
1629 memcpy(s
->s3
.client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1631 /* Choose the version */
1633 if (clienthello
->isv2
) {
1634 if (clienthello
->legacy_version
== SSL2_VERSION
1635 || (clienthello
->legacy_version
& 0xff00)
1636 != (SSL3_VERSION_MAJOR
<< 8)) {
1638 * This is real SSLv2 or something completely unknown. We don't
1641 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1642 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1643 SSL_R_UNKNOWN_PROTOCOL
);
1647 s
->client_version
= clienthello
->legacy_version
;
1650 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1651 * versions are potentially compatible. Version negotiation comes later.
1653 if (!SSL_IS_DTLS(s
)) {
1654 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1655 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1656 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1657 protverr
= SSL_R_VERSION_TOO_LOW
;
1663 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1664 /* like ssl3_get_record, send alert using remote version number */
1665 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1667 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1668 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1672 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1673 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1674 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1675 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1676 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1680 if (SSL_IS_DTLS(s
)) {
1681 /* Empty cookie was already handled above by returning early. */
1682 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1683 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1684 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1685 clienthello
->dtls_cookie_len
) == 0) {
1686 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1687 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1688 SSL_R_COOKIE_MISMATCH
);
1690 /* else cookie verification succeeded */
1692 /* default verification */
1693 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1694 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1695 s
->d1
->cookie_len
) != 0) {
1696 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1697 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1698 SSL_R_COOKIE_MISMATCH
);
1701 s
->d1
->cookie_verified
= 1;
1703 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1704 protverr
= ssl_choose_server_version(s
, clienthello
, &dgrd
);
1705 if (protverr
!= 0) {
1706 s
->version
= s
->client_version
;
1707 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1708 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1716 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1717 clienthello
->isv2
) ||
1718 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1719 clienthello
->isv2
, 1)) {
1720 /* SSLfatal() already called */
1724 s
->s3
.send_connection_binding
= 0;
1725 /* Check what signalling cipher-suite values were received. */
1726 if (scsvs
!= NULL
) {
1727 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1728 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1729 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1730 if (s
->renegotiate
) {
1731 /* SCSV is fatal if renegotiating */
1732 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1733 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1734 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1737 s
->s3
.send_connection_binding
= 1;
1738 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1739 !ssl_check_version_downgrade(s
)) {
1741 * This SCSV indicates that the client previously tried
1742 * a higher version. We should fail if the current version
1743 * is an unexpected downgrade, as that indicates that the first
1744 * connection may have been tampered with in order to trigger
1745 * an insecure downgrade.
1747 SSLfatal(s
, SSL_AD_INAPPROPRIATE_FALLBACK
,
1748 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1749 SSL_R_INAPPROPRIATE_FALLBACK
);
1755 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1756 if (SSL_IS_TLS13(s
)) {
1757 const SSL_CIPHER
*cipher
=
1758 ssl3_choose_cipher(s
, ciphers
, SSL_get_ciphers(s
));
1760 if (cipher
== NULL
) {
1761 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1762 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1763 SSL_R_NO_SHARED_CIPHER
);
1766 if (s
->hello_retry_request
== SSL_HRR_PENDING
1767 && (s
->s3
.tmp
.new_cipher
== NULL
1768 || s
->s3
.tmp
.new_cipher
->id
!= cipher
->id
)) {
1770 * A previous HRR picked a different ciphersuite to the one we
1771 * just selected. Something must have changed.
1773 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1774 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1778 s
->s3
.tmp
.new_cipher
= cipher
;
1781 /* We need to do this before getting the session */
1782 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1783 SSL_EXT_CLIENT_HELLO
,
1784 clienthello
->pre_proc_exts
, NULL
, 0)) {
1785 /* SSLfatal() already called */
1790 * We don't allow resumption in a backwards compatible ClientHello.
1791 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1793 * Versions before 0.9.7 always allow clients to resume sessions in
1794 * renegotiation. 0.9.7 and later allow this by default, but optionally
1795 * ignore resumption requests with flag
1796 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1797 * than a change to default behavior so that applications relying on
1798 * this for security won't even compile against older library versions).
1799 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1800 * request renegotiation but not a new session (s->new_session remains
1801 * unset): for servers, this essentially just means that the
1802 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1805 if (clienthello
->isv2
||
1807 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1808 if (!ssl_get_new_session(s
, 1)) {
1809 /* SSLfatal() already called */
1813 i
= ssl_get_prev_session(s
, clienthello
);
1815 /* previous session */
1817 } else if (i
== -1) {
1818 /* SSLfatal() already called */
1822 if (!ssl_get_new_session(s
, 1)) {
1823 /* SSLfatal() already called */
1829 if (SSL_IS_TLS13(s
)) {
1830 memcpy(s
->tmp_session_id
, s
->clienthello
->session_id
,
1831 s
->clienthello
->session_id_len
);
1832 s
->tmp_session_id_len
= s
->clienthello
->session_id_len
;
1836 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1837 * ciphersuite compatibility with the session as part of resumption.
1839 if (!SSL_IS_TLS13(s
) && s
->hit
) {
1841 id
= s
->session
->cipher
->id
;
1843 OSSL_TRACE_BEGIN(TLS_CIPHER
) {
1844 BIO_printf(trc_out
, "client sent %d ciphers\n",
1845 sk_SSL_CIPHER_num(ciphers
));
1847 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1848 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1849 if (trc_out
!= NULL
)
1850 BIO_printf(trc_out
, "client [%2d of %2d]:%s\n", i
,
1851 sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1859 * we need to have the cipher in the cipher list if we are asked
1862 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1863 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1864 SSL_R_REQUIRED_CIPHER_MISSING
);
1865 OSSL_TRACE_CANCEL(TLS_CIPHER
);
1868 OSSL_TRACE_END(TLS_CIPHER
);
1871 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1872 if (clienthello
->compressions
[loop
] == 0)
1876 if (loop
>= clienthello
->compressions_len
) {
1878 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1879 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1880 SSL_R_NO_COMPRESSION_SPECIFIED
);
1884 #ifndef OPENSSL_NO_EC
1885 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1886 ssl_check_for_safari(s
, clienthello
);
1887 #endif /* !OPENSSL_NO_EC */
1889 /* TLS extensions */
1890 if (!tls_parse_all_extensions(s
, SSL_EXT_CLIENT_HELLO
,
1891 clienthello
->pre_proc_exts
, NULL
, 0, 1)) {
1892 /* SSLfatal() already called */
1897 * Check if we want to use external pre-shared secret for this handshake
1898 * for not reused session only. We need to generate server_random before
1899 * calling tls_session_secret_cb in order to allow SessionTicket
1900 * processing to use it in key derivation.
1904 pos
= s
->s3
.server_random
;
1905 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
, dgrd
) <= 0) {
1906 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1907 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1908 ERR_R_INTERNAL_ERROR
);
1914 && s
->version
>= TLS1_VERSION
1917 && s
->ext
.session_secret_cb
) {
1918 const SSL_CIPHER
*pref_cipher
= NULL
;
1920 * s->session->master_key_length is a size_t, but this is an int for
1921 * backwards compat reasons
1923 int master_key_length
;
1925 master_key_length
= sizeof(s
->session
->master_key
);
1926 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1927 &master_key_length
, ciphers
,
1929 s
->ext
.session_secret_cb_arg
)
1930 && master_key_length
> 0) {
1931 s
->session
->master_key_length
= master_key_length
;
1933 s
->session
->ciphers
= ciphers
;
1934 s
->session
->verify_result
= X509_V_OK
;
1938 /* check if some cipher was preferred by call back */
1939 if (pref_cipher
== NULL
)
1940 pref_cipher
= ssl3_choose_cipher(s
, s
->session
->ciphers
,
1941 SSL_get_ciphers(s
));
1942 if (pref_cipher
== NULL
) {
1943 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1944 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1945 SSL_R_NO_SHARED_CIPHER
);
1949 s
->session
->cipher
= pref_cipher
;
1950 sk_SSL_CIPHER_free(s
->cipher_list
);
1951 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1952 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1953 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1958 * Worst case, we will use the NULL compression, but if we have other
1959 * options, we will now look for them. We have complen-1 compression
1960 * algorithms from the client, starting at q.
1962 s
->s3
.tmp
.new_compression
= NULL
;
1963 if (SSL_IS_TLS13(s
)) {
1965 * We already checked above that the NULL compression method appears in
1966 * the list. Now we check there aren't any others (which is illegal in
1967 * a TLSv1.3 ClientHello.
1969 if (clienthello
->compressions_len
!= 1) {
1970 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1971 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1972 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1976 #ifndef OPENSSL_NO_COMP
1977 /* This only happens if we have a cache hit */
1978 else if (s
->session
->compress_meth
!= 0) {
1979 int m
, comp_id
= s
->session
->compress_meth
;
1981 /* Perform sanity checks on resumed compression algorithm */
1982 /* Can't disable compression */
1983 if (!ssl_allow_compression(s
)) {
1984 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1985 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1986 SSL_R_INCONSISTENT_COMPRESSION
);
1989 /* Look for resumed compression method */
1990 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
1991 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1992 if (comp_id
== comp
->id
) {
1993 s
->s3
.tmp
.new_compression
= comp
;
1997 if (s
->s3
.tmp
.new_compression
== NULL
) {
1998 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
1999 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2000 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
2003 /* Look for resumed method in compression list */
2004 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
2005 if (clienthello
->compressions
[k
] == comp_id
)
2008 if (k
>= clienthello
->compressions_len
) {
2009 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2010 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2011 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
2014 } else if (s
->hit
) {
2016 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
) {
2017 /* See if we have a match */
2018 int m
, nn
, v
, done
= 0;
2021 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
2022 for (m
= 0; m
< nn
; m
++) {
2023 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
2025 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
2026 if (v
== clienthello
->compressions
[o
]) {
2035 s
->s3
.tmp
.new_compression
= comp
;
2041 * If compression is disabled we'd better not try to resume a session
2042 * using compression.
2044 if (s
->session
->compress_meth
!= 0) {
2045 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2046 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2047 SSL_R_INCONSISTENT_COMPRESSION
);
2053 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
2056 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2057 sk_SSL_CIPHER_free(s
->session
->ciphers
);
2058 s
->session
->ciphers
= ciphers
;
2059 if (ciphers
== NULL
) {
2060 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2061 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
2062 ERR_R_INTERNAL_ERROR
);
2069 #ifdef OPENSSL_NO_COMP
2070 s
->session
->compress_meth
= 0;
2072 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
2076 sk_SSL_CIPHER_free(ciphers
);
2077 sk_SSL_CIPHER_free(scsvs
);
2078 OPENSSL_free(clienthello
->pre_proc_exts
);
2079 OPENSSL_free(s
->clienthello
);
2080 s
->clienthello
= NULL
;
2083 sk_SSL_CIPHER_free(ciphers
);
2084 sk_SSL_CIPHER_free(scsvs
);
2085 OPENSSL_free(clienthello
->pre_proc_exts
);
2086 OPENSSL_free(s
->clienthello
);
2087 s
->clienthello
= NULL
;
2093 * Call the status request callback if needed. Upon success, returns 1.
2094 * Upon failure, returns 0.
2096 static int tls_handle_status_request(SSL
*s
)
2098 s
->ext
.status_expected
= 0;
2101 * If status request then ask callback what to do. Note: this must be
2102 * called after servername callbacks in case the certificate has changed,
2103 * and must be called after the cipher has been chosen because this may
2104 * influence which certificate is sent
2106 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
2107 && s
->ctx
->ext
.status_cb
!= NULL
) {
2110 /* If no certificate can't return certificate status */
2111 if (s
->s3
.tmp
.cert
!= NULL
) {
2113 * Set current certificate to one we will use so SSL_get_certificate
2114 * et al can pick it up.
2116 s
->cert
->key
= s
->s3
.tmp
.cert
;
2117 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
2119 /* We don't want to send a status request response */
2120 case SSL_TLSEXT_ERR_NOACK
:
2121 s
->ext
.status_expected
= 0;
2123 /* status request response should be sent */
2124 case SSL_TLSEXT_ERR_OK
:
2125 if (s
->ext
.ocsp
.resp
)
2126 s
->ext
.status_expected
= 1;
2128 /* something bad happened */
2129 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2131 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2132 SSL_F_TLS_HANDLE_STATUS_REQUEST
,
2133 SSL_R_CLIENTHELLO_TLSEXT
);
2143 * Call the alpn_select callback if needed. Upon success, returns 1.
2144 * Upon failure, returns 0.
2146 int tls_handle_alpn(SSL
*s
)
2148 const unsigned char *selected
= NULL
;
2149 unsigned char selected_len
= 0;
2151 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
.alpn_proposed
!= NULL
) {
2152 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
2153 s
->s3
.alpn_proposed
,
2154 (unsigned int)s
->s3
.alpn_proposed_len
,
2155 s
->ctx
->ext
.alpn_select_cb_arg
);
2157 if (r
== SSL_TLSEXT_ERR_OK
) {
2158 OPENSSL_free(s
->s3
.alpn_selected
);
2159 s
->s3
.alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
2160 if (s
->s3
.alpn_selected
== NULL
) {
2161 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_HANDLE_ALPN
,
2162 ERR_R_INTERNAL_ERROR
);
2165 s
->s3
.alpn_selected_len
= selected_len
;
2166 #ifndef OPENSSL_NO_NEXTPROTONEG
2167 /* ALPN takes precedence over NPN. */
2171 /* Check ALPN is consistent with session */
2172 if (s
->session
->ext
.alpn_selected
== NULL
2173 || selected_len
!= s
->session
->ext
.alpn_selected_len
2174 || memcmp(selected
, s
->session
->ext
.alpn_selected
,
2175 selected_len
) != 0) {
2176 /* Not consistent so can't be used for early_data */
2177 s
->ext
.early_data_ok
= 0;
2181 * This is a new session and so alpn_selected should have
2182 * been initialised to NULL. We should update it with the
2185 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
2186 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2187 SSL_F_TLS_HANDLE_ALPN
,
2188 ERR_R_INTERNAL_ERROR
);
2191 s
->session
->ext
.alpn_selected
= OPENSSL_memdup(selected
,
2193 if (s
->session
->ext
.alpn_selected
== NULL
) {
2194 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2195 SSL_F_TLS_HANDLE_ALPN
,
2196 ERR_R_INTERNAL_ERROR
);
2199 s
->session
->ext
.alpn_selected_len
= selected_len
;
2204 } else if (r
!= SSL_TLSEXT_ERR_NOACK
) {
2205 SSLfatal(s
, SSL_AD_NO_APPLICATION_PROTOCOL
, SSL_F_TLS_HANDLE_ALPN
,
2206 SSL_R_NO_APPLICATION_PROTOCOL
);
2210 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2215 /* Check ALPN is consistent with session */
2216 if (s
->session
->ext
.alpn_selected
!= NULL
) {
2217 /* Not consistent so can't be used for early_data */
2218 s
->ext
.early_data_ok
= 0;
2224 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
2226 const SSL_CIPHER
*cipher
;
2228 if (wst
== WORK_MORE_A
) {
2229 int rv
= tls_early_post_process_client_hello(s
);
2231 /* SSLfatal() was already called */
2238 if (wst
== WORK_MORE_B
) {
2239 if (!s
->hit
|| SSL_IS_TLS13(s
)) {
2240 /* Let cert callback update server certificates if required */
2242 if (s
->cert
->cert_cb
!= NULL
) {
2243 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
2245 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2246 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2247 SSL_R_CERT_CB_ERROR
);
2251 s
->rwstate
= SSL_X509_LOOKUP
;
2254 s
->rwstate
= SSL_NOTHING
;
2256 if (!tls1_set_server_sigalgs(s
)) {
2257 /* SSLfatal already called */
2262 /* In TLSv1.3 we selected the ciphersuite before resumption */
2263 if (!SSL_IS_TLS13(s
)) {
2265 ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
2267 if (cipher
== NULL
) {
2268 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2269 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2270 SSL_R_NO_SHARED_CIPHER
);
2273 s
->s3
.tmp
.new_cipher
= cipher
;
2276 if (!tls_choose_sigalg(s
, 1)) {
2277 /* SSLfatal already called */
2280 /* check whether we should disable session resumption */
2281 if (s
->not_resumable_session_cb
!= NULL
)
2282 s
->session
->not_resumable
=
2283 s
->not_resumable_session_cb(s
,
2284 ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
2285 & (SSL_kDHE
| SSL_kECDHE
)) != 0));
2286 if (s
->session
->not_resumable
)
2287 /* do not send a session ticket */
2288 s
->ext
.ticket_expected
= 0;
2291 /* Session-id reuse */
2292 s
->s3
.tmp
.new_cipher
= s
->session
->cipher
;
2296 * we now have the following setup.
2298 * cipher_list - our preferred list of ciphers
2299 * ciphers - the clients preferred list of ciphers
2300 * compression - basically ignored right now
2301 * ssl version is set - sslv3
2302 * s->session - The ssl session has been setup.
2303 * s->hit - session reuse flag
2304 * s->s3.tmp.new_cipher - the new cipher to use.
2308 * Call status_request callback if needed. Has to be done after the
2309 * certificate callbacks etc above.
2311 if (!tls_handle_status_request(s
)) {
2312 /* SSLfatal() already called */
2316 * Call alpn_select callback if needed. Has to be done after SNI and
2317 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2318 * we already did this because cipher negotiation happens earlier, and
2319 * we must handle ALPN before we decide whether to accept early_data.
2321 if (!SSL_IS_TLS13(s
) && !tls_handle_alpn(s
)) {
2322 /* SSLfatal() already called */
2328 #ifndef OPENSSL_NO_SRP
2329 if (wst
== WORK_MORE_C
) {
2331 if ((ret
= ssl_check_srp_ext_ClientHello(s
)) == 0) {
2333 * callback indicates further work to be done
2335 s
->rwstate
= SSL_X509_LOOKUP
;
2339 /* SSLfatal() already called */
2345 return WORK_FINISHED_STOP
;
2350 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2355 unsigned char *session_id
;
2356 int usetls13
= SSL_IS_TLS13(s
) || s
->hello_retry_request
== SSL_HRR_PENDING
;
2358 version
= usetls13
? TLS1_2_VERSION
: s
->version
;
2359 if (!WPACKET_put_bytes_u16(pkt
, version
)
2361 * Random stuff. Filling of the server_random takes place in
2362 * tls_process_client_hello()
2364 || !WPACKET_memcpy(pkt
,
2365 s
->hello_retry_request
== SSL_HRR_PENDING
2366 ? hrrrandom
: s
->s3
.server_random
,
2367 SSL3_RANDOM_SIZE
)) {
2368 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2369 ERR_R_INTERNAL_ERROR
);
2374 * There are several cases for the session ID to send
2375 * back in the server hello:
2376 * - For session reuse from the session cache,
2377 * we send back the old session ID.
2378 * - If stateless session reuse (using a session ticket)
2379 * is successful, we send back the client's "session ID"
2380 * (which doesn't actually identify the session).
2381 * - If it is a new session, we send back the new
2383 * - However, if we want the new session to be single-use,
2384 * we send back a 0-length session ID.
2385 * - In TLSv1.3 we echo back the session id sent to us by the client
2387 * s->hit is non-zero in either case of session reuse,
2388 * so the following won't overwrite an ID that we're supposed
2391 if (s
->session
->not_resumable
||
2392 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2394 s
->session
->session_id_length
= 0;
2397 sl
= s
->tmp_session_id_len
;
2398 session_id
= s
->tmp_session_id
;
2400 sl
= s
->session
->session_id_length
;
2401 session_id
= s
->session
->session_id
;
2404 if (sl
> sizeof(s
->session
->session_id
)) {
2405 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2406 ERR_R_INTERNAL_ERROR
);
2410 /* set up the compression method */
2411 #ifdef OPENSSL_NO_COMP
2414 if (usetls13
|| s
->s3
.tmp
.new_compression
== NULL
)
2417 compm
= s
->s3
.tmp
.new_compression
->id
;
2420 if (!WPACKET_sub_memcpy_u8(pkt
, session_id
, sl
)
2421 || !s
->method
->put_cipher_by_char(s
->s3
.tmp
.new_cipher
, pkt
, &len
)
2422 || !WPACKET_put_bytes_u8(pkt
, compm
)) {
2423 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_SERVER_HELLO
,
2424 ERR_R_INTERNAL_ERROR
);
2428 if (!tls_construct_extensions(s
, pkt
,
2429 s
->hello_retry_request
== SSL_HRR_PENDING
2430 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2432 ? SSL_EXT_TLS1_3_SERVER_HELLO
2433 : SSL_EXT_TLS1_2_SERVER_HELLO
),
2435 /* SSLfatal() already called */
2439 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
2440 /* Ditch the session. We'll create a new one next time around */
2441 SSL_SESSION_free(s
->session
);
2446 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2447 * a synthetic message_hash in place of ClientHello1.
2449 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
2450 /* SSLfatal() already called */
2453 } else if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2454 && !ssl3_digest_cached_records(s
, 0)) {
2455 /* SSLfatal() already called */;
2462 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2464 if (!s
->s3
.tmp
.cert_request
) {
2465 if (!ssl3_digest_cached_records(s
, 0)) {
2466 /* SSLfatal() already called */
2473 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2475 #ifndef OPENSSL_NO_DH
2476 EVP_PKEY
*pkdh
= NULL
;
2478 #ifndef OPENSSL_NO_EC
2479 unsigned char *encodedPoint
= NULL
;
2480 size_t encodedlen
= 0;
2483 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
2487 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2488 EVP_PKEY_CTX
*pctx
= NULL
;
2489 size_t paramlen
, paramoffset
;
2491 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2492 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2493 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2497 if (md_ctx
== NULL
) {
2498 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2499 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2503 type
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2505 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2506 #ifndef OPENSSL_NO_PSK
2507 /* Plain PSK or RSAPSK nothing to do */
2508 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2510 #endif /* !OPENSSL_NO_PSK */
2511 #ifndef OPENSSL_NO_DH
2512 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2513 CERT
*cert
= s
->cert
;
2515 EVP_PKEY
*pkdhp
= NULL
;
2518 if (s
->cert
->dh_tmp_auto
) {
2519 DH
*dhp
= ssl_get_auto_dh(s
);
2520 pkdh
= EVP_PKEY_new();
2521 if (pkdh
== NULL
|| dhp
== NULL
) {
2523 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2524 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2525 ERR_R_INTERNAL_ERROR
);
2528 EVP_PKEY_assign_DH(pkdh
, dhp
);
2531 pkdhp
= cert
->dh_tmp
;
2533 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2534 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2535 pkdh
= ssl_dh_to_pkey(dhp
);
2537 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2538 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2539 ERR_R_INTERNAL_ERROR
);
2544 if (pkdhp
== NULL
) {
2545 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2546 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2547 SSL_R_MISSING_TMP_DH_KEY
);
2550 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2551 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2552 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2553 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2554 SSL_R_DH_KEY_TOO_SMALL
);
2557 if (s
->s3
.tmp
.pkey
!= NULL
) {
2558 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2559 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2560 ERR_R_INTERNAL_ERROR
);
2564 s
->s3
.tmp
.pkey
= ssl_generate_pkey(pkdhp
);
2565 if (s
->s3
.tmp
.pkey
== NULL
) {
2566 /* SSLfatal() already called */
2570 dh
= EVP_PKEY_get0_DH(s
->s3
.tmp
.pkey
);
2572 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2573 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2574 ERR_R_INTERNAL_ERROR
);
2578 EVP_PKEY_free(pkdh
);
2581 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2582 DH_get0_key(dh
, &r
[2], NULL
);
2585 #ifndef OPENSSL_NO_EC
2586 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2588 if (s
->s3
.tmp
.pkey
!= NULL
) {
2589 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2590 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2591 ERR_R_INTERNAL_ERROR
);
2595 /* Get NID of appropriate shared curve */
2596 curve_id
= tls1_shared_group(s
, -2);
2597 if (curve_id
== 0) {
2598 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
2599 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2600 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2603 s
->s3
.tmp
.pkey
= ssl_generate_pkey_group(s
, curve_id
);
2604 /* Generate a new key for this curve */
2605 if (s
->s3
.tmp
.pkey
== NULL
) {
2606 /* SSLfatal() already called */
2610 /* Encode the public key. */
2611 encodedlen
= EVP_PKEY_get1_tls_encodedpoint(s
->s3
.tmp
.pkey
,
2613 if (encodedlen
== 0) {
2614 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2615 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2620 * We'll generate the serverKeyExchange message explicitly so we
2621 * can set these to NULLs
2628 #endif /* !OPENSSL_NO_EC */
2629 #ifndef OPENSSL_NO_SRP
2630 if (type
& SSL_kSRP
) {
2631 if ((s
->srp_ctx
.N
== NULL
) ||
2632 (s
->srp_ctx
.g
== NULL
) ||
2633 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2634 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2635 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2636 SSL_R_MISSING_SRP_PARAM
);
2639 r
[0] = s
->srp_ctx
.N
;
2640 r
[1] = s
->srp_ctx
.g
;
2641 r
[2] = s
->srp_ctx
.s
;
2642 r
[3] = s
->srp_ctx
.B
;
2646 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2647 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2648 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2652 if (((s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2653 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2655 } else if (lu
== NULL
) {
2656 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
2657 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2661 #ifndef OPENSSL_NO_PSK
2662 if (type
& SSL_PSK
) {
2663 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2664 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2667 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2668 * checked this when we set the identity hint - but just in case
2670 if (len
> PSK_MAX_IDENTITY_LEN
2671 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2673 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2674 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2675 ERR_R_INTERNAL_ERROR
);
2681 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2682 unsigned char *binval
;
2685 #ifndef OPENSSL_NO_SRP
2686 if ((i
== 2) && (type
& SSL_kSRP
)) {
2687 res
= WPACKET_start_sub_packet_u8(pkt
);
2690 res
= WPACKET_start_sub_packet_u16(pkt
);
2693 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2694 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2695 ERR_R_INTERNAL_ERROR
);
2699 #ifndef OPENSSL_NO_DH
2701 * for interoperability with some versions of the Microsoft TLS
2702 * stack, we need to zero pad the DHE pub key to the same length
2705 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2706 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2709 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2710 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2711 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2712 ERR_R_INTERNAL_ERROR
);
2715 memset(binval
, 0, len
);
2719 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2720 || !WPACKET_close(pkt
)) {
2721 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2722 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2723 ERR_R_INTERNAL_ERROR
);
2727 BN_bn2bin(r
[i
], binval
);
2730 #ifndef OPENSSL_NO_EC
2731 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2733 * We only support named (not generic) curves. In this situation, the
2734 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2735 * [1 byte length of encoded point], followed by the actual encoded
2738 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2739 || !WPACKET_put_bytes_u8(pkt
, 0)
2740 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2741 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2742 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2743 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2744 ERR_R_INTERNAL_ERROR
);
2747 OPENSSL_free(encodedPoint
);
2748 encodedPoint
= NULL
;
2754 EVP_PKEY
*pkey
= s
->s3
.tmp
.cert
->privatekey
;
2756 unsigned char *sigbytes1
, *sigbytes2
, *tbs
;
2757 size_t siglen
, tbslen
;
2760 if (pkey
== NULL
|| !tls1_lookup_md(lu
, &md
)) {
2761 /* Should never happen */
2762 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2763 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2764 ERR_R_INTERNAL_ERROR
);
2767 /* Get length of the parameters we have written above */
2768 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2769 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2770 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2771 ERR_R_INTERNAL_ERROR
);
2774 /* send signature algorithm */
2775 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
2776 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2777 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2778 ERR_R_INTERNAL_ERROR
);
2782 * Create the signature. We don't know the actual length of the sig
2783 * until after we've created it, so we reserve enough bytes for it
2784 * up front, and then properly allocate them in the WPACKET
2787 siglen
= EVP_PKEY_size(pkey
);
2788 if (!WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2789 || EVP_DigestSignInit(md_ctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
2790 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2791 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2792 ERR_R_INTERNAL_ERROR
);
2795 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2796 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2797 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2798 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2799 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2804 tbslen
= construct_key_exchange_tbs(s
, &tbs
,
2805 s
->init_buf
->data
+ paramoffset
,
2808 /* SSLfatal() already called */
2811 rv
= EVP_DigestSign(md_ctx
, sigbytes1
, &siglen
, tbs
, tbslen
);
2813 if (rv
<= 0 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2814 || sigbytes1
!= sigbytes2
) {
2815 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2816 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2817 ERR_R_INTERNAL_ERROR
);
2822 EVP_MD_CTX_free(md_ctx
);
2825 #ifndef OPENSSL_NO_DH
2826 EVP_PKEY_free(pkdh
);
2828 #ifndef OPENSSL_NO_EC
2829 OPENSSL_free(encodedPoint
);
2831 EVP_MD_CTX_free(md_ctx
);
2835 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2837 if (SSL_IS_TLS13(s
)) {
2838 /* Send random context when doing post-handshake auth */
2839 if (s
->post_handshake_auth
== SSL_PHA_REQUEST_PENDING
) {
2840 OPENSSL_free(s
->pha_context
);
2841 s
->pha_context_len
= 32;
2842 if ((s
->pha_context
= OPENSSL_malloc(s
->pha_context_len
)) == NULL
2843 || RAND_bytes(s
->pha_context
, s
->pha_context_len
) <= 0
2844 || !WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
, s
->pha_context_len
)) {
2845 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2846 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2847 ERR_R_INTERNAL_ERROR
);
2850 /* reset the handshake hash back to just after the ClientFinished */
2851 if (!tls13_restore_handshake_digest_for_pha(s
)) {
2852 /* SSLfatal() already called */
2856 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2857 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2858 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2859 ERR_R_INTERNAL_ERROR
);
2864 if (!tls_construct_extensions(s
, pkt
,
2865 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
, NULL
,
2867 /* SSLfatal() already called */
2873 /* get the list of acceptable cert types */
2874 if (!WPACKET_start_sub_packet_u8(pkt
)
2875 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2876 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2877 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2881 if (SSL_USE_SIGALGS(s
)) {
2882 const uint16_t *psigs
;
2883 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2885 if (!WPACKET_start_sub_packet_u16(pkt
)
2886 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
2887 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2888 || !WPACKET_close(pkt
)) {
2889 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2890 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2891 ERR_R_INTERNAL_ERROR
);
2896 if (!construct_ca_names(s
, get_ca_names(s
), pkt
)) {
2897 /* SSLfatal() already called */
2903 s
->s3
.tmp
.cert_request
= 1;
2907 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
)
2909 #ifndef OPENSSL_NO_PSK
2910 unsigned char psk
[PSK_MAX_PSK_LEN
];
2912 PACKET psk_identity
;
2914 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2915 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2916 SSL_R_LENGTH_MISMATCH
);
2919 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2920 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2921 SSL_R_DATA_LENGTH_TOO_LONG
);
2924 if (s
->psk_server_callback
== NULL
) {
2925 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2926 SSL_R_PSK_NO_SERVER_CB
);
2930 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2931 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2932 ERR_R_INTERNAL_ERROR
);
2936 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2939 if (psklen
> PSK_MAX_PSK_LEN
) {
2940 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2941 ERR_R_INTERNAL_ERROR
);
2943 } else if (psklen
== 0) {
2945 * PSK related to the given identity not found
2947 SSLfatal(s
, SSL_AD_UNKNOWN_PSK_IDENTITY
,
2948 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2949 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2953 OPENSSL_free(s
->s3
.tmp
.psk
);
2954 s
->s3
.tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2955 OPENSSL_cleanse(psk
, psklen
);
2957 if (s
->s3
.tmp
.psk
== NULL
) {
2958 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2959 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2963 s
->s3
.tmp
.psklen
= psklen
;
2967 /* Should never happen */
2968 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2969 ERR_R_INTERNAL_ERROR
);
2974 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
)
2976 #ifndef OPENSSL_NO_RSA
2977 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
2979 unsigned char decrypt_good
, version_good
;
2980 size_t j
, padding_len
;
2981 PACKET enc_premaster
;
2983 unsigned char *rsa_decrypt
= NULL
;
2986 rsa
= EVP_PKEY_get0_RSA(s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
);
2988 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
2989 SSL_R_MISSING_RSA_CERTIFICATE
);
2993 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2994 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2995 enc_premaster
= *pkt
;
2997 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2998 || PACKET_remaining(pkt
) != 0) {
2999 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3000 SSL_R_LENGTH_MISMATCH
);
3006 * We want to be sure that the plaintext buffer size makes it safe to
3007 * iterate over the entire size of a premaster secret
3008 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
3009 * their ciphertext cannot accommodate a premaster secret anyway.
3011 if (RSA_size(rsa
) < SSL_MAX_MASTER_KEY_LENGTH
) {
3012 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3013 RSA_R_KEY_SIZE_TOO_SMALL
);
3017 rsa_decrypt
= OPENSSL_malloc(RSA_size(rsa
));
3018 if (rsa_decrypt
== NULL
) {
3019 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3020 ERR_R_MALLOC_FAILURE
);
3025 * We must not leak whether a decryption failure occurs because of
3026 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
3027 * section 7.4.7.1). The code follows that advice of the TLS RFC and
3028 * generates a random premaster secret for the case that the decrypt
3029 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
3032 if (RAND_priv_bytes(rand_premaster_secret
,
3033 sizeof(rand_premaster_secret
)) <= 0) {
3034 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3035 ERR_R_INTERNAL_ERROR
);
3040 * Decrypt with no padding. PKCS#1 padding will be removed as part of
3041 * the timing-sensitive code below.
3043 /* TODO(size_t): Convert this function */
3044 decrypt_len
= (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster
),
3045 PACKET_data(&enc_premaster
),
3046 rsa_decrypt
, rsa
, RSA_NO_PADDING
);
3047 if (decrypt_len
< 0) {
3048 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3049 ERR_R_INTERNAL_ERROR
);
3053 /* Check the padding. See RFC 3447, section 7.2.2. */
3056 * The smallest padded premaster is 11 bytes of overhead. Small keys
3057 * are publicly invalid, so this may return immediately. This ensures
3058 * PS is at least 8 bytes.
3060 if (decrypt_len
< 11 + SSL_MAX_MASTER_KEY_LENGTH
) {
3061 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3062 SSL_R_DECRYPTION_FAILED
);
3066 padding_len
= decrypt_len
- SSL_MAX_MASTER_KEY_LENGTH
;
3067 decrypt_good
= constant_time_eq_int_8(rsa_decrypt
[0], 0) &
3068 constant_time_eq_int_8(rsa_decrypt
[1], 2);
3069 for (j
= 2; j
< padding_len
- 1; j
++) {
3070 decrypt_good
&= ~constant_time_is_zero_8(rsa_decrypt
[j
]);
3072 decrypt_good
&= constant_time_is_zero_8(rsa_decrypt
[padding_len
- 1]);
3075 * If the version in the decrypted pre-master secret is correct then
3076 * version_good will be 0xff, otherwise it'll be zero. The
3077 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
3078 * (http://eprint.iacr.org/2003/052/) exploits the version number
3079 * check as a "bad version oracle". Thus version checks are done in
3080 * constant time and are treated like any other decryption error.
3083 constant_time_eq_8(rsa_decrypt
[padding_len
],
3084 (unsigned)(s
->client_version
>> 8));
3086 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
3087 (unsigned)(s
->client_version
& 0xff));
3090 * The premaster secret must contain the same version number as the
3091 * ClientHello to detect version rollback attacks (strangely, the
3092 * protocol does not offer such protection for DH ciphersuites).
3093 * However, buggy clients exist that send the negotiated protocol
3094 * version instead if the server does not support the requested
3095 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
3098 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) {
3099 unsigned char workaround_good
;
3100 workaround_good
= constant_time_eq_8(rsa_decrypt
[padding_len
],
3101 (unsigned)(s
->version
>> 8));
3103 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
3104 (unsigned)(s
->version
& 0xff));
3105 version_good
|= workaround_good
;
3109 * Both decryption and version must be good for decrypt_good to
3110 * remain non-zero (0xff).
3112 decrypt_good
&= version_good
;
3115 * Now copy rand_premaster_secret over from p using
3116 * decrypt_good_mask. If decryption failed, then p does not
3117 * contain valid plaintext, however, a check above guarantees
3118 * it is still sufficiently large to read from.
3120 for (j
= 0; j
< sizeof(rand_premaster_secret
); j
++) {
3121 rsa_decrypt
[padding_len
+ j
] =
3122 constant_time_select_8(decrypt_good
,
3123 rsa_decrypt
[padding_len
+ j
],
3124 rand_premaster_secret
[j
]);
3127 if (!ssl_generate_master_secret(s
, rsa_decrypt
+ padding_len
,
3128 sizeof(rand_premaster_secret
), 0)) {
3129 /* SSLfatal() already called */
3135 OPENSSL_free(rsa_decrypt
);
3138 /* Should never happen */
3139 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_RSA
,
3140 ERR_R_INTERNAL_ERROR
);
3145 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
)
3147 #ifndef OPENSSL_NO_DH
3148 EVP_PKEY
*skey
= NULL
;
3152 const unsigned char *data
;
3153 EVP_PKEY
*ckey
= NULL
;
3156 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
3157 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3158 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
3161 skey
= s
->s3
.tmp
.pkey
;
3163 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3164 SSL_R_MISSING_TMP_DH_KEY
);
3168 if (PACKET_remaining(pkt
) == 0L) {
3169 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3170 SSL_R_MISSING_TMP_DH_KEY
);
3173 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
3174 /* We already checked we have enough data */
3175 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3176 ERR_R_INTERNAL_ERROR
);
3179 ckey
= EVP_PKEY_new();
3180 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
3181 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3186 cdh
= EVP_PKEY_get0_DH(ckey
);
3187 pub_key
= BN_bin2bn(data
, i
, NULL
);
3188 if (pub_key
== NULL
|| cdh
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
3189 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3190 ERR_R_INTERNAL_ERROR
);
3195 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3196 /* SSLfatal() already called */
3201 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3202 s
->s3
.tmp
.pkey
= NULL
;
3204 EVP_PKEY_free(ckey
);
3207 /* Should never happen */
3208 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_DHE
,
3209 ERR_R_INTERNAL_ERROR
);
3214 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
)
3216 #ifndef OPENSSL_NO_EC
3217 EVP_PKEY
*skey
= s
->s3
.tmp
.pkey
;
3218 EVP_PKEY
*ckey
= NULL
;
3221 if (PACKET_remaining(pkt
) == 0L) {
3222 /* We don't support ECDH client auth */
3223 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3224 SSL_R_MISSING_TMP_ECDH_KEY
);
3228 const unsigned char *data
;
3231 * Get client's public key from encoded point in the
3232 * ClientKeyExchange message.
3235 /* Get encoded point length */
3236 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
3237 || PACKET_remaining(pkt
) != 0) {
3238 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3239 SSL_R_LENGTH_MISMATCH
);
3243 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3244 SSL_R_MISSING_TMP_ECDH_KEY
);
3248 ckey
= EVP_PKEY_new();
3249 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
3250 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3254 if (EVP_PKEY_set1_tls_encodedpoint(ckey
, data
, i
) == 0) {
3255 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3261 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
3262 /* SSLfatal() already called */
3267 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
3268 s
->s3
.tmp
.pkey
= NULL
;
3270 EVP_PKEY_free(ckey
);
3274 /* Should never happen */
3275 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_ECDHE
,
3276 ERR_R_INTERNAL_ERROR
);
3281 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
)
3283 #ifndef OPENSSL_NO_SRP
3285 const unsigned char *data
;
3287 if (!PACKET_get_net_2(pkt
, &i
)
3288 || !PACKET_get_bytes(pkt
, &data
, i
)) {
3289 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3290 SSL_R_BAD_SRP_A_LENGTH
);
3293 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
3294 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3298 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
3299 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS_PROCESS_CKE_SRP
,
3300 SSL_R_BAD_SRP_PARAMETERS
);
3303 OPENSSL_free(s
->session
->srp_username
);
3304 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3305 if (s
->session
->srp_username
== NULL
) {
3306 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3307 ERR_R_MALLOC_FAILURE
);
3311 if (!srp_generate_server_master_secret(s
)) {
3312 /* SSLfatal() already called */
3318 /* Should never happen */
3319 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_SRP
,
3320 ERR_R_INTERNAL_ERROR
);
3325 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
)
3327 #ifndef OPENSSL_NO_GOST
3328 EVP_PKEY_CTX
*pkey_ctx
;
3329 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
3330 unsigned char premaster_secret
[32];
3331 const unsigned char *start
;
3332 size_t outlen
= 32, inlen
;
3333 unsigned long alg_a
;
3334 unsigned int asn1id
, asn1len
;
3338 /* Get our certificate private key */
3339 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3340 if (alg_a
& SSL_aGOST12
) {
3342 * New GOST ciphersuites have SSL_aGOST01 bit too
3344 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
3346 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
3349 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3351 } else if (alg_a
& SSL_aGOST01
) {
3352 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
3355 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
3356 if (pkey_ctx
== NULL
) {
3357 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3358 ERR_R_MALLOC_FAILURE
);
3361 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
3362 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3363 ERR_R_INTERNAL_ERROR
);
3367 * If client certificate is present and is of the same type, maybe
3368 * use it for key exchange. Don't mind errors from
3369 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3370 * client certificate for authorization only.
3372 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3373 if (client_pub_pkey
) {
3374 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3377 /* Decrypt session key */
3378 if (!PACKET_get_1(pkt
, &asn1id
)
3379 || asn1id
!= (V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
)
3380 || !PACKET_peek_1(pkt
, &asn1len
)) {
3381 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3382 SSL_R_DECRYPTION_FAILED
);
3385 if (asn1len
== 0x81) {
3387 * Long form length. Should only be one byte of length. Anything else
3389 * We did a successful peek before so this shouldn't fail
3391 if (!PACKET_forward(pkt
, 1)) {
3392 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3393 SSL_R_DECRYPTION_FAILED
);
3396 } else if (asn1len
>= 0x80) {
3398 * Indefinite length, or more than one long form length bytes. We don't
3401 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3402 SSL_R_DECRYPTION_FAILED
);
3404 } /* else short form length */
3406 if (!PACKET_as_length_prefixed_1(pkt
, &encdata
)) {
3407 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3408 SSL_R_DECRYPTION_FAILED
);
3411 inlen
= PACKET_remaining(&encdata
);
3412 start
= PACKET_data(&encdata
);
3414 if (EVP_PKEY_decrypt(pkey_ctx
, premaster_secret
, &outlen
, start
,
3416 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3417 SSL_R_DECRYPTION_FAILED
);
3420 /* Generate master secret */
3421 if (!ssl_generate_master_secret(s
, premaster_secret
,
3422 sizeof(premaster_secret
), 0)) {
3423 /* SSLfatal() already called */
3426 /* Check if pubkey from client certificate was used */
3427 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2,
3429 s
->statem
.no_cert_verify
= 1;
3433 EVP_PKEY_CTX_free(pkey_ctx
);
3436 /* Should never happen */
3437 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CKE_GOST
,
3438 ERR_R_INTERNAL_ERROR
);
3443 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3445 unsigned long alg_k
;
3447 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3449 /* For PSK parse and retrieve identity, obtain PSK key */
3450 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
)) {
3451 /* SSLfatal() already called */
3455 if (alg_k
& SSL_kPSK
) {
3456 /* Identity extracted earlier: should be nothing left */
3457 if (PACKET_remaining(pkt
) != 0) {
3458 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3459 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3460 SSL_R_LENGTH_MISMATCH
);
3463 /* PSK handled by ssl_generate_master_secret */
3464 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3465 /* SSLfatal() already called */
3468 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3469 if (!tls_process_cke_rsa(s
, pkt
)) {
3470 /* SSLfatal() already called */
3473 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3474 if (!tls_process_cke_dhe(s
, pkt
)) {
3475 /* SSLfatal() already called */
3478 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3479 if (!tls_process_cke_ecdhe(s
, pkt
)) {
3480 /* SSLfatal() already called */
3483 } else if (alg_k
& SSL_kSRP
) {
3484 if (!tls_process_cke_srp(s
, pkt
)) {
3485 /* SSLfatal() already called */
3488 } else if (alg_k
& SSL_kGOST
) {
3489 if (!tls_process_cke_gost(s
, pkt
)) {
3490 /* SSLfatal() already called */
3494 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3495 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3496 SSL_R_UNKNOWN_CIPHER_TYPE
);
3500 return MSG_PROCESS_CONTINUE_PROCESSING
;
3502 #ifndef OPENSSL_NO_PSK
3503 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3504 s
->s3
.tmp
.psk
= NULL
;
3506 return MSG_PROCESS_ERROR
;
3509 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3511 #ifndef OPENSSL_NO_SCTP
3512 if (wst
== WORK_MORE_A
) {
3513 if (SSL_IS_DTLS(s
)) {
3514 unsigned char sctpauthkey
[64];
3515 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3518 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3521 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3522 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3524 /* Don't include the terminating zero. */
3525 labellen
= sizeof(labelbuffer
) - 1;
3526 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3529 if (SSL_export_keying_material(s
, sctpauthkey
,
3530 sizeof(sctpauthkey
), labelbuffer
,
3533 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3534 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3535 ERR_R_INTERNAL_ERROR
);
3539 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3540 sizeof(sctpauthkey
), sctpauthkey
);
3545 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3547 * No certificate verify or no peer certificate so we no longer need
3548 * the handshake_buffer
3550 if (!ssl3_digest_cached_records(s
, 0)) {
3551 /* SSLfatal() already called */
3554 return WORK_FINISHED_CONTINUE
;
3556 if (!s
->s3
.handshake_buffer
) {
3557 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3558 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3559 ERR_R_INTERNAL_ERROR
);
3563 * For sigalgs freeze the handshake buffer. If we support
3564 * extms we've done this already so this is a no-op
3566 if (!ssl3_digest_cached_records(s
, 1)) {
3567 /* SSLfatal() already called */
3572 return WORK_FINISHED_CONTINUE
;
3575 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3578 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
3581 const unsigned char *certstart
, *certbytes
;
3582 STACK_OF(X509
) *sk
= NULL
;
3583 PACKET spkt
, context
;
3585 SSL_SESSION
*new_sess
= NULL
;
3588 * To get this far we must have read encrypted data from the client. We no
3589 * longer tolerate unencrypted alerts. This value is ignored if less than
3592 s
->statem
.enc_read_state
= ENC_READ_STATE_VALID
;
3594 if ((sk
= sk_X509_new_null()) == NULL
) {
3595 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3596 ERR_R_MALLOC_FAILURE
);
3600 if (SSL_IS_TLS13(s
) && (!PACKET_get_length_prefixed_1(pkt
, &context
)
3601 || (s
->pha_context
== NULL
&& PACKET_remaining(&context
) != 0)
3602 || (s
->pha_context
!= NULL
&&
3603 !PACKET_equal(&context
, s
->pha_context
, s
->pha_context_len
)))) {
3604 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3605 SSL_R_INVALID_CONTEXT
);
3609 if (!PACKET_get_length_prefixed_3(pkt
, &spkt
)
3610 || PACKET_remaining(pkt
) != 0) {
3611 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3612 SSL_R_LENGTH_MISMATCH
);
3616 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3617 if (!PACKET_get_net_3(&spkt
, &l
)
3618 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3619 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3620 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3621 SSL_R_CERT_LENGTH_MISMATCH
);
3625 certstart
= certbytes
;
3626 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, l
);
3628 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3629 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3632 if (certbytes
!= (certstart
+ l
)) {
3633 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3634 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3635 SSL_R_CERT_LENGTH_MISMATCH
);
3639 if (SSL_IS_TLS13(s
)) {
3640 RAW_EXTENSION
*rawexts
= NULL
;
3643 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3644 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
3645 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3649 if (!tls_collect_extensions(s
, &extensions
,
3650 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
3651 NULL
, chainidx
== 0)
3652 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
3653 rawexts
, x
, chainidx
,
3654 PACKET_remaining(&spkt
) == 0)) {
3655 OPENSSL_free(rawexts
);
3658 OPENSSL_free(rawexts
);
3661 if (!sk_X509_push(sk
, x
)) {
3662 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3663 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3664 ERR_R_MALLOC_FAILURE
);
3670 if (sk_X509_num(sk
) <= 0) {
3671 /* TLS does not mind 0 certs returned */
3672 if (s
->version
== SSL3_VERSION
) {
3673 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3674 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3675 SSL_R_NO_CERTIFICATES_RETURNED
);
3678 /* Fail for TLS only if we required a certificate */
3679 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3680 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3681 SSLfatal(s
, SSL_AD_CERTIFICATE_REQUIRED
,
3682 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3683 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3686 /* No client certificate so digest cached records */
3687 if (s
->s3
.handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3688 /* SSLfatal() already called */
3693 i
= ssl_verify_cert_chain(s
, sk
);
3695 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
3696 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3697 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3701 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3702 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3705 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3707 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3708 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3709 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3715 * Sessions must be immutable once they go into the session cache. Otherwise
3716 * we can get multi-thread problems. Therefore we don't "update" sessions,
3717 * we replace them with a duplicate. Here, we need to do this every time
3718 * a new certificate is received via post-handshake authentication, as the
3719 * session may have already gone into the session cache.
3722 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3723 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
3724 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3725 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3726 ERR_R_MALLOC_FAILURE
);
3730 SSL_SESSION_free(s
->session
);
3731 s
->session
= new_sess
;
3734 X509_free(s
->session
->peer
);
3735 s
->session
->peer
= sk_X509_shift(sk
);
3736 s
->session
->verify_result
= s
->verify_result
;
3738 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3739 s
->session
->peer_chain
= sk
;
3742 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3745 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3746 /* SSLfatal() already called */
3751 * Inconsistency alert: cert_chain does *not* include the peer's own
3752 * certificate, while we do include it in statem_clnt.c
3756 /* Save the current hash state for when we receive the CertificateVerify */
3757 if (SSL_IS_TLS13(s
)) {
3758 if (!ssl_handshake_hash(s
, s
->cert_verify_hash
,
3759 sizeof(s
->cert_verify_hash
),
3760 &s
->cert_verify_hash_len
)) {
3761 /* SSLfatal() already called */
3765 /* Resend session tickets */
3766 s
->sent_tickets
= 0;
3769 ret
= MSG_PROCESS_CONTINUE_READING
;
3773 sk_X509_pop_free(sk
, X509_free
);
3777 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3779 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
3782 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3783 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3788 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3789 * for the server Certificate message
3791 if (SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0)) {
3792 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3793 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3796 if (!ssl3_output_cert_chain(s
, pkt
, cpk
)) {
3797 /* SSLfatal() already called */
3804 static int create_ticket_prequel(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3805 unsigned char *tick_nonce
)
3808 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3809 * unspecified for resumed session (for simplicity).
3810 * In TLSv1.3 we reset the "time" field above, and always specify the
3813 if (!WPACKET_put_bytes_u32(pkt
,
3814 (s
->hit
&& !SSL_IS_TLS13(s
))
3815 ? 0 : s
->session
->timeout
)) {
3816 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3817 ERR_R_INTERNAL_ERROR
);
3821 if (SSL_IS_TLS13(s
)) {
3822 if (!WPACKET_put_bytes_u32(pkt
, age_add
)
3823 || !WPACKET_sub_memcpy_u8(pkt
, tick_nonce
, TICKET_NONCE_SIZE
)) {
3824 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3825 ERR_R_INTERNAL_ERROR
);
3830 /* Start the sub-packet for the actual ticket data */
3831 if (!WPACKET_start_sub_packet_u16(pkt
)) {
3832 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CREATE_TICKET_PREQUEL
,
3833 ERR_R_INTERNAL_ERROR
);
3840 static int construct_stateless_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
3841 unsigned char *tick_nonce
)
3843 unsigned char *senc
= NULL
;
3844 EVP_CIPHER_CTX
*ctx
= NULL
;
3845 HMAC_CTX
*hctx
= NULL
;
3846 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3847 const unsigned char *const_p
;
3848 int len
, slen_full
, slen
, lenfinal
;
3851 SSL_CTX
*tctx
= s
->session_ctx
;
3852 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3853 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3855 size_t macoffset
, macendoffset
;
3857 /* get session encoding length */
3858 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3860 * Some length values are 16 bits, so forget it if session is too
3863 if (slen_full
== 0 || slen_full
> 0xFF00) {
3864 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3865 ERR_R_INTERNAL_ERROR
);
3868 senc
= OPENSSL_malloc(slen_full
);
3870 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3871 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_MALLOC_FAILURE
);
3875 ctx
= EVP_CIPHER_CTX_new();
3876 hctx
= HMAC_CTX_new();
3877 if (ctx
== NULL
|| hctx
== NULL
) {
3878 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3879 ERR_R_MALLOC_FAILURE
);
3884 if (!i2d_SSL_SESSION(s
->session
, &p
)) {
3885 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3886 ERR_R_INTERNAL_ERROR
);
3891 * create a fresh copy (not shared with other threads) to clean up
3894 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3896 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3897 ERR_R_INTERNAL_ERROR
);
3901 slen
= i2d_SSL_SESSION(sess
, NULL
);
3902 if (slen
== 0 || slen
> slen_full
) {
3903 /* shouldn't ever happen */
3904 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3905 ERR_R_INTERNAL_ERROR
);
3906 SSL_SESSION_free(sess
);
3910 if (!i2d_SSL_SESSION(sess
, &p
)) {
3911 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3912 ERR_R_INTERNAL_ERROR
);
3913 SSL_SESSION_free(sess
);
3916 SSL_SESSION_free(sess
);
3919 * Initialize HMAC and cipher contexts. If callback present it does
3920 * all the work otherwise use generated values from parent ctx.
3922 if (tctx
->ext
.ticket_key_cb
) {
3923 /* if 0 is returned, write an empty ticket */
3924 int ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3929 /* Put timeout and length */
3930 if (!WPACKET_put_bytes_u32(pkt
, 0)
3931 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3932 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3933 SSL_F_CONSTRUCT_STATELESS_TICKET
,
3934 ERR_R_INTERNAL_ERROR
);
3938 EVP_CIPHER_CTX_free(ctx
);
3939 HMAC_CTX_free(hctx
);
3943 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3944 SSL_R_CALLBACK_FAILED
);
3947 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
3949 const EVP_CIPHER
*cipher
= EVP_aes_256_cbc();
3951 iv_len
= EVP_CIPHER_iv_length(cipher
);
3952 if (RAND_bytes(iv
, iv_len
) <= 0
3953 || !EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3954 tctx
->ext
.secure
->tick_aes_key
, iv
)
3955 || !HMAC_Init_ex(hctx
, tctx
->ext
.secure
->tick_hmac_key
,
3956 sizeof(tctx
->ext
.secure
->tick_hmac_key
),
3957 EVP_sha256(), NULL
)) {
3958 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
3959 ERR_R_INTERNAL_ERROR
);
3962 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3963 sizeof(tctx
->ext
.tick_key_name
));
3966 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
3967 /* SSLfatal() already called */
3971 if (!WPACKET_get_total_written(pkt
, &macoffset
)
3972 /* Output key name */
3973 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3975 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3976 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3978 /* Encrypt session data */
3979 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3980 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3981 || encdata1
!= encdata2
3982 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3983 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3984 || encdata1
+ len
!= encdata2
3985 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3986 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3987 || !HMAC_Update(hctx
,
3988 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3989 macendoffset
- macoffset
)
3990 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3991 || !HMAC_Final(hctx
, macdata1
, &hlen
)
3992 || hlen
> EVP_MAX_MD_SIZE
3993 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3994 || macdata1
!= macdata2
) {
3995 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
3996 SSL_F_CONSTRUCT_STATELESS_TICKET
, ERR_R_INTERNAL_ERROR
);
4000 /* Close the sub-packet created by create_ticket_prequel() */
4001 if (!WPACKET_close(pkt
)) {
4002 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATELESS_TICKET
,
4003 ERR_R_INTERNAL_ERROR
);
4010 EVP_CIPHER_CTX_free(ctx
);
4011 HMAC_CTX_free(hctx
);
4015 static int construct_stateful_ticket(SSL
*s
, WPACKET
*pkt
, uint32_t age_add
,
4016 unsigned char *tick_nonce
)
4018 if (!create_ticket_prequel(s
, pkt
, age_add
, tick_nonce
)) {
4019 /* SSLfatal() already called */
4023 if (!WPACKET_memcpy(pkt
, s
->session
->session_id
,
4024 s
->session
->session_id_length
)
4025 || !WPACKET_close(pkt
)) {
4026 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_CONSTRUCT_STATEFUL_TICKET
,
4027 ERR_R_INTERNAL_ERROR
);
4034 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
4036 SSL_CTX
*tctx
= s
->session_ctx
;
4037 unsigned char tick_nonce
[TICKET_NONCE_SIZE
];
4039 unsigned char age_add_c
[sizeof(uint32_t)];
4043 age_add_u
.age_add
= 0;
4045 if (SSL_IS_TLS13(s
)) {
4048 static const unsigned char nonce_label
[] = "resumption";
4049 const EVP_MD
*md
= ssl_handshake_md(s
);
4050 int hashleni
= EVP_MD_size(md
);
4052 /* Ensure cast to size_t is safe */
4053 if (!ossl_assert(hashleni
>= 0)) {
4054 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4055 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4056 ERR_R_INTERNAL_ERROR
);
4059 hashlen
= (size_t)hashleni
;
4062 * If we already sent one NewSessionTicket, or we resumed then
4063 * s->session may already be in a cache and so we must not modify it.
4064 * Instead we need to take a copy of it and modify that.
4066 if (s
->sent_tickets
!= 0 || s
->hit
) {
4067 SSL_SESSION
*new_sess
= ssl_session_dup(s
->session
, 0);
4069 if (new_sess
== NULL
) {
4070 /* SSLfatal already called */
4074 SSL_SESSION_free(s
->session
);
4075 s
->session
= new_sess
;
4078 if (!ssl_generate_session_id(s
, s
->session
)) {
4079 /* SSLfatal() already called */
4082 if (RAND_bytes(age_add_u
.age_add_c
, sizeof(age_add_u
)) <= 0) {
4083 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4084 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4085 ERR_R_INTERNAL_ERROR
);
4088 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
4090 nonce
= s
->next_ticket_nonce
;
4091 for (i
= TICKET_NONCE_SIZE
; i
> 0; i
--) {
4092 tick_nonce
[i
- 1] = (unsigned char)(nonce
& 0xff);
4096 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
4098 sizeof(nonce_label
) - 1,
4101 s
->session
->master_key
,
4103 /* SSLfatal() already called */
4106 s
->session
->master_key_length
= hashlen
;
4108 s
->session
->time
= (long)time(NULL
);
4109 if (s
->s3
.alpn_selected
!= NULL
) {
4110 OPENSSL_free(s
->session
->ext
.alpn_selected
);
4111 s
->session
->ext
.alpn_selected
=
4112 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
4113 if (s
->session
->ext
.alpn_selected
== NULL
) {
4114 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
4115 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
4116 ERR_R_MALLOC_FAILURE
);
4119 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
4121 s
->session
->ext
.max_early_data
= s
->max_early_data
;
4124 if (tctx
->generate_ticket_cb
!= NULL
&&
4125 tctx
->generate_ticket_cb(s
, tctx
->ticket_cb_data
) == 0)
4129 * If we are using anti-replay protection then we behave as if
4130 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4131 * is no point in using full stateless tickets.
4134 && ((s
->options
& SSL_OP_NO_TICKET
) != 0
4135 || (s
->max_early_data
> 0
4136 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0))) {
4137 if (!construct_stateful_ticket(s
, pkt
, age_add_u
.age_add
, tick_nonce
)) {
4138 /* SSLfatal() already called */
4141 } else if (!construct_stateless_ticket(s
, pkt
, age_add_u
.age_add
,
4143 /* SSLfatal() already called */
4147 if (SSL_IS_TLS13(s
)) {
4148 if (!tls_construct_extensions(s
, pkt
,
4149 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
4151 /* SSLfatal() already called */
4155 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4156 * gets reset to 0 if we send more tickets following a post-handshake
4157 * auth, but |next_ticket_nonce| does not.
4160 s
->next_ticket_nonce
++;
4161 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
4170 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4171 * create a separate message. Returns 1 on success or 0 on failure.
4173 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
4175 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
4176 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
4177 s
->ext
.ocsp
.resp_len
)) {
4178 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
,
4179 ERR_R_INTERNAL_ERROR
);
4186 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
4188 if (!tls_construct_cert_status_body(s
, pkt
)) {
4189 /* SSLfatal() already called */
4196 #ifndef OPENSSL_NO_NEXTPROTONEG
4198 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4199 * It sets the next_proto member in s if found
4201 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
4203 PACKET next_proto
, padding
;
4204 size_t next_proto_len
;
4207 * The payload looks like:
4209 * uint8 proto[proto_len];
4210 * uint8 padding_len;
4211 * uint8 padding[padding_len];
4213 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
4214 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
4215 || PACKET_remaining(pkt
) > 0) {
4216 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4217 SSL_R_LENGTH_MISMATCH
);
4218 return MSG_PROCESS_ERROR
;
4221 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
4223 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_NEXT_PROTO
,
4224 ERR_R_INTERNAL_ERROR
);
4225 return MSG_PROCESS_ERROR
;
4228 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
4230 return MSG_PROCESS_CONTINUE_READING
;
4234 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
4236 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
4238 /* SSLfatal() already called */
4245 MSG_PROCESS_RETURN
tls_process_end_of_early_data(SSL
*s
, PACKET
*pkt
)
4247 if (PACKET_remaining(pkt
) != 0) {
4248 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4249 SSL_R_LENGTH_MISMATCH
);
4250 return MSG_PROCESS_ERROR
;
4253 if (s
->early_data_state
!= SSL_EARLY_DATA_READING
4254 && s
->early_data_state
!= SSL_EARLY_DATA_READ_RETRY
) {
4255 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4256 ERR_R_INTERNAL_ERROR
);
4257 return MSG_PROCESS_ERROR
;
4261 * EndOfEarlyData signals a key change so the end of the message must be on
4262 * a record boundary.
4264 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
4265 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
4266 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA
,
4267 SSL_R_NOT_ON_RECORD_BOUNDARY
);
4268 return MSG_PROCESS_ERROR
;
4271 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
4272 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
4273 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
4274 /* SSLfatal() already called */
4275 return MSG_PROCESS_ERROR
;
4278 return MSG_PROCESS_CONTINUE_READING
;