]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/bio_b64.c
Create BIO_read_ex() which handles size_t arguments
[thirdparty/openssl.git] / crypto / evp / bio_b64.c
1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * 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 */
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 /*
21 * static int b64_gets(BIO *h, char *str, int size);
22 */
23 static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
24 static int b64_new(BIO *h);
25 static int b64_free(BIO *data);
26 static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
27 #define B64_BLOCK_SIZE 1024
28 #define B64_BLOCK_SIZE2 768
29 #define B64_NONE 0
30 #define B64_ENCODE 1
31 #define B64_DECODE 2
32
33 typedef struct b64_struct {
34 /*
35 * BIO *bio; moved to the BIO structure
36 */
37 int buf_len;
38 int buf_off;
39 int tmp_len; /* used to find the start when decoding */
40 int tmp_nl; /* If true, scan until '\n' */
41 int encode;
42 int start; /* have we started decoding yet? */
43 int cont; /* <= 0 when finished */
44 EVP_ENCODE_CTX *base64;
45 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
46 char tmp[B64_BLOCK_SIZE];
47 } BIO_B64_CTX;
48
49 static const BIO_METHOD methods_b64 = {
50 BIO_TYPE_BASE64, "base64 encoding",
51 b64_write,
52 /* TODO: Convert to new style read function */
53 bread_conv,
54 b64_read,
55 b64_puts,
56 NULL, /* b64_gets, */
57 b64_ctrl,
58 b64_new,
59 b64_free,
60 b64_callback_ctrl,
61 };
62
63
64 const BIO_METHOD *BIO_f_base64(void)
65 {
66 return &methods_b64;
67 }
68
69 static int b64_new(BIO *bi)
70 {
71 BIO_B64_CTX *ctx;
72
73 ctx = OPENSSL_zalloc(sizeof(*ctx));
74 if (ctx == NULL)
75 return 0;
76
77 ctx->cont = 1;
78 ctx->start = 1;
79 ctx->base64 = EVP_ENCODE_CTX_new();
80 if (ctx->base64 == NULL) {
81 OPENSSL_free(ctx);
82 return 0;
83 }
84
85 BIO_set_data(bi, ctx);
86 BIO_set_init(bi, 1);
87
88 return 1;
89 }
90
91 static int b64_free(BIO *a)
92 {
93 BIO_B64_CTX *ctx;
94 if (a == NULL)
95 return 0;
96
97 ctx = BIO_get_data(a);
98 if (ctx == NULL)
99 return 0;
100
101 EVP_ENCODE_CTX_free(ctx->base64);
102 OPENSSL_free(ctx);
103 BIO_set_data(a, NULL);
104 BIO_set_init(a, 0);
105
106 return 1;
107 }
108
109 static int b64_read(BIO *b, char *out, int outl)
110 {
111 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
112 BIO_B64_CTX *ctx;
113 unsigned char *p, *q;
114 BIO *next;
115
116 if (out == NULL)
117 return (0);
118 ctx = (BIO_B64_CTX *)BIO_get_data(b);
119
120 next = BIO_next(b);
121 if ((ctx == NULL) || (next == NULL))
122 return 0;
123
124 BIO_clear_retry_flags(b);
125
126 if (ctx->encode != B64_DECODE) {
127 ctx->encode = B64_DECODE;
128 ctx->buf_len = 0;
129 ctx->buf_off = 0;
130 ctx->tmp_len = 0;
131 EVP_DecodeInit(ctx->base64);
132 }
133
134 /* First check if there are bytes decoded/encoded */
135 if (ctx->buf_len > 0) {
136 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
137 i = ctx->buf_len - ctx->buf_off;
138 if (i > outl)
139 i = outl;
140 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
141 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
142 ret = i;
143 out += i;
144 outl -= i;
145 ctx->buf_off += i;
146 if (ctx->buf_len == ctx->buf_off) {
147 ctx->buf_len = 0;
148 ctx->buf_off = 0;
149 }
150 }
151
152 /*
153 * At this point, we have room of outl bytes and an empty buffer, so we
154 * should read in some more.
155 */
156
157 ret_code = 0;
158 while (outl > 0) {
159 if (ctx->cont <= 0)
160 break;
161
162 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
163 B64_BLOCK_SIZE - ctx->tmp_len);
164
165 if (i <= 0) {
166 ret_code = i;
167
168 /* Should we continue next time we are called? */
169 if (!BIO_should_retry(next)) {
170 ctx->cont = i;
171 /* If buffer empty break */
172 if (ctx->tmp_len == 0)
173 break;
174 /* Fall through and process what we have */
175 else
176 i = 0;
177 }
178 /* else we retry and add more data to buffer */
179 else
180 break;
181 }
182 i += ctx->tmp_len;
183 ctx->tmp_len = i;
184
185 /*
186 * We need to scan, a line at a time until we have a valid line if we
187 * are starting.
188 */
189 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
190 /* ctx->start=1; */
191 ctx->tmp_len = 0;
192 } else if (ctx->start) {
193 q = p = (unsigned char *)ctx->tmp;
194 num = 0;
195 for (j = 0; j < i; j++) {
196 if (*(q++) != '\n')
197 continue;
198
199 /*
200 * due to a previous very long line, we need to keep on
201 * scanning for a '\n' before we even start looking for
202 * base64 encoded stuff.
203 */
204 if (ctx->tmp_nl) {
205 p = q;
206 ctx->tmp_nl = 0;
207 continue;
208 }
209
210 k = EVP_DecodeUpdate(ctx->base64,
211 (unsigned char *)ctx->buf,
212 &num, p, q - p);
213 if ((k <= 0) && (num == 0) && (ctx->start))
214 EVP_DecodeInit(ctx->base64);
215 else {
216 if (p != (unsigned char *)
217 &(ctx->tmp[0])) {
218 i -= (p - (unsigned char *)
219 &(ctx->tmp[0]));
220 for (x = 0; x < i; x++)
221 ctx->tmp[x] = p[x];
222 }
223 EVP_DecodeInit(ctx->base64);
224 ctx->start = 0;
225 break;
226 }
227 p = q;
228 }
229
230 /* we fell off the end without starting */
231 if ((j == i) && (num == 0)) {
232 /*
233 * Is this is one long chunk?, if so, keep on reading until a
234 * new line.
235 */
236 if (p == (unsigned char *)&(ctx->tmp[0])) {
237 /* Check buffer full */
238 if (i == B64_BLOCK_SIZE) {
239 ctx->tmp_nl = 1;
240 ctx->tmp_len = 0;
241 }
242 } else if (p != q) { /* finished on a '\n' */
243 n = q - p;
244 for (ii = 0; ii < n; ii++)
245 ctx->tmp[ii] = p[ii];
246 ctx->tmp_len = n;
247 }
248 /* else finished on a '\n' */
249 continue;
250 } else {
251 ctx->tmp_len = 0;
252 }
253 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
254 /*
255 * If buffer isn't full and we can retry then restart to read in
256 * more data.
257 */
258 continue;
259 }
260
261 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
262 int z, jj;
263
264 jj = i & ~3; /* process per 4 */
265 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
266 (unsigned char *)ctx->tmp, jj);
267 if (jj > 2) {
268 if (ctx->tmp[jj - 1] == '=') {
269 z--;
270 if (ctx->tmp[jj - 2] == '=')
271 z--;
272 }
273 }
274 /*
275 * z is now number of output bytes and jj is the number consumed
276 */
277 if (jj != i) {
278 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
279 ctx->tmp_len = i - jj;
280 }
281 ctx->buf_len = 0;
282 if (z > 0) {
283 ctx->buf_len = z;
284 }
285 i = z;
286 } else {
287 i = EVP_DecodeUpdate(ctx->base64,
288 (unsigned char *)ctx->buf, &ctx->buf_len,
289 (unsigned char *)ctx->tmp, i);
290 ctx->tmp_len = 0;
291 }
292 ctx->buf_off = 0;
293 if (i < 0) {
294 ret_code = 0;
295 ctx->buf_len = 0;
296 break;
297 }
298
299 if (ctx->buf_len <= outl)
300 i = ctx->buf_len;
301 else
302 i = outl;
303
304 memcpy(out, ctx->buf, i);
305 ret += i;
306 ctx->buf_off = i;
307 if (ctx->buf_off == ctx->buf_len) {
308 ctx->buf_len = 0;
309 ctx->buf_off = 0;
310 }
311 outl -= i;
312 out += i;
313 }
314 /* BIO_clear_retry_flags(b); */
315 BIO_copy_next_retry(b);
316 return ((ret == 0) ? ret_code : ret);
317 }
318
319 static int b64_write(BIO *b, const char *in, int inl)
320 {
321 int ret = 0;
322 int n;
323 int i;
324 BIO_B64_CTX *ctx;
325 BIO *next;
326
327 ctx = (BIO_B64_CTX *)BIO_get_data(b);
328 next = BIO_next(b);
329 if ((ctx == NULL) || (next == NULL))
330 return 0;
331
332 BIO_clear_retry_flags(b);
333
334 if (ctx->encode != B64_ENCODE) {
335 ctx->encode = B64_ENCODE;
336 ctx->buf_len = 0;
337 ctx->buf_off = 0;
338 ctx->tmp_len = 0;
339 EVP_EncodeInit(ctx->base64);
340 }
341
342 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
343 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
344 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
345 n = ctx->buf_len - ctx->buf_off;
346 while (n > 0) {
347 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
348 if (i <= 0) {
349 BIO_copy_next_retry(b);
350 return (i);
351 }
352 OPENSSL_assert(i <= n);
353 ctx->buf_off += i;
354 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
355 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
356 n -= i;
357 }
358 /* at this point all pending data has been written */
359 ctx->buf_off = 0;
360 ctx->buf_len = 0;
361
362 if ((in == NULL) || (inl <= 0))
363 return (0);
364
365 while (inl > 0) {
366 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
367
368 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
369 if (ctx->tmp_len > 0) {
370 OPENSSL_assert(ctx->tmp_len <= 3);
371 n = 3 - ctx->tmp_len;
372 /*
373 * There's a theoretical possibility for this
374 */
375 if (n > inl)
376 n = inl;
377 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
378 ctx->tmp_len += n;
379 ret += n;
380 if (ctx->tmp_len < 3)
381 break;
382 ctx->buf_len =
383 EVP_EncodeBlock((unsigned char *)ctx->buf,
384 (unsigned char *)ctx->tmp, ctx->tmp_len);
385 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
386 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
387 /*
388 * Since we're now done using the temporary buffer, the
389 * length should be 0'd
390 */
391 ctx->tmp_len = 0;
392 } else {
393 if (n < 3) {
394 memcpy(ctx->tmp, in, n);
395 ctx->tmp_len = n;
396 ret += n;
397 break;
398 }
399 n -= n % 3;
400 ctx->buf_len =
401 EVP_EncodeBlock((unsigned char *)ctx->buf,
402 (const unsigned char *)in, n);
403 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
404 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
405 ret += n;
406 }
407 } else {
408 if (!EVP_EncodeUpdate(ctx->base64,
409 (unsigned char *)ctx->buf, &ctx->buf_len,
410 (unsigned char *)in, n))
411 return ((ret == 0) ? -1 : ret);
412 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
413 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
414 ret += n;
415 }
416 inl -= n;
417 in += n;
418
419 ctx->buf_off = 0;
420 n = ctx->buf_len;
421 while (n > 0) {
422 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
423 if (i <= 0) {
424 BIO_copy_next_retry(b);
425 return ((ret == 0) ? i : ret);
426 }
427 OPENSSL_assert(i <= n);
428 n -= i;
429 ctx->buf_off += i;
430 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
431 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
432 }
433 ctx->buf_len = 0;
434 ctx->buf_off = 0;
435 }
436 return (ret);
437 }
438
439 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
440 {
441 BIO_B64_CTX *ctx;
442 long ret = 1;
443 int i;
444 BIO *next;
445
446 ctx = (BIO_B64_CTX *)BIO_get_data(b);
447 next = BIO_next(b);
448 if ((ctx == NULL) || (next == NULL))
449 return 0;
450
451 switch (cmd) {
452 case BIO_CTRL_RESET:
453 ctx->cont = 1;
454 ctx->start = 1;
455 ctx->encode = B64_NONE;
456 ret = BIO_ctrl(next, cmd, num, ptr);
457 break;
458 case BIO_CTRL_EOF: /* More to read */
459 if (ctx->cont <= 0)
460 ret = 1;
461 else
462 ret = BIO_ctrl(next, cmd, num, ptr);
463 break;
464 case BIO_CTRL_WPENDING: /* More to write in buffer */
465 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
466 ret = ctx->buf_len - ctx->buf_off;
467 if ((ret == 0) && (ctx->encode != B64_NONE)
468 && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
469 ret = 1;
470 else if (ret <= 0)
471 ret = BIO_ctrl(next, cmd, num, ptr);
472 break;
473 case BIO_CTRL_PENDING: /* More to read in buffer */
474 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
475 ret = ctx->buf_len - ctx->buf_off;
476 if (ret <= 0)
477 ret = BIO_ctrl(next, cmd, num, ptr);
478 break;
479 case BIO_CTRL_FLUSH:
480 /* do a final write */
481 again:
482 while (ctx->buf_len != ctx->buf_off) {
483 i = b64_write(b, NULL, 0);
484 if (i < 0)
485 return i;
486 }
487 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
488 if (ctx->tmp_len != 0) {
489 ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
490 (unsigned char *)ctx->tmp,
491 ctx->tmp_len);
492 ctx->buf_off = 0;
493 ctx->tmp_len = 0;
494 goto again;
495 }
496 } else if (ctx->encode != B64_NONE
497 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
498 ctx->buf_off = 0;
499 EVP_EncodeFinal(ctx->base64,
500 (unsigned char *)ctx->buf, &(ctx->buf_len));
501 /* push out the bytes */
502 goto again;
503 }
504 /* Finally flush the underlying BIO */
505 ret = BIO_ctrl(next, cmd, num, ptr);
506 break;
507
508 case BIO_C_DO_STATE_MACHINE:
509 BIO_clear_retry_flags(b);
510 ret = BIO_ctrl(next, cmd, num, ptr);
511 BIO_copy_next_retry(b);
512 break;
513
514 case BIO_CTRL_DUP:
515 break;
516 case BIO_CTRL_INFO:
517 case BIO_CTRL_GET:
518 case BIO_CTRL_SET:
519 default:
520 ret = BIO_ctrl(next, cmd, num, ptr);
521 break;
522 }
523 return ret;
524 }
525
526 static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
527 {
528 long ret = 1;
529 BIO *next = BIO_next(b);
530
531 if (next == NULL)
532 return 0;
533 switch (cmd) {
534 default:
535 ret = BIO_callback_ctrl(next, cmd, fp);
536 break;
537 }
538 return (ret);
539 }
540
541 static int b64_puts(BIO *b, const char *str)
542 {
543 return b64_write(b, str, strlen(str));
544 }