xref: /minix3/crypto/external/bsd/openssl/dist/apps/gendh.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/gendh.c */
2ebfedea0SLionel Sambuc /* obsoleted by dhparam.c */
3ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4ebfedea0SLionel Sambuc  * All rights reserved.
5ebfedea0SLionel Sambuc  *
6ebfedea0SLionel Sambuc  * This package is an SSL implementation written
7ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
8ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
11ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
12ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
13ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
14ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
15ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
18ebfedea0SLionel Sambuc  * the code are not to be removed.
19ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
20ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
21ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
22ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
23ebfedea0SLionel Sambuc  *
24ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
25ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
26ebfedea0SLionel Sambuc  * are met:
27ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
28ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
29ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
30ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
31ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
32ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
33ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
34ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
35ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
36ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
37ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
38ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
39ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
40ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41ebfedea0SLionel Sambuc  *
42ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
43ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52ebfedea0SLionel Sambuc  * SUCH DAMAGE.
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
55ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
56ebfedea0SLionel Sambuc  * copied and put under another distribution licence
57ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #include <openssl/opensslconf.h>
61*0a6a1f1dSLionel Sambuc /*
62*0a6a1f1dSLionel Sambuc  * Until the key-gen callbacks are modified to use newer prototypes, we allow
63*0a6a1f1dSLionel Sambuc  * deprecated functions for openssl-internal code
64*0a6a1f1dSLionel Sambuc  */
65ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DEPRECATED
66ebfedea0SLionel Sambuc # undef OPENSSL_NO_DEPRECATED
67ebfedea0SLionel Sambuc #endif
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
70ebfedea0SLionel Sambuc # include <stdio.h>
71ebfedea0SLionel Sambuc # include <string.h>
72ebfedea0SLionel Sambuc # include <sys/types.h>
73ebfedea0SLionel Sambuc # include <sys/stat.h>
74ebfedea0SLionel Sambuc # include "apps.h"
75ebfedea0SLionel Sambuc # include <openssl/bio.h>
76ebfedea0SLionel Sambuc # include <openssl/rand.h>
77ebfedea0SLionel Sambuc # include <openssl/err.h>
78ebfedea0SLionel Sambuc # include <openssl/bn.h>
79ebfedea0SLionel Sambuc # include <openssl/dh.h>
80ebfedea0SLionel Sambuc # include <openssl/x509.h>
81ebfedea0SLionel Sambuc # include <openssl/pem.h>
82ebfedea0SLionel Sambuc 
83*0a6a1f1dSLionel Sambuc # define DEFBITS 2048
84ebfedea0SLionel Sambuc # undef PROG
85ebfedea0SLionel Sambuc # define PROG gendh_main
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc int MAIN(int, char **);
90ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)91ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
92ebfedea0SLionel Sambuc {
93ebfedea0SLionel Sambuc     BN_GENCB cb;
94ebfedea0SLionel Sambuc     DH *dh = NULL;
95ebfedea0SLionel Sambuc     int ret = 1, num = DEFBITS;
96ebfedea0SLionel Sambuc     int g = 2;
97ebfedea0SLionel Sambuc     char *outfile = NULL;
98ebfedea0SLionel Sambuc     char *inrand = NULL;
99ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
100ebfedea0SLionel Sambuc     char *engine = NULL;
101ebfedea0SLionel Sambuc # endif
102ebfedea0SLionel Sambuc     BIO *out = NULL;
103ebfedea0SLionel Sambuc 
104ebfedea0SLionel Sambuc     apps_startup();
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc     BN_GENCB_set(&cb, dh_cb, bio_err);
107ebfedea0SLionel Sambuc     if (bio_err == NULL)
108ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
109ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
112ebfedea0SLionel Sambuc         goto end;
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc     argv++;
115ebfedea0SLionel Sambuc     argc--;
116*0a6a1f1dSLionel Sambuc     for (;;) {
117*0a6a1f1dSLionel Sambuc         if (argc <= 0)
118*0a6a1f1dSLionel Sambuc             break;
119*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-out") == 0) {
120*0a6a1f1dSLionel Sambuc             if (--argc < 1)
121*0a6a1f1dSLionel Sambuc                 goto bad;
122ebfedea0SLionel Sambuc             outfile = *(++argv);
123*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-2") == 0)
124ebfedea0SLionel Sambuc             g = 2;
125*0a6a1f1dSLionel Sambuc /*-     else if (strcmp(*argv,"-3") == 0)
126ebfedea0SLionel Sambuc                 g=3; */
127ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-5") == 0)
128ebfedea0SLionel Sambuc             g = 5;
129ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
130*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-engine") == 0) {
131*0a6a1f1dSLionel Sambuc             if (--argc < 1)
132*0a6a1f1dSLionel Sambuc                 goto bad;
133ebfedea0SLionel Sambuc             engine = *(++argv);
134ebfedea0SLionel Sambuc         }
135ebfedea0SLionel Sambuc # endif
136*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-rand") == 0) {
137*0a6a1f1dSLionel Sambuc             if (--argc < 1)
138*0a6a1f1dSLionel Sambuc                 goto bad;
139ebfedea0SLionel Sambuc             inrand = *(++argv);
140*0a6a1f1dSLionel Sambuc         } else
141ebfedea0SLionel Sambuc             break;
142ebfedea0SLionel Sambuc         argv++;
143ebfedea0SLionel Sambuc         argc--;
144ebfedea0SLionel Sambuc     }
145*0a6a1f1dSLionel Sambuc     if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) {
146ebfedea0SLionel Sambuc  bad:
147ebfedea0SLionel Sambuc         BIO_printf(bio_err, "usage: gendh [args] [numbits]\n");
148ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -out file - output the key to 'file\n");
149ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -2        - use 2 as the generator value\n");
150*0a6a1f1dSLionel Sambuc         /*
151*0a6a1f1dSLionel Sambuc          * BIO_printf(bio_err," -3 - use 3 as the generator value\n");
152*0a6a1f1dSLionel Sambuc          */
153ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -5        - use 5 as the generator value\n");
154ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
155*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
156*0a6a1f1dSLionel Sambuc                    " -engine e - use engine e, possibly a hardware device.\n");
157ebfedea0SLionel Sambuc # endif
158*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
159*0a6a1f1dSLionel Sambuc                    LIST_SEPARATOR_CHAR);
160*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
161*0a6a1f1dSLionel Sambuc                    "           - load the file (or the files in the directory) into\n");
162ebfedea0SLionel Sambuc         BIO_printf(bio_err, "             the random number generator\n");
163ebfedea0SLionel Sambuc         goto end;
164ebfedea0SLionel Sambuc     }
165ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
166ebfedea0SLionel Sambuc     setup_engine(bio_err, engine, 0);
167ebfedea0SLionel Sambuc # endif
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
170*0a6a1f1dSLionel Sambuc     if (out == NULL) {
171ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
172ebfedea0SLionel Sambuc         goto end;
173ebfedea0SLionel Sambuc     }
174ebfedea0SLionel Sambuc 
175*0a6a1f1dSLionel Sambuc     if (outfile == NULL) {
176ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
177ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
178ebfedea0SLionel Sambuc         {
179ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
180ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
181ebfedea0SLionel Sambuc         }
182ebfedea0SLionel Sambuc # endif
183*0a6a1f1dSLionel Sambuc     } else {
184*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outfile) <= 0) {
185ebfedea0SLionel Sambuc             perror(outfile);
186ebfedea0SLionel Sambuc             goto end;
187ebfedea0SLionel Sambuc         }
188ebfedea0SLionel Sambuc     }
189ebfedea0SLionel Sambuc 
190*0a6a1f1dSLionel Sambuc     if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) {
191*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
192*0a6a1f1dSLionel Sambuc                    "warning, not much extra random data, consider using the -rand option\n");
193ebfedea0SLionel Sambuc     }
194ebfedea0SLionel Sambuc     if (inrand != NULL)
195ebfedea0SLionel Sambuc         BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
196ebfedea0SLionel Sambuc                    app_RAND_load_files(inrand));
197ebfedea0SLionel Sambuc 
198*0a6a1f1dSLionel Sambuc     BIO_printf(bio_err,
199*0a6a1f1dSLionel Sambuc                "Generating DH parameters, %d bit long safe prime, generator %d\n",
200*0a6a1f1dSLionel Sambuc                num, g);
201ebfedea0SLionel Sambuc     BIO_printf(bio_err, "This is going to take a long time\n");
202ebfedea0SLionel Sambuc 
203*0a6a1f1dSLionel Sambuc     if (((dh = DH_new()) == NULL)
204*0a6a1f1dSLionel Sambuc         || !DH_generate_parameters_ex(dh, num, g, &cb))
205ebfedea0SLionel Sambuc         goto end;
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc     app_RAND_write_file(NULL, bio_err);
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     if (!PEM_write_bio_DHparams(out, dh))
210ebfedea0SLionel Sambuc         goto end;
211ebfedea0SLionel Sambuc     ret = 0;
212ebfedea0SLionel Sambuc  end:
213ebfedea0SLionel Sambuc     if (ret != 0)
214ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
215*0a6a1f1dSLionel Sambuc     if (out != NULL)
216*0a6a1f1dSLionel Sambuc         BIO_free_all(out);
217*0a6a1f1dSLionel Sambuc     if (dh != NULL)
218*0a6a1f1dSLionel Sambuc         DH_free(dh);
219ebfedea0SLionel Sambuc     apps_shutdown();
220ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
221ebfedea0SLionel Sambuc }
222ebfedea0SLionel Sambuc 
dh_cb(int p,int n,BN_GENCB * cb)223ebfedea0SLionel Sambuc static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
224ebfedea0SLionel Sambuc {
225ebfedea0SLionel Sambuc     char c = '*';
226ebfedea0SLionel Sambuc 
227*0a6a1f1dSLionel Sambuc     if (p == 0)
228*0a6a1f1dSLionel Sambuc         c = '.';
229*0a6a1f1dSLionel Sambuc     if (p == 1)
230*0a6a1f1dSLionel Sambuc         c = '+';
231*0a6a1f1dSLionel Sambuc     if (p == 2)
232*0a6a1f1dSLionel Sambuc         c = '*';
233*0a6a1f1dSLionel Sambuc     if (p == 3)
234*0a6a1f1dSLionel Sambuc         c = '\n';
235ebfedea0SLionel Sambuc     BIO_write(cb->arg, &c, 1);
236ebfedea0SLionel Sambuc     (void)BIO_flush(cb->arg);
237ebfedea0SLionel Sambuc # ifdef LINT
238ebfedea0SLionel Sambuc     p = n;
239ebfedea0SLionel Sambuc # endif
240ebfedea0SLionel Sambuc     return 1;
241ebfedea0SLionel Sambuc }
242ebfedea0SLionel Sambuc #else                           /* !OPENSSL_NO_DH */
243ebfedea0SLionel Sambuc 
244ebfedea0SLionel Sambuc # if PEDANTIC
245ebfedea0SLionel Sambuc static void *dummy = &dummy;
246ebfedea0SLionel Sambuc # endif
247ebfedea0SLionel Sambuc 
248ebfedea0SLionel Sambuc #endif
249