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
);
172 #ifndef OPENSSL_NO_SSL3_METHOD
173 static const SSL_METHOD
*ssl3_get_client_method(int ver
)
175 if (ver
== SSL3_VERSION
)
176 return(SSLv3_client_method());
181 IMPLEMENT_ssl3_meth_func(SSLv3_client_method
,
182 ssl_undefined_function
,
184 ssl3_get_client_method
)
187 int ssl3_connect(SSL
*s
)
190 unsigned long Time
=(unsigned long)time(NULL
);
191 void (*cb
)(const SSL
*ssl
,int type
,int val
)=NULL
;
193 int new_state
,state
,skip
=0;
195 RAND_add(&Time
,sizeof(Time
),0);
199 if (s
->info_callback
!= NULL
)
201 else if (s
->ctx
->info_callback
!= NULL
)
202 cb
=s
->ctx
->info_callback
;
205 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
207 #ifndef OPENSSL_NO_HEARTBEATS
208 /* If we're awaiting a HeartbeatResponse, pretend we
209 * already got and don't await it anymore, because
210 * Heartbeats don't make sense during handshakes anyway.
212 if (s
->tlsext_hb_pending
)
214 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
:
236 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
238 if ((s
->version
& 0xff00 ) != 0x0300)
240 SSLerr(SSL_F_SSL3_CONNECT
, ERR_R_INTERNAL_ERROR
);
245 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0,
248 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_VERSION_TOO_LOW
);
252 /* s->version=SSL3_VERSION; */
253 s
->type
=SSL_ST_CONNECT
;
255 if (s
->init_buf
== NULL
)
257 if ((buf
=BUF_MEM_new()) == NULL
)
262 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
271 if (!ssl3_setup_buffers(s
)) { ret
= -1; goto end
; }
273 /* setup buffing BIO */
274 if (!ssl_init_wbio_buffer(s
,0)) { ret
= -1; goto end
; }
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
++;
285 case SSL3_ST_CW_CLNT_HELLO_A
:
286 case SSL3_ST_CW_CLNT_HELLO_B
:
289 ret
=ssl3_client_hello(s
);
290 if (ret
<= 0) goto end
;
291 s
->state
=SSL3_ST_CR_SRVR_HELLO_A
;
294 /* turn on buffering for the next lot of output */
295 if (s
->bbio
!= s
->wbio
)
296 s
->wbio
=BIO_push(s
->bbio
,s
->wbio
);
300 case SSL3_ST_CR_SRVR_HELLO_A
:
301 case SSL3_ST_CR_SRVR_HELLO_B
:
302 ret
=ssl3_get_server_hello(s
);
303 if (ret
<= 0) goto end
;
307 s
->state
=SSL3_ST_CR_FINISHED_A
;
308 #ifndef OPENSSL_NO_TLSEXT
309 if (s
->tlsext_ticket_expected
)
311 /* receive renewed session ticket */
312 s
->state
=SSL3_ST_CR_SESSION_TICKET_A
;
318 s
->state
=SSL3_ST_CR_CERT_A
;
322 case SSL3_ST_CR_CERT_A
:
323 case SSL3_ST_CR_CERT_B
:
324 #ifndef OPENSSL_NO_TLSEXT
325 ret
=ssl3_check_finished(s
);
326 if (ret
<= 0) goto end
;
330 if (s
->tlsext_ticket_expected
)
331 s
->state
=SSL3_ST_CR_SESSION_TICKET_A
;
333 s
->state
=SSL3_ST_CR_FINISHED_A
;
338 /* Check if it is anon DH/ECDH, SRP auth */
340 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
|SSL_aSRP
)) &&
341 !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
))
343 ret
=ssl3_get_server_certificate(s
);
344 if (ret
<= 0) goto end
;
345 #ifndef OPENSSL_NO_TLSEXT
346 if (s
->tlsext_status_expected
)
347 s
->state
=SSL3_ST_CR_CERT_STATUS_A
;
349 s
->state
=SSL3_ST_CR_KEY_EXCH_A
;
354 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
);
369 if (ret
<= 0) goto end
;
370 s
->state
=SSL3_ST_CR_CERT_REQ_A
;
373 /* at this point we check that we have the
374 * required stuff from the server */
375 if (!ssl3_check_cert_and_algorithm(s
))
382 case SSL3_ST_CR_CERT_REQ_A
:
383 case SSL3_ST_CR_CERT_REQ_B
:
384 ret
=ssl3_get_certificate_request(s
);
385 if (ret
<= 0) goto end
;
386 s
->state
=SSL3_ST_CR_SRVR_DONE_A
;
390 case SSL3_ST_CR_SRVR_DONE_A
:
391 case SSL3_ST_CR_SRVR_DONE_B
:
392 ret
=ssl3_get_server_done(s
);
393 if (ret
<= 0) goto end
;
394 #ifndef OPENSSL_NO_SRP
395 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
)
397 if ((ret
= SRP_Calc_A_param(s
))<=0)
399 SSLerr(SSL_F_SSL3_CONNECT
,SSL_R_SRP_A_CALC
);
400 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_INTERNAL_ERROR
);
405 if (s
->s3
->tmp
.cert_req
)
406 s
->state
=SSL3_ST_CW_CERT_A
;
408 s
->state
=SSL3_ST_CW_KEY_EXCH_A
;
413 case SSL3_ST_CW_CERT_A
:
414 case SSL3_ST_CW_CERT_B
:
415 case SSL3_ST_CW_CERT_C
:
416 case SSL3_ST_CW_CERT_D
:
417 ret
=ssl3_send_client_certificate(s
);
418 if (ret
<= 0) goto end
;
419 s
->state
=SSL3_ST_CW_KEY_EXCH_A
;
423 case SSL3_ST_CW_KEY_EXCH_A
:
424 case SSL3_ST_CW_KEY_EXCH_B
:
425 ret
=ssl3_send_client_key_exchange(s
);
426 if (ret
<= 0) goto end
;
427 /* EAY EAY EAY need to check for DH fix cert
429 /* For TLS, cert_req is set to 2, so a cert chain
430 * of nothing is sent, but no verify packet is sent */
431 /* XXX: For now, we do not support client
432 * authentication in ECDH cipher suites with
433 * ECDH (rather than ECDSA) certificates.
434 * We need to skip the certificate verify
435 * message when client's ECDH public key is sent
436 * inside the client certificate.
438 if (s
->s3
->tmp
.cert_req
== 1)
440 s
->state
=SSL3_ST_CW_CERT_VRFY_A
;
444 s
->state
=SSL3_ST_CW_CHANGE_A
;
445 s
->s3
->change_cipher_spec
=0;
447 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
)
449 s
->state
=SSL3_ST_CW_CHANGE_A
;
450 s
->s3
->change_cipher_spec
=0;
456 case SSL3_ST_CW_CERT_VRFY_A
:
457 case SSL3_ST_CW_CERT_VRFY_B
:
458 ret
=ssl3_send_client_verify(s
);
459 if (ret
<= 0) goto end
;
460 s
->state
=SSL3_ST_CW_CHANGE_A
;
462 s
->s3
->change_cipher_spec
=0;
465 case SSL3_ST_CW_CHANGE_A
:
466 case SSL3_ST_CW_CHANGE_B
:
467 ret
=ssl3_send_change_cipher_spec(s
,
468 SSL3_ST_CW_CHANGE_A
,SSL3_ST_CW_CHANGE_B
);
469 if (ret
<= 0) goto end
;
471 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
472 s
->state
=SSL3_ST_CW_FINISHED_A
;
474 if (s
->s3
->next_proto_neg_seen
)
475 s
->state
=SSL3_ST_CW_NEXT_PROTO_A
;
477 s
->state
=SSL3_ST_CW_FINISHED_A
;
481 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
482 #ifdef OPENSSL_NO_COMP
483 s
->session
->compress_meth
=0;
485 if (s
->s3
->tmp
.new_compression
== NULL
)
486 s
->session
->compress_meth
=0;
488 s
->session
->compress_meth
=
489 s
->s3
->tmp
.new_compression
->id
;
491 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
497 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
498 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
506 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
507 case SSL3_ST_CW_NEXT_PROTO_A
:
508 case SSL3_ST_CW_NEXT_PROTO_B
:
509 ret
=ssl3_send_next_proto(s
);
510 if (ret
<= 0) goto end
;
511 s
->state
=SSL3_ST_CW_FINISHED_A
;
515 case SSL3_ST_CW_FINISHED_A
:
516 case SSL3_ST_CW_FINISHED_B
:
517 ret
=ssl3_send_finished(s
,
518 SSL3_ST_CW_FINISHED_A
,SSL3_ST_CW_FINISHED_B
,
519 s
->method
->ssl3_enc
->client_finished_label
,
520 s
->method
->ssl3_enc
->client_finished_label_len
);
521 if (ret
<= 0) goto end
;
522 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
523 s
->state
=SSL3_ST_CW_FLUSH
;
526 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
529 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
530 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
)
533 s
->s3
->flags
|=SSL3_FLAGS_POP_BUFFER
;
534 s
->s3
->delay_buf_pop_ret
=0;
539 #ifndef OPENSSL_NO_TLSEXT
540 /* Allow NewSessionTicket if ticket expected */
541 if (s
->tlsext_ticket_expected
)
542 s
->s3
->tmp
.next_state
=SSL3_ST_CR_SESSION_TICKET_A
;
546 s
->s3
->tmp
.next_state
=SSL3_ST_CR_FINISHED_A
;
551 #ifndef OPENSSL_NO_TLSEXT
552 case SSL3_ST_CR_SESSION_TICKET_A
:
553 case SSL3_ST_CR_SESSION_TICKET_B
:
554 ret
=ssl3_get_new_session_ticket(s
);
555 if (ret
<= 0) goto end
;
556 s
->state
=SSL3_ST_CR_FINISHED_A
;
560 case SSL3_ST_CR_CERT_STATUS_A
:
561 case SSL3_ST_CR_CERT_STATUS_B
:
562 ret
=ssl3_get_cert_status(s
);
563 if (ret
<= 0) goto end
;
564 s
->state
=SSL3_ST_CR_KEY_EXCH_A
;
569 case SSL3_ST_CR_FINISHED_A
:
570 case SSL3_ST_CR_FINISHED_B
:
572 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
573 ret
=ssl3_get_finished(s
,SSL3_ST_CR_FINISHED_A
,
574 SSL3_ST_CR_FINISHED_B
);
575 if (ret
<= 0) goto end
;
578 s
->state
=SSL3_ST_CW_CHANGE_A
;
584 case SSL3_ST_CW_FLUSH
:
585 s
->rwstate
=SSL_WRITING
;
586 if (BIO_flush(s
->wbio
) <= 0)
591 s
->rwstate
=SSL_NOTHING
;
592 s
->state
=s
->s3
->tmp
.next_state
;
596 /* clean a few things up */
597 ssl3_cleanup_key_block(s
);
599 if (s
->init_buf
!= NULL
)
601 BUF_MEM_free(s
->init_buf
);
605 /* If we are not 'joining' the last two packets,
606 * remove the buffering now */
607 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
608 ssl_free_wbio_buffer(s
);
609 /* else do it later in ssl3_write */
615 ssl_update_cache(s
,SSL_SESS_CACHE_CLIENT
);
616 if (s
->hit
) s
->ctx
->stats
.sess_hit
++;
620 s
->handshake_func
=ssl3_connect
;
621 s
->ctx
->stats
.sess_connect_good
++;
623 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
629 SSLerr(SSL_F_SSL3_CONNECT
,SSL_R_UNKNOWN_STATE
);
635 /* did we do anything */
636 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
640 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
644 if ((cb
!= NULL
) && (s
->state
!= state
))
648 cb(s
,SSL_CB_CONNECT_LOOP
,1);
659 cb(s
,SSL_CB_CONNECT_EXIT
,ret
);
664 int ssl3_client_hello(SSL
*s
)
671 #ifndef OPENSSL_NO_COMP
676 buf
=(unsigned char *)s
->init_buf
->data
;
677 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
)
679 SSL_SESSION
*sess
= s
->session
;
680 if ((sess
== NULL
) ||
681 (sess
->ssl_version
!= s
->version
) ||
682 #ifdef OPENSSL_NO_TLSEXT
683 !sess
->session_id_length
||
685 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
687 (sess
->not_resumable
))
689 if (!ssl_get_new_session(s
,0))
692 if (s
->method
->version
== DTLS_ANY_VERSION
)
694 /* Determine which DTLS version to use */
695 int options
= s
->options
;
696 /* If DTLS 1.2 disabled correct the version number */
697 if (options
& SSL_OP_NO_DTLSv1_2
)
701 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
704 /* Disabling all versions is silly: return an
707 if (options
& SSL_OP_NO_DTLSv1
)
709 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,SSL_R_WRONG_SSL_VERSION
);
712 /* Update method so we don't use any DTLS 1.2
715 s
->method
= DTLSv1_client_method();
716 s
->version
= DTLS1_VERSION
;
720 /* We only support one version: update method */
721 if (options
& SSL_OP_NO_DTLSv1
)
722 s
->method
= DTLSv1_2_client_method();
723 s
->version
= DTLS1_2_VERSION
;
725 s
->client_version
= s
->version
;
727 /* else use the pre-loaded session */
729 p
=s
->s3
->client_random
;
731 /* for DTLS if client_random is initialized, reuse it, we are
732 * required to use same upon reply to HelloVerify */
737 for (idx
=0; idx
< sizeof(s
->s3
->client_random
); idx
++)
750 ssl_fill_hello_random(s
, 0, p
,
751 sizeof(s
->s3
->client_random
));
753 /* Do the message type and length last */
754 d
=p
= ssl_handshake_start(s
);
756 /* version indicates the negotiated version: for example from
757 * an SSLv2/v3 compatible client hello). The client_version
758 * field is the maximum version we permit and it is also
759 * used in RSA encrypted premaster secrets. Some servers can
760 * choke if we initially report a higher version then
761 * renegotiate to a lower one in the premaster secret. This
762 * didn't happen with TLS 1.0 as most servers supported it
763 * but it can with TLS 1.1 or later if the server only supports
766 * Possible scenario with previous logic:
767 * 1. Client hello indicates TLS 1.2
768 * 2. Server hello says TLS 1.0
769 * 3. RSA encrypted premaster secret uses 1.2.
770 * 4. Handhaked proceeds using TLS 1.0.
771 * 5. Server sends hello request to renegotiate.
772 * 6. Client hello indicates TLS v1.0 as we now
773 * know that is maximum server supports.
774 * 7. Server chokes on RSA encrypted premaster secret
775 * containing version 1.0.
777 * For interoperability it should be OK to always use the
778 * maximum version we support in client hello and then rely
779 * on the checking of version to ensure the servers isn't
780 * being inconsistent: for example initially negotiating with
781 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
782 * client_version in client hello and not resetting it to
783 * the negotiated version.
786 *(p
++)=s
->version
>>8;
787 *(p
++)=s
->version
&0xff;
788 s
->client_version
=s
->version
;
790 *(p
++)=s
->client_version
>>8;
791 *(p
++)=s
->client_version
&0xff;
795 memcpy(p
,s
->s3
->client_random
,SSL3_RANDOM_SIZE
);
802 i
=s
->session
->session_id_length
;
806 if (i
> (int)sizeof(s
->session
->session_id
))
808 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
811 memcpy(p
,s
->session
->session_id
,i
);
815 /* cookie stuff for DTLS */
818 if ( s
->d1
->cookie_len
> sizeof(s
->d1
->cookie
))
820 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
823 *(p
++) = s
->d1
->cookie_len
;
824 memcpy(p
, s
->d1
->cookie
, s
->d1
->cookie_len
);
825 p
+= s
->d1
->cookie_len
;
828 /* Ciphers supported */
829 i
=ssl_cipher_list_to_bytes(s
,SSL_get_ciphers(s
),&(p
[2]),0);
832 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,SSL_R_NO_CIPHERS_AVAILABLE
);
835 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
836 /* Some servers hang if client hello > 256 bytes
837 * as hack workaround chop number of supported ciphers
838 * to keep it well below this if we use TLS v1.2
840 if (TLS1_get_version(s
) >= TLS1_2_VERSION
841 && i
> OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
)
842 i
= OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
& ~1;
848 #ifdef OPENSSL_NO_COMP
852 if (!ssl_allow_compression(s
) || !s
->ctx
->comp_methods
)
855 j
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
859 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,i
);
863 *(p
++)=0; /* Add the NULL method */
865 #ifndef OPENSSL_NO_TLSEXT
867 if (ssl_prepare_clienthello_tlsext(s
) <= 0)
869 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,SSL_R_CLIENTHELLO_TLSEXT
);
872 if ((p
= ssl_add_clienthello_tlsext(s
, p
, buf
+SSL3_RT_MAX_PLAIN_LENGTH
, &al
)) == NULL
)
874 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
875 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,ERR_R_INTERNAL_ERROR
);
881 ssl_set_handshake_header(s
, SSL3_MT_CLIENT_HELLO
, l
);
882 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
885 /* SSL3_ST_CW_CLNT_HELLO_B */
886 return ssl_do_write(s
);
891 int ssl3_get_server_hello(SSL
*s
)
893 STACK_OF(SSL_CIPHER
) *sk
;
897 int i
,al
=SSL_AD_INTERNAL_ERROR
,ok
;
900 #ifndef OPENSSL_NO_COMP
903 /* Hello verify request and/or server hello version may not
904 * match so set first packet if we're negotiating version.
909 n
=s
->method
->ssl_get_message(s
,
910 SSL3_ST_CR_SRVR_HELLO_A
,
911 SSL3_ST_CR_SRVR_HELLO_B
,
916 if (!ok
) return((int)n
);
921 if ( s
->s3
->tmp
.message_type
== DTLS1_MT_HELLO_VERIFY_REQUEST
)
923 if ( s
->d1
->send_cookie
== 0)
925 s
->s3
->tmp
.reuse_message
= 1;
928 else /* already sent a cookie */
930 al
=SSL_AD_UNEXPECTED_MESSAGE
;
931 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_BAD_MESSAGE_TYPE
);
937 if ( s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_HELLO
)
939 al
=SSL_AD_UNEXPECTED_MESSAGE
;
940 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_BAD_MESSAGE_TYPE
);
944 d
=p
=(unsigned char *)s
->init_msg
;
945 if (s
->method
->version
== DTLS_ANY_VERSION
)
947 /* Work out correct protocol version to use */
948 int hversion
= (p
[0] << 8)|p
[1];
949 int options
= s
->options
;
950 if (hversion
== DTLS1_2_VERSION
951 && !(options
& SSL_OP_NO_DTLSv1_2
))
952 s
->method
= DTLSv1_2_client_method();
953 else if (tls1_suiteb(s
))
955 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
956 s
->version
= hversion
;
957 al
= SSL_AD_PROTOCOL_VERSION
;
960 else if (hversion
== DTLS1_VERSION
961 && !(options
& SSL_OP_NO_DTLSv1
))
962 s
->method
= DTLSv1_client_method();
965 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_SSL_VERSION
);
966 s
->version
= hversion
;
967 al
= SSL_AD_PROTOCOL_VERSION
;
970 s
->version
= s
->client_version
= s
->method
->version
;
973 if ((p
[0] != (s
->version
>>8)) || (p
[1] != (s
->version
&0xff)))
975 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_SSL_VERSION
);
976 s
->version
=(s
->version
&0xff00)|p
[1];
977 al
=SSL_AD_PROTOCOL_VERSION
;
982 /* load the server hello data */
983 /* load the server random */
984 memcpy(s
->s3
->server_random
,p
,SSL3_RANDOM_SIZE
);
989 /* get the session-id */
992 if ((j
> sizeof s
->session
->session_id
) || (j
> SSL3_SESSION_ID_SIZE
))
994 al
=SSL_AD_ILLEGAL_PARAMETER
;
995 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_SSL3_SESSION_ID_TOO_LONG
);
999 #ifndef OPENSSL_NO_TLSEXT
1000 /* check if we want to resume the session based on external pre-shared secret */
1001 if (s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
)
1003 SSL_CIPHER
*pref_cipher
=NULL
;
1004 s
->session
->master_key_length
=sizeof(s
->session
->master_key
);
1005 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
1006 &s
->session
->master_key_length
,
1008 s
->tls_session_secret_cb_arg
))
1010 s
->session
->cipher
= pref_cipher
?
1011 pref_cipher
: ssl_get_cipher_by_char(s
, p
+j
);
1013 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
1016 #endif /* OPENSSL_NO_TLSEXT */
1018 if (!s
->hit
&& j
!= 0 && j
== s
->session
->session_id_length
1019 && memcmp(p
,s
->session
->session_id
,j
) == 0)
1021 if(s
->sid_ctx_length
!= s
->session
->sid_ctx_length
1022 || memcmp(s
->session
->sid_ctx
,s
->sid_ctx
,s
->sid_ctx_length
))
1024 /* actually a client application bug */
1025 al
=SSL_AD_ILLEGAL_PARAMETER
;
1026 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
1029 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
1032 /* a miss or crap from the other end */
1035 /* If we were trying for session-id reuse, make a new
1036 * SSL_SESSION so we don't stuff up other people */
1037 if (s
->session
->session_id_length
> 0)
1039 if (!ssl_get_new_session(s
,0))
1044 s
->session
->session_id_length
=j
;
1045 memcpy(s
->session
->session_id
,p
,j
); /* j could be 0 */
1048 c
=ssl_get_cipher_by_char(s
,p
);
1051 /* unknown cipher */
1052 al
=SSL_AD_ILLEGAL_PARAMETER
;
1053 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNKNOWN_CIPHER_RETURNED
);
1056 /* Set version disabled mask now we know version */
1057 if (!SSL_USE_TLS1_2_CIPHERS(s
))
1058 ct
->mask_ssl
= SSL_TLSV1_2
;
1061 /* If it is a disabled cipher we didn't send it in client hello,
1062 * so return an error.
1064 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_CHECK
))
1066 al
=SSL_AD_ILLEGAL_PARAMETER
;
1067 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_CIPHER_RETURNED
);
1070 p
+=ssl_put_cipher_by_char(s
,NULL
,NULL
);
1072 sk
=ssl_get_ciphers_by_id(s
);
1073 i
=sk_SSL_CIPHER_find(sk
,c
);
1076 /* we did not say we would use this cipher */
1077 al
=SSL_AD_ILLEGAL_PARAMETER
;
1078 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_CIPHER_RETURNED
);
1082 /* Depending on the session caching (internal/external), the cipher
1083 and/or cipher_id values may not be set. Make sure that
1084 cipher_id is set and use it for comparison. */
1085 if (s
->session
->cipher
)
1086 s
->session
->cipher_id
= s
->session
->cipher
->id
;
1087 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
))
1089 /* Workaround is now obsolete */
1092 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
))
1095 al
=SSL_AD_ILLEGAL_PARAMETER
;
1096 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
1100 s
->s3
->tmp
.new_cipher
=c
;
1101 /* Don't digest cached records if no sigalgs: we may need them for
1102 * client authentication.
1104 if (!SSL_USE_SIGALGS(s
) && !ssl3_digest_cached_records(s
))
1106 /* lets get the compression algorithm */
1108 #ifdef OPENSSL_NO_COMP
1111 al
=SSL_AD_ILLEGAL_PARAMETER
;
1112 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1115 /* If compression is disabled we'd better not try to resume a session
1116 * using compression.
1118 if (s
->session
->compress_meth
!= 0)
1120 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_INCONSISTENT_COMPRESSION
);
1125 if (s
->hit
&& j
!= s
->session
->compress_meth
)
1127 al
=SSL_AD_ILLEGAL_PARAMETER
;
1128 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED
);
1133 else if (!ssl_allow_compression(s
))
1135 al
=SSL_AD_ILLEGAL_PARAMETER
;
1136 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_COMPRESSION_DISABLED
);
1140 comp
=ssl3_comp_find(s
->ctx
->comp_methods
,j
);
1142 if ((j
!= 0) && (comp
== NULL
))
1144 al
=SSL_AD_ILLEGAL_PARAMETER
;
1145 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1150 s
->s3
->tmp
.new_compression
=comp
;
1154 #ifndef OPENSSL_NO_TLSEXT
1156 if (!ssl_parse_serverhello_tlsext(s
,&p
,d
,n
))
1158 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_PARSE_TLSEXT
);
1165 /* wrong packet length */
1166 al
=SSL_AD_DECODE_ERROR
;
1167 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_BAD_PACKET_LENGTH
);
1173 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1178 int ssl3_get_server_certificate(SSL
*s
)
1180 int al
,i
,ok
,ret
= -1;
1181 unsigned long n
,nc
,llen
,l
;
1183 const unsigned char *q
,*p
;
1185 STACK_OF(X509
) *sk
=NULL
;
1187 EVP_PKEY
*pkey
=NULL
;
1188 int need_cert
= 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
1190 n
=s
->method
->ssl_get_message(s
,
1197 if (!ok
) return((int)n
);
1199 if ((s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
) ||
1200 ((s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
) &&
1201 (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
)))
1203 s
->s3
->tmp
.reuse_message
=1;
1207 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
1209 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1210 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_BAD_MESSAGE_TYPE
);
1213 p
=d
=(unsigned char *)s
->init_msg
;
1215 if ((sk
=sk_X509_new_null()) == NULL
)
1217 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1224 al
=SSL_AD_DECODE_ERROR
;
1225 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
1228 for (nc
=0; nc
<llen
; )
1231 if ((l
+nc
+3) > llen
)
1233 al
=SSL_AD_DECODE_ERROR
;
1234 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1239 x
=d2i_X509(NULL
,&q
,l
);
1242 al
=SSL_AD_BAD_CERTIFICATE
;
1243 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_ASN1_LIB
);
1248 al
=SSL_AD_DECODE_ERROR
;
1249 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1252 if (!sk_X509_push(sk
,x
))
1254 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1262 i
=ssl_verify_cert_chain(s
,sk
);
1263 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (i
<= 0)
1264 #ifndef OPENSSL_NO_KRB5
1265 && !((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
) &&
1266 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
))
1267 #endif /* OPENSSL_NO_KRB5 */
1270 al
=ssl_verify_alarm_type(s
->verify_result
);
1271 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERTIFICATE_VERIFY_FAILED
);
1274 ERR_clear_error(); /* but we keep s->verify_result */
1277 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
, i
);
1278 al
= SSL_AD_HANDSHAKE_FAILURE
;
1282 sc
=ssl_sess_cert_new();
1283 if (sc
== NULL
) goto err
;
1285 if (s
->session
->sess_cert
) ssl_sess_cert_free(s
->session
->sess_cert
);
1286 s
->session
->sess_cert
=sc
;
1289 /* Inconsistency alert: cert_chain does include the peer's
1290 * certificate, which we don't include in s3_srvr.c */
1291 x
=sk_X509_value(sk
,0);
1293 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1295 pkey
=X509_get_pubkey(x
);
1297 /* VRS: allow null cert if auth == KRB5 */
1298 need_cert
= ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
) &&
1299 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
))
1303 printf("pkey,x = %p, %p\n", pkey
,x
);
1304 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x
,pkey
));
1305 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s
->s3
->tmp
.new_cipher
->name
,
1306 s
->s3
->tmp
.new_cipher
->algorithm_mkey
, s
->s3
->tmp
.new_cipher
->algorithm_auth
, need_cert
);
1307 #endif /* KSSL_DEBUG */
1309 if (need_cert
&& ((pkey
== NULL
) || EVP_PKEY_missing_parameters(pkey
)))
1313 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1314 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1318 i
=ssl_cert_type(x
,pkey
);
1319 if (need_cert
&& i
< 0)
1323 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1324 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1330 int exp_idx
= ssl_cipher_get_cert_index(s
->s3
->tmp
.new_cipher
);
1331 if (exp_idx
>= 0 && i
!= exp_idx
)
1334 al
=SSL_AD_ILLEGAL_PARAMETER
;
1335 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
1336 SSL_R_WRONG_CERTIFICATE_TYPE
);
1339 sc
->peer_cert_type
=i
;
1340 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
1341 /* Why would the following ever happen?
1342 * We just created sc a couple of lines ago. */
1343 if (sc
->peer_pkeys
[i
].x509
!= NULL
)
1344 X509_free(sc
->peer_pkeys
[i
].x509
);
1345 sc
->peer_pkeys
[i
].x509
=x
;
1346 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
1348 if (s
->session
->peer
!= NULL
)
1349 X509_free(s
->session
->peer
);
1350 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
1355 sc
->peer_cert_type
=i
;
1358 if (s
->session
->peer
!= NULL
)
1359 X509_free(s
->session
->peer
);
1360 s
->session
->peer
=NULL
;
1362 s
->session
->verify_result
= s
->verify_result
;
1369 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1372 EVP_PKEY_free(pkey
);
1374 sk_X509_pop_free(sk
,X509_free
);
1378 int ssl3_get_key_exchange(SSL
*s
)
1380 #ifndef OPENSSL_NO_RSA
1381 unsigned char *q
,md_buf
[EVP_MAX_MD_SIZE
*2];
1384 unsigned char *param
,*p
;
1386 long i
,param_len
,n
,alg_k
,alg_a
;
1387 EVP_PKEY
*pkey
=NULL
;
1388 const EVP_MD
*md
= NULL
;
1389 #ifndef OPENSSL_NO_RSA
1392 #ifndef OPENSSL_NO_DH
1395 #ifndef OPENSSL_NO_ECDH
1396 EC_KEY
*ecdh
= NULL
;
1397 BN_CTX
*bn_ctx
= NULL
;
1398 EC_POINT
*srvr_ecpoint
= NULL
;
1400 int encoded_pt_len
= 0;
1403 /* use same message size as in ssl3_get_certificate_request()
1404 * as ServerKeyExchange message may be skipped */
1405 n
=s
->method
->ssl_get_message(s
,
1406 SSL3_ST_CR_KEY_EXCH_A
,
1407 SSL3_ST_CR_KEY_EXCH_B
,
1411 if (!ok
) return((int)n
);
1413 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
)
1415 #ifndef OPENSSL_NO_PSK
1416 /* In plain PSK ciphersuite, ServerKeyExchange can be
1417 omitted if no identity hint is sent. Set
1418 session->sess_cert anyway to avoid problems
1420 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
)
1422 s
->session
->sess_cert
=ssl_sess_cert_new();
1423 if (s
->ctx
->psk_identity_hint
)
1424 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1425 s
->ctx
->psk_identity_hint
= NULL
;
1428 s
->s3
->tmp
.reuse_message
=1;
1432 param
=p
=(unsigned char *)s
->init_msg
;
1433 if (s
->session
->sess_cert
!= NULL
)
1435 #ifndef OPENSSL_NO_RSA
1436 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
1438 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
1439 s
->session
->sess_cert
->peer_rsa_tmp
=NULL
;
1442 #ifndef OPENSSL_NO_DH
1443 if (s
->session
->sess_cert
->peer_dh_tmp
)
1445 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
1446 s
->session
->sess_cert
->peer_dh_tmp
=NULL
;
1449 #ifndef OPENSSL_NO_ECDH
1450 if (s
->session
->sess_cert
->peer_ecdh_tmp
)
1452 EC_KEY_free(s
->session
->sess_cert
->peer_ecdh_tmp
);
1453 s
->session
->sess_cert
->peer_ecdh_tmp
=NULL
;
1459 s
->session
->sess_cert
=ssl_sess_cert_new();
1462 /* Total length of the parameters including the length prefix */
1465 alg_k
=s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1466 alg_a
=s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1467 EVP_MD_CTX_init(&md_ctx
);
1469 al
=SSL_AD_DECODE_ERROR
;
1471 #ifndef OPENSSL_NO_PSK
1472 if (alg_k
& SSL_kPSK
)
1474 char tmp_id_hint
[PSK_MAX_IDENTITY_LEN
+1];
1479 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1480 SSL_R_LENGTH_TOO_SHORT
);
1485 /* Store PSK identity hint for later use, hint is used
1486 * in ssl3_send_client_key_exchange. Assume that the
1487 * maximum length of a PSK identity hint can be as
1488 * long as the maximum length of a PSK identity. */
1489 if (i
> PSK_MAX_IDENTITY_LEN
)
1491 al
=SSL_AD_HANDSHAKE_FAILURE
;
1492 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1493 SSL_R_DATA_LENGTH_TOO_LONG
);
1496 if (i
> n
- param_len
)
1498 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1499 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH
);
1504 /* If received PSK identity hint contains NULL
1505 * characters, the hint is truncated from the first
1506 * NULL. p may not be ending with NULL, so create a
1507 * NULL-terminated string. */
1508 memcpy(tmp_id_hint
, p
, i
);
1509 memset(tmp_id_hint
+i
, 0, PSK_MAX_IDENTITY_LEN
+1-i
);
1510 if (s
->ctx
->psk_identity_hint
!= NULL
)
1511 OPENSSL_free(s
->ctx
->psk_identity_hint
);
1512 s
->ctx
->psk_identity_hint
= BUF_strdup(tmp_id_hint
);
1513 if (s
->ctx
->psk_identity_hint
== NULL
)
1515 al
=SSL_AD_HANDSHAKE_FAILURE
;
1516 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1524 #endif /* !OPENSSL_NO_PSK */
1525 #ifndef OPENSSL_NO_SRP
1526 if (alg_k
& SSL_kSRP
)
1531 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1532 SSL_R_LENGTH_TOO_SHORT
);
1537 if (i
> n
- param_len
)
1539 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SRP_N_LENGTH
);
1544 if (!(s
->srp_ctx
.N
=BN_bin2bn(p
,i
,NULL
)))
1546 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1552 if (2 > n
- param_len
)
1554 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1555 SSL_R_LENGTH_TOO_SHORT
);
1562 if (i
> n
- param_len
)
1564 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SRP_G_LENGTH
);
1569 if (!(s
->srp_ctx
.g
=BN_bin2bn(p
,i
,NULL
)))
1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1577 if (1 > n
- param_len
)
1579 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1580 SSL_R_LENGTH_TOO_SHORT
);
1585 i
= (unsigned int)(p
[0]);
1588 if (i
> n
- param_len
)
1590 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SRP_S_LENGTH
);
1595 if (!(s
->srp_ctx
.s
=BN_bin2bn(p
,i
,NULL
)))
1597 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1602 if (2 > n
- param_len
)
1604 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1605 SSL_R_LENGTH_TOO_SHORT
);
1612 if (i
> n
- param_len
)
1614 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SRP_B_LENGTH
);
1619 if (!(s
->srp_ctx
.B
=BN_bin2bn(p
,i
,NULL
)))
1621 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1627 if (!srp_verify_server_param(s
, &al
))
1629 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SRP_PARAMETERS
);
1633 /* We must check if there is a certificate */
1634 #ifndef OPENSSL_NO_RSA
1635 if (alg_a
& SSL_aRSA
)
1636 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1641 #ifndef OPENSSL_NO_DSA
1642 else if (alg_a
& SSL_aDSS
)
1643 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].x509
);
1647 #endif /* !OPENSSL_NO_SRP */
1648 #ifndef OPENSSL_NO_RSA
1649 if (alg_k
& SSL_kRSA
)
1651 if ((rsa
=RSA_new()) == NULL
)
1653 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
1660 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1661 SSL_R_LENGTH_TOO_SHORT
);
1666 if (i
> n
- param_len
)
1668 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_MODULUS_LENGTH
);
1673 if (!(rsa
->n
=BN_bin2bn(p
,i
,rsa
->n
)))
1675 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1680 if (2 > n
- param_len
)
1682 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1683 SSL_R_LENGTH_TOO_SHORT
);
1690 if (i
> n
- param_len
)
1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_E_LENGTH
);
1697 if (!(rsa
->e
=BN_bin2bn(p
,i
,rsa
->e
)))
1699 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1705 /* this should be because we are using an export cipher */
1706 if (alg_a
& SSL_aRSA
)
1707 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1710 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1713 s
->session
->sess_cert
->peer_rsa_tmp
=rsa
;
1716 #else /* OPENSSL_NO_RSA */
1720 #ifndef OPENSSL_NO_DH
1721 else if (alg_k
& SSL_kDHE
)
1723 if ((dh
=DH_new()) == NULL
)
1725 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1732 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1733 SSL_R_LENGTH_TOO_SHORT
);
1738 if (i
> n
- param_len
)
1740 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_P_LENGTH
);
1745 if (!(dh
->p
=BN_bin2bn(p
,i
,NULL
)))
1747 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1752 if (2 > n
- param_len
)
1754 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1755 SSL_R_LENGTH_TOO_SHORT
);
1762 if (i
> n
- param_len
)
1764 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_G_LENGTH
);
1769 if (!(dh
->g
=BN_bin2bn(p
,i
,NULL
)))
1771 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1776 if (2 > n
- param_len
)
1778 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1779 SSL_R_LENGTH_TOO_SHORT
);
1786 if (i
> n
- param_len
)
1788 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1793 if (!(dh
->pub_key
=BN_bin2bn(p
,i
,NULL
)))
1795 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1801 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
1802 DH_security_bits(dh
), 0, dh
))
1804 al
=SSL_AD_HANDSHAKE_FAILURE
;
1805 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_DH_KEY_TOO_SMALL
);
1809 #ifndef OPENSSL_NO_RSA
1810 if (alg_a
& SSL_aRSA
)
1811 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1816 #ifndef OPENSSL_NO_DSA
1817 else if (alg_a
& SSL_aDSS
)
1818 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].x509
);
1820 /* else anonymous DH, so no certificate or pkey. */
1822 s
->session
->sess_cert
->peer_dh_tmp
=dh
;
1825 else if ((alg_k
& SSL_kDHr
) || (alg_k
& SSL_kDHd
))
1827 al
=SSL_AD_ILLEGAL_PARAMETER
;
1828 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1831 #endif /* !OPENSSL_NO_DH */
1833 #ifndef OPENSSL_NO_ECDH
1834 else if (alg_k
& SSL_kECDHE
)
1837 const EC_GROUP
*group
;
1839 if ((ecdh
=EC_KEY_new()) == NULL
)
1841 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
1845 /* Extract elliptic curve parameters and the
1846 * server's ephemeral ECDH public key.
1847 * Keep accumulating lengths of various components in
1848 * param_len and make sure it never exceeds n.
1851 /* XXX: For now we only support named (not generic) curves
1852 * and the ECParameters in this case is just three bytes. We
1853 * also need one byte for the length of the encoded point
1858 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1859 SSL_R_LENGTH_TOO_SHORT
);
1862 /* Check curve is one of our preferences, if not server has
1863 * sent an invalid curve. ECParameters is 3 bytes.
1865 if (!tls1_check_curve(s
, p
, 3))
1867 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_WRONG_CURVE
);
1871 if ((curve_nid
= tls1_ec_curve_id2nid(*(p
+ 2))) == 0)
1873 al
=SSL_AD_INTERNAL_ERROR
;
1874 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1878 ngroup
= EC_GROUP_new_by_curve_name(curve_nid
);
1881 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_EC_LIB
);
1884 if (EC_KEY_set_group(ecdh
, ngroup
) == 0)
1886 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_EC_LIB
);
1889 EC_GROUP_free(ngroup
);
1891 group
= EC_KEY_get0_group(ecdh
);
1893 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1894 (EC_GROUP_get_degree(group
) > 163))
1896 al
=SSL_AD_EXPORT_RESTRICTION
;
1897 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1903 /* Next, get the encoded ECPoint */
1904 if (((srvr_ecpoint
= EC_POINT_new(group
)) == NULL
) ||
1905 ((bn_ctx
= BN_CTX_new()) == NULL
))
1907 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
1911 encoded_pt_len
= *p
; /* length of encoded point */
1914 if ((encoded_pt_len
> n
- param_len
) ||
1915 (EC_POINT_oct2point(group
, srvr_ecpoint
,
1916 p
, encoded_pt_len
, bn_ctx
) == 0))
1918 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_ECPOINT
);
1921 param_len
+= encoded_pt_len
;
1926 /* The ECC/TLS specification does not mention
1927 * the use of DSA to sign ECParameters in the server
1928 * key exchange message. We do support RSA and ECDSA.
1931 #ifndef OPENSSL_NO_RSA
1932 else if (alg_a
& SSL_aRSA
)
1933 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1935 #ifndef OPENSSL_NO_ECDSA
1936 else if (alg_a
& SSL_aECDSA
)
1937 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
1939 /* else anonymous ECDH, so no certificate or pkey. */
1940 EC_KEY_set_public_key(ecdh
, srvr_ecpoint
);
1941 s
->session
->sess_cert
->peer_ecdh_tmp
=ecdh
;
1943 BN_CTX_free(bn_ctx
);
1945 EC_POINT_free(srvr_ecpoint
);
1946 srvr_ecpoint
= NULL
;
1950 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1951 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_UNEXPECTED_MESSAGE
);
1954 #endif /* !OPENSSL_NO_ECDH */
1957 /* p points to the next byte, there are 'n' bytes left */
1959 /* if it was signed, check the signature */
1962 if (SSL_USE_SIGALGS(s
))
1967 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1968 SSL_R_LENGTH_TOO_SHORT
);
1971 rv
= tls12_check_peer_sigalg(&md
, s
, p
, pkey
);
1979 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
1989 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,
1990 SSL_R_LENGTH_TOO_SHORT
);
1995 j
=EVP_PKEY_size(pkey
);
1997 /* Check signature length. If n is 0 then signature is empty */
1998 if ((i
!= n
) || (n
> j
) || (n
<= 0))
2000 /* wrong packet length */
2001 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_WRONG_SIGNATURE_LENGTH
);
2005 #ifndef OPENSSL_NO_RSA
2006 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
))
2013 for (num
=2; num
> 0; num
--)
2015 EVP_MD_CTX_set_flags(&md_ctx
,
2016 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
2017 EVP_DigestInit_ex(&md_ctx
,(num
== 2)
2018 ?s
->ctx
->md5
:s
->ctx
->sha1
, NULL
);
2019 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
2020 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
2021 EVP_DigestUpdate(&md_ctx
,param
,param_len
);
2022 EVP_DigestFinal_ex(&md_ctx
,q
,&size
);
2026 i
=RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
,
2030 al
=SSL_AD_DECRYPT_ERROR
;
2031 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_DECRYPT
);
2037 al
=SSL_AD_DECRYPT_ERROR
;
2038 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SIGNATURE
);
2045 EVP_VerifyInit_ex(&md_ctx
, md
, NULL
);
2046 EVP_VerifyUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
2047 EVP_VerifyUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
2048 EVP_VerifyUpdate(&md_ctx
,param
,param_len
);
2049 if (EVP_VerifyFinal(&md_ctx
,p
,(int)n
,pkey
) <= 0)
2052 al
=SSL_AD_DECRYPT_ERROR
;
2053 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SIGNATURE
);
2060 /* aNULL, aSRP or kPSK do not need public keys */
2061 if (!(alg_a
& (SSL_aNULL
|SSL_aSRP
)) && !(alg_k
& SSL_kPSK
))
2063 /* Might be wrong key type, check it */
2064 if (ssl3_check_cert_and_algorithm(s
))
2065 /* Otherwise this shouldn't happen */
2066 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
2069 /* still data left over */
2072 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_EXTRA_DATA_IN_MESSAGE
);
2076 EVP_PKEY_free(pkey
);
2077 EVP_MD_CTX_cleanup(&md_ctx
);
2080 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
2082 EVP_PKEY_free(pkey
);
2083 #ifndef OPENSSL_NO_RSA
2087 #ifndef OPENSSL_NO_DH
2091 #ifndef OPENSSL_NO_ECDH
2092 BN_CTX_free(bn_ctx
);
2093 EC_POINT_free(srvr_ecpoint
);
2097 EVP_MD_CTX_cleanup(&md_ctx
);
2101 int ssl3_get_certificate_request(SSL
*s
)
2104 unsigned long n
,nc
,l
;
2105 unsigned int llen
, ctype_num
,i
;
2107 const unsigned char *p
,*q
;
2109 STACK_OF(X509_NAME
) *ca_sk
=NULL
;
2111 n
=s
->method
->ssl_get_message(s
,
2112 SSL3_ST_CR_CERT_REQ_A
,
2113 SSL3_ST_CR_CERT_REQ_B
,
2118 if (!ok
) return((int)n
);
2120 s
->s3
->tmp
.cert_req
=0;
2122 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
)
2124 s
->s3
->tmp
.reuse_message
=1;
2125 /* If we get here we don't need any cached handshake records
2126 * as we wont be doing client auth.
2128 if (s
->s3
->handshake_buffer
)
2130 if (!ssl3_digest_cached_records(s
))
2136 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
)
2138 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
2139 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_WRONG_MESSAGE_TYPE
);
2143 /* TLS does not like anon-DH with client cert */
2144 if (s
->version
> SSL3_VERSION
)
2146 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
2148 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
2149 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
2154 p
=d
=(unsigned char *)s
->init_msg
;
2156 if ((ca_sk
=sk_X509_NAME_new(ca_dn_cmp
)) == NULL
)
2158 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_MALLOC_FAILURE
);
2162 /* get the certificate types */
2164 if (s
->cert
->ctypes
)
2166 OPENSSL_free(s
->cert
->ctypes
);
2167 s
->cert
->ctypes
= NULL
;
2169 if (ctype_num
> SSL3_CT_NUMBER
)
2171 /* If we exceed static buffer copy all to cert structure */
2172 s
->cert
->ctypes
= OPENSSL_malloc(ctype_num
);
2173 memcpy(s
->cert
->ctypes
, p
, ctype_num
);
2174 s
->cert
->ctype_num
= (size_t)ctype_num
;
2175 ctype_num
=SSL3_CT_NUMBER
;
2177 for (i
=0; i
<ctype_num
; i
++)
2178 s
->s3
->tmp
.ctype
[i
]= p
[i
];
2180 if (SSL_USE_SIGALGS(s
))
2183 /* Check we have enough room for signature algorithms and
2184 * following length value.
2186 if ((unsigned long)(p
- d
+ llen
+ 2) > n
)
2188 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2189 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_DATA_LENGTH_TOO_LONG
);
2192 /* Clear certificate digests and validity flags */
2193 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
2195 s
->cert
->pkeys
[i
].digest
= NULL
;
2196 s
->cert
->pkeys
[i
].valid_flags
= 0;
2198 if ((llen
& 1) || !tls1_save_sigalgs(s
, p
, llen
))
2200 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2201 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
2204 if (!tls1_process_sigalgs(s
))
2206 ssl3_send_alert(s
,SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2207 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
2213 /* get the CA RDNs */
2218 out
=fopen("/tmp/vsign.der","w");
2219 fwrite(p
,1,llen
,out
);
2224 if ((unsigned long)(p
- d
+ llen
) != n
)
2226 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2227 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_LENGTH_MISMATCH
);
2231 for (nc
=0; nc
<llen
; )
2234 if ((l
+nc
+2) > llen
)
2236 if ((s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
2237 goto cont
; /* netscape bugs */
2238 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2239 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_CA_DN_TOO_LONG
);
2245 if ((xn
=d2i_X509_NAME(NULL
,&q
,l
)) == NULL
)
2247 /* If netscape tolerance is on, ignore errors */
2248 if (s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
)
2252 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2253 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_ASN1_LIB
);
2260 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2261 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_CA_DN_LENGTH_MISMATCH
);
2264 if (!sk_X509_NAME_push(ca_sk
,xn
))
2266 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_MALLOC_FAILURE
);
2280 /* we should setup a certificate to return.... */
2281 s
->s3
->tmp
.cert_req
=1;
2282 s
->s3
->tmp
.ctype_num
=ctype_num
;
2283 if (s
->s3
->tmp
.ca_names
!= NULL
)
2284 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
,X509_NAME_free
);
2285 s
->s3
->tmp
.ca_names
=ca_sk
;
2290 if (ca_sk
!= NULL
) sk_X509_NAME_pop_free(ca_sk
,X509_NAME_free
);
2294 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
2296 return(X509_NAME_cmp(*a
,*b
));
2298 #ifndef OPENSSL_NO_TLSEXT
2299 int ssl3_get_new_session_ticket(SSL
*s
)
2301 int ok
,al
,ret
=0, ticklen
;
2303 const unsigned char *p
;
2306 n
=s
->method
->ssl_get_message(s
,
2307 SSL3_ST_CR_SESSION_TICKET_A
,
2308 SSL3_ST_CR_SESSION_TICKET_B
,
2316 if (s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
)
2318 s
->s3
->tmp
.reuse_message
=1;
2321 if (s
->s3
->tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
)
2323 al
=SSL_AD_UNEXPECTED_MESSAGE
;
2324 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,SSL_R_BAD_MESSAGE_TYPE
);
2329 /* need at least ticket_lifetime_hint + ticket length */
2330 al
= SSL_AD_DECODE_ERROR
;
2331 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,SSL_R_LENGTH_MISMATCH
);
2335 p
=d
=(unsigned char *)s
->init_msg
;
2336 n2l(p
, s
->session
->tlsext_tick_lifetime_hint
);
2338 /* ticket_lifetime_hint + ticket_length + ticket */
2339 if (ticklen
+ 6 != n
)
2341 al
= SSL_AD_DECODE_ERROR
;
2342 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,SSL_R_LENGTH_MISMATCH
);
2345 if (s
->session
->tlsext_tick
)
2347 OPENSSL_free(s
->session
->tlsext_tick
);
2348 s
->session
->tlsext_ticklen
= 0;
2350 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
2351 if (!s
->session
->tlsext_tick
)
2353 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET
,ERR_R_MALLOC_FAILURE
);
2356 memcpy(s
->session
->tlsext_tick
, p
, ticklen
);
2357 s
->session
->tlsext_ticklen
= ticklen
;
2358 /* There are two ways to detect a resumed ticket session.
2359 * One is to set an appropriate session ID and then the server
2360 * must return a match in ServerHello. This allows the normal
2361 * client session ID matching to work and we know much
2362 * earlier that the ticket has been accepted.
2364 * The other way is to set zero length session ID when the
2365 * ticket is presented and rely on the handshake to determine
2366 * session resumption.
2368 * We choose the former approach because this fits in with
2369 * assumptions elsewhere in OpenSSL. The session ID is set
2370 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
2373 EVP_Digest(p
, ticklen
,
2374 s
->session
->session_id
, &s
->session
->session_id_length
,
2375 #ifndef OPENSSL_NO_SHA256
2376 EVP_sha256(), NULL
);
2383 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
2388 int ssl3_get_cert_status(SSL
*s
)
2391 unsigned long resplen
,n
;
2392 const unsigned char *p
;
2394 n
=s
->method
->ssl_get_message(s
,
2395 SSL3_ST_CR_CERT_STATUS_A
,
2396 SSL3_ST_CR_CERT_STATUS_B
,
2397 SSL3_MT_CERTIFICATE_STATUS
,
2401 if (!ok
) return((int)n
);
2404 /* need at least status type + length */
2405 al
= SSL_AD_DECODE_ERROR
;
2406 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,SSL_R_LENGTH_MISMATCH
);
2409 p
= (unsigned char *)s
->init_msg
;
2410 if (*p
++ != TLSEXT_STATUSTYPE_ocsp
)
2412 al
= SSL_AD_DECODE_ERROR
;
2413 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,SSL_R_UNSUPPORTED_STATUS_TYPE
);
2417 if (resplen
+ 4 != n
)
2419 al
= SSL_AD_DECODE_ERROR
;
2420 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,SSL_R_LENGTH_MISMATCH
);
2423 if (s
->tlsext_ocsp_resp
)
2424 OPENSSL_free(s
->tlsext_ocsp_resp
);
2425 s
->tlsext_ocsp_resp
= BUF_memdup(p
, resplen
);
2426 if (!s
->tlsext_ocsp_resp
)
2428 al
= SSL_AD_INTERNAL_ERROR
;
2429 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,ERR_R_MALLOC_FAILURE
);
2432 s
->tlsext_ocsp_resplen
= resplen
;
2433 if (s
->ctx
->tlsext_status_cb
)
2436 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2439 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
2440 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,SSL_R_INVALID_STATUS_RESPONSE
);
2445 al
= SSL_AD_INTERNAL_ERROR
;
2446 SSLerr(SSL_F_SSL3_GET_CERT_STATUS
,ERR_R_MALLOC_FAILURE
);
2452 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
2457 int ssl3_get_server_done(SSL
*s
)
2462 n
=s
->method
->ssl_get_message(s
,
2463 SSL3_ST_CR_SRVR_DONE_A
,
2464 SSL3_ST_CR_SRVR_DONE_B
,
2465 SSL3_MT_SERVER_DONE
,
2466 30, /* should be very small, like 0 :-) */
2469 if (!ok
) return((int)n
);
2472 /* should contain no data */
2473 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
2474 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
,SSL_R_LENGTH_MISMATCH
);
2482 int ssl3_send_client_key_exchange(SSL
*s
)
2486 unsigned long alg_k
;
2487 #ifndef OPENSSL_NO_RSA
2489 EVP_PKEY
*pkey
=NULL
;
2491 #ifndef OPENSSL_NO_KRB5
2493 #endif /* OPENSSL_NO_KRB5 */
2494 #ifndef OPENSSL_NO_ECDH
2495 EC_KEY
*clnt_ecdh
= NULL
;
2496 const EC_POINT
*srvr_ecpoint
= NULL
;
2497 EVP_PKEY
*srvr_pub_pkey
= NULL
;
2498 unsigned char *encodedPoint
= NULL
;
2499 int encoded_pt_len
= 0;
2500 BN_CTX
* bn_ctx
= NULL
;
2503 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
)
2505 p
= ssl_handshake_start(s
);
2507 alg_k
=s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2509 /* Fool emacs indentation */
2511 #ifndef OPENSSL_NO_RSA
2512 else if (alg_k
& SSL_kRSA
)
2515 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
2517 if (s
->session
->sess_cert
== NULL
)
2519 /* We should always have a server certificate with SSL_kRSA. */
2520 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
2524 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
2525 rsa
=s
->session
->sess_cert
->peer_rsa_tmp
;
2528 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
2529 if ((pkey
== NULL
) ||
2530 (pkey
->type
!= EVP_PKEY_RSA
) ||
2531 (pkey
->pkey
.rsa
== NULL
))
2533 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
2537 EVP_PKEY_free(pkey
);
2540 tmp_buf
[0]=s
->client_version
>>8;
2541 tmp_buf
[1]=s
->client_version
&0xff;
2542 if (RAND_bytes(&(tmp_buf
[2]),sizeof tmp_buf
-2) <= 0)
2545 s
->session
->master_key_length
=sizeof tmp_buf
;
2548 /* Fix buf for TLS and beyond */
2549 if (s
->version
> SSL3_VERSION
)
2551 n
=RSA_public_encrypt(sizeof tmp_buf
,
2552 tmp_buf
,p
,rsa
,RSA_PKCS1_PADDING
);
2554 if (s
->options
& SSL_OP_PKCS1_CHECK_1
) p
[1]++;
2555 if (s
->options
& SSL_OP_PKCS1_CHECK_2
) tmp_buf
[0]=0x70;
2559 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_RSA_ENCRYPT
);
2563 /* Fix buf for TLS and beyond */
2564 if (s
->version
> SSL3_VERSION
)
2570 s
->session
->master_key_length
=
2571 s
->method
->ssl3_enc
->generate_master_secret(s
,
2572 s
->session
->master_key
,
2573 tmp_buf
,sizeof tmp_buf
);
2574 OPENSSL_cleanse(tmp_buf
,sizeof tmp_buf
);
2577 #ifndef OPENSSL_NO_KRB5
2578 else if (alg_k
& SSL_kKRB5
)
2580 krb5_error_code krb5rc
;
2581 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
2582 /* krb5_data krb5_ap_req; */
2583 krb5_data
*enc_ticket
;
2584 krb5_data authenticator
, *authp
= NULL
;
2585 EVP_CIPHER_CTX ciph_ctx
;
2586 const EVP_CIPHER
*enc
= NULL
;
2587 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2588 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
2589 unsigned char epms
[SSL_MAX_MASTER_KEY_LENGTH
2590 + EVP_MAX_IV_LENGTH
];
2591 int padl
, outl
= sizeof(epms
);
2593 EVP_CIPHER_CTX_init(&ciph_ctx
);
2596 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2598 #endif /* KSSL_DEBUG */
2602 if (KRB5SENDAUTH
) authp
= &authenticator
;
2603 #endif /* KRB5SENDAUTH */
2605 krb5rc
= kssl_cget_tkt(kssl_ctx
, &enc_ticket
, authp
,
2607 enc
= kssl_map_enc(kssl_ctx
->enctype
);
2612 printf("kssl_cget_tkt rtn %d\n", krb5rc
);
2613 if (krb5rc
&& kssl_err
.text
)
2614 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err
.text
);
2616 #endif /* KSSL_DEBUG */
2620 ssl3_send_alert(s
,SSL3_AL_FATAL
,
2621 SSL_AD_HANDSHAKE_FAILURE
);
2622 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2627 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2628 ** in place of RFC 2712 KerberosWrapper, as in:
2630 ** Send ticket (copy to *p, set n = length)
2631 ** n = krb5_ap_req.length;
2632 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2633 ** if (krb5_ap_req.data)
2634 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2636 ** Now using real RFC 2712 KerberosWrapper
2637 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2638 ** Note: 2712 "opaque" types are here replaced
2639 ** with a 2-byte length followed by the value.
2641 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2642 ** Where "xx xx" = length bytes. Shown here with
2643 ** optional authenticator omitted.
2646 /* KerberosWrapper.Ticket */
2647 s2n(enc_ticket
->length
,p
);
2648 memcpy(p
, enc_ticket
->data
, enc_ticket
->length
);
2649 p
+= enc_ticket
->length
;
2650 n
= enc_ticket
->length
+ 2;
2652 /* KerberosWrapper.Authenticator */
2653 if (authp
&& authp
->length
)
2655 s2n(authp
->length
,p
);
2656 memcpy(p
, authp
->data
, authp
->length
);
2658 n
+= authp
->length
+ 2;
2666 s2n(0,p
);/* null authenticator length */
2670 tmp_buf
[0]=s
->client_version
>>8;
2671 tmp_buf
[1]=s
->client_version
&0xff;
2672 if (RAND_bytes(&(tmp_buf
[2]),sizeof tmp_buf
-2) <= 0)
2675 /* 20010420 VRS. Tried it this way; failed.
2676 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2677 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2678 ** kssl_ctx->length);
2679 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2682 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2683 EVP_EncryptInit_ex(&ciph_ctx
,enc
, NULL
,
2685 EVP_EncryptUpdate(&ciph_ctx
,epms
,&outl
,tmp_buf
,
2687 EVP_EncryptFinal_ex(&ciph_ctx
,&(epms
[outl
]),&padl
);
2689 if (outl
> (int)sizeof epms
)
2691 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2694 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2696 /* KerberosWrapper.EncryptedPreMasterSecret */
2698 memcpy(p
, epms
, outl
);
2702 s
->session
->master_key_length
=
2703 s
->method
->ssl3_enc
->generate_master_secret(s
,
2704 s
->session
->master_key
,
2705 tmp_buf
, sizeof tmp_buf
);
2707 OPENSSL_cleanse(tmp_buf
, sizeof tmp_buf
);
2708 OPENSSL_cleanse(epms
, outl
);
2711 #ifndef OPENSSL_NO_DH
2712 else if (alg_k
& (SSL_kDHE
|SSL_kDHr
|SSL_kDHd
))
2714 DH
*dh_srvr
,*dh_clnt
;
2715 SESS_CERT
*scert
= s
->session
->sess_cert
;
2719 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
2720 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_UNEXPECTED_MESSAGE
);
2724 if (scert
->peer_dh_tmp
!= NULL
)
2725 dh_srvr
=scert
->peer_dh_tmp
;
2728 /* we get them from the cert */
2729 int idx
= scert
->peer_cert_type
;
2730 EVP_PKEY
*spkey
= NULL
;
2733 spkey
= X509_get_pubkey(
2734 scert
->peer_pkeys
[idx
].x509
);
2737 dh_srvr
= EVP_PKEY_get1_DH(spkey
);
2738 EVP_PKEY_free(spkey
);
2740 if (dh_srvr
== NULL
)
2742 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2743 ERR_R_INTERNAL_ERROR
);
2747 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
)
2749 /* Use client certificate key */
2750 EVP_PKEY
*clkey
= s
->cert
->key
->privatekey
;
2753 dh_clnt
= EVP_PKEY_get1_DH(clkey
);
2754 if (dh_clnt
== NULL
)
2756 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2757 ERR_R_INTERNAL_ERROR
);
2763 /* generate a new random key */
2764 if ((dh_clnt
=DHparams_dup(dh_srvr
)) == NULL
)
2766 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
2769 if (!DH_generate_key(dh_clnt
))
2771 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
2777 /* use the 'p' output buffer for the DH key, but
2778 * make sure to clear it out afterwards */
2780 n
=DH_compute_key(p
,dh_srvr
->pub_key
,dh_clnt
);
2781 if (scert
->peer_dh_tmp
== NULL
)
2786 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
2791 /* generate master key from the result */
2792 s
->session
->master_key_length
=
2793 s
->method
->ssl3_enc
->generate_master_secret(s
,
2794 s
->session
->master_key
,p
,n
);
2798 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
)
2802 /* send off the data */
2803 n
=BN_num_bytes(dh_clnt
->pub_key
);
2805 BN_bn2bin(dh_clnt
->pub_key
,p
);
2811 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2815 #ifndef OPENSSL_NO_ECDH
2816 else if (alg_k
& (SSL_kECDHE
|SSL_kECDHr
|SSL_kECDHe
))
2818 const EC_GROUP
*srvr_group
= NULL
;
2820 int ecdh_clnt_cert
= 0;
2823 if (s
->session
->sess_cert
== NULL
)
2825 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
2826 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_UNEXPECTED_MESSAGE
);
2830 /* Did we send out the client's
2831 * ECDH share for use in premaster
2832 * computation as part of client certificate?
2833 * If so, set ecdh_clnt_cert to 1.
2835 if ((alg_k
& (SSL_kECDHr
|SSL_kECDHe
)) && (s
->cert
!= NULL
))
2837 /* XXX: For now, we do not support client
2838 * authentication using ECDH certificates.
2839 * To add such support, one needs to add
2840 * code that checks for appropriate
2841 * conditions and sets ecdh_clnt_cert to 1.
2842 * For example, the cert have an ECC
2843 * key on the same curve as the server's
2844 * and the key should be authorized for
2847 * One also needs to add code in ssl3_connect
2848 * to skip sending the certificate verify
2851 * if ((s->cert->key->privatekey != NULL) &&
2852 * (s->cert->key->privatekey->type ==
2853 * EVP_PKEY_EC) && ...)
2854 * ecdh_clnt_cert = 1;
2858 if (s
->session
->sess_cert
->peer_ecdh_tmp
!= NULL
)
2860 tkey
= s
->session
->sess_cert
->peer_ecdh_tmp
;
2864 /* Get the Server Public Key from Cert */
2865 srvr_pub_pkey
= X509_get_pubkey(s
->session
-> \
2866 sess_cert
->peer_pkeys
[SSL_PKEY_ECC
].x509
);
2867 if ((srvr_pub_pkey
== NULL
) ||
2868 (srvr_pub_pkey
->type
!= EVP_PKEY_EC
) ||
2869 (srvr_pub_pkey
->pkey
.ec
== NULL
))
2871 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2872 ERR_R_INTERNAL_ERROR
);
2876 tkey
= srvr_pub_pkey
->pkey
.ec
;
2879 srvr_group
= EC_KEY_get0_group(tkey
);
2880 srvr_ecpoint
= EC_KEY_get0_public_key(tkey
);
2882 if ((srvr_group
== NULL
) || (srvr_ecpoint
== NULL
))
2884 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2885 ERR_R_INTERNAL_ERROR
);
2889 if ((clnt_ecdh
=EC_KEY_new()) == NULL
)
2891 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
2895 if (!EC_KEY_set_group(clnt_ecdh
, srvr_group
))
2897 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_EC_LIB
);
2902 /* Reuse key info from our certificate
2903 * We only need our private key to perform
2904 * the ECDH computation.
2906 const BIGNUM
*priv_key
;
2907 tkey
= s
->cert
->key
->privatekey
->pkey
.ec
;
2908 priv_key
= EC_KEY_get0_private_key(tkey
);
2909 if (priv_key
== NULL
)
2911 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
2914 if (!EC_KEY_set_private_key(clnt_ecdh
, priv_key
))
2916 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_EC_LIB
);
2922 /* Generate a new ECDH key pair */
2923 if (!(EC_KEY_generate_key(clnt_ecdh
)))
2925 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_ECDH_LIB
);
2930 /* use the 'p' output buffer for the ECDH key, but
2931 * make sure to clear it out afterwards
2934 field_size
= EC_GROUP_get_degree(srvr_group
);
2935 if (field_size
<= 0)
2937 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2941 n
=ECDH_compute_key(p
, (field_size
+7)/8, srvr_ecpoint
, clnt_ecdh
, NULL
);
2944 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
2949 /* generate master key from the result */
2950 s
->session
->master_key_length
= s
->method
->ssl3_enc \
2951 -> generate_master_secret(s
,
2952 s
->session
->master_key
,
2955 memset(p
, 0, n
); /* clean up */
2959 /* Send empty client key exch message */
2964 /* First check the size of encoding and
2965 * allocate memory accordingly.
2968 EC_POINT_point2oct(srvr_group
,
2969 EC_KEY_get0_public_key(clnt_ecdh
),
2970 POINT_CONVERSION_UNCOMPRESSED
,
2973 encodedPoint
= (unsigned char *)
2974 OPENSSL_malloc(encoded_pt_len
*
2975 sizeof(unsigned char));
2976 bn_ctx
= BN_CTX_new();
2977 if ((encodedPoint
== NULL
) ||
2980 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
2984 /* Encode the public key */
2985 n
= EC_POINT_point2oct(srvr_group
,
2986 EC_KEY_get0_public_key(clnt_ecdh
),
2987 POINT_CONVERSION_UNCOMPRESSED
,
2988 encodedPoint
, encoded_pt_len
, bn_ctx
);
2990 *p
= n
; /* length of encoded point */
2991 /* Encoded point will be copied here */
2993 /* copy the point */
2994 memcpy((unsigned char *)p
, encodedPoint
, n
);
2995 /* increment n to account for length field */
2999 /* Free allocated memory */
3000 BN_CTX_free(bn_ctx
);
3001 if (encodedPoint
!= NULL
) OPENSSL_free(encodedPoint
);
3002 if (clnt_ecdh
!= NULL
)
3003 EC_KEY_free(clnt_ecdh
);
3004 EVP_PKEY_free(srvr_pub_pkey
);
3006 #endif /* !OPENSSL_NO_ECDH */
3007 else if (alg_k
& SSL_kGOST
)
3009 /* GOST key exchange message creation */
3010 EVP_PKEY_CTX
*pkey_ctx
;
3013 unsigned int md_len
;
3015 unsigned char premaster_secret
[32],shared_ukm
[32], tmp
[256];
3016 EVP_MD_CTX
*ukm_hash
;
3019 /* Get server sertificate PKEY and create ctx from it */
3020 peer_cert
=s
->session
->sess_cert
->peer_pkeys
[(keytype
=SSL_PKEY_GOST01
)].x509
;
3022 peer_cert
=s
->session
->sess_cert
->peer_pkeys
[(keytype
=SSL_PKEY_GOST94
)].x509
;
3024 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
3028 pkey_ctx
=EVP_PKEY_CTX_new(pub_key
=X509_get_pubkey(peer_cert
),NULL
);
3029 /* If we have send a certificate, and certificate key
3031 * parameters match those of server certificate, use
3032 * certificate key for key exchange
3035 /* Otherwise, generate ephemeral key pair */
3037 EVP_PKEY_encrypt_init(pkey_ctx
);
3038 /* Generate session key */
3039 RAND_bytes(premaster_secret
,32);
3040 /* If we have client certificate, use its secret as peer key */
3041 if (s
->s3
->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
3042 if (EVP_PKEY_derive_set_peer(pkey_ctx
,s
->cert
->key
->privatekey
) <=0) {
3043 /* If there was an error - just ignore it. Ephemeral key
3049 /* Compute shared IV and store it in algorithm-specific
3051 ukm_hash
= EVP_MD_CTX_create();
3052 EVP_DigestInit(ukm_hash
,EVP_get_digestbynid(NID_id_GostR3411_94
));
3053 EVP_DigestUpdate(ukm_hash
,s
->s3
->client_random
,SSL3_RANDOM_SIZE
);
3054 EVP_DigestUpdate(ukm_hash
,s
->s3
->server_random
,SSL3_RANDOM_SIZE
);
3055 EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
);
3056 EVP_MD_CTX_destroy(ukm_hash
);
3057 if (EVP_PKEY_CTX_ctrl(pkey_ctx
,-1,EVP_PKEY_OP_ENCRYPT
,EVP_PKEY_CTRL_SET_IV
,
3059 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3063 /* Make GOST keytransport blob message */
3064 /*Encapsulate it into sequence */
3065 *(p
++)=V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
;
3067 if (EVP_PKEY_encrypt(pkey_ctx
,tmp
,&msglen
,premaster_secret
,32)<0) {
3068 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3075 *(p
++)= msglen
& 0xff;
3080 *(p
++)= msglen
& 0xff;
3083 memcpy(p
, tmp
, msglen
);
3084 /* Check if pubkey from client certificate was used */
3085 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
3087 /* Set flag "skip certificate verify" */
3088 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
3090 EVP_PKEY_CTX_free(pkey_ctx
);
3091 s
->session
->master_key_length
=
3092 s
->method
->ssl3_enc
->generate_master_secret(s
,
3093 s
->session
->master_key
,premaster_secret
,32);
3094 EVP_PKEY_free(pub_key
);
3097 #ifndef OPENSSL_NO_SRP
3098 else if (alg_k
& SSL_kSRP
)
3100 if (s
->srp_ctx
.A
!= NULL
)
3102 /* send off the data */
3103 n
=BN_num_bytes(s
->srp_ctx
.A
);
3105 BN_bn2bin(s
->srp_ctx
.A
,p
);
3110 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
3113 if (s
->session
->srp_username
!= NULL
)
3114 OPENSSL_free(s
->session
->srp_username
);
3115 s
->session
->srp_username
= BUF_strdup(s
->srp_ctx
.login
);
3116 if (s
->session
->srp_username
== NULL
)
3118 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3119 ERR_R_MALLOC_FAILURE
);
3123 if ((s
->session
->master_key_length
= SRP_generate_client_master_secret(s
,s
->session
->master_key
))<0)
3125 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
3130 #ifndef OPENSSL_NO_PSK
3131 else if (alg_k
& SSL_kPSK
)
3133 /* The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes
3134 * to return a \0-terminated identity. The last byte
3135 * is for us for simulating strnlen. */
3136 char identity
[PSK_MAX_IDENTITY_LEN
+ 2];
3137 size_t identity_len
;
3138 unsigned char *t
= NULL
;
3139 unsigned char psk_or_pre_ms
[PSK_MAX_PSK_LEN
*2+4];
3140 unsigned int pre_ms_len
= 0, psk_len
= 0;
3144 if (s
->psk_client_callback
== NULL
)
3146 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3147 SSL_R_PSK_NO_CLIENT_CB
);
3151 memset(identity
, 0, sizeof(identity
));
3152 psk_len
= s
->psk_client_callback(s
, s
->ctx
->psk_identity_hint
,
3153 identity
, sizeof(identity
) - 1,
3154 psk_or_pre_ms
, sizeof(psk_or_pre_ms
));
3155 if (psk_len
> PSK_MAX_PSK_LEN
)
3157 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3158 ERR_R_INTERNAL_ERROR
);
3161 else if (psk_len
== 0)
3163 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3164 SSL_R_PSK_IDENTITY_NOT_FOUND
);
3167 identity
[PSK_MAX_IDENTITY_LEN
+ 1] = '\0';
3168 identity_len
= strlen(identity
);
3169 if (identity_len
> PSK_MAX_IDENTITY_LEN
)
3171 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3172 ERR_R_INTERNAL_ERROR
);
3175 /* create PSK pre_master_secret */
3176 pre_ms_len
= 2+psk_len
+2+psk_len
;
3178 memmove(psk_or_pre_ms
+psk_len
+4, psk_or_pre_ms
, psk_len
);
3180 memset(t
, 0, psk_len
);
3184 if (s
->session
->psk_identity_hint
!= NULL
)
3185 OPENSSL_free(s
->session
->psk_identity_hint
);
3186 s
->session
->psk_identity_hint
= BUF_strdup(s
->ctx
->psk_identity_hint
);
3187 if (s
->ctx
->psk_identity_hint
!= NULL
&&
3188 s
->session
->psk_identity_hint
== NULL
)
3190 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3191 ERR_R_MALLOC_FAILURE
);
3195 if (s
->session
->psk_identity
!= NULL
)
3196 OPENSSL_free(s
->session
->psk_identity
);
3197 s
->session
->psk_identity
= BUF_strdup(identity
);
3198 if (s
->session
->psk_identity
== NULL
)
3200 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3201 ERR_R_MALLOC_FAILURE
);
3205 s
->session
->master_key_length
=
3206 s
->method
->ssl3_enc
->generate_master_secret(s
,
3207 s
->session
->master_key
,
3208 psk_or_pre_ms
, pre_ms_len
);
3209 s2n(identity_len
, p
);
3210 memcpy(p
, identity
, identity_len
);
3211 n
= 2 + identity_len
;
3214 OPENSSL_cleanse(identity
, sizeof(identity
));
3215 OPENSSL_cleanse(psk_or_pre_ms
, sizeof(psk_or_pre_ms
));
3218 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
3225 ssl3_send_alert(s
, SSL3_AL_FATAL
,
3226 SSL_AD_HANDSHAKE_FAILURE
);
3227 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
3228 ERR_R_INTERNAL_ERROR
);
3232 ssl_set_handshake_header(s
, SSL3_MT_CLIENT_KEY_EXCHANGE
, n
);
3233 s
->state
=SSL3_ST_CW_KEY_EXCH_B
;
3236 /* SSL3_ST_CW_KEY_EXCH_B */
3237 return ssl_do_write(s
);
3239 #ifndef OPENSSL_NO_ECDH
3240 BN_CTX_free(bn_ctx
);
3241 if (encodedPoint
!= NULL
) OPENSSL_free(encodedPoint
);
3242 if (clnt_ecdh
!= NULL
)
3243 EC_KEY_free(clnt_ecdh
);
3244 EVP_PKEY_free(srvr_pub_pkey
);
3249 int ssl3_send_client_verify(SSL
*s
)
3252 unsigned char data
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
3254 EVP_PKEY_CTX
*pctx
=NULL
;
3260 EVP_MD_CTX_init(&mctx
);
3262 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
)
3264 p
= ssl_handshake_start(s
);
3265 pkey
=s
->cert
->key
->privatekey
;
3266 /* Create context from key and test if sha1 is allowed as digest */
3267 pctx
= EVP_PKEY_CTX_new(pkey
,NULL
);
3268 EVP_PKEY_sign_init(pctx
);
3269 if (EVP_PKEY_CTX_set_signature_md(pctx
, EVP_sha1())>0)
3271 if (!SSL_USE_SIGALGS(s
))
3272 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3274 &(data
[MD5_DIGEST_LENGTH
]));
3280 /* For TLS v1.2 send signature algorithm and signature
3281 * using agreed digest and cached handshake records.
3283 if (SSL_USE_SIGALGS(s
))
3287 const EVP_MD
*md
= s
->cert
->key
->digest
;
3288 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
,
3290 if (hdatalen
<= 0 || !tls12_get_sigandhash(p
, pkey
, md
))
3292 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
3293 ERR_R_INTERNAL_ERROR
);
3298 fprintf(stderr
, "Using TLS 1.2 with client alg %s\n",
3301 if (!EVP_SignInit_ex(&mctx
, md
, NULL
)
3302 || !EVP_SignUpdate(&mctx
, hdata
, hdatalen
)
3303 || !EVP_SignFinal(&mctx
, p
+ 2, &u
, pkey
))
3305 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
3311 if (!ssl3_digest_cached_records(s
))
3315 #ifndef OPENSSL_NO_RSA
3316 if (pkey
->type
== EVP_PKEY_RSA
)
3318 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3321 if (RSA_sign(NID_md5_sha1
, data
,
3322 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
,
3323 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0 )
3325 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_RSA_LIB
);
3333 #ifndef OPENSSL_NO_DSA
3334 if (pkey
->type
== EVP_PKEY_DSA
)
3336 if (!DSA_sign(pkey
->save_type
,
3337 &(data
[MD5_DIGEST_LENGTH
]),
3338 SHA_DIGEST_LENGTH
,&(p
[2]),
3339 (unsigned int *)&j
,pkey
->pkey
.dsa
))
3341 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_DSA_LIB
);
3349 #ifndef OPENSSL_NO_ECDSA
3350 if (pkey
->type
== EVP_PKEY_EC
)
3352 if (!ECDSA_sign(pkey
->save_type
,
3353 &(data
[MD5_DIGEST_LENGTH
]),
3354 SHA_DIGEST_LENGTH
,&(p
[2]),
3355 (unsigned int *)&j
,pkey
->pkey
.ec
))
3357 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
3366 if (pkey
->type
== NID_id_GostR3410_94
|| pkey
->type
== NID_id_GostR3410_2001
)
3368 unsigned char signbuf
[64];
3371 s
->method
->ssl3_enc
->cert_verify_mac(s
,
3372 NID_id_GostR3411_94
,
3374 if (EVP_PKEY_sign(pctx
, signbuf
, &sigsize
, data
, 32) <= 0) {
3375 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,
3376 ERR_R_INTERNAL_ERROR
);
3379 for (i
=63,j
=0; i
>=0; j
++, i
--) {
3387 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_INTERNAL_ERROR
);
3390 ssl_set_handshake_header(s
, SSL3_MT_CERTIFICATE_VERIFY
, n
);
3391 s
->state
=SSL3_ST_CW_CERT_VRFY_B
;
3393 EVP_MD_CTX_cleanup(&mctx
);
3394 EVP_PKEY_CTX_free(pctx
);
3395 return ssl_do_write(s
);
3397 EVP_MD_CTX_cleanup(&mctx
);
3398 EVP_PKEY_CTX_free(pctx
);
3402 /* Check a certificate can be used for client authentication. Currently
3403 * check cert exists, if we have a suitable digest for TLS 1.2 if
3404 * static DH client certificates can be used and optionally checks
3405 * suitability for Suite B.
3407 static int ssl3_check_client_certificate(SSL
*s
)
3409 unsigned long alg_k
;
3410 if (!s
->cert
|| !s
->cert
->key
->x509
|| !s
->cert
->key
->privatekey
)
3412 /* If no suitable signature algorithm can't use certificate */
3413 if (SSL_USE_SIGALGS(s
) && !s
->cert
->key
->digest
)
3415 /* If strict mode check suitability of chain before using it.
3416 * This also adjusts suite B digest if necessary.
3418 if (s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
&&
3419 !tls1_check_chain(s
, NULL
, NULL
, NULL
, -2))
3421 alg_k
=s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3422 /* See if we can use client certificate for fixed DH */
3423 if (alg_k
& (SSL_kDHr
|SSL_kDHd
))
3425 SESS_CERT
*scert
= s
->session
->sess_cert
;
3426 int i
= scert
->peer_cert_type
;
3427 EVP_PKEY
*clkey
= NULL
, *spkey
= NULL
;
3428 clkey
= s
->cert
->key
->privatekey
;
3429 /* If client key not DH assume it can be used */
3430 if (EVP_PKEY_id(clkey
) != EVP_PKEY_DH
)
3433 spkey
= X509_get_pubkey(scert
->peer_pkeys
[i
].x509
);
3436 /* Compare server and client parameters */
3437 i
= EVP_PKEY_cmp_parameters(clkey
, spkey
);
3438 EVP_PKEY_free(spkey
);
3442 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
3447 int ssl3_send_client_certificate(SSL
*s
)
3450 EVP_PKEY
*pkey
=NULL
;
3453 if (s
->state
== SSL3_ST_CW_CERT_A
)
3455 /* Let cert callback update client certificates if required */
3456 if (s
->cert
->cert_cb
)
3458 i
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
3461 s
->rwstate
=SSL_X509_LOOKUP
;
3466 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_INTERNAL_ERROR
);
3469 s
->rwstate
=SSL_NOTHING
;
3471 if (ssl3_check_client_certificate(s
))
3472 s
->state
=SSL3_ST_CW_CERT_C
;
3474 s
->state
=SSL3_ST_CW_CERT_B
;
3477 /* We need to get a client cert */
3478 if (s
->state
== SSL3_ST_CW_CERT_B
)
3480 /* If we get an error, we need to
3481 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
3482 * We then get retied later */
3484 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
3487 s
->rwstate
=SSL_X509_LOOKUP
;
3490 s
->rwstate
=SSL_NOTHING
;
3491 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
))
3493 s
->state
=SSL3_ST_CW_CERT_B
;
3494 if ( !SSL_use_certificate(s
,x509
) ||
3495 !SSL_use_PrivateKey(s
,pkey
))
3501 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
3504 if (x509
!= NULL
) X509_free(x509
);
3505 if (pkey
!= NULL
) EVP_PKEY_free(pkey
);
3506 if (i
&& !ssl3_check_client_certificate(s
))
3510 if (s
->version
== SSL3_VERSION
)
3512 s
->s3
->tmp
.cert_req
=0;
3513 ssl3_send_alert(s
,SSL3_AL_WARNING
,SSL_AD_NO_CERTIFICATE
);
3518 s
->s3
->tmp
.cert_req
=2;
3522 /* Ok, we have a cert */
3523 s
->state
=SSL3_ST_CW_CERT_C
;
3526 if (s
->state
== SSL3_ST_CW_CERT_C
)
3528 s
->state
=SSL3_ST_CW_CERT_D
;
3529 if (!ssl3_output_cert_chain(s
,
3530 (s
->s3
->tmp
.cert_req
== 2)?NULL
:s
->cert
->key
))
3532 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3533 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_INTERNAL_ERROR
);
3537 /* SSL3_ST_CW_CERT_D */
3538 return ssl_do_write(s
);
3541 #define has_bits(i,m) (((i)&(m)) == (m))
3543 int ssl3_check_cert_and_algorithm(SSL
*s
)
3547 EVP_PKEY
*pkey
=NULL
;
3549 #ifndef OPENSSL_NO_RSA
3552 #ifndef OPENSSL_NO_DH
3556 alg_k
=s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3557 alg_a
=s
->s3
->tmp
.new_cipher
->algorithm_auth
;
3559 /* we don't have a certificate */
3560 if ((alg_a
& (SSL_aNULL
|SSL_aKRB5
)) || (alg_k
& SSL_kPSK
))
3563 sc
=s
->session
->sess_cert
;
3566 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,ERR_R_INTERNAL_ERROR
);
3570 #ifndef OPENSSL_NO_RSA
3571 rsa
=s
->session
->sess_cert
->peer_rsa_tmp
;
3573 #ifndef OPENSSL_NO_DH
3574 dh
=s
->session
->sess_cert
->peer_dh_tmp
;
3577 /* This is the passed certificate */
3579 idx
=sc
->peer_cert_type
;
3580 #ifndef OPENSSL_NO_ECDH
3581 if (idx
== SSL_PKEY_ECC
)
3583 if (ssl_check_srvr_ecc_cert_and_alg(sc
->peer_pkeys
[idx
].x509
,
3585 { /* check failed */
3586 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_BAD_ECC_CERT
);
3594 else if (alg_a
& SSL_aECDSA
)
3596 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_ECDSA_SIGNING_CERT
);
3599 else if (alg_k
& (SSL_kECDHr
|SSL_kECDHe
))
3601 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_ECDH_CERT
);
3605 pkey
=X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
3606 i
=X509_certificate_type(sc
->peer_pkeys
[idx
].x509
,pkey
);
3607 EVP_PKEY_free(pkey
);
3610 /* Check that we have a certificate if we require one */
3611 if ((alg_a
& SSL_aRSA
) && !has_bits(i
,EVP_PK_RSA
|EVP_PKT_SIGN
))
3613 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_RSA_SIGNING_CERT
);
3616 #ifndef OPENSSL_NO_DSA
3617 else if ((alg_a
& SSL_aDSS
) && !has_bits(i
,EVP_PK_DSA
|EVP_PKT_SIGN
))
3619 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DSA_SIGNING_CERT
);
3623 #ifndef OPENSSL_NO_RSA
3624 if ((alg_k
& SSL_kRSA
) &&
3625 !(has_bits(i
,EVP_PK_RSA
|EVP_PKT_ENC
) || (rsa
!= NULL
)))
3627 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
3631 #ifndef OPENSSL_NO_DH
3632 if ((alg_k
& SSL_kDHE
) &&
3633 !(has_bits(i
,EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
)))
3635 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_KEY
);
3638 else if ((alg_k
& SSL_kDHr
) && !SSL_USE_SIGALGS(s
) &&
3639 !has_bits(i
,EVP_PK_DH
|EVP_PKS_RSA
))
3641 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_RSA_CERT
);
3644 #ifndef OPENSSL_NO_DSA
3645 else if ((alg_k
& SSL_kDHd
) && !SSL_USE_SIGALGS(s
) &&
3646 !has_bits(i
,EVP_PK_DH
|EVP_PKS_DSA
))
3648 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_DSA_CERT
);
3654 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && !has_bits(i
,EVP_PKT_EXP
))
3656 #ifndef OPENSSL_NO_RSA
3657 if (alg_k
& SSL_kRSA
)
3660 || RSA_size(rsa
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
))
3662 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
3668 #ifndef OPENSSL_NO_DH
3669 if (alg_k
& (SSL_kDHE
|SSL_kDHr
|SSL_kDHd
))
3672 || DH_size(dh
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
))
3674 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
3681 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
3687 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);
3692 /* Check to see if handshake is full or resumed. Usually this is just a
3693 * case of checking to see if a cache hit has occurred. In the case of
3694 * session tickets we have to check the next message to be sure.
3697 #ifndef OPENSSL_NO_TLSEXT
3698 # ifndef OPENSSL_NO_NEXTPROTONEG
3699 int ssl3_send_next_proto(SSL
*s
)
3701 unsigned int len
, padding_len
;
3704 if (s
->state
== SSL3_ST_CW_NEXT_PROTO_A
)
3706 len
= s
->next_proto_negotiated_len
;
3707 padding_len
= 32 - ((len
+ 2) % 32);
3708 d
= (unsigned char *)s
->init_buf
->data
;
3710 memcpy(d
+ 5, s
->next_proto_negotiated
, len
);
3711 d
[5 + len
] = padding_len
;
3712 memset(d
+ 6 + len
, 0, padding_len
);
3713 *(d
++)=SSL3_MT_NEXT_PROTO
;
3714 l2n3(2 + len
+ padding_len
, d
);
3715 s
->state
= SSL3_ST_CW_NEXT_PROTO_B
;
3716 s
->init_num
= 4 + 2 + len
+ padding_len
;
3720 return ssl3_do_write(s
, SSL3_RT_HANDSHAKE
);
3724 int ssl3_check_finished(SSL
*s
)
3729 /* If we have no ticket it cannot be a resumed session. */
3730 if (!s
->session
->tlsext_tick
)
3732 /* this function is called when we really expect a Certificate
3733 * message, so permit appropriate message length */
3734 n
=s
->method
->ssl_get_message(s
,
3740 if (!ok
) return((int)n
);
3741 s
->s3
->tmp
.reuse_message
= 1;
3743 if ((s
->s3
->tmp
.message_type
== SSL3_MT_FINISHED
)
3744 || (s
->s3
->tmp
.message_type
== SSL3_MT_NEWSESSION_TICKET
))
3751 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
3754 #ifndef OPENSSL_NO_ENGINE
3755 if (s
->ctx
->client_cert_engine
)
3757 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
3758 SSL_get_client_CA_list(s
),
3759 px509
, ppkey
, NULL
, NULL
, NULL
);
3764 if (s
->ctx
->client_cert_cb
)
3765 i
= s
->ctx
->client_cert_cb(s
,px509
,ppkey
);