]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2-util: make tpm2_pcr_bank_from_string() case-insensitive
authorLennart Poettering <lennart@poettering.net>
Fri, 16 Sep 2022 21:42:05 +0000 (23:42 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 19 Sep 2022 13:50:44 +0000 (22:50 +0900)
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.

src/boot/measure.c
src/shared/tpm2-util.c

index 983d407e39b056dcfb1cd7ef27f06c22ddfbdabf..eee076075cbba04671fd5d9cb9b0e218e3a5d018 100644 (file)
@@ -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;
                 }
 
index 487d258ca99773ded7af82ce758ce435e2ea8856..a7af7528d7fcc54b4e580f3c8c81165bcb37b55f 100644 (file)
@@ -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;
 }