version 1.3, 2014/08/28 14:25:48 |
version 1.4, 2015/04/11 15:21:42 |
|
|
#include <openssl/evp.h> |
#include <openssl/evp.h> |
#include <openssl/pem.h> |
#include <openssl/pem.h> |
|
|
|
struct { |
|
#ifndef OPENSSL_NO_ENGINE |
|
char *engine; |
|
#endif |
|
char *infile; |
|
int noout; |
|
char *outfile; |
|
int text; |
|
} pkeyparam_config; |
|
|
|
struct option pkeyparam_options[] = { |
|
#ifndef OPENSSL_NO_ENGINE |
|
{ |
|
.name = "engine", |
|
.argname = "id", |
|
.desc = "Use the engine specified by the given identifier", |
|
.type = OPTION_ARG, |
|
.opt.arg = &pkeyparam_config.engine, |
|
}, |
|
#endif |
|
{ |
|
.name = "in", |
|
.argname = "file", |
|
.desc = "Input file (default stdin)", |
|
.type = OPTION_ARG, |
|
.opt.arg = &pkeyparam_config.infile, |
|
}, |
|
{ |
|
.name = "noout", |
|
.desc = "Do not print encoded version of the parameters", |
|
.type = OPTION_FLAG, |
|
.opt.flag = &pkeyparam_config.noout, |
|
}, |
|
{ |
|
.name = "out", |
|
.argname = "file", |
|
.desc = "Output file (default stdout)", |
|
.type = OPTION_ARG, |
|
.opt.arg = &pkeyparam_config.outfile, |
|
}, |
|
{ |
|
.name = "text", |
|
.desc = "Print out the parameters in plain text", |
|
.type = OPTION_FLAG, |
|
.opt.flag = &pkeyparam_config.text, |
|
}, |
|
{ NULL }, |
|
}; |
|
|
|
static void |
|
pkeyparam_usage() |
|
{ |
|
fprintf(stderr, |
|
"usage: pkeyparam [-engine id] [-in file] [-noout] [-out file] " |
|
"[-text]\n"); |
|
options_usage(pkeyparam_options); |
|
} |
|
|
int pkeyparam_main(int, char **); |
int pkeyparam_main(int, char **); |
|
|
int |
int |
pkeyparam_main(int argc, char **argv) |
pkeyparam_main(int argc, char **argv) |
{ |
{ |
char **args, *infile = NULL, *outfile = NULL; |
|
BIO *in = NULL, *out = NULL; |
BIO *in = NULL, *out = NULL; |
int text = 0, noout = 0; |
|
EVP_PKEY *pkey = NULL; |
EVP_PKEY *pkey = NULL; |
int badarg = 0; |
|
#ifndef OPENSSL_NO_ENGINE |
|
char *engine = NULL; |
|
#endif |
|
int ret = 1; |
int ret = 1; |
|
|
args = argv + 1; |
memset(&pkeyparam_config, 0, sizeof(pkeyparam_config)); |
while (!badarg && *args && *args[0] == '-') { |
|
if (!strcmp(*args, "-in")) { |
|
if (args[1]) { |
|
args++; |
|
infile = *args; |
|
} else |
|
badarg = 1; |
|
} else if (!strcmp(*args, "-out")) { |
|
if (args[1]) { |
|
args++; |
|
outfile = *args; |
|
} else |
|
badarg = 1; |
|
} |
|
#ifndef OPENSSL_NO_ENGINE |
|
else if (strcmp(*args, "-engine") == 0) { |
|
if (!args[1]) |
|
goto bad; |
|
engine = *(++args); |
|
} |
|
#endif |
|
|
|
else if (strcmp(*args, "-text") == 0) |
if (options_parse(argc, argv, pkeyparam_options, NULL, NULL) != 0) { |
text = 1; |
pkeyparam_usage(); |
else if (strcmp(*args, "-noout") == 0) |
return (1); |
noout = 1; |
|
args++; |
|
} |
} |
|
|
if (badarg) { |
|
#ifndef OPENSSL_NO_ENGINE |
#ifndef OPENSSL_NO_ENGINE |
bad: |
setup_engine(bio_err, pkeyparam_config.engine, 0); |
#endif |
#endif |
BIO_printf(bio_err, "Usage pkeyparam [options]\n"); |
|
BIO_printf(bio_err, "where options are\n"); |
|
BIO_printf(bio_err, "-in file input file\n"); |
|
BIO_printf(bio_err, "-out file output file\n"); |
|
BIO_printf(bio_err, "-text print parameters as text\n"); |
|
BIO_printf(bio_err, "-noout don't output encoded parameters\n"); |
|
#ifndef OPENSSL_NO_ENGINE |
|
BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n"); |
|
#endif |
|
return 1; |
|
} |
|
#ifndef OPENSSL_NO_ENGINE |
|
setup_engine(bio_err, engine, 0); |
|
#endif |
|
|
|
if (infile) { |
if (pkeyparam_config.infile) { |
if (!(in = BIO_new_file(infile, "r"))) { |
if (!(in = BIO_new_file(pkeyparam_config.infile, "r"))) { |
BIO_printf(bio_err, |
BIO_printf(bio_err, "Can't open input file %s\n", |
"Can't open input file %s\n", infile); |
pkeyparam_config.infile); |
goto end; |
|
} |
} |
} else |
} else |
in = BIO_new_fp(stdin, BIO_NOCLOSE); |
in = BIO_new_fp(stdin, BIO_NOCLOSE); |
|
|
if (outfile) { |
if (pkeyparam_config.outfile) { |
if (!(out = BIO_new_file(outfile, "w"))) { |
if (!(out = BIO_new_file(pkeyparam_config.outfile, "w"))) { |
BIO_printf(bio_err, |
BIO_printf(bio_err, "Can't open output file %s\n", |
"Can't open output file %s\n", outfile); |
pkeyparam_config.outfile); |
goto end; |
goto end; |
} |
} |
} else { |
} else { |
|
|
ERR_print_errors(bio_err); |
ERR_print_errors(bio_err); |
goto end; |
goto end; |
} |
} |
if (!noout) |
if (!pkeyparam_config.noout) |
PEM_write_bio_Parameters(out, pkey); |
PEM_write_bio_Parameters(out, pkey); |
|
|
if (text) |
if (pkeyparam_config.text) |
EVP_PKEY_print_params(out, pkey, 0, NULL); |
EVP_PKEY_print_params(out, pkey, 0, NULL); |
|
|
ret = 0; |
ret = 0; |