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