2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
15 #include "../ssl_local.h"
16 #include "statem_local.h"
17 #include <openssl/buffer.h>
18 #include <openssl/rand.h>
19 #include <openssl/objects.h>
20 #include <openssl/evp.h>
21 #include <openssl/md5.h>
22 #include <openssl/dh.h>
23 #include <openssl/rsa.h>
24 #include <openssl/bn.h>
25 #include <openssl/engine.h>
26 #include <openssl/trace.h>
27 #include <openssl/core_names.h>
28 #include <openssl/param_build.h>
29 #include <internal/cryptlib.h>
31 static MSG_PROCESS_RETURN
tls_process_as_hello_retry_request(SSL
*s
, PACKET
*pkt
);
32 static MSG_PROCESS_RETURN
tls_process_encrypted_extensions(SSL
*s
, PACKET
*pkt
);
34 static ossl_inline
int cert_req_allowed(SSL
*s
);
35 static int key_exchange_expected(SSL
*s
);
36 static int ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
,
40 * Is a CertificateRequest message allowed at the moment or not?
46 static ossl_inline
int cert_req_allowed(SSL
*s
)
48 /* TLS does not like anon-DH with client cert */
49 if ((s
->version
> SSL3_VERSION
50 && (s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
))
51 || (s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aSRP
| SSL_aPSK
)))
58 * Should we expect the ServerKeyExchange message or not?
64 static int key_exchange_expected(SSL
*s
)
66 long alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
69 * Can't skip server key exchange if this is an ephemeral
70 * ciphersuite or for SRP
72 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
| SSL_kDHEPSK
| SSL_kECDHEPSK
81 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
82 * handshake state transitions when a TLS1.3 client is reading messages from the
83 * server. The message type that the server has sent is provided in |mt|. The
84 * current state is in |s->statem.hand_state|.
86 * Return values are 1 for success (transition allowed) and 0 on error
87 * (transition not allowed)
89 static int ossl_statem_client13_read_transition(SSL
*s
, int mt
)
91 OSSL_STATEM
*st
= &s
->statem
;
94 * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
95 * yet negotiated TLSv1.3 at that point so that is handled by
96 * ossl_statem_client_read_transition()
99 switch (st
->hand_state
) {
103 case TLS_ST_CW_CLNT_HELLO
:
105 * This must a ClientHello following a HelloRetryRequest, so the only
106 * thing we can get now is a ServerHello.
108 if (mt
== SSL3_MT_SERVER_HELLO
) {
109 st
->hand_state
= TLS_ST_CR_SRVR_HELLO
;
114 case TLS_ST_CR_SRVR_HELLO
:
115 if (mt
== SSL3_MT_ENCRYPTED_EXTENSIONS
) {
116 st
->hand_state
= TLS_ST_CR_ENCRYPTED_EXTENSIONS
;
121 case TLS_ST_CR_ENCRYPTED_EXTENSIONS
:
123 if (mt
== SSL3_MT_FINISHED
) {
124 st
->hand_state
= TLS_ST_CR_FINISHED
;
128 if (mt
== SSL3_MT_CERTIFICATE_REQUEST
) {
129 st
->hand_state
= TLS_ST_CR_CERT_REQ
;
132 if (mt
== SSL3_MT_CERTIFICATE
) {
133 st
->hand_state
= TLS_ST_CR_CERT
;
139 case TLS_ST_CR_CERT_REQ
:
140 if (mt
== SSL3_MT_CERTIFICATE
) {
141 st
->hand_state
= TLS_ST_CR_CERT
;
147 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
148 st
->hand_state
= TLS_ST_CR_CERT_VRFY
;
153 case TLS_ST_CR_CERT_VRFY
:
154 if (mt
== SSL3_MT_FINISHED
) {
155 st
->hand_state
= TLS_ST_CR_FINISHED
;
161 if (mt
== SSL3_MT_NEWSESSION_TICKET
) {
162 st
->hand_state
= TLS_ST_CR_SESSION_TICKET
;
165 if (mt
== SSL3_MT_KEY_UPDATE
) {
166 st
->hand_state
= TLS_ST_CR_KEY_UPDATE
;
169 if (mt
== SSL3_MT_CERTIFICATE_REQUEST
) {
170 #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
171 # error TODO(DTLS1.3): Restore digest for PHA before adding message.
173 if (!SSL_IS_DTLS(s
) && s
->post_handshake_auth
== SSL_PHA_EXT_SENT
) {
174 s
->post_handshake_auth
= SSL_PHA_REQUESTED
;
176 * In TLS, this is called before the message is added to the
177 * digest. In DTLS, this is expected to be called after adding
178 * to the digest. Either move the digest restore, or add the
179 * message here after the swap, or do it after the clientFinished?
181 if (!tls13_restore_handshake_digest_for_pha(s
)) {
182 /* SSLfatal() already called */
185 st
->hand_state
= TLS_ST_CR_CERT_REQ
;
192 /* No valid transition found */
197 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
198 * handshake state transitions when the client is reading messages from the
199 * server. The message type that the server has sent is provided in |mt|. The
200 * current state is in |s->statem.hand_state|.
202 * Return values are 1 for success (transition allowed) and 0 on error
203 * (transition not allowed)
205 int ossl_statem_client_read_transition(SSL
*s
, int mt
)
207 OSSL_STATEM
*st
= &s
->statem
;
211 * Note that after writing the first ClientHello we don't know what version
212 * we are going to negotiate yet, so we don't take this branch until later.
214 if (SSL_IS_TLS13(s
)) {
215 if (!ossl_statem_client13_read_transition(s
, mt
))
220 switch (st
->hand_state
) {
224 case TLS_ST_CW_CLNT_HELLO
:
225 if (mt
== SSL3_MT_SERVER_HELLO
) {
226 st
->hand_state
= TLS_ST_CR_SRVR_HELLO
;
230 if (SSL_IS_DTLS(s
)) {
231 if (mt
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
232 st
->hand_state
= DTLS_ST_CR_HELLO_VERIFY_REQUEST
;
238 case TLS_ST_EARLY_DATA
:
240 * We've not actually selected TLSv1.3 yet, but we have sent early
241 * data. The only thing allowed now is a ServerHello or a
244 if (mt
== SSL3_MT_SERVER_HELLO
) {
245 st
->hand_state
= TLS_ST_CR_SRVR_HELLO
;
250 case TLS_ST_CR_SRVR_HELLO
:
252 if (s
->ext
.ticket_expected
) {
253 if (mt
== SSL3_MT_NEWSESSION_TICKET
) {
254 st
->hand_state
= TLS_ST_CR_SESSION_TICKET
;
257 } else if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
258 st
->hand_state
= TLS_ST_CR_CHANGE
;
262 if (SSL_IS_DTLS(s
) && mt
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
263 st
->hand_state
= DTLS_ST_CR_HELLO_VERIFY_REQUEST
;
265 } else if (s
->version
>= TLS1_VERSION
266 && s
->ext
.session_secret_cb
!= NULL
267 && s
->session
->ext
.tick
!= NULL
268 && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
270 * Normally, we can tell if the server is resuming the session
271 * from the session ID. EAP-FAST (RFC 4851), however, relies on
272 * the next server message after the ServerHello to determine if
273 * the server is resuming.
276 st
->hand_state
= TLS_ST_CR_CHANGE
;
278 } else if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
279 & (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
280 if (mt
== SSL3_MT_CERTIFICATE
) {
281 st
->hand_state
= TLS_ST_CR_CERT
;
285 ske_expected
= key_exchange_expected(s
);
286 /* SKE is optional for some PSK ciphersuites */
288 || ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)
289 && mt
== SSL3_MT_SERVER_KEY_EXCHANGE
)) {
290 if (mt
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
291 st
->hand_state
= TLS_ST_CR_KEY_EXCH
;
294 } else if (mt
== SSL3_MT_CERTIFICATE_REQUEST
295 && cert_req_allowed(s
)) {
296 st
->hand_state
= TLS_ST_CR_CERT_REQ
;
298 } else if (mt
== SSL3_MT_SERVER_DONE
) {
299 st
->hand_state
= TLS_ST_CR_SRVR_DONE
;
308 * The CertificateStatus message is optional even if
309 * |ext.status_expected| is set
311 if (s
->ext
.status_expected
&& mt
== SSL3_MT_CERTIFICATE_STATUS
) {
312 st
->hand_state
= TLS_ST_CR_CERT_STATUS
;
317 case TLS_ST_CR_CERT_STATUS
:
318 ske_expected
= key_exchange_expected(s
);
319 /* SKE is optional for some PSK ciphersuites */
320 if (ske_expected
|| ((s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)
321 && mt
== SSL3_MT_SERVER_KEY_EXCHANGE
)) {
322 if (mt
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
323 st
->hand_state
= TLS_ST_CR_KEY_EXCH
;
330 case TLS_ST_CR_KEY_EXCH
:
331 if (mt
== SSL3_MT_CERTIFICATE_REQUEST
) {
332 if (cert_req_allowed(s
)) {
333 st
->hand_state
= TLS_ST_CR_CERT_REQ
;
340 case TLS_ST_CR_CERT_REQ
:
341 if (mt
== SSL3_MT_SERVER_DONE
) {
342 st
->hand_state
= TLS_ST_CR_SRVR_DONE
;
347 case TLS_ST_CW_FINISHED
:
348 if (s
->ext
.ticket_expected
) {
349 if (mt
== SSL3_MT_NEWSESSION_TICKET
) {
350 st
->hand_state
= TLS_ST_CR_SESSION_TICKET
;
353 } else if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
354 st
->hand_state
= TLS_ST_CR_CHANGE
;
359 case TLS_ST_CR_SESSION_TICKET
:
360 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
361 st
->hand_state
= TLS_ST_CR_CHANGE
;
366 case TLS_ST_CR_CHANGE
:
367 if (mt
== SSL3_MT_FINISHED
) {
368 st
->hand_state
= TLS_ST_CR_FINISHED
;
374 if (mt
== SSL3_MT_HELLO_REQUEST
) {
375 st
->hand_state
= TLS_ST_CR_HELLO_REQ
;
382 /* No valid transition found */
383 if (SSL_IS_DTLS(s
) && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
387 * CCS messages don't have a message sequence number so this is probably
388 * because of an out-of-order CCS. We'll just drop it.
391 s
->rwstate
= SSL_READING
;
392 rbio
= SSL_get_rbio(s
);
393 BIO_clear_retry_flags(rbio
);
394 BIO_set_retry_read(rbio
);
397 SSLfatal(s
, SSL3_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
402 * ossl_statem_client13_write_transition() works out what handshake state to
403 * move to next when the TLSv1.3 client is writing messages to be sent to the
406 static WRITE_TRAN
ossl_statem_client13_write_transition(SSL
*s
)
408 OSSL_STATEM
*st
= &s
->statem
;
411 * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
412 * TLSv1.3 yet at that point. They are handled by
413 * ossl_statem_client_write_transition().
415 switch (st
->hand_state
) {
417 /* Shouldn't happen */
418 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
419 return WRITE_TRAN_ERROR
;
421 case TLS_ST_CR_CERT_REQ
:
422 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
423 st
->hand_state
= TLS_ST_CW_CERT
;
424 return WRITE_TRAN_CONTINUE
;
427 * We should only get here if we received a CertificateRequest after
428 * we already sent close_notify
430 if (!ossl_assert((s
->shutdown
& SSL_SENT_SHUTDOWN
) != 0)) {
431 /* Shouldn't happen - same as default case */
432 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
433 return WRITE_TRAN_ERROR
;
435 st
->hand_state
= TLS_ST_OK
;
436 return WRITE_TRAN_CONTINUE
;
438 case TLS_ST_CR_FINISHED
:
439 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
440 || s
->early_data_state
== SSL_EARLY_DATA_FINISHED_WRITING
)
441 st
->hand_state
= TLS_ST_PENDING_EARLY_DATA_END
;
442 else if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
443 && s
->hello_retry_request
== SSL_HRR_NONE
)
444 st
->hand_state
= TLS_ST_CW_CHANGE
;
446 st
->hand_state
= (s
->s3
.tmp
.cert_req
!= 0) ? TLS_ST_CW_CERT
447 : TLS_ST_CW_FINISHED
;
448 return WRITE_TRAN_CONTINUE
;
450 case TLS_ST_PENDING_EARLY_DATA_END
:
451 if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
452 st
->hand_state
= TLS_ST_CW_END_OF_EARLY_DATA
;
453 return WRITE_TRAN_CONTINUE
;
457 case TLS_ST_CW_END_OF_EARLY_DATA
:
458 case TLS_ST_CW_CHANGE
:
459 st
->hand_state
= (s
->s3
.tmp
.cert_req
!= 0) ? TLS_ST_CW_CERT
460 : TLS_ST_CW_FINISHED
;
461 return WRITE_TRAN_CONTINUE
;
464 /* If a non-empty Certificate we also send CertificateVerify */
465 st
->hand_state
= (s
->s3
.tmp
.cert_req
== 1) ? TLS_ST_CW_CERT_VRFY
466 : TLS_ST_CW_FINISHED
;
467 return WRITE_TRAN_CONTINUE
;
469 case TLS_ST_CW_CERT_VRFY
:
470 st
->hand_state
= TLS_ST_CW_FINISHED
;
471 return WRITE_TRAN_CONTINUE
;
473 case TLS_ST_CR_KEY_UPDATE
:
474 case TLS_ST_CW_KEY_UPDATE
:
475 case TLS_ST_CR_SESSION_TICKET
:
476 case TLS_ST_CW_FINISHED
:
477 st
->hand_state
= TLS_ST_OK
;
478 return WRITE_TRAN_CONTINUE
;
481 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
482 st
->hand_state
= TLS_ST_CW_KEY_UPDATE
;
483 return WRITE_TRAN_CONTINUE
;
486 /* Try to read from the server instead */
487 return WRITE_TRAN_FINISHED
;
492 * ossl_statem_client_write_transition() works out what handshake state to
493 * move to next when the client is writing messages to be sent to the server.
495 WRITE_TRAN
ossl_statem_client_write_transition(SSL
*s
)
497 OSSL_STATEM
*st
= &s
->statem
;
500 * Note that immediately before/after a ClientHello we don't know what
501 * version we are going to negotiate yet, so we don't take this branch until
505 return ossl_statem_client13_write_transition(s
);
507 switch (st
->hand_state
) {
509 /* Shouldn't happen */
510 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
511 return WRITE_TRAN_ERROR
;
514 if (!s
->renegotiate
) {
516 * We haven't requested a renegotiation ourselves so we must have
517 * received a message from the server. Better read it.
519 return WRITE_TRAN_FINISHED
;
524 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
525 return WRITE_TRAN_CONTINUE
;
527 case TLS_ST_CW_CLNT_HELLO
:
528 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
) {
530 * We are assuming this is a TLSv1.3 connection, although we haven't
531 * actually selected a version yet.
533 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0)
534 st
->hand_state
= TLS_ST_CW_CHANGE
;
536 st
->hand_state
= TLS_ST_EARLY_DATA
;
537 return WRITE_TRAN_CONTINUE
;
540 * No transition at the end of writing because we don't know what
543 return WRITE_TRAN_FINISHED
;
545 case TLS_ST_CR_SRVR_HELLO
:
547 * We only get here in TLSv1.3. We just received an HRR, so issue a
548 * CCS unless middlebox compat mode is off, or we already issued one
549 * because we did early data.
551 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0
552 && s
->early_data_state
!= SSL_EARLY_DATA_FINISHED_WRITING
)
553 st
->hand_state
= TLS_ST_CW_CHANGE
;
555 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
556 return WRITE_TRAN_CONTINUE
;
558 case TLS_ST_EARLY_DATA
:
559 return WRITE_TRAN_FINISHED
;
561 case DTLS_ST_CR_HELLO_VERIFY_REQUEST
:
562 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
563 return WRITE_TRAN_CONTINUE
;
565 case TLS_ST_CR_SRVR_DONE
:
566 if (s
->s3
.tmp
.cert_req
)
567 st
->hand_state
= TLS_ST_CW_CERT
;
569 st
->hand_state
= TLS_ST_CW_KEY_EXCH
;
570 return WRITE_TRAN_CONTINUE
;
573 st
->hand_state
= TLS_ST_CW_KEY_EXCH
;
574 return WRITE_TRAN_CONTINUE
;
576 case TLS_ST_CW_KEY_EXCH
:
578 * For TLS, cert_req is set to 2, so a cert chain of nothing is
579 * sent, but no verify packet is sent
582 * XXX: For now, we do not support client authentication in ECDH
583 * cipher suites with ECDH (rather than ECDSA) certificates. We
584 * need to skip the certificate verify message when client's
585 * ECDH public key is sent inside the client certificate.
587 if (s
->s3
.tmp
.cert_req
== 1) {
588 st
->hand_state
= TLS_ST_CW_CERT_VRFY
;
590 st
->hand_state
= TLS_ST_CW_CHANGE
;
592 if (s
->s3
.flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
593 st
->hand_state
= TLS_ST_CW_CHANGE
;
595 return WRITE_TRAN_CONTINUE
;
597 case TLS_ST_CW_CERT_VRFY
:
598 st
->hand_state
= TLS_ST_CW_CHANGE
;
599 return WRITE_TRAN_CONTINUE
;
601 case TLS_ST_CW_CHANGE
:
602 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
603 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
604 } else if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
) {
605 st
->hand_state
= TLS_ST_EARLY_DATA
;
607 #if defined(OPENSSL_NO_NEXTPROTONEG)
608 st
->hand_state
= TLS_ST_CW_FINISHED
;
610 if (!SSL_IS_DTLS(s
) && s
->s3
.npn_seen
)
611 st
->hand_state
= TLS_ST_CW_NEXT_PROTO
;
613 st
->hand_state
= TLS_ST_CW_FINISHED
;
616 return WRITE_TRAN_CONTINUE
;
618 #if !defined(OPENSSL_NO_NEXTPROTONEG)
619 case TLS_ST_CW_NEXT_PROTO
:
620 st
->hand_state
= TLS_ST_CW_FINISHED
;
621 return WRITE_TRAN_CONTINUE
;
624 case TLS_ST_CW_FINISHED
:
626 st
->hand_state
= TLS_ST_OK
;
627 return WRITE_TRAN_CONTINUE
;
629 return WRITE_TRAN_FINISHED
;
632 case TLS_ST_CR_FINISHED
:
634 st
->hand_state
= TLS_ST_CW_CHANGE
;
635 return WRITE_TRAN_CONTINUE
;
637 st
->hand_state
= TLS_ST_OK
;
638 return WRITE_TRAN_CONTINUE
;
641 case TLS_ST_CR_HELLO_REQ
:
643 * If we can renegotiate now then do so, otherwise wait for a more
646 if (ssl3_renegotiate_check(s
, 1)) {
647 if (!tls_setup_handshake(s
)) {
648 /* SSLfatal() already called */
649 return WRITE_TRAN_ERROR
;
651 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
652 return WRITE_TRAN_CONTINUE
;
654 st
->hand_state
= TLS_ST_OK
;
655 return WRITE_TRAN_CONTINUE
;
660 * Perform any pre work that needs to be done prior to sending a message from
661 * the client to the server.
663 WORK_STATE
ossl_statem_client_pre_work(SSL
*s
, WORK_STATE wst
)
665 OSSL_STATEM
*st
= &s
->statem
;
667 switch (st
->hand_state
) {
669 /* No pre work to be done */
672 case TLS_ST_CW_CLNT_HELLO
:
674 if (SSL_IS_DTLS(s
)) {
675 /* every DTLS ClientHello resets Finished MAC */
676 if (!ssl3_init_finished_mac(s
)) {
677 /* SSLfatal() already called */
683 case TLS_ST_CW_CHANGE
:
684 if (SSL_IS_DTLS(s
)) {
687 * We're into the last flight so we don't retransmit these
688 * messages unless we need to.
692 #ifndef OPENSSL_NO_SCTP
693 if (BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
694 /* Calls SSLfatal() as required */
695 return dtls_wait_for_dry(s
);
701 case TLS_ST_PENDING_EARLY_DATA_END
:
703 * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
704 * attempt to write early data before calling SSL_read() then we press
705 * on with the handshake. Otherwise we pause here.
707 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_WRITING
708 || s
->early_data_state
== SSL_EARLY_DATA_NONE
)
709 return WORK_FINISHED_CONTINUE
;
712 case TLS_ST_EARLY_DATA
:
713 return tls_finish_handshake(s
, wst
, 0, 1);
716 /* Calls SSLfatal() as required */
717 return tls_finish_handshake(s
, wst
, 1, 1);
720 return WORK_FINISHED_CONTINUE
;
724 * Perform any work that needs to be done after sending a message from the
725 * client to the server.
727 WORK_STATE
ossl_statem_client_post_work(SSL
*s
, WORK_STATE wst
)
729 OSSL_STATEM
*st
= &s
->statem
;
733 switch (st
->hand_state
) {
735 /* No post work to be done */
738 case TLS_ST_CW_CLNT_HELLO
:
739 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
740 && s
->max_early_data
> 0) {
742 * We haven't selected TLSv1.3 yet so we don't call the change
743 * cipher state function associated with the SSL_METHOD. Instead
744 * we call tls13_change_cipher_state() directly.
746 if ((s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) == 0) {
747 if (!tls13_change_cipher_state(s
,
748 SSL3_CC_EARLY
| SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
749 /* SSLfatal() already called */
753 /* else we're in compat mode so we delay flushing until after CCS */
754 } else if (!statem_flush(s
)) {
758 if (SSL_IS_DTLS(s
)) {
759 /* Treat the next message as the first packet */
764 case TLS_ST_CW_END_OF_EARLY_DATA
:
766 * We set the enc_write_ctx back to NULL because we may end up writing
767 * in cleartext again if we get a HelloRetryRequest from the server.
769 EVP_CIPHER_CTX_free(s
->enc_write_ctx
);
770 s
->enc_write_ctx
= NULL
;
773 case TLS_ST_CW_KEY_EXCH
:
774 if (tls_client_key_exchange_post_work(s
) == 0) {
775 /* SSLfatal() already called */
780 case TLS_ST_CW_CHANGE
:
781 if (SSL_IS_TLS13(s
) || s
->hello_retry_request
== SSL_HRR_PENDING
)
783 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
784 && s
->max_early_data
> 0) {
786 * We haven't selected TLSv1.3 yet so we don't call the change
787 * cipher state function associated with the SSL_METHOD. Instead
788 * we call tls13_change_cipher_state() directly.
790 if (!tls13_change_cipher_state(s
,
791 SSL3_CC_EARLY
| SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
795 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
796 #ifdef OPENSSL_NO_COMP
797 s
->session
->compress_meth
= 0;
799 if (s
->s3
.tmp
.new_compression
== NULL
)
800 s
->session
->compress_meth
= 0;
802 s
->session
->compress_meth
= s
->s3
.tmp
.new_compression
->id
;
804 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
805 /* SSLfatal() already called */
809 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
810 SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
811 /* SSLfatal() already called */
815 if (SSL_IS_DTLS(s
)) {
816 #ifndef OPENSSL_NO_SCTP
819 * Change to new shared key of SCTP-Auth, will be ignored if
822 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
827 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
831 case TLS_ST_CW_FINISHED
:
832 #ifndef OPENSSL_NO_SCTP
833 if (wst
== WORK_MORE_A
&& SSL_IS_DTLS(s
) && s
->hit
== 0) {
835 * Change to new shared key of SCTP-Auth, will be ignored if
838 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
842 if (statem_flush(s
) != 1)
845 if (SSL_IS_TLS13(s
)) {
846 if (!tls13_save_handshake_digest_for_pha(s
)) {
847 /* SSLfatal() already called */
850 if (s
->post_handshake_auth
!= SSL_PHA_REQUESTED
) {
851 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
852 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
853 /* SSLfatal() already called */
860 case TLS_ST_CW_KEY_UPDATE
:
861 if (statem_flush(s
) != 1)
863 if (!tls13_update_key(s
, 1)) {
864 /* SSLfatal() already called */
870 return WORK_FINISHED_CONTINUE
;
874 * Get the message construction function and message type for sending from the
877 * Valid return values are:
881 int ossl_statem_client_construct_message(SSL
*s
, WPACKET
*pkt
,
882 confunc_f
*confunc
, int *mt
)
884 OSSL_STATEM
*st
= &s
->statem
;
886 switch (st
->hand_state
) {
888 /* Shouldn't happen */
889 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_STATE
);
892 case TLS_ST_CW_CHANGE
:
894 *confunc
= dtls_construct_change_cipher_spec
;
896 *confunc
= tls_construct_change_cipher_spec
;
897 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
900 case TLS_ST_CW_CLNT_HELLO
:
901 *confunc
= tls_construct_client_hello
;
902 *mt
= SSL3_MT_CLIENT_HELLO
;
905 case TLS_ST_CW_END_OF_EARLY_DATA
:
906 *confunc
= tls_construct_end_of_early_data
;
907 *mt
= SSL3_MT_END_OF_EARLY_DATA
;
910 case TLS_ST_PENDING_EARLY_DATA_END
:
916 *confunc
= tls_construct_client_certificate
;
917 *mt
= SSL3_MT_CERTIFICATE
;
920 case TLS_ST_CW_KEY_EXCH
:
921 *confunc
= tls_construct_client_key_exchange
;
922 *mt
= SSL3_MT_CLIENT_KEY_EXCHANGE
;
925 case TLS_ST_CW_CERT_VRFY
:
926 *confunc
= tls_construct_cert_verify
;
927 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
930 #if !defined(OPENSSL_NO_NEXTPROTONEG)
931 case TLS_ST_CW_NEXT_PROTO
:
932 *confunc
= tls_construct_next_proto
;
933 *mt
= SSL3_MT_NEXT_PROTO
;
936 case TLS_ST_CW_FINISHED
:
937 *confunc
= tls_construct_finished
;
938 *mt
= SSL3_MT_FINISHED
;
941 case TLS_ST_CW_KEY_UPDATE
:
942 *confunc
= tls_construct_key_update
;
943 *mt
= SSL3_MT_KEY_UPDATE
;
951 * Returns the maximum allowed length for the current message that we are
952 * reading. Excludes the message header.
954 size_t ossl_statem_client_max_message_size(SSL
*s
)
956 OSSL_STATEM
*st
= &s
->statem
;
958 switch (st
->hand_state
) {
960 /* Shouldn't happen */
963 case TLS_ST_CR_SRVR_HELLO
:
964 return SERVER_HELLO_MAX_LENGTH
;
966 case DTLS_ST_CR_HELLO_VERIFY_REQUEST
:
967 return HELLO_VERIFY_REQUEST_MAX_LENGTH
;
970 return s
->max_cert_list
;
972 case TLS_ST_CR_CERT_VRFY
:
973 return SSL3_RT_MAX_PLAIN_LENGTH
;
975 case TLS_ST_CR_CERT_STATUS
:
976 return SSL3_RT_MAX_PLAIN_LENGTH
;
978 case TLS_ST_CR_KEY_EXCH
:
979 return SERVER_KEY_EXCH_MAX_LENGTH
;
981 case TLS_ST_CR_CERT_REQ
:
983 * Set to s->max_cert_list for compatibility with previous releases. In
984 * practice these messages can get quite long if servers are configured
985 * to provide a long list of acceptable CAs
987 return s
->max_cert_list
;
989 case TLS_ST_CR_SRVR_DONE
:
990 return SERVER_HELLO_DONE_MAX_LENGTH
;
992 case TLS_ST_CR_CHANGE
:
993 if (s
->version
== DTLS1_BAD_VER
)
995 return CCS_MAX_LENGTH
;
997 case TLS_ST_CR_SESSION_TICKET
:
998 return SSL3_RT_MAX_PLAIN_LENGTH
;
1000 case TLS_ST_CR_FINISHED
:
1001 return FINISHED_MAX_LENGTH
;
1003 case TLS_ST_CR_ENCRYPTED_EXTENSIONS
:
1004 return ENCRYPTED_EXTENSIONS_MAX_LENGTH
;
1006 case TLS_ST_CR_KEY_UPDATE
:
1007 return KEY_UPDATE_MAX_LENGTH
;
1012 * Process a message that the client has received from the server.
1014 MSG_PROCESS_RETURN
ossl_statem_client_process_message(SSL
*s
, PACKET
*pkt
)
1016 OSSL_STATEM
*st
= &s
->statem
;
1018 switch (st
->hand_state
) {
1020 /* Shouldn't happen */
1021 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1022 return MSG_PROCESS_ERROR
;
1024 case TLS_ST_CR_SRVR_HELLO
:
1025 return tls_process_server_hello(s
, pkt
);
1027 case DTLS_ST_CR_HELLO_VERIFY_REQUEST
:
1028 return dtls_process_hello_verify(s
, pkt
);
1030 case TLS_ST_CR_CERT
:
1031 return tls_process_server_certificate(s
, pkt
);
1033 case TLS_ST_CR_CERT_VRFY
:
1034 return tls_process_cert_verify(s
, pkt
);
1036 case TLS_ST_CR_CERT_STATUS
:
1037 return tls_process_cert_status(s
, pkt
);
1039 case TLS_ST_CR_KEY_EXCH
:
1040 return tls_process_key_exchange(s
, pkt
);
1042 case TLS_ST_CR_CERT_REQ
:
1043 return tls_process_certificate_request(s
, pkt
);
1045 case TLS_ST_CR_SRVR_DONE
:
1046 return tls_process_server_done(s
, pkt
);
1048 case TLS_ST_CR_CHANGE
:
1049 return tls_process_change_cipher_spec(s
, pkt
);
1051 case TLS_ST_CR_SESSION_TICKET
:
1052 return tls_process_new_session_ticket(s
, pkt
);
1054 case TLS_ST_CR_FINISHED
:
1055 return tls_process_finished(s
, pkt
);
1057 case TLS_ST_CR_HELLO_REQ
:
1058 return tls_process_hello_req(s
, pkt
);
1060 case TLS_ST_CR_ENCRYPTED_EXTENSIONS
:
1061 return tls_process_encrypted_extensions(s
, pkt
);
1063 case TLS_ST_CR_KEY_UPDATE
:
1064 return tls_process_key_update(s
, pkt
);
1069 * Perform any further processing required following the receipt of a message
1072 WORK_STATE
ossl_statem_client_post_process_message(SSL
*s
, WORK_STATE wst
)
1074 OSSL_STATEM
*st
= &s
->statem
;
1076 switch (st
->hand_state
) {
1078 /* Shouldn't happen */
1079 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1082 case TLS_ST_CR_CERT
:
1083 return tls_post_process_server_certificate(s
, wst
);
1085 case TLS_ST_CR_CERT_VRFY
:
1086 case TLS_ST_CR_CERT_REQ
:
1087 return tls_prepare_client_certificate(s
, wst
);
1091 int tls_construct_client_hello(SSL
*s
, WPACKET
*pkt
)
1096 #ifndef OPENSSL_NO_COMP
1099 SSL_SESSION
*sess
= s
->session
;
1100 unsigned char *session_id
;
1102 /* Work out what SSL/TLS/DTLS version to use */
1103 protverr
= ssl_set_client_hello_version(s
);
1104 if (protverr
!= 0) {
1105 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, protverr
);
1110 || !ssl_version_supported(s
, sess
->ssl_version
, NULL
)
1111 || !SSL_SESSION_is_resumable(sess
)) {
1112 if (s
->hello_retry_request
== SSL_HRR_NONE
1113 && !ssl_get_new_session(s
, 0)) {
1114 /* SSLfatal() already called */
1118 /* else use the pre-loaded session */
1120 p
= s
->s3
.client_random
;
1123 * for DTLS if client_random is initialized, reuse it, we are
1124 * required to use same upon reply to HelloVerify
1126 if (SSL_IS_DTLS(s
)) {
1129 for (idx
= 0; idx
< sizeof(s
->s3
.client_random
); idx
++) {
1136 i
= (s
->hello_retry_request
== SSL_HRR_NONE
);
1139 if (i
&& ssl_fill_hello_random(s
, 0, p
, sizeof(s
->s3
.client_random
),
1140 DOWNGRADE_NONE
) <= 0) {
1141 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1146 * version indicates the negotiated version: for example from
1147 * an SSLv2/v3 compatible client hello). The client_version
1148 * field is the maximum version we permit and it is also
1149 * used in RSA encrypted premaster secrets. Some servers can
1150 * choke if we initially report a higher version then
1151 * renegotiate to a lower one in the premaster secret. This
1152 * didn't happen with TLS 1.0 as most servers supported it
1153 * but it can with TLS 1.1 or later if the server only supports
1156 * Possible scenario with previous logic:
1157 * 1. Client hello indicates TLS 1.2
1158 * 2. Server hello says TLS 1.0
1159 * 3. RSA encrypted premaster secret uses 1.2.
1160 * 4. Handshake proceeds using TLS 1.0.
1161 * 5. Server sends hello request to renegotiate.
1162 * 6. Client hello indicates TLS v1.0 as we now
1163 * know that is maximum server supports.
1164 * 7. Server chokes on RSA encrypted premaster secret
1165 * containing version 1.0.
1167 * For interoperability it should be OK to always use the
1168 * maximum version we support in client hello and then rely
1169 * on the checking of version to ensure the servers isn't
1170 * being inconsistent: for example initially negotiating with
1171 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
1172 * client_version in client hello and not resetting it to
1173 * the negotiated version.
1175 * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
1176 * supported_versions extension for the real supported versions.
1178 if (!WPACKET_put_bytes_u16(pkt
, s
->client_version
)
1179 || !WPACKET_memcpy(pkt
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
)) {
1180 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1185 session_id
= s
->session
->session_id
;
1186 if (s
->new_session
|| s
->session
->ssl_version
== TLS1_3_VERSION
) {
1187 if (s
->version
== TLS1_3_VERSION
1188 && (s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0) {
1189 sess_id_len
= sizeof(s
->tmp_session_id
);
1190 s
->tmp_session_id_len
= sess_id_len
;
1191 session_id
= s
->tmp_session_id
;
1192 if (s
->hello_retry_request
== SSL_HRR_NONE
1193 && RAND_bytes_ex(s
->ctx
->libctx
, s
->tmp_session_id
,
1194 sess_id_len
) <= 0) {
1195 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1202 assert(s
->session
->session_id_length
<= sizeof(s
->session
->session_id
));
1203 sess_id_len
= s
->session
->session_id_length
;
1204 if (s
->version
== TLS1_3_VERSION
) {
1205 s
->tmp_session_id_len
= sess_id_len
;
1206 memcpy(s
->tmp_session_id
, s
->session
->session_id
, sess_id_len
);
1209 if (!WPACKET_start_sub_packet_u8(pkt
)
1210 || (sess_id_len
!= 0 && !WPACKET_memcpy(pkt
, session_id
,
1212 || !WPACKET_close(pkt
)) {
1213 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1217 /* cookie stuff for DTLS */
1218 if (SSL_IS_DTLS(s
)) {
1219 if (s
->d1
->cookie_len
> sizeof(s
->d1
->cookie
)
1220 || !WPACKET_sub_memcpy_u8(pkt
, s
->d1
->cookie
,
1221 s
->d1
->cookie_len
)) {
1222 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1227 /* Ciphers supported */
1228 if (!WPACKET_start_sub_packet_u16(pkt
)) {
1229 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1233 if (!ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), pkt
)) {
1234 /* SSLfatal() already called */
1237 if (!WPACKET_close(pkt
)) {
1238 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1243 if (!WPACKET_start_sub_packet_u8(pkt
)) {
1244 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1247 #ifndef OPENSSL_NO_COMP
1248 if (ssl_allow_compression(s
)
1249 && s
->ctx
->comp_methods
1250 && (SSL_IS_DTLS(s
) || s
->s3
.tmp
.max_ver
< TLS1_3_VERSION
)) {
1251 int compnum
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1252 for (i
= 0; i
< compnum
; i
++) {
1253 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, i
);
1254 if (!WPACKET_put_bytes_u8(pkt
, comp
->id
)) {
1255 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1261 /* Add the NULL method */
1262 if (!WPACKET_put_bytes_u8(pkt
, 0) || !WPACKET_close(pkt
)) {
1263 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1267 /* TLS extensions */
1268 if (!tls_construct_extensions(s
, pkt
, SSL_EXT_CLIENT_HELLO
, NULL
, 0)) {
1269 /* SSLfatal() already called */
1276 MSG_PROCESS_RETURN
dtls_process_hello_verify(SSL
*s
, PACKET
*pkt
)
1281 if (!PACKET_forward(pkt
, 2)
1282 || !PACKET_get_length_prefixed_1(pkt
, &cookiepkt
)) {
1283 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1284 return MSG_PROCESS_ERROR
;
1287 cookie_len
= PACKET_remaining(&cookiepkt
);
1288 if (cookie_len
> sizeof(s
->d1
->cookie
)) {
1289 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_LENGTH_TOO_LONG
);
1290 return MSG_PROCESS_ERROR
;
1293 if (!PACKET_copy_bytes(&cookiepkt
, s
->d1
->cookie
, cookie_len
)) {
1294 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1295 return MSG_PROCESS_ERROR
;
1297 s
->d1
->cookie_len
= cookie_len
;
1299 return MSG_PROCESS_FINISHED_READING
;
1302 static int set_client_ciphersuite(SSL
*s
, const unsigned char *cipherchars
)
1304 STACK_OF(SSL_CIPHER
) *sk
;
1305 const SSL_CIPHER
*c
;
1308 c
= ssl_get_cipher_by_char(s
, cipherchars
, 0);
1310 /* unknown cipher */
1311 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
1315 * If it is a disabled cipher we either didn't send it in client hello,
1316 * or it's not allowed for the selected protocol. So we return an error.
1318 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_CHECK
, 1)) {
1319 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_WRONG_CIPHER_RETURNED
);
1323 sk
= ssl_get_ciphers_by_id(s
);
1324 i
= sk_SSL_CIPHER_find(sk
, c
);
1326 /* we did not say we would use this cipher */
1327 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_WRONG_CIPHER_RETURNED
);
1331 if (SSL_IS_TLS13(s
) && s
->s3
.tmp
.new_cipher
!= NULL
1332 && s
->s3
.tmp
.new_cipher
->id
!= c
->id
) {
1333 /* ServerHello selected a different ciphersuite to that in the HRR */
1334 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_WRONG_CIPHER_RETURNED
);
1339 * Depending on the session caching (internal/external), the cipher
1340 * and/or cipher_id values may not be set. Make sure that cipher_id is
1341 * set and use it for comparison.
1343 if (s
->session
->cipher
!= NULL
)
1344 s
->session
->cipher_id
= s
->session
->cipher
->id
;
1345 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
1346 if (SSL_IS_TLS13(s
)) {
1348 * In TLSv1.3 it is valid for the server to select a different
1349 * ciphersuite as long as the hash is the same.
1351 if (ssl_md(s
->ctx
, c
->algorithm2
)
1352 != ssl_md(s
->ctx
, s
->session
->cipher
->algorithm2
)) {
1353 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1354 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED
);
1359 * Prior to TLSv1.3 resuming a session always meant using the same
1362 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1363 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
1367 s
->s3
.tmp
.new_cipher
= c
;
1372 MSG_PROCESS_RETURN
tls_process_server_hello(SSL
*s
, PACKET
*pkt
)
1374 PACKET session_id
, extpkt
;
1375 size_t session_id_len
;
1376 const unsigned char *cipherchars
;
1378 unsigned int compression
;
1379 unsigned int sversion
;
1380 unsigned int context
;
1381 RAW_EXTENSION
*extensions
= NULL
;
1382 #ifndef OPENSSL_NO_COMP
1386 if (!PACKET_get_net_2(pkt
, &sversion
)) {
1387 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1391 /* load the server random */
1392 if (s
->version
== TLS1_3_VERSION
1393 && sversion
== TLS1_2_VERSION
1394 && PACKET_remaining(pkt
) >= SSL3_RANDOM_SIZE
1395 && memcmp(hrrrandom
, PACKET_data(pkt
), SSL3_RANDOM_SIZE
) == 0) {
1396 s
->hello_retry_request
= SSL_HRR_PENDING
;
1398 if (!PACKET_forward(pkt
, SSL3_RANDOM_SIZE
)) {
1399 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1403 if (!PACKET_copy_bytes(pkt
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
)) {
1404 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1409 /* Get the session-id. */
1410 if (!PACKET_get_length_prefixed_1(pkt
, &session_id
)) {
1411 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1414 session_id_len
= PACKET_remaining(&session_id
);
1415 if (session_id_len
> sizeof(s
->session
->session_id
)
1416 || session_id_len
> SSL3_SESSION_ID_SIZE
) {
1417 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
1421 if (!PACKET_get_bytes(pkt
, &cipherchars
, TLS_CIPHER_LEN
)) {
1422 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1426 if (!PACKET_get_1(pkt
, &compression
)) {
1427 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1431 /* TLS extensions */
1432 if (PACKET_remaining(pkt
) == 0 && !hrr
) {
1433 PACKET_null_init(&extpkt
);
1434 } else if (!PACKET_as_length_prefixed_2(pkt
, &extpkt
)
1435 || PACKET_remaining(pkt
) != 0) {
1436 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
1441 if (!tls_collect_extensions(s
, &extpkt
,
1442 SSL_EXT_TLS1_2_SERVER_HELLO
1443 | SSL_EXT_TLS1_3_SERVER_HELLO
,
1444 &extensions
, NULL
, 1)) {
1445 /* SSLfatal() already called */
1449 if (!ssl_choose_client_version(s
, sversion
, extensions
)) {
1450 /* SSLfatal() already called */
1455 if (SSL_IS_TLS13(s
) || hrr
) {
1456 if (compression
!= 0) {
1457 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1458 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1462 if (session_id_len
!= s
->tmp_session_id_len
1463 || memcmp(PACKET_data(&session_id
), s
->tmp_session_id
,
1464 session_id_len
) != 0) {
1465 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_INVALID_SESSION_ID
);
1471 if (!set_client_ciphersuite(s
, cipherchars
)) {
1472 /* SSLfatal() already called */
1476 return tls_process_as_hello_retry_request(s
, &extpkt
);
1480 * Now we have chosen the version we need to check again that the extensions
1481 * are appropriate for this version.
1483 context
= SSL_IS_TLS13(s
) ? SSL_EXT_TLS1_3_SERVER_HELLO
1484 : SSL_EXT_TLS1_2_SERVER_HELLO
;
1485 if (!tls_validate_all_contexts(s
, context
, extensions
)) {
1486 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_EXTENSION
);
1492 if (SSL_IS_TLS13(s
)) {
1494 * In TLSv1.3 a ServerHello message signals a key change so the end of
1495 * the message must be on a record boundary.
1497 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
1498 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1499 SSL_R_NOT_ON_RECORD_BOUNDARY
);
1503 /* This will set s->hit if we are resuming */
1504 if (!tls_parse_extension(s
, TLSEXT_IDX_psk
,
1505 SSL_EXT_TLS1_3_SERVER_HELLO
,
1506 extensions
, NULL
, 0)) {
1507 /* SSLfatal() already called */
1512 * Check if we can resume the session based on external pre-shared
1513 * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
1514 * Resumption based on server-side state works with session IDs.
1515 * Resumption based on pre-shared Protected Access Credentials (PACs)
1516 * works by overriding the SessionTicket extension at the application
1517 * layer, and does not send a session ID. (We do not know whether
1518 * EAP-FAST servers would honour the session ID.) Therefore, the session
1519 * ID alone is not a reliable indicator of session resumption, so we
1520 * first check if we can resume, and later peek at the next handshake
1521 * message to see if the server wants to resume.
1523 if (s
->version
>= TLS1_VERSION
1524 && s
->ext
.session_secret_cb
!= NULL
&& s
->session
->ext
.tick
) {
1525 const SSL_CIPHER
*pref_cipher
= NULL
;
1527 * s->session->master_key_length is a size_t, but this is an int for
1528 * backwards compat reasons
1530 int master_key_length
;
1531 master_key_length
= sizeof(s
->session
->master_key
);
1532 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1535 s
->ext
.session_secret_cb_arg
)
1536 && master_key_length
> 0) {
1537 s
->session
->master_key_length
= master_key_length
;
1538 s
->session
->cipher
= pref_cipher
?
1539 pref_cipher
: ssl_get_cipher_by_char(s
, cipherchars
, 0);
1541 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1546 if (session_id_len
!= 0
1547 && session_id_len
== s
->session
->session_id_length
1548 && memcmp(PACKET_data(&session_id
), s
->session
->session_id
,
1549 session_id_len
) == 0)
1554 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
1555 || memcmp(s
->session
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
)) {
1556 /* actually a client application bug */
1557 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1558 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
1563 * If we were trying for session-id reuse but the server
1564 * didn't resume, make a new SSL_SESSION.
1565 * In the case of EAP-FAST and PAC, we do not send a session ID,
1566 * so the PAC-based session secret is always preserved. It'll be
1567 * overwritten if the server refuses resumption.
1569 if (s
->session
->session_id_length
> 0) {
1570 tsan_counter(&s
->session_ctx
->stats
.sess_miss
);
1571 if (!ssl_get_new_session(s
, 0)) {
1572 /* SSLfatal() already called */
1577 s
->session
->ssl_version
= s
->version
;
1579 * In TLSv1.2 and below we save the session id we were sent so we can
1580 * resume it later. In TLSv1.3 the session id we were sent is just an
1581 * echo of what we originally sent in the ClientHello and should not be
1582 * used for resumption.
1584 if (!SSL_IS_TLS13(s
)) {
1585 s
->session
->session_id_length
= session_id_len
;
1586 /* session_id_len could be 0 */
1587 if (session_id_len
> 0)
1588 memcpy(s
->session
->session_id
, PACKET_data(&session_id
),
1593 /* Session version and negotiated protocol version should match */
1594 if (s
->version
!= s
->session
->ssl_version
) {
1595 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
1596 SSL_R_SSL_SESSION_VERSION_MISMATCH
);
1600 * Now that we know the version, update the check to see if it's an allowed
1603 s
->s3
.tmp
.min_ver
= s
->version
;
1604 s
->s3
.tmp
.max_ver
= s
->version
;
1606 if (!set_client_ciphersuite(s
, cipherchars
)) {
1607 /* SSLfatal() already called */
1611 #ifdef OPENSSL_NO_COMP
1612 if (compression
!= 0) {
1613 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1614 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1618 * If compression is disabled we'd better not try to resume a session
1619 * using compression.
1621 if (s
->session
->compress_meth
!= 0) {
1622 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_INCONSISTENT_COMPRESSION
);
1626 if (s
->hit
&& compression
!= s
->session
->compress_meth
) {
1627 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1628 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED
);
1631 if (compression
== 0)
1633 else if (!ssl_allow_compression(s
)) {
1634 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_COMPRESSION_DISABLED
);
1637 comp
= ssl3_comp_find(s
->ctx
->comp_methods
, compression
);
1640 if (compression
!= 0 && comp
== NULL
) {
1641 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1642 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1645 s
->s3
.tmp
.new_compression
= comp
;
1649 if (!tls_parse_all_extensions(s
, context
, extensions
, NULL
, 0, 1)) {
1650 /* SSLfatal() already called */
1654 #ifndef OPENSSL_NO_SCTP
1655 if (SSL_IS_DTLS(s
) && s
->hit
) {
1656 unsigned char sctpauthkey
[64];
1657 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
1661 * Add new shared key for SCTP-Auth, will be ignored if
1664 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
1665 sizeof(DTLS1_SCTP_AUTH_LABEL
));
1667 /* Don't include the terminating zero. */
1668 labellen
= sizeof(labelbuffer
) - 1;
1669 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
1672 if (SSL_export_keying_material(s
, sctpauthkey
,
1673 sizeof(sctpauthkey
),
1675 labellen
, NULL
, 0, 0) <= 0) {
1676 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1680 BIO_ctrl(SSL_get_wbio(s
),
1681 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
1682 sizeof(sctpauthkey
), sctpauthkey
);
1687 * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1688 * we're done with this message
1691 && (!s
->method
->ssl3_enc
->setup_key_block(s
)
1692 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
1693 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_CLIENT_READ
))) {
1694 /* SSLfatal() already called */
1698 OPENSSL_free(extensions
);
1699 return MSG_PROCESS_CONTINUE_READING
;
1701 OPENSSL_free(extensions
);
1702 return MSG_PROCESS_ERROR
;
1705 static MSG_PROCESS_RETURN
tls_process_as_hello_retry_request(SSL
*s
,
1708 RAW_EXTENSION
*extensions
= NULL
;
1711 * If we were sending early_data then the enc_write_ctx is now invalid and
1712 * should not be used.
1714 EVP_CIPHER_CTX_free(s
->enc_write_ctx
);
1715 s
->enc_write_ctx
= NULL
;
1717 if (!tls_collect_extensions(s
, extpkt
, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
,
1718 &extensions
, NULL
, 1)
1719 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
,
1720 extensions
, NULL
, 0, 1)) {
1721 /* SSLfatal() already called */
1725 OPENSSL_free(extensions
);
1728 if (s
->ext
.tls13_cookie_len
== 0
1729 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1730 && s
->s3
.tmp
.pkey
!= NULL
1734 * We didn't receive a cookie or a new key_share so the next
1735 * ClientHello will not change
1737 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_NO_CHANGE_FOLLOWING_HRR
);
1742 * Re-initialise the Transcript Hash. We're going to prepopulate it with
1743 * a synthetic message_hash in place of ClientHello1.
1745 if (!create_synthetic_message_hash(s
, NULL
, 0, NULL
, 0)) {
1746 /* SSLfatal() already called */
1751 * Add this message to the Transcript Hash. Normally this is done
1752 * automatically prior to the message processing stage. However due to the
1753 * need to create the synthetic message hash, we defer that step until now
1756 if (!ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1757 s
->init_num
+ SSL3_HM_HEADER_LENGTH
)) {
1758 /* SSLfatal() already called */
1762 return MSG_PROCESS_FINISHED_READING
;
1764 OPENSSL_free(extensions
);
1765 return MSG_PROCESS_ERROR
;
1768 /* prepare server cert verificaton by setting s->session->peer_chain from pkt */
1769 MSG_PROCESS_RETURN
tls_process_server_certificate(SSL
*s
, PACKET
*pkt
)
1771 unsigned long cert_list_len
, cert_len
;
1773 const unsigned char *certstart
, *certbytes
;
1775 unsigned int context
= 0;
1777 if ((s
->session
->peer_chain
= sk_X509_new_null()) == NULL
) {
1778 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
1782 if ((SSL_IS_TLS13(s
) && !PACKET_get_1(pkt
, &context
))
1784 || !PACKET_get_net_3(pkt
, &cert_list_len
)
1785 || PACKET_remaining(pkt
) != cert_list_len
1786 || PACKET_remaining(pkt
) == 0) {
1787 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1790 for (chainidx
= 0; PACKET_remaining(pkt
); chainidx
++) {
1791 if (!PACKET_get_net_3(pkt
, &cert_len
)
1792 || !PACKET_get_bytes(pkt
, &certbytes
, cert_len
)) {
1793 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
1797 certstart
= certbytes
;
1798 x
= X509_new_ex(s
->ctx
->libctx
, s
->ctx
->propq
);
1800 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_MALLOC_FAILURE
);
1801 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
1804 if (d2i_X509(&x
, (const unsigned char **)&certbytes
,
1805 cert_len
) == NULL
) {
1806 SSLfatal(s
, SSL_AD_BAD_CERTIFICATE
, ERR_R_ASN1_LIB
);
1810 if (certbytes
!= (certstart
+ cert_len
)) {
1811 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CERT_LENGTH_MISMATCH
);
1815 if (SSL_IS_TLS13(s
)) {
1816 RAW_EXTENSION
*rawexts
= NULL
;
1819 if (!PACKET_get_length_prefixed_2(pkt
, &extensions
)) {
1820 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
1823 if (!tls_collect_extensions(s
, &extensions
,
1824 SSL_EXT_TLS1_3_CERTIFICATE
, &rawexts
,
1825 NULL
, chainidx
== 0)
1826 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE
,
1827 rawexts
, x
, chainidx
,
1828 PACKET_remaining(pkt
) == 0)) {
1829 OPENSSL_free(rawexts
);
1830 /* SSLfatal already called */
1833 OPENSSL_free(rawexts
);
1836 if (!sk_X509_push(s
->session
->peer_chain
, x
)) {
1837 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
1842 return MSG_PROCESS_CONTINUE_PROCESSING
;
1846 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
1847 s
->session
->peer_chain
= NULL
;
1848 return MSG_PROCESS_ERROR
;
1852 * Verify the s->session->peer_chain and check server cert type.
1853 * On success set s->session->peer and s->session->verify_result.
1854 * Else the peer certificate verification callback may request retry.
1856 WORK_STATE
tls_post_process_server_certificate(SSL
*s
, WORK_STATE wst
)
1859 EVP_PKEY
*pkey
= NULL
;
1860 const SSL_CERT_LOOKUP
*clu
;
1864 i
= ssl_verify_cert_chain(s
, s
->session
->peer_chain
);
1866 s
->rwstate
= SSL_RETRY_VERIFY
;
1870 * The documented interface is that SSL_VERIFY_PEER should be set in order
1871 * for client side verification of the server certificate to take place.
1872 * However, historically the code has only checked that *any* flag is set
1873 * to cause server verification to take place. Use of the other flags makes
1874 * no sense in client mode. An attempt to clean up the semantics was
1875 * reverted because at least one application *only* set
1876 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1877 * server verification to take place, after the clean up it silently did
1878 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1879 * sent to them because they are void functions. Therefore, we now use the
1880 * (less clean) historic behaviour of performing validation if any flag is
1881 * set. The *documented* interface remains the same.
1883 if (s
->verify_mode
!= SSL_VERIFY_NONE
&& i
<= 0) {
1884 SSLfatal(s
, ssl_x509err2alert(s
->verify_result
),
1885 SSL_R_CERTIFICATE_VERIFY_FAILED
);
1888 ERR_clear_error(); /* but we keep s->verify_result */
1890 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, i
);
1895 * Inconsistency alert: cert_chain does include the peer's certificate,
1896 * which we don't include in statem_srvr.c
1898 x
= sk_X509_value(s
->session
->peer_chain
, 0);
1900 pkey
= X509_get0_pubkey(x
);
1902 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1903 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1904 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1908 if ((clu
= ssl_cert_lookup_by_pkey(pkey
, &certidx
)) == NULL
) {
1909 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1913 * Check certificate type is consistent with ciphersuite. For TLS 1.3
1914 * skip check since TLS 1.3 ciphersuites can be used with any certificate
1917 if (!SSL_IS_TLS13(s
)) {
1918 if ((clu
->amask
& s
->s3
.tmp
.new_cipher
->algorithm_auth
) == 0) {
1919 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_WRONG_CERTIFICATE_TYPE
);
1923 s
->session
->peer_type
= certidx
;
1925 X509_free(s
->session
->peer
);
1927 s
->session
->peer
= x
;
1928 s
->session
->verify_result
= s
->verify_result
;
1930 /* Save the current hash state for when we receive the CertificateVerify */
1932 && !ssl_handshake_hash(s
, s
->cert_verify_hash
,
1933 sizeof(s
->cert_verify_hash
),
1934 &s
->cert_verify_hash_len
)) {
1935 /* SSLfatal() already called */;
1938 return WORK_FINISHED_CONTINUE
;
1941 static int tls_process_ske_psk_preamble(SSL
*s
, PACKET
*pkt
)
1943 #ifndef OPENSSL_NO_PSK
1944 PACKET psk_identity_hint
;
1946 /* PSK ciphersuites are preceded by an identity hint */
1948 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity_hint
)) {
1949 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1954 * Store PSK identity hint for later use, hint is used in
1955 * tls_construct_client_key_exchange. Assume that the maximum length of
1956 * a PSK identity hint can be as long as the maximum length of a PSK
1959 if (PACKET_remaining(&psk_identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
1960 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_DATA_LENGTH_TOO_LONG
);
1964 if (PACKET_remaining(&psk_identity_hint
) == 0) {
1965 OPENSSL_free(s
->session
->psk_identity_hint
);
1966 s
->session
->psk_identity_hint
= NULL
;
1967 } else if (!PACKET_strndup(&psk_identity_hint
,
1968 &s
->session
->psk_identity_hint
)) {
1969 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1975 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1980 static int tls_process_ske_srp(SSL
*s
, PACKET
*pkt
, EVP_PKEY
**pkey
)
1982 #ifndef OPENSSL_NO_SRP
1983 PACKET prime
, generator
, salt
, server_pub
;
1985 if (!PACKET_get_length_prefixed_2(pkt
, &prime
)
1986 || !PACKET_get_length_prefixed_2(pkt
, &generator
)
1987 || !PACKET_get_length_prefixed_1(pkt
, &salt
)
1988 || !PACKET_get_length_prefixed_2(pkt
, &server_pub
)) {
1989 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1993 /* TODO(size_t): Convert BN_bin2bn() calls */
1995 BN_bin2bn(PACKET_data(&prime
),
1996 (int)PACKET_remaining(&prime
), NULL
)) == NULL
1998 BN_bin2bn(PACKET_data(&generator
),
1999 (int)PACKET_remaining(&generator
), NULL
)) == NULL
2001 BN_bin2bn(PACKET_data(&salt
),
2002 (int)PACKET_remaining(&salt
), NULL
)) == NULL
2004 BN_bin2bn(PACKET_data(&server_pub
),
2005 (int)PACKET_remaining(&server_pub
), NULL
)) == NULL
) {
2006 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BN_LIB
);
2010 if (!srp_verify_server_param(s
)) {
2011 /* SSLfatal() already called */
2015 /* We must check if there is a certificate */
2016 if (s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aRSA
| SSL_aDSS
))
2017 *pkey
= X509_get0_pubkey(s
->session
->peer
);
2021 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2026 static int tls_process_ske_dhe(SSL
*s
, PACKET
*pkt
, EVP_PKEY
**pkey
)
2028 PACKET prime
, generator
, pub_key
;
2029 EVP_PKEY
*peer_tmp
= NULL
;
2030 BIGNUM
*p
= NULL
, *g
= NULL
, *bnpub_key
= NULL
;
2031 EVP_PKEY_CTX
*pctx
= NULL
;
2032 OSSL_PARAM
*params
= NULL
;
2033 OSSL_PARAM_BLD
*tmpl
= NULL
;
2036 if (!PACKET_get_length_prefixed_2(pkt
, &prime
)
2037 || !PACKET_get_length_prefixed_2(pkt
, &generator
)
2038 || !PACKET_get_length_prefixed_2(pkt
, &pub_key
)) {
2039 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2043 /* TODO(size_t): Convert these calls */
2044 p
= BN_bin2bn(PACKET_data(&prime
), (int)PACKET_remaining(&prime
), NULL
);
2045 g
= BN_bin2bn(PACKET_data(&generator
), (int)PACKET_remaining(&generator
),
2047 bnpub_key
= BN_bin2bn(PACKET_data(&pub_key
),
2048 (int)PACKET_remaining(&pub_key
), NULL
);
2049 if (p
== NULL
|| g
== NULL
|| bnpub_key
== NULL
) {
2050 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BN_LIB
);
2054 tmpl
= OSSL_PARAM_BLD_new();
2056 || !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_P
, p
)
2057 || !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_G
, g
)
2058 || !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_PUB_KEY
,
2060 || (params
= OSSL_PARAM_BLD_to_param(tmpl
)) == NULL
) {
2061 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2065 pctx
= EVP_PKEY_CTX_new_from_name(s
->ctx
->libctx
, "DH", s
->ctx
->propq
);
2067 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2070 if (EVP_PKEY_key_fromdata_init(pctx
) <= 0
2071 || EVP_PKEY_fromdata(pctx
, &peer_tmp
, params
) <= 0) {
2072 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_DH_VALUE
);
2076 EVP_PKEY_CTX_free(pctx
);
2077 pctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, peer_tmp
, s
->ctx
->propq
);
2079 || EVP_PKEY_param_check(pctx
) != 1
2080 || EVP_PKEY_public_check(pctx
) != 1) {
2081 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_DH_VALUE
);
2085 if (!ssl_security(s
, SSL_SECOP_TMP_DH
, EVP_PKEY_security_bits(peer_tmp
),
2087 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_DH_KEY_TOO_SMALL
);
2091 s
->s3
.peer_tmp
= peer_tmp
;
2095 * FIXME: This makes assumptions about which ciphersuites come with
2096 * public keys. We should have a less ad-hoc way of doing this
2098 if (s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aRSA
| SSL_aDSS
))
2099 *pkey
= X509_get0_pubkey(s
->session
->peer
);
2100 /* else anonymous DH, so no certificate or pkey. */
2105 OSSL_PARAM_BLD_free(tmpl
);
2106 OSSL_PARAM_BLD_free_params(params
);
2107 EVP_PKEY_free(peer_tmp
);
2108 EVP_PKEY_CTX_free(pctx
);
2116 static int tls_process_ske_ecdhe(SSL
*s
, PACKET
*pkt
, EVP_PKEY
**pkey
)
2118 #ifndef OPENSSL_NO_EC
2120 unsigned int curve_type
, curve_id
;
2123 * Extract elliptic curve parameters and the server's ephemeral ECDH
2124 * public key. We only support named (not generic) curves and
2125 * ECParameters in this case is just three bytes.
2127 if (!PACKET_get_1(pkt
, &curve_type
) || !PACKET_get_net_2(pkt
, &curve_id
)) {
2128 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_TOO_SHORT
);
2132 * Check curve is named curve type and one of our preferences, if not
2133 * server has sent an invalid curve.
2135 if (curve_type
!= NAMED_CURVE_TYPE
2136 || !tls1_check_group_id(s
, curve_id
, 1)) {
2137 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_WRONG_CURVE
);
2141 if ((s
->s3
.peer_tmp
= ssl_generate_param_group(s
, curve_id
)) == NULL
) {
2142 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2143 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
2147 if (!PACKET_get_length_prefixed_1(pkt
, &encoded_pt
)) {
2148 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2152 if (EVP_PKEY_set1_encoded_public_key(s
->s3
.peer_tmp
,
2153 PACKET_data(&encoded_pt
),
2154 PACKET_remaining(&encoded_pt
)) <= 0) {
2155 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_ECPOINT
);
2160 * The ECC/TLS specification does not mention the use of DSA to sign
2161 * ECParameters in the server key exchange message. We do support RSA
2164 if (s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aECDSA
)
2165 *pkey
= X509_get0_pubkey(s
->session
->peer
);
2166 else if (s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aRSA
)
2167 *pkey
= X509_get0_pubkey(s
->session
->peer
);
2168 /* else anonymous ECDH, so no certificate or pkey. */
2172 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2177 MSG_PROCESS_RETURN
tls_process_key_exchange(SSL
*s
, PACKET
*pkt
)
2180 EVP_PKEY
*pkey
= NULL
;
2181 EVP_MD_CTX
*md_ctx
= NULL
;
2182 EVP_PKEY_CTX
*pctx
= NULL
;
2183 PACKET save_param_start
, signature
;
2185 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
2187 save_param_start
= *pkt
;
2189 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2190 EVP_PKEY_free(s
->s3
.peer_tmp
);
2191 s
->s3
.peer_tmp
= NULL
;
2194 if (alg_k
& SSL_PSK
) {
2195 if (!tls_process_ske_psk_preamble(s
, pkt
)) {
2196 /* SSLfatal() already called */
2201 /* Nothing else to do for plain PSK or RSAPSK */
2202 if (alg_k
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2203 } else if (alg_k
& SSL_kSRP
) {
2204 if (!tls_process_ske_srp(s
, pkt
, &pkey
)) {
2205 /* SSLfatal() already called */
2208 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2209 if (!tls_process_ske_dhe(s
, pkt
, &pkey
)) {
2210 /* SSLfatal() already called */
2213 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2214 if (!tls_process_ske_ecdhe(s
, pkt
, &pkey
)) {
2215 /* SSLfatal() already called */
2219 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
2223 /* if it was signed, check the signature */
2226 const EVP_MD
*md
= NULL
;
2232 * |pkt| now points to the beginning of the signature, so the difference
2233 * equals the length of the parameters.
2235 if (!PACKET_get_sub_packet(&save_param_start
, ¶ms
,
2236 PACKET_remaining(&save_param_start
) -
2237 PACKET_remaining(pkt
))) {
2238 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_INTERNAL_ERROR
);
2242 if (SSL_USE_SIGALGS(s
)) {
2243 unsigned int sigalg
;
2245 if (!PACKET_get_net_2(pkt
, &sigalg
)) {
2246 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_TOO_SHORT
);
2249 if (tls12_check_peer_sigalg(s
, sigalg
, pkey
) <=0) {
2250 /* SSLfatal() already called */
2253 } else if (!tls1_set_peer_legacy_sigalg(s
, pkey
)) {
2254 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2258 if (!tls1_lookup_md(s
->ctx
, s
->s3
.tmp
.peer_sigalg
, &md
)) {
2259 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2260 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM
);
2263 if (SSL_USE_SIGALGS(s
))
2264 OSSL_TRACE1(TLS
, "USING TLSv1.2 HASH %s\n",
2265 md
== NULL
? "n/a" : EVP_MD_name(md
));
2267 if (!PACKET_get_length_prefixed_2(pkt
, &signature
)
2268 || PACKET_remaining(pkt
) != 0) {
2269 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2273 md_ctx
= EVP_MD_CTX_new();
2274 if (md_ctx
== NULL
) {
2275 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2279 if (EVP_DigestVerifyInit_ex(md_ctx
, &pctx
,
2280 md
== NULL
? NULL
: EVP_MD_name(md
),
2281 s
->ctx
->libctx
, s
->ctx
->propq
, pkey
) <= 0) {
2282 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2285 if (SSL_USE_PSS(s
)) {
2286 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2287 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
2288 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2289 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2293 tbslen
= construct_key_exchange_tbs(s
, &tbs
, PACKET_data(¶ms
),
2294 PACKET_remaining(¶ms
));
2296 /* SSLfatal() already called */
2300 rv
= EVP_DigestVerify(md_ctx
, PACKET_data(&signature
),
2301 PACKET_remaining(&signature
), tbs
, tbslen
);
2304 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_BAD_SIGNATURE
);
2307 EVP_MD_CTX_free(md_ctx
);
2310 /* aNULL, aSRP or PSK do not need public keys */
2311 if (!(s
->s3
.tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
))
2312 && !(alg_k
& SSL_PSK
)) {
2313 /* Might be wrong key type, check it */
2314 if (ssl3_check_cert_and_algorithm(s
)) {
2315 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_DATA
);
2317 /* else this shouldn't happen, SSLfatal() already called */
2320 /* still data left over */
2321 if (PACKET_remaining(pkt
) != 0) {
2322 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
2327 return MSG_PROCESS_CONTINUE_READING
;
2329 EVP_MD_CTX_free(md_ctx
);
2330 return MSG_PROCESS_ERROR
;
2333 MSG_PROCESS_RETURN
tls_process_certificate_request(SSL
*s
, PACKET
*pkt
)
2337 /* Clear certificate validity flags */
2338 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
2339 s
->s3
.tmp
.valid_flags
[i
] = 0;
2341 if (SSL_IS_TLS13(s
)) {
2342 PACKET reqctx
, extensions
;
2343 RAW_EXTENSION
*rawexts
= NULL
;
2345 if ((s
->shutdown
& SSL_SENT_SHUTDOWN
) != 0) {
2347 * We already sent close_notify. This can only happen in TLSv1.3
2348 * post-handshake messages. We can't reasonably respond to this, so
2351 return MSG_PROCESS_FINISHED_READING
;
2354 /* Free and zero certificate types: it is not present in TLS 1.3 */
2355 OPENSSL_free(s
->s3
.tmp
.ctype
);
2356 s
->s3
.tmp
.ctype
= NULL
;
2357 s
->s3
.tmp
.ctype_len
= 0;
2358 OPENSSL_free(s
->pha_context
);
2359 s
->pha_context
= NULL
;
2361 if (!PACKET_get_length_prefixed_1(pkt
, &reqctx
) ||
2362 !PACKET_memdup(&reqctx
, &s
->pha_context
, &s
->pha_context_len
)) {
2363 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2364 return MSG_PROCESS_ERROR
;
2367 if (!PACKET_get_length_prefixed_2(pkt
, &extensions
)) {
2368 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
2369 return MSG_PROCESS_ERROR
;
2371 if (!tls_collect_extensions(s
, &extensions
,
2372 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
,
2374 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
,
2375 rawexts
, NULL
, 0, 1)) {
2376 /* SSLfatal() already called */
2377 OPENSSL_free(rawexts
);
2378 return MSG_PROCESS_ERROR
;
2380 OPENSSL_free(rawexts
);
2381 if (!tls1_process_sigalgs(s
)) {
2382 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_LENGTH
);
2383 return MSG_PROCESS_ERROR
;
2388 /* get the certificate types */
2389 if (!PACKET_get_length_prefixed_1(pkt
, &ctypes
)) {
2390 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2391 return MSG_PROCESS_ERROR
;
2394 if (!PACKET_memdup(&ctypes
, &s
->s3
.tmp
.ctype
, &s
->s3
.tmp
.ctype_len
)) {
2395 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2396 return MSG_PROCESS_ERROR
;
2399 if (SSL_USE_SIGALGS(s
)) {
2402 if (!PACKET_get_length_prefixed_2(pkt
, &sigalgs
)) {
2403 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2404 return MSG_PROCESS_ERROR
;
2408 * Despite this being for certificates, preserve compatibility
2409 * with pre-TLS 1.3 and use the regular sigalgs field.
2411 if (!tls1_save_sigalgs(s
, &sigalgs
, 0)) {
2412 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
2413 SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
2414 return MSG_PROCESS_ERROR
;
2416 if (!tls1_process_sigalgs(s
)) {
2417 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2418 return MSG_PROCESS_ERROR
;
2422 /* get the CA RDNs */
2423 if (!parse_ca_names(s
, pkt
)) {
2424 /* SSLfatal() already called */
2425 return MSG_PROCESS_ERROR
;
2429 if (PACKET_remaining(pkt
) != 0) {
2430 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2431 return MSG_PROCESS_ERROR
;
2434 /* we should setup a certificate to return.... */
2435 s
->s3
.tmp
.cert_req
= 1;
2438 * In TLSv1.3 we don't prepare the client certificate yet. We wait until
2439 * after the CertificateVerify message has been received. This is because
2440 * in TLSv1.3 the CertificateRequest arrives before the Certificate message
2441 * but in TLSv1.2 it is the other way around. We want to make sure that
2442 * SSL_get1_peer_certificate() returns something sensible in
2445 if (SSL_IS_TLS13(s
) && s
->post_handshake_auth
!= SSL_PHA_REQUESTED
)
2446 return MSG_PROCESS_CONTINUE_READING
;
2448 return MSG_PROCESS_CONTINUE_PROCESSING
;
2451 MSG_PROCESS_RETURN
tls_process_new_session_ticket(SSL
*s
, PACKET
*pkt
)
2453 unsigned int ticklen
;
2454 unsigned long ticket_lifetime_hint
, age_add
= 0;
2455 unsigned int sess_len
;
2456 RAW_EXTENSION
*exts
= NULL
;
2458 EVP_MD
*sha256
= NULL
;
2460 PACKET_null_init(&nonce
);
2462 if (!PACKET_get_net_4(pkt
, &ticket_lifetime_hint
)
2464 && (!PACKET_get_net_4(pkt
, &age_add
)
2465 || !PACKET_get_length_prefixed_1(pkt
, &nonce
)))
2466 || !PACKET_get_net_2(pkt
, &ticklen
)
2467 || (SSL_IS_TLS13(s
) ? (ticklen
== 0 || PACKET_remaining(pkt
) < ticklen
)
2468 : PACKET_remaining(pkt
) != ticklen
)) {
2469 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2474 * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
2475 * ticket. We already checked this TLSv1.3 case above, so it should never
2476 * be 0 here in that instance
2479 return MSG_PROCESS_CONTINUE_READING
;
2482 * Sessions must be immutable once they go into the session cache. Otherwise
2483 * we can get multi-thread problems. Therefore we don't "update" sessions,
2484 * we replace them with a duplicate. In TLSv1.3 we need to do this every
2485 * time a NewSessionTicket arrives because those messages arrive
2486 * post-handshake and the session may have already gone into the session
2489 if (SSL_IS_TLS13(s
) || s
->session
->session_id_length
> 0) {
2490 SSL_SESSION
*new_sess
;
2493 * We reused an existing session, so we need to replace it with a new
2496 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
2497 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2501 if ((s
->session_ctx
->session_cache_mode
& SSL_SESS_CACHE_CLIENT
) != 0
2502 && !SSL_IS_TLS13(s
)) {
2504 * In TLSv1.2 and below the arrival of a new tickets signals that
2505 * any old ticket we were using is now out of date, so we remove the
2506 * old session from the cache. We carry on if this fails
2508 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
2511 SSL_SESSION_free(s
->session
);
2512 s
->session
= new_sess
;
2516 * Technically the cast to long here is not guaranteed by the C standard -
2517 * but we use it elsewhere, so this should be ok.
2519 s
->session
->time
= (long)time(NULL
);
2521 OPENSSL_free(s
->session
->ext
.tick
);
2522 s
->session
->ext
.tick
= NULL
;
2523 s
->session
->ext
.ticklen
= 0;
2525 s
->session
->ext
.tick
= OPENSSL_malloc(ticklen
);
2526 if (s
->session
->ext
.tick
== NULL
) {
2527 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2530 if (!PACKET_copy_bytes(pkt
, s
->session
->ext
.tick
, ticklen
)) {
2531 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2535 s
->session
->ext
.tick_lifetime_hint
= ticket_lifetime_hint
;
2536 s
->session
->ext
.tick_age_add
= age_add
;
2537 s
->session
->ext
.ticklen
= ticklen
;
2539 if (SSL_IS_TLS13(s
)) {
2542 if (!PACKET_as_length_prefixed_2(pkt
, &extpkt
)
2543 || PACKET_remaining(pkt
) != 0) {
2544 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2548 if (!tls_collect_extensions(s
, &extpkt
,
2549 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
, &exts
,
2551 || !tls_parse_all_extensions(s
,
2552 SSL_EXT_TLS1_3_NEW_SESSION_TICKET
,
2553 exts
, NULL
, 0, 1)) {
2554 /* SSLfatal() already called */
2560 * There are two ways to detect a resumed ticket session. One is to set
2561 * an appropriate session ID and then the server must return a match in
2562 * ServerHello. This allows the normal client session ID matching to work
2563 * and we know much earlier that the ticket has been accepted. The
2564 * other way is to set zero length session ID when the ticket is
2565 * presented and rely on the handshake to determine session resumption.
2566 * We choose the former approach because this fits in with assumptions
2567 * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
2570 sha256
= EVP_MD_fetch(s
->ctx
->libctx
, "SHA2-256", s
->ctx
->propq
);
2571 if (sha256
== NULL
) {
2572 /* Error is already recorded */
2573 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
2577 * TODO(size_t): we use sess_len here because EVP_Digest expects an int
2578 * but s->session->session_id_length is a size_t
2580 if (!EVP_Digest(s
->session
->ext
.tick
, ticklen
,
2581 s
->session
->session_id
, &sess_len
,
2583 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2586 EVP_MD_free(sha256
);
2588 s
->session
->session_id_length
= sess_len
;
2589 s
->session
->not_resumable
= 0;
2591 /* This is a standalone message in TLSv1.3, so there is no more to read */
2592 if (SSL_IS_TLS13(s
)) {
2593 const EVP_MD
*md
= ssl_handshake_md(s
);
2594 int hashleni
= EVP_MD_size(md
);
2596 static const unsigned char nonce_label
[] = "resumption";
2598 /* Ensure cast to size_t is safe */
2599 if (!ossl_assert(hashleni
>= 0)) {
2600 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2603 hashlen
= (size_t)hashleni
;
2605 if (!tls13_hkdf_expand(s
, md
, s
->resumption_master_secret
,
2607 sizeof(nonce_label
) - 1,
2608 PACKET_data(&nonce
),
2609 PACKET_remaining(&nonce
),
2610 s
->session
->master_key
,
2612 /* SSLfatal() already called */
2615 s
->session
->master_key_length
= hashlen
;
2618 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
2619 return MSG_PROCESS_FINISHED_READING
;
2622 return MSG_PROCESS_CONTINUE_READING
;
2624 EVP_MD_free(sha256
);
2626 return MSG_PROCESS_ERROR
;
2630 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
2631 * parse a separate message. Returns 1 on success or 0 on failure
2633 int tls_process_cert_status_body(SSL
*s
, PACKET
*pkt
)
2638 if (!PACKET_get_1(pkt
, &type
)
2639 || type
!= TLSEXT_STATUSTYPE_ocsp
) {
2640 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
2643 if (!PACKET_get_net_3_len(pkt
, &resplen
)
2644 || PACKET_remaining(pkt
) != resplen
) {
2645 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2648 s
->ext
.ocsp
.resp
= OPENSSL_malloc(resplen
);
2649 if (s
->ext
.ocsp
.resp
== NULL
) {
2650 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2653 if (!PACKET_copy_bytes(pkt
, s
->ext
.ocsp
.resp
, resplen
)) {
2654 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2657 s
->ext
.ocsp
.resp_len
= resplen
;
2663 MSG_PROCESS_RETURN
tls_process_cert_status(SSL
*s
, PACKET
*pkt
)
2665 if (!tls_process_cert_status_body(s
, pkt
)) {
2666 /* SSLfatal() already called */
2667 return MSG_PROCESS_ERROR
;
2670 return MSG_PROCESS_CONTINUE_READING
;
2674 * Perform miscellaneous checks and processing after we have received the
2675 * server's initial flight. In TLS1.3 this is after the Server Finished message.
2676 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
2679 int tls_process_initial_server_flight(SSL
*s
)
2682 * at this point we check that we have the required stuff from
2685 if (!ssl3_check_cert_and_algorithm(s
)) {
2686 /* SSLfatal() already called */
2691 * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
2692 * |ext.ocsp.resp_len| values will be set if we actually received a status
2693 * message, or NULL and -1 otherwise
2695 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
2696 && s
->ctx
->ext
.status_cb
!= NULL
) {
2697 int ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
2700 SSLfatal(s
, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
,
2701 SSL_R_INVALID_STATUS_RESPONSE
);
2705 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2709 #ifndef OPENSSL_NO_CT
2710 if (s
->ct_validation_callback
!= NULL
) {
2711 /* Note we validate the SCTs whether or not we abort on error */
2712 if (!ssl_validate_ct(s
) && (s
->verify_mode
& SSL_VERIFY_PEER
)) {
2713 /* SSLfatal() already called */
2722 MSG_PROCESS_RETURN
tls_process_server_done(SSL
*s
, PACKET
*pkt
)
2724 if (PACKET_remaining(pkt
) > 0) {
2725 /* should contain no data */
2726 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2727 return MSG_PROCESS_ERROR
;
2729 #ifndef OPENSSL_NO_SRP
2730 if (s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
2731 if (SRP_Calc_A_param(s
) <= 0) {
2732 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_SRP_A_CALC
);
2733 return MSG_PROCESS_ERROR
;
2738 if (!tls_process_initial_server_flight(s
)) {
2739 /* SSLfatal() already called */
2740 return MSG_PROCESS_ERROR
;
2743 return MSG_PROCESS_FINISHED_READING
;
2746 static int tls_construct_cke_psk_preamble(SSL
*s
, WPACKET
*pkt
)
2748 #ifndef OPENSSL_NO_PSK
2751 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2752 * \0-terminated identity. The last byte is for us for simulating
2755 char identity
[PSK_MAX_IDENTITY_LEN
+ 1];
2756 size_t identitylen
= 0;
2757 unsigned char psk
[PSK_MAX_PSK_LEN
];
2758 unsigned char *tmppsk
= NULL
;
2759 char *tmpidentity
= NULL
;
2762 if (s
->psk_client_callback
== NULL
) {
2763 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_PSK_NO_CLIENT_CB
);
2767 memset(identity
, 0, sizeof(identity
));
2769 psklen
= s
->psk_client_callback(s
, s
->session
->psk_identity_hint
,
2770 identity
, sizeof(identity
) - 1,
2773 if (psklen
> PSK_MAX_PSK_LEN
) {
2774 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, ERR_R_INTERNAL_ERROR
);
2776 } else if (psklen
== 0) {
2777 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_PSK_IDENTITY_NOT_FOUND
);
2781 identitylen
= strlen(identity
);
2782 if (identitylen
> PSK_MAX_IDENTITY_LEN
) {
2783 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2787 tmppsk
= OPENSSL_memdup(psk
, psklen
);
2788 tmpidentity
= OPENSSL_strdup(identity
);
2789 if (tmppsk
== NULL
|| tmpidentity
== NULL
) {
2790 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2794 OPENSSL_free(s
->s3
.tmp
.psk
);
2795 s
->s3
.tmp
.psk
= tmppsk
;
2796 s
->s3
.tmp
.psklen
= psklen
;
2798 OPENSSL_free(s
->session
->psk_identity
);
2799 s
->session
->psk_identity
= tmpidentity
;
2802 if (!WPACKET_sub_memcpy_u16(pkt
, identity
, identitylen
)) {
2803 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2810 OPENSSL_cleanse(psk
, psklen
);
2811 OPENSSL_cleanse(identity
, sizeof(identity
));
2812 OPENSSL_clear_free(tmppsk
, psklen
);
2813 OPENSSL_clear_free(tmpidentity
, identitylen
);
2817 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2822 static int tls_construct_cke_rsa(SSL
*s
, WPACKET
*pkt
)
2824 unsigned char *encdata
= NULL
;
2825 EVP_PKEY
*pkey
= NULL
;
2826 EVP_PKEY_CTX
*pctx
= NULL
;
2828 unsigned char *pms
= NULL
;
2831 if (s
->session
->peer
== NULL
) {
2833 * We should always have a server certificate with SSL_kRSA.
2835 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2839 pkey
= X509_get0_pubkey(s
->session
->peer
);
2840 if (!EVP_PKEY_is_a(pkey
, "RSA")) {
2841 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2845 pmslen
= SSL_MAX_MASTER_KEY_LENGTH
;
2846 pms
= OPENSSL_malloc(pmslen
);
2848 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2852 pms
[0] = s
->client_version
>> 8;
2853 pms
[1] = s
->client_version
& 0xff;
2854 /* TODO(size_t): Convert this function */
2855 if (RAND_bytes_ex(s
->ctx
->libctx
, pms
+ 2, (int)(pmslen
- 2)) <= 0) {
2856 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2860 /* Fix buf for TLS and beyond */
2861 if (s
->version
> SSL3_VERSION
&& !WPACKET_start_sub_packet_u16(pkt
)) {
2862 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2866 pctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
, pkey
, s
->ctx
->propq
);
2867 if (pctx
== NULL
|| EVP_PKEY_encrypt_init(pctx
) <= 0
2868 || EVP_PKEY_encrypt(pctx
, NULL
, &enclen
, pms
, pmslen
) <= 0) {
2869 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
2872 if (!WPACKET_allocate_bytes(pkt
, enclen
, &encdata
)
2873 || EVP_PKEY_encrypt(pctx
, encdata
, &enclen
, pms
, pmslen
) <= 0) {
2874 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_RSA_ENCRYPT
);
2877 EVP_PKEY_CTX_free(pctx
);
2880 /* Fix buf for TLS and beyond */
2881 if (s
->version
> SSL3_VERSION
&& !WPACKET_close(pkt
)) {
2882 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2886 /* Log the premaster secret, if logging is enabled. */
2887 if (!ssl_log_rsa_client_key_exchange(s
, encdata
, enclen
, pms
, pmslen
)) {
2888 /* SSLfatal() already called */
2892 s
->s3
.tmp
.pms
= pms
;
2893 s
->s3
.tmp
.pmslen
= pmslen
;
2897 OPENSSL_clear_free(pms
, pmslen
);
2898 EVP_PKEY_CTX_free(pctx
);
2903 static int tls_construct_cke_dhe(SSL
*s
, WPACKET
*pkt
)
2905 EVP_PKEY
*ckey
= NULL
, *skey
= NULL
;
2906 unsigned char *keybytes
= NULL
;
2908 unsigned char *encoded_pub
= NULL
;
2909 size_t encoded_pub_len
, pad_len
;
2912 skey
= s
->s3
.peer_tmp
;
2914 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2918 ckey
= ssl_generate_pkey(s
, skey
);
2920 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2924 if (ssl_derive(s
, ckey
, skey
, 0) == 0) {
2925 /* SSLfatal() already called */
2929 /* send off the data */
2931 /* Generate encoding of server key */
2932 encoded_pub_len
= EVP_PKEY_get1_encoded_public_key(ckey
, &encoded_pub
);
2933 if (encoded_pub_len
== 0) {
2934 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2935 EVP_PKEY_free(skey
);
2936 return EXT_RETURN_FAIL
;
2940 * For interoperability with some versions of the Microsoft TLS
2941 * stack, we need to zero pad the DHE pub key to the same length
2944 prime_len
= EVP_PKEY_size(ckey
);
2945 pad_len
= prime_len
- encoded_pub_len
;
2947 if (!WPACKET_sub_allocate_bytes_u16(pkt
, pad_len
, &keybytes
)) {
2948 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2951 memset(keybytes
, 0, pad_len
);
2954 if (!WPACKET_sub_memcpy_u16(pkt
, encoded_pub
, encoded_pub_len
)) {
2955 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2961 OPENSSL_free(encoded_pub
);
2962 EVP_PKEY_free(ckey
);
2966 static int tls_construct_cke_ecdhe(SSL
*s
, WPACKET
*pkt
)
2968 #ifndef OPENSSL_NO_EC
2969 unsigned char *encodedPoint
= NULL
;
2970 size_t encoded_pt_len
= 0;
2971 EVP_PKEY
*ckey
= NULL
, *skey
= NULL
;
2974 skey
= s
->s3
.peer_tmp
;
2976 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2980 ckey
= ssl_generate_pkey(s
, skey
);
2982 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
2986 if (ssl_derive(s
, ckey
, skey
, 0) == 0) {
2987 /* SSLfatal() already called */
2991 /* Generate encoding of client key */
2992 encoded_pt_len
= EVP_PKEY_get1_encoded_public_key(ckey
, &encodedPoint
);
2994 if (encoded_pt_len
== 0) {
2995 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
2999 if (!WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encoded_pt_len
)) {
3000 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3006 OPENSSL_free(encodedPoint
);
3007 EVP_PKEY_free(ckey
);
3010 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3015 static int tls_construct_cke_gost(SSL
*s
, WPACKET
*pkt
)
3017 #ifndef OPENSSL_NO_GOST
3018 /* GOST key exchange message creation */
3019 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3022 unsigned int md_len
;
3023 unsigned char shared_ukm
[32], tmp
[256];
3024 EVP_MD_CTX
*ukm_hash
= NULL
;
3025 int dgst_nid
= NID_id_GostR3411_94
;
3026 unsigned char *pms
= NULL
;
3029 if ((s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aGOST12
) != 0)
3030 dgst_nid
= NID_id_GostR3411_2012_256
;
3033 * Get server certificate PKEY and create ctx from it
3035 peer_cert
= s
->session
->peer
;
3036 if (peer_cert
== NULL
) {
3037 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3038 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
3042 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
,
3043 X509_get0_pubkey(peer_cert
),
3045 if (pkey_ctx
== NULL
) {
3046 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3050 * If we have send a certificate, and certificate key
3051 * parameters match those of server certificate, use
3052 * certificate key for key exchange
3055 /* Otherwise, generate ephemeral key pair */
3057 pms
= OPENSSL_malloc(pmslen
);
3059 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3063 if (EVP_PKEY_encrypt_init(pkey_ctx
) <= 0
3064 /* Generate session key
3065 * TODO(size_t): Convert this function
3067 || RAND_bytes_ex(s
->ctx
->libctx
, pms
, (int)pmslen
) <= 0) {
3068 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3072 * Compute shared IV and store it in algorithm-specific context
3075 ukm_hash
= EVP_MD_CTX_new();
3076 if (ukm_hash
== NULL
3077 || EVP_DigestInit(ukm_hash
, EVP_get_digestbynid(dgst_nid
)) <= 0
3078 || EVP_DigestUpdate(ukm_hash
, s
->s3
.client_random
,
3079 SSL3_RANDOM_SIZE
) <= 0
3080 || EVP_DigestUpdate(ukm_hash
, s
->s3
.server_random
,
3081 SSL3_RANDOM_SIZE
) <= 0
3082 || EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
) <= 0) {
3083 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3086 EVP_MD_CTX_free(ukm_hash
);
3088 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
3089 EVP_PKEY_CTRL_SET_IV
, 8, shared_ukm
) < 0) {
3090 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3093 /* Make GOST keytransport blob message */
3095 * Encapsulate it into sequence
3098 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, pms
, pmslen
) <= 0) {
3099 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3103 if (!WPACKET_put_bytes_u8(pkt
, V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
)
3104 || (msglen
>= 0x80 && !WPACKET_put_bytes_u8(pkt
, 0x81))
3105 || !WPACKET_sub_memcpy_u8(pkt
, tmp
, msglen
)) {
3106 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3110 EVP_PKEY_CTX_free(pkey_ctx
);
3111 s
->s3
.tmp
.pms
= pms
;
3112 s
->s3
.tmp
.pmslen
= pmslen
;
3116 EVP_PKEY_CTX_free(pkey_ctx
);
3117 OPENSSL_clear_free(pms
, pmslen
);
3118 EVP_MD_CTX_free(ukm_hash
);
3121 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3126 #ifndef OPENSSL_NO_GOST
3127 int gost18_cke_cipher_nid(const SSL
*s
)
3129 if ((s
->s3
.tmp
.new_cipher
->algorithm_enc
& SSL_MAGMA
) != 0)
3130 return NID_magma_ctr
;
3131 else if ((s
->s3
.tmp
.new_cipher
->algorithm_enc
& SSL_KUZNYECHIK
) != 0)
3132 return NID_kuznyechik_ctr
;
3137 int gost_ukm(const SSL
*s
, unsigned char *dgst_buf
)
3139 EVP_MD_CTX
* hash
= NULL
;
3140 unsigned int md_len
;
3141 const EVP_MD
*md
= ssl_evp_md_fetch(s
->ctx
->libctx
, NID_id_GostR3411_2012_256
, s
->ctx
->propq
);
3146 if ((hash
= EVP_MD_CTX_new()) == NULL
3147 || EVP_DigestInit(hash
, md
) <= 0
3148 || EVP_DigestUpdate(hash
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
) <= 0
3149 || EVP_DigestUpdate(hash
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
) <= 0
3150 || EVP_DigestFinal_ex(hash
, dgst_buf
, &md_len
) <= 0) {
3151 EVP_MD_CTX_free(hash
);
3152 ssl_evp_md_free(md
);
3156 EVP_MD_CTX_free(hash
);
3157 ssl_evp_md_free(md
);
3162 static int tls_construct_cke_gost18(SSL
*s
, WPACKET
*pkt
)
3164 #ifndef OPENSSL_NO_GOST
3165 /* GOST 2018 key exchange message creation */
3166 unsigned char rnd_dgst
[32], tmp
[255];
3167 EVP_PKEY_CTX
*pkey_ctx
= NULL
;
3169 unsigned char *pms
= NULL
;
3172 int cipher_nid
= gost18_cke_cipher_nid(s
);
3174 if (cipher_nid
== NID_undef
) {
3175 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3179 if (gost_ukm(s
, rnd_dgst
) <= 0) {
3180 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3184 /* Pre-master secret - random bytes */
3186 pms
= OPENSSL_malloc(pmslen
);
3188 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3192 if (RAND_bytes_ex(s
->ctx
->libctx
, pms
, (int)pmslen
) <= 0) {
3193 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3197 /* Get server certificate PKEY and create ctx from it */
3198 peer_cert
= s
->session
->peer
;
3199 if (peer_cert
== NULL
) {
3200 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3201 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
3205 pkey_ctx
= EVP_PKEY_CTX_new_from_pkey(s
->ctx
->libctx
,
3206 X509_get0_pubkey(peer_cert
),
3208 if (pkey_ctx
== NULL
) {
3209 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3213 if (EVP_PKEY_encrypt_init(pkey_ctx
) <= 0 ) {
3214 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3218 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
3219 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
3220 EVP_PKEY_CTRL_SET_IV
, 32, rnd_dgst
) < 0) {
3221 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3225 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
,
3226 EVP_PKEY_CTRL_CIPHER
, cipher_nid
, NULL
) < 0) {
3227 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3232 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, pms
, pmslen
) <= 0) {
3233 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_LIBRARY_BUG
);
3237 if (!WPACKET_memcpy(pkt
, tmp
, msglen
)) {
3238 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3242 EVP_PKEY_CTX_free(pkey_ctx
);
3243 s
->s3
.tmp
.pms
= pms
;
3244 s
->s3
.tmp
.pmslen
= pmslen
;
3248 EVP_PKEY_CTX_free(pkey_ctx
);
3249 OPENSSL_clear_free(pms
, pmslen
);
3252 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3257 static int tls_construct_cke_srp(SSL
*s
, WPACKET
*pkt
)
3259 #ifndef OPENSSL_NO_SRP
3260 unsigned char *abytes
= NULL
;
3262 if (s
->srp_ctx
.A
== NULL
3263 || !WPACKET_sub_allocate_bytes_u16(pkt
, BN_num_bytes(s
->srp_ctx
.A
),
3265 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3268 BN_bn2bin(s
->srp_ctx
.A
, abytes
);
3270 OPENSSL_free(s
->session
->srp_username
);
3271 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
3272 if (s
->session
->srp_username
== NULL
) {
3273 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3279 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3284 int tls_construct_client_key_exchange(SSL
*s
, WPACKET
*pkt
)
3286 unsigned long alg_k
;
3288 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3291 * All of the construct functions below call SSLfatal() if necessary so
3292 * no need to do so here.
3294 if ((alg_k
& SSL_PSK
)
3295 && !tls_construct_cke_psk_preamble(s
, pkt
))
3298 if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3299 if (!tls_construct_cke_rsa(s
, pkt
))
3301 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3302 if (!tls_construct_cke_dhe(s
, pkt
))
3304 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3305 if (!tls_construct_cke_ecdhe(s
, pkt
))
3307 } else if (alg_k
& SSL_kGOST
) {
3308 if (!tls_construct_cke_gost(s
, pkt
))
3310 } else if (alg_k
& SSL_kGOST18
) {
3311 if (!tls_construct_cke_gost18(s
, pkt
))
3313 } else if (alg_k
& SSL_kSRP
) {
3314 if (!tls_construct_cke_srp(s
, pkt
))
3316 } else if (!(alg_k
& SSL_kPSK
)) {
3317 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3323 OPENSSL_clear_free(s
->s3
.tmp
.pms
, s
->s3
.tmp
.pmslen
);
3324 s
->s3
.tmp
.pms
= NULL
;
3325 #ifndef OPENSSL_NO_PSK
3326 OPENSSL_clear_free(s
->s3
.tmp
.psk
, s
->s3
.tmp
.psklen
);
3327 s
->s3
.tmp
.psk
= NULL
;
3332 int tls_client_key_exchange_post_work(SSL
*s
)
3334 unsigned char *pms
= NULL
;
3337 pms
= s
->s3
.tmp
.pms
;
3338 pmslen
= s
->s3
.tmp
.pmslen
;
3340 #ifndef OPENSSL_NO_SRP
3342 if (s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
3343 if (!srp_generate_client_master_secret(s
)) {
3344 /* SSLfatal() already called */
3351 if (pms
== NULL
&& !(s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
)) {
3352 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
3355 if (!ssl_generate_master_secret(s
, pms
, pmslen
, 1)) {
3356 /* SSLfatal() already called */
3357 /* ssl_generate_master_secret frees the pms even on error */
3365 #ifndef OPENSSL_NO_SCTP
3366 if (SSL_IS_DTLS(s
)) {
3367 unsigned char sctpauthkey
[64];
3368 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3372 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3375 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3376 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3378 /* Don't include the terminating zero. */
3379 labellen
= sizeof(labelbuffer
) - 1;
3380 if (s
->mode
& SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
)
3383 if (SSL_export_keying_material(s
, sctpauthkey
,
3384 sizeof(sctpauthkey
), labelbuffer
,
3385 labellen
, NULL
, 0, 0) <= 0) {
3386 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3390 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3391 sizeof(sctpauthkey
), sctpauthkey
);
3397 OPENSSL_clear_free(pms
, pmslen
);
3398 s
->s3
.tmp
.pms
= NULL
;
3403 * Check a certificate can be used for client authentication. Currently check
3404 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3405 * certificates can be used and optionally checks suitability for Suite B.
3407 static int ssl3_check_client_certificate(SSL
*s
)
3409 /* If no suitable signature algorithm can't use certificate */
3410 if (!tls_choose_sigalg(s
, 0) || s
->s3
.tmp
.sigalg
== NULL
)
3413 * If strict mode check suitability of chain before using it. This also
3414 * adjusts suite B digest if necessary.
3416 if (s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
&&
3417 !tls1_check_chain(s
, NULL
, NULL
, NULL
, -2))
3422 WORK_STATE
tls_prepare_client_certificate(SSL
*s
, WORK_STATE wst
)
3425 EVP_PKEY
*pkey
= NULL
;
3428 if (wst
== WORK_MORE_A
) {
3429 /* Let cert callback update client certificates if required */
3430 if (s
->cert
->cert_cb
) {
3431 i
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
3433 s
->rwstate
= SSL_X509_LOOKUP
;
3437 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_CALLBACK_FAILED
);
3440 s
->rwstate
= SSL_NOTHING
;
3442 if (ssl3_check_client_certificate(s
)) {
3443 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
) {
3444 return WORK_FINISHED_STOP
;
3446 return WORK_FINISHED_CONTINUE
;
3449 /* Fall through to WORK_MORE_B */
3453 /* We need to get a client cert */
3454 if (wst
== WORK_MORE_B
) {
3456 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3457 * return(-1); We then get retied later
3459 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
3461 s
->rwstate
= SSL_X509_LOOKUP
;
3464 s
->rwstate
= SSL_NOTHING
;
3465 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
3466 if (!SSL_use_certificate(s
, x509
) || !SSL_use_PrivateKey(s
, pkey
))
3468 } else if (i
== 1) {
3470 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
3474 EVP_PKEY_free(pkey
);
3475 if (i
&& !ssl3_check_client_certificate(s
))
3478 if (s
->version
== SSL3_VERSION
) {
3479 s
->s3
.tmp
.cert_req
= 0;
3480 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_CERTIFICATE
);
3481 return WORK_FINISHED_CONTINUE
;
3483 s
->s3
.tmp
.cert_req
= 2;
3484 if (!ssl3_digest_cached_records(s
, 0)) {
3485 /* SSLfatal() already called */
3491 if (s
->post_handshake_auth
== SSL_PHA_REQUESTED
)
3492 return WORK_FINISHED_STOP
;
3493 return WORK_FINISHED_CONTINUE
;
3496 /* Shouldn't ever get here */
3497 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3501 int tls_construct_client_certificate(SSL
*s
, WPACKET
*pkt
)
3503 if (SSL_IS_TLS13(s
)) {
3504 if (s
->pha_context
== NULL
) {
3505 /* no context available, add 0-length context */
3506 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
3507 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3510 } else if (!WPACKET_sub_memcpy_u8(pkt
, s
->pha_context
, s
->pha_context_len
)) {
3511 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3515 if (!ssl3_output_cert_chain(s
, pkt
,
3516 (s
->s3
.tmp
.cert_req
== 2) ? NULL
3518 /* SSLfatal() already called */
3523 && SSL_IS_FIRST_HANDSHAKE(s
)
3524 && (!s
->method
->ssl3_enc
->change_cipher_state(s
,
3525 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_CLIENT_WRITE
))) {
3527 * This is a fatal error, which leaves enc_write_ctx in an inconsistent
3528 * state and thus ssl3_send_alert may crash.
3530 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_R_CANNOT_CHANGE_CIPHER
);
3537 int ssl3_check_cert_and_algorithm(SSL
*s
)
3539 const SSL_CERT_LOOKUP
*clu
;
3543 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
3544 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
3546 /* we don't have a certificate */
3547 if (!(alg_a
& SSL_aCERT
))
3550 /* This is the passed certificate */
3551 clu
= ssl_cert_lookup_by_pkey(X509_get0_pubkey(s
->session
->peer
), &idx
);
3553 /* Check certificate is recognised and suitable for cipher */
3554 if (clu
== NULL
|| (alg_a
& clu
->amask
) == 0) {
3555 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_MISSING_SIGNING_CERT
);
3559 #ifndef OPENSSL_NO_EC
3560 if (clu
->amask
& SSL_aECDSA
) {
3561 if (ssl_check_srvr_ecc_cert_and_alg(s
->session
->peer
, s
))
3563 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_BAD_ECC_CERT
);
3567 if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
) && idx
!= SSL_PKEY_RSA
) {
3568 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
3569 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
3572 #ifndef OPENSSL_NO_DH
3573 if ((alg_k
& SSL_kDHE
) && (s
->s3
.peer_tmp
== NULL
)) {
3574 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3582 #ifndef OPENSSL_NO_NEXTPROTONEG
3583 int tls_construct_next_proto(SSL
*s
, WPACKET
*pkt
)
3585 size_t len
, padding_len
;
3586 unsigned char *padding
= NULL
;
3588 len
= s
->ext
.npn_len
;
3589 padding_len
= 32 - ((len
+ 2) % 32);
3591 if (!WPACKET_sub_memcpy_u8(pkt
, s
->ext
.npn
, len
)
3592 || !WPACKET_sub_allocate_bytes_u8(pkt
, padding_len
, &padding
)) {
3593 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3597 memset(padding
, 0, padding_len
);
3603 MSG_PROCESS_RETURN
tls_process_hello_req(SSL
*s
, PACKET
*pkt
)
3605 if (PACKET_remaining(pkt
) > 0) {
3606 /* should contain no data */
3607 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3608 return MSG_PROCESS_ERROR
;
3611 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
)) {
3612 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
3613 return MSG_PROCESS_FINISHED_READING
;
3617 * This is a historical discrepancy (not in the RFC) maintained for
3618 * compatibility reasons. If a TLS client receives a HelloRequest it will
3619 * attempt an abbreviated handshake. However if a DTLS client receives a
3620 * HelloRequest it will do a full handshake. Either behaviour is reasonable
3621 * but doing one for TLS and another for DTLS is odd.
3626 SSL_renegotiate_abbreviated(s
);
3628 return MSG_PROCESS_FINISHED_READING
;
3631 static MSG_PROCESS_RETURN
tls_process_encrypted_extensions(SSL
*s
, PACKET
*pkt
)
3634 RAW_EXTENSION
*rawexts
= NULL
;
3636 if (!PACKET_as_length_prefixed_2(pkt
, &extensions
)
3637 || PACKET_remaining(pkt
) != 0) {
3638 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
3642 if (!tls_collect_extensions(s
, &extensions
,
3643 SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
, &rawexts
,
3645 || !tls_parse_all_extensions(s
, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
3646 rawexts
, NULL
, 0, 1)) {
3647 /* SSLfatal() already called */
3651 OPENSSL_free(rawexts
);
3652 return MSG_PROCESS_CONTINUE_READING
;
3655 OPENSSL_free(rawexts
);
3656 return MSG_PROCESS_ERROR
;
3659 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
3662 #ifndef OPENSSL_NO_ENGINE
3663 if (s
->ctx
->client_cert_engine
) {
3664 i
= tls_engine_load_ssl_client_cert(s
, px509
, ppkey
);
3669 if (s
->ctx
->client_cert_cb
)
3670 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);
3674 int ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
, WPACKET
*pkt
)
3677 size_t totlen
= 0, len
, maxlen
, maxverok
= 0;
3678 int empty_reneg_info_scsv
= !s
->renegotiate
;
3680 /* Set disabled masks for this session */
3681 if (!ssl_set_client_disabled(s
)) {
3682 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
3687 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3691 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
3692 # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
3693 # error Max cipher length too short
3696 * Some servers hang if client hello > 256 bytes as hack workaround
3697 * chop number of supported ciphers to keep it well below this if we
3700 if (TLS1_get_version(s
) >= TLS1_2_VERSION
)
3701 maxlen
= OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
& ~1;
3704 /* Maximum length that can be stored in 2 bytes. Length must be even */
3707 if (empty_reneg_info_scsv
)
3709 if (s
->mode
& SSL_MODE_SEND_FALLBACK_SCSV
)
3712 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
) && totlen
< maxlen
; i
++) {
3713 const SSL_CIPHER
*c
;
3715 c
= sk_SSL_CIPHER_value(sk
, i
);
3716 /* Skip disabled ciphers */
3717 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_SUPPORTED
, 0))
3720 if (!s
->method
->put_cipher_by_char(c
, pkt
, &len
)) {
3721 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3725 /* Sanity check that the maximum version we offer has ciphers enabled */
3727 if (SSL_IS_DTLS(s
)) {
3728 if (DTLS_VERSION_GE(c
->max_dtls
, s
->s3
.tmp
.max_ver
)
3729 && DTLS_VERSION_LE(c
->min_dtls
, s
->s3
.tmp
.max_ver
))
3732 if (c
->max_tls
>= s
->s3
.tmp
.max_ver
3733 && c
->min_tls
<= s
->s3
.tmp
.max_ver
)
3741 if (totlen
== 0 || !maxverok
) {
3742 const char *maxvertext
=
3744 ? "No ciphers enabled for max supported SSL/TLS version"
3747 SSLfatal_data(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_CIPHERS_AVAILABLE
,
3753 if (empty_reneg_info_scsv
) {
3754 static SSL_CIPHER scsv
= {
3755 0, NULL
, NULL
, SSL3_CK_SCSV
, 0, 0, 0, 0, 0, 0, 0, 0, 0
3757 if (!s
->method
->put_cipher_by_char(&scsv
, pkt
, &len
)) {
3758 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3762 if (s
->mode
& SSL_MODE_SEND_FALLBACK_SCSV
) {
3763 static SSL_CIPHER scsv
= {
3764 0, NULL
, NULL
, SSL3_CK_FALLBACK_SCSV
, 0, 0, 0, 0, 0, 0, 0, 0, 0
3766 if (!s
->method
->put_cipher_by_char(&scsv
, pkt
, &len
)) {
3767 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
3776 int tls_construct_end_of_early_data(SSL
*s
, WPACKET
*pkt
)
3778 if (s
->early_data_state
!= SSL_EARLY_DATA_WRITE_RETRY
3779 && s
->early_data_state
!= SSL_EARLY_DATA_FINISHED_WRITING
) {
3780 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
3784 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;