diff mbox series

[3/3] wolfssl: pkcs7: reject weak digest algorithms

Message ID 20261010123148.1394045-4-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 PKCS7_verify(), both with and without signed attributes.

Accept only SHA-256, SHA-384, SHA-512, SHA-3 and SHAKE, as for the
OpenSSL backend. The digest of the signer is set by PKCS7_verify() and
checked after verification. Updates signed with SHA-1 or MD5 are not
accepted anymore.

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

Patch

diff --git a/crypto/swupdate_pkcs7_verify_wolfssl.c b/crypto/swupdate_pkcs7_verify_wolfssl.c
index 0f9c0154..5e25e53e 100644
--- a/crypto/swupdate_pkcs7_verify_wolfssl.c
+++ b/crypto/swupdate_pkcs7_verify_wolfssl.c
@@ -20,6 +20,7 @@ 
 #include "util.h"
 #include "swupdate_crypto.h"
 #include <wolfssl/openssl/pkcs7.h>
+#include <wolfssl/wolfcrypt/asn.h>
 
 #ifndef PKCS7_BINARY
 #define PKCS7_BINARY            0x80
@@ -216,6 +217,27 @@  dgst_init_error:
 	return ret;
 }
 
+/*
+ * The digest algorithm is chosen by the signer: accept only
+ * algorithms that are not considered weak (no MD5, SHA-1, SHA-224).
+ */
+static bool is_digest_allowed(int hash_oid)
+{
+	switch (hash_oid) {
+	case SHA256h:
+	case SHA384h:
+	case SHA512h:
+	case SHA3_256h:
+	case SHA3_384h:
+	case SHA3_512h:
+	case SHAKE128h:
+	case SHAKE256h:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static int wolfssl_pkcs7_verify_file(void *ctx, const char *sigfile,
 		const char *file, const char *signer_name)
 {
@@ -281,6 +303,13 @@  static int wolfssl_pkcs7_verify_file(void *ctx, const char *sigfile,
 		goto out;
 	}
 
+	/* the digest algorithm of the signer is set by PKCS7_verify() */
+	if (!is_digest_allowed(pkcs7->pkcs7.hashOID)) {
+		ERROR("digest algorithm (OID sum %d) not allowed", pkcs7->pkcs7.hashOID);
+		status = -EBADMSG;
+		goto out;
+	}
+
 	if (check_signer_name(pkcs7, signer_name)) {
 		ERROR("failed to verify signer name");
 		status = -EFAULT;