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) \
38 ? (p)->staticbuf : (unsigned char *)(p)->buf->data)
40 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
42 /* Internal API, so should not fail */
43 if (!ossl_assert(pkt->subs != NULL && len != 0))
46 if (pkt->maxsize - pkt->written < len)
49 if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
53 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
55 if (reflen > SIZE_MAX / 2) {
59 if (newlen < DEFAULT_BUF_SIZE)
60 newlen = DEFAULT_BUF_SIZE;
62 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
65 if (allocbytes != NULL)
66 *allocbytes = WPACKET_get_curr(pkt);
71 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
72 unsigned char **allocbytes, size_t lenbytes)
74 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
77 *allocbytes += lenbytes;
82 static size_t maxmaxsize(size_t lenbytes)
84 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
87 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
90 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
92 unsigned char *lenchars;
97 if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
98 SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE);
105 pkt->subs->pwritten = lenbytes;
106 pkt->subs->lenbytes = lenbytes;
108 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
109 OPENSSL_free(pkt->subs);
113 pkt->subs->packet_len = lenchars - GETBUF(pkt);
118 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
121 size_t max = maxmaxsize(lenbytes);
123 /* Internal API, so should not fail */
124 if (!ossl_assert(buf != NULL && len > 0))
127 pkt->staticbuf = buf;
129 pkt->maxsize = (max < len) ? max : len;
131 return wpacket_intern_init_len(pkt, lenbytes);
134 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
136 /* Internal API, so should not fail */
137 if (!ossl_assert(buf != NULL))
140 pkt->staticbuf = NULL;
142 pkt->maxsize = maxmaxsize(lenbytes);
144 return wpacket_intern_init_len(pkt, lenbytes);
147 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
149 return WPACKET_init_len(pkt, buf, 0);
152 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
154 /* Internal API, so should not fail */
155 if (!ossl_assert(pkt->subs != NULL))
158 pkt->subs->flags = flags;
163 /* Store the |value| of length |len| at location |data| */
164 static int put_value(unsigned char *data, size_t value, size_t len)
166 for (data += len - 1; len > 0; len--) {
167 *data = (unsigned char)(value & 0xff);
172 /* Check whether we could fit the value in the assigned number of bytes */
181 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
182 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
183 * necessary. If |doclose| is 0 then it goes through the motions of closing
184 * (i.e. it fills in all the lengths), but doesn't actually close anything.
186 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
188 size_t packlen = pkt->written - sub->pwritten;
191 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
195 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
196 /* We can't handle this case. Return an error */
200 /* Deallocate any bytes allocated for the length of the WPACKET */
201 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
202 pkt->written -= sub->lenbytes;
203 pkt->curr -= sub->lenbytes;
206 /* Don't write out the packet length */
211 /* Write out the WPACKET length if needed */
212 if (sub->lenbytes > 0
213 && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
218 pkt->subs = sub->parent;
225 int WPACKET_fill_lengths(WPACKET *pkt)
229 if (!ossl_assert(pkt->subs != NULL))
232 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
233 if (!wpacket_intern_close(pkt, sub, 0))
240 int WPACKET_close(WPACKET *pkt)
243 * Internal API, so should not fail - but we do negative testing of this
244 * so no assert (otherwise the tests fail)
246 if (pkt->subs == NULL || pkt->subs->parent == NULL)
249 return wpacket_intern_close(pkt, pkt->subs, 1);
252 int WPACKET_finish(WPACKET *pkt)
257 * Internal API, so should not fail - but we do negative testing of this
258 * so no assert (otherwise the tests fail)
260 if (pkt->subs == NULL || pkt->subs->parent != NULL)
263 ret = wpacket_intern_close(pkt, pkt->subs, 1);
265 OPENSSL_free(pkt->subs);
272 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
275 unsigned char *lenchars;
277 /* Internal API, so should not fail */
278 if (!ossl_assert(pkt->subs != NULL))
281 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
282 SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
286 sub->parent = pkt->subs;
288 sub->pwritten = pkt->written + lenbytes;
289 sub->lenbytes = lenbytes;
296 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
298 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
299 sub->packet_len = lenchars - GETBUF(pkt);
304 int WPACKET_start_sub_packet(WPACKET *pkt)
306 return WPACKET_start_sub_packet_len__(pkt, 0);
309 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
313 /* Internal API, so should not fail */
314 if (!ossl_assert(size <= sizeof(unsigned int))
315 || !WPACKET_allocate_bytes(pkt, size, &data)
316 || !put_value(data, val, size))
322 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
327 /* Internal API, so should not fail */
328 if (!ossl_assert(pkt->subs != NULL))
331 /* Find the WPACKET_SUB for the top level */
332 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
335 lenbytes = sub->lenbytes;
337 lenbytes = sizeof(pkt->maxsize);
339 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
342 pkt->maxsize = maxsize;
347 int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
354 if (!WPACKET_allocate_bytes(pkt, len, &dest))
357 memset(dest, ch, len);
362 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
369 if (!WPACKET_allocate_bytes(pkt, len, &dest))
372 memcpy(dest, src, len);
377 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
380 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
381 || !WPACKET_memcpy(pkt, src, len)
382 || !WPACKET_close(pkt))
388 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
390 /* Internal API, so should not fail */
391 if (!ossl_assert(written != NULL))
394 *written = pkt->written;
399 int WPACKET_get_length(WPACKET *pkt, size_t *len)
401 /* Internal API, so should not fail */
402 if (!ossl_assert(pkt->subs != NULL && len != NULL))
405 *len = pkt->written - pkt->subs->pwritten;
410 unsigned char *WPACKET_get_curr(WPACKET *pkt)
412 return GETBUF(pkt) + pkt->curr;
415 void WPACKET_cleanup(WPACKET *pkt)
417 WPACKET_SUB *sub, *parent;
419 for (sub = pkt->subs; sub != NULL; sub = parent) {
420 parent = sub->parent;