Public Access
Desktop: probe for the TPM once, off the UI thread, instead of on every unlock
This commit is contained in:
1 parent
3ccafdd53b
commit
365954c541
4 files changed
+133
-5
No files matched your search
@@ -0,0 +1,62 @@
|
||||
using DodoSSH.Client.Session;
|
||||
|
||||
namespace DodoSSH.Client.App.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// The device key store this machine supports, chosen once and off the UI thread.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Choosing a store means probing for it, and on Windows the probe is creating an RSA-2048 key inside the TPM
|
||||
/// and deleting it again — see <see cref="WindowsDeviceKeyStore.IsSupported"/> — which is a slow device doing
|
||||
/// real work. It used to run synchronously on the UI thread three times over: once to choose the store at
|
||||
/// startup, again when the unlock card asked whether to offer the gesture, and again after every unlock when
|
||||
/// the security page asked whether to offer registering. A machine does not gain or lose a TPM while this
|
||||
/// process runs, so the answer is worked out once, on the thread pool, and every question after it is a
|
||||
/// lookup.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Available means the choice landed on a real store, which is what the probe that chose it established.
|
||||
/// Everything else waits for the choice and hands over to it — in practice it has long since finished,
|
||||
/// since nothing reaches for the device key until somebody is at the unlock card.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal sealed class ProbedDeviceKeyStore : IDeviceKeyStore
|
||||
{
|
||||
private readonly Task<IDeviceKeyStore> chosen;
|
||||
|
||||
/// <summary>Starts choosing at once, on the thread pool.</summary>
|
||||
internal ProbedDeviceKeyStore(Func<IDeviceKeyStore> choose)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(choose);
|
||||
chosen = Task.Run(choose);
|
||||
}
|
||||
|
||||
/// <summary>The store the probe settled on, for a test that needs to know which.</summary>
|
||||
internal Task<IDeviceKeyStore> Chosen => chosen;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<bool> IsAvailableAsync(CancellationToken cancellationToken) =>
|
||||
await chosen.WaitAsync(cancellationToken).ConfigureAwait(false) is not UnavailableDeviceKeyStore;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask SaveAsync(ReadOnlyMemory<byte> devicePrivateKey, CancellationToken cancellationToken)
|
||||
{
|
||||
var store = await chosen.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
await store.SaveAsync(devicePrivateKey, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<byte[]?> TryLoadAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var store = await chosen.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
return await store.TryLoadAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask ForgetAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var store = await chosen.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
await store.ForgetAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,12 @@ namespace DodoSSH.Client.App.Platform;
|
||||
/// </remarks>
|
||||
public static class DesktopDeviceKeyStores
|
||||
{
|
||||
/// <summary>The best store this machine supports.</summary>
|
||||
/// <summary>The best store this machine supports, chosen on the thread pool.</summary>
|
||||
/// <remarks>
|
||||
/// Returns at once: the probing that decides between them is slow — a TPM key created and deleted on
|
||||
/// Windows — and runs behind <see cref="ProbedDeviceKeyStore"/> rather than here, which is on the UI
|
||||
/// thread at startup.
|
||||
/// </remarks>
|
||||
/// <param name="paths">Where a stored key lives.</param>
|
||||
/// <param name="askForPin">
|
||||
/// Shows the Linux PIN prompt, and is never called on the other two platforms — there the platform
|
||||
@@ -57,6 +62,13 @@ public static class DesktopDeviceKeyStores
|
||||
ArgumentNullException.ThrowIfNull(paths);
|
||||
ArgumentNullException.ThrowIfNull(askForPin);
|
||||
|
||||
return new ProbedDeviceKeyStore(() => Choose(paths, askForPin));
|
||||
}
|
||||
|
||||
private static IDeviceKeyStore Choose(
|
||||
ClientPaths paths,
|
||||
Func<DevicePinPurpose, CancellationToken, ValueTask<string?>> askForPin)
|
||||
{
|
||||
if (OperatingSystem.IsWindows() && WindowsDeviceKeyStore.IsSupported())
|
||||
{
|
||||
return new WindowsDeviceKeyStore(paths);
|
||||
|
||||
Reference in new issue
Block a user