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 #include <openssl/pem.h> 705c87c606SMark Murray #include <ctype.h> 7174664626SKris Kennaway 7274664626SKris Kennaway int set_hex(char *in,unsigned char *out,int size); 7374664626SKris Kennaway #undef SIZE 7474664626SKris Kennaway #undef BSIZE 7574664626SKris Kennaway #undef PROG 7674664626SKris Kennaway 7774664626SKris Kennaway #define SIZE (512) 7874664626SKris Kennaway #define BSIZE (8*1024) 7974664626SKris Kennaway #define PROG enc_main 8074664626SKris Kennaway 815c87c606SMark Murray static void show_ciphers(const OBJ_NAME *name,void *bio_) 825c87c606SMark Murray { 835c87c606SMark Murray BIO *bio=bio_; 845c87c606SMark Murray static int n; 855c87c606SMark Murray 865c87c606SMark Murray if(!islower((unsigned char)*name->name)) 875c87c606SMark Murray return; 885c87c606SMark Murray 895c87c606SMark Murray BIO_printf(bio,"-%-25s",name->name); 905c87c606SMark Murray if(++n == 3) 915c87c606SMark Murray { 925c87c606SMark Murray BIO_printf(bio,"\n"); 935c87c606SMark Murray n=0; 945c87c606SMark Murray } 955c87c606SMark Murray else 965c87c606SMark Murray BIO_printf(bio," "); 975c87c606SMark Murray } 985c87c606SMark Murray 99f579bf8eSKris Kennaway int MAIN(int, char **); 100f579bf8eSKris Kennaway 10174664626SKris Kennaway int MAIN(int argc, char **argv) 10274664626SKris Kennaway { 103fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 1045c87c606SMark Murray ENGINE *e = NULL; 105fceca8a3SJacques Vidrine #endif 106f579bf8eSKris Kennaway static const char magic[]="Salted__"; 1075c87c606SMark Murray char mbuf[sizeof magic-1]; 10874664626SKris Kennaway char *strbuf=NULL; 10974664626SKris Kennaway unsigned char *buff=NULL,*bufsize=NULL; 11074664626SKris Kennaway int bsize=BSIZE,verbose=0; 11174664626SKris Kennaway int ret=1,inl; 1125c87c606SMark Murray int nopad = 0; 1135c87c606SMark Murray unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH]; 114f579bf8eSKris Kennaway unsigned char salt[PKCS5_SALT_LEN]; 115f579bf8eSKris Kennaway char *str=NULL, *passarg = NULL, *pass = NULL; 116f579bf8eSKris Kennaway char *hkey=NULL,*hiv=NULL,*hsalt = NULL; 1176be8ae07SJacques Vidrine char *md=NULL; 11874664626SKris Kennaway int enc=1,printkey=0,i,base64=0; 119f579bf8eSKris Kennaway int debug=0,olb64=0,nosalt=0; 12074664626SKris Kennaway const EVP_CIPHER *cipher=NULL,*c; 1213b4e3dcbSSimon L. B. Nielsen EVP_CIPHER_CTX *ctx = NULL; 12274664626SKris Kennaway char *inf=NULL,*outf=NULL; 12374664626SKris Kennaway BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL; 124c1803d78SJacques Vidrine #define PROG_NAME_SIZE 39 125c1803d78SJacques Vidrine char pname[PROG_NAME_SIZE+1]; 126fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 1275c87c606SMark Murray char *engine = NULL; 128fceca8a3SJacques Vidrine #endif 1296be8ae07SJacques Vidrine const EVP_MD *dgst=NULL; 130db522d3aSSimon L. B. Nielsen int non_fips_allow = 0; 13174664626SKris Kennaway 13274664626SKris Kennaway apps_startup(); 13374664626SKris Kennaway 13474664626SKris Kennaway if (bio_err == NULL) 13574664626SKris Kennaway if ((bio_err=BIO_new(BIO_s_file())) != NULL) 13674664626SKris Kennaway BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 13774664626SKris Kennaway 1385c87c606SMark Murray if (!load_config(bio_err, NULL)) 1395c87c606SMark Murray goto end; 1405c87c606SMark Murray 14174664626SKris Kennaway /* first check the program name */ 1425c87c606SMark Murray program_name(argv[0],pname,sizeof pname); 14374664626SKris Kennaway if (strcmp(pname,"base64") == 0) 14474664626SKris Kennaway base64=1; 14574664626SKris Kennaway 14674664626SKris Kennaway cipher=EVP_get_cipherbyname(pname); 14774664626SKris Kennaway if (!base64 && (cipher == NULL) && (strcmp(pname,"enc") != 0)) 14874664626SKris Kennaway { 14974664626SKris Kennaway BIO_printf(bio_err,"%s is an unknown cipher\n",pname); 15074664626SKris Kennaway goto bad; 15174664626SKris Kennaway } 15274664626SKris Kennaway 15374664626SKris Kennaway argc--; 15474664626SKris Kennaway argv++; 15574664626SKris Kennaway while (argc >= 1) 15674664626SKris Kennaway { 15774664626SKris Kennaway if (strcmp(*argv,"-e") == 0) 15874664626SKris Kennaway enc=1; 15974664626SKris Kennaway else if (strcmp(*argv,"-in") == 0) 16074664626SKris Kennaway { 16174664626SKris Kennaway if (--argc < 1) goto bad; 16274664626SKris Kennaway inf= *(++argv); 16374664626SKris Kennaway } 16474664626SKris Kennaway else if (strcmp(*argv,"-out") == 0) 16574664626SKris Kennaway { 16674664626SKris Kennaway if (--argc < 1) goto bad; 16774664626SKris Kennaway outf= *(++argv); 16874664626SKris Kennaway } 169f579bf8eSKris Kennaway else if (strcmp(*argv,"-pass") == 0) 170f579bf8eSKris Kennaway { 171f579bf8eSKris Kennaway if (--argc < 1) goto bad; 172f579bf8eSKris Kennaway passarg= *(++argv); 173f579bf8eSKris Kennaway } 174fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 1755c87c606SMark Murray else if (strcmp(*argv,"-engine") == 0) 1765c87c606SMark Murray { 1775c87c606SMark Murray if (--argc < 1) goto bad; 1785c87c606SMark Murray engine= *(++argv); 1795c87c606SMark Murray } 180fceca8a3SJacques Vidrine #endif 18174664626SKris Kennaway else if (strcmp(*argv,"-d") == 0) 18274664626SKris Kennaway enc=0; 18374664626SKris Kennaway else if (strcmp(*argv,"-p") == 0) 18474664626SKris Kennaway printkey=1; 18574664626SKris Kennaway else if (strcmp(*argv,"-v") == 0) 18674664626SKris Kennaway verbose=1; 1875c87c606SMark Murray else if (strcmp(*argv,"-nopad") == 0) 1885c87c606SMark Murray nopad=1; 189f579bf8eSKris Kennaway else if (strcmp(*argv,"-salt") == 0) 190f579bf8eSKris Kennaway nosalt=0; 191f579bf8eSKris Kennaway else if (strcmp(*argv,"-nosalt") == 0) 192f579bf8eSKris Kennaway nosalt=1; 193f579bf8eSKris Kennaway else if (strcmp(*argv,"-debug") == 0) 19474664626SKris Kennaway debug=1; 19574664626SKris Kennaway else if (strcmp(*argv,"-P") == 0) 19674664626SKris Kennaway printkey=2; 19774664626SKris Kennaway else if (strcmp(*argv,"-A") == 0) 19874664626SKris Kennaway olb64=1; 19974664626SKris Kennaway else if (strcmp(*argv,"-a") == 0) 20074664626SKris Kennaway base64=1; 20174664626SKris Kennaway else if (strcmp(*argv,"-base64") == 0) 20274664626SKris Kennaway base64=1; 20374664626SKris Kennaway else if (strcmp(*argv,"-bufsize") == 0) 20474664626SKris Kennaway { 20574664626SKris Kennaway if (--argc < 1) goto bad; 20674664626SKris Kennaway bufsize=(unsigned char *)*(++argv); 20774664626SKris Kennaway } 20874664626SKris Kennaway else if (strcmp(*argv,"-k") == 0) 20974664626SKris Kennaway { 21074664626SKris Kennaway if (--argc < 1) goto bad; 21174664626SKris Kennaway str= *(++argv); 21274664626SKris Kennaway } 21374664626SKris Kennaway else if (strcmp(*argv,"-kfile") == 0) 21474664626SKris Kennaway { 21574664626SKris Kennaway static char buf[128]; 21674664626SKris Kennaway FILE *infile; 21774664626SKris Kennaway char *file; 21874664626SKris Kennaway 21974664626SKris Kennaway if (--argc < 1) goto bad; 22074664626SKris Kennaway file= *(++argv); 22174664626SKris Kennaway infile=fopen(file,"r"); 22274664626SKris Kennaway if (infile == NULL) 22374664626SKris Kennaway { 22474664626SKris Kennaway BIO_printf(bio_err,"unable to read key from '%s'\n", 22574664626SKris Kennaway file); 22674664626SKris Kennaway goto bad; 22774664626SKris Kennaway } 22874664626SKris Kennaway buf[0]='\0'; 2295c87c606SMark Murray fgets(buf,sizeof buf,infile); 23074664626SKris Kennaway fclose(infile); 23174664626SKris Kennaway i=strlen(buf); 23274664626SKris Kennaway if ((i > 0) && 23374664626SKris Kennaway ((buf[i-1] == '\n') || (buf[i-1] == '\r'))) 23474664626SKris Kennaway buf[--i]='\0'; 23574664626SKris Kennaway if ((i > 0) && 23674664626SKris Kennaway ((buf[i-1] == '\n') || (buf[i-1] == '\r'))) 23774664626SKris Kennaway buf[--i]='\0'; 23874664626SKris Kennaway if (i < 1) 23974664626SKris Kennaway { 24074664626SKris Kennaway BIO_printf(bio_err,"zero length password\n"); 24174664626SKris Kennaway goto bad; 24274664626SKris Kennaway } 24374664626SKris Kennaway str=buf; 24474664626SKris Kennaway } 24574664626SKris Kennaway else if (strcmp(*argv,"-K") == 0) 24674664626SKris Kennaway { 24774664626SKris Kennaway if (--argc < 1) goto bad; 24874664626SKris Kennaway hkey= *(++argv); 24974664626SKris Kennaway } 250f579bf8eSKris Kennaway else if (strcmp(*argv,"-S") == 0) 251f579bf8eSKris Kennaway { 252f579bf8eSKris Kennaway if (--argc < 1) goto bad; 253f579bf8eSKris Kennaway hsalt= *(++argv); 254f579bf8eSKris Kennaway } 25574664626SKris Kennaway else if (strcmp(*argv,"-iv") == 0) 25674664626SKris Kennaway { 25774664626SKris Kennaway if (--argc < 1) goto bad; 25874664626SKris Kennaway hiv= *(++argv); 25974664626SKris Kennaway } 2606be8ae07SJacques Vidrine else if (strcmp(*argv,"-md") == 0) 2616be8ae07SJacques Vidrine { 2626be8ae07SJacques Vidrine if (--argc < 1) goto bad; 2636be8ae07SJacques Vidrine md= *(++argv); 2646be8ae07SJacques Vidrine } 265db522d3aSSimon L. B. Nielsen else if (strcmp(*argv,"-non-fips-allow") == 0) 266db522d3aSSimon L. B. Nielsen non_fips_allow = 1; 26774664626SKris Kennaway else if ((argv[0][0] == '-') && 26874664626SKris Kennaway ((c=EVP_get_cipherbyname(&(argv[0][1]))) != NULL)) 26974664626SKris Kennaway { 27074664626SKris Kennaway cipher=c; 27174664626SKris Kennaway } 27274664626SKris Kennaway else if (strcmp(*argv,"-none") == 0) 27374664626SKris Kennaway cipher=NULL; 27474664626SKris Kennaway else 27574664626SKris Kennaway { 27674664626SKris Kennaway BIO_printf(bio_err,"unknown option '%s'\n",*argv); 27774664626SKris Kennaway bad: 27874664626SKris Kennaway BIO_printf(bio_err,"options are\n"); 27974664626SKris Kennaway BIO_printf(bio_err,"%-14s input file\n","-in <file>"); 280f579bf8eSKris Kennaway BIO_printf(bio_err,"%-14s output file\n","-out <file>"); 281f579bf8eSKris Kennaway BIO_printf(bio_err,"%-14s pass phrase source\n","-pass <arg>"); 28274664626SKris Kennaway BIO_printf(bio_err,"%-14s encrypt\n","-e"); 28374664626SKris Kennaway BIO_printf(bio_err,"%-14s decrypt\n","-d"); 28474664626SKris Kennaway BIO_printf(bio_err,"%-14s base64 encode/decode, depending on encryption flag\n","-a/-base64"); 2856be8ae07SJacques Vidrine BIO_printf(bio_err,"%-14s passphrase is the next argument\n","-k"); 2866be8ae07SJacques Vidrine BIO_printf(bio_err,"%-14s passphrase is the first line of the file argument\n","-kfile"); 2876be8ae07SJacques Vidrine BIO_printf(bio_err,"%-14s the next argument is the md to use to create a key\n","-md"); 2886be8ae07SJacques Vidrine BIO_printf(bio_err,"%-14s from a passphrase. One of md2, md5, sha or sha1\n",""); 28974664626SKris Kennaway BIO_printf(bio_err,"%-14s key/iv in hex is the next argument\n","-K/-iv"); 29074664626SKris Kennaway BIO_printf(bio_err,"%-14s print the iv/key (then exit if -P)\n","-[pP]"); 29174664626SKris Kennaway BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>"); 292fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 2935c87c606SMark Murray BIO_printf(bio_err,"%-14s use engine e, possibly a hardware device.\n","-engine e"); 294fceca8a3SJacques Vidrine #endif 29574664626SKris Kennaway 29674664626SKris Kennaway BIO_printf(bio_err,"Cipher Types\n"); 2975c87c606SMark Murray OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, 2985c87c606SMark Murray show_ciphers, 2995c87c606SMark Murray bio_err); 3005c87c606SMark Murray BIO_printf(bio_err,"\n"); 30174664626SKris Kennaway 30274664626SKris Kennaway goto end; 30374664626SKris Kennaway } 30474664626SKris Kennaway argc--; 30574664626SKris Kennaway argv++; 30674664626SKris Kennaway } 30774664626SKris Kennaway 308fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 3095c87c606SMark Murray e = setup_engine(bio_err, engine, 0); 310fceca8a3SJacques Vidrine #endif 3115c87c606SMark Murray 3126be8ae07SJacques Vidrine if (md && (dgst=EVP_get_digestbyname(md)) == NULL) 3136be8ae07SJacques Vidrine { 3146be8ae07SJacques Vidrine BIO_printf(bio_err,"%s is an unsupported message digest type\n",md); 3156be8ae07SJacques Vidrine goto end; 3166be8ae07SJacques Vidrine } 3176be8ae07SJacques Vidrine 3186be8ae07SJacques Vidrine if (dgst == NULL) 3196be8ae07SJacques Vidrine { 320db522d3aSSimon L. B. Nielsen if (in_FIPS_mode) 321db522d3aSSimon L. B. Nielsen dgst = EVP_sha1(); 322db522d3aSSimon L. B. Nielsen else 3236be8ae07SJacques Vidrine dgst = EVP_md5(); 3246be8ae07SJacques Vidrine } 3256be8ae07SJacques Vidrine 32674664626SKris Kennaway if (bufsize != NULL) 32774664626SKris Kennaway { 32874664626SKris Kennaway unsigned long n; 32974664626SKris Kennaway 33074664626SKris Kennaway for (n=0; *bufsize; bufsize++) 33174664626SKris Kennaway { 33274664626SKris Kennaway i= *bufsize; 33374664626SKris Kennaway if ((i <= '9') && (i >= '0')) 33474664626SKris Kennaway n=n*10+i-'0'; 33574664626SKris Kennaway else if (i == 'k') 33674664626SKris Kennaway { 33774664626SKris Kennaway n*=1024; 33874664626SKris Kennaway bufsize++; 33974664626SKris Kennaway break; 34074664626SKris Kennaway } 34174664626SKris Kennaway } 34274664626SKris Kennaway if (*bufsize != '\0') 34374664626SKris Kennaway { 34474664626SKris Kennaway BIO_printf(bio_err,"invalid 'bufsize' specified.\n"); 34574664626SKris Kennaway goto end; 34674664626SKris Kennaway } 34774664626SKris Kennaway 34874664626SKris Kennaway /* It must be large enough for a base64 encoded line */ 349ed5d4f9aSSimon L. B. Nielsen if (base64 && n < 80) n=80; 35074664626SKris Kennaway 35174664626SKris Kennaway bsize=(int)n; 35274664626SKris Kennaway if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize); 35374664626SKris Kennaway } 35474664626SKris Kennaway 355ddd58736SKris Kennaway strbuf=OPENSSL_malloc(SIZE); 356ddd58736SKris Kennaway buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize)); 35774664626SKris Kennaway if ((buff == NULL) || (strbuf == NULL)) 35874664626SKris Kennaway { 359ddd58736SKris Kennaway BIO_printf(bio_err,"OPENSSL_malloc failure %ld\n",(long)EVP_ENCODE_LENGTH(bsize)); 36074664626SKris Kennaway goto end; 36174664626SKris Kennaway } 36274664626SKris Kennaway 36374664626SKris Kennaway in=BIO_new(BIO_s_file()); 36474664626SKris Kennaway out=BIO_new(BIO_s_file()); 36574664626SKris Kennaway if ((in == NULL) || (out == NULL)) 36674664626SKris Kennaway { 36774664626SKris Kennaway ERR_print_errors(bio_err); 36874664626SKris Kennaway goto end; 36974664626SKris Kennaway } 37074664626SKris Kennaway if (debug) 37174664626SKris Kennaway { 37274664626SKris Kennaway BIO_set_callback(in,BIO_debug_callback); 37374664626SKris Kennaway BIO_set_callback(out,BIO_debug_callback); 3745471f83eSSimon L. B. Nielsen BIO_set_callback_arg(in,(char *)bio_err); 3755471f83eSSimon L. B. Nielsen BIO_set_callback_arg(out,(char *)bio_err); 37674664626SKris Kennaway } 37774664626SKris Kennaway 37874664626SKris Kennaway if (inf == NULL) 379ed5d4f9aSSimon L. B. Nielsen { 380ed5d4f9aSSimon L. B. Nielsen if (bufsize != NULL) 381ed5d4f9aSSimon L. B. Nielsen setvbuf(stdin, (char *)NULL, _IONBF, 0); 38274664626SKris Kennaway BIO_set_fp(in,stdin,BIO_NOCLOSE); 383ed5d4f9aSSimon L. B. Nielsen } 38474664626SKris Kennaway else 38574664626SKris Kennaway { 38674664626SKris Kennaway if (BIO_read_filename(in,inf) <= 0) 38774664626SKris Kennaway { 38874664626SKris Kennaway perror(inf); 38974664626SKris Kennaway goto end; 39074664626SKris Kennaway } 39174664626SKris Kennaway } 39274664626SKris Kennaway 393f579bf8eSKris Kennaway if(!str && passarg) { 394f579bf8eSKris Kennaway if(!app_passwd(bio_err, passarg, NULL, &pass, NULL)) { 395f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting password\n"); 396f579bf8eSKris Kennaway goto end; 397f579bf8eSKris Kennaway } 398f579bf8eSKris Kennaway str = pass; 399f579bf8eSKris Kennaway } 400f579bf8eSKris Kennaway 40174664626SKris Kennaway if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) 40274664626SKris Kennaway { 40374664626SKris Kennaway for (;;) 40474664626SKris Kennaway { 40574664626SKris Kennaway char buf[200]; 40674664626SKris Kennaway 407ced566fdSJacques Vidrine BIO_snprintf(buf,sizeof buf,"enter %s %s password:", 40874664626SKris Kennaway OBJ_nid2ln(EVP_CIPHER_nid(cipher)), 40974664626SKris Kennaway (enc)?"encryption":"decryption"); 41074664626SKris Kennaway strbuf[0]='\0'; 41174664626SKris Kennaway i=EVP_read_pw_string((char *)strbuf,SIZE,buf,enc); 41274664626SKris Kennaway if (i == 0) 41374664626SKris Kennaway { 41474664626SKris Kennaway if (strbuf[0] == '\0') 41574664626SKris Kennaway { 41674664626SKris Kennaway ret=1; 41774664626SKris Kennaway goto end; 41874664626SKris Kennaway } 41974664626SKris Kennaway str=strbuf; 42074664626SKris Kennaway break; 42174664626SKris Kennaway } 42274664626SKris Kennaway if (i < 0) 42374664626SKris Kennaway { 42474664626SKris Kennaway BIO_printf(bio_err,"bad password read\n"); 42574664626SKris Kennaway goto end; 42674664626SKris Kennaway } 42774664626SKris Kennaway } 42874664626SKris Kennaway } 42974664626SKris Kennaway 43074664626SKris Kennaway 43174664626SKris Kennaway if (outf == NULL) 432ddd58736SKris Kennaway { 43374664626SKris Kennaway BIO_set_fp(out,stdout,BIO_NOCLOSE); 434ed5d4f9aSSimon L. B. Nielsen if (bufsize != NULL) 435ed5d4f9aSSimon L. B. Nielsen setvbuf(stdout, (char *)NULL, _IONBF, 0); 4365c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 437ddd58736SKris Kennaway { 438ddd58736SKris Kennaway BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 439ddd58736SKris Kennaway out = BIO_push(tmpbio, out); 440ddd58736SKris Kennaway } 441ddd58736SKris Kennaway #endif 442ddd58736SKris Kennaway } 44374664626SKris Kennaway else 44474664626SKris Kennaway { 44574664626SKris Kennaway if (BIO_write_filename(out,outf) <= 0) 44674664626SKris Kennaway { 44774664626SKris Kennaway perror(outf); 44874664626SKris Kennaway goto end; 44974664626SKris Kennaway } 45074664626SKris Kennaway } 45174664626SKris Kennaway 45274664626SKris Kennaway rbio=in; 45374664626SKris Kennaway wbio=out; 45474664626SKris Kennaway 45574664626SKris Kennaway if (base64) 45674664626SKris Kennaway { 45774664626SKris Kennaway if ((b64=BIO_new(BIO_f_base64())) == NULL) 45874664626SKris Kennaway goto end; 45974664626SKris Kennaway if (debug) 46074664626SKris Kennaway { 46174664626SKris Kennaway BIO_set_callback(b64,BIO_debug_callback); 4625471f83eSSimon L. B. Nielsen BIO_set_callback_arg(b64,(char *)bio_err); 46374664626SKris Kennaway } 46474664626SKris Kennaway if (olb64) 46574664626SKris Kennaway BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 46674664626SKris Kennaway if (enc) 46774664626SKris Kennaway wbio=BIO_push(b64,wbio); 46874664626SKris Kennaway else 46974664626SKris Kennaway rbio=BIO_push(b64,rbio); 47074664626SKris Kennaway } 47174664626SKris Kennaway 472f579bf8eSKris Kennaway if (cipher != NULL) 473f579bf8eSKris Kennaway { 4745c87c606SMark Murray /* Note that str is NULL if a key was passed on the command 4755c87c606SMark Murray * line, so we get no salt in that case. Is this a bug? 4765c87c606SMark Murray */ 477f579bf8eSKris Kennaway if (str != NULL) 478f579bf8eSKris Kennaway { 479f579bf8eSKris Kennaway /* Salt handling: if encrypting generate a salt and 480f579bf8eSKris Kennaway * write to output BIO. If decrypting read salt from 481f579bf8eSKris Kennaway * input BIO. 482f579bf8eSKris Kennaway */ 483f579bf8eSKris Kennaway unsigned char *sptr; 484f579bf8eSKris Kennaway if(nosalt) sptr = NULL; 485f579bf8eSKris Kennaway else { 486f579bf8eSKris Kennaway if(enc) { 487f579bf8eSKris Kennaway if(hsalt) { 4885c87c606SMark Murray if(!set_hex(hsalt,salt,sizeof salt)) { 489f579bf8eSKris Kennaway BIO_printf(bio_err, 490f579bf8eSKris Kennaway "invalid hex salt value\n"); 491f579bf8eSKris Kennaway goto end; 492f579bf8eSKris Kennaway } 4935c87c606SMark Murray } else if (RAND_pseudo_bytes(salt, sizeof salt) < 0) 494f579bf8eSKris Kennaway goto end; 495f579bf8eSKris Kennaway /* If -P option then don't bother writing */ 496f579bf8eSKris Kennaway if((printkey != 2) 497f579bf8eSKris Kennaway && (BIO_write(wbio,magic, 498f579bf8eSKris Kennaway sizeof magic-1) != sizeof magic-1 499f579bf8eSKris Kennaway || BIO_write(wbio, 500f579bf8eSKris Kennaway (char *)salt, 5015c87c606SMark Murray sizeof salt) != sizeof salt)) { 502f579bf8eSKris Kennaway BIO_printf(bio_err,"error writing output file\n"); 503f579bf8eSKris Kennaway goto end; 504f579bf8eSKris Kennaway } 505f579bf8eSKris Kennaway } else if(BIO_read(rbio,mbuf,sizeof mbuf) != sizeof mbuf 506f579bf8eSKris Kennaway || BIO_read(rbio, 507f579bf8eSKris Kennaway (unsigned char *)salt, 5085c87c606SMark Murray sizeof salt) != sizeof salt) { 509f579bf8eSKris Kennaway BIO_printf(bio_err,"error reading input file\n"); 510f579bf8eSKris Kennaway goto end; 511f579bf8eSKris Kennaway } else if(memcmp(mbuf,magic,sizeof magic-1)) { 512f579bf8eSKris Kennaway BIO_printf(bio_err,"bad magic number\n"); 513f579bf8eSKris Kennaway goto end; 514f579bf8eSKris Kennaway } 515f579bf8eSKris Kennaway 516f579bf8eSKris Kennaway sptr = salt; 517f579bf8eSKris Kennaway } 518f579bf8eSKris Kennaway 5196be8ae07SJacques Vidrine EVP_BytesToKey(cipher,dgst,sptr, 520f579bf8eSKris Kennaway (unsigned char *)str, 521f579bf8eSKris Kennaway strlen(str),1,key,iv); 522f579bf8eSKris Kennaway /* zero the complete buffer or the string 523f579bf8eSKris Kennaway * passed from the command line 524f579bf8eSKris Kennaway * bug picked up by 525f579bf8eSKris Kennaway * Larry J. Hughes Jr. <hughes@indiana.edu> */ 526f579bf8eSKris Kennaway if (str == strbuf) 5275c87c606SMark Murray OPENSSL_cleanse(str,SIZE); 528f579bf8eSKris Kennaway else 5295c87c606SMark Murray OPENSSL_cleanse(str,strlen(str)); 530f579bf8eSKris Kennaway } 5315c87c606SMark Murray if ((hiv != NULL) && !set_hex(hiv,iv,sizeof iv)) 532f579bf8eSKris Kennaway { 533f579bf8eSKris Kennaway BIO_printf(bio_err,"invalid hex iv value\n"); 534f579bf8eSKris Kennaway goto end; 535f579bf8eSKris Kennaway } 536db522d3aSSimon L. B. Nielsen if ((hiv == NULL) && (str == NULL) 537db522d3aSSimon L. B. Nielsen && EVP_CIPHER_iv_length(cipher) != 0) 53826d191b4SKris Kennaway { 53926d191b4SKris Kennaway /* No IV was explicitly set and no IV was generated 54026d191b4SKris Kennaway * during EVP_BytesToKey. Hence the IV is undefined, 54126d191b4SKris Kennaway * making correct decryption impossible. */ 54226d191b4SKris Kennaway BIO_printf(bio_err, "iv undefined\n"); 54326d191b4SKris Kennaway goto end; 54426d191b4SKris Kennaway } 5455c87c606SMark Murray if ((hkey != NULL) && !set_hex(hkey,key,sizeof key)) 546f579bf8eSKris Kennaway { 547f579bf8eSKris Kennaway BIO_printf(bio_err,"invalid hex key value\n"); 548f579bf8eSKris Kennaway goto end; 549f579bf8eSKris Kennaway } 550f579bf8eSKris Kennaway 551f579bf8eSKris Kennaway if ((benc=BIO_new(BIO_f_cipher())) == NULL) 552f579bf8eSKris Kennaway goto end; 5533b4e3dcbSSimon L. B. Nielsen 5543b4e3dcbSSimon L. B. Nielsen /* Since we may be changing parameters work on the encryption 5553b4e3dcbSSimon L. B. Nielsen * context rather than calling BIO_set_cipher(). 5563b4e3dcbSSimon L. B. Nielsen */ 5573b4e3dcbSSimon L. B. Nielsen 5585c87c606SMark Murray BIO_get_cipher_ctx(benc, &ctx); 559db522d3aSSimon L. B. Nielsen 560db522d3aSSimon L. B. Nielsen if (non_fips_allow) 561db522d3aSSimon L. B. Nielsen EVP_CIPHER_CTX_set_flags(ctx, 562db522d3aSSimon L. B. Nielsen EVP_CIPH_FLAG_NON_FIPS_ALLOW); 563db522d3aSSimon L. B. Nielsen 5643b4e3dcbSSimon L. B. Nielsen if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) 5653b4e3dcbSSimon L. B. Nielsen { 5663b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error setting cipher %s\n", 5673b4e3dcbSSimon L. B. Nielsen EVP_CIPHER_name(cipher)); 5683b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 5693b4e3dcbSSimon L. B. Nielsen goto end; 5705c87c606SMark Murray } 5713b4e3dcbSSimon L. B. Nielsen 5723b4e3dcbSSimon L. B. Nielsen if (nopad) 5733b4e3dcbSSimon L. B. Nielsen EVP_CIPHER_CTX_set_padding(ctx, 0); 5743b4e3dcbSSimon L. B. Nielsen 5753b4e3dcbSSimon L. B. Nielsen if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) 5763b4e3dcbSSimon L. B. Nielsen { 5773b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error setting cipher %s\n", 5783b4e3dcbSSimon L. B. Nielsen EVP_CIPHER_name(cipher)); 5793b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 5803b4e3dcbSSimon L. B. Nielsen goto end; 5813b4e3dcbSSimon L. B. Nielsen } 5823b4e3dcbSSimon L. B. Nielsen 583f579bf8eSKris Kennaway if (debug) 584f579bf8eSKris Kennaway { 585f579bf8eSKris Kennaway BIO_set_callback(benc,BIO_debug_callback); 5865471f83eSSimon L. B. Nielsen BIO_set_callback_arg(benc,(char *)bio_err); 587f579bf8eSKris Kennaway } 588f579bf8eSKris Kennaway 589f579bf8eSKris Kennaway if (printkey) 590f579bf8eSKris Kennaway { 591f579bf8eSKris Kennaway if (!nosalt) 592f579bf8eSKris Kennaway { 593f579bf8eSKris Kennaway printf("salt="); 5943b4e3dcbSSimon L. B. Nielsen for (i=0; i<(int)sizeof(salt); i++) 595f579bf8eSKris Kennaway printf("%02X",salt[i]); 596f579bf8eSKris Kennaway printf("\n"); 597f579bf8eSKris Kennaway } 598f579bf8eSKris Kennaway if (cipher->key_len > 0) 599f579bf8eSKris Kennaway { 600f579bf8eSKris Kennaway printf("key="); 601f579bf8eSKris Kennaway for (i=0; i<cipher->key_len; i++) 602f579bf8eSKris Kennaway printf("%02X",key[i]); 603f579bf8eSKris Kennaway printf("\n"); 604f579bf8eSKris Kennaway } 605f579bf8eSKris Kennaway if (cipher->iv_len > 0) 606f579bf8eSKris Kennaway { 607f579bf8eSKris Kennaway printf("iv ="); 608f579bf8eSKris Kennaway for (i=0; i<cipher->iv_len; i++) 609f579bf8eSKris Kennaway printf("%02X",iv[i]); 610f579bf8eSKris Kennaway printf("\n"); 611f579bf8eSKris Kennaway } 612f579bf8eSKris Kennaway if (printkey == 2) 613f579bf8eSKris Kennaway { 614f579bf8eSKris Kennaway ret=0; 615f579bf8eSKris Kennaway goto end; 616f579bf8eSKris Kennaway } 617f579bf8eSKris Kennaway } 618f579bf8eSKris Kennaway } 619f579bf8eSKris Kennaway 62074664626SKris Kennaway /* Only encrypt/decrypt as we write the file */ 62174664626SKris Kennaway if (benc != NULL) 62274664626SKris Kennaway wbio=BIO_push(benc,wbio); 62374664626SKris Kennaway 62474664626SKris Kennaway for (;;) 62574664626SKris Kennaway { 62674664626SKris Kennaway inl=BIO_read(rbio,(char *)buff,bsize); 62774664626SKris Kennaway if (inl <= 0) break; 62874664626SKris Kennaway if (BIO_write(wbio,(char *)buff,inl) != inl) 62974664626SKris Kennaway { 63074664626SKris Kennaway BIO_printf(bio_err,"error writing output file\n"); 63174664626SKris Kennaway goto end; 63274664626SKris Kennaway } 63374664626SKris Kennaway } 63474664626SKris Kennaway if (!BIO_flush(wbio)) 63574664626SKris Kennaway { 63674664626SKris Kennaway BIO_printf(bio_err,"bad decrypt\n"); 63774664626SKris Kennaway goto end; 63874664626SKris Kennaway } 63974664626SKris Kennaway 64074664626SKris Kennaway ret=0; 64174664626SKris Kennaway if (verbose) 64274664626SKris Kennaway { 64374664626SKris Kennaway BIO_printf(bio_err,"bytes read :%8ld\n",BIO_number_read(in)); 64474664626SKris Kennaway BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out)); 64574664626SKris Kennaway } 64674664626SKris Kennaway end: 647f579bf8eSKris Kennaway ERR_print_errors(bio_err); 648ddd58736SKris Kennaway if (strbuf != NULL) OPENSSL_free(strbuf); 649ddd58736SKris Kennaway if (buff != NULL) OPENSSL_free(buff); 65074664626SKris Kennaway if (in != NULL) BIO_free(in); 651ddd58736SKris Kennaway if (out != NULL) BIO_free_all(out); 65274664626SKris Kennaway if (benc != NULL) BIO_free(benc); 65374664626SKris Kennaway if (b64 != NULL) BIO_free(b64); 654ddd58736SKris Kennaway if(pass) OPENSSL_free(pass); 6555c87c606SMark Murray apps_shutdown(); 6565c87c606SMark Murray OPENSSL_EXIT(ret); 65774664626SKris Kennaway } 65874664626SKris Kennaway 65974664626SKris Kennaway int set_hex(char *in, unsigned char *out, int size) 66074664626SKris Kennaway { 66174664626SKris Kennaway int i,n; 66274664626SKris Kennaway unsigned char j; 66374664626SKris Kennaway 66474664626SKris Kennaway n=strlen(in); 66574664626SKris Kennaway if (n > (size*2)) 66674664626SKris Kennaway { 66774664626SKris Kennaway BIO_printf(bio_err,"hex string is too long\n"); 66874664626SKris Kennaway return(0); 66974664626SKris Kennaway } 67074664626SKris Kennaway memset(out,0,size); 67174664626SKris Kennaway for (i=0; i<n; i++) 67274664626SKris Kennaway { 67374664626SKris Kennaway j=(unsigned char)*in; 67474664626SKris Kennaway *(in++)='\0'; 67574664626SKris Kennaway if (j == 0) break; 67674664626SKris Kennaway if ((j >= '0') && (j <= '9')) 67774664626SKris Kennaway j-='0'; 67874664626SKris Kennaway else if ((j >= 'A') && (j <= 'F')) 67974664626SKris Kennaway j=j-'A'+10; 68074664626SKris Kennaway else if ((j >= 'a') && (j <= 'f')) 68174664626SKris Kennaway j=j-'a'+10; 68274664626SKris Kennaway else 68374664626SKris Kennaway { 68474664626SKris Kennaway BIO_printf(bio_err,"non-hex digit\n"); 68574664626SKris Kennaway return(0); 68674664626SKris Kennaway } 68774664626SKris Kennaway if (i&1) 68874664626SKris Kennaway out[i/2]|=j; 68974664626SKris Kennaway else 69074664626SKris Kennaway out[i/2]=(j<<4); 69174664626SKris Kennaway } 69274664626SKris Kennaway return(1); 69374664626SKris Kennaway } 694