How to import a DSA signature in ASN.1 format using BouncyCastle (C #)

OpenSSL, as well as most other DSA implementations, displays signatures in ASN.1 format. Thus, a 40-byte signature (two 20-byte integers) becomes 46 bytes due to the headers of the ASN.1 structure. (For more details see this forum post )

My question is: how to handle this format in C #? (or elsewhere, for that matter)

I spent some time trying to deal with it using .NET packages System.Security.Crypto, but refused it (it really disappoints because it clearly has internal code to parse ASN.1, since it can read the DER format, but there is no way to use it - but I'm distracted ...)

Then I started working with the BouncyCastle C # library. I can get it in Asn1Object, and if I expand it during debugging, I see that it contains DerSequencewith two integers, but how to get it out (preferably in BigIntegersso that I can feed them DSA.VerifySignature?)

Code example:

Byte[] msgText = ReadFile("test_msg.txt");
Byte[] msgSigRaw = ReadFile("test_sig_1.bin");  // reads binary ASN.1 sig using FileStream
Asn1Object sigASN = Asn1Object.FromByteArray(msgSigRaw);  // parses into Asn1Object
...
X509Certificate implCert = ReadCertificate("pubcert_dsa.cer");  // cert in DER format
DsaSigner DSA = new DsaSigner();
DSA.Init(false, implCert.GetPublicKey());
...
BigInteger sigIntR, sigIntS;
... //TODO: how to get signature from sigASN into sigIntR, sigIntS?
Boolean validSig = DSA.VerifySignature(msgText, sigIntR, sigIntS);  // my goal
+5
source share
2 answers

Take a look at this CodeProject article: http://www.codeproject.com/KB/security/CryptoInteropSign.aspx

It contains code to convert a DSA signature to the P1363 format expected in C #.

+4
source

Some examples of DSA signature verification code in BouncyCastle C #:

ISigner sig = SignerUtilities.GetSigner("SHA1withDSA");
sig.Init(false, implCert.GetPublicKey());
sig.BlockUpdate(msgText, 0, msgText.Length);
bool valid = sig.VerifySignature(msgSigRaw);

, ASN.1 ( , SHA-1 ) .

, {r, s} / ASN.1, DsaDigestSigner. / ASN.1, DsaSigner sig.

+2

All Articles