-/* ssl/packet_locl.h */
/*
* Written by Matt Caswell for the OpenSSL project.
*/
typedef struct {
/* Pointer to where we are currently reading from */
- unsigned char *curr;
+ const unsigned char *curr;
/* Number of bytes remaining */
size_t remaining;
} PACKET;
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.
- * TODO(openssl-team): this should return 'const unsigned char*' but can't
- * currently because legacy code passes 'unsigned char*'s around.
*/
-static ossl_inline unsigned char *PACKET_data(const PACKET *pkt)
+static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
{
return pkt->curr;
}
* 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, unsigned char *buf,
+__owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
+ const unsigned char *buf,
size_t len)
{
/* Sanity check for negative values. */
* underlying buffer gets freed
*/
__owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
- unsigned char **data,
+ const unsigned char **data,
size_t len)
{
if (PACKET_remaining(pkt) < len)
* freed
*/
__owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
- unsigned char **data,
+ const unsigned char **data,
size_t len)
{
if (!PACKET_peek_bytes(pkt, data, len))
return (*data != NULL);
}
+/* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
+static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
+{
+ return memchr(pkt->curr, 0, pkt->remaining) != NULL;
+}
+
/* Move the current reading position forward |len| bytes */
__owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
{
PACKET *subpkt)
{
unsigned int length;
- unsigned char *data;
+ const unsigned char *data;
PACKET tmp = *pkt;
if (!PACKET_get_1(&tmp, &length) ||
!PACKET_get_bytes(&tmp, &data, (size_t)length)) {
return 1;
}
+/*
+ * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
+ * leftover bytes in |pkt|.
+ */
+__owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
+{
+ unsigned int length;
+ const unsigned char *data;
+ PACKET tmp = *pkt;
+ if (!PACKET_get_1(&tmp, &length) ||
+ !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
+ PACKET_remaining(&tmp) != 0) {
+ return 0;
+ }
+
+ *pkt = tmp;
+ subpkt->curr = data;
+ subpkt->remaining = length;
+
+ return 1;
+}
+
/*
* Reads a variable-length vector prefixed with a two-byte length, and stores
* the contents in |subpkt|. |pkt| can equal |subpkt|.
PACKET *subpkt)
{
unsigned int length;
- unsigned char *data;
+ const unsigned char *data;
PACKET tmp = *pkt;
+
if (!PACKET_get_net_2(&tmp, &length) ||
!PACKET_get_bytes(&tmp, &data, (size_t)length)) {
return 0;
return 1;
}
+/*
+ * Like PACKET_get_length_prefixed_2, but additionally, fails when there are
+ * leftover bytes in |pkt|.
+ */
+__owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
+ PACKET *subpkt)
+{
+ unsigned int length;
+ const unsigned char *data;
+ PACKET tmp = *pkt;
+
+ if (!PACKET_get_net_2(&tmp, &length) ||
+ !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
+ PACKET_remaining(&tmp) != 0) {
+ return 0;
+ }
+
+ *pkt = tmp;
+ subpkt->curr = data;
+ subpkt->remaining = length;
+
+ return 1;
+}
+
/*
* Reads a variable-length vector prefixed with a three-byte length, and stores
* the contents in |subpkt|. |pkt| can equal |subpkt|.
PACKET *subpkt)
{
unsigned long length;
- unsigned char *data;
+ const unsigned char *data;
PACKET tmp = *pkt;
if (!PACKET_get_net_3(&tmp, &length) ||
!PACKET_get_bytes(&tmp, &data, (size_t)length)) {