2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/dsa.h>
21 #include <openssl/evp.h>
22 #include <openssl/x509.h>
23 #include <openssl/pem.h>
24 #include <openssl/bn.h>
25 #include <openssl/encoder.h>
26 #include <openssl/core_names.h>
27 #include <openssl/core_dispatch.h>
29 #ifndef OPENSSL_NO_RC4
30 # define DEFAULT_PVK_ENCR_STRENGTH 2
32 # define DEFAULT_PVK_ENCR_STRENGTH 0
35 typedef enum OPTION_choice
{
36 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
37 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_ENGINE
,
38 /* Do not change the order here; see case statements below */
39 OPT_PVK_NONE
, OPT_PVK_WEAK
, OPT_PVK_STRONG
,
40 OPT_NOOUT
, OPT_TEXT
, OPT_MODULUS
, OPT_PUBIN
,
41 OPT_PUBOUT
, OPT_CIPHER
, OPT_PASSIN
, OPT_PASSOUT
,
45 const OPTIONS dsa_options
[] = {
46 OPT_SECTION("General"),
47 {"help", OPT_HELP
, '-', "Display this summary"},
48 {"", OPT_CIPHER
, '-', "Any supported cipher"},
49 #ifndef OPENSSL_NO_RC4
50 {"pvk-strong", OPT_PVK_STRONG
, '-', "Enable 'Strong' PVK encoding level (default)"},
51 {"pvk-weak", OPT_PVK_WEAK
, '-', "Enable 'Weak' PVK encoding level"},
52 {"pvk-none", OPT_PVK_NONE
, '-', "Don't enforce PVK encoding"},
54 #ifndef OPENSSL_NO_ENGINE
55 {"engine", OPT_ENGINE
, 's', "Use engine e, possibly a hardware device"},
59 {"in", OPT_IN
, 's', "Input key"},
60 {"inform", OPT_INFORM
, 'f', "Input format (DER/PEM/PVK); has no effect"},
61 {"pubin", OPT_PUBIN
, '-', "Expect a public key in input file"},
62 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
64 OPT_SECTION("Output"),
65 {"out", OPT_OUT
, '>', "Output file"},
66 {"outform", OPT_OUTFORM
, 'f', "Output format, DER PEM PVK"},
67 {"noout", OPT_NOOUT
, '-', "Don't print key out"},
68 {"text", OPT_TEXT
, '-', "Print the key in text"},
69 {"modulus", OPT_MODULUS
, '-', "Print the DSA public value"},
70 {"pubout", OPT_PUBOUT
, '-', "Output public key, not private"},
71 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
77 int dsa_main(int argc
, char **argv
)
81 EVP_PKEY
*pkey
= NULL
;
82 const EVP_CIPHER
*enc
= NULL
;
83 char *infile
= NULL
, *outfile
= NULL
, *prog
;
84 char *passin
= NULL
, *passout
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
86 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, text
= 0, noout
= 0;
87 int modulus
= 0, pubin
= 0, pubout
= 0, ret
= 1;
88 int pvk_encr
= DEFAULT_PVK_ENCR_STRENGTH
;
90 const char *output_type
= NULL
, *ciphername
= NULL
;
91 const char *output_structure
= NULL
;
93 OSSL_ENCODER_CTX
*ectx
= NULL
;
95 prog
= opt_init(argc
, argv
, dsa_options
);
96 while ((o
= opt_next()) != OPT_EOF
) {
102 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
105 opt_help(dsa_options
);
109 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &informat
))
116 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &outformat
))
123 e
= setup_engine(opt_arg(), 0);
126 passinarg
= opt_arg();
129 passoutarg
= opt_arg();
131 case OPT_PVK_STRONG
: /* pvk_encr:= 2 */
132 case OPT_PVK_WEAK
: /* pvk_encr:= 1 */
133 case OPT_PVK_NONE
: /* pvk_encr:= 0 */
134 #ifndef OPENSSL_NO_RC4
135 pvk_encr
= (o
- OPT_PVK_NONE
);
154 ciphername
= opt_unknown();
157 if (!opt_provider(o
))
164 argc
= opt_num_rest();
168 if (ciphername
!= NULL
) {
169 if (!opt_cipher(ciphername
, &enc
))
172 private = pubin
|| pubout
? 0 : 1;
176 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
177 BIO_printf(bio_err
, "Error getting passwords\n");
181 BIO_printf(bio_err
, "read DSA key\n");
183 pkey
= load_pubkey(infile
, informat
, 1, passin
, e
, "public key");
185 pkey
= load_key(infile
, informat
, 1, passin
, e
, "private key");
188 BIO_printf(bio_err
, "unable to load Key\n");
189 ERR_print_errors(bio_err
);
192 if (!EVP_PKEY_is_a(pkey
, "DSA")) {
193 BIO_printf(bio_err
, "Not a DSA key\n");
197 out
= bio_open_owner(outfile
, outformat
, private);
202 assert(pubin
|| private);
203 if ((pubin
&& EVP_PKEY_print_public(out
, pkey
, 0, NULL
) <= 0)
204 || (!pubin
&& EVP_PKEY_print_private(out
, pkey
, 0, NULL
) <= 0)) {
206 ERR_print_errors(bio_err
);
212 BIGNUM
*pub_key
= NULL
;
214 if (!EVP_PKEY_get_bn_param(pkey
, "pub", &pub_key
)) {
215 ERR_print_errors(bio_err
);
218 BIO_printf(out
, "Public Key=");
219 BN_print(out
, pub_key
);
220 BIO_printf(out
, "\n");
228 BIO_printf(bio_err
, "writing DSA key\n");
229 if (outformat
== FORMAT_ASN1
) {
231 } else if (outformat
== FORMAT_PEM
) {
233 } else if (outformat
== FORMAT_MSBLOB
) {
234 output_type
= "MSBLOB";
235 } else if (outformat
== FORMAT_PVK
) {
237 BIO_printf(bio_err
, "PVK form impossible with public key input\n");
242 BIO_printf(bio_err
, "bad output format specified for outfile\n");
246 if (outformat
== FORMAT_ASN1
|| outformat
== FORMAT_PEM
) {
248 output_structure
= "SubjectPublicKeyInfo";
250 output_structure
= "type-specific";
253 /* Select what you want in the output */
254 if (pubout
|| pubin
) {
255 selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
258 selection
= (OSSL_KEYMGMT_SELECT_KEYPAIR
259 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
);
262 /* Perform the encoding */
263 ectx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, output_type
,
264 output_structure
, NULL
);
265 if (OSSL_ENCODER_CTX_get_num_encoders(ectx
) == 0) {
266 BIO_printf(bio_err
, "%s format not supported\n", output_type
);
270 /* PVK requires a bit more */
271 if (outformat
== FORMAT_PVK
) {
272 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
274 params
[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr
);
275 if (!OSSL_ENCODER_CTX_set_params(ectx
, params
)) {
276 BIO_printf(bio_err
, "invalid PVK encryption level\n");
281 if (!OSSL_ENCODER_to_bio(ectx
, out
)) {
282 BIO_printf(bio_err
, "unable to write key\n");
288 ERR_print_errors(bio_err
);
289 OSSL_ENCODER_CTX_free(ectx
);
293 OPENSSL_free(passin
);
294 OPENSSL_free(passout
);