diff mbox series

[2/3] openssl: cms: reject weak digest algorithms

Message ID 20261010123148.1394045-3-stefano.babic@swupdate.org
State Accepted
Headers show
Series CMS: reject weak digest for all backends | expand

Commit Message

Stefano Babic Oct. 10, 2026, 12:31 p.m. UTC
The digest algorithm used for the signature is chosen by the signer
and it is not checked: signatures computed with MD5 or SHA-1 are
accepted by CMS_verify(), both with and without signed attributes.

Accept only SHA-256, SHA-384, SHA-512 and, if supported by OpenSSL,
SHA-3 and SHAKE (used by SLH-DSA-SHAKE signers). The digest of each
SignerInfo is checked before verification. Updates signed with SHA-1
or MD5 are not accepted anymore.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 crypto/swupdate_cms_verify_openssl.c | 56 ++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
diff mbox series

Patch

diff --git a/crypto/swupdate_cms_verify_openssl.c b/crypto/swupdate_cms_verify_openssl.c
index 2587c09a..b959cd44 100644
--- a/crypto/swupdate_cms_verify_openssl.c
+++ b/crypto/swupdate_cms_verify_openssl.c
@@ -317,6 +317,57 @@  out_check_signer:
 	return ret;
 }
 
+/*
+ * The digest algorithm is chosen by the signer: accept only
+ * algorithms that are not considered weak (no MD5, SHA-1, SHA-224).
+ * SHAKE is used by SLH-DSA-SHAKE signers (RFC 9814).
+ */
+static bool is_digest_allowed(int nid)
+{
+	switch (nid) {
+	case NID_sha256:
+	case NID_sha384:
+	case NID_sha512:
+#ifdef NID_sha3_256
+	case NID_sha3_256:
+	case NID_sha3_384:
+	case NID_sha3_512:
+	case NID_shake128:
+	case NID_shake256:
+#endif
+		return true;
+	default:
+		return false;
+	}
+}
+
+static int check_signer_digests(CMS_ContentInfo *cms)
+{
+	STACK_OF(CMS_SignerInfo) *infos = CMS_get0_SignerInfos(cms);
+	int i;
+
+	if (!infos || sk_CMS_SignerInfo_num(infos) <= 0)
+		return -EBADMSG;
+
+	for (i = 0; i < sk_CMS_SignerInfo_num(infos); ++i) {
+		CMS_SignerInfo *si = sk_CMS_SignerInfo_value(infos, i);
+		X509_ALGOR *digest = NULL;
+		int nid;
+
+		CMS_SignerInfo_get0_algs(si, NULL, NULL, &digest, NULL);
+		if (!digest)
+			return -EBADMSG;
+		nid = OBJ_obj2nid(digest->algorithm);
+		if (!is_digest_allowed(nid)) {
+			ERROR("digest algorithm %s of signer %d not allowed",
+			      OBJ_nid2sn(nid), i);
+			return -EBADMSG;
+		}
+	}
+
+	return 0;
+}
+
 #if defined(CONFIG_CMS_SKIP_UNKNOWN_SIGNERS) || defined(CONFIG_CMS_IGNORE_ADDITIONAL_CERTS)
 static int verify_signer_certs(CMS_ContentInfo* cms, X509_STORE* store)
 {
@@ -549,6 +600,11 @@  static int openssl_cms_verify_file(void  *ctx, const char *sigfile,
 		goto out;
 	}
 
+	if (check_signer_digests(cms)) {
+		status = -EBADMSG;
+		goto out;
+	}
+
 	/* Open the content file (data which was signed) */
 	content_bio = BIO_new_file(file, "rb");
 	if (!content_bio) {