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);
240 s->state = SSL_ST_ERR;
245 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
246 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
250 /* s->version=SSL3_VERSION; */
251 s->type = SSL_ST_CONNECT;
253 if (s->init_buf == NULL) {
254 if ((buf = BUF_MEM_new()) == NULL) {
256 s->state = SSL_ST_ERR;
259 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
261 s->state = SSL_ST_ERR;
268 if (!ssl3_setup_buffers(s)) {
273 /* setup buffing BIO */
274 if (!ssl_init_wbio_buffer(s, 0)) {
276 s->state = SSL_ST_ERR;
280 /* don't push the buffering BIO quite yet */
282 ssl3_init_finished_mac(s);
284 s->state = SSL3_ST_CW_CLNT_HELLO_A;
285 s->ctx->stats.sess_connect++;
287 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
289 * Should have been reset by ssl3_get_finished, too.
291 s->s3->change_cipher_spec = 0;
294 case SSL3_ST_CW_CLNT_HELLO_A:
295 case SSL3_ST_CW_CLNT_HELLO_B:
298 ret = ssl3_client_hello(s);
301 s->state = SSL3_ST_CR_SRVR_HELLO_A;
304 /* turn on buffering for the next lot of output */
305 if (s->bbio != s->wbio)
306 s->wbio = BIO_push(s->bbio, s->wbio);
310 case SSL3_ST_CR_SRVR_HELLO_A:
311 case SSL3_ST_CR_SRVR_HELLO_B:
312 ret = ssl3_get_server_hello(s);
317 s->state = SSL3_ST_CR_FINISHED_A;
318 #ifndef OPENSSL_NO_TLSEXT
319 if (s->tlsext_ticket_expected) {
320 /* receive renewed session ticket */
321 s->state = SSL3_ST_CR_SESSION_TICKET_A;
325 s->state = SSL3_ST_CR_CERT_A;
329 case SSL3_ST_CR_CERT_A:
330 case SSL3_ST_CR_CERT_B:
331 #ifndef OPENSSL_NO_TLSEXT
332 /* Noop (ret = 0) for everything but EAP-FAST. */
333 ret = ssl3_check_finished(s);
338 s->state = SSL3_ST_CR_FINISHED_A;
343 /* Check if it is anon DH/ECDH, SRP auth */
347 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
348 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
349 ret = ssl3_get_server_certificate(s);
352 #ifndef OPENSSL_NO_TLSEXT
353 if (s->tlsext_status_expected)
354 s->state = SSL3_ST_CR_CERT_STATUS_A;
356 s->state = SSL3_ST_CR_KEY_EXCH_A;
359 s->state = SSL3_ST_CR_KEY_EXCH_A;
365 s->state = SSL3_ST_CR_KEY_EXCH_A;
370 case SSL3_ST_CR_KEY_EXCH_A:
371 case SSL3_ST_CR_KEY_EXCH_B:
372 ret = ssl3_get_key_exchange(s);
375 s->state = SSL3_ST_CR_CERT_REQ_A;
379 * at this point we check that we have the required stuff from
382 if (!ssl3_check_cert_and_algorithm(s)) {
384 s->state = SSL_ST_ERR;
389 case SSL3_ST_CR_CERT_REQ_A:
390 case SSL3_ST_CR_CERT_REQ_B:
391 ret = ssl3_get_certificate_request(s);
394 s->state = SSL3_ST_CR_SRVR_DONE_A;
398 case SSL3_ST_CR_SRVR_DONE_A:
399 case SSL3_ST_CR_SRVR_DONE_B:
400 ret = ssl3_get_server_done(s);
403 #ifndef OPENSSL_NO_SRP
404 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
405 if ((ret = SRP_Calc_A_param(s)) <= 0) {
406 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
407 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
408 s->state = SSL_ST_ERR;
413 if (s->s3->tmp.cert_req)
414 s->state = SSL3_ST_CW_CERT_A;
416 s->state = SSL3_ST_CW_KEY_EXCH_A;
421 case SSL3_ST_CW_CERT_A:
422 case SSL3_ST_CW_CERT_B:
423 case SSL3_ST_CW_CERT_C:
424 case SSL3_ST_CW_CERT_D:
425 ret = ssl3_send_client_certificate(s);
428 s->state = SSL3_ST_CW_KEY_EXCH_A;
432 case SSL3_ST_CW_KEY_EXCH_A:
433 case SSL3_ST_CW_KEY_EXCH_B:
434 ret = ssl3_send_client_key_exchange(s);
438 * EAY EAY EAY need to check for DH fix cert sent back
441 * For TLS, cert_req is set to 2, so a cert chain of nothing is
442 * sent, but no verify packet is sent
445 * XXX: For now, we do not support client authentication in ECDH
446 * cipher suites with ECDH (rather than ECDSA) certificates. We
447 * need to skip the certificate verify message when client's
448 * ECDH public key is sent inside the client certificate.
450 if (s->s3->tmp.cert_req == 1) {
451 s->state = SSL3_ST_CW_CERT_VRFY_A;
453 s->state = SSL3_ST_CW_CHANGE_A;
455 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
456 s->state = SSL3_ST_CW_CHANGE_A;
462 case SSL3_ST_CW_CERT_VRFY_A:
463 case SSL3_ST_CW_CERT_VRFY_B:
464 ret = ssl3_send_client_verify(s);
467 s->state = SSL3_ST_CW_CHANGE_A;
471 case SSL3_ST_CW_CHANGE_A:
472 case SSL3_ST_CW_CHANGE_B:
473 ret = ssl3_send_change_cipher_spec(s,
475 SSL3_ST_CW_CHANGE_B);
479 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
480 s->state = SSL3_ST_CW_FINISHED_A;
482 if (s->s3->next_proto_neg_seen)
483 s->state = SSL3_ST_CW_NEXT_PROTO_A;
485 s->state = SSL3_ST_CW_FINISHED_A;
489 s->session->cipher = s->s3->tmp.new_cipher;
490 #ifdef OPENSSL_NO_COMP
491 s->session->compress_meth = 0;
493 if (s->s3->tmp.new_compression == NULL)
494 s->session->compress_meth = 0;
496 s->session->compress_meth = s->s3->tmp.new_compression->id;
498 if (!s->method->ssl3_enc->setup_key_block(s)) {
500 s->state = SSL_ST_ERR;
504 if (!s->method->ssl3_enc->change_cipher_state(s,
505 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
508 s->state = SSL_ST_ERR;
514 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
515 case SSL3_ST_CW_NEXT_PROTO_A:
516 case SSL3_ST_CW_NEXT_PROTO_B:
517 ret = ssl3_send_next_proto(s);
520 s->state = SSL3_ST_CW_FINISHED_A;
524 case SSL3_ST_CW_FINISHED_A:
525 case SSL3_ST_CW_FINISHED_B:
526 ret = ssl3_send_finished(s,
527 SSL3_ST_CW_FINISHED_A,
528 SSL3_ST_CW_FINISHED_B,
530 ssl3_enc->client_finished_label,
532 ssl3_enc->client_finished_label_len);
535 s->state = SSL3_ST_CW_FLUSH;
538 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
540 s->s3->tmp.next_state = SSL_ST_OK;
541 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
542 s->state = SSL_ST_OK;
543 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
544 s->s3->delay_buf_pop_ret = 0;
547 #ifndef OPENSSL_NO_TLSEXT
549 * Allow NewSessionTicket if ticket expected
551 if (s->tlsext_ticket_expected)
552 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
556 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
561 #ifndef OPENSSL_NO_TLSEXT
562 case SSL3_ST_CR_SESSION_TICKET_A:
563 case SSL3_ST_CR_SESSION_TICKET_B:
564 ret = ssl3_get_new_session_ticket(s);
567 s->state = SSL3_ST_CR_FINISHED_A;
571 case SSL3_ST_CR_CERT_STATUS_A:
572 case SSL3_ST_CR_CERT_STATUS_B:
573 ret = ssl3_get_cert_status(s);
576 s->state = SSL3_ST_CR_KEY_EXCH_A;
581 case SSL3_ST_CR_FINISHED_A:
582 case SSL3_ST_CR_FINISHED_B:
583 if (!s->s3->change_cipher_spec)
584 s->s3->flags |= SSL3_FLAGS_CCS_OK;
585 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
586 SSL3_ST_CR_FINISHED_B);
591 s->state = SSL3_ST_CW_CHANGE_A;
593 s->state = SSL_ST_OK;
597 case SSL3_ST_CW_FLUSH:
598 s->rwstate = SSL_WRITING;
599 if (BIO_flush(s->wbio) <= 0) {
603 s->rwstate = SSL_NOTHING;
604 s->state = s->s3->tmp.next_state;
608 /* clean a few things up */
609 ssl3_cleanup_key_block(s);
610 BUF_MEM_free(s->init_buf);
614 * If we are not 'joining' the last two packets, remove the
617 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
618 ssl_free_wbio_buffer(s);
619 /* else do it later in ssl3_write */
625 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
627 s->ctx->stats.sess_hit++;
631 s->handshake_func = ssl3_connect;
632 s->ctx->stats.sess_connect_good++;
635 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
642 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
648 /* did we do anything */
649 if (!s->s3->tmp.reuse_message && !skip) {
651 if ((ret = BIO_flush(s->wbio)) <= 0)
655 if ((cb != NULL) && (s->state != state)) {
656 new_state = s->state;
658 cb(s, SSL_CB_CONNECT_LOOP, 1);
659 s->state = new_state;
668 cb(s, SSL_CB_CONNECT_EXIT, ret);
672 int ssl3_client_hello(SSL *s)
675 unsigned char *p, *d;
679 #ifndef OPENSSL_NO_COMP
684 buf = (unsigned char *)s->init_buf->data;
685 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
686 SSL_SESSION *sess = s->session;
687 if ((sess == NULL) || (sess->ssl_version != s->version) ||
688 #ifdef OPENSSL_NO_TLSEXT
689 !sess->session_id_length ||
692 * In the case of EAP-FAST, we can have a pre-shared
693 * "ticket" without a session ID.
695 (!sess->session_id_length && !sess->tlsext_tick) ||
697 (sess->not_resumable)) {
698 if (!ssl_get_new_session(s, 0))
701 if (s->method->version == DTLS_ANY_VERSION) {
702 /* Determine which DTLS version to use */
703 int options = s->options;
704 /* If DTLS 1.2 disabled correct the version number */
705 if (options & SSL_OP_NO_DTLSv1_2) {
706 if (tls1_suiteb(s)) {
707 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
708 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
712 * Disabling all versions is silly: return an error.
714 if (options & SSL_OP_NO_DTLSv1) {
715 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
719 * Update method so we don't use any DTLS 1.2 features.
721 s->method = DTLSv1_client_method();
722 s->version = DTLS1_VERSION;
725 * We only support one version: update method
727 if (options & SSL_OP_NO_DTLSv1)
728 s->method = DTLSv1_2_client_method();
729 s->version = DTLS1_2_VERSION;
731 s->client_version = s->version;
733 /* else use the pre-loaded session */
735 p = s->s3->client_random;
738 * for DTLS if client_random is initialized, reuse it, we are
739 * required to use same upon reply to HelloVerify
741 if (SSL_IS_DTLS(s)) {
744 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
753 if (i && ssl_fill_hello_random(s, 0, p,
754 sizeof(s->s3->client_random)) <= 0)
757 /* Do the message type and length last */
758 d = p = ssl_handshake_start(s);
761 * version indicates the negotiated version: for example from
762 * an SSLv2/v3 compatible client hello). The client_version
763 * field is the maximum version we permit and it is also
764 * used in RSA encrypted premaster secrets. Some servers can
765 * choke if we initially report a higher version then
766 * renegotiate to a lower one in the premaster secret. This
767 * didn't happen with TLS 1.0 as most servers supported it
768 * but it can with TLS 1.1 or later if the server only supports
771 * Possible scenario with previous logic:
772 * 1. Client hello indicates TLS 1.2
773 * 2. Server hello says TLS 1.0
774 * 3. RSA encrypted premaster secret uses 1.2.
775 * 4. Handhaked proceeds using TLS 1.0.
776 * 5. Server sends hello request to renegotiate.
777 * 6. Client hello indicates TLS v1.0 as we now
778 * know that is maximum server supports.
779 * 7. Server chokes on RSA encrypted premaster secret
780 * containing version 1.0.
782 * For interoperability it should be OK to always use the
783 * maximum version we support in client hello and then rely
784 * on the checking of version to ensure the servers isn't
785 * being inconsistent: for example initially negotiating with
786 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
787 * client_version in client hello and not resetting it to
788 * the negotiated version.
790 *(p++) = s->client_version >> 8;
791 *(p++) = s->client_version & 0xff;
794 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
795 p += SSL3_RANDOM_SIZE;
801 i = s->session->session_id_length;
804 if (i > (int)sizeof(s->session->session_id)) {
805 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
808 memcpy(p, s->session->session_id, i);
812 /* cookie stuff for DTLS */
813 if (SSL_IS_DTLS(s)) {
814 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
815 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
818 *(p++) = s->d1->cookie_len;
819 memcpy(p, s->d1->cookie, s->d1->cookie_len);
820 p += s->d1->cookie_len;
823 /* Ciphers supported */
824 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
826 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
829 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
831 * Some servers hang if client hello > 256 bytes as hack workaround
832 * chop number of supported ciphers to keep it well below this if we
835 if (TLS1_get_version(s) >= TLS1_2_VERSION
836 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
837 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
843 #ifdef OPENSSL_NO_COMP
847 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
850 j = sk_SSL_COMP_num(s->ctx->comp_methods);
852 for (i = 0; i < j; i++) {
853 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
857 *(p++) = 0; /* Add the NULL method */
859 #ifndef OPENSSL_NO_TLSEXT
861 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
862 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
866 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
868 ssl3_send_alert(s, SSL3_AL_FATAL, al);
869 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
875 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
876 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
877 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
880 s->state = SSL3_ST_CW_CLNT_HELLO_B;
883 /* SSL3_ST_CW_CLNT_HELLO_B */
884 return ssl_do_write(s);
886 s->state = SSL_ST_ERR;
890 int ssl3_get_server_hello(SSL *s)
892 STACK_OF(SSL_CIPHER) *sk;
895 unsigned char *p, *d;
896 int i, al = SSL_AD_INTERNAL_ERROR, ok;
899 #ifndef OPENSSL_NO_COMP
903 * Hello verify request and/or server hello version may not match so set
904 * first packet if we're negotiating version.
909 n = s->method->ssl_get_message(s,
910 SSL3_ST_CR_SRVR_HELLO_A,
911 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
916 if (SSL_IS_DTLS(s)) {
918 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
919 if (s->d1->send_cookie == 0) {
920 s->s3->tmp.reuse_message = 1;
922 } else { /* already sent a cookie */
924 al = SSL_AD_UNEXPECTED_MESSAGE;
925 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
931 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
932 al = SSL_AD_UNEXPECTED_MESSAGE;
933 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
937 d = p = (unsigned char *)s->init_msg;
938 if (s->method->version == DTLS_ANY_VERSION) {
939 /* Work out correct protocol version to use */
940 int hversion = (p[0] << 8) | p[1];
941 int options = s->options;
942 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
943 s->method = DTLSv1_2_client_method();
944 else if (tls1_suiteb(s)) {
945 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
946 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
947 s->version = hversion;
948 al = SSL_AD_PROTOCOL_VERSION;
950 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
951 s->method = DTLSv1_client_method();
953 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
954 s->version = hversion;
955 al = SSL_AD_PROTOCOL_VERSION;
958 s->version = s->method->version;
961 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
962 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
963 s->version = (s->version & 0xff00) | p[1];
964 al = SSL_AD_PROTOCOL_VERSION;
969 /* load the server hello data */
970 /* load the server random */
971 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
972 p += SSL3_RANDOM_SIZE;
976 /* get the session-id */
979 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
980 al = SSL_AD_ILLEGAL_PARAMETER;
981 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
984 #ifndef OPENSSL_NO_TLSEXT
986 * Check if we can resume the session based on external pre-shared secret.
987 * EAP-FAST (RFC 4851) supports two types of session resumption.
988 * Resumption based on server-side state works with session IDs.
989 * Resumption based on pre-shared Protected Access Credentials (PACs)
990 * works by overriding the SessionTicket extension at the application
991 * layer, and does not send a session ID. (We do not know whether EAP-FAST
992 * servers would honour the session ID.) Therefore, the session ID alone
993 * is not a reliable indicator of session resumption, so we first check if
994 * we can resume, and later peek at the next handshake message to see if the
995 * server wants to resume.
997 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
998 s->session->tlsext_tick) {
999 SSL_CIPHER *pref_cipher = NULL;
1000 s->session->master_key_length = sizeof(s->session->master_key);
1001 if (s->tls_session_secret_cb(s, s->session->master_key,
1002 &s->session->master_key_length,
1004 s->tls_session_secret_cb_arg)) {
1005 s->session->cipher = pref_cipher ?
1006 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1008 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1009 al = SSL_AD_INTERNAL_ERROR;
1013 #endif /* OPENSSL_NO_TLSEXT */
1015 if (j != 0 && j == s->session->session_id_length
1016 && memcmp(p, s->session->session_id, j) == 0) {
1017 if (s->sid_ctx_length != s->session->sid_ctx_length
1018 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1019 /* actually a client application bug */
1020 al = SSL_AD_ILLEGAL_PARAMETER;
1021 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1022 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1028 * If we were trying for session-id reuse but the server
1029 * didn't echo the ID, make a new SSL_SESSION.
1030 * In the case of EAP-FAST and PAC, we do not send a session ID,
1031 * so the PAC-based session secret is always preserved. It'll be
1032 * overwritten if the server refuses resumption.
1034 if (s->session->session_id_length > 0) {
1035 if (!ssl_get_new_session(s, 0)) {
1039 s->session->session_id_length = j;
1040 memcpy(s->session->session_id, p, j); /* j could be 0 */
1043 c = ssl_get_cipher_by_char(s, p);
1045 /* unknown cipher */
1046 al = SSL_AD_ILLEGAL_PARAMETER;
1047 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1050 /* Set version disabled mask now we know version */
1051 if (!SSL_USE_TLS1_2_CIPHERS(s))
1052 ct->mask_ssl = SSL_TLSV1_2;
1056 * If it is a disabled cipher we didn't send it in client hello, so
1059 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1060 al = SSL_AD_ILLEGAL_PARAMETER;
1061 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1064 p += ssl_put_cipher_by_char(s, NULL, NULL);
1066 sk = ssl_get_ciphers_by_id(s);
1067 i = sk_SSL_CIPHER_find(sk, c);
1069 /* we did not say we would use this cipher */
1070 al = SSL_AD_ILLEGAL_PARAMETER;
1071 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1076 * Depending on the session caching (internal/external), the cipher
1077 * and/or cipher_id values may not be set. Make sure that cipher_id is
1078 * set and use it for comparison.
1080 if (s->session->cipher)
1081 s->session->cipher_id = s->session->cipher->id;
1082 if (s->hit && (s->session->cipher_id != c->id)) {
1083 al = SSL_AD_ILLEGAL_PARAMETER;
1084 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1085 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1088 s->s3->tmp.new_cipher = c;
1090 * Don't digest cached records if no sigalgs: we may need them for client
1093 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1095 /* lets get the compression algorithm */
1097 #ifdef OPENSSL_NO_COMP
1099 al = SSL_AD_ILLEGAL_PARAMETER;
1100 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1101 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1105 * If compression is disabled we'd better not try to resume a session
1106 * using compression.
1108 if (s->session->compress_meth != 0) {
1109 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1114 if (s->hit && j != s->session->compress_meth) {
1115 al = SSL_AD_ILLEGAL_PARAMETER;
1116 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1117 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1122 else if (!ssl_allow_compression(s)) {
1123 al = SSL_AD_ILLEGAL_PARAMETER;
1124 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1127 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1129 if ((j != 0) && (comp == NULL)) {
1130 al = SSL_AD_ILLEGAL_PARAMETER;
1131 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1132 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1135 s->s3->tmp.new_compression = comp;
1139 #ifndef OPENSSL_NO_TLSEXT
1140 /* TLS extensions */
1141 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1142 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1148 /* wrong packet length */
1149 al = SSL_AD_DECODE_ERROR;
1150 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1156 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1158 s->state = SSL_ST_ERR;
1162 int ssl3_get_server_certificate(SSL *s)
1164 int al, i, ok, ret = -1;
1165 unsigned long n, nc, llen, l;
1167 const unsigned char *q, *p;
1169 STACK_OF(X509) *sk = NULL;
1171 EVP_PKEY *pkey = NULL;
1172 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1175 n = s->method->ssl_get_message(s,
1178 -1, s->max_cert_list, &ok);
1183 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1184 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1185 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1186 s->s3->tmp.reuse_message = 1;
1190 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1191 al = SSL_AD_UNEXPECTED_MESSAGE;
1192 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1195 p = d = (unsigned char *)s->init_msg;
1197 if ((sk = sk_X509_new_null()) == NULL) {
1198 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1203 if (llen + 3 != n) {
1204 al = SSL_AD_DECODE_ERROR;
1205 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1208 for (nc = 0; nc < llen;) {
1210 if ((l + nc + 3) > llen) {
1211 al = SSL_AD_DECODE_ERROR;
1212 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1213 SSL_R_CERT_LENGTH_MISMATCH);
1218 x = d2i_X509(NULL, &q, l);
1220 al = SSL_AD_BAD_CERTIFICATE;
1221 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1225 al = SSL_AD_DECODE_ERROR;
1226 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1227 SSL_R_CERT_LENGTH_MISMATCH);
1230 if (!sk_X509_push(sk, x)) {
1231 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1239 i = ssl_verify_cert_chain(s, sk);
1240 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1241 #ifndef OPENSSL_NO_KRB5
1242 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1243 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1244 #endif /* OPENSSL_NO_KRB5 */
1246 al = ssl_verify_alarm_type(s->verify_result);
1247 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1248 SSL_R_CERTIFICATE_VERIFY_FAILED);
1251 ERR_clear_error(); /* but we keep s->verify_result */
1253 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1254 al = SSL_AD_HANDSHAKE_FAILURE;
1258 sc = ssl_sess_cert_new();
1262 ssl_sess_cert_free(s->session->sess_cert);
1263 s->session->sess_cert = sc;
1265 sc->cert_chain = sk;
1267 * Inconsistency alert: cert_chain does include the peer's certificate,
1268 * which we don't include in s3_srvr.c
1270 x = sk_X509_value(sk, 0);
1273 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1276 pkey = X509_get_pubkey(x);
1278 /* VRS: allow null cert if auth == KRB5 */
1279 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1280 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1284 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1285 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1286 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1287 s->s3->tmp.new_cipher->name,
1288 s->s3->tmp.new_cipher->algorithm_mkey,
1289 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1290 #endif /* KSSL_DEBUG */
1292 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1295 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1296 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1300 i = ssl_cert_type(x, pkey);
1301 if (need_cert && i < 0) {
1304 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1305 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1310 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1311 if (exp_idx >= 0 && i != exp_idx) {
1313 al = SSL_AD_ILLEGAL_PARAMETER;
1314 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1315 SSL_R_WRONG_CERTIFICATE_TYPE);
1318 sc->peer_cert_type = i;
1319 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1321 * Why would the following ever happen? We just created sc a couple
1324 X509_free(sc->peer_pkeys[i].x509);
1325 sc->peer_pkeys[i].x509 = x;
1326 sc->peer_key = &(sc->peer_pkeys[i]);
1328 X509_free(s->session->peer);
1329 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1330 s->session->peer = x;
1332 sc->peer_cert_type = i;
1333 sc->peer_key = NULL;
1335 X509_free(s->session->peer);
1336 s->session->peer = NULL;
1338 s->session->verify_result = s->verify_result;
1345 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1347 s->state = SSL_ST_ERR;
1349 EVP_PKEY_free(pkey);
1351 sk_X509_pop_free(sk, X509_free);
1355 int ssl3_get_key_exchange(SSL *s)
1357 #ifndef OPENSSL_NO_RSA
1358 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1361 unsigned char *param, *p;
1363 long i, param_len, n, alg_k, alg_a;
1364 EVP_PKEY *pkey = NULL;
1365 const EVP_MD *md = NULL;
1366 #ifndef OPENSSL_NO_RSA
1369 #ifndef OPENSSL_NO_DH
1372 #ifndef OPENSSL_NO_EC
1373 EC_KEY *ecdh = NULL;
1374 BN_CTX *bn_ctx = NULL;
1375 EC_POINT *srvr_ecpoint = NULL;
1377 int encoded_pt_len = 0;
1380 EVP_MD_CTX_init(&md_ctx);
1383 * use same message size as in ssl3_get_certificate_request() as
1384 * ServerKeyExchange message may be skipped
1386 n = s->method->ssl_get_message(s,
1387 SSL3_ST_CR_KEY_EXCH_A,
1388 SSL3_ST_CR_KEY_EXCH_B,
1389 -1, s->max_cert_list, &ok);
1393 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1395 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1397 * Can't skip server key exchange if this is an ephemeral
1400 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1401 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1402 al = SSL_AD_UNEXPECTED_MESSAGE;
1405 #ifndef OPENSSL_NO_PSK
1407 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1408 * identity hint is sent. Set session->sess_cert anyway to avoid
1411 if (alg_k & SSL_kPSK) {
1412 s->session->sess_cert = ssl_sess_cert_new();
1413 OPENSSL_free(s->ctx->psk_identity_hint);
1414 s->ctx->psk_identity_hint = NULL;
1417 s->s3->tmp.reuse_message = 1;
1421 param = p = (unsigned char *)s->init_msg;
1422 if (s->session->sess_cert != NULL) {
1423 #ifndef OPENSSL_NO_RSA
1424 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1425 s->session->sess_cert->peer_rsa_tmp = NULL;
1427 #ifndef OPENSSL_NO_DH
1428 DH_free(s->session->sess_cert->peer_dh_tmp);
1429 s->session->sess_cert->peer_dh_tmp = NULL;
1431 #ifndef OPENSSL_NO_EC
1432 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1433 s->session->sess_cert->peer_ecdh_tmp = NULL;
1436 s->session->sess_cert = ssl_sess_cert_new();
1439 /* Total length of the parameters including the length prefix */
1442 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1444 al = SSL_AD_DECODE_ERROR;
1446 #ifndef OPENSSL_NO_PSK
1447 if (alg_k & SSL_kPSK) {
1448 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1451 if (param_len > n) {
1452 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1458 * Store PSK identity hint for later use, hint is used in
1459 * ssl3_send_client_key_exchange. Assume that the maximum length of
1460 * a PSK identity hint can be as long as the maximum length of a PSK
1463 if (i > PSK_MAX_IDENTITY_LEN) {
1464 al = SSL_AD_HANDSHAKE_FAILURE;
1465 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1468 if (i > n - param_len) {
1469 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1470 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1476 * If received PSK identity hint contains NULL characters, the hint
1477 * is truncated from the first NULL. p may not be ending with NULL,
1478 * so create a NULL-terminated string.
1480 memcpy(tmp_id_hint, p, i);
1481 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1482 OPENSSL_free(s->ctx->psk_identity_hint);
1483 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1484 if (s->ctx->psk_identity_hint == NULL) {
1485 al = SSL_AD_HANDSHAKE_FAILURE;
1486 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1493 #endif /* !OPENSSL_NO_PSK */
1494 #ifndef OPENSSL_NO_SRP
1495 if (alg_k & SSL_kSRP) {
1497 if (param_len > n) {
1498 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1503 if (i > n - param_len) {
1504 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1509 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1510 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1515 if (2 > n - param_len) {
1516 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1523 if (i > n - param_len) {
1524 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1529 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1530 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1535 if (1 > n - param_len) {
1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1541 i = (unsigned int)(p[0]);
1544 if (i > n - param_len) {
1545 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1550 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1551 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1556 if (2 > n - param_len) {
1557 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1564 if (i > n - param_len) {
1565 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1570 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1577 if (!srp_verify_server_param(s, &al)) {
1578 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1582 /* We must check if there is a certificate */
1583 # ifndef OPENSSL_NO_RSA
1584 if (alg_a & SSL_aRSA)
1586 X509_get_pubkey(s->session->
1587 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1591 # ifndef OPENSSL_NO_DSA
1592 else if (alg_a & SSL_aDSS)
1594 X509_get_pubkey(s->session->
1595 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1599 #endif /* !OPENSSL_NO_SRP */
1600 #ifndef OPENSSL_NO_RSA
1601 if (alg_k & SSL_kRSA) {
1602 /* Temporary RSA keys only allowed in export ciphersuites */
1603 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1604 al = SSL_AD_UNEXPECTED_MESSAGE;
1605 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1608 if ((rsa = RSA_new()) == NULL) {
1609 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1614 if (param_len > n) {
1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1620 if (i > n - param_len) {
1621 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1626 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1632 if (2 > n - param_len) {
1633 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1640 if (i > n - param_len) {
1641 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1646 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1647 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1653 /* this should be because we are using an export cipher */
1654 if (alg_a & SSL_aRSA)
1656 X509_get_pubkey(s->session->
1657 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1659 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1662 s->session->sess_cert->peer_rsa_tmp = rsa;
1665 #else /* OPENSSL_NO_RSA */
1668 #ifndef OPENSSL_NO_DH
1669 else if (alg_k & SSL_kDHE) {
1670 if ((dh = DH_new()) == NULL) {
1671 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1676 if (param_len > n) {
1677 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1682 if (i > n - param_len) {
1683 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1688 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1689 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1694 if (2 > n - param_len) {
1695 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1702 if (i > n - param_len) {
1703 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1708 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1709 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1714 if (2 > n - param_len) {
1715 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1722 if (i > n - param_len) {
1723 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1728 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1729 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1735 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1736 al = SSL_AD_HANDSHAKE_FAILURE;
1737 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1740 # ifndef OPENSSL_NO_RSA
1741 if (alg_a & SSL_aRSA)
1743 X509_get_pubkey(s->session->
1744 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1748 # ifndef OPENSSL_NO_DSA
1749 else if (alg_a & SSL_aDSS)
1751 X509_get_pubkey(s->session->
1752 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1755 /* else anonymous DH, so no certificate or pkey. */
1757 s->session->sess_cert->peer_dh_tmp = dh;
1760 #endif /* !OPENSSL_NO_DH */
1762 #ifndef OPENSSL_NO_EC
1763 else if (alg_k & SSL_kECDHE) {
1765 const EC_GROUP *group;
1767 if ((ecdh = EC_KEY_new()) == NULL) {
1768 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1773 * Extract elliptic curve parameters and the server's ephemeral ECDH
1774 * public key. Keep accumulating lengths of various components in
1775 * param_len and make sure it never exceeds n.
1779 * XXX: For now we only support named (not generic) curves and the
1780 * ECParameters in this case is just three bytes. We also need one
1781 * byte for the length of the encoded point
1784 if (param_len > n) {
1785 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1789 * Check curve is one of our preferences, if not server has sent an
1790 * invalid curve. ECParameters is 3 bytes.
1792 if (!tls1_check_curve(s, p, 3)) {
1793 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1797 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1798 al = SSL_AD_INTERNAL_ERROR;
1799 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1800 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1804 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1805 if (ngroup == NULL) {
1806 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1809 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1810 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1813 EC_GROUP_free(ngroup);
1815 group = EC_KEY_get0_group(ecdh);
1817 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1818 (EC_GROUP_get_degree(group) > 163)) {
1819 al = SSL_AD_EXPORT_RESTRICTION;
1820 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1821 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1827 /* Next, get the encoded ECPoint */
1828 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1829 ((bn_ctx = BN_CTX_new()) == NULL)) {
1830 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1834 encoded_pt_len = *p; /* length of encoded point */
1837 if ((encoded_pt_len > n - param_len) ||
1838 (EC_POINT_oct2point(group, srvr_ecpoint,
1839 p, encoded_pt_len, bn_ctx) == 0)) {
1840 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1843 param_len += encoded_pt_len;
1846 p += encoded_pt_len;
1849 * The ECC/TLS specification does not mention the use of DSA to sign
1850 * ECParameters in the server key exchange message. We do support RSA
1854 # ifndef OPENSSL_NO_RSA
1855 else if (alg_a & SSL_aRSA)
1857 X509_get_pubkey(s->session->
1858 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1860 # ifndef OPENSSL_NO_EC
1861 else if (alg_a & SSL_aECDSA)
1863 X509_get_pubkey(s->session->
1864 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1866 /* else anonymous ECDH, so no certificate or pkey. */
1867 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1868 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1870 BN_CTX_free(bn_ctx);
1872 EC_POINT_free(srvr_ecpoint);
1873 srvr_ecpoint = NULL;
1875 al = SSL_AD_UNEXPECTED_MESSAGE;
1876 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1879 #endif /* !OPENSSL_NO_EC */
1881 /* p points to the next byte, there are 'n' bytes left */
1883 /* if it was signed, check the signature */
1885 if (SSL_USE_SIGALGS(s)) {
1888 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1891 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1898 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1906 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1911 j = EVP_PKEY_size(pkey);
1914 * Check signature length. If n is 0 then signature is empty
1916 if ((i != n) || (n > j) || (n <= 0)) {
1917 /* wrong packet length */
1918 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1921 #ifndef OPENSSL_NO_RSA
1922 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1928 for (num = 2; num > 0; num--) {
1929 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1930 EVP_DigestInit_ex(&md_ctx, (num == 2)
1931 ? s->ctx->md5 : s->ctx->sha1, NULL);
1932 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1934 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1936 EVP_DigestUpdate(&md_ctx, param, param_len);
1937 EVP_DigestFinal_ex(&md_ctx, q, &size);
1941 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1943 al = SSL_AD_DECRYPT_ERROR;
1944 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1949 al = SSL_AD_DECRYPT_ERROR;
1950 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1956 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1957 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1959 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1961 EVP_VerifyUpdate(&md_ctx, param, param_len);
1962 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1964 al = SSL_AD_DECRYPT_ERROR;
1965 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1970 /* aNULL, aSRP or kPSK do not need public keys */
1971 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1972 /* Might be wrong key type, check it */
1973 if (ssl3_check_cert_and_algorithm(s))
1974 /* Otherwise this shouldn't happen */
1975 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1978 /* still data left over */
1980 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1984 EVP_PKEY_free(pkey);
1985 EVP_MD_CTX_cleanup(&md_ctx);
1988 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1990 EVP_PKEY_free(pkey);
1991 #ifndef OPENSSL_NO_RSA
1994 #ifndef OPENSSL_NO_DH
1997 #ifndef OPENSSL_NO_EC
1998 BN_CTX_free(bn_ctx);
1999 EC_POINT_free(srvr_ecpoint);
2002 EVP_MD_CTX_cleanup(&md_ctx);
2003 s->state = SSL_ST_ERR;
2007 int ssl3_get_certificate_request(SSL *s)
2010 unsigned long n, nc, l;
2011 unsigned int llen, ctype_num, i;
2012 X509_NAME *xn = NULL;
2013 const unsigned char *p, *q;
2015 STACK_OF(X509_NAME) *ca_sk = NULL;
2017 n = s->method->ssl_get_message(s,
2018 SSL3_ST_CR_CERT_REQ_A,
2019 SSL3_ST_CR_CERT_REQ_B,
2020 -1, s->max_cert_list, &ok);
2025 s->s3->tmp.cert_req = 0;
2027 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2028 s->s3->tmp.reuse_message = 1;
2030 * If we get here we don't need any cached handshake records as we
2031 * wont be doing client auth.
2033 if (s->s3->handshake_buffer) {
2034 if (!ssl3_digest_cached_records(s))
2040 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2041 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2042 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2046 /* TLS does not like anon-DH with client cert */
2047 if (s->version > SSL3_VERSION) {
2048 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2049 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2050 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2051 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2056 p = d = (unsigned char *)s->init_msg;
2058 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2059 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2063 /* get the certificate types */
2065 OPENSSL_free(s->cert->ctypes);
2066 s->cert->ctypes = NULL;
2067 if (ctype_num > SSL3_CT_NUMBER) {
2068 /* If we exceed static buffer copy all to cert structure */
2069 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2070 if (s->cert->ctypes == NULL) {
2071 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2074 memcpy(s->cert->ctypes, p, ctype_num);
2075 s->cert->ctype_num = (size_t)ctype_num;
2076 ctype_num = SSL3_CT_NUMBER;
2078 for (i = 0; i < ctype_num; i++)
2079 s->s3->tmp.ctype[i] = p[i];
2081 if (SSL_USE_SIGALGS(s)) {
2084 * Check we have enough room for signature algorithms and following
2087 if ((unsigned long)(p - d + llen + 2) > n) {
2088 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2089 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2090 SSL_R_DATA_LENGTH_TOO_LONG);
2093 /* Clear certificate digests and validity flags */
2094 for (i = 0; i < SSL_PKEY_NUM; i++) {
2095 s->cert->pkeys[i].digest = NULL;
2096 s->cert->pkeys[i].valid_flags = 0;
2098 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2100 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2101 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2104 if (!tls1_process_sigalgs(s)) {
2105 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2106 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2112 /* get the CA RDNs */
2115 if ((unsigned long)(p - d + llen) != n) {
2116 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2117 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2121 for (nc = 0; nc < llen;) {
2123 if ((l + nc + 2) > llen) {
2124 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2125 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2131 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2132 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2133 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2138 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2139 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2140 SSL_R_CA_DN_LENGTH_MISMATCH);
2143 if (!sk_X509_NAME_push(ca_sk, xn)) {
2144 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2152 /* we should setup a certificate to return.... */
2153 s->s3->tmp.cert_req = 1;
2154 s->s3->tmp.ctype_num = ctype_num;
2155 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2156 s->s3->tmp.ca_names = ca_sk;
2162 s->state = SSL_ST_ERR;
2164 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2168 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2170 return (X509_NAME_cmp(*a, *b));
2173 #ifndef OPENSSL_NO_TLSEXT
2174 int ssl3_get_new_session_ticket(SSL *s)
2176 int ok, al, ret = 0, ticklen;
2178 const unsigned char *p;
2181 n = s->method->ssl_get_message(s,
2182 SSL3_ST_CR_SESSION_TICKET_A,
2183 SSL3_ST_CR_SESSION_TICKET_B,
2184 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2190 /* need at least ticket_lifetime_hint + ticket length */
2191 al = SSL_AD_DECODE_ERROR;
2192 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2196 p = d = (unsigned char *)s->init_msg;
2197 n2l(p, s->session->tlsext_tick_lifetime_hint);
2199 /* ticket_lifetime_hint + ticket_length + ticket */
2200 if (ticklen + 6 != n) {
2201 al = SSL_AD_DECODE_ERROR;
2202 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2205 OPENSSL_free(s->session->tlsext_tick);
2206 s->session->tlsext_ticklen = 0;
2207 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2208 if (!s->session->tlsext_tick) {
2209 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2212 memcpy(s->session->tlsext_tick, p, ticklen);
2213 s->session->tlsext_ticklen = ticklen;
2215 * There are two ways to detect a resumed ticket session. One is to set
2216 * an appropriate session ID and then the server must return a match in
2217 * ServerHello. This allows the normal client session ID matching to work
2218 * and we know much earlier that the ticket has been accepted. The
2219 * other way is to set zero length session ID when the ticket is
2220 * presented and rely on the handshake to determine session resumption.
2221 * We choose the former approach because this fits in with assumptions
2222 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2223 * SHA256 is disabled) hash of the ticket.
2225 EVP_Digest(p, ticklen,
2226 s->session->session_id, &s->session->session_id_length,
2227 EVP_sha256(), NULL);
2231 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2233 s->state = SSL_ST_ERR;
2237 int ssl3_get_cert_status(SSL *s)
2240 unsigned long resplen, n;
2241 const unsigned char *p;
2243 n = s->method->ssl_get_message(s,
2244 SSL3_ST_CR_CERT_STATUS_A,
2245 SSL3_ST_CR_CERT_STATUS_B,
2246 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2251 /* need at least status type + length */
2252 al = SSL_AD_DECODE_ERROR;
2253 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2256 p = (unsigned char *)s->init_msg;
2257 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2258 al = SSL_AD_DECODE_ERROR;
2259 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2263 if (resplen + 4 != n) {
2264 al = SSL_AD_DECODE_ERROR;
2265 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2268 OPENSSL_free(s->tlsext_ocsp_resp);
2269 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2270 if (!s->tlsext_ocsp_resp) {
2271 al = SSL_AD_INTERNAL_ERROR;
2272 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2275 s->tlsext_ocsp_resplen = resplen;
2276 if (s->ctx->tlsext_status_cb) {
2278 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2280 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2281 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2285 al = SSL_AD_INTERNAL_ERROR;
2286 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2292 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2293 s->state = SSL_ST_ERR;
2298 int ssl3_get_server_done(SSL *s)
2303 /* Second to last param should be very small, like 0 :-) */
2304 n = s->method->ssl_get_message(s,
2305 SSL3_ST_CR_SRVR_DONE_A,
2306 SSL3_ST_CR_SRVR_DONE_B,
2307 SSL3_MT_SERVER_DONE, 30, &ok);
2312 /* should contain no data */
2313 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2314 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2315 s->state = SSL_ST_ERR;
2322 int ssl3_send_client_key_exchange(SSL *s)
2326 unsigned long alg_k;
2327 #ifndef OPENSSL_NO_RSA
2329 EVP_PKEY *pkey = NULL;
2331 #ifndef OPENSSL_NO_KRB5
2333 #endif /* OPENSSL_NO_KRB5 */
2334 #ifndef OPENSSL_NO_EC
2335 EC_KEY *clnt_ecdh = NULL;
2336 const EC_POINT *srvr_ecpoint = NULL;
2337 EVP_PKEY *srvr_pub_pkey = NULL;
2338 unsigned char *encodedPoint = NULL;
2339 int encoded_pt_len = 0;
2340 BN_CTX *bn_ctx = NULL;
2342 unsigned char *pms = NULL;
2345 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2346 p = ssl_handshake_start(s);
2348 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2350 /* Fool emacs indentation */
2353 #ifndef OPENSSL_NO_RSA
2354 else if (alg_k & SSL_kRSA) {
2356 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2357 pms = OPENSSL_malloc(pmslen);
2361 if (s->session->sess_cert == NULL) {
2363 * We should always have a server certificate with SSL_kRSA.
2365 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2366 ERR_R_INTERNAL_ERROR);
2370 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2371 rsa = s->session->sess_cert->peer_rsa_tmp;
2374 X509_get_pubkey(s->session->
2375 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2377 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2378 || (pkey->pkey.rsa == NULL)) {
2379 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2380 ERR_R_INTERNAL_ERROR);
2383 rsa = pkey->pkey.rsa;
2384 EVP_PKEY_free(pkey);
2387 pms[0] = s->client_version >> 8;
2388 pms[1] = s->client_version & 0xff;
2389 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2393 /* Fix buf for TLS and beyond */
2394 if (s->version > SSL3_VERSION)
2396 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2398 if (s->options & SSL_OP_PKCS1_CHECK_1)
2400 if (s->options & SSL_OP_PKCS1_CHECK_2)
2404 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2405 SSL_R_BAD_RSA_ENCRYPT);
2409 /* Fix buf for TLS and beyond */
2410 if (s->version > SSL3_VERSION) {
2416 #ifndef OPENSSL_NO_KRB5
2417 else if (alg_k & SSL_kKRB5) {
2418 krb5_error_code krb5rc;
2419 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2420 /* krb5_data krb5_ap_req; */
2421 krb5_data *enc_ticket;
2422 krb5_data authenticator, *authp = NULL;
2423 EVP_CIPHER_CTX ciph_ctx;
2424 const EVP_CIPHER *enc = NULL;
2425 unsigned char iv[EVP_MAX_IV_LENGTH];
2426 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2427 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2428 int padl, outl = sizeof(epms);
2430 EVP_CIPHER_CTX_init(&ciph_ctx);
2433 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2435 # endif /* KSSL_DEBUG */
2438 # ifdef KRB5SENDAUTH
2440 authp = &authenticator;
2441 # endif /* KRB5SENDAUTH */
2443 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2444 enc = kssl_map_enc(kssl_ctx->enctype);
2449 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2450 if (krb5rc && kssl_err.text)
2451 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2454 # endif /* KSSL_DEBUG */
2457 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2458 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2463 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2464 * in place of RFC 2712 KerberosWrapper, as in:
2466 * Send ticket (copy to *p, set n = length)
2467 * n = krb5_ap_req.length;
2468 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2469 * if (krb5_ap_req.data)
2470 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2472 * Now using real RFC 2712 KerberosWrapper
2473 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2474 * Note: 2712 "opaque" types are here replaced
2475 * with a 2-byte length followed by the value.
2477 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2478 * Where "xx xx" = length bytes. Shown here with
2479 * optional authenticator omitted.
2482 /* KerberosWrapper.Ticket */
2483 s2n(enc_ticket->length, p);
2484 memcpy(p, enc_ticket->data, enc_ticket->length);
2485 p += enc_ticket->length;
2486 n = enc_ticket->length + 2;
2488 /* KerberosWrapper.Authenticator */
2489 if (authp && authp->length) {
2490 s2n(authp->length, p);
2491 memcpy(p, authp->data, authp->length);
2493 n += authp->length + 2;
2499 s2n(0, p); /* null authenticator length */
2503 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2504 pms = OPENSSL_malloc(pmslen);
2508 pms[0] = s->client_version >> 8;
2509 pms[1] = s->client_version & 0xff;
2510 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2514 * 20010420 VRS. Tried it this way; failed.
2515 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2516 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2517 * kssl_ctx->length);
2518 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2521 memset(iv, 0, sizeof(iv)); /* per RFC 1510 */
2522 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2523 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen);
2524 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2526 if (outl > (int)sizeof epms) {
2527 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2528 ERR_R_INTERNAL_ERROR);
2531 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2533 /* KerberosWrapper.EncryptedPreMasterSecret */
2535 memcpy(p, epms, outl);
2538 OPENSSL_cleanse(epms, outl);
2541 #ifndef OPENSSL_NO_DH
2542 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2543 DH *dh_srvr, *dh_clnt;
2544 SESS_CERT *scert = s->session->sess_cert;
2546 if (scert == NULL) {
2547 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2548 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2549 SSL_R_UNEXPECTED_MESSAGE);
2553 if (scert->peer_dh_tmp != NULL)
2554 dh_srvr = scert->peer_dh_tmp;
2556 /* we get them from the cert */
2557 int idx = scert->peer_cert_type;
2558 EVP_PKEY *spkey = NULL;
2561 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2563 dh_srvr = EVP_PKEY_get1_DH(spkey);
2564 EVP_PKEY_free(spkey);
2566 if (dh_srvr == NULL) {
2567 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2568 ERR_R_INTERNAL_ERROR);
2572 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2573 /* Use client certificate key */
2574 EVP_PKEY *clkey = s->cert->key->privatekey;
2577 dh_clnt = EVP_PKEY_get1_DH(clkey);
2578 if (dh_clnt == NULL) {
2579 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2580 ERR_R_INTERNAL_ERROR);
2584 /* generate a new random key */
2585 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2586 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2589 if (!DH_generate_key(dh_clnt)) {
2590 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2596 pmslen = DH_size(dh_clnt);
2597 pms = OPENSSL_malloc(pmslen);
2602 * use the 'p' output buffer for the DH key, but make sure to
2603 * clear it out afterwards
2606 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2607 if (scert->peer_dh_tmp == NULL)
2611 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2617 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2620 /* send off the data */
2621 n = BN_num_bytes(dh_clnt->pub_key);
2623 BN_bn2bin(dh_clnt->pub_key, p);
2629 /* perhaps clean things up a bit EAY EAY EAY EAY */
2633 #ifndef OPENSSL_NO_EC
2634 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2635 const EC_GROUP *srvr_group = NULL;
2637 int ecdh_clnt_cert = 0;
2640 if (s->session->sess_cert == NULL) {
2641 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2642 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2643 SSL_R_UNEXPECTED_MESSAGE);
2648 * Did we send out the client's ECDH share for use in premaster
2649 * computation as part of client certificate? If so, set
2650 * ecdh_clnt_cert to 1.
2652 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2654 * XXX: For now, we do not support client
2655 * authentication using ECDH certificates.
2656 * To add such support, one needs to add
2657 * code that checks for appropriate
2658 * conditions and sets ecdh_clnt_cert to 1.
2659 * For example, the cert have an ECC
2660 * key on the same curve as the server's
2661 * and the key should be authorized for
2664 * One also needs to add code in ssl3_connect
2665 * to skip sending the certificate verify
2668 * if ((s->cert->key->privatekey != NULL) &&
2669 * (s->cert->key->privatekey->type ==
2670 * EVP_PKEY_EC) && ...)
2671 * ecdh_clnt_cert = 1;
2675 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2676 tkey = s->session->sess_cert->peer_ecdh_tmp;
2678 /* Get the Server Public Key from Cert */
2680 X509_get_pubkey(s->session->
2681 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2682 if ((srvr_pub_pkey == NULL)
2683 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2684 || (srvr_pub_pkey->pkey.ec == NULL)) {
2685 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2686 ERR_R_INTERNAL_ERROR);
2690 tkey = srvr_pub_pkey->pkey.ec;
2693 srvr_group = EC_KEY_get0_group(tkey);
2694 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2696 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2697 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2698 ERR_R_INTERNAL_ERROR);
2702 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2703 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2704 ERR_R_MALLOC_FAILURE);
2708 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2709 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2712 if (ecdh_clnt_cert) {
2714 * Reuse key info from our certificate We only need our
2715 * private key to perform the ECDH computation.
2717 const BIGNUM *priv_key;
2718 tkey = s->cert->key->privatekey->pkey.ec;
2719 priv_key = EC_KEY_get0_private_key(tkey);
2720 if (priv_key == NULL) {
2721 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2722 ERR_R_MALLOC_FAILURE);
2725 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2726 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2730 /* Generate a new ECDH key pair */
2731 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2732 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2739 * use the 'p' output buffer for the ECDH key, but make sure to
2740 * clear it out afterwards
2743 field_size = EC_GROUP_get_degree(srvr_group);
2744 if (field_size <= 0) {
2745 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2748 pmslen = (field_size + 7) / 8;
2749 pms = OPENSSL_malloc(pmslen);
2752 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2753 if (n <= 0 || pmslen != (size_t)n) {
2754 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2758 if (ecdh_clnt_cert) {
2759 /* Send empty client key exch message */
2763 * First check the size of encoding and allocate memory
2767 EC_POINT_point2oct(srvr_group,
2768 EC_KEY_get0_public_key(clnt_ecdh),
2769 POINT_CONVERSION_UNCOMPRESSED,
2772 encodedPoint = (unsigned char *)
2773 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2774 bn_ctx = BN_CTX_new();
2775 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2776 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2777 ERR_R_MALLOC_FAILURE);
2781 /* Encode the public key */
2782 n = EC_POINT_point2oct(srvr_group,
2783 EC_KEY_get0_public_key(clnt_ecdh),
2784 POINT_CONVERSION_UNCOMPRESSED,
2785 encodedPoint, encoded_pt_len, bn_ctx);
2787 *p = n; /* length of encoded point */
2788 /* Encoded point will be copied here */
2790 /* copy the point */
2791 memcpy(p, encodedPoint, n);
2792 /* increment n to account for length field */
2796 /* Free allocated memory */
2797 BN_CTX_free(bn_ctx);
2798 OPENSSL_free(encodedPoint);
2799 EC_KEY_free(clnt_ecdh);
2800 EVP_PKEY_free(srvr_pub_pkey);
2802 #endif /* !OPENSSL_NO_EC */
2803 else if (alg_k & SSL_kGOST) {
2804 /* GOST key exchange message creation */
2805 EVP_PKEY_CTX *pkey_ctx;
2808 unsigned int md_len;
2810 unsigned char shared_ukm[32], tmp[256];
2811 EVP_MD_CTX *ukm_hash;
2815 pms = OPENSSL_malloc(pmslen);
2820 * Get server sertificate PKEY and create ctx from it
2824 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2828 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2830 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2831 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2835 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2836 X509_get_pubkey(peer_cert), NULL);
2838 * If we have send a certificate, and certificate key
2840 * * parameters match those of server certificate, use
2841 * certificate key for key exchange
2844 /* Otherwise, generate ephemeral key pair */
2846 EVP_PKEY_encrypt_init(pkey_ctx);
2847 /* Generate session key */
2848 if (RAND_bytes(pms, pmslen) <= 0) {
2849 EVP_PKEY_CTX_free(pkey_ctx);
2850 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2851 ERR_R_INTERNAL_ERROR);
2855 * If we have client certificate, use its secret as peer key
2857 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2858 if (EVP_PKEY_derive_set_peer
2859 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2861 * If there was an error - just ignore it. Ephemeral key
2868 * Compute shared IV and store it in algorithm-specific context
2871 ukm_hash = EVP_MD_CTX_create();
2872 EVP_DigestInit(ukm_hash,
2873 EVP_get_digestbynid(NID_id_GostR3411_94));
2874 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2876 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2878 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2879 EVP_MD_CTX_destroy(ukm_hash);
2880 if (EVP_PKEY_CTX_ctrl
2881 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2883 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2887 /* Make GOST keytransport blob message */
2889 * Encapsulate it into sequence
2891 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2893 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2894 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2898 if (msglen >= 0x80) {
2900 *(p++) = msglen & 0xff;
2903 *(p++) = msglen & 0xff;
2906 memcpy(p, tmp, msglen);
2907 /* Check if pubkey from client certificate was used */
2908 if (EVP_PKEY_CTX_ctrl
2909 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2910 /* Set flag "skip certificate verify" */
2911 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2913 EVP_PKEY_CTX_free(pkey_ctx);
2914 EVP_PKEY_free(pub_key);
2917 #ifndef OPENSSL_NO_SRP
2918 else if (alg_k & SSL_kSRP) {
2919 if (s->srp_ctx.A != NULL) {
2920 /* send off the data */
2921 n = BN_num_bytes(s->srp_ctx.A);
2923 BN_bn2bin(s->srp_ctx.A, p);
2926 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2927 ERR_R_INTERNAL_ERROR);
2930 OPENSSL_free(s->session->srp_username);
2931 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2932 if (s->session->srp_username == NULL) {
2933 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2934 ERR_R_MALLOC_FAILURE);
2939 #ifndef OPENSSL_NO_PSK
2940 else if (alg_k & SSL_kPSK) {
2942 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2943 * \0-terminated identity. The last byte is for us for simulating
2946 char identity[PSK_MAX_IDENTITY_LEN + 2];
2947 size_t identity_len;
2948 unsigned char *t = NULL;
2949 unsigned int psk_len = 0;
2953 if (s->psk_client_callback == NULL) {
2954 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2955 SSL_R_PSK_NO_CLIENT_CB);
2959 memset(identity, 0, sizeof(identity));
2960 /* Allocate maximum size buffer */
2961 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2962 pms = OPENSSL_malloc(pmslen);
2966 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2967 identity, sizeof(identity) - 1,
2969 if (psk_len > PSK_MAX_PSK_LEN) {
2970 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2971 ERR_R_INTERNAL_ERROR);
2973 } else if (psk_len == 0) {
2974 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2975 SSL_R_PSK_IDENTITY_NOT_FOUND);
2978 /* Change pmslen to real length */
2979 pmslen = 2 + psk_len + 2 + psk_len;
2980 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2981 identity_len = strlen(identity);
2982 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2983 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2984 ERR_R_INTERNAL_ERROR);
2987 /* create PSK pre_master_secret */
2989 memmove(pms + psk_len + 4, pms, psk_len);
2991 memset(t, 0, psk_len);
2995 OPENSSL_free(s->session->psk_identity_hint);
2996 s->session->psk_identity_hint =
2997 BUF_strdup(s->ctx->psk_identity_hint);
2998 if (s->ctx->psk_identity_hint != NULL
2999 && s->session->psk_identity_hint == NULL) {
3000 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3001 ERR_R_MALLOC_FAILURE);
3005 OPENSSL_free(s->session->psk_identity);
3006 s->session->psk_identity = BUF_strdup(identity);
3007 if (s->session->psk_identity == NULL) {
3008 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3009 ERR_R_MALLOC_FAILURE);
3013 s2n(identity_len, p);
3014 memcpy(p, identity, identity_len);
3015 n = 2 + identity_len;
3018 OPENSSL_cleanse(identity, sizeof(identity));
3020 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3026 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3027 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3031 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
3032 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3033 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3037 s->state = SSL3_ST_CW_KEY_EXCH_B;
3040 /* SSL3_ST_CW_KEY_EXCH_B */
3041 n = ssl_do_write(s);
3042 #ifndef OPENSSL_NO_SRP
3044 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3046 * If everything written generate master key: no need to save PMS as
3047 * SRP_generate_client_master_secret generates it internally.
3050 if ((s->session->master_key_length =
3051 SRP_generate_client_master_secret(s,
3052 s->session->master_key)) <
3054 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3055 ERR_R_INTERNAL_ERROR);
3061 /* If we haven't written everything save PMS */
3064 s->cert->pmslen = pmslen;
3066 /* If we don't have a PMS restore */
3069 pmslen = s->cert->pmslen;
3072 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3073 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3076 s->session->master_key_length =
3077 s->method->ssl3_enc->generate_master_secret(s,
3079 session->master_key,
3081 OPENSSL_clear_free(pms, pmslen);
3082 s->cert->pms = NULL;
3083 if (s->session->master_key_length < 0) {
3084 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3085 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3091 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3092 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3094 OPENSSL_clear_free(pms, pmslen);
3095 s->cert->pms = NULL;
3096 #ifndef OPENSSL_NO_EC
3097 BN_CTX_free(bn_ctx);
3098 OPENSSL_free(encodedPoint);
3099 EC_KEY_free(clnt_ecdh);
3100 EVP_PKEY_free(srvr_pub_pkey);
3102 s->state = SSL_ST_ERR;
3106 int ssl3_send_client_verify(SSL *s)
3109 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3111 EVP_PKEY_CTX *pctx = NULL;
3117 EVP_MD_CTX_init(&mctx);
3119 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3120 p = ssl_handshake_start(s);
3121 pkey = s->cert->key->privatekey;
3122 /* Create context from key and test if sha1 is allowed as digest */
3123 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3124 EVP_PKEY_sign_init(pctx);
3125 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3126 if (!SSL_USE_SIGALGS(s))
3127 s->method->ssl3_enc->cert_verify_mac(s,
3130 [MD5_DIGEST_LENGTH]));
3135 * For TLS v1.2 send signature algorithm and signature using agreed
3136 * digest and cached handshake records.
3138 if (SSL_USE_SIGALGS(s)) {
3141 const EVP_MD *md = s->cert->key->digest;
3142 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3143 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3144 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3149 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3152 if (!EVP_SignInit_ex(&mctx, md, NULL)
3153 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3154 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3155 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3161 * For extended master secret we've already digested cached
3164 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
3165 BIO_free(s->s3->handshake_buffer);
3166 s->s3->handshake_buffer = NULL;
3167 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3168 } else if (!ssl3_digest_cached_records(s))
3171 #ifndef OPENSSL_NO_RSA
3172 if (pkey->type == EVP_PKEY_RSA) {
3173 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3174 if (RSA_sign(NID_md5_sha1, data,
3175 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3176 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3177 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3184 #ifndef OPENSSL_NO_DSA
3185 if (pkey->type == EVP_PKEY_DSA) {
3186 if (!DSA_sign(pkey->save_type,
3187 &(data[MD5_DIGEST_LENGTH]),
3188 SHA_DIGEST_LENGTH, &(p[2]),
3189 (unsigned int *)&j, pkey->pkey.dsa)) {
3190 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3197 #ifndef OPENSSL_NO_EC
3198 if (pkey->type == EVP_PKEY_EC) {
3199 if (!ECDSA_sign(pkey->save_type,
3200 &(data[MD5_DIGEST_LENGTH]),
3201 SHA_DIGEST_LENGTH, &(p[2]),
3202 (unsigned int *)&j, pkey->pkey.ec)) {
3203 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3210 if (pkey->type == NID_id_GostR3410_94
3211 || pkey->type == NID_id_GostR3410_2001) {
3212 unsigned char signbuf[64];
3214 size_t sigsize = 64;
3215 s->method->ssl3_enc->cert_verify_mac(s,
3216 NID_id_GostR3411_94, data);
3217 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3218 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3221 for (i = 63, j = 0; i >= 0; j++, i--) {
3222 p[2 + j] = signbuf[i];
3227 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3230 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3231 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3234 s->state = SSL3_ST_CW_CERT_VRFY_B;
3236 EVP_MD_CTX_cleanup(&mctx);
3237 EVP_PKEY_CTX_free(pctx);
3238 return ssl_do_write(s);
3240 EVP_MD_CTX_cleanup(&mctx);
3241 EVP_PKEY_CTX_free(pctx);
3242 s->state = SSL_ST_ERR;
3247 * Check a certificate can be used for client authentication. Currently check
3248 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3249 * certificates can be used and optionally checks suitability for Suite B.
3251 static int ssl3_check_client_certificate(SSL *s)
3253 unsigned long alg_k;
3254 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3256 /* If no suitable signature algorithm can't use certificate */
3257 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3260 * If strict mode check suitability of chain before using it. This also
3261 * adjusts suite B digest if necessary.
3263 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3264 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3266 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3267 /* See if we can use client certificate for fixed DH */
3268 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3269 SESS_CERT *scert = s->session->sess_cert;
3270 int i = scert->peer_cert_type;
3271 EVP_PKEY *clkey = NULL, *spkey = NULL;
3272 clkey = s->cert->key->privatekey;
3273 /* If client key not DH assume it can be used */
3274 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3277 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3279 /* Compare server and client parameters */
3280 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3281 EVP_PKEY_free(spkey);
3285 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3290 int ssl3_send_client_certificate(SSL *s)
3293 EVP_PKEY *pkey = NULL;
3296 if (s->state == SSL3_ST_CW_CERT_A) {
3297 /* Let cert callback update client certificates if required */
3298 if (s->cert->cert_cb) {
3299 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3301 s->rwstate = SSL_X509_LOOKUP;
3305 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3306 s->state = SSL_ST_ERR;
3309 s->rwstate = SSL_NOTHING;
3311 if (ssl3_check_client_certificate(s))
3312 s->state = SSL3_ST_CW_CERT_C;
3314 s->state = SSL3_ST_CW_CERT_B;
3317 /* We need to get a client cert */
3318 if (s->state == SSL3_ST_CW_CERT_B) {
3320 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3321 * return(-1); We then get retied later
3324 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3326 s->rwstate = SSL_X509_LOOKUP;
3329 s->rwstate = SSL_NOTHING;
3330 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3331 s->state = SSL3_ST_CW_CERT_B;
3332 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3334 } else if (i == 1) {
3336 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3337 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3341 EVP_PKEY_free(pkey);
3342 if (i && !ssl3_check_client_certificate(s))
3345 if (s->version == SSL3_VERSION) {
3346 s->s3->tmp.cert_req = 0;
3347 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3350 s->s3->tmp.cert_req = 2;
3354 /* Ok, we have a cert */
3355 s->state = SSL3_ST_CW_CERT_C;
3358 if (s->state == SSL3_ST_CW_CERT_C) {
3359 s->state = SSL3_ST_CW_CERT_D;
3360 if (!ssl3_output_cert_chain(s,
3361 (s->s3->tmp.cert_req ==
3362 2) ? NULL : s->cert->key)) {
3363 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3364 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3365 s->state = SSL_ST_ERR;
3369 /* SSL3_ST_CW_CERT_D */
3370 return ssl_do_write(s);
3373 #define has_bits(i,m) (((i)&(m)) == (m))
3375 int ssl3_check_cert_and_algorithm(SSL *s)
3379 EVP_PKEY *pkey = NULL;
3381 #ifndef OPENSSL_NO_RSA
3384 #ifndef OPENSSL_NO_DH
3388 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3389 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3391 /* we don't have a certificate */
3392 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3395 sc = s->session->sess_cert;
3397 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3400 #ifndef OPENSSL_NO_RSA
3401 rsa = s->session->sess_cert->peer_rsa_tmp;
3403 #ifndef OPENSSL_NO_DH
3404 dh = s->session->sess_cert->peer_dh_tmp;
3407 /* This is the passed certificate */
3409 idx = sc->peer_cert_type;
3410 #ifndef OPENSSL_NO_EC
3411 if (idx == SSL_PKEY_ECC) {
3412 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3414 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3419 } else if (alg_a & SSL_aECDSA) {
3420 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3421 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3423 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3424 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3428 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3429 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3430 EVP_PKEY_free(pkey);
3432 /* Check that we have a certificate if we require one */
3433 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3434 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3435 SSL_R_MISSING_RSA_SIGNING_CERT);
3438 #ifndef OPENSSL_NO_DSA
3439 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3440 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3441 SSL_R_MISSING_DSA_SIGNING_CERT);
3445 #ifndef OPENSSL_NO_RSA
3446 if ((alg_k & SSL_kRSA) &&
3447 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3448 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3449 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3453 #ifndef OPENSSL_NO_DH
3454 if ((alg_k & SSL_kDHE) &&
3455 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3456 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3458 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3459 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3460 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3461 SSL_R_MISSING_DH_RSA_CERT);
3464 # ifndef OPENSSL_NO_DSA
3465 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3466 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3467 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3468 SSL_R_MISSING_DH_DSA_CERT);
3474 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3475 #ifndef OPENSSL_NO_RSA
3476 if (alg_k & SSL_kRSA) {
3478 || RSA_size(rsa) * 8 >
3479 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3480 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3481 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3486 #ifndef OPENSSL_NO_DH
3487 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3489 || DH_size(dh) * 8 >
3490 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3491 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3492 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3498 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3499 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3505 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3510 #ifndef OPENSSL_NO_TLSEXT
3512 * Normally, we can tell if the server is resuming the session from
3513 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3514 * message after the ServerHello to determine if the server is resuming.
3515 * Therefore, we allow EAP-FAST to peek ahead.
3516 * ssl3_check_finished returns 1 if we are resuming from an external
3517 * pre-shared secret, we have a "ticket" and the next server handshake message
3518 * is Finished; and 0 otherwise. It returns -1 upon an error.
3520 static int ssl3_check_finished(SSL *s)
3524 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3525 !s->session->tlsext_tick)
3528 /* Need to permit this temporarily, in case the next message is Finished. */
3529 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3531 * This function is called when we might get a Certificate message instead,
3532 * so permit appropriate message length.
3533 * We ignore the return value as we're only interested in the message type
3534 * and not its length.
3536 s->method->ssl_get_message(s,
3539 -1, s->max_cert_list, &ok);
3540 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3545 s->s3->tmp.reuse_message = 1;
3547 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3550 /* If we're not done, then the CCS arrived early and we should bail. */
3551 if (s->s3->change_cipher_spec) {
3552 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3553 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3560 # ifndef OPENSSL_NO_NEXTPROTONEG
3561 int ssl3_send_next_proto(SSL *s)
3563 unsigned int len, padding_len;
3566 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3567 len = s->next_proto_negotiated_len;
3568 padding_len = 32 - ((len + 2) % 32);
3569 d = (unsigned char *)s->init_buf->data;
3571 memcpy(d + 5, s->next_proto_negotiated, len);
3572 d[5 + len] = padding_len;
3573 memset(d + 6 + len, 0, padding_len);
3574 *(d++) = SSL3_MT_NEXT_PROTO;
3575 l2n3(2 + len + padding_len, d);
3576 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3577 s->init_num = 4 + 2 + len + padding_len;
3581 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3586 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3589 #ifndef OPENSSL_NO_ENGINE
3590 if (s->ctx->client_cert_engine) {
3591 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3592 SSL_get_client_CA_list(s),
3593 px509, ppkey, NULL, NULL, NULL);
3598 if (s->ctx->client_cert_cb)
3599 i = s->ctx->client_cert_cb(s, px509, ppkey);