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;
273 /* No valid transition found */
278 * client_write_transition() works out what handshake state to move to next
279 * when the client is writing messages to be sent to the server.
281 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
283 OSSL_STATEM *st = &s->statem;
285 switch(st->hand_state) {
287 /* Renegotiation - fall through */
289 st->hand_state = TLS_ST_CW_CLNT_HELLO;
290 return WRITE_TRAN_CONTINUE;
292 case TLS_ST_CW_CLNT_HELLO:
294 * No transition at the end of writing because we don't know what
297 return WRITE_TRAN_FINISHED;
299 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
300 st->hand_state = TLS_ST_CW_CLNT_HELLO;
301 return WRITE_TRAN_CONTINUE;
303 case TLS_ST_CR_SRVR_DONE:
304 if (s->s3->tmp.cert_req)
305 st->hand_state = TLS_ST_CW_CERT;
307 st->hand_state = TLS_ST_CW_KEY_EXCH;
308 return WRITE_TRAN_CONTINUE;
311 st->hand_state = TLS_ST_CW_KEY_EXCH;
312 return WRITE_TRAN_CONTINUE;
314 case TLS_ST_CW_KEY_EXCH:
316 * For TLS, cert_req is set to 2, so a cert chain of nothing is
317 * sent, but no verify packet is sent
320 * XXX: For now, we do not support client authentication in ECDH
321 * cipher suites with ECDH (rather than ECDSA) certificates. We
322 * need to skip the certificate verify message when client's
323 * ECDH public key is sent inside the client certificate.
325 if (s->s3->tmp.cert_req == 1) {
326 st->hand_state = TLS_ST_CW_CERT_VRFY;
328 st->hand_state = TLS_ST_CW_CHANGE;
330 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
331 st->hand_state = TLS_ST_CW_CHANGE;
333 return WRITE_TRAN_CONTINUE;
335 case TLS_ST_CW_CERT_VRFY:
336 st->hand_state = TLS_ST_CW_CHANGE;
337 return WRITE_TRAN_CONTINUE;
339 case TLS_ST_CW_CHANGE:
340 #if defined(OPENSSL_NO_NEXTPROTONEG)
341 st->hand_state = TLS_ST_CW_FINISHED;
343 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
344 st->hand_state = TLS_ST_CW_NEXT_PROTO;
346 st->hand_state = TLS_ST_CW_FINISHED;
348 return WRITE_TRAN_CONTINUE;
350 #if !defined(OPENSSL_NO_NEXTPROTONEG)
351 case TLS_ST_CW_NEXT_PROTO:
352 st->hand_state = TLS_ST_CW_FINISHED;
353 return WRITE_TRAN_CONTINUE;
356 case TLS_ST_CW_FINISHED:
358 st->hand_state = TLS_ST_OK;
359 ossl_statem_set_in_init(s, 0);
360 return WRITE_TRAN_CONTINUE;
362 return WRITE_TRAN_FINISHED;
365 case TLS_ST_CR_FINISHED:
367 st->hand_state = TLS_ST_CW_CHANGE;
368 return WRITE_TRAN_CONTINUE;
370 st->hand_state = TLS_ST_OK;
371 ossl_statem_set_in_init(s, 0);
372 return WRITE_TRAN_CONTINUE;
376 /* Shouldn't happen */
377 return WRITE_TRAN_ERROR;
382 * Perform any pre work that needs to be done prior to sending a message from
383 * the client to the server.
385 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
387 OSSL_STATEM *st = &s->statem;
389 switch(st->hand_state) {
390 case TLS_ST_CW_CLNT_HELLO:
392 if (SSL_IS_DTLS(s)) {
393 /* every DTLS ClientHello resets Finished MAC */
394 ssl3_init_finished_mac(s);
399 return tls_prepare_client_certificate(s, wst);
401 case TLS_ST_CW_CHANGE:
402 if (SSL_IS_DTLS(s)) {
405 * We're into the last flight so we don't retransmit these
406 * messages unless we need to.
410 #ifndef OPENSSL_NO_SCTP
411 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
412 return dtls_wait_for_dry(s);
415 return WORK_FINISHED_CONTINUE;
418 return tls_finish_handshake(s, wst);
421 /* No pre work to be done */
425 return WORK_FINISHED_CONTINUE;
429 * Perform any work that needs to be done after sending a message from the
430 * client to the server.
432 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
434 OSSL_STATEM *st = &s->statem;
438 switch(st->hand_state) {
439 case TLS_ST_CW_CLNT_HELLO:
440 if (wst == WORK_MORE_A && statem_flush(s) != 1)
443 if (SSL_IS_DTLS(s)) {
444 /* Treat the next message as the first packet */
449 case TLS_ST_CW_KEY_EXCH:
450 if (tls_client_key_exchange_post_work(s) == 0)
454 case TLS_ST_CW_CHANGE:
455 s->session->cipher = s->s3->tmp.new_cipher;
456 #ifdef OPENSSL_NO_COMP
457 s->session->compress_meth = 0;
459 if (s->s3->tmp.new_compression == NULL)
460 s->session->compress_meth = 0;
462 s->session->compress_meth = s->s3->tmp.new_compression->id;
464 if (!s->method->ssl3_enc->setup_key_block(s))
467 if (!s->method->ssl3_enc->change_cipher_state(s,
468 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
471 if (SSL_IS_DTLS(s)) {
472 #ifndef OPENSSL_NO_SCTP
475 * Change to new shared key of SCTP-Auth, will be ignored if
478 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
483 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
487 case TLS_ST_CW_FINISHED:
488 #ifndef OPENSSL_NO_SCTP
489 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
491 * Change to new shared key of SCTP-Auth, will be ignored if
494 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
498 if (statem_flush(s) != 1)
503 /* No post work to be done */
507 return WORK_FINISHED_CONTINUE;
511 * Construct a message to be sent from the client to the server.
513 * Valid return values are:
517 int ossl_statem_client_construct_message(SSL *s)
519 OSSL_STATEM *st = &s->statem;
521 switch(st->hand_state) {
522 case TLS_ST_CW_CLNT_HELLO:
523 return tls_construct_client_hello(s);
526 return tls_construct_client_certificate(s);
528 case TLS_ST_CW_KEY_EXCH:
529 return tls_construct_client_key_exchange(s);
531 case TLS_ST_CW_CERT_VRFY:
532 return tls_construct_client_verify(s);
534 case TLS_ST_CW_CHANGE:
536 return dtls_construct_change_cipher_spec(s);
538 return tls_construct_change_cipher_spec(s);
540 #if !defined(OPENSSL_NO_NEXTPROTONEG)
541 case TLS_ST_CW_NEXT_PROTO:
542 return tls_construct_next_proto(s);
544 case TLS_ST_CW_FINISHED:
545 return tls_construct_finished(s,
547 ssl3_enc->client_finished_label,
549 ssl3_enc->client_finished_label_len);
552 /* Shouldn't happen */
560 * Returns the maximum allowed length for the current message that we are
561 * reading. Excludes the message header.
563 unsigned long ossl_statem_client_max_message_size(SSL *s)
565 OSSL_STATEM *st = &s->statem;
567 switch(st->hand_state) {
568 case TLS_ST_CR_SRVR_HELLO:
569 return SERVER_HELLO_MAX_LENGTH;
571 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
572 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
575 return s->max_cert_list;
577 case TLS_ST_CR_CERT_STATUS:
578 return SSL3_RT_MAX_PLAIN_LENGTH;
580 case TLS_ST_CR_KEY_EXCH:
581 return SERVER_KEY_EXCH_MAX_LENGTH;
583 case TLS_ST_CR_CERT_REQ:
584 /* Set to s->max_cert_list for compatibility with previous releases.
585 * In practice these messages can get quite long if servers are
586 * configured to provide a long list of acceptable CAs
588 return s->max_cert_list;
590 case TLS_ST_CR_SRVR_DONE:
591 return SERVER_HELLO_DONE_MAX_LENGTH;
593 case TLS_ST_CR_CHANGE:
594 return CCS_MAX_LENGTH;
596 case TLS_ST_CR_SESSION_TICKET:
597 return SSL3_RT_MAX_PLAIN_LENGTH;
599 case TLS_ST_CR_FINISHED:
600 return FINISHED_MAX_LENGTH;
603 /* Shouldn't happen */
611 * Process a message that the client has been received from the server.
613 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
615 OSSL_STATEM *st = &s->statem;
617 switch(st->hand_state) {
618 case TLS_ST_CR_SRVR_HELLO:
619 return tls_process_server_hello(s, pkt);
621 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
622 return dtls_process_hello_verify(s, pkt);
625 return tls_process_server_certificate(s, pkt);
627 case TLS_ST_CR_CERT_STATUS:
628 return tls_process_cert_status(s, pkt);
630 case TLS_ST_CR_KEY_EXCH:
631 return tls_process_key_exchange(s, pkt);
633 case TLS_ST_CR_CERT_REQ:
634 return tls_process_certificate_request(s, pkt);
636 case TLS_ST_CR_SRVR_DONE:
637 return tls_process_server_done(s, pkt);
639 case TLS_ST_CR_CHANGE:
640 return tls_process_change_cipher_spec(s, pkt);
642 case TLS_ST_CR_SESSION_TICKET:
643 return tls_process_new_session_ticket(s, pkt);
645 case TLS_ST_CR_FINISHED:
646 return tls_process_finished(s, pkt);
649 /* Shouldn't happen */
653 return MSG_PROCESS_ERROR;
657 * Perform any further processing required following the receipt of a message
660 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
662 OSSL_STATEM *st = &s->statem;
664 switch(st->hand_state) {
665 #ifndef OPENSSL_NO_SCTP
666 case TLS_ST_CR_SRVR_DONE:
667 /* We only get here if we are using SCTP and we are renegotiating */
668 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
669 s->s3->in_read_app_data = 2;
670 s->rwstate = SSL_READING;
671 BIO_clear_retry_flags(SSL_get_rbio(s));
672 BIO_set_retry_read(SSL_get_rbio(s));
673 ossl_statem_set_sctp_read_sock(s, 1);
676 ossl_statem_set_sctp_read_sock(s, 0);
677 return WORK_FINISHED_STOP;
684 /* Shouldn't happen */
688 int tls_construct_client_hello(SSL *s)
691 unsigned char *p, *d;
696 #ifndef OPENSSL_NO_COMP
700 SSL_SESSION *sess = s->session;
702 buf = (unsigned char *)s->init_buf->data;
704 /* Work out what SSL/TLS/DTLS version to use */
705 protverr = ssl_set_client_hello_version(s);
707 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
711 if ((sess == NULL) ||
712 !ssl_version_supported(s, sess->ssl_version) ||
714 * In the case of EAP-FAST, we can have a pre-shared
715 * "ticket" without a session ID.
717 (!sess->session_id_length && !sess->tlsext_tick) ||
718 (sess->not_resumable)) {
719 if (!ssl_get_new_session(s, 0))
722 /* else use the pre-loaded session */
724 p = s->s3->client_random;
727 * for DTLS if client_random is initialized, reuse it, we are
728 * required to use same upon reply to HelloVerify
730 if (SSL_IS_DTLS(s)) {
733 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
742 if (i && ssl_fill_hello_random(s, 0, p,
743 sizeof(s->s3->client_random)) <= 0)
746 /* Do the message type and length last */
747 d = p = ssl_handshake_start(s);
750 * version indicates the negotiated version: for example from
751 * an SSLv2/v3 compatible client hello). The client_version
752 * field is the maximum version we permit and it is also
753 * used in RSA encrypted premaster secrets. Some servers can
754 * choke if we initially report a higher version then
755 * renegotiate to a lower one in the premaster secret. This
756 * didn't happen with TLS 1.0 as most servers supported it
757 * but it can with TLS 1.1 or later if the server only supports
760 * Possible scenario with previous logic:
761 * 1. Client hello indicates TLS 1.2
762 * 2. Server hello says TLS 1.0
763 * 3. RSA encrypted premaster secret uses 1.2.
764 * 4. Handshake proceeds using TLS 1.0.
765 * 5. Server sends hello request to renegotiate.
766 * 6. Client hello indicates TLS v1.0 as we now
767 * know that is maximum server supports.
768 * 7. Server chokes on RSA encrypted premaster secret
769 * containing version 1.0.
771 * For interoperability it should be OK to always use the
772 * maximum version we support in client hello and then rely
773 * on the checking of version to ensure the servers isn't
774 * being inconsistent: for example initially negotiating with
775 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
776 * client_version in client hello and not resetting it to
777 * the negotiated version.
779 *(p++) = s->client_version >> 8;
780 *(p++) = s->client_version & 0xff;
783 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
784 p += SSL3_RANDOM_SIZE;
790 i = s->session->session_id_length;
793 if (i > (int)sizeof(s->session->session_id)) {
794 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
797 memcpy(p, s->session->session_id, i);
801 /* cookie stuff for DTLS */
802 if (SSL_IS_DTLS(s)) {
803 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
804 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
807 *(p++) = s->d1->cookie_len;
808 memcpy(p, s->d1->cookie, s->d1->cookie_len);
809 p += s->d1->cookie_len;
812 /* Ciphers supported */
813 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
815 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
818 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
820 * Some servers hang if client hello > 256 bytes as hack workaround
821 * chop number of supported ciphers to keep it well below this if we
824 if (TLS1_get_version(s) >= TLS1_2_VERSION
825 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
826 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
832 #ifdef OPENSSL_NO_COMP
836 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
839 j = sk_SSL_COMP_num(s->ctx->comp_methods);
841 for (i = 0; i < j; i++) {
842 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
846 *(p++) = 0; /* Add the NULL method */
849 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
850 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
854 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
856 ssl3_send_alert(s, SSL3_AL_FATAL, al);
857 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
862 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
863 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
864 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
870 ossl_statem_set_error(s);
874 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
877 unsigned int cookie_len;
880 if (!PACKET_forward(pkt, 2)
881 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
882 al = SSL_AD_DECODE_ERROR;
883 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
887 cookie_len = PACKET_remaining(&cookiepkt);
888 if (cookie_len > sizeof(s->d1->cookie)) {
889 al = SSL_AD_ILLEGAL_PARAMETER;
890 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
894 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
895 al = SSL_AD_DECODE_ERROR;
896 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
899 s->d1->cookie_len = cookie_len;
901 return MSG_PROCESS_FINISHED_READING;
903 ssl3_send_alert(s, SSL3_AL_FATAL, al);
904 ossl_statem_set_error(s);
905 return MSG_PROCESS_ERROR;
908 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
910 STACK_OF(SSL_CIPHER) *sk;
913 size_t session_id_len;
914 const unsigned char *cipherchars;
915 int i, al = SSL_AD_INTERNAL_ERROR;
916 unsigned int compression;
917 unsigned int sversion;
919 #ifndef OPENSSL_NO_COMP
923 if (!PACKET_get_net_2(pkt, &sversion)) {
924 al = SSL_AD_DECODE_ERROR;
925 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
929 protverr = ssl_choose_client_version(s, sversion);
931 al = SSL_AD_PROTOCOL_VERSION;
932 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
936 /* load the server hello data */
937 /* load the server random */
938 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
939 al = SSL_AD_DECODE_ERROR;
940 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
946 /* Get the session-id. */
947 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
948 al = SSL_AD_DECODE_ERROR;
949 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
952 session_id_len = PACKET_remaining(&session_id);
953 if (session_id_len > sizeof s->session->session_id
954 || session_id_len > SSL3_SESSION_ID_SIZE) {
955 al = SSL_AD_ILLEGAL_PARAMETER;
956 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
960 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
961 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
962 al = SSL_AD_DECODE_ERROR;
967 * Check if we can resume the session based on external pre-shared secret.
968 * EAP-FAST (RFC 4851) supports two types of session resumption.
969 * Resumption based on server-side state works with session IDs.
970 * Resumption based on pre-shared Protected Access Credentials (PACs)
971 * works by overriding the SessionTicket extension at the application
972 * layer, and does not send a session ID. (We do not know whether EAP-FAST
973 * servers would honour the session ID.) Therefore, the session ID alone
974 * is not a reliable indicator of session resumption, so we first check if
975 * we can resume, and later peek at the next handshake message to see if the
976 * server wants to resume.
978 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
979 s->session->tlsext_tick) {
980 const SSL_CIPHER *pref_cipher = NULL;
981 s->session->master_key_length = sizeof(s->session->master_key);
982 if (s->tls_session_secret_cb(s, s->session->master_key,
983 &s->session->master_key_length,
985 s->tls_session_secret_cb_arg)) {
986 s->session->cipher = pref_cipher ?
987 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
989 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
990 al = SSL_AD_INTERNAL_ERROR;
995 if (session_id_len != 0 && session_id_len == s->session->session_id_length
996 && memcmp(PACKET_data(&session_id), s->session->session_id,
997 session_id_len) == 0) {
998 if (s->sid_ctx_length != s->session->sid_ctx_length
999 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1000 /* actually a client application bug */
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1003 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1009 * If we were trying for session-id reuse but the server
1010 * didn't echo the ID, make a new SSL_SESSION.
1011 * In the case of EAP-FAST and PAC, we do not send a session ID,
1012 * so the PAC-based session secret is always preserved. It'll be
1013 * overwritten if the server refuses resumption.
1015 if (s->session->session_id_length > 0) {
1016 if (!ssl_get_new_session(s, 0)) {
1021 s->session->ssl_version = s->version;
1022 s->session->session_id_length = session_id_len;
1023 /* session_id_len could be 0 */
1024 memcpy(s->session->session_id, PACKET_data(&session_id),
1028 /* Session version and negotiated protocol version should match */
1029 if (s->version != s->session->ssl_version) {
1030 al = SSL_AD_PROTOCOL_VERSION;
1032 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1033 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1037 c = ssl_get_cipher_by_char(s, cipherchars);
1039 /* unknown cipher */
1040 al = SSL_AD_ILLEGAL_PARAMETER;
1041 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1045 * Now that we know the version, update the check to see if it's an allowed
1048 s->s3->tmp.min_ver = s->version;
1049 s->s3->tmp.max_ver = s->version;
1051 * If it is a disabled cipher we either didn't send it in client hello,
1052 * or it's not allowed for the selected protocol. So we return an error.
1054 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1055 al = SSL_AD_ILLEGAL_PARAMETER;
1056 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1060 sk = ssl_get_ciphers_by_id(s);
1061 i = sk_SSL_CIPHER_find(sk, c);
1063 /* we did not say we would use this cipher */
1064 al = SSL_AD_ILLEGAL_PARAMETER;
1065 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1070 * Depending on the session caching (internal/external), the cipher
1071 * and/or cipher_id values may not be set. Make sure that cipher_id is
1072 * set and use it for comparison.
1074 if (s->session->cipher)
1075 s->session->cipher_id = s->session->cipher->id;
1076 if (s->hit && (s->session->cipher_id != c->id)) {
1077 al = SSL_AD_ILLEGAL_PARAMETER;
1078 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1079 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1082 s->s3->tmp.new_cipher = c;
1083 /* lets get the compression algorithm */
1085 if (!PACKET_get_1(pkt, &compression)) {
1086 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1087 al = SSL_AD_DECODE_ERROR;
1090 #ifdef OPENSSL_NO_COMP
1091 if (compression != 0) {
1092 al = SSL_AD_ILLEGAL_PARAMETER;
1093 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1094 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1098 * If compression is disabled we'd better not try to resume a session
1099 * using compression.
1101 if (s->session->compress_meth != 0) {
1102 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1106 if (s->hit && compression != s->session->compress_meth) {
1107 al = SSL_AD_ILLEGAL_PARAMETER;
1108 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1109 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1112 if (compression == 0)
1114 else if (!ssl_allow_compression(s)) {
1115 al = SSL_AD_ILLEGAL_PARAMETER;
1116 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1119 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1122 if (compression != 0 && comp == NULL) {
1123 al = SSL_AD_ILLEGAL_PARAMETER;
1124 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1125 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1128 s->s3->tmp.new_compression = comp;
1132 /* TLS extensions */
1133 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1134 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1138 if (PACKET_remaining(pkt) != 0) {
1139 /* wrong packet length */
1140 al = SSL_AD_DECODE_ERROR;
1141 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1145 #ifndef OPENSSL_NO_SCTP
1146 if (SSL_IS_DTLS(s) && s->hit) {
1147 unsigned char sctpauthkey[64];
1148 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1151 * Add new shared key for SCTP-Auth, will be ignored if
1154 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1155 sizeof(DTLS1_SCTP_AUTH_LABEL));
1157 if (SSL_export_keying_material(s, sctpauthkey,
1158 sizeof(sctpauthkey),
1160 sizeof(labelbuffer), NULL, 0,
1164 BIO_ctrl(SSL_get_wbio(s),
1165 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1166 sizeof(sctpauthkey), sctpauthkey);
1170 return MSG_PROCESS_CONTINUE_READING;
1172 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1174 ossl_statem_set_error(s);
1175 return MSG_PROCESS_ERROR;
1178 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1180 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1181 unsigned long cert_list_len, cert_len;
1183 const unsigned char *certstart, *certbytes;
1184 STACK_OF(X509) *sk = NULL;
1185 EVP_PKEY *pkey = NULL;
1187 if ((sk = sk_X509_new_null()) == NULL) {
1188 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1192 if (!PACKET_get_net_3(pkt, &cert_list_len)
1193 || PACKET_remaining(pkt) != cert_list_len) {
1194 al = SSL_AD_DECODE_ERROR;
1195 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1198 while (PACKET_remaining(pkt)) {
1199 if (!PACKET_get_net_3(pkt, &cert_len)
1200 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1201 al = SSL_AD_DECODE_ERROR;
1202 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1203 SSL_R_CERT_LENGTH_MISMATCH);
1207 certstart = certbytes;
1208 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1210 al = SSL_AD_BAD_CERTIFICATE;
1211 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1214 if (certbytes != (certstart + cert_len)) {
1215 al = SSL_AD_DECODE_ERROR;
1216 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1217 SSL_R_CERT_LENGTH_MISMATCH);
1220 if (!sk_X509_push(sk, x)) {
1221 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1227 i = ssl_verify_cert_chain(s, sk);
1228 if ((s->verify_mode & SSL_VERIFY_PEER) && i <= 0) {
1229 al = ssl_verify_alarm_type(s->verify_result);
1230 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1231 SSL_R_CERTIFICATE_VERIFY_FAILED);
1234 ERR_clear_error(); /* but we keep s->verify_result */
1236 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1237 al = SSL_AD_HANDSHAKE_FAILURE;
1241 s->session->peer_chain = sk;
1243 * Inconsistency alert: cert_chain does include the peer's certificate,
1244 * which we don't include in statem_srvr.c
1246 x = sk_X509_value(sk, 0);
1249 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1252 pkey = X509_get0_pubkey(x);
1254 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1257 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1258 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1262 i = ssl_cert_type(x, pkey);
1266 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1267 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1271 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1272 if (exp_idx >= 0 && i != exp_idx
1273 && (exp_idx != SSL_PKEY_GOST_EC ||
1274 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1275 && i != SSL_PKEY_GOST01))) {
1277 al = SSL_AD_ILLEGAL_PARAMETER;
1278 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1279 SSL_R_WRONG_CERTIFICATE_TYPE);
1282 s->session->peer_type = i;
1284 X509_free(s->session->peer);
1286 s->session->peer = x;
1287 s->session->verify_result = s->verify_result;
1290 ret = MSG_PROCESS_CONTINUE_READING;
1294 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1296 ossl_statem_set_error(s);
1299 sk_X509_pop_free(sk, X509_free);
1303 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1308 EVP_PKEY *pkey = NULL;
1309 const EVP_MD *md = NULL;
1310 #ifndef OPENSSL_NO_RSA
1313 #ifndef OPENSSL_NO_EC
1314 EVP_PKEY_CTX *pctx = NULL;
1316 PACKET save_param_start, signature;
1318 md_ctx = EVP_MD_CTX_new();
1319 if (md_ctx == NULL) {
1320 al = SSL_AD_INTERNAL_ERROR;
1321 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1325 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1327 save_param_start = *pkt;
1329 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1330 EVP_PKEY_free(s->s3->peer_tmp);
1331 s->s3->peer_tmp = NULL;
1334 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1336 al = SSL_AD_DECODE_ERROR;
1338 #ifndef OPENSSL_NO_PSK
1339 /* PSK ciphersuites are preceded by an identity hint */
1340 if (alg_k & SSL_PSK) {
1341 PACKET psk_identity_hint;
1342 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1343 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1348 * Store PSK identity hint for later use, hint is used in
1349 * ssl3_send_client_key_exchange. Assume that the maximum length of
1350 * a PSK identity hint can be as long as the maximum length of a PSK
1353 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1354 al = SSL_AD_HANDSHAKE_FAILURE;
1355 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1359 if (PACKET_remaining(&psk_identity_hint) == 0) {
1360 OPENSSL_free(s->session->psk_identity_hint);
1361 s->session->psk_identity_hint = NULL;
1362 } else if (!PACKET_strndup(&psk_identity_hint,
1363 &s->session->psk_identity_hint)) {
1364 al = SSL_AD_INTERNAL_ERROR;
1369 /* Nothing else to do for plain PSK or RSAPSK */
1370 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1372 #endif /* !OPENSSL_NO_PSK */
1374 * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_*
1379 #ifndef OPENSSL_NO_SRP
1380 else if (alg_k & SSL_kSRP) {
1381 PACKET prime, generator, salt, server_pub;
1382 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1383 || !PACKET_get_length_prefixed_2(pkt, &generator)
1384 || !PACKET_get_length_prefixed_1(pkt, &salt)
1385 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1386 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1391 BN_bin2bn(PACKET_data(&prime),
1392 PACKET_remaining(&prime), NULL)) == NULL
1394 BN_bin2bn(PACKET_data(&generator),
1395 PACKET_remaining(&generator), NULL)) == NULL
1397 BN_bin2bn(PACKET_data(&salt),
1398 PACKET_remaining(&salt), NULL)) == NULL
1400 BN_bin2bn(PACKET_data(&server_pub),
1401 PACKET_remaining(&server_pub), NULL)) == NULL) {
1402 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1406 if (!srp_verify_server_param(s, &al)) {
1407 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1411 /* We must check if there is a certificate */
1412 if (alg_a & (SSL_aRSA|SSL_aDSS))
1413 pkey = X509_get0_pubkey(s->session->peer);
1415 #endif /* !OPENSSL_NO_SRP */
1416 #ifndef OPENSSL_NO_DH
1417 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1418 PACKET prime, generator, pub_key;
1419 EVP_PKEY *peer_tmp = NULL;
1422 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1424 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1425 || !PACKET_get_length_prefixed_2(pkt, &generator)
1426 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1427 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1431 peer_tmp = EVP_PKEY_new();
1434 if (peer_tmp == NULL || dh == NULL) {
1435 al = SSL_AD_INTERNAL_ERROR;
1436 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1440 p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL);
1441 g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator),
1443 bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key),
1445 if (p == NULL || g == NULL || bnpub_key == NULL) {
1446 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1450 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1451 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1455 if (!DH_set0_pqg(dh, p, NULL, g)) {
1456 al = SSL_AD_INTERNAL_ERROR;
1457 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1461 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1462 al = SSL_AD_INTERNAL_ERROR;
1463 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1467 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1468 al = SSL_AD_HANDSHAKE_FAILURE;
1469 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1473 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1474 al = SSL_AD_INTERNAL_ERROR;
1475 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1479 s->s3->peer_tmp = peer_tmp;
1487 EVP_PKEY_free(peer_tmp);
1491 * FIXME: This makes assumptions about which ciphersuites come with
1492 * public keys. We should have a less ad-hoc way of doing this
1494 if (alg_a & (SSL_aRSA|SSL_aDSS))
1495 pkey = X509_get0_pubkey(s->session->peer);
1496 /* else anonymous DH, so no certificate or pkey. */
1498 #endif /* !OPENSSL_NO_DH */
1500 #ifndef OPENSSL_NO_EC
1501 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1503 const unsigned char *ecparams;
1507 * Extract elliptic curve parameters and the server's ephemeral ECDH
1508 * public key. For now we only support named (not generic) curves and
1509 * ECParameters in this case is just three bytes.
1511 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1512 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1516 * Check curve is one of our preferences, if not server has sent an
1517 * invalid curve. ECParameters is 3 bytes.
1519 if (!tls1_check_curve(s, ecparams, 3)) {
1520 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1524 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2));
1525 if (curve_nid == 0) {
1526 al = SSL_AD_INTERNAL_ERROR;
1527 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1528 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1532 /* Set up EVP_PKEY with named curve as parameters */
1533 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1535 || EVP_PKEY_paramgen_init(pctx) <= 0
1536 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1537 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1538 al = SSL_AD_INTERNAL_ERROR;
1539 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1542 EVP_PKEY_CTX_free(pctx);
1545 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1546 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1550 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp),
1551 PACKET_data(&encoded_pt),
1552 PACKET_remaining(&encoded_pt), NULL) == 0) {
1553 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1558 * The ECC/TLS specification does not mention the use of DSA to sign
1559 * ECParameters in the server key exchange message. We do support RSA
1563 # ifndef OPENSSL_NO_RSA
1564 else if (alg_a & SSL_aRSA)
1565 pkey = X509_get0_pubkey(s->session->peer);
1567 # ifndef OPENSSL_NO_EC
1568 else if (alg_a & SSL_aECDSA)
1569 pkey = X509_get0_pubkey(s->session->peer);
1571 /* else anonymous ECDH, so no certificate or pkey. */
1573 al = SSL_AD_UNEXPECTED_MESSAGE;
1574 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1577 #endif /* !OPENSSL_NO_EC */
1579 /* if it was signed, check the signature */
1583 * |pkt| now points to the beginning of the signature, so the difference
1584 * equals the length of the parameters.
1586 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1587 PACKET_remaining(&save_param_start) -
1588 PACKET_remaining(pkt))) {
1589 al = SSL_AD_INTERNAL_ERROR;
1590 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1594 if (SSL_USE_SIGALGS(s)) {
1595 const unsigned char *sigalgs;
1597 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1598 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1601 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1608 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1610 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1611 md = EVP_md5_sha1();
1616 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1617 || PACKET_remaining(pkt) != 0) {
1618 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1621 j = EVP_PKEY_size(pkey);
1623 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1628 * Check signature length
1630 if (PACKET_remaining(&signature) > (size_t)j) {
1631 /* wrong packet length */
1632 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1635 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1636 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1637 SSL3_RANDOM_SIZE) <= 0
1638 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1639 SSL3_RANDOM_SIZE) <= 0
1640 || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms),
1641 PACKET_remaining(¶ms)) <= 0) {
1642 al = SSL_AD_INTERNAL_ERROR;
1643 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1646 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1647 PACKET_remaining(&signature), pkey) <= 0) {
1649 al = SSL_AD_DECRYPT_ERROR;
1650 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1654 /* aNULL, aSRP or PSK do not need public keys */
1655 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1656 /* Might be wrong key type, check it */
1657 if (ssl3_check_cert_and_algorithm(s))
1658 /* Otherwise this shouldn't happen */
1659 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1662 /* still data left over */
1663 if (PACKET_remaining(pkt) != 0) {
1664 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1668 EVP_MD_CTX_free(md_ctx);
1669 return MSG_PROCESS_CONTINUE_READING;
1671 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1673 #ifndef OPENSSL_NO_RSA
1676 #ifndef OPENSSL_NO_EC
1677 EVP_PKEY_CTX_free(pctx);
1679 EVP_MD_CTX_free(md_ctx);
1680 ossl_statem_set_error(s);
1681 return MSG_PROCESS_ERROR;
1684 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1686 int ret = MSG_PROCESS_ERROR;
1687 unsigned int list_len, ctype_num, i, name_len;
1688 X509_NAME *xn = NULL;
1689 const unsigned char *data;
1690 const unsigned char *namestart, *namebytes;
1691 STACK_OF(X509_NAME) *ca_sk = NULL;
1693 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1694 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1698 /* get the certificate types */
1699 if (!PACKET_get_1(pkt, &ctype_num)
1700 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1701 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1702 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1705 OPENSSL_free(s->cert->ctypes);
1706 s->cert->ctypes = NULL;
1707 if (ctype_num > SSL3_CT_NUMBER) {
1708 /* If we exceed static buffer copy all to cert structure */
1709 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1710 if (s->cert->ctypes == NULL) {
1711 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1714 memcpy(s->cert->ctypes, data, ctype_num);
1715 s->cert->ctype_num = (size_t)ctype_num;
1716 ctype_num = SSL3_CT_NUMBER;
1718 for (i = 0; i < ctype_num; i++)
1719 s->s3->tmp.ctype[i] = data[i];
1721 if (SSL_USE_SIGALGS(s)) {
1722 if (!PACKET_get_net_2(pkt, &list_len)
1723 || !PACKET_get_bytes(pkt, &data, list_len)) {
1724 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1725 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1726 SSL_R_LENGTH_MISMATCH);
1730 /* Clear certificate digests and validity flags */
1731 for (i = 0; i < SSL_PKEY_NUM; i++) {
1732 s->s3->tmp.md[i] = NULL;
1733 s->s3->tmp.valid_flags[i] = 0;
1735 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1736 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1737 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1738 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1741 if (!tls1_process_sigalgs(s)) {
1742 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1743 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1747 ssl_set_default_md(s);
1750 /* get the CA RDNs */
1751 if (!PACKET_get_net_2(pkt, &list_len)
1752 || PACKET_remaining(pkt) != list_len) {
1753 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1754 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1758 while (PACKET_remaining(pkt)) {
1759 if (!PACKET_get_net_2(pkt, &name_len)
1760 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1761 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1762 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1763 SSL_R_LENGTH_MISMATCH);
1767 namestart = namebytes;
1769 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1770 name_len)) == NULL) {
1771 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1772 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1776 if (namebytes != (namestart + name_len)) {
1777 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1778 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1779 SSL_R_CA_DN_LENGTH_MISMATCH);
1782 if (!sk_X509_NAME_push(ca_sk, xn)) {
1783 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1789 /* we should setup a certificate to return.... */
1790 s->s3->tmp.cert_req = 1;
1791 s->s3->tmp.ctype_num = ctype_num;
1792 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1793 s->s3->tmp.ca_names = ca_sk;
1796 ret = MSG_PROCESS_CONTINUE_READING;
1799 ossl_statem_set_error(s);
1802 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1806 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1808 return (X509_NAME_cmp(*a, *b));
1811 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1814 unsigned int ticklen;
1815 unsigned long ticket_lifetime_hint;
1817 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1818 || !PACKET_get_net_2(pkt, &ticklen)
1819 || PACKET_remaining(pkt) != ticklen) {
1820 al = SSL_AD_DECODE_ERROR;
1821 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1825 /* Server is allowed to change its mind and send an empty ticket. */
1827 return MSG_PROCESS_CONTINUE_READING;
1829 if (s->session->session_id_length > 0) {
1830 int i = s->session_ctx->session_cache_mode;
1831 SSL_SESSION *new_sess;
1833 * We reused an existing session, so we need to replace it with a new
1836 if (i & SSL_SESS_CACHE_CLIENT) {
1838 * Remove the old session from the cache
1840 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
1841 if (s->session_ctx->remove_session_cb != NULL)
1842 s->session_ctx->remove_session_cb(s->session_ctx,
1845 /* We carry on if this fails */
1846 SSL_CTX_remove_session(s->session_ctx, s->session);
1850 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1851 al = SSL_AD_INTERNAL_ERROR;
1852 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1856 SSL_SESSION_free(s->session);
1857 s->session = new_sess;
1860 OPENSSL_free(s->session->tlsext_tick);
1861 s->session->tlsext_ticklen = 0;
1863 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1864 if (s->session->tlsext_tick == NULL) {
1865 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1868 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1869 al = SSL_AD_DECODE_ERROR;
1870 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1874 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1875 s->session->tlsext_ticklen = ticklen;
1877 * There are two ways to detect a resumed ticket session. One is to set
1878 * an appropriate session ID and then the server must return a match in
1879 * ServerHello. This allows the normal client session ID matching to work
1880 * and we know much earlier that the ticket has been accepted. The
1881 * other way is to set zero length session ID when the ticket is
1882 * presented and rely on the handshake to determine session resumption.
1883 * We choose the former approach because this fits in with assumptions
1884 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1885 * SHA256 is disabled) hash of the ticket.
1887 EVP_Digest(s->session->tlsext_tick, ticklen,
1888 s->session->session_id, &s->session->session_id_length,
1889 EVP_sha256(), NULL);
1890 return MSG_PROCESS_CONTINUE_READING;
1892 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1894 ossl_statem_set_error(s);
1895 return MSG_PROCESS_ERROR;
1898 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1901 unsigned long resplen;
1904 if (!PACKET_get_1(pkt, &type)
1905 || type != TLSEXT_STATUSTYPE_ocsp) {
1906 al = SSL_AD_DECODE_ERROR;
1907 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1910 if (!PACKET_get_net_3(pkt, &resplen)
1911 || PACKET_remaining(pkt) != resplen) {
1912 al = SSL_AD_DECODE_ERROR;
1913 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1916 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
1917 if (s->tlsext_ocsp_resp == NULL) {
1918 al = SSL_AD_INTERNAL_ERROR;
1919 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1922 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
1923 al = SSL_AD_DECODE_ERROR;
1924 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1927 s->tlsext_ocsp_resplen = resplen;
1928 return MSG_PROCESS_CONTINUE_READING;
1930 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1931 ossl_statem_set_error(s);
1932 return MSG_PROCESS_ERROR;
1935 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
1937 if (PACKET_remaining(pkt) > 0) {
1938 /* should contain no data */
1939 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1940 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1941 ossl_statem_set_error(s);
1942 return MSG_PROCESS_ERROR;
1945 #ifndef OPENSSL_NO_SRP
1946 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
1947 if (SRP_Calc_A_param(s) <= 0) {
1948 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
1949 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1950 ossl_statem_set_error(s);
1951 return MSG_PROCESS_ERROR;
1957 * at this point we check that we have the required stuff from
1960 if (!ssl3_check_cert_and_algorithm(s)) {
1961 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1962 ossl_statem_set_error(s);
1963 return MSG_PROCESS_ERROR;
1967 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
1968 * |tlsext_ocsp_resplen| values will be set if we actually received a status
1969 * message, or NULL and -1 otherwise
1971 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
1973 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1975 ssl3_send_alert(s, SSL3_AL_FATAL,
1976 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
1977 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
1978 SSL_R_INVALID_STATUS_RESPONSE);
1979 return MSG_PROCESS_ERROR;
1982 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1983 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
1984 return MSG_PROCESS_ERROR;
1988 #ifndef OPENSSL_NO_CT
1989 if (s->ct_validation_callback != NULL) {
1990 /* Note we validate the SCTs whether or not we abort on error */
1991 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
1992 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1993 return MSG_PROCESS_ERROR;
1998 #ifndef OPENSSL_NO_SCTP
1999 /* Only applies to renegotiation */
2000 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2001 && s->renegotiate != 0)
2002 return MSG_PROCESS_CONTINUE_PROCESSING;
2005 return MSG_PROCESS_FINISHED_READING;
2008 int tls_construct_client_key_exchange(SSL *s)
2012 #ifndef OPENSSL_NO_PSK
2013 size_t pskhdrlen = 0;
2015 unsigned long alg_k;
2016 #ifndef OPENSSL_NO_RSA
2018 EVP_PKEY *pkey = NULL;
2019 EVP_PKEY_CTX *pctx = NULL;
2021 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2022 EVP_PKEY *ckey = NULL, *skey = NULL;
2024 #ifndef OPENSSL_NO_EC
2025 unsigned char *encodedPoint = NULL;
2026 int encoded_pt_len = 0;
2028 unsigned char *pms = NULL;
2030 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2032 p = ssl_handshake_start(s);
2035 #ifndef OPENSSL_NO_PSK
2036 if (alg_k & SSL_PSK) {
2039 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2040 * \0-terminated identity. The last byte is for us for simulating
2043 char identity[PSK_MAX_IDENTITY_LEN + 1];
2045 unsigned char psk[PSK_MAX_PSK_LEN];
2048 if (s->psk_client_callback == NULL) {
2049 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2050 SSL_R_PSK_NO_CLIENT_CB);
2054 memset(identity, 0, sizeof(identity));
2056 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2057 identity, sizeof(identity) - 1,
2060 if (psklen > PSK_MAX_PSK_LEN) {
2061 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2062 ERR_R_INTERNAL_ERROR);
2064 } else if (psklen == 0) {
2065 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2066 SSL_R_PSK_IDENTITY_NOT_FOUND);
2069 OPENSSL_free(s->s3->tmp.psk);
2070 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2071 OPENSSL_cleanse(psk, psklen);
2073 if (s->s3->tmp.psk == NULL) {
2074 OPENSSL_cleanse(identity, sizeof(identity));
2078 s->s3->tmp.psklen = psklen;
2079 identitylen = strlen(identity);
2080 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2081 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2082 ERR_R_INTERNAL_ERROR);
2085 OPENSSL_free(s->session->psk_identity);
2086 s->session->psk_identity = OPENSSL_strdup(identity);
2087 if (s->session->psk_identity == NULL) {
2088 OPENSSL_cleanse(identity, sizeof(identity));
2092 s2n(identitylen, p);
2093 memcpy(p, identity, identitylen);
2094 pskhdrlen = 2 + identitylen;
2098 OPENSSL_cleanse(identity, sizeof(identity));
2100 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2104 if (alg_k & SSL_kPSK) {
2109 /* Fool emacs indentation */
2112 #ifndef OPENSSL_NO_RSA
2113 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2115 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2116 pms = OPENSSL_malloc(pmslen);
2120 if (s->session->peer == NULL) {
2122 * We should always have a server certificate with SSL_kRSA.
2124 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2125 ERR_R_INTERNAL_ERROR);
2129 pkey = X509_get0_pubkey(s->session->peer);
2130 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2131 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2132 ERR_R_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)
2142 /* Fix buf for TLS and beyond */
2143 if (s->version > SSL3_VERSION)
2145 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2146 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2147 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2148 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2152 if (EVP_PKEY_encrypt(pctx, p, &enclen, pms, pmslen) <= 0) {
2153 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2154 SSL_R_BAD_RSA_ENCRYPT);
2158 EVP_PKEY_CTX_free(pctx);
2161 if (s->options & SSL_OP_PKCS1_CHECK_1)
2163 if (s->options & SSL_OP_PKCS1_CHECK_2)
2167 /* Fix buf for TLS and beyond */
2168 if (s->version > SSL3_VERSION) {
2174 #ifndef OPENSSL_NO_DH
2175 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2178 skey = s->s3->peer_tmp;
2180 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2181 ERR_R_INTERNAL_ERROR);
2184 ckey = ssl_generate_pkey(skey, NID_undef);
2185 dh_clnt = EVP_PKEY_get0_DH(ckey);
2187 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
2188 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2189 ERR_R_INTERNAL_ERROR);
2194 /* send off the data */
2195 DH_get0_key(dh_clnt, &pub_key, NULL);
2196 n = BN_num_bytes(pub_key);
2198 BN_bn2bin(pub_key, p);
2200 EVP_PKEY_free(ckey);
2205 #ifndef OPENSSL_NO_EC
2206 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2208 skey = s->s3->peer_tmp;
2209 if ((skey == NULL) || EVP_PKEY_get0_EC_KEY(skey) == NULL) {
2210 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2211 ERR_R_INTERNAL_ERROR);
2215 ckey = ssl_generate_pkey(skey, NID_undef);
2217 if (ssl_derive(s, ckey, skey) == 0) {
2218 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);
2222 /* Generate encoding of client key */
2223 encoded_pt_len = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey),
2224 POINT_CONVERSION_UNCOMPRESSED,
2225 &encodedPoint, NULL);
2227 if (encoded_pt_len == 0) {
2228 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2232 EVP_PKEY_free(ckey);
2237 *p = n; /* length of encoded point */
2238 /* Encoded point will be copied here */
2240 /* copy the point */
2241 memcpy(p, encodedPoint, n);
2242 /* increment n to account for length field */
2245 /* Free allocated memory */
2246 OPENSSL_free(encodedPoint);
2248 #endif /* !OPENSSL_NO_EC */
2249 #ifndef OPENSSL_NO_GOST
2250 else if (alg_k & SSL_kGOST) {
2251 /* GOST key exchange message creation */
2252 EVP_PKEY_CTX *pkey_ctx;
2255 unsigned int md_len;
2256 unsigned char shared_ukm[32], tmp[256];
2257 EVP_MD_CTX *ukm_hash;
2258 int dgst_nid = NID_id_GostR3411_94;
2259 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2260 dgst_nid = NID_id_GostR3411_2012_256;
2264 pms = OPENSSL_malloc(pmslen);
2269 * Get server sertificate PKEY and create ctx from it
2271 peer_cert = s->session->peer;
2273 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2274 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2278 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2279 if (pkey_ctx == NULL) {
2280 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2281 ERR_R_MALLOC_FAILURE);
2285 * If we have send a certificate, and certificate key
2286 * parameters match those of server certificate, use
2287 * certificate key for key exchange
2290 /* Otherwise, generate ephemeral key pair */
2292 if (pkey_ctx == NULL
2293 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2294 /* Generate session key */
2295 || RAND_bytes(pms, pmslen) <= 0) {
2296 EVP_PKEY_CTX_free(pkey_ctx);
2297 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2298 ERR_R_INTERNAL_ERROR);
2302 * If we have client certificate, use its secret as peer key
2304 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2305 if (EVP_PKEY_derive_set_peer
2306 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2308 * If there was an error - just ignore it. Ephemeral key
2315 * Compute shared IV and store it in algorithm-specific context
2318 ukm_hash = EVP_MD_CTX_new();
2319 if (EVP_DigestInit(ukm_hash,
2320 EVP_get_digestbynid(dgst_nid)) <= 0
2321 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2322 SSL3_RANDOM_SIZE) <= 0
2323 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2324 SSL3_RANDOM_SIZE) <= 0
2325 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2326 EVP_MD_CTX_free(ukm_hash);
2327 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2328 ERR_R_INTERNAL_ERROR);
2331 EVP_MD_CTX_free(ukm_hash);
2332 if (EVP_PKEY_CTX_ctrl
2333 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2335 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2339 /* Make GOST keytransport blob message */
2341 * Encapsulate it into sequence
2343 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2345 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2346 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2350 if (msglen >= 0x80) {
2352 *(p++) = msglen & 0xff;
2355 *(p++) = msglen & 0xff;
2358 memcpy(p, tmp, msglen);
2359 /* Check if pubkey from client certificate was used */
2360 if (EVP_PKEY_CTX_ctrl
2361 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2362 /* Set flag "skip certificate verify" */
2363 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2365 EVP_PKEY_CTX_free(pkey_ctx);
2369 #ifndef OPENSSL_NO_SRP
2370 else if (alg_k & SSL_kSRP) {
2371 if (s->srp_ctx.A != NULL) {
2372 /* send off the data */
2373 n = BN_num_bytes(s->srp_ctx.A);
2375 BN_bn2bin(s->srp_ctx.A, p);
2378 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2379 ERR_R_INTERNAL_ERROR);
2382 OPENSSL_free(s->session->srp_username);
2383 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2384 if (s->session->srp_username == NULL) {
2385 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2386 ERR_R_MALLOC_FAILURE);
2392 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2393 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2397 #ifndef OPENSSL_NO_PSK
2401 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2402 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2403 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2408 s->s3->tmp.pms = pms;
2409 s->s3->tmp.pmslen = pmslen;
2414 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2415 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2417 OPENSSL_clear_free(pms, pmslen);
2418 s->s3->tmp.pms = NULL;
2419 #ifndef OPENSSL_NO_RSA
2420 EVP_PKEY_CTX_free(pctx);
2422 #ifndef OPENSSL_NO_EC
2423 OPENSSL_free(encodedPoint);
2425 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2426 EVP_PKEY_free(ckey);
2428 #ifndef OPENSSL_NO_PSK
2429 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2430 s->s3->tmp.psk = NULL;
2432 ossl_statem_set_error(s);
2436 int tls_client_key_exchange_post_work(SSL *s)
2438 unsigned char *pms = NULL;
2441 pms = s->s3->tmp.pms;
2442 pmslen = s->s3->tmp.pmslen;
2444 #ifndef OPENSSL_NO_SRP
2446 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2447 if (!srp_generate_client_master_secret(s)) {
2448 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2449 ERR_R_INTERNAL_ERROR);
2456 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2457 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2458 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2461 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2462 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2463 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2464 /* ssl_generate_master_secret frees the pms even on error */
2472 #ifndef OPENSSL_NO_SCTP
2473 if (SSL_IS_DTLS(s)) {
2474 unsigned char sctpauthkey[64];
2475 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2478 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2481 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2482 sizeof(DTLS1_SCTP_AUTH_LABEL));
2484 if (SSL_export_keying_material(s, sctpauthkey,
2485 sizeof(sctpauthkey), labelbuffer,
2486 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2489 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2490 sizeof(sctpauthkey), sctpauthkey);
2496 OPENSSL_clear_free(pms, pmslen);
2497 s->s3->tmp.pms = NULL;
2501 int tls_construct_client_verify(SSL *s)
2505 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2508 unsigned long n = 0;
2512 mctx = EVP_MD_CTX_new();
2514 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2518 p = ssl_handshake_start(s);
2519 pkey = s->cert->key->privatekey;
2521 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2522 if (hdatalen <= 0) {
2523 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2526 if (SSL_USE_SIGALGS(s)) {
2527 if (!tls12_get_sigandhash(p, pkey, md)) {
2528 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2535 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2537 if (!EVP_SignInit_ex(mctx, md, NULL)
2538 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2539 || (s->version == SSL3_VERSION
2540 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2541 s->session->master_key_length,
2542 s->session->master_key))
2543 || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
2544 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2547 #ifndef OPENSSL_NO_GOST
2549 int pktype = EVP_PKEY_id(pkey);
2550 if (pktype == NID_id_GostR3410_2001
2551 || pktype == NID_id_GostR3410_2012_256
2552 || pktype == NID_id_GostR3410_2012_512)
2553 BUF_reverse(p + 2, NULL, u);
2559 /* Digest cached records and discard handshake buffer */
2560 if (!ssl3_digest_cached_records(s, 0))
2562 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2563 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2567 EVP_MD_CTX_free(mctx);
2570 EVP_MD_CTX_free(mctx);
2575 * Check a certificate can be used for client authentication. Currently check
2576 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2577 * certificates can be used and optionally checks suitability for Suite B.
2579 static int ssl3_check_client_certificate(SSL *s)
2581 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2583 /* If no suitable signature algorithm can't use certificate */
2584 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2587 * If strict mode check suitability of chain before using it. This also
2588 * adjusts suite B digest if necessary.
2590 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2591 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2596 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2599 EVP_PKEY *pkey = NULL;
2602 if (wst == WORK_MORE_A) {
2603 /* Let cert callback update client certificates if required */
2604 if (s->cert->cert_cb) {
2605 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2607 s->rwstate = SSL_X509_LOOKUP;
2611 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2612 ossl_statem_set_error(s);
2615 s->rwstate = SSL_NOTHING;
2617 if (ssl3_check_client_certificate(s))
2618 return WORK_FINISHED_CONTINUE;
2620 /* Fall through to WORK_MORE_B */
2624 /* We need to get a client cert */
2625 if (wst == WORK_MORE_B) {
2627 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2628 * return(-1); We then get retied later
2630 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2632 s->rwstate = SSL_X509_LOOKUP;
2635 s->rwstate = SSL_NOTHING;
2636 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2637 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2639 } else if (i == 1) {
2641 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2642 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2646 EVP_PKEY_free(pkey);
2647 if (i && !ssl3_check_client_certificate(s))
2650 if (s->version == SSL3_VERSION) {
2651 s->s3->tmp.cert_req = 0;
2652 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2653 return WORK_FINISHED_CONTINUE;
2655 s->s3->tmp.cert_req = 2;
2656 if (!ssl3_digest_cached_records(s, 0)) {
2657 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2658 ossl_statem_set_error(s);
2664 return WORK_FINISHED_CONTINUE;
2667 /* Shouldn't ever get here */
2671 int tls_construct_client_certificate(SSL *s)
2673 if (!ssl3_output_cert_chain(s,
2674 (s->s3->tmp.cert_req ==
2675 2) ? NULL : s->cert->key)) {
2676 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2677 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2678 ossl_statem_set_error(s);
2685 #define has_bits(i,m) (((i)&(m)) == (m))
2687 int ssl3_check_cert_and_algorithm(SSL *s)
2690 #ifndef OPENSSL_NO_EC
2694 EVP_PKEY *pkey = NULL;
2695 int al = SSL_AD_HANDSHAKE_FAILURE;
2697 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2698 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2700 /* we don't have a certificate */
2701 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2704 /* This is the passed certificate */
2706 #ifndef OPENSSL_NO_EC
2707 idx = s->session->peer_type;
2708 if (idx == SSL_PKEY_ECC) {
2709 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2711 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2716 } else if (alg_a & SSL_aECDSA) {
2717 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2718 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2722 pkey = X509_get0_pubkey(s->session->peer);
2723 i = X509_certificate_type(s->session->peer, pkey);
2725 /* Check that we have a certificate if we require one */
2726 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2727 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2728 SSL_R_MISSING_RSA_SIGNING_CERT);
2731 #ifndef OPENSSL_NO_DSA
2732 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2733 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2734 SSL_R_MISSING_DSA_SIGNING_CERT);
2738 #ifndef OPENSSL_NO_RSA
2739 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2740 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2741 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2742 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2746 #ifndef OPENSSL_NO_DH
2747 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
2748 al = SSL_AD_INTERNAL_ERROR;
2749 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2756 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2760 #ifndef OPENSSL_NO_NEXTPROTONEG
2761 int tls_construct_next_proto(SSL *s)
2763 unsigned int len, padding_len;
2766 len = s->next_proto_negotiated_len;
2767 padding_len = 32 - ((len + 2) % 32);
2768 d = (unsigned char *)s->init_buf->data;
2770 memcpy(d + 5, s->next_proto_negotiated, len);
2771 d[5 + len] = padding_len;
2772 memset(d + 6 + len, 0, padding_len);
2773 *(d++) = SSL3_MT_NEXT_PROTO;
2774 l2n3(2 + len + padding_len, d);
2775 s->init_num = 4 + 2 + len + padding_len;
2782 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2785 #ifndef OPENSSL_NO_ENGINE
2786 if (s->ctx->client_cert_engine) {
2787 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2788 SSL_get_client_CA_list(s),
2789 px509, ppkey, NULL, NULL, NULL);
2794 if (s->ctx->client_cert_cb)
2795 i = s->ctx->client_cert_cb(s, px509, ppkey);
2799 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
2803 const SSL_CIPHER *c;
2805 int empty_reneg_info_scsv = !s->renegotiate;
2806 /* Set disabled masks for this session */
2807 ssl_set_client_disabled(s);
2813 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2814 c = sk_SSL_CIPHER_value(sk, i);
2815 /* Skip disabled ciphers */
2816 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2818 j = s->method->put_cipher_by_char(c, p);
2822 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2826 if (empty_reneg_info_scsv) {
2827 static SSL_CIPHER scsv = {
2828 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2830 j = s->method->put_cipher_by_char(&scsv, p);
2833 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2834 static SSL_CIPHER scsv = {
2835 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2837 j = s->method->put_cipher_by_char(&scsv, p);