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 /* Internal API, so should not fail */
18 assert(pkt->subs != NULL && len != 0);
19 if (pkt->subs == NULL || len == 0)
22 if (pkt->maxsize - pkt->written < len)
25 if (pkt->buf->length - pkt->written < len) {
29 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
31 if (reflen > SIZE_MAX / 2) {
35 if (newlen < DEFAULT_BUF_SIZE)
36 newlen = DEFAULT_BUF_SIZE;
38 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
41 *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
48 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
49 unsigned char **allocbytes, size_t lenbytes)
51 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
52 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
53 || !WPACKET_close(pkt))
59 static size_t maxmaxsize(size_t lenbytes)
61 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
64 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
67 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
69 unsigned char *lenchars;
71 /* Internal API, so should not fail */
79 pkt->maxsize = maxmaxsize(lenbytes);
81 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
82 if (pkt->subs == NULL)
88 pkt->subs->pwritten = lenbytes;
89 pkt->subs->lenbytes = lenbytes;
91 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
92 OPENSSL_free(pkt->subs);
96 pkt->subs->packet_len = lenchars - (unsigned char *)pkt->buf->data;
101 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
103 return WPACKET_init_len(pkt, buf, 0);
106 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
108 /* Internal API, so should not fail */
109 assert(pkt->subs != NULL);
110 if (pkt->subs == NULL)
113 pkt->subs->flags = flags;
118 /* Store the |value| of length |len| at location |data| */
119 static int put_value(unsigned char *data, size_t value, size_t len)
121 for (data += len - 1; len > 0; len--) {
122 *data = (unsigned char)(value & 0xff);
127 /* Check whether we could fit the value in the assigned number of bytes */
136 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
137 * close a sub-packet and write out its length if necessary.
139 static int wpacket_intern_close(WPACKET *pkt)
141 WPACKET_SUB *sub = pkt->subs;
142 size_t packlen = pkt->written - sub->pwritten;
145 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
149 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
150 /* Deallocate any bytes allocated for the length of the WPACKET */
151 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
152 pkt->written -= sub->lenbytes;
153 pkt->curr -= sub->lenbytes;
156 /* Don't write out the packet length */
161 /* Write out the WPACKET length if needed */
162 if (sub->lenbytes > 0
163 && !put_value((unsigned char *)&pkt->buf->data[sub->packet_len],
164 packlen, sub->lenbytes))
167 pkt->subs = sub->parent;
173 int WPACKET_close(WPACKET *pkt)
176 * Internal API, so should not fail - but we do negative testing of this
177 * so no assert (otherwise the tests fail)
179 if (pkt->subs == NULL || pkt->subs->parent == NULL)
182 return wpacket_intern_close(pkt);
185 int WPACKET_finish(WPACKET *pkt)
190 * Internal API, so should not fail - but we do negative testing of this
191 * so no assert (otherwise the tests fail)
193 if (pkt->subs == NULL || pkt->subs->parent != NULL)
196 ret = wpacket_intern_close(pkt);
198 OPENSSL_free(pkt->subs);
205 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
208 unsigned char *lenchars;
210 /* Internal API, so should not fail */
211 assert(pkt->subs != NULL);
212 if (pkt->subs == NULL)
215 sub = OPENSSL_zalloc(sizeof(*sub));
219 sub->parent = pkt->subs;
221 sub->pwritten = pkt->written + lenbytes;
222 sub->lenbytes = lenbytes;
229 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
231 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
232 sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
237 int WPACKET_start_sub_packet(WPACKET *pkt)
239 return WPACKET_start_sub_packet_len__(pkt, 0);
242 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
246 /* Internal API, so should not fail */
247 assert(size <= sizeof(unsigned int));
249 if (size > sizeof(unsigned int)
250 || !WPACKET_allocate_bytes(pkt, size, &data)
251 || !put_value(data, val, size))
257 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
262 /* Internal API, so should not fail */
263 assert(pkt->subs != NULL);
264 if (pkt->subs == NULL)
267 /* Find the WPACKET_SUB for the top level */
268 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
271 lenbytes = sub->lenbytes;
273 lenbytes = sizeof(pkt->maxsize);
275 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
278 pkt->maxsize = maxsize;
283 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
290 if (!WPACKET_allocate_bytes(pkt, len, &dest))
293 memcpy(dest, src, len);
298 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
301 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
302 || !WPACKET_memcpy(pkt, src, len)
303 || !WPACKET_close(pkt))
309 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
311 /* Internal API, so should not fail */
312 assert(written != NULL);
316 *written = pkt->written;
321 int WPACKET_get_length(WPACKET *pkt, size_t *len)
323 /* Internal API, so should not fail */
324 assert(pkt->subs != NULL && len != NULL);
325 if (pkt->subs == NULL || len == NULL)
328 *len = pkt->written - pkt->subs->pwritten;
333 void WPACKET_cleanup(WPACKET *pkt)
335 WPACKET_SUB *sub, *parent;
337 for (sub = pkt->subs; sub != NULL; sub = parent) {
338 parent = sub->parent;