From 5c033291fa870bdc6408c02fcac5bed1258a18fe Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 11 Apr 2015 15:21:42 +0000 Subject: [PATCH] Convert openssl(1) pkeyparam to new option handling. --- usr.bin/openssl/pkeyparam.c | 135 ++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 61 deletions(-) diff --git a/usr.bin/openssl/pkeyparam.c b/usr.bin/openssl/pkeyparam.c index d9e4085e254..25213628047 100644 --- a/usr.bin/openssl/pkeyparam.c +++ b/usr.bin/openssl/pkeyparam.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pkeyparam.c,v 1.3 2014/08/28 14:25:48 jsing Exp $ */ +/* $OpenBSD: pkeyparam.c,v 1.4 2015/04/11 15:21:42 jsing Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006 */ @@ -65,83 +65,96 @@ #include #include +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 argc, char **argv) { - char **args, *infile = NULL, *outfile = NULL; BIO *in = NULL, *out = NULL; - int text = 0, noout = 0; EVP_PKEY *pkey = NULL; - int badarg = 0; -#ifndef OPENSSL_NO_ENGINE - char *engine = NULL; -#endif int ret = 1; - args = argv + 1; - 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 + memset(&pkeyparam_config, 0, sizeof(pkeyparam_config)); - else if (strcmp(*args, "-text") == 0) - text = 1; - else if (strcmp(*args, "-noout") == 0) - noout = 1; - args++; + if (options_parse(argc, argv, pkeyparam_options, NULL, NULL) != 0) { + pkeyparam_usage(); + return (1); } - if (badarg) { -#ifndef OPENSSL_NO_ENGINE -bad: -#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); + setup_engine(bio_err, pkeyparam_config.engine, 0); #endif - if (infile) { - if (!(in = BIO_new_file(infile, "r"))) { - BIO_printf(bio_err, - "Can't open input file %s\n", infile); - goto end; + if (pkeyparam_config.infile) { + if (!(in = BIO_new_file(pkeyparam_config.infile, "r"))) { + BIO_printf(bio_err, "Can't open input file %s\n", + pkeyparam_config.infile); } } else in = BIO_new_fp(stdin, BIO_NOCLOSE); - if (outfile) { - if (!(out = BIO_new_file(outfile, "w"))) { - BIO_printf(bio_err, - "Can't open output file %s\n", outfile); + if (pkeyparam_config.outfile) { + if (!(out = BIO_new_file(pkeyparam_config.outfile, "w"))) { + BIO_printf(bio_err, "Can't open output file %s\n", + pkeyparam_config.outfile); goto end; } } else { @@ -154,10 +167,10 @@ bad: ERR_print_errors(bio_err); goto end; } - if (!noout) + if (!pkeyparam_config.noout) PEM_write_bio_Parameters(out, pkey); - if (text) + if (pkeyparam_config.text) EVP_PKEY_print_params(out, pkey, 0, NULL); ret = 0; -- 2.20.1