feat(AsymCryptParams): Funktionalität erstellt, um pem aus der Datei zu setzen, wenn diese null ist.
- Wenn die pem Datei nicht existiert, erstellt
This commit is contained in:
parent
644283cf8f
commit
f267fe955b
@ -1,4 +1,5 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Security.Cryptographer;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.Config
|
namespace DigitalData.Core.Security.Config
|
||||||
{
|
{
|
||||||
@ -6,10 +7,7 @@ namespace DigitalData.Core.Security.Config
|
|||||||
{
|
{
|
||||||
public string PemDirectory { get; init; } = string.Empty;
|
public string PemDirectory { get; init; } = string.Empty;
|
||||||
|
|
||||||
/// <summary>
|
public string Separator { get; init; } = "_-_";
|
||||||
/// 0: Issuer - 1: Audience - 2: Type tag - 3: Version
|
|
||||||
/// </summary>
|
|
||||||
public string FileNameFormat { get; init; } = "{0}_-_{1}_-_{2}_-_{3}.pem";
|
|
||||||
|
|
||||||
public string EncryptorTag { get; init; } = "public";
|
public string EncryptorTag { get; init; } = "public";
|
||||||
|
|
||||||
@ -17,40 +15,72 @@ namespace DigitalData.Core.Security.Config
|
|||||||
|
|
||||||
public string EncryptedDecryptorTag { get; init; } = "enc-private";
|
public string EncryptedDecryptorTag { get; init; } = "enc-private";
|
||||||
|
|
||||||
public IEnumerable<IRSADecryptor> Decryptors { get; init; } = new List<IRSADecryptor>();
|
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
|
||||||
|
|
||||||
public IEnumerable<IRSAEncryptor> Encryptors { get; init; } = new List<IRSAEncryptor>();
|
public IEnumerable<RSAEncryptor> Encryptors { get; init; } = new List<RSAEncryptor>();
|
||||||
|
|
||||||
private string TypeTagOf(IRSACryptographer crypt)
|
/// <summary>
|
||||||
|
/// 0: Issuer - 1: Audience - 2: Type tag - 3: Secret version
|
||||||
|
/// </summary>
|
||||||
|
private string CreateFileName(params object[] objs) => string.Join(Separator, objs);
|
||||||
|
|
||||||
|
private static (bool IsDecryptor, bool IsEncrypted) StateOf(RSACryptographer crypt) => crypt switch
|
||||||
{
|
{
|
||||||
if (crypt is IRSAEncryptor)
|
RSAEncryptor => (false, false),
|
||||||
return EncryptorTag;
|
RSADecryptor decryptor => (true, decryptor.Encrypt),
|
||||||
else if (crypt is IRSADecryptor decryptor)
|
_ => throw new InvalidOperationException("Unknown cryptographer type. The crypt parameter must be either RSAEncryptor or RSADecryptor.")
|
||||||
return decryptor.Encrypt ? EncryptedDecryptorTag : DecryptorTag;
|
};
|
||||||
else
|
|
||||||
throw new InvalidOperationException(
|
private string TypeTagOf((bool IsDecryptor, bool IsEncrypted) stateOfCrypt) => stateOfCrypt switch
|
||||||
"Unknown cryptographer type. The crypt parameter must be either IRSAEncryptor or IRSADecryptor.");
|
{
|
||||||
}
|
(false, false) => EncryptorTag,
|
||||||
|
(true, false) => DecryptorTag,
|
||||||
|
(true, true) => EncryptedDecryptorTag,
|
||||||
|
_ => throw new InvalidOperationException("Unknown cryptographer type. The crypt parameter must be either RSAEncryptor or RSADecryptor.")
|
||||||
|
};
|
||||||
|
|
||||||
|
private string CreatePem((bool IsDecryptor, bool IsEncrypted) stateOfCrypt) => stateOfCrypt switch
|
||||||
|
{
|
||||||
|
(true, false) => Instance.RSAFactory.CreateRSAPrivateKeyPem(keySizeInBits: KeySizeInBits),
|
||||||
|
(true, true) => Instance.RSAFactory.CreateEncryptedPrivateKeyPem(keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD,
|
||||||
|
pbeEncryptionAlgorithm: PbeEncryptionAlgorithm, hashAlgorithmName: PbeHashAlgorithmName, iterationCount: PbeIterationCount),
|
||||||
|
_ => throw new InvalidOperationException("Unknown cryptographer type. The crypt parameter must be either RSAEncryptor or RSADecryptor.")
|
||||||
|
};
|
||||||
|
|
||||||
public override void OnDeserialized()
|
public override void OnDeserialized()
|
||||||
{
|
{
|
||||||
base.OnDeserialized();
|
base.OnDeserialized();
|
||||||
|
|
||||||
var cryptographers = Encryptors.Cast<IRSACryptographer>().Concat(Decryptors.Cast<IRSACryptographer>());
|
// Create root folder if it does not exist
|
||||||
|
if (!Directory.Exists(PemDirectory))
|
||||||
|
Directory.CreateDirectory(PemDirectory);
|
||||||
|
|
||||||
|
// merge decryptors and encryptors to process under one loop
|
||||||
|
var cryptographers = Encryptors.Cast<RSACryptographer>().Concat(Decryptors.Cast<RSACryptographer>());
|
||||||
|
|
||||||
foreach (var crypt in cryptographers)
|
foreach (var crypt in cryptographers)
|
||||||
{
|
{
|
||||||
// set default path
|
// set default path
|
||||||
//if (crypt.Pem is null)
|
if (crypt.IsPemNull)
|
||||||
//{
|
{
|
||||||
// crypt.Directory ??= Directory;
|
var state = StateOf(crypt);
|
||||||
// crypt.FileName ??= string.Format(
|
|
||||||
// FileNameFormat,
|
var file_name_params = new List<object> { crypt.Issuer, crypt.Audience, TypeTagOf(state) };
|
||||||
// crypt.Issuer,
|
if (state.IsEncrypted)
|
||||||
// crypt.Audience,
|
file_name_params.Add(Secrets.Version);
|
||||||
// TypeTagOf(crypt),
|
|
||||||
// Secrets.Version);
|
var file_name = CreateFileName(file_name_params);
|
||||||
//}
|
var path = Path.Combine(PemDirectory, file_name);
|
||||||
|
|
||||||
|
if (File.Exists(path))
|
||||||
|
crypt.SetPem(File.ReadAllText(path));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var pem = CreatePem(state);
|
||||||
|
crypt.SetPem(File.ReadAllText(pem));
|
||||||
|
Task.Run(async () => File.WriteAllTextAsync(path: path, pem));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
crypt.Init();
|
crypt.Init();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user