From: Bruno Meneguele <bmeneg@redhat.com>
To: linux-integrity@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, keyrings@vger.kernel.org,
zohar@linux.ibm.com, roberto.sassu@huawei.com,
dhowells@redhat.com, bauerman@linux.ibm.com,
Bruno Meneguele <bmeneg@redhat.com>
Subject: [RFC PATCH 3/8] pkcs7: support multiple signers in pkcs7_get_digest()
Date: Thu, 26 Nov 2026 23:59:06 -0300 [thread overview]
Message-ID: <20261127025912.1383750-4-bmeneg@redhat.com> (raw)
In-Reply-To: <20261127025912.1383750-1-bmeneg@redhat.com>
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 <bmeneg@redhat.com>
---
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
next prev parent reply other threads:[~2026-11-27 2:59 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 1/8] pkcs7: don't cache a failed digest in pkcs7_digest() Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 2/8] pkcs7: return the content digest from pkcs7_get_digest() Bruno Meneguele
2026-11-27 2:59 ` Bruno Meneguele [this message]
2026-11-27 2:59 ` [RFC PATCH 4/8] ima: get the modsig content digest of each signer Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 5/8] ima: record data-signing algorithms by name in 'd-modsig' Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 6/8] ima: add the 'd-modsig-list' template field Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 7/8] ima: define the ima-modsigv2 template Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 8/8] ima: check the digest of every modsig signer against the blacklist Bruno Meneguele
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127025912.1383750-4-bmeneg@redhat.com \
--to=bmeneg@redhat.com \
--cc=bauerman@linux.ibm.com \
--cc=dhowells@redhat.com \
--cc=keyrings@vger.kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=roberto.sassu@huawei.com \
--cc=zohar@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®