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