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.]
67 /* SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
68 * SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
69 * SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
70 * SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
71 * SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
72 * SSLerr(SSL_F_SSL3_GET_SERVER_DONE,ERR_R_MALLOC_FAILURE);
73 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
77 static SSL_METHOD *ssl3_get_client_method(int ver);
78 static int ssl3_client_hello(SSL *s);
79 static int ssl3_get_server_hello(SSL *s);
80 static int ssl3_get_certificate_request(SSL *s);
81 static int ca_dn_cmp(X509_NAME **a,X509_NAME **b);
82 static int ssl3_get_server_done(SSL *s);
83 static int ssl3_send_client_verify(SSL *s);
84 static int ssl3_send_client_certificate(SSL *s);
85 static int ssl3_send_client_key_exchange(SSL *s);
86 static int ssl3_get_key_exchange(SSL *s);
87 static int ssl3_get_server_certificate(SSL *s);
88 static int ssl3_check_cert_and_algorithm(SSL *s);
90 static SSL_METHOD *ssl3_get_client_method();
91 static int ssl3_client_hello();
92 static int ssl3_get_server_hello();
93 static int ssl3_get_certificate_request();
94 static int ca_dn_cmp();
95 static int ssl3_get_server_done();
96 static int ssl3_send_client_verify();
97 static int ssl3_send_client_certificate();
98 static int ssl3_send_client_key_exchange();
99 static int ssl3_get_key_exchange();
100 static int ssl3_get_server_certificate();
101 static int ssl3_check_cert_and_algorithm();
104 static SSL_METHOD *ssl3_get_client_method(ver)
107 if (ver == SSL3_VERSION)
108 return(SSLv3_client_method());
113 SSL_METHOD *SSLv3_client_method()
116 static SSL_METHOD SSLv3_client_data;
121 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
123 SSLv3_client_data.ssl_connect=ssl3_connect;
124 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
126 return(&SSLv3_client_data);
133 unsigned long Time=time(NULL),l;
138 int new_state,state,skip=0;;
140 RAND_seed(&Time,sizeof(Time));
144 if (s->info_callback != NULL)
146 else if (s->ctx->info_callback != NULL)
147 cb=s->ctx->info_callback;
149 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
158 case SSL_ST_RENEGOTIATE:
160 s->state=SSL_ST_CONNECT;
161 s->ctx->sess_connect_renegotiate++;
165 case SSL_ST_BEFORE|SSL_ST_CONNECT:
166 case SSL_ST_OK|SSL_ST_CONNECT:
168 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
170 if ((s->version & 0xff00 ) != 0x0300)
172 /* s->version=SSL3_VERSION; */
173 s->type=SSL_ST_CONNECT;
175 if (s->init_buf == NULL)
177 if ((buf=BUF_MEM_new()) == NULL)
182 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
190 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
192 /* setup buffing BIO */
193 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
195 /* don't push the buffering BIO quite yet */
197 ssl3_init_finished_mac(s);
199 s->state=SSL3_ST_CW_CLNT_HELLO_A;
200 s->ctx->sess_connect++;
204 case SSL3_ST_CW_CLNT_HELLO_A:
205 case SSL3_ST_CW_CLNT_HELLO_B:
208 ret=ssl3_client_hello(s);
209 if (ret <= 0) goto end;
210 s->state=SSL3_ST_CR_SRVR_HELLO_A;
213 /* turn on buffering for the next lot of output */
214 if (s->bbio != s->wbio)
215 s->wbio=BIO_push(s->bbio,s->wbio);
219 case SSL3_ST_CR_SRVR_HELLO_A:
220 case SSL3_ST_CR_SRVR_HELLO_B:
221 ret=ssl3_get_server_hello(s);
222 if (ret <= 0) goto end;
224 s->state=SSL3_ST_CR_FINISHED_A;
226 s->state=SSL3_ST_CR_CERT_A;
230 case SSL3_ST_CR_CERT_A:
231 case SSL3_ST_CR_CERT_B:
232 /* Check if it is anon DH */
233 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
235 ret=ssl3_get_server_certificate(s);
236 if (ret <= 0) goto end;
240 s->state=SSL3_ST_CR_KEY_EXCH_A;
244 case SSL3_ST_CR_KEY_EXCH_A:
245 case SSL3_ST_CR_KEY_EXCH_B:
246 ret=ssl3_get_key_exchange(s);
247 if (ret <= 0) goto end;
248 s->state=SSL3_ST_CR_CERT_REQ_A;
251 /* at this point we check that we have the
252 * required stuff from the server */
253 if (!ssl3_check_cert_and_algorithm(s))
260 case SSL3_ST_CR_CERT_REQ_A:
261 case SSL3_ST_CR_CERT_REQ_B:
262 ret=ssl3_get_certificate_request(s);
263 if (ret <= 0) goto end;
264 s->state=SSL3_ST_CR_SRVR_DONE_A;
268 case SSL3_ST_CR_SRVR_DONE_A:
269 case SSL3_ST_CR_SRVR_DONE_B:
270 ret=ssl3_get_server_done(s);
271 if (ret <= 0) goto end;
272 if (s->s3->tmp.cert_req)
273 s->state=SSL3_ST_CW_CERT_A;
275 s->state=SSL3_ST_CW_KEY_EXCH_A;
280 case SSL3_ST_CW_CERT_A:
281 case SSL3_ST_CW_CERT_B:
282 case SSL3_ST_CW_CERT_C:
283 ret=ssl3_send_client_certificate(s);
284 if (ret <= 0) goto end;
285 s->state=SSL3_ST_CW_KEY_EXCH_A;
289 case SSL3_ST_CW_KEY_EXCH_A:
290 case SSL3_ST_CW_KEY_EXCH_B:
291 ret=ssl3_send_client_key_exchange(s);
292 if (ret <= 0) goto end;
293 l=s->s3->tmp.new_cipher->algorithms;
294 /* EAY EAY EAY need to check for DH fix cert
296 /* For TLS, cert_req is set to 2, so a cert chain
297 * of nothing is sent, but no verify packet is sent */
298 if (s->s3->tmp.cert_req == 1)
300 s->state=SSL3_ST_CW_CERT_VRFY_A;
304 s->state=SSL3_ST_CW_CHANGE_A;
305 s->s3->change_cipher_spec=0;
311 case SSL3_ST_CW_CERT_VRFY_A:
312 case SSL3_ST_CW_CERT_VRFY_B:
313 ret=ssl3_send_client_verify(s);
314 if (ret <= 0) goto end;
315 s->state=SSL3_ST_CW_CHANGE_A;
317 s->s3->change_cipher_spec=0;
320 case SSL3_ST_CW_CHANGE_A:
321 case SSL3_ST_CW_CHANGE_B:
322 ret=ssl3_send_change_cipher_spec(s,
323 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
324 if (ret <= 0) goto end;
325 s->state=SSL3_ST_CW_FINISHED_A;
328 s->session->cipher=s->s3->tmp.new_cipher;
329 if (!s->method->ssl3_enc->setup_key_block(s))
335 if (!s->method->ssl3_enc->change_cipher_state(s,
336 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
344 case SSL3_ST_CW_FINISHED_A:
345 case SSL3_ST_CW_FINISHED_B:
346 ret=ssl3_send_finished(s,
347 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
348 s->method->ssl3_enc->client_finished,
349 s->method->ssl3_enc->client_finished_len);
350 if (ret <= 0) goto end;
351 s->state=SSL3_ST_CW_FLUSH;
354 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
357 s->s3->tmp.next_state=SSL_ST_OK;
358 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
361 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
362 s->s3->delay_buf_pop_ret=0;
367 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
372 case SSL3_ST_CR_FINISHED_A:
373 case SSL3_ST_CR_FINISHED_B:
375 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
376 SSL3_ST_CR_FINISHED_B);
377 if (ret <= 0) goto end;
380 s->state=SSL3_ST_CW_CHANGE_A;
386 case SSL3_ST_CW_FLUSH:
387 /* number of bytes to be flushed */
388 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
391 s->rwstate=SSL_WRITING;
392 num1=BIO_flush(s->wbio);
393 if (num1 <= 0) { ret= -1; goto end; }
394 s->rwstate=SSL_NOTHING;
397 s->state=s->s3->tmp.next_state;
401 /* clean a few things up */
402 ssl3_cleanup_key_block(s);
404 BUF_MEM_free(s->init_buf);
407 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
409 /* remove buffering */
410 under=BIO_pop(s->wbio);
419 /* else do it later */
424 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
425 if (s->hit) s->ctx->sess_hit++;
429 s->handshake_func=ssl3_connect;
430 s->ctx->sess_connect_good++;
432 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
438 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
444 /* did we do anything */
445 if (!s->s3->tmp.reuse_message && !skip)
449 if ((ret=BIO_flush(s->wbio)) <= 0)
453 if ((cb != NULL) && (s->state != state))
457 cb(s,SSL_CB_CONNECT_LOOP,1);
465 cb(s,SSL_CB_CONNECT_EXIT,ret);
471 static int ssl3_client_hello(s)
477 unsigned long Time,l;
479 buf=(unsigned char *)s->init_buf->data;
480 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
482 if ((s->session == NULL) ||
483 (s->session->ssl_version != s->version) ||
484 (s->session->not_resumable))
486 if (!ssl_get_new_session(s,0))
489 /* else use the pre-loaded session */
491 p=s->s3->client_random;
492 Time=time(NULL); /* Time */
494 RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
496 /* Do the message type and length last */
499 *(p++)=s->version>>8;
500 *(p++)=s->version&0xff;
503 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
510 i=s->session->session_id_length;
514 memcpy(p,s->session->session_id,i);
518 /* Ciphers supported */
519 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
522 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
528 /* hardwire in the NULL compression algorithm. */
535 *(d++)=SSL3_MT_CLIENT_HELLO;
538 s->state=SSL3_ST_CW_CLNT_HELLO_B;
539 /* number of bytes to write */
544 /* SSL3_ST_CW_CLNT_HELLO_B */
545 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
550 static int ssl3_get_server_hello(s)
560 n=ssl3_get_message(s,
561 SSL3_ST_CR_SRVR_HELLO_A,
562 SSL3_ST_CR_SRVR_HELLO_B,
563 SSL3_MT_SERVER_HELLO,
567 if (!ok) return((int)n);
568 d=p=(unsigned char *)s->init_buf->data;
570 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
572 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
573 s->version=(s->version&0xff00)|p[1];
574 al=SSL_AD_PROTOCOL_VERSION;
579 /* load the server hello data */
580 /* load the server random */
581 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
584 /* get the session-id */
587 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
589 /* SSLref returns 16 :-( */
590 if (j < SSL2_SSL_SESSION_ID_LENGTH)
592 al=SSL_AD_ILLEGAL_PARAMETER;
593 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
597 if ((j != 0) && (j == s->session->session_id_length) &&
598 (memcmp(p,s->session->session_id,j) == 0))
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_find(sk,(char *)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 */
654 al=SSL_AD_ILLEGAL_PARAMETER;
655 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
661 /* wrong packet length */
662 al=SSL_AD_DECODE_ERROR;
663 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
669 ssl3_send_alert(s,SSL3_AL_FATAL,al);
674 static int ssl3_get_server_certificate(s)
678 unsigned long n,nc,llen,l;
680 unsigned char *p,*d,*q;
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_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_push(sk,(char *)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);
768 if (c == NULL) goto err;
770 if (s->session->cert) ssl_cert_free(s->session->cert);
774 x=(X509 *)sk_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);
797 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
798 if (c->pkeys[i].x509 != NULL)
799 X509_free(c->pkeys[i].x509);
801 c->key= &(c->pkeys[i]);
803 if ((s->session != NULL) && (s->session->peer != NULL))
804 X509_free(s->session->peer);
805 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
814 ssl3_send_alert(s,SSL3_AL_FATAL,al);
819 sk_pop_free(sk,X509_free);
823 static int ssl3_get_key_exchange(s)
827 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
830 unsigned char *param,*p;
831 int al,i,j,param_len,ok;
839 n=ssl3_get_message(s,
840 SSL3_ST_CR_KEY_EXCH_A,
841 SSL3_ST_CR_KEY_EXCH_B,
846 if (!ok) return((int)n);
848 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
850 s->s3->tmp.reuse_message=1;
854 param=p=(unsigned char *)s->init_buf->data;
856 if (s->session->cert != NULL)
859 if (s->session->cert->rsa_tmp != NULL)
861 RSA_free(s->session->cert->rsa_tmp);
862 s->session->cert->rsa_tmp=NULL;
866 if (s->session->cert->dh_tmp)
868 DH_free(s->session->cert->dh_tmp);
869 s->session->cert->dh_tmp=NULL;
875 s->session->cert=ssl_cert_new();
879 alg=s->s3->tmp.new_cipher->algorithms;
884 if ((rsa=RSA_new()) == NULL)
886 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
893 al=SSL_AD_DECODE_ERROR;
894 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
897 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
899 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
908 al=SSL_AD_DECODE_ERROR;
909 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
912 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
914 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
920 /* s->session->cert->rsa_tmp=rsa;*/
921 /* this should be because we are using an export cipher */
923 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
926 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
929 s->session->cert->rsa_tmp=rsa;
936 if ((dh=DH_new()) == NULL)
938 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
945 al=SSL_AD_DECODE_ERROR;
946 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
949 if (!(dh->p=BN_bin2bn(p,i,NULL)))
951 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
960 al=SSL_AD_DECODE_ERROR;
961 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
964 if (!(dh->g=BN_bin2bn(p,i,NULL)))
966 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
975 al=SSL_AD_DECODE_ERROR;
976 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
979 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
981 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
989 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
994 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_DSA_SIGN].x509);
996 /* else anonymous DH, so no certificate or pkey. */
998 s->session->cert->dh_tmp=dh;
1000 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1002 al=SSL_AD_ILLEGAL_PARAMETER;
1003 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1009 al=SSL_AD_HANDSHAKE_FAILURE;
1010 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1015 /* p points to the next byte, there are 'n' bytes left */
1018 /* if it was signed, check the signature */
1023 j=EVP_PKEY_size(pkey);
1025 if ((i != n) || (n > j) || (n <= 0))
1027 /* wrong packet length */
1028 al=SSL_AD_DECODE_ERROR;
1029 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1034 if (pkey->type == EVP_PKEY_RSA)
1040 for (num=2; num > 0; num--)
1042 EVP_DigestInit(&md_ctx,(num == 2)
1043 ?s->ctx->md5:s->ctx->sha1);
1044 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1045 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1046 EVP_DigestUpdate(&md_ctx,param,param_len);
1047 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1051 i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,
1055 al=SSL_AD_DECRYPT_ERROR;
1056 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1059 if ((j != i) || (memcmp(p,md_buf,i) != 0))
1062 al=SSL_AD_DECRYPT_ERROR;
1063 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1070 if (pkey->type == EVP_PKEY_DSA)
1073 EVP_VerifyInit(&md_ctx,EVP_dss1());
1074 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1075 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1076 EVP_VerifyUpdate(&md_ctx,param,param_len);
1077 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1080 al=SSL_AD_DECRYPT_ERROR;
1081 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1088 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1094 /* still data left over */
1095 if (!(alg & SSL_aNULL))
1097 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1102 al=SSL_AD_DECODE_ERROR;
1103 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1107 EVP_PKEY_free(pkey);
1110 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1112 EVP_PKEY_free(pkey);
1116 static int ssl3_get_certificate_request(s)
1120 unsigned long n,nc,l;
1121 unsigned int llen,ctype_num,i;
1123 unsigned char *p,*d,*q;
1126 n=ssl3_get_message(s,
1127 SSL3_ST_CR_CERT_REQ_A,
1128 SSL3_ST_CR_CERT_REQ_B,
1130 #if defined(MSDOS) && !defined(WIN32)
1131 1024*30, /* 30k max cert list :-) */
1133 1024*100, /* 100k max cert list :-) */
1137 if (!ok) return((int)n);
1139 s->s3->tmp.cert_req=0;
1141 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1143 s->s3->tmp.reuse_message=1;
1147 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1149 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1150 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1154 /* TLS does not like anon-DH with client cert */
1155 if (s->version > SSL3_VERSION)
1157 l=s->s3->tmp.new_cipher->algorithms;
1160 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1161 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1166 d=p=(unsigned char *)s->init_buf->data;
1168 if ((ca_sk=sk_new(ca_dn_cmp)) == NULL)
1170 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1174 /* get the certificate types */
1176 if (ctype_num > SSL3_CT_NUMBER)
1177 ctype_num=SSL3_CT_NUMBER;
1178 for (i=0; i<ctype_num; i++)
1179 s->s3->tmp.ctype[i]= p[i];
1182 /* get the CA RDNs */
1187 out=fopen("/tmp/vsign.der","w");
1188 fwrite(p,1,llen,out);
1193 if ((llen+ctype_num+2+1) != n)
1195 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1196 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1200 for (nc=0; nc<llen; )
1203 if ((l+nc+2) > llen)
1205 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1206 goto cont; /* netscape bugs */
1207 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1208 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1214 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1216 /* If netscape tollerance is on, ignore errors */
1217 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1221 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1222 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1229 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1230 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1233 if (!sk_push(ca_sk,(char *)xn))
1235 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1249 /* we should setup a certficate to return.... */
1250 s->s3->tmp.cert_req=1;
1251 s->s3->tmp.ctype_num=ctype_num;
1252 if (s->s3->tmp.ca_names != NULL)
1253 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1254 s->s3->tmp.ca_names=ca_sk;
1259 if (ca_sk != NULL) sk_pop_free(ca_sk,X509_NAME_free);
1263 static int ca_dn_cmp(a,b)
1266 return(X509_NAME_cmp(*a,*b));
1269 static int ssl3_get_server_done(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(s)
1296 unsigned char *p,*q,*d;
1299 EVP_PKEY *pkey=NULL;
1301 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1303 d=(unsigned char *)s->init_buf->data;
1306 l=s->s3->tmp.new_cipher->algorithms;
1312 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1314 if (s->session->cert->rsa_tmp != NULL)
1315 rsa=s->session->cert->rsa_tmp;
1318 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
1319 if ((pkey == NULL) ||
1320 (pkey->type != EVP_PKEY_RSA) ||
1321 (pkey->pkey.rsa == NULL))
1323 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1329 tmp_buf[0]=s->version>>8;
1330 tmp_buf[1]=s->version&0xff;
1331 RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1333 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1336 /* Fix buf for TLS and beyond */
1337 if (s->version > SSL3_VERSION)
1339 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1340 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1342 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1343 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1347 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1351 /* Fix buf for TLS and beyond */
1352 if (s->version > SSL3_VERSION)
1358 s->session->master_key_length=
1359 s->method->ssl3_enc->generate_master_secret(s,
1360 s->session->master_key,
1361 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1362 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1367 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1369 DH *dh_srvr,*dh_clnt;
1371 if (s->session->cert->dh_tmp != NULL)
1372 dh_srvr=s->session->cert->dh_tmp;
1375 /* we get them from the cert */
1376 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1377 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1381 /* generate a new random key */
1382 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1384 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1387 if (!DH_generate_key(dh_clnt))
1389 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1393 /* use the 'p' output buffer for the DH key, but
1394 * make sure to clear it out afterwards */
1396 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1400 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1404 /* generate master key from the result */
1405 s->session->master_key_length=
1406 s->method->ssl3_enc->generate_master_secret(s,
1407 s->session->master_key,p,n);
1411 /* send off the data */
1412 n=BN_num_bytes(dh_clnt->pub_key);
1414 BN_bn2bin(dh_clnt->pub_key,p);
1419 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1424 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1425 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1429 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1432 s->state=SSL3_ST_CW_KEY_EXCH_B;
1433 /* number of bytes to write */
1438 /* SSL3_ST_CW_KEY_EXCH_B */
1439 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1444 static int ssl3_send_client_verify(s)
1447 unsigned char *p,*d;
1448 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1456 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1458 d=(unsigned char *)s->init_buf->data;
1460 pkey=s->cert->key->privatekey;
1462 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1463 &(data[MD5_DIGEST_LENGTH]));
1466 if (pkey->type == EVP_PKEY_RSA)
1468 s->method->ssl3_enc->cert_verify_mac(s,
1469 &(s->s3->finish_dgst1),&(data[0]));
1470 i=RSA_private_encrypt(
1471 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1472 data,&(p[2]),pkey->pkey.rsa,
1476 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1485 if (pkey->type == EVP_PKEY_DSA)
1487 if (!DSA_sign(pkey->save_type,
1488 &(data[MD5_DIGEST_LENGTH]),
1489 SHA_DIGEST_LENGTH,&(p[2]),
1490 (unsigned int *)&j,pkey->pkey.dsa))
1492 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1501 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
1504 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1507 s->init_num=(int)n+4;
1510 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1515 static int ssl3_send_client_certificate(s)
1519 EVP_PKEY *pkey=NULL;
1523 if (s->state == SSL3_ST_CW_CERT_A)
1525 if ((s->cert == NULL) ||
1526 (s->cert->key->x509 == NULL) ||
1527 (s->cert->key->privatekey == NULL))
1528 s->state=SSL3_ST_CW_CERT_B;
1530 s->state=SSL3_ST_CW_CERT_C;
1533 /* We need to get a client cert */
1534 if (s->state == SSL3_ST_CW_CERT_B)
1536 /* If we get an error, we need to
1537 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1538 * We then get retied later */
1540 if (s->ctx->client_cert_cb != NULL)
1541 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1544 s->rwstate=SSL_X509_LOOKUP;
1547 s->rwstate=SSL_NOTHING;
1548 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1550 s->state=SSL3_ST_CW_CERT_B;
1551 if ( !SSL_use_certificate(s,x509) ||
1552 !SSL_use_PrivateKey(s,pkey))
1558 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1561 if (x509 != NULL) X509_free(x509);
1562 if (pkey != NULL) EVP_PKEY_free(pkey);
1565 if (s->version == SSL3_VERSION)
1567 s->s3->tmp.cert_req=0;
1568 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1573 s->s3->tmp.cert_req=2;
1577 /* Ok, we have a cert */
1578 s->state=SSL3_ST_CW_CERT_C;
1581 if (s->state == SSL3_ST_CW_CERT_C)
1583 s->state=SSL3_ST_CW_CERT_D;
1584 l=ssl3_output_cert_chain(s,
1585 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1589 /* SSL3_ST_CW_CERT_D */
1590 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1593 #define has_bits(i,m) (((i)&(m)) == (m))
1595 static int ssl3_check_cert_and_algorithm(s)
1600 EVP_PKEY *pkey=NULL;
1609 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
1613 algs=s->s3->tmp.new_cipher->algorithms;
1615 /* we don't have a certificate */
1616 if (algs & (SSL_aDH|SSL_aNULL))
1619 rsa=s->session->cert->rsa_tmp;
1620 dh=s->session->cert->dh_tmp;
1622 /* This is the passed certificate */
1625 pkey=X509_get_pubkey(c->pkeys[idx].x509);
1626 i=X509_certificate_type(c->pkeys[idx].x509,pkey);
1627 EVP_PKEY_free(pkey);
1630 /* Check that we have a certificate if we require one */
1631 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1633 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1637 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1639 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1644 if ((algs & SSL_kRSA) &&
1645 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1647 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1651 else if ((algs & SSL_kEDH) &&
1652 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1654 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1657 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1659 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1663 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1665 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1671 if ((algs & SSL_EXP) && !has_bits(i,EVP_PKT_EXP))
1674 if (algs & SSL_kRSA)
1676 if ((rsa == NULL) || (RSA_size(rsa) > 512))
1678 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1685 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1687 if ((dh == NULL) || (DH_size(dh) > 512))
1689 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1696 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1702 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);