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-2003 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.
126 #include "ssl_locl.h"
127 #include "kssl_lcl.h"
128 #include <openssl/buffer.h>
129 #include <openssl/rand.h>
130 #include <openssl/objects.h>
131 #include <openssl/evp.h>
132 #include <openssl/md5.h>
134 # include <openssl/fips.h>
137 #ifndef OPENSSL_NO_DH
138 # include <openssl/dh.h>
140 #include <openssl/bn.h>
141 #ifndef OPENSSL_NO_ENGINE
142 # include <openssl/engine.h>
145 static SSL_METHOD
*ssl3_get_client_method(int ver
);
146 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
);
148 #ifndef OPENSSL_NO_ECDH
149 static int curve_id2nid(int curve_id
);
150 int check_srvr_ecc_cert_and_alg(X509
*x
, SSL_CIPHER
*cs
);
153 static SSL_METHOD
*ssl3_get_client_method(int ver
)
155 if (ver
== SSL3_VERSION
)
156 return (SSLv3_client_method());
161 IMPLEMENT_ssl3_meth_func(SSLv3_client_method
,
162 ssl_undefined_function
,
163 ssl3_connect
, ssl3_get_client_method
)
165 int ssl3_connect(SSL
*s
)
168 unsigned long Time
= (unsigned long)time(NULL
);
169 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
171 int new_state
, state
, skip
= 0;
173 RAND_add(&Time
, sizeof(Time
), 0);
177 if (s
->info_callback
!= NULL
)
178 cb
= s
->info_callback
;
179 else if (s
->ctx
->info_callback
!= NULL
)
180 cb
= s
->ctx
->info_callback
;
183 if (!SSL_in_init(s
) || SSL_in_before(s
))
190 case SSL_ST_RENEGOTIATE
:
192 s
->state
= SSL_ST_CONNECT
;
193 s
->ctx
->stats
.sess_connect_renegotiate
++;
197 case SSL_ST_BEFORE
| SSL_ST_CONNECT
:
198 case SSL_ST_OK
| SSL_ST_CONNECT
:
202 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
204 if ((s
->version
& 0xff00) != 0x0300) {
205 SSLerr(SSL_F_SSL3_CONNECT
, ERR_R_INTERNAL_ERROR
);
210 /* s->version=SSL3_VERSION; */
211 s
->type
= SSL_ST_CONNECT
;
213 if (s
->init_buf
== NULL
) {
214 if ((buf
= BUF_MEM_new()) == NULL
) {
218 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
226 if (!ssl3_setup_buffers(s
)) {
231 /* setup buffing BIO */
232 if (!ssl_init_wbio_buffer(s
, 0)) {
237 /* don't push the buffering BIO quite yet */
239 ssl3_init_finished_mac(s
);
241 s
->state
= SSL3_ST_CW_CLNT_HELLO_A
;
242 s
->ctx
->stats
.sess_connect
++;
246 case SSL3_ST_CW_CLNT_HELLO_A
:
247 case SSL3_ST_CW_CLNT_HELLO_B
:
250 ret
= ssl3_client_hello(s
);
253 s
->state
= SSL3_ST_CR_SRVR_HELLO_A
;
256 /* turn on buffering for the next lot of output */
257 if (s
->bbio
!= s
->wbio
)
258 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
262 case SSL3_ST_CR_SRVR_HELLO_A
:
263 case SSL3_ST_CR_SRVR_HELLO_B
:
264 ret
= ssl3_get_server_hello(s
);
268 s
->state
= SSL3_ST_CR_FINISHED_A
;
269 #ifndef OPENSSL_NO_TLSEXT
270 if (s
->tlsext_ticket_expected
) {
271 /* receive renewed session ticket */
272 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
276 s
->state
= SSL3_ST_CR_CERT_A
;
280 case SSL3_ST_CR_CERT_A
:
281 case SSL3_ST_CR_CERT_B
:
282 #ifndef OPENSSL_NO_TLSEXT
283 ret
= ssl3_check_finished(s
);
288 if (s
->tlsext_ticket_expected
)
289 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
291 s
->state
= SSL3_ST_CR_FINISHED_A
;
296 /* Check if it is anon DH/ECDH */
297 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
)) {
298 ret
= ssl3_get_server_certificate(s
);
301 #ifndef OPENSSL_NO_TLSEXT
302 if (s
->tlsext_status_expected
)
303 s
->state
= SSL3_ST_CR_CERT_STATUS_A
;
305 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
308 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
314 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
319 case SSL3_ST_CR_KEY_EXCH_A
:
320 case SSL3_ST_CR_KEY_EXCH_B
:
321 ret
= ssl3_get_key_exchange(s
);
324 s
->state
= SSL3_ST_CR_CERT_REQ_A
;
328 * at this point we check that we have the required stuff from
331 if (!ssl3_check_cert_and_algorithm(s
)) {
337 case SSL3_ST_CR_CERT_REQ_A
:
338 case SSL3_ST_CR_CERT_REQ_B
:
339 ret
= ssl3_get_certificate_request(s
);
342 s
->state
= SSL3_ST_CR_SRVR_DONE_A
;
346 case SSL3_ST_CR_SRVR_DONE_A
:
347 case SSL3_ST_CR_SRVR_DONE_B
:
348 ret
= ssl3_get_server_done(s
);
351 if (s
->s3
->tmp
.cert_req
)
352 s
->state
= SSL3_ST_CW_CERT_A
;
354 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
359 case SSL3_ST_CW_CERT_A
:
360 case SSL3_ST_CW_CERT_B
:
361 case SSL3_ST_CW_CERT_C
:
362 case SSL3_ST_CW_CERT_D
:
363 ret
= ssl3_send_client_certificate(s
);
366 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
370 case SSL3_ST_CW_KEY_EXCH_A
:
371 case SSL3_ST_CW_KEY_EXCH_B
:
372 ret
= ssl3_send_client_key_exchange(s
);
376 * EAY EAY EAY need to check for DH fix cert sent back
379 * For TLS, cert_req is set to 2, so a cert chain of nothing is
380 * sent, but no verify packet is sent
383 * XXX: For now, we do not support client authentication in ECDH
384 * cipher suites with ECDH (rather than ECDSA) certificates. We
385 * need to skip the certificate verify message when client's
386 * ECDH public key is sent inside the client certificate.
388 if (s
->s3
->tmp
.cert_req
== 1) {
389 s
->state
= SSL3_ST_CW_CERT_VRFY_A
;
391 s
->state
= SSL3_ST_CW_CHANGE_A
;
392 s
->s3
->change_cipher_spec
= 0;
398 case SSL3_ST_CW_CERT_VRFY_A
:
399 case SSL3_ST_CW_CERT_VRFY_B
:
400 ret
= ssl3_send_client_verify(s
);
403 s
->state
= SSL3_ST_CW_CHANGE_A
;
405 s
->s3
->change_cipher_spec
= 0;
408 case SSL3_ST_CW_CHANGE_A
:
409 case SSL3_ST_CW_CHANGE_B
:
410 ret
= ssl3_send_change_cipher_spec(s
,
412 SSL3_ST_CW_CHANGE_B
);
415 s
->state
= SSL3_ST_CW_FINISHED_A
;
418 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
419 #ifdef OPENSSL_NO_COMP
420 s
->session
->compress_meth
= 0;
422 if (s
->s3
->tmp
.new_compression
== NULL
)
423 s
->session
->compress_meth
= 0;
425 s
->session
->compress_meth
= s
->s3
->tmp
.new_compression
->id
;
427 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
432 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
433 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
441 case SSL3_ST_CW_FINISHED_A
:
442 case SSL3_ST_CW_FINISHED_B
:
443 ret
= ssl3_send_finished(s
,
444 SSL3_ST_CW_FINISHED_A
,
445 SSL3_ST_CW_FINISHED_B
,
447 ssl3_enc
->client_finished_label
,
449 ssl3_enc
->client_finished_label_len
);
452 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
453 s
->state
= SSL3_ST_CW_FLUSH
;
456 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
458 s
->s3
->tmp
.next_state
= SSL_ST_OK
;
459 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
460 s
->state
= SSL_ST_OK
;
461 s
->s3
->flags
|= SSL3_FLAGS_POP_BUFFER
;
462 s
->s3
->delay_buf_pop_ret
= 0;
465 #ifndef OPENSSL_NO_TLSEXT
467 * Allow NewSessionTicket if ticket expected
469 if (s
->tlsext_ticket_expected
)
470 s
->s3
->tmp
.next_state
= SSL3_ST_CR_SESSION_TICKET_A
;
474 s
->s3
->tmp
.next_state
= SSL3_ST_CR_FINISHED_A
;
479 #ifndef OPENSSL_NO_TLSEXT
480 case SSL3_ST_CR_SESSION_TICKET_A
:
481 case SSL3_ST_CR_SESSION_TICKET_B
:
482 ret
= ssl3_get_new_session_ticket(s
);
485 s
->state
= SSL3_ST_CR_FINISHED_A
;
489 case SSL3_ST_CR_CERT_STATUS_A
:
490 case SSL3_ST_CR_CERT_STATUS_B
:
491 ret
= ssl3_get_cert_status(s
);
494 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
499 case SSL3_ST_CR_FINISHED_A
:
500 case SSL3_ST_CR_FINISHED_B
:
502 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
503 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
504 SSL3_ST_CR_FINISHED_B
);
509 s
->state
= SSL3_ST_CW_CHANGE_A
;
511 s
->state
= SSL_ST_OK
;
515 case SSL3_ST_CW_FLUSH
:
516 s
->rwstate
= SSL_WRITING
;
517 if (BIO_flush(s
->wbio
) <= 0) {
521 s
->rwstate
= SSL_NOTHING
;
522 s
->state
= s
->s3
->tmp
.next_state
;
526 /* clean a few things up */
527 ssl3_cleanup_key_block(s
);
529 if (s
->init_buf
!= NULL
) {
530 BUF_MEM_free(s
->init_buf
);
535 * If we are not 'joining' the last two packets, remove the
538 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
539 ssl_free_wbio_buffer(s
);
540 /* else do it later in ssl3_write */
545 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
547 s
->ctx
->stats
.sess_hit
++;
551 s
->handshake_func
= ssl3_connect
;
552 s
->ctx
->stats
.sess_connect_good
++;
555 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
561 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_UNKNOWN_STATE
);
567 /* did we do anything */
568 if (!s
->s3
->tmp
.reuse_message
&& !skip
) {
570 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
574 if ((cb
!= NULL
) && (s
->state
!= state
)) {
575 new_state
= s
->state
;
577 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
578 s
->state
= new_state
;
588 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
592 int ssl3_client_hello(SSL
*s
)
595 unsigned char *p
, *d
;
597 unsigned long Time
, l
;
598 #ifndef OPENSSL_NO_COMP
603 buf
= (unsigned char *)s
->init_buf
->data
;
604 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
) {
605 SSL_SESSION
*sess
= s
->session
;
606 if ((sess
== NULL
) || (sess
->ssl_version
!= s
->version
) ||
607 #ifdef OPENSSL_NO_TLSEXT
608 !sess
->session_id_length
||
610 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
612 (sess
->not_resumable
)) {
613 if (!ssl_get_new_session(s
, 0))
616 /* else use the pre-loaded session */
618 p
= s
->s3
->client_random
;
619 Time
= (unsigned long)time(NULL
); /* Time */
621 if (RAND_pseudo_bytes(p
, SSL3_RANDOM_SIZE
- 4) <= 0)
624 /* Do the message type and length last */
627 *(p
++) = s
->version
>> 8;
628 *(p
++) = s
->version
& 0xff;
629 s
->client_version
= s
->version
;
632 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
633 p
+= SSL3_RANDOM_SIZE
;
639 i
= s
->session
->session_id_length
;
642 if (i
> (int)sizeof(s
->session
->session_id
)) {
643 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
646 memcpy(p
, s
->session
->session_id
, i
);
650 /* Ciphers supported */
651 i
= ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &(p
[2]), 0);
653 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_NO_CIPHERS_AVAILABLE
);
660 #ifdef OPENSSL_NO_COMP
663 if (s
->ctx
->comp_methods
== NULL
)
666 j
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
668 for (i
= 0; i
< j
; i
++) {
669 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, i
);
673 *(p
++) = 0; /* Add the NULL method */
674 #ifndef OPENSSL_NO_TLSEXT
676 ssl_add_clienthello_tlsext(s
, p
,
677 buf
+ SSL3_RT_MAX_PLAIN_LENGTH
)) ==
679 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
685 *(d
++) = SSL3_MT_CLIENT_HELLO
;
688 s
->state
= SSL3_ST_CW_CLNT_HELLO_B
;
689 /* number of bytes to write */
690 s
->init_num
= p
- buf
;
694 /* SSL3_ST_CW_CLNT_HELLO_B */
695 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
700 int ssl3_get_server_hello(SSL
*s
)
702 STACK_OF(SSL_CIPHER
) *sk
;
704 unsigned char *p
, *d
;
708 #ifndef OPENSSL_NO_COMP
712 n
= s
->method
->ssl_get_message(s
,
713 SSL3_ST_CR_SRVR_HELLO_A
,
714 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, &ok
);
719 if (SSL_version(s
) == DTLS1_VERSION
|| SSL_version(s
) == DTLS1_BAD_VER
) {
720 if (s
->s3
->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
721 if (s
->d1
->send_cookie
== 0) {
722 s
->s3
->tmp
.reuse_message
= 1;
724 } else { /* already sent a cookie */
726 al
= SSL_AD_UNEXPECTED_MESSAGE
;
727 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
733 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
734 al
= SSL_AD_UNEXPECTED_MESSAGE
;
735 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
739 d
= p
= (unsigned char *)s
->init_msg
;
741 if ((p
[0] != (s
->version
>> 8)) || (p
[1] != (s
->version
& 0xff))) {
742 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
743 s
->version
= (s
->version
& 0xff00) | p
[1];
744 al
= SSL_AD_PROTOCOL_VERSION
;
749 /* load the server hello data */
750 /* load the server random */
751 memcpy(s
->s3
->server_random
, p
, SSL3_RANDOM_SIZE
);
752 p
+= SSL3_RANDOM_SIZE
;
754 /* get the session-id */
757 if ((j
> sizeof s
->session
->session_id
) || (j
> SSL3_SESSION_ID_SIZE
)) {
758 al
= SSL_AD_ILLEGAL_PARAMETER
;
759 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
763 if (j
!= 0 && j
== s
->session
->session_id_length
764 && memcmp(p
, s
->session
->session_id
, j
) == 0) {
765 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
766 || memcmp(s
->session
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
)) {
767 /* actually a client application bug */
768 al
= SSL_AD_ILLEGAL_PARAMETER
;
769 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
770 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
773 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
775 } else { /* a miss or crap from the other end */
778 * If we were trying for session-id reuse, make a new SSL_SESSION so
779 * we don't stuff up other people
782 if (s
->session
->session_id_length
> 0) {
783 if (!ssl_get_new_session(s
, 0)) {
784 al
= SSL_AD_INTERNAL_ERROR
;
788 s
->session
->session_id_length
= j
;
789 memcpy(s
->session
->session_id
, p
, j
); /* j could be 0 */
792 c
= ssl_get_cipher_by_char(s
, p
);
795 al
= SSL_AD_ILLEGAL_PARAMETER
;
796 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
799 p
+= ssl_put_cipher_by_char(s
, NULL
, NULL
);
801 sk
= ssl_get_ciphers_by_id(s
);
802 i
= sk_SSL_CIPHER_find(sk
, c
);
804 /* we did not say we would use this cipher */
805 al
= SSL_AD_ILLEGAL_PARAMETER
;
806 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
811 * Depending on the session caching (internal/external), the cipher
812 * and/or cipher_id values may not be set. Make sure that cipher_id is
813 * set and use it for comparison.
815 if (s
->session
->cipher
)
816 s
->session
->cipher_id
= s
->session
->cipher
->id
;
817 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
818 /* Workaround is now obsolete */
820 if (!(s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
))
823 al
= SSL_AD_ILLEGAL_PARAMETER
;
824 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
825 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
829 s
->s3
->tmp
.new_cipher
= c
;
831 /* lets get the compression algorithm */
833 #ifdef OPENSSL_NO_COMP
835 al
= SSL_AD_ILLEGAL_PARAMETER
;
836 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
837 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
845 comp
= ssl3_comp_find(s
->ctx
->comp_methods
, j
);
847 if ((j
!= 0) && (comp
== NULL
)) {
848 al
= SSL_AD_ILLEGAL_PARAMETER
;
849 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
850 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
853 s
->s3
->tmp
.new_compression
= comp
;
856 #ifndef OPENSSL_NO_TLSEXT
858 if (s
->version
>= SSL3_VERSION
) {
859 if (!ssl_parse_serverhello_tlsext(s
, &p
, d
, n
, &al
)) {
860 /* 'al' set by ssl_parse_serverhello_tlsext */
861 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_PARSE_TLSEXT
);
864 if (ssl_check_serverhello_tlsext(s
) <= 0) {
865 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SERVERHELLO_TLSEXT
);
872 /* wrong packet length */
873 al
= SSL_AD_DECODE_ERROR
;
874 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_PACKET_LENGTH
);
880 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
881 #ifndef OPENSSL_NO_TLSEXT
887 int ssl3_get_server_certificate(SSL
*s
)
889 int al
, i
, ok
, ret
= -1;
890 unsigned long n
, nc
, llen
, l
;
892 const unsigned char *q
, *p
;
894 STACK_OF(X509
) *sk
= NULL
;
896 EVP_PKEY
*pkey
= NULL
;
897 int need_cert
= 1; /* VRS: 0=> will allow null cert if auth ==
900 n
= s
->method
->ssl_get_message(s
,
903 -1, s
->max_cert_list
, &ok
);
908 if ((s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) ||
909 ((s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aKRB5
) &&
910 (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
))) {
911 s
->s3
->tmp
.reuse_message
= 1;
915 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
916 al
= SSL_AD_UNEXPECTED_MESSAGE
;
917 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_BAD_MESSAGE_TYPE
);
920 p
= d
= (unsigned char *)s
->init_msg
;
922 if ((sk
= sk_X509_new_null()) == NULL
) {
923 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
929 al
= SSL_AD_DECODE_ERROR
;
930 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
933 for (nc
= 0; nc
< llen
;) {
935 if ((l
+ nc
+ 3) > llen
) {
936 al
= SSL_AD_DECODE_ERROR
;
937 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
938 SSL_R_CERT_LENGTH_MISMATCH
);
943 x
= d2i_X509(NULL
, &q
, l
);
945 al
= SSL_AD_BAD_CERTIFICATE
;
946 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_ASN1_LIB
);
950 al
= SSL_AD_DECODE_ERROR
;
951 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
952 SSL_R_CERT_LENGTH_MISMATCH
);
955 if (!sk_X509_push(sk
, x
)) {
956 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
964 i
= ssl_verify_cert_chain(s
, sk
);
965 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)
966 #ifndef OPENSSL_NO_KRB5
968 new_cipher
->algorithms
& (SSL_MKEY_MASK
| SSL_AUTH_MASK
))
969 != (SSL_aKRB5
| SSL_kKRB5
)
970 #endif /* OPENSSL_NO_KRB5 */
972 al
= ssl_verify_alarm_type(s
->verify_result
);
973 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
974 SSL_R_CERTIFICATE_VERIFY_FAILED
);
977 ERR_clear_error(); /* but we keep s->verify_result */
979 sc
= ssl_sess_cert_new();
983 if (s
->session
->sess_cert
)
984 ssl_sess_cert_free(s
->session
->sess_cert
);
985 s
->session
->sess_cert
= sc
;
989 * Inconsistency alert: cert_chain does include the peer's certificate,
990 * which we don't include in s3_srvr.c
992 x
= sk_X509_value(sk
, 0);
995 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
998 pkey
= X509_get_pubkey(x
);
1000 /* VRS: allow null cert if auth == KRB5 */
1001 need_cert
= ((s
->s3
->tmp
.new_cipher
->algorithms
1002 & (SSL_MKEY_MASK
| SSL_AUTH_MASK
))
1003 == (SSL_aKRB5
| SSL_kKRB5
)) ? 0 : 1;
1006 printf("pkey,x = %p, %p\n", (void *)pkey
, (void *)x
);
1007 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x
, pkey
));
1008 printf("cipher, alg, nc = %s, %lx, %d\n", s
->s3
->tmp
.new_cipher
->name
,
1009 s
->s3
->tmp
.new_cipher
->algorithms
, need_cert
);
1010 #endif /* KSSL_DEBUG */
1012 if (need_cert
&& ((pkey
== NULL
) || EVP_PKEY_missing_parameters(pkey
))) {
1015 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1016 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1020 i
= ssl_cert_type(x
, pkey
);
1021 if (need_cert
&& i
< 0) {
1024 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1025 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1030 sc
->peer_cert_type
= i
;
1031 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1033 * Why would the following ever happen? We just created sc a couple
1036 if (sc
->peer_pkeys
[i
].x509
!= NULL
)
1037 X509_free(sc
->peer_pkeys
[i
].x509
);
1038 sc
->peer_pkeys
[i
].x509
= x
;
1039 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1041 if (s
->session
->peer
!= NULL
)
1042 X509_free(s
->session
->peer
);
1043 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1044 s
->session
->peer
= x
;
1046 sc
->peer_cert_type
= i
;
1047 sc
->peer_key
= NULL
;
1049 if (s
->session
->peer
!= NULL
)
1050 X509_free(s
->session
->peer
);
1051 s
->session
->peer
= NULL
;
1053 s
->session
->verify_result
= s
->verify_result
;
1060 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1063 EVP_PKEY_free(pkey
);
1065 sk_X509_pop_free(sk
, X509_free
);
1069 int ssl3_get_key_exchange(SSL
*s
)
1071 #ifndef OPENSSL_NO_RSA
1072 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
* 2];
1075 unsigned char *param
, *p
;
1077 long i
, param_len
, n
, alg
;
1078 EVP_PKEY
*pkey
= NULL
;
1079 #ifndef OPENSSL_NO_RSA
1082 #ifndef OPENSSL_NO_DH
1085 #ifndef OPENSSL_NO_ECDH
1086 EC_KEY
*ecdh
= NULL
;
1087 BN_CTX
*bn_ctx
= NULL
;
1088 EC_POINT
*srvr_ecpoint
= NULL
;
1090 int encoded_pt_len
= 0;
1094 * use same message size as in ssl3_get_certificate_request() as
1095 * ServerKeyExchange message may be skipped
1097 n
= s
->method
->ssl_get_message(s
,
1098 SSL3_ST_CR_KEY_EXCH_A
,
1099 SSL3_ST_CR_KEY_EXCH_B
,
1100 -1, s
->max_cert_list
, &ok
);
1105 alg
= s
->s3
->tmp
.new_cipher
->algorithms
;
1106 EVP_MD_CTX_init(&md_ctx
);
1108 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1110 * Can't skip server key exchange if this is an ephemeral
1113 if (alg
& (SSL_kEDH
| SSL_kECDHE
)) {
1114 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1115 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1118 s
->s3
->tmp
.reuse_message
= 1;
1122 param
= p
= (unsigned char *)s
->init_msg
;
1124 if (s
->session
->sess_cert
!= NULL
) {
1125 #ifndef OPENSSL_NO_RSA
1126 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
) {
1127 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
1128 s
->session
->sess_cert
->peer_rsa_tmp
= NULL
;
1131 #ifndef OPENSSL_NO_DH
1132 if (s
->session
->sess_cert
->peer_dh_tmp
) {
1133 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
1134 s
->session
->sess_cert
->peer_dh_tmp
= NULL
;
1137 #ifndef OPENSSL_NO_ECDH
1138 if (s
->session
->sess_cert
->peer_ecdh_tmp
) {
1139 EC_KEY_free(s
->session
->sess_cert
->peer_ecdh_tmp
);
1140 s
->session
->sess_cert
->peer_ecdh_tmp
= NULL
;
1144 s
->session
->sess_cert
= ssl_sess_cert_new();
1147 /* Total length of the parameters including the length prefix */
1150 al
= SSL_AD_DECODE_ERROR
;
1151 #ifndef OPENSSL_NO_RSA
1152 if (alg
& SSL_kRSA
) {
1153 /* Temporary RSA keys only allowed in export ciphersuites */
1154 if (!SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)) {
1155 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1156 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1159 if ((rsa
= RSA_new()) == NULL
) {
1160 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1165 if (param_len
> n
) {
1166 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1171 if (i
> n
- param_len
) {
1172 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_MODULUS_LENGTH
);
1177 if (!(rsa
->n
= BN_bin2bn(p
, i
, rsa
->n
))) {
1178 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1183 if (2 > n
- param_len
) {
1184 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1191 if (i
> n
- param_len
) {
1192 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_E_LENGTH
);
1197 if (!(rsa
->e
= BN_bin2bn(p
, i
, rsa
->e
))) {
1198 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1204 /* this should be because we are using an export cipher */
1207 X509_get_pubkey(s
->session
->
1208 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1210 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1213 s
->session
->sess_cert
->peer_rsa_tmp
= rsa
;
1216 #else /* OPENSSL_NO_RSA */
1219 #ifndef OPENSSL_NO_DH
1220 else if (alg
& SSL_kEDH
) {
1221 if ((dh
= DH_new()) == NULL
) {
1222 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1227 if (param_len
> n
) {
1228 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1233 if (i
> n
- param_len
) {
1234 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_P_LENGTH
);
1239 if (!(dh
->p
= BN_bin2bn(p
, i
, NULL
))) {
1240 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1245 if (2 > n
- param_len
) {
1246 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1253 if (i
> n
- param_len
) {
1254 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_G_LENGTH
);
1259 if (!(dh
->g
= BN_bin2bn(p
, i
, NULL
))) {
1260 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1265 if (2 > n
- param_len
) {
1266 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1273 if (i
> n
- param_len
) {
1274 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1279 if (!(dh
->pub_key
= BN_bin2bn(p
, i
, NULL
))) {
1280 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1286 # ifndef OPENSSL_NO_RSA
1289 X509_get_pubkey(s
->session
->
1290 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1294 # ifndef OPENSSL_NO_DSA
1295 else if (alg
& SSL_aDSS
)
1297 X509_get_pubkey(s
->session
->
1298 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1301 /* else anonymous DH, so no certificate or pkey. */
1303 s
->session
->sess_cert
->peer_dh_tmp
= dh
;
1305 } else if ((alg
& SSL_kDHr
) || (alg
& SSL_kDHd
)) {
1306 al
= SSL_AD_ILLEGAL_PARAMETER
;
1307 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1308 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1311 #endif /* !OPENSSL_NO_DH */
1313 #ifndef OPENSSL_NO_ECDH
1314 else if (alg
& SSL_kECDHE
) {
1316 const EC_GROUP
*group
;
1318 if ((ecdh
= EC_KEY_new()) == NULL
) {
1319 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1324 * Extract elliptic curve parameters and the server's ephemeral ECDH
1325 * public key. Keep accumulating lengths of various components in
1326 * param_len and make sure it never exceeds n.
1330 * XXX: For now we only support named (not generic) curves and the
1331 * ECParameters in this case is just three bytes. We also need one
1332 * byte for the length of the encoded point
1335 if (param_len
> n
) {
1336 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1340 if ((*p
!= NAMED_CURVE_TYPE
) ||
1341 ((curve_nid
= curve_id2nid(*(p
+ 2))) == 0)) {
1342 al
= SSL_AD_INTERNAL_ERROR
;
1343 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1344 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1348 ngroup
= EC_GROUP_new_by_curve_name(curve_nid
);
1349 if (ngroup
== NULL
) {
1350 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1353 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1354 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1357 EC_GROUP_free(ngroup
);
1359 group
= EC_KEY_get0_group(ecdh
);
1361 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1362 (EC_GROUP_get_degree(group
) > 163)) {
1363 al
= SSL_AD_EXPORT_RESTRICTION
;
1364 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1365 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1371 /* Next, get the encoded ECPoint */
1372 if (((srvr_ecpoint
= EC_POINT_new(group
)) == NULL
) ||
1373 ((bn_ctx
= BN_CTX_new()) == NULL
)) {
1374 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1378 encoded_pt_len
= *p
; /* length of encoded point */
1381 if ((encoded_pt_len
> n
- param_len
) ||
1382 (EC_POINT_oct2point(group
, srvr_ecpoint
,
1383 p
, encoded_pt_len
, bn_ctx
) == 0)) {
1384 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_ECPOINT
);
1387 param_len
+= encoded_pt_len
;
1390 p
+= encoded_pt_len
;
1393 * The ECC/TLS specification does not mention the use of DSA to sign
1394 * ECParameters in the server key exchange message. We do support RSA
1398 # ifndef OPENSSL_NO_RSA
1399 else if (alg
& SSL_aRSA
)
1401 X509_get_pubkey(s
->session
->
1402 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1404 # ifndef OPENSSL_NO_ECDSA
1405 else if (alg
& SSL_aECDSA
)
1407 X509_get_pubkey(s
->session
->
1408 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1410 /* else anonymous ECDH, so no certificate or pkey. */
1411 EC_KEY_set_public_key(ecdh
, srvr_ecpoint
);
1412 s
->session
->sess_cert
->peer_ecdh_tmp
= ecdh
;
1414 BN_CTX_free(bn_ctx
);
1416 EC_POINT_free(srvr_ecpoint
);
1417 srvr_ecpoint
= NULL
;
1418 } else if (alg
& SSL_kECDH
) {
1419 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1420 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1423 #endif /* !OPENSSL_NO_ECDH */
1424 if (alg
& SSL_aFZA
) {
1425 al
= SSL_AD_HANDSHAKE_FAILURE
;
1426 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1427 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1431 /* p points to the next byte, there are 'n' bytes left */
1433 /* if it was signed, check the signature */
1437 j
= EVP_PKEY_size(pkey
);
1440 * Check signature length. If n is 0 then signature is empty
1442 if ((i
!= n
) || (n
> j
) || (n
<= 0)) {
1443 /* wrong packet length */
1444 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1447 #ifndef OPENSSL_NO_RSA
1448 if (pkey
->type
== EVP_PKEY_RSA
) {
1454 for (num
= 2; num
> 0; num
--) {
1455 EVP_MD_CTX_set_flags(&md_ctx
, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
1456 EVP_DigestInit_ex(&md_ctx
, (num
== 2)
1457 ? s
->ctx
->md5
: s
->ctx
->sha1
, NULL
);
1458 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1460 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1462 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1463 EVP_DigestFinal_ex(&md_ctx
, q
, &size
);
1467 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
, pkey
->pkey
.rsa
);
1469 al
= SSL_AD_DECRYPT_ERROR
;
1470 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_DECRYPT
);
1475 al
= SSL_AD_DECRYPT_ERROR
;
1476 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1481 #ifndef OPENSSL_NO_DSA
1482 if (pkey
->type
== EVP_PKEY_DSA
) {
1484 EVP_VerifyInit_ex(&md_ctx
, EVP_dss1(), NULL
);
1485 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1487 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1489 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1490 if (EVP_VerifyFinal(&md_ctx
, p
, (int)n
, pkey
) <= 0) {
1492 al
= SSL_AD_DECRYPT_ERROR
;
1493 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1498 #ifndef OPENSSL_NO_ECDSA
1499 if (pkey
->type
== EVP_PKEY_EC
) {
1500 /* let's do ECDSA */
1501 EVP_VerifyInit_ex(&md_ctx
, EVP_ecdsa(), NULL
);
1502 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1504 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1506 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1507 if (EVP_VerifyFinal(&md_ctx
, p
, (int)n
, pkey
) <= 0) {
1509 al
= SSL_AD_DECRYPT_ERROR
;
1510 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1516 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1520 /* still data left over */
1521 if (!(alg
& SSL_aNULL
)) {
1522 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1526 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1530 EVP_PKEY_free(pkey
);
1531 EVP_MD_CTX_cleanup(&md_ctx
);
1534 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1536 EVP_PKEY_free(pkey
);
1537 #ifndef OPENSSL_NO_RSA
1541 #ifndef OPENSSL_NO_DH
1545 #ifndef OPENSSL_NO_ECDH
1546 BN_CTX_free(bn_ctx
);
1547 EC_POINT_free(srvr_ecpoint
);
1551 EVP_MD_CTX_cleanup(&md_ctx
);
1555 int ssl3_get_certificate_request(SSL
*s
)
1558 unsigned long n
, nc
, l
;
1559 unsigned int llen
, ctype_num
, i
;
1560 X509_NAME
*xn
= NULL
;
1561 const unsigned char *p
, *q
;
1563 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1565 n
= s
->method
->ssl_get_message(s
,
1566 SSL3_ST_CR_CERT_REQ_A
,
1567 SSL3_ST_CR_CERT_REQ_B
,
1568 -1, s
->max_cert_list
, &ok
);
1573 s
->s3
->tmp
.cert_req
= 0;
1575 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1576 s
->s3
->tmp
.reuse_message
= 1;
1580 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1581 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1582 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_WRONG_MESSAGE_TYPE
);
1586 /* TLS does not like anon-DH with client cert */
1587 if (s
->version
> SSL3_VERSION
) {
1588 l
= s
->s3
->tmp
.new_cipher
->algorithms
;
1589 if (l
& SSL_aNULL
) {
1590 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1591 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1592 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1597 p
= d
= (unsigned char *)s
->init_msg
;
1599 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1600 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
1604 /* get the certificate types */
1606 if (ctype_num
> SSL3_CT_NUMBER
)
1607 ctype_num
= SSL3_CT_NUMBER
;
1608 for (i
= 0; i
< ctype_num
; i
++)
1609 s
->s3
->tmp
.ctype
[i
] = p
[i
];
1612 /* get the CA RDNs */
1617 out
= fopen("/tmp/vsign.der", "w");
1618 fwrite(p
, 1, llen
, out
);
1623 if ((llen
+ ctype_num
+ 2 + 1) != n
) {
1624 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1625 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_LENGTH_MISMATCH
);
1629 for (nc
= 0; nc
< llen
;) {
1631 if ((l
+ nc
+ 2) > llen
) {
1632 if ((s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1633 goto cont
; /* netscape bugs */
1634 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1635 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_CA_DN_TOO_LONG
);
1641 if ((xn
= d2i_X509_NAME(NULL
, &q
, l
)) == NULL
) {
1642 /* If netscape tolerance is on, ignore errors */
1643 if (s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
)
1646 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1647 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_ASN1_LIB
);
1653 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1654 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1655 SSL_R_CA_DN_LENGTH_MISMATCH
);
1658 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1659 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
1672 /* we should setup a certificate to return.... */
1673 s
->s3
->tmp
.cert_req
= 1;
1674 s
->s3
->tmp
.ctype_num
= ctype_num
;
1675 if (s
->s3
->tmp
.ca_names
!= NULL
)
1676 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
1677 s
->s3
->tmp
.ca_names
= ca_sk
;
1683 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1687 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
1689 return (X509_NAME_cmp(*a
, *b
));
1692 #ifndef OPENSSL_NO_TLSEXT
1693 int ssl3_get_new_session_ticket(SSL
*s
)
1695 int ok
, al
, ret
= 0, ticklen
;
1697 const unsigned char *p
;
1700 n
= s
->method
->ssl_get_message(s
,
1701 SSL3_ST_CR_SESSION_TICKET_A
,
1702 SSL3_ST_CR_SESSION_TICKET_B
,
1708 if (s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
) {
1709 s
->s3
->tmp
.reuse_message
= 1;
1712 if (s
->s3
->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
) {
1713 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1714 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_BAD_MESSAGE_TYPE
);
1718 /* need at least ticket_lifetime_hint + ticket length */
1719 al
= SSL_AD_DECODE_ERROR
;
1720 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
1724 p
= d
= (unsigned char *)s
->init_msg
;
1726 if (s
->session
->session_id_length
> 0) {
1727 int i
= s
->session_ctx
->session_cache_mode
;
1728 SSL_SESSION
*new_sess
;
1730 * We reused an existing session, so we need to replace it with a new
1733 if (i
& SSL_SESS_CACHE_CLIENT
) {
1735 * Remove the old session from the cache
1737 if (i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) {
1738 if (s
->session_ctx
->remove_session_cb
!= NULL
)
1739 s
->session_ctx
->remove_session_cb(s
->session_ctx
,
1742 /* We carry on if this fails */
1743 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1747 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
1748 al
= SSL_AD_INTERNAL_ERROR
;
1749 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
1753 SSL_SESSION_free(s
->session
);
1754 s
->session
= new_sess
;
1757 n2l(p
, s
->session
->tlsext_tick_lifetime_hint
);
1759 /* ticket_lifetime_hint + ticket_length + ticket */
1760 if (ticklen
+ 6 != n
) {
1761 al
= SSL_AD_DECODE_ERROR
;
1762 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
1765 if (s
->session
->tlsext_tick
) {
1766 OPENSSL_free(s
->session
->tlsext_tick
);
1767 s
->session
->tlsext_ticklen
= 0;
1769 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
1770 if (!s
->session
->tlsext_tick
) {
1771 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
1774 memcpy(s
->session
->tlsext_tick
, p
, ticklen
);
1775 s
->session
->tlsext_ticklen
= ticklen
;
1777 * There are two ways to detect a resumed ticket sesion. One is to set an
1778 * appropriate session ID and then the server must return a match in
1779 * ServerHello. This allows the normal client session ID matching to work
1780 * and we know much earlier that the ticket has been accepted. The
1781 * other way is to set zero length session ID when the ticket is
1782 * presented and rely on the handshake to determine session resumption.
1783 * We choose the former approach because this fits in with assumptions
1784 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1785 * SHA256 is disabled) hash of the ticket.
1787 EVP_Digest(p
, ticklen
,
1788 s
->session
->session_id
, &s
->session
->session_id_length
,
1789 # ifndef OPENSSL_NO_SHA256
1790 EVP_sha256(), NULL
);
1797 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1802 int ssl3_get_cert_status(SSL
*s
)
1805 unsigned long resplen
;
1807 const unsigned char *p
;
1809 n
= s
->method
->ssl_get_message(s
,
1810 SSL3_ST_CR_CERT_STATUS_A
,
1811 SSL3_ST_CR_CERT_STATUS_B
,
1812 SSL3_MT_CERTIFICATE_STATUS
, 16384, &ok
);
1817 /* need at least status type + length */
1818 al
= SSL_AD_DECODE_ERROR
;
1819 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
1822 p
= (unsigned char *)s
->init_msg
;
1823 if (*p
++ != TLSEXT_STATUSTYPE_ocsp
) {
1824 al
= SSL_AD_DECODE_ERROR
;
1825 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
1829 if (resplen
+ 4 != (unsigned long)n
) {
1830 al
= SSL_AD_DECODE_ERROR
;
1831 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
1834 if (s
->tlsext_ocsp_resp
)
1835 OPENSSL_free(s
->tlsext_ocsp_resp
);
1836 s
->tlsext_ocsp_resp
= BUF_memdup(p
, resplen
);
1837 if (!s
->tlsext_ocsp_resp
) {
1838 al
= SSL_AD_INTERNAL_ERROR
;
1839 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
1842 s
->tlsext_ocsp_resplen
= resplen
;
1843 if (s
->ctx
->tlsext_status_cb
) {
1845 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
1847 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
1848 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_INVALID_STATUS_RESPONSE
);
1852 al
= SSL_AD_INTERNAL_ERROR
;
1853 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
1859 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1864 int ssl3_get_server_done(SSL
*s
)
1869 /* Second to last param should be very small, like 0 :-) */
1870 n
= s
->method
->ssl_get_message(s
,
1871 SSL3_ST_CR_SRVR_DONE_A
,
1872 SSL3_ST_CR_SRVR_DONE_B
,
1873 SSL3_MT_SERVER_DONE
, 30, &ok
);
1878 /* should contain no data */
1879 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1880 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
, SSL_R_LENGTH_MISMATCH
);
1887 int ssl3_send_client_key_exchange(SSL
*s
)
1889 unsigned char *p
, *d
;
1892 #ifndef OPENSSL_NO_RSA
1894 EVP_PKEY
*pkey
= NULL
;
1896 #ifndef OPENSSL_NO_KRB5
1898 #endif /* OPENSSL_NO_KRB5 */
1899 #ifndef OPENSSL_NO_ECDH
1900 EC_KEY
*clnt_ecdh
= NULL
;
1901 const EC_POINT
*srvr_ecpoint
= NULL
;
1902 EVP_PKEY
*srvr_pub_pkey
= NULL
;
1903 unsigned char *encodedPoint
= NULL
;
1904 int encoded_pt_len
= 0;
1905 BN_CTX
*bn_ctx
= NULL
;
1908 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
) {
1909 d
= (unsigned char *)s
->init_buf
->data
;
1912 l
= s
->s3
->tmp
.new_cipher
->algorithms
;
1914 /* Fool emacs indentation */
1917 #ifndef OPENSSL_NO_RSA
1918 else if (l
& SSL_kRSA
) {
1920 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
1922 if (s
->session
->sess_cert
== NULL
) {
1924 * We should always have a server certificate with SSL_kRSA.
1926 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1927 ERR_R_INTERNAL_ERROR
);
1931 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
1932 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
1935 X509_get_pubkey(s
->session
->
1936 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].
1938 if ((pkey
== NULL
) || (pkey
->type
!= EVP_PKEY_RSA
)
1939 || (pkey
->pkey
.rsa
== NULL
)) {
1940 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1941 ERR_R_INTERNAL_ERROR
);
1944 rsa
= pkey
->pkey
.rsa
;
1945 EVP_PKEY_free(pkey
);
1948 tmp_buf
[0] = s
->client_version
>> 8;
1949 tmp_buf
[1] = s
->client_version
& 0xff;
1950 if (RAND_bytes(&(tmp_buf
[2]), sizeof tmp_buf
- 2) <= 0)
1953 s
->session
->master_key_length
= sizeof tmp_buf
;
1956 /* Fix buf for TLS and beyond */
1957 if (s
->version
> SSL3_VERSION
)
1959 n
= RSA_public_encrypt(sizeof tmp_buf
,
1960 tmp_buf
, p
, rsa
, RSA_PKCS1_PADDING
);
1962 if (s
->options
& SSL_OP_PKCS1_CHECK_1
)
1964 if (s
->options
& SSL_OP_PKCS1_CHECK_2
)
1968 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1969 SSL_R_BAD_RSA_ENCRYPT
);
1973 /* Fix buf for TLS and beyond */
1974 if (s
->version
> SSL3_VERSION
) {
1979 s
->session
->master_key_length
=
1980 s
->method
->ssl3_enc
->generate_master_secret(s
,
1982 session
->master_key
,
1985 OPENSSL_cleanse(tmp_buf
, sizeof tmp_buf
);
1988 #ifndef OPENSSL_NO_KRB5
1989 else if (l
& SSL_kKRB5
) {
1990 krb5_error_code krb5rc
;
1991 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
1992 /* krb5_data krb5_ap_req; */
1993 krb5_data
*enc_ticket
;
1994 krb5_data authenticator
, *authp
= NULL
;
1995 EVP_CIPHER_CTX ciph_ctx
;
1996 EVP_CIPHER
*enc
= NULL
;
1997 unsigned char iv
[EVP_MAX_IV_LENGTH
];
1998 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
1999 unsigned char epms
[SSL_MAX_MASTER_KEY_LENGTH
+ EVP_MAX_IV_LENGTH
];
2000 int padl
, outl
= sizeof(epms
);
2002 EVP_CIPHER_CTX_init(&ciph_ctx
);
2005 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2007 # endif /* KSSL_DEBUG */
2010 # ifdef KRB5SENDAUTH
2012 authp
= &authenticator
;
2013 # endif /* KRB5SENDAUTH */
2015 krb5rc
= kssl_cget_tkt(kssl_ctx
, &enc_ticket
, authp
, &kssl_err
);
2016 enc
= kssl_map_enc(kssl_ctx
->enctype
);
2021 printf("kssl_cget_tkt rtn %d\n", krb5rc
);
2022 if (krb5rc
&& kssl_err
.text
)
2023 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err
.text
);
2025 # endif /* KSSL_DEBUG */
2028 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2029 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, kssl_err
.reason
);
2034 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2035 * in place of RFC 2712 KerberosWrapper, as in:
2037 * Send ticket (copy to *p, set n = length)
2038 * n = krb5_ap_req.length;
2039 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2040 * if (krb5_ap_req.data)
2041 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2043 * Now using real RFC 2712 KerberosWrapper
2044 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2045 * Note: 2712 "opaque" types are here replaced
2046 * with a 2-byte length followed by the value.
2048 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2049 * Where "xx xx" = length bytes. Shown here with
2050 * optional authenticator omitted.
2053 /* KerberosWrapper.Ticket */
2054 s2n(enc_ticket
->length
, p
);
2055 memcpy(p
, enc_ticket
->data
, enc_ticket
->length
);
2056 p
+= enc_ticket
->length
;
2057 n
= enc_ticket
->length
+ 2;
2059 /* KerberosWrapper.Authenticator */
2060 if (authp
&& authp
->length
) {
2061 s2n(authp
->length
, p
);
2062 memcpy(p
, authp
->data
, authp
->length
);
2064 n
+= authp
->length
+ 2;
2070 s2n(0, p
); /* null authenticator length */
2074 tmp_buf
[0] = s
->client_version
>> 8;
2075 tmp_buf
[1] = s
->client_version
& 0xff;
2076 if (RAND_bytes(&(tmp_buf
[2]), sizeof tmp_buf
- 2) <= 0)
2080 * 20010420 VRS. Tried it this way; failed.
2081 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2082 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2083 * kssl_ctx->length);
2084 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2087 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2088 EVP_EncryptInit_ex(&ciph_ctx
, enc
, NULL
, kssl_ctx
->key
, iv
);
2089 EVP_EncryptUpdate(&ciph_ctx
, epms
, &outl
, tmp_buf
,
2091 EVP_EncryptFinal_ex(&ciph_ctx
, &(epms
[outl
]), &padl
);
2093 if (outl
> sizeof epms
) {
2094 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2095 ERR_R_INTERNAL_ERROR
);
2098 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2100 /* KerberosWrapper.EncryptedPreMasterSecret */
2102 memcpy(p
, epms
, outl
);
2106 s
->session
->master_key_length
=
2107 s
->method
->ssl3_enc
->generate_master_secret(s
,
2109 session
->master_key
,
2113 OPENSSL_cleanse(tmp_buf
, sizeof tmp_buf
);
2114 OPENSSL_cleanse(epms
, outl
);
2117 #ifndef OPENSSL_NO_DH
2118 else if (l
& (SSL_kEDH
| SSL_kDHr
| SSL_kDHd
)) {
2119 DH
*dh_srvr
, *dh_clnt
;
2121 if (s
->session
->sess_cert
== NULL
) {
2122 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2123 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2124 SSL_R_UNEXPECTED_MESSAGE
);
2128 if (s
->session
->sess_cert
->peer_dh_tmp
!= NULL
)
2129 dh_srvr
= s
->session
->sess_cert
->peer_dh_tmp
;
2131 /* we get them from the cert */
2132 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2133 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2134 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
2138 /* generate a new random key */
2139 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
2140 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2143 if (!DH_generate_key(dh_clnt
)) {
2144 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2149 * use the 'p' output buffer for the DH key, but make sure to
2150 * clear it out afterwards
2153 n
= DH_compute_key(p
, dh_srvr
->pub_key
, dh_clnt
);
2156 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2160 /* generate master key from the result */
2161 s
->session
->master_key_length
=
2162 s
->method
->ssl3_enc
->generate_master_secret(s
,
2164 session
->master_key
,
2169 /* send off the data */
2170 n
= BN_num_bytes(dh_clnt
->pub_key
);
2172 BN_bn2bin(dh_clnt
->pub_key
, p
);
2177 /* perhaps clean things up a bit EAY EAY EAY EAY */
2181 #ifndef OPENSSL_NO_ECDH
2182 else if ((l
& SSL_kECDH
) || (l
& SSL_kECDHE
)) {
2183 const EC_GROUP
*srvr_group
= NULL
;
2185 int ecdh_clnt_cert
= 0;
2188 if (s
->session
->sess_cert
== NULL
) {
2189 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2190 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2191 SSL_R_UNEXPECTED_MESSAGE
);
2196 * Did we send out the client's ECDH share for use in premaster
2197 * computation as part of client certificate? If so, set
2198 * ecdh_clnt_cert to 1.
2200 if ((l
& SSL_kECDH
) && (s
->cert
!= NULL
)) {
2202 * XXX: For now, we do not support client
2203 * authentication using ECDH certificates.
2204 * To add such support, one needs to add
2205 * code that checks for appropriate
2206 * conditions and sets ecdh_clnt_cert to 1.
2207 * For example, the cert have an ECC
2208 * key on the same curve as the server's
2209 * and the key should be authorized for
2212 * One also needs to add code in ssl3_connect
2213 * to skip sending the certificate verify
2216 * if ((s->cert->key->privatekey != NULL) &&
2217 * (s->cert->key->privatekey->type ==
2218 * EVP_PKEY_EC) && ...)
2219 * ecdh_clnt_cert = 1;
2223 if (s
->session
->sess_cert
->peer_ecdh_tmp
!= NULL
) {
2224 tkey
= s
->session
->sess_cert
->peer_ecdh_tmp
;
2226 /* Get the Server Public Key from Cert */
2228 X509_get_pubkey(s
->session
->
2229 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
2230 if ((srvr_pub_pkey
== NULL
)
2231 || (srvr_pub_pkey
->type
!= EVP_PKEY_EC
)
2232 || (srvr_pub_pkey
->pkey
.ec
== NULL
)) {
2233 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2234 ERR_R_INTERNAL_ERROR
);
2238 tkey
= srvr_pub_pkey
->pkey
.ec
;
2241 srvr_group
= EC_KEY_get0_group(tkey
);
2242 srvr_ecpoint
= EC_KEY_get0_public_key(tkey
);
2244 if ((srvr_group
== NULL
) || (srvr_ecpoint
== NULL
)) {
2245 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2246 ERR_R_INTERNAL_ERROR
);
2250 if ((clnt_ecdh
= EC_KEY_new()) == NULL
) {
2251 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2252 ERR_R_MALLOC_FAILURE
);
2256 if (!EC_KEY_set_group(clnt_ecdh
, srvr_group
)) {
2257 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2260 if (ecdh_clnt_cert
) {
2262 * Reuse key info from our certificate We only need our
2263 * private key to perform the ECDH computation.
2265 const BIGNUM
*priv_key
;
2266 tkey
= s
->cert
->key
->privatekey
->pkey
.ec
;
2267 priv_key
= EC_KEY_get0_private_key(tkey
);
2268 if (priv_key
== NULL
) {
2269 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2270 ERR_R_MALLOC_FAILURE
);
2273 if (!EC_KEY_set_private_key(clnt_ecdh
, priv_key
)) {
2274 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2278 /* Generate a new ECDH key pair */
2279 if (!(EC_KEY_generate_key(clnt_ecdh
))) {
2280 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2287 * use the 'p' output buffer for the ECDH key, but make sure to
2288 * clear it out afterwards
2291 field_size
= EC_GROUP_get_degree(srvr_group
);
2292 if (field_size
<= 0) {
2293 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2296 n
= ECDH_compute_key(p
, (field_size
+ 7) / 8, srvr_ecpoint
,
2299 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2303 /* generate master key from the result */
2304 s
->session
->master_key_length
=
2305 s
->method
->ssl3_enc
->generate_master_secret(s
,
2307 session
->master_key
,
2310 memset(p
, 0, n
); /* clean up */
2312 if (ecdh_clnt_cert
) {
2313 /* Send empty client key exch message */
2317 * First check the size of encoding and allocate memory
2321 EC_POINT_point2oct(srvr_group
,
2322 EC_KEY_get0_public_key(clnt_ecdh
),
2323 POINT_CONVERSION_UNCOMPRESSED
,
2326 encodedPoint
= (unsigned char *)
2327 OPENSSL_malloc(encoded_pt_len
* sizeof(unsigned char));
2328 bn_ctx
= BN_CTX_new();
2329 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
)) {
2330 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2331 ERR_R_MALLOC_FAILURE
);
2335 /* Encode the public key */
2336 n
= EC_POINT_point2oct(srvr_group
,
2337 EC_KEY_get0_public_key(clnt_ecdh
),
2338 POINT_CONVERSION_UNCOMPRESSED
,
2339 encodedPoint
, encoded_pt_len
, bn_ctx
);
2341 *p
= n
; /* length of encoded point */
2342 /* Encoded point will be copied here */
2344 /* copy the point */
2345 memcpy((unsigned char *)p
, encodedPoint
, n
);
2346 /* increment n to account for length field */
2350 /* Free allocated memory */
2351 BN_CTX_free(bn_ctx
);
2352 if (encodedPoint
!= NULL
)
2353 OPENSSL_free(encodedPoint
);
2354 if (clnt_ecdh
!= NULL
)
2355 EC_KEY_free(clnt_ecdh
);
2356 EVP_PKEY_free(srvr_pub_pkey
);
2358 #endif /* !OPENSSL_NO_ECDH */
2360 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2361 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2365 *(d
++) = SSL3_MT_CLIENT_KEY_EXCHANGE
;
2368 s
->state
= SSL3_ST_CW_KEY_EXCH_B
;
2369 /* number of bytes to write */
2370 s
->init_num
= n
+ 4;
2374 /* SSL3_ST_CW_KEY_EXCH_B */
2375 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
2377 #ifndef OPENSSL_NO_ECDH
2378 BN_CTX_free(bn_ctx
);
2379 if (encodedPoint
!= NULL
)
2380 OPENSSL_free(encodedPoint
);
2381 if (clnt_ecdh
!= NULL
)
2382 EC_KEY_free(clnt_ecdh
);
2383 EVP_PKEY_free(srvr_pub_pkey
);
2388 int ssl3_send_client_verify(SSL
*s
)
2390 unsigned char *p
, *d
;
2391 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
2393 #ifndef OPENSSL_NO_RSA
2397 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
2401 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
) {
2402 d
= (unsigned char *)s
->init_buf
->data
;
2404 pkey
= s
->cert
->key
->privatekey
;
2406 s
->method
->ssl3_enc
->cert_verify_mac(s
, &(s
->s3
->finish_dgst2
),
2407 &(data
[MD5_DIGEST_LENGTH
]));
2409 #ifndef OPENSSL_NO_RSA
2410 if (pkey
->type
== EVP_PKEY_RSA
) {
2411 s
->method
->ssl3_enc
->cert_verify_mac(s
,
2412 &(s
->s3
->finish_dgst1
),
2415 (NID_md5_sha1
, data
, MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
,
2416 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
2417 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_RSA_LIB
);
2424 #ifndef OPENSSL_NO_DSA
2425 if (pkey
->type
== EVP_PKEY_DSA
) {
2426 if (!DSA_sign(pkey
->save_type
,
2427 &(data
[MD5_DIGEST_LENGTH
]),
2428 SHA_DIGEST_LENGTH
, &(p
[2]),
2429 (unsigned int *)&j
, pkey
->pkey
.dsa
)) {
2430 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_DSA_LIB
);
2437 #ifndef OPENSSL_NO_ECDSA
2438 if (pkey
->type
== EVP_PKEY_EC
) {
2439 if (!ECDSA_sign(pkey
->save_type
,
2440 &(data
[MD5_DIGEST_LENGTH
]),
2441 SHA_DIGEST_LENGTH
, &(p
[2]),
2442 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
2443 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_ECDSA_LIB
);
2451 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
2454 *(d
++) = SSL3_MT_CERTIFICATE_VERIFY
;
2457 s
->state
= SSL3_ST_CW_CERT_VRFY_B
;
2458 s
->init_num
= (int)n
+ 4;
2461 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
2466 int ssl3_send_client_certificate(SSL
*s
)
2469 EVP_PKEY
*pkey
= NULL
;
2473 if (s
->state
== SSL3_ST_CW_CERT_A
) {
2474 if ((s
->cert
== NULL
) ||
2475 (s
->cert
->key
->x509
== NULL
) ||
2476 (s
->cert
->key
->privatekey
== NULL
))
2477 s
->state
= SSL3_ST_CW_CERT_B
;
2479 s
->state
= SSL3_ST_CW_CERT_C
;
2482 /* We need to get a client cert */
2483 if (s
->state
== SSL3_ST_CW_CERT_B
) {
2485 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2486 * return(-1); We then get retied later
2489 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2491 s
->rwstate
= SSL_X509_LOOKUP
;
2494 s
->rwstate
= SSL_NOTHING
;
2495 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2496 s
->state
= SSL3_ST_CW_CERT_B
;
2497 if (!SSL_use_certificate(s
, x509
) || !SSL_use_PrivateKey(s
, pkey
))
2499 } else if (i
== 1) {
2501 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,
2502 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2508 EVP_PKEY_free(pkey
);
2510 if (s
->version
== SSL3_VERSION
) {
2511 s
->s3
->tmp
.cert_req
= 0;
2512 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_CERTIFICATE
);
2515 s
->s3
->tmp
.cert_req
= 2;
2519 /* Ok, we have a cert */
2520 s
->state
= SSL3_ST_CW_CERT_C
;
2523 if (s
->state
== SSL3_ST_CW_CERT_C
) {
2524 s
->state
= SSL3_ST_CW_CERT_D
;
2525 l
= ssl3_output_cert_chain(s
,
2526 (s
->s3
->tmp
.cert_req
==
2527 2) ? NULL
: s
->cert
->key
->x509
);
2528 s
->init_num
= (int)l
;
2531 /* SSL3_ST_CW_CERT_D */
2532 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
2535 #define has_bits(i,m) (((i)&(m)) == (m))
2537 int ssl3_check_cert_and_algorithm(SSL
*s
)
2541 EVP_PKEY
*pkey
= NULL
;
2543 #ifndef OPENSSL_NO_RSA
2546 #ifndef OPENSSL_NO_DH
2550 sc
= s
->session
->sess_cert
;
2552 algs
= s
->s3
->tmp
.new_cipher
->algorithms
;
2554 /* we don't have a certificate */
2555 if (algs
& (SSL_aDH
| SSL_aNULL
| SSL_aKRB5
))
2559 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
2562 #ifndef OPENSSL_NO_RSA
2563 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
2565 #ifndef OPENSSL_NO_DH
2566 dh
= s
->session
->sess_cert
->peer_dh_tmp
;
2569 /* This is the passed certificate */
2571 idx
= sc
->peer_cert_type
;
2572 #ifndef OPENSSL_NO_ECDH
2573 if (idx
== SSL_PKEY_ECC
) {
2574 if (check_srvr_ecc_cert_and_alg(sc
->peer_pkeys
[idx
].x509
,
2575 s
->s3
->tmp
.new_cipher
) == 0) {
2577 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_BAD_ECC_CERT
);
2584 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
2585 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
2586 EVP_PKEY_free(pkey
);
2588 /* Check that we have a certificate if we require one */
2589 if ((algs
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
| EVP_PKT_SIGN
)) {
2590 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2591 SSL_R_MISSING_RSA_SIGNING_CERT
);
2594 #ifndef OPENSSL_NO_DSA
2595 else if ((algs
& SSL_aDSS
) && !has_bits(i
, EVP_PK_DSA
| EVP_PKT_SIGN
)) {
2596 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2597 SSL_R_MISSING_DSA_SIGNING_CERT
);
2601 #ifndef OPENSSL_NO_RSA
2602 if ((algs
& SSL_kRSA
) &&
2603 !(has_bits(i
, EVP_PK_RSA
| EVP_PKT_ENC
) || (rsa
!= NULL
))) {
2604 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2605 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2609 #ifndef OPENSSL_NO_DH
2610 if ((algs
& SSL_kEDH
) &&
2611 !(has_bits(i
, EVP_PK_DH
| EVP_PKT_EXCH
) || (dh
!= NULL
))) {
2612 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_MISSING_DH_KEY
);
2614 } else if ((algs
& SSL_kDHr
) && !has_bits(i
, EVP_PK_DH
| EVP_PKS_RSA
)) {
2615 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2616 SSL_R_MISSING_DH_RSA_CERT
);
2619 # ifndef OPENSSL_NO_DSA
2620 else if ((algs
& SSL_kDHd
) && !has_bits(i
, EVP_PK_DH
| EVP_PKS_DSA
)) {
2621 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2622 SSL_R_MISSING_DH_DSA_CERT
);
2628 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && !has_bits(i
, EVP_PKT_EXP
)) {
2629 #ifndef OPENSSL_NO_RSA
2630 if (algs
& SSL_kRSA
) {
2632 || RSA_size(rsa
) * 8 >
2633 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
2634 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2635 SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
2640 #ifndef OPENSSL_NO_DH
2641 if (algs
& (SSL_kEDH
| SSL_kDHr
| SSL_kDHd
)) {
2643 || DH_size(dh
) * 8 >
2644 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
2645 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2646 SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
2652 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2653 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2659 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2664 #ifndef OPENSSL_NO_ECDH
2665 /* This is the complement of nid2curve_id in s3_srvr.c. */
2666 static int curve_id2nid(int curve_id
)
2669 * ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) (no changes
2670 * in draft-ietf-tls-ecc-03.txt [June 2003])
2672 static int nid_list
[26] = {
2674 NID_sect163k1
, /* sect163k1 (1) */
2675 NID_sect163r1
, /* sect163r1 (2) */
2676 NID_sect163r2
, /* sect163r2 (3) */
2677 NID_sect193r1
, /* sect193r1 (4) */
2678 NID_sect193r2
, /* sect193r2 (5) */
2679 NID_sect233k1
, /* sect233k1 (6) */
2680 NID_sect233r1
, /* sect233r1 (7) */
2681 NID_sect239k1
, /* sect239k1 (8) */
2682 NID_sect283k1
, /* sect283k1 (9) */
2683 NID_sect283r1
, /* sect283r1 (10) */
2684 NID_sect409k1
, /* sect409k1 (11) */
2685 NID_sect409r1
, /* sect409r1 (12) */
2686 NID_sect571k1
, /* sect571k1 (13) */
2687 NID_sect571r1
, /* sect571r1 (14) */
2688 NID_secp160k1
, /* secp160k1 (15) */
2689 NID_secp160r1
, /* secp160r1 (16) */
2690 NID_secp160r2
, /* secp160r2 (17) */
2691 NID_secp192k1
, /* secp192k1 (18) */
2692 NID_X9_62_prime192v1
, /* secp192r1 (19) */
2693 NID_secp224k1
, /* secp224k1 (20) */
2694 NID_secp224r1
, /* secp224r1 (21) */
2695 NID_secp256k1
, /* secp256k1 (22) */
2696 NID_X9_62_prime256v1
, /* secp256r1 (23) */
2697 NID_secp384r1
, /* secp384r1 (24) */
2698 NID_secp521r1
/* secp521r1 (25) */
2701 if ((curve_id
< 1) || (curve_id
> 25))
2704 return nid_list
[curve_id
];
2709 * Check to see if handshake is full or resumed. Usually this is just a case
2710 * of checking to see if a cache hit has occurred. In the case of session
2711 * tickets we have to check the next message to be sure.
2714 #ifndef OPENSSL_NO_TLSEXT
2715 int ssl3_check_finished(SSL
*s
)
2720 * If we have no ticket or session ID is non-zero length (a match of a
2721 * non-zero session length would never reach here) it cannot be a resumed
2724 if (!s
->session
->tlsext_tick
|| s
->session
->session_id_length
)
2727 * this function is called when we really expect a Certificate message,
2728 * so permit appropriate message length
2730 n
= s
->method
->ssl_get_message(s
,
2733 -1, s
->max_cert_list
, &ok
);
2736 s
->s3
->tmp
.reuse_message
= 1;
2737 if ((s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
)
2738 || (s
->s3
->tmp
.message_type
== SSL3_MT_NEWSESSION_TICKET
))
2745 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
2748 #ifndef OPENSSL_NO_ENGINE
2749 if (s
->ctx
->client_cert_engine
) {
2750 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
2751 SSL_get_client_CA_list(s
),
2752 px509
, ppkey
, NULL
, NULL
, NULL
);
2757 if (s
->ctx
->client_cert_cb
)
2758 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);