summaryrefslogtreecommitdiff
path: root/src/lib/libssl/src/apps/dsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/src/apps/dsa.c')
-rw-r--r--src/lib/libssl/src/apps/dsa.c329
1 files changed, 157 insertions, 172 deletions
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