2 * Copyright 2015-2020 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);
70 if (pkt->endfirst && *allocbytes != NULL)
77 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
78 unsigned char **allocbytes, size_t lenbytes)
80 if (pkt->endfirst && lenbytes > 0)
83 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
86 if (*allocbytes != NULL)
87 *allocbytes += lenbytes;
92 static size_t maxmaxsize(size_t lenbytes)
94 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
97 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
100 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
102 unsigned char *lenchars;
107 if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
108 SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE);
115 pkt->subs->pwritten = lenbytes;
116 pkt->subs->lenbytes = lenbytes;
118 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
119 OPENSSL_free(pkt->subs);
123 pkt->subs->packet_len = 0;
128 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
131 size_t max = maxmaxsize(lenbytes);
133 /* Internal API, so should not fail */
134 if (!ossl_assert(buf != NULL && len > 0))
137 pkt->staticbuf = buf;
139 pkt->maxsize = (max < len) ? max : len;
142 return wpacket_intern_init_len(pkt, lenbytes);
145 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
147 /* Internal API, so should not fail */
148 if (!ossl_assert(buf != NULL && len > 0))
151 pkt->staticbuf = buf;
156 return wpacket_intern_init_len(pkt, 0);
159 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
161 /* Internal API, so should not fail */
162 if (!ossl_assert(buf != NULL))
165 pkt->staticbuf = NULL;
167 pkt->maxsize = maxmaxsize(lenbytes);
170 return wpacket_intern_init_len(pkt, lenbytes);
173 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
175 return WPACKET_init_len(pkt, buf, 0);
178 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
180 pkt->staticbuf = NULL;
182 pkt->maxsize = maxmaxsize(lenbytes);
185 return wpacket_intern_init_len(pkt, 0);
188 int WPACKET_init_null_der(WPACKET *pkt)
190 pkt->staticbuf = NULL;
192 pkt->maxsize = SIZE_MAX;
195 return wpacket_intern_init_len(pkt, 0);
198 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
200 /* Internal API, so should not fail */
201 if (!ossl_assert(pkt->subs != NULL))
204 pkt->subs->flags = flags;
209 /* Store the |value| of length |len| at location |data| */
210 static int put_value(unsigned char *data, size_t value, size_t len)
215 for (data += len - 1; len > 0; len--) {
216 *data = (unsigned char)(value & 0xff);
221 /* Check whether we could fit the value in the assigned number of bytes */
230 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
231 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
232 * necessary. If |doclose| is 0 then it goes through the motions of closing
233 * (i.e. it fills in all the lengths), but doesn't actually close anything.
235 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
237 size_t packlen = pkt->written - sub->pwritten;
240 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
244 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
245 /* We can't handle this case. Return an error */
249 /* Deallocate any bytes allocated for the length of the WPACKET */
250 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
251 pkt->written -= sub->lenbytes;
252 pkt->curr -= sub->lenbytes;
255 /* Don't write out the packet length */
260 /* Write out the WPACKET length if needed */
261 if (sub->lenbytes > 0) {
262 unsigned char *buf = GETBUF(pkt);
265 && !put_value(&buf[sub->packet_len], packlen,
268 } else if (pkt->endfirst && sub->parent != NULL
271 & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
272 size_t tmplen = packlen;
273 size_t numlenbytes = 1;
275 while ((tmplen = tmplen >> 8) > 0)
277 if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
279 if (packlen > 0x7f) {
281 if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
287 pkt->subs = sub->parent;
294 int WPACKET_fill_lengths(WPACKET *pkt)
298 if (!ossl_assert(pkt->subs != NULL))
301 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
302 if (!wpacket_intern_close(pkt, sub, 0))
309 int WPACKET_close(WPACKET *pkt)
312 * Internal API, so should not fail - but we do negative testing of this
313 * so no assert (otherwise the tests fail)
315 if (pkt->subs == NULL || pkt->subs->parent == NULL)
318 return wpacket_intern_close(pkt, pkt->subs, 1);
321 int WPACKET_finish(WPACKET *pkt)
326 * Internal API, so should not fail - but we do negative testing of this
327 * so no assert (otherwise the tests fail)
329 if (pkt->subs == NULL || pkt->subs->parent != NULL)
332 ret = wpacket_intern_close(pkt, pkt->subs, 1);
334 OPENSSL_free(pkt->subs);
341 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
344 unsigned char *lenchars;
346 /* Internal API, so should not fail */
347 if (!ossl_assert(pkt->subs != NULL))
350 /* We don't support lenbytes greater than 0 when doing endfirst writing */
351 if (lenbytes > 0 && pkt->endfirst)
354 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
355 SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
359 sub->parent = pkt->subs;
361 sub->pwritten = pkt->written + lenbytes;
362 sub->lenbytes = lenbytes;
369 sub->packet_len = pkt->written;
371 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
377 int WPACKET_start_sub_packet(WPACKET *pkt)
379 return WPACKET_start_sub_packet_len__(pkt, 0);
382 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
386 /* Internal API, so should not fail */
387 if (!ossl_assert(size <= sizeof(unsigned int))
388 || !WPACKET_allocate_bytes(pkt, size, &data)
389 || !put_value(data, val, size))
395 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
400 /* Internal API, so should not fail */
401 if (!ossl_assert(pkt->subs != NULL))
404 /* Find the WPACKET_SUB for the top level */
405 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
408 lenbytes = sub->lenbytes;
410 lenbytes = sizeof(pkt->maxsize);
412 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
415 pkt->maxsize = maxsize;
420 int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
427 if (!WPACKET_allocate_bytes(pkt, len, &dest))
431 memset(dest, ch, len);
436 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
443 if (!WPACKET_allocate_bytes(pkt, len, &dest))
447 memcpy(dest, src, len);
452 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
455 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
456 || !WPACKET_memcpy(pkt, src, len)
457 || !WPACKET_close(pkt))
463 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
465 /* Internal API, so should not fail */
466 if (!ossl_assert(written != NULL))
469 *written = pkt->written;
474 int WPACKET_get_length(WPACKET *pkt, size_t *len)
476 /* Internal API, so should not fail */
477 if (!ossl_assert(pkt->subs != NULL && len != NULL))
480 *len = pkt->written - pkt->subs->pwritten;
485 unsigned char *WPACKET_get_curr(WPACKET *pkt)
487 unsigned char *buf = GETBUF(pkt);
493 return buf + pkt->maxsize - pkt->curr;
495 return buf + pkt->curr;
498 int WPACKET_is_null_buf(WPACKET *pkt)
500 return pkt->buf == NULL && pkt->staticbuf == NULL;
503 void WPACKET_cleanup(WPACKET *pkt)
505 WPACKET_SUB *sub, *parent;
507 for (sub = pkt->subs; sub != NULL; sub = parent) {
508 parent = sub->parent;