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