10Sstevel@tonic-gate /* crypto/evp/bio_b64.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <errno.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/buffer.h>
630Sstevel@tonic-gate #include <openssl/evp.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate static int b64_write(BIO *h, const char *buf, int num);
660Sstevel@tonic-gate static int b64_read(BIO *h, char *buf, int size);
670Sstevel@tonic-gate /*static int b64_puts(BIO *h, const char *str); */
680Sstevel@tonic-gate /*static int b64_gets(BIO *h, char *str, int size); */
690Sstevel@tonic-gate static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
700Sstevel@tonic-gate static int b64_new(BIO *h);
710Sstevel@tonic-gate static int b64_free(BIO *data);
720Sstevel@tonic-gate static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
730Sstevel@tonic-gate #define B64_BLOCK_SIZE 1024
740Sstevel@tonic-gate #define B64_BLOCK_SIZE2 768
750Sstevel@tonic-gate #define B64_NONE 0
760Sstevel@tonic-gate #define B64_ENCODE 1
770Sstevel@tonic-gate #define B64_DECODE 2
780Sstevel@tonic-gate
790Sstevel@tonic-gate typedef struct b64_struct
800Sstevel@tonic-gate {
810Sstevel@tonic-gate /*BIO *bio; moved to the BIO structure */
820Sstevel@tonic-gate int buf_len;
830Sstevel@tonic-gate int buf_off;
840Sstevel@tonic-gate int tmp_len; /* used to find the start when decoding */
850Sstevel@tonic-gate int tmp_nl; /* If true, scan until '\n' */
860Sstevel@tonic-gate int encode;
870Sstevel@tonic-gate int start; /* have we started decoding yet? */
880Sstevel@tonic-gate int cont; /* <= 0 when finished */
890Sstevel@tonic-gate EVP_ENCODE_CTX base64;
900Sstevel@tonic-gate char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10];
910Sstevel@tonic-gate char tmp[B64_BLOCK_SIZE];
920Sstevel@tonic-gate } BIO_B64_CTX;
930Sstevel@tonic-gate
940Sstevel@tonic-gate static BIO_METHOD methods_b64=
950Sstevel@tonic-gate {
960Sstevel@tonic-gate BIO_TYPE_BASE64,"base64 encoding",
970Sstevel@tonic-gate b64_write,
980Sstevel@tonic-gate b64_read,
990Sstevel@tonic-gate NULL, /* b64_puts, */
1000Sstevel@tonic-gate NULL, /* b64_gets, */
1010Sstevel@tonic-gate b64_ctrl,
1020Sstevel@tonic-gate b64_new,
1030Sstevel@tonic-gate b64_free,
1040Sstevel@tonic-gate b64_callback_ctrl,
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate
BIO_f_base64(void)1070Sstevel@tonic-gate BIO_METHOD *BIO_f_base64(void)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate return(&methods_b64);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
b64_new(BIO * bi)1120Sstevel@tonic-gate static int b64_new(BIO *bi)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate BIO_B64_CTX *ctx;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate ctx=(BIO_B64_CTX *)OPENSSL_malloc(sizeof(BIO_B64_CTX));
1170Sstevel@tonic-gate if (ctx == NULL) return(0);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate ctx->buf_len=0;
1200Sstevel@tonic-gate ctx->tmp_len=0;
1210Sstevel@tonic-gate ctx->tmp_nl=0;
1220Sstevel@tonic-gate ctx->buf_off=0;
1230Sstevel@tonic-gate ctx->cont=1;
1240Sstevel@tonic-gate ctx->start=1;
1250Sstevel@tonic-gate ctx->encode=0;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate bi->init=1;
1280Sstevel@tonic-gate bi->ptr=(char *)ctx;
1290Sstevel@tonic-gate bi->flags=0;
1300Sstevel@tonic-gate return(1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
b64_free(BIO * a)1330Sstevel@tonic-gate static int b64_free(BIO *a)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate if (a == NULL) return(0);
1360Sstevel@tonic-gate OPENSSL_free(a->ptr);
1370Sstevel@tonic-gate a->ptr=NULL;
1380Sstevel@tonic-gate a->init=0;
1390Sstevel@tonic-gate a->flags=0;
1400Sstevel@tonic-gate return(1);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
b64_read(BIO * b,char * out,int outl)1430Sstevel@tonic-gate static int b64_read(BIO *b, char *out, int outl)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate int ret=0,i,ii,j,k,x,n,num,ret_code=0;
1460Sstevel@tonic-gate BIO_B64_CTX *ctx;
1470Sstevel@tonic-gate unsigned char *p,*q;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate if (out == NULL) return(0);
1500Sstevel@tonic-gate ctx=(BIO_B64_CTX *)b->ptr;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if (ctx->encode != B64_DECODE)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate ctx->encode=B64_DECODE;
1570Sstevel@tonic-gate ctx->buf_len=0;
1580Sstevel@tonic-gate ctx->buf_off=0;
1590Sstevel@tonic-gate ctx->tmp_len=0;
1600Sstevel@tonic-gate EVP_DecodeInit(&(ctx->base64));
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /* First check if there are bytes decoded/encoded */
1640Sstevel@tonic-gate if (ctx->buf_len > 0)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate i=ctx->buf_len-ctx->buf_off;
1670Sstevel@tonic-gate if (i > outl) i=outl;
168*2139Sjp161948 OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf));
1690Sstevel@tonic-gate memcpy(out,&(ctx->buf[ctx->buf_off]),i);
1700Sstevel@tonic-gate ret=i;
1710Sstevel@tonic-gate out+=i;
1720Sstevel@tonic-gate outl-=i;
1730Sstevel@tonic-gate ctx->buf_off+=i;
1740Sstevel@tonic-gate if (ctx->buf_len == ctx->buf_off)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate ctx->buf_len=0;
1770Sstevel@tonic-gate ctx->buf_off=0;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /* At this point, we have room of outl bytes and an empty
1820Sstevel@tonic-gate * buffer, so we should read in some more. */
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate ret_code=0;
1850Sstevel@tonic-gate while (outl > 0)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate if (ctx->cont <= 0)
1890Sstevel@tonic-gate break;
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]),
1920Sstevel@tonic-gate B64_BLOCK_SIZE-ctx->tmp_len);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (i <= 0)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate ret_code=i;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /* Should be continue next time we are called? */
1990Sstevel@tonic-gate if (!BIO_should_retry(b->next_bio))
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate ctx->cont=i;
2020Sstevel@tonic-gate /* If buffer empty break */
2030Sstevel@tonic-gate if(ctx->tmp_len == 0)
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate /* Fall through and process what we have */
2060Sstevel@tonic-gate else
2070Sstevel@tonic-gate i = 0;
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate /* else we retry and add more data to buffer */
2100Sstevel@tonic-gate else
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate i+=ctx->tmp_len;
2140Sstevel@tonic-gate ctx->tmp_len = i;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /* We need to scan, a line at a time until we
2170Sstevel@tonic-gate * have a valid line if we are starting. */
2180Sstevel@tonic-gate if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL))
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate /* ctx->start=1; */
2210Sstevel@tonic-gate ctx->tmp_len=0;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate else if (ctx->start)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate q=p=(unsigned char *)ctx->tmp;
2260Sstevel@tonic-gate for (j=0; j<i; j++)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate if (*(q++) != '\n') continue;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate /* due to a previous very long line,
2310Sstevel@tonic-gate * we need to keep on scanning for a '\n'
2320Sstevel@tonic-gate * before we even start looking for
2330Sstevel@tonic-gate * base64 encoded stuff. */
2340Sstevel@tonic-gate if (ctx->tmp_nl)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate p=q;
2370Sstevel@tonic-gate ctx->tmp_nl=0;
2380Sstevel@tonic-gate continue;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate k=EVP_DecodeUpdate(&(ctx->base64),
2420Sstevel@tonic-gate (unsigned char *)ctx->buf,
2430Sstevel@tonic-gate &num,p,q-p);
2440Sstevel@tonic-gate if ((k <= 0) && (num == 0) && (ctx->start))
2450Sstevel@tonic-gate EVP_DecodeInit(&ctx->base64);
2460Sstevel@tonic-gate else
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate if (p != (unsigned char *)
2490Sstevel@tonic-gate &(ctx->tmp[0]))
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate i-=(p- (unsigned char *)
2520Sstevel@tonic-gate &(ctx->tmp[0]));
2530Sstevel@tonic-gate for (x=0; x < i; x++)
2540Sstevel@tonic-gate ctx->tmp[x]=p[x];
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate EVP_DecodeInit(&ctx->base64);
2570Sstevel@tonic-gate ctx->start=0;
2580Sstevel@tonic-gate break;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate p=q;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate /* we fell off the end without starting */
2640Sstevel@tonic-gate if (j == i)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate /* Is this is one long chunk?, if so, keep on
2670Sstevel@tonic-gate * reading until a new line. */
2680Sstevel@tonic-gate if (p == (unsigned char *)&(ctx->tmp[0]))
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate /* Check buffer full */
2710Sstevel@tonic-gate if (i == B64_BLOCK_SIZE)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate ctx->tmp_nl=1;
2740Sstevel@tonic-gate ctx->tmp_len=0;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate else if (p != q) /* finished on a '\n' */
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate n=q-p;
2800Sstevel@tonic-gate for (ii=0; ii<n; ii++)
2810Sstevel@tonic-gate ctx->tmp[ii]=p[ii];
2820Sstevel@tonic-gate ctx->tmp_len=n;
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate /* else finished on a '\n' */
2850Sstevel@tonic-gate continue;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate else
2880Sstevel@tonic-gate ctx->tmp_len=0;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate /* If buffer isn't full and we can retry then
2910Sstevel@tonic-gate * restart to read in more data.
2920Sstevel@tonic-gate */
2930Sstevel@tonic-gate else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
2940Sstevel@tonic-gate continue;
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate int z,jj;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate jj=(i>>2)<<2;
3010Sstevel@tonic-gate z=EVP_DecodeBlock((unsigned char *)ctx->buf,
3020Sstevel@tonic-gate (unsigned char *)ctx->tmp,jj);
3030Sstevel@tonic-gate if (jj > 2)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate if (ctx->tmp[jj-1] == '=')
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate z--;
3080Sstevel@tonic-gate if (ctx->tmp[jj-2] == '=')
3090Sstevel@tonic-gate z--;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate /* z is now number of output bytes and jj is the
3130Sstevel@tonic-gate * number consumed */
3140Sstevel@tonic-gate if (jj != i)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate memcpy((unsigned char *)ctx->tmp,
3170Sstevel@tonic-gate (unsigned char *)&(ctx->tmp[jj]),i-jj);
3180Sstevel@tonic-gate ctx->tmp_len=i-jj;
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate ctx->buf_len=0;
3210Sstevel@tonic-gate if (z > 0)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate ctx->buf_len=z;
3240Sstevel@tonic-gate i=1;
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate else
3270Sstevel@tonic-gate i=z;
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate else
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate i=EVP_DecodeUpdate(&(ctx->base64),
3320Sstevel@tonic-gate (unsigned char *)ctx->buf,&ctx->buf_len,
3330Sstevel@tonic-gate (unsigned char *)ctx->tmp,i);
3340Sstevel@tonic-gate ctx->tmp_len = 0;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate ctx->buf_off=0;
3370Sstevel@tonic-gate if (i < 0)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate ret_code=0;
3400Sstevel@tonic-gate ctx->buf_len=0;
3410Sstevel@tonic-gate break;
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if (ctx->buf_len <= outl)
3450Sstevel@tonic-gate i=ctx->buf_len;
3460Sstevel@tonic-gate else
3470Sstevel@tonic-gate i=outl;
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate memcpy(out,ctx->buf,i);
3500Sstevel@tonic-gate ret+=i;
3510Sstevel@tonic-gate ctx->buf_off=i;
3520Sstevel@tonic-gate if (ctx->buf_off == ctx->buf_len)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate ctx->buf_len=0;
3550Sstevel@tonic-gate ctx->buf_off=0;
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate outl-=i;
3580Sstevel@tonic-gate out+=i;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate BIO_clear_retry_flags(b);
3610Sstevel@tonic-gate BIO_copy_next_retry(b);
3620Sstevel@tonic-gate return((ret == 0)?ret_code:ret);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate
b64_write(BIO * b,const char * in,int inl)3650Sstevel@tonic-gate static int b64_write(BIO *b, const char *in, int inl)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate int ret=inl,n,i;
3680Sstevel@tonic-gate BIO_B64_CTX *ctx;
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate ctx=(BIO_B64_CTX *)b->ptr;
3710Sstevel@tonic-gate BIO_clear_retry_flags(b);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate if (ctx->encode != B64_ENCODE)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate ctx->encode=B64_ENCODE;
3760Sstevel@tonic-gate ctx->buf_len=0;
3770Sstevel@tonic-gate ctx->buf_off=0;
3780Sstevel@tonic-gate ctx->tmp_len=0;
3790Sstevel@tonic-gate EVP_EncodeInit(&(ctx->base64));
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate n=ctx->buf_len-ctx->buf_off;
3830Sstevel@tonic-gate while (n > 0)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
3860Sstevel@tonic-gate if (i <= 0)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate BIO_copy_next_retry(b);
3890Sstevel@tonic-gate return(i);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate ctx->buf_off+=i;
3920Sstevel@tonic-gate n-=i;
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate /* at this point all pending data has been written */
3950Sstevel@tonic-gate ctx->buf_off=0;
3960Sstevel@tonic-gate ctx->buf_len=0;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if ((in == NULL) || (inl <= 0)) return(0);
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate while (inl > 0)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate if (ctx->tmp_len > 0)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate n=3-ctx->tmp_len;
4090Sstevel@tonic-gate /* There's a teoretical possibility for this */
4100Sstevel@tonic-gate if (n > inl)
4110Sstevel@tonic-gate n=inl;
4120Sstevel@tonic-gate memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
4130Sstevel@tonic-gate ctx->tmp_len+=n;
4140Sstevel@tonic-gate if (ctx->tmp_len < 3)
4150Sstevel@tonic-gate break;
4160Sstevel@tonic-gate ctx->buf_len=EVP_EncodeBlock(
4170Sstevel@tonic-gate (unsigned char *)ctx->buf,
4180Sstevel@tonic-gate (unsigned char *)ctx->tmp,
4190Sstevel@tonic-gate ctx->tmp_len);
4200Sstevel@tonic-gate /* Since we're now done using the temporary
4210Sstevel@tonic-gate buffer, the length should be 0'd */
4220Sstevel@tonic-gate ctx->tmp_len=0;
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate else
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate if (n < 3)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate memcpy(&(ctx->tmp[0]),in,n);
4290Sstevel@tonic-gate ctx->tmp_len=n;
4300Sstevel@tonic-gate break;
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate n-=n%3;
4330Sstevel@tonic-gate ctx->buf_len=EVP_EncodeBlock(
4340Sstevel@tonic-gate (unsigned char *)ctx->buf,
4350Sstevel@tonic-gate (unsigned char *)in,n);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate else
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate EVP_EncodeUpdate(&(ctx->base64),
4410Sstevel@tonic-gate (unsigned char *)ctx->buf,&ctx->buf_len,
4420Sstevel@tonic-gate (unsigned char *)in,n);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate inl-=n;
4450Sstevel@tonic-gate in+=n;
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate ctx->buf_off=0;
4480Sstevel@tonic-gate n=ctx->buf_len;
4490Sstevel@tonic-gate while (n > 0)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
4520Sstevel@tonic-gate if (i <= 0)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate BIO_copy_next_retry(b);
4550Sstevel@tonic-gate return((ret == 0)?i:ret);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate n-=i;
4580Sstevel@tonic-gate ctx->buf_off+=i;
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate ctx->buf_len=0;
4610Sstevel@tonic-gate ctx->buf_off=0;
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate return(ret);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
b64_ctrl(BIO * b,int cmd,long num,void * ptr)4660Sstevel@tonic-gate static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate BIO_B64_CTX *ctx;
4690Sstevel@tonic-gate long ret=1;
4700Sstevel@tonic-gate int i;
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate ctx=(BIO_B64_CTX *)b->ptr;
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate switch (cmd)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate case BIO_CTRL_RESET:
4770Sstevel@tonic-gate ctx->cont=1;
4780Sstevel@tonic-gate ctx->start=1;
4790Sstevel@tonic-gate ctx->encode=B64_NONE;
4800Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4810Sstevel@tonic-gate break;
4820Sstevel@tonic-gate case BIO_CTRL_EOF: /* More to read */
4830Sstevel@tonic-gate if (ctx->cont <= 0)
4840Sstevel@tonic-gate ret=1;
4850Sstevel@tonic-gate else
4860Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4870Sstevel@tonic-gate break;
4880Sstevel@tonic-gate case BIO_CTRL_WPENDING: /* More to write in buffer */
4890Sstevel@tonic-gate ret=ctx->buf_len-ctx->buf_off;
4900Sstevel@tonic-gate if ((ret == 0) && (ctx->encode != B64_NONE)
4910Sstevel@tonic-gate && (ctx->base64.num != 0))
4920Sstevel@tonic-gate ret=1;
4930Sstevel@tonic-gate else if (ret <= 0)
4940Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4950Sstevel@tonic-gate break;
4960Sstevel@tonic-gate case BIO_CTRL_PENDING: /* More to read in buffer */
4970Sstevel@tonic-gate ret=ctx->buf_len-ctx->buf_off;
4980Sstevel@tonic-gate if (ret <= 0)
4990Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
5000Sstevel@tonic-gate break;
5010Sstevel@tonic-gate case BIO_CTRL_FLUSH:
5020Sstevel@tonic-gate /* do a final write */
5030Sstevel@tonic-gate again:
5040Sstevel@tonic-gate while (ctx->buf_len != ctx->buf_off)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate i=b64_write(b,NULL,0);
5070Sstevel@tonic-gate if (i < 0)
5080Sstevel@tonic-gate return i;
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate if (ctx->tmp_len != 0)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate ctx->buf_len=EVP_EncodeBlock(
5150Sstevel@tonic-gate (unsigned char *)ctx->buf,
5160Sstevel@tonic-gate (unsigned char *)ctx->tmp,
5170Sstevel@tonic-gate ctx->tmp_len);
5180Sstevel@tonic-gate ctx->buf_off=0;
5190Sstevel@tonic-gate ctx->tmp_len=0;
5200Sstevel@tonic-gate goto again;
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate else if (ctx->encode != B64_NONE && ctx->base64.num != 0)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate ctx->buf_off=0;
5260Sstevel@tonic-gate EVP_EncodeFinal(&(ctx->base64),
5270Sstevel@tonic-gate (unsigned char *)ctx->buf,
5280Sstevel@tonic-gate &(ctx->buf_len));
5290Sstevel@tonic-gate /* push out the bytes */
5300Sstevel@tonic-gate goto again;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate /* Finally flush the underlying BIO */
5330Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
5340Sstevel@tonic-gate break;
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate case BIO_C_DO_STATE_MACHINE:
5370Sstevel@tonic-gate BIO_clear_retry_flags(b);
5380Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
5390Sstevel@tonic-gate BIO_copy_next_retry(b);
5400Sstevel@tonic-gate break;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate case BIO_CTRL_DUP:
5430Sstevel@tonic-gate break;
5440Sstevel@tonic-gate case BIO_CTRL_INFO:
5450Sstevel@tonic-gate case BIO_CTRL_GET:
5460Sstevel@tonic-gate case BIO_CTRL_SET:
5470Sstevel@tonic-gate default:
5480Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
5490Sstevel@tonic-gate break;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate return(ret);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
b64_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)5540Sstevel@tonic-gate static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate long ret=1;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
5590Sstevel@tonic-gate switch (cmd)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate default:
5620Sstevel@tonic-gate ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
5630Sstevel@tonic-gate break;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate return(ret);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate
568