]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
d02b48c6 RE |
8 | */ |
9 | ||
effaf4de RS |
10 | #include <openssl/opensslconf.h> |
11 | #ifdef OPENSSL_NO_DSA | |
12 | NON_EMPTY_TRANSLATION_UNIT | |
13 | #else | |
5daec7ea | 14 | |
0f113f3e MC |
15 | # include <stdio.h> |
16 | # include <stdlib.h> | |
17 | # include <time.h> | |
18 | # include <string.h> | |
19 | # include "apps.h" | |
20 | # include <openssl/bio.h> | |
21 | # include <openssl/err.h> | |
22 | # include <openssl/bn.h> | |
23 | # include <openssl/dsa.h> | |
24 | # include <openssl/x509.h> | |
25 | # include <openssl/pem.h> | |
26 | ||
0f113f3e | 27 | # ifdef GENCB_TEST |
5daec7ea GT |
28 | |
29 | static int stop_keygen_flag = 0; | |
30 | ||
f92570f0 | 31 | static void timebomb_sigalarm(int foo) |
0f113f3e MC |
32 | { |
33 | stop_keygen_flag = 1; | |
34 | } | |
5daec7ea | 35 | |
0f113f3e | 36 | # endif |
5daec7ea | 37 | |
6d23cf97 | 38 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
667ac4ec | 39 | |
7e1b7485 RS |
40 | typedef enum OPTION_choice { |
41 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
42 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, | |
700b4a4a | 43 | OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE, |
7e1b7485 RS |
44 | OPT_TIMEBOMB |
45 | } OPTION_CHOICE; | |
46 | ||
47 | OPTIONS dsaparam_options[] = { | |
48 | {"help", OPT_HELP, '-', "Display this summary"}, | |
49 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, | |
50 | {"in", OPT_IN, '<', "Input file"}, | |
51 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, | |
52 | {"out", OPT_OUT, '>', "Output file"}, | |
53 | {"text", OPT_TEXT, '-', "Print as text"}, | |
54 | {"C", OPT_C, '-', "Output C code"}, | |
55 | {"noout", OPT_NOOUT, '-', "No output"}, | |
56 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, | |
57 | {"rand", OPT_RAND, 's', "Files to use for random number input"}, | |
7e1b7485 RS |
58 | # ifdef GENCB_TEST |
59 | {"timebomb", OPT_TIMEBOMB, 'p', "Interrupt keygen after 'pnum' seconds"}, | |
9c3bcfa0 RS |
60 | # endif |
61 | # ifndef OPENSSL_NO_ENGINE | |
62 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
7e1b7485 RS |
63 | # endif |
64 | {NULL} | |
65 | }; | |
667ac4ec | 66 | |
7e1b7485 | 67 | int dsaparam_main(int argc, char **argv) |
0f113f3e MC |
68 | { |
69 | DSA *dsa = NULL; | |
0f113f3e | 70 | BIO *in = NULL, *out = NULL; |
0f113f3e | 71 | BN_GENCB *cb = NULL; |
700b4a4a | 72 | int numbits = -1, num = 0, genkey = 0, need_rand = 0; |
3b061a00 RS |
73 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
74 | int ret = 1, i, text = 0, private = 0; | |
0f113f3e MC |
75 | # ifdef GENCB_TEST |
76 | int timebomb = 0; | |
77 | # endif | |
333b070e | 78 | char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL; |
7e1b7485 RS |
79 | OPTION_CHOICE o; |
80 | ||
81 | prog = opt_init(argc, argv, dsaparam_options); | |
82 | while ((o = opt_next()) != OPT_EOF) { | |
83 | switch (o) { | |
84 | case OPT_EOF: | |
85 | case OPT_ERR: | |
86 | opthelp: | |
87 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
88 | goto end; | |
89 | case OPT_HELP: | |
90 | opt_help(dsaparam_options); | |
91 | ret = 0; | |
92 | goto end; | |
93 | case OPT_INFORM: | |
94 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
95 | goto opthelp; | |
96 | break; | |
97 | case OPT_IN: | |
98 | infile = opt_arg(); | |
99 | break; | |
100 | case OPT_OUTFORM: | |
101 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
102 | goto opthelp; | |
103 | break; | |
104 | case OPT_OUT: | |
105 | outfile = opt_arg(); | |
106 | break; | |
107 | case OPT_ENGINE: | |
333b070e | 108 | (void)setup_engine(opt_arg(), 0); |
7e1b7485 RS |
109 | break; |
110 | case OPT_TIMEBOMB: | |
0f113f3e | 111 | # ifdef GENCB_TEST |
7e1b7485 RS |
112 | timebomb = atoi(opt_arg()); |
113 | break; | |
0f113f3e | 114 | # endif |
7e1b7485 | 115 | case OPT_TEXT: |
0f113f3e | 116 | text = 1; |
7e1b7485 RS |
117 | break; |
118 | case OPT_C: | |
0f113f3e | 119 | C = 1; |
7e1b7485 RS |
120 | break; |
121 | case OPT_GENKEY: | |
122 | genkey = need_rand = 1; | |
123 | break; | |
124 | case OPT_RAND: | |
125 | inrand = opt_arg(); | |
0f113f3e | 126 | need_rand = 1; |
7e1b7485 RS |
127 | break; |
128 | case OPT_NOOUT: | |
0f113f3e | 129 | noout = 1; |
7e1b7485 | 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; | |
141 | need_rand = 1; | |
0f113f3e | 142 | } |
3b061a00 | 143 | private = genkey ? 1 : 0; |
0f113f3e | 144 | |
bdd58d98 | 145 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 RS |
146 | if (in == NULL) |
147 | goto end; | |
bdd58d98 | 148 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 | 149 | if (out == NULL) |
0f113f3e | 150 | goto end; |
0f113f3e | 151 | |
0f113f3e | 152 | if (need_rand) { |
7e1b7485 | 153 | app_RAND_load_file(NULL, (inrand != NULL)); |
0f113f3e MC |
154 | if (inrand != NULL) |
155 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
156 | app_RAND_load_files(inrand)); | |
157 | } | |
158 | ||
159 | if (numbits > 0) { | |
160 | cb = BN_GENCB_new(); | |
96487cdd | 161 | if (cb == NULL) { |
0f113f3e MC |
162 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); |
163 | goto end; | |
164 | } | |
165 | BN_GENCB_set(cb, dsa_cb, bio_err); | |
166 | assert(need_rand); | |
167 | dsa = DSA_new(); | |
96487cdd | 168 | if (dsa == NULL) { |
0f113f3e MC |
169 | BIO_printf(bio_err, "Error allocating DSA object\n"); |
170 | goto end; | |
171 | } | |
0f113f3e MC |
172 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", |
173 | num); | |
174 | BIO_printf(bio_err, "This could take some time\n"); | |
175 | # ifdef GENCB_TEST | |
176 | if (timebomb > 0) { | |
177 | struct sigaction act; | |
178 | act.sa_handler = timebomb_sigalarm; | |
179 | act.sa_flags = 0; | |
180 | BIO_printf(bio_err, | |
181 | "(though I'll stop it if not done within %d secs)\n", | |
182 | timebomb); | |
183 | if (sigaction(SIGALRM, &act, NULL) != 0) { | |
184 | BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n"); | |
185 | goto end; | |
186 | } | |
187 | alarm(timebomb); | |
188 | } | |
189 | # endif | |
190 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { | |
191 | # ifdef GENCB_TEST | |
192 | if (stop_keygen_flag) { | |
193 | BIO_printf(bio_err, "DSA key generation time-stopped\n"); | |
194 | /* This is an asked-for behaviour! */ | |
195 | ret = 0; | |
196 | goto end; | |
197 | } | |
198 | # endif | |
199 | ERR_print_errors(bio_err); | |
200 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); | |
201 | goto end; | |
202 | } | |
203 | } else if (informat == FORMAT_ASN1) | |
204 | dsa = d2i_DSAparams_bio(in, NULL); | |
7e1b7485 | 205 | else |
0f113f3e | 206 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
0f113f3e MC |
207 | if (dsa == NULL) { |
208 | BIO_printf(bio_err, "unable to load DSA parameters\n"); | |
209 | ERR_print_errors(bio_err); | |
210 | goto end; | |
211 | } | |
212 | ||
213 | if (text) { | |
214 | DSAparams_print(out, dsa); | |
215 | } | |
216 | ||
217 | if (C) { | |
2ac6115d | 218 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; |
ae6c553e | 219 | unsigned char *data; |
6e9fa57c MC |
220 | int len, bits_p; |
221 | ||
222 | DSA_get0_pqg(dsa, &p, &q, &g); | |
223 | len = BN_num_bytes(p); | |
224 | bits_p = BN_num_bits(p); | |
225 | ||
ae6c553e | 226 | data = app_malloc(len + 20, "BN space"); |
0f113f3e | 227 | |
7e1b7485 | 228 | BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p); |
6e9fa57c MC |
229 | print_bignum_var(bio_out, p, "dsap", len, data); |
230 | print_bignum_var(bio_out, q, "dsaq", len, data); | |
231 | print_bignum_var(bio_out, g, "dsag", len, data); | |
7e1b7485 RS |
232 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" |
233 | "\n"); | |
234 | BIO_printf(bio_out, " if (dsa == NULL)\n" | |
235 | " return NULL;\n"); | |
236 | BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n", | |
0f113f3e | 237 | bits_p, bits_p); |
7e1b7485 | 238 | BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n", |
0f113f3e | 239 | bits_p, bits_p); |
7e1b7485 | 240 | BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n", |
0f113f3e | 241 | bits_p, bits_p); |
7e1b7485 RS |
242 | BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n" |
243 | " DSA_free(dsa);\n" | |
244 | " return NULL;\n" | |
245 | " }\n" | |
246 | " return(dsa);\n}\n"); | |
a855d1a1 | 247 | OPENSSL_free(data); |
0f113f3e MC |
248 | } |
249 | ||
250 | if (!noout) { | |
251 | if (outformat == FORMAT_ASN1) | |
252 | i = i2d_DSAparams_bio(out, dsa); | |
7e1b7485 | 253 | else |
0f113f3e | 254 | i = PEM_write_bio_DSAparams(out, dsa); |
0f113f3e MC |
255 | if (!i) { |
256 | BIO_printf(bio_err, "unable to write DSA parameters\n"); | |
257 | ERR_print_errors(bio_err); | |
258 | goto end; | |
259 | } | |
260 | } | |
261 | if (genkey) { | |
262 | DSA *dsakey; | |
263 | ||
264 | assert(need_rand); | |
265 | if ((dsakey = DSAparams_dup(dsa)) == NULL) | |
266 | goto end; | |
0f113f3e MC |
267 | if (!DSA_generate_key(dsakey)) { |
268 | ERR_print_errors(bio_err); | |
269 | DSA_free(dsakey); | |
270 | goto end; | |
271 | } | |
3b061a00 | 272 | assert(private); |
0f113f3e MC |
273 | if (outformat == FORMAT_ASN1) |
274 | i = i2d_DSAPrivateKey_bio(out, dsakey); | |
7e1b7485 | 275 | else |
0f113f3e MC |
276 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
277 | NULL); | |
0f113f3e MC |
278 | DSA_free(dsakey); |
279 | } | |
280 | if (need_rand) | |
7e1b7485 | 281 | app_RAND_write_file(NULL); |
0f113f3e MC |
282 | ret = 0; |
283 | end: | |
23a1d5e9 | 284 | BN_GENCB_free(cb); |
ca3a82c3 RS |
285 | BIO_free(in); |
286 | BIO_free_all(out); | |
d6407083 | 287 | DSA_free(dsa); |
7e1b7485 | 288 | return (ret); |
0f113f3e | 289 | } |
d02b48c6 | 290 | |
6d23cf97 | 291 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
0f113f3e MC |
292 | { |
293 | char c = '*'; | |
294 | ||
295 | if (p == 0) | |
296 | c = '.'; | |
297 | if (p == 1) | |
298 | c = '+'; | |
299 | if (p == 2) | |
300 | c = '*'; | |
301 | if (p == 3) | |
302 | c = '\n'; | |
303 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); | |
304 | (void)BIO_flush(BN_GENCB_get_arg(cb)); | |
305 | # ifdef GENCB_TEST | |
306 | if (stop_keygen_flag) | |
307 | return 0; | |
308 | # endif | |
309 | return 1; | |
310 | } | |
f5d7a031 | 311 | #endif |