]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/dhparam.c
Big apps cleanup (option-parsing, etc)
[thirdparty/openssl.git] / apps / dhparam.c
CommitLineData
09483c58
DSH
1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
09483c58
DSH
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
09483c58
DSH
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
09483c58
DSH
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
09483c58
DSH
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
09483c58
DSH
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
09483c58
DSH
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
41918458
BM
57/* ====================================================================
58 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
0f113f3e 65 * notice, this list of conditions and the following disclaimer.
41918458
BM
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
09483c58 110
0f113f3e 111#include <openssl/opensslconf.h> /* for OPENSSL_NO_DH */
cf1b7d96 112#ifndef OPENSSL_NO_DH
0f113f3e
MC
113# include <stdio.h>
114# include <stdlib.h>
115# include <time.h>
116# include <string.h>
117# include "apps.h"
118# include <openssl/bio.h>
119# include <openssl/err.h>
120# include <openssl/bn.h>
121# include <openssl/dh.h>
122# include <openssl/x509.h>
123# include <openssl/pem.h>
124
125# ifndef OPENSSL_NO_DSA
126# include <openssl/dsa.h>
127# endif
41918458 128
0f113f3e 129# define DEFBITS 2048
09483c58 130
6d23cf97 131static int dh_cb(int p, int n, BN_GENCB *cb);
09483c58 132
7e1b7485
RS
133typedef enum OPTION_choice {
134 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
135 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
136 OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
137 OPT_RAND, OPT_DSAPARAM, OPT_C, OPT_2, OPT_5
138} OPTION_CHOICE;
139
140OPTIONS dhparam_options[] = {
141 {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"},
142 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
143 {"help", OPT_HELP, '-', "Display this summary"},
144 {"in", OPT_IN, '<', "Input file"},
145 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
146 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
147 {"out", OPT_OUT, '>', "Output file"},
148 {"check", OPT_CHECK, '-', "Check the DH parameters"},
149 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
150 {"noout", OPT_NOOUT, '-'},
151 {"rand", OPT_RAND, 's',
152 "Load the file(s) into the random number generator"},
153 {"C", OPT_C, '-', "Print C code"},
154 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
155 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
0f113f3e 156# ifndef OPENSSL_NO_ENGINE
7e1b7485 157 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
0f113f3e 158# endif
7e1b7485
RS
159# ifndef OPENSSL_NO_DSA
160 {"dsaparam", OPT_DSAPARAM, '-',
161 "Read or generate DSA parameters, convert to DH"},
0f113f3e 162# endif
7e1b7485
RS
163 {NULL}
164};
165
166int dhparam_main(int argc, char **argv)
167{
168 BIO *in = NULL, *out = NULL;
169 DH *dh = NULL;
170 char *engine = NULL, *infile = NULL, *outfile = NULL, *prog, *inrand =
171 NULL;
172 int dsaparam = 0, i, text = 0, C = 0, ret = 1, num = 0, g = 0;
173 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
174 OPTION_CHOICE o;
175
176 prog = opt_init(argc, argv, dhparam_options);
177 while ((o = opt_next()) != OPT_EOF) {
178 switch (o) {
179 case OPT_EOF:
180 case OPT_ERR:
181 opthelp:
182 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
183 goto end;
184 case OPT_HELP:
185 opt_help(dhparam_options);
186 ret = 0;
187 goto end;
188 case OPT_INFORM:
189 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
190 goto opthelp;
191 break;
192 case OPT_OUTFORM:
193 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
194 goto opthelp;
195 break;
196 case OPT_IN:
197 infile = opt_arg();
198 break;
199 case OPT_OUT:
200 outfile = opt_arg();
201 break;
202 case OPT_ENGINE:
203 engine = opt_arg();
204 break;
205 case OPT_CHECK:
0f113f3e 206 check = 1;
7e1b7485
RS
207 break;
208 case OPT_TEXT:
0f113f3e 209 text = 1;
7e1b7485
RS
210 break;
211 case OPT_DSAPARAM:
0f113f3e 212 dsaparam = 1;
7e1b7485
RS
213 break;
214 case OPT_C:
0f113f3e 215 C = 1;
7e1b7485
RS
216 break;
217 case OPT_2:
0f113f3e 218 g = 2;
7e1b7485
RS
219 break;
220 case OPT_5:
0f113f3e 221 g = 5;
7e1b7485
RS
222 break;
223 case OPT_NOOUT:
224 noout = 1;
225 break;
226 case OPT_RAND:
227 inrand = opt_arg();
228 break;
229 }
0f113f3e 230 }
7e1b7485
RS
231 argc = opt_num_rest();
232 argv = opt_rest();
0f113f3e 233
7e1b7485 234 if (argv[0] && (!opt_int(argv[0], &num) || num <= 0))
0f113f3e 235 goto end;
0f113f3e
MC
236
237# ifndef OPENSSL_NO_ENGINE
7e1b7485 238 setup_engine(engine, 0);
0f113f3e 239# endif
5270e702 240
0f113f3e
MC
241 if (g && !num)
242 num = DEFBITS;
243
244# ifndef OPENSSL_NO_DSA
7e1b7485
RS
245 if (dsaparam && g) {
246 BIO_printf(bio_err,
247 "generator may not be chosen for DSA parameters\n");
248 goto end;
0f113f3e 249 }
7e1b7485
RS
250# endif
251 /* DH parameters */
252 if (num && !g)
253 g = 2;
0f113f3e
MC
254
255 if (num) {
256
257 BN_GENCB *cb;
258 cb = BN_GENCB_new();
259 if (!cb) {
260 ERR_print_errors(bio_err);
261 goto end;
262 }
263
264 BN_GENCB_set(cb, dh_cb, bio_err);
7e1b7485 265 if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {
0f113f3e
MC
266 BIO_printf(bio_err,
267 "warning, not much extra random data, consider using the -rand option\n");
268 }
269 if (inrand != NULL)
270 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
271 app_RAND_load_files(inrand));
272
273# ifndef OPENSSL_NO_DSA
274 if (dsaparam) {
275 DSA *dsa = DSA_new();
276
277 BIO_printf(bio_err,
278 "Generating DSA parameters, %d bit long prime\n", num);
279 if (!dsa
280 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
281 cb)) {
d6407083 282 DSA_free(dsa);
0f113f3e
MC
283 BN_GENCB_free(cb);
284 ERR_print_errors(bio_err);
285 goto end;
286 }
287
288 dh = DSA_dup_DH(dsa);
289 DSA_free(dsa);
290 if (dh == NULL) {
291 BN_GENCB_free(cb);
292 ERR_print_errors(bio_err);
293 goto end;
294 }
295 } else
296# endif
297 {
298 dh = DH_new();
299 BIO_printf(bio_err,
300 "Generating DH parameters, %d bit long safe prime, generator %d\n",
301 num, g);
302 BIO_printf(bio_err, "This is going to take a long time\n");
303 if (!dh || !DH_generate_parameters_ex(dh, num, g, cb)) {
304 BN_GENCB_free(cb);
305 ERR_print_errors(bio_err);
306 goto end;
307 }
308 }
309
310 BN_GENCB_free(cb);
7e1b7485 311 app_RAND_write_file(NULL);
0f113f3e
MC
312 } else {
313
7e1b7485
RS
314 in = bio_open_default(infile, RB(informat));
315 if (in == NULL)
0f113f3e 316 goto end;
0f113f3e 317
0f113f3e
MC
318# ifndef OPENSSL_NO_DSA
319 if (dsaparam) {
320 DSA *dsa;
321
322 if (informat == FORMAT_ASN1)
323 dsa = d2i_DSAparams_bio(in, NULL);
324 else /* informat == FORMAT_PEM */
325 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
326
327 if (dsa == NULL) {
328 BIO_printf(bio_err, "unable to load DSA parameters\n");
329 ERR_print_errors(bio_err);
330 goto end;
331 }
332
333 dh = DSA_dup_DH(dsa);
334 DSA_free(dsa);
335 if (dh == NULL) {
336 ERR_print_errors(bio_err);
337 goto end;
338 }
339 } else
340# endif
341 {
342 if (informat == FORMAT_ASN1)
343 dh = d2i_DHparams_bio(in, NULL);
344 else /* informat == FORMAT_PEM */
345 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
346
347 if (dh == NULL) {
348 BIO_printf(bio_err, "unable to load DH parameters\n");
349 ERR_print_errors(bio_err);
350 goto end;
351 }
352 }
353
354 /* dh != NULL */
355 }
356
7e1b7485
RS
357 out = bio_open_default(outfile, "w");
358 if (out == NULL)
0f113f3e 359 goto end;
0f113f3e
MC
360
361 if (text) {
362 DHparams_print(out, dh);
363 }
364
365 if (check) {
366 if (!DH_check(dh, &i)) {
367 ERR_print_errors(bio_err);
368 goto end;
369 }
370 if (i & DH_CHECK_P_NOT_PRIME)
371 printf("p value is not prime\n");
372 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
373 printf("p value is not a safe prime\n");
374 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
375 printf("unable to check the generator value\n");
376 if (i & DH_NOT_SUITABLE_GENERATOR)
377 printf("the g value is not a generator\n");
378 if (i == 0)
379 printf("DH parameters appear to be ok.\n");
380 }
381 if (C) {
382 unsigned char *data;
7e1b7485 383 int len, bits;
0f113f3e
MC
384
385 len = BN_num_bytes(dh->p);
386 bits = BN_num_bits(dh->p);
387 data = (unsigned char *)OPENSSL_malloc(len);
388 if (data == NULL) {
389 perror("OPENSSL_malloc");
390 goto end;
391 }
7e1b7485
RS
392 BIO_printf(out, "#ifndef HEADER_DH_H\n"
393 "# include <openssl/dh.h>\n"
394 "#endif\n"
395 "\n");
396 BIO_printf(out, "DH *get_dh%d()\n{\n", bits);
397 print_bignum_var(out, dh->p, "dhp", bits, data);
398 print_bignum_var(out, dh->g, "dhg", bits, data);
399 BIO_printf(out, " DH *dh = DN_new();\n"
400 "\n"
401 " if (dh == NULL)\n"
402 " return NULL;\n");
403 BIO_printf(out, " dh->p = BN_bin2bn(dhp_%d, sizeof (dhp_%d), NULL);\n",
0f113f3e 404 bits, bits);
7e1b7485 405 BIO_printf(out, " dh->g = BN_bin2bn(dhg_%d, sizeof (dhg_%d), NULL);\n",
0f113f3e 406 bits, bits);
7e1b7485
RS
407 BIO_printf(out, " if (!dh->p || !dh->g) {\n"
408 " DH_free(dh);\n"
409 " return NULL;\n"
410 " }\n");
0f113f3e 411 if (dh->length)
7e1b7485
RS
412 BIO_printf(out,
413 " dh->length = %ld;\n", dh->length);
414 BIO_printf(out, " return dh;\n}\n");
0f113f3e
MC
415 OPENSSL_free(data);
416 }
417
418 if (!noout) {
419 if (outformat == FORMAT_ASN1)
420 i = i2d_DHparams_bio(out, dh);
7e1b7485
RS
421 else if (dh->q)
422 i = PEM_write_bio_DHxparams(out, dh);
423 else
424 i = PEM_write_bio_DHparams(out, dh);
0f113f3e
MC
425 if (!i) {
426 BIO_printf(bio_err, "unable to write DH parameters\n");
427 ERR_print_errors(bio_err);
428 goto end;
429 }
430 }
431 ret = 0;
432 end:
ca3a82c3
RS
433 BIO_free(in);
434 BIO_free_all(out);
d6407083 435 DH_free(dh);
7e1b7485 436 return (ret);
0f113f3e 437}
09483c58 438
6d23cf97 439static int dh_cb(int p, int n, BN_GENCB *cb)
0f113f3e
MC
440{
441 char c = '*';
442
443 if (p == 0)
444 c = '.';
445 if (p == 1)
446 c = '+';
447 if (p == 2)
448 c = '*';
449 if (p == 3)
450 c = '\n';
451 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
452 (void)BIO_flush(BN_GENCB_get_arg(cb));
453 return 1;
454}
455
456#else /* !OPENSSL_NO_DH */
d4f0339c
DSH
457
458# if PEDANTIC
0f113f3e 459static void *dummy = &dummy;
d4f0339c
DSH
460# endif
461
09483c58 462#endif