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