refactor(RSAFactory): params-Abhängigkeit durch Ersetzen von params.Defaults entfernt
This commit is contained in:
parent
6a12ad77ec
commit
8498dc0456
@ -6,13 +6,13 @@ namespace DigitalData.Core.Security.Config
|
|||||||
{
|
{
|
||||||
public class RSAFactoryParams
|
public class RSAFactoryParams
|
||||||
{
|
{
|
||||||
public int KeySizeInBits { get; init; } = 2048;
|
public int KeySizeInBits { get; init; } = Default.KeySizeInBits;
|
||||||
|
|
||||||
public string PbePassword { internal get; init; } = Secrets.PBE_PASSWORD;
|
public string PbePassword { internal get; init; } = Default.PbePassword;
|
||||||
|
|
||||||
public PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } = PbeEncryptionAlgorithm.Aes256Cbc;
|
public PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } = Default.PbeEncryptionAlgorithm;
|
||||||
|
|
||||||
public HashAlgorithmName PbeHashAlgorithm { get; init; } = HashAlgorithmName.SHA256;
|
public HashAlgorithmName PbeHashAlgorithm { get; init; } = Default.PbeHashAlgorithm;
|
||||||
|
|
||||||
// TODO: add as json converter to IConfigurIConfiguration.Config
|
// TODO: add as json converter to IConfigurIConfiguration.Config
|
||||||
public string PbeHashAlgorithmName
|
public string PbeHashAlgorithmName
|
||||||
@ -23,9 +23,9 @@ namespace DigitalData.Core.Security.Config
|
|||||||
: new(value);
|
: new(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int PbeIterationCount { get; init; } = 100_000;
|
public int PbeIterationCount { get; init; } = Default.PbeIterationCount;
|
||||||
|
|
||||||
public string EncryptedPrivateKeyPemLabel { get; init; } = "ENCRYPTED PRIVATE KEY";
|
public string EncryptedPrivateKeyPemLabel { get; init; } = Default.EncryptedPrivateKeyPemLabel;
|
||||||
|
|
||||||
private readonly Lazy<PbeParameters> _lazyPbeParameters;
|
private readonly Lazy<PbeParameters> _lazyPbeParameters;
|
||||||
|
|
||||||
@ -36,5 +36,22 @@ namespace DigitalData.Core.Security.Config
|
|||||||
{
|
{
|
||||||
_lazyPbeParameters = new(() => new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithm, PbeIterationCount));
|
_lazyPbeParameters = new(() => new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithm, PbeIterationCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Default
|
||||||
|
{
|
||||||
|
public static readonly int KeySizeInBits = 2048;
|
||||||
|
|
||||||
|
public static readonly string PbePassword = Secrets.PBE_PASSWORD;
|
||||||
|
|
||||||
|
public static readonly PbeEncryptionAlgorithm PbeEncryptionAlgorithm = PbeEncryptionAlgorithm.Aes256Cbc;
|
||||||
|
|
||||||
|
public static readonly HashAlgorithmName PbeHashAlgorithm = HashAlgorithmName.SHA256;
|
||||||
|
|
||||||
|
public static readonly int PbeIterationCount = 100_000;
|
||||||
|
|
||||||
|
public static readonly string EncryptedPrivateKeyPemLabel = "ENCRYPTED PRIVATE KEY";
|
||||||
|
|
||||||
|
public static readonly PbeParameters PbeParameters = new(PbeEncryptionAlgorithm, PbeHashAlgorithm, PbeIterationCount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4,10 +4,12 @@ using DigitalData.Core.Security.RSAKey;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security
|
namespace DigitalData.Core.Security;
|
||||||
{
|
|
||||||
public class CryptoFactory : RSAFactory<CryptoFactoryParams>, ICryptoFactory, IAsymmetricKeyFactory
|
public class CryptoFactory : RSAFactory, ICryptoFactory, IAsymmetricKeyFactory
|
||||||
{
|
{
|
||||||
|
private readonly CryptoFactoryParams _params;
|
||||||
|
|
||||||
public IEnumerable<IAsymmetricDecryptor> Decryptors { get; }
|
public IEnumerable<IAsymmetricDecryptor> Decryptors { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -17,8 +19,10 @@ namespace DigitalData.Core.Security
|
|||||||
|
|
||||||
public IEnumerable<IAsymmetricTokenDescriptor> TokenDescriptors { get; init; } = new List<IAsymmetricTokenDescriptor>();
|
public IEnumerable<IAsymmetricTokenDescriptor> TokenDescriptors { get; init; } = new List<IAsymmetricTokenDescriptor>();
|
||||||
|
|
||||||
public CryptoFactory(IOptions<CryptoFactoryParams> options, ILogger<CryptoFactory>? logger = null) : base(options)
|
public CryptoFactory(IOptions<CryptoFactoryParams> cryptoFactoryParamsOptions, ILogger<CryptoFactory>? logger = null)
|
||||||
{
|
{
|
||||||
|
_params = cryptoFactoryParamsOptions.Value;
|
||||||
|
|
||||||
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
|
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
|
||||||
|
|
||||||
if (!_params.Decryptors.Any())
|
if (!_params.Decryptors.Any())
|
||||||
@ -35,4 +39,3 @@ namespace DigitalData.Core.Security
|
|||||||
VaultDecryptor = _params.VaultDecryptor ?? Decryptors.First();
|
VaultDecryptor = _params.VaultDecryptor ?? Decryptors.First();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@ -1,22 +1,14 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Abstractions.Security;
|
||||||
using DigitalData.Core.Security.Config;
|
using DigitalData.Core.Security.Config;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.RSAKey
|
namespace DigitalData.Core.Security.RSAKey;
|
||||||
{
|
|
||||||
public class RSAFactory<TRSAFactoryParams> : IAsymmetricKeyFactory where TRSAFactoryParams : RSAFactoryParams
|
|
||||||
{
|
|
||||||
protected readonly TRSAFactoryParams _params;
|
|
||||||
|
|
||||||
public RSAFactory(IOptions<TRSAFactoryParams> options)
|
public class RSAFactory : IAsymmetricKeyFactory
|
||||||
{
|
{
|
||||||
_params = options.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
|
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
|
||||||
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
|
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
|
||||||
: RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem();
|
: RSA.Create(keySizeInBits ?? RSAFactoryParams.Default.KeySizeInBits).ExportRSAPrivateKeyPem();
|
||||||
|
|
||||||
public string CreateEncryptedPrivateKeyPem(
|
public string CreateEncryptedPrivateKeyPem(
|
||||||
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
|
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
|
||||||
@ -25,18 +17,16 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
int? keySizeInBits = null,
|
int? keySizeInBits = null,
|
||||||
string? password = null)
|
string? password = null)
|
||||||
{
|
{
|
||||||
password ??= _params.PbePassword;
|
password ??= RSAFactoryParams.Default.PbePassword;
|
||||||
|
|
||||||
var pbeParameters = pbeEncryptionAlgorithm is null && hashAlgorithmName is null && iterationCount is null
|
var pbeParameters = new PbeParameters(
|
||||||
? new PbeParameters(
|
pbeEncryptionAlgorithm ?? RSAFactoryParams.Default.PbeEncryptionAlgorithm,
|
||||||
pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm,
|
hashAlgorithmName ?? RSAFactoryParams.Default.PbeHashAlgorithm,
|
||||||
hashAlgorithmName ?? _params.PbeHashAlgorithm,
|
iterationCount ?? RSAFactoryParams.Default.PbeIterationCount);
|
||||||
iterationCount ?? _params.PbeIterationCount)
|
|
||||||
: _params.PbeParameters;
|
|
||||||
|
|
||||||
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
|
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? RSAFactoryParams.Default.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
|
||||||
|
|
||||||
var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
|
var pemChars = PemEncoding.Write(RSAFactoryParams.Default.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
|
||||||
|
|
||||||
return new string(pemChars);
|
return new string(pemChars);
|
||||||
}
|
}
|
||||||
@ -46,11 +36,11 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
int? keySizeInBits = null,
|
int? keySizeInBits = null,
|
||||||
string? password = null)
|
string? password = null)
|
||||||
{
|
{
|
||||||
password ??= _params.PbePassword;
|
password ??= RSAFactoryParams.Default.PbePassword;
|
||||||
|
|
||||||
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
|
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? RSAFactoryParams.Default.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
|
||||||
|
|
||||||
var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
|
var pemChars = PemEncoding.Write(RSAFactoryParams.Default.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
|
||||||
|
|
||||||
return new string(pemChars);
|
return new string(pemChars);
|
||||||
}
|
}
|
||||||
@ -62,4 +52,3 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
Padding = padding ?? RSAEncryptionPadding.OaepSHA256
|
Padding = padding ?? RSAEncryptionPadding.OaepSHA256
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user