2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "internal/cryptlib.h"
11 #include "internal/packet.h"
12 #include <openssl/sslerr.h>
14 #define DEFAULT_BUF_SIZE 256
16 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
18 if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
26 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
27 unsigned char **allocbytes, size_t lenbytes)
29 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
30 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
31 || !WPACKET_close(pkt))
37 #define GETBUF(p) (((p)->staticbuf != NULL) \
40 ? (unsigned char *)(p)->buf->data \
43 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
45 /* Internal API, so should not fail */
46 if (!ossl_assert(pkt->subs != NULL && len != 0))
49 if (pkt->maxsize - pkt->written < len)
52 if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
56 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
58 if (reflen > SIZE_MAX / 2) {
62 if (newlen < DEFAULT_BUF_SIZE)
63 newlen = DEFAULT_BUF_SIZE;
65 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
68 if (allocbytes != NULL)
69 *allocbytes = WPACKET_get_curr(pkt);
74 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
75 unsigned char **allocbytes, size_t lenbytes)
77 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
80 if (*allocbytes != NULL)
81 *allocbytes += lenbytes;
86 static size_t maxmaxsize(size_t lenbytes)
88 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
91 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
94 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
96 unsigned char *lenchars;
101 if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
102 SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE);
109 pkt->subs->pwritten = lenbytes;
110 pkt->subs->lenbytes = lenbytes;
112 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
113 OPENSSL_free(pkt->subs);
117 pkt->subs->packet_len = 0;
122 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
125 size_t max = maxmaxsize(lenbytes);
127 /* Internal API, so should not fail */
128 if (!ossl_assert(buf != NULL && len > 0))
131 pkt->staticbuf = buf;
133 pkt->maxsize = (max < len) ? max : len;
135 return wpacket_intern_init_len(pkt, lenbytes);
138 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
140 /* Internal API, so should not fail */
141 if (!ossl_assert(buf != NULL))
144 pkt->staticbuf = NULL;
146 pkt->maxsize = maxmaxsize(lenbytes);
148 return wpacket_intern_init_len(pkt, lenbytes);
151 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
153 return WPACKET_init_len(pkt, buf, 0);
156 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
158 pkt->staticbuf = NULL;
160 pkt->maxsize = maxmaxsize(lenbytes);
162 return wpacket_intern_init_len(pkt, 0);
165 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
167 /* Internal API, so should not fail */
168 if (!ossl_assert(pkt->subs != NULL))
171 pkt->subs->flags = flags;
176 /* Store the |value| of length |len| at location |data| */
177 static int put_value(unsigned char *data, size_t value, size_t len)
182 for (data += len - 1; len > 0; len--) {
183 *data = (unsigned char)(value & 0xff);
188 /* Check whether we could fit the value in the assigned number of bytes */
197 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
198 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
199 * necessary. If |doclose| is 0 then it goes through the motions of closing
200 * (i.e. it fills in all the lengths), but doesn't actually close anything.
202 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
204 size_t packlen = pkt->written - sub->pwritten;
207 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
211 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
212 /* We can't handle this case. Return an error */
216 /* Deallocate any bytes allocated for the length of the WPACKET */
217 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
218 pkt->written -= sub->lenbytes;
219 pkt->curr -= sub->lenbytes;
222 /* Don't write out the packet length */
227 /* Write out the WPACKET length if needed */
228 if (sub->lenbytes > 0) {
229 unsigned char *buf = GETBUF(pkt);
232 && !put_value(&buf[sub->packet_len], packlen,
238 pkt->subs = sub->parent;
245 int WPACKET_fill_lengths(WPACKET *pkt)
249 if (!ossl_assert(pkt->subs != NULL))
252 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
253 if (!wpacket_intern_close(pkt, sub, 0))
260 int WPACKET_close(WPACKET *pkt)
263 * Internal API, so should not fail - but we do negative testing of this
264 * so no assert (otherwise the tests fail)
266 if (pkt->subs == NULL || pkt->subs->parent == NULL)
269 return wpacket_intern_close(pkt, pkt->subs, 1);
272 int WPACKET_finish(WPACKET *pkt)
277 * Internal API, so should not fail - but we do negative testing of this
278 * so no assert (otherwise the tests fail)
280 if (pkt->subs == NULL || pkt->subs->parent != NULL)
283 ret = wpacket_intern_close(pkt, pkt->subs, 1);
285 OPENSSL_free(pkt->subs);
292 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
295 unsigned char *lenchars;
297 /* Internal API, so should not fail */
298 if (!ossl_assert(pkt->subs != NULL))
301 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
302 SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
306 sub->parent = pkt->subs;
308 sub->pwritten = pkt->written + lenbytes;
309 sub->lenbytes = lenbytes;
316 sub->packet_len = pkt->written;
318 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
324 int WPACKET_start_sub_packet(WPACKET *pkt)
326 return WPACKET_start_sub_packet_len__(pkt, 0);
329 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
333 /* Internal API, so should not fail */
334 if (!ossl_assert(size <= sizeof(unsigned int))
335 || !WPACKET_allocate_bytes(pkt, size, &data)
336 || !put_value(data, val, size))
342 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
347 /* Internal API, so should not fail */
348 if (!ossl_assert(pkt->subs != NULL))
351 /* Find the WPACKET_SUB for the top level */
352 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
355 lenbytes = sub->lenbytes;
357 lenbytes = sizeof(pkt->maxsize);
359 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
362 pkt->maxsize = maxsize;
367 int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
374 if (!WPACKET_allocate_bytes(pkt, len, &dest))
378 memset(dest, ch, len);
383 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
390 if (!WPACKET_allocate_bytes(pkt, len, &dest))
394 memcpy(dest, src, len);
399 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
402 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
403 || !WPACKET_memcpy(pkt, src, len)
404 || !WPACKET_close(pkt))
410 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
412 /* Internal API, so should not fail */
413 if (!ossl_assert(written != NULL))
416 *written = pkt->written;
421 int WPACKET_get_length(WPACKET *pkt, size_t *len)
423 /* Internal API, so should not fail */
424 if (!ossl_assert(pkt->subs != NULL && len != NULL))
427 *len = pkt->written - pkt->subs->pwritten;
432 unsigned char *WPACKET_get_curr(WPACKET *pkt)
434 unsigned char *buf = GETBUF(pkt);
439 return buf + pkt->curr;
442 int WPACKET_is_null_buf(WPACKET *pkt)
444 return pkt->buf == NULL && pkt->staticbuf == NULL;
447 void WPACKET_cleanup(WPACKET *pkt)
449 WPACKET_SUB *sub, *parent;
451 for (sub = pkt->subs; sub != NULL; sub = parent) {
452 parent = sub->parent;