#include "echoion/core/licensing/LicenseVerifier.hpp" #include #include #include #include #include #include #include #include namespace { constexpr std::string_view seedHex = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"; constexpr std::string_view publicHex = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; std::string hex(std::string_view value) { std::string output; output.reserve(value.size() / 2); for (std::size_t index = 0; index < value.size(); index += 2) { output.push_back(static_cast( std::stoi(std::string{value.substr(index, 2)}, nullptr, 16))); } return output; } std::string sign(std::string_view message) { using Key = std::unique_ptr; using Context = std::unique_ptr; const auto seed = hex(seedHex); Key key{EVP_PKEY_new_raw_private_key( EVP_PKEY_ED25519, nullptr, reinterpret_cast(seed.data()), seed.size()), &EVP_PKEY_free}; Context context{EVP_MD_CTX_new(), &EVP_MD_CTX_free}; if (!key || !context || EVP_DigestSignInit(context.get(), nullptr, nullptr, nullptr, key.get()) != 1) { return {}; } std::string signature(Void::Crypto::Ed25519SignatureSize, '\0'); std::size_t size = signature.size(); if (EVP_DigestSign( context.get(), reinterpret_cast(signature.data()), &size, reinterpret_cast(message.data()), message.size()) != 1) { return {}; } signature.resize(size); return signature; } std::string envelope(std::string_view payload) { return std::format( "{{\n \"schemaVersion\": 3,\n \"keyId\": \"test-key\",\n" " \"document\": {},\n \"signedPayload\": \"{}\",\n" " \"signature\": \"{}\"\n}}", payload, Void::Encoding::Base64::encode_url(payload), Void::Encoding::Base64::encode_url(sign(payload))); } int failures = 0; void expect(bool condition, std::string_view message) { if (!condition) { std::cerr << "FAILED: " << message << '\n'; ++failures; } } } // namespace int main() { using namespace Echoion::Core::Licensing; const auto publicKey = hex(publicHex); const auto encodedKey = Void::Encoding::Base64::encode_url(publicKey); Void::Update::Keyring keys; expect(addPublicKey(keys, "test-key", encodedKey).has_value(), "base64url public key is accepted"); expect(!addPublicKey(keys, "broken\nkey", encodedKey), "public key ids reject whitespace and control characters"); constexpr std::uint64_t now = 1'800'000'000; const auto lease = std::format( R"({{"document":"echoion-license-lease","licenseId":"lic-1","accountId":"account-1","deviceId":"device-1","productId":"echoion","productVersion":"2.0.0","buildSignature":"build-1","challenge":"challenge-1","status":"active","issuedAt":{},"refreshAfter":{},"expiresAt":{},"sequence":42,"revocationSequence":8,"entitlements":["runtime","updates"]}})", now - 10, now + 3600, now + 7200); const auto revocations = std::format( R"({{"document":"echoion-license-revocations","sequence":8,"generatedAt":{},"expiresAt":{},"licenseIds":[],"accountIds":[],"deviceIds":[],"buildSignatures":[],"keyIds":[]}})", now - 10, now + 7200); LeaseContext context{ .productId = "echoion", .productVersion = "2.0.0", .deviceId = "device-1", .buildSignature = "build-1", .challenge = "challenge-1", .now = std::chrono::system_clock::time_point{std::chrono::seconds{now}}, .maximumLeaseLifetime = std::chrono::hours(24), .minimumSequence = 41}; auto active = verifyLease(envelope(lease), envelope(revocations), keys, context); expect(active && active->mode == Mode::Active && active->sequence == 42, "matching signed lease enables normal operation"); context.buildSignature = "pirated-build"; expect(!verifyLease(envelope(lease), envelope(revocations), keys, context), "build signature mismatch fails closed"); context.buildSignature = "build-1"; const auto revoked = std::format( R"({{"document":"echoion-license-revocations","sequence":9,"generatedAt":{},"expiresAt":{},"licenseIds":["lic-1"],"accountIds":[],"deviceIds":[],"buildSignatures":[],"keyIds":[]}})", now - 10, now + 7200); expect(!verifyLease(envelope(lease), envelope(revoked), keys, context), "specific license revocation fails closed"); context.minimumSequence = 43; expect(!verifyLease(envelope(lease), envelope(revocations), keys, context), "older lease sequence cannot be replayed"); return failures == 0 ? 0 : 1; }