Public Access
Settings → Security gains a SPELUNK section: an API address and a key, saved as a new keychain item, a Spelunk key (SyncEntityType 14, AadResourceType 17, server table spelunk_key via the AddSpelunkKeyItem migration). It is ciphertext only on the server, lives in the personal vault, syncs like every secret, and holds the key together with the one address it may be sent to. The key box empties once saved; the list shows name, address and how many hosts use each key, never the key. Saving a key for an address that already has one replaces it, and deleting goes through the shared confirmation card with the hosts that would stop connecting. A host's route names a saved key where it named a credential, in the same apiKeyCredentialId field, so no host schema changes and an older build reads such a host as one whose key credential has gone. At connect a saved key is sent only to its own address; a host naming it with another Spelunk address is refused in words. Keys kept as a credential's password still work. The editor offers saved keys first and picks the one saved for an address as it is typed. A link from a Spelunk with a saved key now connects straight away, including the first one: saving the key is the user's say-so that the first-link editor used to ask for. The address and key merge as one pair, so two concurrent edits can never pair one device's key with another's address, and the key never reaches the conflict log.
126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using System.Security.Cryptography;
|
|
using DodoSSH.Client.Domain;
|
|
using DodoSSH.Contracts;
|
|
using DodoSSH.Crypto;
|
|
|
|
namespace DodoSSH.Client.Sync;
|
|
|
|
/// <summary>
|
|
/// Turns a Spelunk key into an item payload and back.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// Mirrors <see cref="ObjectStoreCipher"/> exactly, including the version rule in <see cref="SyncVersions"/>.
|
|
/// </para>
|
|
/// <para>
|
|
/// <b>The resource type is named, never cast.</b> <c>SyncEntityType.SpelunkKey</c> is 14 and
|
|
/// <c>CryptoSpec.AadResourceType.SpelunkKey</c> is 17; 14 in the crypto enum is a connection log entry, and a
|
|
/// cast would seal an API key under that — which decrypts perfectly on the machine that wrote it and violates
|
|
/// docs/crypto.md everywhere else.
|
|
/// </para>
|
|
/// </remarks>
|
|
public static class SpelunkKeyCipher
|
|
{
|
|
private const CryptoSpec.AadResourceType Resource = CryptoSpec.AadResourceType.SpelunkKey;
|
|
|
|
/// <summary>Encrypts a Spelunk key.</summary>
|
|
/// <param name="key">The key. Must be valid for storage.</param>
|
|
/// <param name="vaultKey">The vault key, which the data key is wrapped under.</param>
|
|
/// <param name="entityId">The item id, which the AAD binds.</param>
|
|
/// <param name="keyGeneration">The vault's current key generation.</param>
|
|
/// <param name="itemVersion">The version this payload will hold once the server accepts it.</param>
|
|
public static EncryptedPayload Seal(
|
|
SpelunkKeySecret key,
|
|
ReadOnlySpan<byte> vaultKey,
|
|
Guid entityId,
|
|
uint keyGeneration,
|
|
int itemVersion)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(key);
|
|
ArgumentOutOfRangeException.ThrowIfLessThan(itemVersion, 1);
|
|
|
|
var plaintext = SpelunkKeySecretCodec.Encode(key);
|
|
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);
|
|
|
|
// It holds an API key, which is live on the Spelunk it belongs to.
|
|
CryptographicOperations.ZeroMemory(plaintext);
|
|
}
|
|
}
|
|
|
|
/// <summary>Decrypts a Spelunk key.</summary>
|
|
/// <inheritdoc cref="HostCipher.TryOpen" path="/returns" />
|
|
public static SpelunkKeySecretDocument? TryOpen(
|
|
EncryptedPayload payload,
|
|
ReadOnlySpan<byte> 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 SpelunkKeySecretCodec.TryDecode(plaintext, out var document) ? document : null;
|
|
}
|
|
finally
|
|
{
|
|
CryptographicOperations.ZeroMemory(plaintext);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
CryptographicOperations.ZeroMemory(dataKey);
|
|
}
|
|
}
|
|
}
|