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)
38 *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
44 static size_t maxmaxsize(size_t lenbytes)
46 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
49 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
52 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
54 unsigned char *lenchars;
56 /* Internal API, so should not fail */
64 pkt->maxsize = maxmaxsize(lenbytes);
66 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
67 if (pkt->subs == NULL)
73 pkt->subs->pwritten = lenbytes;
74 pkt->subs->lenbytes = lenbytes;
76 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
77 OPENSSL_free(pkt->subs);
81 pkt->subs->packet_len = lenchars - (unsigned char *)pkt->buf->data;
86 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
88 return WPACKET_init_len(pkt, buf, 0);
91 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
93 /* Internal API, so should not fail */
94 assert(pkt->subs != NULL);
95 if (pkt->subs == NULL)
98 pkt->subs->flags = flags;
103 /* Store the |value| of length |size| at location |data| */
104 static int put_value(unsigned char *data, size_t value, size_t size)
106 for (data += size - 1; size > 0; size--) {
107 *data = (unsigned char)(value & 0xff);
112 /* Check whether we could fit the value in the assigned number of bytes */
121 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
122 * close a sub-packet and write out its length if necessary.
124 static int wpacket_intern_close(WPACKET *pkt)
126 WPACKET_SUB *sub = pkt->subs;
127 size_t packlen = pkt->written - sub->pwritten;
130 && sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH)
134 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
135 /* Deallocate any bytes allocated for the length of the WPACKET */
136 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
137 pkt->written -= sub->lenbytes;
138 pkt->curr -= sub->lenbytes;
141 /* Don't write out the packet length */
146 /* Write out the WPACKET length if needed */
147 if (sub->lenbytes > 0
148 && !put_value((unsigned char *)&pkt->buf->data[sub->packet_len],
149 packlen, sub->lenbytes))
152 pkt->subs = sub->parent;
158 int WPACKET_close(WPACKET *pkt)
161 * Internal API, so should not fail - but we do negative testing of this
162 * so no assert (otherwise the tests fail)
164 if (pkt->subs == NULL || pkt->subs->parent == NULL)
167 return wpacket_intern_close(pkt);
170 int WPACKET_finish(WPACKET *pkt)
175 * Internal API, so should not fail - but we do negative testing of this
176 * so no assert (otherwise the tests fail)
178 if (pkt->subs == NULL || pkt->subs->parent != NULL)
181 ret = wpacket_intern_close(pkt);
183 OPENSSL_free(pkt->subs);
190 int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
193 unsigned char *lenchars;
195 /* Internal API, so should not fail */
196 assert(pkt->subs != NULL);
197 if (pkt->subs == NULL)
200 sub = OPENSSL_zalloc(sizeof(*sub));
204 sub->parent = pkt->subs;
206 sub->pwritten = pkt->written + lenbytes;
207 sub->lenbytes = lenbytes;
214 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
216 sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
221 int WPACKET_start_sub_packet(WPACKET *pkt)
223 return WPACKET_start_sub_packet_len(pkt, 0);
226 int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
230 /* Internal API, so should not fail */
231 assert(size <= sizeof(unsigned int));
232 if (size > sizeof(unsigned int)
233 || !WPACKET_allocate_bytes(pkt, size, &data)
234 || !put_value(data, val, size))
240 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
245 /* Internal API, so should not fail */
246 assert(pkt->subs != NULL);
247 if (pkt->subs == NULL)
250 /* Find the WPACKET_SUB for the top level */
251 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
254 lenbytes = sub->lenbytes;
256 lenbytes = sizeof(pkt->maxsize);
258 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
261 pkt->maxsize = maxsize;
266 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
273 if (!WPACKET_allocate_bytes(pkt, len, &dest))
276 memcpy(dest, src, len);
281 int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
283 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
284 || !WPACKET_memcpy(pkt, src, len)
285 || !WPACKET_close(pkt))
291 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
293 /* Internal API, so should not fail */
294 assert(written != NULL);
298 *written = pkt->written;
303 int WPACKET_get_length(WPACKET *pkt, size_t *len)
305 /* Internal API, so should not fail */
306 assert(pkt->subs != NULL && len != NULL);
307 if (pkt->subs == NULL || len == NULL)
310 *len = pkt->written - pkt->subs->pwritten;
315 void WPACKET_cleanup(WPACKET *pkt)
317 WPACKET_SUB *sub, *parent;
319 for (sub = pkt->subs; sub != NULL; sub = parent) {
320 parent = sub->parent;