Skip to content

Commit

Permalink
Add Key Store options and variables on key entry id
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Feb 9, 2024
1 parent 305ae3e commit 82431f4
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 92 deletions.
11 changes: 11 additions & 0 deletions KeyManager.Library.KeyStore.File/FileKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public override Task Open()
log.Error(string.Format("Cannot open the key sore `{0}`.", GetFileProperties().Fullpath));
throw new KeyStoreException("Cannot open the key sore.");
}

if (!Attributes.ContainsKey(ATTRIBUTE_NAME))
{
var dirname = System.IO.Path.GetDirectoryName(GetFileProperties().Fullpath);
if (!string.IsNullOrEmpty(dirname))
{
Attributes[ATTRIBUTE_NAME] = dirname;
Attributes[ATTRIBUTE_HEXNAME] = Convert.ToHexString(Encoding.UTF8.GetBytes(dirname));
}
}

log.Info("Key store opened.");
return Task.CompletedTask;
}
Expand Down
53 changes: 38 additions & 15 deletions KeyManager.Library.KeyStore.HSM_PKCS11/PKCS11KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,21 @@ public override async Task Create(IChangeKeyEntry change)
}
else if (change is KeyEntryCryptogram cryptogram)
{
if (cryptogram.WrappingKeyId == null)
var wrappingKey = cryptogram.WrappingKey;
if (!(wrappingKey?.KeyId.IsConfigured()).GetValueOrDefault(false))
{
wrappingKey = Options?.WrappingKey;
}

if (wrappingKey?.KeyId == null)
{
log.Error("Wrapping Key Entry Identifier parameter is expected.");
throw new KeyStoreException("Wrapping Key Entry Identifier parameter is expected.");
}

if (!await CheckKeyEntryExists(cryptogram.WrappingKeyId, out IObjectHandle? wrapHandle))
if (!await CheckKeyEntryExists(wrappingKey.KeyId, out IObjectHandle? wrapHandle))
{
log.Error(string.Format("The key entry `{0}` doesn't exist.", cryptogram.WrappingKeyId));
log.Error(string.Format("The key entry `{0}` doesn't exist.", wrappingKey.KeyId));
throw new KeyStoreException("The key entry doesn't exist.");
}
if (!string.IsNullOrEmpty(cryptogram.Value))
Expand Down Expand Up @@ -201,6 +207,11 @@ public override Task<KeyEntryId> Generate(KeyEntry keyEntry)
{
mechanism = _session!.Factories.MechanismFactory.Create(CKM.CKM_DES3_KEY_GEN);
}

if (key.KeySize > 0)
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_VALUE_LEN, key.KeySize));
}
}

mechanism ??= _session!.Factories.MechanismFactory.Create(CKM.CKM_GENERIC_SECRET_KEY_GEN);
Expand Down Expand Up @@ -280,21 +291,19 @@ private List<IObjectAttribute> GetKeyEntryAttributes(KeyEntry? entry, bool creat
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true));
}

var cclass = CKO.CKO_SECRET_KEY;
if (entry != null && !string.IsNullOrEmpty(materialName))
{
if (entry.KClass == KeyEntryClass.PrivateKey || (entry.KClass == KeyEntryClass.Asymmetric && materialName == KeyMaterial.PRIVATE_KEY))
{
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
cclass = CKO.CKO_PRIVATE_KEY;
}
else if (entry.KClass == KeyEntryClass.PublicKey || (entry.KClass == KeyEntryClass.Asymmetric && materialName == 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));
cclass = CKO.CKO_PUBLIC_KEY;
}
}
attributes.Add(_session!.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, cclass));

return attributes;
}
Expand Down Expand Up @@ -535,10 +544,10 @@ public override Task Open()
return Task.CompletedTask;
}

