/src/openssl/crypto/evp/bio_b64.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-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 | | #include <stdio.h> |
11 | | #include <errno.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/buffer.h> |
14 | | #include <openssl/evp.h> |
15 | | #include "internal/bio.h" |
16 | | |
17 | | static int b64_write(BIO *h, const char *buf, int num); |
18 | | static int b64_read(BIO *h, char *buf, int size); |
19 | | static int b64_puts(BIO *h, const char *str); |
20 | | static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
21 | | static int b64_new(BIO *h); |
22 | | static int b64_free(BIO *data); |
23 | | static long b64_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); |
24 | 117k | #define B64_BLOCK_SIZE 1024 |
25 | | #define B64_BLOCK_SIZE2 768 |
26 | 5.64k | #define B64_NONE 0 |
27 | 2.69k | #define B64_ENCODE 1 |
28 | 2.31M | #define B64_DECODE 2 |
29 | | |
30 | | typedef struct b64_struct { |
31 | | /* |
32 | | * BIO *bio; moved to the BIO structure |
33 | | */ |
34 | | int buf_len; |
35 | | int buf_off; |
36 | | int tmp_len; /* used to find the start when decoding */ |
37 | | int tmp_nl; /* If true, scan until '\n' */ |
38 | | int encode; |
39 | | int start; /* have we started decoding yet? */ |
40 | | int cont; /* <= 0 when finished */ |
41 | | EVP_ENCODE_CTX *base64; |
42 | | unsigned char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10]; |
43 | | unsigned char tmp[B64_BLOCK_SIZE]; |
44 | | } BIO_B64_CTX; |
45 | | |
46 | | static const BIO_METHOD methods_b64 = { |
47 | | BIO_TYPE_BASE64, |
48 | | "base64 encoding", |
49 | | bwrite_conv, |
50 | | b64_write, |
51 | | bread_conv, |
52 | | b64_read, |
53 | | b64_puts, |
54 | | NULL, /* b64_gets, */ |
55 | | b64_ctrl, |
56 | | b64_new, |
57 | | b64_free, |
58 | | b64_callback_ctrl, |
59 | | }; |
60 | | |
61 | | const BIO_METHOD *BIO_f_base64(void) |
62 | 12.5k | { |
63 | 12.5k | return &methods_b64; |
64 | 12.5k | } |
65 | | |
66 | | static int b64_new(BIO *bi) |
67 | 12.5k | { |
68 | 12.5k | BIO_B64_CTX *ctx; |
69 | | |
70 | 12.5k | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) |
71 | 0 | return 0; |
72 | | |
73 | 12.5k | ctx->cont = 1; |
74 | 12.5k | ctx->start = 1; |
75 | 12.5k | ctx->base64 = EVP_ENCODE_CTX_new(); |
76 | 12.5k | if (ctx->base64 == NULL) { |
77 | 0 | OPENSSL_free(ctx); |
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | 12.5k | BIO_set_data(bi, ctx); |
82 | 12.5k | BIO_set_init(bi, 1); |
83 | | |
84 | 12.5k | return 1; |
85 | 12.5k | } |
86 | | |
87 | | static int b64_free(BIO *a) |
88 | 12.5k | { |
89 | 12.5k | BIO_B64_CTX *ctx; |
90 | | |
91 | 12.5k | if (a == NULL) |
92 | 0 | return 0; |
93 | | |
94 | 12.5k | ctx = BIO_get_data(a); |
95 | 12.5k | if (ctx == NULL) |
96 | 0 | return 0; |
97 | | |
98 | 12.5k | EVP_ENCODE_CTX_free(ctx->base64); |
99 | 12.5k | OPENSSL_free(ctx); |
100 | 12.5k | BIO_set_data(a, NULL); |
101 | 12.5k | BIO_set_init(a, 0); |
102 | | |
103 | 12.5k | return 1; |
104 | 12.5k | } |
105 | | |
106 | | /* |
107 | | * Unless `BIO_FLAGS_BASE64_NO_NL` is set, this BIO ignores leading lines that |
108 | | * aren't exclusively composed of valid Base64 characters (followed by <CRLF> |
109 | | * or <LF>). Once a valid Base64 line is found, `ctx->start` is set to 0 and |
110 | | * lines are processed until EOF or the first line that contains invalid Base64 |
111 | | * characters. In a nod to PEM, lines that start with a '-' (hyphen) are |
112 | | * treated as a soft EOF, rather than an error. |
113 | | */ |
114 | | static int b64_read(BIO *b, char *out, int outl) |
115 | 2.31M | { |
116 | 2.31M | int ret = 0, i, ii, j, k, x, n, num, ret_code; |
117 | 2.31M | BIO_B64_CTX *ctx; |
118 | 2.31M | unsigned char *p, *q; |
119 | 2.31M | BIO *next; |
120 | | |
121 | 2.31M | if (out == NULL) |
122 | 0 | return 0; |
123 | 2.31M | ctx = (BIO_B64_CTX *)BIO_get_data(b); |
124 | | |
125 | 2.31M | next = BIO_next(b); |
126 | 2.31M | if (ctx == NULL || next == NULL) |
127 | 0 | return 0; |
128 | | |
129 | 2.31M | BIO_clear_retry_flags(b); |
130 | | |
131 | 2.31M | if (ctx->encode != B64_DECODE) { |
132 | 2.75k | ctx->encode = B64_DECODE; |
133 | 2.75k | ctx->buf_len = 0; |
134 | 2.75k | ctx->buf_off = 0; |
135 | 2.75k | ctx->tmp_len = 0; |
136 | 2.75k | EVP_DecodeInit(ctx->base64); |
137 | 2.75k | } |
138 | | |
139 | | /* First check if there are buffered bytes already decoded */ |
140 | 2.31M | if (ctx->buf_len > 0) { |
141 | 2.30M | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
142 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
143 | 0 | return -1; |
144 | 0 | } |
145 | 2.30M | i = ctx->buf_len - ctx->buf_off; |
146 | 2.30M | if (i > outl) |
147 | 2.28M | i = outl; |
148 | 2.30M | if (!ossl_assert(ctx->buf_off + i < (int)sizeof(ctx->buf))) { |
149 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
150 | 0 | return -1; |
151 | 0 | } |
152 | 2.30M | memcpy(out, &(ctx->buf[ctx->buf_off]), i); |
153 | 2.30M | ret = i; |
154 | 2.30M | out += i; |
155 | 2.30M | outl -= i; |
156 | 2.30M | ctx->buf_off += i; |
157 | 2.30M | if (ctx->buf_len == ctx->buf_off) { |
158 | 22.3k | ctx->buf_len = 0; |
159 | 22.3k | ctx->buf_off = 0; |
160 | 22.3k | } |
161 | 2.30M | } |
162 | | |
163 | | /* Restore any non-retriable error condition (ctx->cont < 0) */ |
164 | 2.31M | ret_code = ctx->cont < 0 ? ctx->cont : 0; |
165 | | |
166 | | /* |
167 | | * At this point, we have room of outl bytes and an either an empty buffer, |
168 | | * or outl == 0, so we'll attempt to read in some more. |
169 | | */ |
170 | 2.35M | while (outl > 0) { |
171 | 43.5k | int again = ctx->cont; |
172 | | |
173 | 43.5k | if (again <= 0) |
174 | 934 | break; |
175 | | |
176 | 42.6k | i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]), |
177 | 42.6k | B64_BLOCK_SIZE - ctx->tmp_len); |
178 | | |
179 | 42.6k | if (i <= 0) { |
180 | 1.91k | ret_code = i; |
181 | | |
182 | | /* Should we continue next time we are called? */ |
183 | 1.91k | if (!BIO_should_retry(next)) { |
184 | | /* Incomplete final Base64 chunk in the decoder is an error */ |
185 | 1.91k | if (ctx->tmp_len == 0) { |
186 | 786 | if (EVP_DecodeFinal(ctx->base64, NULL, &num) < 0) |
187 | 151 | ret_code = -1; |
188 | 786 | EVP_DecodeInit(ctx->base64); |
189 | 786 | } |
190 | 1.91k | ctx->cont = ret_code; |
191 | 1.91k | } |
192 | 1.91k | if (ctx->tmp_len == 0) |
193 | 786 | break; |
194 | | /* Fall through and process what we have */ |
195 | 1.13k | i = 0; |
196 | | /* But don't loop to top-up even if the buffer is not full! */ |
197 | 1.13k | again = 0; |
198 | 1.13k | } |
199 | | |
200 | 41.8k | i += ctx->tmp_len; |
201 | 41.8k | ctx->tmp_len = i; |
202 | | |
203 | | /* |
204 | | * We need to scan, a line at a time until we have a valid line if we |
205 | | * are starting. |
206 | | */ |
207 | 41.8k | if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) { |
208 | 0 | ctx->tmp_len = 0; |
209 | 41.8k | } else if (ctx->start) { |
210 | 4.74k | q = p = ctx->tmp; |
211 | 4.74k | num = 0; |
212 | 2.13M | for (j = 0; j < i; j++) { |
213 | 2.13M | if (*(q++) != '\n') |
214 | 2.03M | continue; |
215 | | |
216 | | /* |
217 | | * due to a previous very long line, we need to keep on |
218 | | * scanning for a '\n' before we even start looking for |
219 | | * base64 encoded stuff. |
220 | | */ |
221 | 98.2k | if (ctx->tmp_nl) { |
222 | 621 | p = q; |
223 | 621 | ctx->tmp_nl = 0; |
224 | 621 | continue; |
225 | 621 | } |
226 | | |
227 | 97.5k | k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, (int)(q - p)); |
228 | 97.5k | EVP_DecodeInit(ctx->base64); |
229 | 97.5k | if (k <= 0 && num == 0) { |
230 | 94.9k | p = q; |
231 | 94.9k | continue; |
232 | 94.9k | } |
233 | | |
234 | 2.62k | ctx->start = 0; |
235 | 2.62k | if (p != ctx->tmp) { |
236 | 112 | i -= (int)(p - ctx->tmp); |
237 | 53.1k | for (x = 0; x < i; x++) |
238 | 52.9k | ctx->tmp[x] = p[x]; |
239 | 112 | } |
240 | 2.62k | break; |
241 | 97.5k | } |
242 | | |
243 | | /* we fell off the end without starting */ |
244 | 4.74k | if (ctx->start) { |
245 | | /* |
246 | | * Is this is one long chunk?, if so, keep on reading until a |
247 | | * new line. |
248 | | */ |
249 | 2.12k | if (p == ctx->tmp) { |
250 | | /* Check buffer full */ |
251 | 930 | if (i == B64_BLOCK_SIZE) { |
252 | 825 | ctx->tmp_nl = 1; |
253 | 825 | ctx->tmp_len = 0; |
254 | 825 | } |
255 | 1.19k | } else if (p != q) { |
256 | | /* Retain partial line at end of buffer */ |
257 | 946 | n = (int)(q - p); |
258 | 773k | for (ii = 0; ii < n; ii++) |
259 | 772k | ctx->tmp[ii] = p[ii]; |
260 | 946 | ctx->tmp_len = n; |
261 | 946 | } else { |
262 | | /* All we have is newline terminated non-start data */ |
263 | 247 | ctx->tmp_len = 0; |
264 | 247 | } |
265 | | /* |
266 | | * Try to read more if possible, otherwise we can't make |
267 | | * progress unless the underlying BIO is retriable and may |
268 | | * produce more data next time we're called. |
269 | | */ |
270 | 2.12k | if (again > 0) |
271 | 2.05k | continue; |
272 | 64 | else |
273 | 64 | break; |
274 | 2.62k | } else { |
275 | 2.62k | ctx->tmp_len = 0; |
276 | 2.62k | } |
277 | 37.0k | } else if (i < B64_BLOCK_SIZE && again > 0) { |
278 | | /* |
279 | | * If buffer isn't full and we can retry then restart to read in |
280 | | * more data. |
281 | | */ |
282 | 1.06k | continue; |
283 | 1.06k | } |
284 | | |
285 | 38.6k | i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len, |
286 | 38.6k | ctx->tmp, i); |
287 | 38.6k | ctx->tmp_len = 0; |
288 | | /* |
289 | | * If eof or an error was signalled, then the condition |
290 | | * 'ctx->cont <= 0' will prevent b64_read() from reading |
291 | | * more data on subsequent calls. This assignment was |
292 | | * deleted accidentally in commit 5562cfaca4f3. |
293 | | */ |
294 | 38.6k | ctx->cont = i; |
295 | | |
296 | 38.6k | ctx->buf_off = 0; |
297 | 38.6k | if (i < 0) { |
298 | 87 | ret_code = ctx->start ? 0 : i; |
299 | 87 | ctx->buf_len = 0; |
300 | 87 | break; |
301 | 87 | } |
302 | | |
303 | 38.5k | if (ctx->buf_len <= outl) |
304 | 14.9k | i = ctx->buf_len; |
305 | 23.6k | else |
306 | 23.6k | i = outl; |
307 | | |
308 | 38.5k | memcpy(out, ctx->buf, i); |
309 | 38.5k | ret += i; |
310 | 38.5k | ctx->buf_off = i; |
311 | 38.5k | if (ctx->buf_off == ctx->buf_len) { |
312 | 14.9k | ctx->buf_len = 0; |
313 | 14.9k | ctx->buf_off = 0; |
314 | 14.9k | } |
315 | 38.5k | outl -= i; |
316 | 38.5k | out += i; |
317 | 38.5k | } |
318 | | /* BIO_clear_retry_flags(b); */ |
319 | 2.31M | BIO_copy_next_retry(b); |
320 | 2.31M | return ret == 0 ? ret_code : ret; |
321 | 2.31M | } |
322 | | |
323 | | static int b64_write(BIO *b, const char *in, int inl) |
324 | 1.34k | { |
325 | 1.34k | int ret = 0; |
326 | 1.34k | int n; |
327 | 1.34k | int i; |
328 | 1.34k | BIO_B64_CTX *ctx; |
329 | 1.34k | BIO *next; |
330 | | |
331 | 1.34k | ctx = (BIO_B64_CTX *)BIO_get_data(b); |
332 | 1.34k | next = BIO_next(b); |
333 | 1.34k | if (ctx == NULL || next == NULL) |
334 | 0 | return 0; |
335 | | |
336 | 1.34k | BIO_clear_retry_flags(b); |
337 | | |
338 | 1.34k | if (ctx->encode != B64_ENCODE) { |
339 | 1.34k | ctx->encode = B64_ENCODE; |
340 | 1.34k | ctx->buf_len = 0; |
341 | 1.34k | ctx->buf_off = 0; |
342 | 1.34k | ctx->tmp_len = 0; |
343 | 1.34k | EVP_EncodeInit(ctx->base64); |
344 | 1.34k | } |
345 | 1.34k | if (!ossl_assert(ctx->buf_off < (int)sizeof(ctx->buf))) { |
346 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
347 | 0 | return -1; |
348 | 0 | } |
349 | 1.34k | if (!ossl_assert(ctx->buf_len <= (int)sizeof(ctx->buf))) { |
350 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
351 | 0 | return -1; |
352 | 0 | } |
353 | 1.34k | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
354 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
355 | 0 | return -1; |
356 | 0 | } |
357 | 1.34k | n = ctx->buf_len - ctx->buf_off; |
358 | 1.34k | while (n > 0) { |
359 | 0 | i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n); |
360 | 0 | if (i <= 0) { |
361 | 0 | BIO_copy_next_retry(b); |
362 | 0 | return i; |
363 | 0 | } |
364 | 0 | ctx->buf_off += i; |
365 | 0 | if (!ossl_assert(ctx->buf_off <= (int)sizeof(ctx->buf))) { |
366 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
367 | 0 | return -1; |
368 | 0 | } |
369 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
370 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
371 | 0 | return -1; |
372 | 0 | } |
373 | 0 | n -= i; |
374 | 0 | } |
375 | | /* at this point all pending data has been written */ |
376 | 1.34k | ctx->buf_off = 0; |
377 | 1.34k | ctx->buf_len = 0; |
378 | | |
379 | 1.34k | if (in == NULL || inl <= 0) |
380 | 1.34k | return 0; |
381 | | |
382 | 0 | while (inl > 0) { |
383 | 0 | n = inl > B64_BLOCK_SIZE ? B64_BLOCK_SIZE : inl; |
384 | |
|
385 | 0 | if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) { |
386 | 0 | if (ctx->tmp_len > 0) { |
387 | 0 | if (!ossl_assert(ctx->tmp_len <= 3)) { |
388 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
389 | 0 | return ret == 0 ? -1 : ret; |
390 | 0 | } |
391 | 0 | n = 3 - ctx->tmp_len; |
392 | | /* |
393 | | * There's a theoretical possibility for this |
394 | | */ |
395 | 0 | if (n > inl) |
396 | 0 | n = inl; |
397 | 0 | memcpy(&(ctx->tmp[ctx->tmp_len]), in, n); |
398 | 0 | ctx->tmp_len += n; |
399 | 0 | ret += n; |
400 | 0 | if (ctx->tmp_len < 3) |
401 | 0 | break; |
402 | 0 | ctx->buf_len = |
403 | 0 | EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len); |
404 | 0 | if (!ossl_assert(ctx->buf_len <= (int)sizeof(ctx->buf))) { |
405 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
406 | 0 | return ret == 0 ? -1 : ret; |
407 | 0 | } |
408 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
409 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
410 | 0 | return ret == 0 ? -1 : ret; |
411 | 0 | } |
412 | | /* |
413 | | * Since we're now done using the temporary buffer, the |
414 | | * length should be 0'd |
415 | | */ |
416 | 0 | ctx->tmp_len = 0; |
417 | 0 | } else { |
418 | 0 | if (n < 3) { |
419 | 0 | memcpy(ctx->tmp, in, n); |
420 | 0 | ctx->tmp_len = n; |
421 | 0 | ret += n; |
422 | 0 | break; |
423 | 0 | } |
424 | 0 | n -= n % 3; |
425 | 0 | ctx->buf_len = |
426 | 0 | EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n); |
427 | 0 | if (!ossl_assert(ctx->buf_len <= (int)sizeof(ctx->buf))) { |
428 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
429 | 0 | return ret == 0 ? -1 : ret; |
430 | 0 | } |
431 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
432 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
433 | 0 | return ret == 0 ? -1 : ret; |
434 | 0 | } |
435 | 0 | ret += n; |
436 | 0 | } |
437 | 0 | } else { |
438 | 0 | if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len, |
439 | 0 | (unsigned char *)in, n)) |
440 | 0 | return ret == 0 ? -1 : ret; |
441 | 0 | if (!ossl_assert(ctx->buf_len <= (int)sizeof(ctx->buf))) { |
442 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
443 | 0 | return ret == 0 ? -1 : ret; |
444 | 0 | } |
445 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
446 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
447 | 0 | return ret == 0 ? -1 : ret; |
448 | 0 | } |
449 | 0 | ret += n; |
450 | 0 | } |
451 | 0 | inl -= n; |
452 | 0 | in += n; |
453 | |
|
454 | 0 | ctx->buf_off = 0; |
455 | 0 | n = ctx->buf_len; |
456 | 0 | while (n > 0) { |
457 | 0 | i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n); |
458 | 0 | if (i <= 0) { |
459 | 0 | BIO_copy_next_retry(b); |
460 | 0 | return ret == 0 ? i : ret; |
461 | 0 | } |
462 | 0 | n -= i; |
463 | 0 | ctx->buf_off += i; |
464 | 0 | if (!ossl_assert(ctx->buf_off <= (int)sizeof(ctx->buf))) { |
465 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
466 | 0 | return ret == 0 ? -1 : ret; |
467 | 0 | } |
468 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
469 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
470 | 0 | return ret == 0 ? -1 : ret; |
471 | 0 | } |
472 | 0 | } |
473 | 0 | ctx->buf_len = 0; |
474 | 0 | ctx->buf_off = 0; |
475 | 0 | } |
476 | 0 | return ret; |
477 | 0 | } |
478 | | |
479 | | static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) |
480 | 8.25k | { |
481 | 8.25k | BIO_B64_CTX *ctx; |
482 | 8.25k | long ret = 1; |
483 | 8.25k | int i; |
484 | 8.25k | BIO *next; |
485 | | |
486 | 8.25k | ctx = (BIO_B64_CTX *)BIO_get_data(b); |
487 | 8.25k | next = BIO_next(b); |
488 | 8.25k | if (ctx == NULL || next == NULL) |
489 | 0 | return 0; |
490 | | |
491 | 8.25k | switch (cmd) { |
492 | 0 | case BIO_CTRL_RESET: |
493 | 0 | ctx->cont = 1; |
494 | 0 | ctx->start = 1; |
495 | 0 | ctx->encode = B64_NONE; |
496 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
497 | 0 | break; |
498 | 0 | case BIO_CTRL_EOF: /* More to read */ |
499 | 0 | if (ctx->cont <= 0) |
500 | 0 | ret = 1; |
501 | 0 | else |
502 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
503 | 0 | break; |
504 | 0 | case BIO_CTRL_WPENDING: /* More to write in buffer */ |
505 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
506 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
507 | 0 | return -1; |
508 | 0 | } |
509 | 0 | ret = ctx->buf_len - ctx->buf_off; |
510 | 0 | if (ret == 0 && ctx->encode != B64_NONE |
511 | 0 | && EVP_ENCODE_CTX_num(ctx->base64) != 0) |
512 | 0 | ret = 1; |
513 | 0 | else if (ret <= 0) |
514 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
515 | 0 | break; |
516 | 0 | case BIO_CTRL_PENDING: /* More to read in buffer */ |
517 | 0 | if (!ossl_assert(ctx->buf_len >= ctx->buf_off)) { |
518 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
519 | 0 | return -1; |
520 | 0 | } |
521 | 0 | ret = ctx->buf_len - ctx->buf_off; |
522 | 0 | if (ret <= 0) |
523 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
524 | 0 | break; |
525 | 2.75k | case BIO_CTRL_FLUSH: |
526 | | /* do a final write */ |
527 | 2.82k | again: |
528 | 4.17k | while (ctx->buf_len != ctx->buf_off) { |
529 | 1.34k | i = b64_write(b, NULL, 0); |
530 | 1.34k | if (i < 0) |
531 | 0 | return i; |
532 | 1.34k | } |
533 | 2.82k | if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) { |
534 | 0 | if (ctx->tmp_len != 0) { |
535 | 0 | ctx->buf_len = EVP_EncodeBlock(ctx->buf, |
536 | 0 | ctx->tmp, ctx->tmp_len); |
537 | 0 | ctx->buf_off = 0; |
538 | 0 | ctx->tmp_len = 0; |
539 | 0 | goto again; |
540 | 0 | } |
541 | 2.82k | } else if (ctx->encode != B64_NONE |
542 | 2.82k | && EVP_ENCODE_CTX_num(ctx->base64) != 0) { |
543 | 69 | ctx->buf_off = 0; |
544 | 69 | EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len)); |
545 | | /* push out the bytes */ |
546 | 69 | goto again; |
547 | 69 | } |
548 | | /* Finally flush the underlying BIO */ |
549 | 2.75k | ret = BIO_ctrl(next, cmd, num, ptr); |
550 | 2.75k | BIO_copy_next_retry(b); |
551 | 2.75k | break; |
552 | | |
553 | 0 | case BIO_C_DO_STATE_MACHINE: |
554 | 0 | BIO_clear_retry_flags(b); |
555 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
556 | 0 | BIO_copy_next_retry(b); |
557 | 0 | break; |
558 | | |
559 | 0 | case BIO_CTRL_DUP: |
560 | 0 | break; |
561 | 0 | case BIO_CTRL_INFO: |
562 | 0 | case BIO_CTRL_GET: |
563 | 0 | case BIO_CTRL_SET: |
564 | 5.50k | default: |
565 | 5.50k | ret = BIO_ctrl(next, cmd, num, ptr); |
566 | 5.50k | break; |
567 | 8.25k | } |
568 | 8.25k | return ret; |
569 | 8.25k | } |
570 | | |
571 | | static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
572 | 0 | { |
573 | 0 | BIO *next = BIO_next(b); |
574 | |
|
575 | 0 | if (next == NULL) |
576 | 0 | return 0; |
577 | | |
578 | 0 | return BIO_callback_ctrl(next, cmd, fp); |
579 | 0 | } |
580 | | |
581 | | static int b64_puts(BIO *b, const char *str) |
582 | 0 | { |
583 | 0 | size_t len = strlen(str); |
584 | |
|
585 | 0 | if (len > INT_MAX) |
586 | 0 | return -1; |
587 | 0 | return b64_write(b, str, (int)len); |
588 | 0 | } |