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-2002 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).
113 #include <openssl/buffer.h>
114 #include <openssl/rand.h>
115 #include <openssl/objects.h>
116 #include <openssl/md5.h>
117 #include <openssl/sha.h>
118 #include <openssl/evp.h>
119 #include "ssl_locl.h"
121 static SSL_METHOD
*ssl3_get_client_method(int ver
);
122 static int ssl3_client_hello(SSL
*s
);
123 static int ssl3_get_server_hello(SSL
*s
);
124 static int ssl3_get_certificate_request(SSL
*s
);
125 static int ca_dn_cmp(const X509_NAME
* const *a
,const X509_NAME
* const *b
);
126 static int ssl3_get_server_done(SSL
*s
);
127 static int ssl3_send_client_verify(SSL
*s
);
128 static int ssl3_send_client_certificate(SSL
*s
);
129 static int ssl3_send_client_key_exchange(SSL
*s
);
130 static int ssl3_get_key_exchange(SSL
*s
);
131 static int ssl3_get_server_certificate(SSL
*s
);
132 static int ssl3_check_cert_and_algorithm(SSL
*s
);
133 static SSL_METHOD
*ssl3_get_client_method(int ver
)
135 if (ver
== SSL3_VERSION
)
136 return(SSLv3_client_method());
141 SSL_METHOD
*SSLv3_client_method(void)
144 static SSL_METHOD SSLv3_client_data
;
149 memcpy((char *)&SSLv3_client_data
,(char *)sslv3_base_method(),
151 SSLv3_client_data
.ssl_connect
=ssl3_connect
;
152 SSLv3_client_data
.get_ssl_method
=ssl3_get_client_method
;
154 return(&SSLv3_client_data
);
157 int ssl3_connect(SSL
*s
)
160 unsigned long Time
=time(NULL
),l
;
164 int new_state
,state
,skip
=0;;
166 RAND_add(&Time
,sizeof(Time
),0);
170 if (s
->info_callback
!= NULL
)
172 else if (s
->ctx
->info_callback
!= NULL
)
173 cb
=s
->ctx
->info_callback
;
176 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
184 case SSL_ST_RENEGOTIATE
:
186 s
->state
=SSL_ST_CONNECT
;
187 s
->ctx
->stats
.sess_connect_renegotiate
++;
191 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
192 case SSL_ST_OK
|SSL_ST_CONNECT
:
195 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
197 if ((s
->version
& 0xff00 ) != 0x0300)
199 SSLerr(SSL_F_SSL3_CONNECT
, SSL_R_INTERNAL_ERROR
);
204 /* s->version=SSL3_VERSION; */
205 s
->type
=SSL_ST_CONNECT
;
207 if (s
->init_buf
== NULL
)
209 if ((buf
=BUF_MEM_new()) == NULL
)
214 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
222 if (!ssl3_setup_buffers(s
)) { ret
= -1; goto end
; }
224 /* setup buffing BIO */
225 if (!ssl_init_wbio_buffer(s
,0)) { ret
= -1; goto end
; }
227 /* don't push the buffering BIO quite yet */
229 ssl3_init_finished_mac(s
);
231 s
->state
=SSL3_ST_CW_CLNT_HELLO_A
;
232 s
->ctx
->stats
.sess_connect
++;
236 case SSL3_ST_CW_CLNT_HELLO_A
:
237 case SSL3_ST_CW_CLNT_HELLO_B
:
240 ret
=ssl3_client_hello(s
);
241 if (ret
<= 0) goto end
;
242 s
->state
=SSL3_ST_CR_SRVR_HELLO_A
;
245 /* turn on buffering for the next lot of output */
246 if (s
->bbio
!= s
->wbio
)
247 s
->wbio
=BIO_push(s
->bbio
,s
->wbio
);
251 case SSL3_ST_CR_SRVR_HELLO_A
:
252 case SSL3_ST_CR_SRVR_HELLO_B
:
253 ret
=ssl3_get_server_hello(s
);
254 if (ret
<= 0) goto end
;
256 s
->state
=SSL3_ST_CR_FINISHED_A
;
258 s
->state
=SSL3_ST_CR_CERT_A
;
262 case SSL3_ST_CR_CERT_A
:
263 case SSL3_ST_CR_CERT_B
:
264 /* Check if it is anon DH */
265 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
267 ret
=ssl3_get_server_certificate(s
);
268 if (ret
<= 0) goto end
;
272 s
->state
=SSL3_ST_CR_KEY_EXCH_A
;
276 case SSL3_ST_CR_KEY_EXCH_A
:
277 case SSL3_ST_CR_KEY_EXCH_B
:
278 ret
=ssl3_get_key_exchange(s
);
279 if (ret
<= 0) goto end
;
280 s
->state
=SSL3_ST_CR_CERT_REQ_A
;
283 /* at this point we check that we have the
284 * required stuff from the server */
285 if (!ssl3_check_cert_and_algorithm(s
))
292 case SSL3_ST_CR_CERT_REQ_A
:
293 case SSL3_ST_CR_CERT_REQ_B
:
294 ret
=ssl3_get_certificate_request(s
);
295 if (ret
<= 0) goto end
;
296 s
->state
=SSL3_ST_CR_SRVR_DONE_A
;
300 case SSL3_ST_CR_SRVR_DONE_A
:
301 case SSL3_ST_CR_SRVR_DONE_B
:
302 ret
=ssl3_get_server_done(s
);
303 if (ret
<= 0) goto end
;
304 if (s
->s3
->tmp
.cert_req
)
305 s
->state
=SSL3_ST_CW_CERT_A
;
307 s
->state
=SSL3_ST_CW_KEY_EXCH_A
;
312 case SSL3_ST_CW_CERT_A
:
313 case SSL3_ST_CW_CERT_B
:
314 case SSL3_ST_CW_CERT_C
:
315 case SSL3_ST_CW_CERT_D
:
316 ret
=ssl3_send_client_certificate(s
);
317 if (ret
<= 0) goto end
;
318 s
->state
=SSL3_ST_CW_KEY_EXCH_A
;
322 case SSL3_ST_CW_KEY_EXCH_A
:
323 case SSL3_ST_CW_KEY_EXCH_B
:
324 ret
=ssl3_send_client_key_exchange(s
);
325 if (ret
<= 0) goto end
;
326 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
327 /* EAY EAY EAY need to check for DH fix cert
329 /* For TLS, cert_req is set to 2, so a cert chain
330 * of nothing is sent, but no verify packet is sent */
331 if (s
->s3
->tmp
.cert_req
== 1)
333 s
->state
=SSL3_ST_CW_CERT_VRFY_A
;
337 s
->state
=SSL3_ST_CW_CHANGE_A
;
338 s
->s3
->change_cipher_spec
=0;
344 case SSL3_ST_CW_CERT_VRFY_A
:
345 case SSL3_ST_CW_CERT_VRFY_B
:
346 ret
=ssl3_send_client_verify(s
);
347 if (ret
<= 0) goto end
;
348 s
->state
=SSL3_ST_CW_CHANGE_A
;
350 s
->s3
->change_cipher_spec
=0;
353 case SSL3_ST_CW_CHANGE_A
:
354 case SSL3_ST_CW_CHANGE_B
:
355 ret
=ssl3_send_change_cipher_spec(s
,
356 SSL3_ST_CW_CHANGE_A
,SSL3_ST_CW_CHANGE_B
);
357 if (ret
<= 0) goto end
;
358 s
->state
=SSL3_ST_CW_FINISHED_A
;
361 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
362 if (s
->s3
->tmp
.new_compression
== NULL
)
363 s
->session
->compress_meth
=0;
365 s
->session
->compress_meth
=
366 s
->s3
->tmp
.new_compression
->id
;
367 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
373 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
374 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
382 case SSL3_ST_CW_FINISHED_A
:
383 case SSL3_ST_CW_FINISHED_B
:
384 ret
=ssl3_send_finished(s
,
385 SSL3_ST_CW_FINISHED_A
,SSL3_ST_CW_FINISHED_B
,
386 s
->method
->ssl3_enc
->client_finished_label
,
387 s
->method
->ssl3_enc
->client_finished_label_len
);
388 if (ret
<= 0) goto end
;
389 s
->state
=SSL3_ST_CW_FLUSH
;
392 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
395 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
396 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
)
399 s
->s3
->flags
|=SSL3_FLAGS_POP_BUFFER
;
400 s
->s3
->delay_buf_pop_ret
=0;
405 s
->s3
->tmp
.next_state
=SSL3_ST_CR_FINISHED_A
;
410 case SSL3_ST_CR_FINISHED_A
:
411 case SSL3_ST_CR_FINISHED_B
:
413 ret
=ssl3_get_finished(s
,SSL3_ST_CR_FINISHED_A
,
414 SSL3_ST_CR_FINISHED_B
);
415 if (ret
<= 0) goto end
;
418 s
->state
=SSL3_ST_CW_CHANGE_A
;
424 case SSL3_ST_CW_FLUSH
:
425 /* number of bytes to be flushed */
426 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
429 s
->rwstate
=SSL_WRITING
;
430 num1
=BIO_flush(s
->wbio
);
431 if (num1
<= 0) { ret
= -1; goto end
; }
432 s
->rwstate
=SSL_NOTHING
;
435 s
->state
=s
->s3
->tmp
.next_state
;
439 /* clean a few things up */
440 ssl3_cleanup_key_block(s
);
442 if (s
->init_buf
!= NULL
)
444 BUF_MEM_free(s
->init_buf
);
448 /* If we are not 'joining' the last two packets,
449 * remove the buffering now */
450 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
451 ssl_free_wbio_buffer(s
);
452 /* else do it later in ssl3_write */
457 ssl_update_cache(s
,SSL_SESS_CACHE_CLIENT
);
458 if (s
->hit
) s
->ctx
->stats
.sess_hit
++;
462 s
->handshake_func
=ssl3_connect
;
463 s
->ctx
->stats
.sess_connect_good
++;
465 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
471 SSLerr(SSL_F_SSL3_CONNECT
,SSL_R_UNKNOWN_STATE
);
477 /* did we do anything */
478 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
482 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
486 if ((cb
!= NULL
) && (s
->state
!= state
))
490 cb(s
,SSL_CB_CONNECT_LOOP
,1);
499 cb(s
,SSL_CB_CONNECT_EXIT
,ret
);
504 static int ssl3_client_hello(SSL
*s
)
509 unsigned long Time
,l
;
512 buf
=(unsigned char *)s
->init_buf
->data
;
513 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
)
515 if ((s
->session
== NULL
) ||
516 (s
->session
->ssl_version
!= s
->version
) ||
517 (s
->session
->not_resumable
))
519 if (!ssl_get_new_session(s
,0))
522 /* else use the pre-loaded session */
524 p
=s
->s3
->client_random
;
525 Time
=time(NULL
); /* Time */
527 RAND_pseudo_bytes(p
,SSL3_RANDOM_SIZE
-sizeof(Time
));
529 /* Do the message type and length last */
532 *(p
++)=s
->version
>>8;
533 *(p
++)=s
->version
&0xff;
534 s
->client_version
=s
->version
;
537 memcpy(p
,s
->s3
->client_random
,SSL3_RANDOM_SIZE
);
544 i
=s
->session
->session_id_length
;
548 memcpy(p
,s
->session
->session_id
,i
);
552 /* Ciphers supported */
553 i
=ssl_cipher_list_to_bytes(s
,SSL_get_ciphers(s
),&(p
[2]));
556 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,SSL_R_NO_CIPHERS_AVAILABLE
);
563 if (s
->ctx
->comp_methods
== NULL
)
566 j
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
570 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,i
);
573 *(p
++)=0; /* Add the NULL method */
577 *(d
++)=SSL3_MT_CLIENT_HELLO
;
580 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
581 /* number of bytes to write */
586 /* SSL3_ST_CW_CLNT_HELLO_B */
587 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
592 static int ssl3_get_server_hello(SSL
*s
)
594 STACK_OF(SSL_CIPHER
) *sk
;
602 n
=ssl3_get_message(s
,
603 SSL3_ST_CR_SRVR_HELLO_A
,
604 SSL3_ST_CR_SRVR_HELLO_B
,
605 SSL3_MT_SERVER_HELLO
,
609 if (!ok
) return((int)n
);
610 d
=p
=(unsigned char *)s
->init_buf
->data
;
612 if ((p
[0] != (s
->version
>>8)) || (p
[1] != (s
->version
&0xff)))
614 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_SSL_VERSION
);
615 s
->version
=(s
->version
&0xff00)|p
[1];
616 al
=SSL_AD_PROTOCOL_VERSION
;
621 /* load the server hello data */
622 /* load the server random */
623 memcpy(s
->s3
->server_random
,p
,SSL3_RANDOM_SIZE
);
626 /* get the session-id */
629 if ((j
!= 0) && (j
!= SSL3_SESSION_ID_SIZE
))
631 /* SSLref returns 16 :-( */
632 if (j
< SSL2_SSL_SESSION_ID_LENGTH
)
634 al
=SSL_AD_ILLEGAL_PARAMETER
;
635 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_SSL3_SESSION_ID_TOO_SHORT
);
639 if (j
!= 0 && j
== s
->session
->session_id_length
640 && memcmp(p
,s
->session
->session_id
,j
) == 0)
642 if(s
->sid_ctx_length
!= s
->session
->sid_ctx_length
643 || memcmp(s
->session
->sid_ctx
,s
->sid_ctx
,s
->sid_ctx_length
))
645 al
=SSL_AD_ILLEGAL_PARAMETER
;
646 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
651 else /* a miss or crap from the other end */
653 /* If we were trying for session-id reuse, make a new
654 * SSL_SESSION so we don't stuff up other people */
656 if (s
->session
->session_id_length
> 0)
658 if (!ssl_get_new_session(s
,0))
660 al
=SSL_AD_INTERNAL_ERROR
;
664 s
->session
->session_id_length
=j
;
665 memcpy(s
->session
->session_id
,p
,j
); /* j could be 0 */
668 c
=ssl_get_cipher_by_char(s
,p
);
672 al
=SSL_AD_ILLEGAL_PARAMETER
;
673 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNKNOWN_CIPHER_RETURNED
);
676 p
+=ssl_put_cipher_by_char(s
,NULL
,NULL
);
678 sk
=ssl_get_ciphers_by_id(s
);
679 i
=sk_SSL_CIPHER_find(sk
,c
);
682 /* we did not say we would use this cipher */
683 al
=SSL_AD_ILLEGAL_PARAMETER
;
684 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_CIPHER_RETURNED
);
688 if (s
->hit
&& (s
->session
->cipher
!= c
))
691 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
))
693 al
=SSL_AD_ILLEGAL_PARAMETER
;
694 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
698 s
->s3
->tmp
.new_cipher
=c
;
700 /* lets get the compression algorithm */
706 comp
=ssl3_comp_find(s
->ctx
->comp_methods
,j
);
708 if ((j
!= 0) && (comp
== NULL
))
710 al
=SSL_AD_ILLEGAL_PARAMETER
;
711 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
716 s
->s3
->tmp
.new_compression
=comp
;
721 /* wrong packet length */
722 al
=SSL_AD_DECODE_ERROR
;
723 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_BAD_PACKET_LENGTH
);
729 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
734 static int ssl3_get_server_certificate(SSL
*s
)
737 unsigned long n
,nc
,llen
,l
;
739 unsigned char *p
,*d
,*q
;
740 STACK_OF(X509
) *sk
=NULL
;
744 n
=ssl3_get_message(s
,
748 #if defined(MSDOS) && !defined(WIN32)
749 1024*30, /* 30k max cert list :-) */
751 1024*100, /* 100k max cert list :-) */
755 if (!ok
) return((int)n
);
757 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
)
759 s
->s3
->tmp
.reuse_message
=1;
763 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
765 al
=SSL_AD_UNEXPECTED_MESSAGE
;
766 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_BAD_MESSAGE_TYPE
);
769 d
=p
=(unsigned char *)s
->init_buf
->data
;
771 if ((sk
=sk_X509_new_null()) == NULL
)
773 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
780 al
=SSL_AD_DECODE_ERROR
;
781 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
784 for (nc
=0; nc
<llen
; )
789 al
=SSL_AD_DECODE_ERROR
;
790 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
795 x
=d2i_X509(NULL
,&q
,l
);
798 al
=SSL_AD_BAD_CERTIFICATE
;
799 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_ASN1_LIB
);
804 al
=SSL_AD_DECODE_ERROR
;
805 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
808 if (!sk_X509_push(sk
,x
))
810 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
818 i
=ssl_verify_cert_chain(s
,sk
);
819 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (!i
))
821 al
=ssl_verify_alarm_type(s
->verify_result
);
822 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERTIFICATE_VERIFY_FAILED
);
825 ERR_clear_error(); /* but we keep s->verify_result */
827 sc
=ssl_sess_cert_new();
828 if (sc
== NULL
) goto err
;
830 if (s
->session
->sess_cert
) ssl_sess_cert_free(s
->session
->sess_cert
);
831 s
->session
->sess_cert
=sc
;
834 /* Inconsistency alert: cert_chain does include the peer's
835 * certificate, which we don't include in s3_srvr.c */
836 x
=sk_X509_value(sk
,0);
839 pkey
=X509_get_pubkey(x
);
841 if ((pkey
== NULL
) || EVP_PKEY_missing_parameters(pkey
))
845 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
849 i
=ssl_cert_type(x
,pkey
);
854 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
858 sc
->peer_cert_type
=i
;
859 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
860 if (sc
->peer_pkeys
[i
].x509
!= NULL
) /* Why would this ever happen?
861 * We just created sc a couple of
863 X509_free(sc
->peer_pkeys
[i
].x509
);
864 sc
->peer_pkeys
[i
].x509
=x
;
865 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
867 if (s
->session
->peer
!= NULL
)
868 X509_free(s
->session
->peer
);
869 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
871 s
->session
->verify_result
= s
->verify_result
;
879 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
884 sk_X509_pop_free(sk
,X509_free
);
888 static int ssl3_get_key_exchange(SSL
*s
)
891 unsigned char *q
,md_buf
[EVP_MAX_MD_SIZE
*2];
894 unsigned char *param
,*p
;
895 int al
,i
,j
,param_len
,ok
;
905 /* use same message size as in ssl3_get_certificate_request()
906 * as ServerKeyExchange message may be skipped */
907 n
=ssl3_get_message(s
,
908 SSL3_ST_CR_KEY_EXCH_A
,
909 SSL3_ST_CR_KEY_EXCH_B
,
911 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
912 1024*30, /* 30k max cert list :-) */
914 1024*100, /* 100k max cert list :-) */
918 if (!ok
) return((int)n
);
920 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
)
922 s
->s3
->tmp
.reuse_message
=1;
926 param
=p
=(unsigned char *)s
->init_buf
->data
;
928 if (s
->session
->sess_cert
!= NULL
)
931 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
933 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
934 s
->session
->sess_cert
->peer_rsa_tmp
=NULL
;
938 if (s
->session
->sess_cert
->peer_dh_tmp
)
940 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
941 s
->session
->sess_cert
->peer_dh_tmp
=NULL
;
947 s
->session
->sess_cert
=ssl_sess_cert_new();
951 alg
=s
->s3
->tmp
.new_cipher
->algorithms
;
956 if ((rsa
=RSA_new()) == NULL
)
958 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
965 al
=SSL_AD_DECODE_ERROR
;
966 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_MODULUS_LENGTH
);
969 if (!(rsa
->n
=BN_bin2bn(p
,i
,rsa
->n
)))
971 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
980 al
=SSL_AD_DECODE_ERROR
;
981 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_E_LENGTH
);
984 if (!(rsa
->e
=BN_bin2bn(p
,i
,rsa
->e
)))
986 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
992 /* this should be because we are using an export cipher */
994 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
997 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_INTERNAL_ERROR
);
1000 s
->session
->sess_cert
->peer_rsa_tmp
=rsa
;
1008 else if (alg
& SSL_kEDH
)
1010 if ((dh
=DH_new()) == NULL
)
1012 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1019 al
=SSL_AD_DECODE_ERROR
;
1020 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_P_LENGTH
);
1023 if (!(dh
->p
=BN_bin2bn(p
,i
,NULL
)))
1025 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1034 al
=SSL_AD_DECODE_ERROR
;
1035 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_G_LENGTH
);
1038 if (!(dh
->g
=BN_bin2bn(p
,i
,NULL
)))
1040 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1049 al
=SSL_AD_DECODE_ERROR
;
1050 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1053 if (!(dh
->pub_key
=BN_bin2bn(p
,i
,NULL
)))
1055 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1063 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1069 else if (alg
& SSL_aDSS
)
1070 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].x509
);
1072 /* else anonymous DH, so no certificate or pkey. */
1074 s
->session
->sess_cert
->peer_dh_tmp
=dh
;
1077 else if ((alg
& SSL_kDHr
) || (alg
& SSL_kDHd
))
1079 al
=SSL_AD_ILLEGAL_PARAMETER
;
1080 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1086 al
=SSL_AD_HANDSHAKE_FAILURE
;
1087 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1092 /* p points to the next byte, there are 'n' bytes left */
1095 /* if it was signed, check the signature */
1100 j
=EVP_PKEY_size(pkey
);
1102 if ((i
!= n
) || (n
> j
) || (n
<= 0))
1104 /* wrong packet length */
1105 al
=SSL_AD_DECODE_ERROR
;
1106 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_WRONG_SIGNATURE_LENGTH
);
1111 if (pkey
->type
== EVP_PKEY_RSA
)
1117 for (num
=2; num
> 0; num
--)
1119 EVP_DigestInit(&md_ctx
,(num
== 2)
1120 ?s
->ctx
->md5
:s
->ctx
->sha1
);
1121 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1122 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1123 EVP_DigestUpdate(&md_ctx
,param
,param_len
);
1124 EVP_DigestFinal(&md_ctx
,q
,(unsigned int *)&i
);
1128 i
=RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
,
1132 al
=SSL_AD_DECRYPT_ERROR
;
1133 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_DECRYPT
);
1139 al
=SSL_AD_DECRYPT_ERROR
;
1140 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SIGNATURE
);
1147 if (pkey
->type
== EVP_PKEY_DSA
)
1150 EVP_VerifyInit(&md_ctx
,EVP_dss1());
1151 EVP_VerifyUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1152 EVP_VerifyUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1153 EVP_VerifyUpdate(&md_ctx
,param
,param_len
);
1154 if (!EVP_VerifyFinal(&md_ctx
,p
,(int)n
,pkey
))
1157 al
=SSL_AD_DECRYPT_ERROR
;
1158 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SIGNATURE
);
1165 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_INTERNAL_ERROR
);
1171 /* still data left over */
1172 if (!(alg
& SSL_aNULL
))
1174 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_INTERNAL_ERROR
);
1179 al
=SSL_AD_DECODE_ERROR
;
1180 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_EXTRA_DATA_IN_MESSAGE
);
1184 EVP_PKEY_free(pkey
);
1187 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1189 EVP_PKEY_free(pkey
);
1201 static int ssl3_get_certificate_request(SSL
*s
)
1204 unsigned long n
,nc
,l
;
1205 unsigned int llen
,ctype_num
,i
;
1207 unsigned char *p
,*d
,*q
;
1208 STACK_OF(X509_NAME
) *ca_sk
=NULL
;
1210 n
=ssl3_get_message(s
,
1211 SSL3_ST_CR_CERT_REQ_A
,
1212 SSL3_ST_CR_CERT_REQ_B
,
1214 #if defined(MSDOS) && !defined(WIN32)
1215 1024*30, /* 30k max cert list :-) */
1217 1024*100, /* 100k max cert list :-) */
1221 if (!ok
) return((int)n
);
1223 s
->s3
->tmp
.cert_req
=0;
1225 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
)
1227 s
->s3
->tmp
.reuse_message
=1;
1231 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
)
1233 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
1234 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_WRONG_MESSAGE_TYPE
);
1238 /* TLS does not like anon-DH with client cert */
1239 if (s
->version
> SSL3_VERSION
)
1241 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1244 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
1245 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1250 d
=p
=(unsigned char *)s
->init_buf
->data
;
1252 if ((ca_sk
=sk_X509_NAME_new(ca_dn_cmp
)) == NULL
)
1254 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_MALLOC_FAILURE
);
1258 /* get the certificate types */
1260 if (ctype_num
> SSL3_CT_NUMBER
)
1261 ctype_num
=SSL3_CT_NUMBER
;
1262 for (i
=0; i
<ctype_num
; i
++)
1263 s
->s3
->tmp
.ctype
[i
]= p
[i
];
1266 /* get the CA RDNs */
1271 out
=fopen("/tmp/vsign.der","w");
1272 fwrite(p
,1,llen
,out
);
1277 if ((llen
+ctype_num
+2+1) != n
)
1279 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1280 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_LENGTH_MISMATCH
);
1284 for (nc
=0; nc
<llen
; )
1287 if ((l
+nc
+2) > llen
)
1289 if ((s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1290 goto cont
; /* netscape bugs */
1291 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1292 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_CA_DN_TOO_LONG
);
1298 if ((xn
=d2i_X509_NAME(NULL
,&q
,l
)) == NULL
)
1300 /* If netscape tolerance is on, ignore errors */
1301 if (s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
)
1305 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1306 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_ASN1_LIB
);
1313 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1314 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_CA_DN_LENGTH_MISMATCH
);
1317 if (!sk_X509_NAME_push(ca_sk
,xn
))
1319 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_MALLOC_FAILURE
);
1333 /* we should setup a certificate to return.... */
1334 s
->s3
->tmp
.cert_req
=1;
1335 s
->s3
->tmp
.ctype_num
=ctype_num
;
1336 if (s
->s3
->tmp
.ca_names
!= NULL
)
1337 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
,X509_NAME_free
);
1338 s
->s3
->tmp
.ca_names
=ca_sk
;
1343 if (ca_sk
!= NULL
) sk_X509_NAME_pop_free(ca_sk
,X509_NAME_free
);
1347 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1349 return(X509_NAME_cmp(*a
,*b
));
1352 static int ssl3_get_server_done(SSL
*s
)
1357 n
=ssl3_get_message(s
,
1358 SSL3_ST_CR_SRVR_DONE_A
,
1359 SSL3_ST_CR_SRVR_DONE_B
,
1360 SSL3_MT_SERVER_DONE
,
1361 30, /* should be very small, like 0 :-) */
1364 if (!ok
) return((int)n
);
1367 /* should contain no data */
1368 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1369 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
,SSL_R_LENGTH_MISMATCH
);
1376 static int ssl3_send_client_key_exchange(SSL
*s
)
1378 unsigned char *p
,*d
;
1383 EVP_PKEY
*pkey
=NULL
;
1386 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
)
1388 d
=(unsigned char *)s
->init_buf
->data
;
1391 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1397 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
1399 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
1400 rsa
=s
->session
->sess_cert
->peer_rsa_tmp
;
1403 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1404 if ((pkey
== NULL
) ||
1405 (pkey
->type
!= EVP_PKEY_RSA
) ||
1406 (pkey
->pkey
.rsa
== NULL
))
1408 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_INTERNAL_ERROR
);
1412 EVP_PKEY_free(pkey
);
1415 tmp_buf
[0]=s
->client_version
>>8;
1416 tmp_buf
[1]=s
->client_version
&0xff;
1417 if (RAND_bytes(&(tmp_buf
[2]),SSL_MAX_MASTER_KEY_LENGTH
-2) <= 0)
1420 s
->session
->master_key_length
=SSL_MAX_MASTER_KEY_LENGTH
;
1423 /* Fix buf for TLS and beyond */
1424 if (s
->version
> SSL3_VERSION
)
1426 n
=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH
,
1427 tmp_buf
,p
,rsa
,RSA_PKCS1_PADDING
);
1429 if (s
->options
& SSL_OP_PKCS1_CHECK_1
) p
[1]++;
1430 if (s
->options
& SSL_OP_PKCS1_CHECK_2
) tmp_buf
[0]=0x70;
1434 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_RSA_ENCRYPT
);
1438 /* Fix buf for TLS and beyond */
1439 if (s
->version
> SSL3_VERSION
)
1445 s
->session
->master_key_length
=
1446 s
->method
->ssl3_enc
->generate_master_secret(s
,
1447 s
->session
->master_key
,
1448 tmp_buf
,SSL_MAX_MASTER_KEY_LENGTH
);
1449 memset(tmp_buf
,0,SSL_MAX_MASTER_KEY_LENGTH
);
1454 if (l
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1456 DH
*dh_srvr
,*dh_clnt
;
1458 if (s
->session
->sess_cert
->peer_dh_tmp
!= NULL
)
1459 dh_srvr
=s
->session
->sess_cert
->peer_dh_tmp
;
1462 /* we get them from the cert */
1463 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);
1464 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
1468 /* generate a new random key */
1469 if ((dh_clnt
=DHparams_dup(dh_srvr
)) == NULL
)
1471 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1474 if (!DH_generate_key(dh_clnt
))
1476 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1480 /* use the 'p' output buffer for the DH key, but
1481 * make sure to clear it out afterwards */
1483 n
=DH_compute_key(p
,dh_srvr
->pub_key
,dh_clnt
);
1487 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1491 /* generate master key from the result */
1492 s
->session
->master_key_length
=
1493 s
->method
->ssl3_enc
->generate_master_secret(s
,
1494 s
->session
->master_key
,p
,n
);
1498 /* send off the data */
1499 n
=BN_num_bytes(dh_clnt
->pub_key
);
1501 BN_bn2bin(dh_clnt
->pub_key
,p
);
1506 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1511 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);
1512 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_INTERNAL_ERROR
);
1516 *(d
++)=SSL3_MT_CLIENT_KEY_EXCHANGE
;
1519 s
->state
=SSL3_ST_CW_KEY_EXCH_B
;
1520 /* number of bytes to write */
1525 /* SSL3_ST_CW_KEY_EXCH_B */
1526 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1531 static int ssl3_send_client_verify(SSL
*s
)
1533 unsigned char *p
,*d
;
1534 unsigned char data
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
1544 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
)
1546 d
=(unsigned char *)s
->init_buf
->data
;
1548 pkey
=s
->cert
->key
->privatekey
;
1550 s
->method
->ssl3_enc
->cert_verify_mac(s
,&(s
->s3
->finish_dgst2
),
1551 &(data
[MD5_DIGEST_LENGTH
]));
1554 if (pkey
->type
== EVP_PKEY_RSA
)
1556 s
->method
->ssl3_enc
->cert_verify_mac(s
,
1557 &(s
->s3
->finish_dgst1
),&(data
[0]));
1558 if (RSA_sign(NID_md5_sha1
, data
,
1559 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
,
1560 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0 )
1562 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_RSA_LIB
);
1571 if (pkey
->type
== EVP_PKEY_DSA
)
1573 if (!DSA_sign(pkey
->save_type
,
1574 &(data
[MD5_DIGEST_LENGTH
]),
1575 SHA_DIGEST_LENGTH
,&(p
[2]),
1576 (unsigned int *)&j
,pkey
->pkey
.dsa
))
1578 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_DSA_LIB
);
1587 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,SSL_R_INTERNAL_ERROR
);
1590 *(d
++)=SSL3_MT_CERTIFICATE_VERIFY
;
1593 s
->init_num
=(int)n
+4;
1596 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1601 static int ssl3_send_client_certificate(SSL
*s
)
1604 EVP_PKEY
*pkey
=NULL
;
1608 if (s
->state
== SSL3_ST_CW_CERT_A
)
1610 if ((s
->cert
== NULL
) ||
1611 (s
->cert
->key
->x509
== NULL
) ||
1612 (s
->cert
->key
->privatekey
== NULL
))
1613 s
->state
=SSL3_ST_CW_CERT_B
;
1615 s
->state
=SSL3_ST_CW_CERT_C
;
1618 /* We need to get a client cert */
1619 if (s
->state
== SSL3_ST_CW_CERT_B
)
1621 /* If we get an error, we need to
1622 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1623 * We then get retied later */
1625 if (s
->ctx
->client_cert_cb
!= NULL
)
1626 i
=s
->ctx
->client_cert_cb(s
,&(x509
),&(pkey
));
1629 s
->rwstate
=SSL_X509_LOOKUP
;
1632 s
->rwstate
=SSL_NOTHING
;
1633 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
))
1635 s
->state
=SSL3_ST_CW_CERT_B
;
1636 if ( !SSL_use_certificate(s
,x509
) ||
1637 !SSL_use_PrivateKey(s
,pkey
))
1643 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
1646 if (x509
!= NULL
) X509_free(x509
);
1647 if (pkey
!= NULL
) EVP_PKEY_free(pkey
);
1650 if (s
->version
== SSL3_VERSION
)
1652 s
->s3
->tmp
.cert_req
=0;
1653 ssl3_send_alert(s
,SSL3_AL_WARNING
,SSL_AD_NO_CERTIFICATE
);
1658 s
->s3
->tmp
.cert_req
=2;
1662 /* Ok, we have a cert */
1663 s
->state
=SSL3_ST_CW_CERT_C
;
1666 if (s
->state
== SSL3_ST_CW_CERT_C
)
1668 s
->state
=SSL3_ST_CW_CERT_D
;
1669 l
=ssl3_output_cert_chain(s
,
1670 (s
->s3
->tmp
.cert_req
== 2)?NULL
:s
->cert
->key
->x509
);
1674 /* SSL3_ST_CW_CERT_D */
1675 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1678 #define has_bits(i,m) (((i)&(m)) == (m))
1680 static int ssl3_check_cert_and_algorithm(SSL
*s
)
1684 EVP_PKEY
*pkey
=NULL
;
1693 sc
=s
->session
->sess_cert
;
1697 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_INTERNAL_ERROR
);
1701 algs
=s
->s3
->tmp
.new_cipher
->algorithms
;
1703 /* we don't have a certificate */
1704 if (algs
& (SSL_aDH
|SSL_aNULL
))
1708 rsa
=s
->session
->sess_cert
->peer_rsa_tmp
;
1711 dh
=s
->session
->sess_cert
->peer_dh_tmp
;
1714 /* This is the passed certificate */
1716 idx
=sc
->peer_cert_type
;
1717 pkey
=X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
1718 i
=X509_certificate_type(sc
->peer_pkeys
[idx
].x509
,pkey
);
1719 EVP_PKEY_free(pkey
);
1722 /* Check that we have a certificate if we require one */
1723 if ((algs
& SSL_aRSA
) && !has_bits(i
,EVP_PK_RSA
|EVP_PKT_SIGN
))
1725 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_RSA_SIGNING_CERT
);
1729 else if ((algs
& SSL_aDSS
) && !has_bits(i
,EVP_PK_DSA
|EVP_PKT_SIGN
))
1731 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DSA_SIGNING_CERT
);
1736 if ((algs
& SSL_kRSA
) &&
1737 !(has_bits(i
,EVP_PK_RSA
|EVP_PKT_ENC
) || (rsa
!= NULL
)))
1739 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
1744 if ((algs
& SSL_kEDH
) &&
1745 !(has_bits(i
,EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
)))
1747 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_KEY
);
1750 else if ((algs
& SSL_kDHr
) && !has_bits(i
,EVP_PK_DH
|EVP_PKS_RSA
))
1752 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_RSA_CERT
);
1756 else if ((algs
& SSL_kDHd
) && !has_bits(i
,EVP_PK_DH
|EVP_PKS_DSA
))
1758 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_DSA_CERT
);
1764 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && !has_bits(i
,EVP_PKT_EXP
))
1767 if (algs
& SSL_kRSA
)
1770 || RSA_size(rsa
) > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
))
1772 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
1779 if (algs
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1782 || DH_size(dh
) > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
))
1784 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
1791 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1797 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);