2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
152 #include "ssl_locl.h"
153 #include <openssl/buffer.h>
154 #include <openssl/rand.h>
155 #include <openssl/objects.h>
156 #include <openssl/evp.h>
157 #include <openssl/md5.h>
158 #ifndef OPENSSL_NO_DH
159 # include <openssl/dh.h>
161 #include <openssl/bn.h>
162 #ifndef OPENSSL_NO_ENGINE
163 # include <openssl/engine.h>
166 static int ssl_set_version(SSL *s);
167 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
168 static int ssl3_check_finished(SSL *s);
169 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
171 int (*put_cb) (const SSL_CIPHER *,
175 int ssl3_connect(SSL *s)
178 unsigned long Time = (unsigned long)time(NULL);
179 void (*cb) (const SSL *ssl, int type, int val) = NULL;
181 int new_state, state, skip = 0;
183 RAND_add(&Time, sizeof(Time), 0);
187 if (s->info_callback != NULL)
188 cb = s->info_callback;
189 else if (s->ctx->info_callback != NULL)
190 cb = s->ctx->info_callback;
193 if (!SSL_in_init(s) || SSL_in_before(s)) {
198 #ifndef OPENSSL_NO_HEARTBEATS
200 * If we're awaiting a HeartbeatResponse, pretend we already got and
201 * don't await it anymore, because Heartbeats don't make sense during
204 if (s->tlsext_hb_pending) {
205 s->tlsext_hb_pending = 0;
214 case SSL_ST_RENEGOTIATE:
216 s->state = SSL_ST_CONNECT;
217 s->ctx->stats.sess_connect_renegotiate++;
221 case SSL_ST_BEFORE | SSL_ST_CONNECT:
222 case SSL_ST_OK | SSL_ST_CONNECT:
226 cb(s, SSL_CB_HANDSHAKE_START, 1);
228 if ((s->version >> 8) != SSL3_VERSION_MAJOR
229 && s->version != TLS_ANY_VERSION) {
230 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
231 s->state = SSL_ST_ERR;
236 if (s->version != TLS_ANY_VERSION &&
237 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
238 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
242 /* s->version=SSL3_VERSION; */
243 s->type = SSL_ST_CONNECT;
245 if (s->init_buf == NULL) {
246 if ((buf = BUF_MEM_new()) == NULL) {
248 s->state = SSL_ST_ERR;
251 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
253 s->state = SSL_ST_ERR;
260 if (!ssl3_setup_buffers(s)) {
265 /* setup buffing BIO */
266 if (!ssl_init_wbio_buffer(s, 0)) {
268 s->state = SSL_ST_ERR;
272 /* don't push the buffering BIO quite yet */
274 ssl3_init_finished_mac(s);
276 s->state = SSL3_ST_CW_CLNT_HELLO_A;
277 s->ctx->stats.sess_connect++;
279 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
281 * Should have been reset by ssl3_get_finished, too.
283 s->s3->change_cipher_spec = 0;
286 case SSL3_ST_CW_CLNT_HELLO_A:
287 case SSL3_ST_CW_CLNT_HELLO_B:
290 ret = ssl3_client_hello(s);
293 s->state = SSL3_ST_CR_SRVR_HELLO_A;
296 /* turn on buffering for the next lot of output */
297 if (s->bbio != s->wbio)
298 s->wbio = BIO_push(s->bbio, s->wbio);
302 case SSL3_ST_CR_SRVR_HELLO_A:
303 case SSL3_ST_CR_SRVR_HELLO_B:
304 ret = ssl3_get_server_hello(s);
309 s->state = SSL3_ST_CR_FINISHED_A;
310 if (s->tlsext_ticket_expected) {
311 /* receive renewed session ticket */
312 s->state = SSL3_ST_CR_SESSION_TICKET_A;
315 s->state = SSL3_ST_CR_CERT_A;
319 case SSL3_ST_CR_CERT_A:
320 case SSL3_ST_CR_CERT_B:
321 /* Noop (ret = 0) for everything but EAP-FAST. */
322 ret = ssl3_check_finished(s);
327 s->state = SSL3_ST_CR_FINISHED_A;
332 /* Check if it is anon DH/ECDH, SRP auth */
336 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
337 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
338 ret = ssl3_get_server_certificate(s);
342 if (s->tlsext_status_expected)
343 s->state = SSL3_ST_CR_CERT_STATUS_A;
345 s->state = SSL3_ST_CR_KEY_EXCH_A;
348 s->state = SSL3_ST_CR_KEY_EXCH_A;
354 case SSL3_ST_CR_KEY_EXCH_A:
355 case SSL3_ST_CR_KEY_EXCH_B:
356 ret = ssl3_get_key_exchange(s);
359 s->state = SSL3_ST_CR_CERT_REQ_A;
363 * at this point we check that we have the required stuff from
366 if (!ssl3_check_cert_and_algorithm(s)) {
368 s->state = SSL_ST_ERR;
373 case SSL3_ST_CR_CERT_REQ_A:
374 case SSL3_ST_CR_CERT_REQ_B:
375 ret = ssl3_get_certificate_request(s);
378 s->state = SSL3_ST_CR_SRVR_DONE_A;
382 case SSL3_ST_CR_SRVR_DONE_A:
383 case SSL3_ST_CR_SRVR_DONE_B:
384 ret = ssl3_get_server_done(s);
387 #ifndef OPENSSL_NO_SRP
388 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
389 if ((ret = SRP_Calc_A_param(s)) <= 0) {
390 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
391 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
392 s->state = SSL_ST_ERR;
397 if (s->s3->tmp.cert_req)
398 s->state = SSL3_ST_CW_CERT_A;
400 s->state = SSL3_ST_CW_KEY_EXCH_A;
405 case SSL3_ST_CW_CERT_A:
406 case SSL3_ST_CW_CERT_B:
407 case SSL3_ST_CW_CERT_C:
408 case SSL3_ST_CW_CERT_D:
409 ret = ssl3_send_client_certificate(s);
412 s->state = SSL3_ST_CW_KEY_EXCH_A;
416 case SSL3_ST_CW_KEY_EXCH_A:
417 case SSL3_ST_CW_KEY_EXCH_B:
418 ret = ssl3_send_client_key_exchange(s);
422 * EAY EAY EAY need to check for DH fix cert sent back
425 * For TLS, cert_req is set to 2, so a cert chain of nothing is
426 * sent, but no verify packet is sent
429 * XXX: For now, we do not support client authentication in ECDH
430 * cipher suites with ECDH (rather than ECDSA) certificates. We
431 * need to skip the certificate verify message when client's
432 * ECDH public key is sent inside the client certificate.
434 if (s->s3->tmp.cert_req == 1) {
435 s->state = SSL3_ST_CW_CERT_VRFY_A;
437 s->state = SSL3_ST_CW_CHANGE_A;
439 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
440 s->state = SSL3_ST_CW_CHANGE_A;
446 case SSL3_ST_CW_CERT_VRFY_A:
447 case SSL3_ST_CW_CERT_VRFY_B:
448 ret = ssl3_send_client_verify(s);
451 s->state = SSL3_ST_CW_CHANGE_A;
455 case SSL3_ST_CW_CHANGE_A:
456 case SSL3_ST_CW_CHANGE_B:
457 ret = ssl3_send_change_cipher_spec(s,
459 SSL3_ST_CW_CHANGE_B);
463 #if defined(OPENSSL_NO_NEXTPROTONEG)
464 s->state = SSL3_ST_CW_FINISHED_A;
466 if (s->s3->next_proto_neg_seen)
467 s->state = SSL3_ST_CW_NEXT_PROTO_A;
469 s->state = SSL3_ST_CW_FINISHED_A;
473 s->session->cipher = s->s3->tmp.new_cipher;
474 #ifdef OPENSSL_NO_COMP
475 s->session->compress_meth = 0;
477 if (s->s3->tmp.new_compression == NULL)
478 s->session->compress_meth = 0;
480 s->session->compress_meth = s->s3->tmp.new_compression->id;
482 if (!s->method->ssl3_enc->setup_key_block(s)) {
484 s->state = SSL_ST_ERR;
488 if (!s->method->ssl3_enc->change_cipher_state(s,
489 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
492 s->state = SSL_ST_ERR;
498 #if !defined(OPENSSL_NO_NEXTPROTONEG)
499 case SSL3_ST_CW_NEXT_PROTO_A:
500 case SSL3_ST_CW_NEXT_PROTO_B:
501 ret = ssl3_send_next_proto(s);
504 s->state = SSL3_ST_CW_FINISHED_A;
508 case SSL3_ST_CW_FINISHED_A:
509 case SSL3_ST_CW_FINISHED_B:
510 ret = ssl3_send_finished(s,
511 SSL3_ST_CW_FINISHED_A,
512 SSL3_ST_CW_FINISHED_B,
514 ssl3_enc->client_finished_label,
516 ssl3_enc->client_finished_label_len);
519 s->state = SSL3_ST_CW_FLUSH;
522 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
524 s->s3->tmp.next_state = SSL_ST_OK;
525 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
526 s->state = SSL_ST_OK;
527 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
528 s->s3->delay_buf_pop_ret = 0;
532 * Allow NewSessionTicket if ticket expected
534 if (s->tlsext_ticket_expected)
535 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
537 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
542 case SSL3_ST_CR_SESSION_TICKET_A:
543 case SSL3_ST_CR_SESSION_TICKET_B:
544 ret = ssl3_get_new_session_ticket(s);
547 s->state = SSL3_ST_CR_FINISHED_A;
551 case SSL3_ST_CR_CERT_STATUS_A:
552 case SSL3_ST_CR_CERT_STATUS_B:
553 ret = ssl3_get_cert_status(s);
556 s->state = SSL3_ST_CR_KEY_EXCH_A;
560 case SSL3_ST_CR_FINISHED_A:
561 case SSL3_ST_CR_FINISHED_B:
562 if (!s->s3->change_cipher_spec)
563 s->s3->flags |= SSL3_FLAGS_CCS_OK;
564 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
565 SSL3_ST_CR_FINISHED_B);
570 s->state = SSL3_ST_CW_CHANGE_A;
572 s->state = SSL_ST_OK;
576 case SSL3_ST_CW_FLUSH:
577 s->rwstate = SSL_WRITING;
578 if (BIO_flush(s->wbio) <= 0) {
582 s->rwstate = SSL_NOTHING;
583 s->state = s->s3->tmp.next_state;
587 /* clean a few things up */
588 ssl3_cleanup_key_block(s);
589 BUF_MEM_free(s->init_buf);
593 * If we are not 'joining' the last two packets, remove the
596 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
597 ssl_free_wbio_buffer(s);
598 /* else do it later in ssl3_write */
604 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
606 s->ctx->stats.sess_hit++;
610 s->handshake_func = ssl3_connect;
611 s->ctx->stats.sess_connect_good++;
614 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
621 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
627 /* did we do anything */
628 if (!s->s3->tmp.reuse_message && !skip) {
630 if ((ret = BIO_flush(s->wbio)) <= 0)
634 if ((cb != NULL) && (s->state != state)) {
635 new_state = s->state;
637 cb(s, SSL_CB_CONNECT_LOOP, 1);
638 s->state = new_state;
647 cb(s, SSL_CB_CONNECT_EXIT, ret);
652 * Work out what version we should be using for the initial ClientHello if
653 * the version is currently set to (D)TLS_ANY_VERSION.
654 * Returns 1 on success
657 static int ssl_set_version(SSL *s)
659 unsigned long mask, options = s->options;
661 if (s->method->version == TLS_ANY_VERSION) {
663 * SSL_OP_NO_X disables all protocols above X *if* there are
664 * some protocols below X enabled. This is required in order
665 * to maintain "version capability" vector contiguous. So
666 * that if application wants to disable TLS1.0 in favour of
667 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
668 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
670 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
671 #if !defined(OPENSSL_NO_SSL3)
675 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
676 if (options & SSL_OP_NO_TLSv1_2) {
677 if ((options & mask) != mask) {
678 s->version = TLS1_1_VERSION;
680 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
684 s->version = TLS1_2_VERSION;
687 if ((options & mask) == mask) {
688 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
691 s->version = TLS1_1_VERSION;
694 mask &= ~SSL_OP_NO_TLSv1_1;
695 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
696 s->version = TLS1_VERSION;
697 mask &= ~SSL_OP_NO_TLSv1;
698 #if !defined(OPENSSL_NO_SSL3)
699 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
700 s->version = SSL3_VERSION;
703 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
704 SSLerr(SSL_F_SSL_SET_VERSION,
705 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
709 if (s->version == SSL3_VERSION && FIPS_mode()) {
710 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
714 } else if (s->method->version == DTLS_ANY_VERSION) {
715 /* Determine which DTLS version to use */
716 /* If DTLS 1.2 disabled correct the version number */
717 if (options & SSL_OP_NO_DTLSv1_2) {
718 if (tls1_suiteb(s)) {
719 SSLerr(SSL_F_SSL_SET_VERSION,
720 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
724 * Disabling all versions is silly: return an error.
726 if (options & SSL_OP_NO_DTLSv1) {
727 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
731 * Update method so we don't use any DTLS 1.2 features.
733 s->method = DTLSv1_client_method();
734 s->version = DTLS1_VERSION;
737 * We only support one version: update method
739 if (options & SSL_OP_NO_DTLSv1)
740 s->method = DTLSv1_2_client_method();
741 s->version = DTLS1_2_VERSION;
745 s->client_version = s->version;
750 int ssl3_client_hello(SSL *s)
753 unsigned char *p, *d;
757 #ifndef OPENSSL_NO_COMP
762 buf = (unsigned char *)s->init_buf->data;
763 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
764 SSL_SESSION *sess = s->session;
766 /* Work out what SSL/TLS/DTLS version to use */
767 if (ssl_set_version(s) == 0)
770 if ((sess == NULL) || (sess->ssl_version != s->version) ||
772 * In the case of EAP-FAST, we can have a pre-shared
773 * "ticket" without a session ID.
775 (!sess->session_id_length && !sess->tlsext_tick) ||
776 (sess->not_resumable)) {
777 if (!ssl_get_new_session(s, 0))
780 /* else use the pre-loaded session */
782 p = s->s3->client_random;
785 * for DTLS if client_random is initialized, reuse it, we are
786 * required to use same upon reply to HelloVerify
788 if (SSL_IS_DTLS(s)) {
791 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
800 if (i && ssl_fill_hello_random(s, 0, p,
801 sizeof(s->s3->client_random)) <= 0)
804 /* Do the message type and length last */
805 d = p = ssl_handshake_start(s);
808 * version indicates the negotiated version: for example from
809 * an SSLv2/v3 compatible client hello). The client_version
810 * field is the maximum version we permit and it is also
811 * used in RSA encrypted premaster secrets. Some servers can
812 * choke if we initially report a higher version then
813 * renegotiate to a lower one in the premaster secret. This
814 * didn't happen with TLS 1.0 as most servers supported it
815 * but it can with TLS 1.1 or later if the server only supports
818 * Possible scenario with previous logic:
819 * 1. Client hello indicates TLS 1.2
820 * 2. Server hello says TLS 1.0
821 * 3. RSA encrypted premaster secret uses 1.2.
822 * 4. Handhaked proceeds using TLS 1.0.
823 * 5. Server sends hello request to renegotiate.
824 * 6. Client hello indicates TLS v1.0 as we now
825 * know that is maximum server supports.
826 * 7. Server chokes on RSA encrypted premaster secret
827 * containing version 1.0.
829 * For interoperability it should be OK to always use the
830 * maximum version we support in client hello and then rely
831 * on the checking of version to ensure the servers isn't
832 * being inconsistent: for example initially negotiating with
833 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
834 * client_version in client hello and not resetting it to
835 * the negotiated version.
837 *(p++) = s->client_version >> 8;
838 *(p++) = s->client_version & 0xff;
841 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
842 p += SSL3_RANDOM_SIZE;
848 i = s->session->session_id_length;
851 if (i > (int)sizeof(s->session->session_id)) {
852 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
855 memcpy(p, s->session->session_id, i);
859 /* cookie stuff for DTLS */
860 if (SSL_IS_DTLS(s)) {
861 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
862 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
865 *(p++) = s->d1->cookie_len;
866 memcpy(p, s->d1->cookie, s->d1->cookie_len);
867 p += s->d1->cookie_len;
870 /* Ciphers supported */
871 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
873 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
876 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
878 * Some servers hang if client hello > 256 bytes as hack workaround
879 * chop number of supported ciphers to keep it well below this if we
882 if (TLS1_get_version(s) >= TLS1_2_VERSION
883 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
884 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
890 #ifdef OPENSSL_NO_COMP
894 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
897 j = sk_SSL_COMP_num(s->ctx->comp_methods);
899 for (i = 0; i < j; i++) {
900 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
904 *(p++) = 0; /* Add the NULL method */
907 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
908 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
912 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
914 ssl3_send_alert(s, SSL3_AL_FATAL, al);
915 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
920 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
921 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
922 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
925 s->state = SSL3_ST_CW_CLNT_HELLO_B;
928 /* SSL3_ST_CW_CLNT_HELLO_B */
929 return ssl_do_write(s);
931 s->state = SSL_ST_ERR;
935 int ssl3_get_server_hello(SSL *s)
937 STACK_OF(SSL_CIPHER) *sk;
939 unsigned char *p, *d;
940 int i, al = SSL_AD_INTERNAL_ERROR, ok;
943 #ifndef OPENSSL_NO_COMP
947 * Hello verify request and/or server hello version may not match so set
948 * first packet if we're negotiating version.
952 n = s->method->ssl_get_message(s,
953 SSL3_ST_CR_SRVR_HELLO_A,
954 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
960 if (SSL_IS_DTLS(s)) {
961 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
962 if (s->d1->send_cookie == 0) {
963 s->s3->tmp.reuse_message = 1;
965 } else { /* already sent a cookie */
967 al = SSL_AD_UNEXPECTED_MESSAGE;
968 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
974 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
975 al = SSL_AD_UNEXPECTED_MESSAGE;
976 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
980 d = p = (unsigned char *)s->init_msg;
982 if (s->method->version == TLS_ANY_VERSION) {
983 int sversion = (p[0] << 8) | p[1];
985 #if TLS_MAX_VERSION != TLS1_2_VERSION
986 #error Code needs updating for new TLS version
988 #ifndef OPENSSL_NO_SSL3
989 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
991 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
992 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
993 al = SSL_AD_PROTOCOL_VERSION;
996 s->method = SSLv3_client_method();
999 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1000 s->method = TLSv1_client_method();
1001 } else if ((sversion == TLS1_1_VERSION) &&
1002 !(s->options & SSL_OP_NO_TLSv1_1)) {
1003 s->method = TLSv1_1_client_method();
1004 } else if ((sversion == TLS1_2_VERSION) &&
1005 !(s->options & SSL_OP_NO_TLSv1_2)) {
1006 s->method = TLSv1_2_client_method();
1008 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1009 al = SSL_AD_PROTOCOL_VERSION;
1012 s->session->ssl_version = s->version = s->method->version;
1014 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1015 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1016 al = SSL_AD_PROTOCOL_VERSION;
1019 } else if (s->method->version == DTLS_ANY_VERSION) {
1020 /* Work out correct protocol version to use */
1021 int hversion = (p[0] << 8) | p[1];
1022 int options = s->options;
1023 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1024 s->method = DTLSv1_2_client_method();
1025 else if (tls1_suiteb(s)) {
1026 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1027 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1028 s->version = hversion;
1029 al = SSL_AD_PROTOCOL_VERSION;
1031 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1032 s->method = DTLSv1_client_method();
1034 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1035 s->version = hversion;
1036 al = SSL_AD_PROTOCOL_VERSION;
1039 s->session->ssl_version = s->version = s->method->version;
1040 } else if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
1041 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1042 s->version = (s->version & 0xff00) | p[1];
1043 al = SSL_AD_PROTOCOL_VERSION;
1048 /* load the server hello data */
1049 /* load the server random */
1050 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
1051 p += SSL3_RANDOM_SIZE;
1055 /* get the session-id */
1058 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
1059 al = SSL_AD_ILLEGAL_PARAMETER;
1060 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1065 * Check if we can resume the session based on external pre-shared secret.
1066 * EAP-FAST (RFC 4851) supports two types of session resumption.
1067 * Resumption based on server-side state works with session IDs.
1068 * Resumption based on pre-shared Protected Access Credentials (PACs)
1069 * works by overriding the SessionTicket extension at the application
1070 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1071 * servers would honour the session ID.) Therefore, the session ID alone
1072 * is not a reliable indicator of session resumption, so we first check if
1073 * we can resume, and later peek at the next handshake message to see if the
1074 * server wants to resume.
1076 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1077 s->session->tlsext_tick) {
1078 SSL_CIPHER *pref_cipher = NULL;
1079 s->session->master_key_length = sizeof(s->session->master_key);
1080 if (s->tls_session_secret_cb(s, s->session->master_key,
1081 &s->session->master_key_length,
1083 s->tls_session_secret_cb_arg)) {
1084 s->session->cipher = pref_cipher ?
1085 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1087 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1088 al = SSL_AD_INTERNAL_ERROR;
1093 if (j != 0 && j == s->session->session_id_length
1094 && memcmp(p, s->session->session_id, j) == 0) {
1095 if (s->sid_ctx_length != s->session->sid_ctx_length
1096 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1097 /* actually a client application bug */
1098 al = SSL_AD_ILLEGAL_PARAMETER;
1099 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1100 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1106 * If we were trying for session-id reuse but the server
1107 * didn't echo the ID, make a new SSL_SESSION.
1108 * In the case of EAP-FAST and PAC, we do not send a session ID,
1109 * so the PAC-based session secret is always preserved. It'll be
1110 * overwritten if the server refuses resumption.
1112 if (s->session->session_id_length > 0) {
1113 if (!ssl_get_new_session(s, 0)) {
1117 s->session->session_id_length = j;
1118 memcpy(s->session->session_id, p, j); /* j could be 0 */
1121 c = ssl_get_cipher_by_char(s, p);
1123 /* unknown cipher */
1124 al = SSL_AD_ILLEGAL_PARAMETER;
1125 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1128 /* Set version disabled mask now we know version */
1129 if (!SSL_USE_TLS1_2_CIPHERS(s))
1130 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1132 s->s3->tmp.mask_ssl = 0;
1134 * If it is a disabled cipher we didn't send it in client hello, so
1137 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1138 al = SSL_AD_ILLEGAL_PARAMETER;
1139 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1142 p += ssl_put_cipher_by_char(s, NULL, NULL);
1144 sk = ssl_get_ciphers_by_id(s);
1145 i = sk_SSL_CIPHER_find(sk, c);
1147 /* we did not say we would use this cipher */
1148 al = SSL_AD_ILLEGAL_PARAMETER;
1149 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1154 * Depending on the session caching (internal/external), the cipher
1155 * and/or cipher_id values may not be set. Make sure that cipher_id is
1156 * set and use it for comparison.
1158 if (s->session->cipher)
1159 s->session->cipher_id = s->session->cipher->id;
1160 if (s->hit && (s->session->cipher_id != c->id)) {
1161 al = SSL_AD_ILLEGAL_PARAMETER;
1162 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1163 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1166 s->s3->tmp.new_cipher = c;
1168 * Don't digest cached records if no sigalgs: we may need them for client
1171 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1173 /* lets get the compression algorithm */
1175 #ifdef OPENSSL_NO_COMP
1177 al = SSL_AD_ILLEGAL_PARAMETER;
1178 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1179 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1183 * If compression is disabled we'd better not try to resume a session
1184 * using compression.
1186 if (s->session->compress_meth != 0) {
1187 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1192 if (s->hit && j != s->session->compress_meth) {
1193 al = SSL_AD_ILLEGAL_PARAMETER;
1194 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1195 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1200 else if (!ssl_allow_compression(s)) {
1201 al = SSL_AD_ILLEGAL_PARAMETER;
1202 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1205 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1207 if ((j != 0) && (comp == NULL)) {
1208 al = SSL_AD_ILLEGAL_PARAMETER;
1209 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1210 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1213 s->s3->tmp.new_compression = comp;
1217 /* TLS extensions */
1218 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1219 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1224 /* wrong packet length */
1225 al = SSL_AD_DECODE_ERROR;
1226 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1232 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1234 s->state = SSL_ST_ERR;
1238 int ssl3_get_server_certificate(SSL *s)
1240 int al, i, ok, ret = -1, exp_idx;
1241 unsigned long n, nc, llen, l;
1243 const unsigned char *q, *p;
1245 STACK_OF(X509) *sk = NULL;
1247 EVP_PKEY *pkey = NULL;
1249 n = s->method->ssl_get_message(s,
1252 -1, s->max_cert_list, &ok);
1257 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1258 s->s3->tmp.reuse_message = 1;
1262 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1263 al = SSL_AD_UNEXPECTED_MESSAGE;
1264 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1267 p = d = (unsigned char *)s->init_msg;
1269 if ((sk = sk_X509_new_null()) == NULL) {
1270 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1275 if (llen + 3 != n) {
1276 al = SSL_AD_DECODE_ERROR;
1277 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1280 for (nc = 0; nc < llen;) {
1282 if ((l + nc + 3) > llen) {
1283 al = SSL_AD_DECODE_ERROR;
1284 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1285 SSL_R_CERT_LENGTH_MISMATCH);
1290 x = d2i_X509(NULL, &q, l);
1292 al = SSL_AD_BAD_CERTIFICATE;
1293 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1297 al = SSL_AD_DECODE_ERROR;
1298 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1299 SSL_R_CERT_LENGTH_MISMATCH);
1302 if (!sk_X509_push(sk, x)) {
1303 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1311 i = ssl_verify_cert_chain(s, sk);
1312 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1313 al = ssl_verify_alarm_type(s->verify_result);
1314 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1315 SSL_R_CERTIFICATE_VERIFY_FAILED);
1318 ERR_clear_error(); /* but we keep s->verify_result */
1320 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1321 al = SSL_AD_HANDSHAKE_FAILURE;
1325 sc = ssl_sess_cert_new();
1329 ssl_sess_cert_free(s->session->sess_cert);
1330 s->session->sess_cert = sc;
1332 sc->cert_chain = sk;
1334 * Inconsistency alert: cert_chain does include the peer's certificate,
1335 * which we don't include in s3_srvr.c
1337 x = sk_X509_value(sk, 0);
1340 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1343 pkey = X509_get_pubkey(x);
1345 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1348 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1349 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1353 i = ssl_cert_type(x, pkey);
1357 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1358 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1362 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1363 if (exp_idx >= 0 && i != exp_idx) {
1365 al = SSL_AD_ILLEGAL_PARAMETER;
1366 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1367 SSL_R_WRONG_CERTIFICATE_TYPE);
1370 s->session->peer_type = i;
1372 X509_free(s->session->peer);
1373 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1374 s->session->peer = x;
1375 s->session->verify_result = s->verify_result;
1382 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1384 s->state = SSL_ST_ERR;
1386 EVP_PKEY_free(pkey);
1388 sk_X509_pop_free(sk, X509_free);
1392 int ssl3_get_key_exchange(SSL *s)
1394 #ifndef OPENSSL_NO_RSA
1395 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1398 unsigned char *param, *p;
1400 long i, param_len, n, alg_k, alg_a;
1401 EVP_PKEY *pkey = NULL;
1402 const EVP_MD *md = NULL;
1403 #ifndef OPENSSL_NO_RSA
1406 #ifndef OPENSSL_NO_DH
1409 #ifndef OPENSSL_NO_EC
1410 EC_KEY *ecdh = NULL;
1411 BN_CTX *bn_ctx = NULL;
1412 EC_POINT *srvr_ecpoint = NULL;
1414 int encoded_pt_len = 0;
1417 EVP_MD_CTX_init(&md_ctx);
1420 * use same message size as in ssl3_get_certificate_request() as
1421 * ServerKeyExchange message may be skipped
1423 n = s->method->ssl_get_message(s,
1424 SSL3_ST_CR_KEY_EXCH_A,
1425 SSL3_ST_CR_KEY_EXCH_B,
1426 -1, s->max_cert_list, &ok);
1430 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1432 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1434 * Can't skip server key exchange if this is an ephemeral
1437 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1438 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1439 al = SSL_AD_UNEXPECTED_MESSAGE;
1442 #ifndef OPENSSL_NO_PSK
1444 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1445 * identity hint is sent. Set session->sess_cert anyway to avoid
1448 if (alg_k & SSL_kPSK) {
1449 s->session->sess_cert = ssl_sess_cert_new();
1450 OPENSSL_free(s->ctx->psk_identity_hint);
1451 s->ctx->psk_identity_hint = NULL;
1454 s->s3->tmp.reuse_message = 1;
1458 param = p = (unsigned char *)s->init_msg;
1460 #ifndef OPENSSL_NO_RSA
1461 RSA_free(s->s3->peer_rsa_tmp);
1462 s->s3->peer_rsa_tmp = NULL;
1464 #ifndef OPENSSL_NO_DH
1465 DH_free(s->s3->peer_dh_tmp);
1466 s->s3->peer_dh_tmp = NULL;
1468 #ifndef OPENSSL_NO_EC
1469 EC_KEY_free(s->s3->peer_ecdh_tmp);
1470 s->s3->peer_ecdh_tmp = NULL;
1473 if (s->session->sess_cert == NULL)
1474 s->session->sess_cert = ssl_sess_cert_new();
1476 /* Total length of the parameters including the length prefix */
1479 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1481 al = SSL_AD_DECODE_ERROR;
1483 #ifndef OPENSSL_NO_PSK
1484 if (alg_k & SSL_kPSK) {
1485 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1488 if (param_len > n) {
1489 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1495 * Store PSK identity hint for later use, hint is used in
1496 * ssl3_send_client_key_exchange. Assume that the maximum length of
1497 * a PSK identity hint can be as long as the maximum length of a PSK
1500 if (i > PSK_MAX_IDENTITY_LEN) {
1501 al = SSL_AD_HANDSHAKE_FAILURE;
1502 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1505 if (i > n - param_len) {
1506 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1507 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1513 * If received PSK identity hint contains NULL characters, the hint
1514 * is truncated from the first NULL. p may not be ending with NULL,
1515 * so create a NULL-terminated string.
1517 memcpy(tmp_id_hint, p, i);
1518 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1519 OPENSSL_free(s->ctx->psk_identity_hint);
1520 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1521 if (s->ctx->psk_identity_hint == NULL) {
1522 al = SSL_AD_HANDSHAKE_FAILURE;
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1530 #endif /* !OPENSSL_NO_PSK */
1531 #ifndef OPENSSL_NO_SRP
1532 if (alg_k & SSL_kSRP) {
1534 if (param_len > n) {
1535 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1540 if (i > n - param_len) {
1541 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1546 if ((s->srp_ctx.N = BN_bin2bn(p, i, NULL)) == NULL) {
1547 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1552 if (2 > n - param_len) {
1553 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1560 if (i > n - param_len) {
1561 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1566 if ((s->srp_ctx.g = BN_bin2bn(p, i, NULL)) == NULL) {
1567 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1572 if (1 > n - param_len) {
1573 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1578 i = (unsigned int)(p[0]);
1581 if (i > n - param_len) {
1582 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1587 if ((s->srp_ctx.s = BN_bin2bn(p, i, NULL)) == NULL) {
1588 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1593 if (2 > n - param_len) {
1594 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1601 if (i > n - param_len) {
1602 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1607 if ((s->srp_ctx.B = BN_bin2bn(p, i, NULL)) == NULL) {
1608 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1614 if (!srp_verify_server_param(s, &al)) {
1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1619 /* We must check if there is a certificate */
1620 # ifndef OPENSSL_NO_RSA
1621 if (alg_a & SSL_aRSA)
1622 pkey = X509_get_pubkey(s->session->peer);
1626 # ifndef OPENSSL_NO_DSA
1627 else if (alg_a & SSL_aDSS)
1628 pkey = X509_get_pubkey(s->session->peer);
1631 #endif /* !OPENSSL_NO_SRP */
1632 #ifndef OPENSSL_NO_RSA
1633 if (alg_k & SSL_kRSA) {
1634 /* Temporary RSA keys only allowed in export ciphersuites */
1635 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1636 al = SSL_AD_UNEXPECTED_MESSAGE;
1637 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1640 if ((rsa = RSA_new()) == NULL) {
1641 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1646 if (param_len > n) {
1647 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1652 if (i > n - param_len) {
1653 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1658 if ((rsa->n = BN_bin2bn(p, i, rsa->n)) == NULL) {
1659 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1664 if (2 > n - param_len) {
1665 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1672 if (i > n - param_len) {
1673 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1678 if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == NULL) {
1679 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1685 /* this should be because we are using an export cipher */
1686 if (alg_a & SSL_aRSA)
1687 pkey = X509_get_pubkey(s->session->peer);
1689 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1693 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1694 al = SSL_AD_UNEXPECTED_MESSAGE;
1695 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1699 s->s3->peer_rsa_tmp = rsa;
1702 #else /* OPENSSL_NO_RSA */
1705 #ifndef OPENSSL_NO_DH
1706 else if (alg_k & SSL_kDHE) {
1707 if ((dh = DH_new()) == NULL) {
1708 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1713 if (param_len > n) {
1714 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1719 if (i > n - param_len) {
1720 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1725 if ((dh->p = BN_bin2bn(p, i, NULL)) == NULL) {
1726 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1731 if (2 > n - param_len) {
1732 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1739 if (i > n - param_len) {
1740 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1745 if ((dh->g = BN_bin2bn(p, i, NULL)) == NULL) {
1746 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1751 if (2 > n - param_len) {
1752 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1759 if (i > n - param_len) {
1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1765 if ((dh->pub_key = BN_bin2bn(p, i, NULL)) == NULL) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1772 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1773 al = SSL_AD_HANDSHAKE_FAILURE;
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1777 # ifndef OPENSSL_NO_RSA
1778 if (alg_a & SSL_aRSA)
1779 pkey = X509_get_pubkey(s->session->peer);
1783 # ifndef OPENSSL_NO_DSA
1784 else if (alg_a & SSL_aDSS)
1785 pkey = X509_get_pubkey(s->session->peer);
1787 /* else anonymous DH, so no certificate or pkey. */
1789 s->s3->peer_dh_tmp = dh;
1792 #endif /* !OPENSSL_NO_DH */
1794 #ifndef OPENSSL_NO_EC
1795 else if (alg_k & SSL_kECDHE) {
1797 const EC_GROUP *group;
1799 if ((ecdh = EC_KEY_new()) == NULL) {
1800 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1805 * Extract elliptic curve parameters and the server's ephemeral ECDH
1806 * public key. Keep accumulating lengths of various components in
1807 * param_len and make sure it never exceeds n.
1811 * XXX: For now we only support named (not generic) curves and the
1812 * ECParameters in this case is just three bytes. We also need one
1813 * byte for the length of the encoded point
1816 if (param_len > n) {
1817 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1821 * Check curve is one of our preferences, if not server has sent an
1822 * invalid curve. ECParameters is 3 bytes.
1824 if (!tls1_check_curve(s, p, 3)) {
1825 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1829 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1830 al = SSL_AD_INTERNAL_ERROR;
1831 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1832 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1836 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1837 if (ngroup == NULL) {
1838 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1841 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1842 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1845 EC_GROUP_free(ngroup);
1847 group = EC_KEY_get0_group(ecdh);
1849 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1850 (EC_GROUP_get_degree(group) > 163)) {
1851 al = SSL_AD_EXPORT_RESTRICTION;
1852 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1853 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1859 /* Next, get the encoded ECPoint */
1860 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1861 ((bn_ctx = BN_CTX_new()) == NULL)) {
1862 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1866 encoded_pt_len = *p; /* length of encoded point */
1869 if ((encoded_pt_len > n - param_len) ||
1870 (EC_POINT_oct2point(group, srvr_ecpoint,
1871 p, encoded_pt_len, bn_ctx) == 0)) {
1872 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1875 param_len += encoded_pt_len;
1878 p += encoded_pt_len;
1881 * The ECC/TLS specification does not mention the use of DSA to sign
1882 * ECParameters in the server key exchange message. We do support RSA
1886 # ifndef OPENSSL_NO_RSA
1887 else if (alg_a & SSL_aRSA)
1888 pkey = X509_get_pubkey(s->session->peer);
1890 # ifndef OPENSSL_NO_EC
1891 else if (alg_a & SSL_aECDSA)
1892 pkey = X509_get_pubkey(s->session->peer);
1894 /* else anonymous ECDH, so no certificate or pkey. */
1895 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1896 s->s3->peer_ecdh_tmp = ecdh;
1898 BN_CTX_free(bn_ctx);
1900 EC_POINT_free(srvr_ecpoint);
1901 srvr_ecpoint = NULL;
1903 al = SSL_AD_UNEXPECTED_MESSAGE;
1904 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1907 #endif /* !OPENSSL_NO_EC */
1909 /* p points to the next byte, there are 'n' bytes left */
1911 /* if it was signed, check the signature */
1913 if (SSL_USE_SIGALGS(s)) {
1916 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1919 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1926 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1934 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1939 j = EVP_PKEY_size(pkey);
1942 * Check signature length. If n is 0 then signature is empty
1944 if ((i != n) || (n > j) || (n <= 0)) {
1945 /* wrong packet length */
1946 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1949 #ifndef OPENSSL_NO_RSA
1950 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1956 for (num = 2; num > 0; num--) {
1957 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1958 EVP_DigestInit_ex(&md_ctx, (num == 2)
1959 ? s->ctx->md5 : s->ctx->sha1, NULL);
1960 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1962 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1964 EVP_DigestUpdate(&md_ctx, param, param_len);
1965 EVP_DigestFinal_ex(&md_ctx, q, &size);
1969 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1971 al = SSL_AD_DECRYPT_ERROR;
1972 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1977 al = SSL_AD_DECRYPT_ERROR;
1978 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1984 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1985 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1987 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1989 EVP_VerifyUpdate(&md_ctx, param, param_len);
1990 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1992 al = SSL_AD_DECRYPT_ERROR;
1993 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1998 /* aNULL, aSRP or kPSK do not need public keys */
1999 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
2000 /* Might be wrong key type, check it */
2001 if (ssl3_check_cert_and_algorithm(s))
2002 /* Otherwise this shouldn't happen */
2003 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2006 /* still data left over */
2008 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2012 EVP_PKEY_free(pkey);
2013 EVP_MD_CTX_cleanup(&md_ctx);
2016 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2018 EVP_PKEY_free(pkey);
2019 #ifndef OPENSSL_NO_RSA
2022 #ifndef OPENSSL_NO_DH
2025 #ifndef OPENSSL_NO_EC
2026 BN_CTX_free(bn_ctx);
2027 EC_POINT_free(srvr_ecpoint);
2030 EVP_MD_CTX_cleanup(&md_ctx);
2031 s->state = SSL_ST_ERR;
2035 int ssl3_get_certificate_request(SSL *s)
2038 unsigned long n, nc, l;
2039 unsigned int llen, ctype_num, i;
2040 X509_NAME *xn = NULL;
2041 const unsigned char *p, *q;
2043 STACK_OF(X509_NAME) *ca_sk = NULL;
2045 n = s->method->ssl_get_message(s,
2046 SSL3_ST_CR_CERT_REQ_A,
2047 SSL3_ST_CR_CERT_REQ_B,
2048 -1, s->max_cert_list, &ok);
2053 s->s3->tmp.cert_req = 0;
2055 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2056 s->s3->tmp.reuse_message = 1;
2058 * If we get here we don't need any cached handshake records as we
2059 * wont be doing client auth.
2061 if (s->s3->handshake_buffer) {
2062 if (!ssl3_digest_cached_records(s))
2068 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2069 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2070 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2074 /* TLS does not like anon-DH with client cert */
2075 if (s->version > SSL3_VERSION) {
2076 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2077 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2078 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2079 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2084 p = d = (unsigned char *)s->init_msg;
2086 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2087 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2091 /* get the certificate types */
2093 OPENSSL_free(s->cert->ctypes);
2094 s->cert->ctypes = NULL;
2095 if (ctype_num > SSL3_CT_NUMBER) {
2096 /* If we exceed static buffer copy all to cert structure */
2097 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2098 if (s->cert->ctypes == NULL) {
2099 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2102 memcpy(s->cert->ctypes, p, ctype_num);
2103 s->cert->ctype_num = (size_t)ctype_num;
2104 ctype_num = SSL3_CT_NUMBER;
2106 for (i = 0; i < ctype_num; i++)
2107 s->s3->tmp.ctype[i] = p[i];
2109 if (SSL_USE_SIGALGS(s)) {
2112 * Check we have enough room for signature algorithms and following
2115 if ((unsigned long)(p - d + llen + 2) > n) {
2116 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2117 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2118 SSL_R_DATA_LENGTH_TOO_LONG);
2121 /* Clear certificate digests and validity flags */
2122 for (i = 0; i < SSL_PKEY_NUM; i++) {
2123 s->s3->tmp.md[i] = NULL;
2124 s->s3->tmp.valid_flags[i] = 0;
2126 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2127 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2128 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2129 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2132 if (!tls1_process_sigalgs(s)) {
2133 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2134 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2140 /* get the CA RDNs */
2143 if ((unsigned long)(p - d + llen) != n) {
2144 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2145 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2149 for (nc = 0; nc < llen;) {
2151 if ((l + nc + 2) > llen) {
2152 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2153 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2159 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2160 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2161 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2166 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2167 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2168 SSL_R_CA_DN_LENGTH_MISMATCH);
2171 if (!sk_X509_NAME_push(ca_sk, xn)) {
2172 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2180 /* we should setup a certificate to return.... */
2181 s->s3->tmp.cert_req = 1;
2182 s->s3->tmp.ctype_num = ctype_num;
2183 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2184 s->s3->tmp.ca_names = ca_sk;
2190 s->state = SSL_ST_ERR;
2192 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2196 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2198 return (X509_NAME_cmp(*a, *b));
2201 int ssl3_get_new_session_ticket(SSL *s)
2203 int ok, al, ret = 0, ticklen;
2205 const unsigned char *p;
2208 n = s->method->ssl_get_message(s,
2209 SSL3_ST_CR_SESSION_TICKET_A,
2210 SSL3_ST_CR_SESSION_TICKET_B,
2211 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2217 /* need at least ticket_lifetime_hint + ticket length */
2218 al = SSL_AD_DECODE_ERROR;
2219 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2223 p = d = (unsigned char *)s->init_msg;
2225 if (s->session->session_id_length > 0) {
2226 int i = s->session_ctx->session_cache_mode;
2227 SSL_SESSION *new_sess;
2229 * We reused an existing session, so we need to replace it with a new
2232 if (i & SSL_SESS_CACHE_CLIENT) {
2234 * Remove the old session from the cache
2236 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2237 if (s->session_ctx->remove_session_cb != NULL)
2238 s->session_ctx->remove_session_cb(s->session_ctx,
2241 /* We carry on if this fails */
2242 SSL_CTX_remove_session(s->session_ctx, s->session);
2246 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2247 al = SSL_AD_INTERNAL_ERROR;
2248 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2252 SSL_SESSION_free(s->session);
2253 s->session = new_sess;
2256 n2l(p, s->session->tlsext_tick_lifetime_hint);
2258 /* ticket_lifetime_hint + ticket_length + ticket */
2259 if (ticklen + 6 != n) {
2260 al = SSL_AD_DECODE_ERROR;
2261 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2264 OPENSSL_free(s->session->tlsext_tick);
2265 s->session->tlsext_ticklen = 0;
2266 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2267 if (!s->session->tlsext_tick) {
2268 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2271 memcpy(s->session->tlsext_tick, p, ticklen);
2272 s->session->tlsext_ticklen = ticklen;
2274 * There are two ways to detect a resumed ticket session. One is to set
2275 * an appropriate session ID and then the server must return a match in
2276 * ServerHello. This allows the normal client session ID matching to work
2277 * and we know much earlier that the ticket has been accepted. The
2278 * other way is to set zero length session ID when the ticket is
2279 * presented and rely on the handshake to determine session resumption.
2280 * We choose the former approach because this fits in with assumptions
2281 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2282 * SHA256 is disabled) hash of the ticket.
2284 EVP_Digest(p, ticklen,
2285 s->session->session_id, &s->session->session_id_length,
2286 EVP_sha256(), NULL);
2290 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2292 s->state = SSL_ST_ERR;
2296 int ssl3_get_cert_status(SSL *s)
2299 unsigned long resplen, n;
2300 const unsigned char *p;
2302 n = s->method->ssl_get_message(s,
2303 SSL3_ST_CR_CERT_STATUS_A,
2304 SSL3_ST_CR_CERT_STATUS_B,
2305 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2310 /* need at least status type + length */
2311 al = SSL_AD_DECODE_ERROR;
2312 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2315 p = (unsigned char *)s->init_msg;
2316 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2317 al = SSL_AD_DECODE_ERROR;
2318 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2322 if (resplen + 4 != n) {
2323 al = SSL_AD_DECODE_ERROR;
2324 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2327 OPENSSL_free(s->tlsext_ocsp_resp);
2328 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2329 if (!s->tlsext_ocsp_resp) {
2330 al = SSL_AD_INTERNAL_ERROR;
2331 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2334 s->tlsext_ocsp_resplen = resplen;
2335 if (s->ctx->tlsext_status_cb) {
2337 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2339 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2340 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2344 al = SSL_AD_INTERNAL_ERROR;
2345 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2351 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2352 s->state = SSL_ST_ERR;
2356 int ssl3_get_server_done(SSL *s)
2361 /* Second to last param should be very small, like 0 :-) */
2362 n = s->method->ssl_get_message(s,
2363 SSL3_ST_CR_SRVR_DONE_A,
2364 SSL3_ST_CR_SRVR_DONE_B,
2365 SSL3_MT_SERVER_DONE, 30, &ok);
2370 /* should contain no data */
2371 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2372 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2373 s->state = SSL_ST_ERR;
2380 int ssl3_send_client_key_exchange(SSL *s)
2384 unsigned long alg_k;
2385 #ifndef OPENSSL_NO_RSA
2387 EVP_PKEY *pkey = NULL;
2389 #ifndef OPENSSL_NO_EC
2390 EC_KEY *clnt_ecdh = NULL;
2391 const EC_POINT *srvr_ecpoint = NULL;
2392 EVP_PKEY *srvr_pub_pkey = NULL;
2393 unsigned char *encodedPoint = NULL;
2394 int encoded_pt_len = 0;
2395 BN_CTX *bn_ctx = NULL;
2397 unsigned char *pms = NULL;
2400 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2401 p = ssl_handshake_start(s);
2403 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2405 /* Fool emacs indentation */
2408 #ifndef OPENSSL_NO_RSA
2409 else if (alg_k & SSL_kRSA) {
2411 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2412 pms = OPENSSL_malloc(pmslen);
2416 if (s->session->sess_cert == NULL) {
2418 * We should always have a server certificate with SSL_kRSA.
2420 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2421 ERR_R_INTERNAL_ERROR);
2425 if (s->s3->peer_rsa_tmp != NULL)
2426 rsa = s->s3->peer_rsa_tmp;
2428 pkey = X509_get_pubkey(s->session->peer);
2429 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2430 || (pkey->pkey.rsa == NULL)) {
2431 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2432 ERR_R_INTERNAL_ERROR);
2435 rsa = pkey->pkey.rsa;
2436 EVP_PKEY_free(pkey);
2439 pms[0] = s->client_version >> 8;
2440 pms[1] = s->client_version & 0xff;
2441 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2445 /* Fix buf for TLS and beyond */
2446 if (s->version > SSL3_VERSION)
2448 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2450 if (s->options & SSL_OP_PKCS1_CHECK_1)
2452 if (s->options & SSL_OP_PKCS1_CHECK_2)
2456 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2457 SSL_R_BAD_RSA_ENCRYPT);
2461 /* Fix buf for TLS and beyond */
2462 if (s->version > SSL3_VERSION) {
2468 #ifndef OPENSSL_NO_DH
2469 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2470 DH *dh_srvr, *dh_clnt;
2471 SESS_CERT *scert = s->session->sess_cert;
2473 if (scert == NULL) {
2474 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2475 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2476 SSL_R_UNEXPECTED_MESSAGE);
2480 if (s->s3->peer_dh_tmp != NULL)
2481 dh_srvr = s->s3->peer_dh_tmp;
2483 /* we get them from the cert */
2484 EVP_PKEY *spkey = NULL;
2486 spkey = X509_get_pubkey(s->session->peer);
2488 dh_srvr = EVP_PKEY_get1_DH(spkey);
2489 EVP_PKEY_free(spkey);
2491 if (dh_srvr == NULL) {
2492 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2493 ERR_R_INTERNAL_ERROR);
2497 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2498 /* Use client certificate key */
2499 EVP_PKEY *clkey = s->cert->key->privatekey;
2502 dh_clnt = EVP_PKEY_get1_DH(clkey);
2503 if (dh_clnt == NULL) {
2504 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2505 ERR_R_INTERNAL_ERROR);
2509 /* generate a new random key */
2510 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2511 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2514 if (!DH_generate_key(dh_clnt)) {
2515 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2521 pmslen = DH_size(dh_clnt);
2522 pms = OPENSSL_malloc(pmslen);
2527 * use the 'p' output buffer for the DH key, but make sure to
2528 * clear it out afterwards
2531 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2532 if (s->s3->peer_dh_tmp == NULL)
2536 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2542 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2545 /* send off the data */
2546 n = BN_num_bytes(dh_clnt->pub_key);
2548 BN_bn2bin(dh_clnt->pub_key, p);
2556 #ifndef OPENSSL_NO_EC
2557 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2558 const EC_GROUP *srvr_group = NULL;
2560 int ecdh_clnt_cert = 0;
2563 if (s->session->sess_cert == NULL) {
2564 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2565 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2566 SSL_R_UNEXPECTED_MESSAGE);
2571 * Did we send out the client's ECDH share for use in premaster
2572 * computation as part of client certificate? If so, set
2573 * ecdh_clnt_cert to 1.
2575 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2577 * XXX: For now, we do not support client
2578 * authentication using ECDH certificates.
2579 * To add such support, one needs to add
2580 * code that checks for appropriate
2581 * conditions and sets ecdh_clnt_cert to 1.
2582 * For example, the cert have an ECC
2583 * key on the same curve as the server's
2584 * and the key should be authorized for
2587 * One also needs to add code in ssl3_connect
2588 * to skip sending the certificate verify
2591 * if ((s->cert->key->privatekey != NULL) &&
2592 * (s->cert->key->privatekey->type ==
2593 * EVP_PKEY_EC) && ...)
2594 * ecdh_clnt_cert = 1;
2598 if (s->s3->peer_ecdh_tmp != NULL) {
2599 tkey = s->s3->peer_ecdh_tmp;
2601 /* Get the Server Public Key from Cert */
2602 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2603 if ((srvr_pub_pkey == NULL)
2604 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2605 || (srvr_pub_pkey->pkey.ec == NULL)) {
2606 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2607 ERR_R_INTERNAL_ERROR);
2611 tkey = srvr_pub_pkey->pkey.ec;
2614 srvr_group = EC_KEY_get0_group(tkey);
2615 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2617 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2618 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2619 ERR_R_INTERNAL_ERROR);
2623 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2624 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2625 ERR_R_MALLOC_FAILURE);
2629 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2630 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2633 if (ecdh_clnt_cert) {
2635 * Reuse key info from our certificate We only need our
2636 * private key to perform the ECDH computation.
2638 const BIGNUM *priv_key;
2639 tkey = s->cert->key->privatekey->pkey.ec;
2640 priv_key = EC_KEY_get0_private_key(tkey);
2641 if (priv_key == NULL) {
2642 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2643 ERR_R_MALLOC_FAILURE);
2646 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2647 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2651 /* Generate a new ECDH key pair */
2652 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2653 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2660 * use the 'p' output buffer for the ECDH key, but make sure to
2661 * clear it out afterwards
2664 field_size = EC_GROUP_get_degree(srvr_group);
2665 if (field_size <= 0) {
2666 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2669 pmslen = (field_size + 7) / 8;
2670 pms = OPENSSL_malloc(pmslen);
2673 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2674 if (n <= 0 || pmslen != (size_t)n) {
2675 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2679 if (ecdh_clnt_cert) {
2680 /* Send empty client key exch message */
2684 * First check the size of encoding and allocate memory
2688 EC_POINT_point2oct(srvr_group,
2689 EC_KEY_get0_public_key(clnt_ecdh),
2690 POINT_CONVERSION_UNCOMPRESSED,
2693 encodedPoint = (unsigned char *)
2694 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2695 bn_ctx = BN_CTX_new();
2696 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2697 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2698 ERR_R_MALLOC_FAILURE);
2702 /* Encode the public key */
2703 n = EC_POINT_point2oct(srvr_group,
2704 EC_KEY_get0_public_key(clnt_ecdh),
2705 POINT_CONVERSION_UNCOMPRESSED,
2706 encodedPoint, encoded_pt_len, bn_ctx);
2708 *p = n; /* length of encoded point */
2709 /* Encoded point will be copied here */
2711 /* copy the point */
2712 memcpy(p, encodedPoint, n);
2713 /* increment n to account for length field */
2717 /* Free allocated memory */
2718 BN_CTX_free(bn_ctx);
2719 OPENSSL_free(encodedPoint);
2720 EC_KEY_free(clnt_ecdh);
2721 EVP_PKEY_free(srvr_pub_pkey);
2723 #endif /* !OPENSSL_NO_EC */
2724 else if (alg_k & SSL_kGOST) {
2725 /* GOST key exchange message creation */
2726 EVP_PKEY_CTX *pkey_ctx;
2729 unsigned int md_len;
2730 unsigned char shared_ukm[32], tmp[256];
2731 EVP_MD_CTX *ukm_hash;
2735 pms = OPENSSL_malloc(pmslen);
2740 * Get server sertificate PKEY and create ctx from it
2742 peer_cert = s->session->peer;
2744 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2745 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2749 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2750 X509_get_pubkey(peer_cert), NULL);
2752 * If we have send a certificate, and certificate key
2754 * * parameters match those of server certificate, use
2755 * certificate key for key exchange
2758 /* Otherwise, generate ephemeral key pair */
2760 EVP_PKEY_encrypt_init(pkey_ctx);
2761 /* Generate session key */
2762 if (RAND_bytes(pms, pmslen) <= 0) {
2763 EVP_PKEY_CTX_free(pkey_ctx);
2764 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2765 ERR_R_INTERNAL_ERROR);
2769 * If we have client certificate, use its secret as peer key
2771 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2772 if (EVP_PKEY_derive_set_peer
2773 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2775 * If there was an error - just ignore it. Ephemeral key
2782 * Compute shared IV and store it in algorithm-specific context
2785 ukm_hash = EVP_MD_CTX_create();
2786 EVP_DigestInit(ukm_hash,
2787 EVP_get_digestbynid(NID_id_GostR3411_94));
2788 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2790 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2792 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2793 EVP_MD_CTX_destroy(ukm_hash);
2794 if (EVP_PKEY_CTX_ctrl
2795 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2797 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2801 /* Make GOST keytransport blob message */
2803 * Encapsulate it into sequence
2805 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2807 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2808 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2812 if (msglen >= 0x80) {
2814 *(p++) = msglen & 0xff;
2817 *(p++) = msglen & 0xff;
2820 memcpy(p, tmp, msglen);
2821 /* Check if pubkey from client certificate was used */
2822 if (EVP_PKEY_CTX_ctrl
2823 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2824 /* Set flag "skip certificate verify" */
2825 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2827 EVP_PKEY_CTX_free(pkey_ctx);
2828 EVP_PKEY_free(pub_key);
2831 #ifndef OPENSSL_NO_SRP
2832 else if (alg_k & SSL_kSRP) {
2833 if (s->srp_ctx.A != NULL) {
2834 /* send off the data */
2835 n = BN_num_bytes(s->srp_ctx.A);
2837 BN_bn2bin(s->srp_ctx.A, p);
2840 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2841 ERR_R_INTERNAL_ERROR);
2844 OPENSSL_free(s->session->srp_username);
2845 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2846 if (s->session->srp_username == NULL) {
2847 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2848 ERR_R_MALLOC_FAILURE);
2853 #ifndef OPENSSL_NO_PSK
2854 else if (alg_k & SSL_kPSK) {
2856 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2857 * \0-terminated identity. The last byte is for us for simulating
2860 char identity[PSK_MAX_IDENTITY_LEN + 2];
2861 size_t identity_len;
2862 unsigned char *t = NULL;
2863 unsigned int psk_len = 0;
2867 if (s->psk_client_callback == NULL) {
2868 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2869 SSL_R_PSK_NO_CLIENT_CB);
2873 memset(identity, 0, sizeof(identity));
2874 /* Allocate maximum size buffer */
2875 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2876 pms = OPENSSL_malloc(pmslen);
2880 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2881 identity, sizeof(identity) - 1,
2883 if (psk_len > PSK_MAX_PSK_LEN) {
2884 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2885 ERR_R_INTERNAL_ERROR);
2887 } else if (psk_len == 0) {
2888 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2889 SSL_R_PSK_IDENTITY_NOT_FOUND);
2892 /* Change pmslen to real length */
2893 pmslen = 2 + psk_len + 2 + psk_len;
2894 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2895 identity_len = strlen(identity);
2896 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2897 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2898 ERR_R_INTERNAL_ERROR);
2901 /* create PSK pre_master_secret */
2903 memmove(pms + psk_len + 4, pms, psk_len);
2905 memset(t, 0, psk_len);
2909 OPENSSL_free(s->session->psk_identity_hint);
2910 s->session->psk_identity_hint =
2911 BUF_strdup(s->ctx->psk_identity_hint);
2912 if (s->ctx->psk_identity_hint != NULL
2913 && s->session->psk_identity_hint == NULL) {
2914 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2915 ERR_R_MALLOC_FAILURE);
2919 OPENSSL_free(s->session->psk_identity);
2920 s->session->psk_identity = BUF_strdup(identity);
2921 if (s->session->psk_identity == NULL) {
2922 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2923 ERR_R_MALLOC_FAILURE);
2927 s2n(identity_len, p);
2928 memcpy(p, identity, identity_len);
2929 n = 2 + identity_len;
2932 OPENSSL_cleanse(identity, sizeof(identity));
2934 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2940 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2941 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2945 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2946 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2947 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2951 s->state = SSL3_ST_CW_KEY_EXCH_B;
2954 /* SSL3_ST_CW_KEY_EXCH_B */
2955 n = ssl_do_write(s);
2956 #ifndef OPENSSL_NO_SRP
2958 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2960 * If everything written generate master key: no need to save PMS as
2961 * SRP_generate_client_master_secret generates it internally.
2964 if ((s->session->master_key_length =
2965 SRP_generate_client_master_secret(s,
2966 s->session->master_key)) <
2968 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2969 ERR_R_INTERNAL_ERROR);
2975 /* If we haven't written everything save PMS */
2977 s->s3->tmp.pms = pms;
2978 s->s3->tmp.pmslen = pmslen;
2980 /* If we don't have a PMS restore */
2982 pms = s->s3->tmp.pms;
2983 pmslen = s->s3->tmp.pmslen;
2986 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2987 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2990 s->session->master_key_length =
2991 s->method->ssl3_enc->generate_master_secret(s,
2993 session->master_key,
2995 OPENSSL_clear_free(pms, pmslen);
2996 s->s3->tmp.pms = NULL;
2997 if (s->session->master_key_length < 0) {
2998 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2999 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3005 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3006 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3008 OPENSSL_clear_free(pms, pmslen);
3009 s->s3->tmp.pms = NULL;
3010 #ifndef OPENSSL_NO_EC
3011 BN_CTX_free(bn_ctx);
3012 OPENSSL_free(encodedPoint);
3013 EC_KEY_free(clnt_ecdh);
3014 EVP_PKEY_free(srvr_pub_pkey);
3016 s->state = SSL_ST_ERR;
3020 int ssl3_send_client_verify(SSL *s)
3023 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3025 EVP_PKEY_CTX *pctx = NULL;
3031 EVP_MD_CTX_init(&mctx);
3033 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3034 p = ssl_handshake_start(s);
3035 pkey = s->cert->key->privatekey;
3036 /* Create context from key and test if sha1 is allowed as digest */
3037 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3038 EVP_PKEY_sign_init(pctx);
3039 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3040 if (!SSL_USE_SIGALGS(s))
3041 s->method->ssl3_enc->cert_verify_mac(s,
3044 [MD5_DIGEST_LENGTH]));
3049 * For TLS v1.2 send signature algorithm and signature using agreed
3050 * digest and cached handshake records.
3052 if (SSL_USE_SIGALGS(s)) {
3055 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3056 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3057 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3058 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3063 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3066 if (!EVP_SignInit_ex(&mctx, md, NULL)
3067 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3068 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3069 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3075 * For extended master secret we've already digested cached
3078 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
3079 BIO_free(s->s3->handshake_buffer);
3080 s->s3->handshake_buffer = NULL;
3081 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3082 } else if (!ssl3_digest_cached_records(s))
3085 #ifndef OPENSSL_NO_RSA
3086 if (pkey->type == EVP_PKEY_RSA) {
3087 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3088 if (RSA_sign(NID_md5_sha1, data,
3089 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3090 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3091 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3098 #ifndef OPENSSL_NO_DSA
3099 if (pkey->type == EVP_PKEY_DSA) {
3100 if (!DSA_sign(pkey->save_type,
3101 &(data[MD5_DIGEST_LENGTH]),
3102 SHA_DIGEST_LENGTH, &(p[2]),
3103 (unsigned int *)&j, pkey->pkey.dsa)) {
3104 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3111 #ifndef OPENSSL_NO_EC
3112 if (pkey->type == EVP_PKEY_EC) {
3113 if (!ECDSA_sign(pkey->save_type,
3114 &(data[MD5_DIGEST_LENGTH]),
3115 SHA_DIGEST_LENGTH, &(p[2]),
3116 (unsigned int *)&j, pkey->pkey.ec)) {
3117 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3124 if (pkey->type == NID_id_GostR3410_94
3125 || pkey->type == NID_id_GostR3410_2001) {
3126 unsigned char signbuf[64];
3128 size_t sigsize = 64;
3129 s->method->ssl3_enc->cert_verify_mac(s,
3130 NID_id_GostR3411_94, data);
3131 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3132 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3135 for (i = 63, j = 0; i >= 0; j++, i--) {
3136 p[2 + j] = signbuf[i];
3141 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3144 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3145 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3148 s->state = SSL3_ST_CW_CERT_VRFY_B;
3150 EVP_MD_CTX_cleanup(&mctx);
3151 EVP_PKEY_CTX_free(pctx);
3152 return ssl_do_write(s);
3154 EVP_MD_CTX_cleanup(&mctx);
3155 EVP_PKEY_CTX_free(pctx);
3156 s->state = SSL_ST_ERR;
3161 * Check a certificate can be used for client authentication. Currently check
3162 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3163 * certificates can be used and optionally checks suitability for Suite B.
3165 static int ssl3_check_client_certificate(SSL *s)
3167 unsigned long alg_k;
3168 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3170 /* If no suitable signature algorithm can't use certificate */
3171 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3174 * If strict mode check suitability of chain before using it. This also
3175 * adjusts suite B digest if necessary.
3177 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3178 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3180 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3181 /* See if we can use client certificate for fixed DH */
3182 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3183 int i = s->session->peer_type;
3184 EVP_PKEY *clkey = NULL, *spkey = NULL;
3185 clkey = s->cert->key->privatekey;
3186 /* If client key not DH assume it can be used */
3187 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3190 spkey = X509_get_pubkey(s->session->peer);
3192 /* Compare server and client parameters */
3193 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3194 EVP_PKEY_free(spkey);
3198 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3203 int ssl3_send_client_certificate(SSL *s)
3206 EVP_PKEY *pkey = NULL;
3209 if (s->state == SSL3_ST_CW_CERT_A) {
3210 /* Let cert callback update client certificates if required */
3211 if (s->cert->cert_cb) {
3212 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3214 s->rwstate = SSL_X509_LOOKUP;
3218 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3219 s->state = SSL_ST_ERR;
3222 s->rwstate = SSL_NOTHING;
3224 if (ssl3_check_client_certificate(s))
3225 s->state = SSL3_ST_CW_CERT_C;
3227 s->state = SSL3_ST_CW_CERT_B;
3230 /* We need to get a client cert */
3231 if (s->state == SSL3_ST_CW_CERT_B) {
3233 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3234 * return(-1); We then get retied later
3237 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3239 s->rwstate = SSL_X509_LOOKUP;
3242 s->rwstate = SSL_NOTHING;
3243 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3244 s->state = SSL3_ST_CW_CERT_B;
3245 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3247 } else if (i == 1) {
3249 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3250 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3254 EVP_PKEY_free(pkey);
3255 if (i && !ssl3_check_client_certificate(s))
3258 if (s->version == SSL3_VERSION) {
3259 s->s3->tmp.cert_req = 0;
3260 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3263 s->s3->tmp.cert_req = 2;
3264 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3265 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3266 s->state = SSL_ST_ERR;
3272 /* Ok, we have a cert */
3273 s->state = SSL3_ST_CW_CERT_C;
3276 if (s->state == SSL3_ST_CW_CERT_C) {
3277 s->state = SSL3_ST_CW_CERT_D;
3278 if (!ssl3_output_cert_chain(s,
3279 (s->s3->tmp.cert_req ==
3280 2) ? NULL : s->cert->key)) {
3281 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3282 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3283 s->state = SSL_ST_ERR;
3287 /* SSL3_ST_CW_CERT_D */
3288 return ssl_do_write(s);
3291 #define has_bits(i,m) (((i)&(m)) == (m))
3293 int ssl3_check_cert_and_algorithm(SSL *s)
3297 EVP_PKEY *pkey = NULL;
3300 #ifndef OPENSSL_NO_RSA
3303 #ifndef OPENSSL_NO_DH
3306 int al = SSL_AD_HANDSHAKE_FAILURE;
3308 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3309 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3311 /* we don't have a certificate */
3312 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3315 sc = s->session->sess_cert;
3317 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3320 #ifndef OPENSSL_NO_RSA
3321 rsa = s->s3->peer_rsa_tmp;
3323 #ifndef OPENSSL_NO_DH
3324 dh = s->s3->peer_dh_tmp;
3327 /* This is the passed certificate */
3329 idx = s->session->peer_type;
3330 #ifndef OPENSSL_NO_EC
3331 if (idx == SSL_PKEY_ECC) {
3332 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3334 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3339 } else if (alg_a & SSL_aECDSA) {
3340 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3341 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3343 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3344 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3348 pkey = X509_get_pubkey(s->session->peer);
3349 pkey_bits = EVP_PKEY_bits(pkey);
3350 i = X509_certificate_type(s->session->peer, pkey);
3351 EVP_PKEY_free(pkey);
3353 /* Check that we have a certificate if we require one */
3354 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3355 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3356 SSL_R_MISSING_RSA_SIGNING_CERT);
3359 #ifndef OPENSSL_NO_DSA
3360 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3361 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3362 SSL_R_MISSING_DSA_SIGNING_CERT);
3366 #ifndef OPENSSL_NO_RSA
3367 if (alg_k & SSL_kRSA) {
3368 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3369 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3370 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3371 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3373 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3374 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3375 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3376 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3377 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3381 /* server key exchange is not allowed. */
3382 al = SSL_AD_INTERNAL_ERROR;
3383 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3390 #ifndef OPENSSL_NO_DH
3391 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3392 al = SSL_AD_INTERNAL_ERROR;
3393 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3395 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3396 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3397 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3398 SSL_R_MISSING_DH_RSA_CERT);
3401 # ifndef OPENSSL_NO_DSA
3402 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3403 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3404 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3405 SSL_R_MISSING_DH_DSA_CERT);
3411 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3412 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3413 #ifndef OPENSSL_NO_RSA
3414 if (alg_k & SSL_kRSA) {
3416 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3417 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3419 } else if (RSA_bits(rsa) >
3420 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3421 /* We have a temporary RSA key but it's too large. */
3422 al = SSL_AD_EXPORT_RESTRICTION;
3423 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3424 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3429 #ifndef OPENSSL_NO_DH
3430 if (alg_k & SSL_kDHE) {
3432 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3433 /* We have a temporary DH key but it's too large. */
3434 al = SSL_AD_EXPORT_RESTRICTION;
3435 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3436 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3439 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3440 /* The cert should have had an export DH key. */
3441 al = SSL_AD_EXPORT_RESTRICTION;
3442 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3443 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3448 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3449 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3455 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3461 * Normally, we can tell if the server is resuming the session from
3462 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3463 * message after the ServerHello to determine if the server is resuming.
3464 * Therefore, we allow EAP-FAST to peek ahead.
3465 * ssl3_check_finished returns 1 if we are resuming from an external
3466 * pre-shared secret, we have a "ticket" and the next server handshake message
3467 * is Finished; and 0 otherwise. It returns -1 upon an error.
3469 static int ssl3_check_finished(SSL *s)
3473 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3474 !s->session->tlsext_tick)
3477 /* Need to permit this temporarily, in case the next message is Finished. */
3478 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3480 * This function is called when we might get a Certificate message instead,
3481 * so permit appropriate message length.
3482 * We ignore the return value as we're only interested in the message type
3483 * and not its length.
3485 s->method->ssl_get_message(s,
3488 -1, s->max_cert_list, &ok);
3489 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3494 s->s3->tmp.reuse_message = 1;
3496 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3499 /* If we're not done, then the CCS arrived early and we should bail. */
3500 if (s->s3->change_cipher_spec) {
3501 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3502 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3509 #ifndef OPENSSL_NO_NEXTPROTONEG
3510 int ssl3_send_next_proto(SSL *s)
3512 unsigned int len, padding_len;
3515 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3516 len = s->next_proto_negotiated_len;
3517 padding_len = 32 - ((len + 2) % 32);
3518 d = (unsigned char *)s->init_buf->data;
3520 memcpy(d + 5, s->next_proto_negotiated, len);
3521 d[5 + len] = padding_len;
3522 memset(d + 6 + len, 0, padding_len);
3523 *(d++) = SSL3_MT_NEXT_PROTO;
3524 l2n3(2 + len + padding_len, d);
3525 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3526 s->init_num = 4 + 2 + len + padding_len;
3530 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3534 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3537 #ifndef OPENSSL_NO_ENGINE
3538 if (s->ctx->client_cert_engine) {
3539 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3540 SSL_get_client_CA_list(s),
3541 px509, ppkey, NULL, NULL, NULL);
3546 if (s->ctx->client_cert_cb)
3547 i = s->ctx->client_cert_cb(s, px509, ppkey);
3551 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3553 int (*put_cb) (const SSL_CIPHER *,
3559 int empty_reneg_info_scsv = !s->renegotiate;
3560 /* Set disabled masks for this session */
3561 ssl_set_client_disabled(s);
3567 put_cb = s->method->put_cipher_by_char;
3569 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3570 c = sk_SSL_CIPHER_value(sk, i);
3571 /* Skip disabled ciphers */
3572 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3574 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3575 if (c->id == SSL3_CK_SCSV) {
3576 if (!empty_reneg_info_scsv)
3579 empty_reneg_info_scsv = 0;
3586 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3590 if (empty_reneg_info_scsv) {
3591 static SSL_CIPHER scsv = {
3592 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3594 j = put_cb(&scsv, p);
3596 #ifdef OPENSSL_RI_DEBUG
3598 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3601 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3602 static SSL_CIPHER scsv = {
3603 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3605 j = put_cb(&scsv, p);