Skip to content

Commit 832fc3d

Browse files
committed
Add SHA1 and KCV variants for key checksum
Add encryption key change utility for File Key Store
1 parent aa6b384 commit 832fc3d

18 files changed

+236
-59
lines changed

KeyManager.Library.KeyGen/KCV.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Leosac.KeyManager.Library.KeyGen
1010
/// </summary>
1111
public class KCV : KeyChecksum
1212
{
13+
protected byte _paddedByte;
14+
1315
public override string Name => "KCV";
1416

1517
public override byte[] ComputeKCV(Key key, byte[]? iv)
@@ -19,10 +21,9 @@ public override byte[] ComputeKCV(Key key, byte[]? iv)
1921
var paddediv = new byte[KeyHelper.GetBlockSize(key.Tags)];
2022
if (key.Tags.Contains("AES"))
2123
{
22-
// For AES, GlobalPlatform specification is using a default byte value set to 0x01 and not 0x00
2324
for (var i = 0; i < paddediv.Length; i++)
2425
{
25-
paddediv[i] = 0x01;
26+
paddediv[i] = _paddedByte;
2627
}
2728
}
2829
if (iv != null)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Leosac.KeyManager.Library.KeyGen
2+
{
3+
public class KCVGlobalPlatform : KCV
4+
{
5+
public KCVGlobalPlatform()
6+
{
7+
// For AES, GlobalPlatform specification is using a default byte value set to 0x01 and not 0x00
8+
_paddedByte = 0x01;
9+
}
10+
11+
public override string Name => "KCV GlobalPlatform";
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Security.Cryptography;
2+
3+
namespace Leosac.KeyManager.Library.KeyGen
4+
{
5+
public class Sha1Checksum : KeyChecksum
6+
{
7+
public override string Name => "SHA1";
8+
9+
public override byte[] ComputeKCV(Key key, byte[]? iv)
10+
{
11+
var rawkey = key.GetAggregatedValueAsBinary() ?? throw new Exception("Key value is null");
12+
13+
// Use the IV as a Salt
14+
byte[] data;
15+
if (iv != null)
16+
{
17+
data = new byte[iv.Length + rawkey.Length];
18+
Array.Copy(iv, 0, data, 0, iv.Length);
19+
Array.Copy(rawkey, 0, data, iv.Length, rawkey.Length);
20+
}
21+
else
22+
{
23+
data = rawkey;
24+
}
25+
return SHA1.HashData(data);
26+
}
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Security.Cryptography;
2+
3+
namespace Leosac.KeyManager.Library.KeyGen
4+
{
5+
public class Sha1KCVChecksum : Sha1Checksum
6+
{
7+
public override string Name => "SHA1 KCV";
8+
9+
public override byte[] ComputeKCV(Key key, byte[]? iv)
10+
{
11+
var data = base.ComputeKCV(key, iv);
12+
Array.Resize(ref data, 3);
13+
return data;
14+
}
15+
}
16+
}

KeyManager.Library.KeyStore.File.UI/Domain/FileKeyStoreImportExportControlViewModel.cs renamed to KeyManager.Library.KeyStore.File.UI/Domain/FileKeyStoreToolsControlViewModel.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,53 @@
22
using Leosac.KeyManager.Library.UI.Domain;
33
using Leosac.WpfApp;
44
using log4net;
5+
using MaterialDesignThemes.Wpf;
56
using Microsoft.Win32;
67
using System.IO;
78
using System.Security.Cryptography;
89

910
namespace Leosac.KeyManager.Library.KeyStore.File.UI.Domain
1011
{
11-
public class FileKeyStoreImportExportControlViewModel : KeyStoreAdditionalControlViewModel
12+
public class FileKeyStoreToolsControlViewModel : KeyStoreAdditionalControlViewModel
1213
{
1314
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);
1415

15-
public FileKeyStoreImportExportControlViewModel()
16+
public FileKeyStoreToolsControlViewModel()
1617
{
1718
ImportCommand = new RelayCommand(Import);
18-
1919
ExportCommand = new RelayCommand(Export);
20+
ResetEncryptionKeyCommand = new AsyncRelayCommand(ResetEncryptionKey);
21+
_newKeyStore = new FileKeyStore();
22+
}
23+
24+
public override KeyStore? KeyStore
25+
{
26+
get => base.KeyStore;
27+
set
28+
{
29+
base.KeyStore = value;
30+
NewKeyStore.Properties = value?.Properties?.Clone() as KeyStoreProperties;
31+
if (NewKeyStore.Properties != null)
32+
{
33+
NewKeyStore.Properties.StoreSecret = false;
34+
NewKeyStore.Properties.Secret = string.Empty;
35+
}
36+
}
37+
}
38+
39+
protected KeyStore _newKeyStore;
40+
public KeyStore NewKeyStore
41+
{
42+
get => _newKeyStore;
43+
set => SetProperty(ref _newKeyStore, value);
2044
}
2145

2246
public RelayCommand ImportCommand { get; }
2347

2448
public RelayCommand ExportCommand { get; }
2549

50+
public AsyncRelayCommand ResetEncryptionKeyCommand { get; }
51+
2652
private void Import()
2753
{
2854
var ofd = new OpenFileDialog();
@@ -51,5 +77,21 @@ private void Export()
5177
}
5278
}
5379
}
80+
81+
private async Task ResetEncryptionKey()
82+
{
83+
if (KeyStore != null)
84+
{
85+
KeyStore.Options = new StoreOptions()
86+
{
87+
ResolveKeyLinks = false,
88+
ResolveVariables = false
89+
};
90+
await KeyStore.Publish(NewKeyStore, null, null, null);
91+
KeyStore.Properties = NewKeyStore.Properties;
92+
93+
DialogHost.CloseDialogCommand.Execute(null, null);
94+
}
95+
}
5496
}
5597
}

KeyManager.Library.KeyStore.File.UI/FileKeyStoreImportExportControl.xaml

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<UserControl x:Class="Leosac.KeyManager.Library.KeyStore.File.UI.FileKeyStoreToolsControl"
2+
x:Name="toolsControl"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:domain="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Domain"
8+
xmlns:libui="clr-namespace:Leosac.KeyManager.Library.UI;assembly=KeyManager.Library.UI"
9+
xmlns:local="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI"
10+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
11+
xmlns:properties="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Properties"
12+
xmlns:wpfappctrls="clr-namespace:Leosac.WpfApp.Controls;assembly=WpfApp"
13+
mc:Ignorable="d"
14+
d:DataContext="{d:DesignInstance domain:FileKeyStoreToolsControlViewModel}"
15+
d:DesignHeight="450" d:DesignWidth="800">
16+
<UserControl.Resources>
17+
<materialDesign:DialogHost DialogTheme="Inherit" DataContext="{Binding NewKeyStore}" x:Key="changeKeyDialog">
18+
<materialDesign:DialogHost.DialogContent>
19+
<libui:OpenFavoriteControl Title="{x:Static properties:Resources.ResetEncryptionKey}" Command="{Binding Path=DataContext.ResetEncryptionKeyCommand, Source={x:Reference toolsControl}}" />
20+
</materialDesign:DialogHost.DialogContent>
21+
22+
<Button Margin="5" Height="64" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" IsEnabled="{x:Static libui:UIPreferences.IsUserElevated}">
23+
<Button.Content>
24+
<WrapPanel>
25+
<materialDesign:PackIcon Kind="EncryptionReset" Width="32" Height="32" />
26+
<TextBlock Text="{x:Static properties:Resources.ResetEncryptionKey}" VerticalAlignment="Center" Margin="3" />
27+
</WrapPanel>
28+
</Button.Content>
29+
</Button>
30+
</materialDesign:DialogHost>
31+
</UserControl.Resources>
32+
<StackPanel Orientation="Vertical">
33+
<Expander HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Header="{x:Static properties:Resources.ImportExport}" IsExpanded="False">
34+
<Grid>
35+
<Grid.ColumnDefinitions>
36+
<ColumnDefinition />
37+
<ColumnDefinition />
38+
</Grid.ColumnDefinitions>
39+
<Button Grid.Column="0" Margin="5" Height="64" Command="{Binding ImportCommand}" IsEnabled="{x:Static libui:UIPreferences.IsUserElevated}">
40+
<Button.Content>
41+
<WrapPanel>
42+
<materialDesign:PackIcon Kind="Import" Width="32" Height="32" />
43+
<TextBlock Text="{x:Static properties:Resources.Import}" VerticalAlignment="Center" Margin="3" />
44+
</WrapPanel>
45+
</Button.Content>
46+
</Button>
47+
<Button Grid.Column="1" Margin="5" Height="64" Command="{Binding ExportCommand}" IsEnabled="{x:Static libui:UIPreferences.IsUserElevated}">
48+
<Button.Content>
49+
<WrapPanel>
50+
<materialDesign:PackIcon Kind="Export" Width="32" Height="32" />
51+
<TextBlock Text="{x:Static properties:Resources.Export}" VerticalAlignment="Center" Margin="3" />
52+
</WrapPanel>
53+
</Button.Content>
54+
</Button>
55+
</Grid>
56+
</Expander>
57+
<Expander HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Header="{x:Static properties:Resources.EncryptionKey}" IsExpanded="False">
58+
<StaticResource ResourceKey="changeKeyDialog" />
59+
</Expander>
60+
</StackPanel>
61+
</UserControl>

KeyManager.Library.KeyStore.File.UI/FileKeyStoreImportExportControl.xaml.cs renamed to KeyManager.Library.KeyStore.File.UI/FileKeyStoreToolsControl.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace Leosac.KeyManager.Library.KeyStore.File.UI
66
/// <summary>
77
/// Interaction logic for FileKeyStoreImportExportControl.xaml
88
/// </summary>
9-
public partial class FileKeyStoreImportExportControl : UserControl
9+
public partial class FileKeyStoreToolsControl : UserControl
1010
{
11-
public FileKeyStoreImportExportControl()
11+
public FileKeyStoreToolsControl()
1212
{
13-
InitializeComponent();
13+
DataContext = new FileKeyStoreToolsControlViewModel();
1414

15-
DataContext = new FileKeyStoreImportExportControlViewModel();
15+
InitializeComponent();
1616
}
1717
}
1818
}

KeyManager.Library.KeyStore.File.UI/FileKeyStoreUIFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override IDictionary<string, UserControl> CreateKeyStoreAdditionalControl
3333
{
3434
return new Dictionary<string, UserControl>
3535
{
36-
{ Properties.Resources.ImportExport, new FileKeyStoreImportExportControl() }
36+
{ Properties.Resources.Tools, new FileKeyStoreToolsControl() }
3737
};
3838
}
3939
}

KeyManager.Library.KeyStore.File.UI/KeyManager.Library.KeyStore.File.UI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<AutoGen>True</AutoGen>
4242
<DependentUpon>Resources.resx</DependentUpon>
4343
</Compile>
44-
<Compile Update="FileKeyStoreImportExportControl.xaml.cs">
44+
<Compile Update="FileKeyStoreToolsControl.xaml.cs">
4545
<SubType>Code</SubType>
4646
</Compile>
4747
</ItemGroup>

0 commit comments

Comments
 (0)