public override async Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, KeyEntryId? wrappingKeyId, string? wrappingContainerSelector)
public override async Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, WrappingKey? wrappingKey)
{
log.Info(string.Format("Resolving key entry link with Key Entry Identifier `{0}` and Wrapping Key Entry Identifier `{1}`...", keyIdentifier, wrappingKeyId));
if (wrappingKeyId == null)
log.Info(string.Format("Resolving key entry link with Key Entry Identifier `{0}` and Wrapping Key Entry Identifier `{1}`...", keyIdentifier, wrappingKey?.KeyId));
if (wrappingKey == null || !wrappingKey.KeyId.IsConfigured())
{
log.Error("Wrapping Key Entry Identifier parameter is expected.");
throw new KeyStoreException("Wrapping Key Entry Identifier parameter is expected.");
Expand All @@ -554,9 +563,9 @@ public override Task Open()
log.Error(string.Format("The key entry `{0}` doesn't exist.", keyIdentifier));
throw new KeyStoreException("The key entry doesn't exist.");
}
if (!await CheckKeyEntryExists(wrappingKeyId, out IObjectHandle? wrapHandle))
if (!await CheckKeyEntryExists(wrappingKey.KeyId, out IObjectHandle? wrapHandle))
{
log.Error(string.Format("The key entry `{0}` doesn't exist.", wrappingKeyId));
log.Error(string.Format("The key entry `{0}` doesn't exist.", wrappingKey.KeyId));
throw new KeyStoreException("The key entry doesn't exist.");
}

Expand Down Expand Up @@ -622,7 +631,21 @@ public override async Task Store(IList<IChangeKeyEntry> changes)
}
else
{
await Create(change);
if ((Options?.GenerateKeys).GetValueOrDefault(false))
{
if (change is KeyEntry ke)
{
await Generate(ke);
}
else
{
await Generate(change.Identifier, change.KClass);
}
}
else
{
await Create(change);
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions KeyManager.Library.KeyStore.LCP/LCPKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ public override async Task Create(IChangeKeyEntry change)
}
else if (change is KeyEntryCryptogram cryptogram)
{
if (cryptogram.WrappingKeyId == null)
var wrappingKey = cryptogram.WrappingKey;
if (wrappingKey == null)
{
wrappingKey = Options?.WrappingKey;
}
if (wrappingKey == null || !wrappingKey.KeyId.IsConfigured())
{
log.Error("Wrapping Key Entry Identifier parameter is expected.");
throw new KeyStoreException("Wrapping Key Entry Identifier parameter is expected.");
}

if (!await CheckKeyEntryExists(cryptogram.WrappingKeyId, change.KClass))
if (!await CheckKeyEntryExists(wrappingKey.KeyId, change.KClass))
{
log.Error(string.Format("The key entry `{0}` doesn't exist.", cryptogram.WrappingKeyId));
log.Error(string.Format("The key entry `{0}` doesn't exist.", wrappingKey.KeyId));
throw new KeyStoreException("The key entry doesn't exist.");
}

Expand Down
2 changes: 1 addition & 1 deletion KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
log.Info(string.Format("Key usage counter `{0}` updated.", counter.Identifier));
}

public override Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, KeyEntryId? wrappingKeyId, string? wrappingContainerSelector)
public override Task<string?> ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, WrappingKey? wrappingKey)
{
// Will be supported with SAM AV3
throw new NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override async Task RunLinkImpl(KeyStore.KeyStore ks)
{
if (KeyEntryLink != null)
{
LinkResult = await ks.ResolveKeyEntryLink(KeyEntryLink.KeyIdentifier, Class, DivInputResult, KeyEntryLink.WrappingKeyId, KeyEntryLink.WrappingKeySelector);
LinkResult = await ks.ResolveKeyEntryLink(KeyEntryLink.KeyIdentifier, Class, DivInputResult, KeyEntryLink.WrappingKey);
}
}
}
Expand Down
21 changes: 5 additions & 16 deletions KeyManager.Library.UI/Domain/PublishKeyStoreDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,21 @@ public class PublishKeyStoreDialogViewModel : ObservableValidator
{
public PublishKeyStoreDialogViewModel()
{
_wrappingKeySelector = "0";
_wrappingKeyId = new KeyEntryId();
_options = new StoreOptions();
}

private Favorite? _favorite;

public Favorite? Favorite
{
get => _favorite;
set => SetProperty(ref _favorite, value);
}

private KeyEntryId _wrappingKeyId;

public KeyEntryId WrappingKeyId
{
get => _wrappingKeyId;
set => SetProperty(ref _wrappingKeyId, value);
}

private string _wrappingKeySelector;

public string WrappingKeySelector
private StoreOptions _options;
public StoreOptions Options
{
get => _wrappingKeySelector;
set => SetProperty(ref _wrappingKeySelector, value);
get => _options;
set => SetProperty(ref _options, value);
}
}
}
2 changes: 2 additions & 0 deletions KeyManager.Library.UI/FavoriteExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Leosac.KeyManager.Library.Plugin;
using System.Text;

namespace Leosac.KeyManager.Library.UI
{
Expand All @@ -14,6 +15,7 @@ public static class FavoriteExtension
ks.DefaultKeyEntries = fav.DefaultKeyEntries;

ks.Attributes[KeyStore.KeyStore.ATTRIBUTE_NAME] = fav.Name;
ks.Attributes[KeyStore.KeyStore.ATTRIBUTE_HEXNAME] = Convert.ToHexString(Encoding.UTF8.GetBytes(fav.Name));

return ks;
}
Expand Down
2 changes: 2 additions & 0 deletions KeyManager.Library.UI/Favorites.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Leosac.WpfApp;
using System.Collections.ObjectModel;
using System.Text;

