Path: blob/main/crypto/openssl/include/internal/packet.h
105223 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) || !PACKET_get_bytes(&tmp, &data, (size_t)length)) {520return 0;521}522523*pkt = tmp;524subpkt->curr = data;525subpkt->remaining = length;526527return 1;528}529530/*531* Like PACKET_get_length_prefixed_1, but additionally, fails when there are532* leftover bytes in |pkt|.533*/534__owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,535PACKET *subpkt)536{537unsigned int length;538const unsigned char *data;539PACKET tmp = *pkt;540if (!PACKET_get_1(&tmp, &length) || !PACKET_get_bytes(&tmp, &data, (size_t)length) || PACKET_remaining(&tmp) != 0) {541return 0;542}543544*pkt = tmp;545subpkt->curr = data;546subpkt->remaining = length;547548return 1;549}550551/*552* Reads a variable-length vector prefixed with a two-byte length, and stores553* the contents in |subpkt|. |pkt| can equal |subpkt|.554* Data is not copied: the |subpkt| packet will share its underlying buffer with555* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.556* Upon failure, the original |pkt| and |subpkt| are not modified.557*/558__owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,559PACKET *subpkt)560{561unsigned int length;562const unsigned char *data;563PACKET tmp = *pkt;564565if (!PACKET_get_net_2(&tmp, &length) || !PACKET_get_bytes(&tmp, &data, (size_t)length)) {566return 0;567}568569*pkt = tmp;570subpkt->curr = data;571subpkt->remaining = length;572573return 1;574}575576/*577* Like PACKET_get_length_prefixed_2, but additionally, fails when there are578* leftover bytes in |pkt|.579*/580__owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,581PACKET *subpkt)582{583unsigned int length;584const unsigned char *data;585PACKET tmp = *pkt;586587if (!PACKET_get_net_2(&tmp, &length) || !PACKET_get_bytes(&tmp, &data, (size_t)length) || PACKET_remaining(&tmp) != 0) {588return 0;589}590591*pkt = tmp;592subpkt->curr = data;593subpkt->remaining = length;594595return 1;596}597598/*599* Reads a variable-length vector prefixed with a three-byte length, and stores600* the contents in |subpkt|. |pkt| can equal |subpkt|.601* Data is not copied: the |subpkt| packet will share its underlying buffer with602* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.603* Upon failure, the original |pkt| and |subpkt| are not modified.604*/605__owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,606PACKET *subpkt)607{608unsigned long length;609const unsigned char *data;610PACKET tmp = *pkt;611if (!PACKET_get_net_3(&tmp, &length) || !PACKET_get_bytes(&tmp, &data, (size_t)length)) {612return 0;613}614615*pkt = tmp;616subpkt->curr = data;617subpkt->remaining = length;618619return 1;620}621622/* Writable packets */623624typedef struct wpacket_sub WPACKET_SUB;625struct wpacket_sub {626/* The parent WPACKET_SUB if we have one or NULL otherwise */627WPACKET_SUB *parent;628629/*630* Offset into the buffer where the length of this WPACKET goes. We use an631* offset in case the buffer grows and gets reallocated.632*/633size_t packet_len;634635/* Number of bytes in the packet_len or 0 if we don't write the length */636size_t lenbytes;637638/* Number of bytes written to the buf prior to this packet starting */639size_t pwritten;640641/* Flags for this sub-packet */642unsigned int flags;643};644645typedef struct wpacket_st WPACKET;646struct wpacket_st {647/* The buffer where we store the output data */648BUF_MEM *buf;649650/* Fixed sized buffer which can be used as an alternative to buf */651unsigned char *staticbuf;652653/*654* Offset into the buffer where we are currently writing. We use an offset655* in case the buffer grows and gets reallocated.656*/657size_t curr;658659/* Number of bytes written so far */660size_t written;661662/* Maximum number of bytes we will allow to be written to this WPACKET */663size_t maxsize;664665/* Our sub-packets (always at least one if not finished) */666WPACKET_SUB *subs;667668/* Writing from the end first? */669unsigned int endfirst : 1;670};671672/* Flags */673674/* Default */675#define WPACKET_FLAGS_NONE 0676677/* Error on WPACKET_close() if no data written to the WPACKET */678#define WPACKET_FLAGS_NON_ZERO_LENGTH 1679680/*681* Abandon all changes on WPACKET_close() if no data written to the WPACKET,682* i.e. this does not write out a zero packet length683*/684#define WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH 2685686/* QUIC variable-length integer length prefix */687#define WPACKET_FLAGS_QUIC_VLINT 4688689/*690* Initialise a WPACKET with the buffer in |buf|. The buffer must exist691* for the whole time that the WPACKET is being used. Additionally |lenbytes| of692* data is preallocated at the start of the buffer to store the length of the693* WPACKET once we know it.694*/695int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);696697/*698* Same as WPACKET_init_len except there is no preallocation of the WPACKET699* length.700*/701int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);702703/*704* Same as WPACKET_init_len except there is no underlying buffer. No data is705* ever actually written. We just keep track of how much data would have been706* written if a buffer was there.707*/708int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);709710/*711* Same as WPACKET_init_null except we set the WPACKET to assume DER length712* encoding for sub-packets.713*/714int WPACKET_init_null_der(WPACKET *pkt);715716/*717* Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.718* A fixed buffer of memory |buf| of size |len| is used instead. A failure will719* occur if you attempt to write beyond the end of the buffer720*/721int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,722size_t lenbytes);723724/*725* Same as WPACKET_init_static_len except lenbytes is always 0, and we set the726* WPACKET to write to the end of the buffer moving towards the start and use727* DER length encoding for sub-packets.728*/729int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);730731/*732* Set the flags to be applied to the current sub-packet733*/734int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);735736/*737* Closes the most recent sub-packet. It also writes out the length of the738* packet to the required location (normally the start of the WPACKET) if739* appropriate. The top level WPACKET should be closed using WPACKET_finish()740* instead of this function.741*/742int WPACKET_close(WPACKET *pkt);743744/*745* The same as WPACKET_close() but only for the top most WPACKET. Additionally746* frees memory resources for this WPACKET.747*/748int WPACKET_finish(WPACKET *pkt);749750/*751* Iterate through all the sub-packets and write out their lengths as if they752* were being closed. The lengths will be overwritten with the final lengths753* when the sub-packets are eventually closed (which may be different if more754* data is added to the WPACKET). This function fails if a sub-packet is of 0755* length and WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH is set.756*/757int WPACKET_fill_lengths(WPACKET *pkt);758759/*760* Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated761* at the start of the sub-packet to store its length once we know it. Don't762* call this directly. Use the convenience macros below instead.763*/764int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);765766/*767* Convenience macros for calling WPACKET_start_sub_packet_len with different768* lengths769*/770#define WPACKET_start_sub_packet_u8(pkt) \771WPACKET_start_sub_packet_len__((pkt), 1)772#define WPACKET_start_sub_packet_u16(pkt) \773WPACKET_start_sub_packet_len__((pkt), 2)774#define WPACKET_start_sub_packet_u24(pkt) \775WPACKET_start_sub_packet_len__((pkt), 3)776#define WPACKET_start_sub_packet_u32(pkt) \777WPACKET_start_sub_packet_len__((pkt), 4)778779/*780* Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated781* for the sub-packet length.782*/783int WPACKET_start_sub_packet(WPACKET *pkt);784785/*786* Allocate bytes in the WPACKET for the output. This reserves the bytes787* and counts them as "written", but doesn't actually do the writing. A pointer788* to the allocated bytes is stored in |*allocbytes|. |allocbytes| may be NULL.789* WARNING: the allocated bytes must be filled in immediately, without further790* WPACKET_* calls. If not then the underlying buffer may be realloc'd and791* change its location.792*/793int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,794unsigned char **allocbytes);795796/*797* The same as WPACKET_allocate_bytes() except additionally a new sub-packet is798* started for the allocated bytes, and then closed immediately afterwards. The799* number of length bytes for the sub-packet is in |lenbytes|. Don't call this800* directly. Use the convenience macros below instead.801*/802int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,803unsigned char **allocbytes, size_t lenbytes);804805/*806* Convenience macros for calling WPACKET_sub_allocate_bytes with different807* lengths808*/809#define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \810WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)811#define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \812WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)813#define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \814WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)815#define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \816WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)817818/*819* The same as WPACKET_allocate_bytes() except the reserved bytes are not820* actually counted as written. Typically this will be for when we don't know821* how big arbitrary data is going to be up front, but we do know what the822* maximum size will be. If this function is used, then it should be immediately823* followed by a WPACKET_allocate_bytes() call before any other WPACKET824* functions are called (unless the write to the allocated bytes is abandoned).825*826* For example: If we are generating a signature, then the size of that827* signature may not be known in advance. We can use WPACKET_reserve_bytes() to828* handle this:829*830* if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_get_size(pkey), &sigbytes1)831* || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0832* || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)833* || sigbytes1 != sigbytes2)834* goto err;835*/836int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);837838/*839* The "reserve_bytes" equivalent of WPACKET_sub_allocate_bytes__()840*/841int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,842unsigned char **allocbytes, size_t lenbytes);843844/*845* Convenience macros for WPACKET_sub_reserve_bytes with different lengths846*/847#define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \848WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)849#define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \850WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)851#define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \852WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)853#define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \854WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)855856/*857* Write the value stored in |val| into the WPACKET. The value will consume858* |bytes| amount of storage. An error will occur if |val| cannot be859* accommodated in |bytes| storage, e.g. attempting to write the value 256 into860* 1 byte will fail. Don't call this directly. Use the convenience macros below861* instead.862*/863int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t bytes);864865/*866* Convenience macros for calling WPACKET_put_bytes with different867* lengths868*/869#define WPACKET_put_bytes_u8(pkt, val) \870WPACKET_put_bytes__((pkt), (val), 1)871#define WPACKET_put_bytes_u16(pkt, val) \872WPACKET_put_bytes__((pkt), (val), 2)873#define WPACKET_put_bytes_u24(pkt, val) \874WPACKET_put_bytes__((pkt), (val), 3)875#define WPACKET_put_bytes_u32(pkt, val) \876WPACKET_put_bytes__((pkt), (val), 4)877#define WPACKET_put_bytes_u64(pkt, val) \878WPACKET_put_bytes__((pkt), (val), 8)879880/* Set a maximum size that we will not allow the WPACKET to grow beyond */881int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);882883/* Copy |len| bytes of data from |*src| into the WPACKET. */884int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);885886/* Set |len| bytes of data to |ch| into the WPACKET. */887int WPACKET_memset(WPACKET *pkt, int ch, size_t len);888889/*890* Copy |len| bytes of data from |*src| into the WPACKET and prefix with its891* length (consuming |lenbytes| of data for the length). Don't call this892* directly. Use the convenience macros below instead.893*/894int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,895size_t lenbytes);896897/* Convenience macros for calling WPACKET_sub_memcpy with different lengths */898#define WPACKET_sub_memcpy_u8(pkt, src, len) \899WPACKET_sub_memcpy__((pkt), (src), (len), 1)900#define WPACKET_sub_memcpy_u16(pkt, src, len) \901WPACKET_sub_memcpy__((pkt), (src), (len), 2)902#define WPACKET_sub_memcpy_u24(pkt, src, len) \903WPACKET_sub_memcpy__((pkt), (src), (len), 3)904#define WPACKET_sub_memcpy_u32(pkt, src, len) \905WPACKET_sub_memcpy__((pkt), (src), (len), 4)906907/*908* Return the total number of bytes written so far to the underlying buffer909* including any storage allocated for length bytes910*/911int WPACKET_get_total_written(WPACKET *pkt, size_t *written);912913/*914* Returns the length of the current sub-packet. This excludes any bytes915* allocated for the length itself.916*/917int WPACKET_get_length(WPACKET *pkt, size_t *len);918919/*920* Returns a pointer to the current write location, but does not allocate any921* bytes.922*/923unsigned char *WPACKET_get_curr(WPACKET *pkt);924925/* Returns true if the underlying buffer is actually NULL */926int WPACKET_is_null_buf(WPACKET *pkt);927928/* Release resources in a WPACKET if a failure has occurred. */929void WPACKET_cleanup(WPACKET *pkt);930931#endif /* OSSL_INTERNAL_PACKET_H */932933934