apps/opt: refactor input format parsing

- split OPT_FMT_PEMDER flag into OPT_FMT_PEM and OPT_FMT_DER
- add OPT_FMT_B64 option (`-inform b64`)

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/7320)
This commit is contained in:
Dr. Matthias St. Pierre 2018-09-26 08:30:54 +02:00 committed by Tomas Mraz
parent dc19f2f622
commit ca857d7332
2 changed files with 47 additions and 29 deletions

View File

@ -343,22 +343,27 @@ typedef struct string_int_pair_st {
} OPT_PAIR, STRINT_PAIR; } OPT_PAIR, STRINT_PAIR;
/* Flags to pass into opt_format; see FORMAT_xxx, below. */ /* Flags to pass into opt_format; see FORMAT_xxx, below. */
# define OPT_FMT_PEMDER (1L << 1) # define OPT_FMT_PEM (1L << 1)
# define OPT_FMT_PKCS12 (1L << 2) # define OPT_FMT_DER (1L << 2)
# define OPT_FMT_SMIME (1L << 3) # define OPT_FMT_B64 (1L << 3)
# define OPT_FMT_ENGINE (1L << 4) # define OPT_FMT_PKCS12 (1L << 4)
# define OPT_FMT_MSBLOB (1L << 5) # define OPT_FMT_SMIME (1L << 5)
/* (1L << 6) was OPT_FMT_NETSCAPE, but wasn't used */ # define OPT_FMT_ENGINE (1L << 6)
# define OPT_FMT_NSS (1L << 7) # define OPT_FMT_MSBLOB (1L << 7)
# define OPT_FMT_TEXT (1L << 8) # define OPT_FMT_NSS (1L << 8)
# define OPT_FMT_HTTP (1L << 9) # define OPT_FMT_TEXT (1L << 9)
# define OPT_FMT_PVK (1L << 10) # define OPT_FMT_HTTP (1L << 10)
# define OPT_FMT_PVK (1L << 11)
# define OPT_FMT_PEMDER (OPT_FMT_PEM | OPT_FMT_DER)
# define OPT_FMT_ASN1 (OPT_FMT_PEM | OPT_FMT_DER | OPT_FMT_B64)
# define OPT_FMT_PDE (OPT_FMT_PEMDER | OPT_FMT_ENGINE) # define OPT_FMT_PDE (OPT_FMT_PEMDER | OPT_FMT_ENGINE)
# define OPT_FMT_PDS (OPT_FMT_PEMDER | OPT_FMT_SMIME) # define OPT_FMT_PDS (OPT_FMT_PEMDER | OPT_FMT_SMIME)
# define OPT_FMT_ANY ( \ # define OPT_FMT_ANY ( \
OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_SMIME | \ OPT_FMT_PEM | OPT_FMT_DER | OPT_FMT_B64 | \
OPT_FMT_ENGINE | OPT_FMT_MSBLOB | OPT_FMT_NSS | \ OPT_FMT_PKCS12 | OPT_FMT_SMIME | \
OPT_FMT_TEXT | OPT_FMT_HTTP | OPT_FMT_PVK) OPT_FMT_ENGINE | OPT_FMT_MSBLOB | OPT_FMT_NSS | \
OPT_FMT_TEXT | OPT_FMT_HTTP | OPT_FMT_PVK)
/* Divide options into sections when displaying usage */ /* Divide options into sections when displaying usage */
#define OPT_SECTION(sec) { OPT_SECTION_STR, 1, '-', sec " options:\n" } #define OPT_SECTION(sec) { OPT_SECTION_STR, 1, '-', sec " options:\n" }

View File

@ -194,7 +194,7 @@ char *opt_init(int ac, char **av, const OPTIONS *o)
case 0: case '-': case '.': case 0: case '-': case '.':
case '/': case '<': case '>': case 'E': case 'F': case '/': case '<': case '>': case 'E': case 'F':
case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's': case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
case 'u': case 'c': case ':': case 'N': case 'u': case 'c': case ':': case 'N': case 'A':
break; break;
default: default:
OPENSSL_assert(0); OPENSSL_assert(0);
@ -225,7 +225,9 @@ char *opt_init(int ac, char **av, const OPTIONS *o)
} }
static OPT_PAIR formats[] = { static OPT_PAIR formats[] = {
{"PEM/DER", OPT_FMT_PEMDER}, {"pem", OPT_FMT_PEM},
{"der", OPT_FMT_DER},
{"b64", OPT_FMT_B64},
{"pkcs12", OPT_FMT_PKCS12}, {"pkcs12", OPT_FMT_PKCS12},
{"smime", OPT_FMT_SMIME}, {"smime", OPT_FMT_SMIME},
{"engine", OPT_FMT_ENGINE}, {"engine", OPT_FMT_ENGINE},
@ -247,16 +249,12 @@ static int opt_format_error(const char *s, unsigned long flags)
{ {
OPT_PAIR *ap; OPT_PAIR *ap;
if (flags == OPT_FMT_PEMDER) { opt_printf_stderr("%s: Bad format \"%s\"; must be one of: ", prog, s);
opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n", for (ap = formats; ap->name; ap++)
prog, s); if (flags & ap->retval)
} else { opt_printf_stderr(" %s", ap->name);
opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n", opt_printf_stderr("\n");
prog, s);
for (ap = formats; ap->name; ap++)
if (flags & ap->retval)
opt_printf_stderr(" %s\n", ap->name);
}
return 0; return 0;
} }
@ -267,9 +265,21 @@ int opt_format(const char *s, unsigned long flags, int *result)
default: default:
opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s); opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
return 0; return 0;
case 'B':
case 'b':
if (s[1] == '\0'
|| strcmp(s, "B64") == 0 || strcmp(s, "b64") == 0
|| strcmp(s, "BASE64") == 0 || strcmp(s, "base64") == 0 ) {
if ((flags & OPT_FMT_B64) == 0)
return opt_format_error(s, flags);
*result = FORMAT_BASE64;
} else {
return 0;
}
break;
case 'D': case 'D':
case 'd': case 'd':
if ((flags & OPT_FMT_PEMDER) == 0) if ((flags & OPT_FMT_DER) == 0)
return opt_format_error(s, flags); return opt_format_error(s, flags);
*result = FORMAT_ASN1; *result = FORMAT_ASN1;
break; break;
@ -319,7 +329,7 @@ int opt_format(const char *s, unsigned long flags, int *result)
case 'P': case 'P':
case 'p': case 'p':
if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) { if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
if ((flags & OPT_FMT_PEMDER) == 0) if ((flags & OPT_FMT_PEM) == 0)
return opt_format_error(s, flags); return opt_format_error(s, flags);
*result = FORMAT_PEM; *result = FORMAT_PEM;
} else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) { } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
@ -976,11 +986,14 @@ int opt_next(void)
case 'E': case 'E':
case 'F': case 'F':
case 'f': case 'f':
case 'A':
case 'a':
if (opt_format(arg, if (opt_format(arg,
o->valtype == 'c' ? OPT_FMT_PDS : o->valtype == 'c' ? OPT_FMT_PDS :
o->valtype == 'E' ? OPT_FMT_PDE : o->valtype == 'E' ? OPT_FMT_PDE :
o->valtype == 'F' ? OPT_FMT_PEMDER o->valtype == 'F' ? OPT_FMT_PEMDER :
: OPT_FMT_ANY, &ival)) o->valtype == 'A' ? OPT_FMT_ASN1 :
OPT_FMT_ANY, &ival))
break; break;
opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n", opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n",
prog, arg, o->name); prog, arg, o->name);