Add protocol v1: versioned, bounded frames (Hello, Welcome, Intent, IntentResult, environment/organism views, Close) carrying session, epoch and commit watermark, with an explicit little-endian canonical codec, a header-first FrameReader, seeded mutation tests and a libFuzzer target. SessionAdmission enforces world/region scope, grant lifetime and skew, single-use grants, one session per subject, epoch, replay, tick window and per-session rate. Dev identity is a 108-byte HMAC-SHA256 grant (PSA) that cannot be created or verified outside the dev environment. Transport security is TLS 1.3 only via pinned Mbed TLS 4.1.1 LTS with TLS 1.2, DTLS, tickets and PSK compiled out, AEAD suites, ECDHE groups and a pinned dev CA. Opt-in EVO_BUILD_NETWORK, new network CI suite. Sockets and region integration follow in M9.7. ADR-0082 records the decision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
5.5 KiB
C++
130 lines
5.5 KiB
C++
#include "evo/network/dev_grant.hpp"
|
|
#include <algorithm>
|
|
#include <psa/crypto.h>
|
|
#include <type_traits>
|
|
|
|
namespace evo::network {
|
|
namespace {
|
|
static_assert(std::is_same_v<mbedtls_svc_key_id_t, std::uint32_t>,
|
|
"key ids are stored as plain PSA identifiers");
|
|
constexpr std::array<std::uint8_t, 16> marker{'E', 'V', 'O', '-', 'D', 'E', 'V', '-',
|
|
'O', 'N', 'L', 'Y', 0, 0, 0, 0};
|
|
constexpr std::uint16_t grant_version = 1;
|
|
constexpr std::uint8_t dev_environment_byte = 1;
|
|
constexpr std::size_t claims_end = 76; // Tag covers bytes [0, 76).
|
|
constexpr psa_algorithm_t algorithm = PSA_ALG_HMAC(PSA_ALG_SHA_256);
|
|
void put(DevGrant& out, std::size_t at, std::uint64_t v, std::size_t count) {
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
out.at(at + i) = static_cast<std::uint8_t>(v >> (8 * i));
|
|
}
|
|
std::uint64_t get(std::span<const std::uint8_t> in, std::size_t at, std::size_t count) {
|
|
std::uint64_t v = 0;
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
v |= static_cast<std::uint64_t>(in[at + i]) << (8 * i);
|
|
return v;
|
|
}
|
|
bool structurally_valid(const DevGrantClaims& c) {
|
|
return c.grant_id != 0 && c.subject != 0 && c.world.world != 0 && c.world.frame != 0 &&
|
|
c.region != 0 && c.expires_at > c.issued_at;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<DevGrantKey, GrantError> DevGrantKey::import(DeploymentEnvironment environment,
|
|
std::span<const std::uint8_t> secret) {
|
|
// The dev identity must not exist anywhere but the dev environment.
|
|
if (environment != DeploymentEnvironment::dev)
|
|
return std::unexpected(GrantError::not_dev_environment);
|
|
if (secret.size() != dev_key_bytes ||
|
|
std::ranges::all_of(secret, [](std::uint8_t b) { return b == 0; }))
|
|
return std::unexpected(GrantError::invalid_key);
|
|
if (psa_crypto_init() != PSA_SUCCESS)
|
|
return std::unexpected(GrantError::crypto);
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
|
|
psa_set_key_bits(&attributes, dev_key_bytes * 8);
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE);
|
|
psa_set_key_algorithm(&attributes, algorithm);
|
|
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
|
|
const auto status = psa_import_key(&attributes, secret.data(), secret.size(), &key);
|
|
psa_reset_key_attributes(&attributes);
|
|
if (status != PSA_SUCCESS)
|
|
return std::unexpected(GrantError::crypto);
|
|
return DevGrantKey{key};
|
|
}
|
|
|
|
DevGrantKey::DevGrantKey(DevGrantKey&& other) noexcept : key_{other.key_} {
|
|
other.key_ = PSA_KEY_ID_NULL;
|
|
}
|
|
|
|
DevGrantKey& DevGrantKey::operator=(DevGrantKey&& other) noexcept {
|
|
if (this != &other) {
|
|
if (key_ != PSA_KEY_ID_NULL)
|
|
(void)psa_destroy_key(key_);
|
|
key_ = other.key_;
|
|
other.key_ = PSA_KEY_ID_NULL;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
DevGrantKey::~DevGrantKey() {
|
|
if (key_ != PSA_KEY_ID_NULL)
|
|
(void)psa_destroy_key(key_);
|
|
}
|
|
|
|
std::expected<DevGrant, GrantError> DevGrantKey::issue(const DevGrantClaims& claims) const {
|
|
if (key_ == PSA_KEY_ID_NULL)
|
|
return std::unexpected(GrantError::invalid_key);
|
|
if (!structurally_valid(claims))
|
|
return std::unexpected(GrantError::invalid_claims);
|
|
DevGrant out{};
|
|
std::ranges::copy(marker, out.begin());
|
|
put(out, 16, grant_version, 2);
|
|
out.at(18) = dev_environment_byte;
|
|
out.at(19) = 0;
|
|
put(out, 20, claims.grant_id, 8);
|
|
put(out, 28, claims.subject, 8);
|
|
put(out, 36, claims.world.world, 8);
|
|
put(out, 44, claims.world.frame, 8);
|
|
put(out, 52, claims.region, 8);
|
|
put(out, 60, claims.issued_at, 8);
|
|
put(out, 68, claims.expires_at, 8);
|
|
std::size_t length = 0;
|
|
if (psa_mac_compute(key_, algorithm, out.data(), claims_end, out.data() + claims_end,
|
|
dev_grant_bytes - claims_end, &length) != PSA_SUCCESS ||
|
|
length != dev_grant_bytes - claims_end)
|
|
return std::unexpected(GrantError::crypto);
|
|
return out;
|
|
}
|
|
|
|
std::expected<VerifiedGrant, GrantError>
|
|
DevGrantKey::verify(std::span<const std::uint8_t> grant) const {
|
|
if (key_ == PSA_KEY_ID_NULL)
|
|
return std::unexpected(GrantError::invalid_key);
|
|
if (grant.size() != dev_grant_bytes)
|
|
return std::unexpected(GrantError::malformed);
|
|
// Authenticate first: no claim byte influences behaviour until the tag matches.
|
|
const auto status = psa_mac_verify(key_, algorithm, grant.data(), claims_end,
|
|
grant.data() + claims_end, dev_grant_bytes - claims_end);
|
|
if (status == PSA_ERROR_INVALID_SIGNATURE)
|
|
return std::unexpected(GrantError::bad_tag);
|
|
if (status != PSA_SUCCESS)
|
|
return std::unexpected(GrantError::crypto);
|
|
if (!std::equal(marker.begin(), marker.end(), grant.begin()))
|
|
return std::unexpected(GrantError::wrong_marker);
|
|
if (get(grant, 16, 2) != grant_version || grant[19] != 0)
|
|
return std::unexpected(GrantError::unsupported_version);
|
|
if (grant[18] != dev_environment_byte)
|
|
return std::unexpected(GrantError::wrong_environment);
|
|
DevGrantClaims claims;
|
|
claims.grant_id = get(grant, 20, 8);
|
|
claims.subject = get(grant, 28, 8);
|
|
claims.world = {get(grant, 36, 8), get(grant, 44, 8)};
|
|
claims.region = get(grant, 52, 8);
|
|
claims.issued_at = get(grant, 60, 8);
|
|
claims.expires_at = get(grant, 68, 8);
|
|
if (!structurally_valid(claims))
|
|
return std::unexpected(GrantError::invalid_claims);
|
|
return VerifiedGrant{claims};
|
|
}
|
|
} // namespace evo::network
|