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"
13 * Allocate bytes in the WPACKET for the output. This reserves the bytes
14 * and count them as "written", but doesn't actually do the writing.
16 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
18 if (pkt->subs == NULL || len == 0)
21 if (SIZE_MAX - pkt->written < len)
24 if (pkt->maxsize > 0 && pkt->written + len > pkt->maxsize)
27 if (pkt->buf->length - pkt->written < len) {
30 if (pkt->buf->length > SIZE_MAX / 2)
33 newlen = pkt->buf->length * 2;
34 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
38 *allocbytes = pkt->curr;
45 * Initialise a WPACKET with the buffer in |buf|. The buffer must exist
46 * for the whole time that the WPACKET is being used. Additionally |lenbytes| of
47 * data is preallocated at the start of the buffer to store the length of the
48 * WPACKET once we know it.
50 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
57 pkt->curr = (unsigned char *)buf->data;
61 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
62 if (pkt->subs == NULL)
68 pkt->subs->pwritten = lenbytes;
69 pkt->subs->lenbytes = lenbytes;
71 if (!WPACKET_allocate_bytes(pkt, lenbytes, &(pkt->subs->packet_len))) {
72 OPENSSL_free(pkt->subs);
81 * Same as WPACKET_init_len except there is no preallocation of the WPACKET
84 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
86 return WPACKET_init_len(pkt, buf, 0);
90 * Set the WPACKET length, and the location for where we should write that
91 * length. Normally this will be at the start of the WPACKET, and therefore
92 * the WPACKET would have been initialised via WPACKET_init_len(). However there
93 * is the possibility that the length needs to be written to some other location
94 * other than the start of the WPACKET. In that case init via WPACKET_init() and
95 * then set the location for the length using this function.
97 int WPACKET_set_packet_len(WPACKET *pkt, unsigned char *packet_len,
100 /* We only allow this to be set once */
101 if (pkt->subs == NULL)
104 pkt->subs->lenbytes = lenbytes;
105 pkt->subs->packet_len = packet_len;
110 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
112 if (pkt->subs == NULL)
115 pkt->subs->flags = flags;
122 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
123 * close a sub-packet and write out its length if necessary.
125 static int wpacket_intern_close(WPACKET *pkt)
128 WPACKET_SUB *sub = pkt->subs;
130 packlen = pkt->written - sub->pwritten;
132 && sub->flags & OPENSSL_WPACKET_FLAGS_NON_ZERO_LENGTH)
136 && sub->flags & OPENSSL_WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
137 /* Deallocate any bytes allocated for the length of the WPACKET */
138 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
139 pkt->written -= sub->lenbytes;
140 pkt->curr -= sub->lenbytes;
143 /* Don't write out the packet length */
144 sub->packet_len = NULL;
147 /* Write out the WPACKET length if needed */
148 if (sub->packet_len != NULL) {
151 lenbytes = sub->lenbytes;
153 for (; lenbytes > 0; lenbytes--) {
154 sub->packet_len[lenbytes - 1]
155 = (unsigned char)(packlen & 0xff);
160 * We've extended beyond the max allowed for the number of len bytes
166 pkt->subs = sub->parent;
173 * Closes the most recent sub-packet. It also writes out the length of the
174 * packet to the required location (normally the start of the WPACKET) if
175 * appropriate. The top level WPACKET should be closed using WPACKET_finish()
176 * instead of this function.
178 int WPACKET_close(WPACKET *pkt)
180 if (pkt->subs == NULL || pkt->subs->parent == NULL)
183 return wpacket_intern_close(pkt);
187 * The same as WPACKET_close() but only for the top most WPACKET. Additionally
188 * frees memory resources for this WPACKET.
190 int WPACKET_finish(WPACKET *pkt)
194 if (pkt->subs == NULL || pkt->subs->parent != NULL)
197 ret = wpacket_intern_close(pkt);
199 /* We free up memory no matter whether |ret| is zero or not */
200 OPENSSL_free(pkt->subs);
206 * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
207 * at the start of the sub-packet to store its length once we know it.
209 int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
213 if (pkt->subs == NULL)
216 sub = OPENSSL_zalloc(sizeof(*sub));
220 sub->parent = pkt->subs;
222 sub->pwritten = pkt->written + lenbytes;
223 sub->lenbytes = lenbytes;
226 sub->packet_len = NULL;
230 if (!WPACKET_allocate_bytes(pkt, lenbytes, &sub->packet_len)) {
238 * Same as WPACKET_get_sub_packet_len() except no bytes are pre-allocated for
239 * the sub-packet length.
241 int WPACKET_start_sub_packet(WPACKET *pkt)
243 return WPACKET_start_sub_packet_len(pkt, 0);
247 * Write the value stored in |val| into the WPACKET. The value will consome
248 * |bytes| amount of storage. An error will occur if |val| cannot be accommdated
249 * in |bytes| storage, e.g. attempting to write the value 256 into 1 byte will
252 int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t bytes)
256 if (bytes > sizeof(unsigned int)
257 || !WPACKET_allocate_bytes(pkt, bytes, &data))
261 for (; bytes > 0; bytes--) {
262 *data = (unsigned char)(val & 0xff);
267 /* Check whether we could fit the value in the assigned number of bytes */
275 * Set a maximum size that we will not allow the WPACKET to grow beyond. If not
276 * set then there is no maximum.
278 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
280 pkt->maxsize = maxsize;
286 * Copy |len| bytes of data from |*src| into the WPACKET.
288 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
295 if (!WPACKET_allocate_bytes(pkt, len, &dest))
298 memcpy(dest, src, len);
304 * Return the total number of bytes written so far to the underlying buffer.
305 * This might includes bytes written by a parent WPACKET.
307 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
309 if (pkt->subs == NULL || written == NULL)
312 *written = pkt->written;
318 * Returns the length of the last sub-packet. This excludes any bytes allocated
319 * for the length itself.
321 int WPACKET_get_length(WPACKET *pkt, size_t *len)
323 if (pkt->subs == NULL || len == NULL)
326 *len = pkt->written - pkt->subs->pwritten;