Skip to content

Commit

Permalink
Fix 3rd set of Codacy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Oct 14, 2023
1 parent 9cd3bfa commit bd1b98d
Show file tree
Hide file tree
Showing 27 changed files with 261 additions and 163 deletions.
18 changes: 12 additions & 6 deletions KeyManager.Library.KeyStore.File/FileKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Leosac.KeyManager.Library.KeyStore.File
{
public class FileKeyStore : KeyStore
{
public const string LeosacKeyFileExtension = ".leok";
public static string LeosacKeyFileExtension => ".leok";

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);

Expand Down Expand Up @@ -49,7 +49,9 @@ public override Task Open()
if (!System.IO.Directory.Exists(GetFileProperties().Fullpath))
{
if (CreateIfMissing)
{
System.IO.Directory.CreateDirectory(GetFileProperties().Fullpath);
}
else
{
log.Error(string.Format("Cannot open the key sore `{0}`.", GetFileProperties().Fullpath));
Expand Down Expand Up @@ -100,12 +102,14 @@ public override async Task Create(IChangeKeyEntry change)
System.IO.File.WriteAllText(kefile, cryptogram.Value);
}
else
{
throw new KeyStoreException("Unsupported `change` parameter.");
}

log.Info(string.Format("Key entry `{0}` created.", change.Identifier));
}

public override async Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool ignoreIfMissing = false)
public override async Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool ignoreIfMissing)
{
log.Info(string.Format("Deleting key entry `{0}`...", identifier));
var exists = await CheckKeyEntryExists(identifier, keClass);
Expand Down Expand Up @@ -158,7 +162,9 @@ private byte[] GetWrappingKey()

var key = Convert.FromHexString(Properties.Secret);
if (key.Length != 16 && key.Length != 32)
{
throw new KeyStoreException("Wrong wrapping key length.");
}

return key;
}
Expand All @@ -170,7 +176,7 @@ public override Task<IList<KeyEntryId>> GetAll(KeyEntryClass? keClass = null)
var filter = "*";
if (keClass != null)
{
filter += "." + keClass.ToString()!.ToLower();
filter += "." + keClass.ToString()!.ToLowerInvariant();
}
filter += LeosacKeyFileExtension;
var files = System.IO.Directory.GetFiles(GetFileProperties().Fullpath, filter);
Expand Down Expand Up @@ -203,7 +209,7 @@ public override async Task Store(IList<IChangeKeyEntry> changes)
log.Info("Key Entries storing completed.");
}

public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing = false)
public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing)
{
log.Info(string.Format("Updating key entry `{0}`...", change.Identifier));
await Delete(change.Identifier, change.KClass, ignoreIfMissing);
Expand All @@ -212,7 +218,7 @@ public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing =
log.Info(string.Format("Key entry `{0}` updated.", change.Identifier));
}

public override async Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput = null, KeyEntryId? wrappingKeyId = null, string? wrappingKeyContainerSelector = null)
public override async Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, KeyEntryId? wrappingKeyId, string? wrappingKeyContainerSelector)
{
string? result = null;
log.Info(string.Format("Resolving key entry link with Key Entry Identifier `{0}`, Div Input `{1}`...", keyIdentifier, divInput));
Expand Down Expand Up @@ -249,7 +255,7 @@ public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing =
return result;
}

