174664626SKris Kennaway /* apps/dsa.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 #ifndef NO_DSA 6074664626SKris Kennaway #include <stdio.h> 6174664626SKris Kennaway #include <stdlib.h> 6274664626SKris Kennaway #include <string.h> 6374664626SKris Kennaway #include <time.h> 6474664626SKris Kennaway #include "apps.h" 6574664626SKris Kennaway #include <openssl/bio.h> 6674664626SKris Kennaway #include <openssl/err.h> 6774664626SKris Kennaway #include <openssl/dsa.h> 6874664626SKris Kennaway #include <openssl/evp.h> 6974664626SKris Kennaway #include <openssl/x509.h> 7074664626SKris Kennaway #include <openssl/pem.h> 7174664626SKris Kennaway 7274664626SKris Kennaway #undef PROG 7374664626SKris Kennaway #define PROG dsa_main 7474664626SKris Kennaway 7574664626SKris Kennaway /* -inform arg - input format - default PEM (one of DER, NET or PEM) 7674664626SKris Kennaway * -outform arg - output format - default PEM 7774664626SKris Kennaway * -in arg - input file - default stdin 7874664626SKris Kennaway * -out arg - output file - default stdout 7974664626SKris Kennaway * -des - encrypt output if PEM format with DES in cbc mode 8074664626SKris Kennaway * -des3 - encrypt output if PEM format 8174664626SKris Kennaway * -idea - encrypt output if PEM format 8274664626SKris Kennaway * -text - print a text version 8374664626SKris Kennaway * -modulus - print the DSA public key 8474664626SKris Kennaway */ 8574664626SKris Kennaway 86f579bf8eSKris Kennaway int MAIN(int, char **); 87f579bf8eSKris Kennaway 8874664626SKris Kennaway int MAIN(int argc, char **argv) 8974664626SKris Kennaway { 9074664626SKris Kennaway int ret=1; 9174664626SKris Kennaway DSA *dsa=NULL; 9274664626SKris Kennaway int i,badops=0; 9374664626SKris Kennaway const EVP_CIPHER *enc=NULL; 9474664626SKris Kennaway BIO *in=NULL,*out=NULL; 9574664626SKris Kennaway int informat,outformat,text=0,noout=0; 96f579bf8eSKris Kennaway int pubin = 0, pubout = 0; 9774664626SKris Kennaway char *infile,*outfile,*prog; 98f579bf8eSKris Kennaway char *passargin = NULL, *passargout = NULL; 99f579bf8eSKris Kennaway char *passin = NULL, *passout = NULL; 10074664626SKris Kennaway int modulus=0; 10174664626SKris Kennaway 10274664626SKris Kennaway apps_startup(); 10374664626SKris Kennaway 10474664626SKris Kennaway if (bio_err == NULL) 10574664626SKris Kennaway if ((bio_err=BIO_new(BIO_s_file())) != NULL) 10674664626SKris Kennaway BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 10774664626SKris Kennaway 10874664626SKris Kennaway infile=NULL; 10974664626SKris Kennaway outfile=NULL; 11074664626SKris Kennaway informat=FORMAT_PEM; 11174664626SKris Kennaway outformat=FORMAT_PEM; 11274664626SKris Kennaway 11374664626SKris Kennaway prog=argv[0]; 11474664626SKris Kennaway argc--; 11574664626SKris Kennaway argv++; 11674664626SKris Kennaway while (argc >= 1) 11774664626SKris Kennaway { 11874664626SKris Kennaway if (strcmp(*argv,"-inform") == 0) 11974664626SKris Kennaway { 12074664626SKris Kennaway if (--argc < 1) goto bad; 12174664626SKris Kennaway informat=str2fmt(*(++argv)); 12274664626SKris Kennaway } 12374664626SKris Kennaway else if (strcmp(*argv,"-outform") == 0) 12474664626SKris Kennaway { 12574664626SKris Kennaway if (--argc < 1) goto bad; 12674664626SKris Kennaway outformat=str2fmt(*(++argv)); 12774664626SKris Kennaway } 12874664626SKris Kennaway else if (strcmp(*argv,"-in") == 0) 12974664626SKris Kennaway { 13074664626SKris Kennaway if (--argc < 1) goto bad; 13174664626SKris Kennaway infile= *(++argv); 13274664626SKris Kennaway } 13374664626SKris Kennaway else if (strcmp(*argv,"-out") == 0) 13474664626SKris Kennaway { 13574664626SKris Kennaway if (--argc < 1) goto bad; 13674664626SKris Kennaway outfile= *(++argv); 13774664626SKris Kennaway } 138f579bf8eSKris Kennaway else if (strcmp(*argv,"-passin") == 0) 139f579bf8eSKris Kennaway { 140f579bf8eSKris Kennaway if (--argc < 1) goto bad; 141f579bf8eSKris Kennaway passargin= *(++argv); 142f579bf8eSKris Kennaway } 143f579bf8eSKris Kennaway else if (strcmp(*argv,"-passout") == 0) 144f579bf8eSKris Kennaway { 145f579bf8eSKris Kennaway if (--argc < 1) goto bad; 146f579bf8eSKris Kennaway passargout= *(++argv); 147f579bf8eSKris Kennaway } 14874664626SKris Kennaway else if (strcmp(*argv,"-noout") == 0) 14974664626SKris Kennaway noout=1; 15074664626SKris Kennaway else if (strcmp(*argv,"-text") == 0) 15174664626SKris Kennaway text=1; 15274664626SKris Kennaway else if (strcmp(*argv,"-modulus") == 0) 15374664626SKris Kennaway modulus=1; 154f579bf8eSKris Kennaway else if (strcmp(*argv,"-pubin") == 0) 155f579bf8eSKris Kennaway pubin=1; 156f579bf8eSKris Kennaway else if (strcmp(*argv,"-pubout") == 0) 157f579bf8eSKris Kennaway pubout=1; 15874664626SKris Kennaway else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL) 15974664626SKris Kennaway { 16074664626SKris Kennaway BIO_printf(bio_err,"unknown option %s\n",*argv); 16174664626SKris Kennaway badops=1; 16274664626SKris Kennaway break; 16374664626SKris Kennaway } 16474664626SKris Kennaway argc--; 16574664626SKris Kennaway argv++; 16674664626SKris Kennaway } 16774664626SKris Kennaway 16874664626SKris Kennaway if (badops) 16974664626SKris Kennaway { 17074664626SKris Kennaway bad: 17174664626SKris Kennaway BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog); 17274664626SKris Kennaway BIO_printf(bio_err,"where options are\n"); 173f579bf8eSKris Kennaway BIO_printf(bio_err," -inform arg input format - DER or PEM\n"); 174f579bf8eSKris Kennaway BIO_printf(bio_err," -outform arg output format - DER or PEM\n"); 17574664626SKris Kennaway BIO_printf(bio_err," -in arg input file\n"); 176f579bf8eSKris Kennaway BIO_printf(bio_err," -passin arg input file pass phrase source\n"); 17774664626SKris Kennaway BIO_printf(bio_err," -out arg output file\n"); 178f579bf8eSKris Kennaway BIO_printf(bio_err," -passout arg output file pass phrase source\n"); 17974664626SKris Kennaway BIO_printf(bio_err," -des encrypt PEM output with cbc des\n"); 18074664626SKris Kennaway BIO_printf(bio_err," -des3 encrypt PEM output with ede cbc des using 168 bit key\n"); 18174664626SKris Kennaway #ifndef NO_IDEA 18274664626SKris Kennaway BIO_printf(bio_err," -idea encrypt PEM output with cbc idea\n"); 18374664626SKris Kennaway #endif 18474664626SKris Kennaway BIO_printf(bio_err," -text print the key in text\n"); 18574664626SKris Kennaway BIO_printf(bio_err," -noout don't print key out\n"); 18674664626SKris Kennaway BIO_printf(bio_err," -modulus print the DSA public value\n"); 18774664626SKris Kennaway goto end; 18874664626SKris Kennaway } 18974664626SKris Kennaway 19074664626SKris Kennaway ERR_load_crypto_strings(); 19174664626SKris Kennaway 192f579bf8eSKris Kennaway if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { 193f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting passwords\n"); 194f579bf8eSKris Kennaway goto end; 195f579bf8eSKris Kennaway } 196f579bf8eSKris Kennaway 19774664626SKris Kennaway in=BIO_new(BIO_s_file()); 19874664626SKris Kennaway out=BIO_new(BIO_s_file()); 19974664626SKris Kennaway if ((in == NULL) || (out == NULL)) 20074664626SKris Kennaway { 20174664626SKris Kennaway ERR_print_errors(bio_err); 20274664626SKris Kennaway goto end; 20374664626SKris Kennaway } 20474664626SKris Kennaway 20574664626SKris Kennaway if (infile == NULL) 20674664626SKris Kennaway BIO_set_fp(in,stdin,BIO_NOCLOSE); 20774664626SKris Kennaway else 20874664626SKris Kennaway { 20974664626SKris Kennaway if (BIO_read_filename(in,infile) <= 0) 21074664626SKris Kennaway { 21174664626SKris Kennaway perror(infile); 21274664626SKris Kennaway goto end; 21374664626SKris Kennaway } 21474664626SKris Kennaway } 21574664626SKris Kennaway 216f579bf8eSKris Kennaway BIO_printf(bio_err,"read DSA key\n"); 217f579bf8eSKris Kennaway if (informat == FORMAT_ASN1) { 218f579bf8eSKris Kennaway if(pubin) dsa=d2i_DSA_PUBKEY_bio(in,NULL); 219f579bf8eSKris Kennaway else dsa=d2i_DSAPrivateKey_bio(in,NULL); 220f579bf8eSKris Kennaway } else if (informat == FORMAT_PEM) { 221f579bf8eSKris Kennaway if(pubin) dsa=PEM_read_bio_DSA_PUBKEY(in,NULL, NULL, NULL); 222f579bf8eSKris Kennaway else dsa=PEM_read_bio_DSAPrivateKey(in,NULL,NULL,passin); 223f579bf8eSKris Kennaway } else 22474664626SKris Kennaway { 22574664626SKris Kennaway BIO_printf(bio_err,"bad input format specified for key\n"); 22674664626SKris Kennaway goto end; 22774664626SKris Kennaway } 22874664626SKris Kennaway if (dsa == NULL) 22974664626SKris Kennaway { 230f579bf8eSKris Kennaway BIO_printf(bio_err,"unable to load Key\n"); 23174664626SKris Kennaway ERR_print_errors(bio_err); 23274664626SKris Kennaway goto end; 23374664626SKris Kennaway } 23474664626SKris Kennaway 23574664626SKris Kennaway if (outfile == NULL) 23674664626SKris Kennaway BIO_set_fp(out,stdout,BIO_NOCLOSE); 23774664626SKris Kennaway else 23874664626SKris Kennaway { 23974664626SKris Kennaway if (BIO_write_filename(out,outfile) <= 0) 24074664626SKris Kennaway { 24174664626SKris Kennaway perror(outfile); 24274664626SKris Kennaway goto end; 24374664626SKris Kennaway } 24474664626SKris Kennaway } 24574664626SKris Kennaway 24674664626SKris Kennaway if (text) 24774664626SKris Kennaway if (!DSA_print(out,dsa,0)) 24874664626SKris Kennaway { 24974664626SKris Kennaway perror(outfile); 25074664626SKris Kennaway ERR_print_errors(bio_err); 25174664626SKris Kennaway goto end; 25274664626SKris Kennaway } 25374664626SKris Kennaway 25474664626SKris Kennaway if (modulus) 25574664626SKris Kennaway { 25674664626SKris Kennaway fprintf(stdout,"Public Key="); 25774664626SKris Kennaway BN_print(out,dsa->pub_key); 25874664626SKris Kennaway fprintf(stdout,"\n"); 25974664626SKris Kennaway } 26074664626SKris Kennaway 26174664626SKris Kennaway if (noout) goto end; 262f579bf8eSKris Kennaway BIO_printf(bio_err,"writing DSA key\n"); 263f579bf8eSKris Kennaway if (outformat == FORMAT_ASN1) { 264f579bf8eSKris Kennaway if(pubin || pubout) i=i2d_DSA_PUBKEY_bio(out,dsa); 265f579bf8eSKris Kennaway else i=i2d_DSAPrivateKey_bio(out,dsa); 266f579bf8eSKris Kennaway } else if (outformat == FORMAT_PEM) { 267f579bf8eSKris Kennaway if(pubin || pubout) 268f579bf8eSKris Kennaway i=PEM_write_bio_DSA_PUBKEY(out,dsa); 269f579bf8eSKris Kennaway else i=PEM_write_bio_DSAPrivateKey(out,dsa,enc, 270f579bf8eSKris Kennaway NULL,0,NULL, passout); 271f579bf8eSKris Kennaway } else { 27274664626SKris Kennaway BIO_printf(bio_err,"bad output format specified for outfile\n"); 27374664626SKris Kennaway goto end; 27474664626SKris Kennaway } 27574664626SKris Kennaway if (!i) 27674664626SKris Kennaway { 27774664626SKris Kennaway BIO_printf(bio_err,"unable to write private key\n"); 27874664626SKris Kennaway ERR_print_errors(bio_err); 27974664626SKris Kennaway } 28074664626SKris Kennaway else 28174664626SKris Kennaway ret=0; 28274664626SKris Kennaway end: 28374664626SKris Kennaway if(in != NULL) BIO_free(in); 28474664626SKris Kennaway if(out != NULL) BIO_free(out); 28574664626SKris Kennaway if(dsa != NULL) DSA_free(dsa); 286f579bf8eSKris Kennaway if(passin) Free(passin); 287f579bf8eSKris Kennaway if(passout) Free(passout); 28874664626SKris Kennaway EXIT(ret); 28974664626SKris Kennaway } 29074664626SKris Kennaway #endif 291