Files
DodoSSH/src/DodoSSH.Client.Sync/VaultKeyring.cs
T
jaap-jan e9cea2ccbc Let a shared vault arrive, a bucket be found, and a vault be deleted
Three things a user reported, one of which was a real bug and one of which was
not the bug it looked like.

**A vault shared with somebody never reached their machine.** The grant was
correct at both ends: the sharing client verified the recipient's key against the
key log and wrapped every generation to it, the server stored it, and /me would
have returned it. Nothing asked. VaultSession.RefreshVaultsAsync — the method
whose own summary says it is "called after a share and on a periodic pass" — had
no caller anywhere in the application, so the vault list was whatever the last
browser sign-in cached. A restart did not help: an offline unlock reads that same
cache. The vault appeared only if the recipient happened to sign in through the
browser again, which is why this looked like sharing being broken rather than
like a list that was never re-read.

So every synchronisation pass now re-reads it, before it syncs. SyncOnceAsync
takes the whole server rather than its sync half for that reason, and the order
matters: a vault admitted by the refresh is one that same pass then pulls, where
the other order would show a newly shared vault as an empty one until the minute
after. The shell is told only when the set actually changed — it rebuilds the tab
strip's vault menu from the session's list, and doing that on every quiet pass
would rebuild a menu once a minute for nothing.

The test needed the fake server to be able to do something no test here had
needed before: hand this account a vault it did not make. ShareVaultWithMe wraps
a real key to the encryption key this account enrolled, so the keyring opens it
exactly as it opens a real colleague's — a helper that filled the field with
bytes would let a vault appear in the list and never prove it could be read.

**Adding an S3 bucket on the desktop works, and could not be found.** The report
was that it is not possible; driving the real XAML headlessly says otherwise —
Keychain, + BUCKET, and the editor saves. What is true is that S3 is where
somebody goes looking, and from there SELECT BUCKET opened a combo box with
nothing in it and no sentence anywhere saying that a bucket is a keychain item.
From where the user was standing that is indistinguishable from an application
with no way to add one.

The empty state now says what a bucket is and offers a button that lands on the
keychain with the editor already open — navigating to the screen and leaving
+ BUCKET to be found among five buttons would be most of the same problem. The
phone gets the sentence and no button: its keychain screen reads and deletes and
edits nothing, so there is no editor to send anybody to, and naming the machine
that has one beats an empty control that reads as a screen still loading.

The keychain screen's layout test grew the two categories it never covered.
Tags and buckets arrived after it was written, and the header strip it measures
is one that has overflowed twice before.

**A vault can now be deleted.** DELETE /api/v1/vaults/{id}, gated on Admin —
the line the rename already drew, for a stronger version of its reason, since
this takes the vault from everybody in it at once. The row is soft-deleted and
every grant to it withdrawn in one write; VaultAccessService filters on the stamp
at both ends, so from that moment the vault is absent from every member's /me and
every call naming it answers 404. Their clients notice on the pass described
above.

The team behind it is archived when it owned nothing else, which is the mirror of
renaming it: a vault made from the vaults screen gets a team named after it that
nobody was ever shown, and leaving that behind would leave a membership list no
screen has a row for. That is a second call rather than one transaction —
archiving is TeamService's, it refuses while a team owns vaults, and it can only
tell that this one no longer does once the deletion is committed. A crash between
the two leaves an empty team: invisible, archivable afterwards, harmless, and a
better failure than a vault that could not be deleted because tidying up after it
did not work.

Two refusals worth stating. The personal vault cannot be deleted at either end:
it is created by enrollment, everything filed nowhere else lives in it, and no
call would make another. And the items are kept — ciphertext behind a vault
nothing will resolve, so deleting them buys no confidentiality while destroying
what an operator undoing a mistake would need.

The client drops the key from the keyring and the row from the cache rather than
waiting for a refresh, so the list is right immediately; the items stay, as they
stay for a vault whose grant was withdrawn, because a copy is on every other
member's machine too and removing these rows would be the client pretending to a
reach it does not have. The confirmation says that out loud before it is
answered. It is the one sentence this screen must not leave implied: deletion is
no more retroactive than revocation is. See ADR 0001.

Desktop only, deliberately. The Android vaults screen offers no rename and no
hand-over either, so adding delete alone there would be the one destructive vault
operation on a screen with no other.

Three places asserted that a vault can never be deleted — TeamService's refusal
message, the TeamNotEmpty problem code, and ADR 0009 — and each now names the
route instead.
2026-08-04 15:34:40 +02:00

