]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/dsaparam.c
Don't downgrade keys in libssl
[thirdparty/openssl.git] / apps / dsaparam.c
CommitLineData
846e33c7 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
d02b48c6
RE
8 */
9
effaf4de 10#include <openssl/opensslconf.h>
5daec7ea 11
1ae56f2f
RS
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>
0f113f3e 24
b6a07f67
PP
25static int verbose = 0;
26
cd3572a1 27static int gendsa_cb(EVP_PKEY_CTX *ctx);
667ac4ec 28
7e1b7485
RS
29typedef enum OPTION_choice {
30 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
b6a07f67 32 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
6bd4e3f2 33 OPT_R_ENUM, OPT_PROV_ENUM
7e1b7485
RS
34} OPTION_CHOICE;
35
44c83ebd 36const OPTIONS dsaparam_options[] = {
92de469f
RS
37 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
38
5388f986 39 OPT_SECTION("General"),
7e1b7485 40 {"help", OPT_HELP, '-', "Display this summary"},
1ae56f2f 41#ifndef OPENSSL_NO_ENGINE
5388f986 42 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
1ae56f2f 43#endif
5388f986
RS
44
45 OPT_SECTION("Input"),
7e1b7485 46 {"in", OPT_IN, '<', "Input file"},
5388f986
RS
47 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
48
49 OPT_SECTION("Output"),
7e1b7485 50 {"out", OPT_OUT, '>', "Output file"},
5388f986 51 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
7e1b7485
RS
52 {"text", OPT_TEXT, '-', "Print as text"},
53 {"C", OPT_C, '-', "Output C code"},
54 {"noout", OPT_NOOUT, '-', "No output"},
5388f986 55 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
7e1b7485 56 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
5388f986 57
3ee1eac2 58 OPT_R_OPTIONS,
6bd4e3f2 59 OPT_PROV_OPTIONS,
92de469f
RS
60
61 OPT_PARAMETERS(),
62 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
7e1b7485
RS
63 {NULL}
64};
667ac4ec 65
7e1b7485 66int dsaparam_main(int argc, char **argv)
0f113f3e 67{
dd1abd44 68 ENGINE *e = NULL;
0f113f3e 69 DSA *dsa = NULL;
0f113f3e 70 BIO *in = NULL, *out = NULL;
cd3572a1
P
71 EVP_PKEY *pkey = NULL;
72 EVP_PKEY_CTX *ctx = NULL;
3ee1eac2 73 int numbits = -1, num = 0, genkey = 0;
3b061a00
RS
74 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
75 int ret = 1, i, text = 0, private = 0;
3ee1eac2 76 char *infile = NULL, *outfile = NULL, *prog;
7e1b7485
RS
77 OPTION_CHOICE o;
78
79 prog = opt_init(argc, argv, dsaparam_options);
80 while ((o = opt_next()) != OPT_EOF) {
81 switch (o) {
82 case OPT_EOF:
83 case OPT_ERR:
84 opthelp:
85 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
86 goto end;
87 case OPT_HELP:
88 opt_help(dsaparam_options);
89 ret = 0;
90 goto end;
91 case OPT_INFORM:
92 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
93 goto opthelp;
94 break;
95 case OPT_IN:
96 infile = opt_arg();
97 break;
98 case OPT_OUTFORM:
99 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
100 goto opthelp;
101 break;
102 case OPT_OUT:
103 outfile = opt_arg();
104 break;
105 case OPT_ENGINE:
dd1abd44 106 e = setup_engine(opt_arg(), 0);
7e1b7485 107 break;
7e1b7485 108 case OPT_TEXT:
0f113f3e 109 text = 1;
7e1b7485
RS
110 break;
111 case OPT_C:
0f113f3e 112 C = 1;
7e1b7485
RS
113 break;
114 case OPT_GENKEY:
3ee1eac2 115 genkey = 1;
7e1b7485 116 break;
3ee1eac2
RS
117 case OPT_R_CASES:
118 if (!opt_rand(o))
119 goto end;
6bd4e3f2
P
120 break;
121 case OPT_PROV_CASES:
122 if (!opt_provider(o))
123 goto end;
7e1b7485
RS
124 break;
125 case OPT_NOOUT:
0f113f3e 126 noout = 1;
7e1b7485 127 break;
b6a07f67
PP
128 case OPT_VERBOSE:
129 verbose = 1;
130 break;
0f113f3e 131 }
0f113f3e 132 }
7e1b7485
RS
133 argc = opt_num_rest();
134 argv = opt_rest();
0f113f3e 135
7e1b7485 136 if (argc == 1) {
bd4850df 137 if (!opt_int(argv[0], &num) || num < 0)
7e1b7485
RS
138 goto end;
139 /* generate a key */
140 numbits = num;
0f113f3e 141 }
3b061a00 142 private = genkey ? 1 : 0;
0f113f3e 143
bdd58d98 144 in = bio_open_default(infile, 'r', informat);
7e1b7485
RS
145 if (in == NULL)
146 goto end;
bdd58d98 147 out = bio_open_owner(outfile, outformat, private);
7e1b7485 148 if (out == NULL)
0f113f3e 149 goto end;
0f113f3e 150
cd3572a1
P
151 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
152 if (ctx == NULL) {
153 ERR_print_errors(bio_err);
154 BIO_printf(bio_err,
155 "Error, DSA parameter generation context allocation failed\n");
156 goto end;
157 }
0f113f3e 158 if (numbits > 0) {
0336df2f
GS
159 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
160 BIO_printf(bio_err,
161 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
162 " Your key size is %d! Larger key size may behave not as expected.\n",
163 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
164
cd3572a1
P
165 EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
166 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
b6a07f67
PP
167 if (verbose) {
168 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
169 num);
170 BIO_printf(bio_err, "This could take some time\n");
171 }
cd3572a1
P
172 if (EVP_PKEY_paramgen_init(ctx) <= 0) {
173 ERR_print_errors(bio_err);
174 BIO_printf(bio_err,
175 "Error, DSA key generation paramgen init failed\n");
176 goto end;
177 }
178 if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
179 ERR_print_errors(bio_err);
180 BIO_printf(bio_err,
181 "Error, DSA key generation setting bit length failed\n");
182 goto end;
183 }
184 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
0f113f3e
MC
185 ERR_print_errors(bio_err);
186 BIO_printf(bio_err, "Error, DSA key generation failed\n");
187 goto end;
188 }
cd3572a1
P
189 dsa = EVP_PKEY_get1_DSA(pkey);
190 if (dsa == NULL) {
191 ERR_print_errors(bio_err);
192 BIO_printf(bio_err, "Error, DSA key extraction failed\n");
193 goto end;
194 }
2234212c 195 } else if (informat == FORMAT_ASN1) {
0f113f3e 196 dsa = d2i_DSAparams_bio(in, NULL);
2234212c 197 } else {
0f113f3e 198 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
2234212c 199 }
0f113f3e
MC
200 if (dsa == NULL) {
201 BIO_printf(bio_err, "unable to load DSA parameters\n");
202 ERR_print_errors(bio_err);
203 goto end;
204 }
205
cd3572a1
P
206 if (pkey == NULL) {
207 pkey = EVP_PKEY_new();
208 if (pkey == NULL) {
209 BIO_printf(bio_err, "Error, unable to allocate PKEY object\n");
210 ERR_print_errors(bio_err);
211 goto end;
212 }
213 if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
214 BIO_printf(bio_err, "Error, unable to set DSA parameters\n");
215 ERR_print_errors(bio_err);
216 goto end;
217 }
218 }
0f113f3e 219 if (text) {
cd3572a1 220 EVP_PKEY_print_params(out, pkey, 0, NULL);
0f113f3e
MC
221 }
222
223 if (C) {
2ac6115d 224 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
ae6c553e 225 unsigned char *data;
6e9fa57c
MC
226 int len, bits_p;
227
228 DSA_get0_pqg(dsa, &p, &q, &g);
229 len = BN_num_bytes(p);
230 bits_p = BN_num_bits(p);
231
ae6c553e 232 data = app_malloc(len + 20, "BN space");
0f113f3e 233
d6b50b6e 234 BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
708c28f2
AP
235 print_bignum_var(bio_out, p, "dsap", bits_p, data);
236 print_bignum_var(bio_out, q, "dsaq", bits_p, data);
237 print_bignum_var(bio_out, g, "dsag", bits_p, data);
7e1b7485 238 BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
708c28f2 239 " BIGNUM *p, *q, *g;\n"
7e1b7485
RS
240 "\n");
241 BIO_printf(bio_out, " if (dsa == NULL)\n"
242 " return NULL;\n");
708c28f2
AP
243 BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
244 bits_p, bits_p);
245 BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
246 bits_p, bits_p);
247 BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
248 bits_p, bits_p);
249 BIO_printf(bio_out, " DSA_free(dsa);\n"
250 " BN_free(p);\n"
251 " BN_free(q);\n"
252 " BN_free(g);\n"
7e1b7485
RS
253 " return NULL;\n"
254 " }\n"
708c28f2 255 " return dsa;\n}\n");
a855d1a1 256 OPENSSL_free(data);
0f113f3e
MC
257 }
258
52814352
BE
259 if (outformat == FORMAT_ASN1 && genkey)
260 noout = 1;
261
0f113f3e
MC
262 if (!noout) {
263 if (outformat == FORMAT_ASN1)
264 i = i2d_DSAparams_bio(out, dsa);
7e1b7485 265 else
0f113f3e 266 i = PEM_write_bio_DSAparams(out, dsa);
0f113f3e
MC
267 if (!i) {
268 BIO_printf(bio_err, "unable to write DSA parameters\n");
269 ERR_print_errors(bio_err);
270 goto end;
271 }
272 }
273 if (genkey) {
274 DSA *dsakey;
275
cd3572a1
P
276 EVP_PKEY_CTX_free(ctx);
277 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
278 if (ctx == NULL) {
279 ERR_print_errors(bio_err);
280 BIO_printf(bio_err,
281 "Error, DSA key generation context allocation failed\n");
0f113f3e 282 goto end;
cd3572a1
P
283 }
284 if (!EVP_PKEY_keygen_init(ctx)) {
285 BIO_printf(bio_err, "unable to initialise for key generation\n");
286 ERR_print_errors(bio_err);
287 goto end;
288 }
289 if (!EVP_PKEY_keygen(ctx, &pkey)) {
290 BIO_printf(bio_err, "unable to generate key\n");
291 ERR_print_errors(bio_err);
292 goto end;
293 }
294 dsakey = EVP_PKEY_get0_DSA(pkey);
295 if (dsakey == NULL) {
296 BIO_printf(bio_err, "unable to extract generated key\n");
0f113f3e 297 ERR_print_errors(bio_err);
0f113f3e
MC
298 goto end;
299 }
3b061a00 300 assert(private);
0f113f3e
MC
301 if (outformat == FORMAT_ASN1)
302 i = i2d_DSAPrivateKey_bio(out, dsakey);
7e1b7485 303 else
0f113f3e
MC
304 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
305 NULL);
0f113f3e 306 }
0f113f3e
MC
307 ret = 0;
308 end:
ca3a82c3
RS
309 BIO_free(in);
310 BIO_free_all(out);
cd3572a1
P
311 EVP_PKEY_CTX_free(ctx);
312 EVP_PKEY_free(pkey);
d6407083 313 DSA_free(dsa);
dd1abd44 314 release_engine(e);
26a7d938 315 return ret;
0f113f3e 316}
d02b48c6 317
cd3572a1 318static int gendsa_cb(EVP_PKEY_CTX *ctx)
0f113f3e 319{
201b305a 320 static const char symbols[] = ".+*\n";
cd3572a1
P
321 int p;
322 char c;
323 BIO *b;
0f113f3e 324
b6a07f67
PP
325 if (!verbose)
326 return 1;
327
cd3572a1
P
328 b = EVP_PKEY_CTX_get_app_data(ctx);
329 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
330 c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
331
332 BIO_write(b, &c, 1);
333 (void)BIO_flush(b);
0f113f3e
MC
334 return 1;
335}