namespace Leosac.KeyManager.Library.UI
{
Expand Down Expand Up @@ -62,6 +63,7 @@ public Favorite CreateFromKeyStore(KeyStore.KeyStore store, bool save)
}
log.Info(string.Format("New Favorite `{0}` saved.", favorite.Name));
store.Attributes[KeyStore.KeyStore.ATTRIBUTE_NAME] = favorite.Name;
store.Attributes[KeyStore.KeyStore.ATTRIBUTE_HEXNAME] = Convert.ToHexString(Encoding.UTF8.GetBytes(favorite.Name));
return favorite;
}
}
Expand Down
4 changes: 2 additions & 2 deletions KeyManager.Library.UI/KeyEntryLinkDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
<StackPanel>
<Expander HorizontalAlignment="Stretch" Header="{x:Static properties:Resources.WrappingKey}">
<StackPanel Orientation="Vertical" TextBlock.Foreground="{DynamicResource MaterialDesignBody}" Margin="24,8,24,16">
<local:KeyEntryIdControl KeyEntryId="{Binding KeyEntryLink.WrappingKeyId, Mode=TwoWay}" />
<TextBox Text="{Binding KeyEntryLink.WrappingKeySelector, Mode=TwoWay}" Margin="5"
<local:KeyEntryIdControl KeyEntryId="{Binding KeyEntryLink.WrappingKey.KeyId, Mode=TwoWay}" />
<TextBox Text="{Binding KeyEntryLink.WrappingKey.ContainerSelector, Mode=TwoWay}" Margin="5"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KeyVersionHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KeyVersion}"/>
</StackPanel>
Expand Down
18 changes: 18 additions & 0 deletions KeyManager.Library.UI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions KeyManager.Library.UI/Properties/Resources.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<data name="General" xml:space="preserve">
<value>Général</value>
</data>
<data name="GenerateKeyEntries" xml:space="preserve">
<value>Générer les clés</value>
</data>
<data name="GenerateKeyEntry" xml:space="preserve">
<value>Générer un enregistrement de clés</value>
</data>
Expand Down Expand Up @@ -444,6 +447,9 @@
<data name="OK" xml:space="preserve">
<value>OK</value>
</data>
<data name="Options" xml:space="preserve">
<value>Options</value>
</data>
<data name="PadByte" xml:space="preserve">
<value>Byte</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions KeyManager.Library.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<data name="General" xml:space="preserve">
<value>General</value>
</data>
<data name="GenerateKeyEntries" xml:space="preserve">
<value>Generate Key Entries</value>
</data>
<data name="GenerateKeyEntry" xml:space="preserve">
<value>Generate Key Entry</value>
</data>
Expand Down Expand Up @@ -444,6 +447,9 @@
<data name="OK" xml:space="preserve">
<value>OK</value>
</data>
<data name="Options" xml:space="preserve">
<value>Options</value>
</data>
<data name="PadByte" xml:space="preserve">
<value>Byte</value>
</data>
Expand Down
20 changes: 14 additions & 6 deletions KeyManager.Library.UI/PublishKeyStoreDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@
<local:FavoriteKeyStoreSelectionControl SelectedKeyStoreFavorite="{Binding Favorite, Mode=TwoWay}" />
<materialDesign:Card Margin="5,20,5,5">
<StackPanel>
<Expander HorizontalAlignment="Stretch" Header="{x:Static properties:Resources.WrappingKey}">
<StackPanel Orientation="Vertical" TextBlock.Foreground="{DynamicResource MaterialDesignBody}" Margin="24,8,24,16">
<local:KeyEntryIdControl KeyEntryId="{Binding WrappingKeyId, Mode=TwoWay}" />
<TextBox Text="{Binding WrappingKeySelector, Mode=TwoWay}" Margin="5"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KeyVersionHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KeyVersion}"/>
<Expander HorizontalAlignment="Stretch" Header="{x:Static properties:Resources.Options}">
<StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="5">
<ToggleButton IsChecked="{Binding Properties.Options.GenerateKeys}" Style="{StaticResource MaterialDesignSwitchLightToggleButton}" />
<TextBlock Text="{x:Static properties:Resources.GenerateKeyEntries}" Padding="3" />
</StackPanel>
<Expander HorizontalAlignment="Stretch" Header="{x:Static properties:Resources.WrappingKey}" Margin="5">
<StackPanel Orientation="Vertical" TextBlock.Foreground="{DynamicResource MaterialDesignBody}" Margin="24,8,24,16">
<local:KeyEntryIdControl KeyEntryId="{Binding Options.WrappingKey.KeyId, Mode=TwoWay}" />
<TextBox Text="{Binding Options.WrappingKey.ContainerSelector, Mode=TwoWay}" Margin="5"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KeyVersionHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KeyVersion}"/>
</StackPanel>
</Expander>
</StackPanel>
</Expander>
</StackPanel>
Expand Down
16 changes: 4 additions & 12 deletions KeyManager.Library/KeyStore/KeyEntryCryptogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@ public string? Value
set => SetProperty(ref _value, value);
}

private KeyEntryId? _wrappingKeyId;
private WrappingKey? _wrappingKey;

public KeyEntryId? WrappingKeyId
public WrappingKey? WrappingKey
{
get => _wrappingKeyId;
set => SetProperty(ref _wrappingKeyId, value);
}

private string? _wrappingContainerSelector;

public string? WrappingContainerSelector
{
get => _wrappingContainerSelector;
set => SetProperty(ref _wrappingContainerSelector, value);
get => _wrappingKey;
set => SetProperty(ref _wrappingKey, value);
}
}
}
Loading

0 comments on commit 82431f4

Please sign in to comment.