1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
5 | * |
---|
6 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
7 | * this file except in compliance with the License. You can obtain a copy |
---|
8 | * in the file LICENSE in the source distribution or at |
---|
9 | * https://www.openssl.org/source/license.html |
---|
10 | */ |
---|
11 | |
---|
12 | #include <openssl/opensslconf.h> |
---|
13 | #ifdef OPENSSL_NO_DH |
---|
14 | NON_EMPTY_TRANSLATION_UNIT |
---|
15 | #else |
---|
16 | |
---|
17 | # include <stdio.h> |
---|
18 | # include <stdlib.h> |
---|
19 | # include <time.h> |
---|
20 | # include <string.h> |
---|
21 | # include "apps.h" |
---|
22 | # include "progs.h" |
---|
23 | # include <openssl/bio.h> |
---|
24 | # include <openssl/err.h> |
---|
25 | # include <openssl/bn.h> |
---|
26 | # include <openssl/dh.h> |
---|
27 | # include <openssl/x509.h> |
---|
28 | # include <openssl/pem.h> |
---|
29 | |
---|
30 | # ifndef OPENSSL_NO_DSA |
---|
31 | # include <openssl/dsa.h> |
---|
32 | # endif |
---|
33 | |
---|
34 | # define DEFBITS 2048 |
---|
35 | |
---|
36 | static int dh_cb(int p, int n, BN_GENCB *cb); |
---|
37 | |
---|
38 | typedef enum OPTION_choice { |
---|
39 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
---|
40 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, |
---|
41 | OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT, |
---|
42 | OPT_DSAPARAM, OPT_C, OPT_2, OPT_5, |
---|
43 | OPT_R_ENUM |
---|
44 | } OPTION_CHOICE; |
---|
45 | |
---|
46 | const OPTIONS dhparam_options[] = { |
---|
47 | {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"}, |
---|
48 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, |
---|
49 | {"help", OPT_HELP, '-', "Display this summary"}, |
---|
50 | {"in", OPT_IN, '<', "Input file"}, |
---|
51 | {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"}, |
---|
52 | {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"}, |
---|
53 | {"out", OPT_OUT, '>', "Output file"}, |
---|
54 | {"check", OPT_CHECK, '-', "Check the DH parameters"}, |
---|
55 | {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"}, |
---|
56 | {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"}, |
---|
57 | OPT_R_OPTIONS, |
---|
58 | {"C", OPT_C, '-', "Print C code"}, |
---|
59 | {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"}, |
---|
60 | {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"}, |
---|
61 | # ifndef OPENSSL_NO_DSA |
---|
62 | {"dsaparam", OPT_DSAPARAM, '-', |
---|
63 | "Read or generate DSA parameters, convert to DH"}, |
---|
64 | # endif |
---|
65 | # ifndef OPENSSL_NO_ENGINE |
---|
66 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, |
---|
67 | # endif |
---|
68 | {NULL} |
---|
69 | }; |
---|
70 | |
---|
71 | int dhparam_main(int argc, char **argv) |
---|
72 | { |
---|
73 | BIO *in = NULL, *out = NULL; |
---|
74 | DH *dh = NULL; |
---|
75 | char *infile = NULL, *outfile = NULL, *prog; |
---|
76 | ENGINE *e = NULL; |
---|
77 | #ifndef OPENSSL_NO_DSA |
---|
78 | int dsaparam = 0; |
---|
79 | #endif |
---|
80 | int i, text = 0, C = 0, ret = 1, num = 0, g = 0; |
---|
81 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0; |
---|
82 | OPTION_CHOICE o; |
---|
83 | |
---|
84 | prog = opt_init(argc, argv, dhparam_options); |
---|
85 | while ((o = opt_next()) != OPT_EOF) { |
---|
86 | switch (o) { |
---|
87 | case OPT_EOF: |
---|
88 | case OPT_ERR: |
---|
89 | opthelp: |
---|
90 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
---|
91 | goto end; |
---|
92 | case OPT_HELP: |
---|
93 | opt_help(dhparam_options); |
---|
94 | ret = 0; |
---|
95 | goto end; |
---|
96 | case OPT_INFORM: |
---|
97 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
---|
98 | goto opthelp; |
---|
99 | break; |
---|
100 | case OPT_OUTFORM: |
---|
101 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
---|
102 | goto opthelp; |
---|
103 | break; |
---|
104 | case OPT_IN: |
---|
105 | infile = opt_arg(); |
---|
106 | break; |
---|
107 | case OPT_OUT: |
---|
108 | outfile = opt_arg(); |
---|
109 | break; |
---|
110 | case OPT_ENGINE: |
---|
111 | e = setup_engine(opt_arg(), 0); |
---|
112 | break; |
---|
113 | case OPT_CHECK: |
---|
114 | check = 1; |
---|
115 | break; |
---|
116 | case OPT_TEXT: |
---|
117 | text = 1; |
---|
118 | break; |
---|
119 | case OPT_DSAPARAM: |
---|
120 | #ifndef OPENSSL_NO_DSA |
---|
121 | dsaparam = 1; |
---|
122 | #endif |
---|
123 | break; |
---|
124 | case OPT_C: |
---|
125 | C = 1; |
---|
126 | break; |
---|
127 | case OPT_2: |
---|
128 | g = 2; |
---|
129 | break; |
---|
130 | case OPT_5: |
---|
131 | g = 5; |
---|
132 | break; |
---|
133 | case OPT_NOOUT: |
---|
134 | noout = 1; |
---|
135 | break; |
---|
136 | case OPT_R_CASES: |
---|
137 | if (!opt_rand(o)) |
---|
138 | goto end; |
---|
139 | break; |
---|
140 | } |
---|
141 | } |
---|
142 | argc = opt_num_rest(); |
---|
143 | argv = opt_rest(); |
---|
144 | |
---|
145 | if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0)) |
---|
146 | goto end; |
---|
147 | |
---|
148 | if (g && !num) |
---|
149 | num = DEFBITS; |
---|
150 | |
---|
151 | # ifndef OPENSSL_NO_DSA |
---|
152 | if (dsaparam && g) { |
---|
153 | BIO_printf(bio_err, |
---|
154 | "generator may not be chosen for DSA parameters\n"); |
---|
155 | goto end; |
---|
156 | } |
---|
157 | # endif |
---|
158 | |
---|
159 | out = bio_open_default(outfile, 'w', outformat); |
---|
160 | if (out == NULL) |
---|
161 | goto end; |
---|
162 | |
---|
163 | /* DH parameters */ |
---|
164 | if (num && !g) |
---|
165 | g = 2; |
---|
166 | |
---|
167 | if (num) { |
---|
168 | |
---|
169 | BN_GENCB *cb; |
---|
170 | cb = BN_GENCB_new(); |
---|
171 | if (cb == NULL) { |
---|
172 | ERR_print_errors(bio_err); |
---|
173 | goto end; |
---|
174 | } |
---|
175 | |
---|
176 | BN_GENCB_set(cb, dh_cb, bio_err); |
---|
177 | |
---|
178 | # ifndef OPENSSL_NO_DSA |
---|
179 | if (dsaparam) { |
---|
180 | DSA *dsa = DSA_new(); |
---|
181 | |
---|
182 | BIO_printf(bio_err, |
---|
183 | "Generating DSA parameters, %d bit long prime\n", num); |
---|
184 | if (dsa == NULL |
---|
185 | || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, |
---|
186 | cb)) { |
---|
187 | DSA_free(dsa); |
---|
188 | BN_GENCB_free(cb); |
---|
189 | ERR_print_errors(bio_err); |
---|
190 | goto end; |
---|
191 | } |
---|
192 | |
---|
193 | dh = DSA_dup_DH(dsa); |
---|
194 | DSA_free(dsa); |
---|
195 | if (dh == NULL) { |
---|
196 | BN_GENCB_free(cb); |
---|
197 | ERR_print_errors(bio_err); |
---|
198 | goto end; |
---|
199 | } |
---|
200 | } else |
---|
201 | # endif |
---|
202 | { |
---|
203 | dh = DH_new(); |
---|
204 | BIO_printf(bio_err, |
---|
205 | "Generating DH parameters, %d bit long safe prime, generator %d\n", |
---|
206 | num, g); |
---|
207 | BIO_printf(bio_err, "This is going to take a long time\n"); |
---|
208 | if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) { |
---|
209 | BN_GENCB_free(cb); |
---|
210 | ERR_print_errors(bio_err); |
---|
211 | goto end; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | BN_GENCB_free(cb); |
---|
216 | } else { |
---|
217 | |
---|
218 | in = bio_open_default(infile, 'r', informat); |
---|
219 | if (in == NULL) |
---|
220 | goto end; |
---|
221 | |
---|
222 | # ifndef OPENSSL_NO_DSA |
---|
223 | if (dsaparam) { |
---|
224 | DSA *dsa; |
---|
225 | |
---|
226 | if (informat == FORMAT_ASN1) |
---|
227 | dsa = d2i_DSAparams_bio(in, NULL); |
---|
228 | else /* informat == FORMAT_PEM */ |
---|
229 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
---|
230 | |
---|
231 | if (dsa == NULL) { |
---|
232 | BIO_printf(bio_err, "unable to load DSA parameters\n"); |
---|
233 | ERR_print_errors(bio_err); |
---|
234 | goto end; |
---|
235 | } |
---|
236 | |
---|
237 | dh = DSA_dup_DH(dsa); |
---|
238 | DSA_free(dsa); |
---|
239 | if (dh == NULL) { |
---|
240 | ERR_print_errors(bio_err); |
---|
241 | goto end; |
---|
242 | } |
---|
243 | } else |
---|
244 | # endif |
---|
245 | { |
---|
246 | if (informat == FORMAT_ASN1) { |
---|
247 | /* |
---|
248 | * We have no PEM header to determine what type of DH params it |
---|
249 | * is. We'll just try both. |
---|
250 | */ |
---|
251 | dh = d2i_DHparams_bio(in, NULL); |
---|
252 | /* BIO_reset() returns 0 for success for file BIOs only!!! */ |
---|
253 | if (dh == NULL && BIO_reset(in) == 0) |
---|
254 | dh = d2i_DHxparams_bio(in, NULL); |
---|
255 | } else { |
---|
256 | /* informat == FORMAT_PEM */ |
---|
257 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
---|
258 | } |
---|
259 | |
---|
260 | if (dh == NULL) { |
---|
261 | BIO_printf(bio_err, "unable to load DH parameters\n"); |
---|
262 | ERR_print_errors(bio_err); |
---|
263 | goto end; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /* dh != NULL */ |
---|
268 | } |
---|
269 | |
---|
270 | if (text) { |
---|
271 | DHparams_print(out, dh); |
---|
272 | } |
---|
273 | |
---|
274 | if (check) { |
---|
275 | if (!DH_check(dh, &i)) { |
---|
276 | ERR_print_errors(bio_err); |
---|
277 | goto end; |
---|
278 | } |
---|
279 | if (i & DH_CHECK_P_NOT_PRIME) |
---|
280 | BIO_printf(bio_err, "WARNING: p value is not prime\n"); |
---|
281 | if (i & DH_CHECK_P_NOT_SAFE_PRIME) |
---|
282 | BIO_printf(bio_err, "WARNING: p value is not a safe prime\n"); |
---|
283 | if (i & DH_CHECK_Q_NOT_PRIME) |
---|
284 | BIO_printf(bio_err, "WARNING: q value is not a prime\n"); |
---|
285 | if (i & DH_CHECK_INVALID_Q_VALUE) |
---|
286 | BIO_printf(bio_err, "WARNING: q value is invalid\n"); |
---|
287 | if (i & DH_CHECK_INVALID_J_VALUE) |
---|
288 | BIO_printf(bio_err, "WARNING: j value is invalid\n"); |
---|
289 | if (i & DH_UNABLE_TO_CHECK_GENERATOR) |
---|
290 | BIO_printf(bio_err, |
---|
291 | "WARNING: unable to check the generator value\n"); |
---|
292 | if (i & DH_NOT_SUITABLE_GENERATOR) |
---|
293 | BIO_printf(bio_err, "WARNING: the g value is not a generator\n"); |
---|
294 | if (i == 0) |
---|
295 | BIO_printf(bio_err, "DH parameters appear to be ok.\n"); |
---|
296 | if (num != 0 && i != 0) { |
---|
297 | /* |
---|
298 | * We have generated parameters but DH_check() indicates they are |
---|
299 | * invalid! This should never happen! |
---|
300 | */ |
---|
301 | BIO_printf(bio_err, "ERROR: Invalid parameters generated\n"); |
---|
302 | goto end; |
---|
303 | } |
---|
304 | } |
---|
305 | if (C) { |
---|
306 | unsigned char *data; |
---|
307 | int len, bits; |
---|
308 | const BIGNUM *pbn, *gbn; |
---|
309 | |
---|
310 | len = DH_size(dh); |
---|
311 | bits = DH_bits(dh); |
---|
312 | DH_get0_pqg(dh, &pbn, NULL, &gbn); |
---|
313 | data = app_malloc(len, "print a BN"); |
---|
314 | |
---|
315 | BIO_printf(out, "static DH *get_dh%d(void)\n{\n", bits); |
---|
316 | print_bignum_var(out, pbn, "dhp", bits, data); |
---|
317 | print_bignum_var(out, gbn, "dhg", bits, data); |
---|
318 | BIO_printf(out, " DH *dh = DH_new();\n" |
---|
319 | " BIGNUM *p, *g;\n" |
---|
320 | "\n" |
---|
321 | " if (dh == NULL)\n" |
---|
322 | " return NULL;\n"); |
---|
323 | BIO_printf(out, " p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n", |
---|
324 | bits, bits); |
---|
325 | BIO_printf(out, " g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n", |
---|
326 | bits, bits); |
---|
327 | BIO_printf(out, " if (p == NULL || g == NULL\n" |
---|
328 | " || !DH_set0_pqg(dh, p, NULL, g)) {\n" |
---|
329 | " DH_free(dh);\n" |
---|
330 | " BN_free(p);\n" |
---|
331 | " BN_free(g);\n" |
---|
332 | " return NULL;\n" |
---|
333 | " }\n"); |
---|
334 | if (DH_get_length(dh) > 0) |
---|
335 | BIO_printf(out, |
---|
336 | " if (!DH_set_length(dh, %ld)) {\n" |
---|
337 | " DH_free(dh);\n" |
---|
338 | " return NULL;\n" |
---|
339 | " }\n", DH_get_length(dh)); |
---|
340 | BIO_printf(out, " return dh;\n}\n"); |
---|
341 | OPENSSL_free(data); |
---|
342 | } |
---|
343 | |
---|
344 | if (!noout) { |
---|
345 | const BIGNUM *q; |
---|
346 | DH_get0_pqg(dh, NULL, &q, NULL); |
---|
347 | if (outformat == FORMAT_ASN1) { |
---|
348 | if (q != NULL) |
---|
349 | i = i2d_DHxparams_bio(out, dh); |
---|
350 | else |
---|
351 | i = i2d_DHparams_bio(out, dh); |
---|
352 | } else if (q != NULL) { |
---|
353 | i = PEM_write_bio_DHxparams(out, dh); |
---|
354 | } else { |
---|
355 | i = PEM_write_bio_DHparams(out, dh); |
---|
356 | } |
---|
357 | if (!i) { |
---|
358 | BIO_printf(bio_err, "unable to write DH parameters\n"); |
---|
359 | ERR_print_errors(bio_err); |
---|
360 | goto end; |
---|
361 | } |
---|
362 | } |
---|
363 | ret = 0; |
---|
364 | end: |
---|
365 | BIO_free(in); |
---|
366 | BIO_free_all(out); |
---|
367 | DH_free(dh); |
---|
368 | release_engine(e); |
---|
369 | return ret; |
---|
370 | } |
---|
371 | |
---|
372 | static int dh_cb(int p, int n, BN_GENCB *cb) |
---|
373 | { |
---|
374 | static const char symbols[] = ".+*\n"; |
---|
375 | char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?'; |
---|
376 | |
---|
377 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); |
---|
378 | (void)BIO_flush(BN_GENCB_get_arg(cb)); |
---|
379 | return 1; |
---|
380 | } |
---|
381 | #endif |
---|