Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find certificate by its thumbprint in C#

I am using this code to find the certificate by its thumbprint. certificate exists in certificate manager in personal certificate store but this code is not finding that certificate.

Please tell me where I'm doing wrong in it.

namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             string certThumbPrint = "‎‎fe14593dd66b2406c5269d742d04b6e1ab03adb1";             X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);             // Try to open the store.              certStore.Open(OpenFlags.ReadOnly);             // Find the certificate that matches the thumbprint.             X509Certificate2Collection certCollection = certStore.Certificates.Find(                 X509FindType.FindByThumbprint, certThumbPrint, false);             certStore.Close();              // Check to see if our certificate was added to the collection. If no,              // throw an error, if yes, create a certificate using it.             if (0 == certCollection.Count)             {                 Console.WriteLine("Error: No certificate found containing thumbprint " );             }             Console.ReadLine(); } 
like image 583
RATHI Avatar asked Jun 20 '12 08:06

RATHI


People also ask

How do I find a certificate name from thumbprint?

Right-click Certificates (Local Computer) in MMC > Find Certificates, and pick the hash algorithm under Look in Field, with the thumbprint in the Contains box.

How is a certificate thumbprint generated?

A certificate thumbprint is an hexadecimal string that uniquely identifies a certificate. A thumbprint is calculated from the content of the certificate using a thumbprint algorithm. CES accepts Secure Hash Algorithm 1 (SHA-1) thumbprints in the 40-digit hexadecimal string form without spaces.

Is the certificate hash the thumbprint?

A certificate thumbprint is a hash of a certificate, computed over all certificate data and its signature. Thumbprints are used as unique identifiers for certificates, in applications when making trust decisions, in configuration files, and displayed in interfaces.

Is certificate thumbprint a secret?

The certificate fingerprint is calculated from the certificate. The certificate itself is public information and transferred in clear during the SSL/TLS handshake. Which makes the fingerprint public information too, i.e. there is usually no danger in having it known by others.


2 Answers

Just stumbled over this question when Googling for the same issue, and found the answer here: if, like me, you obtained your "source" thumbprint from MMC by highlighting the thumbprint and copying it to the clipboard, you've almost certainly caught an invisible character at the start of the screen, so:

string certThumbPrint = "‎‎fe14593dd66b2406c5269d742d04b6e1ab03adb1";

is actually

string certThumbPrint = "‎‎INVISIBLECHARACTERfe14593dd66b2406c5269d742d04b6e1ab03adb1";

If you delete this invisible character (you can tell it's there when you press backspace or delete beside it and nothing seems to happen), or just retype the thumbprint by hand, your code should work fine. Now if only Visual Studio had a "show invisible characters" option ...

like image 116
KenD Avatar answered Oct 11 '22 12:10

KenD


The string literal containing your thumbprint has a left-to-right mark at the beginning. When MMC lists the certificate properties, it precedes the thumbprint value with this character so that the hex bytes are listed left to right even in locales where the text is normally rendered right to left.

Likely, this was a shortcut someone took because it was easier to prepend a character to one of the values in the property list than write a bit of code to dynamically update the edit control's style. Perhaps it was a quick fix to a bug report during localization testing.

In the MMC, the left-to-right mark has non-zero width, which you can observe by watching the cursor move when you arrow across it and my noticing that the first row of hex bytes is shifted slightly to the right compared to the second row.

In other editors such as Visual Studio, the left-to-right mark has no width, but you can still observe it by noticing that the cursor does not move when you arrow across is. As KenD answered, deleting this character solves the problem.

Quick way to identify the invisible character: Use the keyboard to select the invisible character; then paste it into Word between some normal characters. Select it in Word; then click Insert > Symbol > More Symbols. Look in the lower left under "Unicode name".

like image 27
Edward Brey Avatar answered Oct 11 '22 12:10

Edward Brey