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