xref: /dragonfly/crypto/libressl/apps/openssl/dh.c (revision 6f5ec8b5)
1 /* $OpenBSD: dh.c,v 1.13 2022/01/14 09:21:54 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <openssl/opensslconf.h>	/* for OPENSSL_NO_DH */
60 
61 #ifndef OPENSSL_NO_DH
62 
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <time.h>
67 
68 #include "apps.h"
69 
70 #include <openssl/bio.h>
71 #include <openssl/bn.h>
72 #include <openssl/err.h>
73 #include <openssl/dh.h>
74 #include <openssl/pem.h>
75 #include <openssl/x509.h>
76 
77 static struct {
78 	int C;
79 	int check;
80 	char *infile;
81 	int informat;
82 	int noout;
83 	char *outfile;
84 	int outformat;
85 	int text;
86 } dh_config;
87 
88 static const struct option dh_options[] = {
89 	{
90 		.name = "C",
91 		.desc = "Convert DH parameters into C code",
92 		.type = OPTION_FLAG,
93 		.opt.flag = &dh_config.C,
94 	},
95 	{
96 		.name = "check",
97 		.desc = "Check the DH parameters",
98 		.type = OPTION_FLAG,
99 		.opt.flag = &dh_config.check,
100 	},
101 	{
102 		.name = "in",
103 		.argname = "file",
104 		.desc = "Input file (default stdin)",
105 		.type = OPTION_ARG,
106 		.opt.arg = &dh_config.infile,
107 	},
108 	{
109 		.name = "inform",
110 		.argname = "format",
111 		.desc = "Input format (DER or PEM (default))",
112 		.type = OPTION_ARG_FORMAT,
113 		.opt.value = &dh_config.informat,
114 	},
115 	{
116 		.name = "noout",
117 		.desc = "No output",
118 		.type = OPTION_FLAG,
119 		.opt.flag = &dh_config.noout,
120 	},
121 	{
122 		.name = "out",
123 		.argname = "file",
124 		.desc = "Output file (default stdout)",
125 		.type = OPTION_ARG,
126 		.opt.arg = &dh_config.outfile,
127 	},
128 	{
129 		.name = "outform",
130 		.argname = "format",
131 		.desc = "Output format (DER or PEM (default))",
132 		.type = OPTION_ARG_FORMAT,
133 		.opt.value = &dh_config.outformat,
134 	},
135 	{
136 		.name = "text",
137 		.desc = "Print a text form of the DH parameters",
138 		.type = OPTION_FLAG,
139 		.opt.flag = &dh_config.text,
140 	},
141 	{ NULL },
142 };
143 
144 static void
145 dh_usage(void)
146 {
147 	fprintf(stderr,
148 	    "usage: dh [-C] [-check] [-in file] [-inform format]\n"
149 	    "    [-noout] [-out file] [-outform format] [-text]\n\n");
150 	options_usage(dh_options);
151 }
152 
153 int
154 dh_main(int argc, char **argv)
155 {
156 	DH *dh = NULL;
157 	int i;
158 	BIO *in = NULL, *out = NULL;
159 	int ret = 1;
160 
161 	if (single_execution) {
162 		if (pledge("stdio cpath wpath rpath", NULL) == -1) {
163 			perror("pledge");
164 			exit(1);
165 		}
166 	}
167 
168 	memset(&dh_config, 0, sizeof(dh_config));
169 
170 	dh_config.informat = FORMAT_PEM;
171 	dh_config.outformat = FORMAT_PEM;
172 
173 	if (options_parse(argc, argv, dh_options, NULL, NULL) != 0) {
174 		dh_usage();
175 		goto end;
176 	}
177 
178 	in = BIO_new(BIO_s_file());
179 	out = BIO_new(BIO_s_file());
180 	if (in == NULL || out == NULL) {
181 		ERR_print_errors(bio_err);
182 		goto end;
183 	}
184 	if (dh_config.infile == NULL)
185 		BIO_set_fp(in, stdin, BIO_NOCLOSE);
186 	else {
187 		if (BIO_read_filename(in, dh_config.infile) <= 0) {
188 			perror(dh_config.infile);
189 			goto end;
190 		}
191 	}
192 	if (dh_config.outfile == NULL) {
193 		BIO_set_fp(out, stdout, BIO_NOCLOSE);
194 	} else {
195 		if (BIO_write_filename(out, dh_config.outfile) <= 0) {
196 			perror(dh_config.outfile);
197 			goto end;
198 		}
199 	}
200 
201 	if (dh_config.informat == FORMAT_ASN1)
202 		dh = d2i_DHparams_bio(in, NULL);
203 	else if (dh_config.informat == FORMAT_PEM)
204 		dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
205 	else {
206 		BIO_printf(bio_err, "bad input format specified\n");
207 		goto end;
208 	}
209 	if (dh == NULL) {
210 		BIO_printf(bio_err, "unable to load DH parameters\n");
211 		ERR_print_errors(bio_err);
212 		goto end;
213 	}
214 	if (dh_config.text) {
215 		DHparams_print(out, dh);
216 	}
217 	if (dh_config.check) {
218 		if (!DH_check(dh, &i)) {
219 			ERR_print_errors(bio_err);
220 			goto end;
221 		}
222 		if (i & DH_CHECK_P_NOT_PRIME)
223 			printf("p value is not prime\n");
224 		if (i & DH_CHECK_P_NOT_SAFE_PRIME)
225 			printf("p value is not a safe prime\n");
226 		if (i & DH_UNABLE_TO_CHECK_GENERATOR)
227 			printf("unable to check the generator value\n");
228 		if (i & DH_NOT_SUITABLE_GENERATOR)
229 			printf("the g value is not a generator\n");
230 		if (i == 0)
231 			printf("DH parameters appear to be ok.\n");
232 	}
233 	if (dh_config.C) {
234 		unsigned char *data;
235 		int len, l, bits;
236 
237 		len = BN_num_bytes(DH_get0_p(dh));
238 		bits = BN_num_bits(DH_get0_p(dh));
239 		data = malloc(len);
240 		if (data == NULL) {
241 			perror("malloc");
242 			goto end;
243 		}
244 		l = BN_bn2bin(DH_get0_p(dh), data);
245 		printf("static unsigned char dh%d_p[] = {", bits);
246 		for (i = 0; i < l; i++) {
247 			if ((i % 12) == 0)
248 				printf("\n\t");
249 			printf("0x%02X, ", data[i]);
250 		}
251 		printf("\n\t};\n");
252 
253 		l = BN_bn2bin(DH_get0_g(dh), data);
254 		printf("static unsigned char dh%d_g[] = {", bits);
255 		for (i = 0; i < l; i++) {
256 			if ((i % 12) == 0)
257 				printf("\n\t");
258 			printf("0x%02X, ", data[i]);
259 		}
260 		printf("\n\t};\n\n");
261 
262 		printf("DH *get_dh%d()\n\t{\n", bits);
263 		printf("\tDH *dh;\n");
264 		printf("\tBIGNUM *p = NULL, *g = NULL;\n\n");
265 		printf("\tif ((dh = DH_new()) == NULL) return(NULL);\n");
266 		printf("\tp = BN_bin2bn(dh%d_p, sizeof(dh%d_p), NULL);\n",
267 		    bits, bits);
268 		printf("\tg = BN_bin2bn(dh%d_g, sizeof(dh%d_g), NULL);\n",
269 		    bits, bits);
270 		printf("\tif (p == NULL || g == NULL)\n");
271 		printf("\t\t{ BN_free(p); BN_free(q); DH_free(dh); return(NULL); }\n");
272 		printf("\tDH_set0_pqg(dh, p, NULL, g);\n");
273 		printf("\treturn(dh);\n\t}\n");
274 		free(data);
275 	}
276 	if (!dh_config.noout) {
277 		if (dh_config.outformat == FORMAT_ASN1)
278 			i = i2d_DHparams_bio(out, dh);
279 		else if (dh_config.outformat == FORMAT_PEM)
280 			i = PEM_write_bio_DHparams(out, dh);
281 		else {
282 			BIO_printf(bio_err, "bad output format specified for outfile\n");
283 			goto end;
284 		}
285 		if (!i) {
286 			BIO_printf(bio_err, "unable to write DH parameters\n");
287 			ERR_print_errors(bio_err);
288 			goto end;
289 		}
290 	}
291 	ret = 0;
292 
293  end:
294 	BIO_free(in);
295 	BIO_free_all(out);
296 	DH_free(dh);
297 
298 	return (ret);
299 }
300 #endif
301