summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2022-02-19 08:11:16 +0000
committerjsing <>2022-02-19 08:11:16 +0000
commit27a1ec1417b81f08a40f0c6567f4ba0350d820b5 (patch)
tree29a1663e9be4393739f2297314e21fe8e96e05c6 /src/lib
parentf069b526ab8a9a426e8d02a39c00bb857f492a7d (diff)
downloadopenbsd-27a1ec1417b81f08a40f0c6567f4ba0350d820b5.tar.gz
openbsd-27a1ec1417b81f08a40f0c6567f4ba0350d820b5.tar.bz2
openbsd-27a1ec1417b81f08a40f0c6567f4ba0350d820b5.zip
Provide a struct bio_mem for memory BIO specific data.
In order to fix and improve the memory BIO, we need to be able to track more than just a single BUF_MEM *. Provide a struct bio_mem (which currently only contains a BUF_MEM *) and rework the internals to use this struct. ok inoguchi@ tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/bio/bss_mem.c105
1 files changed, 58 insertions, 47 deletions
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c
index 594351b92b..6100a1861e 100644
--- a/src/lib/libcrypto/bio/bss_mem.c
+++ b/src/lib/libcrypto/bio/bss_mem.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bss_mem.c,v 1.19 2022/02/18 17:30:13 jsing Exp $ */ 1/* $OpenBSD: bss_mem.c,v 1.20 2022/02/19 08:11:16 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -67,6 +67,10 @@
67 67
68#include "bio_local.h" 68#include "bio_local.h"
69 69
70struct bio_mem {
71 BUF_MEM *buf;
72};
73
70static int mem_new(BIO *bio); 74static int mem_new(BIO *bio);
71static int mem_free(BIO *bio); 75static int mem_free(BIO *bio);
72static int mem_write(BIO *bio, const char *in, int in_len); 76static int mem_write(BIO *bio, const char *in, int in_len);
@@ -101,8 +105,8 @@ BIO_s_mem(void)
101BIO * 105BIO *
102BIO_new_mem_buf(const void *buf, int buf_len) 106BIO_new_mem_buf(const void *buf, int buf_len)
103{ 107{
108 struct bio_mem *bm;
104 BIO *bio; 109 BIO *bio;
105 BUF_MEM *b;
106 110
107 if (buf == NULL) { 111 if (buf == NULL) {
108 BIOerror(BIO_R_NULL_PARAMETER); 112 BIOerror(BIO_R_NULL_PARAMETER);
@@ -118,10 +122,10 @@ BIO_new_mem_buf(const void *buf, int buf_len)
118 if ((bio = BIO_new(BIO_s_mem())) == NULL) 122 if ((bio = BIO_new(BIO_s_mem())) == NULL)
119 return NULL; 123 return NULL;
120 124
121 b = bio->ptr; 125 bm = bio->ptr;
122 b->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */ 126 bm->buf->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */
123 b->length = buf_len; 127 bm->buf->length = buf_len;
124 b->max = buf_len; 128 bm->buf->max = buf_len;
125 bio->flags |= BIO_FLAGS_MEM_RDONLY; 129 bio->flags |= BIO_FLAGS_MEM_RDONLY;
126 /* Since this is static data retrying will not help. */ 130 /* Since this is static data retrying will not help. */
127 bio->num = 0; 131 bio->num = 0;
@@ -132,15 +136,19 @@ BIO_new_mem_buf(const void *buf, int buf_len)
132static int 136static int
133mem_new(BIO *bio) 137mem_new(BIO *bio)
134{ 138{
135 BUF_MEM *b; 139 struct bio_mem *bm;
136 140
137 if ((b = BUF_MEM_new()) == NULL) 141 if ((bm = calloc(1, sizeof(*bm))) == NULL)
142 return 0;
143 if ((bm->buf = BUF_MEM_new()) == NULL) {
144 free(bm);
138 return 0; 145 return 0;
146 }
139 147
140 bio->shutdown = 1; 148 bio->shutdown = 1;
141 bio->init = 1; 149 bio->init = 1;
142 bio->num = -1; 150 bio->num = -1;
143 bio->ptr = b; 151 bio->ptr = bm;
144 152
145 return 1; 153 return 1;
146} 154}
@@ -148,17 +156,20 @@ mem_new(BIO *bio)
148static int 156static int
149mem_free(BIO *bio) 157mem_free(BIO *bio)
150{ 158{
151 BUF_MEM *b; 159 struct bio_mem *bm;
152 160
153 if (bio == NULL) 161 if (bio == NULL)
154 return 0; 162 return 0;
155 if (!bio->shutdown || !bio->init || bio->ptr == NULL) 163 if (!bio->init || bio->ptr == NULL)
156 return 1; 164 return 1;
157 165
158 b = bio->ptr; 166 bm = bio->ptr;
159 if (bio->flags & BIO_FLAGS_MEM_RDONLY) 167 if (bio->shutdown) {
160 b->data = NULL; 168 if (bio->flags & BIO_FLAGS_MEM_RDONLY)
161 BUF_MEM_free(b); 169 bm->buf->data = NULL;
170 BUF_MEM_free(bm->buf);
171 }
172 free(bm);
162 bio->ptr = NULL; 173 bio->ptr = NULL;
163 174
164 return 1; 175 return 1;
@@ -167,15 +178,15 @@ mem_free(BIO *bio)
167static int 178static int
168mem_read(BIO *bio, char *out, int out_len) 179mem_read(BIO *bio, char *out, int out_len)
169{ 180{
170 BUF_MEM *bm = bio->ptr; 181 struct bio_mem *bm = bio->ptr;
171 182
172 BIO_clear_retry_flags(bio); 183 BIO_clear_retry_flags(bio);
173 184
174 if (out == NULL || out_len <= 0) 185 if (out == NULL || out_len <= 0)
175 return 0; 186 return 0;
176 187
177 if ((size_t)out_len > bm->length) 188 if ((size_t)out_len > bm->buf->length)
178 out_len = bm->length; 189 out_len = bm->buf->length;
179 190
180 if (out_len == 0) { 191 if (out_len == 0) {
181 if (bio->num != 0) 192 if (bio->num != 0)
@@ -183,13 +194,13 @@ mem_read(BIO *bio, char *out, int out_len)
183 return bio->num; 194 return bio->num;
184 } 195 }
185 196
186 memcpy(out, bm->data, out_len); 197 memcpy(out, bm->buf->data, out_len);
187 bm->length -= out_len; 198 bm->buf->length -= out_len;
188 if (bio->flags & BIO_FLAGS_MEM_RDONLY) { 199 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
189 bm->data += out_len; 200 bm->buf->data += out_len;
190 } else { 201 } else {
191 memmove(&(bm->data[0]), &(bm->data[out_len]), 202 memmove(&(bm->buf->data[0]), &(bm->buf->data[out_len]),
192 bm->length); 203 bm->buf->length);
193 } 204 }
194 return out_len; 205 return out_len;
195} 206}
@@ -197,7 +208,7 @@ mem_read(BIO *bio, char *out, int out_len)
197static int 208static int
198mem_write(BIO *bio, const char *in, int in_len) 209mem_write(BIO *bio, const char *in, int in_len)
199{ 210{
200 BUF_MEM *bm = bio->ptr; 211 struct bio_mem *bm = bio->ptr;
201 size_t buf_len; 212 size_t buf_len;
202 213
203 BIO_clear_retry_flags(bio); 214 BIO_clear_retry_flags(bio);
@@ -214,14 +225,14 @@ mem_write(BIO *bio, const char *in, int in_len)
214 * Check for overflow and ensure we do not exceed an int, otherwise we 225 * Check for overflow and ensure we do not exceed an int, otherwise we
215 * cannot tell if BUF_MEM_grow_clean() succeeded. 226 * cannot tell if BUF_MEM_grow_clean() succeeded.
216 */ 227 */
217 buf_len = bm->length + in_len; 228 buf_len = bm->buf->length + in_len;
218 if (buf_len < bm->length || buf_len > INT_MAX) 229 if (buf_len < bm->buf->length || buf_len > INT_MAX)
219 return -1; 230 return -1;
220 231
221 if (BUF_MEM_grow_clean(bm, buf_len) != buf_len) 232 if (BUF_MEM_grow_clean(bm->buf, buf_len) != buf_len)
222 return -1; 233 return -1;
223 234
224 memcpy(&bm->data[buf_len - in_len], in, in_len); 235 memcpy(&bm->buf->data[buf_len - in_len], in, in_len);
225 236
226 return in_len; 237 return in_len;
227} 238}
@@ -229,45 +240,45 @@ mem_write(BIO *bio, const char *in, int in_len)
229static long 240static long
230mem_ctrl(BIO *bio, int cmd, long num, void *ptr) 241mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
231{ 242{
232 BUF_MEM *bm = bio->ptr; 243 struct bio_mem *bm = bio->ptr;
244 void **pptr;
233 long ret = 1; 245 long ret = 1;
234 char **pptr;
235 246
236 switch (cmd) { 247 switch (cmd) {
237 case BIO_CTRL_RESET: 248 case BIO_CTRL_RESET:
238 if (bm->data != NULL) { 249 if (bm->buf->data != NULL) {
239 /* For read only case reset to the start again */ 250 /* For read only case reset to the start again */
240 if (bio->flags & BIO_FLAGS_MEM_RDONLY) { 251 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
241 bm->data -= bm->max - bm->length; 252 bm->buf->data -= bm->buf->max - bm->buf->length;
242 bm->length = bm->max; 253 bm->buf->length = bm->buf->max;
243 } else { 254 } else {
244 memset(bm->data, 0, bm->max); 255 memset(bm->buf->data, 0, bm->buf->max);
245 bm->length = 0; 256 bm->buf->length = 0;
246 } 257 }
247 } 258 }
248 break; 259 break;
249 case BIO_CTRL_EOF: 260 case BIO_CTRL_EOF:
250 ret = (long)(bm->length == 0); 261 ret = (long)(bm->buf->length == 0);
251 break; 262 break;
252 case BIO_C_SET_BUF_MEM_EOF_RETURN: 263 case BIO_C_SET_BUF_MEM_EOF_RETURN:
253 bio->num = (int)num; 264 bio->num = (int)num;
254 break; 265 break;
255 case BIO_CTRL_INFO: 266 case BIO_CTRL_INFO:
256 if (ptr != NULL) { 267 if (ptr != NULL) {
257 pptr = (char **)ptr; 268 pptr = (void **)ptr;
258 *pptr = (char *)bm->data; 269 *pptr = bm->buf->data;
259 } 270 }
260 ret = (long)bm->length; 271 ret = (long)bm->buf->length;
261 break; 272 break;
262 case BIO_C_SET_BUF_MEM: 273 case BIO_C_SET_BUF_MEM:
263 mem_free(bio); 274 BUF_MEM_free(bm->buf);
264 bio->shutdown = (int)num; 275 bio->shutdown = (int)num;
265 bio->ptr = ptr; 276 bm->buf = ptr;
266 break; 277 break;
267 case BIO_C_GET_BUF_MEM_PTR: 278 case BIO_C_GET_BUF_MEM_PTR:
268 if (ptr != NULL) { 279 if (ptr != NULL) {
269 pptr = (char **)ptr; 280 pptr = (void **)ptr;
270 *pptr = (char *)bm; 281 *pptr = bm->buf;
271 } 282 }
272 break; 283 break;
273 case BIO_CTRL_GET_CLOSE: 284 case BIO_CTRL_GET_CLOSE:
@@ -280,7 +291,7 @@ mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
280 ret = 0L; 291 ret = 0L;
281 break; 292 break;
282 case BIO_CTRL_PENDING: 293 case BIO_CTRL_PENDING:
283 ret = (long)bm->length; 294 ret = (long)bm->buf->length;
284 break; 295 break;
285 case BIO_CTRL_DUP: 296 case BIO_CTRL_DUP:
286 case BIO_CTRL_FLUSH: 297 case BIO_CTRL_FLUSH:
@@ -298,14 +309,14 @@ mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
298static int 309static int
299mem_gets(BIO *bio, char *out, int out_len) 310mem_gets(BIO *bio, char *out, int out_len)
300{ 311{
301 BUF_MEM *bm = bio->ptr; 312 struct bio_mem *bm = bio->ptr;
302 int i, out_max; 313 int i, out_max;
303 char *p; 314 char *p;
304 int ret = -1; 315 int ret = -1;
305 316
306 BIO_clear_retry_flags(bio); 317 BIO_clear_retry_flags(bio);
307 318
308 out_max = bm->length; 319 out_max = bm->buf->length;
309 if (out_len - 1 < out_max) 320 if (out_len - 1 < out_max)
310 out_max = out_len - 1; 321 out_max = out_len - 1;
311 if (out_max <= 0) { 322 if (out_max <= 0) {
@@ -313,7 +324,7 @@ mem_gets(BIO *bio, char *out, int out_len)
313 return 0; 324 return 0;
314 } 325 }
315 326
316 p = bm->data; 327 p = bm->buf->data;
317 for (i = 0; i < out_max; i++) { 328 for (i = 0; i < out_max; i++) {
318 if (p[i] == '\n') { 329 if (p[i] == '\n') {
319 i++; 330 i++;