/src/openssl/crypto/bio/bf_readbuff.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021-2024 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 | | /* |
11 | | * This is a read only BIO filter that can be used to add BIO_tell() and |
12 | | * BIO_seek() support to source/sink BIO's (such as a file BIO that uses stdin). |
13 | | * It does this by caching ALL data read from the BIO source/sink into a |
14 | | * resizable memory buffer. |
15 | | */ |
16 | | |
17 | | #include <stdio.h> |
18 | | #include <errno.h> |
19 | | #include "bio_local.h" |
20 | | #include "internal/cryptlib.h" |
21 | | |
22 | 0 | #define DEFAULT_BUFFER_SIZE 4096 |
23 | | |
24 | | static int readbuffer_write(BIO *h, const char *buf, int num); |
25 | | static int readbuffer_read(BIO *h, char *buf, int size); |
26 | | static int readbuffer_puts(BIO *h, const char *str); |
27 | | static int readbuffer_gets(BIO *h, char *str, int size); |
28 | | static long readbuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
29 | | static int readbuffer_new(BIO *h); |
30 | | static int readbuffer_free(BIO *data); |
31 | | static long readbuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); |
32 | | |
33 | | static const BIO_METHOD methods_readbuffer = { |
34 | | BIO_TYPE_BUFFER, |
35 | | "readbuffer", |
36 | | bwrite_conv, |
37 | | readbuffer_write, |
38 | | bread_conv, |
39 | | readbuffer_read, |
40 | | readbuffer_puts, |
41 | | readbuffer_gets, |
42 | | readbuffer_ctrl, |
43 | | readbuffer_new, |
44 | | readbuffer_free, |
45 | | readbuffer_callback_ctrl, |
46 | | }; |
47 | | |
48 | | const BIO_METHOD *BIO_f_readbuffer(void) |
49 | 0 | { |
50 | 0 | return &methods_readbuffer; |
51 | 0 | } |
52 | | |
53 | | static int readbuffer_new(BIO *bi) |
54 | 0 | { |
55 | 0 | BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
56 | |
|
57 | 0 | if (ctx == NULL) |
58 | 0 | return 0; |
59 | 0 | ctx->ibuf_size = DEFAULT_BUFFER_SIZE; |
60 | 0 | ctx->ibuf = OPENSSL_zalloc(DEFAULT_BUFFER_SIZE); |
61 | 0 | if (ctx->ibuf == NULL) { |
62 | 0 | OPENSSL_free(ctx); |
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | 0 | bi->init = 1; |
67 | 0 | bi->ptr = (char *)ctx; |
68 | 0 | bi->flags = 0; |
69 | 0 | return 1; |
70 | 0 | } |
71 | | |
72 | | static int readbuffer_free(BIO *a) |
73 | 0 | { |
74 | 0 | BIO_F_BUFFER_CTX *b; |
75 | |
|
76 | 0 | if (a == NULL) |
77 | 0 | return 0; |
78 | 0 | b = (BIO_F_BUFFER_CTX *)a->ptr; |
79 | 0 | OPENSSL_free(b->ibuf); |
80 | 0 | OPENSSL_free(a->ptr); |
81 | 0 | a->ptr = NULL; |
82 | 0 | a->init = 0; |
83 | 0 | a->flags = 0; |
84 | 0 | return 1; |
85 | 0 | } |
86 | | |
87 | | static int readbuffer_resize(BIO_F_BUFFER_CTX *ctx, int sz) |
88 | 0 | { |
89 | 0 | char *tmp; |
90 | 0 | size_t newsize; |
91 | | |
92 | | /* |
93 | | * Do the arithmetic in size_t and fail if the required size does |
94 | | * not fit in the int size/offset fields: ibuf_off only grows as |
95 | | * data is cached, so the int calculation overflowed near INT_MAX. |
96 | | */ |
97 | 0 | newsize = (size_t)ctx->ibuf_off + (size_t)sz; |
98 | 0 | if (newsize > (size_t)(INT_MAX - (DEFAULT_BUFFER_SIZE - 1))) |
99 | 0 | return 0; |
100 | 0 | newsize += DEFAULT_BUFFER_SIZE - 1; |
101 | 0 | newsize = DEFAULT_BUFFER_SIZE * (newsize / DEFAULT_BUFFER_SIZE); |
102 | | |
103 | | /* Resize if the buffer is not big enough */ |
104 | 0 | if (newsize > (size_t)ctx->ibuf_size) { |
105 | 0 | tmp = OPENSSL_realloc(ctx->ibuf, newsize); |
106 | 0 | if (tmp == NULL) |
107 | 0 | return 0; |
108 | 0 | ctx->ibuf = tmp; |
109 | 0 | ctx->ibuf_size = (int)newsize; |
110 | 0 | } |
111 | 0 | return 1; |
112 | 0 | } |
113 | | |
114 | | static int readbuffer_read(BIO *b, char *out, int outl) |
115 | 0 | { |
116 | 0 | int i, num = 0; |
117 | 0 | BIO_F_BUFFER_CTX *ctx; |
118 | |
|
119 | 0 | if (out == NULL || outl == 0) |
120 | 0 | return 0; |
121 | 0 | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
122 | |
|
123 | 0 | if ((ctx == NULL) || (b->next_bio == NULL)) |
124 | 0 | return 0; |
125 | 0 | BIO_clear_retry_flags(b); |
126 | |
|
127 | 0 | for (;;) { |
128 | 0 | i = ctx->ibuf_len; |
129 | | /* If there is something in the buffer just read it. */ |
130 | 0 | if (i != 0) { |
131 | 0 | if (i > outl) |
132 | 0 | i = outl; |
133 | 0 | memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i); |
134 | 0 | ctx->ibuf_off += i; |
135 | 0 | ctx->ibuf_len -= i; |
136 | 0 | num += i; |
137 | | /* Exit if we have read the bytes required out of the buffer */ |
138 | 0 | if (outl == i) |
139 | 0 | return num; |
140 | 0 | outl -= i; |
141 | 0 | out += i; |
142 | 0 | } |
143 | | |
144 | | /* Only gets here if the buffer has been consumed */ |
145 | 0 | if (!readbuffer_resize(ctx, outl)) |
146 | 0 | return 0; |
147 | | |
148 | | /* Do some buffering by reading from the next bio */ |
149 | 0 | i = BIO_read(b->next_bio, ctx->ibuf + ctx->ibuf_off, outl); |
150 | 0 | if (i <= 0) { |
151 | 0 | BIO_copy_next_retry(b); |
152 | 0 | if (i < 0) |
153 | 0 | return ((num > 0) ? num : i); |
154 | 0 | else |
155 | 0 | return num; /* i == 0 */ |
156 | 0 | } |
157 | 0 | ctx->ibuf_len = i; |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | | static int readbuffer_write(BIO *b, const char *in, int inl) |
162 | 0 | { |
163 | 0 | return 0; |
164 | 0 | } |
165 | | static int readbuffer_puts(BIO *b, const char *str) |
166 | 0 | { |
167 | 0 | return 0; |
168 | 0 | } |
169 | | |
170 | | static long readbuffer_ctrl(BIO *b, int cmd, long num, void *ptr) |
171 | 0 | { |
172 | 0 | BIO_F_BUFFER_CTX *ctx; |
173 | 0 | long ret = 1, sz; |
174 | |
|
175 | 0 | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
176 | |
|
177 | 0 | switch (cmd) { |
178 | 0 | case BIO_CTRL_EOF: |
179 | 0 | if (ctx->ibuf_len > 0) |
180 | 0 | return 0; |
181 | 0 | if (b->next_bio == NULL) |
182 | 0 | return 1; |
183 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
184 | 0 | break; |
185 | | |
186 | 0 | case BIO_C_FILE_SEEK: |
187 | 0 | case BIO_CTRL_RESET: |
188 | 0 | sz = ctx->ibuf_off + ctx->ibuf_len; |
189 | | /* Assume it can only seek backwards */ |
190 | 0 | if (num < 0 || num > sz) |
191 | 0 | return 0; |
192 | 0 | ctx->ibuf_off = num; |
193 | 0 | ctx->ibuf_len = sz - num; |
194 | 0 | break; |
195 | | |
196 | 0 | case BIO_C_FILE_TELL: |
197 | 0 | case BIO_CTRL_INFO: |
198 | 0 | ret = (long)ctx->ibuf_off; |
199 | 0 | break; |
200 | 0 | case BIO_CTRL_PENDING: |
201 | 0 | ret = (long)ctx->ibuf_len; |
202 | 0 | if (ret == 0) { |
203 | 0 | if (b->next_bio == NULL) |
204 | 0 | return 0; |
205 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
206 | 0 | } |
207 | 0 | break; |
208 | 0 | case BIO_CTRL_DUP: |
209 | 0 | case BIO_CTRL_FLUSH: |
210 | 0 | ret = 1; |
211 | 0 | break; |
212 | 0 | default: |
213 | 0 | ret = 0; |
214 | 0 | break; |
215 | 0 | } |
216 | 0 | return ret; |
217 | 0 | } |
218 | | |
219 | | static long readbuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
220 | 0 | { |
221 | 0 | if (b->next_bio == NULL) |
222 | 0 | return 0; |
223 | 0 | return BIO_callback_ctrl(b->next_bio, cmd, fp); |
224 | 0 | } |
225 | | |
226 | | static int readbuffer_gets(BIO *b, char *buf, int size) |
227 | 0 | { |
228 | 0 | BIO_F_BUFFER_CTX *ctx; |
229 | 0 | int num = 0, num_chars, found_newline; |
230 | 0 | char *p; |
231 | 0 | int i, j; |
232 | |
|
233 | 0 | if (buf == NULL || size == 0) |
234 | 0 | return 0; |
235 | 0 | --size; /* the passed in size includes the terminator - so remove it here */ |
236 | 0 | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
237 | |
|
238 | 0 | if (ctx == NULL || b->next_bio == NULL) |
239 | 0 | return 0; |
240 | 0 | BIO_clear_retry_flags(b); |
241 | | |
242 | | /* If data is already buffered then use this first */ |
243 | 0 | if (ctx->ibuf_len > 0) { |
244 | 0 | p = ctx->ibuf + ctx->ibuf_off; |
245 | 0 | found_newline = 0; |
246 | 0 | for (num_chars = 0; |
247 | 0 | (num_chars < ctx->ibuf_len) && (num_chars < size); |
248 | 0 | num_chars++) { |
249 | 0 | *buf++ = p[num_chars]; |
250 | 0 | if (p[num_chars] == '\n') { |
251 | 0 | found_newline = 1; |
252 | 0 | num_chars++; |
253 | 0 | break; |
254 | 0 | } |
255 | 0 | } |
256 | 0 | num += num_chars; |
257 | 0 | size -= num_chars; |
258 | 0 | ctx->ibuf_len -= num_chars; |
259 | 0 | ctx->ibuf_off += num_chars; |
260 | 0 | if (found_newline || size == 0) { |
261 | 0 | *buf = '\0'; |
262 | 0 | return num; |
263 | 0 | } |
264 | 0 | } |
265 | | /* |
266 | | * If there is no buffered data left then read any remaining data from the |
267 | | * next bio. |
268 | | */ |
269 | | |
270 | | /* Resize if we have to */ |
271 | 0 | if (!readbuffer_resize(ctx, 1 + size)) |
272 | 0 | return 0; |
273 | | /* |
274 | | * Read more data from the next bio using BIO_read_ex: |
275 | | * Note we cannot use BIO_gets() here as it does not work on a |
276 | | * binary stream that contains 0x00. (Since strlen() will stop at |
277 | | * any 0x00 not at the last read '\n' in a FILE bio). |
278 | | * Also note that some applications open and close the file bio |
279 | | * multiple times and need to read the next available block when using |
280 | | * stdin - so we need to READ one byte at a time! |
281 | | */ |
282 | 0 | p = ctx->ibuf + ctx->ibuf_off; |
283 | 0 | for (i = 0; i < size; ++i) { |
284 | 0 | j = BIO_read(b->next_bio, p, 1); |
285 | 0 | if (j <= 0) { |
286 | 0 | BIO_copy_next_retry(b); |
287 | 0 | *buf = '\0'; |
288 | 0 | return num > 0 ? num : j; |
289 | 0 | } |
290 | 0 | *buf++ = *p; |
291 | 0 | num++; |
292 | 0 | ctx->ibuf_off++; |
293 | 0 | if (*p == '\n') |
294 | 0 | break; |
295 | 0 | ++p; |
296 | 0 | } |
297 | 0 | *buf = '\0'; |
298 | 0 | return num; |
299 | 0 | } |