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 #ifndef OPENSSL_NO_TLSEXT
169 static int ssl3_check_finished(SSL *s);
171 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
173 int (*put_cb) (const SSL_CIPHER *,
177 int ssl3_connect(SSL *s)
180 unsigned long Time = (unsigned long)time(NULL);
181 void (*cb) (const SSL *ssl, int type, int val) = NULL;
183 int new_state, state, skip = 0;
185 RAND_add(&Time, sizeof(Time), 0);
189 if (s->info_callback != NULL)
190 cb = s->info_callback;
191 else if (s->ctx->info_callback != NULL)
192 cb = s->ctx->info_callback;
195 if (!SSL_in_init(s) || SSL_in_before(s)) {
200 #ifndef OPENSSL_NO_HEARTBEATS
202 * If we're awaiting a HeartbeatResponse, pretend we already got and
203 * don't await it anymore, because Heartbeats don't make sense during
206 if (s->tlsext_hb_pending) {
207 s->tlsext_hb_pending = 0;
216 case SSL_ST_RENEGOTIATE:
218 s->state = SSL_ST_CONNECT;
219 s->ctx->stats.sess_connect_renegotiate++;
223 case SSL_ST_BEFORE | SSL_ST_CONNECT:
224 case SSL_ST_OK | SSL_ST_CONNECT:
228 cb(s, SSL_CB_HANDSHAKE_START, 1);
230 if ((s->version >> 8) != SSL3_VERSION_MAJOR
231 && s->version != TLS_ANY_VERSION) {
232 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
233 s->state = SSL_ST_ERR;
238 if (s->version != TLS_ANY_VERSION &&
239 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
240 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
244 /* s->version=SSL3_VERSION; */
245 s->type = SSL_ST_CONNECT;
247 if (s->init_buf == NULL) {
248 if ((buf = BUF_MEM_new()) == NULL) {
250 s->state = SSL_ST_ERR;
253 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
255 s->state = SSL_ST_ERR;
262 if (!ssl3_setup_buffers(s)) {
267 /* setup buffing BIO */
268 if (!ssl_init_wbio_buffer(s, 0)) {
270 s->state = SSL_ST_ERR;
274 /* don't push the buffering BIO quite yet */
276 ssl3_init_finished_mac(s);
278 s->state = SSL3_ST_CW_CLNT_HELLO_A;
279 s->ctx->stats.sess_connect++;
281 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
283 * Should have been reset by ssl3_get_finished, too.
285 s->s3->change_cipher_spec = 0;
288 case SSL3_ST_CW_CLNT_HELLO_A:
289 case SSL3_ST_CW_CLNT_HELLO_B:
292 ret = ssl3_client_hello(s);
295 s->state = SSL3_ST_CR_SRVR_HELLO_A;
298 /* turn on buffering for the next lot of output */
299 if (s->bbio != s->wbio)
300 s->wbio = BIO_push(s->bbio, s->wbio);
304 case SSL3_ST_CR_SRVR_HELLO_A:
305 case SSL3_ST_CR_SRVR_HELLO_B:
306 ret = ssl3_get_server_hello(s);
311 s->state = SSL3_ST_CR_FINISHED_A;
312 #ifndef OPENSSL_NO_TLSEXT
313 if (s->tlsext_ticket_expected) {
314 /* receive renewed session ticket */
315 s->state = SSL3_ST_CR_SESSION_TICKET_A;
319 s->state = SSL3_ST_CR_CERT_A;
323 case SSL3_ST_CR_CERT_A:
324 case SSL3_ST_CR_CERT_B:
325 #ifndef OPENSSL_NO_TLSEXT
326 /* Noop (ret = 0) for everything but EAP-FAST. */
327 ret = ssl3_check_finished(s);
332 s->state = SSL3_ST_CR_FINISHED_A;
337 /* Check if it is anon DH/ECDH, SRP auth */
341 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
342 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
343 ret = ssl3_get_server_certificate(s);
346 #ifndef OPENSSL_NO_TLSEXT
347 if (s->tlsext_status_expected)
348 s->state = SSL3_ST_CR_CERT_STATUS_A;
350 s->state = SSL3_ST_CR_KEY_EXCH_A;
353 s->state = SSL3_ST_CR_KEY_EXCH_A;
359 s->state = SSL3_ST_CR_KEY_EXCH_A;
364 case SSL3_ST_CR_KEY_EXCH_A:
365 case SSL3_ST_CR_KEY_EXCH_B:
366 ret = ssl3_get_key_exchange(s);
369 s->state = SSL3_ST_CR_CERT_REQ_A;
373 * at this point we check that we have the required stuff from
376 if (!ssl3_check_cert_and_algorithm(s)) {
378 s->state = SSL_ST_ERR;
383 case SSL3_ST_CR_CERT_REQ_A:
384 case SSL3_ST_CR_CERT_REQ_B:
385 ret = ssl3_get_certificate_request(s);
388 s->state = SSL3_ST_CR_SRVR_DONE_A;
392 case SSL3_ST_CR_SRVR_DONE_A:
393 case SSL3_ST_CR_SRVR_DONE_B:
394 ret = ssl3_get_server_done(s);
397 #ifndef OPENSSL_NO_SRP
398 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
399 if ((ret = SRP_Calc_A_param(s)) <= 0) {
400 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
401 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
402 s->state = SSL_ST_ERR;
407 if (s->s3->tmp.cert_req)
408 s->state = SSL3_ST_CW_CERT_A;
410 s->state = SSL3_ST_CW_KEY_EXCH_A;
415 case SSL3_ST_CW_CERT_A:
416 case SSL3_ST_CW_CERT_B:
417 case SSL3_ST_CW_CERT_C:
418 case SSL3_ST_CW_CERT_D:
419 ret = ssl3_send_client_certificate(s);
422 s->state = SSL3_ST_CW_KEY_EXCH_A;
426 case SSL3_ST_CW_KEY_EXCH_A:
427 case SSL3_ST_CW_KEY_EXCH_B:
428 ret = ssl3_send_client_key_exchange(s);
432 * EAY EAY EAY need to check for DH fix cert sent back
435 * For TLS, cert_req is set to 2, so a cert chain of nothing is
436 * sent, but no verify packet is sent
439 * XXX: For now, we do not support client authentication in ECDH
440 * cipher suites with ECDH (rather than ECDSA) certificates. We
441 * need to skip the certificate verify message when client's
442 * ECDH public key is sent inside the client certificate.
444 if (s->s3->tmp.cert_req == 1) {
445 s->state = SSL3_ST_CW_CERT_VRFY_A;
447 s->state = SSL3_ST_CW_CHANGE_A;
449 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
450 s->state = SSL3_ST_CW_CHANGE_A;
456 case SSL3_ST_CW_CERT_VRFY_A:
457 case SSL3_ST_CW_CERT_VRFY_B:
458 ret = ssl3_send_client_verify(s);
461 s->state = SSL3_ST_CW_CHANGE_A;
465 case SSL3_ST_CW_CHANGE_A:
466 case SSL3_ST_CW_CHANGE_B:
467 ret = ssl3_send_change_cipher_spec(s,
469 SSL3_ST_CW_CHANGE_B);
473 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
474 s->state = SSL3_ST_CW_FINISHED_A;
476 if (s->s3->next_proto_neg_seen)
477 s->state = SSL3_ST_CW_NEXT_PROTO_A;
479 s->state = SSL3_ST_CW_FINISHED_A;
483 s->session->cipher = s->s3->tmp.new_cipher;
484 #ifdef OPENSSL_NO_COMP
485 s->session->compress_meth = 0;
487 if (s->s3->tmp.new_compression == NULL)
488 s->session->compress_meth = 0;
490 s->session->compress_meth = s->s3->tmp.new_compression->id;
492 if (!s->method->ssl3_enc->setup_key_block(s)) {
494 s->state = SSL_ST_ERR;
498 if (!s->method->ssl3_enc->change_cipher_state(s,
499 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
502 s->state = SSL_ST_ERR;
508 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
509 case SSL3_ST_CW_NEXT_PROTO_A:
510 case SSL3_ST_CW_NEXT_PROTO_B:
511 ret = ssl3_send_next_proto(s);
514 s->state = SSL3_ST_CW_FINISHED_A;
518 case SSL3_ST_CW_FINISHED_A:
519 case SSL3_ST_CW_FINISHED_B:
520 ret = ssl3_send_finished(s,
521 SSL3_ST_CW_FINISHED_A,
522 SSL3_ST_CW_FINISHED_B,
524 ssl3_enc->client_finished_label,
526 ssl3_enc->client_finished_label_len);
529 s->state = SSL3_ST_CW_FLUSH;
532 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
534 s->s3->tmp.next_state = SSL_ST_OK;
535 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
536 s->state = SSL_ST_OK;
537 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
538 s->s3->delay_buf_pop_ret = 0;
541 #ifndef OPENSSL_NO_TLSEXT
543 * Allow NewSessionTicket if ticket expected
545 if (s->tlsext_ticket_expected)
546 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
550 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
555 #ifndef OPENSSL_NO_TLSEXT
556 case SSL3_ST_CR_SESSION_TICKET_A:
557 case SSL3_ST_CR_SESSION_TICKET_B:
558 ret = ssl3_get_new_session_ticket(s);
561 s->state = SSL3_ST_CR_FINISHED_A;
565 case SSL3_ST_CR_CERT_STATUS_A:
566 case SSL3_ST_CR_CERT_STATUS_B:
567 ret = ssl3_get_cert_status(s);
570 s->state = SSL3_ST_CR_KEY_EXCH_A;
575 case SSL3_ST_CR_FINISHED_A:
576 case SSL3_ST_CR_FINISHED_B:
577 if (!s->s3->change_cipher_spec)
578 s->s3->flags |= SSL3_FLAGS_CCS_OK;
579 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
580 SSL3_ST_CR_FINISHED_B);
585 s->state = SSL3_ST_CW_CHANGE_A;
587 s->state = SSL_ST_OK;
591 case SSL3_ST_CW_FLUSH:
592 s->rwstate = SSL_WRITING;
593 if (BIO_flush(s->wbio) <= 0) {
597 s->rwstate = SSL_NOTHING;
598 s->state = s->s3->tmp.next_state;
602 /* clean a few things up */
603 ssl3_cleanup_key_block(s);
604 BUF_MEM_free(s->init_buf);
608 * If we are not 'joining' the last two packets, remove the
611 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
612 ssl_free_wbio_buffer(s);
613 /* else do it later in ssl3_write */
619 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
621 s->ctx->stats.sess_hit++;
625 s->handshake_func = ssl3_connect;
626 s->ctx->stats.sess_connect_good++;
629 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
636 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
642 /* did we do anything */
643 if (!s->s3->tmp.reuse_message && !skip) {
645 if ((ret = BIO_flush(s->wbio)) <= 0)
649 if ((cb != NULL) && (s->state != state)) {
650 new_state = s->state;
652 cb(s, SSL_CB_CONNECT_LOOP, 1);
653 s->state = new_state;
662 cb(s, SSL_CB_CONNECT_EXIT, ret);
667 * Work out what version we should be using for the initial ClientHello if
668 * the version is currently set to (D)TLS_ANY_VERSION.
669 * Returns 1 on success
672 static int ssl_set_version(SSL *s)
674 unsigned long mask, options = s->options;
676 if (s->method->version == TLS_ANY_VERSION) {
678 * SSL_OP_NO_X disables all protocols above X *if* there are
679 * some protocols below X enabled. This is required in order
680 * to maintain "version capability" vector contiguous. So
681 * that if application wants to disable TLS1.0 in favour of
682 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
683 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
685 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
686 #if !defined(OPENSSL_NO_SSL3)
690 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
691 if (options & SSL_OP_NO_TLSv1_2) {
692 if ((options & mask) != mask) {
693 s->version = TLS1_1_VERSION;
695 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
699 s->version = TLS1_2_VERSION;
702 if ((options & mask) == mask) {
703 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
706 s->version = TLS1_1_VERSION;
709 mask &= ~SSL_OP_NO_TLSv1_1;
710 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
711 s->version = TLS1_VERSION;
712 mask &= ~SSL_OP_NO_TLSv1;
713 #if !defined(OPENSSL_NO_SSL3)
714 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
715 s->version = SSL3_VERSION;
718 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
719 SSLerr(SSL_F_SSL_SET_VERSION,
720 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
724 if (s->version == SSL3_VERSION && FIPS_mode()) {
725 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
729 } else if (s->method->version == DTLS_ANY_VERSION) {
730 /* Determine which DTLS version to use */
731 /* If DTLS 1.2 disabled correct the version number */
732 if (options & SSL_OP_NO_DTLSv1_2) {
733 if (tls1_suiteb(s)) {
734 SSLerr(SSL_F_SSL_SET_VERSION,
735 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
739 * Disabling all versions is silly: return an error.
741 if (options & SSL_OP_NO_DTLSv1) {
742 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
746 * Update method so we don't use any DTLS 1.2 features.
748 s->method = DTLSv1_client_method();
749 s->version = DTLS1_VERSION;
752 * We only support one version: update method
754 if (options & SSL_OP_NO_DTLSv1)
755 s->method = DTLSv1_2_client_method();
756 s->version = DTLS1_2_VERSION;
760 s->client_version = s->version;
765 int ssl3_client_hello(SSL *s)
768 unsigned char *p, *d;
772 #ifndef OPENSSL_NO_COMP
777 buf = (unsigned char *)s->init_buf->data;
778 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
779 SSL_SESSION *sess = s->session;
781 /* Work out what SSL/TLS/DTLS version to use */
782 if (ssl_set_version(s) == 0)
785 if ((sess == NULL) || (sess->ssl_version != s->version) ||
786 #ifdef OPENSSL_NO_TLSEXT
787 !sess->session_id_length ||
790 * In the case of EAP-FAST, we can have a pre-shared
791 * "ticket" without a session ID.
793 (!sess->session_id_length && !sess->tlsext_tick) ||
795 (sess->not_resumable)) {
796 if (!ssl_get_new_session(s, 0))
799 /* else use the pre-loaded session */
801 p = s->s3->client_random;
804 * for DTLS if client_random is initialized, reuse it, we are
805 * required to use same upon reply to HelloVerify
807 if (SSL_IS_DTLS(s)) {
810 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
819 if (i && ssl_fill_hello_random(s, 0, p,
820 sizeof(s->s3->client_random)) <= 0)
823 /* Do the message type and length last */
824 d = p = ssl_handshake_start(s);
827 * version indicates the negotiated version: for example from
828 * an SSLv2/v3 compatible client hello). The client_version
829 * field is the maximum version we permit and it is also
830 * used in RSA encrypted premaster secrets. Some servers can
831 * choke if we initially report a higher version then
832 * renegotiate to a lower one in the premaster secret. This
833 * didn't happen with TLS 1.0 as most servers supported it
834 * but it can with TLS 1.1 or later if the server only supports
837 * Possible scenario with previous logic:
838 * 1. Client hello indicates TLS 1.2
839 * 2. Server hello says TLS 1.0
840 * 3. RSA encrypted premaster secret uses 1.2.
841 * 4. Handhaked proceeds using TLS 1.0.
842 * 5. Server sends hello request to renegotiate.
843 * 6. Client hello indicates TLS v1.0 as we now
844 * know that is maximum server supports.
845 * 7. Server chokes on RSA encrypted premaster secret
846 * containing version 1.0.
848 * For interoperability it should be OK to always use the
849 * maximum version we support in client hello and then rely
850 * on the checking of version to ensure the servers isn't
851 * being inconsistent: for example initially negotiating with
852 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
853 * client_version in client hello and not resetting it to
854 * the negotiated version.
856 *(p++) = s->client_version >> 8;
857 *(p++) = s->client_version & 0xff;
860 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
861 p += SSL3_RANDOM_SIZE;
867 i = s->session->session_id_length;
870 if (i > (int)sizeof(s->session->session_id)) {
871 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
874 memcpy(p, s->session->session_id, i);
878 /* cookie stuff for DTLS */
879 if (SSL_IS_DTLS(s)) {
880 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
881 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
884 *(p++) = s->d1->cookie_len;
885 memcpy(p, s->d1->cookie, s->d1->cookie_len);
886 p += s->d1->cookie_len;
889 /* Ciphers supported */
890 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
892 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
895 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
897 * Some servers hang if client hello > 256 bytes as hack workaround
898 * chop number of supported ciphers to keep it well below this if we
901 if (TLS1_get_version(s) >= TLS1_2_VERSION
902 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
903 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
909 #ifdef OPENSSL_NO_COMP
913 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
916 j = sk_SSL_COMP_num(s->ctx->comp_methods);
918 for (i = 0; i < j; i++) {
919 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
923 *(p++) = 0; /* Add the NULL method */
925 #ifndef OPENSSL_NO_TLSEXT
927 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
928 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
932 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
934 ssl3_send_alert(s, SSL3_AL_FATAL, al);
935 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
941 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
942 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
943 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
946 s->state = SSL3_ST_CW_CLNT_HELLO_B;
949 /* SSL3_ST_CW_CLNT_HELLO_B */
950 return ssl_do_write(s);
952 s->state = SSL_ST_ERR;
956 int ssl3_get_server_hello(SSL *s)
958 STACK_OF(SSL_CIPHER) *sk;
960 unsigned char *p, *d;
961 int i, al = SSL_AD_INTERNAL_ERROR, ok;
964 #ifndef OPENSSL_NO_COMP
968 * Hello verify request and/or server hello version may not match so set
969 * first packet if we're negotiating version.
974 n = s->method->ssl_get_message(s,
975 SSL3_ST_CR_SRVR_HELLO_A,
976 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
981 if (SSL_IS_DTLS(s)) {
983 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
984 if (s->d1->send_cookie == 0) {
985 s->s3->tmp.reuse_message = 1;
987 } else { /* already sent a cookie */
989 al = SSL_AD_UNEXPECTED_MESSAGE;
990 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
996 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
997 al = SSL_AD_UNEXPECTED_MESSAGE;
998 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
1002 d = p = (unsigned char *)s->init_msg;
1004 if (s->method->version == TLS_ANY_VERSION) {
1005 int sversion = (p[0] << 8) | p[1];
1007 #if TLS_MAX_VERSION != TLS1_2_VERSION
1008 #error Code needs updating for new TLS version
1010 #ifndef OPENSSL_NO_SSL3
1011 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1013 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1014 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1015 al = SSL_AD_PROTOCOL_VERSION;
1018 s->method = SSLv3_client_method();
1021 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1022 s->method = TLSv1_client_method();
1023 } else if ((sversion == TLS1_1_VERSION) &&
1024 !(s->options & SSL_OP_NO_TLSv1_1)) {
1025 s->method = TLSv1_1_client_method();
1026 } else if ((sversion == TLS1_2_VERSION) &&
1027 !(s->options & SSL_OP_NO_TLSv1_2)) {
1028 s->method = TLSv1_2_client_method();
1030 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1031 al = SSL_AD_PROTOCOL_VERSION;
1034 s->session->ssl_version = s->version = s->method->version;
1036 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1037 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1038 al = SSL_AD_PROTOCOL_VERSION;
1041 } else if (s->method->version == DTLS_ANY_VERSION) {
1042 /* Work out correct protocol version to use */
1043 int hversion = (p[0] << 8) | p[1];
1044 int options = s->options;
1045 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1046 s->method = DTLSv1_2_client_method();
1047 else if (tls1_suiteb(s)) {
1048 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1049 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1050 s->version = hversion;
1051 al = SSL_AD_PROTOCOL_VERSION;
1053 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1054 s->method = DTLSv1_client_method();
1056 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1057 s->version = hversion;
1058 al = SSL_AD_PROTOCOL_VERSION;
1061 s->version = s->method->version;
1062 } else if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
1063 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1064 s->version = (s->version & 0xff00) | p[1];
1065 al = SSL_AD_PROTOCOL_VERSION;
1070 /* load the server hello data */
1071 /* load the server random */
1072 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
1073 p += SSL3_RANDOM_SIZE;
1077 /* get the session-id */
1080 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
1081 al = SSL_AD_ILLEGAL_PARAMETER;
1082 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1085 #ifndef OPENSSL_NO_TLSEXT
1087 * Check if we can resume the session based on external pre-shared secret.
1088 * EAP-FAST (RFC 4851) supports two types of session resumption.
1089 * Resumption based on server-side state works with session IDs.
1090 * Resumption based on pre-shared Protected Access Credentials (PACs)
1091 * works by overriding the SessionTicket extension at the application
1092 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1093 * servers would honour the session ID.) Therefore, the session ID alone
1094 * is not a reliable indicator of session resumption, so we first check if
1095 * we can resume, and later peek at the next handshake message to see if the
1096 * server wants to resume.
1098 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1099 s->session->tlsext_tick) {
1100 SSL_CIPHER *pref_cipher = NULL;
1101 s->session->master_key_length = sizeof(s->session->master_key);
1102 if (s->tls_session_secret_cb(s, s->session->master_key,
1103 &s->session->master_key_length,
1105 s->tls_session_secret_cb_arg)) {
1106 s->session->cipher = pref_cipher ?
1107 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1109 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1110 al = SSL_AD_INTERNAL_ERROR;
1114 #endif /* OPENSSL_NO_TLSEXT */
1116 if (j != 0 && j == s->session->session_id_length
1117 && memcmp(p, s->session->session_id, j) == 0) {
1118 if (s->sid_ctx_length != s->session->sid_ctx_length
1119 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1120 /* actually a client application bug */
1121 al = SSL_AD_ILLEGAL_PARAMETER;
1122 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1123 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1129 * If we were trying for session-id reuse but the server
1130 * didn't echo the ID, make a new SSL_SESSION.
1131 * In the case of EAP-FAST and PAC, we do not send a session ID,
1132 * so the PAC-based session secret is always preserved. It'll be
1133 * overwritten if the server refuses resumption.
1135 if (s->session->session_id_length > 0) {
1136 if (!ssl_get_new_session(s, 0)) {
1140 s->session->session_id_length = j;
1141 memcpy(s->session->session_id, p, j); /* j could be 0 */
1144 c = ssl_get_cipher_by_char(s, p);
1146 /* unknown cipher */
1147 al = SSL_AD_ILLEGAL_PARAMETER;
1148 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1151 /* Set version disabled mask now we know version */
1152 if (!SSL_USE_TLS1_2_CIPHERS(s))
1153 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1155 s->s3->tmp.mask_ssl = 0;
1157 * If it is a disabled cipher we didn't send it in client hello, so
1160 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1161 al = SSL_AD_ILLEGAL_PARAMETER;
1162 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1165 p += ssl_put_cipher_by_char(s, NULL, NULL);
1167 sk = ssl_get_ciphers_by_id(s);
1168 i = sk_SSL_CIPHER_find(sk, c);
1170 /* we did not say we would use this cipher */
1171 al = SSL_AD_ILLEGAL_PARAMETER;
1172 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1177 * Depending on the session caching (internal/external), the cipher
1178 * and/or cipher_id values may not be set. Make sure that cipher_id is
1179 * set and use it for comparison.
1181 if (s->session->cipher)
1182 s->session->cipher_id = s->session->cipher->id;
1183 if (s->hit && (s->session->cipher_id != c->id)) {
1184 al = SSL_AD_ILLEGAL_PARAMETER;
1185 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1186 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1189 s->s3->tmp.new_cipher = c;
1191 * Don't digest cached records if no sigalgs: we may need them for client
1194 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1196 /* lets get the compression algorithm */
1198 #ifdef OPENSSL_NO_COMP
1200 al = SSL_AD_ILLEGAL_PARAMETER;
1201 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1202 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1206 * If compression is disabled we'd better not try to resume a session
1207 * using compression.
1209 if (s->session->compress_meth != 0) {
1210 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1215 if (s->hit && j != s->session->compress_meth) {
1216 al = SSL_AD_ILLEGAL_PARAMETER;
1217 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1218 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1223 else if (!ssl_allow_compression(s)) {
1224 al = SSL_AD_ILLEGAL_PARAMETER;
1225 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1228 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1230 if ((j != 0) && (comp == NULL)) {
1231 al = SSL_AD_ILLEGAL_PARAMETER;
1232 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1233 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1236 s->s3->tmp.new_compression = comp;
1240 #ifndef OPENSSL_NO_TLSEXT
1241 /* TLS extensions */
1242 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1243 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1249 /* wrong packet length */
1250 al = SSL_AD_DECODE_ERROR;
1251 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1257 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1259 s->state = SSL_ST_ERR;
1263 int ssl3_get_server_certificate(SSL *s)
1265 int al, i, ok, ret = -1, exp_idx;
1266 unsigned long n, nc, llen, l;
1268 const unsigned char *q, *p;
1270 STACK_OF(X509) *sk = NULL;
1272 EVP_PKEY *pkey = NULL;
1274 n = s->method->ssl_get_message(s,
1277 -1, s->max_cert_list, &ok);
1282 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1283 s->s3->tmp.reuse_message = 1;
1287 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1288 al = SSL_AD_UNEXPECTED_MESSAGE;
1289 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1292 p = d = (unsigned char *)s->init_msg;
1294 if ((sk = sk_X509_new_null()) == NULL) {
1295 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1300 if (llen + 3 != n) {
1301 al = SSL_AD_DECODE_ERROR;
1302 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1305 for (nc = 0; nc < llen;) {
1307 if ((l + nc + 3) > llen) {
1308 al = SSL_AD_DECODE_ERROR;
1309 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1310 SSL_R_CERT_LENGTH_MISMATCH);
1315 x = d2i_X509(NULL, &q, l);
1317 al = SSL_AD_BAD_CERTIFICATE;
1318 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1322 al = SSL_AD_DECODE_ERROR;
1323 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1324 SSL_R_CERT_LENGTH_MISMATCH);
1327 if (!sk_X509_push(sk, x)) {
1328 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1336 i = ssl_verify_cert_chain(s, sk);
1337 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1338 al = ssl_verify_alarm_type(s->verify_result);
1339 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1340 SSL_R_CERTIFICATE_VERIFY_FAILED);
1343 ERR_clear_error(); /* but we keep s->verify_result */
1345 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1346 al = SSL_AD_HANDSHAKE_FAILURE;
1350 sc = ssl_sess_cert_new();
1354 ssl_sess_cert_free(s->session->sess_cert);
1355 s->session->sess_cert = sc;
1357 sc->cert_chain = sk;
1359 * Inconsistency alert: cert_chain does include the peer's certificate,
1360 * which we don't include in s3_srvr.c
1362 x = sk_X509_value(sk, 0);
1365 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1368 pkey = X509_get_pubkey(x);
1370 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1373 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1374 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1378 i = ssl_cert_type(x, pkey);
1382 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1383 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1387 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1388 if (exp_idx >= 0 && i != exp_idx) {
1390 al = SSL_AD_ILLEGAL_PARAMETER;
1391 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1392 SSL_R_WRONG_CERTIFICATE_TYPE);
1395 sc->peer_cert_type = i;
1396 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1398 * Why would the following ever happen? We just created sc a couple
1401 X509_free(sc->peer_pkeys[i].x509);
1402 sc->peer_pkeys[i].x509 = x;
1403 sc->peer_key = &(sc->peer_pkeys[i]);
1405 X509_free(s->session->peer);
1406 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1407 s->session->peer = x;
1408 s->session->verify_result = s->verify_result;
1415 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1417 s->state = SSL_ST_ERR;
1419 EVP_PKEY_free(pkey);
1421 sk_X509_pop_free(sk, X509_free);
1425 int ssl3_get_key_exchange(SSL *s)
1427 #ifndef OPENSSL_NO_RSA
1428 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1431 unsigned char *param, *p;
1433 long i, param_len, n, alg_k, alg_a;
1434 EVP_PKEY *pkey = NULL;
1435 const EVP_MD *md = NULL;
1436 #ifndef OPENSSL_NO_RSA
1439 #ifndef OPENSSL_NO_DH
1442 #ifndef OPENSSL_NO_EC
1443 EC_KEY *ecdh = NULL;
1444 BN_CTX *bn_ctx = NULL;
1445 EC_POINT *srvr_ecpoint = NULL;
1447 int encoded_pt_len = 0;
1450 EVP_MD_CTX_init(&md_ctx);
1453 * use same message size as in ssl3_get_certificate_request() as
1454 * ServerKeyExchange message may be skipped
1456 n = s->method->ssl_get_message(s,
1457 SSL3_ST_CR_KEY_EXCH_A,
1458 SSL3_ST_CR_KEY_EXCH_B,
1459 -1, s->max_cert_list, &ok);
1463 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1465 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1467 * Can't skip server key exchange if this is an ephemeral
1470 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1471 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1472 al = SSL_AD_UNEXPECTED_MESSAGE;
1475 #ifndef OPENSSL_NO_PSK
1477 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1478 * identity hint is sent. Set session->sess_cert anyway to avoid
1481 if (alg_k & SSL_kPSK) {
1482 s->session->sess_cert = ssl_sess_cert_new();
1483 OPENSSL_free(s->ctx->psk_identity_hint);
1484 s->ctx->psk_identity_hint = NULL;
1487 s->s3->tmp.reuse_message = 1;
1491 param = p = (unsigned char *)s->init_msg;
1492 if (s->session->sess_cert != NULL) {
1493 #ifndef OPENSSL_NO_RSA
1494 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1495 s->session->sess_cert->peer_rsa_tmp = NULL;
1497 #ifndef OPENSSL_NO_DH
1498 DH_free(s->session->sess_cert->peer_dh_tmp);
1499 s->session->sess_cert->peer_dh_tmp = NULL;
1501 #ifndef OPENSSL_NO_EC
1502 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1503 s->session->sess_cert->peer_ecdh_tmp = NULL;
1506 s->session->sess_cert = ssl_sess_cert_new();
1509 /* Total length of the parameters including the length prefix */
1512 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1514 al = SSL_AD_DECODE_ERROR;
1516 #ifndef OPENSSL_NO_PSK
1517 if (alg_k & SSL_kPSK) {
1518 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1521 if (param_len > n) {
1522 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1528 * Store PSK identity hint for later use, hint is used in
1529 * ssl3_send_client_key_exchange. Assume that the maximum length of
1530 * a PSK identity hint can be as long as the maximum length of a PSK
1533 if (i > PSK_MAX_IDENTITY_LEN) {
1534 al = SSL_AD_HANDSHAKE_FAILURE;
1535 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1538 if (i > n - param_len) {
1539 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1540 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1546 * If received PSK identity hint contains NULL characters, the hint
1547 * is truncated from the first NULL. p may not be ending with NULL,
1548 * so create a NULL-terminated string.
1550 memcpy(tmp_id_hint, p, i);
1551 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1552 OPENSSL_free(s->ctx->psk_identity_hint);
1553 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1554 if (s->ctx->psk_identity_hint == NULL) {
1555 al = SSL_AD_HANDSHAKE_FAILURE;
1556 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1563 #endif /* !OPENSSL_NO_PSK */
1564 #ifndef OPENSSL_NO_SRP
1565 if (alg_k & SSL_kSRP) {
1567 if (param_len > n) {
1568 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1573 if (i > n - param_len) {
1574 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1579 if ((s->srp_ctx.N = BN_bin2bn(p, i, NULL)) == NULL) {
1580 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1585 if (2 > n - param_len) {
1586 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1593 if (i > n - param_len) {
1594 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1599 if ((s->srp_ctx.g = BN_bin2bn(p, i, NULL)) == NULL) {
1600 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1605 if (1 > n - param_len) {
1606 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1611 i = (unsigned int)(p[0]);
1614 if (i > n - param_len) {
1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1620 if ((s->srp_ctx.s = BN_bin2bn(p, i, NULL)) == NULL) {
1621 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1626 if (2 > n - param_len) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1634 if (i > n - param_len) {
1635 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1640 if ((s->srp_ctx.B = BN_bin2bn(p, i, NULL)) == NULL) {
1641 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1647 if (!srp_verify_server_param(s, &al)) {
1648 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1652 /* We must check if there is a certificate */
1653 # ifndef OPENSSL_NO_RSA
1654 if (alg_a & SSL_aRSA)
1656 X509_get_pubkey(s->session->
1657 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1661 # ifndef OPENSSL_NO_DSA
1662 else if (alg_a & SSL_aDSS)
1664 X509_get_pubkey(s->session->
1665 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1669 #endif /* !OPENSSL_NO_SRP */
1670 #ifndef OPENSSL_NO_RSA
1671 if (alg_k & SSL_kRSA) {
1672 /* Temporary RSA keys only allowed in export ciphersuites */
1673 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1674 al = SSL_AD_UNEXPECTED_MESSAGE;
1675 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1678 if ((rsa = RSA_new()) == NULL) {
1679 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1684 if (param_len > n) {
1685 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1690 if (i > n - param_len) {
1691 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1696 if ((rsa->n = BN_bin2bn(p, i, rsa->n)) == NULL) {
1697 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1702 if (2 > n - param_len) {
1703 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1710 if (i > n - param_len) {
1711 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1716 if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == NULL) {
1717 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1723 /* this should be because we are using an export cipher */
1724 if (alg_a & SSL_aRSA)
1726 X509_get_pubkey(s->session->
1727 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1729 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1732 s->session->sess_cert->peer_rsa_tmp = rsa;
1735 #else /* OPENSSL_NO_RSA */
1738 #ifndef OPENSSL_NO_DH
1739 else if (alg_k & SSL_kDHE) {
1740 if ((dh = DH_new()) == NULL) {
1741 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1746 if (param_len > n) {
1747 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1752 if (i > n - param_len) {
1753 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1758 if ((dh->p = BN_bin2bn(p, i, NULL)) == NULL) {
1759 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1764 if (2 > n - param_len) {
1765 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1772 if (i > n - param_len) {
1773 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1778 if ((dh->g = BN_bin2bn(p, i, NULL)) == NULL) {
1779 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1784 if (2 > n - param_len) {
1785 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1792 if (i > n - param_len) {
1793 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1798 if ((dh->pub_key = BN_bin2bn(p, i, NULL)) == NULL) {
1799 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1805 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1806 al = SSL_AD_HANDSHAKE_FAILURE;
1807 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1810 # ifndef OPENSSL_NO_RSA
1811 if (alg_a & SSL_aRSA)
1813 X509_get_pubkey(s->session->
1814 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1818 # ifndef OPENSSL_NO_DSA
1819 else if (alg_a & SSL_aDSS)
1821 X509_get_pubkey(s->session->
1822 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1825 /* else anonymous DH, so no certificate or pkey. */
1827 s->session->sess_cert->peer_dh_tmp = dh;
1830 #endif /* !OPENSSL_NO_DH */
1832 #ifndef OPENSSL_NO_EC
1833 else if (alg_k & SSL_kECDHE) {
1835 const EC_GROUP *group;
1837 if ((ecdh = EC_KEY_new()) == NULL) {
1838 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1843 * Extract elliptic curve parameters and the server's ephemeral ECDH
1844 * public key. Keep accumulating lengths of various components in
1845 * param_len and make sure it never exceeds n.
1849 * XXX: For now we only support named (not generic) curves and the
1850 * ECParameters in this case is just three bytes. We also need one
1851 * byte for the length of the encoded point
1854 if (param_len > n) {
1855 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1859 * Check curve is one of our preferences, if not server has sent an
1860 * invalid curve. ECParameters is 3 bytes.
1862 if (!tls1_check_curve(s, p, 3)) {
1863 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1867 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1868 al = SSL_AD_INTERNAL_ERROR;
1869 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1870 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1874 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1875 if (ngroup == NULL) {
1876 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1879 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1880 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1883 EC_GROUP_free(ngroup);
1885 group = EC_KEY_get0_group(ecdh);
1887 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1888 (EC_GROUP_get_degree(group) > 163)) {
1889 al = SSL_AD_EXPORT_RESTRICTION;
1890 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1891 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1897 /* Next, get the encoded ECPoint */
1898 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1899 ((bn_ctx = BN_CTX_new()) == NULL)) {
1900 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1904 encoded_pt_len = *p; /* length of encoded point */
1907 if ((encoded_pt_len > n - param_len) ||
1908 (EC_POINT_oct2point(group, srvr_ecpoint,
1909 p, encoded_pt_len, bn_ctx) == 0)) {
1910 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1913 param_len += encoded_pt_len;
1916 p += encoded_pt_len;
1919 * The ECC/TLS specification does not mention the use of DSA to sign
1920 * ECParameters in the server key exchange message. We do support RSA
1924 # ifndef OPENSSL_NO_RSA
1925 else if (alg_a & SSL_aRSA)
1927 X509_get_pubkey(s->session->
1928 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1930 # ifndef OPENSSL_NO_EC
1931 else if (alg_a & SSL_aECDSA)
1933 X509_get_pubkey(s->session->
1934 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1936 /* else anonymous ECDH, so no certificate or pkey. */
1937 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1938 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1940 BN_CTX_free(bn_ctx);
1942 EC_POINT_free(srvr_ecpoint);
1943 srvr_ecpoint = NULL;
1945 al = SSL_AD_UNEXPECTED_MESSAGE;
1946 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1949 #endif /* !OPENSSL_NO_EC */
1951 /* p points to the next byte, there are 'n' bytes left */
1953 /* if it was signed, check the signature */
1955 if (SSL_USE_SIGALGS(s)) {
1958 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1961 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1968 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1976 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1981 j = EVP_PKEY_size(pkey);
1984 * Check signature length. If n is 0 then signature is empty
1986 if ((i != n) || (n > j) || (n <= 0)) {
1987 /* wrong packet length */
1988 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1991 #ifndef OPENSSL_NO_RSA
1992 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1998 for (num = 2; num > 0; num--) {
1999 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2000 EVP_DigestInit_ex(&md_ctx, (num == 2)
2001 ? s->ctx->md5 : s->ctx->sha1, NULL);
2002 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2004 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2006 EVP_DigestUpdate(&md_ctx, param, param_len);
2007 EVP_DigestFinal_ex(&md_ctx, q, &size);
2011 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
2013 al = SSL_AD_DECRYPT_ERROR;
2014 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
2019 al = SSL_AD_DECRYPT_ERROR;
2020 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2026 EVP_VerifyInit_ex(&md_ctx, md, NULL);
2027 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
2029 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
2031 EVP_VerifyUpdate(&md_ctx, param, param_len);
2032 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
2034 al = SSL_AD_DECRYPT_ERROR;
2035 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2040 /* aNULL, aSRP or kPSK do not need public keys */
2041 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
2042 /* Might be wrong key type, check it */
2043 if (ssl3_check_cert_and_algorithm(s))
2044 /* Otherwise this shouldn't happen */
2045 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2048 /* still data left over */
2050 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2054 EVP_PKEY_free(pkey);
2055 EVP_MD_CTX_cleanup(&md_ctx);
2058 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2060 EVP_PKEY_free(pkey);
2061 #ifndef OPENSSL_NO_RSA
2064 #ifndef OPENSSL_NO_DH
2067 #ifndef OPENSSL_NO_EC
2068 BN_CTX_free(bn_ctx);
2069 EC_POINT_free(srvr_ecpoint);
2072 EVP_MD_CTX_cleanup(&md_ctx);
2073 s->state = SSL_ST_ERR;
2077 int ssl3_get_certificate_request(SSL *s)
2080 unsigned long n, nc, l;
2081 unsigned int llen, ctype_num, i;
2082 X509_NAME *xn = NULL;
2083 const unsigned char *p, *q;
2085 STACK_OF(X509_NAME) *ca_sk = NULL;
2087 n = s->method->ssl_get_message(s,
2088 SSL3_ST_CR_CERT_REQ_A,
2089 SSL3_ST_CR_CERT_REQ_B,
2090 -1, s->max_cert_list, &ok);
2095 s->s3->tmp.cert_req = 0;
2097 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2098 s->s3->tmp.reuse_message = 1;
2100 * If we get here we don't need any cached handshake records as we
2101 * wont be doing client auth.
2103 if (s->s3->handshake_buffer) {
2104 if (!ssl3_digest_cached_records(s))
2110 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2111 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2112 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2116 /* TLS does not like anon-DH with client cert */
2117 if (s->version > SSL3_VERSION) {
2118 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2119 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2120 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2121 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2126 p = d = (unsigned char *)s->init_msg;
2128 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2129 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2133 /* get the certificate types */
2135 OPENSSL_free(s->cert->ctypes);
2136 s->cert->ctypes = NULL;
2137 if (ctype_num > SSL3_CT_NUMBER) {
2138 /* If we exceed static buffer copy all to cert structure */
2139 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2140 if (s->cert->ctypes == NULL) {
2141 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2144 memcpy(s->cert->ctypes, p, ctype_num);
2145 s->cert->ctype_num = (size_t)ctype_num;
2146 ctype_num = SSL3_CT_NUMBER;
2148 for (i = 0; i < ctype_num; i++)
2149 s->s3->tmp.ctype[i] = p[i];
2151 if (SSL_USE_SIGALGS(s)) {
2154 * Check we have enough room for signature algorithms and following
2157 if ((unsigned long)(p - d + llen + 2) > n) {
2158 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2159 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2160 SSL_R_DATA_LENGTH_TOO_LONG);
2163 /* Clear certificate digests and validity flags */
2164 for (i = 0; i < SSL_PKEY_NUM; i++) {
2165 s->s3->tmp.md[i] = NULL;
2166 s->s3->tmp.valid_flags[i] = 0;
2168 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2169 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2170 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2171 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2174 if (!tls1_process_sigalgs(s)) {
2175 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2176 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2182 /* get the CA RDNs */
2185 if ((unsigned long)(p - d + llen) != n) {
2186 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2187 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2191 for (nc = 0; nc < llen;) {
2193 if ((l + nc + 2) > llen) {
2194 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2195 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2201 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2202 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2203 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2208 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2209 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2210 SSL_R_CA_DN_LENGTH_MISMATCH);
2213 if (!sk_X509_NAME_push(ca_sk, xn)) {
2214 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2222 /* we should setup a certificate to return.... */
2223 s->s3->tmp.cert_req = 1;
2224 s->s3->tmp.ctype_num = ctype_num;
2225 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2226 s->s3->tmp.ca_names = ca_sk;
2232 s->state = SSL_ST_ERR;
2234 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2238 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2240 return (X509_NAME_cmp(*a, *b));
2243 #ifndef OPENSSL_NO_TLSEXT
2244 int ssl3_get_new_session_ticket(SSL *s)
2246 int ok, al, ret = 0, ticklen;
2248 const unsigned char *p;
2251 n = s->method->ssl_get_message(s,
2252 SSL3_ST_CR_SESSION_TICKET_A,
2253 SSL3_ST_CR_SESSION_TICKET_B,
2254 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2260 /* need at least ticket_lifetime_hint + ticket length */
2261 al = SSL_AD_DECODE_ERROR;
2262 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2266 p = d = (unsigned char *)s->init_msg;
2267 n2l(p, s->session->tlsext_tick_lifetime_hint);
2269 /* ticket_lifetime_hint + ticket_length + ticket */
2270 if (ticklen + 6 != n) {
2271 al = SSL_AD_DECODE_ERROR;
2272 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2275 OPENSSL_free(s->session->tlsext_tick);
2276 s->session->tlsext_ticklen = 0;
2277 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2278 if (!s->session->tlsext_tick) {
2279 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2282 memcpy(s->session->tlsext_tick, p, ticklen);
2283 s->session->tlsext_ticklen = ticklen;
2285 * There are two ways to detect a resumed ticket session. One is to set
2286 * an appropriate session ID and then the server must return a match in
2287 * ServerHello. This allows the normal client session ID matching to work
2288 * and we know much earlier that the ticket has been accepted. The
2289 * other way is to set zero length session ID when the ticket is
2290 * presented and rely on the handshake to determine session resumption.
2291 * We choose the former approach because this fits in with assumptions
2292 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2293 * SHA256 is disabled) hash of the ticket.
2295 EVP_Digest(p, ticklen,
2296 s->session->session_id, &s->session->session_id_length,
2297 EVP_sha256(), NULL);
2301 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2303 s->state = SSL_ST_ERR;
2307 int ssl3_get_cert_status(SSL *s)
2310 unsigned long resplen, n;
2311 const unsigned char *p;
2313 n = s->method->ssl_get_message(s,
2314 SSL3_ST_CR_CERT_STATUS_A,
2315 SSL3_ST_CR_CERT_STATUS_B,
2316 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2321 /* need at least status type + length */
2322 al = SSL_AD_DECODE_ERROR;
2323 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2326 p = (unsigned char *)s->init_msg;
2327 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2328 al = SSL_AD_DECODE_ERROR;
2329 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2333 if (resplen + 4 != n) {
2334 al = SSL_AD_DECODE_ERROR;
2335 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2338 OPENSSL_free(s->tlsext_ocsp_resp);
2339 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2340 if (!s->tlsext_ocsp_resp) {
2341 al = SSL_AD_INTERNAL_ERROR;
2342 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2345 s->tlsext_ocsp_resplen = resplen;
2346 if (s->ctx->tlsext_status_cb) {
2348 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2350 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2351 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2355 al = SSL_AD_INTERNAL_ERROR;
2356 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2362 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2363 s->state = SSL_ST_ERR;
2368 int ssl3_get_server_done(SSL *s)
2373 /* Second to last param should be very small, like 0 :-) */
2374 n = s->method->ssl_get_message(s,
2375 SSL3_ST_CR_SRVR_DONE_A,
2376 SSL3_ST_CR_SRVR_DONE_B,
2377 SSL3_MT_SERVER_DONE, 30, &ok);
2382 /* should contain no data */
2383 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2384 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2385 s->state = SSL_ST_ERR;
2392 int ssl3_send_client_key_exchange(SSL *s)
2396 unsigned long alg_k;
2397 #ifndef OPENSSL_NO_RSA
2399 EVP_PKEY *pkey = NULL;
2401 #ifndef OPENSSL_NO_EC
2402 EC_KEY *clnt_ecdh = NULL;
2403 const EC_POINT *srvr_ecpoint = NULL;
2404 EVP_PKEY *srvr_pub_pkey = NULL;
2405 unsigned char *encodedPoint = NULL;
2406 int encoded_pt_len = 0;
2407 BN_CTX *bn_ctx = NULL;
2409 unsigned char *pms = NULL;
2412 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2413 p = ssl_handshake_start(s);
2415 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2417 /* Fool emacs indentation */
2420 #ifndef OPENSSL_NO_RSA
2421 else if (alg_k & SSL_kRSA) {
2423 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2424 pms = OPENSSL_malloc(pmslen);
2428 if (s->session->sess_cert == NULL) {
2430 * We should always have a server certificate with SSL_kRSA.
2432 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2433 ERR_R_INTERNAL_ERROR);
2437 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2438 rsa = s->session->sess_cert->peer_rsa_tmp;
2441 X509_get_pubkey(s->session->
2442 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2444 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2445 || (pkey->pkey.rsa == NULL)) {
2446 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2447 ERR_R_INTERNAL_ERROR);
2450 rsa = pkey->pkey.rsa;
2451 EVP_PKEY_free(pkey);
2454 pms[0] = s->client_version >> 8;
2455 pms[1] = s->client_version & 0xff;
2456 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2460 /* Fix buf for TLS and beyond */
2461 if (s->version > SSL3_VERSION)
2463 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2465 if (s->options & SSL_OP_PKCS1_CHECK_1)
2467 if (s->options & SSL_OP_PKCS1_CHECK_2)
2471 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2472 SSL_R_BAD_RSA_ENCRYPT);
2476 /* Fix buf for TLS and beyond */
2477 if (s->version > SSL3_VERSION) {
2483 #ifndef OPENSSL_NO_DH
2484 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2485 DH *dh_srvr, *dh_clnt;
2486 SESS_CERT *scert = s->session->sess_cert;
2488 if (scert == NULL) {
2489 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2490 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2491 SSL_R_UNEXPECTED_MESSAGE);
2495 if (scert->peer_dh_tmp != NULL)
2496 dh_srvr = scert->peer_dh_tmp;
2498 /* we get them from the cert */
2499 int idx = scert->peer_cert_type;
2500 EVP_PKEY *spkey = NULL;
2503 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2505 dh_srvr = EVP_PKEY_get1_DH(spkey);
2506 EVP_PKEY_free(spkey);
2508 if (dh_srvr == NULL) {
2509 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2510 ERR_R_INTERNAL_ERROR);
2514 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2515 /* Use client certificate key */
2516 EVP_PKEY *clkey = s->cert->key->privatekey;
2519 dh_clnt = EVP_PKEY_get1_DH(clkey);
2520 if (dh_clnt == NULL) {
2521 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2522 ERR_R_INTERNAL_ERROR);
2526 /* generate a new random key */
2527 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2528 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2531 if (!DH_generate_key(dh_clnt)) {
2532 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2538 pmslen = DH_size(dh_clnt);
2539 pms = OPENSSL_malloc(pmslen);
2544 * use the 'p' output buffer for the DH key, but make sure to
2545 * clear it out afterwards
2548 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2549 if (scert->peer_dh_tmp == NULL)
2553 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2559 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2562 /* send off the data */
2563 n = BN_num_bytes(dh_clnt->pub_key);
2565 BN_bn2bin(dh_clnt->pub_key, p);
2571 /* perhaps clean things up a bit EAY EAY EAY EAY */
2575 #ifndef OPENSSL_NO_EC
2576 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2577 const EC_GROUP *srvr_group = NULL;
2579 int ecdh_clnt_cert = 0;
2582 if (s->session->sess_cert == NULL) {
2583 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2584 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2585 SSL_R_UNEXPECTED_MESSAGE);
2590 * Did we send out the client's ECDH share for use in premaster
2591 * computation as part of client certificate? If so, set
2592 * ecdh_clnt_cert to 1.
2594 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2596 * XXX: For now, we do not support client
2597 * authentication using ECDH certificates.
2598 * To add such support, one needs to add
2599 * code that checks for appropriate
2600 * conditions and sets ecdh_clnt_cert to 1.
2601 * For example, the cert have an ECC
2602 * key on the same curve as the server's
2603 * and the key should be authorized for
2606 * One also needs to add code in ssl3_connect
2607 * to skip sending the certificate verify
2610 * if ((s->cert->key->privatekey != NULL) &&
2611 * (s->cert->key->privatekey->type ==
2612 * EVP_PKEY_EC) && ...)
2613 * ecdh_clnt_cert = 1;
2617 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2618 tkey = s->session->sess_cert->peer_ecdh_tmp;
2620 /* Get the Server Public Key from Cert */
2622 X509_get_pubkey(s->session->
2623 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2624 if ((srvr_pub_pkey == NULL)
2625 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2626 || (srvr_pub_pkey->pkey.ec == NULL)) {
2627 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2628 ERR_R_INTERNAL_ERROR);
2632 tkey = srvr_pub_pkey->pkey.ec;
2635 srvr_group = EC_KEY_get0_group(tkey);
2636 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2638 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2639 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2640 ERR_R_INTERNAL_ERROR);
2644 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2645 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2646 ERR_R_MALLOC_FAILURE);
2650 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2651 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2654 if (ecdh_clnt_cert) {
2656 * Reuse key info from our certificate We only need our
2657 * private key to perform the ECDH computation.
2659 const BIGNUM *priv_key;
2660 tkey = s->cert->key->privatekey->pkey.ec;
2661 priv_key = EC_KEY_get0_private_key(tkey);
2662 if (priv_key == NULL) {
2663 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2664 ERR_R_MALLOC_FAILURE);
2667 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2668 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2672 /* Generate a new ECDH key pair */
2673 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2674 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2681 * use the 'p' output buffer for the ECDH key, but make sure to
2682 * clear it out afterwards
2685 field_size = EC_GROUP_get_degree(srvr_group);
2686 if (field_size <= 0) {
2687 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2690 pmslen = (field_size + 7) / 8;
2691 pms = OPENSSL_malloc(pmslen);
2694 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2695 if (n <= 0 || pmslen != (size_t)n) {
2696 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2700 if (ecdh_clnt_cert) {
2701 /* Send empty client key exch message */
2705 * First check the size of encoding and allocate memory
2709 EC_POINT_point2oct(srvr_group,
2710 EC_KEY_get0_public_key(clnt_ecdh),
2711 POINT_CONVERSION_UNCOMPRESSED,
2714 encodedPoint = (unsigned char *)
2715 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2716 bn_ctx = BN_CTX_new();
2717 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2718 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2719 ERR_R_MALLOC_FAILURE);
2723 /* Encode the public key */
2724 n = EC_POINT_point2oct(srvr_group,
2725 EC_KEY_get0_public_key(clnt_ecdh),
2726 POINT_CONVERSION_UNCOMPRESSED,
2727 encodedPoint, encoded_pt_len, bn_ctx);
2729 *p = n; /* length of encoded point */
2730 /* Encoded point will be copied here */
2732 /* copy the point */
2733 memcpy(p, encodedPoint, n);
2734 /* increment n to account for length field */
2738 /* Free allocated memory */
2739 BN_CTX_free(bn_ctx);
2740 OPENSSL_free(encodedPoint);
2741 EC_KEY_free(clnt_ecdh);
2742 EVP_PKEY_free(srvr_pub_pkey);
2744 #endif /* !OPENSSL_NO_EC */
2745 else if (alg_k & SSL_kGOST) {
2746 /* GOST key exchange message creation */
2747 EVP_PKEY_CTX *pkey_ctx;
2750 unsigned int md_len;
2752 unsigned char shared_ukm[32], tmp[256];
2753 EVP_MD_CTX *ukm_hash;
2757 pms = OPENSSL_malloc(pmslen);
2762 * Get server sertificate PKEY and create ctx from it
2766 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2770 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2772 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2773 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2777 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2778 X509_get_pubkey(peer_cert), NULL);
2780 * If we have send a certificate, and certificate key
2782 * * parameters match those of server certificate, use
2783 * certificate key for key exchange
2786 /* Otherwise, generate ephemeral key pair */
2788 EVP_PKEY_encrypt_init(pkey_ctx);
2789 /* Generate session key */
2790 if (RAND_bytes(pms, pmslen) <= 0) {
2791 EVP_PKEY_CTX_free(pkey_ctx);
2792 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2793 ERR_R_INTERNAL_ERROR);
2797 * If we have client certificate, use its secret as peer key
2799 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2800 if (EVP_PKEY_derive_set_peer
2801 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2803 * If there was an error - just ignore it. Ephemeral key
2810 * Compute shared IV and store it in algorithm-specific context
2813 ukm_hash = EVP_MD_CTX_create();
2814 EVP_DigestInit(ukm_hash,
2815 EVP_get_digestbynid(NID_id_GostR3411_94));
2816 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2818 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2820 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2821 EVP_MD_CTX_destroy(ukm_hash);
2822 if (EVP_PKEY_CTX_ctrl
2823 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2825 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2829 /* Make GOST keytransport blob message */
2831 * Encapsulate it into sequence
2833 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2835 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2836 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2840 if (msglen >= 0x80) {
2842 *(p++) = msglen & 0xff;
2845 *(p++) = msglen & 0xff;
2848 memcpy(p, tmp, msglen);
2849 /* Check if pubkey from client certificate was used */
2850 if (EVP_PKEY_CTX_ctrl
2851 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2852 /* Set flag "skip certificate verify" */
2853 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2855 EVP_PKEY_CTX_free(pkey_ctx);
2856 EVP_PKEY_free(pub_key);
2859 #ifndef OPENSSL_NO_SRP
2860 else if (alg_k & SSL_kSRP) {
2861 if (s->srp_ctx.A != NULL) {
2862 /* send off the data */
2863 n = BN_num_bytes(s->srp_ctx.A);
2865 BN_bn2bin(s->srp_ctx.A, p);
2868 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2869 ERR_R_INTERNAL_ERROR);
2872 OPENSSL_free(s->session->srp_username);
2873 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2874 if (s->session->srp_username == NULL) {
2875 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2876 ERR_R_MALLOC_FAILURE);
2881 #ifndef OPENSSL_NO_PSK
2882 else if (alg_k & SSL_kPSK) {
2884 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2885 * \0-terminated identity. The last byte is for us for simulating
2888 char identity[PSK_MAX_IDENTITY_LEN + 2];
2889 size_t identity_len;
2890 unsigned char *t = NULL;
2891 unsigned int psk_len = 0;
2895 if (s->psk_client_callback == NULL) {
2896 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2897 SSL_R_PSK_NO_CLIENT_CB);
2901 memset(identity, 0, sizeof(identity));
2902 /* Allocate maximum size buffer */
2903 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2904 pms = OPENSSL_malloc(pmslen);
2908 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2909 identity, sizeof(identity) - 1,
2911 if (psk_len > PSK_MAX_PSK_LEN) {
2912 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2913 ERR_R_INTERNAL_ERROR);
2915 } else if (psk_len == 0) {
2916 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2917 SSL_R_PSK_IDENTITY_NOT_FOUND);
2920 /* Change pmslen to real length */
2921 pmslen = 2 + psk_len + 2 + psk_len;
2922 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2923 identity_len = strlen(identity);
2924 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2925 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2926 ERR_R_INTERNAL_ERROR);
2929 /* create PSK pre_master_secret */
2931 memmove(pms + psk_len + 4, pms, psk_len);
2933 memset(t, 0, psk_len);
2937 OPENSSL_free(s->session->psk_identity_hint);
2938 s->session->psk_identity_hint =
2939 BUF_strdup(s->ctx->psk_identity_hint);
2940 if (s->ctx->psk_identity_hint != NULL
2941 && s->session->psk_identity_hint == NULL) {
2942 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2943 ERR_R_MALLOC_FAILURE);
2947 OPENSSL_free(s->session->psk_identity);
2948 s->session->psk_identity = BUF_strdup(identity);
2949 if (s->session->psk_identity == NULL) {
2950 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2951 ERR_R_MALLOC_FAILURE);
2955 s2n(identity_len, p);
2956 memcpy(p, identity, identity_len);
2957 n = 2 + identity_len;
2960 OPENSSL_cleanse(identity, sizeof(identity));
2962 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2968 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2969 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2973 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2974 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2975 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2979 s->state = SSL3_ST_CW_KEY_EXCH_B;
2982 /* SSL3_ST_CW_KEY_EXCH_B */
2983 n = ssl_do_write(s);
2984 #ifndef OPENSSL_NO_SRP
2986 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2988 * If everything written generate master key: no need to save PMS as
2989 * SRP_generate_client_master_secret generates it internally.
2992 if ((s->session->master_key_length =
2993 SRP_generate_client_master_secret(s,
2994 s->session->master_key)) <
2996 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2997 ERR_R_INTERNAL_ERROR);
3003 /* If we haven't written everything save PMS */
3005 s->s3->tmp.pms = pms;
3006 s->s3->tmp.pmslen = pmslen;
3008 /* If we don't have a PMS restore */
3010 pms = s->s3->tmp.pms;
3011 pmslen = s->s3->tmp.pmslen;
3014 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3015 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3018 s->session->master_key_length =
3019 s->method->ssl3_enc->generate_master_secret(s,
3021 session->master_key,
3023 OPENSSL_clear_free(pms, pmslen);
3024 s->s3->tmp.pms = NULL;
3025 if (s->session->master_key_length < 0) {
3026 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3027 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3034 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3036 OPENSSL_clear_free(pms, pmslen);
3037 s->s3->tmp.pms = NULL;
3038 #ifndef OPENSSL_NO_EC
3039 BN_CTX_free(bn_ctx);
3040 OPENSSL_free(encodedPoint);
3041 EC_KEY_free(clnt_ecdh);
3042 EVP_PKEY_free(srvr_pub_pkey);
3044 s->state = SSL_ST_ERR;
3048 int ssl3_send_client_verify(SSL *s)
3051 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3053 EVP_PKEY_CTX *pctx = NULL;
3059 EVP_MD_CTX_init(&mctx);
3061 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3062 p = ssl_handshake_start(s);
3063 pkey = s->cert->key->privatekey;
3064 /* Create context from key and test if sha1 is allowed as digest */
3065 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3066 EVP_PKEY_sign_init(pctx);
3067 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3068 if (!SSL_USE_SIGALGS(s))
3069 s->method->ssl3_enc->cert_verify_mac(s,
3072 [MD5_DIGEST_LENGTH]));
3077 * For TLS v1.2 send signature algorithm and signature using agreed
3078 * digest and cached handshake records.
3080 if (SSL_USE_SIGALGS(s)) {
3083 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3084 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3085 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3086 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3091 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3094 if (!EVP_SignInit_ex(&mctx, md, NULL)
3095 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3096 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3097 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3103 * For extended master secret we've already digested cached
3106 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
3107 BIO_free(s->s3->handshake_buffer);
3108 s->s3->handshake_buffer = NULL;
3109 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3110 } else if (!ssl3_digest_cached_records(s))
3113 #ifndef OPENSSL_NO_RSA
3114 if (pkey->type == EVP_PKEY_RSA) {
3115 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3116 if (RSA_sign(NID_md5_sha1, data,
3117 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3118 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3119 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3126 #ifndef OPENSSL_NO_DSA
3127 if (pkey->type == EVP_PKEY_DSA) {
3128 if (!DSA_sign(pkey->save_type,
3129 &(data[MD5_DIGEST_LENGTH]),
3130 SHA_DIGEST_LENGTH, &(p[2]),
3131 (unsigned int *)&j, pkey->pkey.dsa)) {
3132 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3139 #ifndef OPENSSL_NO_EC
3140 if (pkey->type == EVP_PKEY_EC) {
3141 if (!ECDSA_sign(pkey->save_type,
3142 &(data[MD5_DIGEST_LENGTH]),
3143 SHA_DIGEST_LENGTH, &(p[2]),
3144 (unsigned int *)&j, pkey->pkey.ec)) {
3145 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3152 if (pkey->type == NID_id_GostR3410_94
3153 || pkey->type == NID_id_GostR3410_2001) {
3154 unsigned char signbuf[64];
3156 size_t sigsize = 64;
3157 s->method->ssl3_enc->cert_verify_mac(s,
3158 NID_id_GostR3411_94, data);
3159 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3160 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3163 for (i = 63, j = 0; i >= 0; j++, i--) {
3164 p[2 + j] = signbuf[i];
3169 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3172 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3173 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3176 s->state = SSL3_ST_CW_CERT_VRFY_B;
3178 EVP_MD_CTX_cleanup(&mctx);
3179 EVP_PKEY_CTX_free(pctx);
3180 return ssl_do_write(s);
3182 EVP_MD_CTX_cleanup(&mctx);
3183 EVP_PKEY_CTX_free(pctx);
3184 s->state = SSL_ST_ERR;
3189 * Check a certificate can be used for client authentication. Currently check
3190 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3191 * certificates can be used and optionally checks suitability for Suite B.
3193 static int ssl3_check_client_certificate(SSL *s)
3195 unsigned long alg_k;
3196 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3198 /* If no suitable signature algorithm can't use certificate */
3199 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3202 * If strict mode check suitability of chain before using it. This also
3203 * adjusts suite B digest if necessary.
3205 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3206 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3208 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3209 /* See if we can use client certificate for fixed DH */
3210 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3211 SESS_CERT *scert = s->session->sess_cert;
3212 int i = scert->peer_cert_type;
3213 EVP_PKEY *clkey = NULL, *spkey = NULL;
3214 clkey = s->cert->key->privatekey;
3215 /* If client key not DH assume it can be used */
3216 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3219 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3221 /* Compare server and client parameters */
3222 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3223 EVP_PKEY_free(spkey);
3227 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3232 int ssl3_send_client_certificate(SSL *s)
3235 EVP_PKEY *pkey = NULL;
3238 if (s->state == SSL3_ST_CW_CERT_A) {
3239 /* Let cert callback update client certificates if required */
3240 if (s->cert->cert_cb) {
3241 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3243 s->rwstate = SSL_X509_LOOKUP;
3247 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3248 s->state = SSL_ST_ERR;
3251 s->rwstate = SSL_NOTHING;
3253 if (ssl3_check_client_certificate(s))
3254 s->state = SSL3_ST_CW_CERT_C;
3256 s->state = SSL3_ST_CW_CERT_B;
3259 /* We need to get a client cert */
3260 if (s->state == SSL3_ST_CW_CERT_B) {
3262 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3263 * return(-1); We then get retied later
3266 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3268 s->rwstate = SSL_X509_LOOKUP;
3271 s->rwstate = SSL_NOTHING;
3272 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3273 s->state = SSL3_ST_CW_CERT_B;
3274 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3276 } else if (i == 1) {
3278 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3279 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3283 EVP_PKEY_free(pkey);
3284 if (i && !ssl3_check_client_certificate(s))
3287 if (s->version == SSL3_VERSION) {
3288 s->s3->tmp.cert_req = 0;
3289 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3292 s->s3->tmp.cert_req = 2;
3293 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3294 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3295 s->state = SSL_ST_ERR;
3301 /* Ok, we have a cert */
3302 s->state = SSL3_ST_CW_CERT_C;
3305 if (s->state == SSL3_ST_CW_CERT_C) {
3306 s->state = SSL3_ST_CW_CERT_D;
3307 if (!ssl3_output_cert_chain(s,
3308 (s->s3->tmp.cert_req ==
3309 2) ? NULL : s->cert->key)) {
3310 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3311 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3312 s->state = SSL_ST_ERR;
3316 /* SSL3_ST_CW_CERT_D */
3317 return ssl_do_write(s);
3320 #define has_bits(i,m) (((i)&(m)) == (m))
3322 int ssl3_check_cert_and_algorithm(SSL *s)
3326 EVP_PKEY *pkey = NULL;
3328 #ifndef OPENSSL_NO_RSA
3331 #ifndef OPENSSL_NO_DH
3335 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3336 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3338 /* we don't have a certificate */
3339 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3342 sc = s->session->sess_cert;
3344 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3347 #ifndef OPENSSL_NO_RSA
3348 rsa = s->session->sess_cert->peer_rsa_tmp;
3350 #ifndef OPENSSL_NO_DH
3351 dh = s->session->sess_cert->peer_dh_tmp;
3354 /* This is the passed certificate */
3356 idx = sc->peer_cert_type;
3357 #ifndef OPENSSL_NO_EC
3358 if (idx == SSL_PKEY_ECC) {
3359 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3361 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3366 } else if (alg_a & SSL_aECDSA) {
3367 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3368 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3370 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3371 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3375 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3376 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3377 EVP_PKEY_free(pkey);
3379 /* Check that we have a certificate if we require one */
3380 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3381 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3382 SSL_R_MISSING_RSA_SIGNING_CERT);
3385 #ifndef OPENSSL_NO_DSA
3386 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3387 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3388 SSL_R_MISSING_DSA_SIGNING_CERT);
3392 #ifndef OPENSSL_NO_RSA
3393 if ((alg_k & SSL_kRSA) &&
3394 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3395 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3396 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3400 #ifndef OPENSSL_NO_DH
3401 if ((alg_k & SSL_kDHE) &&
3402 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3403 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3405 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3406 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3407 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3408 SSL_R_MISSING_DH_RSA_CERT);
3411 # ifndef OPENSSL_NO_DSA
3412 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3413 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3414 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3415 SSL_R_MISSING_DH_DSA_CERT);
3421 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3422 #ifndef OPENSSL_NO_RSA
3423 if (alg_k & SSL_kRSA) {
3425 || RSA_size(rsa) * 8 >
3426 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3427 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3428 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3433 #ifndef OPENSSL_NO_DH
3434 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3436 || DH_size(dh) * 8 >
3437 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3438 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3439 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3445 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3446 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3452 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3457 #ifndef OPENSSL_NO_TLSEXT
3459 * Normally, we can tell if the server is resuming the session from
3460 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3461 * message after the ServerHello to determine if the server is resuming.
3462 * Therefore, we allow EAP-FAST to peek ahead.
3463 * ssl3_check_finished returns 1 if we are resuming from an external
3464 * pre-shared secret, we have a "ticket" and the next server handshake message
3465 * is Finished; and 0 otherwise. It returns -1 upon an error.
3467 static int ssl3_check_finished(SSL *s)
3471 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3472 !s->session->tlsext_tick)
3475 /* Need to permit this temporarily, in case the next message is Finished. */
3476 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3478 * This function is called when we might get a Certificate message instead,
3479 * so permit appropriate message length.
3480 * We ignore the return value as we're only interested in the message type
3481 * and not its length.
3483 s->method->ssl_get_message(s,
3486 -1, s->max_cert_list, &ok);
3487 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3492 s->s3->tmp.reuse_message = 1;
3494 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3497 /* If we're not done, then the CCS arrived early and we should bail. */
3498 if (s->s3->change_cipher_spec) {
3499 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3500 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3507 # ifndef OPENSSL_NO_NEXTPROTONEG
3508 int ssl3_send_next_proto(SSL *s)
3510 unsigned int len, padding_len;
3513 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3514 len = s->next_proto_negotiated_len;
3515 padding_len = 32 - ((len + 2) % 32);
3516 d = (unsigned char *)s->init_buf->data;
3518 memcpy(d + 5, s->next_proto_negotiated, len);
3519 d[5 + len] = padding_len;
3520 memset(d + 6 + len, 0, padding_len);
3521 *(d++) = SSL3_MT_NEXT_PROTO;
3522 l2n3(2 + len + padding_len, d);
3523 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3524 s->init_num = 4 + 2 + len + padding_len;
3528 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3533 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3536 #ifndef OPENSSL_NO_ENGINE
3537 if (s->ctx->client_cert_engine) {
3538 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3539 SSL_get_client_CA_list(s),
3540 px509, ppkey, NULL, NULL, NULL);
3545 if (s->ctx->client_cert_cb)
3546 i = s->ctx->client_cert_cb(s, px509, ppkey);
3550 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3552 int (*put_cb) (const SSL_CIPHER *,
3558 int empty_reneg_info_scsv = !s->renegotiate;
3559 /* Set disabled masks for this session */
3560 ssl_set_client_disabled(s);
3566 put_cb = s->method->put_cipher_by_char;
3568 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3569 c = sk_SSL_CIPHER_value(sk, i);
3570 /* Skip disabled ciphers */
3571 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3573 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3574 if (c->id == SSL3_CK_SCSV) {
3575 if (!empty_reneg_info_scsv)
3578 empty_reneg_info_scsv = 0;
3585 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3589 if (empty_reneg_info_scsv) {
3590 static SSL_CIPHER scsv = {
3591 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3593 j = put_cb(&scsv, p);
3595 #ifdef OPENSSL_RI_DEBUG
3597 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3600 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3601 static SSL_CIPHER scsv = {
3602 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3604 j = put_cb(&scsv, p);