Main Page | Class List | Directories | File List | Class Members | File Members

dsa.c

Go to the documentation of this file.
00001 /* apps/dsa.c */
00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00003  * All rights reserved.
00004  *
00005  * This package is an SSL implementation written
00006  * by Eric Young (eay@cryptsoft.com).
00007  * The implementation was written so as to conform with Netscapes SSL.
00008  * 
00009  * This library is free for commercial and non-commercial use as long as
00010  * the following conditions are aheared to.  The following conditions
00011  * apply to all code found in this distribution, be it the RC4, RSA,
00012  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00013  * included with this distribution is covered by the same copyright terms
00014  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00015  * 
00016  * Copyright remains Eric Young's, and as such any Copyright notices in
00017  * the code are not to be removed.
00018  * If this package is used in a product, Eric Young should be given attribution
00019  * as the author of the parts of the library used.
00020  * This can be in the form of a textual message at program startup or
00021  * in documentation (online or textual) provided with the package.
00022  * 
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  * 3. All advertising materials mentioning features or use of this software
00032  *    must display the following acknowledgement:
00033  *    "This product includes cryptographic software written by
00034  *     Eric Young (eay@cryptsoft.com)"
00035  *    The word 'cryptographic' can be left out if the rouines from the library
00036  *    being used are not cryptographic related :-).
00037  * 4. If you include any Windows specific code (or a derivative thereof) from 
00038  *    the apps directory (application code) you must include an acknowledgement:
00039  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00040  * 
00041  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  * 
00053  * The licence and distribution terms for any publically available version or
00054  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00055  * copied and put under another distribution licence
00056  * [including the GNU Public Licence.]
00057  */
00058 
00059 #include <openssl/opensslconf.h>        /* for OPENSSL_NO_DSA */
00060 #ifndef OPENSSL_NO_DSA
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 #include <string.h>
00064 #include <time.h>
00065 #include "apps.h"
00066 #include <openssl/bio.h>
00067 #include <openssl/err.h>
00068 #include <openssl/dsa.h>
00069 #include <openssl/evp.h>
00070 #include <openssl/x509.h>
00071 #include <openssl/pem.h>
00072 #include <openssl/bn.h>
00073 
00074 #undef PROG
00075 #define PROG    dsa_main
00076 
00077 /* -inform arg  - input format - default PEM (one of DER, NET or PEM)
00078  * -outform arg - output format - default PEM
00079  * -in arg      - input file - default stdin
00080  * -out arg     - output file - default stdout
00081  * -des         - encrypt output if PEM format with DES in cbc mode
00082  * -des3        - encrypt output if PEM format
00083  * -idea        - encrypt output if PEM format
00084  * -aes128      - encrypt output if PEM format
00085  * -aes192      - encrypt output if PEM format
00086  * -aes256      - encrypt output if PEM format
00087  * -text        - print a text version
00088  * -modulus     - print the DSA public key
00089  */
00090 
00091 int MAIN(int, char **);
00092 
00093 int MAIN(int argc, char **argv)
00094         {
00095 #ifndef OPENSSL_NO_ENGINE
00096         ENGINE *e = NULL;
00097 #endif
00098         int ret=1;
00099         DSA *dsa=NULL;
00100         int i,badops=0;
00101         const EVP_CIPHER *enc=NULL;
00102         BIO *in=NULL,*out=NULL;
00103         int informat,outformat,text=0,noout=0;
00104         int pubin = 0, pubout = 0;
00105         char *infile,*outfile,*prog;
00106 #ifndef OPENSSL_NO_ENGINE
00107         char *engine;
00108 #endif
00109         char *passargin = NULL, *passargout = NULL;
00110         char *passin = NULL, *passout = NULL;
00111         int modulus=0;
00112 
00113         apps_startup();
00114 
00115         if (bio_err == NULL)
00116                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00117                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
00118 
00119         if (!load_config(bio_err, NULL))
00120                 goto end;
00121 
00122 #ifndef OPENSSL_NO_ENGINE
00123         engine=NULL;
00124 #endif
00125         infile=NULL;
00126         outfile=NULL;
00127         informat=FORMAT_PEM;
00128         outformat=FORMAT_PEM;
00129 
00130         prog=argv[0];
00131         argc--;
00132         argv++;
00133         while (argc >= 1)
00134                 {
00135                 if      (strcmp(*argv,"-inform") == 0)
00136                         {
00137                         if (--argc < 1) goto bad;
00138                         informat=str2fmt(*(++argv));
00139                         }
00140                 else if (strcmp(*argv,"-outform") == 0)
00141                         {
00142                         if (--argc < 1) goto bad;
00143                         outformat=str2fmt(*(++argv));
00144                         }
00145                 else if (strcmp(*argv,"-in") == 0)
00146                         {
00147                         if (--argc < 1) goto bad;
00148                         infile= *(++argv);
00149                         }
00150                 else if (strcmp(*argv,"-out") == 0)
00151                         {
00152                         if (--argc < 1) goto bad;
00153                         outfile= *(++argv);
00154                         }
00155                 else if (strcmp(*argv,"-passin") == 0)
00156                         {
00157                         if (--argc < 1) goto bad;
00158                         passargin= *(++argv);
00159                         }
00160                 else if (strcmp(*argv,"-passout") == 0)
00161                         {
00162                         if (--argc < 1) goto bad;
00163                         passargout= *(++argv);
00164                         }
00165 #ifndef OPENSSL_NO_ENGINE
00166                 else if (strcmp(*argv,"-engine") == 0)
00167                         {
00168                         if (--argc < 1) goto bad;
00169                         engine= *(++argv);
00170                         }
00171 #endif
00172                 else if (strcmp(*argv,"-noout") == 0)
00173                         noout=1;
00174                 else if (strcmp(*argv,"-text") == 0)
00175                         text=1;
00176                 else if (strcmp(*argv,"-modulus") == 0)
00177                         modulus=1;
00178                 else if (strcmp(*argv,"-pubin") == 0)
00179                         pubin=1;
00180                 else if (strcmp(*argv,"-pubout") == 0)
00181                         pubout=1;
00182                 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
00183                         {
00184                         BIO_printf(bio_err,"unknown option %s\n",*argv);
00185                         badops=1;
00186                         break;
00187                         }
00188                 argc--;
00189                 argv++;
00190                 }
00191 
00192         if (badops)
00193                 {
00194 bad:
00195                 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
00196                 BIO_printf(bio_err,"where options are\n");
00197                 BIO_printf(bio_err," -inform arg     input format - DER or PEM\n");
00198                 BIO_printf(bio_err," -outform arg    output format - DER or PEM\n");
00199                 BIO_printf(bio_err," -in arg         input file\n");
00200                 BIO_printf(bio_err," -passin arg     input file pass phrase source\n");
00201                 BIO_printf(bio_err," -out arg        output file\n");
00202                 BIO_printf(bio_err," -passout arg    output file pass phrase source\n");
00203 #ifndef OPENSSL_NO_ENGINE
00204                 BIO_printf(bio_err," -engine e       use engine e, possibly a hardware device.\n");
00205 #endif
00206                 BIO_printf(bio_err," -des            encrypt PEM output with cbc des\n");
00207                 BIO_printf(bio_err," -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
00208 #ifndef OPENSSL_NO_IDEA
00209                 BIO_printf(bio_err," -idea           encrypt PEM output with cbc idea\n");
00210 #endif
00211 #ifndef OPENSSL_NO_AES
00212                 BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
00213                 BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
00214 #endif
00215                 BIO_printf(bio_err," -text           print the key in text\n");
00216                 BIO_printf(bio_err," -noout          don't print key out\n");
00217                 BIO_printf(bio_err," -modulus        print the DSA public value\n");
00218                 goto end;
00219                 }
00220 
00221         ERR_load_crypto_strings();
00222 
00223 #ifndef OPENSSL_NO_ENGINE
00224         e = setup_engine(bio_err, engine, 0);
00225 #endif
00226 
00227         if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
00228                 BIO_printf(bio_err, "Error getting passwords\n");
00229                 goto end;
00230         }
00231 
00232         in=BIO_new(BIO_s_file());
00233         out=BIO_new(BIO_s_file());
00234         if ((in == NULL) || (out == NULL))
00235                 {
00236                 ERR_print_errors(bio_err);
00237                 goto end;
00238                 }
00239 
00240         if (infile == NULL)
00241                 BIO_set_fp(in,stdin,BIO_NOCLOSE);
00242         else
00243                 {
00244                 if (BIO_read_filename(in,infile) <= 0)
00245                         {
00246                         perror(infile);
00247                         goto end;
00248                         }
00249                 }
00250 
00251         BIO_printf(bio_err,"read DSA key\n");
00252         if      (informat == FORMAT_ASN1) {
00253                 if(pubin) dsa=d2i_DSA_PUBKEY_bio(in,NULL);
00254                 else dsa=d2i_DSAPrivateKey_bio(in,NULL);
00255         } else if (informat == FORMAT_PEM) {
00256                 if(pubin) dsa=PEM_read_bio_DSA_PUBKEY(in,NULL, NULL, NULL);
00257                 else dsa=PEM_read_bio_DSAPrivateKey(in,NULL,NULL,passin);
00258         } else
00259                 {
00260                 BIO_printf(bio_err,"bad input format specified for key\n");
00261                 goto end;
00262                 }
00263         if (dsa == NULL)
00264                 {
00265                 BIO_printf(bio_err,"unable to load Key\n");
00266                 ERR_print_errors(bio_err);
00267                 goto end;
00268                 }
00269 
00270         if (outfile == NULL)
00271                 {
00272                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
00273 #ifdef OPENSSL_SYS_VMS
00274                 {
00275                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00276                 out = BIO_push(tmpbio, out);
00277                 }
00278 #endif
00279                 }
00280         else
00281                 {
00282                 if (BIO_write_filename(out,outfile) <= 0)
00283                         {
00284                         perror(outfile);
00285                         goto end;
00286                         }
00287                 }
00288 
00289         if (text) 
00290                 if (!DSA_print(out,dsa,0))
00291                         {
00292                         perror(outfile);
00293                         ERR_print_errors(bio_err);
00294                         goto end;
00295                         }
00296 
00297         if (modulus)
00298                 {
00299                 fprintf(stdout,"Public Key=");
00300                 BN_print(out,dsa->pub_key);
00301                 fprintf(stdout,"\n");
00302                 }
00303 
00304         if (noout) goto end;
00305         BIO_printf(bio_err,"writing DSA key\n");
00306         if      (outformat == FORMAT_ASN1) {
00307                 if(pubin || pubout) i=i2d_DSA_PUBKEY_bio(out,dsa);
00308                 else i=i2d_DSAPrivateKey_bio(out,dsa);
00309         } else if (outformat == FORMAT_PEM) {
00310                 if(pubin || pubout)
00311                         i=PEM_write_bio_DSA_PUBKEY(out,dsa);
00312                 else i=PEM_write_bio_DSAPrivateKey(out,dsa,enc,
00313                                                         NULL,0,NULL, passout);
00314         } else {
00315                 BIO_printf(bio_err,"bad output format specified for outfile\n");
00316                 goto end;
00317                 }
00318         if (!i)
00319                 {
00320                 BIO_printf(bio_err,"unable to write private key\n");
00321                 ERR_print_errors(bio_err);
00322                 }
00323         else
00324                 ret=0;
00325 end:
00326         if(in != NULL) BIO_free(in);
00327         if(out != NULL) BIO_free_all(out);
00328         if(dsa != NULL) DSA_free(dsa);
00329         if(passin) OPENSSL_free(passin);
00330         if(passout) OPENSSL_free(passout);
00331         apps_shutdown();
00332         OPENSSL_EXIT(ret);
00333         }
00334 #endif

© sourcejam.com 2005-2008