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 "ssl_locl.h"
114 #include "kssl_lcl.h"
115 #include <openssl/buffer.h>
116 #include <openssl/rand.h>
117 #include <openssl/objects.h>
118 #include <openssl/evp.h>
119 #include <openssl/md5.h>
120 #include <openssl/fips.h>
122 static SSL_METHOD
*ssl3_get_client_method(int ver
);
123 static int ssl3_client_hello(SSL
*s
);
124 static int ssl3_get_server_hello(SSL
*s
);
125 static int ssl3_get_certificate_request(SSL
*s
);
126 static int ca_dn_cmp(const X509_NAME
* const *a
,const X509_NAME
* const *b
);
127 static int ssl3_get_server_done(SSL
*s
);
128 static int ssl3_send_client_verify(SSL
*s
);
129 static int ssl3_send_client_certificate(SSL
*s
);
130 static int ssl3_send_client_key_exchange(SSL
*s
);
131 static int ssl3_get_key_exchange(SSL
*s
);
132 static int ssl3_get_server_certificate(SSL
*s
);
133 static int ssl3_check_cert_and_algorithm(SSL
*s
);
134 static SSL_METHOD
*ssl3_get_client_method(int ver
)
136 if (ver
== SSL3_VERSION
)
137 return(SSLv3_client_method());
142 SSL_METHOD
*SSLv3_client_method(void)
145 static SSL_METHOD SSLv3_client_data
;
149 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD
);
153 memcpy((char *)&SSLv3_client_data
,(char *)sslv3_base_method(),
155 SSLv3_client_data
.ssl_connect
=ssl3_connect
;
156 SSLv3_client_data
.get_ssl_method
=ssl3_get_client_method
;
160 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD
);
162 return(&SSLv3_client_data
);
165 int ssl3_connect(SSL
*s
)
168 unsigned long Time
=time(NULL
),l
;
170 void (*cb
)(const SSL
*ssl
,int type
,int val
)=NULL
;
172 int new_state
,state
,skip
=0;;
174 RAND_add(&Time
,sizeof(Time
),0);
178 if (s
->info_callback
!= NULL
)
180 else if (s
->ctx
->info_callback
!= NULL
)
181 cb
=s
->ctx
->info_callback
;
184 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
192 case SSL_ST_RENEGOTIATE
:
194 s
->state
=SSL_ST_CONNECT
;
195 s
->ctx
->stats
.sess_connect_renegotiate
++;
199 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
200 case SSL_ST_OK
|SSL_ST_CONNECT
:
203 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
205 if ((s
->version
& 0xff00 ) != 0x0300)
207 SSLerr(SSL_F_SSL3_CONNECT
, ERR_R_INTERNAL_ERROR
);
212 /* s->version=SSL3_VERSION; */
213 s
->type
=SSL_ST_CONNECT
;
215 if (s
->init_buf
== NULL
)
217 if ((buf
=BUF_MEM_new()) == NULL
)
222 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
231 if (!ssl3_setup_buffers(s
)) { ret
= -1; goto end
; }
233 /* setup buffing BIO */
234 if (!ssl_init_wbio_buffer(s
,0)) { ret
= -1; goto end
; }
236 /* don't push the buffering BIO quite yet */
238 ssl3_init_finished_mac(s
);
240 s
->state
=SSL3_ST_CW_CLNT_HELLO_A
;
241 s
->ctx
->stats
.sess_connect
++;
245 case SSL3_ST_CW_CLNT_HELLO_A
:
246 case SSL3_ST_CW_CLNT_HELLO_B
:
249 ret
=ssl3_client_hello(s
);
250 if (ret
<= 0) goto end
;
251 s
->state
=SSL3_ST_CR_SRVR_HELLO_A
;
254 /* turn on buffering for the next lot of output */
255 if (s
->bbio
!= s
->wbio
)
256 s
->wbio
=BIO_push(s
->bbio
,s
->wbio
);
260 case SSL3_ST_CR_SRVR_HELLO_A
:
261 case SSL3_ST_CR_SRVR_HELLO_B
:
262 ret
=ssl3_get_server_hello(s
);
263 if (ret
<= 0) goto end
;
265 s
->state
=SSL3_ST_CR_FINISHED_A
;
267 s
->state
=SSL3_ST_CR_CERT_A
;
271 case SSL3_ST_CR_CERT_A
:
272 case SSL3_ST_CR_CERT_B
:
273 /* Check if it is anon DH */
274 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
276 ret
=ssl3_get_server_certificate(s
);
277 if (ret
<= 0) goto end
;
281 s
->state
=SSL3_ST_CR_KEY_EXCH_A
;
285 case SSL3_ST_CR_KEY_EXCH_A
:
286 case SSL3_ST_CR_KEY_EXCH_B
:
287 ret
=ssl3_get_key_exchange(s
);
288 if (ret
<= 0) goto end
;
289 s
->state
=SSL3_ST_CR_CERT_REQ_A
;
292 /* at this point we check that we have the
293 * required stuff from the server */
294 if (!ssl3_check_cert_and_algorithm(s
))
301 case SSL3_ST_CR_CERT_REQ_A
:
302 case SSL3_ST_CR_CERT_REQ_B
:
303 ret
=ssl3_get_certificate_request(s
);
304 if (ret
<= 0) goto end
;
305 s
->state
=SSL3_ST_CR_SRVR_DONE_A
;
309 case SSL3_ST_CR_SRVR_DONE_A
:
310 case SSL3_ST_CR_SRVR_DONE_B
:
311 ret
=ssl3_get_server_done(s
);
312 if (ret
<= 0) goto end
;
313 if (s
->s3
->tmp
.cert_req
)
314 s
->state
=SSL3_ST_CW_CERT_A
;
316 s
->state
=SSL3_ST_CW_KEY_EXCH_A
;
321 case SSL3_ST_CW_CERT_A
:
322 case SSL3_ST_CW_CERT_B
:
323 case SSL3_ST_CW_CERT_C
:
324 case SSL3_ST_CW_CERT_D
:
325 ret
=ssl3_send_client_certificate(s
);
326 if (ret
<= 0) goto end
;
327 s
->state
=SSL3_ST_CW_KEY_EXCH_A
;
331 case SSL3_ST_CW_KEY_EXCH_A
:
332 case SSL3_ST_CW_KEY_EXCH_B
:
333 ret
=ssl3_send_client_key_exchange(s
);
334 if (ret
<= 0) goto end
;
335 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
336 /* EAY EAY EAY need to check for DH fix cert
338 /* For TLS, cert_req is set to 2, so a cert chain
339 * of nothing is sent, but no verify packet is sent */
340 if (s
->s3
->tmp
.cert_req
== 1)
342 s
->state
=SSL3_ST_CW_CERT_VRFY_A
;
346 s
->state
=SSL3_ST_CW_CHANGE_A
;
347 s
->s3
->change_cipher_spec
=0;
353 case SSL3_ST_CW_CERT_VRFY_A
:
354 case SSL3_ST_CW_CERT_VRFY_B
:
355 ret
=ssl3_send_client_verify(s
);
356 if (ret
<= 0) goto end
;
357 s
->state
=SSL3_ST_CW_CHANGE_A
;
359 s
->s3
->change_cipher_spec
=0;
362 case SSL3_ST_CW_CHANGE_A
:
363 case SSL3_ST_CW_CHANGE_B
:
364 ret
=ssl3_send_change_cipher_spec(s
,
365 SSL3_ST_CW_CHANGE_A
,SSL3_ST_CW_CHANGE_B
);
366 if (ret
<= 0) goto end
;
367 s
->state
=SSL3_ST_CW_FINISHED_A
;
370 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
371 if (s
->s3
->tmp
.new_compression
== NULL
)
372 s
->session
->compress_meth
=0;
374 s
->session
->compress_meth
=
375 s
->s3
->tmp
.new_compression
->id
;
376 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
382 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
383 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
391 case SSL3_ST_CW_FINISHED_A
:
392 case SSL3_ST_CW_FINISHED_B
:
393 ret
=ssl3_send_finished(s
,
394 SSL3_ST_CW_FINISHED_A
,SSL3_ST_CW_FINISHED_B
,
395 s
->method
->ssl3_enc
->client_finished_label
,
396 s
->method
->ssl3_enc
->client_finished_label_len
);
397 if (ret
<= 0) goto end
;
398 s
->state
=SSL3_ST_CW_FLUSH
;
401 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
404 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
405 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
)
408 s
->s3
->flags
|=SSL3_FLAGS_POP_BUFFER
;
409 s
->s3
->delay_buf_pop_ret
=0;
414 s
->s3
->tmp
.next_state
=SSL3_ST_CR_FINISHED_A
;
419 case SSL3_ST_CR_FINISHED_A
:
420 case SSL3_ST_CR_FINISHED_B
:
422 ret
=ssl3_get_finished(s
,SSL3_ST_CR_FINISHED_A
,
423 SSL3_ST_CR_FINISHED_B
);
424 if (ret
<= 0) goto end
;
427 s
->state
=SSL3_ST_CW_CHANGE_A
;
433 case SSL3_ST_CW_FLUSH
:
434 /* number of bytes to be flushed */
435 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
438 s
->rwstate
=SSL_WRITING
;
439 num1
=BIO_flush(s
->wbio
);
440 if (num1
<= 0) { ret
= -1; goto end
; }
441 s
->rwstate
=SSL_NOTHING
;
444 s
->state
=s
->s3
->tmp
.next_state
;
448 /* clean a few things up */
449 ssl3_cleanup_key_block(s
);
451 if (s
->init_buf
!= NULL
)
453 BUF_MEM_free(s
->init_buf
);
457 /* If we are not 'joining' the last two packets,
458 * remove the buffering now */
459 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
460 ssl_free_wbio_buffer(s
);
461 /* else do it later in ssl3_write */
466 ssl_update_cache(s
,SSL_SESS_CACHE_CLIENT
);
467 if (s
->hit
) s
->ctx
->stats
.sess_hit
++;
471 s
->handshake_func
=ssl3_connect
;
472 s
->ctx
->stats
.sess_connect_good
++;
474 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
480 SSLerr(SSL_F_SSL3_CONNECT
,SSL_R_UNKNOWN_STATE
);
486 /* did we do anything */
487 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
491 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
495 if ((cb
!= NULL
) && (s
->state
!= state
))
499 cb(s
,SSL_CB_CONNECT_LOOP
,1);
510 cb(s
,SSL_CB_CONNECT_EXIT
,ret
);
515 static int ssl3_client_hello(SSL
*s
)
520 unsigned long Time
,l
;
523 buf
=(unsigned char *)s
->init_buf
->data
;
524 if (s
->state
== SSL3_ST_CW_CLNT_HELLO_A
)
526 if ((s
->session
== NULL
) ||
527 (s
->session
->ssl_version
!= s
->version
) ||
528 (s
->session
->not_resumable
))
530 if (!ssl_get_new_session(s
,0))
533 /* else use the pre-loaded session */
535 p
=s
->s3
->client_random
;
536 Time
=time(NULL
); /* Time */
538 if(RAND_pseudo_bytes(p
,SSL3_RANDOM_SIZE
-sizeof(Time
)) <= 0)
541 /* Do the message type and length last */
544 *(p
++)=s
->version
>>8;
545 *(p
++)=s
->version
&0xff;
546 s
->client_version
=s
->version
;
549 memcpy(p
,s
->s3
->client_random
,SSL3_RANDOM_SIZE
);
556 i
=s
->session
->session_id_length
;
560 if (i
> sizeof s
->session
->session_id
)
562 SSLerr(SSL_F_SSL3_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
565 memcpy(p
,s
->session
->session_id
,i
);
569 /* Ciphers supported */
570 i
=ssl_cipher_list_to_bytes(s
,SSL_get_ciphers(s
),&(p
[2]));
573 SSLerr(SSL_F_SSL3_CLIENT_HELLO
,SSL_R_NO_CIPHERS_AVAILABLE
);
580 if (s
->ctx
->comp_methods
== NULL
)
583 j
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
587 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,i
);
590 *(p
++)=0; /* Add the NULL method */
594 *(d
++)=SSL3_MT_CLIENT_HELLO
;
597 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
598 /* number of bytes to write */
603 /* SSL3_ST_CW_CLNT_HELLO_B */
604 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
609 static int ssl3_get_server_hello(SSL
*s
)
611 STACK_OF(SSL_CIPHER
) *sk
;
619 n
=ssl3_get_message(s
,
620 SSL3_ST_CR_SRVR_HELLO_A
,
621 SSL3_ST_CR_SRVR_HELLO_B
,
622 SSL3_MT_SERVER_HELLO
,
626 if (!ok
) return((int)n
);
627 d
=p
=(unsigned char *)s
->init_msg
;
629 if ((p
[0] != (s
->version
>>8)) || (p
[1] != (s
->version
&0xff)))
631 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_SSL_VERSION
);
632 s
->version
=(s
->version
&0xff00)|p
[1];
633 al
=SSL_AD_PROTOCOL_VERSION
;
638 /* load the server hello data */
639 /* load the server random */
640 memcpy(s
->s3
->server_random
,p
,SSL3_RANDOM_SIZE
);
643 /* get the session-id */
646 if ((j
> sizeof s
->session
->session_id
) || (j
> SSL3_SESSION_ID_SIZE
))
648 al
=SSL_AD_ILLEGAL_PARAMETER
;
649 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_SSL3_SESSION_ID_TOO_LONG
);
653 if (j
!= 0 && j
== s
->session
->session_id_length
654 && memcmp(p
,s
->session
->session_id
,j
) == 0)
656 if(s
->sid_ctx_length
!= s
->session
->sid_ctx_length
657 || memcmp(s
->session
->sid_ctx
,s
->sid_ctx
,s
->sid_ctx_length
))
659 /* actually a client application bug */
660 al
=SSL_AD_ILLEGAL_PARAMETER
;
661 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
666 else /* a miss or crap from the other end */
668 /* If we were trying for session-id reuse, make a new
669 * SSL_SESSION so we don't stuff up other people */
671 if (s
->session
->session_id_length
> 0)
673 if (!ssl_get_new_session(s
,0))
675 al
=SSL_AD_INTERNAL_ERROR
;
679 s
->session
->session_id_length
=j
;
680 memcpy(s
->session
->session_id
,p
,j
); /* j could be 0 */
683 c
=ssl_get_cipher_by_char(s
,p
);
687 al
=SSL_AD_ILLEGAL_PARAMETER
;
688 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNKNOWN_CIPHER_RETURNED
);
691 p
+=ssl_put_cipher_by_char(s
,NULL
,NULL
);
693 sk
=ssl_get_ciphers_by_id(s
);
694 i
=sk_SSL_CIPHER_find(sk
,c
);
697 /* we did not say we would use this cipher */
698 al
=SSL_AD_ILLEGAL_PARAMETER
;
699 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_WRONG_CIPHER_RETURNED
);
703 /* Depending on the session caching (internal/external), the cipher
704 and/or cipher_id values may not be set. Make sure that
705 cipher_id is set and use it for comparison. */
706 if (s
->session
->cipher
)
707 s
->session
->cipher_id
= s
->session
->cipher
->id
;
708 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
))
711 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
))
713 al
=SSL_AD_ILLEGAL_PARAMETER
;
714 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
718 s
->s3
->tmp
.new_cipher
=c
;
720 /* lets get the compression algorithm */
726 comp
=ssl3_comp_find(s
->ctx
->comp_methods
,j
);
728 if ((j
!= 0) && (comp
== NULL
))
730 al
=SSL_AD_ILLEGAL_PARAMETER
;
731 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
736 s
->s3
->tmp
.new_compression
=comp
;
741 /* wrong packet length */
742 al
=SSL_AD_DECODE_ERROR
;
743 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO
,SSL_R_BAD_PACKET_LENGTH
);
749 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
754 static int ssl3_get_server_certificate(SSL
*s
)
757 unsigned long n
,nc
,llen
,l
;
759 unsigned char *p
,*d
,*q
;
760 STACK_OF(X509
) *sk
=NULL
;
763 int need_cert
= 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
765 n
=ssl3_get_message(s
,
772 if (!ok
) return((int)n
);
774 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_KEY_EXCHANGE
)
776 s
->s3
->tmp
.reuse_message
=1;
780 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
782 al
=SSL_AD_UNEXPECTED_MESSAGE
;
783 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_BAD_MESSAGE_TYPE
);
786 d
=p
=(unsigned char *)s
->init_msg
;
788 if ((sk
=sk_X509_new_null()) == NULL
)
790 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
797 al
=SSL_AD_DECODE_ERROR
;
798 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
801 for (nc
=0; nc
<llen
; )
806 al
=SSL_AD_DECODE_ERROR
;
807 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
812 x
=d2i_X509(NULL
,&q
,l
);
815 al
=SSL_AD_BAD_CERTIFICATE
;
816 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_ASN1_LIB
);
821 al
=SSL_AD_DECODE_ERROR
;
822 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
825 if (!sk_X509_push(sk
,x
))
827 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
835 i
=ssl_verify_cert_chain(s
,sk
);
836 if ((s
->verify_mode
!= SSL_VERIFY_NONE
) && (!i
)
837 #ifndef OPENSSL_NO_KRB5
838 && (s
->s3
->tmp
.new_cipher
->algorithms
& (SSL_MKEY_MASK
|SSL_AUTH_MASK
))
839 != (SSL_aKRB5
|SSL_kKRB5
)
840 #endif /* OPENSSL_NO_KRB5 */
843 al
=ssl_verify_alarm_type(s
->verify_result
);
844 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,SSL_R_CERTIFICATE_VERIFY_FAILED
);
847 ERR_clear_error(); /* but we keep s->verify_result */
849 sc
=ssl_sess_cert_new();
850 if (sc
== NULL
) goto err
;
852 if (s
->session
->sess_cert
) ssl_sess_cert_free(s
->session
->sess_cert
);
853 s
->session
->sess_cert
=sc
;
856 /* Inconsistency alert: cert_chain does include the peer's
857 * certificate, which we don't include in s3_srvr.c */
858 x
=sk_X509_value(sk
,0);
860 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
862 pkey
=X509_get_pubkey(x
);
864 /* VRS: allow null cert if auth == KRB5 */
865 need_cert
= ((s
->s3
->tmp
.new_cipher
->algorithms
866 & (SSL_MKEY_MASK
|SSL_AUTH_MASK
))
867 == (SSL_aKRB5
|SSL_kKRB5
))? 0: 1;
870 printf("pkey,x = %p, %p\n", pkey
,x
);
871 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x
,pkey
));
872 printf("cipher, alg, nc = %s, %lx, %d\n", s
->s3
->tmp
.new_cipher
->name
,
873 s
->s3
->tmp
.new_cipher
->algorithms
, need_cert
);
874 #endif /* KSSL_DEBUG */
876 if (need_cert
&& ((pkey
== NULL
) || EVP_PKEY_missing_parameters(pkey
)))
880 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
881 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
885 i
=ssl_cert_type(x
,pkey
);
886 if (need_cert
&& i
< 0)
890 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE
,
891 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
897 sc
->peer_cert_type
=i
;
898 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
899 /* Why would the following ever happen?
900 * We just created sc a couple of lines ago. */
901 if (sc
->peer_pkeys
[i
].x509
!= NULL
)
902 X509_free(sc
->peer_pkeys
[i
].x509
);
903 sc
->peer_pkeys
[i
].x509
=x
;
904 sc
->peer_key
= &(sc
->peer_pkeys
[i
]);
906 if (s
->session
->peer
!= NULL
)
907 X509_free(s
->session
->peer
);
908 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
913 sc
->peer_cert_type
=i
;
916 if (s
->session
->peer
!= NULL
)
917 X509_free(s
->session
->peer
);
918 s
->session
->peer
=NULL
;
920 s
->session
->verify_result
= s
->verify_result
;
928 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
933 sk_X509_pop_free(sk
,X509_free
);
937 static int ssl3_get_key_exchange(SSL
*s
)
939 #ifndef OPENSSL_NO_RSA
940 unsigned char *q
,md_buf
[EVP_MAX_MD_SIZE
*2];
943 unsigned char *param
,*p
;
944 int al
,i
,j
,param_len
,ok
;
947 #ifndef OPENSSL_NO_RSA
950 #ifndef OPENSSL_NO_DH
954 /* use same message size as in ssl3_get_certificate_request()
955 * as ServerKeyExchange message may be skipped */
956 n
=ssl3_get_message(s
,
957 SSL3_ST_CR_KEY_EXCH_A
,
958 SSL3_ST_CR_KEY_EXCH_B
,
963 if (!ok
) return((int)n
);
965 if (s
->s3
->tmp
.message_type
!= SSL3_MT_SERVER_KEY_EXCHANGE
)
967 s
->s3
->tmp
.reuse_message
=1;
971 param
=p
=(unsigned char *)s
->init_msg
;
973 if (s
->session
->sess_cert
!= NULL
)
975 #ifndef OPENSSL_NO_RSA
976 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
978 RSA_free(s
->session
->sess_cert
->peer_rsa_tmp
);
979 s
->session
->sess_cert
->peer_rsa_tmp
=NULL
;
982 #ifndef OPENSSL_NO_DH
983 if (s
->session
->sess_cert
->peer_dh_tmp
)
985 DH_free(s
->session
->sess_cert
->peer_dh_tmp
);
986 s
->session
->sess_cert
->peer_dh_tmp
=NULL
;
992 s
->session
->sess_cert
=ssl_sess_cert_new();
996 alg
=s
->s3
->tmp
.new_cipher
->algorithms
;
997 EVP_MD_CTX_init(&md_ctx
);
999 #ifndef OPENSSL_NO_RSA
1002 if ((rsa
=RSA_new()) == NULL
)
1004 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
1011 al
=SSL_AD_DECODE_ERROR
;
1012 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_MODULUS_LENGTH
);
1015 if (!(rsa
->n
=BN_bin2bn(p
,i
,rsa
->n
)))
1017 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1026 al
=SSL_AD_DECODE_ERROR
;
1027 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_E_LENGTH
);
1030 if (!(rsa
->e
=BN_bin2bn(p
,i
,rsa
->e
)))
1032 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1038 /* this should be because we are using an export cipher */
1040 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1043 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1046 s
->session
->sess_cert
->peer_rsa_tmp
=rsa
;
1049 #else /* OPENSSL_NO_RSA */
1053 #ifndef OPENSSL_NO_DH
1054 else if (alg
& SSL_kEDH
)
1056 if ((dh
=DH_new()) == NULL
)
1058 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1065 al
=SSL_AD_DECODE_ERROR
;
1066 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_P_LENGTH
);
1069 if (!(dh
->p
=BN_bin2bn(p
,i
,NULL
)))
1071 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1080 al
=SSL_AD_DECODE_ERROR
;
1081 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_G_LENGTH
);
1084 if (!(dh
->g
=BN_bin2bn(p
,i
,NULL
)))
1086 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1095 al
=SSL_AD_DECODE_ERROR
;
1096 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_DH_PUB_KEY_LENGTH
);
1099 if (!(dh
->pub_key
=BN_bin2bn(p
,i
,NULL
)))
1101 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_BN_LIB
);
1107 #ifndef OPENSSL_NO_RSA
1109 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1114 #ifndef OPENSSL_NO_DSA
1115 else if (alg
& SSL_aDSS
)
1116 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_DSA_SIGN
].x509
);
1118 /* else anonymous DH, so no certificate or pkey. */
1120 s
->session
->sess_cert
->peer_dh_tmp
=dh
;
1123 else if ((alg
& SSL_kDHr
) || (alg
& SSL_kDHd
))
1125 al
=SSL_AD_ILLEGAL_PARAMETER
;
1126 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1129 #endif /* !OPENSSL_NO_DH */
1132 al
=SSL_AD_HANDSHAKE_FAILURE
;
1133 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER
);
1138 /* p points to the next byte, there are 'n' bytes left */
1141 /* if it was signed, check the signature */
1146 j
=EVP_PKEY_size(pkey
);
1148 if ((i
!= n
) || (n
> j
) || (n
<= 0))
1150 /* wrong packet length */
1151 al
=SSL_AD_DECODE_ERROR
;
1152 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_WRONG_SIGNATURE_LENGTH
);
1156 #ifndef OPENSSL_NO_RSA
1157 if (pkey
->type
== EVP_PKEY_RSA
)
1163 for (num
=2; num
> 0; num
--)
1165 EVP_DigestInit_ex(&md_ctx
,(num
== 2)
1166 ?s
->ctx
->md5
:s
->ctx
->sha1
, NULL
);
1167 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1168 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1169 EVP_DigestUpdate(&md_ctx
,param
,param_len
);
1171 if(s
->version
== TLS1_VERSION
&& num
== 2)
1175 EVP_DigestFinal_ex(&md_ctx
,q
,(unsigned int *)&i
);
1177 if(s
->version
== TLS1_VERSION
&& num
== 2)
1183 i
=RSA_verify(NID_md5_sha1
, md_buf
, j
, p
, n
,
1187 al
=SSL_AD_DECRYPT_ERROR
;
1188 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_RSA_DECRYPT
);
1194 al
=SSL_AD_DECRYPT_ERROR
;
1195 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SIGNATURE
);
1201 #ifndef OPENSSL_NO_DSA
1202 if (pkey
->type
== EVP_PKEY_DSA
)
1205 EVP_VerifyInit_ex(&md_ctx
,EVP_dss1(), NULL
);
1206 EVP_VerifyUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1207 EVP_VerifyUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1208 EVP_VerifyUpdate(&md_ctx
,param
,param_len
);
1209 if (!EVP_VerifyFinal(&md_ctx
,p
,(int)n
,pkey
))
1212 al
=SSL_AD_DECRYPT_ERROR
;
1213 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_BAD_SIGNATURE
);
1220 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1226 /* still data left over */
1227 if (!(alg
& SSL_aNULL
))
1229 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1234 al
=SSL_AD_DECODE_ERROR
;
1235 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE
,SSL_R_EXTRA_DATA_IN_MESSAGE
);
1239 EVP_PKEY_free(pkey
);
1240 EVP_MD_CTX_cleanup(&md_ctx
);
1243 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1245 EVP_PKEY_free(pkey
);
1246 #ifndef OPENSSL_NO_RSA
1250 #ifndef OPENSSL_NO_DH
1254 EVP_MD_CTX_cleanup(&md_ctx
);
1258 static int ssl3_get_certificate_request(SSL
*s
)
1261 unsigned long n
,nc
,l
;
1262 unsigned int llen
,ctype_num
,i
;
1264 unsigned char *p
,*d
,*q
;
1265 STACK_OF(X509_NAME
) *ca_sk
=NULL
;
1267 n
=ssl3_get_message(s
,
1268 SSL3_ST_CR_CERT_REQ_A
,
1269 SSL3_ST_CR_CERT_REQ_B
,
1274 if (!ok
) return((int)n
);
1276 s
->s3
->tmp
.cert_req
=0;
1278 if (s
->s3
->tmp
.message_type
== SSL3_MT_SERVER_DONE
)
1280 s
->s3
->tmp
.reuse_message
=1;
1284 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_REQUEST
)
1286 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
1287 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_WRONG_MESSAGE_TYPE
);
1291 /* TLS does not like anon-DH with client cert */
1292 if (s
->version
> SSL3_VERSION
)
1294 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1297 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_UNEXPECTED_MESSAGE
);
1298 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER
);
1303 d
=p
=(unsigned char *)s
->init_msg
;
1305 if ((ca_sk
=sk_X509_NAME_new(ca_dn_cmp
)) == NULL
)
1307 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_MALLOC_FAILURE
);
1311 /* get the certificate types */
1313 if (ctype_num
> SSL3_CT_NUMBER
)
1314 ctype_num
=SSL3_CT_NUMBER
;
1315 for (i
=0; i
<ctype_num
; i
++)
1316 s
->s3
->tmp
.ctype
[i
]= p
[i
];
1319 /* get the CA RDNs */
1324 out
=fopen("/tmp/vsign.der","w");
1325 fwrite(p
,1,llen
,out
);
1330 if ((llen
+ctype_num
+2+1) != n
)
1332 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1333 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_LENGTH_MISMATCH
);
1337 for (nc
=0; nc
<llen
; )
1340 if ((l
+nc
+2) > llen
)
1342 if ((s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1343 goto cont
; /* netscape bugs */
1344 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1345 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_CA_DN_TOO_LONG
);
1351 if ((xn
=d2i_X509_NAME(NULL
,&q
,l
)) == NULL
)
1353 /* If netscape tolerance is on, ignore errors */
1354 if (s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
)
1358 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1359 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_ASN1_LIB
);
1366 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1367 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,SSL_R_CA_DN_LENGTH_MISMATCH
);
1370 if (!sk_X509_NAME_push(ca_sk
,xn
))
1372 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST
,ERR_R_MALLOC_FAILURE
);
1386 /* we should setup a certificate to return.... */
1387 s
->s3
->tmp
.cert_req
=1;
1388 s
->s3
->tmp
.ctype_num
=ctype_num
;
1389 if (s
->s3
->tmp
.ca_names
!= NULL
)
1390 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
,X509_NAME_free
);
1391 s
->s3
->tmp
.ca_names
=ca_sk
;
1396 if (ca_sk
!= NULL
) sk_X509_NAME_pop_free(ca_sk
,X509_NAME_free
);
1400 static int ca_dn_cmp(const X509_NAME
* const *a
, const X509_NAME
* const *b
)
1402 return(X509_NAME_cmp(*a
,*b
));
1405 static int ssl3_get_server_done(SSL
*s
)
1410 n
=ssl3_get_message(s
,
1411 SSL3_ST_CR_SRVR_DONE_A
,
1412 SSL3_ST_CR_SRVR_DONE_B
,
1413 SSL3_MT_SERVER_DONE
,
1414 30, /* should be very small, like 0 :-) */
1417 if (!ok
) return((int)n
);
1420 /* should contain no data */
1421 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_DECODE_ERROR
);
1422 SSLerr(SSL_F_SSL3_GET_SERVER_DONE
,SSL_R_LENGTH_MISMATCH
);
1429 static int ssl3_send_client_key_exchange(SSL
*s
)
1431 unsigned char *p
,*d
;
1434 #ifndef OPENSSL_NO_RSA
1436 EVP_PKEY
*pkey
=NULL
;
1438 #ifndef OPENSSL_NO_KRB5
1440 #endif /* OPENSSL_NO_KRB5 */
1442 if (s
->state
== SSL3_ST_CW_KEY_EXCH_A
)
1444 d
=(unsigned char *)s
->init_buf
->data
;
1447 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1449 /* Fool emacs indentation */
1451 #ifndef OPENSSL_NO_RSA
1452 else if (l
& SSL_kRSA
)
1455 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
1457 if (s
->session
->sess_cert
->peer_rsa_tmp
!= NULL
)
1458 rsa
=s
->session
->sess_cert
->peer_rsa_tmp
;
1461 pkey
=X509_get_pubkey(s
->session
->sess_cert
->peer_pkeys
[SSL_PKEY_RSA_ENC
].x509
);
1462 if ((pkey
== NULL
) ||
1463 (pkey
->type
!= EVP_PKEY_RSA
) ||
1464 (pkey
->pkey
.rsa
== NULL
))
1466 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1470 EVP_PKEY_free(pkey
);
1473 tmp_buf
[0]=s
->client_version
>>8;
1474 tmp_buf
[1]=s
->client_version
&0xff;
1475 if (RAND_bytes(&(tmp_buf
[2]),sizeof tmp_buf
-2) <= 0)
1478 s
->session
->master_key_length
=sizeof tmp_buf
;
1481 /* Fix buf for TLS and beyond */
1482 if (s
->version
> SSL3_VERSION
)
1484 n
=RSA_public_encrypt(sizeof tmp_buf
,
1485 tmp_buf
,p
,rsa
,RSA_PKCS1_PADDING
);
1487 if (s
->options
& SSL_OP_PKCS1_CHECK_1
) p
[1]++;
1488 if (s
->options
& SSL_OP_PKCS1_CHECK_2
) tmp_buf
[0]=0x70;
1492 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_RSA_ENCRYPT
);
1496 /* Fix buf for TLS and beyond */
1497 if (s
->version
> SSL3_VERSION
)
1503 s
->session
->master_key_length
=
1504 s
->method
->ssl3_enc
->generate_master_secret(s
,
1505 s
->session
->master_key
,
1506 tmp_buf
,sizeof tmp_buf
);
1507 OPENSSL_cleanse(tmp_buf
,sizeof tmp_buf
);
1510 #ifndef OPENSSL_NO_KRB5
1511 else if (l
& SSL_kKRB5
)
1513 krb5_error_code krb5rc
;
1514 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
1515 /* krb5_data krb5_ap_req; */
1516 krb5_data
*enc_ticket
;
1517 krb5_data authenticator
, *authp
= NULL
;
1518 EVP_CIPHER_CTX ciph_ctx
;
1519 EVP_CIPHER
*enc
= NULL
;
1520 unsigned char iv
[EVP_MAX_IV_LENGTH
];
1521 unsigned char tmp_buf
[SSL_MAX_MASTER_KEY_LENGTH
];
1522 unsigned char epms
[SSL_MAX_MASTER_KEY_LENGTH
1523 + EVP_MAX_IV_LENGTH
];
1524 int padl
, outl
= sizeof(epms
);
1526 EVP_CIPHER_CTX_init(&ciph_ctx
);
1529 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1531 #endif /* KSSL_DEBUG */
1535 if (KRB5SENDAUTH
) authp
= &authenticator
;
1536 #endif /* KRB5SENDAUTH */
1538 krb5rc
= kssl_cget_tkt(kssl_ctx
, &enc_ticket
, authp
,
1540 enc
= kssl_map_enc(kssl_ctx
->enctype
);
1545 printf("kssl_cget_tkt rtn %d\n", krb5rc
);
1546 if (krb5rc
&& kssl_err
.text
)
1547 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err
.text
);
1549 #endif /* KSSL_DEBUG */
1553 ssl3_send_alert(s
,SSL3_AL_FATAL
,
1554 SSL_AD_HANDSHAKE_FAILURE
);
1555 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1560 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
1561 ** in place of RFC 2712 KerberosWrapper, as in:
1563 ** Send ticket (copy to *p, set n = length)
1564 ** n = krb5_ap_req.length;
1565 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1566 ** if (krb5_ap_req.data)
1567 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1569 ** Now using real RFC 2712 KerberosWrapper
1570 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1571 ** Note: 2712 "opaque" types are here replaced
1572 ** with a 2-byte length followed by the value.
1574 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1575 ** Where "xx xx" = length bytes. Shown here with
1576 ** optional authenticator omitted.
1579 /* KerberosWrapper.Ticket */
1580 s2n(enc_ticket
->length
,p
);
1581 memcpy(p
, enc_ticket
->data
, enc_ticket
->length
);
1582 p
+= enc_ticket
->length
;
1583 n
= enc_ticket
->length
+ 2;
1585 /* KerberosWrapper.Authenticator */
1586 if (authp
&& authp
->length
)
1588 s2n(authp
->length
,p
);
1589 memcpy(p
, authp
->data
, authp
->length
);
1591 n
+= authp
->length
+ 2;
1599 s2n(0,p
);/* null authenticator length */
1603 if (RAND_bytes(tmp_buf
,sizeof tmp_buf
) <= 0)
1606 /* 20010420 VRS. Tried it this way; failed.
1607 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1608 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1609 ** kssl_ctx->length);
1610 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1613 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
1614 EVP_EncryptInit_ex(&ciph_ctx
,enc
, NULL
,
1616 EVP_EncryptUpdate(&ciph_ctx
,epms
,&outl
,tmp_buf
,
1618 EVP_EncryptFinal_ex(&ciph_ctx
,&(epms
[outl
]),&padl
);
1620 if (outl
> sizeof epms
)
1622 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1625 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
1627 /* KerberosWrapper.EncryptedPreMasterSecret */
1629 memcpy(p
, epms
, outl
);
1633 s
->session
->master_key_length
=
1634 s
->method
->ssl3_enc
->generate_master_secret(s
,
1635 s
->session
->master_key
,
1636 tmp_buf
, sizeof tmp_buf
);
1638 OPENSSL_cleanse(tmp_buf
, sizeof tmp_buf
);
1639 OPENSSL_cleanse(epms
, outl
);
1642 #ifndef OPENSSL_NO_DH
1643 else if (l
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1645 DH
*dh_srvr
,*dh_clnt
;
1647 if (s
->session
->sess_cert
->peer_dh_tmp
!= NULL
)
1648 dh_srvr
=s
->session
->sess_cert
->peer_dh_tmp
;
1651 /* we get them from the cert */
1652 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);
1653 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS
);
1657 /* generate a new random key */
1658 if ((dh_clnt
=DHparams_dup(dh_srvr
)) == NULL
)
1660 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1663 if (!DH_generate_key(dh_clnt
))
1665 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1669 /* use the 'p' output buffer for the DH key, but
1670 * make sure to clear it out afterwards */
1672 n
=DH_compute_key(p
,dh_srvr
->pub_key
,dh_clnt
);
1676 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1680 /* generate master key from the result */
1681 s
->session
->master_key_length
=
1682 s
->method
->ssl3_enc
->generate_master_secret(s
,
1683 s
->session
->master_key
,p
,n
);
1687 /* send off the data */
1688 n
=BN_num_bytes(dh_clnt
->pub_key
);
1690 BN_bn2bin(dh_clnt
->pub_key
,p
);
1695 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1700 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);
1701 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1705 *(d
++)=SSL3_MT_CLIENT_KEY_EXCHANGE
;
1708 s
->state
=SSL3_ST_CW_KEY_EXCH_B
;
1709 /* number of bytes to write */
1714 /* SSL3_ST_CW_KEY_EXCH_B */
1715 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1720 static int ssl3_send_client_verify(SSL
*s
)
1722 unsigned char *p
,*d
;
1723 unsigned char data
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
1725 #ifndef OPENSSL_NO_RSA
1729 #ifndef OPENSSL_NO_DSA
1733 if (s
->state
== SSL3_ST_CW_CERT_VRFY_A
)
1735 d
=(unsigned char *)s
->init_buf
->data
;
1737 pkey
=s
->cert
->key
->privatekey
;
1739 s
->method
->ssl3_enc
->cert_verify_mac(s
,&(s
->s3
->finish_dgst2
),
1740 &(data
[MD5_DIGEST_LENGTH
]));
1742 #ifndef OPENSSL_NO_RSA
1743 if (pkey
->type
== EVP_PKEY_RSA
)
1745 s
->method
->ssl3_enc
->cert_verify_mac(s
,
1746 &(s
->s3
->finish_dgst1
),&(data
[0]));
1747 if (RSA_sign(NID_md5_sha1
, data
,
1748 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
,
1749 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0 )
1751 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_RSA_LIB
);
1759 #ifndef OPENSSL_NO_DSA
1760 if (pkey
->type
== EVP_PKEY_DSA
)
1762 if (!DSA_sign(pkey
->save_type
,
1763 &(data
[MD5_DIGEST_LENGTH
]),
1764 SHA_DIGEST_LENGTH
,&(p
[2]),
1765 (unsigned int *)&j
,pkey
->pkey
.dsa
))
1767 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_DSA_LIB
);
1776 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY
,ERR_R_INTERNAL_ERROR
);
1779 *(d
++)=SSL3_MT_CERTIFICATE_VERIFY
;
1782 s
->state
=SSL3_ST_CW_CERT_VRFY_B
;
1783 s
->init_num
=(int)n
+4;
1786 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1791 static int ssl3_send_client_certificate(SSL
*s
)
1794 EVP_PKEY
*pkey
=NULL
;
1798 if (s
->state
== SSL3_ST_CW_CERT_A
)
1800 if ((s
->cert
== NULL
) ||
1801 (s
->cert
->key
->x509
== NULL
) ||
1802 (s
->cert
->key
->privatekey
== NULL
))
1803 s
->state
=SSL3_ST_CW_CERT_B
;
1805 s
->state
=SSL3_ST_CW_CERT_C
;
1808 /* We need to get a client cert */
1809 if (s
->state
== SSL3_ST_CW_CERT_B
)
1811 /* If we get an error, we need to
1812 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1813 * We then get retied later */
1815 if (s
->ctx
->client_cert_cb
!= NULL
)
1816 i
=s
->ctx
->client_cert_cb(s
,&(x509
),&(pkey
));
1819 s
->rwstate
=SSL_X509_LOOKUP
;
1822 s
->rwstate
=SSL_NOTHING
;
1823 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
))
1825 s
->state
=SSL3_ST_CW_CERT_B
;
1826 if ( !SSL_use_certificate(s
,x509
) ||
1827 !SSL_use_PrivateKey(s
,pkey
))
1833 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE
,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
1836 if (x509
!= NULL
) X509_free(x509
);
1837 if (pkey
!= NULL
) EVP_PKEY_free(pkey
);
1840 if (s
->version
== SSL3_VERSION
)
1842 s
->s3
->tmp
.cert_req
=0;
1843 ssl3_send_alert(s
,SSL3_AL_WARNING
,SSL_AD_NO_CERTIFICATE
);
1848 s
->s3
->tmp
.cert_req
=2;
1852 /* Ok, we have a cert */
1853 s
->state
=SSL3_ST_CW_CERT_C
;
1856 if (s
->state
== SSL3_ST_CW_CERT_C
)
1858 s
->state
=SSL3_ST_CW_CERT_D
;
1859 l
=ssl3_output_cert_chain(s
,
1860 (s
->s3
->tmp
.cert_req
== 2)?NULL
:s
->cert
->key
->x509
);
1864 /* SSL3_ST_CW_CERT_D */
1865 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1868 #define has_bits(i,m) (((i)&(m)) == (m))
1870 static int ssl3_check_cert_and_algorithm(SSL
*s
)
1874 EVP_PKEY
*pkey
=NULL
;
1876 #ifndef OPENSSL_NO_RSA
1879 #ifndef OPENSSL_NO_DH
1883 sc
=s
->session
->sess_cert
;
1887 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,ERR_R_INTERNAL_ERROR
);
1891 algs
=s
->s3
->tmp
.new_cipher
->algorithms
;
1893 /* we don't have a certificate */
1894 if (algs
& (SSL_aDH
|SSL_aNULL
|SSL_aKRB5
))
1897 #ifndef OPENSSL_NO_RSA
1898 rsa
=s
->session
->sess_cert
->peer_rsa_tmp
;
1900 #ifndef OPENSSL_NO_DH
1901 dh
=s
->session
->sess_cert
->peer_dh_tmp
;
1904 /* This is the passed certificate */
1906 idx
=sc
->peer_cert_type
;
1907 pkey
=X509_get_pubkey(sc
->peer_pkeys
[idx
].x509
);
1908 i
=X509_certificate_type(sc
->peer_pkeys
[idx
].x509
,pkey
);
1909 EVP_PKEY_free(pkey
);
1912 /* Check that we have a certificate if we require one */
1913 if ((algs
& SSL_aRSA
) && !has_bits(i
,EVP_PK_RSA
|EVP_PKT_SIGN
))
1915 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_RSA_SIGNING_CERT
);
1918 #ifndef OPENSSL_NO_DSA
1919 else if ((algs
& SSL_aDSS
) && !has_bits(i
,EVP_PK_DSA
|EVP_PKT_SIGN
))
1921 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DSA_SIGNING_CERT
);
1925 #ifndef OPENSSL_NO_RSA
1926 if ((algs
& SSL_kRSA
) &&
1927 !(has_bits(i
,EVP_PK_RSA
|EVP_PKT_ENC
) || (rsa
!= NULL
)))
1929 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
1933 #ifndef OPENSSL_NO_DH
1934 if ((algs
& SSL_kEDH
) &&
1935 !(has_bits(i
,EVP_PK_DH
|EVP_PKT_EXCH
) || (dh
!= NULL
)))
1937 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_KEY
);
1940 else if ((algs
& SSL_kDHr
) && !has_bits(i
,EVP_PK_DH
|EVP_PKS_RSA
))
1942 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_RSA_CERT
);
1945 #ifndef OPENSSL_NO_DSA
1946 else if ((algs
& SSL_kDHd
) && !has_bits(i
,EVP_PK_DH
|EVP_PKS_DSA
))
1948 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_DH_DSA_CERT
);
1954 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) && !has_bits(i
,EVP_PKT_EXP
))
1956 #ifndef OPENSSL_NO_RSA
1957 if (algs
& SSL_kRSA
)
1960 || RSA_size(rsa
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
))
1962 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_EXPORT_TMP_RSA_KEY
);
1968 #ifndef OPENSSL_NO_DH
1969 if (algs
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1972 || DH_size(dh
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
))
1974 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_MISSING_EXPORT_TMP_DH_KEY
);
1981 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1987 ssl3_send_alert(s
,SSL3_AL_FATAL
,SSL_AD_HANDSHAKE_FAILURE
);