| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/bio.h> |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/buf.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
#include "../internal.h" |
| 14 |
|
| 15 |
typedef struct bio_buf_mem_st { |
| 16 |
struct buf_mem_st *buf; /* allocated buffer */ |
| 17 |
size_t read_off; /* read pointer offset from current buffer position */ |
| 18 |
} BIO_BUF_MEM; |
| 19 |
|
| 20 |
|
| 21 |
BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) { |
| 22 |
BIO *ret; |
| 23 |
BUF_MEM *b; |
| 24 |
BIO_BUF_MEM *bbm; |
| 25 |
|
| 26 |
const size_t size = (len < 0) ? strlen((char *)buf) : (size_t)len; |
| 27 |
|
| 28 |
if (!buf && len != 0) { |
| 29 |
OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER); |
| 30 |
return NULL; |
| 31 |
} |
| 32 |
|
| 33 |
ret = BIO_new(BIO_s_mem()); |
| 34 |
if (ret == NULL) { |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
bbm = (BIO_BUF_MEM *)ret->ptr; |
| 39 |
b = bbm->buf; |
| 40 |
// BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to. |
| 41 |
b->data = (void *)buf; |
| 42 |
b->length = size; |
| 43 |
b->max = size; |
| 44 |
|
| 45 |
ret->flags |= BIO_FLAGS_MEM_RDONLY; |
| 46 |
|
| 47 |
// |num| is used to store the value that this BIO will return when it runs |
| 48 |
// out of data. If it's negative then the retry flags will also be set. Since |
| 49 |
// this is static data, retrying wont help |
| 50 |
ret->num = 0; |
| 51 |
|
| 52 |
return ret; |
| 53 |
} |
| 54 |
|
| 55 |
static int mem_new(BIO *bio) { |
| 56 |
BIO_BUF_MEM *bbm = OPENSSL_zalloc(sizeof(*bbm)); |
| 57 |
|
| 58 |
if (bbm == NULL) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
bbm->buf = BUF_MEM_new(); |
| 63 |
if (bbm->buf == NULL) { |
| 64 |
OPENSSL_free(bbm); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
// |shutdown| is used to store the close flag: whether the BIO has ownership |
| 69 |
// of the BUF_MEM. |
| 70 |
bbm->read_off = 0; |
| 71 |
bio->shutdown = 1; |
| 72 |
bio->init = 1; |
| 73 |
bio->num = -1; |
| 74 |
bio->ptr = (char *)bbm; |
| 75 |
|
| 76 |
return 1; |
| 77 |
} |
| 78 |
|
| 79 |
static int mem_buf_free(BIO *bio) { |
| 80 |
if (bio == NULL) { |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
if (!bio->shutdown || !bio->init || bio->ptr == NULL) { |
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bio->ptr; |
| 89 |
BUF_MEM *b = bbm->buf; |
| 90 |
|
| 91 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 92 |
b->data = NULL; |
| 93 |
} |
| 94 |
BUF_MEM_free(b); |
| 95 |
|
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
static int mem_free(BIO *bio) { |
| 100 |
if (bio == NULL) { |
| 101 |
return 0; |
| 102 |
} |
| 103 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bio->ptr; |
| 104 |
if (!mem_buf_free(bio)) { |
| 105 |
return 0; |
| 106 |
} |
| 107 |
bio->ptr = NULL; |
| 108 |
OPENSSL_free(bbm); |
| 109 |
return 1; |
| 110 |
} |
| 111 |
|
| 112 |
static void mem_buf_sync(BIO *bio) { |
| 113 |
if (bio->init != 0 && bio->ptr != NULL) { |
| 114 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr; |
| 115 |
BUF_MEM *b = bbm->buf; |
| 116 |
|
| 117 |
if (b->data != NULL) { |
| 118 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 119 |
b->data += bbm->read_off; |
| 120 |
} else { |
| 121 |
OPENSSL_memmove(b->data, &b->data[bbm->read_off], b->length); |
| 122 |
} |
| 123 |
bbm->read_off = 0; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static int mem_read(BIO *bio, char *out, int outl) { |
| 129 |
BIO_clear_retry_flags(bio); |
| 130 |
if (outl <= 0) { |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr; |
| 135 |
BUF_MEM *b = bbm->buf; |
| 136 |
|
| 137 |
int ret = outl; |
| 138 |
if ((size_t)ret > b->length) { |
| 139 |
ret = (int)b->length; |
| 140 |
} |
| 141 |
|
| 142 |
if (ret > 0) { |
| 143 |
OPENSSL_memcpy(out, &b->data[bbm->read_off], ret); |
| 144 |
b->length -= ret; |
| 145 |
bbm->read_off += ret; |
| 146 |
} else if (b->length == 0) { |
| 147 |
ret = bio->num; |
| 148 |
if (ret != 0) { |
| 149 |
BIO_set_retry_read(bio); |
| 150 |
} |
| 151 |
} |
| 152 |
return ret; |
| 153 |
} |
| 154 |
|
| 155 |
static int mem_write(BIO *bio, const char *in, int inl) { |
| 156 |
BIO_clear_retry_flags(bio); |
| 157 |
if (inl <= 0) { |
| 158 |
return 0; // Successfully write zero bytes. |
| 159 |
} |
| 160 |
|
| 161 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 162 |
OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO); |
| 163 |
return -1; |
| 164 |
} |
| 165 |
|
| 166 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr; |
| 167 |
BUF_MEM *b = bbm->buf; |
| 168 |
|
| 169 |
mem_buf_sync(bio); |
| 170 |
|
| 171 |
if (!BUF_MEM_append(b, in, inl)) { |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
return inl; |
| 176 |
} |
| 177 |
|
| 178 |
static int mem_gets(BIO *bio, char *buf, int size) { |
| 179 |
BIO_clear_retry_flags(bio); |
| 180 |
if (size <= 0) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
// The buffer size includes space for the trailing NUL, so we can read at most |
| 185 |
// one fewer byte. |
| 186 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr; |
| 187 |
BUF_MEM *b = bbm->buf; |
| 188 |
int ret = size - 1; |
| 189 |
if ((size_t)ret > b->length) { |
| 190 |
ret = (int)b->length; |
| 191 |
} |
| 192 |
|
| 193 |
// Stop at the first newline. |
| 194 |
if (b->data != NULL) { |
| 195 |
char *readp = &b->data[bbm->read_off]; |
| 196 |
|
| 197 |
const char *newline = OPENSSL_memchr(readp, '\n', ret); |
| 198 |
if (newline != NULL) { |
| 199 |
ret = (int)(newline - readp + 1); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
ret = mem_read(bio, buf, ret); |
| 204 |
if (ret >= 0) { |
| 205 |
buf[ret] = '\0'; |
| 206 |
} |
| 207 |
return ret; |
| 208 |
} |
| 209 |
|
| 210 |
static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 211 |
long ret = 1; |
| 212 |
|
| 213 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr; |
| 214 |
BUF_MEM *b = bbm->buf; |
| 215 |
|
| 216 |
switch (cmd) { |
| 217 |
case BIO_CTRL_RESET: |
| 218 |
if (b->data != NULL) { |
| 219 |
// For read only case reset to the start again |
| 220 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 221 |
b->data -= b->max - b->length - bbm->read_off; |
| 222 |
b->length = b->max; |
| 223 |
} else { |
| 224 |
OPENSSL_cleanse(b->data, b->max); |
| 225 |
b->length = 0; |
| 226 |
} |
| 227 |
bbm->read_off = 0; |
| 228 |
} |
| 229 |
break; |
| 230 |
case BIO_C_FILE_SEEK: |
| 231 |
if (num < 0 || (size_t)num > b->max) { |
| 232 |
ret = -1; |
| 233 |
break; |
| 234 |
} |
| 235 |
|
| 236 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 237 |
b->data -= b->max - b->length - bbm->read_off; |
| 238 |
b->length = b->max - num; |
| 239 |
} else { |
| 240 |
if ((size_t)num > bbm->read_off + b->length) { |
| 241 |
ret = -1; |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
b->length = (b->length + bbm->read_off) - num; |
| 246 |
} |
| 247 |
|
| 248 |
bbm->read_off = num; |
| 249 |
ret = num; |
| 250 |
break; |
| 251 |
case BIO_CTRL_EOF: |
| 252 |
ret = (long)(b->length == 0); |
| 253 |
break; |
| 254 |
case BIO_C_SET_BUF_MEM_EOF_RETURN: |
| 255 |
bio->num = (int)num; |
| 256 |
break; |
| 257 |
case BIO_CTRL_INFO: |
| 258 |
ret = (long)b->length; |
| 259 |
if (ptr != NULL) { |
| 260 |
char **pptr = ptr; |
| 261 |
*pptr = (b->data != NULL) ? &b->data[bbm->read_off] : NULL; |
| 262 |
} |
| 263 |
break; |
| 264 |
case BIO_C_SET_BUF_MEM: { |
| 265 |
// |bio->ptr| stores a |BIO_BUF_MEM| wrapper, not a raw |BUF_MEM*|. |
| 266 |
// Allocate the wrapper before releasing the old state so a failure here |
| 267 |
// leaves |bio| usable and does not take ownership of |ptr|. |
| 268 |
BIO_BUF_MEM *new_bbm = OPENSSL_zalloc(sizeof(*new_bbm)); |
| 269 |
if (new_bbm == NULL) { |
| 270 |
ret = 0; |
| 271 |
break; |
| 272 |
} |
| 273 |
new_bbm->buf = ptr; |
| 274 |
new_bbm->read_off = 0; |
| 275 |
mem_free(bio); |
| 276 |
bio->shutdown = (int)num; |
| 277 |
bio->init = 1; |
| 278 |
bio->ptr = new_bbm; |
| 279 |
bio->num = -1; |
| 280 |
bio->flags &= ~BIO_FLAGS_MEM_RDONLY; |
| 281 |
break; |
| 282 |
} |
| 283 |
case BIO_C_GET_BUF_MEM_PTR: |
| 284 |
if (ptr != NULL) { |
| 285 |
mem_buf_sync(bio); |
| 286 |
BUF_MEM **pptr = ptr; |
| 287 |
*pptr = b; |
| 288 |
} |
| 289 |
break; |
| 290 |
case BIO_CTRL_GET_CLOSE: |
| 291 |
ret = (long)bio->shutdown; |
| 292 |
break; |
| 293 |
case BIO_CTRL_SET_CLOSE: |
| 294 |
bio->shutdown = (int)num; |
| 295 |
break; |
| 296 |
|
| 297 |
case BIO_CTRL_WPENDING: |
| 298 |
ret = 0L; |
| 299 |
break; |
| 300 |
case BIO_CTRL_PENDING: |
| 301 |
ret = (long)b->length; |
| 302 |
break; |
| 303 |
case BIO_CTRL_FLUSH: |
| 304 |
ret = 1; |
| 305 |
break; |
| 306 |
default: |
| 307 |
ret = 0; |
| 308 |
break; |
| 309 |
} |
| 310 |
return ret; |
| 311 |
} |
| 312 |
|
| 313 |
static const BIO_METHOD mem_method = { |
| 314 |
BIO_TYPE_MEM, "memory buffer", |
| 315 |
mem_write, mem_read, |
| 316 |
NULL /* puts */, mem_gets, |
| 317 |
mem_ctrl, mem_new, |
| 318 |
mem_free, NULL /* callback_ctrl */, |
| 319 |
}; |
| 320 |
|
| 321 |
const BIO_METHOD *BIO_s_mem(void) { return &mem_method; } |
| 322 |
|
| 323 |
int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents, |
| 324 |
size_t *out_len) { |
| 325 |
if (!bio || bio->method != &mem_method) { |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr; |
| 330 |
const BUF_MEM *b = bbm->buf; |
| 331 |
|
| 332 |
mem_buf_sync((BIO *)bio); |
| 333 |
|
| 334 |
if (out_contents != NULL) { |
| 335 |
*out_contents = (uint8_t *)b->data; |
| 336 |
} |
| 337 |
if(out_len) { |
| 338 |
*out_len = b->length; |
| 339 |
} |
| 340 |
return 1; |
| 341 |
} |
| 342 |
|
| 343 |
int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) { |
| 344 |
return (int)BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, out); |
| 345 |
} |
| 346 |
|
| 347 |
int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) { |
| 348 |
return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, b); |
| 349 |
} |
| 350 |
|
| 351 |
int BIO_set_mem_eof_return(BIO *bio, int eof_value) { |
| 352 |
return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL); |
| 353 |
} |
| 354 |
|
| 355 |
const BIO_METHOD *BIO_s_secmem(void) { |
| 356 |
return BIO_s_mem(); |
| 357 |
} |