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-2005 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.
125 #define REUSE_CIPHER_BUG
126 #define NETSCAPE_HANG_BUG
129 #include "ssl_locl.h"
130 #include "kssl_lcl.h"
131 #include "../crypto/constant_time_locl.h"
132 #include <openssl/buffer.h>
133 #include <openssl/rand.h>
134 #include <openssl/objects.h>
135 #include <openssl/evp.h>
136 #include <openssl/hmac.h>
137 #include <openssl/x509.h>
138 #ifndef OPENSSL_NO_DH
139 # include <openssl/dh.h>
141 #include <openssl/bn.h>
142 #ifndef OPENSSL_NO_KRB5
143 # include <openssl/krb5_asn.h>
145 #include <openssl/md5.h>
147 static SSL_METHOD
*ssl3_get_server_method(int ver
);
148 #ifndef OPENSSL_NO_ECDH
149 static int nid2curve_id(int nid
);
152 static SSL_METHOD
*ssl3_get_server_method(int ver
)
154 if (ver
== SSL3_VERSION
)
155 return (SSLv3_server_method());
160 IMPLEMENT_ssl3_meth_func(SSLv3_server_method
,
162 ssl_undefined_function
, ssl3_get_server_method
)
164 int ssl3_accept(SSL
*s
)
167 unsigned long l
, Time
= (unsigned long)time(NULL
);
168 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
170 int new_state
, state
, skip
= 0;
172 RAND_add(&Time
, sizeof(Time
), 0);
176 if (s
->info_callback
!= NULL
)
177 cb
= s
->info_callback
;
178 else if (s
->ctx
->info_callback
!= NULL
)
179 cb
= s
->ctx
->info_callback
;
181 /* init things to blank */
183 if (!SSL_in_init(s
) || SSL_in_before(s
))
186 if (s
->cert
== NULL
) {
187 SSLerr(SSL_F_SSL3_ACCEPT
, SSL_R_NO_CERTIFICATE_SET
);
195 case SSL_ST_RENEGOTIATE
:
197 /* s->state=SSL_ST_ACCEPT; */
201 case SSL_ST_BEFORE
| SSL_ST_ACCEPT
:
202 case SSL_ST_OK
| SSL_ST_ACCEPT
:
206 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
208 if ((s
->version
>> 8) != 3) {
209 SSLerr(SSL_F_SSL3_ACCEPT
, ERR_R_INTERNAL_ERROR
);
212 s
->type
= SSL_ST_ACCEPT
;
214 if (s
->init_buf
== NULL
) {
215 if ((buf
= BUF_MEM_new()) == NULL
) {
219 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
226 if (!ssl3_setup_buffers(s
)) {
232 s
->s3
->flags
&= ~SSL3_FLAGS_SGC_RESTART_DONE
;
234 if (s
->state
!= SSL_ST_RENEGOTIATE
) {
236 * Ok, we now need to push on a buffering BIO so that the
237 * output is sent in a way that TCP likes :-)
239 if (!ssl_init_wbio_buffer(s
, 1)) {
244 ssl3_init_finished_mac(s
);
245 s
->state
= SSL3_ST_SR_CLNT_HELLO_A
;
246 s
->ctx
->stats
.sess_accept
++;
247 } else if (!s
->s3
->send_connection_binding
&&
249 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)) {
251 * Server attempting to renegotiate with client that doesn't
252 * support secure renegotiation.
254 SSLerr(SSL_F_SSL3_ACCEPT
,
255 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
256 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
261 * s->state == SSL_ST_RENEGOTIATE, we will just send a
264 s
->ctx
->stats
.sess_accept_renegotiate
++;
265 s
->state
= SSL3_ST_SW_HELLO_REQ_A
;
269 case SSL3_ST_SW_HELLO_REQ_A
:
270 case SSL3_ST_SW_HELLO_REQ_B
:
273 ret
= ssl3_send_hello_request(s
);
276 s
->s3
->tmp
.next_state
= SSL3_ST_SW_HELLO_REQ_C
;
277 s
->state
= SSL3_ST_SW_FLUSH
;
280 ssl3_init_finished_mac(s
);
283 case SSL3_ST_SW_HELLO_REQ_C
:
284 s
->state
= SSL_ST_OK
;
287 case SSL3_ST_SR_CLNT_HELLO_A
:
288 case SSL3_ST_SR_CLNT_HELLO_B
:
289 case SSL3_ST_SR_CLNT_HELLO_C
:
292 ret
= ssl3_get_client_hello(s
);
296 s
->state
= SSL3_ST_SW_SRVR_HELLO_A
;
300 case SSL3_ST_SW_SRVR_HELLO_A
:
301 case SSL3_ST_SW_SRVR_HELLO_B
:
302 ret
= ssl3_send_server_hello(s
);
305 #ifndef OPENSSL_NO_TLSEXT
307 if (s
->tlsext_ticket_expected
)
308 s
->state
= SSL3_ST_SW_SESSION_TICKET_A
;
310 s
->state
= SSL3_ST_SW_CHANGE_A
;
314 s
->state
= SSL3_ST_SW_CHANGE_A
;
317 s
->state
= SSL3_ST_SW_CERT_A
;
321 case SSL3_ST_SW_CERT_A
:
322 case SSL3_ST_SW_CERT_B
:
323 /* Check if it is anon DH or anon ECDH or KRB5 */
324 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
)
325 && !(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aKRB5
)) {
326 ret
= ssl3_send_server_certificate(s
);
329 #ifndef OPENSSL_NO_TLSEXT
330 if (s
->tlsext_status_expected
)
331 s
->state
= SSL3_ST_SW_CERT_STATUS_A
;
333 s
->state
= SSL3_ST_SW_KEY_EXCH_A
;
336 s
->state
= SSL3_ST_SW_KEY_EXCH_A
;
342 s
->state
= SSL3_ST_SW_KEY_EXCH_A
;
347 case SSL3_ST_SW_KEY_EXCH_A
:
348 case SSL3_ST_SW_KEY_EXCH_B
:
349 l
= s
->s3
->tmp
.new_cipher
->algorithms
;
352 * clear this, it may get reset by send_server_key_exchange
354 s
->s3
->tmp
.use_rsa_tmp
= 0;
357 * only send if a DH key exchange, fortezza or RSA but we have a
358 * sign only certificate For ECC ciphersuites, we send a
359 * serverKeyExchange message only if the cipher suite is either
360 * ECDH-anon or ECDHE. In other cases, the server certificate
361 * contains the server's public key for key exchange.
364 || (l
& (SSL_DH
| SSL_kFZA
))
366 && (s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
367 || (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)
368 && EVP_PKEY_size(s
->cert
->pkeys
369 [SSL_PKEY_RSA_ENC
].privatekey
) *
370 8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)
375 ret
= ssl3_send_server_key_exchange(s
);
381 s
->state
= SSL3_ST_SW_CERT_REQ_A
;
385 case SSL3_ST_SW_CERT_REQ_A
:
386 case SSL3_ST_SW_CERT_REQ_B
:
387 if ( /* don't request cert unless asked for it: */
388 !(s
->verify_mode
& SSL_VERIFY_PEER
) ||
390 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
391 * during re-negotiation:
393 ((s
->session
->peer
!= NULL
) &&
394 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
396 * never request cert in anonymous ciphersuites (see
397 * section "Certificate request" in SSL 3 drafts and in
400 ((s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
) &&
402 * ... except when the application insists on
403 * verification (against the specs, but s3_clnt.c accepts
406 !(s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) ||
408 * never request cert in Kerberos ciphersuites
410 (s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aKRB5
)) {
411 /* no cert request */
413 s
->s3
->tmp
.cert_request
= 0;
414 s
->state
= SSL3_ST_SW_SRVR_DONE_A
;
416 s
->s3
->tmp
.cert_request
= 1;
417 ret
= ssl3_send_certificate_request(s
);
420 #ifndef NETSCAPE_HANG_BUG
421 s
->state
= SSL3_ST_SW_SRVR_DONE_A
;
423 s
->state
= SSL3_ST_SW_FLUSH
;
424 s
->s3
->tmp
.next_state
= SSL3_ST_SR_CERT_A
;
430 case SSL3_ST_SW_SRVR_DONE_A
:
431 case SSL3_ST_SW_SRVR_DONE_B
:
432 ret
= ssl3_send_server_done(s
);
435 s
->s3
->tmp
.next_state
= SSL3_ST_SR_CERT_A
;
436 s
->state
= SSL3_ST_SW_FLUSH
;
440 case SSL3_ST_SW_FLUSH
:
443 * This code originally checked to see if any data was pending
444 * using BIO_CTRL_INFO and then flushed. This caused problems as
445 * documented in PR#1939. The proposed fix doesn't completely
446 * resolve this issue as buggy implementations of
447 * BIO_CTRL_PENDING still exist. So instead we just flush
451 s
->rwstate
= SSL_WRITING
;
452 if (BIO_flush(s
->wbio
) <= 0) {
456 s
->rwstate
= SSL_NOTHING
;
458 s
->state
= s
->s3
->tmp
.next_state
;
461 case SSL3_ST_SR_CERT_A
:
462 case SSL3_ST_SR_CERT_B
:
463 /* Check for second client hello (MS SGC) */
464 ret
= ssl3_check_client_hello(s
);
468 s
->state
= SSL3_ST_SR_CLNT_HELLO_C
;
470 if (s
->s3
->tmp
.cert_request
) {
471 ret
= ssl3_get_client_certificate(s
);
476 s
->state
= SSL3_ST_SR_KEY_EXCH_A
;
480 case SSL3_ST_SR_KEY_EXCH_A
:
481 case SSL3_ST_SR_KEY_EXCH_B
:
482 ret
= ssl3_get_client_key_exchange(s
);
487 * For the ECDH ciphersuites when the client sends its ECDH
488 * pub key in a certificate, the CertificateVerify message is
491 s
->state
= SSL3_ST_SR_FINISHED_A
;
494 s
->state
= SSL3_ST_SR_CERT_VRFY_A
;
498 * We need to get hashes here so if there is a client cert,
501 s
->method
->ssl3_enc
->cert_verify_mac(s
,
502 &(s
->s3
->finish_dgst1
),
506 s
->method
->ssl3_enc
->cert_verify_mac(s
,
507 &(s
->s3
->finish_dgst2
),
510 [MD5_DIGEST_LENGTH
]));
514 case SSL3_ST_SR_CERT_VRFY_A
:
515 case SSL3_ST_SR_CERT_VRFY_B
:
517 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
518 /* we should decide if we expected this one */
519 ret
= ssl3_get_cert_verify(s
);
523 s
->state
= SSL3_ST_SR_FINISHED_A
;
527 case SSL3_ST_SR_FINISHED_A
:
528 case SSL3_ST_SR_FINISHED_B
:
529 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
530 ret
= ssl3_get_finished(s
, SSL3_ST_SR_FINISHED_A
,
531 SSL3_ST_SR_FINISHED_B
);
535 s
->state
= SSL_ST_OK
;
536 #ifndef OPENSSL_NO_TLSEXT
537 else if (s
->tlsext_ticket_expected
)
538 s
->state
= SSL3_ST_SW_SESSION_TICKET_A
;
541 s
->state
= SSL3_ST_SW_CHANGE_A
;
545 #ifndef OPENSSL_NO_TLSEXT
546 case SSL3_ST_SW_SESSION_TICKET_A
:
547 case SSL3_ST_SW_SESSION_TICKET_B
:
548 ret
= ssl3_send_newsession_ticket(s
);
551 s
->state
= SSL3_ST_SW_CHANGE_A
;
555 case SSL3_ST_SW_CERT_STATUS_A
:
556 case SSL3_ST_SW_CERT_STATUS_B
:
557 ret
= ssl3_send_cert_status(s
);
560 s
->state
= SSL3_ST_SW_KEY_EXCH_A
;
566 case SSL3_ST_SW_CHANGE_A
:
567 case SSL3_ST_SW_CHANGE_B
:
569 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
570 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
575 ret
= ssl3_send_change_cipher_spec(s
,
577 SSL3_ST_SW_CHANGE_B
);
581 s
->state
= SSL3_ST_SW_FINISHED_A
;
584 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
585 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
593 case SSL3_ST_SW_FINISHED_A
:
594 case SSL3_ST_SW_FINISHED_B
:
595 ret
= ssl3_send_finished(s
,
596 SSL3_ST_SW_FINISHED_A
,
597 SSL3_ST_SW_FINISHED_B
,
599 ssl3_enc
->server_finished_label
,
601 ssl3_enc
->server_finished_label_len
);
604 s
->state
= SSL3_ST_SW_FLUSH
;
606 s
->s3
->tmp
.next_state
= SSL3_ST_SR_FINISHED_A
;
608 s
->s3
->tmp
.next_state
= SSL_ST_OK
;
613 /* clean a few things up */
614 ssl3_cleanup_key_block(s
);
616 BUF_MEM_free(s
->init_buf
);
619 /* remove buffering on output */
620 ssl_free_wbio_buffer(s
);
624 if (s
->new_session
== 2) { /* skipped if we just sent a
627 * actually not necessarily a 'new' session unless
628 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set
633 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
635 s
->ctx
->stats
.sess_accept_good
++;
637 s
->handshake_func
= ssl3_accept
;
640 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
648 SSLerr(SSL_F_SSL3_ACCEPT
, SSL_R_UNKNOWN_STATE
);
654 if (!s
->s3
->tmp
.reuse_message
&& !skip
) {
656 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
660 if ((cb
!= NULL
) && (s
->state
!= state
)) {
661 new_state
= s
->state
;
663 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
664 s
->state
= new_state
;
670 /* BIO_flush(s->wbio); */
674 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
678 int ssl3_send_hello_request(SSL
*s
)
682 if (s
->state
== SSL3_ST_SW_HELLO_REQ_A
) {
683 p
= (unsigned char *)s
->init_buf
->data
;
684 *(p
++) = SSL3_MT_HELLO_REQUEST
;
689 s
->state
= SSL3_ST_SW_HELLO_REQ_B
;
690 /* number of bytes to write */
695 /* SSL3_ST_SW_HELLO_REQ_B */
696 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
699 int ssl3_check_client_hello(SSL
*s
)
705 * this function is called when we really expect a Certificate message,
706 * so permit appropriate message length
708 n
= s
->method
->ssl_get_message(s
,
711 -1, s
->max_cert_list
, &ok
);
714 s
->s3
->tmp
.reuse_message
= 1;
715 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_HELLO
) {
717 * We only allow the client to restart the handshake once per
720 if (s
->s3
->flags
& SSL3_FLAGS_SGC_RESTART_DONE
) {
721 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO
,
722 SSL_R_MULTIPLE_SGC_RESTARTS
);
726 * Throw away what we have done so far in the current handshake,
727 * which will now be aborted. (A full SSL_clear would be too much.)
729 #ifndef OPENSSL_NO_DH
730 if (s
->s3
->tmp
.dh
!= NULL
) {
731 DH_free(s
->s3
->tmp
.dh
);
732 s
->s3
->tmp
.dh
= NULL
;
735 #ifndef OPENSSL_NO_ECDH
736 if (s
->s3
->tmp
.ecdh
!= NULL
) {
737 EC_KEY_free(s
->s3
->tmp
.ecdh
);
738 s
->s3
->tmp
.ecdh
= NULL
;
741 s
->s3
->flags
|= SSL3_FLAGS_SGC_RESTART_DONE
;
747 int ssl3_get_client_hello(SSL
*s
)
749 int i
, j
, ok
, al
, ret
= -1;
750 unsigned int cookie_len
;
753 unsigned char *p
, *d
, *q
;
755 #ifndef OPENSSL_NO_COMP
756 SSL_COMP
*comp
= NULL
;
758 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
761 * We do this so that we will respond with our native type. If we are
762 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
763 * switching should be handled by a different method. If we are SSLv3, we
764 * will respond with SSLv3, even if prompted with TLSv1.
766 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_A
) {
767 s
->state
= SSL3_ST_SR_CLNT_HELLO_B
;
770 n
= s
->method
->ssl_get_message(s
,
771 SSL3_ST_SR_CLNT_HELLO_B
,
772 SSL3_ST_SR_CLNT_HELLO_C
,
773 SSL3_MT_CLIENT_HELLO
,
774 SSL3_RT_MAX_PLAIN_LENGTH
, &ok
);
779 d
= p
= (unsigned char *)s
->init_msg
;
782 * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
783 * for session id length
785 if (n
< 2 + SSL3_RANDOM_SIZE
+ 1) {
786 al
= SSL_AD_DECODE_ERROR
;
787 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
792 * use version from inside client hello, not from record header (may
793 * differ: see RFC 2246, Appendix E, second paragraph)
795 s
->client_version
= (((int)p
[0]) << 8) | (int)p
[1];
798 if ((s
->version
== DTLS1_VERSION
&& s
->client_version
> s
->version
) ||
799 (s
->version
!= DTLS1_VERSION
&& s
->client_version
< s
->version
)) {
800 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_WRONG_VERSION_NUMBER
);
801 if ((s
->client_version
>> 8) == SSL3_VERSION_MAJOR
) {
803 * similar to ssl3_get_record, send alert using remote version
806 s
->version
= s
->client_version
;
808 al
= SSL_AD_PROTOCOL_VERSION
;
813 * If we require cookies and this ClientHello doesn't contain one, just
814 * return since we do not want to allocate any memory yet. So check
817 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
818 unsigned int session_length
, cookie_length
;
820 session_length
= *(p
+ SSL3_RANDOM_SIZE
);
822 if (p
+ SSL3_RANDOM_SIZE
+ session_length
+ 1 >= d
+ n
) {
823 al
= SSL_AD_DECODE_ERROR
;
824 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
827 cookie_length
= *(p
+ SSL3_RANDOM_SIZE
+ session_length
+ 1);
829 if (cookie_length
== 0)
833 /* load the client random */
834 memcpy(s
->s3
->client_random
, p
, SSL3_RANDOM_SIZE
);
835 p
+= SSL3_RANDOM_SIZE
;
837 /* get the session-id */
841 al
= SSL_AD_DECODE_ERROR
;
842 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
848 * Versions before 0.9.7 always allow session reuse during renegotiation
849 * (i.e. when s->new_session is true), option
850 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. Maybe
851 * this optional behaviour should always have been the default, but we
852 * cannot safely change the default behaviour (or new applications might
853 * be written that become totally unsecure when compiled with an earlier
857 && (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
858 if (!ssl_get_new_session(s
, 1))
861 i
= ssl_get_prev_session(s
, p
, j
, d
+ n
);
862 if (i
== 1) { /* previous session */
868 if (!ssl_get_new_session(s
, 1))
875 if (s
->version
== DTLS1_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
878 al
= SSL_AD_DECODE_ERROR
;
879 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
884 if (p
+ cookie_len
> d
+ n
) {
885 al
= SSL_AD_DECODE_ERROR
;
886 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
891 * The ClientHello may contain a cookie even if the
892 * HelloVerify message has not been sent--make sure that it
893 * does not cause an overflow.
895 if (cookie_len
> sizeof(s
->d1
->rcvd_cookie
)) {
897 al
= SSL_AD_DECODE_ERROR
;
898 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_COOKIE_MISMATCH
);
902 /* verify the cookie if appropriate option is set. */
903 if ((SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) && cookie_len
> 0) {
904 memcpy(s
->d1
->rcvd_cookie
, p
, cookie_len
);
906 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
907 if (s
->ctx
->app_verify_cookie_cb(s
, s
->d1
->rcvd_cookie
,
909 al
= SSL_AD_HANDSHAKE_FAILURE
;
910 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,
911 SSL_R_COOKIE_MISMATCH
);
914 /* else cookie verification succeeded */
916 /* default verification */
917 else if (memcmp(s
->d1
->rcvd_cookie
, s
->d1
->cookie
,
918 s
->d1
->cookie_len
) != 0) {
919 al
= SSL_AD_HANDSHAKE_FAILURE
;
920 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_COOKIE_MISMATCH
);
931 al
= SSL_AD_DECODE_ERROR
;
932 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
936 if ((i
== 0) && (j
!= 0)) {
937 /* we need a cipher if we are not resuming a session */
938 al
= SSL_AD_ILLEGAL_PARAMETER
;
939 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_NO_CIPHERS_SPECIFIED
);
943 /* i bytes of cipher data + 1 byte for compression length later */
944 if ((p
+ i
+ 1) > (d
+ n
)) {
945 /* not enough data */
946 al
= SSL_AD_DECODE_ERROR
;
947 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
950 if ((i
> 0) && (ssl_bytes_to_cipher_list(s
, p
, i
, &(ciphers
))
956 /* If it is a hit, check that the cipher is in the list */
957 if ((s
->hit
) && (i
> 0)) {
959 id
= s
->session
->cipher
->id
;
962 printf("client sent %d ciphers\n", sk_num(ciphers
));
964 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
965 c
= sk_SSL_CIPHER_value(ciphers
, i
);
967 printf("client [%2d of %2d]:%s\n",
968 i
, sk_num(ciphers
), SSL_CIPHER_get_name(c
));
976 * Disabled because it can be used in a ciphersuite downgrade attack:
980 if (j
== 0 && (s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
)
981 && (sk_SSL_CIPHER_num(ciphers
) == 1)) {
983 * Special case as client bug workaround: the previously used
984 * cipher may not be in the current list, the client instead
985 * might be trying to continue using a cipher that before wasn't
986 * chosen due to server preferences. We'll have to reject the
987 * connection if the cipher is not enabled, though.
989 c
= sk_SSL_CIPHER_value(ciphers
, 0);
990 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s
), c
) >= 0) {
991 s
->session
->cipher
= c
;
998 * we need to have the cipher in the cipher list if we are asked
1001 al
= SSL_AD_ILLEGAL_PARAMETER
;
1002 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,
1003 SSL_R_REQUIRED_CIPHER_MISSING
);
1010 if ((p
+ i
) > (d
+ n
)) {
1011 /* not enough data */
1012 al
= SSL_AD_DECODE_ERROR
;
1013 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1017 for (j
= 0; j
< i
; j
++) {
1025 al
= SSL_AD_DECODE_ERROR
;
1026 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1029 #ifndef OPENSSL_NO_TLSEXT
1030 /* TLS extensions */
1031 if (s
->version
>= SSL3_VERSION
) {
1032 if (!ssl_parse_clienthello_tlsext(s
, &p
, d
, n
, &al
)) {
1033 /* 'al' set by ssl_parse_clienthello_tlsext */
1034 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_PARSE_TLSEXT
);
1038 if (ssl_check_clienthello_tlsext_early(s
) <= 0) {
1039 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1044 * Worst case, we will use the NULL compression, but if we have other
1045 * options, we will now look for them. We have i-1 compression
1046 * algorithms from the client, starting at q.
1048 s
->s3
->tmp
.new_compression
= NULL
;
1049 #ifndef OPENSSL_NO_COMP
1050 if (s
->ctx
->comp_methods
!= NULL
) {
1051 /* See if we have a match */
1052 int m
, nn
, o
, v
, done
= 0;
1054 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1055 for (m
= 0; m
< nn
; m
++) {
1056 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1058 for (o
= 0; o
< i
; o
++) {
1068 s
->s3
->tmp
.new_compression
= comp
;
1074 /* TLS does not mind if there is extra stuff */
1077 * SSL 3.0 does not mind either, so we should disable this test (was
1078 * enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b, in earlier
1079 * SSLeay/OpenSSL releases this test existed but was buggy)
1081 if (s
->version
== SSL3_VERSION
) {
1084 * wrong number of bytes, there could be more to follow
1086 al
= SSL_AD_DECODE_ERROR
;
1087 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1094 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1098 #ifdef OPENSSL_NO_COMP
1099 s
->session
->compress_meth
= 0;
1101 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
1103 if (s
->session
->ciphers
!= NULL
)
1104 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1105 s
->session
->ciphers
= ciphers
;
1106 if (ciphers
== NULL
) {
1107 al
= SSL_AD_ILLEGAL_PARAMETER
;
1108 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_NO_CIPHERS_PASSED
);
1112 c
= ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
1115 al
= SSL_AD_HANDSHAKE_FAILURE
;
1116 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_NO_SHARED_CIPHER
);
1119 s
->s3
->tmp
.new_cipher
= c
;
1121 /* Session-id reuse */
1122 #ifdef REUSE_CIPHER_BUG
1123 STACK_OF(SSL_CIPHER
) *sk
;
1124 SSL_CIPHER
*nc
= NULL
;
1125 SSL_CIPHER
*ec
= NULL
;
1127 if (s
->options
& SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
) {
1128 sk
= s
->session
->ciphers
;
1129 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); i
++) {
1130 c
= sk_SSL_CIPHER_value(sk
, i
);
1131 if (c
->algorithms
& SSL_eNULL
)
1133 if (SSL_C_IS_EXPORT(c
))
1137 s
->s3
->tmp
.new_cipher
= nc
;
1138 else if (ec
!= NULL
)
1139 s
->s3
->tmp
.new_cipher
= ec
;
1141 s
->s3
->tmp
.new_cipher
= s
->session
->cipher
;
1144 s
->s3
->tmp
.new_cipher
= s
->session
->cipher
;
1148 * we now have the following setup.
1150 * cipher_list - our prefered list of ciphers
1151 * ciphers - the clients prefered list of ciphers
1152 * compression - basically ignored right now
1153 * ssl version is set - sslv3
1154 * s->session - The ssl session has been setup.
1155 * s->hit - session reuse flag
1156 * s->tmp.new_cipher - the new cipher to use.
1159 #ifndef OPENSSL_NO_TLSEXT
1160 /* Handles TLS extensions that we couldn't check earlier */
1161 if (s
->version
>= SSL3_VERSION
) {
1162 if (ssl_check_clienthello_tlsext_late(s
) <= 0) {
1163 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1173 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1176 if (ciphers
!= NULL
)
1177 sk_SSL_CIPHER_free(ciphers
);
1181 int ssl3_send_server_hello(SSL
*s
)
1184 unsigned char *p
, *d
;
1186 unsigned long l
, Time
;
1188 if (s
->state
== SSL3_ST_SW_SRVR_HELLO_A
) {
1189 buf
= (unsigned char *)s
->init_buf
->data
;
1190 p
= s
->s3
->server_random
;
1191 Time
= (unsigned long)time(NULL
); /* Time */
1193 if (RAND_pseudo_bytes(p
, SSL3_RANDOM_SIZE
- 4) <= 0)
1195 /* Do the message type and length last */
1198 *(p
++) = s
->version
>> 8;
1199 *(p
++) = s
->version
& 0xff;
1202 memcpy(p
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
1203 p
+= SSL3_RANDOM_SIZE
;
1206 * now in theory we have 3 options to sending back the session id.
1207 * If it is a re-use, we send back the old session-id, if it is a new
1208 * session, we send back the new session-id or we send back a 0
1209 * length session-id if we want it to be single use. Currently I will
1210 * not implement the '0' length session-id 12-Jan-98 - I'll now
1211 * support the '0' length stuff. We also have an additional case
1212 * where stateless session resumption is successful: we always send
1213 * back the old session id. In this case s->hit is non zero: this can
1214 * only happen if stateless session resumption is succesful if session
1215 * caching is disabled so existing functionality is unaffected.
1217 if (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
1219 s
->session
->session_id_length
= 0;
1221 sl
= s
->session
->session_id_length
;
1222 if (sl
> (int)sizeof(s
->session
->session_id
)) {
1223 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1227 memcpy(p
, s
->session
->session_id
, sl
);
1230 /* put the cipher */
1231 i
= ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
, p
);
1234 /* put the compression method */
1235 #ifdef OPENSSL_NO_COMP
1238 if (s
->s3
->tmp
.new_compression
== NULL
)
1241 *(p
++) = s
->s3
->tmp
.new_compression
->id
;
1243 #ifndef OPENSSL_NO_TLSEXT
1245 ssl_add_serverhello_tlsext(s
, p
,
1246 buf
+ SSL3_RT_MAX_PLAIN_LENGTH
)) ==
1248 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1255 *(d
++) = SSL3_MT_SERVER_HELLO
;
1258 s
->state
= SSL3_ST_SW_SRVR_HELLO_B
;
1259 /* number of bytes to write */
1260 s
->init_num
= p
- buf
;
1264 /* SSL3_ST_SW_SRVR_HELLO_B */
1265 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
1268 int ssl3_send_server_done(SSL
*s
)
1272 if (s
->state
== SSL3_ST_SW_SRVR_DONE_A
) {
1273 p
= (unsigned char *)s
->init_buf
->data
;
1276 *(p
++) = SSL3_MT_SERVER_DONE
;
1281 s
->state
= SSL3_ST_SW_SRVR_DONE_B
;
1282 /* number of bytes to write */
1287 /* SSL3_ST_SW_SRVR_DONE_B */
1288 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
1291 int ssl3_send_server_key_exchange(SSL
*s
)
1293 #ifndef OPENSSL_NO_RSA
1297 unsigned char md_buf
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
1300 #ifndef OPENSSL_NO_DH
1301 DH
*dh
= NULL
, *dhp
;
1303 #ifndef OPENSSL_NO_ECDH
1304 EC_KEY
*ecdh
= NULL
, *ecdhp
;
1305 unsigned char *encodedPoint
= NULL
;
1308 BN_CTX
*bn_ctx
= NULL
;
1311 unsigned char *p
, *d
;
1321 EVP_MD_CTX_init(&md_ctx
);
1322 if (s
->state
== SSL3_ST_SW_KEY_EXCH_A
) {
1323 type
= s
->s3
->tmp
.new_cipher
->algorithms
& SSL_MKEY_MASK
;
1328 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
1330 #ifndef OPENSSL_NO_RSA
1331 if (type
& SSL_kRSA
) {
1332 rsa
= cert
->rsa_tmp
;
1333 if ((rsa
== NULL
) && (s
->cert
->rsa_tmp_cb
!= NULL
)) {
1334 rsa
= s
->cert
->rsa_tmp_cb(s
,
1335 SSL_C_IS_EXPORT(s
->s3
->
1337 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->
1340 al
= SSL_AD_HANDSHAKE_FAILURE
;
1341 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1342 SSL_R_ERROR_GENERATING_TMP_RSA_KEY
);
1346 cert
->rsa_tmp
= rsa
;
1349 al
= SSL_AD_HANDSHAKE_FAILURE
;
1350 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1351 SSL_R_MISSING_TMP_RSA_KEY
);
1356 s
->s3
->tmp
.use_rsa_tmp
= 1;
1359 #ifndef OPENSSL_NO_DH
1360 if (type
& SSL_kEDH
) {
1362 if ((dhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
))
1363 dhp
= s
->cert
->dh_tmp_cb(s
,
1364 SSL_C_IS_EXPORT(s
->s3
->
1366 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->
1369 al
= SSL_AD_HANDSHAKE_FAILURE
;
1370 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1371 SSL_R_MISSING_TMP_DH_KEY
);
1375 if (s
->s3
->tmp
.dh
!= NULL
) {
1376 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1377 ERR_R_INTERNAL_ERROR
);
1381 if ((dh
= DHparams_dup(dhp
)) == NULL
) {
1382 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1387 if ((dhp
->pub_key
== NULL
||
1388 dhp
->priv_key
== NULL
||
1389 (s
->options
& SSL_OP_SINGLE_DH_USE
))) {
1390 if (!DH_generate_key(dh
)) {
1391 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1395 dh
->pub_key
= BN_dup(dhp
->pub_key
);
1396 dh
->priv_key
= BN_dup(dhp
->priv_key
);
1397 if ((dh
->pub_key
== NULL
) || (dh
->priv_key
== NULL
)) {
1398 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1407 #ifndef OPENSSL_NO_ECDH
1408 if (type
& SSL_kECDHE
) {
1409 const EC_GROUP
*group
;
1411 ecdhp
= cert
->ecdh_tmp
;
1412 if ((ecdhp
== NULL
) && (s
->cert
->ecdh_tmp_cb
!= NULL
)) {
1413 ecdhp
= s
->cert
->ecdh_tmp_cb(s
,
1414 SSL_C_IS_EXPORT(s
->s3
->
1416 SSL_C_EXPORT_PKEYLENGTH(s
->
1417 s3
->tmp
.new_cipher
));
1419 if (ecdhp
== NULL
) {
1420 al
= SSL_AD_HANDSHAKE_FAILURE
;
1421 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1422 SSL_R_MISSING_TMP_ECDH_KEY
);
1426 if (s
->s3
->tmp
.ecdh
!= NULL
) {
1427 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1428 ERR_R_INTERNAL_ERROR
);
1432 /* Duplicate the ECDH structure. */
1433 if (ecdhp
== NULL
) {
1434 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
1437 if ((ecdh
= EC_KEY_dup(ecdhp
)) == NULL
) {
1438 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
1442 s
->s3
->tmp
.ecdh
= ecdh
;
1443 if ((EC_KEY_get0_public_key(ecdh
) == NULL
) ||
1444 (EC_KEY_get0_private_key(ecdh
) == NULL
) ||
1445 (s
->options
& SSL_OP_SINGLE_ECDH_USE
)) {
1446 if (!EC_KEY_generate_key(ecdh
)) {
1447 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1453 if (((group
= EC_KEY_get0_group(ecdh
)) == NULL
) ||
1454 (EC_KEY_get0_public_key(ecdh
) == NULL
) ||
1455 (EC_KEY_get0_private_key(ecdh
) == NULL
)) {
1456 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
1460 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1461 (EC_GROUP_get_degree(group
) > 163)) {
1462 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1463 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1468 * XXX: For now, we only support ephemeral ECDH keys over named
1469 * (not generic) curves. For supported named curves, curve_id is
1472 if ((curve_id
= nid2curve_id(EC_GROUP_get_curve_name(group
)))
1474 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1475 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1480 * Encode the public key. First check the size of encoding and
1481 * allocate memory accordingly.
1483 encodedlen
= EC_POINT_point2oct(group
,
1484 EC_KEY_get0_public_key(ecdh
),
1485 POINT_CONVERSION_UNCOMPRESSED
,
1488 encodedPoint
= (unsigned char *)
1489 OPENSSL_malloc(encodedlen
* sizeof(unsigned char));
1490 bn_ctx
= BN_CTX_new();
1491 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
)) {
1492 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1493 ERR_R_MALLOC_FAILURE
);
1497 encodedlen
= EC_POINT_point2oct(group
,
1498 EC_KEY_get0_public_key(ecdh
),
1499 POINT_CONVERSION_UNCOMPRESSED
,
1500 encodedPoint
, encodedlen
, bn_ctx
);
1502 if (encodedlen
== 0) {
1503 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
1507 BN_CTX_free(bn_ctx
);
1511 * XXX: For now, we only support named (not generic) curves in
1512 * ECDH ephemeral key exchanges. In this situation, we need four
1513 * additional bytes to encode the entire ServerECDHParams
1519 * We'll generate the serverKeyExchange message explicitly so we
1520 * can set these to NULLs
1527 #endif /* !OPENSSL_NO_ECDH */
1529 al
= SSL_AD_HANDSHAKE_FAILURE
;
1530 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1531 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1534 for (i
= 0; r
[i
] != NULL
; i
++) {
1535 nr
[i
] = BN_num_bytes(r
[i
]);
1539 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
)) {
1540 if ((pkey
= ssl_get_sign_pkey(s
, s
->s3
->tmp
.new_cipher
))
1542 al
= SSL_AD_DECODE_ERROR
;
1545 kn
= EVP_PKEY_size(pkey
);
1551 if (!BUF_MEM_grow_clean(buf
, n
+ 4 + kn
)) {
1552 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_LIB_BUF
);
1555 d
= (unsigned char *)s
->init_buf
->data
;
1558 for (i
= 0; r
[i
] != NULL
; i
++) {
1564 #ifndef OPENSSL_NO_ECDH
1565 if (type
& SSL_kECDHE
) {
1567 * XXX: For now, we only support named (not generic) curves. In
1568 * this situation, the serverKeyExchange message has: [1 byte
1569 * CurveType], [2 byte CurveName] [1 byte length of encoded
1570 * point], followed by the actual encoded point itself
1572 *p
= NAMED_CURVE_TYPE
;
1580 memcpy((unsigned char *)p
,
1581 (unsigned char *)encodedPoint
, encodedlen
);
1582 OPENSSL_free(encodedPoint
);
1583 encodedPoint
= NULL
;
1591 * n is the length of the params, they start at &(d[4]) and p
1592 * points to the space at the end.
1594 #ifndef OPENSSL_NO_RSA
1595 if (pkey
->type
== EVP_PKEY_RSA
) {
1598 for (num
= 2; num
> 0; num
--) {
1599 EVP_MD_CTX_set_flags(&md_ctx
,
1600 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
1601 EVP_DigestInit_ex(&md_ctx
, (num
== 2)
1602 ? s
->ctx
->md5
: s
->ctx
->sha1
, NULL
);
1603 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1605 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1607 EVP_DigestUpdate(&md_ctx
, &(d
[4]), n
);
1608 EVP_DigestFinal_ex(&md_ctx
, q
, (unsigned int *)&i
);
1612 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
1613 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
1614 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_LIB_RSA
);
1621 #if !defined(OPENSSL_NO_DSA)
1622 if (pkey
->type
== EVP_PKEY_DSA
) {
1624 EVP_SignInit_ex(&md_ctx
, EVP_dss1(), NULL
);
1625 EVP_SignUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1627 EVP_SignUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1629 EVP_SignUpdate(&md_ctx
, &(d
[4]), n
);
1630 if (!EVP_SignFinal(&md_ctx
, &(p
[2]),
1631 (unsigned int *)&i
, pkey
)) {
1632 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_LIB_DSA
);
1639 #if !defined(OPENSSL_NO_ECDSA)
1640 if (pkey
->type
== EVP_PKEY_EC
) {
1641 /* let's do ECDSA */
1642 EVP_SignInit_ex(&md_ctx
, EVP_ecdsa(), NULL
);
1643 EVP_SignUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1645 EVP_SignUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1647 EVP_SignUpdate(&md_ctx
, &(d
[4]), n
);
1648 if (!EVP_SignFinal(&md_ctx
, &(p
[2]),
1649 (unsigned int *)&i
, pkey
)) {
1650 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1659 /* Is this error check actually needed? */
1660 al
= SSL_AD_HANDSHAKE_FAILURE
;
1661 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1662 SSL_R_UNKNOWN_PKEY_TYPE
);
1667 *(d
++) = SSL3_MT_SERVER_KEY_EXCHANGE
;
1671 * we should now have things packed up, so lets send it off
1673 s
->init_num
= n
+ 4;
1677 s
->state
= SSL3_ST_SW_KEY_EXCH_B
;
1678 EVP_MD_CTX_cleanup(&md_ctx
);
1679 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
1681 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1683 #ifndef OPENSSL_NO_ECDH
1684 if (encodedPoint
!= NULL
)
1685 OPENSSL_free(encodedPoint
);
1686 BN_CTX_free(bn_ctx
);
1688 EVP_MD_CTX_cleanup(&md_ctx
);
1692 int ssl3_send_certificate_request(SSL
*s
)
1694 unsigned char *p
, *d
;
1695 int i
, j
, nl
, off
, n
;
1696 STACK_OF(X509_NAME
) *sk
= NULL
;
1700 if (s
->state
== SSL3_ST_SW_CERT_REQ_A
) {
1703 d
= p
= (unsigned char *)&(buf
->data
[4]);
1705 /* get the list of acceptable cert types */
1707 n
= ssl3_get_req_cert_type(s
, p
);
1716 sk
= SSL_get_client_CA_list(s
);
1719 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
1720 name
= sk_X509_NAME_value(sk
, i
);
1721 j
= i2d_X509_NAME(name
, NULL
);
1722 if (!BUF_MEM_grow_clean(buf
, 4 + n
+ j
+ 2)) {
1723 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,
1727 p
= (unsigned char *)&(buf
->data
[4 + n
]);
1728 if (!(s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
)) {
1730 i2d_X509_NAME(name
, &p
);
1735 i2d_X509_NAME(name
, &p
);
1744 /* else no CA names */
1745 p
= (unsigned char *)&(buf
->data
[4 + off
]);
1748 d
= (unsigned char *)buf
->data
;
1749 *(d
++) = SSL3_MT_CERTIFICATE_REQUEST
;
1753 * we should now have things packed up, so lets send it off
1756 s
->init_num
= n
+ 4;
1758 #ifdef NETSCAPE_HANG_BUG
1759 if (!BUF_MEM_grow_clean(buf
, s
->init_num
+ 4)) {
1760 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
, ERR_R_BUF_LIB
);
1763 p
= (unsigned char *)s
->init_buf
->data
+ s
->init_num
;
1766 *(p
++) = SSL3_MT_SERVER_DONE
;
1773 s
->state
= SSL3_ST_SW_CERT_REQ_B
;
1776 /* SSL3_ST_SW_CERT_REQ_B */
1777 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
1782 int ssl3_get_client_key_exchange(SSL
*s
)
1788 #ifndef OPENSSL_NO_RSA
1790 EVP_PKEY
*pkey
= NULL
;
1792 #ifndef OPENSSL_NO_DH
1796 #ifndef OPENSSL_NO_KRB5
1798 #endif /* OPENSSL_NO_KRB5 */
1800 #ifndef OPENSSL_NO_ECDH
1801 EC_KEY
*srvr_ecdh
= NULL
;
1802 EVP_PKEY
*clnt_pub_pkey
= NULL
;
1803 EC_POINT
*clnt_ecpoint
= NULL
;
1804 BN_CTX
*bn_ctx
= NULL
;
1807 n
= s
->method
->ssl_get_message(s
,
1808 SSL3_ST_SR_KEY_EXCH_A
,
1809 SSL3_ST_SR_KEY_EXCH_B
,
1810 SSL3_MT_CLIENT_KEY_EXCHANGE
, 2048, &ok
);
1814 p
= (unsigned char *)s
->init_msg
;
1816 l
= s
->s3
->tmp
.new_cipher
->algorithms
;
1818 #ifndef OPENSSL_NO_RSA
1820 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
1822 unsigned char decrypt_good
, version_good
;
1824 /* FIX THIS UP EAY EAY EAY EAY */
1825 if (s
->s3
->tmp
.use_rsa_tmp
) {
1826 if ((s
->cert
!= NULL
) && (s
->cert
->rsa_tmp
!= NULL
))
1827 rsa
= s
->cert
->rsa_tmp
;
1829 * Don't do a callback because rsa_tmp should be sent already
1832 al
= SSL_AD_HANDSHAKE_FAILURE
;
1833 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1834 SSL_R_MISSING_TMP_RSA_PKEY
);
1839 pkey
= s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1840 if ((pkey
== NULL
) ||
1841 (pkey
->type
!= EVP_PKEY_RSA
) || (pkey
->pkey
.rsa
== NULL
)) {
1842 al
= SSL_AD_HANDSHAKE_FAILURE
;
1843 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1844 SSL_R_MISSING_RSA_CERTIFICATE
);
1847 rsa
= pkey
->pkey
.rsa
;
1850 /* TLS and [incidentally] DTLS, including pre-0.9.8f */
1851 if (s
->version
> SSL3_VERSION
&& s
->client_version
!= DTLS1_BAD_VER
) {
1854 if (!(s
->options
& SSL_OP_TLS_D5_BUG
)) {
1855 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1856 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1865 * We must not leak whether a decryption failure occurs because of
1866 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1867 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1868 * generates a random premaster secret for the case that the decrypt
1869 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
1873 * should be RAND_bytes, but we cannot work around a failure.
1875 if (RAND_pseudo_bytes(rand_premaster_secret
,
1876 sizeof(rand_premaster_secret
)) <= 0)
1879 RSA_private_decrypt((int)n
, p
, p
, rsa
, RSA_PKCS1_PADDING
);
1883 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
1884 * be 0xff if so and zero otherwise.
1887 constant_time_eq_int_8(decrypt_len
, SSL_MAX_MASTER_KEY_LENGTH
);
1890 * If the version in the decrypted pre-master secret is correct then
1891 * version_good will be 0xff, otherwise it'll be zero. The
1892 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1893 * (http://eprint.iacr.org/2003/052/) exploits the version number
1894 * check as a "bad version oracle". Thus version checks are done in
1895 * constant time and are treated like any other decryption error.
1898 constant_time_eq_8(p
[0], (unsigned)(s
->client_version
>> 8));
1900 constant_time_eq_8(p
[1], (unsigned)(s
->client_version
& 0xff));
1903 * The premaster secret must contain the same version number as the
1904 * ClientHello to detect version rollback attacks (strangely, the
1905 * protocol does not offer such protection for DH ciphersuites).
1906 * However, buggy clients exist that send the negotiated protocol
1907 * version instead if the server does not support the requested
1908 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1911 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) {
1912 unsigned char workaround_good
;
1914 constant_time_eq_8(p
[0], (unsigned)(s
->version
>> 8));
1916 constant_time_eq_8(p
[1], (unsigned)(s
->version
& 0xff));
1917 version_good
|= workaround_good
;
1921 * Both decryption and version must be good for decrypt_good to
1922 * remain non-zero (0xff).
1924 decrypt_good
&= version_good
;
1927 * Now copy rand_premaster_secret over p using decrypt_good_mask.
1929 for (i
= 0; i
< (int)sizeof(rand_premaster_secret
); i
++) {
1930 p
[i
] = constant_time_select_8(decrypt_good
, p
[i
],
1931 rand_premaster_secret
[i
]);
1934 s
->session
->master_key_length
=
1935 s
->method
->ssl3_enc
->generate_master_secret(s
,
1937 session
->master_key
,
1939 OPENSSL_cleanse(p
, i
);
1942 #ifndef OPENSSL_NO_DH
1943 if (l
& (SSL_kEDH
| SSL_kDHr
| SSL_kDHd
)) {
1946 if (!(s
->options
& SSL_OP_SSLEAY_080_CLIENT_DH_BUG
)) {
1947 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1948 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
1956 if (n
== 0L) { /* the parameters are in the cert */
1957 al
= SSL_AD_HANDSHAKE_FAILURE
;
1958 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1959 SSL_R_UNABLE_TO_DECODE_DH_CERTS
);
1962 if (s
->s3
->tmp
.dh
== NULL
) {
1963 al
= SSL_AD_HANDSHAKE_FAILURE
;
1964 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1965 SSL_R_MISSING_TMP_DH_KEY
);
1968 dh_srvr
= s
->s3
->tmp
.dh
;
1971 pub
= BN_bin2bn(p
, i
, NULL
);
1973 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, SSL_R_BN_LIB
);
1977 i
= DH_compute_key(p
, pub
, dh_srvr
);
1980 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1985 DH_free(s
->s3
->tmp
.dh
);
1986 s
->s3
->tmp
.dh
= NULL
;
1990 s
->session
->master_key_length
=
1991 s
->method
->ssl3_enc
->generate_master_secret(s
,
1993 session
->master_key
,
1995 OPENSSL_cleanse(p
, i
);
1998 #ifndef OPENSSL_NO_KRB5
1999 if (l
& SSL_kKRB5
) {
2000 krb5_error_code krb5rc
;
2001 krb5_data enc_ticket
;
2002 krb5_data authenticator
;
2004 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
2005 EVP_CIPHER_CTX ciph_ctx
;
2006 EVP_CIPHER
*enc
= NULL
;
2007 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2008 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
+ EVP_MAX_BLOCK_LENGTH
];
2010 krb5_timestamp authtime
= 0;
2011 krb5_ticket_times ttimes
;
2013 EVP_CIPHER_CTX_init(&ciph_ctx
);
2016 kssl_ctx
= kssl_ctx_new();
2019 enc_ticket
.length
= i
;
2021 if (n
< (int)enc_ticket
.length
+ 6) {
2022 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2023 SSL_R_DATA_LENGTH_TOO_LONG
);
2027 enc_ticket
.data
= (char *)p
;
2028 p
+= enc_ticket
.length
;
2031 authenticator
.length
= i
;
2033 if (n
< (int)(enc_ticket
.length
+ authenticator
.length
) + 6) {
2034 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2035 SSL_R_DATA_LENGTH_TOO_LONG
);
2039 authenticator
.data
= (char *)p
;
2040 p
+= authenticator
.length
;
2044 enc_pms
.data
= (char *)p
;
2045 p
+= enc_pms
.length
;
2048 * Note that the length is checked again below, ** after decryption
2050 if (enc_pms
.length
> sizeof pms
) {
2051 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2052 SSL_R_DATA_LENGTH_TOO_LONG
);
2056 if (n
!= (long)(enc_ticket
.length
+ authenticator
.length
+
2057 enc_pms
.length
+ 6)) {
2058 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2059 SSL_R_DATA_LENGTH_TOO_LONG
);
2063 if ((krb5rc
= kssl_sget_tkt(kssl_ctx
, &enc_ticket
, &ttimes
,
2066 printf("kssl_sget_tkt rtn %d [%d]\n", krb5rc
, kssl_err
.reason
);
2068 printf("kssl_err text= %s\n", kssl_err
.text
);
2069 # endif /* KSSL_DEBUG */
2070 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, kssl_err
.reason
);
2075 * Note: no authenticator is not considered an error, ** but will
2076 * return authtime == 0.
2078 if ((krb5rc
= kssl_check_authent(kssl_ctx
, &authenticator
,
2079 &authtime
, &kssl_err
)) != 0) {
2081 printf("kssl_check_authent rtn %d [%d]\n",
2082 krb5rc
, kssl_err
.reason
);
2084 printf("kssl_err text= %s\n", kssl_err
.text
);
2085 # endif /* KSSL_DEBUG */
2086 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, kssl_err
.reason
);
2090 if ((krb5rc
= kssl_validate_times(authtime
, &ttimes
)) != 0) {
2091 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, krb5rc
);
2095 kssl_ctx_show(kssl_ctx
);
2096 # endif /* KSSL_DEBUG */
2098 enc
= kssl_map_enc(kssl_ctx
->enctype
);
2102 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2104 if (!EVP_DecryptInit_ex(&ciph_ctx
, enc
, NULL
, kssl_ctx
->key
, iv
)) {
2105 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2106 SSL_R_DECRYPTION_FAILED
);
2109 if (!EVP_DecryptUpdate(&ciph_ctx
, pms
, &outl
,
2110 (unsigned char *)enc_pms
.data
, enc_pms
.length
))
2112 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2113 SSL_R_DECRYPTION_FAILED
);
2116 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
) {
2117 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2118 SSL_R_DATA_LENGTH_TOO_LONG
);
2121 if (!EVP_DecryptFinal_ex(&ciph_ctx
, &(pms
[outl
]), &padl
)) {
2122 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2123 SSL_R_DECRYPTION_FAILED
);
2127 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
) {
2128 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2129 SSL_R_DATA_LENGTH_TOO_LONG
);
2132 if (!((pms
[0] == (s
->client_version
>> 8))
2133 && (pms
[1] == (s
->client_version
& 0xff)))) {
2135 * The premaster secret must contain the same version number as
2136 * the ClientHello to detect version rollback attacks (strangely,
2137 * the protocol does not offer such protection for DH
2138 * ciphersuites). However, buggy clients exist that send random
2139 * bytes instead of the protocol version. If
2140 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2141 * (Perhaps we should have a separate BUG value for the Kerberos
2144 if (!(s
->options
& SSL_OP_TLS_ROLLBACK_BUG
)) {
2145 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2146 SSL_AD_DECODE_ERROR
);
2151 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2153 s
->session
->master_key_length
=
2154 s
->method
->ssl3_enc
->generate_master_secret(s
,
2156 session
->master_key
,
2159 if (kssl_ctx
->client_princ
) {
2160 size_t len
= strlen(kssl_ctx
->client_princ
);
2161 if (len
< SSL_MAX_KRB5_PRINCIPAL_LENGTH
) {
2162 s
->session
->krb5_client_princ_len
= len
;
2163 memcpy(s
->session
->krb5_client_princ
, kssl_ctx
->client_princ
,
2168 /*- Was doing kssl_ctx_free() here,
2169 * but it caused problems for apache.
2170 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2171 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2174 #endif /* OPENSSL_NO_KRB5 */
2176 #ifndef OPENSSL_NO_ECDH
2177 if ((l
& SSL_kECDH
) || (l
& SSL_kECDHE
)) {
2181 const EC_GROUP
*group
;
2182 const BIGNUM
*priv_key
;
2184 /* initialize structures for server's ECDH key pair */
2185 if ((srvr_ecdh
= EC_KEY_new()) == NULL
) {
2186 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2190 /* Let's get server private key and group information */
2191 if (l
& SSL_kECDH
) {
2192 /* use the certificate */
2193 tkey
= s
->cert
->pkeys
[SSL_PKEY_ECC
].privatekey
->pkey
.ec
;
2196 * use the ephermeral values we saved when generating the
2197 * ServerKeyExchange msg.
2199 tkey
= s
->s3
->tmp
.ecdh
;
2202 group
= EC_KEY_get0_group(tkey
);
2203 priv_key
= EC_KEY_get0_private_key(tkey
);
2205 if (!EC_KEY_set_group(srvr_ecdh
, group
) ||
2206 !EC_KEY_set_private_key(srvr_ecdh
, priv_key
)) {
2207 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2211 /* Let's get client's public key */
2212 if ((clnt_ecpoint
= EC_POINT_new(group
)) == NULL
) {
2213 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2218 /* Client Publickey was in Client Certificate */
2220 if (l
& SSL_kECDHE
) {
2221 al
= SSL_AD_HANDSHAKE_FAILURE
;
2222 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2223 SSL_R_MISSING_TMP_ECDH_KEY
);
2226 if (((clnt_pub_pkey
= X509_get_pubkey(s
->session
->peer
))
2227 == NULL
) || (clnt_pub_pkey
->type
!= EVP_PKEY_EC
)) {
2229 * XXX: For now, we do not support client authentication
2230 * using ECDH certificates so this branch (n == 0L) of the
2231 * code is never executed. When that support is added, we
2232 * ought to ensure the key received in the certificate is
2233 * authorized for key agreement. ECDH_compute_key implicitly
2234 * checks that the two ECDH shares are for the same group.
2236 al
= SSL_AD_HANDSHAKE_FAILURE
;
2237 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2238 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS
);
2242 if (EC_POINT_copy(clnt_ecpoint
,
2243 EC_KEY_get0_public_key(clnt_pub_pkey
->
2245 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2248 ret
= 2; /* Skip certificate verify processing */
2251 * Get client's public key from encoded point in the
2252 * ClientKeyExchange message.
2254 if ((bn_ctx
= BN_CTX_new()) == NULL
) {
2255 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2256 ERR_R_MALLOC_FAILURE
);
2260 /* Get encoded point length */
2264 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2267 if (EC_POINT_oct2point(group
, clnt_ecpoint
, p
, i
, bn_ctx
) == 0) {
2268 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2272 * p is pointing to somewhere in the buffer currently, so set it
2275 p
= (unsigned char *)s
->init_buf
->data
;
2278 /* Compute the shared pre-master secret */
2279 field_size
= EC_GROUP_get_degree(group
);
2280 if (field_size
<= 0) {
2281 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2284 i
= ECDH_compute_key(p
, (field_size
+ 7) / 8, clnt_ecpoint
, srvr_ecdh
,
2287 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2291 EVP_PKEY_free(clnt_pub_pkey
);
2292 EC_POINT_free(clnt_ecpoint
);
2293 if (srvr_ecdh
!= NULL
)
2294 EC_KEY_free(srvr_ecdh
);
2295 BN_CTX_free(bn_ctx
);
2297 /* Compute the master secret */
2298 s
->session
->master_key_length
=
2299 s
->method
->ssl3_enc
->generate_master_secret(s
,
2301 session
->master_key
,
2304 OPENSSL_cleanse(p
, i
);
2309 al
= SSL_AD_HANDSHAKE_FAILURE
;
2310 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, SSL_R_UNKNOWN_CIPHER_TYPE
);
2316 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2317 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2320 #ifndef OPENSSL_NO_ECDH
2321 EVP_PKEY_free(clnt_pub_pkey
);
2322 EC_POINT_free(clnt_ecpoint
);
2323 if (srvr_ecdh
!= NULL
)
2324 EC_KEY_free(srvr_ecdh
);
2325 BN_CTX_free(bn_ctx
);
2330 int ssl3_get_cert_verify(SSL
*s
)
2332 EVP_PKEY
*pkey
= NULL
;
2334 int al
, ok
, ret
= 0;
2339 n
= s
->method
->ssl_get_message(s
,
2340 SSL3_ST_SR_CERT_VRFY_A
,
2341 SSL3_ST_SR_CERT_VRFY_B
,
2342 -1, SSL3_RT_MAX_PLAIN_LENGTH
, &ok
);
2347 if (s
->session
->peer
!= NULL
) {
2348 peer
= s
->session
->peer
;
2349 pkey
= X509_get_pubkey(peer
);
2350 type
= X509_certificate_type(peer
, pkey
);
2356 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
) {
2357 s
->s3
->tmp
.reuse_message
= 1;
2359 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2360 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_MISSING_VERIFY_MESSAGE
);
2368 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_NO_CLIENT_CERT_RECEIVED
);
2369 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2373 if (!(type
& EVP_PKT_SIGN
)) {
2374 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,
2375 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
2376 al
= SSL_AD_ILLEGAL_PARAMETER
;
2380 if (s
->s3
->change_cipher_spec
) {
2381 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_CCS_RECEIVED_EARLY
);
2382 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2386 /* we now have a signature that we need to verify */
2387 p
= (unsigned char *)s
->init_msg
;
2391 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
2392 al
= SSL_AD_DECODE_ERROR
;
2396 j
= EVP_PKEY_size(pkey
);
2397 if ((i
> j
) || (n
> j
) || (n
<= 0)) {
2398 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_WRONG_SIGNATURE_SIZE
);
2399 al
= SSL_AD_DECODE_ERROR
;
2402 #ifndef OPENSSL_NO_RSA
2403 if (pkey
->type
== EVP_PKEY_RSA
) {
2404 i
= RSA_verify(NID_md5_sha1
, s
->s3
->tmp
.cert_verify_md
,
2405 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
, p
, i
,
2408 al
= SSL_AD_DECRYPT_ERROR
;
2409 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_BAD_RSA_DECRYPT
);
2413 al
= SSL_AD_DECRYPT_ERROR
;
2414 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_BAD_RSA_SIGNATURE
);
2419 #ifndef OPENSSL_NO_DSA
2420 if (pkey
->type
== EVP_PKEY_DSA
) {
2421 j
= DSA_verify(pkey
->save_type
,
2422 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
2423 SHA_DIGEST_LENGTH
, p
, i
, pkey
->pkey
.dsa
);
2426 al
= SSL_AD_DECRYPT_ERROR
;
2427 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_BAD_DSA_SIGNATURE
);
2432 #ifndef OPENSSL_NO_ECDSA
2433 if (pkey
->type
== EVP_PKEY_EC
) {
2434 j
= ECDSA_verify(pkey
->save_type
,
2435 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
2436 SHA_DIGEST_LENGTH
, p
, i
, pkey
->pkey
.ec
);
2439 al
= SSL_AD_DECRYPT_ERROR
;
2440 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, SSL_R_BAD_ECDSA_SIGNATURE
);
2446 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
2447 al
= SSL_AD_UNSUPPORTED_CERTIFICATE
;
2454 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2457 EVP_PKEY_free(pkey
);
2461 int ssl3_get_client_certificate(SSL
*s
)
2463 int i
, ok
, al
, ret
= -1;
2465 unsigned long l
, nc
, llen
, n
;
2466 const unsigned char *p
, *q
;
2468 STACK_OF(X509
) *sk
= NULL
;
2470 n
= s
->method
->ssl_get_message(s
,
2473 -1, s
->max_cert_list
, &ok
);
2478 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
2479 if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2480 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2481 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2482 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2483 al
= SSL_AD_HANDSHAKE_FAILURE
;
2487 * If tls asked for a client cert, the client must return a 0 list
2489 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
) {
2490 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2491 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
2492 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2495 s
->s3
->tmp
.reuse_message
= 1;
2499 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
2500 al
= SSL_AD_UNEXPECTED_MESSAGE
;
2501 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, SSL_R_WRONG_MESSAGE_TYPE
);
2504 p
= d
= (unsigned char *)s
->init_msg
;
2506 if ((sk
= sk_X509_new_null()) == NULL
) {
2507 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
2512 if (llen
+ 3 != n
) {
2513 al
= SSL_AD_DECODE_ERROR
;
2514 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
2517 for (nc
= 0; nc
< llen
;) {
2519 if ((l
+ nc
+ 3) > llen
) {
2520 al
= SSL_AD_DECODE_ERROR
;
2521 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2522 SSL_R_CERT_LENGTH_MISMATCH
);
2527 x
= d2i_X509(NULL
, &p
, l
);
2529 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
2533 al
= SSL_AD_DECODE_ERROR
;
2534 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2535 SSL_R_CERT_LENGTH_MISMATCH
);
2538 if (!sk_X509_push(sk
, x
)) {
2539 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
2546 if (sk_X509_num(sk
) <= 0) {
2547 /* TLS does not mind 0 certs returned */
2548 if (s
->version
== SSL3_VERSION
) {
2549 al
= SSL_AD_HANDSHAKE_FAILURE
;
2550 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2551 SSL_R_NO_CERTIFICATES_RETURNED
);
2554 /* Fail for TLS only if we required a certificate */
2555 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
2556 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
2557 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2558 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2559 al
= SSL_AD_HANDSHAKE_FAILURE
;
2563 i
= ssl_verify_cert_chain(s
, sk
);
2565 al
= ssl_verify_alarm_type(s
->verify_result
);
2566 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
2567 SSL_R_NO_CERTIFICATE_RETURNED
);
2572 if (s
->session
->peer
!= NULL
) /* This should not be needed */
2573 X509_free(s
->session
->peer
);
2574 s
->session
->peer
= sk_X509_shift(sk
);
2575 s
->session
->verify_result
= s
->verify_result
;
2578 * With the current implementation, sess_cert will always be NULL when we
2581 if (s
->session
->sess_cert
== NULL
) {
2582 s
->session
->sess_cert
= ssl_sess_cert_new();
2583 if (s
->session
->sess_cert
== NULL
) {
2584 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
2588 if (s
->session
->sess_cert
->cert_chain
!= NULL
)
2589 sk_X509_pop_free(s
->session
->sess_cert
->cert_chain
, X509_free
);
2590 s
->session
->sess_cert
->cert_chain
= sk
;
2592 * Inconsistency alert: cert_chain does *not* include the peer's own
2593 * certificate, while we do include it in s3_clnt.c
2601 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2607 sk_X509_pop_free(sk
, X509_free
);
2611 int ssl3_send_server_certificate(SSL
*s
)
2616 if (s
->state
== SSL3_ST_SW_CERT_A
) {
2617 x
= ssl_get_server_send_cert(s
);
2619 /* VRS: allow null cert if auth == KRB5 */
2620 (s
->s3
->tmp
.new_cipher
->algorithms
2621 & (SSL_MKEY_MASK
| SSL_AUTH_MASK
))
2622 != (SSL_aKRB5
| SSL_kKRB5
)) {
2623 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
2627 l
= ssl3_output_cert_chain(s
, x
);
2628 s
->state
= SSL3_ST_SW_CERT_B
;
2629 s
->init_num
= (int)l
;
2633 /* SSL3_ST_SW_CERT_B */
2634 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
2637 #ifndef OPENSSL_NO_ECDH
2638 /* This is the complement of curve_id2nid in s3_clnt.c. */
2639 static int nid2curve_id(int nid
)
2642 * ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) (no changes
2643 * in draft-ietf-tls-ecc-03.txt [June 2003])
2646 case NID_sect163k1
: /* sect163k1 (1) */
2648 case NID_sect163r1
: /* sect163r1 (2) */
2650 case NID_sect163r2
: /* sect163r2 (3) */
2652 case NID_sect193r1
: /* sect193r1 (4) */
2654 case NID_sect193r2
: /* sect193r2 (5) */
2656 case NID_sect233k1
: /* sect233k1 (6) */
2658 case NID_sect233r1
: /* sect233r1 (7) */
2660 case NID_sect239k1
: /* sect239k1 (8) */
2662 case NID_sect283k1
: /* sect283k1 (9) */
2664 case NID_sect283r1
: /* sect283r1 (10) */
2666 case NID_sect409k1
: /* sect409k1 (11) */
2668 case NID_sect409r1
: /* sect409r1 (12) */
2670 case NID_sect571k1
: /* sect571k1 (13) */
2672 case NID_sect571r1
: /* sect571r1 (14) */
2674 case NID_secp160k1
: /* secp160k1 (15) */
2676 case NID_secp160r1
: /* secp160r1 (16) */
2678 case NID_secp160r2
: /* secp160r2 (17) */
2680 case NID_secp192k1
: /* secp192k1 (18) */
2682 case NID_X9_62_prime192v1
: /* secp192r1 (19) */
2684 case NID_secp224k1
: /* secp224k1 (20) */
2686 case NID_secp224r1
: /* secp224r1 (21) */
2688 case NID_secp256k1
: /* secp256k1 (22) */
2690 case NID_X9_62_prime256v1
: /* secp256r1 (23) */
2692 case NID_secp384r1
: /* secp384r1 (24) */
2694 case NID_secp521r1
: /* secp521r1 (25) */
2701 #ifndef OPENSSL_NO_TLSEXT
2702 int ssl3_send_newsession_ticket(SSL
*s
)
2704 if (s
->state
== SSL3_ST_SW_SESSION_TICKET_A
) {
2705 unsigned char *p
, *senc
, *macstart
;
2710 SSL_CTX
*tctx
= s
->initial_ctx
;
2711 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2712 unsigned char key_name
[16];
2714 /* get session encoding length */
2715 slen
= i2d_SSL_SESSION(s
->session
, NULL
);
2717 * Some length values are 16 bits, so forget it if session is too
2723 * Grow buffer if need be: the length calculation is as
2724 * follows 1 (size of message name) + 3 (message length
2725 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2726 * 16 (key name) + max_iv_len (iv length) +
2727 * session_length + max_enc_block_size (max encrypted session
2728 * length) + max_md_size (HMAC).
2730 if (!BUF_MEM_grow(s
->init_buf
,
2731 26 + EVP_MAX_IV_LENGTH
+ EVP_MAX_BLOCK_LENGTH
+
2732 EVP_MAX_MD_SIZE
+ slen
))
2734 senc
= OPENSSL_malloc(slen
);
2738 i2d_SSL_SESSION(s
->session
, &p
);
2740 p
= (unsigned char *)s
->init_buf
->data
;
2742 *(p
++) = SSL3_MT_NEWSESSION_TICKET
;
2743 /* Skip message length for now */
2745 EVP_CIPHER_CTX_init(&ctx
);
2746 HMAC_CTX_init(&hctx
);
2748 * Initialize HMAC and cipher contexts. If callback present it does
2749 * all the work otherwise use generated values from parent ctx.
2751 if (tctx
->tlsext_ticket_key_cb
) {
2752 if (tctx
->tlsext_ticket_key_cb(s
, key_name
, iv
, &ctx
,
2758 RAND_pseudo_bytes(iv
, 16);
2759 EVP_EncryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
2760 tctx
->tlsext_tick_aes_key
, iv
);
2761 HMAC_Init_ex(&hctx
, tctx
->tlsext_tick_hmac_key
, 16,
2762 tlsext_tick_md(), NULL
);
2763 memcpy(key_name
, tctx
->tlsext_tick_key_name
, 16);
2765 l2n(s
->session
->tlsext_tick_lifetime_hint
, p
);
2766 /* Skip ticket length for now */
2768 /* Output key name */
2770 memcpy(p
, key_name
, 16);
2773 memcpy(p
, iv
, EVP_CIPHER_CTX_iv_length(&ctx
));
2774 p
+= EVP_CIPHER_CTX_iv_length(&ctx
);
2775 /* Encrypt session data */
2776 EVP_EncryptUpdate(&ctx
, p
, &len
, senc
, slen
);
2778 EVP_EncryptFinal(&ctx
, p
, &len
);
2780 EVP_CIPHER_CTX_cleanup(&ctx
);
2782 HMAC_Update(&hctx
, macstart
, p
- macstart
);
2783 HMAC_Final(&hctx
, p
, &hlen
);
2784 HMAC_CTX_cleanup(&hctx
);
2787 /* Now write out lengths: p points to end of data written */
2789 len
= p
- (unsigned char *)s
->init_buf
->data
;
2790 p
= (unsigned char *)s
->init_buf
->data
+ 1;
2791 l2n3(len
- 4, p
); /* Message length */
2793 s2n(len
- 10, p
); /* Ticket length */
2795 /* number of bytes to write */
2797 s
->state
= SSL3_ST_SW_SESSION_TICKET_B
;
2802 /* SSL3_ST_SW_SESSION_TICKET_B */
2803 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
2806 int ssl3_send_cert_status(SSL
*s
)
2808 if (s
->state
== SSL3_ST_SW_CERT_STATUS_A
) {
2811 * Grow buffer if need be: the length calculation is as
2812 * follows 1 (message type) + 3 (message length) +
2813 * 1 (ocsp response type) + 3 (ocsp response length)
2816 if (!BUF_MEM_grow(s
->init_buf
, 8 + s
->tlsext_ocsp_resplen
))
2819 p
= (unsigned char *)s
->init_buf
->data
;
2822 *(p
++) = SSL3_MT_CERTIFICATE_STATUS
;
2823 /* message length */
2824 l2n3(s
->tlsext_ocsp_resplen
+ 4, p
);
2826 *(p
++) = s
->tlsext_status_type
;
2827 /* length of OCSP response */
2828 l2n3(s
->tlsext_ocsp_resplen
, p
);
2829 /* actual response */
2830 memcpy(p
, s
->tlsext_ocsp_resp
, s
->tlsext_ocsp_resplen
);
2831 /* number of bytes to write */
2832 s
->init_num
= 8 + s
->tlsext_ocsp_resplen
;
2833 s
->state
= SSL3_ST_SW_CERT_STATUS_B
;
2837 /* SSL3_ST_SW_CERT_STATUS_B */
2838 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));