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 "kssl_lcl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
159 #ifndef OPENSSL_NO_DH
160 # include <openssl/dh.h>
162 #include <openssl/bn.h>
163 #ifndef OPENSSL_NO_ENGINE
164 # include <openssl/engine.h>
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);
172 #ifndef OPENSSL_NO_SSL3_METHOD
173 static const SSL_METHOD *ssl3_get_client_method(int ver)
175 if (ver == SSL3_VERSION)
176 return (SSLv3_client_method());
181 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
182 ssl_undefined_function,
183 ssl3_connect, ssl3_get_client_method)
185 int ssl3_connect(SSL *s)
188 unsigned long Time = (unsigned long)time(NULL);
189 void (*cb) (const SSL *ssl, int type, int val) = NULL;
191 int new_state, state, skip = 0;
193 RAND_add(&Time, sizeof(Time), 0);
197 if (s->info_callback != NULL)
198 cb = s->info_callback;
199 else if (s->ctx->info_callback != NULL)
200 cb = s->ctx->info_callback;
203 if (!SSL_in_init(s) || SSL_in_before(s)) {
208 #ifndef OPENSSL_NO_HEARTBEATS
210 * If we're awaiting a HeartbeatResponse, pretend we already got and
211 * don't await it anymore, because Heartbeats don't make sense during
214 if (s->tlsext_hb_pending) {
215 s->tlsext_hb_pending = 0;
224 case SSL_ST_RENEGOTIATE:
226 s->state = SSL_ST_CONNECT;
227 s->ctx->stats.sess_connect_renegotiate++;
231 case SSL_ST_BEFORE | SSL_ST_CONNECT:
232 case SSL_ST_OK | SSL_ST_CONNECT:
236 cb(s, SSL_CB_HANDSHAKE_START, 1);
238 if ((s->version & 0xff00) != 0x0300) {
239 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
244 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
245 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
249 /* s->version=SSL3_VERSION; */
250 s->type = SSL_ST_CONNECT;
252 if (s->init_buf == NULL) {
253 if ((buf = BUF_MEM_new()) == NULL) {
257 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
265 if (!ssl3_setup_buffers(s)) {
270 /* setup buffing BIO */
271 if (!ssl_init_wbio_buffer(s, 0)) {
276 /* don't push the buffering BIO quite yet */
278 ssl3_init_finished_mac(s);
280 s->state = SSL3_ST_CW_CLNT_HELLO_A;
281 s->ctx->stats.sess_connect++;
283 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
285 * Should have been reset by ssl3_get_finished, too.
287 s->s3->change_cipher_spec = 0;
290 case SSL3_ST_CW_CLNT_HELLO_A:
291 case SSL3_ST_CW_CLNT_HELLO_B:
294 ret = ssl3_client_hello(s);
297 s->state = SSL3_ST_CR_SRVR_HELLO_A;
300 /* turn on buffering for the next lot of output */
301 if (s->bbio != s->wbio)
302 s->wbio = BIO_push(s->bbio, s->wbio);
306 case SSL3_ST_CR_SRVR_HELLO_A:
307 case SSL3_ST_CR_SRVR_HELLO_B:
308 ret = ssl3_get_server_hello(s);
313 s->state = SSL3_ST_CR_FINISHED_A;
314 #ifndef OPENSSL_NO_TLSEXT
315 if (s->tlsext_ticket_expected) {
316 /* receive renewed session ticket */
317 s->state = SSL3_ST_CR_SESSION_TICKET_A;
321 s->state = SSL3_ST_CR_CERT_A;
325 case SSL3_ST_CR_CERT_A:
326 case SSL3_ST_CR_CERT_B:
327 #ifndef OPENSSL_NO_TLSEXT
328 /* Noop (ret = 0) for everything but EAP-FAST. */
329 ret = ssl3_check_finished(s);
334 s->state = SSL3_ST_CR_FINISHED_A;
339 /* Check if it is anon DH/ECDH, SRP auth */
343 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
344 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
345 ret = ssl3_get_server_certificate(s);
348 #ifndef OPENSSL_NO_TLSEXT
349 if (s->tlsext_status_expected)
350 s->state = SSL3_ST_CR_CERT_STATUS_A;
352 s->state = SSL3_ST_CR_KEY_EXCH_A;
355 s->state = SSL3_ST_CR_KEY_EXCH_A;
361 s->state = SSL3_ST_CR_KEY_EXCH_A;
366 case SSL3_ST_CR_KEY_EXCH_A:
367 case SSL3_ST_CR_KEY_EXCH_B:
368 ret = ssl3_get_key_exchange(s);
371 s->state = SSL3_ST_CR_CERT_REQ_A;
375 * at this point we check that we have the required stuff from
378 if (!ssl3_check_cert_and_algorithm(s)) {
384 case SSL3_ST_CR_CERT_REQ_A:
385 case SSL3_ST_CR_CERT_REQ_B:
386 ret = ssl3_get_certificate_request(s);
389 s->state = SSL3_ST_CR_SRVR_DONE_A;
393 case SSL3_ST_CR_SRVR_DONE_A:
394 case SSL3_ST_CR_SRVR_DONE_B:
395 ret = ssl3_get_server_done(s);
398 #ifndef OPENSSL_NO_SRP
399 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
400 if ((ret = SRP_Calc_A_param(s)) <= 0) {
401 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
402 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
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)) {
497 if (!s->method->ssl3_enc->change_cipher_state(s,
498 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
506 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
507 case SSL3_ST_CW_NEXT_PROTO_A:
508 case SSL3_ST_CW_NEXT_PROTO_B:
509 ret = ssl3_send_next_proto(s);
512 s->state = SSL3_ST_CW_FINISHED_A;
516 case SSL3_ST_CW_FINISHED_A:
517 case SSL3_ST_CW_FINISHED_B:
518 ret = ssl3_send_finished(s,
519 SSL3_ST_CW_FINISHED_A,
520 SSL3_ST_CW_FINISHED_B,
522 ssl3_enc->client_finished_label,
524 ssl3_enc->client_finished_label_len);
527 s->state = SSL3_ST_CW_FLUSH;
530 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
532 s->s3->tmp.next_state = SSL_ST_OK;
533 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
534 s->state = SSL_ST_OK;
535 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
536 s->s3->delay_buf_pop_ret = 0;
539 #ifndef OPENSSL_NO_TLSEXT
541 * Allow NewSessionTicket if ticket expected
543 if (s->tlsext_ticket_expected)
544 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
548 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
553 #ifndef OPENSSL_NO_TLSEXT
554 case SSL3_ST_CR_SESSION_TICKET_A:
555 case SSL3_ST_CR_SESSION_TICKET_B:
556 ret = ssl3_get_new_session_ticket(s);
559 s->state = SSL3_ST_CR_FINISHED_A;
563 case SSL3_ST_CR_CERT_STATUS_A:
564 case SSL3_ST_CR_CERT_STATUS_B:
565 ret = ssl3_get_cert_status(s);
568 s->state = SSL3_ST_CR_KEY_EXCH_A;
573 case SSL3_ST_CR_FINISHED_A:
574 case SSL3_ST_CR_FINISHED_B:
575 if (!s->s3->change_cipher_spec)
576 s->s3->flags |= SSL3_FLAGS_CCS_OK;
577 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
578 SSL3_ST_CR_FINISHED_B);
583 s->state = SSL3_ST_CW_CHANGE_A;
585 s->state = SSL_ST_OK;
589 case SSL3_ST_CW_FLUSH:
590 s->rwstate = SSL_WRITING;
591 if (BIO_flush(s->wbio) <= 0) {
595 s->rwstate = SSL_NOTHING;
596 s->state = s->s3->tmp.next_state;
600 /* clean a few things up */
601 ssl3_cleanup_key_block(s);
603 if (s->init_buf != NULL) {
604 BUF_MEM_free(s->init_buf);
609 * If we are not 'joining' the last two packets, remove the
612 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
613 ssl_free_wbio_buffer(s);
614 /* else do it later in ssl3_write */
620 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
622 s->ctx->stats.sess_hit++;
626 s->handshake_func = ssl3_connect;
627 s->ctx->stats.sess_connect_good++;
630 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;
663 cb(s, SSL_CB_CONNECT_EXIT, ret);
667 int ssl3_client_hello(SSL *s)
670 unsigned char *p, *d;
674 #ifndef OPENSSL_NO_COMP
679 buf = (unsigned char *)s->init_buf->data;
680 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
681 SSL_SESSION *sess = s->session;
682 if ((sess == NULL) || (sess->ssl_version != s->version) ||
683 #ifdef OPENSSL_NO_TLSEXT
684 !sess->session_id_length ||
687 * In the case of EAP-FAST, we can have a pre-shared
688 * "ticket" without a session ID.
690 (!sess->session_id_length && !sess->tlsext_tick) ||
692 (sess->not_resumable)) {
693 if (!ssl_get_new_session(s, 0))
696 if (s->method->version == DTLS_ANY_VERSION) {
697 /* Determine which DTLS version to use */
698 int options = s->options;
699 /* If DTLS 1.2 disabled correct the version number */
700 if (options & SSL_OP_NO_DTLSv1_2) {
701 if (tls1_suiteb(s)) {
702 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
703 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
707 * Disabling all versions is silly: return an error.
709 if (options & SSL_OP_NO_DTLSv1) {
710 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
714 * Update method so we don't use any DTLS 1.2 features.
716 s->method = DTLSv1_client_method();
717 s->version = DTLS1_VERSION;
720 * We only support one version: update method
722 if (options & SSL_OP_NO_DTLSv1)
723 s->method = DTLSv1_2_client_method();
724 s->version = DTLS1_2_VERSION;
726 s->client_version = s->version;
728 /* else use the pre-loaded session */
730 p = s->s3->client_random;
733 * for DTLS if client_random is initialized, reuse it, we are
734 * required to use same upon reply to HelloVerify
736 if (SSL_IS_DTLS(s)) {
739 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
748 if (i && ssl_fill_hello_random(s, 0, p,
749 sizeof(s->s3->client_random)) <= 0)
752 /* Do the message type and length last */
753 d = p = ssl_handshake_start(s);
756 * version indicates the negotiated version: for example from
757 * an SSLv2/v3 compatible client hello). The client_version
758 * field is the maximum version we permit and it is also
759 * used in RSA encrypted premaster secrets. Some servers can
760 * choke if we initially report a higher version then
761 * renegotiate to a lower one in the premaster secret. This
762 * didn't happen with TLS 1.0 as most servers supported it
763 * but it can with TLS 1.1 or later if the server only supports
766 * Possible scenario with previous logic:
767 * 1. Client hello indicates TLS 1.2
768 * 2. Server hello says TLS 1.0
769 * 3. RSA encrypted premaster secret uses 1.2.
770 * 4. Handhaked proceeds using TLS 1.0.
771 * 5. Server sends hello request to renegotiate.
772 * 6. Client hello indicates TLS v1.0 as we now
773 * know that is maximum server supports.
774 * 7. Server chokes on RSA encrypted premaster secret
775 * containing version 1.0.
777 * For interoperability it should be OK to always use the
778 * maximum version we support in client hello and then rely
779 * on the checking of version to ensure the servers isn't
780 * being inconsistent: for example initially negotiating with
781 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
782 * client_version in client hello and not resetting it to
783 * the negotiated version.
785 *(p++) = s->client_version >> 8;
786 *(p++) = s->client_version & 0xff;
789 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
790 p += SSL3_RANDOM_SIZE;
796 i = s->session->session_id_length;
799 if (i > (int)sizeof(s->session->session_id)) {
800 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
803 memcpy(p, s->session->session_id, i);
807 /* cookie stuff for DTLS */
808 if (SSL_IS_DTLS(s)) {
809 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
810 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
813 *(p++) = s->d1->cookie_len;
814 memcpy(p, s->d1->cookie, s->d1->cookie_len);
815 p += s->d1->cookie_len;
818 /* Ciphers supported */
819 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
821 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
824 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
826 * Some servers hang if client hello > 256 bytes as hack workaround
827 * chop number of supported ciphers to keep it well below this if we
830 if (TLS1_get_version(s) >= TLS1_2_VERSION
831 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
832 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
838 #ifdef OPENSSL_NO_COMP
842 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
845 j = sk_SSL_COMP_num(s->ctx->comp_methods);
847 for (i = 0; i < j; i++) {
848 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
852 *(p++) = 0; /* Add the NULL method */
854 #ifndef OPENSSL_NO_TLSEXT
856 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
857 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
861 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
863 ssl3_send_alert(s, SSL3_AL_FATAL, al);
864 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
870 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
871 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
872 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
875 s->state = SSL3_ST_CW_CLNT_HELLO_B;
878 /* SSL3_ST_CW_CLNT_HELLO_B */
879 return ssl_do_write(s);
884 int ssl3_get_server_hello(SSL *s)
886 STACK_OF(SSL_CIPHER) *sk;
889 unsigned char *p, *d;
890 int i, al = SSL_AD_INTERNAL_ERROR, ok;
893 #ifndef OPENSSL_NO_COMP
897 * Hello verify request and/or server hello version may not match so set
898 * first packet if we're negotiating version.
903 n = s->method->ssl_get_message(s,
904 SSL3_ST_CR_SRVR_HELLO_A,
905 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
910 if (SSL_IS_DTLS(s)) {
912 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
913 if (s->d1->send_cookie == 0) {
914 s->s3->tmp.reuse_message = 1;
916 } else { /* already sent a cookie */
918 al = SSL_AD_UNEXPECTED_MESSAGE;
919 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
925 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
926 al = SSL_AD_UNEXPECTED_MESSAGE;
927 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
931 d = p = (unsigned char *)s->init_msg;
932 if (s->method->version == DTLS_ANY_VERSION) {
933 /* Work out correct protocol version to use */
934 int hversion = (p[0] << 8) | p[1];
935 int options = s->options;
936 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
937 s->method = DTLSv1_2_client_method();
938 else if (tls1_suiteb(s)) {
939 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
940 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
941 s->version = hversion;
942 al = SSL_AD_PROTOCOL_VERSION;
944 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
945 s->method = DTLSv1_client_method();
947 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
948 s->version = hversion;
949 al = SSL_AD_PROTOCOL_VERSION;
952 s->version = s->method->version;
955 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
956 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
957 s->version = (s->version & 0xff00) | p[1];
958 al = SSL_AD_PROTOCOL_VERSION;
963 /* load the server hello data */
964 /* load the server random */
965 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
966 p += SSL3_RANDOM_SIZE;
970 /* get the session-id */
973 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
974 al = SSL_AD_ILLEGAL_PARAMETER;
975 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
978 #ifndef OPENSSL_NO_TLSEXT
980 * Check if we can resume the session based on external pre-shared secret.
981 * EAP-FAST (RFC 4851) supports two types of session resumption.
982 * Resumption based on server-side state works with session IDs.
983 * Resumption based on pre-shared Protected Access Credentials (PACs)
984 * works by overriding the SessionTicket extension at the application
985 * layer, and does not send a session ID. (We do not know whether EAP-FAST
986 * servers would honour the session ID.) Therefore, the session ID alone
987 * is not a reliable indicator of session resumption, so we first check if
988 * we can resume, and later peek at the next handshake message to see if the
989 * server wants to resume.
991 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
992 s->session->tlsext_tick) {
993 SSL_CIPHER *pref_cipher = NULL;
994 s->session->master_key_length = sizeof(s->session->master_key);
995 if (s->tls_session_secret_cb(s, s->session->master_key,
996 &s->session->master_key_length,
998 s->tls_session_secret_cb_arg)) {
999 s->session->cipher = pref_cipher ?
1000 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1002 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1003 al = SSL_AD_INTERNAL_ERROR;
1007 #endif /* OPENSSL_NO_TLSEXT */
1009 if (j != 0 && j == s->session->session_id_length
1010 && memcmp(p, s->session->session_id, j) == 0) {
1011 if (s->sid_ctx_length != s->session->sid_ctx_length
1012 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1013 /* actually a client application bug */
1014 al = SSL_AD_ILLEGAL_PARAMETER;
1015 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1016 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1022 * If we were trying for session-id reuse but the server
1023 * didn't echo the ID, make a new SSL_SESSION.
1024 * In the case of EAP-FAST and PAC, we do not send a session ID,
1025 * so the PAC-based session secret is always preserved. It'll be
1026 * overwritten if the server refuses resumption.
1028 if (s->session->session_id_length > 0) {
1029 if (!ssl_get_new_session(s, 0)) {
1033 s->session->session_id_length = j;
1034 memcpy(s->session->session_id, p, j); /* j could be 0 */
1037 c = ssl_get_cipher_by_char(s, p);
1039 /* unknown cipher */
1040 al = SSL_AD_ILLEGAL_PARAMETER;
1041 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1044 /* Set version disabled mask now we know version */
1045 if (!SSL_USE_TLS1_2_CIPHERS(s))
1046 ct->mask_ssl = SSL_TLSV1_2;
1050 * If it is a disabled cipher we didn't send it in client hello, so
1053 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1054 al = SSL_AD_ILLEGAL_PARAMETER;
1055 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1058 p += ssl_put_cipher_by_char(s, NULL, NULL);
1060 sk = ssl_get_ciphers_by_id(s);
1061 i = sk_SSL_CIPHER_find(sk, c);
1063 /* we did not say we would use this cipher */
1064 al = SSL_AD_ILLEGAL_PARAMETER;
1065 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1070 * Depending on the session caching (internal/external), the cipher
1071 * and/or cipher_id values may not be set. Make sure that cipher_id is
1072 * set and use it for comparison.
1074 if (s->session->cipher)
1075 s->session->cipher_id = s->session->cipher->id;
1076 if (s->hit && (s->session->cipher_id != c->id)) {
1077 al = SSL_AD_ILLEGAL_PARAMETER;
1078 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1079 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1082 s->s3->tmp.new_cipher = c;
1084 * Don't digest cached records if no sigalgs: we may need them for client
1087 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1089 /* lets get the compression algorithm */
1091 #ifdef OPENSSL_NO_COMP
1093 al = SSL_AD_ILLEGAL_PARAMETER;
1094 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1095 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1099 * If compression is disabled we'd better not try to resume a session
1100 * using compression.
1102 if (s->session->compress_meth != 0) {
1103 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1108 if (s->hit && j != s->session->compress_meth) {
1109 al = SSL_AD_ILLEGAL_PARAMETER;
1110 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1111 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1116 else if (!ssl_allow_compression(s)) {
1117 al = SSL_AD_ILLEGAL_PARAMETER;
1118 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1121 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1123 if ((j != 0) && (comp == NULL)) {
1124 al = SSL_AD_ILLEGAL_PARAMETER;
1125 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1126 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1129 s->s3->tmp.new_compression = comp;
1133 #ifndef OPENSSL_NO_TLSEXT
1134 /* TLS extensions */
1135 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1136 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1142 /* wrong packet length */
1143 al = SSL_AD_DECODE_ERROR;
1144 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1150 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1155 int ssl3_get_server_certificate(SSL *s)
1157 int al, i, ok, ret = -1;
1158 unsigned long n, nc, llen, l;
1160 const unsigned char *q, *p;
1162 STACK_OF(X509) *sk = NULL;
1164 EVP_PKEY *pkey = NULL;
1165 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1168 n = s->method->ssl_get_message(s,
1171 -1, s->max_cert_list, &ok);
1176 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1177 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1178 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1179 s->s3->tmp.reuse_message = 1;
1183 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1184 al = SSL_AD_UNEXPECTED_MESSAGE;
1185 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1188 p = d = (unsigned char *)s->init_msg;
1190 if ((sk = sk_X509_new_null()) == NULL) {
1191 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1196 if (llen + 3 != n) {
1197 al = SSL_AD_DECODE_ERROR;
1198 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1201 for (nc = 0; nc < llen;) {
1203 if ((l + nc + 3) > llen) {
1204 al = SSL_AD_DECODE_ERROR;
1205 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1206 SSL_R_CERT_LENGTH_MISMATCH);
1211 x = d2i_X509(NULL, &q, l);
1213 al = SSL_AD_BAD_CERTIFICATE;
1214 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1218 al = SSL_AD_DECODE_ERROR;
1219 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1220 SSL_R_CERT_LENGTH_MISMATCH);
1223 if (!sk_X509_push(sk, x)) {
1224 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1232 i = ssl_verify_cert_chain(s, sk);
1233 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1234 #ifndef OPENSSL_NO_KRB5
1235 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1236 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1237 #endif /* OPENSSL_NO_KRB5 */
1239 al = ssl_verify_alarm_type(s->verify_result);
1240 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1241 SSL_R_CERTIFICATE_VERIFY_FAILED);
1244 ERR_clear_error(); /* but we keep s->verify_result */
1246 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1247 al = SSL_AD_HANDSHAKE_FAILURE;
1251 sc = ssl_sess_cert_new();
1255 ssl_sess_cert_free(s->session->sess_cert);
1256 s->session->sess_cert = sc;
1258 sc->cert_chain = sk;
1260 * Inconsistency alert: cert_chain does include the peer's certificate,
1261 * which we don't include in s3_srvr.c
1263 x = sk_X509_value(sk, 0);
1266 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1269 pkey = X509_get_pubkey(x);
1271 /* VRS: allow null cert if auth == KRB5 */
1272 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1273 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1277 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1278 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1279 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1280 s->s3->tmp.new_cipher->name,
1281 s->s3->tmp.new_cipher->algorithm_mkey,
1282 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1283 #endif /* KSSL_DEBUG */
1285 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1288 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1289 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1293 i = ssl_cert_type(x, pkey);
1294 if (need_cert && i < 0) {
1297 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1298 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1303 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1304 if (exp_idx >= 0 && i != exp_idx) {
1306 al = SSL_AD_ILLEGAL_PARAMETER;
1307 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1308 SSL_R_WRONG_CERTIFICATE_TYPE);
1311 sc->peer_cert_type = i;
1312 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1314 * Why would the following ever happen? We just created sc a couple
1317 X509_free(sc->peer_pkeys[i].x509);
1318 sc->peer_pkeys[i].x509 = x;
1319 sc->peer_key = &(sc->peer_pkeys[i]);
1321 X509_free(s->session->peer);
1322 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1323 s->session->peer = x;
1325 sc->peer_cert_type = i;
1326 sc->peer_key = NULL;
1328 X509_free(s->session->peer);
1329 s->session->peer = NULL;
1331 s->session->verify_result = s->verify_result;
1337 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1340 EVP_PKEY_free(pkey);
1342 sk_X509_pop_free(sk, X509_free);
1346 int ssl3_get_key_exchange(SSL *s)
1348 #ifndef OPENSSL_NO_RSA
1349 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1352 unsigned char *param, *p;
1354 long i, param_len, n, alg_k, alg_a;
1355 EVP_PKEY *pkey = NULL;
1356 const EVP_MD *md = NULL;
1357 #ifndef OPENSSL_NO_RSA
1360 #ifndef OPENSSL_NO_DH
1363 #ifndef OPENSSL_NO_EC
1364 EC_KEY *ecdh = NULL;
1365 BN_CTX *bn_ctx = NULL;
1366 EC_POINT *srvr_ecpoint = NULL;
1368 int encoded_pt_len = 0;
1371 EVP_MD_CTX_init(&md_ctx);
1374 * use same message size as in ssl3_get_certificate_request() as
1375 * ServerKeyExchange message may be skipped
1377 n = s->method->ssl_get_message(s,
1378 SSL3_ST_CR_KEY_EXCH_A,
1379 SSL3_ST_CR_KEY_EXCH_B,
1380 -1, s->max_cert_list, &ok);
1384 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1386 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1388 * Can't skip server key exchange if this is an ephemeral
1391 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1392 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1393 al = SSL_AD_UNEXPECTED_MESSAGE;
1396 #ifndef OPENSSL_NO_PSK
1398 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1399 * identity hint is sent. Set session->sess_cert anyway to avoid
1402 if (alg_k & SSL_kPSK) {
1403 s->session->sess_cert = ssl_sess_cert_new();
1404 if (s->ctx->psk_identity_hint)
1405 OPENSSL_free(s->ctx->psk_identity_hint);
1406 s->ctx->psk_identity_hint = NULL;
1409 s->s3->tmp.reuse_message = 1;
1413 param = p = (unsigned char *)s->init_msg;
1414 if (s->session->sess_cert != NULL) {
1415 #ifndef OPENSSL_NO_RSA
1416 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1417 s->session->sess_cert->peer_rsa_tmp = NULL;
1419 #ifndef OPENSSL_NO_DH
1420 DH_free(s->session->sess_cert->peer_dh_tmp);
1421 s->session->sess_cert->peer_dh_tmp = NULL;
1423 #ifndef OPENSSL_NO_EC
1424 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1425 s->session->sess_cert->peer_ecdh_tmp = NULL;
1428 s->session->sess_cert = ssl_sess_cert_new();
1431 /* Total length of the parameters including the length prefix */
1434 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1436 al = SSL_AD_DECODE_ERROR;
1438 #ifndef OPENSSL_NO_PSK
1439 if (alg_k & SSL_kPSK) {
1440 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1443 if (param_len > n) {
1444 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1450 * Store PSK identity hint for later use, hint is used in
1451 * ssl3_send_client_key_exchange. Assume that the maximum length of
1452 * a PSK identity hint can be as long as the maximum length of a PSK
1455 if (i > PSK_MAX_IDENTITY_LEN) {
1456 al = SSL_AD_HANDSHAKE_FAILURE;
1457 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1460 if (i > n - param_len) {
1461 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1462 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1468 * If received PSK identity hint contains NULL characters, the hint
1469 * is truncated from the first NULL. p may not be ending with NULL,
1470 * so create a NULL-terminated string.
1472 memcpy(tmp_id_hint, p, i);
1473 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1474 if (s->ctx->psk_identity_hint != NULL)
1475 OPENSSL_free(s->ctx->psk_identity_hint);
1476 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1477 if (s->ctx->psk_identity_hint == NULL) {
1478 al = SSL_AD_HANDSHAKE_FAILURE;
1479 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1486 #endif /* !OPENSSL_NO_PSK */
1487 #ifndef OPENSSL_NO_SRP
1488 if (alg_k & SSL_kSRP) {
1490 if (param_len > n) {
1491 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1496 if (i > n - param_len) {
1497 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1502 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1503 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1508 if (2 > n - param_len) {
1509 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1516 if (i > n - param_len) {
1517 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1522 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1528 if (1 > n - param_len) {
1529 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1534 i = (unsigned int)(p[0]);
1537 if (i > n - param_len) {
1538 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1543 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1544 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1549 if (2 > n - param_len) {
1550 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1557 if (i > n - param_len) {
1558 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1563 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1564 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1570 if (!srp_verify_server_param(s, &al)) {
1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1575 /* We must check if there is a certificate */
1576 # ifndef OPENSSL_NO_RSA
1577 if (alg_a & SSL_aRSA)
1579 X509_get_pubkey(s->session->
1580 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1584 # ifndef OPENSSL_NO_DSA
1585 else if (alg_a & SSL_aDSS)
1587 X509_get_pubkey(s->session->
1588 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1592 #endif /* !OPENSSL_NO_SRP */
1593 #ifndef OPENSSL_NO_RSA
1594 if (alg_k & SSL_kRSA) {
1595 /* Temporary RSA keys only allowed in export ciphersuites */
1596 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1597 al = SSL_AD_UNEXPECTED_MESSAGE;
1598 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1601 if ((rsa = RSA_new()) == NULL) {
1602 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1607 if (param_len > n) {
1608 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1613 if (i > n - param_len) {
1614 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1619 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1620 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1625 if (2 > n - param_len) {
1626 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1633 if (i > n - param_len) {
1634 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1639 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1640 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1646 /* this should be because we are using an export cipher */
1647 if (alg_a & SSL_aRSA)
1649 X509_get_pubkey(s->session->
1650 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1652 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1655 s->session->sess_cert->peer_rsa_tmp = rsa;
1658 #else /* OPENSSL_NO_RSA */
1661 #ifndef OPENSSL_NO_DH
1662 else if (alg_k & SSL_kDHE) {
1663 if ((dh = DH_new()) == NULL) {
1664 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1669 if (param_len > n) {
1670 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1675 if (i > n - param_len) {
1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1681 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1682 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1687 if (2 > n - param_len) {
1688 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1695 if (i > n - param_len) {
1696 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1701 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1702 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1707 if (2 > n - param_len) {
1708 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1715 if (i > n - param_len) {
1716 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1721 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1722 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1728 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1729 al = SSL_AD_HANDSHAKE_FAILURE;
1730 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1733 # ifndef OPENSSL_NO_RSA
1734 if (alg_a & SSL_aRSA)
1736 X509_get_pubkey(s->session->
1737 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1741 # ifndef OPENSSL_NO_DSA
1742 else if (alg_a & SSL_aDSS)
1744 X509_get_pubkey(s->session->
1745 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1748 /* else anonymous DH, so no certificate or pkey. */
1750 s->session->sess_cert->peer_dh_tmp = dh;
1753 #endif /* !OPENSSL_NO_DH */
1755 #ifndef OPENSSL_NO_EC
1756 else if (alg_k & SSL_kECDHE) {
1758 const EC_GROUP *group;
1760 if ((ecdh = EC_KEY_new()) == NULL) {
1761 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1766 * Extract elliptic curve parameters and the server's ephemeral ECDH
1767 * public key. Keep accumulating lengths of various components in
1768 * param_len and make sure it never exceeds n.
1772 * XXX: For now we only support named (not generic) curves and the
1773 * ECParameters in this case is just three bytes. We also need one
1774 * byte for the length of the encoded point
1777 if (param_len > n) {
1778 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1782 * Check curve is one of our preferences, if not server has sent an
1783 * invalid curve. ECParameters is 3 bytes.
1785 if (!tls1_check_curve(s, p, 3)) {
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1790 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1791 al = SSL_AD_INTERNAL_ERROR;
1792 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1793 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1797 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1798 if (ngroup == NULL) {
1799 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1802 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1803 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1806 EC_GROUP_free(ngroup);
1808 group = EC_KEY_get0_group(ecdh);
1810 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1811 (EC_GROUP_get_degree(group) > 163)) {
1812 al = SSL_AD_EXPORT_RESTRICTION;
1813 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1814 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1820 /* Next, get the encoded ECPoint */
1821 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1822 ((bn_ctx = BN_CTX_new()) == NULL)) {
1823 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1827 encoded_pt_len = *p; /* length of encoded point */
1830 if ((encoded_pt_len > n - param_len) ||
1831 (EC_POINT_oct2point(group, srvr_ecpoint,
1832 p, encoded_pt_len, bn_ctx) == 0)) {
1833 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1836 param_len += encoded_pt_len;
1839 p += encoded_pt_len;
1842 * The ECC/TLS specification does not mention the use of DSA to sign
1843 * ECParameters in the server key exchange message. We do support RSA
1847 # ifndef OPENSSL_NO_RSA
1848 else if (alg_a & SSL_aRSA)
1850 X509_get_pubkey(s->session->
1851 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1853 # ifndef OPENSSL_NO_EC
1854 else if (alg_a & SSL_aECDSA)
1856 X509_get_pubkey(s->session->
1857 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1859 /* else anonymous ECDH, so no certificate or pkey. */
1860 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1861 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1863 BN_CTX_free(bn_ctx);
1865 EC_POINT_free(srvr_ecpoint);
1866 srvr_ecpoint = NULL;
1868 al = SSL_AD_UNEXPECTED_MESSAGE;
1869 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1872 #endif /* !OPENSSL_NO_EC */
1874 /* p points to the next byte, there are 'n' bytes left */
1876 /* if it was signed, check the signature */
1878 if (SSL_USE_SIGALGS(s)) {
1881 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1884 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1891 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1899 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1904 j = EVP_PKEY_size(pkey);
1907 * Check signature length. If n is 0 then signature is empty
1909 if ((i != n) || (n > j) || (n <= 0)) {
1910 /* wrong packet length */
1911 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1914 #ifndef OPENSSL_NO_RSA
1915 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1921 for (num = 2; num > 0; num--) {
1922 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1923 EVP_DigestInit_ex(&md_ctx, (num == 2)
1924 ? s->ctx->md5 : s->ctx->sha1, NULL);
1925 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1927 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1929 EVP_DigestUpdate(&md_ctx, param, param_len);
1930 EVP_DigestFinal_ex(&md_ctx, q, &size);
1934 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1936 al = SSL_AD_DECRYPT_ERROR;
1937 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1942 al = SSL_AD_DECRYPT_ERROR;
1943 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1949 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1950 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1952 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1954 EVP_VerifyUpdate(&md_ctx, param, param_len);
1955 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1957 al = SSL_AD_DECRYPT_ERROR;
1958 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1963 /* aNULL, aSRP or kPSK do not need public keys */
1964 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1965 /* Might be wrong key type, check it */
1966 if (ssl3_check_cert_and_algorithm(s))
1967 /* Otherwise this shouldn't happen */
1968 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1971 /* still data left over */
1973 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1977 EVP_PKEY_free(pkey);
1978 EVP_MD_CTX_cleanup(&md_ctx);
1981 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1983 EVP_PKEY_free(pkey);
1984 #ifndef OPENSSL_NO_RSA
1987 #ifndef OPENSSL_NO_DH
1990 #ifndef OPENSSL_NO_EC
1991 BN_CTX_free(bn_ctx);
1992 EC_POINT_free(srvr_ecpoint);
1995 EVP_MD_CTX_cleanup(&md_ctx);
1999 int ssl3_get_certificate_request(SSL *s)
2002 unsigned long n, nc, l;
2003 unsigned int llen, ctype_num, i;
2004 X509_NAME *xn = NULL;
2005 const unsigned char *p, *q;
2007 STACK_OF(X509_NAME) *ca_sk = NULL;
2009 n = s->method->ssl_get_message(s,
2010 SSL3_ST_CR_CERT_REQ_A,
2011 SSL3_ST_CR_CERT_REQ_B,
2012 -1, s->max_cert_list, &ok);
2017 s->s3->tmp.cert_req = 0;
2019 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2020 s->s3->tmp.reuse_message = 1;
2022 * If we get here we don't need any cached handshake records as we
2023 * wont be doing client auth.
2025 if (s->s3->handshake_buffer) {
2026 if (!ssl3_digest_cached_records(s))
2032 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2034 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2038 /* TLS does not like anon-DH with client cert */
2039 if (s->version > SSL3_VERSION) {
2040 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2041 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2042 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2043 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2048 p = d = (unsigned char *)s->init_msg;
2050 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2051 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2055 /* get the certificate types */
2057 if (s->cert->ctypes) {
2058 OPENSSL_free(s->cert->ctypes);
2059 s->cert->ctypes = NULL;
2061 if (ctype_num > SSL3_CT_NUMBER) {
2062 /* If we exceed static buffer copy all to cert structure */
2063 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2064 if (s->cert->ctypes == NULL) {
2065 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2068 memcpy(s->cert->ctypes, p, ctype_num);
2069 s->cert->ctype_num = (size_t)ctype_num;
2070 ctype_num = SSL3_CT_NUMBER;
2072 for (i = 0; i < ctype_num; i++)
2073 s->s3->tmp.ctype[i] = p[i];
2075 if (SSL_USE_SIGALGS(s)) {
2078 * Check we have enough room for signature algorithms and following
2081 if ((unsigned long)(p - d + llen + 2) > n) {
2082 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2083 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2084 SSL_R_DATA_LENGTH_TOO_LONG);
2087 /* Clear certificate digests and validity flags */
2088 for (i = 0; i < SSL_PKEY_NUM; i++) {
2089 s->cert->pkeys[i].digest = NULL;
2090 s->cert->pkeys[i].valid_flags = 0;
2092 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2093 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2094 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2095 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2098 if (!tls1_process_sigalgs(s)) {
2099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2100 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2106 /* get the CA RDNs */
2109 if ((unsigned long)(p - d + llen) != n) {
2110 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2111 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2115 for (nc = 0; nc < llen;) {
2117 if ((l + nc + 2) > llen) {
2118 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2119 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2125 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2126 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2127 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2132 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2133 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2134 SSL_R_CA_DN_LENGTH_MISMATCH);
2137 if (!sk_X509_NAME_push(ca_sk, xn)) {
2138 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2146 /* we should setup a certificate to return.... */
2147 s->s3->tmp.cert_req = 1;
2148 s->s3->tmp.ctype_num = ctype_num;
2149 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2150 s->s3->tmp.ca_names = ca_sk;
2155 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2159 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2161 return (X509_NAME_cmp(*a, *b));
2164 #ifndef OPENSSL_NO_TLSEXT
2165 int ssl3_get_new_session_ticket(SSL *s)
2167 int ok, al, ret = 0, ticklen;
2169 const unsigned char *p;
2172 n = s->method->ssl_get_message(s,
2173 SSL3_ST_CR_SESSION_TICKET_A,
2174 SSL3_ST_CR_SESSION_TICKET_B,
2175 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2181 /* need at least ticket_lifetime_hint + ticket length */
2182 al = SSL_AD_DECODE_ERROR;
2183 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2187 p = d = (unsigned char *)s->init_msg;
2188 n2l(p, s->session->tlsext_tick_lifetime_hint);
2190 /* ticket_lifetime_hint + ticket_length + ticket */
2191 if (ticklen + 6 != n) {
2192 al = SSL_AD_DECODE_ERROR;
2193 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2196 if (s->session->tlsext_tick) {
2197 OPENSSL_free(s->session->tlsext_tick);
2198 s->session->tlsext_ticklen = 0;
2200 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2201 if (!s->session->tlsext_tick) {
2202 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2205 memcpy(s->session->tlsext_tick, p, ticklen);
2206 s->session->tlsext_ticklen = ticklen;
2208 * There are two ways to detect a resumed ticket session. One is to set
2209 * an appropriate session ID and then the server must return a match in
2210 * ServerHello. This allows the normal client session ID matching to work
2211 * and we know much earlier that the ticket has been accepted. The
2212 * other way is to set zero length session ID when the ticket is
2213 * presented and rely on the handshake to determine session resumption.
2214 * We choose the former approach because this fits in with assumptions
2215 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2216 * SHA256 is disabled) hash of the ticket.
2218 EVP_Digest(p, ticklen,
2219 s->session->session_id, &s->session->session_id_length,
2220 EVP_sha256(), NULL);
2224 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2229 int ssl3_get_cert_status(SSL *s)
2232 unsigned long resplen, n;
2233 const unsigned char *p;
2235 n = s->method->ssl_get_message(s,
2236 SSL3_ST_CR_CERT_STATUS_A,
2237 SSL3_ST_CR_CERT_STATUS_B,
2238 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2243 /* need at least status type + length */
2244 al = SSL_AD_DECODE_ERROR;
2245 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2248 p = (unsigned char *)s->init_msg;
2249 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2250 al = SSL_AD_DECODE_ERROR;
2251 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2255 if (resplen + 4 != n) {
2256 al = SSL_AD_DECODE_ERROR;
2257 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2260 if (s->tlsext_ocsp_resp)
2261 OPENSSL_free(s->tlsext_ocsp_resp);
2262 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2263 if (!s->tlsext_ocsp_resp) {
2264 al = SSL_AD_INTERNAL_ERROR;
2265 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2268 s->tlsext_ocsp_resplen = resplen;
2269 if (s->ctx->tlsext_status_cb) {
2271 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2273 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2274 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2278 al = SSL_AD_INTERNAL_ERROR;
2279 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2285 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2290 int ssl3_get_server_done(SSL *s)
2295 /* Second to last param should be very small, like 0 :-) */
2296 n = s->method->ssl_get_message(s,
2297 SSL3_ST_CR_SRVR_DONE_A,
2298 SSL3_ST_CR_SRVR_DONE_B,
2299 SSL3_MT_SERVER_DONE, 30, &ok);
2304 /* should contain no data */
2305 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2306 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2313 int ssl3_send_client_key_exchange(SSL *s)
2317 unsigned long alg_k;
2318 #ifndef OPENSSL_NO_RSA
2320 EVP_PKEY *pkey = NULL;
2322 #ifndef OPENSSL_NO_KRB5
2324 #endif /* OPENSSL_NO_KRB5 */
2325 #ifndef OPENSSL_NO_EC
2326 EC_KEY *clnt_ecdh = NULL;
2327 const EC_POINT *srvr_ecpoint = NULL;
2328 EVP_PKEY *srvr_pub_pkey = NULL;
2329 unsigned char *encodedPoint = NULL;
2330 int encoded_pt_len = 0;
2331 BN_CTX *bn_ctx = NULL;
2333 unsigned char *pms = NULL;
2336 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2337 p = ssl_handshake_start(s);
2339 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2341 /* Fool emacs indentation */
2344 #ifndef OPENSSL_NO_RSA
2345 else if (alg_k & SSL_kRSA) {
2347 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2348 pms = OPENSSL_malloc(pmslen);
2352 if (s->session->sess_cert == NULL) {
2354 * We should always have a server certificate with SSL_kRSA.
2356 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2357 ERR_R_INTERNAL_ERROR);
2361 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2362 rsa = s->session->sess_cert->peer_rsa_tmp;
2365 X509_get_pubkey(s->session->
2366 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2368 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2369 || (pkey->pkey.rsa == NULL)) {
2370 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2371 ERR_R_INTERNAL_ERROR);
2374 rsa = pkey->pkey.rsa;
2375 EVP_PKEY_free(pkey);
2378 pms[0] = s->client_version >> 8;
2379 pms[1] = s->client_version & 0xff;
2380 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2384 /* Fix buf for TLS and beyond */
2385 if (s->version > SSL3_VERSION)
2387 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2389 if (s->options & SSL_OP_PKCS1_CHECK_1)
2391 if (s->options & SSL_OP_PKCS1_CHECK_2)
2395 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2396 SSL_R_BAD_RSA_ENCRYPT);
2400 /* Fix buf for TLS and beyond */
2401 if (s->version > SSL3_VERSION) {
2407 #ifndef OPENSSL_NO_KRB5
2408 else if (alg_k & SSL_kKRB5) {
2409 krb5_error_code krb5rc;
2410 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2411 /* krb5_data krb5_ap_req; */
2412 krb5_data *enc_ticket;
2413 krb5_data authenticator, *authp = NULL;
2414 EVP_CIPHER_CTX ciph_ctx;
2415 const EVP_CIPHER *enc = NULL;
2416 unsigned char iv[EVP_MAX_IV_LENGTH];
2417 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2418 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2419 int padl, outl = sizeof(epms);
2421 EVP_CIPHER_CTX_init(&ciph_ctx);
2424 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2426 # endif /* KSSL_DEBUG */
2429 # ifdef KRB5SENDAUTH
2431 authp = &authenticator;
2432 # endif /* KRB5SENDAUTH */
2434 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2435 enc = kssl_map_enc(kssl_ctx->enctype);
2440 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2441 if (krb5rc && kssl_err.text)
2442 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2445 # endif /* KSSL_DEBUG */
2448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2449 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2454 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2455 * in place of RFC 2712 KerberosWrapper, as in:
2457 * Send ticket (copy to *p, set n = length)
2458 * n = krb5_ap_req.length;
2459 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2460 * if (krb5_ap_req.data)
2461 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2463 * Now using real RFC 2712 KerberosWrapper
2464 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2465 * Note: 2712 "opaque" types are here replaced
2466 * with a 2-byte length followed by the value.
2468 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2469 * Where "xx xx" = length bytes. Shown here with
2470 * optional authenticator omitted.
2473 /* KerberosWrapper.Ticket */
2474 s2n(enc_ticket->length, p);
2475 memcpy(p, enc_ticket->data, enc_ticket->length);
2476 p += enc_ticket->length;
2477 n = enc_ticket->length + 2;
2479 /* KerberosWrapper.Authenticator */
2480 if (authp && authp->length) {
2481 s2n(authp->length, p);
2482 memcpy(p, authp->data, authp->length);
2484 n += authp->length + 2;
2490 s2n(0, p); /* null authenticator length */
2494 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2495 pms = OPENSSL_malloc(pmslen);
2499 pms[0] = s->client_version >> 8;
2500 pms[1] = s->client_version & 0xff;
2501 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2505 * 20010420 VRS. Tried it this way; failed.
2506 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2507 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2508 * kssl_ctx->length);
2509 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2512 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2513 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2514 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen);
2515 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2517 if (outl > (int)sizeof epms) {
2518 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2519 ERR_R_INTERNAL_ERROR);
2522 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2524 /* KerberosWrapper.EncryptedPreMasterSecret */
2526 memcpy(p, epms, outl);
2529 OPENSSL_cleanse(epms, outl);
2532 #ifndef OPENSSL_NO_DH
2533 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2534 DH *dh_srvr, *dh_clnt;
2535 SESS_CERT *scert = s->session->sess_cert;
2537 if (scert == NULL) {
2538 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2539 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2540 SSL_R_UNEXPECTED_MESSAGE);
2544 if (scert->peer_dh_tmp != NULL)
2545 dh_srvr = scert->peer_dh_tmp;
2547 /* we get them from the cert */
2548 int idx = scert->peer_cert_type;
2549 EVP_PKEY *spkey = NULL;
2552 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2554 dh_srvr = EVP_PKEY_get1_DH(spkey);
2555 EVP_PKEY_free(spkey);
2557 if (dh_srvr == NULL) {
2558 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2559 ERR_R_INTERNAL_ERROR);
2563 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2564 /* Use client certificate key */
2565 EVP_PKEY *clkey = s->cert->key->privatekey;
2568 dh_clnt = EVP_PKEY_get1_DH(clkey);
2569 if (dh_clnt == NULL) {
2570 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2571 ERR_R_INTERNAL_ERROR);
2575 /* generate a new random key */
2576 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2577 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2580 if (!DH_generate_key(dh_clnt)) {
2581 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2587 pmslen = DH_size(dh_clnt);
2588 pms = OPENSSL_malloc(pmslen);
2593 * use the 'p' output buffer for the DH key, but make sure to
2594 * clear it out afterwards
2597 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2598 if (scert->peer_dh_tmp == NULL)
2602 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2608 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2611 /* send off the data */
2612 n = BN_num_bytes(dh_clnt->pub_key);
2614 BN_bn2bin(dh_clnt->pub_key, p);
2620 /* perhaps clean things up a bit EAY EAY EAY EAY */
2624 #ifndef OPENSSL_NO_EC
2625 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2626 const EC_GROUP *srvr_group = NULL;
2628 int ecdh_clnt_cert = 0;
2631 if (s->session->sess_cert == NULL) {
2632 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2633 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2634 SSL_R_UNEXPECTED_MESSAGE);
2639 * Did we send out the client's ECDH share for use in premaster
2640 * computation as part of client certificate? If so, set
2641 * ecdh_clnt_cert to 1.
2643 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2645 * XXX: For now, we do not support client
2646 * authentication using ECDH certificates.
2647 * To add such support, one needs to add
2648 * code that checks for appropriate
2649 * conditions and sets ecdh_clnt_cert to 1.
2650 * For example, the cert have an ECC
2651 * key on the same curve as the server's
2652 * and the key should be authorized for
2655 * One also needs to add code in ssl3_connect
2656 * to skip sending the certificate verify
2659 * if ((s->cert->key->privatekey != NULL) &&
2660 * (s->cert->key->privatekey->type ==
2661 * EVP_PKEY_EC) && ...)
2662 * ecdh_clnt_cert = 1;
2666 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2667 tkey = s->session->sess_cert->peer_ecdh_tmp;
2669 /* Get the Server Public Key from Cert */
2671 X509_get_pubkey(s->session->
2672 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2673 if ((srvr_pub_pkey == NULL)
2674 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2675 || (srvr_pub_pkey->pkey.ec == NULL)) {
2676 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2677 ERR_R_INTERNAL_ERROR);
2681 tkey = srvr_pub_pkey->pkey.ec;
2684 srvr_group = EC_KEY_get0_group(tkey);
2685 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2687 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2688 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2689 ERR_R_INTERNAL_ERROR);
2693 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2694 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2695 ERR_R_MALLOC_FAILURE);
2699 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2700 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2703 if (ecdh_clnt_cert) {
2705 * Reuse key info from our certificate We only need our
2706 * private key to perform the ECDH computation.
2708 const BIGNUM *priv_key;
2709 tkey = s->cert->key->privatekey->pkey.ec;
2710 priv_key = EC_KEY_get0_private_key(tkey);
2711 if (priv_key == NULL) {
2712 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2713 ERR_R_MALLOC_FAILURE);
2716 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2717 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2721 /* Generate a new ECDH key pair */
2722 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2723 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2730 * use the 'p' output buffer for the ECDH key, but make sure to
2731 * clear it out afterwards
2734 field_size = EC_GROUP_get_degree(srvr_group);
2735 if (field_size <= 0) {
2736 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2739 pmslen = (field_size + 7) / 8;
2740 pms = OPENSSL_malloc(pmslen);
2743 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2744 if (n <= 0 || pmslen != (size_t)n) {
2745 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2749 if (ecdh_clnt_cert) {
2750 /* Send empty client key exch message */
2754 * First check the size of encoding and allocate memory
2758 EC_POINT_point2oct(srvr_group,
2759 EC_KEY_get0_public_key(clnt_ecdh),
2760 POINT_CONVERSION_UNCOMPRESSED,
2763 encodedPoint = (unsigned char *)
2764 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2765 bn_ctx = BN_CTX_new();
2766 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2767 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2768 ERR_R_MALLOC_FAILURE);
2772 /* Encode the public key */
2773 n = EC_POINT_point2oct(srvr_group,
2774 EC_KEY_get0_public_key(clnt_ecdh),
2775 POINT_CONVERSION_UNCOMPRESSED,
2776 encodedPoint, encoded_pt_len, bn_ctx);
2778 *p = n; /* length of encoded point */
2779 /* Encoded point will be copied here */
2781 /* copy the point */
2782 memcpy((unsigned char *)p, encodedPoint, n);
2783 /* increment n to account for length field */
2787 /* Free allocated memory */
2788 BN_CTX_free(bn_ctx);
2789 if (encodedPoint != NULL)
2790 OPENSSL_free(encodedPoint);
2791 EC_KEY_free(clnt_ecdh);
2792 EVP_PKEY_free(srvr_pub_pkey);
2794 #endif /* !OPENSSL_NO_EC */
2795 else if (alg_k & SSL_kGOST) {
2796 /* GOST key exchange message creation */
2797 EVP_PKEY_CTX *pkey_ctx;
2800 unsigned int md_len;
2802 unsigned char shared_ukm[32], tmp[256];
2803 EVP_MD_CTX *ukm_hash;
2807 pms = OPENSSL_malloc(pmslen);
2812 * Get server sertificate PKEY and create ctx from it
2816 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2820 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2822 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2823 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2827 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2828 X509_get_pubkey(peer_cert), NULL);
2830 * If we have send a certificate, and certificate key
2832 * * parameters match those of server certificate, use
2833 * certificate key for key exchange
2836 /* Otherwise, generate ephemeral key pair */
2838 EVP_PKEY_encrypt_init(pkey_ctx);
2839 /* Generate session key */
2840 if (RAND_bytes(pms, pmslen) <= 0) {
2841 EVP_PKEY_CTX_free(pkey_ctx);
2842 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2843 ERR_R_INTERNAL_ERROR);
2847 * If we have client certificate, use its secret as peer key
2849 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2850 if (EVP_PKEY_derive_set_peer
2851 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2853 * If there was an error - just ignore it. Ephemeral key
2860 * Compute shared IV and store it in algorithm-specific context
2863 ukm_hash = EVP_MD_CTX_create();
2864 EVP_DigestInit(ukm_hash,
2865 EVP_get_digestbynid(NID_id_GostR3411_94));
2866 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2868 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2870 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2871 EVP_MD_CTX_destroy(ukm_hash);
2872 if (EVP_PKEY_CTX_ctrl
2873 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2875 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2879 /* Make GOST keytransport blob message */
2881 * Encapsulate it into sequence
2883 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2885 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2886 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2890 if (msglen >= 0x80) {
2892 *(p++) = msglen & 0xff;
2895 *(p++) = msglen & 0xff;
2898 memcpy(p, tmp, msglen);
2899 /* Check if pubkey from client certificate was used */
2900 if (EVP_PKEY_CTX_ctrl
2901 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2902 /* Set flag "skip certificate verify" */
2903 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2905 EVP_PKEY_CTX_free(pkey_ctx);
2906 EVP_PKEY_free(pub_key);
2909 #ifndef OPENSSL_NO_SRP
2910 else if (alg_k & SSL_kSRP) {
2911 if (s->srp_ctx.A != NULL) {
2912 /* send off the data */
2913 n = BN_num_bytes(s->srp_ctx.A);
2915 BN_bn2bin(s->srp_ctx.A, p);
2918 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2919 ERR_R_INTERNAL_ERROR);
2922 if (s->session->srp_username != NULL)
2923 OPENSSL_free(s->session->srp_username);
2924 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2925 if (s->session->srp_username == NULL) {
2926 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2927 ERR_R_MALLOC_FAILURE);
2932 #ifndef OPENSSL_NO_PSK
2933 else if (alg_k & SSL_kPSK) {
2935 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2936 * \0-terminated identity. The last byte is for us for simulating
2939 char identity[PSK_MAX_IDENTITY_LEN + 2];
2940 size_t identity_len;
2941 unsigned char *t = NULL;
2942 unsigned int psk_len = 0;
2946 if (s->psk_client_callback == NULL) {
2947 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2948 SSL_R_PSK_NO_CLIENT_CB);
2952 memset(identity, 0, sizeof(identity));
2953 /* Allocate maximum size buffer */
2954 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2955 pms = OPENSSL_malloc(pmslen);
2959 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2960 identity, sizeof(identity) - 1,
2962 if (psk_len > PSK_MAX_PSK_LEN) {
2963 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2964 ERR_R_INTERNAL_ERROR);
2966 } else if (psk_len == 0) {
2967 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2968 SSL_R_PSK_IDENTITY_NOT_FOUND);
2971 /* Change pmslen to real length */
2972 pmslen = 2 + psk_len + 2 + psk_len;
2973 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2974 identity_len = strlen(identity);
2975 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2976 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2977 ERR_R_INTERNAL_ERROR);
2980 /* create PSK pre_master_secret */
2982 memmove(pms + psk_len + 4, pms, psk_len);
2984 memset(t, 0, psk_len);
2988 if (s->session->psk_identity_hint != NULL)
2989 OPENSSL_free(s->session->psk_identity_hint);
2990 s->session->psk_identity_hint =
2991 BUF_strdup(s->ctx->psk_identity_hint);
2992 if (s->ctx->psk_identity_hint != NULL
2993 && s->session->psk_identity_hint == NULL) {
2994 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2995 ERR_R_MALLOC_FAILURE);
2999 if (s->session->psk_identity != NULL)
3000 OPENSSL_free(s->session->psk_identity);
3001 s->session->psk_identity = BUF_strdup(identity);
3002 if (s->session->psk_identity == NULL) {
3003 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3004 ERR_R_MALLOC_FAILURE);
3008 s2n(identity_len, p);
3009 memcpy(p, identity, identity_len);
3010 n = 2 + identity_len;
3013 OPENSSL_cleanse(identity, sizeof(identity));
3015 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3021 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3022 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3026 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
3027 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3028 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3032 s->state = SSL3_ST_CW_KEY_EXCH_B;
3035 /* SSL3_ST_CW_KEY_EXCH_B */
3036 n = ssl_do_write(s);
3037 #ifndef OPENSSL_NO_SRP
3039 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3041 * If everything written generate master key: no need to save PMS as
3042 * SRP_generate_client_master_secret generates it internally.
3045 if ((s->session->master_key_length =
3046 SRP_generate_client_master_secret(s,
3047 s->session->master_key)) <
3049 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3050 ERR_R_INTERNAL_ERROR);
3056 /* If we haven't written everything save PMS */
3059 s->cert->pmslen = pmslen;
3061 /* If we don't have a PMS restore */
3064 pmslen = s->cert->pmslen;
3067 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3068 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3071 s->session->master_key_length =
3072 s->method->ssl3_enc->generate_master_secret(s,
3074 session->master_key,
3076 OPENSSL_cleanse(pms, pmslen);
3078 s->cert->pms = NULL;
3079 if (s->session->master_key_length < 0) {
3080 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3081 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3087 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3088 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3091 OPENSSL_cleanse(pms, pmslen);
3093 s->cert->pms = NULL;
3095 #ifndef OPENSSL_NO_EC
3096 BN_CTX_free(bn_ctx);
3097 if (encodedPoint != NULL)
3098 OPENSSL_free(encodedPoint);
3099 EC_KEY_free(clnt_ecdh);
3100 EVP_PKEY_free(srvr_pub_pkey);
3105 int ssl3_send_client_verify(SSL *s)
3108 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3110 EVP_PKEY_CTX *pctx = NULL;
3116 EVP_MD_CTX_init(&mctx);
3118 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3119 p = ssl_handshake_start(s);
3120 pkey = s->cert->key->privatekey;
3121 /* Create context from key and test if sha1 is allowed as digest */
3122 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3123 EVP_PKEY_sign_init(pctx);
3124 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3125 if (!SSL_USE_SIGALGS(s))
3126 s->method->ssl3_enc->cert_verify_mac(s,
3129 [MD5_DIGEST_LENGTH]));
3134 * For TLS v1.2 send signature algorithm and signature using agreed
3135 * digest and cached handshake records.
3137 if (SSL_USE_SIGALGS(s)) {
3140 const EVP_MD *md = s->cert->key->digest;
3141 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3142 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3143 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3148 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3151 if (!EVP_SignInit_ex(&mctx, md, NULL)
3152 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3153 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3154 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3160 * For extended master secret we've already digested cached
3163 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
3164 BIO_free(s->s3->handshake_buffer);
3165 s->s3->handshake_buffer = NULL;
3166 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3167 } else if (!ssl3_digest_cached_records(s))
3170 #ifndef OPENSSL_NO_RSA
3171 if (pkey->type == EVP_PKEY_RSA) {
3172 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3173 if (RSA_sign(NID_md5_sha1, data,
3174 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3175 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3176 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3183 #ifndef OPENSSL_NO_DSA
3184 if (pkey->type == EVP_PKEY_DSA) {
3185 if (!DSA_sign(pkey->save_type,
3186 &(data[MD5_DIGEST_LENGTH]),
3187 SHA_DIGEST_LENGTH, &(p[2]),
3188 (unsigned int *)&j, pkey->pkey.dsa)) {
3189 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3196 #ifndef OPENSSL_NO_EC
3197 if (pkey->type == EVP_PKEY_EC) {
3198 if (!ECDSA_sign(pkey->save_type,
3199 &(data[MD5_DIGEST_LENGTH]),
3200 SHA_DIGEST_LENGTH, &(p[2]),
3201 (unsigned int *)&j, pkey->pkey.ec)) {
3202 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3209 if (pkey->type == NID_id_GostR3410_94
3210 || pkey->type == NID_id_GostR3410_2001) {
3211 unsigned char signbuf[64];
3213 size_t sigsize = 64;
3214 s->method->ssl3_enc->cert_verify_mac(s,
3215 NID_id_GostR3411_94, data);
3216 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3217 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3220 for (i = 63, j = 0; i >= 0; j++, i--) {
3221 p[2 + j] = signbuf[i];
3226 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3229 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3230 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3233 s->state = SSL3_ST_CW_CERT_VRFY_B;
3235 EVP_MD_CTX_cleanup(&mctx);
3236 EVP_PKEY_CTX_free(pctx);
3237 return ssl_do_write(s);
3239 EVP_MD_CTX_cleanup(&mctx);
3240 EVP_PKEY_CTX_free(pctx);
3245 * Check a certificate can be used for client authentication. Currently check
3246 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3247 * certificates can be used and optionally checks suitability for Suite B.
3249 static int ssl3_check_client_certificate(SSL *s)
3251 unsigned long alg_k;
3252 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3254 /* If no suitable signature algorithm can't use certificate */
3255 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3258 * If strict mode check suitability of chain before using it. This also
3259 * adjusts suite B digest if necessary.
3261 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3262 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3264 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3265 /* See if we can use client certificate for fixed DH */
3266 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3267 SESS_CERT *scert = s->session->sess_cert;
3268 int i = scert->peer_cert_type;
3269 EVP_PKEY *clkey = NULL, *spkey = NULL;
3270 clkey = s->cert->key->privatekey;
3271 /* If client key not DH assume it can be used */
3272 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3275 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3277 /* Compare server and client parameters */
3278 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3279 EVP_PKEY_free(spkey);
3283 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3288 int ssl3_send_client_certificate(SSL *s)
3291 EVP_PKEY *pkey = NULL;
3294 if (s->state == SSL3_ST_CW_CERT_A) {
3295 /* Let cert callback update client certificates if required */
3296 if (s->cert->cert_cb) {
3297 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3299 s->rwstate = SSL_X509_LOOKUP;
3303 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3306 s->rwstate = SSL_NOTHING;
3308 if (ssl3_check_client_certificate(s))
3309 s->state = SSL3_ST_CW_CERT_C;
3311 s->state = SSL3_ST_CW_CERT_B;
3314 /* We need to get a client cert */
3315 if (s->state == SSL3_ST_CW_CERT_B) {
3317 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3318 * return(-1); We then get retied later
3321 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3323 s->rwstate = SSL_X509_LOOKUP;
3326 s->rwstate = SSL_NOTHING;
3327 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3328 s->state = SSL3_ST_CW_CERT_B;
3329 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3331 } else if (i == 1) {
3333 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3334 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3339 EVP_PKEY_free(pkey);
3340 if (i && !ssl3_check_client_certificate(s))
3343 if (s->version == SSL3_VERSION) {
3344 s->s3->tmp.cert_req = 0;
3345 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3348 s->s3->tmp.cert_req = 2;
3352 /* Ok, we have a cert */
3353 s->state = SSL3_ST_CW_CERT_C;
3356 if (s->state == SSL3_ST_CW_CERT_C) {
3357 s->state = SSL3_ST_CW_CERT_D;
3358 if (!ssl3_output_cert_chain(s,
3359 (s->s3->tmp.cert_req ==
3360 2) ? NULL : s->cert->key)) {
3361 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3362 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3366 /* SSL3_ST_CW_CERT_D */
3367 return ssl_do_write(s);
3370 #define has_bits(i,m) (((i)&(m)) == (m))
3372 int ssl3_check_cert_and_algorithm(SSL *s)
3376 EVP_PKEY *pkey = NULL;
3378 #ifndef OPENSSL_NO_RSA
3381 #ifndef OPENSSL_NO_DH
3385 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3386 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3388 /* we don't have a certificate */
3389 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3392 sc = s->session->sess_cert;
3394 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3397 #ifndef OPENSSL_NO_RSA
3398 rsa = s->session->sess_cert->peer_rsa_tmp;
3400 #ifndef OPENSSL_NO_DH
3401 dh = s->session->sess_cert->peer_dh_tmp;
3404 /* This is the passed certificate */
3406 idx = sc->peer_cert_type;
3407 #ifndef OPENSSL_NO_EC
3408 if (idx == SSL_PKEY_ECC) {
3409 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3411 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3416 } else if (alg_a & SSL_aECDSA) {
3417 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3418 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3420 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3421 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3425 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3426 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3427 EVP_PKEY_free(pkey);
3429 /* Check that we have a certificate if we require one */
3430 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3431 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3432 SSL_R_MISSING_RSA_SIGNING_CERT);
3435 #ifndef OPENSSL_NO_DSA
3436 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3437 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3438 SSL_R_MISSING_DSA_SIGNING_CERT);
3442 #ifndef OPENSSL_NO_RSA
3443 if ((alg_k & SSL_kRSA) &&
3444 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3445 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3446 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3450 #ifndef OPENSSL_NO_DH
3451 if ((alg_k & SSL_kDHE) &&
3452 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3453 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3455 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3456 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3457 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3458 SSL_R_MISSING_DH_RSA_CERT);
3461 # ifndef OPENSSL_NO_DSA
3462 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3463 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3464 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3465 SSL_R_MISSING_DH_DSA_CERT);
3471 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3472 #ifndef OPENSSL_NO_RSA
3473 if (alg_k & SSL_kRSA) {
3475 || RSA_size(rsa) * 8 >
3476 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3477 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3478 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3483 #ifndef OPENSSL_NO_DH
3484 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3486 || DH_size(dh) * 8 >
3487 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3488 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3489 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3495 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3496 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3502 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3507 #ifndef OPENSSL_NO_TLSEXT
3509 * Normally, we can tell if the server is resuming the session from
3510 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3511 * message after the ServerHello to determine if the server is resuming.
3512 * Therefore, we allow EAP-FAST to peek ahead.
3513 * ssl3_check_finished returns 1 if we are resuming from an external
3514 * pre-shared secret, we have a "ticket" and the next server handshake message
3515 * is Finished; and 0 otherwise. It returns -1 upon an error.
3517 static int ssl3_check_finished(SSL *s)
3521 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3522 !s->session->tlsext_tick)
3525 /* Need to permit this temporarily, in case the next message is Finished. */
3526 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3528 * This function is called when we might get a Certificate message instead,
3529 * so permit appropriate message length.
3530 * We ignore the return value as we're only interested in the message type
3531 * and not its length.
3533 s->method->ssl_get_message(s,
3536 -1, s->max_cert_list, &ok);
3537 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3542 s->s3->tmp.reuse_message = 1;
3544 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3547 /* If we're not done, then the CCS arrived early and we should bail. */
3548 if (s->s3->change_cipher_spec) {
3549 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3550 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3557 # ifndef OPENSSL_NO_NEXTPROTONEG
3558 int ssl3_send_next_proto(SSL *s)
3560 unsigned int len, padding_len;
3563 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3564 len = s->next_proto_negotiated_len;
3565 padding_len = 32 - ((len + 2) % 32);
3566 d = (unsigned char *)s->init_buf->data;
3568 memcpy(d + 5, s->next_proto_negotiated, len);
3569 d[5 + len] = padding_len;
3570 memset(d + 6 + len, 0, padding_len);
3571 *(d++) = SSL3_MT_NEXT_PROTO;
3572 l2n3(2 + len + padding_len, d);
3573 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3574 s->init_num = 4 + 2 + len + padding_len;
3578 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3583 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3586 #ifndef OPENSSL_NO_ENGINE
3587 if (s->ctx->client_cert_engine) {
3588 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3589 SSL_get_client_CA_list(s),
3590 px509, ppkey, NULL, NULL, NULL);
3595 if (s->ctx->client_cert_cb)
3596 i = s->ctx->client_cert_cb(s, px509, ppkey);