174664626SKris Kennaway /* apps/enc.c */ 274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 374664626SKris Kennaway * All rights reserved. 474664626SKris Kennaway * 574664626SKris Kennaway * This package is an SSL implementation written 674664626SKris Kennaway * by Eric Young (eay@cryptsoft.com). 774664626SKris Kennaway * The implementation was written so as to conform with Netscapes SSL. 874664626SKris Kennaway * 974664626SKris Kennaway * This library is free for commercial and non-commercial use as long as 1074664626SKris Kennaway * the following conditions are aheared to. The following conditions 1174664626SKris Kennaway * apply to all code found in this distribution, be it the RC4, RSA, 1274664626SKris Kennaway * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1374664626SKris Kennaway * included with this distribution is covered by the same copyright terms 1474664626SKris Kennaway * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1574664626SKris Kennaway * 1674664626SKris Kennaway * Copyright remains Eric Young's, and as such any Copyright notices in 1774664626SKris Kennaway * the code are not to be removed. 1874664626SKris Kennaway * If this package is used in a product, Eric Young should be given attribution 1974664626SKris Kennaway * as the author of the parts of the library used. 2074664626SKris Kennaway * This can be in the form of a textual message at program startup or 2174664626SKris Kennaway * in documentation (online or textual) provided with the package. 2274664626SKris Kennaway * 2374664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 2474664626SKris Kennaway * modification, are permitted provided that the following conditions 2574664626SKris Kennaway * are met: 2674664626SKris Kennaway * 1. Redistributions of source code must retain the copyright 2774664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 2874664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 2974664626SKris Kennaway * notice, this list of conditions and the following disclaimer in the 3074664626SKris Kennaway * documentation and/or other materials provided with the distribution. 3174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this software 3274664626SKris Kennaway * must display the following acknowledgement: 3374664626SKris Kennaway * "This product includes cryptographic software written by 3474664626SKris Kennaway * Eric Young (eay@cryptsoft.com)" 3574664626SKris Kennaway * The word 'cryptographic' can be left out if the rouines from the library 3674664626SKris Kennaway * being used are not cryptographic related :-). 3774664626SKris Kennaway * 4. If you include any Windows specific code (or a derivative thereof) from 3874664626SKris Kennaway * the apps directory (application code) you must include an acknowledgement: 3974664626SKris Kennaway * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4074664626SKris Kennaway * 4174664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4274664626SKris Kennaway * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4374664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4474664626SKris Kennaway * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4574664626SKris Kennaway * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4674664626SKris Kennaway * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4774664626SKris Kennaway * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4974664626SKris Kennaway * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5074664626SKris Kennaway * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5174664626SKris Kennaway * SUCH DAMAGE. 5274664626SKris Kennaway * 5374664626SKris Kennaway * The licence and distribution terms for any publically available version or 5474664626SKris Kennaway * derivative of this code cannot be changed. i.e. this code cannot simply be 5574664626SKris Kennaway * copied and put under another distribution licence 5674664626SKris Kennaway * [including the GNU Public Licence.] 5774664626SKris Kennaway */ 5874664626SKris Kennaway 5974664626SKris Kennaway #include <stdio.h> 6074664626SKris Kennaway #include <stdlib.h> 6174664626SKris Kennaway #include <string.h> 6274664626SKris Kennaway #include "apps.h" 6374664626SKris Kennaway #include <openssl/bio.h> 6474664626SKris Kennaway #include <openssl/err.h> 6574664626SKris Kennaway #include <openssl/evp.h> 6674664626SKris Kennaway #include <openssl/objects.h> 6774664626SKris Kennaway #include <openssl/x509.h> 68f579bf8eSKris Kennaway #include <openssl/rand.h> 6974664626SKris Kennaway #ifndef NO_MD5 7074664626SKris Kennaway #include <openssl/md5.h> 7174664626SKris Kennaway #endif 7274664626SKris Kennaway #include <openssl/pem.h> 7374664626SKris Kennaway 7474664626SKris Kennaway int set_hex(char *in,unsigned char *out,int size); 7574664626SKris Kennaway #undef SIZE 7674664626SKris Kennaway #undef BSIZE 7774664626SKris Kennaway #undef PROG 7874664626SKris Kennaway 7974664626SKris Kennaway #define SIZE (512) 8074664626SKris Kennaway #define BSIZE (8*1024) 8174664626SKris Kennaway #define PROG enc_main 8274664626SKris Kennaway 83f579bf8eSKris Kennaway int MAIN(int, char **); 84f579bf8eSKris Kennaway 8574664626SKris Kennaway int MAIN(int argc, char **argv) 8674664626SKris Kennaway { 87f579bf8eSKris Kennaway static const char magic[]="Salted__"; 88f579bf8eSKris Kennaway char mbuf[8]; /* should be 1 smaller than magic */ 8974664626SKris Kennaway char *strbuf=NULL; 9074664626SKris Kennaway unsigned char *buff=NULL,*bufsize=NULL; 9174664626SKris Kennaway int bsize=BSIZE,verbose=0; 9274664626SKris Kennaway int ret=1,inl; 9374664626SKris Kennaway unsigned char key[24],iv[MD5_DIGEST_LENGTH]; 94f579bf8eSKris Kennaway unsigned char salt[PKCS5_SALT_LEN]; 95f579bf8eSKris Kennaway char *str=NULL, *passarg = NULL, *pass = NULL; 96f579bf8eSKris Kennaway char *hkey=NULL,*hiv=NULL,*hsalt = NULL; 9774664626SKris Kennaway int enc=1,printkey=0,i,base64=0; 98f579bf8eSKris Kennaway int debug=0,olb64=0,nosalt=0; 9974664626SKris Kennaway const EVP_CIPHER *cipher=NULL,*c; 10074664626SKris Kennaway char *inf=NULL,*outf=NULL; 10174664626SKris Kennaway BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL; 10274664626SKris Kennaway #define PROG_NAME_SIZE 16 10374664626SKris Kennaway char pname[PROG_NAME_SIZE]; 10474664626SKris Kennaway 10574664626SKris Kennaway apps_startup(); 10674664626SKris Kennaway 10774664626SKris Kennaway if (bio_err == NULL) 10874664626SKris Kennaway if ((bio_err=BIO_new(BIO_s_file())) != NULL) 10974664626SKris Kennaway BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 11074664626SKris Kennaway 11174664626SKris Kennaway /* first check the program name */ 11274664626SKris Kennaway program_name(argv[0],pname,PROG_NAME_SIZE); 11374664626SKris Kennaway if (strcmp(pname,"base64") == 0) 11474664626SKris Kennaway base64=1; 11574664626SKris Kennaway 11674664626SKris Kennaway cipher=EVP_get_cipherbyname(pname); 11774664626SKris Kennaway if (!base64 && (cipher == NULL) && (strcmp(pname,"enc") != 0)) 11874664626SKris Kennaway { 11974664626SKris Kennaway BIO_printf(bio_err,"%s is an unknown cipher\n",pname); 12074664626SKris Kennaway goto bad; 12174664626SKris Kennaway } 12274664626SKris Kennaway 12374664626SKris Kennaway argc--; 12474664626SKris Kennaway argv++; 12574664626SKris Kennaway while (argc >= 1) 12674664626SKris Kennaway { 12774664626SKris Kennaway if (strcmp(*argv,"-e") == 0) 12874664626SKris Kennaway enc=1; 12974664626SKris Kennaway else if (strcmp(*argv,"-in") == 0) 13074664626SKris Kennaway { 13174664626SKris Kennaway if (--argc < 1) goto bad; 13274664626SKris Kennaway inf= *(++argv); 13374664626SKris Kennaway } 13474664626SKris Kennaway else if (strcmp(*argv,"-out") == 0) 13574664626SKris Kennaway { 13674664626SKris Kennaway if (--argc < 1) goto bad; 13774664626SKris Kennaway outf= *(++argv); 13874664626SKris Kennaway } 139f579bf8eSKris Kennaway else if (strcmp(*argv,"-pass") == 0) 140f579bf8eSKris Kennaway { 141f579bf8eSKris Kennaway if (--argc < 1) goto bad; 142f579bf8eSKris Kennaway passarg= *(++argv); 143f579bf8eSKris Kennaway } 14474664626SKris Kennaway else if (strcmp(*argv,"-d") == 0) 14574664626SKris Kennaway enc=0; 14674664626SKris Kennaway else if (strcmp(*argv,"-p") == 0) 14774664626SKris Kennaway printkey=1; 14874664626SKris Kennaway else if (strcmp(*argv,"-v") == 0) 14974664626SKris Kennaway verbose=1; 150f579bf8eSKris Kennaway else if (strcmp(*argv,"-salt") == 0) 151f579bf8eSKris Kennaway nosalt=0; 152f579bf8eSKris Kennaway else if (strcmp(*argv,"-nosalt") == 0) 153f579bf8eSKris Kennaway nosalt=1; 154f579bf8eSKris Kennaway else if (strcmp(*argv,"-debug") == 0) 15574664626SKris Kennaway debug=1; 15674664626SKris Kennaway else if (strcmp(*argv,"-P") == 0) 15774664626SKris Kennaway printkey=2; 15874664626SKris Kennaway else if (strcmp(*argv,"-A") == 0) 15974664626SKris Kennaway olb64=1; 16074664626SKris Kennaway else if (strcmp(*argv,"-a") == 0) 16174664626SKris Kennaway base64=1; 16274664626SKris Kennaway else if (strcmp(*argv,"-base64") == 0) 16374664626SKris Kennaway base64=1; 16474664626SKris Kennaway else if (strcmp(*argv,"-bufsize") == 0) 16574664626SKris Kennaway { 16674664626SKris Kennaway if (--argc < 1) goto bad; 16774664626SKris Kennaway bufsize=(unsigned char *)*(++argv); 16874664626SKris Kennaway } 16974664626SKris Kennaway else if (strcmp(*argv,"-k") == 0) 17074664626SKris Kennaway { 17174664626SKris Kennaway if (--argc < 1) goto bad; 17274664626SKris Kennaway str= *(++argv); 17374664626SKris Kennaway } 17474664626SKris Kennaway else if (strcmp(*argv,"-kfile") == 0) 17574664626SKris Kennaway { 17674664626SKris Kennaway static char buf[128]; 17774664626SKris Kennaway FILE *infile; 17874664626SKris Kennaway char *file; 17974664626SKris Kennaway 18074664626SKris Kennaway if (--argc < 1) goto bad; 18174664626SKris Kennaway file= *(++argv); 18274664626SKris Kennaway infile=fopen(file,"r"); 18374664626SKris Kennaway if (infile == NULL) 18474664626SKris Kennaway { 18574664626SKris Kennaway BIO_printf(bio_err,"unable to read key from '%s'\n", 18674664626SKris Kennaway file); 18774664626SKris Kennaway goto bad; 18874664626SKris Kennaway } 18974664626SKris Kennaway buf[0]='\0'; 19074664626SKris Kennaway fgets(buf,128,infile); 19174664626SKris Kennaway fclose(infile); 19274664626SKris Kennaway i=strlen(buf); 19374664626SKris Kennaway if ((i > 0) && 19474664626SKris Kennaway ((buf[i-1] == '\n') || (buf[i-1] == '\r'))) 19574664626SKris Kennaway buf[--i]='\0'; 19674664626SKris Kennaway if ((i > 0) && 19774664626SKris Kennaway ((buf[i-1] == '\n') || (buf[i-1] == '\r'))) 19874664626SKris Kennaway buf[--i]='\0'; 19974664626SKris Kennaway if (i < 1) 20074664626SKris Kennaway { 20174664626SKris Kennaway BIO_printf(bio_err,"zero length password\n"); 20274664626SKris Kennaway goto bad; 20374664626SKris Kennaway } 20474664626SKris Kennaway str=buf; 20574664626SKris Kennaway } 20674664626SKris Kennaway else if (strcmp(*argv,"-K") == 0) 20774664626SKris Kennaway { 20874664626SKris Kennaway if (--argc < 1) goto bad; 20974664626SKris Kennaway hkey= *(++argv); 21074664626SKris Kennaway } 211f579bf8eSKris Kennaway else if (strcmp(*argv,"-S") == 0) 212f579bf8eSKris Kennaway { 213f579bf8eSKris Kennaway if (--argc < 1) goto bad; 214f579bf8eSKris Kennaway hsalt= *(++argv); 215f579bf8eSKris Kennaway } 21674664626SKris Kennaway else if (strcmp(*argv,"-iv") == 0) 21774664626SKris Kennaway { 21874664626SKris Kennaway if (--argc < 1) goto bad; 21974664626SKris Kennaway hiv= *(++argv); 22074664626SKris Kennaway } 22174664626SKris Kennaway else if ((argv[0][0] == '-') && 22274664626SKris Kennaway ((c=EVP_get_cipherbyname(&(argv[0][1]))) != NULL)) 22374664626SKris Kennaway { 22474664626SKris Kennaway cipher=c; 22574664626SKris Kennaway } 22674664626SKris Kennaway else if (strcmp(*argv,"-none") == 0) 22774664626SKris Kennaway cipher=NULL; 22874664626SKris Kennaway else 22974664626SKris Kennaway { 23074664626SKris Kennaway BIO_printf(bio_err,"unknown option '%s'\n",*argv); 23174664626SKris Kennaway bad: 23274664626SKris Kennaway BIO_printf(bio_err,"options are\n"); 23374664626SKris Kennaway BIO_printf(bio_err,"%-14s input file\n","-in <file>"); 234f579bf8eSKris Kennaway BIO_printf(bio_err,"%-14s output file\n","-out <file>"); 235f579bf8eSKris Kennaway BIO_printf(bio_err,"%-14s pass phrase source\n","-pass <arg>"); 23674664626SKris Kennaway BIO_printf(bio_err,"%-14s encrypt\n","-e"); 23774664626SKris Kennaway BIO_printf(bio_err,"%-14s decrypt\n","-d"); 23874664626SKris Kennaway BIO_printf(bio_err,"%-14s base64 encode/decode, depending on encryption flag\n","-a/-base64"); 23974664626SKris Kennaway BIO_printf(bio_err,"%-14s key is the next argument\n","-k"); 24074664626SKris Kennaway BIO_printf(bio_err,"%-14s key is the first line of the file argument\n","-kfile"); 24174664626SKris Kennaway BIO_printf(bio_err,"%-14s key/iv in hex is the next argument\n","-K/-iv"); 24274664626SKris Kennaway BIO_printf(bio_err,"%-14s print the iv/key (then exit if -P)\n","-[pP]"); 24374664626SKris Kennaway BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>"); 24474664626SKris Kennaway 24574664626SKris Kennaway BIO_printf(bio_err,"Cipher Types\n"); 24674664626SKris Kennaway BIO_printf(bio_err,"des : 56 bit key DES encryption\n"); 24774664626SKris Kennaway BIO_printf(bio_err,"des_ede :112 bit key ede DES encryption\n"); 24874664626SKris Kennaway BIO_printf(bio_err,"des_ede3:168 bit key ede DES encryption\n"); 24974664626SKris Kennaway #ifndef NO_IDEA 25074664626SKris Kennaway BIO_printf(bio_err,"idea :128 bit key IDEA encryption\n"); 25174664626SKris Kennaway #endif 25274664626SKris Kennaway #ifndef NO_RC4 25374664626SKris Kennaway BIO_printf(bio_err,"rc2 :128 bit key RC2 encryption\n"); 25474664626SKris Kennaway #endif 25574664626SKris Kennaway #ifndef NO_BF 256f579bf8eSKris Kennaway BIO_printf(bio_err,"bf :128 bit key Blowfish encryption\n"); 25774664626SKris Kennaway #endif 25874664626SKris Kennaway #ifndef NO_RC4 25974664626SKris Kennaway BIO_printf(bio_err," -%-5s :128 bit key RC4 encryption\n", 26074664626SKris Kennaway LN_rc4); 26174664626SKris Kennaway #endif 26274664626SKris Kennaway 26374664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 26474664626SKris Kennaway LN_des_ecb,LN_des_cbc, 26574664626SKris Kennaway LN_des_cfb64,LN_des_ofb64); 26674664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n", 26774664626SKris Kennaway "des", LN_des_cbc); 26874664626SKris Kennaway 26974664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 27074664626SKris Kennaway LN_des_ede,LN_des_ede_cbc, 27174664626SKris Kennaway LN_des_ede_cfb64,LN_des_ede_ofb64); 27274664626SKris Kennaway BIO_printf(bio_err," -desx -none\n"); 27374664626SKris Kennaway 27474664626SKris Kennaway 27574664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 27674664626SKris Kennaway LN_des_ede3,LN_des_ede3_cbc, 27774664626SKris Kennaway LN_des_ede3_cfb64,LN_des_ede3_ofb64); 27874664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n", 27974664626SKris Kennaway "des3", LN_des_ede3_cbc); 28074664626SKris Kennaway 28174664626SKris Kennaway #ifndef NO_IDEA 28274664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 28374664626SKris Kennaway LN_idea_ecb, LN_idea_cbc, 28474664626SKris Kennaway LN_idea_cfb64, LN_idea_ofb64); 28574664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n","idea",LN_idea_cbc); 28674664626SKris Kennaway #endif 28774664626SKris Kennaway #ifndef NO_RC2 28874664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 28974664626SKris Kennaway LN_rc2_ecb, LN_rc2_cbc, 29074664626SKris Kennaway LN_rc2_cfb64, LN_rc2_ofb64); 29174664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n","rc2", LN_rc2_cbc); 29274664626SKris Kennaway #endif 29374664626SKris Kennaway #ifndef NO_BF 29474664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 29574664626SKris Kennaway LN_bf_ecb, LN_bf_cbc, 29674664626SKris Kennaway LN_bf_cfb64, LN_bf_ofb64); 29774664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n","bf", LN_bf_cbc); 29874664626SKris Kennaway #endif 29974664626SKris Kennaway #ifndef NO_CAST 30074664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 30174664626SKris Kennaway LN_cast5_ecb, LN_cast5_cbc, 30274664626SKris Kennaway LN_cast5_cfb64, LN_cast5_ofb64); 30374664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n","cast", LN_cast5_cbc); 30474664626SKris Kennaway #endif 30574664626SKris Kennaway #ifndef NO_RC5 30674664626SKris Kennaway BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", 30774664626SKris Kennaway LN_rc5_ecb, LN_rc5_cbc, 30874664626SKris Kennaway LN_rc5_cfb64, LN_rc5_ofb64); 30974664626SKris Kennaway BIO_printf(bio_err," -%-4s (%s)\n","rc5", LN_rc5_cbc); 31074664626SKris Kennaway #endif 31174664626SKris Kennaway goto end; 31274664626SKris Kennaway } 31374664626SKris Kennaway argc--; 31474664626SKris Kennaway argv++; 31574664626SKris Kennaway } 31674664626SKris Kennaway 31774664626SKris Kennaway if (bufsize != NULL) 31874664626SKris Kennaway { 31974664626SKris Kennaway unsigned long n; 32074664626SKris Kennaway 32174664626SKris Kennaway for (n=0; *bufsize; bufsize++) 32274664626SKris Kennaway { 32374664626SKris Kennaway i= *bufsize; 32474664626SKris Kennaway if ((i <= '9') && (i >= '0')) 32574664626SKris Kennaway n=n*10+i-'0'; 32674664626SKris Kennaway else if (i == 'k') 32774664626SKris Kennaway { 32874664626SKris Kennaway n*=1024; 32974664626SKris Kennaway bufsize++; 33074664626SKris Kennaway break; 33174664626SKris Kennaway } 33274664626SKris Kennaway } 33374664626SKris Kennaway if (*bufsize != '\0') 33474664626SKris Kennaway { 33574664626SKris Kennaway BIO_printf(bio_err,"invalid 'bufsize' specified.\n"); 33674664626SKris Kennaway goto end; 33774664626SKris Kennaway } 33874664626SKris Kennaway 33974664626SKris Kennaway /* It must be large enough for a base64 encoded line */ 34074664626SKris Kennaway if (n < 80) n=80; 34174664626SKris Kennaway 34274664626SKris Kennaway bsize=(int)n; 34374664626SKris Kennaway if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize); 34474664626SKris Kennaway } 34574664626SKris Kennaway 34674664626SKris Kennaway strbuf=Malloc(SIZE); 34774664626SKris Kennaway buff=(unsigned char *)Malloc(EVP_ENCODE_LENGTH(bsize)); 34874664626SKris Kennaway if ((buff == NULL) || (strbuf == NULL)) 34974664626SKris Kennaway { 35074664626SKris Kennaway BIO_printf(bio_err,"Malloc failure %ld\n",(long)EVP_ENCODE_LENGTH(bsize)); 35174664626SKris Kennaway goto end; 35274664626SKris Kennaway } 35374664626SKris Kennaway 35474664626SKris Kennaway in=BIO_new(BIO_s_file()); 35574664626SKris Kennaway out=BIO_new(BIO_s_file()); 35674664626SKris Kennaway if ((in == NULL) || (out == NULL)) 35774664626SKris Kennaway { 35874664626SKris Kennaway ERR_print_errors(bio_err); 35974664626SKris Kennaway goto end; 36074664626SKris Kennaway } 36174664626SKris Kennaway if (debug) 36274664626SKris Kennaway { 36374664626SKris Kennaway BIO_set_callback(in,BIO_debug_callback); 36474664626SKris Kennaway BIO_set_callback(out,BIO_debug_callback); 36574664626SKris Kennaway BIO_set_callback_arg(in,bio_err); 36674664626SKris Kennaway BIO_set_callback_arg(out,bio_err); 36774664626SKris Kennaway } 36874664626SKris Kennaway 36974664626SKris Kennaway if (inf == NULL) 37074664626SKris Kennaway BIO_set_fp(in,stdin,BIO_NOCLOSE); 37174664626SKris Kennaway else 37274664626SKris Kennaway { 37374664626SKris Kennaway if (BIO_read_filename(in,inf) <= 0) 37474664626SKris Kennaway { 37574664626SKris Kennaway perror(inf); 37674664626SKris Kennaway goto end; 37774664626SKris Kennaway } 37874664626SKris Kennaway } 37974664626SKris Kennaway 380f579bf8eSKris Kennaway if(!str && passarg) { 381f579bf8eSKris Kennaway if(!app_passwd(bio_err, passarg, NULL, &pass, NULL)) { 382f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting password\n"); 383f579bf8eSKris Kennaway goto end; 384f579bf8eSKris Kennaway } 385f579bf8eSKris Kennaway str = pass; 386f579bf8eSKris Kennaway } 387f579bf8eSKris Kennaway 38874664626SKris Kennaway if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) 38974664626SKris Kennaway { 39074664626SKris Kennaway for (;;) 39174664626SKris Kennaway { 39274664626SKris Kennaway char buf[200]; 39374664626SKris Kennaway 39474664626SKris Kennaway sprintf(buf,"enter %s %s password:", 39574664626SKris Kennaway OBJ_nid2ln(EVP_CIPHER_nid(cipher)), 39674664626SKris Kennaway (enc)?"encryption":"decryption"); 39774664626SKris Kennaway strbuf[0]='\0'; 39874664626SKris Kennaway i=EVP_read_pw_string((char *)strbuf,SIZE,buf,enc); 39974664626SKris Kennaway if (i == 0) 40074664626SKris Kennaway { 40174664626SKris Kennaway if (strbuf[0] == '\0') 40274664626SKris Kennaway { 40374664626SKris Kennaway ret=1; 40474664626SKris Kennaway goto end; 40574664626SKris Kennaway } 40674664626SKris Kennaway str=strbuf; 40774664626SKris Kennaway break; 40874664626SKris Kennaway } 40974664626SKris Kennaway if (i < 0) 41074664626SKris Kennaway { 41174664626SKris Kennaway BIO_printf(bio_err,"bad password read\n"); 41274664626SKris Kennaway goto end; 41374664626SKris Kennaway } 41474664626SKris Kennaway } 41574664626SKris Kennaway } 41674664626SKris Kennaway 41774664626SKris Kennaway 41874664626SKris Kennaway if (outf == NULL) 41974664626SKris Kennaway BIO_set_fp(out,stdout,BIO_NOCLOSE); 42074664626SKris Kennaway else 42174664626SKris Kennaway { 42274664626SKris Kennaway if (BIO_write_filename(out,outf) <= 0) 42374664626SKris Kennaway { 42474664626SKris Kennaway perror(outf); 42574664626SKris Kennaway goto end; 42674664626SKris Kennaway } 42774664626SKris Kennaway } 42874664626SKris Kennaway 42974664626SKris Kennaway rbio=in; 43074664626SKris Kennaway wbio=out; 43174664626SKris Kennaway 43274664626SKris Kennaway if (base64) 43374664626SKris Kennaway { 43474664626SKris Kennaway if ((b64=BIO_new(BIO_f_base64())) == NULL) 43574664626SKris Kennaway goto end; 43674664626SKris Kennaway if (debug) 43774664626SKris Kennaway { 43874664626SKris Kennaway BIO_set_callback(b64,BIO_debug_callback); 43974664626SKris Kennaway BIO_set_callback_arg(b64,bio_err); 44074664626SKris Kennaway } 44174664626SKris Kennaway if (olb64) 44274664626SKris Kennaway BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 44374664626SKris Kennaway if (enc) 44474664626SKris Kennaway wbio=BIO_push(b64,wbio); 44574664626SKris Kennaway else 44674664626SKris Kennaway rbio=BIO_push(b64,rbio); 44774664626SKris Kennaway } 44874664626SKris Kennaway 449f579bf8eSKris Kennaway if (cipher != NULL) 450f579bf8eSKris Kennaway { 451f579bf8eSKris Kennaway if (str != NULL) 452f579bf8eSKris Kennaway { 453f579bf8eSKris Kennaway /* Salt handling: if encrypting generate a salt and 454f579bf8eSKris Kennaway * write to output BIO. If decrypting read salt from 455f579bf8eSKris Kennaway * input BIO. 456f579bf8eSKris Kennaway */ 457f579bf8eSKris Kennaway unsigned char *sptr; 458f579bf8eSKris Kennaway if(nosalt) sptr = NULL; 459f579bf8eSKris Kennaway else { 460f579bf8eSKris Kennaway if(enc) { 461f579bf8eSKris Kennaway if(hsalt) { 462f579bf8eSKris Kennaway if(!set_hex(hsalt,salt,PKCS5_SALT_LEN)) { 463f579bf8eSKris Kennaway BIO_printf(bio_err, 464f579bf8eSKris Kennaway "invalid hex salt value\n"); 465f579bf8eSKris Kennaway goto end; 466f579bf8eSKris Kennaway } 467f579bf8eSKris Kennaway } else if (RAND_pseudo_bytes(salt, PKCS5_SALT_LEN) < 0) 468f579bf8eSKris Kennaway goto end; 469f579bf8eSKris Kennaway /* If -P option then don't bother writing */ 470f579bf8eSKris Kennaway if((printkey != 2) 471f579bf8eSKris Kennaway && (BIO_write(wbio,magic, 472f579bf8eSKris Kennaway sizeof magic-1) != sizeof magic-1 473f579bf8eSKris Kennaway || BIO_write(wbio, 474f579bf8eSKris Kennaway (char *)salt, 475f579bf8eSKris Kennaway PKCS5_SALT_LEN) != PKCS5_SALT_LEN)) { 476f579bf8eSKris Kennaway BIO_printf(bio_err,"error writing output file\n"); 477f579bf8eSKris Kennaway goto end; 478f579bf8eSKris Kennaway } 479f579bf8eSKris Kennaway } else if(BIO_read(rbio,mbuf,sizeof mbuf) != sizeof mbuf 480f579bf8eSKris Kennaway || BIO_read(rbio, 481f579bf8eSKris Kennaway (unsigned char *)salt, 482f579bf8eSKris Kennaway PKCS5_SALT_LEN) != PKCS5_SALT_LEN) { 483f579bf8eSKris Kennaway BIO_printf(bio_err,"error reading input file\n"); 484f579bf8eSKris Kennaway goto end; 485f579bf8eSKris Kennaway } else if(memcmp(mbuf,magic,sizeof magic-1)) { 486f579bf8eSKris Kennaway BIO_printf(bio_err,"bad magic number\n"); 487f579bf8eSKris Kennaway goto end; 488f579bf8eSKris Kennaway } 489f579bf8eSKris Kennaway 490f579bf8eSKris Kennaway sptr = salt; 491f579bf8eSKris Kennaway } 492f579bf8eSKris Kennaway 493f579bf8eSKris Kennaway EVP_BytesToKey(cipher,EVP_md5(),sptr, 494f579bf8eSKris Kennaway (unsigned char *)str, 495f579bf8eSKris Kennaway strlen(str),1,key,iv); 496f579bf8eSKris Kennaway /* zero the complete buffer or the string 497f579bf8eSKris Kennaway * passed from the command line 498f579bf8eSKris Kennaway * bug picked up by 499f579bf8eSKris Kennaway * Larry J. Hughes Jr. <hughes@indiana.edu> */ 500f579bf8eSKris Kennaway if (str == strbuf) 501f579bf8eSKris Kennaway memset(str,0,SIZE); 502f579bf8eSKris Kennaway else 503f579bf8eSKris Kennaway memset(str,0,strlen(str)); 504f579bf8eSKris Kennaway } 505f579bf8eSKris Kennaway if ((hiv != NULL) && !set_hex(hiv,iv,8)) 506f579bf8eSKris Kennaway { 507f579bf8eSKris Kennaway BIO_printf(bio_err,"invalid hex iv value\n"); 508f579bf8eSKris Kennaway goto end; 509f579bf8eSKris Kennaway } 510f579bf8eSKris Kennaway if ((hkey != NULL) && !set_hex(hkey,key,24)) 511f579bf8eSKris Kennaway { 512f579bf8eSKris Kennaway BIO_printf(bio_err,"invalid hex key value\n"); 513f579bf8eSKris Kennaway goto end; 514f579bf8eSKris Kennaway } 515f579bf8eSKris Kennaway 516f579bf8eSKris Kennaway if ((benc=BIO_new(BIO_f_cipher())) == NULL) 517f579bf8eSKris Kennaway goto end; 518f579bf8eSKris Kennaway BIO_set_cipher(benc,cipher,key,iv,enc); 519f579bf8eSKris Kennaway if (debug) 520f579bf8eSKris Kennaway { 521f579bf8eSKris Kennaway BIO_set_callback(benc,BIO_debug_callback); 522f579bf8eSKris Kennaway BIO_set_callback_arg(benc,bio_err); 523f579bf8eSKris Kennaway } 524f579bf8eSKris Kennaway 525f579bf8eSKris Kennaway if (printkey) 526f579bf8eSKris Kennaway { 527f579bf8eSKris Kennaway if (!nosalt) 528f579bf8eSKris Kennaway { 529f579bf8eSKris Kennaway printf("salt="); 530f579bf8eSKris Kennaway for (i=0; i<PKCS5_SALT_LEN; i++) 531f579bf8eSKris Kennaway printf("%02X",salt[i]); 532f579bf8eSKris Kennaway printf("\n"); 533f579bf8eSKris Kennaway } 534f579bf8eSKris Kennaway if (cipher->key_len > 0) 535f579bf8eSKris Kennaway { 536f579bf8eSKris Kennaway printf("key="); 537f579bf8eSKris Kennaway for (i=0; i<cipher->key_len; i++) 538f579bf8eSKris Kennaway printf("%02X",key[i]); 539f579bf8eSKris Kennaway printf("\n"); 540f579bf8eSKris Kennaway } 541f579bf8eSKris Kennaway if (cipher->iv_len > 0) 542f579bf8eSKris Kennaway { 543f579bf8eSKris Kennaway printf("iv ="); 544f579bf8eSKris Kennaway for (i=0; i<cipher->iv_len; i++) 545f579bf8eSKris Kennaway printf("%02X",iv[i]); 546f579bf8eSKris Kennaway printf("\n"); 547f579bf8eSKris Kennaway } 548f579bf8eSKris Kennaway if (printkey == 2) 549f579bf8eSKris Kennaway { 550f579bf8eSKris Kennaway ret=0; 551f579bf8eSKris Kennaway goto end; 552f579bf8eSKris Kennaway } 553f579bf8eSKris Kennaway } 554f579bf8eSKris Kennaway } 555f579bf8eSKris Kennaway 55674664626SKris Kennaway /* Only encrypt/decrypt as we write the file */ 55774664626SKris Kennaway if (benc != NULL) 55874664626SKris Kennaway wbio=BIO_push(benc,wbio); 55974664626SKris Kennaway 56074664626SKris Kennaway for (;;) 56174664626SKris Kennaway { 56274664626SKris Kennaway inl=BIO_read(rbio,(char *)buff,bsize); 56374664626SKris Kennaway if (inl <= 0) break; 56474664626SKris Kennaway if (BIO_write(wbio,(char *)buff,inl) != inl) 56574664626SKris Kennaway { 56674664626SKris Kennaway BIO_printf(bio_err,"error writing output file\n"); 56774664626SKris Kennaway goto end; 56874664626SKris Kennaway } 56974664626SKris Kennaway } 57074664626SKris Kennaway if (!BIO_flush(wbio)) 57174664626SKris Kennaway { 57274664626SKris Kennaway BIO_printf(bio_err,"bad decrypt\n"); 57374664626SKris Kennaway goto end; 57474664626SKris Kennaway } 57574664626SKris Kennaway 57674664626SKris Kennaway ret=0; 57774664626SKris Kennaway if (verbose) 57874664626SKris Kennaway { 57974664626SKris Kennaway BIO_printf(bio_err,"bytes read :%8ld\n",BIO_number_read(in)); 58074664626SKris Kennaway BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out)); 58174664626SKris Kennaway } 58274664626SKris Kennaway end: 583f579bf8eSKris Kennaway ERR_print_errors(bio_err); 58474664626SKris Kennaway if (strbuf != NULL) Free(strbuf); 58574664626SKris Kennaway if (buff != NULL) Free(buff); 58674664626SKris Kennaway if (in != NULL) BIO_free(in); 58774664626SKris Kennaway if (out != NULL) BIO_free(out); 58874664626SKris Kennaway if (benc != NULL) BIO_free(benc); 58974664626SKris Kennaway if (b64 != NULL) BIO_free(b64); 590f579bf8eSKris Kennaway if(pass) Free(pass); 59174664626SKris Kennaway EXIT(ret); 59274664626SKris Kennaway } 59374664626SKris Kennaway 59474664626SKris Kennaway int set_hex(char *in, unsigned char *out, int size) 59574664626SKris Kennaway { 59674664626SKris Kennaway int i,n; 59774664626SKris Kennaway unsigned char j; 59874664626SKris Kennaway 59974664626SKris Kennaway n=strlen(in); 60074664626SKris Kennaway if (n > (size*2)) 60174664626SKris Kennaway { 60274664626SKris Kennaway BIO_printf(bio_err,"hex string is too long\n"); 60374664626SKris Kennaway return(0); 60474664626SKris Kennaway } 60574664626SKris Kennaway memset(out,0,size); 60674664626SKris Kennaway for (i=0; i<n; i++) 60774664626SKris Kennaway { 60874664626SKris Kennaway j=(unsigned char)*in; 60974664626SKris Kennaway *(in++)='\0'; 61074664626SKris Kennaway if (j == 0) break; 61174664626SKris Kennaway if ((j >= '0') && (j <= '9')) 61274664626SKris Kennaway j-='0'; 61374664626SKris Kennaway else if ((j >= 'A') && (j <= 'F')) 61474664626SKris Kennaway j=j-'A'+10; 61574664626SKris Kennaway else if ((j >= 'a') && (j <= 'f')) 61674664626SKris Kennaway j=j-'a'+10; 61774664626SKris Kennaway else 61874664626SKris Kennaway { 61974664626SKris Kennaway BIO_printf(bio_err,"non-hex digit\n"); 62074664626SKris Kennaway return(0); 62174664626SKris Kennaway } 62274664626SKris Kennaway if (i&1) 62374664626SKris Kennaway out[i/2]|=j; 62474664626SKris Kennaway else 62574664626SKris Kennaway out[i/2]=(j<<4); 62674664626SKris Kennaway } 62774664626SKris Kennaway return(1); 62874664626SKris Kennaway } 629