summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordoug <>2015-07-12 16:37:37 +0000
committerdoug <>2015-07-12 16:37:37 +0000
commit0cadff7aab820b882c7c857b18c90291340bef1e (patch)
tree8bace69436b73f981b273a7a919eb93d3c0fd6b0
parent7b5c206c69f4bf71acfc7583571a9e43b3ce43e8 (diff)
downloadopenbsd-0cadff7aab820b882c7c857b18c90291340bef1e.tar.gz
openbsd-0cadff7aab820b882c7c857b18c90291340bef1e.tar.bz2
openbsd-0cadff7aab820b882c7c857b18c90291340bef1e.zip
Convert openssl(1) dsaparam to the new option handling.
This also removes support for -timebomb related code which was only enabled for GENCB_TEST. ok jsing@
-rw-r--r--src/usr.bin/openssl/dsaparam.c277
1 files changed, 123 insertions, 154 deletions
diff --git a/src/usr.bin/openssl/dsaparam.c b/src/usr.bin/openssl/dsaparam.c
index ccb533421a..e54e3cbe78 100644
--- a/src/usr.bin/openssl/dsaparam.c
+++ b/src/usr.bin/openssl/dsaparam.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dsaparam.c,v 1.2 2014/08/28 14:23:52 jsing Exp $ */ 1/* $OpenBSD: dsaparam.c,v 1.3 2015/07/12 16:37:37 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 *
@@ -64,7 +64,7 @@
64#undef OPENSSL_NO_DEPRECATED 64#undef OPENSSL_NO_DEPRECATED
65#endif 65#endif
66 66
67 67#include <limits.h>
68#include <stdio.h> 68#include <stdio.h>
69#include <stdlib.h> 69#include <stdlib.h>
70#include <string.h> 70#include <string.h>
@@ -79,32 +79,95 @@
79#include <openssl/pem.h> 79#include <openssl/pem.h>
80#include <openssl/x509.h> 80#include <openssl/x509.h>
81 81
82/* -inform arg - input format - default PEM (DER or PEM) 82static struct {
83 * -outform arg - output format - default PEM 83 int C;
84 * -in arg - input file - default stdin 84#ifndef OPENSSL_NO_ENGINE
85 * -out arg - output file - default stdout 85 char *engine;
86 * -noout 86#endif
87 * -text 87 int genkey;
88 * -C 88 char *infile;
89 * -noout 89 int informat;
90 * -genkey 90 int noout;
91 * #ifdef GENCB_TEST 91 char *outfile;
92 * -timebomb n - interrupt keygen after <n> seconds 92 int outformat;
93 * #endif 93 int text;
94 */ 94} dsaparam_config;
95
96#ifdef GENCB_TEST
97 95
98static int stop_keygen_flag = 0; 96static struct option dsaparam_options[] = {
97 {
98 .name = "C",
99 .desc = "Convert DSA parameters into C code",
100 .type = OPTION_FLAG,
101 .opt.flag = &dsaparam_config.C,
102 },
103#ifndef OPENSSL_NO_ENGINE
104 {
105 .name = "engine",
106 .argname = "id",
107 .desc = "Use the engine specified by the given identifier",
108 .type = OPTION_ARG,
109 .opt.arg = &dsaparam_config.engine,
110 },
111#endif
112 {
113 .name = "genkey",
114 .desc = "Generate a DSA key",
115 .type = OPTION_FLAG,
116 .opt.flag = &dsaparam_config.genkey,
117 },
118 {
119 .name = "in",
120 .argname = "file",
121 .desc = "Input file (default stdin)",
122 .type = OPTION_ARG,
123 .opt.arg = &dsaparam_config.infile,
124 },
125 {
126 .name = "inform",
127 .argname = "format",
128 .desc = "Input format (DER or PEM (default))",
129 .type = OPTION_ARG_FORMAT,
130 .opt.value = &dsaparam_config.informat,
131 },
132 {
133 .name = "noout",
134 .desc = "No output",
135 .type = OPTION_FLAG,
136 .opt.flag = &dsaparam_config.noout,
137 },
138 {
139 .name = "out",
140 .argname = "file",
141 .desc = "Output file (default stdout)",
142 .type = OPTION_ARG,
143 .opt.arg = &dsaparam_config.outfile,
144 },
145 {
146 .name = "outform",
147 .argname = "format",
148 .desc = "Output format (DER or PEM (default))",
149 .type = OPTION_ARG_FORMAT,
150 .opt.value = &dsaparam_config.outformat,
151 },
152 {
153 .name = "text",
154 .desc = "Print as text",
155 .type = OPTION_FLAG,
156 .opt.flag = &dsaparam_config.text,
157 },
158 { NULL },
159};
99 160
100static void 161static void
101timebomb_sigalarm(int foo) 162dsaparam_usage(void)
102{ 163{
103 stop_keygen_flag = 1; 164 fprintf(stderr,
165 "usage: dsaparam [-C] [-engine id] [-genkey] [-in file]\n"
166 " [-inform format] [-noout] [-out file] [-outform format]\n"
167 " [-text] [numbits]\n\n");
168 options_usage(dsaparam_options);
104} 169}
105 170
106#endif
107
108static int dsa_cb(int p, int n, BN_GENCB * cb); 171static int dsa_cb(int p, int n, BN_GENCB * cb);
109 172
110int dsaparam_main(int, char **); 173int dsaparam_main(int, char **);
@@ -113,128 +176,56 @@ int
113dsaparam_main(int argc, char **argv) 176dsaparam_main(int argc, char **argv)
114{ 177{
115 DSA *dsa = NULL; 178 DSA *dsa = NULL;
116 int i, badops = 0, text = 0; 179 int i;
117 BIO *in = NULL, *out = NULL; 180 BIO *in = NULL, *out = NULL;
118 int informat, outformat, noout = 0, C = 0, ret = 1; 181 int ret = 1;
119 char *infile, *outfile, *prog; 182 int numbits = -1;
120 int numbits = -1, num, genkey = 0; 183 char *strbits = NULL;
121#ifndef OPENSSL_NO_ENGINE
122 char *engine = NULL;
123#endif
124#ifdef GENCB_TEST
125 const char *errstr = NULL;
126 int timebomb = 0;
127#endif
128 184
129 infile = NULL; 185 memset(&dsaparam_config, 0, sizeof(dsaparam_config));
130 outfile = NULL;
131 informat = FORMAT_PEM;
132 outformat = FORMAT_PEM;
133 186
134 prog = argv[0]; 187 dsaparam_config.informat = FORMAT_PEM;
135 argc--; 188 dsaparam_config.outformat = FORMAT_PEM;
136 argv++;
137 while (argc >= 1) {
138 if (strcmp(*argv, "-inform") == 0) {
139 if (--argc < 1)
140 goto bad;
141 informat = str2fmt(*(++argv));
142 } else if (strcmp(*argv, "-outform") == 0) {
143 if (--argc < 1)
144 goto bad;
145 outformat = str2fmt(*(++argv));
146 } else if (strcmp(*argv, "-in") == 0) {
147 if (--argc < 1)
148 goto bad;
149 infile = *(++argv);
150 } else if (strcmp(*argv, "-out") == 0) {
151 if (--argc < 1)
152 goto bad;
153 outfile = *(++argv);
154 }
155#ifndef OPENSSL_NO_ENGINE
156 else if (strcmp(*argv, "-engine") == 0) {
157 if (--argc < 1)
158 goto bad;
159 engine = *(++argv);
160 }
161#endif
162#ifdef GENCB_TEST
163 else if (strcmp(*argv, "-timebomb") == 0) {
164 if (--argc < 1)
165 goto bad;
166 timebomb = strtonum(*(++argv), 0, INT_MAX, &errstr);
167 if (errstr)
168 goto bad;
169 }
170#endif
171 else if (strcmp(*argv, "-text") == 0)
172 text = 1;
173 else if (strcmp(*argv, "-C") == 0)
174 C = 1;
175 else if (strcmp(*argv, "-genkey") == 0) {
176 genkey = 1;
177 } else if (strcmp(*argv, "-noout") == 0)
178 noout = 1;
179 else if (sscanf(*argv, "%d", &num) == 1) {
180 /* generate a key */
181 numbits = num;
182 } else {
183 BIO_printf(bio_err, "unknown option %s\n", *argv);
184 badops = 1;
185 break;
186 }
187 argc--;
188 argv++;
189 }
190 189
191 if (badops) { 190 if (options_parse(argc, argv, dsaparam_options, &strbits, NULL) != 0) {
192bad: 191 dsaparam_usage();
193 BIO_printf(bio_err, "%s [options] [bits] <infile >outfile\n", prog);
194 BIO_printf(bio_err, "where options are\n");
195 BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
196 BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
197 BIO_printf(bio_err, " -in arg input file\n");
198 BIO_printf(bio_err, " -out arg output file\n");
199 BIO_printf(bio_err, " -text print as text\n");
200 BIO_printf(bio_err, " -C Output C code\n");
201 BIO_printf(bio_err, " -noout no output\n");
202 BIO_printf(bio_err, " -genkey generate a DSA key\n");
203#ifndef OPENSSL_NO_ENGINE
204 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
205#endif
206#ifdef GENCB_TEST
207 BIO_printf(bio_err, " -timebomb n interrupt keygen after <n> seconds\n");
208#endif
209 BIO_printf(bio_err, " number number of bits to use for generating private key\n");
210 goto end; 192 goto end;
211 } 193 }
212 194
195 if (strbits != NULL) {
196 const char *errstr;
197 numbits = strtonum(strbits, 0, INT_MAX, &errstr);
198 if (errstr) {
199 fprintf(stderr, "Invalid number of bits: %s", errstr);
200 goto end;
201 }
202 }
203
213 in = BIO_new(BIO_s_file()); 204 in = BIO_new(BIO_s_file());
214 out = BIO_new(BIO_s_file()); 205 out = BIO_new(BIO_s_file());
215 if ((in == NULL) || (out == NULL)) { 206 if (in == NULL || out == NULL) {
216 ERR_print_errors(bio_err); 207 ERR_print_errors(bio_err);
217 goto end; 208 goto end;
218 } 209 }
219 if (infile == NULL) 210 if (dsaparam_config.infile == NULL)
220 BIO_set_fp(in, stdin, BIO_NOCLOSE); 211 BIO_set_fp(in, stdin, BIO_NOCLOSE);
221 else { 212 else {
222 if (BIO_read_filename(in, infile) <= 0) { 213 if (BIO_read_filename(in, dsaparam_config.infile) <= 0) {
223 perror(infile); 214 perror(dsaparam_config.infile);
224 goto end; 215 goto end;
225 } 216 }
226 } 217 }
227 if (outfile == NULL) { 218 if (dsaparam_config.outfile == NULL) {
228 BIO_set_fp(out, stdout, BIO_NOCLOSE); 219 BIO_set_fp(out, stdout, BIO_NOCLOSE);
229 } else { 220 } else {
230 if (BIO_write_filename(out, outfile) <= 0) { 221 if (BIO_write_filename(out, dsaparam_config.outfile) <= 0) {
231 perror(outfile); 222 perror(dsaparam_config.outfile);
232 goto end; 223 goto end;
233 } 224 }
234 } 225 }
235 226
236#ifndef OPENSSL_NO_ENGINE 227#ifndef OPENSSL_NO_ENGINE
237 setup_engine(bio_err, engine, 0); 228 setup_engine(bio_err, dsaparam_config.engine, 0);
238#endif 229#endif
239 230
240 if (numbits > 0) { 231 if (numbits > 0) {
@@ -245,38 +236,16 @@ bad:
245 BIO_printf(bio_err, "Error allocating DSA object\n"); 236 BIO_printf(bio_err, "Error allocating DSA object\n");
246 goto end; 237 goto end;
247 } 238 }
248 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num); 239 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", numbits);
249 BIO_printf(bio_err, "This could take some time\n"); 240 BIO_printf(bio_err, "This could take some time\n");
250#ifdef GENCB_TEST 241 if (!DSA_generate_parameters_ex(dsa, numbits, NULL, 0, NULL, NULL, &cb)) {
251 if (timebomb > 0) {
252 struct sigaction act;
253 act.sa_handler = timebomb_sigalarm;
254 act.sa_flags = 0;
255 BIO_printf(bio_err, "(though I'll stop it if not done within %d secs)\n",
256 timebomb);
257 if (sigaction(SIGALRM, &act, NULL) != 0) {
258 BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n");
259 goto end;
260 }
261 alarm(timebomb);
262 }
263#endif
264 if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, &cb)) {
265#ifdef GENCB_TEST
266 if (stop_keygen_flag) {
267 BIO_printf(bio_err, "DSA key generation time-stopped\n");
268 /* This is an asked-for behaviour! */
269 ret = 0;
270 goto end;
271 }
272#endif
273 ERR_print_errors(bio_err); 242 ERR_print_errors(bio_err);
274 BIO_printf(bio_err, "Error, DSA key generation failed\n"); 243 BIO_printf(bio_err, "Error, DSA key generation failed\n");
275 goto end; 244 goto end;
276 } 245 }
277 } else if (informat == FORMAT_ASN1) 246 } else if (dsaparam_config.informat == FORMAT_ASN1)
278 dsa = d2i_DSAparams_bio(in, NULL); 247 dsa = d2i_DSAparams_bio(in, NULL);
279 else if (informat == FORMAT_PEM) 248 else if (dsaparam_config.informat == FORMAT_PEM)
280 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); 249 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
281 else { 250 else {
282 BIO_printf(bio_err, "bad input format specified\n"); 251 BIO_printf(bio_err, "bad input format specified\n");
@@ -287,10 +256,10 @@ bad:
287 ERR_print_errors(bio_err); 256 ERR_print_errors(bio_err);
288 goto end; 257 goto end;
289 } 258 }
290 if (text) { 259 if (dsaparam_config.text) {
291 DSAparams_print(out, dsa); 260 DSAparams_print(out, dsa);
292 } 261 }
293 if (C) { 262 if (dsaparam_config.C) {
294 unsigned char *data; 263 unsigned char *data;
295 int l, len, bits_p; 264 int l, len, bits_p;
296 265
@@ -342,10 +311,10 @@ bad:
342 printf("\t\t{ DSA_free(dsa); return(NULL); }\n"); 311 printf("\t\t{ DSA_free(dsa); return(NULL); }\n");
343 printf("\treturn(dsa);\n\t}\n"); 312 printf("\treturn(dsa);\n\t}\n");
344 } 313 }
345 if (!noout) { 314 if (!dsaparam_config.noout) {
346 if (outformat == FORMAT_ASN1) 315 if (dsaparam_config.outformat == FORMAT_ASN1)
347 i = i2d_DSAparams_bio(out, dsa); 316 i = i2d_DSAparams_bio(out, dsa);
348 else if (outformat == FORMAT_PEM) 317 else if (dsaparam_config.outformat == FORMAT_PEM)
349 i = PEM_write_bio_DSAparams(out, dsa); 318 i = PEM_write_bio_DSAparams(out, dsa);
350 else { 319 else {
351 BIO_printf(bio_err, "bad output format specified for outfile\n"); 320 BIO_printf(bio_err, "bad output format specified for outfile\n");
@@ -357,7 +326,7 @@ bad:
357 goto end; 326 goto end;
358 } 327 }
359 } 328 }
360 if (genkey) { 329 if (dsaparam_config.genkey) {
361 DSA *dsakey; 330 DSA *dsakey;
362 331
363 if ((dsakey = DSAparams_dup(dsa)) == NULL) 332 if ((dsakey = DSAparams_dup(dsa)) == NULL)
@@ -367,9 +336,9 @@ bad:
367 DSA_free(dsakey); 336 DSA_free(dsakey);
368 goto end; 337 goto end;
369 } 338 }
370 if (outformat == FORMAT_ASN1) 339 if (dsaparam_config.outformat == FORMAT_ASN1)
371 i = i2d_DSAPrivateKey_bio(out, dsakey); 340 i = i2d_DSAPrivateKey_bio(out, dsakey);
372 else if (outformat == FORMAT_PEM) 341 else if (dsaparam_config.outformat == FORMAT_PEM)
373 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, NULL); 342 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, NULL);
374 else { 343 else {
375 BIO_printf(bio_err, "bad output format specified for outfile\n"); 344 BIO_printf(bio_err, "bad output format specified for outfile\n");