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