public override async Task<string?> ResolveKeyLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? containerSelector, string? divInput = null)
public override async Task<string?> ResolveKeyLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? containerSelector, string? divInput)
{
string? result = null;
log.Info(string.Format("Resolving key link with Key Entry Identifier `{0}`, Container Selector `{1}`, Div Input `{2}`...", keyIdentifier, containerSelector, divInput));
Expand Down
25 changes: 10 additions & 15 deletions KeyManager.Library.KeyStore.File/FileKeyStoreProperties.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Leosac.KeyManager.Library.KeyStore.File
namespace Leosac.KeyManager.Library.KeyStore.File
{
public class FileKeyStoreProperties : KeyStoreProperties, IEquatable<FileKeyStoreProperties>
{
public FileKeyStoreProperties() : base()
public FileKeyStoreProperties()
{
_fullpath = String.Empty;
_fullpath = string.Empty;
}

private string _fullpath;
Expand All @@ -33,13 +25,19 @@ public override bool Equals(object? obj)
public bool Equals(FileKeyStoreProperties? p)
{
if (p is null)
{
return false;
}

if (Object.ReferenceEquals(this, p))
{
return true;
}

if (this.GetType() != p.GetType())
{
return false;
}

return (Fullpath == p.Fullpath);
}
Expand All @@ -50,10 +48,7 @@ public bool Equals(FileKeyStoreProperties? p)
{
if (lhs is null)
{
if (rhs is null)
return true;

return false;
return rhs is null;
}

return lhs.Equals(rhs);
Expand Down
36 changes: 24 additions & 12 deletions KeyManager.Library.KeyStore.HSM_PKCS11/AsymmetricPKCS11KeyEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ namespace Leosac.KeyManager.Library.KeyStore.HSM_PKCS11
{
public class AsymmetricPKCS11KeyEntry : PKCS11KeyEntry
{
public AsymmetricPKCS11KeyEntry(KeyEntryClass kclass = KeyEntryClass.Asymmetric) : base()
public AsymmetricPKCS11KeyEntry() : this(KeyEntryClass.Asymmetric)
{

}

public AsymmetricPKCS11KeyEntry(KeyEntryClass kclass)
{
Properties = new AsymmetricPKCS11KeyEntryProperties();
_kclass = kclass;
}

KeyEntryClass _kclass;
private readonly KeyEntryClass _kclass;

public override KeyEntryClass KClass => _kclass;

Expand All @@ -27,16 +32,23 @@ public override void GetAttributes(ISession session, IObjectHandle? handle)

foreach (var attribute in attributes)
{
if (attribute.Type == (ulong)CKA.CKA_ENCRYPT)
PKCS11Properties!.Encrypt = attribute.GetValueAsBool();
else if (attribute.Type == (ulong)CKA.CKA_DECRYPT)
PKCS11Properties!.Decrypt = attribute.GetValueAsBool();
else if (attribute.Type == (ulong)CKA.CKA_DERIVE)
PKCS11Properties!.Derive = attribute.GetValueAsBool();
else if (attribute.Type == (ulong)CKA.CKA_EXTRACTABLE)
PKCS11Properties!.Extractable = attribute.GetValueAsBool();
else
throw new KeyStoreException("Unexpected attribute.");
switch (attribute.Type)
{
case (ulong)CKA.CKA_ENCRYPT:
PKCS11Properties!.Encrypt = attribute.GetValueAsBool();
break;
case (ulong)CKA.CKA_DECRYPT:
PKCS11Properties!.Decrypt = attribute.GetValueAsBool();
break;
case (ulong)CKA.CKA_DERIVE:
PKCS11Properties!.Derive = attribute.GetValueAsBool();
break;
case (ulong)CKA.CKA_EXTRACTABLE:
PKCS11Properties!.Extractable = attribute.GetValueAsBool();
break;
default:
throw new KeyStoreException("Unexpected attribute.");
}
}
}
}
Expand Down
54 changes: 43 additions & 11 deletions KeyManager.Library.KeyStore.HSM_PKCS11/PKCS11KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ public class PKCS11KeyStore : KeyStore
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);

public PKCS11KeyStore()
{

}

private IPkcs11Library? _library;
private ISlot? _slot;
private ISession? _session;
Expand Down Expand Up @@ -43,7 +38,9 @@ public Task<bool> CheckKeyEntryExists(KeyEntryId identifier, out IObjectHandle?
public Task<bool> CheckKeyEntryExists(KeyEntryId identifier, KeyEntryClass? keClass, out IObjectHandle? handle)
{
if (_session == null)
{
throw new KeyStoreException("No valid session.");
}

if (identifier.Handle != null && identifier.Handle is IObjectHandle h)
{
Expand All @@ -53,9 +50,13 @@ public Task<bool> CheckKeyEntryExists(KeyEntryId identifier, KeyEntryClass? keCl

var attributes = new List<IObjectAttribute>();
if (!string.IsNullOrEmpty(identifier.Id))
{
attributes.Add(_session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ID, Convert.FromHexString(identifier.Id)));
}
if (!string.IsNullOrEmpty(identifier.Label))
{
attributes.Add(_session.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL, UTF8Encoding.UTF8.GetBytes(identifier.Label)));
}

var objects = new List<IObjectHandle>();
if (keClass != null)
Expand All @@ -81,7 +82,9 @@ public Task<bool> CheckKeyEntryExists(KeyEntryId identifier, KeyEntryClass? keCl
}

if (attributes.Count == 0)
{
throw new KeyStoreException("No key identifier.");
}

var allObjects = objects.Union(_session.FindAllObjects(attributes));
handle = allObjects.FirstOrDefault();
Expand Down Expand Up @@ -120,11 +123,18 @@ public override async Task Create(IChangeKeyEntry change)
var attributes = GetKeyEntryAttributes(entry, true);
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_VALUE, rawkey));
if (entry.KClass == KeyEntryClass.PrivateKey || (entry.KClass == KeyEntryClass.Asymmetric && material.Name == KeyMaterial.PRIVATE_KEY))
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
}
else if (entry.KClass == KeyEntryClass.PublicKey || (entry.KClass == KeyEntryClass.Asymmetric && material.Name == KeyMaterial.PUBLIC_KEY))
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY));
}
else
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY));
}

