Path: blob/main/crypto/openssl/include/internal/packet_quic.h
34879 views
/*1* Copyright 2022-2023 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_QUIC_H10# define OSSL_INTERNAL_PACKET_QUIC_H11# pragma once1213# include "internal/packet.h"14# include "internal/quic_vlint.h"1516# ifndef OPENSSL_NO_QUIC17/*18* Decodes a QUIC variable-length integer in |pkt| and stores the result in19* |data|.20*/21__owur static ossl_inline int PACKET_get_quic_vlint(PACKET *pkt,22uint64_t *data)23{24size_t enclen;2526if (PACKET_remaining(pkt) < 1)27return 0;2829enclen = ossl_quic_vlint_decode_len(*pkt->curr);3031if (PACKET_remaining(pkt) < enclen)32return 0;3334*data = ossl_quic_vlint_decode_unchecked(pkt->curr);35packet_forward(pkt, enclen);36return 1;37}3839/*40* Decodes a QUIC variable-length integer in |pkt| and stores the result in41* |data|. Unlike PACKET_get_quic_vlint, this does not advance the current42* position. If was_minimal is non-NULL, *was_minimal is set to 1 if the integer43* was encoded using the minimal possible number of bytes and 0 otherwise.44*/45__owur static ossl_inline int PACKET_peek_quic_vlint_ex(PACKET *pkt,46uint64_t *data,47int *was_minimal)48{49size_t enclen;5051if (PACKET_remaining(pkt) < 1)52return 0;5354enclen = ossl_quic_vlint_decode_len(*pkt->curr);5556if (PACKET_remaining(pkt) < enclen)57return 0;5859*data = ossl_quic_vlint_decode_unchecked(pkt->curr);6061if (was_minimal != NULL)62*was_minimal = (enclen == ossl_quic_vlint_encode_len(*data));6364return 1;65}6667__owur static ossl_inline int PACKET_peek_quic_vlint(PACKET *pkt,68uint64_t *data)69{70return PACKET_peek_quic_vlint_ex(pkt, data, NULL);71}7273/*74* Skips over a QUIC variable-length integer in |pkt| without decoding it.75*/76__owur static ossl_inline int PACKET_skip_quic_vlint(PACKET *pkt)77{78size_t enclen;7980if (PACKET_remaining(pkt) < 1)81return 0;8283enclen = ossl_quic_vlint_decode_len(*pkt->curr);8485if (PACKET_remaining(pkt) < enclen)86return 0;8788packet_forward(pkt, enclen);89return 1;90}9192/*93* Reads a variable-length vector prefixed with a QUIC variable-length integer94* denoting the length, and stores the contents in |subpkt|. |pkt| can equal95* |subpkt|. Data is not copied: the |subpkt| packet will share its underlying96* buffer with the original |pkt|, so data wrapped by |pkt| must outlive the97* |subpkt|. Upon failure, the original |pkt| and |subpkt| are not modified.98*/99__owur static ossl_inline int PACKET_get_quic_length_prefixed(PACKET *pkt,100PACKET *subpkt)101{102uint64_t length;103const unsigned char *data;104PACKET tmp = *pkt;105106if (!PACKET_get_quic_vlint(&tmp, &length) ||107length > SIZE_MAX ||108!PACKET_get_bytes(&tmp, &data, (size_t)length)) {109return 0;110}111112*pkt = tmp;113subpkt->curr = data;114subpkt->remaining = (size_t)length;115116return 1;117}118119/*120* Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte121* representation is used.122*/123__owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);124125/*126* Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len127* specifies the upper bound for the sub-packet size at the time the sub-packet128* is closed, which determines the encoding size for the variable-length129* integer header. max_len can be a precise figure or a worst-case bound130* if a precise figure is not available.131*/132__owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);133134/*135* Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a136* QUIC variable-length integer. The pointer to the payload buffer is output and137* must be filled by the caller. This function assures optimal selection of138* variable-length integer encoding length.139*/140__owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,141unsigned char **bytes);142143/*144* Write a QUIC variable-length integer to the packet.145*/146__owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);147148# endif /* OPENSSL_NO_QUIC */149#endif /* OSSL_INTERNAL_PACKET_QUIC_H */150151152