2 * Copyright 2015-2021 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/err.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)
113 pkt->subs->pwritten = lenbytes;
114 pkt->subs->lenbytes = lenbytes;
116 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
117 OPENSSL_free(pkt->subs);
121 pkt->subs->packet_len = 0;
126 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
129 size_t max = maxmaxsize(lenbytes);
131 /* Internal API, so should not fail */
132 if (!ossl_assert(buf != NULL && len > 0))
135 pkt->staticbuf = buf;
137 pkt->maxsize = (max < len) ? max : len;
140 return wpacket_intern_init_len(pkt, lenbytes);
143 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
145 /* Internal API, so should not fail */
146 if (!ossl_assert(buf != NULL && len > 0))
149 pkt->staticbuf = buf;
154 return wpacket_intern_init_len(pkt, 0);
157 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
159 /* Internal API, so should not fail */
160 if (!ossl_assert(buf != NULL))
163 pkt->staticbuf = NULL;
165 pkt->maxsize = maxmaxsize(lenbytes);
168 return wpacket_intern_init_len(pkt, lenbytes);
171 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
173 return WPACKET_init_len(pkt, buf, 0);
176 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
178 pkt->staticbuf = NULL;
180 pkt->maxsize = maxmaxsize(lenbytes);
183 return wpacket_intern_init_len(pkt, 0);
186 int WPACKET_init_null_der(WPACKET *pkt)
188 pkt->staticbuf = NULL;
190 pkt->maxsize = SIZE_MAX;
193 return wpacket_intern_init_len(pkt, 0);
196 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
198 /* Internal API, so should not fail */
199 if (!ossl_assert(pkt->subs != NULL))
202 pkt->subs->flags = flags;
207 /* Store the |value| of length |len| at location |data| */
208 static int put_value(unsigned char *data, uint64_t value, size_t len)
213 for (data += len - 1; len > 0; len--) {
214 *data = (unsigned char)(value & 0xff);
219 /* Check whether we could fit the value in the assigned number of bytes */
226 static int put_quic_value(unsigned char *data, size_t value, size_t len)
231 /* Value too large for field. */
232 if (ossl_quic_vlint_encode_len(value) > len)
235 ossl_quic_vlint_encode_n(data, value, len);
240 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
241 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
242 * necessary. If |doclose| is 0 then it goes through the motions of closing
243 * (i.e. it fills in all the lengths), but doesn't actually close anything.
245 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
247 size_t packlen = pkt->written - sub->pwritten;
250 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
254 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
255 /* We can't handle this case. Return an error */
259 /* Deallocate any bytes allocated for the length of the WPACKET */
260 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
261 pkt->written -= sub->lenbytes;
262 pkt->curr -= sub->lenbytes;
265 /* Don't write out the packet length */
270 /* Write out the WPACKET length if needed */
271 if (sub->lenbytes > 0) {
272 unsigned char *buf = GETBUF(pkt);
275 if ((sub->flags & WPACKET_FLAGS_QUIC_VLINT) == 0) {
276 if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
279 if (!put_quic_value(&buf[sub->packet_len], packlen, sub->lenbytes))
283 } else if (pkt->endfirst && sub->parent != NULL
286 & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
287 size_t tmplen = packlen;
288 size_t numlenbytes = 1;
290 while ((tmplen = tmplen >> 8) > 0)
292 if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
294 if (packlen > 0x7f) {
296 if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
302 pkt->subs = sub->parent;
309 int WPACKET_fill_lengths(WPACKET *pkt)
313 if (!ossl_assert(pkt->subs != NULL))
316 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
317 if (!wpacket_intern_close(pkt, sub, 0))
324 int WPACKET_close(WPACKET *pkt)
327 * Internal API, so should not fail - but we do negative testing of this
328 * so no assert (otherwise the tests fail)
330 if (pkt->subs == NULL || pkt->subs->parent == NULL)
333 return wpacket_intern_close(pkt, pkt->subs, 1);
336 int WPACKET_finish(WPACKET *pkt)
341 * Internal API, so should not fail - but we do negative testing of this
342 * so no assert (otherwise the tests fail)
344 if (pkt->subs == NULL || pkt->subs->parent != NULL)
347 ret = wpacket_intern_close(pkt, pkt->subs, 1);
349 OPENSSL_free(pkt->subs);
356 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
359 unsigned char *lenchars;
361 /* Internal API, so should not fail */
362 if (!ossl_assert(pkt->subs != NULL))
365 /* We don't support lenbytes greater than 0 when doing endfirst writing */
366 if (lenbytes > 0 && pkt->endfirst)
369 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL)
372 sub->parent = pkt->subs;
374 sub->pwritten = pkt->written + lenbytes;
375 sub->lenbytes = lenbytes;
382 sub->packet_len = pkt->written;
384 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
390 int WPACKET_start_sub_packet(WPACKET *pkt)
392 return WPACKET_start_sub_packet_len__(pkt, 0);
395 int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
399 /* Internal API, so should not fail */
400 if (!ossl_assert(size <= sizeof(uint64_t))
401 || !WPACKET_allocate_bytes(pkt, size, &data)
402 || !put_value(data, val, size))
408 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
413 /* Internal API, so should not fail */
414 if (!ossl_assert(pkt->subs != NULL))
417 /* Find the WPACKET_SUB for the top level */
418 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
421 lenbytes = sub->lenbytes;
423 lenbytes = sizeof(pkt->maxsize);
425 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
428 pkt->maxsize = maxsize;
433 int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
440 if (!WPACKET_allocate_bytes(pkt, len, &dest))
444 memset(dest, ch, len);
449 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
456 if (!WPACKET_allocate_bytes(pkt, len, &dest))
460 memcpy(dest, src, len);
465 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
468 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
469 || !WPACKET_memcpy(pkt, src, len)
470 || !WPACKET_close(pkt))
476 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
478 /* Internal API, so should not fail */
479 if (!ossl_assert(written != NULL))
482 *written = pkt->written;
487 int WPACKET_get_length(WPACKET *pkt, size_t *len)
489 /* Internal API, so should not fail */
490 if (!ossl_assert(pkt->subs != NULL && len != NULL))
493 *len = pkt->written - pkt->subs->pwritten;
498 unsigned char *WPACKET_get_curr(WPACKET *pkt)
500 unsigned char *buf = GETBUF(pkt);
506 return buf + pkt->maxsize - pkt->curr;
508 return buf + pkt->curr;
511 int WPACKET_is_null_buf(WPACKET *pkt)
513 return pkt->buf == NULL && pkt->staticbuf == NULL;
516 void WPACKET_cleanup(WPACKET *pkt)
518 WPACKET_SUB *sub, *parent;
520 for (sub = pkt->subs; sub != NULL; sub = parent) {
521 parent = sub->parent;
527 int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len)
529 size_t enclen = ossl_quic_vlint_encode_len(max_len);
534 if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0)
537 pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT;
541 int WPACKET_start_quic_sub_packet(WPACKET *pkt)
544 * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not
547 return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN);
550 int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
552 if (!WPACKET_start_quic_sub_packet_bound(pkt, len)
553 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
554 || !WPACKET_close(pkt))
561 * Write a QUIC variable-length integer to the packet.
563 int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v)
565 unsigned char *b = NULL;
566 size_t enclen = ossl_quic_vlint_encode_len(v);
571 if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0)
574 ossl_quic_vlint_encode(b, v);