Search code examples
c#dsa

DSA signature c#


CertificateI have a Certificate

This is the text i have to verify:

B5080F731EE89EC82FD2E8B22E9_I_CANNOT_SHOW_THE_REAL_TEXT

This is the signed:

MIIBUwYJKoZIhvcNAQcCoIIBRDCCAUACAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHATGCAR8wggEbAgEBMG8wZDELMAkGA1UEBhMCREUxHDAaBgNVBAoTE1NBUCBUcnVzdCBDb21tdW5pdHkxEzARBgNVBAsTClNBUCBXZWIgQVMxFDASBgNVBAsTC0kwMDIwMjEyMzYwMQwwCgYDVQQDEwNFMTUCByASBQYIEQgwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTE4MDYyNzE5MzcyNVowIwYJKoZIhvcNAQkEMRYEFDgpp0877pKaChyIGVw5sPeD0W03MAkGByqGSM44BAMEMDAuAhUA4PH8bdBPHHtuPHvhJxjei%2BFrJYUCFQCnZ6IABDiRlctS9E9N3IQK60JLIg%3D%3D

Can´t find a way to do verify the signature with c#. When i use the "normal" DSACryptoServiceProvider I always get the error saying the signature size should be 40 bytes.

I just need to know were to go. wath to use I know is DSA. I know the signature is around 500bytes

this is the code i'm trying:

DSACryptoServiceProvider csp = (DSACryptoServiceProvider)CurrentCer.csp.PublicKey.Key;

SHA1Managed sha1 = new SHA1Managed();
byte[] data = Encoding.UTF8.GetBytes(ToSign);
byte[] hash = sha1.ComputeHash(data);

var base64EncodedBytes = System.Convert.FromBase64String(signature);
result = csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), base64EncodedBytes);

DSASignatureDeformatter verifier = new DSASignatureDeformatter(csp);
verifier.SetHashAlgorithm("SHA1");
bool valid = verifier.VerifySignature(hash, base64EncodedBytes);

Solution

  • Your data isn't a signature, per se. It's a query-string-encoded base64-encoded representation of a CMS Signed-Data with detached content, and it happens to have been signed with DSA.

    str = Uri.UnescapeDataString(str);
    byte[] signatureMessage = Convert.FromBase64String(str);
    ContentInfo content = new ContentInfo(yourDataHere);
    SignedCms signedCms = new SignedCms(content, detached: true);
    signedCms.Decode(signatureMessage);
    
    SignerInfoCollection signers = signedCms.SignerInfos;
    
    if (signers.Count != 1 || signers[0].Certificate != null)
    {
        // Reject it, this isn't what you're looking for.
        // At least, based on the sample you gave.
        //
        // You could, for Count == 1, accept Certificate == null or
        // Certificate.RawData.SequenceEqual(CurrentCer.RawData),
        // if you're so inclined.
    }
    
    // This throws if the signature doesn't check out.
    signedCms.CheckSignature(new X509Certificate2Collection(CurrentCer), verifySignatureOnly: true);