Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

4.5 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Biometric unlock from this PC (shared/unlock-client.js): Windows Hello unlocks another computer of the
// mesh (or the one-to-one partner) when that one allows computers. A Windows Hello key of its own
// (KeyCredentialManager: RSA-2048 in the TPM, every use behind Windows Hello's face, fingerprint or PIN
// prompt) per computer it unlocks, named "TwinDesk unlock <id>". The other computer keeps its public half.
// Windows on the other side also needs a key K (its vault for the password, windows/fullkvm/service/
// Unlock.cs): kept here with DPAPI for this Windows account, sent only after Windows Hello signed.
//   helloCan {}                      → {can}
//   helloCreate {key}                → {pub}       (SPKI DER, base64; asks Windows Hello)
//   helloSign {key, data}            → {sig}       (data: base64 of what's signed; asks Windows Hello)
//   helloDelete {key}
//   helloProtect {data} → {data} ; helloUnprotect {data} → {data}   (DPAPI, this Windows account)
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Windows.Security.Credentials;
using Windows.Security.Cryptography.Core;

namespace MacShare
{
    static class Hello
    {
        static readonly Regex KeyId = new Regex("^[A-Za-z0-9_-]{8,64}$");
        static readonly byte[] Entropy = Encoding.UTF8.GetBytes("twindesk-unlock-k");

        static string Name(Dictionary<string, object> c)
        {
            string key = Json.Str(c, "key", "");
            if (!KeyId.IsMatch(key)) throw new Exception("bad key");
            return "TwinDesk unlock " + key;
        }

        public static void Dispatch(string cmd, Dictionary<string, object> c, Dictionary<string, object> r)
        {
            switch (cmd)
            {
                case "helloCan":
                    r["can"] = KeyCredentialManager.IsSupportedAsync().AsTask().Result;
                    break;
                case "helloCreate":
                    {
                        KeyCredentialRetrievalResult k = KeyCredentialManager.RequestCreateAsync(Name(c), KeyCredentialCreationOption.ReplaceExisting).AsTask().Result;
                        if (k.Status != KeyCredentialStatus.Success) throw new Exception(Why(k.Status));
                        r["pub"] = Convert.ToBase64String(k.Credential.RetrievePublicKey(CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo).ToArray());
                        break;
                    }
                case "helloSign":
                    {
                        KeyCredentialRetrievalResult k = KeyCredentialManager.OpenAsync(Name(c)).AsTask().Result;
                        if (k.Status != KeyCredentialStatus.Success) throw new Exception(k.Status == KeyCredentialStatus.NotFound ? "gone" : Why(k.Status));
                        byte[] data = Convert.FromBase64String(Json.Str(c, "data", ""));
                        KeyCredentialOperationResult s = k.Credential.RequestSignAsync(data.AsBuffer()).AsTask().Result;
                        if (s.Status != KeyCredentialStatus.Success) throw new Exception(Why(s.Status));
                        r["sig"] = Convert.ToBase64String(s.Result.ToArray());
                        break;
                    }
                case "helloDelete":
                    try { KeyCredentialManager.DeleteAsync(Name(c)).AsTask().Wait(); } catch (Exception) { /* gone already */ }
                    break;
                case "helloProtect":
                    r["data"] = Convert.ToBase64String(ProtectedData.Protect(Convert.FromBase64String(Json.Str(c, "data", "")), Entropy, DataProtectionScope.CurrentUser));
                    break;
                case "helloUnprotect":
                    r["data"] = Convert.ToBase64String(ProtectedData.Unprotect(Convert.FromBase64String(Json.Str(c, "data", "")), Entropy, DataProtectionScope.CurrentUser));
                    break;
            }
        }

        static string Why(KeyCredentialStatus s)
        {
            switch (s)
            {
                case KeyCredentialStatus.UserCanceled: return "cancelled";
                case KeyCredentialStatus.NotFound: return "gone";
                case KeyCredentialStatus.UserPrefersPassword: return "cancelled";
                case KeyCredentialStatus.SecurityDeviceLocked: return "Windows Hello is locked for now";
                default: return "Windows Hello: " + s;
            }
        }
    }
}