-
Notifications
You must be signed in to change notification settings - Fork 1
/
CertificateLoader.cs
63 lines (55 loc) · 2.47 KB
/
CertificateLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#region
using System;
using System.Security.Cryptography.X509Certificates;
#endregion
namespace Kombit.Samples.Common
{
/// <summary>
/// Helper methods to load certificates from store
/// </summary>
public static class CertificateLoader
{
/// <summary>
/// Loads certificate from LocalMachine/My with thumbprint
/// </summary>
/// <param name="thumbprint">Thumbprint of a certificate</param>
/// <returns>The first found certificate</returns>
public static X509Certificate2 LoadCertificateFromMyStore(string thumbprint)
{
return LoadCertificateFromStore(StoreName.My, StoreLocation.LocalMachine, thumbprint);
}
/// <summary>
/// Loads certificate from LocalMachine/TrustedPeople with thumbprint
/// </summary>
/// <param name="thumbprint">Thumbprint of a certificate</param>
/// <returns>The first found certificate</returns>
public static X509Certificate2 LoadCertificateFromTrustedPeopleStore(string thumbprint)
{
return LoadCertificateFromStore(StoreName.TrustedPeople, StoreLocation.LocalMachine, thumbprint);
}
/// <summary>
/// Loads certificate from a store with thumbprint
/// </summary>
/// <param name="storeLocation">A store location</param>
/// <param name="thumbprint">Thumbprint of a certificate</param>
/// <param name="storeName">Store name</param>
/// <returns>The first found certificate</returns>
private static X509Certificate2 LoadCertificateFromStore(StoreName storeName, StoreLocation storeLocation,
string thumbprint)
{
if (string.IsNullOrEmpty(thumbprint))
throw new ArgumentNullException("thumbprint");
var store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
var result = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
Logging.Instance.Debug(
"Loading certificates whose thumbprint is {Thumbprint} from store {Store} and location {Location}. Found {Count} certificates.",
thumbprint, storeName, storeLocation, result.Count);
if (result.Count == 0)
{
throw new ArgumentException("No certificate whose thumbprint is " + thumbprint + " is not found.");
}
return result[0];
}
}
}