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
60 #define NETSCAPE_HANG_BUG
64 #include <openssl/buffer.h>
65 #include <openssl/rand.h>
66 #include <openssl/objects.h>
67 #include <openssl/md5.h>
68 #include <openssl/sha.h>
69 #include <openssl/evp.h>
70 #include <openssl/x509.h>
73 static SSL_METHOD *ssl3_get_server_method(int ver);
74 static int ssl3_get_client_hello(SSL *s);
75 static int ssl3_check_client_hello(SSL *s);
76 static int ssl3_send_server_hello(SSL *s);
77 static int ssl3_send_server_key_exchange(SSL *s);
78 static int ssl3_send_certificate_request(SSL *s);
79 static int ssl3_send_server_done(SSL *s);
80 static int ssl3_get_client_key_exchange(SSL *s);
81 static int ssl3_get_client_certificate(SSL *s);
82 static int ssl3_get_cert_verify(SSL *s);
83 static int ssl3_send_hello_request(SSL *s);
85 static SSL_METHOD *ssl3_get_server_method(int ver)
87 if (ver == SSL3_VERSION)
88 return(SSLv3_server_method());
93 SSL_METHOD *SSLv3_server_method(void)
96 static SSL_METHOD SSLv3_server_data;
100 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
102 SSLv3_server_data.ssl_accept=ssl3_accept;
103 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
106 return(&SSLv3_server_data);
109 int ssl3_accept(SSL *s)
112 unsigned long l,Time=time(NULL);
116 int new_state,state,skip=0;
118 RAND_add(&Time,sizeof(Time),0);
122 if (s->info_callback != NULL)
124 else if (s->ctx->info_callback != NULL)
125 cb=s->ctx->info_callback;
127 /* init things to blank */
128 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
133 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
143 case SSL_ST_RENEGOTIATE:
145 /* s->state=SSL_ST_ACCEPT; */
149 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
150 case SSL_ST_OK|SSL_ST_ACCEPT:
153 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
155 if ((s->version>>8) != 3)
157 s->type=SSL_ST_ACCEPT;
159 if (s->init_buf == NULL)
161 if ((buf=BUF_MEM_new()) == NULL)
166 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
174 if (!ssl3_setup_buffers(s))
180 /* Ok, we now need to push on a buffering BIO so that
181 * the output is sent in a way that TCP likes :-)
183 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
187 if (s->state != SSL_ST_RENEGOTIATE)
189 ssl3_init_finished_mac(s);
190 s->state=SSL3_ST_SR_CLNT_HELLO_A;
191 s->ctx->stats.sess_accept++;
195 s->ctx->stats.sess_accept_renegotiate++;
196 s->state=SSL3_ST_SW_HELLO_REQ_A;
200 case SSL3_ST_SW_HELLO_REQ_A:
201 case SSL3_ST_SW_HELLO_REQ_B:
204 ret=ssl3_send_hello_request(s);
205 if (ret <= 0) goto end;
206 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
207 s->state=SSL3_ST_SW_FLUSH;
210 ssl3_init_finished_mac(s);
213 case SSL3_ST_SW_HELLO_REQ_C:
219 case SSL3_ST_SR_CLNT_HELLO_A:
220 case SSL3_ST_SR_CLNT_HELLO_B:
221 case SSL3_ST_SR_CLNT_HELLO_C:
224 ret=ssl3_get_client_hello(s);
225 if (ret <= 0) goto end;
226 s->state=SSL3_ST_SW_SRVR_HELLO_A;
230 case SSL3_ST_SW_SRVR_HELLO_A:
231 case SSL3_ST_SW_SRVR_HELLO_B:
232 ret=ssl3_send_server_hello(s);
233 if (ret <= 0) goto end;
236 s->state=SSL3_ST_SW_CHANGE_A;
238 s->state=SSL3_ST_SW_CERT_A;
242 case SSL3_ST_SW_CERT_A:
243 case SSL3_ST_SW_CERT_B:
244 /* Check if it is anon DH */
245 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
247 ret=ssl3_send_server_certificate(s);
248 if (ret <= 0) goto end;
252 s->state=SSL3_ST_SW_KEY_EXCH_A;
256 case SSL3_ST_SW_KEY_EXCH_A:
257 case SSL3_ST_SW_KEY_EXCH_B:
258 l=s->s3->tmp.new_cipher->algorithms;
260 /* clear this, it may get reset by
261 * send_server_key_exchange */
262 if (s->options & SSL_OP_EPHEMERAL_RSA)
263 s->s3->tmp.use_rsa_tmp=1;
265 s->s3->tmp.use_rsa_tmp=0;
267 /* only send if a DH key exchange, fortezza or
268 * RSA but we have a sign only certificate */
269 if (s->s3->tmp.use_rsa_tmp
270 || (l & (SSL_DH|SSL_kFZA))
272 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
273 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
274 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
280 ret=ssl3_send_server_key_exchange(s);
281 if (ret <= 0) goto end;
286 s->state=SSL3_ST_SW_CERT_REQ_A;
290 case SSL3_ST_SW_CERT_REQ_A:
291 case SSL3_ST_SW_CERT_REQ_B:
292 if (/* don't request cert unless asked for it: */
293 !(s->verify_mode & SSL_VERIFY_PEER) ||
294 /* if SSL_VERIFY_CLIENT_ONCE is set,
295 * don't request cert during re-negotiation: */
296 ((s->session->peer != NULL) &&
297 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
298 /* never request cert in anonymous ciphersuites
299 * (see section "Certificate request" in SSL 3 drafts
300 * and in RFC 2246): */
301 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
302 /* ... except when the application insists on verification
303 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
304 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
306 /* no cert request */
308 s->s3->tmp.cert_request=0;
309 s->state=SSL3_ST_SW_SRVR_DONE_A;
313 s->s3->tmp.cert_request=1;
314 ret=ssl3_send_certificate_request(s);
315 if (ret <= 0) goto end;
316 #ifndef NETSCAPE_HANG_BUG
317 s->state=SSL3_ST_SW_SRVR_DONE_A;
319 s->state=SSL3_ST_SW_FLUSH;
320 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
326 case SSL3_ST_SW_SRVR_DONE_A:
327 case SSL3_ST_SW_SRVR_DONE_B:
328 ret=ssl3_send_server_done(s);
329 if (ret <= 0) goto end;
330 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
331 s->state=SSL3_ST_SW_FLUSH;
335 case SSL3_ST_SW_FLUSH:
336 /* number of bytes to be flushed */
337 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
340 s->rwstate=SSL_WRITING;
341 num1=BIO_flush(s->wbio);
342 if (num1 <= 0) { ret= -1; goto end; }
343 s->rwstate=SSL_NOTHING;
346 s->state=s->s3->tmp.next_state;
349 case SSL3_ST_SR_CERT_A:
350 case SSL3_ST_SR_CERT_B:
351 /* Check for second client hello (MS SGC) */
352 ret = ssl3_check_client_hello(s);
356 s->state = SSL3_ST_SR_CLNT_HELLO_C;
358 /* could be sent for a DH cert, even if we
359 * have not asked for it :-) */
360 ret=ssl3_get_client_certificate(s);
361 if (ret <= 0) goto end;
363 s->state=SSL3_ST_SR_KEY_EXCH_A;
367 case SSL3_ST_SR_KEY_EXCH_A:
368 case SSL3_ST_SR_KEY_EXCH_B:
369 ret=ssl3_get_client_key_exchange(s);
370 if (ret <= 0) goto end;
371 s->state=SSL3_ST_SR_CERT_VRFY_A;
374 /* We need to get hashes here so if there is
375 * a client cert, it can be verified */
376 s->method->ssl3_enc->cert_verify_mac(s,
377 &(s->s3->finish_dgst1),
378 &(s->s3->tmp.cert_verify_md[0]));
379 s->method->ssl3_enc->cert_verify_mac(s,
380 &(s->s3->finish_dgst2),
381 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
385 case SSL3_ST_SR_CERT_VRFY_A:
386 case SSL3_ST_SR_CERT_VRFY_B:
388 /* we should decide if we expected this one */
389 ret=ssl3_get_cert_verify(s);
390 if (ret <= 0) goto end;
392 s->state=SSL3_ST_SR_FINISHED_A;
396 case SSL3_ST_SR_FINISHED_A:
397 case SSL3_ST_SR_FINISHED_B:
398 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
399 SSL3_ST_SR_FINISHED_B);
400 if (ret <= 0) goto end;
404 s->state=SSL3_ST_SW_CHANGE_A;
408 case SSL3_ST_SW_CHANGE_A:
409 case SSL3_ST_SW_CHANGE_B:
411 s->session->cipher=s->s3->tmp.new_cipher;
412 if (!s->method->ssl3_enc->setup_key_block(s))
413 { ret= -1; goto end; }
415 ret=ssl3_send_change_cipher_spec(s,
416 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
418 if (ret <= 0) goto end;
419 s->state=SSL3_ST_SW_FINISHED_A;
422 if (!s->method->ssl3_enc->change_cipher_state(s,
423 SSL3_CHANGE_CIPHER_SERVER_WRITE))
431 case SSL3_ST_SW_FINISHED_A:
432 case SSL3_ST_SW_FINISHED_B:
433 ret=ssl3_send_finished(s,
434 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
435 s->method->ssl3_enc->server_finished_label,
436 s->method->ssl3_enc->server_finished_label_len);
437 if (ret <= 0) goto end;
438 s->state=SSL3_ST_SW_FLUSH;
440 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
442 s->s3->tmp.next_state=SSL_ST_OK;
447 /* clean a few things up */
448 ssl3_cleanup_key_block(s);
450 BUF_MEM_free(s->init_buf);
453 /* remove buffering on output */
454 ssl_free_wbio_buffer(s);
459 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
461 s->ctx->stats.sess_accept_good++;
463 s->handshake_func=ssl3_accept;
466 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
472 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
478 if (!s->s3->tmp.reuse_message && !skip)
482 if ((ret=BIO_flush(s->wbio)) <= 0)
487 if ((cb != NULL) && (s->state != state))
491 cb(s,SSL_CB_ACCEPT_LOOP,1);
498 /* BIO_flush(s->wbio); */
501 cb(s,SSL_CB_ACCEPT_EXIT,ret);
506 static int ssl3_send_hello_request(SSL *s)
510 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
512 p=(unsigned char *)s->init_buf->data;
513 *(p++)=SSL3_MT_HELLO_REQUEST;
518 s->state=SSL3_ST_SW_HELLO_REQ_B;
519 /* number of bytes to write */
524 /* SSL3_ST_SW_HELLO_REQ_B */
525 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
528 static int ssl3_check_client_hello(SSL *s)
533 n=ssl3_get_message(s,
537 SSL3_RT_MAX_PLAIN_LENGTH,
539 if (!ok) return((int)n);
540 s->s3->tmp.reuse_message = 1;
541 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
543 /* Throw away what we have done so far in the current handshake,
544 * which will now be aborted. (A full SSL_clear would be too much.)
545 * I hope that tmp.dh is the only thing that may need to be cleared
546 * when a handshake is not completed ... */
548 if (s->s3->tmp.dh != NULL)
550 DH_free(s->s3->tmp.dh);
551 s->s3->tmp.dh = NULL;
559 static int ssl3_get_client_hello(SSL *s)
561 int i,j,ok,al,ret= -1;
564 unsigned char *p,*d,*q;
567 STACK_OF(SSL_CIPHER) *ciphers=NULL;
569 /* We do this so that we will respond with our native type.
570 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
571 * This down switching should be handled by a different method.
572 * If we are SSLv3, we will respond with SSLv3, even if prompted with
575 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
578 s->state=SSL3_ST_SR_CLNT_HELLO_B;
580 n=ssl3_get_message(s,
581 SSL3_ST_SR_CLNT_HELLO_B,
582 SSL3_ST_SR_CLNT_HELLO_C,
583 SSL3_MT_CLIENT_HELLO,
584 SSL3_RT_MAX_PLAIN_LENGTH,
587 if (!ok) return((int)n);
588 d=p=(unsigned char *)s->init_buf->data;
590 /* use version from inside client hello, not from record header
591 * (may differ: see RFC 2246, Appendix E, second paragraph) */
592 s->client_version=(((int)p[0])<<8)|(int)p[1];
595 /* load the client random */
596 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
599 /* get the session-id */
605 if (!ssl_get_new_session(s,1))
610 i=ssl_get_prev_session(s,p,j);
612 { /* previous session */
619 if (!ssl_get_new_session(s,1))
626 if ((i == 0) && (j != 0))
628 /* we need a cipher if we are not resuming a session */
629 al=SSL_AD_ILLEGAL_PARAMETER;
630 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
635 /* not enough data */
636 al=SSL_AD_DECODE_ERROR;
637 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
640 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
647 /* If it is a hit, check that the cipher is in the list */
648 if ((s->hit) && (i > 0))
651 id=s->session->cipher->id;
654 printf("client sent %d ciphers\n",sk_num(ciphers));
656 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
658 c=sk_SSL_CIPHER_value(ciphers,i);
660 printf("client [%2d of %2d]:%s\n",
661 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
671 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
673 /* Very bad for multi-threading.... */
674 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
679 /* we need to have the cipher in the cipher
680 * list if we are asked to reuse it */
681 al=SSL_AD_ILLEGAL_PARAMETER;
682 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
693 if (p[j] == 0) break;
700 al=SSL_AD_DECODE_ERROR;
701 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
705 /* Worst case, we will use the NULL compression, but if we have other
706 * options, we will now look for them. We have i-1 compression
707 * algorithms from the client, starting at q. */
708 s->s3->tmp.new_compression=NULL;
709 if (s->ctx->comp_methods != NULL)
710 { /* See if we have a match */
713 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
716 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
729 s->s3->tmp.new_compression=comp;
734 /* TLS does not mind if there is extra stuff */
735 if (s->version == SSL3_VERSION)
739 /* wrong number of bytes,
740 * there could be more to follow */
741 al=SSL_AD_DECODE_ERROR;
742 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
747 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
752 s->session->compress_meth=(comp == NULL)?0:comp->id;
753 if (s->session->ciphers != NULL)
754 sk_SSL_CIPHER_free(s->session->ciphers);
755 s->session->ciphers=ciphers;
758 al=SSL_AD_ILLEGAL_PARAMETER;
759 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
763 c=ssl3_choose_cipher(s,s->session->ciphers,
764 ssl_get_ciphers_by_id(s));
768 al=SSL_AD_HANDSHAKE_FAILURE;
769 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
772 s->s3->tmp.new_cipher=c;
776 /* Session-id reuse */
777 #ifdef REUSE_CIPHER_BUG
778 STACK_OF(SSL_CIPHER) *sk;
782 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
784 sk=s->session->ciphers;
785 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
787 c=sk_SSL_CIPHER_value(sk,i);
788 if (c->algorithms & SSL_eNULL)
790 if (SSL_C_IS_EXPORT(c))
794 s->s3->tmp.new_cipher=nc;
796 s->s3->tmp.new_cipher=ec;
798 s->s3->tmp.new_cipher=s->session->cipher;
802 s->s3->tmp.new_cipher=s->session->cipher;
805 /* we now have the following setup.
807 * cipher_list - our prefered list of ciphers
808 * ciphers - the clients prefered list of ciphers
809 * compression - basically ignored right now
810 * ssl version is set - sslv3
811 * s->session - The ssl session has been setup.
812 * s->hit - session reuse flag
813 * s->tmp.new_cipher - the new cipher to use.
820 ssl3_send_alert(s,SSL3_AL_FATAL,al);
823 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
827 static int ssl3_send_server_hello(SSL *s)
832 unsigned long l,Time;
834 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
836 buf=(unsigned char *)s->init_buf->data;
837 p=s->s3->server_random;
838 Time=time(NULL); /* Time */
840 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
841 /* Do the message type and length last */
844 *(p++)=s->version>>8;
845 *(p++)=s->version&0xff;
848 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
851 /* now in theory we have 3 options to sending back the
852 * session id. If it is a re-use, we send back the
853 * old session-id, if it is a new session, we send
854 * back the new session-id or we send back a 0 length
855 * session-id if we want it to be single use.
856 * Currently I will not implement the '0' length session-id
857 * 12-Jan-98 - I'll now support the '0' length stuff.
859 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
860 s->session->session_id_length=0;
862 sl=s->session->session_id_length;
864 memcpy(p,s->session->session_id,sl);
868 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
871 /* put the compression method */
872 if (s->s3->tmp.new_compression == NULL)
875 *(p++)=s->s3->tmp.new_compression->id;
880 *(d++)=SSL3_MT_SERVER_HELLO;
883 s->state=SSL3_ST_CW_CLNT_HELLO_B;
884 /* number of bytes to write */
889 /* SSL3_ST_CW_CLNT_HELLO_B */
890 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
893 static int ssl3_send_server_done(SSL *s)
897 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
899 p=(unsigned char *)s->init_buf->data;
902 *(p++)=SSL3_MT_SERVER_DONE;
907 s->state=SSL3_ST_SW_SRVR_DONE_B;
908 /* number of bytes to write */
913 /* SSL3_ST_CW_CLNT_HELLO_B */
914 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
917 static int ssl3_send_server_key_exchange(SSL *s)
923 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
940 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
942 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
947 r[0]=r[1]=r[2]=r[3]=NULL;
953 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
955 rsa=s->cert->rsa_tmp_cb(s,
956 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
957 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
958 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
963 al=SSL_AD_HANDSHAKE_FAILURE;
964 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
969 s->s3->tmp.use_rsa_tmp=1;
977 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
978 dhp=s->cert->dh_tmp_cb(s,
979 !SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
980 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
983 al=SSL_AD_HANDSHAKE_FAILURE;
984 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
988 if (s->s3->tmp.dh != NULL)
991 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
995 if ((dh=DHparams_dup(dhp)) == NULL)
997 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1002 if ((dhp->pub_key == NULL ||
1003 dhp->priv_key == NULL ||
1004 (s->options & SSL_OP_SINGLE_DH_USE)))
1006 if(!DH_generate_key(dh))
1008 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1015 dh->pub_key=BN_dup(dhp->pub_key);
1016 dh->priv_key=BN_dup(dhp->priv_key);
1017 if ((dh->pub_key == NULL) ||
1018 (dh->priv_key == NULL))
1020 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1031 al=SSL_AD_HANDSHAKE_FAILURE;
1032 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1035 for (i=0; r[i] != NULL; i++)
1037 nr[i]=BN_num_bytes(r[i]);
1041 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1043 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1046 al=SSL_AD_DECODE_ERROR;
1049 kn=EVP_PKEY_size(pkey);
1057 if (!BUF_MEM_grow(buf,n+4+kn))
1059 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1062 d=(unsigned char *)s->init_buf->data;
1065 for (i=0; r[i] != NULL; i++)
1075 /* n is the length of the params, they start at &(d[4])
1076 * and p points to the space at the end. */
1078 if (pkey->type == EVP_PKEY_RSA)
1082 for (num=2; num > 0; num--)
1084 EVP_DigestInit(&md_ctx,(num == 2)
1085 ?s->ctx->md5:s->ctx->sha1);
1086 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1087 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1088 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1089 EVP_DigestFinal(&md_ctx,q,
1090 (unsigned int *)&i);
1094 if (RSA_sign(NID_md5_sha1, md_buf, j,
1095 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1097 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1105 #if !defined(NO_DSA)
1106 if (pkey->type == EVP_PKEY_DSA)
1109 EVP_SignInit(&md_ctx,EVP_dss1());
1110 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1111 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1112 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1113 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1114 (unsigned int *)&i,pkey))
1116 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1125 /* Is this error check actually needed? */
1126 al=SSL_AD_HANDSHAKE_FAILURE;
1127 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1132 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1135 /* we should now have things packed up, so lets send
1141 s->state = SSL3_ST_SW_KEY_EXCH_B;
1142 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1144 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1149 static int ssl3_send_certificate_request(SSL *s)
1151 unsigned char *p,*d;
1153 STACK_OF(X509_NAME) *sk=NULL;
1157 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1161 d=p=(unsigned char *)&(buf->data[4]);
1163 /* get the list of acceptable cert types */
1165 n=ssl3_get_req_cert_type(s,p);
1174 sk=SSL_get_client_CA_list(s);
1178 for (i=0; i<sk_X509_NAME_num(sk); i++)
1180 name=sk_X509_NAME_value(sk,i);
1181 j=i2d_X509_NAME(name,NULL);
1182 if (!BUF_MEM_grow(buf,4+n+j+2))
1184 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1187 p=(unsigned char *)&(buf->data[4+n]);
1188 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1191 i2d_X509_NAME(name,&p);
1198 i2d_X509_NAME(name,&p);
1199 j-=2; s2n(j,d); j+=2;
1205 /* else no CA names */
1206 p=(unsigned char *)&(buf->data[4+off]);
1209 d=(unsigned char *)buf->data;
1210 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1213 /* we should now have things packed up, so lets send
1218 #ifdef NETSCAPE_HANG_BUG
1219 p=(unsigned char *)s->init_buf->data + s->init_num;
1222 *(p++)=SSL3_MT_SERVER_DONE;
1231 /* SSL3_ST_SW_CERT_REQ_B */
1232 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1237 static int ssl3_get_client_key_exchange(SSL *s)
1245 EVP_PKEY *pkey=NULL;
1252 n=ssl3_get_message(s,
1253 SSL3_ST_SR_KEY_EXCH_A,
1254 SSL3_ST_SR_KEY_EXCH_B,
1255 SSL3_MT_CLIENT_KEY_EXCHANGE,
1259 if (!ok) return((int)n);
1260 p=(unsigned char *)s->init_buf->data;
1262 l=s->s3->tmp.new_cipher->algorithms;
1267 /* FIX THIS UP EAY EAY EAY EAY */
1268 if (s->s3->tmp.use_rsa_tmp)
1270 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1271 rsa=s->cert->rsa_tmp;
1272 /* Don't do a callback because rsa_tmp should
1273 * be sent already */
1276 al=SSL_AD_HANDSHAKE_FAILURE;
1277 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1284 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1285 if ( (pkey == NULL) ||
1286 (pkey->type != EVP_PKEY_RSA) ||
1287 (pkey->pkey.rsa == NULL))
1289 al=SSL_AD_HANDSHAKE_FAILURE;
1290 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1297 if (s->version > SSL3_VERSION)
1302 if (!(s->options & SSL_OP_TLS_D5_BUG))
1304 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1314 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1316 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1318 al=SSL_AD_DECODE_ERROR;
1319 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1323 if ((p[0] != (s->client_version>>8)) || (p[1] != (s->client_version & 0xff)))
1325 al=SSL_AD_DECODE_ERROR;
1326 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1330 s->session->master_key_length=
1331 s->method->ssl3_enc->generate_master_secret(s,
1332 s->session->master_key,
1339 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1344 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1346 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1356 if (n == 0L) /* the parameters are in the cert */
1358 al=SSL_AD_HANDSHAKE_FAILURE;
1359 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1364 if (s->s3->tmp.dh == NULL)
1366 al=SSL_AD_HANDSHAKE_FAILURE;
1367 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1371 dh_srvr=s->s3->tmp.dh;
1374 pub=BN_bin2bn(p,i,NULL);
1377 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1381 i=DH_compute_key(p,pub,dh_srvr);
1385 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1389 DH_free(s->s3->tmp.dh);
1394 s->session->master_key_length=
1395 s->method->ssl3_enc->generate_master_secret(s,
1396 s->session->master_key,p,i);
1401 al=SSL_AD_HANDSHAKE_FAILURE;
1402 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
1408 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1409 #if !defined(NO_DH) || !defined(NO_RSA)
1415 static int ssl3_get_cert_verify(SSL *s)
1417 EVP_PKEY *pkey=NULL;
1424 n=ssl3_get_message(s,
1425 SSL3_ST_SR_CERT_VRFY_A,
1426 SSL3_ST_SR_CERT_VRFY_B,
1431 if (!ok) return((int)n);
1433 if (s->session->peer != NULL)
1435 peer=s->session->peer;
1436 pkey=X509_get_pubkey(peer);
1437 type=X509_certificate_type(peer,pkey);
1445 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1447 s->s3->tmp.reuse_message=1;
1448 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1450 al=SSL_AD_UNEXPECTED_MESSAGE;
1451 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1460 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1461 al=SSL_AD_UNEXPECTED_MESSAGE;
1465 if (!(type & EVP_PKT_SIGN))
1467 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1468 al=SSL_AD_ILLEGAL_PARAMETER;
1472 if (s->s3->change_cipher_spec)
1474 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1475 al=SSL_AD_UNEXPECTED_MESSAGE;
1479 /* we now have a signature that we need to verify */
1480 p=(unsigned char *)s->init_buf->data;
1485 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1486 al=SSL_AD_DECODE_ERROR;
1490 j=EVP_PKEY_size(pkey);
1491 if ((i > j) || (n > j) || (n <= 0))
1493 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1494 al=SSL_AD_DECODE_ERROR;
1499 if (pkey->type == EVP_PKEY_RSA)
1501 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1502 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1506 al=SSL_AD_DECRYPT_ERROR;
1507 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1512 al=SSL_AD_DECRYPT_ERROR;
1513 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1520 if (pkey->type == EVP_PKEY_DSA)
1522 j=DSA_verify(pkey->save_type,
1523 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1524 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1528 al=SSL_AD_DECRYPT_ERROR;
1529 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1536 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
1537 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1546 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1549 EVP_PKEY_free(pkey);
1553 static int ssl3_get_client_certificate(SSL *s)
1555 int i,ok,al,ret= -1;
1557 unsigned long l,nc,llen,n;
1558 unsigned char *p,*d,*q;
1559 STACK_OF(X509) *sk=NULL;
1561 n=ssl3_get_message(s,
1565 #if defined(MSDOS) && !defined(WIN32)
1566 1024*30, /* 30k max cert list :-) */
1568 1024*100, /* 100k max cert list :-) */
1572 if (!ok) return((int)n);
1574 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1576 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1577 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1579 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1580 al=SSL_AD_HANDSHAKE_FAILURE;
1583 /* If tls asked for a client cert, the client must return a 0 list */
1584 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1586 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1587 al=SSL_AD_UNEXPECTED_MESSAGE;
1590 s->s3->tmp.reuse_message=1;
1594 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1596 al=SSL_AD_UNEXPECTED_MESSAGE;
1597 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1600 d=p=(unsigned char *)s->init_buf->data;
1602 if ((sk=sk_X509_new_null()) == NULL)
1604 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1611 al=SSL_AD_DECODE_ERROR;
1612 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1615 for (nc=0; nc<llen; )
1618 if ((l+nc+3) > llen)
1620 al=SSL_AD_DECODE_ERROR;
1621 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1626 x=d2i_X509(NULL,&p,l);
1629 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1634 al=SSL_AD_DECODE_ERROR;
1635 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1638 if (!sk_X509_push(sk,x))
1640 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1647 if (sk_X509_num(sk) <= 0)
1649 /* TLS does not mind 0 certs returned */
1650 if (s->version == SSL3_VERSION)
1652 al=SSL_AD_HANDSHAKE_FAILURE;
1653 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1656 /* Fail for TLS only if we required a certificate */
1657 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1658 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1660 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1661 al=SSL_AD_HANDSHAKE_FAILURE;
1667 i=ssl_verify_cert_chain(s,sk);
1670 al=ssl_verify_alarm_type(s->verify_result);
1671 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1676 if (s->session->peer != NULL) /* This should not be needed */
1677 X509_free(s->session->peer);
1678 s->session->peer=sk_X509_shift(sk);
1679 s->session->verify_result = s->verify_result;
1681 /* With the current implementation, sess_cert will always be NULL
1682 * when we arrive here. */
1683 if (s->session->sess_cert == NULL)
1685 s->session->sess_cert = ssl_sess_cert_new();
1686 if (s->session->sess_cert == NULL)
1688 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1692 if (s->session->sess_cert->cert_chain != NULL)
1693 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
1694 s->session->sess_cert->cert_chain=sk;
1702 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1705 if (x != NULL) X509_free(x);
1706 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
1710 int ssl3_send_server_certificate(SSL *s)
1715 if (s->state == SSL3_ST_SW_CERT_A)
1717 x=ssl_get_server_send_cert(s);
1720 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
1724 l=ssl3_output_cert_chain(s,x);
1725 s->state=SSL3_ST_SW_CERT_B;
1730 /* SSL3_ST_SW_CERT_B */
1731 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));