swtpm_cert: Use keychoice variable to disallow conflicting CLI options

Introduce a 'keychoice' variable and set it when the user provides an
option for a particular type of key, such as the modulus or exponent for an
RSA key or the x and y parameters for an EC key. Check that no conflicting
parameters are provided.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
This commit is contained in:
Stefan Berger 2026-04-01 17:38:51 -04:00 committed by Stefan Berger
parent dc84d9ba7b
commit caec46bacb

View File

@ -1034,8 +1034,20 @@ static void capabilities_print_json(void)
"}\n");
}
int
main(int argc, char *argv[])
static bool check_keychoice(const char **keychoice, const char *kc)
{
if (*keychoice && strcmp(*keychoice, kc) != 0) {
fprintf(stderr,
"Already found options for '%s' key; cannot switch to '%s'.\n",
*keychoice, kc);
return false;
}
*keychoice = kc;
return true;
}
int main(int argc, char *argv[])
{
int ret = 1;
EVP_PKEY *pubkey = NULL;
@ -1092,6 +1104,7 @@ main(int argc, char *argv[])
int flags = 0;
bool is_ecc = false;
char *endptr;
const char *keychoice = NULL;
static struct option long_options[] = {
{"pubkey", required_argument, NULL, 'p'},
{"modulus", required_argument, NULL, 'm'},
@ -1142,30 +1155,39 @@ main(int argc, char *argv[])
#endif
switch (opt) {
case 'p': /* --pubkey */
if (!check_keychoice(&keychoice, "read from file"))
goto cleanup;
pubkey_filename = optarg;
break;
case 'm': /* --modulus */
free(modulus_bin);
if (!(modulus_bin = hex_str_to_bin(optarg, &modulus_len))) {
if (!(modulus_bin = hex_str_to_bin(optarg, &modulus_len)))
goto cleanup;
if (!check_keychoice(&keychoice, "RSA"))
goto cleanup;
}
break;
case 'x': /* --ecc-x */
free(ecc_x_bin);
if (!(ecc_x_bin = hex_str_to_bin(optarg, &ecc_x_len))) {
if (!(ecc_x_bin = hex_str_to_bin(optarg, &ecc_x_len)))
goto cleanup;
if (!check_keychoice(&keychoice, "EC"))
goto cleanup;
}
break;
case 'y': /* --ecc-y */
free(ecc_y_bin);
if (!(ecc_y_bin = hex_str_to_bin(optarg, &ecc_y_len))) {
if (!(ecc_y_bin = hex_str_to_bin(optarg, &ecc_y_len)))
goto cleanup;
if (!check_keychoice(&keychoice, "EC"))
goto cleanup;
}
break;
case 'z': /* --ecc-curveid */
if (!check_keychoice(&keychoice, "EC"))
goto cleanup;
ecc_curveid = optarg;
break;
case 'e': /* --exponent */
if (!check_keychoice(&keychoice, "RSA"))
goto cleanup;
errno = 0;
exponent = strtol(optarg, &endptr, 0);
if (errno || endptr == optarg || *endptr != '\0') {
@ -1337,9 +1359,8 @@ main(int argc, char *argv[])
goto cleanup;
}
if (modulus_bin && (ecc_x_bin || ecc_y_bin)) {
fprintf(stderr, "RSA modulus and ECC parameters cannot both be "
"given.\n");
if (keychoice == NULL) {
fprintf(stderr, "No parameters for a public key were given.\n");
goto cleanup;
}