2 * Copyright 1995-2021 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 /* Restore digest for PHA before adding message.*/
172 # error Internal DTLS version error
174 if (!SSL_IS_DTLS(s) && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
175 s->post_handshake_auth = SSL_PHA_REQUESTED;
177 * In TLS, this is called before the message is added to the
178 * digest. In DTLS, this is expected to be called after adding
179 * to the digest. Either move the digest restore, or add the
180 * message here after the swap, or do it after the clientFinished?
182 if (!tls13_restore_handshake_digest_for_pha(s)) {
183 /* SSLfatal() already called */
186 st->hand_state = TLS_ST_CR_CERT_REQ;
193 /* No valid transition found */
198 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
199 * handshake state transitions when the client is reading messages from the
200 * server. The message type that the server has sent is provided in |mt|. The
201 * current state is in |s->statem.hand_state|.
203 * Return values are 1 for success (transition allowed) and 0 on error
204 * (transition not allowed)
206 int ossl_statem_client_read_transition(SSL *s, int mt)
208 OSSL_STATEM *st = &s->statem;
212 * Note that after writing the first ClientHello we don't know what version
213 * we are going to negotiate yet, so we don't take this branch until later.
215 if (SSL_IS_TLS13(s)) {
216 if (!ossl_statem_client13_read_transition(s, mt))
221 switch (st->hand_state) {
225 case TLS_ST_CW_CLNT_HELLO:
226 if (mt == SSL3_MT_SERVER_HELLO) {
227 st->hand_state = TLS_ST_CR_SRVR_HELLO;
231 if (SSL_IS_DTLS(s)) {
232 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
233 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
239 case TLS_ST_EARLY_DATA:
241 * We've not actually selected TLSv1.3 yet, but we have sent early
242 * data. The only thing allowed now is a ServerHello or a
245 if (mt == SSL3_MT_SERVER_HELLO) {
246 st->hand_state = TLS_ST_CR_SRVR_HELLO;
251 case TLS_ST_CR_SRVR_HELLO:
253 if (s->ext.ticket_expected) {
254 if (mt == SSL3_MT_NEWSESSION_TICKET) {
255 st->hand_state = TLS_ST_CR_SESSION_TICKET;
258 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
259 st->hand_state = TLS_ST_CR_CHANGE;
263 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
264 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
266 } else if (s->version >= TLS1_VERSION
267 && s->ext.session_secret_cb != NULL
268 && s->session->ext.tick != NULL
269 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
271 * Normally, we can tell if the server is resuming the session
272 * from the session ID. EAP-FAST (RFC 4851), however, relies on
273 * the next server message after the ServerHello to determine if
274 * the server is resuming.
277 st->hand_state = TLS_ST_CR_CHANGE;
279 } else if (!(s->s3.tmp.new_cipher->algorithm_auth
280 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
281 if (mt == SSL3_MT_CERTIFICATE) {
282 st->hand_state = TLS_ST_CR_CERT;
286 ske_expected = key_exchange_expected(s);
287 /* SKE is optional for some PSK ciphersuites */
289 || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
290 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
291 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
292 st->hand_state = TLS_ST_CR_KEY_EXCH;
295 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
296 && cert_req_allowed(s)) {
297 st->hand_state = TLS_ST_CR_CERT_REQ;
299 } else if (mt == SSL3_MT_SERVER_DONE) {
300 st->hand_state = TLS_ST_CR_SRVR_DONE;
309 * The CertificateStatus message is optional even if
310 * |ext.status_expected| is set
312 if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
313 st->hand_state = TLS_ST_CR_CERT_STATUS;
318 case TLS_ST_CR_CERT_STATUS:
319 ske_expected = key_exchange_expected(s);
320 /* SKE is optional for some PSK ciphersuites */
321 if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
322 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
323 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
324 st->hand_state = TLS_ST_CR_KEY_EXCH;
331 case TLS_ST_CR_KEY_EXCH:
332 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
333 if (cert_req_allowed(s)) {
334 st->hand_state = TLS_ST_CR_CERT_REQ;
341 case TLS_ST_CR_CERT_REQ:
342 if (mt == SSL3_MT_SERVER_DONE) {
343 st->hand_state = TLS_ST_CR_SRVR_DONE;
348 case TLS_ST_CW_FINISHED:
349 if (s->ext.ticket_expected) {
350 if (mt == SSL3_MT_NEWSESSION_TICKET) {
351 st->hand_state = TLS_ST_CR_SESSION_TICKET;
354 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
355 st->hand_state = TLS_ST_CR_CHANGE;
360 case TLS_ST_CR_SESSION_TICKET:
361 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
362 st->hand_state = TLS_ST_CR_CHANGE;
367 case TLS_ST_CR_CHANGE:
368 if (mt == SSL3_MT_FINISHED) {
369 st->hand_state = TLS_ST_CR_FINISHED;
375 if (mt == SSL3_MT_HELLO_REQUEST) {
376 st->hand_state = TLS_ST_CR_HELLO_REQ;
383 /* No valid transition found */
384 if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
388 * CCS messages don't have a message sequence number so this is probably
389 * because of an out-of-order CCS. We'll just drop it.
392 s->rwstate = SSL_READING;
393 rbio = SSL_get_rbio(s);
394 BIO_clear_retry_flags(rbio);
395 BIO_set_retry_read(rbio);
398 SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
403 * ossl_statem_client13_write_transition() works out what handshake state to
404 * move to next when the TLSv1.3 client is writing messages to be sent to the
407 static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s)
409 OSSL_STATEM *st = &s->statem;
412 * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
413 * TLSv1.3 yet at that point. They are handled by
414 * ossl_statem_client_write_transition().
416 switch (st->hand_state) {
418 /* Shouldn't happen */
419 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
420 return WRITE_TRAN_ERROR;
422 case TLS_ST_CR_CERT_REQ:
423 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
424 st->hand_state = TLS_ST_CW_CERT;
425 return WRITE_TRAN_CONTINUE;
428 * We should only get here if we received a CertificateRequest after
429 * we already sent close_notify
431 if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
432 /* Shouldn't happen - same as default case */
433 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
434 return WRITE_TRAN_ERROR;
436 st->hand_state = TLS_ST_OK;
437 return WRITE_TRAN_CONTINUE;
439 case TLS_ST_CR_FINISHED:
440 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
441 || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
442 st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
443 else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
444 && s->hello_retry_request == SSL_HRR_NONE)
445 st->hand_state = TLS_ST_CW_CHANGE;
447 st->hand_state = (s->s3.tmp.cert_req != 0) ? TLS_ST_CW_CERT
448 : TLS_ST_CW_FINISHED;
449 return WRITE_TRAN_CONTINUE;
451 case TLS_ST_PENDING_EARLY_DATA_END:
452 if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
453 st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
454 return WRITE_TRAN_CONTINUE;
458 case TLS_ST_CW_END_OF_EARLY_DATA:
459 case TLS_ST_CW_CHANGE:
460 st->hand_state = (s->s3.tmp.cert_req != 0) ? TLS_ST_CW_CERT
461 : TLS_ST_CW_FINISHED;
462 return WRITE_TRAN_CONTINUE;
465 /* If a non-empty Certificate we also send CertificateVerify */
466 st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
467 : TLS_ST_CW_FINISHED;
468 return WRITE_TRAN_CONTINUE;
470 case TLS_ST_CW_CERT_VRFY:
471 st->hand_state = TLS_ST_CW_FINISHED;
472 return WRITE_TRAN_CONTINUE;
474 case TLS_ST_CR_KEY_UPDATE:
475 case TLS_ST_CW_KEY_UPDATE:
476 case TLS_ST_CR_SESSION_TICKET:
477 case TLS_ST_CW_FINISHED:
478 st->hand_state = TLS_ST_OK;
479 return WRITE_TRAN_CONTINUE;
482 if (s->key_update != SSL_KEY_UPDATE_NONE) {
483 st->hand_state = TLS_ST_CW_KEY_UPDATE;
484 return WRITE_TRAN_CONTINUE;
487 /* Try to read from the server instead */
488 return WRITE_TRAN_FINISHED;
493 * ossl_statem_client_write_transition() works out what handshake state to
494 * move to next when the client is writing messages to be sent to the server.
496 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
498 OSSL_STATEM *st = &s->statem;
501 * Note that immediately before/after a ClientHello we don't know what
502 * version we are going to negotiate yet, so we don't take this branch until
506 return ossl_statem_client13_write_transition(s);
508 switch (st->hand_state) {
510 /* Shouldn't happen */
511 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
512 return WRITE_TRAN_ERROR;
515 if (!s->renegotiate) {
517 * We haven't requested a renegotiation ourselves so we must have
518 * received a message from the server. Better read it.
520 return WRITE_TRAN_FINISHED;
525 st->hand_state = TLS_ST_CW_CLNT_HELLO;
526 return WRITE_TRAN_CONTINUE;
528 case TLS_ST_CW_CLNT_HELLO:
529 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
531 * We are assuming this is a TLSv1.3 connection, although we haven't
532 * actually selected a version yet.
534 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
535 st->hand_state = TLS_ST_CW_CHANGE;
537 st->hand_state = TLS_ST_EARLY_DATA;
538 return WRITE_TRAN_CONTINUE;
541 * No transition at the end of writing because we don't know what
544 return WRITE_TRAN_FINISHED;
546 case TLS_ST_CR_SRVR_HELLO:
548 * We only get here in TLSv1.3. We just received an HRR, so issue a
549 * CCS unless middlebox compat mode is off, or we already issued one
550 * because we did early data.
552 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
553 && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
554 st->hand_state = TLS_ST_CW_CHANGE;
556 st->hand_state = TLS_ST_CW_CLNT_HELLO;
557 return WRITE_TRAN_CONTINUE;
559 case TLS_ST_EARLY_DATA:
560 return WRITE_TRAN_FINISHED;
562 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
563 st->hand_state = TLS_ST_CW_CLNT_HELLO;
564 return WRITE_TRAN_CONTINUE;
566 case TLS_ST_CR_SRVR_DONE:
567 if (s->s3.tmp.cert_req)
568 st->hand_state = TLS_ST_CW_CERT;
570 st->hand_state = TLS_ST_CW_KEY_EXCH;
571 return WRITE_TRAN_CONTINUE;
574 st->hand_state = TLS_ST_CW_KEY_EXCH;
575 return WRITE_TRAN_CONTINUE;
577 case TLS_ST_CW_KEY_EXCH:
579 * For TLS, cert_req is set to 2, so a cert chain of nothing is
580 * sent, but no verify packet is sent
583 * XXX: For now, we do not support client authentication in ECDH
584 * cipher suites with ECDH (rather than ECDSA) certificates. We
585 * need to skip the certificate verify message when client's
586 * ECDH public key is sent inside the client certificate.
588 if (s->s3.tmp.cert_req == 1) {
589 st->hand_state = TLS_ST_CW_CERT_VRFY;
591 st->hand_state = TLS_ST_CW_CHANGE;
593 if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
594 st->hand_state = TLS_ST_CW_CHANGE;
596 return WRITE_TRAN_CONTINUE;
598 case TLS_ST_CW_CERT_VRFY:
599 st->hand_state = TLS_ST_CW_CHANGE;
600 return WRITE_TRAN_CONTINUE;
602 case TLS_ST_CW_CHANGE:
603 if (s->hello_retry_request == SSL_HRR_PENDING) {
604 st->hand_state = TLS_ST_CW_CLNT_HELLO;
605 } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
606 st->hand_state = TLS_ST_EARLY_DATA;
608 #if defined(OPENSSL_NO_NEXTPROTONEG)
609 st->hand_state = TLS_ST_CW_FINISHED;
611 if (!SSL_IS_DTLS(s) && s->s3.npn_seen)
612 st->hand_state = TLS_ST_CW_NEXT_PROTO;
614 st->hand_state = TLS_ST_CW_FINISHED;
617 return WRITE_TRAN_CONTINUE;
619 #if !defined(OPENSSL_NO_NEXTPROTONEG)
620 case TLS_ST_CW_NEXT_PROTO:
621 st->hand_state = TLS_ST_CW_FINISHED;
622 return WRITE_TRAN_CONTINUE;
625 case TLS_ST_CW_FINISHED:
627 st->hand_state = TLS_ST_OK;
628 return WRITE_TRAN_CONTINUE;
630 return WRITE_TRAN_FINISHED;
633 case TLS_ST_CR_FINISHED:
635 st->hand_state = TLS_ST_CW_CHANGE;
636 return WRITE_TRAN_CONTINUE;
638 st->hand_state = TLS_ST_OK;
639 return WRITE_TRAN_CONTINUE;
642 case TLS_ST_CR_HELLO_REQ:
644 * If we can renegotiate now then do so, otherwise wait for a more
647 if (ssl3_renegotiate_check(s, 1)) {
648 if (!tls_setup_handshake(s)) {
649 /* SSLfatal() already called */
650 return WRITE_TRAN_ERROR;
652 st->hand_state = TLS_ST_CW_CLNT_HELLO;
653 return WRITE_TRAN_CONTINUE;
655 st->hand_state = TLS_ST_OK;
656 return WRITE_TRAN_CONTINUE;
661 * Perform any pre work that needs to be done prior to sending a message from
662 * the client to the server.
664 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
666 OSSL_STATEM *st = &s->statem;
668 switch (st->hand_state) {
670 /* No pre work to be done */
673 case TLS_ST_CW_CLNT_HELLO:
675 if (SSL_IS_DTLS(s)) {
676 /* every DTLS ClientHello resets Finished MAC */
677 if (!ssl3_init_finished_mac(s)) {
678 /* SSLfatal() already called */
684 case TLS_ST_CW_CHANGE:
685 if (SSL_IS_DTLS(s)) {
688 * We're into the last flight so we don't retransmit these
689 * messages unless we need to.
693 #ifndef OPENSSL_NO_SCTP
694 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
695 /* Calls SSLfatal() as required */
696 return dtls_wait_for_dry(s);
702 case TLS_ST_PENDING_EARLY_DATA_END:
704 * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
705 * attempt to write early data before calling SSL_read() then we press
706 * on with the handshake. Otherwise we pause here.
708 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
709 || s->early_data_state == SSL_EARLY_DATA_NONE)
710 return WORK_FINISHED_CONTINUE;
713 case TLS_ST_EARLY_DATA:
714 return tls_finish_handshake(s, wst, 0, 1);
717 /* Calls SSLfatal() as required */
718 return tls_finish_handshake(s, wst, 1, 1);
721 return WORK_FINISHED_CONTINUE;
725 * Perform any work that needs to be done after sending a message from the
726 * client to the server.
728 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
730 OSSL_STATEM *st = &s->statem;
734 switch (st->hand_state) {
736 /* No post work to be done */
739 case TLS_ST_CW_CLNT_HELLO:
740 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
741 && s->max_early_data > 0) {
743 * We haven't selected TLSv1.3 yet so we don't call the change
744 * cipher state function associated with the SSL_METHOD. Instead
745 * we call tls13_change_cipher_state() directly.
747 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
748 if (!tls13_change_cipher_state(s,
749 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
750 /* SSLfatal() already called */
754 /* else we're in compat mode so we delay flushing until after CCS */
755 } else if (!statem_flush(s)) {
759 if (SSL_IS_DTLS(s)) {
760 /* Treat the next message as the first packet */
765 case TLS_ST_CW_END_OF_EARLY_DATA:
767 * We set the enc_write_ctx back to NULL because we may end up writing
768 * in cleartext again if we get a HelloRetryRequest from the server.
770 EVP_CIPHER_CTX_free(s->enc_write_ctx);
771 s->enc_write_ctx = NULL;
774 case TLS_ST_CW_KEY_EXCH:
775 if (tls_client_key_exchange_post_work(s) == 0) {
776 /* SSLfatal() already called */
781 case TLS_ST_CW_CHANGE:
782 if (SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING)
784 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
785 && s->max_early_data > 0) {
787 * We haven't selected TLSv1.3 yet so we don't call the change
788 * cipher state function associated with the SSL_METHOD. Instead
789 * we call tls13_change_cipher_state() directly.
791 if (!tls13_change_cipher_state(s,
792 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
796 s->session->cipher = s->s3.tmp.new_cipher;
797 #ifdef OPENSSL_NO_COMP
798 s->session->compress_meth = 0;
800 if (s->s3.tmp.new_compression == NULL)
801 s->session->compress_meth = 0;
803 s->session->compress_meth = s->s3.tmp.new_compression->id;
805 if (!s->method->ssl3_enc->setup_key_block(s)) {
806 /* SSLfatal() already called */
810 if (!s->method->ssl3_enc->change_cipher_state(s,
811 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
812 /* SSLfatal() already called */
816 if (SSL_IS_DTLS(s)) {
817 #ifndef OPENSSL_NO_SCTP
820 * Change to new shared key of SCTP-Auth, will be ignored if
823 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
828 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
832 case TLS_ST_CW_FINISHED:
833 #ifndef OPENSSL_NO_SCTP
834 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
836 * Change to new shared key of SCTP-Auth, will be ignored if
839 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
843 if (statem_flush(s) != 1)
846 if (SSL_IS_TLS13(s)) {
847 if (!tls13_save_handshake_digest_for_pha(s)) {
848 /* SSLfatal() already called */
851 if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
852 if (!s->method->ssl3_enc->change_cipher_state(s,
853 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
854 /* SSLfatal() already called */
861 case TLS_ST_CW_KEY_UPDATE:
862 if (statem_flush(s) != 1)
864 if (!tls13_update_key(s, 1)) {
865 /* SSLfatal() already called */
871 return WORK_FINISHED_CONTINUE;
875 * Get the message construction function and message type for sending from the
878 * Valid return values are:
882 int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
883 confunc_f *confunc, int *mt)
885 OSSL_STATEM *st = &s->statem;
887 switch (st->hand_state) {
889 /* Shouldn't happen */
890 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
893 case TLS_ST_CW_CHANGE:
895 *confunc = dtls_construct_change_cipher_spec;
897 *confunc = tls_construct_change_cipher_spec;
898 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
901 case TLS_ST_CW_CLNT_HELLO:
902 *confunc = tls_construct_client_hello;
903 *mt = SSL3_MT_CLIENT_HELLO;
906 case TLS_ST_CW_END_OF_EARLY_DATA:
907 *confunc = tls_construct_end_of_early_data;
908 *mt = SSL3_MT_END_OF_EARLY_DATA;
911 case TLS_ST_PENDING_EARLY_DATA_END:
917 *confunc = tls_construct_client_certificate;
918 *mt = SSL3_MT_CERTIFICATE;
921 case TLS_ST_CW_KEY_EXCH:
922 *confunc = tls_construct_client_key_exchange;
923 *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
926 case TLS_ST_CW_CERT_VRFY:
927 *confunc = tls_construct_cert_verify;
928 *mt = SSL3_MT_CERTIFICATE_VERIFY;
931 #if !defined(OPENSSL_NO_NEXTPROTONEG)
932 case TLS_ST_CW_NEXT_PROTO:
933 *confunc = tls_construct_next_proto;
934 *mt = SSL3_MT_NEXT_PROTO;
937 case TLS_ST_CW_FINISHED:
938 *confunc = tls_construct_finished;
939 *mt = SSL3_MT_FINISHED;
942 case TLS_ST_CW_KEY_UPDATE:
943 *confunc = tls_construct_key_update;
944 *mt = SSL3_MT_KEY_UPDATE;
952 * Returns the maximum allowed length for the current message that we are
953 * reading. Excludes the message header.
955 size_t ossl_statem_client_max_message_size(SSL *s)
957 OSSL_STATEM *st = &s->statem;
959 switch (st->hand_state) {
961 /* Shouldn't happen */
964 case TLS_ST_CR_SRVR_HELLO:
965 return SERVER_HELLO_MAX_LENGTH;
967 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
968 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
971 return s->max_cert_list;
973 case TLS_ST_CR_CERT_VRFY:
974 return SSL3_RT_MAX_PLAIN_LENGTH;
976 case TLS_ST_CR_CERT_STATUS:
977 return SSL3_RT_MAX_PLAIN_LENGTH;
979 case TLS_ST_CR_KEY_EXCH:
980 return SERVER_KEY_EXCH_MAX_LENGTH;
982 case TLS_ST_CR_CERT_REQ:
984 * Set to s->max_cert_list for compatibility with previous releases. In
985 * practice these messages can get quite long if servers are configured
986 * to provide a long list of acceptable CAs
988 return s->max_cert_list;
990 case TLS_ST_CR_SRVR_DONE:
991 return SERVER_HELLO_DONE_MAX_LENGTH;
993 case TLS_ST_CR_CHANGE:
994 if (s->version == DTLS1_BAD_VER)
996 return CCS_MAX_LENGTH;
998 case TLS_ST_CR_SESSION_TICKET:
999 return (SSL_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
1000 : SESSION_TICKET_MAX_LENGTH_TLS12;
1002 case TLS_ST_CR_FINISHED:
1003 return FINISHED_MAX_LENGTH;
1005 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1006 return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
1008 case TLS_ST_CR_KEY_UPDATE:
1009 return KEY_UPDATE_MAX_LENGTH;
1014 * Process a message that the client has received from the server.
1016 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
1018 OSSL_STATEM *st = &s->statem;
1020 switch (st->hand_state) {
1022 /* Shouldn't happen */
1023 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1024 return MSG_PROCESS_ERROR;
1026 case TLS_ST_CR_SRVR_HELLO:
1027 return tls_process_server_hello(s, pkt);
1029 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1030 return dtls_process_hello_verify(s, pkt);
1032 case TLS_ST_CR_CERT:
1033 return tls_process_server_certificate(s, pkt);
1035 case TLS_ST_CR_CERT_VRFY:
1036 return tls_process_cert_verify(s, pkt);
1038 case TLS_ST_CR_CERT_STATUS:
1039 return tls_process_cert_status(s, pkt);
1041 case TLS_ST_CR_KEY_EXCH:
1042 return tls_process_key_exchange(s, pkt);
1044 case TLS_ST_CR_CERT_REQ:
1045 return tls_process_certificate_request(s, pkt);
1047 case TLS_ST_CR_SRVR_DONE:
1048 return tls_process_server_done(s, pkt);
1050 case TLS_ST_CR_CHANGE:
1051 return tls_process_change_cipher_spec(s, pkt);
1053 case TLS_ST_CR_SESSION_TICKET:
1054 return tls_process_new_session_ticket(s, pkt);
1056 case TLS_ST_CR_FINISHED:
1057 return tls_process_finished(s, pkt);
1059 case TLS_ST_CR_HELLO_REQ:
1060 return tls_process_hello_req(s, pkt);
1062 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1063 return tls_process_encrypted_extensions(s, pkt);
1065 case TLS_ST_CR_KEY_UPDATE:
1066 return tls_process_key_update(s, pkt);
1071 * Perform any further processing required following the receipt of a message
1074 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
1076 OSSL_STATEM *st = &s->statem;
1078 switch (st->hand_state) {
1080 /* Shouldn't happen */
1081 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1084 case TLS_ST_CR_CERT:
1085 return tls_post_process_server_certificate(s, wst);
1087 case TLS_ST_CR_CERT_VRFY:
1088 case TLS_ST_CR_CERT_REQ:
1089 return tls_prepare_client_certificate(s, wst);
1093 int tls_construct_client_hello(SSL *s, WPACKET *pkt)
1098 #ifndef OPENSSL_NO_COMP
1101 SSL_SESSION *sess = s->session;
1102 unsigned char *session_id;
1104 /* Work out what SSL/TLS/DTLS version to use */
1105 protverr = ssl_set_client_hello_version(s);
1106 if (protverr != 0) {
1107 SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr);
1112 || !ssl_version_supported(s, sess->ssl_version, NULL)
1113 || !SSL_SESSION_is_resumable(sess)) {
1114 if (s->hello_retry_request == SSL_HRR_NONE
1115 && !ssl_get_new_session(s, 0)) {
1116 /* SSLfatal() already called */
1120 /* else use the pre-loaded session */
1122 p = s->s3.client_random;
1125 * for DTLS if client_random is initialized, reuse it, we are
1126 * required to use same upon reply to HelloVerify
1128 if (SSL_IS_DTLS(s)) {
1131 for (idx = 0; idx < sizeof(s->s3.client_random); idx++) {
1138 i = (s->hello_retry_request == SSL_HRR_NONE);
1141 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random),
1142 DOWNGRADE_NONE) <= 0) {
1143 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1148 * version indicates the negotiated version: for example from
1149 * an SSLv2/v3 compatible client hello). The client_version
1150 * field is the maximum version we permit and it is also
1151 * used in RSA encrypted premaster secrets. Some servers can
1152 * choke if we initially report a higher version then
1153 * renegotiate to a lower one in the premaster secret. This
1154 * didn't happen with TLS 1.0 as most servers supported it
1155 * but it can with TLS 1.1 or later if the server only supports
1158 * Possible scenario with previous logic:
1159 * 1. Client hello indicates TLS 1.2
1160 * 2. Server hello says TLS 1.0
1161 * 3. RSA encrypted premaster secret uses 1.2.
1162 * 4. Handshake proceeds using TLS 1.0.
1163 * 5. Server sends hello request to renegotiate.
1164 * 6. Client hello indicates TLS v1.0 as we now
1165 * know that is maximum server supports.
1166 * 7. Server chokes on RSA encrypted premaster secret
1167 * containing version 1.0.
1169 * For interoperability it should be OK to always use the
1170 * maximum version we support in client hello and then rely
1171 * on the checking of version to ensure the servers isn't
1172 * being inconsistent: for example initially negotiating with
1173 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
1174 * client_version in client hello and not resetting it to
1175 * the negotiated version.
1177 * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
1178 * supported_versions extension for the real supported versions.
1180 if (!WPACKET_put_bytes_u16(pkt, s->client_version)
1181 || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) {
1182 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1187 session_id = s->session->session_id;
1188 if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
1189 if (s->version == TLS1_3_VERSION
1190 && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
1191 sess_id_len = sizeof(s->tmp_session_id);
1192 s->tmp_session_id_len = sess_id_len;
1193 session_id = s->tmp_session_id;
1194 if (s->hello_retry_request == SSL_HRR_NONE
1195 && RAND_bytes_ex(s->ctx->libctx, s->tmp_session_id,
1196 sess_id_len, 0) <= 0) {
1197 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1204 assert(s->session->session_id_length <= sizeof(s->session->session_id));
1205 sess_id_len = s->session->session_id_length;
1206 if (s->version == TLS1_3_VERSION) {
1207 s->tmp_session_id_len = sess_id_len;
1208 memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
1211 if (!WPACKET_start_sub_packet_u8(pkt)
1212 || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id,
1214 || !WPACKET_close(pkt)) {
1215 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1219 /* cookie stuff for DTLS */
1220 if (SSL_IS_DTLS(s)) {
1221 if (s->d1->cookie_len > sizeof(s->d1->cookie)
1222 || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
1223 s->d1->cookie_len)) {
1224 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1229 /* Ciphers supported */
1230 if (!WPACKET_start_sub_packet_u16(pkt)) {
1231 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1235 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt)) {
1236 /* SSLfatal() already called */
1239 if (!WPACKET_close(pkt)) {
1240 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1245 if (!WPACKET_start_sub_packet_u8(pkt)) {
1246 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1249 #ifndef OPENSSL_NO_COMP
1250 if (ssl_allow_compression(s)
1251 && s->ctx->comp_methods
1252 && (SSL_IS_DTLS(s) || s->s3.tmp.max_ver < TLS1_3_VERSION)) {
1253 int compnum = sk_SSL_COMP_num(s->ctx->comp_methods);
1254 for (i = 0; i < compnum; i++) {
1255 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1256 if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
1257 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1263 /* Add the NULL method */
1264 if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
1265 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1269 /* TLS extensions */
1270 if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
1271 /* SSLfatal() already called */
1278 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
1283 if (!PACKET_forward(pkt, 2)
1284 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1285 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1286 return MSG_PROCESS_ERROR;
1289 cookie_len = PACKET_remaining(&cookiepkt);
1290 if (cookie_len > sizeof(s->d1->cookie)) {
1291 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG);
1292 return MSG_PROCESS_ERROR;
1295 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1296 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1297 return MSG_PROCESS_ERROR;
1299 s->d1->cookie_len = cookie_len;
1301 return MSG_PROCESS_FINISHED_READING;
1304 static int set_client_ciphersuite(SSL *s, const unsigned char *cipherchars)
1306 STACK_OF(SSL_CIPHER) *sk;
1307 const SSL_CIPHER *c;
1310 c = ssl_get_cipher_by_char(s, cipherchars, 0);
1312 /* unknown cipher */
1313 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED);
1317 * If it is a disabled cipher we either didn't send it in client hello,
1318 * or it's not allowed for the selected protocol. So we return an error.
1320 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
1321 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1325 sk = ssl_get_ciphers_by_id(s);
1326 i = sk_SSL_CIPHER_find(sk, c);
1328 /* we did not say we would use this cipher */
1329 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1333 if (SSL_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL
1334 && s->s3.tmp.new_cipher->id != c->id) {
1335 /* ServerHello selected a different ciphersuite to that in the HRR */
1336 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1341 * Depending on the session caching (internal/external), the cipher
1342 * and/or cipher_id values may not be set. Make sure that cipher_id is
1343 * set and use it for comparison.
1345 if (s->session->cipher != NULL)
1346 s->session->cipher_id = s->session->cipher->id;
1347 if (s->hit && (s->session->cipher_id != c->id)) {
1348 if (SSL_IS_TLS13(s)) {
1350 * In TLSv1.3 it is valid for the server to select a different
1351 * ciphersuite as long as the hash is the same.
1353 if (ssl_md(s->ctx, c->algorithm2)
1354 != ssl_md(s->ctx, s->session->cipher->algorithm2)) {
1355 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1356 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
1361 * Prior to TLSv1.3 resuming a session always meant using the same
1364 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1365 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1369 s->s3.tmp.new_cipher = c;
1374 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
1376 PACKET session_id, extpkt;
1377 size_t session_id_len;
1378 const unsigned char *cipherchars;
1380 unsigned int compression;
1381 unsigned int sversion;
1382 unsigned int context;
1383 RAW_EXTENSION *extensions = NULL;
1384 #ifndef OPENSSL_NO_COMP
1388 if (!PACKET_get_net_2(pkt, &sversion)) {
1389 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1393 /* load the server random */
1394 if (s->version == TLS1_3_VERSION
1395 && sversion == TLS1_2_VERSION
1396 && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
1397 && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
1398 s->hello_retry_request = SSL_HRR_PENDING;
1400 if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
1401 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1405 if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) {
1406 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1411 /* Get the session-id. */
1412 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1413 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1416 session_id_len = PACKET_remaining(&session_id);
1417 if (session_id_len > sizeof(s->session->session_id)
1418 || session_id_len > SSL3_SESSION_ID_SIZE) {
1419 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1423 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1424 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1428 if (!PACKET_get_1(pkt, &compression)) {
1429 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1433 /* TLS extensions */
1434 if (PACKET_remaining(pkt) == 0 && !hrr) {
1435 PACKET_null_init(&extpkt);
1436 } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
1437 || PACKET_remaining(pkt) != 0) {
1438 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1443 if (!tls_collect_extensions(s, &extpkt,
1444 SSL_EXT_TLS1_2_SERVER_HELLO
1445 | SSL_EXT_TLS1_3_SERVER_HELLO,
1446 &extensions, NULL, 1)) {
1447 /* SSLfatal() already called */
1451 if (!ssl_choose_client_version(s, sversion, extensions)) {
1452 /* SSLfatal() already called */
1457 if (SSL_IS_TLS13(s) || hrr) {
1458 if (compression != 0) {
1459 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1460 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1464 if (session_id_len != s->tmp_session_id_len
1465 || memcmp(PACKET_data(&session_id), s->tmp_session_id,
1466 session_id_len) != 0) {
1467 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID);
1473 if (!set_client_ciphersuite(s, cipherchars)) {
1474 /* SSLfatal() already called */
1478 return tls_process_as_hello_retry_request(s, &extpkt);
1482 * Now we have chosen the version we need to check again that the extensions
1483 * are appropriate for this version.
1485 context = SSL_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
1486 : SSL_EXT_TLS1_2_SERVER_HELLO;
1487 if (!tls_validate_all_contexts(s, context, extensions)) {
1488 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1494 if (SSL_IS_TLS13(s)) {
1496 * In TLSv1.3 a ServerHello message signals a key change so the end of
1497 * the message must be on a record boundary.
1499 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1500 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1501 SSL_R_NOT_ON_RECORD_BOUNDARY);
1505 /* This will set s->hit if we are resuming */
1506 if (!tls_parse_extension(s, TLSEXT_IDX_psk,
1507 SSL_EXT_TLS1_3_SERVER_HELLO,
1508 extensions, NULL, 0)) {
1509 /* SSLfatal() already called */
1514 * Check if we can resume the session based on external pre-shared
1515 * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
1516 * Resumption based on server-side state works with session IDs.
1517 * Resumption based on pre-shared Protected Access Credentials (PACs)
1518 * works by overriding the SessionTicket extension at the application
1519 * layer, and does not send a session ID. (We do not know whether
1520 * EAP-FAST servers would honour the session ID.) Therefore, the session
1521 * ID alone is not a reliable indicator of session resumption, so we
1522 * first check if we can resume, and later peek at the next handshake
1523 * message to see if the server wants to resume.
1525 if (s->version >= TLS1_VERSION
1526 && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
1527 const SSL_CIPHER *pref_cipher = NULL;
1529 * s->session->master_key_length is a size_t, but this is an int for
1530 * backwards compat reasons
1532 int master_key_length;
1533 master_key_length = sizeof(s->session->master_key);
1534 if (s->ext.session_secret_cb(s, s->session->master_key,
1537 s->ext.session_secret_cb_arg)
1538 && master_key_length > 0) {
1539 s->session->master_key_length = master_key_length;
1540 s->session->cipher = pref_cipher ?
1541 pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
1543 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1548 if (session_id_len != 0
1549 && session_id_len == s->session->session_id_length
1550 && memcmp(PACKET_data(&session_id), s->session->session_id,
1551 session_id_len) == 0)
1556 if (s->sid_ctx_length != s->session->sid_ctx_length
1557 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1558 /* actually a client application bug */
1559 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1560 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1565 * If we were trying for session-id reuse but the server
1566 * didn't resume, make a new SSL_SESSION.
1567 * In the case of EAP-FAST and PAC, we do not send a session ID,
1568 * so the PAC-based session secret is always preserved. It'll be
1569 * overwritten if the server refuses resumption.
1571 if (s->session->session_id_length > 0) {
1572 tsan_counter(&s->session_ctx->stats.sess_miss);
1573 if (!ssl_get_new_session(s, 0)) {
1574 /* SSLfatal() already called */
1579 s->session->ssl_version = s->version;
1581 * In TLSv1.2 and below we save the session id we were sent so we can
1582 * resume it later. In TLSv1.3 the session id we were sent is just an
1583 * echo of what we originally sent in the ClientHello and should not be
1584 * used for resumption.
1586 if (!SSL_IS_TLS13(s)) {
1587 s->session->session_id_length = session_id_len;
1588 /* session_id_len could be 0 */
1589 if (session_id_len > 0)
1590 memcpy(s->session->session_id, PACKET_data(&session_id),
1595 /* Session version and negotiated protocol version should match */
1596 if (s->version != s->session->ssl_version) {
1597 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1598 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1602 * Now that we know the version, update the check to see if it's an allowed
1605 s->s3.tmp.min_ver = s->version;
1606 s->s3.tmp.max_ver = s->version;
1608 if (!set_client_ciphersuite(s, cipherchars)) {
1609 /* SSLfatal() already called */
1613 #ifdef OPENSSL_NO_COMP
1614 if (compression != 0) {
1615 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1616 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1620 * If compression is disabled we'd better not try to resume a session
1621 * using compression.
1623 if (s->session->compress_meth != 0) {
1624 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
1628 if (s->hit && compression != s->session->compress_meth) {
1629 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1630 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1633 if (compression == 0)
1635 else if (!ssl_allow_compression(s)) {
1636 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED);
1639 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1642 if (compression != 0 && comp == NULL) {
1643 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1644 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1647 s->s3.tmp.new_compression = comp;
1651 if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
1652 /* SSLfatal() already called */
1656 #ifndef OPENSSL_NO_SCTP
1657 if (SSL_IS_DTLS(s) && s->hit) {
1658 unsigned char sctpauthkey[64];
1659 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1663 * Add new shared key for SCTP-Auth, will be ignored if
1666 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1667 sizeof(DTLS1_SCTP_AUTH_LABEL));
1669 /* Don't include the terminating zero. */
1670 labellen = sizeof(labelbuffer) - 1;
1671 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
1674 if (SSL_export_keying_material(s, sctpauthkey,
1675 sizeof(sctpauthkey),
1677 labellen, NULL, 0, 0) <= 0) {
1678 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1682 BIO_ctrl(SSL_get_wbio(s),
1683 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1684 sizeof(sctpauthkey), sctpauthkey);
1689 * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1690 * we're done with this message
1693 && (!s->method->ssl3_enc->setup_key_block(s)
1694 || !s->method->ssl3_enc->change_cipher_state(s,
1695 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) {
1696 /* SSLfatal() already called */
1700 OPENSSL_free(extensions);
1701 return MSG_PROCESS_CONTINUE_READING;
1703 OPENSSL_free(extensions);
1704 return MSG_PROCESS_ERROR;
1707 static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL *s,
1710 RAW_EXTENSION *extensions = NULL;
1713 * If we were sending early_data then the enc_write_ctx is now invalid and
1714 * should not be used.
1716 EVP_CIPHER_CTX_free(s->enc_write_ctx);
1717 s->enc_write_ctx = NULL;
1719 if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1720 &extensions, NULL, 1)
1721 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1722 extensions, NULL, 0, 1)) {
1723 /* SSLfatal() already called */
1727 OPENSSL_free(extensions);
1730 if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) {
1732 * We didn't receive a cookie or a new key_share so the next
1733 * ClientHello will not change
1735 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR);
1740 * Re-initialise the Transcript Hash. We're going to prepopulate it with
1741 * a synthetic message_hash in place of ClientHello1.
1743 if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
1744 /* SSLfatal() already called */
1749 * Add this message to the Transcript Hash. Normally this is done
1750 * automatically prior to the message processing stage. However due to the
1751 * need to create the synthetic message hash, we defer that step until now
1754 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1755 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1756 /* SSLfatal() already called */
1760 return MSG_PROCESS_FINISHED_READING;
1762 OPENSSL_free(extensions);
1763 return MSG_PROCESS_ERROR;
1766 /* prepare server cert verification by setting s->session->peer_chain from pkt */
1767 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1769 unsigned long cert_list_len, cert_len;
1771 const unsigned char *certstart, *certbytes;
1773 unsigned int context = 0;
1775 if ((s->session->peer_chain = sk_X509_new_null()) == NULL) {
1776 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1780 if ((SSL_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
1782 || !PACKET_get_net_3(pkt, &cert_list_len)
1783 || PACKET_remaining(pkt) != cert_list_len
1784 || PACKET_remaining(pkt) == 0) {
1785 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1788 for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
1789 if (!PACKET_get_net_3(pkt, &cert_len)
1790 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1791 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
1795 certstart = certbytes;
1796 x = X509_new_ex(s->ctx->libctx, s->ctx->propq);
1798 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_MALLOC_FAILURE);
1799 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1802 if (d2i_X509(&x, (const unsigned char **)&certbytes,
1803 cert_len) == NULL) {
1804 SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB);
1808 if (certbytes != (certstart + cert_len)) {
1809 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
1813 if (SSL_IS_TLS13(s)) {
1814 RAW_EXTENSION *rawexts = NULL;
1817 if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
1818 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1821 if (!tls_collect_extensions(s, &extensions,
1822 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
1823 NULL, chainidx == 0)
1824 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
1825 rawexts, x, chainidx,
1826 PACKET_remaining(pkt) == 0)) {
1827 OPENSSL_free(rawexts);
1828 /* SSLfatal already called */
1831 OPENSSL_free(rawexts);
1834 if (!sk_X509_push(s->session->peer_chain, x)) {
1835 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1840 return MSG_PROCESS_CONTINUE_PROCESSING;
1844 sk_X509_pop_free(s->session->peer_chain, X509_free);
1845 s->session->peer_chain = NULL;
1846 return MSG_PROCESS_ERROR;
1850 * Verify the s->session->peer_chain and check server cert type.
1851 * On success set s->session->peer and s->session->verify_result.
1852 * Else the peer certificate verification callback may request retry.
1854 WORK_STATE tls_post_process_server_certificate(SSL *s, WORK_STATE wst)
1857 EVP_PKEY *pkey = NULL;
1858 const SSL_CERT_LOOKUP *clu;
1862 i = ssl_verify_cert_chain(s, s->session->peer_chain);
1864 s->rwstate = SSL_RETRY_VERIFY;
1868 * The documented interface is that SSL_VERIFY_PEER should be set in order
1869 * for client side verification of the server certificate to take place.
1870 * However, historically the code has only checked that *any* flag is set
1871 * to cause server verification to take place. Use of the other flags makes
1872 * no sense in client mode. An attempt to clean up the semantics was
1873 * reverted because at least one application *only* set
1874 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1875 * server verification to take place, after the clean up it silently did
1876 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1877 * sent to them because they are void functions. Therefore, we now use the
1878 * (less clean) historic behaviour of performing validation if any flag is
1879 * set. The *documented* interface remains the same.
1881 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1882 SSLfatal(s, ssl_x509err2alert(s->verify_result),
1883 SSL_R_CERTIFICATE_VERIFY_FAILED);
1886 ERR_clear_error(); /* but we keep s->verify_result */
1888 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, i);
1893 * Inconsistency alert: cert_chain does include the peer's certificate,
1894 * which we don't include in statem_srvr.c
1896 x = sk_X509_value(s->session->peer_chain, 0);
1898 pkey = X509_get0_pubkey(x);
1900 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1901 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1902 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1906 if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx)) == NULL) {
1907 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1911 * Check certificate type is consistent with ciphersuite. For TLS 1.3
1912 * skip check since TLS 1.3 ciphersuites can be used with any certificate
1915 if (!SSL_IS_TLS13(s)) {
1916 if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) {
1917 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE);
1922 X509_free(s->session->peer);
1924 s->session->peer = x;
1925 s->session->verify_result = s->verify_result;
1927 /* Save the current hash state for when we receive the CertificateVerify */
1929 && !ssl_handshake_hash(s, s->cert_verify_hash,
1930 sizeof(s->cert_verify_hash),
1931 &s->cert_verify_hash_len)) {
1932 /* SSLfatal() already called */;
1935 return WORK_FINISHED_CONTINUE;
1938 static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt)
1940 #ifndef OPENSSL_NO_PSK
1941 PACKET psk_identity_hint;
1943 /* PSK ciphersuites are preceded by an identity hint */
1945 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1946 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1951 * Store PSK identity hint for later use, hint is used in
1952 * tls_construct_client_key_exchange. Assume that the maximum length of
1953 * a PSK identity hint can be as long as the maximum length of a PSK
1956 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1957 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG);
1961 if (PACKET_remaining(&psk_identity_hint) == 0) {
1962 OPENSSL_free(s->session->psk_identity_hint);
1963 s->session->psk_identity_hint = NULL;
1964 } else if (!PACKET_strndup(&psk_identity_hint,
1965 &s->session->psk_identity_hint)) {
1966 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1972 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1977 static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
1979 #ifndef OPENSSL_NO_SRP
1980 PACKET prime, generator, salt, server_pub;
1982 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1983 || !PACKET_get_length_prefixed_2(pkt, &generator)
1984 || !PACKET_get_length_prefixed_1(pkt, &salt)
1985 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1986 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1991 BN_bin2bn(PACKET_data(&prime),
1992 (int)PACKET_remaining(&prime), NULL)) == NULL
1994 BN_bin2bn(PACKET_data(&generator),
1995 (int)PACKET_remaining(&generator), NULL)) == NULL
1997 BN_bin2bn(PACKET_data(&salt),
1998 (int)PACKET_remaining(&salt), NULL)) == NULL
2000 BN_bin2bn(PACKET_data(&server_pub),
2001 (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
2002 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2006 if (!srp_verify_server_param(s)) {
2007 /* SSLfatal() already called */
2011 /* We must check if there is a certificate */
2012 if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2013 *pkey = X509_get0_pubkey(s->session->peer);
2017 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2022 static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
2024 PACKET prime, generator, pub_key;
2025 EVP_PKEY *peer_tmp = NULL;
2026 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
2027 EVP_PKEY_CTX *pctx = NULL;
2028 OSSL_PARAM *params = NULL;
2029 OSSL_PARAM_BLD *tmpl = NULL;
2032 if (!PACKET_get_length_prefixed_2(pkt, &prime)
2033 || !PACKET_get_length_prefixed_2(pkt, &generator)
2034 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
2035 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2039 p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
2040 g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
2042 bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
2043 (int)PACKET_remaining(&pub_key), NULL);
2044 if (p == NULL || g == NULL || bnpub_key == NULL) {
2045 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2049 tmpl = OSSL_PARAM_BLD_new();
2051 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
2052 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
2053 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
2055 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
2056 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2060 pctx = EVP_PKEY_CTX_new_from_name(s->ctx->libctx, "DH", s->ctx->propq);
2062 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2065 if (EVP_PKEY_fromdata_init(pctx) <= 0
2066 || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) {
2067 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE);
2071 EVP_PKEY_CTX_free(pctx);
2072 pctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, peer_tmp, s->ctx->propq);
2075 * EVP_PKEY_param_check() will verify that the DH params are using
2076 * a safe prime. In this context, because we're using ephemeral DH,
2077 * we're ok with it not being a safe prime.
2078 * EVP_PKEY_param_check_quick() skips the safe prime check.
2080 || EVP_PKEY_param_check_quick(pctx) != 1
2081 || EVP_PKEY_public_check(pctx) != 1) {
2082 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE);
2086 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2087 EVP_PKEY_get_security_bits(peer_tmp),
2089 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
2093 s->s3.peer_tmp = peer_tmp;
2097 * FIXME: This makes assumptions about which ciphersuites come with
2098 * public keys. We should have a less ad-hoc way of doing this
2100 if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2101 *pkey = X509_get0_pubkey(s->session->peer);
2102 /* else anonymous DH, so no certificate or pkey. */
2107 OSSL_PARAM_BLD_free(tmpl);
2108 OSSL_PARAM_free(params);
2109 EVP_PKEY_free(peer_tmp);
2110 EVP_PKEY_CTX_free(pctx);
2118 static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
2121 unsigned int curve_type, curve_id;
2124 * Extract elliptic curve parameters and the server's ephemeral ECDH
2125 * public key. We only support named (not generic) curves and
2126 * ECParameters in this case is just three bytes.
2128 if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
2129 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2133 * Check curve is named curve type and one of our preferences, if not
2134 * server has sent an invalid curve.
2136 if (curve_type != NAMED_CURVE_TYPE
2137 || !tls1_check_group_id(s, curve_id, 1)) {
2138 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
2142 if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) {
2143 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2144 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
2148 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
2149 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2153 if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
2154 PACKET_data(&encoded_pt),
2155 PACKET_remaining(&encoded_pt)) <= 0) {
2156 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
2161 * The ECC/TLS specification does not mention the use of DSA to sign
2162 * ECParameters in the server key exchange message. We do support RSA
2165 if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
2166 *pkey = X509_get0_pubkey(s->session->peer);
2167 else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
2168 *pkey = X509_get0_pubkey(s->session->peer);
2169 /* else anonymous ECDH, so no certificate or pkey. */
2171 /* Cache the agreed upon group in the SSL_SESSION */
2172 s->session->kex_group = curve_id;
2176 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
2179 EVP_PKEY *pkey = NULL;
2180 EVP_MD_CTX *md_ctx = NULL;
2181 EVP_PKEY_CTX *pctx = NULL;
2182 PACKET save_param_start, signature;
2184 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
2186 save_param_start = *pkt;
2188 EVP_PKEY_free(s->s3.peer_tmp);
2189 s->s3.peer_tmp = NULL;
2191 if (alg_k & SSL_PSK) {
2192 if (!tls_process_ske_psk_preamble(s, pkt)) {
2193 /* SSLfatal() already called */
2198 /* Nothing else to do for plain PSK or RSAPSK */
2199 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
2200 } else if (alg_k & SSL_kSRP) {
2201 if (!tls_process_ske_srp(s, pkt, &pkey)) {
2202 /* SSLfatal() already called */
2205 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2206 if (!tls_process_ske_dhe(s, pkt, &pkey)) {
2207 /* SSLfatal() already called */
2210 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2211 if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
2212 /* SSLfatal() already called */
2216 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
2220 /* if it was signed, check the signature */
2223 const EVP_MD *md = NULL;
2229 * |pkt| now points to the beginning of the signature, so the difference
2230 * equals the length of the parameters.
2232 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
2233 PACKET_remaining(&save_param_start) -
2234 PACKET_remaining(pkt))) {
2235 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
2239 if (SSL_USE_SIGALGS(s)) {
2240 unsigned int sigalg;
2242 if (!PACKET_get_net_2(pkt, &sigalg)) {
2243 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2246 if (tls12_check_peer_sigalg(s, sigalg, pkey) <=0) {
2247 /* SSLfatal() already called */
2250 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
2251 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2255 if (!tls1_lookup_md(s->ctx, s->s3.tmp.peer_sigalg, &md)) {
2256 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2257 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
2260 if (SSL_USE_SIGALGS(s))
2261 OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
2262 md == NULL ? "n/a" : EVP_MD_get0_name(md));
2264 if (!PACKET_get_length_prefixed_2(pkt, &signature)
2265 || PACKET_remaining(pkt) != 0) {
2266 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2270 md_ctx = EVP_MD_CTX_new();
2271 if (md_ctx == NULL) {
2272 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2276 if (EVP_DigestVerifyInit_ex(md_ctx, &pctx,
2277 md == NULL ? NULL : EVP_MD_get0_name(md),
2278 s->ctx->libctx, s->ctx->propq, pkey,
2280 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2283 if (SSL_USE_PSS(s)) {
2284 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2285 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
2286 RSA_PSS_SALTLEN_DIGEST) <= 0) {
2287 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2291 tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(¶ms),
2292 PACKET_remaining(¶ms));
2294 /* SSLfatal() already called */
2298 rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
2299 PACKET_remaining(&signature), tbs, tbslen);
2302 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
2305 EVP_MD_CTX_free(md_ctx);
2308 /* aNULL, aSRP or PSK do not need public keys */
2309 if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
2310 && !(alg_k & SSL_PSK)) {
2311 /* Might be wrong key type, check it */
2312 if (ssl3_check_cert_and_algorithm(s)) {
2313 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA);
2315 /* else this shouldn't happen, SSLfatal() already called */
2318 /* still data left over */
2319 if (PACKET_remaining(pkt) != 0) {
2320 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE);
2325 return MSG_PROCESS_CONTINUE_READING;
2327 EVP_MD_CTX_free(md_ctx);
2328 return MSG_PROCESS_ERROR;
2331 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
2335 /* Clear certificate validity flags */
2336 for (i = 0; i < SSL_PKEY_NUM; i++)
2337 s->s3.tmp.valid_flags[i] = 0;
2339 if (SSL_IS_TLS13(s)) {
2340 PACKET reqctx, extensions;
2341 RAW_EXTENSION *rawexts = NULL;
2343 if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
2345 * We already sent close_notify. This can only happen in TLSv1.3
2346 * post-handshake messages. We can't reasonably respond to this, so
2349 return MSG_PROCESS_FINISHED_READING;
2352 /* Free and zero certificate types: it is not present in TLS 1.3 */
2353 OPENSSL_free(s->s3.tmp.ctype);
2354 s->s3.tmp.ctype = NULL;
2355 s->s3.tmp.ctype_len = 0;
2356 OPENSSL_free(s->pha_context);
2357 s->pha_context = NULL;
2358 s->pha_context_len = 0;
2360 if (!PACKET_get_length_prefixed_1(pkt, &reqctx) ||
2361 !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
2362 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2363 return MSG_PROCESS_ERROR;
2366 if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
2367 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
2368 return MSG_PROCESS_ERROR;
2370 if (!tls_collect_extensions(s, &extensions,
2371 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2373 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2374 rawexts, NULL, 0, 1)) {
2375 /* SSLfatal() already called */
2376 OPENSSL_free(rawexts);
2377 return MSG_PROCESS_ERROR;
2379 OPENSSL_free(rawexts);
2380 if (!tls1_process_sigalgs(s)) {
2381 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
2382 return MSG_PROCESS_ERROR;
2387 /* get the certificate types */
2388 if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
2389 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2390 return MSG_PROCESS_ERROR;
2393 if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
2394 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2395 return MSG_PROCESS_ERROR;
2398 if (SSL_USE_SIGALGS(s)) {
2401 if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
2402 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2403 return MSG_PROCESS_ERROR;
2407 * Despite this being for certificates, preserve compatibility
2408 * with pre-TLS 1.3 and use the regular sigalgs field.
2410 if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
2411 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2412 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2413 return MSG_PROCESS_ERROR;
2415 if (!tls1_process_sigalgs(s)) {
2416 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2417 return MSG_PROCESS_ERROR;
2421 /* get the CA RDNs */
2422 if (!parse_ca_names(s, pkt)) {
2423 /* SSLfatal() already called */
2424 return MSG_PROCESS_ERROR;
2428 if (PACKET_remaining(pkt) != 0) {
2429 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2430 return MSG_PROCESS_ERROR;
2433 /* we should setup a certificate to return.... */
2434 s->s3.tmp.cert_req = 1;
2437 * In TLSv1.3 we don't prepare the client certificate yet. We wait until
2438 * after the CertificateVerify message has been received. This is because
2439 * in TLSv1.3 the CertificateRequest arrives before the Certificate message
2440 * but in TLSv1.2 it is the other way around. We want to make sure that
2441 * SSL_get1_peer_certificate() returns something sensible in
2444 if (SSL_IS_TLS13(s) && s->post_handshake_auth != SSL_PHA_REQUESTED)
2445 return MSG_PROCESS_CONTINUE_READING;
2447 return MSG_PROCESS_CONTINUE_PROCESSING;
2450 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
2452 unsigned int ticklen;
2453 unsigned long ticket_lifetime_hint, age_add = 0;
2454 unsigned int sess_len;
2455 RAW_EXTENSION *exts = NULL;
2457 EVP_MD *sha256 = NULL;
2459 PACKET_null_init(&nonce);
2461 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2463 && (!PACKET_get_net_4(pkt, &age_add)
2464 || !PACKET_get_length_prefixed_1(pkt, &nonce)))
2465 || !PACKET_get_net_2(pkt, &ticklen)
2466 || (SSL_IS_TLS13(s) ? (ticklen == 0 || PACKET_remaining(pkt) < ticklen)
2467 : PACKET_remaining(pkt) != ticklen)) {
2468 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2473 * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
2474 * ticket. We already checked this TLSv1.3 case above, so it should never
2475 * be 0 here in that instance
2478 return MSG_PROCESS_CONTINUE_READING;
2481 * Sessions must be immutable once they go into the session cache. Otherwise
2482 * we can get multi-thread problems. Therefore we don't "update" sessions,
2483 * we replace them with a duplicate. In TLSv1.3 we need to do this every
2484 * time a NewSessionTicket arrives because those messages arrive
2485 * post-handshake and the session may have already gone into the session
2488 if (SSL_IS_TLS13(s) || s->session->session_id_length > 0) {
2489 SSL_SESSION *new_sess;
2492 * We reused an existing session, so we need to replace it with a new
2495 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2496 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2500 if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
2501 && !SSL_IS_TLS13(s)) {
2503 * In TLSv1.2 and below the arrival of a new tickets signals that
2504 * any old ticket we were using is now out of date, so we remove the
2505 * old session from the cache. We carry on if this fails
2507 SSL_CTX_remove_session(s->session_ctx, s->session);
2510 SSL_SESSION_free(s->session);
2511 s->session = new_sess;
2514 s->session->time = time(NULL);
2515 ssl_session_calculate_timeout(s->session);
2517 OPENSSL_free(s->session->ext.tick);
2518 s->session->ext.tick = NULL;
2519 s->session->ext.ticklen = 0;
2521 s->session->ext.tick = OPENSSL_malloc(ticklen);
2522 if (s->session->ext.tick == NULL) {
2523 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2526 if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
2527 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2531 s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
2532 s->session->ext.tick_age_add = age_add;
2533 s->session->ext.ticklen = ticklen;
2535 if (SSL_IS_TLS13(s)) {
2538 if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
2539 || PACKET_remaining(pkt) != 0) {
2540 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2544 if (!tls_collect_extensions(s, &extpkt,
2545 SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
2547 || !tls_parse_all_extensions(s,
2548 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
2549 exts, NULL, 0, 1)) {
2550 /* SSLfatal() already called */
2556 * There are two ways to detect a resumed ticket session. One is to set
2557 * an appropriate session ID and then the server must return a match in
2558 * ServerHello. This allows the normal client session ID matching to work
2559 * and we know much earlier that the ticket has been accepted. The
2560 * other way is to set zero length session ID when the ticket is
2561 * presented and rely on the handshake to determine session resumption.
2562 * We choose the former approach because this fits in with assumptions
2563 * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
2566 sha256 = EVP_MD_fetch(s->ctx->libctx, "SHA2-256", s->ctx->propq);
2567 if (sha256 == NULL) {
2568 /* Error is already recorded */
2569 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
2573 * We use sess_len here because EVP_Digest expects an int
2574 * but s->session->session_id_length is a size_t
2576 if (!EVP_Digest(s->session->ext.tick, ticklen,
2577 s->session->session_id, &sess_len,
2579 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2582 EVP_MD_free(sha256);
2584 s->session->session_id_length = sess_len;
2585 s->session->not_resumable = 0;
2587 /* This is a standalone message in TLSv1.3, so there is no more to read */
2588 if (SSL_IS_TLS13(s)) {
2589 const EVP_MD *md = ssl_handshake_md(s);
2590 int hashleni = EVP_MD_get_size(md);
2592 static const unsigned char nonce_label[] = "resumption";
2594 /* Ensure cast to size_t is safe */
2595 if (!ossl_assert(hashleni >= 0)) {
2596 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2599 hashlen = (size_t)hashleni;
2601 if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
2603 sizeof(nonce_label) - 1,
2604 PACKET_data(&nonce),
2605 PACKET_remaining(&nonce),
2606 s->session->master_key,
2608 /* SSLfatal() already called */
2611 s->session->master_key_length = hashlen;
2614 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
2615 return MSG_PROCESS_FINISHED_READING;
2618 return MSG_PROCESS_CONTINUE_READING;
2620 EVP_MD_free(sha256);
2622 return MSG_PROCESS_ERROR;
2626 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
2627 * parse a separate message. Returns 1 on success or 0 on failure
2629 int tls_process_cert_status_body(SSL *s, PACKET *pkt)
2634 if (!PACKET_get_1(pkt, &type)
2635 || type != TLSEXT_STATUSTYPE_ocsp) {
2636 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE);
2639 if (!PACKET_get_net_3_len(pkt, &resplen)
2640 || PACKET_remaining(pkt) != resplen) {
2641 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2644 s->ext.ocsp.resp = OPENSSL_malloc(resplen);
2645 if (s->ext.ocsp.resp == NULL) {
2646 s->ext.ocsp.resp_len = 0;
2647 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2650 s->ext.ocsp.resp_len = resplen;
2651 if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
2652 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2660 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
2662 if (!tls_process_cert_status_body(s, pkt)) {
2663 /* SSLfatal() already called */
2664 return MSG_PROCESS_ERROR;
2667 return MSG_PROCESS_CONTINUE_READING;
2671 * Perform miscellaneous checks and processing after we have received the
2672 * server's initial flight. In TLS1.3 this is after the Server Finished message.
2673 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
2676 int tls_process_initial_server_flight(SSL *s)
2679 * at this point we check that we have the required stuff from
2682 if (!ssl3_check_cert_and_algorithm(s)) {
2683 /* SSLfatal() already called */
2688 * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
2689 * |ext.ocsp.resp_len| values will be set if we actually received a status
2690 * message, or NULL and -1 otherwise
2692 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
2693 && s->ctx->ext.status_cb != NULL) {
2694 int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
2697 SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
2698 SSL_R_INVALID_STATUS_RESPONSE);
2702 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2703 SSL_R_OCSP_CALLBACK_FAILURE);
2707 #ifndef OPENSSL_NO_CT
2708 if (s->ct_validation_callback != NULL) {
2709 /* Note we validate the SCTs whether or not we abort on error */
2710 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2711 /* SSLfatal() already called */
2720 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2722 if (PACKET_remaining(pkt) > 0) {
2723 /* should contain no data */
2724 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2725 return MSG_PROCESS_ERROR;
2727 #ifndef OPENSSL_NO_SRP
2728 if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2729 if (ssl_srp_calc_a_param_intern(s) <= 0) {
2730 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC);
2731 return MSG_PROCESS_ERROR;
2736 if (!tls_process_initial_server_flight(s)) {
2737 /* SSLfatal() already called */
2738 return MSG_PROCESS_ERROR;
2741 return MSG_PROCESS_FINISHED_READING;
2744 static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt)
2746 #ifndef OPENSSL_NO_PSK
2749 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2750 * \0-terminated identity. The last byte is for us for simulating
2753 char identity[PSK_MAX_IDENTITY_LEN + 1];
2754 size_t identitylen = 0;
2755 unsigned char psk[PSK_MAX_PSK_LEN];
2756 unsigned char *tmppsk = NULL;
2757 char *tmpidentity = NULL;
2760 if (s->psk_client_callback == NULL) {
2761 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB);
2765 memset(identity, 0, sizeof(identity));
2767 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2768 identity, sizeof(identity) - 1,
2771 if (psklen > PSK_MAX_PSK_LEN) {
2772 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
2773 psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
2775 } else if (psklen == 0) {
2776 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND);
2780 identitylen = strlen(identity);
2781 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2782 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2786 tmppsk = OPENSSL_memdup(psk, psklen);
2787 tmpidentity = OPENSSL_strdup(identity);
2788 if (tmppsk == NULL || tmpidentity == NULL) {
2789 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2793 OPENSSL_free(s->s3.tmp.psk);
2794 s->s3.tmp.psk = tmppsk;
2795 s->s3.tmp.psklen = psklen;
2797 OPENSSL_free(s->session->psk_identity);
2798 s->session->psk_identity = tmpidentity;
2801 if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
2802 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2809 OPENSSL_cleanse(psk, psklen);
2810 OPENSSL_cleanse(identity, sizeof(identity));
2811 OPENSSL_clear_free(tmppsk, psklen);
2812 OPENSSL_clear_free(tmpidentity, identitylen);
2816 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2821 static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt)
2823 unsigned char *encdata = NULL;
2824 EVP_PKEY *pkey = NULL;
2825 EVP_PKEY_CTX *pctx = NULL;
2827 unsigned char *pms = NULL;
2830 if (s->session->peer == NULL) {
2832 * We should always have a server certificate with SSL_kRSA.
2834 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2838 pkey = X509_get0_pubkey(s->session->peer);
2839 if (!EVP_PKEY_is_a(pkey, "RSA")) {
2840 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2844 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2845 pms = OPENSSL_malloc(pmslen);
2847 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2851 pms[0] = s->client_version >> 8;
2852 pms[1] = s->client_version & 0xff;
2853 if (RAND_bytes_ex(s->ctx->libctx, pms + 2, pmslen - 2, 0) <= 0) {
2854 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2858 /* Fix buf for TLS and beyond */
2859 if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
2860 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2864 pctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pkey, s->ctx->propq);
2865 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2866 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2867 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2870 if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
2871 || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
2872 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT);
2875 EVP_PKEY_CTX_free(pctx);
2878 /* Fix buf for TLS and beyond */
2879 if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
2880 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2884 /* Log the premaster secret, if logging is enabled. */
2885 if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
2886 /* SSLfatal() already called */
2890 s->s3.tmp.pms = pms;
2891 s->s3.tmp.pmslen = pmslen;
2895 OPENSSL_clear_free(pms, pmslen);
2896 EVP_PKEY_CTX_free(pctx);
2901 static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt)
2903 EVP_PKEY *ckey = NULL, *skey = NULL;
2904 unsigned char *keybytes = NULL;
2906 unsigned char *encoded_pub = NULL;
2907 size_t encoded_pub_len, pad_len;
2910 skey = s->s3.peer_tmp;
2912 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2916 ckey = ssl_generate_pkey(s, skey);
2918 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2922 if (ssl_derive(s, ckey, skey, 0) == 0) {
2923 /* SSLfatal() already called */
2927 /* send off the data */
2929 /* Generate encoding of server key */
2930 encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub);
2931 if (encoded_pub_len == 0) {
2932 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2933 EVP_PKEY_free(skey);
2934 return EXT_RETURN_FAIL;
2938 * For interoperability with some versions of the Microsoft TLS
2939 * stack, we need to zero pad the DHE pub key to the same length
2942 prime_len = EVP_PKEY_get_size(ckey);
2943 pad_len = prime_len - encoded_pub_len;
2945 if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) {
2946 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2949 memset(keybytes, 0, pad_len);
2952 if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) {
2953 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2959 OPENSSL_free(encoded_pub);
2960 EVP_PKEY_free(ckey);
2964 static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt)
2966 unsigned char *encodedPoint = NULL;
2967 size_t encoded_pt_len = 0;
2968 EVP_PKEY *ckey = NULL, *skey = NULL;
2971 skey = s->s3.peer_tmp;
2973 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2977 ckey = ssl_generate_pkey(s, skey);
2979 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2983 if (ssl_derive(s, ckey, skey, 0) == 0) {
2984 /* SSLfatal() already called */
2988 /* Generate encoding of client key */
2989 encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint);
2991 if (encoded_pt_len == 0) {
2992 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2996 if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
2997 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3003 OPENSSL_free(encodedPoint);
3004 EVP_PKEY_free(ckey);
3008 static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)
3010 #ifndef OPENSSL_NO_GOST
3011 /* GOST key exchange message creation */
3012 EVP_PKEY_CTX *pkey_ctx = NULL;
3015 unsigned int md_len;
3016 unsigned char shared_ukm[32], tmp[256];
3017 EVP_MD_CTX *ukm_hash = NULL;
3018 int dgst_nid = NID_id_GostR3411_94;
3019 unsigned char *pms = NULL;
3022 if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
3023 dgst_nid = NID_id_GostR3411_2012_256;
3026 * Get server certificate PKEY and create ctx from it
3028 peer_cert = s->session->peer;
3029 if (peer_cert == NULL) {
3030 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3031 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3035 pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx,
3036 X509_get0_pubkey(peer_cert),
3038 if (pkey_ctx == NULL) {
3039 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3043 * If we have send a certificate, and certificate key
3044 * parameters match those of server certificate, use
3045 * certificate key for key exchange
3048 /* Otherwise, generate ephemeral key pair */
3050 pms = OPENSSL_malloc(pmslen);
3052 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3056 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3057 /* Generate session key
3059 || RAND_bytes_ex(s->ctx->libctx, pms, pmslen, 0) <= 0) {
3060 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3064 * Compute shared IV and store it in algorithm-specific context
3067 ukm_hash = EVP_MD_CTX_new();
3068 if (ukm_hash == NULL
3069 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
3070 || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
3071 SSL3_RANDOM_SIZE) <= 0
3072 || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
3073 SSL3_RANDOM_SIZE) <= 0
3074 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3075 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3078 EVP_MD_CTX_free(ukm_hash);
3080 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3081 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
3082 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3085 /* Make GOST keytransport blob message */
3087 * Encapsulate it into sequence
3090 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
3091 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3095 if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
3096 || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
3097 || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
3098 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3102 EVP_PKEY_CTX_free(pkey_ctx);
3103 s->s3.tmp.pms = pms;
3104 s->s3.tmp.pmslen = pmslen;
3108 EVP_PKEY_CTX_free(pkey_ctx);
3109 OPENSSL_clear_free(pms, pmslen);
3110 EVP_MD_CTX_free(ukm_hash);
3113 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3118 #ifndef OPENSSL_NO_GOST
3119 int ossl_gost18_cke_cipher_nid(const SSL *s)
3121 if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0)
3122 return NID_magma_ctr;
3123 else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0)
3124 return NID_kuznyechik_ctr;
3129 int ossl_gost_ukm(const SSL *s, unsigned char *dgst_buf)
3131 EVP_MD_CTX * hash = NULL;
3132 unsigned int md_len;
3133 const EVP_MD *md = ssl_evp_md_fetch(s->ctx->libctx, NID_id_GostR3411_2012_256, s->ctx->propq);
3138 if ((hash = EVP_MD_CTX_new()) == NULL
3139 || EVP_DigestInit(hash, md) <= 0
3140 || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0
3141 || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0
3142 || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) {
3143 EVP_MD_CTX_free(hash);
3144 ssl_evp_md_free(md);
3148 EVP_MD_CTX_free(hash);
3149 ssl_evp_md_free(md);
3154 static int tls_construct_cke_gost18(SSL *s, WPACKET *pkt)
3156 #ifndef OPENSSL_NO_GOST
3157 /* GOST 2018 key exchange message creation */
3158 unsigned char rnd_dgst[32], tmp[255];
3159 EVP_PKEY_CTX *pkey_ctx = NULL;
3161 unsigned char *pms = NULL;
3164 int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3166 if (cipher_nid == NID_undef) {
3167 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3171 if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3172 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3176 /* Pre-master secret - random bytes */
3178 pms = OPENSSL_malloc(pmslen);
3180 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3184 if (RAND_bytes_ex(s->ctx->libctx, pms, pmslen, 0) <= 0) {
3185 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3189 /* Get server certificate PKEY and create ctx from it */
3190 peer_cert = s->session->peer;
3191 if (peer_cert == NULL) {
3192 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3193 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3197 pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx,
3198 X509_get0_pubkey(peer_cert),
3200 if (pkey_ctx == NULL) {
3201 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3205 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0 ) {
3206 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3210 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
3211 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3212 EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) < 0) {
3213 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3217 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3218 EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) < 0) {
3219 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3224 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
3225 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3229 if (!WPACKET_memcpy(pkt, tmp, msglen)) {
3230 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3234 EVP_PKEY_CTX_free(pkey_ctx);
3235 s->s3.tmp.pms = pms;
3236 s->s3.tmp.pmslen = pmslen;
3240 EVP_PKEY_CTX_free(pkey_ctx);
3241 OPENSSL_clear_free(pms, pmslen);
3244 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3249 static int tls_construct_cke_srp(SSL *s, WPACKET *pkt)
3251 #ifndef OPENSSL_NO_SRP
3252 unsigned char *abytes = NULL;
3254 if (s->srp_ctx.A == NULL
3255 || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
3257 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3260 BN_bn2bin(s->srp_ctx.A, abytes);
3262 OPENSSL_free(s->session->srp_username);
3263 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3264 if (s->session->srp_username == NULL) {
3265 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3271 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3276 int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
3278 unsigned long alg_k;
3280 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3283 * All of the construct functions below call SSLfatal() if necessary so
3284 * no need to do so here.
3286 if ((alg_k & SSL_PSK)
3287 && !tls_construct_cke_psk_preamble(s, pkt))
3290 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3291 if (!tls_construct_cke_rsa(s, pkt))
3293 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3294 if (!tls_construct_cke_dhe(s, pkt))
3296 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3297 if (!tls_construct_cke_ecdhe(s, pkt))
3299 } else if (alg_k & SSL_kGOST) {
3300 if (!tls_construct_cke_gost(s, pkt))
3302 } else if (alg_k & SSL_kGOST18) {
3303 if (!tls_construct_cke_gost18(s, pkt))
3305 } else if (alg_k & SSL_kSRP) {
3306 if (!tls_construct_cke_srp(s, pkt))
3308 } else if (!(alg_k & SSL_kPSK)) {
3309 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3315 OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
3316 s->s3.tmp.pms = NULL;
3317 s->s3.tmp.pmslen = 0;
3318 #ifndef OPENSSL_NO_PSK
3319 OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
3320 s->s3.tmp.psk = NULL;
3321 s->s3.tmp.psklen = 0;
3326 int tls_client_key_exchange_post_work(SSL *s)
3328 unsigned char *pms = NULL;
3331 pms = s->s3.tmp.pms;
3332 pmslen = s->s3.tmp.pmslen;
3334 #ifndef OPENSSL_NO_SRP
3336 if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3337 if (!srp_generate_client_master_secret(s)) {
3338 /* SSLfatal() already called */
3345 if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
3346 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3349 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
3350 /* SSLfatal() already called */
3351 /* ssl_generate_master_secret frees the pms even on error */
3359 #ifndef OPENSSL_NO_SCTP
3360 if (SSL_IS_DTLS(s)) {
3361 unsigned char sctpauthkey[64];
3362 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3366 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3369 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3370 sizeof(DTLS1_SCTP_AUTH_LABEL));
3372 /* Don't include the terminating zero. */
3373 labellen = sizeof(labelbuffer) - 1;
3374 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3377 if (SSL_export_keying_material(s, sctpauthkey,
3378 sizeof(sctpauthkey), labelbuffer,
3379 labellen, NULL, 0, 0) <= 0) {
3380 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3384 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3385 sizeof(sctpauthkey), sctpauthkey);
3391 OPENSSL_clear_free(pms, pmslen);
3392 s->s3.tmp.pms = NULL;
3393 s->s3.tmp.pmslen = 0;
3398 * Check a certificate can be used for client authentication. Currently check
3399 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3400 * certificates can be used and optionally checks suitability for Suite B.
3402 static int ssl3_check_client_certificate(SSL *s)
3404 /* If no suitable signature algorithm can't use certificate */
3405 if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
3408 * If strict mode check suitability of chain before using it. This also
3409 * adjusts suite B digest if necessary.
3411 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3412 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3417 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
3420 EVP_PKEY *pkey = NULL;
3423 if (wst == WORK_MORE_A) {
3424 /* Let cert callback update client certificates if required */
3425 if (s->cert->cert_cb) {
3426 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3428 s->rwstate = SSL_X509_LOOKUP;
3432 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
3435 s->rwstate = SSL_NOTHING;
3437 if (ssl3_check_client_certificate(s)) {
3438 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3439 return WORK_FINISHED_STOP;
3441 return WORK_FINISHED_CONTINUE;
3444 /* Fall through to WORK_MORE_B */
3448 /* We need to get a client cert */
3449 if (wst == WORK_MORE_B) {
3451 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3452 * return(-1); We then get retied later
3454 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3456 s->rwstate = SSL_X509_LOOKUP;
3459 s->rwstate = SSL_NOTHING;
3460 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3461 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3463 } else if (i == 1) {
3465 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3469 EVP_PKEY_free(pkey);
3470 if (i && !ssl3_check_client_certificate(s))
3473 if (s->version == SSL3_VERSION) {
3474 s->s3.tmp.cert_req = 0;
3475 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3476 return WORK_FINISHED_CONTINUE;
3478 s->s3.tmp.cert_req = 2;
3479 if (!ssl3_digest_cached_records(s, 0)) {
3480 /* SSLfatal() already called */
3486 if (s->post_handshake_auth == SSL_PHA_REQUESTED)
3487 return WORK_FINISHED_STOP;
3488 return WORK_FINISHED_CONTINUE;
3491 /* Shouldn't ever get here */
3492 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3496 int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
3498 if (SSL_IS_TLS13(s)) {
3499 if (s->pha_context == NULL) {
3500 /* no context available, add 0-length context */
3501 if (!WPACKET_put_bytes_u8(pkt, 0)) {
3502 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3505 } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
3506 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3510 if (!ssl3_output_cert_chain(s, pkt,
3511 (s->s3.tmp.cert_req == 2) ? NULL
3513 /* SSLfatal() already called */
3518 && SSL_IS_FIRST_HANDSHAKE(s)
3519 && (!s->method->ssl3_enc->change_cipher_state(s,
3520 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
3522 * This is a fatal error, which leaves enc_write_ctx in an inconsistent
3523 * state and thus ssl3_send_alert may crash.
3525 SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
3532 int ssl3_check_cert_and_algorithm(SSL *s)
3534 const SSL_CERT_LOOKUP *clu;
3538 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3539 alg_a = s->s3.tmp.new_cipher->algorithm_auth;
3541 /* we don't have a certificate */
3542 if (!(alg_a & SSL_aCERT))
3545 /* This is the passed certificate */
3546 clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx);
3548 /* Check certificate is recognised and suitable for cipher */
3549 if (clu == NULL || (alg_a & clu->amask) == 0) {
3550 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT);
3554 if (clu->amask & SSL_aECDSA) {
3555 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
3557 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT);
3561 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
3562 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3563 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3567 if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
3568 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3575 #ifndef OPENSSL_NO_NEXTPROTONEG
3576 int tls_construct_next_proto(SSL *s, WPACKET *pkt)
3578 size_t len, padding_len;
3579 unsigned char *padding = NULL;
3581 len = s->ext.npn_len;
3582 padding_len = 32 - ((len + 2) % 32);
3584 if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
3585 || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
3586 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3590 memset(padding, 0, padding_len);
3596 MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt)
3598 if (PACKET_remaining(pkt) > 0) {
3599 /* should contain no data */
3600 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3601 return MSG_PROCESS_ERROR;
3604 if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
3605 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
3606 return MSG_PROCESS_FINISHED_READING;
3610 * This is a historical discrepancy (not in the RFC) maintained for
3611 * compatibility reasons. If a TLS client receives a HelloRequest it will
3612 * attempt an abbreviated handshake. However if a DTLS client receives a
3613 * HelloRequest it will do a full handshake. Either behaviour is reasonable
3614 * but doing one for TLS and another for DTLS is odd.
3619 SSL_renegotiate_abbreviated(s);
3621 return MSG_PROCESS_FINISHED_READING;
3624 static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt)
3627 RAW_EXTENSION *rawexts = NULL;
3629 if (!PACKET_as_length_prefixed_2(pkt, &extensions)
3630 || PACKET_remaining(pkt) != 0) {
3631 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3635 if (!tls_collect_extensions(s, &extensions,
3636 SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
3638 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
3639 rawexts, NULL, 0, 1)) {
3640 /* SSLfatal() already called */
3644 OPENSSL_free(rawexts);
3645 return MSG_PROCESS_CONTINUE_READING;
3648 OPENSSL_free(rawexts);
3649 return MSG_PROCESS_ERROR;
3652 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3655 #ifndef OPENSSL_NO_ENGINE
3656 if (s->ctx->client_cert_engine) {
3657 i = tls_engine_load_ssl_client_cert(s, px509, ppkey);
3662 if (s->ctx->client_cert_cb)
3663 i = s->ctx->client_cert_cb(s, px509, ppkey);
3667 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
3670 size_t totlen = 0, len, maxlen, maxverok = 0;
3671 int empty_reneg_info_scsv = !s->renegotiate;
3673 /* Set disabled masks for this session */
3674 if (!ssl_set_client_disabled(s)) {
3675 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE);
3680 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3684 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
3685 # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
3686 # error Max cipher length too short
3689 * Some servers hang if client hello > 256 bytes as hack workaround
3690 * chop number of supported ciphers to keep it well below this if we
3693 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3694 maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
3697 /* Maximum length that can be stored in 2 bytes. Length must be even */
3700 if (empty_reneg_info_scsv)
3702 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
3705 for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
3706 const SSL_CIPHER *c;
3708 c = sk_SSL_CIPHER_value(sk, i);
3709 /* Skip disabled ciphers */
3710 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
3713 if (!s->method->put_cipher_by_char(c, pkt, &len)) {
3714 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3718 /* Sanity check that the maximum version we offer has ciphers enabled */
3720 if (SSL_IS_DTLS(s)) {
3721 if (DTLS_VERSION_GE(c->max_dtls, s->s3.tmp.max_ver)
3722 && DTLS_VERSION_LE(c->min_dtls, s->s3.tmp.max_ver))
3725 if (c->max_tls >= s->s3.tmp.max_ver
3726 && c->min_tls <= s->s3.tmp.max_ver)
3734 if (totlen == 0 || !maxverok) {
3735 const char *maxvertext =
3737 ? "No ciphers enabled for max supported SSL/TLS version"
3740 SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE,
3746 if (empty_reneg_info_scsv) {
3747 static SSL_CIPHER scsv = {
3748 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3750 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3751 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3755 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3756 static SSL_CIPHER scsv = {
3757 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3759 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3760 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3769 int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt)
3771 if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
3772 && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
3773 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3777 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;