2016-08-04 00:06:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (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
|
|
|
|
*/
|
|
|
|
|
2016-09-09 16:49:16 +08:00
|
|
|
#include <assert.h>
|
2016-08-04 00:06:39 +08:00
|
|
|
#include "packet_locl.h"
|
|
|
|
|
2016-09-08 16:58:29 +08:00
|
|
|
#define DEFAULT_BUF_SIZE 256
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
|
2016-09-29 18:43:37 +08:00
|
|
|
{
|
|
|
|
if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
|
|
|
|
return 0;
|
2016-09-29 21:39:47 +08:00
|
|
|
|
2016-09-29 18:43:37 +08:00
|
|
|
pkt->written += len;
|
|
|
|
pkt->curr += len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
|
|
|
|
unsigned char **allocbytes, size_t lenbytes)
|
|
|
|
{
|
|
|
|
if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
|
|
|
|
|| !WPACKET_allocate_bytes(pkt, len, allocbytes)
|
|
|
|
|| !WPACKET_close(pkt))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL && len != 0);
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->subs == NULL || len == 0)
|
|
|
|
return 0;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-09 06:08:53 +08:00
|
|
|
if (pkt->maxsize - pkt->written < len)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->buf->length - pkt->written < len) {
|
2016-08-04 00:06:39 +08:00
|
|
|
size_t newlen;
|
2016-09-23 23:41:50 +08:00
|
|
|
size_t reflen;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-23 23:41:50 +08:00
|
|
|
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
|
|
|
|
|
|
|
|
if (reflen > SIZE_MAX / 2) {
|
2016-08-04 00:06:39 +08:00
|
|
|
newlen = SIZE_MAX;
|
2016-09-08 16:58:29 +08:00
|
|
|
} else {
|
2016-09-23 23:41:50 +08:00
|
|
|
newlen = reflen * 2;
|
|
|
|
if (newlen < DEFAULT_BUF_SIZE)
|
|
|
|
newlen = DEFAULT_BUF_SIZE;
|
2016-09-08 16:58:29 +08:00
|
|
|
}
|
2016-09-06 22:09:51 +08:00
|
|
|
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
|
|
|
|
return 0;
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
2016-09-09 07:13:41 +08:00
|
|
|
*allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
return 1;
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 18:43:37 +08:00
|
|
|
int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
|
|
|
|
unsigned char **allocbytes, size_t lenbytes)
|
2016-09-13 18:32:52 +08:00
|
|
|
{
|
2016-09-29 18:43:37 +08:00
|
|
|
if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
|
2016-09-13 18:32:52 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-29 18:43:37 +08:00
|
|
|
*allocbytes += lenbytes;
|
|
|
|
|
2016-09-13 18:32:52 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-08 16:58:29 +08:00
|
|
|
static size_t maxmaxsize(size_t lenbytes)
|
|
|
|
{
|
|
|
|
if (lenbytes >= sizeof(size_t) || lenbytes == 0)
|
|
|
|
return SIZE_MAX;
|
2016-09-12 16:41:01 +08:00
|
|
|
|
|
|
|
return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
|
2016-09-08 16:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-09 07:13:41 +08:00
|
|
|
unsigned char *lenchars;
|
|
|
|
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(buf != NULL);
|
2016-08-04 00:06:39 +08:00
|
|
|
if (buf == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
pkt->buf = buf;
|
2016-09-09 07:13:41 +08:00
|
|
|
pkt->curr = 0;
|
2016-09-06 22:09:51 +08:00
|
|
|
pkt->written = 0;
|
2016-09-08 16:58:29 +08:00
|
|
|
pkt->maxsize = maxmaxsize(lenbytes);
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
|
|
|
|
if (pkt->subs == NULL)
|
|
|
|
return 0;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
if (lenbytes == 0)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 1;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
pkt->subs->pwritten = lenbytes;
|
|
|
|
pkt->subs->lenbytes = lenbytes;
|
|
|
|
|
2016-09-09 07:13:41 +08:00
|
|
|
if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
|
2016-09-06 22:09:51 +08:00
|
|
|
OPENSSL_free(pkt->subs);
|
|
|
|
pkt->subs = NULL;
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-09 07:13:41 +08:00
|
|
|
pkt->subs->packet_len = lenchars - (unsigned char *)pkt->buf->data;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-06 00:34:04 +08:00
|
|
|
return WPACKET_init_len(pkt, buf, 0);
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL);
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->subs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pkt->subs->flags = flags;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 16:40:38 +08:00
|
|
|
/* Store the |value| of length |len| at location |data| */
|
|
|
|
static int put_value(unsigned char *data, size_t value, size_t len)
|
2016-09-12 16:39:10 +08:00
|
|
|
{
|
2016-09-13 16:40:38 +08:00
|
|
|
for (data += len - 1; len > 0; len--) {
|
2016-09-12 16:39:10 +08:00
|
|
|
*data = (unsigned char)(value & 0xff);
|
|
|
|
data--;
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether we could fit the value in the assigned number of bytes */
|
|
|
|
if (value > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
|
2016-08-04 00:06:39 +08:00
|
|
|
/*
|
2016-09-06 22:09:51 +08:00
|
|
|
* Internal helper function used by WPACKET_close() and WPACKET_finish() to
|
|
|
|
* close a sub-packet and write out its length if necessary.
|
2016-08-04 00:06:39 +08:00
|
|
|
*/
|
2016-09-06 22:09:51 +08:00
|
|
|
static int wpacket_intern_close(WPACKET *pkt)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-06 22:09:51 +08:00
|
|
|
WPACKET_SUB *sub = pkt->subs;
|
2016-09-09 07:13:41 +08:00
|
|
|
size_t packlen = pkt->written - sub->pwritten;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
if (packlen == 0
|
2016-09-13 16:40:38 +08:00
|
|
|
&& (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (packlen == 0
|
2016-09-09 07:13:41 +08:00
|
|
|
&& sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
|
2016-09-06 00:34:04 +08:00
|
|
|
/* Deallocate any bytes allocated for the length of the WPACKET */
|
2016-09-06 22:09:51 +08:00
|
|
|
if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
|
|
|
|
pkt->written -= sub->lenbytes;
|
|
|
|
pkt->curr -= sub->lenbytes;
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't write out the packet length */
|
2016-09-09 07:13:41 +08:00
|
|
|
sub->packet_len = 0;
|
|
|
|
sub->lenbytes = 0;
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
/* Write out the WPACKET length if needed */
|
2016-10-11 00:01:24 +08:00
|
|
|
if (sub->lenbytes > 0
|
2016-09-12 16:39:10 +08:00
|
|
|
&& !put_value((unsigned char *)&pkt->buf->data[sub->packet_len],
|
|
|
|
packlen, sub->lenbytes))
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
pkt->subs = sub->parent;
|
|
|
|
OPENSSL_free(sub);
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
int WPACKET_close(WPACKET *pkt)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-09 16:49:16 +08:00
|
|
|
/*
|
|
|
|
* Internal API, so should not fail - but we do negative testing of this
|
|
|
|
* so no assert (otherwise the tests fail)
|
|
|
|
*/
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->subs == NULL || pkt->subs->parent == NULL)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
return wpacket_intern_close(pkt);
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
int WPACKET_finish(WPACKET *pkt)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-06 22:09:51 +08:00
|
|
|
int ret;
|
|
|
|
|
2016-09-09 16:49:16 +08:00
|
|
|
/*
|
|
|
|
* Internal API, so should not fail - but we do negative testing of this
|
|
|
|
* so no assert (otherwise the tests fail)
|
|
|
|
*/
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->subs == NULL || pkt->subs->parent != NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = wpacket_intern_close(pkt);
|
2016-09-08 16:58:29 +08:00
|
|
|
if (ret) {
|
|
|
|
OPENSSL_free(pkt->subs);
|
|
|
|
pkt->subs = NULL;
|
|
|
|
}
|
2016-09-09 07:13:41 +08:00
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
return ret;
|
2016-08-04 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:42:12 +08:00
|
|
|
int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-06 22:09:51 +08:00
|
|
|
WPACKET_SUB *sub;
|
2016-09-09 07:13:41 +08:00
|
|
|
unsigned char *lenchars;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL);
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->subs == NULL)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
sub = OPENSSL_zalloc(sizeof(*sub));
|
|
|
|
if (sub == NULL)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
sub->parent = pkt->subs;
|
|
|
|
pkt->subs = sub;
|
|
|
|
sub->pwritten = pkt->written + lenbytes;
|
|
|
|
sub->lenbytes = lenbytes;
|
|
|
|
|
|
|
|
if (lenbytes == 0) {
|
2016-09-09 07:13:41 +08:00
|
|
|
sub->packet_len = 0;
|
2016-09-06 22:09:51 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-09 07:13:41 +08:00
|
|
|
if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
|
2016-09-06 22:09:51 +08:00
|
|
|
return 0;
|
2016-09-21 18:20:18 +08:00
|
|
|
/* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
|
2016-09-09 07:13:41 +08:00
|
|
|
sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
int WPACKET_start_sub_packet(WPACKET *pkt)
|
|
|
|
{
|
2016-09-13 22:42:12 +08:00
|
|
|
return WPACKET_start_sub_packet_len__(pkt, 0);
|
2016-09-06 22:09:51 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 21:47:44 +08:00
|
|
|
int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
|
|
|
unsigned char *data;
|
|
|
|
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(size <= sizeof(unsigned int));
|
2016-09-13 16:40:38 +08:00
|
|
|
|
2016-09-09 07:13:41 +08:00
|
|
|
if (size > sizeof(unsigned int)
|
2016-09-12 16:39:10 +08:00
|
|
|
|| !WPACKET_allocate_bytes(pkt, size, &data)
|
|
|
|
|| !put_value(data, val, size))
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-08 16:58:29 +08:00
|
|
|
WPACKET_SUB *sub;
|
|
|
|
size_t lenbytes;
|
|
|
|
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL);
|
2016-09-08 16:58:29 +08:00
|
|
|
if (pkt->subs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Find the WPACKET_SUB for the top level */
|
2016-09-09 07:13:41 +08:00
|
|
|
for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
|
|
|
|
continue;
|
2016-09-08 16:58:29 +08:00
|
|
|
|
|
|
|
lenbytes = sub->lenbytes;
|
|
|
|
if (lenbytes == 0)
|
|
|
|
lenbytes = sizeof(pkt->maxsize);
|
|
|
|
|
|
|
|
if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
pkt->maxsize = maxsize;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
|
|
|
unsigned char *dest;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 1;
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
if (!WPACKET_allocate_bytes(pkt, len, &dest))
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy(dest, src, len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 22:42:12 +08:00
|
|
|
int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
|
|
|
|
size_t lenbytes)
|
2016-09-06 22:19:32 +08:00
|
|
|
{
|
2016-09-13 22:42:12 +08:00
|
|
|
if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
|
2016-09-06 22:19:32 +08:00
|
|
|
|| !WPACKET_memcpy(pkt, src, len)
|
|
|
|
|| !WPACKET_close(pkt))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(written != NULL);
|
2016-09-08 16:58:29 +08:00
|
|
|
if (written == NULL)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
*written = pkt->written;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:34:04 +08:00
|
|
|
int WPACKET_get_length(WPACKET *pkt, size_t *len)
|
2016-08-04 00:06:39 +08:00
|
|
|
{
|
2016-09-09 16:49:16 +08:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL && len != NULL);
|
2016-09-06 22:09:51 +08:00
|
|
|
if (pkt->subs == NULL || len == NULL)
|
2016-08-04 00:06:39 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 22:09:51 +08:00
|
|
|
*len = pkt->written - pkt->subs->pwritten;
|
2016-08-04 00:06:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-08 16:58:29 +08:00
|
|
|
|
|
|
|
void WPACKET_cleanup(WPACKET *pkt)
|
|
|
|
{
|
|
|
|
WPACKET_SUB *sub, *parent;
|
|
|
|
|
|
|
|
for (sub = pkt->subs; sub != NULL; sub = parent) {
|
|
|
|
parent = sub->parent;
|
|
|
|
OPENSSL_free(sub);
|
|
|
|
}
|
|
|
|
pkt->subs = NULL;
|
|
|
|
}
|