| 1 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* This package is an SSL implementation written |
| 5 |
* by Eric Young ([email protected]). |
| 6 |
* The implementation was written so as to conform with Netscapes SSL. |
| 7 |
* |
| 8 |
* This library is free for commercial and non-commercial use as long as |
| 9 |
* the following conditions are aheared to. The following conditions |
| 10 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 11 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 |
* included with this distribution is covered by the same copyright terms |
| 13 |
* except that the holder is Tim Hudson ([email protected]). |
| 14 |
* |
| 15 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 |
* the code are not to be removed. |
| 17 |
* If this package is used in a product, Eric Young should be given attribution |
| 18 |
* as the author of the parts of the library used. |
| 19 |
* This can be in the form of a textual message at program startup or |
| 20 |
* in documentation (online or textual) provided with the package. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* 3. All advertising materials mentioning features or use of this software |
| 31 |
* must display the following acknowledgement: |
| 32 |
* "This product includes cryptographic software written by |
| 33 |
* Eric Young ([email protected])" |
| 34 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 35 |
* being used are not cryptographic related :-). |
| 36 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 |
* the apps directory (application code) you must include an acknowledgement: |
| 38 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 39 |
* |
| 40 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 |
* SUCH DAMAGE. |
| 51 |
* |
| 52 |
* The licence and distribution terms for any publically available version or |
| 53 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 |
* copied and put under another distribution licence |
| 55 |
* [including the GNU Public Licence.] */ |
| 56 |
|
| 57 |
#include <openssl/bio.h> |
| 58 |
|
| 59 |
#include <limits.h> |
| 60 |
#include <string.h> |
| 61 |
|
| 62 |
#include <openssl/buf.h> |
| 63 |
#include <openssl/err.h> |
| 64 |
#include <openssl/mem.h> |
| 65 |
|
| 66 |
|
| 67 |
BIO *BIO_new_mem_buf(const void *buf, int len) { |
| 68 |
BIO *ret; |
| 69 |
BUF_MEM *b; |
| 70 |
const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len; |
| 71 |
|
| 72 |
if (!buf && len != 0) { |
| 73 |
OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
|
| 77 |
ret = BIO_new(BIO_s_mem()); |
| 78 |
if (ret == NULL) { |
| 79 |
return NULL; |
| 80 |
} |
| 81 |
|
| 82 |
b = (BUF_MEM *)ret->ptr; |
| 83 |
/* BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to. */ |
| 84 |
b->data = (void *)buf; |
| 85 |
b->length = size; |
| 86 |
b->max = size; |
| 87 |
|
| 88 |
ret->flags |= BIO_FLAGS_MEM_RDONLY; |
| 89 |
|
| 90 |
/* |num| is used to store the value that this BIO will return when it runs |
| 91 |
* out of data. If it's negative then the retry flags will also be set. Since |
| 92 |
* this is static data, retrying wont help */ |
| 93 |
ret->num = 0; |
| 94 |
|
| 95 |
return ret; |
| 96 |
} |
| 97 |
|
| 98 |
static int mem_new(BIO *bio) { |
| 99 |
BUF_MEM *b; |
| 100 |
|
| 101 |
b = BUF_MEM_new(); |
| 102 |
if (b == NULL) { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
/* |shutdown| is used to store the close flag: whether the BIO has ownership |
| 107 |
* of the BUF_MEM. */ |
| 108 |
bio->shutdown = 1; |
| 109 |
bio->init = 1; |
| 110 |
bio->num = -1; |
| 111 |
bio->ptr = (char *)b; |
| 112 |
|
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
static int mem_free(BIO *bio) { |
| 117 |
BUF_MEM *b; |
| 118 |
|
| 119 |
if (bio == NULL) { |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
if (!bio->shutdown || !bio->init || bio->ptr == NULL) { |
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
b = (BUF_MEM *)bio->ptr; |
| 128 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 129 |
b->data = NULL; |
| 130 |
} |
| 131 |
BUF_MEM_free(b); |
| 132 |
bio->ptr = NULL; |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
static int mem_read(BIO *bio, char *out, int outl) { |
| 137 |
int ret; |
| 138 |
BUF_MEM *b = (BUF_MEM*) bio->ptr; |
| 139 |
|
| 140 |
BIO_clear_retry_flags(bio); |
| 141 |
ret = outl; |
| 142 |
if (b->length < INT_MAX && ret > (int)b->length) { |
| 143 |
ret = b->length; |
| 144 |
} |
| 145 |
|
| 146 |
if (ret > 0) { |
| 147 |
memcpy(out, b->data, ret); |
| 148 |
b->length -= ret; |
| 149 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 150 |
b->data += ret; |
| 151 |
} else { |
| 152 |
memmove(b->data, &b->data[ret], b->length); |
| 153 |
} |
| 154 |
} else if (b->length == 0) { |
| 155 |
ret = bio->num; |
| 156 |
if (ret != 0) { |
| 157 |
BIO_set_retry_read(bio); |
| 158 |
} |
| 159 |
} |
| 160 |
return ret; |
| 161 |
} |
| 162 |
|
| 163 |
static int mem_write(BIO *bio, const char *in, int inl) { |
| 164 |
int ret = -1; |
| 165 |
int blen; |
| 166 |
BUF_MEM *b; |
| 167 |
|
| 168 |
b = (BUF_MEM *)bio->ptr; |
| 169 |
|
| 170 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 171 |
OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO); |
| 172 |
goto err; |
| 173 |
} |
| 174 |
|
| 175 |
BIO_clear_retry_flags(bio); |
| 176 |
blen = b->length; |
| 177 |
if (INT_MAX - blen < inl) { |
| 178 |
goto err; |
| 179 |
} |
| 180 |
if (BUF_MEM_grow_clean(b, blen + inl) != ((size_t) blen) + inl) { |
| 181 |
goto err; |
| 182 |
} |
| 183 |
memcpy(&b->data[blen], in, inl); |
| 184 |
ret = inl; |
| 185 |
|
| 186 |
err: |
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
static int mem_puts(BIO *bp, const char *str) { |
| 191 |
return mem_write(bp, str, strlen(str)); |
| 192 |
} |
| 193 |
|
| 194 |
static int mem_gets(BIO *bio, char *buf, int size) { |
| 195 |
int i, j; |
| 196 |
char *p; |
| 197 |
BUF_MEM *b = (BUF_MEM *)bio->ptr; |
| 198 |
|
| 199 |
BIO_clear_retry_flags(bio); |
| 200 |
j = b->length; |
| 201 |
if (size - 1 < j) { |
| 202 |
j = size - 1; |
| 203 |
} |
| 204 |
if (j <= 0) { |
| 205 |
if (size > 0) { |
| 206 |
*buf = 0; |
| 207 |
} |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
p = b->data; |
| 212 |
for (i = 0; i < j; i++) { |
| 213 |
if (p[i] == '\n') { |
| 214 |
i++; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* i is now the max num of bytes to copy, either j or up to and including the |
| 220 |
* first newline */ |
| 221 |
|
| 222 |
i = mem_read(bio, buf, i); |
| 223 |
if (i > 0) { |
| 224 |
buf[i] = '\0'; |
| 225 |
} |
| 226 |
return i; |
| 227 |
} |
| 228 |
|
| 229 |
static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 230 |
long ret = 1; |
| 231 |
char **pptr; |
| 232 |
|
| 233 |
BUF_MEM *b = (BUF_MEM *)bio->ptr; |
| 234 |
|
| 235 |
switch (cmd) { |
| 236 |
case BIO_CTRL_RESET: |
| 237 |
if (b->data != NULL) { |
| 238 |
/* For read only case reset to the start again */ |
| 239 |
if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 240 |
b->data -= b->max - b->length; |
| 241 |
b->length = b->max; |
| 242 |
} else { |
| 243 |
memset(b->data, 0, b->max); |
| 244 |
b->length = 0; |
| 245 |
} |
| 246 |
} |
| 247 |
break; |
| 248 |
case BIO_CTRL_EOF: |
| 249 |
ret = (long)(b->length == 0); |
| 250 |
break; |
| 251 |
case BIO_C_SET_BUF_MEM_EOF_RETURN: |
| 252 |
bio->num = (int)num; |
| 253 |
break; |
| 254 |
case BIO_CTRL_INFO: |
| 255 |
ret = (long)b->length; |
| 256 |
if (ptr != NULL) { |
| 257 |
pptr = (char **)ptr; |
| 258 |
*pptr = (char *)&b->data[0]; |
| 259 |
} |
| 260 |
break; |
| 261 |
case BIO_C_SET_BUF_MEM: |
| 262 |
mem_free(bio); |
| 263 |
bio->shutdown = (int)num; |
| 264 |
bio->ptr = ptr; |
| 265 |
break; |
| 266 |
case BIO_C_GET_BUF_MEM_PTR: |
| 267 |
if (ptr != NULL) { |
| 268 |
pptr = (char **)ptr; |
| 269 |
*pptr = (char *)b; |
| 270 |
} |
| 271 |
break; |
| 272 |
case BIO_CTRL_GET_CLOSE: |
| 273 |
ret = (long)bio->shutdown; |
| 274 |
break; |
| 275 |
case BIO_CTRL_SET_CLOSE: |
| 276 |
bio->shutdown = (int)num; |
| 277 |
break; |
| 278 |
|
| 279 |
case BIO_CTRL_WPENDING: |
| 280 |
ret = 0L; |
| 281 |
break; |
| 282 |
case BIO_CTRL_PENDING: |
| 283 |
ret = (long)b->length; |
| 284 |
break; |
| 285 |
case BIO_CTRL_FLUSH: |
| 286 |
ret = 1; |
| 287 |
break; |
| 288 |
default: |
| 289 |
ret = 0; |
| 290 |
break; |
| 291 |
} |
| 292 |
return ret; |
| 293 |
} |
| 294 |
|
| 295 |
static const BIO_METHOD mem_method = { |
| 296 |
BIO_TYPE_MEM, "memory buffer", mem_write, mem_read, mem_puts, |
| 297 |
mem_gets, mem_ctrl, mem_new, mem_free, NULL, }; |
| 298 |
|
| 299 |
const BIO_METHOD *BIO_s_mem(void) { return &mem_method; } |
| 300 |
|
| 301 |
int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents, |
| 302 |
size_t *out_len) { |
| 303 |
const BUF_MEM *b; |
| 304 |
if (bio->method != &mem_method) { |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
b = (BUF_MEM *)bio->ptr; |
| 309 |
*out_contents = (uint8_t *)b->data; |
| 310 |
*out_len = b->length; |
| 311 |
return 1; |
| 312 |
} |
| 313 |
|
| 314 |
long BIO_get_mem_data(BIO *bio, char **contents) { |
| 315 |
return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents); |
| 316 |
} |
| 317 |
|
| 318 |
int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) { |
| 319 |
return BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out); |
| 320 |
} |
| 321 |
|
| 322 |
int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) { |
| 323 |
return BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b); |
| 324 |
} |
| 325 |
|
| 326 |
int BIO_set_mem_eof_return(BIO *bio, int eof_value) { |
| 327 |
return BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL); |
| 328 |
} |