]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
6738bf14 | 2 | * Copyright 1995-2018 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 | ||
1ddf2594 P |
10 | /* We need to use some deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
effaf4de | 13 | #include <openssl/opensslconf.h> |
8bb9c60b | 14 | #ifdef OPENSSL_NO_DSA |
effaf4de RS |
15 | NON_EMPTY_TRANSLATION_UNIT |
16 | #else | |
5daec7ea | 17 | |
0f113f3e MC |
18 | # include <stdio.h> |
19 | # include <stdlib.h> | |
20 | # include <time.h> | |
21 | # include <string.h> | |
22 | # include "apps.h" | |
dab2cd68 | 23 | # include "progs.h" |
0f113f3e MC |
24 | # include <openssl/bio.h> |
25 | # include <openssl/err.h> | |
26 | # include <openssl/bn.h> | |
27 | # include <openssl/dsa.h> | |
28 | # include <openssl/x509.h> | |
29 | # include <openssl/pem.h> | |
30 | ||
b6a07f67 PP |
31 | static int verbose = 0; |
32 | ||
6d23cf97 | 33 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
667ac4ec | 34 | |
7e1b7485 RS |
35 | typedef enum OPTION_choice { |
36 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
37 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, | |
b6a07f67 PP |
38 | OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE, |
39 | OPT_R_ENUM | |
7e1b7485 RS |
40 | } OPTION_CHOICE; |
41 | ||
44c83ebd | 42 | const OPTIONS dsaparam_options[] = { |
92de469f RS |
43 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"}, |
44 | ||
5388f986 | 45 | OPT_SECTION("General"), |
7e1b7485 | 46 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
47 | # ifndef OPENSSL_NO_ENGINE |
48 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
49 | # endif | |
50 | ||
51 | OPT_SECTION("Input"), | |
7e1b7485 | 52 | {"in", OPT_IN, '<', "Input file"}, |
5388f986 RS |
53 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, |
54 | ||
55 | OPT_SECTION("Output"), | |
7e1b7485 | 56 | {"out", OPT_OUT, '>', "Output file"}, |
5388f986 | 57 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, |
7e1b7485 RS |
58 | {"text", OPT_TEXT, '-', "Print as text"}, |
59 | {"C", OPT_C, '-', "Output C code"}, | |
60 | {"noout", OPT_NOOUT, '-', "No output"}, | |
5388f986 | 61 | {"verbose", OPT_VERBOSE, '-', "Verbose output"}, |
7e1b7485 | 62 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, |
5388f986 | 63 | |
3ee1eac2 | 64 | OPT_R_OPTIONS, |
92de469f RS |
65 | |
66 | OPT_PARAMETERS(), | |
67 | {"numbits", 0, 0, "Number of bits if generating parameters (optional)"}, | |
7e1b7485 RS |
68 | {NULL} |
69 | }; | |
667ac4ec | 70 | |
7e1b7485 | 71 | int dsaparam_main(int argc, char **argv) |
0f113f3e | 72 | { |
dd1abd44 | 73 | ENGINE *e = NULL; |
0f113f3e | 74 | DSA *dsa = NULL; |
0f113f3e | 75 | BIO *in = NULL, *out = NULL; |
0f113f3e | 76 | BN_GENCB *cb = NULL; |
3ee1eac2 | 77 | int numbits = -1, num = 0, genkey = 0; |
3b061a00 RS |
78 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
79 | int ret = 1, i, text = 0, private = 0; | |
3ee1eac2 | 80 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 RS |
81 | OPTION_CHOICE o; |
82 | ||
83 | prog = opt_init(argc, argv, dsaparam_options); | |
84 | while ((o = opt_next()) != OPT_EOF) { | |
85 | switch (o) { | |
86 | case OPT_EOF: | |
87 | case OPT_ERR: | |
88 | opthelp: | |
89 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
90 | goto end; | |
91 | case OPT_HELP: | |
92 | opt_help(dsaparam_options); | |
93 | ret = 0; | |
94 | goto end; | |
95 | case OPT_INFORM: | |
96 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
97 | goto opthelp; | |
98 | break; | |
99 | case OPT_IN: | |
100 | infile = opt_arg(); | |
101 | break; | |
102 | case OPT_OUTFORM: | |
103 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
104 | goto opthelp; | |
105 | break; | |
106 | case OPT_OUT: | |
107 | outfile = opt_arg(); | |
108 | break; | |
109 | case OPT_ENGINE: | |
dd1abd44 | 110 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 111 | break; |
7e1b7485 | 112 | case OPT_TEXT: |
0f113f3e | 113 | text = 1; |
7e1b7485 RS |
114 | break; |
115 | case OPT_C: | |
0f113f3e | 116 | C = 1; |
7e1b7485 RS |
117 | break; |
118 | case OPT_GENKEY: | |
3ee1eac2 | 119 | genkey = 1; |
7e1b7485 | 120 | break; |
3ee1eac2 RS |
121 | case OPT_R_CASES: |
122 | if (!opt_rand(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 | |
0f113f3e | 151 | if (numbits > 0) { |
0336df2f GS |
152 | if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS) |
153 | BIO_printf(bio_err, | |
154 | "Warning: It is not recommended to use more than %d bit for DSA keys.\n" | |
155 | " Your key size is %d! Larger key size may behave not as expected.\n", | |
156 | OPENSSL_DSA_MAX_MODULUS_BITS, numbits); | |
157 | ||
0f113f3e | 158 | cb = BN_GENCB_new(); |
96487cdd | 159 | if (cb == NULL) { |
0f113f3e MC |
160 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); |
161 | goto end; | |
162 | } | |
163 | BN_GENCB_set(cb, dsa_cb, bio_err); | |
0f113f3e | 164 | dsa = DSA_new(); |
96487cdd | 165 | if (dsa == NULL) { |
0f113f3e MC |
166 | BIO_printf(bio_err, "Error allocating DSA object\n"); |
167 | goto end; | |
168 | } | |
b6a07f67 PP |
169 | if (verbose) { |
170 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", | |
171 | num); | |
172 | BIO_printf(bio_err, "This could take some time\n"); | |
173 | } | |
0f113f3e | 174 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { |
0f113f3e MC |
175 | ERR_print_errors(bio_err); |
176 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); | |
177 | goto end; | |
178 | } | |
2234212c | 179 | } else if (informat == FORMAT_ASN1) { |
0f113f3e | 180 | dsa = d2i_DSAparams_bio(in, NULL); |
2234212c | 181 | } else { |
0f113f3e | 182 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
2234212c | 183 | } |
0f113f3e MC |
184 | if (dsa == NULL) { |
185 | BIO_printf(bio_err, "unable to load DSA parameters\n"); | |
186 | ERR_print_errors(bio_err); | |
187 | goto end; | |
188 | } | |
189 | ||
190 | if (text) { | |
191 | DSAparams_print(out, dsa); | |
192 | } | |
193 | ||
194 | if (C) { | |
2ac6115d | 195 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; |
ae6c553e | 196 | unsigned char *data; |
6e9fa57c MC |
197 | int len, bits_p; |
198 | ||
199 | DSA_get0_pqg(dsa, &p, &q, &g); | |
200 | len = BN_num_bytes(p); | |
201 | bits_p = BN_num_bits(p); | |
202 | ||
ae6c553e | 203 | data = app_malloc(len + 20, "BN space"); |
0f113f3e | 204 | |
d6b50b6e | 205 | BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p); |
708c28f2 AP |
206 | print_bignum_var(bio_out, p, "dsap", bits_p, data); |
207 | print_bignum_var(bio_out, q, "dsaq", bits_p, data); | |
208 | print_bignum_var(bio_out, g, "dsag", bits_p, data); | |
7e1b7485 | 209 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" |
708c28f2 | 210 | " BIGNUM *p, *q, *g;\n" |
7e1b7485 RS |
211 | "\n"); |
212 | BIO_printf(bio_out, " if (dsa == NULL)\n" | |
213 | " return NULL;\n"); | |
708c28f2 AP |
214 | BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n", |
215 | bits_p, bits_p); | |
216 | BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n", | |
217 | bits_p, bits_p); | |
218 | BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n", | |
219 | bits_p, bits_p); | |
220 | BIO_printf(bio_out, " DSA_free(dsa);\n" | |
221 | " BN_free(p);\n" | |
222 | " BN_free(q);\n" | |
223 | " BN_free(g);\n" | |
7e1b7485 RS |
224 | " return NULL;\n" |
225 | " }\n" | |
708c28f2 | 226 | " return dsa;\n}\n"); |
a855d1a1 | 227 | OPENSSL_free(data); |
0f113f3e MC |
228 | } |
229 | ||
52814352 BE |
230 | if (outformat == FORMAT_ASN1 && genkey) |
231 | noout = 1; | |
232 | ||
0f113f3e MC |
233 | if (!noout) { |
234 | if (outformat == FORMAT_ASN1) | |
235 | i = i2d_DSAparams_bio(out, dsa); | |
7e1b7485 | 236 | else |
0f113f3e | 237 | i = PEM_write_bio_DSAparams(out, dsa); |
0f113f3e MC |
238 | if (!i) { |
239 | BIO_printf(bio_err, "unable to write DSA parameters\n"); | |
240 | ERR_print_errors(bio_err); | |
241 | goto end; | |
242 | } | |
243 | } | |
244 | if (genkey) { | |
245 | DSA *dsakey; | |
246 | ||
0f113f3e MC |
247 | if ((dsakey = DSAparams_dup(dsa)) == NULL) |
248 | goto end; | |
0f113f3e MC |
249 | if (!DSA_generate_key(dsakey)) { |
250 | ERR_print_errors(bio_err); | |
251 | DSA_free(dsakey); | |
252 | goto end; | |
253 | } | |
3b061a00 | 254 | assert(private); |
0f113f3e MC |
255 | if (outformat == FORMAT_ASN1) |
256 | i = i2d_DSAPrivateKey_bio(out, dsakey); | |
7e1b7485 | 257 | else |
0f113f3e MC |
258 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
259 | NULL); | |
0f113f3e MC |
260 | DSA_free(dsakey); |
261 | } | |
0f113f3e MC |
262 | ret = 0; |
263 | end: | |
23a1d5e9 | 264 | BN_GENCB_free(cb); |
ca3a82c3 RS |
265 | BIO_free(in); |
266 | BIO_free_all(out); | |
d6407083 | 267 | DSA_free(dsa); |
dd1abd44 | 268 | release_engine(e); |
26a7d938 | 269 | return ret; |
0f113f3e | 270 | } |
d02b48c6 | 271 | |
6d23cf97 | 272 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
0f113f3e | 273 | { |
201b305a BB |
274 | static const char symbols[] = ".+*\n"; |
275 | char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?'; | |
0f113f3e | 276 | |
b6a07f67 PP |
277 | if (!verbose) |
278 | return 1; | |
279 | ||
0f113f3e MC |
280 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); |
281 | (void)BIO_flush(BN_GENCB_get_arg(cb)); | |
0f113f3e MC |
282 | return 1; |
283 | } | |
f5d7a031 | 284 | #endif |