435 lines
17 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using DodoSSH.Client.Storage;
using DodoSSH.Crypto;
namespace DodoSSH.Client.Sync;
/// <summary>
/// The vault keys held for the duration of an unlocked session.
/// </summary>
/// <remarks>
/// <para>
/// One place that holds plaintext vault keys, so there is one place that clears them. Every store and
/// every cipher call borrows a key from here rather than keeping a copy, which is what makes
/// "the keys exist only while unlocked" a property of the code and not of everyone's discipline.
/// </para>
/// <para>
/// <b>A vault has a key per generation, and this holds every one it was granted.</b> A rotation does not
/// re-encrypt what is already stored — each item keeps the generation it was sealed under — so reading a
/// rotated vault means opening items under two or three different keys, chosen per item rather than per
/// vault. Writing uses the newest, which is what <see cref="TryGet"/> answers; reading an item asks for
/// the generation that item names, which is <see cref="TryGetAt"/>.
/// </para>
/// <para>
/// A grant that will not open is not an error: it means the vault has been rekeyed and this client's
/// grant has not been re-wrapped yet, or the grant was fabricated. Both leave the vault temporarily
/// unreadable and both are reported rather than thrown, so one bad grant does not take the other vaults
/// down with it.
/// </para>
/// </remarks>
public sealed class VaultKeyring : IDisposable
{
private readonly Dictionary<Guid, Dictionary<uint, byte[]>> keys = [];
private readonly Dictionary<Guid, uint> generations = [];
private bool disposed;
private VaultKeyring()
{
}
/// <summary>Vaults whose grant could not be opened, and which are therefore unreadable.</summary>
public IReadOnlyList<Guid> Unopened { get; private set; } = [];
/// <summary>
/// Opens every grant the bundle can.
/// </summary>
/// <param name="bundle">The unlocked identity keys.</param>
/// <param name="vaults">The cached vault list, each with its wrapped key.</param>
public static VaultKeyring Open(UserSecretBundle bundle, IReadOnlyList<StoredVault> vaults)
{
ArgumentNullException.ThrowIfNull(bundle);
ArgumentNullException.ThrowIfNull(vaults);
var keyring = new VaultKeyring();
var unopened = new List<Guid>();
try
{
foreach (var vault in vaults)
{
// The history first, and never conditional on the current generation opening. A member
// who has been rotated past but not yet re-wrapped can still read everything written
// before the rotation, and dropping those keys because the newest grant is missing
// would turn "you cannot see the last hour's changes" into "the vault is empty".
keyring.OpenPriorWraps(bundle, vault);
if (vault.WrappedVaultKey is null)
{
// The server said so itself: a grant awaiting re-wrap after a rekey.
unopened.Add(vault.VaultId);
continue;
}
var key = VaultKeys.TryUnwrap(
bundle.EncryptionKey,
vault.WrappedVaultKey,
vault.VaultId,
vault.KeyGeneration);
if (key is null)
{
unopened.Add(vault.VaultId);
continue;
}
keyring.Adopt(vault.VaultId, key, vault.KeyGeneration);
}
keyring.Unopened = unopened;
return keyring;
}
catch
{
keyring.Dispose();
throw;
}
}
/// <summary>
/// Takes a vault key this session has just generated.
/// </summary>
/// <param name="vaultId">The vault.</param>
/// <param name="vaultKey">
/// The plaintext key. <b>The keyring takes ownership</b> and zeroes it on disposal; the caller must
/// not keep a reference or zero it itself.
/// </param>
/// <param name="keyGeneration">The generation this key is for.</param>
/// <remarks>
/// <para>
/// Two cases, and they are the same operation: creating a team vault, and rotating one. Both
/// generate the key here, wrap it to this user and send the wrap, so the plaintext is already in
/// this process and unwrapping the server's copy back would be a round trip to learn something it
/// just chose. Adopting it also means the vault is usable immediately rather than at the next
/// unlock, which is what somebody who has just pressed a button expects.
/// </para>
/// <para>
/// This generation becomes the one writes are sealed under. A key for a generation the vault has
/// moved <em>past</em> goes in through <see cref="AdoptPrior"/> instead, which is not the same
/// operation: it makes old items readable and must not walk the write target backwards.
/// </para>
/// </remarks>
public void Adopt(Guid vaultId, byte[] vaultKey, uint keyGeneration)
{
ObjectDisposedException.ThrowIf(disposed, this);
ArgumentNullException.ThrowIfNull(vaultKey);
Store(vaultId, vaultKey, keyGeneration);
Promote(vaultId, keyGeneration);
}
/// <summary>
/// Takes a vault key for a generation the vault has already moved past.
/// </summary>
/// <param name="vaultId">The vault.</param>
/// <param name="vaultKey">
/// The plaintext key. <b>The keyring takes ownership</b>, exactly as <see cref="Adopt"/> does.
/// </param>
/// <param name="keyGeneration">The superseded generation this key opens.</param>
/// <remarks>
/// Holding one of these is what lets a rotated vault be read at all: items are not re-encrypted by a
/// rotation, so everything written before it is still sealed under the key it was written with.
/// Nothing is ever <em>written</em> under one, which is why this does not touch the current
/// generation and does not make an otherwise unreadable vault readable.
/// </remarks>
public void AdoptPrior(Guid vaultId, byte[] vaultKey, uint keyGeneration)
{
ObjectDisposedException.ThrowIf(disposed, this);
ArgumentNullException.ThrowIfNull(vaultKey);
Store(vaultId, vaultKey, keyGeneration);
}
/// <summary>
/// Tries to open a vault that has become readable since the session was unlocked.
/// </summary>
/// <returns>Whether the grant opened.</returns>
/// <remarks>
/// What a share looks like from the receiving end: the vault was in the list all along, listed and
/// unreadable, and a member holding Share has now wrapped its key. Re-opening it here rather than
/// waiting for a relock is the difference between "someone shared a vault with you" arriving and
/// arriving tomorrow.
/// </remarks>
public bool TryAdmit(UserSecretBundle bundle, StoredVault vault)
{
ObjectDisposedException.ThrowIf(disposed, this);
ArgumentNullException.ThrowIfNull(bundle);
ArgumentNullException.ThrowIfNull(vault);
// Attempted whatever happens to the current generation, and before it. A share of a vault that
// has been rotated since it was created arrives as a current wrap plus its history, and the
// history is not a consolation prize — without it the recipient sees a vault full of items that
// will not decrypt.
OpenPriorWraps(bundle, vault);
if (vault.WrappedVaultKey is null)
{
return false;
}
if (Held(vault.VaultId, vault.KeyGeneration) is not null)
{
// Already open at this generation. Promoted rather than returned early, because a vault
// that was rotated and re-granted arrives here with a generation this keyring has been
// treating as historic, and it is now the one writes belong under.
Promote(vault.VaultId, vault.KeyGeneration);
return true;
}
var key = VaultKeys.TryUnwrap(
bundle.EncryptionKey, vault.WrappedVaultKey, vault.VaultId, vault.KeyGeneration);
if (key is null)
{
return false;
}
Adopt(vault.VaultId, key, vault.KeyGeneration);
return true;
}
/// <summary>Records that a vault cannot be read, so the interface can say so.</summary>
/// <remarks>
/// <para>
/// The counterpart of <see cref="TryAdmit"/> for the case where the grant did not open. Kept
/// explicit rather than inferred from the absence of a key, because "no key" is also what a vault
/// this session has never heard of looks like.
/// </para>
/// <para>
/// <b>It also gives up the write target, and that is the load-bearing half.</b> The usual way to
/// reach here is another client having rotated the vault: this session still holds the previous
/// generation's key and it is no longer the current one. Going on treating it as current would seal
/// new items under a superseded key — readable here, unreadable to everybody else, and with nothing
/// to show the author that anything was wrong. The keys themselves are kept, because the items
/// already written under them are still readable through <see cref="TryGetAt"/>.
/// </para>
/// </remarks>
public void MarkUnreadable(Guid vaultId)
{
ObjectDisposedException.ThrowIf(disposed, this);
generations.Remove(vaultId);
if (!Unopened.Contains(vaultId))
{
Unopened = [.. Unopened, vaultId];
}
}
/// <summary>
/// Drops every key to one vault, zeroing them.
/// </summary>
/// <remarks>
/// <para>
/// For a vault that has been deleted, which is the one case where this session is not merely unable to
/// read a vault but has no vault to read. <see cref="MarkUnreadable"/> is the other shape and keeps the
/// keys deliberately — the items sealed under them are still there and still wanted — whereas here
/// there is nothing left for them to open and holding them would be keeping key material for something
/// that no longer exists.
/// </para>
/// <para>
/// It is also taken off the unopened list, which is what stops a deleted vault being reported for ever
/// as one this machine is waiting for a key to.
/// </para>
/// </remarks>
public void Forget(Guid vaultId)
{
ObjectDisposedException.ThrowIf(disposed, this);
generations.Remove(vaultId);
Unopened = [.. Unopened.Where(id => id != vaultId)];
if (!keys.Remove(vaultId, out var held))
{
return;
}
foreach (var key in held.Values)
{
CryptographicOperations.ZeroMemory(key);
}
}
/// <summary>
/// Borrows a vault's current key: the one new items are sealed under.
/// </summary>
/// <remarks>
/// The returned memory is the keyring's own buffer, not a copy, and is zeroed when the keyring is
/// disposed. Callers must not retain it past the operation they borrowed it for.
/// <para>
/// False for a vault this session holds only the history of — one rotated past a grant that has not
/// been re-wrapped yet. That is deliberate: writing under a superseded key would produce an item
/// nobody else could read, and the honest answer is that the vault is not writable until the new
/// key arrives.
/// </para>
/// </remarks>
public bool TryGet(Guid vaultId, out ReadOnlyMemory<byte> vaultKey, out uint keyGeneration)
{
ObjectDisposedException.ThrowIf(disposed, this);
if (generations.TryGetValue(vaultId, out var current)
&& Held(vaultId, current) is { } key)
{
vaultKey = key;
keyGeneration = current;
return true;
}
vaultKey = default;
keyGeneration = 0;
return false;
}
/// <summary>
/// Borrows the key one particular generation of a vault was sealed under.
/// </summary>
/// <param name="vaultId">The vault.</param>
/// <param name="keyGeneration">The generation the item names.</param>
/// <param name="vaultKey">The key, borrowed on the same terms as <see cref="TryGet"/>.</param>
/// <returns>Whether this session holds that generation.</returns>
/// <remarks>
/// What every read goes through, because an item names the generation it was sealed under and a
/// rotated vault holds items from more than one. False means that item is unreadable here and says
/// nothing about the rest of the vault — which is why a caller counts it rather than failing.
/// </remarks>
public bool TryGetAt(Guid vaultId, uint keyGeneration, out ReadOnlyMemory<byte> vaultKey)
{
ObjectDisposedException.ThrowIf(disposed, this);
if (Held(vaultId, keyGeneration) is { } key)
{
vaultKey = key;
return true;
}
vaultKey = default;
return false;
}
/// <summary>
/// Every generation of one vault's key that this session holds, oldest first.
/// </summary>
/// <remarks>
/// Read when sharing: a recipient given only the newest key would find the vault's history
/// undecryptable, so the sharing client wraps each of these in turn. It is the only party that can
/// — the server holds ciphertext, and the recipient holds nothing yet.
/// </remarks>
public IReadOnlyList<uint> GenerationsHeld(Guid vaultId)
{
ObjectDisposedException.ThrowIf(disposed, this);
return keys.TryGetValue(vaultId, out var held) ? [.. held.Keys.Order()] : [];
}
/// <summary>Whether this vault can be read and written at its current generation.</summary>
public bool CanRead(Guid vaultId) =>
!disposed
&& generations.TryGetValue(vaultId, out var current)
&& Held(vaultId, current) is not null;
/// <inheritdoc />
public void Dispose()
{
if (disposed)
{
return;
}
disposed = true;
foreach (var held in keys.Values)
{
foreach (var key in held.Values)
{
CryptographicOperations.ZeroMemory(key);
}
}
keys.Clear();
generations.Clear();
}
/// <summary>Opens whatever superseded generations this vault came with.</summary>
/// <remarks>
/// A wrap that will not open is skipped rather than reported. It means one historic grant is
/// unusable — the items under that generation stay unreadable and are counted as such where they
/// are listed — and it is not a reason to refuse the generations that did open.
/// </remarks>
private void OpenPriorWraps(UserSecretBundle bundle, StoredVault vault)
{
foreach (var wrap in vault.PriorKeyWraps ?? [])
{
if (Held(vault.VaultId, wrap.KeyGeneration) is not null)
{
continue;
}
var key = VaultKeys.TryUnwrap(
bundle.EncryptionKey, wrap.WrappedKey, vault.VaultId, wrap.KeyGeneration);
if (key is not null)
{
AdoptPrior(vault.VaultId, key, wrap.KeyGeneration);
}
}
}
private byte[]? Held(Guid vaultId, uint keyGeneration) =>
keys.TryGetValue(vaultId, out var held) && held.TryGetValue(keyGeneration, out var key)
? key
: null;
private void Store(Guid vaultId, byte[] vaultKey, uint keyGeneration)
{
if (!keys.TryGetValue(vaultId, out var held))
{
held = [];
keys[vaultId] = held;
}
if (held.TryGetValue(keyGeneration, out var previous))
{
CryptographicOperations.ZeroMemory(previous);
}
held[keyGeneration] = vaultKey;
}
private void Promote(Guid vaultId, uint keyGeneration)
{
generations[vaultId] = keyGeneration;
Unopened = [.. Unopened.Where(id => id != vaultId)];
}
}
/// <summary>Thrown when an operation needs a vault key the keyring does not hold.</summary>
/// <remarks>
/// An exception rather than a silent no-op, because every caller that reaches this point has already
/// been given the chance to check <see cref="VaultKeyring.CanRead"/>. Continuing without the key would
/// mean writing an item nobody can open.
/// </remarks>
[SuppressMessage(
"Design",
"CA1032:Implement standard exception constructors",
Justification = "The vault id is required context; a message-only constructor would lose it.")]
public sealed class VaultUnreadableException(Guid vaultId)
: InvalidOperationException(
$"Vault {vaultId} has no usable key. Its grant is missing or awaiting re-wrap after a rekey.")
{
/// <summary>The vault that cannot be read.</summary>
public Guid VaultId { get; } = vaultId;
}