summaryrefslogtreecommitdiffstats
path: root/ssl/packet.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2016-09-29 11:43:37 +0100
committerMatt Caswell <matt@openssl.org>2016-09-29 15:09:02 +0100
commit1ff8434040b35f35c27f77ef064481622490bba9 (patch)
tree32a48d1ef1a6bacb4311e2e6a7e2073cb53fac88 /ssl/packet.c
parentac8cc3efb26fa91c4f29463044cfe9e7070ebc14 (diff)
Add the WPACKET_reserve_bytes() function
WPACKET_allocate_bytes() requires you to know the size of the data you are allocating for, before you create it. Sometimes this isn't the case, for example we know the maximum size that a signature will be before we create it, but not the actual size. WPACKET_reserve_bytes() enables us to reserve bytes in the WPACKET, but not count them as written yet. We then subsequently need to acall WPACKET_allocate_bytes to actually count them as written. Reviewed-by: Rich Salz <rsalz@openssl.org>
Diffstat (limited to 'ssl/packet.c')
-rw-r--r--ssl/packet.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/ssl/packet.c b/ssl/packet.c
index 4077de5c33..7c240a4a82 100644
--- a/ssl/packet.c
+++ b/ssl/packet.c
@@ -14,6 +14,27 @@
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
+ if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
+ return 0;
+ pkt->written += len;
+ pkt->curr += len;
+
+ return 1;
+}
+
+int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
+ unsigned char **allocbytes, size_t lenbytes)
+{
+ if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
+ || !WPACKET_allocate_bytes(pkt, len, allocbytes)
+ || !WPACKET_close(pkt))
+ return 0;
+
+ return 1;
+}
+
+int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
+{
/* Internal API, so should not fail */
assert(pkt->subs != NULL && len != 0);
if (pkt->subs == NULL || len == 0)
@@ -39,20 +60,18 @@ int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
return 0;
}
*allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
- pkt->written += len;
- pkt->curr += len;
return 1;
}
-int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
- unsigned char **allocbytes, size_t lenbytes)
+int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
+ unsigned char **allocbytes, size_t lenbytes)
{
- if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
- || !WPACKET_allocate_bytes(pkt, len, allocbytes)
- || !WPACKET_close(pkt))
+ if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
return 0;
+ *allocbytes += lenbytes;
+
return 1;
}