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,
70 * Is a CertificateRequest message allowed at the moment or not?
76 static ossl_inline int cert_req_allowed(SSL *s)
78 /* TLS does not like anon-DH with client cert */
79 if ((s->version > SSL3_VERSION
80 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
81 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
88 * Should we expect the ServerKeyExchange message or not?
94 static int key_exchange_expected(SSL *s)
96 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
99 * Can't skip server key exchange if this is an ephemeral
100 * ciphersuite or for SRP
102 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
111 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
112 * handshake state transitions when the client is reading messages from the
113 * server. The message type that the server has sent is provided in |mt|. The
114 * current state is in |s->statem.hand_state|.
117 * 1: Success (transition allowed)
118 * 0: Error (transition not allowed)
120 int ossl_statem_client_read_transition(SSL *s, int mt)
122 OSSL_STATEM *st = &s->statem;
125 switch(st->hand_state) {
126 case TLS_ST_CW_CLNT_HELLO:
127 if (mt == SSL3_MT_SERVER_HELLO) {
128 st->hand_state = TLS_ST_CR_SRVR_HELLO;
132 if (SSL_IS_DTLS(s)) {
133 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
134 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
140 case TLS_ST_CR_SRVR_HELLO:
142 if (s->tlsext_ticket_expected) {
143 if (mt == SSL3_MT_NEWSESSION_TICKET) {
144 st->hand_state = TLS_ST_CR_SESSION_TICKET;
147 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
148 st->hand_state = TLS_ST_CR_CHANGE;
152 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
153 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
155 } else if (s->version >= TLS1_VERSION
156 && s->tls_session_secret_cb != NULL
157 && s->session->tlsext_tick != NULL
158 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
160 * Normally, we can tell if the server is resuming the session
161 * from the session ID. EAP-FAST (RFC 4851), however, relies on
162 * the next server message after the ServerHello to determine if
163 * the server is resuming.
166 st->hand_state = TLS_ST_CR_CHANGE;
168 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
169 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
170 if (mt == SSL3_MT_CERTIFICATE) {
171 st->hand_state = TLS_ST_CR_CERT;
175 ske_expected = key_exchange_expected(s);
176 /* SKE is optional for some PSK ciphersuites */
178 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
179 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
180 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
181 st->hand_state = TLS_ST_CR_KEY_EXCH;
184 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
185 && cert_req_allowed(s)) {
186 st->hand_state = TLS_ST_CR_CERT_REQ;
188 } else if (mt == SSL3_MT_SERVER_DONE) {
189 st->hand_state = TLS_ST_CR_SRVR_DONE;
198 * The CertificateStatus message is optional even if
199 * |tlsext_status_expected| is set
201 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
202 st->hand_state = TLS_ST_CR_CERT_STATUS;
207 case TLS_ST_CR_CERT_STATUS:
208 ske_expected = key_exchange_expected(s);
209 /* SKE is optional for some PSK ciphersuites */
211 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
212 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
213 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
214 st->hand_state = TLS_ST_CR_KEY_EXCH;
221 case TLS_ST_CR_KEY_EXCH:
222 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
223 if (cert_req_allowed(s)) {
224 st->hand_state = TLS_ST_CR_CERT_REQ;
231 case TLS_ST_CR_CERT_REQ:
232 if (mt == SSL3_MT_SERVER_DONE) {
233 st->hand_state = TLS_ST_CR_SRVR_DONE;
238 case TLS_ST_CW_FINISHED:
239 if (s->tlsext_ticket_expected) {
240 if (mt == SSL3_MT_NEWSESSION_TICKET) {
241 st->hand_state = TLS_ST_CR_SESSION_TICKET;
244 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
245 st->hand_state = TLS_ST_CR_CHANGE;
250 case TLS_ST_CR_SESSION_TICKET:
251 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
252 st->hand_state = TLS_ST_CR_CHANGE;
257 case TLS_ST_CR_CHANGE:
258 if (mt == SSL3_MT_FINISHED) {
259 st->hand_state = TLS_ST_CR_FINISHED;
269 /* No valid transition found */
270 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
271 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_UNEXPECTED_MESSAGE);
276 * client_write_transition() works out what handshake state to move to next
277 * when the client is writing messages to be sent to the server.
279 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
281 OSSL_STATEM *st = &s->statem;
283 switch(st->hand_state) {
285 /* Renegotiation - fall through */
287 st->hand_state = TLS_ST_CW_CLNT_HELLO;
288 return WRITE_TRAN_CONTINUE;
290 case TLS_ST_CW_CLNT_HELLO:
292 * No transition at the end of writing because we don't know what
295 return WRITE_TRAN_FINISHED;
297 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
298 st->hand_state = TLS_ST_CW_CLNT_HELLO;
299 return WRITE_TRAN_CONTINUE;
301 case TLS_ST_CR_SRVR_DONE:
302 if (s->s3->tmp.cert_req)
303 st->hand_state = TLS_ST_CW_CERT;
305 st->hand_state = TLS_ST_CW_KEY_EXCH;
306 return WRITE_TRAN_CONTINUE;
309 st->hand_state = TLS_ST_CW_KEY_EXCH;
310 return WRITE_TRAN_CONTINUE;
312 case TLS_ST_CW_KEY_EXCH:
314 * For TLS, cert_req is set to 2, so a cert chain of nothing is
315 * sent, but no verify packet is sent
318 * XXX: For now, we do not support client authentication in ECDH
319 * cipher suites with ECDH (rather than ECDSA) certificates. We
320 * need to skip the certificate verify message when client's
321 * ECDH public key is sent inside the client certificate.
323 if (s->s3->tmp.cert_req == 1) {
324 st->hand_state = TLS_ST_CW_CERT_VRFY;
326 st->hand_state = TLS_ST_CW_CHANGE;
328 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
329 st->hand_state = TLS_ST_CW_CHANGE;
331 return WRITE_TRAN_CONTINUE;
333 case TLS_ST_CW_CERT_VRFY:
334 st->hand_state = TLS_ST_CW_CHANGE;
335 return WRITE_TRAN_CONTINUE;
337 case TLS_ST_CW_CHANGE:
338 #if defined(OPENSSL_NO_NEXTPROTONEG)
339 st->hand_state = TLS_ST_CW_FINISHED;
341 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
342 st->hand_state = TLS_ST_CW_NEXT_PROTO;
344 st->hand_state = TLS_ST_CW_FINISHED;
346 return WRITE_TRAN_CONTINUE;
348 #if !defined(OPENSSL_NO_NEXTPROTONEG)
349 case TLS_ST_CW_NEXT_PROTO:
350 st->hand_state = TLS_ST_CW_FINISHED;
351 return WRITE_TRAN_CONTINUE;
354 case TLS_ST_CW_FINISHED:
356 st->hand_state = TLS_ST_OK;
357 ossl_statem_set_in_init(s, 0);
358 return WRITE_TRAN_CONTINUE;
360 return WRITE_TRAN_FINISHED;
363 case TLS_ST_CR_FINISHED:
365 st->hand_state = TLS_ST_CW_CHANGE;
366 return WRITE_TRAN_CONTINUE;
368 st->hand_state = TLS_ST_OK;
369 ossl_statem_set_in_init(s, 0);
370 return WRITE_TRAN_CONTINUE;
374 /* Shouldn't happen */
375 return WRITE_TRAN_ERROR;
380 * Perform any pre work that needs to be done prior to sending a message from
381 * the client to the server.
383 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
385 OSSL_STATEM *st = &s->statem;
387 switch(st->hand_state) {
388 case TLS_ST_CW_CLNT_HELLO:
390 if (SSL_IS_DTLS(s)) {
391 /* every DTLS ClientHello resets Finished MAC */
392 if (!ssl3_init_finished_mac(s)) {
393 ossl_statem_set_error(s);
399 case TLS_ST_CW_CHANGE:
400 if (SSL_IS_DTLS(s)) {
403 * We're into the last flight so we don't retransmit these
404 * messages unless we need to.
408 #ifndef OPENSSL_NO_SCTP
409 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
410 return dtls_wait_for_dry(s);
413 return WORK_FINISHED_CONTINUE;
416 return tls_finish_handshake(s, wst);
419 /* No pre work to be done */
423 return WORK_FINISHED_CONTINUE;
427 * Perform any work that needs to be done after sending a message from the
428 * client to the server.
430 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
432 OSSL_STATEM *st = &s->statem;
436 switch(st->hand_state) {
437 case TLS_ST_CW_CLNT_HELLO:
438 if (wst == WORK_MORE_A && statem_flush(s) != 1)
441 if (SSL_IS_DTLS(s)) {
442 /* Treat the next message as the first packet */
447 case TLS_ST_CW_KEY_EXCH:
448 if (tls_client_key_exchange_post_work(s) == 0)
452 case TLS_ST_CW_CHANGE:
453 s->session->cipher = s->s3->tmp.new_cipher;
454 #ifdef OPENSSL_NO_COMP
455 s->session->compress_meth = 0;
457 if (s->s3->tmp.new_compression == NULL)
458 s->session->compress_meth = 0;
460 s->session->compress_meth = s->s3->tmp.new_compression->id;
462 if (!s->method->ssl3_enc->setup_key_block(s))
465 if (!s->method->ssl3_enc->change_cipher_state(s,
466 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
469 if (SSL_IS_DTLS(s)) {
470 #ifndef OPENSSL_NO_SCTP
473 * Change to new shared key of SCTP-Auth, will be ignored if
476 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
481 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
485 case TLS_ST_CW_FINISHED:
486 #ifndef OPENSSL_NO_SCTP
487 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
489 * Change to new shared key of SCTP-Auth, will be ignored if
492 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
496 if (statem_flush(s) != 1)
501 /* No post work to be done */
505 return WORK_FINISHED_CONTINUE;
509 * Construct a message to be sent from the client to the server.
511 * Valid return values are:
515 int ossl_statem_client_construct_message(SSL *s)
517 OSSL_STATEM *st = &s->statem;
519 switch(st->hand_state) {
520 case TLS_ST_CW_CLNT_HELLO:
521 return tls_construct_client_hello(s);
524 return tls_construct_client_certificate(s);
526 case TLS_ST_CW_KEY_EXCH:
527 return tls_construct_client_key_exchange(s);
529 case TLS_ST_CW_CERT_VRFY:
530 return tls_construct_client_verify(s);
532 case TLS_ST_CW_CHANGE:
534 return dtls_construct_change_cipher_spec(s);
536 return tls_construct_change_cipher_spec(s);
538 #if !defined(OPENSSL_NO_NEXTPROTONEG)
539 case TLS_ST_CW_NEXT_PROTO:
540 return tls_construct_next_proto(s);
542 case TLS_ST_CW_FINISHED:
543 return tls_construct_finished(s,
545 ssl3_enc->client_finished_label,
547 ssl3_enc->client_finished_label_len);
550 /* Shouldn't happen */
558 * Returns the maximum allowed length for the current message that we are
559 * reading. Excludes the message header.
561 unsigned long ossl_statem_client_max_message_size(SSL *s)
563 OSSL_STATEM *st = &s->statem;
565 switch(st->hand_state) {
566 case TLS_ST_CR_SRVR_HELLO:
567 return SERVER_HELLO_MAX_LENGTH;
569 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
570 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
573 return s->max_cert_list;
575 case TLS_ST_CR_CERT_STATUS:
576 return SSL3_RT_MAX_PLAIN_LENGTH;
578 case TLS_ST_CR_KEY_EXCH:
579 return SERVER_KEY_EXCH_MAX_LENGTH;
581 case TLS_ST_CR_CERT_REQ:
582 /* Set to s->max_cert_list for compatibility with previous releases.
583 * In practice these messages can get quite long if servers are
584 * configured to provide a long list of acceptable CAs
586 return s->max_cert_list;
588 case TLS_ST_CR_SRVR_DONE:
589 return SERVER_HELLO_DONE_MAX_LENGTH;
591 case TLS_ST_CR_CHANGE:
592 return CCS_MAX_LENGTH;
594 case TLS_ST_CR_SESSION_TICKET:
595 return SSL3_RT_MAX_PLAIN_LENGTH;
597 case TLS_ST_CR_FINISHED:
598 return FINISHED_MAX_LENGTH;
601 /* Shouldn't happen */
609 * Process a message that the client has been received from the server.
611 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
613 OSSL_STATEM *st = &s->statem;
615 switch(st->hand_state) {
616 case TLS_ST_CR_SRVR_HELLO:
617 return tls_process_server_hello(s, pkt);
619 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
620 return dtls_process_hello_verify(s, pkt);
623 return tls_process_server_certificate(s, pkt);
625 case TLS_ST_CR_CERT_STATUS:
626 return tls_process_cert_status(s, pkt);
628 case TLS_ST_CR_KEY_EXCH:
629 return tls_process_key_exchange(s, pkt);
631 case TLS_ST_CR_CERT_REQ:
632 return tls_process_certificate_request(s, pkt);
634 case TLS_ST_CR_SRVR_DONE:
635 return tls_process_server_done(s, pkt);
637 case TLS_ST_CR_CHANGE:
638 return tls_process_change_cipher_spec(s, pkt);
640 case TLS_ST_CR_SESSION_TICKET:
641 return tls_process_new_session_ticket(s, pkt);
643 case TLS_ST_CR_FINISHED:
644 return tls_process_finished(s, pkt);
647 /* Shouldn't happen */
651 return MSG_PROCESS_ERROR;
655 * Perform any further processing required following the receipt of a message
658 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
660 OSSL_STATEM *st = &s->statem;
662 switch(st->hand_state) {
663 case TLS_ST_CR_CERT_REQ:
664 return tls_prepare_client_certificate(s, wst);
666 #ifndef OPENSSL_NO_SCTP
667 case TLS_ST_CR_SRVR_DONE:
668 /* We only get here if we are using SCTP and we are renegotiating */
669 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
670 s->s3->in_read_app_data = 2;
671 s->rwstate = SSL_READING;
672 BIO_clear_retry_flags(SSL_get_rbio(s));
673 BIO_set_retry_read(SSL_get_rbio(s));
674 ossl_statem_set_sctp_read_sock(s, 1);
677 ossl_statem_set_sctp_read_sock(s, 0);
678 return WORK_FINISHED_STOP;
685 /* Shouldn't happen */
689 int tls_construct_client_hello(SSL *s)
692 unsigned char *p, *d;
697 #ifndef OPENSSL_NO_COMP
701 SSL_SESSION *sess = s->session;
703 buf = (unsigned char *)s->init_buf->data;
705 /* Work out what SSL/TLS/DTLS version to use */
706 protverr = ssl_set_client_hello_version(s);
708 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
712 if ((sess == NULL) ||
713 !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,
744 sizeof(s->s3->client_random)) <= 0)
747 /* Do the message type and length last */
748 d = p = ssl_handshake_start(s);
751 * version indicates the negotiated version: for example from
752 * an SSLv2/v3 compatible client hello). The client_version
753 * field is the maximum version we permit and it is also
754 * used in RSA encrypted premaster secrets. Some servers can
755 * choke if we initially report a higher version then
756 * renegotiate to a lower one in the premaster secret. This
757 * didn't happen with TLS 1.0 as most servers supported it
758 * but it can with TLS 1.1 or later if the server only supports
761 * Possible scenario with previous logic:
762 * 1. Client hello indicates TLS 1.2
763 * 2. Server hello says TLS 1.0
764 * 3. RSA encrypted premaster secret uses 1.2.
765 * 4. Handshake proceeds using TLS 1.0.
766 * 5. Server sends hello request to renegotiate.
767 * 6. Client hello indicates TLS v1.0 as we now
768 * know that is maximum server supports.
769 * 7. Server chokes on RSA encrypted premaster secret
770 * containing version 1.0.
772 * For interoperability it should be OK to always use the
773 * maximum version we support in client hello and then rely
774 * on the checking of version to ensure the servers isn't
775 * being inconsistent: for example initially negotiating with
776 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
777 * client_version in client hello and not resetting it to
778 * the negotiated version.
780 *(p++) = s->client_version >> 8;
781 *(p++) = s->client_version & 0xff;
784 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
785 p += SSL3_RANDOM_SIZE;
791 i = s->session->session_id_length;
794 if (i > (int)sizeof(s->session->session_id)) {
795 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
798 memcpy(p, s->session->session_id, i);
802 /* cookie stuff for DTLS */
803 if (SSL_IS_DTLS(s)) {
804 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
805 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
808 *(p++) = s->d1->cookie_len;
809 memcpy(p, s->d1->cookie, s->d1->cookie_len);
810 p += s->d1->cookie_len;
813 /* Ciphers supported */
814 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
816 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
819 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
821 * Some servers hang if client hello > 256 bytes as hack workaround
822 * chop number of supported ciphers to keep it well below this if we
825 if (TLS1_get_version(s) >= TLS1_2_VERSION
826 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
827 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
833 #ifdef OPENSSL_NO_COMP
837 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
840 j = sk_SSL_COMP_num(s->ctx->comp_methods);
842 for (i = 0; i < j; i++) {
843 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
847 *(p++) = 0; /* Add the NULL method */
850 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
851 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
855 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
857 ssl3_send_alert(s, SSL3_AL_FATAL, al);
858 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
863 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
864 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
865 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
871 ossl_statem_set_error(s);
875 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
878 unsigned int cookie_len;
881 if (!PACKET_forward(pkt, 2)
882 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
883 al = SSL_AD_DECODE_ERROR;
884 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
888 cookie_len = PACKET_remaining(&cookiepkt);
889 if (cookie_len > sizeof(s->d1->cookie)) {
890 al = SSL_AD_ILLEGAL_PARAMETER;
891 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
895 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
896 al = SSL_AD_DECODE_ERROR;
897 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
900 s->d1->cookie_len = cookie_len;
902 return MSG_PROCESS_FINISHED_READING;
904 ssl3_send_alert(s, SSL3_AL_FATAL, al);
905 ossl_statem_set_error(s);
906 return MSG_PROCESS_ERROR;
909 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
911 STACK_OF(SSL_CIPHER) *sk;
914 size_t session_id_len;
915 const unsigned char *cipherchars;
916 int i, al = SSL_AD_INTERNAL_ERROR;
917 unsigned int compression;
918 unsigned int sversion;
920 #ifndef OPENSSL_NO_COMP
924 if (!PACKET_get_net_2(pkt, &sversion)) {
925 al = SSL_AD_DECODE_ERROR;
926 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
930 protverr = ssl_choose_client_version(s, sversion);
932 al = SSL_AD_PROTOCOL_VERSION;
933 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
937 /* load the server hello data */
938 /* load the server random */
939 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
940 al = SSL_AD_DECODE_ERROR;
941 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
947 /* Get the session-id. */
948 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
949 al = SSL_AD_DECODE_ERROR;
950 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
953 session_id_len = PACKET_remaining(&session_id);
954 if (session_id_len > sizeof s->session->session_id
955 || session_id_len > SSL3_SESSION_ID_SIZE) {
956 al = SSL_AD_ILLEGAL_PARAMETER;
957 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
961 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
962 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
963 al = SSL_AD_DECODE_ERROR;
968 * Check if we can resume the session based on external pre-shared secret.
969 * EAP-FAST (RFC 4851) supports two types of session resumption.
970 * Resumption based on server-side state works with session IDs.
971 * Resumption based on pre-shared Protected Access Credentials (PACs)
972 * works by overriding the SessionTicket extension at the application
973 * layer, and does not send a session ID. (We do not know whether EAP-FAST
974 * servers would honour the session ID.) Therefore, the session ID alone
975 * is not a reliable indicator of session resumption, so we first check if
976 * we can resume, and later peek at the next handshake message to see if the
977 * server wants to resume.
979 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
980 s->session->tlsext_tick) {
981 const SSL_CIPHER *pref_cipher = NULL;
982 s->session->master_key_length = sizeof(s->session->master_key);
983 if (s->tls_session_secret_cb(s, s->session->master_key,
984 &s->session->master_key_length,
986 s->tls_session_secret_cb_arg)) {
987 s->session->cipher = pref_cipher ?
988 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
990 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
991 al = SSL_AD_INTERNAL_ERROR;
996 if (session_id_len != 0 && session_id_len == s->session->session_id_length
997 && memcmp(PACKET_data(&session_id), s->session->session_id,
998 session_id_len) == 0) {
999 if (s->sid_ctx_length != s->session->sid_ctx_length
1000 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1001 /* actually a client application bug */
1002 al = SSL_AD_ILLEGAL_PARAMETER;
1003 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1004 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1010 * If we were trying for session-id reuse but the server
1011 * didn't echo the ID, make a new SSL_SESSION.
1012 * In the case of EAP-FAST and PAC, we do not send a session ID,
1013 * so the PAC-based session secret is always preserved. It'll be
1014 * overwritten if the server refuses resumption.
1016 if (s->session->session_id_length > 0) {
1017 s->ctx->stats.sess_miss++;
1018 if (!ssl_get_new_session(s, 0)) {
1023 s->session->ssl_version = s->version;
1024 s->session->session_id_length = session_id_len;
1025 /* session_id_len could be 0 */
1026 memcpy(s->session->session_id, PACKET_data(&session_id),
1030 /* Session version and negotiated protocol version should match */
1031 if (s->version != s->session->ssl_version) {
1032 al = SSL_AD_PROTOCOL_VERSION;
1034 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1035 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1039 c = ssl_get_cipher_by_char(s, cipherchars);
1041 /* unknown cipher */
1042 al = SSL_AD_ILLEGAL_PARAMETER;
1043 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1047 * Now that we know the version, update the check to see if it's an allowed
1050 s->s3->tmp.min_ver = s->version;
1051 s->s3->tmp.max_ver = s->version;
1053 * If it is a disabled cipher we either didn't send it in client hello,
1054 * or it's not allowed for the selected protocol. So we return an error.
1056 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1057 al = SSL_AD_ILLEGAL_PARAMETER;
1058 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1062 sk = ssl_get_ciphers_by_id(s);
1063 i = sk_SSL_CIPHER_find(sk, c);
1065 /* we did not say we would use this cipher */
1066 al = SSL_AD_ILLEGAL_PARAMETER;
1067 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1072 * Depending on the session caching (internal/external), the cipher
1073 * and/or cipher_id values may not be set. Make sure that cipher_id is
1074 * set and use it for comparison.
1076 if (s->session->cipher)
1077 s->session->cipher_id = s->session->cipher->id;
1078 if (s->hit && (s->session->cipher_id != c->id)) {
1079 al = SSL_AD_ILLEGAL_PARAMETER;
1080 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1081 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1084 s->s3->tmp.new_cipher = c;
1085 /* lets get the compression algorithm */
1087 if (!PACKET_get_1(pkt, &compression)) {
1088 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1089 al = SSL_AD_DECODE_ERROR;
1092 #ifdef OPENSSL_NO_COMP
1093 if (compression != 0) {
1094 al = SSL_AD_ILLEGAL_PARAMETER;
1095 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1096 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1100 * If compression is disabled we'd better not try to resume a session
1101 * using compression.
1103 if (s->session->compress_meth != 0) {
1104 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1108 if (s->hit && compression != s->session->compress_meth) {
1109 al = SSL_AD_ILLEGAL_PARAMETER;
1110 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1111 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1114 if (compression == 0)
1116 else if (!ssl_allow_compression(s)) {
1117 al = SSL_AD_ILLEGAL_PARAMETER;
1118 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1121 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1124 if (compression != 0 && comp == NULL) {
1125 al = SSL_AD_ILLEGAL_PARAMETER;
1126 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1127 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1130 s->s3->tmp.new_compression = comp;
1134 /* TLS extensions */
1135 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1136 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1140 if (PACKET_remaining(pkt) != 0) {
1141 /* wrong packet length */
1142 al = SSL_AD_DECODE_ERROR;
1143 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1147 #ifndef OPENSSL_NO_SCTP
1148 if (SSL_IS_DTLS(s) && s->hit) {
1149 unsigned char sctpauthkey[64];
1150 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1153 * Add new shared key for SCTP-Auth, will be ignored if
1156 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1157 sizeof(DTLS1_SCTP_AUTH_LABEL));
1159 if (SSL_export_keying_material(s, sctpauthkey,
1160 sizeof(sctpauthkey),
1162 sizeof(labelbuffer), NULL, 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);
1230 if ((s->verify_mode & SSL_VERIFY_PEER) && i <= 0) {
1231 al = ssl_verify_alarm_type(s->verify_result);
1232 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1233 SSL_R_CERTIFICATE_VERIFY_FAILED);
1236 ERR_clear_error(); /* but we keep s->verify_result */
1238 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1239 al = SSL_AD_HANDSHAKE_FAILURE;
1243 s->session->peer_chain = sk;
1245 * Inconsistency alert: cert_chain does include the peer's certificate,
1246 * which we don't include in statem_srvr.c
1248 x = sk_X509_value(sk, 0);
1251 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1254 pkey = X509_get0_pubkey(x);
1256 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1259 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1260 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1264 i = ssl_cert_type(x, pkey);
1268 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1269 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1273 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1274 if (exp_idx >= 0 && i != exp_idx
1275 && (exp_idx != SSL_PKEY_GOST_EC ||
1276 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1277 && i != SSL_PKEY_GOST01))) {
1279 al = SSL_AD_ILLEGAL_PARAMETER;
1280 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1281 SSL_R_WRONG_CERTIFICATE_TYPE);
1284 s->session->peer_type = i;
1286 X509_free(s->session->peer);
1288 s->session->peer = x;
1289 s->session->verify_result = s->verify_result;
1292 ret = MSG_PROCESS_CONTINUE_READING;
1296 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1298 ossl_statem_set_error(s);
1301 sk_X509_pop_free(sk, X509_free);
1305 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1310 EVP_PKEY *pkey = NULL;
1311 const EVP_MD *md = NULL;
1312 #ifndef OPENSSL_NO_RSA
1315 #ifndef OPENSSL_NO_EC
1316 EVP_PKEY_CTX *pctx = NULL;
1318 PACKET save_param_start, signature;
1320 md_ctx = EVP_MD_CTX_new();
1321 if (md_ctx == NULL) {
1322 al = SSL_AD_INTERNAL_ERROR;
1323 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1327 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1329 save_param_start = *pkt;
1331 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1332 EVP_PKEY_free(s->s3->peer_tmp);
1333 s->s3->peer_tmp = NULL;
1336 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1338 al = SSL_AD_DECODE_ERROR;
1340 #ifndef OPENSSL_NO_PSK
1341 /* PSK ciphersuites are preceded by an identity hint */
1342 if (alg_k & SSL_PSK) {
1343 PACKET psk_identity_hint;
1344 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1345 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1350 * Store PSK identity hint for later use, hint is used in
1351 * ssl3_send_client_key_exchange. Assume that the maximum length of
1352 * a PSK identity hint can be as long as the maximum length of a PSK
1355 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1356 al = SSL_AD_HANDSHAKE_FAILURE;
1357 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1361 if (PACKET_remaining(&psk_identity_hint) == 0) {
1362 OPENSSL_free(s->session->psk_identity_hint);
1363 s->session->psk_identity_hint = NULL;
1364 } else if (!PACKET_strndup(&psk_identity_hint,
1365 &s->session->psk_identity_hint)) {
1366 al = SSL_AD_INTERNAL_ERROR;
1371 /* Nothing else to do for plain PSK or RSAPSK */
1372 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1374 #endif /* !OPENSSL_NO_PSK */
1376 * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_*
1381 #ifndef OPENSSL_NO_SRP
1382 else if (alg_k & SSL_kSRP) {
1383 PACKET prime, generator, salt, server_pub;
1384 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1385 || !PACKET_get_length_prefixed_2(pkt, &generator)
1386 || !PACKET_get_length_prefixed_1(pkt, &salt)
1387 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1388 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1393 BN_bin2bn(PACKET_data(&prime),
1394 PACKET_remaining(&prime), NULL)) == NULL
1396 BN_bin2bn(PACKET_data(&generator),
1397 PACKET_remaining(&generator), NULL)) == NULL
1399 BN_bin2bn(PACKET_data(&salt),
1400 PACKET_remaining(&salt), NULL)) == NULL
1402 BN_bin2bn(PACKET_data(&server_pub),
1403 PACKET_remaining(&server_pub), NULL)) == NULL) {
1404 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1408 if (!srp_verify_server_param(s, &al)) {
1409 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1413 /* We must check if there is a certificate */
1414 if (alg_a & (SSL_aRSA|SSL_aDSS))
1415 pkey = X509_get0_pubkey(s->session->peer);
1417 #endif /* !OPENSSL_NO_SRP */
1418 #ifndef OPENSSL_NO_DH
1419 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1420 PACKET prime, generator, pub_key;
1421 EVP_PKEY *peer_tmp = NULL;
1424 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1426 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1427 || !PACKET_get_length_prefixed_2(pkt, &generator)
1428 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1429 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1433 peer_tmp = EVP_PKEY_new();
1436 if (peer_tmp == NULL || dh == NULL) {
1437 al = SSL_AD_INTERNAL_ERROR;
1438 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1442 p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL);
1443 g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator),
1445 bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key),
1447 if (p == NULL || g == NULL || bnpub_key == NULL) {
1448 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1452 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1453 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1457 if (!DH_set0_pqg(dh, p, NULL, g)) {
1458 al = SSL_AD_INTERNAL_ERROR;
1459 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1464 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1465 al = SSL_AD_INTERNAL_ERROR;
1466 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1471 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1472 al = SSL_AD_HANDSHAKE_FAILURE;
1473 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1477 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1478 al = SSL_AD_INTERNAL_ERROR;
1479 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1483 s->s3->peer_tmp = peer_tmp;
1491 EVP_PKEY_free(peer_tmp);
1495 * FIXME: This makes assumptions about which ciphersuites come with
1496 * public keys. We should have a less ad-hoc way of doing this
1498 if (alg_a & (SSL_aRSA|SSL_aDSS))
1499 pkey = X509_get0_pubkey(s->session->peer);
1500 /* else anonymous DH, so no certificate or pkey. */
1502 #endif /* !OPENSSL_NO_DH */
1504 #ifndef OPENSSL_NO_EC
1505 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1507 const unsigned char *ecparams;
1511 * Extract elliptic curve parameters and the server's ephemeral ECDH
1512 * public key. For now we only support named (not generic) curves and
1513 * ECParameters in this case is just three bytes.
1515 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1516 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1520 * Check curve is one of our preferences, if not server has sent an
1521 * invalid curve. ECParameters is 3 bytes.
1523 if (!tls1_check_curve(s, ecparams, 3)) {
1524 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1528 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2));
1529 if (curve_nid == 0) {
1530 al = SSL_AD_INTERNAL_ERROR;
1531 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1532 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1536 /* Set up EVP_PKEY with named curve as parameters */
1537 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1539 || EVP_PKEY_paramgen_init(pctx) <= 0
1540 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1541 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1542 al = SSL_AD_INTERNAL_ERROR;
1543 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1546 EVP_PKEY_CTX_free(pctx);
1549 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1550 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1554 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp),
1555 PACKET_data(&encoded_pt),
1556 PACKET_remaining(&encoded_pt), NULL) == 0) {
1557 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1562 * The ECC/TLS specification does not mention the use of DSA to sign
1563 * ECParameters in the server key exchange message. We do support RSA
1567 # ifndef OPENSSL_NO_RSA
1568 else if (alg_a & SSL_aRSA)
1569 pkey = X509_get0_pubkey(s->session->peer);
1571 # ifndef OPENSSL_NO_EC
1572 else if (alg_a & SSL_aECDSA)
1573 pkey = X509_get0_pubkey(s->session->peer);
1575 /* else anonymous ECDH, so no certificate or pkey. */
1577 al = SSL_AD_UNEXPECTED_MESSAGE;
1578 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1581 #endif /* !OPENSSL_NO_EC */
1583 /* if it was signed, check the signature */
1587 * |pkt| now points to the beginning of the signature, so the difference
1588 * equals the length of the parameters.
1590 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1591 PACKET_remaining(&save_param_start) -
1592 PACKET_remaining(pkt))) {
1593 al = SSL_AD_INTERNAL_ERROR;
1594 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1598 if (SSL_USE_SIGALGS(s)) {
1599 const unsigned char *sigalgs;
1601 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1602 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1605 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1612 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1614 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1615 md = EVP_md5_sha1();
1620 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1621 || PACKET_remaining(pkt) != 0) {
1622 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1625 j = EVP_PKEY_size(pkey);
1627 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1632 * Check signature length
1634 if (PACKET_remaining(&signature) > (size_t)j) {
1635 /* wrong packet length */
1636 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1639 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1640 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1641 SSL3_RANDOM_SIZE) <= 0
1642 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1643 SSL3_RANDOM_SIZE) <= 0
1644 || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms),
1645 PACKET_remaining(¶ms)) <= 0) {
1646 al = SSL_AD_INTERNAL_ERROR;
1647 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1650 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1651 PACKET_remaining(&signature), pkey) <= 0) {
1653 al = SSL_AD_DECRYPT_ERROR;
1654 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1658 /* aNULL, aSRP or PSK do not need public keys */
1659 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1660 /* Might be wrong key type, check it */
1661 if (ssl3_check_cert_and_algorithm(s))
1662 /* Otherwise this shouldn't happen */
1663 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1666 /* still data left over */
1667 if (PACKET_remaining(pkt) != 0) {
1668 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1672 EVP_MD_CTX_free(md_ctx);
1673 return MSG_PROCESS_CONTINUE_READING;
1675 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1677 #ifndef OPENSSL_NO_RSA
1680 #ifndef OPENSSL_NO_EC
1681 EVP_PKEY_CTX_free(pctx);
1683 EVP_MD_CTX_free(md_ctx);
1684 ossl_statem_set_error(s);
1685 return MSG_PROCESS_ERROR;
1688 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1690 int ret = MSG_PROCESS_ERROR;
1691 unsigned int list_len, ctype_num, i, name_len;
1692 X509_NAME *xn = NULL;
1693 const unsigned char *data;
1694 const unsigned char *namestart, *namebytes;
1695 STACK_OF(X509_NAME) *ca_sk = NULL;
1697 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1698 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1702 /* get the certificate types */
1703 if (!PACKET_get_1(pkt, &ctype_num)
1704 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1705 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1706 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1709 OPENSSL_free(s->cert->ctypes);
1710 s->cert->ctypes = NULL;
1711 if (ctype_num > SSL3_CT_NUMBER) {
1712 /* If we exceed static buffer copy all to cert structure */
1713 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1714 if (s->cert->ctypes == NULL) {
1715 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1718 memcpy(s->cert->ctypes, data, ctype_num);
1719 s->cert->ctype_num = (size_t)ctype_num;
1720 ctype_num = SSL3_CT_NUMBER;
1722 for (i = 0; i < ctype_num; i++)
1723 s->s3->tmp.ctype[i] = data[i];
1725 if (SSL_USE_SIGALGS(s)) {
1726 if (!PACKET_get_net_2(pkt, &list_len)
1727 || !PACKET_get_bytes(pkt, &data, list_len)) {
1728 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1729 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1730 SSL_R_LENGTH_MISMATCH);
1734 /* Clear certificate digests and validity flags */
1735 for (i = 0; i < SSL_PKEY_NUM; i++) {
1736 s->s3->tmp.md[i] = NULL;
1737 s->s3->tmp.valid_flags[i] = 0;
1739 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1740 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1741 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1742 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1745 if (!tls1_process_sigalgs(s)) {
1746 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1747 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1751 ssl_set_default_md(s);
1754 /* get the CA RDNs */
1755 if (!PACKET_get_net_2(pkt, &list_len)
1756 || PACKET_remaining(pkt) != list_len) {
1757 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1758 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1762 while (PACKET_remaining(pkt)) {
1763 if (!PACKET_get_net_2(pkt, &name_len)
1764 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1765 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1766 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1767 SSL_R_LENGTH_MISMATCH);
1771 namestart = namebytes;
1773 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1774 name_len)) == NULL) {
1775 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1776 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1780 if (namebytes != (namestart + name_len)) {
1781 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1782 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1783 SSL_R_CA_DN_LENGTH_MISMATCH);
1786 if (!sk_X509_NAME_push(ca_sk, xn)) {
1787 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1793 /* we should setup a certificate to return.... */
1794 s->s3->tmp.cert_req = 1;
1795 s->s3->tmp.ctype_num = ctype_num;
1796 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1797 s->s3->tmp.ca_names = ca_sk;
1800 ret = MSG_PROCESS_CONTINUE_PROCESSING;
1803 ossl_statem_set_error(s);
1806 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1810 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1812 return (X509_NAME_cmp(*a, *b));
1815 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1818 unsigned int ticklen;
1819 unsigned long ticket_lifetime_hint;
1821 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1822 || !PACKET_get_net_2(pkt, &ticklen)
1823 || PACKET_remaining(pkt) != ticklen) {
1824 al = SSL_AD_DECODE_ERROR;
1825 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1829 /* Server is allowed to change its mind and send an empty ticket. */
1831 return MSG_PROCESS_CONTINUE_READING;
1833 if (s->session->session_id_length > 0) {
1834 int i = s->session_ctx->session_cache_mode;
1835 SSL_SESSION *new_sess;
1837 * We reused an existing session, so we need to replace it with a new
1840 if (i & SSL_SESS_CACHE_CLIENT) {
1842 * Remove the old session from the cache
1844 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
1845 if (s->session_ctx->remove_session_cb != NULL)
1846 s->session_ctx->remove_session_cb(s->session_ctx,
1849 /* We carry on if this fails */
1850 SSL_CTX_remove_session(s->session_ctx, s->session);
1854 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1855 al = SSL_AD_INTERNAL_ERROR;
1856 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1860 SSL_SESSION_free(s->session);
1861 s->session = new_sess;
1864 OPENSSL_free(s->session->tlsext_tick);
1865 s->session->tlsext_ticklen = 0;
1867 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1868 if (s->session->tlsext_tick == NULL) {
1869 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1872 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1873 al = SSL_AD_DECODE_ERROR;
1874 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1878 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1879 s->session->tlsext_ticklen = ticklen;
1881 * There are two ways to detect a resumed ticket session. One is to set
1882 * an appropriate session ID and then the server must return a match in
1883 * ServerHello. This allows the normal client session ID matching to work
1884 * and we know much earlier that the ticket has been accepted. The
1885 * other way is to set zero length session ID when the ticket is
1886 * presented and rely on the handshake to determine session resumption.
1887 * We choose the former approach because this fits in with assumptions
1888 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1889 * SHA256 is disabled) hash of the ticket.
1891 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
1892 s->session->session_id, &s->session->session_id_length,
1893 EVP_sha256(), NULL)) {
1894 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
1897 return MSG_PROCESS_CONTINUE_READING;
1899 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1901 ossl_statem_set_error(s);
1902 return MSG_PROCESS_ERROR;
1905 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1908 unsigned long resplen;
1911 if (!PACKET_get_1(pkt, &type)
1912 || type != TLSEXT_STATUSTYPE_ocsp) {
1913 al = SSL_AD_DECODE_ERROR;
1914 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1917 if (!PACKET_get_net_3(pkt, &resplen)
1918 || PACKET_remaining(pkt) != resplen) {
1919 al = SSL_AD_DECODE_ERROR;
1920 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1923 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
1924 if (s->tlsext_ocsp_resp == NULL) {
1925 al = SSL_AD_INTERNAL_ERROR;
1926 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1929 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
1930 al = SSL_AD_DECODE_ERROR;
1931 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1934 s->tlsext_ocsp_resplen = resplen;
1935 return MSG_PROCESS_CONTINUE_READING;
1937 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1938 ossl_statem_set_error(s);
1939 return MSG_PROCESS_ERROR;
1942 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
1944 if (PACKET_remaining(pkt) > 0) {
1945 /* should contain no data */
1946 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1947 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1948 ossl_statem_set_error(s);
1949 return MSG_PROCESS_ERROR;
1952 #ifndef OPENSSL_NO_SRP
1953 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
1954 if (SRP_Calc_A_param(s) <= 0) {
1955 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
1956 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1957 ossl_statem_set_error(s);
1958 return MSG_PROCESS_ERROR;
1964 * at this point we check that we have the required stuff from
1967 if (!ssl3_check_cert_and_algorithm(s)) {
1968 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1969 ossl_statem_set_error(s);
1970 return MSG_PROCESS_ERROR;
1974 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
1975 * |tlsext_ocsp_resplen| values will be set if we actually received a status
1976 * message, or NULL and -1 otherwise
1978 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
1980 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1982 ssl3_send_alert(s, SSL3_AL_FATAL,
1983 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
1984 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
1985 SSL_R_INVALID_STATUS_RESPONSE);
1986 return MSG_PROCESS_ERROR;
1989 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1990 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
1991 return MSG_PROCESS_ERROR;
1995 #ifndef OPENSSL_NO_CT
1996 if (s->ct_validation_callback != NULL) {
1997 /* Note we validate the SCTs whether or not we abort on error */
1998 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
1999 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2000 return MSG_PROCESS_ERROR;
2005 #ifndef OPENSSL_NO_SCTP
2006 /* Only applies to renegotiation */
2007 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2008 && s->renegotiate != 0)
2009 return MSG_PROCESS_CONTINUE_PROCESSING;
2012 return MSG_PROCESS_FINISHED_READING;
2015 static int tls_construct_cke_psk_preamble(SSL *s, unsigned char **p,
2016 size_t *pskhdrlen, int *al)
2018 #ifndef OPENSSL_NO_PSK
2021 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2022 * \0-terminated identity. The last byte is for us for simulating
2025 char identity[PSK_MAX_IDENTITY_LEN + 1];
2026 size_t identitylen = 0;
2027 unsigned char psk[PSK_MAX_PSK_LEN];
2028 unsigned char *tmppsk = NULL;
2029 char *tmpidentity = NULL;
2032 if (s->psk_client_callback == NULL) {
2033 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2034 SSL_R_PSK_NO_CLIENT_CB);
2035 *al = SSL_AD_INTERNAL_ERROR;
2039 memset(identity, 0, sizeof(identity));
2041 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2042 identity, sizeof(identity) - 1,
2045 if (psklen > PSK_MAX_PSK_LEN) {
2046 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2047 ERR_R_INTERNAL_ERROR);
2048 *al = SSL_AD_HANDSHAKE_FAILURE;
2050 } else if (psklen == 0) {
2051 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2052 SSL_R_PSK_IDENTITY_NOT_FOUND);
2053 *al = SSL_AD_HANDSHAKE_FAILURE;
2057 identitylen = strlen(identity);
2058 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2059 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2060 ERR_R_INTERNAL_ERROR);
2061 *al = SSL_AD_HANDSHAKE_FAILURE;
2065 tmppsk = OPENSSL_memdup(psk, psklen);
2066 tmpidentity = OPENSSL_strdup(identity);
2067 if (tmppsk == NULL || tmpidentity == NULL) {
2068 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2069 *al = SSL_AD_INTERNAL_ERROR;
2073 OPENSSL_free(s->s3->tmp.psk);
2074 s->s3->tmp.psk = tmppsk;
2075 s->s3->tmp.psklen = psklen;
2077 OPENSSL_free(s->session->psk_identity);
2078 s->session->psk_identity = tmpidentity;
2080 s2n(identitylen, *p);
2081 memcpy(*p, identity, identitylen);
2082 *pskhdrlen = 2 + identitylen;
2088 OPENSSL_cleanse(psk, psklen);
2089 OPENSSL_cleanse(identity, sizeof(identity));
2090 OPENSSL_clear_free(tmppsk, psklen);
2091 OPENSSL_clear_free(tmpidentity, identitylen);
2095 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2096 *al = SSL_AD_INTERNAL_ERROR;
2101 static int tls_construct_cke_rsa(SSL *s, unsigned char **p, int *len, int *al)
2103 #ifndef OPENSSL_NO_RSA
2105 EVP_PKEY *pkey = NULL;
2106 EVP_PKEY_CTX *pctx = NULL;
2108 unsigned char *pms = NULL;
2111 if (s->session->peer == NULL) {
2113 * We should always have a server certificate with SSL_kRSA.
2115 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2116 ERR_R_INTERNAL_ERROR);
2120 pkey = X509_get0_pubkey(s->session->peer);
2121 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2122 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2123 ERR_R_INTERNAL_ERROR);
2127 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2128 pms = OPENSSL_malloc(pmslen);
2130 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2131 ERR_R_MALLOC_FAILURE);
2132 *al = SSL_AD_INTERNAL_ERROR;
2136 pms[0] = s->client_version >> 8;
2137 pms[1] = s->client_version & 0xff;
2138 if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {
2143 /* Fix buf for TLS and beyond */
2144 if (s->version > SSL3_VERSION)
2146 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2147 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2148 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2149 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2153 if (EVP_PKEY_encrypt(pctx, *p, &enclen, pms, pmslen) <= 0) {
2154 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2155 SSL_R_BAD_RSA_ENCRYPT);
2159 EVP_PKEY_CTX_free(pctx);
2162 if (s->options & SSL_OP_PKCS1_CHECK_1)
2164 if (s->options & SSL_OP_PKCS1_CHECK_2)
2168 /* Fix buf for TLS and beyond */
2169 if (s->version > SSL3_VERSION) {
2174 s->s3->tmp.pms = pms;
2175 s->s3->tmp.pmslen = pmslen;
2179 OPENSSL_clear_free(pms, pmslen);
2180 EVP_PKEY_CTX_free(pctx);
2184 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2185 *al = SSL_AD_INTERNAL_ERROR;
2190 int tls_construct_client_key_exchange(SSL *s)
2194 size_t pskhdrlen = 0;
2195 unsigned long alg_k;
2198 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2200 p = ssl_handshake_start(s);
2204 if ((alg_k & SSL_PSK)
2205 && !tls_construct_cke_psk_preamble(s, &p, &pskhdrlen, &al))
2208 if (alg_k & SSL_kPSK) {
2210 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2211 if (!tls_construct_cke_rsa(s, &p, &n, &al))
2214 #ifndef OPENSSL_NO_DH
2215 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2217 const BIGNUM *pub_key;
2218 EVP_PKEY *ckey = NULL, *skey = NULL;
2220 skey = s->s3->peer_tmp;
2222 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2223 ERR_R_INTERNAL_ERROR);
2226 ckey = ssl_generate_pkey(skey, NID_undef);
2227 dh_clnt = EVP_PKEY_get0_DH(ckey);
2229 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
2230 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2231 ERR_R_INTERNAL_ERROR);
2232 EVP_PKEY_free(ckey);
2237 /* send off the data */
2238 DH_get0_key(dh_clnt, &pub_key, NULL);
2239 n = BN_num_bytes(pub_key);
2241 BN_bn2bin(pub_key, p);
2243 EVP_PKEY_free(ckey);
2248 #ifndef OPENSSL_NO_EC
2249 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2250 unsigned char *encodedPoint = NULL;
2251 int encoded_pt_len = 0;
2252 EVP_PKEY *ckey = NULL, *skey = NULL;
2254 skey = s->s3->peer_tmp;
2255 if ((skey == NULL) || EVP_PKEY_get0_EC_KEY(skey) == NULL) {
2256 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2257 ERR_R_INTERNAL_ERROR);
2261 ckey = ssl_generate_pkey(skey, NID_undef);
2263 if (ssl_derive(s, ckey, skey) == 0) {
2264 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);
2265 EVP_PKEY_free(ckey);
2269 /* Generate encoding of client key */
2270 encoded_pt_len = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey),
2271 POINT_CONVERSION_UNCOMPRESSED,
2272 &encodedPoint, NULL);
2274 if (encoded_pt_len == 0) {
2275 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2276 EVP_PKEY_free(ckey);
2280 EVP_PKEY_free(ckey);
2285 *p = n; /* length of encoded point */
2286 /* Encoded point will be copied here */
2288 /* copy the point */
2289 memcpy(p, encodedPoint, n);
2290 /* increment n to account for length field */
2293 /* Free allocated memory */
2294 OPENSSL_free(encodedPoint);
2296 #endif /* !OPENSSL_NO_EC */
2297 #ifndef OPENSSL_NO_GOST
2298 else if (alg_k & SSL_kGOST) {
2299 /* GOST key exchange message creation */
2300 EVP_PKEY_CTX *pkey_ctx;
2303 unsigned int md_len;
2304 unsigned char shared_ukm[32], tmp[256];
2305 EVP_MD_CTX *ukm_hash;
2306 int dgst_nid = NID_id_GostR3411_94;
2307 unsigned char *pms = NULL;
2310 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2311 dgst_nid = NID_id_GostR3411_2012_256;
2315 * Get server sertificate PKEY and create ctx from it
2317 peer_cert = s->session->peer;
2319 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2320 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2324 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2325 if (pkey_ctx == NULL) {
2326 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2327 ERR_R_MALLOC_FAILURE);
2331 * If we have send a certificate, and certificate key
2332 * parameters match those of server certificate, use
2333 * certificate key for key exchange
2336 /* Otherwise, generate ephemeral key pair */
2338 pms = OPENSSL_malloc(pmslen);
2342 if (pkey_ctx == NULL
2343 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2344 /* Generate session key */
2345 || RAND_bytes(pms, pmslen) <= 0) {
2346 EVP_PKEY_CTX_free(pkey_ctx);
2347 OPENSSL_clear_free(pms, pmslen);
2348 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2349 ERR_R_INTERNAL_ERROR);
2353 * If we have client certificate, use its secret as peer key
2355 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2356 if (EVP_PKEY_derive_set_peer
2357 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2359 * If there was an error - just ignore it. Ephemeral key
2366 * Compute shared IV and store it in algorithm-specific context
2369 ukm_hash = EVP_MD_CTX_new();
2370 if (EVP_DigestInit(ukm_hash,
2371 EVP_get_digestbynid(dgst_nid)) <= 0
2372 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2373 SSL3_RANDOM_SIZE) <= 0
2374 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2375 SSL3_RANDOM_SIZE) <= 0
2376 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2377 EVP_MD_CTX_free(ukm_hash);
2378 OPENSSL_clear_free(pms, pmslen);
2379 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2380 ERR_R_INTERNAL_ERROR);
2383 EVP_MD_CTX_free(ukm_hash);
2384 if (EVP_PKEY_CTX_ctrl
2385 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2387 OPENSSL_clear_free(pms, pmslen);
2388 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2392 /* Make GOST keytransport blob message */
2394 * Encapsulate it into sequence
2396 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2398 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2399 OPENSSL_clear_free(pms, pmslen);
2400 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2404 if (msglen >= 0x80) {
2406 *(p++) = msglen & 0xff;
2409 *(p++) = msglen & 0xff;
2412 memcpy(p, tmp, msglen);
2413 /* Check if pubkey from client certificate was used */
2414 if (EVP_PKEY_CTX_ctrl
2415 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2416 /* Set flag "skip certificate verify" */
2417 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2419 EVP_PKEY_CTX_free(pkey_ctx);
2420 s->s3->tmp.pms = pms;
2421 s->s3->tmp.pmslen = pmslen;
2424 #ifndef OPENSSL_NO_SRP
2425 else if (alg_k & SSL_kSRP) {
2426 if (s->srp_ctx.A != NULL) {
2427 /* send off the data */
2428 n = BN_num_bytes(s->srp_ctx.A);
2430 BN_bn2bin(s->srp_ctx.A, p);
2433 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2434 ERR_R_INTERNAL_ERROR);
2437 OPENSSL_free(s->session->srp_username);
2438 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2439 if (s->session->srp_username == NULL) {
2440 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2441 ERR_R_MALLOC_FAILURE);
2447 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2448 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2454 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2455 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2456 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2462 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2463 al = SSL_AD_INTERNAL_ERROR;
2466 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2467 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
2468 s->s3->tmp.pms = NULL;
2469 #ifndef OPENSSL_NO_PSK
2470 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2471 s->s3->tmp.psk = NULL;
2473 ossl_statem_set_error(s);
2477 int tls_client_key_exchange_post_work(SSL *s)
2479 unsigned char *pms = NULL;
2482 pms = s->s3->tmp.pms;
2483 pmslen = s->s3->tmp.pmslen;
2485 #ifndef OPENSSL_NO_SRP
2487 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2488 if (!srp_generate_client_master_secret(s)) {
2489 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2490 ERR_R_INTERNAL_ERROR);
2497 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2498 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2499 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2502 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2503 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2504 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2505 /* ssl_generate_master_secret frees the pms even on error */
2513 #ifndef OPENSSL_NO_SCTP
2514 if (SSL_IS_DTLS(s)) {
2515 unsigned char sctpauthkey[64];
2516 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2519 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2522 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2523 sizeof(DTLS1_SCTP_AUTH_LABEL));
2525 if (SSL_export_keying_material(s, sctpauthkey,
2526 sizeof(sctpauthkey), labelbuffer,
2527 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2530 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2531 sizeof(sctpauthkey), sctpauthkey);
2537 OPENSSL_clear_free(pms, pmslen);
2538 s->s3->tmp.pms = NULL;
2542 int tls_construct_client_verify(SSL *s)
2546 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2549 unsigned long n = 0;
2553 mctx = EVP_MD_CTX_new();
2555 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2559 p = ssl_handshake_start(s);
2560 pkey = s->cert->key->privatekey;
2562 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2563 if (hdatalen <= 0) {
2564 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2567 if (SSL_USE_SIGALGS(s)) {
2568 if (!tls12_get_sigandhash(p, pkey, md)) {
2569 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2576 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2578 if (!EVP_SignInit_ex(mctx, md, NULL)
2579 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2580 || (s->version == SSL3_VERSION
2581 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2582 s->session->master_key_length,
2583 s->session->master_key))
2584 || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
2585 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2588 #ifndef OPENSSL_NO_GOST
2590 int pktype = EVP_PKEY_id(pkey);
2591 if (pktype == NID_id_GostR3410_2001
2592 || pktype == NID_id_GostR3410_2012_256
2593 || pktype == NID_id_GostR3410_2012_512)
2594 BUF_reverse(p + 2, NULL, u);
2600 /* Digest cached records and discard handshake buffer */
2601 if (!ssl3_digest_cached_records(s, 0))
2603 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2604 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2608 EVP_MD_CTX_free(mctx);
2611 EVP_MD_CTX_free(mctx);
2616 * Check a certificate can be used for client authentication. Currently check
2617 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2618 * certificates can be used and optionally checks suitability for Suite B.
2620 static int ssl3_check_client_certificate(SSL *s)
2622 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2624 /* If no suitable signature algorithm can't use certificate */
2625 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2628 * If strict mode check suitability of chain before using it. This also
2629 * adjusts suite B digest if necessary.
2631 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2632 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2637 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2640 EVP_PKEY *pkey = NULL;
2643 if (wst == WORK_MORE_A) {
2644 /* Let cert callback update client certificates if required */
2645 if (s->cert->cert_cb) {
2646 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2648 s->rwstate = SSL_X509_LOOKUP;
2652 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2653 ossl_statem_set_error(s);
2656 s->rwstate = SSL_NOTHING;
2658 if (ssl3_check_client_certificate(s))
2659 return WORK_FINISHED_CONTINUE;
2661 /* Fall through to WORK_MORE_B */
2665 /* We need to get a client cert */
2666 if (wst == WORK_MORE_B) {
2668 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2669 * return(-1); We then get retied later
2671 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2673 s->rwstate = SSL_X509_LOOKUP;
2676 s->rwstate = SSL_NOTHING;
2677 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2678 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2680 } else if (i == 1) {
2682 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2683 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2687 EVP_PKEY_free(pkey);
2688 if (i && !ssl3_check_client_certificate(s))
2691 if (s->version == SSL3_VERSION) {
2692 s->s3->tmp.cert_req = 0;
2693 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2694 return WORK_FINISHED_CONTINUE;
2696 s->s3->tmp.cert_req = 2;
2697 if (!ssl3_digest_cached_records(s, 0)) {
2698 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2699 ossl_statem_set_error(s);
2705 return WORK_FINISHED_CONTINUE;
2708 /* Shouldn't ever get here */
2712 int tls_construct_client_certificate(SSL *s)
2714 if (!ssl3_output_cert_chain(s,
2715 (s->s3->tmp.cert_req ==
2716 2) ? NULL : s->cert->key)) {
2717 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2718 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2719 ossl_statem_set_error(s);
2726 #define has_bits(i,m) (((i)&(m)) == (m))
2728 int ssl3_check_cert_and_algorithm(SSL *s)
2731 #ifndef OPENSSL_NO_EC
2735 EVP_PKEY *pkey = NULL;
2736 int al = SSL_AD_HANDSHAKE_FAILURE;
2738 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2739 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2741 /* we don't have a certificate */
2742 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2745 /* This is the passed certificate */
2747 #ifndef OPENSSL_NO_EC
2748 idx = s->session->peer_type;
2749 if (idx == SSL_PKEY_ECC) {
2750 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2752 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2757 } else if (alg_a & SSL_aECDSA) {
2758 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2759 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2763 pkey = X509_get0_pubkey(s->session->peer);
2764 i = X509_certificate_type(s->session->peer, pkey);
2766 /* Check that we have a certificate if we require one */
2767 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2768 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2769 SSL_R_MISSING_RSA_SIGNING_CERT);
2772 #ifndef OPENSSL_NO_DSA
2773 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2774 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2775 SSL_R_MISSING_DSA_SIGNING_CERT);
2779 #ifndef OPENSSL_NO_RSA
2780 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2781 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2782 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2783 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2787 #ifndef OPENSSL_NO_DH
2788 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
2789 al = SSL_AD_INTERNAL_ERROR;
2790 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2797 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2801 #ifndef OPENSSL_NO_NEXTPROTONEG
2802 int tls_construct_next_proto(SSL *s)
2804 unsigned int len, padding_len;
2807 len = s->next_proto_negotiated_len;
2808 padding_len = 32 - ((len + 2) % 32);
2809 d = (unsigned char *)s->init_buf->data;
2811 memcpy(d + 5, s->next_proto_negotiated, len);
2812 d[5 + len] = padding_len;
2813 memset(d + 6 + len, 0, padding_len);
2814 *(d++) = SSL3_MT_NEXT_PROTO;
2815 l2n3(2 + len + padding_len, d);
2816 s->init_num = 4 + 2 + len + padding_len;
2823 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2826 #ifndef OPENSSL_NO_ENGINE
2827 if (s->ctx->client_cert_engine) {
2828 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2829 SSL_get_client_CA_list(s),
2830 px509, ppkey, NULL, NULL, NULL);
2835 if (s->ctx->client_cert_cb)
2836 i = s->ctx->client_cert_cb(s, px509, ppkey);
2840 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
2844 const SSL_CIPHER *c;
2846 int empty_reneg_info_scsv = !s->renegotiate;
2847 /* Set disabled masks for this session */
2848 ssl_set_client_disabled(s);
2854 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2855 c = sk_SSL_CIPHER_value(sk, i);
2856 /* Skip disabled ciphers */
2857 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2859 j = s->method->put_cipher_by_char(c, p);
2863 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2867 if (empty_reneg_info_scsv) {
2868 static SSL_CIPHER scsv = {
2869 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2871 j = s->method->put_cipher_by_char(&scsv, p);
2874 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2875 static SSL_CIPHER scsv = {
2876 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2878 j = s->method->put_cipher_by_char(&scsv, p);