#include "evo/network/tls_channel.hpp" #include #include #include #include #include #include // 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(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(context); const auto result = self.transport.write( {buffer, std::min(length, static_cast(INT_MAX))}); if (!result) { self.transport_failed = true; return MBEDTLS_ERR_SSL_INTERNAL_ERROR; } return *result == 0 ? MBEDTLS_ERR_SSL_WANT_WRITE : static_cast(*result); } static int receive(void* context, unsigned char* buffer, std::size_t length) { auto& self = *static_cast(context); const auto result = self.transport.read( {buffer, std::min(length, static_cast(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(*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 fail(TlsError e) { error = e; return std::unexpected(e); } std::unexpected 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 error; }; TlsChannel::TlsChannel(std::unique_ptr impl) : impl_{std::move(impl)} {} TlsChannel::~TlsChannel() = default; std::expected, TlsError> TlsChannel::server(const ServerCredentials& credentials, ByteTransport& transport) { auto impl = std::make_unique(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(new TlsChannel(std::move(impl))); } std::expected, TlsError> TlsChannel::client(const ClientTrust& trust, ByteTransport& transport) { if (trust.server_name.empty()) return std::unexpected(TlsError::configuration); auto impl = std::make_unique(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(new TlsChannel(std::move(impl))); } std::expected 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 TlsChannel::send(std::span 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(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 TlsChannel::receive(std::span 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(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 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