-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
331 changed files
with
15,042 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/Kurrent.Client/Core/Certificates/X509Certificates.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
#if NET48 | ||
using Org.BouncyCastle.Crypto; | ||
using Org.BouncyCastle.Crypto.Parameters; | ||
using Org.BouncyCastle.OpenSsl; | ||
using Org.BouncyCastle.Security; | ||
#endif | ||
|
||
namespace EventStore.Client; | ||
|
||
static class X509Certificates { | ||
// TODO SS: Use .NET 8 X509Certificate2.CreateFromPemFile(certPemFilePath, keyPemFilePath) once the Windows32Exception issue is resolved | ||
public static X509Certificate2 CreateFromPemFile(string certPemFilePath, string keyPemFilePath) { | ||
try { | ||
#if NET9_0_OR_GREATER | ||
using var publicCert = X509CertificateLoader.LoadCertificateFromFile(certPemFilePath); | ||
#else | ||
using var publicCert = new X509Certificate2(certPemFilePath); | ||
#endif | ||
using var privateKey = RSA.Create().ImportPrivateKeyFromFile(keyPemFilePath); | ||
using var certificate = publicCert.CopyWithPrivateKey(privateKey); | ||
|
||
#if NET48 | ||
return new(certificate.Export(X509ContentType.Pfx)); | ||
#else | ||
return X509Certificate2.CreateFromPemFile(certPemFilePath, keyPemFilePath); | ||
#endif | ||
} catch (Exception ex) { | ||
throw new CryptographicException($"Failed to load private key: {ex.Message}"); | ||
} | ||
|
||
// Notes: | ||
// using X509Certificate2.CreateFromPemFile(certPemFilePath, keyPemFilePath) would be the ideal choice here, | ||
// but it's currently causing a Win32Exception specifically on Windows. Alternative implementation is used until the issue is resolved. | ||
// | ||
// Error: The SSL connection could not be established, see inner exception. AuthenticationException: Authentication failed because the platform | ||
// does not support ephemeral keys. Win32Exception: No credentials are available in the security package | ||
// | ||
// public static X509Certificate2 CreateFromPemFile(string certPemFilePath, string keyPemFilePath) => | ||
// X509Certificate2.CreateFromPemFile(certPemFilePath, keyPemFilePath); | ||
} | ||
} | ||
|
||
public static class RsaExtensions { | ||
#if NET48 | ||
public static RSA ImportPrivateKeyFromFile(this RSA rsa, string privateKeyPath) { | ||
var (content, label) = LoadPemKeyFile(privateKeyPath); | ||
|
||
using var reader = new PemReader(new StringReader(string.Join(Environment.NewLine, content))); | ||
|
||
var keyParameters = reader.ReadObject() switch { | ||
RsaPrivateCrtKeyParameters parameters => parameters, | ||
AsymmetricCipherKeyPair keyPair => keyPair.Private as RsaPrivateCrtKeyParameters, | ||
_ => throw new NotSupportedException($"Invalid private key format: {label}") | ||
}; | ||
|
||
rsa.ImportParameters(DotNetUtilities.ToRSAParameters(keyParameters)); | ||
|
||
return rsa; | ||
} | ||
#else | ||
public static RSA ImportPrivateKeyFromFile(this RSA rsa, string privateKeyPath) { | ||
var (content, label) = LoadPemKeyFile(privateKeyPath); | ||
|
||
var privateKey = string.Join(string.Empty, content[1..^1]); | ||
var privateKeyBytes = Convert.FromBase64String(privateKey); | ||
|
||
if (label == RsaPemLabels.Pkcs8PrivateKey) | ||
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _); | ||
else if (label == RsaPemLabels.RSAPrivateKey) | ||
rsa.ImportRSAPrivateKey(privateKeyBytes, out _); | ||
|
||
return rsa; | ||
} | ||
#endif | ||
|
||
static (string[] Content, string Label) LoadPemKeyFile(string privateKeyPath) { | ||
var content = File.ReadAllLines(privateKeyPath); | ||
var label = RsaPemLabels.ParseKeyLabel(content[0]); | ||
|
||
if (RsaPemLabels.IsEncryptedPrivateKey(label)) | ||
throw new NotSupportedException("Encrypted private keys are not supported"); | ||
|
||
return (content, label); | ||
} | ||
} | ||
|
||
static class RsaPemLabels { | ||
public const string RSAPrivateKey = "RSA PRIVATE KEY"; | ||
public const string Pkcs8PrivateKey = "PRIVATE KEY"; | ||
public const string EncryptedPkcs8PrivateKey = "ENCRYPTED PRIVATE KEY"; | ||
|
||
public static readonly string[] PrivateKeyLabels = [RSAPrivateKey, Pkcs8PrivateKey, EncryptedPkcs8PrivateKey]; | ||
|
||
public static bool IsPrivateKey(string label) => Array.IndexOf(PrivateKeyLabels, label) != -1; | ||
|
||
public static bool IsEncryptedPrivateKey(string label) => label == EncryptedPkcs8PrivateKey; | ||
|
||
const string LabelPrefix = "-----BEGIN "; | ||
const string LabelSuffix = "-----"; | ||
|
||
public static string ParseKeyLabel(string pemFileHeader) { | ||
var label = pemFileHeader.Replace(LabelPrefix, string.Empty).Replace(LabelSuffix, string.Empty); | ||
|
||
if (!IsPrivateKey(label)) | ||
throw new CryptographicException($"Unknown private key label: {label}"); | ||
|
||
return label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Grpc.Core; | ||
|
||
namespace EventStore.Client; | ||
|
||
static class ChannelBaseExtensions { | ||
public static async ValueTask DisposeAsync(this ChannelBase channel) { | ||
await channel.ShutdownAsync().ConfigureAwait(false); | ||
(channel as IDisposable)?.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
using System.Net; | ||
using TChannel = Grpc.Net.Client.GrpcChannel; | ||
|
||
namespace EventStore.Client { | ||
// Maintains Channels keyed by DnsEndPoint so the channels can be reused. | ||
// Deals with the disposal difference between grpc.net and grpc.core | ||
// Thread safe. | ||
internal class ChannelCache : | ||
IAsyncDisposable { | ||
|
||
private readonly KurrentClientSettings _settings; | ||
private readonly Random _random; | ||
private readonly Dictionary<DnsEndPoint, TChannel> _channels; | ||
private readonly object _lock = new(); | ||
private bool _disposed; | ||
|
||
public ChannelCache(KurrentClientSettings settings) { | ||
_settings = settings; | ||
_random = new Random(0); | ||
_channels = new Dictionary<DnsEndPoint, TChannel>( | ||
DnsEndPointEqualityComparer.Instance); | ||
} | ||
|
||
public TChannel GetChannelInfo(DnsEndPoint endPoint) { | ||
lock (_lock) { | ||
ThrowIfDisposed(); | ||
|
||
if (!_channels.TryGetValue(endPoint, out var channel)) { | ||
channel = ChannelFactory.CreateChannel( | ||
settings: _settings, | ||
endPoint: endPoint); | ||
_channels[endPoint] = channel; | ||
} | ||
|
||
return channel; | ||
} | ||
} | ||
|
||
public KeyValuePair<DnsEndPoint, TChannel>[] GetRandomOrderSnapshot() { | ||
lock (_lock) { | ||
ThrowIfDisposed(); | ||
|
||
return _channels | ||
.OrderBy(_ => _random.Next()) | ||
.ToArray(); | ||
} | ||
} | ||
|
||
// Update the cache to contain channels for exactly these endpoints | ||
public void UpdateCache(IEnumerable<DnsEndPoint> endPoints) { | ||
lock (_lock) { | ||
ThrowIfDisposed(); | ||
|
||
// remove | ||
var endPointsToDiscard = _channels.Keys | ||
.Except(endPoints, DnsEndPointEqualityComparer.Instance) | ||
.ToArray(); | ||
|
||
var channelsToDispose = new List<TChannel>(endPointsToDiscard.Length); | ||
|
||
foreach (var endPoint in endPointsToDiscard) { | ||
if (!_channels.TryGetValue(endPoint, out var channel)) | ||
continue; | ||
|
||
_channels.Remove(endPoint); | ||
channelsToDispose.Add(channel); | ||
} | ||
|
||
_ = DisposeChannelsAsync(channelsToDispose); | ||
|
||
// add | ||
foreach (var endPoint in endPoints) { | ||
GetChannelInfo(endPoint); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() { | ||
lock (_lock) { | ||
if (_disposed) | ||
return; | ||
|
||
_disposed = true; | ||
|
||
foreach (var channel in _channels.Values) { | ||
channel.Dispose(); | ||
} | ||
|
||
_channels.Clear(); | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() { | ||
var channelsToDispose = Array.Empty<TChannel>(); | ||
|
||
lock (_lock) { | ||
if (_disposed) | ||
return; | ||
_disposed = true; | ||
|
||
channelsToDispose = _channels.Values.ToArray(); | ||
_channels.Clear(); | ||
} | ||
|
||
await DisposeChannelsAsync(channelsToDispose).ConfigureAwait(false); | ||
} | ||
|
||
private void ThrowIfDisposed() { | ||
lock (_lock) { | ||
if (_disposed) { | ||
throw new ObjectDisposedException(GetType().ToString()); | ||
} | ||
} | ||
} | ||
|
||
private static async Task DisposeChannelsAsync(IEnumerable<TChannel> channels) { | ||
foreach (var channel in channels) | ||
await channel.DisposeAsync().ConfigureAwait(false); | ||
} | ||
|
||
private class DnsEndPointEqualityComparer : IEqualityComparer<DnsEndPoint> { | ||
public static readonly DnsEndPointEqualityComparer Instance = new(); | ||
|
||
public bool Equals(DnsEndPoint? x, DnsEndPoint? y) { | ||
if (ReferenceEquals(x, y)) | ||
return true; | ||
if (x is null) | ||
return false; | ||
if (y is null) | ||
return false; | ||
if (x.GetType() != y.GetType()) | ||
return false; | ||
return | ||
string.Equals(x.Host, y.Host, StringComparison.OrdinalIgnoreCase) && | ||
x.Port == y.Port; | ||
} | ||
|
||
public int GetHashCode(DnsEndPoint obj) { | ||
unchecked { | ||
return (StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Host) * 397) ^ | ||
obj.Port; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.