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
10 #include "packet_locl.h"
12 #define DEFAULT_BUF_SIZE 256
14 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
16 if (pkt->subs == NULL || len == 0)
19 if (pkt->maxsize - pkt->written < len)
22 if (pkt->buf->length - pkt->written < len) {
25 if (pkt->buf->length > SIZE_MAX / 2) {
28 if (pkt->buf->length == 0)
29 newlen = DEFAULT_BUF_SIZE;
31 newlen = pkt->buf->length * 2;
33 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
35 if (pkt->curr == NULL) {
37 * Can happen if initialised with a BUF_MEM that hasn't been
40 pkt->curr = (unsigned char *)pkt->buf->data;
44 *allocbytes = pkt->curr;
50 static size_t maxmaxsize(size_t lenbytes)
52 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
55 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
58 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
65 pkt->curr = (unsigned char *)buf->data;
67 pkt->maxsize = maxmaxsize(lenbytes);
69 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
70 if (pkt->subs == NULL)
76 pkt->subs->pwritten = lenbytes;
77 pkt->subs->lenbytes = lenbytes;
79 if (!WPACKET_allocate_bytes(pkt, lenbytes, &(pkt->subs->packet_len))) {
80 OPENSSL_free(pkt->subs);
88 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
90 return WPACKET_init_len(pkt, buf, 0);
93 int WPACKET_set_packet_len(WPACKET *pkt, unsigned char *packet_len,
98 /* We only allow this to be set once */
99 if (pkt->subs == NULL || pkt->subs->lenbytes != 0)
102 pkt->subs->lenbytes = lenbytes;
103 pkt->subs->packet_len = packet_len;
105 maxmax = maxmaxsize(lenbytes);
106 if (pkt->maxsize > maxmax)
107 pkt->maxsize = maxmax;
112 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
114 if (pkt->subs == NULL)
117 pkt->subs->flags = flags;
124 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
125 * close a sub-packet and write out its length if necessary.
127 static int wpacket_intern_close(WPACKET *pkt)
130 WPACKET_SUB *sub = pkt->subs;
132 packlen = pkt->written - sub->pwritten;
134 && sub->flags & OPENSSL_WPACKET_FLAGS_NON_ZERO_LENGTH)
138 && sub->flags & OPENSSL_WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
139 /* Deallocate any bytes allocated for the length of the WPACKET */
140 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
141 pkt->written -= sub->lenbytes;
142 pkt->curr -= sub->lenbytes;
145 /* Don't write out the packet length */
146 sub->packet_len = NULL;
149 /* Write out the WPACKET length if needed */
150 if (sub->packet_len != NULL) {
153 lenbytes = sub->lenbytes;
155 for (; lenbytes > 0; lenbytes--) {
156 sub->packet_len[lenbytes - 1]
157 = (unsigned char)(packlen & 0xff);
162 * We've extended beyond the max allowed for the number of len bytes
168 pkt->subs = sub->parent;
174 int WPACKET_close(WPACKET *pkt)
176 if (pkt->subs == NULL || pkt->subs->parent == NULL)
179 return wpacket_intern_close(pkt);
182 int WPACKET_finish(WPACKET *pkt)
186 if (pkt->subs == NULL || pkt->subs->parent != NULL)
189 ret = wpacket_intern_close(pkt);
192 OPENSSL_free(pkt->subs);
198 int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
202 if (pkt->subs == NULL)
205 sub = OPENSSL_zalloc(sizeof(*sub));
209 sub->parent = pkt->subs;
211 sub->pwritten = pkt->written + lenbytes;
212 sub->lenbytes = lenbytes;
215 sub->packet_len = NULL;
219 if (!WPACKET_allocate_bytes(pkt, lenbytes, &sub->packet_len)) {
226 int WPACKET_start_sub_packet(WPACKET *pkt)
228 return WPACKET_start_sub_packet_len(pkt, 0);
231 int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t bytes)
235 if (bytes > sizeof(unsigned int)
236 || !WPACKET_allocate_bytes(pkt, bytes, &data))
240 for (; bytes > 0; bytes--) {
241 *data = (unsigned char)(val & 0xff);
246 /* Check whether we could fit the value in the assigned number of bytes */
253 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
258 if (pkt->subs == NULL)
261 /* Find the WPACKET_SUB for the top level */
262 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent);
264 lenbytes = sub->lenbytes;
266 lenbytes = sizeof(pkt->maxsize);
268 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
271 pkt->maxsize = maxsize;
276 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
283 if (!WPACKET_allocate_bytes(pkt, len, &dest))
286 memcpy(dest, src, len);
291 int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
293 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
294 || !WPACKET_memcpy(pkt, src, len)
295 || !WPACKET_close(pkt))
301 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
306 *written = pkt->written;
311 int WPACKET_get_length(WPACKET *pkt, size_t *len)
313 if (pkt->subs == NULL || len == NULL)
316 *len = pkt->written - pkt->subs->pwritten;
321 void WPACKET_cleanup(WPACKET *pkt)
323 WPACKET_SUB *sub, *parent;
325 for (sub = pkt->subs; sub != NULL; sub = parent) {
326 parent = sub->parent;