2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
152 #include "ssl_locl.h"
153 #include "kssl_lcl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
160 # include <openssl/fips.h>
162 #ifndef OPENSSL_NO_DH
163 # include <openssl/dh.h>
165 #include <openssl/bn.h>
166 #ifndef OPENSSL_NO_ENGINE
167 # include <openssl/engine.h>
170 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
);
171 #ifndef OPENSSL_NO_TLSEXT
172 static int ssl3_check_finished(SSL
*s
);
175 #ifndef OPENSSL_NO_SSL3_METHOD
176 static const SSL_METHOD
*ssl3_get_client_method(int ver
)
178 if (ver
== SSL3_VERSION
)
179 return (SSLv3_client_method());
184 IMPLEMENT_ssl3_meth_func(SSLv3_client_method
,
185 ssl_undefined_function
,
186 ssl3_connect
, ssl3_get_client_method
)
188 int ssl3_connect(SSL
*s
)
191 unsigned long Time
= (unsigned long)time(NULL
);
192 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
194 int new_state
, state
, skip
= 0;
196 RAND_add(&Time
, sizeof(Time
), 0);
200 if (s
->info_callback
!= NULL
)
201 cb
= s
->info_callback
;
202 else if (s
->ctx
->info_callback
!= NULL
)
203 cb
= s
->ctx
->info_callback
;
206 if (!SSL_in_init(s
) || SSL_in_before(s
))
209 #ifndef OPENSSL_NO_HEARTBEATS
211 * If we're awaiting a HeartbeatResponse, pretend we already got and
212 * don't await it anymore, because Heartbeats don't make sense during
215 if (s
->tlsext_hb_pending
) {
216 s
->tlsext_hb_pending
= 0;
225 case SSL_ST_RENEGOTIATE
:
227 s
->state
= SSL_ST_CONNECT
;
228 s
->ctx
->stats
.sess_connect_renegotiate
++;
232 case SSL_ST_BEFORE
| SSL_ST_CONNECT
:
233 case SSL_ST_OK
| SSL_ST_CONNECT
:
237 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
239 if ((s
->version
& 0xff00) != 0x0300) {
240 SSLerr(SSL_F_SSL3_CONNECT
, ERR_R_INTERNAL_ERROR
);
241 s
->state
= SSL_ST_ERR
;
246 /* s->version=SSL3_VERSION; */
247 s
->type
= SSL_ST_CONNECT
;
249 if (s
->init_buf
== NULL
) {
250 if ((buf
= BUF_MEM_new()) == NULL
) {
252 s
->state
= SSL_ST_ERR
;
255 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
257 s
->state
= SSL_ST_ERR
;
264 if (!ssl3_setup_buffers(s
)) {
269 /* setup buffing BIO */
270 if (!ssl_init_wbio_buffer(s
, 0)) {
272 s
->state
= SSL_ST_ERR
;
276 /* don't push the buffering BIO quite yet */
278 ssl3_init_finished_mac(s
);
280 s
->state
= SSL3_ST_CW_CLNT_HELLO_A
;
281 s
->ctx
->stats
.sess_connect
++;
283 s
->s3
->flags
&= ~SSL3_FLAGS_CCS_OK
;
285 * Should have been reset by ssl3_get_finished, too.
287 s
->s3
->change_cipher_spec
= 0;
290 case SSL3_ST_CW_CLNT_HELLO_A
:
291 case SSL3_ST_CW_CLNT_HELLO_B
:
294 ret
= ssl3_client_hello(s
);
297 s
->state
= SSL3_ST_CR_SRVR_HELLO_A
;
300 /* turn on buffering for the next lot of output */
301 if (s
->bbio
!= s
->wbio
)
302 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
306 case SSL3_ST_CR_SRVR_HELLO_A
:
307 case SSL3_ST_CR_SRVR_HELLO_B
:
308 ret
= ssl3_get_server_hello(s
);
313 s
->state
= SSL3_ST_CR_FINISHED_A
;
314 #ifndef OPENSSL_NO_TLSEXT
315 if (s
->tlsext_ticket_expected
) {
316 /* receive renewed session ticket */
317 s
->state
= SSL3_ST_CR_SESSION_TICKET_A
;
321 s
->state
= SSL3_ST_CR_CERT_A
;
325 case SSL3_ST_CR_CERT_A
:
326 case SSL3_ST_CR_CERT_B
:
327 #ifndef OPENSSL_NO_TLSEXT
328 /* Noop (ret = 0) for everything but EAP-FAST. */
329 ret
= ssl3_check_finished(s
);
334 s
->state
= SSL3_ST_CR_FINISHED_A
;
339 /* Check if it is anon DH/ECDH, SRP auth */
343 new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
))
344 && !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
)) {
345 ret
= ssl3_get_server_certificate(s
);
348 #ifndef OPENSSL_NO_TLSEXT
349 if (s
->tlsext_status_expected
)
350 s
->state
= SSL3_ST_CR_CERT_STATUS_A
;
352 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
355 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
361 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
366 case SSL3_ST_CR_KEY_EXCH_A
:
367 case SSL3_ST_CR_KEY_EXCH_B
:
368 ret
= ssl3_get_key_exchange(s
);
371 s
->state
= SSL3_ST_CR_CERT_REQ_A
;
375 * at this point we check that we have the required stuff from
378 if (!ssl3_check_cert_and_algorithm(s
)) {
380 s
->state
= SSL_ST_ERR
;
385 case SSL3_ST_CR_CERT_REQ_A
:
386 case SSL3_ST_CR_CERT_REQ_B
:
387 ret
= ssl3_get_certificate_request(s
);
390 s
->state
= SSL3_ST_CR_SRVR_DONE_A
;
394 case SSL3_ST_CR_SRVR_DONE_A
:
395 case SSL3_ST_CR_SRVR_DONE_B
:
396 ret
= ssl3_get_server_done(s
);
399 #ifndef OPENSSL_NO_SRP
400 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
401 if ((ret
= SRP_Calc_A_param(s
)) <= 0) {
402 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_SRP_A_CALC
);
403 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
404 s
->state
= SSL_ST_ERR
;
409 if (s
->s3
->tmp
.cert_req
)
410 s
->state
= SSL3_ST_CW_CERT_A
;
412 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
417 case SSL3_ST_CW_CERT_A
:
418 case SSL3_ST_CW_CERT_B
:
419 case SSL3_ST_CW_CERT_C
:
420 case SSL3_ST_CW_CERT_D
:
421 ret
= ssl3_send_client_certificate(s
);
424 s
->state
= SSL3_ST_CW_KEY_EXCH_A
;
428 case SSL3_ST_CW_KEY_EXCH_A
:
429 case SSL3_ST_CW_KEY_EXCH_B
:
430 ret
= ssl3_send_client_key_exchange(s
);
434 * EAY EAY EAY need to check for DH fix cert sent back
437 * For TLS, cert_req is set to 2, so a cert chain of nothing is
438 * sent, but no verify packet is sent
441 * XXX: For now, we do not support client authentication in ECDH
442 * cipher suites with ECDH (rather than ECDSA) certificates. We
443 * need to skip the certificate verify message when client's
444 * ECDH public key is sent inside the client certificate.
446 if (s
->s3
->tmp
.cert_req
== 1) {
447 s
->state
= SSL3_ST_CW_CERT_VRFY_A
;
449 s
->state
= SSL3_ST_CW_CHANGE_A
;
451 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
452 s
->state
= SSL3_ST_CW_CHANGE_A
;
458 case SSL3_ST_CW_CERT_VRFY_A
:
459 case SSL3_ST_CW_CERT_VRFY_B
:
460 ret
= ssl3_send_client_verify(s
);
463 s
->state
= SSL3_ST_CW_CHANGE_A
;
467 case SSL3_ST_CW_CHANGE_A
:
468 case SSL3_ST_CW_CHANGE_B
:
469 ret
= ssl3_send_change_cipher_spec(s
,
471 SSL3_ST_CW_CHANGE_B
);
475 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
476 s
->state
= SSL3_ST_CW_FINISHED_A
;
478 if (s
->s3
->next_proto_neg_seen
)
479 s
->state
= SSL3_ST_CW_NEXT_PROTO_A
;
481 s
->state
= SSL3_ST_CW_FINISHED_A
;
485 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
486 #ifdef OPENSSL_NO_COMP
487 s
->session
->compress_meth
= 0;
489 if (s
->s3
->tmp
.new_compression
== NULL
)
490 s
->session
->compress_meth
= 0;
492 s
->session
->compress_meth
= s
->s3
->tmp
.new_compression
->id
;
494 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
496 s
->state
= SSL_ST_ERR
;
500 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
501 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
504 s
->state
= SSL_ST_ERR
;
510 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
511 case SSL3_ST_CW_NEXT_PROTO_A
:
512 case SSL3_ST_CW_NEXT_PROTO_B
:
513 ret
= ssl3_send_next_proto(s
);
516 s
->state
= SSL3_ST_CW_FINISHED_A
;
520 case SSL3_ST_CW_FINISHED_A
:
521 case SSL3_ST_CW_FINISHED_B
:
522 ret
= ssl3_send_finished(s
,
523 SSL3_ST_CW_FINISHED_A
,
524 SSL3_ST_CW_FINISHED_B
,
526 ssl3_enc
->client_finished_label
,
528 ssl3_enc
->client_finished_label_len
);
531 s
->state
= SSL3_ST_CW_FLUSH
;
534 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
536 s
->s3
->tmp
.next_state
= SSL_ST_OK
;
537 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
538 s
->state
= SSL_ST_OK
;
539 s
->s3
->flags
|= SSL3_FLAGS_POP_BUFFER
;
540 s
->s3
->delay_buf_pop_ret
= 0;
543 #ifndef OPENSSL_NO_TLSEXT
545 * Allow NewSessionTicket if ticket expected
547 if (s
->tlsext_ticket_expected
)
548 s
->s3
->tmp
.next_state
= SSL3_ST_CR_SESSION_TICKET_A
;
552 s
->s3
->tmp
.next_state
= SSL3_ST_CR_FINISHED_A
;
557 #ifndef OPENSSL_NO_TLSEXT
558 case SSL3_ST_CR_SESSION_TICKET_A
:
559 case SSL3_ST_CR_SESSION_TICKET_B
:
560 ret
= ssl3_get_new_session_ticket(s
);
563 s
->state
= SSL3_ST_CR_FINISHED_A
;
567 case SSL3_ST_CR_CERT_STATUS_A
:
568 case SSL3_ST_CR_CERT_STATUS_B
:
569 ret
= ssl3_get_cert_status(s
);
572 s
->state
= SSL3_ST_CR_KEY_EXCH_A
;
577 case SSL3_ST_CR_FINISHED_A
:
578 case SSL3_ST_CR_FINISHED_B
:
579 if (!s
->s3
->change_cipher_spec
)
580 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
581 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
582 SSL3_ST_CR_FINISHED_B
);
587 s
->state
= SSL3_ST_CW_CHANGE_A
;
589 s
->state
= SSL_ST_OK
;
593 case SSL3_ST_CW_FLUSH
:
594 s
->rwstate
= SSL_WRITING
;
595 if (BIO_flush(s
->wbio
) <= 0) {
599 s
->rwstate
= SSL_NOTHING
;
600 s
->state
= s
->s3
->tmp
.next_state
;
604 /* clean a few things up */
605 ssl3_cleanup_key_block(s
);
607 if (s
->init_buf
!= NULL
) {
608 BUF_MEM_free(s
->init_buf
);
613 * If we are not 'joining' the last two packets, remove the
616 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
617 ssl_free_wbio_buffer(s
);
618 /* else do it later in ssl3_write */
624 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
626 s
->ctx
->stats
.sess_hit
++;
630 s
->handshake_func
= ssl3_connect
;
631 s
->ctx
->stats
.sess_connect_good
++;
634 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
641 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_UNKNOWN_STATE
);
647 /* did we do anything */
648 if (!s
->s3
->tmp
.reuse_message
&& !skip
) {
650 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
654 if ((cb
!= NULL
) && (s
->state
!= state
)) {
655 new_state
= s
->state
;
657 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
658 s
->state
= new_state
;
668 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
672 int ssl3_client_hello(SSL
*s
)
675 unsigned char *p
, *d
;
678 #ifndef OPENSSL_NO_COMP
683 buf
= (unsigned char *)s
->init_buf
->data
;
684 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
) {
685 SSL_SESSION
*sess
= s
->session
;
686 if ((sess
== NULL
) || (sess
->ssl_version
!= s
->version
) ||
687 #ifdef OPENSSL_NO_TLSEXT
688 !sess
->session_id_length
||
691 * In the case of EAP-FAST, we can have a pre-shared
692 * "ticket" without a session ID.
694 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
696 (sess
->not_resumable
)) {
697 if (!ssl_get_new_session(s
, 0))
700 /* else use the pre-loaded session */
702 p
= s
->s3
->client_random
;
704 if (ssl_fill_hello_random(s
, 0, p
, SSL3_RANDOM_SIZE
) <= 0)
707 /* Do the message type and length last */
711 * version indicates the negotiated version: for example from
712 * an SSLv2/v3 compatible client hello). The client_version
713 * field is the maximum version we permit and it is also
714 * used in RSA encrypted premaster secrets. Some servers can
715 * choke if we initially report a higher version then
716 * renegotiate to a lower one in the premaster secret. This
717 * didn't happen with TLS 1.0 as most servers supported it
718 * but it can with TLS 1.1 or later if the server only supports
721 * Possible scenario with previous logic:
722 * 1. Client hello indicates TLS 1.2
723 * 2. Server hello says TLS 1.0
724 * 3. RSA encrypted premaster secret uses 1.2.
725 * 4. Handhaked proceeds using TLS 1.0.
726 * 5. Server sends hello request to renegotiate.
727 * 6. Client hello indicates TLS v1.0 as we now
728 * know that is maximum server supports.
729 * 7. Server chokes on RSA encrypted premaster secret
730 * containing version 1.0.
732 * For interoperability it should be OK to always use the
733 * maximum version we support in client hello and then rely
734 * on the checking of version to ensure the servers isn't
735 * being inconsistent: for example initially negotiating with
736 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
737 * client_version in client hello and not resetting it to
738 * the negotiated version.
741 *(p
++) = s
->version
>> 8;
742 *(p
++) = s
->version
& 0xff;
743 s
->client_version
= s
->version
;
745 *(p
++) = s
->client_version
>> 8;
746 *(p
++) = s
->client_version
& 0xff;
750 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
751 p
+= SSL3_RANDOM_SIZE
;
757 i
= s
->session
->session_id_length
;
760 if (i
> (int)sizeof(s
->session
->session_id
)) {
761 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
764 memcpy(p
, s
->session
->session_id
, i
);
768 /* Ciphers supported */
769 i
= ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &(p
[2]), 0);
771 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_NO_CIPHERS_AVAILABLE
);
774 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
776 * Some servers hang if client hello > 256 bytes as hack workaround
777 * chop number of supported ciphers to keep it well below this if we
780 if (TLS1_get_version(s
) >= TLS1_2_VERSION
781 && i
> OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
)
782 i
= OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
& ~1;
788 #ifdef OPENSSL_NO_COMP
792 if ((s
->options
& SSL_OP_NO_COMPRESSION
)
793 || !s
->ctx
->comp_methods
)
796 j
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
798 for (i
= 0; i
< j
; i
++) {
799 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, i
);
803 *(p
++) = 0; /* Add the NULL method */
805 #ifndef OPENSSL_NO_TLSEXT
807 if (ssl_prepare_clienthello_tlsext(s
) <= 0) {
808 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
812 ssl_add_clienthello_tlsext(s
, p
,
813 buf
+ SSL3_RT_MAX_PLAIN_LENGTH
)) ==
815 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
822 *(d
++) = SSL3_MT_CLIENT_HELLO
;
825 s
->state
= SSL3_ST_CW_CLNT_HELLO_B
;
826 /* number of bytes to write */
827 s
->init_num
= p
- buf
;
831 /* SSL3_ST_CW_CLNT_HELLO_B */
832 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
834 s
->state
= SSL_ST_ERR
;
838 int ssl3_get_server_hello(SSL
*s
)
840 STACK_OF(SSL_CIPHER
) *sk
;
842 unsigned char *p
, *d
;
846 #ifndef OPENSSL_NO_COMP
850 n
= s
->method
->ssl_get_message(s
,
851 SSL3_ST_CR_SRVR_HELLO_A
,
852 SSL3_ST_CR_SRVR_HELLO_B
, -1, 20000, &ok
);
857 if (SSL_version(s
) == DTLS1_VERSION
|| SSL_version(s
) == DTLS1_BAD_VER
) {
858 if (s
->s3
->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
859 if (s
->d1
->send_cookie
== 0) {
860 s
->s3
->tmp
.reuse_message
= 1;
862 } else { /* already sent a cookie */
864 al
= SSL_AD_UNEXPECTED_MESSAGE
;
865 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
871 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
) {
872 al
= SSL_AD_UNEXPECTED_MESSAGE
;
873 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_MESSAGE_TYPE
);
877 d
= p
= (unsigned char *)s
->init_msg
;
879 if ((p
[0] != (s
->version
>> 8)) || (p
[1] != (s
->version
& 0xff))) {
880 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_SSL_VERSION
);
881 s
->version
= (s
->version
& 0xff00) | p
[1];
882 al
= SSL_AD_PROTOCOL_VERSION
;
887 /* load the server hello data */
888 /* load the server random */
889 memcpy(s
->s3
->server_random
, p
, SSL3_RANDOM_SIZE
);
890 p
+= SSL3_RANDOM_SIZE
;
894 /* get the session-id */
897 if ((j
> sizeof s
->session
->session_id
) || (j
> SSL3_SESSION_ID_SIZE
)) {
898 al
= SSL_AD_ILLEGAL_PARAMETER
;
899 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
902 #ifndef OPENSSL_NO_TLSEXT
904 * Check if we can resume the session based on external pre-shared secret.
905 * EAP-FAST (RFC 4851) supports two types of session resumption.
906 * Resumption based on server-side state works with session IDs.
907 * Resumption based on pre-shared Protected Access Credentials (PACs)
908 * works by overriding the SessionTicket extension at the application
909 * layer, and does not send a session ID. (We do not know whether EAP-FAST
910 * servers would honour the session ID.) Therefore, the session ID alone
911 * is not a reliable indicator of session resumption, so we first check if
912 * we can resume, and later peek at the next handshake message to see if the
913 * server wants to resume.
915 if (s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
&&
916 s
->session
->tlsext_tick
) {
917 SSL_CIPHER
*pref_cipher
= NULL
;
918 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
919 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
920 &s
->session
->master_key_length
,
922 s
->tls_session_secret_cb_arg
)) {
923 s
->session
->cipher
= pref_cipher
?
924 pref_cipher
: ssl_get_cipher_by_char(s
, p
+ j
);
926 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
927 al
= SSL_AD_INTERNAL_ERROR
;
931 #endif /* OPENSSL_NO_TLSEXT */
933 if (j
!= 0 && j
== s
->session
->session_id_length
934 && memcmp(p
, s
->session
->session_id
, j
) == 0) {
935 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
936 || memcmp(s
->session
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
)) {
937 /* actually a client application bug */
938 al
= SSL_AD_ILLEGAL_PARAMETER
;
939 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
940 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
946 * If we were trying for session-id reuse but the server
947 * didn't echo the ID, make a new SSL_SESSION.
948 * In the case of EAP-FAST and PAC, we do not send a session ID,
949 * so the PAC-based session secret is always preserved. It'll be
950 * overwritten if the server refuses resumption.
952 if (s
->session
->session_id_length
> 0) {
953 if (!ssl_get_new_session(s
, 0)) {
954 al
= SSL_AD_INTERNAL_ERROR
;
958 s
->session
->session_id_length
= j
;
959 memcpy(s
->session
->session_id
, p
, j
); /* j could be 0 */
962 c
= ssl_get_cipher_by_char(s
, p
);
965 al
= SSL_AD_ILLEGAL_PARAMETER
;
966 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
969 /* TLS v1.2 only ciphersuites require v1.2 or later */
970 if ((c
->algorithm_ssl
& SSL_TLSV1_2
) &&
971 (TLS1_get_version(s
) < TLS1_2_VERSION
)) {
972 al
= SSL_AD_ILLEGAL_PARAMETER
;
973 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
976 #ifndef OPENSSL_NO_SRP
977 if (((c
->algorithm_mkey
& SSL_kSRP
) || (c
->algorithm_auth
& SSL_aSRP
)) &&
978 !(s
->srp_ctx
.srp_Mask
& SSL_kSRP
)) {
979 al
= SSL_AD_ILLEGAL_PARAMETER
;
980 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
983 #endif /* OPENSSL_NO_SRP */
984 p
+= ssl_put_cipher_by_char(s
, NULL
, NULL
);
986 sk
= ssl_get_ciphers_by_id(s
);
987 i
= sk_SSL_CIPHER_find(sk
, c
);
989 /* we did not say we would use this cipher */
990 al
= SSL_AD_ILLEGAL_PARAMETER
;
991 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
996 * Depending on the session caching (internal/external), the cipher
997 * and/or cipher_id values may not be set. Make sure that cipher_id is
998 * set and use it for comparison.
1000 if (s
->session
->cipher
)
1001 s
->session
->cipher_id
= s
->session
->cipher
->id
;
1002 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
1003 /* Workaround is now obsolete */
1005 if (!(s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
))
1008 al
= SSL_AD_ILLEGAL_PARAMETER
;
1009 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1010 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
1014 s
->s3
->tmp
.new_cipher
= c
;
1016 * Don't digest cached records if TLS v1.2: we may need them for client
1019 if (TLS1_get_version(s
) < TLS1_2_VERSION
1020 && !ssl3_digest_cached_records(s
)) {
1021 al
= SSL_AD_INTERNAL_ERROR
;
1024 /* lets get the compression algorithm */
1026 #ifdef OPENSSL_NO_COMP
1028 al
= SSL_AD_ILLEGAL_PARAMETER
;
1029 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1030 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1034 * If compression is disabled we'd better not try to resume a session
1035 * using compression.
1037 if (s
->session
->compress_meth
!= 0) {
1038 al
= SSL_AD_INTERNAL_ERROR
;
1039 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1044 if (s
->hit
&& j
!= s
->session
->compress_meth
) {
1045 al
= SSL_AD_ILLEGAL_PARAMETER
;
1046 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1047 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED
);
1052 else if (s
->options
& SSL_OP_NO_COMPRESSION
) {
1053 al
= SSL_AD_ILLEGAL_PARAMETER
;
1054 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_COMPRESSION_DISABLED
);
1057 comp
= ssl3_comp_find(s
->ctx
->comp_methods
, j
);
1059 if ((j
!= 0) && (comp
== NULL
)) {
1060 al
= SSL_AD_ILLEGAL_PARAMETER
;
1061 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,
1062 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1065 s
->s3
->tmp
.new_compression
= comp
;
1069 #ifndef OPENSSL_NO_TLSEXT
1070 /* TLS extensions */
1071 if (s
->version
>= SSL3_VERSION
) {
1072 if (!ssl_parse_serverhello_tlsext(s
, &p
, d
, n
, &al
)) {
1073 /* 'al' set by ssl_parse_serverhello_tlsext */
1074 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_PARSE_TLSEXT
);
1077 if (ssl_check_serverhello_tlsext(s
) <= 0) {
1078 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_SERVERHELLO_TLSEXT
);
1085 /* wrong packet length */
1086 al
= SSL_AD_DECODE_ERROR
;
1087 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_BAD_PACKET_LENGTH
);
1093 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1095 s
->state
= SSL_ST_ERR
;
1099 int ssl3_get_server_certificate(SSL
*s
)
1101 int al
, i
, ok
, ret
= -1;
1102 unsigned long n
, nc
, llen
, l
;
1104 const unsigned char *q
, *p
;
1106 STACK_OF(X509
) *sk
= NULL
;
1108 EVP_PKEY
*pkey
= NULL
;
1109 int need_cert
= 1; /* VRS: 0=> will allow null cert if auth ==
1112 n
= s
->method
->ssl_get_message(s
,
1115 -1, s
->max_cert_list
, &ok
);
1120 if ((s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) ||
1121 ((s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
) &&
1122 (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
))) {
1123 s
->s3
->tmp
.reuse_message
= 1;
1127 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
) {
1128 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1129 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_BAD_MESSAGE_TYPE
);
1132 p
= d
= (unsigned char *)s
->init_msg
;
1134 if ((sk
= sk_X509_new_null()) == NULL
) {
1135 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1140 if (llen
+ 3 != n
) {
1141 al
= SSL_AD_DECODE_ERROR
;
1142 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
1145 for (nc
= 0; nc
< llen
;) {
1147 if ((l
+ nc
+ 3) > llen
) {
1148 al
= SSL_AD_DECODE_ERROR
;
1149 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1150 SSL_R_CERT_LENGTH_MISMATCH
);
1155 x
= d2i_X509(NULL
, &q
, l
);
1157 al
= SSL_AD_BAD_CERTIFICATE
;
1158 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_ASN1_LIB
);
1162 al
= SSL_AD_DECODE_ERROR
;
1163 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1164 SSL_R_CERT_LENGTH_MISMATCH
);
1167 if (!sk_X509_push(sk
, x
)) {
1168 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1176 i
= ssl_verify_cert_chain(s
, sk
);
1177 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)
1178 #ifndef OPENSSL_NO_KRB5
1179 && !((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
) &&
1180 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
))
1181 #endif /* OPENSSL_NO_KRB5 */
1183 al
= ssl_verify_alarm_type(s
->verify_result
);
1184 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1185 SSL_R_CERTIFICATE_VERIFY_FAILED
);
1188 ERR_clear_error(); /* but we keep s->verify_result */
1190 sc
= ssl_sess_cert_new();
1194 if (s
->session
->sess_cert
)
1195 ssl_sess_cert_free(s
->session
->sess_cert
);
1196 s
->session
->sess_cert
= sc
;
1198 sc
->cert_chain
= sk
;
1200 * Inconsistency alert: cert_chain does include the peer's certificate,
1201 * which we don't include in s3_srvr.c
1203 x
= sk_X509_value(sk
, 0);
1206 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1209 pkey
= X509_get_pubkey(x
);
1211 /* VRS: allow null cert if auth == KRB5 */
1212 need_cert
= ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
) &&
1213 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
))
1217 fprintf(stderr
, "pkey,x = %p, %p\n", pkey
, x
);
1218 fprintf(stderr
, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x
, pkey
));
1219 fprintf(stderr
, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1220 s
->s3
->tmp
.new_cipher
->name
,
1221 s
->s3
->tmp
.new_cipher
->algorithm_mkey
,
1222 s
->s3
->tmp
.new_cipher
->algorithm_auth
, need_cert
);
1223 #endif /* KSSL_DEBUG */
1225 if (need_cert
&& ((pkey
== NULL
) || EVP_PKEY_missing_parameters(pkey
))) {
1228 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1229 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1233 i
= ssl_cert_type(x
, pkey
);
1234 if (need_cert
&& i
< 0) {
1237 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1238 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1243 sc
->peer_cert_type
= i
;
1244 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1246 * Why would the following ever happen? We just created sc a couple
1249 if (sc
->peer_pkeys
[i
].x509
!= NULL
)
1250 X509_free(sc
->peer_pkeys
[i
].x509
);
1251 sc
->peer_pkeys
[i
].x509
= x
;
1252 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1254 if (s
->session
->peer
!= NULL
)
1255 X509_free(s
->session
->peer
);
1256 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1257 s
->session
->peer
= x
;
1259 sc
->peer_cert_type
= i
;
1260 sc
->peer_key
= NULL
;
1262 if (s
->session
->peer
!= NULL
)
1263 X509_free(s
->session
->peer
);
1264 s
->session
->peer
= NULL
;
1266 s
->session
->verify_result
= s
->verify_result
;
1273 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1275 s
->state
= SSL_ST_ERR
;
1278 EVP_PKEY_free(pkey
);
1280 sk_X509_pop_free(sk
, X509_free
);
1284 int ssl3_get_key_exchange(SSL
*s
)
1286 #ifndef OPENSSL_NO_RSA
1287 unsigned char *q
, md_buf
[EVP_MAX_MD_SIZE
* 2];
1290 unsigned char *param
, *p
;
1292 long i
, param_len
, n
, alg_k
, alg_a
;
1293 EVP_PKEY
*pkey
= NULL
;
1294 const EVP_MD
*md
= NULL
;
1295 #ifndef OPENSSL_NO_RSA
1298 #ifndef OPENSSL_NO_DH
1301 #ifndef OPENSSL_NO_ECDH
1302 EC_KEY
*ecdh
= NULL
;
1303 BN_CTX
*bn_ctx
= NULL
;
1304 EC_POINT
*srvr_ecpoint
= NULL
;
1306 int encoded_pt_len
= 0;
1309 EVP_MD_CTX_init(&md_ctx
);
1312 * use same message size as in ssl3_get_certificate_request() as
1313 * ServerKeyExchange message may be skipped
1315 n
= s
->method
->ssl_get_message(s
,
1316 SSL3_ST_CR_KEY_EXCH_A
,
1317 SSL3_ST_CR_KEY_EXCH_B
,
1318 -1, s
->max_cert_list
, &ok
);
1322 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1324 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
) {
1326 * Can't skip server key exchange if this is an ephemeral
1329 if (alg_k
& (SSL_kEDH
| SSL_kEECDH
)) {
1330 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1331 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1334 #ifndef OPENSSL_NO_PSK
1336 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1337 * identity hint is sent. Set session->sess_cert anyway to avoid
1340 if (alg_k
& SSL_kPSK
) {
1341 s
->session
->sess_cert
= ssl_sess_cert_new();
1342 if (s
->ctx
->psk_identity_hint
)
1343 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1344 s
->ctx
->psk_identity_hint
= NULL
;
1347 s
->s3
->tmp
.reuse_message
= 1;
1351 param
= p
= (unsigned char *)s
->init_msg
;
1352 if (s
->session
->sess_cert
!= NULL
) {
1353 #ifndef OPENSSL_NO_RSA
1354 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
) {
1355 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
1356 s
->session
->sess_cert
->peer_rsa_tmp
= NULL
;
1359 #ifndef OPENSSL_NO_DH
1360 if (s
->session
->sess_cert
->peer_dh_tmp
) {
1361 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
1362 s
->session
->sess_cert
->peer_dh_tmp
= NULL
;
1365 #ifndef OPENSSL_NO_ECDH
1366 if (s
->session
->sess_cert
->peer_ecdh_tmp
) {
1367 EC_KEY_free(s
->session
->sess_cert
->peer_ecdh_tmp
);
1368 s
->session
->sess_cert
->peer_ecdh_tmp
= NULL
;
1372 s
->session
->sess_cert
= ssl_sess_cert_new();
1375 /* Total length of the parameters including the length prefix */
1378 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1380 al
= SSL_AD_DECODE_ERROR
;
1382 #ifndef OPENSSL_NO_PSK
1383 if (alg_k
& SSL_kPSK
) {
1385 if (param_len
> n
) {
1386 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1392 * Store PSK identity hint for later use, hint is used in
1393 * ssl3_send_client_key_exchange. Assume that the maximum length of
1394 * a PSK identity hint can be as long as the maximum length of a PSK
1397 if (i
> PSK_MAX_IDENTITY_LEN
) {
1398 al
= SSL_AD_HANDSHAKE_FAILURE
;
1399 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_DATA_LENGTH_TOO_LONG
);
1402 if (i
> n
- param_len
) {
1403 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1404 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH
);
1409 s
->session
->psk_identity_hint
= BUF_strndup((char *)p
, i
);
1410 if (s
->session
->psk_identity_hint
== NULL
) {
1411 al
= SSL_AD_HANDSHAKE_FAILURE
;
1412 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1419 #endif /* !OPENSSL_NO_PSK */
1420 #ifndef OPENSSL_NO_SRP
1421 if (alg_k
& SSL_kSRP
) {
1423 if (param_len
> n
) {
1424 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1429 if (i
> n
- param_len
) {
1430 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_N_LENGTH
);
1435 if (!(s
->srp_ctx
.N
= BN_bin2bn(p
, i
, NULL
))) {
1436 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1441 if (2 > n
- param_len
) {
1442 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1449 if (i
> n
- param_len
) {
1450 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_G_LENGTH
);
1455 if (!(s
->srp_ctx
.g
= BN_bin2bn(p
, i
, NULL
))) {
1456 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1461 if (1 > n
- param_len
) {
1462 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1467 i
= (unsigned int)(p
[0]);
1470 if (i
> n
- param_len
) {
1471 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_S_LENGTH
);
1476 if (!(s
->srp_ctx
.s
= BN_bin2bn(p
, i
, NULL
))) {
1477 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1482 if (2 > n
- param_len
) {
1483 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1490 if (i
> n
- param_len
) {
1491 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_B_LENGTH
);
1496 if (!(s
->srp_ctx
.B
= BN_bin2bn(p
, i
, NULL
))) {
1497 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1503 if (!srp_verify_server_param(s
, &al
)) {
1504 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SRP_PARAMETERS
);
1508 /* We must check if there is a certificate */
1509 # ifndef OPENSSL_NO_RSA
1510 if (alg_a
& SSL_aRSA
)
1512 X509_get_pubkey(s
->session
->
1513 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1517 # ifndef OPENSSL_NO_DSA
1518 else if (alg_a
& SSL_aDSS
)
1520 X509_get_pubkey(s
->session
->
1521 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1525 #endif /* !OPENSSL_NO_SRP */
1526 #ifndef OPENSSL_NO_RSA
1527 if (alg_k
& SSL_kRSA
) {
1528 /* Temporary RSA keys only allowed in export ciphersuites */
1529 if (!SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)) {
1530 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1531 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1534 if ((rsa
= RSA_new()) == NULL
) {
1535 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1540 if (param_len
> n
) {
1541 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1546 if (i
> n
- param_len
) {
1547 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_MODULUS_LENGTH
);
1552 if (!(rsa
->n
= BN_bin2bn(p
, i
, rsa
->n
))) {
1553 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1558 if (2 > n
- param_len
) {
1559 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1566 if (i
> n
- param_len
) {
1567 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_E_LENGTH
);
1572 if (!(rsa
->e
= BN_bin2bn(p
, i
, rsa
->e
))) {
1573 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1579 /* this should be because we are using an export cipher */
1580 if (alg_a
& SSL_aRSA
)
1582 X509_get_pubkey(s
->session
->
1583 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1585 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1589 if (EVP_PKEY_bits(pkey
) <= SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
1590 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1591 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1595 s
->session
->sess_cert
->peer_rsa_tmp
= rsa
;
1598 #else /* OPENSSL_NO_RSA */
1601 #ifndef OPENSSL_NO_DH
1602 else if (alg_k
& SSL_kEDH
) {
1603 if ((dh
= DH_new()) == NULL
) {
1604 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_DH_LIB
);
1609 if (param_len
> n
) {
1610 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1615 if (i
> n
- param_len
) {
1616 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_P_LENGTH
);
1621 if (!(dh
->p
= BN_bin2bn(p
, i
, NULL
))) {
1622 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1627 if (2 > n
- param_len
) {
1628 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1635 if (i
> n
- param_len
) {
1636 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_G_LENGTH
);
1641 if (!(dh
->g
= BN_bin2bn(p
, i
, NULL
))) {
1642 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1647 if (2 > n
- param_len
) {
1648 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1655 if (i
> n
- param_len
) {
1656 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1661 if (!(dh
->pub_key
= BN_bin2bn(p
, i
, NULL
))) {
1662 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1668 # ifndef OPENSSL_NO_RSA
1669 if (alg_a
& SSL_aRSA
)
1671 X509_get_pubkey(s
->session
->
1672 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1676 # ifndef OPENSSL_NO_DSA
1677 else if (alg_a
& SSL_aDSS
)
1679 X509_get_pubkey(s
->session
->
1680 sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].
1683 /* else anonymous DH, so no certificate or pkey. */
1685 s
->session
->sess_cert
->peer_dh_tmp
= dh
;
1687 } else if ((alg_k
& SSL_kDHr
) || (alg_k
& SSL_kDHd
)) {
1688 al
= SSL_AD_ILLEGAL_PARAMETER
;
1689 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1690 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1693 #endif /* !OPENSSL_NO_DH */
1695 #ifndef OPENSSL_NO_ECDH
1696 else if (alg_k
& SSL_kEECDH
) {
1698 const EC_GROUP
*group
;
1700 if ((ecdh
= EC_KEY_new()) == NULL
) {
1701 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1706 * Extract elliptic curve parameters and the server's ephemeral ECDH
1707 * public key. Keep accumulating lengths of various components in
1708 * param_len and make sure it never exceeds n.
1712 * XXX: For now we only support named (not generic) curves and the
1713 * ECParameters in this case is just three bytes. We also need one
1714 * byte for the length of the encoded point
1717 if (param_len
> n
) {
1718 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1722 if ((*p
!= NAMED_CURVE_TYPE
) ||
1723 ((curve_nid
= tls1_ec_curve_id2nid(*(p
+ 2))) == 0)) {
1724 al
= SSL_AD_INTERNAL_ERROR
;
1725 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1726 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1730 ngroup
= EC_GROUP_new_by_curve_name(curve_nid
);
1731 if (ngroup
== NULL
) {
1732 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1735 if (EC_KEY_set_group(ecdh
, ngroup
) == 0) {
1736 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_EC_LIB
);
1739 EC_GROUP_free(ngroup
);
1741 group
= EC_KEY_get0_group(ecdh
);
1743 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1744 (EC_GROUP_get_degree(group
) > 163)) {
1745 al
= SSL_AD_EXPORT_RESTRICTION
;
1746 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1747 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1753 /* Next, get the encoded ECPoint */
1754 if (((srvr_ecpoint
= EC_POINT_new(group
)) == NULL
) ||
1755 ((bn_ctx
= BN_CTX_new()) == NULL
)) {
1756 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1760 encoded_pt_len
= *p
; /* length of encoded point */
1763 if ((encoded_pt_len
> n
- param_len
) ||
1764 (EC_POINT_oct2point(group
, srvr_ecpoint
,
1765 p
, encoded_pt_len
, bn_ctx
) == 0)) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_ECPOINT
);
1769 param_len
+= encoded_pt_len
;
1772 p
+= encoded_pt_len
;
1775 * The ECC/TLS specification does not mention the use of DSA to sign
1776 * ECParameters in the server key exchange message. We do support RSA
1780 # ifndef OPENSSL_NO_RSA
1781 else if (alg_a
& SSL_aRSA
)
1783 X509_get_pubkey(s
->session
->
1784 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1786 # ifndef OPENSSL_NO_ECDSA
1787 else if (alg_a
& SSL_aECDSA
)
1789 X509_get_pubkey(s
->session
->
1790 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1792 /* else anonymous ECDH, so no certificate or pkey. */
1793 EC_KEY_set_public_key(ecdh
, srvr_ecpoint
);
1794 s
->session
->sess_cert
->peer_ecdh_tmp
= ecdh
;
1796 BN_CTX_free(bn_ctx
);
1798 EC_POINT_free(srvr_ecpoint
);
1799 srvr_ecpoint
= NULL
;
1801 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1802 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1805 #endif /* !OPENSSL_NO_ECDH */
1807 /* p points to the next byte, there are 'n' bytes left */
1809 /* if it was signed, check the signature */
1811 if (TLS1_get_version(s
) >= TLS1_2_VERSION
) {
1814 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1818 sigalg
= tls12_get_sigid(pkey
);
1819 /* Should never happen */
1821 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1824 /* Check key type is consistent with signature */
1825 if (sigalg
!= (int)p
[1]) {
1826 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1827 SSL_R_WRONG_SIGNATURE_TYPE
);
1828 al
= SSL_AD_DECODE_ERROR
;
1831 md
= tls12_get_hash(p
[0]);
1833 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_UNKNOWN_DIGEST
);
1837 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
1845 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1850 j
= EVP_PKEY_size(pkey
);
1853 * Check signature length. If n is 0 then signature is empty
1855 if ((i
!= n
) || (n
> j
) || (n
<= 0)) {
1856 /* wrong packet length */
1857 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1860 #ifndef OPENSSL_NO_RSA
1861 if (pkey
->type
== EVP_PKEY_RSA
1862 && TLS1_get_version(s
) < TLS1_2_VERSION
) {
1868 for (num
= 2; num
> 0; num
--) {
1869 EVP_MD_CTX_set_flags(&md_ctx
, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
1870 EVP_DigestInit_ex(&md_ctx
, (num
== 2)
1871 ? s
->ctx
->md5
: s
->ctx
->sha1
, NULL
);
1872 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1874 EVP_DigestUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1876 EVP_DigestUpdate(&md_ctx
, param
, param_len
);
1877 EVP_DigestFinal_ex(&md_ctx
, q
, &size
);
1881 i
= RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
, pkey
->pkey
.rsa
);
1883 al
= SSL_AD_DECRYPT_ERROR
;
1884 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_RSA_DECRYPT
);
1889 al
= SSL_AD_DECRYPT_ERROR
;
1890 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1896 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
1897 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->client_random
[0]),
1899 EVP_VerifyUpdate(&md_ctx
, &(s
->s3
->server_random
[0]),
1901 EVP_VerifyUpdate(&md_ctx
, param
, param_len
);
1902 if (EVP_VerifyFinal(&md_ctx
, p
, (int)n
, pkey
) <= 0) {
1904 al
= SSL_AD_DECRYPT_ERROR
;
1905 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1910 /* aNULL, aSRP or kPSK do not need public keys */
1911 if (!(alg_a
& (SSL_aNULL
| SSL_aSRP
)) && !(alg_k
& SSL_kPSK
)) {
1912 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1915 /* still data left over */
1917 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1921 EVP_PKEY_free(pkey
);
1922 EVP_MD_CTX_cleanup(&md_ctx
);
1925 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1927 EVP_PKEY_free(pkey
);
1928 #ifndef OPENSSL_NO_RSA
1932 #ifndef OPENSSL_NO_DH
1936 #ifndef OPENSSL_NO_ECDH
1937 BN_CTX_free(bn_ctx
);
1938 EC_POINT_free(srvr_ecpoint
);
1942 EVP_MD_CTX_cleanup(&md_ctx
);
1943 s
->state
= SSL_ST_ERR
;
1947 int ssl3_get_certificate_request(SSL
*s
)
1950 unsigned long n
, nc
, l
;
1951 unsigned int llen
, ctype_num
, i
;
1952 X509_NAME
*xn
= NULL
;
1953 const unsigned char *p
, *q
;
1955 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1957 n
= s
->method
->ssl_get_message(s
,
1958 SSL3_ST_CR_CERT_REQ_A
,
1959 SSL3_ST_CR_CERT_REQ_B
,
1960 -1, s
->max_cert_list
, &ok
);
1965 s
->s3
->tmp
.cert_req
= 0;
1967 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
) {
1968 s
->s3
->tmp
.reuse_message
= 1;
1970 * If we get here we don't need any cached handshake records as we
1971 * wont be doing client auth.
1973 if (s
->s3
->handshake_buffer
) {
1974 if (!ssl3_digest_cached_records(s
))
1980 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
) {
1981 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1982 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_WRONG_MESSAGE_TYPE
);
1986 /* TLS does not like anon-DH with client cert */
1987 if (s
->version
> SSL3_VERSION
) {
1988 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
1989 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
1990 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
1991 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1996 p
= d
= (unsigned char *)s
->init_msg
;
1998 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1999 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2003 /* get the certificate types */
2005 if (ctype_num
> SSL3_CT_NUMBER
)
2006 ctype_num
= SSL3_CT_NUMBER
;
2007 for (i
= 0; i
< ctype_num
; i
++)
2008 s
->s3
->tmp
.ctype
[i
] = p
[i
];
2010 if (TLS1_get_version(s
) >= TLS1_2_VERSION
) {
2013 * Check we have enough room for signature algorithms and following
2016 if ((unsigned long)(p
- d
+ llen
+ 2) > n
) {
2017 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2018 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2019 SSL_R_DATA_LENGTH_TOO_LONG
);
2022 if ((llen
& 1) || !tls1_process_sigalgs(s
, p
, llen
)) {
2023 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2024 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2025 SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
2031 /* get the CA RDNs */
2036 out
= fopen("/tmp/vsign.der", "w");
2037 fwrite(p
, 1, llen
, out
);
2042 if ((unsigned long)(p
- d
+ llen
) != n
) {
2043 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2044 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_LENGTH_MISMATCH
);
2048 for (nc
= 0; nc
< llen
;) {
2050 if ((l
+ nc
+ 2) > llen
) {
2051 if ((s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
2052 goto cont
; /* netscape bugs */
2053 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2054 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, SSL_R_CA_DN_TOO_LONG
);
2060 if ((xn
= d2i_X509_NAME(NULL
, &q
, l
)) == NULL
) {
2061 /* If netscape tolerance is on, ignore errors */
2062 if (s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
)
2065 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2066 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_ASN1_LIB
);
2072 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2073 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,
2074 SSL_R_CA_DN_LENGTH_MISMATCH
);
2077 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
2078 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2091 /* we should setup a certificate to return.... */
2092 s
->s3
->tmp
.cert_req
= 1;
2093 s
->s3
->tmp
.ctype_num
= ctype_num
;
2094 if (s
->s3
->tmp
.ca_names
!= NULL
)
2095 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
2096 s
->s3
->tmp
.ca_names
= ca_sk
;
2102 s
->state
= SSL_ST_ERR
;
2105 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
2109 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
2111 return (X509_NAME_cmp(*a
, *b
));
2114 #ifndef OPENSSL_NO_TLSEXT
2115 int ssl3_get_new_session_ticket(SSL
*s
)
2117 int ok
, al
, ret
= 0, ticklen
;
2119 const unsigned char *p
;
2122 n
= s
->method
->ssl_get_message(s
,
2123 SSL3_ST_CR_SESSION_TICKET_A
,
2124 SSL3_ST_CR_SESSION_TICKET_B
,
2125 SSL3_MT_NEWSESSION_TICKET
, 16384, &ok
);
2131 /* need at least ticket_lifetime_hint + ticket length */
2132 al
= SSL_AD_DECODE_ERROR
;
2133 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
2137 p
= d
= (unsigned char *)s
->init_msg
;
2139 if (s
->session
->session_id_length
> 0) {
2140 int i
= s
->session_ctx
->session_cache_mode
;
2141 SSL_SESSION
*new_sess
;
2143 * We reused an existing session, so we need to replace it with a new
2146 if (i
& SSL_SESS_CACHE_CLIENT
) {
2148 * Remove the old session from the cache
2150 if (i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) {
2151 if (s
->session_ctx
->remove_session_cb
!= NULL
)
2152 s
->session_ctx
->remove_session_cb(s
->session_ctx
,
2155 /* We carry on if this fails */
2156 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
2160 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
2161 al
= SSL_AD_INTERNAL_ERROR
;
2162 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
2166 SSL_SESSION_free(s
->session
);
2167 s
->session
= new_sess
;
2170 n2l(p
, s
->session
->tlsext_tick_lifetime_hint
);
2172 /* ticket_lifetime_hint + ticket_length + ticket */
2173 if (ticklen
+ 6 != n
) {
2174 al
= SSL_AD_DECODE_ERROR
;
2175 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
2178 if (s
->session
->tlsext_tick
) {
2179 OPENSSL_free(s
->session
->tlsext_tick
);
2180 s
->session
->tlsext_ticklen
= 0;
2182 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
2183 if (!s
->session
->tlsext_tick
) {
2184 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
2187 memcpy(s
->session
->tlsext_tick
, p
, ticklen
);
2188 s
->session
->tlsext_ticklen
= ticklen
;
2190 * There are two ways to detect a resumed ticket session. One is to set
2191 * an appropriate session ID and then the server must return a match in
2192 * ServerHello. This allows the normal client session ID matching to work
2193 * and we know much earlier that the ticket has been accepted. The
2194 * other way is to set zero length session ID when the ticket is
2195 * presented and rely on the handshake to determine session resumption.
2196 * We choose the former approach because this fits in with assumptions
2197 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2198 * SHA256 is disabled) hash of the ticket.
2200 EVP_Digest(p
, ticklen
,
2201 s
->session
->session_id
, &s
->session
->session_id_length
,
2202 # ifndef OPENSSL_NO_SHA256
2203 EVP_sha256(), NULL
);
2210 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2212 s
->state
= SSL_ST_ERR
;
2216 int ssl3_get_cert_status(SSL
*s
)
2219 unsigned long resplen
, n
;
2220 const unsigned char *p
;
2222 n
= s
->method
->ssl_get_message(s
,
2223 SSL3_ST_CR_CERT_STATUS_A
,
2224 SSL3_ST_CR_CERT_STATUS_B
,
2225 SSL3_MT_CERTIFICATE_STATUS
, 16384, &ok
);
2230 /* need at least status type + length */
2231 al
= SSL_AD_DECODE_ERROR
;
2232 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2235 p
= (unsigned char *)s
->init_msg
;
2236 if (*p
++ != TLSEXT_STATUSTYPE_ocsp
) {
2237 al
= SSL_AD_DECODE_ERROR
;
2238 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
2242 if (resplen
+ 4 != n
) {
2243 al
= SSL_AD_DECODE_ERROR
;
2244 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2247 if (s
->tlsext_ocsp_resp
)
2248 OPENSSL_free(s
->tlsext_ocsp_resp
);
2249 s
->tlsext_ocsp_resp
= BUF_memdup(p
, resplen
);
2250 if (!s
->tlsext_ocsp_resp
) {
2251 al
= SSL_AD_INTERNAL_ERROR
;
2252 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
2255 s
->tlsext_ocsp_resplen
= resplen
;
2256 if (s
->ctx
->tlsext_status_cb
) {
2258 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2260 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
2261 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, SSL_R_INVALID_STATUS_RESPONSE
);
2265 al
= SSL_AD_INTERNAL_ERROR
;
2266 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
2272 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2273 s
->state
= SSL_ST_ERR
;
2278 int ssl3_get_server_done(SSL
*s
)
2283 /* Second to last param should be very small, like 0 :-) */
2284 n
= s
->method
->ssl_get_message(s
,
2285 SSL3_ST_CR_SRVR_DONE_A
,
2286 SSL3_ST_CR_SRVR_DONE_B
,
2287 SSL3_MT_SERVER_DONE
, 30, &ok
);
2292 /* should contain no data */
2293 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2294 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
, SSL_R_LENGTH_MISMATCH
);
2295 s
->state
= SSL_ST_ERR
;
2302 int ssl3_send_client_key_exchange(SSL
*s
)
2304 unsigned char *p
, *d
;
2306 unsigned long alg_k
;
2307 #ifndef OPENSSL_NO_RSA
2309 EVP_PKEY
*pkey
= NULL
;
2311 #ifndef OPENSSL_NO_KRB5
2313 #endif /* OPENSSL_NO_KRB5 */
2314 #ifndef OPENSSL_NO_ECDH
2315 EC_KEY
*clnt_ecdh
= NULL
;
2316 const EC_POINT
*srvr_ecpoint
= NULL
;
2317 EVP_PKEY
*srvr_pub_pkey
= NULL
;
2318 unsigned char *encodedPoint
= NULL
;
2319 int encoded_pt_len
= 0;
2320 BN_CTX
*bn_ctx
= NULL
;
2323 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
) {
2324 d
= (unsigned char *)s
->init_buf
->data
;
2327 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2329 /* Fool emacs indentation */
2332 #ifndef OPENSSL_NO_RSA
2333 else if (alg_k
& SSL_kRSA
) {
2335 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
2337 if (s
->session
->sess_cert
== NULL
) {
2339 * We should always have a server certificate with SSL_kRSA.
2341 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2342 ERR_R_INTERNAL_ERROR
);
2346 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
2347 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
2350 X509_get_pubkey(s
->session
->
2351 sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].
2353 if ((pkey
== NULL
) || (pkey
->type
!= EVP_PKEY_RSA
)
2354 || (pkey
->pkey
.rsa
== NULL
)) {
2355 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2356 ERR_R_INTERNAL_ERROR
);
2359 rsa
= pkey
->pkey
.rsa
;
2360 EVP_PKEY_free(pkey
);
2363 tmp_buf
[0] = s
->client_version
>> 8;
2364 tmp_buf
[1] = s
->client_version
& 0xff;
2365 if (RAND_bytes(&(tmp_buf
[2]), sizeof tmp_buf
- 2) <= 0)
2368 s
->session
->master_key_length
= sizeof tmp_buf
;
2371 /* Fix buf for TLS and beyond */
2372 if (s
->version
> SSL3_VERSION
)
2374 n
= RSA_public_encrypt(sizeof tmp_buf
,
2375 tmp_buf
, p
, rsa
, RSA_PKCS1_PADDING
);
2377 if (s
->options
& SSL_OP_PKCS1_CHECK_1
)
2379 if (s
->options
& SSL_OP_PKCS1_CHECK_2
)
2383 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2384 SSL_R_BAD_RSA_ENCRYPT
);
2388 /* Fix buf for TLS and beyond */
2389 if (s
->version
> SSL3_VERSION
) {
2394 s
->session
->master_key_length
=
2395 s
->method
->ssl3_enc
->generate_master_secret(s
,
2397 session
->master_key
,
2400 OPENSSL_cleanse(tmp_buf
, sizeof tmp_buf
);
2403 #ifndef OPENSSL_NO_KRB5
2404 else if (alg_k
& SSL_kKRB5
) {
2405 krb5_error_code krb5rc
;
2406 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
2407 /* krb5_data krb5_ap_req; */
2408 krb5_data
*enc_ticket
;
2409 krb5_data authenticator
, *authp
= NULL
;
2410 EVP_CIPHER_CTX ciph_ctx
;
2411 const EVP_CIPHER
*enc
= NULL
;
2412 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2413 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
2414 unsigned char epms
[SSL_MAX_MASTER_KEY_LENGTH
+ EVP_MAX_IV_LENGTH
];
2415 int padl
, outl
= sizeof(epms
);
2417 EVP_CIPHER_CTX_init(&ciph_ctx
);
2420 fprintf(stderr
, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2422 # endif /* KSSL_DEBUG */
2425 # ifdef KRB5SENDAUTH
2427 authp
= &authenticator
;
2428 # endif /* KRB5SENDAUTH */
2430 krb5rc
= kssl_cget_tkt(kssl_ctx
, &enc_ticket
, authp
, &kssl_err
);
2431 enc
= kssl_map_enc(kssl_ctx
->enctype
);
2436 fprintf(stderr
, "kssl_cget_tkt rtn %d\n", krb5rc
);
2437 if (krb5rc
&& kssl_err
.text
)
2438 fprintf(stderr
, "kssl_cget_tkt kssl_err=%s\n",
2441 # endif /* KSSL_DEBUG */
2444 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2445 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, kssl_err
.reason
);
2450 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2451 * in place of RFC 2712 KerberosWrapper, as in:
2453 * Send ticket (copy to *p, set n = length)
2454 * n = krb5_ap_req.length;
2455 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2456 * if (krb5_ap_req.data)
2457 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2459 * Now using real RFC 2712 KerberosWrapper
2460 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2461 * Note: 2712 "opaque" types are here replaced
2462 * with a 2-byte length followed by the value.
2464 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2465 * Where "xx xx" = length bytes. Shown here with
2466 * optional authenticator omitted.
2469 /* KerberosWrapper.Ticket */
2470 s2n(enc_ticket
->length
, p
);
2471 memcpy(p
, enc_ticket
->data
, enc_ticket
->length
);
2472 p
+= enc_ticket
->length
;
2473 n
= enc_ticket
->length
+ 2;
2475 /* KerberosWrapper.Authenticator */
2476 if (authp
&& authp
->length
) {
2477 s2n(authp
->length
, p
);
2478 memcpy(p
, authp
->data
, authp
->length
);
2480 n
+= authp
->length
+ 2;
2486 s2n(0, p
); /* null authenticator length */
2490 tmp_buf
[0] = s
->client_version
>> 8;
2491 tmp_buf
[1] = s
->client_version
& 0xff;
2492 if (RAND_bytes(&(tmp_buf
[2]), sizeof tmp_buf
- 2) <= 0)
2496 * 20010420 VRS. Tried it this way; failed.
2497 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2498 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2499 * kssl_ctx->length);
2500 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2503 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2504 EVP_EncryptInit_ex(&ciph_ctx
, enc
, NULL
, kssl_ctx
->key
, iv
);
2505 EVP_EncryptUpdate(&ciph_ctx
, epms
, &outl
, tmp_buf
,
2507 EVP_EncryptFinal_ex(&ciph_ctx
, &(epms
[outl
]), &padl
);
2509 if (outl
> (int)sizeof epms
) {
2510 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2511 ERR_R_INTERNAL_ERROR
);
2514 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2516 /* KerberosWrapper.EncryptedPreMasterSecret */
2518 memcpy(p
, epms
, outl
);
2522 s
->session
->master_key_length
=
2523 s
->method
->ssl3_enc
->generate_master_secret(s
,
2525 session
->master_key
,
2529 OPENSSL_cleanse(tmp_buf
, sizeof tmp_buf
);
2530 OPENSSL_cleanse(epms
, outl
);
2533 #ifndef OPENSSL_NO_DH
2534 else if (alg_k
& (SSL_kEDH
| SSL_kDHr
| SSL_kDHd
)) {
2535 DH
*dh_srvr
, *dh_clnt
;
2537 if (s
->session
->sess_cert
== NULL
) {
2538 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2539 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2540 SSL_R_UNEXPECTED_MESSAGE
);
2544 if (s
->session
->sess_cert
->peer_dh_tmp
!= NULL
)
2545 dh_srvr
= s
->session
->sess_cert
->peer_dh_tmp
;
2547 /* we get them from the cert */
2548 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2549 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2550 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
2554 /* generate a new random key */
2555 if ((dh_clnt
= DHparams_dup(dh_srvr
)) == NULL
) {
2556 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2559 if (!DH_generate_key(dh_clnt
)) {
2560 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2566 * use the 'p' output buffer for the DH key, but make sure to
2567 * clear it out afterwards
2570 n
= DH_compute_key(p
, dh_srvr
->pub_key
, dh_clnt
);
2573 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_DH_LIB
);
2578 /* generate master key from the result */
2579 s
->session
->master_key_length
=
2580 s
->method
->ssl3_enc
->generate_master_secret(s
,
2582 session
->master_key
,
2587 /* send off the data */
2588 n
= BN_num_bytes(dh_clnt
->pub_key
);
2590 BN_bn2bin(dh_clnt
->pub_key
, p
);
2597 #ifndef OPENSSL_NO_ECDH
2598 else if (alg_k
& (SSL_kEECDH
| SSL_kECDHr
| SSL_kECDHe
)) {
2599 const EC_GROUP
*srvr_group
= NULL
;
2601 int ecdh_clnt_cert
= 0;
2604 if (s
->session
->sess_cert
== NULL
) {
2605 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
2606 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2607 SSL_R_UNEXPECTED_MESSAGE
);
2612 * Did we send out the client's ECDH share for use in premaster
2613 * computation as part of client certificate? If so, set
2614 * ecdh_clnt_cert to 1.
2616 if ((alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) && (s
->cert
!= NULL
)) {
2618 * XXX: For now, we do not support client
2619 * authentication using ECDH certificates.
2620 * To add such support, one needs to add
2621 * code that checks for appropriate
2622 * conditions and sets ecdh_clnt_cert to 1.
2623 * For example, the cert have an ECC
2624 * key on the same curve as the server's
2625 * and the key should be authorized for
2628 * One also needs to add code in ssl3_connect
2629 * to skip sending the certificate verify
2632 * if ((s->cert->key->privatekey != NULL) &&
2633 * (s->cert->key->privatekey->type ==
2634 * EVP_PKEY_EC) && ...)
2635 * ecdh_clnt_cert = 1;
2639 if (s
->session
->sess_cert
->peer_ecdh_tmp
!= NULL
) {
2640 tkey
= s
->session
->sess_cert
->peer_ecdh_tmp
;
2642 /* Get the Server Public Key from Cert */
2644 X509_get_pubkey(s
->session
->
2645 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
2646 if ((srvr_pub_pkey
== NULL
)
2647 || (srvr_pub_pkey
->type
!= EVP_PKEY_EC
)
2648 || (srvr_pub_pkey
->pkey
.ec
== NULL
)) {
2649 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2650 ERR_R_INTERNAL_ERROR
);
2654 tkey
= srvr_pub_pkey
->pkey
.ec
;
2657 srvr_group
= EC_KEY_get0_group(tkey
);
2658 srvr_ecpoint
= EC_KEY_get0_public_key(tkey
);
2660 if ((srvr_group
== NULL
) || (srvr_ecpoint
== NULL
)) {
2661 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2662 ERR_R_INTERNAL_ERROR
);
2666 if ((clnt_ecdh
= EC_KEY_new()) == NULL
) {
2667 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2668 ERR_R_MALLOC_FAILURE
);
2672 if (!EC_KEY_set_group(clnt_ecdh
, srvr_group
)) {
2673 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2676 if (ecdh_clnt_cert
) {
2678 * Reuse key info from our certificate We only need our
2679 * private key to perform the ECDH computation.
2681 const BIGNUM
*priv_key
;
2682 tkey
= s
->cert
->key
->privatekey
->pkey
.ec
;
2683 priv_key
= EC_KEY_get0_private_key(tkey
);
2684 if (priv_key
== NULL
) {
2685 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2686 ERR_R_MALLOC_FAILURE
);
2689 if (!EC_KEY_set_private_key(clnt_ecdh
, priv_key
)) {
2690 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2694 /* Generate a new ECDH key pair */
2695 if (!(EC_KEY_generate_key(clnt_ecdh
))) {
2696 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2703 * use the 'p' output buffer for the ECDH key, but make sure to
2704 * clear it out afterwards
2707 field_size
= EC_GROUP_get_degree(srvr_group
);
2708 if (field_size
<= 0) {
2709 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2712 n
= ECDH_compute_key(p
, (field_size
+ 7) / 8, srvr_ecpoint
,
2715 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2719 /* generate master key from the result */
2720 s
->session
->master_key_length
=
2721 s
->method
->ssl3_enc
->generate_master_secret(s
,
2723 session
->master_key
,
2726 memset(p
, 0, n
); /* clean up */
2728 if (ecdh_clnt_cert
) {
2729 /* Send empty client key exch message */
2733 * First check the size of encoding and allocate memory
2737 EC_POINT_point2oct(srvr_group
,
2738 EC_KEY_get0_public_key(clnt_ecdh
),
2739 POINT_CONVERSION_UNCOMPRESSED
,
2742 encodedPoint
= (unsigned char *)
2743 OPENSSL_malloc(encoded_pt_len
* sizeof(unsigned char));
2744 bn_ctx
= BN_CTX_new();
2745 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
)) {
2746 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2747 ERR_R_MALLOC_FAILURE
);
2751 /* Encode the public key */
2752 n
= EC_POINT_point2oct(srvr_group
,
2753 EC_KEY_get0_public_key(clnt_ecdh
),
2754 POINT_CONVERSION_UNCOMPRESSED
,
2755 encodedPoint
, encoded_pt_len
, bn_ctx
);
2757 *p
= n
; /* length of encoded point */
2758 /* Encoded point will be copied here */
2760 /* copy the point */
2761 memcpy((unsigned char *)p
, encodedPoint
, n
);
2762 /* increment n to account for length field */
2766 /* Free allocated memory */
2767 BN_CTX_free(bn_ctx
);
2768 if (encodedPoint
!= NULL
)
2769 OPENSSL_free(encodedPoint
);
2770 if (clnt_ecdh
!= NULL
)
2771 EC_KEY_free(clnt_ecdh
);
2772 EVP_PKEY_free(srvr_pub_pkey
);
2774 #endif /* !OPENSSL_NO_ECDH */
2775 else if (alg_k
& SSL_kGOST
) {
2776 /* GOST key exchange message creation */
2777 EVP_PKEY_CTX
*pkey_ctx
;
2780 unsigned int md_len
;
2782 unsigned char premaster_secret
[32], shared_ukm
[32], tmp
[256];
2783 EVP_MD_CTX
*ukm_hash
;
2787 * Get server sertificate PKEY and create ctx from it
2791 sess_cert
->peer_pkeys
[(keytype
= SSL_PKEY_GOST01
)].x509
;
2795 sess_cert
->peer_pkeys
[(keytype
= SSL_PKEY_GOST94
)].x509
;
2797 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2798 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2802 pkey_ctx
= EVP_PKEY_CTX_new(pub_key
=
2803 X509_get_pubkey(peer_cert
), NULL
);
2805 * If we have send a certificate, and certificate key
2807 * * parameters match those of server certificate, use
2808 * certificate key for key exchange
2811 /* Otherwise, generate ephemeral key pair */
2813 EVP_PKEY_encrypt_init(pkey_ctx
);
2814 /* Generate session key */
2815 if (RAND_bytes(premaster_secret
, 32) <= 0) {
2816 EVP_PKEY_CTX_free(pkey_ctx
);
2820 * If we have client certificate, use its secret as peer key
2822 if (s
->s3
->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2823 if (EVP_PKEY_derive_set_peer
2824 (pkey_ctx
, s
->cert
->key
->privatekey
) <= 0) {
2826 * If there was an error - just ignore it. Ephemeral key
2833 * Compute shared IV and store it in algorithm-specific context
2836 ukm_hash
= EVP_MD_CTX_create();
2837 EVP_DigestInit(ukm_hash
,
2838 EVP_get_digestbynid(NID_id_GostR3411_94
));
2839 EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
,
2841 EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
,
2843 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
2844 EVP_MD_CTX_destroy(ukm_hash
);
2845 if (EVP_PKEY_CTX_ctrl
2846 (pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
, EVP_PKEY_CTRL_SET_IV
, 8,
2848 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2852 /* Make GOST keytransport blob message */
2854 * Encapsulate it into sequence
2856 *(p
++) = V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
;
2858 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, premaster_secret
, 32)
2860 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2864 if (msglen
>= 0x80) {
2866 *(p
++) = msglen
& 0xff;
2869 *(p
++) = msglen
& 0xff;
2872 memcpy(p
, tmp
, msglen
);
2873 /* Check if pubkey from client certificate was used */
2874 if (EVP_PKEY_CTX_ctrl
2875 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0) {
2876 /* Set flag "skip certificate verify" */
2877 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2879 EVP_PKEY_CTX_free(pkey_ctx
);
2880 s
->session
->master_key_length
=
2881 s
->method
->ssl3_enc
->generate_master_secret(s
,
2883 session
->master_key
,
2886 EVP_PKEY_free(pub_key
);
2889 #ifndef OPENSSL_NO_SRP
2890 else if (alg_k
& SSL_kSRP
) {
2891 if (s
->srp_ctx
.A
!= NULL
) {
2892 /* send off the data */
2893 n
= BN_num_bytes(s
->srp_ctx
.A
);
2895 BN_bn2bin(s
->srp_ctx
.A
, p
);
2898 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2899 ERR_R_INTERNAL_ERROR
);
2902 if (s
->session
->srp_username
!= NULL
)
2903 OPENSSL_free(s
->session
->srp_username
);
2904 s
->session
->srp_username
= BUF_strdup(s
->srp_ctx
.login
);
2905 if (s
->session
->srp_username
== NULL
) {
2906 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2907 ERR_R_MALLOC_FAILURE
);
2911 if ((s
->session
->master_key_length
=
2912 SRP_generate_client_master_secret(s
,
2913 s
->session
->master_key
)) <
2915 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2916 ERR_R_INTERNAL_ERROR
);
2921 #ifndef OPENSSL_NO_PSK
2922 else if (alg_k
& SSL_kPSK
) {
2924 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2925 * \0-terminated identity. The last byte is for us for simulating
2928 char identity
[PSK_MAX_IDENTITY_LEN
+ 2];
2929 size_t identity_len
;
2930 unsigned char *t
= NULL
;
2931 unsigned char psk_or_pre_ms
[PSK_MAX_PSK_LEN
* 2 + 4];
2932 unsigned int pre_ms_len
= 0, psk_len
= 0;
2936 if (s
->psk_client_callback
== NULL
) {
2937 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2938 SSL_R_PSK_NO_CLIENT_CB
);
2942 memset(identity
, 0, sizeof(identity
));
2943 psk_len
= s
->psk_client_callback(s
, s
->session
->psk_identity_hint
,
2944 identity
, sizeof(identity
) - 1,
2946 sizeof(psk_or_pre_ms
));
2947 if (psk_len
> PSK_MAX_PSK_LEN
) {
2948 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2949 ERR_R_INTERNAL_ERROR
);
2951 } else if (psk_len
== 0) {
2952 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2953 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2956 identity
[PSK_MAX_IDENTITY_LEN
+ 1] = '\0';
2957 identity_len
= strlen(identity
);
2958 if (identity_len
> PSK_MAX_IDENTITY_LEN
) {
2959 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2960 ERR_R_INTERNAL_ERROR
);
2963 /* create PSK pre_master_secret */
2964 pre_ms_len
= 2 + psk_len
+ 2 + psk_len
;
2966 memmove(psk_or_pre_ms
+ psk_len
+ 4, psk_or_pre_ms
, psk_len
);
2968 memset(t
, 0, psk_len
);
2972 if (s
->session
->psk_identity_hint
!= NULL
)
2973 OPENSSL_free(s
->session
->psk_identity_hint
);
2974 s
->session
->psk_identity_hint
=
2975 BUF_strdup(s
->ctx
->psk_identity_hint
);
2976 if (s
->ctx
->psk_identity_hint
!= NULL
2977 && s
->session
->psk_identity_hint
== NULL
) {
2978 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2979 ERR_R_MALLOC_FAILURE
);
2983 if (s
->session
->psk_identity
!= NULL
)
2984 OPENSSL_free(s
->session
->psk_identity
);
2985 s
->session
->psk_identity
= BUF_strdup(identity
);
2986 if (s
->session
->psk_identity
== NULL
) {
2987 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2988 ERR_R_MALLOC_FAILURE
);
2992 s
->session
->master_key_length
=
2993 s
->method
->ssl3_enc
->generate_master_secret(s
,
2995 session
->master_key
,
2998 s2n(identity_len
, p
);
2999 memcpy(p
, identity
, identity_len
);
3000 n
= 2 + identity_len
;
3003 OPENSSL_cleanse(identity
, sizeof(identity
));
3004 OPENSSL_cleanse(psk_or_pre_ms
, sizeof(psk_or_pre_ms
));
3006 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3012 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3013 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3017 *(d
++) = SSL3_MT_CLIENT_KEY_EXCHANGE
;
3020 s
->state
= SSL3_ST_CW_KEY_EXCH_B
;
3021 /* number of bytes to write */
3022 s
->init_num
= n
+ 4;
3026 /* SSL3_ST_CW_KEY_EXCH_B */
3027 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
3029 #ifndef OPENSSL_NO_ECDH
3030 BN_CTX_free(bn_ctx
);
3031 if (encodedPoint
!= NULL
)
3032 OPENSSL_free(encodedPoint
);
3033 if (clnt_ecdh
!= NULL
)
3034 EC_KEY_free(clnt_ecdh
);
3035 EVP_PKEY_free(srvr_pub_pkey
);
3037 s
->state
= SSL_ST_ERR
;
3041 int ssl3_send_client_verify(SSL
*s
)
3043 unsigned char *p
, *d
;
3044 unsigned char data
[MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
];
3046 EVP_PKEY_CTX
*pctx
= NULL
;
3052 EVP_MD_CTX_init(&mctx
);
3054 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
) {
3055 d
= (unsigned char *)s
->init_buf
->data
;
3057 pkey
= s
->cert
->key
->privatekey
;
3058 /* Create context from key and test if sha1 is allowed as digest */
3059 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
3060 EVP_PKEY_sign_init(pctx
);
3061 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1()) > 0) {
3062 if (TLS1_get_version(s
) < TLS1_2_VERSION
)
3063 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3066 [MD5_DIGEST_LENGTH
]));
3071 * For TLS v1.2 send signature algorithm and signature using agreed
3072 * digest and cached handshake records.
3074 if (TLS1_get_version(s
) >= TLS1_2_VERSION
) {
3077 const EVP_MD
*md
= s
->cert
->key
->digest
;
3078 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
3079 if (hdatalen
<= 0 || !tls12_get_sigandhash(p
, pkey
, md
)) {
3080 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3085 fprintf(stderr
, "Using TLS 1.2 with client alg %s\n",
3088 if (!EVP_SignInit_ex(&mctx
, md
, NULL
)
3089 || !EVP_SignUpdate(&mctx
, hdata
, hdatalen
)
3090 || !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
)) {
3091 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_EVP_LIB
);
3096 if (!ssl3_digest_cached_records(s
))
3099 #ifndef OPENSSL_NO_RSA
3100 if (pkey
->type
== EVP_PKEY_RSA
) {
3101 s
->method
->ssl3_enc
->cert_verify_mac(s
, NID_md5
, &(data
[0]));
3102 if (RSA_sign(NID_md5_sha1
, data
,
3103 MD5_DIGEST_LENGTH
+ SHA_DIGEST_LENGTH
,
3104 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0) {
3105 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_RSA_LIB
);
3112 #ifndef OPENSSL_NO_DSA
3113 if (pkey
->type
== EVP_PKEY_DSA
) {
3114 if (!DSA_sign(pkey
->save_type
,
3115 &(data
[MD5_DIGEST_LENGTH
]),
3116 SHA_DIGEST_LENGTH
, &(p
[2]),
3117 (unsigned int *)&j
, pkey
->pkey
.dsa
)) {
3118 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_DSA_LIB
);
3125 #ifndef OPENSSL_NO_ECDSA
3126 if (pkey
->type
== EVP_PKEY_EC
) {
3127 if (!ECDSA_sign(pkey
->save_type
,
3128 &(data
[MD5_DIGEST_LENGTH
]),
3129 SHA_DIGEST_LENGTH
, &(p
[2]),
3130 (unsigned int *)&j
, pkey
->pkey
.ec
)) {
3131 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_ECDSA_LIB
);
3138 if (pkey
->type
== NID_id_GostR3410_94
3139 || pkey
->type
== NID_id_GostR3410_2001
) {
3140 unsigned char signbuf
[64];
3142 size_t sigsize
= 64;
3143 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3144 NID_id_GostR3411_94
, data
);
3145 if (EVP_PKEY_sign(pctx
, signbuf
, &sigsize
, data
, 32) <= 0) {
3146 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3149 for (i
= 63, j
= 0; i
>= 0; j
++, i
--) {
3150 p
[2 + j
] = signbuf
[i
];
3155 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3158 *(d
++) = SSL3_MT_CERTIFICATE_VERIFY
;
3161 s
->state
= SSL3_ST_CW_CERT_VRFY_B
;
3162 s
->init_num
= (int)n
+ 4;
3165 EVP_MD_CTX_cleanup(&mctx
);
3166 EVP_PKEY_CTX_free(pctx
);
3167 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
3169 EVP_MD_CTX_cleanup(&mctx
);
3170 EVP_PKEY_CTX_free(pctx
);
3171 s
->state
= SSL_ST_ERR
;
3175 int ssl3_send_client_certificate(SSL
*s
)
3178 EVP_PKEY
*pkey
= NULL
;
3182 if (s
->state
== SSL3_ST_CW_CERT_A
) {
3183 if ((s
->cert
== NULL
) ||
3184 (s
->cert
->key
->x509
== NULL
) ||
3185 (s
->cert
->key
->privatekey
== NULL
))
3186 s
->state
= SSL3_ST_CW_CERT_B
;
3188 s
->state
= SSL3_ST_CW_CERT_C
;
3191 /* We need to get a client cert */
3192 if (s
->state
== SSL3_ST_CW_CERT_B
) {
3194 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3195 * return(-1); We then get retied later
3198 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
3200 s
->rwstate
= SSL_X509_LOOKUP
;
3203 s
->rwstate
= SSL_NOTHING
;
3204 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
3205 s
->state
= SSL3_ST_CW_CERT_B
;
3206 if (!SSL_use_certificate(s
, x509
) || !SSL_use_PrivateKey(s
, pkey
))
3208 } else if (i
== 1) {
3210 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,
3211 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
3217 EVP_PKEY_free(pkey
);
3219 if (s
->version
== SSL3_VERSION
) {
3220 s
->s3
->tmp
.cert_req
= 0;
3221 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_CERTIFICATE
);
3224 s
->s3
->tmp
.cert_req
= 2;
3228 /* Ok, we have a cert */
3229 s
->state
= SSL3_ST_CW_CERT_C
;
3232 if (s
->state
== SSL3_ST_CW_CERT_C
) {
3233 s
->state
= SSL3_ST_CW_CERT_D
;
3234 l
= ssl3_output_cert_chain(s
,
3235 (s
->s3
->tmp
.cert_req
==
3236 2) ? NULL
: s
->cert
->key
->x509
);
3238 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3239 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3240 s
->state
= SSL_ST_ERR
;
3243 s
->init_num
= (int)l
;
3246 /* SSL3_ST_CW_CERT_D */
3247 return (ssl3_do_write(s
, SSL3_RT_HANDSHAKE
));
3250 #define has_bits(i,m) (((i)&(m)) == (m))
3252 int ssl3_check_cert_and_algorithm(SSL
*s
)
3256 EVP_PKEY
*pkey
= NULL
;
3259 #ifndef OPENSSL_NO_RSA
3262 #ifndef OPENSSL_NO_DH
3265 int al
= SSL_AD_HANDSHAKE_FAILURE
;
3267 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3268 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
3270 /* we don't have a certificate */
3271 if ((alg_a
& (SSL_aDH
| SSL_aNULL
| SSL_aKRB5
)) || (alg_k
& SSL_kPSK
))
3274 sc
= s
->session
->sess_cert
;
3276 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
3279 #ifndef OPENSSL_NO_RSA
3280 rsa
= s
->session
->sess_cert
->peer_rsa_tmp
;
3282 #ifndef OPENSSL_NO_DH
3283 dh
= s
->session
->sess_cert
->peer_dh_tmp
;
3286 /* This is the passed certificate */
3288 idx
= sc
->peer_cert_type
;
3289 #ifndef OPENSSL_NO_ECDH
3290 if (idx
== SSL_PKEY_ECC
) {
3291 if (ssl_check_srvr_ecc_cert_and_alg(sc
->peer_pkeys
[idx
].x509
, s
) == 0) {
3293 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_BAD_ECC_CERT
);
3300 pkey
= X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
3301 pkey_bits
= EVP_PKEY_bits(pkey
);
3302 i
= X509_certificate_type(sc
->peer_pkeys
[idx
].x509
, pkey
);
3303 EVP_PKEY_free(pkey
);
3305 /* Check that we have a certificate if we require one */
3306 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
| EVP_PKT_SIGN
)) {
3307 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3308 SSL_R_MISSING_RSA_SIGNING_CERT
);
3311 #ifndef OPENSSL_NO_DSA
3312 else if ((alg_a
& SSL_aDSS
) && !has_bits(i
, EVP_PK_DSA
| EVP_PKT_SIGN
)) {
3313 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3314 SSL_R_MISSING_DSA_SIGNING_CERT
);
3318 #ifndef OPENSSL_NO_RSA
3319 if (alg_k
& SSL_kRSA
) {
3320 if (!SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
3321 !has_bits(i
, EVP_PK_RSA
| EVP_PKT_ENC
)) {
3322 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3323 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
3325 } else if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)) {
3326 if (pkey_bits
<= SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3327 if (!has_bits(i
, EVP_PK_RSA
| EVP_PKT_ENC
)) {
3328 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3329 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
3333 /* server key exchange is not allowed. */
3334 al
= SSL_AD_INTERNAL_ERROR
;
3335 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
3342 #ifndef OPENSSL_NO_DH
3343 if ((alg_k
& SSL_kEDH
) && dh
== NULL
) {
3344 al
= SSL_AD_INTERNAL_ERROR
;
3345 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
3348 if ((alg_k
& SSL_kDHr
) && !has_bits(i
, EVP_PK_DH
| EVP_PKS_RSA
)) {
3349 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3350 SSL_R_MISSING_DH_RSA_CERT
);
3353 # ifndef OPENSSL_NO_DSA
3354 if ((alg_k
& SSL_kDHd
) && !has_bits(i
, EVP_PK_DH
| EVP_PKS_DSA
)) {
3355 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3356 SSL_R_MISSING_DH_DSA_CERT
);
3361 /* Check DHE only: static DH not implemented. */
3362 if (alg_k
& SSL_kEDH
) {
3363 int dh_size
= BN_num_bits(dh
->p
);
3364 if ((!SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && dh_size
< 768)
3365 || (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && dh_size
< 512)) {
3366 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_DH_KEY_TOO_SMALL
);
3370 #endif /* !OPENSSL_NO_DH */
3372 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
3373 pkey_bits
> SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3374 #ifndef OPENSSL_NO_RSA
3375 if (alg_k
& SSL_kRSA
) {
3377 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3378 SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
3380 } else if (BN_num_bits(rsa
->n
) >
3381 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3382 /* We have a temporary RSA key but it's too large. */
3383 al
= SSL_AD_EXPORT_RESTRICTION
;
3384 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3385 SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
3390 #ifndef OPENSSL_NO_DH
3391 if (alg_k
& SSL_kEDH
) {
3392 if (BN_num_bits(dh
->p
) >
3393 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)) {
3394 /* We have a temporary DH key but it's too large. */
3395 al
= SSL_AD_EXPORT_RESTRICTION
;
3396 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3397 SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
3400 } else if (alg_k
& (SSL_kDHr
| SSL_kDHd
)) {
3401 /* The cert should have had an export DH key. */
3402 al
= SSL_AD_EXPORT_RESTRICTION
;
3403 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3404 SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
3409 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
3410 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
3416 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3421 #ifndef OPENSSL_NO_TLSEXT
3423 * Normally, we can tell if the server is resuming the session from
3424 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3425 * message after the ServerHello to determine if the server is resuming.
3426 * Therefore, we allow EAP-FAST to peek ahead.
3427 * ssl3_check_finished returns 1 if we are resuming from an external
3428 * pre-shared secret, we have a "ticket" and the next server handshake message
3429 * is Finished; and 0 otherwise. It returns -1 upon an error.
3431 static int ssl3_check_finished(SSL
*s
)
3435 if (s
->version
< TLS1_VERSION
|| !s
->tls_session_secret_cb
||
3436 !s
->session
->tlsext_tick
)
3439 /* Need to permit this temporarily, in case the next message is Finished. */
3440 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
3442 * This function is called when we might get a Certificate message instead,
3443 * so permit appropriate message length.
3444 * We ignore the return value as we're only interested in the message type
3445 * and not its length.
3447 s
->method
->ssl_get_message(s
,
3450 -1, s
->max_cert_list
, &ok
);
3451 s
->s3
->flags
&= ~SSL3_FLAGS_CCS_OK
;
3456 s
->s3
->tmp
.reuse_message
= 1;
3458 if (s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
)
3461 /* If we're not done, then the CCS arrived early and we should bail. */
3462 if (s
->s3
->change_cipher_spec
) {
3463 SSLerr(SSL_F_SSL3_CHECK_FINISHED
, SSL_R_CCS_RECEIVED_EARLY
);
3464 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_UNEXPECTED_MESSAGE
);
3471 # ifndef OPENSSL_NO_NEXTPROTONEG
3472 int ssl3_send_next_proto(SSL
*s
)
3474 unsigned int len
, padding_len
;
3477 if (s
->state
== SSL3_ST_CW_NEXT_PROTO_A
) {
3478 len
= s
->next_proto_negotiated_len
;
3479 padding_len
= 32 - ((len
+ 2) % 32);
3480 d
= (unsigned char *)s
->init_buf
->data
;
3482 memcpy(d
+ 5, s
->next_proto_negotiated
, len
);
3483 d
[5 + len
] = padding_len
;
3484 memset(d
+ 6 + len
, 0, padding_len
);
3485 *(d
++) = SSL3_MT_NEXT_PROTO
;
3486 l2n3(2 + len
+ padding_len
, d
);
3487 s
->state
= SSL3_ST_CW_NEXT_PROTO_B
;
3488 s
->init_num
= 4 + 2 + len
+ padding_len
;
3492 return ssl3_do_write(s
, SSL3_RT_HANDSHAKE
);
3494 #endif /* !OPENSSL_NO_NEXTPROTONEG */
3495 #endif /* !OPENSSL_NO_TLSEXT */
3497 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
3500 #ifndef OPENSSL_NO_ENGINE
3501 if (s
->ctx
->client_cert_engine
) {
3502 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
3503 SSL_get_client_CA_list(s
),
3504 px509
, ppkey
, NULL
, NULL
, NULL
);
3509 if (s
->ctx
->client_cert_cb
)
3510 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);