using System.Security.Cryptography;
using DodoSSH.Client.Domain;
using DodoSSH.Contracts;
using DodoSSH.Crypto;
namespace DodoSSH.Client.Sync;
///
/// Turns a host group into an item payload and back.
///
///
///
/// Mirrors exactly, including the rule that a payload is sealed at the
/// version the server will assign rather than the one it replaces — see .
///
///
/// The resource type is the one thing not to copy. SyncEntityType.HostGroup is 4 and
/// CryptoSpec.AadResourceType.HostGroup is 7, because the crypto enum carries None, User, Device and
/// Vault ahead of the item types. Casting one to the other would seal a group under the resource type for a
/// host — which encrypts perfectly, decrypts perfectly on the machine that wrote it, and is a
/// specification violation nothing would notice until an interoperating client refused the item.
///
///
public static class HostGroupCipher
{
private const CryptoSpec.AadResourceType Resource = CryptoSpec.AadResourceType.HostGroup;
/// Encrypts a group.
/// The group. Must be valid for storage.
/// The vault key, which the data key is wrapped under.
/// The item id, which the AAD binds.
/// The vault's current key generation.
/// The version this payload will hold once the server accepts it.
public static EncryptedPayload Seal(
HostGroupSecret group,
ReadOnlySpan vaultKey,
Guid entityId,
uint keyGeneration,
int itemVersion)
{
ArgumentNullException.ThrowIfNull(group);
ArgumentOutOfRangeException.ThrowIfLessThan(itemVersion, 1);
var plaintext = HostGroupSecretCodec.Encode(group);
var dataKey = ItemKeys.CreateDataKey();
try
{
var dataKeyId = Guid.CreateVersion7();
var wrappedDataKey = ItemKeys.WrapDataKey(
dataKey, vaultKey, Resource, entityId, keyGeneration, (uint)itemVersion);
var envelope = ItemKeys.SealPayload(
dataKey, plaintext, Resource, entityId, dataKeyId, keyGeneration, (uint)itemVersion);
return new EncryptedPayload(
envelope, wrappedDataKey, dataKeyId, keyGeneration, CryptoSpec.CurrentAadVersion);
}
finally
{
CryptographicOperations.ZeroMemory(dataKey);
// Wiped like every other payload in this folder, and here for the smallest reason of all: the
// buffer holds one name somebody chose for a folder. It is wiped anyway, because the rule this
// folder follows is that plaintext does not outlive the call that made it, and an exception for
// the case that seems harmless is how the rule stops being one.
CryptographicOperations.ZeroMemory(plaintext);
}
}
/// Decrypts a group.
///
public static HostGroupSecretDocument? TryOpen(
EncryptedPayload payload,
ReadOnlySpan vaultKey,
Guid entityId,
int itemVersion)
{
ArgumentNullException.ThrowIfNull(payload);
if (itemVersion < 1 || payload.WrappedDataKey.Length == 0)
{
return null;
}
var dataKey = ItemKeys.TryUnwrapDataKey(
vaultKey,
payload.WrappedDataKey,
Resource,
entityId,
payload.KeyGeneration,
(uint)itemVersion);
if (dataKey is null)
{
return null;
}
try
{
var plaintext = ItemKeys.TryOpenPayload(
dataKey,
payload.Envelope,
Resource,
entityId,
payload.DataKeyId,
payload.KeyGeneration,
(uint)itemVersion);
if (plaintext is null)
{
return null;
}
try
{
return HostGroupSecretCodec.TryDecode(plaintext, out var document) ? document : null;
}
finally
{
CryptographicOperations.ZeroMemory(plaintext);
}
}
finally
{
CryptographicOperations.ZeroMemory(dataKey);
}
}
}