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 #ifndef OSSL_INTERNAL_PACKET_H
11 # define OSSL_INTERNAL_PACKET_H
15 # include <openssl/bn.h>
16 # include <openssl/buffer.h>
17 # include <openssl/crypto.h>
18 # include <openssl/e_os2.h>
20 # include "internal/numbers.h"
21 # include "internal/quic_vlint.h"
24 /* Pointer to where we are currently reading from */
25 const unsigned char *curr;
26 /* Number of bytes remaining */
30 /* Internal unchecked shorthand; don't use outside this file. */
31 static ossl_inline void packet_forward(PACKET *pkt, size_t len)
34 pkt->remaining -= len;
38 * Returns the number of bytes remaining to be read in the PACKET
40 static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
42 return pkt->remaining;
46 * Returns a pointer to the first byte after the packet data.
47 * Useful for integrating with non-PACKET parsing code.
48 * Specifically, we use PACKET_end() to verify that a d2i_... call
49 * has consumed the entire packet contents.
51 static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
53 return pkt->curr + pkt->remaining;
57 * Returns a pointer to the PACKET's current position.
58 * For use in non-PACKETized APIs.
60 static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
66 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
67 * copy of the data so |buf| must be present for the whole time that the PACKET
70 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
71 const unsigned char *buf,
74 /* Sanity check for negative values. */
75 if (len > (size_t)(SIZE_MAX / 2))
83 /* Initialize a PACKET to hold zero bytes. */
84 static ossl_inline void PACKET_null_init(PACKET *pkt)
91 * Returns 1 if the packet has length |num| and its contents equal the |num|
92 * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
93 * If lengths are equal, performs the comparison in constant time.
95 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
98 if (PACKET_remaining(pkt) != num)
100 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
104 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
105 * Data is not copied: the |subpkt| packet will share its underlying buffer with
106 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
108 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
109 PACKET *subpkt, size_t len)
111 if (PACKET_remaining(pkt) < len)
114 return PACKET_buf_init(subpkt, pkt->curr, len);
118 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
119 * copied: the |subpkt| packet will share its underlying buffer with the
120 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
122 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
123 PACKET *subpkt, size_t len)
125 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
128 packet_forward(pkt, len);
134 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
137 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
140 if (PACKET_remaining(pkt) < 2)
143 *data = ((unsigned int)(*pkt->curr)) << 8;
144 *data |= *(pkt->curr + 1);
149 /* Equivalent of n2s */
150 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
151 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
153 if (!PACKET_peek_net_2(pkt, data))
156 packet_forward(pkt, 2);
161 /* Same as PACKET_get_net_2() but for a size_t */
162 __owur static ossl_inline int PACKET_get_net_2_len(PACKET *pkt, size_t *data)
165 int ret = PACKET_get_net_2(pkt, &i);
174 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
177 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
180 if (PACKET_remaining(pkt) < 3)
183 *data = ((unsigned long)(*pkt->curr)) << 16;
184 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
185 *data |= *(pkt->curr + 2);
190 /* Equivalent of n2l3 */
191 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
192 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
194 if (!PACKET_peek_net_3(pkt, data))
197 packet_forward(pkt, 3);
202 /* Same as PACKET_get_net_3() but for a size_t */
203 __owur static ossl_inline int PACKET_get_net_3_len(PACKET *pkt, size_t *data)
206 int ret = PACKET_get_net_3(pkt, &i);
215 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
218 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
221 if (PACKET_remaining(pkt) < 4)
224 *data = ((unsigned long)(*pkt->curr)) << 24;
225 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
226 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
227 *data |= *(pkt->curr + 3);
233 * Peek ahead at 8 bytes in network order from |pkt| and store the value in
236 __owur static ossl_inline int PACKET_peek_net_8(const PACKET *pkt,
239 if (PACKET_remaining(pkt) < 8)
242 *data = ((uint64_t)(*pkt->curr)) << 56;
243 *data |= ((uint64_t)(*(pkt->curr + 1))) << 48;
244 *data |= ((uint64_t)(*(pkt->curr + 2))) << 40;
245 *data |= ((uint64_t)(*(pkt->curr + 3))) << 32;
246 *data |= ((uint64_t)(*(pkt->curr + 4))) << 24;
247 *data |= ((uint64_t)(*(pkt->curr + 5))) << 16;
248 *data |= ((uint64_t)(*(pkt->curr + 6))) << 8;
249 *data |= *(pkt->curr + 7);
254 # ifndef OPENSSL_NO_QUIC
257 * Decodes a QUIC variable-length integer in |pkt| and stores the result in
260 __owur static ossl_inline int PACKET_get_quic_vlint(PACKET *pkt,
265 if (PACKET_remaining(pkt) < 1)
268 enclen = ossl_quic_vlint_decode_len(*pkt->curr);
270 if (PACKET_remaining(pkt) < enclen)
273 *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
274 packet_forward(pkt, enclen);
279 * Decodes a QUIC variable-length integer in |pkt| and stores the result in
280 * |data|. Unlike PACKET_get_quic_vlint, this does not advance the current
283 __owur static ossl_inline int PACKET_peek_quic_vlint(PACKET *pkt,
288 if (PACKET_remaining(pkt) < 1)
291 enclen = ossl_quic_vlint_decode_len(*pkt->curr);
293 if (PACKET_remaining(pkt) < enclen)
296 *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
301 * Skips over a QUIC variable-length integer in |pkt| without decoding it.
303 __owur static ossl_inline int PACKET_skip_quic_vlint(PACKET *pkt)
307 if (PACKET_remaining(pkt) < 1)
310 enclen = ossl_quic_vlint_decode_len(*pkt->curr);
312 if (PACKET_remaining(pkt) < enclen)
315 packet_forward(pkt, enclen);
321 /* Equivalent of n2l */
322 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
323 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
325 if (!PACKET_peek_net_4(pkt, data))
328 packet_forward(pkt, 4);
333 /* Same as PACKET_get_net_4() but for a size_t */
334 __owur static ossl_inline int PACKET_get_net_4_len(PACKET *pkt, size_t *data)
337 int ret = PACKET_get_net_4(pkt, &i);
345 /* Get 8 bytes in network order from |pkt| and store the value in |*data| */
346 __owur static ossl_inline int PACKET_get_net_8(PACKET *pkt, uint64_t *data)
348 if (!PACKET_peek_net_8(pkt, data))
351 packet_forward(pkt, 8);
356 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
357 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
360 if (!PACKET_remaining(pkt))
368 /* Get 1 byte from |pkt| and store the value in |*data| */
369 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
371 if (!PACKET_peek_1(pkt, data))
374 packet_forward(pkt, 1);
379 /* Same as PACKET_get_1() but for a size_t */
380 __owur static ossl_inline int PACKET_get_1_len(PACKET *pkt, size_t *data)
383 int ret = PACKET_get_1(pkt, &i);
392 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
395 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
398 if (PACKET_remaining(pkt) < 4)
402 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
403 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
404 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
409 /* Equivalent of c2l */
411 * Get 4 bytes in reverse network order from |pkt| and store the value in
414 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
416 if (!PACKET_peek_4(pkt, data))
419 packet_forward(pkt, 4);
425 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
426 * |*data|. This just points at the underlying buffer that |pkt| is using. The
427 * caller should not free this data directly (it will be freed when the
428 * underlying buffer gets freed
430 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
431 const unsigned char **data,
434 if (PACKET_remaining(pkt) < len)
443 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
444 * just points at the underlying buffer that |pkt| is using. The caller should
445 * not free this data directly (it will be freed when the underlying buffer gets
448 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
449 const unsigned char **data,
452 if (!PACKET_peek_bytes(pkt, data, len))
455 packet_forward(pkt, len);
460 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
461 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
465 if (PACKET_remaining(pkt) < len)
468 memcpy(data, pkt->curr, len);
474 * Read |len| bytes from |pkt| and copy them to |data|.
475 * The caller is responsible for ensuring that |data| can hold |len| bytes.
477 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
478 unsigned char *data, size_t len)
480 if (!PACKET_peek_copy_bytes(pkt, data, len))
483 packet_forward(pkt, len);
489 * Copy packet data to |dest|, and set |len| to the number of copied bytes.
490 * If the packet has more than |dest_len| bytes, nothing is copied.
491 * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
492 * Does not forward PACKET position (because it is typically the last thing
493 * done with a given PACKET).
495 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
497 size_t dest_len, size_t *len)
499 if (PACKET_remaining(pkt) > dest_len) {
503 *len = pkt->remaining;
504 memcpy(dest, pkt->curr, pkt->remaining);
509 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
510 * result in |*data|, and the length in |len|.
511 * If |*data| is not NULL, the old data is OPENSSL_free'd.
512 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
513 * Returns 1 if the malloc succeeds and 0 otherwise.
514 * Does not forward PACKET position (because it is typically the last thing
515 * done with a given PACKET).
517 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
518 unsigned char **data, size_t *len)
526 length = PACKET_remaining(pkt);
531 *data = OPENSSL_memdup(pkt->curr, length);
540 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
541 * buffer. Store a pointer to the result in |*data|.
542 * If |*data| is not NULL, the old data is OPENSSL_free'd.
543 * If the data in |pkt| does not contain a NUL-byte, the entire data is
544 * copied and NUL-terminated.
545 * Returns 1 if the malloc succeeds and 0 otherwise.
546 * Does not forward PACKET position (because it is typically the last thing done
547 * with a given PACKET).
549 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
553 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
554 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
555 return (*data != NULL);
558 /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
559 static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
561 return memchr(pkt->curr, 0, pkt->remaining) != NULL;
564 /* Move the current reading position forward |len| bytes */
565 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
567 if (PACKET_remaining(pkt) < len)
570 packet_forward(pkt, len);
576 * Reads a variable-length vector prefixed with a one-byte length, and stores
577 * the contents in |subpkt|. |pkt| can equal |subpkt|.
578 * Data is not copied: the |subpkt| packet will share its underlying buffer with
579 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
580 * Upon failure, the original |pkt| and |subpkt| are not modified.
582 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
586 const unsigned char *data;
588 if (!PACKET_get_1(&tmp, &length) ||
589 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
595 subpkt->remaining = length;
601 * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
602 * leftover bytes in |pkt|.
604 __owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,
608 const unsigned char *data;
610 if (!PACKET_get_1(&tmp, &length) ||
611 !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
612 PACKET_remaining(&tmp) != 0) {
618 subpkt->remaining = length;
624 * Reads a variable-length vector prefixed with a two-byte length, and stores
625 * the contents in |subpkt|. |pkt| can equal |subpkt|.
626 * Data is not copied: the |subpkt| packet will share its underlying buffer with
627 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
628 * Upon failure, the original |pkt| and |subpkt| are not modified.
630 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
634 const unsigned char *data;
637 if (!PACKET_get_net_2(&tmp, &length) ||
638 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
644 subpkt->remaining = length;
650 * Like PACKET_get_length_prefixed_2, but additionally, fails when there are
651 * leftover bytes in |pkt|.
653 __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
657 const unsigned char *data;
660 if (!PACKET_get_net_2(&tmp, &length) ||
661 !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
662 PACKET_remaining(&tmp) != 0) {
668 subpkt->remaining = length;
674 * Reads a variable-length vector prefixed with a three-byte length, and stores
675 * the contents in |subpkt|. |pkt| can equal |subpkt|.
676 * Data is not copied: the |subpkt| packet will share its underlying buffer with
677 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
678 * Upon failure, the original |pkt| and |subpkt| are not modified.
680 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
683 unsigned long length;
684 const unsigned char *data;
686 if (!PACKET_get_net_3(&tmp, &length) ||
687 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
693 subpkt->remaining = length;
698 # ifndef OPENSSL_NO_QUIC
701 * Reads a variable-length vector prefixed with a QUIC variable-length integer
702 * denoting the length, and stores the contents in |subpkt|. |pkt| can equal
703 * |subpkt|. Data is not copied: the |subpkt| packet will share its underlying
704 * buffer with the original |pkt|, so data wrapped by |pkt| must outlive the
705 * |subpkt|. Upon failure, the original |pkt| and |subpkt| are not modified.
707 __owur static ossl_inline int PACKET_get_quic_length_prefixed(PACKET *pkt,
711 const unsigned char *data;
714 if (!PACKET_get_quic_vlint(&tmp, &length) ||
716 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
722 subpkt->remaining = (size_t)length;
729 /* Writeable packets */
731 typedef struct wpacket_sub WPACKET_SUB;
733 /* The parent WPACKET_SUB if we have one or NULL otherwise */
737 * Offset into the buffer where the length of this WPACKET goes. We use an
738 * offset in case the buffer grows and gets reallocated.
742 /* Number of bytes in the packet_len or 0 if we don't write the length */
745 /* Number of bytes written to the buf prior to this packet starting */
748 /* Flags for this sub-packet */
752 typedef struct wpacket_st WPACKET;
754 /* The buffer where we store the output data */
757 /* Fixed sized buffer which can be used as an alternative to buf */
758 unsigned char *staticbuf;
761 * Offset into the buffer where we are currently writing. We use an offset
762 * in case the buffer grows and gets reallocated.
766 /* Number of bytes written so far */
769 /* Maximum number of bytes we will allow to be written to this WPACKET */
772 /* Our sub-packets (always at least one if not finished) */
775 /* Writing from the end first? */
776 unsigned int endfirst : 1;
782 #define WPACKET_FLAGS_NONE 0
784 /* Error on WPACKET_close() if no data written to the WPACKET */
785 #define WPACKET_FLAGS_NON_ZERO_LENGTH 1
788 * Abandon all changes on WPACKET_close() if no data written to the WPACKET,
789 * i.e. this does not write out a zero packet length
791 #define WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH 2
793 /* QUIC variable-length integer length prefix */
794 #define WPACKET_FLAGS_QUIC_VLINT 4
797 * Initialise a WPACKET with the buffer in |buf|. The buffer must exist
798 * for the whole time that the WPACKET is being used. Additionally |lenbytes| of
799 * data is preallocated at the start of the buffer to store the length of the
800 * WPACKET once we know it.
802 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);
805 * Same as WPACKET_init_len except there is no preallocation of the WPACKET
808 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);
811 * Same as WPACKET_init_len except there is no underlying buffer. No data is
812 * ever actually written. We just keep track of how much data would have been
813 * written if a buffer was there.
815 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);
818 * Same as WPACKET_init_null except we set the WPACKET to assume DER length
819 * encoding for sub-packets.
821 int WPACKET_init_null_der(WPACKET *pkt);
824 * Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.
825 * A fixed buffer of memory |buf| of size |len| is used instead. A failure will
826 * occur if you attempt to write beyond the end of the buffer
828 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
832 * Same as WPACKET_init_static_len except lenbytes is always 0, and we set the
833 * WPACKET to write to the end of the buffer moving towards the start and use
834 * DER length encoding for sub-packets.
836 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);
839 * Set the flags to be applied to the current sub-packet
841 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);
844 * Closes the most recent sub-packet. It also writes out the length of the
845 * packet to the required location (normally the start of the WPACKET) if
846 * appropriate. The top level WPACKET should be closed using WPACKET_finish()
847 * instead of this function.
849 int WPACKET_close(WPACKET *pkt);
852 * The same as WPACKET_close() but only for the top most WPACKET. Additionally
853 * frees memory resources for this WPACKET.
855 int WPACKET_finish(WPACKET *pkt);
858 * Iterate through all the sub-packets and write out their lengths as if they
859 * were being closed. The lengths will be overwritten with the final lengths
860 * when the sub-packets are eventually closed (which may be different if more
861 * data is added to the WPACKET). This function fails if a sub-packet is of 0
862 * length and WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH is set.
864 int WPACKET_fill_lengths(WPACKET *pkt);
867 * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
868 * at the start of the sub-packet to store its length once we know it. Don't
869 * call this directly. Use the convenience macros below instead.
871 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);
874 * Convenience macros for calling WPACKET_start_sub_packet_len with different
877 #define WPACKET_start_sub_packet_u8(pkt) \
878 WPACKET_start_sub_packet_len__((pkt), 1)
879 #define WPACKET_start_sub_packet_u16(pkt) \
880 WPACKET_start_sub_packet_len__((pkt), 2)
881 #define WPACKET_start_sub_packet_u24(pkt) \
882 WPACKET_start_sub_packet_len__((pkt), 3)
883 #define WPACKET_start_sub_packet_u32(pkt) \
884 WPACKET_start_sub_packet_len__((pkt), 4)
887 * Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated
888 * for the sub-packet length.
890 int WPACKET_start_sub_packet(WPACKET *pkt);
893 * Allocate bytes in the WPACKET for the output. This reserves the bytes
894 * and counts them as "written", but doesn't actually do the writing. A pointer
895 * to the allocated bytes is stored in |*allocbytes|. |allocbytes| may be NULL.
896 * WARNING: the allocated bytes must be filled in immediately, without further
897 * WPACKET_* calls. If not then the underlying buffer may be realloc'd and
898 * change its location.
900 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,
901 unsigned char **allocbytes);
904 * The same as WPACKET_allocate_bytes() except additionally a new sub-packet is
905 * started for the allocated bytes, and then closed immediately afterwards. The
906 * number of length bytes for the sub-packet is in |lenbytes|. Don't call this
907 * directly. Use the convenience macros below instead.
909 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
910 unsigned char **allocbytes, size_t lenbytes);
913 * Convenience macros for calling WPACKET_sub_allocate_bytes with different
916 #define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \
917 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)
918 #define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \
919 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)
920 #define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \
921 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)
922 #define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \
923 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)
926 * The same as WPACKET_allocate_bytes() except the reserved bytes are not
927 * actually counted as written. Typically this will be for when we don't know
928 * how big arbitrary data is going to be up front, but we do know what the
929 * maximum size will be. If this function is used, then it should be immediately
930 * followed by a WPACKET_allocate_bytes() call before any other WPACKET
931 * functions are called (unless the write to the allocated bytes is abandoned).
933 * For example: If we are generating a signature, then the size of that
934 * signature may not be known in advance. We can use WPACKET_reserve_bytes() to
937 * if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_get_size(pkey), &sigbytes1)
938 * || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0
939 * || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)
940 * || sigbytes1 != sigbytes2)
943 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);
946 * The "reserve_bytes" equivalent of WPACKET_sub_allocate_bytes__()
948 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
949 unsigned char **allocbytes, size_t lenbytes);
952 * Convenience macros for WPACKET_sub_reserve_bytes with different lengths
954 #define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \
955 WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)
956 #define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \
957 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)
958 #define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \
959 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)
960 #define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \
961 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)
964 * Write the value stored in |val| into the WPACKET. The value will consume
965 * |bytes| amount of storage. An error will occur if |val| cannot be
966 * accommodated in |bytes| storage, e.g. attempting to write the value 256 into
967 * 1 byte will fail. Don't call this directly. Use the convenience macros below
970 int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t bytes);
973 * Convenience macros for calling WPACKET_put_bytes with different
976 #define WPACKET_put_bytes_u8(pkt, val) \
977 WPACKET_put_bytes__((pkt), (val), 1)
978 #define WPACKET_put_bytes_u16(pkt, val) \
979 WPACKET_put_bytes__((pkt), (val), 2)
980 #define WPACKET_put_bytes_u24(pkt, val) \
981 WPACKET_put_bytes__((pkt), (val), 3)
982 #define WPACKET_put_bytes_u32(pkt, val) \
983 WPACKET_put_bytes__((pkt), (val), 4)
984 #define WPACKET_put_bytes_u64(pkt, val) \
985 WPACKET_put_bytes__((pkt), (val), 8)
987 /* Set a maximum size that we will not allow the WPACKET to grow beyond */
988 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);
990 /* Copy |len| bytes of data from |*src| into the WPACKET. */
991 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);
993 /* Set |len| bytes of data to |ch| into the WPACKET. */
994 int WPACKET_memset(WPACKET *pkt, int ch, size_t len);
997 * Copy |len| bytes of data from |*src| into the WPACKET and prefix with its
998 * length (consuming |lenbytes| of data for the length). Don't call this
999 * directly. Use the convenience macros below instead.
1001 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
1004 /* Convenience macros for calling WPACKET_sub_memcpy with different lengths */
1005 #define WPACKET_sub_memcpy_u8(pkt, src, len) \
1006 WPACKET_sub_memcpy__((pkt), (src), (len), 1)
1007 #define WPACKET_sub_memcpy_u16(pkt, src, len) \
1008 WPACKET_sub_memcpy__((pkt), (src), (len), 2)
1009 #define WPACKET_sub_memcpy_u24(pkt, src, len) \
1010 WPACKET_sub_memcpy__((pkt), (src), (len), 3)
1011 #define WPACKET_sub_memcpy_u32(pkt, src, len) \
1012 WPACKET_sub_memcpy__((pkt), (src), (len), 4)
1015 * Return the total number of bytes written so far to the underlying buffer
1016 * including any storage allocated for length bytes
1018 int WPACKET_get_total_written(WPACKET *pkt, size_t *written);
1021 * Returns the length of the current sub-packet. This excludes any bytes
1022 * allocated for the length itself.
1024 int WPACKET_get_length(WPACKET *pkt, size_t *len);
1027 * Returns a pointer to the current write location, but does not allocate any
1030 unsigned char *WPACKET_get_curr(WPACKET *pkt);
1032 /* Returns true if the underlying buffer is actually NULL */
1033 int WPACKET_is_null_buf(WPACKET *pkt);
1035 /* Release resources in a WPACKET if a failure has occurred. */
1036 void WPACKET_cleanup(WPACKET *pkt);
1038 # ifndef OPENSSL_NO_QUIC
1041 * Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte
1042 * representation is used.
1044 __owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);
1047 * Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len
1048 * specifies the upper bound for the sub-packet size at the time the sub-packet
1049 * is closed, which determines the encoding size for tthe variable-length
1050 * integer header. max_len can be a precise figure or a worst-case bound
1051 * if a precise figure is not available.
1053 __owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);
1056 * Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a
1057 * QUIC variable-length integer. The pointer to the payload buffer is output and
1058 * must be filled by the caller. This function assures optimal selection of
1059 * variable-length integer encoding length.
1061 __owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,
1062 unsigned char **bytes);
1065 * Write a QUIC variable-length integer to the packet.
1067 __owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);
1071 #endif /* OSSL_INTERNAL_PACKET_H */