xref: /openbsd-src/usr.bin/openssl/dh.c (revision a9d90585ca40c280201dda70d6b331b26701653a)
1 /* $OpenBSD: dh.c,v 1.16 2025/01/19 10:24:17 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 check;
79 	char *infile;
80 	int informat;
81 	int noout;
82 	char *outfile;
83 	int outformat;
84 	int text;
85 } cfg;
86 
87 static const struct option dh_options[] = {
88 	{
89 		.name = "check",
90 		.desc = "Check the DH parameters",
91 		.type = OPTION_FLAG,
92 		.opt.flag = &cfg.check,
93 	},
94 	{
95 		.name = "in",
96 		.argname = "file",
97 		.desc = "Input file (default stdin)",
98 		.type = OPTION_ARG,
99 		.opt.arg = &cfg.infile,
100 	},
101 	{
102 		.name = "inform",
103 		.argname = "format",
104 		.desc = "Input format (DER or PEM (default))",
105 		.type = OPTION_ARG_FORMAT,
106 		.opt.value = &cfg.informat,
107 	},
108 	{
109 		.name = "noout",
110 		.desc = "No output",
111 		.type = OPTION_FLAG,
112 		.opt.flag = &cfg.noout,
113 	},
114 	{
115 		.name = "out",
116 		.argname = "file",
117 		.desc = "Output file (default stdout)",
118 		.type = OPTION_ARG,
119 		.opt.arg = &cfg.outfile,
120 	},
121 	{
122 		.name = "outform",
123 		.argname = "format",
124 		.desc = "Output format (DER or PEM (default))",
125 		.type = OPTION_ARG_FORMAT,
126 		.opt.value = &cfg.outformat,
127 	},
128 	{
129 		.name = "text",
130 		.desc = "Print a text form of the DH parameters",
131 		.type = OPTION_FLAG,
132 		.opt.flag = &cfg.text,
133 	},
134 	{ NULL },
135 };
136 
137 static void
138 dh_usage(void)
139 {
140 	fprintf(stderr,
141 	    "usage: dh [-check] [-in file] [-inform format]\n"
142 	    "    [-noout] [-out file] [-outform format] [-text]\n\n");
143 	options_usage(dh_options);
144 }
145 
146 int
147 dh_main(int argc, char **argv)
148 {
149 	DH *dh = NULL;
150 	int i;
151 	BIO *in = NULL, *out = NULL;
152 	int ret = 1;
153 
154 	if (pledge("stdio cpath wpath rpath", NULL) == -1) {
155 		perror("pledge");
156 		exit(1);
157 	}
158 
159 	memset(&cfg, 0, sizeof(cfg));
160 
161 	cfg.informat = FORMAT_PEM;
162 	cfg.outformat = FORMAT_PEM;
163 
164 	if (options_parse(argc, argv, dh_options, NULL, NULL) != 0) {
165 		dh_usage();
166 		goto end;
167 	}
168 
169 	in = BIO_new(BIO_s_file());
170 	out = BIO_new(BIO_s_file());
171 	if (in == NULL || out == NULL) {
172 		ERR_print_errors(bio_err);
173 		goto end;
174 	}
175 	if (cfg.infile == NULL)
176 		BIO_set_fp(in, stdin, BIO_NOCLOSE);
177 	else {
178 		if (BIO_read_filename(in, cfg.infile) <= 0) {
179 			perror(cfg.infile);
180 			goto end;
181 		}
182 	}
183 	if (cfg.outfile == NULL) {
184 		BIO_set_fp(out, stdout, BIO_NOCLOSE);
185 	} else {
186 		if (BIO_write_filename(out, cfg.outfile) <= 0) {
187 			perror(cfg.outfile);
188 			goto end;
189 		}
190 	}
191 
192 	if (cfg.informat == FORMAT_ASN1)
193 		dh = d2i_DHparams_bio(in, NULL);
194 	else if (cfg.informat == FORMAT_PEM)
195 		dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
196 	else {
197 		BIO_printf(bio_err, "bad input format specified\n");
198 		goto end;
199 	}
200 	if (dh == NULL) {
201 		BIO_printf(bio_err, "unable to load DH parameters\n");
202 		ERR_print_errors(bio_err);
203 		goto end;
204 	}
205 	if (cfg.text) {
206 		DHparams_print(out, dh);
207 	}
208 	if (cfg.check) {
209 		if (!DH_check(dh, &i)) {
210 			ERR_print_errors(bio_err);
211 			goto end;
212 		}
213 		if (i & DH_CHECK_P_NOT_PRIME)
214 			printf("p value is not prime\n");
215 		if (i & DH_CHECK_P_NOT_SAFE_PRIME)
216 			printf("p value is not a safe prime\n");
217 		if (i & DH_UNABLE_TO_CHECK_GENERATOR)
218 			printf("unable to check the generator value\n");
219 		if (i & DH_NOT_SUITABLE_GENERATOR)
220 			printf("the g value is not a generator\n");
221 		if (i == 0)
222 			printf("DH parameters appear to be ok.\n");
223 	}
224 	if (!cfg.noout) {
225 		if (cfg.outformat == FORMAT_ASN1)
226 			i = i2d_DHparams_bio(out, dh);
227 		else if (cfg.outformat == FORMAT_PEM)
228 			i = PEM_write_bio_DHparams(out, dh);
229 		else {
230 			BIO_printf(bio_err, "bad output format specified for outfile\n");
231 			goto end;
232 		}
233 		if (!i) {
234 			BIO_printf(bio_err, "unable to write DH parameters\n");
235 			ERR_print_errors(bio_err);
236 			goto end;
237 		}
238 	}
239 	ret = 0;
240 
241  end:
242 	BIO_free(in);
243 	BIO_free_all(out);
244 	DH_free(dh);
245 
246 	return (ret);
247 }
248 #endif
249