100
100
ERR_load_crypto_strings();
101
101
OpenSSL_add_all_algorithms();
103
while (!badarg && *args && *args[0] == '-') {
104
if (!strcmp(*args,"-v2")) {
103
while (!badarg && *args && *args[0] == '-')
105
if (!strcmp(*args,"-v2"))
107
110
cipher=EVP_get_cipherbyname(*args);
109
113
BIO_printf(bio_err,
110
114
"Unknown cipher %s\n", *args);
114
} else if (!strcmp(*args,"-v1")) {
121
else if (!strcmp(*args,"-v1"))
117
126
pbe_nid=OBJ_txt2nid(*args);
118
if(pbe_nid == NID_undef) {
127
if (pbe_nid == NID_undef)
119
129
BIO_printf(bio_err,
120
130
"Unknown PBE algorithm %s\n", *args);
124
} else if (!strcmp(*args,"-inform")) {
137
else if (!strcmp(*args,"-inform"))
127
142
informat=str2fmt(*args);
129
} else if (!strcmp(*args,"-outform")) {
146
else if (!strcmp(*args,"-outform"))
132
151
outformat=str2fmt(*args);
134
} else if (!strcmp (*args, "-topk8")) topk8 = 1;
135
else if (!strcmp (*args, "-noiter")) iter = 1;
136
else if (!strcmp (*args, "-nocrypt")) nocrypt = 1;
137
else if (!strcmp (*args, "-nooct")) p8_broken = PKCS8_NO_OCTET;
138
else if (!strcmp (*args, "-nsdb")) p8_broken = PKCS8_NS_DB;
139
else if (!strcmp (*args, "-embed")) p8_broken = PKCS8_EMBEDDED_PARAM;
155
else if (!strcmp (*args, "-topk8"))
157
else if (!strcmp (*args, "-noiter"))
159
else if (!strcmp (*args, "-nocrypt"))
161
else if (!strcmp (*args, "-nooct"))
162
p8_broken = PKCS8_NO_OCTET;
163
else if (!strcmp (*args, "-nsdb"))
164
p8_broken = PKCS8_NS_DB;
165
else if (!strcmp (*args, "-embed"))
166
p8_broken = PKCS8_EMBEDDED_PARAM;
140
167
else if (!strcmp(*args,"-passin"))
142
169
if (!args[1]) goto bad;
189
225
#ifndef OPENSSL_NO_ENGINE
190
226
BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
195
231
#ifndef OPENSSL_NO_ENGINE
196
232
e = setup_engine(bio_err, engine, 0);
199
if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
235
if (!app_passwd(bio_err, passargin, passargout, &passin, &passout))
200
237
BIO_printf(bio_err, "Error getting passwords\n");
204
if ((pbe_nid == -1) && !cipher) pbe_nid = NID_pbeWithMD5AndDES_CBC;
207
if (!(in = BIO_new_file(infile, "rb"))) {
241
if ((pbe_nid == -1) && !cipher)
242
pbe_nid = NID_pbeWithMD5AndDES_CBC;
246
if (!(in = BIO_new_file(infile, "rb")))
208
248
BIO_printf(bio_err,
209
249
"Can't open input file %s\n", infile);
212
} else in = BIO_new_fp (stdin, BIO_NOCLOSE);
254
in = BIO_new_fp (stdin, BIO_NOCLOSE);
215
if (!(out = BIO_new_file (outfile, "wb"))) {
258
if (!(out = BIO_new_file (outfile, "wb")))
216
260
BIO_printf(bio_err,
217
261
"Can't open output file %s\n", outfile);
221
267
out = BIO_new_fp (stdout, BIO_NOCLOSE);
222
268
#ifdef OPENSSL_SYS_VMS
224
270
BIO *tmpbio = BIO_new(BIO_f_linebuffer());
225
271
out = BIO_push(tmpbio, out);
231
277
BIO_free(in); /* Not needed in this section */
232
278
pkey = load_key(bio_err, infile, informat, 1,
233
279
passin, e, "key");
237
if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) {
285
if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken)))
238
287
BIO_printf(bio_err, "Error converting key\n");
239
288
ERR_print_errors(bio_err);
243
if(outformat == FORMAT_PEM)
295
if (outformat == FORMAT_PEM)
244
296
PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
245
else if(outformat == FORMAT_ASN1)
297
else if (outformat == FORMAT_ASN1)
246
298
i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
248
301
BIO_printf(bio_err, "Bad format specified for key\n");
302
PKCS8_PRIV_KEY_INFO_free(p8inf);
252
if(passout) p8pass = passout;
255
315
if (EVP_read_pw_string(pass, sizeof pass, "Enter Encryption Password:", 1))
317
PKCS8_PRIV_KEY_INFO_free(p8inf);
258
323
app_RAND_load_file(NULL, bio_err, 0);
259
324
if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
260
325
p8pass, strlen(p8pass),
261
NULL, 0, iter, p8inf))) {
326
NULL, 0, iter, p8inf)))
262
328
BIO_printf(bio_err, "Error encrypting key\n");
263
329
ERR_print_errors(bio_err);
330
PKCS8_PRIV_KEY_INFO_free(p8inf);
266
335
app_RAND_write_file(NULL, bio_err);
267
if(outformat == FORMAT_PEM)
336
if (outformat == FORMAT_PEM)
268
337
PEM_write_bio_PKCS8(out, p8);
269
else if(outformat == FORMAT_ASN1)
338
else if (outformat == FORMAT_ASN1)
270
339
i2d_PKCS8_bio(out, p8);
272
342
BIO_printf(bio_err, "Bad format specified for key\n");
343
PKCS8_PRIV_KEY_INFO_free(p8inf);
275
348
X509_SIG_free(p8);
277
351
PKCS8_PRIV_KEY_INFO_free (p8inf);
278
352
EVP_PKEY_free(pkey);
279
353
BIO_free_all(out);
280
if(passin) OPENSSL_free(passin);
281
if(passout) OPENSSL_free(passout);
355
OPENSSL_free(passin);
357
OPENSSL_free(passout);
286
if(informat == FORMAT_PEM)
363
if (informat == FORMAT_PEM)
287
364
p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in,NULL,NULL, NULL);
288
else if(informat == FORMAT_ASN1)
365
else if (informat == FORMAT_ASN1)
289
366
p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
291
369
BIO_printf(bio_err, "Bad format specified for key\n");
295
if(informat == FORMAT_PEM)
375
if (informat == FORMAT_PEM)
296
376
p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
297
else if(informat == FORMAT_ASN1)
377
else if (informat == FORMAT_ASN1)
298
378
p8 = d2i_PKCS8_bio(in, NULL);
300
381
BIO_printf(bio_err, "Bad format specified for key\n");
305
387
BIO_printf (bio_err, "Error reading key\n");
306
388
ERR_print_errors(bio_err);
309
if(passin) p8pass = passin;
312
396
EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
314
398
p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
315
399
X509_SIG_free(p8);
319
404
BIO_printf(bio_err, "Error decrypting key\n");
320
405
ERR_print_errors(bio_err);
324
if (!(pkey = EVP_PKCS82PKEY(p8inf))) {
409
if (!(pkey = EVP_PKCS82PKEY(p8inf)))
325
411
BIO_printf(bio_err, "Error converting key\n");
326
412
ERR_print_errors(bio_err);
331
418
BIO_printf(bio_err, "Warning: broken key encoding: ");
332
switch (p8inf->broken) {
419
switch (p8inf->broken)
333
421
case PKCS8_NO_OCTET:
334
422
BIO_printf(bio_err, "No Octet String in PrivateKey\n");