7 #include "../mbedTLS++/SslContext.h"
8 #include "../mbedTLS++/SslConfig.h"
14 cSslContext::cSslContext(
void) :
16 m_HasHandshaken(false)
18 mbedtls_ssl_init(&m_Ssl);
25 cSslContext::~cSslContext()
27 mbedtls_ssl_free(&m_Ssl);
34 int cSslContext::Initialize(std::shared_ptr<const cSslConfig> a_Config)
39 LOGWARNING(
"SSL: Double initialization is not supported.");
40 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
44 m_Config = std::move(a_Config);
45 if (m_Config ==
nullptr)
47 ASSERT(!
"Config must not be nullptr");
48 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
52 int res = mbedtls_ssl_setup(&m_Ssl, m_Config->GetInternal());
59 mbedtls_ssl_set_bio(&m_Ssl,
this, SendEncrypted, ReceiveEncrypted,
nullptr);
69 int cSslContext::Initialize(
bool a_IsClient)
85 void cSslContext::SetExpectedPeerName(
const std::string_view a_ExpectedPeerName)
88 mbedtls_ssl_set_hostname(&m_Ssl, a_ExpectedPeerName.data());
95 int cSslContext::WritePlain(
const void * a_Data,
size_t a_NumBytes)
100 int res = Handshake();
107 return mbedtls_ssl_write(&m_Ssl,
static_cast<const unsigned char *
>(a_Data), a_NumBytes);
114 int cSslContext::ReadPlain(
void * a_Data,
size_t a_MaxBytes)
117 if (!m_HasHandshaken)
119 int res = Handshake();
126 return mbedtls_ssl_read(&m_Ssl,
static_cast<unsigned char *
>(a_Data), a_MaxBytes);
133 int cSslContext::Handshake(
void)
138 int res = mbedtls_ssl_handshake(&m_Ssl);
141 m_HasHandshaken =
true;
150 int cSslContext::NotifyClose(
void)
152 return mbedtls_ssl_close_notify(&m_Ssl);
void LOGWARNING(std::string_view a_Format, const Args &... args)
static std::shared_ptr< const cSslConfig > GetDefaultServerConfig()
Returns the default config for server connections.
static std::shared_ptr< const cSslConfig > GetDefaultClientConfig()
Returns the default config for client connections.