/src/openssl30/crypto/bio/bss_mem.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include <stdio.h> | 
| 11 |  | #include <errno.h> | 
| 12 |  | #include "bio_local.h" | 
| 13 |  | #include "internal/cryptlib.h" | 
| 14 |  |  | 
| 15 |  | static int mem_write(BIO *h, const char *buf, int num); | 
| 16 |  | static int mem_read(BIO *h, char *buf, int size); | 
| 17 |  | static int mem_puts(BIO *h, const char *str); | 
| 18 |  | static int mem_gets(BIO *h, char *str, int size); | 
| 19 |  | static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2); | 
| 20 |  | static int mem_new(BIO *h); | 
| 21 |  | static int secmem_new(BIO *h); | 
| 22 |  | static int mem_free(BIO *data); | 
| 23 |  | static int mem_buf_free(BIO *data); | 
| 24 |  | static int mem_buf_sync(BIO *h); | 
| 25 |  |  | 
| 26 |  | static const BIO_METHOD mem_method = { | 
| 27 |  |     BIO_TYPE_MEM, | 
| 28 |  |     "memory buffer", | 
| 29 |  |     bwrite_conv, | 
| 30 |  |     mem_write, | 
| 31 |  |     bread_conv, | 
| 32 |  |     mem_read, | 
| 33 |  |     mem_puts, | 
| 34 |  |     mem_gets, | 
| 35 |  |     mem_ctrl, | 
| 36 |  |     mem_new, | 
| 37 |  |     mem_free, | 
| 38 |  |     NULL,                      /* mem_callback_ctrl */ | 
| 39 |  | }; | 
| 40 |  |  | 
| 41 |  | static const BIO_METHOD secmem_method = { | 
| 42 |  |     BIO_TYPE_MEM, | 
| 43 |  |     "secure memory buffer", | 
| 44 |  |     bwrite_conv, | 
| 45 |  |     mem_write, | 
| 46 |  |     bread_conv, | 
| 47 |  |     mem_read, | 
| 48 |  |     mem_puts, | 
| 49 |  |     mem_gets, | 
| 50 |  |     mem_ctrl, | 
| 51 |  |     secmem_new, | 
| 52 |  |     mem_free, | 
| 53 |  |     NULL,                      /* mem_callback_ctrl */ | 
| 54 |  | }; | 
| 55 |  |  | 
| 56 |  | /* | 
| 57 |  |  * BIO memory stores buffer and read pointer | 
| 58 |  |  * however the roles are different for read only BIOs. | 
| 59 |  |  * In that case the readp just stores the original state | 
| 60 |  |  * to be used for reset. | 
| 61 |  |  */ | 
| 62 |  | typedef struct bio_buf_mem_st { | 
| 63 |  |     struct buf_mem_st *buf;   /* allocated buffer */ | 
| 64 |  |     struct buf_mem_st *readp; /* read pointer */ | 
| 65 |  | } BIO_BUF_MEM; | 
| 66 |  |  | 
| 67 |  | /* | 
| 68 |  |  * bio->num is used to hold the value to return on 'empty', if it is 0, | 
| 69 |  |  * should_retry is not set | 
| 70 |  |  */ | 
| 71 |  |  | 
| 72 |  | const BIO_METHOD *BIO_s_mem(void) | 
| 73 | 1.57M | { | 
| 74 | 1.57M |     return &mem_method; | 
| 75 | 1.57M | } | 
| 76 |  |  | 
| 77 |  | const BIO_METHOD *BIO_s_secmem(void) | 
| 78 | 12.7k | { | 
| 79 | 12.7k |     return(&secmem_method); | 
| 80 | 12.7k | } | 
| 81 |  |  | 
| 82 |  | BIO *BIO_new_mem_buf(const void *buf, int len) | 
| 83 | 841k | { | 
| 84 | 841k |     BIO *ret; | 
| 85 | 841k |     BUF_MEM *b; | 
| 86 | 841k |     BIO_BUF_MEM *bb; | 
| 87 | 841k |     size_t sz; | 
| 88 |  |  | 
| 89 | 841k |     if (buf == NULL) { | 
| 90 | 0 |         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | 
| 91 | 0 |         return NULL; | 
| 92 | 0 |     } | 
| 93 | 841k |     sz = (len < 0) ? strlen(buf) : (size_t)len; | 
| 94 | 841k |     if ((ret = BIO_new(BIO_s_mem())) == NULL) | 
| 95 | 0 |         return NULL; | 
| 96 | 841k |     bb = (BIO_BUF_MEM *)ret->ptr; | 
| 97 | 841k |     b = bb->buf; | 
| 98 |  |     /* Cast away const and trust in the MEM_RDONLY flag. */ | 
| 99 | 841k |     b->data = (void *)buf; | 
| 100 | 841k |     b->length = sz; | 
| 101 | 841k |     b->max = sz; | 
| 102 | 841k |     *bb->readp = *bb->buf; | 
| 103 | 841k |     ret->flags |= BIO_FLAGS_MEM_RDONLY; | 
| 104 |  |     /* Since this is static data retrying won't help */ | 
| 105 | 841k |     ret->num = 0; | 
| 106 | 841k |     return ret; | 
| 107 | 841k | } | 
| 108 |  |  | 
| 109 |  | static int mem_init(BIO *bi, unsigned long flags) | 
| 110 | 1.66M | { | 
| 111 | 1.66M |     BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb)); | 
| 112 |  |  | 
| 113 | 1.66M |     if (bb == NULL) | 
| 114 | 0 |         return 0; | 
| 115 | 1.66M |     if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) { | 
| 116 | 0 |         OPENSSL_free(bb); | 
| 117 | 0 |         return 0; | 
| 118 | 0 |     } | 
| 119 | 1.66M |     if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) { | 
| 120 | 0 |         BUF_MEM_free(bb->buf); | 
| 121 | 0 |         OPENSSL_free(bb); | 
| 122 | 0 |         return 0; | 
| 123 | 0 |     } | 
| 124 | 1.66M |     *bb->readp = *bb->buf; | 
| 125 | 1.66M |     bi->shutdown = 1; | 
| 126 | 1.66M |     bi->init = 1; | 
| 127 | 1.66M |     bi->num = -1; | 
| 128 | 1.66M |     bi->ptr = (char *)bb; | 
| 129 | 1.66M |     return 1; | 
| 130 | 1.66M | } | 
| 131 |  |  | 
| 132 |  | static int mem_new(BIO *bi) | 
| 133 | 1.63M | { | 
| 134 | 1.63M |     return mem_init(bi, 0L); | 
| 135 | 1.63M | } | 
| 136 |  |  | 
| 137 |  | static int secmem_new(BIO *bi) | 
| 138 | 25.4k | { | 
| 139 | 25.4k |     return mem_init(bi, BUF_MEM_FLAG_SECURE); | 
| 140 | 25.4k | } | 
| 141 |  |  | 
| 142 |  | static int mem_free(BIO *a) | 
| 143 | 1.66M | { | 
| 144 | 1.66M |     BIO_BUF_MEM *bb; | 
| 145 |  |  | 
| 146 | 1.66M |     if (a == NULL) | 
| 147 | 0 |         return 0; | 
| 148 |  |  | 
| 149 | 1.66M |     bb = (BIO_BUF_MEM *)a->ptr; | 
| 150 | 1.66M |     if (!mem_buf_free(a)) | 
| 151 | 0 |         return 0; | 
| 152 | 1.66M |     OPENSSL_free(bb->readp); | 
| 153 | 1.66M |     OPENSSL_free(bb); | 
| 154 | 1.66M |     return 1; | 
| 155 | 1.66M | } | 
| 156 |  |  | 
| 157 |  | static int mem_buf_free(BIO *a) | 
| 158 | 1.66M | { | 
| 159 | 1.66M |     if (a == NULL) | 
| 160 | 0 |         return 0; | 
| 161 |  |  | 
| 162 | 1.66M |     if (a->shutdown && a->init && a->ptr != NULL) { | 
| 163 | 1.66M |         BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr; | 
| 164 | 1.66M |         BUF_MEM *b = bb->buf; | 
| 165 |  |  | 
| 166 | 1.66M |         if (a->flags & BIO_FLAGS_MEM_RDONLY) | 
| 167 | 841k |             b->data = NULL; | 
| 168 | 1.66M |         BUF_MEM_free(b); | 
| 169 | 1.66M |     } | 
| 170 | 1.66M |     return 1; | 
| 171 | 1.66M | } | 
| 172 |  |  | 
| 173 |  | /* | 
| 174 |  |  * Reallocate memory buffer if read pointer differs | 
| 175 |  |  * NOT FOR RDONLY | 
| 176 |  |  */ | 
| 177 |  | static int mem_buf_sync(BIO *b) | 
| 178 | 15.8M | { | 
| 179 | 15.8M |     if (b != NULL && b->init != 0 && b->ptr != NULL) { | 
| 180 | 15.8M |         BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr; | 
| 181 |  |  | 
| 182 | 15.8M |         if (bbm->readp->data != bbm->buf->data) { | 
| 183 | 159 |             memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length); | 
| 184 | 159 |             bbm->buf->length = bbm->readp->length; | 
| 185 | 159 |             bbm->readp->data = bbm->buf->data; | 
| 186 | 159 |         } | 
| 187 | 15.8M |     } | 
| 188 | 15.8M |     return 0; | 
| 189 | 15.8M | } | 
| 190 |  |  | 
| 191 |  | static int mem_read(BIO *b, char *out, int outl) | 
| 192 | 720M | { | 
| 193 | 720M |     int ret = -1; | 
| 194 | 720M |     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr; | 
| 195 | 720M |     BUF_MEM *bm = bbm->readp; | 
| 196 |  |  | 
| 197 | 720M |     if (b->flags & BIO_FLAGS_MEM_RDONLY) | 
| 198 | 640M |         bm = bbm->buf; | 
| 199 | 720M |     BIO_clear_retry_flags(b); | 
| 200 | 720M |     ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl; | 
| 201 | 720M |     if ((out != NULL) && (ret > 0)) { | 
| 202 | 719M |         memcpy(out, bm->data, ret); | 
| 203 | 719M |         bm->length -= ret; | 
| 204 | 719M |         bm->max -= ret; | 
| 205 | 719M |         bm->data += ret; | 
| 206 | 719M |     } else if (bm->length == 0) { | 
| 207 | 1.08M |         ret = b->num; | 
| 208 | 1.08M |         if (ret != 0) | 
| 209 | 90.6k |             BIO_set_retry_read(b); | 
| 210 | 1.08M |     } | 
| 211 | 720M |     return ret; | 
| 212 | 720M | } | 
| 213 |  |  | 
| 214 |  | static int mem_write(BIO *b, const char *in, int inl) | 
| 215 | 15.4M | { | 
| 216 | 15.4M |     int ret = -1; | 
| 217 | 15.4M |     int blen; | 
| 218 | 15.4M |     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr; | 
| 219 |  |  | 
| 220 | 15.4M |     if (b->flags & BIO_FLAGS_MEM_RDONLY) { | 
| 221 | 0 |         ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO); | 
| 222 | 0 |         goto end; | 
| 223 | 0 |     } | 
| 224 | 15.4M |     BIO_clear_retry_flags(b); | 
| 225 | 15.4M |     if (inl == 0) | 
| 226 | 4.36k |         return 0; | 
| 227 | 15.4M |     if (in == NULL) { | 
| 228 | 0 |         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | 
| 229 | 0 |         goto end; | 
| 230 | 0 |     } | 
| 231 | 15.4M |     blen = bbm->readp->length; | 
| 232 | 15.4M |     mem_buf_sync(b); | 
| 233 | 15.4M |     if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0) | 
| 234 | 0 |         goto end; | 
| 235 | 15.4M |     memcpy(bbm->buf->data + blen, in, inl); | 
| 236 | 15.4M |     *bbm->readp = *bbm->buf; | 
| 237 | 15.4M |     ret = inl; | 
| 238 | 15.4M |  end: | 
| 239 | 15.4M |     return ret; | 
| 240 | 15.4M | } | 
| 241 |  |  | 
| 242 |  | static long mem_ctrl(BIO *b, int cmd, long num, void *ptr) | 
| 243 | 5.66M | { | 
| 244 | 5.66M |     long ret = 1; | 
| 245 | 5.66M |     char **pptr; | 
| 246 | 5.66M |     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr; | 
| 247 | 5.66M |     BUF_MEM *bm, *bo;            /* bio_mem, bio_other */ | 
| 248 | 5.66M |     long off, remain; | 
| 249 |  |  | 
| 250 | 5.66M |     if (b->flags & BIO_FLAGS_MEM_RDONLY) { | 
| 251 | 4.93M |         bm = bbm->buf; | 
| 252 | 4.93M |         bo = bbm->readp; | 
| 253 | 4.93M |     } else { | 
| 254 | 728k |         bm = bbm->readp; | 
| 255 | 728k |         bo = bbm->buf; | 
| 256 | 728k |     } | 
| 257 | 5.66M |     off = (bm->data == bo->data) ? 0 : bm->data - bo->data; | 
| 258 | 5.66M |     remain = bm->length; | 
| 259 |  |  | 
| 260 | 5.66M |     switch (cmd) { | 
| 261 | 0 |     case BIO_CTRL_RESET: | 
| 262 | 0 |         bm = bbm->buf; | 
| 263 | 0 |         if (bm->data != NULL) { | 
| 264 | 0 |             if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) { | 
| 265 | 0 |                 if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) { | 
| 266 | 0 |                     memset(bm->data, 0, bm->max); | 
| 267 | 0 |                     bm->length = 0; | 
| 268 | 0 |                 } | 
| 269 | 0 |                 *bbm->readp = *bbm->buf; | 
| 270 | 0 |             } else { | 
| 271 |  |                 /* For read only case just reset to the start again */ | 
| 272 | 0 |                 *bbm->buf = *bbm->readp; | 
| 273 | 0 |             } | 
| 274 | 0 |         } | 
| 275 | 0 |         break; | 
| 276 | 1.77M |     case BIO_C_FILE_SEEK: | 
| 277 | 1.77M |         if (num < 0 || num > off + remain) | 
| 278 | 0 |             return -1;   /* Can't see outside of the current buffer */ | 
| 279 |  |  | 
| 280 | 1.77M |         bm->data = (num != 0) ? bo->data + num : bo->data; | 
| 281 | 1.77M |         bm->length = bo->length - num; | 
| 282 | 1.77M |         bm->max = bo->max - num; | 
| 283 | 1.77M |         off = num; | 
| 284 |  |         /* FALLTHRU */ | 
| 285 | 4.86M |     case BIO_C_FILE_TELL: | 
| 286 | 4.86M |         ret = off; | 
| 287 | 4.86M |         break; | 
| 288 | 1.18k |     case BIO_CTRL_EOF: | 
| 289 | 1.18k |         ret = (long)(bm->length == 0); | 
| 290 | 1.18k |         break; | 
| 291 | 395k |     case BIO_C_SET_BUF_MEM_EOF_RETURN: | 
| 292 | 395k |         b->num = (int)num; | 
| 293 | 395k |         break; | 
| 294 | 232k |     case BIO_CTRL_INFO: | 
| 295 | 232k |         ret = (long)bm->length; | 
| 296 | 232k |         if (ptr != NULL) { | 
| 297 | 192k |             pptr = (char **)ptr; | 
| 298 | 192k |             *pptr = (char *)(bm->data); | 
| 299 | 192k |         } | 
| 300 | 232k |         break; | 
| 301 | 0 |     case BIO_C_SET_BUF_MEM: | 
| 302 | 0 |         mem_buf_free(b); | 
| 303 | 0 |         b->shutdown = (int)num; | 
| 304 | 0 |         bbm->buf = ptr; | 
| 305 | 0 |         *bbm->readp = *bbm->buf; | 
| 306 | 0 |         break; | 
| 307 | 43.1k |     case BIO_C_GET_BUF_MEM_PTR: | 
| 308 | 43.1k |         if (ptr != NULL) { | 
| 309 | 43.1k |             if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) | 
| 310 | 43.1k |                 mem_buf_sync(b); | 
| 311 | 43.1k |             bm = bbm->buf; | 
| 312 | 43.1k |             pptr = (char **)ptr; | 
| 313 | 43.1k |             *pptr = (char *)bm; | 
| 314 | 43.1k |         } | 
| 315 | 43.1k |         break; | 
| 316 | 0 |     case BIO_CTRL_GET_CLOSE: | 
| 317 | 0 |         ret = (long)b->shutdown; | 
| 318 | 0 |         break; | 
| 319 | 25.3k |     case BIO_CTRL_SET_CLOSE: | 
| 320 | 25.3k |         b->shutdown = (int)num; | 
| 321 | 25.3k |         break; | 
| 322 | 0 |     case BIO_CTRL_WPENDING: | 
| 323 | 0 |         ret = 0L; | 
| 324 | 0 |         break; | 
| 325 | 0 |     case BIO_CTRL_PENDING: | 
| 326 | 0 |         ret = (long)bm->length; | 
| 327 | 0 |         break; | 
| 328 | 0 |     case BIO_CTRL_DUP: | 
| 329 | 44.2k |     case BIO_CTRL_FLUSH: | 
| 330 | 44.2k |         ret = 1; | 
| 331 | 44.2k |         break; | 
| 332 | 28.3k |     case BIO_CTRL_PUSH: | 
| 333 | 56.7k |     case BIO_CTRL_POP: | 
| 334 | 56.7k |     default: | 
| 335 | 56.7k |         ret = 0; | 
| 336 | 56.7k |         break; | 
| 337 | 5.66M |     } | 
| 338 | 5.66M |     return ret; | 
| 339 | 5.66M | } | 
| 340 |  |  | 
| 341 |  | static int mem_gets(BIO *bp, char *buf, int size) | 
| 342 | 26.6M | { | 
| 343 | 26.6M |     int i, j; | 
| 344 | 26.6M |     int ret = -1; | 
| 345 | 26.6M |     char *p; | 
| 346 | 26.6M |     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr; | 
| 347 | 26.6M |     BUF_MEM *bm = bbm->readp; | 
| 348 |  |  | 
| 349 | 26.6M |     if (bp->flags & BIO_FLAGS_MEM_RDONLY) | 
| 350 | 6.37M |         bm = bbm->buf; | 
| 351 | 26.6M |     BIO_clear_retry_flags(bp); | 
| 352 | 26.6M |     j = bm->length; | 
| 353 | 26.6M |     if ((size - 1) < j) | 
| 354 | 26.1M |         j = size - 1; | 
| 355 | 26.6M |     if (j <= 0) { | 
| 356 | 23.5k |         *buf = '\0'; | 
| 357 | 23.5k |         return 0; | 
| 358 | 23.5k |     } | 
| 359 | 26.6M |     p = bm->data; | 
| 360 | 293M |     for (i = 0; i < j; i++) { | 
| 361 | 292M |         if (p[i] == '\n') { | 
| 362 | 26.0M |             i++; | 
| 363 | 26.0M |             break; | 
| 364 | 26.0M |         } | 
| 365 | 292M |     } | 
| 366 |  |  | 
| 367 |  |     /* | 
| 368 |  |      * i is now the max num of bytes to copy, either j or up to | 
| 369 |  |      * and including the first newline | 
| 370 |  |      */ | 
| 371 |  |  | 
| 372 | 26.6M |     i = mem_read(bp, buf, i); | 
| 373 | 26.6M |     if (i > 0) | 
| 374 | 26.6M |         buf[i] = '\0'; | 
| 375 | 26.6M |     ret = i; | 
| 376 | 26.6M |     return ret; | 
| 377 | 26.6M | } | 
| 378 |  |  | 
| 379 |  | static int mem_puts(BIO *bp, const char *str) | 
| 380 | 1.01M | { | 
| 381 | 1.01M |     int n, ret; | 
| 382 |  |  | 
| 383 | 1.01M |     n = strlen(str); | 
| 384 | 1.01M |     ret = mem_write(bp, str, n); | 
| 385 |  |     /* memory semantics is that it will always work */ | 
| 386 | 1.01M |     return ret; | 
| 387 | 1.01M | } |