2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/dsa.h>
23 # include <openssl/evp.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
26 # include <openssl/bn.h>
28 typedef enum OPTION_choice
{
29 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
30 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_ENGINE
,
31 /* Do not change the order here; see case statements below */
32 OPT_PVK_NONE
, OPT_PVK_WEAK
, OPT_PVK_STRONG
,
33 OPT_NOOUT
, OPT_TEXT
, OPT_MODULUS
, OPT_PUBIN
,
34 OPT_PUBOUT
, OPT_CIPHER
, OPT_PASSIN
, OPT_PASSOUT
37 const OPTIONS dsa_options
[] = {
38 {"help", OPT_HELP
, '-', "Display this summary"},
39 {"inform", OPT_INFORM
, 'f', "Input format, DER PEM PVK"},
40 {"outform", OPT_OUTFORM
, 'F', "Output format, DER PEM PVK"},
41 {"in", OPT_IN
, 's', "Input key"},
42 {"out", OPT_OUT
, '>', "Output file"},
43 {"noout", OPT_NOOUT
, '-', "Don't print key out"},
44 {"text", OPT_TEXT
, '-', "Print the key in text"},
45 {"modulus", OPT_MODULUS
, '-', "Print the DSA public value"},
46 {"pubin", OPT_PUBIN
, '-', "Expect a public key in input file"},
47 {"pubout", OPT_PUBOUT
, '-', "Output public key, not private"},
48 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
49 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
50 {"", OPT_CIPHER
, '-', "Any supported cipher"},
51 # ifndef OPENSSL_NO_RC4
52 {"pvk-strong", OPT_PVK_STRONG
, '-', "Enable 'Strong' PVK encoding level (default)"},
53 {"pvk-weak", OPT_PVK_WEAK
, '-', "Enable 'Weak' PVK encoding level"},
54 {"pvk-none", OPT_PVK_NONE
, '-', "Don't enforce PVK encoding"},
56 # ifndef OPENSSL_NO_ENGINE
57 {"engine", OPT_ENGINE
, 's', "Use engine e, possibly a hardware device"},
62 int dsa_main(int argc
, char **argv
)
67 const EVP_CIPHER
*enc
= NULL
;
68 char *infile
= NULL
, *outfile
= NULL
, *prog
;
69 char *passin
= NULL
, *passout
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
71 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, text
= 0, noout
= 0;
72 int i
, modulus
= 0, pubin
= 0, pubout
= 0, ret
= 1;
73 # ifndef OPENSSL_NO_RC4
78 prog
= opt_init(argc
, argv
, dsa_options
);
79 while ((o
= opt_next()) != OPT_EOF
) {
85 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
88 opt_help(dsa_options
);
92 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &informat
))
100 (opt_arg(), OPT_FMT_PEMDER
| OPT_FMT_PVK
, &outformat
))
107 e
= setup_engine(opt_arg(), 0);
110 passinarg
= opt_arg();
113 passoutarg
= opt_arg();
115 case OPT_PVK_STRONG
: /* pvk_encr:= 2 */
116 case OPT_PVK_WEAK
: /* pvk_encr:= 1 */
117 case OPT_PVK_NONE
: /* pvk_encr:= 0 */
118 #ifndef OPENSSL_NO_RC4
119 pvk_encr
= (o
- OPT_PVK_NONE
);
138 if (!opt_cipher(opt_unknown(), &enc
))
143 argc
= opt_num_rest();
147 private = pubin
|| pubout
? 0 : 1;
151 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
152 BIO_printf(bio_err
, "Error getting passwords\n");
156 BIO_printf(bio_err
, "read DSA key\n");
161 pkey
= load_pubkey(infile
, informat
, 1, passin
, e
, "Public Key");
163 pkey
= load_key(infile
, informat
, 1, passin
, e
, "Private Key");
166 dsa
= EVP_PKEY_get1_DSA(pkey
);
171 BIO_printf(bio_err
, "unable to load Key\n");
172 ERR_print_errors(bio_err
);
176 out
= bio_open_owner(outfile
, outformat
, private);
181 assert(pubin
|| private);
182 if (!DSA_print(out
, dsa
, 0)) {
184 ERR_print_errors(bio_err
);
190 const BIGNUM
*pub_key
= NULL
;
191 DSA_get0_key(dsa
, &pub_key
, NULL
);
192 BIO_printf(out
, "Public Key=");
193 BN_print(out
, pub_key
);
194 BIO_printf(out
, "\n");
201 BIO_printf(bio_err
, "writing DSA key\n");
202 if (outformat
== FORMAT_ASN1
) {
204 i
= i2d_DSA_PUBKEY_bio(out
, dsa
);
207 i
= i2d_DSAPrivateKey_bio(out
, dsa
);
209 } else if (outformat
== FORMAT_PEM
) {
211 i
= PEM_write_bio_DSA_PUBKEY(out
, dsa
);
214 i
= PEM_write_bio_DSAPrivateKey(out
, dsa
, enc
,
215 NULL
, 0, NULL
, passout
);
217 # if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
218 } else if (outformat
== FORMAT_MSBLOB
|| outformat
== FORMAT_PVK
) {
221 EVP_PKEY_set1_DSA(pk
, dsa
);
222 if (outformat
== FORMAT_PVK
) {
224 BIO_printf(bio_err
, "PVK form impossible with public key input\n");
229 i
= i2b_PVK_bio(out
, pk
, pvk_encr
, 0, passout
);
231 else if (pubin
|| pubout
)
232 i
= i2b_PublicKey_bio(out
, pk
);
235 i
= i2b_PrivateKey_bio(out
, pk
);
240 BIO_printf(bio_err
, "bad output format specified for outfile\n");
244 BIO_printf(bio_err
, "unable to write private key\n");
245 ERR_print_errors(bio_err
);
252 OPENSSL_free(passin
);
253 OPENSSL_free(passout
);