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;
105 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
106 * close a sub-packet and write out its length if necessary.
108 static int wpacket_intern_close(WPACKET *pkt)
110 WPACKET_SUB *sub = pkt->subs;
111 size_t packlen = pkt->written - sub->pwritten;
114 && sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH)
118 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
119 /* Deallocate any bytes allocated for the length of the WPACKET */
120 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
121 pkt->written -= sub->lenbytes;
122 pkt->curr -= sub->lenbytes;
125 /* Don't write out the packet length */
130 /* Write out the WPACKET length if needed */
131 if (sub->lenbytes > 0) {
134 for (lenbytes = sub->lenbytes; lenbytes > 0; lenbytes--) {
135 pkt->buf->data[sub->packet_len + lenbytes - 1]
136 = (unsigned char)(packlen & 0xff);
141 * We've extended beyond the max allowed for the number of len bytes
147 pkt->subs = sub->parent;
153 int WPACKET_close(WPACKET *pkt)
156 * Internal API, so should not fail - but we do negative testing of this
157 * so no assert (otherwise the tests fail)
159 if (pkt->subs == NULL || pkt->subs->parent == NULL)
162 return wpacket_intern_close(pkt);
165 int WPACKET_finish(WPACKET *pkt)
170 * Internal API, so should not fail - but we do negative testing of this
171 * so no assert (otherwise the tests fail)
173 if (pkt->subs == NULL || pkt->subs->parent != NULL)
176 ret = wpacket_intern_close(pkt);
178 OPENSSL_free(pkt->subs);
185 int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
188 unsigned char *lenchars;
190 /* Internal API, so should not fail */
191 assert(pkt->subs != NULL);
192 if (pkt->subs == NULL)
195 sub = OPENSSL_zalloc(sizeof(*sub));
199 sub->parent = pkt->subs;
201 sub->pwritten = pkt->written + lenbytes;
202 sub->lenbytes = lenbytes;
209 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
211 sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
216 int WPACKET_start_sub_packet(WPACKET *pkt)
218 return WPACKET_start_sub_packet_len(pkt, 0);
221 int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
225 /* Internal API, so should not fail */
226 assert(size <= sizeof(unsigned int));
227 if (size > sizeof(unsigned int)
228 || !WPACKET_allocate_bytes(pkt, size, &data))
231 for (data += size - 1; size > 0; size--) {
232 *data = (unsigned char)(val & 0xff);
237 /* Check whether we could fit the value in the assigned number of bytes */
244 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
249 /* Internal API, so should not fail */
250 assert(pkt->subs != NULL);
251 if (pkt->subs == NULL)
254 /* Find the WPACKET_SUB for the top level */
255 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
258 lenbytes = sub->lenbytes;
260 lenbytes = sizeof(pkt->maxsize);
262 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
265 pkt->maxsize = maxsize;
270 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
277 if (!WPACKET_allocate_bytes(pkt, len, &dest))
280 memcpy(dest, src, len);
285 int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
287 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
288 || !WPACKET_memcpy(pkt, src, len)
289 || !WPACKET_close(pkt))
295 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
297 /* Internal API, so should not fail */
298 assert(written != NULL);
302 *written = pkt->written;
307 int WPACKET_get_length(WPACKET *pkt, size_t *len)
309 /* Internal API, so should not fail */
310 assert(pkt->subs != NULL && len != NULL);
311 if (pkt->subs == NULL || len == NULL)
314 *len = pkt->written - pkt->subs->pwritten;
319 void WPACKET_cleanup(WPACKET *pkt)
321 WPACKET_SUB *sub, *parent;
323 for (sub = pkt->subs; sub != NULL; sub = parent) {
324 parent = sub->parent;