bc-csharp icon indicating copy to clipboard operation
bc-csharp copied to clipboard

Verify detached signature of empty file does not work

Open Alexey2040 opened this issue 4 years ago • 0 comments

I have a problem when verifying detached signature of empty file.

private static bool VerifyDetachedSignature(byte[] fileRawBytes, string sign)
    {
        try
        {
            var signatureFileRawBytes = Convert.FromBase64String(sign);
            var cms = new CmsSignedData(new CmsProcessableByteArray(fileRawBytes), signatureFileRawBytes);
            var signers = cms.GetSignerInfos();

            var certificates = cms.GetCertificates("Collection");
            var signerInfos = signers.GetSigners();
            foreach (SignerInformation signer in signerInfos)
            {
                var certList = new ArrayList(certificates.GetMatches(signer.SignerID));
                var cert = (X509Certificate)certList[0];
                if (cert == null) throw new NullReferenceException();

                var publicKey = cert.GetPublicKey();

                signer.Verify(publicKey);
            }

            return true;
        }
        catch (Exception exception)
        {
            return false;
        }
    }

On signer.Verify(publicKey); it throws an exception:

Message:'message-digest attribute value does not match calculated value' StackTrace: at Org.BouncyCastle.Cms.SignerInformation.DoVerify(AsymmetricKeyParameter key) at Org.BouncyCastle.Cms.SignerInformation.Verify(AsymmetricKeyParameter pubKey) at myProject.Controllers.Controller.VerifyDetachedSignature(Byte[] fileRawBytes, String sign) ...

I assume problem is that empty byte array passed to new CmsProcessableByteArray(fileRawBytes), so there no copy of signed data passed to CmsSignedData. Is there work around?

Alexey2040 avatar Nov 26 '21 19:11 Alexey2040