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.]
60 #include <openssl/buffer.h>
61 #include <openssl/rand.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
66 #ifndef OPENSSL_NO_KRB5
70 static SSL_METHOD *ssl3_get_client_method(int ver);
71 static int ssl3_client_hello(SSL *s);
72 static int ssl3_get_server_hello(SSL *s);
73 static int ssl3_get_certificate_request(SSL *s);
74 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
75 static int ssl3_get_server_done(SSL *s);
76 static int ssl3_send_client_verify(SSL *s);
77 static int ssl3_send_client_certificate(SSL *s);
78 static int ssl3_send_client_key_exchange(SSL *s);
79 static int ssl3_get_key_exchange(SSL *s);
80 static int ssl3_get_server_certificate(SSL *s);
81 static int ssl3_check_cert_and_algorithm(SSL *s);
82 static SSL_METHOD *ssl3_get_client_method(int ver)
84 if (ver == SSL3_VERSION)
85 return(SSLv3_client_method());
90 SSL_METHOD *SSLv3_client_method(void)
93 static SSL_METHOD SSLv3_client_data;
98 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
100 SSLv3_client_data.ssl_connect=ssl3_connect;
101 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
103 return(&SSLv3_client_data);
106 int ssl3_connect(SSL *s)
109 unsigned long Time=time(NULL),l;
113 int new_state,state,skip=0;;
115 RAND_add(&Time,sizeof(Time),0);
119 if (s->info_callback != NULL)
121 else if (s->ctx->info_callback != NULL)
122 cb=s->ctx->info_callback;
124 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
133 case SSL_ST_RENEGOTIATE:
135 s->state=SSL_ST_CONNECT;
136 s->ctx->stats.sess_connect_renegotiate++;
140 case SSL_ST_BEFORE|SSL_ST_CONNECT:
141 case SSL_ST_OK|SSL_ST_CONNECT:
144 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
146 if ((s->version & 0xff00 ) != 0x0300)
148 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
153 /* s->version=SSL3_VERSION; */
154 s->type=SSL_ST_CONNECT;
156 if (s->init_buf == NULL)
158 if ((buf=BUF_MEM_new()) == NULL)
163 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
171 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
173 /* setup buffing BIO */
174 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
176 /* don't push the buffering BIO quite yet */
178 ssl3_init_finished_mac(s);
180 s->state=SSL3_ST_CW_CLNT_HELLO_A;
181 s->ctx->stats.sess_connect++;
185 case SSL3_ST_CW_CLNT_HELLO_A:
186 case SSL3_ST_CW_CLNT_HELLO_B:
189 ret=ssl3_client_hello(s);
190 if (ret <= 0) goto end;
191 s->state=SSL3_ST_CR_SRVR_HELLO_A;
194 /* turn on buffering for the next lot of output */
195 if (s->bbio != s->wbio)
196 s->wbio=BIO_push(s->bbio,s->wbio);
200 case SSL3_ST_CR_SRVR_HELLO_A:
201 case SSL3_ST_CR_SRVR_HELLO_B:
202 ret=ssl3_get_server_hello(s);
203 if (ret <= 0) goto end;
205 s->state=SSL3_ST_CR_FINISHED_A;
207 s->state=SSL3_ST_CR_CERT_A;
211 case SSL3_ST_CR_CERT_A:
212 case SSL3_ST_CR_CERT_B:
213 /* Check if it is anon DH */
214 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
216 ret=ssl3_get_server_certificate(s);
217 if (ret <= 0) goto end;
221 s->state=SSL3_ST_CR_KEY_EXCH_A;
225 case SSL3_ST_CR_KEY_EXCH_A:
226 case SSL3_ST_CR_KEY_EXCH_B:
227 ret=ssl3_get_key_exchange(s);
228 if (ret <= 0) goto end;
229 s->state=SSL3_ST_CR_CERT_REQ_A;
232 /* at this point we check that we have the
233 * required stuff from the server */
234 if (!ssl3_check_cert_and_algorithm(s))
241 case SSL3_ST_CR_CERT_REQ_A:
242 case SSL3_ST_CR_CERT_REQ_B:
243 ret=ssl3_get_certificate_request(s);
244 if (ret <= 0) goto end;
245 s->state=SSL3_ST_CR_SRVR_DONE_A;
249 case SSL3_ST_CR_SRVR_DONE_A:
250 case SSL3_ST_CR_SRVR_DONE_B:
251 ret=ssl3_get_server_done(s);
252 if (ret <= 0) goto end;
253 if (s->s3->tmp.cert_req)
254 s->state=SSL3_ST_CW_CERT_A;
256 s->state=SSL3_ST_CW_KEY_EXCH_A;
261 case SSL3_ST_CW_CERT_A:
262 case SSL3_ST_CW_CERT_B:
263 case SSL3_ST_CW_CERT_C:
264 case SSL3_ST_CW_CERT_D:
265 ret=ssl3_send_client_certificate(s);
266 if (ret <= 0) goto end;
267 s->state=SSL3_ST_CW_KEY_EXCH_A;
271 case SSL3_ST_CW_KEY_EXCH_A:
272 case SSL3_ST_CW_KEY_EXCH_B:
273 ret=ssl3_send_client_key_exchange(s);
274 if (ret <= 0) goto end;
275 l=s->s3->tmp.new_cipher->algorithms;
276 /* EAY EAY EAY need to check for DH fix cert
278 /* For TLS, cert_req is set to 2, so a cert chain
279 * of nothing is sent, but no verify packet is sent */
280 if (s->s3->tmp.cert_req == 1)
282 s->state=SSL3_ST_CW_CERT_VRFY_A;
286 s->state=SSL3_ST_CW_CHANGE_A;
287 s->s3->change_cipher_spec=0;
293 case SSL3_ST_CW_CERT_VRFY_A:
294 case SSL3_ST_CW_CERT_VRFY_B:
295 ret=ssl3_send_client_verify(s);
296 if (ret <= 0) goto end;
297 s->state=SSL3_ST_CW_CHANGE_A;
299 s->s3->change_cipher_spec=0;
302 case SSL3_ST_CW_CHANGE_A:
303 case SSL3_ST_CW_CHANGE_B:
304 ret=ssl3_send_change_cipher_spec(s,
305 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
306 if (ret <= 0) goto end;
307 s->state=SSL3_ST_CW_FINISHED_A;
310 s->session->cipher=s->s3->tmp.new_cipher;
311 if (s->s3->tmp.new_compression == NULL)
312 s->session->compress_meth=0;
314 s->session->compress_meth=
315 s->s3->tmp.new_compression->id;
316 if (!s->method->ssl3_enc->setup_key_block(s))
322 if (!s->method->ssl3_enc->change_cipher_state(s,
323 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
331 case SSL3_ST_CW_FINISHED_A:
332 case SSL3_ST_CW_FINISHED_B:
333 ret=ssl3_send_finished(s,
334 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
335 s->method->ssl3_enc->client_finished_label,
336 s->method->ssl3_enc->client_finished_label_len);
337 if (ret <= 0) goto end;
338 s->state=SSL3_ST_CW_FLUSH;
341 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
344 s->s3->tmp.next_state=SSL_ST_OK;
345 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
348 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
349 s->s3->delay_buf_pop_ret=0;
354 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
359 case SSL3_ST_CR_FINISHED_A:
360 case SSL3_ST_CR_FINISHED_B:
362 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
363 SSL3_ST_CR_FINISHED_B);
364 if (ret <= 0) goto end;
367 s->state=SSL3_ST_CW_CHANGE_A;
373 case SSL3_ST_CW_FLUSH:
374 /* number of bytes to be flushed */
375 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
378 s->rwstate=SSL_WRITING;
379 num1=BIO_flush(s->wbio);
380 if (num1 <= 0) { ret= -1; goto end; }
381 s->rwstate=SSL_NOTHING;
384 s->state=s->s3->tmp.next_state;
388 /* clean a few things up */
389 ssl3_cleanup_key_block(s);
391 if (s->init_buf != NULL)
393 BUF_MEM_free(s->init_buf);
397 /* If we are not 'joining' the last two packets,
398 * remove the buffering now */
399 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
400 ssl_free_wbio_buffer(s);
401 /* else do it later in ssl3_write */
406 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
407 if (s->hit) s->ctx->stats.sess_hit++;
411 s->handshake_func=ssl3_connect;
412 s->ctx->stats.sess_connect_good++;
414 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
420 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
426 /* did we do anything */
427 if (!s->s3->tmp.reuse_message && !skip)
431 if ((ret=BIO_flush(s->wbio)) <= 0)
435 if ((cb != NULL) && (s->state != state))
439 cb(s,SSL_CB_CONNECT_LOOP,1);
447 cb(s,SSL_CB_CONNECT_EXIT,ret);
453 static int ssl3_client_hello(SSL *s)
458 unsigned long Time,l;
461 buf=(unsigned char *)s->init_buf->data;
462 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
464 if ((s->session == NULL) ||
465 (s->session->ssl_version != s->version) ||
466 (s->session->not_resumable))
468 if (!ssl_get_new_session(s,0))
471 /* else use the pre-loaded session */
473 p=s->s3->client_random;
474 Time=time(NULL); /* Time */
476 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
478 /* Do the message type and length last */
481 *(p++)=s->version>>8;
482 *(p++)=s->version&0xff;
483 s->client_version=s->version;
486 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
493 i=s->session->session_id_length;
497 memcpy(p,s->session->session_id,i);
501 /* Ciphers supported */
502 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
505 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
512 if (s->ctx->comp_methods == NULL)
515 j=sk_SSL_COMP_num(s->ctx->comp_methods);
519 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
522 *(p++)=0; /* Add the NULL method */
526 *(d++)=SSL3_MT_CLIENT_HELLO;
529 s->state=SSL3_ST_CW_CLNT_HELLO_B;
530 /* number of bytes to write */
535 /* SSL3_ST_CW_CLNT_HELLO_B */
536 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
541 static int ssl3_get_server_hello(SSL *s)
543 STACK_OF(SSL_CIPHER) *sk;
551 n=ssl3_get_message(s,
552 SSL3_ST_CR_SRVR_HELLO_A,
553 SSL3_ST_CR_SRVR_HELLO_B,
554 SSL3_MT_SERVER_HELLO,
558 if (!ok) return((int)n);
559 d=p=(unsigned char *)s->init_buf->data;
561 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
563 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
564 s->version=(s->version&0xff00)|p[1];
565 al=SSL_AD_PROTOCOL_VERSION;
570 /* load the server hello data */
571 /* load the server random */
572 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
575 /* get the session-id */
578 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
580 /* SSLref returns 16 :-( */
581 if (j < SSL2_SSL_SESSION_ID_LENGTH)
583 al=SSL_AD_ILLEGAL_PARAMETER;
584 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
588 if (j != 0 && j == s->session->session_id_length
589 && memcmp(p,s->session->session_id,j) == 0)
591 if(s->sid_ctx_length != s->session->sid_ctx_length
592 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
594 al=SSL_AD_ILLEGAL_PARAMETER;
595 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
600 else /* a miss or crap from the other end */
602 /* If we were trying for session-id reuse, make a new
603 * SSL_SESSION so we don't stuff up other people */
605 if (s->session->session_id_length > 0)
607 if (!ssl_get_new_session(s,0))
609 al=SSL_AD_INTERNAL_ERROR;
613 s->session->session_id_length=j;
614 memcpy(s->session->session_id,p,j); /* j could be 0 */
617 c=ssl_get_cipher_by_char(s,p);
621 al=SSL_AD_ILLEGAL_PARAMETER;
622 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
625 p+=ssl_put_cipher_by_char(s,NULL,NULL);
627 sk=ssl_get_ciphers_by_id(s);
628 i=sk_SSL_CIPHER_find(sk,c);
631 /* we did not say we would use this cipher */
632 al=SSL_AD_ILLEGAL_PARAMETER;
633 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
637 if (s->hit && (s->session->cipher != c))
640 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
642 al=SSL_AD_ILLEGAL_PARAMETER;
643 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
647 s->s3->tmp.new_cipher=c;
649 /* lets get the compression algorithm */
655 comp=ssl3_comp_find(s->ctx->comp_methods,j);
657 if ((j != 0) && (comp == NULL))
659 al=SSL_AD_ILLEGAL_PARAMETER;
660 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
665 s->s3->tmp.new_compression=comp;
670 /* wrong packet length */
671 al=SSL_AD_DECODE_ERROR;
672 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
678 ssl3_send_alert(s,SSL3_AL_FATAL,al);
683 static int ssl3_get_server_certificate(SSL *s)
686 unsigned long n,nc,llen,l;
688 unsigned char *p,*d,*q;
689 STACK_OF(X509) *sk=NULL;
692 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
694 n=ssl3_get_message(s,
698 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
699 1024*30, /* 30k max cert list :-) */
701 1024*100, /* 100k max cert list :-) */
705 if (!ok) return((int)n);
707 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
709 s->s3->tmp.reuse_message=1;
713 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
715 al=SSL_AD_UNEXPECTED_MESSAGE;
716 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
719 d=p=(unsigned char *)s->init_buf->data;
721 if ((sk=sk_X509_new_null()) == NULL)
723 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
730 al=SSL_AD_DECODE_ERROR;
731 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
734 for (nc=0; nc<llen; )
739 al=SSL_AD_DECODE_ERROR;
740 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
745 x=d2i_X509(NULL,&q,l);
748 al=SSL_AD_BAD_CERTIFICATE;
749 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
754 al=SSL_AD_DECODE_ERROR;
755 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
758 if (!sk_X509_push(sk,x))
760 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
768 i=ssl_verify_cert_chain(s,sk);
769 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
771 al=ssl_verify_alarm_type(s->verify_result);
772 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
775 ERR_clear_error(); /* but we keep s->verify_result */
777 sc=ssl_sess_cert_new();
778 if (sc == NULL) goto err;
780 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
781 s->session->sess_cert=sc;
784 /* Inconsistency alert: cert_chain does include the peer's
785 * certificate, which we don't include in s3_srvr.c */
786 x=sk_X509_value(sk,0);
788 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end */
790 pkey=X509_get_pubkey(x);
792 /* VRS: allow null cert if auth == KRB5 */
794 ((s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
795 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
798 printf("pkey,x = %p, %p\n", pkey,x);
799 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
800 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
801 s->s3->tmp.new_cipher->algorithms, need_cert);
802 #endif /* KSSL_DEBUG */
804 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
808 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
812 i=ssl_cert_type(x,pkey);
813 if (need_cert && i < 0)
817 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
823 sc->peer_cert_type=i;
824 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
825 /* Why would the following ever happen?
826 * We just created sc a couple of lines ago. */
827 if (sc->peer_pkeys[i].x509 != NULL)
828 X509_free(sc->peer_pkeys[i].x509);
829 sc->peer_pkeys[i].x509=x;
830 sc->peer_key= &(sc->peer_pkeys[i]);
832 if (s->session->peer != NULL)
833 X509_free(s->session->peer);
834 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
839 sc->peer_cert_type=i;
842 if (s->session->peer != NULL)
843 X509_free(s->session->peer);
844 s->session->peer=NULL;
846 s->session->verify_result = s->verify_result;
854 ssl3_send_alert(s,SSL3_AL_FATAL,al);
859 sk_X509_pop_free(sk,X509_free);
863 static int ssl3_get_key_exchange(SSL *s)
865 #ifndef OPENSSL_NO_RSA
866 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
869 unsigned char *param,*p;
870 int al,i,j,param_len,ok;
873 #ifndef OPENSSL_NO_RSA
876 #ifndef OPENSSL_NO_DH
880 n=ssl3_get_message(s,
881 SSL3_ST_CR_KEY_EXCH_A,
882 SSL3_ST_CR_KEY_EXCH_B,
887 if (!ok) return((int)n);
889 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
891 s->s3->tmp.reuse_message=1;
895 param=p=(unsigned char *)s->init_buf->data;
897 if (s->session->sess_cert != NULL)
899 #ifndef OPENSSL_NO_RSA
900 if (s->session->sess_cert->peer_rsa_tmp != NULL)
902 RSA_free(s->session->sess_cert->peer_rsa_tmp);
903 s->session->sess_cert->peer_rsa_tmp=NULL;
906 #ifndef OPENSSL_NO_DH
907 if (s->session->sess_cert->peer_dh_tmp)
909 DH_free(s->session->sess_cert->peer_dh_tmp);
910 s->session->sess_cert->peer_dh_tmp=NULL;
916 s->session->sess_cert=ssl_sess_cert_new();
920 alg=s->s3->tmp.new_cipher->algorithms;
922 #ifndef OPENSSL_NO_RSA
925 if ((rsa=RSA_new()) == NULL)
927 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
934 al=SSL_AD_DECODE_ERROR;
935 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
938 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
940 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
949 al=SSL_AD_DECODE_ERROR;
950 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
953 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
955 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
961 /* this should be because we are using an export cipher */
963 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
966 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
969 s->session->sess_cert->peer_rsa_tmp=rsa;
972 #else /* OPENSSL_NO_RSA */
976 #ifndef OPENSSL_NO_DH
977 else if (alg & SSL_kEDH)
979 if ((dh=DH_new()) == NULL)
981 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
988 al=SSL_AD_DECODE_ERROR;
989 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
992 if (!(dh->p=BN_bin2bn(p,i,NULL)))
994 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1003 al=SSL_AD_DECODE_ERROR;
1004 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1007 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1009 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1018 al=SSL_AD_DECODE_ERROR;
1019 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1022 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1024 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1030 #ifndef OPENSSL_NO_RSA
1032 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1037 #ifndef OPENSSL_NO_DSA
1038 else if (alg & SSL_aDSS)
1039 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1041 /* else anonymous DH, so no certificate or pkey. */
1043 s->session->sess_cert->peer_dh_tmp=dh;
1046 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1048 al=SSL_AD_ILLEGAL_PARAMETER;
1049 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1052 #endif /* !OPENSSL_NO_DH */
1055 al=SSL_AD_HANDSHAKE_FAILURE;
1056 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1061 /* p points to the next byte, there are 'n' bytes left */
1064 /* if it was signed, check the signature */
1069 j=EVP_PKEY_size(pkey);
1071 if ((i != n) || (n > j) || (n <= 0))
1073 /* wrong packet length */
1074 al=SSL_AD_DECODE_ERROR;
1075 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1079 #ifndef OPENSSL_NO_RSA
1080 if (pkey->type == EVP_PKEY_RSA)
1086 for (num=2; num > 0; num--)
1088 EVP_DigestInit(&md_ctx,(num == 2)
1089 ?s->ctx->md5:s->ctx->sha1);
1090 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1091 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1092 EVP_DigestUpdate(&md_ctx,param,param_len);
1093 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1097 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1101 al=SSL_AD_DECRYPT_ERROR;
1102 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1108 al=SSL_AD_DECRYPT_ERROR;
1109 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1115 #ifndef OPENSSL_NO_DSA
1116 if (pkey->type == EVP_PKEY_DSA)
1119 EVP_VerifyInit(&md_ctx,EVP_dss1());
1120 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1121 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1122 EVP_VerifyUpdate(&md_ctx,param,param_len);
1123 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1126 al=SSL_AD_DECRYPT_ERROR;
1127 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1134 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1140 /* still data left over */
1141 if (!(alg & SSL_aNULL))
1143 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1148 al=SSL_AD_DECODE_ERROR;
1149 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1153 EVP_PKEY_free(pkey);
1156 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1158 EVP_PKEY_free(pkey);
1159 #ifndef OPENSSL_NO_RSA
1163 #ifndef OPENSSL_NO_DH
1170 static int ssl3_get_certificate_request(SSL *s)
1173 unsigned long n,nc,l;
1174 unsigned int llen,ctype_num,i;
1176 unsigned char *p,*d,*q;
1177 STACK_OF(X509_NAME) *ca_sk=NULL;
1179 n=ssl3_get_message(s,
1180 SSL3_ST_CR_CERT_REQ_A,
1181 SSL3_ST_CR_CERT_REQ_B,
1183 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
1184 1024*30, /* 30k max cert list :-) */
1186 1024*100, /* 100k max cert list :-) */
1190 if (!ok) return((int)n);
1192 s->s3->tmp.cert_req=0;
1194 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1196 s->s3->tmp.reuse_message=1;
1200 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1202 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1203 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1207 /* TLS does not like anon-DH with client cert */
1208 if (s->version > SSL3_VERSION)
1210 l=s->s3->tmp.new_cipher->algorithms;
1213 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1214 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1219 d=p=(unsigned char *)s->init_buf->data;
1221 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1223 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1227 /* get the certificate types */
1229 if (ctype_num > SSL3_CT_NUMBER)
1230 ctype_num=SSL3_CT_NUMBER;
1231 for (i=0; i<ctype_num; i++)
1232 s->s3->tmp.ctype[i]= p[i];
1235 /* get the CA RDNs */
1240 out=fopen("/tmp/vsign.der","w");
1241 fwrite(p,1,llen,out);
1246 if ((llen+ctype_num+2+1) != n)
1248 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1249 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1253 for (nc=0; nc<llen; )
1256 if ((l+nc+2) > llen)
1258 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1259 goto cont; /* netscape bugs */
1260 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1261 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1267 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1269 /* If netscape tolerance is on, ignore errors */
1270 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1274 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1275 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1282 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1283 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1286 if (!sk_X509_NAME_push(ca_sk,xn))
1288 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1302 /* we should setup a certificate to return.... */
1303 s->s3->tmp.cert_req=1;
1304 s->s3->tmp.ctype_num=ctype_num;
1305 if (s->s3->tmp.ca_names != NULL)
1306 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1307 s->s3->tmp.ca_names=ca_sk;
1312 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1316 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1318 return(X509_NAME_cmp(*a,*b));
1321 static int ssl3_get_server_done(SSL *s)
1326 n=ssl3_get_message(s,
1327 SSL3_ST_CR_SRVR_DONE_A,
1328 SSL3_ST_CR_SRVR_DONE_B,
1329 SSL3_MT_SERVER_DONE,
1330 30, /* should be very small, like 0 :-) */
1333 if (!ok) return((int)n);
1336 /* should contain no data */
1337 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1338 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1344 static int ssl3_send_client_key_exchange(SSL *s)
1346 unsigned char *p,*d;
1349 #ifndef OPENSSL_NO_RSA
1351 EVP_PKEY *pkey=NULL;
1353 #ifndef OPENSSL_NO_KRB5
1355 #endif /* OPENSSL_NO_KRB5 */
1357 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1359 d=(unsigned char *)s->init_buf->data;
1362 l=s->s3->tmp.new_cipher->algorithms;
1364 /* Fool emacs indentation */
1366 #ifndef OPENSSL_NO_RSA
1367 else if (l & SSL_kRSA)
1370 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1372 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1373 rsa=s->session->sess_cert->peer_rsa_tmp;
1376 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1377 if ((pkey == NULL) ||
1378 (pkey->type != EVP_PKEY_RSA) ||
1379 (pkey->pkey.rsa == NULL))
1381 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1385 EVP_PKEY_free(pkey);
1388 tmp_buf[0]=s->client_version>>8;
1389 tmp_buf[1]=s->client_version&0xff;
1390 if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
1393 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1396 /* Fix buf for TLS and beyond */
1397 if (s->version > SSL3_VERSION)
1399 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1400 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1402 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1403 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1407 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1411 /* Fix buf for TLS and beyond */
1412 if (s->version > SSL3_VERSION)
1418 s->session->master_key_length=
1419 s->method->ssl3_enc->generate_master_secret(s,
1420 s->session->master_key,
1421 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1422 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1425 #ifndef OPENSSL_NO_KRB5
1426 else if (l & SSL_kKRB5)
1428 krb5_error_code krb5rc;
1429 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1430 krb5_data krb5_ap_req;
1433 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1435 #endif /* KSSL_DEBUG */
1438 ** Tried to send random tmp_buf[] as PMS in Kerberos ticket
1439 ** by passing krb5_mk_req_extended(ctx,authctx,opts, tmp_buf, ...)
1440 ** but: I can't retrieve the PMS on the other side! There is
1441 ** some indication in the krb5 source that this is only used
1442 ** to generate a checksum. OTOH, the Tung book shows data
1443 ** ("GET widget01.txt") being passed in krb5_mk_req_extended()
1444 ** by way of krb5_sendauth(). I don't get it.
1445 ** Until Kerberos goes 3DES, the big PMS secret would only be
1446 ** encrypted in 1-DES anyway. So losing the PMS shouldn't be
1449 krb5rc = kssl_cget_tkt(kssl_ctx, &krb5_ap_req,
1453 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1454 kssl_ctx_show(kssl_ctx);
1455 if (krb5rc && kssl_err.text)
1456 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1458 #endif /* KSSL_DEBUG */
1462 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1463 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
1467 /* Send ticket (copy to *p, set n = length)
1469 n = krb5_ap_req.length;
1470 memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1471 if (krb5_ap_req.data)
1472 kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1474 /* 19991013 VRS - 3DES is kind of bogus here,
1475 ** at least until Kerberos supports 3DES. The only
1476 ** real secret is the 8-byte Kerberos session key;
1477 ** the other key material ((s->) client_random, server_random)
1478 ** could be sniffed. Mixing in these nonces should help
1479 ** protect against replay attacks, however.
1481 ** Alternate code for Kerberos Purists:
1483 ** memcpy(s->session->master_key, kssl_ctx->key, kssl_ctx->length);
1484 ** s->session->master_key_length = kssl_ctx->length;
1486 s->session->master_key_length=
1487 s->method->ssl3_enc->generate_master_secret(s,
1488 s->session->master_key, kssl_ctx->key,kssl_ctx->length);
1491 #ifndef OPENSSL_NO_DH
1492 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1494 DH *dh_srvr,*dh_clnt;
1496 if (s->session->sess_cert->peer_dh_tmp != NULL)
1497 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1500 /* we get them from the cert */
1501 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1502 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1506 /* generate a new random key */
1507 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1509 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1512 if (!DH_generate_key(dh_clnt))
1514 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1518 /* use the 'p' output buffer for the DH key, but
1519 * make sure to clear it out afterwards */
1521 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1525 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1529 /* generate master key from the result */
1530 s->session->master_key_length=
1531 s->method->ssl3_enc->generate_master_secret(s,
1532 s->session->master_key,p,n);
1536 /* send off the data */
1537 n=BN_num_bytes(dh_clnt->pub_key);
1539 BN_bn2bin(dh_clnt->pub_key,p);
1544 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1549 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1550 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1554 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1557 s->state=SSL3_ST_CW_KEY_EXCH_B;
1558 /* number of bytes to write */
1563 /* SSL3_ST_CW_KEY_EXCH_B */
1564 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1569 static int ssl3_send_client_verify(SSL *s)
1571 unsigned char *p,*d;
1572 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1574 #ifndef OPENSSL_NO_RSA
1578 #ifndef OPENSSL_NO_DSA
1582 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1584 d=(unsigned char *)s->init_buf->data;
1586 pkey=s->cert->key->privatekey;
1588 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1589 &(data[MD5_DIGEST_LENGTH]));
1591 #ifndef OPENSSL_NO_RSA
1592 if (pkey->type == EVP_PKEY_RSA)
1594 s->method->ssl3_enc->cert_verify_mac(s,
1595 &(s->s3->finish_dgst1),&(data[0]));
1596 if (RSA_sign(NID_md5_sha1, data,
1597 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1598 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1600 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1608 #ifndef OPENSSL_NO_DSA
1609 if (pkey->type == EVP_PKEY_DSA)
1611 if (!DSA_sign(pkey->save_type,
1612 &(data[MD5_DIGEST_LENGTH]),
1613 SHA_DIGEST_LENGTH,&(p[2]),
1614 (unsigned int *)&j,pkey->pkey.dsa))
1616 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1625 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1628 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1631 s->init_num=(int)n+4;
1634 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1639 static int ssl3_send_client_certificate(SSL *s)
1642 EVP_PKEY *pkey=NULL;
1646 if (s->state == SSL3_ST_CW_CERT_A)
1648 if ((s->cert == NULL) ||
1649 (s->cert->key->x509 == NULL) ||
1650 (s->cert->key->privatekey == NULL))
1651 s->state=SSL3_ST_CW_CERT_B;
1653 s->state=SSL3_ST_CW_CERT_C;
1656 /* We need to get a client cert */
1657 if (s->state == SSL3_ST_CW_CERT_B)
1659 /* If we get an error, we need to
1660 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1661 * We then get retied later */
1663 if (s->ctx->client_cert_cb != NULL)
1664 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1667 s->rwstate=SSL_X509_LOOKUP;
1670 s->rwstate=SSL_NOTHING;
1671 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1673 s->state=SSL3_ST_CW_CERT_B;
1674 if ( !SSL_use_certificate(s,x509) ||
1675 !SSL_use_PrivateKey(s,pkey))
1681 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1684 if (x509 != NULL) X509_free(x509);
1685 if (pkey != NULL) EVP_PKEY_free(pkey);
1688 if (s->version == SSL3_VERSION)
1690 s->s3->tmp.cert_req=0;
1691 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1696 s->s3->tmp.cert_req=2;
1700 /* Ok, we have a cert */
1701 s->state=SSL3_ST_CW_CERT_C;
1704 if (s->state == SSL3_ST_CW_CERT_C)
1706 s->state=SSL3_ST_CW_CERT_D;
1707 l=ssl3_output_cert_chain(s,
1708 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1712 /* SSL3_ST_CW_CERT_D */
1713 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1716 #define has_bits(i,m) (((i)&(m)) == (m))
1718 static int ssl3_check_cert_and_algorithm(SSL *s)
1722 EVP_PKEY *pkey=NULL;
1724 #ifndef OPENSSL_NO_RSA
1727 #ifndef OPENSSL_NO_DH
1731 sc=s->session->sess_cert;
1735 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
1739 algs=s->s3->tmp.new_cipher->algorithms;
1741 /* we don't have a certificate */
1742 if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
1745 #ifndef OPENSSL_NO_RSA
1746 rsa=s->session->sess_cert->peer_rsa_tmp;
1748 #ifndef OPENSSL_NO_DH
1749 dh=s->session->sess_cert->peer_dh_tmp;
1752 /* This is the passed certificate */
1754 idx=sc->peer_cert_type;
1755 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1756 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1757 EVP_PKEY_free(pkey);
1760 /* Check that we have a certificate if we require one */
1761 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1763 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1766 #ifndef OPENSSL_NO_DSA
1767 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1769 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1773 #ifndef OPENSSL_NO_RSA
1774 if ((algs & SSL_kRSA) &&
1775 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1777 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1781 #ifndef OPENSSL_NO_DH
1782 if ((algs & SSL_kEDH) &&
1783 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1785 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1788 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1790 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1793 #ifndef OPENSSL_NO_DSA
1794 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1796 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1802 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
1804 #ifndef OPENSSL_NO_RSA
1805 if (algs & SSL_kRSA)
1808 || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1810 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1816 #ifndef OPENSSL_NO_DH
1817 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1820 || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1822 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1829 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1835 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);