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 <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
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
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