early-access version 2698
This commit is contained in:
362
externals/libressl/crypto/bio/bss_mem.c
vendored
362
externals/libressl/crypto/bio/bss_mem.c
vendored
@@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: bss_mem.c,v 1.17 2018/05/12 18:51:59 tb Exp $ */
|
||||
/* $OpenBSD: bss_mem.c,v 1.21 2022/02/19 15:59:12 jsing Exp $ */
|
||||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
@@ -57,6 +57,7 @@
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -64,13 +65,35 @@
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/buffer.h>
|
||||
|
||||
static int mem_write(BIO *h, const char *buf, int num);
|
||||
static int mem_read(BIO *h, char *buf, int size);
|
||||
static int mem_puts(BIO *h, const char *str);
|
||||
static int mem_gets(BIO *h, char *str, int size);
|
||||
static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
||||
static int mem_new(BIO *h);
|
||||
static int mem_free(BIO *data);
|
||||
#include "bio_local.h"
|
||||
|
||||
struct bio_mem {
|
||||
BUF_MEM *buf;
|
||||
size_t read_offset;
|
||||
};
|
||||
|
||||
static size_t
|
||||
bio_mem_pending(struct bio_mem *bm)
|
||||
{
|
||||
if (bm->read_offset > bm->buf->length)
|
||||
return 0;
|
||||
|
||||
return bm->buf->length - bm->read_offset;
|
||||
}
|
||||
|
||||
static uint8_t *
|
||||
bio_mem_read_ptr(struct bio_mem *bm)
|
||||
{
|
||||
return &bm->buf->data[bm->read_offset];
|
||||
}
|
||||
|
||||
static int mem_new(BIO *bio);
|
||||
static int mem_free(BIO *bio);
|
||||
static int mem_write(BIO *bio, const char *in, int in_len);
|
||||
static int mem_read(BIO *bio, char *out, int out_len);
|
||||
static int mem_puts(BIO *bio, const char *in);
|
||||
static int mem_gets(BIO *bio, char *out, int out_len);
|
||||
static long mem_ctrl(BIO *bio, int cmd, long arg1, void *arg2);
|
||||
|
||||
static const BIO_METHOD mem_method = {
|
||||
.type = BIO_TYPE_MEM,
|
||||
@@ -84,181 +107,207 @@ static const BIO_METHOD mem_method = {
|
||||
.destroy = mem_free
|
||||
};
|
||||
|
||||
/* bio->num is used to hold the value to return on 'empty', if it is
|
||||
* 0, should_retry is not set */
|
||||
/*
|
||||
* bio->num is used to hold the value to return on 'empty', if it is
|
||||
* 0, should_retry is not set.
|
||||
*/
|
||||
|
||||
const BIO_METHOD *
|
||||
BIO_s_mem(void)
|
||||
{
|
||||
return (&mem_method);
|
||||
return &mem_method;
|
||||
}
|
||||
|
||||
BIO *
|
||||
BIO_new_mem_buf(const void *buf, int len)
|
||||
BIO_new_mem_buf(const void *buf, int buf_len)
|
||||
{
|
||||
BIO *ret;
|
||||
BUF_MEM *b;
|
||||
size_t sz;
|
||||
struct bio_mem *bm;
|
||||
BIO *bio;
|
||||
|
||||
if (!buf) {
|
||||
if (buf == NULL) {
|
||||
BIOerror(BIO_R_NULL_PARAMETER);
|
||||
return NULL;
|
||||
}
|
||||
sz = (len < 0) ? strlen(buf) : (size_t)len;
|
||||
if (!(ret = BIO_new(BIO_s_mem())))
|
||||
if (buf_len == -1)
|
||||
buf_len = strlen(buf);
|
||||
if (buf_len < 0) {
|
||||
BIOerror(BIO_R_INVALID_ARGUMENT);
|
||||
return NULL;
|
||||
b = (BUF_MEM *)ret->ptr;
|
||||
b->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */
|
||||
b->length = sz;
|
||||
b->max = sz;
|
||||
ret->flags |= BIO_FLAGS_MEM_RDONLY;
|
||||
/* Since this is static data retrying wont help */
|
||||
ret->num = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_new(BIO *bi)
|
||||
{
|
||||
BUF_MEM *b;
|
||||
|
||||
if ((b = BUF_MEM_new()) == NULL)
|
||||
return (0);
|
||||
bi->shutdown = 1;
|
||||
bi->init = 1;
|
||||
bi->num = -1;
|
||||
bi->ptr = (char *)b;
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int
|
||||
mem_free(BIO *a)
|
||||
{
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
if (a->shutdown) {
|
||||
if ((a->init) && (a->ptr != NULL)) {
|
||||
BUF_MEM *b;
|
||||
b = (BUF_MEM *)a->ptr;
|
||||
if (a->flags & BIO_FLAGS_MEM_RDONLY)
|
||||
b->data = NULL;
|
||||
BUF_MEM_free(b);
|
||||
a->ptr = NULL;
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int
|
||||
mem_read(BIO *b, char *out, int outl)
|
||||
{
|
||||
int ret = -1;
|
||||
BUF_MEM *bm;
|
||||
|
||||
bm = (BUF_MEM *)b->ptr;
|
||||
BIO_clear_retry_flags(b);
|
||||
ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
|
||||
if ((out != NULL) && (ret > 0)) {
|
||||
memcpy(out, bm->data, ret);
|
||||
bm->length -= ret;
|
||||
if (b->flags & BIO_FLAGS_MEM_RDONLY)
|
||||
bm->data += ret;
|
||||
else {
|
||||
memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
|
||||
}
|
||||
} else if (bm->length == 0) {
|
||||
ret = b->num;
|
||||
if (ret != 0)
|
||||
BIO_set_retry_read(b);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
static int
|
||||
mem_write(BIO *b, const char *in, int inl)
|
||||
{
|
||||
int ret = -1;
|
||||
int blen;
|
||||
BUF_MEM *bm;
|
||||
|
||||
bm = (BUF_MEM *)b->ptr;
|
||||
if (in == NULL) {
|
||||
BIOerror(BIO_R_NULL_PARAMETER);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (b->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
if ((bio = BIO_new(BIO_s_mem())) == NULL)
|
||||
return NULL;
|
||||
|
||||
bm = bio->ptr;
|
||||
bm->buf->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */
|
||||
bm->buf->length = buf_len;
|
||||
bm->buf->max = buf_len;
|
||||
bio->flags |= BIO_FLAGS_MEM_RDONLY;
|
||||
/* Since this is static data retrying will not help. */
|
||||
bio->num = 0;
|
||||
|
||||
return bio;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_new(BIO *bio)
|
||||
{
|
||||
struct bio_mem *bm;
|
||||
|
||||
if ((bm = calloc(1, sizeof(*bm))) == NULL)
|
||||
return 0;
|
||||
if ((bm->buf = BUF_MEM_new()) == NULL) {
|
||||
free(bm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bio->shutdown = 1;
|
||||
bio->init = 1;
|
||||
bio->num = -1;
|
||||
bio->ptr = bm;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_free(BIO *bio)
|
||||
{
|
||||
struct bio_mem *bm;
|
||||
|
||||
if (bio == NULL)
|
||||
return 0;
|
||||
if (!bio->init || bio->ptr == NULL)
|
||||
return 1;
|
||||
|
||||
bm = bio->ptr;
|
||||
if (bio->shutdown) {
|
||||
if (bio->flags & BIO_FLAGS_MEM_RDONLY)
|
||||
bm->buf->data = NULL;
|
||||
BUF_MEM_free(bm->buf);
|
||||
}
|
||||
free(bm);
|
||||
bio->ptr = NULL;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_read(BIO *bio, char *out, int out_len)
|
||||
{
|
||||
struct bio_mem *bm = bio->ptr;
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
|
||||
if (out == NULL || out_len <= 0)
|
||||
return 0;
|
||||
|
||||
if ((size_t)out_len > bio_mem_pending(bm))
|
||||
out_len = bio_mem_pending(bm);
|
||||
|
||||
if (out_len == 0) {
|
||||
if (bio->num != 0)
|
||||
BIO_set_retry_read(bio);
|
||||
return bio->num;
|
||||
}
|
||||
|
||||
memcpy(out, bio_mem_read_ptr(bm), out_len);
|
||||
bm->read_offset += out_len;
|
||||
|
||||
return out_len;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_write(BIO *bio, const char *in, int in_len)
|
||||
{
|
||||
struct bio_mem *bm = bio->ptr;
|
||||
size_t buf_len;
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
|
||||
if (in == NULL || in_len <= 0)
|
||||
return 0;
|
||||
|
||||
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO);
|
||||
goto end;
|
||||
return -1;
|
||||
}
|
||||
|
||||
BIO_clear_retry_flags(b);
|
||||
blen = bm->length;
|
||||
if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
|
||||
goto end;
|
||||
memcpy(&(bm->data[blen]), in, inl);
|
||||
ret = inl;
|
||||
end:
|
||||
return (ret);
|
||||
if (bm->read_offset > 4096) {
|
||||
memmove(bm->buf->data, bio_mem_read_ptr(bm),
|
||||
bio_mem_pending(bm));
|
||||
bm->buf->length = bio_mem_pending(bm);
|
||||
bm->read_offset = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for overflow and ensure we do not exceed an int, otherwise we
|
||||
* cannot tell if BUF_MEM_grow_clean() succeeded.
|
||||
*/
|
||||
buf_len = bm->buf->length + in_len;
|
||||
if (buf_len < bm->buf->length || buf_len > INT_MAX)
|
||||
return -1;
|
||||
|
||||
if (BUF_MEM_grow_clean(bm->buf, buf_len) != buf_len)
|
||||
return -1;
|
||||
|
||||
memcpy(&bm->buf->data[buf_len - in_len], in, in_len);
|
||||
|
||||
return in_len;
|
||||
}
|
||||
|
||||
static long
|
||||
mem_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
|
||||
{
|
||||
struct bio_mem *bm = bio->ptr;
|
||||
void **pptr;
|
||||
long ret = 1;
|
||||
char **pptr;
|
||||
|
||||
BUF_MEM *bm = (BUF_MEM *)b->ptr;
|
||||
|
||||
switch (cmd) {
|
||||
case BIO_CTRL_RESET:
|
||||
if (bm->data != NULL) {
|
||||
/* For read only case reset to the start again */
|
||||
if (b->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
bm->data -= bm->max - bm->length;
|
||||
bm->length = bm->max;
|
||||
} else {
|
||||
memset(bm->data, 0, bm->max);
|
||||
bm->length = 0;
|
||||
if (bm->buf->data != NULL) {
|
||||
if (!(bio->flags & BIO_FLAGS_MEM_RDONLY)) {
|
||||
memset(bm->buf->data, 0, bm->buf->max);
|
||||
bm->buf->length = 0;
|
||||
}
|
||||
bm->read_offset = 0;
|
||||
}
|
||||
break;
|
||||
case BIO_CTRL_EOF:
|
||||
ret = (long)(bm->length == 0);
|
||||
ret = (long)(bio_mem_pending(bm) == 0);
|
||||
break;
|
||||
case BIO_C_SET_BUF_MEM_EOF_RETURN:
|
||||
b->num = (int)num;
|
||||
bio->num = (int)num;
|
||||
break;
|
||||
case BIO_CTRL_INFO:
|
||||
ret = (long)bm->length;
|
||||
if (ptr != NULL) {
|
||||
pptr = (char **)ptr;
|
||||
*pptr = (char *)&(bm->data[0]);
|
||||
pptr = (void **)ptr;
|
||||
*pptr = bio_mem_read_ptr(bm);
|
||||
}
|
||||
ret = (long)bio_mem_pending(bm);
|
||||
break;
|
||||
case BIO_C_SET_BUF_MEM:
|
||||
mem_free(b);
|
||||
b->shutdown = (int)num;
|
||||
b->ptr = ptr;
|
||||
BUF_MEM_free(bm->buf);
|
||||
bio->shutdown = (int)num;
|
||||
bm->buf = ptr;
|
||||
bm->read_offset = 0;
|
||||
break;
|
||||
case BIO_C_GET_BUF_MEM_PTR:
|
||||
if (ptr != NULL) {
|
||||
pptr = (char **)ptr;
|
||||
*pptr = (char *)bm;
|
||||
pptr = (void **)ptr;
|
||||
*pptr = bm->buf;
|
||||
}
|
||||
break;
|
||||
case BIO_CTRL_GET_CLOSE:
|
||||
ret = (long)b->shutdown;
|
||||
ret = (long)bio->shutdown;
|
||||
break;
|
||||
case BIO_CTRL_SET_CLOSE:
|
||||
b->shutdown = (int)num;
|
||||
bio->shutdown = (int)num;
|
||||
break;
|
||||
|
||||
case BIO_CTRL_WPENDING:
|
||||
ret = 0L;
|
||||
break;
|
||||
case BIO_CTRL_PENDING:
|
||||
ret = (long)bm->length;
|
||||
ret = (long)bio_mem_pending(bm);
|
||||
break;
|
||||
case BIO_CTRL_DUP:
|
||||
case BIO_CTRL_FLUSH:
|
||||
@@ -270,27 +319,29 @@ mem_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_gets(BIO *bp, char *buf, int size)
|
||||
mem_gets(BIO *bio, char *out, int out_len)
|
||||
{
|
||||
int i, j;
|
||||
int ret = -1;
|
||||
struct bio_mem *bm = bio->ptr;
|
||||
int i, out_max;
|
||||
char *p;
|
||||
BUF_MEM *bm = (BUF_MEM *)bp->ptr;
|
||||
int ret = -1;
|
||||
|
||||
BIO_clear_retry_flags(bp);
|
||||
j = bm->length;
|
||||
if ((size - 1) < j)
|
||||
j = size - 1;
|
||||
if (j <= 0) {
|
||||
*buf = '\0';
|
||||
BIO_clear_retry_flags(bio);
|
||||
|
||||
out_max = bio_mem_pending(bm);
|
||||
if (out_len - 1 < out_max)
|
||||
out_max = out_len - 1;
|
||||
if (out_max <= 0) {
|
||||
*out = '\0';
|
||||
return 0;
|
||||
}
|
||||
p = bm->data;
|
||||
for (i = 0; i < j; i++) {
|
||||
|
||||
p = bio_mem_read_ptr(bm);
|
||||
for (i = 0; i < out_max; i++) {
|
||||
if (p[i] == '\n') {
|
||||
i++;
|
||||
break;
|
||||
@@ -298,24 +349,17 @@ mem_gets(BIO *bp, char *buf, int size)
|
||||
}
|
||||
|
||||
/*
|
||||
* i is now the max num of bytes to copy, either j or up to
|
||||
* and including the first newline
|
||||
* i is now the max num of bytes to copy, either out_max or up to and
|
||||
* including the first newline
|
||||
*/
|
||||
if ((ret = mem_read(bio, out, i)) > 0)
|
||||
out[ret] = '\0';
|
||||
|
||||
i = mem_read(bp, buf, i);
|
||||
if (i > 0)
|
||||
buf[i] = '\0';
|
||||
ret = i;
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
mem_puts(BIO *bp, const char *str)
|
||||
mem_puts(BIO *bio, const char *in)
|
||||
{
|
||||
int n, ret;
|
||||
|
||||
n = strlen(str);
|
||||
ret = mem_write(bp, str, n);
|
||||
/* memory semantics is that it will always work */
|
||||
return (ret);
|
||||
return mem_write(bio, in, strlen(in));
|
||||
}
|
||||
|
Reference in New Issue
Block a user