summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2014-04-16 17:04:13 +0000
committerjsing <>2014-04-16 17:04:13 +0000
commitc1004dd77ba8447c75842b490d693604be9c79b9 (patch)
treefa7ca6ee7a48dc85d44002696a082234a4b87335
parentb8d179e6738c9383629b87f386a8d7c447bb254d (diff)
downloadopenbsd-c1004dd77ba8447c75842b490d693604be9c79b9.tar.gz
openbsd-c1004dd77ba8447c75842b490d693604be9c79b9.tar.bz2
openbsd-c1004dd77ba8447c75842b490d693604be9c79b9.zip
Initial KNF.
-rw-r--r--src/lib/libssl/src/apps/dgst.c538
-rw-r--r--src/lib/libssl/src/apps/dh.c324
-rw-r--r--src/lib/libssl/src/apps/dhparam.c474
-rw-r--r--src/lib/libssl/src/apps/dsa.c329
-rw-r--r--src/lib/libssl/src/apps/dsaparam.c484
5 files changed, 1006 insertions, 1143 deletions
diff --git a/src/lib/libssl/src/apps/dgst.c b/src/lib/libssl/src/apps/dgst.c
index 0615fa837b..388c95e5ec 100644
--- a/src/lib/libssl/src/apps/dgst.c
+++ b/src/lib/libssl/src/apps/dgst.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -75,13 +75,13 @@
75#define PROG dgst_main 75#define PROG dgst_main
76 76
77int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, 77int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
78 EVP_PKEY *key, unsigned char *sigin, int siglen, 78 EVP_PKEY *key, unsigned char *sigin, int siglen,
79 const char *sig_name, const char *md_name, 79 const char *sig_name, const char *md_name,
80 const char *file,BIO *bmd); 80 const char *file, BIO *bmd);
81 81
82static void list_md_fn(const EVP_MD *m, 82static void
83 const char *from, const char *to, void *arg) 83list_md_fn(const EVP_MD *m, const char *from, const char *to, void *arg)
84 { 84{
85 const char *mname; 85 const char *mname;
86 /* Skip aliases */ 86 /* Skip aliases */
87 if (!m) 87 if (!m)
@@ -94,27 +94,28 @@ static void list_md_fn(const EVP_MD *m,
94 if (EVP_MD_flags(m) & EVP_MD_FLAG_PKEY_DIGEST) 94 if (EVP_MD_flags(m) & EVP_MD_FLAG_PKEY_DIGEST)
95 return; 95 return;
96 if (strchr(mname, ' ')) 96 if (strchr(mname, ' '))
97 mname= EVP_MD_name(m); 97 mname = EVP_MD_name(m);
98 BIO_printf(arg, "-%-14s to use the %s message digest algorithm\n", 98 BIO_printf(arg, "-%-14s to use the %s message digest algorithm\n",
99 mname, mname); 99 mname, mname);
100 } 100}
101 101
102int MAIN(int, char **); 102int MAIN(int, char **);
103 103
104int MAIN(int argc, char **argv) 104int
105 { 105MAIN(int argc, char **argv)
106{
106 ENGINE *e = NULL; 107 ENGINE *e = NULL;
107 unsigned char *buf=NULL; 108 unsigned char *buf = NULL;
108 int i,err=1; 109 int i, err = 1;
109 const EVP_MD *md=NULL,*m; 110 const EVP_MD *md = NULL, *m;
110 BIO *in=NULL,*inp; 111 BIO *in = NULL, *inp;
111 BIO *bmd=NULL; 112 BIO *bmd = NULL;
112 BIO *out = NULL; 113 BIO *out = NULL;
113#define PROG_NAME_SIZE 39 114#define PROG_NAME_SIZE 39
114 char pname[PROG_NAME_SIZE+1]; 115 char pname[PROG_NAME_SIZE + 1];
115 int separator=0; 116 int separator = 0;
116 int debug=0; 117 int debug = 0;
117 int keyform=FORMAT_PEM; 118 int keyform = FORMAT_PEM;
118 const char *outfile = NULL, *keyfile = NULL; 119 const char *outfile = NULL, *keyfile = NULL;
119 const char *sigfile = NULL, *randfile = NULL; 120 const char *sigfile = NULL, *randfile = NULL;
120 int out_bin = -1, want_pub = 0, do_verify = 0; 121 int out_bin = -1, want_pub = 0, do_verify = 0;
@@ -123,418 +124,374 @@ int MAIN(int argc, char **argv)
123 int siglen = 0; 124 int siglen = 0;
124 char *passargin = NULL, *passin = NULL; 125 char *passargin = NULL, *passin = NULL;
125#ifndef OPENSSL_NO_ENGINE 126#ifndef OPENSSL_NO_ENGINE
126 char *engine=NULL; 127 char *engine = NULL;
127#endif 128#endif
128 char *hmac_key=NULL; 129 char *hmac_key = NULL;
129 char *mac_name=NULL; 130 char *mac_name = NULL;
130 int non_fips_allow = 0; 131 int non_fips_allow = 0;
131 STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL; 132 STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL;
132 133
133 apps_startup(); 134 apps_startup();
134 135
135 if ((buf=(unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL) 136 if ((buf = (unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL) {
136 { 137 BIO_printf(bio_err, "out of memory\n");
137 BIO_printf(bio_err,"out of memory\n");
138 goto end; 138 goto end;
139 } 139 }
140 if (bio_err == NULL) 140 if (bio_err == NULL)
141 if ((bio_err=BIO_new(BIO_s_file())) != NULL) 141 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
142 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 142 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
143 143
144 if (!load_config(bio_err, NULL)) 144 if (!load_config(bio_err, NULL))
145 goto end; 145 goto end;
146 146
147 /* first check the program name */ 147 /* first check the program name */
148 program_name(argv[0],pname,sizeof pname); 148 program_name(argv[0], pname, sizeof pname);
149 149
150 md=EVP_get_digestbyname(pname); 150 md = EVP_get_digestbyname(pname);
151 151
152 argc--; 152 argc--;
153 argv++; 153 argv++;
154 while (argc > 0) 154 while (argc > 0) {
155 { 155 if ((*argv)[0] != '-')
156 if ((*argv)[0] != '-') break; 156 break;
157 if (strcmp(*argv,"-c") == 0) 157 if (strcmp(*argv, "-c") == 0)
158 separator=1; 158 separator = 1;
159 else if (strcmp(*argv,"-r") == 0) 159 else if (strcmp(*argv, "-r") == 0)
160 separator=2; 160 separator = 2;
161 else if (strcmp(*argv,"-rand") == 0) 161 else if (strcmp(*argv, "-rand") == 0) {
162 { 162 if (--argc < 1)
163 if (--argc < 1) break; 163 break;
164 randfile=*(++argv); 164 randfile=*(++argv);
165 } 165 } else if (strcmp(*argv, "-out") == 0) {
166 else if (strcmp(*argv,"-out") == 0) 166 if (--argc < 1)
167 { 167 break;
168 if (--argc < 1) break;
169 outfile=*(++argv); 168 outfile=*(++argv);
170 } 169 } else if (strcmp(*argv, "-sign") == 0) {
171 else if (strcmp(*argv,"-sign") == 0) 170 if (--argc < 1)
172 { 171 break;
173 if (--argc < 1) break;
174 keyfile=*(++argv); 172 keyfile=*(++argv);
175 } 173 } else if (!strcmp(*argv, "-passin")) {
176 else if (!strcmp(*argv,"-passin"))
177 {
178 if (--argc < 1) 174 if (--argc < 1)
179 break; 175 break;
180 passargin=*++argv; 176 passargin=*++argv;
181 } 177 } else if (strcmp(*argv, "-verify") == 0) {
182 else if (strcmp(*argv,"-verify") == 0) 178 if (--argc < 1)
183 { 179 break;
184 if (--argc < 1) break;
185 keyfile=*(++argv); 180 keyfile=*(++argv);
186 want_pub = 1; 181 want_pub = 1;
187 do_verify = 1; 182 do_verify = 1;
188 } 183 } else if (strcmp(*argv, "-prverify") == 0) {
189 else if (strcmp(*argv,"-prverify") == 0) 184 if (--argc < 1)
190 { 185 break;
191 if (--argc < 1) break;
192 keyfile=*(++argv); 186 keyfile=*(++argv);
193 do_verify = 1; 187 do_verify = 1;
194 } 188 } else if (strcmp(*argv, "-signature") == 0) {
195 else if (strcmp(*argv,"-signature") == 0) 189 if (--argc < 1)
196 { 190 break;
197 if (--argc < 1) break;
198 sigfile=*(++argv); 191 sigfile=*(++argv);
199 } 192 } else if (strcmp(*argv, "-keyform") == 0) {
200 else if (strcmp(*argv,"-keyform") == 0) 193 if (--argc < 1)
201 { 194 break;
202 if (--argc < 1) break; 195 keyform = str2fmt(*(++argv));
203 keyform=str2fmt(*(++argv)); 196 }
204 }
205#ifndef OPENSSL_NO_ENGINE 197#ifndef OPENSSL_NO_ENGINE
206 else if (strcmp(*argv,"-engine") == 0) 198 else if (strcmp(*argv, "-engine") == 0) {
207 { 199 if (--argc < 1)
208 if (--argc < 1) break; 200 break;
209 engine= *(++argv); 201 engine= *(++argv);
210 e = setup_engine(bio_err, engine, 0); 202 e = setup_engine(bio_err, engine, 0);
211 } 203 }
212#endif 204#endif
213 else if (strcmp(*argv,"-hex") == 0) 205 else if (strcmp(*argv, "-hex") == 0)
214 out_bin = 0; 206 out_bin = 0;
215 else if (strcmp(*argv,"-binary") == 0) 207 else if (strcmp(*argv, "-binary") == 0)
216 out_bin = 1; 208 out_bin = 1;
217 else if (strcmp(*argv,"-d") == 0) 209 else if (strcmp(*argv, "-d") == 0)
218 debug=1; 210 debug = 1;
219 else if (!strcmp(*argv,"-fips-fingerprint")) 211 else if (!strcmp(*argv, "-fips-fingerprint"))
220 hmac_key = "etaonrishdlcupfm"; 212 hmac_key = "etaonrishdlcupfm";
221 else if (strcmp(*argv,"-non-fips-allow") == 0) 213 else if (strcmp(*argv, "-non-fips-allow") == 0)
222 non_fips_allow=1; 214 non_fips_allow = 1;
223 else if (!strcmp(*argv,"-hmac")) 215 else if (!strcmp(*argv, "-hmac")) {
224 {
225 if (--argc < 1) 216 if (--argc < 1)
226 break; 217 break;
227 hmac_key=*++argv; 218 hmac_key=*++argv;
228 } 219 } else if (!strcmp(*argv, "-mac")) {
229 else if (!strcmp(*argv,"-mac"))
230 {
231 if (--argc < 1) 220 if (--argc < 1)
232 break; 221 break;
233 mac_name=*++argv; 222 mac_name=*++argv;
234 } 223 } else if (strcmp(*argv, "-sigopt") == 0) {
235 else if (strcmp(*argv,"-sigopt") == 0)
236 {
237 if (--argc < 1) 224 if (--argc < 1)
238 break; 225 break;
239 if (!sigopts) 226 if (!sigopts)
240 sigopts = sk_OPENSSL_STRING_new_null(); 227 sigopts = sk_OPENSSL_STRING_new_null();
241 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv))) 228 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
242 break; 229 break;
243 } 230 } else if (strcmp(*argv, "-macopt") == 0) {
244 else if (strcmp(*argv,"-macopt") == 0)
245 {
246 if (--argc < 1) 231 if (--argc < 1)
247 break; 232 break;
248 if (!macopts) 233 if (!macopts)
249 macopts = sk_OPENSSL_STRING_new_null(); 234 macopts = sk_OPENSSL_STRING_new_null();
250 if (!macopts || !sk_OPENSSL_STRING_push(macopts, *(++argv))) 235 if (!macopts || !sk_OPENSSL_STRING_push(macopts, *(++argv)))
251 break; 236 break;
252 } 237 } else if ((m = EVP_get_digestbyname(&((*argv)[1]))) != NULL)
253 else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL) 238 md = m;
254 md=m;
255 else 239 else
256 break; 240 break;
257 argc--; 241 argc--;
258 argv++; 242 argv++;
259 } 243 }
260 244
261 245
262 if(do_verify && !sigfile) { 246 if (do_verify && !sigfile) {
263 BIO_printf(bio_err, "No signature to verify: use the -signature option\n"); 247 BIO_printf(bio_err, "No signature to verify: use the -signature option\n");
264 goto end; 248 goto end;
265 } 249 }
266 250
267 if ((argc > 0) && (argv[0][0] == '-')) /* bad option */ 251 if ((argc > 0) && (argv[0][0] == '-')) /* bad option */
268 { 252 {
269 BIO_printf(bio_err,"unknown option '%s'\n",*argv); 253 BIO_printf(bio_err, "unknown option '%s'\n", *argv);
270 BIO_printf(bio_err,"options are\n"); 254 BIO_printf(bio_err, "options are\n");
271 BIO_printf(bio_err,"-c to output the digest with separating colons\n"); 255 BIO_printf(bio_err, "-c to output the digest with separating colons\n");
272 BIO_printf(bio_err,"-r to output the digest in coreutils format\n"); 256 BIO_printf(bio_err, "-r to output the digest in coreutils format\n");
273 BIO_printf(bio_err,"-d to output debug info\n"); 257 BIO_printf(bio_err, "-d to output debug info\n");
274 BIO_printf(bio_err,"-hex output as hex dump\n"); 258 BIO_printf(bio_err, "-hex output as hex dump\n");
275 BIO_printf(bio_err,"-binary output in binary form\n"); 259 BIO_printf(bio_err, "-binary output in binary form\n");
276 BIO_printf(bio_err,"-sign file sign digest using private key in file\n"); 260 BIO_printf(bio_err, "-sign file sign digest using private key in file\n");
277 BIO_printf(bio_err,"-verify file verify a signature using public key in file\n"); 261 BIO_printf(bio_err, "-verify file verify a signature using public key in file\n");
278 BIO_printf(bio_err,"-prverify file verify a signature using private key in file\n"); 262 BIO_printf(bio_err, "-prverify file verify a signature using private key in file\n");
279 BIO_printf(bio_err,"-keyform arg key file format (PEM or ENGINE)\n"); 263 BIO_printf(bio_err, "-keyform arg key file format (PEM or ENGINE)\n");
280 BIO_printf(bio_err,"-out filename output to filename rather than stdout\n"); 264 BIO_printf(bio_err, "-out filename output to filename rather than stdout\n");
281 BIO_printf(bio_err,"-signature file signature to verify\n"); 265 BIO_printf(bio_err, "-signature file signature to verify\n");
282 BIO_printf(bio_err,"-sigopt nm:v signature parameter\n"); 266 BIO_printf(bio_err, "-sigopt nm:v signature parameter\n");
283 BIO_printf(bio_err,"-hmac key create hashed MAC with key\n"); 267 BIO_printf(bio_err, "-hmac key create hashed MAC with key\n");
284 BIO_printf(bio_err,"-mac algorithm create MAC (not neccessarily HMAC)\n"); 268 BIO_printf(bio_err, "-mac algorithm create MAC (not neccessarily HMAC)\n");
285 BIO_printf(bio_err,"-macopt nm:v MAC algorithm parameters or key\n"); 269 BIO_printf(bio_err, "-macopt nm:v MAC algorithm parameters or key\n");
286#ifndef OPENSSL_NO_ENGINE 270#ifndef OPENSSL_NO_ENGINE
287 BIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\n"); 271 BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
288#endif 272#endif
289 273
290 EVP_MD_do_all_sorted(list_md_fn, bio_err); 274 EVP_MD_do_all_sorted(list_md_fn, bio_err);
291 goto end; 275 goto end;
292 } 276 }
293 277
294 in=BIO_new(BIO_s_file()); 278 in = BIO_new(BIO_s_file());
295 bmd=BIO_new(BIO_f_md()); 279 bmd = BIO_new(BIO_f_md());
296 if (debug) 280 if (debug) {
297 { 281 BIO_set_callback(in, BIO_debug_callback);
298 BIO_set_callback(in,BIO_debug_callback);
299 /* needed for windows 3.1 */ 282 /* needed for windows 3.1 */
300 BIO_set_callback_arg(in,(char *)bio_err); 283 BIO_set_callback_arg(in, (char *)bio_err);
301 } 284 }
302 285
303 if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) 286 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
304 {
305 BIO_printf(bio_err, "Error getting password\n"); 287 BIO_printf(bio_err, "Error getting password\n");
306 goto end; 288 goto end;
307 } 289 }
308 290
309 if ((in == NULL) || (bmd == NULL)) 291 if ((in == NULL) || (bmd == NULL)) {
310 {
311 ERR_print_errors(bio_err); 292 ERR_print_errors(bio_err);
312 goto end; 293 goto end;
313 } 294 }
314 295
315 if(out_bin == -1) { 296 if (out_bin == -1) {
316 if(keyfile) 297 if (keyfile)
317 out_bin = 1; 298 out_bin = 1;
318 else 299 else
319 out_bin = 0; 300 out_bin = 0;
320 } 301 }
321 302
322 if(randfile) 303 if (randfile)
323 app_RAND_load_file(randfile, bio_err, 0); 304 app_RAND_load_file(randfile, bio_err, 0);
324 305
325 if(outfile) { 306 if (outfile) {
326 if(out_bin) 307 if (out_bin)
327 out = BIO_new_file(outfile, "wb"); 308 out = BIO_new_file(outfile, "wb");
328 else out = BIO_new_file(outfile, "w"); 309 else
310 out = BIO_new_file(outfile, "w");
329 } else { 311 } else {
330 out = BIO_new_fp(stdout, BIO_NOCLOSE); 312 out = BIO_new_fp(stdout, BIO_NOCLOSE);
331 } 313 }
332 314
333 if(!out) { 315 if (!out) {
334 BIO_printf(bio_err, "Error opening output file %s\n", 316 BIO_printf(bio_err, "Error opening output file %s\n",
335 outfile ? outfile : "(stdout)"); 317 outfile ? outfile : "(stdout)");
336 ERR_print_errors(bio_err); 318 ERR_print_errors(bio_err);
337 goto end; 319 goto end;
338 } 320 }
339 if ((!!mac_name + !!keyfile + !!hmac_key) > 1) 321 if ((!!mac_name + !!keyfile + !!hmac_key) > 1) {
340 {
341 BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n"); 322 BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
342 goto end; 323 goto end;
343 } 324 }
344 325
345 if(keyfile) 326 if (keyfile) {
346 {
347 if (want_pub) 327 if (want_pub)
348 sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL, 328 sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
349 e, "key file"); 329 e, "key file");
350 else 330 else
351 sigkey = load_key(bio_err, keyfile, keyform, 0, passin, 331 sigkey = load_key(bio_err, keyfile, keyform, 0, passin,
352 e, "key file"); 332 e, "key file");
353 if (!sigkey) 333 if (!sigkey) {
354 {
355 /* load_[pub]key() has already printed an appropriate 334 /* load_[pub]key() has already printed an appropriate
356 message */ 335 message */
357 goto end; 336 goto end;
358 }
359 } 337 }
338 }
360 339
361 if (mac_name) 340 if (mac_name) {
362 {
363 EVP_PKEY_CTX *mac_ctx = NULL; 341 EVP_PKEY_CTX *mac_ctx = NULL;
364 int r = 0; 342 int r = 0;
365 if (!init_gen_str(bio_err, &mac_ctx, mac_name,e, 0)) 343 if (!init_gen_str(bio_err, &mac_ctx, mac_name, e, 0))
366 goto mac_end; 344 goto mac_end;
367 if (macopts) 345 if (macopts) {
368 {
369 char *macopt; 346 char *macopt;
370 for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) 347 for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
371 {
372 macopt = sk_OPENSSL_STRING_value(macopts, i); 348 macopt = sk_OPENSSL_STRING_value(macopts, i);
373 if (pkey_ctrl_string(mac_ctx, macopt) <= 0) 349 if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
374 {
375 BIO_printf(bio_err, 350 BIO_printf(bio_err,
376 "MAC parameter error \"%s\"\n", 351 "MAC parameter error \"%s\"\n",
377 macopt); 352 macopt);
378 ERR_print_errors(bio_err); 353 ERR_print_errors(bio_err);
379 goto mac_end; 354 goto mac_end;
380 }
381 } 355 }
382 } 356 }
383 if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) 357 }
384 { 358 if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
385 BIO_puts(bio_err, "Error generating key\n"); 359 BIO_puts(bio_err, "Error generating key\n");
386 ERR_print_errors(bio_err); 360 ERR_print_errors(bio_err);
387 goto mac_end; 361 goto mac_end;
388 } 362 }
389 r = 1; 363 r = 1;
390 mac_end: 364 mac_end:
391 if (mac_ctx) 365 if (mac_ctx)
392 EVP_PKEY_CTX_free(mac_ctx); 366 EVP_PKEY_CTX_free(mac_ctx);
393 if (r == 0) 367 if (r == 0)
394 goto end; 368 goto end;
395 } 369 }
396 370
397 if (non_fips_allow) 371 if (non_fips_allow) {
398 {
399 EVP_MD_CTX *md_ctx; 372 EVP_MD_CTX *md_ctx;
400 BIO_get_md_ctx(bmd,&md_ctx); 373 BIO_get_md_ctx(bmd, &md_ctx);
401 EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 374 EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
402 } 375 }
403 376
404 if (hmac_key) 377 if (hmac_key) {
405 {
406 sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, e, 378 sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, e,
407 (unsigned char *)hmac_key, -1); 379 (unsigned char *)hmac_key, -1);
408 if (!sigkey) 380 if (!sigkey)
409 goto end; 381 goto end;
410 } 382 }
411 383
412 if (sigkey) 384 if (sigkey) {
413 {
414 EVP_MD_CTX *mctx = NULL; 385 EVP_MD_CTX *mctx = NULL;
415 EVP_PKEY_CTX *pctx = NULL; 386 EVP_PKEY_CTX *pctx = NULL;
416 int r; 387 int r;
417 if (!BIO_get_md_ctx(bmd, &mctx)) 388 if (!BIO_get_md_ctx(bmd, &mctx)) {
418 {
419 BIO_printf(bio_err, "Error getting context\n"); 389 BIO_printf(bio_err, "Error getting context\n");
420 ERR_print_errors(bio_err); 390 ERR_print_errors(bio_err);
421 goto end; 391 goto end;
422 } 392 }
423 if (do_verify) 393 if (do_verify)
424 r = EVP_DigestVerifyInit(mctx, &pctx, md, NULL, sigkey); 394 r = EVP_DigestVerifyInit(mctx, &pctx, md, NULL, sigkey);
425 else 395 else
426 r = EVP_DigestSignInit(mctx, &pctx, md, NULL, sigkey); 396 r = EVP_DigestSignInit(mctx, &pctx, md, NULL, sigkey);
427 if (!r) 397 if (!r) {
428 {
429 BIO_printf(bio_err, "Error setting context\n"); 398 BIO_printf(bio_err, "Error setting context\n");
430 ERR_print_errors(bio_err); 399 ERR_print_errors(bio_err);
431 goto end; 400 goto end;
432 } 401 }
433 if (sigopts) 402 if (sigopts) {
434 {
435 char *sigopt; 403 char *sigopt;
436 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) 404 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
437 {
438 sigopt = sk_OPENSSL_STRING_value(sigopts, i); 405 sigopt = sk_OPENSSL_STRING_value(sigopts, i);
439 if (pkey_ctrl_string(pctx, sigopt) <= 0) 406 if (pkey_ctrl_string(pctx, sigopt) <= 0) {
440 {
441 BIO_printf(bio_err, 407 BIO_printf(bio_err,
442 "parameter error \"%s\"\n", 408 "parameter error \"%s\"\n",
443 sigopt); 409 sigopt);
444 ERR_print_errors(bio_err); 410 ERR_print_errors(bio_err);
445 goto end; 411 goto end;
446 }
447 } 412 }
448 } 413 }
449 } 414 }
415 }
450 /* we use md as a filter, reading from 'in' */ 416 /* we use md as a filter, reading from 'in' */
451 else 417 else {
452 {
453 if (md == NULL) 418 if (md == NULL)
454 md = EVP_md5(); 419 md = EVP_md5();
455 if (!BIO_set_md(bmd,md)) 420 if (!BIO_set_md(bmd, md)) {
456 {
457 BIO_printf(bio_err, "Error setting digest %s\n", pname); 421 BIO_printf(bio_err, "Error setting digest %s\n", pname);
458 ERR_print_errors(bio_err); 422 ERR_print_errors(bio_err);
459 goto end; 423 goto end;
460 }
461 } 424 }
425 }
462 426
463 if(sigfile && sigkey) { 427 if (sigfile && sigkey) {
464 BIO *sigbio; 428 BIO *sigbio;
465 sigbio = BIO_new_file(sigfile, "rb"); 429 sigbio = BIO_new_file(sigfile, "rb");
466 siglen = EVP_PKEY_size(sigkey); 430 siglen = EVP_PKEY_size(sigkey);
467 sigbuf = OPENSSL_malloc(siglen); 431 sigbuf = OPENSSL_malloc(siglen);
468 if(!sigbio) { 432 if (!sigbio) {
469 BIO_printf(bio_err, "Error opening signature file %s\n", 433 BIO_printf(bio_err, "Error opening signature file %s\n",
470 sigfile); 434 sigfile);
471 ERR_print_errors(bio_err); 435 ERR_print_errors(bio_err);
472 goto end; 436 goto end;
473 } 437 }
474 siglen = BIO_read(sigbio, sigbuf, siglen); 438 siglen = BIO_read(sigbio, sigbuf, siglen);
475 BIO_free(sigbio); 439 BIO_free(sigbio);
476 if(siglen <= 0) { 440 if (siglen <= 0) {
477 BIO_printf(bio_err, "Error reading signature file %s\n", 441 BIO_printf(bio_err, "Error reading signature file %s\n",
478 sigfile); 442 sigfile);
479 ERR_print_errors(bio_err); 443 ERR_print_errors(bio_err);
480 goto end; 444 goto end;
481 } 445 }
482 } 446 }
483 inp=BIO_push(bmd,in); 447 inp = BIO_push(bmd, in);
484 448
485 if (md == NULL) 449 if (md == NULL) {
486 {
487 EVP_MD_CTX *tctx; 450 EVP_MD_CTX *tctx;
488 BIO_get_md_ctx(bmd, &tctx); 451 BIO_get_md_ctx(bmd, &tctx);
489 md = EVP_MD_CTX_md(tctx); 452 md = EVP_MD_CTX_md(tctx);
490 } 453 }
491 454
492 if (argc == 0) 455 if (argc == 0) {
493 { 456 BIO_set_fp(in, stdin, BIO_NOCLOSE);
494 BIO_set_fp(in,stdin,BIO_NOCLOSE); 457 err = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
495 err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf, 458 siglen, NULL, NULL, "stdin", bmd);
496 siglen,NULL,NULL,"stdin",bmd); 459 } else {
497 }
498 else
499 {
500 const char *md_name = NULL, *sig_name = NULL; 460 const char *md_name = NULL, *sig_name = NULL;
501 if(!out_bin) 461 if (!out_bin) {
502 { 462 if (sigkey) {
503 if (sigkey)
504 {
505 const EVP_PKEY_ASN1_METHOD *ameth; 463 const EVP_PKEY_ASN1_METHOD *ameth;
506 ameth = EVP_PKEY_get0_asn1(sigkey); 464 ameth = EVP_PKEY_get0_asn1(sigkey);
507 if (ameth) 465 if (ameth)
508 EVP_PKEY_asn1_get0_info(NULL, NULL, 466 EVP_PKEY_asn1_get0_info(NULL, NULL,
509 NULL, NULL, &sig_name, ameth); 467 NULL, NULL, &sig_name, ameth);
510 }
511 md_name = EVP_MD_name(md);
512 } 468 }
469 md_name = EVP_MD_name(md);
470 }
513 err = 0; 471 err = 0;
514 for (i=0; i<argc; i++) 472 for (i = 0; i < argc; i++) {
515 {
516 int r; 473 int r;
517 if (BIO_read_filename(in,argv[i]) <= 0) 474 if (BIO_read_filename(in, argv[i]) <= 0) {
518 {
519 perror(argv[i]); 475 perror(argv[i]);
520 err++; 476 err++;
521 continue; 477 continue;
522 } 478 } else
523 else 479 r = do_fp(out, buf, inp, separator, out_bin,
524 r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf, 480 sigkey, sigbuf, siglen, sig_name, md_name,
525 siglen,sig_name,md_name, argv[i],bmd); 481 argv[i], bmd);
526 if(r) 482 if (r)
527 err=r; 483 err = r;
528 (void)BIO_reset(bmd); 484 (void)BIO_reset(bmd);
529 }
530 } 485 }
486 }
487
531end: 488end:
532 if (buf != NULL) 489 if (buf != NULL) {
533 { 490 OPENSSL_cleanse(buf, BUFSIZE);
534 OPENSSL_cleanse(buf,BUFSIZE);
535 OPENSSL_free(buf); 491 OPENSSL_free(buf);
536 } 492 }
537 if (in != NULL) BIO_free(in); 493 if (in != NULL)
494 BIO_free(in);
538 if (passin) 495 if (passin)
539 OPENSSL_free(passin); 496 OPENSSL_free(passin);
540 BIO_free_all(out); 497 BIO_free_all(out);
@@ -543,96 +500,83 @@ end:
543 sk_OPENSSL_STRING_free(sigopts); 500 sk_OPENSSL_STRING_free(sigopts);
544 if (macopts) 501 if (macopts)
545 sk_OPENSSL_STRING_free(macopts); 502 sk_OPENSSL_STRING_free(macopts);
546 if(sigbuf) OPENSSL_free(sigbuf); 503 if (sigbuf) OPENSSL_free(sigbuf);
547 if (bmd != NULL) BIO_free(bmd); 504 if (bmd != NULL)
505 BIO_free(bmd);
548 apps_shutdown(); 506 apps_shutdown();
549 OPENSSL_EXIT(err); 507 OPENSSL_EXIT(err);
550 } 508}
551 509
552int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, 510int
553 EVP_PKEY *key, unsigned char *sigin, int siglen, 511do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
554 const char *sig_name, const char *md_name, 512 EVP_PKEY *key, unsigned char *sigin, int siglen,
555 const char *file,BIO *bmd) 513 const char *sig_name, const char *md_name,
556 { 514 const char *file, BIO *bmd)
515{
557 size_t len; 516 size_t len;
558 int i; 517 int i;
559 518
560 for (;;) 519 for (;;) {
561 { 520 i = BIO_read(bp, (char *)buf, BUFSIZE);
562 i=BIO_read(bp,(char *)buf,BUFSIZE); 521 if (i < 0) {
563 if(i < 0) 522 BIO_printf(bio_err, "Read Error in %s\n", file);
564 {
565 BIO_printf(bio_err, "Read Error in %s\n",file);
566 ERR_print_errors(bio_err); 523 ERR_print_errors(bio_err);
567 return 1; 524 return 1;
568 }
569 if (i == 0) break;
570 } 525 }
571 if(sigin) 526 if (i == 0)
572 { 527 break;
528 }
529 if (sigin) {
573 EVP_MD_CTX *ctx; 530 EVP_MD_CTX *ctx;
574 BIO_get_md_ctx(bp, &ctx); 531 BIO_get_md_ctx(bp, &ctx);
575 i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen); 532 i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
576 if(i > 0) 533 if (i > 0)
577 BIO_printf(out, "Verified OK\n"); 534 BIO_printf(out, "Verified OK\n");
578 else if(i == 0) 535 else if (i == 0) {
579 {
580 BIO_printf(out, "Verification Failure\n"); 536 BIO_printf(out, "Verification Failure\n");
581 return 1; 537 return 1;
582 } 538 } else {
583 else
584 {
585 BIO_printf(bio_err, "Error Verifying Data\n"); 539 BIO_printf(bio_err, "Error Verifying Data\n");
586 ERR_print_errors(bio_err); 540 ERR_print_errors(bio_err);
587 return 1; 541 return 1;
588 }
589 return 0;
590 } 542 }
591 if(key) 543 return 0;
592 { 544 }
545 if (key) {
593 EVP_MD_CTX *ctx; 546 EVP_MD_CTX *ctx;
594 BIO_get_md_ctx(bp, &ctx); 547 BIO_get_md_ctx(bp, &ctx);
595 len = BUFSIZE; 548 len = BUFSIZE;
596 if(!EVP_DigestSignFinal(ctx, buf, &len)) 549 if (!EVP_DigestSignFinal(ctx, buf, &len)) {
597 {
598 BIO_printf(bio_err, "Error Signing Data\n"); 550 BIO_printf(bio_err, "Error Signing Data\n");
599 ERR_print_errors(bio_err); 551 ERR_print_errors(bio_err);
600 return 1; 552 return 1;
601 }
602 } 553 }
603 else 554 } else {
604 { 555 len = BIO_gets(bp, (char *)buf, BUFSIZE);
605 len=BIO_gets(bp,(char *)buf,BUFSIZE); 556 if ((int)len < 0) {
606 if ((int)len <0)
607 {
608 ERR_print_errors(bio_err); 557 ERR_print_errors(bio_err);
609 return 1; 558 return 1;
610 }
611 } 559 }
560 }
612 561
613 if(binout) BIO_write(out, buf, len); 562 if (binout) BIO_write(out, buf, len);
614 else if (sep == 2) 563 else if (sep == 2) {
615 { 564 for (i = 0; i < (int)len; i++)
616 for (i=0; i<(int)len; i++) 565 BIO_printf(out, "%02x", buf[i]);
617 BIO_printf(out, "%02x",buf[i]);
618 BIO_printf(out, " *%s\n", file); 566 BIO_printf(out, " *%s\n", file);
619 } 567 } else {
620 else
621 {
622 if (sig_name) 568 if (sig_name)
623 BIO_printf(out, "%s-%s(%s)= ", sig_name, md_name, file); 569 BIO_printf(out, "%s-%s(%s)= ", sig_name, md_name, file);
624 else if (md_name) 570 else if (md_name)
625 BIO_printf(out, "%s(%s)= ", md_name, file); 571 BIO_printf(out, "%s(%s)= ", md_name, file);
626 else 572 else
627 BIO_printf(out, "(%s)= ", file); 573 BIO_printf(out, "(%s)= ", file);
628 for (i=0; i<(int)len; i++) 574 for (i = 0; i < (int)len; i++) {
629 {
630 if (sep && (i != 0)) 575 if (sep && (i != 0))
631 BIO_printf(out, ":"); 576 BIO_printf(out, ":");
632 BIO_printf(out, "%02x",buf[i]); 577 BIO_printf(out, "%02x", buf[i]);
633 }
634 BIO_printf(out, "\n");
635 } 578 }
636 return 0; 579 BIO_printf(out, "\n");
637 } 580 }
638 581 return 0;
582}
diff --git a/src/lib/libssl/src/apps/dh.c b/src/lib/libssl/src/apps/dh.c
index 5f534261ed..0ad7121b37 100644
--- a/src/lib/libssl/src/apps/dh.c
+++ b/src/lib/libssl/src/apps/dh.c
@@ -6,21 +6,21 @@
6 * This package is an SSL implementation written 6 * This package is an SSL implementation written
7 * by Eric Young (eay@cryptsoft.com). 7 * by Eric Young (eay@cryptsoft.com).
8 * The implementation was written so as to conform with Netscapes SSL. 8 * The implementation was written so as to conform with Netscapes SSL.
9 * 9 *
10 * This library is free for commercial and non-commercial use as long as 10 * This library is free for commercial and non-commercial use as long as
11 * the following conditions are aheared to. The following conditions 11 * the following conditions are aheared to. The following conditions
12 * apply to all code found in this distribution, be it the RC4, RSA, 12 * apply to all code found in this distribution, be it the RC4, RSA,
13 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
14 * included with this distribution is covered by the same copyright terms 14 * included with this distribution is covered by the same copyright terms
15 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * 16 *
17 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * Copyright remains Eric Young's, and as such any Copyright notices in
18 * the code are not to be removed. 18 * the code are not to be removed.
19 * If this package is used in a product, Eric Young should be given attribution 19 * If this package is used in a product, Eric Young should be given attribution
20 * as the author of the parts of the library used. 20 * as the author of the parts of the library used.
21 * This can be in the form of a textual message at program startup or 21 * This can be in the form of a textual message at program startup or
22 * in documentation (online or textual) provided with the package. 22 * in documentation (online or textual) provided with the package.
23 * 23 *
24 * Redistribution and use in source and binary forms, with or without 24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions 25 * modification, are permitted provided that the following conditions
26 * are met: 26 * are met:
@@ -35,10 +35,10 @@
35 * Eric Young (eay@cryptsoft.com)" 35 * Eric Young (eay@cryptsoft.com)"
36 * The word 'cryptographic' can be left out if the rouines from the library 36 * The word 'cryptographic' can be left out if the rouines from the library
37 * being used are not cryptographic related :-). 37 * being used are not cryptographic related :-).
38 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * 4. If you include any Windows specific code (or a derivative thereof) from
39 * the apps directory (application code) you must include an acknowledgement: 39 * the apps directory (application code) you must include an acknowledgement:
40 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * 41 *
42 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -50,7 +50,7 @@
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE. 52 * SUCH DAMAGE.
53 * 53 *
54 * The licence and distribution terms for any publically available version or 54 * The licence and distribution terms for any publically available version or
55 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * derivative of this code cannot be changed. i.e. this code cannot simply be
56 * copied and put under another distribution licence 56 * copied and put under another distribution licence
@@ -86,13 +86,14 @@
86 86
87int MAIN(int, char **); 87int MAIN(int, char **);
88 88
89int MAIN(int argc, char **argv) 89int
90 { 90MAIN(int argc, char **argv)
91 DH *dh=NULL; 91{
92 int i,badops=0,text=0; 92 DH *dh = NULL;
93 BIO *in=NULL,*out=NULL; 93 int i, badops = 0, text = 0;
94 int informat,outformat,check=0,noout=0,C=0,ret=1; 94 BIO *in = NULL, *out = NULL;
95 char *infile,*outfile,*prog; 95 int informat, outformat, check = 0, noout = 0, C = 0, ret = 1;
96 char *infile, *outfile, *prog;
96#ifndef OPENSSL_NO_ENGINE 97#ifndef OPENSSL_NO_ENGINE
97 char *engine; 98 char *engine;
98#endif 99#endif
@@ -100,165 +101,148 @@ int MAIN(int argc, char **argv)
100 apps_startup(); 101 apps_startup();
101 102
102 if (bio_err == NULL) 103 if (bio_err == NULL)
103 if ((bio_err=BIO_new(BIO_s_file())) != NULL) 104 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
104 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 105 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
105 106
106 if (!load_config(bio_err, NULL)) 107 if (!load_config(bio_err, NULL))
107 goto end; 108 goto end;
108 109
109#ifndef OPENSSL_NO_ENGINE 110#ifndef OPENSSL_NO_ENGINE
110 engine=NULL; 111 engine = NULL;
111#endif 112#endif
112 infile=NULL; 113 infile = NULL;
113 outfile=NULL; 114 outfile = NULL;
114 informat=FORMAT_PEM; 115 informat = FORMAT_PEM;
115 outformat=FORMAT_PEM; 116 outformat = FORMAT_PEM;
116 117
117 prog=argv[0]; 118 prog = argv[0];
118 argc--; 119 argc--;
119 argv++; 120 argv++;
120 while (argc >= 1) 121 while (argc >= 1) {
121 { 122 if (strcmp(*argv, "-inform") == 0) {
122 if (strcmp(*argv,"-inform") == 0) 123 if (--argc < 1)
123 { 124 goto bad;
124 if (--argc < 1) goto bad; 125 informat = str2fmt(*(++argv));
125 informat=str2fmt(*(++argv)); 126 }
126 } 127 else if (strcmp(*argv, "-outform") == 0) {
127 else if (strcmp(*argv,"-outform") == 0) 128 if (--argc < 1)
128 { 129 goto bad;
129 if (--argc < 1) goto bad; 130 outformat = str2fmt(*(++argv));
130 outformat=str2fmt(*(++argv)); 131 }
131 } 132 else if (strcmp(*argv, "-in") == 0) {
132 else if (strcmp(*argv,"-in") == 0) 133 if (--argc < 1)
133 { 134 goto bad;
134 if (--argc < 1) goto bad;
135 infile= *(++argv); 135 infile= *(++argv);
136 } 136 }
137 else if (strcmp(*argv,"-out") == 0) 137 else if (strcmp(*argv, "-out") == 0) {
138 { 138 if (--argc < 1)
139 if (--argc < 1) goto bad; 139 goto bad;
140 outfile= *(++argv); 140 outfile= *(++argv);
141 } 141 }
142#ifndef OPENSSL_NO_ENGINE 142#ifndef OPENSSL_NO_ENGINE
143 else if (strcmp(*argv,"-engine") == 0) 143 else if (strcmp(*argv, "-engine") == 0) {
144 { 144 if (--argc < 1)
145 if (--argc < 1) goto bad; 145 goto bad;
146 engine= *(++argv); 146 engine= *(++argv);
147 } 147 }
148#endif 148#endif
149 else if (strcmp(*argv,"-check") == 0) 149 else if (strcmp(*argv, "-check") == 0)
150 check=1; 150 check = 1;
151 else if (strcmp(*argv,"-text") == 0) 151 else if (strcmp(*argv, "-text") == 0)
152 text=1; 152 text = 1;
153 else if (strcmp(*argv,"-C") == 0) 153 else if (strcmp(*argv, "-C") == 0)
154 C=1; 154 C = 1;
155 else if (strcmp(*argv,"-noout") == 0) 155 else if (strcmp(*argv, "-noout") == 0)
156 noout=1; 156 noout = 1;
157 else 157 else {
158 { 158 BIO_printf(bio_err, "unknown option %s\n", *argv);
159 BIO_printf(bio_err,"unknown option %s\n",*argv); 159 badops = 1;
160 badops=1;
161 break; 160 break;
162 } 161 }
163 argc--; 162 argc--;
164 argv++; 163 argv++;
165 } 164 }
166 165
167 if (badops) 166 if (badops) {
168 {
169bad: 167bad:
170 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog); 168 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
171 BIO_printf(bio_err,"where options are\n"); 169 BIO_printf(bio_err, "where options are\n");
172 BIO_printf(bio_err," -inform arg input format - one of DER PEM\n"); 170 BIO_printf(bio_err, " -inform arg input format - one of DER PEM\n");
173 BIO_printf(bio_err," -outform arg output format - one of DER PEM\n"); 171 BIO_printf(bio_err, " -outform arg output format - one of DER PEM\n");
174 BIO_printf(bio_err," -in arg input file\n"); 172 BIO_printf(bio_err, " -in arg input file\n");
175 BIO_printf(bio_err," -out arg output file\n"); 173 BIO_printf(bio_err, " -out arg output file\n");
176 BIO_printf(bio_err," -check check the DH parameters\n"); 174 BIO_printf(bio_err, " -check check the DH parameters\n");
177 BIO_printf(bio_err," -text print a text form of the DH parameters\n"); 175 BIO_printf(bio_err, " -text print a text form of the DH parameters\n");
178 BIO_printf(bio_err," -C Output C code\n"); 176 BIO_printf(bio_err, " -C Output C code\n");
179 BIO_printf(bio_err," -noout no output\n"); 177 BIO_printf(bio_err, " -noout no output\n");
180#ifndef OPENSSL_NO_ENGINE 178#ifndef OPENSSL_NO_ENGINE
181 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); 179 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
182#endif 180#endif
183 goto end; 181 goto end;
184 } 182 }
185 183
186 ERR_load_crypto_strings(); 184 ERR_load_crypto_strings();
187 185
188#ifndef OPENSSL_NO_ENGINE 186#ifndef OPENSSL_NO_ENGINE
189 setup_engine(bio_err, engine, 0); 187 setup_engine(bio_err, engine, 0);
190#endif 188#endif
191 189
192 in=BIO_new(BIO_s_file()); 190 in = BIO_new(BIO_s_file());
193 out=BIO_new(BIO_s_file()); 191 out = BIO_new(BIO_s_file());
194 if ((in == NULL) || (out == NULL)) 192 if ((in == NULL) || (out == NULL)) {
195 {
196 ERR_print_errors(bio_err); 193 ERR_print_errors(bio_err);
197 goto end; 194 goto end;
198 } 195 }
199 196
200 if (infile == NULL) 197 if (infile == NULL)
201 BIO_set_fp(in,stdin,BIO_NOCLOSE); 198 BIO_set_fp(in, stdin, BIO_NOCLOSE);
202 else 199 else {
203 { 200 if (BIO_read_filename(in, infile) <= 0) {
204 if (BIO_read_filename(in,infile) <= 0)
205 {
206 perror(infile); 201 perror(infile);
207 goto end; 202 goto end;
208 }
209 }
210 if (outfile == NULL)
211 {
212 BIO_set_fp(out,stdout,BIO_NOCLOSE);
213 } 203 }
214 else 204 }
215 { 205 if (outfile == NULL) {
216 if (BIO_write_filename(out,outfile) <= 0) 206 BIO_set_fp(out, stdout, BIO_NOCLOSE);
217 { 207 } else {
208 if (BIO_write_filename(out, outfile) <= 0) {
218 perror(outfile); 209 perror(outfile);
219 goto end; 210 goto end;
220 }
221 } 211 }
212 }
222 213
223 if (informat == FORMAT_ASN1) 214 if (informat == FORMAT_ASN1)
224 dh=d2i_DHparams_bio(in,NULL); 215 dh = d2i_DHparams_bio(in, NULL);
225 else if (informat == FORMAT_PEM) 216 else if (informat == FORMAT_PEM)
226 dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL); 217 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
227 else 218 else {
228 { 219 BIO_printf(bio_err, "bad input format specified\n");
229 BIO_printf(bio_err,"bad input format specified\n");
230 goto end; 220 goto end;
231 } 221 }
232 if (dh == NULL) 222 if (dh == NULL) {
233 { 223 BIO_printf(bio_err, "unable to load DH parameters\n");
234 BIO_printf(bio_err,"unable to load DH parameters\n");
235 ERR_print_errors(bio_err); 224 ERR_print_errors(bio_err);
236 goto end; 225 goto end;
237 } 226 }
238
239
240 227
241 if (text) 228 if (text) {
242 { 229 DHparams_print(out, dh);
243 DHparams_print(out,dh);
244#ifdef undef 230#ifdef undef
245 printf("p="); 231 printf("p=");
246 BN_print(stdout,dh->p); 232 BN_print(stdout, dh->p);
247 printf("\ng="); 233 printf("\ng=");
248 BN_print(stdout,dh->g); 234 BN_print(stdout, dh->g);
249 printf("\n"); 235 printf("\n");
250 if (dh->length != 0) 236 if (dh->length != 0)
251 printf("recommended private length=%ld\n",dh->length); 237 printf("recommended private length=%ld\n", dh->length);
252#endif 238#endif
253 } 239 }
254 240
255 if (check) 241 if (check) {
256 { 242 if (!DH_check(dh, &i)) {
257 if (!DH_check(dh,&i))
258 {
259 ERR_print_errors(bio_err); 243 ERR_print_errors(bio_err);
260 goto end; 244 goto end;
261 } 245 }
262 if (i & DH_CHECK_P_NOT_PRIME) 246 if (i & DH_CHECK_P_NOT_PRIME)
263 printf("p value is not prime\n"); 247 printf("p value is not prime\n");
264 if (i & DH_CHECK_P_NOT_SAFE_PRIME) 248 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
@@ -269,81 +253,81 @@ bad:
269 printf("the g value is not a generator\n"); 253 printf("the g value is not a generator\n");
270 if (i == 0) 254 if (i == 0)
271 printf("DH parameters appear to be ok.\n"); 255 printf("DH parameters appear to be ok.\n");
272 } 256 }
273 if (C) 257
274 { 258 if (C) {
275 unsigned char *data; 259 unsigned char *data;
276 int len,l,bits; 260 int len, l, bits;
277 261
278 len=BN_num_bytes(dh->p); 262 len = BN_num_bytes(dh->p);
279 bits=BN_num_bits(dh->p); 263 bits = BN_num_bits(dh->p);
280 data=(unsigned char *)OPENSSL_malloc(len); 264 data = (unsigned char *)OPENSSL_malloc(len);
281 if (data == NULL) 265 if (data == NULL) {
282 {
283 perror("OPENSSL_malloc"); 266 perror("OPENSSL_malloc");
284 goto end; 267 goto end;
285 } 268 }
286 l=BN_bn2bin(dh->p,data); 269 l = BN_bn2bin(dh->p, data);
287 printf("static unsigned char dh%d_p[]={",bits); 270 printf("static unsigned char dh%d_p[] = {", bits);
288 for (i=0; i<l; i++) 271 for (i = 0; i < l; i++) {
289 { 272 if ((i % 12) == 0)
290 if ((i%12) == 0) printf("\n\t"); 273 printf("\n\t");
291 printf("0x%02X,",data[i]); 274 printf("0x%02X, ", data[i]);
292 } 275 }
293 printf("\n\t};\n"); 276 printf("\n\t};\n");
294 277
295 l=BN_bn2bin(dh->g,data); 278 l = BN_bn2bin(dh->g, data);
296 printf("static unsigned char dh%d_g[]={",bits); 279 printf("static unsigned char dh%d_g[] = {", bits);
297 for (i=0; i<l; i++) 280 for (i = 0; i < l; i++) {
298 { 281 if ((i % 12) == 0)
299 if ((i%12) == 0) printf("\n\t"); 282 printf("\n\t");
300 printf("0x%02X,",data[i]); 283 printf("0x%02X, ", data[i]);
301 } 284 }
302 printf("\n\t};\n\n"); 285 printf("\n\t};\n\n");
303 286
304 printf("DH *get_dh%d()\n\t{\n",bits); 287 printf("DH *get_dh%d()\n\t{\n", bits);
305 printf("\tDH *dh;\n\n"); 288 printf("\tDH *dh;\n\n");
306 printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n"); 289 printf("\tif ((dh = DH_new()) == NULL) return(NULL);\n");
307 printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n", 290 printf("\tdh->p = BN_bin2bn(dh%d_p, sizeof(dh%d_p), NULL);\n",
308 bits,bits); 291 bits, bits);
309 printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n", 292 printf("\tdh->g = BN_bin2bn(dh%d_g, sizeof(dh%d_g), NULL);\n",
310 bits,bits); 293 bits, bits);
311 printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n"); 294 printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
312 printf("\t\treturn(NULL);\n"); 295 printf("\t\treturn(NULL);\n");
313 printf("\treturn(dh);\n\t}\n"); 296 printf("\treturn(dh);\n\t}\n");
314 OPENSSL_free(data); 297 OPENSSL_free(data);
315 } 298 }
316
317 299
318 if (!noout) 300 if (!noout) {
319 { 301 if (outformat == FORMAT_ASN1)
320 if (outformat == FORMAT_ASN1) 302 i = i2d_DHparams_bio(out, dh);
321 i=i2d_DHparams_bio(out,dh);
322 else if (outformat == FORMAT_PEM) 303 else if (outformat == FORMAT_PEM)
323 i=PEM_write_bio_DHparams(out,dh); 304 i = PEM_write_bio_DHparams(out, dh);
324 else { 305 else {
325 BIO_printf(bio_err,"bad output format specified for outfile\n"); 306 BIO_printf(bio_err, "bad output format specified for outfile\n");
326 goto end; 307 goto end;
327 } 308 }
328 if (!i) 309 if (!i) {
329 { 310 BIO_printf(bio_err, "unable to write DH parameters\n");
330 BIO_printf(bio_err,"unable to write DH parameters\n");
331 ERR_print_errors(bio_err); 311 ERR_print_errors(bio_err);
332 goto end; 312 goto end;
333 }
334 } 313 }
335 ret=0; 314 }
315 ret = 0;
316
336end: 317end:
337 if (in != NULL) BIO_free(in); 318 if (in != NULL)
338 if (out != NULL) BIO_free_all(out); 319 BIO_free(in);
339 if (dh != NULL) DH_free(dh); 320 if (out != NULL)
321 BIO_free_all(out);
322 if (dh != NULL)
323 DH_free(dh);
340 apps_shutdown(); 324 apps_shutdown();
341 OPENSSL_EXIT(ret); 325 OPENSSL_EXIT(ret);
342 } 326}
343#else /* !OPENSSL_NO_DH */ 327#else /* !OPENSSL_NO_DH */
344 328
345# if PEDANTIC 329# if PEDANTIC
346static void *dummy=&dummy; 330static void *dummy = &dummy;
347# endif 331# endif
348 332
349#endif 333#endif
diff --git a/src/lib/libssl/src/apps/dhparam.c b/src/lib/libssl/src/apps/dhparam.c
index 193e59bd01..718c744722 100644
--- a/src/lib/libssl/src/apps/dhparam.c
+++ b/src/lib/libssl/src/apps/dhparam.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -63,7 +63,7 @@
63 * are met: 63 * are met:
64 * 64 *
65 * 1. Redistributions of source code must retain the above copyright 65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer. 66 * notice, this list of conditions and the following disclaimer.
67 * 67 *
68 * 2. Redistributions in binary form must reproduce the above copyright 68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in 69 * notice, this list of conditions and the following disclaimer in
@@ -147,298 +147,268 @@ static int dh_cb(int p, int n, BN_GENCB *cb);
147 147
148int MAIN(int, char **); 148int MAIN(int, char **);
149 149
150int MAIN(int argc, char **argv) 150int
151 { 151MAIN(int argc, char **argv)
152 DH *dh=NULL; 152{
153 int i,badops=0,text=0; 153 DH *dh = NULL;
154 int i, badops = 0, text = 0;
154#ifndef OPENSSL_NO_DSA 155#ifndef OPENSSL_NO_DSA
155 int dsaparam=0; 156 int dsaparam = 0;
156#endif 157#endif
157 BIO *in=NULL,*out=NULL; 158 BIO *in = NULL, *out = NULL;
158 int informat,outformat,check=0,noout=0,C=0,ret=1; 159 int informat, outformat, check = 0, noout = 0, C = 0, ret = 1;
159 char *infile,*outfile,*prog; 160 char *infile, *outfile, *prog;
160 char *inrand=NULL; 161 char *inrand = NULL;
161#ifndef OPENSSL_NO_ENGINE 162#ifndef OPENSSL_NO_ENGINE
162 char *engine=NULL; 163 char *engine = NULL;
163#endif 164#endif
164 int num = 0, g = 0; 165 int num = 0, g = 0;
165 166
166 apps_startup(); 167 apps_startup();
167 168
168 if (bio_err == NULL) 169 if (bio_err == NULL)
169 if ((bio_err=BIO_new(BIO_s_file())) != NULL) 170 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
170 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 171 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
171 172
172 if (!load_config(bio_err, NULL)) 173 if (!load_config(bio_err, NULL))
173 goto end; 174 goto end;
174 175
175 infile=NULL; 176 infile = NULL;
176 outfile=NULL; 177 outfile = NULL;
177 informat=FORMAT_PEM; 178 informat = FORMAT_PEM;
178 outformat=FORMAT_PEM; 179 outformat = FORMAT_PEM;
179 180
180 prog=argv[0]; 181 prog = argv[0];
181 argc--; 182 argc--;
182 argv++; 183 argv++;
183 while (argc >= 1) 184 while (argc >= 1) {
184 { 185 if (strcmp(*argv, "-inform") == 0) {
185 if (strcmp(*argv,"-inform") == 0) 186 if (--argc < 1)
186 { 187 goto bad;
187 if (--argc < 1) goto bad; 188 informat = str2fmt(*(++argv));
188 informat=str2fmt(*(++argv)); 189 } else if (strcmp(*argv, "-outform") == 0) {
189 } 190 if (--argc < 1)
190 else if (strcmp(*argv,"-outform") == 0) 191 goto bad;
191 { 192 outformat = str2fmt(*(++argv));
192 if (--argc < 1) goto bad; 193 } else if (strcmp(*argv, "-in") == 0) {
193 outformat=str2fmt(*(++argv)); 194 if (--argc < 1)
194 } 195 goto bad;
195 else if (strcmp(*argv,"-in") == 0)
196 {
197 if (--argc < 1) goto bad;
198 infile= *(++argv); 196 infile= *(++argv);
199 } 197 } else if (strcmp(*argv, "-out") == 0) {
200 else if (strcmp(*argv,"-out") == 0) 198 if (--argc < 1)
201 { 199 goto bad;
202 if (--argc < 1) goto bad;
203 outfile= *(++argv); 200 outfile= *(++argv);
204 } 201 }
205#ifndef OPENSSL_NO_ENGINE 202#ifndef OPENSSL_NO_ENGINE
206 else if (strcmp(*argv,"-engine") == 0) 203 else if (strcmp(*argv, "-engine") == 0) {
207 { 204 if (--argc < 1)
208 if (--argc < 1) goto bad; 205 goto bad;
209 engine= *(++argv); 206 engine= *(++argv);
210 } 207 }
211#endif 208#endif
212 else if (strcmp(*argv,"-check") == 0) 209 else if (strcmp(*argv, "-check") == 0)
213 check=1; 210 check = 1;
214 else if (strcmp(*argv,"-text") == 0) 211 else if (strcmp(*argv, "-text") == 0)
215 text=1; 212 text = 1;
216#ifndef OPENSSL_NO_DSA 213#ifndef OPENSSL_NO_DSA
217 else if (strcmp(*argv,"-dsaparam") == 0) 214 else if (strcmp(*argv, "-dsaparam") == 0)
218 dsaparam=1; 215 dsaparam = 1;
219#endif 216#endif
220 else if (strcmp(*argv,"-C") == 0) 217 else if (strcmp(*argv, "-C") == 0)
221 C=1; 218 C = 1;
222 else if (strcmp(*argv,"-noout") == 0) 219 else if (strcmp(*argv, "-noout") == 0)
223 noout=1; 220 noout = 1;
224 else if (strcmp(*argv,"-2") == 0) 221 else if (strcmp(*argv, "-2") == 0)
225 g=2; 222 g = 2;
226 else if (strcmp(*argv,"-5") == 0) 223 else if (strcmp(*argv, "-5") == 0)
227 g=5; 224 g = 5;
228 else if (strcmp(*argv,"-rand") == 0) 225 else if (strcmp(*argv, "-rand") == 0) {
229 { 226 if (--argc < 1)
230 if (--argc < 1) goto bad; 227 goto bad;
231 inrand= *(++argv); 228 inrand= *(++argv);
232 } 229 }
233 else if (((sscanf(*argv,"%d",&num) == 0) || (num <= 0))) 230 else if (((sscanf(*argv, "%d", &num) == 0) || (num <= 0)))
234 goto bad; 231 goto bad;
235 argv++; 232 argv++;
236 argc--; 233 argc--;
237 } 234 }
238 235
239 if (badops) 236 if (badops) {
240 {
241bad: 237bad:
242 BIO_printf(bio_err,"%s [options] [numbits]\n",prog); 238 BIO_printf(bio_err, "%s [options] [numbits]\n", prog);
243 BIO_printf(bio_err,"where options are\n"); 239 BIO_printf(bio_err, "where options are\n");
244 BIO_printf(bio_err," -inform arg input format - one of DER PEM\n"); 240 BIO_printf(bio_err, " -inform arg input format - one of DER PEM\n");
245 BIO_printf(bio_err," -outform arg output format - one of DER PEM\n"); 241 BIO_printf(bio_err, " -outform arg output format - one of DER PEM\n");
246 BIO_printf(bio_err," -in arg input file\n"); 242 BIO_printf(bio_err, " -in arg input file\n");
247 BIO_printf(bio_err," -out arg output file\n"); 243 BIO_printf(bio_err, " -out arg output file\n");
248#ifndef OPENSSL_NO_DSA 244#ifndef OPENSSL_NO_DSA
249 BIO_printf(bio_err," -dsaparam read or generate DSA parameters, convert to DH\n"); 245 BIO_printf(bio_err, " -dsaparam read or generate DSA parameters, convert to DH\n");
250#endif 246#endif
251 BIO_printf(bio_err," -check check the DH parameters\n"); 247 BIO_printf(bio_err, " -check check the DH parameters\n");
252 BIO_printf(bio_err," -text print a text form of the DH parameters\n"); 248 BIO_printf(bio_err, " -text print a text form of the DH parameters\n");
253 BIO_printf(bio_err," -C Output C code\n"); 249 BIO_printf(bio_err, " -C Output C code\n");
254 BIO_printf(bio_err," -2 generate parameters using 2 as the generator value\n"); 250 BIO_printf(bio_err, " -2 generate parameters using 2 as the generator value\n");
255 BIO_printf(bio_err," -5 generate parameters using 5 as the generator value\n"); 251 BIO_printf(bio_err, " -5 generate parameters using 5 as the generator value\n");
256 BIO_printf(bio_err," numbits number of bits in to generate (default 512)\n"); 252 BIO_printf(bio_err, " numbits number of bits in to generate (default 512)\n");
257#ifndef OPENSSL_NO_ENGINE 253#ifndef OPENSSL_NO_ENGINE
258 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); 254 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
259#endif 255#endif
260 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 256 BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
261 BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); 257 BIO_printf(bio_err, " - load the file (or the files in the directory) into\n");
262 BIO_printf(bio_err," the random number generator\n"); 258 BIO_printf(bio_err, " the random number generator\n");
263 BIO_printf(bio_err," -noout no output\n"); 259 BIO_printf(bio_err, " -noout no output\n");
264 goto end; 260 goto end;
265 } 261 }
266 262
267 ERR_load_crypto_strings(); 263 ERR_load_crypto_strings();
268 264
269#ifndef OPENSSL_NO_ENGINE 265#ifndef OPENSSL_NO_ENGINE
270 setup_engine(bio_err, engine, 0); 266 setup_engine(bio_err, engine, 0);
271#endif 267#endif
272 268
273 if (g && !num) 269 if (g && !num)
274 num = DEFBITS; 270 num = DEFBITS;
275 271
276#ifndef OPENSSL_NO_DSA 272#ifndef OPENSSL_NO_DSA
277 if (dsaparam) 273 if (dsaparam) {
278 { 274 if (g) {
279 if (g)
280 {
281 BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n"); 275 BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n");
282 goto end; 276 goto end;
283 }
284 } 277 }
285 else 278 } else
286#endif 279#endif
287 { 280 {
288 /* DH parameters */ 281 /* DH parameters */
289 if (num && !g) 282 if (num && !g)
290 g = 2; 283 g = 2;
291 } 284 }
292 285
293 if(num) { 286 if (num) {
294 287
295 BN_GENCB cb; 288 BN_GENCB cb;
296 BN_GENCB_set(&cb, dh_cb, bio_err); 289 BN_GENCB_set(&cb, dh_cb, bio_err);
297 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) 290 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) {
298 { 291 BIO_printf(bio_err, "warning, not much extra random data, consider using the -rand option\n");
299 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); 292 }
300 }
301 if (inrand != NULL) 293 if (inrand != NULL)
302 BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 294 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
303 app_RAND_load_files(inrand)); 295 app_RAND_load_files(inrand));
304 296
305#ifndef OPENSSL_NO_DSA 297#ifndef OPENSSL_NO_DSA
306 if (dsaparam) 298 if (dsaparam) {
307 {
308 DSA *dsa = DSA_new(); 299 DSA *dsa = DSA_new();
309 300
310 BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num); 301 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num);
311 if(!dsa || !DSA_generate_parameters_ex(dsa, num, 302 if (!dsa || !DSA_generate_parameters_ex(dsa, num,
312 NULL, 0, NULL, NULL, &cb)) 303 NULL, 0, NULL, NULL, &cb)) {
313 { 304 if (dsa) DSA_free(dsa);
314 if(dsa) DSA_free(dsa); 305 ERR_print_errors(bio_err);
315 ERR_print_errors(bio_err);
316 goto end; 306 goto end;
317 } 307 }
318 308
319 dh = DSA_dup_DH(dsa); 309 dh = DSA_dup_DH(dsa);
320 DSA_free(dsa); 310 DSA_free(dsa);
321 if (dh == NULL) 311 if (dh == NULL) {
322 {
323 ERR_print_errors(bio_err); 312 ERR_print_errors(bio_err);
324 goto end; 313 goto end;
325 }
326 } 314 }
327 else 315 } else
328#endif 316#endif
329 { 317 {
330 dh = DH_new(); 318 dh = DH_new();
331 BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g); 319 BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime, generator %d\n", num, g);
332 BIO_printf(bio_err,"This is going to take a long time\n"); 320 BIO_printf(bio_err, "This is going to take a long time\n");
333 if(!dh || !DH_generate_parameters_ex(dh, num, g, &cb)) 321 if (!dh || !DH_generate_parameters_ex(dh, num, g, &cb)) {
334 {
335 ERR_print_errors(bio_err); 322 ERR_print_errors(bio_err);
336 goto end; 323 goto end;
337 }
338 } 324 }
325 }
339 326
340 app_RAND_write_file(NULL, bio_err); 327 app_RAND_write_file(NULL, bio_err);
341 } else { 328 } else {
342 329
343 in=BIO_new(BIO_s_file()); 330 in = BIO_new(BIO_s_file());
344 if (in == NULL) 331 if (in == NULL) {
345 {
346 ERR_print_errors(bio_err); 332 ERR_print_errors(bio_err);
347 goto end; 333 goto end;
348 } 334 }
349 if (infile == NULL) 335 if (infile == NULL)
350 BIO_set_fp(in,stdin,BIO_NOCLOSE); 336 BIO_set_fp(in, stdin, BIO_NOCLOSE);
351 else 337 else {
352 { 338 if (BIO_read_filename(in, infile) <= 0) {
353 if (BIO_read_filename(in,infile) <= 0)
354 {
355 perror(infile); 339 perror(infile);
356 goto end; 340 goto end;
357 }
358 } 341 }
342 }
359 343
360 if (informat != FORMAT_ASN1 && informat != FORMAT_PEM) 344 if (informat != FORMAT_ASN1 && informat != FORMAT_PEM) {
361 { 345 BIO_printf(bio_err, "bad input format specified\n");
362 BIO_printf(bio_err,"bad input format specified\n");
363 goto end; 346 goto end;
364 } 347 }
365 348
366#ifndef OPENSSL_NO_DSA 349#ifndef OPENSSL_NO_DSA
367 if (dsaparam) 350 if (dsaparam) {
368 {
369 DSA *dsa; 351 DSA *dsa;
370 352
371 if (informat == FORMAT_ASN1) 353 if (informat == FORMAT_ASN1)
372 dsa=d2i_DSAparams_bio(in,NULL); 354 dsa = d2i_DSAparams_bio(in, NULL);
373 else /* informat == FORMAT_PEM */ 355 else /* informat == FORMAT_PEM */
374 dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL); 356 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
375 357
376 if (dsa == NULL) 358 if (dsa == NULL) {
377 { 359 BIO_printf(bio_err, "unable to load DSA parameters\n");
378 BIO_printf(bio_err,"unable to load DSA parameters\n");
379 ERR_print_errors(bio_err); 360 ERR_print_errors(bio_err);
380 goto end; 361 goto end;
381 } 362 }
382 363
383 dh = DSA_dup_DH(dsa); 364 dh = DSA_dup_DH(dsa);
384 DSA_free(dsa); 365 DSA_free(dsa);
385 if (dh == NULL) 366 if (dh == NULL) {
386 {
387 ERR_print_errors(bio_err); 367 ERR_print_errors(bio_err);
388 goto end; 368 goto end;
389 }
390 } 369 }
391 else 370 } else
392#endif 371#endif
393 { 372 {
394 if (informat == FORMAT_ASN1) 373 if (informat == FORMAT_ASN1)
395 dh=d2i_DHparams_bio(in,NULL); 374 dh = d2i_DHparams_bio(in, NULL);
396 else /* informat == FORMAT_PEM */ 375 else /* informat == FORMAT_PEM */
397 dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL); 376 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
398 377
399 if (dh == NULL) 378 if (dh == NULL) {
400 { 379 BIO_printf(bio_err, "unable to load DH parameters\n");
401 BIO_printf(bio_err,"unable to load DH parameters\n");
402 ERR_print_errors(bio_err); 380 ERR_print_errors(bio_err);
403 goto end; 381 goto end;
404 }
405 } 382 }
406 383 }
384
407 /* dh != NULL */ 385 /* dh != NULL */
408 } 386 }
409 387
410 out=BIO_new(BIO_s_file()); 388 out = BIO_new(BIO_s_file());
411 if (out == NULL) 389 if (out == NULL) {
412 {
413 ERR_print_errors(bio_err); 390 ERR_print_errors(bio_err);
414 goto end; 391 goto end;
415 } 392 }
416 if (outfile == NULL) 393 if (outfile == NULL) {
417 { 394 BIO_set_fp(out, stdout, BIO_NOCLOSE);
418 BIO_set_fp(out,stdout,BIO_NOCLOSE); 395 } else {
419 } 396 if (BIO_write_filename(out, outfile) <= 0) {
420 else
421 {
422 if (BIO_write_filename(out,outfile) <= 0)
423 {
424 perror(outfile); 397 perror(outfile);
425 goto end; 398 goto end;
426 }
427 } 399 }
400 }
428 401
429 402
430 if (text) 403 if (text) {
431 { 404 DHparams_print(out, dh);
432 DHparams_print(out,dh); 405 }
433 } 406
434 407 if (check) {
435 if (check) 408 if (!DH_check(dh, &i)) {
436 {
437 if (!DH_check(dh,&i))
438 {
439 ERR_print_errors(bio_err); 409 ERR_print_errors(bio_err);
440 goto end; 410 goto end;
441 } 411 }
442 if (i & DH_CHECK_P_NOT_PRIME) 412 if (i & DH_CHECK_P_NOT_PRIME)
443 printf("p value is not prime\n"); 413 printf("p value is not prime\n");
444 if (i & DH_CHECK_P_NOT_SAFE_PRIME) 414 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
@@ -449,105 +419,109 @@ bad:
449 printf("the g value is not a generator\n"); 419 printf("the g value is not a generator\n");
450 if (i == 0) 420 if (i == 0)
451 printf("DH parameters appear to be ok.\n"); 421 printf("DH parameters appear to be ok.\n");
452 } 422 }
453 if (C) 423 if (C) {
454 {
455 unsigned char *data; 424 unsigned char *data;
456 int len,l,bits; 425 int len, l, bits;
457 426
458 len=BN_num_bytes(dh->p); 427 len = BN_num_bytes(dh->p);
459 bits=BN_num_bits(dh->p); 428 bits = BN_num_bits(dh->p);
460 data=(unsigned char *)OPENSSL_malloc(len); 429 data = (unsigned char *)OPENSSL_malloc(len);
461 if (data == NULL) 430 if (data == NULL) {
462 {
463 perror("OPENSSL_malloc"); 431 perror("OPENSSL_malloc");
464 goto end; 432 goto end;
465 } 433 }
466 printf("#ifndef HEADER_DH_H\n" 434 printf("#ifndef HEADER_DH_H\n"
467 "#include <openssl/dh.h>\n" 435 "#include <openssl/dh.h>\n"
468 "#endif\n"); 436 "#endif\n");
469 printf("DH *get_dh%d()\n\t{\n",bits); 437 printf("DH *get_dh%d()\n\t{\n", bits);
470 438
471 l=BN_bn2bin(dh->p,data); 439 l = BN_bn2bin(dh->p, data);
472 printf("\tstatic unsigned char dh%d_p[]={",bits); 440 printf("\tstatic unsigned char dh%d_p[] = {", bits);
473 for (i=0; i<l; i++) 441 for (i = 0; i < l; i++) {
474 { 442 if ((i % 12) == 0)
475 if ((i%12) == 0) printf("\n\t\t"); 443 printf("\n\t\t");
476 printf("0x%02X,",data[i]); 444 printf("0x%02X, ", data[i]);
477 } 445 }
478 printf("\n\t\t};\n"); 446 printf("\n\t\t};\n");
479 447
480 l=BN_bn2bin(dh->g,data); 448 l = BN_bn2bin(dh->g, data);
481 printf("\tstatic unsigned char dh%d_g[]={",bits); 449 printf("\tstatic unsigned char dh%d_g[] = {", bits);
482 for (i=0; i<l; i++) 450 for (i = 0; i < l; i++) {
483 { 451 if ((i % 12) == 0)
484 if ((i%12) == 0) printf("\n\t\t"); 452 printf("\n\t\t");
485 printf("0x%02X,",data[i]); 453 printf("0x%02X, ", data[i]);
486 } 454 }
487 printf("\n\t\t};\n"); 455 printf("\n\t\t};\n");
488 456
489 printf("\tDH *dh;\n\n"); 457 printf("\tDH *dh;\n\n");
490 printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n"); 458 printf("\tif ((dh = DH_new()) == NULL) return(NULL);\n");
491 printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n", 459 printf("\tdh->p = BN_bin2bn(dh%d_p, sizeof(dh%d_p), NULL);\n",
492 bits,bits); 460 bits, bits);
493 printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n", 461 printf("\tdh->g = BN_bin2bn(dh%d_g, sizeof(dh%d_g), NULL);\n",
494 bits,bits); 462 bits, bits);
495 printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n"); 463 printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
496 printf("\t\t{ DH_free(dh); return(NULL); }\n"); 464 printf("\t\t{ DH_free(dh); return(NULL); }\n");
497 if (dh->length) 465 if (dh->length)
498 printf("\tdh->length = %ld;\n", dh->length); 466 printf("\tdh->length = %ld;\n", dh->length);
499 printf("\treturn(dh);\n\t}\n"); 467 printf("\treturn(dh);\n\t}\n");
500 OPENSSL_free(data); 468 OPENSSL_free(data);
501 } 469 }
502
503 470
504 if (!noout) 471 if (!noout) {
505 { 472 if (outformat == FORMAT_ASN1)
506 if (outformat == FORMAT_ASN1) 473 i = i2d_DHparams_bio(out, dh);
507 i=i2d_DHparams_bio(out,dh);
508 else if (outformat == FORMAT_PEM) 474 else if (outformat == FORMAT_PEM)
509 i=PEM_write_bio_DHparams(out,dh); 475 i = PEM_write_bio_DHparams(out, dh);
510 else { 476 else {
511 BIO_printf(bio_err,"bad output format specified for outfile\n"); 477 BIO_printf(bio_err, "bad output format specified for outfile\n");
512 goto end; 478 goto end;
513 } 479 }
514 if (!i) 480 if (!i) {
515 { 481 BIO_printf(bio_err, "unable to write DH parameters\n");
516 BIO_printf(bio_err,"unable to write DH parameters\n");
517 ERR_print_errors(bio_err); 482 ERR_print_errors(bio_err);
518 goto end; 483 goto end;
519 }
520 } 484 }
521 ret=0; 485 }
486 ret = 0;
487
522end: 488end:
523 if (in != NULL) BIO_free(in); 489 if (in != NULL)
524 if (out != NULL) BIO_free_all(out); 490 BIO_free(in);
525 if (dh != NULL) DH_free(dh); 491 if (out != NULL)
492 BIO_free_all(out);
493 if (dh != NULL)
494 DH_free(dh);
526 apps_shutdown(); 495 apps_shutdown();
527 OPENSSL_EXIT(ret); 496 OPENSSL_EXIT(ret);
528 } 497}
529 498
530/* dh_cb is identical to dsa_cb in apps/dsaparam.c */ 499/* dh_cb is identical to dsa_cb in apps/dsaparam.c */
531static int dh_cb(int p, int n, BN_GENCB *cb) 500static int
532 { 501dh_cb(int p, int n, BN_GENCB *cb)
533 char c='*'; 502{
534 503 char c = '*';
535 if (p == 0) c='.'; 504
536 if (p == 1) c='+'; 505 if (p == 0)
537 if (p == 2) c='*'; 506 c = '.';
538 if (p == 3) c='\n'; 507 if (p == 1)
539 BIO_write(cb->arg,&c,1); 508 c = '+';
509 if (p == 2)
510 c = '*';
511 if (p == 3)
512 c = '\n';
513 BIO_write(cb->arg, &c, 1);
540 (void)BIO_flush(cb->arg); 514 (void)BIO_flush(cb->arg);
541#ifdef LINT 515#ifdef LINT
542 p=n; 516 p = n;
543#endif 517#endif
544 return 1; 518 return 1;
545 } 519}
546 520
547#else /* !OPENSSL_NO_DH */ 521#else /* !OPENSSL_NO_DH */
548 522
549# if PEDANTIC 523# if PEDANTIC
550static void *dummy=&dummy; 524static void *dummy = &dummy;
551# endif 525# endif
552 526
553#endif 527#endif
diff --git a/src/lib/libssl/src/apps/dsa.c b/src/lib/libssl/src/apps/dsa.c
index 45bac4a9d1..b2c58d03c1 100644
--- a/src/lib/libssl/src/apps/dsa.c
+++ b/src/lib/libssl/src/apps/dsa.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -94,240 +94,227 @@
94 94
95int MAIN(int, char **); 95int MAIN(int, char **);
96 96
97int MAIN(int argc, char **argv) 97int
98 { 98MAIN(int argc, char **argv)
99{
99 ENGINE *e = NULL; 100 ENGINE *e = NULL;
100 int ret=1; 101 int ret = 1;
101 DSA *dsa=NULL; 102 DSA *dsa = NULL;
102 int i,badops=0; 103 int i, badops = 0;
103 const EVP_CIPHER *enc=NULL; 104 const EVP_CIPHER *enc = NULL;
104 BIO *in=NULL,*out=NULL; 105 BIO *in = NULL, *out = NULL;
105 int informat,outformat,text=0,noout=0; 106 int informat, outformat, text = 0, noout = 0;
106 int pubin = 0, pubout = 0; 107 int pubin = 0, pubout = 0;
107 char *infile,*outfile,*prog; 108 char *infile, *outfile, *prog;
108#ifndef OPENSSL_NO_ENGINE 109#ifndef OPENSSL_NO_ENGINE
109 char *engine; 110 char *engine;
110#endif 111#endif
111 char *passargin = NULL, *passargout = NULL; 112 char *passargin = NULL, *passargout = NULL;
112 char *passin = NULL, *passout = NULL; 113 char *passin = NULL, *passout = NULL;
113 int modulus=0; 114 int modulus = 0;
114 115
115 int pvk_encr = 2; 116 int pvk_encr = 2;
116 117
117 apps_startup(); 118 apps_startup();
118 119
119 if (bio_err == NULL) 120 if (bio_err == NULL)
120 if ((bio_err=BIO_new(BIO_s_file())) != NULL) 121 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
121 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 122 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
122 123
123 if (!load_config(bio_err, NULL)) 124 if (!load_config(bio_err, NULL))
124 goto end; 125 goto end;
125 126
126#ifndef OPENSSL_NO_ENGINE 127#ifndef OPENSSL_NO_ENGINE
127 engine=NULL; 128 engine = NULL;
128#endif 129#endif
129 infile=NULL; 130 infile = NULL;
130 outfile=NULL; 131 outfile = NULL;
131 informat=FORMAT_PEM; 132 informat = FORMAT_PEM;
132 outformat=FORMAT_PEM; 133 outformat = FORMAT_PEM;
133 134
134 prog=argv[0]; 135 prog = argv[0];
135 argc--; 136 argc--;
136 argv++; 137 argv++;
137 while (argc >= 1) 138 while (argc >= 1) {
138 { 139 if (strcmp(*argv, "-inform") == 0) {
139 if (strcmp(*argv,"-inform") == 0) 140 if (--argc < 1)
140 { 141 goto bad;
141 if (--argc < 1) goto bad; 142 informat = str2fmt(*(++argv));
142 informat=str2fmt(*(++argv)); 143 } else if (strcmp(*argv, "-outform") == 0) {
143 } 144 if (--argc < 1)
144 else if (strcmp(*argv,"-outform") == 0) 145 goto bad;
145 { 146 outformat = str2fmt(*(++argv));
146 if (--argc < 1) goto bad; 147 } else if (strcmp(*argv, "-in") == 0) {
147 outformat=str2fmt(*(++argv)); 148 if (--argc < 1)
148 } 149 goto bad;
149 else if (strcmp(*argv,"-in") == 0)
150 {
151 if (--argc < 1) goto bad;
152 infile= *(++argv); 150 infile= *(++argv);
153 } 151 }
154 else if (strcmp(*argv,"-out") == 0) 152 else if (strcmp(*argv, "-out") == 0) {
155 { 153 if (--argc < 1)
156 if (--argc < 1) goto bad; 154 goto bad;
157 outfile= *(++argv); 155 outfile= *(++argv);
158 } 156 } else if (strcmp(*argv, "-passin") == 0) {
159 else if (strcmp(*argv,"-passin") == 0) 157 if (--argc < 1)
160 { 158 goto bad;
161 if (--argc < 1) goto bad;
162 passargin= *(++argv); 159 passargin= *(++argv);
163 } 160 } else if (strcmp(*argv, "-passout") == 0) {
164 else if (strcmp(*argv,"-passout") == 0) 161 if (--argc < 1)
165 { 162 goto bad;
166 if (--argc < 1) goto bad;
167 passargout= *(++argv); 163 passargout= *(++argv);
168 } 164 }
169#ifndef OPENSSL_NO_ENGINE 165#ifndef OPENSSL_NO_ENGINE
170 else if (strcmp(*argv,"-engine") == 0) 166 else if (strcmp(*argv, "-engine") == 0) {
171 { 167 if (--argc < 1)
172 if (--argc < 1) goto bad; 168 goto bad;
173 engine= *(++argv); 169 engine= *(++argv);
174 } 170 }
175#endif 171#endif
176 else if (strcmp(*argv,"-pvk-strong") == 0) 172 else if (strcmp(*argv, "-pvk-strong") == 0)
177 pvk_encr=2; 173 pvk_encr = 2;
178 else if (strcmp(*argv,"-pvk-weak") == 0) 174 else if (strcmp(*argv, "-pvk-weak") == 0)
179 pvk_encr=1; 175 pvk_encr = 1;
180 else if (strcmp(*argv,"-pvk-none") == 0) 176 else if (strcmp(*argv, "-pvk-none") == 0)
181 pvk_encr=0; 177 pvk_encr = 0;
182 else if (strcmp(*argv,"-noout") == 0) 178 else if (strcmp(*argv, "-noout") == 0)
183 noout=1; 179 noout = 1;
184 else if (strcmp(*argv,"-text") == 0) 180 else if (strcmp(*argv, "-text") == 0)
185 text=1; 181 text = 1;
186 else if (strcmp(*argv,"-modulus") == 0) 182 else if (strcmp(*argv, "-modulus") == 0)
187 modulus=1; 183 modulus = 1;
188 else if (strcmp(*argv,"-pubin") == 0) 184 else if (strcmp(*argv, "-pubin") == 0)
189 pubin=1; 185 pubin = 1;
190 else if (strcmp(*argv,"-pubout") == 0) 186 else if (strcmp(*argv, "-pubout") == 0)
191 pubout=1; 187 pubout = 1;
192 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL) 188 else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
193 { 189 BIO_printf(bio_err, "unknown option %s\n", *argv);
194 BIO_printf(bio_err,"unknown option %s\n",*argv); 190 badops = 1;
195 badops=1;
196 break; 191 break;
197 } 192 }
198 argc--; 193 argc--;
199 argv++; 194 argv++;
200 } 195 }
201 196
202 if (badops) 197 if (badops) {
203 {
204bad: 198bad:
205 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog); 199 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
206 BIO_printf(bio_err,"where options are\n"); 200 BIO_printf(bio_err, "where options are\n");
207 BIO_printf(bio_err," -inform arg input format - DER or PEM\n"); 201 BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
208 BIO_printf(bio_err," -outform arg output format - DER or PEM\n"); 202 BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
209 BIO_printf(bio_err," -in arg input file\n"); 203 BIO_printf(bio_err, " -in arg input file\n");
210 BIO_printf(bio_err," -passin arg input file pass phrase source\n"); 204 BIO_printf(bio_err, " -passin arg input file pass phrase source\n");
211 BIO_printf(bio_err," -out arg output file\n"); 205 BIO_printf(bio_err, " -out arg output file\n");
212 BIO_printf(bio_err," -passout arg output file pass phrase source\n"); 206 BIO_printf(bio_err, " -passout arg output file pass phrase source\n");
213#ifndef OPENSSL_NO_ENGINE 207#ifndef OPENSSL_NO_ENGINE
214 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); 208 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
215#endif 209#endif
216 BIO_printf(bio_err," -des encrypt PEM output with cbc des\n"); 210 BIO_printf(bio_err, " -des encrypt PEM output with cbc des\n");
217 BIO_printf(bio_err," -des3 encrypt PEM output with ede cbc des using 168 bit key\n"); 211 BIO_printf(bio_err, " -des3 encrypt PEM output with ede cbc des using 168 bit key\n");
218#ifndef OPENSSL_NO_IDEA 212#ifndef OPENSSL_NO_IDEA
219 BIO_printf(bio_err," -idea encrypt PEM output with cbc idea\n"); 213 BIO_printf(bio_err, " -idea encrypt PEM output with cbc idea\n");
220#endif 214#endif
221#ifndef OPENSSL_NO_AES 215#ifndef OPENSSL_NO_AES
222 BIO_printf(bio_err," -aes128, -aes192, -aes256\n"); 216 BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
223 BIO_printf(bio_err," encrypt PEM output with cbc aes\n"); 217 BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
224#endif 218#endif
225#ifndef OPENSSL_NO_CAMELLIA 219#ifndef OPENSSL_NO_CAMELLIA
226 BIO_printf(bio_err," -camellia128, -camellia192, -camellia256\n"); 220 BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
227 BIO_printf(bio_err," encrypt PEM output with cbc camellia\n"); 221 BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n");
228#endif 222#endif
229#ifndef OPENSSL_NO_SEED 223#ifndef OPENSSL_NO_SEED
230 BIO_printf(bio_err," -seed encrypt PEM output with cbc seed\n"); 224 BIO_printf(bio_err, " -seed encrypt PEM output with cbc seed\n");
231#endif 225#endif
232 BIO_printf(bio_err," -text print the key in text\n"); 226 BIO_printf(bio_err, " -text print the key in text\n");
233 BIO_printf(bio_err," -noout don't print key out\n"); 227 BIO_printf(bio_err, " -noout don't print key out\n");
234 BIO_printf(bio_err," -modulus print the DSA public value\n"); 228 BIO_printf(bio_err, " -modulus print the DSA public value\n");
235 goto end; 229 goto end;
236 } 230 }
237 231
238 ERR_load_crypto_strings(); 232 ERR_load_crypto_strings();
239 233
240#ifndef OPENSSL_NO_ENGINE 234#ifndef OPENSSL_NO_ENGINE
241 e = setup_engine(bio_err, engine, 0); 235 e = setup_engine(bio_err, engine, 0);
242#endif 236#endif
243 237
244 if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { 238 if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
245 BIO_printf(bio_err, "Error getting passwords\n"); 239 BIO_printf(bio_err, "Error getting passwords\n");
246 goto end; 240 goto end;
247 } 241 }
248 242
249 in=BIO_new(BIO_s_file()); 243 in = BIO_new(BIO_s_file());
250 out=BIO_new(BIO_s_file()); 244 out = BIO_new(BIO_s_file());
251 if ((in == NULL) || (out == NULL)) 245 if ((in == NULL) || (out == NULL)) {
252 {
253 ERR_print_errors(bio_err); 246 ERR_print_errors(bio_err);
254 goto end; 247 goto end;
255 } 248 }
256 249
257 if (infile == NULL) 250 if (infile == NULL)
258 BIO_set_fp(in,stdin,BIO_NOCLOSE); 251 BIO_set_fp(in, stdin, BIO_NOCLOSE);
259 else 252 else {
260 { 253 if (BIO_read_filename(in, infile) <= 0) {
261 if (BIO_read_filename(in,infile) <= 0)
262 {
263 perror(infile); 254 perror(infile);
264 goto end; 255 goto end;
265 }
266 } 256 }
257 }
267 258
268 BIO_printf(bio_err,"read DSA key\n"); 259 BIO_printf(bio_err, "read DSA key\n");
269 260
270 { 261 {
271 EVP_PKEY *pkey; 262 EVP_PKEY *pkey;
272 263
273 if (pubin) 264 if (pubin)
274 pkey = load_pubkey(bio_err, infile, informat, 1, 265 pkey = load_pubkey(bio_err, infile, informat, 1,
275 passin, e, "Public Key"); 266 passin, e, "Public Key");
276 else 267 else
277 pkey = load_key(bio_err, infile, informat, 1, 268 pkey = load_key(bio_err, infile, informat, 1,
278 passin, e, "Private Key"); 269 passin, e, "Private Key");
279 270
280 if (pkey) 271 if (pkey) {
281 {
282 dsa = EVP_PKEY_get1_DSA(pkey); 272 dsa = EVP_PKEY_get1_DSA(pkey);
283 EVP_PKEY_free(pkey); 273 EVP_PKEY_free(pkey);
284 }
285 } 274 }
286 if (dsa == NULL) 275 }
287 { 276 if (dsa == NULL) {
288 BIO_printf(bio_err,"unable to load Key\n"); 277 BIO_printf(bio_err, "unable to load Key\n");
289 ERR_print_errors(bio_err); 278 ERR_print_errors(bio_err);
290 goto end; 279 goto end;
291 } 280 }
292 281
293 if (outfile == NULL) 282 if (outfile == NULL) {
294 { 283 BIO_set_fp(out, stdout, BIO_NOCLOSE);
295 BIO_set_fp(out,stdout,BIO_NOCLOSE); 284 } else {
296 } 285 if (BIO_write_filename(out, outfile) <= 0) {
297 else
298 {
299 if (BIO_write_filename(out,outfile) <= 0)
300 {
301 perror(outfile); 286 perror(outfile);
302 goto end; 287 goto end;
303 }
304 } 288 }
289 }
305 290
306 if (text) 291 if (text)
307 if (!DSA_print(out,dsa,0)) 292 if (!DSA_print(out, dsa, 0)) {
308 { 293 perror(outfile);
309 perror(outfile); 294 ERR_print_errors(bio_err);
310 ERR_print_errors(bio_err); 295 goto end;
311 goto end; 296 }
312 }
313 297
314 if (modulus) 298 if (modulus) {
315 { 299 fprintf(stdout, "Public Key=");
316 fprintf(stdout,"Public Key="); 300 BN_print(out, dsa->pub_key);
317 BN_print(out,dsa->pub_key); 301 fprintf(stdout, "\n");
318 fprintf(stdout,"\n"); 302 }
319 }
320 303
321 if (noout) goto end; 304 if (noout)
322 BIO_printf(bio_err,"writing DSA key\n"); 305 goto end;
323 if (outformat == FORMAT_ASN1) { 306 BIO_printf(bio_err, "writing DSA key\n");
324 if(pubin || pubout) i=i2d_DSA_PUBKEY_bio(out,dsa); 307 if (outformat == FORMAT_ASN1) {
325 else i=i2d_DSAPrivateKey_bio(out,dsa); 308 if (pubin || pubout)
309 i = i2d_DSA_PUBKEY_bio(out, dsa);
310 else
311 i = i2d_DSAPrivateKey_bio(out, dsa);
326 } else if (outformat == FORMAT_PEM) { 312 } else if (outformat == FORMAT_PEM) {
327 if(pubin || pubout) 313 if (pubin || pubout)
328 i=PEM_write_bio_DSA_PUBKEY(out,dsa); 314 i = PEM_write_bio_DSA_PUBKEY(out, dsa);
329 else i=PEM_write_bio_DSAPrivateKey(out,dsa,enc, 315 else
330 NULL,0,NULL, passout); 316 i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
317 NULL, 0, NULL, passout);
331#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4) 318#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
332 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) { 319 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
333 EVP_PKEY *pk; 320 EVP_PKEY *pk;
@@ -342,29 +329,27 @@ bad:
342 EVP_PKEY_free(pk); 329 EVP_PKEY_free(pk);
343#endif 330#endif
344 } else { 331 } else {
345 BIO_printf(bio_err,"bad output format specified for outfile\n"); 332 BIO_printf(bio_err, "bad output format specified for outfile\n");
346 goto end; 333 goto end;
347 } 334 }
348 if (i <= 0) 335 if (i <= 0) {
349 { 336 BIO_printf(bio_err, "unable to write private key\n");
350 BIO_printf(bio_err,"unable to write private key\n");
351 ERR_print_errors(bio_err); 337 ERR_print_errors(bio_err);
352 } 338 } else
353 else 339 ret = 0;
354 ret=0;
355end: 340end:
356 if(in != NULL) BIO_free(in); 341 if (in != NULL) BIO_free(in);
357 if(out != NULL) BIO_free_all(out); 342 if (out != NULL) BIO_free_all(out);
358 if(dsa != NULL) DSA_free(dsa); 343 if (dsa != NULL) DSA_free(dsa);
359 if(passin) OPENSSL_free(passin); 344 if (passin) OPENSSL_free(passin);
360 if(passout) OPENSSL_free(passout); 345 if (passout) OPENSSL_free(passout);
361 apps_shutdown(); 346 apps_shutdown();
362 OPENSSL_EXIT(ret); 347 OPENSSL_EXIT(ret);
363 } 348}
364#else /* !OPENSSL_NO_DSA */ 349#else /* !OPENSSL_NO_DSA */
365 350
366# if PEDANTIC 351# if PEDANTIC
367static void *dummy=&dummy; 352static void *dummy = &dummy;
368# endif 353# endif
369 354
370#endif 355#endif
diff --git a/src/lib/libssl/src/apps/dsaparam.c b/src/lib/libssl/src/apps/dsaparam.c
index 47f8c83702..9cd81ff4f7 100644
--- a/src/lib/libssl/src/apps/dsaparam.c
+++ b/src/lib/libssl/src/apps/dsaparam.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -98,10 +98,11 @@
98 98
99static int stop_keygen_flag = 0; 99static int stop_keygen_flag = 0;
100 100
101static void timebomb_sigalarm(int foo) 101static void
102 { 102timebomb_sigalarm(int foo)
103{
103 stop_keygen_flag = 1; 104 stop_keygen_flag = 1;
104 } 105}
105 106
106#endif 107#endif
107 108
@@ -109,372 +110,347 @@ static int dsa_cb(int p, int n, BN_GENCB *cb);
109 110
110int MAIN(int, char **); 111int MAIN(int, char **);
111 112
112int MAIN(int argc, char **argv) 113int
113 { 114MAIN(int argc, char **argv)
114 DSA *dsa=NULL; 115{
115 int i,badops=0,text=0; 116 DSA *dsa = NULL;
116 BIO *in=NULL,*out=NULL; 117 int i, badops = 0, text = 0;
117 int informat,outformat,noout=0,C=0,ret=1; 118 BIO *in = NULL, *out = NULL;
118 char *infile,*outfile,*prog,*inrand=NULL; 119 int informat, outformat, noout = 0, C = 0, ret = 1;
119 int numbits= -1,num,genkey=0; 120 char *infile, *outfile, *prog, *inrand = NULL;
120 int need_rand=0; 121 int numbits = -1, num, genkey = 0;
122 int need_rand = 0;
121#ifndef OPENSSL_NO_ENGINE 123#ifndef OPENSSL_NO_ENGINE
122 char *engine=NULL; 124 char *engine = NULL;
123#endif 125#endif
124#ifdef GENCB_TEST 126#ifdef GENCB_TEST
125 int timebomb=0; 127 int timebomb = 0;
126#endif 128#endif
127 129
128 apps_startup(); 130 apps_startup();
129 131
130 if (bio_err == NULL) 132 if (bio_err == NULL)
131 if ((bio_err=BIO_new(BIO_s_file())) != NULL) 133 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
132 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 134 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
133 135
134 if (!load_config(bio_err, NULL)) 136 if (!load_config(bio_err, NULL))
135 goto end; 137 goto end;
136 138
137 infile=NULL; 139 infile = NULL;
138 outfile=NULL; 140 outfile = NULL;
139 informat=FORMAT_PEM; 141 informat = FORMAT_PEM;
140 outformat=FORMAT_PEM; 142 outformat = FORMAT_PEM;
141 143
142 prog=argv[0]; 144 prog = argv[0];
143 argc--; 145 argc--;
144 argv++; 146 argv++;
145 while (argc >= 1) 147 while (argc >= 1) {
146 { 148 if (strcmp(*argv, "-inform") == 0) {
147 if (strcmp(*argv,"-inform") == 0) 149 if (--argc < 1)
148 { 150 goto bad;
149 if (--argc < 1) goto bad; 151 informat = str2fmt(*(++argv));
150 informat=str2fmt(*(++argv)); 152 } else if (strcmp(*argv, "-outform") == 0) {
151 } 153 if (--argc < 1)
152 else if (strcmp(*argv,"-outform") == 0) 154 goto bad;
153 { 155 outformat = str2fmt(*(++argv));
154 if (--argc < 1) goto bad; 156 } else if (strcmp(*argv, "-in") == 0) {
155 outformat=str2fmt(*(++argv)); 157 if (--argc < 1)
156 } 158 goto bad;
157 else if (strcmp(*argv,"-in") == 0)
158 {
159 if (--argc < 1) goto bad;
160 infile= *(++argv); 159 infile= *(++argv);
161 } 160 } else if (strcmp(*argv, "-out") == 0) {
162 else if (strcmp(*argv,"-out") == 0) 161 if (--argc < 1)
163 { 162 goto bad;
164 if (--argc < 1) goto bad;
165 outfile= *(++argv); 163 outfile= *(++argv);
166 } 164 }
167#ifndef OPENSSL_NO_ENGINE 165#ifndef OPENSSL_NO_ENGINE
168 else if(strcmp(*argv, "-engine") == 0) 166 else if (strcmp(*argv, "-engine") == 0) {
169 { 167 if (--argc < 1)
170 if (--argc < 1) goto bad; 168 goto bad;
171 engine = *(++argv); 169 engine = *(++argv);
172 } 170 }
173#endif 171#endif
174#ifdef GENCB_TEST 172#ifdef GENCB_TEST
175 else if(strcmp(*argv, "-timebomb") == 0) 173 else if (strcmp(*argv, "-timebomb") == 0) {
176 { 174 if (--argc < 1)
177 if (--argc < 1) goto bad; 175 goto bad;
178 timebomb = atoi(*(++argv)); 176 timebomb = atoi(*(++argv));
179 } 177 }
180#endif 178#endif
181 else if (strcmp(*argv,"-text") == 0) 179 else if (strcmp(*argv, "-text") == 0)
182 text=1; 180 text = 1;
183 else if (strcmp(*argv,"-C") == 0) 181 else if (strcmp(*argv, "-C") == 0)
184 C=1; 182 C = 1;
185 else if (strcmp(*argv,"-genkey") == 0) 183 else if (strcmp(*argv, "-genkey") == 0) {
186 { 184 genkey = 1;
187 genkey=1; 185 need_rand = 1;
188 need_rand=1; 186 } else if (strcmp(*argv, "-rand") == 0) {
189 } 187 if (--argc < 1)
190 else if (strcmp(*argv,"-rand") == 0) 188 goto bad;
191 {
192 if (--argc < 1) goto bad;
193 inrand= *(++argv); 189 inrand= *(++argv);
194 need_rand=1; 190 need_rand = 1;
195 } 191 } else if (strcmp(*argv, "-noout") == 0)
196 else if (strcmp(*argv,"-noout") == 0) 192 noout = 1;
197 noout=1; 193 else if (sscanf(*argv, "%d", &num) == 1) {
198 else if (sscanf(*argv,"%d",&num) == 1)
199 {
200 /* generate a key */ 194 /* generate a key */
201 numbits=num; 195 numbits = num;
202 need_rand=1; 196 need_rand = 1;
203 } 197 } else {
204 else 198 BIO_printf(bio_err, "unknown option %s\n", *argv);
205 { 199 badops = 1;
206 BIO_printf(bio_err,"unknown option %s\n",*argv);
207 badops=1;
208 break; 200 break;
209 } 201 }
210 argc--; 202 argc--;
211 argv++; 203 argv++;
212 } 204 }
213 205
214 if (badops) 206 if (badops) {
215 {
216bad: 207bad:
217 BIO_printf(bio_err,"%s [options] [bits] <infile >outfile\n",prog); 208 BIO_printf(bio_err, "%s [options] [bits] <infile >outfile\n", prog);
218 BIO_printf(bio_err,"where options are\n"); 209 BIO_printf(bio_err, "where options are\n");
219 BIO_printf(bio_err," -inform arg input format - DER or PEM\n"); 210 BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
220 BIO_printf(bio_err," -outform arg output format - DER or PEM\n"); 211 BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
221 BIO_printf(bio_err," -in arg input file\n"); 212 BIO_printf(bio_err, " -in arg input file\n");
222 BIO_printf(bio_err," -out arg output file\n"); 213 BIO_printf(bio_err, " -out arg output file\n");
223 BIO_printf(bio_err," -text print as text\n"); 214 BIO_printf(bio_err, " -text print as text\n");
224 BIO_printf(bio_err," -C Output C code\n"); 215 BIO_printf(bio_err, " -C Output C code\n");
225 BIO_printf(bio_err," -noout no output\n"); 216 BIO_printf(bio_err, " -noout no output\n");
226 BIO_printf(bio_err," -genkey generate a DSA key\n"); 217 BIO_printf(bio_err, " -genkey generate a DSA key\n");
227 BIO_printf(bio_err," -rand files to use for random number input\n"); 218 BIO_printf(bio_err, " -rand files to use for random number input\n");
228#ifndef OPENSSL_NO_ENGINE 219#ifndef OPENSSL_NO_ENGINE
229 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); 220 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
230#endif 221#endif
231#ifdef GENCB_TEST 222#ifdef GENCB_TEST
232 BIO_printf(bio_err," -timebomb n interrupt keygen after <n> seconds\n"); 223 BIO_printf(bio_err, " -timebomb n interrupt keygen after <n> seconds\n");
233#endif 224#endif
234 BIO_printf(bio_err," number number of bits to use for generating private key\n"); 225 BIO_printf(bio_err, " number number of bits to use for generating private key\n");
235 goto end; 226 goto end;
236 } 227 }
237 228
238 ERR_load_crypto_strings(); 229 ERR_load_crypto_strings();
239 230
240 in=BIO_new(BIO_s_file()); 231 in = BIO_new(BIO_s_file());
241 out=BIO_new(BIO_s_file()); 232 out = BIO_new(BIO_s_file());
242 if ((in == NULL) || (out == NULL)) 233 if ((in == NULL) || (out == NULL)) {
243 {
244 ERR_print_errors(bio_err); 234 ERR_print_errors(bio_err);
245 goto end; 235 goto end;
246 } 236 }
247 237
248 if (infile == NULL) 238 if (infile == NULL)
249 BIO_set_fp(in,stdin,BIO_NOCLOSE); 239 BIO_set_fp(in, stdin, BIO_NOCLOSE);
250 else 240 else {
251 { 241 if (BIO_read_filename(in, infile) <= 0) {
252 if (BIO_read_filename(in,infile) <= 0)
253 {
254 perror(infile); 242 perror(infile);
255 goto end; 243 goto end;
256 }
257 } 244 }
258 if (outfile == NULL) 245 }
259 { 246 if (outfile == NULL) {
260 BIO_set_fp(out,stdout,BIO_NOCLOSE); 247 BIO_set_fp(out, stdout, BIO_NOCLOSE);
261 } 248 } else {
262 else 249 if (BIO_write_filename(out, outfile) <= 0) {
263 {
264 if (BIO_write_filename(out,outfile) <= 0)
265 {
266 perror(outfile); 250 perror(outfile);
267 goto end; 251 goto end;
268 }
269 } 252 }
253 }
270 254
271#ifndef OPENSSL_NO_ENGINE 255#ifndef OPENSSL_NO_ENGINE
272 setup_engine(bio_err, engine, 0); 256 setup_engine(bio_err, engine, 0);
273#endif 257#endif
274 258
275 if (need_rand) 259 if (need_rand) {
276 {
277 app_RAND_load_file(NULL, bio_err, (inrand != NULL)); 260 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
278 if (inrand != NULL) 261 if (inrand != NULL)
279 BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 262 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
280 app_RAND_load_files(inrand)); 263 app_RAND_load_files(inrand));
281 } 264 }
282 265
283 if (numbits > 0) 266 if (numbits > 0) {
284 {
285 BN_GENCB cb; 267 BN_GENCB cb;
286 BN_GENCB_set(&cb, dsa_cb, bio_err); 268 BN_GENCB_set(&cb, dsa_cb, bio_err);
287 assert(need_rand); 269 assert(need_rand);
288 dsa = DSA_new(); 270 dsa = DSA_new();
289 if(!dsa) 271 if (!dsa) {
290 { 272 BIO_printf(bio_err, "Error allocating DSA object\n");
291 BIO_printf(bio_err,"Error allocating DSA object\n");
292 goto end; 273 goto end;
293 } 274 }
294 BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num); 275 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num);
295 BIO_printf(bio_err,"This could take some time\n"); 276 BIO_printf(bio_err, "This could take some time\n");
296#ifdef GENCB_TEST 277#ifdef GENCB_TEST
297 if(timebomb > 0) 278 if (timebomb > 0) {
298 { 279 struct sigaction act;
299 struct sigaction act; 280 act.sa_handler = timebomb_sigalarm;
300 act.sa_handler = timebomb_sigalarm; 281 act.sa_flags = 0;
301 act.sa_flags = 0; 282 BIO_printf(bio_err, "(though I'll stop it if not done within %d secs)\n",
302 BIO_printf(bio_err,"(though I'll stop it if not done within %d secs)\n", 283 timebomb);
303 timebomb); 284 if (sigaction(SIGALRM, &act, NULL) != 0) {
304 if(sigaction(SIGALRM, &act, NULL) != 0) 285 BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n");
305 { 286 goto end;
306 BIO_printf(bio_err,"Error, couldn't set SIGALRM handler\n");
307 goto end;
308 } 287 }
309 alarm(timebomb); 288 alarm(timebomb);
310 } 289 }
311#endif 290#endif
312 if(!DSA_generate_parameters_ex(dsa,num,NULL,0,NULL,NULL, &cb)) 291 if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, &cb)) {
313 {
314#ifdef GENCB_TEST 292#ifdef GENCB_TEST
315 if(stop_keygen_flag) 293 if (stop_keygen_flag) {
316 { 294 BIO_printf(bio_err, "DSA key generation time-stopped\n");
317 BIO_printf(bio_err,"DSA key generation time-stopped\n");
318 /* This is an asked-for behaviour! */ 295 /* This is an asked-for behaviour! */
319 ret = 0; 296 ret = 0;
320 goto end; 297 goto end;
321 } 298 }
322#endif 299#endif
323 ERR_print_errors(bio_err); 300 ERR_print_errors(bio_err);
324 BIO_printf(bio_err,"Error, DSA key generation failed\n"); 301 BIO_printf(bio_err, "Error, DSA key generation failed\n");
325 goto end; 302 goto end;
326 }
327 } 303 }
328 else if (informat == FORMAT_ASN1) 304 } else if (informat == FORMAT_ASN1)
329 dsa=d2i_DSAparams_bio(in,NULL); 305 dsa = d2i_DSAparams_bio(in, NULL);
330 else if (informat == FORMAT_PEM) 306 else if (informat == FORMAT_PEM)
331 dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL); 307 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
332 else 308 else {
333 { 309 BIO_printf(bio_err, "bad input format specified\n");
334 BIO_printf(bio_err,"bad input format specified\n");
335 goto end; 310 goto end;
336 } 311 }
337 if (dsa == NULL) 312 if (dsa == NULL) {
338 { 313 BIO_printf(bio_err, "unable to load DSA parameters\n");
339 BIO_printf(bio_err,"unable to load DSA parameters\n");
340 ERR_print_errors(bio_err); 314 ERR_print_errors(bio_err);
341 goto end; 315 goto end;
342 } 316 }
343 317
344 if (text) 318 if (text) {
345 { 319 DSAparams_print(out, dsa);
346 DSAparams_print(out,dsa); 320 }
347 } 321
348 322 if (C) {
349 if (C)
350 {
351 unsigned char *data; 323 unsigned char *data;
352 int l,len,bits_p; 324 int l, len, bits_p;
353 325
354 len=BN_num_bytes(dsa->p); 326 len = BN_num_bytes(dsa->p);
355 bits_p=BN_num_bits(dsa->p); 327 bits_p = BN_num_bits(dsa->p);
356 data=(unsigned char *)OPENSSL_malloc(len+20); 328 data = (unsigned char *)OPENSSL_malloc(len + 20);
357 if (data == NULL) 329 if (data == NULL) {
358 {
359 perror("OPENSSL_malloc"); 330 perror("OPENSSL_malloc");
360 goto end; 331 goto end;
361 } 332 }
362 l=BN_bn2bin(dsa->p,data); 333 l = BN_bn2bin(dsa->p, data);
363 printf("static unsigned char dsa%d_p[]={",bits_p); 334 printf("static unsigned char dsa%d_p[] = {", bits_p);
364 for (i=0; i<l; i++) 335 for (i = 0; i < l; i++) {
365 { 336 if ((i % 12) == 0)
366 if ((i%12) == 0) printf("\n\t"); 337 printf("\n\t");
367 printf("0x%02X,",data[i]); 338 printf("0x%02X, ", data[i]);
368 } 339 }
369 printf("\n\t};\n"); 340 printf("\n\t};\n");
370 341
371 l=BN_bn2bin(dsa->q,data); 342 l = BN_bn2bin(dsa->q, data);
372 printf("static unsigned char dsa%d_q[]={",bits_p); 343 printf("static unsigned char dsa%d_q[] = {", bits_p);
373 for (i=0; i<l; i++) 344 for (i = 0; i < l; i++) {
374 { 345 if ((i % 12) == 0)
375 if ((i%12) == 0) printf("\n\t"); 346 printf("\n\t");
376 printf("0x%02X,",data[i]); 347 printf("0x%02X, ", data[i]);
377 } 348 }
378 printf("\n\t};\n"); 349 printf("\n\t};\n");
379 350
380 l=BN_bn2bin(dsa->g,data); 351 l = BN_bn2bin(dsa->g, data);
381 printf("static unsigned char dsa%d_g[]={",bits_p); 352 printf("static unsigned char dsa%d_g[] = {", bits_p);
382 for (i=0; i<l; i++) 353 for (i = 0; i < l; i++) {
383 { 354 if ((i % 12) == 0)
384 if ((i%12) == 0) printf("\n\t"); 355 printf("\n\t");
385 printf("0x%02X,",data[i]); 356 printf("0x%02X, ", data[i]);
386 } 357 }
387 printf("\n\t};\n\n"); 358 printf("\n\t};\n\n");
388 359
389 printf("DSA *get_dsa%d()\n\t{\n",bits_p); 360 printf("DSA *get_dsa%d()\n\t{\n", bits_p);
390 printf("\tDSA *dsa;\n\n"); 361 printf("\tDSA *dsa;\n\n");
391 printf("\tif ((dsa=DSA_new()) == NULL) return(NULL);\n"); 362 printf("\tif ((dsa = DSA_new()) == NULL) return(NULL);\n");
392 printf("\tdsa->p=BN_bin2bn(dsa%d_p,sizeof(dsa%d_p),NULL);\n", 363 printf("\tdsa->p = BN_bin2bn(dsa%d_p, sizeof(dsa%d_p), NULL);\n",
393 bits_p,bits_p); 364 bits_p, bits_p);
394 printf("\tdsa->q=BN_bin2bn(dsa%d_q,sizeof(dsa%d_q),NULL);\n", 365 printf("\tdsa->q = BN_bin2bn(dsa%d_q, sizeof(dsa%d_q), NULL);\n",
395 bits_p,bits_p); 366 bits_p, bits_p);
396 printf("\tdsa->g=BN_bin2bn(dsa%d_g,sizeof(dsa%d_g),NULL);\n", 367 printf("\tdsa->g = BN_bin2bn(dsa%d_g, sizeof(dsa%d_g), NULL);\n",
397 bits_p,bits_p); 368 bits_p, bits_p);
398 printf("\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n"); 369 printf("\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n");
399 printf("\t\t{ DSA_free(dsa); return(NULL); }\n"); 370 printf("\t\t{ DSA_free(dsa); return(NULL); }\n");
400 printf("\treturn(dsa);\n\t}\n"); 371 printf("\treturn(dsa);\n\t}\n");
401 } 372 }
402
403 373
404 if (!noout) 374 if (!noout) {
405 { 375 if (outformat == FORMAT_ASN1)
406 if (outformat == FORMAT_ASN1) 376 i = i2d_DSAparams_bio(out, dsa);
407 i=i2d_DSAparams_bio(out,dsa);
408 else if (outformat == FORMAT_PEM) 377 else if (outformat == FORMAT_PEM)
409 i=PEM_write_bio_DSAparams(out,dsa); 378 i = PEM_write_bio_DSAparams(out, dsa);
410 else { 379 else {
411 BIO_printf(bio_err,"bad output format specified for outfile\n"); 380 BIO_printf(bio_err, "bad output format specified for outfile\n");
412 goto end; 381 goto end;
413 } 382 }
414 if (!i) 383 if (!i) {
415 { 384 BIO_printf(bio_err, "unable to write DSA parameters\n");
416 BIO_printf(bio_err,"unable to write DSA parameters\n");
417 ERR_print_errors(bio_err); 385 ERR_print_errors(bio_err);
418 goto end; 386 goto end;
419 }
420 } 387 }
421 if (genkey) 388 }
422 { 389 if (genkey) {
423 DSA *dsakey; 390 DSA *dsakey;
424 391
425 assert(need_rand); 392 assert(need_rand);
426 if ((dsakey=DSAparams_dup(dsa)) == NULL) goto end; 393 if ((dsakey = DSAparams_dup(dsa)) == NULL)
427 if (!DSA_generate_key(dsakey)) 394 goto end;
428 { 395 if (!DSA_generate_key(dsakey)) {
429 ERR_print_errors(bio_err); 396 ERR_print_errors(bio_err);
430 DSA_free(dsakey); 397 DSA_free(dsakey);
431 goto end; 398 goto end;
432 } 399 }
433 if (outformat == FORMAT_ASN1) 400 if (outformat == FORMAT_ASN1)
434 i=i2d_DSAPrivateKey_bio(out,dsakey); 401 i = i2d_DSAPrivateKey_bio(out, dsakey);
435 else if (outformat == FORMAT_PEM) 402 else if (outformat == FORMAT_PEM)
436 i=PEM_write_bio_DSAPrivateKey(out,dsakey,NULL,NULL,0,NULL,NULL); 403 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, NULL);
437 else { 404 else {
438 BIO_printf(bio_err,"bad output format specified for outfile\n"); 405 BIO_printf(bio_err, "bad output format specified for outfile\n");
439 DSA_free(dsakey); 406 DSA_free(dsakey);
440 goto end; 407 goto end;
441 }
442 DSA_free(dsakey);
443 } 408 }
409 DSA_free(dsakey);
410 }
444 if (need_rand) 411 if (need_rand)
445 app_RAND_write_file(NULL, bio_err); 412 app_RAND_write_file(NULL, bio_err);
446 ret=0; 413 ret = 0;
414
447end: 415end:
448 if (in != NULL) BIO_free(in); 416 if (in != NULL)
449 if (out != NULL) BIO_free_all(out); 417 BIO_free(in);
450 if (dsa != NULL) DSA_free(dsa); 418 if (out != NULL)
419 BIO_free_all(out);
420 if (dsa != NULL)
421 DSA_free(dsa);
451 apps_shutdown(); 422 apps_shutdown();
452 OPENSSL_EXIT(ret); 423 OPENSSL_EXIT(ret);
453 } 424}
454 425
455static int dsa_cb(int p, int n, BN_GENCB *cb) 426static int
456 { 427dsa_cb(int p, int n, BN_GENCB *cb)
457 char c='*'; 428{
458 429 char c = '*';
459 if (p == 0) c='.'; 430
460 if (p == 1) c='+'; 431 if (p == 0)
461 if (p == 2) c='*'; 432 c = '.';
462 if (p == 3) c='\n'; 433 if (p == 1)
463 BIO_write(cb->arg,&c,1); 434 c = '+';
435 if (p == 2)
436 c = '*';
437 if (p == 3)
438 c = '\n';
439 BIO_write(cb->arg, &c, 1);
464 (void)BIO_flush(cb->arg); 440 (void)BIO_flush(cb->arg);
465#ifdef LINT 441#ifdef LINT
466 p=n; 442 p = n;
467#endif 443#endif
468#ifdef GENCB_TEST 444#ifdef GENCB_TEST
469 if(stop_keygen_flag) 445 if (stop_keygen_flag)
470 return 0; 446 return 0;
471#endif 447#endif
472 return 1; 448 return 1;
473 } 449}
474#else /* !OPENSSL_NO_DSA */ 450#else /* !OPENSSL_NO_DSA */
475 451
476# if PEDANTIC 452# if PEDANTIC
477static void *dummy=&dummy; 453static void *dummy = &dummy;
478# endif 454# endif
479 455
480#endif 456#endif