using System.Security.Cryptography;
using DodoSSH.Client.Domain;
using DodoSSH.Contracts;
using DodoSSH.Crypto;
namespace DodoSSH.Client.Sync;
///
/// Turns a snippet 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.Snippet is 8 and
/// CryptoSpec.AadResourceType.Snippet is 9 — a difference of one, which is the most dangerous kind,
/// because a cast that is wrong by one still produces a defined value and seals the item under the resource
/// type for a tag. That round-trips on the machine that wrote it and violates docs/crypto.md
/// everywhere else.
///
///
public static class SnippetCipher
{
private const CryptoSpec.AadResourceType Resource = CryptoSpec.AadResourceType.Snippet;
/// Encrypts a snippet.
/// The snippet. 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(
SnippetSecret snippet,
ReadOnlySpan vaultKey,
Guid entityId,
uint keyGeneration,
int itemVersion)
{
ArgumentNullException.ThrowIfNull(snippet);
ArgumentOutOfRangeException.ThrowIfLessThan(itemVersion, 1);
var plaintext = SnippetSecretCodec.Encode(snippet);
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);
// A snippet is not a credential, and it is closer to one than it looks: people paste tokens into
// commands, and the command that unlocks a service is worth as much as the password it carries.
CryptographicOperations.ZeroMemory(plaintext);
}
}
/// Decrypts a snippet.
///
public static SnippetSecretDocument? 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 SnippetSecretCodec.TryDecode(plaintext, out var document) ? document : null;
}
finally
{
CryptographicOperations.ZeroMemory(plaintext);
}
}
finally
{
CryptographicOperations.ZeroMemory(dataKey);
}
}
}