title: Parse PKCS #7 SSL Certificate Chain (.p7b) without private key?
body: <p>I have a PKCS #7, signed, .p7b file which contains an X509 SSL certificate and the intermediate and root CA certs it was signed with. I need to use C# to parse the .p7b file, pull out the SSL certificate, and pull some values off of it (expiry date, DN, etc).</p>
  
  <p>I've tried reading it as an X509 certificate like so:</p>
  
  <pre><code>//certContent is a byte array with the p7b file contents
  X509Certificate2 cert = new X509Certificate2(certContent);
  </code></pre>
  
  <p>That works fine with a regular .cer certificate, but throws a <code>CryptographicException</code> when used with a .p7b certificate. This is because the .p7b contains the entire certificate chain.</p>
  
  <p>I've also tried parsing it as a <code>SignedCms</code> object, then iterating through the certificate chain and pulling out my SSL certificate:</p>
  
  <pre><code>SignedCms certContainer = new SignedCms();
  certContainer.Decode(certContent);
  foreach(X509Certificate2 cert in certConatiner.Certificates)
  {
       ...
  }
  </code></pre>
  
  <p>However that throws an exception on <code>Decode</code> saying <code>ASN1 bad tag value met</code>. After some searching, I believe that is because I do not have the private key which was used to create the certificate and/or sign the certificate.</p>
  
  <p>Does anyone know how I can parse this .p7b certificate chain using C#? </p>