16 #if !defined(NDEBUG) && defined(ENABLE_SSL_DEBUG_MSG)
17 #include "mbedtls/debug.h"
22 void SSLDebugMessage(
void * a_UserParam,
int a_Level,
const char * a_Filename,
int a_LineNo,
const char * a_Text)
31 size_t len = strlen(a_Text) - 1;
32 while ((len > 0) && (a_Text[len] <= 32))
38 LOGD(
"SSL (%d): %s", a_Level, Text.c_str());
45 int SSLVerifyCert(
void * a_This, mbedtls_x509_crt * a_Crt,
int a_Depth, uint32_t * a_Flags)
50 LOG(
"Verify requested for (Depth %d):", a_Depth);
51 mbedtls_x509_crt_info(buf,
sizeof(buf) - 1,
"", a_Crt);
54 uint32_t Flags = *a_Flags;
55 if ((Flags & MBEDTLS_X509_BADCERT_EXPIRED) != 0)
57 LOG(
" ! server certificate has expired");
60 if ((Flags & MBEDTLS_X509_BADCERT_REVOKED) != 0)
62 LOG(
" ! server certificate has been revoked");
65 if ((Flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) != 0)
67 LOG(
" ! CN mismatch");
70 if ((Flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) != 0)
72 LOG(
" ! self-signed or not signed by a trusted CA");
75 if ((Flags & MBEDTLS_X509_BADCRL_NOT_TRUSTED) != 0)
77 LOG(
" ! CRL not trusted");
80 if ((Flags & MBEDTLS_X509_BADCRL_EXPIRED) != 0)
82 LOG(
" ! CRL expired");
85 if ((Flags & MBEDTLS_X509_BADCERT_OTHER) != 0)
87 LOG(
" ! other (unknown) flag");
92 LOG(
" This certificate has no flags");
126 return mbedtls_ssl_config_defaults(
128 a_IsClient ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
129 MBEDTLS_SSL_TRANSPORT_STREAM,
130 MBEDTLS_SSL_PRESET_DEFAULT
140 const int Mode = [=]()
161 ASSERT(a_CtrDrbg !=
nullptr);
172 mbedtls_ssl_conf_dbg(&
m_Config, a_CallbackFun, a_CallbackData);
181 ASSERT(a_OwnCert !=
nullptr);
182 ASSERT(a_OwnCertPrivKey !=
nullptr);
198 mbedtls_ssl_conf_verify(&
m_Config, a_CallbackFun, a_CallbackData);
228 auto Ret = std::make_shared<cSslConfig>();
230 Ret->InitDefaults(a_IsClient);
233 auto CtrDrbg = std::make_shared<cCtrDrbgContext>();
234 CtrDrbg->Initialize(
"Cuberite", 8);
235 Ret->SetRng(std::move(CtrDrbg));
242 #ifdef ENABLE_SSL_DEBUG_MSG
243 Ret->SetDebugCallback(&SSLDebugMessage,
nullptr);
244 Ret->SetVerifyCallback(SSLVerifyCert,
nullptr);
245 mbedtls_debug_set_threshold(2);
269 static const std::shared_ptr<const cSslConfig> ClientConfig =
MakeDefaultConfig(
true);
279 static const std::shared_ptr<const cSslConfig> ServerConfig =
MakeDefaultConfig(
false);
void LOG(std::string_view a_Format, const Args &... args)
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
std::shared_ptr< cCtrDrbgContext > cCtrDrbgContextPtr
std::shared_ptr< cX509Cert > cX509CertPtr
int(*)(void *, mbedtls_x509_crt *, int, uint32_t *) cVerifyCallback
Type of the SSL certificate verify callback.
std::vector< int > m_CipherSuites
void(*)(void *, int, const char *, int, const char *) cDebugCallback
Type of the SSL debug callback.
void SetRng(cCtrDrbgContextPtr a_CtrDrbg)
Set the random number generator.
static std::shared_ptr< cSslConfig > MakeDefaultConfig(bool a_IsClient)
Creates a new config with some sensible defaults on top of mbedTLS basic settings.
void SetCipherSuites(std::vector< int > a_CipherSuites)
Set the enabled cipher suites.
void SetOwnCert(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnCertPrivKey)
Set the certificate to use for connections.
int InitDefaults(bool a_IsClient)
Initialize with mbedTLS default settings.
static std::shared_ptr< const cSslConfig > GetDefaultServerConfig()
Returns the default config for server connections.
mbedtls_ssl_config m_Config
void SetVerifyCallback(cVerifyCallback a_CallbackFun, void *a_CallbackData)
Set the certificate verify callback.
cCryptoKeyPtr m_OwnCertPrivKey
void SetAuthMode(eSslAuthMode a_AuthMode)
Set the authorization mode.
static std::shared_ptr< const cSslConfig > GetDefaultClientConfig()
Returns the default config for client connections.
void SetDebugCallback(cDebugCallback a_CallbackFun, void *a_CallbackData)
Set the debug callback.
void SetCACerts(cX509CertPtr a_CACert)
Set the trusted certificate authority chain.
cCtrDrbgContextPtr m_CtrDrbg