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/md5.h>
64 #include <openssl/sha.h>
65 #include <openssl/evp.h>
68 static SSL_METHOD *ssl3_get_client_method(int ver);
69 static int ssl3_client_hello(SSL *s);
70 static int ssl3_get_server_hello(SSL *s);
71 static int ssl3_get_certificate_request(SSL *s);
72 static int ca_dn_cmp(X509_NAME **a,X509_NAME **b);
73 static int ssl3_get_server_done(SSL *s);
74 static int ssl3_send_client_verify(SSL *s);
75 static int ssl3_send_client_certificate(SSL *s);
76 static int ssl3_send_client_key_exchange(SSL *s);
77 static int ssl3_get_key_exchange(SSL *s);
78 static int ssl3_get_server_certificate(SSL *s);
79 static int ssl3_check_cert_and_algorithm(SSL *s);
80 static SSL_METHOD *ssl3_get_client_method(int ver)
82 if (ver == SSL3_VERSION)
83 return(SSLv3_client_method());
88 SSL_METHOD *SSLv3_client_method(void)
91 static SSL_METHOD SSLv3_client_data;
96 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
98 SSLv3_client_data.ssl_connect=ssl3_connect;
99 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
101 return(&SSLv3_client_data);
104 int ssl3_connect(SSL *s)
107 unsigned long Time=time(NULL),l;
111 int new_state,state,skip=0;;
113 RAND_seed(&Time,sizeof(Time));
117 if (s->info_callback != NULL)
119 else if (s->ctx->info_callback != NULL)
120 cb=s->ctx->info_callback;
122 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
131 case SSL_ST_RENEGOTIATE:
133 s->state=SSL_ST_CONNECT;
134 s->ctx->stats.sess_connect_renegotiate++;
138 case SSL_ST_BEFORE|SSL_ST_CONNECT:
139 case SSL_ST_OK|SSL_ST_CONNECT:
142 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
144 if ((s->version & 0xff00 ) != 0x0300)
146 /* s->version=SSL3_VERSION; */
147 s->type=SSL_ST_CONNECT;
149 if (s->init_buf == NULL)
151 if ((buf=BUF_MEM_new()) == NULL)
156 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
164 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
166 /* setup buffing BIO */
167 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
169 /* don't push the buffering BIO quite yet */
171 ssl3_init_finished_mac(s);
173 s->state=SSL3_ST_CW_CLNT_HELLO_A;
174 s->ctx->stats.sess_connect++;
178 case SSL3_ST_CW_CLNT_HELLO_A:
179 case SSL3_ST_CW_CLNT_HELLO_B:
182 ret=ssl3_client_hello(s);
183 if (ret <= 0) goto end;
184 s->state=SSL3_ST_CR_SRVR_HELLO_A;
187 /* turn on buffering for the next lot of output */
188 if (s->bbio != s->wbio)
189 s->wbio=BIO_push(s->bbio,s->wbio);
193 case SSL3_ST_CR_SRVR_HELLO_A:
194 case SSL3_ST_CR_SRVR_HELLO_B:
195 ret=ssl3_get_server_hello(s);
196 if (ret <= 0) goto end;
198 s->state=SSL3_ST_CR_FINISHED_A;
200 s->state=SSL3_ST_CR_CERT_A;
204 case SSL3_ST_CR_CERT_A:
205 case SSL3_ST_CR_CERT_B:
206 /* Check if it is anon DH */
207 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
209 ret=ssl3_get_server_certificate(s);
210 if (ret <= 0) goto end;
214 s->state=SSL3_ST_CR_KEY_EXCH_A;
218 case SSL3_ST_CR_KEY_EXCH_A:
219 case SSL3_ST_CR_KEY_EXCH_B:
220 ret=ssl3_get_key_exchange(s);
221 if (ret <= 0) goto end;
222 s->state=SSL3_ST_CR_CERT_REQ_A;
225 /* at this point we check that we have the
226 * required stuff from the server */
227 if (!ssl3_check_cert_and_algorithm(s))
234 case SSL3_ST_CR_CERT_REQ_A:
235 case SSL3_ST_CR_CERT_REQ_B:
236 ret=ssl3_get_certificate_request(s);
237 if (ret <= 0) goto end;
238 s->state=SSL3_ST_CR_SRVR_DONE_A;
242 case SSL3_ST_CR_SRVR_DONE_A:
243 case SSL3_ST_CR_SRVR_DONE_B:
244 ret=ssl3_get_server_done(s);
245 if (ret <= 0) goto end;
246 if (s->s3->tmp.cert_req)
247 s->state=SSL3_ST_CW_CERT_A;
249 s->state=SSL3_ST_CW_KEY_EXCH_A;
254 case SSL3_ST_CW_CERT_A:
255 case SSL3_ST_CW_CERT_B:
256 case SSL3_ST_CW_CERT_C:
257 ret=ssl3_send_client_certificate(s);
258 if (ret <= 0) goto end;
259 s->state=SSL3_ST_CW_KEY_EXCH_A;
263 case SSL3_ST_CW_KEY_EXCH_A:
264 case SSL3_ST_CW_KEY_EXCH_B:
265 ret=ssl3_send_client_key_exchange(s);
266 if (ret <= 0) goto end;
267 l=s->s3->tmp.new_cipher->algorithms;
268 /* EAY EAY EAY need to check for DH fix cert
270 /* For TLS, cert_req is set to 2, so a cert chain
271 * of nothing is sent, but no verify packet is sent */
272 if (s->s3->tmp.cert_req == 1)
274 s->state=SSL3_ST_CW_CERT_VRFY_A;
278 s->state=SSL3_ST_CW_CHANGE_A;
279 s->s3->change_cipher_spec=0;
285 case SSL3_ST_CW_CERT_VRFY_A:
286 case SSL3_ST_CW_CERT_VRFY_B:
287 ret=ssl3_send_client_verify(s);
288 if (ret <= 0) goto end;
289 s->state=SSL3_ST_CW_CHANGE_A;
291 s->s3->change_cipher_spec=0;
294 case SSL3_ST_CW_CHANGE_A:
295 case SSL3_ST_CW_CHANGE_B:
296 ret=ssl3_send_change_cipher_spec(s,
297 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
298 if (ret <= 0) goto end;
299 s->state=SSL3_ST_CW_FINISHED_A;
302 s->session->cipher=s->s3->tmp.new_cipher;
303 if (s->s3->tmp.new_compression == NULL)
304 s->session->compress_meth=0;
306 s->session->compress_meth=
307 s->s3->tmp.new_compression->id;
308 if (!s->method->ssl3_enc->setup_key_block(s))
314 if (!s->method->ssl3_enc->change_cipher_state(s,
315 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
323 case SSL3_ST_CW_FINISHED_A:
324 case SSL3_ST_CW_FINISHED_B:
325 ret=ssl3_send_finished(s,
326 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
327 s->method->ssl3_enc->client_finished,
328 s->method->ssl3_enc->client_finished_len);
329 if (ret <= 0) goto end;
330 s->state=SSL3_ST_CW_FLUSH;
333 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
336 s->s3->tmp.next_state=SSL_ST_OK;
337 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
340 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
341 s->s3->delay_buf_pop_ret=0;
346 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
351 case SSL3_ST_CR_FINISHED_A:
352 case SSL3_ST_CR_FINISHED_B:
354 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
355 SSL3_ST_CR_FINISHED_B);
356 if (ret <= 0) goto end;
359 s->state=SSL3_ST_CW_CHANGE_A;
365 case SSL3_ST_CW_FLUSH:
366 /* number of bytes to be flushed */
367 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
370 s->rwstate=SSL_WRITING;
371 num1=BIO_flush(s->wbio);
372 if (num1 <= 0) { ret= -1; goto end; }
373 s->rwstate=SSL_NOTHING;
376 s->state=s->s3->tmp.next_state;
380 /* clean a few things up */
381 ssl3_cleanup_key_block(s);
383 if (s->init_buf != NULL)
385 BUF_MEM_free(s->init_buf);
389 /* If we are not 'joining' the last two packets,
390 * remove the buffering now */
391 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
392 ssl_free_wbio_buffer(s);
393 /* else do it later in ssl3_write */
398 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
399 if (s->hit) s->ctx->stats.sess_hit++;
403 s->handshake_func=ssl3_connect;
404 s->ctx->stats.sess_connect_good++;
406 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
412 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
418 /* did we do anything */
419 if (!s->s3->tmp.reuse_message && !skip)
423 if ((ret=BIO_flush(s->wbio)) <= 0)
427 if ((cb != NULL) && (s->state != state))
431 cb(s,SSL_CB_CONNECT_LOOP,1);
439 cb(s,SSL_CB_CONNECT_EXIT,ret);
445 static int ssl3_client_hello(SSL *s)
450 unsigned long Time,l;
453 buf=(unsigned char *)s->init_buf->data;
454 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
456 if ((s->session == NULL) ||
457 (s->session->ssl_version != s->version) ||
458 (s->session->not_resumable))
460 if (!ssl_get_new_session(s,0))
463 /* else use the pre-loaded session */
465 p=s->s3->client_random;
466 Time=time(NULL); /* Time */
468 RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
470 /* Do the message type and length last */
473 *(p++)=s->version>>8;
474 *(p++)=s->version&0xff;
475 s->client_version=s->version;
478 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
485 i=s->session->session_id_length;
489 memcpy(p,s->session->session_id,i);
493 /* Ciphers supported */
494 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
497 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
504 if (s->ctx->comp_methods == NULL)
507 j=sk_SSL_COMP_num(s->ctx->comp_methods);
511 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
514 *(p++)=0; /* Add the NULL method */
518 *(d++)=SSL3_MT_CLIENT_HELLO;
521 s->state=SSL3_ST_CW_CLNT_HELLO_B;
522 /* number of bytes to write */
527 /* SSL3_ST_CW_CLNT_HELLO_B */
528 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
533 static int ssl3_get_server_hello(SSL *s)
535 STACK_OF(SSL_CIPHER) *sk;
543 n=ssl3_get_message(s,
544 SSL3_ST_CR_SRVR_HELLO_A,
545 SSL3_ST_CR_SRVR_HELLO_B,
546 SSL3_MT_SERVER_HELLO,
550 if (!ok) return((int)n);
551 d=p=(unsigned char *)s->init_buf->data;
553 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
555 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
556 s->version=(s->version&0xff00)|p[1];
557 al=SSL_AD_PROTOCOL_VERSION;
562 /* load the server hello data */
563 /* load the server random */
564 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
567 /* get the session-id */
570 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
572 /* SSLref returns 16 :-( */
573 if (j < SSL2_SSL_SESSION_ID_LENGTH)
575 al=SSL_AD_ILLEGAL_PARAMETER;
576 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
580 if (j != 0 && j == s->session->session_id_length
581 && memcmp(p,s->session->session_id,j) == 0)
583 if(s->sid_ctx_length != s->session->sid_ctx_length
584 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
586 al=SSL_AD_ILLEGAL_PARAMETER;
587 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
592 else /* a miss or crap from the other end */
594 /* If we were trying for session-id reuse, make a new
595 * SSL_SESSION so we don't stuff up other people */
597 if (s->session->session_id_length > 0)
599 if (!ssl_get_new_session(s,0))
601 al=SSL_AD_INTERNAL_ERROR;
605 s->session->session_id_length=j;
606 memcpy(s->session->session_id,p,j); /* j could be 0 */
609 c=ssl_get_cipher_by_char(s,p);
613 al=SSL_AD_ILLEGAL_PARAMETER;
614 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
617 p+=ssl_put_cipher_by_char(s,NULL,NULL);
619 sk=ssl_get_ciphers_by_id(s);
620 i=sk_SSL_CIPHER_find(sk,c);
623 /* we did not say we would use this cipher */
624 al=SSL_AD_ILLEGAL_PARAMETER;
625 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
629 if (s->hit && (s->session->cipher != c))
632 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
634 al=SSL_AD_ILLEGAL_PARAMETER;
635 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
639 s->s3->tmp.new_cipher=c;
641 /* lets get the compression algorithm */
647 comp=ssl3_comp_find(s->ctx->comp_methods,j);
649 if ((j != 0) && (comp == NULL))
651 al=SSL_AD_ILLEGAL_PARAMETER;
652 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
657 s->s3->tmp.new_compression=comp;
662 /* wrong packet length */
663 al=SSL_AD_DECODE_ERROR;
664 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
670 ssl3_send_alert(s,SSL3_AL_FATAL,al);
675 static int ssl3_get_server_certificate(SSL *s)
678 unsigned long n,nc,llen,l;
680 unsigned char *p,*d,*q;
681 STACK_OF(X509) *sk=NULL;
685 n=ssl3_get_message(s,
689 #if defined(MSDOS) && !defined(WIN32)
690 1024*30, /* 30k max cert list :-) */
692 1024*100, /* 100k max cert list :-) */
696 if (!ok) return((int)n);
698 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
700 s->s3->tmp.reuse_message=1;
704 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
706 al=SSL_AD_UNEXPECTED_MESSAGE;
707 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
710 d=p=(unsigned char *)s->init_buf->data;
712 if ((sk=sk_X509_new_null()) == NULL)
714 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
721 al=SSL_AD_DECODE_ERROR;
722 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
725 for (nc=0; nc<llen; )
730 al=SSL_AD_DECODE_ERROR;
731 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
736 x=d2i_X509(NULL,&q,l);
739 al=SSL_AD_BAD_CERTIFICATE;
740 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
745 al=SSL_AD_DECODE_ERROR;
746 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
749 if (!sk_X509_push(sk,x))
751 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
759 i=ssl_verify_cert_chain(s,sk);
760 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
762 al=ssl_verify_alarm_type(s->verify_result);
763 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
767 sc=ssl_sess_cert_new();
768 if (sc == NULL) goto err;
770 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
771 s->session->sess_cert=sc;
774 x=sk_X509_value(sk,0);
777 pkey=X509_get_pubkey(x);
779 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
783 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
787 i=ssl_cert_type(x,pkey);
792 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
796 sc->peer_cert_type=i;
797 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
798 if (sc->peer_pkeys[i].x509 != NULL) /* Why would this ever happen?
799 * We just created sc a couple of
801 X509_free(sc->peer_pkeys[i].x509);
802 sc->peer_pkeys[i].x509=x;
803 sc->peer_key= &(sc->peer_pkeys[i]);
805 if (s->session->peer != NULL)
806 X509_free(s->session->peer);
807 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
816 ssl3_send_alert(s,SSL3_AL_FATAL,al);
821 sk_X509_pop_free(sk,X509_free);
825 static int ssl3_get_key_exchange(SSL *s)
828 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
831 unsigned char *param,*p;
832 int al,i,j,param_len,ok;
842 n=ssl3_get_message(s,
843 SSL3_ST_CR_KEY_EXCH_A,
844 SSL3_ST_CR_KEY_EXCH_B,
849 if (!ok) return((int)n);
851 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
853 s->s3->tmp.reuse_message=1;
857 param=p=(unsigned char *)s->init_buf->data;
859 if (s->session->sess_cert != NULL)
862 if (s->session->sess_cert->peer_rsa_tmp != NULL)
864 RSA_free(s->session->sess_cert->peer_rsa_tmp);
865 s->session->sess_cert->peer_rsa_tmp=NULL;
869 if (s->session->sess_cert->peer_dh_tmp)
871 DH_free(s->session->sess_cert->peer_dh_tmp);
872 s->session->sess_cert->peer_dh_tmp=NULL;
878 s->session->sess_cert=ssl_sess_cert_new();
882 alg=s->s3->tmp.new_cipher->algorithms;
887 if ((rsa=RSA_new()) == NULL)
889 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
896 al=SSL_AD_DECODE_ERROR;
897 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
900 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
902 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
911 al=SSL_AD_DECODE_ERROR;
912 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
915 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
917 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
923 /* this should be because we are using an export cipher */
925 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
928 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
931 s->session->sess_cert->peer_rsa_tmp=rsa;
938 if ((dh=DH_new()) == NULL)
940 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
947 al=SSL_AD_DECODE_ERROR;
948 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
951 if (!(dh->p=BN_bin2bn(p,i,NULL)))
953 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
962 al=SSL_AD_DECODE_ERROR;
963 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
966 if (!(dh->g=BN_bin2bn(p,i,NULL)))
968 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
977 al=SSL_AD_DECODE_ERROR;
978 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
981 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
983 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
991 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
996 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
998 /* else anonymous DH, so no certificate or pkey. */
1000 s->session->sess_cert->peer_dh_tmp=dh;
1003 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1005 al=SSL_AD_ILLEGAL_PARAMETER;
1006 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1012 al=SSL_AD_HANDSHAKE_FAILURE;
1013 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1018 /* p points to the next byte, there are 'n' bytes left */
1021 /* if it was signed, check the signature */
1026 j=EVP_PKEY_size(pkey);
1028 if ((i != n) || (n > j) || (n <= 0))
1030 /* wrong packet length */
1031 al=SSL_AD_DECODE_ERROR;
1032 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1037 if (pkey->type == EVP_PKEY_RSA)
1043 for (num=2; num > 0; num--)
1045 EVP_DigestInit(&md_ctx,(num == 2)
1046 ?s->ctx->md5:s->ctx->sha1);
1047 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1048 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1049 EVP_DigestUpdate(&md_ctx,param,param_len);
1050 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1054 i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,
1058 al=SSL_AD_DECRYPT_ERROR;
1059 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1062 if ((j != i) || (memcmp(p,md_buf,i) != 0))
1065 al=SSL_AD_DECRYPT_ERROR;
1066 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1073 if (pkey->type == EVP_PKEY_DSA)
1076 EVP_VerifyInit(&md_ctx,EVP_dss1());
1077 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1078 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1079 EVP_VerifyUpdate(&md_ctx,param,param_len);
1080 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1083 al=SSL_AD_DECRYPT_ERROR;
1084 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1091 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1097 /* still data left over */
1098 if (!(alg & SSL_aNULL))
1100 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1105 al=SSL_AD_DECODE_ERROR;
1106 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1110 EVP_PKEY_free(pkey);
1113 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1115 EVP_PKEY_free(pkey);
1119 static int ssl3_get_certificate_request(SSL *s)
1122 unsigned long n,nc,l;
1123 unsigned int llen,ctype_num,i;
1125 unsigned char *p,*d,*q;
1126 STACK_OF(X509_NAME) *ca_sk=NULL;
1128 n=ssl3_get_message(s,
1129 SSL3_ST_CR_CERT_REQ_A,
1130 SSL3_ST_CR_CERT_REQ_B,
1132 #if defined(MSDOS) && !defined(WIN32)
1133 1024*30, /* 30k max cert list :-) */
1135 1024*100, /* 100k max cert list :-) */
1139 if (!ok) return((int)n);
1141 s->s3->tmp.cert_req=0;
1143 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1145 s->s3->tmp.reuse_message=1;
1149 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1151 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1152 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1156 /* TLS does not like anon-DH with client cert */
1157 if (s->version > SSL3_VERSION)
1159 l=s->s3->tmp.new_cipher->algorithms;
1162 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1163 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1168 d=p=(unsigned char *)s->init_buf->data;
1170 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1172 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1176 /* get the certificate types */
1178 if (ctype_num > SSL3_CT_NUMBER)
1179 ctype_num=SSL3_CT_NUMBER;
1180 for (i=0; i<ctype_num; i++)
1181 s->s3->tmp.ctype[i]= p[i];
1184 /* get the CA RDNs */
1189 out=fopen("/tmp/vsign.der","w");
1190 fwrite(p,1,llen,out);
1195 if ((llen+ctype_num+2+1) != n)
1197 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1198 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1202 for (nc=0; nc<llen; )
1205 if ((l+nc+2) > llen)
1207 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1208 goto cont; /* netscape bugs */
1209 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1210 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1216 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1218 /* If netscape tollerance is on, ignore errors */
1219 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1223 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1224 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1231 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1232 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1235 if (!sk_X509_NAME_push(ca_sk,xn))
1237 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1251 /* we should setup a certficate to return.... */
1252 s->s3->tmp.cert_req=1;
1253 s->s3->tmp.ctype_num=ctype_num;
1254 if (s->s3->tmp.ca_names != NULL)
1255 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1256 s->s3->tmp.ca_names=ca_sk;
1261 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1265 static int ca_dn_cmp(X509_NAME **a, X509_NAME **b)
1267 return(X509_NAME_cmp(*a,*b));
1270 static int ssl3_get_server_done(SSL *s)
1275 n=ssl3_get_message(s,
1276 SSL3_ST_CR_SRVR_DONE_A,
1277 SSL3_ST_CR_SRVR_DONE_B,
1278 SSL3_MT_SERVER_DONE,
1279 30, /* should be very small, like 0 :-) */
1282 if (!ok) return((int)n);
1285 /* should contain no data */
1286 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1287 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1293 static int ssl3_send_client_key_exchange(SSL *s)
1295 unsigned char *p,*d;
1300 EVP_PKEY *pkey=NULL;
1303 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1305 d=(unsigned char *)s->init_buf->data;
1308 l=s->s3->tmp.new_cipher->algorithms;
1314 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1316 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1317 rsa=s->session->sess_cert->peer_rsa_tmp;
1320 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1321 if ((pkey == NULL) ||
1322 (pkey->type != EVP_PKEY_RSA) ||
1323 (pkey->pkey.rsa == NULL))
1325 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1331 tmp_buf[0]=s->client_version>>8;
1332 tmp_buf[1]=s->client_version&0xff;
1333 RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1335 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1338 /* Fix buf for TLS and beyond */
1339 if (s->version > SSL3_VERSION)
1341 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1342 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1344 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1345 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1349 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1353 /* Fix buf for TLS and beyond */
1354 if (s->version > SSL3_VERSION)
1360 s->session->master_key_length=
1361 s->method->ssl3_enc->generate_master_secret(s,
1362 s->session->master_key,
1363 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1364 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1369 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1371 DH *dh_srvr,*dh_clnt;
1373 if (s->session->sess_cert->peer_dh_tmp != NULL)
1374 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1377 /* we get them from the cert */
1378 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1379 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1383 /* generate a new random key */
1384 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1386 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1389 if (!DH_generate_key(dh_clnt))
1391 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1395 /* use the 'p' output buffer for the DH key, but
1396 * make sure to clear it out afterwards */
1398 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1402 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1406 /* generate master key from the result */
1407 s->session->master_key_length=
1408 s->method->ssl3_enc->generate_master_secret(s,
1409 s->session->master_key,p,n);
1413 /* send off the data */
1414 n=BN_num_bytes(dh_clnt->pub_key);
1416 BN_bn2bin(dh_clnt->pub_key,p);
1421 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1426 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1427 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1431 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1434 s->state=SSL3_ST_CW_KEY_EXCH_B;
1435 /* number of bytes to write */
1440 /* SSL3_ST_CW_KEY_EXCH_B */
1441 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1446 static int ssl3_send_client_verify(SSL *s)
1448 unsigned char *p,*d;
1449 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1459 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1461 d=(unsigned char *)s->init_buf->data;
1463 pkey=s->cert->key->privatekey;
1465 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1466 &(data[MD5_DIGEST_LENGTH]));
1469 if (pkey->type == EVP_PKEY_RSA)
1471 s->method->ssl3_enc->cert_verify_mac(s,
1472 &(s->s3->finish_dgst1),&(data[0]));
1473 i=RSA_private_encrypt(
1474 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1475 data,&(p[2]),pkey->pkey.rsa,
1479 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1488 if (pkey->type == EVP_PKEY_DSA)
1490 if (!DSA_sign(pkey->save_type,
1491 &(data[MD5_DIGEST_LENGTH]),
1492 SHA_DIGEST_LENGTH,&(p[2]),
1493 (unsigned int *)&j,pkey->pkey.dsa))
1495 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1504 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
1507 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1510 s->init_num=(int)n+4;
1513 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1518 static int ssl3_send_client_certificate(SSL *s)
1521 EVP_PKEY *pkey=NULL;
1525 if (s->state == SSL3_ST_CW_CERT_A)
1527 if ((s->cert == NULL) ||
1528 (s->cert->key->x509 == NULL) ||
1529 (s->cert->key->privatekey == NULL))
1530 s->state=SSL3_ST_CW_CERT_B;
1532 s->state=SSL3_ST_CW_CERT_C;
1535 /* We need to get a client cert */
1536 if (s->state == SSL3_ST_CW_CERT_B)
1538 /* If we get an error, we need to
1539 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1540 * We then get retied later */
1542 if (s->ctx->client_cert_cb != NULL)
1543 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1546 s->rwstate=SSL_X509_LOOKUP;
1549 s->rwstate=SSL_NOTHING;
1550 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1552 s->state=SSL3_ST_CW_CERT_B;
1553 if ( !SSL_use_certificate(s,x509) ||
1554 !SSL_use_PrivateKey(s,pkey))
1560 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1563 if (x509 != NULL) X509_free(x509);
1564 if (pkey != NULL) EVP_PKEY_free(pkey);
1567 if (s->version == SSL3_VERSION)
1569 s->s3->tmp.cert_req=0;
1570 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1575 s->s3->tmp.cert_req=2;
1579 /* Ok, we have a cert */
1580 s->state=SSL3_ST_CW_CERT_C;
1583 if (s->state == SSL3_ST_CW_CERT_C)
1585 s->state=SSL3_ST_CW_CERT_D;
1586 l=ssl3_output_cert_chain(s,
1587 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1591 /* SSL3_ST_CW_CERT_D */
1592 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1595 #define has_bits(i,m) (((i)&(m)) == (m))
1597 static int ssl3_check_cert_and_algorithm(SSL *s)
1601 EVP_PKEY *pkey=NULL;
1610 sc=s->session->sess_cert;
1614 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
1618 algs=s->s3->tmp.new_cipher->algorithms;
1620 /* we don't have a certificate */
1621 if (algs & (SSL_aDH|SSL_aNULL))
1625 rsa=s->session->sess_cert->peer_rsa_tmp;
1628 dh=s->session->sess_cert->peer_dh_tmp;
1631 /* This is the passed certificate */
1633 idx=sc->peer_cert_type;
1634 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1635 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1636 EVP_PKEY_free(pkey);
1639 /* Check that we have a certificate if we require one */
1640 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1642 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1646 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1648 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1653 if ((algs & SSL_kRSA) &&
1654 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1656 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1661 if ((algs & SSL_kEDH) &&
1662 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1664 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1667 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1669 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1673 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1675 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1681 if (SSL_IS_EXPORT(algs) && !has_bits(i,EVP_PKT_EXP))
1684 if (algs & SSL_kRSA)
1687 || RSA_size(rsa) > SSL_EXPORT_PKEYLENGTH(algs))
1689 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1696 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1699 || DH_size(dh) > SSL_EXPORT_PKEYLENGTH(algs))
1701 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1708 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1714 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);