]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/bio_b64.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
60 #include "internal/cryptlib.h"
61 #include <openssl/buffer.h>
62 #include <openssl/evp.h>
64 static int b64_write(BIO
*h
, const char *buf
, int num
);
65 static int b64_read(BIO
*h
, char *buf
, int size
);
66 static int b64_puts(BIO
*h
, const char *str
);
68 * static int b64_gets(BIO *h, char *str, int size);
70 static long b64_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
71 static int b64_new(BIO
*h
);
72 static int b64_free(BIO
*data
);
73 static long b64_callback_ctrl(BIO
*h
, int cmd
, bio_info_cb
*fp
);
74 #define B64_BLOCK_SIZE 1024
75 #define B64_BLOCK_SIZE2 768
80 typedef struct b64_struct
{
82 * BIO *bio; moved to the BIO structure
86 int tmp_len
; /* used to find the start when decoding */
87 int tmp_nl
; /* If true, scan until '\n' */
89 int start
; /* have we started decoding yet? */
90 int cont
; /* <= 0 when finished */
91 EVP_ENCODE_CTX
*base64
;
92 char buf
[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE
) + 10];
93 char tmp
[B64_BLOCK_SIZE
];
96 static BIO_METHOD methods_b64
= {
97 BIO_TYPE_BASE64
, "base64 encoding",
101 NULL
, /* b64_gets, */
108 BIO_METHOD
*BIO_f_base64(void)
110 return (&methods_b64
);
113 static int b64_new(BIO
*bi
)
117 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
123 ctx
->base64
= EVP_ENCODE_CTX_new();
125 bi
->ptr
= (char *)ctx
;
131 static int b64_free(BIO
*a
)
135 EVP_ENCODE_CTX_free(((BIO_B64_CTX
*)a
->ptr
)->base64
);
136 OPENSSL_free(a
->ptr
);
143 static int b64_read(BIO
*b
, char *out
, int outl
)
145 int ret
= 0, i
, ii
, j
, k
, x
, n
, num
, ret_code
= 0;
147 unsigned char *p
, *q
;
151 ctx
= (BIO_B64_CTX
*)b
->ptr
;
153 if ((ctx
== NULL
) || (b
->next_bio
== NULL
))
156 BIO_clear_retry_flags(b
);
158 if (ctx
->encode
!= B64_DECODE
) {
159 ctx
->encode
= B64_DECODE
;
163 EVP_DecodeInit(ctx
->base64
);
166 /* First check if there are bytes decoded/encoded */
167 if (ctx
->buf_len
> 0) {
168 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
169 i
= ctx
->buf_len
- ctx
->buf_off
;
172 OPENSSL_assert(ctx
->buf_off
+ i
< (int)sizeof(ctx
->buf
));
173 memcpy(out
, &(ctx
->buf
[ctx
->buf_off
]), i
);
178 if (ctx
->buf_len
== ctx
->buf_off
) {
185 * At this point, we have room of outl bytes and an empty buffer, so we
186 * should read in some more.
194 i
= BIO_read(b
->next_bio
, &(ctx
->tmp
[ctx
->tmp_len
]),
195 B64_BLOCK_SIZE
- ctx
->tmp_len
);
200 /* Should we continue next time we are called? */
201 if (!BIO_should_retry(b
->next_bio
)) {
203 /* If buffer empty break */
204 if (ctx
->tmp_len
== 0)
206 /* Fall through and process what we have */
210 /* else we retry and add more data to buffer */
218 * We need to scan, a line at a time until we have a valid line if we
221 if (ctx
->start
&& (BIO_get_flags(b
) & BIO_FLAGS_BASE64_NO_NL
)) {
224 } else if (ctx
->start
) {
225 q
= p
= (unsigned char *)ctx
->tmp
;
227 for (j
= 0; j
< i
; j
++) {
232 * due to a previous very long line, we need to keep on
233 * scanning for a '\n' before we even start looking for
234 * base64 encoded stuff.
242 k
= EVP_DecodeUpdate(ctx
->base64
,
243 (unsigned char *)ctx
->buf
,
245 if ((k
<= 0) && (num
== 0) && (ctx
->start
))
246 EVP_DecodeInit(ctx
->base64
);
248 if (p
!= (unsigned char *)
250 i
-= (p
- (unsigned char *)
252 for (x
= 0; x
< i
; x
++)
255 EVP_DecodeInit(ctx
->base64
);
262 /* we fell off the end without starting */
263 if ((j
== i
) && (num
== 0)) {
265 * Is this is one long chunk?, if so, keep on reading until a
268 if (p
== (unsigned char *)&(ctx
->tmp
[0])) {
269 /* Check buffer full */
270 if (i
== B64_BLOCK_SIZE
) {
274 } else if (p
!= q
) { /* finished on a '\n' */
276 for (ii
= 0; ii
< n
; ii
++)
277 ctx
->tmp
[ii
] = p
[ii
];
280 /* else finished on a '\n' */
285 } else if ((i
< B64_BLOCK_SIZE
) && (ctx
->cont
> 0)) {
287 * If buffer isn't full and we can retry then restart to read in
293 if (BIO_get_flags(b
) & BIO_FLAGS_BASE64_NO_NL
) {
296 jj
= i
& ~3; /* process per 4 */
297 z
= EVP_DecodeBlock((unsigned char *)ctx
->buf
,
298 (unsigned char *)ctx
->tmp
, jj
);
300 if (ctx
->tmp
[jj
- 1] == '=') {
302 if (ctx
->tmp
[jj
- 2] == '=')
307 * z is now number of output bytes and jj is the number consumed
310 memmove(ctx
->tmp
, &ctx
->tmp
[jj
], i
- jj
);
311 ctx
->tmp_len
= i
- jj
;
319 i
= EVP_DecodeUpdate(ctx
->base64
,
320 (unsigned char *)ctx
->buf
, &ctx
->buf_len
,
321 (unsigned char *)ctx
->tmp
, i
);
331 if (ctx
->buf_len
<= outl
)
336 memcpy(out
, ctx
->buf
, i
);
339 if (ctx
->buf_off
== ctx
->buf_len
) {
346 /* BIO_clear_retry_flags(b); */
347 BIO_copy_next_retry(b
);
348 return ((ret
== 0) ? ret_code
: ret
);
351 static int b64_write(BIO
*b
, const char *in
, int inl
)
358 ctx
= (BIO_B64_CTX
*)b
->ptr
;
359 BIO_clear_retry_flags(b
);
361 if (ctx
->encode
!= B64_ENCODE
) {
362 ctx
->encode
= B64_ENCODE
;
366 EVP_EncodeInit(ctx
->base64
);
369 OPENSSL_assert(ctx
->buf_off
< (int)sizeof(ctx
->buf
));
370 OPENSSL_assert(ctx
->buf_len
<= (int)sizeof(ctx
->buf
));
371 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
372 n
= ctx
->buf_len
- ctx
->buf_off
;
374 i
= BIO_write(b
->next_bio
, &(ctx
->buf
[ctx
->buf_off
]), n
);
376 BIO_copy_next_retry(b
);
379 OPENSSL_assert(i
<= n
);
381 OPENSSL_assert(ctx
->buf_off
<= (int)sizeof(ctx
->buf
));
382 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
385 /* at this point all pending data has been written */
389 if ((in
== NULL
) || (inl
<= 0))
393 n
= (inl
> B64_BLOCK_SIZE
) ? B64_BLOCK_SIZE
: inl
;
395 if (BIO_get_flags(b
) & BIO_FLAGS_BASE64_NO_NL
) {
396 if (ctx
->tmp_len
> 0) {
397 OPENSSL_assert(ctx
->tmp_len
<= 3);
398 n
= 3 - ctx
->tmp_len
;
400 * There's a theoretical possibility for this
404 memcpy(&(ctx
->tmp
[ctx
->tmp_len
]), in
, n
);
407 if (ctx
->tmp_len
< 3)
410 EVP_EncodeBlock((unsigned char *)ctx
->buf
,
411 (unsigned char *)ctx
->tmp
, ctx
->tmp_len
);
412 OPENSSL_assert(ctx
->buf_len
<= (int)sizeof(ctx
->buf
));
413 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
415 * Since we're now done using the temporary buffer, the
416 * length should be 0'd
421 memcpy(ctx
->tmp
, in
, n
);
428 EVP_EncodeBlock((unsigned char *)ctx
->buf
,
429 (const unsigned char *)in
, n
);
430 OPENSSL_assert(ctx
->buf_len
<= (int)sizeof(ctx
->buf
));
431 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
435 EVP_EncodeUpdate(ctx
->base64
,
436 (unsigned char *)ctx
->buf
, &ctx
->buf_len
,
437 (unsigned char *)in
, n
);
438 OPENSSL_assert(ctx
->buf_len
<= (int)sizeof(ctx
->buf
));
439 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
448 i
= BIO_write(b
->next_bio
, &(ctx
->buf
[ctx
->buf_off
]), n
);
450 BIO_copy_next_retry(b
);
451 return ((ret
== 0) ? i
: ret
);
453 OPENSSL_assert(i
<= n
);
456 OPENSSL_assert(ctx
->buf_off
<= (int)sizeof(ctx
->buf
));
457 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
465 static long b64_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
471 ctx
= (BIO_B64_CTX
*)b
->ptr
;
477 ctx
->encode
= B64_NONE
;
478 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
480 case BIO_CTRL_EOF
: /* More to read */
484 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
486 case BIO_CTRL_WPENDING
: /* More to write in buffer */
487 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
488 ret
= ctx
->buf_len
- ctx
->buf_off
;
489 if ((ret
== 0) && (ctx
->encode
!= B64_NONE
)
490 && (EVP_ENCODE_CTX_num(ctx
->base64
) != 0))
493 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
495 case BIO_CTRL_PENDING
: /* More to read in buffer */
496 OPENSSL_assert(ctx
->buf_len
>= ctx
->buf_off
);
497 ret
= ctx
->buf_len
- ctx
->buf_off
;
499 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
502 /* do a final write */
504 while (ctx
->buf_len
!= ctx
->buf_off
) {
505 i
= b64_write(b
, NULL
, 0);
509 if (BIO_get_flags(b
) & BIO_FLAGS_BASE64_NO_NL
) {
510 if (ctx
->tmp_len
!= 0) {
511 ctx
->buf_len
= EVP_EncodeBlock((unsigned char *)ctx
->buf
,
512 (unsigned char *)ctx
->tmp
,
518 } else if (ctx
->encode
!= B64_NONE
519 && EVP_ENCODE_CTX_num(ctx
->base64
) != 0) {
521 EVP_EncodeFinal(ctx
->base64
,
522 (unsigned char *)ctx
->buf
, &(ctx
->buf_len
));
523 /* push out the bytes */
526 /* Finally flush the underlying BIO */
527 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
530 case BIO_C_DO_STATE_MACHINE
:
531 BIO_clear_retry_flags(b
);
532 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
533 BIO_copy_next_retry(b
);
542 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
548 static long b64_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
)
552 if (b
->next_bio
== NULL
)
556 ret
= BIO_callback_ctrl(b
->next_bio
, cmd
, fp
);
562 static int b64_puts(BIO
*b
, const char *str
)
564 return b64_write(b
, str
, strlen(str
));