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