This commit is contained in:
52
src/sp/extensions/SslContext.cpp
Normal file
52
src/sp/extensions/SslContext.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <mbedtls/ctr_drbg.h>
|
||||
#include <mbedtls/entropy.h>
|
||||
#include <mbedtls/x509_crt.h>
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/common/NonCopyable.h>
|
||||
|
||||
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
|
||||
@@ -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"}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user