Fix SSL_peek and SSL_pending.
[openssl.git] / ssl / s3_pkt.c
index fd344c4ceb1e45786f3fee881689989b1e75ae55..9ab76604a64534192a45569a6f90257487b2135a 100644 (file)
@@ -56,7 +56,7 @@
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
- * Copyright (c) 1998-1999 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -704,9 +704,10 @@ static int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  *     Application data protocol
  *             none of our business
  */
-int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len)
+int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
        {
-       int al,i,j,n,ret;
+       int al,i,j,ret;
+       unsigned int n;
        SSL3_RECORD *rr;
        void (*cb)()=NULL;
 
@@ -714,7 +715,8 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len)
                if (!ssl3_setup_buffers(s))
                        return(-1);
 
-       if ((type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type)
+       if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) ||
+           (peek && (type != SSL3_RT_APPLICATION_DATA)))
                {
                SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_INTERNAL_ERROR);
                return -1;
@@ -725,7 +727,9 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len)
                {
                unsigned char *src = s->s3->handshake_fragment;
                unsigned char *dst = buf;
+               unsigned int k;
 
+               /* peek == 0 */
                n = 0;
                while ((len > 0) && (s->s3->handshake_fragment_len > 0))
                        {
@@ -734,8 +738,8 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len)
                        n++;
                        }
                /* move any remaining fragment bytes: */
-               for (i = 0; i < s->s3->handshake_fragment_len; i++)
-                       s->s3->handshake_fragment[i] = *src++;
+               for (k = 0; k < s->s3->handshake_fragment_len; k++)
+                       s->s3->handshake_fragment[k] = *src++;
                return n;
        }
 
@@ -761,7 +765,7 @@ start:
         * s->s3->rrec.length,  - number of bytes. */
        rr = &(s->s3->rrec);
 
-       /* get new packet */
+       /* get new packet if necessary */
        if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))
                {
                ret=ssl3_get_record(s);
@@ -779,7 +783,8 @@ start:
                goto err;
                }
 
-       /* If the other end has shutdown, throw anything we read away */
+       /* If the other end has shut down, throw anything we read away
+        * (even in 'peek' mode) */
        if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
                {
                rr->length=0;
@@ -803,17 +808,20 @@ start:
                if (len <= 0) return(len);
 
                if ((unsigned int)len > rr->length)
-                       n=rr->length;
+                       n = rr->length;
                else
-                       n=len;
+                       n = (unsigned int)len;
 
-               memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
-               rr->length-=n;
-               rr->off+=n;
-               if (rr->length == 0)
+               memcpy(buf,&(rr->data[rr->off]),n);
+               if (!peek)
                        {
-                       s->rstate=SSL_ST_READ_HEADER;
-                       rr->off=0;
+                       rr->length-=n;
+                       rr->off+=n;
+                       if (rr->length == 0)
+                               {
+                               s->rstate=SSL_ST_READ_HEADER;
+                               rr->off=0;
+                               }
                        }
                return(n);
                }
