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.]
63 char *ssl3_version_str="SSLv3 part of OpenSSL 0.9.2 31-Dec-1998";
65 #define SSL3_NUM_CIPHERS (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
68 static long ssl3_default_timeout(void );
70 static long ssl3_default_timeout();
73 SSL_CIPHER ssl3_ciphers[]={
78 SSL3_TXT_RSA_NULL_MD5,
80 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3,
87 SSL3_TXT_RSA_NULL_SHA,
89 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
98 SSL3_TXT_ADH_RC4_40_MD5,
99 SSL3_CK_ADH_RC4_40_MD5,
100 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_EXP40|SSL_SSLV3,
107 SSL3_TXT_ADH_RC4_128_MD5,
108 SSL3_CK_ADH_RC4_128_MD5,
109 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3,
116 SSL3_TXT_ADH_DES_40_CBC_SHA,
117 SSL3_CK_ADH_DES_40_CBC_SHA,
118 SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
125 SSL3_TXT_ADH_DES_64_CBC_SHA,
126 SSL3_CK_ADH_DES_64_CBC_SHA,
127 SSL_kEDH |SSL_aNULL|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
134 SSL3_TXT_ADH_DES_192_CBC_SHA,
135 SSL3_CK_ADH_DES_192_CBC_SHA,
136 SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
145 SSL3_TXT_RSA_RC4_40_MD5,
146 SSL3_CK_RSA_RC4_40_MD5,
147 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5 |SSL_EXP40|SSL_SSLV3,
154 SSL3_TXT_RSA_RC4_128_MD5,
155 SSL3_CK_RSA_RC4_128_MD5,
156 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3|SSL_MEDIUM,
163 SSL3_TXT_RSA_RC4_128_SHA,
164 SSL3_CK_RSA_RC4_128_SHA,
165 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_MEDIUM,
172 SSL3_TXT_RSA_RC2_40_MD5,
173 SSL3_CK_RSA_RC2_40_MD5,
174 SSL_kRSA|SSL_aRSA|SSL_RC2 |SSL_MD5 |SSL_EXP40|SSL_SSLV3,
181 SSL3_TXT_RSA_IDEA_128_SHA,
182 SSL3_CK_RSA_IDEA_128_SHA,
183 SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_MEDIUM,
190 SSL3_TXT_RSA_DES_40_CBC_SHA,
191 SSL3_CK_RSA_DES_40_CBC_SHA,
192 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
199 SSL3_TXT_RSA_DES_64_CBC_SHA,
200 SSL3_CK_RSA_DES_64_CBC_SHA,
201 SSL_kRSA|SSL_aRSA|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
208 SSL3_TXT_RSA_DES_192_CBC3_SHA,
209 SSL3_CK_RSA_DES_192_CBC3_SHA,
210 SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
219 SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
220 SSL3_CK_DH_DSS_DES_40_CBC_SHA,
221 SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
228 SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
229 SSL3_CK_DH_DSS_DES_64_CBC_SHA,
230 SSL_kDHd |SSL_aDH|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
237 SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
238 SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
239 SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
246 SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
247 SSL3_CK_DH_RSA_DES_40_CBC_SHA,
248 SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
255 SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
256 SSL3_CK_DH_RSA_DES_64_CBC_SHA,
257 SSL_kDHr |SSL_aDH|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
264 SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
265 SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
266 SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
271 /* The Ephemeral DH ciphers */
275 SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
276 SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
277 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
284 SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
285 SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
286 SSL_kEDH|SSL_aDSS|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
293 SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
294 SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
295 SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
302 SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
303 SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
304 SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
311 SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
312 SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
313 SSL_kEDH|SSL_aRSA|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
320 SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
321 SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
322 SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
331 SSL3_TXT_FZA_DMS_NULL_SHA,
332 SSL3_CK_FZA_DMS_NULL_SHA,
333 SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
341 SSL3_TXT_FZA_DMS_FZA_SHA,
342 SSL3_CK_FZA_DMS_FZA_SHA,
343 SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
351 SSL3_TXT_FZA_DMS_RC4_SHA,
352 SSL3_CK_FZA_DMS_RC4_SHA,
353 SSL_kFZA|SSL_aFZA |SSL_RC4 |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
358 /* New TLS Export CipherSuites */
362 TLS1_TXT_RSA_EXPORT56_WITH_RC4_56_MD5,
363 TLS1_CK_RSA_EXPORT56_WITH_RC4_56_MD5,
364 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_EXP56|SSL_TLSV1,
371 TLS1_TXT_RSA_EXPORT56_WITH_RC2_CBC_56_MD5,
372 TLS1_CK_RSA_EXPORT56_WITH_RC2_CBC_56_MD5,
373 SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_EXP56|SSL_TLSV1,
380 TLS1_TXT_RSA_EXPORT56_WITH_DES_CBC_SHA,
381 TLS1_CK_RSA_EXPORT56_WITH_DES_CBC_SHA,
382 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_EXP56|SSL_TLSV1,
390 static SSL3_ENC_METHOD SSLv3_enc_data={
393 ssl3_setup_key_block,
394 ssl3_generate_master_secret,
395 ssl3_change_cipher_state,
396 ssl3_final_finish_mac,
397 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
398 ssl3_cert_verify_mac,
399 SSL3_MD_CLIENT_FINISHED_CONST,4,
400 SSL3_MD_SERVER_FINISHED_CONST,4,
404 static SSL_METHOD SSLv3_data= {
409 ssl_undefined_function,
410 ssl_undefined_function,
416 ssl3_renegotiate_check,
419 ssl3_get_cipher_by_char,
420 ssl3_put_cipher_by_char,
425 ssl3_default_timeout,
429 static long ssl3_default_timeout()
431 /* 2 hours, the 24 hours mentioned in the SSLv3 spec
432 * is way too long for http, the cache would over fill */
436 SSL_METHOD *sslv3_base_method()
441 int ssl3_num_ciphers()
443 return(SSL3_NUM_CIPHERS);
446 SSL_CIPHER *ssl3_get_cipher(u)
449 if (u < SSL3_NUM_CIPHERS)
450 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
455 /* The problem is that it may not be the correct record type */
459 return(s->s3->rrec.length);
467 if ((s3=(SSL3_CTX *)Malloc(sizeof(SSL3_CTX))) == NULL) goto err;
468 memset(s3,0,sizeof(SSL3_CTX));
472 s->s3->tmp.ca_names=NULL;
473 s->s3->tmp.key_block=NULL;
474 s->s3->tmp.key_block_length=0;
475 s->s3->rbuf.buf=NULL;
476 s->s3->wbuf.buf=NULL;
479 s->method->ssl_clear(s);
491 ssl3_cleanup_key_block(s);
492 if (s->s3->rbuf.buf != NULL)
493 Free(s->s3->rbuf.buf);
494 if (s->s3->wbuf.buf != NULL)
495 Free(s->s3->wbuf.buf);
496 if (s->s3->rrec.comp != NULL)
497 Free(s->s3->rrec.comp);
499 if (s->s3->tmp.dh != NULL)
500 DH_free(s->s3->tmp.dh);
502 if (s->s3->tmp.ca_names != NULL)
503 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
504 memset(s->s3,0,sizeof(SSL3_CTX));
512 unsigned char *rp,*wp;
514 ssl3_cleanup_key_block(s);
515 if (s->s3->tmp.ca_names != NULL)
516 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
518 if (s->s3->rrec.comp != NULL)
520 Free(s->s3->rrec.comp);
521 s->s3->rrec.comp=NULL;
527 memset(s->s3,0,sizeof(SSL3_CTX));
528 if (rp != NULL) s->s3->rbuf.buf=rp;
529 if (wp != NULL) s->s3->wbuf.buf=wp;
531 ssl_free_wbio_buffer(s);
534 s->s3->renegotiate=0;
535 s->s3->total_renegotiations=0;
536 s->s3->num_renegotiations=0;
537 s->s3->in_read_app_data=0;
538 s->version=SSL3_VERSION;
541 long ssl3_ctrl(s,cmd,larg,parg)
549 #if !defined(NO_DSA) || !defined(NO_RSA)
552 cmd == SSL_CTRL_SET_TMP_RSA ||
553 cmd == SSL_CTRL_SET_TMP_RSA_CB ||
556 cmd == SSL_CTRL_SET_TMP_DH ||
557 cmd == SSL_CTRL_SET_TMP_DH_CB ||
561 if (!ssl_cert_instantiate(&s->cert, s->ctx->default_cert))
563 SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
571 case SSL_CTRL_GET_SESSION_REUSED:
574 case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
576 case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
577 ret=s->s3->num_renegotiations;
579 case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
580 ret=s->s3->num_renegotiations;
581 s->s3->num_renegotiations=0;
583 case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
584 ret=s->s3->total_renegotiations;
586 case SSL_CTRL_GET_FLAGS:
587 ret=(int)(s->s3->flags);
590 case SSL_CTRL_NEED_TMP_RSA:
591 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
592 ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
593 (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
596 case SSL_CTRL_SET_TMP_RSA:
598 RSA *rsa = (RSA *)parg;
600 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
603 if ((rsa = RSAPrivateKey_dup(rsa)) == NULL) {
604 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
607 if (s->cert->rsa_tmp != NULL)
608 RSA_free(s->cert->rsa_tmp);
609 s->cert->rsa_tmp = rsa;
613 case SSL_CTRL_SET_TMP_RSA_CB:
615 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))parg;
617 s->cert->rsa_tmp_cb = (RSA *(*)())parg;
622 case SSL_CTRL_SET_TMP_DH:
626 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
629 if ((dh = DHparams_dup(dh)) == NULL) {
630 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
633 if (!DH_generate_key(dh)) {
635 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
638 if (s->cert->dh_tmp != NULL)
639 DH_free(s->cert->dh_tmp);
640 s->cert->dh_tmp = dh;
644 case SSL_CTRL_SET_TMP_DH_CB:
646 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))parg;
648 s->cert->dh_tmp_cb = (DH *(*)())parg;
658 long ssl3_ctx_ctrl(ctx,cmd,larg,parg)
666 cert=ctx->default_cert;
671 case SSL_CTRL_NEED_TMP_RSA:
672 if ( (cert->rsa_tmp == NULL) &&
673 ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
674 (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
680 case SSL_CTRL_SET_TMP_RSA:
691 if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
696 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
701 if (cert->rsa_tmp != NULL)
702 RSA_free(cert->rsa_tmp);
708 case SSL_CTRL_SET_TMP_RSA_CB:
710 cert->rsa_tmp_cb=(RSA *(*)(SSL *, int, int))parg;
712 cert->rsa_tmp_cb=(RSA *(*)())parg;
717 case SSL_CTRL_SET_TMP_DH:
723 if ( ((new=DHparams_dup(dh)) == NULL) ||
724 (!DH_generate_key(new)))
726 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
727 if (new != NULL) DH_free(new);
731 if (cert->dh_tmp != NULL)
732 DH_free(cert->dh_tmp);
739 case SSL_CTRL_SET_TMP_DH_CB:
741 cert->dh_tmp_cb=(DH *(*)(SSL *, int, int))parg;
743 cert->dh_tmp_cb=(DH *(*)())parg;
747 /* A Thawte special :-) */
748 case SSL_CTRL_EXTRA_CHAIN_CERT:
749 if (ctx->extra_certs == NULL)
751 if ((ctx->extra_certs=sk_new_null()) == NULL)
754 sk_push(ctx->extra_certs,(char *)parg);
763 /* This function needs to check if the ciphers required are actually
765 SSL_CIPHER *ssl3_get_cipher_by_char(p)
766 const unsigned char *p;
769 static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
770 SSL_CIPHER c,*cp= &c,**cpp;
778 for (i=0; i<SSL3_NUM_CIPHERS; i++)
779 sorted[i]= &(ssl3_ciphers[i]);
781 qsort( (char *)sorted,
782 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
783 FP_ICC ssl_cipher_ptr_id_cmp);
786 id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
788 cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
790 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
791 (int (*)())ssl_cipher_ptr_id_cmp);
792 if ((cpp == NULL) || !(*cpp)->valid)
798 int ssl3_put_cipher_by_char(c,p)
807 if ((l & 0xff000000) != 0x03000000) return(0);
808 p[0]=((unsigned char)(l>> 8L))&0xFF;
809 p[1]=((unsigned char)(l ))&0xFF;
814 int ssl3_part_read(s,i)
818 s->rwstate=SSL_READING;
831 SSL_CIPHER *ssl3_choose_cipher(s,have,pref)
835 SSL_CIPHER *c,*ret=NULL;
838 unsigned long alg,mask,emask;
840 /* Lets see which ciphers we can supported */
844 cert=s->ctx->default_cert;
846 sk_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
848 for (i=0; i<sk_num(have); i++)
850 c=(SSL_CIPHER *)sk_value(have,i);
852 ssl_set_cert_masks(cert,c);
854 emask=cert->export_mask;
856 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
857 if (SSL_IS_EXPORT(c->algorithms))
859 ok=((alg & emask) == alg)?1:0;
861 printf("%d:[%08lX:%08lX]%s (export)\n",ok,alg,mask,c->name);
866 ok=((alg & mask) == alg)?1:0;
868 printf("%d:[%08lX:%08lX]%s\n",ok,alg,mask,c->name);
874 j=sk_find(pref,(char *)c);
877 ret=(SSL_CIPHER *)sk_value(pref,j);
884 int ssl3_get_req_cert_type(s,p)
891 alg=s->s3->tmp.new_cipher->algorithms;
894 if (alg & (SSL_kDHr|SSL_kEDH))
897 p[ret++]=SSL3_CT_RSA_FIXED_DH;
900 p[ret++]=SSL3_CT_DSS_FIXED_DH;
903 if ((s->version == SSL3_VERSION) &&
904 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
907 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
910 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
915 p[ret++]=SSL3_CT_RSA_SIGN;
918 p[ret++]=SSL3_CT_DSS_SIGN;
927 /* Don't do anything much if we have not done the handshake or
928 * we don't want to send messages :-) */
929 if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
931 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
935 if (!(s->shutdown & SSL_SENT_SHUTDOWN))
937 s->shutdown|=SSL_SENT_SHUTDOWN;
939 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
941 /* our shutdown alert has been sent now, and if it still needs
942 * to be written, s->s3->alert_dispatch will be true */
944 else if (s->s3->alert_dispatch)
946 /* resend it if not sent */
948 ssl3_dispatch_alert(s);
951 else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
953 /* If we are waiting for a close from our peer, we are closed */
954 ssl3_read_bytes(s,0,NULL,0);
957 if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
958 !s->s3->alert_dispatch)
964 int ssl3_write(s,buf,len)
972 if (s->shutdown & SSL_SEND_SHUTDOWN)
974 s->rwstate=SSL_NOTHING;
979 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
981 /* This is an experimental flag that sends the
982 * last handshake message in the same packet as the first
983 * use data - used to see if it helps the TCP protocol during
984 * session-id reuse */
985 /* The second test is because the buffer may have been removed */
986 if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
988 /* First time through, we write into the buffer */
989 if (s->s3->delay_buf_pop_ret == 0)
991 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
993 if (ret <= 0) return(ret);
995 s->s3->delay_buf_pop_ret=ret;
998 s->rwstate=SSL_WRITING;
999 n=BIO_flush(s->wbio);
1000 if (n <= 0) return(n);
1001 s->rwstate=SSL_NOTHING;
1003 /* We have flushed the buffer, so remove it */
1004 ssl_free_wbio_buffer(s);
1005 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1007 ret=s->s3->delay_buf_pop_ret;
1008 s->s3->delay_buf_pop_ret=0;
1012 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1014 if (ret <= 0) return(ret);
1020 int ssl3_read(s,buf,len)
1028 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1029 s->s3->in_read_app_data=1;
1030 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1031 if ((ret == -1) && (s->s3->in_read_app_data == 0))
1033 ERR_get_error(); /* clear the error */
1034 s->s3->in_read_app_data=0;
1036 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1040 s->s3->in_read_app_data=0;
1045 int ssl3_peek(s,buf,len)
1054 if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
1056 n=ssl3_read(s,buf,1);
1057 if (n <= 0) return(n);
1062 if ((unsigned int)len > rr->length)
1066 memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
1070 int ssl3_renegotiate(s)
1073 if (s->handshake_func == NULL)
1076 if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
1079 s->s3->renegotiate=1;
1083 int ssl3_renegotiate_check(s)
1088 if (s->s3->renegotiate)
1090 if ( (s->s3->rbuf.left == 0) &&
1091 (s->s3->wbuf.left == 0) &&
1095 if we are the server, and we have sent a 'RENEGOTIATE' message, we
1096 need to go to SSL_ST_ACCEPT.
\1e
1099 s->state=SSL_ST_RENEGOTIATE;
1100 s->s3->renegotiate=0;
1101 s->s3->num_renegotiations++;
1102 s->s3->total_renegotiations++;