summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/usr.bin/openssl/dh.c209
1 files changed, 111 insertions, 98 deletions
diff --git a/src/usr.bin/openssl/dh.c b/src/usr.bin/openssl/dh.c
index 4f82143511..436ce1ae0b 100644
--- a/src/usr.bin/openssl/dh.c
+++ b/src/usr.bin/openssl/dh.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dh.c,v 1.3 2015/02/08 10:22:45 doug Exp $ */ 1/* $OpenBSD: dh.c,v 1.4 2015/07/12 22:57:00 doug Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -74,15 +74,93 @@
74#include <openssl/pem.h> 74#include <openssl/pem.h>
75#include <openssl/x509.h> 75#include <openssl/x509.h>
76 76
77/* -inform arg - input format - default PEM (DER or PEM) 77static struct {
78 * -outform arg - output format - default PEM 78 int C;
79 * -in arg - input file - default stdin 79 int check;
80 * -out arg - output file - default stdout 80#ifndef OPENSSL_NO_ENGINE
81 * -check - check the parameters are ok 81 char *engine;
82 * -noout 82#endif
83 * -text 83 char *infile;
84 * -C 84 int informat;
85 */ 85 int noout;
86 char *outfile;
87 int outformat;
88 int text;
89} dh_config;
90
91static struct option dh_options[] = {
92 {
93 .name = "C",
94 .desc = "Convert DH parameters into C code",
95 .type = OPTION_FLAG,
96 .opt.flag = &dh_config.C,
97 },
98 {
99 .name = "check",
100 .desc = "Check the DH parameters",
101 .type = OPTION_FLAG,
102 .opt.flag = &dh_config.check,
103 },
104#ifndef OPENSSL_NO_ENGINE
105 {
106 .name = "engine",
107 .argname = "id",
108 .desc = "Use the engine specified by the given identifier",
109 .type = OPTION_ARG,
110 .opt.arg = &dh_config.engine,
111 },
112#endif
113 {
114 .name = "in",
115 .argname = "file",
116 .desc = "Input file (default stdin)",
117 .type = OPTION_ARG,
118 .opt.arg = &dh_config.infile,
119 },
120 {
121 .name = "inform",
122 .argname = "format",
123 .desc = "Input format (DER or PEM (default))",
124 .type = OPTION_ARG_FORMAT,
125 .opt.value = &dh_config.informat,
126 },
127 {
128 .name = "noout",
129 .desc = "No output",
130 .type = OPTION_FLAG,
131 .opt.flag = &dh_config.noout,
132 },
133 {
134 .name = "out",
135 .argname = "file",
136 .desc = "Output file (default stdout)",
137 .type = OPTION_ARG,
138 .opt.arg = &dh_config.outfile,
139 },
140 {
141 .name = "outform",
142 .argname = "format",
143 .desc = "Output format (DER or PEM (default))",
144 .type = OPTION_ARG_FORMAT,
145 .opt.value = &dh_config.outformat,
146 },
147 {
148 .name = "text",
149 .desc = "Print a text form of the DH parameters",
150 .type = OPTION_FLAG,
151 .opt.flag = &dh_config.text,
152 },
153 { NULL },
154};
155
156static void
157dh_usage(void)
158{
159 fprintf(stderr,
160 "usage: dh [-C] [-check] [-engine id] [-in file] [-inform format]\n"
161 " [-noout] [-out file] [-outform format] [-text]\n\n");
162 options_usage(dh_options);
163}
86 164
87int dh_main(int, char **); 165int dh_main(int, char **);
88 166
@@ -90,115 +168,50 @@ int
90dh_main(int argc, char **argv) 168dh_main(int argc, char **argv)
91{ 169{
92 DH *dh = NULL; 170 DH *dh = NULL;
93 int i, badops = 0, text = 0; 171 int i;
94 BIO *in = NULL, *out = NULL; 172 BIO *in = NULL, *out = NULL;
95 int informat, outformat, check = 0, noout = 0, C = 0, ret = 1; 173 int ret = 1;
96 char *infile, *outfile, *prog;
97#ifndef OPENSSL_NO_ENGINE
98 char *engine;
99#endif
100 174
101#ifndef OPENSSL_NO_ENGINE 175 memset(&dh_config, 0, sizeof(dh_config));
102 engine = NULL;
103#endif
104 infile = NULL;
105 outfile = NULL;
106 informat = FORMAT_PEM;
107 outformat = FORMAT_PEM;
108 176
109 prog = argv[0]; 177 dh_config.informat = FORMAT_PEM;
110 argc--; 178 dh_config.outformat = FORMAT_PEM;
111 argv++;
112 while (argc >= 1) {
113 if (strcmp(*argv, "-inform") == 0) {
114 if (--argc < 1)
115 goto bad;
116 informat = str2fmt(*(++argv));
117 } else if (strcmp(*argv, "-outform") == 0) {
118 if (--argc < 1)
119 goto bad;
120 outformat = str2fmt(*(++argv));
121 } else if (strcmp(*argv, "-in") == 0) {
122 if (--argc < 1)
123 goto bad;
124 infile = *(++argv);
125 } else if (strcmp(*argv, "-out") == 0) {
126 if (--argc < 1)
127 goto bad;
128 outfile = *(++argv);
129 }
130#ifndef OPENSSL_NO_ENGINE
131 else if (strcmp(*argv, "-engine") == 0) {
132 if (--argc < 1)
133 goto bad;
134 engine = *(++argv);
135 }
136#endif
137 else if (strcmp(*argv, "-check") == 0)
138 check = 1;
139 else if (strcmp(*argv, "-text") == 0)
140 text = 1;
141 else if (strcmp(*argv, "-C") == 0)
142 C = 1;
143 else if (strcmp(*argv, "-noout") == 0)
144 noout = 1;
145 else {
146 BIO_printf(bio_err, "unknown option %s\n", *argv);
147 badops = 1;
148 break;
149 }
150 argc--;
151 argv++;
152 }
153 179
154 if (badops) { 180 if (options_parse(argc, argv, dh_options, NULL, NULL) != 0) {
155bad: 181 dh_usage();
156 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
157 BIO_printf(bio_err, "where options are\n");
158 BIO_printf(bio_err, " -inform arg input format - one of DER PEM\n");
159 BIO_printf(bio_err, " -outform arg output format - one of DER PEM\n");
160 BIO_printf(bio_err, " -in arg input file\n");
161 BIO_printf(bio_err, " -out arg output file\n");
162 BIO_printf(bio_err, " -check check the DH parameters\n");
163 BIO_printf(bio_err, " -text print a text form of the DH parameters\n");
164 BIO_printf(bio_err, " -C Output C code\n");
165 BIO_printf(bio_err, " -noout no output\n");
166#ifndef OPENSSL_NO_ENGINE
167 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
168#endif
169 goto end; 182 goto end;
170 } 183 }
171 184
172#ifndef OPENSSL_NO_ENGINE 185#ifndef OPENSSL_NO_ENGINE
173 setup_engine(bio_err, engine, 0); 186 setup_engine(bio_err, dh_config.engine, 0);
174#endif 187#endif
175 188
176 in = BIO_new(BIO_s_file()); 189 in = BIO_new(BIO_s_file());
177 out = BIO_new(BIO_s_file()); 190 out = BIO_new(BIO_s_file());
178 if ((in == NULL) || (out == NULL)) { 191 if (in == NULL || out == NULL) {
179 ERR_print_errors(bio_err); 192 ERR_print_errors(bio_err);
180 goto end; 193 goto end;
181 } 194 }
182 if (infile == NULL) 195 if (dh_config.infile == NULL)
183 BIO_set_fp(in, stdin, BIO_NOCLOSE); 196 BIO_set_fp(in, stdin, BIO_NOCLOSE);
184 else { 197 else {
185 if (BIO_read_filename(in, infile) <= 0) { 198 if (BIO_read_filename(in, dh_config.infile) <= 0) {
186 perror(infile); 199 perror(dh_config.infile);
187 goto end; 200 goto end;
188 } 201 }
189 } 202 }
190 if (outfile == NULL) { 203 if (dh_config.outfile == NULL) {
191 BIO_set_fp(out, stdout, BIO_NOCLOSE); 204 BIO_set_fp(out, stdout, BIO_NOCLOSE);
192 } else { 205 } else {
193 if (BIO_write_filename(out, outfile) <= 0) { 206 if (BIO_write_filename(out, dh_config.outfile) <= 0) {
194 perror(outfile); 207 perror(dh_config.outfile);
195 goto end; 208 goto end;
196 } 209 }
197 } 210 }
198 211
199 if (informat == FORMAT_ASN1) 212 if (dh_config.informat == FORMAT_ASN1)
200 dh = d2i_DHparams_bio(in, NULL); 213 dh = d2i_DHparams_bio(in, NULL);
201 else if (informat == FORMAT_PEM) 214 else if (dh_config.informat == FORMAT_PEM)
202 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); 215 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
203 else { 216 else {
204 BIO_printf(bio_err, "bad input format specified\n"); 217 BIO_printf(bio_err, "bad input format specified\n");
@@ -209,10 +222,10 @@ bad:
209 ERR_print_errors(bio_err); 222 ERR_print_errors(bio_err);
210 goto end; 223 goto end;
211 } 224 }
212 if (text) { 225 if (dh_config.text) {
213 DHparams_print(out, dh); 226 DHparams_print(out, dh);
214 } 227 }
215 if (check) { 228 if (dh_config.check) {
216 if (!DH_check(dh, &i)) { 229 if (!DH_check(dh, &i)) {
217 ERR_print_errors(bio_err); 230 ERR_print_errors(bio_err);
218 goto end; 231 goto end;
@@ -228,7 +241,7 @@ bad:
228 if (i == 0) 241 if (i == 0)
229 printf("DH parameters appear to be ok.\n"); 242 printf("DH parameters appear to be ok.\n");
230 } 243 }
231 if (C) { 244 if (dh_config.C) {
232 unsigned char *data; 245 unsigned char *data;
233 int len, l, bits; 246 int len, l, bits;
234 247
@@ -269,10 +282,10 @@ bad:
269 printf("\treturn(dh);\n\t}\n"); 282 printf("\treturn(dh);\n\t}\n");
270 free(data); 283 free(data);
271 } 284 }
272 if (!noout) { 285 if (!dh_config.noout) {
273 if (outformat == FORMAT_ASN1) 286 if (dh_config.outformat == FORMAT_ASN1)
274 i = i2d_DHparams_bio(out, dh); 287 i = i2d_DHparams_bio(out, dh);
275 else if (outformat == FORMAT_PEM) 288 else if (dh_config.outformat == FORMAT_PEM)
276 i = PEM_write_bio_DHparams(out, dh); 289 i = PEM_write_bio_DHparams(out, dh);
277 else { 290 else {
278 BIO_printf(bio_err, "bad output format specified for outfile\n"); 291 BIO_printf(bio_err, "bad output format specified for outfile\n");