Path: blob/main/crypto/openssl/include/internal/packet.h
34879 views
/*1* Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OSSL_INTERNAL_PACKET_H10# define OSSL_INTERNAL_PACKET_H11# pragma once1213# include <string.h>14# include <openssl/bn.h>15# include <openssl/buffer.h>16# include <openssl/crypto.h>17# include <openssl/e_os2.h>1819# include "internal/numbers.h"2021typedef struct {22/* Pointer to where we are currently reading from */23const unsigned char *curr;24/* Number of bytes remaining */25size_t remaining;26} PACKET;2728/* Internal unchecked shorthand; don't use outside this file. */29static ossl_inline void packet_forward(PACKET *pkt, size_t len)30{31pkt->curr += len;32pkt->remaining -= len;33}3435/*36* Returns the number of bytes remaining to be read in the PACKET37*/38static ossl_inline size_t PACKET_remaining(const PACKET *pkt)39{40return pkt->remaining;41}4243/*44* Returns a pointer to the first byte after the packet data.45* Useful for integrating with non-PACKET parsing code.46* Specifically, we use PACKET_end() to verify that a d2i_... call47* has consumed the entire packet contents.48*/49static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)50{51return pkt->curr + pkt->remaining;52}5354/*55* Returns a pointer to the PACKET's current position.56* For use in non-PACKETized APIs.57*/58static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)59{60return pkt->curr;61}6263/*64* Initialise a PACKET with |len| bytes held in |buf|. This does not make a65* copy of the data so |buf| must be present for the whole time that the PACKET66* is being used.67*/68__owur static ossl_inline int PACKET_buf_init(PACKET *pkt,69const unsigned char *buf,70size_t len)71{72/* Sanity check for negative values. */73if (len > (size_t)(SIZE_MAX / 2))74return 0;7576pkt->curr = buf;77pkt->remaining = len;78return 1;79}8081/* Initialize a PACKET to hold zero bytes. */82static ossl_inline void PACKET_null_init(PACKET *pkt)83{84pkt->curr = NULL;85pkt->remaining = 0;86}8788/*89* Returns 1 if the packet has length |num| and its contents equal the |num|90* bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).91* If lengths are equal, performs the comparison in constant time.92*/93__owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,94size_t num)95{96if (PACKET_remaining(pkt) != num)97return 0;98return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;99}100101/*102* Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.103* Data is not copied: the |subpkt| packet will share its underlying buffer with104* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.105*/106__owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,107PACKET *subpkt, size_t len)108{109if (PACKET_remaining(pkt) < len)110return 0;111112return PACKET_buf_init(subpkt, pkt->curr, len);113}114115/*116* Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not117* copied: the |subpkt| packet will share its underlying buffer with the118* original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.119*/120__owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,121PACKET *subpkt, size_t len)122{123if (!PACKET_peek_sub_packet(pkt, subpkt, len))124return 0;125126packet_forward(pkt, len);127128return 1;129}130131/*132* Peek ahead at 2 bytes in network order from |pkt| and store the value in133* |*data|134*/135__owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,136unsigned int *data)137{138if (PACKET_remaining(pkt) < 2)139return 0;140141*data = ((unsigned int)(*pkt->curr)) << 8;142*data |= *(pkt->curr + 1);143144return 1;145}146147/* Equivalent of n2s */148/* Get 2 bytes in network order from |pkt| and store the value in |*data| */149__owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)150{151if (!PACKET_peek_net_2(pkt, data))152return 0;153154packet_forward(pkt, 2);155156return 1;157}158159/* Same as PACKET_get_net_2() but for a size_t */160__owur static ossl_inline int PACKET_get_net_2_len(PACKET *pkt, size_t *data)161{162unsigned int i;163int ret = PACKET_get_net_2(pkt, &i);164165if (ret)166*data = (size_t)i;167168return ret;169}170171/*172* Peek ahead at 3 bytes in network order from |pkt| and store the value in173* |*data|174*/175__owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,176unsigned long *data)177{178if (PACKET_remaining(pkt) < 3)179return 0;180181*data = ((unsigned long)(*pkt->curr)) << 16;182*data |= ((unsigned long)(*(pkt->curr + 1))) << 8;183*data |= *(pkt->curr + 2);184185return 1;186}187188/* Equivalent of n2l3 */189/* Get 3 bytes in network order from |pkt| and store the value in |*data| */190__owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)191{192if (!PACKET_peek_net_3(pkt, data))193return 0;194195packet_forward(pkt, 3);196197return 1;198}199200/* Same as PACKET_get_net_3() but for a size_t */201__owur static ossl_inline int PACKET_get_net_3_len(PACKET *pkt, size_t *data)202{203unsigned long i;204int ret = PACKET_get_net_3(pkt, &i);205206if (ret)207*data = (size_t)i;208209return ret;210}211212/*213* Peek ahead at 4 bytes in network order from |pkt| and store the value in214* |*data|215*/216__owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,217unsigned long *data)218{219if (PACKET_remaining(pkt) < 4)220return 0;221222*data = ((unsigned long)(*pkt->curr)) << 24;223*data |= ((unsigned long)(*(pkt->curr + 1))) << 16;224*data |= ((unsigned long)(*(pkt->curr + 2))) << 8;225*data |= *(pkt->curr + 3);226227return 1;228}229230/*231* Peek ahead at 8 bytes in network order from |pkt| and store the value in232* |*data|233*/234__owur static ossl_inline int PACKET_peek_net_8(const PACKET *pkt,235uint64_t *data)236{237if (PACKET_remaining(pkt) < 8)238return 0;239240*data = ((uint64_t)(*pkt->curr)) << 56;241*data |= ((uint64_t)(*(pkt->curr + 1))) << 48;242*data |= ((uint64_t)(*(pkt->curr + 2))) << 40;243*data |= ((uint64_t)(*(pkt->curr + 3))) << 32;244*data |= ((uint64_t)(*(pkt->curr + 4))) << 24;245*data |= ((uint64_t)(*(pkt->curr + 5))) << 16;246*data |= ((uint64_t)(*(pkt->curr + 6))) << 8;247*data |= *(pkt->curr + 7);248249return 1;250}251252/* Equivalent of n2l */253/* Get 4 bytes in network order from |pkt| and store the value in |*data| */254__owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)255{256if (!PACKET_peek_net_4(pkt, data))257return 0;258259packet_forward(pkt, 4);260261return 1;262}263264/* Same as PACKET_get_net_4() but for a size_t */265__owur static ossl_inline int PACKET_get_net_4_len(PACKET *pkt, size_t *data)266{267unsigned long i;268int ret = PACKET_get_net_4(pkt, &i);269270if (ret)271*data = (size_t)i;272273return ret;274}275276/* Get 8 bytes in network order from |pkt| and store the value in |*data| */277__owur static ossl_inline int PACKET_get_net_8(PACKET *pkt, uint64_t *data)278{279if (!PACKET_peek_net_8(pkt, data))280return 0;281282packet_forward(pkt, 8);283284return 1;285}286287/* Peek ahead at 1 byte from |pkt| and store the value in |*data| */288__owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,289unsigned int *data)290{291if (!PACKET_remaining(pkt))292return 0;293294*data = *pkt->curr;295296return 1;297}298299/* Get 1 byte from |pkt| and store the value in |*data| */300__owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)301{302if (!PACKET_peek_1(pkt, data))303return 0;304305packet_forward(pkt, 1);306307return 1;308}309310/* Same as PACKET_get_1() but for a size_t */311__owur static ossl_inline int PACKET_get_1_len(PACKET *pkt, size_t *data)312{313unsigned int i;314int ret = PACKET_get_1(pkt, &i);315316if (ret)317*data = (size_t)i;318319return ret;320}321322/*323* Peek ahead at 4 bytes in reverse network order from |pkt| and store the value324* in |*data|325*/326__owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,327unsigned long *data)328{329if (PACKET_remaining(pkt) < 4)330return 0;331332*data = *pkt->curr;333*data |= ((unsigned long)(*(pkt->curr + 1))) << 8;334*data |= ((unsigned long)(*(pkt->curr + 2))) << 16;335*data |= ((unsigned long)(*(pkt->curr + 3))) << 24;336337return 1;338}339340/* Equivalent of c2l */341/*342* Get 4 bytes in reverse network order from |pkt| and store the value in343* |*data|344*/345__owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)346{347if (!PACKET_peek_4(pkt, data))348return 0;349350packet_forward(pkt, 4);351352return 1;353}354355/*356* Peek ahead at |len| bytes from the |pkt| and store a pointer to them in357* |*data|. This just points at the underlying buffer that |pkt| is using. The358* caller should not free this data directly (it will be freed when the359* underlying buffer gets freed360*/361__owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,362const unsigned char **data,363size_t len)364{365if (PACKET_remaining(pkt) < len)366return 0;367368*data = pkt->curr;369370return 1;371}372373/*374* Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This375* just points at the underlying buffer that |pkt| is using. The caller should376* not free this data directly (it will be freed when the underlying buffer gets377* freed378*/379__owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,380const unsigned char **data,381size_t len)382{383if (!PACKET_peek_bytes(pkt, data, len))384return 0;385386packet_forward(pkt, len);387388return 1;389}390391/* Peek ahead at |len| bytes from |pkt| and copy them to |data| */392__owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,393unsigned char *data,394size_t len)395{396if (PACKET_remaining(pkt) < len)397return 0;398399memcpy(data, pkt->curr, len);400401return 1;402}403404/*405* Read |len| bytes from |pkt| and copy them to |data|.406* The caller is responsible for ensuring that |data| can hold |len| bytes.407*/408__owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,409unsigned char *data, size_t len)410{411if (!PACKET_peek_copy_bytes(pkt, data, len))412return 0;413414packet_forward(pkt, len);415416return 1;417}418419/*420* Copy packet data to |dest|, and set |len| to the number of copied bytes.421* If the packet has more than |dest_len| bytes, nothing is copied.422* Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.423* Does not forward PACKET position (because it is typically the last thing424* done with a given PACKET).425*/426__owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,427unsigned char *dest,428size_t dest_len, size_t *len)429{430if (PACKET_remaining(pkt) > dest_len) {431*len = 0;432return 0;433}434*len = pkt->remaining;435memcpy(dest, pkt->curr, pkt->remaining);436return 1;437}438439/*440* Copy |pkt| bytes to a newly allocated buffer and store a pointer to the441* result in |*data|, and the length in |len|.442* If |*data| is not NULL, the old data is OPENSSL_free'd.443* If the packet is empty, or malloc fails, |*data| will be set to NULL.444* Returns 1 if the malloc succeeds and 0 otherwise.445* Does not forward PACKET position (because it is typically the last thing446* done with a given PACKET).447*/448__owur static ossl_inline int PACKET_memdup(const PACKET *pkt,449unsigned char **data, size_t *len)450{451size_t length;452453OPENSSL_free(*data);454*data = NULL;455*len = 0;456457length = PACKET_remaining(pkt);458459if (length == 0)460return 1;461462*data = OPENSSL_memdup(pkt->curr, length);463if (*data == NULL)464return 0;465466*len = length;467return 1;468}469470/*471* Read a C string from |pkt| and copy to a newly allocated, NUL-terminated472* buffer. Store a pointer to the result in |*data|.473* If |*data| is not NULL, the old data is OPENSSL_free'd.474* If the data in |pkt| does not contain a NUL-byte, the entire data is475* copied and NUL-terminated.476* Returns 1 if the malloc succeeds and 0 otherwise.477* Does not forward PACKET position (because it is typically the last thing done478* with a given PACKET).479*/480__owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)481{482OPENSSL_free(*data);483484/* This will succeed on an empty packet, unless pkt->curr == NULL. */485*data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));486return (*data != NULL);487}488489/* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */490static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)491{492return memchr(pkt->curr, 0, pkt->remaining) != NULL;493}494495/* Move the current reading position forward |len| bytes */496__owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)497{498if (PACKET_remaining(pkt) < len)499return 0;500501packet_forward(pkt, len);502503return 1;504}505506/*507* Reads a variable-length vector prefixed with a one-byte length, and stores508* the contents in |subpkt|. |pkt| can equal |subpkt|.509* Data is not copied: the |subpkt| packet will share its underlying buffer with510* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.511* Upon failure, the original |pkt| and |subpkt| are not modified.512*/513__owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,514PACKET *subpkt)515{516unsigned int length;517const unsigned char *data;518PACKET tmp = *pkt;519if (!PACKET_get_1(&tmp, &length) ||520!PACKET_get_bytes(&tmp, &data, (size_t)length)) {521return 0;522}523524*pkt = tmp;525subpkt->curr = data;526subpkt->remaining = length;527528return 1;529}530531/*532* Like PACKET_get_length_prefixed_1, but additionally, fails when there are533* leftover bytes in |pkt|.534*/535__owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,536PACKET *subpkt)537{538unsigned int length;539const unsigned char *data;540PACKET tmp = *pkt;541if (!PACKET_get_1(&tmp, &length) ||542!PACKET_get_bytes(&tmp, &data, (size_t)length) ||543PACKET_remaining(&tmp) != 0) {544return 0;545}546547*pkt = tmp;548subpkt->curr = data;549subpkt->remaining = length;550551return 1;552}553554/*555* Reads a variable-length vector prefixed with a two-byte length, and stores556* the contents in |subpkt|. |pkt| can equal |subpkt|.557* Data is not copied: the |subpkt| packet will share its underlying buffer with558* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.559* Upon failure, the original |pkt| and |subpkt| are not modified.560*/561__owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,562PACKET *subpkt)563{564unsigned int length;565const unsigned char *data;566PACKET tmp = *pkt;567568if (!PACKET_get_net_2(&tmp, &length) ||569!PACKET_get_bytes(&tmp, &data, (size_t)length)) {570return 0;571}572573*pkt = tmp;574subpkt->curr = data;575subpkt->remaining = length;576577return 1;578}579580/*581* Like PACKET_get_length_prefixed_2, but additionally, fails when there are582* leftover bytes in |pkt|.583*/584__owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,585PACKET *subpkt)586{587unsigned int length;588const unsigned char *data;589PACKET tmp = *pkt;590591if (!PACKET_get_net_2(&tmp, &length) ||592!PACKET_get_bytes(&tmp, &data, (size_t)length) ||593PACKET_remaining(&tmp) != 0) {594return 0;595}596597*pkt = tmp;598subpkt->curr = data;599subpkt->remaining = length;600601return 1;602}603604/*605* Reads a variable-length vector prefixed with a three-byte length, and stores606* the contents in |subpkt|. |pkt| can equal |subpkt|.607* Data is not copied: the |subpkt| packet will share its underlying buffer with608* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.609* Upon failure, the original |pkt| and |subpkt| are not modified.610*/611__owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,612PACKET *subpkt)613{614unsigned long length;615const unsigned char *data;616PACKET tmp = *pkt;617if (!PACKET_get_net_3(&tmp, &length) ||618!PACKET_get_bytes(&tmp, &data, (size_t)length)) {619return 0;620}621622*pkt = tmp;623subpkt->curr = data;624subpkt->remaining = length;625626return 1;627}628629/* Writable packets */630631typedef struct wpacket_sub WPACKET_SUB;632struct wpacket_sub {633/* The parent WPACKET_SUB if we have one or NULL otherwise */634WPACKET_SUB *parent;635636/*637* Offset into the buffer where the length of this WPACKET goes. We use an638* offset in case the buffer grows and gets reallocated.639*/640size_t packet_len;641642/* Number of bytes in the packet_len or 0 if we don't write the length */643size_t lenbytes;644645/* Number of bytes written to the buf prior to this packet starting */646size_t pwritten;647648/* Flags for this sub-packet */649unsigned int flags;650};651652typedef struct wpacket_st WPACKET;653struct wpacket_st {654/* The buffer where we store the output data */655BUF_MEM *buf;656657/* Fixed sized buffer which can be used as an alternative to buf */658unsigned char *staticbuf;659660/*661* Offset into the buffer where we are currently writing. We use an offset662* in case the buffer grows and gets reallocated.663*/664size_t curr;665666/* Number of bytes written so far */667size_t written;668669/* Maximum number of bytes we will allow to be written to this WPACKET */670size_t maxsize;671672/* Our sub-packets (always at least one if not finished) */673WPACKET_SUB *subs;674675/* Writing from the end first? */676unsigned int endfirst : 1;677};678679/* Flags */680681/* Default */682#define WPACKET_FLAGS_NONE 0683684/* Error on WPACKET_close() if no data written to the WPACKET */685#define WPACKET_FLAGS_NON_ZERO_LENGTH 1686687/*688* Abandon all changes on WPACKET_close() if no data written to the WPACKET,689* i.e. this does not write out a zero packet length690*/691#define WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH 2692693/* QUIC variable-length integer length prefix */694#define WPACKET_FLAGS_QUIC_VLINT 4695696/*697* Initialise a WPACKET with the buffer in |buf|. The buffer must exist698* for the whole time that the WPACKET is being used. Additionally |lenbytes| of699* data is preallocated at the start of the buffer to store the length of the700* WPACKET once we know it.701*/702int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);703704/*705* Same as WPACKET_init_len except there is no preallocation of the WPACKET706* length.707*/708int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);709710/*711* Same as WPACKET_init_len except there is no underlying buffer. No data is712* ever actually written. We just keep track of how much data would have been713* written if a buffer was there.714*/715int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);716717/*718* Same as WPACKET_init_null except we set the WPACKET to assume DER length719* encoding for sub-packets.720*/721int WPACKET_init_null_der(WPACKET *pkt);722723/*724* Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.725* A fixed buffer of memory |buf| of size |len| is used instead. A failure will726* occur if you attempt to write beyond the end of the buffer727*/728int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,729size_t lenbytes);730731/*732* Same as WPACKET_init_static_len except lenbytes is always 0, and we set the733* WPACKET to write to the end of the buffer moving towards the start and use734* DER length encoding for sub-packets.735*/736int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);737738/*739* Set the flags to be applied to the current sub-packet740*/741int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);742743/*744* Closes the most recent sub-packet. It also writes out the length of the745* packet to the required location (normally the start of the WPACKET) if746* appropriate. The top level WPACKET should be closed using WPACKET_finish()747* instead of this function.748*/749int WPACKET_close(WPACKET *pkt);750751/*752* The same as WPACKET_close() but only for the top most WPACKET. Additionally753* frees memory resources for this WPACKET.754*/755int WPACKET_finish(WPACKET *pkt);756757/*758* Iterate through all the sub-packets and write out their lengths as if they759* were being closed. The lengths will be overwritten with the final lengths760* when the sub-packets are eventually closed (which may be different if more761* data is added to the WPACKET). This function fails if a sub-packet is of 0762* length and WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH is set.763*/764int WPACKET_fill_lengths(WPACKET *pkt);765766/*767* Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated768* at the start of the sub-packet to store its length once we know it. Don't769* call this directly. Use the convenience macros below instead.770*/771int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);772773/*774* Convenience macros for calling WPACKET_start_sub_packet_len with different775* lengths776*/777#define WPACKET_start_sub_packet_u8(pkt) \778WPACKET_start_sub_packet_len__((pkt), 1)779#define WPACKET_start_sub_packet_u16(pkt) \780WPACKET_start_sub_packet_len__((pkt), 2)781#define WPACKET_start_sub_packet_u24(pkt) \782WPACKET_start_sub_packet_len__((pkt), 3)783#define WPACKET_start_sub_packet_u32(pkt) \784WPACKET_start_sub_packet_len__((pkt), 4)785786/*787* Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated788* for the sub-packet length.789*/790int WPACKET_start_sub_packet(WPACKET *pkt);791792/*793* Allocate bytes in the WPACKET for the output. This reserves the bytes794* and counts them as "written", but doesn't actually do the writing. A pointer795* to the allocated bytes is stored in |*allocbytes|. |allocbytes| may be NULL.796* WARNING: the allocated bytes must be filled in immediately, without further797* WPACKET_* calls. If not then the underlying buffer may be realloc'd and798* change its location.799*/800int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,801unsigned char **allocbytes);802803/*804* The same as WPACKET_allocate_bytes() except additionally a new sub-packet is805* started for the allocated bytes, and then closed immediately afterwards. The806* number of length bytes for the sub-packet is in |lenbytes|. Don't call this807* directly. Use the convenience macros below instead.808*/809int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,810unsigned char **allocbytes, size_t lenbytes);811812/*813* Convenience macros for calling WPACKET_sub_allocate_bytes with different814* lengths815*/816#define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \817WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)818#define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \819WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)820#define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \821WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)822#define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \823WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)824825/*826* The same as WPACKET_allocate_bytes() except the reserved bytes are not827* actually counted as written. Typically this will be for when we don't know828* how big arbitrary data is going to be up front, but we do know what the829* maximum size will be. If this function is used, then it should be immediately830* followed by a WPACKET_allocate_bytes() call before any other WPACKET831* functions are called (unless the write to the allocated bytes is abandoned).832*833* For example: If we are generating a signature, then the size of that834* signature may not be known in advance. We can use WPACKET_reserve_bytes() to835* handle this:836*837* if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_get_size(pkey), &sigbytes1)838* || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0839* || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)840* || sigbytes1 != sigbytes2)841* goto err;842*/843int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);844845/*846* The "reserve_bytes" equivalent of WPACKET_sub_allocate_bytes__()847*/848int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,849unsigned char **allocbytes, size_t lenbytes);850851/*852* Convenience macros for WPACKET_sub_reserve_bytes with different lengths853*/854#define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \855WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)856#define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \857WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)858#define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \859WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)860#define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \861WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)862863/*864* Write the value stored in |val| into the WPACKET. The value will consume865* |bytes| amount of storage. An error will occur if |val| cannot be866* accommodated in |bytes| storage, e.g. attempting to write the value 256 into867* 1 byte will fail. Don't call this directly. Use the convenience macros below868* instead.869*/870int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t bytes);871872/*873* Convenience macros for calling WPACKET_put_bytes with different874* lengths875*/876#define WPACKET_put_bytes_u8(pkt, val) \877WPACKET_put_bytes__((pkt), (val), 1)878#define WPACKET_put_bytes_u16(pkt, val) \879WPACKET_put_bytes__((pkt), (val), 2)880#define WPACKET_put_bytes_u24(pkt, val) \881WPACKET_put_bytes__((pkt), (val), 3)882#define WPACKET_put_bytes_u32(pkt, val) \883WPACKET_put_bytes__((pkt), (val), 4)884#define WPACKET_put_bytes_u64(pkt, val) \885WPACKET_put_bytes__((pkt), (val), 8)886887/* Set a maximum size that we will not allow the WPACKET to grow beyond */888int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);889890/* Copy |len| bytes of data from |*src| into the WPACKET. */891int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);892893/* Set |len| bytes of data to |ch| into the WPACKET. */894int WPACKET_memset(WPACKET *pkt, int ch, size_t len);895896/*897* Copy |len| bytes of data from |*src| into the WPACKET and prefix with its898* length (consuming |lenbytes| of data for the length). Don't call this899* directly. Use the convenience macros below instead.900*/901int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,902size_t lenbytes);903904/* Convenience macros for calling WPACKET_sub_memcpy with different lengths */905#define WPACKET_sub_memcpy_u8(pkt, src, len) \906WPACKET_sub_memcpy__((pkt), (src), (len), 1)907#define WPACKET_sub_memcpy_u16(pkt, src, len) \908WPACKET_sub_memcpy__((pkt), (src), (len), 2)909#define WPACKET_sub_memcpy_u24(pkt, src, len) \910WPACKET_sub_memcpy__((pkt), (src), (len), 3)911#define WPACKET_sub_memcpy_u32(pkt, src, len) \912WPACKET_sub_memcpy__((pkt), (src), (len), 4)913914/*915* Return the total number of bytes written so far to the underlying buffer916* including any storage allocated for length bytes917*/918int WPACKET_get_total_written(WPACKET *pkt, size_t *written);919920/*921* Returns the length of the current sub-packet. This excludes any bytes922* allocated for the length itself.923*/924int WPACKET_get_length(WPACKET *pkt, size_t *len);925926/*927* Returns a pointer to the current write location, but does not allocate any928* bytes.929*/930unsigned char *WPACKET_get_curr(WPACKET *pkt);931932/* Returns true if the underlying buffer is actually NULL */933int WPACKET_is_null_buf(WPACKET *pkt);934935/* Release resources in a WPACKET if a failure has occurred. */936void WPACKET_cleanup(WPACKET *pkt);937938#endif /* OSSL_INTERNAL_PACKET_H */939940941