155714Skris/* apps/dh.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.
9280304Sjkim *
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).
16280304Sjkim *
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.
23280304Sjkim *
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 :-).
38280304Sjkim * 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)"
41280304Sjkim *
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.
53280304Sjkim *
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
60280304Sjkim#include <openssl/opensslconf.h> /* for OPENSSL_NO_DH */
61109998Smarkm#ifndef OPENSSL_NO_DH
62280304Sjkim# include <stdio.h>
63280304Sjkim# include <stdlib.h>
64280304Sjkim# include <time.h>
65280304Sjkim# include <string.h>
66280304Sjkim# include "apps.h"
67280304Sjkim# include <openssl/bio.h>
68280304Sjkim# include <openssl/err.h>
69280304Sjkim# include <openssl/bn.h>
70280304Sjkim# include <openssl/dh.h>
71280304Sjkim# include <openssl/x509.h>
72280304Sjkim# include <openssl/pem.h>
7355714Skris
74280304Sjkim# undef PROG
75280304Sjkim# define PROG    dh_main
7655714Skris
77280304Sjkim/*-
78280304Sjkim * -inform arg  - input format - default PEM (DER or PEM)
7955714Skris * -outform arg - output format - default PEM
80280304Sjkim * -in arg      - input file - default stdin
81280304Sjkim * -out arg     - output file - default stdout
82280304Sjkim * -check       - check the parameters are ok
8355714Skris * -noout
8455714Skris * -text
8555714Skris * -C
8655714Skris */
8755714Skris
8859191Skrisint MAIN(int, char **);
8959191Skris
9055714Skrisint MAIN(int argc, char **argv)
91280304Sjkim{
92280304Sjkim    DH *dh = NULL;
93280304Sjkim    int i, badops = 0, text = 0;
94280304Sjkim    BIO *in = NULL, *out = NULL;
95280304Sjkim    int informat, outformat, check = 0, noout = 0, C = 0, ret = 1;
96280304Sjkim    char *infile, *outfile, *prog;
97280304Sjkim# ifndef OPENSSL_NO_ENGINE
98280304Sjkim    char *engine;
99280304Sjkim# endif
10055714Skris
101280304Sjkim    apps_startup();
10255714Skris
103280304Sjkim    if (bio_err == NULL)
104280304Sjkim        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
105280304Sjkim            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
10655714Skris
107280304Sjkim    if (!load_config(bio_err, NULL))
108280304Sjkim        goto end;
109109998Smarkm
110280304Sjkim# ifndef OPENSSL_NO_ENGINE
111280304Sjkim    engine = NULL;
112280304Sjkim# endif
113280304Sjkim    infile = NULL;
114280304Sjkim    outfile = NULL;
115280304Sjkim    informat = FORMAT_PEM;
116280304Sjkim    outformat = FORMAT_PEM;
11755714Skris
118280304Sjkim    prog = argv[0];
119280304Sjkim    argc--;
120280304Sjkim    argv++;
121280304Sjkim    while (argc >= 1) {
122280304Sjkim        if (strcmp(*argv, "-inform") == 0) {
123280304Sjkim            if (--argc < 1)
124280304Sjkim                goto bad;
125280304Sjkim            informat = str2fmt(*(++argv));
126280304Sjkim        } else if (strcmp(*argv, "-outform") == 0) {
127280304Sjkim            if (--argc < 1)
128280304Sjkim                goto bad;
129280304Sjkim            outformat = str2fmt(*(++argv));
130280304Sjkim        } else if (strcmp(*argv, "-in") == 0) {
131280304Sjkim            if (--argc < 1)
132280304Sjkim                goto bad;
133280304Sjkim            infile = *(++argv);
134280304Sjkim        } else if (strcmp(*argv, "-out") == 0) {
135280304Sjkim            if (--argc < 1)
136280304Sjkim                goto bad;
137280304Sjkim            outfile = *(++argv);
138280304Sjkim        }
139280304Sjkim# ifndef OPENSSL_NO_ENGINE
140280304Sjkim        else if (strcmp(*argv, "-engine") == 0) {
141280304Sjkim            if (--argc < 1)
142280304Sjkim                goto bad;
143280304Sjkim            engine = *(++argv);
144280304Sjkim        }
145280304Sjkim# endif
146280304Sjkim        else if (strcmp(*argv, "-check") == 0)
147280304Sjkim            check = 1;
148280304Sjkim        else if (strcmp(*argv, "-text") == 0)
149280304Sjkim            text = 1;
150280304Sjkim        else if (strcmp(*argv, "-C") == 0)
151280304Sjkim            C = 1;
152280304Sjkim        else if (strcmp(*argv, "-noout") == 0)
153280304Sjkim            noout = 1;
154280304Sjkim        else {
155280304Sjkim            BIO_printf(bio_err, "unknown option %s\n", *argv);
156280304Sjkim            badops = 1;
157280304Sjkim            break;
158280304Sjkim        }
159280304Sjkim        argc--;
160280304Sjkim        argv++;
161280304Sjkim    }
16255714Skris
163280304Sjkim    if (badops) {
164280304Sjkim bad:
165280304Sjkim        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
166280304Sjkim        BIO_printf(bio_err, "where options are\n");
167280304Sjkim        BIO_printf(bio_err, " -inform arg   input format - one of DER PEM\n");
168280304Sjkim        BIO_printf(bio_err,
169280304Sjkim                   " -outform arg  output format - one of DER PEM\n");
170280304Sjkim        BIO_printf(bio_err, " -in arg       input file\n");
171280304Sjkim        BIO_printf(bio_err, " -out arg      output file\n");
172280304Sjkim        BIO_printf(bio_err, " -check        check the DH parameters\n");
173280304Sjkim        BIO_printf(bio_err,
174280304Sjkim                   " -text         print a text form of the DH parameters\n");
175280304Sjkim        BIO_printf(bio_err, " -C            Output C code\n");
176280304Sjkim        BIO_printf(bio_err, " -noout        no output\n");
177280304Sjkim# ifndef OPENSSL_NO_ENGINE
178280304Sjkim        BIO_printf(bio_err,
179280304Sjkim                   " -engine e     use engine e, possibly a hardware device.\n");
180280304Sjkim# endif
181280304Sjkim        goto end;
182280304Sjkim    }
18355714Skris
184280304Sjkim    ERR_load_crypto_strings();
18555714Skris
186280304Sjkim# ifndef OPENSSL_NO_ENGINE
187280304Sjkim    setup_engine(bio_err, engine, 0);
188280304Sjkim# endif
189109998Smarkm
190280304Sjkim    in = BIO_new(BIO_s_file());
191280304Sjkim    out = BIO_new(BIO_s_file());
192280304Sjkim    if ((in == NULL) || (out == NULL)) {
193280304Sjkim        ERR_print_errors(bio_err);
194280304Sjkim        goto end;
195280304Sjkim    }
19655714Skris
197280304Sjkim    if (infile == NULL)
198280304Sjkim        BIO_set_fp(in, stdin, BIO_NOCLOSE);
199280304Sjkim    else {
200280304Sjkim        if (BIO_read_filename(in, infile) <= 0) {
201280304Sjkim            perror(infile);
202280304Sjkim            goto end;
203280304Sjkim        }
204280304Sjkim    }
205280304Sjkim    if (outfile == NULL) {
206280304Sjkim        BIO_set_fp(out, stdout, BIO_NOCLOSE);
207280304Sjkim# ifdef OPENSSL_SYS_VMS
208280304Sjkim        {
209280304Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
210280304Sjkim            out = BIO_push(tmpbio, out);
211280304Sjkim        }
212280304Sjkim# endif
213280304Sjkim    } else {
214280304Sjkim        if (BIO_write_filename(out, outfile) <= 0) {
215280304Sjkim            perror(outfile);
216280304Sjkim            goto end;
217280304Sjkim        }
218280304Sjkim    }
21955714Skris
220280304Sjkim    if (informat == FORMAT_ASN1)
221280304Sjkim        dh = d2i_DHparams_bio(in, NULL);
222280304Sjkim    else if (informat == FORMAT_PEM)
223280304Sjkim        dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
224280304Sjkim    else {
225280304Sjkim        BIO_printf(bio_err, "bad input format specified\n");
226280304Sjkim        goto end;
227280304Sjkim    }
228280304Sjkim    if (dh == NULL) {
229280304Sjkim        BIO_printf(bio_err, "unable to load DH parameters\n");
230280304Sjkim        ERR_print_errors(bio_err);
231280304Sjkim        goto end;
232280304Sjkim    }
23355714Skris
234280304Sjkim    if (text) {
235280304Sjkim        DHparams_print(out, dh);
236280304Sjkim# ifdef undef
237280304Sjkim        printf("p=");
238280304Sjkim        BN_print(stdout, dh->p);
239280304Sjkim        printf("\ng=");
240280304Sjkim        BN_print(stdout, dh->g);
241280304Sjkim        printf("\n");
242280304Sjkim        if (dh->length != 0)
243280304Sjkim            printf("recommended private length=%ld\n", dh->length);
244280304Sjkim# endif
245280304Sjkim    }
24655714Skris
247280304Sjkim    if (check) {
248280304Sjkim        if (!DH_check(dh, &i)) {
249280304Sjkim            ERR_print_errors(bio_err);
250280304Sjkim            goto end;
251280304Sjkim        }
252280304Sjkim        if (i & DH_CHECK_P_NOT_PRIME)
253280304Sjkim            printf("p value is not prime\n");
254280304Sjkim        if (i & DH_CHECK_P_NOT_SAFE_PRIME)
255280304Sjkim            printf("p value is not a safe prime\n");
256280304Sjkim        if (i & DH_UNABLE_TO_CHECK_GENERATOR)
257280304Sjkim            printf("unable to check the generator value\n");
258280304Sjkim        if (i & DH_NOT_SUITABLE_GENERATOR)
259280304Sjkim            printf("the g value is not a generator\n");
260280304Sjkim        if (i == 0)
261280304Sjkim            printf("DH parameters appear to be ok.\n");
262280304Sjkim    }
263280304Sjkim    if (C) {
264280304Sjkim        unsigned char *data;
265280304Sjkim        int len, l, bits;
26655714Skris
267280304Sjkim        len = BN_num_bytes(dh->p);
268280304Sjkim        bits = BN_num_bits(dh->p);
269280304Sjkim        data = (unsigned char *)OPENSSL_malloc(len);
270280304Sjkim        if (data == NULL) {
271280304Sjkim            perror("OPENSSL_malloc");
272280304Sjkim            goto end;
273280304Sjkim        }
274280304Sjkim        l = BN_bn2bin(dh->p, data);
275280304Sjkim        printf("static unsigned char dh%d_p[]={", bits);
276280304Sjkim        for (i = 0; i < l; i++) {
277280304Sjkim            if ((i % 12) == 0)
278280304Sjkim                printf("\n\t");
279280304Sjkim            printf("0x%02X,", data[i]);
280280304Sjkim        }
281280304Sjkim        printf("\n\t};\n");
28255714Skris
283280304Sjkim        l = BN_bn2bin(dh->g, data);
284280304Sjkim        printf("static unsigned char dh%d_g[]={", bits);
285280304Sjkim        for (i = 0; i < l; i++) {
286280304Sjkim            if ((i % 12) == 0)
287280304Sjkim                printf("\n\t");
288280304Sjkim            printf("0x%02X,", data[i]);
289280304Sjkim        }
290280304Sjkim        printf("\n\t};\n\n");
29155714Skris
292280304Sjkim        printf("DH *get_dh%d()\n\t{\n", bits);
293280304Sjkim        printf("\tDH *dh;\n\n");
294280304Sjkim        printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
295280304Sjkim        printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
296280304Sjkim               bits, bits);
297280304Sjkim        printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
298280304Sjkim               bits, bits);
299280304Sjkim        printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
300280304Sjkim        printf("\t\treturn(NULL);\n");
301280304Sjkim        printf("\treturn(dh);\n\t}\n");
302280304Sjkim        OPENSSL_free(data);
303280304Sjkim    }
30455714Skris
305280304Sjkim    if (!noout) {
306280304Sjkim        if (outformat == FORMAT_ASN1)
307280304Sjkim            i = i2d_DHparams_bio(out, dh);
308280304Sjkim        else if (outformat == FORMAT_PEM)
309280304Sjkim            i = PEM_write_bio_DHparams(out, dh);
310280304Sjkim        else {
311280304Sjkim            BIO_printf(bio_err, "bad output format specified for outfile\n");
312280304Sjkim            goto end;
313280304Sjkim        }
314280304Sjkim        if (!i) {
315280304Sjkim            BIO_printf(bio_err, "unable to write DH parameters\n");
316280304Sjkim            ERR_print_errors(bio_err);
317280304Sjkim            goto end;
318280304Sjkim        }
319280304Sjkim    }
320280304Sjkim    ret = 0;
321280304Sjkim end:
322280304Sjkim    if (in != NULL)
323280304Sjkim        BIO_free(in);
324280304Sjkim    if (out != NULL)
325280304Sjkim        BIO_free_all(out);
326280304Sjkim    if (dh != NULL)
327280304Sjkim        DH_free(dh);
328280304Sjkim    apps_shutdown();
329280304Sjkim    OPENSSL_EXIT(ret);
330280304Sjkim}
331280304Sjkim#else                           /* !OPENSSL_NO_DH */
33255714Skris
333238405Sjkim# if PEDANTIC
334280304Sjkimstatic void *dummy = &dummy;
335238405Sjkim# endif
336238405Sjkim
33755714Skris#endif
338