Skip to content

Commit

Permalink
apps/opt: refactor input format parsing
Browse files Browse the repository at this point in the history
- 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 #7320)
  • Loading branch information
mspncp authored and t8m committed Jun 9, 2023
1 parent dc19f2f commit ca857d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
31 changes: 18 additions & 13 deletions apps/include/opt.h
Expand Up @@ -343,22 +343,27 @@ typedef struct string_int_pair_st {
} OPT_PAIR, STRINT_PAIR;

/* Flags to pass into opt_format; see FORMAT_xxx, below. */
# define OPT_FMT_PEMDER (1L << 1)
# define OPT_FMT_PKCS12 (1L << 2)
# define OPT_FMT_SMIME (1L << 3)
# define OPT_FMT_ENGINE (1L << 4)
# define OPT_FMT_MSBLOB (1L << 5)
/* (1L << 6) was OPT_FMT_NETSCAPE, but wasn't used */
# define OPT_FMT_NSS (1L << 7)
# define OPT_FMT_TEXT (1L << 8)
# define OPT_FMT_HTTP (1L << 9)
# define OPT_FMT_PVK (1L << 10)
# define OPT_FMT_PEM (1L << 1)
# define OPT_FMT_DER (1L << 2)
# define OPT_FMT_B64 (1L << 3)
# define OPT_FMT_PKCS12 (1L << 4)
# define OPT_FMT_SMIME (1L << 5)
# define OPT_FMT_ENGINE (1L << 6)
# define OPT_FMT_MSBLOB (1L << 7)
# define OPT_FMT_NSS (1L << 8)
# define OPT_FMT_TEXT (1L << 9)
# 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_PDS (OPT_FMT_PEMDER | OPT_FMT_SMIME)
# define OPT_FMT_ANY ( \
OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_SMIME | \
OPT_FMT_ENGINE | OPT_FMT_MSBLOB | OPT_FMT_NSS | \
OPT_FMT_TEXT | OPT_FMT_HTTP | OPT_FMT_PVK)
OPT_FMT_PEM | OPT_FMT_DER | OPT_FMT_B64 | \
OPT_FMT_PKCS12 | OPT_FMT_SMIME | \
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 */
#define OPT_SECTION(sec) { OPT_SECTION_STR, 1, '-', sec " options:\n" }
Expand Down
45 changes: 29 additions & 16 deletions apps/lib/opt.c
Expand Up @@ -194,7 +194,7 @@ char *opt_init(int ac, char **av, const OPTIONS *o)
case 0: case '-': case '.':
case '/': case '<': case '>': case 'E': case 'F':
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;
default:
OPENSSL_assert(0);
Expand Down Expand Up @@ -225,7 +225,9 @@ char *opt_init(int ac, char **av, const OPTIONS *o)
}

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},
{"smime", OPT_FMT_SMIME},
{"engine", OPT_FMT_ENGINE},
Expand All @@ -247,16 +249,12 @@ static int opt_format_error(const char *s, unsigned long flags)
{
OPT_PAIR *ap;

if (flags == OPT_FMT_PEMDER) {
opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
prog, s);
} else {
opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
prog, s);
for (ap = formats; ap->name; ap++)
if (flags & ap->retval)
opt_printf_stderr(" %s\n", ap->name);
}
opt_printf_stderr("%s: Bad format \"%s\"; must be one of: ", prog, s);
for (ap = formats; ap->name; ap++)
if (flags & ap->retval)
opt_printf_stderr(" %s", ap->name);
opt_printf_stderr("\n");

return 0;
}

Expand All @@ -267,9 +265,21 @@ int opt_format(const char *s, unsigned long flags, int *result)
default:
opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
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':
if ((flags & OPT_FMT_PEMDER) == 0)
if ((flags & OPT_FMT_DER) == 0)
return opt_format_error(s, flags);
*result = FORMAT_ASN1;
break;
Expand Down Expand Up @@ -319,7 +329,7 @@ int opt_format(const char *s, unsigned long flags, int *result)
case 'P':
case 'p':
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);
*result = FORMAT_PEM;
} else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
Expand Down Expand Up @@ -976,11 +986,14 @@ int opt_next(void)
case 'E':
case 'F':
case 'f':
case 'A':
case 'a':
if (opt_format(arg,
o->valtype == 'c' ? OPT_FMT_PDS :
o->valtype == 'E' ? OPT_FMT_PDE :
o->valtype == 'F' ? OPT_FMT_PEMDER
: OPT_FMT_ANY, &ival))
o->valtype == 'F' ? OPT_FMT_PEMDER :
o->valtype == 'A' ? OPT_FMT_ASN1 :
OPT_FMT_ANY, &ival))
break;
opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n",
prog, arg, o->name);
Expand Down

0 comments on commit ca857d7

Please sign in to comment.