1*ebfedea0SLionel Sambuc /* demos/b64.c */ 2*ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*ebfedea0SLionel Sambuc * All rights reserved. 4*ebfedea0SLionel Sambuc * 5*ebfedea0SLionel Sambuc * This package is an SSL implementation written 6*ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com). 7*ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL. 8*ebfedea0SLionel Sambuc * 9*ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as 10*ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions 11*ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA, 12*ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms 14*ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*ebfedea0SLionel Sambuc * 16*ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in 17*ebfedea0SLionel Sambuc * the code are not to be removed. 18*ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution 19*ebfedea0SLionel Sambuc * as the author of the parts of the library used. 20*ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or 21*ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package. 22*ebfedea0SLionel Sambuc * 23*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 24*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 25*ebfedea0SLionel Sambuc * are met: 26*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright 27*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 28*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 29*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 30*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 31*ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software 32*ebfedea0SLionel Sambuc * must display the following acknowledgement: 33*ebfedea0SLionel Sambuc * "This product includes cryptographic software written by 34*ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)" 35*ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library 36*ebfedea0SLionel Sambuc * being used are not cryptographic related :-). 37*ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from 38*ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement: 39*ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*ebfedea0SLionel Sambuc * 41*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*ebfedea0SLionel Sambuc * SUCH DAMAGE. 52*ebfedea0SLionel Sambuc * 53*ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or 54*ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be 55*ebfedea0SLionel Sambuc * copied and put under another distribution licence 56*ebfedea0SLionel Sambuc * [including the GNU Public Licence.] 57*ebfedea0SLionel Sambuc */ 58*ebfedea0SLionel Sambuc 59*ebfedea0SLionel Sambuc #include <stdio.h> 60*ebfedea0SLionel Sambuc #include <stdlib.h> 61*ebfedea0SLionel Sambuc #include <string.h> 62*ebfedea0SLionel Sambuc #include "../apps/apps.h" 63*ebfedea0SLionel Sambuc #include <openssl/buffer.h> 64*ebfedea0SLionel Sambuc #include <openssl/err.h> 65*ebfedea0SLionel Sambuc #include <openssl/evp.h> 66*ebfedea0SLionel Sambuc #include <openssl/objects.h> 67*ebfedea0SLionel Sambuc #include <openssl/x509.h> 68*ebfedea0SLionel Sambuc #include <openssl/pem.h> 69*ebfedea0SLionel Sambuc 70*ebfedea0SLionel Sambuc #undef SIZE 71*ebfedea0SLionel Sambuc #undef BSIZE 72*ebfedea0SLionel Sambuc #undef PROG 73*ebfedea0SLionel Sambuc 74*ebfedea0SLionel Sambuc #define SIZE (512) 75*ebfedea0SLionel Sambuc #define BSIZE (8*1024) 76*ebfedea0SLionel Sambuc #define PROG enc_main 77*ebfedea0SLionel Sambuc 78*ebfedea0SLionel Sambuc int main(argc,argv) 79*ebfedea0SLionel Sambuc int argc; 80*ebfedea0SLionel Sambuc char **argv; 81*ebfedea0SLionel Sambuc { 82*ebfedea0SLionel Sambuc char *strbuf=NULL; 83*ebfedea0SLionel Sambuc unsigned char *buff=NULL,*bufsize=NULL; 84*ebfedea0SLionel Sambuc int bsize=BSIZE,verbose=0; 85*ebfedea0SLionel Sambuc int ret=1,inl; 86*ebfedea0SLionel Sambuc char *str=NULL; 87*ebfedea0SLionel Sambuc char *hkey=NULL,*hiv=NULL; 88*ebfedea0SLionel Sambuc int enc=1,printkey=0,i,base64=0; 89*ebfedea0SLionel Sambuc int debug=0; 90*ebfedea0SLionel Sambuc EVP_CIPHER *cipher=NULL,*c; 91*ebfedea0SLionel Sambuc char *inf=NULL,*outf=NULL; 92*ebfedea0SLionel Sambuc BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL; 93*ebfedea0SLionel Sambuc #define PROG_NAME_SIZE 39 94*ebfedea0SLionel Sambuc 95*ebfedea0SLionel Sambuc 96*ebfedea0SLionel Sambuc apps_startup(); 97*ebfedea0SLionel Sambuc 98*ebfedea0SLionel Sambuc if (bio_err == NULL) 99*ebfedea0SLionel Sambuc if ((bio_err=BIO_new(BIO_s_file())) != NULL) 100*ebfedea0SLionel Sambuc BIO_set_fp(bio_err,stderr,BIO_NOCLOSE); 101*ebfedea0SLionel Sambuc 102*ebfedea0SLionel Sambuc base64=1; 103*ebfedea0SLionel Sambuc 104*ebfedea0SLionel Sambuc argc--; 105*ebfedea0SLionel Sambuc argv++; 106*ebfedea0SLionel Sambuc while (argc >= 1) 107*ebfedea0SLionel Sambuc { 108*ebfedea0SLionel Sambuc if (strcmp(*argv,"-e") == 0) 109*ebfedea0SLionel Sambuc enc=1; 110*ebfedea0SLionel Sambuc if (strcmp(*argv,"-in") == 0) 111*ebfedea0SLionel Sambuc { 112*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 113*ebfedea0SLionel Sambuc inf= *(++argv); 114*ebfedea0SLionel Sambuc } 115*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-out") == 0) 116*ebfedea0SLionel Sambuc { 117*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 118*ebfedea0SLionel Sambuc outf= *(++argv); 119*ebfedea0SLionel Sambuc } 120*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-d") == 0) 121*ebfedea0SLionel Sambuc enc=0; 122*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-v") == 0) 123*ebfedea0SLionel Sambuc verbose=1; 124*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-debug") == 0) 125*ebfedea0SLionel Sambuc debug=1; 126*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-bufsize") == 0) 127*ebfedea0SLionel Sambuc { 128*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 129*ebfedea0SLionel Sambuc bufsize=(unsigned char *)*(++argv); 130*ebfedea0SLionel Sambuc } 131*ebfedea0SLionel Sambuc else 132*ebfedea0SLionel Sambuc { 133*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unknown option '%s'\n",*argv); 134*ebfedea0SLionel Sambuc bad: 135*ebfedea0SLionel Sambuc BIO_printf(bio_err,"options are\n"); 136*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%-14s input file\n","-in <file>"); 137*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%-14s output file\n","-out <file>"); 138*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%-14s encode\n","-e"); 139*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%-14s decode\n","-d"); 140*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>"); 141*ebfedea0SLionel Sambuc 142*ebfedea0SLionel Sambuc goto end; 143*ebfedea0SLionel Sambuc } 144*ebfedea0SLionel Sambuc argc--; 145*ebfedea0SLionel Sambuc argv++; 146*ebfedea0SLionel Sambuc } 147*ebfedea0SLionel Sambuc 148*ebfedea0SLionel Sambuc if (bufsize != NULL) 149*ebfedea0SLionel Sambuc { 150*ebfedea0SLionel Sambuc int i; 151*ebfedea0SLionel Sambuc unsigned long n; 152*ebfedea0SLionel Sambuc 153*ebfedea0SLionel Sambuc for (n=0; *bufsize; bufsize++) 154*ebfedea0SLionel Sambuc { 155*ebfedea0SLionel Sambuc i= *bufsize; 156*ebfedea0SLionel Sambuc if ((i <= '9') && (i >= '0')) 157*ebfedea0SLionel Sambuc n=n*10+i-'0'; 158*ebfedea0SLionel Sambuc else if (i == 'k') 159*ebfedea0SLionel Sambuc { 160*ebfedea0SLionel Sambuc n*=1024; 161*ebfedea0SLionel Sambuc bufsize++; 162*ebfedea0SLionel Sambuc break; 163*ebfedea0SLionel Sambuc } 164*ebfedea0SLionel Sambuc } 165*ebfedea0SLionel Sambuc if (*bufsize != '\0') 166*ebfedea0SLionel Sambuc { 167*ebfedea0SLionel Sambuc BIO_printf(bio_err,"invalid 'bufsize' specified.\n"); 168*ebfedea0SLionel Sambuc goto end; 169*ebfedea0SLionel Sambuc } 170*ebfedea0SLionel Sambuc 171*ebfedea0SLionel Sambuc /* It must be large enough for a base64 encoded line */ 172*ebfedea0SLionel Sambuc if (n < 80) n=80; 173*ebfedea0SLionel Sambuc 174*ebfedea0SLionel Sambuc bsize=(int)n; 175*ebfedea0SLionel Sambuc if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize); 176*ebfedea0SLionel Sambuc } 177*ebfedea0SLionel Sambuc 178*ebfedea0SLionel Sambuc strbuf=OPENSSL_malloc(SIZE); 179*ebfedea0SLionel Sambuc buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize)); 180*ebfedea0SLionel Sambuc if ((buff == NULL) || (strbuf == NULL)) 181*ebfedea0SLionel Sambuc { 182*ebfedea0SLionel Sambuc BIO_printf(bio_err,"OPENSSL_malloc failure\n"); 183*ebfedea0SLionel Sambuc goto end; 184*ebfedea0SLionel Sambuc } 185*ebfedea0SLionel Sambuc 186*ebfedea0SLionel Sambuc in=BIO_new(BIO_s_file()); 187*ebfedea0SLionel Sambuc out=BIO_new(BIO_s_file()); 188*ebfedea0SLionel Sambuc if ((in == NULL) || (out == NULL)) 189*ebfedea0SLionel Sambuc { 190*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 191*ebfedea0SLionel Sambuc goto end; 192*ebfedea0SLionel Sambuc } 193*ebfedea0SLionel Sambuc if (debug) 194*ebfedea0SLionel Sambuc { 195*ebfedea0SLionel Sambuc BIO_set_callback(in,BIO_debug_callback); 196*ebfedea0SLionel Sambuc BIO_set_callback(out,BIO_debug_callback); 197*ebfedea0SLionel Sambuc BIO_set_callback_arg(in,bio_err); 198*ebfedea0SLionel Sambuc BIO_set_callback_arg(out,bio_err); 199*ebfedea0SLionel Sambuc } 200*ebfedea0SLionel Sambuc 201*ebfedea0SLionel Sambuc if (inf == NULL) 202*ebfedea0SLionel Sambuc BIO_set_fp(in,stdin,BIO_NOCLOSE); 203*ebfedea0SLionel Sambuc else 204*ebfedea0SLionel Sambuc { 205*ebfedea0SLionel Sambuc if (BIO_read_filename(in,inf) <= 0) 206*ebfedea0SLionel Sambuc { 207*ebfedea0SLionel Sambuc perror(inf); 208*ebfedea0SLionel Sambuc goto end; 209*ebfedea0SLionel Sambuc } 210*ebfedea0SLionel Sambuc } 211*ebfedea0SLionel Sambuc 212*ebfedea0SLionel Sambuc if (outf == NULL) 213*ebfedea0SLionel Sambuc BIO_set_fp(out,stdout,BIO_NOCLOSE); 214*ebfedea0SLionel Sambuc else 215*ebfedea0SLionel Sambuc { 216*ebfedea0SLionel Sambuc if (BIO_write_filename(out,outf) <= 0) 217*ebfedea0SLionel Sambuc { 218*ebfedea0SLionel Sambuc perror(outf); 219*ebfedea0SLionel Sambuc goto end; 220*ebfedea0SLionel Sambuc } 221*ebfedea0SLionel Sambuc } 222*ebfedea0SLionel Sambuc 223*ebfedea0SLionel Sambuc rbio=in; 224*ebfedea0SLionel Sambuc wbio=out; 225*ebfedea0SLionel Sambuc 226*ebfedea0SLionel Sambuc if (base64) 227*ebfedea0SLionel Sambuc { 228*ebfedea0SLionel Sambuc if ((b64=BIO_new(BIO_f_base64())) == NULL) 229*ebfedea0SLionel Sambuc goto end; 230*ebfedea0SLionel Sambuc if (debug) 231*ebfedea0SLionel Sambuc { 232*ebfedea0SLionel Sambuc BIO_set_callback(b64,BIO_debug_callback); 233*ebfedea0SLionel Sambuc BIO_set_callback_arg(b64,bio_err); 234*ebfedea0SLionel Sambuc } 235*ebfedea0SLionel Sambuc if (enc) 236*ebfedea0SLionel Sambuc wbio=BIO_push(b64,wbio); 237*ebfedea0SLionel Sambuc else 238*ebfedea0SLionel Sambuc rbio=BIO_push(b64,rbio); 239*ebfedea0SLionel Sambuc } 240*ebfedea0SLionel Sambuc 241*ebfedea0SLionel Sambuc for (;;) 242*ebfedea0SLionel Sambuc { 243*ebfedea0SLionel Sambuc inl=BIO_read(rbio,(char *)buff,bsize); 244*ebfedea0SLionel Sambuc if (inl <= 0) break; 245*ebfedea0SLionel Sambuc if (BIO_write(wbio,(char *)buff,inl) != inl) 246*ebfedea0SLionel Sambuc { 247*ebfedea0SLionel Sambuc BIO_printf(bio_err,"error writing output file\n"); 248*ebfedea0SLionel Sambuc goto end; 249*ebfedea0SLionel Sambuc } 250*ebfedea0SLionel Sambuc } 251*ebfedea0SLionel Sambuc BIO_flush(wbio); 252*ebfedea0SLionel Sambuc 253*ebfedea0SLionel Sambuc ret=0; 254*ebfedea0SLionel Sambuc if (verbose) 255*ebfedea0SLionel Sambuc { 256*ebfedea0SLionel Sambuc BIO_printf(bio_err,"bytes read :%8ld\n",BIO_number_read(in)); 257*ebfedea0SLionel Sambuc BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out)); 258*ebfedea0SLionel Sambuc } 259*ebfedea0SLionel Sambuc end: 260*ebfedea0SLionel Sambuc if (strbuf != NULL) OPENSSL_free(strbuf); 261*ebfedea0SLionel Sambuc if (buff != NULL) OPENSSL_free(buff); 262*ebfedea0SLionel Sambuc if (in != NULL) BIO_free(in); 263*ebfedea0SLionel Sambuc if (out != NULL) BIO_free(out); 264*ebfedea0SLionel Sambuc if (benc != NULL) BIO_free(benc); 265*ebfedea0SLionel Sambuc if (b64 != NULL) BIO_free(b64); 266*ebfedea0SLionel Sambuc EXIT(ret); 267*ebfedea0SLionel Sambuc } 268*ebfedea0SLionel Sambuc 269