RT2842: Remove spurious close-comment marker.
[openssl.git] / ssl / s3_pkt.c
index fe46cb0b88f4c1e5d41aa421a15ad4f490df4991..3103b54619c283038021196ea3386ce1948fba29 100644 (file)
  */
 
 #include <stdio.h>
+#include <limits.h>
 #include <errno.h>
 #define USE_SOCKETS
 #include "ssl_locl.h"
@@ -286,6 +287,12 @@ int ssl3_read_n(SSL *s, int n, int max, int extend)
        return(n);
        }
 
+/* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
+ * be processed per call to ssl3_get_record. Without this limit an attacker
+ * could send empty records at a faster rate than we can process and cause
+ * ssl3_get_record to loop forever. */
+#define MAX_EMPTY_RECORDS 32
+
 /* Call this to get a new input record.
  * It will return <= 0 if more data is needed, normally due to an error
  * or non-blocking IO.
@@ -306,6 +313,7 @@ static int ssl3_get_record(SSL *s)
        short version;
        unsigned mac_size, orig_len;
        size_t extra;
+       unsigned empty_record_count = 0;
 
        rr= &(s->s3->rrec);
        sess=s->session;
@@ -538,7 +546,17 @@ printf("\n");
        s->packet_length=0;
 
        /* just read a 0 length packet */
-       if (rr->length == 0) goto again;
+       if (rr->length == 0)
+               {
+               empty_record_count++;
+               if (empty_record_count > MAX_EMPTY_RECORDS)
+                       {
+                       al=SSL_AD_UNEXPECTED_MESSAGE;
+                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_RECORD_TOO_SMALL);
+                       goto f_err;
+                       }
+               goto again;
+               }
 
 #if 0
 fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type, rr->length);
@@ -657,7 +675,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
         * compromise is considered worthy.
         */
        if (type==SSL3_RT_APPLICATION_DATA &&
-           len >= 4*(max_send_fragment=s->max_send_fragment) &&
+           len >= 4*(int)(max_send_fragment=s->max_send_fragment) &&
            s->compress==NULL && s->msg_callback==NULL &&
            SSL_USE_EXPLICIT_IV(s) &&
            EVP_CIPHER_flags(s->enc_write_ctx->cipher)&EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)
@@ -667,7 +685,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
                int packlen;
 
                /* minimize address aliasing conflicts */
-               if ((max_send_fragment&0xffff) == 0)
+               if ((max_send_fragment&0xfff) == 0)
                        max_send_fragment -= 512;
 
                if (tot==0 || wb->buf==NULL)    /* allocate jumbo buffer */
@@ -678,7 +696,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
                                        EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
                                        max_send_fragment,NULL);
 
-                       if (len>=8*max_send_fragment)   packlen *= 8;
+                       if (len>=8*(int)max_send_fragment)      packlen *= 8;
                        else                            packlen *= 4;
 
                        wb->buf=OPENSSL_malloc(packlen);
@@ -730,7 +748,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
                                        EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
                                        sizeof(mb_param),&mb_param);
 
-                       if (packlen<=0 || packlen>wb->len)      /* never happens */
+                       if (packlen<=0 || packlen>(int)wb->len) /* never happens */
                                {
                                OPENSSL_free(wb->buf);  /* free jumbo buffer */
                                wb->buf = NULL;
@@ -1151,7 +1169,7 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
                if (!ssl3_setup_read_buffer(s))
                        return(-1);
 
-       if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) ||
+       if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE)) ||
            (peek && (type != SSL3_RT_APPLICATION_DATA)))
                {
                SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);