Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b2fdb95
feat: updated mbedtls from v3.2.1 to v3.6.2
ccrugoPhilips Jan 27, 2025
1497f31
fix: #error "No mbedtls_ms_time available"
ccrugoPhilips Feb 4, 2025
9ef1785
fix: fetching mbedtls patch for VS2019 build fail
ccrugoPhilips Feb 5, 2025
0d605e8
fix: fetching mbedtls patch for VS2019 build fail
ccrugoPhilips Feb 5, 2025
40ed0f6
fix: fetching mbedtls patch for VS2019 build fail
ccrugoPhilips Feb 5, 2025
89f7fbc
fix: fetching mbedtls patch for VS2019 build fail
ccrugoPhilips Feb 5, 2025
073e733
fix: fetching mbedtls patch for VS2019 build fail
ccrugoPhilips Feb 5, 2025
fdc8b23
fix: fetching mbedtls patch for VS2019 build fail
ccrugoPhilips Feb 5, 2025
8ec1c47
feat: added fix for session loading assert
ccrugoPhilips Mar 5, 2025
841f2d5
Merge commit 'e8a03f4e61d5cfce7356b85712722e3c3a8a9bf8' into feature/…
ccrugoPhilips Mar 5, 2025
86bb3bd
feat: addressed PR review comments
ccrugoPhilips Mar 11, 2025
8eb57a4
feat: addressed PR review comments
ccrugoPhilips Mar 11, 2025
56d2e58
feat: added missing unit test
ccrugoPhilips Mar 17, 2025
e3d853d
Apply suggestions from code review
ccrugoPhilips Mar 17, 2025
1c20999
feat: addressed PR review comment
ccrugoPhilips Mar 17, 2025
9461a3e
feat: added missing unit test
ccrugoPhilips Mar 17, 2025
0233f7d
Apply suggestions from code review
ccrugoPhilips Mar 17, 2025
b4a2b11
Update services/network/test/TestConnectionMbedTls.cpp
ccrugoPhilips Mar 18, 2025
2a01c84
fix: bug within LoadSessions
ccrugoPhilips Mar 19, 2025
14eb976
Update services/network/MbedTlsSession.cpp
ccrugoPhilips Mar 19, 2025
895c446
fix: failing unit test
ccrugoPhilips Mar 19, 2025
11cd4ac
Addressed code review comment
ccrugoPhilips Mar 19, 2025
ec0f0f6
Merge branch 'main' into feature/mbedtls-version-to-latest
ccrugoPhilips Mar 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions external/crypto/mbedtls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FetchContent_Declare(
mbedtls
GIT_REPOSITORY https://github.com/Mbed-TLS/mbedtls
GIT_TAG 869298bffeea13b205343361b7a7daf2b210e33d # v3.2.1
GIT_REPOSITORY https://github.com/ccrugoPhilips/mbedtls # Mbed-TLS fork
GIT_TAG e0b9fdf17e1d584ac3713fc7970fd9de530e7063 # v3.6.2 with patch
)

set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL On)
Expand Down
4 changes: 4 additions & 0 deletions external/crypto/mbedtls/mbedtls_emil_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@
//#define MBEDTLS_PLATFORM_NV_SEED_ALT
//#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT

#ifndef EMIL_HOST_BUILD
#define MBEDTLS_PLATFORM_MS_TIME_ALT
#endif

