1 /* $OpenBSD: dh.c,v 1.11 2018/02/07 05:47:55 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 #include <openssl/opensslconf.h> /* for OPENSSL_NO_DH */
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>
88 static struct option dh_options
[] = {
91 .desc
= "Convert DH parameters into C code",
93 .opt
.flag
= &dh_config
.C
,
97 .desc
= "Check the DH parameters",
99 .opt
.flag
= &dh_config
.check
,
104 .desc
= "Input file (default stdin)",
106 .opt
.arg
= &dh_config
.infile
,
111 .desc
= "Input format (DER or PEM (default))",
112 .type
= OPTION_ARG_FORMAT
,
113 .opt
.value
= &dh_config
.informat
,
119 .opt
.flag
= &dh_config
.noout
,
124 .desc
= "Output file (default stdout)",
126 .opt
.arg
= &dh_config
.outfile
,
131 .desc
= "Output format (DER or PEM (default))",
132 .type
= OPTION_ARG_FORMAT
,
133 .opt
.value
= &dh_config
.outformat
,
137 .desc
= "Print a text form of the DH parameters",
139 .opt
.flag
= &dh_config
.text
,
148 "usage: dh [-C] [-check] [-in file] [-inform format]\n"
149 " [-noout] [-out file] [-outform format] [-text]\n\n");
150 options_usage(dh_options
);
154 dh_main(int argc
, char **argv
)
158 BIO
*in
= NULL
, *out
= NULL
;
161 if (single_execution
) {
162 if (pledge("stdio cpath wpath rpath", NULL
) == -1) {
168 memset(&dh_config
, 0, sizeof(dh_config
));
170 dh_config
.informat
= FORMAT_PEM
;
171 dh_config
.outformat
= FORMAT_PEM
;
173 if (options_parse(argc
, argv
, dh_options
, NULL
, NULL
) != 0) {
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
);
184 if (dh_config
.infile
== NULL
)
185 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
187 if (BIO_read_filename(in
, dh_config
.infile
) <= 0) {
188 perror(dh_config
.infile
);
192 if (dh_config
.outfile
== NULL
) {
193 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
195 if (BIO_write_filename(out
, dh_config
.outfile
) <= 0) {
196 perror(dh_config
.outfile
);
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
);
206 BIO_printf(bio_err
, "bad input format specified\n");
210 BIO_printf(bio_err
, "unable to load DH parameters\n");
211 ERR_print_errors(bio_err
);
214 if (dh_config
.text
) {
215 DHparams_print(out
, dh
);
217 if (dh_config
.check
) {
218 if (!DH_check(dh
, &i
)) {
219 ERR_print_errors(bio_err
);
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");
231 printf("DH parameters appear to be ok.\n");
237 len
= BN_num_bytes(dh
->p
);
238 bits
= BN_num_bits(dh
->p
);
244 l
= BN_bn2bin(dh
->p
, data
);
245 printf("static unsigned char dh%d_p[] = {", bits
);
246 for (i
= 0; i
< l
; i
++) {
249 printf("0x%02X, ", data
[i
]);
253 l
= BN_bn2bin(dh
->g
, data
);
254 printf("static unsigned char dh%d_g[] = {", bits
);
255 for (i
= 0; i
< l
; i
++) {
258 printf("0x%02X, ", data
[i
]);
260 printf("\n\t};\n\n");
262 printf("DH *get_dh%d()\n\t{\n", bits
);
263 printf("\tDH *dh;\n\n");
264 printf("\tif ((dh = DH_new()) == NULL) return(NULL);\n");
265 printf("\tdh->p = BN_bin2bn(dh%d_p, sizeof(dh%d_p), NULL);\n",
267 printf("\tdh->g = BN_bin2bn(dh%d_g, sizeof(dh%d_g), NULL);\n",
269 printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
270 printf("\t\treturn(NULL);\n");
271 printf("\treturn(dh);\n\t}\n");
274 if (!dh_config
.noout
) {
275 if (dh_config
.outformat
== FORMAT_ASN1
)
276 i
= i2d_DHparams_bio(out
, dh
);
277 else if (dh_config
.outformat
== FORMAT_PEM
)
278 i
= PEM_write_bio_DHparams(out
, dh
);
280 BIO_printf(bio_err
, "bad output format specified for outfile\n");
284 BIO_printf(bio_err
, "unable to write DH parameters\n");
285 ERR_print_errors(bio_err
);