92 lines
4.0 KiB
C++
92 lines
4.0 KiB
C++
#include "lan_clients.h"
|
|
#include <iostream>
|
|
|
|
namespace
|
|
{
|
|
using ks::lan::Json;
|
|
void require(bool value, const char *message)
|
|
{
|
|
if (!value) throw std::runtime_error(message);
|
|
}
|
|
|
|
Json request(const std::wstring &name)
|
|
{
|
|
require(WaitNamedPipeW(name.c_str(), 3000) != FALSE, "LAN client pipe did not start");
|
|
struct Pipe
|
|
{
|
|
HANDLE value;
|
|
~Pipe() { if (value != INVALID_HANDLE_VALUE) CloseHandle(value); }
|
|
} pipe{CreateFileW(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING,
|
|
SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, nullptr)};
|
|
require(pipe.value != INVALID_HANDLE_VALUE, "could not open LAN client pipe");
|
|
auto transfer = [&](void *buffer, DWORD length, bool write) {
|
|
auto *cursor = static_cast<char *>(buffer);
|
|
while (length)
|
|
{
|
|
DWORD count{};
|
|
require((write ? WriteFile(pipe.value, cursor, length, &count, nullptr) :
|
|
ReadFile(pipe.value, cursor, length, &count, nullptr)) && count, "pipe transfer failed");
|
|
cursor += count;
|
|
length -= count;
|
|
}
|
|
};
|
|
auto payload = Json{{"action", "peers"}}.dump();
|
|
DWORD length = DWORD(payload.size());
|
|
transfer(&length, sizeof(length), true);
|
|
transfer(payload.data(), length, true);
|
|
transfer(&length, sizeof(length), false);
|
|
require(length > 0 && length < 4096, "unexpected reply size");
|
|
payload.resize(length);
|
|
transfer(payload.data(), length, false);
|
|
char ack{};
|
|
transfer(&ack, 1, true);
|
|
return Json::parse(payload);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
const auto suffix = L".test." + std::to_wstring(GetCurrentProcessId());
|
|
const auto root = std::filesystem::temp_directory_path() / (L"KSwitcher-LAN" + suffix);
|
|
try
|
|
{
|
|
require(std::filesystem::create_directory(root), "could not create test directory");
|
|
DWORD session{};
|
|
require(ProcessIdToSessionId(GetCurrentProcessId(), &session) != FALSE, "no Windows session");
|
|
const auto pipe = L"\\\\.\\pipe\\KSwitcher.LAN.Clients.v1." + std::to_wstring(session) + suffix;
|
|
{
|
|
ks::lan::LanClients clients(root);
|
|
clients.start([](const Json &request, const auto &, const auto &send) {
|
|
require(request.at("action") == "peers", "unexpected request");
|
|
constexpr std::string_view reply = R"([{"id":"fixture","name":"Paired PC","online":true}])";
|
|
send(reply.data(), DWORD(reply.size()));
|
|
}, suffix);
|
|
// WaitNamedPipe returns immediately if the instance has not been created yet.
|
|
for (int n = 0; n < 100 && !WaitNamedPipeW(pipe.c_str(), 20); ++n) Sleep(20);
|
|
require(request(pipe).contains("error"), "an untrusted executable was accepted");
|
|
const auto path = ks::utf8(ks::executable());
|
|
const auto trusted = clients.settings("trustClient", {{"path", path}});
|
|
require(trusted.size() == 1, "the program must be enrolled exactly once");
|
|
require(request(pipe).at(0).at("name") == "Paired PC", "the trusted program was rejected");
|
|
auto changed = trusted;
|
|
changed.at(0)["sha256"] = std::string(64, '0');
|
|
const auto store = root / L"lan-clients.bin";
|
|
const auto differentHash = changed.dump();
|
|
ks::atomicWrite(store, ks::protect(ks::Bytes(differentHash.begin(), differentHash.end())));
|
|
require(request(pipe).contains("error"), "an executable with a different fingerprint was accepted");
|
|
clients.settings("trustClient", {{"path", path}});
|
|
clients.settings("removeClient", {{"path", trusted.at(0).at("path")}});
|
|
require(request(pipe).contains("error"), "revocation must reject the next request");
|
|
}
|
|
std::filesystem::remove(root / L"lan-clients.bin");
|
|
std::filesystem::remove(root);
|
|
std::cout << "Verified executable approval, resident pipe access and revocation.\n";
|
|
return 0;
|
|
}
|
|
catch (const std::exception &error)
|
|
{
|
|
std::cerr << error.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|