I've been googling around for half a day looking for a way to read a .pfx file and import the certificates into the certstore.
So far, I am able to read the .pfx file with X509Certifcate and able to import one certificate within the .pfx file. So far so good, but there are three certificates in the .pfx file and when loading the .pfx with X509Certificate, I am not able to see the other two certificates.
The certificate was exported with
*Personal Information Exchange - PKCS #12 (.PFX)
Include all certificates in the certification path if possible
Enable strong protection (requires IE 5.0, NT 4.0 SP4 or above)
Those are the options selected when exporting the certificate(s). I know there are three certificates because I manually go into the certstore (MMC) and import it into a personal folder myself.
You should be able to get a collection object containing the certs in your .pfx file by using the
X509Certificate2Collectionclass... here's some C# example code:Then you can iterate over the collection:
Depending on the type of certificate (client cert, intermediate CA cert, root CA) you'll need to open the proper cert store (as an
X509Storeobject) to import it.Check out the
X509Storedocs:http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.aspx
And the different members in the
StoreNameenumeration:http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx
From what I understand, you want to use
StoreName.Myfor client certificates that contain a private key,StoreName.CertificateAuthorityfor intermediate CA certs, andStoreName.Rootfor root CA certs.