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>
205 lines
9.2 KiB
C++
205 lines
9.2 KiB
C++
#include "evo/network/tls_channel.hpp"
|
|
#include <algorithm>
|
|
#include <climits>
|
|
#include <mbedtls/ssl.h>
|
|
#include <mbedtls/x509_crt.h>
|
|
#include <optional>
|
|
#include <psa/crypto.h>
|
|
|
|
// The owned user config (third_party/mbedtls/evo_mbedtls_config.h) must be in effect.
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) || defined(MBEDTLS_SSL_PROTO_DTLS) || \
|
|
defined(MBEDTLS_SSL_SESSION_TICKETS) || defined(MBEDTLS_SSL_RENEGOTIATION) || \
|
|
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) || \
|
|
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED) || \
|
|
!defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
|
#error "ADR-0082 requires the TLS 1.3-only Mbed TLS configuration"
|
|
#endif
|
|
|
|
namespace evo::network {
|
|
namespace {
|
|
// ADR-0082 profile: TLS 1.3 only, AEAD suites, ECDHE groups, ephemeral key exchange.
|
|
constexpr int suites[]{MBEDTLS_TLS1_3_AES_128_GCM_SHA256, MBEDTLS_TLS1_3_CHACHA20_POLY1305_SHA256,
|
|
0};
|
|
constexpr std::uint16_t groups[]{MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, 0};
|
|
const unsigned char* bytes_of(const std::string& text) {
|
|
return reinterpret_cast<const unsigned char*>(text.c_str());
|
|
}
|
|
} // namespace
|
|
|
|
struct TlsChannel::Impl {
|
|
explicit Impl(ByteTransport& t) : transport{t} {
|
|
mbedtls_ssl_config_init(&config);
|
|
mbedtls_ssl_init(&ssl);
|
|
mbedtls_x509_crt_init(&chain);
|
|
mbedtls_x509_crt_init(&authority);
|
|
mbedtls_pk_init(&key);
|
|
}
|
|
~Impl() {
|
|
mbedtls_ssl_free(&ssl);
|
|
mbedtls_ssl_config_free(&config);
|
|
mbedtls_x509_crt_free(&chain);
|
|
mbedtls_x509_crt_free(&authority);
|
|
mbedtls_pk_free(&key); // Zeroizes key material.
|
|
}
|
|
Impl(const Impl&) = delete;
|
|
Impl& operator=(const Impl&) = delete;
|
|
static int send(void* context, const unsigned char* buffer, std::size_t length) {
|
|
auto& self = *static_cast<Impl*>(context);
|
|
const auto result = self.transport.write(
|
|
{buffer, std::min<std::size_t>(length, static_cast<std::size_t>(INT_MAX))});
|
|
if (!result) {
|
|
self.transport_failed = true;
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
}
|
|
return *result == 0 ? MBEDTLS_ERR_SSL_WANT_WRITE : static_cast<int>(*result);
|
|
}
|
|
static int receive(void* context, unsigned char* buffer, std::size_t length) {
|
|
auto& self = *static_cast<Impl*>(context);
|
|
const auto result = self.transport.read(
|
|
{buffer, std::min<std::size_t>(length, static_cast<std::size_t>(INT_MAX))});
|
|
if (!result) {
|
|
if (result.error() == TransportError::closed)
|
|
return 0; // End of stream.
|
|
self.transport_failed = true;
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
}
|
|
return *result == 0 ? MBEDTLS_ERR_SSL_WANT_READ : static_cast<int>(*result);
|
|
}
|
|
bool common(int endpoint) {
|
|
if (psa_crypto_init() != PSA_SUCCESS ||
|
|
mbedtls_ssl_config_defaults(&config, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
MBEDTLS_SSL_PRESET_DEFAULT) != 0)
|
|
return false;
|
|
mbedtls_ssl_conf_min_tls_version(&config, MBEDTLS_SSL_VERSION_TLS1_3);
|
|
mbedtls_ssl_conf_max_tls_version(&config, MBEDTLS_SSL_VERSION_TLS1_3);
|
|
mbedtls_ssl_conf_ciphersuites(&config, suites);
|
|
mbedtls_ssl_conf_groups(&config, groups);
|
|
mbedtls_ssl_conf_tls13_key_exchange_modes(&config,
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL);
|
|
return true;
|
|
}
|
|
bool attach() {
|
|
if (mbedtls_ssl_setup(&ssl, &config) != 0)
|
|
return false;
|
|
mbedtls_ssl_set_bio(&ssl, this, send, receive, nullptr);
|
|
return true;
|
|
}
|
|
std::unexpected<TlsError> fail(TlsError e) {
|
|
error = e;
|
|
return std::unexpected(e);
|
|
}
|
|
std::unexpected<TlsError> fail_from(int code, TlsError otherwise) {
|
|
if (transport_failed)
|
|
return fail(TlsError::transport);
|
|
// Primary chain/name rejection or secondary certificate checks (ssl_tls.c).
|
|
if (code == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || code == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)
|
|
return fail(TlsError::peer_verification);
|
|
return fail(otherwise);
|
|
}
|
|
ByteTransport& transport;
|
|
mbedtls_ssl_config config;
|
|
mbedtls_ssl_context ssl;
|
|
mbedtls_x509_crt chain, authority;
|
|
mbedtls_pk_context key;
|
|
bool transport_failed{};
|
|
std::optional<TlsError> error;
|
|
};
|
|
|
|
TlsChannel::TlsChannel(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
|
TlsChannel::~TlsChannel() = default;
|
|
|
|
std::expected<std::unique_ptr<TlsChannel>, TlsError>
|
|
TlsChannel::server(const ServerCredentials& credentials, ByteTransport& transport) {
|
|
auto impl = std::make_unique<Impl>(transport);
|
|
if (!impl->common(MBEDTLS_SSL_IS_SERVER))
|
|
return std::unexpected(TlsError::configuration);
|
|
// PEM parsing requires the terminating NUL in the length.
|
|
if (mbedtls_x509_crt_parse(&impl->chain, bytes_of(credentials.certificate_chain_pem),
|
|
credentials.certificate_chain_pem.size() + 1) != 0 ||
|
|
mbedtls_pk_parse_key(&impl->key, bytes_of(credentials.private_key_pem),
|
|
credentials.private_key_pem.size() + 1, nullptr, 0) != 0 ||
|
|
mbedtls_pk_check_pair(&impl->chain.pk, &impl->key) != 0 ||
|
|
mbedtls_ssl_conf_own_cert(&impl->config, &impl->chain, &impl->key) != 0)
|
|
return std::unexpected(TlsError::credentials);
|
|
// Client identity is the in-channel grant, not a client certificate.
|
|
mbedtls_ssl_conf_authmode(&impl->config, MBEDTLS_SSL_VERIFY_NONE);
|
|
if (!impl->attach())
|
|
return std::unexpected(TlsError::configuration);
|
|
return std::unique_ptr<TlsChannel>(new TlsChannel(std::move(impl)));
|
|
}
|
|
|
|
std::expected<std::unique_ptr<TlsChannel>, TlsError> TlsChannel::client(const ClientTrust& trust,
|
|
ByteTransport& transport) {
|
|
if (trust.server_name.empty())
|
|
return std::unexpected(TlsError::configuration);
|
|
auto impl = std::make_unique<Impl>(transport);
|
|
if (!impl->common(MBEDTLS_SSL_IS_CLIENT))
|
|
return std::unexpected(TlsError::configuration);
|
|
if (mbedtls_x509_crt_parse(&impl->authority, bytes_of(trust.authority_pem),
|
|
trust.authority_pem.size() + 1) != 0)
|
|
return std::unexpected(TlsError::credentials);
|
|
mbedtls_ssl_conf_ca_chain(&impl->config, &impl->authority, nullptr);
|
|
mbedtls_ssl_conf_authmode(&impl->config, MBEDTLS_SSL_VERIFY_REQUIRED);
|
|
if (!impl->attach() || mbedtls_ssl_set_hostname(&impl->ssl, trust.server_name.c_str()) != 0)
|
|
return std::unexpected(TlsError::configuration);
|
|
return std::unique_ptr<TlsChannel>(new TlsChannel(std::move(impl)));
|
|
}
|
|
|
|
std::expected<TlsProgress, TlsError> TlsChannel::handshake() {
|
|
if (const auto latched = impl_->error)
|
|
return std::unexpected(*latched);
|
|
const int result = mbedtls_ssl_handshake(&impl_->ssl);
|
|
if (result == 0)
|
|
return TlsProgress::done;
|
|
if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
return TlsProgress::want_io;
|
|
return impl_->fail_from(result, TlsError::handshake);
|
|
}
|
|
|
|
std::expected<std::size_t, TlsError> TlsChannel::send(std::span<const std::uint8_t> bytes) {
|
|
if (const auto latched = impl_->error)
|
|
return std::unexpected(*latched);
|
|
const int result = mbedtls_ssl_write(&impl_->ssl, bytes.data(), bytes.size());
|
|
if (result >= 0)
|
|
return static_cast<std::size_t>(result);
|
|
if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
return 0;
|
|
return impl_->fail_from(result, TlsError::record);
|
|
}
|
|
|
|
std::expected<std::size_t, TlsError> TlsChannel::receive(std::span<std::uint8_t> bytes) {
|
|
if (const auto latched = impl_->error)
|
|
return std::unexpected(*latched);
|
|
const int result = mbedtls_ssl_read(&impl_->ssl, bytes.data(), bytes.size());
|
|
if (result > 0)
|
|
return static_cast<std::size_t>(result);
|
|
if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
return 0;
|
|
if (result == 0 || result == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
|
|
return impl_->fail(TlsError::closed);
|
|
return impl_->fail_from(result, TlsError::record);
|
|
}
|
|
|
|
std::expected<TlsProgress, TlsError> TlsChannel::close() {
|
|
if (const auto latched = impl_->error)
|
|
return std::unexpected(*latched);
|
|
const int result = mbedtls_ssl_close_notify(&impl_->ssl);
|
|
if (result == 0)
|
|
return TlsProgress::done;
|
|
if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
return TlsProgress::want_io;
|
|
return impl_->fail_from(result, TlsError::record);
|
|
}
|
|
|
|
std::string_view TlsChannel::version() const noexcept {
|
|
const char* name = mbedtls_ssl_get_version(&impl_->ssl);
|
|
return name == nullptr ? std::string_view{} : std::string_view{name};
|
|
}
|
|
|
|
std::string_view TlsChannel::suite() const noexcept {
|
|
const char* name = mbedtls_ssl_get_ciphersuite(&impl_->ssl);
|
|
return name == nullptr ? std::string_view{} : std::string_view{name};
|
|
}
|
|
} // namespace evo::network
|