2016-05-17 18:52:22 +00:00
|
|
|
/*
|
2019-02-26 14:05:09 +00:00
|
|
|
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* 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
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2016-03-22 09:21:29 +00:00
|
|
|
#include "bio_lcl.h"
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-05-15 22:54:43 +00:00
|
|
|
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);
|
1998-12-21 10:52:47 +00:00
|
|
|
static int mem_new(BIO *h);
|
2015-04-24 20:39:40 +00:00
|
|
|
static int secmem_new(BIO *h);
|
1998-12-21 10:52:47 +00:00
|
|
|
static int mem_free(BIO *data);
|
2019-01-21 07:47:02 +00:00
|
|
|
static int mem_buf_free(BIO *data);
|
2016-03-13 12:20:52 +00:00
|
|
|
static int mem_buf_sync(BIO *h);
|
2016-03-30 16:39:42 +00:00
|
|
|
|
2016-03-19 16:32:14 +00:00
|
|
|
static const BIO_METHOD mem_method = {
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_TYPE_MEM,
|
|
|
|
"memory buffer",
|
2016-10-20 14:18:39 +00:00
|
|
|
/* TODO: Convert to new style write function */
|
|
|
|
bwrite_conv,
|
2015-01-22 03:40:55 +00:00
|
|
|
mem_write,
|
2016-09-05 16:26:58 +00:00
|
|
|
/* TODO: Convert to new style read function */
|
|
|
|
bread_conv,
|
2015-01-22 03:40:55 +00:00
|
|
|
mem_read,
|
|
|
|
mem_puts,
|
|
|
|
mem_gets,
|
|
|
|
mem_ctrl,
|
|
|
|
mem_new,
|
|
|
|
mem_free,
|
2017-12-17 21:04:48 +00:00
|
|
|
NULL, /* mem_callback_ctrl */
|
2015-01-22 03:40:55 +00:00
|
|
|
};
|
2016-03-30 16:39:42 +00:00
|
|
|
|
2016-03-19 16:32:14 +00:00
|
|
|
static const BIO_METHOD secmem_method = {
|
2015-04-24 20:39:40 +00:00
|
|
|
BIO_TYPE_MEM,
|
|
|
|
"secure memory buffer",
|
2016-10-20 14:18:39 +00:00
|
|
|
/* TODO: Convert to new style write function */
|
|
|
|
bwrite_conv,
|
2015-04-24 20:39:40 +00:00
|
|
|
mem_write,
|
2016-09-05 16:26:58 +00:00
|
|
|
/* TODO: Convert to new style read function */
|
|
|
|
bread_conv,
|
2015-04-24 20:39:40 +00:00
|
|
|
mem_read,
|
|
|
|
mem_puts,
|
|
|
|
mem_gets,
|
|
|
|
mem_ctrl,
|
|
|
|
secmem_new,
|
|
|
|
mem_free,
|
2017-12-17 21:04:48 +00:00
|
|
|
NULL, /* mem_callback_ctrl */
|
2015-04-24 20:39:40 +00:00
|
|
|
};
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-03-30 16:39:42 +00:00
|
|
|
/* BIO memory stores buffer and read pointer */
|
|
|
|
typedef struct bio_buf_mem_st {
|
|
|
|
struct buf_mem_st *buf; /* allocated buffer */
|
|
|
|
struct buf_mem_st *readp; /* read pointer */
|
|
|
|
} BIO_BUF_MEM;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* bio->num is used to hold the value to return on 'empty', if it is 0,
|
|
|
|
* should_retry is not set
|
|
|
|
*/
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2016-03-19 16:32:14 +00:00
|
|
|
const BIO_METHOD *BIO_s_mem(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return &mem_method;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-03-19 16:32:14 +00:00
|
|
|
const BIO_METHOD *BIO_s_secmem(void)
|
2015-04-24 20:39:40 +00:00
|
|
|
{
|
|
|
|
return(&secmem_method);
|
|
|
|
}
|
|
|
|
|
2016-01-31 01:38:08 +00:00
|
|
|
BIO *BIO_new_mem_buf(const void *buf, int len)
|
1999-08-19 13:07:43 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO *ret;
|
|
|
|
BUF_MEM *b;
|
2016-03-13 12:20:52 +00:00
|
|
|
BIO_BUF_MEM *bb;
|
2015-01-22 03:40:55 +00:00
|
|
|
size_t sz;
|
2007-10-09 16:37:24 +00:00
|
|
|
|
2015-05-06 17:43:59 +00:00
|
|
|
if (buf == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sz = (len < 0) ? strlen(buf) : (size_t)len;
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((ret = BIO_new(BIO_s_mem())) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
2016-03-13 12:20:52 +00:00
|
|
|
bb = (BIO_BUF_MEM *)ret->ptr;
|
|
|
|
b = bb->buf;
|
2016-01-31 01:38:08 +00:00
|
|
|
/* Cast away const and trust in the MEM_RDONLY flag. */
|
|
|
|
b->data = (void *)buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
b->length = sz;
|
|
|
|
b->max = sz;
|
2016-03-13 12:20:52 +00:00
|
|
|
*bb->readp = *bb->buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
ret->flags |= BIO_FLAGS_MEM_RDONLY;
|
2016-08-05 17:56:58 +00:00
|
|
|
/* Since this is static data retrying won't help */
|
2015-01-22 03:40:55 +00:00
|
|
|
ret->num = 0;
|
|
|
|
return ret;
|
1999-08-19 13:07:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 20:39:40 +00:00
|
|
|
static int mem_init(BIO *bi, unsigned long flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-03-16 21:05:59 +00:00
|
|
|
BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-03-13 12:20:52 +00:00
|
|
|
if (bb == NULL)
|
2016-04-27 11:52:29 +00:00
|
|
|
return 0;
|
|
|
|
if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
|
|
|
|
OPENSSL_free(bb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
|
|
|
|
BUF_MEM_free(bb->buf);
|
|
|
|
OPENSSL_free(bb);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-13 12:20:52 +00:00
|
|
|
*bb->readp = *bb->buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
bi->shutdown = 1;
|
|
|
|
bi->init = 1;
|
|
|
|
bi->num = -1;
|
2016-03-13 12:20:52 +00:00
|
|
|
bi->ptr = (char *)bb;
|
2016-04-27 11:52:29 +00:00
|
|
|
return 1;
|
2015-04-24 20:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mem_new(BIO *bi)
|
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return mem_init(bi, 0L);
|
2015-04-24 20:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int secmem_new(BIO *bi)
|
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return mem_init(bi, BUF_MEM_FLAG_SECURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static int mem_free(BIO *a)
|
2016-03-13 12:20:52 +00:00
|
|
|
{
|
2019-01-21 07:47:02 +00:00
|
|
|
BIO_BUF_MEM *bb;
|
|
|
|
|
|
|
|
if (a == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bb = (BIO_BUF_MEM *)a->ptr;
|
|
|
|
if (!mem_buf_free(a))
|
|
|
|
return 0;
|
|
|
|
OPENSSL_free(bb->readp);
|
|
|
|
OPENSSL_free(bb);
|
|
|
|
return 1;
|
2016-03-13 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 07:47:02 +00:00
|
|
|
static int mem_buf_free(BIO *a)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (a == NULL)
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
2017-12-08 15:49:41 +00:00
|
|
|
|
|
|
|
if (a->shutdown && a->init && a->ptr != NULL) {
|
|
|
|
BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
|
|
|
|
BUF_MEM *b = bb->buf;
|
|
|
|
|
|
|
|
if (a->flags & BIO_FLAGS_MEM_RDONLY)
|
|
|
|
b->data = NULL;
|
|
|
|
BUF_MEM_free(b);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-10-09 11:05:58 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 12:20:52 +00:00
|
|
|
/*
|
|
|
|
* Reallocate memory buffer if read pointer differs
|
|
|
|
*/
|
|
|
|
static int mem_buf_sync(BIO *b)
|
|
|
|
{
|
2016-06-28 22:18:50 +00:00
|
|
|
if (b != NULL && b->init != 0 && b->ptr != NULL) {
|
2016-03-13 12:20:52 +00:00
|
|
|
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
|
|
|
|
|
2016-06-28 22:18:50 +00:00
|
|
|
if (bbm->readp->data != bbm->buf->data) {
|
2016-03-13 12:20:52 +00:00
|
|
|
memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
|
|
|
|
bbm->buf->length = bbm->readp->length;
|
|
|
|
bbm->readp->data = bbm->buf->data;
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
2016-03-13 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static int mem_read(BIO *b, char *out, int outl)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2016-03-13 12:20:52 +00:00
|
|
|
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
|
|
|
|
BUF_MEM *bm = bbm->readp;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
2016-03-13 12:20:52 +00:00
|
|
|
bm->data += ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (bm->length == 0) {
|
|
|
|
ret = b->num;
|
|
|
|
if (ret != 0)
|
|
|
|
BIO_set_retry_read(b);
|
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-05-15 22:54:43 +00:00
|
|
|
static int mem_write(BIO *b, const char *in, int inl)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int blen;
|
2016-03-13 12:20:52 +00:00
|
|
|
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (in == NULL) {
|
|
|
|
BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (b->flags & BIO_FLAGS_MEM_RDONLY) {
|
|
|
|
BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
BIO_clear_retry_flags(b);
|
2018-05-04 12:44:19 +00:00
|
|
|
if (inl == 0)
|
|
|
|
return 0;
|
2016-03-13 12:20:52 +00:00
|
|
|
blen = bbm->readp->length;
|
|
|
|
mem_buf_sync(b);
|
|
|
|
if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
2016-03-13 12:20:52 +00:00
|
|
|
memcpy(bbm->buf->data + blen, in, inl);
|
|
|
|
*bbm->readp = *bbm->buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = inl;
|
|
|
|
end:
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-05-15 22:54:43 +00:00
|
|
|
static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
long ret = 1;
|
|
|
|
char **pptr;
|
2016-03-13 12:20:52 +00:00
|
|
|
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
|
|
|
|
BUF_MEM *bm;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case BIO_CTRL_RESET:
|
2016-03-13 12:20:52 +00:00
|
|
|
bm = bbm->buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
if (bm->data != NULL) {
|
|
|
|
/* For read only case reset to the start again */
|
2016-03-13 12:20:52 +00:00
|
|
|
if ((b->flags & BIO_FLAGS_MEM_RDONLY) || (b->flags & BIO_FLAGS_NONCLEAR_RST)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
bm->length = bm->max;
|
|
|
|
} else {
|
|
|
|
memset(bm->data, 0, bm->max);
|
|
|
|
bm->length = 0;
|
|
|
|
}
|
2016-03-13 12:20:52 +00:00
|
|
|
*bbm->readp = *bbm->buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_EOF:
|
2016-03-13 12:20:52 +00:00
|
|
|
bm = bbm->readp;
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = (long)(bm->length == 0);
|
|
|
|
break;
|
|
|
|
case BIO_C_SET_BUF_MEM_EOF_RETURN:
|
|
|
|
b->num = (int)num;
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_INFO:
|
2016-03-13 12:20:52 +00:00
|
|
|
bm = bbm->readp;
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = (long)bm->length;
|
|
|
|
if (ptr != NULL) {
|
|
|
|
pptr = (char **)ptr;
|
|
|
|
*pptr = (char *)&(bm->data[0]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BIO_C_SET_BUF_MEM:
|
2019-01-21 07:47:02 +00:00
|
|
|
mem_buf_free(b);
|
2015-01-22 03:40:55 +00:00
|
|
|
b->shutdown = (int)num;
|
2016-03-13 12:20:52 +00:00
|
|
|
bbm->buf = ptr;
|
|
|
|
*bbm->readp = *bbm->buf;
|
2015-01-22 03:40:55 +00:00
|
|
|
break;
|
|
|
|
case BIO_C_GET_BUF_MEM_PTR:
|
|
|
|
if (ptr != NULL) {
|
2016-03-13 12:20:52 +00:00
|
|
|
mem_buf_sync(b);
|
|
|
|
bm = bbm->readp;
|
2015-01-22 03:40:55 +00:00
|
|
|
pptr = (char **)ptr;
|
|
|
|
*pptr = (char *)bm;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_GET_CLOSE:
|
|
|
|
ret = (long)b->shutdown;
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_SET_CLOSE:
|
|
|
|
b->shutdown = (int)num;
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_WPENDING:
|
|
|
|
ret = 0L;
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_PENDING:
|
2016-03-13 12:20:52 +00:00
|
|
|
bm = bbm->readp;
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = (long)bm->length;
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_DUP:
|
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_PUSH:
|
|
|
|
case BIO_CTRL_POP:
|
|
|
|
default:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static int mem_gets(BIO *bp, char *buf, int size)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int ret = -1;
|
|
|
|
char *p;
|
2016-03-13 12:20:52 +00:00
|
|
|
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
|
|
|
|
BUF_MEM *bm = bbm->readp;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_clear_retry_flags(bp);
|
|
|
|
j = bm->length;
|
|
|
|
if ((size - 1) < j)
|
|
|
|
j = size - 1;
|
|
|
|
if (j <= 0) {
|
|
|
|
*buf = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p = bm->data;
|
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
if (p[i] == '\n') {
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-07 17:00:23 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* i is now the max num of bytes to copy, either j or up to
|
|
|
|
* and including the first newline
|
|
|
|
*/
|
2009-03-07 17:00:23 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
i = mem_read(bp, buf, i);
|
|
|
|
if (i > 0)
|
|
|
|
buf[i] = '\0';
|
|
|
|
ret = i;
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-05-15 22:54:43 +00:00
|
|
|
static int mem_puts(BIO *bp, const char *str)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int n, ret;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
n = strlen(str);
|
|
|
|
ret = mem_write(bp, str, n);
|
|
|
|
/* memory semantics is that it will always work */
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|