]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/bio_b64.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "internal/bio.h"
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
);
21 * static int b64_gets(BIO *h, char *str, int size);
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
33 typedef struct b64_struct
{
35 * BIO *bio; moved to the BIO structure
39 int tmp_len
; /* used to find the start when decoding */
40 int tmp_nl
; /* If true, scan until '\n' */
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
];
49 static const BIO_METHOD methods_b64
= {
50 BIO_TYPE_BASE64
, "base64 encoding",
62 const BIO_METHOD
*BIO_f_base64(void)
67 static int b64_new(BIO
*bi
)
71 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
77 ctx
->base64
= EVP_ENCODE_CTX_new();
78 if (ctx
->base64
== NULL
) {
83 BIO_set_data(bi
, ctx
);
89 static int b64_free(BIO
*a
)
95 ctx
= BIO_get_data(a
);
99 EVP_ENCODE_CTX_free(ctx
->base64
);
101 BIO_set_data(a
, NULL
);
107 static int b64_read(BIO
*b
, char *out
, int outl
)
109 int ret
= 0, i
, ii
, j
, k
, x
, n
, num
, ret_code
= 0;
111 unsigned char *p
, *q
;
116 ctx
= (BIO_B64_CTX
*)BIO_get_data(b
);
119 if ((ctx
== NULL
) || (next
== NULL
))
122 BIO_clear_retry_flags(b
);
124 if (ctx
->encode
!= B64_DECODE
) {
125 ctx
->encode
= B64_DECODE
;
129 EVP_DecodeInit(ctx
->base64
);
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
;
138 OPENSSL_assert(ctx
->buf_off
+ i
< (int)sizeof(ctx
->buf
));
139 memcpy(out
, &(ctx
->buf
[ctx
->buf_off
]), i
);
144 if (ctx
->buf_len
== ctx
->buf_off
) {
151 * At this point, we have room of outl bytes and an empty buffer, so we
152 * should read in some more.
160 i
= BIO_read(next
, &(ctx
->tmp
[ctx
->tmp_len
]),
161 B64_BLOCK_SIZE
- ctx
->tmp_len
);
166 /* Should we continue next time we are called? */
167 if (!BIO_should_retry(next
)) {
169 /* If buffer empty break */
170 if (ctx
->tmp_len
== 0)
172 /* Fall through and process what we have */
176 /* else we retry and add more data to buffer */
184 * We need to scan, a line at a time until we have a valid line if we
187 if (ctx
->start
&& (BIO_get_flags(b
) & BIO_FLAGS_BASE64_NO_NL
)) {
190 } else if (ctx
->start
) {
191 q
= p
= (unsigned char *)ctx
->tmp
;
193 for (j
= 0; j
< i
; j
++) {
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.
208 k
= EVP_DecodeUpdate(ctx
->base64
,
209 (unsigned char *)ctx
->buf
,
211 if ((k
<= 0) && (num
== 0) && (ctx
->start
))
212 EVP_DecodeInit(ctx
->base64
);
214 if (p
!= (unsigned char *)
216 i
-= (p
- (unsigned char *)
218 for (x
= 0; x
< i
; x
++)
221 EVP_DecodeInit(ctx
->base64
);
228 /* we fell off the end without starting */
229 if ((j
== i
) && (num
== 0)) {
231 * Is this is one long chunk?, if so, keep on reading until a
234 if (p
== (unsigned char *)&(ctx
->tmp
[0])) {
235 /* Check buffer full */
236 if (i
== B64_BLOCK_SIZE
) {
240 } else if (p
!= q
) { /* finished on a '\n' */
242 for (ii
= 0; ii
< n
; ii
++)
243 ctx
->tmp
[ii
] = p
[ii
];
246 /* else finished on a '\n' */
251 } else if ((i
< B64_BLOCK_SIZE
) && (ctx
->cont
> 0)) {
253 * If buffer isn't full and we can retry then restart to read in
259 if (BIO_get_flags(b
) & BIO_FLAGS_BASE64_NO_NL
) {
262 jj
= i
& ~3; /* process per 4 */
263 z
= EVP_DecodeBlock((unsigned char *)ctx
->buf
,
264 (unsigned char *)ctx
->tmp
, jj
);
266 if (ctx
->tmp
[jj
- 1] == '=') {
268 if (ctx
->tmp
[jj
- 2] == '=')
273 * z is now number of output bytes and jj is the number consumed
276 memmove(ctx
->tmp
, &ctx
->tmp
[jj
], i
- jj
);
277 ctx
->tmp_len
= i
- jj
;
285 i
= EVP_DecodeUpdate(ctx
->base64
,
286 (unsigned char *)ctx
->buf
, &ctx
->buf_len
,
287 (unsigned char *)ctx
->tmp
, i
);
297 if (ctx
->buf_len
<= outl
)
302 memcpy(out
, ctx
->buf
, i
);
305 if (ctx
->buf_off
== ctx
->buf_len
) {
312 /* BIO_clear_retry_flags(b); */
313 BIO_copy_next_retry(b
);
314 return ((ret
== 0) ? ret_code
: ret
);
317 static int b64_write(BIO
*b
, const char *in
, int inl
)
325 ctx
= (BIO_B64_CTX
*)BIO_get_data(b
);
327 if ((ctx
== NULL
) || (next
== NULL
))
330 BIO_clear_retry_flags(b
);
332 if (ctx
->encode
!= B64_ENCODE
) {
333 ctx
->encode
= B64_ENCODE
;
337 EVP_EncodeInit(ctx
->base64
);
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
;
345 i
= BIO_write(next
, &(ctx
->buf
[ctx
->buf_off
]), n
);
347 BIO_copy_next_retry(b
);
350 OPENSSL_assert(i
<= n
);
352 OPENSSL_assert(ctx
->buf_off
<= (int)sizeof(ctx
->buf
));
353 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
356 /* at this point all pending data has been written */
360 if ((in
== NULL
) || (inl
<= 0))
364 n
= (inl
> B64_BLOCK_SIZE
) ? B64_BLOCK_SIZE
: inl
;
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
;
371 * There's a theoretical possibility for this
375 memcpy(&(ctx
->tmp
[ctx
->tmp_len
]), in
, n
);
378 if (ctx
->tmp_len
< 3)
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
);
386 * Since we're now done using the temporary buffer, the
387 * length should be 0'd
392 memcpy(ctx
->tmp
, in
, n
);
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
);
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
);
419 i
= BIO_write(next
, &(ctx
->buf
[ctx
->buf_off
]), n
);
421 BIO_copy_next_retry(b
);
422 return ((ret
== 0) ? i
: ret
);
424 OPENSSL_assert(i
<= n
);
427 OPENSSL_assert(ctx
->buf_off
<= (int)sizeof(ctx
->buf
));
428 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
436 static long b64_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
443 ctx
= (BIO_B64_CTX
*)BIO_get_data(b
);
445 if ((ctx
== NULL
) || (next
== NULL
))
452 ctx
->encode
= B64_NONE
;
453 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
455 case BIO_CTRL_EOF
: /* More to read */
459 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
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))
468 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
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
;
474 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
477 /* do a final write */
479 while (ctx
->buf_len
!= ctx
->buf_off
) {
480 i
= b64_write(b
, NULL
, 0);
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
,
493 } else if (ctx
->encode
!= B64_NONE
494 && EVP_ENCODE_CTX_num(ctx
->base64
) != 0) {
496 EVP_EncodeFinal(ctx
->base64
,
497 (unsigned char *)ctx
->buf
, &(ctx
->buf_len
));
498 /* push out the bytes */
501 /* Finally flush the underlying BIO */
502 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
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
);
517 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
523 static long b64_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
)
526 BIO
*next
= BIO_next(b
);
532 ret
= BIO_callback_ctrl(next
, cmd
, fp
);
538 static int b64_puts(BIO
*b
, const char *str
)
540 return b64_write(b
, str
, strlen(str
));