xref: /openbsd-src/usr.bin/openssl/gendh.c (revision e7718adaf9bdf5a32db0f291795197a85ccbb6ed)
1*e7718adaStb /* $OpenBSD: gendh.c,v 1.14 2023/03/06 14:32:06 tb Exp $ */
2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3dab3f910Sjsing  * All rights reserved.
4dab3f910Sjsing  *
5dab3f910Sjsing  * This package is an SSL implementation written
6dab3f910Sjsing  * by Eric Young (eay@cryptsoft.com).
7dab3f910Sjsing  * The implementation was written so as to conform with Netscapes SSL.
8dab3f910Sjsing  *
9dab3f910Sjsing  * This library is free for commercial and non-commercial use as long as
10dab3f910Sjsing  * the following conditions are aheared to.  The following conditions
11dab3f910Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
12dab3f910Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13dab3f910Sjsing  * included with this distribution is covered by the same copyright terms
14dab3f910Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15dab3f910Sjsing  *
16dab3f910Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
17dab3f910Sjsing  * the code are not to be removed.
18dab3f910Sjsing  * If this package is used in a product, Eric Young should be given attribution
19dab3f910Sjsing  * as the author of the parts of the library used.
20dab3f910Sjsing  * This can be in the form of a textual message at program startup or
21dab3f910Sjsing  * in documentation (online or textual) provided with the package.
22dab3f910Sjsing  *
23dab3f910Sjsing  * Redistribution and use in source and binary forms, with or without
24dab3f910Sjsing  * modification, are permitted provided that the following conditions
25dab3f910Sjsing  * are met:
26dab3f910Sjsing  * 1. Redistributions of source code must retain the copyright
27dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer.
28dab3f910Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
29dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer in the
30dab3f910Sjsing  *    documentation and/or other materials provided with the distribution.
31dab3f910Sjsing  * 3. All advertising materials mentioning features or use of this software
32dab3f910Sjsing  *    must display the following acknowledgement:
33dab3f910Sjsing  *    "This product includes cryptographic software written by
34dab3f910Sjsing  *     Eric Young (eay@cryptsoft.com)"
35dab3f910Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
36dab3f910Sjsing  *    being used are not cryptographic related :-).
37dab3f910Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
38dab3f910Sjsing  *    the apps directory (application code) you must include an acknowledgement:
39dab3f910Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40dab3f910Sjsing  *
41dab3f910Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42dab3f910Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43dab3f910Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44dab3f910Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45dab3f910Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46dab3f910Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47dab3f910Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48dab3f910Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49dab3f910Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50dab3f910Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51dab3f910Sjsing  * SUCH DAMAGE.
52dab3f910Sjsing  *
53dab3f910Sjsing  * The licence and distribution terms for any publically available version or
54dab3f910Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55dab3f910Sjsing  * copied and put under another distribution licence
56dab3f910Sjsing  * [including the GNU Public Licence.]
57dab3f910Sjsing  */
58dab3f910Sjsing 
59dab3f910Sjsing #include <openssl/opensslconf.h>
60dab3f910Sjsing 
61dab3f910Sjsing /* Until the key-gen callbacks are modified to use newer prototypes, we allow
62dab3f910Sjsing  * deprecated functions for openssl-internal code */
63dab3f910Sjsing #ifdef OPENSSL_NO_DEPRECATED
64dab3f910Sjsing #undef OPENSSL_NO_DEPRECATED
65dab3f910Sjsing #endif
66dab3f910Sjsing 
67dab3f910Sjsing #ifndef OPENSSL_NO_DH
68dab3f910Sjsing 
69dab3f910Sjsing #include <sys/types.h>
70dab3f910Sjsing #include <sys/stat.h>
71dab3f910Sjsing 
722b7537d7Sdoug #include <limits.h>
73dab3f910Sjsing #include <stdio.h>
74dab3f910Sjsing #include <string.h>
75dab3f910Sjsing 
76dab3f910Sjsing #include "apps.h"
77dab3f910Sjsing 
78dab3f910Sjsing #include <openssl/bio.h>
79dab3f910Sjsing #include <openssl/bn.h>
80dab3f910Sjsing #include <openssl/dh.h>
81dab3f910Sjsing #include <openssl/err.h>
82dab3f910Sjsing #include <openssl/pem.h>
83dab3f910Sjsing #include <openssl/x509.h>
84dab3f910Sjsing 
85dab3f910Sjsing #define DEFBITS	512
86dab3f910Sjsing 
87dab3f910Sjsing static int dh_cb(int p, int n, BN_GENCB *cb);
88dab3f910Sjsing 
892b7537d7Sdoug static struct {
902b7537d7Sdoug 	int g;
912b7537d7Sdoug 	char *outfile;
92*e7718adaStb } cfg;
932b7537d7Sdoug 
94ea149709Sguenther static const struct option gendh_options[] = {
952b7537d7Sdoug 	{
962b7537d7Sdoug 		.name = "2",
972b7537d7Sdoug 		.desc = "Generate DH parameters with a generator value of 2 "
982b7537d7Sdoug 		    "(default)",
992b7537d7Sdoug 		.type = OPTION_VALUE,
1002b7537d7Sdoug 		.value = 2,
101*e7718adaStb 		.opt.value = &cfg.g,
1022b7537d7Sdoug 	},
1032b7537d7Sdoug 	{
1042b7537d7Sdoug 		.name = "5",
1052b7537d7Sdoug 		.desc = "Generate DH parameters with a generator value of 5",
1062b7537d7Sdoug 		.type = OPTION_VALUE,
1072b7537d7Sdoug 		.value = 5,
108*e7718adaStb 		.opt.value = &cfg.g,
1092b7537d7Sdoug 	},
1102b7537d7Sdoug 	{
1112b7537d7Sdoug 		.name = "out",
1122b7537d7Sdoug 		.argname = "file",
1132b7537d7Sdoug 		.desc = "Output file (default stdout)",
1142b7537d7Sdoug 		.type = OPTION_ARG,
115*e7718adaStb 		.opt.arg = &cfg.outfile,
1162b7537d7Sdoug 	},
1172b7537d7Sdoug 	{ NULL },
1182b7537d7Sdoug };
1192b7537d7Sdoug 
1202b7537d7Sdoug static void
gendh_usage(void)1212b7537d7Sdoug gendh_usage(void)
1222b7537d7Sdoug {
1232b7537d7Sdoug 	fprintf(stderr,
1245284dfeaSbcook 	    "usage: gendh [-2 | -5] [-out file] [numbits]\n\n");
1252b7537d7Sdoug 	options_usage(gendh_options);
1262b7537d7Sdoug }
1272b7537d7Sdoug 
128dab3f910Sjsing int
gendh_main(int argc,char ** argv)129dab3f910Sjsing gendh_main(int argc, char **argv)
130dab3f910Sjsing {
131e0a955c7Stb 	BN_GENCB *cb = NULL;
132dab3f910Sjsing 	DH *dh = NULL;
1332b7537d7Sdoug 	int ret = 1, numbits = DEFBITS;
134dab3f910Sjsing 	BIO *out = NULL;
1352b7537d7Sdoug 	char *strbits = NULL;
136dab3f910Sjsing 
13751811eadSderaadt 	if (pledge("stdio cpath wpath rpath", NULL) == -1) {
1389bc487adSdoug 		perror("pledge");
139e370f0eeSdoug 		exit(1);
140e370f0eeSdoug 	}
1419bc487adSdoug 
142e0a955c7Stb 	if ((cb = BN_GENCB_new()) == NULL) {
143e0a955c7Stb 		BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
144e0a955c7Stb 		goto end;
145e0a955c7Stb 	}
146e0a955c7Stb 
147e0a955c7Stb 	BN_GENCB_set(cb, dh_cb, bio_err);
148dab3f910Sjsing 
149*e7718adaStb 	memset(&cfg, 0, sizeof(cfg));
1502b7537d7Sdoug 
151*e7718adaStb 	cfg.g = 2;
1522b7537d7Sdoug 
1532b7537d7Sdoug 	if (options_parse(argc, argv, gendh_options, &strbits, NULL) != 0) {
1542b7537d7Sdoug 		gendh_usage();
155dab3f910Sjsing 		goto end;
156dab3f910Sjsing 	}
1572b7537d7Sdoug 
1582b7537d7Sdoug 	if (strbits != NULL) {
1592b7537d7Sdoug 		const char *errstr;
1602b7537d7Sdoug 		numbits = strtonum(strbits, 0, INT_MAX, &errstr);
1612b7537d7Sdoug 		if (errstr) {
1622b7537d7Sdoug 			fprintf(stderr, "Invalid number of bits: %s\n", errstr);
1632b7537d7Sdoug 			goto end;
1642b7537d7Sdoug 		}
1652b7537d7Sdoug 	}
1662b7537d7Sdoug 
167dab3f910Sjsing 	out = BIO_new(BIO_s_file());
168dab3f910Sjsing 	if (out == NULL) {
169dab3f910Sjsing 		ERR_print_errors(bio_err);
170dab3f910Sjsing 		goto end;
171dab3f910Sjsing 	}
172*e7718adaStb 	if (cfg.outfile == NULL) {
173dab3f910Sjsing 		BIO_set_fp(out, stdout, BIO_NOCLOSE);
174dab3f910Sjsing 	} else {
175*e7718adaStb 		if (BIO_write_filename(out, cfg.outfile) <= 0) {
176*e7718adaStb 			perror(cfg.outfile);
177dab3f910Sjsing 			goto end;
178dab3f910Sjsing 		}
179dab3f910Sjsing 	}
180dab3f910Sjsing 
1812b7537d7Sdoug 	BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime,"
182*e7718adaStb 	    " generator %d\n", numbits, cfg.g);
183dab3f910Sjsing 	BIO_printf(bio_err, "This is going to take a long time\n");
184dab3f910Sjsing 
1852b7537d7Sdoug 	if (((dh = DH_new()) == NULL) ||
186*e7718adaStb 	    !DH_generate_parameters_ex(dh, numbits, cfg.g, cb))
187dab3f910Sjsing 		goto end;
188dab3f910Sjsing 
189dab3f910Sjsing 	if (!PEM_write_bio_DHparams(out, dh))
190dab3f910Sjsing 		goto end;
191dab3f910Sjsing 	ret = 0;
192dab3f910Sjsing  end:
193dab3f910Sjsing 	if (ret != 0)
194dab3f910Sjsing 		ERR_print_errors(bio_err);
195dab3f910Sjsing 	BIO_free_all(out);
196e0a955c7Stb 	BN_GENCB_free(cb);
197dab3f910Sjsing 	DH_free(dh);
198dab3f910Sjsing 
199dab3f910Sjsing 	return (ret);
200dab3f910Sjsing }
201dab3f910Sjsing 
202dab3f910Sjsing static int
dh_cb(int p,int n,BN_GENCB * cb)203dab3f910Sjsing dh_cb(int p, int n, BN_GENCB *cb)
204dab3f910Sjsing {
205dab3f910Sjsing 	char c = '*';
206dab3f910Sjsing 
207dab3f910Sjsing 	if (p == 0)
208dab3f910Sjsing 		c = '.';
209dab3f910Sjsing 	if (p == 1)
210dab3f910Sjsing 		c = '+';
211dab3f910Sjsing 	if (p == 2)
212dab3f910Sjsing 		c = '*';
213dab3f910Sjsing 	if (p == 3)
214dab3f910Sjsing 		c = '\n';
215e0a955c7Stb 	BIO_write(BN_GENCB_get_arg(cb), &c, 1);
216e0a955c7Stb 	(void) BIO_flush(BN_GENCB_get_arg(cb));
217dab3f910Sjsing 	return 1;
218dab3f910Sjsing }
219dab3f910Sjsing #endif
220