Cannot find cetificates through certificatestore class

+1 vote
asked Jun 20, 2013 by Gade Chua (200 points)
edited Jun 24, 2013

Hi - I retied to use CertificateStore.FindCertificates to get existing certificate for email signing. However, it never returns any certificate back from My store no matter what find options I used. My code looks like this:

CertificateStore certStore = new CertificateStore(CertificateStoreName.My); //search from personal cert store
CertificateFindOptions findOptions = 
CertificateFindOptions.ServerAuthentication|
CertificateFindOptions.ClientAuthentication|
CertificateFindOptions.HasPrivateKey | 
CertificateFindOptions.IsTimeValid; //I started with one by one, and even I combine them all nothing works.
Certificate[] certs = certStore.FindCertificates(findOptions);

My questions are: 1) is there a way to list out all the certificates in the store? that way I can filter by CN name 2) Is there a way to convert certificate obtained from X509 store into Rebex Certificate class?

Quick response is very appreciated. thanks! Gade

1 Answer

+1 vote
answered Jun 21, 2013 by Tomas Knopp (58,890 points)
edited Jun 24, 2013

Hello,

1) to list all certificates (i.e. to specify no CertificateFindOptions) use CertificateFindOptions options = 0; as in the example:

var certStore = new CertificateStore(CertificateStoreName.My);
Certificate[] certs = certStore.FindCertificates(0);

2) converting between Rebex.Security.Certificates.Certificate class and X509Certificate (or X509Certificate2) classes is done automatically, so you can simply assign as in the example below:

X509Certificate x509Certificate;
Rebex.Security.Certificates.Certificate rebexCertificate;

...
rebexCertificate = x509Certificate; //convert X509Certificate into Certificate
x509Certificate = rebexCertificate; // convert vice versa
commented Jun 21, 2013 by Gade Chua (200 points)
edited Jun 21, 2013

Thank you Thomas! The option 0 did list out all the certificates. However, the conversion of rebex cert into X509 cert wasn't successful. I tried both implicit and explicit conversion. Can you give me a working example if you have done it before?

commented Jun 24, 2013 by Tomas Knopp (58,890 points)
edited Jun 24, 2013

The implicit conversion between Certificate and X509Certificate(2) class was added in release 2012R2. If you use earlier version you can convert Rebex into X509 Certificate like this:

Certificate rebexCert = ...;
var dotnetCert = new X509Certificate2(rebexCert.Handle);
...