diff options
Diffstat (limited to '')
-rw-r--r-- | src/PolarSSL++/SslContext.cpp | 307 | ||||
-rw-r--r-- | src/mbedTLS++/AesCfb128Decryptor.cpp (renamed from src/PolarSSL++/AesCfb128Decryptor.cpp) | 24 | ||||
-rw-r--r-- | src/mbedTLS++/AesCfb128Decryptor.h (renamed from src/PolarSSL++/AesCfb128Decryptor.h) | 9 | ||||
-rw-r--r-- | src/mbedTLS++/AesCfb128Encryptor.cpp (renamed from src/PolarSSL++/AesCfb128Encryptor.cpp) | 25 | ||||
-rw-r--r-- | src/mbedTLS++/AesCfb128Encryptor.h (renamed from src/PolarSSL++/AesCfb128Encryptor.h) | 9 | ||||
-rw-r--r-- | src/mbedTLS++/BlockingSslClientSocket.cpp (renamed from src/PolarSSL++/BlockingSslClientSocket.cpp) | 60 | ||||
-rw-r--r-- | src/mbedTLS++/BlockingSslClientSocket.h (renamed from src/PolarSSL++/BlockingSslClientSocket.h) | 23 | ||||
-rw-r--r-- | src/mbedTLS++/BufferedSslContext.cpp (renamed from src/PolarSSL++/BufferedSslContext.cpp) | 14 | ||||
-rw-r--r-- | src/mbedTLS++/BufferedSslContext.h (renamed from src/PolarSSL++/BufferedSslContext.h) | 3 | ||||
-rw-r--r-- | src/mbedTLS++/CMakeLists.txt (renamed from src/PolarSSL++/CMakeLists.txt) | 7 | ||||
-rw-r--r-- | src/mbedTLS++/CallbackSslContext.cpp (renamed from src/PolarSSL++/CallbackSslContext.cpp) | 6 | ||||
-rw-r--r-- | src/mbedTLS++/CallbackSslContext.h (renamed from src/PolarSSL++/CallbackSslContext.h) | 15 | ||||
-rw-r--r-- | src/mbedTLS++/CryptoKey.cpp (renamed from src/PolarSSL++/CryptoKey.cpp) | 28 | ||||
-rw-r--r-- | src/mbedTLS++/CryptoKey.h (renamed from src/PolarSSL++/CryptoKey.h) | 20 | ||||
-rw-r--r-- | src/mbedTLS++/CtrDrbgContext.cpp (renamed from src/PolarSSL++/CtrDrbgContext.cpp) | 10 | ||||
-rw-r--r-- | src/mbedTLS++/CtrDrbgContext.h (renamed from src/PolarSSL++/CtrDrbgContext.h) | 16 | ||||
-rw-r--r-- | src/mbedTLS++/EntropyContext.cpp (renamed from src/PolarSSL++/EntropyContext.cpp) | 8 | ||||
-rw-r--r-- | src/mbedTLS++/EntropyContext.h (renamed from src/PolarSSL++/EntropyContext.h) | 8 | ||||
-rw-r--r-- | src/mbedTLS++/RsaPrivateKey.cpp (renamed from src/PolarSSL++/RsaPrivateKey.cpp) | 38 | ||||
-rw-r--r-- | src/mbedTLS++/RsaPrivateKey.h (renamed from src/PolarSSL++/RsaPrivateKey.h) | 12 | ||||
-rw-r--r-- | src/mbedTLS++/Sha1Checksum.cpp (renamed from src/PolarSSL++/Sha1Checksum.cpp) | 10 | ||||
-rw-r--r-- | src/mbedTLS++/Sha1Checksum.h (renamed from src/PolarSSL++/Sha1Checksum.h) | 6 | ||||
-rw-r--r-- | src/mbedTLS++/SslContext.h (renamed from src/PolarSSL++/SslContext.h) | 90 | ||||
-rw-r--r-- | src/mbedTLS++/X509Cert.cpp (renamed from src/PolarSSL++/X509Cert.cpp) | 10 | ||||
-rw-r--r-- | src/mbedTLS++/X509Cert.h (renamed from src/PolarSSL++/X509Cert.h) | 16 |
25 files changed, 216 insertions, 558 deletions
diff --git a/src/PolarSSL++/SslContext.cpp b/src/PolarSSL++/SslContext.cpp deleted file mode 100644 index 557f81db4..000000000 --- a/src/PolarSSL++/SslContext.cpp +++ /dev/null @@ -1,307 +0,0 @@ - -// SslContext.cpp - -// Implements the cSslContext class that holds everything a single SSL context needs to function - -#include "Globals.h" -#include "SslContext.h" -#include "EntropyContext.h" -#include "CtrDrbgContext.h" -#include "polarssl/debug.h" - - - - - -cSslContext::cSslContext(void) : - m_IsValid(false), - m_HasHandshaken(false) -{ - memset(&m_Ssl, 0, sizeof(m_Ssl)); -} - - - - - -cSslContext::~cSslContext() -{ - if (m_IsValid) - { - ssl_free(&m_Ssl); - } -} - - - - - -int cSslContext::Initialize(bool a_IsClient, const std::shared_ptr<cCtrDrbgContext> & a_CtrDrbg) -{ - // Check double-initialization: - if (m_IsValid) - { - LOGWARNING("SSL: Double initialization is not supported."); - return POLARSSL_ERR_SSL_BAD_INPUT_DATA; // There is no return value well-suited for this, reuse this one. - } - - // Set the CtrDrbg context, create a new one if needed: - m_CtrDrbg = a_CtrDrbg; - if (m_CtrDrbg.get() == nullptr) - { - m_CtrDrbg.reset(new cCtrDrbgContext); - m_CtrDrbg->Initialize("Cuberite", 8); - } - - // Initialize PolarSSL's structures: - memset(&m_Ssl, 0, sizeof(m_Ssl)); - int res = ssl_init(&m_Ssl); - if (res != 0) - { - return res; - } - ssl_set_endpoint(&m_Ssl, a_IsClient ? SSL_IS_CLIENT : SSL_IS_SERVER); - ssl_set_authmode(&m_Ssl, SSL_VERIFY_NONE); // We cannot verify because we don't have a CA chain, required by PolarSSL, implemented yet (TODO) - ssl_set_rng(&m_Ssl, ctr_drbg_random, &m_CtrDrbg->m_CtrDrbg); - ssl_set_bio(&m_Ssl, ReceiveEncrypted, this, SendEncrypted, this); - - #ifdef _DEBUG - /* - // These functions allow us to debug SSL and certificate problems, but produce way too much output, - // so they're disabled until someone needs them - ssl_set_dbg(&m_Ssl, &SSLDebugMessage, this); - debug_set_threshold(2); - - ssl_set_verify(&m_Ssl, &SSLVerifyCert, this); - //*/ - - /* - // Set ciphersuite to the easiest one to decode, so that the connection can be wireshark-decoded: - static const int CipherSuites[] = - { - TLS_RSA_WITH_RC4_128_MD5, - TLS_RSA_WITH_RC4_128_SHA, - TLS_RSA_WITH_AES_128_CBC_SHA, - 0, // Must be 0-terminated! - }; - ssl_set_ciphersuites(&m_Ssl, CipherSuites); - //*/ - #endif - - m_IsValid = true; - return 0; -} - - - - - -void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cRsaPrivateKeyPtr & a_OwnCertPrivKey) -{ - ASSERT(m_IsValid); // Call Initialize() first - - // Check that both the cert and the key is valid: - if ((a_OwnCert.get() == nullptr) || (a_OwnCertPrivKey.get() == nullptr)) - { - LOGWARNING("SSL: Own certificate is not valid, skipping the set."); - return; - } - - // Make sure we have the cert stored for later, PolarSSL only uses the cert later on - m_OwnCert = a_OwnCert; - m_OwnCertPrivKey = a_OwnCertPrivKey; - - // Set into the context: - ssl_set_own_cert_rsa(&m_Ssl, m_OwnCert->GetInternal(), m_OwnCertPrivKey->GetInternal()); -} - - - - - -void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cCryptoKeyPtr & a_OwnCertPrivKey) -{ - ASSERT(m_IsValid); // Call Initialize() first - - // Check that both the cert and the key is valid: - if ((a_OwnCert.get() == nullptr) || (a_OwnCertPrivKey.get() == nullptr)) - { - LOGWARNING("SSL: Own certificate is not valid, skipping the set."); - return; - } - - // Make sure we have the cert stored for later, PolarSSL only uses the cert later on - m_OwnCert = a_OwnCert; - m_OwnCertPrivKey2 = a_OwnCertPrivKey; - - // Set into the context: - ssl_set_own_cert(&m_Ssl, m_OwnCert->GetInternal(), m_OwnCertPrivKey2->GetInternal()); -} - - - - - -void cSslContext::SetCACerts(const cX509CertPtr & a_CACert, const AString & a_ExpectedPeerName) -{ - ASSERT(m_IsValid); // Call Initialize() first - - // Store the data in our internal buffers, to avoid losing the pointers later on - // PolarSSL will need these after this call returns, and the caller may move / delete the data before that: - m_ExpectedPeerName = a_ExpectedPeerName; - m_CACerts = a_CACert; - - // Set the trusted CA root cert store: - ssl_set_authmode(&m_Ssl, SSL_VERIFY_REQUIRED); - ssl_set_ca_chain(&m_Ssl, m_CACerts->GetInternal(), nullptr, m_ExpectedPeerName.empty() ? nullptr : m_ExpectedPeerName.c_str()); -} - - - - - -int cSslContext::WritePlain(const void * a_Data, size_t a_NumBytes) -{ - ASSERT(m_IsValid); // Need to call Initialize() first - if (!m_HasHandshaken) - { - int res = Handshake(); - if (res != 0) - { - return res; - } - } - - return ssl_write(&m_Ssl, reinterpret_cast<const unsigned char *>(a_Data), a_NumBytes); -} - - - - - -int cSslContext::ReadPlain(void * a_Data, size_t a_MaxBytes) -{ - ASSERT(m_IsValid); // Need to call Initialize() first - if (!m_HasHandshaken) - { - int res = Handshake(); - if (res != 0) - { - return res; - } - } - - return ssl_read(&m_Ssl, reinterpret_cast<unsigned char *>(a_Data), a_MaxBytes); -} - - - - - -int cSslContext::Handshake(void) -{ - ASSERT(m_IsValid); // Need to call Initialize() first - ASSERT(!m_HasHandshaken); // Must not call twice - - int res = ssl_handshake(&m_Ssl); - if (res == 0) - { - m_HasHandshaken = true; - } - return res; -} - - - - - -int cSslContext::NotifyClose(void) -{ - return ssl_close_notify(&m_Ssl); -} - - - - - -#ifdef _DEBUG - void cSslContext::SSLDebugMessage(void * a_UserParam, int a_Level, const char * a_Text) - { - if (a_Level > 3) - { - // Don't want the trace messages - return; - } - - // Remove the terminating LF: - size_t len = strlen(a_Text) - 1; - while ((len > 0) && (a_Text[len] <= 32)) - { - len--; - } - AString Text(a_Text, len + 1); - - LOGD("SSL (%d): %s", a_Level, Text.c_str()); - } - - - - - - int cSslContext::SSLVerifyCert(void * a_This, x509_crt * a_Crt, int a_Depth, int * a_Flags) - { - char buf[1024]; - UNUSED(a_This); - - LOG("Verify requested for (Depth %d):", a_Depth); - x509_crt_info(buf, sizeof(buf) - 1, "", a_Crt); - LOG("%s", buf); - - int Flags = *a_Flags; - if ((Flags & BADCERT_EXPIRED) != 0) - { - LOG(" ! server certificate has expired"); - } - - if ((Flags & BADCERT_REVOKED) != 0) - { - LOG(" ! server certificate has been revoked"); - } - - if ((Flags & BADCERT_CN_MISMATCH) != 0) - { - LOG(" ! CN mismatch"); - } - - if ((Flags & BADCERT_NOT_TRUSTED) != 0) - { - LOG(" ! self-signed or not signed by a trusted CA"); - } - - if ((Flags & BADCRL_NOT_TRUSTED) != 0) - { - LOG(" ! CRL not trusted"); - } - - if ((Flags & BADCRL_EXPIRED) != 0) - { - LOG(" ! CRL expired"); - } - - if ((Flags & BADCERT_OTHER) != 0) - { - LOG(" ! other (unknown) flag"); - } - - if (Flags == 0) - { - LOG(" This certificate has no flags"); - } - - return 0; - } -#endif // _DEBUG - - - - diff --git a/src/PolarSSL++/AesCfb128Decryptor.cpp b/src/mbedTLS++/AesCfb128Decryptor.cpp index 0aba1c42c..78a7ab9c5 100644 --- a/src/PolarSSL++/AesCfb128Decryptor.cpp +++ b/src/mbedTLS++/AesCfb128Decryptor.cpp @@ -1,4 +1,4 @@ - + // AesCfb128Decryptor.cpp // Implements the cAesCfb128Decryptor class decrypting data using AES CFB-128 @@ -10,10 +10,10 @@ -cAesCfb128Decryptor::cAesCfb128Decryptor(void) : - m_IVOffset(0), +cAesCfb128Decryptor::cAesCfb128Decryptor(void): m_IsValid(false) { + mbedtls_aes_init(&m_Aes); } @@ -23,7 +23,7 @@ cAesCfb128Decryptor::cAesCfb128Decryptor(void) : cAesCfb128Decryptor::~cAesCfb128Decryptor() { // Clear the leftover in-memory data, so that they can't be accessed by a backdoor - memset(&m_Aes, 0, sizeof(m_Aes)); + mbedtls_aes_free(&m_Aes); } @@ -35,7 +35,7 @@ void cAesCfb128Decryptor::Init(const Byte a_Key[16], const Byte a_IV[16]) ASSERT(!IsValid()); // Cannot Init twice memcpy(m_IV, a_IV, 16); - aes_setkey_enc(&m_Aes, a_Key, 128); + mbedtls_aes_setkey_enc(&m_Aes, a_Key, 128); m_IsValid = true; } @@ -46,19 +46,7 @@ void cAesCfb128Decryptor::Init(const Byte a_Key[16], const Byte a_IV[16]) void cAesCfb128Decryptor::ProcessData(Byte * a_DecryptedOut, const Byte * a_EncryptedIn, size_t a_Length) { ASSERT(IsValid()); // Must Init() first - - // PolarSSL doesn't support AES-CFB8, need to implement it manually: - for (size_t i = 0; i < a_Length; i++) - { - Byte Buffer[sizeof(m_IV)]; - aes_crypt_ecb(&m_Aes, AES_ENCRYPT, m_IV, Buffer); - for (size_t idx = 0; idx < sizeof(m_IV) - 1; idx++) - { - m_IV[idx] = m_IV[idx + 1]; - } - m_IV[sizeof(m_IV) - 1] = a_EncryptedIn[i]; - a_DecryptedOut[i] = a_EncryptedIn[i] ^ Buffer[0]; - } + mbedtls_aes_crypt_cfb8(&m_Aes, MBEDTLS_AES_DECRYPT, a_Length, m_IV, a_EncryptedIn, a_DecryptedOut); } diff --git a/src/PolarSSL++/AesCfb128Decryptor.h b/src/mbedTLS++/AesCfb128Decryptor.h index 56b96d3b3..54c5536ea 100644 --- a/src/PolarSSL++/AesCfb128Decryptor.h +++ b/src/mbedTLS++/AesCfb128Decryptor.h @@ -1,4 +1,4 @@ - + // AesCfb128Decryptor.h // Declares the cAesCfb128Decryptor class decrypting data using AES CFB-128 @@ -9,7 +9,7 @@ #pragma once -#include "polarssl/aes.h" +#include "mbedtls/aes.h" @@ -33,14 +33,11 @@ public: bool IsValid(void) const { return m_IsValid; } protected: - aes_context m_Aes; + mbedtls_aes_context m_Aes; /** The InitialVector, used by the CFB mode decryption */ Byte m_IV[16]; - /** Current offset in the m_IV, used by the CFB mode decryption */ - size_t m_IVOffset; - /** Indicates whether the object has been initialized with the Key / IV */ bool m_IsValid; } ; diff --git a/src/PolarSSL++/AesCfb128Encryptor.cpp b/src/mbedTLS++/AesCfb128Encryptor.cpp index ac0262e69..11582fc19 100644 --- a/src/PolarSSL++/AesCfb128Encryptor.cpp +++ b/src/mbedTLS++/AesCfb128Encryptor.cpp @@ -1,4 +1,4 @@ - + // AesCfb128Encryptor.cpp // Implements the cAesCfb128Encryptor class encrypting data using AES CFB-128 @@ -10,10 +10,10 @@ -cAesCfb128Encryptor::cAesCfb128Encryptor(void) : - m_IVOffset(0), +cAesCfb128Encryptor::cAesCfb128Encryptor(void): m_IsValid(false) { + mbedtls_aes_init(&m_Aes); } @@ -23,7 +23,7 @@ cAesCfb128Encryptor::cAesCfb128Encryptor(void) : cAesCfb128Encryptor::~cAesCfb128Encryptor() { // Clear the leftover in-memory data, so that they can't be accessed by a backdoor - memset(&m_Aes, 0, sizeof(m_Aes)); + mbedtls_aes_free(&m_Aes); } @@ -33,10 +33,9 @@ cAesCfb128Encryptor::~cAesCfb128Encryptor() void cAesCfb128Encryptor::Init(const Byte a_Key[16], const Byte a_IV[16]) { ASSERT(!IsValid()); // Cannot Init twice - ASSERT(m_IVOffset == 0); memcpy(m_IV, a_IV, 16); - aes_setkey_enc(&m_Aes, a_Key, 128); + mbedtls_aes_setkey_enc(&m_Aes, a_Key, 128); m_IsValid = true; } @@ -47,19 +46,7 @@ void cAesCfb128Encryptor::Init(const Byte a_Key[16], const Byte a_IV[16]) void cAesCfb128Encryptor::ProcessData(Byte * a_EncryptedOut, const Byte * a_PlainIn, size_t a_Length) { ASSERT(IsValid()); // Must Init() first - - // PolarSSL doesn't do AES-CFB8, so we need to implement it ourselves: - for (size_t i = 0; i < a_Length; i++) - { - Byte Buffer[sizeof(m_IV)]; - aes_crypt_ecb(&m_Aes, AES_ENCRYPT, m_IV, Buffer); - for (size_t idx = 0; idx < sizeof(m_IV) - 1; idx++) - { - m_IV[idx] = m_IV[idx + 1]; - } - a_EncryptedOut[i] = a_PlainIn[i] ^ Buffer[0]; - m_IV[sizeof(m_IV) - 1] = a_EncryptedOut[i]; - } + mbedtls_aes_crypt_cfb8(&m_Aes, MBEDTLS_AES_ENCRYPT, a_Length, m_IV, a_PlainIn, a_EncryptedOut); } diff --git a/src/PolarSSL++/AesCfb128Encryptor.h b/src/mbedTLS++/AesCfb128Encryptor.h index 71280a098..6bfa6b5c9 100644 --- a/src/PolarSSL++/AesCfb128Encryptor.h +++ b/src/mbedTLS++/AesCfb128Encryptor.h @@ -1,4 +1,4 @@ - + // AesCfb128Encryptor.h // Declares the cAesCfb128Encryptor class encrypting data using AES CFB-128 @@ -9,7 +9,7 @@ #pragma once -#include "polarssl/aes.h" +#include "mbedtls/aes.h" @@ -32,14 +32,11 @@ public: bool IsValid(void) const { return m_IsValid; } protected: - aes_context m_Aes; + mbedtls_aes_context m_Aes; /** The InitialVector, used by the CFB mode encryption */ Byte m_IV[16]; - /** Current offset in the m_IV, used by the CFB mode encryption */ - size_t m_IVOffset; - /** Indicates whether the object has been initialized with the Key / IV */ bool m_IsValid; } ; diff --git a/src/PolarSSL++/BlockingSslClientSocket.cpp b/src/mbedTLS++/BlockingSslClientSocket.cpp index 7d7fc4ccf..6f765f607 100644 --- a/src/PolarSSL++/BlockingSslClientSocket.cpp +++ b/src/mbedTLS++/BlockingSslClientSocket.cpp @@ -1,4 +1,4 @@ - + // BlockingSslClientSocket.cpp // Implements the cBlockingSslClientSocket class representing a blocking TCP socket with client SSL encryption over it @@ -125,7 +125,16 @@ bool cBlockingSslClientSocket::Connect(const AString & a_ServerName, UInt16 a_Po } // Initialize the SSL: - int ret = m_Ssl.Initialize(true); + int ret = 0; + if (m_Config != nullptr) + { + ret = m_Ssl.Initialize(m_Config); + } + else + { + ret = m_Ssl.Initialize(true); + } + if (ret != 0) { Printf(m_LastErrorText, "SSL initialization failed: -0x%x", -ret); @@ -133,9 +142,9 @@ bool cBlockingSslClientSocket::Connect(const AString & a_ServerName, UInt16 a_Po } // If we have been assigned a trusted CA root cert store, push it into the SSL context: - if (m_CACerts.get() != nullptr) + if (!m_ExpectedPeerName.empty()) { - m_Ssl.SetCACerts(m_CACerts, m_ExpectedPeerName); + m_Ssl.SetExpectedPeerName(m_ExpectedPeerName); } ret = m_Ssl.Handshake(); @@ -153,28 +162,37 @@ bool cBlockingSslClientSocket::Connect(const AString & a_ServerName, UInt16 a_Po -bool cBlockingSslClientSocket::SetTrustedRootCertsFromString(const AString & a_CACerts, const AString & a_ExpectedPeerName) +void cBlockingSslClientSocket::SetExpectedPeerName(AString a_ExpectedPeerName) { + ASSERT(!m_IsConnected); // Must be called before connect + // Warn if used multiple times, but don't signal an error: - if (m_CACerts.get() != nullptr) + if (!m_ExpectedPeerName.empty()) { LOGWARNING( - "SSL: Trying to set multiple trusted CA root cert stores, only the last one will be used. Name: %s", + "SSL: Trying to set multiple expected peer names, only the last one will be used. Name: %s", a_ExpectedPeerName.c_str() ); } - // Parse the cert: - m_CACerts.reset(new cX509Cert); - int ret = m_CACerts->Parse(a_CACerts.data(), a_CACerts.size()); - if (ret < 0) + m_ExpectedPeerName = std::move(a_ExpectedPeerName); +} + + + + + +void cBlockingSslClientSocket::SetSslConfig(std::shared_ptr<const cSslConfig> a_Config) +{ + ASSERT(!m_IsConnected); // Must be called before connect + + // Warn if used multiple times, but don't signal an error: + if (m_Config != nullptr) { - Printf(m_LastErrorText, "CA cert parsing failed: -0x%x", -ret); - return false; + LOGWARNING("SSL: Trying to set multiple configurations, only the last one will be used."); } - m_ExpectedPeerName = a_ExpectedPeerName; - return true; + m_Config = std::move(a_Config); } @@ -197,8 +215,8 @@ bool cBlockingSslClientSocket::Send(const void * a_Data, size_t a_NumBytes) int res = m_Ssl.WritePlain(Data, a_NumBytes); if (res < 0) { - ASSERT(res != POLARSSL_ERR_NET_WANT_READ); // This should never happen with callback-based SSL - ASSERT(res != POLARSSL_ERR_NET_WANT_WRITE); // This should never happen with callback-based SSL + ASSERT(res != MBEDTLS_ERR_SSL_WANT_READ); // This should never happen with callback-based SSL + ASSERT(res != MBEDTLS_ERR_SSL_WANT_WRITE); // This should never happen with callback-based SSL Printf(m_LastErrorText, "Data cannot be written to SSL context: -0x%x", -res); return false; } @@ -272,7 +290,7 @@ int cBlockingSslClientSocket::ReceiveEncrypted(unsigned char * a_Buffer, size_t // If we got disconnected, report an error after processing all data: if (!m_IsConnected && m_IncomingData.empty()) { - return POLARSSL_ERR_NET_RECV_FAILED; + return MBEDTLS_ERR_NET_RECV_FAILED; } // Copy the data from the incoming buffer into the specified space: @@ -291,12 +309,12 @@ int cBlockingSslClientSocket::SendEncrypted(const unsigned char * a_Buffer, size cTCPLinkPtr Socket(m_Socket); // Make a copy so that multiple threads don't race on deleting the socket. if (Socket == nullptr) { - return POLARSSL_ERR_NET_SEND_FAILED; + return MBEDTLS_ERR_NET_SEND_FAILED; } if (!Socket->Send(a_Buffer, a_NumBytes)) { - // PolarSSL's net routines distinguish between connection reset and general failure, we don't need to - return POLARSSL_ERR_NET_SEND_FAILED; + // mbedTLS's net routines distinguish between connection reset and general failure, we don't need to + return MBEDTLS_ERR_NET_SEND_FAILED; } return static_cast<int>(a_NumBytes); } diff --git a/src/PolarSSL++/BlockingSslClientSocket.h b/src/mbedTLS++/BlockingSslClientSocket.h index 651d750e6..24ee32680 100644 --- a/src/PolarSSL++/BlockingSslClientSocket.h +++ b/src/mbedTLS++/BlockingSslClientSocket.h @@ -1,4 +1,4 @@ - + // BlockingSslClientSocket.h // Declares the cBlockingSslClientSocket class representing a blocking TCP socket with client SSL encryption over it @@ -45,12 +45,15 @@ public: Note that this also frees the internal SSL context, so all the certificates etc. are lost. */ void Disconnect(void); - /** Sets the root certificates that are to be trusted. Forces the connection to use strict cert - verification. Needs to be used before calling Connect(). - a_ExpectedPeerName is the name that we expect to receive in the SSL peer's cert; verification will fail if - the presented name is different (possible MITM). - Returns true on success, false on failure. Sets internal error text on failure. */ - bool SetTrustedRootCertsFromString(const AString & a_CACerts, const AString & a_ExpectedPeerName); + /** Sets the Expected peer name. + Needs to be used before calling Connect(). + \param a_ExpectedPeerName Name that we expect to receive in the SSL peer's cert; verification will fail if + the presented name is different (possible MITM). */ + void SetExpectedPeerName(AString a_ExpectedPeerName); + + /** Set the config to be used by the SSL context. + Config must not be modified after calling connect. */ + void SetSslConfig(std::shared_ptr<const cSslConfig> a_Config); /** Returns the text of the last error that has occurred in this instance. */ const AString & GetLastErrorText(void) const { return m_LastErrorText; } @@ -68,10 +71,10 @@ protected: /** The object used to signal state changes in the socket (the cause of the blocking). */ cEvent m_Event; - /** The trusted CA root cert store, if we are to verify the cert strictly. Set by SetTrustedRootCertsFromString(). */ - cX509CertPtr m_CACerts; + /** The configuration to be used by the SSL context. Set by SetSslConfig(). */ + std::shared_ptr<const cSslConfig> m_Config; - /** The expected SSL peer's name, if we are to verify the cert strictly. Set by SetTrustedRootCertsFromString(). */ + /** The expected SSL peer's name, if we are to verify the cert strictly. Set by SetExpectedPeerName(). */ AString m_ExpectedPeerName; /** The hostname to which the socket is connecting (stored for error reporting). */ diff --git a/src/PolarSSL++/BufferedSslContext.cpp b/src/mbedTLS++/BufferedSslContext.cpp index c8d4736f7..5cdf04323 100644 --- a/src/PolarSSL++/BufferedSslContext.cpp +++ b/src/mbedTLS++/BufferedSslContext.cpp @@ -1,4 +1,4 @@ - + // BufferedSslContext.cpp // Implements the cBufferedSslContext class representing a SSL context with the SSL peer data backed by a cByteBuffer @@ -53,17 +53,17 @@ size_t cBufferedSslContext::ReadOutgoing(void * a_Data, size_t a_DataMaxSize) int cBufferedSslContext::ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) { - // Called when PolarSSL wants to read encrypted data from the SSL peer + // Called when mbedTLS wants to read encrypted data from the SSL peer // Read the data from the buffer inside this object, where the owner has stored them using WriteIncoming(): size_t NumBytes = std::min(a_NumBytes, m_IncomingData.GetReadableSpace()); if (NumBytes == 0) { - return POLARSSL_ERR_NET_WANT_READ; + return MBEDTLS_ERR_SSL_WANT_READ; } if (!m_IncomingData.ReadBuf(a_Buffer, NumBytes)) { m_IncomingData.ResetRead(); - return POLARSSL_ERR_NET_RECV_FAILED; + return MBEDTLS_ERR_NET_RECV_FAILED; } m_IncomingData.CommitRead(); return static_cast<int>(NumBytes); @@ -75,15 +75,15 @@ int cBufferedSslContext::ReceiveEncrypted(unsigned char * a_Buffer, size_t a_Num int cBufferedSslContext::SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes) { - // Called when PolarSSL wants to write encrypted data to the SSL peer + // Called when mbedTLS wants to write encrypted data to the SSL peer // Write the data into the buffer inside this object, where the owner can later read them using ReadOutgoing(): if (!m_OutgoingData.CanWriteBytes(a_NumBytes)) { - return POLARSSL_ERR_NET_WANT_WRITE; + return MBEDTLS_ERR_SSL_WANT_WRITE; } if (!m_OutgoingData.Write(reinterpret_cast<const char *>(a_Buffer), a_NumBytes)) { - return POLARSSL_ERR_NET_SEND_FAILED; + return MBEDTLS_ERR_NET_SEND_FAILED; } return static_cast<int>(a_NumBytes); } diff --git a/src/PolarSSL++/BufferedSslContext.h b/src/mbedTLS++/BufferedSslContext.h index ab058a52e..9c9dd8f73 100644 --- a/src/PolarSSL++/BufferedSslContext.h +++ b/src/mbedTLS++/BufferedSslContext.h @@ -1,4 +1,4 @@ - + // BufferedSslContext.h // Declares the cBufferedSslContext class representing a SSL context with the SSL peer data backed by a cByteBuffer @@ -10,6 +10,7 @@ #pragma once #include "SslContext.h" +#include "ErrorCodes.h" diff --git a/src/PolarSSL++/CMakeLists.txt b/src/mbedTLS++/CMakeLists.txt index 3d77e15d1..18ef22312 100644 --- a/src/PolarSSL++/CMakeLists.txt +++ b/src/mbedTLS++/CMakeLists.txt @@ -13,6 +13,7 @@ set(SRCS EntropyContext.cpp RsaPrivateKey.cpp Sha1Checksum.cpp + SslConfig.cpp SslContext.cpp X509Cert.cpp ) @@ -26,16 +27,18 @@ set(HDRS CtrDrbgContext.h CryptoKey.h EntropyContext.h + ErrorCodes.h RsaPrivateKey.h + SslConfig.h SslContext.h Sha1Checksum.h X509Cert.h ) if(NOT MSVC) - add_library(PolarSSL++ ${SRCS} ${HDRS}) + add_library(mbedTLS++ ${SRCS} ${HDRS}) if (UNIX) - target_link_libraries(PolarSSL++ mbedtls) + target_link_libraries(mbedTLS++ mbedtls) endif() endif() diff --git a/src/PolarSSL++/CallbackSslContext.cpp b/src/mbedTLS++/CallbackSslContext.cpp index e061e3f03..26bcec2ff 100644 --- a/src/PolarSSL++/CallbackSslContext.cpp +++ b/src/mbedTLS++/CallbackSslContext.cpp @@ -1,4 +1,4 @@ - + // CallbackSslContext.cpp // Declares the cCallbackSslContext class representing a SSL context wrapper that uses callbacks to read and write SSL peer data @@ -35,7 +35,7 @@ int cCallbackSslContext::ReceiveEncrypted(unsigned char * a_Buffer, size_t a_Num if (m_Callbacks == nullptr) { LOGWARNING("SSL: Trying to receive data with no callbacks, aborting."); - return POLARSSL_ERR_NET_RECV_FAILED; + return MBEDTLS_ERR_NET_RECV_FAILED; } return m_Callbacks->ReceiveEncrypted(a_Buffer, a_NumBytes); } @@ -49,7 +49,7 @@ int cCallbackSslContext::SendEncrypted(const unsigned char * a_Buffer, size_t a_ if (m_Callbacks == nullptr) { LOGWARNING("SSL: Trying to send data with no callbacks, aborting."); - return POLARSSL_ERR_NET_SEND_FAILED; + return MBEDTLS_ERR_NET_SEND_FAILED; } return m_Callbacks->SendEncrypted(a_Buffer, a_NumBytes); } diff --git a/src/PolarSSL++/CallbackSslContext.h b/src/mbedTLS++/CallbackSslContext.h index 1fc131182..da1abb707 100644 --- a/src/PolarSSL++/CallbackSslContext.h +++ b/src/mbedTLS++/CallbackSslContext.h @@ -1,4 +1,4 @@ - + // CallbackSslContext.h // Declares the cCallbackSslContext class representing a SSL context wrapper that uses callbacks to read and write SSL peer data @@ -10,6 +10,7 @@ #pragma once #include "SslContext.h" +#include "ErrorCodes.h" @@ -26,17 +27,17 @@ public: // Force a virtual destructor in descendants: virtual ~cDataCallbacks() {} - /** Called when PolarSSL wants to read encrypted data from the SSL peer. - The returned value is the number of bytes received, or a PolarSSL error on failure. - The implementation can return POLARSSL_ERR_NET_WANT_READ or POLARSSL_ERR_NET_WANT_WRITE to indicate + /** Called when mbedTLS wants to read encrypted data from the SSL peer. + The returned value is the number of bytes received, or a mbedTLS error on failure. + The implementation can return MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE to indicate that there's currently no more data and that there might be more data in the future. In such cases the SSL operation that invoked this call will terminate with the same return value, so that the owner is notified of this condition and can potentially restart the operation later on. */ virtual int ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) = 0; - /** Called when PolarSSL wants to write encrypted data to the SSL peer. - The returned value is the number of bytes sent, or a PolarSSL error on failure. - The implementation can return POLARSSL_ERR_NET_WANT_READ or POLARSSL_ERR_NET_WANT_WRITE to indicate + /** Called when mbedTLS wants to write encrypted data to the SSL peer. + The returned value is the number of bytes sent, or a mbedTLS error on failure. + The implementation can return MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE to indicate that there's currently no more data and that there might be more data in the future. In such cases the SSL operation that invoked this call will terminate with the same return value, so that the owner is notified of this condition and can potentially restart the operation later on. */ diff --git a/src/PolarSSL++/CryptoKey.cpp b/src/mbedTLS++/CryptoKey.cpp index b01fee5f9..4ebb0f300 100644 --- a/src/PolarSSL++/CryptoKey.cpp +++ b/src/mbedTLS++/CryptoKey.cpp @@ -1,7 +1,7 @@ - + // CryptoKey.cpp -// Implements the cCryptoKey class representing a RSA public key in PolarSSL +// Implements the cCryptoKey class representing a RSA public key in mbedTLS #include "Globals.h" #include "CryptoKey.h" @@ -12,7 +12,7 @@ cCryptoKey::cCryptoKey(void) { - pk_init(&m_Pk); + mbedtls_pk_init(&m_Pk); m_CtrDrbg.Initialize("rsa_pubkey", 10); } @@ -22,7 +22,7 @@ cCryptoKey::cCryptoKey(void) cCryptoKey::cCryptoKey(const AString & a_PublicKeyData) { - pk_init(&m_Pk); + mbedtls_pk_init(&m_Pk); m_CtrDrbg.Initialize("rsa_pubkey", 10); int res = ParsePublic(a_PublicKeyData.data(), a_PublicKeyData.size()); if (res != 0) @@ -39,7 +39,7 @@ cCryptoKey::cCryptoKey(const AString & a_PublicKeyData) cCryptoKey::cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Password) { - pk_init(&m_Pk); + mbedtls_pk_init(&m_Pk); m_CtrDrbg.Initialize("rsa_privkey", 11); int res = ParsePrivate(a_PrivateKeyData.data(), a_PrivateKeyData.size(), a_Password); if (res != 0) @@ -56,7 +56,7 @@ cCryptoKey::cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Passw cCryptoKey::~cCryptoKey() { - pk_free(&m_Pk); + mbedtls_pk_free(&m_Pk); } @@ -68,10 +68,10 @@ int cCryptoKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, ASSERT(IsValid()); size_t DecryptedLen = a_DecryptedMaxLength; - int res = pk_decrypt(&m_Pk, + int res = mbedtls_pk_decrypt(&m_Pk, a_EncryptedData, a_EncryptedLength, a_DecryptedData, &DecryptedLen, a_DecryptedMaxLength, - ctr_drbg_random, m_CtrDrbg.GetInternal() + mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal() ); if (res != 0) { @@ -89,9 +89,9 @@ int cCryptoKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a ASSERT(IsValid()); size_t EncryptedLength = a_EncryptedMaxLength; - int res = pk_encrypt(&m_Pk, + int res = mbedtls_pk_encrypt(&m_Pk, a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength, - ctr_drbg_random, m_CtrDrbg.GetInternal() + mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal() ); if (res != 0) { @@ -109,7 +109,7 @@ int cCryptoKey::ParsePublic(const void * a_Data, size_t a_NumBytes) { ASSERT(!IsValid()); // Cannot parse a second key - return pk_parse_public_key(&m_Pk, reinterpret_cast<const unsigned char *>(a_Data), a_NumBytes); + return mbedtls_pk_parse_public_key(&m_Pk, reinterpret_cast<const unsigned char *>(a_Data), a_NumBytes); } @@ -123,11 +123,11 @@ int cCryptoKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AStri if (a_Password.empty()) { - return pk_parse_key(&m_Pk, reinterpret_cast<const unsigned char *>(a_Data), a_NumBytes, nullptr, 0); + return mbedtls_pk_parse_key(&m_Pk, reinterpret_cast<const unsigned char *>(a_Data), a_NumBytes, nullptr, 0); } else { - return pk_parse_key( + return mbedtls_pk_parse_key( &m_Pk, reinterpret_cast<const unsigned char *>(a_Data), a_NumBytes, reinterpret_cast<const unsigned char *>(a_Password.c_str()), a_Password.size() @@ -141,7 +141,7 @@ int cCryptoKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AStri bool cCryptoKey::IsValid(void) const { - return (pk_get_type(&m_Pk) != POLARSSL_PK_NONE); + return (mbedtls_pk_get_type(&m_Pk) != MBEDTLS_PK_NONE); } diff --git a/src/PolarSSL++/CryptoKey.h b/src/mbedTLS++/CryptoKey.h index db15cefb4..1a74090ac 100644 --- a/src/PolarSSL++/CryptoKey.h +++ b/src/mbedTLS++/CryptoKey.h @@ -1,7 +1,7 @@ - + // CryptoKey.h -// Declares the cCryptoKey class representing a RSA public key in PolarSSL +// Declares the cCryptoKey class representing a RSA public key in mbedTLS @@ -10,7 +10,7 @@ #pragma once #include "CtrDrbgContext.h" -#include "polarssl/pk.h" +#include "mbedtls/pk.h" @@ -18,7 +18,7 @@ class cCryptoKey { - friend class cSslContext; + friend class cSslConfig; public: /** Constructs an empty key instance. Before use, it needs to be filled by ParsePublic() or ParsePrivate() */ @@ -45,28 +45,28 @@ public: /** Parses the specified data into a public key representation. The key can be DER- or PEM-encoded. - Returns 0 on success, PolarSSL error code on failure. */ + Returns 0 on success, mbedTLS error code on failure. */ int ParsePublic(const void * a_Data, size_t a_NumBytes); /** Parses the specified data into a private key representation. If a_Password is empty, no password is assumed. The key can be DER- or PEM-encoded. - Returns 0 on success, PolarSSL error code on failure. */ + Returns 0 on success, mbedTLS error code on failure. */ int ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password); /** Returns true if the contained key is valid. */ bool IsValid(void) const; protected: - /** The PolarSSL representation of the key data */ - pk_context m_Pk; + /** The mbedTLS representation of the key data */ + mbedtls_pk_context m_Pk; /** The random generator used in encryption and decryption */ cCtrDrbgContext m_CtrDrbg; - /** Returns the internal context ptr. Only use in PolarSSL API calls. */ - pk_context * GetInternal(void) { return &m_Pk; } + /** Returns the internal context ptr. Only use in mbedTLS API calls. */ + mbedtls_pk_context * GetInternal(void) { return &m_Pk; } } ; typedef std::shared_ptr<cCryptoKey> cCryptoKeyPtr; diff --git a/src/PolarSSL++/CtrDrbgContext.cpp b/src/mbedTLS++/CtrDrbgContext.cpp index c2243db38..bd4a55000 100644 --- a/src/PolarSSL++/CtrDrbgContext.cpp +++ b/src/mbedTLS++/CtrDrbgContext.cpp @@ -1,7 +1,7 @@ - + // CtrDrbgContext.cpp -// Implements the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in PolarSSL +// Implements the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in mbedTLS #include "Globals.h" #include "CtrDrbgContext.h" @@ -12,9 +12,10 @@ cCtrDrbgContext::cCtrDrbgContext(void) : - m_EntropyContext(new cEntropyContext), + m_EntropyContext(std::make_shared<cEntropyContext>()), m_IsValid(false) { + mbedtls_ctr_drbg_init(&m_CtrDrbg); } @@ -25,6 +26,7 @@ cCtrDrbgContext::cCtrDrbgContext(const std::shared_ptr<cEntropyContext> & a_Entr m_EntropyContext(a_EntropyContext), m_IsValid(false) { + mbedtls_ctr_drbg_init(&m_CtrDrbg); } @@ -39,7 +41,7 @@ int cCtrDrbgContext::Initialize(const void * a_Custom, size_t a_CustomSize) return 0; } - int res = ctr_drbg_init(&m_CtrDrbg, entropy_func, &(m_EntropyContext->m_Entropy), reinterpret_cast<const unsigned char *>(a_Custom), a_CustomSize); + int res = mbedtls_ctr_drbg_seed(&m_CtrDrbg, mbedtls_entropy_func, &(m_EntropyContext->m_Entropy), reinterpret_cast<const unsigned char *>(a_Custom), a_CustomSize); m_IsValid = (res == 0); return res; } diff --git a/src/PolarSSL++/CtrDrbgContext.h b/src/mbedTLS++/CtrDrbgContext.h index 20d687015..21d786c2e 100644 --- a/src/PolarSSL++/CtrDrbgContext.h +++ b/src/mbedTLS++/CtrDrbgContext.h @@ -1,7 +1,7 @@ - + // CtrDrbgContext.h -// Declares the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in PolarSSL +// Declares the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in mbedTLS @@ -9,7 +9,7 @@ #pragma once -#include "polarssl/ctr_drbg.h" +#include "mbedtls/ctr_drbg.h" @@ -24,7 +24,7 @@ class cEntropyContext; class cCtrDrbgContext { - friend class cSslContext; + friend class cSslConfig; friend class cRsaPrivateKey; friend class cCryptoKey; @@ -37,7 +37,7 @@ public: /** Initializes the context. a_Custom is optional additional data to use for entropy, nullptr is accepted. - Returns 0 if successful, PolarSSL error code on failure. */ + Returns 0 if successful, mbedTLS error code on failure. */ int Initialize(const void * a_Custom, size_t a_CustomSize); /** Returns true if the object is valid (has been initialized properly) */ @@ -48,14 +48,14 @@ protected: std::shared_ptr<cEntropyContext> m_EntropyContext; /** The random generator context */ - ctr_drbg_context m_CtrDrbg; + mbedtls_ctr_drbg_context m_CtrDrbg; /** Set to true if the object is valid (has been initialized properly) */ bool m_IsValid; - /** Returns the internal context ptr. Only use in PolarSSL API calls. */ - ctr_drbg_context * GetInternal(void) { return &m_CtrDrbg; } + /** Returns the internal context ptr. Only use in mbedTLS API calls. */ + mbedtls_ctr_drbg_context * GetInternal(void) { return &m_CtrDrbg; } } ; diff --git a/src/PolarSSL++/EntropyContext.cpp b/src/mbedTLS++/EntropyContext.cpp index 9c59b3f11..aea056f4e 100644 --- a/src/PolarSSL++/EntropyContext.cpp +++ b/src/mbedTLS++/EntropyContext.cpp @@ -1,7 +1,7 @@ - + // EntropyContext.cpp -// Implements the cEntropyContext class representing a wrapper over entropy contexts in PolarSSL +// Implements the cEntropyContext class representing a wrapper over entropy contexts in mbedTLS #include "Globals.h" #include "EntropyContext.h" @@ -12,7 +12,7 @@ cEntropyContext::cEntropyContext(void) { - entropy_init(&m_Entropy); + mbedtls_entropy_init(&m_Entropy); } @@ -21,7 +21,7 @@ cEntropyContext::cEntropyContext(void) cEntropyContext::~cEntropyContext() { - entropy_free(&m_Entropy); + mbedtls_entropy_free(&m_Entropy); } diff --git a/src/PolarSSL++/EntropyContext.h b/src/mbedTLS++/EntropyContext.h index 69671d32f..37b6f120e 100644 --- a/src/PolarSSL++/EntropyContext.h +++ b/src/mbedTLS++/EntropyContext.h @@ -1,7 +1,7 @@ - + // EntropyContext.h -// Declares the cEntropyContext class representing a wrapper over entropy contexts in PolarSSL +// Declares the cEntropyContext class representing a wrapper over entropy contexts in mbedTLS @@ -9,7 +9,7 @@ #pragma once -#include "polarssl/entropy.h" +#include "mbedtls/entropy.h" @@ -23,7 +23,7 @@ public: ~cEntropyContext(); protected: - entropy_context m_Entropy; + mbedtls_entropy_context m_Entropy; } ; diff --git a/src/PolarSSL++/RsaPrivateKey.cpp b/src/mbedTLS++/RsaPrivateKey.cpp index 5705b01f2..3dfb3bac3 100644 --- a/src/PolarSSL++/RsaPrivateKey.cpp +++ b/src/mbedTLS++/RsaPrivateKey.cpp @@ -1,9 +1,9 @@ - + // RsaPrivateKey.cpp #include "Globals.h" #include "RsaPrivateKey.h" -#include <polarssl/pk.h> +#include "mbedtls/pk.h" @@ -11,7 +11,7 @@ cRsaPrivateKey::cRsaPrivateKey(void) { - rsa_init(&m_Rsa, RSA_PKCS_V15, 0); + mbedtls_rsa_init(&m_Rsa, MBEDTLS_RSA_PKCS_V15, 0); m_CtrDrbg.Initialize("RSA", 3); } @@ -21,8 +21,8 @@ cRsaPrivateKey::cRsaPrivateKey(void) cRsaPrivateKey::cRsaPrivateKey(const cRsaPrivateKey & a_Other) { - rsa_init(&m_Rsa, RSA_PKCS_V15, 0); - rsa_copy(&m_Rsa, &a_Other.m_Rsa); + mbedtls_rsa_init(&m_Rsa, MBEDTLS_RSA_PKCS_V15, 0); + mbedtls_rsa_copy(&m_Rsa, &a_Other.m_Rsa); m_CtrDrbg.Initialize("RSA", 3); } @@ -32,7 +32,7 @@ cRsaPrivateKey::cRsaPrivateKey(const cRsaPrivateKey & a_Other) cRsaPrivateKey::~cRsaPrivateKey() { - rsa_free(&m_Rsa); + mbedtls_rsa_free(&m_Rsa); } @@ -41,7 +41,7 @@ cRsaPrivateKey::~cRsaPrivateKey() bool cRsaPrivateKey::Generate(unsigned a_KeySizeBits) { - int res = rsa_gen_key(&m_Rsa, ctr_drbg_random, m_CtrDrbg.GetInternal(), a_KeySizeBits, 65537); + int res = mbedtls_rsa_gen_key(&m_Rsa, mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal(), a_KeySizeBits, 65537); if (res != 0) { LOG("RSA key generation failed: -0x%x", -res); @@ -60,16 +60,16 @@ AString cRsaPrivateKey::GetPubKeyDER(void) class cPubKey { public: - cPubKey(rsa_context * a_Rsa) : + cPubKey(mbedtls_rsa_context * a_Rsa) : m_IsValid(false) { - pk_init(&m_Key); - if (pk_init_ctx(&m_Key, pk_info_from_type(POLARSSL_PK_RSA)) != 0) + mbedtls_pk_init(&m_Key); + if (mbedtls_pk_setup(&m_Key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) != 0) { ASSERT(!"Cannot init PrivKey context"); return; } - if (rsa_copy(pk_rsa(m_Key), a_Rsa) != 0) + if (mbedtls_rsa_copy(mbedtls_pk_rsa(m_Key), a_Rsa) != 0) { ASSERT(!"Cannot copy PrivKey to PK context"); return; @@ -81,19 +81,19 @@ AString cRsaPrivateKey::GetPubKeyDER(void) { if (m_IsValid) { - pk_free(&m_Key); + mbedtls_pk_free(&m_Key); } } - operator pk_context * (void) { return &m_Key; } + operator mbedtls_pk_context * (void) { return &m_Key; } protected: bool m_IsValid; - pk_context m_Key; + mbedtls_pk_context m_Key; } PkCtx(&m_Rsa); unsigned char buf[3000]; - int res = pk_write_pubkey_der(PkCtx, buf, sizeof(buf)); + int res = mbedtls_pk_write_pubkey_der(PkCtx, buf, sizeof(buf)); if (res < 0) { return AString(); @@ -124,8 +124,8 @@ int cRsaPrivateKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLeng return -1; } size_t DecryptedLength; - int res = rsa_pkcs1_decrypt( - &m_Rsa, ctr_drbg_random, m_CtrDrbg.GetInternal(), RSA_PRIVATE, &DecryptedLength, + int res = mbedtls_rsa_pkcs1_decrypt( + &m_Rsa, mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal(), MBEDTLS_RSA_PRIVATE, &DecryptedLength, a_EncryptedData, a_DecryptedData, a_DecryptedMaxLength ); if (res != 0) @@ -157,8 +157,8 @@ int cRsaPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte ASSERT(!"Invalid a_PlainLength!"); return -1; } - int res = rsa_pkcs1_encrypt( - &m_Rsa, ctr_drbg_random, m_CtrDrbg.GetInternal(), RSA_PRIVATE, + int res = mbedtls_rsa_pkcs1_encrypt( + &m_Rsa, mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal(), MBEDTLS_RSA_PRIVATE, a_PlainLength, a_PlainData, a_EncryptedData ); if (res != 0) diff --git a/src/PolarSSL++/RsaPrivateKey.h b/src/mbedTLS++/RsaPrivateKey.h index 595ba5663..7be0152b7 100644 --- a/src/PolarSSL++/RsaPrivateKey.h +++ b/src/mbedTLS++/RsaPrivateKey.h @@ -1,4 +1,4 @@ - + // RsaPrivateKey.h // Declares the cRsaPrivateKey class representing a private key for RSA operations. @@ -10,7 +10,7 @@ #pragma once #include "CtrDrbgContext.h" -#include "polarssl/rsa.h" +#include "mbedtls/rsa.h" @@ -48,15 +48,15 @@ public: int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength); protected: - /** The PolarSSL key context */ - rsa_context m_Rsa; + /** The mbedTLS key context */ + mbedtls_rsa_context m_Rsa; /** The random generator used for generating the key and encryption / decryption */ cCtrDrbgContext m_CtrDrbg; - /** Returns the internal context ptr. Only use in PolarSSL API calls. */ - rsa_context * GetInternal(void) { return &m_Rsa; } + /** Returns the internal context ptr. Only use in mbedTLS API calls. */ + mbedtls_rsa_context * GetInternal(void) { return &m_Rsa; } } ; typedef std::shared_ptr<cRsaPrivateKey> cRsaPrivateKeyPtr; diff --git a/src/PolarSSL++/Sha1Checksum.cpp b/src/mbedTLS++/Sha1Checksum.cpp index 5a56c18b0..9c82d92fe 100644 --- a/src/PolarSSL++/Sha1Checksum.cpp +++ b/src/mbedTLS++/Sha1Checksum.cpp @@ -1,4 +1,4 @@ - + // Sha1Checksum.cpp // Declares the cSha1Checksum class representing the SHA-1 checksum calculator @@ -56,7 +56,7 @@ public: cSha1Checksum::cSha1Checksum(void) : m_DoesAcceptInput(true) { - sha1_starts(&m_Sha1); + mbedtls_sha1_starts(&m_Sha1); } @@ -67,7 +67,7 @@ void cSha1Checksum::Update(const Byte * a_Data, size_t a_Length) { ASSERT(m_DoesAcceptInput); // Not Finalize()-d yet, or Restart()-ed - sha1_update(&m_Sha1, a_Data, a_Length); + mbedtls_sha1_update(&m_Sha1, a_Data, a_Length); } @@ -78,7 +78,7 @@ void cSha1Checksum::Finalize(cSha1Checksum::Checksum & a_Output) { ASSERT(m_DoesAcceptInput); // Not Finalize()-d yet, or Restart()-ed - sha1_finish(&m_Sha1, a_Output); + mbedtls_sha1_finish(&m_Sha1, a_Output); m_DoesAcceptInput = false; } @@ -129,7 +129,7 @@ void cSha1Checksum::DigestToJava(const Checksum & a_Digest, AString & a_Out) void cSha1Checksum::Restart(void) { - sha1_starts(&m_Sha1); + mbedtls_sha1_starts(&m_Sha1); m_DoesAcceptInput = true; } diff --git a/src/PolarSSL++/Sha1Checksum.h b/src/mbedTLS++/Sha1Checksum.h index b78fbfc62..43180e531 100644 --- a/src/PolarSSL++/Sha1Checksum.h +++ b/src/mbedTLS++/Sha1Checksum.h @@ -1,4 +1,4 @@ - + // Sha1Checksum.h // Declares the cSha1Checksum class representing the SHA-1 checksum calculator @@ -9,7 +9,7 @@ #pragma once -#include "polarssl/sha1.h" +#include "mbedtls/sha1.h" @@ -44,7 +44,7 @@ protected: /** True if the object is accepts more input data, false if Finalize()-d (need to Restart()) */ bool m_DoesAcceptInput; - sha1_context m_Sha1; + mbedtls_sha1_context m_Sha1; } ; diff --git a/src/PolarSSL++/SslContext.h b/src/mbedTLS++/SslContext.h index 0588122ba..c51a9f149 100644 --- a/src/PolarSSL++/SslContext.h +++ b/src/mbedTLS++/SslContext.h @@ -1,4 +1,4 @@ - + // SslContext.h // Declares the cSslContext class that holds everything a single SSL context needs to function @@ -9,11 +9,8 @@ #pragma once -#include "polarssl/ssl.h" +#include "mbedtls/ssl.h" #include "../ByteBuffer.h" -#include "CryptoKey.h" -#include "RsaPrivateKey.h" -#include "X509Cert.h" @@ -21,6 +18,7 @@ // fwd: class cCtrDrbgContext; +class cSslConfig; @@ -43,45 +41,40 @@ public: virtual ~cSslContext(); /** Initializes the context for use as a server or client. - Returns 0 on success, PolarSSL error on failure. */ - int Initialize(bool a_IsClient, const std::shared_ptr<cCtrDrbgContext> & a_CtrDrbg = {}); + a_Config must not be nullptr and the config must not be changed after this call. + Returns 0 on success, mbedTLS error on failure. */ + int Initialize(std::shared_ptr<const cSslConfig> a_Config); + + /** Initializes the context using the default config. */ + int Initialize(bool a_IsClient); /** Returns true if the object has been initialized properly. */ bool IsValid(void) const { return m_IsValid; } - /** Sets the certificate to use as our own. Must be used when representing a server, optional when client. - Must be called after Initialize(). */ - void SetOwnCert(const cX509CertPtr & a_OwnCert, const cRsaPrivateKeyPtr & a_OwnCertPrivKey); - - /** Sets the certificate to use as our own. Must be used when representing a server, optional when client. - Must be called after Initialize(). */ - void SetOwnCert(const cX509CertPtr & a_OwnCert, const cCryptoKeyPtr & a_OwnCertPrivKey); - - /** Sets a cert chain as the trusted cert store for this context. Must be called after Initialize(). - Calling this will switch the context into strict cert verification mode. - a_ExpectedPeerName is the CommonName that we expect the SSL peer to have in its cert, + /** Sets the SSL peer name expected for this context. Must be called after Initialize(). + \param a_ExpectedPeerName CommonName that we expect the SSL peer to have in its cert, if it is different, the verification will fail. An empty string will disable the CN check. */ - void SetCACerts(const cX509CertPtr & a_CACert, const AString & a_ExpectedPeerName); + void SetExpectedPeerName(const AString & a_ExpectedPeerName); /** Writes data to be encrypted and sent to the SSL peer. Will perform SSL handshake, if needed. - Returns the number of bytes actually written, or PolarSSL error code. - If the return value is POLARSSL_ERR_NET_WANT_READ or POLARSSL_ERR_NET_WANT_WRITE, the owner should send any + Returns the number of bytes actually written, or mbedTLS error code. + If the return value is MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, the owner should send any cached outgoing data to the SSL peer and write any incoming data received from the SSL peer and then call this function again with the same parameters. Note that this may repeat a few times before the data is actually written, mainly due to initial handshake. */ int WritePlain(const void * a_Data, size_t a_NumBytes); /** Reads data decrypted from the SSL stream. Will perform SSL handshake, if needed. - Returns the number of bytes actually read, or PolarSSL error code. - If the return value is POLARSSL_ERR_NET_WANT_READ or POLARSSL_ERR_NET_WANT_WRITE, the owner should send any + Returns the number of bytes actually read, or mbedTLS error code. + If the return value is MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, the owner should send any cached outgoing data to the SSL peer and write any incoming data received from the SSL peer and then call this function again with the same parameters. Note that this may repeat a few times before the data is actually read, mainly due to initial handshake. */ int ReadPlain(void * a_Data, size_t a_MaxBytes); /** Performs the SSL handshake. - Returns zero on success, PoladSSL error code on failure. - If the return value is POLARSSL_ERR_NET_WANT_READ or POLARSSL_ERR_NET_WANT_WRITE, the owner should send any + Returns zero on success, mbedTLS error code on failure. + If the return value is MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, the owner should send any cached outgoing data to the SSL peer and write any incoming data received from the SSL peer and then call this function again. Note that this may repeat a few times before the handshake is completed. */ int Handshake(void); @@ -90,64 +83,39 @@ public: bool HasHandshaken(void) const { return m_HasHandshaken; } /** Notifies the SSL peer that the connection is being closed. - Returns 0 on success, PolarSSL error code on failure. */ + Returns 0 on success, mbedTLS error code on failure. */ int NotifyClose(void); protected: - /** True if the object has been initialized properly. */ - bool m_IsValid; - - /** The random generator to use */ - std::shared_ptr<cCtrDrbgContext> m_CtrDrbg; - - /** The SSL context that PolarSSL uses. */ - ssl_context m_Ssl; - /** The certificate that we present to the peer. */ - cX509CertPtr m_OwnCert; + /** Configuration of the SSL context. */ + std::shared_ptr<const cSslConfig> m_Config; - /** Private key for m_OwnCert, if initialized from a cRsaPrivateKey. */ - cRsaPrivateKeyPtr m_OwnCertPrivKey; + /** The SSL context that mbedTLS uses. */ + mbedtls_ssl_context m_Ssl; - /** Private key for m_OwnCert, if initialized from a cCryptoKey. */ - cCryptoKeyPtr m_OwnCertPrivKey2; + /** True if the object has been initialized properly. */ + bool m_IsValid; /** True if the SSL handshake has been completed. */ bool m_HasHandshaken; - /** A copy of the trusted CA root cert store that is passed to us in SetCACerts(), so that the pointer - stays valid even after the call, when PolarSSL finally uses it. */ - cX509CertPtr m_CACerts; - - /** Buffer for the expected peer name. We need to buffer it because the caller may free the string they - give us before PolarSSL consumes the raw pointer it gets to the CN. */ - AString m_ExpectedPeerName; - - - /** The callback used by PolarSSL when it wants to read encrypted data. */ + /** The callback used by mbedTLS when it wants to read encrypted data. */ static int ReceiveEncrypted(void * a_This, unsigned char * a_Buffer, size_t a_NumBytes) { return (reinterpret_cast<cSslContext *>(a_This))->ReceiveEncrypted(a_Buffer, a_NumBytes); } - /** The callback used by PolarSSL when it wants to write encrypted data. */ + /** The callback used by mbedTLS when it wants to write encrypted data. */ static int SendEncrypted(void * a_This, const unsigned char * a_Buffer, size_t a_NumBytes) { return (reinterpret_cast<cSslContext *>(a_This))->SendEncrypted(a_Buffer, a_NumBytes); } - #ifdef _DEBUG - /** The callback used by PolarSSL to output debug messages */ - static void SSLDebugMessage(void * a_UserParam, int a_Level, const char * a_Text); - - /** The callback used by PolarSSL to log information on the cert chain */ - static int SSLVerifyCert(void * a_This, x509_crt * a_Crt, int a_Depth, int * a_Flags); - #endif // _DEBUG - - /** Called when PolarSSL wants to read encrypted data. */ + /** Called when mbedTLS wants to read encrypted data. */ virtual int ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) = 0; - /** Called when PolarSSL wants to write encrypted data. */ + /** Called when mbedTLS wants to write encrypted data. */ virtual int SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes) = 0; } ; diff --git a/src/PolarSSL++/X509Cert.cpp b/src/mbedTLS++/X509Cert.cpp index ed65639a5..1e51dd2b7 100644 --- a/src/PolarSSL++/X509Cert.cpp +++ b/src/mbedTLS++/X509Cert.cpp @@ -1,7 +1,7 @@ - + // X509Cert.cpp -// Implements the cX509Cert class representing a wrapper over X509 certs in PolarSSL +// Implements the cX509Cert class representing a wrapper over X509 certs in mbedTLS #include "Globals.h" #include "X509Cert.h" @@ -12,7 +12,7 @@ cX509Cert::cX509Cert(void) { - x509_crt_init(&m_Cert); + mbedtls_x509_crt_init(&m_Cert); } @@ -21,7 +21,7 @@ cX509Cert::cX509Cert(void) cX509Cert::~cX509Cert() { - x509_crt_free(&m_Cert); + mbedtls_x509_crt_free(&m_Cert); } @@ -30,7 +30,7 @@ cX509Cert::~cX509Cert() int cX509Cert::Parse(const void * a_CertContents, size_t a_Size) { - return x509_crt_parse(&m_Cert, reinterpret_cast<const unsigned char *>(a_CertContents), a_Size); + return mbedtls_x509_crt_parse(&m_Cert, reinterpret_cast<const unsigned char *>(a_CertContents), a_Size); } diff --git a/src/PolarSSL++/X509Cert.h b/src/mbedTLS++/X509Cert.h index 45f714d14..4234308ff 100644 --- a/src/PolarSSL++/X509Cert.h +++ b/src/mbedTLS++/X509Cert.h @@ -1,7 +1,7 @@ - + // X509Cert.h -// Declares the cX509Cert class representing a wrapper over X509 certs in PolarSSL +// Declares the cX509Cert class representing a wrapper over X509 certs in mbedTLS @@ -9,7 +9,7 @@ #pragma once -#include "polarssl/x509_crt.h" +#include "mbedtls/x509_crt.h" @@ -17,21 +17,21 @@ class cX509Cert { - friend class cSslContext; + friend class cSslConfig; public: cX509Cert(void); ~cX509Cert(void); /** Parses the certificate chain data into the context. - Returns 0 on succes, or PolarSSL error code on failure. */ + Returns 0 on succes, or mbedTLS error code on failure. */ int Parse(const void * a_CertContents, size_t a_Size); protected: - x509_crt m_Cert; + mbedtls_x509_crt m_Cert; - /** Returns the internal cert ptr. Only use in PolarSSL API calls. */ - x509_crt * GetInternal(void) { return &m_Cert; } + /** Returns the internal cert ptr. Only use in mbedTLS API calls. */ + mbedtls_x509_crt * GetInternal(void) { return &m_Cert; } } ; typedef std::shared_ptr<cX509Cert> cX509CertPtr; |