278 lines
9.7 KiB
C++
278 lines
9.7 KiB
C++
#include "echoion/core/licensing/LicenseVerifier.hpp"
|
|
|
|
#include <Void/Data/Json.hpp>
|
|
#include <Void/Text/Base64.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cctype>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <ranges>
|
|
|
|
namespace Echoion::Core::Licensing
|
|
{
|
|
namespace
|
|
{
|
|
using Clock = std::chrono::system_clock;
|
|
using Result = Void::Result::Result<void>;
|
|
|
|
[[nodiscard]] bool validKeyId(std::string_view value)
|
|
{
|
|
return !value.empty() && value.size() <= 128 &&
|
|
std::ranges::all_of(value, [](unsigned char character) {
|
|
return std::isalnum(character) != 0 ||
|
|
character == '.' || character == '_' ||
|
|
character == '-';
|
|
});
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<Void::Json::Value>
|
|
parseObject(std::string_view json, std::string_view name)
|
|
{
|
|
auto parsed = Void::Json::parse(json);
|
|
if (!parsed || !parsed->is_object()) {
|
|
return Void::Result::fail(std::string{name} +
|
|
" payload is not a JSON object");
|
|
}
|
|
return std::move(*parsed);
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
requiredString(const Void::Json::Value& value, std::string_view key,
|
|
std::size_t maximum = 512)
|
|
{
|
|
const auto* field = value.find(key);
|
|
if (field == nullptr || !field->is_string() ||
|
|
field->as_string().empty() ||
|
|
field->as_string().size() > maximum) {
|
|
return Void::Result::fail("Licensing: invalid field " +
|
|
std::string{key});
|
|
}
|
|
return field->as_string();
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
|
requiredUnsigned(const Void::Json::Value& value,
|
|
std::string_view key)
|
|
{
|
|
const auto* field = value.find(key);
|
|
if (field == nullptr || !field->is_number()) {
|
|
return Void::Result::fail("Licensing: invalid field " +
|
|
std::string{key});
|
|
}
|
|
const double number = field->as_number();
|
|
if (!std::isfinite(number) || number < 0.0 ||
|
|
std::trunc(number) != number ||
|
|
number > static_cast<double>(
|
|
std::numeric_limits<std::uint64_t>::max())) {
|
|
return Void::Result::fail("Licensing: invalid field " +
|
|
std::string{key});
|
|
}
|
|
return static_cast<std::uint64_t>(number);
|
|
}
|
|
|
|
[[nodiscard]] Clock::time_point unixTime(std::uint64_t seconds)
|
|
{
|
|
return Clock::time_point{std::chrono::seconds{seconds}};
|
|
}
|
|
|
|
[[nodiscard]] bool stringArrayContains(const Void::Json::Value& object,
|
|
std::string_view key,
|
|
std::string_view needle)
|
|
{
|
|
const auto* value = object.find(key);
|
|
if (value == nullptr || !value->is_array()) {
|
|
return false;
|
|
}
|
|
return std::ranges::any_of(
|
|
value->as_array(), [&](const auto& item) {
|
|
return item.is_string() && item.as_string() == needle;
|
|
});
|
|
}
|
|
|
|
[[nodiscard]] std::vector<std::string>
|
|
stringArray(const Void::Json::Value& object, std::string_view key)
|
|
{
|
|
std::vector<std::string> output;
|
|
const auto* value = object.find(key);
|
|
if (value == nullptr || !value->is_array()) {
|
|
return output;
|
|
}
|
|
for (const auto& item : value->as_array()) {
|
|
if (item.is_string() && item.as_string().size() <= 256) {
|
|
output.push_back(item.as_string());
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
} // namespace
|
|
|
|
Void::Result::Result<void> addPublicKey(Void::Update::Keyring& keyring,
|
|
std::string_view keyId,
|
|
std::string_view encodedKey)
|
|
{
|
|
if (!validKeyId(keyId)) {
|
|
return Void::Result::fail("Licensing: invalid public key id");
|
|
}
|
|
auto decoded = Void::Encoding::Base64::decode_url(encodedKey);
|
|
if (!decoded) {
|
|
decoded = Void::Encoding::Base64::decode(encodedKey);
|
|
}
|
|
if (!decoded || decoded->size() != Void::Crypto::Ed25519PublicKeySize) {
|
|
return Void::Result::fail("Licensing: invalid Ed25519 public key");
|
|
}
|
|
Void::Update::Ed25519PublicKey key{};
|
|
std::memcpy(key.data(), decoded->data(), decoded->size());
|
|
keyring.insert_or_assign(std::string{keyId}, key);
|
|
return {};
|
|
}
|
|
|
|
Void::Result::Result<Void::Update::Keyring>
|
|
verifyKeyset(std::string_view envelopeJson,
|
|
const Void::Update::Keyring& rootKeys,
|
|
Clock::time_point now)
|
|
{
|
|
Void::Update::VerificationPolicy policy;
|
|
policy.now = now;
|
|
policy.maxEnvelopeBytes = 256 * 1024;
|
|
policy.maxPayloadBytes = 192 * 1024;
|
|
auto verified =
|
|
Void::Update::verifyEnvelope(envelopeJson, rootKeys, policy);
|
|
if (!verified) {
|
|
return std::unexpected{verified.error()};
|
|
}
|
|
auto document = parseObject(verified->signedPayload, "Keyset");
|
|
if (!document) {
|
|
return std::unexpected{document.error()};
|
|
}
|
|
if (auto name = requiredString(*document, "document");
|
|
!name || *name != "echoion-license-keyset") {
|
|
return Void::Result::fail("Licensing: wrong keyset document");
|
|
}
|
|
auto generatedAt = requiredUnsigned(*document, "generatedAt");
|
|
auto expiresAt = requiredUnsigned(*document, "expiresAt");
|
|
const auto* keys = document->find("keys");
|
|
if (!generatedAt || !expiresAt || keys == nullptr ||
|
|
!keys->is_array() || keys->as_array().size() > 64 ||
|
|
unixTime(*generatedAt) > now + std::chrono::minutes(5) ||
|
|
unixTime(*expiresAt) <= now) {
|
|
return Void::Result::fail("Licensing: invalid or expired keyset");
|
|
}
|
|
|
|
auto output = rootKeys;
|
|
for (const auto& item : keys->as_array()) {
|
|
if (!item.is_object() || item.get_bool("revoked", false)) {
|
|
continue;
|
|
}
|
|
auto id = requiredString(item, "id", 256);
|
|
auto key = requiredString(item, "ed25519", 256);
|
|
auto notBefore = requiredUnsigned(item, "notBefore");
|
|
auto notAfter = requiredUnsigned(item, "notAfter");
|
|
if (!id || !key || !notBefore || !notAfter ||
|
|
unixTime(*notBefore) > now || unixTime(*notAfter) <= now) {
|
|
continue;
|
|
}
|
|
if (auto added = addPublicKey(output, *id, *key); !added) {
|
|
return std::unexpected{added.error()};
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
Void::Result::Result<LeaseStatus>
|
|
verifyLease(std::string_view leaseEnvelopeJson,
|
|
std::string_view revocationEnvelopeJson,
|
|
const Void::Update::Keyring& leaseKeys,
|
|
const LeaseContext& context)
|
|
{
|
|
Void::Update::VerificationPolicy policy;
|
|
policy.now = context.now;
|
|
policy.maxEnvelopeBytes = 512 * 1024;
|
|
policy.maxPayloadBytes = 384 * 1024;
|
|
auto leaseEnvelope =
|
|
Void::Update::verifyEnvelope(leaseEnvelopeJson, leaseKeys, policy);
|
|
if (!leaseEnvelope) {
|
|
return std::unexpected{leaseEnvelope.error()};
|
|
}
|
|
auto revocationEnvelope = Void::Update::verifyEnvelope(
|
|
revocationEnvelopeJson, leaseKeys, policy);
|
|
if (!revocationEnvelope) {
|
|
return std::unexpected{revocationEnvelope.error()};
|
|
}
|
|
auto lease = parseObject(leaseEnvelope->signedPayload, "Lease");
|
|
auto revocations =
|
|
parseObject(revocationEnvelope->signedPayload, "Revocation");
|
|
if (!lease || !revocations) {
|
|
return Void::Result::fail("Licensing: malformed signed payload");
|
|
}
|
|
|
|
auto leaseDocument = requiredString(*lease, "document");
|
|
auto revocationDocument = requiredString(*revocations, "document");
|
|
auto licenseId = requiredString(*lease, "licenseId");
|
|
auto accountId = requiredString(*lease, "accountId");
|
|
auto deviceId = requiredString(*lease, "deviceId");
|
|
auto productId = requiredString(*lease, "productId");
|
|
auto productVersion = requiredString(*lease, "productVersion");
|
|
auto buildSignature = requiredString(*lease, "buildSignature");
|
|
auto challenge = requiredString(*lease, "challenge");
|
|
auto status = requiredString(*lease, "status");
|
|
auto issuedAt = requiredUnsigned(*lease, "issuedAt");
|
|
auto refreshAfter = requiredUnsigned(*lease, "refreshAfter");
|
|
auto expiresAt = requiredUnsigned(*lease, "expiresAt");
|
|
auto sequence = requiredUnsigned(*lease, "sequence");
|
|
auto revocationSequence =
|
|
requiredUnsigned(*lease, "revocationSequence");
|
|
auto snapshotSequence = requiredUnsigned(*revocations, "sequence");
|
|
auto snapshotExpiresAt = requiredUnsigned(*revocations, "expiresAt");
|
|
if (!leaseDocument || *leaseDocument != "echoion-license-lease" ||
|
|
!revocationDocument ||
|
|
*revocationDocument != "echoion-license-revocations" ||
|
|
!licenseId || !accountId || !deviceId || !productId ||
|
|
!productVersion || !buildSignature || !challenge || !status ||
|
|
!issuedAt || !refreshAfter || !expiresAt || !sequence ||
|
|
!revocationSequence || !snapshotSequence || !snapshotExpiresAt) {
|
|
return Void::Result::fail("Licensing: incomplete signed decision");
|
|
}
|
|
|
|
const auto issued = unixTime(*issuedAt);
|
|
const auto refresh = unixTime(*refreshAfter);
|
|
const auto expiry = unixTime(*expiresAt);
|
|
if (*status != "active" || *productId != context.productId ||
|
|
*productVersion != context.productVersion ||
|
|
*deviceId != context.deviceId ||
|
|
*buildSignature != context.buildSignature ||
|
|
*challenge != context.challenge ||
|
|
*sequence < context.minimumSequence ||
|
|
*snapshotSequence < *revocationSequence ||
|
|
issued > context.now + std::chrono::minutes(5) ||
|
|
expiry <= context.now || expiry <= issued ||
|
|
expiry - issued > context.maximumLeaseLifetime ||
|
|
refresh < issued || refresh > expiry ||
|
|
unixTime(*snapshotExpiresAt) <= context.now) {
|
|
return Void::Result::fail("Licensing: lease is inactive, "
|
|
"mismatched, expired, or replayed");
|
|
}
|
|
|
|
if (stringArrayContains(*revocations, "licenseIds", *licenseId) ||
|
|
stringArrayContains(*revocations, "accountIds", *accountId) ||
|
|
stringArrayContains(*revocations, "deviceIds", *deviceId) ||
|
|
stringArrayContains(*revocations, "buildSignatures",
|
|
*buildSignature) ||
|
|
stringArrayContains(*revocations, "productVersions",
|
|
*productVersion) ||
|
|
stringArrayContains(*revocations, "keyIds", leaseEnvelope->keyId)) {
|
|
return Void::Result::fail("Licensing: entitlement was revoked");
|
|
}
|
|
|
|
return LeaseStatus{.mode = Mode::Active,
|
|
.reason = "active",
|
|
.licenseId = std::move(*licenseId),
|
|
.sequence = *sequence,
|
|
.refreshAfter = refresh,
|
|
.expiresAt = expiry,
|
|
.entitlements = stringArray(*lease, "entitlements")};
|
|
}
|
|
} // namespace Echoion::Core::Licensing
|