/**
* \def MBEDTLS_DEPRECATED_WARNING
*
Expand Down
11 changes: 11 additions & 0 deletions services/network/ConnectionMbedTls.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include "services/network/ConnectionMbedTls.hpp"
#include "infra/event/EventDispatcherWithWeakPtr.hpp"

#ifndef EMIL_HOST_BUILD
#include "mbedtls/platform_time.h"
extern "C"
{
mbedtls_ms_time_t mbedtls_ms_time(void)
{
return static_cast<mbedtls_ms_time_t>(std::chrono::duration_cast<std::chrono::milliseconds>(infra::Now(3).time_since_epoch()).count());
}
}
#endif

namespace services
{
ConnectionMbedTls::ConnectionMbedTls(infra::AutoResetFunction<void(infra::SharedPtr<services::ConnectionObserver> connectionObserver)>&& createdObserver, CertificatesMbedTls& certificates,
Expand Down
20 changes: 16 additions & 4 deletions services/network/MbedTlsSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "infra/util/BoundedString.hpp"
#include "infra/util/BoundedVector.hpp"
#include "infra/util/ByteRange.hpp"
#include "infra/util/ReallyAssert.hpp"
#include "services/network/Address.hpp"
#include "services/util/Sha256.hpp"

Expand Down Expand Up @@ -34,7 +33,7 @@ namespace services
, identifier(reference.identifier)
{
mbedtls_ssl_session_init(&session);
really_assert(mbedtls_ssl_session_load(&session, reference.serializedSession.begin(), reference.serializedSession.size()) == 0);
clientSessionDeserialized = mbedtls_ssl_session_load(&session, reference.serializedSession.begin(), reference.serializedSession.size()) == 0;
}

MbedTlsSession::~MbedTlsSession()
Expand All @@ -58,6 +57,11 @@ namespace services
return clientSessionObtained;
}

bool MbedTlsSession::IsDeserialized()
{
return clientSessionDeserialized;
}

int MbedTlsSession::SetSession(mbedtls_ssl_context* context)
{
return mbedtls_ssl_set_session(context, &session);
Expand Down Expand Up @@ -249,12 +253,20 @@ namespace services

void MbedTlsSessionStoragePersistent::LoadSessions()
{
for (auto& persistedSession : *nvm)
for (auto persistedSession = nvm->begin(); persistedSession != nvm->end();)
{
storage.emplace_back(persistedSession, [this](MbedTlsSession* session)
storage.emplace_back(*persistedSession, [this](MbedTlsSession* session)
{
SerializeSessionToFlash(session);
});

if (storage.back().IsDeserialized())
++persistedSession;
else
{
persistedSession = nvm->erase(persistedSession);
storage.pop_back();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions services/network/MbedTlsSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ namespace services
virtual void Reinitialize();
virtual void Obtained();
virtual bool IsObtained();
virtual bool IsDeserialized();
virtual int SetSession(mbedtls_ssl_context* context);
virtual int GetSession(mbedtls_ssl_context* context);
virtual const infra::BoundedVector<uint8_t>& Identifier() const;

private:
mbedtls_ssl_session session;
bool clientSessionObtained = false;
bool clientSessionDeserialized = false;
infra::BoundedVector<uint8_t>::WithMaxSize<32> identifier;
};

Expand Down
58 changes: 58 additions & 0 deletions services/network/test/TestConnectionMbedTls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "services/util/Sha256MbedTls.hpp"
#include "services/util/test_doubles/ConfigurationStoreMock.hpp"
#include "gmock/gmock.h"
#include <algorithm>

class ConnectionMbedTlsTest
: public testing::Test
Expand Down Expand Up @@ -237,6 +238,63 @@ TEST_F(ConnectionMbedTlsTest, persistent_session_reopen_connection)
}
}

TEST_F(ConnectionMbedTlsTest, persistent_session_fails_deserialize)
{
infra::BoundedVector<network::MbedTlsPersistedSession>::WithMaxSize<1> stores;
testing::StrictMock<services::ConfigurationStoreInterfaceMock> configInterface;
services::ConfigurationStoreAccess<infra::BoundedVector<network::MbedTlsPersistedSession>> configStore{ configInterface, stores };
services::Sha256MbedTls sha256;
services::MbedTlsSessionHasher mbedTlsHasher{ sha256 };
services::MbedTlsSessionStoragePersistent::WithMaxSize<1> persistentStorage{ configStore, mbedTlsHasher };

infra::BoundedVector<network::MbedTlsPersistedSession>::WithMaxSize<2> newStores;
services::ConfigurationStoreAccess<infra::BoundedVector<network::MbedTlsPersistedSession>> newConfigStore{ configInterface, newStores };

services::ConnectionFactoryMbedTls::WithMaxConnectionsListenersAndConnectors<2, 1, 0> tlsNetworkServer(loopBackNetwork, serverCertificates, randomDataGenerator);
services::ConnectionFactoryMbedTls::CustomSessionStorageWithMaxConnectionsListenersAndConnectors<2, 0, 1> tlsNetworkClient(persistentStorage, loopBackNetwork, clientCertificates, randomDataGenerator);
infra::SharedPtr<void> listener = tlsNetworkServer.Listen(1234, serverObserverFactory);

{
EXPECT_CALL(clientObserverFactory, Port()).WillOnce(testing::Return(1234));
tlsNetworkClient.Connect(clientObserverFactory);

infra::SharedOptional<services::ConnectionObserverStub> observer1;
infra::SharedOptional<services::ConnectionObserverStub> observer2;

EXPECT_CALL(serverObserverFactory, ConnectionAccepted(testing::_, testing::_))
.WillOnce(testing::Invoke([&](infra::AutoResetFunction<void(infra::SharedPtr<services::ConnectionObserver> connectionObserver)> createdObserver, services::IPAddress address)
{
createdObserver(observer1.Emplace());
}));
EXPECT_CALL(clientObserverFactory, Address());
EXPECT_CALL(configInterface, Write());
EXPECT_CALL(clientObserverFactory, ConnectionEstablished(testing::_))
.WillOnce(testing::Invoke([&](infra::AutoResetFunction<void(infra::SharedPtr<services::ConnectionObserver> connectionObserver)> createdObserver)
{
createdObserver(observer2.Emplace());
}));
ExecuteAllActions();

observer1->Subject().AbortAndDestroy();
}

{
newConfigStore->emplace_back();
std::copy(stores.back().serializedSession.begin(), stores.back().serializedSession.end(), std::back_inserter(newConfigStore->back().serializedSession));
newConfigStore->emplace_back();
EXPECT_EQ(newConfigStore->size(), 2);
services::MbedTlsSessionStoragePersistent::WithMaxSize<2> newPersistentStorage{ newConfigStore, mbedTlsHasher };
EXPECT_EQ(newConfigStore->size(), 1);
}
}

TEST_F(ConnectionMbedTlsTest, mbedtls_session_fails_deserialize)
{
network::MbedTlsPersistedSession persistedSession;
auto sessionDeserialize = services::MbedTlsSession(persistedSession);
EXPECT_FALSE(sessionDeserialize.IsDeserialized());
}

class ConnectionWithNameResolverMbedTlsTest
: public testing::Test
, public infra::ClockFixture
Expand Down
Loading