sl@0: /* crypto/evp/bio_b64.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: static int b64_write(BIO *h, const char *buf, int num); sl@0: static int b64_read(BIO *h, char *buf, int size); sl@0: /*static int b64_puts(BIO *h, const char *str); */ sl@0: /*static int b64_gets(BIO *h, char *str, int size); */ sl@0: static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2); sl@0: static int b64_new(BIO *h); sl@0: static int b64_free(BIO *data); sl@0: static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); sl@0: #define B64_BLOCK_SIZE 1024 sl@0: #define B64_BLOCK_SIZE2 768 sl@0: #define B64_NONE 0 sl@0: #define B64_ENCODE 1 sl@0: #define B64_DECODE 2 sl@0: sl@0: typedef struct b64_struct sl@0: { sl@0: /*BIO *bio; moved to the BIO structure */ sl@0: int buf_len; sl@0: int buf_off; sl@0: int tmp_len; /* used to find the start when decoding */ sl@0: int tmp_nl; /* If true, scan until '\n' */ sl@0: int encode; sl@0: int start; /* have we started decoding yet? */ sl@0: int cont; /* <= 0 when finished */ sl@0: EVP_ENCODE_CTX base64; sl@0: char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10]; sl@0: char tmp[B64_BLOCK_SIZE]; sl@0: } BIO_B64_CTX; sl@0: sl@0: #ifndef EMULATOR sl@0: static BIO_METHOD methods_b64= sl@0: { sl@0: BIO_TYPE_BASE64,"base64 encoding", sl@0: b64_write, sl@0: b64_read, sl@0: NULL, /* b64_puts, */ sl@0: NULL, /* b64_gets, */ sl@0: b64_ctrl, sl@0: b64_new, sl@0: b64_free, sl@0: b64_callback_ctrl, sl@0: }; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(methods_b64,bio_b64,BIO_METHOD) sl@0: #define methods_b64 (*GET_WSD_VAR_NAME(methods_b64,bio_b64, s)()) sl@0: const BIO_METHOD temp_s_methods_b64= sl@0: { sl@0: BIO_TYPE_BASE64,"base64 encoding", sl@0: b64_write, sl@0: b64_read, sl@0: NULL, /* b64_puts, */ sl@0: NULL, /* b64_gets, */ sl@0: b64_ctrl, sl@0: b64_new, sl@0: b64_free, sl@0: b64_callback_ctrl, sl@0: }; sl@0: #endif sl@0: EXPORT_C BIO_METHOD *BIO_f_base64(void) sl@0: { sl@0: return(&methods_b64); sl@0: } sl@0: sl@0: static int b64_new(BIO *bi) sl@0: { sl@0: BIO_B64_CTX *ctx; sl@0: sl@0: ctx=(BIO_B64_CTX *)OPENSSL_malloc(sizeof(BIO_B64_CTX)); sl@0: if (ctx == NULL) return(0); sl@0: sl@0: ctx->buf_len=0; sl@0: ctx->tmp_len=0; sl@0: ctx->tmp_nl=0; sl@0: ctx->buf_off=0; sl@0: ctx->cont=1; sl@0: ctx->start=1; sl@0: ctx->encode=0; sl@0: sl@0: bi->init=1; sl@0: bi->ptr=(char *)ctx; sl@0: bi->flags=0; sl@0: return(1); sl@0: } sl@0: sl@0: static int b64_free(BIO *a) sl@0: { sl@0: if (a == NULL) return(0); sl@0: OPENSSL_free(a->ptr); sl@0: a->ptr=NULL; sl@0: a->init=0; sl@0: a->flags=0; sl@0: return(1); sl@0: } sl@0: sl@0: static int b64_read(BIO *b, char *out, int outl) sl@0: { sl@0: int ret=0,i,ii,j,k,x,n,num,ret_code=0; sl@0: BIO_B64_CTX *ctx; sl@0: unsigned char *p,*q; sl@0: sl@0: if (out == NULL) return(0); sl@0: ctx=(BIO_B64_CTX *)b->ptr; sl@0: sl@0: if ((ctx == NULL) || (b->next_bio == NULL)) return(0); sl@0: sl@0: if (ctx->encode != B64_DECODE) sl@0: { sl@0: ctx->encode=B64_DECODE; sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: ctx->tmp_len=0; sl@0: EVP_DecodeInit(&(ctx->base64)); sl@0: } sl@0: sl@0: /* First check if there are bytes decoded/encoded */ sl@0: if (ctx->buf_len > 0) sl@0: { sl@0: i=ctx->buf_len-ctx->buf_off; sl@0: if (i > outl) i=outl; sl@0: OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf)); sl@0: memcpy(out,&(ctx->buf[ctx->buf_off]),i); sl@0: ret=i; sl@0: out+=i; sl@0: outl-=i; sl@0: ctx->buf_off+=i; sl@0: if (ctx->buf_len == ctx->buf_off) sl@0: { sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: } sl@0: } sl@0: sl@0: /* At this point, we have room of outl bytes and an empty sl@0: * buffer, so we should read in some more. */ sl@0: sl@0: ret_code=0; sl@0: while (outl > 0) sl@0: { sl@0: sl@0: if (ctx->cont <= 0) sl@0: break; sl@0: sl@0: i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]), sl@0: B64_BLOCK_SIZE-ctx->tmp_len); sl@0: sl@0: if (i <= 0) sl@0: { sl@0: ret_code=i; sl@0: sl@0: /* Should be continue next time we are called? */ sl@0: if (!BIO_should_retry(b->next_bio)) sl@0: { sl@0: ctx->cont=i; sl@0: /* If buffer empty break */ sl@0: if(ctx->tmp_len == 0) sl@0: break; sl@0: /* Fall through and process what we have */ sl@0: else sl@0: i = 0; sl@0: } sl@0: /* else we retry and add more data to buffer */ sl@0: else sl@0: break; sl@0: } sl@0: i+=ctx->tmp_len; sl@0: ctx->tmp_len = i; sl@0: sl@0: /* We need to scan, a line at a time until we sl@0: * have a valid line if we are starting. */ sl@0: if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) sl@0: { sl@0: /* ctx->start=1; */ sl@0: ctx->tmp_len=0; sl@0: } sl@0: else if (ctx->start) sl@0: { sl@0: q=p=(unsigned char *)ctx->tmp; sl@0: for (j=0; jtmp_nl) sl@0: { sl@0: p=q; sl@0: ctx->tmp_nl=0; sl@0: continue; sl@0: } sl@0: sl@0: k=EVP_DecodeUpdate(&(ctx->base64), sl@0: (unsigned char *)ctx->buf, sl@0: &num,p,q-p); sl@0: if ((k <= 0) && (num == 0) && (ctx->start)) sl@0: EVP_DecodeInit(&ctx->base64); sl@0: else sl@0: { sl@0: if (p != (unsigned char *) sl@0: &(ctx->tmp[0])) sl@0: { sl@0: i-=(p- (unsigned char *) sl@0: &(ctx->tmp[0])); sl@0: for (x=0; x < i; x++) sl@0: ctx->tmp[x]=p[x]; sl@0: } sl@0: EVP_DecodeInit(&ctx->base64); sl@0: ctx->start=0; sl@0: break; sl@0: } sl@0: p=q; sl@0: } sl@0: sl@0: /* we fell off the end without starting */ sl@0: if (j == i) sl@0: { sl@0: /* Is this is one long chunk?, if so, keep on sl@0: * reading until a new line. */ sl@0: if (p == (unsigned char *)&(ctx->tmp[0])) sl@0: { sl@0: /* Check buffer full */ sl@0: if (i == B64_BLOCK_SIZE) sl@0: { sl@0: ctx->tmp_nl=1; sl@0: ctx->tmp_len=0; sl@0: } sl@0: } sl@0: else if (p != q) /* finished on a '\n' */ sl@0: { sl@0: n=q-p; sl@0: for (ii=0; iitmp[ii]=p[ii]; sl@0: ctx->tmp_len=n; sl@0: } sl@0: /* else finished on a '\n' */ sl@0: continue; sl@0: } sl@0: else sl@0: ctx->tmp_len=0; sl@0: } sl@0: /* If buffer isn't full and we can retry then sl@0: * restart to read in more data. sl@0: */ sl@0: else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) sl@0: continue; sl@0: sl@0: if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) sl@0: { sl@0: int z,jj; sl@0: sl@0: jj=(i>>2)<<2; sl@0: z=EVP_DecodeBlock((unsigned char *)ctx->buf, sl@0: (unsigned char *)ctx->tmp,jj); sl@0: if (jj > 2) sl@0: { sl@0: if (ctx->tmp[jj-1] == '=') sl@0: { sl@0: z--; sl@0: if (ctx->tmp[jj-2] == '=') sl@0: z--; sl@0: } sl@0: } sl@0: /* z is now number of output bytes and jj is the sl@0: * number consumed */ sl@0: if (jj != i) sl@0: { sl@0: memcpy((unsigned char *)ctx->tmp, sl@0: (unsigned char *)&(ctx->tmp[jj]),i-jj); sl@0: ctx->tmp_len=i-jj; sl@0: } sl@0: ctx->buf_len=0; sl@0: if (z > 0) sl@0: { sl@0: ctx->buf_len=z; sl@0: i=1; sl@0: } sl@0: else sl@0: i=z; sl@0: } sl@0: else sl@0: { sl@0: i=EVP_DecodeUpdate(&(ctx->base64), sl@0: (unsigned char *)ctx->buf,&ctx->buf_len, sl@0: (unsigned char *)ctx->tmp,i); sl@0: ctx->tmp_len = 0; sl@0: } sl@0: ctx->buf_off=0; sl@0: if (i < 0) sl@0: { sl@0: ret_code=0; sl@0: ctx->buf_len=0; sl@0: break; sl@0: } sl@0: sl@0: if (ctx->buf_len <= outl) sl@0: i=ctx->buf_len; sl@0: else sl@0: i=outl; sl@0: sl@0: memcpy(out,ctx->buf,i); sl@0: ret+=i; sl@0: ctx->buf_off=i; sl@0: if (ctx->buf_off == ctx->buf_len) sl@0: { sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: } sl@0: outl-=i; sl@0: out+=i; sl@0: } sl@0: BIO_clear_retry_flags(b); sl@0: BIO_copy_next_retry(b); sl@0: return((ret == 0)?ret_code:ret); sl@0: } sl@0: sl@0: static int b64_write(BIO *b, const char *in, int inl) sl@0: { sl@0: int ret=inl,n,i; sl@0: BIO_B64_CTX *ctx; sl@0: sl@0: ctx=(BIO_B64_CTX *)b->ptr; sl@0: BIO_clear_retry_flags(b); sl@0: sl@0: if (ctx->encode != B64_ENCODE) sl@0: { sl@0: ctx->encode=B64_ENCODE; sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: ctx->tmp_len=0; sl@0: EVP_EncodeInit(&(ctx->base64)); sl@0: } sl@0: sl@0: n=ctx->buf_len-ctx->buf_off; sl@0: while (n > 0) sl@0: { sl@0: i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return(i); sl@0: } sl@0: ctx->buf_off+=i; sl@0: n-=i; sl@0: } sl@0: /* at this point all pending data has been written */ sl@0: ctx->buf_off=0; sl@0: ctx->buf_len=0; sl@0: sl@0: if ((in == NULL) || (inl <= 0)) return(0); sl@0: sl@0: while (inl > 0) sl@0: { sl@0: n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl; sl@0: sl@0: if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) sl@0: { sl@0: if (ctx->tmp_len > 0) sl@0: { sl@0: n=3-ctx->tmp_len; sl@0: /* There's a teoretical possibility for this */ sl@0: if (n > inl) sl@0: n=inl; sl@0: memcpy(&(ctx->tmp[ctx->tmp_len]),in,n); sl@0: ctx->tmp_len+=n; sl@0: if (ctx->tmp_len < 3) sl@0: break; sl@0: ctx->buf_len=EVP_EncodeBlock( sl@0: (unsigned char *)ctx->buf, sl@0: (unsigned char *)ctx->tmp, sl@0: ctx->tmp_len); sl@0: /* Since we're now done using the temporary sl@0: buffer, the length should be 0'd */ sl@0: ctx->tmp_len=0; sl@0: } sl@0: else sl@0: { sl@0: if (n < 3) sl@0: { sl@0: memcpy(&(ctx->tmp[0]),in,n); sl@0: ctx->tmp_len=n; sl@0: break; sl@0: } sl@0: n-=n%3; sl@0: ctx->buf_len=EVP_EncodeBlock( sl@0: (unsigned char *)ctx->buf, sl@0: (unsigned char *)in,n); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: EVP_EncodeUpdate(&(ctx->base64), sl@0: (unsigned char *)ctx->buf,&ctx->buf_len, sl@0: (unsigned char *)in,n); sl@0: } sl@0: inl-=n; sl@0: in+=n; sl@0: sl@0: ctx->buf_off=0; sl@0: n=ctx->buf_len; sl@0: while (n > 0) sl@0: { sl@0: i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return((ret == 0)?i:ret); sl@0: } sl@0: n-=i; sl@0: ctx->buf_off+=i; sl@0: } sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) sl@0: { sl@0: BIO_B64_CTX *ctx; sl@0: long ret=1; sl@0: int i; sl@0: sl@0: ctx=(BIO_B64_CTX *)b->ptr; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case BIO_CTRL_RESET: sl@0: ctx->cont=1; sl@0: ctx->start=1; sl@0: ctx->encode=B64_NONE; sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_EOF: /* More to read */ sl@0: if (ctx->cont <= 0) sl@0: ret=1; sl@0: else sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_WPENDING: /* More to write in buffer */ sl@0: ret=ctx->buf_len-ctx->buf_off; sl@0: if ((ret == 0) && (ctx->encode != B64_NONE) sl@0: && (ctx->base64.num != 0)) sl@0: ret=1; sl@0: else if (ret <= 0) sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_PENDING: /* More to read in buffer */ sl@0: ret=ctx->buf_len-ctx->buf_off; sl@0: if (ret <= 0) sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_FLUSH: sl@0: /* do a final write */ sl@0: again: sl@0: while (ctx->buf_len != ctx->buf_off) sl@0: { sl@0: i=b64_write(b,NULL,0); sl@0: if (i < 0) sl@0: return i; sl@0: } sl@0: if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) sl@0: { sl@0: if (ctx->tmp_len != 0) sl@0: { sl@0: ctx->buf_len=EVP_EncodeBlock( sl@0: (unsigned char *)ctx->buf, sl@0: (unsigned char *)ctx->tmp, sl@0: ctx->tmp_len); sl@0: ctx->buf_off=0; sl@0: ctx->tmp_len=0; sl@0: goto again; sl@0: } sl@0: } sl@0: else if (ctx->encode != B64_NONE && ctx->base64.num != 0) sl@0: { sl@0: ctx->buf_off=0; sl@0: EVP_EncodeFinal(&(ctx->base64), sl@0: (unsigned char *)ctx->buf, sl@0: &(ctx->buf_len)); sl@0: /* push out the bytes */ sl@0: goto again; sl@0: } sl@0: /* Finally flush the underlying BIO */ sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: sl@0: case BIO_C_DO_STATE_MACHINE: sl@0: BIO_clear_retry_flags(b); sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: BIO_copy_next_retry(b); sl@0: break; sl@0: sl@0: case BIO_CTRL_DUP: sl@0: break; sl@0: case BIO_CTRL_INFO: sl@0: case BIO_CTRL_GET: sl@0: case BIO_CTRL_SET: sl@0: default: sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) sl@0: { sl@0: long ret=1; sl@0: sl@0: if (b->next_bio == NULL) return(0); sl@0: switch (cmd) sl@0: { sl@0: default: sl@0: ret=BIO_callback_ctrl(b->next_bio,cmd,fp); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: