From 5e2b640532f36e5ad5e616a52a371d20edd78d01 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 8 Jun 2021 09:14:28 +0200
Subject: Receive blocks of data bigger than TLS records if needed.

---
 src/common/packed.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/common/packed.c b/src/common/packed.c
index d246de8..b8ed21b 100644
--- a/src/common/packed.c
+++ b/src/common/packed.c
@@ -591,7 +591,10 @@ bool ssl_recv_packed_buffer(packed_buffer_t *pbuf, SSL *fd)
 {
     bool result;                            /* Bilan à retourner           */
     uint32_t used;                          /* Taille de charge utile      */
-    int got;                                /* Quantité de données traitées*/
+    size_t pos;                             /* Localisation du stockage    */
+    size_t remaining;                       /* Quantité à lire restante    */
+    size_t got;                             /* QUantité lue par appel      */
+    int ret;                                /* Code de retour d'une lecture*/
 
     got = SSL_read(fd, &used, sizeof(uint32_t));
     if (got <= 0) LOG_ERROR_OPENSSL;
@@ -610,10 +613,28 @@ bool ssl_recv_packed_buffer(packed_buffer_t *pbuf, SSL *fd)
 
         pbuf->used = used;
 
-        got = SSL_read(fd, pbuf->data + pbuf->pos, used);
-        if (got <= 0) LOG_ERROR_OPENSSL;
+        pos = pbuf->pos;
+        remaining = used;
 
-        result = (got == used);
+        while (remaining > 0)
+        {
+            ret = SSL_read_ex(fd, pbuf->data + pos, remaining, &got);
+
+            if (ret > 0)
+            {
+                pos += got;
+                remaining -= got;
+            }
+
+            else
+            {
+                LOG_ERROR_OPENSSL;
+                break;
+            }
+
+        }
+
+        result = (remaining == 0);
 
     }
 
-- 
cgit v0.11.2-87-g4458