|
1 | 1 |
|
2 | 2 | //...................................................................................
|
3 |
| -#region About X509CertificateCache |
| 3 | +#region Readme: X509CertificateCache |
4 | 4 | //...................................................................................
|
5 |
| -// Per-Thread-Cache of X590 Certificates identified by Thumbprint. |
6 |
| -// Certificates are cached based on StoreName and StoreLocation to avoid ambuguity. |
7 |
| -// Supports locating certs ONLY by thumbprint. |
8 |
| -// Certs identified by thumbprint can't change for life-time. |
9 |
| -// If certificate is NOT found, cache is NOT updated with NULL. |
10 |
| -// As such, you should never encounter a situation where you need to re-start the server after adding/updating certificate. |
11 |
| -// DELETING a certificate might require a restart if the certificate is already cached. |
12 |
| -// IMP: |
| 5 | +// |
| 6 | +// It takes approx 5 miiliSec to lookup and obtain the certificate from local certificate store, |
| 7 | +// unless the Store itself is handled as singleton and never closed during process lifetime. |
| 8 | +// |
| 9 | +// X509CertificateCache can be used to cache and re-use the certs. |
| 10 | +// This is NOT a secrity issue, as the process has access to the certificate in the store. |
| 11 | +// If given process do not have access to the certificate, it won't reach the cache in the first place. |
| 12 | +// |
| 13 | +// Using the X509 certificate instance: |
13 | 14 | // Use the cache ONLY IF you absolutely know how you are using the X509Certificate2 instance.
|
14 |
| -// Disposing the certificate, for example, will leave a stale and useless X509Certificate2 instance in the cache. |
15 |
| -// Given caller can use a secret cache-key-prefix, in which case, his/her version of cached instances is NOT shared with otthers. |
| 15 | +// Disposing the certificate, or disposing the AsymmetricAlgorithm for example, |
| 16 | +// will leave a STALE and USELESS X509Certificate2 instance in the cache. |
| 17 | +// Your code may not have control over other parts using/abusing the cache. |
| 18 | +// If your use-case needs a private space that is not available to other callers, |
| 19 | +// use a unique-cache-prfix, that is not shared with others. |
| 20 | +// Example: private static readonly string MyCachePrefix = Guid.NewGuid().ToString(); |
| 21 | +// |
| 22 | +// Is this thread-safe? |
| 23 | +// The X509CertificateCache maintains per-thread-cache. |
| 24 | +// Each thread has its own instance of the cache and the cached versions of the X509 certificate. |
| 25 | +// The cache is as thread-safe as the X509Certificate instance itself. |
| 26 | +// The cache can't prevent you from passing the certificate instances in an async call, crossing thread boundary. |
| 27 | +// If you are concerned about thread safety, do not pass the certifcates across async-call boundaries. |
| 28 | +// For threadsafety of X509Certificate2 related operations, refer Microsoft documentation. |
16 | 29 | //
|
| 30 | +// How about server-restart on certificate changes? |
| 31 | +// The ONLY option supported by the cache is lookup by thumbprint. |
| 32 | +// The cache doesn't support lookup by other properties that may change, such as SubjectName. |
| 33 | +// The thumbprint is a digital fingerprint for specific certificate instance. |
| 34 | +// Also, the certificates are cached based on StoreName and StoreLocation to avoid ambuguity. |
| 35 | +// Certs identified by thumbprint can't change for life-time, without change to the thumbprint itself. |
| 36 | +// In general, you should not have to to re-start the server after adding/updating certificate. |
| 37 | +// DELETING a certificate might require a restart if the certificate is already cached. |
| 38 | +// Stop using the old thumbprint to avoid restart. |
| 39 | +// Another case is, if the permission to a certificate was revoked to the process, after it was already cached. |
| 40 | +// |
| 41 | +// As with any other library, read-and-understand the code before using. |
| 42 | +// |
17 | 43 | #endregion
|
18 | 44 |
|
19 | 45 | using System;
|
@@ -99,6 +125,5 @@ public static X509Certificate2 TryGetCertificate(string x509Thumbprint, StoreNam
|
99 | 125 | }
|
100 | 126 | }
|
101 | 127 | }
|
102 |
| - |
103 | 128 | }
|
104 | 129 | }
|
0 commit comments