Path: blob/main/crypto/openssl/include/internal/packet_quic.h
105548 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) || length > SIZE_MAX || !PACKET_get_bytes(&tmp, &data, (size_t)length)) {107return 0;108}109110*pkt = tmp;111subpkt->curr = data;112subpkt->remaining = (size_t)length;113114return 1;115}116117/*118* Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte119* representation is used.120*/121__owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);122123/*124* Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len125* specifies the upper bound for the sub-packet size at the time the sub-packet126* is closed, which determines the encoding size for the variable-length127* integer header. max_len can be a precise figure or a worst-case bound128* if a precise figure is not available.129*/130__owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);131132/*133* Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a134* QUIC variable-length integer. The pointer to the payload buffer is output and135* must be filled by the caller. This function assures optimal selection of136* variable-length integer encoding length.137*/138__owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,139unsigned char **bytes);140141/*142* Write a QUIC variable-length integer to the packet.143*/144__owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);145146#endif /* OPENSSL_NO_QUIC */147#endif /* OSSL_INTERNAL_PACKET_QUIC_H */148149150