/*
* Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#ifndef OSSL_INTERNAL_PACKET_H
# define OSSL_INTERNAL_PACKET_H
# pragma once
# include <string.h>
# include <openssl/bn.h>
# include <openssl/buffer.h>
# include <openssl/crypto.h>
# include <openssl/e_os2.h>
# include "internal/numbers.h"
typedef struct {
/* Pointer to where we are currently reading from */
const unsigned char *curr;
/* Number of bytes remaining */
size_t remaining;
} PACKET;
/* Internal unchecked shorthand; don't use outside this file. */
static ossl_inline void packet_forward(PACKET *pkt, size_t len)
{
pkt->curr += len;
pkt->remaining -= len;
}
/*
* Returns the number of bytes remaining to be read in the PACKET
*/
static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
{
return pkt->remaining;
}
/*
* Returns a pointer to the first byte after the packet data.
* Useful for integrating with non-PACKET parsing code.
* Specifically, we use PACKET_end() to verify that a d2i_... call
* has consumed the entire packet contents.
*/
static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
{
return pkt->curr + pkt->remaining;
}
/*
* Returns a pointer to the PACKET's current position.
* For use in non-PACKETized APIs.
*/
static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
{
return pkt->curr;
}
/*
* Initialise a PACKET with |len| bytes held in |buf|. This does not make a
* copy of the data so |buf| must be present for the whole time that the PACKET
* is being used.
*/
__owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
const unsigned char *buf,
size_t len)
{
/* Sanity check for negative values. */
if (len > (size_t)(SIZE_MAX / 2))
return 0;
pkt->curr = buf;
pkt->remaining = len;
return 1;
}
/* Initialize a PACKET to hold zero bytes. */
static ossl_inline void PACKET_null_init(PACKET *pkt)
{
pkt->curr = NULL;
pkt->remaining = 0;
}
/*
* Returns 1 if the packet has length |num| and its contents equal the |num|
* bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
* If lengths are equal, performs the comparison in constant time.
*/
__owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
size_t num)
{
if (PACKET_remaining(pkt) != num)
return 0;
return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
}
/*
* Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
* Data is not copied: the |subpkt| packet will share its underlying buffer with
* the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
*/
__owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
PACKET *subpkt, size_t len)
{
if (PACKET_remaining(pkt) < len)
return 0;
return PACKET_buf_init(subpkt, pkt->curr, len);
}
/*
* Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
* copied: the |subpkt| packet will share its underlying buffer with the
* original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
*/
__owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
PACKET *subpkt, size_t len)
{
if (!PACKET_peek_sub_packet(pkt, subpkt, len))
return 0;
packet_forward(pkt, len);
return 1;
}
/*
* Peek ahead at 2 bytes in network order from |pkt| and store the value in
* |*data|
*/
__owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
unsigned int *data)
{
if (PACKET_remaining(pkt) < 2)
return 0;
*data = ((unsigned int)(*pkt->curr)) << 8;
*data |= *(pkt->curr + 1);
return 1;
}
/* Equivalent of n2s */
/* Get 2 bytes in network order from |pkt| and store the value in |*data| */
__owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
{
if (!PACKET_peek_net_2(pkt, data))
return 0;
packet_forward(pkt, 2);
return 1;
}
/* Same as PACKET_get_net_2() but for a size_t */
__owur static ossl_inline int PACKET_get_net_2_len(PACKET *pkt, size_t *data)
{
unsigned int i;
int ret = PACKET_get_net_2(pkt, &i);
if (ret)
*data = (size_t)i;
return ret;
}
/*
* Peek ahead at 3 bytes in network order from |pkt| and store the value in
* |*data|
*/
__owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
unsigned long *data)