2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
23 /* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
51 #include "../ssl_locl.h"
52 #include "statem_locl.h"
53 #include <openssl/buffer.h>
54 #include <openssl/rand.h>
55 #include <openssl/objects.h>
56 #include <openssl/evp.h>
57 #include <openssl/md5.h>
58 #include <openssl/dh.h>
59 #include <openssl/bn.h>
60 #include <openssl/engine.h>
62 static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt);
64 static ossl_inline int cert_req_allowed(SSL *s);
65 static int key_exchange_expected(SSL *s);
66 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
67 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
71 * Is a CertificateRequest message allowed at the moment or not?
77 static ossl_inline int cert_req_allowed(SSL *s)
79 /* TLS does not like anon-DH with client cert */
80 if ((s->version > SSL3_VERSION
81 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
82 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
89 * Should we expect the ServerKeyExchange message or not?
95 static int key_exchange_expected(SSL *s)
97 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
100 * Can't skip server key exchange if this is an ephemeral
101 * ciphersuite or for SRP
103 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
112 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
113 * handshake state transitions when a TLS1.3 client is reading messages from the
114 * server. The message type that the server has sent is provided in |mt|. The
115 * current state is in |s->statem.hand_state|.
117 * Return values are 1 for success (transition allowed) and 0 on error
118 * (transition not allowed)
120 static int ossl_statem_client13_read_transition(SSL *s, int mt)
122 OSSL_STATEM *st = &s->statem;
125 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
126 * we will update this to look more like real TLSv1.3
130 * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
131 * yet negotiated TLSv1.3 at that point so that is handled by
132 * ossl_statem_client_read_transition()
135 switch (st->hand_state) {
139 case TLS_ST_CR_SRVR_HELLO:
140 if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
141 st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
146 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
148 if (mt == SSL3_MT_FINISHED) {
149 st->hand_state = TLS_ST_CR_FINISHED;
153 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
154 st->hand_state = TLS_ST_CR_CERT_REQ;
157 if (mt == SSL3_MT_CERTIFICATE) {
158 st->hand_state = TLS_ST_CR_CERT;
164 case TLS_ST_CR_CERT_REQ:
165 if (mt == SSL3_MT_CERTIFICATE) {
166 st->hand_state = TLS_ST_CR_CERT;
173 * The CertificateStatus message is optional even if
174 * |tlsext_status_expected| is set
176 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
177 st->hand_state = TLS_ST_CR_CERT_STATUS;
182 case TLS_ST_CR_CERT_STATUS:
183 if (mt == SSL3_MT_FINISHED) {
184 st->hand_state = TLS_ST_CR_FINISHED;
191 /* No valid transition found */
196 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
197 * handshake state transitions when the client is reading messages from the
198 * server. The message type that the server has sent is provided in |mt|. The
199 * current state is in |s->statem.hand_state|.
201 * Return values are 1 for success (transition allowed) and 0 on error
202 * (transition not allowed)
204 int ossl_statem_client_read_transition(SSL *s, int mt)
206 OSSL_STATEM *st = &s->statem;
210 * Note that after a ClientHello we don't know what version we are going
211 * to negotiate yet, so we don't take this branch until later
213 if (SSL_IS_TLS13(s)) {
214 if (!ossl_statem_client13_read_transition(s, mt))
219 switch (st->hand_state) {
223 case TLS_ST_CW_CLNT_HELLO:
224 if (mt == SSL3_MT_SERVER_HELLO) {
225 st->hand_state = TLS_ST_CR_SRVR_HELLO;
229 if (SSL_IS_DTLS(s)) {
230 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
231 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
237 case TLS_ST_CR_SRVR_HELLO:
239 if (s->tlsext_ticket_expected) {
240 if (mt == SSL3_MT_NEWSESSION_TICKET) {
241 st->hand_state = TLS_ST_CR_SESSION_TICKET;
244 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
245 st->hand_state = TLS_ST_CR_CHANGE;
249 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
250 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
252 } else if (s->version >= TLS1_VERSION
253 && s->tls_session_secret_cb != NULL
254 && s->session->tlsext_tick != NULL
255 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
257 * Normally, we can tell if the server is resuming the session
258 * from the session ID. EAP-FAST (RFC 4851), however, relies on
259 * the next server message after the ServerHello to determine if
260 * the server is resuming.
263 st->hand_state = TLS_ST_CR_CHANGE;
265 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
266 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
267 if (mt == SSL3_MT_CERTIFICATE) {
268 st->hand_state = TLS_ST_CR_CERT;
272 ske_expected = key_exchange_expected(s);
273 /* SKE is optional for some PSK ciphersuites */
275 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
276 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
277 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
278 st->hand_state = TLS_ST_CR_KEY_EXCH;
281 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
282 && cert_req_allowed(s)) {
283 st->hand_state = TLS_ST_CR_CERT_REQ;
285 } else if (mt == SSL3_MT_SERVER_DONE) {
286 st->hand_state = TLS_ST_CR_SRVR_DONE;
295 * The CertificateStatus message is optional even if
296 * |tlsext_status_expected| is set
298 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
299 st->hand_state = TLS_ST_CR_CERT_STATUS;
304 case TLS_ST_CR_CERT_STATUS:
305 ske_expected = key_exchange_expected(s);
306 /* SKE is optional for some PSK ciphersuites */
307 if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
308 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
309 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
310 st->hand_state = TLS_ST_CR_KEY_EXCH;
317 case TLS_ST_CR_KEY_EXCH:
318 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
319 if (cert_req_allowed(s)) {
320 st->hand_state = TLS_ST_CR_CERT_REQ;
327 case TLS_ST_CR_CERT_REQ:
328 if (mt == SSL3_MT_SERVER_DONE) {
329 st->hand_state = TLS_ST_CR_SRVR_DONE;
334 case TLS_ST_CW_FINISHED:
335 if (s->tlsext_ticket_expected) {
336 if (mt == SSL3_MT_NEWSESSION_TICKET) {
337 st->hand_state = TLS_ST_CR_SESSION_TICKET;
340 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
341 st->hand_state = TLS_ST_CR_CHANGE;
346 case TLS_ST_CR_SESSION_TICKET:
347 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
348 st->hand_state = TLS_ST_CR_CHANGE;
353 case TLS_ST_CR_CHANGE:
354 if (mt == SSL3_MT_FINISHED) {
355 st->hand_state = TLS_ST_CR_FINISHED;
362 /* No valid transition found */
363 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
364 SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
369 * ossl_statem_client13_write_transition() works out what handshake state to
370 * move to next when the TLSv1.3 client is writing messages to be sent to the
373 static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s)
375 OSSL_STATEM *st = &s->statem;
378 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
379 * we will update this to look more like real TLSv1.3
383 * Note: There are no cases for TLS_ST_BEFORE or TLS_ST_CW_CLNT_HELLO,
384 * because we haven't negotiated TLSv1.3 yet at that point. They are
385 * handled by ossl_statem_client_write_transition().
387 switch (st->hand_state) {
389 /* Shouldn't happen */
390 return WRITE_TRAN_ERROR;
392 case TLS_ST_CR_FINISHED:
393 st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
394 : TLS_ST_CW_FINISHED;
395 return WRITE_TRAN_CONTINUE;
398 /* If a non-empty Certificate we also send CertificateVerify */
399 st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
400 : TLS_ST_CW_FINISHED;
401 return WRITE_TRAN_CONTINUE;
403 case TLS_ST_CW_CERT_VRFY:
404 st->hand_state = TLS_ST_CW_FINISHED;
405 return WRITE_TRAN_CONTINUE;
407 case TLS_ST_CW_FINISHED:
408 st->hand_state = TLS_ST_OK;
409 ossl_statem_set_in_init(s, 0);
410 return WRITE_TRAN_CONTINUE;
415 * ossl_statem_client_write_transition() works out what handshake state to
416 * move to next when the client is writing messages to be sent to the server.
418 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
420 OSSL_STATEM *st = &s->statem;
423 * Note that immediately before/after a ClientHello we don't know what
424 * version we are going to negotiate yet, so we don't take this branch until
428 return ossl_statem_client13_write_transition(s);
430 switch (st->hand_state) {
432 /* Shouldn't happen */
433 return WRITE_TRAN_ERROR;
436 /* Renegotiation - fall through */
438 st->hand_state = TLS_ST_CW_CLNT_HELLO;
439 return WRITE_TRAN_CONTINUE;
441 case TLS_ST_CW_CLNT_HELLO:
443 * No transition at the end of writing because we don't know what
446 return WRITE_TRAN_FINISHED;
448 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
449 st->hand_state = TLS_ST_CW_CLNT_HELLO;
450 return WRITE_TRAN_CONTINUE;
452 case TLS_ST_CR_SRVR_DONE:
453 if (s->s3->tmp.cert_req)
454 st->hand_state = TLS_ST_CW_CERT;
456 st->hand_state = TLS_ST_CW_KEY_EXCH;
457 return WRITE_TRAN_CONTINUE;
460 st->hand_state = TLS_ST_CW_KEY_EXCH;
461 return WRITE_TRAN_CONTINUE;
463 case TLS_ST_CW_KEY_EXCH:
465 * For TLS, cert_req is set to 2, so a cert chain of nothing is
466 * sent, but no verify packet is sent
469 * XXX: For now, we do not support client authentication in ECDH
470 * cipher suites with ECDH (rather than ECDSA) certificates. We
471 * need to skip the certificate verify message when client's
472 * ECDH public key is sent inside the client certificate.
474 if (s->s3->tmp.cert_req == 1) {
475 st->hand_state = TLS_ST_CW_CERT_VRFY;
477 st->hand_state = TLS_ST_CW_CHANGE;
479 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
480 st->hand_state = TLS_ST_CW_CHANGE;
482 return WRITE_TRAN_CONTINUE;
484 case TLS_ST_CW_CERT_VRFY:
485 st->hand_state = TLS_ST_CW_CHANGE;
486 return WRITE_TRAN_CONTINUE;
488 case TLS_ST_CW_CHANGE:
489 #if defined(OPENSSL_NO_NEXTPROTONEG)
490 st->hand_state = TLS_ST_CW_FINISHED;
492 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
493 st->hand_state = TLS_ST_CW_NEXT_PROTO;
495 st->hand_state = TLS_ST_CW_FINISHED;
497 return WRITE_TRAN_CONTINUE;
499 #if !defined(OPENSSL_NO_NEXTPROTONEG)
500 case TLS_ST_CW_NEXT_PROTO:
501 st->hand_state = TLS_ST_CW_FINISHED;
502 return WRITE_TRAN_CONTINUE;
505 case TLS_ST_CW_FINISHED:
507 st->hand_state = TLS_ST_OK;
508 ossl_statem_set_in_init(s, 0);
509 return WRITE_TRAN_CONTINUE;
511 return WRITE_TRAN_FINISHED;
514 case TLS_ST_CR_FINISHED:
516 st->hand_state = TLS_ST_CW_CHANGE;
517 return WRITE_TRAN_CONTINUE;
519 st->hand_state = TLS_ST_OK;
520 ossl_statem_set_in_init(s, 0);
521 return WRITE_TRAN_CONTINUE;
527 * Perform any pre work that needs to be done prior to sending a message from
528 * the client to the server.
530 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
532 OSSL_STATEM *st = &s->statem;
534 switch (st->hand_state) {
536 /* No pre work to be done */
539 case TLS_ST_CW_CLNT_HELLO:
541 if (SSL_IS_DTLS(s)) {
542 /* every DTLS ClientHello resets Finished MAC */
543 if (!ssl3_init_finished_mac(s)) {
544 ossl_statem_set_error(s);
550 case TLS_ST_CW_CHANGE:
551 if (SSL_IS_DTLS(s)) {
554 * We're into the last flight so we don't retransmit these
555 * messages unless we need to.
559 #ifndef OPENSSL_NO_SCTP
560 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
561 return dtls_wait_for_dry(s);
567 return tls_finish_handshake(s, wst);
570 return WORK_FINISHED_CONTINUE;
574 * Perform any work that needs to be done after sending a message from the
575 * client to the server.
577 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
579 OSSL_STATEM *st = &s->statem;
583 switch (st->hand_state) {
585 /* No post work to be done */
588 case TLS_ST_CW_CLNT_HELLO:
589 if (wst == WORK_MORE_A && statem_flush(s) != 1)
592 if (SSL_IS_DTLS(s)) {
593 /* Treat the next message as the first packet */
598 case TLS_ST_CW_KEY_EXCH:
599 if (tls_client_key_exchange_post_work(s) == 0)
603 case TLS_ST_CW_CHANGE:
604 s->session->cipher = s->s3->tmp.new_cipher;
605 #ifdef OPENSSL_NO_COMP
606 s->session->compress_meth = 0;
608 if (s->s3->tmp.new_compression == NULL)
609 s->session->compress_meth = 0;
611 s->session->compress_meth = s->s3->tmp.new_compression->id;
613 if (!s->method->ssl3_enc->setup_key_block(s))
616 if (!s->method->ssl3_enc->change_cipher_state(s,
617 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
620 if (SSL_IS_DTLS(s)) {
621 #ifndef OPENSSL_NO_SCTP
624 * Change to new shared key of SCTP-Auth, will be ignored if
627 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
632 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
636 case TLS_ST_CW_FINISHED:
637 #ifndef OPENSSL_NO_SCTP
638 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
640 * Change to new shared key of SCTP-Auth, will be ignored if
643 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
647 if (statem_flush(s) != 1)
650 if (SSL_IS_TLS13(s)) {
651 if (!s->method->ssl3_enc->change_cipher_state(s,
652 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
658 return WORK_FINISHED_CONTINUE;
662 * Get the message construction function and message type for sending from the
665 * Valid return values are:
669 int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
670 confunc_f *confunc, int *mt)
672 OSSL_STATEM *st = &s->statem;
674 switch (st->hand_state) {
676 /* Shouldn't happen */
679 case TLS_ST_CW_CHANGE:
681 *confunc = dtls_construct_change_cipher_spec;
683 *confunc = tls_construct_change_cipher_spec;
684 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
687 case TLS_ST_CW_CLNT_HELLO:
688 *confunc = tls_construct_client_hello;
689 *mt = SSL3_MT_CLIENT_HELLO;
693 *confunc = tls_construct_client_certificate;
694 *mt = SSL3_MT_CERTIFICATE;
697 case TLS_ST_CW_KEY_EXCH:
698 *confunc = tls_construct_client_key_exchange;
699 *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
702 case TLS_ST_CW_CERT_VRFY:
703 *confunc = tls_construct_client_verify;
704 *mt = SSL3_MT_CERTIFICATE_VERIFY;
707 #if !defined(OPENSSL_NO_NEXTPROTONEG)
708 case TLS_ST_CW_NEXT_PROTO:
709 *confunc = tls_construct_next_proto;
710 *mt = SSL3_MT_NEXT_PROTO;
713 case TLS_ST_CW_FINISHED:
714 *confunc = tls_construct_finished;
715 *mt = SSL3_MT_FINISHED;
723 * Returns the maximum allowed length for the current message that we are
724 * reading. Excludes the message header.
726 size_t ossl_statem_client_max_message_size(SSL *s)
728 OSSL_STATEM *st = &s->statem;
730 switch (st->hand_state) {
732 /* Shouldn't happen */
735 case TLS_ST_CR_SRVR_HELLO:
736 return SERVER_HELLO_MAX_LENGTH;
738 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
739 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
742 return s->max_cert_list;
744 case TLS_ST_CR_CERT_STATUS:
745 return SSL3_RT_MAX_PLAIN_LENGTH;
747 case TLS_ST_CR_KEY_EXCH:
748 return SERVER_KEY_EXCH_MAX_LENGTH;
750 case TLS_ST_CR_CERT_REQ:
752 * Set to s->max_cert_list for compatibility with previous releases. In
753 * practice these messages can get quite long if servers are configured
754 * to provide a long list of acceptable CAs
756 return s->max_cert_list;
758 case TLS_ST_CR_SRVR_DONE:
759 return SERVER_HELLO_DONE_MAX_LENGTH;
761 case TLS_ST_CR_CHANGE:
762 if (s->version == DTLS1_BAD_VER)
764 return CCS_MAX_LENGTH;
766 case TLS_ST_CR_SESSION_TICKET:
767 return SSL3_RT_MAX_PLAIN_LENGTH;
769 case TLS_ST_CR_FINISHED:
770 return FINISHED_MAX_LENGTH;
772 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
773 return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
778 * Process a message that the client has been received from the server.
780 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
782 OSSL_STATEM *st = &s->statem;
784 switch (st->hand_state) {
786 /* Shouldn't happen */
787 return MSG_PROCESS_ERROR;
789 case TLS_ST_CR_SRVR_HELLO:
790 return tls_process_server_hello(s, pkt);
792 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
793 return dtls_process_hello_verify(s, pkt);
796 return tls_process_server_certificate(s, pkt);
798 case TLS_ST_CR_CERT_STATUS:
799 return tls_process_cert_status(s, pkt);
801 case TLS_ST_CR_KEY_EXCH:
802 return tls_process_key_exchange(s, pkt);
804 case TLS_ST_CR_CERT_REQ:
805 return tls_process_certificate_request(s, pkt);
807 case TLS_ST_CR_SRVR_DONE:
808 return tls_process_server_done(s, pkt);
810 case TLS_ST_CR_CHANGE:
811 return tls_process_change_cipher_spec(s, pkt);
813 case TLS_ST_CR_SESSION_TICKET:
814 return tls_process_new_session_ticket(s, pkt);
816 case TLS_ST_CR_FINISHED:
817 return tls_process_finished(s, pkt);
819 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
820 return tls_process_encrypted_extensions(s, pkt);
825 * Perform any further processing required following the receipt of a message
828 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
830 OSSL_STATEM *st = &s->statem;
832 switch (st->hand_state) {
834 /* Shouldn't happen */
837 case TLS_ST_CR_CERT_REQ:
838 return tls_prepare_client_certificate(s, wst);
840 #ifndef OPENSSL_NO_SCTP
841 case TLS_ST_CR_SRVR_DONE:
842 /* We only get here if we are using SCTP and we are renegotiating */
843 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
844 s->s3->in_read_app_data = 2;
845 s->rwstate = SSL_READING;
846 BIO_clear_retry_flags(SSL_get_rbio(s));
847 BIO_set_retry_read(SSL_get_rbio(s));
848 ossl_statem_set_sctp_read_sock(s, 1);
851 ossl_statem_set_sctp_read_sock(s, 0);
852 return WORK_FINISHED_STOP;
857 int tls_construct_client_hello(SSL *s, WPACKET *pkt)
862 int al = SSL_AD_HANDSHAKE_FAILURE;
863 #ifndef OPENSSL_NO_COMP
866 SSL_SESSION *sess = s->session;
868 if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) {
869 /* Should not happen */
870 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
874 /* Work out what SSL/TLS/DTLS version to use */
875 protverr = ssl_set_client_hello_version(s);
877 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
881 if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
883 * In the case of EAP-FAST, we can have a pre-shared
884 * "ticket" without a session ID.
886 (!sess->session_id_length && !sess->tlsext_tick) ||
887 (sess->not_resumable)) {
888 if (!ssl_get_new_session(s, 0))
891 /* else use the pre-loaded session */
893 p = s->s3->client_random;
896 * for DTLS if client_random is initialized, reuse it, we are
897 * required to use same upon reply to HelloVerify
899 if (SSL_IS_DTLS(s)) {
902 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
911 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
915 * version indicates the negotiated version: for example from
916 * an SSLv2/v3 compatible client hello). The client_version
917 * field is the maximum version we permit and it is also
918 * used in RSA encrypted premaster secrets. Some servers can
919 * choke if we initially report a higher version then
920 * renegotiate to a lower one in the premaster secret. This
921 * didn't happen with TLS 1.0 as most servers supported it
922 * but it can with TLS 1.1 or later if the server only supports
925 * Possible scenario with previous logic:
926 * 1. Client hello indicates TLS 1.2
927 * 2. Server hello says TLS 1.0
928 * 3. RSA encrypted premaster secret uses 1.2.
929 * 4. Handshake proceeds using TLS 1.0.
930 * 5. Server sends hello request to renegotiate.
931 * 6. Client hello indicates TLS v1.0 as we now
932 * know that is maximum server supports.
933 * 7. Server chokes on RSA encrypted premaster secret
934 * containing version 1.0.
936 * For interoperability it should be OK to always use the
937 * maximum version we support in client hello and then rely
938 * on the checking of version to ensure the servers isn't
939 * being inconsistent: for example initially negotiating with
940 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
941 * client_version in client hello and not resetting it to
942 * the negotiated version.
944 * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
945 * supported_versions extension for the real supported versions.
947 if (!WPACKET_put_bytes_u16(pkt, s->client_version)
948 || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) {
949 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
957 sess_id_len = s->session->session_id_length;
958 if (sess_id_len > sizeof(s->session->session_id)
959 || !WPACKET_start_sub_packet_u8(pkt)
960 || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id,
962 || !WPACKET_close(pkt)) {
963 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
967 /* cookie stuff for DTLS */
968 if (SSL_IS_DTLS(s)) {
969 if (s->d1->cookie_len > sizeof(s->d1->cookie)
970 || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
971 s->d1->cookie_len)) {
972 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
977 /* Ciphers supported */
978 if (!WPACKET_start_sub_packet_u16(pkt)) {
979 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
982 /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */
983 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt))
985 if (!WPACKET_close(pkt)) {
986 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
991 if (!WPACKET_start_sub_packet_u8(pkt)) {
992 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
995 #ifndef OPENSSL_NO_COMP
996 if (ssl_allow_compression(s) && s->ctx->comp_methods) {
997 int compnum = sk_SSL_COMP_num(s->ctx->comp_methods);
998 for (i = 0; i < compnum; i++) {
999 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1000 if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
1001 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1007 /* Add the NULL method */
1008 if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
1009 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1013 /* TLS extensions */
1014 if (!tls_construct_extensions(s, pkt, EXT_CLIENT_HELLO, &al)) {
1015 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1016 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1023 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
1029 if (!PACKET_forward(pkt, 2)
1030 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1031 al = SSL_AD_DECODE_ERROR;
1032 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1036 cookie_len = PACKET_remaining(&cookiepkt);
1037 if (cookie_len > sizeof(s->d1->cookie)) {
1038 al = SSL_AD_ILLEGAL_PARAMETER;
1039 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1043 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1044 al = SSL_AD_DECODE_ERROR;
1045 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1048 s->d1->cookie_len = cookie_len;
1050 return MSG_PROCESS_FINISHED_READING;
1052 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1053 ossl_statem_set_error(s);
1054 return MSG_PROCESS_ERROR;
1057 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
1059 STACK_OF(SSL_CIPHER) *sk;
1060 const SSL_CIPHER *c;
1061 PACKET session_id, extpkt;
1062 size_t session_id_len;
1063 const unsigned char *cipherchars;
1064 int i, al = SSL_AD_INTERNAL_ERROR;
1065 unsigned int compression;
1066 unsigned int sversion;
1067 unsigned int context;
1069 RAW_EXTENSION *extensions = NULL;
1070 #ifndef OPENSSL_NO_COMP
1074 if (!PACKET_get_net_2(pkt, &sversion)) {
1075 al = SSL_AD_DECODE_ERROR;
1076 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1080 protverr = ssl_choose_client_version(s, sversion);
1081 if (protverr != 0) {
1082 al = SSL_AD_PROTOCOL_VERSION;
1083 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
1087 /* load the server hello data */
1088 /* load the server random */
1089 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1090 al = SSL_AD_DECODE_ERROR;
1091 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1097 /* Get the session-id. */
1098 if (!SSL_IS_TLS13(s)) {
1099 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1100 al = SSL_AD_DECODE_ERROR;
1101 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1104 session_id_len = PACKET_remaining(&session_id);
1105 if (session_id_len > sizeof s->session->session_id
1106 || session_id_len > SSL3_SESSION_ID_SIZE) {
1107 al = SSL_AD_ILLEGAL_PARAMETER;
1108 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1109 SSL_R_SSL3_SESSION_ID_TOO_LONG);
1113 PACKET_null_init(&session_id);
1117 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1118 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1119 al = SSL_AD_DECODE_ERROR;
1124 * Check if we can resume the session based on external pre-shared secret.
1125 * EAP-FAST (RFC 4851) supports two types of session resumption.
1126 * Resumption based on server-side state works with session IDs.
1127 * Resumption based on pre-shared Protected Access Credentials (PACs)
1128 * works by overriding the SessionTicket extension at the application
1129 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1130 * servers would honour the session ID.) Therefore, the session ID alone
1131 * is not a reliable indicator of session resumption, so we first check if
1132 * we can resume, and later peek at the next handshake message to see if the
1133 * server wants to resume.
1135 if (s->version >= TLS1_VERSION && !SSL_IS_TLS13(s)
1136 && s->tls_session_secret_cb != NULL && s->session->tlsext_tick) {
1137 const SSL_CIPHER *pref_cipher = NULL;
1139 * s->session->master_key_length is a size_t, but this is an int for
1140 * backwards compat reasons
1142 int master_key_length;
1143 master_key_length = sizeof(s->session->master_key);
1144 if (s->tls_session_secret_cb(s, s->session->master_key,
1147 s->tls_session_secret_cb_arg)
1148 && master_key_length > 0) {
1149 s->session->master_key_length = master_key_length;
1150 s->session->cipher = pref_cipher ?
1151 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1153 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1154 al = SSL_AD_INTERNAL_ERROR;
1159 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1160 && memcmp(PACKET_data(&session_id), s->session->session_id,
1161 session_id_len) == 0) {
1162 if (s->sid_ctx_length != s->session->sid_ctx_length
1163 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1164 /* actually a client application bug */
1165 al = SSL_AD_ILLEGAL_PARAMETER;
1166 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1167 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1173 * If we were trying for session-id reuse but the server
1174 * didn't echo the ID, make a new SSL_SESSION.
1175 * In the case of EAP-FAST and PAC, we do not send a session ID,
1176 * so the PAC-based session secret is always preserved. It'll be
1177 * overwritten if the server refuses resumption.
1179 if (s->session->session_id_length > 0) {
1180 s->ctx->stats.sess_miss++;
1181 if (!ssl_get_new_session(s, 0)) {
1186 s->session->ssl_version = s->version;
1187 s->session->session_id_length = session_id_len;
1188 /* session_id_len could be 0 */
1189 memcpy(s->session->session_id, PACKET_data(&session_id),
1193 /* Session version and negotiated protocol version should match */
1194 if (s->version != s->session->ssl_version) {
1195 al = SSL_AD_PROTOCOL_VERSION;
1197 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1198 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1202 c = ssl_get_cipher_by_char(s, cipherchars);
1204 /* unknown cipher */
1205 al = SSL_AD_ILLEGAL_PARAMETER;
1206 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1210 * Now that we know the version, update the check to see if it's an allowed
1213 s->s3->tmp.min_ver = s->version;
1214 s->s3->tmp.max_ver = s->version;
1216 * If it is a disabled cipher we either didn't send it in client hello,
1217 * or it's not allowed for the selected protocol. So we return an error.
1219 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1220 al = SSL_AD_ILLEGAL_PARAMETER;
1221 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1225 sk = ssl_get_ciphers_by_id(s);
1226 i = sk_SSL_CIPHER_find(sk, c);
1228 /* we did not say we would use this cipher */
1229 al = SSL_AD_ILLEGAL_PARAMETER;
1230 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1235 * Depending on the session caching (internal/external), the cipher
1236 * and/or cipher_id values may not be set. Make sure that cipher_id is
1237 * set and use it for comparison.
1239 if (s->session->cipher)
1240 s->session->cipher_id = s->session->cipher->id;
1241 if (s->hit && (s->session->cipher_id != c->id)) {
1242 al = SSL_AD_ILLEGAL_PARAMETER;
1243 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1244 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1247 s->s3->tmp.new_cipher = c;
1248 /* lets get the compression algorithm */
1250 if (!SSL_IS_TLS13(s)) {
1251 if (!PACKET_get_1(pkt, &compression)) {
1252 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1253 al = SSL_AD_DECODE_ERROR;
1260 #ifdef OPENSSL_NO_COMP
1261 if (compression != 0) {
1262 al = SSL_AD_ILLEGAL_PARAMETER;
1263 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1264 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1268 * If compression is disabled we'd better not try to resume a session
1269 * using compression.
1271 if (s->session->compress_meth != 0) {
1272 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1276 if (s->hit && compression != s->session->compress_meth) {
1277 al = SSL_AD_ILLEGAL_PARAMETER;
1278 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1279 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1282 if (compression == 0)
1284 else if (!ssl_allow_compression(s)) {
1285 al = SSL_AD_ILLEGAL_PARAMETER;
1286 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1289 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1292 if (compression != 0 && comp == NULL) {
1293 al = SSL_AD_ILLEGAL_PARAMETER;
1294 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1295 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1298 s->s3->tmp.new_compression = comp;
1302 /* TLS extensions */
1303 if (PACKET_remaining(pkt) == 0) {
1304 PACKET_null_init(&extpkt);
1305 } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)) {
1306 al = SSL_AD_DECODE_ERROR;
1307 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_LENGTH);
1311 context = SSL_IS_TLS13(s) ? EXT_TLS1_3_SERVER_HELLO
1312 : EXT_TLS1_2_SERVER_HELLO;
1313 if (!tls_collect_extensions(s, &extpkt, context, &extensions, &al)
1314 || !tls_parse_all_extensions(s, context, extensions, &al))
1317 #ifndef OPENSSL_NO_SCTP
1318 if (SSL_IS_DTLS(s) && s->hit) {
1319 unsigned char sctpauthkey[64];
1320 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1323 * Add new shared key for SCTP-Auth, will be ignored if
1326 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1327 sizeof(DTLS1_SCTP_AUTH_LABEL));
1329 if (SSL_export_keying_material(s, sctpauthkey,
1330 sizeof(sctpauthkey),
1332 sizeof(labelbuffer), NULL, 0, 0) <= 0)
1335 BIO_ctrl(SSL_get_wbio(s),
1336 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1337 sizeof(sctpauthkey), sctpauthkey);
1342 * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1343 * we're done with this message
1346 && (!s->method->ssl3_enc->setup_key_block(s)
1347 || !s->method->ssl3_enc->change_cipher_state(s,
1348 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)
1349 || !s->method->ssl3_enc->change_cipher_state(s,
1350 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) {
1351 al = SSL_AD_INTERNAL_ERROR;
1352 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_CANNOT_CHANGE_CIPHER);
1356 OPENSSL_free(extensions);
1357 return MSG_PROCESS_CONTINUE_READING;
1359 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1360 ossl_statem_set_error(s);
1361 OPENSSL_free(extensions);
1362 return MSG_PROCESS_ERROR;
1365 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1367 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1368 unsigned long cert_list_len, cert_len;
1370 const unsigned char *certstart, *certbytes;
1371 STACK_OF(X509) *sk = NULL;
1372 EVP_PKEY *pkey = NULL;
1374 if ((sk = sk_X509_new_null()) == NULL) {
1375 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1379 if (!PACKET_get_net_3(pkt, &cert_list_len)
1380 || PACKET_remaining(pkt) != cert_list_len) {
1381 al = SSL_AD_DECODE_ERROR;
1382 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1385 while (PACKET_remaining(pkt)) {
1386 if (!PACKET_get_net_3(pkt, &cert_len)
1387 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1388 al = SSL_AD_DECODE_ERROR;
1389 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1390 SSL_R_CERT_LENGTH_MISMATCH);
1394 certstart = certbytes;
1395 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1397 al = SSL_AD_BAD_CERTIFICATE;
1398 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1401 if (certbytes != (certstart + cert_len)) {
1402 al = SSL_AD_DECODE_ERROR;
1403 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1404 SSL_R_CERT_LENGTH_MISMATCH);
1407 if (!sk_X509_push(sk, x)) {
1408 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1414 i = ssl_verify_cert_chain(s, sk);
1416 * The documented interface is that SSL_VERIFY_PEER should be set in order
1417 * for client side verification of the server certificate to take place.
1418 * However, historically the code has only checked that *any* flag is set
1419 * to cause server verification to take place. Use of the other flags makes
1420 * no sense in client mode. An attempt to clean up the semantics was
1421 * reverted because at least one application *only* set
1422 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1423 * server verification to take place, after the clean up it silently did
1424 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1425 * sent to them because they are void functions. Therefore, we now use the
1426 * (less clean) historic behaviour of performing validation if any flag is
1427 * set. The *documented* interface remains the same.
1429 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1430 al = ssl_verify_alarm_type(s->verify_result);
1431 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1432 SSL_R_CERTIFICATE_VERIFY_FAILED);
1435 ERR_clear_error(); /* but we keep s->verify_result */
1437 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1438 al = SSL_AD_HANDSHAKE_FAILURE;
1442 s->session->peer_chain = sk;
1444 * Inconsistency alert: cert_chain does include the peer's certificate,
1445 * which we don't include in statem_srvr.c
1447 x = sk_X509_value(sk, 0);
1450 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1453 pkey = X509_get0_pubkey(x);
1455 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1458 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1459 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1463 i = ssl_cert_type(x, pkey);
1467 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1468 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1472 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1473 if (exp_idx >= 0 && i != exp_idx
1474 && (exp_idx != SSL_PKEY_GOST_EC ||
1475 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1476 && i != SSL_PKEY_GOST01))) {
1478 al = SSL_AD_ILLEGAL_PARAMETER;
1479 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1480 SSL_R_WRONG_CERTIFICATE_TYPE);
1483 s->session->peer_type = i;
1485 X509_free(s->session->peer);
1487 s->session->peer = x;
1488 s->session->verify_result = s->verify_result;
1491 ret = MSG_PROCESS_CONTINUE_READING;
1495 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1497 ossl_statem_set_error(s);
1500 sk_X509_pop_free(sk, X509_free);
1504 static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
1506 #ifndef OPENSSL_NO_PSK
1507 PACKET psk_identity_hint;
1509 /* PSK ciphersuites are preceded by an identity hint */
1511 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1512 *al = SSL_AD_DECODE_ERROR;
1513 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
1518 * Store PSK identity hint for later use, hint is used in
1519 * tls_construct_client_key_exchange. Assume that the maximum length of
1520 * a PSK identity hint can be as long as the maximum length of a PSK
1523 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1524 *al = SSL_AD_HANDSHAKE_FAILURE;
1525 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
1529 if (PACKET_remaining(&psk_identity_hint) == 0) {
1530 OPENSSL_free(s->session->psk_identity_hint);
1531 s->session->psk_identity_hint = NULL;
1532 } else if (!PACKET_strndup(&psk_identity_hint,
1533 &s->session->psk_identity_hint)) {
1534 *al = SSL_AD_INTERNAL_ERROR;
1540 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
1541 *al = SSL_AD_INTERNAL_ERROR;
1546 static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1548 #ifndef OPENSSL_NO_SRP
1549 PACKET prime, generator, salt, server_pub;
1551 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1552 || !PACKET_get_length_prefixed_2(pkt, &generator)
1553 || !PACKET_get_length_prefixed_1(pkt, &salt)
1554 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1555 *al = SSL_AD_DECODE_ERROR;
1556 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
1560 /* TODO(size_t): Convert BN_bin2bn() calls */
1562 BN_bin2bn(PACKET_data(&prime),
1563 (int)PACKET_remaining(&prime), NULL)) == NULL
1565 BN_bin2bn(PACKET_data(&generator),
1566 (int)PACKET_remaining(&generator), NULL)) == NULL
1568 BN_bin2bn(PACKET_data(&salt),
1569 (int)PACKET_remaining(&salt), NULL)) == NULL
1571 BN_bin2bn(PACKET_data(&server_pub),
1572 (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
1573 *al = SSL_AD_INTERNAL_ERROR;
1574 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
1578 if (!srp_verify_server_param(s, al)) {
1579 *al = SSL_AD_DECODE_ERROR;
1580 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
1584 /* We must check if there is a certificate */
1585 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1586 *pkey = X509_get0_pubkey(s->session->peer);
1590 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
1591 *al = SSL_AD_INTERNAL_ERROR;
1596 static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1598 #ifndef OPENSSL_NO_DH
1599 PACKET prime, generator, pub_key;
1600 EVP_PKEY *peer_tmp = NULL;
1603 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1605 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1606 || !PACKET_get_length_prefixed_2(pkt, &generator)
1607 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1608 *al = SSL_AD_DECODE_ERROR;
1609 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
1613 peer_tmp = EVP_PKEY_new();
1616 if (peer_tmp == NULL || dh == NULL) {
1617 *al = SSL_AD_INTERNAL_ERROR;
1618 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
1622 /* TODO(size_t): Convert these calls */
1623 p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
1624 g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
1626 bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
1627 (int)PACKET_remaining(&pub_key), NULL);
1628 if (p == NULL || g == NULL || bnpub_key == NULL) {
1629 *al = SSL_AD_INTERNAL_ERROR;
1630 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1634 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1635 *al = SSL_AD_DECODE_ERROR;
1636 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
1640 if (!DH_set0_pqg(dh, p, NULL, g)) {
1641 *al = SSL_AD_INTERNAL_ERROR;
1642 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1647 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1648 *al = SSL_AD_INTERNAL_ERROR;
1649 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1654 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1655 *al = SSL_AD_HANDSHAKE_FAILURE;
1656 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
1660 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1661 *al = SSL_AD_INTERNAL_ERROR;
1662 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
1666 s->s3->peer_tmp = peer_tmp;
1669 * FIXME: This makes assumptions about which ciphersuites come with
1670 * public keys. We should have a less ad-hoc way of doing this
1672 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1673 *pkey = X509_get0_pubkey(s->session->peer);
1674 /* else anonymous DH, so no certificate or pkey. */
1683 EVP_PKEY_free(peer_tmp);
1687 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
1688 *al = SSL_AD_INTERNAL_ERROR;
1693 static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1695 #ifndef OPENSSL_NO_EC
1697 const unsigned char *ecparams;
1699 unsigned int curve_flags;
1700 EVP_PKEY_CTX *pctx = NULL;
1703 * Extract elliptic curve parameters and the server's ephemeral ECDH
1704 * public key. For now we only support named (not generic) curves and
1705 * ECParameters in this case is just three bytes.
1707 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1708 *al = SSL_AD_DECODE_ERROR;
1709 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
1713 * Check curve is one of our preferences, if not server has sent an
1714 * invalid curve. ECParameters is 3 bytes.
1716 if (!tls1_check_curve(s, ecparams, 3)) {
1717 *al = SSL_AD_DECODE_ERROR;
1718 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
1722 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
1724 if (curve_nid == 0) {
1725 *al = SSL_AD_INTERNAL_ERROR;
1726 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
1727 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1731 if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
1732 EVP_PKEY *key = EVP_PKEY_new();
1734 if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
1735 *al = SSL_AD_INTERNAL_ERROR;
1736 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1740 s->s3->peer_tmp = key;
1742 /* Set up EVP_PKEY with named curve as parameters */
1743 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1745 || EVP_PKEY_paramgen_init(pctx) <= 0
1746 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1747 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1748 *al = SSL_AD_INTERNAL_ERROR;
1749 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1750 EVP_PKEY_CTX_free(pctx);
1753 EVP_PKEY_CTX_free(pctx);
1757 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1758 *al = SSL_AD_DECODE_ERROR;
1759 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
1763 if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
1764 PACKET_data(&encoded_pt),
1765 PACKET_remaining(&encoded_pt))) {
1766 *al = SSL_AD_DECODE_ERROR;
1767 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
1772 * The ECC/TLS specification does not mention the use of DSA to sign
1773 * ECParameters in the server key exchange message. We do support RSA
1776 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
1777 *pkey = X509_get0_pubkey(s->session->peer);
1778 else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
1779 *pkey = X509_get0_pubkey(s->session->peer);
1780 /* else anonymous ECDH, so no certificate or pkey. */
1784 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
1785 *al = SSL_AD_INTERNAL_ERROR;
1790 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1794 EVP_PKEY *pkey = NULL;
1795 PACKET save_param_start, signature;
1797 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1799 save_param_start = *pkt;
1801 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1802 EVP_PKEY_free(s->s3->peer_tmp);
1803 s->s3->peer_tmp = NULL;
1806 if (alg_k & SSL_PSK) {
1807 if (!tls_process_ske_psk_preamble(s, pkt, &al))
1811 /* Nothing else to do for plain PSK or RSAPSK */
1812 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1813 } else if (alg_k & SSL_kSRP) {
1814 if (!tls_process_ske_srp(s, pkt, &pkey, &al))
1816 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1817 if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
1819 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1820 if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
1823 al = SSL_AD_UNEXPECTED_MESSAGE;
1824 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1828 /* if it was signed, check the signature */
1832 const EVP_MD *md = NULL;
1836 * |pkt| now points to the beginning of the signature, so the difference
1837 * equals the length of the parameters.
1839 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1840 PACKET_remaining(&save_param_start) -
1841 PACKET_remaining(pkt))) {
1842 al = SSL_AD_INTERNAL_ERROR;
1843 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1847 if (SSL_USE_SIGALGS(s)) {
1848 const unsigned char *sigalgs;
1850 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1851 al = SSL_AD_DECODE_ERROR;
1852 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1855 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1857 al = SSL_AD_INTERNAL_ERROR;
1859 } else if (rv == 0) {
1860 al = SSL_AD_DECODE_ERROR;
1864 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1866 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1867 md = EVP_md5_sha1();
1872 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1873 || PACKET_remaining(pkt) != 0) {
1874 al = SSL_AD_DECODE_ERROR;
1875 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1878 maxsig = EVP_PKEY_size(pkey);
1880 al = SSL_AD_INTERNAL_ERROR;
1881 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1886 * Check signature length
1888 if (PACKET_remaining(&signature) > (size_t)maxsig) {
1889 /* wrong packet length */
1890 al = SSL_AD_DECODE_ERROR;
1891 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1892 SSL_R_WRONG_SIGNATURE_LENGTH);
1896 md_ctx = EVP_MD_CTX_new();
1897 if (md_ctx == NULL) {
1898 al = SSL_AD_INTERNAL_ERROR;
1899 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1903 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1904 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1905 SSL3_RANDOM_SIZE) <= 0
1906 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1907 SSL3_RANDOM_SIZE) <= 0
1908 || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms),
1909 PACKET_remaining(¶ms)) <= 0) {
1910 EVP_MD_CTX_free(md_ctx);
1911 al = SSL_AD_INTERNAL_ERROR;
1912 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1915 /* TODO(size_t): Convert this call */
1916 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1917 (unsigned int)PACKET_remaining(&signature),
1920 EVP_MD_CTX_free(md_ctx);
1921 al = SSL_AD_DECRYPT_ERROR;
1922 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1925 EVP_MD_CTX_free(md_ctx);
1927 /* aNULL, aSRP or PSK do not need public keys */
1928 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1929 && !(alg_k & SSL_PSK)) {
1930 /* Might be wrong key type, check it */
1931 if (ssl3_check_cert_and_algorithm(s)) {
1932 /* Otherwise this shouldn't happen */
1933 al = SSL_AD_INTERNAL_ERROR;
1934 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1936 al = SSL_AD_DECODE_ERROR;
1940 /* still data left over */
1941 if (PACKET_remaining(pkt) != 0) {
1942 al = SSL_AD_DECODE_ERROR;
1943 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1948 return MSG_PROCESS_CONTINUE_READING;
1951 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1952 ossl_statem_set_error(s);
1953 return MSG_PROCESS_ERROR;
1956 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1958 int ret = MSG_PROCESS_ERROR;
1959 unsigned int list_len, ctype_num, i, name_len;
1960 X509_NAME *xn = NULL;
1961 const unsigned char *data;
1962 const unsigned char *namestart, *namebytes;
1963 STACK_OF(X509_NAME) *ca_sk = NULL;
1965 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1966 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1970 /* get the certificate types */
1971 if (!PACKET_get_1(pkt, &ctype_num)
1972 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1973 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1974 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1977 OPENSSL_free(s->cert->ctypes);
1978 s->cert->ctypes = NULL;
1979 if (ctype_num > SSL3_CT_NUMBER) {
1980 /* If we exceed static buffer copy all to cert structure */
1981 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1982 if (s->cert->ctypes == NULL) {
1983 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1986 memcpy(s->cert->ctypes, data, ctype_num);
1987 s->cert->ctype_num = ctype_num;
1988 ctype_num = SSL3_CT_NUMBER;
1990 for (i = 0; i < ctype_num; i++)
1991 s->s3->tmp.ctype[i] = data[i];
1993 if (SSL_USE_SIGALGS(s)) {
1994 if (!PACKET_get_net_2(pkt, &list_len)
1995 || !PACKET_get_bytes(pkt, &data, list_len)) {
1996 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1997 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1998 SSL_R_LENGTH_MISMATCH);
2002 /* Clear certificate digests and validity flags */
2003 for (i = 0; i < SSL_PKEY_NUM; i++) {
2004 s->s3->tmp.md[i] = NULL;
2005 s->s3->tmp.valid_flags[i] = 0;
2007 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2008 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2009 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2010 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2013 if (!tls1_process_sigalgs(s)) {
2014 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2015 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2019 ssl_set_default_md(s);
2022 /* get the CA RDNs */
2023 if (!PACKET_get_net_2(pkt, &list_len)
2024 || PACKET_remaining(pkt) != list_len) {
2025 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2026 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2030 while (PACKET_remaining(pkt)) {
2031 if (!PACKET_get_net_2(pkt, &name_len)
2032 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
2033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2034 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2035 SSL_R_LENGTH_MISMATCH);
2039 namestart = namebytes;
2041 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2042 name_len)) == NULL) {
2043 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2044 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2048 if (namebytes != (namestart + name_len)) {
2049 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2050 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2051 SSL_R_CA_DN_LENGTH_MISMATCH);
2054 if (!sk_X509_NAME_push(ca_sk, xn)) {
2055 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2061 /* we should setup a certificate to return.... */
2062 s->s3->tmp.cert_req = 1;
2063 s->s3->tmp.ctype_num = ctype_num;
2064 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2065 s->s3->tmp.ca_names = ca_sk;
2068 ret = MSG_PROCESS_CONTINUE_PROCESSING;
2071 ossl_statem_set_error(s);
2074 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2078 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2080 return (X509_NAME_cmp(*a, *b));
2083 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
2086 unsigned int ticklen;
2087 unsigned long ticket_lifetime_hint;
2088 unsigned int sess_len;
2090 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2091 || !PACKET_get_net_2(pkt, &ticklen)
2092 || PACKET_remaining(pkt) != ticklen) {
2093 al = SSL_AD_DECODE_ERROR;
2094 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2098 /* Server is allowed to change its mind and send an empty ticket. */
2100 return MSG_PROCESS_CONTINUE_READING;
2102 if (s->session->session_id_length > 0) {
2103 int i = s->session_ctx->session_cache_mode;
2104 SSL_SESSION *new_sess;
2106 * We reused an existing session, so we need to replace it with a new
2109 if (i & SSL_SESS_CACHE_CLIENT) {
2111 * Remove the old session from the cache. We carry on if this fails
2113 SSL_CTX_remove_session(s->session_ctx, s->session);
2116 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2117 al = SSL_AD_INTERNAL_ERROR;
2118 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2122 SSL_SESSION_free(s->session);
2123 s->session = new_sess;
2126 OPENSSL_free(s->session->tlsext_tick);
2127 s->session->tlsext_ticklen = 0;
2129 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2130 if (s->session->tlsext_tick == NULL) {
2131 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2134 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
2135 al = SSL_AD_DECODE_ERROR;
2136 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2140 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2141 s->session->tlsext_ticklen = ticklen;
2143 * There are two ways to detect a resumed ticket session. One is to set
2144 * an appropriate session ID and then the server must return a match in
2145 * ServerHello. This allows the normal client session ID matching to work
2146 * and we know much earlier that the ticket has been accepted. The
2147 * other way is to set zero length session ID when the ticket is
2148 * presented and rely on the handshake to determine session resumption.
2149 * We choose the former approach because this fits in with assumptions
2150 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2151 * SHA256 is disabled) hash of the ticket.
2154 * TODO(size_t): we use sess_len here because EVP_Digest expects an int
2155 * but s->session->session_id_length is a size_t
2157 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
2158 s->session->session_id, &sess_len,
2159 EVP_sha256(), NULL)) {
2160 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
2163 s->session->session_id_length = sess_len;
2164 return MSG_PROCESS_CONTINUE_READING;
2166 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2168 ossl_statem_set_error(s);
2169 return MSG_PROCESS_ERROR;
2172 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
2178 if (!PACKET_get_1(pkt, &type)
2179 || type != TLSEXT_STATUSTYPE_ocsp) {
2180 al = SSL_AD_DECODE_ERROR;
2181 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2184 if (!PACKET_get_net_3_len(pkt, &resplen)
2185 || PACKET_remaining(pkt) != resplen) {
2186 al = SSL_AD_DECODE_ERROR;
2187 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2190 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2191 if (s->tlsext_ocsp_resp == NULL) {
2192 al = SSL_AD_INTERNAL_ERROR;
2193 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2196 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
2197 al = SSL_AD_DECODE_ERROR;
2198 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2201 s->tlsext_ocsp_resplen = resplen;
2202 return MSG_PROCESS_CONTINUE_READING;
2204 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2205 ossl_statem_set_error(s);
2206 return MSG_PROCESS_ERROR;
2210 * Perform miscellaneous checks and processing after we have received the
2211 * server's initial flight. In TLS1.3 this is after the Server Finished message.
2212 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
2215 int tls_process_initial_server_flight(SSL *s, int *al)
2218 * at this point we check that we have the required stuff from
2221 if (!ssl3_check_cert_and_algorithm(s)) {
2222 *al = SSL_AD_HANDSHAKE_FAILURE;
2227 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2228 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2229 * message, or NULL and -1 otherwise
2231 if (s->tlsext_status_type != TLSEXT_STATUSTYPE_nothing
2232 && s->ctx->tlsext_status_cb != NULL) {
2234 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2236 *al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2237 SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
2238 SSL_R_INVALID_STATUS_RESPONSE);
2242 *al = SSL_AD_INTERNAL_ERROR;
2243 SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
2244 ERR_R_MALLOC_FAILURE);
2248 #ifndef OPENSSL_NO_CT
2249 if (s->ct_validation_callback != NULL) {
2250 /* Note we validate the SCTs whether or not we abort on error */
2251 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2252 *al = SSL_AD_HANDSHAKE_FAILURE;
2261 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2263 int al = SSL_AD_INTERNAL_ERROR;
2265 if (PACKET_remaining(pkt) > 0) {
2266 /* should contain no data */
2267 al = SSL_AD_DECODE_ERROR;
2268 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2271 #ifndef OPENSSL_NO_SRP
2272 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2273 if (SRP_Calc_A_param(s) <= 0) {
2274 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2281 * Error queue messages are generated directly by this function
2283 if (!tls_process_initial_server_flight(s, &al))
2286 #ifndef OPENSSL_NO_SCTP
2287 /* Only applies to renegotiation */
2288 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2289 && s->renegotiate != 0)
2290 return MSG_PROCESS_CONTINUE_PROCESSING;
2293 return MSG_PROCESS_FINISHED_READING;
2296 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2297 ossl_statem_set_error(s);
2298 return MSG_PROCESS_ERROR;
2301 static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al)
2303 #ifndef OPENSSL_NO_PSK
2306 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2307 * \0-terminated identity. The last byte is for us for simulating
2310 char identity[PSK_MAX_IDENTITY_LEN + 1];
2311 size_t identitylen = 0;
2312 unsigned char psk[PSK_MAX_PSK_LEN];
2313 unsigned char *tmppsk = NULL;
2314 char *tmpidentity = NULL;
2317 if (s->psk_client_callback == NULL) {
2318 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
2319 *al = SSL_AD_INTERNAL_ERROR;
2323 memset(identity, 0, sizeof(identity));
2325 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2326 identity, sizeof(identity) - 1,
2329 if (psklen > PSK_MAX_PSK_LEN) {
2330 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2331 *al = SSL_AD_HANDSHAKE_FAILURE;
2333 } else if (psklen == 0) {
2334 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
2335 SSL_R_PSK_IDENTITY_NOT_FOUND);
2336 *al = SSL_AD_HANDSHAKE_FAILURE;
2340 identitylen = strlen(identity);
2341 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2342 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2343 *al = SSL_AD_HANDSHAKE_FAILURE;
2347 tmppsk = OPENSSL_memdup(psk, psklen);
2348 tmpidentity = OPENSSL_strdup(identity);
2349 if (tmppsk == NULL || tmpidentity == NULL) {
2350 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2351 *al = SSL_AD_INTERNAL_ERROR;
2355 OPENSSL_free(s->s3->tmp.psk);
2356 s->s3->tmp.psk = tmppsk;
2357 s->s3->tmp.psklen = psklen;
2359 OPENSSL_free(s->session->psk_identity);
2360 s->session->psk_identity = tmpidentity;
2363 if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
2364 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2365 *al = SSL_AD_INTERNAL_ERROR;
2372 OPENSSL_cleanse(psk, psklen);
2373 OPENSSL_cleanse(identity, sizeof(identity));
2374 OPENSSL_clear_free(tmppsk, psklen);
2375 OPENSSL_clear_free(tmpidentity, identitylen);
2379 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2380 *al = SSL_AD_INTERNAL_ERROR;
2385 static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)
2387 #ifndef OPENSSL_NO_RSA
2388 unsigned char *encdata = NULL;
2389 EVP_PKEY *pkey = NULL;
2390 EVP_PKEY_CTX *pctx = NULL;
2392 unsigned char *pms = NULL;
2395 if (s->session->peer == NULL) {
2397 * We should always have a server certificate with SSL_kRSA.
2399 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2403 pkey = X509_get0_pubkey(s->session->peer);
2404 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2405 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2409 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2410 pms = OPENSSL_malloc(pmslen);
2412 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
2413 *al = SSL_AD_INTERNAL_ERROR;
2417 pms[0] = s->client_version >> 8;
2418 pms[1] = s->client_version & 0xff;
2419 /* TODO(size_t): Convert this function */
2420 if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
2424 /* Fix buf for TLS and beyond */
2425 if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
2426 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2429 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2430 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2431 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2432 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
2435 if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
2436 || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
2437 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
2440 EVP_PKEY_CTX_free(pctx);
2443 if (s->options & SSL_OP_PKCS1_CHECK_1)
2445 if (s->options & SSL_OP_PKCS1_CHECK_2)
2449 /* Fix buf for TLS and beyond */
2450 if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
2451 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2455 s->s3->tmp.pms = pms;
2456 s->s3->tmp.pmslen = pmslen;
2460 OPENSSL_clear_free(pms, pmslen);
2461 EVP_PKEY_CTX_free(pctx);
2465 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2466 *al = SSL_AD_INTERNAL_ERROR;
2471 static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al)
2473 #ifndef OPENSSL_NO_DH
2475 const BIGNUM *pub_key;
2476 EVP_PKEY *ckey = NULL, *skey = NULL;
2477 unsigned char *keybytes = NULL;
2479 skey = s->s3->peer_tmp;
2483 ckey = ssl_generate_pkey(skey);
2487 dh_clnt = EVP_PKEY_get0_DH(ckey);
2489 if (dh_clnt == NULL || ssl_derive(s, ckey, skey, 0) == 0)
2492 /* send off the data */
2493 DH_get0_key(dh_clnt, &pub_key, NULL);
2494 if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes))
2497 BN_bn2bin(pub_key, keybytes);
2498 EVP_PKEY_free(ckey);
2502 EVP_PKEY_free(ckey);
2504 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2505 *al = SSL_AD_INTERNAL_ERROR;
2509 static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al)
2511 #ifndef OPENSSL_NO_EC
2512 unsigned char *encodedPoint = NULL;
2513 size_t encoded_pt_len = 0;
2514 EVP_PKEY *ckey = NULL, *skey = NULL;
2517 skey = s->s3->peer_tmp;
2519 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2523 ckey = ssl_generate_pkey(skey);
2525 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_MALLOC_FAILURE);
2529 if (ssl_derive(s, ckey, skey, 0) == 0) {
2530 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
2534 /* Generate encoding of client key */
2535 encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
2537 if (encoded_pt_len == 0) {
2538 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
2542 if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
2543 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2549 OPENSSL_free(encodedPoint);
2550 EVP_PKEY_free(ckey);
2553 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2554 *al = SSL_AD_INTERNAL_ERROR;
2559 static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al)
2561 #ifndef OPENSSL_NO_GOST
2562 /* GOST key exchange message creation */
2563 EVP_PKEY_CTX *pkey_ctx = NULL;
2566 unsigned int md_len;
2567 unsigned char shared_ukm[32], tmp[256];
2568 EVP_MD_CTX *ukm_hash = NULL;
2569 int dgst_nid = NID_id_GostR3411_94;
2570 unsigned char *pms = NULL;
2573 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2574 dgst_nid = NID_id_GostR3411_2012_256;
2577 * Get server sertificate PKEY and create ctx from it
2579 peer_cert = s->session->peer;
2581 *al = SSL_AD_HANDSHAKE_FAILURE;
2582 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
2583 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2587 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2588 if (pkey_ctx == NULL) {
2589 *al = SSL_AD_INTERNAL_ERROR;
2590 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2594 * If we have send a certificate, and certificate key
2595 * parameters match those of server certificate, use
2596 * certificate key for key exchange
2599 /* Otherwise, generate ephemeral key pair */
2601 pms = OPENSSL_malloc(pmslen);
2603 *al = SSL_AD_INTERNAL_ERROR;
2604 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2608 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2609 /* Generate session key
2610 * TODO(size_t): Convert this function
2612 || RAND_bytes(pms, (int)pmslen) <= 0) {
2613 *al = SSL_AD_INTERNAL_ERROR;
2614 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2618 * Compute shared IV and store it in algorithm-specific context
2621 ukm_hash = EVP_MD_CTX_new();
2622 if (ukm_hash == NULL
2623 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
2624 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2625 SSL3_RANDOM_SIZE) <= 0
2626 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2627 SSL3_RANDOM_SIZE) <= 0
2628 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2629 *al = SSL_AD_INTERNAL_ERROR;
2630 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2633 EVP_MD_CTX_free(ukm_hash);
2635 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2636 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2637 *al = SSL_AD_INTERNAL_ERROR;
2638 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2641 /* Make GOST keytransport blob message */
2643 * Encapsulate it into sequence
2646 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2647 *al = SSL_AD_INTERNAL_ERROR;
2648 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2652 if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
2653 || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
2654 || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
2655 *al = SSL_AD_INTERNAL_ERROR;
2656 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2660 EVP_PKEY_CTX_free(pkey_ctx);
2661 s->s3->tmp.pms = pms;
2662 s->s3->tmp.pmslen = pmslen;
2666 EVP_PKEY_CTX_free(pkey_ctx);
2667 OPENSSL_clear_free(pms, pmslen);
2668 EVP_MD_CTX_free(ukm_hash);
2671 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2672 *al = SSL_AD_INTERNAL_ERROR;
2677 static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al)
2679 #ifndef OPENSSL_NO_SRP
2680 unsigned char *abytes = NULL;
2682 if (s->srp_ctx.A == NULL
2683 || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
2685 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2688 BN_bn2bin(s->srp_ctx.A, abytes);
2690 OPENSSL_free(s->session->srp_username);
2691 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2692 if (s->session->srp_username == NULL) {
2693 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
2699 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2700 *al = SSL_AD_INTERNAL_ERROR;
2705 int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
2707 unsigned long alg_k;
2710 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2712 if ((alg_k & SSL_PSK)
2713 && !tls_construct_cke_psk_preamble(s, pkt, &al))
2716 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2717 if (!tls_construct_cke_rsa(s, pkt, &al))
2719 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2720 if (!tls_construct_cke_dhe(s, pkt, &al))
2722 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2723 if (!tls_construct_cke_ecdhe(s, pkt, &al))
2725 } else if (alg_k & SSL_kGOST) {
2726 if (!tls_construct_cke_gost(s, pkt, &al))
2728 } else if (alg_k & SSL_kSRP) {
2729 if (!tls_construct_cke_srp(s, pkt, &al))
2731 } else if (!(alg_k & SSL_kPSK)) {
2732 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2733 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2740 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2741 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
2742 s->s3->tmp.pms = NULL;
2743 #ifndef OPENSSL_NO_PSK
2744 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2745 s->s3->tmp.psk = NULL;
2750 int tls_client_key_exchange_post_work(SSL *s)
2752 unsigned char *pms = NULL;
2755 pms = s->s3->tmp.pms;
2756 pmslen = s->s3->tmp.pmslen;
2758 #ifndef OPENSSL_NO_SRP
2760 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2761 if (!srp_generate_client_master_secret(s)) {
2762 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2763 ERR_R_INTERNAL_ERROR);
2770 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2771 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2772 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2775 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2776 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2777 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2778 /* ssl_generate_master_secret frees the pms even on error */
2786 #ifndef OPENSSL_NO_SCTP
2787 if (SSL_IS_DTLS(s)) {
2788 unsigned char sctpauthkey[64];
2789 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2792 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2795 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2796 sizeof(DTLS1_SCTP_AUTH_LABEL));
2798 if (SSL_export_keying_material(s, sctpauthkey,
2799 sizeof(sctpauthkey), labelbuffer,
2800 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2803 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2804 sizeof(sctpauthkey), sctpauthkey);
2810 OPENSSL_clear_free(pms, pmslen);
2811 s->s3->tmp.pms = NULL;
2815 int tls_construct_client_verify(SSL *s, WPACKET *pkt)
2818 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2819 EVP_MD_CTX *mctx = NULL;
2823 unsigned char *sig = NULL;
2825 mctx = EVP_MD_CTX_new();
2827 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2830 pkey = s->cert->key->privatekey;
2832 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2833 if (hdatalen <= 0) {
2834 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2838 if (SSL_USE_SIGALGS(s)&& !tls12_get_sigandhash(pkt, pkey, md)) {
2839 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2843 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2845 sig = OPENSSL_malloc(EVP_PKEY_size(pkey));
2847 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2850 if (!EVP_SignInit_ex(mctx, md, NULL)
2851 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2852 || (s->version == SSL3_VERSION
2853 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2854 (int)s->session->master_key_length,
2855 s->session->master_key))
2856 || !EVP_SignFinal(mctx, sig, &u, pkey)) {
2857 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2860 #ifndef OPENSSL_NO_GOST
2862 int pktype = EVP_PKEY_id(pkey);
2863 if (pktype == NID_id_GostR3410_2001
2864 || pktype == NID_id_GostR3410_2012_256
2865 || pktype == NID_id_GostR3410_2012_512)
2866 BUF_reverse(sig, NULL, u);
2870 if (!WPACKET_sub_memcpy_u16(pkt, sig, u)) {
2871 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2875 /* Digest cached records and discard handshake buffer */
2876 if (!ssl3_digest_cached_records(s, 0))
2880 EVP_MD_CTX_free(mctx);
2884 EVP_MD_CTX_free(mctx);
2885 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2890 * Check a certificate can be used for client authentication. Currently check
2891 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2892 * certificates can be used and optionally checks suitability for Suite B.
2894 static int ssl3_check_client_certificate(SSL *s)
2896 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2898 /* If no suitable signature algorithm can't use certificate */
2899 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2902 * If strict mode check suitability of chain before using it. This also
2903 * adjusts suite B digest if necessary.
2905 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2906 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2911 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2914 EVP_PKEY *pkey = NULL;
2917 if (wst == WORK_MORE_A) {
2918 /* Let cert callback update client certificates if required */
2919 if (s->cert->cert_cb) {
2920 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2922 s->rwstate = SSL_X509_LOOKUP;
2926 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2927 ossl_statem_set_error(s);
2930 s->rwstate = SSL_NOTHING;
2932 if (ssl3_check_client_certificate(s))
2933 return WORK_FINISHED_CONTINUE;
2935 /* Fall through to WORK_MORE_B */
2939 /* We need to get a client cert */
2940 if (wst == WORK_MORE_B) {
2942 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2943 * return(-1); We then get retied later
2945 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2947 s->rwstate = SSL_X509_LOOKUP;
2950 s->rwstate = SSL_NOTHING;
2951 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2952 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2954 } else if (i == 1) {
2956 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2957 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2961 EVP_PKEY_free(pkey);
2962 if (i && !ssl3_check_client_certificate(s))
2965 if (s->version == SSL3_VERSION) {
2966 s->s3->tmp.cert_req = 0;
2967 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2968 return WORK_FINISHED_CONTINUE;
2970 s->s3->tmp.cert_req = 2;
2971 if (!ssl3_digest_cached_records(s, 0)) {
2972 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2973 ossl_statem_set_error(s);
2979 return WORK_FINISHED_CONTINUE;
2982 /* Shouldn't ever get here */
2986 int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
2988 if (!ssl3_output_cert_chain(s, pkt,
2989 (s->s3->tmp.cert_req == 2) ? NULL
2991 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2992 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2999 #define has_bits(i,m) (((i)&(m)) == (m))
3001 int ssl3_check_cert_and_algorithm(SSL *s)
3004 #ifndef OPENSSL_NO_EC
3008 EVP_PKEY *pkey = NULL;
3009 int al = SSL_AD_HANDSHAKE_FAILURE;
3011 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3012 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3014 /* we don't have a certificate */
3015 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3018 /* This is the passed certificate */
3020 #ifndef OPENSSL_NO_EC
3021 idx = s->session->peer_type;
3022 if (idx == SSL_PKEY_ECC) {
3023 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3025 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3030 } else if (alg_a & SSL_aECDSA) {
3031 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3032 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3036 pkey = X509_get0_pubkey(s->session->peer);
3037 i = X509_certificate_type(s->session->peer, pkey);
3039 /* Check that we have a certificate if we require one */
3040 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3041 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3042 SSL_R_MISSING_RSA_SIGNING_CERT);
3045 #ifndef OPENSSL_NO_DSA
3046 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3047 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3048 SSL_R_MISSING_DSA_SIGNING_CERT);
3052 #ifndef OPENSSL_NO_RSA
3053 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
3054 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3055 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3056 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3060 #ifndef OPENSSL_NO_DH
3061 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
3062 al = SSL_AD_INTERNAL_ERROR;
3063 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3070 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3074 #ifndef OPENSSL_NO_NEXTPROTONEG
3075 int tls_construct_next_proto(SSL *s, WPACKET *pkt)
3077 size_t len, padding_len;
3078 unsigned char *padding = NULL;
3080 len = s->next_proto_negotiated_len;
3081 padding_len = 32 - ((len + 2) % 32);
3083 if (!WPACKET_sub_memcpy_u8(pkt, s->next_proto_negotiated, len)
3084 || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
3085 SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR);
3089 memset(padding, 0, padding_len);
3093 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3098 static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt)
3100 int al = SSL_AD_INTERNAL_ERROR;
3102 RAW_EXTENSION *rawexts = NULL;
3104 if (!PACKET_as_length_prefixed_2(pkt, &extensions)) {
3105 al = SSL_AD_DECODE_ERROR;
3106 SSLerr(SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
3111 * TODO(TLS1.3): For now we are processing Encrypted Extensions and
3112 * Certificate extensions as part of this one message. Later we need to
3113 * split out the Certificate extensions into the Certificate message
3115 if (!tls_collect_extensions(s, &extensions,
3116 EXT_TLS1_3_ENCRYPTED_EXTENSIONS
3117 | EXT_TLS1_3_CERTIFICATE,
3119 || !tls_parse_all_extensions(s,
3120 EXT_TLS1_3_ENCRYPTED_EXTENSIONS
3121 | EXT_TLS1_3_CERTIFICATE,
3125 OPENSSL_free(rawexts);
3126 return MSG_PROCESS_CONTINUE_READING;
3129 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3130 ossl_statem_set_error(s);
3131 OPENSSL_free(rawexts);
3132 return MSG_PROCESS_ERROR;
3135 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3138 #ifndef OPENSSL_NO_ENGINE
3139 if (s->ctx->client_cert_engine) {
3140 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3141 SSL_get_client_CA_list(s),
3142 px509, ppkey, NULL, NULL, NULL);
3147 if (s->ctx->client_cert_cb)
3148 i = s->ctx->client_cert_cb(s, px509, ppkey);
3152 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
3155 size_t totlen = 0, len, maxlen;
3156 int empty_reneg_info_scsv = !s->renegotiate;
3157 /* Set disabled masks for this session */
3158 ssl_set_client_disabled(s);
3163 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
3164 # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
3165 # error Max cipher length too short
3168 * Some servers hang if client hello > 256 bytes as hack workaround
3169 * chop number of supported ciphers to keep it well below this if we
3172 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3173 maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
3176 /* Maximum length that can be stored in 2 bytes. Length must be even */
3179 if (empty_reneg_info_scsv)
3181 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
3184 for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
3185 const SSL_CIPHER *c;
3187 c = sk_SSL_CIPHER_value(sk, i);
3188 /* Skip disabled ciphers */
3189 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3192 if (!s->method->put_cipher_by_char(c, pkt, &len)) {
3193 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3201 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE);
3206 if (empty_reneg_info_scsv) {
3207 static SSL_CIPHER scsv = {
3208 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3210 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3211 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3215 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3216 static SSL_CIPHER scsv = {
3217 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3219 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3220 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);