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 *,
176 #ifndef OPENSSL_NO_SSL3_METHOD
177 static const SSL_METHOD *ssl3_get_client_method(int ver)
179 if (ver == SSL3_VERSION)
180 return (SSLv3_client_method());
185 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
186 ssl_undefined_function,
187 ssl3_connect, ssl3_get_client_method)
189 int ssl3_connect(SSL *s)
192 unsigned long Time = (unsigned long)time(NULL);
193 void (*cb) (const SSL *ssl, int type, int val) = NULL;
195 int new_state, state, skip = 0;
197 RAND_add(&Time, sizeof(Time), 0);
201 if (s->info_callback != NULL)
202 cb = s->info_callback;
203 else if (s->ctx->info_callback != NULL)
204 cb = s->ctx->info_callback;
207 if (!SSL_in_init(s) || SSL_in_before(s)) {
212 #ifndef OPENSSL_NO_HEARTBEATS
214 * If we're awaiting a HeartbeatResponse, pretend we already got and
215 * don't await it anymore, because Heartbeats don't make sense during
218 if (s->tlsext_hb_pending) {
219 s->tlsext_hb_pending = 0;
228 case SSL_ST_RENEGOTIATE:
230 s->state = SSL_ST_CONNECT;
231 s->ctx->stats.sess_connect_renegotiate++;
235 case SSL_ST_BEFORE | SSL_ST_CONNECT:
236 case SSL_ST_OK | SSL_ST_CONNECT:
240 cb(s, SSL_CB_HANDSHAKE_START, 1);
242 if ((s->version >> 8) != SSL3_VERSION_MAJOR
243 && s->version != TLS_ANY_VERSION) {
244 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
245 s->state = SSL_ST_ERR;
250 if (s->version != TLS_ANY_VERSION &&
251 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
252 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
256 /* s->version=SSL3_VERSION; */
257 s->type = SSL_ST_CONNECT;
259 if (s->init_buf == NULL) {
260 if ((buf = BUF_MEM_new()) == NULL) {
262 s->state = SSL_ST_ERR;
265 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
267 s->state = SSL_ST_ERR;
274 if (!ssl3_setup_buffers(s)) {
279 /* setup buffing BIO */
280 if (!ssl_init_wbio_buffer(s, 0)) {
282 s->state = SSL_ST_ERR;
286 /* don't push the buffering BIO quite yet */
288 ssl3_init_finished_mac(s);
290 s->state = SSL3_ST_CW_CLNT_HELLO_A;
291 s->ctx->stats.sess_connect++;
293 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
295 * Should have been reset by ssl3_get_finished, too.
297 s->s3->change_cipher_spec = 0;
300 case SSL3_ST_CW_CLNT_HELLO_A:
301 case SSL3_ST_CW_CLNT_HELLO_B:
304 ret = ssl3_client_hello(s);
307 s->state = SSL3_ST_CR_SRVR_HELLO_A;
310 /* turn on buffering for the next lot of output */
311 if (s->bbio != s->wbio)
312 s->wbio = BIO_push(s->bbio, s->wbio);
316 case SSL3_ST_CR_SRVR_HELLO_A:
317 case SSL3_ST_CR_SRVR_HELLO_B:
318 ret = ssl3_get_server_hello(s);
323 s->state = SSL3_ST_CR_FINISHED_A;
324 #ifndef OPENSSL_NO_TLSEXT
325 if (s->tlsext_ticket_expected) {
326 /* receive renewed session ticket */
327 s->state = SSL3_ST_CR_SESSION_TICKET_A;
331 s->state = SSL3_ST_CR_CERT_A;
335 case SSL3_ST_CR_CERT_A:
336 case SSL3_ST_CR_CERT_B:
337 #ifndef OPENSSL_NO_TLSEXT
338 /* Noop (ret = 0) for everything but EAP-FAST. */
339 ret = ssl3_check_finished(s);
344 s->state = SSL3_ST_CR_FINISHED_A;
349 /* Check if it is anon DH/ECDH, SRP auth */
353 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
354 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
355 ret = ssl3_get_server_certificate(s);
358 #ifndef OPENSSL_NO_TLSEXT
359 if (s->tlsext_status_expected)
360 s->state = SSL3_ST_CR_CERT_STATUS_A;
362 s->state = SSL3_ST_CR_KEY_EXCH_A;
365 s->state = SSL3_ST_CR_KEY_EXCH_A;
371 s->state = SSL3_ST_CR_KEY_EXCH_A;
376 case SSL3_ST_CR_KEY_EXCH_A:
377 case SSL3_ST_CR_KEY_EXCH_B:
378 ret = ssl3_get_key_exchange(s);
381 s->state = SSL3_ST_CR_CERT_REQ_A;
385 * at this point we check that we have the required stuff from
388 if (!ssl3_check_cert_and_algorithm(s)) {
390 s->state = SSL_ST_ERR;
395 case SSL3_ST_CR_CERT_REQ_A:
396 case SSL3_ST_CR_CERT_REQ_B:
397 ret = ssl3_get_certificate_request(s);
400 s->state = SSL3_ST_CR_SRVR_DONE_A;
404 case SSL3_ST_CR_SRVR_DONE_A:
405 case SSL3_ST_CR_SRVR_DONE_B:
406 ret = ssl3_get_server_done(s);
409 #ifndef OPENSSL_NO_SRP
410 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
411 if ((ret = SRP_Calc_A_param(s)) <= 0) {
412 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
413 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
414 s->state = SSL_ST_ERR;
419 if (s->s3->tmp.cert_req)
420 s->state = SSL3_ST_CW_CERT_A;
422 s->state = SSL3_ST_CW_KEY_EXCH_A;
427 case SSL3_ST_CW_CERT_A:
428 case SSL3_ST_CW_CERT_B:
429 case SSL3_ST_CW_CERT_C:
430 case SSL3_ST_CW_CERT_D:
431 ret = ssl3_send_client_certificate(s);
434 s->state = SSL3_ST_CW_KEY_EXCH_A;
438 case SSL3_ST_CW_KEY_EXCH_A:
439 case SSL3_ST_CW_KEY_EXCH_B:
440 ret = ssl3_send_client_key_exchange(s);
444 * EAY EAY EAY need to check for DH fix cert sent back
447 * For TLS, cert_req is set to 2, so a cert chain of nothing is
448 * sent, but no verify packet is sent
451 * XXX: For now, we do not support client authentication in ECDH
452 * cipher suites with ECDH (rather than ECDSA) certificates. We
453 * need to skip the certificate verify message when client's
454 * ECDH public key is sent inside the client certificate.
456 if (s->s3->tmp.cert_req == 1) {
457 s->state = SSL3_ST_CW_CERT_VRFY_A;
459 s->state = SSL3_ST_CW_CHANGE_A;
461 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
462 s->state = SSL3_ST_CW_CHANGE_A;
468 case SSL3_ST_CW_CERT_VRFY_A:
469 case SSL3_ST_CW_CERT_VRFY_B:
470 ret = ssl3_send_client_verify(s);
473 s->state = SSL3_ST_CW_CHANGE_A;
477 case SSL3_ST_CW_CHANGE_A:
478 case SSL3_ST_CW_CHANGE_B:
479 ret = ssl3_send_change_cipher_spec(s,
481 SSL3_ST_CW_CHANGE_B);
485 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
486 s->state = SSL3_ST_CW_FINISHED_A;
488 if (s->s3->next_proto_neg_seen)
489 s->state = SSL3_ST_CW_NEXT_PROTO_A;
491 s->state = SSL3_ST_CW_FINISHED_A;
495 s->session->cipher = s->s3->tmp.new_cipher;
496 #ifdef OPENSSL_NO_COMP
497 s->session->compress_meth = 0;
499 if (s->s3->tmp.new_compression == NULL)
500 s->session->compress_meth = 0;
502 s->session->compress_meth = s->s3->tmp.new_compression->id;
504 if (!s->method->ssl3_enc->setup_key_block(s)) {
506 s->state = SSL_ST_ERR;
510 if (!s->method->ssl3_enc->change_cipher_state(s,
511 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
514 s->state = SSL_ST_ERR;
520 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
521 case SSL3_ST_CW_NEXT_PROTO_A:
522 case SSL3_ST_CW_NEXT_PROTO_B:
523 ret = ssl3_send_next_proto(s);
526 s->state = SSL3_ST_CW_FINISHED_A;
530 case SSL3_ST_CW_FINISHED_A:
531 case SSL3_ST_CW_FINISHED_B:
532 ret = ssl3_send_finished(s,
533 SSL3_ST_CW_FINISHED_A,
534 SSL3_ST_CW_FINISHED_B,
536 ssl3_enc->client_finished_label,
538 ssl3_enc->client_finished_label_len);
541 s->state = SSL3_ST_CW_FLUSH;
544 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
546 s->s3->tmp.next_state = SSL_ST_OK;
547 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
548 s->state = SSL_ST_OK;
549 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
550 s->s3->delay_buf_pop_ret = 0;
553 #ifndef OPENSSL_NO_TLSEXT
555 * Allow NewSessionTicket if ticket expected
557 if (s->tlsext_ticket_expected)
558 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
562 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
567 #ifndef OPENSSL_NO_TLSEXT
568 case SSL3_ST_CR_SESSION_TICKET_A:
569 case SSL3_ST_CR_SESSION_TICKET_B:
570 ret = ssl3_get_new_session_ticket(s);
573 s->state = SSL3_ST_CR_FINISHED_A;
577 case SSL3_ST_CR_CERT_STATUS_A:
578 case SSL3_ST_CR_CERT_STATUS_B:
579 ret = ssl3_get_cert_status(s);
582 s->state = SSL3_ST_CR_KEY_EXCH_A;
587 case SSL3_ST_CR_FINISHED_A:
588 case SSL3_ST_CR_FINISHED_B:
589 if (!s->s3->change_cipher_spec)
590 s->s3->flags |= SSL3_FLAGS_CCS_OK;
591 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
592 SSL3_ST_CR_FINISHED_B);
597 s->state = SSL3_ST_CW_CHANGE_A;
599 s->state = SSL_ST_OK;
603 case SSL3_ST_CW_FLUSH:
604 s->rwstate = SSL_WRITING;
605 if (BIO_flush(s->wbio) <= 0) {
609 s->rwstate = SSL_NOTHING;
610 s->state = s->s3->tmp.next_state;
614 /* clean a few things up */
615 ssl3_cleanup_key_block(s);
616 BUF_MEM_free(s->init_buf);
620 * If we are not 'joining' the last two packets, remove the
623 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
624 ssl_free_wbio_buffer(s);
625 /* else do it later in ssl3_write */
631 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
633 s->ctx->stats.sess_hit++;
637 s->handshake_func = ssl3_connect;
638 s->ctx->stats.sess_connect_good++;
641 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
648 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
654 /* did we do anything */
655 if (!s->s3->tmp.reuse_message && !skip) {
657 if ((ret = BIO_flush(s->wbio)) <= 0)
661 if ((cb != NULL) && (s->state != state)) {
662 new_state = s->state;
664 cb(s, SSL_CB_CONNECT_LOOP, 1);
665 s->state = new_state;
674 cb(s, SSL_CB_CONNECT_EXIT, ret);
679 * Work out what version we should be using for the initial ClientHello if
680 * the version is currently set to (D)TLS_ANY_VERSION.
681 * Returns 1 on success
684 static int ssl_set_version(SSL *s)
686 unsigned long mask, options = s->options;
688 if (s->method->version == TLS_ANY_VERSION) {
690 * SSL_OP_NO_X disables all protocols above X *if* there are
691 * some protocols below X enabled. This is required in order
692 * to maintain "version capability" vector contiguous. So
693 * that if application wants to disable TLS1.0 in favour of
694 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
695 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
697 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
698 #if !defined(OPENSSL_NO_SSL3)
702 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
703 if (options & SSL_OP_NO_TLSv1_2) {
704 if ((options & mask) != mask) {
705 s->version = TLS1_1_VERSION;
707 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
711 s->version = TLS1_2_VERSION;
714 if ((options & mask) == mask) {
715 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
718 s->version = TLS1_1_VERSION;
721 mask &= ~SSL_OP_NO_TLSv1_1;
722 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
723 s->version = TLS1_VERSION;
724 mask &= ~SSL_OP_NO_TLSv1;
725 #if !defined(OPENSSL_NO_SSL3)
726 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
727 s->version = SSL3_VERSION;
730 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
731 SSLerr(SSL_F_SSL_SET_VERSION,
732 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
736 if (s->version == SSL3_VERSION && FIPS_mode()) {
737 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
741 } else if (s->method->version == DTLS_ANY_VERSION) {
742 /* Determine which DTLS version to use */
743 /* If DTLS 1.2 disabled correct the version number */
744 if (options & SSL_OP_NO_DTLSv1_2) {
745 if (tls1_suiteb(s)) {
746 SSLerr(SSL_F_SSL_SET_VERSION,
747 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
751 * Disabling all versions is silly: return an error.
753 if (options & SSL_OP_NO_DTLSv1) {
754 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
758 * Update method so we don't use any DTLS 1.2 features.
760 s->method = DTLSv1_client_method();
761 s->version = DTLS1_VERSION;
764 * We only support one version: update method
766 if (options & SSL_OP_NO_DTLSv1)
767 s->method = DTLSv1_2_client_method();
768 s->version = DTLS1_2_VERSION;
772 s->client_version = s->version;
777 int ssl3_client_hello(SSL *s)
780 unsigned char *p, *d;
784 #ifndef OPENSSL_NO_COMP
789 buf = (unsigned char *)s->init_buf->data;
790 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
791 SSL_SESSION *sess = s->session;
793 /* Work out what SSL/TLS/DTLS version to use */
794 if (ssl_set_version(s) == 0)
797 if ((sess == NULL) || (sess->ssl_version != s->version) ||
798 #ifdef OPENSSL_NO_TLSEXT
799 !sess->session_id_length ||
802 * In the case of EAP-FAST, we can have a pre-shared
803 * "ticket" without a session ID.
805 (!sess->session_id_length && !sess->tlsext_tick) ||
807 (sess->not_resumable)) {
808 if (!ssl_get_new_session(s, 0))
811 /* else use the pre-loaded session */
813 p = s->s3->client_random;
816 * for DTLS if client_random is initialized, reuse it, we are
817 * required to use same upon reply to HelloVerify
819 if (SSL_IS_DTLS(s)) {
822 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
831 if (i && ssl_fill_hello_random(s, 0, p,
832 sizeof(s->s3->client_random)) <= 0)
835 /* Do the message type and length last */
836 d = p = ssl_handshake_start(s);
839 * version indicates the negotiated version: for example from
840 * an SSLv2/v3 compatible client hello). The client_version
841 * field is the maximum version we permit and it is also
842 * used in RSA encrypted premaster secrets. Some servers can
843 * choke if we initially report a higher version then
844 * renegotiate to a lower one in the premaster secret. This
845 * didn't happen with TLS 1.0 as most servers supported it
846 * but it can with TLS 1.1 or later if the server only supports
849 * Possible scenario with previous logic:
850 * 1. Client hello indicates TLS 1.2
851 * 2. Server hello says TLS 1.0
852 * 3. RSA encrypted premaster secret uses 1.2.
853 * 4. Handhaked proceeds using TLS 1.0.
854 * 5. Server sends hello request to renegotiate.
855 * 6. Client hello indicates TLS v1.0 as we now
856 * know that is maximum server supports.
857 * 7. Server chokes on RSA encrypted premaster secret
858 * containing version 1.0.
860 * For interoperability it should be OK to always use the
861 * maximum version we support in client hello and then rely
862 * on the checking of version to ensure the servers isn't
863 * being inconsistent: for example initially negotiating with
864 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
865 * client_version in client hello and not resetting it to
866 * the negotiated version.
868 *(p++) = s->client_version >> 8;
869 *(p++) = s->client_version & 0xff;
872 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
873 p += SSL3_RANDOM_SIZE;
879 i = s->session->session_id_length;
882 if (i > (int)sizeof(s->session->session_id)) {
883 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
886 memcpy(p, s->session->session_id, i);
890 /* cookie stuff for DTLS */
891 if (SSL_IS_DTLS(s)) {
892 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
893 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
896 *(p++) = s->d1->cookie_len;
897 memcpy(p, s->d1->cookie, s->d1->cookie_len);
898 p += s->d1->cookie_len;
901 /* Ciphers supported */
902 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
904 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
907 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
909 * Some servers hang if client hello > 256 bytes as hack workaround
910 * chop number of supported ciphers to keep it well below this if we
913 if (TLS1_get_version(s) >= TLS1_2_VERSION
914 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
915 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
921 #ifdef OPENSSL_NO_COMP
925 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
928 j = sk_SSL_COMP_num(s->ctx->comp_methods);
930 for (i = 0; i < j; i++) {
931 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
935 *(p++) = 0; /* Add the NULL method */
937 #ifndef OPENSSL_NO_TLSEXT
939 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
940 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
944 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
946 ssl3_send_alert(s, SSL3_AL_FATAL, al);
947 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
953 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
954 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
955 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
958 s->state = SSL3_ST_CW_CLNT_HELLO_B;
961 /* SSL3_ST_CW_CLNT_HELLO_B */
962 return ssl_do_write(s);
964 s->state = SSL_ST_ERR;
968 int ssl3_get_server_hello(SSL *s)
970 STACK_OF(SSL_CIPHER) *sk;
973 unsigned char *p, *d;
974 int i, al = SSL_AD_INTERNAL_ERROR, ok;
977 #ifndef OPENSSL_NO_COMP
981 * Hello verify request and/or server hello version may not match so set
982 * first packet if we're negotiating version.
987 n = s->method->ssl_get_message(s,
988 SSL3_ST_CR_SRVR_HELLO_A,
989 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
994 if (SSL_IS_DTLS(s)) {
996 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
997 if (s->d1->send_cookie == 0) {
998 s->s3->tmp.reuse_message = 1;
1000 } else { /* already sent a cookie */
1002 al = SSL_AD_UNEXPECTED_MESSAGE;
1003 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
1009 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
1010 al = SSL_AD_UNEXPECTED_MESSAGE;
1011 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
1015 d = p = (unsigned char *)s->init_msg;
1017 if (s->method->version == TLS_ANY_VERSION) {
1018 int sversion = (p[0] << 8) | p[1];
1020 #if TLS_MAX_VERSION != TLS1_2_VERSION
1021 #error Code needs updating for new TLS version
1023 #ifndef OPENSSL_NO_SSL3
1024 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1026 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1027 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1028 al = SSL_AD_PROTOCOL_VERSION;
1031 s->method = SSLv3_client_method();
1034 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1035 s->method = TLSv1_client_method();
1036 } else if ((sversion == TLS1_1_VERSION) &&
1037 !(s->options & SSL_OP_NO_TLSv1_1)) {
1038 s->method = TLSv1_1_client_method();
1039 } else if ((sversion == TLS1_2_VERSION) &&
1040 !(s->options & SSL_OP_NO_TLSv1_2)) {
1041 s->method = TLSv1_2_client_method();
1043 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1044 al = SSL_AD_PROTOCOL_VERSION;
1047 s->session->ssl_version = s->version = s->method->version;
1049 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1050 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1051 al = SSL_AD_PROTOCOL_VERSION;
1054 } else if (s->method->version == DTLS_ANY_VERSION) {
1055 /* Work out correct protocol version to use */
1056 int hversion = (p[0] << 8) | p[1];
1057 int options = s->options;
1058 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1059 s->method = DTLSv1_2_client_method();
1060 else if (tls1_suiteb(s)) {
1061 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1062 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1063 s->version = hversion;
1064 al = SSL_AD_PROTOCOL_VERSION;
1066 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1067 s->method = DTLSv1_client_method();
1069 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1070 s->version = hversion;
1071 al = SSL_AD_PROTOCOL_VERSION;
1074 s->version = s->method->version;
1075 } else if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
1076 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1077 s->version = (s->version & 0xff00) | p[1];
1078 al = SSL_AD_PROTOCOL_VERSION;
1083 /* load the server hello data */
1084 /* load the server random */
1085 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
1086 p += SSL3_RANDOM_SIZE;
1090 /* get the session-id */
1093 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
1094 al = SSL_AD_ILLEGAL_PARAMETER;
1095 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1098 #ifndef OPENSSL_NO_TLSEXT
1100 * Check if we can resume the session based on external pre-shared secret.
1101 * EAP-FAST (RFC 4851) supports two types of session resumption.
1102 * Resumption based on server-side state works with session IDs.
1103 * Resumption based on pre-shared Protected Access Credentials (PACs)
1104 * works by overriding the SessionTicket extension at the application
1105 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1106 * servers would honour the session ID.) Therefore, the session ID alone
1107 * is not a reliable indicator of session resumption, so we first check if
1108 * we can resume, and later peek at the next handshake message to see if the
1109 * server wants to resume.
1111 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1112 s->session->tlsext_tick) {
1113 SSL_CIPHER *pref_cipher = NULL;
1114 s->session->master_key_length = sizeof(s->session->master_key);
1115 if (s->tls_session_secret_cb(s, s->session->master_key,
1116 &s->session->master_key_length,
1118 s->tls_session_secret_cb_arg)) {
1119 s->session->cipher = pref_cipher ?
1120 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1122 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1123 al = SSL_AD_INTERNAL_ERROR;
1127 #endif /* OPENSSL_NO_TLSEXT */
1129 if (j != 0 && j == s->session->session_id_length
1130 && memcmp(p, s->session->session_id, j) == 0) {
1131 if (s->sid_ctx_length != s->session->sid_ctx_length
1132 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1133 /* actually a client application bug */
1134 al = SSL_AD_ILLEGAL_PARAMETER;
1135 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1136 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1142 * If we were trying for session-id reuse but the server
1143 * didn't echo the ID, make a new SSL_SESSION.
1144 * In the case of EAP-FAST and PAC, we do not send a session ID,
1145 * so the PAC-based session secret is always preserved. It'll be
1146 * overwritten if the server refuses resumption.
1148 if (s->session->session_id_length > 0) {
1149 if (!ssl_get_new_session(s, 0)) {
1153 s->session->session_id_length = j;
1154 memcpy(s->session->session_id, p, j); /* j could be 0 */
1157 c = ssl_get_cipher_by_char(s, p);
1159 /* unknown cipher */
1160 al = SSL_AD_ILLEGAL_PARAMETER;
1161 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1164 /* Set version disabled mask now we know version */
1165 if (!SSL_USE_TLS1_2_CIPHERS(s))
1166 ct->mask_ssl = SSL_TLSV1_2;
1170 * If it is a disabled cipher we didn't send it in client hello, so
1173 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1174 al = SSL_AD_ILLEGAL_PARAMETER;
1175 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1178 p += ssl_put_cipher_by_char(s, NULL, NULL);
1180 sk = ssl_get_ciphers_by_id(s);
1181 i = sk_SSL_CIPHER_find(sk, c);
1183 /* we did not say we would use this cipher */
1184 al = SSL_AD_ILLEGAL_PARAMETER;
1185 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1190 * Depending on the session caching (internal/external), the cipher
1191 * and/or cipher_id values may not be set. Make sure that cipher_id is
1192 * set and use it for comparison.
1194 if (s->session->cipher)
1195 s->session->cipher_id = s->session->cipher->id;
1196 if (s->hit && (s->session->cipher_id != c->id)) {
1197 al = SSL_AD_ILLEGAL_PARAMETER;
1198 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1199 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1202 s->s3->tmp.new_cipher = c;
1204 * Don't digest cached records if no sigalgs: we may need them for client
1207 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1209 /* lets get the compression algorithm */
1211 #ifdef OPENSSL_NO_COMP
1213 al = SSL_AD_ILLEGAL_PARAMETER;
1214 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1215 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1219 * If compression is disabled we'd better not try to resume a session
1220 * using compression.
1222 if (s->session->compress_meth != 0) {
1223 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1228 if (s->hit && j != s->session->compress_meth) {
1229 al = SSL_AD_ILLEGAL_PARAMETER;
1230 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1231 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1236 else if (!ssl_allow_compression(s)) {
1237 al = SSL_AD_ILLEGAL_PARAMETER;
1238 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1241 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1243 if ((j != 0) && (comp == NULL)) {
1244 al = SSL_AD_ILLEGAL_PARAMETER;
1245 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1246 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1249 s->s3->tmp.new_compression = comp;
1253 #ifndef OPENSSL_NO_TLSEXT
1254 /* TLS extensions */
1255 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1256 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1262 /* wrong packet length */
1263 al = SSL_AD_DECODE_ERROR;
1264 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1270 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1272 s->state = SSL_ST_ERR;
1276 int ssl3_get_server_certificate(SSL *s)
1278 int al, i, ok, ret = -1, exp_idx;
1279 unsigned long n, nc, llen, l;
1281 const unsigned char *q, *p;
1283 STACK_OF(X509) *sk = NULL;
1285 EVP_PKEY *pkey = NULL;
1287 n = s->method->ssl_get_message(s,
1290 -1, s->max_cert_list, &ok);
1295 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1296 s->s3->tmp.reuse_message = 1;
1300 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1301 al = SSL_AD_UNEXPECTED_MESSAGE;
1302 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1305 p = d = (unsigned char *)s->init_msg;
1307 if ((sk = sk_X509_new_null()) == NULL) {
1308 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1313 if (llen + 3 != n) {
1314 al = SSL_AD_DECODE_ERROR;
1315 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1318 for (nc = 0; nc < llen;) {
1320 if ((l + nc + 3) > llen) {
1321 al = SSL_AD_DECODE_ERROR;
1322 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1323 SSL_R_CERT_LENGTH_MISMATCH);
1328 x = d2i_X509(NULL, &q, l);
1330 al = SSL_AD_BAD_CERTIFICATE;
1331 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1335 al = SSL_AD_DECODE_ERROR;
1336 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1337 SSL_R_CERT_LENGTH_MISMATCH);
1340 if (!sk_X509_push(sk, x)) {
1341 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1349 i = ssl_verify_cert_chain(s, sk);
1350 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1351 al = ssl_verify_alarm_type(s->verify_result);
1352 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1353 SSL_R_CERTIFICATE_VERIFY_FAILED);
1356 ERR_clear_error(); /* but we keep s->verify_result */
1358 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1359 al = SSL_AD_HANDSHAKE_FAILURE;
1363 sc = ssl_sess_cert_new();
1367 ssl_sess_cert_free(s->session->sess_cert);
1368 s->session->sess_cert = sc;
1370 sc->cert_chain = sk;
1372 * Inconsistency alert: cert_chain does include the peer's certificate,
1373 * which we don't include in s3_srvr.c
1375 x = sk_X509_value(sk, 0);
1378 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1381 pkey = X509_get_pubkey(x);
1383 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1386 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1387 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1391 i = ssl_cert_type(x, pkey);
1395 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1396 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1400 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1401 if (exp_idx >= 0 && i != exp_idx) {
1403 al = SSL_AD_ILLEGAL_PARAMETER;
1404 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1405 SSL_R_WRONG_CERTIFICATE_TYPE);
1408 sc->peer_cert_type = i;
1409 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1411 * Why would the following ever happen? We just created sc a couple
1414 X509_free(sc->peer_pkeys[i].x509);
1415 sc->peer_pkeys[i].x509 = x;
1416 sc->peer_key = &(sc->peer_pkeys[i]);
1418 X509_free(s->session->peer);
1419 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1420 s->session->peer = x;
1421 s->session->verify_result = s->verify_result;
1428 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1430 s->state = SSL_ST_ERR;
1432 EVP_PKEY_free(pkey);
1434 sk_X509_pop_free(sk, X509_free);
1438 int ssl3_get_key_exchange(SSL *s)
1440 #ifndef OPENSSL_NO_RSA
1441 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1444 unsigned char *param, *p;
1446 long i, param_len, n, alg_k, alg_a;
1447 EVP_PKEY *pkey = NULL;
1448 const EVP_MD *md = NULL;
1449 #ifndef OPENSSL_NO_RSA
1452 #ifndef OPENSSL_NO_DH
1455 #ifndef OPENSSL_NO_EC
1456 EC_KEY *ecdh = NULL;
1457 BN_CTX *bn_ctx = NULL;
1458 EC_POINT *srvr_ecpoint = NULL;
1460 int encoded_pt_len = 0;
1463 EVP_MD_CTX_init(&md_ctx);
1466 * use same message size as in ssl3_get_certificate_request() as
1467 * ServerKeyExchange message may be skipped
1469 n = s->method->ssl_get_message(s,
1470 SSL3_ST_CR_KEY_EXCH_A,
1471 SSL3_ST_CR_KEY_EXCH_B,
1472 -1, s->max_cert_list, &ok);
1476 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1478 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1480 * Can't skip server key exchange if this is an ephemeral
1483 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1484 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1485 al = SSL_AD_UNEXPECTED_MESSAGE;
1488 #ifndef OPENSSL_NO_PSK
1490 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1491 * identity hint is sent. Set session->sess_cert anyway to avoid
1494 if (alg_k & SSL_kPSK) {
1495 s->session->sess_cert = ssl_sess_cert_new();
1496 OPENSSL_free(s->ctx->psk_identity_hint);
1497 s->ctx->psk_identity_hint = NULL;
1500 s->s3->tmp.reuse_message = 1;
1504 param = p = (unsigned char *)s->init_msg;
1505 if (s->session->sess_cert != NULL) {
1506 #ifndef OPENSSL_NO_RSA
1507 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1508 s->session->sess_cert->peer_rsa_tmp = NULL;
1510 #ifndef OPENSSL_NO_DH
1511 DH_free(s->session->sess_cert->peer_dh_tmp);
1512 s->session->sess_cert->peer_dh_tmp = NULL;
1514 #ifndef OPENSSL_NO_EC
1515 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1516 s->session->sess_cert->peer_ecdh_tmp = NULL;
1519 s->session->sess_cert = ssl_sess_cert_new();
1522 /* Total length of the parameters including the length prefix */
1525 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1527 al = SSL_AD_DECODE_ERROR;
1529 #ifndef OPENSSL_NO_PSK
1530 if (alg_k & SSL_kPSK) {
1531 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1534 if (param_len > n) {
1535 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1541 * Store PSK identity hint for later use, hint is used in
1542 * ssl3_send_client_key_exchange. Assume that the maximum length of
1543 * a PSK identity hint can be as long as the maximum length of a PSK
1546 if (i > PSK_MAX_IDENTITY_LEN) {
1547 al = SSL_AD_HANDSHAKE_FAILURE;
1548 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1551 if (i > n - param_len) {
1552 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1553 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1559 * If received PSK identity hint contains NULL characters, the hint
1560 * is truncated from the first NULL. p may not be ending with NULL,
1561 * so create a NULL-terminated string.
1563 memcpy(tmp_id_hint, p, i);
1564 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1565 OPENSSL_free(s->ctx->psk_identity_hint);
1566 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1567 if (s->ctx->psk_identity_hint == NULL) {
1568 al = SSL_AD_HANDSHAKE_FAILURE;
1569 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1576 #endif /* !OPENSSL_NO_PSK */
1577 #ifndef OPENSSL_NO_SRP
1578 if (alg_k & SSL_kSRP) {
1580 if (param_len > n) {
1581 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1586 if (i > n - param_len) {
1587 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1592 if ((s->srp_ctx.N = BN_bin2bn(p, i, NULL)) == NULL) {
1593 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1598 if (2 > n - param_len) {
1599 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1606 if (i > n - param_len) {
1607 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1612 if ((s->srp_ctx.g = BN_bin2bn(p, i, NULL)) == NULL) {
1613 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1618 if (1 > n - param_len) {
1619 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1624 i = (unsigned int)(p[0]);
1627 if (i > n - param_len) {
1628 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1633 if ((s->srp_ctx.s = BN_bin2bn(p, i, NULL)) == NULL) {
1634 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1639 if (2 > n - param_len) {
1640 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1647 if (i > n - param_len) {
1648 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1653 if ((s->srp_ctx.B = BN_bin2bn(p, i, NULL)) == NULL) {
1654 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1660 if (!srp_verify_server_param(s, &al)) {
1661 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1665 /* We must check if there is a certificate */
1666 # ifndef OPENSSL_NO_RSA
1667 if (alg_a & SSL_aRSA)
1669 X509_get_pubkey(s->session->
1670 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1674 # ifndef OPENSSL_NO_DSA
1675 else if (alg_a & SSL_aDSS)
1677 X509_get_pubkey(s->session->
1678 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1682 #endif /* !OPENSSL_NO_SRP */
1683 #ifndef OPENSSL_NO_RSA
1684 if (alg_k & SSL_kRSA) {
1685 /* Temporary RSA keys only allowed in export ciphersuites */
1686 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1687 al = SSL_AD_UNEXPECTED_MESSAGE;
1688 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1691 if ((rsa = RSA_new()) == NULL) {
1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1697 if (param_len > n) {
1698 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1703 if (i > n - param_len) {
1704 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1709 if ((rsa->n = BN_bin2bn(p, i, rsa->n)) == NULL) {
1710 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1715 if (2 > n - param_len) {
1716 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1723 if (i > n - param_len) {
1724 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1729 if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == NULL) {
1730 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1736 /* this should be because we are using an export cipher */
1737 if (alg_a & SSL_aRSA)
1739 X509_get_pubkey(s->session->
1740 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1742 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1745 s->session->sess_cert->peer_rsa_tmp = rsa;
1748 #else /* OPENSSL_NO_RSA */
1751 #ifndef OPENSSL_NO_DH
1752 else if (alg_k & SSL_kDHE) {
1753 if ((dh = DH_new()) == NULL) {
1754 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1759 if (param_len > n) {
1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1765 if (i > n - param_len) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1771 if ((dh->p = BN_bin2bn(p, i, NULL)) == NULL) {
1772 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1777 if (2 > n - param_len) {
1778 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1785 if (i > n - param_len) {
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1791 if ((dh->g = BN_bin2bn(p, i, NULL)) == NULL) {
1792 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1797 if (2 > n - param_len) {
1798 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1805 if (i > n - param_len) {
1806 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1811 if ((dh->pub_key = BN_bin2bn(p, i, NULL)) == NULL) {
1812 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1818 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1819 al = SSL_AD_HANDSHAKE_FAILURE;
1820 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1823 # ifndef OPENSSL_NO_RSA
1824 if (alg_a & SSL_aRSA)
1826 X509_get_pubkey(s->session->
1827 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1831 # ifndef OPENSSL_NO_DSA
1832 else if (alg_a & SSL_aDSS)
1834 X509_get_pubkey(s->session->
1835 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1838 /* else anonymous DH, so no certificate or pkey. */
1840 s->session->sess_cert->peer_dh_tmp = dh;
1843 #endif /* !OPENSSL_NO_DH */
1845 #ifndef OPENSSL_NO_EC
1846 else if (alg_k & SSL_kECDHE) {
1848 const EC_GROUP *group;
1850 if ((ecdh = EC_KEY_new()) == NULL) {
1851 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1856 * Extract elliptic curve parameters and the server's ephemeral ECDH
1857 * public key. Keep accumulating lengths of various components in
1858 * param_len and make sure it never exceeds n.
1862 * XXX: For now we only support named (not generic) curves and the
1863 * ECParameters in this case is just three bytes. We also need one
1864 * byte for the length of the encoded point
1867 if (param_len > n) {
1868 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1872 * Check curve is one of our preferences, if not server has sent an
1873 * invalid curve. ECParameters is 3 bytes.
1875 if (!tls1_check_curve(s, p, 3)) {
1876 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1880 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1881 al = SSL_AD_INTERNAL_ERROR;
1882 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1883 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1887 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1888 if (ngroup == NULL) {
1889 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1892 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1893 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1896 EC_GROUP_free(ngroup);
1898 group = EC_KEY_get0_group(ecdh);
1900 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1901 (EC_GROUP_get_degree(group) > 163)) {
1902 al = SSL_AD_EXPORT_RESTRICTION;
1903 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1904 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1910 /* Next, get the encoded ECPoint */
1911 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1912 ((bn_ctx = BN_CTX_new()) == NULL)) {
1913 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1917 encoded_pt_len = *p; /* length of encoded point */
1920 if ((encoded_pt_len > n - param_len) ||
1921 (EC_POINT_oct2point(group, srvr_ecpoint,
1922 p, encoded_pt_len, bn_ctx) == 0)) {
1923 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1926 param_len += encoded_pt_len;
1929 p += encoded_pt_len;
1932 * The ECC/TLS specification does not mention the use of DSA to sign
1933 * ECParameters in the server key exchange message. We do support RSA
1937 # ifndef OPENSSL_NO_RSA
1938 else if (alg_a & SSL_aRSA)
1940 X509_get_pubkey(s->session->
1941 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1943 # ifndef OPENSSL_NO_EC
1944 else if (alg_a & SSL_aECDSA)
1946 X509_get_pubkey(s->session->
1947 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1949 /* else anonymous ECDH, so no certificate or pkey. */
1950 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1951 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1953 BN_CTX_free(bn_ctx);
1955 EC_POINT_free(srvr_ecpoint);
1956 srvr_ecpoint = NULL;
1958 al = SSL_AD_UNEXPECTED_MESSAGE;
1959 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1962 #endif /* !OPENSSL_NO_EC */
1964 /* p points to the next byte, there are 'n' bytes left */
1966 /* if it was signed, check the signature */
1968 if (SSL_USE_SIGALGS(s)) {
1971 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1974 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1981 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1989 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1994 j = EVP_PKEY_size(pkey);
1997 * Check signature length. If n is 0 then signature is empty
1999 if ((i != n) || (n > j) || (n <= 0)) {
2000 /* wrong packet length */
2001 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
2004 #ifndef OPENSSL_NO_RSA
2005 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
2011 for (num = 2; num > 0; num--) {
2012 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2013 EVP_DigestInit_ex(&md_ctx, (num == 2)
2014 ? s->ctx->md5 : s->ctx->sha1, NULL);
2015 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2017 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2019 EVP_DigestUpdate(&md_ctx, param, param_len);
2020 EVP_DigestFinal_ex(&md_ctx, q, &size);
2024 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
2026 al = SSL_AD_DECRYPT_ERROR;
2027 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
2032 al = SSL_AD_DECRYPT_ERROR;
2033 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2039 EVP_VerifyInit_ex(&md_ctx, md, NULL);
2040 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
2042 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
2044 EVP_VerifyUpdate(&md_ctx, param, param_len);
2045 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
2047 al = SSL_AD_DECRYPT_ERROR;
2048 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2053 /* aNULL, aSRP or kPSK do not need public keys */
2054 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
2055 /* Might be wrong key type, check it */
2056 if (ssl3_check_cert_and_algorithm(s))
2057 /* Otherwise this shouldn't happen */
2058 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2061 /* still data left over */
2063 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2067 EVP_PKEY_free(pkey);
2068 EVP_MD_CTX_cleanup(&md_ctx);
2071 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2073 EVP_PKEY_free(pkey);
2074 #ifndef OPENSSL_NO_RSA
2077 #ifndef OPENSSL_NO_DH
2080 #ifndef OPENSSL_NO_EC
2081 BN_CTX_free(bn_ctx);
2082 EC_POINT_free(srvr_ecpoint);
2085 EVP_MD_CTX_cleanup(&md_ctx);
2086 s->state = SSL_ST_ERR;
2090 int ssl3_get_certificate_request(SSL *s)
2093 unsigned long n, nc, l;
2094 unsigned int llen, ctype_num, i;
2095 X509_NAME *xn = NULL;
2096 const unsigned char *p, *q;
2098 STACK_OF(X509_NAME) *ca_sk = NULL;
2100 n = s->method->ssl_get_message(s,
2101 SSL3_ST_CR_CERT_REQ_A,
2102 SSL3_ST_CR_CERT_REQ_B,
2103 -1, s->max_cert_list, &ok);
2108 s->s3->tmp.cert_req = 0;
2110 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2111 s->s3->tmp.reuse_message = 1;
2113 * If we get here we don't need any cached handshake records as we
2114 * wont be doing client auth.
2116 if (s->s3->handshake_buffer) {
2117 if (!ssl3_digest_cached_records(s))
2123 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2124 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2125 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2129 /* TLS does not like anon-DH with client cert */
2130 if (s->version > SSL3_VERSION) {
2131 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2132 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2133 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2134 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2139 p = d = (unsigned char *)s->init_msg;
2141 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2142 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2146 /* get the certificate types */
2148 OPENSSL_free(s->cert->ctypes);
2149 s->cert->ctypes = NULL;
2150 if (ctype_num > SSL3_CT_NUMBER) {
2151 /* If we exceed static buffer copy all to cert structure */
2152 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2153 if (s->cert->ctypes == NULL) {
2154 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2157 memcpy(s->cert->ctypes, p, ctype_num);
2158 s->cert->ctype_num = (size_t)ctype_num;
2159 ctype_num = SSL3_CT_NUMBER;
2161 for (i = 0; i < ctype_num; i++)
2162 s->s3->tmp.ctype[i] = p[i];
2164 if (SSL_USE_SIGALGS(s)) {
2167 * Check we have enough room for signature algorithms and following
2170 if ((unsigned long)(p - d + llen + 2) > n) {
2171 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2172 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2173 SSL_R_DATA_LENGTH_TOO_LONG);
2176 /* Clear certificate digests and validity flags */
2177 for (i = 0; i < SSL_PKEY_NUM; i++) {
2178 s->cert->pkeys[i].digest = NULL;
2179 s->cert->pkeys[i].valid_flags = 0;
2181 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2182 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2183 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2184 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2187 if (!tls1_process_sigalgs(s)) {
2188 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2189 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2195 /* get the CA RDNs */
2198 if ((unsigned long)(p - d + llen) != n) {
2199 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2200 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2204 for (nc = 0; nc < llen;) {
2206 if ((l + nc + 2) > llen) {
2207 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2208 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2214 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2215 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2216 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2221 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2222 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2223 SSL_R_CA_DN_LENGTH_MISMATCH);
2226 if (!sk_X509_NAME_push(ca_sk, xn)) {
2227 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2235 /* we should setup a certificate to return.... */
2236 s->s3->tmp.cert_req = 1;
2237 s->s3->tmp.ctype_num = ctype_num;
2238 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2239 s->s3->tmp.ca_names = ca_sk;
2245 s->state = SSL_ST_ERR;
2247 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2251 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2253 return (X509_NAME_cmp(*a, *b));
2256 #ifndef OPENSSL_NO_TLSEXT
2257 int ssl3_get_new_session_ticket(SSL *s)
2259 int ok, al, ret = 0, ticklen;
2261 const unsigned char *p;
2264 n = s->method->ssl_get_message(s,
2265 SSL3_ST_CR_SESSION_TICKET_A,
2266 SSL3_ST_CR_SESSION_TICKET_B,
2267 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2273 /* need at least ticket_lifetime_hint + ticket length */
2274 al = SSL_AD_DECODE_ERROR;
2275 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2279 p = d = (unsigned char *)s->init_msg;
2280 n2l(p, s->session->tlsext_tick_lifetime_hint);
2282 /* ticket_lifetime_hint + ticket_length + ticket */
2283 if (ticklen + 6 != n) {
2284 al = SSL_AD_DECODE_ERROR;
2285 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2288 OPENSSL_free(s->session->tlsext_tick);
2289 s->session->tlsext_ticklen = 0;
2290 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2291 if (!s->session->tlsext_tick) {
2292 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2295 memcpy(s->session->tlsext_tick, p, ticklen);
2296 s->session->tlsext_ticklen = ticklen;
2298 * There are two ways to detect a resumed ticket session. One is to set
2299 * an appropriate session ID and then the server must return a match in
2300 * ServerHello. This allows the normal client session ID matching to work
2301 * and we know much earlier that the ticket has been accepted. The
2302 * other way is to set zero length session ID when the ticket is
2303 * presented and rely on the handshake to determine session resumption.
2304 * We choose the former approach because this fits in with assumptions
2305 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2306 * SHA256 is disabled) hash of the ticket.
2308 EVP_Digest(p, ticklen,
2309 s->session->session_id, &s->session->session_id_length,
2310 EVP_sha256(), NULL);
2314 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2316 s->state = SSL_ST_ERR;
2320 int ssl3_get_cert_status(SSL *s)
2323 unsigned long resplen, n;
2324 const unsigned char *p;
2326 n = s->method->ssl_get_message(s,
2327 SSL3_ST_CR_CERT_STATUS_A,
2328 SSL3_ST_CR_CERT_STATUS_B,
2329 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2334 /* need at least status type + length */
2335 al = SSL_AD_DECODE_ERROR;
2336 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2339 p = (unsigned char *)s->init_msg;
2340 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2341 al = SSL_AD_DECODE_ERROR;
2342 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2346 if (resplen + 4 != n) {
2347 al = SSL_AD_DECODE_ERROR;
2348 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2351 OPENSSL_free(s->tlsext_ocsp_resp);
2352 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2353 if (!s->tlsext_ocsp_resp) {
2354 al = SSL_AD_INTERNAL_ERROR;
2355 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2358 s->tlsext_ocsp_resplen = resplen;
2359 if (s->ctx->tlsext_status_cb) {
2361 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2363 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2364 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2368 al = SSL_AD_INTERNAL_ERROR;
2369 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2375 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2376 s->state = SSL_ST_ERR;
2381 int ssl3_get_server_done(SSL *s)
2386 /* Second to last param should be very small, like 0 :-) */
2387 n = s->method->ssl_get_message(s,
2388 SSL3_ST_CR_SRVR_DONE_A,
2389 SSL3_ST_CR_SRVR_DONE_B,
2390 SSL3_MT_SERVER_DONE, 30, &ok);
2395 /* should contain no data */
2396 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2397 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2398 s->state = SSL_ST_ERR;
2405 int ssl3_send_client_key_exchange(SSL *s)
2409 unsigned long alg_k;
2410 #ifndef OPENSSL_NO_RSA
2412 EVP_PKEY *pkey = NULL;
2414 #ifndef OPENSSL_NO_EC
2415 EC_KEY *clnt_ecdh = NULL;
2416 const EC_POINT *srvr_ecpoint = NULL;
2417 EVP_PKEY *srvr_pub_pkey = NULL;
2418 unsigned char *encodedPoint = NULL;
2419 int encoded_pt_len = 0;
2420 BN_CTX *bn_ctx = NULL;
2422 unsigned char *pms = NULL;
2425 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2426 p = ssl_handshake_start(s);
2428 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2430 /* Fool emacs indentation */
2433 #ifndef OPENSSL_NO_RSA
2434 else if (alg_k & SSL_kRSA) {
2436 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2437 pms = OPENSSL_malloc(pmslen);
2441 if (s->session->sess_cert == NULL) {
2443 * We should always have a server certificate with SSL_kRSA.
2445 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2446 ERR_R_INTERNAL_ERROR);
2450 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2451 rsa = s->session->sess_cert->peer_rsa_tmp;
2454 X509_get_pubkey(s->session->
2455 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2457 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2458 || (pkey->pkey.rsa == NULL)) {
2459 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2460 ERR_R_INTERNAL_ERROR);
2463 rsa = pkey->pkey.rsa;
2464 EVP_PKEY_free(pkey);
2467 pms[0] = s->client_version >> 8;
2468 pms[1] = s->client_version & 0xff;
2469 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2473 /* Fix buf for TLS and beyond */
2474 if (s->version > SSL3_VERSION)
2476 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2478 if (s->options & SSL_OP_PKCS1_CHECK_1)
2480 if (s->options & SSL_OP_PKCS1_CHECK_2)
2484 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2485 SSL_R_BAD_RSA_ENCRYPT);
2489 /* Fix buf for TLS and beyond */
2490 if (s->version > SSL3_VERSION) {
2496 #ifndef OPENSSL_NO_DH
2497 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2498 DH *dh_srvr, *dh_clnt;
2499 SESS_CERT *scert = s->session->sess_cert;
2501 if (scert == NULL) {
2502 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2503 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2504 SSL_R_UNEXPECTED_MESSAGE);
2508 if (scert->peer_dh_tmp != NULL)
2509 dh_srvr = scert->peer_dh_tmp;
2511 /* we get them from the cert */
2512 int idx = scert->peer_cert_type;
2513 EVP_PKEY *spkey = NULL;
2516 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2518 dh_srvr = EVP_PKEY_get1_DH(spkey);
2519 EVP_PKEY_free(spkey);
2521 if (dh_srvr == NULL) {
2522 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2523 ERR_R_INTERNAL_ERROR);
2527 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2528 /* Use client certificate key */
2529 EVP_PKEY *clkey = s->cert->key->privatekey;
2532 dh_clnt = EVP_PKEY_get1_DH(clkey);
2533 if (dh_clnt == NULL) {
2534 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2535 ERR_R_INTERNAL_ERROR);
2539 /* generate a new random key */
2540 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2541 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2544 if (!DH_generate_key(dh_clnt)) {
2545 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2551 pmslen = DH_size(dh_clnt);
2552 pms = OPENSSL_malloc(pmslen);
2557 * use the 'p' output buffer for the DH key, but make sure to
2558 * clear it out afterwards
2561 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2562 if (scert->peer_dh_tmp == NULL)
2566 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2572 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2575 /* send off the data */
2576 n = BN_num_bytes(dh_clnt->pub_key);
2578 BN_bn2bin(dh_clnt->pub_key, p);
2584 /* perhaps clean things up a bit EAY EAY EAY EAY */
2588 #ifndef OPENSSL_NO_EC
2589 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2590 const EC_GROUP *srvr_group = NULL;
2592 int ecdh_clnt_cert = 0;
2595 if (s->session->sess_cert == NULL) {
2596 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2597 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2598 SSL_R_UNEXPECTED_MESSAGE);
2603 * Did we send out the client's ECDH share for use in premaster
2604 * computation as part of client certificate? If so, set
2605 * ecdh_clnt_cert to 1.
2607 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2609 * XXX: For now, we do not support client
2610 * authentication using ECDH certificates.
2611 * To add such support, one needs to add
2612 * code that checks for appropriate
2613 * conditions and sets ecdh_clnt_cert to 1.
2614 * For example, the cert have an ECC
2615 * key on the same curve as the server's
2616 * and the key should be authorized for
2619 * One also needs to add code in ssl3_connect
2620 * to skip sending the certificate verify
2623 * if ((s->cert->key->privatekey != NULL) &&
2624 * (s->cert->key->privatekey->type ==
2625 * EVP_PKEY_EC) && ...)
2626 * ecdh_clnt_cert = 1;
2630 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2631 tkey = s->session->sess_cert->peer_ecdh_tmp;
2633 /* Get the Server Public Key from Cert */
2635 X509_get_pubkey(s->session->
2636 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2637 if ((srvr_pub_pkey == NULL)
2638 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2639 || (srvr_pub_pkey->pkey.ec == NULL)) {
2640 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2641 ERR_R_INTERNAL_ERROR);
2645 tkey = srvr_pub_pkey->pkey.ec;
2648 srvr_group = EC_KEY_get0_group(tkey);
2649 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2651 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2652 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2653 ERR_R_INTERNAL_ERROR);
2657 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2658 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2659 ERR_R_MALLOC_FAILURE);
2663 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2664 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2667 if (ecdh_clnt_cert) {
2669 * Reuse key info from our certificate We only need our
2670 * private key to perform the ECDH computation.
2672 const BIGNUM *priv_key;
2673 tkey = s->cert->key->privatekey->pkey.ec;
2674 priv_key = EC_KEY_get0_private_key(tkey);
2675 if (priv_key == NULL) {
2676 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2677 ERR_R_MALLOC_FAILURE);
2680 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2681 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2685 /* Generate a new ECDH key pair */
2686 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2687 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2694 * use the 'p' output buffer for the ECDH key, but make sure to
2695 * clear it out afterwards
2698 field_size = EC_GROUP_get_degree(srvr_group);
2699 if (field_size <= 0) {
2700 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2703 pmslen = (field_size + 7) / 8;
2704 pms = OPENSSL_malloc(pmslen);
2707 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2708 if (n <= 0 || pmslen != (size_t)n) {
2709 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2713 if (ecdh_clnt_cert) {
2714 /* Send empty client key exch message */
2718 * First check the size of encoding and allocate memory
2722 EC_POINT_point2oct(srvr_group,
2723 EC_KEY_get0_public_key(clnt_ecdh),
2724 POINT_CONVERSION_UNCOMPRESSED,
2727 encodedPoint = (unsigned char *)
2728 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2729 bn_ctx = BN_CTX_new();
2730 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2731 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2732 ERR_R_MALLOC_FAILURE);
2736 /* Encode the public key */
2737 n = EC_POINT_point2oct(srvr_group,
2738 EC_KEY_get0_public_key(clnt_ecdh),
2739 POINT_CONVERSION_UNCOMPRESSED,
2740 encodedPoint, encoded_pt_len, bn_ctx);
2742 *p = n; /* length of encoded point */
2743 /* Encoded point will be copied here */
2745 /* copy the point */
2746 memcpy(p, encodedPoint, n);
2747 /* increment n to account for length field */
2751 /* Free allocated memory */
2752 BN_CTX_free(bn_ctx);
2753 OPENSSL_free(encodedPoint);
2754 EC_KEY_free(clnt_ecdh);
2755 EVP_PKEY_free(srvr_pub_pkey);
2757 #endif /* !OPENSSL_NO_EC */
2758 else if (alg_k & SSL_kGOST) {
2759 /* GOST key exchange message creation */
2760 EVP_PKEY_CTX *pkey_ctx;
2763 unsigned int md_len;
2765 unsigned char shared_ukm[32], tmp[256];
2766 EVP_MD_CTX *ukm_hash;
2770 pms = OPENSSL_malloc(pmslen);
2775 * Get server sertificate PKEY and create ctx from it
2779 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2783 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2785 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2786 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2790 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2791 X509_get_pubkey(peer_cert), NULL);
2793 * If we have send a certificate, and certificate key
2795 * * parameters match those of server certificate, use
2796 * certificate key for key exchange
2799 /* Otherwise, generate ephemeral key pair */
2801 EVP_PKEY_encrypt_init(pkey_ctx);
2802 /* Generate session key */
2803 if (RAND_bytes(pms, pmslen) <= 0) {
2804 EVP_PKEY_CTX_free(pkey_ctx);
2805 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2806 ERR_R_INTERNAL_ERROR);
2810 * If we have client certificate, use its secret as peer key
2812 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2813 if (EVP_PKEY_derive_set_peer
2814 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2816 * If there was an error - just ignore it. Ephemeral key
2823 * Compute shared IV and store it in algorithm-specific context
2826 ukm_hash = EVP_MD_CTX_create();
2827 EVP_DigestInit(ukm_hash,
2828 EVP_get_digestbynid(NID_id_GostR3411_94));
2829 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2831 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2833 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2834 EVP_MD_CTX_destroy(ukm_hash);
2835 if (EVP_PKEY_CTX_ctrl
2836 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2838 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2842 /* Make GOST keytransport blob message */
2844 * Encapsulate it into sequence
2846 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2848 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2849 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2853 if (msglen >= 0x80) {
2855 *(p++) = msglen & 0xff;
2858 *(p++) = msglen & 0xff;
2861 memcpy(p, tmp, msglen);
2862 /* Check if pubkey from client certificate was used */
2863 if (EVP_PKEY_CTX_ctrl
2864 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2865 /* Set flag "skip certificate verify" */
2866 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2868 EVP_PKEY_CTX_free(pkey_ctx);
2869 EVP_PKEY_free(pub_key);
2872 #ifndef OPENSSL_NO_SRP
2873 else if (alg_k & SSL_kSRP) {
2874 if (s->srp_ctx.A != NULL) {
2875 /* send off the data */
2876 n = BN_num_bytes(s->srp_ctx.A);
2878 BN_bn2bin(s->srp_ctx.A, p);
2881 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2882 ERR_R_INTERNAL_ERROR);
2885 OPENSSL_free(s->session->srp_username);
2886 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2887 if (s->session->srp_username == NULL) {
2888 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2889 ERR_R_MALLOC_FAILURE);
2894 #ifndef OPENSSL_NO_PSK
2895 else if (alg_k & SSL_kPSK) {
2897 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2898 * \0-terminated identity. The last byte is for us for simulating
2901 char identity[PSK_MAX_IDENTITY_LEN + 2];
2902 size_t identity_len;
2903 unsigned char *t = NULL;
2904 unsigned int psk_len = 0;
2908 if (s->psk_client_callback == NULL) {
2909 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2910 SSL_R_PSK_NO_CLIENT_CB);
2914 memset(identity, 0, sizeof(identity));
2915 /* Allocate maximum size buffer */
2916 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2917 pms = OPENSSL_malloc(pmslen);
2921 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2922 identity, sizeof(identity) - 1,
2924 if (psk_len > PSK_MAX_PSK_LEN) {
2925 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2926 ERR_R_INTERNAL_ERROR);
2928 } else if (psk_len == 0) {
2929 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2930 SSL_R_PSK_IDENTITY_NOT_FOUND);
2933 /* Change pmslen to real length */
2934 pmslen = 2 + psk_len + 2 + psk_len;
2935 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2936 identity_len = strlen(identity);
2937 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2938 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2939 ERR_R_INTERNAL_ERROR);
2942 /* create PSK pre_master_secret */
2944 memmove(pms + psk_len + 4, pms, psk_len);
2946 memset(t, 0, psk_len);
2950 OPENSSL_free(s->session->psk_identity_hint);
2951 s->session->psk_identity_hint =
2952 BUF_strdup(s->ctx->psk_identity_hint);
2953 if (s->ctx->psk_identity_hint != NULL
2954 && s->session->psk_identity_hint == NULL) {
2955 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2956 ERR_R_MALLOC_FAILURE);
2960 OPENSSL_free(s->session->psk_identity);
2961 s->session->psk_identity = BUF_strdup(identity);
2962 if (s->session->psk_identity == NULL) {
2963 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2964 ERR_R_MALLOC_FAILURE);
2968 s2n(identity_len, p);
2969 memcpy(p, identity, identity_len);
2970 n = 2 + identity_len;
2973 OPENSSL_cleanse(identity, sizeof(identity));
2975 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2981 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2982 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2986 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2987 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2988 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2992 s->state = SSL3_ST_CW_KEY_EXCH_B;
2995 /* SSL3_ST_CW_KEY_EXCH_B */
2996 n = ssl_do_write(s);
2997 #ifndef OPENSSL_NO_SRP
2999 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3001 * If everything written generate master key: no need to save PMS as
3002 * SRP_generate_client_master_secret generates it internally.
3005 if ((s->session->master_key_length =
3006 SRP_generate_client_master_secret(s,
3007 s->session->master_key)) <
3009 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3010 ERR_R_INTERNAL_ERROR);
3016 /* If we haven't written everything save PMS */
3019 s->cert->pmslen = pmslen;
3021 /* If we don't have a PMS restore */
3024 pmslen = s->cert->pmslen;
3027 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3028 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3031 s->session->master_key_length =
3032 s->method->ssl3_enc->generate_master_secret(s,
3034 session->master_key,
3036 OPENSSL_clear_free(pms, pmslen);
3037 s->cert->pms = NULL;
3038 if (s->session->master_key_length < 0) {
3039 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3040 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3046 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3047 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3049 OPENSSL_clear_free(pms, pmslen);
3050 s->cert->pms = NULL;
3051 #ifndef OPENSSL_NO_EC
3052 BN_CTX_free(bn_ctx);
3053 OPENSSL_free(encodedPoint);
3054 EC_KEY_free(clnt_ecdh);
3055 EVP_PKEY_free(srvr_pub_pkey);
3057 s->state = SSL_ST_ERR;
3061 int ssl3_send_client_verify(SSL *s)
3064 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3066 EVP_PKEY_CTX *pctx = NULL;
3072 EVP_MD_CTX_init(&mctx);
3074 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3075 p = ssl_handshake_start(s);
3076 pkey = s->cert->key->privatekey;
3077 /* Create context from key and test if sha1 is allowed as digest */
3078 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3079 EVP_PKEY_sign_init(pctx);
3080 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3081 if (!SSL_USE_SIGALGS(s))
3082 s->method->ssl3_enc->cert_verify_mac(s,
3085 [MD5_DIGEST_LENGTH]));
3090 * For TLS v1.2 send signature algorithm and signature using agreed
3091 * digest and cached handshake records.
3093 if (SSL_USE_SIGALGS(s)) {
3096 const EVP_MD *md = s->cert->key->digest;
3097 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3098 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3099 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3104 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3107 if (!EVP_SignInit_ex(&mctx, md, NULL)
3108 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3109 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3110 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3116 * For extended master secret we've already digested cached
3119 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
3120 BIO_free(s->s3->handshake_buffer);
3121 s->s3->handshake_buffer = NULL;
3122 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3123 } else if (!ssl3_digest_cached_records(s))
3126 #ifndef OPENSSL_NO_RSA
3127 if (pkey->type == EVP_PKEY_RSA) {
3128 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3129 if (RSA_sign(NID_md5_sha1, data,
3130 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3131 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3132 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3139 #ifndef OPENSSL_NO_DSA
3140 if (pkey->type == EVP_PKEY_DSA) {
3141 if (!DSA_sign(pkey->save_type,
3142 &(data[MD5_DIGEST_LENGTH]),
3143 SHA_DIGEST_LENGTH, &(p[2]),
3144 (unsigned int *)&j, pkey->pkey.dsa)) {
3145 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3152 #ifndef OPENSSL_NO_EC
3153 if (pkey->type == EVP_PKEY_EC) {
3154 if (!ECDSA_sign(pkey->save_type,
3155 &(data[MD5_DIGEST_LENGTH]),
3156 SHA_DIGEST_LENGTH, &(p[2]),
3157 (unsigned int *)&j, pkey->pkey.ec)) {
3158 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3165 if (pkey->type == NID_id_GostR3410_94
3166 || pkey->type == NID_id_GostR3410_2001) {
3167 unsigned char signbuf[64];
3169 size_t sigsize = 64;
3170 s->method->ssl3_enc->cert_verify_mac(s,
3171 NID_id_GostR3411_94, data);
3172 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3173 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3176 for (i = 63, j = 0; i >= 0; j++, i--) {
3177 p[2 + j] = signbuf[i];
3182 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3185 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3186 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3189 s->state = SSL3_ST_CW_CERT_VRFY_B;
3191 EVP_MD_CTX_cleanup(&mctx);
3192 EVP_PKEY_CTX_free(pctx);
3193 return ssl_do_write(s);
3195 EVP_MD_CTX_cleanup(&mctx);
3196 EVP_PKEY_CTX_free(pctx);
3197 s->state = SSL_ST_ERR;
3202 * Check a certificate can be used for client authentication. Currently check
3203 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3204 * certificates can be used and optionally checks suitability for Suite B.
3206 static int ssl3_check_client_certificate(SSL *s)
3208 unsigned long alg_k;
3209 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3211 /* If no suitable signature algorithm can't use certificate */
3212 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3215 * If strict mode check suitability of chain before using it. This also
3216 * adjusts suite B digest if necessary.
3218 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3219 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3221 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3222 /* See if we can use client certificate for fixed DH */
3223 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3224 SESS_CERT *scert = s->session->sess_cert;
3225 int i = scert->peer_cert_type;
3226 EVP_PKEY *clkey = NULL, *spkey = NULL;
3227 clkey = s->cert->key->privatekey;
3228 /* If client key not DH assume it can be used */
3229 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3232 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3234 /* Compare server and client parameters */
3235 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3236 EVP_PKEY_free(spkey);
3240 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3245 int ssl3_send_client_certificate(SSL *s)
3248 EVP_PKEY *pkey = NULL;
3251 if (s->state == SSL3_ST_CW_CERT_A) {
3252 /* Let cert callback update client certificates if required */
3253 if (s->cert->cert_cb) {
3254 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3256 s->rwstate = SSL_X509_LOOKUP;
3260 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3261 s->state = SSL_ST_ERR;
3264 s->rwstate = SSL_NOTHING;
3266 if (ssl3_check_client_certificate(s))
3267 s->state = SSL3_ST_CW_CERT_C;
3269 s->state = SSL3_ST_CW_CERT_B;
3272 /* We need to get a client cert */
3273 if (s->state == SSL3_ST_CW_CERT_B) {
3275 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3276 * return(-1); We then get retied later
3279 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3281 s->rwstate = SSL_X509_LOOKUP;
3284 s->rwstate = SSL_NOTHING;
3285 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3286 s->state = SSL3_ST_CW_CERT_B;
3287 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3289 } else if (i == 1) {
3291 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3292 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3296 EVP_PKEY_free(pkey);
3297 if (i && !ssl3_check_client_certificate(s))
3300 if (s->version == SSL3_VERSION) {
3301 s->s3->tmp.cert_req = 0;
3302 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3305 s->s3->tmp.cert_req = 2;
3306 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3307 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3308 s->state = SSL_ST_ERR;
3314 /* Ok, we have a cert */
3315 s->state = SSL3_ST_CW_CERT_C;
3318 if (s->state == SSL3_ST_CW_CERT_C) {
3319 s->state = SSL3_ST_CW_CERT_D;
3320 if (!ssl3_output_cert_chain(s,
3321 (s->s3->tmp.cert_req ==
3322 2) ? NULL : s->cert->key)) {
3323 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3324 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3325 s->state = SSL_ST_ERR;
3329 /* SSL3_ST_CW_CERT_D */
3330 return ssl_do_write(s);
3333 #define has_bits(i,m) (((i)&(m)) == (m))
3335 int ssl3_check_cert_and_algorithm(SSL *s)
3339 EVP_PKEY *pkey = NULL;
3341 #ifndef OPENSSL_NO_RSA
3344 #ifndef OPENSSL_NO_DH
3348 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3349 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3351 /* we don't have a certificate */
3352 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3355 sc = s->session->sess_cert;
3357 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3360 #ifndef OPENSSL_NO_RSA
3361 rsa = s->session->sess_cert->peer_rsa_tmp;
3363 #ifndef OPENSSL_NO_DH
3364 dh = s->session->sess_cert->peer_dh_tmp;
3367 /* This is the passed certificate */
3369 idx = sc->peer_cert_type;
3370 #ifndef OPENSSL_NO_EC
3371 if (idx == SSL_PKEY_ECC) {
3372 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3374 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3379 } else if (alg_a & SSL_aECDSA) {
3380 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3381 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3383 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3384 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3388 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3389 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3390 EVP_PKEY_free(pkey);
3392 /* Check that we have a certificate if we require one */
3393 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3394 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3395 SSL_R_MISSING_RSA_SIGNING_CERT);
3398 #ifndef OPENSSL_NO_DSA
3399 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3400 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3401 SSL_R_MISSING_DSA_SIGNING_CERT);
3405 #ifndef OPENSSL_NO_RSA
3406 if ((alg_k & SSL_kRSA) &&
3407 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3408 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3409 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3413 #ifndef OPENSSL_NO_DH
3414 if ((alg_k & SSL_kDHE) &&
3415 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3416 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3418 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3419 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3420 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3421 SSL_R_MISSING_DH_RSA_CERT);
3424 # ifndef OPENSSL_NO_DSA
3425 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3426 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3427 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3428 SSL_R_MISSING_DH_DSA_CERT);
3434 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3435 #ifndef OPENSSL_NO_RSA
3436 if (alg_k & SSL_kRSA) {
3438 || RSA_size(rsa) * 8 >
3439 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3440 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3441 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3446 #ifndef OPENSSL_NO_DH
3447 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3449 || DH_size(dh) * 8 >
3450 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3451 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3452 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3458 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3459 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3465 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3470 #ifndef OPENSSL_NO_TLSEXT
3472 * Normally, we can tell if the server is resuming the session from
3473 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3474 * message after the ServerHello to determine if the server is resuming.
3475 * Therefore, we allow EAP-FAST to peek ahead.
3476 * ssl3_check_finished returns 1 if we are resuming from an external
3477 * pre-shared secret, we have a "ticket" and the next server handshake message
3478 * is Finished; and 0 otherwise. It returns -1 upon an error.
3480 static int ssl3_check_finished(SSL *s)
3484 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3485 !s->session->tlsext_tick)
3488 /* Need to permit this temporarily, in case the next message is Finished. */
3489 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3491 * This function is called when we might get a Certificate message instead,
3492 * so permit appropriate message length.
3493 * We ignore the return value as we're only interested in the message type
3494 * and not its length.
3496 s->method->ssl_get_message(s,
3499 -1, s->max_cert_list, &ok);
3500 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3505 s->s3->tmp.reuse_message = 1;
3507 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3510 /* If we're not done, then the CCS arrived early and we should bail. */
3511 if (s->s3->change_cipher_spec) {
3512 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3513 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3520 # ifndef OPENSSL_NO_NEXTPROTONEG
3521 int ssl3_send_next_proto(SSL *s)
3523 unsigned int len, padding_len;
3526 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3527 len = s->next_proto_negotiated_len;
3528 padding_len = 32 - ((len + 2) % 32);
3529 d = (unsigned char *)s->init_buf->data;
3531 memcpy(d + 5, s->next_proto_negotiated, len);
3532 d[5 + len] = padding_len;
3533 memset(d + 6 + len, 0, padding_len);
3534 *(d++) = SSL3_MT_NEXT_PROTO;
3535 l2n3(2 + len + padding_len, d);
3536 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3537 s->init_num = 4 + 2 + len + padding_len;
3541 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3546 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3549 #ifndef OPENSSL_NO_ENGINE
3550 if (s->ctx->client_cert_engine) {
3551 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3552 SSL_get_client_CA_list(s),
3553 px509, ppkey, NULL, NULL, NULL);
3558 if (s->ctx->client_cert_cb)
3559 i = s->ctx->client_cert_cb(s, px509, ppkey);
3563 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3565 int (*put_cb) (const SSL_CIPHER *,
3571 int empty_reneg_info_scsv = !s->renegotiate;
3572 /* Set disabled masks for this session */
3573 ssl_set_client_disabled(s);
3579 put_cb = s->method->put_cipher_by_char;
3581 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3582 c = sk_SSL_CIPHER_value(sk, i);
3583 /* Skip disabled ciphers */
3584 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3586 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3587 if (c->id == SSL3_CK_SCSV) {
3588 if (!empty_reneg_info_scsv)
3591 empty_reneg_info_scsv = 0;
3598 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3602 if (empty_reneg_info_scsv) {
3603 static SSL_CIPHER scsv = {
3604 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3606 j = put_cb(&scsv, p);
3608 #ifdef OPENSSL_RI_DEBUG
3610 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3613 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3614 static SSL_CIPHER scsv = {
3615 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3617 j = put_cb(&scsv, p);