1ebfedea0SLionel Sambuc /* apps/dhparam.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60ebfedea0SLionel Sambuc *
61ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc * are met:
64ebfedea0SLionel Sambuc *
65ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc *
68ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc * distribution.
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc * software must display the following acknowledgment:
75ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc *
78ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc * openssl-core@openssl.org.
82ebfedea0SLionel Sambuc *
83ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc *
87ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc * acknowledgment:
89ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc *
92ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc * ====================================================================
105ebfedea0SLionel Sambuc *
106ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
108ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc */
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc #include <openssl/opensslconf.h> /* for OPENSSL_NO_DH */
113ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
114ebfedea0SLionel Sambuc # include <stdio.h>
115ebfedea0SLionel Sambuc # include <stdlib.h>
116ebfedea0SLionel Sambuc # include <time.h>
117ebfedea0SLionel Sambuc # include <string.h>
118ebfedea0SLionel Sambuc # include "apps.h"
119ebfedea0SLionel Sambuc # include <openssl/bio.h>
120ebfedea0SLionel Sambuc # include <openssl/err.h>
121ebfedea0SLionel Sambuc # include <openssl/bn.h>
122ebfedea0SLionel Sambuc # include <openssl/dh.h>
123ebfedea0SLionel Sambuc # include <openssl/x509.h>
124ebfedea0SLionel Sambuc # include <openssl/pem.h>
125ebfedea0SLionel Sambuc
126ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
127ebfedea0SLionel Sambuc # include <openssl/dsa.h>
128ebfedea0SLionel Sambuc # endif
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc # undef PROG
131ebfedea0SLionel Sambuc # define PROG dhparam_main
132ebfedea0SLionel Sambuc
133*0a6a1f1dSLionel Sambuc # define DEFBITS 2048
134ebfedea0SLionel Sambuc
135*0a6a1f1dSLionel Sambuc /*-
136*0a6a1f1dSLionel Sambuc * -inform arg - input format - default PEM (DER or PEM)
137ebfedea0SLionel Sambuc * -outform arg - output format - default PEM
138ebfedea0SLionel Sambuc * -in arg - input file - default stdin
139ebfedea0SLionel Sambuc * -out arg - output file - default stdout
140ebfedea0SLionel Sambuc * -dsaparam - read or generate DSA parameters, convert to DH
141ebfedea0SLionel Sambuc * -check - check the parameters are ok
142ebfedea0SLionel Sambuc * -noout
143ebfedea0SLionel Sambuc * -text
144ebfedea0SLionel Sambuc * -C
145ebfedea0SLionel Sambuc */
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
148ebfedea0SLionel Sambuc
149ebfedea0SLionel Sambuc int MAIN(int, char **);
150ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)151ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
152ebfedea0SLionel Sambuc {
153ebfedea0SLionel Sambuc DH *dh = NULL;
154ebfedea0SLionel Sambuc int i, badops = 0, text = 0;
155ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
156ebfedea0SLionel Sambuc int dsaparam = 0;
157ebfedea0SLionel Sambuc # endif
158ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL;
159ebfedea0SLionel Sambuc int informat, outformat, check = 0, noout = 0, C = 0, ret = 1;
160ebfedea0SLionel Sambuc char *infile, *outfile, *prog;
161ebfedea0SLionel Sambuc char *inrand = NULL;
162ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
163ebfedea0SLionel Sambuc char *engine = NULL;
164ebfedea0SLionel Sambuc # endif
165ebfedea0SLionel Sambuc int num = 0, g = 0;
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc apps_startup();
168ebfedea0SLionel Sambuc
169ebfedea0SLionel Sambuc if (bio_err == NULL)
170ebfedea0SLionel Sambuc if ((bio_err = BIO_new(BIO_s_file())) != NULL)
171ebfedea0SLionel Sambuc BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
172ebfedea0SLionel Sambuc
173ebfedea0SLionel Sambuc if (!load_config(bio_err, NULL))
174ebfedea0SLionel Sambuc goto end;
175ebfedea0SLionel Sambuc
176ebfedea0SLionel Sambuc infile = NULL;
177ebfedea0SLionel Sambuc outfile = NULL;
178ebfedea0SLionel Sambuc informat = FORMAT_PEM;
179ebfedea0SLionel Sambuc outformat = FORMAT_PEM;
180ebfedea0SLionel Sambuc
181ebfedea0SLionel Sambuc prog = argv[0];
182ebfedea0SLionel Sambuc argc--;
183ebfedea0SLionel Sambuc argv++;
184*0a6a1f1dSLionel Sambuc while (argc >= 1) {
185*0a6a1f1dSLionel Sambuc if (strcmp(*argv, "-inform") == 0) {
186*0a6a1f1dSLionel Sambuc if (--argc < 1)
187*0a6a1f1dSLionel Sambuc goto bad;
188ebfedea0SLionel Sambuc informat = str2fmt(*(++argv));
189*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-outform") == 0) {
190*0a6a1f1dSLionel Sambuc if (--argc < 1)
191*0a6a1f1dSLionel Sambuc goto bad;
192ebfedea0SLionel Sambuc outformat = str2fmt(*(++argv));
193*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-in") == 0) {
194*0a6a1f1dSLionel Sambuc if (--argc < 1)
195*0a6a1f1dSLionel Sambuc goto bad;
196ebfedea0SLionel Sambuc infile = *(++argv);
197*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-out") == 0) {
198*0a6a1f1dSLionel Sambuc if (--argc < 1)
199*0a6a1f1dSLionel Sambuc goto bad;
200ebfedea0SLionel Sambuc outfile = *(++argv);
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
203*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-engine") == 0) {
204*0a6a1f1dSLionel Sambuc if (--argc < 1)
205*0a6a1f1dSLionel Sambuc goto bad;
206ebfedea0SLionel Sambuc engine = *(++argv);
207ebfedea0SLionel Sambuc }
208ebfedea0SLionel Sambuc # endif
209ebfedea0SLionel Sambuc else if (strcmp(*argv, "-check") == 0)
210ebfedea0SLionel Sambuc check = 1;
211ebfedea0SLionel Sambuc else if (strcmp(*argv, "-text") == 0)
212ebfedea0SLionel Sambuc text = 1;
213ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
214ebfedea0SLionel Sambuc else if (strcmp(*argv, "-dsaparam") == 0)
215ebfedea0SLionel Sambuc dsaparam = 1;
216ebfedea0SLionel Sambuc # endif
217ebfedea0SLionel Sambuc else if (strcmp(*argv, "-C") == 0)
218ebfedea0SLionel Sambuc C = 1;
219ebfedea0SLionel Sambuc else if (strcmp(*argv, "-noout") == 0)
220ebfedea0SLionel Sambuc noout = 1;
221ebfedea0SLionel Sambuc else if (strcmp(*argv, "-2") == 0)
222ebfedea0SLionel Sambuc g = 2;
223ebfedea0SLionel Sambuc else if (strcmp(*argv, "-5") == 0)
224ebfedea0SLionel Sambuc g = 5;
225*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-rand") == 0) {
226*0a6a1f1dSLionel Sambuc if (--argc < 1)
227*0a6a1f1dSLionel Sambuc goto bad;
228ebfedea0SLionel Sambuc inrand = *(++argv);
229*0a6a1f1dSLionel Sambuc } else if (((sscanf(*argv, "%d", &num) == 0) || (num <= 0)))
230ebfedea0SLionel Sambuc goto bad;
231ebfedea0SLionel Sambuc argv++;
232ebfedea0SLionel Sambuc argc--;
233ebfedea0SLionel Sambuc }
234ebfedea0SLionel Sambuc
235*0a6a1f1dSLionel Sambuc if (badops) {
236ebfedea0SLionel Sambuc bad:
237ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s [options] [numbits]\n", prog);
238ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options are\n");
239ebfedea0SLionel Sambuc BIO_printf(bio_err, " -inform arg input format - one of DER PEM\n");
240*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
241*0a6a1f1dSLionel Sambuc " -outform arg output format - one of DER PEM\n");
242ebfedea0SLionel Sambuc BIO_printf(bio_err, " -in arg input file\n");
243ebfedea0SLionel Sambuc BIO_printf(bio_err, " -out arg output file\n");
244ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
245*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
246*0a6a1f1dSLionel Sambuc " -dsaparam read or generate DSA parameters, convert to DH\n");
247ebfedea0SLionel Sambuc # endif
248ebfedea0SLionel Sambuc BIO_printf(bio_err, " -check check the DH parameters\n");
249*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
250*0a6a1f1dSLionel Sambuc " -text print a text form of the DH parameters\n");
251ebfedea0SLionel Sambuc BIO_printf(bio_err, " -C Output C code\n");
252*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
253*0a6a1f1dSLionel Sambuc " -2 generate parameters using 2 as the generator value\n");
254*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
255*0a6a1f1dSLionel Sambuc " -5 generate parameters using 5 as the generator value\n");
256*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
257*0a6a1f1dSLionel Sambuc " numbits number of bits in to generate (default 2048)\n");
258ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
259*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
260*0a6a1f1dSLionel Sambuc " -engine e use engine e, possibly a hardware device.\n");
261ebfedea0SLionel Sambuc # endif
262*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
263*0a6a1f1dSLionel Sambuc LIST_SEPARATOR_CHAR);
264*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
265*0a6a1f1dSLionel Sambuc " - load the file (or the files in the directory) into\n");
266ebfedea0SLionel Sambuc BIO_printf(bio_err, " the random number generator\n");
267ebfedea0SLionel Sambuc BIO_printf(bio_err, " -noout no output\n");
268ebfedea0SLionel Sambuc goto end;
269ebfedea0SLionel Sambuc }
270ebfedea0SLionel Sambuc
271ebfedea0SLionel Sambuc ERR_load_crypto_strings();
272ebfedea0SLionel Sambuc
273ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
274ebfedea0SLionel Sambuc setup_engine(bio_err, engine, 0);
275ebfedea0SLionel Sambuc # endif
276ebfedea0SLionel Sambuc
277ebfedea0SLionel Sambuc if (g && !num)
278ebfedea0SLionel Sambuc num = DEFBITS;
279ebfedea0SLionel Sambuc
280ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
281*0a6a1f1dSLionel Sambuc if (dsaparam) {
282*0a6a1f1dSLionel Sambuc if (g) {
283*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
284*0a6a1f1dSLionel Sambuc "generator may not be chosen for DSA parameters\n");
285ebfedea0SLionel Sambuc goto end;
286ebfedea0SLionel Sambuc }
287*0a6a1f1dSLionel Sambuc } else
288ebfedea0SLionel Sambuc # endif
289ebfedea0SLionel Sambuc {
290ebfedea0SLionel Sambuc /* DH parameters */
291ebfedea0SLionel Sambuc if (num && !g)
292ebfedea0SLionel Sambuc g = 2;
293ebfedea0SLionel Sambuc }
294ebfedea0SLionel Sambuc
295ebfedea0SLionel Sambuc if (num) {
296ebfedea0SLionel Sambuc
297ebfedea0SLionel Sambuc BN_GENCB cb;
298ebfedea0SLionel Sambuc BN_GENCB_set(&cb, dh_cb, bio_err);
299*0a6a1f1dSLionel Sambuc if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) {
300*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
301*0a6a1f1dSLionel Sambuc "warning, not much extra random data, consider using the -rand option\n");
302ebfedea0SLionel Sambuc }
303ebfedea0SLionel Sambuc if (inrand != NULL)
304ebfedea0SLionel Sambuc BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
305ebfedea0SLionel Sambuc app_RAND_load_files(inrand));
306ebfedea0SLionel Sambuc
307ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
308*0a6a1f1dSLionel Sambuc if (dsaparam) {
309ebfedea0SLionel Sambuc DSA *dsa = DSA_new();
310ebfedea0SLionel Sambuc
311*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
312*0a6a1f1dSLionel Sambuc "Generating DSA parameters, %d bit long prime\n", num);
313*0a6a1f1dSLionel Sambuc if (!dsa
314*0a6a1f1dSLionel Sambuc || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
315*0a6a1f1dSLionel Sambuc &cb)) {
316*0a6a1f1dSLionel Sambuc if (dsa)
317*0a6a1f1dSLionel Sambuc DSA_free(dsa);
318ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
319ebfedea0SLionel Sambuc goto end;
320ebfedea0SLionel Sambuc }
321ebfedea0SLionel Sambuc
322ebfedea0SLionel Sambuc dh = DSA_dup_DH(dsa);
323ebfedea0SLionel Sambuc DSA_free(dsa);
324*0a6a1f1dSLionel Sambuc if (dh == NULL) {
325ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
326ebfedea0SLionel Sambuc goto end;
327ebfedea0SLionel Sambuc }
328*0a6a1f1dSLionel Sambuc } else
329ebfedea0SLionel Sambuc # endif
330ebfedea0SLionel Sambuc {
331ebfedea0SLionel Sambuc dh = DH_new();
332*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
333*0a6a1f1dSLionel Sambuc "Generating DH parameters, %d bit long safe prime, generator %d\n",
334*0a6a1f1dSLionel Sambuc num, g);
335ebfedea0SLionel Sambuc BIO_printf(bio_err, "This is going to take a long time\n");
336*0a6a1f1dSLionel Sambuc if (!dh || !DH_generate_parameters_ex(dh, num, g, &cb)) {
337ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
338ebfedea0SLionel Sambuc goto end;
339ebfedea0SLionel Sambuc }
340ebfedea0SLionel Sambuc }
341ebfedea0SLionel Sambuc
342ebfedea0SLionel Sambuc app_RAND_write_file(NULL, bio_err);
343ebfedea0SLionel Sambuc } else {
344ebfedea0SLionel Sambuc
345ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file());
346*0a6a1f1dSLionel Sambuc if (in == NULL) {
347ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
348ebfedea0SLionel Sambuc goto end;
349ebfedea0SLionel Sambuc }
350ebfedea0SLionel Sambuc if (infile == NULL)
351ebfedea0SLionel Sambuc BIO_set_fp(in, stdin, BIO_NOCLOSE);
352*0a6a1f1dSLionel Sambuc else {
353*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, infile) <= 0) {
354ebfedea0SLionel Sambuc perror(infile);
355ebfedea0SLionel Sambuc goto end;
356ebfedea0SLionel Sambuc }
357ebfedea0SLionel Sambuc }
358ebfedea0SLionel Sambuc
359*0a6a1f1dSLionel Sambuc if (informat != FORMAT_ASN1 && informat != FORMAT_PEM) {
360ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad input format specified\n");
361ebfedea0SLionel Sambuc goto end;
362ebfedea0SLionel Sambuc }
363ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
364*0a6a1f1dSLionel Sambuc if (dsaparam) {
365ebfedea0SLionel Sambuc DSA *dsa;
366ebfedea0SLionel Sambuc
367ebfedea0SLionel Sambuc if (informat == FORMAT_ASN1)
368ebfedea0SLionel Sambuc dsa = d2i_DSAparams_bio(in, NULL);
369ebfedea0SLionel Sambuc else /* informat == FORMAT_PEM */
370ebfedea0SLionel Sambuc dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
371ebfedea0SLionel Sambuc
372*0a6a1f1dSLionel Sambuc if (dsa == NULL) {
373ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to load DSA parameters\n");
374ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
375ebfedea0SLionel Sambuc goto end;
376ebfedea0SLionel Sambuc }
377ebfedea0SLionel Sambuc
378ebfedea0SLionel Sambuc dh = DSA_dup_DH(dsa);
379ebfedea0SLionel Sambuc DSA_free(dsa);
380*0a6a1f1dSLionel Sambuc if (dh == NULL) {
381ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
382ebfedea0SLionel Sambuc goto end;
383ebfedea0SLionel Sambuc }
384*0a6a1f1dSLionel Sambuc } else
385ebfedea0SLionel Sambuc # endif
386ebfedea0SLionel Sambuc {
387ebfedea0SLionel Sambuc if (informat == FORMAT_ASN1)
388ebfedea0SLionel Sambuc dh = d2i_DHparams_bio(in, NULL);
389ebfedea0SLionel Sambuc else /* informat == FORMAT_PEM */
390ebfedea0SLionel Sambuc dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
391ebfedea0SLionel Sambuc
392*0a6a1f1dSLionel Sambuc if (dh == NULL) {
393ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to load DH parameters\n");
394ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
395ebfedea0SLionel Sambuc goto end;
396ebfedea0SLionel Sambuc }
397ebfedea0SLionel Sambuc }
398ebfedea0SLionel Sambuc
399ebfedea0SLionel Sambuc /* dh != NULL */
400ebfedea0SLionel Sambuc }
401ebfedea0SLionel Sambuc
402ebfedea0SLionel Sambuc out = BIO_new(BIO_s_file());
403*0a6a1f1dSLionel Sambuc if (out == NULL) {
404ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
405ebfedea0SLionel Sambuc goto end;
406ebfedea0SLionel Sambuc }
407*0a6a1f1dSLionel Sambuc if (outfile == NULL) {
408ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE);
409ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
410ebfedea0SLionel Sambuc {
411ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
412ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
413ebfedea0SLionel Sambuc }
414ebfedea0SLionel Sambuc # endif
415*0a6a1f1dSLionel Sambuc } else {
416*0a6a1f1dSLionel Sambuc if (BIO_write_filename(out, outfile) <= 0) {
417ebfedea0SLionel Sambuc perror(outfile);
418ebfedea0SLionel Sambuc goto end;
419ebfedea0SLionel Sambuc }
420ebfedea0SLionel Sambuc }
421ebfedea0SLionel Sambuc
422*0a6a1f1dSLionel Sambuc if (text) {
423ebfedea0SLionel Sambuc DHparams_print(out, dh);
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc
426*0a6a1f1dSLionel Sambuc if (check) {
427*0a6a1f1dSLionel Sambuc if (!DH_check(dh, &i)) {
428ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
429ebfedea0SLionel Sambuc goto end;
430ebfedea0SLionel Sambuc }
431ebfedea0SLionel Sambuc if (i & DH_CHECK_P_NOT_PRIME)
432ebfedea0SLionel Sambuc printf("p value is not prime\n");
433ebfedea0SLionel Sambuc if (i & DH_CHECK_P_NOT_SAFE_PRIME)
434ebfedea0SLionel Sambuc printf("p value is not a safe prime\n");
435ebfedea0SLionel Sambuc if (i & DH_UNABLE_TO_CHECK_GENERATOR)
436ebfedea0SLionel Sambuc printf("unable to check the generator value\n");
437ebfedea0SLionel Sambuc if (i & DH_NOT_SUITABLE_GENERATOR)
438ebfedea0SLionel Sambuc printf("the g value is not a generator\n");
439ebfedea0SLionel Sambuc if (i == 0)
440ebfedea0SLionel Sambuc printf("DH parameters appear to be ok.\n");
441ebfedea0SLionel Sambuc }
442*0a6a1f1dSLionel Sambuc if (C) {
443ebfedea0SLionel Sambuc unsigned char *data;
444ebfedea0SLionel Sambuc int len, l, bits;
445ebfedea0SLionel Sambuc
446ebfedea0SLionel Sambuc len = BN_num_bytes(dh->p);
447ebfedea0SLionel Sambuc bits = BN_num_bits(dh->p);
448ebfedea0SLionel Sambuc data = (unsigned char *)OPENSSL_malloc(len);
449*0a6a1f1dSLionel Sambuc if (data == NULL) {
450ebfedea0SLionel Sambuc perror("OPENSSL_malloc");
451ebfedea0SLionel Sambuc goto end;
452ebfedea0SLionel Sambuc }
453ebfedea0SLionel Sambuc printf("#ifndef HEADER_DH_H\n"
454*0a6a1f1dSLionel Sambuc "#include <openssl/dh.h>\n" "#endif\n");
455ebfedea0SLionel Sambuc printf("DH *get_dh%d()\n\t{\n", bits);
456ebfedea0SLionel Sambuc
457ebfedea0SLionel Sambuc l = BN_bn2bin(dh->p, data);
458ebfedea0SLionel Sambuc printf("\tstatic unsigned char dh%d_p[]={", bits);
459*0a6a1f1dSLionel Sambuc for (i = 0; i < l; i++) {
460*0a6a1f1dSLionel Sambuc if ((i % 12) == 0)
461*0a6a1f1dSLionel Sambuc printf("\n\t\t");
462ebfedea0SLionel Sambuc printf("0x%02X,", data[i]);
463ebfedea0SLionel Sambuc }
464ebfedea0SLionel Sambuc printf("\n\t\t};\n");
465ebfedea0SLionel Sambuc
466ebfedea0SLionel Sambuc l = BN_bn2bin(dh->g, data);
467ebfedea0SLionel Sambuc printf("\tstatic unsigned char dh%d_g[]={", bits);
468*0a6a1f1dSLionel Sambuc for (i = 0; i < l; i++) {
469*0a6a1f1dSLionel Sambuc if ((i % 12) == 0)
470*0a6a1f1dSLionel Sambuc printf("\n\t\t");
471ebfedea0SLionel Sambuc printf("0x%02X,", data[i]);
472ebfedea0SLionel Sambuc }
473ebfedea0SLionel Sambuc printf("\n\t\t};\n");
474ebfedea0SLionel Sambuc
475ebfedea0SLionel Sambuc printf("\tDH *dh;\n\n");
476ebfedea0SLionel Sambuc printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
477ebfedea0SLionel Sambuc printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
478ebfedea0SLionel Sambuc bits, bits);
479ebfedea0SLionel Sambuc printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
480ebfedea0SLionel Sambuc bits, bits);
481ebfedea0SLionel Sambuc printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
482ebfedea0SLionel Sambuc printf("\t\t{ DH_free(dh); return(NULL); }\n");
483ebfedea0SLionel Sambuc if (dh->length)
484ebfedea0SLionel Sambuc printf("\tdh->length = %ld;\n", dh->length);
485ebfedea0SLionel Sambuc printf("\treturn(dh);\n\t}\n");
486ebfedea0SLionel Sambuc OPENSSL_free(data);
487ebfedea0SLionel Sambuc }
488ebfedea0SLionel Sambuc
489*0a6a1f1dSLionel Sambuc if (!noout) {
490ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1)
491ebfedea0SLionel Sambuc i = i2d_DHparams_bio(out, dh);
492ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM)
493ebfedea0SLionel Sambuc i = PEM_write_bio_DHparams(out, dh);
494ebfedea0SLionel Sambuc else {
495ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad output format specified for outfile\n");
496ebfedea0SLionel Sambuc goto end;
497ebfedea0SLionel Sambuc }
498*0a6a1f1dSLionel Sambuc if (!i) {
499ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to write DH parameters\n");
500ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
501ebfedea0SLionel Sambuc goto end;
502ebfedea0SLionel Sambuc }
503ebfedea0SLionel Sambuc }
504ebfedea0SLionel Sambuc ret = 0;
505ebfedea0SLionel Sambuc end:
506*0a6a1f1dSLionel Sambuc if (in != NULL)
507*0a6a1f1dSLionel Sambuc BIO_free(in);
508*0a6a1f1dSLionel Sambuc if (out != NULL)
509*0a6a1f1dSLionel Sambuc BIO_free_all(out);
510*0a6a1f1dSLionel Sambuc if (dh != NULL)
511*0a6a1f1dSLionel Sambuc DH_free(dh);
512ebfedea0SLionel Sambuc apps_shutdown();
513ebfedea0SLionel Sambuc OPENSSL_EXIT(ret);
514ebfedea0SLionel Sambuc }
515ebfedea0SLionel Sambuc
516ebfedea0SLionel Sambuc /* dh_cb is identical to dsa_cb in apps/dsaparam.c */
dh_cb(int p,int n,BN_GENCB * cb)517ebfedea0SLionel Sambuc static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
518ebfedea0SLionel Sambuc {
519ebfedea0SLionel Sambuc char c = '*';
520ebfedea0SLionel Sambuc
521*0a6a1f1dSLionel Sambuc if (p == 0)
522*0a6a1f1dSLionel Sambuc c = '.';
523*0a6a1f1dSLionel Sambuc if (p == 1)
524*0a6a1f1dSLionel Sambuc c = '+';
525*0a6a1f1dSLionel Sambuc if (p == 2)
526*0a6a1f1dSLionel Sambuc c = '*';
527*0a6a1f1dSLionel Sambuc if (p == 3)
528*0a6a1f1dSLionel Sambuc c = '\n';
529ebfedea0SLionel Sambuc BIO_write(cb->arg, &c, 1);
530ebfedea0SLionel Sambuc (void)BIO_flush(cb->arg);
531ebfedea0SLionel Sambuc # ifdef LINT
532ebfedea0SLionel Sambuc p = n;
533ebfedea0SLionel Sambuc # endif
534ebfedea0SLionel Sambuc return 1;
535ebfedea0SLionel Sambuc }
536ebfedea0SLionel Sambuc
537ebfedea0SLionel Sambuc #else /* !OPENSSL_NO_DH */
538ebfedea0SLionel Sambuc
539ebfedea0SLionel Sambuc # if PEDANTIC
540ebfedea0SLionel Sambuc static void *dummy = &dummy;
541ebfedea0SLionel Sambuc # endif
542ebfedea0SLionel Sambuc
543ebfedea0SLionel Sambuc #endif
544