From b0ac1d4d5f7bf1b7f3b5afe6b966a2eec5ebdca6 Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Mon, 21 Aug 2023 19:02:18 -0400 Subject: [PATCH] tpm2: use switch() instead of if-else (cherry picked from commit 3f4d5dfd651864adf94e43ffdc6303a41f96fcd4) Related: RHEL-16182 --- src/shared/tpm2-util.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 8a53c0ae59..f29515be14 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -3493,7 +3493,8 @@ int tpm2_tpm2b_public_to_openssl_pkey(const TPM2B_PUBLIC *public, EVP_PKEY **ret assert(ret); const TPMT_PUBLIC *p = &public->publicArea; - if (p->type == TPM2_ALG_ECC) { + switch (p->type) { + case TPM2_ALG_ECC: { int curve_id; r = tpm2_ecc_curve_to_openssl_curve_id(p->parameters.eccDetail.curveID, &curve_id); if (r < 0) @@ -3508,8 +3509,7 @@ int tpm2_tpm2b_public_to_openssl_pkey(const TPM2B_PUBLIC *public, EVP_PKEY **ret point->y.size, ret); } - - if (p->type == TPM2_ALG_RSA) { + case TPM2_ALG_RSA: { /* TPM specification Part 2 ("Structures") section for TPMS_RSA_PARAMS states "An exponent of * zero indicates that the exponent is the default of 2^16 + 1". */ uint32_t exponent = htobe32(p->parameters.rsaDetail.exponent ?: TPM2_RSA_DEFAULT_EXPONENT); @@ -3520,9 +3520,10 @@ int tpm2_tpm2b_public_to_openssl_pkey(const TPM2B_PUBLIC *public, EVP_PKEY **ret sizeof(exponent), ret); } - - return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), - "TPM2 asymmetric algorithm 0x%" PRIx16 " not supported.", p->type); + default: + return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "TPM2 asymmetric algorithm 0x%" PRIx16 " not supported.", p->type); + } } int tpm2_tpm2b_public_from_openssl_pkey(const EVP_PKEY *pkey, TPM2B_PUBLIC *ret) { @@ -3546,7 +3547,8 @@ int tpm2_tpm2b_public_from_openssl_pkey(const EVP_PKEY *pkey, TPM2B_PUBLIC *ret) key_id = EVP_PKEY_id(pkey); #endif - if (key_id == EVP_PKEY_EC) { + switch (key_id) { + case EVP_PKEY_EC: { public.type = TPM2_ALG_ECC; int curve_id; @@ -3576,7 +3578,10 @@ int tpm2_tpm2b_public_from_openssl_pkey(const EVP_PKEY *pkey, TPM2B_PUBLIC *ret) return log_error_errno(r, "ECC key y size %zu too large.", y_size); public.unique.ecc.y = TPM2B_ECC_PARAMETER_MAKE(y, y_size); - } else if (key_id == EVP_PKEY_RSA) { + + break; + } + case EVP_PKEY_RSA: { public.type = TPM2_ALG_RSA; _cleanup_free_ void *n = NULL, *e = NULL; @@ -3602,9 +3607,13 @@ int tpm2_tpm2b_public_from_openssl_pkey(const EVP_PKEY *pkey, TPM2B_PUBLIC *ret) if (exponent == TPM2_RSA_DEFAULT_EXPONENT) exponent = 0; public.parameters.rsaDetail.exponent = exponent; - } else + + break; + } + default: return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "EVP_PKEY type %d not supported.", key_id); + } *ret = (TPM2B_PUBLIC) { .size = sizeof(public),