2016-08-03 16:06:39 +00: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 08:49:16 +00:00
|
|
|
#include <assert.h>
|
2016-08-03 16:06:39 +00:00
|
|
|
#include "packet_locl.h"
|
|
|
|
|
2016-09-08 08:58:29 +00:00
|
|
|
#define DEFAULT_BUF_SIZE 256
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL && len != 0);
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->subs == NULL || len == 0)
|
|
|
|
return 0;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
2016-09-08 22:08:53 +00:00
|
|
|
if (pkt->maxsize - pkt->written < len)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->buf->length - pkt->written < len) {
|
2016-08-03 16:06:39 +00:00
|
|
|
size_t newlen;
|
|
|
|
|
2016-09-08 08:58:29 +00:00
|
|
|
if (pkt->buf->length > SIZE_MAX / 2) {
|
2016-08-03 16:06:39 +00:00
|
|
|
newlen = SIZE_MAX;
|
2016-09-08 08:58:29 +00:00
|
|
|
} else {
|
2016-09-08 23:13:41 +00:00
|
|
|
newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE
|
|
|
|
: pkt->buf->length * 2;
|
2016-09-08 08:58:29 +00:00
|
|
|
}
|
2016-09-06 14:09:51 +00:00
|
|
|
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
|
|
|
|
return 0;
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->written += len;
|
2016-09-08 23:13:41 +00:00
|
|
|
*allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->curr += len;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
return 1;
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 08:58:29 +00:00
|
|
|
static size_t maxmaxsize(size_t lenbytes)
|
|
|
|
{
|
|
|
|
if (lenbytes >= sizeof(size_t) || lenbytes == 0)
|
|
|
|
return SIZE_MAX;
|
|
|
|
else
|
|
|
|
return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-08 23:13:41 +00:00
|
|
|
unsigned char *lenchars;
|
|
|
|
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(buf != NULL);
|
2016-08-03 16:06:39 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->buf = buf;
|
2016-09-08 23:13:41 +00:00
|
|
|
pkt->curr = 0;
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->written = 0;
|
2016-09-08 08:58:29 +00:00
|
|
|
pkt->maxsize = maxmaxsize(lenbytes);
|
2016-08-03 16:06:39 +00:00
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
|
|
|
|
if (pkt->subs == NULL)
|
|
|
|
return 0;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
if (lenbytes == 0)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 1;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->subs->pwritten = lenbytes;
|
|
|
|
pkt->subs->lenbytes = lenbytes;
|
|
|
|
|
2016-09-08 23:13:41 +00:00
|
|
|
if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
|
2016-09-06 14:09:51 +00:00
|
|
|
OPENSSL_free(pkt->subs);
|
|
|
|
pkt->subs = NULL;
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-08 23:13:41 +00:00
|
|
|
pkt->subs->packet_len = lenchars - (unsigned char *)pkt->buf->data;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-05 16:34:04 +00:00
|
|
|
return WPACKET_init_len(pkt, buf, 0);
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL);
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->subs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pkt->subs->flags = flags;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
|
2016-08-03 16:06:39 +00:00
|
|
|
/*
|
2016-09-06 14:09:51 +00: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-03 16:06:39 +00:00
|
|
|
*/
|
2016-09-06 14:09:51 +00:00
|
|
|
static int wpacket_intern_close(WPACKET *pkt)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-06 14:09:51 +00:00
|
|
|
WPACKET_SUB *sub = pkt->subs;
|
2016-09-08 23:13:41 +00:00
|
|
|
size_t packlen = pkt->written - sub->pwritten;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
if (packlen == 0
|
2016-09-08 23:13:41 +00:00
|
|
|
&& sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (packlen == 0
|
2016-09-08 23:13:41 +00:00
|
|
|
&& sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
|
2016-09-05 16:34:04 +00:00
|
|
|
/* Deallocate any bytes allocated for the length of the WPACKET */
|
2016-09-06 14:09:51 +00:00
|
|
|
if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
|
|
|
|
pkt->written -= sub->lenbytes;
|
|
|
|
pkt->curr -= sub->lenbytes;
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't write out the packet length */
|
2016-09-08 23:13:41 +00:00
|
|
|
sub->packet_len = 0;
|
|
|
|
sub->lenbytes = 0;
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
/* Write out the WPACKET length if needed */
|
2016-09-08 23:13:41 +00:00
|
|
|
if (sub->lenbytes > 0) {
|
2016-08-03 16:06:39 +00:00
|
|
|
size_t lenbytes;
|
|
|
|
|
2016-09-08 23:13:41 +00:00
|
|
|
for (lenbytes = sub->lenbytes; lenbytes > 0; lenbytes--) {
|
|
|
|
pkt->buf->data[sub->packet_len + lenbytes - 1]
|
2016-09-06 14:09:51 +00:00
|
|
|
= (unsigned char)(packlen & 0xff);
|
2016-08-03 16:06:39 +00:00
|
|
|
packlen >>= 8;
|
|
|
|
}
|
|
|
|
if (packlen > 0) {
|
|
|
|
/*
|
|
|
|
* We've extended beyond the max allowed for the number of len bytes
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->subs = sub->parent;
|
|
|
|
OPENSSL_free(sub);
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
int WPACKET_close(WPACKET *pkt)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-09 08:49:16 +00:00
|
|
|
/*
|
|
|
|
* Internal API, so should not fail - but we do negative testing of this
|
|
|
|
* so no assert (otherwise the tests fail)
|
|
|
|
*/
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->subs == NULL || pkt->subs->parent == NULL)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
return wpacket_intern_close(pkt);
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
int WPACKET_finish(WPACKET *pkt)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-06 14:09:51 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-09-09 08:49:16 +00:00
|
|
|
/*
|
|
|
|
* Internal API, so should not fail - but we do negative testing of this
|
|
|
|
* so no assert (otherwise the tests fail)
|
|
|
|
*/
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->subs == NULL || pkt->subs->parent != NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = wpacket_intern_close(pkt);
|
2016-09-08 08:58:29 +00:00
|
|
|
if (ret) {
|
|
|
|
OPENSSL_free(pkt->subs);
|
|
|
|
pkt->subs = NULL;
|
|
|
|
}
|
2016-09-08 23:13:41 +00:00
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
return ret;
|
2016-08-03 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-06 14:09:51 +00:00
|
|
|
WPACKET_SUB *sub;
|
2016-09-08 23:13:41 +00:00
|
|
|
unsigned char *lenchars;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL);
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->subs == NULL)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
sub = OPENSSL_zalloc(sizeof(*sub));
|
|
|
|
if (sub == NULL)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
sub->parent = pkt->subs;
|
|
|
|
pkt->subs = sub;
|
|
|
|
sub->pwritten = pkt->written + lenbytes;
|
|
|
|
sub->lenbytes = lenbytes;
|
|
|
|
|
|
|
|
if (lenbytes == 0) {
|
2016-09-08 23:13:41 +00:00
|
|
|
sub->packet_len = 0;
|
2016-09-06 14:09:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:13:41 +00:00
|
|
|
if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
|
2016-09-06 14:09:51 +00:00
|
|
|
return 0;
|
2016-09-08 23:13:41 +00:00
|
|
|
sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
int WPACKET_start_sub_packet(WPACKET *pkt)
|
|
|
|
{
|
|
|
|
return WPACKET_start_sub_packet_len(pkt, 0);
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:13:41 +00:00
|
|
|
int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
|
|
|
unsigned char *data;
|
|
|
|
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(size <= sizeof(unsigned int));
|
2016-09-08 23:13:41 +00:00
|
|
|
if (size > sizeof(unsigned int)
|
|
|
|
|| !WPACKET_allocate_bytes(pkt, size, &data))
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-08 23:13:41 +00:00
|
|
|
for (data += size - 1; size > 0; size--) {
|
2016-08-03 16:06:39 +00:00
|
|
|
*data = (unsigned char)(val & 0xff);
|
|
|
|
data--;
|
|
|
|
val >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether we could fit the value in the assigned number of bytes */
|
|
|
|
if (val > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-08 08:58:29 +00:00
|
|
|
WPACKET_SUB *sub;
|
|
|
|
size_t lenbytes;
|
|
|
|
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL);
|
2016-09-08 08:58:29 +00:00
|
|
|
if (pkt->subs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Find the WPACKET_SUB for the top level */
|
2016-09-08 23:13:41 +00:00
|
|
|
for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
|
|
|
|
continue;
|
2016-09-08 08:58:29 +00:00
|
|
|
|
|
|
|
lenbytes = sub->lenbytes;
|
|
|
|
if (lenbytes == 0)
|
|
|
|
lenbytes = sizeof(pkt->maxsize);
|
|
|
|
|
|
|
|
if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
pkt->maxsize = maxsize;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
|
|
|
unsigned char *dest;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 1;
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
if (!WPACKET_allocate_bytes(pkt, len, &dest))
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy(dest, src, len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:19:32 +00:00
|
|
|
int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
|
|
|
|
{
|
|
|
|
if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
|
|
|
|
|| !WPACKET_memcpy(pkt, src, len)
|
|
|
|
|| !WPACKET_close(pkt))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(written != NULL);
|
2016-09-08 08:58:29 +00:00
|
|
|
if (written == NULL)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
*written = pkt->written;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:34:04 +00:00
|
|
|
int WPACKET_get_length(WPACKET *pkt, size_t *len)
|
2016-08-03 16:06:39 +00:00
|
|
|
{
|
2016-09-09 08:49:16 +00:00
|
|
|
/* Internal API, so should not fail */
|
|
|
|
assert(pkt->subs != NULL && len != NULL);
|
2016-09-06 14:09:51 +00:00
|
|
|
if (pkt->subs == NULL || len == NULL)
|
2016-08-03 16:06:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-06 14:09:51 +00:00
|
|
|
*len = pkt->written - pkt->subs->pwritten;
|
2016-08-03 16:06:39 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-08 08:58:29 +00: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;
|
|
|
|
}
|