diff --git a/src/app/tests/TestWriteInteraction.cpp b/src/app/tests/TestWriteInteraction.cpp index 9c5827575f829e..4e9c5eac95f81b 100644 --- a/src/app/tests/TestWriteInteraction.cpp +++ b/src/app/tests/TestWriteInteraction.cpp @@ -479,10 +479,13 @@ int Test_Setup(void * inContext) VerifyOrReturnError(TestContext::InitializeAsync(inContext) == SUCCESS, FAILURE); - constexpr chip::FabricIndex kFabricIndex1 = 1; - static const uint8_t kCompressedFabricId[] = { 0x29, 0x06, 0xC9, 0x08, 0xD1, 0x15, 0xD3, 0x62 }; + TestContext & ctx = *static_cast(inContext); VerifyOrReturnError(CHIP_NO_ERROR == chip::GroupTesting::InitProvider(), FAILURE); - VerifyOrReturnError(CHIP_NO_ERROR == chip::GroupTesting::InitData(kFabricIndex1, chip::ByteSpan(kCompressedFabricId)), FAILURE); + + uint8_t buf[sizeof(chip::CompressedFabricId)]; + chip::MutableByteSpan span(buf); + VerifyOrReturnError(CHIP_NO_ERROR == ctx.GetBobFabric()->GetCompressedId(span), FAILURE); + VerifyOrReturnError(CHIP_NO_ERROR == chip::GroupTesting::InitData(ctx.GetBobFabricIndex(), span), FAILURE); return SUCCESS; } diff --git a/src/credentials/FabricTable.cpp b/src/credentials/FabricTable.cpp index d93df4b9b103ea..4f584437ba05d7 100644 --- a/src/credentials/FabricTable.cpp +++ b/src/credentials/FabricTable.cpp @@ -809,4 +809,27 @@ CHIP_ERROR formatKey(FabricIndex fabricIndex, MutableCharSpan formattedKey, cons return err; } +CHIP_ERROR FabricInfo::TestOnlyBuildFabric(ByteSpan rootCert, ByteSpan icacCert, ByteSpan nocCert, ByteSpan nodePubKey, + ByteSpan nodePrivateKey) +{ + Reset(); + + ReturnErrorOnFailure(SetRootCert(rootCert)); + ReturnErrorOnFailure(SetICACert(icacCert)); + ReturnErrorOnFailure(SetNOCCert(nocCert)); + + // NOTE: this requres ENABLE_HSM_CASE_OPS_KEY is not defined + P256SerializedKeypair opKeysSerialized; + memcpy(static_cast(opKeysSerialized), nodePubKey.data(), nodePubKey.size()); + memcpy(static_cast(opKeysSerialized) + nodePubKey.size(), nodePrivateKey.data(), nodePrivateKey.size()); + ReturnErrorOnFailure(opKeysSerialized.SetLength(nodePubKey.size() + nodePrivateKey.size())); + + P256Keypair opKey; + ReturnErrorOnFailure(opKey.Deserialize(opKeysSerialized)); + ReturnErrorOnFailure(SetOperationalKeypair(&opKey)); + + // NOTE: mVendorId and mFabricLabel are not initialize, because they are not used in tests. + return CHIP_NO_ERROR; +} + } // namespace chip diff --git a/src/credentials/FabricTable.h b/src/credentials/FabricTable.h index 3939ead901ed56..45d1163643e03f 100644 --- a/src/credentials/FabricTable.h +++ b/src/credentials/FabricTable.h @@ -208,6 +208,10 @@ class DLL_EXPORT FabricInfo friend class FabricTable; + // Test-only, build a fabric using given root cert and NOC + CHIP_ERROR TestOnlyBuildFabric(ByteSpan rootCert, ByteSpan icacCert, ByteSpan nocCert, ByteSpan nodePubKey, + ByteSpan nodePrivateKey); + private: static constexpr size_t MetadataTLVMaxSize() { diff --git a/src/messaging/tests/BUILD.gn b/src/messaging/tests/BUILD.gn index 68b2e2aa11bf44..5caa64a50579ea 100644 --- a/src/messaging/tests/BUILD.gn +++ b/src/messaging/tests/BUILD.gn @@ -31,6 +31,7 @@ static_library("helpers") { cflags = [ "-Wconversion" ] deps = [ + "${chip_root}/src/credentials/tests:cert_test_vectors", "${chip_root}/src/messaging", "${chip_root}/src/protocols", "${chip_root}/src/transport", diff --git a/src/messaging/tests/MessagingContext.cpp b/src/messaging/tests/MessagingContext.cpp index ea280ed400d063..273a98d18cc828 100644 --- a/src/messaging/tests/MessagingContext.cpp +++ b/src/messaging/tests/MessagingContext.cpp @@ -17,6 +17,7 @@ #include "MessagingContext.h" +#include #include #include @@ -38,9 +39,31 @@ CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioCo ReturnErrorOnFailure(mExchangeManager.Init(&mSessionManager)); ReturnErrorOnFailure(mMessageCounterManager.Init(&mExchangeManager)); - ReturnErrorOnFailure(CreateSessionBobToAlice()); - ReturnErrorOnFailure(CreateSessionAliceToBob()); - ReturnErrorOnFailure(CreateSessionBobToFriends()); + if (mInitializeNodes) + { + FabricInfo aliceFabric; + FabricInfo bobFabric; + + aliceFabric.TestOnlyBuildFabric( + ByteSpan(TestCerts::sTestCert_Root01_Chip, TestCerts::sTestCert_Root01_Chip_Len), + ByteSpan(TestCerts::sTestCert_ICA01_Chip, TestCerts::sTestCert_ICA01_Chip_Len), + ByteSpan(TestCerts::sTestCert_Node01_01_Chip, TestCerts::sTestCert_Node01_01_Chip_Len), + ByteSpan(TestCerts::sTestCert_Node01_01_PublicKey, TestCerts::sTestCert_Node01_01_PublicKey_Len), + ByteSpan(TestCerts::sTestCert_Node01_01_PrivateKey, TestCerts::sTestCert_Node01_01_PrivateKey_Len)); + ReturnErrorOnFailure(mFabricTable.AddNewFabric(aliceFabric, &mAliceFabricIndex)); + + bobFabric.TestOnlyBuildFabric( + ByteSpan(TestCerts::sTestCert_Root02_Chip, TestCerts::sTestCert_Root02_Chip_Len), + ByteSpan(TestCerts::sTestCert_ICA02_Chip, TestCerts::sTestCert_ICA02_Chip_Len), + ByteSpan(TestCerts::sTestCert_Node02_01_Chip, TestCerts::sTestCert_Node02_01_Chip_Len), + ByteSpan(TestCerts::sTestCert_Node02_01_PublicKey, TestCerts::sTestCert_Node02_01_PublicKey_Len), + ByteSpan(TestCerts::sTestCert_Node02_01_PrivateKey, TestCerts::sTestCert_Node02_01_PrivateKey_Len)); + ReturnErrorOnFailure(mFabricTable.AddNewFabric(bobFabric, &mBobFabricIndex)); + + ReturnErrorOnFailure(CreateSessionBobToAlice()); + ReturnErrorOnFailure(CreateSessionAliceToBob()); + ReturnErrorOnFailure(CreateSessionBobToFriends()); + } return CHIP_NO_ERROR; } @@ -72,19 +95,21 @@ CHIP_ERROR MessagingContext::ShutdownAndRestoreExisting(MessagingContext & exist CHIP_ERROR MessagingContext::CreateSessionBobToAlice() { - return mSessionManager.NewPairing(mSessionBobToAlice, Optional::Value(mAliceAddress), GetAliceNodeId(), - &mPairingBobToAlice, CryptoContext::SessionRole::kInitiator, mSrcFabricIndex); + return mSessionManager.NewPairing(mSessionBobToAlice, Optional::Value(mAliceAddress), + GetAliceFabric()->GetNodeId(), &mPairingBobToAlice, CryptoContext::SessionRole::kInitiator, + mBobFabricIndex); } CHIP_ERROR MessagingContext::CreateSessionAliceToBob() { - return mSessionManager.NewPairing(mSessionAliceToBob, Optional::Value(mBobAddress), GetBobNodeId(), - &mPairingAliceToBob, CryptoContext::SessionRole::kResponder, mDestFabricIndex); + return mSessionManager.NewPairing(mSessionAliceToBob, Optional::Value(mBobAddress), + GetBobFabric()->GetNodeId(), &mPairingAliceToBob, CryptoContext::SessionRole::kResponder, + mAliceFabricIndex); } CHIP_ERROR MessagingContext::CreateSessionBobToFriends() { - mSessionBobToFriends.Emplace(GetFriendsGroupId(), mSrcFabricIndex, GetBobNodeId()); + mSessionBobToFriends.Emplace(GetFriendsGroupId(), mBobFabricIndex, GetBobFabric()->GetNodeId()); return CHIP_NO_ERROR; } diff --git a/src/messaging/tests/MessagingContext.h b/src/messaging/tests/MessagingContext.h index 3d289de7bdb3d7..bff1a6e9606f72 100644 --- a/src/messaging/tests/MessagingContext.h +++ b/src/messaging/tests/MessagingContext.h @@ -71,11 +71,14 @@ class MessagingContext : public PlatformMemoryUser public: MessagingContext() : mInitialized(false), mAliceAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT + 1)), - mBobAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT)), mPairingAliceToBob(GetBobKeyId(), GetAliceKeyId()), - mPairingBobToAlice(GetAliceKeyId(), GetBobKeyId()) + mBobAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT)), mPairingAliceToBob(kBobKeyId, kAliceKeyId), + mPairingBobToAlice(kAliceKeyId, kBobKeyId) {} ~MessagingContext() { VerifyOrDie(mInitialized == false); } + // Whether Alice and Bob are initialized, must be called before Init + void ConfigInitializeNodes(bool initializeNodes) { mInitializeNodes = initializeNodes; } + /// Initialize the underlying layers and test suite pointer CHIP_ERROR Init(TransportMgrBase * transport, IOContext * io); @@ -96,30 +99,22 @@ class MessagingContext : public PlatformMemoryUser Inet::IPAddress::FromString("::1", addr); return addr; } - NodeId GetBobNodeId() const { return mBobNodeId; } - NodeId GetAliceNodeId() const { return mAliceNodeId; } - - void SetBobNodeId(NodeId nodeId) { mBobNodeId = nodeId; } - void SetAliceNodeId(NodeId nodeId) { mAliceNodeId = nodeId; } - uint16_t GetBobKeyId() const { return mBobKeyId; } - uint16_t GetAliceKeyId() const { return mAliceKeyId; } + static const uint16_t kBobKeyId = 1; + static const uint16_t kAliceKeyId = 2; + NodeId GetBobNodeId() const; + NodeId GetAliceNodeId() const; GroupId GetFriendsGroupId() const { return mFriendsGroupId; } - void SetBobKeyId(uint16_t id) { mBobKeyId = id; } - void SetAliceKeyId(uint16_t id) { mAliceKeyId = id; } - - FabricIndex GetFabricIndex() const { return mSrcFabricIndex; } - void SetFabricIndex(FabricIndex id) - { - mSrcFabricIndex = id; - mDestFabricIndex = id; - } - SessionManager & GetSecureSessionManager() { return mSessionManager; } Messaging::ExchangeManager & GetExchangeManager() { return mExchangeManager; } secure_channel::MessageCounterManager & GetMessageCounterManager() { return mMessageCounterManager; } + FabricIndex GetAliceFabricIndex() { return mAliceFabricIndex; } + FabricIndex GetBobFabricIndex() { return mBobFabricIndex; } + FabricInfo * GetAliceFabric() { return mFabricTable.FindFabricWithIndex(mAliceFabricIndex); } + FabricInfo * GetBobFabric() { return mFabricTable.FindFabricWithIndex(mBobFabricIndex); } + CHIP_ERROR CreateSessionBobToAlice(); CHIP_ERROR CreateSessionAliceToBob(); CHIP_ERROR CreateSessionBobToFriends(); @@ -141,6 +136,7 @@ class MessagingContext : public PlatformMemoryUser System::Layer & GetSystemLayer() { return mIOContext->GetSystemLayer(); } private: + bool mInitializeNodes = true; bool mInitialized; FabricTable mFabricTable; SessionManager mSessionManager; @@ -150,11 +146,9 @@ class MessagingContext : public PlatformMemoryUser TransportMgrBase * mTransport; // Only needed for InitFromExisting. chip::TestPersistentStorageDelegate mStorage; // for SessionManagerInit - NodeId mBobNodeId = 123654; - NodeId mAliceNodeId = 111222333; - uint16_t mBobKeyId = 1; - uint16_t mAliceKeyId = 2; - GroupId mFriendsGroupId = 0x0101; + FabricIndex mAliceFabricIndex = kUndefinedFabricIndex; + FabricIndex mBobFabricIndex = kUndefinedFabricIndex; + GroupId mFriendsGroupId = 0x0101; Transport::PeerAddress mAliceAddress; Transport::PeerAddress mBobAddress; SecurePairingUsingTestSecret mPairingAliceToBob; @@ -162,8 +156,6 @@ class MessagingContext : public PlatformMemoryUser SessionHolder mSessionAliceToBob; SessionHolder mSessionBobToAlice; Optional mSessionBobToFriends; - FabricIndex mSrcFabricIndex = 1; - FabricIndex mDestFabricIndex = 1; }; template diff --git a/src/messaging/tests/TestExchangeMgr.cpp b/src/messaging/tests/TestExchangeMgr.cpp index 9ee9be6954db8d..9d0eec265cf8ee 100644 --- a/src/messaging/tests/TestExchangeMgr.cpp +++ b/src/messaging/tests/TestExchangeMgr.cpp @@ -97,17 +97,13 @@ void CheckNewContextTest(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, ec1 != nullptr); NL_TEST_ASSERT(inSuite, ec1->IsInitiator() == true); NL_TEST_ASSERT(inSuite, ec1->GetExchangeId() != 0); - auto sessionPeerToLocal = ec1->GetSessionHandle()->AsSecureSession(); - NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerNodeId() == ctx.GetBobNodeId()); - NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerSessionId() == ctx.GetBobKeyId()); + NL_TEST_ASSERT(inSuite, ec1->GetSessionHandle() == ctx.GetSessionAliceToBob()); NL_TEST_ASSERT(inSuite, ec1->GetDelegate() == &mockAppDelegate); ExchangeContext * ec2 = ctx.NewExchangeToAlice(&mockAppDelegate); NL_TEST_ASSERT(inSuite, ec2 != nullptr); NL_TEST_ASSERT(inSuite, ec2->GetExchangeId() > ec1->GetExchangeId()); - auto sessionLocalToPeer = ec2->GetSessionHandle()->AsSecureSession(); - NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerNodeId() == ctx.GetAliceNodeId()); - NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerSessionId() == ctx.GetAliceKeyId()); + NL_TEST_ASSERT(inSuite, ec2->GetSessionHandle() == ctx.GetSessionBobToAlice()); ec1->Close(); ec2->Close(); diff --git a/src/protocols/secure_channel/tests/TestCASESession.cpp b/src/protocols/secure_channel/tests/TestCASESession.cpp index a29f03332ecda6..1c75010df24062 100644 --- a/src/protocols/secure_channel/tests/TestCASESession.cpp +++ b/src/protocols/secure_channel/tests/TestCASESession.cpp @@ -656,15 +656,10 @@ CHIP_ERROR CASETestSecurePairingSetup(void * inContext) { TestContext & ctx = *reinterpret_cast(inContext); + ctx.ConfigInitializeNodes(false); ReturnErrorOnFailure(ctx.Init()); ctx.EnableAsyncDispatch(); - ctx.SetBobNodeId(kPlaceholderNodeId); - ctx.SetAliceNodeId(kPlaceholderNodeId); - ctx.SetBobKeyId(0); - ctx.SetAliceKeyId(0); - ctx.SetFabricIndex(kUndefinedFabricIndex); - gCommissionerFabrics.Init(&gCommissionerStorageDelegate); gDeviceFabrics.Init(&gDeviceStorageDelegate); diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index 2dfffa2453d0cd..13a75f8f66a083 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -496,16 +496,12 @@ static nlTestSuite sSuite = */ int TestSecurePairing_Setup(void * inContext) { + auto & ctx = *static_cast(inContext); + // Initialize System memory and resources + ctx.ConfigInitializeNodes(false); VerifyOrReturnError(TestContext::InitializeAsync(inContext) == SUCCESS, FAILURE); - auto & ctx = *static_cast(inContext); - ctx.SetBobNodeId(kPlaceholderNodeId); - ctx.SetAliceNodeId(kPlaceholderNodeId); - ctx.SetBobKeyId(0); - ctx.SetAliceKeyId(0); - ctx.SetFabricIndex(kUndefinedFabricIndex); - return SUCCESS; }