diff --git a/src/sp/extensions/SslContext.cpp b/src/sp/extensions/SslContext.cpp new file mode 100644 index 0000000..f90a3c5 --- /dev/null +++ b/src/sp/extensions/SslContext.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace sp { + +class SslContext : private NonCopyable { + public: + SslContext(unsigned int a_KeySizeBits) { + mbedtls_rsa_context rsaContext; + mbedtls_rsa_init(&rsaContext); + mbedtls_rsa_gen_key(&rsaContext, mbedtls_ctr_drbg_random, &m_CtrCrbg, a_KeySizeBits, 65537); + mbedtls_rsa_free(&rsaContext); + } + + SslContext(const DataBuffer& a_Data) { + mbedtls_x509_crt_parse(&m_CaCert, (const unsigned char*)a_Data.data(), a_Data.GetSize()) == 0; + } + + SslContext(const std::string& a_CertFilePath) { + mbedtls_x509_crt_parse_file(&m_CaCert, a_CertFilePath.c_str()); + } + + ~SslContext() { + mbedtls_ctr_drbg_free(&m_CtrCrbg); + mbedtls_entropy_free(&m_Entropy); + mbedtls_x509_crt_free(&m_CaCert); + } + + private: + void InitContext() { + int error = 0; + + mbedtls_x509_crt_init(&m_CaCert); + mbedtls_ctr_drbg_init(&m_CtrCrbg); + + mbedtls_entropy_init(&m_Entropy); + if ((error = mbedtls_ctr_drbg_seed(&m_CtrCrbg, mbedtls_entropy_func, &m_Entropy, nullptr, 0)) != 0) { + throw std::runtime_error("Failed to initialise random number generator. Returned error: " + std::to_string(error)); + } + } + + mbedtls_entropy_context m_Entropy; + mbedtls_ctr_drbg_context m_CtrCrbg; + mbedtls_x509_crt m_CaCert; +}; + +} // namespace sp diff --git a/xmake.lua b/xmake.lua index 1785976..23c787c 100644 --- a/xmake.lua +++ b/xmake.lua @@ -14,6 +14,12 @@ local modules = { Deps = {}, Includes = {"include/(sp/extensions/Tcp.h)", "include/(sp/extensions/tcp/*.h)"}, Sources = {"src/sp/extensions/Tcp*.cpp"} + }, + MbedTls = { + Option = "tls", + Deps = {"mbedtls"}, + Includes = {"include/(sp/extensions/Ssl.h)", "include/(sp/extensions/Ssl/*.h)"}, + Sources = {"src/sp/extensions/Ssl*.cpp"} } }