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

gendsa.c

Go to the documentation of this file.
00001 /* apps/gendsa.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 <string.h>
00063 #include <sys/types.h>
00064 #include <sys/stat.h>
00065 #include "apps.h"
00066 #include <openssl/bio.h>
00067 #include <openssl/err.h>
00068 #include <openssl/bn.h>
00069 #include <openssl/dsa.h>
00070 #include <openssl/x509.h>
00071 #include <openssl/pem.h>
00072 
00073 #define DEFBITS 512
00074 #undef PROG
00075 #define PROG gendsa_main
00076 
00077 int MAIN(int, char **);
00078 
00079 int MAIN(int argc, char **argv)
00080         {
00081 #ifndef OPENSSL_NO_ENGINE
00082         ENGINE *e = NULL;
00083 #endif
00084         DSA *dsa=NULL;
00085         int ret=1;
00086         char *outfile=NULL;
00087         char *inrand=NULL,*dsaparams=NULL;
00088         char *passargout = NULL, *passout = NULL;
00089         BIO *out=NULL,*in=NULL;
00090         const EVP_CIPHER *enc=NULL;
00091 #ifndef OPENSSL_NO_ENGINE
00092         char *engine=NULL;
00093 #endif
00094 
00095         apps_startup();
00096 
00097         if (bio_err == NULL)
00098                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00099                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
00100 
00101         if (!load_config(bio_err, NULL))
00102                 goto end;
00103 
00104         argv++;
00105         argc--;
00106         for (;;)
00107                 {
00108                 if (argc <= 0) break;
00109                 if (strcmp(*argv,"-out") == 0)
00110                         {
00111                         if (--argc < 1) goto bad;
00112                         outfile= *(++argv);
00113                         }
00114                 else if (strcmp(*argv,"-passout") == 0)
00115                         {
00116                         if (--argc < 1) goto bad;
00117                         passargout= *(++argv);
00118                         }
00119 #ifndef OPENSSL_NO_ENGINE
00120                 else if (strcmp(*argv,"-engine") == 0)
00121                         {
00122                         if (--argc < 1) goto bad;
00123                         engine= *(++argv);
00124                         }
00125 #endif
00126                 else if (strcmp(*argv,"-rand") == 0)
00127                         {
00128                         if (--argc < 1) goto bad;
00129                         inrand= *(++argv);
00130                         }
00131                 else if (strcmp(*argv,"-") == 0)
00132                         goto bad;
00133 #ifndef OPENSSL_NO_DES
00134                 else if (strcmp(*argv,"-des") == 0)
00135                         enc=EVP_des_cbc();
00136                 else if (strcmp(*argv,"-des3") == 0)
00137                         enc=EVP_des_ede3_cbc();
00138 #endif
00139 #ifndef OPENSSL_NO_IDEA
00140                 else if (strcmp(*argv,"-idea") == 0)
00141                         enc=EVP_idea_cbc();
00142 #endif
00143 #ifndef OPENSSL_NO_AES
00144                 else if (strcmp(*argv,"-aes128") == 0)
00145                         enc=EVP_aes_128_cbc();
00146                 else if (strcmp(*argv,"-aes192") == 0)
00147                         enc=EVP_aes_192_cbc();
00148                 else if (strcmp(*argv,"-aes256") == 0)
00149                         enc=EVP_aes_256_cbc();
00150 #endif
00151                 else if (**argv != '-' && dsaparams == NULL)
00152                         {
00153                         dsaparams = *argv;
00154                         }
00155                 else
00156                         goto bad;
00157                 argv++;
00158                 argc--;
00159                 }
00160 
00161         if (dsaparams == NULL)
00162                 {
00163 bad:
00164                 BIO_printf(bio_err,"usage: gendsa [args] dsaparam-file\n");
00165                 BIO_printf(bio_err," -out file - output the key to 'file'\n");
00166 #ifndef OPENSSL_NO_DES
00167                 BIO_printf(bio_err," -des      - encrypt the generated key with DES in cbc mode\n");
00168                 BIO_printf(bio_err," -des3     - encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
00169 #endif
00170 #ifndef OPENSSL_NO_IDEA
00171                 BIO_printf(bio_err," -idea     - encrypt the generated key with IDEA in cbc mode\n");
00172 #endif
00173 #ifndef OPENSSL_NO_AES
00174                 BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
00175                 BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
00176 #endif
00177 #ifndef OPENSSL_NO_ENGINE
00178                 BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n");
00179 #endif
00180                 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
00181                 BIO_printf(bio_err,"           - load the file (or the files in the directory) into\n");
00182                 BIO_printf(bio_err,"             the random number generator\n");
00183                 BIO_printf(bio_err," dsaparam-file\n");
00184                 BIO_printf(bio_err,"           - a DSA parameter file as generated by the dsaparam command\n");
00185                 goto end;
00186                 }
00187 
00188 #ifndef OPENSSL_NO_ENGINE
00189         e = setup_engine(bio_err, engine, 0);
00190 #endif
00191 
00192         if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
00193                 BIO_printf(bio_err, "Error getting password\n");
00194                 goto end;
00195         }
00196 
00197 
00198         in=BIO_new(BIO_s_file());
00199         if (!(BIO_read_filename(in,dsaparams)))
00200                 {
00201                 perror(dsaparams);
00202                 goto end;
00203                 }
00204 
00205         if ((dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
00206                 {
00207                 BIO_printf(bio_err,"unable to load DSA parameter file\n");
00208                 goto end;
00209                 }
00210         BIO_free(in);
00211         in = NULL;
00212                 
00213         out=BIO_new(BIO_s_file());
00214         if (out == NULL) goto end;
00215 
00216         if (outfile == NULL)
00217                 {
00218                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
00219 #ifdef OPENSSL_SYS_VMS
00220                 {
00221                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00222                 out = BIO_push(tmpbio, out);
00223                 }
00224 #endif
00225                 }
00226         else
00227                 {
00228                 if (BIO_write_filename(out,outfile) <= 0)
00229                         {
00230                         perror(outfile);
00231                         goto end;
00232                         }
00233                 }
00234 
00235         if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
00236                 {
00237                 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
00238                 }
00239         if (inrand != NULL)
00240                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
00241                         app_RAND_load_files(inrand));
00242 
00243         BIO_printf(bio_err,"Generating DSA key, %d bits\n",
00244                                                         BN_num_bits(dsa->p));
00245         if (!DSA_generate_key(dsa)) goto end;
00246 
00247         app_RAND_write_file(NULL, bio_err);
00248 
00249         if (!PEM_write_bio_DSAPrivateKey(out,dsa,enc,NULL,0,NULL, passout))
00250                 goto end;
00251         ret=0;
00252 end:
00253         if (ret != 0)
00254                 ERR_print_errors(bio_err);
00255         if (in != NULL) BIO_free(in);
00256         if (out != NULL) BIO_free_all(out);
00257         if (dsa != NULL) DSA_free(dsa);
00258         if(passout) OPENSSL_free(passout);
00259         apps_shutdown();
00260         OPENSSL_EXIT(ret);
00261         }
00262 #endif

© sourcejam.com 2005-2008