sl@0: /* crypto/evp/encode.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: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include "cryptlib.h" 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: #ifndef CHARSET_EBCDIC sl@0: #define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f]) sl@0: #define conv_ascii2bin(a) (data_ascii2bin[(a)&0x7f]) sl@0: #else sl@0: /* We assume that PEM encoded files are EBCDIC files sl@0: * (i.e., printable text files). Convert them here while decoding. sl@0: * When encoding, output is EBCDIC (text) format again. sl@0: * (No need for conversion in the conv_bin2ascii macro, as the sl@0: * underlying textstring data_bin2ascii[] is already EBCDIC) sl@0: */ sl@0: #define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f]) sl@0: #define conv_ascii2bin(a) (data_ascii2bin[os_toascii[a]&0x7f]) sl@0: #endif sl@0: sl@0: /* 64 char lines sl@0: * pad input with 0 sl@0: * left over chars are set to = sl@0: * 1 byte => xx== sl@0: * 2 bytes => xxx= sl@0: * 3 bytes => xxxx sl@0: */ sl@0: #define BIN_PER_LINE (64/4*3) sl@0: #define CHUNKS_PER_LINE (64/4) sl@0: #define CHAR_PER_LINE (64+1) sl@0: sl@0: #ifndef EMULATOR sl@0: static unsigned char data_bin2ascii[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\ sl@0: abcdefghijklmnopqrstuvwxyz0123456789+/"; sl@0: #else sl@0: static const unsigned char data_bin2ascii[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\ sl@0: abcdefghijklmnopqrstuvwxyz0123456789+/"; sl@0: #endif sl@0: sl@0: /* 0xF0 is a EOLN sl@0: * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing). sl@0: * 0xF2 is EOF sl@0: * 0xE0 is ignore at start of line. sl@0: * 0xFF is error sl@0: */ sl@0: sl@0: #define B64_EOLN 0xF0 sl@0: #define B64_CR 0xF1 sl@0: #define B64_EOF 0xF2 sl@0: #define B64_WS 0xE0 sl@0: #define B64_ERROR 0xFF sl@0: #define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3) sl@0: sl@0: #ifndef EMULATOR sl@0: static unsigned char data_ascii2bin[128]={ sl@0: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF, sl@0: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F, sl@0: 0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B, sl@0: 0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF, sl@0: 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, sl@0: 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, sl@0: 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, sl@0: 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20, sl@0: 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, sl@0: 0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30, sl@0: 0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: }; sl@0: sl@0: #else sl@0: static const unsigned char data_ascii2bin[128]={ sl@0: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF, sl@0: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F, sl@0: 0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B, sl@0: 0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF, sl@0: 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, sl@0: 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, sl@0: 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, sl@0: 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: 0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20, sl@0: 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, sl@0: 0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30, sl@0: 0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF, sl@0: }; sl@0: #endif sl@0: sl@0: EXPORT_C void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) sl@0: { sl@0: ctx->length=48; sl@0: ctx->num=0; sl@0: ctx->line_num=0; sl@0: } sl@0: sl@0: EXPORT_C void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl, sl@0: const unsigned char *in, int inl) sl@0: { sl@0: int i,j; sl@0: unsigned int total=0; sl@0: sl@0: *outl=0; sl@0: if (inl == 0) return; sl@0: OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data)); sl@0: if ((ctx->num+inl) < ctx->length) sl@0: { sl@0: memcpy(&(ctx->enc_data[ctx->num]),in,inl); sl@0: ctx->num+=inl; sl@0: return; sl@0: } sl@0: if (ctx->num != 0) sl@0: { sl@0: i=ctx->length-ctx->num; sl@0: memcpy(&(ctx->enc_data[ctx->num]),in,i); sl@0: in+=i; sl@0: inl-=i; sl@0: j=EVP_EncodeBlock(out,ctx->enc_data,ctx->length); sl@0: ctx->num=0; sl@0: out+=j; sl@0: *(out++)='\n'; sl@0: *out='\0'; sl@0: total=j+1; sl@0: } sl@0: while (inl >= ctx->length) sl@0: { sl@0: j=EVP_EncodeBlock(out,in,ctx->length); sl@0: in+=ctx->length; sl@0: inl-=ctx->length; sl@0: out+=j; sl@0: *(out++)='\n'; sl@0: *out='\0'; sl@0: total+=j+1; sl@0: } sl@0: if (inl != 0) sl@0: memcpy(&(ctx->enc_data[0]),in,inl); sl@0: ctx->num=inl; sl@0: *outl=total; sl@0: } sl@0: sl@0: EXPORT_C void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: unsigned int ret=0; sl@0: sl@0: if (ctx->num != 0) sl@0: { sl@0: ret=EVP_EncodeBlock(out,ctx->enc_data,ctx->num); sl@0: out[ret++]='\n'; sl@0: out[ret]='\0'; sl@0: ctx->num=0; sl@0: } sl@0: *outl=ret; sl@0: } sl@0: sl@0: EXPORT_C int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen) sl@0: { sl@0: int i,ret=0; sl@0: unsigned long l; sl@0: sl@0: for (i=dlen; i > 0; i-=3) sl@0: { sl@0: if (i >= 3) sl@0: { sl@0: l= (((unsigned long)f[0])<<16L)| sl@0: (((unsigned long)f[1])<< 8L)|f[2]; sl@0: *(t++)=conv_bin2ascii(l>>18L); sl@0: *(t++)=conv_bin2ascii(l>>12L); sl@0: *(t++)=conv_bin2ascii(l>> 6L); sl@0: *(t++)=conv_bin2ascii(l ); sl@0: } sl@0: else sl@0: { sl@0: l=((unsigned long)f[0])<<16L; sl@0: if (i == 2) l|=((unsigned long)f[1]<<8L); sl@0: sl@0: *(t++)=conv_bin2ascii(l>>18L); sl@0: *(t++)=conv_bin2ascii(l>>12L); sl@0: *(t++)=(i == 1)?'=':conv_bin2ascii(l>> 6L); sl@0: *(t++)='='; sl@0: } sl@0: ret+=4; sl@0: f+=3; sl@0: } sl@0: sl@0: *t='\0'; sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) sl@0: { sl@0: ctx->length=30; sl@0: ctx->num=0; sl@0: ctx->line_num=0; sl@0: ctx->expect_nl=0; sl@0: } sl@0: sl@0: /* -1 for error sl@0: * 0 for last line sl@0: * 1 for full line sl@0: */ sl@0: EXPORT_C int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl, sl@0: const unsigned char *in, int inl) sl@0: { sl@0: int seof= -1,eof=0,rv= -1,ret=0,i,v,tmp,n,ln,tmp2,exp_nl; sl@0: unsigned char *d; sl@0: sl@0: n=ctx->num; sl@0: d=ctx->enc_data; sl@0: ln=ctx->line_num; sl@0: exp_nl=ctx->expect_nl; sl@0: sl@0: /* last line of input. */ sl@0: if ((inl == 0) || ((n == 0) && (conv_ascii2bin(in[0]) == B64_EOF))) sl@0: { rv=0; goto end; } sl@0: sl@0: /* We parse the input data */ sl@0: for (i=0; i 80 characters, scream alot */ sl@0: if (ln >= 80) { rv= -1; goto end; } sl@0: sl@0: /* Get char and put it into the buffer */ sl@0: tmp= *(in++); sl@0: v=conv_ascii2bin(tmp); sl@0: /* only save the good data :-) */ sl@0: if (!B64_NOT_BASE64(v)) sl@0: { sl@0: OPENSSL_assert(n < (int)sizeof(ctx->enc_data)); sl@0: d[n++]=tmp; sl@0: ln++; sl@0: } sl@0: else if (v == B64_ERROR) sl@0: { sl@0: rv= -1; sl@0: goto end; sl@0: } sl@0: sl@0: /* have we seen a '=' which is 'definitly' the last sl@0: * input line. seof will point to the character that sl@0: * holds it. and eof will hold how many characters to sl@0: * chop off. */ sl@0: if (tmp == '=') sl@0: { sl@0: if (seof == -1) seof=n; sl@0: eof++; sl@0: } sl@0: sl@0: if (v == B64_CR) sl@0: { sl@0: ln = 0; sl@0: if (exp_nl) sl@0: continue; sl@0: } sl@0: sl@0: /* eoln */ sl@0: if (v == B64_EOLN) sl@0: { sl@0: ln=0; sl@0: if (exp_nl) sl@0: { sl@0: exp_nl=0; sl@0: continue; sl@0: } sl@0: } sl@0: exp_nl=0; sl@0: sl@0: /* If we are at the end of input and it looks like a sl@0: * line, process it. */ sl@0: if (((i+1) == inl) && (((n&3) == 0) || eof)) sl@0: { sl@0: v=B64_EOF; sl@0: /* In case things were given us in really small sl@0: records (so two '=' were given in separate sl@0: updates), eof may contain the incorrect number sl@0: of ending bytes to skip, so let's redo the count */ sl@0: eof = 0; sl@0: if (d[n-1] == '=') eof++; sl@0: if (d[n-2] == '=') eof++; sl@0: /* There will never be more than two '=' */ sl@0: } sl@0: sl@0: if ((v == B64_EOF && (n&3) == 0) || (n >= 64)) sl@0: { sl@0: /* This is needed to work correctly on 64 byte input sl@0: * lines. We process the line and then need to sl@0: * accept the '\n' */ sl@0: if ((v != B64_EOF) && (n >= 64)) exp_nl=1; sl@0: tmp2=v; sl@0: if (n > 0) sl@0: { sl@0: v=EVP_DecodeBlock(out,d,n); sl@0: n=0; sl@0: if (v < 0) { rv=0; goto end; } sl@0: ret+=(v-eof); sl@0: } sl@0: else sl@0: { sl@0: eof=1; sl@0: v=0; sl@0: } sl@0: sl@0: /* This is the case where we have had a short sl@0: * but valid input line */ sl@0: if ((v < ctx->length) && eof) sl@0: { sl@0: rv=0; sl@0: goto end; sl@0: } sl@0: else sl@0: ctx->length=v; sl@0: sl@0: if (seof >= 0) { rv=0; goto end; } sl@0: out+=v; sl@0: } sl@0: } sl@0: rv=1; sl@0: end: sl@0: *outl=ret; sl@0: ctx->num=n; sl@0: ctx->line_num=ln; sl@0: ctx->expect_nl=exp_nl; sl@0: return(rv); sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n) sl@0: { sl@0: int i,ret=0,a,b,c,d; sl@0: unsigned long l; sl@0: sl@0: /* trim white space from the start of the line. */ sl@0: while ((conv_ascii2bin(*f) == B64_WS) && (n > 0)) sl@0: { sl@0: f++; sl@0: n--; sl@0: } sl@0: sl@0: /* strip off stuff at the end of the line sl@0: * ascii2bin values B64_WS, B64_EOLN, B64_EOLN and B64_EOF */ sl@0: while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n-1])))) sl@0: n--; sl@0: sl@0: if (n%4 != 0) return(-1); sl@0: sl@0: for (i=0; i>16L)&0xff; sl@0: *(t++)=(unsigned char)(l>> 8L)&0xff; sl@0: *(t++)=(unsigned char)(l )&0xff; sl@0: ret+=3; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: int i; sl@0: sl@0: *outl=0; sl@0: if (ctx->num != 0) sl@0: { sl@0: i=EVP_DecodeBlock(out,ctx->enc_data,ctx->num); sl@0: if (i < 0) return(-1); sl@0: ctx->num=0; sl@0: *outl=i; sl@0: return(1); sl@0: } sl@0: else sl@0: return(1); sl@0: } sl@0: sl@0: #ifdef undef sl@0: EXPORT_C int EVP_DecodeValid(unsigned char *buf, int len) sl@0: { sl@0: int i,num=0,bad=0; sl@0: sl@0: if (len == 0) return(-1); sl@0: while (conv_ascii2bin(*buf) == B64_WS) sl@0: { sl@0: buf++; sl@0: len--; sl@0: if (len == 0) return(-1); sl@0: } sl@0: sl@0: for (i=len; i >= 4; i-=4) sl@0: { sl@0: if ( (conv_ascii2bin(buf[0]) >= 0x40) || sl@0: (conv_ascii2bin(buf[1]) >= 0x40) || sl@0: (conv_ascii2bin(buf[2]) >= 0x40) || sl@0: (conv_ascii2bin(buf[3]) >= 0x40)) sl@0: return(-1); sl@0: buf+=4; sl@0: num+=1+(buf[2] != '=')+(buf[3] != '='); sl@0: } sl@0: if ((i == 1) && (conv_ascii2bin(buf[0]) == B64_EOLN)) sl@0: return(num); sl@0: if ((i == 2) && (conv_ascii2bin(buf[0]) == B64_EOLN) && sl@0: (conv_ascii2bin(buf[0]) == B64_EOLN)) sl@0: return(num); sl@0: return(1); sl@0: } sl@0: #endif