2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "packet_locl.h"
13 #define DEFAULT_BUF_SIZE 256
15 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
17 if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
25 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
26 unsigned char **allocbytes, size_t lenbytes)
28 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
29 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
30 || !WPACKET_close(pkt))
36 #define GETBUF(p) (((p)->staticbuf != NULL) \
37 ? (p)->staticbuf : (unsigned char *)(p)->buf->data)
39 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
41 /* Internal API, so should not fail */
42 assert(pkt->subs != NULL && len != 0);
43 if (pkt->subs == NULL || len == 0)
46 if (pkt->maxsize - pkt->written < len)
49 if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
53 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
55 if (reflen > SIZE_MAX / 2) {
59 if (newlen < DEFAULT_BUF_SIZE)
60 newlen = DEFAULT_BUF_SIZE;
62 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
65 *allocbytes = GETBUF(pkt) + pkt->curr;
70 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
71 unsigned char **allocbytes, size_t lenbytes)
73 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
76 *allocbytes += lenbytes;
81 static size_t maxmaxsize(size_t lenbytes)
83 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
86 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
89 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
91 unsigned char *lenchars;
96 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
97 if (pkt->subs == NULL)
103 pkt->subs->pwritten = lenbytes;
104 pkt->subs->lenbytes = lenbytes;
106 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
107 OPENSSL_free(pkt->subs);
111 pkt->subs->packet_len = lenchars - GETBUF(pkt);
116 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
119 size_t max = maxmaxsize(lenbytes);
121 /* Internal API, so should not fail */
122 assert(buf != NULL && len > 0);
123 if (buf == NULL || len == 0)
126 pkt->staticbuf = buf;
128 pkt->maxsize = (max < len) ? max : len;
130 return wpacket_intern_init_len(pkt, lenbytes);
133 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
135 /* Internal API, so should not fail */
140 pkt->staticbuf = NULL;
142 pkt->maxsize = maxmaxsize(lenbytes);
144 return wpacket_intern_init_len(pkt, lenbytes);
147 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
149 return WPACKET_init_len(pkt, buf, 0);
152 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
154 /* Internal API, so should not fail */
155 assert(pkt->subs != NULL);
156 if (pkt->subs == NULL)
159 pkt->subs->flags = flags;
164 /* Store the |value| of length |len| at location |data| */
165 static int put_value(unsigned char *data, size_t value, size_t len)
167 for (data += len - 1; len > 0; len--) {
168 *data = (unsigned char)(value & 0xff);
173 /* Check whether we could fit the value in the assigned number of bytes */
182 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
183 * close a sub-packet and write out its length if necessary.
185 static int wpacket_intern_close(WPACKET *pkt)
187 WPACKET_SUB *sub = pkt->subs;
188 size_t packlen = pkt->written - sub->pwritten;
191 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
195 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
196 /* Deallocate any bytes allocated for the length of the WPACKET */
197 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
198 pkt->written -= sub->lenbytes;
199 pkt->curr -= sub->lenbytes;
202 /* Don't write out the packet length */
207 /* Write out the WPACKET length if needed */
208 if (sub->lenbytes > 0
209 && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
213 pkt->subs = sub->parent;
219 int WPACKET_close(WPACKET *pkt)
222 * Internal API, so should not fail - but we do negative testing of this
223 * so no assert (otherwise the tests fail)
225 if (pkt->subs == NULL || pkt->subs->parent == NULL)
228 return wpacket_intern_close(pkt);
231 int WPACKET_finish(WPACKET *pkt)
236 * Internal API, so should not fail - but we do negative testing of this
237 * so no assert (otherwise the tests fail)
239 if (pkt->subs == NULL || pkt->subs->parent != NULL)
242 ret = wpacket_intern_close(pkt);
244 OPENSSL_free(pkt->subs);
251 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
254 unsigned char *lenchars;
256 /* Internal API, so should not fail */
257 assert(pkt->subs != NULL);
258 if (pkt->subs == NULL)
261 sub = OPENSSL_zalloc(sizeof(*sub));
265 sub->parent = pkt->subs;
267 sub->pwritten = pkt->written + lenbytes;
268 sub->lenbytes = lenbytes;
275 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
277 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
278 sub->packet_len = lenchars - GETBUF(pkt);
283 int WPACKET_start_sub_packet(WPACKET *pkt)
285 return WPACKET_start_sub_packet_len__(pkt, 0);
288 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
292 /* Internal API, so should not fail */
293 assert(size <= sizeof(unsigned int));
295 if (size > sizeof(unsigned int)
296 || !WPACKET_allocate_bytes(pkt, size, &data)
297 || !put_value(data, val, size))
303 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
308 /* Internal API, so should not fail */
309 assert(pkt->subs != NULL);
310 if (pkt->subs == NULL)
313 /* Find the WPACKET_SUB for the top level */
314 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
317 lenbytes = sub->lenbytes;
319 lenbytes = sizeof(pkt->maxsize);
321 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
324 pkt->maxsize = maxsize;
329 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
336 if (!WPACKET_allocate_bytes(pkt, len, &dest))
339 memcpy(dest, src, len);
344 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
347 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
348 || !WPACKET_memcpy(pkt, src, len)
349 || !WPACKET_close(pkt))
355 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
357 /* Internal API, so should not fail */
358 assert(written != NULL);
362 *written = pkt->written;
367 int WPACKET_get_length(WPACKET *pkt, size_t *len)
369 /* Internal API, so should not fail */
370 assert(pkt->subs != NULL && len != NULL);
371 if (pkt->subs == NULL || len == NULL)
374 *len = pkt->written - pkt->subs->pwritten;
379 void WPACKET_cleanup(WPACKET *pkt)
381 WPACKET_SUB *sub, *parent;
383 for (sub = pkt->subs; sub != NULL; sub = parent) {
384 parent = sub->parent;