Add per-SSL 'msg_callback' with 'msg_callback_arg'.
[openssl.git] / ssl / s3_both.c
index 68ddb143da473843cbcbd138fd713c1ba549829c..dd860a6ba688c0f5dc4402d47920777c74006b6d 100644 (file)
@@ -56,7 +56,7 @@
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
- * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2001 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
  *
  */
 
+#include <limits.h>
 #include <string.h>
 #include <stdio.h>
 #include <openssl/buffer.h>
@@ -205,7 +206,7 @@ int ssl3_get_finished(SSL *s, int a, int b)
                }
        s->s3->change_cipher_spec=0;
 
-       p = (unsigned char *)s->init_buf->data;
+       p = (unsigned char *)s->init_msg;
        i = s->s3->tmp.peer_finish_md_len;
 
        if (i != n)
@@ -355,7 +356,9 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                        goto f_err;
                        }
                *ok=1;
-               return((int)s->s3->tmp.message_size);
+               s->init_msg = s->init_buf->data + 4;
+               s->init_num = (int)s->s3->tmp.message_size;
+               return s->init_num;
                }
 
        p=(unsigned char *)s->init_buf->data;
@@ -387,7 +390,11 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                                         * if their format is correct. Does not count for
                                         * 'Finished' MAC. */
                                        if (p[1] == 0 && p[2] == 0 &&p[3] == 0)
+                                               {
+                                               s->init_num = 0;
                                                skip_message = 1;
+                                               }
+                       
                        }
                while (skip_message);
 
@@ -411,8 +418,6 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                        ssl3_init_finished_mac(s);
                        }
 
-               ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, 4);
-                       
                s->s3->tmp.message_type= *(p++);
 
                n2l3(p,l);
@@ -422,7 +427,13 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                        SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);
                        goto f_err;
                        }
-               if (l && !BUF_MEM_grow(s->init_buf,(int)l))
+               if (l > (INT_MAX-4)) /* BUF_MEM_grow takes an 'int' parameter */
+                       {
+                       al=SSL_AD_ILLEGAL_PARAMETER;
+                       SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);
+                       goto f_err;
+                       }
+               if (l && !BUF_MEM_grow(s->init_buf,(int)l+4))
                        {
                        SSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB);
                        goto err;
@@ -430,12 +441,13 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                s->s3->tmp.message_size=l;
                s->state=stn;
 
-               s->init_num=0;
+               s->init_msg = s->init_buf->data + 4;
+               s->init_num = 0;
                }
 
        /* next state (stn) */
-       p=(unsigned char *)s->init_buf->data;
-       n=s->s3->tmp.message_size;
+       p = s->init_msg;
+       n = s->s3->tmp.message_size - s->init_num;
        while (n > 0)
                {
                i=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n,0);
@@ -448,7 +460,7 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                s->init_num += i;
                n -= i;
                }
-       ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num);
+       ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4);
        *ok=1;
        return s->init_num;
 f_err: