2 * Copyright 1995-2021 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 {
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 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_UNDEF, 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 if (!opt_check_rest_arg(NULL))
167 if (!opt_cipher(ciphername, &enc))
169 private = pubin || pubout ? 0 : 1;
173 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
174 BIO_printf(bio_err, "Error getting passwords\n");
178 BIO_printf(bio_err, "read DSA key\n");
180 pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
182 pkey = load_key(infile, informat, 1, passin, e, "private key");
185 BIO_printf(bio_err, "unable to load Key\n");
186 ERR_print_errors(bio_err);
189 if (!EVP_PKEY_is_a(pkey, "DSA")) {
190 BIO_printf(bio_err, "Not a DSA key\n");
194 out = bio_open_owner(outfile, outformat, private);
199 assert(pubin || private);
200 if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
201 || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
203 ERR_print_errors(bio_err);
209 BIGNUM *pub_key = NULL;
211 if (!EVP_PKEY_get_bn_param(pkey, "pub", &pub_key)) {
212 ERR_print_errors(bio_err);
215 BIO_printf(out, "Public Key=");
216 BN_print(out, pub_key);
217 BIO_printf(out, "\n");
225 BIO_printf(bio_err, "writing DSA key\n");
226 if (outformat == FORMAT_ASN1) {
228 } else if (outformat == FORMAT_PEM) {
230 } else if (outformat == FORMAT_MSBLOB) {
231 output_type = "MSBLOB";
232 } else if (outformat == FORMAT_PVK) {
234 BIO_printf(bio_err, "PVK form impossible with public key input\n");
239 BIO_printf(bio_err, "bad output format specified for outfile\n");
243 if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
245 output_structure = "SubjectPublicKeyInfo";
247 output_structure = "type-specific";
250 /* Select what you want in the output */
251 if (pubout || pubin) {
252 selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
255 selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
256 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
259 /* Perform the encoding */
260 ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, output_type,
261 output_structure, NULL);
262 if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
263 BIO_printf(bio_err, "%s format not supported\n", output_type);
267 /* Passphrase setup */
269 OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
271 /* Default passphrase prompter */
272 if (enc != NULL || outformat == FORMAT_PVK) {
273 OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
275 /* When passout given, override the passphrase prompter */
276 OSSL_ENCODER_CTX_set_passphrase(ectx,
277 (const unsigned char *)passout,
281 /* PVK requires a bit more */
282 if (outformat == FORMAT_PVK) {
283 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
285 params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
286 if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
287 BIO_printf(bio_err, "invalid PVK encryption level\n");
292 if (!OSSL_ENCODER_to_bio(ectx, out)) {
293 BIO_printf(bio_err, "unable to write key\n");
299 ERR_print_errors(bio_err);
300 OSSL_ENCODER_CTX_free(ectx);
303 EVP_CIPHER_free(enc);
305 OPENSSL_free(passin);
306 OPENSSL_free(passout);