@@ -826,9 +834,9 @@ start:
         * fill that so that we can process the data at a fixed place.
         */
                {
-               int dest_maxlen = 0;
-               unsigned char *dest;
-               int *dest_len;
+               unsigned int dest_maxlen = 0;
+               unsigned char *dest = NULL;
+               unsigned int *dest_len = NULL;
 
                if (rr->type == SSL3_RT_HANDSHAKE)
                        {
@@ -889,27 +897,29 @@ start:
                        ssl3_renegotiate(s);
                        if (ssl3_renegotiate_check(s))
                                {
-                               n=s->handshake_func(s);
-                               if (n < 0) return(n);
-                               if (n == 0)
+                               i=s->handshake_func(s);
+                               if (i < 0) return(i);
+                               if (i == 0)
                                        {
                                        SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
                                        return(-1);
                                        }
 
-                               if (s->s3->rbuf.left == 0) /* no read-ahead left? */
+                               if (!(s->mode & SSL_MODE_AUTO_RETRY))
                                        {
-                                       BIO *bio;
-                                       /* In the case where we try to read application data
-                                        * the first time, but we trigger an SSL handshake, we
-                                        * return -1 with the retry option set.  I do this
-                                        * otherwise renegotiation can cause nasty problems 
-                                        * in the blocking world */ /* ? */
-                                       s->rwstate=SSL_READING;
-                                       bio=SSL_get_rbio(s);
-                                       BIO_clear_retry_flags(bio);
-                                       BIO_set_retry_read(bio);
-                                       return(-1);
+                                       if (s->s3->rbuf.left == 0) /* no read-ahead left? */
+                                               {
+                                               BIO *bio;
+                                               /* In the case where we try to read application data,
+                                                * but we trigger an SSL handshake, we return -1 with
+                                                * the retry option set.  Otherwise renegotiation may
+                                                * cause nasty problems in the blocking world */
+                                               s->rwstate=SSL_READING;
+                                               bio=SSL_get_rbio(s);
+                                               BIO_clear_retry_flags(bio);
+                                               BIO_set_retry_read(bio);
+                                               return(-1);
+                                               }
                                        }
                                }
                        }
@@ -920,8 +930,8 @@ start:
 
        if (s->s3->alert_fragment_len >= 2)
                {
-               i = s->s3->alert_fragment[0];
-               n = s->s3->alert_fragment[1];
+               int alert_level = s->s3->alert_fragment[0];
+               int alert_descr = s->s3->alert_fragment[1];
 
                s->s3->alert_fragment_len = 0;
 
@@ -932,28 +942,27 @@ start:
 
                if (cb != NULL)
                        {
-                       j=(i<<8)|n;
-                       cb(s,SSL_CB_READ_ALERT,j);
+                       j = (alert_level << 8) | alert_descr;
+                       cb(s, SSL_CB_READ_ALERT, j);
                        }
 
-               if (i == 1) /* warning */
+               if (alert_level == 1) /* warning */
                        {
-                       s->s3->warn_alert=n;
-                       if (n == SSL_AD_CLOSE_NOTIFY)
+                       s->s3->warn_alert = alert_descr;
+                       if (alert_descr == SSL_AD_CLOSE_NOTIFY)
                                {
-                               s->shutdown|=SSL_RECEIVED_SHUTDOWN;
+                               s->shutdown |= SSL_RECEIVED_SHUTDOWN;
                                return(0);
                                }
                        }
-               else if (i == 2) /* fatal */
+               else if (alert_level == 2) /* fatal */
                        {
                        char tmp[16];
 
                        s->rwstate=SSL_NOTHING;
-                       s->s3->fatal_alert=n;
-                       SSLerr(SSL_F_SSL3_READ_BYTES,
-                               SSL_AD_REASON_OFFSET+n);
-                       sprintf(tmp,"%d",n);
+                       s->s3->fatal_alert = alert_descr;
+                       SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
+                       BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);
                        ERR_add_error_data(2,"SSL alert number ",tmp);
                        s->shutdown|=SSL_RECEIVED_SHUTDOWN;
                        SSL_CTX_remove_session(s->ctx,s->session);
@@ -1013,27 +1022,29 @@ start:
 #endif
                        s->new_session=1;
                        }
-               n=s->handshake_func(s);
-               if (n < 0) return(n);
-               if (n == 0)
+               i=s->handshake_func(s);
+               if (i < 0) return(i);
+               if (i == 0)
                        {
                        SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
                        return(-1);
                        }
 
-               if (s->s3->rbuf.left == 0) /* no read-ahead left? */
+               if (!(s->mode & SSL_MODE_AUTO_RETRY))
                        {
-                       BIO *bio;
-                       /* In the case where we try to read application data
-                        * the first time, but we trigger an SSL handshake, we
-                        * return -1 with the retry option set.  I do this
-                        * otherwise renegotiation can cause nasty problems 
-                        * in the blocking world */ /* ? */
-                       s->rwstate=SSL_READING;
-                       bio=SSL_get_rbio(s);
-                       BIO_clear_retry_flags(bio);
-                       BIO_set_retry_read(bio);
-                       return(-1);
+                       if (s->s3->rbuf.left == 0) /* no read-ahead left? */
+                               {
+                               BIO *bio;
+                               /* In the case where we try to read application data,
+                                * but we trigger an SSL handshake, we return -1 with
+                                * the retry option set.  Otherwise renegotiation may
+                                * cause nasty problems in the blocking world */
+                               s->rwstate=SSL_READING;
+                               bio=SSL_get_rbio(s);
+                               BIO_clear_retry_flags(bio);
+                               BIO_set_retry_read(bio);
+                               return(-1);
+                               }
                        }
                goto start;
                }