I have a problem when verifying detached signature of a file of zero length. I'm using BouncyCastle (bc-csharp).
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;
}
}
When I'm trying to verify signature for a file of zero-length, I'm getting 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) ...
In other cases it works well.