Current pkcs7_get_digest() code fails with any message that has more than
one SignerInfo, so the content digest of a multi-signed message can't be
obtained, and it also doesn't support signers whose algorithm signs the
data directly (eg. ML-DSA) without authenticated attributes, which has no
content digest to report.

Give it a signer zero-index and add pkcs7_get_signer_count(), so callers
can get the content digest of each signer of a message. Report a signer
with no content digest as a zero-length digest rather than an error, and
return the name of the signature algorithm when it signs the data directly.

This commit prepares the ground for ima-modsigv2 template, maintaining
current field templates behavior.

Signed-off-by: Bruno Meneguele <[email protected]>
---
 crypto/asymmetric_keys/pkcs7_verify.c | 63 ++++++++++++++++++++++-----
 include/crypto/pkcs7.h                |  7 ++-
 security/integrity/ima/ima_modsig.c   | 10 ++++-
 3 files changed, 66 insertions(+), 14 deletions(-)

diff --git a/crypto/asymmetric_keys/pkcs7_verify.c 
b/crypto/asymmetric_keys/pkcs7_verify.c
index db3f6d080ccf..bd55f86b9464 100644
--- a/crypto/asymmetric_keys/pkcs7_verify.c
+++ b/crypto/asymmetric_keys/pkcs7_verify.c
@@ -140,17 +140,61 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
        return ret;
 }
 
-int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
-                    enum hash_algo *hash_algo)
+/**
+ * pkcs7_get_signer_count - Count the signers of a PKCS#7 message
+ * @pkcs7: The preparsed PKCS#7 message to access
+ *
+ * Return: the number of SignerInfos in the message.
+ */
+unsigned int pkcs7_get_signer_count(const struct pkcs7_message *pkcs7)
+{
+       const struct pkcs7_signed_info *sinfo;
+       unsigned int count = 0;
+
+       for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next)
+               count++;
+
+       return count;
+}
+
+/**
+ * pkcs7_get_digest - Get the content digest of one signer of a PKCS#7 message
+ * @pkcs7: The PKCS#7 message
+ * @index: Zero-based index of the signer, in the message's (DER) order
+ * @buf: The content digest (NULL when there is none)
+ * @len: The content digest length (0 when there is none)
+ * @hash_algo: The digest algorithm (HASH_ALGO__LAST when there is none)
+ * @pkey_algo: The signature algorithm name if the data was signed directly
+ *
+ * The content digest is the digest of the signed content under the signer's
+ * digest algorithm. A signer whose algorithm signs the data directly has no
+ * digest when authenticated/signed attributes is absent, we return 0 in
+ * this case.
+ *
+ * Return: 0 on success, -EINVAL if there is no signer at 'index',
+ * -EKEYREJECTED if the messageDigest attribute doesn't match the content, or
+ * another negative error code.
+ */
+int pkcs7_get_digest(struct pkcs7_message *pkcs7, unsigned int index,
+                    const u8 **buf, u32 *len, enum hash_algo *hash_algo,
+                    const char **pkey_algo)
 {
        struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;
+       unsigned int n;
        int i, ret;
 
-       /*
-        * This function doesn't support messages with more than one signature.
-        */
-       if (sinfo == NULL || sinfo->next != NULL)
-               return -EBADMSG;
+       *buf = NULL;
+       *len = 0;
+       *hash_algo = HASH_ALGO__LAST;
+       *pkey_algo = NULL;
+
+       for (n = 0; sinfo && n < index; n++)
+               sinfo = sinfo->next;
+       if (!sinfo)
+               return -EINVAL;
+
+       if (sinfo->sig->algo_takes_data)
+               *pkey_algo = sinfo->sig->pkey_algo;
 
        ret = pkcs7_digest(pkcs7, sinfo);
        if (ret)
@@ -166,9 +210,8 @@ int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 
**buf, u32 *len,
                *buf = sinfo->sig->m;
                *len = sinfo->sig->m_size;
        } else {
-               pr_notice_once("%s: No digest available\n", __func__);
-               return -EINVAL; /* TODO: MLDSA doesn't necessarily calculate an
-                                * intermediate digest. */
+               /* No intermediate digest and no hash_algo to match. */
+               return 0;
        }
 
        i = match_string(hash_algo_name, HASH_ALGO__LAST,
diff --git a/include/crypto/pkcs7.h b/include/crypto/pkcs7.h
index 38ec7f5f9041..b237fe01b2f4 100644
--- a/include/crypto/pkcs7.h
+++ b/include/crypto/pkcs7.h
@@ -41,7 +41,10 @@ extern int pkcs7_verify(struct pkcs7_message *pkcs7,
 extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
                                      const void *data, size_t datalen);
 
-extern int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf,
-                           u32 *len, enum hash_algo *hash_algo);
+extern unsigned int pkcs7_get_signer_count(const struct pkcs7_message *pkcs7);
+
+extern int pkcs7_get_digest(struct pkcs7_message *pkcs7, unsigned int index,
+                           const u8 **buf, u32 *len, enum hash_algo *hash_algo,
+                           const char **pkey_algo);
 
 #endif /* _CRYPTO_PKCS7_H */
diff --git a/security/integrity/ima/ima_modsig.c 
b/security/integrity/ima/ima_modsig.c
index 632c746fd81e..15e499f7dba1 100644
--- a/security/integrity/ima/ima_modsig.c
+++ b/security/integrity/ima/ima_modsig.c
@@ -99,6 +99,7 @@ int ima_read_modsig(enum ima_hooks func, const void *buf, 
loff_t buf_len,
  */
 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size)
 {
+       const char *pkey_algo;
        int rc;
 
        /*
@@ -111,9 +112,14 @@ void ima_collect_modsig(struct modsig *modsig, const void 
*buf, loff_t size)
        if (rc)
                return;
 
+       /* There's room for the digest of a single signer only. */
+       if (pkcs7_get_signer_count(modsig->pkcs7_msg) != 1)
+               return;
+
        /* Ask the PKCS7 code to calculate the file hash. */
-       rc = pkcs7_get_digest(modsig->pkcs7_msg, &modsig->digest,
-                             &modsig->digest_size, &modsig->hash_algo);
+       rc = pkcs7_get_digest(modsig->pkcs7_msg, 0, &modsig->digest,
+                             &modsig->digest_size, &modsig->hash_algo,
+                             &pkey_algo);
 }
 
 int ima_modsig_verify(struct key *keyring, const struct modsig *modsig)
-- 
2.55.0


Reply via email to