From: Lennart Poettering Date: Fri, 16 Sep 2022 21:42:05 +0000 (+0200) Subject: tpm2-util: make tpm2_pcr_bank_from_string() case-insensitive X-Git-Tag: v252-rc1~144 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f92ebc86dd42b4fab150eb9a220eff3d1c5cd686;p=thirdparty%2Fsystemd.git tpm2-util: make tpm2_pcr_bank_from_string() case-insensitive This way we can use it directly in measure.c, and thus remove a bit of redundant code. OpenSSL prefers uppercasing the MD names, others don't hence let's be lenient here. --- diff --git a/src/boot/measure.c b/src/boot/measure.c index 983d407e39b..eee076075cb 100644 --- a/src/boot/measure.c +++ b/src/boot/measure.c @@ -531,22 +531,6 @@ static int verb_calculate(int argc, char *argv[], void *userdata) { return 0; } -static TPM2_ALG_ID convert_evp_md_name_to_tpm2_alg(const EVP_MD *md) { - const char *mdname; - - mdname = EVP_MD_name(md); - if (strcaseeq(mdname, "sha1")) - return TPM2_ALG_SHA1; - if (strcaseeq(mdname, "sha256")) - return TPM2_ALG_SHA256; - if (strcaseeq(mdname, "sha384")) - return TPM2_ALG_SHA384; - if (strcaseeq(mdname, "sha512")) - return TPM2_ALG_SHA512; - - return TPM2_ALG_ERROR; -} - static int verb_sign(int argc, char *argv[], void *userdata) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; _cleanup_(pcr_state_free_all) PcrState *pcr_states = NULL; @@ -637,9 +621,9 @@ static int verb_sign(int argc, char *argv[], void *userdata) { assert(sizeof(intermediate_digest.buffer) >= SHA256_DIGEST_SIZE); sha256_direct(p->value, p->value_size, intermediate_digest.buffer); - TPM2_ALG_ID tpmalg = convert_evp_md_name_to_tpm2_alg(p->md); - if (tpmalg == TPM2_ALG_ERROR) { - r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unsupported PCR bank"); + int tpmalg = tpm2_pcr_bank_from_string(EVP_MD_name(p->md)); + if (tpmalg < 0) { + log_error_errno(tpmalg, "Unsupported PCR bank"); goto finish; } diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 487d258ca99..a7af7528d7f 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -2048,13 +2048,13 @@ const char *tpm2_pcr_bank_to_string(uint16_t bank) { } int tpm2_pcr_bank_from_string(const char *bank) { - if (streq_ptr(bank, "sha1")) + if (strcaseeq_ptr(bank, "sha1")) return TPM2_ALG_SHA1; - if (streq_ptr(bank, "sha256")) + if (strcaseeq_ptr(bank, "sha256")) return TPM2_ALG_SHA256; - if (streq_ptr(bank, "sha384")) + if (strcaseeq_ptr(bank, "sha384")) return TPM2_ALG_SHA384; - if (streq_ptr(bank, "sha512")) + if (strcaseeq_ptr(bank, "sha512")) return TPM2_ALG_SHA512; return -EINVAL; } @@ -2068,9 +2068,9 @@ const char *tpm2_primary_alg_to_string(uint16_t alg) { } int tpm2_primary_alg_from_string(const char *alg) { - if (streq_ptr(alg, "ecc")) + if (strcaseeq_ptr(alg, "ecc")) return TPM2_ALG_ECC; - if (streq_ptr(alg, "rsa")) + if (strcaseeq_ptr(alg, "rsa")) return TPM2_ALG_RSA; return -EINVAL; }