Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain CN of the certificates in particular store?

i want to obtain the CN of the certificates stored in the MY store as i want to verify if the certificate exists or not in that store.

I don't know the which method should be used to perform this task.

I tried using below code but it doesn't works

X509Certificate2Collection cers =  store.Certificates.Find(X509FindType.FindBySubjectName,"Root_Certificate",false);

if(cers.Count>0)
{

//certificate present

}

else
{

//certificate not present

}

Does the subjectName gives CN?

is there any other method?

Please suggest me how to check whether a particular certificate is present or not and i want to do it using CN.

like image 823
purvang Avatar asked Oct 03 '11 10:10

purvang


1 Answers

You could use the store.Certificates.Find(X509FindType.FindBySubjectName, "SubjectName", false) function to search for a certificate by its subject name. Do NOT include "CN=" in the subject name.

To search more specific you could use the thumbprint to search for your certificate. The following code sample demonstrates this:

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.IncludeArchived);

foreach (var c in store.Certificates)
{
  Console.Out.WriteLine(c.Thumbprint);
  Console.Out.WriteLine(c.Subject);
}

// Find by thumbprint
X509Certificate2Collection col =
store.Certificates.Find(X509FindType.FindByThumbprint, "669502F7273C447A62550D41CD856665FBF23E48", false);

store.Close();

I've added a foreach loop to the code sample to iterate over all certificates in the selected store. Your certificate must be listed there. If not, you probably use the wrong store. Note, there is a My store for the Machine and the Current User. So, be sure to open the right store.

To get the thumbprint of your certificate follow these steps:

  1. Open certmgr.msc.
  2. Double click on your certificate.
  3. Go to the details tab.
  4. Under thumbprint you will find the thumbprint of your certificate.

Hope, this helps.

like image 174
Hans Avatar answered Sep 29 '22 07:09

Hans