title: Verify a DSA signature in C# that's using BER/DER encoded ASN.1 format
body: <p><strong>How can I verify a DSA signature in C#?</strong></p>
  
  <p>Given:</p>
  
  <ul>
  <li>the message text,</li>
  <li>a signed digest (typically ASN.1 DER format), </li>
  <li>the public key (in a signed X.509 certificate, PEM or DER format)</li>
  </ul>
  
  <p>I've tried a number of approaches, but haven't had any success:</p>
  
  <ul>
  <li><p><strong><a href="http://openssl-net.sourceforge.net/" rel="nofollow noreferrer">OpenSSL.NET</a>:</strong> various strange errors with the library; I've got an <a href="https://sourceforge.net/projects/openssl-net/forums/forum/552647/topic/3393454/index/page/1" rel="nofollow noreferrer">open thread running with the author over on SourceForge</a> but haven't been able to resolve this yet.</p></li>
  <li><p><strong>Microsoft .NET API:</strong> can't unpack the DER signature for comparison. A DSA signature is 40 bytes (two 20-byte integers), but is presented as an DER-encoded sequence of two integers, so the total length can range from 46 to 48 bytes (see <a href="http://forums.sun.com/thread.jspa?threadID=144622" rel="nofollow noreferrer">this post</a> for a quick overview.) While .NET includes code to parse ASN.1/DER (because it can read certificates in DER format), it's buried deep, and there's no way to access it so that you can correctly retrieve the 40 bytes from the ASN.1/DER encoded sig. This issue led me to the next option...</p></li>
  <li><p><strong>BouncyCastle:</strong> through the use of the <code>Org.BouncyCastle.Asn1</code> functions, I can parse the ASN.1 signature and pull it into it's component R and S integer values. But when I pass these to the signature verification routines, it's failing with no explanation given. I'm not sure if I'm doing anything wrong, because the C# API is <em>completely undocumented</em>, and the Java version is barely documented (but there's no example or HOWTO information that I can find.)</p></li>
  </ul>
  
  <p>I've been throwing myself at this problem for about a week now. I know someone must have done it before, but I haven't found any complete/working examples.</p>
  
  <p>I've got three C# projects sitting here, each 95% complete but with one critical flaw that causes it to fail. Any working example code would be tremendously appreciated.</p>
  
  <p>edit: here's an example of a signature I'm trying to verify, converted to Base64 and ASCII hex to make it postable. This particular one is 47 bytes but a proper parser must still accept it, read up on the DER spec for more info (BER/DER adds a leading 00 to confirm the sign if the MSB is 1)</p>
  
  <pre><code>Base64: MC0CFQCUgq2DEHWzp3O4nOdo38mDu8mStwIUadZmbin7BvXxpeoJh7dhquD2CTM=
  
  ASCII Hex: 302d0215009482ad831075b3a773b89ce768dfc983bbc992b7021469d6666e29fb06f5f1a5ea0987b761aae0f60933
  </code></pre>
  
  <p>Structure is per the DER spec; it parses as follows:</p>
  
  <pre><code>30 2d: sequence, length 45 (may vary from 44 to 46)
   02 15: integer, length 21 (first byte is 00 to confirm sign)
    009482ad831075b3a773b89ce768dfc983bbc992b7
   02 14: integer, length 20 (leading 00 not necessary for this one)
    69d6666e29fb06f5f1a5ea0987b761aae0f60933
  </code></pre>
  
  <p>Writing my own DER parser is really not an option, too much room for error and there's got to be a way to do this properly.</p>