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;
137 int new_state,state,skip=0;;
139 RAND_seed(&Time,sizeof(Time));
143 if (s->info_callback != NULL)
145 else if (s->ctx->info_callback != NULL)
146 cb=s->ctx->info_callback;
148 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
157 case SSL_ST_RENEGOTIATE:
159 s->state=SSL_ST_CONNECT;
160 s->ctx->stats.sess_connect_renegotiate++;
164 case SSL_ST_BEFORE|SSL_ST_CONNECT:
165 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->stats.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->s3->tmp.new_compression == NULL)
330 s->session->compress_meth=0;
332 s->session->compress_meth=
333 s->s3->tmp.new_compression->id;
334 if (!s->method->ssl3_enc->setup_key_block(s))
340 if (!s->method->ssl3_enc->change_cipher_state(s,
341 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
349 case SSL3_ST_CW_FINISHED_A:
350 case SSL3_ST_CW_FINISHED_B:
351 ret=ssl3_send_finished(s,
352 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
353 s->method->ssl3_enc->client_finished,
354 s->method->ssl3_enc->client_finished_len);
355 if (ret <= 0) goto end;
356 s->state=SSL3_ST_CW_FLUSH;
359 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
362 s->s3->tmp.next_state=SSL_ST_OK;
363 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
366 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
367 s->s3->delay_buf_pop_ret=0;
372 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
377 case SSL3_ST_CR_FINISHED_A:
378 case SSL3_ST_CR_FINISHED_B:
380 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
381 SSL3_ST_CR_FINISHED_B);
382 if (ret <= 0) goto end;
385 s->state=SSL3_ST_CW_CHANGE_A;
391 case SSL3_ST_CW_FLUSH:
392 /* number of bytes to be flushed */
393 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
396 s->rwstate=SSL_WRITING;
397 num1=BIO_flush(s->wbio);
398 if (num1 <= 0) { ret= -1; goto end; }
399 s->rwstate=SSL_NOTHING;
402 s->state=s->s3->tmp.next_state;
406 /* clean a few things up */
407 ssl3_cleanup_key_block(s);
409 if (s->init_buf != NULL)
411 BUF_MEM_free(s->init_buf);
415 /* If we are not 'joining' the last two packets,
416 * remove the buffering now */
417 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
418 ssl_free_wbio_buffer(s);
419 /* else do it later in ssl3_write */
424 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
425 if (s->hit) s->ctx->stats.sess_hit++;
429 s->handshake_func=ssl3_connect;
430 s->ctx->stats.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;
480 buf=(unsigned char *)s->init_buf->data;
481 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
483 if ((s->session == NULL) ||
484 (s->session->ssl_version != s->version) ||
485 (s->session->not_resumable))
487 if (!ssl_get_new_session(s,0))
490 /* else use the pre-loaded session */
492 p=s->s3->client_random;
493 Time=time(NULL); /* Time */
495 RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
497 /* Do the message type and length last */
500 *(p++)=s->version>>8;
501 *(p++)=s->version&0xff;
502 s->client_version=s->version;
505 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
512 i=s->session->session_id_length;
516 memcpy(p,s->session->session_id,i);
520 /* Ciphers supported */
521 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
524 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
531 if (s->ctx->comp_methods == NULL)
534 j=sk_num(s->ctx->comp_methods);
538 comp=(SSL_COMP *)sk_value(s->ctx->comp_methods,i);
541 *(p++)=0; /* Add the NULL method */
545 *(d++)=SSL3_MT_CLIENT_HELLO;
548 s->state=SSL3_ST_CW_CLNT_HELLO_B;
549 /* number of bytes to write */
554 /* SSL3_ST_CW_CLNT_HELLO_B */
555 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
560 static int ssl3_get_server_hello(s)
571 n=ssl3_get_message(s,
572 SSL3_ST_CR_SRVR_HELLO_A,
573 SSL3_ST_CR_SRVR_HELLO_B,
574 SSL3_MT_SERVER_HELLO,
578 if (!ok) return((int)n);
579 d=p=(unsigned char *)s->init_buf->data;
581 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
583 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
584 s->version=(s->version&0xff00)|p[1];
585 al=SSL_AD_PROTOCOL_VERSION;
590 /* load the server hello data */
591 /* load the server random */
592 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
595 /* get the session-id */
598 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
600 /* SSLref returns 16 :-( */
601 if (j < SSL2_SSL_SESSION_ID_LENGTH)
603 al=SSL_AD_ILLEGAL_PARAMETER;
604 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
608 if ((j != 0) && (j == s->session->session_id_length) &&
609 (memcmp(p,s->session->session_id,j) == 0))
611 else /* a miss or crap from the other end */
613 /* If we were trying for session-id reuse, make a new
614 * SSL_SESSION so we don't stuff up other people */
616 if (s->session->session_id_length > 0)
618 if (!ssl_get_new_session(s,0))
620 al=SSL_AD_INTERNAL_ERROR;
624 s->session->session_id_length=j;
625 memcpy(s->session->session_id,p,j); /* j could be 0 */
628 c=ssl_get_cipher_by_char(s,p);
632 al=SSL_AD_ILLEGAL_PARAMETER;
633 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
636 p+=ssl_put_cipher_by_char(s,NULL,NULL);
638 sk=ssl_get_ciphers_by_id(s);
639 i=sk_find(sk,(char *)c);
642 /* we did not say we would use this cipher */
643 al=SSL_AD_ILLEGAL_PARAMETER;
644 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
648 if (s->hit && (s->session->cipher != c))
651 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
653 al=SSL_AD_ILLEGAL_PARAMETER;
654 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
658 s->s3->tmp.new_cipher=c;
660 /* lets get the compression algorithm */
666 comp=ssl3_comp_find(s->ctx->comp_methods,j);
668 if ((j != 0) && (comp == NULL))
670 al=SSL_AD_ILLEGAL_PARAMETER;
671 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
676 s->s3->tmp.new_compression=comp;
681 /* wrong packet length */
682 al=SSL_AD_DECODE_ERROR;
683 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
689 ssl3_send_alert(s,SSL3_AL_FATAL,al);
694 static int ssl3_get_server_certificate(s)
698 unsigned long n,nc,llen,l;
700 unsigned char *p,*d,*q;
705 n=ssl3_get_message(s,
709 #if defined(MSDOS) && !defined(WIN32)
710 1024*30, /* 30k max cert list :-) */
712 1024*100, /* 100k max cert list :-) */
716 if (!ok) return((int)n);
718 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
720 s->s3->tmp.reuse_message=1;
724 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
726 al=SSL_AD_UNEXPECTED_MESSAGE;
727 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
730 d=p=(unsigned char *)s->init_buf->data;
732 if ((sk=sk_new_null()) == NULL)
734 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
741 al=SSL_AD_DECODE_ERROR;
742 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
745 for (nc=0; nc<llen; )
750 al=SSL_AD_DECODE_ERROR;
751 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
756 x=d2i_X509(NULL,&q,l);
759 al=SSL_AD_BAD_CERTIFICATE;
760 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
765 al=SSL_AD_DECODE_ERROR;
766 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
769 if (!sk_push(sk,(char *)x))
771 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
779 i=ssl_verify_cert_chain(s,sk);
780 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
782 al=ssl_verify_alarm_type(s->verify_result);
783 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
788 if (c == NULL) goto err;
790 if (s->session->cert) ssl_cert_free(s->session->cert);
794 x=(X509 *)sk_value(sk,0);
797 pkey=X509_get_pubkey(x);
799 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
803 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
807 i=ssl_cert_type(x,pkey);
812 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
817 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
818 if (c->pkeys[i].x509 != NULL)
819 X509_free(c->pkeys[i].x509);
821 c->key= &(c->pkeys[i]);
823 if ((s->session != NULL) && (s->session->peer != NULL))
824 X509_free(s->session->peer);
825 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
834 ssl3_send_alert(s,SSL3_AL_FATAL,al);
839 sk_pop_free(sk,X509_free);
843 static int ssl3_get_key_exchange(s)
847 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
850 unsigned char *param,*p;
851 int al,i,j,param_len,ok;
859 n=ssl3_get_message(s,
860 SSL3_ST_CR_KEY_EXCH_A,
861 SSL3_ST_CR_KEY_EXCH_B,
866 if (!ok) return((int)n);
868 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
870 s->s3->tmp.reuse_message=1;
874 param=p=(unsigned char *)s->init_buf->data;
876 if (s->session->cert != NULL)
879 if (s->session->cert->rsa_tmp != NULL)
881 RSA_free(s->session->cert->rsa_tmp);
882 s->session->cert->rsa_tmp=NULL;
886 if (s->session->cert->dh_tmp)
888 DH_free(s->session->cert->dh_tmp);
889 s->session->cert->dh_tmp=NULL;
895 s->session->cert=ssl_cert_new();
899 alg=s->s3->tmp.new_cipher->algorithms;
904 if ((rsa=RSA_new()) == NULL)
906 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
913 al=SSL_AD_DECODE_ERROR;
914 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
917 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
919 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
928 al=SSL_AD_DECODE_ERROR;
929 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
932 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
934 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
940 /* s->session->cert->rsa_tmp=rsa;*/
941 /* this should be because we are using an export cipher */
943 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
946 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
949 s->session->cert->rsa_tmp=rsa;
956 if ((dh=DH_new()) == NULL)
958 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
965 al=SSL_AD_DECODE_ERROR;
966 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
969 if (!(dh->p=BN_bin2bn(p,i,NULL)))
971 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
980 al=SSL_AD_DECODE_ERROR;
981 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
984 if (!(dh->g=BN_bin2bn(p,i,NULL)))
986 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
995 al=SSL_AD_DECODE_ERROR;
996 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
999 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1001 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1009 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
1014 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_DSA_SIGN].x509);
1016 /* else anonymous DH, so no certificate or pkey. */
1018 s->session->cert->dh_tmp=dh;
1021 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1023 al=SSL_AD_ILLEGAL_PARAMETER;
1024 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1030 al=SSL_AD_HANDSHAKE_FAILURE;
1031 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1036 /* p points to the next byte, there are 'n' bytes left */
1039 /* if it was signed, check the signature */
1044 j=EVP_PKEY_size(pkey);
1046 if ((i != n) || (n > j) || (n <= 0))
1048 /* wrong packet length */
1049 al=SSL_AD_DECODE_ERROR;
1050 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1055 if (pkey->type == EVP_PKEY_RSA)
1061 for (num=2; num > 0; num--)
1063 EVP_DigestInit(&md_ctx,(num == 2)
1064 ?s->ctx->md5:s->ctx->sha1);
1065 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1066 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1067 EVP_DigestUpdate(&md_ctx,param,param_len);
1068 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1072 i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,
1076 al=SSL_AD_DECRYPT_ERROR;
1077 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1080 if ((j != i) || (memcmp(p,md_buf,i) != 0))
1083 al=SSL_AD_DECRYPT_ERROR;
1084 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1091 if (pkey->type == EVP_PKEY_DSA)
1094 EVP_VerifyInit(&md_ctx,EVP_dss1());
1095 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1096 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1097 EVP_VerifyUpdate(&md_ctx,param,param_len);
1098 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1101 al=SSL_AD_DECRYPT_ERROR;
1102 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1109 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1115 /* still data left over */
1116 if (!(alg & SSL_aNULL))
1118 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1123 al=SSL_AD_DECODE_ERROR;
1124 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1128 EVP_PKEY_free(pkey);
1131 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1133 EVP_PKEY_free(pkey);
1137 static int ssl3_get_certificate_request(s)
1141 unsigned long n,nc,l;
1142 unsigned int llen,ctype_num,i;
1144 unsigned char *p,*d,*q;
1147 n=ssl3_get_message(s,
1148 SSL3_ST_CR_CERT_REQ_A,
1149 SSL3_ST_CR_CERT_REQ_B,
1151 #if defined(MSDOS) && !defined(WIN32)
1152 1024*30, /* 30k max cert list :-) */
1154 1024*100, /* 100k max cert list :-) */
1158 if (!ok) return((int)n);
1160 s->s3->tmp.cert_req=0;
1162 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1164 s->s3->tmp.reuse_message=1;
1168 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1170 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1171 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1175 /* TLS does not like anon-DH with client cert */
1176 if (s->version > SSL3_VERSION)
1178 l=s->s3->tmp.new_cipher->algorithms;
1181 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1182 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1187 d=p=(unsigned char *)s->init_buf->data;
1189 if ((ca_sk=sk_new(ca_dn_cmp)) == NULL)
1191 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1195 /* get the certificate types */
1197 if (ctype_num > SSL3_CT_NUMBER)
1198 ctype_num=SSL3_CT_NUMBER;
1199 for (i=0; i<ctype_num; i++)
1200 s->s3->tmp.ctype[i]= p[i];
1203 /* get the CA RDNs */
1208 out=fopen("/tmp/vsign.der","w");
1209 fwrite(p,1,llen,out);
1214 if ((llen+ctype_num+2+1) != n)
1216 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1217 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1221 for (nc=0; nc<llen; )
1224 if ((l+nc+2) > llen)
1226 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1227 goto cont; /* netscape bugs */
1228 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1229 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1235 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1237 /* If netscape tollerance is on, ignore errors */
1238 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1242 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1243 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1250 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1251 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1254 if (!sk_push(ca_sk,(char *)xn))
1256 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1270 /* we should setup a certficate to return.... */
1271 s->s3->tmp.cert_req=1;
1272 s->s3->tmp.ctype_num=ctype_num;
1273 if (s->s3->tmp.ca_names != NULL)
1274 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1275 s->s3->tmp.ca_names=ca_sk;
1280 if (ca_sk != NULL) sk_pop_free(ca_sk,X509_NAME_free);
1284 static int ca_dn_cmp(a,b)
1287 return(X509_NAME_cmp(*a,*b));
1290 static int ssl3_get_server_done(s)
1296 n=ssl3_get_message(s,
1297 SSL3_ST_CR_SRVR_DONE_A,
1298 SSL3_ST_CR_SRVR_DONE_B,
1299 SSL3_MT_SERVER_DONE,
1300 30, /* should be very small, like 0 :-) */
1303 if (!ok) return((int)n);
1306 /* should contain no data */
1307 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1308 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1314 static int ssl3_send_client_key_exchange(s)
1317 unsigned char *p,*q,*d;
1320 EVP_PKEY *pkey=NULL;
1322 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1324 d=(unsigned char *)s->init_buf->data;
1327 l=s->s3->tmp.new_cipher->algorithms;
1333 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1335 if (s->session->cert->rsa_tmp != NULL)
1336 rsa=s->session->cert->rsa_tmp;
1339 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
1340 if ((pkey == NULL) ||
1341 (pkey->type != EVP_PKEY_RSA) ||
1342 (pkey->pkey.rsa == NULL))
1344 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1350 tmp_buf[0]=s->client_version>>8;
1351 tmp_buf[1]=s->client_version&0xff;
1352 RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1354 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1357 /* Fix buf for TLS and beyond */
1358 if (s->version > SSL3_VERSION)
1360 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1361 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1363 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1364 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1368 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1372 /* Fix buf for TLS and beyond */
1373 if (s->version > SSL3_VERSION)
1379 s->session->master_key_length=
1380 s->method->ssl3_enc->generate_master_secret(s,
1381 s->session->master_key,
1382 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1383 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1388 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1390 DH *dh_srvr,*dh_clnt;
1392 if (s->session->cert->dh_tmp != NULL)
1393 dh_srvr=s->session->cert->dh_tmp;
1396 /* we get them from the cert */
1397 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1398 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1402 /* generate a new random key */
1403 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1405 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1408 if (!DH_generate_key(dh_clnt))
1410 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1414 /* use the 'p' output buffer for the DH key, but
1415 * make sure to clear it out afterwards */
1417 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1421 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1425 /* generate master key from the result */
1426 s->session->master_key_length=
1427 s->method->ssl3_enc->generate_master_secret(s,
1428 s->session->master_key,p,n);
1432 /* send off the data */
1433 n=BN_num_bytes(dh_clnt->pub_key);
1435 BN_bn2bin(dh_clnt->pub_key,p);
1440 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1445 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1446 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1450 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1453 s->state=SSL3_ST_CW_KEY_EXCH_B;
1454 /* number of bytes to write */
1459 /* SSL3_ST_CW_KEY_EXCH_B */
1460 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1465 static int ssl3_send_client_verify(s)
1468 unsigned char *p,*d;
1469 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1477 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1479 d=(unsigned char *)s->init_buf->data;
1481 pkey=s->cert->key->privatekey;
1483 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1484 &(data[MD5_DIGEST_LENGTH]));
1487 if (pkey->type == EVP_PKEY_RSA)
1489 s->method->ssl3_enc->cert_verify_mac(s,
1490 &(s->s3->finish_dgst1),&(data[0]));
1491 i=RSA_private_encrypt(
1492 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1493 data,&(p[2]),pkey->pkey.rsa,
1497 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1506 if (pkey->type == EVP_PKEY_DSA)
1508 if (!DSA_sign(pkey->save_type,
1509 &(data[MD5_DIGEST_LENGTH]),
1510 SHA_DIGEST_LENGTH,&(p[2]),
1511 (unsigned int *)&j,pkey->pkey.dsa))
1513 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1522 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
1525 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1528 s->init_num=(int)n+4;
1531 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1536 static int ssl3_send_client_certificate(s)
1540 EVP_PKEY *pkey=NULL;
1544 if (s->state == SSL3_ST_CW_CERT_A)
1546 if ((s->cert == NULL) ||
1547 (s->cert->key->x509 == NULL) ||
1548 (s->cert->key->privatekey == NULL))
1549 s->state=SSL3_ST_CW_CERT_B;
1551 s->state=SSL3_ST_CW_CERT_C;
1554 /* We need to get a client cert */
1555 if (s->state == SSL3_ST_CW_CERT_B)
1557 /* If we get an error, we need to
1558 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1559 * We then get retied later */
1561 if (s->ctx->client_cert_cb != NULL)
1562 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1565 s->rwstate=SSL_X509_LOOKUP;
1568 s->rwstate=SSL_NOTHING;
1569 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1571 s->state=SSL3_ST_CW_CERT_B;
1572 if ( !SSL_use_certificate(s,x509) ||
1573 !SSL_use_PrivateKey(s,pkey))
1579 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1582 if (x509 != NULL) X509_free(x509);
1583 if (pkey != NULL) EVP_PKEY_free(pkey);
1586 if (s->version == SSL3_VERSION)
1588 s->s3->tmp.cert_req=0;
1589 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1594 s->s3->tmp.cert_req=2;
1598 /* Ok, we have a cert */
1599 s->state=SSL3_ST_CW_CERT_C;
1602 if (s->state == SSL3_ST_CW_CERT_C)
1604 s->state=SSL3_ST_CW_CERT_D;
1605 l=ssl3_output_cert_chain(s,
1606 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1610 /* SSL3_ST_CW_CERT_D */
1611 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1614 #define has_bits(i,m) (((i)&(m)) == (m))
1616 static int ssl3_check_cert_and_algorithm(s)
1621 EVP_PKEY *pkey=NULL;
1630 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
1634 algs=s->s3->tmp.new_cipher->algorithms;
1636 /* we don't have a certificate */
1637 if (algs & (SSL_aDH|SSL_aNULL))
1640 rsa=s->session->cert->rsa_tmp;
1641 dh=s->session->cert->dh_tmp;
1643 /* This is the passed certificate */
1646 pkey=X509_get_pubkey(c->pkeys[idx].x509);
1647 i=X509_certificate_type(c->pkeys[idx].x509,pkey);
1648 EVP_PKEY_free(pkey);
1651 /* Check that we have a certificate if we require one */
1652 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1654 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1658 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1660 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1665 if ((algs & SSL_kRSA) &&
1666 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1668 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1672 else if ((algs & SSL_kEDH) &&
1673 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1675 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1678 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1680 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1684 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1686 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1692 if (SSL_IS_EXPORT(algs) && !has_bits(i,EVP_PKT_EXP))
1695 if (algs & SSL_kRSA)
1698 || RSA_size(rsa) > SSL_EXPORT_PKEYLENGTH(algs))
1700 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1707 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1710 || DH_size(dh) > SSL_EXPORT_PKEYLENGTH(algs))
1712 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1719 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1725 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);