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?
95 static int key_exchange_expected(SSL *s)
97 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
100 * Can't skip server key exchange if this is an ephemeral
101 * ciphersuite or for SRP
103 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
112 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
113 * handshake state transitions when the client is reading messages from the
114 * server. The message type that the server has sent is provided in |mt|. The
115 * current state is in |s->statem.hand_state|.
118 * 1: Success (transition allowed)
119 * 0: Error (transition not allowed)
121 int ossl_statem_client_read_transition(SSL *s, int mt)
123 OSSL_STATEM *st = &s->statem;
126 switch(st->hand_state) {
127 case TLS_ST_CW_CLNT_HELLO:
128 if (mt == SSL3_MT_SERVER_HELLO) {
129 st->hand_state = TLS_ST_CR_SRVR_HELLO;
133 if (SSL_IS_DTLS(s)) {
134 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
135 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
141 case TLS_ST_CR_SRVR_HELLO:
143 if (s->tlsext_ticket_expected) {
144 if (mt == SSL3_MT_NEWSESSION_TICKET) {
145 st->hand_state = TLS_ST_CR_SESSION_TICKET;
148 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
149 st->hand_state = TLS_ST_CR_CHANGE;
153 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
154 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
156 } else if (s->version >= TLS1_VERSION
157 && s->tls_session_secret_cb != NULL
158 && s->session->tlsext_tick != NULL
159 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
161 * Normally, we can tell if the server is resuming the session
162 * from the session ID. EAP-FAST (RFC 4851), however, relies on
163 * the next server message after the ServerHello to determine if
164 * the server is resuming.
167 st->hand_state = TLS_ST_CR_CHANGE;
169 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
170 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
171 if (mt == SSL3_MT_CERTIFICATE) {
172 st->hand_state = TLS_ST_CR_CERT;
176 ske_expected = key_exchange_expected(s);
177 if (ske_expected < 0)
179 /* SKE is optional for some PSK ciphersuites */
181 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
182 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
183 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
184 st->hand_state = TLS_ST_CR_KEY_EXCH;
187 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
188 && cert_req_allowed(s)) {
189 st->hand_state = TLS_ST_CR_CERT_REQ;
191 } else if (mt == SSL3_MT_SERVER_DONE) {
192 st->hand_state = TLS_ST_CR_SRVR_DONE;
201 * The CertificateStatus message is optional even if
202 * |tlsext_status_expected| is set
204 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
205 st->hand_state = TLS_ST_CR_CERT_STATUS;
210 case TLS_ST_CR_CERT_STATUS:
211 ske_expected = key_exchange_expected(s);
212 if (ske_expected < 0)
214 /* SKE is optional for some PSK ciphersuites */
216 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
217 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
218 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
219 st->hand_state = TLS_ST_CR_KEY_EXCH;
226 case TLS_ST_CR_KEY_EXCH:
227 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
228 if (cert_req_allowed(s)) {
229 st->hand_state = TLS_ST_CR_CERT_REQ;
236 case TLS_ST_CR_CERT_REQ:
237 if (mt == SSL3_MT_SERVER_DONE) {
238 st->hand_state = TLS_ST_CR_SRVR_DONE;
243 case TLS_ST_CW_FINISHED:
244 if (s->tlsext_ticket_expected) {
245 if (mt == SSL3_MT_NEWSESSION_TICKET) {
246 st->hand_state = TLS_ST_CR_SESSION_TICKET;
249 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
250 st->hand_state = TLS_ST_CR_CHANGE;
255 case TLS_ST_CR_SESSION_TICKET:
256 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
257 st->hand_state = TLS_ST_CR_CHANGE;
262 case TLS_ST_CR_CHANGE:
263 if (mt == SSL3_MT_FINISHED) {
264 st->hand_state = TLS_ST_CR_FINISHED;
274 /* No valid transition found */
275 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
276 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_UNEXPECTED_MESSAGE);
281 * client_write_transition() works out what handshake state to move to next
282 * when the client is writing messages to be sent to the server.
284 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
286 OSSL_STATEM *st = &s->statem;
288 switch(st->hand_state) {
290 /* Renegotiation - fall through */
292 st->hand_state = TLS_ST_CW_CLNT_HELLO;
293 return WRITE_TRAN_CONTINUE;
295 case TLS_ST_CW_CLNT_HELLO:
297 * No transition at the end of writing because we don't know what
300 return WRITE_TRAN_FINISHED;
302 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
303 st->hand_state = TLS_ST_CW_CLNT_HELLO;
304 return WRITE_TRAN_CONTINUE;
306 case TLS_ST_CR_SRVR_DONE:
307 if (s->s3->tmp.cert_req)
308 st->hand_state = TLS_ST_CW_CERT;
310 st->hand_state = TLS_ST_CW_KEY_EXCH;
311 return WRITE_TRAN_CONTINUE;
314 st->hand_state = TLS_ST_CW_KEY_EXCH;
315 return WRITE_TRAN_CONTINUE;
317 case TLS_ST_CW_KEY_EXCH:
319 * For TLS, cert_req is set to 2, so a cert chain of nothing is
320 * sent, but no verify packet is sent
323 * XXX: For now, we do not support client authentication in ECDH
324 * cipher suites with ECDH (rather than ECDSA) certificates. We
325 * need to skip the certificate verify message when client's
326 * ECDH public key is sent inside the client certificate.
328 if (s->s3->tmp.cert_req == 1) {
329 st->hand_state = TLS_ST_CW_CERT_VRFY;
331 st->hand_state = TLS_ST_CW_CHANGE;
333 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
334 st->hand_state = TLS_ST_CW_CHANGE;
336 return WRITE_TRAN_CONTINUE;
338 case TLS_ST_CW_CERT_VRFY:
339 st->hand_state = TLS_ST_CW_CHANGE;
340 return WRITE_TRAN_CONTINUE;
342 case TLS_ST_CW_CHANGE:
343 #if defined(OPENSSL_NO_NEXTPROTONEG)
344 st->hand_state = TLS_ST_CW_FINISHED;
346 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
347 st->hand_state = TLS_ST_CW_NEXT_PROTO;
349 st->hand_state = TLS_ST_CW_FINISHED;
351 return WRITE_TRAN_CONTINUE;
353 #if !defined(OPENSSL_NO_NEXTPROTONEG)
354 case TLS_ST_CW_NEXT_PROTO:
355 st->hand_state = TLS_ST_CW_FINISHED;
356 return WRITE_TRAN_CONTINUE;
359 case TLS_ST_CW_FINISHED:
361 st->hand_state = TLS_ST_OK;
362 ossl_statem_set_in_init(s, 0);
363 return WRITE_TRAN_CONTINUE;
365 return WRITE_TRAN_FINISHED;
368 case TLS_ST_CR_FINISHED:
370 st->hand_state = TLS_ST_CW_CHANGE;
371 return WRITE_TRAN_CONTINUE;
373 st->hand_state = TLS_ST_OK;
374 ossl_statem_set_in_init(s, 0);
375 return WRITE_TRAN_CONTINUE;
379 /* Shouldn't happen */
380 return WRITE_TRAN_ERROR;
385 * Perform any pre work that needs to be done prior to sending a message from
386 * the client to the server.
388 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
390 OSSL_STATEM *st = &s->statem;
392 switch(st->hand_state) {
393 case TLS_ST_CW_CLNT_HELLO:
395 if (SSL_IS_DTLS(s)) {
396 /* every DTLS ClientHello resets Finished MAC */
397 if (!ssl3_init_finished_mac(s)) {
398 ossl_statem_set_error(s);
404 case TLS_ST_CW_CHANGE:
405 if (SSL_IS_DTLS(s)) {
408 * We're into the last flight so we don't retransmit these
409 * messages unless we need to.
413 #ifndef OPENSSL_NO_SCTP
414 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
415 return dtls_wait_for_dry(s);
418 return WORK_FINISHED_CONTINUE;
421 return tls_finish_handshake(s, wst);
424 /* No pre work to be done */
428 return WORK_FINISHED_CONTINUE;
432 * Perform any work that needs to be done after sending a message from the
433 * client to the server.
435 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
437 OSSL_STATEM *st = &s->statem;
441 switch(st->hand_state) {
442 case TLS_ST_CW_CLNT_HELLO:
443 if (wst == WORK_MORE_A && statem_flush(s) != 1)
446 if (SSL_IS_DTLS(s)) {
447 /* Treat the next message as the first packet */
452 case TLS_ST_CW_KEY_EXCH:
453 if (tls_client_key_exchange_post_work(s) == 0)
457 case TLS_ST_CW_CHANGE:
458 s->session->cipher = s->s3->tmp.new_cipher;
459 #ifdef OPENSSL_NO_COMP
460 s->session->compress_meth = 0;
462 if (s->s3->tmp.new_compression == NULL)
463 s->session->compress_meth = 0;
465 s->session->compress_meth = s->s3->tmp.new_compression->id;
467 if (!s->method->ssl3_enc->setup_key_block(s))
470 if (!s->method->ssl3_enc->change_cipher_state(s,
471 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
474 if (SSL_IS_DTLS(s)) {
475 #ifndef OPENSSL_NO_SCTP
478 * Change to new shared key of SCTP-Auth, will be ignored if
481 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
486 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
490 case TLS_ST_CW_FINISHED:
491 #ifndef OPENSSL_NO_SCTP
492 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
494 * Change to new shared key of SCTP-Auth, will be ignored if
497 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
501 if (statem_flush(s) != 1)
506 /* No post work to be done */
510 return WORK_FINISHED_CONTINUE;
514 * Construct a message to be sent from the client to the server.
516 * Valid return values are:
520 int ossl_statem_client_construct_message(SSL *s)
522 OSSL_STATEM *st = &s->statem;
524 switch(st->hand_state) {
525 case TLS_ST_CW_CLNT_HELLO:
526 return tls_construct_client_hello(s);
529 return tls_construct_client_certificate(s);
531 case TLS_ST_CW_KEY_EXCH:
532 return tls_construct_client_key_exchange(s);
534 case TLS_ST_CW_CERT_VRFY:
535 return tls_construct_client_verify(s);
537 case TLS_ST_CW_CHANGE:
539 return dtls_construct_change_cipher_spec(s);
541 return tls_construct_change_cipher_spec(s);
543 #if !defined(OPENSSL_NO_NEXTPROTONEG)
544 case TLS_ST_CW_NEXT_PROTO:
545 return tls_construct_next_proto(s);
547 case TLS_ST_CW_FINISHED:
548 return tls_construct_finished(s,
550 ssl3_enc->client_finished_label,
552 ssl3_enc->client_finished_label_len);
555 /* Shouldn't happen */
563 * Returns the maximum allowed length for the current message that we are
564 * reading. Excludes the message header.
566 unsigned long ossl_statem_client_max_message_size(SSL *s)
568 OSSL_STATEM *st = &s->statem;
570 switch(st->hand_state) {
571 case TLS_ST_CR_SRVR_HELLO:
572 return SERVER_HELLO_MAX_LENGTH;
574 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
575 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
578 return s->max_cert_list;
580 case TLS_ST_CR_CERT_STATUS:
581 return SSL3_RT_MAX_PLAIN_LENGTH;
583 case TLS_ST_CR_KEY_EXCH:
584 return SERVER_KEY_EXCH_MAX_LENGTH;
586 case TLS_ST_CR_CERT_REQ:
587 /* Set to s->max_cert_list for compatibility with previous releases.
588 * In practice these messages can get quite long if servers are
589 * configured to provide a long list of acceptable CAs
591 return s->max_cert_list;
593 case TLS_ST_CR_SRVR_DONE:
594 return SERVER_HELLO_DONE_MAX_LENGTH;
596 case TLS_ST_CR_CHANGE:
597 return CCS_MAX_LENGTH;
599 case TLS_ST_CR_SESSION_TICKET:
600 return SSL3_RT_MAX_PLAIN_LENGTH;
602 case TLS_ST_CR_FINISHED:
603 return FINISHED_MAX_LENGTH;
606 /* Shouldn't happen */
614 * Process a message that the client has been received from the server.
616 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
618 OSSL_STATEM *st = &s->statem;
620 switch(st->hand_state) {
621 case TLS_ST_CR_SRVR_HELLO:
622 return tls_process_server_hello(s, pkt);
624 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
625 return dtls_process_hello_verify(s, pkt);
628 return tls_process_server_certificate(s, pkt);
630 case TLS_ST_CR_CERT_STATUS:
631 return tls_process_cert_status(s, pkt);
633 case TLS_ST_CR_KEY_EXCH:
634 return tls_process_key_exchange(s, pkt);
636 case TLS_ST_CR_CERT_REQ:
637 return tls_process_certificate_request(s, pkt);
639 case TLS_ST_CR_SRVR_DONE:
640 return tls_process_server_done(s, pkt);
642 case TLS_ST_CR_CHANGE:
643 return tls_process_change_cipher_spec(s, pkt);
645 case TLS_ST_CR_SESSION_TICKET:
646 return tls_process_new_session_ticket(s, pkt);
648 case TLS_ST_CR_FINISHED:
649 return tls_process_finished(s, pkt);
652 /* Shouldn't happen */
656 return MSG_PROCESS_ERROR;
660 * Perform any further processing required following the receipt of a message
663 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
665 OSSL_STATEM *st = &s->statem;
667 switch(st->hand_state) {
668 case TLS_ST_CR_CERT_REQ:
669 return tls_prepare_client_certificate(s, wst);
671 #ifndef OPENSSL_NO_SCTP
672 case TLS_ST_CR_SRVR_DONE:
673 /* We only get here if we are using SCTP and we are renegotiating */
674 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
675 s->s3->in_read_app_data = 2;
676 s->rwstate = SSL_READING;
677 BIO_clear_retry_flags(SSL_get_rbio(s));
678 BIO_set_retry_read(SSL_get_rbio(s));
679 ossl_statem_set_sctp_read_sock(s, 1);
682 ossl_statem_set_sctp_read_sock(s, 0);
683 return WORK_FINISHED_STOP;
690 /* Shouldn't happen */
694 int tls_construct_client_hello(SSL *s)
697 unsigned char *p, *d;
702 #ifndef OPENSSL_NO_COMP
706 SSL_SESSION *sess = s->session;
708 buf = (unsigned char *)s->init_buf->data;
710 /* Work out what SSL/TLS/DTLS version to use */
711 protverr = ssl_set_client_hello_version(s);
713 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
717 if ((sess == NULL) ||
718 !ssl_version_supported(s, sess->ssl_version) ||
720 * In the case of EAP-FAST, we can have a pre-shared
721 * "ticket" without a session ID.
723 (!sess->session_id_length && !sess->tlsext_tick) ||
724 (sess->not_resumable)) {
725 if (!ssl_get_new_session(s, 0))
728 /* else use the pre-loaded session */
730 p = s->s3->client_random;
733 * for DTLS if client_random is initialized, reuse it, we are
734 * required to use same upon reply to HelloVerify
736 if (SSL_IS_DTLS(s)) {
739 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
748 if (i && ssl_fill_hello_random(s, 0, p,
749 sizeof(s->s3->client_random)) <= 0)
752 /* Do the message type and length last */
753 d = p = ssl_handshake_start(s);
756 * version indicates the negotiated version: for example from
757 * an SSLv2/v3 compatible client hello). The client_version
758 * field is the maximum version we permit and it is also
759 * used in RSA encrypted premaster secrets. Some servers can
760 * choke if we initially report a higher version then
761 * renegotiate to a lower one in the premaster secret. This
762 * didn't happen with TLS 1.0 as most servers supported it
763 * but it can with TLS 1.1 or later if the server only supports
766 * Possible scenario with previous logic:
767 * 1. Client hello indicates TLS 1.2
768 * 2. Server hello says TLS 1.0
769 * 3. RSA encrypted premaster secret uses 1.2.
770 * 4. Handshake proceeds using TLS 1.0.
771 * 5. Server sends hello request to renegotiate.
772 * 6. Client hello indicates TLS v1.0 as we now
773 * know that is maximum server supports.
774 * 7. Server chokes on RSA encrypted premaster secret
775 * containing version 1.0.
777 * For interoperability it should be OK to always use the
778 * maximum version we support in client hello and then rely
779 * on the checking of version to ensure the servers isn't
780 * being inconsistent: for example initially negotiating with
781 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
782 * client_version in client hello and not resetting it to
783 * the negotiated version.
785 *(p++) = s->client_version >> 8;
786 *(p++) = s->client_version & 0xff;
789 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
790 p += SSL3_RANDOM_SIZE;
796 i = s->session->session_id_length;
799 if (i > (int)sizeof(s->session->session_id)) {
800 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
803 memcpy(p, s->session->session_id, i);
807 /* cookie stuff for DTLS */
808 if (SSL_IS_DTLS(s)) {
809 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
810 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
813 *(p++) = s->d1->cookie_len;
814 memcpy(p, s->d1->cookie, s->d1->cookie_len);
815 p += s->d1->cookie_len;
818 /* Ciphers supported */
819 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
821 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
824 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
826 * Some servers hang if client hello > 256 bytes as hack workaround
827 * chop number of supported ciphers to keep it well below this if we
830 if (TLS1_get_version(s) >= TLS1_2_VERSION
831 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
832 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
838 #ifdef OPENSSL_NO_COMP
842 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
845 j = sk_SSL_COMP_num(s->ctx->comp_methods);
847 for (i = 0; i < j; i++) {
848 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
852 *(p++) = 0; /* Add the NULL method */
855 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
856 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
860 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
862 ssl3_send_alert(s, SSL3_AL_FATAL, al);
863 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
868 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
869 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
870 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
876 ossl_statem_set_error(s);
880 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
883 unsigned int cookie_len;
886 if (!PACKET_forward(pkt, 2)
887 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
888 al = SSL_AD_DECODE_ERROR;
889 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
893 cookie_len = PACKET_remaining(&cookiepkt);
894 if (cookie_len > sizeof(s->d1->cookie)) {
895 al = SSL_AD_ILLEGAL_PARAMETER;
896 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
900 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
901 al = SSL_AD_DECODE_ERROR;
902 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
905 s->d1->cookie_len = cookie_len;
907 return MSG_PROCESS_FINISHED_READING;
909 ssl3_send_alert(s, SSL3_AL_FATAL, al);
910 ossl_statem_set_error(s);
911 return MSG_PROCESS_ERROR;
914 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
916 STACK_OF(SSL_CIPHER) *sk;
919 size_t session_id_len;
920 const unsigned char *cipherchars;
921 int i, al = SSL_AD_INTERNAL_ERROR;
922 unsigned int compression;
923 unsigned int sversion;
925 #ifndef OPENSSL_NO_COMP
929 if (!PACKET_get_net_2(pkt, &sversion)) {
930 al = SSL_AD_DECODE_ERROR;
931 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
935 protverr = ssl_choose_client_version(s, sversion);
937 al = SSL_AD_PROTOCOL_VERSION;
938 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
942 /* load the server hello data */
943 /* load the server random */
944 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
945 al = SSL_AD_DECODE_ERROR;
946 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
952 /* Get the session-id. */
953 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
954 al = SSL_AD_DECODE_ERROR;
955 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
958 session_id_len = PACKET_remaining(&session_id);
959 if (session_id_len > sizeof s->session->session_id
960 || session_id_len > SSL3_SESSION_ID_SIZE) {
961 al = SSL_AD_ILLEGAL_PARAMETER;
962 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
966 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
967 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
968 al = SSL_AD_DECODE_ERROR;
973 * Check if we can resume the session based on external pre-shared secret.
974 * EAP-FAST (RFC 4851) supports two types of session resumption.
975 * Resumption based on server-side state works with session IDs.
976 * Resumption based on pre-shared Protected Access Credentials (PACs)
977 * works by overriding the SessionTicket extension at the application
978 * layer, and does not send a session ID. (We do not know whether EAP-FAST
979 * servers would honour the session ID.) Therefore, the session ID alone
980 * is not a reliable indicator of session resumption, so we first check if
981 * we can resume, and later peek at the next handshake message to see if the
982 * server wants to resume.
984 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
985 s->session->tlsext_tick) {
986 const SSL_CIPHER *pref_cipher = NULL;
987 s->session->master_key_length = sizeof(s->session->master_key);
988 if (s->tls_session_secret_cb(s, s->session->master_key,
989 &s->session->master_key_length,
991 s->tls_session_secret_cb_arg)) {
992 s->session->cipher = pref_cipher ?
993 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
995 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
996 al = SSL_AD_INTERNAL_ERROR;
1001 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1002 && memcmp(PACKET_data(&session_id), s->session->session_id,
1003 session_id_len) == 0) {
1004 if (s->sid_ctx_length != s->session->sid_ctx_length
1005 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1006 /* actually a client application bug */
1007 al = SSL_AD_ILLEGAL_PARAMETER;
1008 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1009 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1015 * If we were trying for session-id reuse but the server
1016 * didn't echo the ID, make a new SSL_SESSION.
1017 * In the case of EAP-FAST and PAC, we do not send a session ID,
1018 * so the PAC-based session secret is always preserved. It'll be
1019 * overwritten if the server refuses resumption.
1021 if (s->session->session_id_length > 0) {
1022 s->ctx->stats.sess_miss++;
1023 if (!ssl_get_new_session(s, 0)) {
1028 s->session->ssl_version = s->version;
1029 s->session->session_id_length = session_id_len;
1030 /* session_id_len could be 0 */
1031 memcpy(s->session->session_id, PACKET_data(&session_id),
1035 /* Session version and negotiated protocol version should match */
1036 if (s->version != s->session->ssl_version) {
1037 al = SSL_AD_PROTOCOL_VERSION;
1039 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1040 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1044 c = ssl_get_cipher_by_char(s, cipherchars);
1046 /* unknown cipher */
1047 al = SSL_AD_ILLEGAL_PARAMETER;
1048 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1052 * Now that we know the version, update the check to see if it's an allowed
1055 s->s3->tmp.min_ver = s->version;
1056 s->s3->tmp.max_ver = s->version;
1058 * If it is a disabled cipher we either didn't send it in client hello,
1059 * or it's not allowed for the selected protocol. So we return an error.
1061 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1062 al = SSL_AD_ILLEGAL_PARAMETER;
1063 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1067 sk = ssl_get_ciphers_by_id(s);
1068 i = sk_SSL_CIPHER_find(sk, c);
1070 /* we did not say we would use this cipher */
1071 al = SSL_AD_ILLEGAL_PARAMETER;
1072 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1077 * Depending on the session caching (internal/external), the cipher
1078 * and/or cipher_id values may not be set. Make sure that cipher_id is
1079 * set and use it for comparison.
1081 if (s->session->cipher)
1082 s->session->cipher_id = s->session->cipher->id;
1083 if (s->hit && (s->session->cipher_id != c->id)) {
1084 al = SSL_AD_ILLEGAL_PARAMETER;
1085 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1086 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1089 s->s3->tmp.new_cipher = c;
1090 /* lets get the compression algorithm */
1092 if (!PACKET_get_1(pkt, &compression)) {
1093 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1094 al = SSL_AD_DECODE_ERROR;
1097 #ifdef OPENSSL_NO_COMP
1098 if (compression != 0) {
1099 al = SSL_AD_ILLEGAL_PARAMETER;
1100 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1101 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1105 * If compression is disabled we'd better not try to resume a session
1106 * using compression.
1108 if (s->session->compress_meth != 0) {
1109 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1113 if (s->hit && compression != s->session->compress_meth) {
1114 al = SSL_AD_ILLEGAL_PARAMETER;
1115 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1116 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1119 if (compression == 0)
1121 else if (!ssl_allow_compression(s)) {
1122 al = SSL_AD_ILLEGAL_PARAMETER;
1123 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1126 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1129 if (compression != 0 && comp == NULL) {
1130 al = SSL_AD_ILLEGAL_PARAMETER;
1131 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1132 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1135 s->s3->tmp.new_compression = comp;
1139 /* TLS extensions */
1140 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1141 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1145 if (PACKET_remaining(pkt) != 0) {
1146 /* wrong packet length */
1147 al = SSL_AD_DECODE_ERROR;
1148 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1152 #ifndef OPENSSL_NO_SCTP
1153 if (SSL_IS_DTLS(s) && s->hit) {
1154 unsigned char sctpauthkey[64];
1155 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1158 * Add new shared key for SCTP-Auth, will be ignored if
1161 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1162 sizeof(DTLS1_SCTP_AUTH_LABEL));
1164 if (SSL_export_keying_material(s, sctpauthkey,
1165 sizeof(sctpauthkey),
1167 sizeof(labelbuffer), NULL, 0,
1171 BIO_ctrl(SSL_get_wbio(s),
1172 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1173 sizeof(sctpauthkey), sctpauthkey);
1177 return MSG_PROCESS_CONTINUE_READING;
1179 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1181 ossl_statem_set_error(s);
1182 return MSG_PROCESS_ERROR;
1185 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1187 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1188 unsigned long cert_list_len, cert_len;
1190 const unsigned char *certstart, *certbytes;
1191 STACK_OF(X509) *sk = NULL;
1192 EVP_PKEY *pkey = NULL;
1194 if ((sk = sk_X509_new_null()) == NULL) {
1195 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1199 if (!PACKET_get_net_3(pkt, &cert_list_len)
1200 || PACKET_remaining(pkt) != cert_list_len) {
1201 al = SSL_AD_DECODE_ERROR;
1202 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1205 while (PACKET_remaining(pkt)) {
1206 if (!PACKET_get_net_3(pkt, &cert_len)
1207 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1208 al = SSL_AD_DECODE_ERROR;
1209 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1210 SSL_R_CERT_LENGTH_MISMATCH);
1214 certstart = certbytes;
1215 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1217 al = SSL_AD_BAD_CERTIFICATE;
1218 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1221 if (certbytes != (certstart + cert_len)) {
1222 al = SSL_AD_DECODE_ERROR;
1223 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1224 SSL_R_CERT_LENGTH_MISMATCH);
1227 if (!sk_X509_push(sk, x)) {
1228 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1234 i = ssl_verify_cert_chain(s, sk);
1235 if ((s->verify_mode & SSL_VERIFY_PEER) && i <= 0) {
1236 al = ssl_verify_alarm_type(s->verify_result);
1237 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1238 SSL_R_CERTIFICATE_VERIFY_FAILED);
1241 ERR_clear_error(); /* but we keep s->verify_result */
1243 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1244 al = SSL_AD_HANDSHAKE_FAILURE;
1248 s->session->peer_chain = sk;
1250 * Inconsistency alert: cert_chain does include the peer's certificate,
1251 * which we don't include in statem_srvr.c
1253 x = sk_X509_value(sk, 0);
1256 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1259 pkey = X509_get0_pubkey(x);
1261 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1264 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1265 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1269 i = ssl_cert_type(x, pkey);
1273 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1274 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1278 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1279 if (exp_idx >= 0 && i != exp_idx
1280 && (exp_idx != SSL_PKEY_GOST_EC ||
1281 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1282 && i != SSL_PKEY_GOST01))) {
1284 al = SSL_AD_ILLEGAL_PARAMETER;
1285 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1286 SSL_R_WRONG_CERTIFICATE_TYPE);
1289 s->session->peer_type = i;
1291 X509_free(s->session->peer);
1293 s->session->peer = x;
1294 s->session->verify_result = s->verify_result;
1297 ret = MSG_PROCESS_CONTINUE_READING;
1301 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1303 ossl_statem_set_error(s);
1306 sk_X509_pop_free(sk, X509_free);
1310 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1315 EVP_PKEY *pkey = NULL;
1316 const EVP_MD *md = NULL;
1317 #ifndef OPENSSL_NO_RSA
1320 #ifndef OPENSSL_NO_EC
1321 EVP_PKEY_CTX *pctx = NULL;
1323 PACKET save_param_start, signature;
1325 md_ctx = EVP_MD_CTX_new();
1326 if (md_ctx == NULL) {
1327 al = SSL_AD_INTERNAL_ERROR;
1328 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1332 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1334 save_param_start = *pkt;
1336 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1337 EVP_PKEY_free(s->s3->peer_tmp);
1338 s->s3->peer_tmp = NULL;
1341 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1343 al = SSL_AD_DECODE_ERROR;
1345 #ifndef OPENSSL_NO_PSK
1346 /* PSK ciphersuites are preceded by an identity hint */
1347 if (alg_k & SSL_PSK) {
1348 PACKET psk_identity_hint;
1349 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1350 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1355 * Store PSK identity hint for later use, hint is used in
1356 * ssl3_send_client_key_exchange. Assume that the maximum length of
1357 * a PSK identity hint can be as long as the maximum length of a PSK
1360 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1361 al = SSL_AD_HANDSHAKE_FAILURE;
1362 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1366 if (PACKET_remaining(&psk_identity_hint) == 0) {
1367 OPENSSL_free(s->session->psk_identity_hint);
1368 s->session->psk_identity_hint = NULL;
1369 } else if (!PACKET_strndup(&psk_identity_hint,
1370 &s->session->psk_identity_hint)) {
1371 al = SSL_AD_INTERNAL_ERROR;
1376 /* Nothing else to do for plain PSK or RSAPSK */
1377 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1379 #endif /* !OPENSSL_NO_PSK */
1381 * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_*
1386 #ifndef OPENSSL_NO_SRP
1387 else if (alg_k & SSL_kSRP) {
1388 PACKET prime, generator, salt, server_pub;
1389 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1390 || !PACKET_get_length_prefixed_2(pkt, &generator)
1391 || !PACKET_get_length_prefixed_1(pkt, &salt)
1392 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1393 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1398 BN_bin2bn(PACKET_data(&prime),
1399 PACKET_remaining(&prime), NULL)) == NULL
1401 BN_bin2bn(PACKET_data(&generator),
1402 PACKET_remaining(&generator), NULL)) == NULL
1404 BN_bin2bn(PACKET_data(&salt),
1405 PACKET_remaining(&salt), NULL)) == NULL
1407 BN_bin2bn(PACKET_data(&server_pub),
1408 PACKET_remaining(&server_pub), NULL)) == NULL) {
1409 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1413 if (!srp_verify_server_param(s, &al)) {
1414 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1418 /* We must check if there is a certificate */
1419 if (alg_a & (SSL_aRSA|SSL_aDSS))
1420 pkey = X509_get0_pubkey(s->session->peer);
1422 #endif /* !OPENSSL_NO_SRP */
1423 #ifndef OPENSSL_NO_DH
1424 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1425 PACKET prime, generator, pub_key;
1426 EVP_PKEY *peer_tmp = NULL;
1429 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1431 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1432 || !PACKET_get_length_prefixed_2(pkt, &generator)
1433 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1434 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1438 peer_tmp = EVP_PKEY_new();
1441 if (peer_tmp == NULL || dh == NULL) {
1442 al = SSL_AD_INTERNAL_ERROR;
1443 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1447 p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL);
1448 g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator),
1450 bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key),
1452 if (p == NULL || g == NULL || bnpub_key == NULL) {
1453 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1457 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1458 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1462 if (!DH_set0_pqg(dh, p, NULL, g)) {
1463 al = SSL_AD_INTERNAL_ERROR;
1464 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1469 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1470 al = SSL_AD_INTERNAL_ERROR;
1471 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1476 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1477 al = SSL_AD_HANDSHAKE_FAILURE;
1478 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1482 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1483 al = SSL_AD_INTERNAL_ERROR;
1484 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1488 s->s3->peer_tmp = peer_tmp;
1496 EVP_PKEY_free(peer_tmp);
1500 * FIXME: This makes assumptions about which ciphersuites come with
1501 * public keys. We should have a less ad-hoc way of doing this
1503 if (alg_a & (SSL_aRSA|SSL_aDSS))
1504 pkey = X509_get0_pubkey(s->session->peer);
1505 /* else anonymous DH, so no certificate or pkey. */
1507 #endif /* !OPENSSL_NO_DH */
1509 #ifndef OPENSSL_NO_EC
1510 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1512 const unsigned char *ecparams;
1516 * Extract elliptic curve parameters and the server's ephemeral ECDH
1517 * public key. For now we only support named (not generic) curves and
1518 * ECParameters in this case is just three bytes.
1520 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1521 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1525 * Check curve is one of our preferences, if not server has sent an
1526 * invalid curve. ECParameters is 3 bytes.
1528 if (!tls1_check_curve(s, ecparams, 3)) {
1529 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1533 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2));
1534 if (curve_nid == 0) {
1535 al = SSL_AD_INTERNAL_ERROR;
1536 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1537 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1541 /* Set up EVP_PKEY with named curve as parameters */
1542 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1544 || EVP_PKEY_paramgen_init(pctx) <= 0
1545 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1546 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1547 al = SSL_AD_INTERNAL_ERROR;
1548 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1551 EVP_PKEY_CTX_free(pctx);
1554 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1555 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1559 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp),
1560 PACKET_data(&encoded_pt),
1561 PACKET_remaining(&encoded_pt), NULL) == 0) {
1562 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1567 * The ECC/TLS specification does not mention the use of DSA to sign
1568 * ECParameters in the server key exchange message. We do support RSA
1572 # ifndef OPENSSL_NO_RSA
1573 else if (alg_a & SSL_aRSA)
1574 pkey = X509_get0_pubkey(s->session->peer);
1576 # ifndef OPENSSL_NO_EC
1577 else if (alg_a & SSL_aECDSA)
1578 pkey = X509_get0_pubkey(s->session->peer);
1580 /* else anonymous ECDH, so no certificate or pkey. */
1582 al = SSL_AD_UNEXPECTED_MESSAGE;
1583 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1586 #endif /* !OPENSSL_NO_EC */
1588 /* if it was signed, check the signature */
1592 * |pkt| now points to the beginning of the signature, so the difference
1593 * equals the length of the parameters.
1595 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1596 PACKET_remaining(&save_param_start) -
1597 PACKET_remaining(pkt))) {
1598 al = SSL_AD_INTERNAL_ERROR;
1599 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1603 if (SSL_USE_SIGALGS(s)) {
1604 const unsigned char *sigalgs;
1606 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1607 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1610 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1617 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1619 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1620 md = EVP_md5_sha1();
1625 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1626 || PACKET_remaining(pkt) != 0) {
1627 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1630 j = EVP_PKEY_size(pkey);
1632 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1637 * Check signature length
1639 if (PACKET_remaining(&signature) > (size_t)j) {
1640 /* wrong packet length */
1641 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1644 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1645 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1646 SSL3_RANDOM_SIZE) <= 0
1647 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1648 SSL3_RANDOM_SIZE) <= 0
1649 || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms),
1650 PACKET_remaining(¶ms)) <= 0) {
1651 al = SSL_AD_INTERNAL_ERROR;
1652 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1655 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1656 PACKET_remaining(&signature), pkey) <= 0) {
1658 al = SSL_AD_DECRYPT_ERROR;
1659 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1663 /* aNULL, aSRP or PSK do not need public keys */
1664 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1665 /* Might be wrong key type, check it */
1666 if (ssl3_check_cert_and_algorithm(s))
1667 /* Otherwise this shouldn't happen */
1668 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1671 /* still data left over */
1672 if (PACKET_remaining(pkt) != 0) {
1673 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1677 EVP_MD_CTX_free(md_ctx);
1678 return MSG_PROCESS_CONTINUE_READING;
1680 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1682 #ifndef OPENSSL_NO_RSA
1685 #ifndef OPENSSL_NO_EC
1686 EVP_PKEY_CTX_free(pctx);
1688 EVP_MD_CTX_free(md_ctx);
1689 ossl_statem_set_error(s);
1690 return MSG_PROCESS_ERROR;
1693 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1695 int ret = MSG_PROCESS_ERROR;
1696 unsigned int list_len, ctype_num, i, name_len;
1697 X509_NAME *xn = NULL;
1698 const unsigned char *data;
1699 const unsigned char *namestart, *namebytes;
1700 STACK_OF(X509_NAME) *ca_sk = NULL;
1702 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1703 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1707 /* get the certificate types */
1708 if (!PACKET_get_1(pkt, &ctype_num)
1709 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1710 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1711 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1714 OPENSSL_free(s->cert->ctypes);
1715 s->cert->ctypes = NULL;
1716 if (ctype_num > SSL3_CT_NUMBER) {
1717 /* If we exceed static buffer copy all to cert structure */
1718 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1719 if (s->cert->ctypes == NULL) {
1720 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1723 memcpy(s->cert->ctypes, data, ctype_num);
1724 s->cert->ctype_num = (size_t)ctype_num;
1725 ctype_num = SSL3_CT_NUMBER;
1727 for (i = 0; i < ctype_num; i++)
1728 s->s3->tmp.ctype[i] = data[i];
1730 if (SSL_USE_SIGALGS(s)) {
1731 if (!PACKET_get_net_2(pkt, &list_len)
1732 || !PACKET_get_bytes(pkt, &data, list_len)) {
1733 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1734 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1735 SSL_R_LENGTH_MISMATCH);
1739 /* Clear certificate digests and validity flags */
1740 for (i = 0; i < SSL_PKEY_NUM; i++) {
1741 s->s3->tmp.md[i] = NULL;
1742 s->s3->tmp.valid_flags[i] = 0;
1744 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1745 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1746 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1747 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1750 if (!tls1_process_sigalgs(s)) {
1751 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1752 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1756 ssl_set_default_md(s);
1759 /* get the CA RDNs */
1760 if (!PACKET_get_net_2(pkt, &list_len)
1761 || PACKET_remaining(pkt) != list_len) {
1762 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1763 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1767 while (PACKET_remaining(pkt)) {
1768 if (!PACKET_get_net_2(pkt, &name_len)
1769 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1770 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1771 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1772 SSL_R_LENGTH_MISMATCH);
1776 namestart = namebytes;
1778 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1779 name_len)) == NULL) {
1780 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1781 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1785 if (namebytes != (namestart + name_len)) {
1786 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1787 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1788 SSL_R_CA_DN_LENGTH_MISMATCH);
1791 if (!sk_X509_NAME_push(ca_sk, xn)) {
1792 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1798 /* we should setup a certificate to return.... */
1799 s->s3->tmp.cert_req = 1;
1800 s->s3->tmp.ctype_num = ctype_num;
1801 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1802 s->s3->tmp.ca_names = ca_sk;
1805 ret = MSG_PROCESS_CONTINUE_PROCESSING;
1808 ossl_statem_set_error(s);
1811 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1815 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1817 return (X509_NAME_cmp(*a, *b));
1820 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1823 unsigned int ticklen;
1824 unsigned long ticket_lifetime_hint;
1826 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1827 || !PACKET_get_net_2(pkt, &ticklen)
1828 || PACKET_remaining(pkt) != ticklen) {
1829 al = SSL_AD_DECODE_ERROR;
1830 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1834 /* Server is allowed to change its mind and send an empty ticket. */
1836 return MSG_PROCESS_CONTINUE_READING;
1838 if (s->session->session_id_length > 0) {
1839 int i = s->session_ctx->session_cache_mode;
1840 SSL_SESSION *new_sess;
1842 * We reused an existing session, so we need to replace it with a new
1845 if (i & SSL_SESS_CACHE_CLIENT) {
1847 * Remove the old session from the cache
1849 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
1850 if (s->session_ctx->remove_session_cb != NULL)
1851 s->session_ctx->remove_session_cb(s->session_ctx,
1854 /* We carry on if this fails */
1855 SSL_CTX_remove_session(s->session_ctx, s->session);
1859 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1860 al = SSL_AD_INTERNAL_ERROR;
1861 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1865 SSL_SESSION_free(s->session);
1866 s->session = new_sess;
1869 OPENSSL_free(s->session->tlsext_tick);
1870 s->session->tlsext_ticklen = 0;
1872 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1873 if (s->session->tlsext_tick == NULL) {
1874 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1877 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1878 al = SSL_AD_DECODE_ERROR;
1879 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1883 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1884 s->session->tlsext_ticklen = ticklen;
1886 * There are two ways to detect a resumed ticket session. One is to set
1887 * an appropriate session ID and then the server must return a match in
1888 * ServerHello. This allows the normal client session ID matching to work
1889 * and we know much earlier that the ticket has been accepted. The
1890 * other way is to set zero length session ID when the ticket is
1891 * presented and rely on the handshake to determine session resumption.
1892 * We choose the former approach because this fits in with assumptions
1893 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1894 * SHA256 is disabled) hash of the ticket.
1896 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
1897 s->session->session_id, &s->session->session_id_length,
1898 EVP_sha256(), NULL)) {
1899 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
1902 return MSG_PROCESS_CONTINUE_READING;
1904 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1906 ossl_statem_set_error(s);
1907 return MSG_PROCESS_ERROR;
1910 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1913 unsigned long resplen;
1916 if (!PACKET_get_1(pkt, &type)
1917 || type != TLSEXT_STATUSTYPE_ocsp) {
1918 al = SSL_AD_DECODE_ERROR;
1919 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1922 if (!PACKET_get_net_3(pkt, &resplen)
1923 || PACKET_remaining(pkt) != resplen) {
1924 al = SSL_AD_DECODE_ERROR;
1925 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1928 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
1929 if (s->tlsext_ocsp_resp == NULL) {
1930 al = SSL_AD_INTERNAL_ERROR;
1931 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1934 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
1935 al = SSL_AD_DECODE_ERROR;
1936 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1939 s->tlsext_ocsp_resplen = resplen;
1940 return MSG_PROCESS_CONTINUE_READING;
1942 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1943 ossl_statem_set_error(s);
1944 return MSG_PROCESS_ERROR;
1947 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
1949 if (PACKET_remaining(pkt) > 0) {
1950 /* should contain no data */
1951 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1952 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1953 ossl_statem_set_error(s);
1954 return MSG_PROCESS_ERROR;
1957 #ifndef OPENSSL_NO_SRP
1958 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
1959 if (SRP_Calc_A_param(s) <= 0) {
1960 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
1961 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1962 ossl_statem_set_error(s);
1963 return MSG_PROCESS_ERROR;
1969 * at this point we check that we have the required stuff from
1972 if (!ssl3_check_cert_and_algorithm(s)) {
1973 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1974 ossl_statem_set_error(s);
1975 return MSG_PROCESS_ERROR;
1979 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
1980 * |tlsext_ocsp_resplen| values will be set if we actually received a status
1981 * message, or NULL and -1 otherwise
1983 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
1985 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1987 ssl3_send_alert(s, SSL3_AL_FATAL,
1988 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
1989 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
1990 SSL_R_INVALID_STATUS_RESPONSE);
1991 return MSG_PROCESS_ERROR;
1994 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1995 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
1996 return MSG_PROCESS_ERROR;
2000 #ifndef OPENSSL_NO_CT
2001 if (s->ct_validation_callback != NULL) {
2002 /* Note we validate the SCTs whether or not we abort on error */
2003 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2004 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2005 return MSG_PROCESS_ERROR;
2010 #ifndef OPENSSL_NO_SCTP
2011 /* Only applies to renegotiation */
2012 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2013 && s->renegotiate != 0)
2014 return MSG_PROCESS_CONTINUE_PROCESSING;
2017 return MSG_PROCESS_FINISHED_READING;
2020 int tls_construct_client_key_exchange(SSL *s)
2024 #ifndef OPENSSL_NO_PSK
2025 size_t pskhdrlen = 0;
2027 unsigned long alg_k;
2028 #ifndef OPENSSL_NO_RSA
2030 EVP_PKEY *pkey = NULL;
2031 EVP_PKEY_CTX *pctx = NULL;
2033 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2034 EVP_PKEY *ckey = NULL, *skey = NULL;
2036 #ifndef OPENSSL_NO_EC
2037 unsigned char *encodedPoint = NULL;
2038 int encoded_pt_len = 0;
2040 unsigned char *pms = NULL;
2042 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2044 p = ssl_handshake_start(s);
2047 #ifndef OPENSSL_NO_PSK
2048 if (alg_k & SSL_PSK) {
2051 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2052 * \0-terminated identity. The last byte is for us for simulating
2055 char identity[PSK_MAX_IDENTITY_LEN + 1];
2057 unsigned char psk[PSK_MAX_PSK_LEN];
2060 if (s->psk_client_callback == NULL) {
2061 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2062 SSL_R_PSK_NO_CLIENT_CB);
2066 memset(identity, 0, sizeof(identity));
2068 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2069 identity, sizeof(identity) - 1,
2072 if (psklen > PSK_MAX_PSK_LEN) {
2073 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2074 ERR_R_INTERNAL_ERROR);
2076 } else if (psklen == 0) {
2077 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2078 SSL_R_PSK_IDENTITY_NOT_FOUND);
2081 OPENSSL_free(s->s3->tmp.psk);
2082 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2083 OPENSSL_cleanse(psk, psklen);
2085 if (s->s3->tmp.psk == NULL) {
2086 OPENSSL_cleanse(identity, sizeof(identity));
2090 s->s3->tmp.psklen = psklen;
2091 identitylen = strlen(identity);
2092 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2093 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2094 ERR_R_INTERNAL_ERROR);
2097 OPENSSL_free(s->session->psk_identity);
2098 s->session->psk_identity = OPENSSL_strdup(identity);
2099 if (s->session->psk_identity == NULL) {
2100 OPENSSL_cleanse(identity, sizeof(identity));
2104 s2n(identitylen, p);
2105 memcpy(p, identity, identitylen);
2106 pskhdrlen = 2 + identitylen;
2110 OPENSSL_cleanse(identity, sizeof(identity));
2112 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2116 if (alg_k & SSL_kPSK) {
2121 /* Fool emacs indentation */
2124 #ifndef OPENSSL_NO_RSA
2125 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2127 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2128 pms = OPENSSL_malloc(pmslen);
2132 if (s->session->peer == NULL) {
2134 * We should always have a server certificate with SSL_kRSA.
2136 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2137 ERR_R_INTERNAL_ERROR);
2141 pkey = X509_get0_pubkey(s->session->peer);
2142 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2143 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2144 ERR_R_INTERNAL_ERROR);
2148 pms[0] = s->client_version >> 8;
2149 pms[1] = s->client_version & 0xff;
2150 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2154 /* Fix buf for TLS and beyond */
2155 if (s->version > SSL3_VERSION)
2157 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2158 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2159 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2160 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2164 if (EVP_PKEY_encrypt(pctx, p, &enclen, pms, pmslen) <= 0) {
2165 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2166 SSL_R_BAD_RSA_ENCRYPT);
2170 EVP_PKEY_CTX_free(pctx);
2173 if (s->options & SSL_OP_PKCS1_CHECK_1)
2175 if (s->options & SSL_OP_PKCS1_CHECK_2)
2179 /* Fix buf for TLS and beyond */
2180 if (s->version > SSL3_VERSION) {
2186 #ifndef OPENSSL_NO_DH
2187 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2189 const BIGNUM *pub_key;
2190 skey = s->s3->peer_tmp;
2192 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2193 ERR_R_INTERNAL_ERROR);
2196 ckey = ssl_generate_pkey(skey, NID_undef);
2197 dh_clnt = EVP_PKEY_get0_DH(ckey);
2199 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
2200 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2201 ERR_R_INTERNAL_ERROR);
2206 /* send off the data */
2207 DH_get0_key(dh_clnt, &pub_key, NULL);
2208 n = BN_num_bytes(pub_key);
2210 BN_bn2bin(pub_key, p);
2212 EVP_PKEY_free(ckey);
2217 #ifndef OPENSSL_NO_EC
2218 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2220 skey = s->s3->peer_tmp;
2221 if ((skey == NULL) || EVP_PKEY_get0_EC_KEY(skey) == NULL) {
2222 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2223 ERR_R_INTERNAL_ERROR);
2227 ckey = ssl_generate_pkey(skey, NID_undef);
2229 if (ssl_derive(s, ckey, skey) == 0) {
2230 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);
2234 /* Generate encoding of client key */
2235 encoded_pt_len = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey),
2236 POINT_CONVERSION_UNCOMPRESSED,
2237 &encodedPoint, NULL);
2239 if (encoded_pt_len == 0) {
2240 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2244 EVP_PKEY_free(ckey);
2249 *p = n; /* length of encoded point */
2250 /* Encoded point will be copied here */
2252 /* copy the point */
2253 memcpy(p, encodedPoint, n);
2254 /* increment n to account for length field */
2257 /* Free allocated memory */
2258 OPENSSL_free(encodedPoint);
2260 #endif /* !OPENSSL_NO_EC */
2261 #ifndef OPENSSL_NO_GOST
2262 else if (alg_k & SSL_kGOST) {
2263 /* GOST key exchange message creation */
2264 EVP_PKEY_CTX *pkey_ctx;
2267 unsigned int md_len;
2268 unsigned char shared_ukm[32], tmp[256];
2269 EVP_MD_CTX *ukm_hash;
2270 int dgst_nid = NID_id_GostR3411_94;
2271 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2272 dgst_nid = NID_id_GostR3411_2012_256;
2276 pms = OPENSSL_malloc(pmslen);
2281 * Get server sertificate PKEY and create ctx from it
2283 peer_cert = s->session->peer;
2285 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2286 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2290 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2291 if (pkey_ctx == NULL) {
2292 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2293 ERR_R_MALLOC_FAILURE);
2297 * If we have send a certificate, and certificate key
2298 * parameters match those of server certificate, use
2299 * certificate key for key exchange
2302 /* Otherwise, generate ephemeral key pair */
2304 if (pkey_ctx == NULL
2305 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2306 /* Generate session key */
2307 || RAND_bytes(pms, pmslen) <= 0) {
2308 EVP_PKEY_CTX_free(pkey_ctx);
2309 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2310 ERR_R_INTERNAL_ERROR);
2314 * If we have client certificate, use its secret as peer key
2316 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2317 if (EVP_PKEY_derive_set_peer
2318 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2320 * If there was an error - just ignore it. Ephemeral key
2327 * Compute shared IV and store it in algorithm-specific context
2330 ukm_hash = EVP_MD_CTX_new();
2331 if (EVP_DigestInit(ukm_hash,
2332 EVP_get_digestbynid(dgst_nid)) <= 0
2333 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2334 SSL3_RANDOM_SIZE) <= 0
2335 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2336 SSL3_RANDOM_SIZE) <= 0
2337 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2338 EVP_MD_CTX_free(ukm_hash);
2339 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2340 ERR_R_INTERNAL_ERROR);
2343 EVP_MD_CTX_free(ukm_hash);
2344 if (EVP_PKEY_CTX_ctrl
2345 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2347 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2351 /* Make GOST keytransport blob message */
2353 * Encapsulate it into sequence
2355 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2357 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2358 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2362 if (msglen >= 0x80) {
2364 *(p++) = msglen & 0xff;
2367 *(p++) = msglen & 0xff;
2370 memcpy(p, tmp, msglen);
2371 /* Check if pubkey from client certificate was used */
2372 if (EVP_PKEY_CTX_ctrl
2373 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2374 /* Set flag "skip certificate verify" */
2375 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2377 EVP_PKEY_CTX_free(pkey_ctx);
2381 #ifndef OPENSSL_NO_SRP
2382 else if (alg_k & SSL_kSRP) {
2383 if (s->srp_ctx.A != NULL) {
2384 /* send off the data */
2385 n = BN_num_bytes(s->srp_ctx.A);
2387 BN_bn2bin(s->srp_ctx.A, p);
2390 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2391 ERR_R_INTERNAL_ERROR);
2394 OPENSSL_free(s->session->srp_username);
2395 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2396 if (s->session->srp_username == NULL) {
2397 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2398 ERR_R_MALLOC_FAILURE);
2404 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2405 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2409 #ifndef OPENSSL_NO_PSK
2413 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2414 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2415 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2420 s->s3->tmp.pms = pms;
2421 s->s3->tmp.pmslen = pmslen;
2426 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2427 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2429 OPENSSL_clear_free(pms, pmslen);
2430 s->s3->tmp.pms = NULL;
2431 #ifndef OPENSSL_NO_RSA
2432 EVP_PKEY_CTX_free(pctx);
2434 #ifndef OPENSSL_NO_EC
2435 OPENSSL_free(encodedPoint);
2437 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2438 EVP_PKEY_free(ckey);
2440 #ifndef OPENSSL_NO_PSK
2441 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2442 s->s3->tmp.psk = NULL;
2444 ossl_statem_set_error(s);
2448 int tls_client_key_exchange_post_work(SSL *s)
2450 unsigned char *pms = NULL;
2453 pms = s->s3->tmp.pms;
2454 pmslen = s->s3->tmp.pmslen;
2456 #ifndef OPENSSL_NO_SRP
2458 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2459 if (!srp_generate_client_master_secret(s)) {
2460 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2461 ERR_R_INTERNAL_ERROR);
2468 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2469 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2470 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2473 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2474 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2475 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2476 /* ssl_generate_master_secret frees the pms even on error */
2484 #ifndef OPENSSL_NO_SCTP
2485 if (SSL_IS_DTLS(s)) {
2486 unsigned char sctpauthkey[64];
2487 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2490 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2493 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2494 sizeof(DTLS1_SCTP_AUTH_LABEL));
2496 if (SSL_export_keying_material(s, sctpauthkey,
2497 sizeof(sctpauthkey), labelbuffer,
2498 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2501 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2502 sizeof(sctpauthkey), sctpauthkey);
2508 OPENSSL_clear_free(pms, pmslen);
2509 s->s3->tmp.pms = NULL;
2513 int tls_construct_client_verify(SSL *s)
2517 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2520 unsigned long n = 0;
2524 mctx = EVP_MD_CTX_new();
2526 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2530 p = ssl_handshake_start(s);
2531 pkey = s->cert->key->privatekey;
2533 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2534 if (hdatalen <= 0) {
2535 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2538 if (SSL_USE_SIGALGS(s)) {
2539 if (!tls12_get_sigandhash(p, pkey, md)) {
2540 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2547 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2549 if (!EVP_SignInit_ex(mctx, md, NULL)
2550 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2551 || (s->version == SSL3_VERSION
2552 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2553 s->session->master_key_length,
2554 s->session->master_key))
2555 || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
2556 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2559 #ifndef OPENSSL_NO_GOST
2561 int pktype = EVP_PKEY_id(pkey);
2562 if (pktype == NID_id_GostR3410_2001
2563 || pktype == NID_id_GostR3410_2012_256
2564 || pktype == NID_id_GostR3410_2012_512)
2565 BUF_reverse(p + 2, NULL, u);
2571 /* Digest cached records and discard handshake buffer */
2572 if (!ssl3_digest_cached_records(s, 0))
2574 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2575 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2579 EVP_MD_CTX_free(mctx);
2582 EVP_MD_CTX_free(mctx);
2587 * Check a certificate can be used for client authentication. Currently check
2588 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2589 * certificates can be used and optionally checks suitability for Suite B.
2591 static int ssl3_check_client_certificate(SSL *s)
2593 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2595 /* If no suitable signature algorithm can't use certificate */
2596 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2599 * If strict mode check suitability of chain before using it. This also
2600 * adjusts suite B digest if necessary.
2602 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2603 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2608 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2611 EVP_PKEY *pkey = NULL;
2614 if (wst == WORK_MORE_A) {
2615 /* Let cert callback update client certificates if required */
2616 if (s->cert->cert_cb) {
2617 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2619 s->rwstate = SSL_X509_LOOKUP;
2623 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2624 ossl_statem_set_error(s);
2627 s->rwstate = SSL_NOTHING;
2629 if (ssl3_check_client_certificate(s))
2630 return WORK_FINISHED_CONTINUE;
2632 /* Fall through to WORK_MORE_B */
2636 /* We need to get a client cert */
2637 if (wst == WORK_MORE_B) {
2639 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2640 * return(-1); We then get retied later
2642 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2644 s->rwstate = SSL_X509_LOOKUP;
2647 s->rwstate = SSL_NOTHING;
2648 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2649 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2651 } else if (i == 1) {
2653 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2654 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2658 EVP_PKEY_free(pkey);
2659 if (i && !ssl3_check_client_certificate(s))
2662 if (s->version == SSL3_VERSION) {
2663 s->s3->tmp.cert_req = 0;
2664 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2665 return WORK_FINISHED_CONTINUE;
2667 s->s3->tmp.cert_req = 2;
2668 if (!ssl3_digest_cached_records(s, 0)) {
2669 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2670 ossl_statem_set_error(s);
2676 return WORK_FINISHED_CONTINUE;
2679 /* Shouldn't ever get here */
2683 int tls_construct_client_certificate(SSL *s)
2685 if (!ssl3_output_cert_chain(s,
2686 (s->s3->tmp.cert_req ==
2687 2) ? NULL : s->cert->key)) {
2688 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2689 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2690 ossl_statem_set_error(s);
2697 #define has_bits(i,m) (((i)&(m)) == (m))
2699 int ssl3_check_cert_and_algorithm(SSL *s)
2702 #ifndef OPENSSL_NO_EC
2706 EVP_PKEY *pkey = NULL;
2707 int al = SSL_AD_HANDSHAKE_FAILURE;
2709 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2710 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2712 /* we don't have a certificate */
2713 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2716 /* This is the passed certificate */
2718 #ifndef OPENSSL_NO_EC
2719 idx = s->session->peer_type;
2720 if (idx == SSL_PKEY_ECC) {
2721 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2723 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2728 } else if (alg_a & SSL_aECDSA) {
2729 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2730 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2734 pkey = X509_get0_pubkey(s->session->peer);
2735 i = X509_certificate_type(s->session->peer, pkey);
2737 /* Check that we have a certificate if we require one */
2738 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2739 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2740 SSL_R_MISSING_RSA_SIGNING_CERT);
2743 #ifndef OPENSSL_NO_DSA
2744 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2745 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2746 SSL_R_MISSING_DSA_SIGNING_CERT);
2750 #ifndef OPENSSL_NO_RSA
2751 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2752 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2753 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2754 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2758 #ifndef OPENSSL_NO_DH
2759 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
2760 al = SSL_AD_INTERNAL_ERROR;
2761 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2768 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2772 #ifndef OPENSSL_NO_NEXTPROTONEG
2773 int tls_construct_next_proto(SSL *s)
2775 unsigned int len, padding_len;
2778 len = s->next_proto_negotiated_len;
2779 padding_len = 32 - ((len + 2) % 32);
2780 d = (unsigned char *)s->init_buf->data;
2782 memcpy(d + 5, s->next_proto_negotiated, len);
2783 d[5 + len] = padding_len;
2784 memset(d + 6 + len, 0, padding_len);
2785 *(d++) = SSL3_MT_NEXT_PROTO;
2786 l2n3(2 + len + padding_len, d);
2787 s->init_num = 4 + 2 + len + padding_len;
2794 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2797 #ifndef OPENSSL_NO_ENGINE
2798 if (s->ctx->client_cert_engine) {
2799 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2800 SSL_get_client_CA_list(s),
2801 px509, ppkey, NULL, NULL, NULL);
2806 if (s->ctx->client_cert_cb)
2807 i = s->ctx->client_cert_cb(s, px509, ppkey);
2811 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
2815 const SSL_CIPHER *c;
2817 int empty_reneg_info_scsv = !s->renegotiate;
2818 /* Set disabled masks for this session */
2819 ssl_set_client_disabled(s);
2825 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2826 c = sk_SSL_CIPHER_value(sk, i);
2827 /* Skip disabled ciphers */
2828 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2830 j = s->method->put_cipher_by_char(c, p);
2834 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2838 if (empty_reneg_info_scsv) {
2839 static SSL_CIPHER scsv = {
2840 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2842 j = s->method->put_cipher_by_char(&scsv, p);
2845 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2846 static SSL_CIPHER scsv = {
2847 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2849 j = s->method->put_cipher_by_char(&scsv, p);