blob: 08512d396d81e83b93db649f3046b9f1b691347b [file] [log] [blame]
David Benjamin4854ec12024-12-19 17:33:20 -05001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Adam Langley95c29f32014-06-20 12:00:00 -07003 *
David Benjamin4854ec12024-12-19 17:33:20 -05004 * Licensed under the OpenSSL license (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 */
Adam Langley95c29f32014-06-20 12:00:00 -07009
10#include <openssl/bio.h>
11
12#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080013#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070014
15#include <openssl/buf.h>
16#include <openssl/err.h>
17#include <openssl/mem.h>
18
David Benjamin17cf2cb2016-12-13 01:07:13 -050019#include "../internal.h"
20
Adam Langley95c29f32014-06-20 12:00:00 -070021
David Benjamin7ac94aa2022-10-24 15:44:37 -040022BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
Adam Langley95c29f32014-06-20 12:00:00 -070023 BIO *ret;
24 BUF_MEM *b;
25 const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
26
27 if (!buf && len != 0) {
David Benjamin3570d732015-06-29 00:28:17 -040028 OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER);
Adam Langley95c29f32014-06-20 12:00:00 -070029 return NULL;
30 }
31
32 ret = BIO_new(BIO_s_mem());
33 if (ret == NULL) {
34 return NULL;
35 }
36
37 b = (BUF_MEM *)ret->ptr;
David Benjamin808f8322017-08-18 14:06:02 -040038 // BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to.
Adam Langley5813c2c2024-10-30 14:48:00 -070039 b->data = reinterpret_cast<char *>(const_cast<void *>(buf));
Adam Langley95c29f32014-06-20 12:00:00 -070040 b->length = size;
41 b->max = size;
42
43 ret->flags |= BIO_FLAGS_MEM_RDONLY;
44
David Benjamin808f8322017-08-18 14:06:02 -040045 // |num| is used to store the value that this BIO will return when it runs
46 // out of data. If it's negative then the retry flags will also be set. Since
47 // this is static data, retrying wont help
Adam Langley95c29f32014-06-20 12:00:00 -070048 ret->num = 0;
49
50 return ret;
51}
52
53static int mem_new(BIO *bio) {
54 BUF_MEM *b;
55
56 b = BUF_MEM_new();
57 if (b == NULL) {
58 return 0;
59 }
60
David Benjamin808f8322017-08-18 14:06:02 -040061 // |shutdown| is used to store the close flag: whether the BIO has ownership
62 // of the BUF_MEM.
Adam Langley95c29f32014-06-20 12:00:00 -070063 bio->shutdown = 1;
64 bio->init = 1;
65 bio->num = -1;
66 bio->ptr = (char *)b;
67
68 return 1;
69}
70
71static int mem_free(BIO *bio) {
Adam Langley95c29f32014-06-20 12:00:00 -070072 if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
73 return 1;
74 }
75
David Benjamin2e68a052021-08-11 18:14:10 -040076 BUF_MEM *b = (BUF_MEM *)bio->ptr;
Adam Langley95c29f32014-06-20 12:00:00 -070077 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
78 b->data = NULL;
79 }
80 BUF_MEM_free(b);
81 bio->ptr = NULL;
82 return 1;
83}
84
85static int mem_read(BIO *bio, char *out, int outl) {
Adam Langley95c29f32014-06-20 12:00:00 -070086 BIO_clear_retry_flags(bio);
David Benjamine8b168d2023-04-01 17:23:30 +090087 if (outl <= 0) {
88 return 0;
89 }
90
Adam Langley5813c2c2024-10-30 14:48:00 -070091 BUF_MEM *b = reinterpret_cast<BUF_MEM *>(bio->ptr);
David Benjamine8b168d2023-04-01 17:23:30 +090092 int ret = outl;
93 if ((size_t)ret > b->length) {
94 ret = (int)b->length;
Adam Langley95c29f32014-06-20 12:00:00 -070095 }
96
97 if (ret > 0) {
David Benjamin17cf2cb2016-12-13 01:07:13 -050098 OPENSSL_memcpy(out, b->data, ret);
Adam Langley95c29f32014-06-20 12:00:00 -070099 b->length -= ret;
100 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
101 b->data += ret;
102 } else {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500103 OPENSSL_memmove(b->data, &b->data[ret], b->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700104 }
105 } else if (b->length == 0) {
106 ret = bio->num;
107 if (ret != 0) {
108 BIO_set_retry_read(bio);
109 }
110 }
111 return ret;
112}
113
114static int mem_write(BIO *bio, const char *in, int inl) {
David Benjamine8b168d2023-04-01 17:23:30 +0900115 BIO_clear_retry_flags(bio);
116 if (inl <= 0) {
117 return 0; // Successfully write zero bytes.
118 }
Adam Langley95c29f32014-06-20 12:00:00 -0700119
120 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
David Benjamin3570d732015-06-29 00:28:17 -0400121 OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
David Benjamine8b168d2023-04-01 17:23:30 +0900122 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700123 }
124
Adam Langley5813c2c2024-10-30 14:48:00 -0700125 BUF_MEM *b = reinterpret_cast<BUF_MEM *>(bio->ptr);
David Benjamine8b168d2023-04-01 17:23:30 +0900126 if (!BUF_MEM_append(b, in, inl)) {
127 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700128 }
Adam Langley95c29f32014-06-20 12:00:00 -0700129
David Benjamine8b168d2023-04-01 17:23:30 +0900130 return inl;
Adam Langley95c29f32014-06-20 12:00:00 -0700131}
132
Adam Langley95c29f32014-06-20 12:00:00 -0700133static int mem_gets(BIO *bio, char *buf, int size) {
Adam Langley95c29f32014-06-20 12:00:00 -0700134 BIO_clear_retry_flags(bio);
David Benjamine8b168d2023-04-01 17:23:30 +0900135 if (size <= 0) {
Adam Langley95c29f32014-06-20 12:00:00 -0700136 return 0;
137 }
138
David Benjamine8b168d2023-04-01 17:23:30 +0900139 // The buffer size includes space for the trailing NUL, so we can read at most
140 // one fewer byte.
Adam Langley5813c2c2024-10-30 14:48:00 -0700141 BUF_MEM *b = reinterpret_cast<BUF_MEM *>(bio->ptr);
David Benjamine8b168d2023-04-01 17:23:30 +0900142 int ret = size - 1;
143 if ((size_t)ret > b->length) {
144 ret = (int)b->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700145 }
146
David Benjamine8b168d2023-04-01 17:23:30 +0900147 // Stop at the first newline.
Adam Langley5813c2c2024-10-30 14:48:00 -0700148 const char *newline =
149 reinterpret_cast<char *>(OPENSSL_memchr(b->data, '\n', ret));
David Benjamine8b168d2023-04-01 17:23:30 +0900150 if (newline != NULL) {
151 ret = (int)(newline - b->data + 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700152 }
David Benjamine8b168d2023-04-01 17:23:30 +0900153
154 ret = mem_read(bio, buf, ret);
155 if (ret >= 0) {
156 buf[ret] = '\0';
157 }
158 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700159}
160
161static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
162 long ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700163
164 BUF_MEM *b = (BUF_MEM *)bio->ptr;
165
166 switch (cmd) {
167 case BIO_CTRL_RESET:
168 if (b->data != NULL) {
David Benjamin808f8322017-08-18 14:06:02 -0400169 // For read only case reset to the start again
Adam Langley95c29f32014-06-20 12:00:00 -0700170 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
171 b->data -= b->max - b->length;
172 b->length = b->max;
173 } else {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500174 OPENSSL_memset(b->data, 0, b->max);
Adam Langley95c29f32014-06-20 12:00:00 -0700175 b->length = 0;
176 }
177 }
178 break;
179 case BIO_CTRL_EOF:
180 ret = (long)(b->length == 0);
181 break;
182 case BIO_C_SET_BUF_MEM_EOF_RETURN:
183 bio->num = (int)num;
184 break;
185 case BIO_CTRL_INFO:
186 ret = (long)b->length;
187 if (ptr != NULL) {
Adam Langley5813c2c2024-10-30 14:48:00 -0700188 char **pptr = reinterpret_cast<char **>(ptr);
David Benjamin8e16c042023-06-08 11:03:59 -0400189 *pptr = b->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700190 }
191 break;
192 case BIO_C_SET_BUF_MEM:
193 mem_free(bio);
194 bio->shutdown = (int)num;
195 bio->ptr = ptr;
196 break;
197 case BIO_C_GET_BUF_MEM_PTR:
198 if (ptr != NULL) {
Adam Langley5813c2c2024-10-30 14:48:00 -0700199 BUF_MEM **pptr = reinterpret_cast<BUF_MEM **>(ptr);
David Benjamin8e16c042023-06-08 11:03:59 -0400200 *pptr = b;
Adam Langley95c29f32014-06-20 12:00:00 -0700201 }
202 break;
203 case BIO_CTRL_GET_CLOSE:
204 ret = (long)bio->shutdown;
205 break;
206 case BIO_CTRL_SET_CLOSE:
207 bio->shutdown = (int)num;
208 break;
209
210 case BIO_CTRL_WPENDING:
211 ret = 0L;
212 break;
213 case BIO_CTRL_PENDING:
214 ret = (long)b->length;
215 break;
216 case BIO_CTRL_FLUSH:
217 ret = 1;
218 break;
219 default:
220 ret = 0;
221 break;
222 }
223 return ret;
224}
225
226static const BIO_METHOD mem_method = {
David Benjamin63421112017-01-26 21:50:04 -0500227 BIO_TYPE_MEM, "memory buffer",
228 mem_write, mem_read,
229 NULL /* puts */, mem_gets,
230 mem_ctrl, mem_new,
231 mem_free, NULL /* callback_ctrl */,
232};
Adam Langley95c29f32014-06-20 12:00:00 -0700233
234const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
235
Adam Langley37a623c2014-07-18 09:28:40 -0700236int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
237 size_t *out_len) {
238 const BUF_MEM *b;
239 if (bio->method != &mem_method) {
240 return 0;
241 }
242
243 b = (BUF_MEM *)bio->ptr;
244 *out_contents = (uint8_t *)b->data;
245 *out_len = b->length;
246 return 1;
247}
248
Adam Langley95c29f32014-06-20 12:00:00 -0700249long BIO_get_mem_data(BIO *bio, char **contents) {
David Benjamin8e16c042023-06-08 11:03:59 -0400250 return BIO_ctrl(bio, BIO_CTRL_INFO, 0, contents);
Adam Langley95c29f32014-06-20 12:00:00 -0700251}
252
253int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) {
David Benjamin8e16c042023-06-08 11:03:59 -0400254 return (int)BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, out);
Adam Langley95c29f32014-06-20 12:00:00 -0700255}
256
257int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
David Benjamin8e16c042023-06-08 11:03:59 -0400258 return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, b);
Adam Langley95c29f32014-06-20 12:00:00 -0700259}
260
261int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
David Benjamin02f77052022-11-23 13:00:33 -0500262 return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700263}