Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find certificate by serial number

I've imported my certificates to Personal -> Certificates.

I use the following lines of code to find my certificate by serial number but I can't:

    public X509Certificate2Collection FindCerts(string serialNumber)
    {
        var searchType = X509FindType.FindBySerialNumber;
        var storeName = "MY";

        var certificatesStore = new X509Store(storeName, StoreLocation.LocalMachine);
        certificatesStore.Open(OpenFlags.OpenExistingOnly);

        var matchingCertificates = certificatesStore.Certificates.Find(searchType, serialNumber, true);

        certificatesStore.Close();

        return matchingCertificates;
    }

Could you please tell me why I can't find my cert even though it is in certificatesStore.Certificates list?

Note: my certs were created by Go Daddy

like image 824
Phuc Avatar asked Feb 18 '13 04:02

Phuc


People also ask

Do certificates have serial numbers?

The serial number is a unique number issued by the certificate issuer, which is also called the Certificate Authority (CA).

How long is a certificate serial number?

Certificate serial number requirements 2.2, serial numbers MUST be unique, not greater than 20 bytes long non-negative integer and at least 1 bit must be enabled in first byte.


2 Answers

I've fixed this problem by entering the serial number instead copying from the property window. I don't know why when copying from this window, it contains a strange character on the beginning of the serial number.

like image 144
Phuc Avatar answered Sep 20 '22 16:09

Phuc


Since I came across this issue too, I tried to make a workaround to be able to copy paste the value from the certmgr.msc

A summary of what I did :

// The value below is pasted from certmgr.msc
var sslCertificateSerialNumber="‎47 9f da c4 ad d7 33 a6 4c ad 54 d3 d9 95 67 1c"; 


// Remove all non allowed characters that entered the value while copy/paste
var rgx = new Regex("[^a-fA-F0-9]");
var serial = rgx.Replace(sslCertificateSerialNumber, string.Empty).ToUpper();

Now I found the correct certificate with a copy/pasted value.

like image 25
Steven Avatar answered Sep 20 '22 16:09

Steven