X-Git-Url: https://git.openssl.org/gitweb/?a=blobdiff_plain;f=apps%2Fpkey.c;h=6f5ecf0ce658cd5842788deb5d0be5451b64614c;hb=6a78ae2821e89a8838714496524fd39d9d21fb1b;hp=3597be0ee6afd83c4a2ce5cda71698c4bd364f6b;hpb=7e1b7485706c2b11091b5fa897fe496a2faa56cc;p=openssl.git diff --git a/apps/pkey.c b/apps/pkey.c index 3597be0ee6..6f5ecf0ce6 100644 --- a/apps/pkey.c +++ b/apps/pkey.c @@ -75,7 +75,7 @@ OPTIONS pkey_options[] = { {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"}, {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, - {"in", OPT_IN, '<', "Input file"}, + {"in", OPT_IN, 's', "Input key"}, {"out", OPT_OUT, '>', "Output file"}, {"pubin", OPT_PUBIN, '-', "Read public key from input (default is private key)"}, @@ -97,10 +97,11 @@ int pkey_main(int argc, char **argv) EVP_PKEY *pkey = NULL; const EVP_CIPHER *cipher = NULL; char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL; - char *passinarg = NULL, *passoutarg = NULL, *prog, *engine = NULL; + char *passinarg = NULL, *passoutarg = NULL, *prog; OPTION_CHOICE o; int informat = FORMAT_PEM, outformat = FORMAT_PEM; int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1; + int private = 0; prog = opt_init(argc, argv, pkey_options); while ((o = opt_next()) != OPT_EOF) { @@ -115,7 +116,7 @@ int pkey_main(int argc, char **argv) ret = 0; goto end; case OPT_INFORM: - if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) + if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) goto opthelp; break; case OPT_OUTFORM: @@ -129,7 +130,7 @@ int pkey_main(int argc, char **argv) passoutarg = opt_arg(); break; case OPT_ENGINE: - engine = opt_arg(); + e = setup_engine(opt_arg(), 0); break; case OPT_IN: infile = opt_arg(); @@ -159,17 +160,16 @@ int pkey_main(int argc, char **argv) } argc = opt_num_rest(); argv = opt_rest(); - -#ifndef OPENSSL_NO_ENGINE - e = setup_engine(engine, 0); -#endif + private = !noout && !pubout ? 1 : 0; + if (text && !pubtext) + private = 1; if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { BIO_printf(bio_err, "Error getting passwords\n"); goto end; } - out = bio_open_default(outfile, "wb"); + out = bio_open_owner(outfile, outformat, private); if (out == NULL) goto end; @@ -184,14 +184,18 @@ int pkey_main(int argc, char **argv) if (outformat == FORMAT_PEM) { if (pubout) PEM_write_bio_PUBKEY(out, pkey); - else + else { + assert(private); PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, passout); + } } else if (outformat == FORMAT_ASN1) { if (pubout) i2d_PUBKEY_bio(out, pkey); - else + else { + assert(private); i2d_PrivateKey_bio(out, pkey); + } } else { BIO_printf(bio_err, "Bad format specified for key\n"); goto end; @@ -202,8 +206,10 @@ int pkey_main(int argc, char **argv) if (text) { if (pubtext) EVP_PKEY_print_public(out, pkey, 0, NULL); - else + else { + assert(private); EVP_PKEY_print_private(out, pkey, 0, NULL); + } } ret = 0; @@ -212,10 +218,8 @@ int pkey_main(int argc, char **argv) EVP_PKEY_free(pkey); BIO_free_all(out); BIO_free(in); - if (passin) - OPENSSL_free(passin); - if (passout) - OPENSSL_free(passout); + OPENSSL_free(passin); + OPENSSL_free(passout); return ret; }