| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bio.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/buf.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 |
| 24 | #include "../internal.h" |
| 25 |
| 26 |
| 27 | BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) { |
| 28 | BIO *ret; |
| 29 | BUF_MEM *b; |
| 30 | const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len; |
| 31 |
| 32 | if (!buf && len != 0) { |
| 33 | OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER); |
| 34 | return NULL; |
| 35 | } |
| 36 |
| 37 | ret = BIO_new(BIO_s_mem()); |
| 38 | if (ret == NULL) { |
| 39 | return NULL; |
| 40 | } |
| 41 |
| 42 | b = (BUF_MEM *)ret->ptr; |
| 43 | // BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to. |
| 44 | b->data = reinterpret_cast<char *>(const_cast<void *>(buf)); |
| 45 | b->length = size; |
| 46 | b->max = size; |
| 47 |
| 48 | ret->flags |= BIO_FLAGS_MEM_RDONLY; |
| 49 |
| 50 | // |num| is used to store the value that this BIO will return when it runs |
| 51 | // out of data. If it's negative then the retry flags will also be set. Since |
| 52 | // this is static data, retrying wont help |
| 53 | ret->num = 0; |
| 54 |
| 55 | return ret; |
| 56 | } |
| 57 |
| 58 | static int mem_new(BIO *bio) { |
| 59 | BUF_MEM *b; |
| 60 |
| 61 | b = BUF_MEM_new(); |
| 62 | if (b == NULL) { |
| 63 | return 0; |
| 64 | } |
| 65 |
| 66 | // |shutdown| is used to store the close flag: whether the BIO has ownership |
| 67 | // of the BUF_MEM. |
| 68 | bio->shutdown = 1; |
| 69 | bio->init = 1; |
| 70 | bio->num = -1; |
| 71 | bio->ptr = (char *)b; |
| 72 |
| 73 | return 1; |
| 74 | } |
| 75 |
| 76 | static int mem_free(BIO *bio) { |
| 77 | if (!bio->shutdown || !bio->init || bio->ptr == NULL) { |
| 78 | return 1; |
| 79 | } |
| 80 |
| 81 | BUF_MEM *b = (BUF_MEM *)bio->ptr; |
| 82 | if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 83 | b->data = NULL; |
| 84 | } |
| 85 | BUF_MEM_free(b); |
| 86 | bio->ptr = NULL; |
| 87 | return 1; |
| 88 | } |
| 89 |
| 90 | static int mem_read(BIO *bio, char *out, int outl) { |
| 91 | BIO_clear_retry_flags(bio); |
| 92 | if (outl <= 0) { |
| 93 | return 0; |
| 94 | } |
| 95 |
| 96 | BUF_MEM *b = reinterpret_cast<BUF_MEM *>(bio->ptr); |
| 97 | int ret = outl; |
| 98 | if ((size_t)ret > b->length) { |
| 99 | ret = (int)b->length; |
| 100 | } |
| 101 |
| 102 | if (ret > 0) { |
| 103 | OPENSSL_memcpy(out, b->data, ret); |
| 104 | b->length -= ret; |
| 105 | if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 106 | b->data += ret; |
| 107 | } else { |
| 108 | OPENSSL_memmove(b->data, &b->data[ret], b->length); |
| 109 | } |
| 110 | } else if (b->length == 0) { |
| 111 | ret = bio->num; |
| 112 | if (ret != 0) { |
| 113 | BIO_set_retry_read(bio); |
| 114 | } |
| 115 | } |
| 116 | return ret; |
| 117 | } |
| 118 |
| 119 | static int mem_write(BIO *bio, const char *in, int inl) { |
| 120 | BIO_clear_retry_flags(bio); |
| 121 | if (inl <= 0) { |
| 122 | return 0; // Successfully write zero bytes. |
| 123 | } |
| 124 |
| 125 | if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 126 | OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO); |
| 127 | return -1; |
| 128 | } |
| 129 |
| 130 | BUF_MEM *b = reinterpret_cast<BUF_MEM *>(bio->ptr); |
| 131 | if (!BUF_MEM_append(b, in, inl)) { |
| 132 | return -1; |
| 133 | } |
| 134 |
| 135 | return inl; |
| 136 | } |
| 137 |
| 138 | static int mem_gets(BIO *bio, char *buf, int size) { |
| 139 | BIO_clear_retry_flags(bio); |
| 140 | if (size <= 0) { |
| 141 | return 0; |
| 142 | } |
| 143 |
| 144 | // The buffer size includes space for the trailing NUL, so we can read at most |
| 145 | // one fewer byte. |
| 146 | BUF_MEM *b = reinterpret_cast<BUF_MEM *>(bio->ptr); |
| 147 | int ret = size - 1; |
| 148 | if ((size_t)ret > b->length) { |
| 149 | ret = (int)b->length; |
| 150 | } |
| 151 |
| 152 | // Stop at the first newline. |
| 153 | const char *newline = |
| 154 | reinterpret_cast<char *>(OPENSSL_memchr(b->data, '\n', ret)); |
| 155 | if (newline != NULL) { |
| 156 | ret = (int)(newline - b->data + 1); |
| 157 | } |
| 158 |
| 159 | ret = mem_read(bio, buf, ret); |
| 160 | if (ret >= 0) { |
| 161 | buf[ret] = '\0'; |
| 162 | } |
| 163 | return ret; |
| 164 | } |
| 165 |
| 166 | static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 167 | long ret = 1; |
| 168 |
| 169 | BUF_MEM *b = (BUF_MEM *)bio->ptr; |
| 170 |
| 171 | switch (cmd) { |
| 172 | case BIO_CTRL_RESET: |
| 173 | if (b->data != NULL) { |
| 174 | // For read only case reset to the start again |
| 175 | if (bio->flags & BIO_FLAGS_MEM_RDONLY) { |
| 176 | b->data -= b->max - b->length; |
| 177 | b->length = b->max; |
| 178 | } else { |
| 179 | OPENSSL_memset(b->data, 0, b->max); |
| 180 | b->length = 0; |
| 181 | } |
| 182 | } |
| 183 | break; |
| 184 | case BIO_CTRL_EOF: |
| 185 | ret = (long)(b->length == 0); |
| 186 | break; |
| 187 | case BIO_C_SET_BUF_MEM_EOF_RETURN: |
| 188 | bio->num = (int)num; |
| 189 | break; |
| 190 | case BIO_CTRL_INFO: |
| 191 | ret = (long)b->length; |
| 192 | if (ptr != NULL) { |
| 193 | char **pptr = reinterpret_cast<char **>(ptr); |
| 194 | *pptr = b->data; |
| 195 | } |
| 196 | break; |
| 197 | case BIO_C_SET_BUF_MEM: |
| 198 | mem_free(bio); |
| 199 | bio->shutdown = (int)num; |
| 200 | bio->ptr = ptr; |
| 201 | break; |
| 202 | case BIO_C_GET_BUF_MEM_PTR: |
| 203 | if (ptr != NULL) { |
| 204 | BUF_MEM **pptr = reinterpret_cast<BUF_MEM **>(ptr); |
| 205 | *pptr = b; |
| 206 | } |
| 207 | break; |
| 208 | case BIO_CTRL_GET_CLOSE: |
| 209 | ret = (long)bio->shutdown; |
| 210 | break; |
| 211 | case BIO_CTRL_SET_CLOSE: |
| 212 | bio->shutdown = (int)num; |
| 213 | break; |
| 214 |
| 215 | case BIO_CTRL_WPENDING: |
| 216 | ret = 0L; |
| 217 | break; |
| 218 | case BIO_CTRL_PENDING: |
| 219 | ret = (long)b->length; |
| 220 | break; |
| 221 | case BIO_CTRL_FLUSH: |
| 222 | ret = 1; |
| 223 | break; |
| 224 | default: |
| 225 | ret = 0; |
| 226 | break; |
| 227 | } |
| 228 | return ret; |
| 229 | } |
| 230 |
| 231 | static const BIO_METHOD mem_method = { |
| 232 | BIO_TYPE_MEM, "memory buffer", |
| 233 | mem_write, mem_read, |
| 234 | NULL /* puts */, mem_gets, |
| 235 | mem_ctrl, mem_new, |
| 236 | mem_free, NULL /* callback_ctrl */, |
| 237 | }; |
| 238 |
| 239 | const BIO_METHOD *BIO_s_mem(void) { return &mem_method; } |
| 240 |
| 241 | int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents, |
| 242 | size_t *out_len) { |
| 243 | const BUF_MEM *b; |
| 244 | if (bio->method != &mem_method) { |
| 245 | return 0; |
| 246 | } |
| 247 |
| 248 | b = (BUF_MEM *)bio->ptr; |
| 249 | *out_contents = (uint8_t *)b->data; |
| 250 | *out_len = b->length; |
| 251 | return 1; |
| 252 | } |
| 253 |
| 254 | long BIO_get_mem_data(BIO *bio, char **contents) { |
| 255 | return BIO_ctrl(bio, BIO_CTRL_INFO, 0, contents); |
| 256 | } |
| 257 |
| 258 | int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) { |
| 259 | return (int)BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, out); |
| 260 | } |
| 261 |
| 262 | int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) { |
| 263 | return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, b); |
| 264 | } |
| 265 |
| 266 | int BIO_set_mem_eof_return(BIO *bio, int eof_value) { |
| 267 | return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL); |
| 268 | } |
| 269 |