]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/dsaparam.c
Load rand state after loading providers
[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,
1696b890 31 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
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 52 {"text", OPT_TEXT, '-', "Print as text"},
7e1b7485 53 {"noout", OPT_NOOUT, '-', "No output"},
5388f986 54 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
7e1b7485 55 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
5388f986 56
3ee1eac2 57 OPT_R_OPTIONS,
6bd4e3f2 58 OPT_PROV_OPTIONS,
92de469f
RS
59
60 OPT_PARAMETERS(),
61 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
7e1b7485
RS
62 {NULL}
63};
667ac4ec 64
7e1b7485 65int dsaparam_main(int argc, char **argv)
0f113f3e 66{
dd1abd44 67 ENGINE *e = NULL;
b78c777e 68 BIO *out = NULL;
edf96591 69 EVP_PKEY *params = NULL, *pkey = NULL;
cd3572a1 70 EVP_PKEY_CTX *ctx = NULL;
3ee1eac2 71 int numbits = -1, num = 0, genkey = 0;
1696b890 72 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0;
3b061a00 73 int ret = 1, i, text = 0, private = 0;
3ee1eac2 74 char *infile = NULL, *outfile = NULL, *prog;
7e1b7485
RS
75 OPTION_CHOICE o;
76
77 prog = opt_init(argc, argv, dsaparam_options);
78 while ((o = opt_next()) != OPT_EOF) {
79 switch (o) {
80 case OPT_EOF:
81 case OPT_ERR:
82 opthelp:
83 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
84 goto end;
85 case OPT_HELP:
86 opt_help(dsaparam_options);
87 ret = 0;
88 goto end;
89 case OPT_INFORM:
90 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
91 goto opthelp;
92 break;
93 case OPT_IN:
94 infile = opt_arg();
95 break;
96 case OPT_OUTFORM:
97 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
98 goto opthelp;
99 break;
100 case OPT_OUT:
101 outfile = opt_arg();
102 break;
103 case OPT_ENGINE:
dd1abd44 104 e = setup_engine(opt_arg(), 0);
7e1b7485 105 break;
7e1b7485 106 case OPT_TEXT:
0f113f3e 107 text = 1;
7e1b7485 108 break;
7e1b7485 109 case OPT_GENKEY:
3ee1eac2 110 genkey = 1;
7e1b7485 111 break;
3ee1eac2
RS
112 case OPT_R_CASES:
113 if (!opt_rand(o))
114 goto end;
6bd4e3f2
P
115 break;
116 case OPT_PROV_CASES:
117 if (!opt_provider(o))
118 goto end;
7e1b7485
RS
119 break;
120 case OPT_NOOUT:
0f113f3e 121 noout = 1;
7e1b7485 122 break;
b6a07f67
PP
123 case OPT_VERBOSE:
124 verbose = 1;
125 break;
0f113f3e 126 }
0f113f3e 127 }
021410ea
RS
128
129 /* Optional arg is bitsize. */
7e1b7485
RS
130 argc = opt_num_rest();
131 argv = opt_rest();
7e1b7485 132 if (argc == 1) {
bd4850df 133 if (!opt_int(argv[0], &num) || num < 0)
021410ea
RS
134 goto opthelp;
135 } else if (argc != 0) {
136 goto opthelp;
0f113f3e 137 }
51e5df0e 138 app_RAND_load();
021410ea
RS
139
140 /* generate a key */
141 numbits = num;
3b061a00 142 private = genkey ? 1 : 0;
0f113f3e 143
bdd58d98 144 out = bio_open_owner(outfile, outformat, private);
7e1b7485 145 if (out == NULL)
0f113f3e 146 goto end;
0f113f3e 147
cd3572a1
P
148 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
149 if (ctx == NULL) {
cd3572a1
P
150 BIO_printf(bio_err,
151 "Error, DSA parameter generation context allocation failed\n");
152 goto end;
153 }
0f113f3e 154 if (numbits > 0) {
0336df2f
GS
155 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
156 BIO_printf(bio_err,
157 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
158 " Your key size is %d! Larger key size may behave not as expected.\n",
159 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
160
cd3572a1
P
161 EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
162 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
b6a07f67
PP
163 if (verbose) {
164 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
165 num);
166 BIO_printf(bio_err, "This could take some time\n");
167 }
cd3572a1 168 if (EVP_PKEY_paramgen_init(ctx) <= 0) {
cd3572a1
P
169 BIO_printf(bio_err,
170 "Error, DSA key generation paramgen init failed\n");
171 goto end;
172 }
173 if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
cd3572a1
P
174 BIO_printf(bio_err,
175 "Error, DSA key generation setting bit length failed\n");
176 goto end;
177 }
edf96591 178 if (EVP_PKEY_paramgen(ctx, &params) <= 0) {
0f113f3e
MC
179 BIO_printf(bio_err, "Error, DSA key generation failed\n");
180 goto end;
181 }
2234212c 182 } else {
8b0ec099 183 params = load_keyparams(infile, 1, "DSA", "DSA parameters");
2234212c 184 }
edf96591 185 if (params == NULL) {
8b0ec099 186 /* Error message should already have been displayed */
0f113f3e
MC
187 goto end;
188 }
189
190 if (text) {
edf96591 191 EVP_PKEY_print_params(out, params, 0, NULL);
0f113f3e
MC
192 }
193
52814352
BE
194 if (outformat == FORMAT_ASN1 && genkey)
195 noout = 1;
196
0f113f3e
MC
197 if (!noout) {
198 if (outformat == FORMAT_ASN1)
edf96591 199 i = i2d_KeyParams_bio(out, params);
7e1b7485 200 else
edf96591 201 i = PEM_write_bio_Parameters(out, params);
0f113f3e 202 if (!i) {
2a3158ac 203 BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
0f113f3e
MC
204 goto end;
205 }
206 }
207 if (genkey) {
cd3572a1 208 EVP_PKEY_CTX_free(ctx);
edf96591 209 ctx = EVP_PKEY_CTX_new(params, NULL);
cd3572a1 210 if (ctx == NULL) {
cd3572a1
P
211 BIO_printf(bio_err,
212 "Error, DSA key generation context allocation failed\n");
0f113f3e 213 goto end;
cd3572a1
P
214 }
215 if (!EVP_PKEY_keygen_init(ctx)) {
2a3158ac
DDO
216 BIO_printf(bio_err,
217 "Error, unable to initialise for key generation\n");
cd3572a1
P
218 goto end;
219 }
220 if (!EVP_PKEY_keygen(ctx, &pkey)) {
2a3158ac 221 BIO_printf(bio_err, "Error, unable to generate key\n");
cd3572a1
P
222 goto end;
223 }
3b061a00 224 assert(private);
0f113f3e 225 if (outformat == FORMAT_ASN1)
edf96591 226 i = i2d_PrivateKey_bio(out, pkey);
7e1b7485 227 else
edf96591 228 i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
0f113f3e 229 }
0f113f3e
MC
230 ret = 0;
231 end:
2a3158ac
DDO
232 if (ret != 0)
233 ERR_print_errors(bio_err);
ca3a82c3 234 BIO_free_all(out);
cd3572a1
P
235 EVP_PKEY_CTX_free(ctx);
236 EVP_PKEY_free(pkey);
edf96591 237 EVP_PKEY_free(params);
dd1abd44 238 release_engine(e);
26a7d938 239 return ret;
0f113f3e 240}
d02b48c6 241
cd3572a1 242static int gendsa_cb(EVP_PKEY_CTX *ctx)
0f113f3e 243{
201b305a 244 static const char symbols[] = ".+*\n";
cd3572a1
P
245 int p;
246 char c;
247 BIO *b;
0f113f3e 248
b6a07f67
PP
249 if (!verbose)
250 return 1;
251
cd3572a1
P
252 b = EVP_PKEY_CTX_get_app_data(ctx);
253 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
254 c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
255
256 BIO_write(b, &c, 1);
257 (void)BIO_flush(b);
0f113f3e
MC
258 return 1;
259}