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