]>
Commit | Line | Data |
---|---|---|
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 |
25 | static int verbose = 0; |
26 | ||
cd3572a1 | 27 | static int gendsa_cb(EVP_PKEY_CTX *ctx); |
667ac4ec | 28 | |
7e1b7485 RS |
29 | typedef 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 | 36 | const 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 | 65 | int 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 | } |
021410ea RS |
138 | |
139 | /* generate a key */ | |
140 | numbits = num; | |
3b061a00 | 141 | private = genkey ? 1 : 0; |
0f113f3e | 142 | |
bdd58d98 | 143 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 | 144 | if (out == NULL) |
0f113f3e | 145 | goto end; |
0f113f3e | 146 | |
cd3572a1 P |
147 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL); |
148 | if (ctx == NULL) { | |
cd3572a1 P |
149 | BIO_printf(bio_err, |
150 | "Error, DSA parameter generation context allocation failed\n"); | |
151 | goto end; | |
152 | } | |
0f113f3e | 153 | if (numbits > 0) { |
0336df2f GS |
154 | if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS) |
155 | BIO_printf(bio_err, | |
156 | "Warning: It is not recommended to use more than %d bit for DSA keys.\n" | |
157 | " Your key size is %d! Larger key size may behave not as expected.\n", | |
158 | OPENSSL_DSA_MAX_MODULUS_BITS, numbits); | |
159 | ||
cd3572a1 P |
160 | EVP_PKEY_CTX_set_cb(ctx, gendsa_cb); |
161 | EVP_PKEY_CTX_set_app_data(ctx, bio_err); | |
b6a07f67 PP |
162 | if (verbose) { |
163 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", | |
164 | num); | |
165 | BIO_printf(bio_err, "This could take some time\n"); | |
166 | } | |
cd3572a1 | 167 | if (EVP_PKEY_paramgen_init(ctx) <= 0) { |
cd3572a1 P |
168 | BIO_printf(bio_err, |
169 | "Error, DSA key generation paramgen init failed\n"); | |
170 | goto end; | |
171 | } | |
172 | if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) { | |
cd3572a1 P |
173 | BIO_printf(bio_err, |
174 | "Error, DSA key generation setting bit length failed\n"); | |
175 | goto end; | |
176 | } | |
edf96591 | 177 | if (EVP_PKEY_paramgen(ctx, ¶ms) <= 0) { |
0f113f3e MC |
178 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); |
179 | goto end; | |
180 | } | |
2234212c | 181 | } else { |
8b0ec099 | 182 | params = load_keyparams(infile, 1, "DSA", "DSA parameters"); |
2234212c | 183 | } |
edf96591 | 184 | if (params == NULL) { |
8b0ec099 | 185 | /* Error message should already have been displayed */ |
0f113f3e MC |
186 | goto end; |
187 | } | |
188 | ||
189 | if (text) { | |
edf96591 | 190 | EVP_PKEY_print_params(out, params, 0, NULL); |
0f113f3e MC |
191 | } |
192 | ||
52814352 BE |
193 | if (outformat == FORMAT_ASN1 && genkey) |
194 | noout = 1; | |
195 | ||
0f113f3e MC |
196 | if (!noout) { |
197 | if (outformat == FORMAT_ASN1) | |
edf96591 | 198 | i = i2d_KeyParams_bio(out, params); |
7e1b7485 | 199 | else |
edf96591 | 200 | i = PEM_write_bio_Parameters(out, params); |
0f113f3e | 201 | if (!i) { |
2a3158ac | 202 | BIO_printf(bio_err, "Error, unable to write DSA parameters\n"); |
0f113f3e MC |
203 | goto end; |
204 | } | |
205 | } | |
206 | if (genkey) { | |
cd3572a1 | 207 | EVP_PKEY_CTX_free(ctx); |
edf96591 | 208 | ctx = EVP_PKEY_CTX_new(params, NULL); |
cd3572a1 | 209 | if (ctx == NULL) { |
cd3572a1 P |
210 | BIO_printf(bio_err, |
211 | "Error, DSA key generation context allocation failed\n"); | |
0f113f3e | 212 | goto end; |
cd3572a1 P |
213 | } |
214 | if (!EVP_PKEY_keygen_init(ctx)) { | |
2a3158ac DDO |
215 | BIO_printf(bio_err, |
216 | "Error, unable to initialise for key generation\n"); | |
cd3572a1 P |
217 | goto end; |
218 | } | |
219 | if (!EVP_PKEY_keygen(ctx, &pkey)) { | |
2a3158ac | 220 | BIO_printf(bio_err, "Error, unable to generate key\n"); |
cd3572a1 P |
221 | goto end; |
222 | } | |
3b061a00 | 223 | assert(private); |
0f113f3e | 224 | if (outformat == FORMAT_ASN1) |
edf96591 | 225 | i = i2d_PrivateKey_bio(out, pkey); |
7e1b7485 | 226 | else |
edf96591 | 227 | i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL); |
0f113f3e | 228 | } |
0f113f3e MC |
229 | ret = 0; |
230 | end: | |
2a3158ac DDO |
231 | if (ret != 0) |
232 | ERR_print_errors(bio_err); | |
ca3a82c3 | 233 | BIO_free_all(out); |
cd3572a1 P |
234 | EVP_PKEY_CTX_free(ctx); |
235 | EVP_PKEY_free(pkey); | |
edf96591 | 236 | EVP_PKEY_free(params); |
dd1abd44 | 237 | release_engine(e); |
26a7d938 | 238 | return ret; |
0f113f3e | 239 | } |
d02b48c6 | 240 | |
cd3572a1 | 241 | static int gendsa_cb(EVP_PKEY_CTX *ctx) |
0f113f3e | 242 | { |
201b305a | 243 | static const char symbols[] = ".+*\n"; |
cd3572a1 P |
244 | int p; |
245 | char c; | |
246 | BIO *b; | |
0f113f3e | 247 | |
b6a07f67 PP |
248 | if (!verbose) |
249 | return 1; | |
250 | ||
cd3572a1 P |
251 | b = EVP_PKEY_CTX_get_app_data(ctx); |
252 | p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); | |
253 | c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?'; | |
254 | ||
255 | BIO_write(b, &c, 1); | |
256 | (void)BIO_flush(b); | |
0f113f3e MC |
257 | return 1; |
258 | } |