GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/openssl/dh.c Lines: 48 106 45.3 %
Date: 2017-11-07 Branches: 29 65 44.6 %

Line Branch Exec Source
1
/* $OpenBSD: dh.c,v 1.9 2017/01/20 08:57:11 deraadt 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 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
16
	fprintf(stderr,
148
	    "usage: dh [-C] [-check] [-in file] [-inform format]\n"
149
	    "    [-noout] [-out file] [-outform format] [-text]\n\n");
150
8
	options_usage(dh_options);
151
8
}
152
153
int
154
dh_main(int argc, char **argv)
155
{
156
	DH *dh = NULL;
157
32
	int i;
158
	BIO *in = NULL, *out = NULL;
159
	int ret = 1;
160
161
16
	if (single_execution) {
162
16
		if (pledge("stdio cpath wpath rpath flock", NULL) == -1) {
163
			perror("pledge");
164
			exit(1);
165
		}
166
	}
167
168
16
	memset(&dh_config, 0, sizeof(dh_config));
169
170
16
	dh_config.informat = FORMAT_PEM;
171
16
	dh_config.outformat = FORMAT_PEM;
172
173
16
	if (options_parse(argc, argv, dh_options, NULL, NULL) != 0) {
174
8
		dh_usage();
175
8
		goto end;
176
	}
177
178
8
	in = BIO_new(BIO_s_file());
179
8
	out = BIO_new(BIO_s_file());
180
8
	if (in == NULL || out == NULL) {
181
		ERR_print_errors(bio_err);
182
		goto end;
183
	}
184
8
	if (dh_config.infile == NULL)
185
		BIO_set_fp(in, stdin, BIO_NOCLOSE);
186
	else {
187
8
		if (BIO_read_filename(in, dh_config.infile) <= 0) {
188
			perror(dh_config.infile);
189
			goto end;
190
		}
191
	}
192
8
	if (dh_config.outfile == NULL) {
193
		BIO_set_fp(out, stdout, BIO_NOCLOSE);
194
	} else {
195
8
		if (BIO_write_filename(out, dh_config.outfile) <= 0) {
196
			perror(dh_config.outfile);
197
			goto end;
198
		}
199
	}
200
201
8
	if (dh_config.informat == FORMAT_ASN1)
202
		dh = d2i_DHparams_bio(in, NULL);
203
8
	else if (dh_config.informat == FORMAT_PEM)
204
8
		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
8
	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
8
	if (dh_config.text) {
215
8
		DHparams_print(out, dh);
216
8
	}
217
8
	if (dh_config.check) {
218
8
		if (!DH_check(dh, &i)) {
219
			ERR_print_errors(bio_err);
220
			goto end;
221
		}
222
8
		if (i & DH_CHECK_P_NOT_PRIME)
223
			printf("p value is not prime\n");
224
8
		if (i & DH_CHECK_P_NOT_SAFE_PRIME)
225
			printf("p value is not a safe prime\n");
226
8
		if (i & DH_UNABLE_TO_CHECK_GENERATOR)
227
			printf("unable to check the generator value\n");
228
8
		if (i & DH_NOT_SUITABLE_GENERATOR)
229
			printf("the g value is not a generator\n");
230
8
		if (i == 0)
231
8
			printf("DH parameters appear to be ok.\n");
232
	}
233
8
	if (dh_config.C) {
234
		unsigned char *data;
235
		int len, l, bits;
236
237
		len = BN_num_bytes(dh->p);
238
		bits = BN_num_bits(dh->p);
239
		data = malloc(len);
240
		if (data == NULL) {
241
			perror("malloc");
242
			goto end;
243
		}
244
		l = BN_bn2bin(dh->p, 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->g, 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\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",
266
		    bits, bits);
267
		printf("\tdh->g = BN_bin2bn(dh%d_g, sizeof(dh%d_g), NULL);\n",
268
		    bits, bits);
269
		printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
270
		printf("\t\treturn(NULL);\n");
271
		printf("\treturn(dh);\n\t}\n");
272
		free(data);
273
	}
274
8
	if (!dh_config.noout) {
275
8
		if (dh_config.outformat == FORMAT_ASN1)
276
			i = i2d_DHparams_bio(out, dh);
277
8
		else if (dh_config.outformat == FORMAT_PEM)
278
8
			i = PEM_write_bio_DHparams(out, dh);
279
		else {
280
			BIO_printf(bio_err, "bad output format specified for outfile\n");
281
			goto end;
282
		}
283
8
		if (!i) {
284
			BIO_printf(bio_err, "unable to write DH parameters\n");
285
			ERR_print_errors(bio_err);
286
			goto end;
287
		}
288
	}
289
8
	ret = 0;
290
291
end:
292
16
	BIO_free(in);
293
16
	if (out != NULL)
294
8
		BIO_free_all(out);
295
16
	if (dh != NULL)
296
8
		DH_free(dh);
297
298
16
	return (ret);
299
16
}
300
#endif