00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include <openssl/opensslconf.h>
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