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 "cryptlib.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;
150 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
152 SSLv3_client_data.ssl_connect=ssl3_connect;
153 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
155 return(&SSLv3_client_data);
158 int ssl3_connect(SSL *s)
161 unsigned long Time=time(NULL),l;
163 void (*cb)(const SSL *ssl,int type,int val)=NULL;
165 int new_state,state,skip=0;;
167 RAND_add(&Time,sizeof(Time),0);
171 if (s->info_callback != NULL)
173 else if (s->ctx->info_callback != NULL)
174 cb=s->ctx->info_callback;
177 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
185 case SSL_ST_RENEGOTIATE:
187 s->state=SSL_ST_CONNECT;
188 s->ctx->stats.sess_connect_renegotiate++;
192 case SSL_ST_BEFORE|SSL_ST_CONNECT:
193 case SSL_ST_OK|SSL_ST_CONNECT:
196 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
198 if ((s->version & 0xff00 ) != 0x0300)
200 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
205 /* s->version=SSL3_VERSION; */
206 s->type=SSL_ST_CONNECT;
208 if (s->init_buf == NULL)
210 if ((buf=BUF_MEM_new()) == NULL)
215 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
223 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
225 /* setup buffing BIO */
226 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
228 /* don't push the buffering BIO quite yet */
230 ssl3_init_finished_mac(s);
232 s->state=SSL3_ST_CW_CLNT_HELLO_A;
233 s->ctx->stats.sess_connect++;
237 case SSL3_ST_CW_CLNT_HELLO_A:
238 case SSL3_ST_CW_CLNT_HELLO_B:
241 ret=ssl3_client_hello(s);
242 if (ret <= 0) goto end;
243 s->state=SSL3_ST_CR_SRVR_HELLO_A;
246 /* turn on buffering for the next lot of output */
247 if (s->bbio != s->wbio)
248 s->wbio=BIO_push(s->bbio,s->wbio);
252 case SSL3_ST_CR_SRVR_HELLO_A:
253 case SSL3_ST_CR_SRVR_HELLO_B:
254 ret=ssl3_get_server_hello(s);
255 if (ret <= 0) goto end;
257 s->state=SSL3_ST_CR_FINISHED_A;
259 s->state=SSL3_ST_CR_CERT_A;
263 case SSL3_ST_CR_CERT_A:
264 case SSL3_ST_CR_CERT_B:
265 /* Check if it is anon DH */
266 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
268 ret=ssl3_get_server_certificate(s);
269 if (ret <= 0) goto end;
273 s->state=SSL3_ST_CR_KEY_EXCH_A;
277 case SSL3_ST_CR_KEY_EXCH_A:
278 case SSL3_ST_CR_KEY_EXCH_B:
279 ret=ssl3_get_key_exchange(s);
280 if (ret <= 0) goto end;
281 s->state=SSL3_ST_CR_CERT_REQ_A;
284 /* at this point we check that we have the
285 * required stuff from the server */
286 if (!ssl3_check_cert_and_algorithm(s))
293 case SSL3_ST_CR_CERT_REQ_A:
294 case SSL3_ST_CR_CERT_REQ_B:
295 ret=ssl3_get_certificate_request(s);
296 if (ret <= 0) goto end;
297 s->state=SSL3_ST_CR_SRVR_DONE_A;
301 case SSL3_ST_CR_SRVR_DONE_A:
302 case SSL3_ST_CR_SRVR_DONE_B:
303 ret=ssl3_get_server_done(s);
304 if (ret <= 0) goto end;
305 if (s->s3->tmp.cert_req)
306 s->state=SSL3_ST_CW_CERT_A;
308 s->state=SSL3_ST_CW_KEY_EXCH_A;
313 case SSL3_ST_CW_CERT_A:
314 case SSL3_ST_CW_CERT_B:
315 case SSL3_ST_CW_CERT_C:
316 case SSL3_ST_CW_CERT_D:
317 ret=ssl3_send_client_certificate(s);
318 if (ret <= 0) goto end;
319 s->state=SSL3_ST_CW_KEY_EXCH_A;
323 case SSL3_ST_CW_KEY_EXCH_A:
324 case SSL3_ST_CW_KEY_EXCH_B:
325 ret=ssl3_send_client_key_exchange(s);
326 if (ret <= 0) goto end;
327 l=s->s3->tmp.new_cipher->algorithms;
328 /* EAY EAY EAY need to check for DH fix cert
330 /* For TLS, cert_req is set to 2, so a cert chain
331 * of nothing is sent, but no verify packet is sent */
332 if (s->s3->tmp.cert_req == 1)
334 s->state=SSL3_ST_CW_CERT_VRFY_A;
338 s->state=SSL3_ST_CW_CHANGE_A;
339 s->s3->change_cipher_spec=0;
345 case SSL3_ST_CW_CERT_VRFY_A:
346 case SSL3_ST_CW_CERT_VRFY_B:
347 ret=ssl3_send_client_verify(s);
348 if (ret <= 0) goto end;
349 s->state=SSL3_ST_CW_CHANGE_A;
351 s->s3->change_cipher_spec=0;
354 case SSL3_ST_CW_CHANGE_A:
355 case SSL3_ST_CW_CHANGE_B:
356 ret=ssl3_send_change_cipher_spec(s,
357 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
358 if (ret <= 0) goto end;
359 s->state=SSL3_ST_CW_FINISHED_A;
362 s->session->cipher=s->s3->tmp.new_cipher;
363 if (s->s3->tmp.new_compression == NULL)
364 s->session->compress_meth=0;
366 s->session->compress_meth=
367 s->s3->tmp.new_compression->id;
368 if (!s->method->ssl3_enc->setup_key_block(s))
374 if (!s->method->ssl3_enc->change_cipher_state(s,
375 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
383 case SSL3_ST_CW_FINISHED_A:
384 case SSL3_ST_CW_FINISHED_B:
385 ret=ssl3_send_finished(s,
386 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
387 s->method->ssl3_enc->client_finished_label,
388 s->method->ssl3_enc->client_finished_label_len);
389 if (ret <= 0) goto end;
390 s->state=SSL3_ST_CW_FLUSH;
393 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
396 s->s3->tmp.next_state=SSL_ST_OK;
397 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
400 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
401 s->s3->delay_buf_pop_ret=0;
406 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
411 case SSL3_ST_CR_FINISHED_A:
412 case SSL3_ST_CR_FINISHED_B:
414 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
415 SSL3_ST_CR_FINISHED_B);
416 if (ret <= 0) goto end;
419 s->state=SSL3_ST_CW_CHANGE_A;
425 case SSL3_ST_CW_FLUSH:
426 /* number of bytes to be flushed */
427 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
430 s->rwstate=SSL_WRITING;
431 num1=BIO_flush(s->wbio);
432 if (num1 <= 0) { ret= -1; goto end; }
433 s->rwstate=SSL_NOTHING;
436 s->state=s->s3->tmp.next_state;
440 /* clean a few things up */
441 ssl3_cleanup_key_block(s);
443 if (s->init_buf != NULL)
445 BUF_MEM_free(s->init_buf);
449 /* If we are not 'joining' the last two packets,
450 * remove the buffering now */
451 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
452 ssl_free_wbio_buffer(s);
453 /* else do it later in ssl3_write */
458 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
459 if (s->hit) s->ctx->stats.sess_hit++;
463 s->handshake_func=ssl3_connect;
464 s->ctx->stats.sess_connect_good++;
466 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
472 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
478 /* did we do anything */
479 if (!s->s3->tmp.reuse_message && !skip)
483 if ((ret=BIO_flush(s->wbio)) <= 0)
487 if ((cb != NULL) && (s->state != state))
491 cb(s,SSL_CB_CONNECT_LOOP,1);
500 cb(s,SSL_CB_CONNECT_EXIT,ret);
505 static int ssl3_client_hello(SSL *s)
510 unsigned long Time,l;
513 buf=(unsigned char *)s->init_buf->data;
514 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
516 if ((s->session == NULL) ||
517 (s->session->ssl_version != s->version) ||
518 (s->session->not_resumable))
520 if (!ssl_get_new_session(s,0))
523 /* else use the pre-loaded session */
525 p=s->s3->client_random;
526 Time=time(NULL); /* Time */
528 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
530 /* Do the message type and length last */
533 *(p++)=s->version>>8;
534 *(p++)=s->version&0xff;
535 s->client_version=s->version;
538 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
545 i=s->session->session_id_length;
549 if (i > sizeof s->session->session_id)
551 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
554 memcpy(p,s->session->session_id,i);
558 /* Ciphers supported */
559 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
562 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
569 if (s->ctx->comp_methods == NULL)
572 j=sk_SSL_COMP_num(s->ctx->comp_methods);
576 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
579 *(p++)=0; /* Add the NULL method */
583 *(d++)=SSL3_MT_CLIENT_HELLO;
586 s->state=SSL3_ST_CW_CLNT_HELLO_B;
587 /* number of bytes to write */
592 /* SSL3_ST_CW_CLNT_HELLO_B */
593 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
598 static int ssl3_get_server_hello(SSL *s)
600 STACK_OF(SSL_CIPHER) *sk;
608 n=ssl3_get_message(s,
609 SSL3_ST_CR_SRVR_HELLO_A,
610 SSL3_ST_CR_SRVR_HELLO_B,
611 SSL3_MT_SERVER_HELLO,
615 if (!ok) return((int)n);
616 d=p=(unsigned char *)s->init_msg;
618 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
620 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
621 s->version=(s->version&0xff00)|p[1];
622 al=SSL_AD_PROTOCOL_VERSION;
627 /* load the server hello data */
628 /* load the server random */
629 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
632 /* get the session-id */
635 if(j > sizeof s->session->session_id)
637 al=SSL_AD_ILLEGAL_PARAMETER;
638 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
639 SSL_R_SSL3_SESSION_ID_TOO_LONG);
643 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
645 /* SSLref returns 16 :-( */
646 if (j < SSL2_SSL_SESSION_ID_LENGTH)
648 al=SSL_AD_ILLEGAL_PARAMETER;
649 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
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 al=SSL_AD_ILLEGAL_PARAMETER;
660 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
665 else /* a miss or crap from the other end */
667 /* If we were trying for session-id reuse, make a new
668 * SSL_SESSION so we don't stuff up other people */
670 if (s->session->session_id_length > 0)
672 if (!ssl_get_new_session(s,0))
674 al=SSL_AD_INTERNAL_ERROR;
678 s->session->session_id_length=j;
679 memcpy(s->session->session_id,p,j); /* j could be 0 */
682 c=ssl_get_cipher_by_char(s,p);
686 al=SSL_AD_ILLEGAL_PARAMETER;
687 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
690 p+=ssl_put_cipher_by_char(s,NULL,NULL);
692 sk=ssl_get_ciphers_by_id(s);
693 i=sk_SSL_CIPHER_find(sk,c);
696 /* we did not say we would use this cipher */
697 al=SSL_AD_ILLEGAL_PARAMETER;
698 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
702 if (s->hit && (s->session->cipher != c))
705 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
707 al=SSL_AD_ILLEGAL_PARAMETER;
708 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
712 s->s3->tmp.new_cipher=c;
714 /* lets get the compression algorithm */
720 comp=ssl3_comp_find(s->ctx->comp_methods,j);
722 if ((j != 0) && (comp == NULL))
724 al=SSL_AD_ILLEGAL_PARAMETER;
725 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
730 s->s3->tmp.new_compression=comp;
735 /* wrong packet length */
736 al=SSL_AD_DECODE_ERROR;
737 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
743 ssl3_send_alert(s,SSL3_AL_FATAL,al);
748 static int ssl3_get_server_certificate(SSL *s)
751 unsigned long n,nc,llen,l;
753 unsigned char *p,*d,*q;
754 STACK_OF(X509) *sk=NULL;
757 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
759 n=ssl3_get_message(s,
766 if (!ok) return((int)n);
768 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
770 s->s3->tmp.reuse_message=1;
774 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
776 al=SSL_AD_UNEXPECTED_MESSAGE;
777 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
780 d=p=(unsigned char *)s->init_msg;
782 if ((sk=sk_X509_new_null()) == NULL)
784 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
791 al=SSL_AD_DECODE_ERROR;
792 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
795 for (nc=0; nc<llen; )
800 al=SSL_AD_DECODE_ERROR;
801 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
806 x=d2i_X509(NULL,&q,l);
809 al=SSL_AD_BAD_CERTIFICATE;
810 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
815 al=SSL_AD_DECODE_ERROR;
816 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
819 if (!sk_X509_push(sk,x))
821 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
829 i=ssl_verify_cert_chain(s,sk);
830 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
831 #ifndef OPENSSL_NO_KRB5
832 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
833 != (SSL_aKRB5|SSL_kKRB5)
834 #endif /* OPENSSL_NO_KRB5 */
837 al=ssl_verify_alarm_type(s->verify_result);
838 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
841 ERR_clear_error(); /* but we keep s->verify_result */
843 sc=ssl_sess_cert_new();
844 if (sc == NULL) goto err;
846 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
847 s->session->sess_cert=sc;
850 /* Inconsistency alert: cert_chain does include the peer's
851 * certificate, which we don't include in s3_srvr.c */
852 x=sk_X509_value(sk,0);
854 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
856 pkey=X509_get_pubkey(x);
858 /* VRS: allow null cert if auth == KRB5 */
859 need_cert = ((s->s3->tmp.new_cipher->algorithms
860 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
861 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
864 printf("pkey,x = %p, %p\n", pkey,x);
865 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
866 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
867 s->s3->tmp.new_cipher->algorithms, need_cert);
868 #endif /* KSSL_DEBUG */
870 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
874 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
875 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
879 i=ssl_cert_type(x,pkey);
880 if (need_cert && i < 0)
884 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
885 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
891 sc->peer_cert_type=i;
892 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
893 /* Why would the following ever happen?
894 * We just created sc a couple of lines ago. */
895 if (sc->peer_pkeys[i].x509 != NULL)
896 X509_free(sc->peer_pkeys[i].x509);
897 sc->peer_pkeys[i].x509=x;
898 sc->peer_key= &(sc->peer_pkeys[i]);
900 if (s->session->peer != NULL)
901 X509_free(s->session->peer);
902 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
907 sc->peer_cert_type=i;
910 if (s->session->peer != NULL)
911 X509_free(s->session->peer);
912 s->session->peer=NULL;
914 s->session->verify_result = s->verify_result;
922 ssl3_send_alert(s,SSL3_AL_FATAL,al);
927 sk_X509_pop_free(sk,X509_free);
931 static int ssl3_get_key_exchange(SSL *s)
933 #ifndef OPENSSL_NO_RSA
934 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
937 unsigned char *param,*p;
938 int al,i,j,param_len,ok;
941 #ifndef OPENSSL_NO_RSA
944 #ifndef OPENSSL_NO_DH
948 /* use same message size as in ssl3_get_certificate_request()
949 * as ServerKeyExchange message may be skipped */
950 n=ssl3_get_message(s,
951 SSL3_ST_CR_KEY_EXCH_A,
952 SSL3_ST_CR_KEY_EXCH_B,
957 if (!ok) return((int)n);
959 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
961 s->s3->tmp.reuse_message=1;
965 param=p=(unsigned char *)s->init_msg;
967 if (s->session->sess_cert != NULL)
969 #ifndef OPENSSL_NO_RSA
970 if (s->session->sess_cert->peer_rsa_tmp != NULL)
972 RSA_free(s->session->sess_cert->peer_rsa_tmp);
973 s->session->sess_cert->peer_rsa_tmp=NULL;
976 #ifndef OPENSSL_NO_DH
977 if (s->session->sess_cert->peer_dh_tmp)
979 DH_free(s->session->sess_cert->peer_dh_tmp);
980 s->session->sess_cert->peer_dh_tmp=NULL;
986 s->session->sess_cert=ssl_sess_cert_new();
990 alg=s->s3->tmp.new_cipher->algorithms;
991 EVP_MD_CTX_init(&md_ctx);
993 #ifndef OPENSSL_NO_RSA
996 if ((rsa=RSA_new()) == NULL)
998 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1005 al=SSL_AD_DECODE_ERROR;
1006 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1009 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1011 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1020 al=SSL_AD_DECODE_ERROR;
1021 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1024 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1026 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1032 /* this should be because we are using an export cipher */
1034 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1037 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1040 s->session->sess_cert->peer_rsa_tmp=rsa;
1043 #else /* OPENSSL_NO_RSA */
1047 #ifndef OPENSSL_NO_DH
1048 else if (alg & SSL_kEDH)
1050 if ((dh=DH_new()) == NULL)
1052 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1059 al=SSL_AD_DECODE_ERROR;
1060 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1063 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1065 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1074 al=SSL_AD_DECODE_ERROR;
1075 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1078 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1080 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1089 al=SSL_AD_DECODE_ERROR;
1090 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1093 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1095 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1101 #ifndef OPENSSL_NO_RSA
1103 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1108 #ifndef OPENSSL_NO_DSA
1109 else if (alg & SSL_aDSS)
1110 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1112 /* else anonymous DH, so no certificate or pkey. */
1114 s->session->sess_cert->peer_dh_tmp=dh;
1117 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1119 al=SSL_AD_ILLEGAL_PARAMETER;
1120 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1123 #endif /* !OPENSSL_NO_DH */
1126 al=SSL_AD_HANDSHAKE_FAILURE;
1127 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1132 /* p points to the next byte, there are 'n' bytes left */
1135 /* if it was signed, check the signature */
1140 j=EVP_PKEY_size(pkey);
1142 if ((i != n) || (n > j) || (n <= 0))
1144 /* wrong packet length */
1145 al=SSL_AD_DECODE_ERROR;
1146 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1150 #ifndef OPENSSL_NO_RSA
1151 if (pkey->type == EVP_PKEY_RSA)
1157 for (num=2; num > 0; num--)
1159 EVP_DigestInit_ex(&md_ctx,(num == 2)
1160 ?s->ctx->md5:s->ctx->sha1, NULL);
1161 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1162 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1163 EVP_DigestUpdate(&md_ctx,param,param_len);
1164 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1168 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1172 al=SSL_AD_DECRYPT_ERROR;
1173 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1179 al=SSL_AD_DECRYPT_ERROR;
1180 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1186 #ifndef OPENSSL_NO_DSA
1187 if (pkey->type == EVP_PKEY_DSA)
1190 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1191 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1192 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1193 EVP_VerifyUpdate(&md_ctx,param,param_len);
1194 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1197 al=SSL_AD_DECRYPT_ERROR;
1198 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1205 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1211 /* still data left over */
1212 if (!(alg & SSL_aNULL))
1214 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1219 al=SSL_AD_DECODE_ERROR;
1220 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1224 EVP_PKEY_free(pkey);
1225 EVP_MD_CTX_cleanup(&md_ctx);
1228 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1230 EVP_PKEY_free(pkey);
1231 #ifndef OPENSSL_NO_RSA
1235 #ifndef OPENSSL_NO_DH
1239 EVP_MD_CTX_cleanup(&md_ctx);
1243 static int ssl3_get_certificate_request(SSL *s)
1246 unsigned long n,nc,l;
1247 unsigned int llen,ctype_num,i;
1249 unsigned char *p,*d,*q;
1250 STACK_OF(X509_NAME) *ca_sk=NULL;
1252 n=ssl3_get_message(s,
1253 SSL3_ST_CR_CERT_REQ_A,
1254 SSL3_ST_CR_CERT_REQ_B,
1259 if (!ok) return((int)n);
1261 s->s3->tmp.cert_req=0;
1263 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1265 s->s3->tmp.reuse_message=1;
1269 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1271 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1272 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1276 /* TLS does not like anon-DH with client cert */
1277 if (s->version > SSL3_VERSION)
1279 l=s->s3->tmp.new_cipher->algorithms;
1282 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1283 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1288 d=p=(unsigned char *)s->init_msg;
1290 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1292 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1296 /* get the certificate types */
1298 if (ctype_num > SSL3_CT_NUMBER)
1299 ctype_num=SSL3_CT_NUMBER;
1300 for (i=0; i<ctype_num; i++)
1301 s->s3->tmp.ctype[i]= p[i];
1304 /* get the CA RDNs */
1309 out=fopen("/tmp/vsign.der","w");
1310 fwrite(p,1,llen,out);
1315 if ((llen+ctype_num+2+1) != n)
1317 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1318 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1322 for (nc=0; nc<llen; )
1325 if ((l+nc+2) > llen)
1327 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1328 goto cont; /* netscape bugs */
1329 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1330 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1336 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1338 /* If netscape tolerance is on, ignore errors */
1339 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1343 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1344 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1351 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1352 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1355 if (!sk_X509_NAME_push(ca_sk,xn))
1357 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1371 /* we should setup a certificate to return.... */
1372 s->s3->tmp.cert_req=1;
1373 s->s3->tmp.ctype_num=ctype_num;
1374 if (s->s3->tmp.ca_names != NULL)
1375 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1376 s->s3->tmp.ca_names=ca_sk;
1381 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1385 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1387 return(X509_NAME_cmp(*a,*b));
1390 static int ssl3_get_server_done(SSL *s)
1395 n=ssl3_get_message(s,
1396 SSL3_ST_CR_SRVR_DONE_A,
1397 SSL3_ST_CR_SRVR_DONE_B,
1398 SSL3_MT_SERVER_DONE,
1399 30, /* should be very small, like 0 :-) */
1402 if (!ok) return((int)n);
1405 /* should contain no data */
1406 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1407 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1414 static int ssl3_send_client_key_exchange(SSL *s)
1416 unsigned char *p,*d;
1419 #ifndef OPENSSL_NO_RSA
1421 EVP_PKEY *pkey=NULL;
1423 #ifndef OPENSSL_NO_KRB5
1425 #endif /* OPENSSL_NO_KRB5 */
1427 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1429 d=(unsigned char *)s->init_buf->data;
1432 l=s->s3->tmp.new_cipher->algorithms;
1434 /* Fool emacs indentation */
1436 #ifndef OPENSSL_NO_RSA
1437 else if (l & SSL_kRSA)
1440 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1442 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1443 rsa=s->session->sess_cert->peer_rsa_tmp;
1446 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1447 if ((pkey == NULL) ||
1448 (pkey->type != EVP_PKEY_RSA) ||
1449 (pkey->pkey.rsa == NULL))
1451 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1455 EVP_PKEY_free(pkey);
1458 tmp_buf[0]=s->client_version>>8;
1459 tmp_buf[1]=s->client_version&0xff;
1460 if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
1463 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1466 /* Fix buf for TLS and beyond */
1467 if (s->version > SSL3_VERSION)
1469 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1470 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1472 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1473 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1477 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1481 /* Fix buf for TLS and beyond */
1482 if (s->version > SSL3_VERSION)
1488 s->session->master_key_length=
1489 s->method->ssl3_enc->generate_master_secret(s,
1490 s->session->master_key,
1491 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1492 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1495 #ifndef OPENSSL_NO_KRB5
1496 else if (l & SSL_kKRB5)
1498 krb5_error_code krb5rc;
1499 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1500 /* krb5_data krb5_ap_req; */
1501 krb5_data *enc_ticket;
1502 krb5_data authenticator, *authp = NULL;
1503 EVP_CIPHER_CTX ciph_ctx;
1504 EVP_CIPHER *enc = NULL;
1505 unsigned char iv[EVP_MAX_IV_LENGTH];
1506 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1507 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
1508 + EVP_MAX_IV_LENGTH];
1509 int padl, outl = sizeof(epms);
1511 EVP_CIPHER_CTX_init(&ciph_ctx);
1514 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1516 #endif /* KSSL_DEBUG */
1520 if (KRB5SENDAUTH) authp = &authenticator;
1521 #endif /* KRB5SENDAUTH */
1523 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
1525 enc = kssl_map_enc(kssl_ctx->enctype);
1530 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1531 if (krb5rc && kssl_err.text)
1532 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1534 #endif /* KSSL_DEBUG */
1538 ssl3_send_alert(s,SSL3_AL_FATAL,
1539 SSL_AD_HANDSHAKE_FAILURE);
1540 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1545 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
1546 ** in place of RFC 2712 KerberosWrapper, as in:
1548 ** Send ticket (copy to *p, set n = length)
1549 ** n = krb5_ap_req.length;
1550 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1551 ** if (krb5_ap_req.data)
1552 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1554 ** Now using real RFC 2712 KerberosWrapper
1555 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1556 ** Note: 2712 "opaque" types are here replaced
1557 ** with a 2-byte length followed by the value.
1559 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1560 ** Where "xx xx" = length bytes. Shown here with
1561 ** optional authenticator omitted.
1564 /* KerberosWrapper.Ticket */
1565 s2n(enc_ticket->length,p);
1566 memcpy(p, enc_ticket->data, enc_ticket->length);
1567 p+= enc_ticket->length;
1568 n = enc_ticket->length + 2;
1570 /* KerberosWrapper.Authenticator */
1571 if (authp && authp->length)
1573 s2n(authp->length,p);
1574 memcpy(p, authp->data, authp->length);
1576 n+= authp->length + 2;
1584 s2n(0,p);/* null authenticator length */
1588 if (RAND_bytes(tmp_buf,SSL_MAX_MASTER_KEY_LENGTH) <= 0)
1591 /* 20010420 VRS. Tried it this way; failed.
1592 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1593 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1594 ** kssl_ctx->length);
1595 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1598 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1599 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
1601 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
1602 SSL_MAX_MASTER_KEY_LENGTH);
1603 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
1605 if (outl > sizeof epms)
1607 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1610 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1612 /* KerberosWrapper.EncryptedPreMasterSecret */
1614 memcpy(p, epms, outl);
1618 s->session->master_key_length=
1619 s->method->ssl3_enc->generate_master_secret(s,
1620 s->session->master_key,
1621 tmp_buf, SSL_MAX_MASTER_KEY_LENGTH);
1623 memset(tmp_buf, 0, SSL_MAX_MASTER_KEY_LENGTH);
1624 memset(epms, 0, outl);
1627 #ifndef OPENSSL_NO_DH
1628 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1630 DH *dh_srvr,*dh_clnt;
1632 if (s->session->sess_cert->peer_dh_tmp != NULL)
1633 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1636 /* we get them from the cert */
1637 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1638 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1642 /* generate a new random key */
1643 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1645 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1648 if (!DH_generate_key(dh_clnt))
1650 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1654 /* use the 'p' output buffer for the DH key, but
1655 * make sure to clear it out afterwards */
1657 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1661 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1665 /* generate master key from the result */
1666 s->session->master_key_length=
1667 s->method->ssl3_enc->generate_master_secret(s,
1668 s->session->master_key,p,n);
1672 /* send off the data */
1673 n=BN_num_bytes(dh_clnt->pub_key);
1675 BN_bn2bin(dh_clnt->pub_key,p);
1680 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1685 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1686 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1690 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1693 s->state=SSL3_ST_CW_KEY_EXCH_B;
1694 /* number of bytes to write */
1699 /* SSL3_ST_CW_KEY_EXCH_B */
1700 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1705 static int ssl3_send_client_verify(SSL *s)
1707 unsigned char *p,*d;
1708 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1710 #ifndef OPENSSL_NO_RSA
1714 #ifndef OPENSSL_NO_DSA
1718 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1720 d=(unsigned char *)s->init_buf->data;
1722 pkey=s->cert->key->privatekey;
1724 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1725 &(data[MD5_DIGEST_LENGTH]));
1727 #ifndef OPENSSL_NO_RSA
1728 if (pkey->type == EVP_PKEY_RSA)
1730 s->method->ssl3_enc->cert_verify_mac(s,
1731 &(s->s3->finish_dgst1),&(data[0]));
1732 if (RSA_sign(NID_md5_sha1, data,
1733 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1734 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1736 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1744 #ifndef OPENSSL_NO_DSA
1745 if (pkey->type == EVP_PKEY_DSA)
1747 if (!DSA_sign(pkey->save_type,
1748 &(data[MD5_DIGEST_LENGTH]),
1749 SHA_DIGEST_LENGTH,&(p[2]),
1750 (unsigned int *)&j,pkey->pkey.dsa))
1752 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1761 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1764 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1767 s->init_num=(int)n+4;
1770 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1775 static int ssl3_send_client_certificate(SSL *s)
1778 EVP_PKEY *pkey=NULL;
1782 if (s->state == SSL3_ST_CW_CERT_A)
1784 if ((s->cert == NULL) ||
1785 (s->cert->key->x509 == NULL) ||
1786 (s->cert->key->privatekey == NULL))
1787 s->state=SSL3_ST_CW_CERT_B;
1789 s->state=SSL3_ST_CW_CERT_C;
1792 /* We need to get a client cert */
1793 if (s->state == SSL3_ST_CW_CERT_B)
1795 /* If we get an error, we need to
1796 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1797 * We then get retied later */
1799 if (s->ctx->client_cert_cb != NULL)
1800 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1803 s->rwstate=SSL_X509_LOOKUP;
1806 s->rwstate=SSL_NOTHING;
1807 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1809 s->state=SSL3_ST_CW_CERT_B;
1810 if ( !SSL_use_certificate(s,x509) ||
1811 !SSL_use_PrivateKey(s,pkey))
1817 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1820 if (x509 != NULL) X509_free(x509);
1821 if (pkey != NULL) EVP_PKEY_free(pkey);
1824 if (s->version == SSL3_VERSION)
1826 s->s3->tmp.cert_req=0;
1827 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1832 s->s3->tmp.cert_req=2;
1836 /* Ok, we have a cert */
1837 s->state=SSL3_ST_CW_CERT_C;
1840 if (s->state == SSL3_ST_CW_CERT_C)
1842 s->state=SSL3_ST_CW_CERT_D;
1843 l=ssl3_output_cert_chain(s,
1844 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1848 /* SSL3_ST_CW_CERT_D */
1849 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1852 #define has_bits(i,m) (((i)&(m)) == (m))
1854 static int ssl3_check_cert_and_algorithm(SSL *s)
1858 EVP_PKEY *pkey=NULL;
1860 #ifndef OPENSSL_NO_RSA
1863 #ifndef OPENSSL_NO_DH
1867 sc=s->session->sess_cert;
1871 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
1875 algs=s->s3->tmp.new_cipher->algorithms;
1877 /* we don't have a certificate */
1878 if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
1881 #ifndef OPENSSL_NO_RSA
1882 rsa=s->session->sess_cert->peer_rsa_tmp;
1884 #ifndef OPENSSL_NO_DH
1885 dh=s->session->sess_cert->peer_dh_tmp;
1888 /* This is the passed certificate */
1890 idx=sc->peer_cert_type;
1891 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1892 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1893 EVP_PKEY_free(pkey);
1896 /* Check that we have a certificate if we require one */
1897 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1899 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1902 #ifndef OPENSSL_NO_DSA
1903 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1905 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1909 #ifndef OPENSSL_NO_RSA
1910 if ((algs & SSL_kRSA) &&
1911 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1913 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1917 #ifndef OPENSSL_NO_DH
1918 if ((algs & SSL_kEDH) &&
1919 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1921 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1924 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1926 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1929 #ifndef OPENSSL_NO_DSA
1930 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1932 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1938 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
1940 #ifndef OPENSSL_NO_RSA
1941 if (algs & SSL_kRSA)
1944 || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1946 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1952 #ifndef OPENSSL_NO_DH
1953 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1956 || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1958 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1965 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1971 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);