1 /*
2 * Copyright 1995-2022 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 <time.h>
15 #include <string.h>
16 #include "apps.h"
17 #include "progs.h"
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/bn.h>
21 #include <openssl/dsa.h>
22 #include <openssl/x509.h>
23 #include <openssl/pem.h>
24
25 static int verbose = 0;
26
27 typedef enum OPTION_choice {
28 OPT_COMMON,
29 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
30 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE, OPT_QUIET,
31 OPT_R_ENUM, OPT_PROV_ENUM
32 } OPTION_CHOICE;
33
34 const OPTIONS dsaparam_options[] = {
35 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
36
37 OPT_SECTION("General"),
38 {"help", OPT_HELP, '-', "Display this summary"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
41 #endif
42
43 OPT_SECTION("Input"),
44 {"in", OPT_IN, '<', "Input file"},
45 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
46
47 OPT_SECTION("Output"),
48 {"out", OPT_OUT, '>', "Output file"},
49 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
50 {"text", OPT_TEXT, '-', "Print as text"},
51 {"noout", OPT_NOOUT, '-', "No output"},
52 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
53 {"quiet", OPT_QUIET, '-', "Terse output"},
54 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
55
56 OPT_R_OPTIONS,
57 OPT_PROV_OPTIONS,
58
59 OPT_PARAMETERS(),
60 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
61 {NULL}
62 };
63
dsaparam_main(int argc,char ** argv)64 int dsaparam_main(int argc, char **argv)
65 {
66 ENGINE *e = NULL;
67 BIO *out = NULL;
68 EVP_PKEY *params = NULL, *pkey = NULL;
69 EVP_PKEY_CTX *ctx = NULL;
70 int numbits = -1, num = 0, genkey = 0;
71 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
72 int ret = 1, i, text = 0, private = 0;
73 char *infile = NULL, *outfile = NULL, *prog;
74 OPTION_CHOICE o;
75
76 prog = opt_init(argc, argv, dsaparam_options);
77 while ((o = opt_next()) != OPT_EOF) {
78 switch (o) {
79 case OPT_EOF:
80 case OPT_ERR:
81 opthelp:
82 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
83 goto end;
84 case OPT_HELP:
85 opt_help(dsaparam_options);
86 ret = 0;
87 goto end;
88 case OPT_INFORM:
89 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
90 goto opthelp;
91 break;
92 case OPT_IN:
93 infile = opt_arg();
94 break;
95 case OPT_OUTFORM:
96 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
97 goto opthelp;
98 break;
99 case OPT_OUT:
100 outfile = opt_arg();
101 break;
102 case OPT_ENGINE:
103 e = setup_engine(opt_arg(), 0);
104 break;
105 case OPT_TEXT:
106 text = 1;
107 break;
108 case OPT_GENKEY:
109 genkey = 1;
110 break;
111 case OPT_R_CASES:
112 if (!opt_rand(o))
113 goto end;
114 break;
115 case OPT_PROV_CASES:
116 if (!opt_provider(o))
117 goto end;
118 break;
119 case OPT_NOOUT:
120 noout = 1;
121 break;
122 case OPT_VERBOSE:
123 verbose = 1;
124 break;
125 case OPT_QUIET:
126 verbose = 0;
127 break;
128 }
129 }
130
131 /* Optional arg is bitsize. */
132 argc = opt_num_rest();
133 argv = opt_rest();
134 if (argc == 1) {
135 if (!opt_int(argv[0], &num) || num < 0)
136 goto opthelp;
137 } else if (!opt_check_rest_arg(NULL)) {
138 goto opthelp;
139 }
140 if (!app_RAND_load())
141 goto end;
142
143 /* generate a key */
144 numbits = num;
145 private = genkey ? 1 : 0;
146
147 out = bio_open_owner(outfile, outformat, private);
148 if (out == NULL)
149 goto end;
150
151 ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
152 if (ctx == NULL) {
153 BIO_printf(bio_err,
154 "Error, DSA parameter generation context allocation failed\n");
155 goto end;
156 }
157 if (numbits > 0) {
158 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
159 BIO_printf(bio_err,
160 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
161 " Your key size is %d! Larger key size may behave not as expected.\n",
162 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
163
164 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
165 if (verbose) {
166 EVP_PKEY_CTX_set_cb(ctx, progress_cb);
167 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
168 num);
169 BIO_printf(bio_err, "This could take some time\n");
170 }
171 if (EVP_PKEY_paramgen_init(ctx) <= 0) {
172 BIO_printf(bio_err,
173 "Error, DSA key generation paramgen init failed\n");
174 goto end;
175 }
176 if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
177 BIO_printf(bio_err,
178 "Error, DSA key generation setting bit length failed\n");
179 goto end;
180 }
181 params = app_paramgen(ctx, "DSA");
182 } else {
183 params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
184 }
185 if (params == NULL) {
186 /* Error message should already have been displayed */
187 goto end;
188 }
189
190 if (text) {
191 EVP_PKEY_print_params(out, params, 0, NULL);
192 }
193
194 if (outformat == FORMAT_ASN1 && genkey)
195 noout = 1;
196
197 if (!noout) {
198 if (outformat == FORMAT_ASN1)
199 i = i2d_KeyParams_bio(out, params);
200 else
201 i = PEM_write_bio_Parameters(out, params);
202 if (!i) {
203 BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
204 goto end;
205 }
206 }
207 if (genkey) {
208 EVP_PKEY_CTX_free(ctx);
209 ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
210 app_get0_propq());
211 if (ctx == NULL) {
212 BIO_printf(bio_err,
213 "Error, DSA key generation context allocation failed\n");
214 goto end;
215 }
216 if (EVP_PKEY_keygen_init(ctx) <= 0) {
217 BIO_printf(bio_err,
218 "Error, unable to initialise for key generation\n");
219 goto end;
220 }
221 pkey = app_keygen(ctx, "DSA", numbits, verbose);
222 assert(private);
223 if (outformat == FORMAT_ASN1)
224 i = i2d_PrivateKey_bio(out, pkey);
225 else
226 i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
227 }
228 ret = 0;
229 end:
230 if (ret != 0)
231 ERR_print_errors(bio_err);
232 BIO_free_all(out);
233 EVP_PKEY_CTX_free(ctx);
234 EVP_PKEY_free(pkey);
235 EVP_PKEY_free(params);
236 release_engine(e);
237 return ret;
238 }
239
240