2 * Copyright 1995-2022 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_CONNECTION *s,
33 static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
36 static ossl_inline int cert_req_allowed(SSL_CONNECTION *s);
37 static int key_exchange_expected(SSL_CONNECTION *s);
38 static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
42 * Is a CertificateRequest message allowed at the moment or not?
48 static ossl_inline int cert_req_allowed(SSL_CONNECTION *s)
50 /* TLS does not like anon-DH with client cert */
51 if ((s->version > SSL3_VERSION
52 && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL))
53 || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
60 * Should we expect the ServerKeyExchange message or not?
66 static int key_exchange_expected(SSL_CONNECTION *s)
68 long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
71 * Can't skip server key exchange if this is an ephemeral
72 * ciphersuite or for SRP
74 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
83 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
84 * handshake state transitions when a TLS1.3 client is reading messages from the
85 * server. The message type that the server has sent is provided in |mt|. The
86 * current state is in |s->statem.hand_state|.
88 * Return values are 1 for success (transition allowed) and 0 on error
89 * (transition not allowed)
91 static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt)
93 OSSL_STATEM *st = &s->statem;
96 * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
97 * yet negotiated TLSv1.3 at that point so that is handled by
98 * ossl_statem_client_read_transition()
101 switch (st->hand_state) {
105 case TLS_ST_CW_CLNT_HELLO:
107 * This must a ClientHello following a HelloRetryRequest, so the only
108 * thing we can get now is a ServerHello.
110 if (mt == SSL3_MT_SERVER_HELLO) {
111 st->hand_state = TLS_ST_CR_SRVR_HELLO;
116 case TLS_ST_CR_SRVR_HELLO:
117 if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
118 st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
123 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
125 if (mt == SSL3_MT_FINISHED) {
126 st->hand_state = TLS_ST_CR_FINISHED;
130 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
131 st->hand_state = TLS_ST_CR_CERT_REQ;
134 if (mt == SSL3_MT_CERTIFICATE) {
135 st->hand_state = TLS_ST_CR_CERT;
141 case TLS_ST_CR_CERT_REQ:
142 if (mt == SSL3_MT_CERTIFICATE) {
143 st->hand_state = TLS_ST_CR_CERT;
149 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
150 st->hand_state = TLS_ST_CR_CERT_VRFY;
155 case TLS_ST_CR_CERT_VRFY:
156 if (mt == SSL3_MT_FINISHED) {
157 st->hand_state = TLS_ST_CR_FINISHED;
163 if (mt == SSL3_MT_NEWSESSION_TICKET) {
164 st->hand_state = TLS_ST_CR_SESSION_TICKET;
167 if (mt == SSL3_MT_KEY_UPDATE) {
168 st->hand_state = TLS_ST_CR_KEY_UPDATE;
171 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
172 #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
173 /* Restore digest for PHA before adding message.*/
174 # error Internal DTLS version error
176 if (!SSL_CONNECTION_IS_DTLS(s)
177 && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
178 s->post_handshake_auth = SSL_PHA_REQUESTED;
180 * In TLS, this is called before the message is added to the
181 * digest. In DTLS, this is expected to be called after adding
182 * to the digest. Either move the digest restore, or add the
183 * message here after the swap, or do it after the clientFinished?
185 if (!tls13_restore_handshake_digest_for_pha(s)) {
186 /* SSLfatal() already called */
189 st->hand_state = TLS_ST_CR_CERT_REQ;
196 /* No valid transition found */
201 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
202 * handshake state transitions when the client is reading messages from the
203 * server. The message type that the server has sent is provided in |mt|. The
204 * current state is in |s->statem.hand_state|.
206 * Return values are 1 for success (transition allowed) and 0 on error
207 * (transition not allowed)
209 int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt)
211 OSSL_STATEM *st = &s->statem;
215 * Note that after writing the first ClientHello we don't know what version
216 * we are going to negotiate yet, so we don't take this branch until later.
218 if (SSL_CONNECTION_IS_TLS13(s)) {
219 if (!ossl_statem_client13_read_transition(s, mt))
224 switch (st->hand_state) {
228 case TLS_ST_CW_CLNT_HELLO:
229 if (mt == SSL3_MT_SERVER_HELLO) {
230 st->hand_state = TLS_ST_CR_SRVR_HELLO;
234 if (SSL_CONNECTION_IS_DTLS(s)) {
235 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
236 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
242 case TLS_ST_EARLY_DATA:
244 * We've not actually selected TLSv1.3 yet, but we have sent early
245 * data. The only thing allowed now is a ServerHello or a
248 if (mt == SSL3_MT_SERVER_HELLO) {
249 st->hand_state = TLS_ST_CR_SRVR_HELLO;
254 case TLS_ST_CR_SRVR_HELLO:
256 if (s->ext.ticket_expected) {
257 if (mt == SSL3_MT_NEWSESSION_TICKET) {
258 st->hand_state = TLS_ST_CR_SESSION_TICKET;
261 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
262 st->hand_state = TLS_ST_CR_CHANGE;
266 if (SSL_CONNECTION_IS_DTLS(s)
267 && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
268 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
270 } else if (s->version >= TLS1_VERSION
271 && s->ext.session_secret_cb != NULL
272 && s->session->ext.tick != NULL
273 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
275 * Normally, we can tell if the server is resuming the session
276 * from the session ID. EAP-FAST (RFC 4851), however, relies on
277 * the next server message after the ServerHello to determine if
278 * the server is resuming.
281 st->hand_state = TLS_ST_CR_CHANGE;
283 } else if (!(s->s3.tmp.new_cipher->algorithm_auth
284 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
285 if (mt == SSL3_MT_CERTIFICATE) {
286 st->hand_state = TLS_ST_CR_CERT;
290 ske_expected = key_exchange_expected(s);
291 /* SKE is optional for some PSK ciphersuites */
293 || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
294 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
295 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
296 st->hand_state = TLS_ST_CR_KEY_EXCH;
299 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
300 && cert_req_allowed(s)) {
301 st->hand_state = TLS_ST_CR_CERT_REQ;
303 } else if (mt == SSL3_MT_SERVER_DONE) {
304 st->hand_state = TLS_ST_CR_SRVR_DONE;
313 * The CertificateStatus message is optional even if
314 * |ext.status_expected| is set
316 if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
317 st->hand_state = TLS_ST_CR_CERT_STATUS;
322 case TLS_ST_CR_CERT_STATUS:
323 ske_expected = key_exchange_expected(s);
324 /* SKE is optional for some PSK ciphersuites */
325 if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
326 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
327 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
328 st->hand_state = TLS_ST_CR_KEY_EXCH;
335 case TLS_ST_CR_KEY_EXCH:
336 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
337 if (cert_req_allowed(s)) {
338 st->hand_state = TLS_ST_CR_CERT_REQ;
345 case TLS_ST_CR_CERT_REQ:
346 if (mt == SSL3_MT_SERVER_DONE) {
347 st->hand_state = TLS_ST_CR_SRVR_DONE;
352 case TLS_ST_CW_FINISHED:
353 if (s->ext.ticket_expected) {
354 if (mt == SSL3_MT_NEWSESSION_TICKET) {
355 st->hand_state = TLS_ST_CR_SESSION_TICKET;
358 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
359 st->hand_state = TLS_ST_CR_CHANGE;
364 case TLS_ST_CR_SESSION_TICKET:
365 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
366 st->hand_state = TLS_ST_CR_CHANGE;
371 case TLS_ST_CR_CHANGE:
372 if (mt == SSL3_MT_FINISHED) {
373 st->hand_state = TLS_ST_CR_FINISHED;
379 if (mt == SSL3_MT_HELLO_REQUEST) {
380 st->hand_state = TLS_ST_CR_HELLO_REQ;
387 /* No valid transition found */
388 if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
392 * CCS messages don't have a message sequence number so this is probably
393 * because of an out-of-order CCS. We'll just drop it.
396 s->rwstate = SSL_READING;
397 rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
398 BIO_clear_retry_flags(rbio);
399 BIO_set_retry_read(rbio);
402 SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
407 * ossl_statem_client13_write_transition() works out what handshake state to
408 * move to next when the TLSv1.3 client is writing messages to be sent to the
411 static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s)
413 OSSL_STATEM *st = &s->statem;
416 * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
417 * TLSv1.3 yet at that point. They are handled by
418 * ossl_statem_client_write_transition().
420 switch (st->hand_state) {
422 /* Shouldn't happen */
423 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
424 return WRITE_TRAN_ERROR;
426 case TLS_ST_CR_CERT_REQ:
427 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
428 st->hand_state = TLS_ST_CW_CERT;
429 return WRITE_TRAN_CONTINUE;
432 * We should only get here if we received a CertificateRequest after
433 * we already sent close_notify
435 if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
436 /* Shouldn't happen - same as default case */
437 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
438 return WRITE_TRAN_ERROR;
440 st->hand_state = TLS_ST_OK;
441 return WRITE_TRAN_CONTINUE;
443 case TLS_ST_CR_FINISHED:
444 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
445 || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
446 st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
447 else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
448 && s->hello_retry_request == SSL_HRR_NONE)
449 st->hand_state = TLS_ST_CW_CHANGE;
451 st->hand_state = (s->s3.tmp.cert_req != 0) ? TLS_ST_CW_CERT
452 : TLS_ST_CW_FINISHED;
453 return WRITE_TRAN_CONTINUE;
455 case TLS_ST_PENDING_EARLY_DATA_END:
456 if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
457 st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
458 return WRITE_TRAN_CONTINUE;
462 case TLS_ST_CW_END_OF_EARLY_DATA:
463 case TLS_ST_CW_CHANGE:
464 st->hand_state = (s->s3.tmp.cert_req != 0) ? TLS_ST_CW_CERT
465 : TLS_ST_CW_FINISHED;
466 return WRITE_TRAN_CONTINUE;
469 /* If a non-empty Certificate we also send CertificateVerify */
470 st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
471 : TLS_ST_CW_FINISHED;
472 return WRITE_TRAN_CONTINUE;
474 case TLS_ST_CW_CERT_VRFY:
475 st->hand_state = TLS_ST_CW_FINISHED;
476 return WRITE_TRAN_CONTINUE;
478 case TLS_ST_CR_KEY_UPDATE:
479 case TLS_ST_CW_KEY_UPDATE:
480 case TLS_ST_CR_SESSION_TICKET:
481 case TLS_ST_CW_FINISHED:
482 st->hand_state = TLS_ST_OK;
483 return WRITE_TRAN_CONTINUE;
486 if (s->key_update != SSL_KEY_UPDATE_NONE) {
487 st->hand_state = TLS_ST_CW_KEY_UPDATE;
488 return WRITE_TRAN_CONTINUE;
491 /* Try to read from the server instead */
492 return WRITE_TRAN_FINISHED;
497 * ossl_statem_client_write_transition() works out what handshake state to
498 * move to next when the client is writing messages to be sent to the server.
500 WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s)
502 OSSL_STATEM *st = &s->statem;
505 * Note that immediately before/after a ClientHello we don't know what
506 * version we are going to negotiate yet, so we don't take this branch until
509 if (SSL_CONNECTION_IS_TLS13(s))
510 return ossl_statem_client13_write_transition(s);
512 switch (st->hand_state) {
514 /* Shouldn't happen */
515 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
516 return WRITE_TRAN_ERROR;
519 if (!s->renegotiate) {
521 * We haven't requested a renegotiation ourselves so we must have
522 * received a message from the server. Better read it.
524 return WRITE_TRAN_FINISHED;
529 st->hand_state = TLS_ST_CW_CLNT_HELLO;
530 return WRITE_TRAN_CONTINUE;
532 case TLS_ST_CW_CLNT_HELLO:
533 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
535 * We are assuming this is a TLSv1.3 connection, although we haven't
536 * actually selected a version yet.
538 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
539 st->hand_state = TLS_ST_CW_CHANGE;
541 st->hand_state = TLS_ST_EARLY_DATA;
542 return WRITE_TRAN_CONTINUE;
545 * No transition at the end of writing because we don't know what
548 return WRITE_TRAN_FINISHED;
550 case TLS_ST_CR_SRVR_HELLO:
552 * We only get here in TLSv1.3. We just received an HRR, so issue a
553 * CCS unless middlebox compat mode is off, or we already issued one
554 * because we did early data.
556 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
557 && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
558 st->hand_state = TLS_ST_CW_CHANGE;
560 st->hand_state = TLS_ST_CW_CLNT_HELLO;
561 return WRITE_TRAN_CONTINUE;
563 case TLS_ST_EARLY_DATA:
564 return WRITE_TRAN_FINISHED;
566 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
567 st->hand_state = TLS_ST_CW_CLNT_HELLO;
568 return WRITE_TRAN_CONTINUE;
570 case TLS_ST_CR_SRVR_DONE:
571 if (s->s3.tmp.cert_req)
572 st->hand_state = TLS_ST_CW_CERT;
574 st->hand_state = TLS_ST_CW_KEY_EXCH;
575 return WRITE_TRAN_CONTINUE;
578 st->hand_state = TLS_ST_CW_KEY_EXCH;
579 return WRITE_TRAN_CONTINUE;
581 case TLS_ST_CW_KEY_EXCH:
583 * For TLS, cert_req is set to 2, so a cert chain of nothing is
584 * sent, but no verify packet is sent
587 * XXX: For now, we do not support client authentication in ECDH
588 * cipher suites with ECDH (rather than ECDSA) certificates. We
589 * need to skip the certificate verify message when client's
590 * ECDH public key is sent inside the client certificate.
592 if (s->s3.tmp.cert_req == 1) {
593 st->hand_state = TLS_ST_CW_CERT_VRFY;
595 st->hand_state = TLS_ST_CW_CHANGE;
597 if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
598 st->hand_state = TLS_ST_CW_CHANGE;
600 return WRITE_TRAN_CONTINUE;
602 case TLS_ST_CW_CERT_VRFY:
603 st->hand_state = TLS_ST_CW_CHANGE;
604 return WRITE_TRAN_CONTINUE;
606 case TLS_ST_CW_CHANGE:
607 if (s->hello_retry_request == SSL_HRR_PENDING) {
608 st->hand_state = TLS_ST_CW_CLNT_HELLO;
609 } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
610 st->hand_state = TLS_ST_EARLY_DATA;
612 #if defined(OPENSSL_NO_NEXTPROTONEG)
613 st->hand_state = TLS_ST_CW_FINISHED;
615 if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen)
616 st->hand_state = TLS_ST_CW_NEXT_PROTO;
618 st->hand_state = TLS_ST_CW_FINISHED;
621 return WRITE_TRAN_CONTINUE;
623 #if !defined(OPENSSL_NO_NEXTPROTONEG)
624 case TLS_ST_CW_NEXT_PROTO:
625 st->hand_state = TLS_ST_CW_FINISHED;
626 return WRITE_TRAN_CONTINUE;
629 case TLS_ST_CW_FINISHED:
631 st->hand_state = TLS_ST_OK;
632 return WRITE_TRAN_CONTINUE;
634 return WRITE_TRAN_FINISHED;
637 case TLS_ST_CR_FINISHED:
639 st->hand_state = TLS_ST_CW_CHANGE;
640 return WRITE_TRAN_CONTINUE;
642 st->hand_state = TLS_ST_OK;
643 return WRITE_TRAN_CONTINUE;
646 case TLS_ST_CR_HELLO_REQ:
648 * If we can renegotiate now then do so, otherwise wait for a more
651 if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) {
652 if (!tls_setup_handshake(s)) {
653 /* SSLfatal() already called */
654 return WRITE_TRAN_ERROR;
656 st->hand_state = TLS_ST_CW_CLNT_HELLO;
657 return WRITE_TRAN_CONTINUE;
659 st->hand_state = TLS_ST_OK;
660 return WRITE_TRAN_CONTINUE;
665 * Perform any pre work that needs to be done prior to sending a message from
666 * the client to the server.
668 WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
670 OSSL_STATEM *st = &s->statem;
672 switch (st->hand_state) {
674 /* No pre work to be done */
677 case TLS_ST_CW_CLNT_HELLO:
679 if (SSL_CONNECTION_IS_DTLS(s)) {
680 /* every DTLS ClientHello resets Finished MAC */
681 if (!ssl3_init_finished_mac(s)) {
682 /* SSLfatal() already called */
688 case TLS_ST_CW_CHANGE:
689 if (SSL_CONNECTION_IS_DTLS(s)) {
692 * We're into the last flight so we don't retransmit these
693 * messages unless we need to.
697 #ifndef OPENSSL_NO_SCTP
698 if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) {
699 /* Calls SSLfatal() as required */
700 return dtls_wait_for_dry(s);
706 case TLS_ST_PENDING_EARLY_DATA_END:
708 * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
709 * attempt to write early data before calling SSL_read() then we press
710 * on with the handshake. Otherwise we pause here.
712 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
713 || s->early_data_state == SSL_EARLY_DATA_NONE)
714 return WORK_FINISHED_CONTINUE;
717 case TLS_ST_EARLY_DATA:
718 return tls_finish_handshake(s, wst, 0, 1);
721 /* Calls SSLfatal() as required */
722 return tls_finish_handshake(s, wst, 1, 1);
725 return WORK_FINISHED_CONTINUE;
729 * Perform any work that needs to be done after sending a message from the
730 * client to the server.
732 WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst)
734 OSSL_STATEM *st = &s->statem;
735 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
739 switch (st->hand_state) {
741 /* No post work to be done */
744 case TLS_ST_CW_CLNT_HELLO:
745 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
746 && s->max_early_data > 0) {
748 * We haven't selected TLSv1.3 yet so we don't call the change
749 * cipher state function associated with the SSL_METHOD. Instead
750 * we call tls13_change_cipher_state() directly.
752 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
753 if (!tls13_change_cipher_state(s,
754 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
755 /* SSLfatal() already called */
759 /* else we're in compat mode so we delay flushing until after CCS */
760 } else if (!statem_flush(s)) {
764 if (SSL_CONNECTION_IS_DTLS(s)) {
765 /* Treat the next message as the first packet */
770 case TLS_ST_CW_END_OF_EARLY_DATA:
772 * We set the enc_write_ctx back to NULL because we may end up writing
773 * in cleartext again if we get a HelloRetryRequest from the server.
775 EVP_CIPHER_CTX_free(s->enc_write_ctx);
776 s->enc_write_ctx = NULL;
779 case TLS_ST_CW_KEY_EXCH:
780 if (tls_client_key_exchange_post_work(s) == 0) {
781 /* SSLfatal() already called */
786 case TLS_ST_CW_CHANGE:
787 if (SSL_CONNECTION_IS_TLS13(s)
788 || s->hello_retry_request == SSL_HRR_PENDING)
790 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
791 && s->max_early_data > 0) {
793 * We haven't selected TLSv1.3 yet so we don't call the change
794 * cipher state function associated with the SSL_METHOD. Instead
795 * we call tls13_change_cipher_state() directly.
797 if (!tls13_change_cipher_state(s,
798 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
802 s->session->cipher = s->s3.tmp.new_cipher;
803 #ifdef OPENSSL_NO_COMP
804 s->session->compress_meth = 0;
806 if (s->s3.tmp.new_compression == NULL)
807 s->session->compress_meth = 0;
809 s->session->compress_meth = s->s3.tmp.new_compression->id;
811 if (!ssl->method->ssl3_enc->setup_key_block(s)) {
812 /* SSLfatal() already called */
816 if (!ssl->method->ssl3_enc->change_cipher_state(s,
817 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
818 /* SSLfatal() already called */
822 if (SSL_CONNECTION_IS_DTLS(s)) {
823 #ifndef OPENSSL_NO_SCTP
826 * Change to new shared key of SCTP-Auth, will be ignored if
829 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
834 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
838 case TLS_ST_CW_FINISHED:
839 #ifndef OPENSSL_NO_SCTP
840 if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) {
842 * Change to new shared key of SCTP-Auth, will be ignored if
845 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
849 if (statem_flush(s) != 1)
852 if (SSL_CONNECTION_IS_TLS13(s)) {
853 if (!tls13_save_handshake_digest_for_pha(s)) {
854 /* SSLfatal() already called */
857 if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
858 if (!ssl->method->ssl3_enc->change_cipher_state(s,
859 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
860 /* SSLfatal() already called */
867 case TLS_ST_CW_KEY_UPDATE:
868 if (statem_flush(s) != 1)
870 if (!tls13_update_key(s, 1)) {
871 /* SSLfatal() already called */
877 return WORK_FINISHED_CONTINUE;
881 * Get the message construction function and message type for sending from the
884 * Valid return values are:
888 int ossl_statem_client_construct_message(SSL_CONNECTION *s,
889 confunc_f *confunc, int *mt)
891 OSSL_STATEM *st = &s->statem;
893 switch (st->hand_state) {
895 /* Shouldn't happen */
896 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
899 case TLS_ST_CW_CHANGE:
900 if (SSL_CONNECTION_IS_DTLS(s))
901 *confunc = dtls_construct_change_cipher_spec;
903 *confunc = tls_construct_change_cipher_spec;
904 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
907 case TLS_ST_CW_CLNT_HELLO:
908 *confunc = tls_construct_client_hello;
909 *mt = SSL3_MT_CLIENT_HELLO;
912 case TLS_ST_CW_END_OF_EARLY_DATA:
913 *confunc = tls_construct_end_of_early_data;
914 *mt = SSL3_MT_END_OF_EARLY_DATA;
917 case TLS_ST_PENDING_EARLY_DATA_END:
923 *confunc = tls_construct_client_certificate;
924 *mt = SSL3_MT_CERTIFICATE;
927 case TLS_ST_CW_KEY_EXCH:
928 *confunc = tls_construct_client_key_exchange;
929 *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
932 case TLS_ST_CW_CERT_VRFY:
933 *confunc = tls_construct_cert_verify;
934 *mt = SSL3_MT_CERTIFICATE_VERIFY;
937 #if !defined(OPENSSL_NO_NEXTPROTONEG)
938 case TLS_ST_CW_NEXT_PROTO:
939 *confunc = tls_construct_next_proto;
940 *mt = SSL3_MT_NEXT_PROTO;
943 case TLS_ST_CW_FINISHED:
944 *confunc = tls_construct_finished;
945 *mt = SSL3_MT_FINISHED;
948 case TLS_ST_CW_KEY_UPDATE:
949 *confunc = tls_construct_key_update;
950 *mt = SSL3_MT_KEY_UPDATE;
958 * Returns the maximum allowed length for the current message that we are
959 * reading. Excludes the message header.
961 size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s)
963 OSSL_STATEM *st = &s->statem;
965 switch (st->hand_state) {
967 /* Shouldn't happen */
970 case TLS_ST_CR_SRVR_HELLO:
971 return SERVER_HELLO_MAX_LENGTH;
973 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
974 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
977 return s->max_cert_list;
979 case TLS_ST_CR_CERT_VRFY:
980 return SSL3_RT_MAX_PLAIN_LENGTH;
982 case TLS_ST_CR_CERT_STATUS:
983 return SSL3_RT_MAX_PLAIN_LENGTH;
985 case TLS_ST_CR_KEY_EXCH:
986 return SERVER_KEY_EXCH_MAX_LENGTH;
988 case TLS_ST_CR_CERT_REQ:
990 * Set to s->max_cert_list for compatibility with previous releases. In
991 * practice these messages can get quite long if servers are configured
992 * to provide a long list of acceptable CAs
994 return s->max_cert_list;
996 case TLS_ST_CR_SRVR_DONE:
997 return SERVER_HELLO_DONE_MAX_LENGTH;
999 case TLS_ST_CR_CHANGE:
1000 if (s->version == DTLS1_BAD_VER)
1002 return CCS_MAX_LENGTH;
1004 case TLS_ST_CR_SESSION_TICKET:
1005 return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
1006 : SESSION_TICKET_MAX_LENGTH_TLS12;
1008 case TLS_ST_CR_FINISHED:
1009 return FINISHED_MAX_LENGTH;
1011 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1012 return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
1014 case TLS_ST_CR_KEY_UPDATE:
1015 return KEY_UPDATE_MAX_LENGTH;
1020 * Process a message that the client has received from the server.
1022 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s,
1025 OSSL_STATEM *st = &s->statem;
1027 switch (st->hand_state) {
1029 /* Shouldn't happen */
1030 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1031 return MSG_PROCESS_ERROR;
1033 case TLS_ST_CR_SRVR_HELLO:
1034 return tls_process_server_hello(s, pkt);
1036 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1037 return dtls_process_hello_verify(s, pkt);
1039 case TLS_ST_CR_CERT:
1040 return tls_process_server_certificate(s, pkt);
1042 case TLS_ST_CR_CERT_VRFY:
1043 return tls_process_cert_verify(s, pkt);
1045 case TLS_ST_CR_CERT_STATUS:
1046 return tls_process_cert_status(s, pkt);
1048 case TLS_ST_CR_KEY_EXCH:
1049 return tls_process_key_exchange(s, pkt);
1051 case TLS_ST_CR_CERT_REQ:
1052 return tls_process_certificate_request(s, pkt);
1054 case TLS_ST_CR_SRVR_DONE:
1055 return tls_process_server_done(s, pkt);
1057 case TLS_ST_CR_CHANGE:
1058 return tls_process_change_cipher_spec(s, pkt);
1060 case TLS_ST_CR_SESSION_TICKET:
1061 return tls_process_new_session_ticket(s, pkt);
1063 case TLS_ST_CR_FINISHED:
1064 return tls_process_finished(s, pkt);
1066 case TLS_ST_CR_HELLO_REQ:
1067 return tls_process_hello_req(s, pkt);
1069 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1070 return tls_process_encrypted_extensions(s, pkt);
1072 case TLS_ST_CR_KEY_UPDATE:
1073 return tls_process_key_update(s, pkt);
1078 * Perform any further processing required following the receipt of a message
1081 WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s,
1084 OSSL_STATEM *st = &s->statem;
1086 switch (st->hand_state) {
1088 /* Shouldn't happen */
1089 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1092 case TLS_ST_CR_CERT:
1093 return tls_post_process_server_certificate(s, wst);
1095 case TLS_ST_CR_CERT_VRFY:
1096 case TLS_ST_CR_CERT_REQ:
1097 return tls_prepare_client_certificate(s, wst);
1101 int tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt)
1106 #ifndef OPENSSL_NO_COMP
1109 SSL_SESSION *sess = s->session;
1110 unsigned char *session_id;
1111 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1113 /* Work out what SSL/TLS/DTLS version to use */
1114 protverr = ssl_set_client_hello_version(s);
1115 if (protverr != 0) {
1116 SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr);
1121 || !ssl_version_supported(s, sess->ssl_version, NULL)
1122 || !SSL_SESSION_is_resumable(sess)) {
1123 if (s->hello_retry_request == SSL_HRR_NONE
1124 && !ssl_get_new_session(s, 0)) {
1125 /* SSLfatal() already called */
1129 /* else use the pre-loaded session */
1131 p = s->s3.client_random;
1134 * for DTLS if client_random is initialized, reuse it, we are
1135 * required to use same upon reply to HelloVerify
1137 if (SSL_CONNECTION_IS_DTLS(s)) {
1140 for (idx = 0; idx < sizeof(s->s3.client_random); idx++) {
1147 i = (s->hello_retry_request == SSL_HRR_NONE);
1150 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random),
1151 DOWNGRADE_NONE) <= 0) {
1152 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1157 * version indicates the negotiated version: for example from
1158 * an SSLv2/v3 compatible client hello). The client_version
1159 * field is the maximum version we permit and it is also
1160 * used in RSA encrypted premaster secrets. Some servers can
1161 * choke if we initially report a higher version then
1162 * renegotiate to a lower one in the premaster secret. This
1163 * didn't happen with TLS 1.0 as most servers supported it
1164 * but it can with TLS 1.1 or later if the server only supports
1167 * Possible scenario with previous logic:
1168 * 1. Client hello indicates TLS 1.2
1169 * 2. Server hello says TLS 1.0
1170 * 3. RSA encrypted premaster secret uses 1.2.
1171 * 4. Handshake proceeds using TLS 1.0.
1172 * 5. Server sends hello request to renegotiate.
1173 * 6. Client hello indicates TLS v1.0 as we now
1174 * know that is maximum server supports.
1175 * 7. Server chokes on RSA encrypted premaster secret
1176 * containing version 1.0.
1178 * For interoperability it should be OK to always use the
1179 * maximum version we support in client hello and then rely
1180 * on the checking of version to ensure the servers isn't
1181 * being inconsistent: for example initially negotiating with
1182 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
1183 * client_version in client hello and not resetting it to
1184 * the negotiated version.
1186 * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
1187 * supported_versions extension for the real supported versions.
1189 if (!WPACKET_put_bytes_u16(pkt, s->client_version)
1190 || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) {
1191 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1196 session_id = s->session->session_id;
1197 if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
1198 if (s->version == TLS1_3_VERSION
1199 && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
1200 sess_id_len = sizeof(s->tmp_session_id);
1201 s->tmp_session_id_len = sess_id_len;
1202 session_id = s->tmp_session_id;
1203 if (s->hello_retry_request == SSL_HRR_NONE
1204 && RAND_bytes_ex(sctx->libctx, s->tmp_session_id,
1205 sess_id_len, 0) <= 0) {
1206 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1213 assert(s->session->session_id_length <= sizeof(s->session->session_id));
1214 sess_id_len = s->session->session_id_length;
1215 if (s->version == TLS1_3_VERSION) {
1216 s->tmp_session_id_len = sess_id_len;
1217 memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
1220 if (!WPACKET_start_sub_packet_u8(pkt)
1221 || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id,
1223 || !WPACKET_close(pkt)) {
1224 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1228 /* cookie stuff for DTLS */
1229 if (SSL_CONNECTION_IS_DTLS(s)) {
1230 if (s->d1->cookie_len > sizeof(s->d1->cookie)
1231 || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
1232 s->d1->cookie_len)) {
1233 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1238 /* Ciphers supported */
1239 if (!WPACKET_start_sub_packet_u16(pkt)) {
1240 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1244 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)),
1246 /* SSLfatal() already called */
1249 if (!WPACKET_close(pkt)) {
1250 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1255 if (!WPACKET_start_sub_packet_u8(pkt)) {
1256 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1259 #ifndef OPENSSL_NO_COMP
1260 if (ssl_allow_compression(s)
1261 && sctx->comp_methods
1262 && (SSL_CONNECTION_IS_DTLS(s)
1263 || s->s3.tmp.max_ver < TLS1_3_VERSION)) {
1264 int compnum = sk_SSL_COMP_num(sctx->comp_methods);
1265 for (i = 0; i < compnum; i++) {
1266 comp = sk_SSL_COMP_value(sctx->comp_methods, i);
1267 if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
1268 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1274 /* Add the NULL method */
1275 if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
1276 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1280 /* TLS extensions */
1281 if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
1282 /* SSLfatal() already called */
1289 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt)
1294 if (!PACKET_forward(pkt, 2)
1295 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1296 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1297 return MSG_PROCESS_ERROR;
1300 cookie_len = PACKET_remaining(&cookiepkt);
1301 if (cookie_len > sizeof(s->d1->cookie)) {
1302 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG);
1303 return MSG_PROCESS_ERROR;
1306 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1307 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1308 return MSG_PROCESS_ERROR;
1310 s->d1->cookie_len = cookie_len;
1312 return MSG_PROCESS_FINISHED_READING;
1315 static int set_client_ciphersuite(SSL_CONNECTION *s,
1316 const unsigned char *cipherchars)
1318 STACK_OF(SSL_CIPHER) *sk;
1319 const SSL_CIPHER *c;
1321 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1323 c = ssl_get_cipher_by_char(s, cipherchars, 0);
1325 /* unknown cipher */
1326 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED);
1330 * If it is a disabled cipher we either didn't send it in client hello,
1331 * or it's not allowed for the selected protocol. So we return an error.
1333 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
1334 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1338 sk = ssl_get_ciphers_by_id(s);
1339 i = sk_SSL_CIPHER_find(sk, c);
1341 /* we did not say we would use this cipher */
1342 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1346 if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL
1347 && s->s3.tmp.new_cipher->id != c->id) {
1348 /* ServerHello selected a different ciphersuite to that in the HRR */
1349 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1354 * Depending on the session caching (internal/external), the cipher
1355 * and/or cipher_id values may not be set. Make sure that cipher_id is
1356 * set and use it for comparison.
1358 if (s->session->cipher != NULL)
1359 s->session->cipher_id = s->session->cipher->id;
1360 if (s->hit && (s->session->cipher_id != c->id)) {
1361 if (SSL_CONNECTION_IS_TLS13(s)) {
1362 const EVP_MD *md = ssl_md(sctx, c->algorithm2);
1365 * In TLSv1.3 it is valid for the server to select a different
1366 * ciphersuite as long as the hash is the same.
1369 || md != ssl_md(sctx, s->session->cipher->algorithm2)) {
1370 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1371 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
1376 * Prior to TLSv1.3 resuming a session always meant using the same
1379 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1380 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1384 s->s3.tmp.new_cipher = c;
1389 MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt)
1391 PACKET session_id, extpkt;
1392 size_t session_id_len;
1393 const unsigned char *cipherchars;
1395 unsigned int compression;
1396 unsigned int sversion;
1397 unsigned int context;
1398 RAW_EXTENSION *extensions = NULL;
1399 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1400 #ifndef OPENSSL_NO_COMP
1404 if (!PACKET_get_net_2(pkt, &sversion)) {
1405 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1409 /* load the server random */
1410 if (s->version == TLS1_3_VERSION
1411 && sversion == TLS1_2_VERSION
1412 && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
1413 && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
1414 if (s->hello_retry_request != SSL_HRR_NONE) {
1415 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1418 s->hello_retry_request = SSL_HRR_PENDING;
1419 /* Tell the record layer that we know we're going to get TLSv1.3 */
1420 s->rlayer.rrlmethod->set_protocol_version(s->rlayer.rrl, s->version);
1422 if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
1423 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1427 if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) {
1428 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1433 /* Get the session-id. */
1434 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1435 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1438 session_id_len = PACKET_remaining(&session_id);
1439 if (session_id_len > sizeof(s->session->session_id)
1440 || session_id_len > SSL3_SESSION_ID_SIZE) {
1441 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1445 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1446 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1450 if (!PACKET_get_1(pkt, &compression)) {
1451 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1455 /* TLS extensions */
1456 if (PACKET_remaining(pkt) == 0 && !hrr) {
1457 PACKET_null_init(&extpkt);
1458 } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
1459 || PACKET_remaining(pkt) != 0) {
1460 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1465 if (!tls_collect_extensions(s, &extpkt,
1466 SSL_EXT_TLS1_2_SERVER_HELLO
1467 | SSL_EXT_TLS1_3_SERVER_HELLO,
1468 &extensions, NULL, 1)) {
1469 /* SSLfatal() already called */
1473 if (!ssl_choose_client_version(s, sversion, extensions)) {
1474 /* SSLfatal() already called */
1479 if (SSL_CONNECTION_IS_TLS13(s) || hrr) {
1480 if (compression != 0) {
1481 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1482 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1486 if (session_id_len != s->tmp_session_id_len
1487 || memcmp(PACKET_data(&session_id), s->tmp_session_id,
1488 session_id_len) != 0) {
1489 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID);
1495 if (!set_client_ciphersuite(s, cipherchars)) {
1496 /* SSLfatal() already called */
1500 return tls_process_as_hello_retry_request(s, &extpkt);
1504 * Now we have chosen the version we need to check again that the extensions
1505 * are appropriate for this version.
1507 context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
1508 : SSL_EXT_TLS1_2_SERVER_HELLO;
1509 if (!tls_validate_all_contexts(s, context, extensions)) {
1510 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1516 if (SSL_CONNECTION_IS_TLS13(s)) {
1518 * In TLSv1.3 a ServerHello message signals a key change so the end of
1519 * the message must be on a record boundary.
1521 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1522 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1523 SSL_R_NOT_ON_RECORD_BOUNDARY);
1527 /* This will set s->hit if we are resuming */
1528 if (!tls_parse_extension(s, TLSEXT_IDX_psk,
1529 SSL_EXT_TLS1_3_SERVER_HELLO,
1530 extensions, NULL, 0)) {
1531 /* SSLfatal() already called */
1536 * Check if we can resume the session based on external pre-shared
1537 * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
1538 * Resumption based on server-side state works with session IDs.
1539 * Resumption based on pre-shared Protected Access Credentials (PACs)
1540 * works by overriding the SessionTicket extension at the application
1541 * layer, and does not send a session ID. (We do not know whether
1542 * EAP-FAST servers would honour the session ID.) Therefore, the session
1543 * ID alone is not a reliable indicator of session resumption, so we
1544 * first check if we can resume, and later peek at the next handshake
1545 * message to see if the server wants to resume.
1547 if (s->version >= TLS1_VERSION
1548 && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
1549 const SSL_CIPHER *pref_cipher = NULL;
1551 * s->session->master_key_length is a size_t, but this is an int for
1552 * backwards compat reasons
1554 int master_key_length;
1556 master_key_length = sizeof(s->session->master_key);
1557 if (s->ext.session_secret_cb(ssl, s->session->master_key,
1560 s->ext.session_secret_cb_arg)
1561 && master_key_length > 0) {
1562 s->session->master_key_length = master_key_length;
1563 s->session->cipher = pref_cipher ?
1564 pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
1566 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1571 if (session_id_len != 0
1572 && session_id_len == s->session->session_id_length
1573 && memcmp(PACKET_data(&session_id), s->session->session_id,
1574 session_id_len) == 0)
1579 if (s->sid_ctx_length != s->session->sid_ctx_length
1580 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1581 /* actually a client application bug */
1582 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1583 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1588 * If we were trying for session-id reuse but the server
1589 * didn't resume, make a new SSL_SESSION.
1590 * In the case of EAP-FAST and PAC, we do not send a session ID,
1591 * so the PAC-based session secret is always preserved. It'll be
1592 * overwritten if the server refuses resumption.
1594 if (s->session->session_id_length > 0) {
1595 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
1596 if (!ssl_get_new_session(s, 0)) {
1597 /* SSLfatal() already called */
1602 s->session->ssl_version = s->version;
1604 * In TLSv1.2 and below we save the session id we were sent so we can
1605 * resume it later. In TLSv1.3 the session id we were sent is just an
1606 * echo of what we originally sent in the ClientHello and should not be
1607 * used for resumption.
1609 if (!SSL_CONNECTION_IS_TLS13(s)) {
1610 s->session->session_id_length = session_id_len;
1611 /* session_id_len could be 0 */
1612 if (session_id_len > 0)
1613 memcpy(s->session->session_id, PACKET_data(&session_id),
1618 /* Session version and negotiated protocol version should match */
1619 if (s->version != s->session->ssl_version) {
1620 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1621 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1625 * Now that we know the version, update the check to see if it's an allowed
1628 s->s3.tmp.min_ver = s->version;
1629 s->s3.tmp.max_ver = s->version;
1631 if (!set_client_ciphersuite(s, cipherchars)) {
1632 /* SSLfatal() already called */
1636 #ifdef OPENSSL_NO_COMP
1637 if (compression != 0) {
1638 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1639 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1643 * If compression is disabled we'd better not try to resume a session
1644 * using compression.
1646 if (s->session->compress_meth != 0) {
1647 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
1651 if (s->hit && compression != s->session->compress_meth) {
1652 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1653 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1656 if (compression == 0)
1658 else if (!ssl_allow_compression(s)) {
1659 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED);
1662 comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods,
1666 if (compression != 0 && comp == NULL) {
1667 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1668 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1671 s->s3.tmp.new_compression = comp;
1675 if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
1676 /* SSLfatal() already called */
1680 #ifndef OPENSSL_NO_SCTP
1681 if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
1682 unsigned char sctpauthkey[64];
1683 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1687 * Add new shared key for SCTP-Auth, will be ignored if
1690 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1691 sizeof(DTLS1_SCTP_AUTH_LABEL));
1693 /* Don't include the terminating zero. */
1694 labellen = sizeof(labelbuffer) - 1;
1695 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
1698 if (SSL_export_keying_material(ssl, sctpauthkey,
1699 sizeof(sctpauthkey),
1701 labellen, NULL, 0, 0) <= 0) {
1702 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1706 BIO_ctrl(SSL_get_wbio(ssl),
1707 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1708 sizeof(sctpauthkey), sctpauthkey);
1713 * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1714 * we're done with this message
1716 if (SSL_CONNECTION_IS_TLS13(s)
1717 && (!ssl->method->ssl3_enc->setup_key_block(s)
1718 || !ssl->method->ssl3_enc->change_cipher_state(s,
1719 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) {
1720 /* SSLfatal() already called */
1724 OPENSSL_free(extensions);
1725 return MSG_PROCESS_CONTINUE_READING;
1727 OPENSSL_free(extensions);
1728 return MSG_PROCESS_ERROR;
1731 static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
1734 RAW_EXTENSION *extensions = NULL;
1737 * If we were sending early_data then the enc_write_ctx is now invalid and
1738 * should not be used.
1740 EVP_CIPHER_CTX_free(s->enc_write_ctx);
1741 s->enc_write_ctx = NULL;
1743 if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1744 &extensions, NULL, 1)
1745 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1746 extensions, NULL, 0, 1)) {
1747 /* SSLfatal() already called */
1751 OPENSSL_free(extensions);
1754 if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) {
1756 * We didn't receive a cookie or a new key_share so the next
1757 * ClientHello will not change
1759 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR);
1764 * Re-initialise the Transcript Hash. We're going to prepopulate it with
1765 * a synthetic message_hash in place of ClientHello1.
1767 if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
1768 /* SSLfatal() already called */
1773 * Add this message to the Transcript Hash. Normally this is done
1774 * automatically prior to the message processing stage. However due to the
1775 * need to create the synthetic message hash, we defer that step until now
1778 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1779 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1780 /* SSLfatal() already called */
1784 return MSG_PROCESS_FINISHED_READING;
1786 OPENSSL_free(extensions);
1787 return MSG_PROCESS_ERROR;
1790 /* prepare server cert verification by setting s->session->peer_chain from pkt */
1791 MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s,
1794 unsigned long cert_list_len, cert_len;
1796 const unsigned char *certstart, *certbytes;
1798 unsigned int context = 0;
1799 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1801 if ((s->session->peer_chain = sk_X509_new_null()) == NULL) {
1802 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1806 if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
1808 || !PACKET_get_net_3(pkt, &cert_list_len)
1809 || PACKET_remaining(pkt) != cert_list_len
1810 || PACKET_remaining(pkt) == 0) {
1811 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1814 for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
1815 if (!PACKET_get_net_3(pkt, &cert_len)
1816 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1817 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
1821 certstart = certbytes;
1822 x = X509_new_ex(sctx->libctx, sctx->propq);
1824 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_MALLOC_FAILURE);
1825 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1828 if (d2i_X509(&x, (const unsigned char **)&certbytes,
1829 cert_len) == NULL) {
1830 SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB);
1834 if (certbytes != (certstart + cert_len)) {
1835 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
1839 if (SSL_CONNECTION_IS_TLS13(s)) {
1840 RAW_EXTENSION *rawexts = NULL;
1843 if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
1844 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1847 if (!tls_collect_extensions(s, &extensions,
1848 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
1849 NULL, chainidx == 0)
1850 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
1851 rawexts, x, chainidx,
1852 PACKET_remaining(pkt) == 0)) {
1853 OPENSSL_free(rawexts);
1854 /* SSLfatal already called */
1857 OPENSSL_free(rawexts);
1860 if (!sk_X509_push(s->session->peer_chain, x)) {
1861 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1866 return MSG_PROCESS_CONTINUE_PROCESSING;
1870 OSSL_STACK_OF_X509_free(s->session->peer_chain);
1871 s->session->peer_chain = NULL;
1872 return MSG_PROCESS_ERROR;
1876 * Verify the s->session->peer_chain and check server cert type.
1877 * On success set s->session->peer and s->session->verify_result.
1878 * Else the peer certificate verification callback may request retry.
1880 WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s,
1884 EVP_PKEY *pkey = NULL;
1885 const SSL_CERT_LOOKUP *clu;
1889 if (s->rwstate == SSL_RETRY_VERIFY)
1890 s->rwstate = SSL_NOTHING;
1891 i = ssl_verify_cert_chain(s, s->session->peer_chain);
1892 if (i > 0 && s->rwstate == SSL_RETRY_VERIFY) {
1896 * The documented interface is that SSL_VERIFY_PEER should be set in order
1897 * for client side verification of the server certificate to take place.
1898 * However, historically the code has only checked that *any* flag is set
1899 * to cause server verification to take place. Use of the other flags makes
1900 * no sense in client mode. An attempt to clean up the semantics was
1901 * reverted because at least one application *only* set
1902 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1903 * server verification to take place, after the clean up it silently did
1904 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1905 * sent to them because they are void functions. Therefore, we now use the
1906 * (less clean) historic behaviour of performing validation if any flag is
1907 * set. The *documented* interface remains the same.
1909 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1910 SSLfatal(s, ssl_x509err2alert(s->verify_result),
1911 SSL_R_CERTIFICATE_VERIFY_FAILED);
1914 ERR_clear_error(); /* but we keep s->verify_result */
1917 * Inconsistency alert: cert_chain does include the peer's certificate,
1918 * which we don't include in statem_srvr.c
1920 x = sk_X509_value(s->session->peer_chain, 0);
1922 pkey = X509_get0_pubkey(x);
1924 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1925 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1926 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1930 if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx)) == NULL) {
1931 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1935 * Check certificate type is consistent with ciphersuite. For TLS 1.3
1936 * skip check since TLS 1.3 ciphersuites can be used with any certificate
1939 if (!SSL_CONNECTION_IS_TLS13(s)) {
1940 if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) {
1941 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE);
1946 X509_free(s->session->peer);
1948 s->session->peer = x;
1949 s->session->verify_result = s->verify_result;
1951 /* Save the current hash state for when we receive the CertificateVerify */
1952 if (SSL_CONNECTION_IS_TLS13(s)
1953 && !ssl_handshake_hash(s, s->cert_verify_hash,
1954 sizeof(s->cert_verify_hash),
1955 &s->cert_verify_hash_len)) {
1956 /* SSLfatal() already called */;
1959 return WORK_FINISHED_CONTINUE;
1962 static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
1964 #ifndef OPENSSL_NO_PSK
1965 PACKET psk_identity_hint;
1967 /* PSK ciphersuites are preceded by an identity hint */
1969 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1970 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1975 * Store PSK identity hint for later use, hint is used in
1976 * tls_construct_client_key_exchange. Assume that the maximum length of
1977 * a PSK identity hint can be as long as the maximum length of a PSK
1980 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1981 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG);
1985 if (PACKET_remaining(&psk_identity_hint) == 0) {
1986 OPENSSL_free(s->session->psk_identity_hint);
1987 s->session->psk_identity_hint = NULL;
1988 } else if (!PACKET_strndup(&psk_identity_hint,
1989 &s->session->psk_identity_hint)) {
1990 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1996 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2001 static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2003 #ifndef OPENSSL_NO_SRP
2004 PACKET prime, generator, salt, server_pub;
2006 if (!PACKET_get_length_prefixed_2(pkt, &prime)
2007 || !PACKET_get_length_prefixed_2(pkt, &generator)
2008 || !PACKET_get_length_prefixed_1(pkt, &salt)
2009 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
2010 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2015 BN_bin2bn(PACKET_data(&prime),
2016 (int)PACKET_remaining(&prime), NULL)) == NULL
2018 BN_bin2bn(PACKET_data(&generator),
2019 (int)PACKET_remaining(&generator), NULL)) == NULL
2021 BN_bin2bn(PACKET_data(&salt),
2022 (int)PACKET_remaining(&salt), NULL)) == NULL
2024 BN_bin2bn(PACKET_data(&server_pub),
2025 (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
2026 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2030 if (!srp_verify_server_param(s)) {
2031 /* SSLfatal() already called */
2035 /* We must check if there is a certificate */
2036 if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2037 *pkey = X509_get0_pubkey(s->session->peer);
2041 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2046 static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2048 PACKET prime, generator, pub_key;
2049 EVP_PKEY *peer_tmp = NULL;
2050 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
2051 EVP_PKEY_CTX *pctx = NULL;
2052 OSSL_PARAM *params = NULL;
2053 OSSL_PARAM_BLD *tmpl = NULL;
2054 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2057 if (!PACKET_get_length_prefixed_2(pkt, &prime)
2058 || !PACKET_get_length_prefixed_2(pkt, &generator)
2059 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
2060 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2064 p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
2065 g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
2067 bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
2068 (int)PACKET_remaining(&pub_key), NULL);
2069 if (p == NULL || g == NULL || bnpub_key == NULL) {
2070 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2074 tmpl = OSSL_PARAM_BLD_new();
2076 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
2077 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
2078 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
2080 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
2081 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2085 pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq);
2087 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2090 if (EVP_PKEY_fromdata_init(pctx) <= 0
2091 || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) {
2092 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE);
2096 EVP_PKEY_CTX_free(pctx);
2097 pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq);
2100 * EVP_PKEY_param_check() will verify that the DH params are using
2101 * a safe prime. In this context, because we're using ephemeral DH,
2102 * we're ok with it not being a safe prime.
2103 * EVP_PKEY_param_check_quick() skips the safe prime check.
2105 || EVP_PKEY_param_check_quick(pctx) != 1
2106 || EVP_PKEY_public_check(pctx) != 1) {
2107 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE);
2111 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2112 EVP_PKEY_get_security_bits(peer_tmp),
2114 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
2118 s->s3.peer_tmp = peer_tmp;
2122 * FIXME: This makes assumptions about which ciphersuites come with
2123 * public keys. We should have a less ad-hoc way of doing this
2125 if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2126 *pkey = X509_get0_pubkey(s->session->peer);
2127 /* else anonymous DH, so no certificate or pkey. */
2132 OSSL_PARAM_BLD_free(tmpl);
2133 OSSL_PARAM_free(params);
2134 EVP_PKEY_free(peer_tmp);
2135 EVP_PKEY_CTX_free(pctx);
2143 static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2146 unsigned int curve_type, curve_id;
2149 * Extract elliptic curve parameters and the server's ephemeral ECDH
2150 * public key. We only support named (not generic) curves and
2151 * ECParameters in this case is just three bytes.
2153 if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
2154 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2158 * Check curve is named curve type and one of our preferences, if not
2159 * server has sent an invalid curve.
2161 if (curve_type != NAMED_CURVE_TYPE
2162 || !tls1_check_group_id(s, curve_id, 1)) {
2163 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
2167 if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) {
2168 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2169 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
2173 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
2174 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2178 if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
2179 PACKET_data(&encoded_pt),
2180 PACKET_remaining(&encoded_pt)) <= 0) {
2181 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
2186 * The ECC/TLS specification does not mention the use of DSA to sign
2187 * ECParameters in the server key exchange message. We do support RSA
2190 if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
2191 *pkey = X509_get0_pubkey(s->session->peer);
2192 else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
2193 *pkey = X509_get0_pubkey(s->session->peer);
2194 /* else anonymous ECDH, so no certificate or pkey. */
2196 /* Cache the agreed upon group in the SSL_SESSION */
2197 s->session->kex_group = curve_id;
2201 MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt)
2204 EVP_PKEY *pkey = NULL;
2205 EVP_MD_CTX *md_ctx = NULL;
2206 EVP_PKEY_CTX *pctx = NULL;
2207 PACKET save_param_start, signature;
2208 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2210 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
2212 save_param_start = *pkt;
2214 EVP_PKEY_free(s->s3.peer_tmp);
2215 s->s3.peer_tmp = NULL;
2217 if (alg_k & SSL_PSK) {
2218 if (!tls_process_ske_psk_preamble(s, pkt)) {
2219 /* SSLfatal() already called */
2224 /* Nothing else to do for plain PSK or RSAPSK */
2225 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
2226 } else if (alg_k & SSL_kSRP) {
2227 if (!tls_process_ske_srp(s, pkt, &pkey)) {
2228 /* SSLfatal() already called */
2231 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2232 if (!tls_process_ske_dhe(s, pkt, &pkey)) {
2233 /* SSLfatal() already called */
2236 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2237 if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
2238 /* SSLfatal() already called */
2242 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
2246 /* if it was signed, check the signature */
2249 const EVP_MD *md = NULL;
2255 * |pkt| now points to the beginning of the signature, so the difference
2256 * equals the length of the parameters.
2258 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
2259 PACKET_remaining(&save_param_start) -
2260 PACKET_remaining(pkt))) {
2261 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
2265 if (SSL_USE_SIGALGS(s)) {
2266 unsigned int sigalg;
2268 if (!PACKET_get_net_2(pkt, &sigalg)) {
2269 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2272 if (tls12_check_peer_sigalg(s, sigalg, pkey) <=0) {
2273 /* SSLfatal() already called */
2276 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
2277 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2281 if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) {
2282 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2283 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
2286 if (SSL_USE_SIGALGS(s))
2287 OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
2288 md == NULL ? "n/a" : EVP_MD_get0_name(md));
2290 if (!PACKET_get_length_prefixed_2(pkt, &signature)
2291 || PACKET_remaining(pkt) != 0) {
2292 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2296 md_ctx = EVP_MD_CTX_new();
2297 if (md_ctx == NULL) {
2298 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2302 if (EVP_DigestVerifyInit_ex(md_ctx, &pctx,
2303 md == NULL ? NULL : EVP_MD_get0_name(md),
2304 sctx->libctx, sctx->propq, pkey,
2306 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2309 if (SSL_USE_PSS(s)) {
2310 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2311 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
2312 RSA_PSS_SALTLEN_DIGEST) <= 0) {
2313 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2317 tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(¶ms),
2318 PACKET_remaining(¶ms));
2320 /* SSLfatal() already called */
2324 rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
2325 PACKET_remaining(&signature), tbs, tbslen);
2328 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
2331 EVP_MD_CTX_free(md_ctx);
2334 /* aNULL, aSRP or PSK do not need public keys */
2335 if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
2336 && !(alg_k & SSL_PSK)) {
2337 /* Might be wrong key type, check it */
2338 if (ssl3_check_cert_and_algorithm(s)) {
2339 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA);
2341 /* else this shouldn't happen, SSLfatal() already called */
2344 /* still data left over */
2345 if (PACKET_remaining(pkt) != 0) {
2346 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE);
2351 return MSG_PROCESS_CONTINUE_READING;
2353 EVP_MD_CTX_free(md_ctx);
2354 return MSG_PROCESS_ERROR;
2357 MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s,
2362 /* Clear certificate validity flags */
2363 for (i = 0; i < SSL_PKEY_NUM; i++)
2364 s->s3.tmp.valid_flags[i] = 0;
2366 if (SSL_CONNECTION_IS_TLS13(s)) {
2367 PACKET reqctx, extensions;
2368 RAW_EXTENSION *rawexts = NULL;
2370 if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
2372 * We already sent close_notify. This can only happen in TLSv1.3
2373 * post-handshake messages. We can't reasonably respond to this, so
2376 return MSG_PROCESS_FINISHED_READING;
2379 /* Free and zero certificate types: it is not present in TLS 1.3 */
2380 OPENSSL_free(s->s3.tmp.ctype);
2381 s->s3.tmp.ctype = NULL;
2382 s->s3.tmp.ctype_len = 0;
2383 OPENSSL_free(s->pha_context);
2384 s->pha_context = NULL;
2385 s->pha_context_len = 0;
2387 if (!PACKET_get_length_prefixed_1(pkt, &reqctx) ||
2388 !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
2389 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2390 return MSG_PROCESS_ERROR;
2393 if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
2394 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
2395 return MSG_PROCESS_ERROR;
2397 if (!tls_collect_extensions(s, &extensions,
2398 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2400 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2401 rawexts, NULL, 0, 1)) {
2402 /* SSLfatal() already called */
2403 OPENSSL_free(rawexts);
2404 return MSG_PROCESS_ERROR;
2406 OPENSSL_free(rawexts);
2407 if (!tls1_process_sigalgs(s)) {
2408 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
2409 return MSG_PROCESS_ERROR;
2414 /* get the certificate types */
2415 if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
2416 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2417 return MSG_PROCESS_ERROR;
2420 if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
2421 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2422 return MSG_PROCESS_ERROR;
2425 if (SSL_USE_SIGALGS(s)) {
2428 if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
2429 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2430 return MSG_PROCESS_ERROR;
2434 * Despite this being for certificates, preserve compatibility
2435 * with pre-TLS 1.3 and use the regular sigalgs field.
2437 if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
2438 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2439 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2440 return MSG_PROCESS_ERROR;
2442 if (!tls1_process_sigalgs(s)) {
2443 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2444 return MSG_PROCESS_ERROR;
2448 /* get the CA RDNs */
2449 if (!parse_ca_names(s, pkt)) {
2450 /* SSLfatal() already called */
2451 return MSG_PROCESS_ERROR;
2455 if (PACKET_remaining(pkt) != 0) {
2456 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2457 return MSG_PROCESS_ERROR;
2460 /* we should setup a certificate to return.... */
2461 s->s3.tmp.cert_req = 1;
2464 * In TLSv1.3 we don't prepare the client certificate yet. We wait until
2465 * after the CertificateVerify message has been received. This is because
2466 * in TLSv1.3 the CertificateRequest arrives before the Certificate message
2467 * but in TLSv1.2 it is the other way around. We want to make sure that
2468 * SSL_get1_peer_certificate() returns something sensible in
2471 if (SSL_CONNECTION_IS_TLS13(s)
2472 && s->post_handshake_auth != SSL_PHA_REQUESTED)
2473 return MSG_PROCESS_CONTINUE_READING;
2475 return MSG_PROCESS_CONTINUE_PROCESSING;
2478 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s,
2481 unsigned int ticklen;
2482 unsigned long ticket_lifetime_hint, age_add = 0;
2483 unsigned int sess_len;
2484 RAW_EXTENSION *exts = NULL;
2486 EVP_MD *sha256 = NULL;
2487 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2489 PACKET_null_init(&nonce);
2491 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2492 || (SSL_CONNECTION_IS_TLS13(s)
2493 && (!PACKET_get_net_4(pkt, &age_add)
2494 || !PACKET_get_length_prefixed_1(pkt, &nonce)))
2495 || !PACKET_get_net_2(pkt, &ticklen)
2496 || (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0
2497 || PACKET_remaining(pkt) < ticklen)
2498 : PACKET_remaining(pkt) != ticklen)) {
2499 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2504 * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
2505 * ticket. We already checked this TLSv1.3 case above, so it should never
2506 * be 0 here in that instance
2509 return MSG_PROCESS_CONTINUE_READING;
2512 * Sessions must be immutable once they go into the session cache. Otherwise
2513 * we can get multi-thread problems. Therefore we don't "update" sessions,
2514 * we replace them with a duplicate. In TLSv1.3 we need to do this every
2515 * time a NewSessionTicket arrives because those messages arrive
2516 * post-handshake and the session may have already gone into the session
2519 if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) {
2520 SSL_SESSION *new_sess;
2523 * We reused an existing session, so we need to replace it with a new
2526 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2527 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2531 if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
2532 && !SSL_CONNECTION_IS_TLS13(s)) {
2534 * In TLSv1.2 and below the arrival of a new tickets signals that
2535 * any old ticket we were using is now out of date, so we remove the
2536 * old session from the cache. We carry on if this fails
2538 SSL_CTX_remove_session(s->session_ctx, s->session);
2541 SSL_SESSION_free(s->session);
2542 s->session = new_sess;
2545 s->session->time = ossl_time_now();
2546 ssl_session_calculate_timeout(s->session);
2548 OPENSSL_free(s->session->ext.tick);
2549 s->session->ext.tick = NULL;
2550 s->session->ext.ticklen = 0;
2552 s->session->ext.tick = OPENSSL_malloc(ticklen);
2553 if (s->session->ext.tick == NULL) {
2554 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2557 if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
2558 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2562 s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
2563 s->session->ext.tick_age_add = age_add;
2564 s->session->ext.ticklen = ticklen;
2566 if (SSL_CONNECTION_IS_TLS13(s)) {
2569 if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
2570 || PACKET_remaining(pkt) != 0) {
2571 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2575 if (!tls_collect_extensions(s, &extpkt,
2576 SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
2578 || !tls_parse_all_extensions(s,
2579 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
2580 exts, NULL, 0, 1)) {
2581 /* SSLfatal() already called */
2587 * There are two ways to detect a resumed ticket session. One is to set
2588 * an appropriate session ID and then the server must return a match in
2589 * ServerHello. This allows the normal client session ID matching to work
2590 * and we know much earlier that the ticket has been accepted. The
2591 * other way is to set zero length session ID when the ticket is
2592 * presented and rely on the handshake to determine session resumption.
2593 * We choose the former approach because this fits in with assumptions
2594 * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
2597 sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
2598 if (sha256 == NULL) {
2599 /* Error is already recorded */
2600 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
2604 * We use sess_len here because EVP_Digest expects an int
2605 * but s->session->session_id_length is a size_t
2607 if (!EVP_Digest(s->session->ext.tick, ticklen,
2608 s->session->session_id, &sess_len,
2610 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2613 EVP_MD_free(sha256);
2615 s->session->session_id_length = sess_len;
2616 s->session->not_resumable = 0;
2618 /* This is a standalone message in TLSv1.3, so there is no more to read */
2619 if (SSL_CONNECTION_IS_TLS13(s)) {
2620 const EVP_MD *md = ssl_handshake_md(s);
2621 int hashleni = EVP_MD_get_size(md);
2623 static const unsigned char nonce_label[] = "resumption";
2625 /* Ensure cast to size_t is safe */
2626 if (!ossl_assert(hashleni >= 0)) {
2627 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2630 hashlen = (size_t)hashleni;
2632 if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
2634 sizeof(nonce_label) - 1,
2635 PACKET_data(&nonce),
2636 PACKET_remaining(&nonce),
2637 s->session->master_key,
2639 /* SSLfatal() already called */
2642 s->session->master_key_length = hashlen;
2645 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
2646 return MSG_PROCESS_FINISHED_READING;
2649 return MSG_PROCESS_CONTINUE_READING;
2651 EVP_MD_free(sha256);
2653 return MSG_PROCESS_ERROR;
2657 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
2658 * parse a separate message. Returns 1 on success or 0 on failure
2660 int tls_process_cert_status_body(SSL_CONNECTION *s, PACKET *pkt)
2665 if (!PACKET_get_1(pkt, &type)
2666 || type != TLSEXT_STATUSTYPE_ocsp) {
2667 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE);
2670 if (!PACKET_get_net_3_len(pkt, &resplen)
2671 || PACKET_remaining(pkt) != resplen) {
2672 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2675 s->ext.ocsp.resp = OPENSSL_malloc(resplen);
2676 if (s->ext.ocsp.resp == NULL) {
2677 s->ext.ocsp.resp_len = 0;
2678 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2681 s->ext.ocsp.resp_len = resplen;
2682 if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
2683 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2691 MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt)
2693 if (!tls_process_cert_status_body(s, pkt)) {
2694 /* SSLfatal() already called */
2695 return MSG_PROCESS_ERROR;
2698 return MSG_PROCESS_CONTINUE_READING;
2702 * Perform miscellaneous checks and processing after we have received the
2703 * server's initial flight. In TLS1.3 this is after the Server Finished message.
2704 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
2707 int tls_process_initial_server_flight(SSL_CONNECTION *s)
2709 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2712 * at this point we check that we have the required stuff from
2715 if (!ssl3_check_cert_and_algorithm(s)) {
2716 /* SSLfatal() already called */
2721 * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
2722 * |ext.ocsp.resp_len| values will be set if we actually received a status
2723 * message, or NULL and -1 otherwise
2725 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
2726 && sctx->ext.status_cb != NULL) {
2727 int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_SSL(s),
2728 sctx->ext.status_arg);
2731 SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
2732 SSL_R_INVALID_STATUS_RESPONSE);
2736 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2737 SSL_R_OCSP_CALLBACK_FAILURE);
2741 #ifndef OPENSSL_NO_CT
2742 if (s->ct_validation_callback != NULL) {
2743 /* Note we validate the SCTs whether or not we abort on error */
2744 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2745 /* SSLfatal() already called */
2754 MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt)
2756 if (PACKET_remaining(pkt) > 0) {
2757 /* should contain no data */
2758 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2759 return MSG_PROCESS_ERROR;
2761 #ifndef OPENSSL_NO_SRP
2762 if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2763 if (ssl_srp_calc_a_param_intern(s) <= 0) {
2764 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC);
2765 return MSG_PROCESS_ERROR;
2770 if (!tls_process_initial_server_flight(s)) {
2771 /* SSLfatal() already called */
2772 return MSG_PROCESS_ERROR;
2775 return MSG_PROCESS_FINISHED_READING;
2778 static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt)
2780 #ifndef OPENSSL_NO_PSK
2783 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2784 * \0-terminated identity. The last byte is for us for simulating
2787 char identity[PSK_MAX_IDENTITY_LEN + 1];
2788 size_t identitylen = 0;
2789 unsigned char psk[PSK_MAX_PSK_LEN];
2790 unsigned char *tmppsk = NULL;
2791 char *tmpidentity = NULL;
2794 if (s->psk_client_callback == NULL) {
2795 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB);
2799 memset(identity, 0, sizeof(identity));
2801 psklen = s->psk_client_callback(SSL_CONNECTION_GET_SSL(s),
2802 s->session->psk_identity_hint,
2803 identity, sizeof(identity) - 1,
2806 if (psklen > PSK_MAX_PSK_LEN) {
2807 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
2808 psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
2810 } else if (psklen == 0) {
2811 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND);
2815 identitylen = strlen(identity);
2816 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2817 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2821 tmppsk = OPENSSL_memdup(psk, psklen);
2822 tmpidentity = OPENSSL_strdup(identity);
2823 if (tmppsk == NULL || tmpidentity == NULL) {
2824 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2828 OPENSSL_free(s->s3.tmp.psk);
2829 s->s3.tmp.psk = tmppsk;
2830 s->s3.tmp.psklen = psklen;
2832 OPENSSL_free(s->session->psk_identity);
2833 s->session->psk_identity = tmpidentity;
2836 if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
2837 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2844 OPENSSL_cleanse(psk, psklen);
2845 OPENSSL_cleanse(identity, sizeof(identity));
2846 OPENSSL_clear_free(tmppsk, psklen);
2847 OPENSSL_clear_free(tmpidentity, identitylen);
2851 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2856 static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt)
2858 unsigned char *encdata = NULL;
2859 EVP_PKEY *pkey = NULL;
2860 EVP_PKEY_CTX *pctx = NULL;
2862 unsigned char *pms = NULL;
2864 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2866 if (s->session->peer == NULL) {
2868 * We should always have a server certificate with SSL_kRSA.
2870 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2874 pkey = X509_get0_pubkey(s->session->peer);
2875 if (!EVP_PKEY_is_a(pkey, "RSA")) {
2876 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2880 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2881 pms = OPENSSL_malloc(pmslen);
2883 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2887 pms[0] = s->client_version >> 8;
2888 pms[1] = s->client_version & 0xff;
2889 if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) {
2890 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2894 /* Fix buf for TLS and beyond */
2895 if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
2896 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2900 pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq);
2901 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2902 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2903 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2906 if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
2907 || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
2908 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT);
2911 EVP_PKEY_CTX_free(pctx);
2914 /* Fix buf for TLS and beyond */
2915 if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
2916 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2920 /* Log the premaster secret, if logging is enabled. */
2921 if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
2922 /* SSLfatal() already called */
2926 s->s3.tmp.pms = pms;
2927 s->s3.tmp.pmslen = pmslen;
2931 OPENSSL_clear_free(pms, pmslen);
2932 EVP_PKEY_CTX_free(pctx);
2937 static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt)
2939 EVP_PKEY *ckey = NULL, *skey = NULL;
2940 unsigned char *keybytes = NULL;
2942 unsigned char *encoded_pub = NULL;
2943 size_t encoded_pub_len, pad_len;
2946 skey = s->s3.peer_tmp;
2948 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2952 ckey = ssl_generate_pkey(s, skey);
2954 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2958 if (ssl_derive(s, ckey, skey, 0) == 0) {
2959 /* SSLfatal() already called */
2963 /* send off the data */
2965 /* Generate encoding of server key */
2966 encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub);
2967 if (encoded_pub_len == 0) {
2968 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2969 EVP_PKEY_free(ckey);
2970 return EXT_RETURN_FAIL;
2974 * For interoperability with some versions of the Microsoft TLS
2975 * stack, we need to zero pad the DHE pub key to the same length
2978 prime_len = EVP_PKEY_get_size(ckey);
2979 pad_len = prime_len - encoded_pub_len;
2981 if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) {
2982 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2985 memset(keybytes, 0, pad_len);
2988 if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) {
2989 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2995 OPENSSL_free(encoded_pub);
2996 EVP_PKEY_free(ckey);
3000 static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt)
3002 unsigned char *encodedPoint = NULL;
3003 size_t encoded_pt_len = 0;
3004 EVP_PKEY *ckey = NULL, *skey = NULL;
3007 skey = s->s3.peer_tmp;
3009 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3013 ckey = ssl_generate_pkey(s, skey);
3015 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3019 if (ssl_derive(s, ckey, skey, 0) == 0) {
3020 /* SSLfatal() already called */
3024 /* Generate encoding of client key */
3025 encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint);
3027 if (encoded_pt_len == 0) {
3028 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
3032 if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
3033 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3039 OPENSSL_free(encodedPoint);
3040 EVP_PKEY_free(ckey);
3044 static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt)
3046 #ifndef OPENSSL_NO_GOST
3047 /* GOST key exchange message creation */
3048 EVP_PKEY_CTX *pkey_ctx = NULL;
3051 unsigned int md_len;
3052 unsigned char shared_ukm[32], tmp[256];
3053 EVP_MD_CTX *ukm_hash = NULL;
3054 int dgst_nid = NID_id_GostR3411_94;
3055 unsigned char *pms = NULL;
3057 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3059 if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
3060 dgst_nid = NID_id_GostR3411_2012_256;
3063 * Get server certificate PKEY and create ctx from it
3065 peer_cert = s->session->peer;
3066 if (peer_cert == NULL) {
3067 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3068 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3072 pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
3073 X509_get0_pubkey(peer_cert),
3075 if (pkey_ctx == NULL) {
3076 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3080 * If we have send a certificate, and certificate key
3081 * parameters match those of server certificate, use
3082 * certificate key for key exchange
3085 /* Otherwise, generate ephemeral key pair */
3087 pms = OPENSSL_malloc(pmslen);
3089 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3093 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3094 /* Generate session key
3096 || RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
3097 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3101 * Compute shared IV and store it in algorithm-specific context
3104 ukm_hash = EVP_MD_CTX_new();
3105 if (ukm_hash == NULL
3106 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
3107 || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
3108 SSL3_RANDOM_SIZE) <= 0
3109 || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
3110 SSL3_RANDOM_SIZE) <= 0
3111 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3112 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3115 EVP_MD_CTX_free(ukm_hash);
3117 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3118 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) <= 0) {
3119 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3122 /* Make GOST keytransport blob message */
3124 * Encapsulate it into sequence
3127 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
3128 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3132 if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
3133 || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
3134 || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
3135 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3139 EVP_PKEY_CTX_free(pkey_ctx);
3140 s->s3.tmp.pms = pms;
3141 s->s3.tmp.pmslen = pmslen;
3145 EVP_PKEY_CTX_free(pkey_ctx);
3146 OPENSSL_clear_free(pms, pmslen);
3147 EVP_MD_CTX_free(ukm_hash);
3150 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3155 #ifndef OPENSSL_NO_GOST
3156 int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s)
3158 if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0)
3159 return NID_magma_ctr;
3160 else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0)
3161 return NID_kuznyechik_ctr;
3166 int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf)
3168 EVP_MD_CTX * hash = NULL;
3169 unsigned int md_len;
3170 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3171 const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256,
3177 if ((hash = EVP_MD_CTX_new()) == NULL
3178 || EVP_DigestInit(hash, md) <= 0
3179 || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0
3180 || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0
3181 || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) {
3182 EVP_MD_CTX_free(hash);
3183 ssl_evp_md_free(md);
3187 EVP_MD_CTX_free(hash);
3188 ssl_evp_md_free(md);
3193 static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt)
3195 #ifndef OPENSSL_NO_GOST
3196 /* GOST 2018 key exchange message creation */
3197 unsigned char rnd_dgst[32];
3198 unsigned char *encdata = NULL;
3199 EVP_PKEY_CTX *pkey_ctx = NULL;
3201 unsigned char *pms = NULL;
3204 int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3205 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3207 if (cipher_nid == NID_undef) {
3208 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3212 if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3213 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3217 /* Pre-master secret - random bytes */
3219 pms = OPENSSL_malloc(pmslen);
3221 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3225 if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
3226 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3230 /* Get server certificate PKEY and create ctx from it */
3231 peer_cert = s->session->peer;
3232 if (peer_cert == NULL) {
3233 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3234 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3238 pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
3239 X509_get0_pubkey(peer_cert),
3241 if (pkey_ctx == NULL) {
3242 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3246 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) {
3247 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3251 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
3252 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3253 EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
3254 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3258 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3259 EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
3260 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3264 if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) {
3265 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3269 if (!WPACKET_allocate_bytes(pkt, msglen, &encdata)
3270 || EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) {
3271 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3275 EVP_PKEY_CTX_free(pkey_ctx);
3277 s->s3.tmp.pms = pms;
3278 s->s3.tmp.pmslen = pmslen;
3282 EVP_PKEY_CTX_free(pkey_ctx);
3283 OPENSSL_clear_free(pms, pmslen);
3286 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3291 static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt)
3293 #ifndef OPENSSL_NO_SRP
3294 unsigned char *abytes = NULL;
3296 if (s->srp_ctx.A == NULL
3297 || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
3299 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3302 BN_bn2bin(s->srp_ctx.A, abytes);
3304 OPENSSL_free(s->session->srp_username);
3305 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3306 if (s->session->srp_username == NULL) {
3307 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3313 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3318 int tls_construct_client_key_exchange(SSL_CONNECTION *s, WPACKET *pkt)
3320 unsigned long alg_k;
3322 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3325 * All of the construct functions below call SSLfatal() if necessary so
3326 * no need to do so here.
3328 if ((alg_k & SSL_PSK)
3329 && !tls_construct_cke_psk_preamble(s, pkt))
3332 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3333 if (!tls_construct_cke_rsa(s, pkt))
3335 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3336 if (!tls_construct_cke_dhe(s, pkt))
3338 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3339 if (!tls_construct_cke_ecdhe(s, pkt))
3341 } else if (alg_k & SSL_kGOST) {
3342 if (!tls_construct_cke_gost(s, pkt))
3344 } else if (alg_k & SSL_kGOST18) {
3345 if (!tls_construct_cke_gost18(s, pkt))
3347 } else if (alg_k & SSL_kSRP) {
3348 if (!tls_construct_cke_srp(s, pkt))
3350 } else if (!(alg_k & SSL_kPSK)) {
3351 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3357 OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
3358 s->s3.tmp.pms = NULL;
3359 s->s3.tmp.pmslen = 0;
3360 #ifndef OPENSSL_NO_PSK
3361 OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
3362 s->s3.tmp.psk = NULL;
3363 s->s3.tmp.psklen = 0;
3368 int tls_client_key_exchange_post_work(SSL_CONNECTION *s)
3370 unsigned char *pms = NULL;
3373 pms = s->s3.tmp.pms;
3374 pmslen = s->s3.tmp.pmslen;
3376 #ifndef OPENSSL_NO_SRP
3378 if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3379 if (!srp_generate_client_master_secret(s)) {
3380 /* SSLfatal() already called */
3387 if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
3388 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3391 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
3392 /* SSLfatal() already called */
3393 /* ssl_generate_master_secret frees the pms even on error */
3401 #ifndef OPENSSL_NO_SCTP
3402 if (SSL_CONNECTION_IS_DTLS(s)) {
3403 unsigned char sctpauthkey[64];
3404 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3406 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3409 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3412 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3413 sizeof(DTLS1_SCTP_AUTH_LABEL));
3415 /* Don't include the terminating zero. */
3416 labellen = sizeof(labelbuffer) - 1;
3417 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3420 if (SSL_export_keying_material(ssl, sctpauthkey,
3421 sizeof(sctpauthkey), labelbuffer,
3422 labellen, NULL, 0, 0) <= 0) {
3423 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3427 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3428 sizeof(sctpauthkey), sctpauthkey);
3434 OPENSSL_clear_free(pms, pmslen);
3435 s->s3.tmp.pms = NULL;
3436 s->s3.tmp.pmslen = 0;
3441 * Check a certificate can be used for client authentication. Currently check
3442 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3443 * certificates can be used and optionally checks suitability for Suite B.
3445 static int ssl3_check_client_certificate(SSL_CONNECTION *s)
3447 /* If no suitable signature algorithm can't use certificate */
3448 if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
3451 * If strict mode check suitability of chain before using it. This also
3452 * adjusts suite B digest if necessary.
3454 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3455 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3460 WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst)
3463 EVP_PKEY *pkey = NULL;
3465 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3467 if (wst == WORK_MORE_A) {
3468 /* Let cert callback update client certificates if required */
3469 if (s->cert->cert_cb) {
3470 i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
3472 s->rwstate = SSL_X509_LOOKUP;
3476 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
3479 s->rwstate = SSL_NOTHING;
3481 if (ssl3_check_client_certificate(s)) {
3482 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3483 return WORK_FINISHED_STOP;
3485 return WORK_FINISHED_CONTINUE;
3488 /* Fall through to WORK_MORE_B */
3492 /* We need to get a client cert */
3493 if (wst == WORK_MORE_B) {
3495 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3496 * return(-1); We then get retied later
3498 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3500 s->rwstate = SSL_X509_LOOKUP;
3503 s->rwstate = SSL_NOTHING;
3504 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3505 if (!SSL_use_certificate(ssl, x509)
3506 || !SSL_use_PrivateKey(ssl, pkey))
3508 } else if (i == 1) {
3510 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3514 EVP_PKEY_free(pkey);
3515 if (i && !ssl3_check_client_certificate(s))
3518 if (s->version == SSL3_VERSION) {
3519 s->s3.tmp.cert_req = 0;
3520 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3521 return WORK_FINISHED_CONTINUE;
3523 s->s3.tmp.cert_req = 2;
3524 if (!ssl3_digest_cached_records(s, 0)) {
3525 /* SSLfatal() already called */
3531 if (s->post_handshake_auth == SSL_PHA_REQUESTED)
3532 return WORK_FINISHED_STOP;
3533 return WORK_FINISHED_CONTINUE;
3536 /* Shouldn't ever get here */
3537 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3541 int tls_construct_client_certificate(SSL_CONNECTION *s, WPACKET *pkt)
3543 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3545 if (SSL_CONNECTION_IS_TLS13(s)) {
3546 if (s->pha_context == NULL) {
3547 /* no context available, add 0-length context */
3548 if (!WPACKET_put_bytes_u8(pkt, 0)) {
3549 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3552 } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
3553 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3557 if (!ssl3_output_cert_chain(s, pkt,
3558 (s->s3.tmp.cert_req == 2) ? NULL
3560 /* SSLfatal() already called */
3564 if (SSL_CONNECTION_IS_TLS13(s)
3565 && SSL_IS_FIRST_HANDSHAKE(s)
3566 && (!ssl->method->ssl3_enc->change_cipher_state(s,
3567 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
3569 * This is a fatal error, which leaves enc_write_ctx in an inconsistent
3570 * state and thus ssl3_send_alert may crash.
3572 SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
3579 int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s)
3581 const SSL_CERT_LOOKUP *clu;
3585 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3586 alg_a = s->s3.tmp.new_cipher->algorithm_auth;
3588 /* we don't have a certificate */
3589 if (!(alg_a & SSL_aCERT))
3592 /* This is the passed certificate */
3593 clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx);
3595 /* Check certificate is recognised and suitable for cipher */
3596 if (clu == NULL || (alg_a & clu->amask) == 0) {
3597 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT);
3601 if (clu->amask & SSL_aECDSA) {
3602 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
3604 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT);
3608 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
3609 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3610 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3614 if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
3615 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3622 #ifndef OPENSSL_NO_NEXTPROTONEG
3623 int tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt)
3625 size_t len, padding_len;
3626 unsigned char *padding = NULL;
3628 len = s->ext.npn_len;
3629 padding_len = 32 - ((len + 2) % 32);
3631 if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
3632 || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
3633 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3637 memset(padding, 0, padding_len);
3643 MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt)
3645 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3647 if (PACKET_remaining(pkt) > 0) {
3648 /* should contain no data */
3649 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3650 return MSG_PROCESS_ERROR;
3653 if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
3654 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
3655 return MSG_PROCESS_FINISHED_READING;
3659 * This is a historical discrepancy (not in the RFC) maintained for
3660 * compatibility reasons. If a TLS client receives a HelloRequest it will
3661 * attempt an abbreviated handshake. However if a DTLS client receives a
3662 * HelloRequest it will do a full handshake. Either behaviour is reasonable
3663 * but doing one for TLS and another for DTLS is odd.
3665 if (SSL_CONNECTION_IS_DTLS(s))
3666 SSL_renegotiate(ssl);
3668 SSL_renegotiate_abbreviated(ssl);
3670 return MSG_PROCESS_FINISHED_READING;
3673 static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
3677 RAW_EXTENSION *rawexts = NULL;
3679 if (!PACKET_as_length_prefixed_2(pkt, &extensions)
3680 || PACKET_remaining(pkt) != 0) {
3681 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3685 if (!tls_collect_extensions(s, &extensions,
3686 SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
3688 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
3689 rawexts, NULL, 0, 1)) {
3690 /* SSLfatal() already called */
3694 OPENSSL_free(rawexts);
3695 return MSG_PROCESS_CONTINUE_READING;
3698 OPENSSL_free(rawexts);
3699 return MSG_PROCESS_ERROR;
3702 int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey)
3705 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3707 #ifndef OPENSSL_NO_ENGINE
3708 if (sctx->client_cert_engine) {
3709 i = tls_engine_load_ssl_client_cert(s, px509, ppkey);
3714 if (sctx->client_cert_cb)
3715 i = sctx->client_cert_cb(SSL_CONNECTION_GET_SSL(s), px509, ppkey);
3719 int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
3723 size_t totlen = 0, len, maxlen, maxverok = 0;
3724 int empty_reneg_info_scsv = !s->renegotiate;
3725 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3727 /* Set disabled masks for this session */
3728 if (!ssl_set_client_disabled(s)) {
3729 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE);
3734 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3738 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
3739 # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
3740 # error Max cipher length too short
3743 * Some servers hang if client hello > 256 bytes as hack workaround
3744 * chop number of supported ciphers to keep it well below this if we
3747 if (TLS1_get_version(ssl) >= TLS1_2_VERSION)
3748 maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
3751 /* Maximum length that can be stored in 2 bytes. Length must be even */
3754 if (empty_reneg_info_scsv)
3756 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
3759 for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
3760 const SSL_CIPHER *c;
3762 c = sk_SSL_CIPHER_value(sk, i);
3763 /* Skip disabled ciphers */
3764 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
3767 if (!ssl->method->put_cipher_by_char(c, pkt, &len)) {
3768 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3772 /* Sanity check that the maximum version we offer has ciphers enabled */
3774 if (SSL_CONNECTION_IS_DTLS(s)) {
3775 if (DTLS_VERSION_GE(c->max_dtls, s->s3.tmp.max_ver)
3776 && DTLS_VERSION_LE(c->min_dtls, s->s3.tmp.max_ver))
3779 if (c->max_tls >= s->s3.tmp.max_ver
3780 && c->min_tls <= s->s3.tmp.max_ver)
3788 if (totlen == 0 || !maxverok) {
3789 const char *maxvertext =
3791 ? "No ciphers enabled for max supported SSL/TLS version"
3794 SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE,
3800 if (empty_reneg_info_scsv) {
3801 static SSL_CIPHER scsv = {
3802 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3804 if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
3805 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3809 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3810 static SSL_CIPHER scsv = {
3811 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3813 if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
3814 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3823 int tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt)
3825 if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
3826 && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
3827 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3831 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;