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.]
60 #include <openssl/md5.h>
61 #include <openssl/sha.h>
62 #include <openssl/objects.h>
65 const char *ssl3_version_str="SSLv3" OPENSSL_VERSION_PTEXT;
67 #define SSL3_NUM_CIPHERS (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
69 static long ssl3_default_timeout(void );
71 GLOBAL SSL_CIPHER ssl3_ciphers[]={
76 SSL3_TXT_RSA_NULL_MD5,
78 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3,
85 SSL3_TXT_RSA_NULL_SHA,
87 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
96 SSL3_TXT_ADH_RC4_40_MD5,
97 SSL3_CK_ADH_RC4_40_MD5,
98 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_EXP40|SSL_SSLV3,
105 SSL3_TXT_ADH_RC4_128_MD5,
106 SSL3_CK_ADH_RC4_128_MD5,
107 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3,
114 SSL3_TXT_ADH_DES_40_CBC_SHA,
115 SSL3_CK_ADH_DES_40_CBC_SHA,
116 SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
123 SSL3_TXT_ADH_DES_64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SSLV3|SSL_LOW,
206 SSL3_TXT_RSA_DES_192_CBC3_SHA,
207 SSL3_CK_RSA_DES_192_CBC3_SHA,
208 SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
217 SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
218 SSL3_CK_DH_DSS_DES_40_CBC_SHA,
219 SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
226 SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
227 SSL3_CK_DH_DSS_DES_64_CBC_SHA,
228 SSL_kDHd |SSL_aDH|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
235 SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
236 SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
237 SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
244 SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
245 SSL3_CK_DH_RSA_DES_40_CBC_SHA,
246 SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
253 SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
254 SSL3_CK_DH_RSA_DES_64_CBC_SHA,
255 SSL_kDHr |SSL_aDH|SSL_DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
262 SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
263 SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
264 SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
269 /* The Ephemeral DH ciphers */
273 SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
274 SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
275 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|S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356 #if TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES
357 /* New TLS Export CipherSuites */
361 TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_MD5,
362 TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_MD5,
363 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_EXP56|SSL_TLSV1,
370 TLS1_TXT_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
371 TLS1_CK_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
372 SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_EXP56|SSL_TLSV1,
379 TLS1_TXT_RSA_EXPORT1024_WITH_DES_CBC_SHA,
380 TLS1_CK_RSA_EXPORT1024_WITH_DES_CBC_SHA,
381 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_EXP56|SSL_TLSV1,
388 TLS1_TXT_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
389 TLS1_CK_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
390 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA|SSL_EXP56|SSL_TLSV1,
397 TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_SHA,
398 TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_SHA,
399 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_EXP56|SSL_TLSV1,
406 TLS1_TXT_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
407 TLS1_CK_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
408 SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_EXP56|SSL_TLSV1,
415 TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA,
416 TLS1_CK_DHE_DSS_WITH_RC4_128_SHA,
417 SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
426 static SSL3_ENC_METHOD SSLv3_enc_data={
429 ssl3_setup_key_block,
430 ssl3_generate_master_secret,
431 ssl3_change_cipher_state,
432 ssl3_final_finish_mac,
433 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
434 ssl3_cert_verify_mac,
435 SSL3_MD_CLIENT_FINISHED_CONST,4,
436 SSL3_MD_SERVER_FINISHED_CONST,4,
440 static SSL_METHOD SSLv3_data= {
445 ssl_undefined_function,
446 ssl_undefined_function,
452 ssl3_renegotiate_check,
455 ssl3_get_cipher_by_char,
456 ssl3_put_cipher_by_char,
461 ssl3_default_timeout,
465 static long ssl3_default_timeout(void)
467 /* 2 hours, the 24 hours mentioned in the SSLv3 spec
468 * is way too long for http, the cache would over fill */
472 SSL_METHOD *sslv3_base_method(void)
477 int ssl3_num_ciphers(void)
479 return(SSL3_NUM_CIPHERS);
482 SSL_CIPHER *ssl3_get_cipher(unsigned int u)
484 if (u < SSL3_NUM_CIPHERS)
485 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
490 /* The problem is that it may not be the correct record type */
491 int ssl3_pending(SSL *s)
493 return(s->s3->rrec.length);
500 if ((s3=(SSL3_CTX *)Malloc(sizeof(SSL3_CTX))) == NULL) goto err;
501 memset(s3,0,sizeof(SSL3_CTX));
505 s->s3->tmp.ca_names=NULL;
506 s->s3->tmp.key_block=NULL;
507 s->s3->tmp.key_block_length=0;
508 s->s3->rbuf.buf=NULL;
509 s->s3->wbuf.buf=NULL;
512 s->method->ssl_clear(s);
518 void ssl3_free(SSL *s)
523 ssl3_cleanup_key_block(s);
524 if (s->s3->rbuf.buf != NULL)
525 Free(s->s3->rbuf.buf);
526 if (s->s3->wbuf.buf != NULL)
527 Free(s->s3->wbuf.buf);
528 if (s->s3->rrec.comp != NULL)
529 Free(s->s3->rrec.comp);
531 if (s->s3->tmp.dh != NULL)
532 DH_free(s->s3->tmp.dh);
534 if (s->s3->tmp.ca_names != NULL)
535 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
536 memset(s->s3,0,sizeof(SSL3_CTX));
541 void ssl3_clear(SSL *s)
543 unsigned char *rp,*wp;
545 ssl3_cleanup_key_block(s);
546 if (s->s3->tmp.ca_names != NULL)
547 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
549 if (s->s3->rrec.comp != NULL)
551 Free(s->s3->rrec.comp);
552 s->s3->rrec.comp=NULL;
558 memset(s->s3,0,sizeof(SSL3_CTX));
559 if (rp != NULL) s->s3->rbuf.buf=rp;
560 if (wp != NULL) s->s3->wbuf.buf=wp;
562 ssl_free_wbio_buffer(s);
565 s->s3->renegotiate=0;
566 s->s3->total_renegotiations=0;
567 s->s3->num_renegotiations=0;
568 s->s3->in_read_app_data=0;
569 s->version=SSL3_VERSION;
572 long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
576 #if !defined(NO_DSA) || !defined(NO_RSA)
579 cmd == SSL_CTRL_SET_TMP_RSA ||
580 cmd == SSL_CTRL_SET_TMP_RSA_CB ||
583 cmd == SSL_CTRL_SET_TMP_DH ||
584 cmd == SSL_CTRL_SET_TMP_DH_CB ||
588 if (!ssl_cert_inst(&s->cert))
590 SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
598 case SSL_CTRL_GET_SESSION_REUSED:
601 case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
603 case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
604 ret=s->s3->num_renegotiations;
606 case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
607 ret=s->s3->num_renegotiations;
608 s->s3->num_renegotiations=0;
610 case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
611 ret=s->s3->total_renegotiations;
613 case SSL_CTRL_GET_FLAGS:
614 ret=(int)(s->s3->flags);
617 case SSL_CTRL_NEED_TMP_RSA:
618 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
619 ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
620 (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
623 case SSL_CTRL_SET_TMP_RSA:
625 RSA *rsa = (RSA *)parg;
627 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
630 if ((rsa = RSAPrivateKey_dup(rsa)) == NULL) {
631 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
634 if (s->cert->rsa_tmp != NULL)
635 RSA_free(s->cert->rsa_tmp);
636 s->cert->rsa_tmp = rsa;
640 case SSL_CTRL_SET_TMP_RSA_CB:
641 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))parg;
645 case SSL_CTRL_SET_TMP_DH:
649 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
652 if ((dh = DHparams_dup(dh)) == NULL) {
653 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
656 if (!DH_generate_key(dh)) {
658 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
661 if (s->cert->dh_tmp != NULL)
662 DH_free(s->cert->dh_tmp);
663 s->cert->dh_tmp = dh;
667 case SSL_CTRL_SET_TMP_DH_CB:
668 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))parg;
677 long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
686 case SSL_CTRL_NEED_TMP_RSA:
687 if ( (cert->rsa_tmp == NULL) &&
688 ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
689 (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
695 case SSL_CTRL_SET_TMP_RSA:
706 if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
711 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
716 if (cert->rsa_tmp != NULL)
717 RSA_free(cert->rsa_tmp);
723 case SSL_CTRL_SET_TMP_RSA_CB:
724 cert->rsa_tmp_cb=(RSA *(*)(SSL *, int, int))parg;
728 case SSL_CTRL_SET_TMP_DH:
734 if ( ((new=DHparams_dup(dh)) == NULL) ||
735 (!DH_generate_key(new)))
737 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
738 if (new != NULL) DH_free(new);
742 if (cert->dh_tmp != NULL)
743 DH_free(cert->dh_tmp);
750 case SSL_CTRL_SET_TMP_DH_CB:
751 cert->dh_tmp_cb=(DH *(*)(SSL *, int, int))parg;
754 /* A Thawte special :-) */
755 case SSL_CTRL_EXTRA_CHAIN_CERT:
756 if (ctx->extra_certs == NULL)
758 if ((ctx->extra_certs=sk_X509_new_null()) == NULL)
761 sk_X509_push(ctx->extra_certs,(X509 *)parg);
770 /* This function needs to check if the ciphers required are actually
772 SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
775 static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
776 SSL_CIPHER c,*cp= &c,**cpp;
782 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
784 for (i=0; i<SSL3_NUM_CIPHERS; i++)
785 sorted[i]= &(ssl3_ciphers[i]);
787 qsort( (char *)sorted,
788 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
789 FP_ICC ssl_cipher_ptr_id_cmp);
791 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
796 id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
798 cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
800 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
801 (int (*)())ssl_cipher_ptr_id_cmp);
802 if ((cpp == NULL) || !(*cpp)->valid)
808 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
815 if ((l & 0xff000000) != 0x03000000) return(0);
816 p[0]=((unsigned char)(l>> 8L))&0xFF;
817 p[1]=((unsigned char)(l ))&0xFF;
822 int ssl3_part_read(SSL *s, int i)
824 s->rwstate=SSL_READING;
837 SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *have,
838 STACK_OF(SSL_CIPHER) *pref)
840 SSL_CIPHER *c,*ret=NULL;
843 unsigned long alg,mask,emask;
845 /* Let's see which ciphers we can support */
848 sk_SSL_CIPHER_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
852 for(i=0 ; i < sk_num(pref) ; ++i)
854 c=(SSL_CIPHER *)sk_value(pref,i);
855 printf("%p:%s\n",c,c->name);
859 for (i=0; i<sk_SSL_CIPHER_num(have); i++)
861 c=sk_SSL_CIPHER_value(have,i);
863 ssl_set_cert_masks(cert,c);
865 emask=cert->export_mask;
867 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
868 if (SSL_IS_EXPORT(c->algorithms))
870 ok=((alg & emask) == alg)?1:0;
872 printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
878 ok=((alg & mask) == alg)?1:0;
880 printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
887 j=sk_SSL_CIPHER_find(pref,c);
890 ret=sk_SSL_CIPHER_value(pref,j);
897 int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
902 alg=s->s3->tmp.new_cipher->algorithms;
905 if (alg & (SSL_kDHr|SSL_kEDH))
908 p[ret++]=SSL3_CT_RSA_FIXED_DH;
911 p[ret++]=SSL3_CT_DSS_FIXED_DH;
914 if ((s->version == SSL3_VERSION) &&
915 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
918 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
921 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
926 p[ret++]=SSL3_CT_RSA_SIGN;
929 p[ret++]=SSL3_CT_DSS_SIGN;
934 int ssl3_shutdown(SSL *s)
937 /* Don't do anything much if we have not done the handshake or
938 * we don't want to send messages :-) */
939 if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
941 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
945 if (!(s->shutdown & SSL_SENT_SHUTDOWN))
947 s->shutdown|=SSL_SENT_SHUTDOWN;
949 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
951 /* our shutdown alert has been sent now, and if it still needs
952 * to be written, s->s3->alert_dispatch will be true */
954 else if (s->s3->alert_dispatch)
956 /* resend it if not sent */
958 ssl3_dispatch_alert(s);
961 else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
963 /* If we are waiting for a close from our peer, we are closed */
964 ssl3_read_bytes(s,0,NULL,0);
967 if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
968 !s->s3->alert_dispatch)
974 int ssl3_write(SSL *s, const void *buf, int len)
979 if (s->shutdown & SSL_SEND_SHUTDOWN)
981 s->rwstate=SSL_NOTHING;
986 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
988 /* This is an experimental flag that sends the
989 * last handshake message in the same packet as the first
990 * use data - used to see if it helps the TCP protocol during
991 * session-id reuse */
992 /* The second test is because the buffer may have been removed */
993 if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
995 /* First time through, we write into the buffer */
996 if (s->s3->delay_buf_pop_ret == 0)
998 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1000 if (ret <= 0) return(ret);
1002 s->s3->delay_buf_pop_ret=ret;
1005 s->rwstate=SSL_WRITING;
1006 n=BIO_flush(s->wbio);
1007 if (n <= 0) return(n);
1008 s->rwstate=SSL_NOTHING;
1010 /* We have flushed the buffer, so remove it */
1011 ssl_free_wbio_buffer(s);
1012 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1014 ret=s->s3->delay_buf_pop_ret;
1015 s->s3->delay_buf_pop_ret=0;
1019 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1021 if (ret <= 0) return(ret);
1027 int ssl3_read(SSL *s, void *buf, int len)
1032 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1033 s->s3->in_read_app_data=1;
1034 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1035 if ((ret == -1) && (s->s3->in_read_app_data == 0))
1037 ERR_get_error(); /* clear the error */
1038 s->s3->in_read_app_data=0;
1040 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1044 s->s3->in_read_app_data=0;
1049 int ssl3_peek(SSL *s, char *buf, int len)
1055 if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
1057 n=ssl3_read(s,buf,1);
1058 if (n <= 0) return(n);
1063 if ((unsigned int)len > rr->length)
1067 memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
1071 int ssl3_renegotiate(SSL *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(SSL *s)
1087 if (s->s3->renegotiate)
1089 if ( (s->s3->rbuf.left == 0) &&
1090 (s->s3->wbuf.left == 0) &&
1094 if we are the server, and we have sent a 'RENEGOTIATE' message, we
1095 need to go to SSL_ST_ACCEPT.
\1e
1098 s->state=SSL_ST_RENEGOTIATE;
1099 s->s3->renegotiate=0;
1100 s->s3->num_renegotiations++;
1101 s->s3->total_renegotiations++;