-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCreationRoutine.cs
More file actions
99 lines (85 loc) · 3.63 KB
/
CreationRoutine.cs
File metadata and controls
99 lines (85 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using OwlCore.Storage;
using SecureFolderFS.Core.Cryptography.SecureStore;
using SecureFolderFS.Core.DataModels;
using SecureFolderFS.Core.Models;
using SecureFolderFS.Core.VaultAccess;
using SecureFolderFS.Shared.Models;
using System;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using SecureFolderFS.Shared.ComponentModel;
using static SecureFolderFS.Core.Constants.Vault;
using static SecureFolderFS.Core.Cryptography.Constants;
namespace SecureFolderFS.Core.Routines.Operational
{
/// <inheritdoc cref="ICreationRoutine"/>
internal sealed class CreationRoutine : ICreationRoutine
{
private readonly IFolder _vaultFolder;
private readonly VaultWriter _vaultWriter;
private V4VaultKeystoreDataModel? _keystoreDataModel;
private V4VaultConfigurationDataModel? _configDataModel;
private IKeyUsage? _dekKey;
private IKeyUsage? _macKey;
public CreationRoutine(IFolder vaultFolder, VaultWriter vaultWriter)
{
_vaultFolder = vaultFolder;
_vaultWriter = vaultWriter;
}
/// <inheritdoc/>
public Task InitAsync(CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
/// <inheritdoc/>
public void SetCredentials(IKeyUsage passkey)
{
// Allocate keys for later use
var dekKey = new byte[KeyTraits.DEK_KEY_LENGTH];
var macKey = new byte[KeyTraits.MAC_KEY_LENGTH];
var salt = new byte[KeyTraits.SALT_LENGTH];
// Fill keys and salt
RandomNumberGenerator.Fill(dekKey);
RandomNumberGenerator.Fill(macKey);
RandomNumberGenerator.Fill(salt);
// Generate V4 keystore
_keystoreDataModel = passkey.UseKey(key => VaultParser.V4EncryptKeystore(key, dekKey, macKey, salt));
// Create key copies for later use
_dekKey = SecureKey.TakeOwnership(dekKey);
_macKey = SecureKey.TakeOwnership(macKey);
}
/// <inheritdoc/>
public void SetOptions(VaultOptions vaultOptions)
{
_configDataModel = V4VaultConfigurationDataModel.V4FromVaultOptions(vaultOptions);
}
/// <inheritdoc/>
public async Task<IDisposable> FinalizeAsync(CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(_keystoreDataModel);
ArgumentNullException.ThrowIfNull(_configDataModel);
ArgumentNullException.ThrowIfNull(_macKey);
ArgumentNullException.ThrowIfNull(_dekKey);
// First, we need to fill in the PayloadMac of the content
_macKey.UseKey(macKey =>
{
VaultParser.V4CalculateConfigMac(_configDataModel, macKey, _configDataModel.PayloadMac);
});
// Write the whole configuration
await _vaultWriter.WriteKeystoreAsync(_keystoreDataModel, cancellationToken);
await _vaultWriter.WriteV4ConfigurationAsync(_configDataModel, cancellationToken);
// Create the content folder
if (_vaultFolder is IModifiableFolder modifiableFolder)
await modifiableFolder.CreateFolderAsync(Names.VAULT_CONTENT_FOLDERNAME, true, cancellationToken);
// Key copies need to be created because the original ones are disposed of here
return new SecurityWrapper(KeyPair.ImportKeys(_dekKey, _macKey), _configDataModel);
}
/// <inheritdoc/>
public void Dispose()
{
_dekKey?.Dispose();
_macKey?.Dispose();
}
}
}