| OLD | NEW |
| 1 /* apps/dsa.c */ | 1 /* apps/dsa.c */ |
| 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
| 6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
| 7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
| 8 * | 8 * |
| 9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
| 10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ | 59 #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ |
| 60 #ifndef OPENSSL_NO_DSA | 60 #ifndef OPENSSL_NO_DSA |
| 61 #include <stdio.h> | 61 #include <stdio.h> |
| 62 #include <stdlib.h> | 62 #include <stdlib.h> |
| 63 #include <string.h> | 63 #include <string.h> |
| 64 #include <time.h> | 64 #include <time.h> |
| 65 #include "apps.h" | 65 #include "apps.h" |
| 66 #include <openssl/bio.h> | 66 #include <openssl/bio.h> |
| 67 #include <openssl/err.h> | 67 #include <openssl/err.h> |
| 68 #include <openssl/dsa.h> |
| 68 #include <openssl/evp.h> | 69 #include <openssl/evp.h> |
| 69 #include <openssl/x509.h> | 70 #include <openssl/x509.h> |
| 70 #include <openssl/pem.h> | 71 #include <openssl/pem.h> |
| 71 #include <openssl/bn.h> | 72 #include <openssl/bn.h> |
| 72 #include <openssl/dsa.h> | |
| 73 | 73 |
| 74 #undef PROG | 74 #undef PROG |
| 75 #define PROG dsa_main | 75 #define PROG dsa_main |
| 76 | 76 |
| 77 /* -inform arg - input format - default PEM (one of DER, NET or PEM) | 77 /* -inform arg - input format - default PEM (one of DER, NET or PEM) |
| 78 * -outform arg - output format - default PEM | 78 * -outform arg - output format - default PEM |
| 79 * -in arg - input file - default stdin | 79 * -in arg - input file - default stdin |
| 80 * -out arg - output file - default stdout | 80 * -out arg - output file - default stdout |
| 81 * -des - encrypt output if PEM format with DES in cbc mode | 81 * -des - encrypt output if PEM format with DES in cbc mode |
| 82 * -des3 - encrypt output if PEM format | 82 * -des3 - encrypt output if PEM format |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 int informat,outformat,text=0,noout=0; | 105 int informat,outformat,text=0,noout=0; |
| 106 int pubin = 0, pubout = 0; | 106 int pubin = 0, pubout = 0; |
| 107 char *infile,*outfile,*prog; | 107 char *infile,*outfile,*prog; |
| 108 #ifndef OPENSSL_NO_ENGINE | 108 #ifndef OPENSSL_NO_ENGINE |
| 109 char *engine; | 109 char *engine; |
| 110 #endif | 110 #endif |
| 111 char *passargin = NULL, *passargout = NULL; | 111 char *passargin = NULL, *passargout = NULL; |
| 112 char *passin = NULL, *passout = NULL; | 112 char *passin = NULL, *passout = NULL; |
| 113 int modulus=0; | 113 int modulus=0; |
| 114 | 114 |
| 115 int pvk_encr = 2; |
| 116 |
| 115 apps_startup(); | 117 apps_startup(); |
| 116 | 118 |
| 117 if (bio_err == NULL) | 119 if (bio_err == NULL) |
| 118 if ((bio_err=BIO_new(BIO_s_file())) != NULL) | 120 if ((bio_err=BIO_new(BIO_s_file())) != NULL) |
| 119 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); | 121 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); |
| 120 | 122 |
| 121 if (!load_config(bio_err, NULL)) | 123 if (!load_config(bio_err, NULL)) |
| 122 goto end; | 124 goto end; |
| 123 | 125 |
| 124 #ifndef OPENSSL_NO_ENGINE | 126 #ifndef OPENSSL_NO_ENGINE |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 if (--argc < 1) goto bad; | 166 if (--argc < 1) goto bad; |
| 165 passargout= *(++argv); | 167 passargout= *(++argv); |
| 166 } | 168 } |
| 167 #ifndef OPENSSL_NO_ENGINE | 169 #ifndef OPENSSL_NO_ENGINE |
| 168 else if (strcmp(*argv,"-engine") == 0) | 170 else if (strcmp(*argv,"-engine") == 0) |
| 169 { | 171 { |
| 170 if (--argc < 1) goto bad; | 172 if (--argc < 1) goto bad; |
| 171 engine= *(++argv); | 173 engine= *(++argv); |
| 172 } | 174 } |
| 173 #endif | 175 #endif |
| 176 else if (strcmp(*argv,"-pvk-strong") == 0) |
| 177 pvk_encr=2; |
| 178 else if (strcmp(*argv,"-pvk-weak") == 0) |
| 179 pvk_encr=1; |
| 180 else if (strcmp(*argv,"-pvk-none") == 0) |
| 181 pvk_encr=0; |
| 174 else if (strcmp(*argv,"-noout") == 0) | 182 else if (strcmp(*argv,"-noout") == 0) |
| 175 noout=1; | 183 noout=1; |
| 176 else if (strcmp(*argv,"-text") == 0) | 184 else if (strcmp(*argv,"-text") == 0) |
| 177 text=1; | 185 text=1; |
| 178 else if (strcmp(*argv,"-modulus") == 0) | 186 else if (strcmp(*argv,"-modulus") == 0) |
| 179 modulus=1; | 187 modulus=1; |
| 180 else if (strcmp(*argv,"-pubin") == 0) | 188 else if (strcmp(*argv,"-pubin") == 0) |
| 181 pubin=1; | 189 pubin=1; |
| 182 else if (strcmp(*argv,"-pubout") == 0) | 190 else if (strcmp(*argv,"-pubout") == 0) |
| 183 pubout=1; | 191 pubout=1; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 239 |
| 232 #ifndef OPENSSL_NO_ENGINE | 240 #ifndef OPENSSL_NO_ENGINE |
| 233 e = setup_engine(bio_err, engine, 0); | 241 e = setup_engine(bio_err, engine, 0); |
| 234 #endif | 242 #endif |
| 235 | 243 |
| 236 if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { | 244 if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { |
| 237 BIO_printf(bio_err, "Error getting passwords\n"); | 245 BIO_printf(bio_err, "Error getting passwords\n"); |
| 238 goto end; | 246 goto end; |
| 239 } | 247 } |
| 240 | 248 |
| 249 in=BIO_new(BIO_s_file()); |
| 241 out=BIO_new(BIO_s_file()); | 250 out=BIO_new(BIO_s_file()); |
| 242 » if (out == NULL) | 251 » if ((in == NULL) || (out == NULL)) |
| 243 { | 252 { |
| 244 ERR_print_errors(bio_err); | 253 ERR_print_errors(bio_err); |
| 245 goto end; | 254 goto end; |
| 246 } | 255 } |
| 247 | 256 |
| 257 if (infile == NULL) |
| 258 BIO_set_fp(in,stdin,BIO_NOCLOSE); |
| 259 else |
| 260 { |
| 261 if (BIO_read_filename(in,infile) <= 0) |
| 262 { |
| 263 perror(infile); |
| 264 goto end; |
| 265 } |
| 266 } |
| 267 |
| 248 BIO_printf(bio_err,"read DSA key\n"); | 268 BIO_printf(bio_err,"read DSA key\n"); |
| 249 » { | 269 |
| 270 » » { |
| 250 EVP_PKEY *pkey; | 271 EVP_PKEY *pkey; |
| 272 |
| 251 if (pubin) | 273 if (pubin) |
| 252 pkey = load_pubkey(bio_err, infile, informat, 1, | 274 pkey = load_pubkey(bio_err, infile, informat, 1, |
| 253 passin, e, "Public Key"); | 275 passin, e, "Public Key"); |
| 254 else | 276 else |
| 255 pkey = load_key(bio_err, infile, informat, 1, | 277 pkey = load_key(bio_err, infile, informat, 1, |
| 256 passin, e, "Private Key"); | 278 passin, e, "Private Key"); |
| 257 | 279 |
| 258 » » if (pkey != NULL) | 280 » » if (pkey) |
| 259 » » dsa = pkey == NULL ? NULL : EVP_PKEY_get1_DSA(pkey); | 281 » » » { |
| 260 » » EVP_PKEY_free(pkey); | 282 » » » dsa = EVP_PKEY_get1_DSA(pkey); |
| 261 » } | 283 » » » EVP_PKEY_free(pkey); |
| 284 » » » } |
| 285 » » } |
| 262 if (dsa == NULL) | 286 if (dsa == NULL) |
| 263 { | 287 { |
| 264 BIO_printf(bio_err,"unable to load Key\n"); | 288 BIO_printf(bio_err,"unable to load Key\n"); |
| 265 ERR_print_errors(bio_err); | 289 ERR_print_errors(bio_err); |
| 266 goto end; | 290 goto end; |
| 267 } | 291 } |
| 268 | 292 |
| 269 if (outfile == NULL) | 293 if (outfile == NULL) |
| 270 { | 294 { |
| 271 BIO_set_fp(out,stdout,BIO_NOCLOSE); | 295 BIO_set_fp(out,stdout,BIO_NOCLOSE); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 if (noout) goto end; | 327 if (noout) goto end; |
| 304 BIO_printf(bio_err,"writing DSA key\n"); | 328 BIO_printf(bio_err,"writing DSA key\n"); |
| 305 if (outformat == FORMAT_ASN1) { | 329 if (outformat == FORMAT_ASN1) { |
| 306 if(pubin || pubout) i=i2d_DSA_PUBKEY_bio(out,dsa); | 330 if(pubin || pubout) i=i2d_DSA_PUBKEY_bio(out,dsa); |
| 307 else i=i2d_DSAPrivateKey_bio(out,dsa); | 331 else i=i2d_DSAPrivateKey_bio(out,dsa); |
| 308 } else if (outformat == FORMAT_PEM) { | 332 } else if (outformat == FORMAT_PEM) { |
| 309 if(pubin || pubout) | 333 if(pubin || pubout) |
| 310 i=PEM_write_bio_DSA_PUBKEY(out,dsa); | 334 i=PEM_write_bio_DSA_PUBKEY(out,dsa); |
| 311 else i=PEM_write_bio_DSAPrivateKey(out,dsa,enc, | 335 else i=PEM_write_bio_DSAPrivateKey(out,dsa,enc, |
| 312 NULL,0,NULL, passout); | 336 NULL,0,NULL, passout); |
| 337 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4) |
| 338 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) { |
| 339 EVP_PKEY *pk; |
| 340 pk = EVP_PKEY_new(); |
| 341 EVP_PKEY_set1_DSA(pk, dsa); |
| 342 if (outformat == FORMAT_PVK) |
| 343 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout); |
| 344 else if (pubin || pubout) |
| 345 i = i2b_PublicKey_bio(out, pk); |
| 346 else |
| 347 i = i2b_PrivateKey_bio(out, pk); |
| 348 EVP_PKEY_free(pk); |
| 349 #endif |
| 313 } else { | 350 } else { |
| 314 BIO_printf(bio_err,"bad output format specified for outfile\n"); | 351 BIO_printf(bio_err,"bad output format specified for outfile\n"); |
| 315 goto end; | 352 goto end; |
| 316 } | 353 } |
| 317 » if (!i) | 354 » if (i <= 0) |
| 318 { | 355 { |
| 319 BIO_printf(bio_err,"unable to write private key\n"); | 356 BIO_printf(bio_err,"unable to write private key\n"); |
| 320 ERR_print_errors(bio_err); | 357 ERR_print_errors(bio_err); |
| 321 } | 358 } |
| 322 else | 359 else |
| 323 ret=0; | 360 ret=0; |
| 324 end: | 361 end: |
| 325 if(in != NULL) BIO_free(in); | 362 if(in != NULL) BIO_free(in); |
| 326 if(out != NULL) BIO_free_all(out); | 363 if(out != NULL) BIO_free_all(out); |
| 327 if(dsa != NULL) DSA_free(dsa); | 364 if(dsa != NULL) DSA_free(dsa); |
| 328 if(passin) OPENSSL_free(passin); | 365 if(passin) OPENSSL_free(passin); |
| 329 if(passout) OPENSSL_free(passout); | 366 if(passout) OPENSSL_free(passout); |
| 330 apps_shutdown(); | 367 apps_shutdown(); |
| 331 OPENSSL_EXIT(ret); | 368 OPENSSL_EXIT(ret); |
| 332 } | 369 } |
| 370 #else /* !OPENSSL_NO_DSA */ |
| 371 |
| 372 # if PEDANTIC |
| 373 static void *dummy=&dummy; |
| 374 # endif |
| 375 |
| 333 #endif | 376 #endif |
| OLD | NEW |