Public Access
The last of ADR 0007's three pieces, and it does not implement what that ADR originally decided — because writing it exposed a flaw in the decision. The ADR said "a Windows Hello gesture gating a protected blob". That does not deliver what the rest of the document claims for it: a gate inside the process is not a gate. A store that showed a prompt and then read a DPAPI blob would be bypassed by malware that skipped the prompt, read the file and called CryptUnprotectData itself — which is exactly the attacker the whole decision was made against, and exactly the reason DPAPI alone was rejected. The presence requirement has to be a condition of using the key, enforced below the application, or it is decoration. So the device key is encrypted to an RSA key created in the Microsoft Platform Crypto Provider — the TPM — under CngUIProtectionLevels.ProtectKey. Windows requires consent to use that key, so the prompt is not something this code can be talked out of showing. Malware can ask for the key; it cannot answer the dialog. That is strictly stronger than the ADR described, and most of what option D was being saved for: the wrapping key genuinely never leaves hardware. The X25519 device key still lands in memory to open the wrap, because DSH1 fixes that wrap at a curve the TPM cannot do — the remaining gap, and now a smaller step than it was. CngKey is in-box, so this needed no WinRT projection and no Windows target framework. Which is worth stating plainly because the opposite was planned: the piece was scoped as "where the Windows TFM lands", and it turned out a platform guard on one class was enough. Client.App and its two test projects stay on net10.0. Two things were measured on real hardware rather than assumed, and the second changed the shape of the work. The platform provider works here and holds an RSA key — confirmed by creating and deleting one before writing anything that depended on it. And ProtectKey prompts at key *creation*, not only at use. The comment in the first draft of this file said the opposite, with a confident explanation: sealing uses only the public half, so it should be silent. It is not. CngKey.Create blocks on a dialog, because the policy means "protect this key with a PIN" and Windows asks the user to set that up there and then. Found by writing tests around save and forget and watching the suite hang for ten minutes waiting for somebody to type one. That has two consequences worth knowing before touching this file. SaveAsync is user-facing code — it belongs on a UI thread, behind a button somebody pressed, never on a background pass. And almost nothing in the store can be covered automatically: two tests remain, availability and the empty-blob case, both of which provably reach no dialog. Disabling the UI policy to make the rest testable would remove the one property worth having. The interface offers two things and hides both where they cannot work. "Use Windows Hello" appears on the unlock screen only when this machine has a cached wrap and a keystore still willing to release the key; "Use Windows Hello here" appears in the account bar only when the machine can keep a key and has not already registered one, so it is spent once used. Absent rather than disabled, in both cases: a greyed-out button on a machine that never had a TPM reads as something broken, and the passphrase box beside it is not a fallback — it is the ordinary way in. Both unlock paths now share AdoptAsync rather than each opening the known-host store, building the vault and starting auto-sync. The ordering in there is load-bearing and a second copy would be a second chance to get it wrong. The shell's tests drive a fake keystore. Not for speed: the real one prompts on every save and load, so a suite using it would block forever. What the shell has to get right is which buttons appear and what happens when one is pressed, and a fake answers exactly that. It is shared from Client.Session.Tests by source link rather than reimplemented. 882 tests green, 6 of them new. Zero warnings, dotnet format clean. Not verified, and not verifiable here: the dialogs. Whether the consent prompt appears at the right moments, reads sensibly, and returns to a usable window when declined needs the application run by a person on a machine with a TPM. That is the remaining half of outstanding item #7, and it is now the only thing between this feature and being finished.
86 lines
3.9 KiB
C#
86 lines
3.9 KiB
C#
namespace DodoSSH.Client.Session;
|
|
|
|
/// <summary>
|
|
/// Where this machine keeps its profile.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// A record with an explicit directory rather than a static lookup, so a test — or a portable install —
|
|
/// can point it somewhere else without an environment variable.
|
|
/// </para>
|
|
/// <para>
|
|
/// <b>The choice of directory matters more than it looks.</b> The cache is a SQLite file written by one
|
|
/// process, and the whole design assumes each machine has its own: the outbox holds changes this machine
|
|
/// has made and not yet pushed, and two machines sharing one file through a cloud sync client corrupts
|
|
/// it. So this deliberately picks a <em>local</em>, non-roaming location on every platform. On Windows
|
|
/// that means <c>%LOCALAPPDATA%</c> and never <c>%APPDATA%</c>, which roams in a domain environment and
|
|
/// would do exactly the wrong thing.
|
|
/// </para>
|
|
/// </remarks>
|
|
/// <param name="DataDirectory">The profile directory. Created on demand.</param>
|
|
public sealed record ClientPaths(string DataDirectory)
|
|
{
|
|
private const string WindowsFolderName = "DodoSSH";
|
|
private const string UnixFolderName = "dodossh";
|
|
|
|
/// <summary>The conventional location for this platform.</summary>
|
|
public static ClientPaths Default { get; } = new(ResolveDataDirectory());
|
|
|
|
/// <summary>The encrypted local cache.</summary>
|
|
public string CacheFile => Path.Combine(DataDirectory, "cache.db");
|
|
|
|
/// <summary>
|
|
/// This machine's device key, encrypted to a key it cannot export.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Local and non-roaming for a stronger reason than the cache is: the file is decryptable only by a
|
|
/// key held in this machine's TPM, so a copy of it on another machine is bytes nothing can open. It
|
|
/// following a user to a second computer would be useless rather than dangerous — but a roaming
|
|
/// profile that overwrote one machine's blob with another's would break both.
|
|
/// </remarks>
|
|
public string DeviceKeyFile => Path.Combine(DataDirectory, "device.key");
|
|
|
|
/// <summary>Creates the profile directory if it is not there yet.</summary>
|
|
/// <remarks>
|
|
/// Separate from resolving the path, because resolving must never have a side effect: it is read
|
|
/// during startup diagnostics and by tests that have no business creating directories.
|
|
/// </remarks>
|
|
public void EnsureCreated() => Directory.CreateDirectory(DataDirectory);
|
|
|
|
/// <remarks>
|
|
/// The platform branches are explicit rather than delegating to
|
|
/// <see cref="Environment.SpecialFolder.LocalApplicationData"/> everywhere. That enumeration does
|
|
/// the right thing on Windows, but on macOS the runtime maps it to <c>~/.local/share</c> rather than
|
|
/// to <c>~/Library/Application Support</c>, and relying on framework behaviour that differs per
|
|
/// platform for a path users will look at is how a file ends up somewhere nobody expects.
|
|
/// <para>
|
|
/// <c>XDG_DATA_HOME</c> is honoured explicitly for the same reason: it is the spec, and reading it
|
|
/// here is one line versus depending on whether the runtime happens to.
|
|
/// </para>
|
|
/// </remarks>
|
|
private static string ResolveDataDirectory()
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
return Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
WindowsFolderName);
|
|
}
|
|
|
|
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
|
|
|
if (OperatingSystem.IsMacOS())
|
|
{
|
|
return Path.Combine(home, "Library", "Application Support", WindowsFolderName);
|
|
}
|
|
|
|
var xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME");
|
|
|
|
var root = string.IsNullOrWhiteSpace(xdgDataHome)
|
|
? Path.Combine(home, ".local", "share")
|
|
: xdgDataHome;
|
|
|
|
return Path.Combine(root, UnixFolderName);
|
|
}
|
|
}
|