diff options
-rw-r--r-- | src/lib/libcrypto/bio/bf_lbuf.c | 377 |
1 files changed, 0 insertions, 377 deletions
diff --git a/src/lib/libcrypto/bio/bf_lbuf.c b/src/lib/libcrypto/bio/bf_lbuf.c deleted file mode 100644 index d65150881c..0000000000 --- a/src/lib/libcrypto/bio/bf_lbuf.c +++ /dev/null | |||
@@ -1,377 +0,0 @@ | |||
1 | /* $OpenBSD: bf_lbuf.c,v 1.15 2022/01/14 08:40:57 tb Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | #include <errno.h> | ||
60 | #include <stdio.h> | ||
61 | |||
62 | #include <openssl/bio.h> | ||
63 | #include <openssl/evp.h> | ||
64 | |||
65 | static int linebuffer_write(BIO *h, const char *buf, int num); | ||
66 | static int linebuffer_read(BIO *h, char *buf, int size); | ||
67 | static int linebuffer_puts(BIO *h, const char *str); | ||
68 | static int linebuffer_gets(BIO *h, char *str, int size); | ||
69 | static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
70 | static int linebuffer_new(BIO *h); | ||
71 | static int linebuffer_free(BIO *data); | ||
72 | static long linebuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); | ||
73 | |||
74 | /* A 10k maximum should be enough for most purposes */ | ||
75 | #define DEFAULT_LINEBUFFER_SIZE 1024*10 | ||
76 | |||
77 | /* #define DEBUG */ | ||
78 | |||
79 | static BIO_METHOD methods_linebuffer = { | ||
80 | .type = BIO_TYPE_LINEBUFFER, | ||
81 | .name = "linebuffer", | ||
82 | .bwrite = linebuffer_write, | ||
83 | .bread = linebuffer_read, | ||
84 | .bputs = linebuffer_puts, | ||
85 | .bgets = linebuffer_gets, | ||
86 | .ctrl = linebuffer_ctrl, | ||
87 | .create = linebuffer_new, | ||
88 | .destroy = linebuffer_free, | ||
89 | .callback_ctrl = linebuffer_callback_ctrl | ||
90 | }; | ||
91 | |||
92 | BIO_METHOD * | ||
93 | BIO_f_linebuffer(void) | ||
94 | { | ||
95 | return (&methods_linebuffer); | ||
96 | } | ||
97 | |||
98 | typedef struct bio_linebuffer_ctx_struct { | ||
99 | char *obuf; /* the output char array */ | ||
100 | int obuf_size; /* how big is the output buffer */ | ||
101 | int obuf_len; /* how many bytes are in it */ | ||
102 | } BIO_LINEBUFFER_CTX; | ||
103 | |||
104 | static int | ||
105 | linebuffer_new(BIO *bi) | ||
106 | { | ||
107 | BIO_LINEBUFFER_CTX *ctx; | ||
108 | |||
109 | ctx = malloc(sizeof(BIO_LINEBUFFER_CTX)); | ||
110 | if (ctx == NULL) | ||
111 | return (0); | ||
112 | ctx->obuf = malloc(DEFAULT_LINEBUFFER_SIZE); | ||
113 | if (ctx->obuf == NULL) { | ||
114 | free(ctx); | ||
115 | return (0); | ||
116 | } | ||
117 | ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE; | ||
118 | ctx->obuf_len = 0; | ||
119 | |||
120 | bi->init = 1; | ||
121 | bi->ptr = (char *)ctx; | ||
122 | bi->flags = 0; | ||
123 | return (1); | ||
124 | } | ||
125 | |||
126 | static int | ||
127 | linebuffer_free(BIO *a) | ||
128 | { | ||
129 | BIO_LINEBUFFER_CTX *b; | ||
130 | |||
131 | if (a == NULL) | ||
132 | return (0); | ||
133 | b = (BIO_LINEBUFFER_CTX *)a->ptr; | ||
134 | free(b->obuf); | ||
135 | free(a->ptr); | ||
136 | a->ptr = NULL; | ||
137 | a->init = 0; | ||
138 | a->flags = 0; | ||
139 | return (1); | ||
140 | } | ||
141 | |||
142 | static int | ||
143 | linebuffer_read(BIO *b, char *out, int outl) | ||
144 | { | ||
145 | int ret = 0; | ||
146 | |||
147 | if (out == NULL) | ||
148 | return (0); | ||
149 | if (b->next_bio == NULL) | ||
150 | return (0); | ||
151 | ret = BIO_read(b->next_bio, out, outl); | ||
152 | BIO_clear_retry_flags(b); | ||
153 | BIO_copy_next_retry(b); | ||
154 | return (ret); | ||
155 | } | ||
156 | |||
157 | static int | ||
158 | linebuffer_write(BIO *b, const char *in, int inl) | ||
159 | { | ||
160 | int i, num = 0, foundnl; | ||
161 | BIO_LINEBUFFER_CTX *ctx; | ||
162 | |||
163 | if ((in == NULL) || (inl <= 0)) | ||
164 | return (0); | ||
165 | ctx = (BIO_LINEBUFFER_CTX *)b->ptr; | ||
166 | if ((ctx == NULL) || (b->next_bio == NULL)) | ||
167 | return (0); | ||
168 | |||
169 | BIO_clear_retry_flags(b); | ||
170 | |||
171 | do { | ||
172 | const char *p; | ||
173 | |||
174 | for (p = in; p < in + inl && *p != '\n'; p++) | ||
175 | ; | ||
176 | if (*p == '\n') { | ||
177 | p++; | ||
178 | foundnl = 1; | ||
179 | } else | ||
180 | foundnl = 0; | ||
181 | |||
182 | /* If a NL was found and we already have text in the save | ||
183 | buffer, concatenate them and write */ | ||
184 | while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len) && | ||
185 | ctx->obuf_len > 0) { | ||
186 | int orig_olen = ctx->obuf_len; | ||
187 | |||
188 | i = ctx->obuf_size - ctx->obuf_len; | ||
189 | if (p - in > 0) { | ||
190 | if (i >= p - in) { | ||
191 | memcpy(&(ctx->obuf[ctx->obuf_len]), | ||
192 | in, p - in); | ||
193 | ctx->obuf_len += p - in; | ||
194 | inl -= p - in; | ||
195 | num += p - in; | ||
196 | in = p; | ||
197 | } else { | ||
198 | memcpy(&(ctx->obuf[ctx->obuf_len]), | ||
199 | in, i); | ||
200 | ctx->obuf_len += i; | ||
201 | inl -= i; | ||
202 | in += i; | ||
203 | num += i; | ||
204 | } | ||
205 | } | ||
206 | |||
207 | i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len); | ||
208 | if (i <= 0) { | ||
209 | ctx->obuf_len = orig_olen; | ||
210 | BIO_copy_next_retry(b); | ||
211 | if (i < 0) | ||
212 | return ((num > 0) ? num : i); | ||
213 | if (i == 0) | ||
214 | return (num); | ||
215 | } | ||
216 | if (i < ctx->obuf_len) | ||
217 | memmove(ctx->obuf, ctx->obuf + i, | ||
218 | ctx->obuf_len - i); | ||
219 | ctx->obuf_len -= i; | ||
220 | } | ||
221 | |||
222 | /* Now that the save buffer is emptied, let's write the input | ||
223 | buffer if a NL was found and there is anything to write. */ | ||
224 | if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) { | ||
225 | i = BIO_write(b->next_bio, in, p - in); | ||
226 | if (i <= 0) { | ||
227 | BIO_copy_next_retry(b); | ||
228 | if (i < 0) | ||
229 | return ((num > 0) ? num : i); | ||
230 | if (i == 0) | ||
231 | return (num); | ||
232 | } | ||
233 | num += i; | ||
234 | in += i; | ||
235 | inl -= i; | ||
236 | } | ||
237 | } while (foundnl && inl > 0); | ||
238 | /* We've written as much as we can. The rest of the input buffer, if | ||
239 | any, is text that doesn't and with a NL and therefore needs to be | ||
240 | saved for the next trip. */ | ||
241 | if (inl > 0) { | ||
242 | memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl); | ||
243 | ctx->obuf_len += inl; | ||
244 | num += inl; | ||
245 | } | ||
246 | return num; | ||
247 | } | ||
248 | |||
249 | static long | ||
250 | linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
251 | { | ||
252 | BIO *dbio; | ||
253 | BIO_LINEBUFFER_CTX *ctx; | ||
254 | long ret = 1; | ||
255 | char *p; | ||
256 | int r; | ||
257 | int obs; | ||
258 | |||
259 | ctx = (BIO_LINEBUFFER_CTX *)b->ptr; | ||
260 | |||
261 | switch (cmd) { | ||
262 | case BIO_CTRL_RESET: | ||
263 | ctx->obuf_len = 0; | ||
264 | if (b->next_bio == NULL) | ||
265 | return (0); | ||
266 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
267 | break; | ||
268 | case BIO_CTRL_INFO: | ||
269 | ret = (long)ctx->obuf_len; | ||
270 | break; | ||
271 | case BIO_CTRL_WPENDING: | ||
272 | ret = (long)ctx->obuf_len; | ||
273 | if (ret == 0) { | ||
274 | if (b->next_bio == NULL) | ||
275 | return (0); | ||
276 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
277 | } | ||
278 | break; | ||
279 | case BIO_C_SET_BUFF_SIZE: | ||
280 | obs = (int)num; | ||
281 | p = ctx->obuf; | ||
282 | if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) { | ||
283 | p = malloc(num); | ||
284 | if (p == NULL) | ||
285 | goto malloc_error; | ||
286 | } | ||
287 | if (ctx->obuf != p) { | ||
288 | if (ctx->obuf_len > obs) { | ||
289 | ctx->obuf_len = obs; | ||
290 | } | ||
291 | memcpy(p, ctx->obuf, ctx->obuf_len); | ||
292 | free(ctx->obuf); | ||
293 | ctx->obuf = p; | ||
294 | ctx->obuf_size = obs; | ||
295 | } | ||
296 | break; | ||
297 | case BIO_C_DO_STATE_MACHINE: | ||
298 | if (b->next_bio == NULL) | ||
299 | return (0); | ||
300 | BIO_clear_retry_flags(b); | ||
301 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
302 | BIO_copy_next_retry(b); | ||
303 | break; | ||
304 | |||
305 | case BIO_CTRL_FLUSH: | ||
306 | if (b->next_bio == NULL) | ||
307 | return (0); | ||
308 | if (ctx->obuf_len <= 0) { | ||
309 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
310 | break; | ||
311 | } | ||
312 | |||
313 | for (;;) { | ||
314 | BIO_clear_retry_flags(b); | ||
315 | if (ctx->obuf_len > 0) { | ||
316 | r = BIO_write(b->next_bio, | ||
317 | ctx->obuf, ctx->obuf_len); | ||
318 | BIO_copy_next_retry(b); | ||
319 | if (r <= 0) | ||
320 | return ((long)r); | ||
321 | if (r < ctx->obuf_len) | ||
322 | memmove(ctx->obuf, ctx->obuf + r, | ||
323 | ctx->obuf_len - r); | ||
324 | ctx->obuf_len -= r; | ||
325 | } else { | ||
326 | ctx->obuf_len = 0; | ||
327 | ret = 1; | ||
328 | break; | ||
329 | } | ||
330 | } | ||
331 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
332 | break; | ||
333 | case BIO_CTRL_DUP: | ||
334 | dbio = (BIO *)ptr; | ||
335 | if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size)) | ||
336 | ret = 0; | ||
337 | break; | ||
338 | default: | ||
339 | if (b->next_bio == NULL) | ||
340 | return (0); | ||
341 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
342 | break; | ||
343 | } | ||
344 | return (ret); | ||
345 | malloc_error: | ||
346 | BIOerror(ERR_R_MALLOC_FAILURE); | ||
347 | return (0); | ||
348 | } | ||
349 | |||
350 | static long | ||
351 | linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) | ||
352 | { | ||
353 | long ret = 1; | ||
354 | |||
355 | if (b->next_bio == NULL) | ||
356 | return (0); | ||
357 | switch (cmd) { | ||
358 | default: | ||
359 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); | ||
360 | break; | ||
361 | } | ||
362 | return (ret); | ||
363 | } | ||
364 | |||
365 | static int | ||
366 | linebuffer_gets(BIO *b, char *buf, int size) | ||
367 | { | ||
368 | if (b->next_bio == NULL) | ||
369 | return (0); | ||
370 | return (BIO_gets(b->next_bio, buf, size)); | ||
371 | } | ||
372 | |||
373 | static int | ||
374 | linebuffer_puts(BIO *b, const char *str) | ||
375 | { | ||
376 | return (linebuffer_write(b, str, strlen(str))); | ||
377 | } | ||