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) {
125 case TLS_ST_CW_CLNT_HELLO:
126 if (mt == SSL3_MT_SERVER_HELLO) {
127 st->hand_state = TLS_ST_CR_SRVR_HELLO;
131 if (SSL_IS_DTLS(s)) {
132 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
133 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
139 case TLS_ST_CR_SRVR_HELLO:
141 if (s->tlsext_ticket_expected) {
142 if (mt == SSL3_MT_NEWSESSION_TICKET) {
143 st->hand_state = TLS_ST_CR_SESSION_TICKET;
146 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
147 st->hand_state = TLS_ST_CR_CHANGE;
151 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
152 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
154 } else if (s->version >= TLS1_VERSION
155 && s->tls_session_secret_cb != NULL
156 && s->session->tlsext_tick != NULL
157 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
159 * Normally, we can tell if the server is resuming the session
160 * from the session ID. EAP-FAST (RFC 4851), however, relies on
161 * the next server message after the ServerHello to determine if
162 * the server is resuming.
165 st->hand_state = TLS_ST_CR_CHANGE;
167 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
168 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
169 if (mt == SSL3_MT_CERTIFICATE) {
170 st->hand_state = TLS_ST_CR_CERT;
174 ske_expected = key_exchange_expected(s);
175 /* SKE is optional for some PSK ciphersuites */
177 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
178 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
179 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
180 st->hand_state = TLS_ST_CR_KEY_EXCH;
183 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
184 && cert_req_allowed(s)) {
185 st->hand_state = TLS_ST_CR_CERT_REQ;
187 } else if (mt == SSL3_MT_SERVER_DONE) {
188 st->hand_state = TLS_ST_CR_SRVR_DONE;
197 * The CertificateStatus message is optional even if
198 * |tlsext_status_expected| is set
200 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
201 st->hand_state = TLS_ST_CR_CERT_STATUS;
206 case TLS_ST_CR_CERT_STATUS:
207 ske_expected = key_exchange_expected(s);
208 /* SKE is optional for some PSK ciphersuites */
209 if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
210 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
211 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
212 st->hand_state = TLS_ST_CR_KEY_EXCH;
219 case TLS_ST_CR_KEY_EXCH:
220 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
221 if (cert_req_allowed(s)) {
222 st->hand_state = TLS_ST_CR_CERT_REQ;
229 case TLS_ST_CR_CERT_REQ:
230 if (mt == SSL3_MT_SERVER_DONE) {
231 st->hand_state = TLS_ST_CR_SRVR_DONE;
236 case TLS_ST_CW_FINISHED:
237 if (s->tlsext_ticket_expected) {
238 if (mt == SSL3_MT_NEWSESSION_TICKET) {
239 st->hand_state = TLS_ST_CR_SESSION_TICKET;
242 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
243 st->hand_state = TLS_ST_CR_CHANGE;
248 case TLS_ST_CR_SESSION_TICKET:
249 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
250 st->hand_state = TLS_ST_CR_CHANGE;
255 case TLS_ST_CR_CHANGE:
256 if (mt == SSL3_MT_FINISHED) {
257 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 /* Renegotiation - fall through */
285 st->hand_state = TLS_ST_CW_CLNT_HELLO;
286 return WRITE_TRAN_CONTINUE;
288 case TLS_ST_CW_CLNT_HELLO:
290 * No transition at the end of writing because we don't know what
293 return WRITE_TRAN_FINISHED;
295 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
296 st->hand_state = TLS_ST_CW_CLNT_HELLO;
297 return WRITE_TRAN_CONTINUE;
299 case TLS_ST_CR_SRVR_DONE:
300 if (s->s3->tmp.cert_req)
301 st->hand_state = TLS_ST_CW_CERT;
303 st->hand_state = TLS_ST_CW_KEY_EXCH;
304 return WRITE_TRAN_CONTINUE;
307 st->hand_state = TLS_ST_CW_KEY_EXCH;
308 return WRITE_TRAN_CONTINUE;
310 case TLS_ST_CW_KEY_EXCH:
312 * For TLS, cert_req is set to 2, so a cert chain of nothing is
313 * sent, but no verify packet is sent
316 * XXX: For now, we do not support client authentication in ECDH
317 * cipher suites with ECDH (rather than ECDSA) certificates. We
318 * need to skip the certificate verify message when client's
319 * ECDH public key is sent inside the client certificate.
321 if (s->s3->tmp.cert_req == 1) {
322 st->hand_state = TLS_ST_CW_CERT_VRFY;
324 st->hand_state = TLS_ST_CW_CHANGE;
326 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
327 st->hand_state = TLS_ST_CW_CHANGE;
329 return WRITE_TRAN_CONTINUE;
331 case TLS_ST_CW_CERT_VRFY:
332 st->hand_state = TLS_ST_CW_CHANGE;
333 return WRITE_TRAN_CONTINUE;
335 case TLS_ST_CW_CHANGE:
336 #if defined(OPENSSL_NO_NEXTPROTONEG)
337 st->hand_state = TLS_ST_CW_FINISHED;
339 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
340 st->hand_state = TLS_ST_CW_NEXT_PROTO;
342 st->hand_state = TLS_ST_CW_FINISHED;
344 return WRITE_TRAN_CONTINUE;
346 #if !defined(OPENSSL_NO_NEXTPROTONEG)
347 case TLS_ST_CW_NEXT_PROTO:
348 st->hand_state = TLS_ST_CW_FINISHED;
349 return WRITE_TRAN_CONTINUE;
352 case TLS_ST_CW_FINISHED:
354 st->hand_state = TLS_ST_OK;
355 ossl_statem_set_in_init(s, 0);
356 return WRITE_TRAN_CONTINUE;
358 return WRITE_TRAN_FINISHED;
361 case TLS_ST_CR_FINISHED:
363 st->hand_state = TLS_ST_CW_CHANGE;
364 return WRITE_TRAN_CONTINUE;
366 st->hand_state = TLS_ST_OK;
367 ossl_statem_set_in_init(s, 0);
368 return WRITE_TRAN_CONTINUE;
372 /* Shouldn't happen */
373 return WRITE_TRAN_ERROR;
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) {
386 case TLS_ST_CW_CLNT_HELLO:
388 if (SSL_IS_DTLS(s)) {
389 /* every DTLS ClientHello resets Finished MAC */
390 if (!ssl3_init_finished_mac(s)) {
391 ossl_statem_set_error(s);
397 case TLS_ST_CW_CHANGE:
398 if (SSL_IS_DTLS(s)) {
401 * We're into the last flight so we don't retransmit these
402 * messages unless we need to.
406 #ifndef OPENSSL_NO_SCTP
407 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
408 return dtls_wait_for_dry(s);
411 return WORK_FINISHED_CONTINUE;
414 return tls_finish_handshake(s, wst);
417 /* No pre work to be done */
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) {
435 case TLS_ST_CW_CLNT_HELLO:
436 if (wst == WORK_MORE_A && statem_flush(s) != 1)
439 if (SSL_IS_DTLS(s)) {
440 /* Treat the next message as the first packet */
445 case TLS_ST_CW_KEY_EXCH:
446 if (tls_client_key_exchange_post_work(s) == 0)
450 case TLS_ST_CW_CHANGE:
451 s->session->cipher = s->s3->tmp.new_cipher;
452 #ifdef OPENSSL_NO_COMP
453 s->session->compress_meth = 0;
455 if (s->s3->tmp.new_compression == NULL)
456 s->session->compress_meth = 0;
458 s->session->compress_meth = s->s3->tmp.new_compression->id;
460 if (!s->method->ssl3_enc->setup_key_block(s))
463 if (!s->method->ssl3_enc->change_cipher_state(s,
464 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
467 if (SSL_IS_DTLS(s)) {
468 #ifndef OPENSSL_NO_SCTP
471 * Change to new shared key of SCTP-Auth, will be ignored if
474 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
479 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
483 case TLS_ST_CW_FINISHED:
484 #ifndef OPENSSL_NO_SCTP
485 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
487 * Change to new shared key of SCTP-Auth, will be ignored if
490 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
494 if (statem_flush(s) != 1)
499 /* No post work to be done */
503 return WORK_FINISHED_CONTINUE;
507 * Construct a message to be sent from the client to the server.
509 * Valid return values are:
513 int ossl_statem_client_construct_message(SSL *s)
515 OSSL_STATEM *st = &s->statem;
517 switch (st->hand_state) {
518 case TLS_ST_CW_CLNT_HELLO:
519 return tls_construct_client_hello(s);
522 return tls_construct_client_certificate(s);
524 case TLS_ST_CW_KEY_EXCH:
525 return tls_construct_client_key_exchange(s);
527 case TLS_ST_CW_CERT_VRFY:
528 return tls_construct_client_verify(s);
530 case TLS_ST_CW_CHANGE:
532 return dtls_construct_change_cipher_spec(s);
534 return tls_construct_change_cipher_spec(s);
536 #if !defined(OPENSSL_NO_NEXTPROTONEG)
537 case TLS_ST_CW_NEXT_PROTO:
538 return tls_construct_next_proto(s);
540 case TLS_ST_CW_FINISHED:
541 return tls_construct_finished(s,
543 ssl3_enc->client_finished_label,
545 ssl3_enc->client_finished_label_len);
548 /* Shouldn't happen */
556 * Returns the maximum allowed length for the current message that we are
557 * reading. Excludes the message header.
559 unsigned long ossl_statem_client_max_message_size(SSL *s)
561 OSSL_STATEM *st = &s->statem;
563 switch (st->hand_state) {
564 case TLS_ST_CR_SRVR_HELLO:
565 return SERVER_HELLO_MAX_LENGTH;
567 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
568 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
571 return s->max_cert_list;
573 case TLS_ST_CR_CERT_STATUS:
574 return SSL3_RT_MAX_PLAIN_LENGTH;
576 case TLS_ST_CR_KEY_EXCH:
577 return SERVER_KEY_EXCH_MAX_LENGTH;
579 case TLS_ST_CR_CERT_REQ:
581 * Set to s->max_cert_list for compatibility with previous releases. In
582 * practice these messages can get quite long if servers are configured
583 * to provide a long list of acceptable CAs
585 return s->max_cert_list;
587 case TLS_ST_CR_SRVR_DONE:
588 return SERVER_HELLO_DONE_MAX_LENGTH;
590 case TLS_ST_CR_CHANGE:
591 if (s->version == DTLS1_BAD_VER)
593 return CCS_MAX_LENGTH;
595 case TLS_ST_CR_SESSION_TICKET:
596 return SSL3_RT_MAX_PLAIN_LENGTH;
598 case TLS_ST_CR_FINISHED:
599 return FINISHED_MAX_LENGTH;
602 /* Shouldn't happen */
610 * Process a message that the client has been received from the server.
612 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
614 OSSL_STATEM *st = &s->statem;
616 switch (st->hand_state) {
617 case TLS_ST_CR_SRVR_HELLO:
618 return tls_process_server_hello(s, pkt);
620 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
621 return dtls_process_hello_verify(s, pkt);
624 return tls_process_server_certificate(s, pkt);
626 case TLS_ST_CR_CERT_STATUS:
627 return tls_process_cert_status(s, pkt);
629 case TLS_ST_CR_KEY_EXCH:
630 return tls_process_key_exchange(s, pkt);
632 case TLS_ST_CR_CERT_REQ:
633 return tls_process_certificate_request(s, pkt);
635 case TLS_ST_CR_SRVR_DONE:
636 return tls_process_server_done(s, pkt);
638 case TLS_ST_CR_CHANGE:
639 return tls_process_change_cipher_spec(s, pkt);
641 case TLS_ST_CR_SESSION_TICKET:
642 return tls_process_new_session_ticket(s, pkt);
644 case TLS_ST_CR_FINISHED:
645 return tls_process_finished(s, pkt);
648 /* Shouldn't happen */
652 return MSG_PROCESS_ERROR;
656 * Perform any further processing required following the receipt of a message
659 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
661 OSSL_STATEM *st = &s->statem;
663 switch (st->hand_state) {
664 case TLS_ST_CR_CERT_REQ:
665 return tls_prepare_client_certificate(s, wst);
667 #ifndef OPENSSL_NO_SCTP
668 case TLS_ST_CR_SRVR_DONE:
669 /* We only get here if we are using SCTP and we are renegotiating */
670 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
671 s->s3->in_read_app_data = 2;
672 s->rwstate = SSL_READING;
673 BIO_clear_retry_flags(SSL_get_rbio(s));
674 BIO_set_retry_read(SSL_get_rbio(s));
675 ossl_statem_set_sctp_read_sock(s, 1);
678 ossl_statem_set_sctp_read_sock(s, 0);
679 return WORK_FINISHED_STOP;
686 /* Shouldn't happen */
690 int tls_construct_client_hello(SSL *s)
693 unsigned char *p, *d;
698 #ifndef OPENSSL_NO_COMP
702 SSL_SESSION *sess = s->session;
704 buf = (unsigned char *)s->init_buf->data;
706 /* Work out what SSL/TLS/DTLS version to use */
707 protverr = ssl_set_client_hello_version(s);
709 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
713 if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
715 * In the case of EAP-FAST, we can have a pre-shared
716 * "ticket" without a session ID.
718 (!sess->session_id_length && !sess->tlsext_tick) ||
719 (sess->not_resumable)) {
720 if (!ssl_get_new_session(s, 0))
723 /* else use the pre-loaded session */
725 p = s->s3->client_random;
728 * for DTLS if client_random is initialized, reuse it, we are
729 * required to use same upon reply to HelloVerify
731 if (SSL_IS_DTLS(s)) {
734 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
743 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
746 /* Do the message type and length last */
747 d = p = ssl_handshake_start(s);
750 * version indicates the negotiated version: for example from
751 * an SSLv2/v3 compatible client hello). The client_version
752 * field is the maximum version we permit and it is also
753 * used in RSA encrypted premaster secrets. Some servers can
754 * choke if we initially report a higher version then
755 * renegotiate to a lower one in the premaster secret. This
756 * didn't happen with TLS 1.0 as most servers supported it
757 * but it can with TLS 1.1 or later if the server only supports
760 * Possible scenario with previous logic:
761 * 1. Client hello indicates TLS 1.2
762 * 2. Server hello says TLS 1.0
763 * 3. RSA encrypted premaster secret uses 1.2.
764 * 4. Handshake proceeds using TLS 1.0.
765 * 5. Server sends hello request to renegotiate.
766 * 6. Client hello indicates TLS v1.0 as we now
767 * know that is maximum server supports.
768 * 7. Server chokes on RSA encrypted premaster secret
769 * containing version 1.0.
771 * For interoperability it should be OK to always use the
772 * maximum version we support in client hello and then rely
773 * on the checking of version to ensure the servers isn't
774 * being inconsistent: for example initially negotiating with
775 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
776 * client_version in client hello and not resetting it to
777 * the negotiated version.
779 *(p++) = s->client_version >> 8;
780 *(p++) = s->client_version & 0xff;
783 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
784 p += SSL3_RANDOM_SIZE;
790 i = s->session->session_id_length;
793 if (i > (int)sizeof(s->session->session_id)) {
794 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
797 memcpy(p, s->session->session_id, i);
801 /* cookie stuff for DTLS */
802 if (SSL_IS_DTLS(s)) {
803 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
804 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
807 *(p++) = s->d1->cookie_len;
808 memcpy(p, s->d1->cookie, s->d1->cookie_len);
809 p += s->d1->cookie_len;
812 /* Ciphers supported */
813 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
815 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
818 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
820 * Some servers hang if client hello > 256 bytes as hack workaround
821 * chop number of supported ciphers to keep it well below this if we
824 if (TLS1_get_version(s) >= TLS1_2_VERSION
825 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
826 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
832 #ifdef OPENSSL_NO_COMP
836 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
839 j = sk_SSL_COMP_num(s->ctx->comp_methods);
841 for (i = 0; i < j; i++) {
842 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
846 *(p++) = 0; /* Add the NULL method */
849 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
850 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
854 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
856 ssl3_send_alert(s, SSL3_AL_FATAL, al);
857 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
862 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
863 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
864 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
870 ossl_statem_set_error(s);
874 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
877 unsigned int cookie_len;
880 if (!PACKET_forward(pkt, 2)
881 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
882 al = SSL_AD_DECODE_ERROR;
883 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
887 cookie_len = PACKET_remaining(&cookiepkt);
888 if (cookie_len > sizeof(s->d1->cookie)) {
889 al = SSL_AD_ILLEGAL_PARAMETER;
890 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
894 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
895 al = SSL_AD_DECODE_ERROR;
896 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
899 s->d1->cookie_len = cookie_len;
901 return MSG_PROCESS_FINISHED_READING;
903 ssl3_send_alert(s, SSL3_AL_FATAL, al);
904 ossl_statem_set_error(s);
905 return MSG_PROCESS_ERROR;
908 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
910 STACK_OF(SSL_CIPHER) *sk;
913 size_t session_id_len;
914 const unsigned char *cipherchars;
915 int i, al = SSL_AD_INTERNAL_ERROR;
916 unsigned int compression;
917 unsigned int sversion;
919 #ifndef OPENSSL_NO_COMP
923 if (!PACKET_get_net_2(pkt, &sversion)) {
924 al = SSL_AD_DECODE_ERROR;
925 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
929 protverr = ssl_choose_client_version(s, sversion);
931 al = SSL_AD_PROTOCOL_VERSION;
932 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
936 /* load the server hello data */
937 /* load the server random */
938 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
939 al = SSL_AD_DECODE_ERROR;
940 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
946 /* Get the session-id. */
947 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
948 al = SSL_AD_DECODE_ERROR;
949 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
952 session_id_len = PACKET_remaining(&session_id);
953 if (session_id_len > sizeof s->session->session_id
954 || session_id_len > SSL3_SESSION_ID_SIZE) {
955 al = SSL_AD_ILLEGAL_PARAMETER;
956 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
960 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
961 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
962 al = SSL_AD_DECODE_ERROR;
967 * Check if we can resume the session based on external pre-shared secret.
968 * EAP-FAST (RFC 4851) supports two types of session resumption.
969 * Resumption based on server-side state works with session IDs.
970 * Resumption based on pre-shared Protected Access Credentials (PACs)
971 * works by overriding the SessionTicket extension at the application
972 * layer, and does not send a session ID. (We do not know whether EAP-FAST
973 * servers would honour the session ID.) Therefore, the session ID alone
974 * is not a reliable indicator of session resumption, so we first check if
975 * we can resume, and later peek at the next handshake message to see if the
976 * server wants to resume.
978 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
979 s->session->tlsext_tick) {
980 const SSL_CIPHER *pref_cipher = NULL;
981 s->session->master_key_length = sizeof(s->session->master_key);
982 if (s->tls_session_secret_cb(s, s->session->master_key,
983 &s->session->master_key_length,
985 s->tls_session_secret_cb_arg)) {
986 s->session->cipher = pref_cipher ?
987 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
989 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
990 al = SSL_AD_INTERNAL_ERROR;
995 if (session_id_len != 0 && session_id_len == s->session->session_id_length
996 && memcmp(PACKET_data(&session_id), s->session->session_id,
997 session_id_len) == 0) {
998 if (s->sid_ctx_length != s->session->sid_ctx_length
999 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1000 /* actually a client application bug */
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1003 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1009 * If we were trying for session-id reuse but the server
1010 * didn't echo the ID, make a new SSL_SESSION.
1011 * In the case of EAP-FAST and PAC, we do not send a session ID,
1012 * so the PAC-based session secret is always preserved. It'll be
1013 * overwritten if the server refuses resumption.
1015 if (s->session->session_id_length > 0) {
1016 s->ctx->stats.sess_miss++;
1017 if (!ssl_get_new_session(s, 0)) {
1022 s->session->ssl_version = s->version;
1023 s->session->session_id_length = session_id_len;
1024 /* session_id_len could be 0 */
1025 memcpy(s->session->session_id, PACKET_data(&session_id),
1029 /* Session version and negotiated protocol version should match */
1030 if (s->version != s->session->ssl_version) {
1031 al = SSL_AD_PROTOCOL_VERSION;
1033 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1034 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1038 c = ssl_get_cipher_by_char(s, cipherchars);
1040 /* unknown cipher */
1041 al = SSL_AD_ILLEGAL_PARAMETER;
1042 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1046 * Now that we know the version, update the check to see if it's an allowed
1049 s->s3->tmp.min_ver = s->version;
1050 s->s3->tmp.max_ver = s->version;
1052 * If it is a disabled cipher we either didn't send it in client hello,
1053 * or it's not allowed for the selected protocol. So we return an error.
1055 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1056 al = SSL_AD_ILLEGAL_PARAMETER;
1057 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1061 sk = ssl_get_ciphers_by_id(s);
1062 i = sk_SSL_CIPHER_find(sk, c);
1064 /* we did not say we would use this cipher */
1065 al = SSL_AD_ILLEGAL_PARAMETER;
1066 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1071 * Depending on the session caching (internal/external), the cipher
1072 * and/or cipher_id values may not be set. Make sure that cipher_id is
1073 * set and use it for comparison.
1075 if (s->session->cipher)
1076 s->session->cipher_id = s->session->cipher->id;
1077 if (s->hit && (s->session->cipher_id != c->id)) {
1078 al = SSL_AD_ILLEGAL_PARAMETER;
1079 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1080 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1083 s->s3->tmp.new_cipher = c;
1084 /* lets get the compression algorithm */
1086 if (!PACKET_get_1(pkt, &compression)) {
1087 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1088 al = SSL_AD_DECODE_ERROR;
1091 #ifdef OPENSSL_NO_COMP
1092 if (compression != 0) {
1093 al = SSL_AD_ILLEGAL_PARAMETER;
1094 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1095 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1099 * If compression is disabled we'd better not try to resume a session
1100 * using compression.
1102 if (s->session->compress_meth != 0) {
1103 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1107 if (s->hit && compression != s->session->compress_meth) {
1108 al = SSL_AD_ILLEGAL_PARAMETER;
1109 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1110 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1113 if (compression == 0)
1115 else if (!ssl_allow_compression(s)) {
1116 al = SSL_AD_ILLEGAL_PARAMETER;
1117 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1120 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1123 if (compression != 0 && comp == NULL) {
1124 al = SSL_AD_ILLEGAL_PARAMETER;
1125 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1126 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1129 s->s3->tmp.new_compression = comp;
1133 /* TLS extensions */
1134 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1135 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1139 if (PACKET_remaining(pkt) != 0) {
1140 /* wrong packet length */
1141 al = SSL_AD_DECODE_ERROR;
1142 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1145 #ifndef OPENSSL_NO_SCTP
1146 if (SSL_IS_DTLS(s) && s->hit) {
1147 unsigned char sctpauthkey[64];
1148 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1151 * Add new shared key for SCTP-Auth, will be ignored if
1154 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1155 sizeof(DTLS1_SCTP_AUTH_LABEL));
1157 if (SSL_export_keying_material(s, sctpauthkey,
1158 sizeof(sctpauthkey),
1160 sizeof(labelbuffer), NULL, 0, 0) <= 0)
1163 BIO_ctrl(SSL_get_wbio(s),
1164 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1165 sizeof(sctpauthkey), sctpauthkey);
1169 return MSG_PROCESS_CONTINUE_READING;
1171 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1173 ossl_statem_set_error(s);
1174 return MSG_PROCESS_ERROR;
1177 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1179 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1180 unsigned long cert_list_len, cert_len;
1182 const unsigned char *certstart, *certbytes;
1183 STACK_OF(X509) *sk = NULL;
1184 EVP_PKEY *pkey = NULL;
1186 if ((sk = sk_X509_new_null()) == NULL) {
1187 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1191 if (!PACKET_get_net_3(pkt, &cert_list_len)
1192 || PACKET_remaining(pkt) != cert_list_len) {
1193 al = SSL_AD_DECODE_ERROR;
1194 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1197 while (PACKET_remaining(pkt)) {
1198 if (!PACKET_get_net_3(pkt, &cert_len)
1199 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1200 al = SSL_AD_DECODE_ERROR;
1201 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1202 SSL_R_CERT_LENGTH_MISMATCH);
1206 certstart = certbytes;
1207 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1209 al = SSL_AD_BAD_CERTIFICATE;
1210 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1213 if (certbytes != (certstart + cert_len)) {
1214 al = SSL_AD_DECODE_ERROR;
1215 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1216 SSL_R_CERT_LENGTH_MISMATCH);
1219 if (!sk_X509_push(sk, x)) {
1220 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1226 i = ssl_verify_cert_chain(s, sk);
1228 * The documented interface is that SSL_VERIFY_PEER should be set in order
1229 * for client side verification of the server certificate to take place.
1230 * However, historically the code has only checked that *any* flag is set
1231 * to cause server verification to take place. Use of the other flags makes
1232 * no sense in client mode. An attempt to clean up the semantics was
1233 * reverted because at least one application *only* set
1234 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1235 * server verification to take place, after the clean up it silently did
1236 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1237 * sent to them because they are void functions. Therefore, we now use the
1238 * (less clean) historic behaviour of performing validation if any flag is
1239 * set. The *documented* interface remains the same.
1241 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1242 al = ssl_verify_alarm_type(s->verify_result);
1243 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1244 SSL_R_CERTIFICATE_VERIFY_FAILED);
1247 ERR_clear_error(); /* but we keep s->verify_result */
1249 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1250 al = SSL_AD_HANDSHAKE_FAILURE;
1254 s->session->peer_chain = sk;
1256 * Inconsistency alert: cert_chain does include the peer's certificate,
1257 * which we don't include in statem_srvr.c
1259 x = sk_X509_value(sk, 0);
1262 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1265 pkey = X509_get0_pubkey(x);
1267 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1270 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1271 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1275 i = ssl_cert_type(x, pkey);
1279 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1280 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1284 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1285 if (exp_idx >= 0 && i != exp_idx
1286 && (exp_idx != SSL_PKEY_GOST_EC ||
1287 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1288 && i != SSL_PKEY_GOST01))) {
1290 al = SSL_AD_ILLEGAL_PARAMETER;
1291 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1292 SSL_R_WRONG_CERTIFICATE_TYPE);
1295 s->session->peer_type = i;
1297 X509_free(s->session->peer);
1299 s->session->peer = x;
1300 s->session->verify_result = s->verify_result;
1303 ret = MSG_PROCESS_CONTINUE_READING;
1307 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1309 ossl_statem_set_error(s);
1312 sk_X509_pop_free(sk, X509_free);
1316 static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
1318 #ifndef OPENSSL_NO_PSK
1319 PACKET psk_identity_hint;
1321 /* PSK ciphersuites are preceded by an identity hint */
1323 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1324 *al = SSL_AD_DECODE_ERROR;
1325 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
1330 * Store PSK identity hint for later use, hint is used in
1331 * tls_construct_client_key_exchange. Assume that the maximum length of
1332 * a PSK identity hint can be as long as the maximum length of a PSK
1335 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1336 *al = SSL_AD_HANDSHAKE_FAILURE;
1337 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
1341 if (PACKET_remaining(&psk_identity_hint) == 0) {
1342 OPENSSL_free(s->session->psk_identity_hint);
1343 s->session->psk_identity_hint = NULL;
1344 } else if (!PACKET_strndup(&psk_identity_hint,
1345 &s->session->psk_identity_hint)) {
1346 *al = SSL_AD_INTERNAL_ERROR;
1352 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
1353 *al = SSL_AD_INTERNAL_ERROR;
1358 static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1360 #ifndef OPENSSL_NO_SRP
1361 PACKET prime, generator, salt, server_pub;
1363 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1364 || !PACKET_get_length_prefixed_2(pkt, &generator)
1365 || !PACKET_get_length_prefixed_1(pkt, &salt)
1366 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1367 *al = SSL_AD_DECODE_ERROR;
1368 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
1373 BN_bin2bn(PACKET_data(&prime),
1374 PACKET_remaining(&prime), NULL)) == NULL
1376 BN_bin2bn(PACKET_data(&generator),
1377 PACKET_remaining(&generator), NULL)) == NULL
1379 BN_bin2bn(PACKET_data(&salt),
1380 PACKET_remaining(&salt), NULL)) == NULL
1382 BN_bin2bn(PACKET_data(&server_pub),
1383 PACKET_remaining(&server_pub), NULL)) == NULL) {
1384 *al = SSL_AD_INTERNAL_ERROR;
1385 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
1389 if (!srp_verify_server_param(s, al)) {
1390 *al = SSL_AD_DECODE_ERROR;
1391 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
1395 /* We must check if there is a certificate */
1396 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1397 *pkey = X509_get0_pubkey(s->session->peer);
1401 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
1402 *al = SSL_AD_INTERNAL_ERROR;
1407 static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1409 #ifndef OPENSSL_NO_DH
1410 PACKET prime, generator, pub_key;
1411 EVP_PKEY *peer_tmp = NULL;
1414 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1416 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1417 || !PACKET_get_length_prefixed_2(pkt, &generator)
1418 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1419 *al = SSL_AD_DECODE_ERROR;
1420 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
1424 peer_tmp = EVP_PKEY_new();
1427 if (peer_tmp == NULL || dh == NULL) {
1428 *al = SSL_AD_INTERNAL_ERROR;
1429 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
1433 p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL);
1434 g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator), NULL);
1435 bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key),
1437 if (p == NULL || g == NULL || bnpub_key == NULL) {
1438 *al = SSL_AD_INTERNAL_ERROR;
1439 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1443 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1444 *al = SSL_AD_DECODE_ERROR;
1445 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
1449 if (!DH_set0_pqg(dh, p, NULL, g)) {
1450 *al = SSL_AD_INTERNAL_ERROR;
1451 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1456 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1457 *al = SSL_AD_INTERNAL_ERROR;
1458 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1463 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1464 *al = SSL_AD_HANDSHAKE_FAILURE;
1465 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
1469 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1470 *al = SSL_AD_INTERNAL_ERROR;
1471 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
1475 s->s3->peer_tmp = peer_tmp;
1478 * FIXME: This makes assumptions about which ciphersuites come with
1479 * public keys. We should have a less ad-hoc way of doing this
1481 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1482 *pkey = X509_get0_pubkey(s->session->peer);
1483 /* else anonymous DH, so no certificate or pkey. */
1492 EVP_PKEY_free(peer_tmp);
1496 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
1497 *al = SSL_AD_INTERNAL_ERROR;
1502 static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1504 #ifndef OPENSSL_NO_EC
1506 const unsigned char *ecparams;
1508 unsigned int curve_flags;
1509 EVP_PKEY_CTX *pctx = NULL;
1512 * Extract elliptic curve parameters and the server's ephemeral ECDH
1513 * public key. For now we only support named (not generic) curves and
1514 * ECParameters in this case is just three bytes.
1516 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1517 *al = SSL_AD_DECODE_ERROR;
1518 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
1522 * Check curve is one of our preferences, if not server has sent an
1523 * invalid curve. ECParameters is 3 bytes.
1525 if (!tls1_check_curve(s, ecparams, 3)) {
1526 *al = SSL_AD_DECODE_ERROR;
1527 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
1531 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
1533 if (curve_nid == 0) {
1534 *al = SSL_AD_INTERNAL_ERROR;
1535 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
1536 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1540 if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
1541 EVP_PKEY *key = EVP_PKEY_new();
1543 if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
1544 *al = SSL_AD_INTERNAL_ERROR;
1545 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1549 s->s3->peer_tmp = key;
1551 /* Set up EVP_PKEY with named curve as parameters */
1552 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1554 || EVP_PKEY_paramgen_init(pctx) <= 0
1555 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1556 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1557 *al = SSL_AD_INTERNAL_ERROR;
1558 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1559 EVP_PKEY_CTX_free(pctx);
1562 EVP_PKEY_CTX_free(pctx);
1566 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1567 *al = SSL_AD_DECODE_ERROR;
1568 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
1572 if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
1573 PACKET_data(&encoded_pt),
1574 PACKET_remaining(&encoded_pt))) {
1575 *al = SSL_AD_DECODE_ERROR;
1576 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
1581 * The ECC/TLS specification does not mention the use of DSA to sign
1582 * ECParameters in the server key exchange message. We do support RSA
1585 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
1586 *pkey = X509_get0_pubkey(s->session->peer);
1587 else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
1588 *pkey = X509_get0_pubkey(s->session->peer);
1589 /* else anonymous ECDH, so no certificate or pkey. */
1593 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
1594 *al = SSL_AD_INTERNAL_ERROR;
1599 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1603 EVP_PKEY *pkey = NULL;
1604 PACKET save_param_start, signature;
1606 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1608 save_param_start = *pkt;
1610 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1611 EVP_PKEY_free(s->s3->peer_tmp);
1612 s->s3->peer_tmp = NULL;
1615 if (alg_k & SSL_PSK) {
1616 if (!tls_process_ske_psk_preamble(s, pkt, &al))
1620 /* Nothing else to do for plain PSK or RSAPSK */
1621 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1622 } else if (alg_k & SSL_kSRP) {
1623 if (!tls_process_ske_srp(s, pkt, &pkey, &al))
1625 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1626 if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
1628 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1629 if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
1632 al = SSL_AD_UNEXPECTED_MESSAGE;
1633 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1637 /* if it was signed, check the signature */
1641 const EVP_MD *md = NULL;
1645 * |pkt| now points to the beginning of the signature, so the difference
1646 * equals the length of the parameters.
1648 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1649 PACKET_remaining(&save_param_start) -
1650 PACKET_remaining(pkt))) {
1651 al = SSL_AD_INTERNAL_ERROR;
1652 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1656 if (SSL_USE_SIGALGS(s)) {
1657 const unsigned char *sigalgs;
1659 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1660 al = SSL_AD_DECODE_ERROR;
1661 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1664 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1666 al = SSL_AD_INTERNAL_ERROR;
1668 } else if (rv == 0) {
1669 al = SSL_AD_DECODE_ERROR;
1673 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1675 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1676 md = EVP_md5_sha1();
1681 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1682 || PACKET_remaining(pkt) != 0) {
1683 al = SSL_AD_DECODE_ERROR;
1684 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1687 maxsig = EVP_PKEY_size(pkey);
1689 al = SSL_AD_INTERNAL_ERROR;
1690 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1695 * Check signature length
1697 if (PACKET_remaining(&signature) > (size_t)maxsig) {
1698 /* wrong packet length */
1699 al = SSL_AD_DECODE_ERROR;
1700 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1701 SSL_R_WRONG_SIGNATURE_LENGTH);
1705 md_ctx = EVP_MD_CTX_new();
1706 if (md_ctx == NULL) {
1707 al = SSL_AD_INTERNAL_ERROR;
1708 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1712 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1713 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1714 SSL3_RANDOM_SIZE) <= 0
1715 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1716 SSL3_RANDOM_SIZE) <= 0
1717 || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms),
1718 PACKET_remaining(¶ms)) <= 0) {
1719 EVP_MD_CTX_free(md_ctx);
1720 al = SSL_AD_INTERNAL_ERROR;
1721 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1724 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1725 PACKET_remaining(&signature), pkey) <= 0) {
1727 EVP_MD_CTX_free(md_ctx);
1728 al = SSL_AD_DECRYPT_ERROR;
1729 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1732 EVP_MD_CTX_free(md_ctx);
1734 /* aNULL, aSRP or PSK do not need public keys */
1735 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1736 && !(alg_k & SSL_PSK)) {
1737 /* Might be wrong key type, check it */
1738 if (ssl3_check_cert_and_algorithm(s)) {
1739 /* Otherwise this shouldn't happen */
1740 al = SSL_AD_INTERNAL_ERROR;
1741 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1743 al = SSL_AD_DECODE_ERROR;
1747 /* still data left over */
1748 if (PACKET_remaining(pkt) != 0) {
1749 al = SSL_AD_DECODE_ERROR;
1750 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1755 return MSG_PROCESS_CONTINUE_READING;
1758 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1759 ossl_statem_set_error(s);
1760 return MSG_PROCESS_ERROR;
1763 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1765 int ret = MSG_PROCESS_ERROR;
1766 unsigned int list_len, ctype_num, i, name_len;
1767 X509_NAME *xn = NULL;
1768 const unsigned char *data;
1769 const unsigned char *namestart, *namebytes;
1770 STACK_OF(X509_NAME) *ca_sk = NULL;
1772 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1773 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1777 /* get the certificate types */
1778 if (!PACKET_get_1(pkt, &ctype_num)
1779 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1780 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1781 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1784 OPENSSL_free(s->cert->ctypes);
1785 s->cert->ctypes = NULL;
1786 if (ctype_num > SSL3_CT_NUMBER) {
1787 /* If we exceed static buffer copy all to cert structure */
1788 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1789 if (s->cert->ctypes == NULL) {
1790 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1793 memcpy(s->cert->ctypes, data, ctype_num);
1794 s->cert->ctype_num = (size_t)ctype_num;
1795 ctype_num = SSL3_CT_NUMBER;
1797 for (i = 0; i < ctype_num; i++)
1798 s->s3->tmp.ctype[i] = data[i];
1800 if (SSL_USE_SIGALGS(s)) {
1801 if (!PACKET_get_net_2(pkt, &list_len)
1802 || !PACKET_get_bytes(pkt, &data, list_len)) {
1803 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1804 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1805 SSL_R_LENGTH_MISMATCH);
1809 /* Clear certificate digests and validity flags */
1810 for (i = 0; i < SSL_PKEY_NUM; i++) {
1811 s->s3->tmp.md[i] = NULL;
1812 s->s3->tmp.valid_flags[i] = 0;
1814 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1815 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1816 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1817 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1820 if (!tls1_process_sigalgs(s)) {
1821 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1822 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1826 ssl_set_default_md(s);
1829 /* get the CA RDNs */
1830 if (!PACKET_get_net_2(pkt, &list_len)
1831 || PACKET_remaining(pkt) != list_len) {
1832 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1833 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1837 while (PACKET_remaining(pkt)) {
1838 if (!PACKET_get_net_2(pkt, &name_len)
1839 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1840 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1841 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1842 SSL_R_LENGTH_MISMATCH);
1846 namestart = namebytes;
1848 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1849 name_len)) == NULL) {
1850 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1851 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1855 if (namebytes != (namestart + name_len)) {
1856 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1857 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1858 SSL_R_CA_DN_LENGTH_MISMATCH);
1861 if (!sk_X509_NAME_push(ca_sk, xn)) {
1862 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1868 /* we should setup a certificate to return.... */
1869 s->s3->tmp.cert_req = 1;
1870 s->s3->tmp.ctype_num = ctype_num;
1871 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1872 s->s3->tmp.ca_names = ca_sk;
1875 ret = MSG_PROCESS_CONTINUE_PROCESSING;
1878 ossl_statem_set_error(s);
1881 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1885 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1887 return (X509_NAME_cmp(*a, *b));
1890 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1893 unsigned int ticklen;
1894 unsigned long ticket_lifetime_hint;
1896 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1897 || !PACKET_get_net_2(pkt, &ticklen)
1898 || PACKET_remaining(pkt) != ticklen) {
1899 al = SSL_AD_DECODE_ERROR;
1900 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1904 /* Server is allowed to change its mind and send an empty ticket. */
1906 return MSG_PROCESS_CONTINUE_READING;
1908 if (s->session->session_id_length > 0) {
1909 int i = s->session_ctx->session_cache_mode;
1910 SSL_SESSION *new_sess;
1912 * We reused an existing session, so we need to replace it with a new
1915 if (i & SSL_SESS_CACHE_CLIENT) {
1917 * Remove the old session from the cache. We carry on if this fails
1919 SSL_CTX_remove_session(s->session_ctx, s->session);
1922 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1923 al = SSL_AD_INTERNAL_ERROR;
1924 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1928 SSL_SESSION_free(s->session);
1929 s->session = new_sess;
1932 OPENSSL_free(s->session->tlsext_tick);
1933 s->session->tlsext_ticklen = 0;
1935 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1936 if (s->session->tlsext_tick == NULL) {
1937 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1940 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1941 al = SSL_AD_DECODE_ERROR;
1942 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1946 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1947 s->session->tlsext_ticklen = ticklen;
1949 * There are two ways to detect a resumed ticket session. One is to set
1950 * an appropriate session ID and then the server must return a match in
1951 * ServerHello. This allows the normal client session ID matching to work
1952 * and we know much earlier that the ticket has been accepted. The
1953 * other way is to set zero length session ID when the ticket is
1954 * presented and rely on the handshake to determine session resumption.
1955 * We choose the former approach because this fits in with assumptions
1956 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1957 * SHA256 is disabled) hash of the ticket.
1959 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
1960 s->session->session_id, &s->session->session_id_length,
1961 EVP_sha256(), NULL)) {
1962 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
1965 return MSG_PROCESS_CONTINUE_READING;
1967 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1969 ossl_statem_set_error(s);
1970 return MSG_PROCESS_ERROR;
1973 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1976 unsigned long resplen;
1979 if (!PACKET_get_1(pkt, &type)
1980 || type != TLSEXT_STATUSTYPE_ocsp) {
1981 al = SSL_AD_DECODE_ERROR;
1982 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1985 if (!PACKET_get_net_3(pkt, &resplen)
1986 || PACKET_remaining(pkt) != resplen) {
1987 al = SSL_AD_DECODE_ERROR;
1988 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1991 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
1992 if (s->tlsext_ocsp_resp == NULL) {
1993 al = SSL_AD_INTERNAL_ERROR;
1994 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1997 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
1998 al = SSL_AD_DECODE_ERROR;
1999 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2002 s->tlsext_ocsp_resplen = resplen;
2003 return MSG_PROCESS_CONTINUE_READING;
2005 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2006 ossl_statem_set_error(s);
2007 return MSG_PROCESS_ERROR;
2010 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2012 if (PACKET_remaining(pkt) > 0) {
2013 /* should contain no data */
2014 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2015 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2016 ossl_statem_set_error(s);
2017 return MSG_PROCESS_ERROR;
2019 #ifndef OPENSSL_NO_SRP
2020 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2021 if (SRP_Calc_A_param(s) <= 0) {
2022 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2023 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2024 ossl_statem_set_error(s);
2025 return MSG_PROCESS_ERROR;
2031 * at this point we check that we have the required stuff from
2034 if (!ssl3_check_cert_and_algorithm(s)) {
2035 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2036 ossl_statem_set_error(s);
2037 return MSG_PROCESS_ERROR;
2041 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2042 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2043 * message, or NULL and -1 otherwise
2045 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
2047 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2049 ssl3_send_alert(s, SSL3_AL_FATAL,
2050 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
2051 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
2052 SSL_R_INVALID_STATUS_RESPONSE);
2053 return MSG_PROCESS_ERROR;
2056 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2057 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
2058 return MSG_PROCESS_ERROR;
2061 #ifndef OPENSSL_NO_CT
2062 if (s->ct_validation_callback != NULL) {
2063 /* Note we validate the SCTs whether or not we abort on error */
2064 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2065 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2066 return MSG_PROCESS_ERROR;
2071 #ifndef OPENSSL_NO_SCTP
2072 /* Only applies to renegotiation */
2073 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2074 && s->renegotiate != 0)
2075 return MSG_PROCESS_CONTINUE_PROCESSING;
2078 return MSG_PROCESS_FINISHED_READING;
2081 static int tls_construct_cke_psk_preamble(SSL *s, unsigned char **p,
2082 size_t *pskhdrlen, int *al)
2084 #ifndef OPENSSL_NO_PSK
2087 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2088 * \0-terminated identity. The last byte is for us for simulating
2091 char identity[PSK_MAX_IDENTITY_LEN + 1];
2092 size_t identitylen = 0;
2093 unsigned char psk[PSK_MAX_PSK_LEN];
2094 unsigned char *tmppsk = NULL;
2095 char *tmpidentity = NULL;
2098 if (s->psk_client_callback == NULL) {
2099 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
2100 *al = SSL_AD_INTERNAL_ERROR;
2104 memset(identity, 0, sizeof(identity));
2106 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2107 identity, sizeof(identity) - 1,
2110 if (psklen > PSK_MAX_PSK_LEN) {
2111 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2112 *al = SSL_AD_HANDSHAKE_FAILURE;
2114 } else if (psklen == 0) {
2115 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
2116 SSL_R_PSK_IDENTITY_NOT_FOUND);
2117 *al = SSL_AD_HANDSHAKE_FAILURE;
2121 identitylen = strlen(identity);
2122 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2123 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2124 *al = SSL_AD_HANDSHAKE_FAILURE;
2128 tmppsk = OPENSSL_memdup(psk, psklen);
2129 tmpidentity = OPENSSL_strdup(identity);
2130 if (tmppsk == NULL || tmpidentity == NULL) {
2131 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2132 *al = SSL_AD_INTERNAL_ERROR;
2136 OPENSSL_free(s->s3->tmp.psk);
2137 s->s3->tmp.psk = tmppsk;
2138 s->s3->tmp.psklen = psklen;
2140 OPENSSL_free(s->session->psk_identity);
2141 s->session->psk_identity = tmpidentity;
2143 s2n(identitylen, *p);
2144 memcpy(*p, identity, identitylen);
2145 *pskhdrlen = 2 + identitylen;
2151 OPENSSL_cleanse(psk, psklen);
2152 OPENSSL_cleanse(identity, sizeof(identity));
2153 OPENSSL_clear_free(tmppsk, psklen);
2154 OPENSSL_clear_free(tmpidentity, identitylen);
2158 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2159 *al = SSL_AD_INTERNAL_ERROR;
2164 static int tls_construct_cke_rsa(SSL *s, unsigned char **p, int *len, int *al)
2166 #ifndef OPENSSL_NO_RSA
2168 EVP_PKEY *pkey = NULL;
2169 EVP_PKEY_CTX *pctx = NULL;
2171 unsigned char *pms = NULL;
2174 if (s->session->peer == NULL) {
2176 * We should always have a server certificate with SSL_kRSA.
2178 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2182 pkey = X509_get0_pubkey(s->session->peer);
2183 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2184 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2188 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2189 pms = OPENSSL_malloc(pmslen);
2191 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
2192 *al = SSL_AD_INTERNAL_ERROR;
2196 pms[0] = s->client_version >> 8;
2197 pms[1] = s->client_version & 0xff;
2198 if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {
2203 /* Fix buf for TLS and beyond */
2204 if (s->version > SSL3_VERSION)
2206 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2207 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2208 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2209 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
2212 if (EVP_PKEY_encrypt(pctx, *p, &enclen, pms, pmslen) <= 0) {
2213 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
2217 EVP_PKEY_CTX_free(pctx);
2220 if (s->options & SSL_OP_PKCS1_CHECK_1)
2222 if (s->options & SSL_OP_PKCS1_CHECK_2)
2226 /* Fix buf for TLS and beyond */
2227 if (s->version > SSL3_VERSION) {
2232 s->s3->tmp.pms = pms;
2233 s->s3->tmp.pmslen = pmslen;
2237 OPENSSL_clear_free(pms, pmslen);
2238 EVP_PKEY_CTX_free(pctx);
2242 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2243 *al = SSL_AD_INTERNAL_ERROR;
2248 static int tls_construct_cke_dhe(SSL *s, unsigned char **p, int *len, int *al)
2250 #ifndef OPENSSL_NO_DH
2252 const BIGNUM *pub_key;
2253 EVP_PKEY *ckey = NULL, *skey = NULL;
2255 skey = s->s3->peer_tmp;
2257 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2260 ckey = ssl_generate_pkey(skey);
2261 dh_clnt = EVP_PKEY_get0_DH(ckey);
2263 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
2264 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2265 EVP_PKEY_free(ckey);
2269 /* send off the data */
2270 DH_get0_key(dh_clnt, &pub_key, NULL);
2271 *len = BN_num_bytes(pub_key);
2273 BN_bn2bin(pub_key, *p);
2275 EVP_PKEY_free(ckey);
2279 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2280 *al = SSL_AD_INTERNAL_ERROR;
2285 static int tls_construct_cke_ecdhe(SSL *s, unsigned char **p, int *len, int *al)
2287 #ifndef OPENSSL_NO_EC
2288 unsigned char *encodedPoint = NULL;
2289 int encoded_pt_len = 0;
2290 EVP_PKEY *ckey = NULL, *skey = NULL;
2292 skey = s->s3->peer_tmp;
2294 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2298 ckey = ssl_generate_pkey(skey);
2300 if (ssl_derive(s, ckey, skey) == 0) {
2301 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
2305 /* Generate encoding of client key */
2306 encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
2308 if (encoded_pt_len == 0) {
2309 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
2313 EVP_PKEY_free(ckey);
2316 *len = encoded_pt_len;
2318 /* length of encoded point */
2321 /* copy the point */
2322 memcpy(*p, encodedPoint, *len);
2323 /* increment len to account for length field */
2326 OPENSSL_free(encodedPoint);
2330 EVP_PKEY_free(ckey);
2333 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2334 *al = SSL_AD_INTERNAL_ERROR;
2339 static int tls_construct_cke_gost(SSL *s, unsigned char **p, int *len, int *al)
2341 #ifndef OPENSSL_NO_GOST
2342 /* GOST key exchange message creation */
2343 EVP_PKEY_CTX *pkey_ctx = NULL;
2346 unsigned int md_len;
2347 unsigned char shared_ukm[32], tmp[256];
2348 EVP_MD_CTX *ukm_hash = NULL;
2349 int dgst_nid = NID_id_GostR3411_94;
2350 unsigned char *pms = NULL;
2353 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2354 dgst_nid = NID_id_GostR3411_2012_256;
2357 * Get server sertificate PKEY and create ctx from it
2359 peer_cert = s->session->peer;
2361 *al = SSL_AD_HANDSHAKE_FAILURE;
2362 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
2363 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2367 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2368 if (pkey_ctx == NULL) {
2369 *al = SSL_AD_INTERNAL_ERROR;
2370 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2374 * If we have send a certificate, and certificate key
2375 * parameters match those of server certificate, use
2376 * certificate key for key exchange
2379 /* Otherwise, generate ephemeral key pair */
2381 pms = OPENSSL_malloc(pmslen);
2383 *al = SSL_AD_INTERNAL_ERROR;
2384 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2388 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2389 /* Generate session key */
2390 || RAND_bytes(pms, pmslen) <= 0) {
2391 *al = SSL_AD_INTERNAL_ERROR;
2392 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2396 * Compute shared IV and store it in algorithm-specific context
2399 ukm_hash = EVP_MD_CTX_new();
2400 if (ukm_hash == NULL
2401 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
2402 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2403 SSL3_RANDOM_SIZE) <= 0
2404 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2405 SSL3_RANDOM_SIZE) <= 0
2406 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2407 *al = SSL_AD_INTERNAL_ERROR;
2408 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2411 EVP_MD_CTX_free(ukm_hash);
2413 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2414 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2415 *al = SSL_AD_INTERNAL_ERROR;
2416 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2419 /* Make GOST keytransport blob message */
2421 * Encapsulate it into sequence
2423 *((*p)++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2425 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2426 *al = SSL_AD_INTERNAL_ERROR;
2427 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2430 if (msglen >= 0x80) {
2432 *((*p)++) = msglen & 0xff;
2435 *((*p)++) = msglen & 0xff;
2438 memcpy(*p, tmp, msglen);
2440 EVP_PKEY_CTX_free(pkey_ctx);
2441 s->s3->tmp.pms = pms;
2442 s->s3->tmp.pmslen = pmslen;
2446 EVP_PKEY_CTX_free(pkey_ctx);
2447 OPENSSL_clear_free(pms, pmslen);
2448 EVP_MD_CTX_free(ukm_hash);
2451 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2452 *al = SSL_AD_INTERNAL_ERROR;
2457 static int tls_construct_cke_srp(SSL *s, unsigned char **p, int *len, int *al)
2459 #ifndef OPENSSL_NO_SRP
2460 if (s->srp_ctx.A != NULL) {
2461 /* send off the data */
2462 *len = BN_num_bytes(s->srp_ctx.A);
2464 BN_bn2bin(s->srp_ctx.A, *p);
2467 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2470 OPENSSL_free(s->session->srp_username);
2471 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2472 if (s->session->srp_username == NULL) {
2473 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
2479 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2480 *al = SSL_AD_INTERNAL_ERROR;
2485 int tls_construct_client_key_exchange(SSL *s)
2489 size_t pskhdrlen = 0;
2490 unsigned long alg_k;
2493 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2495 p = ssl_handshake_start(s);
2497 if ((alg_k & SSL_PSK)
2498 && !tls_construct_cke_psk_preamble(s, &p, &pskhdrlen, &al))
2501 if (alg_k & SSL_kPSK) {
2503 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2504 if (!tls_construct_cke_rsa(s, &p, &len, &al))
2506 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2507 if (!tls_construct_cke_dhe(s, &p, &len, &al))
2509 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2510 if (!tls_construct_cke_ecdhe(s, &p, &len, &al))
2512 } else if (alg_k & SSL_kGOST) {
2513 if (!tls_construct_cke_gost(s, &p, &len, &al))
2515 } else if (alg_k & SSL_kSRP) {
2516 if (!tls_construct_cke_srp(s, &p, &len, &al))
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);
2526 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, len)) {
2527 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2528 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2535 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2536 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
2537 s->s3->tmp.pms = NULL;
2538 #ifndef OPENSSL_NO_PSK
2539 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2540 s->s3->tmp.psk = NULL;
2542 ossl_statem_set_error(s);
2546 int tls_client_key_exchange_post_work(SSL *s)
2548 unsigned char *pms = NULL;
2551 pms = s->s3->tmp.pms;
2552 pmslen = s->s3->tmp.pmslen;
2554 #ifndef OPENSSL_NO_SRP
2556 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2557 if (!srp_generate_client_master_secret(s)) {
2558 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2559 ERR_R_INTERNAL_ERROR);
2566 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2567 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2568 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2571 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2572 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2573 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2574 /* ssl_generate_master_secret frees the pms even on error */
2582 #ifndef OPENSSL_NO_SCTP
2583 if (SSL_IS_DTLS(s)) {
2584 unsigned char sctpauthkey[64];
2585 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2588 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2591 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2592 sizeof(DTLS1_SCTP_AUTH_LABEL));
2594 if (SSL_export_keying_material(s, sctpauthkey,
2595 sizeof(sctpauthkey), labelbuffer,
2596 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2599 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2600 sizeof(sctpauthkey), sctpauthkey);
2606 OPENSSL_clear_free(pms, pmslen);
2607 s->s3->tmp.pms = NULL;
2611 int tls_construct_client_verify(SSL *s)
2615 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2618 unsigned long n = 0;
2622 mctx = EVP_MD_CTX_new();
2624 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2628 p = ssl_handshake_start(s);
2629 pkey = s->cert->key->privatekey;
2631 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2632 if (hdatalen <= 0) {
2633 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2636 if (SSL_USE_SIGALGS(s)) {
2637 if (!tls12_get_sigandhash(p, pkey, md)) {
2638 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2645 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2647 if (!EVP_SignInit_ex(mctx, md, NULL)
2648 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2649 || (s->version == SSL3_VERSION
2650 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2651 s->session->master_key_length,
2652 s->session->master_key))
2653 || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
2654 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2657 #ifndef OPENSSL_NO_GOST
2659 int pktype = EVP_PKEY_id(pkey);
2660 if (pktype == NID_id_GostR3410_2001
2661 || pktype == NID_id_GostR3410_2012_256
2662 || pktype == NID_id_GostR3410_2012_512)
2663 BUF_reverse(p + 2, NULL, u);
2669 /* Digest cached records and discard handshake buffer */
2670 if (!ssl3_digest_cached_records(s, 0))
2672 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2673 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2677 EVP_MD_CTX_free(mctx);
2680 EVP_MD_CTX_free(mctx);
2685 * Check a certificate can be used for client authentication. Currently check
2686 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2687 * certificates can be used and optionally checks suitability for Suite B.
2689 static int ssl3_check_client_certificate(SSL *s)
2691 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2693 /* If no suitable signature algorithm can't use certificate */
2694 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2697 * If strict mode check suitability of chain before using it. This also
2698 * adjusts suite B digest if necessary.
2700 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2701 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2706 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2709 EVP_PKEY *pkey = NULL;
2712 if (wst == WORK_MORE_A) {
2713 /* Let cert callback update client certificates if required */
2714 if (s->cert->cert_cb) {
2715 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2717 s->rwstate = SSL_X509_LOOKUP;
2721 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2722 ossl_statem_set_error(s);
2725 s->rwstate = SSL_NOTHING;
2727 if (ssl3_check_client_certificate(s))
2728 return WORK_FINISHED_CONTINUE;
2730 /* Fall through to WORK_MORE_B */
2734 /* We need to get a client cert */
2735 if (wst == WORK_MORE_B) {
2737 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2738 * return(-1); We then get retied later
2740 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2742 s->rwstate = SSL_X509_LOOKUP;
2745 s->rwstate = SSL_NOTHING;
2746 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2747 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2749 } else if (i == 1) {
2751 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2752 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2756 EVP_PKEY_free(pkey);
2757 if (i && !ssl3_check_client_certificate(s))
2760 if (s->version == SSL3_VERSION) {
2761 s->s3->tmp.cert_req = 0;
2762 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2763 return WORK_FINISHED_CONTINUE;
2765 s->s3->tmp.cert_req = 2;
2766 if (!ssl3_digest_cached_records(s, 0)) {
2767 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2768 ossl_statem_set_error(s);
2774 return WORK_FINISHED_CONTINUE;
2777 /* Shouldn't ever get here */
2781 int tls_construct_client_certificate(SSL *s)
2783 if (!ssl3_output_cert_chain(s,
2784 (s->s3->tmp.cert_req ==
2785 2) ? NULL : s->cert->key)) {
2786 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2787 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2788 ossl_statem_set_error(s);
2795 #define has_bits(i,m) (((i)&(m)) == (m))
2797 int ssl3_check_cert_and_algorithm(SSL *s)
2800 #ifndef OPENSSL_NO_EC
2804 EVP_PKEY *pkey = NULL;
2805 int al = SSL_AD_HANDSHAKE_FAILURE;
2807 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2808 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2810 /* we don't have a certificate */
2811 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2814 /* This is the passed certificate */
2816 #ifndef OPENSSL_NO_EC
2817 idx = s->session->peer_type;
2818 if (idx == SSL_PKEY_ECC) {
2819 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2821 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2826 } else if (alg_a & SSL_aECDSA) {
2827 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2828 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2832 pkey = X509_get0_pubkey(s->session->peer);
2833 i = X509_certificate_type(s->session->peer, pkey);
2835 /* Check that we have a certificate if we require one */
2836 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2837 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2838 SSL_R_MISSING_RSA_SIGNING_CERT);
2841 #ifndef OPENSSL_NO_DSA
2842 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2843 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2844 SSL_R_MISSING_DSA_SIGNING_CERT);
2848 #ifndef OPENSSL_NO_RSA
2849 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2850 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2851 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2852 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2856 #ifndef OPENSSL_NO_DH
2857 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
2858 al = SSL_AD_INTERNAL_ERROR;
2859 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2866 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2870 #ifndef OPENSSL_NO_NEXTPROTONEG
2871 int tls_construct_next_proto(SSL *s)
2873 unsigned int len, padding_len;
2876 len = s->next_proto_negotiated_len;
2877 padding_len = 32 - ((len + 2) % 32);
2878 d = (unsigned char *)s->init_buf->data;
2880 memcpy(d + 5, s->next_proto_negotiated, len);
2881 d[5 + len] = padding_len;
2882 memset(d + 6 + len, 0, padding_len);
2883 *(d++) = SSL3_MT_NEXT_PROTO;
2884 l2n3(2 + len + padding_len, d);
2885 s->init_num = 4 + 2 + len + padding_len;
2892 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2895 #ifndef OPENSSL_NO_ENGINE
2896 if (s->ctx->client_cert_engine) {
2897 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2898 SSL_get_client_CA_list(s),
2899 px509, ppkey, NULL, NULL, NULL);
2904 if (s->ctx->client_cert_cb)
2905 i = s->ctx->client_cert_cb(s, px509, ppkey);
2909 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, unsigned char *p)
2912 const SSL_CIPHER *c;
2914 int empty_reneg_info_scsv = !s->renegotiate;
2915 /* Set disabled masks for this session */
2916 ssl_set_client_disabled(s);
2922 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2923 c = sk_SSL_CIPHER_value(sk, i);
2924 /* Skip disabled ciphers */
2925 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2927 j = s->method->put_cipher_by_char(c, p);
2931 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2935 if (empty_reneg_info_scsv) {
2936 static SSL_CIPHER scsv = {
2937 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2939 j = s->method->put_cipher_by_char(&scsv, p);
2942 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2943 static SSL_CIPHER scsv = {
2944 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2946 j = s->method->put_cipher_by_char(&scsv, p);