_session!.CreateObject(attributes);
}
}
Expand Down Expand Up @@ -153,7 +163,12 @@ public override async Task Create(IChangeKeyEntry change)
log.Info(string.Format("Key entry `{0}` created.", change.Identifier));
}

private List<IObjectAttribute> GetKeyEntryAttributes(KeyEntry? entry, bool create = false)
private List<IObjectAttribute> GetKeyEntryAttributes(KeyEntry? entry)
{
return GetKeyEntryAttributes(entry, false);
}

private List<IObjectAttribute> GetKeyEntryAttributes(KeyEntry? entry, bool create)
{
if (entry != null && entry.Variant?.KeyContainers.Count > 1)
{
Expand All @@ -164,24 +179,37 @@ private List<IObjectAttribute> GetKeyEntryAttributes(KeyEntry? entry, bool creat
if (entry != null)
{
if (entry.Identifier.Id != null && create)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_ID, Convert.FromHexString(entry.Identifier.Id)));
}

if (entry.Identifier.Label != null)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL, UTF8Encoding.UTF8.GetBytes(entry.Identifier.Label)));
}
}
if (entry is PKCS11KeyEntry pkcsEntry)
{
if (create)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE, pkcsEntry.GetCKK()));
if (pkcsEntry.PKCS11Properties!.Extractable != null)
{
attributes.Add(_session.Factories.ObjectAttributeFactory.Create(CKA.CKA_EXTRACTABLE, pkcsEntry.PKCS11Properties.Extractable.Value));
}
}
if (pkcsEntry.PKCS11Properties!.Encrypt != null)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_ENCRYPT, pkcsEntry.PKCS11Properties!.Encrypt.Value));
}
if (pkcsEntry.PKCS11Properties.Decrypt != null)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_DECRYPT, pkcsEntry.PKCS11Properties.Decrypt.Value));
}
if (pkcsEntry.PKCS11Properties.Derive != null)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_DERIVE, pkcsEntry.PKCS11Properties.Derive.Value));
}
}
else
{
Expand All @@ -203,7 +231,7 @@ private List<IObjectAttribute> GetKeyEntryAttributes(KeyEntry? entry, bool creat
return attributes;
}

public override async Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool ignoreIfMissing = false)
public override async Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool ignoreIfMissing)
{
log.Info(string.Format("Deleting key entry `{0}`...", identifier));
var exists = await CheckKeyEntryExists(identifier, keClass, out IObjectHandle? handle);
Expand Down Expand Up @@ -299,13 +327,15 @@ public override async Task Delete(KeyEntryId identifier, KeyEntryClass keClass,
return keyEntry;
}

public override Task<IList<KeyEntryId>> GetAll(KeyEntryClass? keClass = null)
public override Task<IList<KeyEntryId>> GetAll(KeyEntryClass? keClass)
{
log.Info(string.Format("Getting all key entries (class: `{0}`)...", keClass));
IList<KeyEntryId> entries = new List<KeyEntryId>();

if (_session == null)
{
throw new KeyStoreException("No valid session.");
}

var attributes = new List<IObjectAttribute>
{
Expand Down Expand Up @@ -447,7 +477,7 @@ public override Task Open()
return Task.CompletedTask;
}

public override async Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput = null, KeyEntryId? wrappingKeyId = null, string? wrappingContainerSelector = null)
public override async Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, KeyEntryId? wrappingKeyId, string? wrappingContainerSelector)
{
log.Info(string.Format("Resolving key entry link with Key Entry Identifier `{0}` and Wrapping Key Entry Identifier `{1}`...", keyIdentifier, wrappingKeyId));
if (wrappingKeyId == null)
Expand Down Expand Up @@ -499,7 +529,7 @@ protected IMechanism CreateMostExpectedWrappingMechanism(IObjectHandle handle)
return _session.Factories.MechanismFactory.Create(ckm);
}

public override async Task<string?> ResolveKeyLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? containerSelector = null, string? divInput = null)
public override async Task<string?> ResolveKeyLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? containerSelector, string? divInput)
{
log.Info(string.Format("Resolving key link with Key Entry Identifier `{0}`...", keyIdentifier));
if (!string.IsNullOrEmpty(divInput))
Expand Down Expand Up @@ -541,7 +571,7 @@ public override async Task Store(IList<IChangeKeyEntry> changes)
log.Info("Key Entries storing completed.");
}

public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing = false)
public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing)
{
log.Info(string.Format("Updating key entry `{0}`...", change.Identifier));

Expand All @@ -566,7 +596,9 @@ public override async Task Update(IChangeKeyEntry change, bool ignoreIfMissing =
_session!.SetAttributeValue(handle, attributes);
}
else
{
throw new NotImplementedException();
}

log.Info(string.Format("Key entry `{0}` updated.", change.Identifier));
}
Expand Down
Loading

0 comments on commit bd1b98d

Please sign in to comment.