Skip to content

Commit 6676986

Browse files
committed
x509: add utils to find certs by thumbprint
1 parent adfa71a commit 6676986

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/shared/Core/X509Utils.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Security.Cryptography.X509Certificates;
2+
3+
namespace GitCredentialManager;
4+
5+
public static class X509Utils
6+
{
7+
public static X509Certificate2 GetCertificateByThumbprint(string thumbprint)
8+
{
9+
foreach (var location in new[]{StoreLocation.CurrentUser, StoreLocation.LocalMachine})
10+
{
11+
using var store = new X509Store(StoreName.My, location);
12+
store.Open(OpenFlags.ReadOnly);
13+
14+
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
15+
if (certs.Count > 0)
16+
{
17+
return certs[0];
18+
}
19+
}
20+
21+
return null;
22+
}
23+
}

0 commit comments

Comments
 (0)