Skip to content

Commit 38ac76f

Browse files
committed
Cosmetics.
1 parent bb43a69 commit 38ac76f

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

src/Org.Security.Cryptography.X509Extensions/X509CertificateCache.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11

22
//...................................................................................
3-
#region About X509CertificateCache
3+
#region Readme: X509CertificateCache
44
//...................................................................................
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:
1314
// 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.
1629
//
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+
//
1743
#endregion
1844

1945
using System;
@@ -99,6 +125,5 @@ public static X509Certificate2 TryGetCertificate(string x509Thumbprint, StoreNam
99125
}
100126
}
101127
}
102-
103128
}
104129
}

src/Org.Security.Cryptography.X509Extensions/X509RsaAesStreamEncryptionExtensions.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static void Encrypt(AsymmetricAlgorithm keyEncryption, SymmetricAlgorithm dataEn
128128
using (var transform = dataEncryption.CreateEncryptor())
129129
using (var cryptoStream = new CryptoStream(outputStream, transform, CryptoStreamMode.Write))
130130
{
131-
inputStream.CopyTo(cryptoStream, dataEncryption.BlockSize);
131+
inputStream.CopyTo(cryptoStream, bufferSize: dataEncryption.BlockSize * 4);
132132
}
133133
}
134134

@@ -149,7 +149,7 @@ static void Decrypt(AsymmetricAlgorithm keyEncryption, SymmetricAlgorithm dataEn
149149
using (var transform = dataEncryption.CreateDecryptor())
150150
using (var cryptoStream = new CryptoStream(inputStream, transform, CryptoStreamMode.Read))
151151
{
152-
cryptoStream.CopyTo(outputStream, dataEncryption.BlockSize);
152+
cryptoStream.CopyTo(outputStream, bufferSize: dataEncryption.BlockSize * 4);
153153
}
154154
}
155155

@@ -181,22 +181,5 @@ static byte[] ReadLengthAndBytes(this Stream inputStream)
181181

182182
return bytes;
183183
}
184-
185-
static void CopyTo(this Stream inputStream, Stream outputStream, int bufferSize)
186-
{
187-
if (null == inputStream) throw new ArgumentNullException(nameof(inputStream));
188-
if (null == outputStream) throw new ArgumentNullException(nameof(outputStream));
189-
if (bufferSize <= 0) throw new ArgumentException("Invalid buffer size. Must be > 0");
190-
191-
byte[] buffer = new byte[bufferSize];
192-
int bytesRead = 0;
193-
194-
do {
195-
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
196-
if (bytesRead > 0) outputStream.Write(buffer, 0, bytesRead);
197-
}
198-
while (bytesRead > 0);
199-
}
200-
201184
}
202185
}

0 commit comments

Comments
 (0)