Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Pkcs#7 certificate chain from file/stream in C#?

I have two certificates that I saved to disk. One is a certificate with private key that I exported as a .pfx file, the other one is a certificate that I saved including its certificate chain as a PKCS#7 file ("certchain.p7b").

In C# I can now load the .pfx file with

  var cert = new X509Certificate2(myPfxFileStream); 

(myPfxFileStream is a FileStream opened to the .pfx File for reading), however trying the same thing with the PKCs#7 Certificate fails in a CryptoGraphicException "Der Indexwert ist ungültig" which translates to "invalid index value".

I assume I have to parse PKCS#7 differently (it contains a chain, not a single certificate!), but how?

(Oh, by the way: Currently I have no passwords on those certficiates)

like image 869
froh42 Avatar asked Feb 23 '09 19:02

froh42


People also ask

How do I view a certificate file?

To view certificates for the current userSelect Run from the Start menu, and then enter certmgr. msc. The Certificate Manager tool for the current user appears. To view your certificates, under Certificates - Current User in the left pane, expand the directory for the type of certificate you want to view.

What is a PKCS certificate?

In cryptography, PKCS #12 defines an archive file format for storing many cryptography objects as a single file. It is commonly used to bundle a private key with its X. 509 certificate or to bundle all the members of a chain of trust.


1 Answers

You will want to use the SignedCms class in the System.Security.Cryptography.Pkcs namespace.

This blog entry will show you how to use the class:

link update 2021: https://docs.microsoft.com/en-us/archive/blogs/shawnfa/enveloped-pkcs-7-signatures

original link: http://blogs.msdn.com/shawnfa/archive/2006/02/27/539990.aspx

You basically will call the Decode method, passing the bytes representing the PKCS file.

like image 146
casperOne Avatar answered Sep 19 '22 00:09

casperOne