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
);
169 #ifndef OPENSSL_NO_SSL3_METHOD
170 static const SSL_METHOD
*ssl3_get_client_method(int ver
)
172 if (ver
== SSL3_VERSION
)
173 return (SSLv3_client_method());
178 IMPLEMENT_ssl3_meth_func(SSLv3_client_method
,
179 ssl_undefined_function
,
180 ssl3_connect
, ssl3_get_client_method
)
182 int ssl3_connect(SSL
*s
)
185 unsigned long Time
= (unsigned long)time(NULL
);
186 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
188 int new_state
, state
, skip
= 0;
190 RAND_add(&Time
, sizeof(Time
), 0);
194 if (s
->info_callback
!= NULL
)
195 cb
= s
->info_callback
;
196 else if (s
->ctx
->info_callback
!= NULL
)
197 cb
= s
->ctx
->info_callback
;
200 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
205 #ifndef OPENSSL_NO_HEARTBEATS
207 * If we're awaiting a HeartbeatResponse, pretend we already got and
208 * don't await it anymore, because Heartbeats don't make sense during
211 if (s
->tlsext_hb_pending
) {
212 s
->tlsext_hb_pending
= 0;
221 case SSL_ST_RENEGOTIATE
:
223 s
->state
= SSL_ST_CONNECT
;
224 s
->ctx
->stats
.sess_connect_renegotiate
++;
228 case SSL_ST_BEFORE
| SSL_ST_CONNECT
:
229 case SSL_ST_OK
| SSL_ST_CONNECT
:
233 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
235 if ((s
->version
& 0xff00) != 0x0300) {
236 SSLerr(SSL_F_SSL3_CONNECT
, ERR_R_INTERNAL_ERROR
);
241 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
242 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_VERSION_TOO_LOW
);
246 /* s->version=SSL3_VERSION; */
247 s
->type
= SSL_ST_CONNECT
;
249 if (s
->init_buf
== NULL
) {
250 if ((buf
= BUF_MEM_new()) == NULL
) {
254 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
262 if (!ssl3_setup_buffers(s
)) {
267 /* setup buffing BIO */
268 if (!ssl_init_wbio_buffer(s
, 0)) {
273 /* don't push the buffering BIO quite yet */
275 ssl3_init_finished_mac(s
);
277 s
->state
= SSL3_ST_CW_CLNT_HELLO_A
;
278 s
->ctx
->stats
.sess_connect
++;
280 s
->s3
->flags
&= ~SSL3_FLAGS_CCS_OK
;
282 * Should have been reset by ssl3_get_finished, too.
284 s
->s3
->change_cipher_spec
= 0;
287 case SSL3_ST_CW_CLNT_HELLO_A
:
288 case SSL3_ST_CW_CLNT_HELLO_B
:
291 ret
= ssl3_client_hello(s
);
294 s
->state
= SSL3_ST_CR_SRVR_HELLO_A
;
297 /* turn on buffering for the next lot of output */
298 if (s
->bbio
!= s
->wbio
)
299 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
303 case SSL3_ST_CR_SRVR_HELLO_A
:
304 case SSL3_ST_CR_SRVR_HELLO_B
:
305 ret
= ssl3_get_server_hello(s
);
310 s
->state
= SSL3_ST_CR_FINISHED_A
;
311 #ifndef OPENSSL_NO_TLSEXT
312 if (s
->tlsext_ticket_expected
) {
313 /* receive renewed session ticket */
314 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
318 s
->state
= SSL3_ST_CR_CERT_A
;
322 case SSL3_ST_CR_CERT_A
:
323 case SSL3_ST_CR_CERT_B
:
324 /* Check if it is anon DH/ECDH, SRP auth */
328 new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
))
329 && !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
)) {
330 ret
= ssl3_get_server_certificate(s
);
333 #ifndef OPENSSL_NO_TLSEXT
334 if (s
->tlsext_status_expected
)
335 s
->state
= SSL3_ST_CR_CERT_STATUS_A
;
337 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
340 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
346 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
351 case SSL3_ST_CR_KEY_EXCH_A
:
352 case SSL3_ST_CR_KEY_EXCH_B
:
353 ret
= ssl3_get_key_exchange(s
);
356 s
->state
= SSL3_ST_CR_CERT_REQ_A
;
360 * at this point we check that we have the required stuff from
363 if (!ssl3_check_cert_and_algorithm(s
)) {
369 case SSL3_ST_CR_CERT_REQ_A
:
370 case SSL3_ST_CR_CERT_REQ_B
:
371 ret
= ssl3_get_certificate_request(s
);
374 s
->state
= SSL3_ST_CR_SRVR_DONE_A
;
378 case SSL3_ST_CR_SRVR_DONE_A
:
379 case SSL3_ST_CR_SRVR_DONE_B
:
380 ret
= ssl3_get_server_done(s
);
383 #ifndef OPENSSL_NO_SRP
384 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
385 if ((ret
= SRP_Calc_A_param(s
)) <= 0) {
386 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_SRP_A_CALC
);
387 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
392 if (s
->s3
->tmp
.cert_req
)
393 s
->state
= SSL3_ST_CW_CERT_A
;
395 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
400 case SSL3_ST_CW_CERT_A
:
401 case SSL3_ST_CW_CERT_B
:
402 case SSL3_ST_CW_CERT_C
:
403 case SSL3_ST_CW_CERT_D
:
404 ret
= ssl3_send_client_certificate(s
);
407 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
411 case SSL3_ST_CW_KEY_EXCH_A
:
412 case SSL3_ST_CW_KEY_EXCH_B
:
413 ret
= ssl3_send_client_key_exchange(s
);
417 * EAY EAY EAY need to check for DH fix cert sent back
420 * For TLS, cert_req is set to 2, so a cert chain of nothing is
421 * sent, but no verify packet is sent
424 * XXX: For now, we do not support client authentication in ECDH
425 * cipher suites with ECDH (rather than ECDSA) certificates. We
426 * need to skip the certificate verify message when client's
427 * ECDH public key is sent inside the client certificate.
429 if (s
->s3
->tmp
.cert_req
== 1) {
430 s
->state
= SSL3_ST_CW_CERT_VRFY_A
;
432 s
->state
= SSL3_ST_CW_CHANGE_A
;
434 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
435 s
->state
= SSL3_ST_CW_CHANGE_A
;
441 case SSL3_ST_CW_CERT_VRFY_A
:
442 case SSL3_ST_CW_CERT_VRFY_B
:
443 ret
= ssl3_send_client_verify(s
);
446 s
->state
= SSL3_ST_CW_CHANGE_A
;
450 case SSL3_ST_CW_CHANGE_A
:
451 case SSL3_ST_CW_CHANGE_B
:
452 ret
= ssl3_send_change_cipher_spec(s
,
454 SSL3_ST_CW_CHANGE_B
);
458 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
459 s
->state
= SSL3_ST_CW_FINISHED_A
;
461 if (s
->s3
->next_proto_neg_seen
)
462 s
->state
= SSL3_ST_CW_NEXT_PROTO_A
;
464 s
->state
= SSL3_ST_CW_FINISHED_A
;
468 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
469 #ifdef OPENSSL_NO_COMP
470 s
->session
->compress_meth
= 0;
472 if (s
->s3
->tmp
.new_compression
== NULL
)
473 s
->session
->compress_meth
= 0;
475 s
->session
->compress_meth
= s
->s3
->tmp
.new_compression
->id
;
477 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
482 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
483 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
491 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
492 case SSL3_ST_CW_NEXT_PROTO_A
:
493 case SSL3_ST_CW_NEXT_PROTO_B
:
494 ret
= ssl3_send_next_proto(s
);
497 s
->state
= SSL3_ST_CW_FINISHED_A
;
501 case SSL3_ST_CW_FINISHED_A
:
502 case SSL3_ST_CW_FINISHED_B
:
503 ret
= ssl3_send_finished(s
,
504 SSL3_ST_CW_FINISHED_A
,
505 SSL3_ST_CW_FINISHED_B
,
507 ssl3_enc
->client_finished_label
,
509 ssl3_enc
->client_finished_label_len
);
512 s
->state
= SSL3_ST_CW_FLUSH
;
515 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
517 s
->s3
->tmp
.next_state
= SSL_ST_OK
;
518 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
519 s
->state
= SSL_ST_OK
;
520 s
->s3
->flags
|= SSL3_FLAGS_POP_BUFFER
;
521 s
->s3
->delay_buf_pop_ret
= 0;
524 #ifndef OPENSSL_NO_TLSEXT
526 * Allow NewSessionTicket if ticket expected
528 if (s
->tlsext_ticket_expected
)
529 s
->s3
->tmp
.next_state
= SSL3_ST_CR_SESSION_TICKET_A
;
533 s
->s3
->tmp
.next_state
= SSL3_ST_CR_FINISHED_A
;
538 #ifndef OPENSSL_NO_TLSEXT
539 case SSL3_ST_CR_SESSION_TICKET_A
:
540 case SSL3_ST_CR_SESSION_TICKET_B
:
541 ret
= ssl3_get_new_session_ticket(s
);
544 s
->state
= SSL3_ST_CR_FINISHED_A
;
548 case SSL3_ST_CR_CERT_STATUS_A
:
549 case SSL3_ST_CR_CERT_STATUS_B
:
550 ret
= ssl3_get_cert_status(s
);
553 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
558 case SSL3_ST_CR_FINISHED_A
:
559 case SSL3_ST_CR_FINISHED_B
:
560 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
561 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
562 SSL3_ST_CR_FINISHED_B
);
567 s
->state
= SSL3_ST_CW_CHANGE_A
;
569 s
->state
= SSL_ST_OK
;
573 case SSL3_ST_CW_FLUSH
:
574 s
->rwstate
= SSL_WRITING
;
575 if (BIO_flush(s
->wbio
) <= 0) {
579 s
->rwstate
= SSL_NOTHING
;
580 s
->state
= s
->s3
->tmp
.next_state
;
584 /* clean a few things up */
585 ssl3_cleanup_key_block(s
);
587 if (s
->init_buf
!= NULL
) {
588 BUF_MEM_free(s
->init_buf
);
593 * If we are not 'joining' the last two packets, remove the
596 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
597 ssl_free_wbio_buffer(s
);
598 /* else do it later in ssl3_write */
604 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
606 s
->ctx
->stats
.sess_hit
++;
610 s
->handshake_func
= ssl3_connect
;
611 s
->ctx
->stats
.sess_connect_good
++;
614 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
620 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_UNKNOWN_STATE
);
626 /* did we do anything */
627 if (!s
->s3
->tmp
.reuse_message
&& !skip
) {
629 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
633 if ((cb
!= NULL
) && (s
->state
!= state
)) {
634 new_state
= s
->state
;
636 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
637 s
->state
= new_state
;
647 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
651 int ssl3_client_hello(SSL
*s
)
654 unsigned char *p
, *d
;
658 #ifndef OPENSSL_NO_COMP
663 buf
= (unsigned char *)s
->init_buf
->data
;
664 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
) {
665 SSL_SESSION
*sess
= s
->session
;
666 if ((sess
== NULL
) ||
667 (sess
->ssl_version
!= s
->version
) ||
668 !sess
->session_id_length
|| (sess
->not_resumable
)) {
669 if (!ssl_get_new_session(s
, 0))
672 if (s
->method
->version
== DTLS_ANY_VERSION
) {
673 /* Determine which DTLS version to use */
674 int options
= s
->options
;
675 /* If DTLS 1.2 disabled correct the version number */
676 if (options
& SSL_OP_NO_DTLSv1_2
) {
677 if (tls1_suiteb(s
)) {
678 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,
679 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
683 * Disabling all versions is silly: return an error.
685 if (options
& SSL_OP_NO_DTLSv1
) {
686 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_WRONG_SSL_VERSION
);
690 * Update method so we don't use any DTLS 1.2 features.
692 s
->method
= DTLSv1_client_method();
693 s
->version
= DTLS1_VERSION
;
696 * We only support one version: update method
698 if (options
& SSL_OP_NO_DTLSv1
)
699 s
->method
= DTLSv1_2_client_method();
700 s
->version
= DTLS1_2_VERSION
;
702 s
->client_version
= s
->version
;
704 /* else use the pre-loaded session */
706 p
= s
->s3
->client_random
;
709 * for DTLS if client_random is initialized, reuse it, we are
710 * required to use same upon reply to HelloVerify
712 if (SSL_IS_DTLS(s
)) {
715 for (idx
= 0; idx
< sizeof(s
->s3
->client_random
); idx
++) {
724 if (i
&& ssl_fill_hello_random(s
, 0, p
,
725 sizeof(s
->s3
->client_random
)) <= 0)
728 /* Do the message type and length last */
729 d
= p
= ssl_handshake_start(s
);
732 * version indicates the negotiated version: for example from
733 * an SSLv2/v3 compatible client hello). The client_version
734 * field is the maximum version we permit and it is also
735 * used in RSA encrypted premaster secrets. Some servers can
736 * choke if we initially report a higher version then
737 * renegotiate to a lower one in the premaster secret. This
738 * didn't happen with TLS 1.0 as most servers supported it
739 * but it can with TLS 1.1 or later if the server only supports
742 * Possible scenario with previous logic:
743 * 1. Client hello indicates TLS 1.2
744 * 2. Server hello says TLS 1.0
745 * 3. RSA encrypted premaster secret uses 1.2.
746 * 4. Handhaked proceeds using TLS 1.0.
747 * 5. Server sends hello request to renegotiate.
748 * 6. Client hello indicates TLS v1.0 as we now
749 * know that is maximum server supports.
750 * 7. Server chokes on RSA encrypted premaster secret
751 * containing version 1.0.
753 * For interoperability it should be OK to always use the
754 * maximum version we support in client hello and then rely
755 * on the checking of version to ensure the servers isn't
756 * being inconsistent: for example initially negotiating with
757 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
758 * client_version in client hello and not resetting it to
759 * the negotiated version.
761 *(p
++) = s
->client_version
>> 8;
762 *(p
++) = s
->client_version
& 0xff;
765 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
766 p
+= SSL3_RANDOM_SIZE
;
772 i
= s
->session
->session_id_length
;
775 if (i
> (int)sizeof(s
->session
->session_id
)) {
776 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
779 memcpy(p
, s
->session
->session_id
, i
);
783 /* cookie stuff for DTLS */
784 if (SSL_IS_DTLS(s
)) {
785 if (s
->d1
->cookie_len
> sizeof(s
->d1
->cookie
)) {
786 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
789 *(p
++) = s
->d1
->cookie_len
;
790 memcpy(p
, s
->d1
->cookie
, s
->d1
->cookie_len
);
791 p
+= s
->d1
->cookie_len
;
794 /* Ciphers supported */
795 i
= ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &(p
[2]), 0);
797 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_NO_CIPHERS_AVAILABLE
);
800 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
802 * Some servers hang if client hello > 256 bytes as hack workaround
803 * chop number of supported ciphers to keep it well below this if we
806 if (TLS1_get_version(s
) >= TLS1_2_VERSION
807 && i
> OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
)
808 i
= OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
& ~1;
814 #ifdef OPENSSL_NO_COMP
818 if (!ssl_allow_compression(s
) || !s
->ctx
->comp_methods
)
821 j
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
823 for (i
= 0; i
< j
; i
++) {
824 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, i
);
828 *(p
++) = 0; /* Add the NULL method */
830 #ifndef OPENSSL_NO_TLSEXT
832 if (ssl_prepare_clienthello_tlsext(s
) <= 0) {
833 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
837 ssl_add_clienthello_tlsext(s
, p
, buf
+ SSL3_RT_MAX_PLAIN_LENGTH
,
839 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
840 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
846 if(!ssl_set_handshake_header(s
, SSL3_MT_CLIENT_HELLO
, l
)) {
847 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
848 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
851 s
->state
= SSL3_ST_CW_CLNT_HELLO_B
;
854 /* SSL3_ST_CW_CLNT_HELLO_B */
855 return ssl_do_write(s
);
860 int ssl3_get_server_hello(SSL
*s
)
862 STACK_OF(SSL_CIPHER
) *sk
;
865 unsigned char *p
, *d
;
866 int i
, al
= SSL_AD_INTERNAL_ERROR
, ok
;
869 #ifndef OPENSSL_NO_COMP
873 * Hello verify request and/or server hello version may not match so set
874 * first packet if we're negotiating version.
879 n
= s
->method
->ssl_get_message(s
,
880 SSL3_ST_CR_SRVR_HELLO_A
,
881 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, &ok
);
886 if (SSL_IS_DTLS(s
)) {
888 if (s
->s3
->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
889 if (s
->d1
->send_cookie
== 0) {
890 s
->s3
->tmp
.reuse_message
= 1;
892 } else { /* already sent a cookie */
894 al
= SSL_AD_UNEXPECTED_MESSAGE
;
895 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
901 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
902 al
= SSL_AD_UNEXPECTED_MESSAGE
;
903 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
907 d
= p
= (unsigned char *)s
->init_msg
;
908 if (s
->method
->version
== DTLS_ANY_VERSION
) {
909 /* Work out correct protocol version to use */
910 int hversion
= (p
[0] << 8) | p
[1];
911 int options
= s
->options
;
912 if (hversion
== DTLS1_2_VERSION
&& !(options
& SSL_OP_NO_DTLSv1_2
))
913 s
->method
= DTLSv1_2_client_method();
914 else if (tls1_suiteb(s
)) {
915 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
916 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
917 s
->version
= hversion
;
918 al
= SSL_AD_PROTOCOL_VERSION
;
920 } else if (hversion
== DTLS1_VERSION
&& !(options
& SSL_OP_NO_DTLSv1
))
921 s
->method
= DTLSv1_client_method();
923 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
924 s
->version
= hversion
;
925 al
= SSL_AD_PROTOCOL_VERSION
;
928 s
->version
= s
->method
->version
;
931 if ((p
[0] != (s
->version
>> 8)) || (p
[1] != (s
->version
& 0xff))) {
932 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
933 s
->version
= (s
->version
& 0xff00) | p
[1];
934 al
= SSL_AD_PROTOCOL_VERSION
;
939 /* load the server hello data */
940 /* load the server random */
941 memcpy(s
->s3
->server_random
, p
, SSL3_RANDOM_SIZE
);
942 p
+= SSL3_RANDOM_SIZE
;
946 /* get the session-id */
949 if ((j
> sizeof s
->session
->session_id
) || (j
> SSL3_SESSION_ID_SIZE
)) {
950 al
= SSL_AD_ILLEGAL_PARAMETER
;
951 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
954 #ifndef OPENSSL_NO_TLSEXT
956 * check if we want to resume the session based on external pre-shared
959 if (s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
) {
960 SSL_CIPHER
*pref_cipher
= NULL
;
961 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
962 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
963 &s
->session
->master_key_length
,
965 s
->tls_session_secret_cb_arg
)) {
966 s
->session
->cipher
= pref_cipher
?
967 pref_cipher
: ssl_get_cipher_by_char(s
, p
+ j
);
971 #endif /* OPENSSL_NO_TLSEXT */
973 if (!s
->hit
&& j
!= 0 && j
== s
->session
->session_id_length
974 && memcmp(p
, s
->session
->session_id
, j
) == 0) {
975 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
976 || memcmp(s
->session
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
)) {
977 /* actually a client application bug */
978 al
= SSL_AD_ILLEGAL_PARAMETER
;
979 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
980 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
985 /* a miss or crap from the other end */
988 * If we were trying for session-id reuse, make a new SSL_SESSION so
989 * we don't stuff up other people
991 if (s
->session
->session_id_length
> 0) {
992 if (!ssl_get_new_session(s
, 0)) {
996 s
->session
->session_id_length
= j
;
997 memcpy(s
->session
->session_id
, p
, j
); /* j could be 0 */
1000 c
= ssl_get_cipher_by_char(s
, p
);
1002 /* unknown cipher */
1003 al
= SSL_AD_ILLEGAL_PARAMETER
;
1004 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
1007 /* Set version disabled mask now we know version */
1008 if (!SSL_USE_TLS1_2_CIPHERS(s
))
1009 ct
->mask_ssl
= SSL_TLSV1_2
;
1013 * If it is a disabled cipher we didn't send it in client hello, so
1016 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_CHECK
)) {
1017 al
= SSL_AD_ILLEGAL_PARAMETER
;
1018 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
1021 p
+= ssl_put_cipher_by_char(s
, NULL
, NULL
);
1023 sk
= ssl_get_ciphers_by_id(s
);
1024 i
= sk_SSL_CIPHER_find(sk
, c
);
1026 /* we did not say we would use this cipher */
1027 al
= SSL_AD_ILLEGAL_PARAMETER
;
1028 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
1033 * Depending on the session caching (internal/external), the cipher
1034 * and/or cipher_id values may not be set. Make sure that cipher_id is
1035 * set and use it for comparison.
1037 if (s
->session
->cipher
)
1038 s
->session
->cipher_id
= s
->session
->cipher
->id
;
1039 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
1040 al
= SSL_AD_ILLEGAL_PARAMETER
;
1041 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1042 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
1045 s
->s3
->tmp
.new_cipher
= c
;
1047 * Don't digest cached records if no sigalgs: we may need them for client
1050 if (!SSL_USE_SIGALGS(s
) && !ssl3_digest_cached_records(s
))
1052 /* lets get the compression algorithm */
1054 #ifdef OPENSSL_NO_COMP
1056 al
= SSL_AD_ILLEGAL_PARAMETER
;
1057 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1058 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1062 * If compression is disabled we'd better not try to resume a session
1063 * using compression.
1065 if (s
->session
->compress_meth
!= 0) {
1066 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1071 if (s
->hit
&& j
!= s
->session
->compress_meth
) {
1072 al
= SSL_AD_ILLEGAL_PARAMETER
;
1073 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1074 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED
);
1079 else if (!ssl_allow_compression(s
)) {
1080 al
= SSL_AD_ILLEGAL_PARAMETER
;
1081 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_COMPRESSION_DISABLED
);
1084 comp
= ssl3_comp_find(s
->ctx
->comp_methods
, j
);
1086 if ((j
!= 0) && (comp
== NULL
)) {
1087 al
= SSL_AD_ILLEGAL_PARAMETER
;
1088 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1089 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1092 s
->s3
->tmp
.new_compression
= comp
;
1096 #ifndef OPENSSL_NO_TLSEXT
1097 /* TLS extensions */
1098 if (!ssl_parse_serverhello_tlsext(s
, &p
, d
, n
)) {
1099 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_PARSE_TLSEXT
);
1105 /* wrong packet length */
1106 al
= SSL_AD_DECODE_ERROR
;
1107 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_PACKET_LENGTH
);
1113 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1118 int ssl3_get_server_certificate(SSL
*s
)
1120 int al
, i
, ok
, ret
= -1;
1121 unsigned long n
, nc
, llen
, l
;
1123 const unsigned char *q
, *p
;
1125 STACK_OF(X509
) *sk
= NULL
;
1127 EVP_PKEY
*pkey
= NULL
;
1128 int need_cert
= 1; /* VRS: 0=> will allow null cert if auth ==
1131 n
= s
->method
->ssl_get_message(s
,
1134 -1, s
->max_cert_list
, &ok
);
1139 if ((s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) ||
1140 ((s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
) &&
1141 (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
))) {
1142 s
->s3
->tmp
.reuse_message
= 1;
1146 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
1147 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1148 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_BAD_MESSAGE_TYPE
);
1151 p
= d
= (unsigned char *)s
->init_msg
;
1153 if ((sk
= sk_X509_new_null()) == NULL
) {
1154 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1159 if (llen
+ 3 != n
) {
1160 al
= SSL_AD_DECODE_ERROR
;
1161 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
1164 for (nc
= 0; nc
< llen
;) {
1166 if ((l
+ nc
+ 3) > llen
) {
1167 al
= SSL_AD_DECODE_ERROR
;
1168 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1169 SSL_R_CERT_LENGTH_MISMATCH
);
1174 x
= d2i_X509(NULL
, &q
, l
);
1176 al
= SSL_AD_BAD_CERTIFICATE
;
1177 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_ASN1_LIB
);
1181 al
= SSL_AD_DECODE_ERROR
;
1182 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1183 SSL_R_CERT_LENGTH_MISMATCH
);
1186 if (!sk_X509_push(sk
, x
)) {
1187 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1195 i
= ssl_verify_cert_chain(s
, sk
);
1196 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)
1197 #ifndef OPENSSL_NO_KRB5
1198 && !((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
) &&
1199 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
))
1200 #endif /* OPENSSL_NO_KRB5 */
1202 al
= ssl_verify_alarm_type(s
->verify_result
);
1203 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1204 SSL_R_CERTIFICATE_VERIFY_FAILED
);
1207 ERR_clear_error(); /* but we keep s->verify_result */
1209 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, i
);
1210 al
= SSL_AD_HANDSHAKE_FAILURE
;
1214 sc
= ssl_sess_cert_new();
1218 if (s
->session
->sess_cert
)
1219 ssl_sess_cert_free(s
->session
->sess_cert
);
1220 s
->session
->sess_cert
= sc
;
1222 sc
->cert_chain
= sk
;
1224 * Inconsistency alert: cert_chain does include the peer's certificate,
1225 * which we don't include in s3_srvr.c
1227 x
= sk_X509_value(sk
, 0);
1230 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1233 pkey
= X509_get_pubkey(x
);
1235 /* VRS: allow null cert if auth == KRB5 */
1236 need_cert
= ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
) &&
1237 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
))
1241 fprintf(stderr
, "pkey,x = %p, %p\n", pkey
, x
);
1242 fprintf(stderr
, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x
, pkey
));
1243 fprintf(stderr
, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1244 s
->s3
->tmp
.new_cipher
->name
,
1245 s
->s3
->tmp
.new_cipher
->algorithm_mkey
,
1246 s
->s3
->tmp
.new_cipher
->algorithm_auth
, need_cert
);
1247 #endif /* KSSL_DEBUG */
1249 if (need_cert
&& ((pkey
== NULL
) || EVP_PKEY_missing_parameters(pkey
))) {
1252 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1253 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1257 i
= ssl_cert_type(x
, pkey
);
1258 if (need_cert
&& i
< 0) {
1261 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1262 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1267 int exp_idx
= ssl_cipher_get_cert_index(s
->s3
->tmp
.new_cipher
);
1268 if (exp_idx
>= 0 && i
!= exp_idx
) {
1270 al
= SSL_AD_ILLEGAL_PARAMETER
;
1271 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1272 SSL_R_WRONG_CERTIFICATE_TYPE
);
1275 sc
->peer_cert_type
= i
;
1276 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1278 * Why would the following ever happen? We just created sc a couple
1281 if (sc
->peer_pkeys
[i
].x509
!= NULL
)
1282 X509_free(sc
->peer_pkeys
[i
].x509
);
1283 sc
->peer_pkeys
[i
].x509
= x
;
1284 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1286 if (s
->session
->peer
!= NULL
)
1287 X509_free(s
->session
->peer
);
1288 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1289 s
->session
->peer
= x
;
1291 sc
->peer_cert_type
= i
;
1292 sc
->peer_key
= NULL
;
1294 if (s
->session
->peer
!= NULL
)
1295 X509_free(s
->session
->peer
);
1296 s
->session
->peer
= NULL
;
1298 s
->session
->verify_result
= s
->verify_result
;
1304 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1307 EVP_PKEY_free(pkey
);
1309 sk_X509_pop_free(sk
, X509_free
);
1313 int ssl3_get_key_exchange(SSL
*s
)
1315 #ifndef OPENSSL_NO_RSA
1316 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
* 2];
1319 unsigned char *param
, *p
;
1321 long i
, param_len
, n
, alg_k
, alg_a
;
1322 EVP_PKEY
*pkey
= NULL
;
1323 const EVP_MD
*md
= NULL
;
1324 #ifndef OPENSSL_NO_RSA
1327 #ifndef OPENSSL_NO_DH
1330 #ifndef OPENSSL_NO_EC
1331 EC_KEY
*ecdh
= NULL
;
1332 BN_CTX
*bn_ctx
= NULL
;
1333 EC_POINT
*srvr_ecpoint
= NULL
;
1335 int encoded_pt_len
= 0;
1338 EVP_MD_CTX_init(&md_ctx
);
1341 * use same message size as in ssl3_get_certificate_request() as
1342 * ServerKeyExchange message may be skipped
1344 n
= s
->method
->ssl_get_message(s
,
1345 SSL3_ST_CR_KEY_EXCH_A
,
1346 SSL3_ST_CR_KEY_EXCH_B
,
1347 -1, s
->max_cert_list
, &ok
);
1351 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1353 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1355 * Can't skip server key exchange if this is an ephemeral
1358 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)) {
1359 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1360 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1363 #ifndef OPENSSL_NO_PSK
1365 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1366 * identity hint is sent. Set session->sess_cert anyway to avoid
1369 if (alg_k
& SSL_kPSK
) {
1370 s
->session
->sess_cert
= ssl_sess_cert_new();
1371 if (s
->ctx
->psk_identity_hint
)
1372 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1373 s
->ctx
->psk_identity_hint
= NULL
;
1376 s
->s3
->tmp
.reuse_message
= 1;
1380 param
= p
= (unsigned char *)s
->init_msg
;
1381 if (s
->session
->sess_cert
!= NULL
) {
1382 #ifndef OPENSSL_NO_RSA
1383 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
) {
1384 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
1385 s
->session
->sess_cert
->peer_rsa_tmp
= NULL
;
1388 #ifndef OPENSSL_NO_DH
1389 if (s
->session
->sess_cert
->peer_dh_tmp
) {
1390 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
1391 s
->session
->sess_cert
->peer_dh_tmp
= NULL
;
1394 #ifndef OPENSSL_NO_EC
1395 if (s
->session
->sess_cert
->peer_ecdh_tmp
) {
1396 EC_KEY_free(s
->session
->sess_cert
->peer_ecdh_tmp
);
1397 s
->session
->sess_cert
->peer_ecdh_tmp
= NULL
;
1401 s
->session
->sess_cert
= ssl_sess_cert_new();
1404 /* Total length of the parameters including the length prefix */
1407 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1409 al
= SSL_AD_DECODE_ERROR
;
1411 #ifndef OPENSSL_NO_PSK
1412 if (alg_k
& SSL_kPSK
) {
1413 char tmp_id_hint
[PSK_MAX_IDENTITY_LEN
+ 1];
1416 if (param_len
> n
) {
1417 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1423 * Store PSK identity hint for later use, hint is used in
1424 * ssl3_send_client_key_exchange. Assume that the maximum length of
1425 * a PSK identity hint can be as long as the maximum length of a PSK
1428 if (i
> PSK_MAX_IDENTITY_LEN
) {
1429 al
= SSL_AD_HANDSHAKE_FAILURE
;
1430 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_DATA_LENGTH_TOO_LONG
);
1433 if (i
> n
- param_len
) {
1434 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1435 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH
);
1441 * If received PSK identity hint contains NULL characters, the hint
1442 * is truncated from the first NULL. p may not be ending with NULL,
1443 * so create a NULL-terminated string.
1445 memcpy(tmp_id_hint
, p
, i
);
1446 memset(tmp_id_hint
+ i
, 0, PSK_MAX_IDENTITY_LEN
+ 1 - i
);
1447 if (s
->ctx
->psk_identity_hint
!= NULL
)
1448 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1449 s
->ctx
->psk_identity_hint
= BUF_strdup(tmp_id_hint
);
1450 if (s
->ctx
->psk_identity_hint
== NULL
) {
1451 al
= SSL_AD_HANDSHAKE_FAILURE
;
1452 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1459 #endif /* !OPENSSL_NO_PSK */
1460 #ifndef OPENSSL_NO_SRP
1461 if (alg_k
& SSL_kSRP
) {
1463 if (param_len
> n
) {
1464 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1469 if (i
> n
- param_len
) {
1470 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_N_LENGTH
);
1475 if (!(s
->srp_ctx
.N
= BN_bin2bn(p
, i
, NULL
))) {
1476 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1481 if (2 > n
- param_len
) {
1482 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1489 if (i
> n
- param_len
) {
1490 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_G_LENGTH
);
1495 if (!(s
->srp_ctx
.g
= BN_bin2bn(p
, i
, NULL
))) {
1496 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1501 if (1 > n
- param_len
) {
1502 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1507 i
= (unsigned int)(p
[0]);
1510 if (i
> n
- param_len
) {
1511 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_S_LENGTH
);
1516 if (!(s
->srp_ctx
.s
= BN_bin2bn(p
, i
, NULL
))) {
1517 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1522 if (2 > n
- param_len
) {
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1530 if (i
> n
- param_len
) {
1531 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_B_LENGTH
);
1536 if (!(s
->srp_ctx
.B
= BN_bin2bn(p
, i
, NULL
))) {
1537 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1543 if (!srp_verify_server_param(s
, &al
)) {
1544 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_PARAMETERS
);
1548 /* We must check if there is a certificate */
1549 # ifndef OPENSSL_NO_RSA
1550 if (alg_a
& SSL_aRSA
)
1552 X509_get_pubkey(s
->session
->
1553 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1557 # ifndef OPENSSL_NO_DSA
1558 else if (alg_a
& SSL_aDSS
)
1560 X509_get_pubkey(s
->session
->
1561 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1565 #endif /* !OPENSSL_NO_SRP */
1566 #ifndef OPENSSL_NO_RSA
1567 if (alg_k
& SSL_kRSA
) {
1568 /* Temporary RSA keys only allowed in export ciphersuites */
1569 if (!SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)) {
1570 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1574 if ((rsa
= RSA_new()) == NULL
) {
1575 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1580 if (param_len
> n
) {
1581 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1586 if (i
> n
- param_len
) {
1587 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_MODULUS_LENGTH
);
1592 if (!(rsa
->n
= BN_bin2bn(p
, i
, rsa
->n
))) {
1593 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1598 if (2 > n
- param_len
) {
1599 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1606 if (i
> n
- param_len
) {
1607 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_E_LENGTH
);
1612 if (!(rsa
->e
= BN_bin2bn(p
, i
, rsa
->e
))) {
1613 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1619 /* this should be because we are using an export cipher */
1620 if (alg_a
& SSL_aRSA
)
1622 X509_get_pubkey(s
->session
->
1623 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1625 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1628 s
->session
->sess_cert
->peer_rsa_tmp
= rsa
;
1631 #else /* OPENSSL_NO_RSA */
1634 #ifndef OPENSSL_NO_DH
1635 else if (alg_k
& SSL_kDHE
) {
1636 if ((dh
= DH_new()) == NULL
) {
1637 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1642 if (param_len
> n
) {
1643 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1648 if (i
> n
- param_len
) {
1649 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_P_LENGTH
);
1654 if (!(dh
->p
= BN_bin2bn(p
, i
, NULL
))) {
1655 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1660 if (2 > n
- param_len
) {
1661 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1668 if (i
> n
- param_len
) {
1669 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_G_LENGTH
);
1674 if (!(dh
->g
= BN_bin2bn(p
, i
, NULL
))) {
1675 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1680 if (2 > n
- param_len
) {
1681 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1688 if (i
> n
- param_len
) {
1689 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1694 if (!(dh
->pub_key
= BN_bin2bn(p
, i
, NULL
))) {
1695 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1701 if (!ssl_security(s
, SSL_SECOP_TMP_DH
, DH_security_bits(dh
), 0, dh
)) {
1702 al
= SSL_AD_HANDSHAKE_FAILURE
;
1703 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_DH_KEY_TOO_SMALL
);
1706 # ifndef OPENSSL_NO_RSA
1707 if (alg_a
& SSL_aRSA
)
1709 X509_get_pubkey(s
->session
->
1710 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1714 # ifndef OPENSSL_NO_DSA
1715 else if (alg_a
& SSL_aDSS
)
1717 X509_get_pubkey(s
->session
->
1718 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1721 /* else anonymous DH, so no certificate or pkey. */
1723 s
->session
->sess_cert
->peer_dh_tmp
= dh
;
1725 } else if ((alg_k
& SSL_kDHr
) || (alg_k
& SSL_kDHd
)) {
1726 al
= SSL_AD_ILLEGAL_PARAMETER
;
1727 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1728 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1731 #endif /* !OPENSSL_NO_DH */
1733 #ifndef OPENSSL_NO_EC
1734 else if (alg_k
& SSL_kECDHE
) {
1736 const EC_GROUP
*group
;
1738 if ((ecdh
= EC_KEY_new()) == NULL
) {
1739 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1744 * Extract elliptic curve parameters and the server's ephemeral ECDH
1745 * public key. Keep accumulating lengths of various components in
1746 * param_len and make sure it never exceeds n.
1750 * XXX: For now we only support named (not generic) curves and the
1751 * ECParameters in this case is just three bytes. We also need one
1752 * byte for the length of the encoded point
1755 if (param_len
> n
) {
1756 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1760 * Check curve is one of our preferences, if not server has sent an
1761 * invalid curve. ECParameters is 3 bytes.
1763 if (!tls1_check_curve(s
, p
, 3)) {
1764 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_CURVE
);
1768 if ((curve_nid
= tls1_ec_curve_id2nid(*(p
+ 2))) == 0) {
1769 al
= SSL_AD_INTERNAL_ERROR
;
1770 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1771 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1775 ngroup
= EC_GROUP_new_by_curve_name(curve_nid
);
1776 if (ngroup
== NULL
) {
1777 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1780 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1781 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1784 EC_GROUP_free(ngroup
);
1786 group
= EC_KEY_get0_group(ecdh
);
1788 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1789 (EC_GROUP_get_degree(group
) > 163)) {
1790 al
= SSL_AD_EXPORT_RESTRICTION
;
1791 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1792 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1798 /* Next, get the encoded ECPoint */
1799 if (((srvr_ecpoint
= EC_POINT_new(group
)) == NULL
) ||
1800 ((bn_ctx
= BN_CTX_new()) == NULL
)) {
1801 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1805 encoded_pt_len
= *p
; /* length of encoded point */
1808 if ((encoded_pt_len
> n
- param_len
) ||
1809 (EC_POINT_oct2point(group
, srvr_ecpoint
,
1810 p
, encoded_pt_len
, bn_ctx
) == 0)) {
1811 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_ECPOINT
);
1814 param_len
+= encoded_pt_len
;
1817 p
+= encoded_pt_len
;
1820 * The ECC/TLS specification does not mention the use of DSA to sign
1821 * ECParameters in the server key exchange message. We do support RSA
1825 # ifndef OPENSSL_NO_RSA
1826 else if (alg_a
& SSL_aRSA
)
1828 X509_get_pubkey(s
->session
->
1829 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1831 # ifndef OPENSSL_NO_EC
1832 else if (alg_a
& SSL_aECDSA
)
1834 X509_get_pubkey(s
->session
->
1835 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1837 /* else anonymous ECDH, so no certificate or pkey. */
1838 EC_KEY_set_public_key(ecdh
, srvr_ecpoint
);
1839 s
->session
->sess_cert
->peer_ecdh_tmp
= ecdh
;
1841 BN_CTX_free(bn_ctx
);
1843 EC_POINT_free(srvr_ecpoint
);
1844 srvr_ecpoint
= NULL
;
1846 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1847 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1850 #endif /* !OPENSSL_NO_EC */
1852 /* p points to the next byte, there are 'n' bytes left */
1854 /* if it was signed, check the signature */
1856 if (SSL_USE_SIGALGS(s
)) {
1859 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1862 rv
= tls12_check_peer_sigalg(&md
, s
, p
, pkey
);
1869 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
1877 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1882 j
= EVP_PKEY_size(pkey
);
1885 * Check signature length. If n is 0 then signature is empty
1887 if ((i
!= n
) || (n
> j
) || (n
<= 0)) {
1888 /* wrong packet length */
1889 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1892 #ifndef OPENSSL_NO_RSA
1893 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
)) {
1899 for (num
= 2; num
> 0; num
--) {
1900 EVP_MD_CTX_set_flags(&md_ctx
, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
1901 EVP_DigestInit_ex(&md_ctx
, (num
== 2)
1902 ? s
->ctx
->md5
: s
->ctx
->sha1
, NULL
);
1903 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1905 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1907 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1908 EVP_DigestFinal_ex(&md_ctx
, q
, &size
);
1912 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
, pkey
->pkey
.rsa
);
1914 al
= SSL_AD_DECRYPT_ERROR
;
1915 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_DECRYPT
);
1920 al
= SSL_AD_DECRYPT_ERROR
;
1921 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1927 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
1928 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1930 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1932 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1933 if (EVP_VerifyFinal(&md_ctx
, p
, (int)n
, pkey
) <= 0) {
1935 al
= SSL_AD_DECRYPT_ERROR
;
1936 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1941 /* aNULL, aSRP or kPSK do not need public keys */
1942 if (!(alg_a
& (SSL_aNULL
| SSL_aSRP
)) && !(alg_k
& SSL_kPSK
)) {
1943 /* Might be wrong key type, check it */
1944 if (ssl3_check_cert_and_algorithm(s
))
1945 /* Otherwise this shouldn't happen */
1946 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1949 /* still data left over */
1951 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1955 EVP_PKEY_free(pkey
);
1956 EVP_MD_CTX_cleanup(&md_ctx
);
1959 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1961 EVP_PKEY_free(pkey
);
1962 #ifndef OPENSSL_NO_RSA
1966 #ifndef OPENSSL_NO_DH
1970 #ifndef OPENSSL_NO_EC
1971 BN_CTX_free(bn_ctx
);
1972 EC_POINT_free(srvr_ecpoint
);
1976 EVP_MD_CTX_cleanup(&md_ctx
);
1980 int ssl3_get_certificate_request(SSL
*s
)
1983 unsigned long n
, nc
, l
;
1984 unsigned int llen
, ctype_num
, i
;
1985 X509_NAME
*xn
= NULL
;
1986 const unsigned char *p
, *q
;
1988 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1990 n
= s
->method
->ssl_get_message(s
,
1991 SSL3_ST_CR_CERT_REQ_A
,
1992 SSL3_ST_CR_CERT_REQ_B
,
1993 -1, s
->max_cert_list
, &ok
);
1998 s
->s3
->tmp
.cert_req
= 0;
2000 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
2001 s
->s3
->tmp
.reuse_message
= 1;
2003 * If we get here we don't need any cached handshake records as we
2004 * wont be doing client auth.
2006 if (s
->s3
->handshake_buffer
) {
2007 if (!ssl3_digest_cached_records(s
))
2013 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
2014 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2015 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_WRONG_MESSAGE_TYPE
);
2019 /* TLS does not like anon-DH with client cert */
2020 if (s
->version
> SSL3_VERSION
) {
2021 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
2022 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2023 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2024 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
2029 p
= d
= (unsigned char *)s
->init_msg
;
2031 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
2032 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2036 /* get the certificate types */
2038 if (s
->cert
->ctypes
) {
2039 OPENSSL_free(s
->cert
->ctypes
);
2040 s
->cert
->ctypes
= NULL
;
2042 if (ctype_num
> SSL3_CT_NUMBER
) {
2043 /* If we exceed static buffer copy all to cert structure */
2044 s
->cert
->ctypes
= OPENSSL_malloc(ctype_num
);
2045 if (s
->cert
->ctypes
== NULL
) {
2046 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2049 memcpy(s
->cert
->ctypes
, p
, ctype_num
);
2050 s
->cert
->ctype_num
= (size_t)ctype_num
;
2051 ctype_num
= SSL3_CT_NUMBER
;
2053 for (i
= 0; i
< ctype_num
; i
++)
2054 s
->s3
->tmp
.ctype
[i
] = p
[i
];
2056 if (SSL_USE_SIGALGS(s
)) {
2059 * Check we have enough room for signature algorithms and following
2062 if ((unsigned long)(p
- d
+ llen
+ 2) > n
) {
2063 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2064 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2065 SSL_R_DATA_LENGTH_TOO_LONG
);
2068 /* Clear certificate digests and validity flags */
2069 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
2070 s
->cert
->pkeys
[i
].digest
= NULL
;
2071 s
->cert
->pkeys
[i
].valid_flags
= 0;
2073 if ((llen
& 1) || !tls1_save_sigalgs(s
, p
, llen
)) {
2074 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2075 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2076 SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
2079 if (!tls1_process_sigalgs(s
)) {
2080 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2081 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2087 /* get the CA RDNs */
2090 if ((unsigned long)(p
- d
+ llen
) != n
) {
2091 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2092 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_LENGTH_MISMATCH
);
2096 for (nc
= 0; nc
< llen
;) {
2098 if ((l
+ nc
+ 2) > llen
) {
2099 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2100 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_CA_DN_TOO_LONG
);
2106 if ((xn
= d2i_X509_NAME(NULL
, &q
, l
)) == NULL
) {
2107 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2108 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_ASN1_LIB
);
2113 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2114 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2115 SSL_R_CA_DN_LENGTH_MISMATCH
);
2118 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
2119 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2127 /* we should setup a certificate to return.... */
2128 s
->s3
->tmp
.cert_req
= 1;
2129 s
->s3
->tmp
.ctype_num
= ctype_num
;
2130 if (s
->s3
->tmp
.ca_names
!= NULL
)
2131 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
2132 s
->s3
->tmp
.ca_names
= ca_sk
;
2138 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
2142 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
2144 return (X509_NAME_cmp(*a
, *b
));
2147 #ifndef OPENSSL_NO_TLSEXT
2148 int ssl3_get_new_session_ticket(SSL
*s
)
2150 int ok
, al
, ret
= 0, ticklen
;
2152 const unsigned char *p
;
2155 n
= s
->method
->ssl_get_message(s
,
2156 SSL3_ST_CR_SESSION_TICKET_A
,
2157 SSL3_ST_CR_SESSION_TICKET_B
,
2158 SSL3_MT_NEWSESSION_TICKET
, 16384, &ok
);
2164 /* need at least ticket_lifetime_hint + ticket length */
2165 al
= SSL_AD_DECODE_ERROR
;
2166 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
2170 p
= d
= (unsigned char *)s
->init_msg
;
2171 n2l(p
, s
->session
->tlsext_tick_lifetime_hint
);
2173 /* ticket_lifetime_hint + ticket_length + ticket */
2174 if (ticklen
+ 6 != n
) {
2175 al
= SSL_AD_DECODE_ERROR
;
2176 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
2179 if (s
->session
->tlsext_tick
) {
2180 OPENSSL_free(s
->session
->tlsext_tick
);
2181 s
->session
->tlsext_ticklen
= 0;
2183 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
2184 if (!s
->session
->tlsext_tick
) {
2185 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
2188 memcpy(s
->session
->tlsext_tick
, p
, ticklen
);
2189 s
->session
->tlsext_ticklen
= ticklen
;
2191 * There are two ways to detect a resumed ticket session. One is to set
2192 * an appropriate session ID and then the server must return a match in
2193 * ServerHello. This allows the normal client session ID matching to work
2194 * and we know much earlier that the ticket has been accepted. The
2195 * other way is to set zero length session ID when the ticket is
2196 * presented and rely on the handshake to determine session resumption.
2197 * We choose the former approach because this fits in with assumptions
2198 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2199 * SHA256 is disabled) hash of the ticket.
2201 EVP_Digest(p
, ticklen
,
2202 s
->session
->session_id
, &s
->session
->session_id_length
,
2203 EVP_sha256(), NULL
);
2207 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2212 int ssl3_get_cert_status(SSL
*s
)
2215 unsigned long resplen
, n
;
2216 const unsigned char *p
;
2218 n
= s
->method
->ssl_get_message(s
,
2219 SSL3_ST_CR_CERT_STATUS_A
,
2220 SSL3_ST_CR_CERT_STATUS_B
,
2221 SSL3_MT_CERTIFICATE_STATUS
, 16384, &ok
);
2226 /* need at least status type + length */
2227 al
= SSL_AD_DECODE_ERROR
;
2228 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2231 p
= (unsigned char *)s
->init_msg
;
2232 if (*p
++ != TLSEXT_STATUSTYPE_ocsp
) {
2233 al
= SSL_AD_DECODE_ERROR
;
2234 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
2238 if (resplen
+ 4 != n
) {
2239 al
= SSL_AD_DECODE_ERROR
;
2240 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2243 if (s
->tlsext_ocsp_resp
)
2244 OPENSSL_free(s
->tlsext_ocsp_resp
);
2245 s
->tlsext_ocsp_resp
= BUF_memdup(p
, resplen
);
2246 if (!s
->tlsext_ocsp_resp
) {
2247 al
= SSL_AD_INTERNAL_ERROR
;
2248 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
2251 s
->tlsext_ocsp_resplen
= resplen
;
2252 if (s
->ctx
->tlsext_status_cb
) {
2254 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2256 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
2257 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_INVALID_STATUS_RESPONSE
);
2261 al
= SSL_AD_INTERNAL_ERROR
;
2262 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
2268 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2273 int ssl3_get_server_done(SSL
*s
)
2278 /* Second to last param should be very small, like 0 :-) */
2279 n
= s
->method
->ssl_get_message(s
,
2280 SSL3_ST_CR_SRVR_DONE_A
,
2281 SSL3_ST_CR_SRVR_DONE_B
,
2282 SSL3_MT_SERVER_DONE
, 30, &ok
);
2287 /* should contain no data */
2288 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2289 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
, SSL_R_LENGTH_MISMATCH
);
2296 int ssl3_send_client_key_exchange(SSL
*s
)
2300 unsigned long alg_k
;
2301 #ifndef OPENSSL_NO_RSA
2303 EVP_PKEY
*pkey
= NULL
;
2305 #ifndef OPENSSL_NO_KRB5
2307 #endif /* OPENSSL_NO_KRB5 */
2308 #ifndef OPENSSL_NO_EC
2309 EC_KEY
*clnt_ecdh
= NULL
;
2310 const EC_POINT
*srvr_ecpoint
= NULL
;
2311 EVP_PKEY
*srvr_pub_pkey
= NULL
;
2312 unsigned char *encodedPoint
= NULL
;
2313 int encoded_pt_len
= 0;
2314 BN_CTX
*bn_ctx
= NULL
;
2316 unsigned char *pms
= NULL
;
2319 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
) {
2320 p
= ssl_handshake_start(s
);
2322 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2324 /* Fool emacs indentation */
2327 #ifndef OPENSSL_NO_RSA
2328 else if (alg_k
& SSL_kRSA
) {
2330 pmslen
= SSL_MAX_MASTER_KEY_LENGTH
;
2331 pms
= OPENSSL_malloc(pmslen
);
2335 if (s
->session
->sess_cert
== NULL
) {
2337 * We should always have a server certificate with SSL_kRSA.
2339 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2340 ERR_R_INTERNAL_ERROR
);
2344 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
2345 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
2348 X509_get_pubkey(s
->session
->
2349 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].
2351 if ((pkey
== NULL
) || (pkey
->type
!= EVP_PKEY_RSA
)
2352 || (pkey
->pkey
.rsa
== NULL
)) {
2353 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2354 ERR_R_INTERNAL_ERROR
);
2357 rsa
= pkey
->pkey
.rsa
;
2358 EVP_PKEY_free(pkey
);
2361 pms
[0] = s
->client_version
>> 8;
2362 pms
[1] = s
->client_version
& 0xff;
2363 if (RAND_bytes(pms
+ 2, pmslen
- 2) <= 0)
2367 /* Fix buf for TLS and beyond */
2368 if (s
->version
> SSL3_VERSION
)
2370 n
= RSA_public_encrypt(pmslen
, pms
, p
, rsa
, RSA_PKCS1_PADDING
);
2372 if (s
->options
& SSL_OP_PKCS1_CHECK_1
)
2374 if (s
->options
& SSL_OP_PKCS1_CHECK_2
)
2378 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2379 SSL_R_BAD_RSA_ENCRYPT
);
2383 /* Fix buf for TLS and beyond */
2384 if (s
->version
> SSL3_VERSION
) {
2390 #ifndef OPENSSL_NO_KRB5
2391 else if (alg_k
& SSL_kKRB5
) {
2392 krb5_error_code krb5rc
;
2393 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
2394 /* krb5_data krb5_ap_req; */
2395 krb5_data
*enc_ticket
;
2396 krb5_data authenticator
, *authp
= NULL
;
2397 EVP_CIPHER_CTX ciph_ctx
;
2398 const EVP_CIPHER
*enc
= NULL
;
2399 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2400 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
2401 unsigned char epms
[SSL_MAX_MASTER_KEY_LENGTH
+ EVP_MAX_IV_LENGTH
];
2402 int padl
, outl
= sizeof(epms
);
2404 EVP_CIPHER_CTX_init(&ciph_ctx
);
2407 fprintf(stderr
, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2409 # endif /* KSSL_DEBUG */
2412 # ifdef KRB5SENDAUTH
2414 authp
= &authenticator
;
2415 # endif /* KRB5SENDAUTH */
2417 krb5rc
= kssl_cget_tkt(kssl_ctx
, &enc_ticket
, authp
, &kssl_err
);
2418 enc
= kssl_map_enc(kssl_ctx
->enctype
);
2423 fprintf(stderr
, "kssl_cget_tkt rtn %d\n", krb5rc
);
2424 if (krb5rc
&& kssl_err
.text
)
2425 fprintf(stderr
, "kssl_cget_tkt kssl_err=%s\n",
2428 # endif /* KSSL_DEBUG */
2431 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2432 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, kssl_err
.reason
);
2437 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2438 * in place of RFC 2712 KerberosWrapper, as in:
2440 * Send ticket (copy to *p, set n = length)
2441 * n = krb5_ap_req.length;
2442 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2443 * if (krb5_ap_req.data)
2444 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2446 * Now using real RFC 2712 KerberosWrapper
2447 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2448 * Note: 2712 "opaque" types are here replaced
2449 * with a 2-byte length followed by the value.
2451 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2452 * Where "xx xx" = length bytes. Shown here with
2453 * optional authenticator omitted.
2456 /* KerberosWrapper.Ticket */
2457 s2n(enc_ticket
->length
, p
);
2458 memcpy(p
, enc_ticket
->data
, enc_ticket
->length
);
2459 p
+= enc_ticket
->length
;
2460 n
= enc_ticket
->length
+ 2;
2462 /* KerberosWrapper.Authenticator */
2463 if (authp
&& authp
->length
) {
2464 s2n(authp
->length
, p
);
2465 memcpy(p
, authp
->data
, authp
->length
);
2467 n
+= authp
->length
+ 2;
2473 s2n(0, p
); /* null authenticator length */
2477 pmslen
= SSL_MAX_MASTER_KEY_LENGTH
;
2478 pms
= OPENSSL_malloc(pmslen
);
2482 pms
[0] = s
->client_version
>> 8;
2483 pms
[1] = s
->client_version
& 0xff;
2484 if (RAND_bytes(pms
+ 2, pmslen
- 2) <= 0)
2488 * 20010420 VRS. Tried it this way; failed.
2489 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2490 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2491 * kssl_ctx->length);
2492 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2495 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2496 EVP_EncryptInit_ex(&ciph_ctx
, enc
, NULL
, kssl_ctx
->key
, iv
);
2497 EVP_EncryptUpdate(&ciph_ctx
, epms
, &outl
, pms
, pmslen
);
2498 EVP_EncryptFinal_ex(&ciph_ctx
, &(epms
[outl
]), &padl
);
2500 if (outl
> (int)sizeof epms
) {
2501 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2502 ERR_R_INTERNAL_ERROR
);
2505 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2507 /* KerberosWrapper.EncryptedPreMasterSecret */
2509 memcpy(p
, epms
, outl
);
2512 OPENSSL_cleanse(epms
, outl
);
2515 #ifndef OPENSSL_NO_DH
2516 else if (alg_k
& (SSL_kDHE
| SSL_kDHr
| SSL_kDHd
)) {
2517 DH
*dh_srvr
, *dh_clnt
;
2518 SESS_CERT
*scert
= s
->session
->sess_cert
;
2520 if (scert
== NULL
) {
2521 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2522 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2523 SSL_R_UNEXPECTED_MESSAGE
);
2527 if (scert
->peer_dh_tmp
!= NULL
)
2528 dh_srvr
= scert
->peer_dh_tmp
;
2530 /* we get them from the cert */
2531 int idx
= scert
->peer_cert_type
;
2532 EVP_PKEY
*spkey
= NULL
;
2535 spkey
= X509_get_pubkey(scert
->peer_pkeys
[idx
].x509
);
2537 dh_srvr
= EVP_PKEY_get1_DH(spkey
);
2538 EVP_PKEY_free(spkey
);
2540 if (dh_srvr
== NULL
) {
2541 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2542 ERR_R_INTERNAL_ERROR
);
2546 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
2547 /* Use client certificate key */
2548 EVP_PKEY
*clkey
= s
->cert
->key
->privatekey
;
2551 dh_clnt
= EVP_PKEY_get1_DH(clkey
);
2552 if (dh_clnt
== NULL
) {
2553 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2554 ERR_R_INTERNAL_ERROR
);
2558 /* generate a new random key */
2559 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
2560 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2563 if (!DH_generate_key(dh_clnt
)) {
2564 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2570 pmslen
= DH_size(dh_clnt
);
2571 pms
= OPENSSL_malloc(pmslen
);
2576 * use the 'p' output buffer for the DH key, but make sure to
2577 * clear it out afterwards
2580 n
= DH_compute_key(pms
, dh_srvr
->pub_key
, dh_clnt
);
2581 if (scert
->peer_dh_tmp
== NULL
)
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2591 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
)
2594 /* send off the data */
2595 n
= BN_num_bytes(dh_clnt
->pub_key
);
2597 BN_bn2bin(dh_clnt
->pub_key
, p
);
2603 /* perhaps clean things up a bit EAY EAY EAY EAY */
2607 #ifndef OPENSSL_NO_EC
2608 else if (alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
)) {
2609 const EC_GROUP
*srvr_group
= NULL
;
2611 int ecdh_clnt_cert
= 0;
2614 if (s
->session
->sess_cert
== NULL
) {
2615 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2616 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2617 SSL_R_UNEXPECTED_MESSAGE
);
2622 * Did we send out the client's ECDH share for use in premaster
2623 * computation as part of client certificate? If so, set
2624 * ecdh_clnt_cert to 1.
2626 if ((alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) && (s
->cert
!= NULL
)) {
2628 * XXX: For now, we do not support client
2629 * authentication using ECDH certificates.
2630 * To add such support, one needs to add
2631 * code that checks for appropriate
2632 * conditions and sets ecdh_clnt_cert to 1.
2633 * For example, the cert have an ECC
2634 * key on the same curve as the server's
2635 * and the key should be authorized for
2638 * One also needs to add code in ssl3_connect
2639 * to skip sending the certificate verify
2642 * if ((s->cert->key->privatekey != NULL) &&
2643 * (s->cert->key->privatekey->type ==
2644 * EVP_PKEY_EC) && ...)
2645 * ecdh_clnt_cert = 1;
2649 if (s
->session
->sess_cert
->peer_ecdh_tmp
!= NULL
) {
2650 tkey
= s
->session
->sess_cert
->peer_ecdh_tmp
;
2652 /* Get the Server Public Key from Cert */
2654 X509_get_pubkey(s
->session
->
2655 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
2656 if ((srvr_pub_pkey
== NULL
)
2657 || (srvr_pub_pkey
->type
!= EVP_PKEY_EC
)
2658 || (srvr_pub_pkey
->pkey
.ec
== NULL
)) {
2659 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2660 ERR_R_INTERNAL_ERROR
);
2664 tkey
= srvr_pub_pkey
->pkey
.ec
;
2667 srvr_group
= EC_KEY_get0_group(tkey
);
2668 srvr_ecpoint
= EC_KEY_get0_public_key(tkey
);
2670 if ((srvr_group
== NULL
) || (srvr_ecpoint
== NULL
)) {
2671 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2672 ERR_R_INTERNAL_ERROR
);
2676 if ((clnt_ecdh
= EC_KEY_new()) == NULL
) {
2677 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2678 ERR_R_MALLOC_FAILURE
);
2682 if (!EC_KEY_set_group(clnt_ecdh
, srvr_group
)) {
2683 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2686 if (ecdh_clnt_cert
) {
2688 * Reuse key info from our certificate We only need our
2689 * private key to perform the ECDH computation.
2691 const BIGNUM
*priv_key
;
2692 tkey
= s
->cert
->key
->privatekey
->pkey
.ec
;
2693 priv_key
= EC_KEY_get0_private_key(tkey
);
2694 if (priv_key
== NULL
) {
2695 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2696 ERR_R_MALLOC_FAILURE
);
2699 if (!EC_KEY_set_private_key(clnt_ecdh
, priv_key
)) {
2700 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2704 /* Generate a new ECDH key pair */
2705 if (!(EC_KEY_generate_key(clnt_ecdh
))) {
2706 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2713 * use the 'p' output buffer for the ECDH key, but make sure to
2714 * clear it out afterwards
2717 field_size
= EC_GROUP_get_degree(srvr_group
);
2718 if (field_size
<= 0) {
2719 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2722 pmslen
= (field_size
+ 7) / 8;
2723 pms
= OPENSSL_malloc(pmslen
);
2726 n
= ECDH_compute_key(pms
, pmslen
, srvr_ecpoint
, clnt_ecdh
, NULL
);
2727 if (n
<= 0 || pmslen
!= (size_t)n
) {
2728 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2732 if (ecdh_clnt_cert
) {
2733 /* Send empty client key exch message */
2737 * First check the size of encoding and allocate memory
2741 EC_POINT_point2oct(srvr_group
,
2742 EC_KEY_get0_public_key(clnt_ecdh
),
2743 POINT_CONVERSION_UNCOMPRESSED
,
2746 encodedPoint
= (unsigned char *)
2747 OPENSSL_malloc(encoded_pt_len
* sizeof(unsigned char));
2748 bn_ctx
= BN_CTX_new();
2749 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
)) {
2750 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2751 ERR_R_MALLOC_FAILURE
);
2755 /* Encode the public key */
2756 n
= EC_POINT_point2oct(srvr_group
,
2757 EC_KEY_get0_public_key(clnt_ecdh
),
2758 POINT_CONVERSION_UNCOMPRESSED
,
2759 encodedPoint
, encoded_pt_len
, bn_ctx
);
2761 *p
= n
; /* length of encoded point */
2762 /* Encoded point will be copied here */
2764 /* copy the point */
2765 memcpy((unsigned char *)p
, encodedPoint
, n
);
2766 /* increment n to account for length field */
2770 /* Free allocated memory */
2771 BN_CTX_free(bn_ctx
);
2772 if (encodedPoint
!= NULL
)
2773 OPENSSL_free(encodedPoint
);
2774 if (clnt_ecdh
!= NULL
)
2775 EC_KEY_free(clnt_ecdh
);
2776 EVP_PKEY_free(srvr_pub_pkey
);
2778 #endif /* !OPENSSL_NO_EC */
2779 else if (alg_k
& SSL_kGOST
) {
2780 /* GOST key exchange message creation */
2781 EVP_PKEY_CTX
*pkey_ctx
;
2784 unsigned int md_len
;
2786 unsigned char shared_ukm
[32], tmp
[256];
2787 EVP_MD_CTX
*ukm_hash
;
2791 pms
= OPENSSL_malloc(pmslen
);
2796 * Get server sertificate PKEY and create ctx from it
2800 sess_cert
->peer_pkeys
[(keytype
= SSL_PKEY_GOST01
)].x509
;
2804 sess_cert
->peer_pkeys
[(keytype
= SSL_PKEY_GOST94
)].x509
;
2806 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2807 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2811 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
=
2812 X509_get_pubkey(peer_cert
), NULL
);
2814 * If we have send a certificate, and certificate key
2816 * * parameters match those of server certificate, use
2817 * certificate key for key exchange
2820 /* Otherwise, generate ephemeral key pair */
2822 EVP_PKEY_encrypt_init(pkey_ctx
);
2823 /* Generate session key */
2824 RAND_bytes(pms
, pmslen
);
2826 * If we have client certificate, use its secret as peer key
2828 if (s
->s3
->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2829 if (EVP_PKEY_derive_set_peer
2830 (pkey_ctx
, s
->cert
->key
->privatekey
) <= 0) {
2832 * If there was an error - just ignore it. Ephemeral key
2839 * Compute shared IV and store it in algorithm-specific context
2842 ukm_hash
= EVP_MD_CTX_create();
2843 EVP_DigestInit(ukm_hash
,
2844 EVP_get_digestbynid(NID_id_GostR3411_94
));
2845 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
,
2847 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
,
2849 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2850 EVP_MD_CTX_destroy(ukm_hash
);
2851 if (EVP_PKEY_CTX_ctrl
2852 (pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
, EVP_PKEY_CTRL_SET_IV
, 8,
2854 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2858 /* Make GOST keytransport blob message */
2860 * Encapsulate it into sequence
2862 *(p
++) = V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
;
2864 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, pms
, pmslen
) < 0) {
2865 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2869 if (msglen
>= 0x80) {
2871 *(p
++) = msglen
& 0xff;
2874 *(p
++) = msglen
& 0xff;
2877 memcpy(p
, tmp
, msglen
);
2878 /* Check if pubkey from client certificate was used */
2879 if (EVP_PKEY_CTX_ctrl
2880 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0) {
2881 /* Set flag "skip certificate verify" */
2882 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2884 EVP_PKEY_CTX_free(pkey_ctx
);
2885 EVP_PKEY_free(pub_key
);
2888 #ifndef OPENSSL_NO_SRP
2889 else if (alg_k
& SSL_kSRP
) {
2890 if (s
->srp_ctx
.A
!= NULL
) {
2891 /* send off the data */
2892 n
= BN_num_bytes(s
->srp_ctx
.A
);
2894 BN_bn2bin(s
->srp_ctx
.A
, p
);
2897 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2898 ERR_R_INTERNAL_ERROR
);
2901 if (s
->session
->srp_username
!= NULL
)
2902 OPENSSL_free(s
->session
->srp_username
);
2903 s
->session
->srp_username
= BUF_strdup(s
->srp_ctx
.login
);
2904 if (s
->session
->srp_username
== NULL
) {
2905 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2906 ERR_R_MALLOC_FAILURE
);
2911 #ifndef OPENSSL_NO_PSK
2912 else if (alg_k
& SSL_kPSK
) {
2914 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2915 * \0-terminated identity. The last byte is for us for simulating
2918 char identity
[PSK_MAX_IDENTITY_LEN
+ 2];
2919 size_t identity_len
;
2920 unsigned char *t
= NULL
;
2921 unsigned int psk_len
= 0;
2925 if (s
->psk_client_callback
== NULL
) {
2926 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2927 SSL_R_PSK_NO_CLIENT_CB
);
2931 memset(identity
, 0, sizeof(identity
));
2932 /* Allocate maximum size buffer */
2933 pmslen
= PSK_MAX_PSK_LEN
* 2 + 4;
2934 pms
= OPENSSL_malloc(pmslen
);
2938 psk_len
= s
->psk_client_callback(s
, s
->ctx
->psk_identity_hint
,
2939 identity
, sizeof(identity
) - 1,
2941 if (psk_len
> PSK_MAX_PSK_LEN
) {
2942 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2943 ERR_R_INTERNAL_ERROR
);
2945 } else if (psk_len
== 0) {
2946 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2947 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2950 /* Change pmslen to real length */
2951 pmslen
= 2 + psk_len
+ 2 + psk_len
;
2952 identity
[PSK_MAX_IDENTITY_LEN
+ 1] = '\0';
2953 identity_len
= strlen(identity
);
2954 if (identity_len
> PSK_MAX_IDENTITY_LEN
) {
2955 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2956 ERR_R_INTERNAL_ERROR
);
2959 /* create PSK pre_master_secret */
2961 memmove(pms
+ psk_len
+ 4, pms
, psk_len
);
2963 memset(t
, 0, psk_len
);
2967 if (s
->session
->psk_identity_hint
!= NULL
)
2968 OPENSSL_free(s
->session
->psk_identity_hint
);
2969 s
->session
->psk_identity_hint
=
2970 BUF_strdup(s
->ctx
->psk_identity_hint
);
2971 if (s
->ctx
->psk_identity_hint
!= NULL
2972 && s
->session
->psk_identity_hint
== NULL
) {
2973 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2974 ERR_R_MALLOC_FAILURE
);
2978 if (s
->session
->psk_identity
!= NULL
)
2979 OPENSSL_free(s
->session
->psk_identity
);
2980 s
->session
->psk_identity
= BUF_strdup(identity
);
2981 if (s
->session
->psk_identity
== NULL
) {
2982 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2983 ERR_R_MALLOC_FAILURE
);
2987 s2n(identity_len
, p
);
2988 memcpy(p
, identity
, identity_len
);
2989 n
= 2 + identity_len
;
2992 OPENSSL_cleanse(identity
, sizeof(identity
));
2994 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3000 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3001 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3005 if(!ssl_set_handshake_header(s
, SSL3_MT_CLIENT_KEY_EXCHANGE
, n
)) {
3006 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3007 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3011 s
->state
= SSL3_ST_CW_KEY_EXCH_B
;
3014 /* SSL3_ST_CW_KEY_EXCH_B */
3015 n
= ssl_do_write(s
);
3016 #ifndef OPENSSL_NO_SRP
3018 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
3020 * If everything written generate master key: no need to save PMS as
3021 * SRP_generate_client_master_secret generates it internally.
3024 if ((s
->session
->master_key_length
=
3025 SRP_generate_client_master_secret(s
,
3026 s
->session
->master_key
)) <
3028 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3029 ERR_R_INTERNAL_ERROR
);
3035 /* If we haven't written everything save PMS */
3038 s
->cert
->pmslen
= pmslen
;
3040 /* If we don't have a PMS restore */
3043 pmslen
= s
->cert
->pmslen
;
3046 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3047 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
3050 s
->session
->master_key_length
=
3051 s
->method
->ssl3_enc
->generate_master_secret(s
,
3053 session
->master_key
,
3055 OPENSSL_cleanse(pms
, pmslen
);
3057 s
->cert
->pms
= NULL
;
3058 if(s
->session
->master_key_length
< 0) {
3059 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3060 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3066 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3067 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
3070 OPENSSL_cleanse(pms
, pmslen
);
3072 s
->cert
->pms
= NULL
;
3074 #ifndef OPENSSL_NO_EC
3075 BN_CTX_free(bn_ctx
);
3076 if (encodedPoint
!= NULL
)
3077 OPENSSL_free(encodedPoint
);
3078 if (clnt_ecdh
!= NULL
)
3079 EC_KEY_free(clnt_ecdh
);
3080 EVP_PKEY_free(srvr_pub_pkey
);
3085 int ssl3_send_client_verify(SSL
*s
)
3088 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
3090 EVP_PKEY_CTX
*pctx
= NULL
;
3096 EVP_MD_CTX_init(&mctx
);
3098 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
) {
3099 p
= ssl_handshake_start(s
);
3100 pkey
= s
->cert
->key
->privatekey
;
3101 /* Create context from key and test if sha1 is allowed as digest */
3102 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
3103 EVP_PKEY_sign_init(pctx
);
3104 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) > 0) {
3105 if (!SSL_USE_SIGALGS(s
))
3106 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3109 [MD5_DIGEST_LENGTH
]));
3114 * For TLS v1.2 send signature algorithm and signature using agreed
3115 * digest and cached handshake records.
3117 if (SSL_USE_SIGALGS(s
)) {
3120 const EVP_MD
*md
= s
->cert
->key
->digest
;
3121 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
3122 if (hdatalen
<= 0 || !tls12_get_sigandhash(p
, pkey
, md
)) {
3123 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3128 fprintf(stderr
, "Using TLS 1.2 with client alg %s\n",
3131 if (!EVP_SignInit_ex(&mctx
, md
, NULL
)
3132 || !EVP_SignUpdate(&mctx
, hdata
, hdatalen
)
3133 || !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
3134 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_EVP_LIB
);
3140 * For extended master secret we've already digested cached
3143 if (s
->session
->flags
& SSL_SESS_FLAG_EXTMS
) {
3144 BIO_free(s
->s3
->handshake_buffer
);
3145 s
->s3
->handshake_buffer
= NULL
;
3146 s
->s3
->flags
&= ~TLS1_FLAGS_KEEP_HANDSHAKE
;
3147 } else if (!ssl3_digest_cached_records(s
))
3150 #ifndef OPENSSL_NO_RSA
3151 if (pkey
->type
== EVP_PKEY_RSA
) {
3152 s
->method
->ssl3_enc
->cert_verify_mac(s
, NID_md5
, &(data
[0]));
3153 if (RSA_sign(NID_md5_sha1
, data
,
3154 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
,
3155 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
3156 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_RSA_LIB
);
3163 #ifndef OPENSSL_NO_DSA
3164 if (pkey
->type
== EVP_PKEY_DSA
) {
3165 if (!DSA_sign(pkey
->save_type
,
3166 &(data
[MD5_DIGEST_LENGTH
]),
3167 SHA_DIGEST_LENGTH
, &(p
[2]),
3168 (unsigned int *)&j
, pkey
->pkey
.dsa
)) {
3169 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_DSA_LIB
);
3176 #ifndef OPENSSL_NO_EC
3177 if (pkey
->type
== EVP_PKEY_EC
) {
3178 if (!ECDSA_sign(pkey
->save_type
,
3179 &(data
[MD5_DIGEST_LENGTH
]),
3180 SHA_DIGEST_LENGTH
, &(p
[2]),
3181 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
3182 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_ECDSA_LIB
);
3189 if (pkey
->type
== NID_id_GostR3410_94
3190 || pkey
->type
== NID_id_GostR3410_2001
) {
3191 unsigned char signbuf
[64];
3193 size_t sigsize
= 64;
3194 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3195 NID_id_GostR3411_94
, data
);
3196 if (EVP_PKEY_sign(pctx
, signbuf
, &sigsize
, data
, 32) <= 0) {
3197 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3200 for (i
= 63, j
= 0; i
>= 0; j
++, i
--) {
3201 p
[2 + j
] = signbuf
[i
];
3206 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3209 if(!ssl_set_handshake_header(s
, SSL3_MT_CERTIFICATE_VERIFY
, n
)) {
3210 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3213 s
->state
= SSL3_ST_CW_CERT_VRFY_B
;
3215 EVP_MD_CTX_cleanup(&mctx
);
3216 EVP_PKEY_CTX_free(pctx
);
3217 return ssl_do_write(s
);
3219 EVP_MD_CTX_cleanup(&mctx
);
3220 EVP_PKEY_CTX_free(pctx
);
3225 * Check a certificate can be used for client authentication. Currently check
3226 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3227 * certificates can be used and optionally checks suitability for Suite B.
3229 static int ssl3_check_client_certificate(SSL
*s
)
3231 unsigned long alg_k
;
3232 if (!s
->cert
|| !s
->cert
->key
->x509
|| !s
->cert
->key
->privatekey
)
3234 /* If no suitable signature algorithm can't use certificate */
3235 if (SSL_USE_SIGALGS(s
) && !s
->cert
->key
->digest
)
3238 * If strict mode check suitability of chain before using it. This also
3239 * adjusts suite B digest if necessary.
3241 if (s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
&&
3242 !tls1_check_chain(s
, NULL
, NULL
, NULL
, -2))
3244 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3245 /* See if we can use client certificate for fixed DH */
3246 if (alg_k
& (SSL_kDHr
| SSL_kDHd
)) {
3247 SESS_CERT
*scert
= s
->session
->sess_cert
;
3248 int i
= scert
->peer_cert_type
;
3249 EVP_PKEY
*clkey
= NULL
, *spkey
= NULL
;
3250 clkey
= s
->cert
->key
->privatekey
;
3251 /* If client key not DH assume it can be used */
3252 if (EVP_PKEY_id(clkey
) != EVP_PKEY_DH
)
3255 spkey
= X509_get_pubkey(scert
->peer_pkeys
[i
].x509
);
3257 /* Compare server and client parameters */
3258 i
= EVP_PKEY_cmp_parameters(clkey
, spkey
);
3259 EVP_PKEY_free(spkey
);
3263 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
3268 int ssl3_send_client_certificate(SSL
*s
)
3271 EVP_PKEY
*pkey
= NULL
;
3274 if (s
->state
== SSL3_ST_CW_CERT_A
) {
3275 /* Let cert callback update client certificates if required */
3276 if (s
->cert
->cert_cb
) {
3277 i
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
3279 s
->rwstate
= SSL_X509_LOOKUP
;
3283 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3286 s
->rwstate
= SSL_NOTHING
;
3288 if (ssl3_check_client_certificate(s
))
3289 s
->state
= SSL3_ST_CW_CERT_C
;
3291 s
->state
= SSL3_ST_CW_CERT_B
;
3294 /* We need to get a client cert */
3295 if (s
->state
== SSL3_ST_CW_CERT_B
) {
3297 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3298 * return(-1); We then get retied later
3301 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
3303 s
->rwstate
= SSL_X509_LOOKUP
;
3306 s
->rwstate
= SSL_NOTHING
;
3307 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
3308 s
->state
= SSL3_ST_CW_CERT_B
;
3309 if (!SSL_use_certificate(s
, x509
) || !SSL_use_PrivateKey(s
, pkey
))
3311 } else if (i
== 1) {
3313 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,
3314 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
3320 EVP_PKEY_free(pkey
);
3321 if (i
&& !ssl3_check_client_certificate(s
))
3324 if (s
->version
== SSL3_VERSION
) {
3325 s
->s3
->tmp
.cert_req
= 0;
3326 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_CERTIFICATE
);
3329 s
->s3
->tmp
.cert_req
= 2;
3333 /* Ok, we have a cert */
3334 s
->state
= SSL3_ST_CW_CERT_C
;
3337 if (s
->state
== SSL3_ST_CW_CERT_C
) {
3338 s
->state
= SSL3_ST_CW_CERT_D
;
3339 if (!ssl3_output_cert_chain(s
,
3340 (s
->s3
->tmp
.cert_req
==
3341 2) ? NULL
: s
->cert
->key
)) {
3342 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3343 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3347 /* SSL3_ST_CW_CERT_D */
3348 return ssl_do_write(s
);
3351 #define has_bits(i,m) (((i)&(m)) == (m))
3353 int ssl3_check_cert_and_algorithm(SSL
*s
)
3357 EVP_PKEY
*pkey
= NULL
;
3359 #ifndef OPENSSL_NO_RSA
3362 #ifndef OPENSSL_NO_DH
3366 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3367 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
3369 /* we don't have a certificate */
3370 if ((alg_a
& (SSL_aNULL
| SSL_aKRB5
)) || (alg_k
& SSL_kPSK
))
3373 sc
= s
->session
->sess_cert
;
3375 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
3378 #ifndef OPENSSL_NO_RSA
3379 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
3381 #ifndef OPENSSL_NO_DH
3382 dh
= s
->session
->sess_cert
->peer_dh_tmp
;
3385 /* This is the passed certificate */
3387 idx
= sc
->peer_cert_type
;
3388 #ifndef OPENSSL_NO_EC
3389 if (idx
== SSL_PKEY_ECC
) {
3390 if (ssl_check_srvr_ecc_cert_and_alg(sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
3392 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_BAD_ECC_CERT
);
3397 } else if (alg_a
& SSL_aECDSA
) {
3398 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3399 SSL_R_MISSING_ECDSA_SIGNING_CERT
);
3401 } else if (alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) {
3402 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_MISSING_ECDH_CERT
);
3406 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
3407 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
3408 EVP_PKEY_free(pkey
);
3410 /* Check that we have a certificate if we require one */
3411 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
| EVP_PKT_SIGN
)) {
3412 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3413 SSL_R_MISSING_RSA_SIGNING_CERT
);
3416 #ifndef OPENSSL_NO_DSA
3417 else if ((alg_a
& SSL_aDSS
) && !has_bits(i
, EVP_PK_DSA
| EVP_PKT_SIGN
)) {
3418 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3419 SSL_R_MISSING_DSA_SIGNING_CERT
);
3423 #ifndef OPENSSL_NO_RSA
3424 if ((alg_k
& SSL_kRSA
) &&
3425 !(has_bits(i
, EVP_PK_RSA
| EVP_PKT_ENC
) || (rsa
!= NULL
))) {
3426 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3427 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
3431 #ifndef OPENSSL_NO_DH
3432 if ((alg_k
& SSL_kDHE
) &&
3433 !(has_bits(i
, EVP_PK_DH
| EVP_PKT_EXCH
) || (dh
!= NULL
))) {
3434 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_MISSING_DH_KEY
);
3436 } else if ((alg_k
& SSL_kDHr
) && !SSL_USE_SIGALGS(s
) &&
3437 !has_bits(i
, EVP_PK_DH
| EVP_PKS_RSA
)) {
3438 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3439 SSL_R_MISSING_DH_RSA_CERT
);
3442 # ifndef OPENSSL_NO_DSA
3443 else if ((alg_k
& SSL_kDHd
) && !SSL_USE_SIGALGS(s
) &&
3444 !has_bits(i
, EVP_PK_DH
| EVP_PKS_DSA
)) {
3445 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3446 SSL_R_MISSING_DH_DSA_CERT
);
3452 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && !has_bits(i
, EVP_PKT_EXP
)) {
3453 #ifndef OPENSSL_NO_RSA
3454 if (alg_k
& SSL_kRSA
) {
3456 || RSA_size(rsa
) * 8 >
3457 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3458 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3459 SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
3464 #ifndef OPENSSL_NO_DH
3465 if (alg_k
& (SSL_kDHE
| SSL_kDHr
| SSL_kDHd
)) {
3467 || DH_size(dh
) * 8 >
3468 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3469 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3470 SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
3476 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3477 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
3483 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3489 * Check to see if handshake is full or resumed. Usually this is just a case
3490 * of checking to see if a cache hit has occurred. In the case of session
3491 * tickets we have to check the next message to be sure.
3494 #ifndef OPENSSL_NO_TLSEXT
3495 # ifndef OPENSSL_NO_NEXTPROTONEG
3496 int ssl3_send_next_proto(SSL
*s
)
3498 unsigned int len
, padding_len
;
3501 if (s
->state
== SSL3_ST_CW_NEXT_PROTO_A
) {
3502 len
= s
->next_proto_negotiated_len
;
3503 padding_len
= 32 - ((len
+ 2) % 32);
3504 d
= (unsigned char *)s
->init_buf
->data
;
3506 memcpy(d
+ 5, s
->next_proto_negotiated
, len
);
3507 d
[5 + len
] = padding_len
;
3508 memset(d
+ 6 + len
, 0, padding_len
);
3509 *(d
++) = SSL3_MT_NEXT_PROTO
;
3510 l2n3(2 + len
+ padding_len
, d
);
3511 s
->state
= SSL3_ST_CW_NEXT_PROTO_B
;
3512 s
->init_num
= 4 + 2 + len
+ padding_len
;
3516 return ssl3_do_write(s
, SSL3_RT_HANDSHAKE
);
3521 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
3524 #ifndef OPENSSL_NO_ENGINE
3525 if (s
->ctx
->client_cert_engine
) {
3526 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
3527 SSL_get_client_CA_list(s
),
3528 px509
, ppkey
, NULL
, NULL
, NULL
);
3533 if (s
->ctx
->client_cert_cb
)
3534 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);