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).

I've tried reading it as an X509 certificate like so:

//certContent is a byte array with the p7b file contents
X509Certificate2 cert = new X509Certificate2(certContent);

That works fine with a regular .cer certificate, but throws a CryptographicException when used with a .p7b certificate. This is because the .p7b contains the entire certificate chain.

I've also tried parsing it as a SignedCms object, then iterating through the certificate chain and pulling out my SSL certificate:

However that throws an exception on Decode saying ASN1 bad tag value met. 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.

Does anyone know how I can parse this .p7b certificate chain using C#?

This is exatly what I am having problem with. Could you please share the part of the code which retrieves bytes from database and then convert it into a X509Certificate? Thanks
–
Preyash DesaiSep 5 '11 at 14:17