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.]
59 #define REUSE_CIPHER_BUG
70 /* SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_MALLOC_FAILURE);
71 * SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
72 * SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
73 * SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_MALLOC_FAILURE);
74 * SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
78 static SSL_METHOD *ssl3_get_server_method(int ver);
79 static int ssl3_get_client_hello(SSL *s);
80 static int ssl3_send_server_hello(SSL *s);
81 static int ssl3_send_server_key_exchange(SSL *s);
82 static int ssl3_send_certificate_request(SSL *s);
83 static int ssl3_send_server_done(SSL *s);
84 static int ssl3_get_cert_verify(SSL *s);
85 static int ssl3_get_client_key_exchange(SSL *s);
86 static int ssl3_get_client_certificate(SSL *s);
87 static int ssl3_send_hello_request(SSL *s);
91 static SSL_METHOD *ssl3_get_server_method();
92 static int ssl3_get_client_hello();
93 static int ssl3_send_server_hello();
94 static int ssl3_send_server_key_exchange();
95 static int ssl3_send_certificate_request();
96 static int ssl3_send_server_done();
97 static int ssl3_get_cert_verify();
98 static int ssl3_get_client_key_exchange();
99 static int ssl3_get_client_certificate();
100 static int ssl3_send_hello_request();
104 static SSL_METHOD *ssl3_get_server_method(ver)
107 if (ver == SSL3_VERSION)
108 return(SSLv3_server_method());
113 SSL_METHOD *SSLv3_server_method()
116 static SSL_METHOD SSLv3_server_data;
121 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
123 SSLv3_server_data.ssl_accept=ssl3_accept;
124 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
126 return(&SSLv3_server_data);
133 unsigned long l,Time=time(NULL);
139 int new_state,state,skip=0;
141 RAND_seed(&Time,sizeof(Time));
145 if (s->info_callback != NULL)
147 else if (s->ctx->info_callback != NULL)
148 cb=s->ctx->info_callback;
150 /* init things to blank */
151 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
155 /* FIX THIS EAY EAY EAY */
156 /* we don't actually need a cert, we just need a cert or a DH_tmp */
157 if (((s->session == NULL) || (s->session->cert == NULL)) &&
160 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
172 case SSL_ST_RENEGOTIATE:
174 /* s->state=SSL_ST_ACCEPT; */
178 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
179 case SSL_ST_OK|SSL_ST_ACCEPT:
181 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
183 if ((s->version>>8) != 3)
185 /* s->version=SSL3_VERSION; */
186 s->type=SSL_ST_ACCEPT;
188 if (s->init_buf == NULL)
190 if ((buf=BUF_MEM_new()) == NULL)
195 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
203 if (!ssl3_setup_buffers(s))
209 /* Ok, we now need to push on a buffering BIO so that
210 * the output is sent in a way that TCP likes :-)
212 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
216 if (s->state != SSL_ST_RENEGOTIATE)
218 s->state=SSL3_ST_SR_CLNT_HELLO_A;
219 ssl3_init_finished_mac(s);
220 s->ctx->sess_accept++;
224 s->ctx->sess_accept_renegotiate++;
225 s->state=SSL3_ST_SW_HELLO_REQ_A;
229 case SSL3_ST_SW_HELLO_REQ_A:
230 case SSL3_ST_SW_HELLO_REQ_B:
233 ret=ssl3_send_hello_request(s);
234 if (ret <= 0) goto end;
235 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
236 s->state=SSL3_ST_SW_FLUSH;
239 ssl3_init_finished_mac(s);
242 case SSL3_ST_SW_HELLO_REQ_C:
243 /* remove buffering on output */
244 under=BIO_pop(s->wbio);
257 case SSL3_ST_SR_CLNT_HELLO_A:
258 case SSL3_ST_SR_CLNT_HELLO_B:
259 case SSL3_ST_SR_CLNT_HELLO_C:
262 ret=ssl3_get_client_hello(s);
263 if (ret <= 0) goto end;
264 s->state=SSL3_ST_SW_SRVR_HELLO_A;
268 case SSL3_ST_SW_SRVR_HELLO_A:
269 case SSL3_ST_SW_SRVR_HELLO_B:
270 ret=ssl3_send_server_hello(s);
271 if (ret <= 0) goto end;
274 s->state=SSL3_ST_SW_CHANGE_A;
276 s->state=SSL3_ST_SW_CERT_A;
280 case SSL3_ST_SW_CERT_A:
281 case SSL3_ST_SW_CERT_B:
282 /* Check if it is anon DH */
283 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
285 ret=ssl3_send_server_certificate(s);
286 if (ret <= 0) goto end;
290 s->state=SSL3_ST_SW_KEY_EXCH_A;
294 case SSL3_ST_SW_KEY_EXCH_A:
295 case SSL3_ST_SW_KEY_EXCH_B:
296 l=s->s3->tmp.new_cipher->algorithms;
297 if (s->session->cert == NULL)
301 CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
302 s->session->cert=s->cert;
306 CRYPTO_add(&s->ctx->default_cert->references,1,CRYPTO_LOCK_SSL_CERT);
307 s->session->cert=s->ctx->default_cert;
312 /* clear this, it may get reset by
313 * send_server_key_exchange */
314 if (s->options & SSL_OP_EPHEMERAL_RSA)
315 s->s3->tmp.use_rsa_tmp=1;
317 s->s3->tmp.use_rsa_tmp=0;
319 /* only send if a DH key exchange, fortezza or
320 * RSA but we have a sign only certificate */
321 if ( s->s3->tmp.use_rsa_tmp ||
322 (l & (SSL_DH|SSL_kFZA)) ||
324 ((ct->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)||
326 (EVP_PKEY_size(ct->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > 512)
332 ret=ssl3_send_server_key_exchange(s);
333 if (ret <= 0) goto end;
338 s->state=SSL3_ST_SW_CERT_REQ_A;
342 case SSL3_ST_SW_CERT_REQ_A:
343 case SSL3_ST_SW_CERT_REQ_B:
344 if (!(s->verify_mode & SSL_VERIFY_PEER) ||
345 ((s->session->peer != NULL) &&
346 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
348 /* no cert request */
350 s->s3->tmp.cert_request=0;
351 s->state=SSL3_ST_SW_SRVR_DONE_A;
355 s->s3->tmp.cert_request=1;
356 ret=ssl3_send_certificate_request(s);
357 if (ret <= 0) goto end;
358 s->state=SSL3_ST_SW_SRVR_DONE_A;
363 case SSL3_ST_SW_SRVR_DONE_A:
364 case SSL3_ST_SW_SRVR_DONE_B:
365 ret=ssl3_send_server_done(s);
366 if (ret <= 0) goto end;
367 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
368 s->state=SSL3_ST_SW_FLUSH;
372 case SSL3_ST_SW_FLUSH:
373 /* number of bytes to be flushed */
374 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
377 s->rwstate=SSL_WRITING;
378 num1=BIO_flush(s->wbio);
379 if (num1 <= 0) { ret= -1; goto end; }
380 s->rwstate=SSL_NOTHING;
383 s->state=s->s3->tmp.next_state;
386 case SSL3_ST_SR_CERT_A:
387 case SSL3_ST_SR_CERT_B:
388 /* could be sent for a DH cert, even if we
389 * have not asked for it :-) */
390 ret=ssl3_get_client_certificate(s);
391 if (ret <= 0) goto end;
393 s->state=SSL3_ST_SR_KEY_EXCH_A;
396 case SSL3_ST_SR_KEY_EXCH_A:
397 case SSL3_ST_SR_KEY_EXCH_B:
398 ret=ssl3_get_client_key_exchange(s);
399 if (ret <= 0) goto end;
400 s->state=SSL3_ST_SR_CERT_VRFY_A;
403 /* We need to get hashes here so if there is
404 * a client cert, it can be verified */
405 s->method->ssl3_enc->cert_verify_mac(s,
406 &(s->s3->finish_dgst1),
407 &(s->s3->tmp.finish_md[0]));
408 s->method->ssl3_enc->cert_verify_mac(s,
409 &(s->s3->finish_dgst2),
410 &(s->s3->tmp.finish_md[MD5_DIGEST_LENGTH]));
414 case SSL3_ST_SR_CERT_VRFY_A:
415 case SSL3_ST_SR_CERT_VRFY_B:
417 /* we should decide if we expected this one */
418 ret=ssl3_get_cert_verify(s);
419 if (ret <= 0) goto end;
421 s->state=SSL3_ST_SR_FINISHED_A;
425 case SSL3_ST_SR_FINISHED_A:
426 case SSL3_ST_SR_FINISHED_B:
427 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
428 SSL3_ST_SR_FINISHED_B);
429 if (ret <= 0) goto end;
433 s->state=SSL3_ST_SW_CHANGE_A;
437 case SSL3_ST_SW_CHANGE_A:
438 case SSL3_ST_SW_CHANGE_B:
440 s->session->cipher=s->s3->tmp.new_cipher;
441 if (!s->method->ssl3_enc->setup_key_block(s))
442 { ret= -1; goto end; }
444 ret=ssl3_send_change_cipher_spec(s,
445 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
447 if (ret <= 0) goto end;
448 s->state=SSL3_ST_SW_FINISHED_A;
451 if (!s->method->ssl3_enc->change_cipher_state(s,
452 SSL3_CHANGE_CIPHER_SERVER_WRITE))
460 case SSL3_ST_SW_FINISHED_A:
461 case SSL3_ST_SW_FINISHED_B:
462 ret=ssl3_send_finished(s,
463 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
464 s->method->ssl3_enc->server_finished,
465 s->method->ssl3_enc->server_finished_len);
466 if (ret <= 0) goto end;
467 s->state=SSL3_ST_SW_FLUSH;
469 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
471 s->s3->tmp.next_state=SSL_ST_OK;
476 /* clean a few things up */
477 ssl3_cleanup_key_block(s);
479 BUF_MEM_free(s->init_buf);
482 /* remove buffering on output */
483 under=BIO_pop(s->wbio);
494 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
496 s->ctx->sess_accept_good++;
498 s->handshake_func=ssl3_accept;
501 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
507 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
513 if (!s->s3->tmp.reuse_message && !skip)
517 if ((ret=BIO_flush(s->wbio)) <= 0)
522 if ((cb != NULL) && (s->state != state))
526 cb(s,SSL_CB_ACCEPT_LOOP,1);
533 /* BIO_flush(s->wbio); */
536 cb(s,SSL_CB_ACCEPT_EXIT,ret);
541 static int ssl3_send_hello_request(s)
546 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
548 p=(unsigned char *)s->init_buf->data;
549 *(p++)=SSL3_MT_CLIENT_REQUEST;
554 s->state=SSL3_ST_SW_HELLO_REQ_B;
555 /* number of bytes to write */
560 /* SSL3_ST_SW_HELLO_REQ_B */
561 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
564 static int ssl3_get_client_hello(s)
567 int i,j,ok,al,ret= -1;
574 /* We do this so that we will respond with our native type.
575 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
576 * This down switching should be handled by a different method.
577 * If we are SSLv3, we will respond with SSLv3, even if prompted with
580 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
583 s->state=SSL3_ST_SR_CLNT_HELLO_B;
585 n=ssl3_get_message(s,
586 SSL3_ST_SR_CLNT_HELLO_B,
587 SSL3_ST_SR_CLNT_HELLO_C,
588 SSL3_MT_CLIENT_HELLO,
589 SSL3_RT_MAX_PLAIN_LENGTH,
592 if (!ok) return((int)n);
593 d=p=(unsigned char *)s->init_buf->data;
595 /* The version number has already been checked in ssl3_get_message.
596 * I a native TLSv1/SSLv3 method, the match must be correct except
597 * perhaps for the first message */
600 /* load the client random */
601 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
604 /* get the session-id */
610 if (!ssl_get_new_session(s,1))
615 i=ssl_get_prev_session(s,p,j);
617 { /* previous session */
622 if (!ssl_get_new_session(s,1))
629 if ((i == 0) && (j != 0))
631 /* we need a cipher if we are not resuming a session */
632 al=SSL_AD_ILLEGAL_PARAMETER;
633 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
638 /* not enough data */
639 al=SSL_AD_DECODE_ERROR;
640 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
643 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
650 /* If it is a hit, check that the cipher is in the list */
651 if ((s->hit) && (i > 0))
654 id=s->session->cipher->id;
656 for (i=0; i<sk_num(ciphers); i++)
658 c=(SSL_CIPHER *)sk_value(ciphers,i);
667 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_num(ciphers) == 1))
669 /* Very bad for multi-threading.... */
671 (SSL_CIPHER *)sk_value(ciphers,0);
675 /* we need to have the cipher in the cipher
676 * list if we are asked to reuse it */
677 al=SSL_AD_ILLEGAL_PARAMETER;
678 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
687 if (p[j] == 0) break;
693 al=SSL_AD_DECODE_ERROR;
694 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
698 /* TLS does not mind if there is extra stuff */
699 if (s->version == SSL3_VERSION)
703 /* wrong number of bytes,
704 * there could be more to follow */
705 al=SSL_AD_DECODE_ERROR;
706 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
711 /* do nothing with compression */
713 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
718 if (s->session->ciphers != NULL)
719 sk_free(s->session->ciphers);
720 s->session->ciphers=ciphers;
723 al=SSL_AD_ILLEGAL_PARAMETER;
724 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
728 c=ssl3_choose_cipher(s,s->session->ciphers,
729 ssl_get_ciphers_by_id(s));
733 al=SSL_AD_HANDSHAKE_FAILURE;
734 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
737 s->s3->tmp.new_cipher=c;
741 /* Session-id reuse */
742 #ifdef REUSE_CIPHER_BUG
747 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
749 sk=s->session->ciphers;
750 for (i=0; i<sk_num(sk); i++)
752 c=(SSL_CIPHER *)sk_value(sk,i);
753 if (c->algorithms & SSL_eNULL)
755 if (c->algorithms & SSL_EXP)
759 s->s3->tmp.new_cipher=nc;
761 s->s3->tmp.new_cipher=ec;
763 s->s3->tmp.new_cipher=s->session->cipher;
767 s->s3->tmp.new_cipher=s->session->cipher;
770 /* we now have the following setup.
772 * cipher_list - our prefered list of ciphers
773 * ciphers - the clients prefered list of ciphers
774 * compression - basically ignored right now
775 * ssl version is set - sslv3
776 * s->session - The ssl session has been setup.
777 * s->hit - sesson reuse flag
778 * s->tmp.new_cipher - the new cipher to use.
785 ssl3_send_alert(s,SSL3_AL_FATAL,al);
788 if (ciphers != NULL) sk_free(ciphers);
792 static int ssl3_send_server_hello(s)
798 unsigned long l,Time;
800 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
802 buf=(unsigned char *)s->init_buf->data;
803 p=s->s3->server_random;
804 Time=time(NULL); /* Time */
806 RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
807 /* Do the message type and length last */
810 *(p++)=s->version>>8;
811 *(p++)=s->version&0xff;
814 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
817 /* now in theory we have 3 options to sending back the
818 * session id. If it is a re-use, we send back the
819 * old session-id, if it is a new session, we send
820 * back the new session-id or we send back a 0 length
821 * session-id if we want it to be single use.
822 * Currently I will not implement the '0' length session-id
823 * 12-Jan-98 - I'll now support the '0' length stuff.
825 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
826 s->session->session_id_length=0;
828 sl=s->session->session_id_length;
830 memcpy(p,s->session->session_id,sl);
834 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
837 /* put the compression method */
843 *(d++)=SSL3_MT_SERVER_HELLO;
846 s->state=SSL3_ST_CW_CLNT_HELLO_B;
847 /* number of bytes to write */
852 /* SSL3_ST_CW_CLNT_HELLO_B */
853 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
856 static int ssl3_send_server_done(s)
861 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
863 p=(unsigned char *)s->init_buf->data;
866 *(p++)=SSL3_MT_SERVER_DONE;
871 s->state=SSL3_ST_SW_SRVR_DONE_B;
872 /* number of bytes to write */
877 /* SSL3_ST_CW_CLNT_HELLO_B */
878 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
881 static int ssl3_send_server_key_exchange(s)
888 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
904 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
906 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
907 cert=s->session->cert;
911 r[0]=r[1]=r[2]=r[3]=NULL;
917 if ((rsa == NULL) && (s->ctx->default_cert->rsa_tmp_cb != NULL))
919 rsa=s->ctx->default_cert->rsa_tmp_cb(s,
920 !(s->s3->tmp.new_cipher->algorithms
922 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
927 al=SSL_AD_HANDSHAKE_FAILURE;
928 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
933 s->s3->tmp.use_rsa_tmp=1;
941 if ((dhp == NULL) && (cert->dh_tmp_cb != NULL))
942 dhp=cert->dh_tmp_cb(s,
943 !(s->s3->tmp.new_cipher->algorithms
947 al=SSL_AD_HANDSHAKE_FAILURE;
948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
951 if ((dh=DHparams_dup(dhp)) == NULL)
953 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
958 if ((dhp->pub_key == NULL ||
959 dhp->priv_key == NULL ||
960 (s->options & SSL_OP_SINGLE_DH_USE)))
962 if(!DH_generate_key(dh))
964 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
971 dh->pub_key=BN_dup(dhp->pub_key);
972 dh->priv_key=BN_dup(dhp->priv_key);
973 if ((dh->pub_key == NULL) ||
974 (dh->priv_key == NULL))
976 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
987 al=SSL_AD_HANDSHAKE_FAILURE;
988 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
991 for (i=0; r[i] != NULL; i++)
993 nr[i]=BN_num_bytes(r[i]);
997 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
999 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1002 al=SSL_AD_DECODE_ERROR;
1005 kn=EVP_PKEY_size(pkey);
1013 if (!BUF_MEM_grow(buf,n+4+kn))
1015 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1018 d=(unsigned char *)s->init_buf->data;
1021 for (i=0; r[i] != NULL; i++)
1031 /* n is the length of the params, they start at &(d[4])
1032 * and p points to the space at the end. */
1034 if (pkey->type == EVP_PKEY_RSA)
1038 for (num=2; num > 0; num--)
1040 EVP_DigestInit(&md_ctx,(num == 2)
1041 ?s->ctx->md5:s->ctx->sha1);
1042 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1043 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1044 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1045 EVP_DigestFinal(&md_ctx,q,
1046 (unsigned int *)&i);
1050 i=RSA_private_encrypt(j,md_buf,&(p[2]),
1051 pkey->pkey.rsa,RSA_PKCS1_PADDING);
1054 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1062 #if !defined(NO_DSA)
1063 if (pkey->type == EVP_PKEY_DSA)
1066 EVP_SignInit(&md_ctx,EVP_dss1());
1067 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1068 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1069 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1070 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1071 (unsigned int *)&i,pkey))
1073 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1082 /* Is this error check actually needed? */
1083 al=SSL_AD_HANDSHAKE_FAILURE;
1084 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1089 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1092 /* we should now have things packed up, so lets send
1098 /* SSL3_ST_SW_KEY_EXCH_B */
1099 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1101 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1106 static int ssl3_send_certificate_request(s)
1109 unsigned char *p,*d;
1115 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1119 d=p=(unsigned char *)&(buf->data[4]);
1121 /* get the list of acceptable cert types */
1123 n=ssl3_get_req_cert_type(s,p);
1132 sk=SSL_get_client_CA_list(s);
1136 for (i=0; i<sk_num(sk); i++)
1138 name=(X509_NAME *)sk_value(sk,i);
1139 j=i2d_X509_NAME(name,NULL);
1140 if (!BUF_MEM_grow(buf,4+n+j+2))
1142 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1145 p=(unsigned char *)&(buf->data[4+n]);
1146 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1149 i2d_X509_NAME(name,&p);
1156 i2d_X509_NAME(name,&p);
1157 j-=2; s2n(j,d); j+=2;
1163 /* else no CA names */
1164 p=(unsigned char *)&(buf->data[4+off]);
1167 d=(unsigned char *)buf->data;
1168 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1171 /* we should now have things packed up, so lets send
1178 /* SSL3_ST_SW_CERT_REQ_B */
1179 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1184 static int ssl3_get_client_key_exchange(s)
1192 EVP_PKEY *pkey=NULL;
1198 n=ssl3_get_message(s,
1199 SSL3_ST_SR_KEY_EXCH_A,
1200 SSL3_ST_SR_KEY_EXCH_B,
1201 SSL3_MT_CLIENT_KEY_EXCHANGE,
1205 if (!ok) return((int)n);
1206 p=(unsigned char *)s->init_buf->data;
1208 l=s->s3->tmp.new_cipher->algorithms;
1213 /* FIX THIS UP EAY EAY EAY EAY */
1214 if (s->s3->tmp.use_rsa_tmp)
1216 if ((s->session->cert != NULL) &&
1217 (s->session->cert->rsa_tmp != NULL))
1218 rsa=s->session->cert->rsa_tmp;
1219 else if ((s->ctx->default_cert != NULL) &&
1220 (s->ctx->default_cert->rsa_tmp != NULL))
1221 rsa=s->ctx->default_cert->rsa_tmp;
1222 /* Don't do a callback because rsa_tmp should
1223 * be sent already */
1226 al=SSL_AD_HANDSHAKE_FAILURE;
1227 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1234 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1235 if ( (pkey == NULL) ||
1236 (pkey->type != EVP_PKEY_RSA) ||
1237 (pkey->pkey.rsa == NULL))
1239 al=SSL_AD_HANDSHAKE_FAILURE;
1240 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1247 if (s->version > SSL3_VERSION)
1252 if (!(s->options & SSL_OP_TLS_D5_BUG))
1254 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1264 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1267 /* If a bad decrypt, use a random master key */
1268 if ((i != SSL_MAX_MASTER_KEY_LENGTH) ||
1269 ((p[0] != (s->version>>8)) ||
1270 (p[1] != (s->version & 0xff))))
1272 p[0]=(s->version>>8);
1273 p[1]=(s->version & 0xff);
1274 RAND_bytes(&(p[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1275 i=SSL_MAX_MASTER_KEY_LENGTH;
1278 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1280 al=SSL_AD_DECODE_ERROR;
1281 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1285 if ((p[0] != (s->version>>8)) || (p[1] != (s->version & 0xff)))
1287 al=SSL_AD_DECODE_ERROR;
1288 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1293 s->session->master_key_length=
1294 s->method->ssl3_enc->generate_master_secret(s,
1295 s->session->master_key,
1302 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1307 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1309 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1319 if (n == 0L) /* the parameters are in the cert */
1321 al=SSL_AD_HANDSHAKE_FAILURE;
1322 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1327 if (s->s3->tmp.dh == NULL)
1329 al=SSL_AD_HANDSHAKE_FAILURE;
1330 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1334 dh_srvr=s->s3->tmp.dh;
1337 pub=BN_bin2bn(p,i,NULL);
1340 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1344 i=DH_compute_key(p,pub,dh_srvr);
1348 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1352 DH_free(s->s3->tmp.dh);
1357 s->session->master_key_length=
1358 s->method->ssl3_enc->generate_master_secret(s,
1359 s->session->master_key,p,i);
1364 al=SSL_AD_HANDSHAKE_FAILURE;
1365 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
1371 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1372 #if !defined(NO_DH) || !defined(NO_RSA)
1378 static int ssl3_get_cert_verify(s)
1381 EVP_PKEY *pkey=NULL;
1388 n=ssl3_get_message(s,
1389 SSL3_ST_SR_CERT_VRFY_A,
1390 SSL3_ST_SR_CERT_VRFY_B,
1395 if (!ok) return((int)n);
1397 if (s->session->peer != NULL)
1399 peer=s->session->peer;
1400 pkey=X509_get_pubkey(peer);
1401 type=X509_certificate_type(peer,pkey);
1409 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1411 s->s3->tmp.reuse_message=1;
1412 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1414 al=SSL_AD_UNEXPECTED_MESSAGE;
1415 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1424 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1425 al=SSL_AD_UNEXPECTED_MESSAGE;
1429 if (!(type & EVP_PKT_SIGN))
1431 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1432 al=SSL_AD_ILLEGAL_PARAMETER;
1436 if (s->s3->change_cipher_spec)
1438 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1439 al=SSL_AD_UNEXPECTED_MESSAGE;
1443 /* we now have a signature that we need to verify */
1444 p=(unsigned char *)s->init_buf->data;
1449 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1450 al=SSL_AD_DECODE_ERROR;
1454 j=EVP_PKEY_size(pkey);
1455 if ((i > j) || (n > j) || (n <= 0))
1457 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1458 al=SSL_AD_DECODE_ERROR;
1463 if (pkey->type == EVP_PKEY_RSA)
1465 i=RSA_public_decrypt(i,p,p,pkey->pkey.rsa,RSA_PKCS1_PADDING);
1468 al=SSL_AD_DECRYPT_ERROR;
1469 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1472 if ((i != (MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH)) ||
1473 memcmp(&(s->s3->tmp.finish_md[0]),p,
1474 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH))
1476 al=SSL_AD_DECRYPT_ERROR;
1477 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1484 if (pkey->type == EVP_PKEY_DSA)
1486 j=DSA_verify(pkey->save_type,
1487 &(s->s3->tmp.finish_md[MD5_DIGEST_LENGTH]),
1488 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1492 al=SSL_AD_DECRYPT_ERROR;
1493 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1500 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
1501 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1510 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1513 EVP_PKEY_free(pkey);
1517 static int ssl3_get_client_certificate(s)
1520 int i,ok,al,ret= -1;
1522 unsigned long l,nc,llen,n;
1523 unsigned char *p,*d,*q;
1526 n=ssl3_get_message(s,
1530 #if defined(MSDOS) && !defined(WIN32)
1531 1024*30, /* 30k max cert list :-) */
1533 1024*100, /* 100k max cert list :-) */
1537 if (!ok) return((int)n);
1539 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1541 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1542 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1544 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1545 al=SSL_AD_HANDSHAKE_FAILURE;
1548 /* If tls asked for a client cert we must return a 0 list */
1549 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1551 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1552 al=SSL_AD_UNEXPECTED_MESSAGE;
1555 s->s3->tmp.reuse_message=1;
1559 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1561 al=SSL_AD_UNEXPECTED_MESSAGE;
1562 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1565 d=p=(unsigned char *)s->init_buf->data;
1567 if ((sk=sk_new_null()) == NULL)
1569 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1576 al=SSL_AD_DECODE_ERROR;
1577 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1580 for (nc=0; nc<llen; )
1583 if ((l+nc+3) > llen)
1585 al=SSL_AD_DECODE_ERROR;
1586 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1591 x=d2i_X509(NULL,&p,l);
1594 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1599 al=SSL_AD_DECODE_ERROR;
1600 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1603 if (!sk_push(sk,(char *)x))
1605 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1612 if (sk_num(sk) <= 0)
1614 /* TLS does not mind 0 certs returned */
1615 if (s->version == SSL3_VERSION)
1617 al=SSL_AD_HANDSHAKE_FAILURE;
1618 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1621 /* Fail for TLS only if we required a certificate */
1622 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1623 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1625 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1626 al=SSL_AD_HANDSHAKE_FAILURE;
1632 i=ssl_verify_cert_chain(s,sk);
1635 al=ssl_verify_alarm_type(s->verify_result);
1636 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1641 /* This should not be needed */
1642 if (s->session->peer != NULL)
1643 X509_free(s->session->peer);
1644 s->session->peer=(X509 *)sk_shift(sk);
1650 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1653 if (x != NULL) X509_free(x);
1654 if (sk != NULL) sk_pop_free(sk,X509_free);
1658 int ssl3_send_server_certificate(s)
1664 if (s->state == SSL3_ST_SW_CERT_A)
1666 x=ssl_get_server_send_cert(s);
1669 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
1673 l=ssl3_output_cert_chain(s,x);
1674 s->state=SSL3_ST_SW_CERT_B;
1679 /* SSL3_ST_SW_CERT_B */
1680 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));