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_BUF for the output. This reserves the bytes
14 * and count them as "written", but doesn't actually do the writing.
16 static unsigned char *WPACKET_BUF_allocate(WPACKET_BUF *wbuf, size_t len)
18 unsigned char *ret = wbuf->curr;
20 if (SIZE_MAX - wbuf->written < len)
23 if (wbuf->maxsize > 0 && wbuf->written + len > wbuf->maxsize)
26 if (wbuf->buf->length - wbuf->written < len) {
29 if (wbuf->buf->length > SIZE_MAX / 2)
32 newlen = wbuf->buf->length * 2;
33 if (BUF_MEM_grow(wbuf->buf, newlen) == 0)
43 * Initialise a WPACKET with the buffer in |buf|. The buffer must exist
44 * for the whole time that the WPACKET is being used. Additionally |lenbytes| of
45 * data is preallocated at the start of the buffer to store the length of the
46 * WPACKET once we know it.
48 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
55 wbuf = OPENSSL_zalloc(sizeof(WPACKET_BUF));
62 wbuf->curr = (unsigned char *)buf->data;
68 pkt->pwritten = lenbytes;
69 pkt->lenbytes = lenbytes;
74 pkt->packet_len = NULL;
78 pkt->packet_len = WPACKET_BUF_allocate(wbuf, lenbytes);
79 if (pkt->packet_len == NULL) {
90 * Same as WPACKET_init_len except there is no preallocation of the WPACKET
93 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
95 return WPACKET_init_len(pkt, buf, 0);
99 * Set the WPACKET length, and the location for where we should write that
100 * length. Normally this will be at the start of the WPACKET, and therefore
101 * the WPACKET would have been initialised via WPACKET_init_len(). However there
102 * is the possibility that the length needs to be written to some other location
103 * other than the start of the WPACKET. In that case init via WPACKET_init() and
104 * then set the location for the length using this function.
106 int WPACKET_set_packet_len(WPACKET *pkt, unsigned char *packet_len,
109 /* We only allow this to be set once */
110 if (pkt->isclosed || pkt->packet_len != NULL)
113 pkt->lenbytes = lenbytes;
114 pkt->packet_len = packet_len;
119 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
127 * Closes the WPACKET and marks it as invalid for future writes. It also writes
128 * out the length of the packet to the required location (normally the start
129 * of the WPACKET) if appropriate. A WPACKET cannot be closed if it has an
132 int WPACKET_close(WPACKET *pkt)
136 if (pkt->isclosed || pkt->haschild)
139 packlen = pkt->wbuf->written - pkt->pwritten;
140 if (packlen == 0 && pkt->flags & OPENSSL_WPACKET_FLAGS_NON_ZERO_LENGTH)
144 && pkt->flags & OPENSSL_WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
145 /* Deallocate any bytes allocated for the length of the WPACKET */
146 if ((pkt->wbuf->curr - pkt->lenbytes) == pkt->packet_len) {
147 pkt->wbuf->written -= pkt->lenbytes;
148 pkt->wbuf->curr -= pkt->lenbytes;
151 /* Don't write out the packet length */
152 pkt->packet_len = NULL;
155 /* Write out the WPACKET length if needed */
156 if (pkt->packet_len != NULL) {
159 lenbytes = pkt->lenbytes;
161 for (; lenbytes > 0; lenbytes--) {
162 pkt->packet_len[lenbytes - 1] = (unsigned char)(packlen & 0xff);
167 * We've extended beyond the max allowed for the number of len bytes
173 if (pkt->parent != NULL) {
174 if (pkt->parent->haschild != 1) {
175 /* Should not happen! */
178 pkt->parent->haschild = 0;
188 * Initialise a new sub-packet (|subpkt|), based on a parent (|pkt|).
189 * Additionally |lenbytes| of data is preallocated at the start of the
190 * sub-packet to store its length once we know it.
192 int WPACKET_get_sub_packet_len(WPACKET *pkt, WPACKET *subpkt, size_t lenbytes)
194 if (pkt->isclosed || pkt->haschild || subpkt == NULL)
197 subpkt->parent = pkt;
198 subpkt->wbuf = pkt->wbuf;
199 subpkt->pwritten = pkt->wbuf->written + lenbytes;
200 subpkt->lenbytes = lenbytes;
201 subpkt->haschild = 0;
202 subpkt->isclosed = 0;
205 subpkt->packet_len = NULL;
210 subpkt->packet_len = WPACKET_BUF_allocate(pkt->wbuf, lenbytes);
211 if (subpkt->packet_len == NULL) {
212 subpkt->isclosed = 1;
222 * Same as WPACKET_get_sub_packet_len() except no bytes are pre-allocated for
223 * the sub-packet length.
225 int WPACKET_get_sub_packet(WPACKET *pkt, WPACKET *subpkt)
227 return WPACKET_get_sub_packet_len(pkt, subpkt, 0);
231 * Allocate some bytes in the WPACKET for writing. That number of bytes is
232 * marked as having been written, and a pointer to their location is stored in
235 int WPACKET_allocate_bytes(WPACKET *pkt, size_t bytes,
236 unsigned char **allocbytes)
240 if (pkt->isclosed || pkt->haschild || bytes == 0)
243 data = WPACKET_BUF_allocate(pkt->wbuf, bytes);
253 * Write the value stored in |val| into the WPACKET. The value will consome
254 * |bytes| amount of storage. An error will occur if |val| cannot be accommdated
255 * in |bytes| storage, e.g. attempting to write the value 256 into 1 byte will
258 int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t bytes)
262 if (bytes > sizeof(unsigned int)
263 || !WPACKET_allocate_bytes(pkt, bytes, &data))
267 for (; bytes > 0; bytes--) {
268 *data = (unsigned char)(val & 0xff);
273 /* Check whether we could fit the value in the assigned number of bytes */
281 * Set a maximum size that we will not allow the WPACKET to grow beyond. If not
282 * set then there is no maximum.
284 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
286 pkt->wbuf->maxsize = maxsize;
292 * Copy |len| bytes of data from |*src| into the WPACKET.
294 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
301 if (!WPACKET_allocate_bytes(pkt, len, &dest))
304 memcpy(dest, src, len);
310 * Return the total number of bytes written so far to the underlying buffer.
311 * This might includes bytes written by a parent WPACKET.
313 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
315 if (pkt->isclosed || written == NULL)
318 *written = pkt->wbuf->curr - (unsigned char *)pkt->wbuf->buf->data;
324 * Returns the length of this WPACKET so far. This excludes any bytes allocated
325 * for the length itself.
327 int WPACKET_get_length(WPACKET *pkt, size_t *len)
329 if (pkt->isclosed || len == NULL)
332 *len = pkt->wbuf->written - pkt->pwritten;