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) {
28 if (pkt->buf->length > SIZE_MAX / 2) {
31 newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE
32 : pkt->buf->length * 2;
34 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
37 *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
44 int WPACKET_sub_allocate_bytes(WPACKET *pkt, size_t len,
45 unsigned char **allocbytes, size_t lenbytes)
47 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
48 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
49 || !WPACKET_close(pkt))
55 static size_t maxmaxsize(size_t lenbytes)
57 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
60 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
63 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
65 unsigned char *lenchars;
67 /* Internal API, so should not fail */
75 pkt->maxsize = maxmaxsize(lenbytes);
77 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
78 if (pkt->subs == NULL)
84 pkt->subs->pwritten = lenbytes;
85 pkt->subs->lenbytes = lenbytes;
87 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
88 OPENSSL_free(pkt->subs);
92 pkt->subs->packet_len = lenchars - (unsigned char *)pkt->buf->data;
97 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
99 return WPACKET_init_len(pkt, buf, 0);
102 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
104 /* Internal API, so should not fail */
105 assert(pkt->subs != NULL);
106 if (pkt->subs == NULL)
109 pkt->subs->flags = flags;
114 /* Store the |value| of length |len| at location |data| */
115 static int put_value(unsigned char *data, size_t value, size_t len)
117 for (data += len - 1; len > 0; len--) {
118 *data = (unsigned char)(value & 0xff);
123 /* Check whether we could fit the value in the assigned number of bytes */
132 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
133 * close a sub-packet and write out its length if necessary.
135 static int wpacket_intern_close(WPACKET *pkt)
137 WPACKET_SUB *sub = pkt->subs;
138 size_t packlen = pkt->written - sub->pwritten;
141 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
145 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
146 /* Deallocate any bytes allocated for the length of the WPACKET */
147 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
148 pkt->written -= sub->lenbytes;
149 pkt->curr -= sub->lenbytes;
152 /* Don't write out the packet length */
157 /* Write out the WPACKET length if needed */
158 if (sub->lenbytes > 0
159 && !put_value((unsigned char *)&pkt->buf->data[sub->packet_len],
160 packlen, sub->lenbytes))
163 pkt->subs = sub->parent;
169 int WPACKET_close(WPACKET *pkt)
172 * Internal API, so should not fail - but we do negative testing of this
173 * so no assert (otherwise the tests fail)
175 if (pkt->subs == NULL || pkt->subs->parent == NULL)
178 return wpacket_intern_close(pkt);
181 int WPACKET_finish(WPACKET *pkt)
186 * Internal API, so should not fail - but we do negative testing of this
187 * so no assert (otherwise the tests fail)
189 if (pkt->subs == NULL || pkt->subs->parent != NULL)
192 ret = wpacket_intern_close(pkt);
194 OPENSSL_free(pkt->subs);
201 int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
204 unsigned char *lenchars;
206 /* Internal API, so should not fail */
207 assert(pkt->subs != NULL);
208 if (pkt->subs == NULL)
211 sub = OPENSSL_zalloc(sizeof(*sub));
215 sub->parent = pkt->subs;
217 sub->pwritten = pkt->written + lenbytes;
218 sub->lenbytes = lenbytes;
225 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
227 sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
232 int WPACKET_start_sub_packet(WPACKET *pkt)
234 return WPACKET_start_sub_packet_len(pkt, 0);
237 int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
241 /* Internal API, so should not fail */
242 assert(size <= sizeof(unsigned int));
244 if (size > sizeof(unsigned int)
245 || !WPACKET_allocate_bytes(pkt, size, &data)
246 || !put_value(data, val, size))
252 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
257 /* Internal API, so should not fail */
258 assert(pkt->subs != NULL);
259 if (pkt->subs == NULL)
262 /* Find the WPACKET_SUB for the top level */
263 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
266 lenbytes = sub->lenbytes;
268 lenbytes = sizeof(pkt->maxsize);
270 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
273 pkt->maxsize = maxsize;
278 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
285 if (!WPACKET_allocate_bytes(pkt, len, &dest))
288 memcpy(dest, src, len);
293 int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
295 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
296 || !WPACKET_memcpy(pkt, src, len)
297 || !WPACKET_close(pkt))
303 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
305 /* Internal API, so should not fail */
306 assert(written != NULL);
310 *written = pkt->written;
315 int WPACKET_get_length(WPACKET *pkt, size_t *len)
317 /* Internal API, so should not fail */
318 assert(pkt->subs != NULL && len != NULL);
319 if (pkt->subs == NULL || len == NULL)
322 *len = pkt->written - pkt->subs->pwritten;
327 void WPACKET_cleanup(WPACKET *pkt)
329 WPACKET_SUB *sub, *parent;
331 for (sub = pkt->subs; sub != NULL; sub = parent) {
332 parent = sub->parent;