2 * Copyright 2015-2023 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 #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
13 # include "internal/packet_quic.h"
15 #include <openssl/err.h>
17 #define DEFAULT_BUF_SIZE 256
19 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
21 if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
29 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
30 unsigned char **allocbytes, size_t lenbytes)
32 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
33 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
34 || !WPACKET_close(pkt))
40 #define GETBUF(p) (((p)->staticbuf != NULL) \
43 ? (unsigned char *)(p)->buf->data \
46 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
48 /* Internal API, so should not fail */
49 if (!ossl_assert(pkt->subs != NULL && len != 0))
52 if (pkt->maxsize - pkt->written < len)
55 if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
59 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
61 if (reflen > SIZE_MAX / 2) {
65 if (newlen < DEFAULT_BUF_SIZE)
66 newlen = DEFAULT_BUF_SIZE;
68 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
71 if (allocbytes != NULL) {
72 *allocbytes = WPACKET_get_curr(pkt);
73 if (pkt->endfirst && *allocbytes != NULL)
80 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
81 unsigned char **allocbytes, size_t lenbytes)
83 if (pkt->endfirst && lenbytes > 0)
86 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
89 if (*allocbytes != NULL)
90 *allocbytes += lenbytes;
95 static size_t maxmaxsize(size_t lenbytes)
97 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
100 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
103 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
105 unsigned char *lenchars;
110 if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL)
116 pkt->subs->pwritten = lenbytes;
117 pkt->subs->lenbytes = lenbytes;
119 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
120 OPENSSL_free(pkt->subs);
124 pkt->subs->packet_len = 0;
129 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
132 size_t max = maxmaxsize(lenbytes);
134 /* Internal API, so should not fail */
135 if (!ossl_assert(buf != NULL && len > 0))
138 pkt->staticbuf = buf;
140 pkt->maxsize = (max < len) ? max : len;
143 return wpacket_intern_init_len(pkt, lenbytes);
146 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
148 /* Internal API, so should not fail */
149 if (!ossl_assert(buf != NULL && len > 0))
152 pkt->staticbuf = buf;
157 return wpacket_intern_init_len(pkt, 0);
160 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
162 /* Internal API, so should not fail */
163 if (!ossl_assert(buf != NULL))
166 pkt->staticbuf = NULL;
168 pkt->maxsize = maxmaxsize(lenbytes);
171 return wpacket_intern_init_len(pkt, lenbytes);
174 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
176 return WPACKET_init_len(pkt, buf, 0);
179 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
181 pkt->staticbuf = NULL;
183 pkt->maxsize = maxmaxsize(lenbytes);
186 return wpacket_intern_init_len(pkt, 0);
189 int WPACKET_init_null_der(WPACKET *pkt)
191 pkt->staticbuf = NULL;
193 pkt->maxsize = SIZE_MAX;
196 return wpacket_intern_init_len(pkt, 0);
199 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
201 /* Internal API, so should not fail */
202 if (!ossl_assert(pkt->subs != NULL))
205 pkt->subs->flags = flags;
210 /* Store the |value| of length |len| at location |data| */
211 static int put_value(unsigned char *data, uint64_t value, size_t len)
216 for (data += len - 1; len > 0; len--) {
217 *data = (unsigned char)(value & 0xff);
222 /* Check whether we could fit the value in the assigned number of bytes */
229 #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
230 static int put_quic_value(unsigned char *data, size_t value, size_t len)
235 /* Value too large for field. */
236 if (ossl_quic_vlint_encode_len(value) > len)
239 ossl_quic_vlint_encode_n(data, value, len);
245 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
246 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
247 * necessary. If |doclose| is 0 then it goes through the motions of closing
248 * (i.e. it fills in all the lengths), but doesn't actually close anything.
250 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
252 size_t packlen = pkt->written - sub->pwritten;
255 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
259 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
260 /* We can't handle this case. Return an error */
264 /* Deallocate any bytes allocated for the length of the WPACKET */
265 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
266 pkt->written -= sub->lenbytes;
267 pkt->curr -= sub->lenbytes;
270 /* Don't write out the packet length */
275 /* Write out the WPACKET length if needed */
276 if (sub->lenbytes > 0) {
277 unsigned char *buf = GETBUF(pkt);
280 #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
281 if ((sub->flags & WPACKET_FLAGS_QUIC_VLINT) == 0) {
282 if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
285 if (!put_quic_value(&buf[sub->packet_len], packlen, sub->lenbytes))
289 if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
293 } else if (pkt->endfirst && sub->parent != NULL
296 & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
297 size_t tmplen = packlen;
298 size_t numlenbytes = 1;
300 while ((tmplen = tmplen >> 8) > 0)
302 if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
304 if (packlen > 0x7f) {
306 if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
312 pkt->subs = sub->parent;
319 int WPACKET_fill_lengths(WPACKET *pkt)
323 if (!ossl_assert(pkt->subs != NULL))
326 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
327 if (!wpacket_intern_close(pkt, sub, 0))
334 int WPACKET_close(WPACKET *pkt)
337 * Internal API, so should not fail - but we do negative testing of this
338 * so no assert (otherwise the tests fail)
340 if (pkt->subs == NULL || pkt->subs->parent == NULL)
343 return wpacket_intern_close(pkt, pkt->subs, 1);
346 int WPACKET_finish(WPACKET *pkt)
351 * Internal API, so should not fail - but we do negative testing of this
352 * so no assert (otherwise the tests fail)
354 if (pkt->subs == NULL || pkt->subs->parent != NULL)
357 ret = wpacket_intern_close(pkt, pkt->subs, 1);
359 OPENSSL_free(pkt->subs);
366 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
369 unsigned char *lenchars;
371 /* Internal API, so should not fail */
372 if (!ossl_assert(pkt->subs != NULL))
375 /* We don't support lenbytes greater than 0 when doing endfirst writing */
376 if (lenbytes > 0 && pkt->endfirst)
379 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL)
382 sub->parent = pkt->subs;
384 sub->pwritten = pkt->written + lenbytes;
385 sub->lenbytes = lenbytes;
392 sub->packet_len = pkt->written;
394 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
400 int WPACKET_start_sub_packet(WPACKET *pkt)
402 return WPACKET_start_sub_packet_len__(pkt, 0);
405 int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
409 /* Internal API, so should not fail */
410 if (!ossl_assert(size <= sizeof(uint64_t))
411 || !WPACKET_allocate_bytes(pkt, size, &data)
412 || !put_value(data, val, size))
418 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
423 /* Internal API, so should not fail */
424 if (!ossl_assert(pkt->subs != NULL))
427 /* Find the WPACKET_SUB for the top level */
428 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
431 lenbytes = sub->lenbytes;
433 lenbytes = sizeof(pkt->maxsize);
435 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
438 pkt->maxsize = maxsize;
443 int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
450 if (!WPACKET_allocate_bytes(pkt, len, &dest))
454 memset(dest, ch, len);
459 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
466 if (!WPACKET_allocate_bytes(pkt, len, &dest))
470 memcpy(dest, src, len);
475 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
478 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
479 || !WPACKET_memcpy(pkt, src, len)
480 || !WPACKET_close(pkt))
486 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
488 /* Internal API, so should not fail */
489 if (!ossl_assert(written != NULL))
492 *written = pkt->written;
497 int WPACKET_get_length(WPACKET *pkt, size_t *len)
499 /* Internal API, so should not fail */
500 if (!ossl_assert(pkt->subs != NULL && len != NULL))
503 *len = pkt->written - pkt->subs->pwritten;
508 unsigned char *WPACKET_get_curr(WPACKET *pkt)
510 unsigned char *buf = GETBUF(pkt);
516 return buf + pkt->maxsize - pkt->curr;
518 return buf + pkt->curr;
521 int WPACKET_is_null_buf(WPACKET *pkt)
523 return pkt->buf == NULL && pkt->staticbuf == NULL;
526 void WPACKET_cleanup(WPACKET *pkt)
528 WPACKET_SUB *sub, *parent;
530 for (sub = pkt->subs; sub != NULL; sub = parent) {
531 parent = sub->parent;
537 #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
539 int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len)
541 size_t enclen = ossl_quic_vlint_encode_len(max_len);
546 if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0)
549 pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT;
553 int WPACKET_start_quic_sub_packet(WPACKET *pkt)
556 * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not
559 return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN);
562 int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
564 if (!WPACKET_start_quic_sub_packet_bound(pkt, len)
565 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
566 || !WPACKET_close(pkt))
573 * Write a QUIC variable-length integer to the packet.
575 int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v)
577 unsigned char *b = NULL;
578 size_t enclen = ossl_quic_vlint_encode_len(v);
583 if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0)
586 ossl_quic_vlint_encode(b, v);