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 ossl_inline int cert_req_allowed(SSL *s);
63 static int key_exchange_expected(SSL *s);
64 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
65 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
69 * Is a CertificateRequest message allowed at the moment or not?
75 static ossl_inline int cert_req_allowed(SSL *s)
77 /* TLS does not like anon-DH with client cert */
78 if ((s->version > SSL3_VERSION
79 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
80 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
87 * Should we expect the ServerKeyExchange message or not?
93 static int key_exchange_expected(SSL *s)
95 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
98 * Can't skip server key exchange if this is an ephemeral
99 * ciphersuite or for SRP
101 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
110 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
111 * handshake state transitions when the client is reading messages from the
112 * server. The message type that the server has sent is provided in |mt|. The
113 * current state is in |s->statem.hand_state|.
116 * 1: Success (transition allowed)
117 * 0: Error (transition not allowed)
119 int ossl_statem_client_read_transition(SSL *s, int mt)
121 OSSL_STATEM *st = &s->statem;
124 switch (st->hand_state) {
128 case TLS_ST_CW_CLNT_HELLO:
129 if (mt == SSL3_MT_SERVER_HELLO) {
130 st->hand_state = TLS_ST_CR_SRVR_HELLO;
134 if (SSL_IS_DTLS(s)) {
135 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
136 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
142 case TLS_ST_CR_SRVR_HELLO:
144 if (s->tlsext_ticket_expected) {
145 if (mt == SSL3_MT_NEWSESSION_TICKET) {
146 st->hand_state = TLS_ST_CR_SESSION_TICKET;
149 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
150 st->hand_state = TLS_ST_CR_CHANGE;
154 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
155 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
157 } else if (s->version >= TLS1_VERSION
158 && s->tls_session_secret_cb != NULL
159 && s->session->tlsext_tick != NULL
160 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
162 * Normally, we can tell if the server is resuming the session
163 * from the session ID. EAP-FAST (RFC 4851), however, relies on
164 * the next server message after the ServerHello to determine if
165 * the server is resuming.
168 st->hand_state = TLS_ST_CR_CHANGE;
170 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
171 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
172 if (mt == SSL3_MT_CERTIFICATE) {
173 st->hand_state = TLS_ST_CR_CERT;
177 ske_expected = key_exchange_expected(s);
178 /* SKE is optional for some PSK ciphersuites */
180 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
181 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
182 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
183 st->hand_state = TLS_ST_CR_KEY_EXCH;
186 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
187 && cert_req_allowed(s)) {
188 st->hand_state = TLS_ST_CR_CERT_REQ;
190 } else if (mt == SSL3_MT_SERVER_DONE) {
191 st->hand_state = TLS_ST_CR_SRVR_DONE;
200 * The CertificateStatus message is optional even if
201 * |tlsext_status_expected| is set
203 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
204 st->hand_state = TLS_ST_CR_CERT_STATUS;
209 case TLS_ST_CR_CERT_STATUS:
210 ske_expected = key_exchange_expected(s);
211 /* SKE is optional for some PSK ciphersuites */
212 if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
213 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
214 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
215 st->hand_state = TLS_ST_CR_KEY_EXCH;
222 case TLS_ST_CR_KEY_EXCH:
223 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
224 if (cert_req_allowed(s)) {
225 st->hand_state = TLS_ST_CR_CERT_REQ;
232 case TLS_ST_CR_CERT_REQ:
233 if (mt == SSL3_MT_SERVER_DONE) {
234 st->hand_state = TLS_ST_CR_SRVR_DONE;
239 case TLS_ST_CW_FINISHED:
240 if (s->tlsext_ticket_expected) {
241 if (mt == SSL3_MT_NEWSESSION_TICKET) {
242 st->hand_state = TLS_ST_CR_SESSION_TICKET;
245 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
246 st->hand_state = TLS_ST_CR_CHANGE;
251 case TLS_ST_CR_SESSION_TICKET:
252 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
253 st->hand_state = TLS_ST_CR_CHANGE;
258 case TLS_ST_CR_CHANGE:
259 if (mt == SSL3_MT_FINISHED) {
260 st->hand_state = TLS_ST_CR_FINISHED;
267 /* No valid transition found */
268 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
269 SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
274 * client_write_transition() works out what handshake state to move to next
275 * when the client is writing messages to be sent to the server.
277 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
279 OSSL_STATEM *st = &s->statem;
281 switch (st->hand_state) {
283 /* Shouldn't happen */
284 return WRITE_TRAN_ERROR;
287 /* Renegotiation - fall through */
289 st->hand_state = TLS_ST_CW_CLNT_HELLO;
290 return WRITE_TRAN_CONTINUE;
292 case TLS_ST_CW_CLNT_HELLO:
294 * No transition at the end of writing because we don't know what
297 return WRITE_TRAN_FINISHED;
299 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
300 st->hand_state = TLS_ST_CW_CLNT_HELLO;
301 return WRITE_TRAN_CONTINUE;
303 case TLS_ST_CR_SRVR_DONE:
304 if (s->s3->tmp.cert_req)
305 st->hand_state = TLS_ST_CW_CERT;
307 st->hand_state = TLS_ST_CW_KEY_EXCH;
308 return WRITE_TRAN_CONTINUE;
311 st->hand_state = TLS_ST_CW_KEY_EXCH;
312 return WRITE_TRAN_CONTINUE;
314 case TLS_ST_CW_KEY_EXCH:
316 * For TLS, cert_req is set to 2, so a cert chain of nothing is
317 * sent, but no verify packet is sent
320 * XXX: For now, we do not support client authentication in ECDH
321 * cipher suites with ECDH (rather than ECDSA) certificates. We
322 * need to skip the certificate verify message when client's
323 * ECDH public key is sent inside the client certificate.
325 if (s->s3->tmp.cert_req == 1) {
326 st->hand_state = TLS_ST_CW_CERT_VRFY;
328 st->hand_state = TLS_ST_CW_CHANGE;
330 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
331 st->hand_state = TLS_ST_CW_CHANGE;
333 return WRITE_TRAN_CONTINUE;
335 case TLS_ST_CW_CERT_VRFY:
336 st->hand_state = TLS_ST_CW_CHANGE;
337 return WRITE_TRAN_CONTINUE;
339 case TLS_ST_CW_CHANGE:
340 #if defined(OPENSSL_NO_NEXTPROTONEG)
341 st->hand_state = TLS_ST_CW_FINISHED;
343 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
344 st->hand_state = TLS_ST_CW_NEXT_PROTO;
346 st->hand_state = TLS_ST_CW_FINISHED;
348 return WRITE_TRAN_CONTINUE;
350 #if !defined(OPENSSL_NO_NEXTPROTONEG)
351 case TLS_ST_CW_NEXT_PROTO:
352 st->hand_state = TLS_ST_CW_FINISHED;
353 return WRITE_TRAN_CONTINUE;
356 case TLS_ST_CW_FINISHED:
358 st->hand_state = TLS_ST_OK;
359 ossl_statem_set_in_init(s, 0);
360 return WRITE_TRAN_CONTINUE;
362 return WRITE_TRAN_FINISHED;
365 case TLS_ST_CR_FINISHED:
367 st->hand_state = TLS_ST_CW_CHANGE;
368 return WRITE_TRAN_CONTINUE;
370 st->hand_state = TLS_ST_OK;
371 ossl_statem_set_in_init(s, 0);
372 return WRITE_TRAN_CONTINUE;
378 * Perform any pre work that needs to be done prior to sending a message from
379 * the client to the server.
381 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
383 OSSL_STATEM *st = &s->statem;
385 switch (st->hand_state) {
387 /* No pre work to be done */
390 case TLS_ST_CW_CLNT_HELLO:
392 if (SSL_IS_DTLS(s)) {
393 /* every DTLS ClientHello resets Finished MAC */
394 if (!ssl3_init_finished_mac(s)) {
395 ossl_statem_set_error(s);
401 case TLS_ST_CW_CHANGE:
402 if (SSL_IS_DTLS(s)) {
405 * We're into the last flight so we don't retransmit these
406 * messages unless we need to.
410 #ifndef OPENSSL_NO_SCTP
411 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
412 return dtls_wait_for_dry(s);
418 return tls_finish_handshake(s, wst);
421 return WORK_FINISHED_CONTINUE;
425 * Perform any work that needs to be done after sending a message from the
426 * client to the server.
428 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
430 OSSL_STATEM *st = &s->statem;
434 switch (st->hand_state) {
436 /* No post work to be done */
439 case TLS_ST_CW_CLNT_HELLO:
440 if (wst == WORK_MORE_A && statem_flush(s) != 1)
443 if (SSL_IS_DTLS(s)) {
444 /* Treat the next message as the first packet */
449 case TLS_ST_CW_KEY_EXCH:
450 if (tls_client_key_exchange_post_work(s) == 0)
454 case TLS_ST_CW_CHANGE:
455 s->session->cipher = s->s3->tmp.new_cipher;
456 #ifdef OPENSSL_NO_COMP
457 s->session->compress_meth = 0;
459 if (s->s3->tmp.new_compression == NULL)
460 s->session->compress_meth = 0;
462 s->session->compress_meth = s->s3->tmp.new_compression->id;
464 if (!s->method->ssl3_enc->setup_key_block(s))
467 if (!s->method->ssl3_enc->change_cipher_state(s,
468 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
471 if (SSL_IS_DTLS(s)) {
472 #ifndef OPENSSL_NO_SCTP
475 * Change to new shared key of SCTP-Auth, will be ignored if
478 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
483 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
487 case TLS_ST_CW_FINISHED:
488 #ifndef OPENSSL_NO_SCTP
489 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
491 * Change to new shared key of SCTP-Auth, will be ignored if
494 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
498 if (statem_flush(s) != 1)
503 return WORK_FINISHED_CONTINUE;
507 * Get the message construction function and message type for sending from the
510 * Valid return values are:
514 int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
515 confunc_f *confunc, int *mt)
517 OSSL_STATEM *st = &s->statem;
519 switch (st->hand_state) {
521 /* Shouldn't happen */
524 case TLS_ST_CW_CHANGE:
526 *confunc = dtls_construct_change_cipher_spec;
528 *confunc = tls_construct_change_cipher_spec;
529 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
532 case TLS_ST_CW_CLNT_HELLO:
533 *confunc = tls_construct_client_hello;
534 *mt = SSL3_MT_CLIENT_HELLO;
538 *confunc = tls_construct_client_certificate;
539 *mt = SSL3_MT_CERTIFICATE;
542 case TLS_ST_CW_KEY_EXCH:
543 *confunc = tls_construct_client_key_exchange;
544 *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
547 case TLS_ST_CW_CERT_VRFY:
548 *confunc = tls_construct_client_verify;
549 *mt = SSL3_MT_CERTIFICATE_VERIFY;
552 #if !defined(OPENSSL_NO_NEXTPROTONEG)
553 case TLS_ST_CW_NEXT_PROTO:
554 *confunc = tls_construct_next_proto;
555 *mt = SSL3_MT_NEXT_PROTO;
558 case TLS_ST_CW_FINISHED:
559 *confunc = tls_construct_finished;
560 *mt = SSL3_MT_FINISHED;
568 * Returns the maximum allowed length for the current message that we are
569 * reading. Excludes the message header.
571 size_t ossl_statem_client_max_message_size(SSL *s)
573 OSSL_STATEM *st = &s->statem;
575 switch (st->hand_state) {
577 /* Shouldn't happen */
580 case TLS_ST_CR_SRVR_HELLO:
581 return SERVER_HELLO_MAX_LENGTH;
583 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
584 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
587 return s->max_cert_list;
589 case TLS_ST_CR_CERT_STATUS:
590 return SSL3_RT_MAX_PLAIN_LENGTH;
592 case TLS_ST_CR_KEY_EXCH:
593 return SERVER_KEY_EXCH_MAX_LENGTH;
595 case TLS_ST_CR_CERT_REQ:
597 * Set to s->max_cert_list for compatibility with previous releases. In
598 * practice these messages can get quite long if servers are configured
599 * to provide a long list of acceptable CAs
601 return s->max_cert_list;
603 case TLS_ST_CR_SRVR_DONE:
604 return SERVER_HELLO_DONE_MAX_LENGTH;
606 case TLS_ST_CR_CHANGE:
607 if (s->version == DTLS1_BAD_VER)
609 return CCS_MAX_LENGTH;
611 case TLS_ST_CR_SESSION_TICKET:
612 return SSL3_RT_MAX_PLAIN_LENGTH;
614 case TLS_ST_CR_FINISHED:
615 return FINISHED_MAX_LENGTH;
620 * Process a message that the client has been received from the server.
622 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
624 OSSL_STATEM *st = &s->statem;
626 switch (st->hand_state) {
628 /* Shouldn't happen */
629 return MSG_PROCESS_ERROR;
631 case TLS_ST_CR_SRVR_HELLO:
632 return tls_process_server_hello(s, pkt);
634 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
635 return dtls_process_hello_verify(s, pkt);
638 return tls_process_server_certificate(s, pkt);
640 case TLS_ST_CR_CERT_STATUS:
641 return tls_process_cert_status(s, pkt);
643 case TLS_ST_CR_KEY_EXCH:
644 return tls_process_key_exchange(s, pkt);
646 case TLS_ST_CR_CERT_REQ:
647 return tls_process_certificate_request(s, pkt);
649 case TLS_ST_CR_SRVR_DONE:
650 return tls_process_server_done(s, pkt);
652 case TLS_ST_CR_CHANGE:
653 return tls_process_change_cipher_spec(s, pkt);
655 case TLS_ST_CR_SESSION_TICKET:
656 return tls_process_new_session_ticket(s, pkt);
658 case TLS_ST_CR_FINISHED:
659 return tls_process_finished(s, pkt);
664 * Perform any further processing required following the receipt of a message
667 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
669 OSSL_STATEM *st = &s->statem;
671 switch (st->hand_state) {
673 /* Shouldn't happen */
676 case TLS_ST_CR_CERT_REQ:
677 return tls_prepare_client_certificate(s, wst);
679 #ifndef OPENSSL_NO_SCTP
680 case TLS_ST_CR_SRVR_DONE:
681 /* We only get here if we are using SCTP and we are renegotiating */
682 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
683 s->s3->in_read_app_data = 2;
684 s->rwstate = SSL_READING;
685 BIO_clear_retry_flags(SSL_get_rbio(s));
686 BIO_set_retry_read(SSL_get_rbio(s));
687 ossl_statem_set_sctp_read_sock(s, 1);
690 ossl_statem_set_sctp_read_sock(s, 0);
691 return WORK_FINISHED_STOP;
696 int tls_construct_client_hello(SSL *s, WPACKET *pkt)
701 int al = SSL_AD_HANDSHAKE_FAILURE;
702 #ifndef OPENSSL_NO_COMP
705 SSL_SESSION *sess = s->session;
707 if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) {
708 /* Should not happen */
709 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
713 /* Work out what SSL/TLS/DTLS version to use */
714 protverr = ssl_set_client_hello_version(s);
716 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
720 if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
722 * In the case of EAP-FAST, we can have a pre-shared
723 * "ticket" without a session ID.
725 (!sess->session_id_length && !sess->tlsext_tick) ||
726 (sess->not_resumable)) {
727 if (!ssl_get_new_session(s, 0))
730 /* else use the pre-loaded session */
732 p = s->s3->client_random;
735 * for DTLS if client_random is initialized, reuse it, we are
736 * required to use same upon reply to HelloVerify
738 if (SSL_IS_DTLS(s)) {
741 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
750 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
754 * version indicates the negotiated version: for example from
755 * an SSLv2/v3 compatible client hello). The client_version
756 * field is the maximum version we permit and it is also
757 * used in RSA encrypted premaster secrets. Some servers can
758 * choke if we initially report a higher version then
759 * renegotiate to a lower one in the premaster secret. This
760 * didn't happen with TLS 1.0 as most servers supported it
761 * but it can with TLS 1.1 or later if the server only supports
764 * Possible scenario with previous logic:
765 * 1. Client hello indicates TLS 1.2
766 * 2. Server hello says TLS 1.0
767 * 3. RSA encrypted premaster secret uses 1.2.
768 * 4. Handshake proceeds using TLS 1.0.
769 * 5. Server sends hello request to renegotiate.
770 * 6. Client hello indicates TLS v1.0 as we now
771 * know that is maximum server supports.
772 * 7. Server chokes on RSA encrypted premaster secret
773 * containing version 1.0.
775 * For interoperability it should be OK to always use the
776 * maximum version we support in client hello and then rely
777 * on the checking of version to ensure the servers isn't
778 * being inconsistent: for example initially negotiating with
779 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
780 * client_version in client hello and not resetting it to
781 * the negotiated version.
783 if (!WPACKET_put_bytes_u16(pkt, s->client_version)
784 || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) {
785 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
793 sess_id_len = s->session->session_id_length;
794 if (sess_id_len > sizeof(s->session->session_id)
795 || !WPACKET_start_sub_packet_u8(pkt)
796 || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id,
798 || !WPACKET_close(pkt)) {
799 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
803 /* cookie stuff for DTLS */
804 if (SSL_IS_DTLS(s)) {
805 if (s->d1->cookie_len > sizeof(s->d1->cookie)
806 || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
807 s->d1->cookie_len)) {
808 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
813 /* Ciphers supported */
814 if (!WPACKET_start_sub_packet_u16(pkt)) {
815 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
818 /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */
819 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt))
821 if (!WPACKET_close(pkt)) {
822 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
827 if (!WPACKET_start_sub_packet_u8(pkt)) {
828 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
831 #ifndef OPENSSL_NO_COMP
832 if (ssl_allow_compression(s) && s->ctx->comp_methods) {
833 int compnum = sk_SSL_COMP_num(s->ctx->comp_methods);
834 for (i = 0; i < compnum; i++) {
835 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
836 if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
837 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
843 /* Add the NULL method */
844 if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
845 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
850 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
851 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
854 if (!WPACKET_start_sub_packet_u16(pkt)
856 * If extensions are of zero length then we don't even add the
857 * extensions length bytes
859 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
860 || !ssl_add_clienthello_tlsext(s, pkt, &al)
861 || !WPACKET_close(pkt)) {
862 ssl3_send_alert(s, SSL3_AL_FATAL, al);
863 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
870 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
876 if (!PACKET_forward(pkt, 2)
877 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
878 al = SSL_AD_DECODE_ERROR;
879 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
883 cookie_len = PACKET_remaining(&cookiepkt);
884 if (cookie_len > sizeof(s->d1->cookie)) {
885 al = SSL_AD_ILLEGAL_PARAMETER;
886 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
890 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
891 al = SSL_AD_DECODE_ERROR;
892 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
895 s->d1->cookie_len = cookie_len;
897 return MSG_PROCESS_FINISHED_READING;
899 ssl3_send_alert(s, SSL3_AL_FATAL, al);
900 ossl_statem_set_error(s);
901 return MSG_PROCESS_ERROR;
904 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
906 STACK_OF(SSL_CIPHER) *sk;
909 size_t session_id_len;
910 const unsigned char *cipherchars;
911 int i, al = SSL_AD_INTERNAL_ERROR;
912 unsigned int compression;
913 unsigned int sversion;
915 #ifndef OPENSSL_NO_COMP
919 if (!PACKET_get_net_2(pkt, &sversion)) {
920 al = SSL_AD_DECODE_ERROR;
921 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
925 protverr = ssl_choose_client_version(s, sversion);
927 al = SSL_AD_PROTOCOL_VERSION;
928 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
932 /* load the server hello data */
933 /* load the server random */
934 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
935 al = SSL_AD_DECODE_ERROR;
936 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
942 /* Get the session-id. */
943 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
944 al = SSL_AD_DECODE_ERROR;
945 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
948 session_id_len = PACKET_remaining(&session_id);
949 if (session_id_len > sizeof s->session->session_id
950 || session_id_len > SSL3_SESSION_ID_SIZE) {
951 al = SSL_AD_ILLEGAL_PARAMETER;
952 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
956 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
957 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
958 al = SSL_AD_DECODE_ERROR;
963 * Check if we can resume the session based on external pre-shared secret.
964 * EAP-FAST (RFC 4851) supports two types of session resumption.
965 * Resumption based on server-side state works with session IDs.
966 * Resumption based on pre-shared Protected Access Credentials (PACs)
967 * works by overriding the SessionTicket extension at the application
968 * layer, and does not send a session ID. (We do not know whether EAP-FAST
969 * servers would honour the session ID.) Therefore, the session ID alone
970 * is not a reliable indicator of session resumption, so we first check if
971 * we can resume, and later peek at the next handshake message to see if the
972 * server wants to resume.
974 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
975 s->session->tlsext_tick) {
976 const SSL_CIPHER *pref_cipher = NULL;
978 * s->session->master_key_length is a size_t, but this is an int for
979 * backwards compat reasons
981 int master_key_length;
982 master_key_length = sizeof(s->session->master_key);
983 if (s->tls_session_secret_cb(s, s->session->master_key,
986 s->tls_session_secret_cb_arg)
987 && master_key_length > 0) {
988 s->session->master_key_length = master_key_length;
989 s->session->cipher = pref_cipher ?
990 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
992 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
993 al = SSL_AD_INTERNAL_ERROR;
998 if (session_id_len != 0 && session_id_len == s->session->session_id_length
999 && memcmp(PACKET_data(&session_id), s->session->session_id,
1000 session_id_len) == 0) {
1001 if (s->sid_ctx_length != s->session->sid_ctx_length
1002 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1003 /* actually a client application bug */
1004 al = SSL_AD_ILLEGAL_PARAMETER;
1005 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1006 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1012 * If we were trying for session-id reuse but the server
1013 * didn't echo the ID, make a new SSL_SESSION.
1014 * In the case of EAP-FAST and PAC, we do not send a session ID,
1015 * so the PAC-based session secret is always preserved. It'll be
1016 * overwritten if the server refuses resumption.
1018 if (s->session->session_id_length > 0) {
1019 s->ctx->stats.sess_miss++;
1020 if (!ssl_get_new_session(s, 0)) {
1025 s->session->ssl_version = s->version;
1026 s->session->session_id_length = session_id_len;
1027 /* session_id_len could be 0 */
1028 memcpy(s->session->session_id, PACKET_data(&session_id),
1032 /* Session version and negotiated protocol version should match */
1033 if (s->version != s->session->ssl_version) {
1034 al = SSL_AD_PROTOCOL_VERSION;
1036 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1037 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1041 c = ssl_get_cipher_by_char(s, cipherchars);
1043 /* unknown cipher */
1044 al = SSL_AD_ILLEGAL_PARAMETER;
1045 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1049 * Now that we know the version, update the check to see if it's an allowed
1052 s->s3->tmp.min_ver = s->version;
1053 s->s3->tmp.max_ver = s->version;
1055 * If it is a disabled cipher we either didn't send it in client hello,
1056 * or it's not allowed for the selected protocol. So we return an error.
1058 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1059 al = SSL_AD_ILLEGAL_PARAMETER;
1060 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1064 sk = ssl_get_ciphers_by_id(s);
1065 i = sk_SSL_CIPHER_find(sk, c);
1067 /* we did not say we would use this cipher */
1068 al = SSL_AD_ILLEGAL_PARAMETER;
1069 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1074 * Depending on the session caching (internal/external), the cipher
1075 * and/or cipher_id values may not be set. Make sure that cipher_id is
1076 * set and use it for comparison.
1078 if (s->session->cipher)
1079 s->session->cipher_id = s->session->cipher->id;
1080 if (s->hit && (s->session->cipher_id != c->id)) {
1081 al = SSL_AD_ILLEGAL_PARAMETER;
1082 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1083 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1086 s->s3->tmp.new_cipher = c;
1087 /* lets get the compression algorithm */
1089 if (!PACKET_get_1(pkt, &compression)) {
1090 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1091 al = SSL_AD_DECODE_ERROR;
1094 #ifdef OPENSSL_NO_COMP
1095 if (compression != 0) {
1096 al = SSL_AD_ILLEGAL_PARAMETER;
1097 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1098 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1102 * If compression is disabled we'd better not try to resume a session
1103 * using compression.
1105 if (s->session->compress_meth != 0) {
1106 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1110 if (s->hit && compression != s->session->compress_meth) {
1111 al = SSL_AD_ILLEGAL_PARAMETER;
1112 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1113 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1116 if (compression == 0)
1118 else if (!ssl_allow_compression(s)) {
1119 al = SSL_AD_ILLEGAL_PARAMETER;
1120 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1123 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1126 if (compression != 0 && comp == NULL) {
1127 al = SSL_AD_ILLEGAL_PARAMETER;
1128 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1129 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1132 s->s3->tmp.new_compression = comp;
1136 /* TLS extensions */
1137 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1138 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1142 if (PACKET_remaining(pkt) != 0) {
1143 /* wrong packet length */
1144 al = SSL_AD_DECODE_ERROR;
1145 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1148 #ifndef OPENSSL_NO_SCTP
1149 if (SSL_IS_DTLS(s) && s->hit) {
1150 unsigned char sctpauthkey[64];
1151 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1154 * Add new shared key for SCTP-Auth, will be ignored if
1157 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1158 sizeof(DTLS1_SCTP_AUTH_LABEL));
1160 if (SSL_export_keying_material(s, sctpauthkey,
1161 sizeof(sctpauthkey),
1163 sizeof(labelbuffer), NULL, 0, 0) <= 0)
1166 BIO_ctrl(SSL_get_wbio(s),
1167 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1168 sizeof(sctpauthkey), sctpauthkey);
1172 return MSG_PROCESS_CONTINUE_READING;
1174 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1176 ossl_statem_set_error(s);
1177 return MSG_PROCESS_ERROR;
1180 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1182 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1183 unsigned long cert_list_len, cert_len;
1185 const unsigned char *certstart, *certbytes;
1186 STACK_OF(X509) *sk = NULL;
1187 EVP_PKEY *pkey = NULL;
1189 if ((sk = sk_X509_new_null()) == NULL) {
1190 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1194 if (!PACKET_get_net_3(pkt, &cert_list_len)
1195 || PACKET_remaining(pkt) != cert_list_len) {
1196 al = SSL_AD_DECODE_ERROR;
1197 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1200 while (PACKET_remaining(pkt)) {
1201 if (!PACKET_get_net_3(pkt, &cert_len)
1202 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1203 al = SSL_AD_DECODE_ERROR;
1204 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1205 SSL_R_CERT_LENGTH_MISMATCH);
1209 certstart = certbytes;
1210 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1212 al = SSL_AD_BAD_CERTIFICATE;
1213 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1216 if (certbytes != (certstart + cert_len)) {
1217 al = SSL_AD_DECODE_ERROR;
1218 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1219 SSL_R_CERT_LENGTH_MISMATCH);
1222 if (!sk_X509_push(sk, x)) {
1223 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1229 i = ssl_verify_cert_chain(s, sk);
1231 * The documented interface is that SSL_VERIFY_PEER should be set in order
1232 * for client side verification of the server certificate to take place.
1233 * However, historically the code has only checked that *any* flag is set
1234 * to cause server verification to take place. Use of the other flags makes
1235 * no sense in client mode. An attempt to clean up the semantics was
1236 * reverted because at least one application *only* set
1237 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1238 * server verification to take place, after the clean up it silently did
1239 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1240 * sent to them because they are void functions. Therefore, we now use the
1241 * (less clean) historic behaviour of performing validation if any flag is
1242 * set. The *documented* interface remains the same.
1244 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1245 al = ssl_verify_alarm_type(s->verify_result);
1246 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1247 SSL_R_CERTIFICATE_VERIFY_FAILED);
1250 ERR_clear_error(); /* but we keep s->verify_result */
1252 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1253 al = SSL_AD_HANDSHAKE_FAILURE;
1257 s->session->peer_chain = sk;
1259 * Inconsistency alert: cert_chain does include the peer's certificate,
1260 * which we don't include in statem_srvr.c
1262 x = sk_X509_value(sk, 0);
1265 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1268 pkey = X509_get0_pubkey(x);
1270 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1273 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1274 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1278 i = ssl_cert_type(x, pkey);
1282 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1283 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1287 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1288 if (exp_idx >= 0 && i != exp_idx
1289 && (exp_idx != SSL_PKEY_GOST_EC ||
1290 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1291 && i != SSL_PKEY_GOST01))) {
1293 al = SSL_AD_ILLEGAL_PARAMETER;
1294 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1295 SSL_R_WRONG_CERTIFICATE_TYPE);
1298 s->session->peer_type = i;
1300 X509_free(s->session->peer);
1302 s->session->peer = x;
1303 s->session->verify_result = s->verify_result;
1306 ret = MSG_PROCESS_CONTINUE_READING;
1310 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1312 ossl_statem_set_error(s);
1315 sk_X509_pop_free(sk, X509_free);
1319 static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
1321 #ifndef OPENSSL_NO_PSK
1322 PACKET psk_identity_hint;
1324 /* PSK ciphersuites are preceded by an identity hint */
1326 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1327 *al = SSL_AD_DECODE_ERROR;
1328 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
1333 * Store PSK identity hint for later use, hint is used in
1334 * tls_construct_client_key_exchange. Assume that the maximum length of
1335 * a PSK identity hint can be as long as the maximum length of a PSK
1338 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1339 *al = SSL_AD_HANDSHAKE_FAILURE;
1340 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
1344 if (PACKET_remaining(&psk_identity_hint) == 0) {
1345 OPENSSL_free(s->session->psk_identity_hint);
1346 s->session->psk_identity_hint = NULL;
1347 } else if (!PACKET_strndup(&psk_identity_hint,
1348 &s->session->psk_identity_hint)) {
1349 *al = SSL_AD_INTERNAL_ERROR;
1355 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
1356 *al = SSL_AD_INTERNAL_ERROR;
1361 static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1363 #ifndef OPENSSL_NO_SRP
1364 PACKET prime, generator, salt, server_pub;
1366 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1367 || !PACKET_get_length_prefixed_2(pkt, &generator)
1368 || !PACKET_get_length_prefixed_1(pkt, &salt)
1369 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1370 *al = SSL_AD_DECODE_ERROR;
1371 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
1375 /* TODO(size_t): Convert BN_bin2bn() calls */
1377 BN_bin2bn(PACKET_data(&prime),
1378 (int)PACKET_remaining(&prime), NULL)) == NULL
1380 BN_bin2bn(PACKET_data(&generator),
1381 (int)PACKET_remaining(&generator), NULL)) == NULL
1383 BN_bin2bn(PACKET_data(&salt),
1384 (int)PACKET_remaining(&salt), NULL)) == NULL
1386 BN_bin2bn(PACKET_data(&server_pub),
1387 (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
1388 *al = SSL_AD_INTERNAL_ERROR;
1389 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
1393 if (!srp_verify_server_param(s, al)) {
1394 *al = SSL_AD_DECODE_ERROR;
1395 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
1399 /* We must check if there is a certificate */
1400 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1401 *pkey = X509_get0_pubkey(s->session->peer);
1405 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
1406 *al = SSL_AD_INTERNAL_ERROR;
1411 static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1413 #ifndef OPENSSL_NO_DH
1414 PACKET prime, generator, pub_key;
1415 EVP_PKEY *peer_tmp = NULL;
1418 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1420 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1421 || !PACKET_get_length_prefixed_2(pkt, &generator)
1422 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1423 *al = SSL_AD_DECODE_ERROR;
1424 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
1428 peer_tmp = EVP_PKEY_new();
1431 if (peer_tmp == NULL || dh == NULL) {
1432 *al = SSL_AD_INTERNAL_ERROR;
1433 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
1437 /* TODO(size_t): Convert these calls */
1438 p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
1439 g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
1441 bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
1442 (int)PACKET_remaining(&pub_key), NULL);
1443 if (p == NULL || g == NULL || bnpub_key == NULL) {
1444 *al = SSL_AD_INTERNAL_ERROR;
1445 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1449 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1450 *al = SSL_AD_DECODE_ERROR;
1451 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
1455 if (!DH_set0_pqg(dh, p, NULL, g)) {
1456 *al = SSL_AD_INTERNAL_ERROR;
1457 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1462 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1463 *al = SSL_AD_INTERNAL_ERROR;
1464 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1469 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1470 *al = SSL_AD_HANDSHAKE_FAILURE;
1471 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
1475 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1476 *al = SSL_AD_INTERNAL_ERROR;
1477 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
1481 s->s3->peer_tmp = peer_tmp;
1484 * FIXME: This makes assumptions about which ciphersuites come with
1485 * public keys. We should have a less ad-hoc way of doing this
1487 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1488 *pkey = X509_get0_pubkey(s->session->peer);
1489 /* else anonymous DH, so no certificate or pkey. */
1498 EVP_PKEY_free(peer_tmp);
1502 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
1503 *al = SSL_AD_INTERNAL_ERROR;
1508 static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1510 #ifndef OPENSSL_NO_EC
1512 const unsigned char *ecparams;
1514 unsigned int curve_flags;
1515 EVP_PKEY_CTX *pctx = NULL;
1518 * Extract elliptic curve parameters and the server's ephemeral ECDH
1519 * public key. For now we only support named (not generic) curves and
1520 * ECParameters in this case is just three bytes.
1522 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1523 *al = SSL_AD_DECODE_ERROR;
1524 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
1528 * Check curve is one of our preferences, if not server has sent an
1529 * invalid curve. ECParameters is 3 bytes.
1531 if (!tls1_check_curve(s, ecparams, 3)) {
1532 *al = SSL_AD_DECODE_ERROR;
1533 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
1537 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
1539 if (curve_nid == 0) {
1540 *al = SSL_AD_INTERNAL_ERROR;
1541 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
1542 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1546 if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
1547 EVP_PKEY *key = EVP_PKEY_new();
1549 if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
1550 *al = SSL_AD_INTERNAL_ERROR;
1551 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1555 s->s3->peer_tmp = key;
1557 /* Set up EVP_PKEY with named curve as parameters */
1558 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1560 || EVP_PKEY_paramgen_init(pctx) <= 0
1561 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1562 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1563 *al = SSL_AD_INTERNAL_ERROR;
1564 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1565 EVP_PKEY_CTX_free(pctx);
1568 EVP_PKEY_CTX_free(pctx);
1572 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1573 *al = SSL_AD_DECODE_ERROR;
1574 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
1578 if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
1579 PACKET_data(&encoded_pt),
1580 PACKET_remaining(&encoded_pt))) {
1581 *al = SSL_AD_DECODE_ERROR;
1582 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
1587 * The ECC/TLS specification does not mention the use of DSA to sign
1588 * ECParameters in the server key exchange message. We do support RSA
1591 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
1592 *pkey = X509_get0_pubkey(s->session->peer);
1593 else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
1594 *pkey = X509_get0_pubkey(s->session->peer);
1595 /* else anonymous ECDH, so no certificate or pkey. */
1599 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
1600 *al = SSL_AD_INTERNAL_ERROR;
1605 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1609 EVP_PKEY *pkey = NULL;
1610 PACKET save_param_start, signature;
1612 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1614 save_param_start = *pkt;
1616 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1617 EVP_PKEY_free(s->s3->peer_tmp);
1618 s->s3->peer_tmp = NULL;
1621 if (alg_k & SSL_PSK) {
1622 if (!tls_process_ske_psk_preamble(s, pkt, &al))
1626 /* Nothing else to do for plain PSK or RSAPSK */
1627 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1628 } else if (alg_k & SSL_kSRP) {
1629 if (!tls_process_ske_srp(s, pkt, &pkey, &al))
1631 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1632 if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
1634 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1635 if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
1638 al = SSL_AD_UNEXPECTED_MESSAGE;
1639 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1643 /* if it was signed, check the signature */
1647 const EVP_MD *md = NULL;
1651 * |pkt| now points to the beginning of the signature, so the difference
1652 * equals the length of the parameters.
1654 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1655 PACKET_remaining(&save_param_start) -
1656 PACKET_remaining(pkt))) {
1657 al = SSL_AD_INTERNAL_ERROR;
1658 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1662 if (SSL_USE_SIGALGS(s)) {
1663 const unsigned char *sigalgs;
1665 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1666 al = SSL_AD_DECODE_ERROR;
1667 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1670 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1672 al = SSL_AD_INTERNAL_ERROR;
1674 } else if (rv == 0) {
1675 al = SSL_AD_DECODE_ERROR;
1679 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1681 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1682 md = EVP_md5_sha1();
1687 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1688 || PACKET_remaining(pkt) != 0) {
1689 al = SSL_AD_DECODE_ERROR;
1690 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1693 maxsig = EVP_PKEY_size(pkey);
1695 al = SSL_AD_INTERNAL_ERROR;
1696 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1701 * Check signature length
1703 if (PACKET_remaining(&signature) > (size_t)maxsig) {
1704 /* wrong packet length */
1705 al = SSL_AD_DECODE_ERROR;
1706 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1707 SSL_R_WRONG_SIGNATURE_LENGTH);
1711 md_ctx = EVP_MD_CTX_new();
1712 if (md_ctx == NULL) {
1713 al = SSL_AD_INTERNAL_ERROR;
1714 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1718 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1719 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1720 SSL3_RANDOM_SIZE) <= 0
1721 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1722 SSL3_RANDOM_SIZE) <= 0
1723 || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms),
1724 PACKET_remaining(¶ms)) <= 0) {
1725 EVP_MD_CTX_free(md_ctx);
1726 al = SSL_AD_INTERNAL_ERROR;
1727 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1730 /* TODO(size_t): Convert this call */
1731 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1732 (unsigned int)PACKET_remaining(&signature),
1735 EVP_MD_CTX_free(md_ctx);
1736 al = SSL_AD_DECRYPT_ERROR;
1737 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1740 EVP_MD_CTX_free(md_ctx);
1742 /* aNULL, aSRP or PSK do not need public keys */
1743 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1744 && !(alg_k & SSL_PSK)) {
1745 /* Might be wrong key type, check it */
1746 if (ssl3_check_cert_and_algorithm(s)) {
1747 /* Otherwise this shouldn't happen */
1748 al = SSL_AD_INTERNAL_ERROR;
1749 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1751 al = SSL_AD_DECODE_ERROR;
1755 /* still data left over */
1756 if (PACKET_remaining(pkt) != 0) {
1757 al = SSL_AD_DECODE_ERROR;
1758 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1763 return MSG_PROCESS_CONTINUE_READING;
1766 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1767 ossl_statem_set_error(s);
1768 return MSG_PROCESS_ERROR;
1771 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1773 int ret = MSG_PROCESS_ERROR;
1774 unsigned int list_len, ctype_num, i, name_len;
1775 X509_NAME *xn = NULL;
1776 const unsigned char *data;
1777 const unsigned char *namestart, *namebytes;
1778 STACK_OF(X509_NAME) *ca_sk = NULL;
1780 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1781 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1785 /* get the certificate types */
1786 if (!PACKET_get_1(pkt, &ctype_num)
1787 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1788 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1789 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1792 OPENSSL_free(s->cert->ctypes);
1793 s->cert->ctypes = NULL;
1794 if (ctype_num > SSL3_CT_NUMBER) {
1795 /* If we exceed static buffer copy all to cert structure */
1796 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1797 if (s->cert->ctypes == NULL) {
1798 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1801 memcpy(s->cert->ctypes, data, ctype_num);
1802 s->cert->ctype_num = ctype_num;
1803 ctype_num = SSL3_CT_NUMBER;
1805 for (i = 0; i < ctype_num; i++)
1806 s->s3->tmp.ctype[i] = data[i];
1808 if (SSL_USE_SIGALGS(s)) {
1809 if (!PACKET_get_net_2(pkt, &list_len)
1810 || !PACKET_get_bytes(pkt, &data, list_len)) {
1811 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1812 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1813 SSL_R_LENGTH_MISMATCH);
1817 /* Clear certificate digests and validity flags */
1818 for (i = 0; i < SSL_PKEY_NUM; i++) {
1819 s->s3->tmp.md[i] = NULL;
1820 s->s3->tmp.valid_flags[i] = 0;
1822 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1823 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1824 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1825 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1828 if (!tls1_process_sigalgs(s)) {
1829 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1830 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1834 ssl_set_default_md(s);
1837 /* get the CA RDNs */
1838 if (!PACKET_get_net_2(pkt, &list_len)
1839 || PACKET_remaining(pkt) != list_len) {
1840 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1841 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1845 while (PACKET_remaining(pkt)) {
1846 if (!PACKET_get_net_2(pkt, &name_len)
1847 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1848 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1849 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1850 SSL_R_LENGTH_MISMATCH);
1854 namestart = namebytes;
1856 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1857 name_len)) == NULL) {
1858 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1859 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1863 if (namebytes != (namestart + name_len)) {
1864 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1865 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1866 SSL_R_CA_DN_LENGTH_MISMATCH);
1869 if (!sk_X509_NAME_push(ca_sk, xn)) {
1870 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1876 /* we should setup a certificate to return.... */
1877 s->s3->tmp.cert_req = 1;
1878 s->s3->tmp.ctype_num = ctype_num;
1879 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1880 s->s3->tmp.ca_names = ca_sk;
1883 ret = MSG_PROCESS_CONTINUE_PROCESSING;
1886 ossl_statem_set_error(s);
1889 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1893 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1895 return (X509_NAME_cmp(*a, *b));
1898 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1901 unsigned int ticklen;
1902 unsigned long ticket_lifetime_hint;
1903 unsigned int sess_len;
1905 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1906 || !PACKET_get_net_2(pkt, &ticklen)
1907 || PACKET_remaining(pkt) != ticklen) {
1908 al = SSL_AD_DECODE_ERROR;
1909 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1913 /* Server is allowed to change its mind and send an empty ticket. */
1915 return MSG_PROCESS_CONTINUE_READING;
1917 if (s->session->session_id_length > 0) {
1918 int i = s->session_ctx->session_cache_mode;
1919 SSL_SESSION *new_sess;
1921 * We reused an existing session, so we need to replace it with a new
1924 if (i & SSL_SESS_CACHE_CLIENT) {
1926 * Remove the old session from the cache. We carry on if this fails
1928 SSL_CTX_remove_session(s->session_ctx, s->session);
1931 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1932 al = SSL_AD_INTERNAL_ERROR;
1933 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1937 SSL_SESSION_free(s->session);
1938 s->session = new_sess;
1941 OPENSSL_free(s->session->tlsext_tick);
1942 s->session->tlsext_ticklen = 0;
1944 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1945 if (s->session->tlsext_tick == NULL) {
1946 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1949 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1950 al = SSL_AD_DECODE_ERROR;
1951 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1955 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1956 s->session->tlsext_ticklen = ticklen;
1958 * There are two ways to detect a resumed ticket session. One is to set
1959 * an appropriate session ID and then the server must return a match in
1960 * ServerHello. This allows the normal client session ID matching to work
1961 * and we know much earlier that the ticket has been accepted. The
1962 * other way is to set zero length session ID when the ticket is
1963 * presented and rely on the handshake to determine session resumption.
1964 * We choose the former approach because this fits in with assumptions
1965 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1966 * SHA256 is disabled) hash of the ticket.
1969 * TODO(size_t): we use sess_len here because EVP_Digest expects an int
1970 * but s->session->session_id_length is a size_t
1972 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
1973 s->session->session_id, &sess_len,
1974 EVP_sha256(), NULL)) {
1975 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
1978 s->session->session_id_length = sess_len;
1979 return MSG_PROCESS_CONTINUE_READING;
1981 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1983 ossl_statem_set_error(s);
1984 return MSG_PROCESS_ERROR;
1987 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1993 if (!PACKET_get_1(pkt, &type)
1994 || type != TLSEXT_STATUSTYPE_ocsp) {
1995 al = SSL_AD_DECODE_ERROR;
1996 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1999 if (!PACKET_get_net_3_len(pkt, &resplen)
2000 || PACKET_remaining(pkt) != resplen) {
2001 al = SSL_AD_DECODE_ERROR;
2002 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2005 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2006 if (s->tlsext_ocsp_resp == NULL) {
2007 al = SSL_AD_INTERNAL_ERROR;
2008 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2011 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
2012 al = SSL_AD_DECODE_ERROR;
2013 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2016 s->tlsext_ocsp_resplen = resplen;
2017 return MSG_PROCESS_CONTINUE_READING;
2019 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2020 ossl_statem_set_error(s);
2021 return MSG_PROCESS_ERROR;
2024 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2026 if (PACKET_remaining(pkt) > 0) {
2027 /* should contain no data */
2028 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2029 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2030 ossl_statem_set_error(s);
2031 return MSG_PROCESS_ERROR;
2033 #ifndef OPENSSL_NO_SRP
2034 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2035 if (SRP_Calc_A_param(s) <= 0) {
2036 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2037 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2038 ossl_statem_set_error(s);
2039 return MSG_PROCESS_ERROR;
2045 * at this point we check that we have the required stuff from
2048 if (!ssl3_check_cert_and_algorithm(s)) {
2049 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2050 ossl_statem_set_error(s);
2051 return MSG_PROCESS_ERROR;
2055 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2056 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2057 * message, or NULL and -1 otherwise
2059 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
2061 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2063 ssl3_send_alert(s, SSL3_AL_FATAL,
2064 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
2065 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
2066 SSL_R_INVALID_STATUS_RESPONSE);
2067 return MSG_PROCESS_ERROR;
2070 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2071 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
2072 return MSG_PROCESS_ERROR;
2075 #ifndef OPENSSL_NO_CT
2076 if (s->ct_validation_callback != NULL) {
2077 /* Note we validate the SCTs whether or not we abort on error */
2078 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2079 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2080 return MSG_PROCESS_ERROR;
2085 #ifndef OPENSSL_NO_SCTP
2086 /* Only applies to renegotiation */
2087 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2088 && s->renegotiate != 0)
2089 return MSG_PROCESS_CONTINUE_PROCESSING;
2092 return MSG_PROCESS_FINISHED_READING;
2095 static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al)
2097 #ifndef OPENSSL_NO_PSK
2100 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2101 * \0-terminated identity. The last byte is for us for simulating
2104 char identity[PSK_MAX_IDENTITY_LEN + 1];
2105 size_t identitylen = 0;
2106 unsigned char psk[PSK_MAX_PSK_LEN];
2107 unsigned char *tmppsk = NULL;
2108 char *tmpidentity = NULL;
2111 if (s->psk_client_callback == NULL) {
2112 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
2113 *al = SSL_AD_INTERNAL_ERROR;
2117 memset(identity, 0, sizeof(identity));
2119 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2120 identity, sizeof(identity) - 1,
2123 if (psklen > PSK_MAX_PSK_LEN) {
2124 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2125 *al = SSL_AD_HANDSHAKE_FAILURE;
2127 } else if (psklen == 0) {
2128 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
2129 SSL_R_PSK_IDENTITY_NOT_FOUND);
2130 *al = SSL_AD_HANDSHAKE_FAILURE;
2134 identitylen = strlen(identity);
2135 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2136 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2137 *al = SSL_AD_HANDSHAKE_FAILURE;
2141 tmppsk = OPENSSL_memdup(psk, psklen);
2142 tmpidentity = OPENSSL_strdup(identity);
2143 if (tmppsk == NULL || tmpidentity == NULL) {
2144 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2145 *al = SSL_AD_INTERNAL_ERROR;
2149 OPENSSL_free(s->s3->tmp.psk);
2150 s->s3->tmp.psk = tmppsk;
2151 s->s3->tmp.psklen = psklen;
2153 OPENSSL_free(s->session->psk_identity);
2154 s->session->psk_identity = tmpidentity;
2157 if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
2158 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2159 *al = SSL_AD_INTERNAL_ERROR;
2166 OPENSSL_cleanse(psk, psklen);
2167 OPENSSL_cleanse(identity, sizeof(identity));
2168 OPENSSL_clear_free(tmppsk, psklen);
2169 OPENSSL_clear_free(tmpidentity, identitylen);
2173 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2174 *al = SSL_AD_INTERNAL_ERROR;
2179 static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)
2181 #ifndef OPENSSL_NO_RSA
2182 unsigned char *encdata = NULL;
2183 EVP_PKEY *pkey = NULL;
2184 EVP_PKEY_CTX *pctx = NULL;
2186 unsigned char *pms = NULL;
2189 if (s->session->peer == NULL) {
2191 * We should always have a server certificate with SSL_kRSA.
2193 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2197 pkey = X509_get0_pubkey(s->session->peer);
2198 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2199 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2203 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2204 pms = OPENSSL_malloc(pmslen);
2206 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
2207 *al = SSL_AD_INTERNAL_ERROR;
2211 pms[0] = s->client_version >> 8;
2212 pms[1] = s->client_version & 0xff;
2213 /* TODO(size_t): Convert this function */
2214 if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
2218 /* Fix buf for TLS and beyond */
2219 if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
2220 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2223 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2224 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2225 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2226 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
2229 if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
2230 || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
2231 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
2234 EVP_PKEY_CTX_free(pctx);
2237 if (s->options & SSL_OP_PKCS1_CHECK_1)
2239 if (s->options & SSL_OP_PKCS1_CHECK_2)
2243 /* Fix buf for TLS and beyond */
2244 if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
2245 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2249 s->s3->tmp.pms = pms;
2250 s->s3->tmp.pmslen = pmslen;
2254 OPENSSL_clear_free(pms, pmslen);
2255 EVP_PKEY_CTX_free(pctx);
2259 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2260 *al = SSL_AD_INTERNAL_ERROR;
2265 static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al)
2267 #ifndef OPENSSL_NO_DH
2269 const BIGNUM *pub_key;
2270 EVP_PKEY *ckey = NULL, *skey = NULL;
2271 unsigned char *keybytes = NULL;
2273 skey = s->s3->peer_tmp;
2277 ckey = ssl_generate_pkey(skey);
2278 dh_clnt = EVP_PKEY_get0_DH(ckey);
2280 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0)
2283 /* send off the data */
2284 DH_get0_key(dh_clnt, &pub_key, NULL);
2285 if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes))
2288 BN_bn2bin(pub_key, keybytes);
2289 EVP_PKEY_free(ckey);
2293 EVP_PKEY_free(ckey);
2295 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2296 *al = SSL_AD_INTERNAL_ERROR;
2300 static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al)
2302 #ifndef OPENSSL_NO_EC
2303 unsigned char *encodedPoint = NULL;
2304 size_t encoded_pt_len = 0;
2305 EVP_PKEY *ckey = NULL, *skey = NULL;
2308 skey = s->s3->peer_tmp;
2310 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2314 ckey = ssl_generate_pkey(skey);
2316 if (ssl_derive(s, ckey, skey) == 0) {
2317 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
2321 /* Generate encoding of client key */
2322 encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
2324 if (encoded_pt_len == 0) {
2325 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
2329 if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
2330 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2336 OPENSSL_free(encodedPoint);
2337 EVP_PKEY_free(ckey);
2340 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2341 *al = SSL_AD_INTERNAL_ERROR;
2346 static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al)
2348 #ifndef OPENSSL_NO_GOST
2349 /* GOST key exchange message creation */
2350 EVP_PKEY_CTX *pkey_ctx = NULL;
2353 unsigned int md_len;
2354 unsigned char shared_ukm[32], tmp[256];
2355 EVP_MD_CTX *ukm_hash = NULL;
2356 int dgst_nid = NID_id_GostR3411_94;
2357 unsigned char *pms = NULL;
2360 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2361 dgst_nid = NID_id_GostR3411_2012_256;
2364 * Get server sertificate PKEY and create ctx from it
2366 peer_cert = s->session->peer;
2368 *al = SSL_AD_HANDSHAKE_FAILURE;
2369 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
2370 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2374 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2375 if (pkey_ctx == NULL) {
2376 *al = SSL_AD_INTERNAL_ERROR;
2377 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2381 * If we have send a certificate, and certificate key
2382 * parameters match those of server certificate, use
2383 * certificate key for key exchange
2386 /* Otherwise, generate ephemeral key pair */
2388 pms = OPENSSL_malloc(pmslen);
2390 *al = SSL_AD_INTERNAL_ERROR;
2391 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2395 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2396 /* Generate session key
2397 * TODO(size_t): Convert this function
2399 || RAND_bytes(pms, (int)pmslen) <= 0) {
2400 *al = SSL_AD_INTERNAL_ERROR;
2401 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2405 * Compute shared IV and store it in algorithm-specific context
2408 ukm_hash = EVP_MD_CTX_new();
2409 if (ukm_hash == NULL
2410 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
2411 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2412 SSL3_RANDOM_SIZE) <= 0
2413 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2414 SSL3_RANDOM_SIZE) <= 0
2415 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2416 *al = SSL_AD_INTERNAL_ERROR;
2417 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2420 EVP_MD_CTX_free(ukm_hash);
2422 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2423 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2424 *al = SSL_AD_INTERNAL_ERROR;
2425 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2428 /* Make GOST keytransport blob message */
2430 * Encapsulate it into sequence
2433 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2434 *al = SSL_AD_INTERNAL_ERROR;
2435 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2439 if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
2440 || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
2441 || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
2442 *al = SSL_AD_INTERNAL_ERROR;
2443 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2447 EVP_PKEY_CTX_free(pkey_ctx);
2448 s->s3->tmp.pms = pms;
2449 s->s3->tmp.pmslen = pmslen;
2453 EVP_PKEY_CTX_free(pkey_ctx);
2454 OPENSSL_clear_free(pms, pmslen);
2455 EVP_MD_CTX_free(ukm_hash);
2458 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2459 *al = SSL_AD_INTERNAL_ERROR;
2464 static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al)
2466 #ifndef OPENSSL_NO_SRP
2467 unsigned char *abytes = NULL;
2469 if (s->srp_ctx.A == NULL
2470 || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
2472 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2475 BN_bn2bin(s->srp_ctx.A, abytes);
2477 OPENSSL_free(s->session->srp_username);
2478 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2479 if (s->session->srp_username == NULL) {
2480 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
2486 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2487 *al = SSL_AD_INTERNAL_ERROR;
2492 int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
2494 unsigned long alg_k;
2497 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2499 if ((alg_k & SSL_PSK)
2500 && !tls_construct_cke_psk_preamble(s, pkt, &al))
2503 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2504 if (!tls_construct_cke_rsa(s, pkt, &al))
2506 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2507 if (!tls_construct_cke_dhe(s, pkt, &al))
2509 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2510 if (!tls_construct_cke_ecdhe(s, pkt, &al))
2512 } else if (alg_k & SSL_kGOST) {
2513 if (!tls_construct_cke_gost(s, pkt, &al))
2515 } else if (alg_k & SSL_kSRP) {
2516 if (!tls_construct_cke_srp(s, pkt, &al))
2518 } else if (!(alg_k & SSL_kPSK)) {
2519 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2520 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2527 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2528 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
2529 s->s3->tmp.pms = NULL;
2530 #ifndef OPENSSL_NO_PSK
2531 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2532 s->s3->tmp.psk = NULL;
2537 int tls_client_key_exchange_post_work(SSL *s)
2539 unsigned char *pms = NULL;
2542 pms = s->s3->tmp.pms;
2543 pmslen = s->s3->tmp.pmslen;
2545 #ifndef OPENSSL_NO_SRP
2547 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2548 if (!srp_generate_client_master_secret(s)) {
2549 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2550 ERR_R_INTERNAL_ERROR);
2557 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2558 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2559 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2562 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2563 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2564 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2565 /* ssl_generate_master_secret frees the pms even on error */
2573 #ifndef OPENSSL_NO_SCTP
2574 if (SSL_IS_DTLS(s)) {
2575 unsigned char sctpauthkey[64];
2576 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2579 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2582 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2583 sizeof(DTLS1_SCTP_AUTH_LABEL));
2585 if (SSL_export_keying_material(s, sctpauthkey,
2586 sizeof(sctpauthkey), labelbuffer,
2587 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2590 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2591 sizeof(sctpauthkey), sctpauthkey);
2597 OPENSSL_clear_free(pms, pmslen);
2598 s->s3->tmp.pms = NULL;
2602 int tls_construct_client_verify(SSL *s, WPACKET *pkt)
2605 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2606 EVP_MD_CTX *mctx = NULL;
2610 unsigned char *sig = NULL;
2612 mctx = EVP_MD_CTX_new();
2614 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2617 pkey = s->cert->key->privatekey;
2619 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2620 if (hdatalen <= 0) {
2621 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2624 if (SSL_USE_SIGALGS(s)&& !tls12_get_sigandhash(pkt, pkey, md)) {
2625 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2629 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2631 sig = OPENSSL_malloc(EVP_PKEY_size(pkey));
2633 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2636 if (!EVP_SignInit_ex(mctx, md, NULL)
2637 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2638 || (s->version == SSL3_VERSION
2639 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2640 (int)s->session->master_key_length,
2641 s->session->master_key))
2642 || !EVP_SignFinal(mctx, sig, &u, pkey)) {
2643 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2646 #ifndef OPENSSL_NO_GOST
2648 int pktype = EVP_PKEY_id(pkey);
2649 if (pktype == NID_id_GostR3410_2001
2650 || pktype == NID_id_GostR3410_2012_256
2651 || pktype == NID_id_GostR3410_2012_512)
2652 BUF_reverse(sig, NULL, u);
2656 if (!WPACKET_sub_memcpy_u16(pkt, sig, u)) {
2657 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2661 /* Digest cached records and discard handshake buffer */
2662 if (!ssl3_digest_cached_records(s, 0))
2666 EVP_MD_CTX_free(mctx);
2670 EVP_MD_CTX_free(mctx);
2671 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2676 * Check a certificate can be used for client authentication. Currently check
2677 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2678 * certificates can be used and optionally checks suitability for Suite B.
2680 static int ssl3_check_client_certificate(SSL *s)
2682 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2684 /* If no suitable signature algorithm can't use certificate */
2685 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2688 * If strict mode check suitability of chain before using it. This also
2689 * adjusts suite B digest if necessary.
2691 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2692 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2697 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2700 EVP_PKEY *pkey = NULL;
2703 if (wst == WORK_MORE_A) {
2704 /* Let cert callback update client certificates if required */
2705 if (s->cert->cert_cb) {
2706 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2708 s->rwstate = SSL_X509_LOOKUP;
2712 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2713 ossl_statem_set_error(s);
2716 s->rwstate = SSL_NOTHING;
2718 if (ssl3_check_client_certificate(s))
2719 return WORK_FINISHED_CONTINUE;
2721 /* Fall through to WORK_MORE_B */
2725 /* We need to get a client cert */
2726 if (wst == WORK_MORE_B) {
2728 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2729 * return(-1); We then get retied later
2731 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2733 s->rwstate = SSL_X509_LOOKUP;
2736 s->rwstate = SSL_NOTHING;
2737 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2738 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2740 } else if (i == 1) {
2742 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2743 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2747 EVP_PKEY_free(pkey);
2748 if (i && !ssl3_check_client_certificate(s))
2751 if (s->version == SSL3_VERSION) {
2752 s->s3->tmp.cert_req = 0;
2753 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2754 return WORK_FINISHED_CONTINUE;
2756 s->s3->tmp.cert_req = 2;
2757 if (!ssl3_digest_cached_records(s, 0)) {
2758 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2759 ossl_statem_set_error(s);
2765 return WORK_FINISHED_CONTINUE;
2768 /* Shouldn't ever get here */
2772 int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
2774 if (!ssl3_output_cert_chain(s, pkt,
2775 (s->s3->tmp.cert_req == 2) ? NULL
2777 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2778 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2785 #define has_bits(i,m) (((i)&(m)) == (m))
2787 int ssl3_check_cert_and_algorithm(SSL *s)
2790 #ifndef OPENSSL_NO_EC
2794 EVP_PKEY *pkey = NULL;
2795 int al = SSL_AD_HANDSHAKE_FAILURE;
2797 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2798 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2800 /* we don't have a certificate */
2801 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2804 /* This is the passed certificate */
2806 #ifndef OPENSSL_NO_EC
2807 idx = s->session->peer_type;
2808 if (idx == SSL_PKEY_ECC) {
2809 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2811 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2816 } else if (alg_a & SSL_aECDSA) {
2817 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2818 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2822 pkey = X509_get0_pubkey(s->session->peer);
2823 i = X509_certificate_type(s->session->peer, pkey);
2825 /* Check that we have a certificate if we require one */
2826 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2827 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2828 SSL_R_MISSING_RSA_SIGNING_CERT);
2831 #ifndef OPENSSL_NO_DSA
2832 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2833 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2834 SSL_R_MISSING_DSA_SIGNING_CERT);
2838 #ifndef OPENSSL_NO_RSA
2839 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2840 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2841 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2842 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2846 #ifndef OPENSSL_NO_DH
2847 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
2848 al = SSL_AD_INTERNAL_ERROR;
2849 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2856 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2860 #ifndef OPENSSL_NO_NEXTPROTONEG
2861 int tls_construct_next_proto(SSL *s, WPACKET *pkt)
2863 size_t len, padding_len;
2864 unsigned char *padding = NULL;
2866 len = s->next_proto_negotiated_len;
2867 padding_len = 32 - ((len + 2) % 32);
2869 if (!WPACKET_sub_memcpy_u8(pkt, s->next_proto_negotiated, len)
2870 || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
2871 SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR);
2875 memset(padding, 0, padding_len);
2879 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2884 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2887 #ifndef OPENSSL_NO_ENGINE
2888 if (s->ctx->client_cert_engine) {
2889 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2890 SSL_get_client_CA_list(s),
2891 px509, ppkey, NULL, NULL, NULL);
2896 if (s->ctx->client_cert_cb)
2897 i = s->ctx->client_cert_cb(s, px509, ppkey);
2901 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
2904 size_t totlen = 0, len, maxlen;
2905 int empty_reneg_info_scsv = !s->renegotiate;
2906 /* Set disabled masks for this session */
2907 ssl_set_client_disabled(s);
2912 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
2913 # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
2914 # error Max cipher length too short
2917 * Some servers hang if client hello > 256 bytes as hack workaround
2918 * chop number of supported ciphers to keep it well below this if we
2921 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2922 maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
2925 /* Maximum length that can be stored in 2 bytes. Length must be even */
2928 if (empty_reneg_info_scsv)
2930 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
2933 for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
2934 const SSL_CIPHER *c;
2936 c = sk_SSL_CIPHER_value(sk, i);
2937 /* Skip disabled ciphers */
2938 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2941 if (!s->method->put_cipher_by_char(c, pkt, &len)) {
2942 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
2950 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE);
2955 if (empty_reneg_info_scsv) {
2956 static SSL_CIPHER scsv = {
2957 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2959 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
2960 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
2964 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2965 static SSL_CIPHER scsv = {
2966 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2968 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
2969 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);