10Sstevel@tonic-gate /* crypto/evp/encode.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 "cryptlib.h" 610Sstevel@tonic-gate #include <openssl/evp.h> 620Sstevel@tonic-gate 630Sstevel@tonic-gate #ifndef CHARSET_EBCDIC 640Sstevel@tonic-gate #define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f]) 650Sstevel@tonic-gate #define conv_ascii2bin(a) (data_ascii2bin[(a)&0x7f]) 660Sstevel@tonic-gate #else 670Sstevel@tonic-gate /* We assume that PEM encoded files are EBCDIC files 680Sstevel@tonic-gate * (i.e., printable text files). Convert them here while decoding. 690Sstevel@tonic-gate * When encoding, output is EBCDIC (text) format again. 700Sstevel@tonic-gate * (No need for conversion in the conv_bin2ascii macro, as the 710Sstevel@tonic-gate * underlying textstring data_bin2ascii[] is already EBCDIC) 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate #define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f]) 740Sstevel@tonic-gate #define conv_ascii2bin(a) (data_ascii2bin[os_toascii[a]&0x7f]) 750Sstevel@tonic-gate #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 64 char lines 780Sstevel@tonic-gate * pad input with 0 790Sstevel@tonic-gate * left over chars are set to = 800Sstevel@tonic-gate * 1 byte => xx== 810Sstevel@tonic-gate * 2 bytes => xxx= 820Sstevel@tonic-gate * 3 bytes => xxxx 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate #define BIN_PER_LINE (64/4*3) 850Sstevel@tonic-gate #define CHUNKS_PER_LINE (64/4) 860Sstevel@tonic-gate #define CHAR_PER_LINE (64+1) 870Sstevel@tonic-gate 880Sstevel@tonic-gate static unsigned char data_bin2ascii[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\ 890Sstevel@tonic-gate abcdefghijklmnopqrstuvwxyz0123456789+/"; 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* 0xF0 is a EOLN 920Sstevel@tonic-gate * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing). 930Sstevel@tonic-gate * 0xF2 is EOF 940Sstevel@tonic-gate * 0xE0 is ignore at start of line. 950Sstevel@tonic-gate * 0xFF is error 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate 980Sstevel@tonic-gate #define B64_EOLN 0xF0 990Sstevel@tonic-gate #define B64_CR 0xF1 1000Sstevel@tonic-gate #define B64_EOF 0xF2 1010Sstevel@tonic-gate #define B64_WS 0xE0 1020Sstevel@tonic-gate #define B64_ERROR 0xFF 1030Sstevel@tonic-gate #define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3) 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static unsigned char data_ascii2bin[128]={ 1060Sstevel@tonic-gate 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 1070Sstevel@tonic-gate 0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF, 1080Sstevel@tonic-gate 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 1090Sstevel@tonic-gate 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 1100Sstevel@tonic-gate 0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 1110Sstevel@tonic-gate 0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F, 1120Sstevel@tonic-gate 0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B, 1130Sstevel@tonic-gate 0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF, 1140Sstevel@tonic-gate 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, 1150Sstevel@tonic-gate 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, 1160Sstevel@tonic-gate 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, 1170Sstevel@tonic-gate 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, 1180Sstevel@tonic-gate 0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20, 1190Sstevel@tonic-gate 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, 1200Sstevel@tonic-gate 0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30, 1210Sstevel@tonic-gate 0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF, 1220Sstevel@tonic-gate }; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate ctx->length=48; 1270Sstevel@tonic-gate ctx->num=0; 1280Sstevel@tonic-gate ctx->line_num=0; 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl, 132*2139Sjp161948 const unsigned char *in, int inl) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate int i,j; 1350Sstevel@tonic-gate unsigned int total=0; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate *outl=0; 1380Sstevel@tonic-gate if (inl == 0) return; 139*2139Sjp161948 OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data)); 1400Sstevel@tonic-gate if ((ctx->num+inl) < ctx->length) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate memcpy(&(ctx->enc_data[ctx->num]),in,inl); 1430Sstevel@tonic-gate ctx->num+=inl; 1440Sstevel@tonic-gate return; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate if (ctx->num != 0) 1470Sstevel@tonic-gate { 1480Sstevel@tonic-gate i=ctx->length-ctx->num; 1490Sstevel@tonic-gate memcpy(&(ctx->enc_data[ctx->num]),in,i); 1500Sstevel@tonic-gate in+=i; 1510Sstevel@tonic-gate inl-=i; 1520Sstevel@tonic-gate j=EVP_EncodeBlock(out,ctx->enc_data,ctx->length); 1530Sstevel@tonic-gate ctx->num=0; 1540Sstevel@tonic-gate out+=j; 1550Sstevel@tonic-gate *(out++)='\n'; 1560Sstevel@tonic-gate *out='\0'; 1570Sstevel@tonic-gate total=j+1; 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate while (inl >= ctx->length) 1600Sstevel@tonic-gate { 1610Sstevel@tonic-gate j=EVP_EncodeBlock(out,in,ctx->length); 1620Sstevel@tonic-gate in+=ctx->length; 1630Sstevel@tonic-gate inl-=ctx->length; 1640Sstevel@tonic-gate out+=j; 1650Sstevel@tonic-gate *(out++)='\n'; 1660Sstevel@tonic-gate *out='\0'; 1670Sstevel@tonic-gate total+=j+1; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate if (inl != 0) 1700Sstevel@tonic-gate memcpy(&(ctx->enc_data[0]),in,inl); 1710Sstevel@tonic-gate ctx->num=inl; 1720Sstevel@tonic-gate *outl=total; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl) 1760Sstevel@tonic-gate { 1770Sstevel@tonic-gate unsigned int ret=0; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if (ctx->num != 0) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate ret=EVP_EncodeBlock(out,ctx->enc_data,ctx->num); 1820Sstevel@tonic-gate out[ret++]='\n'; 1830Sstevel@tonic-gate out[ret]='\0'; 1840Sstevel@tonic-gate ctx->num=0; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate *outl=ret; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate int i,ret=0; 1920Sstevel@tonic-gate unsigned long l; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate for (i=dlen; i > 0; i-=3) 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate if (i >= 3) 1970Sstevel@tonic-gate { 1980Sstevel@tonic-gate l= (((unsigned long)f[0])<<16L)| 1990Sstevel@tonic-gate (((unsigned long)f[1])<< 8L)|f[2]; 2000Sstevel@tonic-gate *(t++)=conv_bin2ascii(l>>18L); 2010Sstevel@tonic-gate *(t++)=conv_bin2ascii(l>>12L); 2020Sstevel@tonic-gate *(t++)=conv_bin2ascii(l>> 6L); 2030Sstevel@tonic-gate *(t++)=conv_bin2ascii(l ); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate else 2060Sstevel@tonic-gate { 2070Sstevel@tonic-gate l=((unsigned long)f[0])<<16L; 2080Sstevel@tonic-gate if (i == 2) l|=((unsigned long)f[1]<<8L); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate *(t++)=conv_bin2ascii(l>>18L); 2110Sstevel@tonic-gate *(t++)=conv_bin2ascii(l>>12L); 2120Sstevel@tonic-gate *(t++)=(i == 1)?'=':conv_bin2ascii(l>> 6L); 2130Sstevel@tonic-gate *(t++)='='; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate ret+=4; 2160Sstevel@tonic-gate f+=3; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate *t='\0'; 2200Sstevel@tonic-gate return(ret); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate ctx->length=30; 2260Sstevel@tonic-gate ctx->num=0; 2270Sstevel@tonic-gate ctx->line_num=0; 2280Sstevel@tonic-gate ctx->expect_nl=0; 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* -1 for error 2320Sstevel@tonic-gate * 0 for last line 2330Sstevel@tonic-gate * 1 for full line 2340Sstevel@tonic-gate */ 2350Sstevel@tonic-gate int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl, 236*2139Sjp161948 const unsigned char *in, int inl) 2370Sstevel@tonic-gate { 2380Sstevel@tonic-gate int seof= -1,eof=0,rv= -1,ret=0,i,v,tmp,n,ln,tmp2,exp_nl; 2390Sstevel@tonic-gate unsigned char *d; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate n=ctx->num; 2420Sstevel@tonic-gate d=ctx->enc_data; 2430Sstevel@tonic-gate ln=ctx->line_num; 2440Sstevel@tonic-gate exp_nl=ctx->expect_nl; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* last line of input. */ 2470Sstevel@tonic-gate if ((inl == 0) || ((n == 0) && (conv_ascii2bin(in[0]) == B64_EOF))) 2480Sstevel@tonic-gate { rv=0; goto end; } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* We parse the input data */ 2510Sstevel@tonic-gate for (i=0; i<inl; i++) 2520Sstevel@tonic-gate { 2530Sstevel@tonic-gate /* If the current line is > 80 characters, scream alot */ 2540Sstevel@tonic-gate if (ln >= 80) { rv= -1; goto end; } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* Get char and put it into the buffer */ 2570Sstevel@tonic-gate tmp= *(in++); 2580Sstevel@tonic-gate v=conv_ascii2bin(tmp); 2590Sstevel@tonic-gate /* only save the good data :-) */ 2600Sstevel@tonic-gate if (!B64_NOT_BASE64(v)) 2610Sstevel@tonic-gate { 262*2139Sjp161948 OPENSSL_assert(n < (int)sizeof(ctx->enc_data)); 2630Sstevel@tonic-gate d[n++]=tmp; 2640Sstevel@tonic-gate ln++; 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate else if (v == B64_ERROR) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate rv= -1; 2690Sstevel@tonic-gate goto end; 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* have we seen a '=' which is 'definitly' the last 2730Sstevel@tonic-gate * input line. seof will point to the character that 2740Sstevel@tonic-gate * holds it. and eof will hold how many characters to 2750Sstevel@tonic-gate * chop off. */ 2760Sstevel@tonic-gate if (tmp == '=') 2770Sstevel@tonic-gate { 2780Sstevel@tonic-gate if (seof == -1) seof=n; 2790Sstevel@tonic-gate eof++; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if (v == B64_CR) 2830Sstevel@tonic-gate { 2840Sstevel@tonic-gate ln = 0; 2850Sstevel@tonic-gate if (exp_nl) 2860Sstevel@tonic-gate continue; 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* eoln */ 2900Sstevel@tonic-gate if (v == B64_EOLN) 2910Sstevel@tonic-gate { 2920Sstevel@tonic-gate ln=0; 2930Sstevel@tonic-gate if (exp_nl) 2940Sstevel@tonic-gate { 2950Sstevel@tonic-gate exp_nl=0; 2960Sstevel@tonic-gate continue; 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate exp_nl=0; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* If we are at the end of input and it looks like a 3020Sstevel@tonic-gate * line, process it. */ 3030Sstevel@tonic-gate if (((i+1) == inl) && (((n&3) == 0) || eof)) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate v=B64_EOF; 3060Sstevel@tonic-gate /* In case things were given us in really small 3070Sstevel@tonic-gate records (so two '=' were given in separate 3080Sstevel@tonic-gate updates), eof may contain the incorrect number 3090Sstevel@tonic-gate of ending bytes to skip, so let's redo the count */ 3100Sstevel@tonic-gate eof = 0; 3110Sstevel@tonic-gate if (d[n-1] == '=') eof++; 3120Sstevel@tonic-gate if (d[n-2] == '=') eof++; 3130Sstevel@tonic-gate /* There will never be more than two '=' */ 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 316*2139Sjp161948 if ((v == B64_EOF && (n&3) == 0) || (n >= 64)) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate /* This is needed to work correctly on 64 byte input 3190Sstevel@tonic-gate * lines. We process the line and then need to 3200Sstevel@tonic-gate * accept the '\n' */ 3210Sstevel@tonic-gate if ((v != B64_EOF) && (n >= 64)) exp_nl=1; 3220Sstevel@tonic-gate tmp2=v; 3230Sstevel@tonic-gate if (n > 0) 3240Sstevel@tonic-gate { 3250Sstevel@tonic-gate v=EVP_DecodeBlock(out,d,n); 326*2139Sjp161948 n=0; 3270Sstevel@tonic-gate if (v < 0) { rv=0; goto end; } 3280Sstevel@tonic-gate ret+=(v-eof); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate else 3310Sstevel@tonic-gate { 3320Sstevel@tonic-gate eof=1; 3330Sstevel@tonic-gate v=0; 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* This is the case where we have had a short 3370Sstevel@tonic-gate * but valid input line */ 3380Sstevel@tonic-gate if ((v < ctx->length) && eof) 3390Sstevel@tonic-gate { 3400Sstevel@tonic-gate rv=0; 3410Sstevel@tonic-gate goto end; 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate else 3440Sstevel@tonic-gate ctx->length=v; 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate if (seof >= 0) { rv=0; goto end; } 3470Sstevel@tonic-gate out+=v; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate rv=1; 3510Sstevel@tonic-gate end: 3520Sstevel@tonic-gate *outl=ret; 3530Sstevel@tonic-gate ctx->num=n; 3540Sstevel@tonic-gate ctx->line_num=ln; 3550Sstevel@tonic-gate ctx->expect_nl=exp_nl; 3560Sstevel@tonic-gate return(rv); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n) 3600Sstevel@tonic-gate { 3610Sstevel@tonic-gate int i,ret=0,a,b,c,d; 3620Sstevel@tonic-gate unsigned long l; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* trim white space from the start of the line. */ 3650Sstevel@tonic-gate while ((conv_ascii2bin(*f) == B64_WS) && (n > 0)) 3660Sstevel@tonic-gate { 3670Sstevel@tonic-gate f++; 3680Sstevel@tonic-gate n--; 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* strip off stuff at the end of the line 3720Sstevel@tonic-gate * ascii2bin values B64_WS, B64_EOLN, B64_EOLN and B64_EOF */ 3730Sstevel@tonic-gate while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n-1])))) 3740Sstevel@tonic-gate n--; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate if (n%4 != 0) return(-1); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate for (i=0; i<n; i+=4) 3790Sstevel@tonic-gate { 3800Sstevel@tonic-gate a=conv_ascii2bin(*(f++)); 3810Sstevel@tonic-gate b=conv_ascii2bin(*(f++)); 3820Sstevel@tonic-gate c=conv_ascii2bin(*(f++)); 3830Sstevel@tonic-gate d=conv_ascii2bin(*(f++)); 3840Sstevel@tonic-gate if ( (a & 0x80) || (b & 0x80) || 3850Sstevel@tonic-gate (c & 0x80) || (d & 0x80)) 3860Sstevel@tonic-gate return(-1); 3870Sstevel@tonic-gate l=( (((unsigned long)a)<<18L)| 3880Sstevel@tonic-gate (((unsigned long)b)<<12L)| 3890Sstevel@tonic-gate (((unsigned long)c)<< 6L)| 3900Sstevel@tonic-gate (((unsigned long)d) )); 3910Sstevel@tonic-gate *(t++)=(unsigned char)(l>>16L)&0xff; 3920Sstevel@tonic-gate *(t++)=(unsigned char)(l>> 8L)&0xff; 3930Sstevel@tonic-gate *(t++)=(unsigned char)(l )&0xff; 3940Sstevel@tonic-gate ret+=3; 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate return(ret); 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl) 4000Sstevel@tonic-gate { 4010Sstevel@tonic-gate int i; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate *outl=0; 4040Sstevel@tonic-gate if (ctx->num != 0) 4050Sstevel@tonic-gate { 4060Sstevel@tonic-gate i=EVP_DecodeBlock(out,ctx->enc_data,ctx->num); 4070Sstevel@tonic-gate if (i < 0) return(-1); 4080Sstevel@tonic-gate ctx->num=0; 4090Sstevel@tonic-gate *outl=i; 4100Sstevel@tonic-gate return(1); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate else 4130Sstevel@tonic-gate return(1); 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate #ifdef undef 4170Sstevel@tonic-gate int EVP_DecodeValid(unsigned char *buf, int len) 4180Sstevel@tonic-gate { 4190Sstevel@tonic-gate int i,num=0,bad=0; 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate if (len == 0) return(-1); 4220Sstevel@tonic-gate while (conv_ascii2bin(*buf) == B64_WS) 4230Sstevel@tonic-gate { 4240Sstevel@tonic-gate buf++; 4250Sstevel@tonic-gate len--; 4260Sstevel@tonic-gate if (len == 0) return(-1); 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate for (i=len; i >= 4; i-=4) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate if ( (conv_ascii2bin(buf[0]) >= 0x40) || 4320Sstevel@tonic-gate (conv_ascii2bin(buf[1]) >= 0x40) || 4330Sstevel@tonic-gate (conv_ascii2bin(buf[2]) >= 0x40) || 4340Sstevel@tonic-gate (conv_ascii2bin(buf[3]) >= 0x40)) 4350Sstevel@tonic-gate return(-1); 4360Sstevel@tonic-gate buf+=4; 4370Sstevel@tonic-gate num+=1+(buf[2] != '=')+(buf[3] != '='); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate if ((i == 1) && (conv_ascii2bin(buf[0]) == B64_EOLN)) 4400Sstevel@tonic-gate return(num); 4410Sstevel@tonic-gate if ((i == 2) && (conv_ascii2bin(buf[0]) == B64_EOLN) && 4420Sstevel@tonic-gate (conv_ascii2bin(buf[0]) == B64_EOLN)) 4430Sstevel@tonic-gate return(num); 4440Sstevel@tonic-gate return(1); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate #endif 447