1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
10e71b7053SJung-uk Kim #include <openssl/opensslconf.h>
11*b077aed3SPierre Pronchery
1274664626SKris Kennaway #include <stdio.h>
1374664626SKris Kennaway #include <stdlib.h>
1474664626SKris Kennaway #include <string.h>
1574664626SKris Kennaway #include <time.h>
1674664626SKris Kennaway #include "apps.h"
17e71b7053SJung-uk Kim #include "progs.h"
1874664626SKris Kennaway #include <openssl/bio.h>
1974664626SKris Kennaway #include <openssl/err.h>
201f13597dSJung-uk Kim #include <openssl/dsa.h>
2174664626SKris Kennaway #include <openssl/evp.h>
2274664626SKris Kennaway #include <openssl/x509.h>
2374664626SKris Kennaway #include <openssl/pem.h>
243b4e3dcbSSimon L. B. Nielsen #include <openssl/bn.h>
25*b077aed3SPierre Pronchery #include <openssl/encoder.h>
26*b077aed3SPierre Pronchery #include <openssl/core_names.h>
27*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
28*b077aed3SPierre Pronchery
29*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_RC4
30*b077aed3SPierre Pronchery # define DEFAULT_PVK_ENCR_STRENGTH 2
31*b077aed3SPierre Pronchery #else
32*b077aed3SPierre Pronchery # define DEFAULT_PVK_ENCR_STRENGTH 0
33*b077aed3SPierre Pronchery #endif
3474664626SKris Kennaway
35e71b7053SJung-uk Kim typedef enum OPTION_choice {
36*b077aed3SPierre Pronchery OPT_COMMON,
37e71b7053SJung-uk Kim OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENGINE,
38e71b7053SJung-uk Kim /* Do not change the order here; see case statements below */
39e71b7053SJung-uk Kim OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
40e71b7053SJung-uk Kim OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_PUBIN,
41*b077aed3SPierre Pronchery OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT,
42*b077aed3SPierre Pronchery OPT_PROV_ENUM
43e71b7053SJung-uk Kim } OPTION_CHOICE;
4474664626SKris Kennaway
45e71b7053SJung-uk Kim const OPTIONS dsa_options[] = {
46*b077aed3SPierre Pronchery OPT_SECTION("General"),
47e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
48e71b7053SJung-uk Kim {"", OPT_CIPHER, '-', "Any supported cipher"},
49e71b7053SJung-uk Kim #ifndef OPENSSL_NO_RC4
50e71b7053SJung-uk Kim {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
51e71b7053SJung-uk Kim {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
52e71b7053SJung-uk Kim {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
53fceca8a3SJacques Vidrine #endif
54e71b7053SJung-uk Kim #ifndef OPENSSL_NO_ENGINE
55e71b7053SJung-uk Kim {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
56e71b7053SJung-uk Kim #endif
57*b077aed3SPierre Pronchery
58*b077aed3SPierre Pronchery OPT_SECTION("Input"),
59*b077aed3SPierre Pronchery {"in", OPT_IN, 's', "Input key"},
60*b077aed3SPierre Pronchery {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/PVK); has no effect"},
61*b077aed3SPierre Pronchery {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
62*b077aed3SPierre Pronchery {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
63*b077aed3SPierre Pronchery
64*b077aed3SPierre Pronchery OPT_SECTION("Output"),
65*b077aed3SPierre Pronchery {"out", OPT_OUT, '>', "Output file"},
66*b077aed3SPierre Pronchery {"outform", OPT_OUTFORM, 'f', "Output format, DER PEM PVK"},
67*b077aed3SPierre Pronchery {"noout", OPT_NOOUT, '-', "Don't print key out"},
68*b077aed3SPierre Pronchery {"text", OPT_TEXT, '-', "Print the key in text"},
69*b077aed3SPierre Pronchery {"modulus", OPT_MODULUS, '-', "Print the DSA public value"},
70*b077aed3SPierre Pronchery {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
71*b077aed3SPierre Pronchery {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
72*b077aed3SPierre Pronchery
73*b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
74e71b7053SJung-uk Kim {NULL}
75e71b7053SJung-uk Kim };
76e71b7053SJung-uk Kim
dsa_main(int argc,char ** argv)77e71b7053SJung-uk Kim int dsa_main(int argc, char **argv)
78e71b7053SJung-uk Kim {
79e71b7053SJung-uk Kim BIO *out = NULL;
80e71b7053SJung-uk Kim ENGINE *e = NULL;
81*b077aed3SPierre Pronchery EVP_PKEY *pkey = NULL;
82*b077aed3SPierre Pronchery EVP_CIPHER *enc = NULL;
83e71b7053SJung-uk Kim char *infile = NULL, *outfile = NULL, *prog;
84e71b7053SJung-uk Kim char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
85e71b7053SJung-uk Kim OPTION_CHOICE o;
86*b077aed3SPierre Pronchery int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, text = 0, noout = 0;
87*b077aed3SPierre Pronchery int modulus = 0, pubin = 0, pubout = 0, ret = 1;
88*b077aed3SPierre Pronchery int pvk_encr = DEFAULT_PVK_ENCR_STRENGTH;
89e71b7053SJung-uk Kim int private = 0;
90*b077aed3SPierre Pronchery const char *output_type = NULL, *ciphername = NULL;
91*b077aed3SPierre Pronchery const char *output_structure = NULL;
92*b077aed3SPierre Pronchery int selection = 0;
93*b077aed3SPierre Pronchery OSSL_ENCODER_CTX *ectx = NULL;
94e71b7053SJung-uk Kim
95e71b7053SJung-uk Kim prog = opt_init(argc, argv, dsa_options);
96e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
97e71b7053SJung-uk Kim switch (o) {
98e71b7053SJung-uk Kim case OPT_EOF:
99e71b7053SJung-uk Kim case OPT_ERR:
100e71b7053SJung-uk Kim opthelp:
101e71b7053SJung-uk Kim ret = 0;
102e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
103e71b7053SJung-uk Kim goto end;
104e71b7053SJung-uk Kim case OPT_HELP:
105e71b7053SJung-uk Kim opt_help(dsa_options);
106e71b7053SJung-uk Kim ret = 0;
107e71b7053SJung-uk Kim goto end;
108e71b7053SJung-uk Kim case OPT_INFORM:
109e71b7053SJung-uk Kim if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
110e71b7053SJung-uk Kim goto opthelp;
111e71b7053SJung-uk Kim break;
112e71b7053SJung-uk Kim case OPT_IN:
113e71b7053SJung-uk Kim infile = opt_arg();
114e71b7053SJung-uk Kim break;
115e71b7053SJung-uk Kim case OPT_OUTFORM:
116e71b7053SJung-uk Kim if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
117e71b7053SJung-uk Kim goto opthelp;
118e71b7053SJung-uk Kim break;
119e71b7053SJung-uk Kim case OPT_OUT:
120e71b7053SJung-uk Kim outfile = opt_arg();
121e71b7053SJung-uk Kim break;
122e71b7053SJung-uk Kim case OPT_ENGINE:
123e71b7053SJung-uk Kim e = setup_engine(opt_arg(), 0);
124e71b7053SJung-uk Kim break;
125e71b7053SJung-uk Kim case OPT_PASSIN:
126e71b7053SJung-uk Kim passinarg = opt_arg();
127e71b7053SJung-uk Kim break;
128e71b7053SJung-uk Kim case OPT_PASSOUT:
129e71b7053SJung-uk Kim passoutarg = opt_arg();
130e71b7053SJung-uk Kim break;
131e71b7053SJung-uk Kim case OPT_PVK_STRONG: /* pvk_encr:= 2 */
132e71b7053SJung-uk Kim case OPT_PVK_WEAK: /* pvk_encr:= 1 */
133e71b7053SJung-uk Kim case OPT_PVK_NONE: /* pvk_encr:= 0 */
134e71b7053SJung-uk Kim #ifndef OPENSSL_NO_RC4
135e71b7053SJung-uk Kim pvk_encr = (o - OPT_PVK_NONE);
136e71b7053SJung-uk Kim #endif
137e71b7053SJung-uk Kim break;
138e71b7053SJung-uk Kim case OPT_NOOUT:
13974664626SKris Kennaway noout = 1;
140e71b7053SJung-uk Kim break;
141e71b7053SJung-uk Kim case OPT_TEXT:
14274664626SKris Kennaway text = 1;
143e71b7053SJung-uk Kim break;
144e71b7053SJung-uk Kim case OPT_MODULUS:
14574664626SKris Kennaway modulus = 1;
146e71b7053SJung-uk Kim break;
147e71b7053SJung-uk Kim case OPT_PUBIN:
148f579bf8eSKris Kennaway pubin = 1;
149e71b7053SJung-uk Kim break;
150e71b7053SJung-uk Kim case OPT_PUBOUT:
151f579bf8eSKris Kennaway pubout = 1;
152e71b7053SJung-uk Kim break;
153e71b7053SJung-uk Kim case OPT_CIPHER:
154*b077aed3SPierre Pronchery ciphername = opt_unknown();
155*b077aed3SPierre Pronchery break;
156*b077aed3SPierre Pronchery case OPT_PROV_CASES:
157*b077aed3SPierre Pronchery if (!opt_provider(o))
158e71b7053SJung-uk Kim goto end;
15974664626SKris Kennaway break;
16074664626SKris Kennaway }
16174664626SKris Kennaway }
162*b077aed3SPierre Pronchery
163*b077aed3SPierre Pronchery /* No extra args. */
164e71b7053SJung-uk Kim argc = opt_num_rest();
165e71b7053SJung-uk Kim if (argc != 0)
166e71b7053SJung-uk Kim goto opthelp;
16774664626SKris Kennaway
168*b077aed3SPierre Pronchery if (ciphername != NULL) {
169*b077aed3SPierre Pronchery if (!opt_cipher(ciphername, &enc))
170*b077aed3SPierre Pronchery goto end;
171*b077aed3SPierre Pronchery }
172e71b7053SJung-uk Kim private = pubin || pubout ? 0 : 1;
173e71b7053SJung-uk Kim if (text && !pubin)
174e71b7053SJung-uk Kim private = 1;
17574664626SKris Kennaway
176e71b7053SJung-uk Kim if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
177f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting passwords\n");
178f579bf8eSKris Kennaway goto end;
179f579bf8eSKris Kennaway }
180f579bf8eSKris Kennaway
181f579bf8eSKris Kennaway BIO_printf(bio_err, "read DSA key\n");
182db522d3aSSimon L. B. Nielsen if (pubin)
183*b077aed3SPierre Pronchery pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
184db522d3aSSimon L. B. Nielsen else
185*b077aed3SPierre Pronchery pkey = load_key(infile, informat, 1, passin, e, "private key");
186db522d3aSSimon L. B. Nielsen
187*b077aed3SPierre Pronchery if (pkey == NULL) {
188f579bf8eSKris Kennaway BIO_printf(bio_err, "unable to load Key\n");
18974664626SKris Kennaway ERR_print_errors(bio_err);
19074664626SKris Kennaway goto end;
19174664626SKris Kennaway }
192*b077aed3SPierre Pronchery if (!EVP_PKEY_is_a(pkey, "DSA")) {
193*b077aed3SPierre Pronchery BIO_printf(bio_err, "Not a DSA key\n");
194*b077aed3SPierre Pronchery goto end;
195*b077aed3SPierre Pronchery }
19674664626SKris Kennaway
197e71b7053SJung-uk Kim out = bio_open_owner(outfile, outformat, private);
198e71b7053SJung-uk Kim if (out == NULL)
19974664626SKris Kennaway goto end;
20074664626SKris Kennaway
201e71b7053SJung-uk Kim if (text) {
202e71b7053SJung-uk Kim assert(pubin || private);
203*b077aed3SPierre Pronchery if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
204*b077aed3SPierre Pronchery || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
20574664626SKris Kennaway perror(outfile);
20674664626SKris Kennaway ERR_print_errors(bio_err);
20774664626SKris Kennaway goto end;
20874664626SKris Kennaway }
20974664626SKris Kennaway }
21074664626SKris Kennaway
211e71b7053SJung-uk Kim if (modulus) {
212*b077aed3SPierre Pronchery BIGNUM *pub_key = NULL;
213*b077aed3SPierre Pronchery
214*b077aed3SPierre Pronchery if (!EVP_PKEY_get_bn_param(pkey, "pub", &pub_key)) {
215*b077aed3SPierre Pronchery ERR_print_errors(bio_err);
216*b077aed3SPierre Pronchery goto end;
217*b077aed3SPierre Pronchery }
218e71b7053SJung-uk Kim BIO_printf(out, "Public Key=");
219e71b7053SJung-uk Kim BN_print(out, pub_key);
220e71b7053SJung-uk Kim BIO_printf(out, "\n");
221*b077aed3SPierre Pronchery BN_free(pub_key);
222e71b7053SJung-uk Kim }
223e71b7053SJung-uk Kim
224e71b7053SJung-uk Kim if (noout) {
225e71b7053SJung-uk Kim ret = 0;
2266f9291ceSJung-uk Kim goto end;
227e71b7053SJung-uk Kim }
228f579bf8eSKris Kennaway BIO_printf(bio_err, "writing DSA key\n");
229f579bf8eSKris Kennaway if (outformat == FORMAT_ASN1) {
230*b077aed3SPierre Pronchery output_type = "DER";
231f579bf8eSKris Kennaway } else if (outformat == FORMAT_PEM) {
232*b077aed3SPierre Pronchery output_type = "PEM";
233*b077aed3SPierre Pronchery } else if (outformat == FORMAT_MSBLOB) {
234*b077aed3SPierre Pronchery output_type = "MSBLOB";
235*b077aed3SPierre Pronchery } else if (outformat == FORMAT_PVK) {
236e71b7053SJung-uk Kim if (pubin) {
237e71b7053SJung-uk Kim BIO_printf(bio_err, "PVK form impossible with public key input\n");
238e71b7053SJung-uk Kim goto end;
239e71b7053SJung-uk Kim }
240*b077aed3SPierre Pronchery output_type = "PVK";
241f579bf8eSKris Kennaway } else {
24274664626SKris Kennaway BIO_printf(bio_err, "bad output format specified for outfile\n");
24374664626SKris Kennaway goto end;
24474664626SKris Kennaway }
245*b077aed3SPierre Pronchery
246*b077aed3SPierre Pronchery if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
247*b077aed3SPierre Pronchery if (pubout || pubin)
248*b077aed3SPierre Pronchery output_structure = "SubjectPublicKeyInfo";
249*b077aed3SPierre Pronchery else
250*b077aed3SPierre Pronchery output_structure = "type-specific";
251*b077aed3SPierre Pronchery }
252*b077aed3SPierre Pronchery
253*b077aed3SPierre Pronchery /* Select what you want in the output */
254*b077aed3SPierre Pronchery if (pubout || pubin) {
255*b077aed3SPierre Pronchery selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
256*b077aed3SPierre Pronchery } else {
257*b077aed3SPierre Pronchery assert(private);
258*b077aed3SPierre Pronchery selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
259*b077aed3SPierre Pronchery | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
260*b077aed3SPierre Pronchery }
261*b077aed3SPierre Pronchery
262*b077aed3SPierre Pronchery /* Perform the encoding */
263*b077aed3SPierre Pronchery ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, output_type,
264*b077aed3SPierre Pronchery output_structure, NULL);
265*b077aed3SPierre Pronchery if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
266*b077aed3SPierre Pronchery BIO_printf(bio_err, "%s format not supported\n", output_type);
267*b077aed3SPierre Pronchery goto end;
268*b077aed3SPierre Pronchery }
269*b077aed3SPierre Pronchery
270*b077aed3SPierre Pronchery /* Passphrase setup */
271*b077aed3SPierre Pronchery if (enc != NULL)
272*b077aed3SPierre Pronchery OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
273*b077aed3SPierre Pronchery
274*b077aed3SPierre Pronchery /* Default passphrase prompter */
275*b077aed3SPierre Pronchery if (enc != NULL || outformat == FORMAT_PVK) {
276*b077aed3SPierre Pronchery OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
277*b077aed3SPierre Pronchery if (passout != NULL)
278*b077aed3SPierre Pronchery /* When passout given, override the passphrase prompter */
279*b077aed3SPierre Pronchery OSSL_ENCODER_CTX_set_passphrase(ectx,
280*b077aed3SPierre Pronchery (const unsigned char *)passout,
281*b077aed3SPierre Pronchery strlen(passout));
282*b077aed3SPierre Pronchery }
283*b077aed3SPierre Pronchery
284*b077aed3SPierre Pronchery /* PVK requires a bit more */
285*b077aed3SPierre Pronchery if (outformat == FORMAT_PVK) {
286*b077aed3SPierre Pronchery OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
287*b077aed3SPierre Pronchery
288*b077aed3SPierre Pronchery params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
289*b077aed3SPierre Pronchery if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
290*b077aed3SPierre Pronchery BIO_printf(bio_err, "invalid PVK encryption level\n");
291*b077aed3SPierre Pronchery goto end;
292*b077aed3SPierre Pronchery }
293*b077aed3SPierre Pronchery }
294*b077aed3SPierre Pronchery
295*b077aed3SPierre Pronchery if (!OSSL_ENCODER_to_bio(ectx, out)) {
296*b077aed3SPierre Pronchery BIO_printf(bio_err, "unable to write key\n");
297e71b7053SJung-uk Kim goto end;
298e71b7053SJung-uk Kim }
29974664626SKris Kennaway ret = 0;
30074664626SKris Kennaway end:
301*b077aed3SPierre Pronchery if (ret != 0)
302*b077aed3SPierre Pronchery ERR_print_errors(bio_err);
303*b077aed3SPierre Pronchery OSSL_ENCODER_CTX_free(ectx);
3046f9291ceSJung-uk Kim BIO_free_all(out);
305*b077aed3SPierre Pronchery EVP_PKEY_free(pkey);
306*b077aed3SPierre Pronchery EVP_CIPHER_free(enc);
3076cf8931aSJung-uk Kim release_engine(e);
3086f9291ceSJung-uk Kim OPENSSL_free(passin);
3096f9291ceSJung-uk Kim OPENSSL_free(passout);
310e71b7053SJung-uk Kim return ret;
31174664626SKris Kennaway }
312