155714Skris/* apps/gendh.c */
259191Skris/* obsoleted by dhparam.c */
355714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
455714Skris * All rights reserved.
555714Skris *
655714Skris * This package is an SSL implementation written
755714Skris * by Eric Young (eay@cryptsoft.com).
855714Skris * The implementation was written so as to conform with Netscapes SSL.
9280297Sjkim *
1055714Skris * This library is free for commercial and non-commercial use as long as
1155714Skris * the following conditions are aheared to.  The following conditions
1255714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1355714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1455714Skris * included with this distribution is covered by the same copyright terms
1555714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16280297Sjkim *
1755714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1855714Skris * the code are not to be removed.
1955714Skris * If this package is used in a product, Eric Young should be given attribution
2055714Skris * as the author of the parts of the library used.
2155714Skris * This can be in the form of a textual message at program startup or
2255714Skris * in documentation (online or textual) provided with the package.
23280297Sjkim *
2455714Skris * Redistribution and use in source and binary forms, with or without
2555714Skris * modification, are permitted provided that the following conditions
2655714Skris * are met:
2755714Skris * 1. Redistributions of source code must retain the copyright
2855714Skris *    notice, this list of conditions and the following disclaimer.
2955714Skris * 2. Redistributions in binary form must reproduce the above copyright
3055714Skris *    notice, this list of conditions and the following disclaimer in the
3155714Skris *    documentation and/or other materials provided with the distribution.
3255714Skris * 3. All advertising materials mentioning features or use of this software
3355714Skris *    must display the following acknowledgement:
3455714Skris *    "This product includes cryptographic software written by
3555714Skris *     Eric Young (eay@cryptsoft.com)"
3655714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3755714Skris *    being used are not cryptographic related :-).
38280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3955714Skris *    the apps directory (application code) you must include an acknowledgement:
4055714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41280297Sjkim *
4255714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4355714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4455714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4555714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4655714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4755714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4855714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4955714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5055714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5155714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5255714Skris * SUCH DAMAGE.
53280297Sjkim *
5455714Skris * The licence and distribution terms for any publically available version or
5555714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5655714Skris * copied and put under another distribution licence
5755714Skris * [including the GNU Public Licence.]
5855714Skris */
5955714Skris
60160814Ssimon#include <openssl/opensslconf.h>
61280297Sjkim/*
62280297Sjkim * Until the key-gen callbacks are modified to use newer prototypes, we allow
63280297Sjkim * deprecated functions for openssl-internal code
64280297Sjkim */
65160814Ssimon#ifdef OPENSSL_NO_DEPRECATED
66280297Sjkim# undef OPENSSL_NO_DEPRECATED
67160814Ssimon#endif
68160814Ssimon
69109998Smarkm#ifndef OPENSSL_NO_DH
70280297Sjkim# include <stdio.h>
71280297Sjkim# include <string.h>
72280297Sjkim# include <sys/types.h>
73280297Sjkim# include <sys/stat.h>
74280297Sjkim# include "apps.h"
75280297Sjkim# include <openssl/bio.h>
76280297Sjkim# include <openssl/rand.h>
77280297Sjkim# include <openssl/err.h>
78280297Sjkim# include <openssl/bn.h>
79280297Sjkim# include <openssl/dh.h>
80280297Sjkim# include <openssl/x509.h>
81280297Sjkim# include <openssl/pem.h>
8255714Skris
83284283Sjkim# define DEFBITS 2048
84280297Sjkim# undef PROG
85280297Sjkim# define PROG gendh_main
8655714Skris
87160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
8859191Skris
8959191Skrisint MAIN(int, char **);
9059191Skris
9155714Skrisint MAIN(int argc, char **argv)
92280297Sjkim{
93280297Sjkim    BN_GENCB cb;
94280297Sjkim    DH *dh = NULL;
95280297Sjkim    int ret = 1, num = DEFBITS;
96280297Sjkim    int g = 2;
97280297Sjkim    char *outfile = NULL;
98280297Sjkim    char *inrand = NULL;
99280297Sjkim    char *engine = NULL;
100280297Sjkim    BIO *out = NULL;
10155714Skris
102280297Sjkim    apps_startup();
10355714Skris
104280297Sjkim    BN_GENCB_set(&cb, dh_cb, bio_err);
105280297Sjkim    if (bio_err == NULL)
106280297Sjkim        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
107280297Sjkim            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
10855714Skris
109280297Sjkim    if (!load_config(bio_err, NULL))
110280297Sjkim        goto end;
111109998Smarkm
112280297Sjkim    argv++;
113280297Sjkim    argc--;
114280297Sjkim    for (;;) {
115280297Sjkim        if (argc <= 0)
116280297Sjkim            break;
117280297Sjkim        if (strcmp(*argv, "-out") == 0) {
118280297Sjkim            if (--argc < 1)
119280297Sjkim                goto bad;
120280297Sjkim            outfile = *(++argv);
121280297Sjkim        } else if (strcmp(*argv, "-2") == 0)
122280297Sjkim            g = 2;
123280297Sjkim/*-     else if (strcmp(*argv,"-3") == 0)
124280297Sjkim                g=3; */
125280297Sjkim        else if (strcmp(*argv, "-5") == 0)
126280297Sjkim            g = 5;
127280297Sjkim# ifndef OPENSSL_NO_ENGINE
128280297Sjkim        else if (strcmp(*argv, "-engine") == 0) {
129280297Sjkim            if (--argc < 1)
130280297Sjkim                goto bad;
131280297Sjkim            engine = *(++argv);
132280297Sjkim        }
133280297Sjkim# endif
134280297Sjkim        else if (strcmp(*argv, "-rand") == 0) {
135280297Sjkim            if (--argc < 1)
136280297Sjkim                goto bad;
137280297Sjkim            inrand = *(++argv);
138280297Sjkim        } else
139280297Sjkim            break;
140280297Sjkim        argv++;
141280297Sjkim        argc--;
142280297Sjkim    }
143280297Sjkim    if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) {
144280297Sjkim bad:
145280297Sjkim        BIO_printf(bio_err, "usage: gendh [args] [numbits]\n");
146280297Sjkim        BIO_printf(bio_err, " -out file - output the key to 'file\n");
147280297Sjkim        BIO_printf(bio_err, " -2        - use 2 as the generator value\n");
148280297Sjkim        /*
149280297Sjkim         * BIO_printf(bio_err," -3 - use 3 as the generator value\n");
150280297Sjkim         */
151280297Sjkim        BIO_printf(bio_err, " -5        - use 5 as the generator value\n");
152280297Sjkim# ifndef OPENSSL_NO_ENGINE
153280297Sjkim        BIO_printf(bio_err,
154280297Sjkim                   " -engine e - use engine e, possibly a hardware device.\n");
155280297Sjkim# endif
156280297Sjkim        BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
157280297Sjkim                   LIST_SEPARATOR_CHAR);
158280297Sjkim        BIO_printf(bio_err,
159280297Sjkim                   "           - load the file (or the files in the directory) into\n");
160280297Sjkim        BIO_printf(bio_err, "             the random number generator\n");
161280297Sjkim        goto end;
162280297Sjkim    }
163280297Sjkim    setup_engine(bio_err, engine, 0);
164109998Smarkm
165280297Sjkim    out = BIO_new(BIO_s_file());
166280297Sjkim    if (out == NULL) {
167280297Sjkim        ERR_print_errors(bio_err);
168280297Sjkim        goto end;
169280297Sjkim    }
17055714Skris
171280297Sjkim    if (outfile == NULL) {
172280297Sjkim        BIO_set_fp(out, stdout, BIO_NOCLOSE);
173280297Sjkim# ifdef OPENSSL_SYS_VMS
174280297Sjkim        {
175280297Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
176280297Sjkim            out = BIO_push(tmpbio, out);
177280297Sjkim        }
178280297Sjkim# endif
179280297Sjkim    } else {
180280297Sjkim        if (BIO_write_filename(out, outfile) <= 0) {
181280297Sjkim            perror(outfile);
182280297Sjkim            goto end;
183280297Sjkim        }
184280297Sjkim    }
18555714Skris
186280297Sjkim    if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) {
187280297Sjkim        BIO_printf(bio_err,
188280297Sjkim                   "warning, not much extra random data, consider using the -rand option\n");
189280297Sjkim    }
190280297Sjkim    if (inrand != NULL)
191280297Sjkim        BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
192280297Sjkim                   app_RAND_load_files(inrand));
19355714Skris
194280297Sjkim    BIO_printf(bio_err,
195280297Sjkim               "Generating DH parameters, %d bit long safe prime, generator %d\n",
196280297Sjkim               num, g);
197280297Sjkim    BIO_printf(bio_err, "This is going to take a long time\n");
198160814Ssimon
199280297Sjkim    if (((dh = DH_new()) == NULL)
200280297Sjkim        || !DH_generate_parameters_ex(dh, num, g, &cb))
201280297Sjkim        goto end;
20255714Skris
203280297Sjkim    app_RAND_write_file(NULL, bio_err);
20455714Skris
205280297Sjkim    if (!PEM_write_bio_DHparams(out, dh))
206280297Sjkim        goto end;
207280297Sjkim    ret = 0;
208280297Sjkim end:
209280297Sjkim    if (ret != 0)
210280297Sjkim        ERR_print_errors(bio_err);
211280297Sjkim    if (out != NULL)
212280297Sjkim        BIO_free_all(out);
213280297Sjkim    if (dh != NULL)
214280297Sjkim        DH_free(dh);
215280297Sjkim    apps_shutdown();
216280297Sjkim    OPENSSL_EXIT(ret);
217280297Sjkim}
218280297Sjkim
219160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
220280297Sjkim{
221280297Sjkim    char c = '*';
22255714Skris
223280297Sjkim    if (p == 0)
224280297Sjkim        c = '.';
225280297Sjkim    if (p == 1)
226280297Sjkim        c = '+';
227280297Sjkim    if (p == 2)
228280297Sjkim        c = '*';
229280297Sjkim    if (p == 3)
230280297Sjkim        c = '\n';
231280297Sjkim    BIO_write(cb->arg, &c, 1);
232280297Sjkim    (void)BIO_flush(cb->arg);
233280297Sjkim# ifdef LINT
234280297Sjkim    p = n;
235280297Sjkim# endif
236280297Sjkim    return 1;
237280297Sjkim}
238280297Sjkim#else                           /* !OPENSSL_NO_DH */
239238405Sjkim
240238405Sjkim# if PEDANTIC
241280297Sjkimstatic void *dummy = &dummy;
242238405Sjkim# endif
243238405Sjkim
24455714Skris#endif
245