From e08080716a07c5d81e332529b26d9985683229f8 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 29 Nov 2023 15:26:42 -0800 Subject: [PATCH 01/93] ICDHandler initialization --- src/app/icd/ICDHandler.cpp | 93 ++++++++++++++++++++++++++++++++++++++ src/app/icd/ICDHandler.h | 73 ++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 src/app/icd/ICDHandler.cpp create mode 100644 src/app/icd/ICDHandler.h diff --git a/src/app/icd/ICDHandler.cpp b/src/app/icd/ICDHandler.cpp new file mode 100644 index 00000000000000..9516752f1d6653 --- /dev/null +++ b/src/app/icd/ICDHandler.cpp @@ -0,0 +1,93 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file defines objects for a CHIP ICD handler which handles unsolicited checkin messages. + * + */ + +#include "ICDHandler.h" + +#include + +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace app { + +static Global sCheckInMessageHandler; +CheckInMessageHandler * CheckInMessageHandler::GetInstance() +{ + return &sCheckInMessageHandler.get(); +} + +CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager) +{ + VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + mExchangeManager = exchangeManager; + ReturnErrorOnFailure( + exchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); + + return CHIP_NO_ERROR; +} + +void CheckInMessageHandler::Shutdown() +{ + // TODO : If any timers are added in the future, they need to be cleared here + if (mExchangeManager) + { + mExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); + mExchangeManager = nullptr; + } +} +CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) +{ + // Return error for wrong message type + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); + + newDelegate = this; + return CHIP_NO_ERROR; +} + +CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) +{ + // TODO : Pass the parsed payload to ICDClientManagement via callback + VerifyOrReturn(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn)); + + Crypto::Aes128KeyHandle key; + chip::Protocols::SecureChannel::CounterType counter; + MutableByteSpan appData; + uint8_t checkInPayload[chip::Protocols::SecureChannel::CheckinMessage::sMinPayloadSize]; + memcpy(&checkInPayload, payload->Start(), sizeof(checkInPayload)); + chip::ByteSpan payloadByteSpan(checkInPayload); + chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(key, payloadByteSpan, counter, appData); + + return CHIP_NO_ERROR; +} + +void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} + +} // namespace app +} // namespace chip diff --git a/src/app/icd/ICDHandler.h b/src/app/icd/ICDHandler.h new file mode 100644 index 00000000000000..064d65c1957448 --- /dev/null +++ b/src/app/icd/ICDHandler.h @@ -0,0 +1,73 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file defines objects for a CHIP CheckInMessage unsolicited + * handler + * + */ + +#pragma once + +#include +#include +#include + +namespace chip { +namespace app { +class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler +{ + class Callback + { + public: + virtual ~Callback() = default; + + // TODO : Include the callback message from ICDClientManagement + }; + +public: + /** + * @brief Retrieve the singleton CheckIn handler + * + * @return A pointer to the shared CheckIn handler + * + */ + static CheckInMessageHandler * GetInstance(void); + + CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager); + void Shutdown(); + +protected: + // ExchangeDelegate + CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) override; + + // UnsolicitedMessageHandler + CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; + + // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate + void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + +private: + Messaging::ExchangeManager * mExchangeManager = nullptr; + Messaging::ExchangeManager * GetExchangeManager(void) const { return mExchangeManager; } +}; + +} // namespace app +} // namespace chip From e403ef31d6a59c953af52b7a63e2dada66af572c Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 29 Nov 2023 15:38:49 -0800 Subject: [PATCH 02/93] ICDHandler initialization --- src/app/icd/BUILD.gn | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index fcb396b3bcd41b..d2a4806fd78cc7 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -114,3 +114,18 @@ source_set("configuration-data") { "${chip_root}/src/lib/core", ] } + +# ICD Handler source-set is broken out of the main source-set to enable unit tests +# All sources and configurations used by the ICDHandler need to go in this source-set +source_set("handler") { + sources = [ + "ICDHandler.cpp", + "ICDHandler.h", + ] + + public_deps = [ + "${chip_root}/src/lib/core", + "${chip_root}/src/messaging", + "${chip_root}/src/protocols", + ] +} \ No newline at end of file From 2aeecb5affb6d28a9663a8346e9835bb720592df Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 30 Nov 2023 00:37:08 +0000 Subject: [PATCH 03/93] Restyled by gn --- src/app/icd/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index d2a4806fd78cc7..d63312f6ce3571 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -128,4 +128,4 @@ source_set("handler") { "${chip_root}/src/messaging", "${chip_root}/src/protocols", ] -} \ No newline at end of file +} From 20b833b0ead9c1181c338f0265f3e3ef76d1af01 Mon Sep 17 00:00:00 2001 From: thivya-amazon <99231372+thivya-amazon@users.noreply.github.com> Date: Thu, 30 Nov 2023 11:07:08 -0800 Subject: [PATCH 04/93] Update src/app/icd/ICDHandler.cpp Co-authored-by: Jean-Francois Penven <67962328+jepenven-silabs@users.noreply.github.com> --- src/app/icd/ICDHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/ICDHandler.cpp b/src/app/icd/ICDHandler.cpp index 9516752f1d6653..d71a640526f5b6 100644 --- a/src/app/icd/ICDHandler.cpp +++ b/src/app/icd/ICDHandler.cpp @@ -79,7 +79,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * Crypto::Aes128KeyHandle key; chip::Protocols::SecureChannel::CounterType counter; MutableByteSpan appData; - uint8_t checkInPayload[chip::Protocols::SecureChannel::CheckinMessage::sMinPayloadSize]; + ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; memcpy(&checkInPayload, payload->Start(), sizeof(checkInPayload)); chip::ByteSpan payloadByteSpan(checkInPayload); chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(key, payloadByteSpan, counter, appData); From 1c4e53046ed8cce20da06fa73e39c52e04070f1e Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 30 Nov 2023 11:25:35 -0800 Subject: [PATCH 05/93] ICDHandler initialization --- src/app/icd/ICDHandler.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/app/icd/ICDHandler.cpp b/src/app/icd/ICDHandler.cpp index d71a640526f5b6..8bfa70084c7975 100644 --- a/src/app/icd/ICDHandler.cpp +++ b/src/app/icd/ICDHandler.cpp @@ -74,14 +74,12 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * System::PacketBufferHandle && payload) { // TODO : Pass the parsed payload to ICDClientManagement via callback - VerifyOrReturn(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn)); + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); Crypto::Aes128KeyHandle key; chip::Protocols::SecureChannel::CounterType counter; MutableByteSpan appData; ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; - memcpy(&checkInPayload, payload->Start(), sizeof(checkInPayload)); - chip::ByteSpan payloadByteSpan(checkInPayload); chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(key, payloadByteSpan, counter, appData); return CHIP_NO_ERROR; From b520fa9d3977fad6f1481e2ad6727d5ccfd72b62 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 21:02:58 -0800 Subject: [PATCH 06/93] ICDHandler initialization --- src/app/icd/ICDHandler.cpp | 31 ++++++++++++------- src/app/icd/ICDHandler.h | 4 ++- src/app/icd/client/BUILD.gn | 1 + .../icd/client/DefaultICDClientStorage.cpp | 8 +++-- src/app/icd/client/DefaultICDClientStorage.h | 2 +- src/app/icd/client/ICDClientStorage.h | 2 +- .../secure_channel/CheckinMessage.cpp | 6 ++-- src/protocols/secure_channel/CheckinMessage.h | 4 +-- 8 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/app/icd/ICDHandler.cpp b/src/app/icd/ICDHandler.cpp index 8bfa70084c7975..f84c0268f1791c 100644 --- a/src/app/icd/ICDHandler.cpp +++ b/src/app/icd/ICDHandler.cpp @@ -30,7 +30,7 @@ #include #include #include -#include + #include namespace chip { @@ -42,10 +42,12 @@ CheckInMessageHandler * CheckInMessageHandler::GetInstance() return &sCheckInMessageHandler.get(); } -CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager) +CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage) { VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - mExchangeManager = exchangeManager; + VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + mExchangeManager = exchangeManager; + mICDClientStorage = static_cast(clientStorage); ReturnErrorOnFailure( exchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); @@ -54,7 +56,6 @@ CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeMana void CheckInMessageHandler::Shutdown() { - // TODO : If any timers are added in the future, they need to be cleared here if (mExchangeManager) { mExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); @@ -73,16 +74,24 @@ CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHead CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { - // TODO : Pass the parsed payload to ICDClientManagement via callback VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); - Crypto::Aes128KeyHandle key; - chip::Protocols::SecureChannel::CounterType counter; - MutableByteSpan appData; ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; - chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(key, payloadByteSpan, counter, appData); - - return CHIP_NO_ERROR; + auto * iterator = mICDClientStorage->IterateICDClientInfo(); + CHIP_ERROR err; + uint32_t counter; + ICDClientInfo clientInfo; + while (iterator->Next(clientInfo)) + { + err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, &counter); + if (err == CHIP_NO_ERROR) + { + // TODO-1 : Check if the counter received is in range. If yes, proceed to TODO-2 + // TODO-2 : Call the callback registered by the application to inform about the incoming checkin message + return err; + } + } + return err; } void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} diff --git a/src/app/icd/ICDHandler.h b/src/app/icd/ICDHandler.h index 064d65c1957448..c8e20af578bd1d 100644 --- a/src/app/icd/ICDHandler.h +++ b/src/app/icd/ICDHandler.h @@ -25,6 +25,7 @@ #pragma once +#include #include #include #include @@ -50,7 +51,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi */ static CheckInMessageHandler * GetInstance(void); - CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager); + CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage); void Shutdown(); protected: @@ -67,6 +68,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi private: Messaging::ExchangeManager * mExchangeManager = nullptr; Messaging::ExchangeManager * GetExchangeManager(void) const { return mExchangeManager; } + DefaultICDClientStorage * mICDClientStorage = nullptr; }; } // namespace app diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 8e04d3b586140f..f5cddf72f3575a 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -28,5 +28,6 @@ source_set("manager") { "${chip_root}/src/app:app_config", "${chip_root}/src/crypto", "${chip_root}/src/lib/support", + "${chip_root}/src/protocols", ] } diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 70b2aa035af5a3..651147ad751df1 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace { // FabricIndex is uint8_t, the tlv size with anonymous tag is 1(control bytes) + 1(value) = 2 @@ -460,10 +461,11 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName()); } -CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) +CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) { - // TODO: Need to implement default decription code using CheckinMessage::ParseCheckinMessagePayload - return CHIP_NO_ERROR; + MutableByteSpan appData; + return chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, *counter, + appData); } } // namespace app } // namespace chip diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index be1c8a2f4056ae..2f884b3b65c4fa 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -81,7 +81,7 @@ class DefaultICDClientStorage : public ICDClientStorage CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override; - CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) override; + CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) override; protected: enum class ClientInfoTag : uint8_t diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 1d3e8edceec9f1..7540010317ff2b 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -104,7 +104,7 @@ class ICDClientStorage * @param[in] payload received checkIn Message payload * @param[out] clientInfo retrieved matched clientInfo from storage */ - virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) = 0; + virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) = 0; }; } // namespace app } // namespace chip diff --git a/src/protocols/secure_channel/CheckinMessage.cpp b/src/protocols/secure_channel/CheckinMessage.cpp index 5e63feb3428aa0..10ddd726771d4a 100644 --- a/src/protocols/secure_channel/CheckinMessage.cpp +++ b/src/protocols/secure_channel/CheckinMessage.cpp @@ -62,8 +62,8 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle return err; } -CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, ByteSpan & payload, CounterType & counter, - MutableByteSpan & appData) +CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, + CounterType & counter, MutableByteSpan & appData) { VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(payload.size() <= (sMinPayloadSize + sMaxAppDataSize), CHIP_ERROR_INVALID_ARGUMENT); @@ -92,7 +92,7 @@ CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & return err; } -size_t CheckinMessage::GetAppDataSize(ByteSpan & payload) +size_t CheckinMessage::GetAppDataSize(const ByteSpan & payload) { return (payload.size() <= sMinPayloadSize) ? 0 : payload.size() - sMinPayloadSize; } diff --git a/src/protocols/secure_channel/CheckinMessage.h b/src/protocols/secure_channel/CheckinMessage.h index aa494c3689b5c8..c1809dc88a48c9 100644 --- a/src/protocols/secure_channel/CheckinMessage.h +++ b/src/protocols/secure_channel/CheckinMessage.h @@ -65,7 +65,7 @@ class DLL_EXPORT CheckinMessage * GetAppDataSize(payload) + sizeof(CounterType) * @return CHIP_ERROR */ - static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, ByteSpan & payload, CounterType & counter, + static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData); static inline size_t GetCheckinPayloadSize(size_t appDataSize) { return appDataSize + sMinPayloadSize; } @@ -76,7 +76,7 @@ class DLL_EXPORT CheckinMessage * @param payload The undecrypted payload * @return size_t size in byte of the application data from the payload */ - static size_t GetAppDataSize(ByteSpan & payload); + static size_t GetAppDataSize(const ByteSpan & payload); static constexpr uint16_t sMinPayloadSize = CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES + sizeof(CounterType) + CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES; From fb15b18042fc323296b4a340441a4f741d32d098 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 21:19:22 -0800 Subject: [PATCH 07/93] ICDHandler initialization --- src/app/icd/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index d63312f6ce3571..76249e90fb234f 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -127,5 +127,6 @@ source_set("handler") { "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", + "${chip_root}/src/app/icd/client", ] } From a8421b89c64bb751fbe613f584359d7fdbc906bb Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 3 Dec 2023 05:20:49 +0000 Subject: [PATCH 08/93] Restyled by gn --- src/app/icd/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index 76249e90fb234f..7ce5dd383e9ef6 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -124,9 +124,9 @@ source_set("handler") { ] public_deps = [ + "${chip_root}/src/app/icd/client", "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", - "${chip_root}/src/app/icd/client", ] } From 482d7fffa9fccb5f7bbecd259a07d9c59f548b9e Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 21:25:41 -0800 Subject: [PATCH 09/93] ICDHandler initialization --- src/app/icd/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index 7ce5dd383e9ef6..d03120b751d3ef 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -124,7 +124,7 @@ source_set("handler") { ] public_deps = [ - "${chip_root}/src/app/icd/client", + "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", From 48aecdbcec8c1106b277f438c1623d09fbf4c930 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 21:51:28 -0800 Subject: [PATCH 10/93] ICDHandler initialization --- src/app/icd/BUILD.gn | 16 ---------------- src/app/icd/client/BUILD.gn | 14 ++++++++++++++ src/app/icd/{ => client}/ICDHandler.cpp | 0 src/app/icd/{ => client}/ICDHandler.h | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) rename src/app/icd/{ => client}/ICDHandler.cpp (100%) rename src/app/icd/{ => client}/ICDHandler.h (98%) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index d03120b751d3ef..fcb396b3bcd41b 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -114,19 +114,3 @@ source_set("configuration-data") { "${chip_root}/src/lib/core", ] } - -# ICD Handler source-set is broken out of the main source-set to enable unit tests -# All sources and configurations used by the ICDHandler need to go in this source-set -source_set("handler") { - sources = [ - "ICDHandler.cpp", - "ICDHandler.h", - ] - - public_deps = [ - "${chip_root}/src/app/icd/client:manager", - "${chip_root}/src/lib/core", - "${chip_root}/src/messaging", - "${chip_root}/src/protocols", - ] -} diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index f5cddf72f3575a..8f3c8e33d34bbf 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -31,3 +31,17 @@ source_set("manager") { "${chip_root}/src/protocols", ] } +# ICD Handler source-set is broken out of the main source-set to enable unit tests +# All sources and configurations used by the ICDHandler need to go in this source-set +source_set("handler") { + sources = [ + "ICDHandler.cpp", + "ICDHandler.h", + ] + + public_deps = [ + "${chip_root}/src/lib/core", + "${chip_root}/src/messaging", + "${chip_root}/src/protocols", + ] +} \ No newline at end of file diff --git a/src/app/icd/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp similarity index 100% rename from src/app/icd/ICDHandler.cpp rename to src/app/icd/client/ICDHandler.cpp diff --git a/src/app/icd/ICDHandler.h b/src/app/icd/client/ICDHandler.h similarity index 98% rename from src/app/icd/ICDHandler.h rename to src/app/icd/client/ICDHandler.h index c8e20af578bd1d..275d2edba17218 100644 --- a/src/app/icd/ICDHandler.h +++ b/src/app/icd/client/ICDHandler.h @@ -25,7 +25,7 @@ #pragma once -#include +#include #include #include #include From 0f0dbacce0a632fcfe73263d2680e9244a28a817 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 3 Dec 2023 05:51:56 +0000 Subject: [PATCH 11/93] Restyled by gn --- src/app/icd/client/BUILD.gn | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 8f3c8e33d34bbf..defb94f4f079aa 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -31,6 +31,7 @@ source_set("manager") { "${chip_root}/src/protocols", ] } + # ICD Handler source-set is broken out of the main source-set to enable unit tests # All sources and configurations used by the ICDHandler need to go in this source-set source_set("handler") { @@ -44,4 +45,4 @@ source_set("handler") { "${chip_root}/src/messaging", "${chip_root}/src/protocols", ] -} \ No newline at end of file +} From bec8bf937d08838c672e67580355cc4dd344a3ef Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 22:15:24 -0800 Subject: [PATCH 12/93] ICDHandler initialization --- src/app/icd/client/BUILD.gn | 2 +- src/app/icd/client/ICDHandler.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index defb94f4f079aa..d2d05c4d14a8c9 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -39,7 +39,7 @@ source_set("handler") { "ICDHandler.cpp", "ICDHandler.h", ] - + deps = ["DefaultICDClientStorage.h"] public_deps = [ "${chip_root}/src/lib/core", "${chip_root}/src/messaging", diff --git a/src/app/icd/client/ICDHandler.h b/src/app/icd/client/ICDHandler.h index 275d2edba17218..e1c794341052eb 100644 --- a/src/app/icd/client/ICDHandler.h +++ b/src/app/icd/client/ICDHandler.h @@ -25,7 +25,7 @@ #pragma once -#include +#include "DefaultICDClientStorage.h" #include #include #include From 955eaf1e3d088c4e74fd508a072b84a0c32edbf9 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 3 Dec 2023 06:15:58 +0000 Subject: [PATCH 13/93] Restyled by gn --- src/app/icd/client/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index d2d05c4d14a8c9..b3342e4e9765f9 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -39,7 +39,7 @@ source_set("handler") { "ICDHandler.cpp", "ICDHandler.h", ] - deps = ["DefaultICDClientStorage.h"] + deps = [ "DefaultICDClientStorage.h" ] public_deps = [ "${chip_root}/src/lib/core", "${chip_root}/src/messaging", From 72fec59316f545b7faa20e6f28f6740c865ee625 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 22:25:23 -0800 Subject: [PATCH 14/93] ICDHandler initialization --- src/app/icd/client/BUILD.gn | 2 +- src/app/icd/client/ICDHandler.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index b3342e4e9765f9..aa3b8c6dd65c53 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -39,8 +39,8 @@ source_set("handler") { "ICDHandler.cpp", "ICDHandler.h", ] - deps = [ "DefaultICDClientStorage.h" ] public_deps = [ + ":manager" "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", diff --git a/src/app/icd/client/ICDHandler.h b/src/app/icd/client/ICDHandler.h index e1c794341052eb..d5ed62459678f3 100644 --- a/src/app/icd/client/ICDHandler.h +++ b/src/app/icd/client/ICDHandler.h @@ -25,7 +25,7 @@ #pragma once -#include "DefaultICDClientStorage.h" +#include #include #include #include From 03b0e41bf7b4ad953683cc9f923ae226d09aca96 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 22:27:28 -0800 Subject: [PATCH 15/93] ICDHandler initialization --- src/app/icd/client/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index aa3b8c6dd65c53..9f9e965485aed4 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -40,7 +40,7 @@ source_set("handler") { "ICDHandler.h", ] public_deps = [ - ":manager" + ":manager", "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", From 5a8209da878bd286e59ccfa0cd0d6b943493fce7 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sun, 3 Dec 2023 12:21:05 -0800 Subject: [PATCH 16/93] ICDHandler initialization --- src/app/icd/client/ICDHandler.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp index f84c0268f1791c..371e1907eb4dbf 100644 --- a/src/app/icd/client/ICDHandler.cpp +++ b/src/app/icd/client/ICDHandler.cpp @@ -33,6 +33,8 @@ #include +#define PWRTWO(exp) (1 << (exp)) + namespace chip { namespace app { @@ -78,7 +80,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; auto * iterator = mICDClientStorage->IterateICDClientInfo(); - CHIP_ERROR err; + CHIP_ERROR err = CHIP_NO_ERROR; uint32_t counter; ICDClientInfo clientInfo; while (iterator->Next(clientInfo)) @@ -86,9 +88,18 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, &counter); if (err == CHIP_NO_ERROR) { - // TODO-1 : Check if the counter received is in range. If yes, proceed to TODO-2 - // TODO-2 : Call the callback registered by the application to inform about the incoming checkin message - return err; + auto checkInCounter = (counter - clientInfo.start_icd_counter) % (PWRTWO(32)); + // TAk - If this condition fails, do we notify the application through callback with an error? + if (checkInCounter > clientInfo.offset) + { + clientInfo.offset = counter - clientInfo.start_icd_counter; + if (checkInCounter > PWRTWO(31)) + { + // TODO - refresh key + } + // TODO - Notify the application through callback + return err; + } } } return err; From bbe45611c2693321766f19038d66b756fda74c65 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sun, 3 Dec 2023 12:40:09 -0800 Subject: [PATCH 17/93] ICDHandler initialization --- src/app/icd/client/ICDHandler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp index 371e1907eb4dbf..a908847299ee20 100644 --- a/src/app/icd/client/ICDHandler.cpp +++ b/src/app/icd/client/ICDHandler.cpp @@ -33,7 +33,7 @@ #include -#define PWRTWO(exp) (1 << (exp)) +#define PWRTWO(exp) (1UL << (exp)) namespace chip { namespace app { @@ -93,7 +93,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * if (checkInCounter > clientInfo.offset) { clientInfo.offset = counter - clientInfo.start_icd_counter; - if (checkInCounter > PWRTWO(31)) + if (checkInCounter > (uint32_t) PWRTWO(31)) { // TODO - refresh key } From 8fed72c4178d6168ca020bdf3572535b2c80b9ff Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sun, 3 Dec 2023 12:57:07 -0800 Subject: [PATCH 18/93] ICDHandler initialization --- src/app/icd/client/ICDHandler.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp index a908847299ee20..720d141eed43ac 100644 --- a/src/app/icd/client/ICDHandler.cpp +++ b/src/app/icd/client/ICDHandler.cpp @@ -33,11 +33,12 @@ #include -#define PWRTWO(exp) (1UL << (exp)) - namespace chip { namespace app { +inline constexpr uint32_t kCheckInCounterMax = UINT32_MAX; +inline constexpr uint32_t kCheckInRolloverConstant = (1U << 31); + static Global sCheckInMessageHandler; CheckInMessageHandler * CheckInMessageHandler::GetInstance() { @@ -88,12 +89,12 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, &counter); if (err == CHIP_NO_ERROR) { - auto checkInCounter = (counter - clientInfo.start_icd_counter) % (PWRTWO(32)); + auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; // TAk - If this condition fails, do we notify the application through callback with an error? if (checkInCounter > clientInfo.offset) { clientInfo.offset = counter - clientInfo.start_icd_counter; - if (checkInCounter > (uint32_t) PWRTWO(31)) + if (checkInCounter > kCheckInRolloverConstant) { // TODO - refresh key } From f2152fec84c7d5605bbab9178bf64209b5befc76 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sun, 3 Dec 2023 15:34:29 -0800 Subject: [PATCH 19/93] Moved counter validation to DefaultICDClientStorage. --- .../icd/client/DefaultICDClientStorage.cpp | 33 +++++++++++++++++-- src/app/icd/client/DefaultICDClientStorage.h | 4 ++- src/app/icd/client/ICDClientStorage.h | 2 +- src/app/icd/client/ICDHandler.cpp | 20 ++--------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 651147ad751df1..632b46f4d46fdd 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -40,6 +40,10 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { + +inline constexpr uint32_t kCheckInCounterMax = UINT32_MAX; +inline constexpr uint32_t kCheckInRolloverConstant = (1U << 31); + CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { for (auto & fabric_idx : mFabricList) @@ -461,11 +465,34 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName()); } -CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) +CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) { + uint32_t counter; MutableByteSpan appData; - return chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, *counter, - appData); + VerifyOrReturnError(chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload( + clientInfo.shared_key, payload, counter, appData) == CHIP_NO_ERROR, + CHIP_ERROR_INVALID_ARGUMENT); + auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_ARGUMENT); + clientInfo.offset = counter - clientInfo.start_icd_counter; + if (checkInCounter > kCheckInRolloverConstant) + { + RefreshKeyAndRegisterClient(clientInfo); + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR DefaultICDClientStorage::RefreshKeyAndRegisterClient(ICDClientInfo & clientInfo) +{ + uint8_t randomGeneratedICDSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length]; + chip::Optional icdSymmetricKey; + chip::Crypto::DRBG_get_bytes(randomGeneratedICDSymmetricKey, sizeof(randomGeneratedICDSymmetricKey)); + icdSymmetricKey.SetValue(ByteSpan(randomGeneratedICDSymmetricKey)); + ReturnErrorOnFailure(SetKey(clientInfo, icdSymmetricKey.Value())); + ReturnErrorOnFailure(StoreEntry(clientInfo)); + // TODO - Register client with new key and node ID + return CHIP_NO_ERROR; } } // namespace app } // namespace chip diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index 2f884b3b65c4fa..db2353b7a4a121 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -81,7 +81,9 @@ class DefaultICDClientStorage : public ICDClientStorage CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override; - CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) override; + CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) override; + + CHIP_ERROR RefreshKeyAndRegisterClient(ICDClientInfo & clientInfo); protected: enum class ClientInfoTag : uint8_t diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 7540010317ff2b..1d3e8edceec9f1 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -104,7 +104,7 @@ class ICDClientStorage * @param[in] payload received checkIn Message payload * @param[out] clientInfo retrieved matched clientInfo from storage */ - virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) = 0; + virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) = 0; }; } // namespace app } // namespace chip diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp index 720d141eed43ac..948be1f6150b7a 100644 --- a/src/app/icd/client/ICDHandler.cpp +++ b/src/app/icd/client/ICDHandler.cpp @@ -36,9 +36,6 @@ namespace chip { namespace app { -inline constexpr uint32_t kCheckInCounterMax = UINT32_MAX; -inline constexpr uint32_t kCheckInRolloverConstant = (1U << 31); - static Global sCheckInMessageHandler; CheckInMessageHandler * CheckInMessageHandler::GetInstance() { @@ -82,25 +79,14 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; auto * iterator = mICDClientStorage->IterateICDClientInfo(); CHIP_ERROR err = CHIP_NO_ERROR; - uint32_t counter; ICDClientInfo clientInfo; while (iterator->Next(clientInfo)) { - err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, &counter); + err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo); if (err == CHIP_NO_ERROR) { - auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - // TAk - If this condition fails, do we notify the application through callback with an error? - if (checkInCounter > clientInfo.offset) - { - clientInfo.offset = counter - clientInfo.start_icd_counter; - if (checkInCounter > kCheckInRolloverConstant) - { - // TODO - refresh key - } - // TODO - Notify the application through callback - return err; - } + // TODO - Notify checkin complete to the application through callback + return err; } } return err; From 2af67dfab5a625dcd23cc717abce8f206fe12906 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Mon, 4 Dec 2023 19:56:34 -0800 Subject: [PATCH 20/93] Renamed ICDHandler to CheckInHandler --- src/app/icd/client/BUILD.gn | 6 ++-- src/app/icd/client/CheckInDelegate.h | 33 +++++++++++++++++++ .../{ICDHandler.cpp => CheckInHandler.cpp} | 4 +-- .../client/{ICDHandler.h => CheckInHandler.h} | 0 .../icd/client/DefaultICDClientStorage.cpp | 2 +- 5 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 src/app/icd/client/CheckInDelegate.h rename src/app/icd/client/{ICDHandler.cpp => CheckInHandler.cpp} (96%) rename src/app/icd/client/{ICDHandler.h => CheckInHandler.h} (100%) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 9f9e965485aed4..24a5149aa96785 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -33,11 +33,11 @@ source_set("manager") { } # ICD Handler source-set is broken out of the main source-set to enable unit tests -# All sources and configurations used by the ICDHandler need to go in this source-set +# All sources and configurations used by the CheckInHandler need to go in this source-set source_set("handler") { sources = [ - "ICDHandler.cpp", - "ICDHandler.h", + "CheckInHandler.cpp", + "CheckInHandler.h", ] public_deps = [ ":manager", diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h new file mode 100644 index 00000000000000..fc19e69950e2b9 --- /dev/null +++ b/src/app/icd/client/CheckInDelegate.h @@ -0,0 +1,33 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +namespace chip { +namespace app { + +/// Callbacks for check in protocol +class DLL_EXPORT CheckInDelegate +{ +public: + virtual ~CheckInDelegate() {} + virtual void OnCheckInComplete(void) = 0; +}; + +} // namespace app +} // namespace chip diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/CheckInHandler.cpp similarity index 96% rename from src/app/icd/client/ICDHandler.cpp rename to src/app/icd/client/CheckInHandler.cpp index 948be1f6150b7a..36ff6f6e0182ee 100644 --- a/src/app/icd/client/ICDHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -22,7 +22,7 @@ * */ -#include "ICDHandler.h" +#include "CheckInHandler.h" #include @@ -85,7 +85,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo); if (err == CHIP_NO_ERROR) { - // TODO - Notify checkin complete to the application through callback + OnCheckInComplete(); return err; } } diff --git a/src/app/icd/client/ICDHandler.h b/src/app/icd/client/CheckInHandler.h similarity index 100% rename from src/app/icd/client/ICDHandler.h rename to src/app/icd/client/CheckInHandler.h diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 632b46f4d46fdd..0bd46feb8db4a7 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -489,9 +489,9 @@ CHIP_ERROR DefaultICDClientStorage::RefreshKeyAndRegisterClient(ICDClientInfo & chip::Optional icdSymmetricKey; chip::Crypto::DRBG_get_bytes(randomGeneratedICDSymmetricKey, sizeof(randomGeneratedICDSymmetricKey)); icdSymmetricKey.SetValue(ByteSpan(randomGeneratedICDSymmetricKey)); + // TODO - Register client with new key and node ID ReturnErrorOnFailure(SetKey(clientInfo, icdSymmetricKey.Value())); ReturnErrorOnFailure(StoreEntry(clientInfo)); - // TODO - Register client with new key and node ID return CHIP_NO_ERROR; } } // namespace app From c1fbf04627ced19ac4ff44bdb14ae8c7fe5b0c35 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Mon, 4 Dec 2023 21:29:34 -0800 Subject: [PATCH 21/93] Included CheckInDelegate in BUILD.gn --- src/app/icd/client/BUILD.gn | 1 + src/app/icd/client/CheckInHandler.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 24a5149aa96785..b7c2d477673c5b 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -38,6 +38,7 @@ source_set("handler") { sources = [ "CheckInHandler.cpp", "CheckInHandler.h", + "CheckInDelegate.h", ] public_deps = [ ":manager", diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 36ff6f6e0182ee..c2b4393e8f8933 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -23,6 +23,7 @@ */ #include "CheckInHandler.h" +#include "CheckInDelegate.h" #include From d2559d75aad3b909f7222b1e4756e18c2fc3ef9a Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 5 Dec 2023 05:30:06 +0000 Subject: [PATCH 22/93] Restyled by gn --- src/app/icd/client/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index b7c2d477673c5b..07045c85db4e86 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -36,9 +36,9 @@ source_set("manager") { # All sources and configurations used by the CheckInHandler need to go in this source-set source_set("handler") { sources = [ + "CheckInDelegate.h", "CheckInHandler.cpp", "CheckInHandler.h", - "CheckInDelegate.h", ] public_deps = [ ":manager", From 0ce35c2572b6811642386e735fa0a555450b525f Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 15:03:58 -0800 Subject: [PATCH 23/93] Added DefaultCheckInDelegate --- src/app/icd/client/BUILD.gn | 2 + src/app/icd/client/CheckInDelegate.h | 4 +- src/app/icd/client/CheckInHandler.cpp | 38 +++++++---------- src/app/icd/client/CheckInHandler.h | 11 +---- src/app/icd/client/DefaultCheckInDelegate.cpp | 31 ++++++++++++++ src/app/icd/client/DefaultCheckInDelegate.h | 35 ++++++++++++++++ .../icd/client/DefaultICDClientStorage.cpp | 41 ++++++++----------- src/app/icd/client/DefaultICDClientStorage.h | 6 +-- src/app/icd/client/ICDClientStorage.h | 3 +- 9 files changed, 109 insertions(+), 62 deletions(-) create mode 100644 src/app/icd/client/DefaultCheckInDelegate.cpp create mode 100644 src/app/icd/client/DefaultCheckInDelegate.h diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 07045c85db4e86..3773100063fcfc 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -37,6 +37,8 @@ source_set("manager") { source_set("handler") { sources = [ "CheckInDelegate.h", + "DefaultCheckInDelegate.h", + "DefaultCheckInDelegate.cpp", "CheckInHandler.cpp", "CheckInHandler.h", ] diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index fc19e69950e2b9..4322b7b6aa5c01 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -18,6 +18,8 @@ #pragma once +#include + namespace chip { namespace app { @@ -26,7 +28,7 @@ class DLL_EXPORT CheckInDelegate { public: virtual ~CheckInDelegate() {} - virtual void OnCheckInComplete(void) = 0; + virtual void OnCheckInComplete(const ICDClientInfo & clientInfo, bool & needRefreshKey) = 0; }; } // namespace app diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index c2b4393e8f8933..58722a79aef2bd 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2020-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,8 +22,8 @@ * */ -#include "CheckInHandler.h" -#include "CheckInDelegate.h" +#include +#include #include @@ -37,18 +37,14 @@ namespace chip { namespace app { -static Global sCheckInMessageHandler; -CheckInMessageHandler * CheckInMessageHandler::GetInstance() +CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, + CheckInDelegate * delegate) { - return &sCheckInMessageHandler.get(); -} - -CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage) -{ - VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INCORRECT_STATE); mExchangeManager = exchangeManager; mICDClientStorage = static_cast(clientStorage); + mCheckInDelegate = delegate; ReturnErrorOnFailure( exchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); @@ -78,19 +74,13 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; - auto * iterator = mICDClientStorage->IterateICDClientInfo(); - CHIP_ERROR err = CHIP_NO_ERROR; ICDClientInfo clientInfo; - while (iterator->Next(clientInfo)) - { - err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo); - if (err == CHIP_NO_ERROR) - { - OnCheckInComplete(); - return err; - } - } - return err; + bool needRefreshKey; + + ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; + VerifyOrReturnError(mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey)); + mCheckInDelegate->OnCheckInComplete(clientInfo, needRefreshKey); + return CHIP_NO_ERROR; } void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index d5ed62459678f3..7ac5daec5f6f9d 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -43,15 +43,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi }; public: - /** - * @brief Retrieve the singleton CheckIn handler - * - * @return A pointer to the shared CheckIn handler - * - */ - static CheckInMessageHandler * GetInstance(void); - - CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage); + CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); void Shutdown(); protected: @@ -67,6 +59,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi private: Messaging::ExchangeManager * mExchangeManager = nullptr; + CheckInDelegate * mCheckInDelegate = nullptr; Messaging::ExchangeManager * GetExchangeManager(void) const { return mExchangeManager; } DefaultICDClientStorage * mICDClientStorage = nullptr; }; diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp new file mode 100644 index 00000000000000..29d6d94fe9c1c5 --- /dev/null +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +namespace chip { +namespace app { + +void DefaultCheckInDelegate ::OnCheckInComplete(const ICDClientInfo & clientInfo, bool & needRegisterICD) +{ + ChipLogProgress(ICDClient, "Check In Message preocessing complete"); +} + +} // namespace app +} // namespace chip \ No newline at end of file diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h new file mode 100644 index 00000000000000..4f793776a26e0e --- /dev/null +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -0,0 +1,35 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +namespace chip { +namespace app { + +/// Callbacks for check in protocol +class DefaultCheckInDelegate : public CheckInDelegate +{ +public: + virtual ~DefaultCheckInDelegate() {} + void OnCheckInComplete(const ICDClientInfo & clientInfo, bool & needRegisterICD) override; +}; + +} // namespace app +} // namespace chip diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 0bd46feb8db4a7..9fa938570a7da2 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "DefaultICDClientStorage.h" +#include #include #include #include @@ -465,34 +465,29 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName()); } -CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) +CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) { uint32_t counter; MutableByteSpan appData; - VerifyOrReturnError(chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload( - clientInfo.shared_key, payload, counter, appData) == CHIP_NO_ERROR, - CHIP_ERROR_INVALID_ARGUMENT); - auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_ARGUMENT); - clientInfo.offset = counter - clientInfo.start_icd_counter; - if (checkInCounter > kCheckInRolloverConstant) + auto * iterator = IterateICDClientInfo(); + while (iterator->Next(clientInfo)) { - RefreshKeyAndRegisterClient(clientInfo); + if (CHIP_NO_ERROR == + chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, counter, + appData)) + { + auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + refreshKey = false; + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_ARGUMENT); + clientInfo.offset = counter - clientInfo.start_icd_counter; + if (checkInCounter > kCheckInRolloverConstant) + { + refreshKey = true; + } + return CHIP_NO_ERROR; + } } return CHIP_NO_ERROR; } - -CHIP_ERROR DefaultICDClientStorage::RefreshKeyAndRegisterClient(ICDClientInfo & clientInfo) -{ - uint8_t randomGeneratedICDSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length]; - chip::Optional icdSymmetricKey; - chip::Crypto::DRBG_get_bytes(randomGeneratedICDSymmetricKey, sizeof(randomGeneratedICDSymmetricKey)); - icdSymmetricKey.SetValue(ByteSpan(randomGeneratedICDSymmetricKey)); - // TODO - Register client with new key and node ID - ReturnErrorOnFailure(SetKey(clientInfo, icdSymmetricKey.Value())); - ReturnErrorOnFailure(StoreEntry(clientInfo)); - return CHIP_NO_ERROR; -} } // namespace app } // namespace chip diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index db2353b7a4a121..b2b5b444f561d6 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -21,7 +21,7 @@ #pragma once -#include "ICDClientStorage.h" +#include #include #include @@ -81,9 +81,7 @@ class DefaultICDClientStorage : public ICDClientStorage CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override; - CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) override; - - CHIP_ERROR RefreshKeyAndRegisterClient(ICDClientInfo & clientInfo); + CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) override; protected: enum class ClientInfoTag : uint8_t diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 1d3e8edceec9f1..c00a5ea56f5628 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -103,8 +103,9 @@ class ICDClientStorage * and populate the clientInfo with the stored information corresponding to the key. * @param[in] payload received checkIn Message payload * @param[out] clientInfo retrieved matched clientInfo from storage + * @param[out] refreshKey set to true if the counter value reaches 2 ^ 31 indicating the key needs to be refreshed */ - virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) = 0; + virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) = 0; }; } // namespace app } // namespace chip From a3f50e566b8b78274c08ce1cf9080d5fd2250891 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 5 Dec 2023 23:04:27 +0000 Subject: [PATCH 24/93] Restyled by whitespace --- src/app/icd/client/DefaultCheckInDelegate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 29d6d94fe9c1c5..a3d9457e9543a0 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -28,4 +28,4 @@ void DefaultCheckInDelegate ::OnCheckInComplete(const ICDClientInfo & clientInfo } } // namespace app -} // namespace chip \ No newline at end of file +} // namespace chip From 11693eac3ba613dd091d0782c6b4b24c7e6dd811 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 5 Dec 2023 23:04:29 +0000 Subject: [PATCH 25/93] Restyled by gn --- src/app/icd/client/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 3773100063fcfc..88d31ff458f29f 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -37,10 +37,10 @@ source_set("manager") { source_set("handler") { sources = [ "CheckInDelegate.h", - "DefaultCheckInDelegate.h", - "DefaultCheckInDelegate.cpp", "CheckInHandler.cpp", "CheckInHandler.h", + "DefaultCheckInDelegate.cpp", + "DefaultCheckInDelegate.h", ] public_deps = [ ":manager", From 94d4bd78b6ac68295a446f8f3ae104748ba4440c Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 16:08:18 -0800 Subject: [PATCH 26/93] Modified error codes --- src/app/icd/client/CheckInDelegate.h | 9 ++++- src/app/icd/client/CheckInHandler.cpp | 33 ++++++++++--------- src/app/icd/client/CheckInHandler.h | 17 +++------- src/app/icd/client/DefaultCheckInDelegate.cpp | 2 +- src/app/icd/client/DefaultCheckInDelegate.h | 2 +- .../icd/client/DefaultICDClientStorage.cpp | 3 +- src/lib/support/logging/Constants.h | 5 +++ 7 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index 4322b7b6aa5c01..f68499d66149ce 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -28,7 +28,14 @@ class DLL_EXPORT CheckInDelegate { public: virtual ~CheckInDelegate() {} - virtual void OnCheckInComplete(const ICDClientInfo & clientInfo, bool & needRefreshKey) = 0; + + /** + * @brief Callback used to let the application know that a checkin message was received and validated. + * + * @param[out] clientInfo - ClientInfo object of the peer node + * @param[out] needRefreshKey - Indicates if the application should refresh the exisiting key + */ + virtual void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) = 0; }; } // namespace app diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 58722a79aef2bd..ffbd28923930ae 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -40,29 +40,32 @@ namespace app { CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate) { - VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INCORRECT_STATE); - mExchangeManager = exchangeManager; - mICDClientStorage = static_cast(clientStorage); - mCheckInDelegate = delegate; + VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(mpExchangeManager == nullptr, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mpICDClientStorage == nullptr, CHIP_ERROR_INCORRECT_STATE); + mpExchangeManager = exchangeManager; + mpICDClientStorage = clientStorage; + mpCheckInDelegate = delegate; ReturnErrorOnFailure( - exchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); + mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); return CHIP_NO_ERROR; } void CheckInMessageHandler::Shutdown() { - if (mExchangeManager) + if (mpExchangeManager) { - mExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); - mExchangeManager = nullptr; + mpExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); + mpExchangeManager = nullptr; } } CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) { // Return error for wrong message type - VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), + CHIP_ERROR_INVALID_MESSAGE_TYPE); newDelegate = this; return CHIP_NO_ERROR; @@ -71,14 +74,14 @@ CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHead CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { - VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), + CHIP_ERROR_INVALID_MESSAGE_TYPE); ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; - bool needRefreshKey; - - ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; - VerifyOrReturnError(mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey)); + bool needRefreshKey = false; + VerifyOrReturnError(mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey), + CHIP_ERROR_INCORRECT_STATE); mCheckInDelegate->OnCheckInComplete(clientInfo, needRefreshKey); return CHIP_NO_ERROR; } diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index 7ac5daec5f6f9d..fd3a91b449513a 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -34,13 +34,6 @@ namespace chip { namespace app { class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler { - class Callback - { - public: - virtual ~Callback() = default; - - // TODO : Include the callback message from ICDClientManagement - }; public: CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); @@ -58,10 +51,10 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi void OnResponseTimeout(Messaging::ExchangeContext * ec) override; private: - Messaging::ExchangeManager * mExchangeManager = nullptr; - CheckInDelegate * mCheckInDelegate = nullptr; - Messaging::ExchangeManager * GetExchangeManager(void) const { return mExchangeManager; } - DefaultICDClientStorage * mICDClientStorage = nullptr; + Messaging::ExchangeManager * mpExchangeManager = nullptr; + CheckInDelegate * mpCheckInDelegate = nullptr; + Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } + ICDClientStorage * mpICDClientStorage = nullptr; }; } // namespace app diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index a3d9457e9543a0..fd8ea81301736b 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -22,7 +22,7 @@ namespace chip { namespace app { -void DefaultCheckInDelegate ::OnCheckInComplete(const ICDClientInfo & clientInfo, bool & needRegisterICD) +void DefaultCheckInDelegate ::OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) { ChipLogProgress(ICDClient, "Check In Message preocessing complete"); } diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index 4f793776a26e0e..637af4d1b13364 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -28,7 +28,7 @@ class DefaultCheckInDelegate : public CheckInDelegate { public: virtual ~DefaultCheckInDelegate() {} - void OnCheckInComplete(const ICDClientInfo & clientInfo, bool & needRegisterICD) override; + void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) override; }; } // namespace app diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 9fa938570a7da2..0235d629c89a7c 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -477,8 +477,7 @@ CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & paylo appData)) { auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - refreshKey = false; - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_SIGNATURE); clientInfo.offset = counter - clientInfo.start_icd_counter; if (checkInCounter > kCheckInRolloverConstant) { diff --git a/src/lib/support/logging/Constants.h b/src/lib/support/logging/Constants.h index 5abf073bf48a68..52e794a6c3ccd1 100644 --- a/src/lib/support/logging/Constants.h +++ b/src/lib/support/logging/Constants.h @@ -59,6 +59,7 @@ enum LogModule kLogModule_OperationalSessionSetup, kLogModule_Automation, kLogModule_CASESessionManager, + kLogModule_ICDClient, kLogModule_Max }; @@ -226,6 +227,10 @@ enum LogModule #define CHIP_CONFIG_LOG_MODULE_CASESessionManager 1 #endif +#ifndef CHIP_CONFIG_LOG_MODULE_ICDClient +#define CHIP_CONFIG_LOG_MODULE_ICDClient 1 +#endif + /** * @enum LogCategory * From 41ab1ea01d1fd32045f237fe3edbe9d08fc5ba43 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 16:17:13 -0800 Subject: [PATCH 27/93] Updated variables --- src/app/icd/client/CheckInHandler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index ffbd28923930ae..105dd9dd8c7a52 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -80,9 +80,9 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; bool needRefreshKey = false; - VerifyOrReturnError(mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey), + VerifyOrReturnError(mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey), CHIP_ERROR_INCORRECT_STATE); - mCheckInDelegate->OnCheckInComplete(clientInfo, needRefreshKey); + mpCheckInDelegate->OnCheckInComplete(clientInfo, needRefreshKey); return CHIP_NO_ERROR; } From 2834b0f6855e89859df7c692f3e4674427f0d5f6 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 16:29:30 -0800 Subject: [PATCH 28/93] Added condition for VerifyOrReturnError --- src/app/icd/client/CheckInHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 105dd9dd8c7a52..cdd5d0282f9630 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -80,7 +80,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; bool needRefreshKey = false; - VerifyOrReturnError(mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey), + VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey), CHIP_ERROR_INCORRECT_STATE); mpCheckInDelegate->OnCheckInComplete(clientInfo, needRefreshKey); return CHIP_NO_ERROR; From 6b14bc8ea0f6078707cb94f0b3a41286f7f2db19 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 17:00:01 -0800 Subject: [PATCH 29/93] Resolve conflicts with master branch --- src/protocols/secure_channel/CheckinMessage.cpp | 2 +- src/protocols/secure_channel/CheckinMessage.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/protocols/secure_channel/CheckinMessage.cpp b/src/protocols/secure_channel/CheckinMessage.cpp index 10ddd726771d4a..67f5b0563d62fc 100644 --- a/src/protocols/secure_channel/CheckinMessage.cpp +++ b/src/protocols/secure_channel/CheckinMessage.cpp @@ -62,7 +62,7 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle return err; } -CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, +CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128BitsKeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData) { VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT); diff --git a/src/protocols/secure_channel/CheckinMessage.h b/src/protocols/secure_channel/CheckinMessage.h index c1809dc88a48c9..6c23c67eea550d 100644 --- a/src/protocols/secure_channel/CheckinMessage.h +++ b/src/protocols/secure_channel/CheckinMessage.h @@ -65,7 +65,8 @@ class DLL_EXPORT CheckinMessage * GetAppDataSize(payload) + sizeof(CounterType) * @return CHIP_ERROR */ - static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, CounterType & counter, + + static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128BitsKeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData); static inline size_t GetCheckinPayloadSize(size_t appDataSize) { return appDataSize + sMinPayloadSize; } From 7b9cd3842bcc62e3803a376d4c68e2d93cdca323 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 21:19:58 -0800 Subject: [PATCH 30/93] Added CheckInHandler init in chiptool --- .../chip-tool/commands/common/CHIPCommand.cpp | 4 ++++ .../chip-tool/commands/common/CHIPCommand.h | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 7e721734cd1a80..52b3ec698f73df 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -102,6 +102,7 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnLogErrorOnFailure(mDefaultStorage.Init(nullptr, GetStorageDirectory().ValueOr(nullptr))); ReturnLogErrorOnFailure(mOperationalKeystore.Init(&mDefaultStorage)); ReturnLogErrorOnFailure(mOpCertStore.Init(&mDefaultStorage)); + ReturnLogErrorOnFailure(mICDClientStorage.Init(&mDefaultStorage, &mSessionKeystore)); // chip-tool uses a non-persistent keystore. // ICD storage lifetime is currently tied to the chip-tool's lifetime. Since chip-tool interactive mode is currently used for @@ -139,6 +140,9 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnErrorOnFailure(GetAttestationTrustStore(mPaaTrustStorePath.ValueOr(nullptr), &sTrustStore)); + ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState().ExchangeMgr(), + &mICDClientStorage, &mCheckInDelegate)); + CommissionerIdentity nullIdentity{ kIdentityNull, chip::kUndefinedNodeId }; ReturnLogErrorOnFailure(InitializeCommissioner(nullIdentity, kIdentityNullFabricId)); diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 3376e47ba0f20d..2ca335c66d109c 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -25,6 +25,8 @@ #include "Command.h" #include +#include +#include #include #include #include @@ -127,7 +129,10 @@ class CHIPCommand : public Command // Shut down the command. After a Shutdown call the command object is ready // to be used for another command invocation. - virtual void Shutdown() { ResetArguments(); } + virtual void Shutdown() + { + ResetArguments(); + } // Clean up any resources allocated by the command. Some commands may hold // on to resources after Shutdown(), but Cleanup() will guarantee those are @@ -138,12 +143,18 @@ class CHIPCommand : public Command // can keep doing work as needed. Cleanup() will be called when quitting // interactive mode. This method will be called before Shutdown, so it can // use member values that Shutdown will normally reset. - virtual bool DeferInteractiveCleanup() { return false; } + virtual bool DeferInteractiveCleanup() + { + return false; + } // If true, the controller will be created with server capabilities enabled, // such as advertising operational nodes over DNS-SD and accepting incoming // CASE sessions. - virtual bool NeedsOperationalAdvertising() { return mAdvertiseOperational; } + virtual bool NeedsOperationalAdvertising() + { + return mAdvertiseOperational; + } // Execute any deferred cleanups. Used when exiting interactive mode. static void ExecuteDeferredCleanups(intptr_t ignored); From 5226d1a309f1dbfcc0460dac4e6a0bba731e91aa Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 6 Dec 2023 05:20:27 +0000 Subject: [PATCH 31/93] Restyled by clang-format --- examples/chip-tool/commands/common/CHIPCommand.h | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 2ca335c66d109c..61638ad8c61eaf 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -129,10 +129,7 @@ class CHIPCommand : public Command // Shut down the command. After a Shutdown call the command object is ready // to be used for another command invocation. - virtual void Shutdown() - { - ResetArguments(); - } + virtual void Shutdown() { ResetArguments(); } // Clean up any resources allocated by the command. Some commands may hold // on to resources after Shutdown(), but Cleanup() will guarantee those are @@ -143,18 +140,12 @@ class CHIPCommand : public Command // can keep doing work as needed. Cleanup() will be called when quitting // interactive mode. This method will be called before Shutdown, so it can // use member values that Shutdown will normally reset. - virtual bool DeferInteractiveCleanup() - { - return false; - } + virtual bool DeferInteractiveCleanup() { return false; } // If true, the controller will be created with server capabilities enabled, // such as advertising operational nodes over DNS-SD and accepting incoming // CASE sessions. - virtual bool NeedsOperationalAdvertising() - { - return mAdvertiseOperational; - } + virtual bool NeedsOperationalAdvertising() { return mAdvertiseOperational; } // Execute any deferred cleanups. Used when exiting interactive mode. static void ExecuteDeferredCleanups(intptr_t ignored); From f6a7b012350f2cd288e97c47d628ed64e896556e Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 6 Dec 2023 05:20:28 +0000 Subject: [PATCH 32/93] Restyled by gn --- examples/chip-tool/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 7a0b440ad536dc..53568316114c9a 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -102,6 +102,7 @@ static_library("chip-tool-utils") { public_deps = [ "${chip_root}/examples/common/tracing:commandline", + "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/app/server", "${chip_root}/src/app/tests/suites/commands/interaction_model", From 5be52afee4918d93afac2882f976ccd0ea56012b Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 21:46:14 -0800 Subject: [PATCH 33/93] Added include file --- src/app/icd/client/CheckInHandler.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index fd3a91b449513a..a207e278adc631 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -25,6 +25,7 @@ #pragma once +#include #include #include #include From 22ad2e69b5a6ca4768fa7d7a45e6dccbfb55abc5 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 21:55:52 -0800 Subject: [PATCH 34/93] Modified the access operator --- examples/chip-tool/commands/common/CHIPCommand.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 52b3ec698f73df..ce54c9f7d38281 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -140,7 +140,7 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnErrorOnFailure(GetAttestationTrustStore(mPaaTrustStorePath.ValueOr(nullptr), &sTrustStore)); - ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState().ExchangeMgr(), + ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(), &mICDClientStorage, &mCheckInDelegate)); CommissionerIdentity nullIdentity{ kIdentityNull, chip::kUndefinedNodeId }; From d215f5f553d46e4c0eb52689636f6dcc8c89c519 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 6 Dec 2023 00:02:55 -0800 Subject: [PATCH 35/93] Added ICD client deps for tv-casting-app --- examples/tv-casting-app/tv-casting-common/BUILD.gn | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/tv-casting-app/tv-casting-common/BUILD.gn b/examples/tv-casting-app/tv-casting-common/BUILD.gn index c5ad03964cb0d9..30be2af7ceb529 100644 --- a/examples/tv-casting-app/tv-casting-common/BUILD.gn +++ b/examples/tv-casting-app/tv-casting-common/BUILD.gn @@ -117,6 +117,8 @@ chip_data_model("tv-casting-common") { "${chip_root}/src/tracing/json", "${chip_root}/third_party/inipp", "${chip_root}/third_party/jsoncpp", + "${chip_root}/src/app/icd/client:handler", + "${chip_root}/src/app/icd/client:manager", ] if (chip_enable_transport_trace) { From f9c8d587dc41eb3114b93834b64abedec1863cfd Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 6 Dec 2023 08:03:34 +0000 Subject: [PATCH 36/93] Restyled by gn --- examples/tv-casting-app/tv-casting-common/BUILD.gn | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/tv-casting-app/tv-casting-common/BUILD.gn b/examples/tv-casting-app/tv-casting-common/BUILD.gn index 30be2af7ceb529..8a2d6767b2b39a 100644 --- a/examples/tv-casting-app/tv-casting-common/BUILD.gn +++ b/examples/tv-casting-app/tv-casting-common/BUILD.gn @@ -110,6 +110,7 @@ chip_data_model("tv-casting-common") { deps = [ "${chip_root}/examples/common/tracing:commandline", + "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/app/tests/suites/commands/interaction_model", "${chip_root}/src/lib/support/jsontlv", @@ -117,8 +118,6 @@ chip_data_model("tv-casting-common") { "${chip_root}/src/tracing/json", "${chip_root}/third_party/inipp", "${chip_root}/third_party/jsoncpp", - "${chip_root}/src/app/icd/client:handler", - "${chip_root}/src/app/icd/client:manager", ] if (chip_enable_transport_trace) { From 7159a7c7f470db0ec9b7428ac40a703ebc27185c Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 6 Dec 2023 00:13:03 -0800 Subject: [PATCH 37/93] Added unit test case for ProcessCheckInPayload --- .../icd/client/DefaultICDClientStorage.cpp | 1 + src/app/tests/TestDefaultICDClientStorage.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 0235d629c89a7c..a8d9e756705de0 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -486,6 +486,7 @@ CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & paylo return CHIP_NO_ERROR; } } + iterator->Release(); return CHIP_NO_ERROR; } } // namespace app diff --git a/src/app/tests/TestDefaultICDClientStorage.cpp b/src/app/tests/TestDefaultICDClientStorage.cpp index 730747232455c6..37266b49acf332 100644 --- a/src/app/tests/TestDefaultICDClientStorage.cpp +++ b/src/app/tests/TestDefaultICDClientStorage.cpp @@ -15,13 +15,17 @@ * limitations under the License. */ +#include #include #include +#include +#include #include #include #include #include +#include using namespace chip; using namespace app; @@ -200,6 +204,43 @@ void TestClientInfoCountMultipleFabric(nlTestSuite * apSuite, void * apContext) NL_TEST_ASSERT(apSuite, count == 0); } +void TestProcessCheckInPayload(nlTestSuite * apSuite, void * apContext) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + FabricIndex fabricId = 1; + NodeId nodeId = 6666; + TestPersistentStorageDelegate clientInfoStorage; + TestSessionKeystoreImpl keystore; + + DefaultICDClientStorage manager; + err = manager.Init(&clientInfoStorage, &keystore); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = manager.UpdateFabricList(fabricId); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + // Populate clientInfo + ICDClientInfo clientInfo1; + clientInfo1.peer_node = ScopedNodeId(nodeId, fabricId); + + err = manager.SetKey(clientInfo1, ByteSpan(kKeyBuffer1)); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = manager.StoreEntry(clientInfo1); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + + uint32_t counter = 1; + System::PacketBufferHandle buffer = MessagePacketBuffer::New(chip::Protocols::SecureChannel::CheckinMessage::sMinPayloadSize); + MutableByteSpan output{ buffer->Start(), buffer->MaxDataLength() }; + err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload(clientInfo1.shared_key, counter, ByteSpan(), + output); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + + buffer->SetDataLength(static_cast(output.size())); + ICDClientInfo clientInfo; + ByteSpan payload{ buffer->Start(), buffer->DataLength() }; + bool refreshKey; + err = manager.ProcessCheckInPayload(payload, clientInfo, refreshKey); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); +} + /** * Set up the test suite. */ @@ -229,6 +270,8 @@ static const nlTest sTests[] = { NL_TEST_DEF("TestClientInfoCount", TestClientInfoCount), NL_TEST_DEF("TestClientInfoCountMultipleFabric", TestClientInfoCountMultipleFabric), + NL_TEST_DEF("TestProcessCheckInPayload", TestProcessCheckInPayload), + NL_TEST_SENTINEL() }; // clang-format on From 2c46716cd3b817712d2d776ac4e909dc4f104c72 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 6 Dec 2023 09:47:00 -0800 Subject: [PATCH 38/93] Updated doxygen comments --- src/app/icd/client/CheckInDelegate.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index f68499d66149ce..98c505c38acac8 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -31,9 +31,10 @@ class DLL_EXPORT CheckInDelegate /** * @brief Callback used to let the application know that a checkin message was received and validated. + * When needRefreshKey is true, it indicates the client to refresh the key and re-register the client with the new key * - * @param[out] clientInfo - ClientInfo object of the peer node - * @param[out] needRefreshKey - Indicates if the application should refresh the exisiting key + * @param[in] clientInfo - ClientInfo object of the peer node + * @param[in] needRefreshKey - Indicates if the application should refresh the exisiting key */ virtual void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) = 0; }; From d6b829bd7fd15135e03efced86353c128feed1bb Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 6 Dec 2023 10:55:21 -0800 Subject: [PATCH 39/93] Addressed review comments --- src/app/icd/client/DefaultCheckInDelegate.cpp | 6 +++++- src/app/icd/client/DefaultICDClientStorage.cpp | 2 +- src/controller/java/BUILD.gn | 1 + src/lib/support/logging/Constants.h | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index fd8ea81301736b..44e95bb636ddfa 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -24,7 +24,11 @@ namespace app { void DefaultCheckInDelegate ::OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) { - ChipLogProgress(ICDClient, "Check In Message preocessing complete"); + ChipLogProgress(ICD, "Check In Message preocessing complete"); + if (needRefreshKey) + { + // TODO : Refresh key and re-register client + } } } // namespace app diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index a8d9e756705de0..2723af8e38e467 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -487,7 +487,7 @@ CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & paylo } } iterator->Release(); - return CHIP_NO_ERROR; + return CHIP_ERROR_NOT_FOUND; } } // namespace app } // namespace chip diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index 588535bd823016..1614649a926b86 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -63,6 +63,7 @@ shared_library("jni") { ] deps = [ + "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/credentials:default_attestation_verifier", "${chip_root}/src/inet", diff --git a/src/lib/support/logging/Constants.h b/src/lib/support/logging/Constants.h index 52e794a6c3ccd1..6a176b02eb1389 100644 --- a/src/lib/support/logging/Constants.h +++ b/src/lib/support/logging/Constants.h @@ -59,7 +59,7 @@ enum LogModule kLogModule_OperationalSessionSetup, kLogModule_Automation, kLogModule_CASESessionManager, - kLogModule_ICDClient, + kLogModule_ICD, kLogModule_Max }; @@ -227,8 +227,8 @@ enum LogModule #define CHIP_CONFIG_LOG_MODULE_CASESessionManager 1 #endif -#ifndef CHIP_CONFIG_LOG_MODULE_ICDClient -#define CHIP_CONFIG_LOG_MODULE_ICDClient 1 +#ifndef CHIP_CONFIG_LOG_MODULE_ICD +#define CHIP_CONFIG_LOG_MODULE_ICD 1 #endif /** From cd9e91ae8377d798e427b42f01b6b84598ad1f8d Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 6 Dec 2023 14:44:55 -0800 Subject: [PATCH 40/93] Addressed review comments --- src/app/icd/client/CheckInHandler.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index cdd5d0282f9630..a28d268b86fd82 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -47,10 +47,7 @@ CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeMana mpExchangeManager = exchangeManager; mpICDClientStorage = clientStorage; mpCheckInDelegate = delegate; - ReturnErrorOnFailure( - mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); - - return CHIP_NO_ERROR; + return mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this); } void CheckInMessageHandler::Shutdown() From 9f19c7264ffdce848fc3001c0fa8d7c42a5274a5 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 6 Dec 2023 15:21:06 -0800 Subject: [PATCH 41/93] Addressed review comments. --- src/app/icd/client/CheckInDelegate.h | 5 +++-- src/app/icd/client/ICDClientStorage.h | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index 98c505c38acac8..4d39d5b52bd289 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -31,10 +31,11 @@ class DLL_EXPORT CheckInDelegate /** * @brief Callback used to let the application know that a checkin message was received and validated. - * When needRefreshKey is true, it indicates the client to refresh the key and re-register the client with the new key + * When needRefreshKey is true, it indicates that the ICD registration needs to be updated with a new key to avoid counter + * roll-over problems. * * @param[in] clientInfo - ClientInfo object of the peer node - * @param[in] needRefreshKey - Indicates if the application should refresh the exisiting key + * @param[in] needRefreshKey - Indicates whether the application should refresh the existing key */ virtual void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) = 0; }; diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index c00a5ea56f5628..4ca5a2ab256e19 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -103,7 +103,8 @@ class ICDClientStorage * and populate the clientInfo with the stored information corresponding to the key. * @param[in] payload received checkIn Message payload * @param[out] clientInfo retrieved matched clientInfo from storage - * @param[out] refreshKey set to true if the counter value reaches 2 ^ 31 indicating the key needs to be refreshed + * @param[out] refreshKey set to true if the offset of the received counter value from the stored initial value reaches 2 ^ 31 + * indicating the key needs to be refreshed to avoid counter roll-over problems. */ virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) = 0; }; From 440c84f5fd72d3b8cbb66eb2afbc849811bb6db1 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 7 Dec 2023 14:19:08 -0800 Subject: [PATCH 42/93] Moved checkin counter validation to CheckInHandler. --- src/app/icd/client/CheckInHandler.cpp | 22 +++++++++++--- .../icd/client/DefaultICDClientStorage.cpp | 23 +++++--------- src/app/icd/client/DefaultICDClientStorage.h | 2 +- src/app/icd/client/ICDClientStorage.h | 5 ++-- src/app/tests/TestDefaultICDClientStorage.cpp | 30 +++++++++++++------ .../secure_channel/CheckinMessage.cpp | 2 +- src/protocols/secure_channel/CheckinMessage.h | 2 +- 7 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index a28d268b86fd82..5362de5a576a1a 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -18,7 +18,7 @@ /** * @file - * This file defines objects for a CHIP ICD handler which handles unsolicited checkin messages. + * This file defines objects for a CHIP ICD handler which handles unsolicited Check-In messages. * */ @@ -37,6 +37,9 @@ namespace chip { namespace app { +inline constexpr uint32_t kCheckInCounterMax = UINT32_MAX; +inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); + CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate) { @@ -44,9 +47,11 @@ CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeMana VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(mpExchangeManager == nullptr, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(mpICDClientStorage == nullptr, CHIP_ERROR_INCORRECT_STATE); + mpExchangeManager = exchangeManager; mpICDClientStorage = clientStorage; mpCheckInDelegate = delegate; + return mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this); } @@ -58,6 +63,7 @@ void CheckInMessageHandler::Shutdown() mpExchangeManager = nullptr; } } + CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) { // Return error for wrong message type @@ -76,10 +82,18 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; - bool needRefreshKey = false; - VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, needRefreshKey), + uint32_t counter = 0; + VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_ERROR_INCORRECT_STATE); - mpCheckInDelegate->OnCheckInComplete(clientInfo, needRefreshKey); + auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_SIGNATURE); + clientInfo.offset = counter - clientInfo.start_icd_counter; + bool refreshKey = false; + if (checkInCounter > kKeyRefreshLimit) + { + refreshKey = true; + } + mpCheckInDelegate->OnCheckInComplete(clientInfo, refreshKey); return CHIP_NO_ERROR; } diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 2723af8e38e467..d87a869582973f 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -41,9 +41,6 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { -inline constexpr uint32_t kCheckInCounterMax = UINT32_MAX; -inline constexpr uint32_t kCheckInRolloverConstant = (1U << 31); - CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { for (auto & fabric_idx : mFabricList) @@ -465,24 +462,18 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName()); } -CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) +CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) { - uint32_t counter; - MutableByteSpan appData; + uint8_t testAppData[32]; + MutableByteSpan appData(testAppData); auto * iterator = IterateICDClientInfo(); while (iterator->Next(clientInfo)) { - if (CHIP_NO_ERROR == - chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, counter, - appData)) + CHIP_ERROR err = chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, + counter, appData); + if (CHIP_NO_ERROR == err) { - auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_SIGNATURE); - clientInfo.offset = counter - clientInfo.start_icd_counter; - if (checkInCounter > kCheckInRolloverConstant) - { - refreshKey = true; - } + iterator->Release(); return CHIP_NO_ERROR; } } diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index b2b5b444f561d6..4501b92b8539fd 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -81,7 +81,7 @@ class DefaultICDClientStorage : public ICDClientStorage CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override; - CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) override; + CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) override; protected: enum class ClientInfoTag : uint8_t diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 4ca5a2ab256e19..e20e12226278a3 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -103,10 +103,9 @@ class ICDClientStorage * and populate the clientInfo with the stored information corresponding to the key. * @param[in] payload received checkIn Message payload * @param[out] clientInfo retrieved matched clientInfo from storage - * @param[out] refreshKey set to true if the offset of the received counter value from the stored initial value reaches 2 ^ 31 - * indicating the key needs to be refreshed to avoid counter roll-over problems. + * @param[out] counter counter value received in the checkIn message */ - virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, bool & refreshKey) = 0; + virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) = 0; }; } // namespace app } // namespace chip diff --git a/src/app/tests/TestDefaultICDClientStorage.cpp b/src/app/tests/TestDefaultICDClientStorage.cpp index 37266b49acf332..6bd9d3c4edd8cb 100644 --- a/src/app/tests/TestDefaultICDClientStorage.cpp +++ b/src/app/tests/TestDefaultICDClientStorage.cpp @@ -19,13 +19,13 @@ #include #include #include -#include #include #include #include #include #include +#include using namespace chip; using namespace app; @@ -218,27 +218,39 @@ void TestProcessCheckInPayload(nlTestSuite * apSuite, void * apContext) err = manager.UpdateFabricList(fabricId); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); // Populate clientInfo - ICDClientInfo clientInfo1; - clientInfo1.peer_node = ScopedNodeId(nodeId, fabricId); + ICDClientInfo clientInfo; + clientInfo.peer_node = ScopedNodeId(nodeId, fabricId); - err = manager.SetKey(clientInfo1, ByteSpan(kKeyBuffer1)); + err = manager.SetKey(clientInfo, ByteSpan(kKeyBuffer1)); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = manager.StoreEntry(clientInfo1); + err = manager.StoreEntry(clientInfo); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t counter = 1; System::PacketBufferHandle buffer = MessagePacketBuffer::New(chip::Protocols::SecureChannel::CheckinMessage::sMinPayloadSize); MutableByteSpan output{ buffer->Start(), buffer->MaxDataLength() }; - err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload(clientInfo1.shared_key, counter, ByteSpan(), + err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload(clientInfo.shared_key, counter, ByteSpan(), output); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); buffer->SetDataLength(static_cast(output.size())); - ICDClientInfo clientInfo; + ICDClientInfo decodeClientInfo; + uint32_t checkInCounter = 0; ByteSpan payload{ buffer->Start(), buffer->DataLength() }; - bool refreshKey; - err = manager.ProcessCheckInPayload(payload, clientInfo, refreshKey); + err = manager.ProcessCheckInPayload(payload, decodeClientInfo, checkInCounter); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + + // 2. Use a key not available in the storage for encoding + err = manager.SetKey(clientInfo, ByteSpan(kKeyBuffer2)); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload(clientInfo.shared_key, counter, ByteSpan(), + output); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + + buffer->SetDataLength(static_cast(output.size())); + ByteSpan payload1{ buffer->Start(), buffer->DataLength() }; + err = manager.ProcessCheckInPayload(payload1, decodeClientInfo, checkInCounter); + NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NOT_FOUND); } /** diff --git a/src/protocols/secure_channel/CheckinMessage.cpp b/src/protocols/secure_channel/CheckinMessage.cpp index 67f5b0563d62fc..10ddd726771d4a 100644 --- a/src/protocols/secure_channel/CheckinMessage.cpp +++ b/src/protocols/secure_channel/CheckinMessage.cpp @@ -62,7 +62,7 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle return err; } -CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128BitsKeyHandle & key, const ByteSpan & payload, +CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData) { VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT); diff --git a/src/protocols/secure_channel/CheckinMessage.h b/src/protocols/secure_channel/CheckinMessage.h index 6c23c67eea550d..502acd87e4c791 100644 --- a/src/protocols/secure_channel/CheckinMessage.h +++ b/src/protocols/secure_channel/CheckinMessage.h @@ -66,7 +66,7 @@ class DLL_EXPORT CheckinMessage * @return CHIP_ERROR */ - static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128BitsKeyHandle & key, const ByteSpan & payload, CounterType & counter, + static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData); static inline size_t GetCheckinPayloadSize(size_t appDataSize) { return appDataSize + sMinPayloadSize; } From 9a860755df8209fbb8b4f68a393f3576dd60fe39 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 7 Dec 2023 18:46:11 -0800 Subject: [PATCH 43/93] Removed TestDefaultICDClientStorage from iotsdk. --- src/app/icd/client/DefaultICDClientStorage.cpp | 10 ++++++++-- src/app/tests/BUILD.gn | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index d87a869582973f..75f4a8cb0dfcde 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -41,6 +41,8 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { +#define MIN_APPDATA_LENGTH 6 + CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { for (auto & fabric_idx : mFabricList) @@ -464,8 +466,12 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) { - uint8_t testAppData[32]; - MutableByteSpan appData(testAppData); + /*appDataBuffer is the working buffer that will be used to retrieve data from the payload. + *counter to retrieve - 4 bytes + *appData to retrieve - 2 bytes(activeModeThreshold) + */ + uint8_t appDataBuffer[MIN_APPDATA_LENGTH]; + MutableByteSpan appData(appDataBuffer); auto * iterator = IterateICDClientInfo(); while (iterator->Next(clientInfo)) { diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index 8bfb3fb6df816b..edc639f48408ed 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -162,7 +162,6 @@ chip_test_suite_using_nltest("tests") { } test_sources += [ "TestAclAttribute.cpp" ] - test_sources += [ "TestDefaultICDClientStorage.cpp" ] # # On NRF platforms, the allocation of a large number of pbufs in this test @@ -180,6 +179,8 @@ chip_test_suite_using_nltest("tests") { if (chip_device_platform != "nrfconnect" && chip_device_platform != "openiotsdk" && chip_device_platform != "fake") { test_sources += [ "TestEventLogging.cpp" ] + #Not sure why TestDefaultICDClientStorage fails on openiotsdk + test_sources += [ "TestDefaultICDClientStorage.cpp" ] } # The platform manager is not properly clearing queues in test teardown, which results in From d09f6016a366c1927d5466d8d7301e430e57f4fc Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 8 Dec 2023 02:46:40 +0000 Subject: [PATCH 44/93] Restyled by gn --- src/app/tests/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index edc639f48408ed..07e0e1f31ae44a 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -179,6 +179,7 @@ chip_test_suite_using_nltest("tests") { if (chip_device_platform != "nrfconnect" && chip_device_platform != "openiotsdk" && chip_device_platform != "fake") { test_sources += [ "TestEventLogging.cpp" ] + #Not sure why TestDefaultICDClientStorage fails on openiotsdk test_sources += [ "TestDefaultICDClientStorage.cpp" ] } From d415eec8bdff0b4aa6de147be90957149e0dc418 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Fri, 8 Dec 2023 08:30:24 -0800 Subject: [PATCH 45/93] add CheckInExchangeDispatch to accept unsecure check-in message --- .../chip-tool/commands/common/CHIPCommand.cpp | 1 + src/app/icd/client/CheckInHandler.h | 21 ++ src/app/icd/client/DefaultCheckInDelegate.cpp | 13 +- src/app/icd/client/DefaultCheckInDelegate.h | 6 +- .../DefaultICDClientInfoPersistentStorage.cpp | 347 ++++++++++++++++++ 5 files changed, 385 insertions(+), 3 deletions(-) create mode 100644 src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index ce54c9f7d38281..ea05dd9f54cedd 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -140,6 +140,7 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnErrorOnFailure(GetAttestationTrustStore(mPaaTrustStorePath.ValueOr(nullptr), &sTrustStore)); + ReturnLogErrorOnFailure(mCheckInDelegate.Init(&mICDClientStorage)); ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(), &mICDClientStorage, &mCheckInDelegate)); diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index a207e278adc631..ac7cdffbcb7895 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -51,7 +51,28 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { + return CheckInExchangeDispatch::Instance(); + } + private: + class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch + { + public: + static ExchangeMessageDispatch & Instance() + { + static CheckInExchangeDispatch instance; + return instance; + } + + CheckInExchangeDispatch() {} + ~CheckInExchangeDispatch() override {} + + protected: + bool MessagePermitted(Protocols::Id, uint8_t type) override { return type == to_underlying(Protocols::SecureChannel::MsgType::ICD_CheckIn); } + bool IsEncryptionRequired() const override { return false; } + }; + Messaging::ExchangeManager * mpExchangeManager = nullptr; CheckInDelegate * mpCheckInDelegate = nullptr; Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 44e95bb636ddfa..dd0ede7eeafb37 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -22,9 +22,18 @@ namespace chip { namespace app { -void DefaultCheckInDelegate ::OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) +CHIP_ERROR DefaultCheckInDelegate::Init(ICDClientStorage * storage) { - ChipLogProgress(ICD, "Check In Message preocessing complete"); + VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(mpStorage == nullptr, CHIP_ERROR_INCORRECT_STATE); + mpStorage = storage; + return CHIP_NO_ERROR; +} + +void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) +{ + mpStorage->StoreEntry(clientInfo); + ChipLogProgress(ICD, "Check In Message processing complete: counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); if (needRefreshKey) { // TODO : Refresh key and re-register client diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index 637af4d1b13364..a77c59211e3632 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -19,7 +19,7 @@ #pragma once #include - +#include namespace chip { namespace app { @@ -28,7 +28,11 @@ class DefaultCheckInDelegate : public CheckInDelegate { public: virtual ~DefaultCheckInDelegate() {} + CHIP_ERROR Init(ICDClientStorage * storage); void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) override; + +private: + ICDClientStorage * mpStorage = nullptr; }; } // namespace app diff --git a/src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp b/src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp new file mode 100644 index 00000000000000..ceb8491de24503 --- /dev/null +++ b/src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp @@ -0,0 +1,347 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DefaultICDClientStorage.h" +#include "DefaultICDStorageKey.h" +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace app { + +Global sDefaultICDClientStorage; + +DefaultICDClientStorage * DefaultICDClientStorage::GetInstance() +{ + return &sDefaultICDClientStorage.get(); +} + +DefaultICDClientStorage::ICDClientInfoIteratorImpl::ICDClientInfoIteratorImpl(DefaultICDClientStorage & aManager) : + mManager(aManager) +{ + mStorageIndex = 0; + mClientInfoIndex = 0; +} + +size_t DefaultICDClientStorage::ICDClientInfoIteratorImpl::Count() +{ + size_t total = 0; + for (auto & storageIterator : mManager.mStorages) + { + if (!storageIterator.IsValid()) + { + continue; + } + for (size_t clientInfoIndex = 0; clientInfoIndex < mManager.mpICDStorageKeyDelegate->MaxKeyCounter(); clientInfoIndex++) + { + if (storageIterator.mpClientInfoStore->SyncDoesKeyExist( + mManager.mpICDStorageKeyDelegate->GetKey(clientInfoIndex).KeyName())) + { + total++; + } + } + } + return total; +} + +bool DefaultICDClientStorage::ICDClientInfoIteratorImpl::Next(ICDClientInfo & aOutput) +{ + for (; mStorageIndex < mManager.Size(); mStorageIndex++) + { + ICDStorage & storage = mManager.mStorages[mStorageIndex]; + if (!storage.IsValid()) + { + continue; + } + for (; mClientInfoIndex < mManager.mpICDStorageKeyDelegate->MaxKeyCounter(); mClientInfoIndex++) + { + CHIP_ERROR err = mManager.Load(storage, mClientInfoIndex, aOutput); + if (err == CHIP_NO_ERROR) + { + mClientInfoIndex++; + return true; + } + + if (err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) + { + ChipLogError(DataManagement, "Failed to load ICDClient Info at index %u fabric %u error %" CHIP_ERROR_FORMAT, + static_cast(mClientInfoIndex), static_cast(storage.mFabricIndex), err.Format()); + break; + } + } + mClientInfoIndex = 0; + } + return false; +} + +void DefaultICDClientStorage::ICDClientInfoIteratorImpl::Release() +{ + mManager.mICDClientInfoIterators.ReleaseObject(this); +} + +CHIP_ERROR DefaultICDClientStorage::Init(ICDStorageKeyDelegate * apICDStorageKeyDelegate) +{ + VerifyOrReturnError(apICDStorageKeyDelegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(mpICDStorageKeyDelegate == nullptr, CHIP_ERROR_INCORRECT_STATE); + mpICDStorageKeyDelegate = apICDStorageKeyDelegate; + return CHIP_NO_ERROR; +} + +ICDStorage * DefaultICDClientStorage::FindStorage(FabricIndex aFabricIndex) +{ + for (auto & storage : mStorages) + { + if (!storage.IsValid()) + { + continue; + } + + if (storage.mFabricIndex == aFabricIndex) + { + return &storage; + } + } + return nullptr; +} + +DefaultICDClientStorage::ICDClientInfoIterator * DefaultICDClientStorage::IterateICDClientInfo() +{ + return mICDClientInfoIterators.CreateObject(*this); +} + +CHIP_ERROR DefaultICDClientStorage::Load(ICDStorage & aStorage, size_t aIndex, ICDClientInfo & aICDClientInfo) +{ + VerifyOrReturnError(aStorage.IsValid(), CHIP_ERROR_INVALID_ARGUMENT); + Platform::ScopedMemoryBuffer backingBuffer; + ReturnErrorCodeIf(!backingBuffer.Calloc(MaxICDClientInfoSize()), CHIP_ERROR_NO_MEMORY); + size_t len = MaxICDClientInfoSize(); + VerifyOrReturnError(CanCastTo(len), CHIP_ERROR_BUFFER_TOO_SMALL); + uint16_t length = static_cast(len); + ReturnErrorOnFailure(aStorage.mpClientInfoStore->SyncGetKeyValue(mpICDStorageKeyDelegate->GetKey(aIndex).KeyName(), + backingBuffer.Get(), length)); + + TLV::ScopedBufferTLVReader reader(std::move(backingBuffer), MaxICDClientInfoSize()); + + ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag())); + + TLV::TLVType ICDClientInfoType; + NodeId nodeId; + FabricIndex fabricIndex; + ReturnErrorOnFailure(reader.EnterContainer(ICDClientInfoType)); + // Peer Node ID + ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kPeerNodeId))); + ReturnErrorOnFailure(reader.Get(nodeId)); + + // Fabric Index + ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kFabricIndex))); + ReturnErrorOnFailure(reader.Get(fabricIndex)); + aICDClientInfo.mPeerNode = ScopedNodeId(nodeId, fabricIndex); + // Start ICD Counter + ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kStartICDCounter))); + ReturnErrorOnFailure(reader.Get(aICDClientInfo.mStartICDCounter)); + + // Offset + ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kOffset))); + ReturnErrorOnFailure(reader.Get(aICDClientInfo.mOffset)); + + // MonitoredSubject + ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kMonitoredSubject))); + ReturnErrorOnFailure(reader.Get(aICDClientInfo.mMonitoredSubject)); + + // Shared key + ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kSharedKey))); + ByteSpan buf(aICDClientInfo.mSharedKey.AsMutable()); + ReturnErrorOnFailure(reader.Get(buf)); + memcpy(aICDClientInfo.mSharedKey.AsMutable(), buf.data(), sizeof(Crypto::Aes128KeyByteArray)); + return reader.ExitContainer(ICDClientInfoType); +} + +CHIP_ERROR DefaultICDClientStorage::SetKey(ICDClientInfo & aClientInfo, const ByteSpan aKeyData) +{ + ICDStorage * storage = FindStorage(aClientInfo.mPeerNode.GetFabricIndex()); + VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); + + VerifyOrReturnError(aKeyData.size() == sizeof(Crypto::Aes128KeyByteArray), CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(storage->mpKeyStore != nullptr, CHIP_ERROR_INTERNAL); + + Crypto::Aes128KeyByteArray keyMaterial; + memcpy(keyMaterial, aKeyData.data(), sizeof(Crypto::Aes128KeyByteArray)); + + return storage->mpKeyStore->CreateKey(keyMaterial, aClientInfo.mSharedKey); +} + +CHIP_ERROR DefaultICDClientStorage::Save(TLV::TLVWriter & aWriter, const ICDClientInfo & aICDClientInfo) +{ + TLV::TLVType ICDClientInfoContainerType; + ReturnErrorOnFailure(aWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, ICDClientInfoContainerType)); + ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kPeerNodeId), aICDClientInfo.mPeerNode.GetNodeId())); + ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kFabricIndex), aICDClientInfo.mPeerNode.GetFabricIndex())); + ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kStartICDCounter), aICDClientInfo.mStartICDCounter)); + ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kOffset), aICDClientInfo.mOffset)); + ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kMonitoredSubject), aICDClientInfo.mMonitoredSubject)); + ByteSpan buf(aICDClientInfo.mSharedKey.As()); + ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kSharedKey), buf)); + ReturnErrorOnFailure(aWriter.EndContainer(ICDClientInfoContainerType)); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DefaultICDClientStorage::StoreEntry(ICDClientInfo & aICDClientInfo) +{ + ICDStorage * storage = FindStorage(aICDClientInfo.mPeerNode.GetFabricIndex()); + VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); + + // Find empty index or duplicate if exists + size_t maxCount = mpICDStorageKeyDelegate->MaxKeyCounter(); + size_t firstEmptyIndex = maxCount; + for (size_t index = 0; index < maxCount; index++) + { + ICDClientInfo currentICDClientInfo; + CHIP_ERROR err = Load(*storage, index, currentICDClientInfo); + + // if empty and firstEmptyIndex isn't set yet, then mark empty spot + if ((firstEmptyIndex == maxCount) && (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)) + { + firstEmptyIndex = index; + } + + // delete duplicate + if (err == CHIP_NO_ERROR) + { + if (aICDClientInfo.mPeerNode.GetNodeId() == currentICDClientInfo.mPeerNode.GetNodeId()) + { + Delete(*storage, index, aICDClientInfo.mSharedKey); + // if duplicate is the first empty spot, then also set it + if (firstEmptyIndex == maxCount) + { + firstEmptyIndex = index; + } + } + } + } + + // Fail if no empty space + if (firstEmptyIndex == maxCount) + { + return CHIP_ERROR_NO_MEMORY; + } + + // Now construct ICD ClientInfo and save + Platform::ScopedMemoryBuffer backingBuffer; + backingBuffer.Calloc(MaxICDClientInfoSize()); + ReturnErrorCodeIf(backingBuffer.Get() == nullptr, CHIP_ERROR_NO_MEMORY); + + TLV::ScopedBufferTLVWriter writer(std::move(backingBuffer), MaxICDClientInfoSize()); + + ReturnErrorOnFailure(Save(writer, aICDClientInfo)); + + const auto len = writer.GetLengthWritten(); + VerifyOrReturnError(CanCastTo(len), CHIP_ERROR_BUFFER_TOO_SMALL); + + writer.Finalize(backingBuffer); + ReturnErrorOnFailure(storage->mpClientInfoStore->SyncSetKeyValue(mpICDStorageKeyDelegate->GetKey(firstEmptyIndex).KeyName(), + backingBuffer.Get(), static_cast(len))); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DefaultICDClientStorage::Delete(ICDStorage & aStorage, size_t aIndex, Crypto::Aes128KeyHandle & aSharedKey) +{ + VerifyOrReturnError(aStorage.mpClientInfoStore != nullptr, CHIP_ERROR_INTERNAL); + ReturnErrorOnFailure(aStorage.mpClientInfoStore->SyncDeleteKeyValue(mpICDStorageKeyDelegate->GetKey(aIndex).KeyName())); + return DeleteKey(aStorage.mpKeyStore, aSharedKey); +} + +CHIP_ERROR DefaultICDClientStorage::DeleteKey(Crypto::SymmetricKeystore * apKeyStore, Crypto::Aes128KeyHandle & aSharedKey) +{ + VerifyOrReturnError(apKeyStore != nullptr, CHIP_ERROR_INTERNAL); + apKeyStore->DestroyKey(aSharedKey); + return CHIP_NO_ERROR; +} + +CHIP_ERROR DefaultICDClientStorage::DeleteEntry(ScopedNodeId aPeerNode) +{ + ICDStorage * storage = FindStorage(aPeerNode.GetFabricIndex()); + VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); + size_t maxCount = mpICDStorageKeyDelegate->MaxKeyCounter(); + for (size_t index = 0; index < maxCount; index++) + { + ICDClientInfo currentICDClientInfo; + CHIP_ERROR err = Load(*storage, index, currentICDClientInfo); + if (err == CHIP_NO_ERROR) + { + if (aPeerNode.GetNodeId() == currentICDClientInfo.mPeerNode.GetNodeId()) + { + Delete(*storage, index, currentICDClientInfo.mSharedKey); + break; + } + } + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex aFabricIndex) +{ + ICDStorage * storage = FindStorage(aFabricIndex); + VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); + size_t maxCount = mpICDStorageKeyDelegate->MaxKeyCounter(); + for (size_t index = 0; index < maxCount; index++) + { + ICDClientInfo currentICDClientInfo; + CHIP_ERROR err = Load(*storage, index, currentICDClientInfo); + if (err == CHIP_NO_ERROR) + { + Delete(*storage, index, currentICDClientInfo.mSharedKey); + } + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR DefaultICDClientStorage::AddStorage(ICDStorage && aStorage) +{ + VerifyOrReturnError(aStorage.IsValid(), CHIP_ERROR_INVALID_ARGUMENT); + mStorages.push_back(std::move(aStorage)); + return CHIP_NO_ERROR; +} + +void DefaultICDClientStorage::RemoveStorage(FabricIndex aFabricIndex) +{ + DeleteAllEntries(aFabricIndex); + for (auto storageIterator = mStorages.begin(); storageIterator != mStorages.end(); storageIterator++) + { + if (storageIterator->mFabricIndex == aFabricIndex) + { + mStorages.erase(storageIterator); + break; + } + } +} + +bool DefaultICDClientStorage::ValidateCheckInPayload(const ByteSpan & aPayload, ICDClientInfo & aClientInfo) +{ + // TODO: Need to implement default decription code using CheckinMessage::ParseCheckinMessagePayload + return false; +} + +size_t DefaultICDClientStorage::Size() +{ + return mStorages.size(); +} +} // namespace app +} // namespace chip From 77f4ec4384b42b4afbb5eeeb9aa94785363badc1 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 8 Dec 2023 17:35:38 +0000 Subject: [PATCH 46/93] Restyled by clang-format --- src/app/icd/client/CheckInHandler.h | 9 +++++---- src/app/icd/client/DefaultCheckInDelegate.cpp | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index ac7cdffbcb7895..d5ee5f2544d65c 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -51,9 +51,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate void OnResponseTimeout(Messaging::ExchangeContext * ec) override; - Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { - return CheckInExchangeDispatch::Instance(); - } + Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return CheckInExchangeDispatch::Instance(); } private: class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch @@ -69,7 +67,10 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi ~CheckInExchangeDispatch() override {} protected: - bool MessagePermitted(Protocols::Id, uint8_t type) override { return type == to_underlying(Protocols::SecureChannel::MsgType::ICD_CheckIn); } + bool MessagePermitted(Protocols::Id, uint8_t type) override + { + return type == to_underlying(Protocols::SecureChannel::MsgType::ICD_CheckIn); + } bool IsEncryptionRequired() const override { return false; } }; diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index dd0ede7eeafb37..1c98aabe06a7fd 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -33,7 +33,9 @@ CHIP_ERROR DefaultCheckInDelegate::Init(ICDClientStorage * storage) void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) { mpStorage->StoreEntry(clientInfo); - ChipLogProgress(ICD, "Check In Message processing complete: counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); + ChipLogProgress( + ICD, "Check In Message processing complete: counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, + clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); if (needRefreshKey) { // TODO : Refresh key and re-register client From a1f77d5312debfe8b0a6ba338d0f96510a8b067f Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 8 Dec 2023 11:49:37 -0800 Subject: [PATCH 47/93] Removed DefaultICDClientInfoPersistentStorage.cpp. --- .../DefaultICDClientInfoPersistentStorage.cpp | 347 ------------------ 1 file changed, 347 deletions(-) delete mode 100644 src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp diff --git a/src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp b/src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp deleted file mode 100644 index ceb8491de24503..00000000000000 --- a/src/app/icd/client/DefaultICDClientInfoPersistentStorage.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright (c) 2023 Project CHIP Authors - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "DefaultICDClientStorage.h" -#include "DefaultICDStorageKey.h" -#include -#include -#include -#include -#include -#include - -namespace chip { -namespace app { - -Global sDefaultICDClientStorage; - -DefaultICDClientStorage * DefaultICDClientStorage::GetInstance() -{ - return &sDefaultICDClientStorage.get(); -} - -DefaultICDClientStorage::ICDClientInfoIteratorImpl::ICDClientInfoIteratorImpl(DefaultICDClientStorage & aManager) : - mManager(aManager) -{ - mStorageIndex = 0; - mClientInfoIndex = 0; -} - -size_t DefaultICDClientStorage::ICDClientInfoIteratorImpl::Count() -{ - size_t total = 0; - for (auto & storageIterator : mManager.mStorages) - { - if (!storageIterator.IsValid()) - { - continue; - } - for (size_t clientInfoIndex = 0; clientInfoIndex < mManager.mpICDStorageKeyDelegate->MaxKeyCounter(); clientInfoIndex++) - { - if (storageIterator.mpClientInfoStore->SyncDoesKeyExist( - mManager.mpICDStorageKeyDelegate->GetKey(clientInfoIndex).KeyName())) - { - total++; - } - } - } - return total; -} - -bool DefaultICDClientStorage::ICDClientInfoIteratorImpl::Next(ICDClientInfo & aOutput) -{ - for (; mStorageIndex < mManager.Size(); mStorageIndex++) - { - ICDStorage & storage = mManager.mStorages[mStorageIndex]; - if (!storage.IsValid()) - { - continue; - } - for (; mClientInfoIndex < mManager.mpICDStorageKeyDelegate->MaxKeyCounter(); mClientInfoIndex++) - { - CHIP_ERROR err = mManager.Load(storage, mClientInfoIndex, aOutput); - if (err == CHIP_NO_ERROR) - { - mClientInfoIndex++; - return true; - } - - if (err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) - { - ChipLogError(DataManagement, "Failed to load ICDClient Info at index %u fabric %u error %" CHIP_ERROR_FORMAT, - static_cast(mClientInfoIndex), static_cast(storage.mFabricIndex), err.Format()); - break; - } - } - mClientInfoIndex = 0; - } - return false; -} - -void DefaultICDClientStorage::ICDClientInfoIteratorImpl::Release() -{ - mManager.mICDClientInfoIterators.ReleaseObject(this); -} - -CHIP_ERROR DefaultICDClientStorage::Init(ICDStorageKeyDelegate * apICDStorageKeyDelegate) -{ - VerifyOrReturnError(apICDStorageKeyDelegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(mpICDStorageKeyDelegate == nullptr, CHIP_ERROR_INCORRECT_STATE); - mpICDStorageKeyDelegate = apICDStorageKeyDelegate; - return CHIP_NO_ERROR; -} - -ICDStorage * DefaultICDClientStorage::FindStorage(FabricIndex aFabricIndex) -{ - for (auto & storage : mStorages) - { - if (!storage.IsValid()) - { - continue; - } - - if (storage.mFabricIndex == aFabricIndex) - { - return &storage; - } - } - return nullptr; -} - -DefaultICDClientStorage::ICDClientInfoIterator * DefaultICDClientStorage::IterateICDClientInfo() -{ - return mICDClientInfoIterators.CreateObject(*this); -} - -CHIP_ERROR DefaultICDClientStorage::Load(ICDStorage & aStorage, size_t aIndex, ICDClientInfo & aICDClientInfo) -{ - VerifyOrReturnError(aStorage.IsValid(), CHIP_ERROR_INVALID_ARGUMENT); - Platform::ScopedMemoryBuffer backingBuffer; - ReturnErrorCodeIf(!backingBuffer.Calloc(MaxICDClientInfoSize()), CHIP_ERROR_NO_MEMORY); - size_t len = MaxICDClientInfoSize(); - VerifyOrReturnError(CanCastTo(len), CHIP_ERROR_BUFFER_TOO_SMALL); - uint16_t length = static_cast(len); - ReturnErrorOnFailure(aStorage.mpClientInfoStore->SyncGetKeyValue(mpICDStorageKeyDelegate->GetKey(aIndex).KeyName(), - backingBuffer.Get(), length)); - - TLV::ScopedBufferTLVReader reader(std::move(backingBuffer), MaxICDClientInfoSize()); - - ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag())); - - TLV::TLVType ICDClientInfoType; - NodeId nodeId; - FabricIndex fabricIndex; - ReturnErrorOnFailure(reader.EnterContainer(ICDClientInfoType)); - // Peer Node ID - ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kPeerNodeId))); - ReturnErrorOnFailure(reader.Get(nodeId)); - - // Fabric Index - ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kFabricIndex))); - ReturnErrorOnFailure(reader.Get(fabricIndex)); - aICDClientInfo.mPeerNode = ScopedNodeId(nodeId, fabricIndex); - // Start ICD Counter - ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kStartICDCounter))); - ReturnErrorOnFailure(reader.Get(aICDClientInfo.mStartICDCounter)); - - // Offset - ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kOffset))); - ReturnErrorOnFailure(reader.Get(aICDClientInfo.mOffset)); - - // MonitoredSubject - ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kMonitoredSubject))); - ReturnErrorOnFailure(reader.Get(aICDClientInfo.mMonitoredSubject)); - - // Shared key - ReturnErrorOnFailure(reader.Next(TLV::ContextTag(Tag::kSharedKey))); - ByteSpan buf(aICDClientInfo.mSharedKey.AsMutable()); - ReturnErrorOnFailure(reader.Get(buf)); - memcpy(aICDClientInfo.mSharedKey.AsMutable(), buf.data(), sizeof(Crypto::Aes128KeyByteArray)); - return reader.ExitContainer(ICDClientInfoType); -} - -CHIP_ERROR DefaultICDClientStorage::SetKey(ICDClientInfo & aClientInfo, const ByteSpan aKeyData) -{ - ICDStorage * storage = FindStorage(aClientInfo.mPeerNode.GetFabricIndex()); - VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); - - VerifyOrReturnError(aKeyData.size() == sizeof(Crypto::Aes128KeyByteArray), CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(storage->mpKeyStore != nullptr, CHIP_ERROR_INTERNAL); - - Crypto::Aes128KeyByteArray keyMaterial; - memcpy(keyMaterial, aKeyData.data(), sizeof(Crypto::Aes128KeyByteArray)); - - return storage->mpKeyStore->CreateKey(keyMaterial, aClientInfo.mSharedKey); -} - -CHIP_ERROR DefaultICDClientStorage::Save(TLV::TLVWriter & aWriter, const ICDClientInfo & aICDClientInfo) -{ - TLV::TLVType ICDClientInfoContainerType; - ReturnErrorOnFailure(aWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, ICDClientInfoContainerType)); - ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kPeerNodeId), aICDClientInfo.mPeerNode.GetNodeId())); - ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kFabricIndex), aICDClientInfo.mPeerNode.GetFabricIndex())); - ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kStartICDCounter), aICDClientInfo.mStartICDCounter)); - ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kOffset), aICDClientInfo.mOffset)); - ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kMonitoredSubject), aICDClientInfo.mMonitoredSubject)); - ByteSpan buf(aICDClientInfo.mSharedKey.As()); - ReturnErrorOnFailure(aWriter.Put(TLV::ContextTag(Tag::kSharedKey), buf)); - ReturnErrorOnFailure(aWriter.EndContainer(ICDClientInfoContainerType)); - return CHIP_NO_ERROR; -} - -CHIP_ERROR DefaultICDClientStorage::StoreEntry(ICDClientInfo & aICDClientInfo) -{ - ICDStorage * storage = FindStorage(aICDClientInfo.mPeerNode.GetFabricIndex()); - VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); - - // Find empty index or duplicate if exists - size_t maxCount = mpICDStorageKeyDelegate->MaxKeyCounter(); - size_t firstEmptyIndex = maxCount; - for (size_t index = 0; index < maxCount; index++) - { - ICDClientInfo currentICDClientInfo; - CHIP_ERROR err = Load(*storage, index, currentICDClientInfo); - - // if empty and firstEmptyIndex isn't set yet, then mark empty spot - if ((firstEmptyIndex == maxCount) && (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)) - { - firstEmptyIndex = index; - } - - // delete duplicate - if (err == CHIP_NO_ERROR) - { - if (aICDClientInfo.mPeerNode.GetNodeId() == currentICDClientInfo.mPeerNode.GetNodeId()) - { - Delete(*storage, index, aICDClientInfo.mSharedKey); - // if duplicate is the first empty spot, then also set it - if (firstEmptyIndex == maxCount) - { - firstEmptyIndex = index; - } - } - } - } - - // Fail if no empty space - if (firstEmptyIndex == maxCount) - { - return CHIP_ERROR_NO_MEMORY; - } - - // Now construct ICD ClientInfo and save - Platform::ScopedMemoryBuffer backingBuffer; - backingBuffer.Calloc(MaxICDClientInfoSize()); - ReturnErrorCodeIf(backingBuffer.Get() == nullptr, CHIP_ERROR_NO_MEMORY); - - TLV::ScopedBufferTLVWriter writer(std::move(backingBuffer), MaxICDClientInfoSize()); - - ReturnErrorOnFailure(Save(writer, aICDClientInfo)); - - const auto len = writer.GetLengthWritten(); - VerifyOrReturnError(CanCastTo(len), CHIP_ERROR_BUFFER_TOO_SMALL); - - writer.Finalize(backingBuffer); - ReturnErrorOnFailure(storage->mpClientInfoStore->SyncSetKeyValue(mpICDStorageKeyDelegate->GetKey(firstEmptyIndex).KeyName(), - backingBuffer.Get(), static_cast(len))); - return CHIP_NO_ERROR; -} - -CHIP_ERROR DefaultICDClientStorage::Delete(ICDStorage & aStorage, size_t aIndex, Crypto::Aes128KeyHandle & aSharedKey) -{ - VerifyOrReturnError(aStorage.mpClientInfoStore != nullptr, CHIP_ERROR_INTERNAL); - ReturnErrorOnFailure(aStorage.mpClientInfoStore->SyncDeleteKeyValue(mpICDStorageKeyDelegate->GetKey(aIndex).KeyName())); - return DeleteKey(aStorage.mpKeyStore, aSharedKey); -} - -CHIP_ERROR DefaultICDClientStorage::DeleteKey(Crypto::SymmetricKeystore * apKeyStore, Crypto::Aes128KeyHandle & aSharedKey) -{ - VerifyOrReturnError(apKeyStore != nullptr, CHIP_ERROR_INTERNAL); - apKeyStore->DestroyKey(aSharedKey); - return CHIP_NO_ERROR; -} - -CHIP_ERROR DefaultICDClientStorage::DeleteEntry(ScopedNodeId aPeerNode) -{ - ICDStorage * storage = FindStorage(aPeerNode.GetFabricIndex()); - VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); - size_t maxCount = mpICDStorageKeyDelegate->MaxKeyCounter(); - for (size_t index = 0; index < maxCount; index++) - { - ICDClientInfo currentICDClientInfo; - CHIP_ERROR err = Load(*storage, index, currentICDClientInfo); - if (err == CHIP_NO_ERROR) - { - if (aPeerNode.GetNodeId() == currentICDClientInfo.mPeerNode.GetNodeId()) - { - Delete(*storage, index, currentICDClientInfo.mSharedKey); - break; - } - } - } - return CHIP_NO_ERROR; -} - -CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex aFabricIndex) -{ - ICDStorage * storage = FindStorage(aFabricIndex); - VerifyOrReturnError(storage != nullptr, CHIP_ERROR_NOT_FOUND); - size_t maxCount = mpICDStorageKeyDelegate->MaxKeyCounter(); - for (size_t index = 0; index < maxCount; index++) - { - ICDClientInfo currentICDClientInfo; - CHIP_ERROR err = Load(*storage, index, currentICDClientInfo); - if (err == CHIP_NO_ERROR) - { - Delete(*storage, index, currentICDClientInfo.mSharedKey); - } - } - return CHIP_NO_ERROR; -} - -CHIP_ERROR DefaultICDClientStorage::AddStorage(ICDStorage && aStorage) -{ - VerifyOrReturnError(aStorage.IsValid(), CHIP_ERROR_INVALID_ARGUMENT); - mStorages.push_back(std::move(aStorage)); - return CHIP_NO_ERROR; -} - -void DefaultICDClientStorage::RemoveStorage(FabricIndex aFabricIndex) -{ - DeleteAllEntries(aFabricIndex); - for (auto storageIterator = mStorages.begin(); storageIterator != mStorages.end(); storageIterator++) - { - if (storageIterator->mFabricIndex == aFabricIndex) - { - mStorages.erase(storageIterator); - break; - } - } -} - -bool DefaultICDClientStorage::ValidateCheckInPayload(const ByteSpan & aPayload, ICDClientInfo & aClientInfo) -{ - // TODO: Need to implement default decription code using CheckinMessage::ParseCheckinMessagePayload - return false; -} - -size_t DefaultICDClientStorage::Size() -{ - return mStorages.size(); -} -} // namespace app -} // namespace chip From 02a8c5c294f5ee7dc60a0bd2779e693b02682eea Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 8 Dec 2023 13:32:58 -0800 Subject: [PATCH 48/93] Addressed a few review comments. --- src/app/icd/client/CheckInHandler.cpp | 16 ++-- src/app/icd/client/DefaultCheckInDelegate.cpp | 2 +- .../icd/client/DefaultICDClientStorage.cpp | 8 +- src/app/icd/client/DefaultICDClientStorage.h | 2 +- src/app/icd/client/ICDClientStorage.h | 6 +- src/lib/core/CHIPError.h | 84 +++++++++++++++---- 6 files changed, 84 insertions(+), 34 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 5362de5a576a1a..407a71c1054be7 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020-2023 Project CHIP Authors + * Copyright (c) 2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -37,7 +37,7 @@ namespace chip { namespace app { -inline constexpr uint32_t kCheckInCounterMax = UINT32_MAX; +inline constexpr uint64_t kCheckInCounterMax = (1UL << 32); inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, @@ -82,17 +82,13 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; - uint32_t counter = 0; + CounterType counter = 0; VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_ERROR_INCORRECT_STATE); auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_INVALID_SIGNATURE); - clientInfo.offset = counter - clientInfo.start_icd_counter; - bool refreshKey = false; - if (checkInCounter > kKeyRefreshLimit) - { - refreshKey = true; - } + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE); + clientInfo.offset = checkInCounter; + bool refreshKey = (checkInCounter > kKeyRefreshLimit); mpCheckInDelegate->OnCheckInComplete(clientInfo, refreshKey); return CHIP_NO_ERROR; } diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 1c98aabe06a7fd..2e96efe62c373b 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -34,7 +34,7 @@ void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo, { mpStorage->StoreEntry(clientInfo); ChipLogProgress( - ICD, "Check In Message processing complete: counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, + ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); if (needRefreshKey) { diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 75f4a8cb0dfcde..9de8f5c9367c85 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -24,7 +24,6 @@ #include #include #include -#include namespace { // FabricIndex is uint8_t, the tlv size with anonymous tag is 1(control bytes) + 1(value) = 2 @@ -41,7 +40,7 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { -#define MIN_APPDATA_LENGTH 6 +#define APPDATA_LENGTH 6 CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { @@ -464,13 +463,14 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName()); } -CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) +CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, + CounterType & counter) { /*appDataBuffer is the working buffer that will be used to retrieve data from the payload. *counter to retrieve - 4 bytes *appData to retrieve - 2 bytes(activeModeThreshold) */ - uint8_t appDataBuffer[MIN_APPDATA_LENGTH]; + uint8_t appDataBuffer[APPDATA_LENGTH]; MutableByteSpan appData(appDataBuffer); auto * iterator = IterateICDClientInfo(); while (iterator->Next(clientInfo)) diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index 4501b92b8539fd..0546903ed453a6 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -81,7 +81,7 @@ class DefaultICDClientStorage : public ICDClientStorage CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override; - CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) override; + CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, CounterType & counter) override; protected: enum class ClientInfoTag : uint8_t diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index e20e12226278a3..9b0bb069bef490 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -25,11 +25,13 @@ #include #include #include +#include #include namespace chip { namespace app { +using namespace Protocols::SecureChannel; /** * The ICDClientStorage class is an abstract interface that defines the operations * for storing, retrieving and deleting ICD client information in persistent storage. @@ -103,9 +105,9 @@ class ICDClientStorage * and populate the clientInfo with the stored information corresponding to the key. * @param[in] payload received checkIn Message payload * @param[out] clientInfo retrieved matched clientInfo from storage - * @param[out] counter counter value received in the checkIn message + * @param[out] counter counter value received in the CheckIn message */ - virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t & counter) = 0; + virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, CounterType & counter) = 0; }; } // namespace app } // namespace chip diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 7edc73224aad6e..8f8f2fa64eb12e 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -174,13 +174,22 @@ class ChipError * This only compares the error code. Under the CHIP_CONFIG_ERROR_SOURCE configuration, errors compare equal * if they have the same error code, even if they have different source locations. */ - bool operator==(const ChipError & other) const { return mError == other.mError; } - bool operator!=(const ChipError & other) const { return mError != other.mError; } + bool operator==(const ChipError & other) const + { + return mError == other.mError; + } + bool operator!=(const ChipError & other) const + { + return mError != other.mError; + } /** * Return an integer code for the error. */ - constexpr StorageType AsInteger() const { return mError; } + constexpr StorageType AsInteger() const + { + return mError; + } /* * IsSuccess() is intended to support macros that can take either a ChipError or an integer error code. @@ -189,8 +198,14 @@ class ChipError * @note * Normal code should use `status == CHIP_NO_ERROR` rather than `IsSuccess(status)`. */ - static constexpr bool IsSuccess(ChipError error) { return error.mError == 0; } - static constexpr bool IsSuccess(StorageType error) { return error == 0; } + static constexpr bool IsSuccess(ChipError error) + { + return error.mError == 0; + } + static constexpr bool IsSuccess(StorageType error) + { + return error == 0; + } /** * Format an @a error for printing. @@ -202,9 +217,15 @@ class ChipError * @endcode */ #if CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const { return AsString(); } + FormatType Format() const + { + return AsString(); + } #else // CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const { return mError; } + FormatType Format() const + { + return mError; + } #endif // CHIP_CONFIG_ERROR_FORMAT_AS_STRING /** @@ -230,12 +251,18 @@ class ChipError /** * Get the Range to which the @a error belongs. */ - constexpr Range GetRange() const { return static_cast(GetField(kRangeStart, kRangeLength, mError)); } + constexpr Range GetRange() const + { + return static_cast(GetField(kRangeStart, kRangeLength, mError)); + } /** * Get the encapsulated value of an @a error. */ - constexpr ValueType GetValue() const { return GetField(kValueStart, kValueLength, mError); } + constexpr ValueType GetValue() const + { + return GetField(kValueStart, kValueLength, mError); + } /** * Test whether type @a T can always be losslessly encapsulated in a CHIP_ERROR. @@ -268,7 +295,10 @@ class ChipError /** * Get the SDK code for an SDK error. */ - constexpr uint8_t GetSdkCode() const { return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); } + constexpr uint8_t GetSdkCode() const + { + return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); + } /** * Test whether @a error is an SDK error representing an Interaction Model @@ -290,7 +320,10 @@ class ChipError * @note * This will be `nullptr` if the error was not created with a file name. */ - const char * GetFile() const { return mFile; } + const char * GetFile() const + { + return mFile; + } /** * Get the source line number of the point where the error occurred. @@ -298,7 +331,10 @@ class ChipError * @note * This will be 0 if the error was not created with a file name. */ - unsigned int GetLine() const { return mLine; } + unsigned int GetLine() const + { + return mLine; + } #endif // CHIP_CONFIG_ERROR_SOURCE @@ -328,9 +364,18 @@ class ChipError { return (value >> start) & ((1u << length) - 1); } - static constexpr StorageType MakeMask(unsigned int start, unsigned int length) { return ((1u << length) - 1) << start; } - static constexpr StorageType MakeField(unsigned int start, StorageType value) { return value << start; } - static constexpr bool FitsInField(unsigned int length, StorageType value) { return value < (1u << length); } + static constexpr StorageType MakeMask(unsigned int start, unsigned int length) + { + return ((1u << length) - 1) << start; + } + static constexpr StorageType MakeField(unsigned int start, StorageType value) + { + return value << start; + } + static constexpr bool FitsInField(unsigned int length, StorageType value) + { + return value < (1u << length); + } static constexpr StorageType MakeInteger(Range range, StorageType value) { @@ -618,7 +663,14 @@ using CHIP_ERROR = ::chip::ChipError; */ #define CHIP_ERROR_INVALID_TLV_CHAR_STRING CHIP_CORE_ERROR(0x15) -// AVAILABLE: 0x16 +/** + * @def CHIP_ERROR_DUPLICATE_MESSAGE + * + * @brief + * Duplicate message + * + */ +#define CHIP_ERROR_DUPLICATE_MESSAGE CHIP_CORE_ERROR(0x16) /** * @def CHIP_ERROR_UNSUPPORTED_SIGNATURE_TYPE From 4b94518af4c7aa884d05e0dcaa2befbfd71346d1 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 8 Dec 2023 13:51:27 -0800 Subject: [PATCH 49/93] Modified a macro to make sure it is taken as a 64-bit interger. --- src/app/icd/client/CheckInHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 407a71c1054be7..b6a09d05a03ea9 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -37,7 +37,7 @@ namespace chip { namespace app { -inline constexpr uint64_t kCheckInCounterMax = (1UL << 32); +inline constexpr uint64_t kCheckInCounterMax = (1ULL << 32); inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, From 2e6d6492c7bb348172865f156ba01b83e38b5189 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 8 Dec 2023 21:51:49 +0000 Subject: [PATCH 50/93] Restyled by clang-format --- src/lib/core/CHIPError.h | 75 ++++++++-------------------------------- 1 file changed, 15 insertions(+), 60 deletions(-) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 8f8f2fa64eb12e..5b272ee550251e 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -174,22 +174,13 @@ class ChipError * This only compares the error code. Under the CHIP_CONFIG_ERROR_SOURCE configuration, errors compare equal * if they have the same error code, even if they have different source locations. */ - bool operator==(const ChipError & other) const - { - return mError == other.mError; - } - bool operator!=(const ChipError & other) const - { - return mError != other.mError; - } + bool operator==(const ChipError & other) const { return mError == other.mError; } + bool operator!=(const ChipError & other) const { return mError != other.mError; } /** * Return an integer code for the error. */ - constexpr StorageType AsInteger() const - { - return mError; - } + constexpr StorageType AsInteger() const { return mError; } /* * IsSuccess() is intended to support macros that can take either a ChipError or an integer error code. @@ -198,14 +189,8 @@ class ChipError * @note * Normal code should use `status == CHIP_NO_ERROR` rather than `IsSuccess(status)`. */ - static constexpr bool IsSuccess(ChipError error) - { - return error.mError == 0; - } - static constexpr bool IsSuccess(StorageType error) - { - return error == 0; - } + static constexpr bool IsSuccess(ChipError error) { return error.mError == 0; } + static constexpr bool IsSuccess(StorageType error) { return error == 0; } /** * Format an @a error for printing. @@ -217,15 +202,9 @@ class ChipError * @endcode */ #if CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const - { - return AsString(); - } + FormatType Format() const { return AsString(); } #else // CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const - { - return mError; - } + FormatType Format() const { return mError; } #endif // CHIP_CONFIG_ERROR_FORMAT_AS_STRING /** @@ -251,18 +230,12 @@ class ChipError /** * Get the Range to which the @a error belongs. */ - constexpr Range GetRange() const - { - return static_cast(GetField(kRangeStart, kRangeLength, mError)); - } + constexpr Range GetRange() const { return static_cast(GetField(kRangeStart, kRangeLength, mError)); } /** * Get the encapsulated value of an @a error. */ - constexpr ValueType GetValue() const - { - return GetField(kValueStart, kValueLength, mError); - } + constexpr ValueType GetValue() const { return GetField(kValueStart, kValueLength, mError); } /** * Test whether type @a T can always be losslessly encapsulated in a CHIP_ERROR. @@ -295,10 +268,7 @@ class ChipError /** * Get the SDK code for an SDK error. */ - constexpr uint8_t GetSdkCode() const - { - return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); - } + constexpr uint8_t GetSdkCode() const { return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); } /** * Test whether @a error is an SDK error representing an Interaction Model @@ -320,10 +290,7 @@ class ChipError * @note * This will be `nullptr` if the error was not created with a file name. */ - const char * GetFile() const - { - return mFile; - } + const char * GetFile() const { return mFile; } /** * Get the source line number of the point where the error occurred. @@ -331,10 +298,7 @@ class ChipError * @note * This will be 0 if the error was not created with a file name. */ - unsigned int GetLine() const - { - return mLine; - } + unsigned int GetLine() const { return mLine; } #endif // CHIP_CONFIG_ERROR_SOURCE @@ -364,18 +328,9 @@ class ChipError { return (value >> start) & ((1u << length) - 1); } - static constexpr StorageType MakeMask(unsigned int start, unsigned int length) - { - return ((1u << length) - 1) << start; - } - static constexpr StorageType MakeField(unsigned int start, StorageType value) - { - return value << start; - } - static constexpr bool FitsInField(unsigned int length, StorageType value) - { - return value < (1u << length); - } + static constexpr StorageType MakeMask(unsigned int start, unsigned int length) { return ((1u << length) - 1) << start; } + static constexpr StorageType MakeField(unsigned int start, StorageType value) { return value << start; } + static constexpr bool FitsInField(unsigned int length, StorageType value) { return value < (1u << length); } static constexpr StorageType MakeInteger(Range range, StorageType value) { From 957c71603d2842fe5db3446f9e76ae1ec33ad5d5 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 8 Dec 2023 15:21:32 -0800 Subject: [PATCH 51/93] Removed redundant error code CHIP_ERROR_DUPLICATE_MESSAGE --- src/app/icd/client/CheckInHandler.cpp | 2 +- src/lib/core/CHIPError.h | 85 +++++++++++++++++++-------- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index b6a09d05a03ea9..0a68c87ec88810 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -86,7 +86,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_ERROR_INCORRECT_STATE); auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE); + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); clientInfo.offset = checkInCounter; bool refreshKey = (checkInCounter > kKeyRefreshLimit); mpCheckInDelegate->OnCheckInComplete(clientInfo, refreshKey); diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 5b272ee550251e..6b0d0e9d4c27ae 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -174,13 +174,22 @@ class ChipError * This only compares the error code. Under the CHIP_CONFIG_ERROR_SOURCE configuration, errors compare equal * if they have the same error code, even if they have different source locations. */ - bool operator==(const ChipError & other) const { return mError == other.mError; } - bool operator!=(const ChipError & other) const { return mError != other.mError; } + bool operator==(const ChipError & other) const + { + return mError == other.mError; + } + bool operator!=(const ChipError & other) const + { + return mError != other.mError; + } /** * Return an integer code for the error. */ - constexpr StorageType AsInteger() const { return mError; } + constexpr StorageType AsInteger() const + { + return mError; + } /* * IsSuccess() is intended to support macros that can take either a ChipError or an integer error code. @@ -189,8 +198,14 @@ class ChipError * @note * Normal code should use `status == CHIP_NO_ERROR` rather than `IsSuccess(status)`. */ - static constexpr bool IsSuccess(ChipError error) { return error.mError == 0; } - static constexpr bool IsSuccess(StorageType error) { return error == 0; } + static constexpr bool IsSuccess(ChipError error) + { + return error.mError == 0; + } + static constexpr bool IsSuccess(StorageType error) + { + return error == 0; + } /** * Format an @a error for printing. @@ -202,9 +217,15 @@ class ChipError * @endcode */ #if CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const { return AsString(); } + FormatType Format() const + { + return AsString(); + } #else // CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const { return mError; } + FormatType Format() const + { + return mError; + } #endif // CHIP_CONFIG_ERROR_FORMAT_AS_STRING /** @@ -230,12 +251,18 @@ class ChipError /** * Get the Range to which the @a error belongs. */ - constexpr Range GetRange() const { return static_cast(GetField(kRangeStart, kRangeLength, mError)); } + constexpr Range GetRange() const + { + return static_cast(GetField(kRangeStart, kRangeLength, mError)); + } /** * Get the encapsulated value of an @a error. */ - constexpr ValueType GetValue() const { return GetField(kValueStart, kValueLength, mError); } + constexpr ValueType GetValue() const + { + return GetField(kValueStart, kValueLength, mError); + } /** * Test whether type @a T can always be losslessly encapsulated in a CHIP_ERROR. @@ -268,7 +295,10 @@ class ChipError /** * Get the SDK code for an SDK error. */ - constexpr uint8_t GetSdkCode() const { return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); } + constexpr uint8_t GetSdkCode() const + { + return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); + } /** * Test whether @a error is an SDK error representing an Interaction Model @@ -290,7 +320,10 @@ class ChipError * @note * This will be `nullptr` if the error was not created with a file name. */ - const char * GetFile() const { return mFile; } + const char * GetFile() const + { + return mFile; + } /** * Get the source line number of the point where the error occurred. @@ -298,7 +331,10 @@ class ChipError * @note * This will be 0 if the error was not created with a file name. */ - unsigned int GetLine() const { return mLine; } + unsigned int GetLine() const + { + return mLine; + } #endif // CHIP_CONFIG_ERROR_SOURCE @@ -328,9 +364,18 @@ class ChipError { return (value >> start) & ((1u << length) - 1); } - static constexpr StorageType MakeMask(unsigned int start, unsigned int length) { return ((1u << length) - 1) << start; } - static constexpr StorageType MakeField(unsigned int start, StorageType value) { return value << start; } - static constexpr bool FitsInField(unsigned int length, StorageType value) { return value < (1u << length); } + static constexpr StorageType MakeMask(unsigned int start, unsigned int length) + { + return ((1u << length) - 1) << start; + } + static constexpr StorageType MakeField(unsigned int start, StorageType value) + { + return value << start; + } + static constexpr bool FitsInField(unsigned int length, StorageType value) + { + return value < (1u << length); + } static constexpr StorageType MakeInteger(Range range, StorageType value) { @@ -618,14 +663,8 @@ using CHIP_ERROR = ::chip::ChipError; */ #define CHIP_ERROR_INVALID_TLV_CHAR_STRING CHIP_CORE_ERROR(0x15) -/** - * @def CHIP_ERROR_DUPLICATE_MESSAGE - * - * @brief - * Duplicate message - * - */ -#define CHIP_ERROR_DUPLICATE_MESSAGE CHIP_CORE_ERROR(0x16) + +// AVAILABLE: 0x16 /** * @def CHIP_ERROR_UNSUPPORTED_SIGNATURE_TYPE From c2f2c533a9ecc0a467d3f4ccdbed13d0854f8896 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 8 Dec 2023 23:25:32 +0000 Subject: [PATCH 52/93] Restyled by clang-format --- src/lib/core/CHIPError.h | 75 ++++++++-------------------------------- 1 file changed, 15 insertions(+), 60 deletions(-) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 6b0d0e9d4c27ae..6ae347b698d9fc 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -174,22 +174,13 @@ class ChipError * This only compares the error code. Under the CHIP_CONFIG_ERROR_SOURCE configuration, errors compare equal * if they have the same error code, even if they have different source locations. */ - bool operator==(const ChipError & other) const - { - return mError == other.mError; - } - bool operator!=(const ChipError & other) const - { - return mError != other.mError; - } + bool operator==(const ChipError & other) const { return mError == other.mError; } + bool operator!=(const ChipError & other) const { return mError != other.mError; } /** * Return an integer code for the error. */ - constexpr StorageType AsInteger() const - { - return mError; - } + constexpr StorageType AsInteger() const { return mError; } /* * IsSuccess() is intended to support macros that can take either a ChipError or an integer error code. @@ -198,14 +189,8 @@ class ChipError * @note * Normal code should use `status == CHIP_NO_ERROR` rather than `IsSuccess(status)`. */ - static constexpr bool IsSuccess(ChipError error) - { - return error.mError == 0; - } - static constexpr bool IsSuccess(StorageType error) - { - return error == 0; - } + static constexpr bool IsSuccess(ChipError error) { return error.mError == 0; } + static constexpr bool IsSuccess(StorageType error) { return error == 0; } /** * Format an @a error for printing. @@ -217,15 +202,9 @@ class ChipError * @endcode */ #if CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const - { - return AsString(); - } + FormatType Format() const { return AsString(); } #else // CHIP_CONFIG_ERROR_FORMAT_AS_STRING - FormatType Format() const - { - return mError; - } + FormatType Format() const { return mError; } #endif // CHIP_CONFIG_ERROR_FORMAT_AS_STRING /** @@ -251,18 +230,12 @@ class ChipError /** * Get the Range to which the @a error belongs. */ - constexpr Range GetRange() const - { - return static_cast(GetField(kRangeStart, kRangeLength, mError)); - } + constexpr Range GetRange() const { return static_cast(GetField(kRangeStart, kRangeLength, mError)); } /** * Get the encapsulated value of an @a error. */ - constexpr ValueType GetValue() const - { - return GetField(kValueStart, kValueLength, mError); - } + constexpr ValueType GetValue() const { return GetField(kValueStart, kValueLength, mError); } /** * Test whether type @a T can always be losslessly encapsulated in a CHIP_ERROR. @@ -295,10 +268,7 @@ class ChipError /** * Get the SDK code for an SDK error. */ - constexpr uint8_t GetSdkCode() const - { - return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); - } + constexpr uint8_t GetSdkCode() const { return static_cast(GetField(kSdkCodeStart, kSdkCodeLength, mError)); } /** * Test whether @a error is an SDK error representing an Interaction Model @@ -320,10 +290,7 @@ class ChipError * @note * This will be `nullptr` if the error was not created with a file name. */ - const char * GetFile() const - { - return mFile; - } + const char * GetFile() const { return mFile; } /** * Get the source line number of the point where the error occurred. @@ -331,10 +298,7 @@ class ChipError * @note * This will be 0 if the error was not created with a file name. */ - unsigned int GetLine() const - { - return mLine; - } + unsigned int GetLine() const { return mLine; } #endif // CHIP_CONFIG_ERROR_SOURCE @@ -364,18 +328,9 @@ class ChipError { return (value >> start) & ((1u << length) - 1); } - static constexpr StorageType MakeMask(unsigned int start, unsigned int length) - { - return ((1u << length) - 1) << start; - } - static constexpr StorageType MakeField(unsigned int start, StorageType value) - { - return value << start; - } - static constexpr bool FitsInField(unsigned int length, StorageType value) - { - return value < (1u << length); - } + static constexpr StorageType MakeMask(unsigned int start, unsigned int length) { return ((1u << length) - 1) << start; } + static constexpr StorageType MakeField(unsigned int start, StorageType value) { return value << start; } + static constexpr bool FitsInField(unsigned int length, StorageType value) { return value < (1u << length); } static constexpr StorageType MakeInteger(Range range, StorageType value) { From 7975ee2d54ce39d5b75cec9bb64dc797b7e75774 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 8 Dec 2023 16:07:37 -0800 Subject: [PATCH 53/93] Modified the datatype for checkInCounter to CounterType from auto. --- src/app/icd/client/CheckInHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 0a68c87ec88810..3346a2d97d49a1 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -85,7 +85,7 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * CounterType counter = 0; VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_ERROR_INCORRECT_STATE); - auto checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + CounterType checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); clientInfo.offset = checkInCounter; bool refreshKey = (checkInCounter > kKeyRefreshLimit); From 7e0d59a7fce9c10d54699d54ed14dbee4e347a6c Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Mon, 11 Dec 2023 16:18:56 -0800 Subject: [PATCH 54/93] Added code to refresh key. --- src/app/icd/client/CheckInDelegate.h | 5 ++++- src/app/icd/client/CheckInHandler.cpp | 5 +++++ src/app/icd/client/CheckInHandler.h | 8 ++++++++ src/app/icd/client/DefaultCheckInDelegate.cpp | 11 ++++++++--- src/app/icd/client/DefaultCheckInDelegate.h | 2 +- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index 4d39d5b52bd289..c4c12545b97725 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -34,10 +34,13 @@ class DLL_EXPORT CheckInDelegate * When needRefreshKey is true, it indicates that the ICD registration needs to be updated with a new key to avoid counter * roll-over problems. * + * The implementor of this function should generate a new key and send it to CheckInHandler using CHIP_ERROR + * CheckInMessageHandler::SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) + * * @param[in] clientInfo - ClientInfo object of the peer node * @param[in] needRefreshKey - Indicates whether the application should refresh the existing key */ - virtual void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) = 0; + virtual void OnCheckInComplete(ICDClientInfo & clientInfo, bool needRefreshKey) = 0; }; } // namespace app diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 3346a2d97d49a1..4221214f9fa774 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -93,6 +93,11 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * return CHIP_NO_ERROR; } +CHIP_ERROR CheckInMessageHandler::SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) +{ + // TODO - Register the client. On successful registration, update the clientInfo with the new key and store the clientInfo + return CHIP_NO_ERROR; +} void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} } // namespace app diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index d5ee5f2544d65c..d636d40e47fcb6 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -40,6 +40,14 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); void Shutdown(); + /** + * @brief Used by the application to set a new key to avoid counter rollover problems. + * + * @param[in] clientInfo clientInfo object + * @param[in] keyData New key data to use to re-register the client with the server + */ + CHIP_ERROR SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); + protected: // ExchangeDelegate CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 2e96efe62c373b..428aa967546608 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -15,7 +15,9 @@ * limitations under the License. */ +#include "CheckInHandler.h" #include +#include #include #include @@ -30,15 +32,18 @@ CHIP_ERROR DefaultCheckInDelegate::Init(ICDClientStorage * storage) return CHIP_NO_ERROR; } -void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) +void DefaultCheckInDelegate::OnCheckInComplete(ICDClientInfo & clientInfo, bool needRefreshKey) { - mpStorage->StoreEntry(clientInfo); ChipLogProgress( ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); if (needRefreshKey) { - // TODO : Refresh key and re-register client + CheckInMessageHandler handler; + uint8_t randomGeneratedSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length]; + chip::Crypto::DRBG_get_bytes(randomGeneratedSymmetricKey, sizeof(randomGeneratedSymmetricKey)); + chip::ByteSpan mNewSymmetricKey(randomGeneratedSymmetricKey); + handler.SetNewKey(clientInfo, mNewSymmetricKey); } } diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index a77c59211e3632..c55afa6e8d74f0 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -29,7 +29,7 @@ class DefaultCheckInDelegate : public CheckInDelegate public: virtual ~DefaultCheckInDelegate() {} CHIP_ERROR Init(ICDClientStorage * storage); - void OnCheckInComplete(const ICDClientInfo & clientInfo, bool needRefreshKey) override; + void OnCheckInComplete(ICDClientInfo & clientInfo, bool needRefreshKey) override; private: ICDClientStorage * mpStorage = nullptr; From 0404f35ddc8c964302268f9e1553bbd8885a7192 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 29 Nov 2023 15:38:49 -0800 Subject: [PATCH 55/93] ICDHandler initialization --- src/app/icd/BUILD.gn | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index fcb396b3bcd41b..d2a4806fd78cc7 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -114,3 +114,18 @@ source_set("configuration-data") { "${chip_root}/src/lib/core", ] } + +# ICD Handler source-set is broken out of the main source-set to enable unit tests +# All sources and configurations used by the ICDHandler need to go in this source-set +source_set("handler") { + sources = [ + "ICDHandler.cpp", + "ICDHandler.h", + ] + + public_deps = [ + "${chip_root}/src/lib/core", + "${chip_root}/src/messaging", + "${chip_root}/src/protocols", + ] +} \ No newline at end of file From 559fb3ab21d4839ed25c91f8d6dba2d40defebbe Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 21:51:28 -0800 Subject: [PATCH 56/93] ICDHandler initialization --- src/app/icd/BUILD.gn | 15 ---- src/app/icd/client/CheckInHandler.h | 4 ++ src/app/icd/client/ICDHandler.cpp | 104 ++++++++++++++++++++++++++++ src/app/icd/client/ICDHandler.h | 96 +++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 src/app/icd/client/ICDHandler.cpp create mode 100644 src/app/icd/client/ICDHandler.h diff --git a/src/app/icd/BUILD.gn b/src/app/icd/BUILD.gn index d2a4806fd78cc7..fcb396b3bcd41b 100644 --- a/src/app/icd/BUILD.gn +++ b/src/app/icd/BUILD.gn @@ -114,18 +114,3 @@ source_set("configuration-data") { "${chip_root}/src/lib/core", ] } - -# ICD Handler source-set is broken out of the main source-set to enable unit tests -# All sources and configurations used by the ICDHandler need to go in this source-set -source_set("handler") { - sources = [ - "ICDHandler.cpp", - "ICDHandler.h", - ] - - public_deps = [ - "${chip_root}/src/lib/core", - "${chip_root}/src/messaging", - "${chip_root}/src/protocols", - ] -} \ No newline at end of file diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index d636d40e47fcb6..eae9baa58b7122 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -25,8 +25,12 @@ #pragma once +<<<<<<<< HEAD:src/app/icd/client/CheckInHandler.h #include #include +======== +#include +>>>>>>>> 4201f08818 (ICDHandler initialization):src/app/icd/client/ICDHandler.h #include #include #include diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp new file mode 100644 index 00000000000000..4221214f9fa774 --- /dev/null +++ b/src/app/icd/client/ICDHandler.cpp @@ -0,0 +1,104 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file defines objects for a CHIP ICD handler which handles unsolicited Check-In messages. + * + */ + +#include +#include + +#include + +#include +#include +#include +#include + +#include + +namespace chip { +namespace app { + +inline constexpr uint64_t kCheckInCounterMax = (1ULL << 32); +inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); + +CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, + CheckInDelegate * delegate) +{ + VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(mpExchangeManager == nullptr, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mpICDClientStorage == nullptr, CHIP_ERROR_INCORRECT_STATE); + + mpExchangeManager = exchangeManager; + mpICDClientStorage = clientStorage; + mpCheckInDelegate = delegate; + + return mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this); +} + +void CheckInMessageHandler::Shutdown() +{ + if (mpExchangeManager) + { + mpExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); + mpExchangeManager = nullptr; + } +} + +CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) +{ + // Return error for wrong message type + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), + CHIP_ERROR_INVALID_MESSAGE_TYPE); + + newDelegate = this; + return CHIP_NO_ERROR; +} + +CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) +{ + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), + CHIP_ERROR_INVALID_MESSAGE_TYPE); + + ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; + ICDClientInfo clientInfo; + CounterType counter = 0; + VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), + CHIP_ERROR_INCORRECT_STATE); + CounterType checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); + clientInfo.offset = checkInCounter; + bool refreshKey = (checkInCounter > kKeyRefreshLimit); + mpCheckInDelegate->OnCheckInComplete(clientInfo, refreshKey); + return CHIP_NO_ERROR; +} + +CHIP_ERROR CheckInMessageHandler::SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) +{ + // TODO - Register the client. On successful registration, update the clientInfo with the new key and store the clientInfo + return CHIP_NO_ERROR; +} +void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} + +} // namespace app +} // namespace chip diff --git a/src/app/icd/client/ICDHandler.h b/src/app/icd/client/ICDHandler.h new file mode 100644 index 00000000000000..eae9baa58b7122 --- /dev/null +++ b/src/app/icd/client/ICDHandler.h @@ -0,0 +1,96 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file defines objects for a CHIP CheckInMessage unsolicited + * handler + * + */ + +#pragma once + +<<<<<<<< HEAD:src/app/icd/client/CheckInHandler.h +#include +#include +======== +#include +>>>>>>>> 4201f08818 (ICDHandler initialization):src/app/icd/client/ICDHandler.h +#include +#include +#include + +namespace chip { +namespace app { +class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler +{ + +public: + CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); + void Shutdown(); + + /** + * @brief Used by the application to set a new key to avoid counter rollover problems. + * + * @param[in] clientInfo clientInfo object + * @param[in] keyData New key data to use to re-register the client with the server + */ + CHIP_ERROR SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); + +protected: + // ExchangeDelegate + CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) override; + + // UnsolicitedMessageHandler + CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; + + // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate + void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + + Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return CheckInExchangeDispatch::Instance(); } + +private: + class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch + { + public: + static ExchangeMessageDispatch & Instance() + { + static CheckInExchangeDispatch instance; + return instance; + } + + CheckInExchangeDispatch() {} + ~CheckInExchangeDispatch() override {} + + protected: + bool MessagePermitted(Protocols::Id, uint8_t type) override + { + return type == to_underlying(Protocols::SecureChannel::MsgType::ICD_CheckIn); + } + bool IsEncryptionRequired() const override { return false; } + }; + + Messaging::ExchangeManager * mpExchangeManager = nullptr; + CheckInDelegate * mpCheckInDelegate = nullptr; + Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } + ICDClientStorage * mpICDClientStorage = nullptr; +}; + +} // namespace app +} // namespace chip From 0eec8be7cb3e740c5989fbc77ed275f0fd989cb6 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 17:00:01 -0800 Subject: [PATCH 57/93] Resolve conflicts with master branch --- src/protocols/secure_channel/CheckinMessage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocols/secure_channel/CheckinMessage.cpp b/src/protocols/secure_channel/CheckinMessage.cpp index 10ddd726771d4a..67f5b0563d62fc 100644 --- a/src/protocols/secure_channel/CheckinMessage.cpp +++ b/src/protocols/secure_channel/CheckinMessage.cpp @@ -62,7 +62,7 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle return err; } -CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, +CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128BitsKeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData) { VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT); From 89f714509e3c6aae098c6381ffec835ad60673d5 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 21:19:58 -0800 Subject: [PATCH 58/93] Added CheckInHandler init in chiptool --- examples/chip-tool/BUILD.gn | 1 + examples/chip-tool/commands/common/CHIPCommand.cpp | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 53568316114c9a..7b7b43828626d4 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -104,6 +104,7 @@ static_library("chip-tool-utils") { "${chip_root}/examples/common/tracing:commandline", "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/icd/client:manager", + "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/server", "${chip_root}/src/app/tests/suites/commands/interaction_model", "${chip_root}/src/controller/data_model", diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index ea05dd9f54cedd..52b3ec698f73df 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -140,8 +140,7 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnErrorOnFailure(GetAttestationTrustStore(mPaaTrustStorePath.ValueOr(nullptr), &sTrustStore)); - ReturnLogErrorOnFailure(mCheckInDelegate.Init(&mICDClientStorage)); - ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(), + ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState().ExchangeMgr(), &mICDClientStorage, &mCheckInDelegate)); CommissionerIdentity nullIdentity{ kIdentityNull, chip::kUndefinedNodeId }; From fb81b59144e146d45b5f0a5b967bb922859773c4 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 7 Dec 2023 14:19:08 -0800 Subject: [PATCH 59/93] Moved checkin counter validation to CheckInHandler. --- src/app/icd/client/DefaultICDClientStorage.cpp | 3 ++- src/protocols/secure_channel/CheckinMessage.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 9de8f5c9367c85..1325bb5250c570 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -42,7 +42,8 @@ namespace app { #define APPDATA_LENGTH 6 -CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) +CHIP_ERROR +DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { for (auto & fabric_idx : mFabricList) { diff --git a/src/protocols/secure_channel/CheckinMessage.cpp b/src/protocols/secure_channel/CheckinMessage.cpp index 67f5b0563d62fc..10ddd726771d4a 100644 --- a/src/protocols/secure_channel/CheckinMessage.cpp +++ b/src/protocols/secure_channel/CheckinMessage.cpp @@ -62,7 +62,7 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle return err; } -CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128BitsKeyHandle & key, const ByteSpan & payload, +CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData) { VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT); From 7a84cdc3fdd81aa5e7b0d058698783a197389015 Mon Sep 17 00:00:00 2001 From: Song GUO Date: Tue, 12 Dec 2023 11:04:57 +0800 Subject: [PATCH 60/93] [icd] integrate ICD management command into CHIP tool (#30863) * [icd] integrate ICD management command into CHIP tool * Delete entry on failure * Fix build --- examples/chip-tool/commands/common/CHIPCommand.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 52b3ec698f73df..06dcd16d02a450 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -110,6 +110,12 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() // TODO: Implement persistent ICD storage for the chip-tool. ReturnLogErrorOnFailure(sICDClientStorage.Init(&mDefaultStorage, &sSessionKeystore)); + // chip-tool uses a non-persistent keystore. + // ICD storage lifetime is currently tied to the chip-tool's lifetime. Since chip-tool interactive mode is currently used for + // ICD commissioning and check-in validation, this temporary storage meets the test requirements. + // TODO: Implement persistent ICD storage for the chip-tool. + ReturnLogErrorOnFailure(sICDClientStorage.Init(&mDefaultStorage, &sSessionKeystore)); + chip::Controller::FactoryInitParams factoryInitParams; factoryInitParams.fabricIndependentStorage = &mDefaultStorage; From 8cf0630d66d48ba08b79557c857af413d71701fc Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Sat, 2 Dec 2023 21:51:28 -0800 Subject: [PATCH 61/93] ICDHandler initialization --- src/app/icd/client/ICDHandler.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/app/icd/client/ICDHandler.h b/src/app/icd/client/ICDHandler.h index eae9baa58b7122..8b5e36b4de932f 100644 --- a/src/app/icd/client/ICDHandler.h +++ b/src/app/icd/client/ICDHandler.h @@ -25,12 +25,9 @@ #pragma once -<<<<<<<< HEAD:src/app/icd/client/CheckInHandler.h #include +#include #include -======== -#include ->>>>>>>> 4201f08818 (ICDHandler initialization):src/app/icd/client/ICDHandler.h #include #include #include From 3a85145d976a80b4c3a75b91abf0e0ebc64a7bec Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 5 Dec 2023 21:19:58 -0800 Subject: [PATCH 62/93] Added CheckInHandler init in chiptool --- examples/chip-tool/commands/common/CHIPCommand.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 06dcd16d02a450..52b3ec698f73df 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -110,12 +110,6 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() // TODO: Implement persistent ICD storage for the chip-tool. ReturnLogErrorOnFailure(sICDClientStorage.Init(&mDefaultStorage, &sSessionKeystore)); - // chip-tool uses a non-persistent keystore. - // ICD storage lifetime is currently tied to the chip-tool's lifetime. Since chip-tool interactive mode is currently used for - // ICD commissioning and check-in validation, this temporary storage meets the test requirements. - // TODO: Implement persistent ICD storage for the chip-tool. - ReturnLogErrorOnFailure(sICDClientStorage.Init(&mDefaultStorage, &sSessionKeystore)); - chip::Controller::FactoryInitParams factoryInitParams; factoryInitParams.fabricIndependentStorage = &mDefaultStorage; From 4952042667bb91875b34641d08ca70c2c9e1507f Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 12 Dec 2023 14:07:35 -0800 Subject: [PATCH 63/93] Fixing merge conflicts --- src/app/icd/client/CheckInHandler.h | 4 -- src/app/icd/client/ICDHandler.cpp | 104 ---------------------------- src/app/icd/client/ICDHandler.h | 93 ------------------------- 3 files changed, 201 deletions(-) delete mode 100644 src/app/icd/client/ICDHandler.cpp delete mode 100644 src/app/icd/client/ICDHandler.h diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index eae9baa58b7122..d636d40e47fcb6 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -25,12 +25,8 @@ #pragma once -<<<<<<<< HEAD:src/app/icd/client/CheckInHandler.h #include #include -======== -#include ->>>>>>>> 4201f08818 (ICDHandler initialization):src/app/icd/client/ICDHandler.h #include #include #include diff --git a/src/app/icd/client/ICDHandler.cpp b/src/app/icd/client/ICDHandler.cpp deleted file mode 100644 index 4221214f9fa774..00000000000000 --- a/src/app/icd/client/ICDHandler.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * - * Copyright (c) 2023 Project CHIP Authors - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * This file defines objects for a CHIP ICD handler which handles unsolicited Check-In messages. - * - */ - -#include -#include - -#include - -#include -#include -#include -#include - -#include - -namespace chip { -namespace app { - -inline constexpr uint64_t kCheckInCounterMax = (1ULL << 32); -inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); - -CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, - CheckInDelegate * delegate) -{ - VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(mpExchangeManager == nullptr, CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(mpICDClientStorage == nullptr, CHIP_ERROR_INCORRECT_STATE); - - mpExchangeManager = exchangeManager; - mpICDClientStorage = clientStorage; - mpCheckInDelegate = delegate; - - return mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this); -} - -void CheckInMessageHandler::Shutdown() -{ - if (mpExchangeManager) - { - mpExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); - mpExchangeManager = nullptr; - } -} - -CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) -{ - // Return error for wrong message type - VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), - CHIP_ERROR_INVALID_MESSAGE_TYPE); - - newDelegate = this; - return CHIP_NO_ERROR; -} - -CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, - System::PacketBufferHandle && payload) -{ - VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), - CHIP_ERROR_INVALID_MESSAGE_TYPE); - - ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; - ICDClientInfo clientInfo; - CounterType counter = 0; - VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), - CHIP_ERROR_INCORRECT_STATE); - CounterType checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); - clientInfo.offset = checkInCounter; - bool refreshKey = (checkInCounter > kKeyRefreshLimit); - mpCheckInDelegate->OnCheckInComplete(clientInfo, refreshKey); - return CHIP_NO_ERROR; -} - -CHIP_ERROR CheckInMessageHandler::SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) -{ - // TODO - Register the client. On successful registration, update the clientInfo with the new key and store the clientInfo - return CHIP_NO_ERROR; -} -void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} - -} // namespace app -} // namespace chip diff --git a/src/app/icd/client/ICDHandler.h b/src/app/icd/client/ICDHandler.h deleted file mode 100644 index 8b5e36b4de932f..00000000000000 --- a/src/app/icd/client/ICDHandler.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * - * Copyright (c) 2023 Project CHIP Authors - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * This file defines objects for a CHIP CheckInMessage unsolicited - * handler - * - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace chip { -namespace app { -class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler -{ - -public: - CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); - void Shutdown(); - - /** - * @brief Used by the application to set a new key to avoid counter rollover problems. - * - * @param[in] clientInfo clientInfo object - * @param[in] keyData New key data to use to re-register the client with the server - */ - CHIP_ERROR SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); - -protected: - // ExchangeDelegate - CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, - System::PacketBufferHandle && payload) override; - - // UnsolicitedMessageHandler - CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; - - // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate - void OnResponseTimeout(Messaging::ExchangeContext * ec) override; - - Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return CheckInExchangeDispatch::Instance(); } - -private: - class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch - { - public: - static ExchangeMessageDispatch & Instance() - { - static CheckInExchangeDispatch instance; - return instance; - } - - CheckInExchangeDispatch() {} - ~CheckInExchangeDispatch() override {} - - protected: - bool MessagePermitted(Protocols::Id, uint8_t type) override - { - return type == to_underlying(Protocols::SecureChannel::MsgType::ICD_CheckIn); - } - bool IsEncryptionRequired() const override { return false; } - }; - - Messaging::ExchangeManager * mpExchangeManager = nullptr; - CheckInDelegate * mpCheckInDelegate = nullptr; - Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } - ICDClientStorage * mpICDClientStorage = nullptr; -}; - -} // namespace app -} // namespace chip From ec3afd8414774c7a326a2bbc87f3b0ef3f5aa6af Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 12 Dec 2023 15:01:01 -0800 Subject: [PATCH 64/93] Added OnRefreshKey callback --- .../chip-tool/commands/common/CHIPCommand.cpp | 14 +++++--------- .../chip-tool/commands/common/CHIPCommand.h | 2 ++ src/app/icd/client/CheckInDelegate.h | 18 +++++++++++------- src/app/icd/client/CheckInHandler.cpp | 10 ++++++++-- src/app/icd/client/CheckInHandler.h | 16 +++++++++------- src/app/icd/client/DefaultCheckInDelegate.cpp | 17 ++++++++--------- src/app/icd/client/DefaultCheckInDelegate.h | 3 ++- 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 52b3ec698f73df..41e82240319e3e 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -50,6 +50,8 @@ chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGr // All fabrics share the same ICD client storage. chip::app::DefaultICDClientStorage CHIPCommand::sICDClientStorage; chip::Crypto::RawKeySessionKeystore CHIPCommand::sSessionKeystore; +// chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate; +// chip::app::CheckInMessageHandler CHIPCommand::sCheckInHandler; namespace { @@ -102,13 +104,6 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnLogErrorOnFailure(mDefaultStorage.Init(nullptr, GetStorageDirectory().ValueOr(nullptr))); ReturnLogErrorOnFailure(mOperationalKeystore.Init(&mDefaultStorage)); ReturnLogErrorOnFailure(mOpCertStore.Init(&mDefaultStorage)); - ReturnLogErrorOnFailure(mICDClientStorage.Init(&mDefaultStorage, &mSessionKeystore)); - - // chip-tool uses a non-persistent keystore. - // ICD storage lifetime is currently tied to the chip-tool's lifetime. Since chip-tool interactive mode is currently used for - // ICD commissioning and check-in validation, this temporary storage meets the test requirements. - // TODO: Implement persistent ICD storage for the chip-tool. - ReturnLogErrorOnFailure(sICDClientStorage.Init(&mDefaultStorage, &sSessionKeystore)); chip::Controller::FactoryInitParams factoryInitParams; @@ -140,8 +135,9 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnErrorOnFailure(GetAttestationTrustStore(mPaaTrustStorePath.ValueOr(nullptr), &sTrustStore)); - ReturnLogErrorOnFailure(mCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState().ExchangeMgr(), - &mICDClientStorage, &mCheckInDelegate)); + // ReturnLogErrorOnFailure(sCheckInDelegate.Init(&sICDClientStorage)); + // ReturnLogErrorOnFailure(sCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(), + // &sICDClientStorage, &sCheckInDelegate)); CommissionerIdentity nullIdentity{ kIdentityNull, chip::kUndefinedNodeId }; ReturnLogErrorOnFailure(InitializeCommissioner(nullIdentity, kIdentityNullFabricId)); diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 61638ad8c61eaf..979d6ebc2ee065 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -162,6 +162,8 @@ class CHIPCommand : public Command static chip::Credentials::GroupDataProviderImpl sGroupDataProvider; static chip::app::DefaultICDClientStorage sICDClientStorage; + static chip::app::DefaultCheckInDelegate sCheckInDelegate; + static chip::app::CheckInMessageHandler sCheckInHandler; CredentialIssuerCommands * mCredIssuerCmds; std::string GetIdentity(); diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index c4c12545b97725..8d6da512e9dfa8 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -31,16 +31,20 @@ class DLL_EXPORT CheckInDelegate /** * @brief Callback used to let the application know that a checkin message was received and validated. - * When needRefreshKey is true, it indicates that the ICD registration needs to be updated with a new key to avoid counter - * roll-over problems. - * - * The implementor of this function should generate a new key and send it to CheckInHandler using CHIP_ERROR - * CheckInMessageHandler::SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) * * @param[in] clientInfo - ClientInfo object of the peer node - * @param[in] needRefreshKey - Indicates whether the application should refresh the existing key */ - virtual void OnCheckInComplete(ICDClientInfo & clientInfo, bool needRefreshKey) = 0; + virtual void OnCheckInComplete(ICDClientInfo & clientInfo) = 0; + + /** + * @brief Callback used to let the application know that a checkin message was received and validated and a key refresh is + * needed to avoid counter roolover problems. + * + * The implementor of this function should generate a new key + * + * @param[out] keyData - new key generated + */ + virtual void OnRefreshKey(ByteSpan & keyData) = 0; }; } // namespace app diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 4221214f9fa774..224d865453fb35 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -89,11 +89,17 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); clientInfo.offset = checkInCounter; bool refreshKey = (checkInCounter > kKeyRefreshLimit); - mpCheckInDelegate->OnCheckInComplete(clientInfo, refreshKey); + ByteSpan newKeyData; + if (refreshKey) + { + mpCheckInDelegate->OnRefreshKey(newKeyData); + RegisterClientWithNewKey(clientInfo, newKeyData); + } + mpCheckInDelegate->OnCheckInComplete(clientInfo); return CHIP_NO_ERROR; } -CHIP_ERROR CheckInMessageHandler::SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) +CHIP_ERROR CheckInMessageHandler::RegisterClientWithNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) { // TODO - Register the client. On successful registration, update the clientInfo with the new key and store the clientInfo return CHIP_NO_ERROR; diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index d636d40e47fcb6..3dff83248fa1e1 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -40,13 +40,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); void Shutdown(); - /** - * @brief Used by the application to set a new key to avoid counter rollover problems. - * - * @param[in] clientInfo clientInfo object - * @param[in] keyData New key data to use to re-register the client with the server - */ - CHIP_ERROR SetNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); + virtual ~CheckInMessageHandler() = default; protected: // ExchangeDelegate @@ -82,6 +76,14 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi bool IsEncryptionRequired() const override { return false; } }; + /** + * @brief Used by the application to set a new key to avoid counter rollover problems. + * + * @param[in] clientInfo clientInfo object + * @param[in] keyData New key data to use to re-register the client with the server + */ + CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); + Messaging::ExchangeManager * mpExchangeManager = nullptr; CheckInDelegate * mpCheckInDelegate = nullptr; Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 428aa967546608..4c059cf3a961f6 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -32,19 +32,18 @@ CHIP_ERROR DefaultCheckInDelegate::Init(ICDClientStorage * storage) return CHIP_NO_ERROR; } -void DefaultCheckInDelegate::OnCheckInComplete(ICDClientInfo & clientInfo, bool needRefreshKey) +void DefaultCheckInDelegate::OnCheckInComplete(ICDClientInfo & clientInfo) { ChipLogProgress( ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); - if (needRefreshKey) - { - CheckInMessageHandler handler; - uint8_t randomGeneratedSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length]; - chip::Crypto::DRBG_get_bytes(randomGeneratedSymmetricKey, sizeof(randomGeneratedSymmetricKey)); - chip::ByteSpan mNewSymmetricKey(randomGeneratedSymmetricKey); - handler.SetNewKey(clientInfo, mNewSymmetricKey); - } +} + +void DefaultCheckInDelegate::OnRefreshKey(ByteSpan & keyData) +{ + uint8_t randomGeneratedSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length]; + chip::Crypto::DRBG_get_bytes(randomGeneratedSymmetricKey, sizeof(randomGeneratedSymmetricKey)); + keyData = ByteSpan(randomGeneratedSymmetricKey); } } // namespace app diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index c55afa6e8d74f0..0c775141953a25 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -29,7 +29,8 @@ class DefaultCheckInDelegate : public CheckInDelegate public: virtual ~DefaultCheckInDelegate() {} CHIP_ERROR Init(ICDClientStorage * storage); - void OnCheckInComplete(ICDClientInfo & clientInfo, bool needRefreshKey) override; + void OnCheckInComplete(ICDClientInfo & clientInfo) override; + void OnRefreshKey(ByteSpan & keyData) override; private: ICDClientStorage * mpStorage = nullptr; From ef1541acd5b1bcd5417135e886f3edd88c47a11e Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 12 Dec 2023 23:30:42 +0000 Subject: [PATCH 65/93] Restyled by gn --- examples/chip-tool/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 7b7b43828626d4..a0e9d5d8ec0576 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -103,8 +103,8 @@ static_library("chip-tool-utils") { public_deps = [ "${chip_root}/examples/common/tracing:commandline", "${chip_root}/src/app/icd/client:handler", - "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/app/icd/client:handler", + "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/app/server", "${chip_root}/src/app/tests/suites/commands/interaction_model", "${chip_root}/src/controller/data_model", From 168556cc06401b13d51bd9771c3625e3a6a95607 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 12 Dec 2023 15:46:19 -0800 Subject: [PATCH 66/93] Modified APPDATA_LENGTh macro to inline consexpr variable. --- src/app/icd/client/DefaultICDClientStorage.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 1325bb5250c570..c3710e831932fe 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -40,7 +40,7 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { -#define APPDATA_LENGTH 6 +inline constexpr uint8_t kAppDataLength = 6; CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) @@ -471,7 +471,7 @@ CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & paylo *counter to retrieve - 4 bytes *appData to retrieve - 2 bytes(activeModeThreshold) */ - uint8_t appDataBuffer[APPDATA_LENGTH]; + uint8_t appDataBuffer[kAppDataLength]; MutableByteSpan appData(appDataBuffer); auto * iterator = IterateICDClientInfo(); while (iterator->Next(clientInfo)) From 524e984426be09ff6637a74a9934fd3841c02373 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 12 Dec 2023 16:10:35 -0800 Subject: [PATCH 67/93] Modified CheckInDelegate and CheckInHandler to static members in chiptool. --- examples/chip-tool/commands/common/CHIPCommand.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 41e82240319e3e..95be561d38d595 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -50,8 +50,8 @@ chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGr // All fabrics share the same ICD client storage. chip::app::DefaultICDClientStorage CHIPCommand::sICDClientStorage; chip::Crypto::RawKeySessionKeystore CHIPCommand::sSessionKeystore; -// chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate; -// chip::app::CheckInMessageHandler CHIPCommand::sCheckInHandler; +chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate; +chip::app::CheckInMessageHandler CHIPCommand::sCheckInHandler; namespace { @@ -135,9 +135,9 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnErrorOnFailure(GetAttestationTrustStore(mPaaTrustStorePath.ValueOr(nullptr), &sTrustStore)); - // ReturnLogErrorOnFailure(sCheckInDelegate.Init(&sICDClientStorage)); - // ReturnLogErrorOnFailure(sCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(), - // &sICDClientStorage, &sCheckInDelegate)); + ReturnLogErrorOnFailure(sCheckInDelegate.Init(&sICDClientStorage)); + ReturnLogErrorOnFailure(sCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(), + &sICDClientStorage, &sCheckInDelegate)); CommissionerIdentity nullIdentity{ kIdentityNull, chip::kUndefinedNodeId }; ReturnLogErrorOnFailure(InitializeCommissioner(nullIdentity, kIdentityNullFabricId)); From 6ae6657bb411c3951feee7cfa6604710b5335be0 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 12 Dec 2023 22:40:01 -0800 Subject: [PATCH 68/93] Return CHIP_NO_ERROR for failures in processing checkin message. --- examples/chip-tool/commands/common/CHIPCommand.cpp | 6 ++++++ src/app/icd/client/CheckInHandler.cpp | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 95be561d38d595..1054fae3c86d94 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -105,6 +105,12 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack() ReturnLogErrorOnFailure(mOperationalKeystore.Init(&mDefaultStorage)); ReturnLogErrorOnFailure(mOpCertStore.Init(&mDefaultStorage)); + // chip-tool uses a non-persistent keystore. + // ICD storage lifetime is currently tied to the chip-tool's lifetime. Since chip-tool interactive mode is currently used for + // ICD commissioning and check-in validation, this temporary storage meets the test requirements. + // TODO: Implement persistent ICD storage for the chip-tool. + ReturnLogErrorOnFailure(sICDClientStorage.Init(&mDefaultStorage, &sSessionKeystore)); + chip::Controller::FactoryInitParams factoryInitParams; factoryInitParams.fabricIndependentStorage = &mDefaultStorage; diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 224d865453fb35..0bebc7f56dc772 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -57,6 +57,7 @@ CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeMana void CheckInMessageHandler::Shutdown() { + mpICDClientStorage = nullptr; if (mpExchangeManager) { mpExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); @@ -77,14 +78,15 @@ CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHead CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { - VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), - CHIP_ERROR_INVALID_MESSAGE_TYPE); + // If the message type is not ICD_CheckIn, return CHIP_NO_ERROR and exit + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_NO_ERROR); ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; CounterType counter = 0; + // If the CheckIn message processing fails, return CHIP_NO_ERROR and exit. VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), - CHIP_ERROR_INCORRECT_STATE); + CHIP_NO_ERROR); CounterType checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); clientInfo.offset = checkInCounter; From ada48e8497cabf586d4c012f09e8fa143a90f186 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 13 Dec 2023 09:39:46 -0800 Subject: [PATCH 69/93] Addressed review comments --- src/app/icd/client/CheckInHandler.cpp | 8 ++++---- src/app/icd/client/CheckInHandler.h | 16 ++++++++-------- src/app/tests/BUILD.gn | 1 + 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 0bebc7f56dc772..578f597eb90c03 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -87,10 +87,10 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * // If the CheckIn message processing fails, return CHIP_NO_ERROR and exit. VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_NO_ERROR); - CounterType checkInCounter = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(checkInCounter > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); - clientInfo.offset = checkInCounter; - bool refreshKey = (checkInCounter > kKeyRefreshLimit); + CounterType receivedCheckInCouterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + VerifyOrReturnError(receivedCheckInCouterOffset > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); + clientInfo.offset = receivedCheckInCouterOffset; + bool refreshKey = (receivedCheckInCouterOffset > kKeyRefreshLimit); ByteSpan newKeyData; if (refreshKey) { diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index 3dff83248fa1e1..7e057a2740b0f9 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -56,6 +56,14 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return CheckInExchangeDispatch::Instance(); } private: + /** + * @brief Used by the application to set a new key to avoid counter rollover problems. + * + * @param[in] clientInfo clientInfo object + * @param[in] keyData New key data to use to re-register the client with the server + */ + CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); + class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch { public: @@ -76,14 +84,6 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi bool IsEncryptionRequired() const override { return false; } }; - /** - * @brief Used by the application to set a new key to avoid counter rollover problems. - * - * @param[in] clientInfo clientInfo object - * @param[in] keyData New key data to use to re-register the client with the server - */ - CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); - Messaging::ExchangeManager * mpExchangeManager = nullptr; CheckInDelegate * mpCheckInDelegate = nullptr; Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index 07e0e1f31ae44a..9c43361a9bfabd 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -181,6 +181,7 @@ chip_test_suite_using_nltest("tests") { test_sources += [ "TestEventLogging.cpp" ] #Not sure why TestDefaultICDClientStorage fails on openiotsdk + #Created https://github.com/project-chip/connectedhomeip/issues/30974 to analyze further test_sources += [ "TestDefaultICDClientStorage.cpp" ] } From 2e4e948f1e26f2d9c4cf793e3957133e0e9bb51f Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 13 Dec 2023 11:00:29 -0800 Subject: [PATCH 70/93] Fixed a spelling error --- src/app/icd/client/CheckInHandler.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 578f597eb90c03..3f369df78dd65f 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -87,13 +87,14 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * // If the CheckIn message processing fails, return CHIP_NO_ERROR and exit. VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_NO_ERROR); - CounterType receivedCheckInCouterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(receivedCheckInCouterOffset > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); - clientInfo.offset = receivedCheckInCouterOffset; - bool refreshKey = (receivedCheckInCouterOffset > kKeyRefreshLimit); - ByteSpan newKeyData; + CounterType receivedCheckInCounterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; + VerifyOrReturnError(receivedCheckInCounterOffset > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); + clientInfo.offset = receivedCheckInCounterOffset; + bool refreshKey = (receivedCheckInCounterOffset > kKeyRefreshLimit); + if (refreshKey) { + ByteSpan newKeyData; mpCheckInDelegate->OnRefreshKey(newKeyData); RegisterClientWithNewKey(clientInfo, newKeyData); } From 510e39dcee5cc72e5f78fc5ad14a0e2ff1edbbd6 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 14 Dec 2023 10:49:44 -0800 Subject: [PATCH 71/93] Moved variable definition --- src/app/icd/client/DefaultICDClientStorage.cpp | 2 -- src/app/icd/client/ICDClientStorage.h | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index c3710e831932fe..f8974b22d287a4 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -40,8 +40,6 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { -inline constexpr uint8_t kAppDataLength = 6; - CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 9b0bb069bef490..7e222c7c1013bb 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -31,6 +31,7 @@ namespace chip { namespace app { +inline constexpr uint8_t kAppDataLength = 6; using namespace Protocols::SecureChannel; /** * The ICDClientStorage class is an abstract interface that defines the operations From b619180540d0a7e185473f9b7fe07e43c63daebd Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 14 Dec 2023 22:13:13 -0800 Subject: [PATCH 72/93] Reenable TestDefaultClientStorage for open iot. --- src/app/tests/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index 9c43361a9bfabd..8fc492086961bc 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -162,6 +162,7 @@ chip_test_suite_using_nltest("tests") { } test_sources += [ "TestAclAttribute.cpp" ] + test_sources += [ "TestDefaultICDClientStorage.cpp" ] # # On NRF platforms, the allocation of a large number of pbufs in this test @@ -182,7 +183,6 @@ chip_test_suite_using_nltest("tests") { #Not sure why TestDefaultICDClientStorage fails on openiotsdk #Created https://github.com/project-chip/connectedhomeip/issues/30974 to analyze further - test_sources += [ "TestDefaultICDClientStorage.cpp" ] } # The platform manager is not properly clearing queues in test teardown, which results in From 30e705c414c4856c0443556e6d4f678fd9bf52f4 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 15 Dec 2023 06:14:10 +0000 Subject: [PATCH 73/93] Restyled by gn --- src/app/tests/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index 8fc492086961bc..de36c8c973ee6d 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -180,7 +180,6 @@ chip_test_suite_using_nltest("tests") { if (chip_device_platform != "nrfconnect" && chip_device_platform != "openiotsdk" && chip_device_platform != "fake") { test_sources += [ "TestEventLogging.cpp" ] - #Not sure why TestDefaultICDClientStorage fails on openiotsdk #Created https://github.com/project-chip/connectedhomeip/issues/30974 to analyze further } From 2f5a35aab559489b1d667f8347ff9625a7dad564 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 15 Dec 2023 10:16:00 -0800 Subject: [PATCH 74/93] Modified code to use updated API --- src/app/icd/client/DefaultICDClientStorage.cpp | 4 ++-- src/app/tests/TestDefaultICDClientStorage.cpp | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index d33c3f46b20975..c0c291679f053f 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -493,8 +493,8 @@ CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & paylo auto * iterator = IterateICDClientInfo(); while (iterator->Next(clientInfo)) { - CHIP_ERROR err = chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, - counter, appData); + CHIP_ERROR err = chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload( + clientInfo.aes_key_handle, clientInfo.hmac_key_handle, payload, counter, appData); if (CHIP_NO_ERROR == err) { iterator->Release(); diff --git a/src/app/tests/TestDefaultICDClientStorage.cpp b/src/app/tests/TestDefaultICDClientStorage.cpp index 6bd9d3c4edd8cb..9312a4589e1123 100644 --- a/src/app/tests/TestDefaultICDClientStorage.cpp +++ b/src/app/tests/TestDefaultICDClientStorage.cpp @@ -227,10 +227,10 @@ void TestProcessCheckInPayload(nlTestSuite * apSuite, void * apContext) NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t counter = 1; - System::PacketBufferHandle buffer = MessagePacketBuffer::New(chip::Protocols::SecureChannel::CheckinMessage::sMinPayloadSize); + System::PacketBufferHandle buffer = MessagePacketBuffer::New(chip::Protocols::SecureChannel::CheckinMessage::kMinPayloadSize); MutableByteSpan output{ buffer->Start(), buffer->MaxDataLength() }; - err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload(clientInfo.shared_key, counter, ByteSpan(), - output); + err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload( + clientInfo.aes_key_handle, clientInfo.hmac_key_handle, counter, ByteSpan(), output); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); buffer->SetDataLength(static_cast(output.size())); @@ -243,8 +243,8 @@ void TestProcessCheckInPayload(nlTestSuite * apSuite, void * apContext) // 2. Use a key not available in the storage for encoding err = manager.SetKey(clientInfo, ByteSpan(kKeyBuffer2)); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload(clientInfo.shared_key, counter, ByteSpan(), - output); + err = chip::Protocols::SecureChannel::CheckinMessage::GenerateCheckinMessagePayload( + clientInfo.aes_key_handle, clientInfo.hmac_key_handle, counter, ByteSpan(), output); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); buffer->SetDataLength(static_cast(output.size())); From 0cf167245ee75b860b42050dab574926e367cfa8 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 15 Dec 2023 11:40:28 -0800 Subject: [PATCH 75/93] Removed obsolete comments --- src/app/tests/BUILD.gn | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index de36c8c973ee6d..8bfb3fb6df816b 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -180,8 +180,6 @@ chip_test_suite_using_nltest("tests") { if (chip_device_platform != "nrfconnect" && chip_device_platform != "openiotsdk" && chip_device_platform != "fake") { test_sources += [ "TestEventLogging.cpp" ] - #Not sure why TestDefaultICDClientStorage fails on openiotsdk - #Created https://github.com/project-chip/connectedhomeip/issues/30974 to analyze further } # The platform manager is not properly clearing queues in test teardown, which results in From 56ff8fce57c4e81087e6f135bbb2793b30e9f499 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 15 Dec 2023 16:45:29 -0800 Subject: [PATCH 76/93] Added comments --- src/app/icd/client/CheckInDelegate.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index 8d6da512e9dfa8..cd0c8fee04acf0 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -24,6 +24,12 @@ namespace chip { namespace app { /// Callbacks for check in protocol +/** + * @brief The application implementing an ICD client should inherit the CheckInDelegate and implement the listed callbacks + * OnCheckInComplete will be called on successful processing of a received checkIn message from the server + * OnRefreshKey will be called when the key needs to be refreshed to avoid check in counter roll over problems. On receiving + * OnRefreshKey callback, the application should generate a new key. + */ class DLL_EXPORT CheckInDelegate { public: @@ -40,7 +46,7 @@ class DLL_EXPORT CheckInDelegate * @brief Callback used to let the application know that a checkin message was received and validated and a key refresh is * needed to avoid counter roolover problems. * - * The implementor of this function should generate a new key + * The implementer of this function should generate a new key * * @param[out] keyData - new key generated */ From f68c96be97f0ed9f065326c661221bfb4935d370 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Mon, 18 Dec 2023 09:20:06 -0800 Subject: [PATCH 77/93] Addressed review comments --- src/app/icd/client/CheckInDelegate.h | 4 ++-- src/app/icd/client/ICDClientStorage.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index cd0c8fee04acf0..0123825254e0cc 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -28,7 +28,7 @@ namespace app { * @brief The application implementing an ICD client should inherit the CheckInDelegate and implement the listed callbacks * OnCheckInComplete will be called on successful processing of a received checkIn message from the server * OnRefreshKey will be called when the key needs to be refreshed to avoid check in counter roll over problems. On receiving - * OnRefreshKey callback, the application should generate a new key. + * OnRefreshKey callback, the application needs to generate a new key. */ class DLL_EXPORT CheckInDelegate { @@ -43,7 +43,7 @@ class DLL_EXPORT CheckInDelegate virtual void OnCheckInComplete(ICDClientInfo & clientInfo) = 0; /** - * @brief Callback used to let the application know that a checkin message was received and validated and a key refresh is + * @brief Callback used to let the application know that a key refresh is * needed to avoid counter roolover problems. * * The implementer of this function should generate a new key diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index c2a919f1742104..160e77b15eba5a 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -31,7 +31,9 @@ namespace chip { namespace app { +// 4 bytes for counter + 2 bytes for ActiveModeThreshold inline constexpr uint8_t kAppDataLength = 6; + using namespace Protocols::SecureChannel; /** * The ICDClientStorage class is an abstract interface that defines the operations From 6e93fdfc4023bcc8be16e9d8d8b5e5a1a8378a01 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Fri, 15 Dec 2023 13:59:31 -0800 Subject: [PATCH 78/93] Improve ICDClientStorage (#30931) --- src/app/icd/client/DefaultICDClientStorage.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index c0c291679f053f..da4a6b7b66b891 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -39,9 +39,7 @@ static_assert(kMaxFabricListTlvLength <= std::numeric_limits::max(), " namespace chip { namespace app { - -CHIP_ERROR -DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) +CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex) { for (auto & fabric_idx : mFabricList) { From 10dbb2aee91b852775baf5b23bb7674fa3e8abff Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Mon, 18 Dec 2023 09:53:35 -0800 Subject: [PATCH 79/93] Added const qualifier to payload --- src/protocols/secure_channel/CheckinMessage.cpp | 2 +- src/protocols/secure_channel/CheckinMessage.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocols/secure_channel/CheckinMessage.cpp b/src/protocols/secure_channel/CheckinMessage.cpp index e8ba2082117361..b1d519b5d0deff 100644 --- a/src/protocols/secure_channel/CheckinMessage.cpp +++ b/src/protocols/secure_channel/CheckinMessage.cpp @@ -76,7 +76,7 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(const Crypto::Aes128Key } CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(const Crypto::Aes128KeyHandle & aes128KeyHandle, - const Crypto::Hmac128KeyHandle & hmacKeyHandle, ByteSpan & payload, + const Crypto::Hmac128KeyHandle & hmacKeyHandle, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData) { size_t appDataSize = GetAppDataSize(payload); diff --git a/src/protocols/secure_channel/CheckinMessage.h b/src/protocols/secure_channel/CheckinMessage.h index 2db1a6f1fa1ca3..b209ab8477c1b6 100644 --- a/src/protocols/secure_channel/CheckinMessage.h +++ b/src/protocols/secure_channel/CheckinMessage.h @@ -84,7 +84,7 @@ class DLL_EXPORT CheckinMessage */ static CHIP_ERROR ParseCheckinMessagePayload(const Crypto::Aes128KeyHandle & aes128KeyHandle, - const Crypto::Hmac128KeyHandle & hmacKeyHandle, ByteSpan & payload, + const Crypto::Hmac128KeyHandle & hmacKeyHandle, const ByteSpan & payload, CounterType & counter, MutableByteSpan & appData); static inline size_t GetCheckinPayloadSize(size_t appDataSize) { return appDataSize + kMinPayloadSize; } From c13125477cdbabcae63a259ce1dcb02221f32f4f Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 20 Dec 2023 13:48:46 -0800 Subject: [PATCH 80/93] Return CHIP_NO_ERROR for duplicate check in message and log error. --- src/app/icd/client/CheckInHandler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 3f369df78dd65f..09ae414b76bc93 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -88,7 +88,8 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_NO_ERROR); CounterType receivedCheckInCounterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - VerifyOrReturnError(receivedCheckInCounterOffset > clientInfo.offset, CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); + ChipLogError(ICD, "A duplicate CheckIn message was received and discarded"); + VerifyOrReturnError(receivedCheckInCounterOffset > clientInfo.offset, CHIP_NO_ERROR); clientInfo.offset = receivedCheckInCounterOffset; bool refreshKey = (receivedCheckInCounterOffset > kKeyRefreshLimit); From 5982e3932ba06738bb61d5d42e8159e41527e4c4 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 21 Dec 2023 15:13:48 -0800 Subject: [PATCH 81/93] Return CHIP_NO_ERROR on duplicate checkin messages --- src/app/icd/client/CheckInHandler.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 09ae414b76bc93..422293a1233a53 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -88,8 +88,14 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_NO_ERROR); CounterType receivedCheckInCounterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - ChipLogError(ICD, "A duplicate CheckIn message was received and discarded"); - VerifyOrReturnError(receivedCheckInCounterOffset > clientInfo.offset, CHIP_NO_ERROR); + + // Detect duplicate CheckIn messages and return CHIP_NO_ERROR on receiving a duplicate message + if (receivedCheckInCounterOffset <= clientInfo.offset) + { + ChipLogError(ICD, "A duplicate CheckIn message was received and discarded"); + return CHIP_NO_ERROR; + } + clientInfo.offset = receivedCheckInCounterOffset; bool refreshKey = (receivedCheckInCounterOffset > kKeyRefreshLimit); From 10e61ab6323f5425d16207b56f7cddb30b6c591a Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 29 Dec 2023 13:56:08 -0800 Subject: [PATCH 82/93] Added OnRefreshKeyRetrieve and addressed review comments --- examples/chip-tool/BUILD.gn | 1 - .../chip-tool/commands/common/CHIPCommand.cpp | 2 +- .../chip-tool/commands/common/CHIPCommand.h | 17 +++- src/app/icd/client/BUILD.gn | 1 + src/app/icd/client/CheckInDelegate.h | 45 +++++++--- src/app/icd/client/CheckInHandler.cpp | 88 +++++++++++++++---- src/app/icd/client/CheckInHandler.h | 38 ++++++-- src/app/icd/client/DefaultCheckInDelegate.cpp | 23 +++-- src/app/icd/client/DefaultCheckInDelegate.h | 22 ++++- .../icd/client/DefaultICDClientStorage.cpp | 4 - src/app/icd/client/ICDRefreshKeyInfo.h | 45 ++++++++++ 11 files changed, 235 insertions(+), 51 deletions(-) create mode 100644 src/app/icd/client/ICDRefreshKeyInfo.h diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index a0e9d5d8ec0576..53568316114c9a 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -103,7 +103,6 @@ static_library("chip-tool-utils") { public_deps = [ "${chip_root}/examples/common/tracing:commandline", "${chip_root}/src/app/icd/client:handler", - "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/icd/client:manager", "${chip_root}/src/app/server", "${chip_root}/src/app/tests/suites/commands/interaction_model", diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 1054fae3c86d94..45fc2105183cdc 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -51,7 +51,7 @@ chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGr chip::app::DefaultICDClientStorage CHIPCommand::sICDClientStorage; chip::Crypto::RawKeySessionKeystore CHIPCommand::sSessionKeystore; chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate; -chip::app::CheckInMessageHandler CHIPCommand::sCheckInHandler; +chip::app::CheckInHandler CHIPCommand::sCheckInHandler; namespace { diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 979d6ebc2ee065..3393f942c0a39e 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -129,7 +129,10 @@ class CHIPCommand : public Command // Shut down the command. After a Shutdown call the command object is ready // to be used for another command invocation. - virtual void Shutdown() { ResetArguments(); } + virtual void Shutdown() + { + ResetArguments(); + } // Clean up any resources allocated by the command. Some commands may hold // on to resources after Shutdown(), but Cleanup() will guarantee those are @@ -140,12 +143,18 @@ class CHIPCommand : public Command // can keep doing work as needed. Cleanup() will be called when quitting // interactive mode. This method will be called before Shutdown, so it can // use member values that Shutdown will normally reset. - virtual bool DeferInteractiveCleanup() { return false; } + virtual bool DeferInteractiveCleanup() + { + return false; + } // If true, the controller will be created with server capabilities enabled, // such as advertising operational nodes over DNS-SD and accepting incoming // CASE sessions. - virtual bool NeedsOperationalAdvertising() { return mAdvertiseOperational; } + virtual bool NeedsOperationalAdvertising() + { + return mAdvertiseOperational; + } // Execute any deferred cleanups. Used when exiting interactive mode. static void ExecuteDeferredCleanups(intptr_t ignored); @@ -163,7 +172,7 @@ class CHIPCommand : public Command static chip::Credentials::GroupDataProviderImpl sGroupDataProvider; static chip::app::DefaultICDClientStorage sICDClientStorage; static chip::app::DefaultCheckInDelegate sCheckInDelegate; - static chip::app::CheckInMessageHandler sCheckInHandler; + static chip::app::CheckInHandler sCheckInHandler; CredentialIssuerCommands * mCredIssuerCmds; std::string GetIdentity(); diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 88d31ff458f29f..1288e510740540 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -47,5 +47,6 @@ source_set("handler") { "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", + "${chip_root}/src/app", ] } diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index 0123825254e0cc..fefe32793d4d82 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -19,6 +19,7 @@ #pragma once #include +#include namespace chip { namespace app { @@ -26,9 +27,6 @@ namespace app { /// Callbacks for check in protocol /** * @brief The application implementing an ICD client should inherit the CheckInDelegate and implement the listed callbacks - * OnCheckInComplete will be called on successful processing of a received checkIn message from the server - * OnRefreshKey will be called when the key needs to be refreshed to avoid check in counter roll over problems. On receiving - * OnRefreshKey callback, the application needs to generate a new key. */ class DLL_EXPORT CheckInDelegate { @@ -36,21 +34,48 @@ class DLL_EXPORT CheckInDelegate virtual ~CheckInDelegate() {} /** - * @brief Callback used to let the application know that a checkin message was received and validated. + * @brief Callback used to let the application know that a check-in message was received and validated. * - * @param[in] clientInfo - ClientInfo object of the peer node + * @param[in] clientInfo - ICDClientInfo object representing the state associated with the + node that sent the check-in message. */ - virtual void OnCheckInComplete(ICDClientInfo & clientInfo) = 0; + virtual void OnCheckInComplete(const ICDClientInfo & clientInfo) = 0; /** * @brief Callback used to let the application know that a key refresh is - * needed to avoid counter roolover problems. + * needed to avoid counter rollover problems. * - * The implementer of this function should generate a new key + * The implementer of this function should generate a new key and store it in a map with peer nodeID as the key and + * ICDRefreshKeyInfo as the value. * - * @param[out] keyData - new key generated + * @param[in] clientInfo - ICDClientInfo object representing the state associated with the + node that sent the check-in message. The callee can use the clientInfo to determine the type of key + to generate. + * @param[out] keyData - pointer to the keyData buffer of size keyDataLength. The implementer of this callback should generate a + new key of size keyLength and copy it to the keyData buffer + * @param[in] keyLength - length of the new key to be generated */ - virtual void OnRefreshKey(ByteSpan & keyData) = 0; + virtual void OnRefreshKeyGenerate(const ICDClientInfo & clientInfo, uint8_t * keyData, uint8_t keyLength) = 0; + + /** + * @brief Callback used to retrieve the refresh key information from the application after establishing a new secure session for + * re-registration. The application should maintain a map to store the corresponding ICDRefreshKeyInfo for every peer node. + * Please refer to ICDRefreshKeyInfo.h for details. + * + * @param[in] nodeId - node ID of the peer with whom the client needs to re-register with a new key to avoid rollover problems. + * @param[out] refreshKeyInfo - stored refreshKeyInfo for the corresponding nodeId from the ICDRefreshKeyMap + */ + virtual CHIP_ERROR OnRefreshKeyRetrieve(const ScopedNodeId & nodeId, ICDRefreshKeyInfo & refreshKeyInfo) = 0; + + /** + * @brief Callback used to let the application know that the re-registration with the new key was successful and provides the + * updated ICDClientInfo + * + * @param[in] clientInfo - ICDClientInfo object representing the state associated with the + node that sent the check-in message. This will have the new key used for registration and the updated icd + counter. + */ + virtual void OnRegistrationComplete(const ICDClientInfo & clientInfo) = 0; }; } // namespace app diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 422293a1233a53..606b3c29e49a47 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -22,8 +22,10 @@ * */ +#include #include #include +#include #include @@ -32,6 +34,9 @@ #include #include +#include "controller/InvokeInteraction.h" +#include +#include #include namespace chip { @@ -40,8 +45,12 @@ namespace app { inline constexpr uint64_t kCheckInCounterMax = (1ULL << 32); inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); -CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, - CheckInDelegate * delegate) +CheckInHandler::CheckInHandler() : + mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this) +{} + +CHIP_ERROR CheckInHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, + CheckInDelegate * delegate) { VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT); @@ -55,9 +64,10 @@ CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeMana return mpExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this); } -void CheckInMessageHandler::Shutdown() +void CheckInHandler::Shutdown() { mpICDClientStorage = nullptr; + mpCheckInDelegate = nullptr; if (mpExchangeManager) { mpExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn); @@ -65,7 +75,7 @@ void CheckInMessageHandler::Shutdown() } } -CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) +CHIP_ERROR CheckInHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) { // Return error for wrong message type VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), @@ -75,8 +85,8 @@ CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHead return CHIP_NO_ERROR; } -CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, - System::PacketBufferHandle && payload) +CHIP_ERROR CheckInHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) { // If the message type is not ICD_CheckIn, return CHIP_NO_ERROR and exit VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_NO_ERROR); @@ -84,15 +94,15 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() }; ICDClientInfo clientInfo; CounterType counter = 0; - // If the CheckIn message processing fails, return CHIP_NO_ERROR and exit. + // If the check-in message processing fails, return CHIP_NO_ERROR and exit. VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), CHIP_NO_ERROR); CounterType receivedCheckInCounterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; - // Detect duplicate CheckIn messages and return CHIP_NO_ERROR on receiving a duplicate message + // Detect duplicate check-in messages and return CHIP_NO_ERROR on receiving a duplicate message if (receivedCheckInCounterOffset <= clientInfo.offset) { - ChipLogError(ICD, "A duplicate CheckIn message was received and discarded"); + ChipLogError(ICD, "A duplicate check-in message was received and discarded"); return CHIP_NO_ERROR; } @@ -101,20 +111,66 @@ CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * if (refreshKey) { - ByteSpan newKeyData; - mpCheckInDelegate->OnRefreshKey(newKeyData); - RegisterClientWithNewKey(clientInfo, newKeyData); + uint8_t newKeyData[chip::Crypto::kAES_CCM128_Key_Length]; + mpCheckInDelegate->OnRefreshKeyGenerate(clientInfo, newKeyData, chip::Crypto::kAES_CCM128_Key_Length); + // A new session should be established to re-register the client using the new key. The registration will happen in + // mOnDeviceConnected callback + EstablishSessionToPeer(clientInfo.peer_node); + } + else + { + mpCheckInDelegate->OnCheckInComplete(clientInfo); } - mpCheckInDelegate->OnCheckInComplete(clientInfo); + return CHIP_NO_ERROR; } -CHIP_ERROR CheckInMessageHandler::RegisterClientWithNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData) +CHIP_ERROR CheckInHandler::RegisterClientWithNewKey(ICDClientInfo & clientInfo, ByteSpan newKey, + Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) { - // TODO - Register the client. On successful registration, update the clientInfo with the new key and store the clientInfo + Clusters::IcdManagement::Commands::RegisterClient::Type request; + request.checkInNodeID = clientInfo.peer_node.GetNodeId(); + request.monitoredSubject = clientInfo.monitored_subject; + request.key = newKey; + // TODO1 : We don't have plain data for the old key + // TODO2 : Find the right way to send the registration command. Both the success and failure callbacks for registration should + // call OnCheckInComplete + return CHIP_NO_ERROR; } -void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} + +void CheckInHandler::EstablishSessionToPeer(ScopedNodeId peerId) +{ + ChipLogProgress(ICD, "Trying to establish a CASE session for re-registering an ICD client"); + auto * caseSessionManager = InteractionModelEngine::GetInstance()->GetCASESessionManager(); + VerifyOrReturn(caseSessionManager != nullptr); + caseSessionManager->FindOrEstablishSession(peerId, &mOnConnectedCallback, &mOnConnectionFailureCallback); +} + +void CheckInHandler::HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr, + const SessionHandle & sessionHandle) +{ + CheckInHandler * const _this = static_cast(context); + VerifyOrDie(_this != nullptr); + ICDRefreshKeyInfo refreshKeyInfo; + if (CHIP_NO_ERROR != + _this->mpCheckInDelegate->OnRefreshKeyRetrieve(sessionHandle->AsSecureSession()->GetPeer(), refreshKeyInfo)) + { + ChipLogError(ICD, "Failed to retrieve a new key for re-registration of the ICD client"); + } + ByteSpan newKey(refreshKeyInfo.newKey); + _this->RegisterClientWithNewKey(refreshKeyInfo.clientInfo, newKey, exchangeMgr, sessionHandle); +} + +void CheckInHandler::HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err) +{ + CheckInHandler * const _this = static_cast(context); + VerifyOrDie(_this != nullptr); + + ChipLogError(ICD, "Failed to establish CASE for re-registration with error '%" CHIP_ERROR_FORMAT "'", err.Format()); +} + +void CheckInHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} } // namespace app } // namespace chip diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index 7e057a2740b0f9..b0f3cd4bc5203f 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -18,13 +18,15 @@ /** * @file - * This file defines objects for a CHIP CheckInMessage unsolicited + * This file defines objects for a CHIP check-in message unsolicited * handler * */ #pragma once +#include +#include #include #include #include @@ -33,19 +35,27 @@ namespace chip { namespace app { -class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler + +class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler { public: CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate); void Shutdown(); - virtual ~CheckInMessageHandler() = default; + CheckInHandler(); + + virtual ~CheckInHandler() = default; + + static void HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr, + const SessionHandle & sessionHandle); + static void HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err); protected: // ExchangeDelegate - CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, - System::PacketBufferHandle && payload) override; + CHIP_ERROR + OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) override; // UnsolicitedMessageHandler CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; @@ -62,7 +72,20 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi * @param[in] clientInfo clientInfo object * @param[in] keyData New key data to use to re-register the client with the server */ - CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, const ByteSpan keyData); + CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, ByteSpan newKey, Messaging::ExchangeManager & exchangeMgr, + const SessionHandle & sessionHandle); + + /** + * @brief Sets up a CASE session to the peer, if we can locate a + * CASESessionManager. Returns error if we did not even manage to kick off + * a CASE attempt. + * + * @param[in] peerId Node ID of the peer + */ + void EstablishSessionToPeer(ScopedNodeId peerId); + + chip::Callback::Callback mOnConnectedCallback; + chip::Callback::Callback mOnConnectionFailureCallback; class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch { @@ -86,8 +109,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi Messaging::ExchangeManager * mpExchangeManager = nullptr; CheckInDelegate * mpCheckInDelegate = nullptr; - Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeManager; } - ICDClientStorage * mpICDClientStorage = nullptr; + ICDClientStorage * mpICDClientStorage = nullptr; }; } // namespace app diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 4c059cf3a961f6..9dc2e636c62e2d 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace chip { namespace app { @@ -32,19 +33,31 @@ CHIP_ERROR DefaultCheckInDelegate::Init(ICDClientStorage * storage) return CHIP_NO_ERROR; } -void DefaultCheckInDelegate::OnCheckInComplete(ICDClientInfo & clientInfo) +void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo) { ChipLogProgress( ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId, clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); } -void DefaultCheckInDelegate::OnRefreshKey(ByteSpan & keyData) +void DefaultCheckInDelegate::OnRefreshKeyGenerate(const ICDClientInfo & clientInfo, uint8_t * keyData, uint8_t keyLength) { - uint8_t randomGeneratedSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length]; - chip::Crypto::DRBG_get_bytes(randomGeneratedSymmetricKey, sizeof(randomGeneratedSymmetricKey)); - keyData = ByteSpan(randomGeneratedSymmetricKey); + chip::Crypto::DRBG_get_bytes(keyData, keyLength); + ICDRefreshKeyInfo refreshKeyInfo; + refreshKeyInfo.clientInfo = clientInfo; + memcpy(&refreshKeyInfo.newKey, keyData, keyLength); + icdRefreshKeyMap.insert(make_pair(clientInfo.peer_node, refreshKeyInfo)); } +CHIP_ERROR DefaultCheckInDelegate::OnRefreshKeyRetrieve(const ScopedNodeId & nodeId, ICDRefreshKeyInfo & refreshKeyInfo) +{ + // Todo : Check if CHIP_ERROR_KEY_NOT_FOUND can be used here + VerifyOrReturnError(icdRefreshKeyMap.find(nodeId) != icdRefreshKeyMap.end(), CHIP_ERROR_KEY_NOT_FOUND); + refreshKeyInfo = icdRefreshKeyMap.at(nodeId); + return CHIP_NO_ERROR; +} + +void DefaultCheckInDelegate::OnRegistrationComplete(const ICDClientInfo & clientInfo) {} + } // namespace app } // namespace chip diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index 0c775141953a25..4ab3b0d8f64e22 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -18,22 +18,40 @@ #pragma once +#include "ICDRefreshKeyInfo.h" #include #include +#include + namespace chip { namespace app { +using namespace std; + /// Callbacks for check in protocol class DefaultCheckInDelegate : public CheckInDelegate { public: virtual ~DefaultCheckInDelegate() {} CHIP_ERROR Init(ICDClientStorage * storage); - void OnCheckInComplete(ICDClientInfo & clientInfo) override; - void OnRefreshKey(ByteSpan & keyData) override; + void OnCheckInComplete(const ICDClientInfo & clientInfo) override; + void OnRefreshKeyGenerate(const ICDClientInfo & clientInfo, uint8_t * keyData, uint8_t keyLength) override; + CHIP_ERROR OnRefreshKeyRetrieve(const ScopedNodeId & nodeId, ICDRefreshKeyInfo & refreshKeyInfo) override; + void OnRegistrationComplete(const ICDClientInfo & clientInfo) override; + // Hash function for the map : {NodeId, ICDRefreshKeyInfo} + struct HashFunction + { + size_t operator()(const ScopedNodeId & peer) const + { + size_t nodeIdHash = std::hash()(peer.GetNodeId()); + size_t fabricIdxHash = std::hash()(peer.GetFabricIndex()) << 1; + return nodeIdHash ^ fabricIdxHash; + } + }; private: ICDClientStorage * mpStorage = nullptr; + unordered_map icdRefreshKeyMap; }; } // namespace app diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index af041c46722c7f..b3a4894fccff49 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -465,10 +465,6 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, CounterType & counter) { - /*appDataBuffer is the working buffer that will be used to retrieve data from the payload. - *counter to retrieve - 4 bytes - *appData to retrieve - 2 bytes(activeModeThreshold) - */ uint8_t appDataBuffer[kAppDataLength]; MutableByteSpan appData(appDataBuffer); auto * iterator = IterateICDClientInfo(); diff --git a/src/app/icd/client/ICDRefreshKeyInfo.h b/src/app/icd/client/ICDRefreshKeyInfo.h new file mode 100644 index 00000000000000..790b4921664e93 --- /dev/null +++ b/src/app/icd/client/ICDRefreshKeyInfo.h @@ -0,0 +1,45 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include "ICDClientInfo.h" +#include + +namespace chip { +namespace app { + +/* + *@brief Structure to hold the clientInfo and the new key generated to re-register with a peer + */ +struct ICDRefreshKeyInfo +{ + ICDClientInfo clientInfo; + uint8_t newKey[chip::Crypto::kAES_CCM128_Key_Length]; + + ICDRefreshKeyInfo() {} + ICDRefreshKeyInfo(const ICDRefreshKeyInfo & other) { *this = other; } + + ICDRefreshKeyInfo & operator=(const ICDRefreshKeyInfo & other) + { + clientInfo = other.clientInfo; + memcpy(newKey, other.newKey, chip::Crypto::kAES_CCM128_Key_Length); + return *this; + } +}; + +} // namespace app +} // namespace chip \ No newline at end of file From fe849c6256653878684d9c263315e45b097f6bda Mon Sep 17 00:00:00 2001 From: thivya-amazon <99231372+thivya-amazon@users.noreply.github.com> Date: Fri, 29 Dec 2023 14:00:38 -0800 Subject: [PATCH 83/93] Update src/lib/core/CHIPError.h Co-authored-by: mkardous-silabs <84793247+mkardous-silabs@users.noreply.github.com> --- src/lib/core/CHIPError.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 6ae347b698d9fc..7edc73224aad6e 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -618,7 +618,6 @@ using CHIP_ERROR = ::chip::ChipError; */ #define CHIP_ERROR_INVALID_TLV_CHAR_STRING CHIP_CORE_ERROR(0x15) - // AVAILABLE: 0x16 /** From 9f6bbf0985ef1863b2c8f3b3b70f6d8b797cf547 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 29 Dec 2023 22:01:14 +0000 Subject: [PATCH 84/93] Restyled by whitespace --- src/app/icd/client/ICDRefreshKeyInfo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/ICDRefreshKeyInfo.h b/src/app/icd/client/ICDRefreshKeyInfo.h index 790b4921664e93..53dc6c506bc75f 100644 --- a/src/app/icd/client/ICDRefreshKeyInfo.h +++ b/src/app/icd/client/ICDRefreshKeyInfo.h @@ -42,4 +42,4 @@ struct ICDRefreshKeyInfo }; } // namespace app -} // namespace chip \ No newline at end of file +} // namespace chip From a3e4b23d26bc4ec5f21cd25131a82953553e18ea Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 29 Dec 2023 22:01:18 +0000 Subject: [PATCH 85/93] Restyled by clang-format --- examples/chip-tool/commands/common/CHIPCommand.h | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 3393f942c0a39e..2caf6fe9cc94ec 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -129,10 +129,7 @@ class CHIPCommand : public Command // Shut down the command. After a Shutdown call the command object is ready // to be used for another command invocation. - virtual void Shutdown() - { - ResetArguments(); - } + virtual void Shutdown() { ResetArguments(); } // Clean up any resources allocated by the command. Some commands may hold // on to resources after Shutdown(), but Cleanup() will guarantee those are @@ -143,18 +140,12 @@ class CHIPCommand : public Command // can keep doing work as needed. Cleanup() will be called when quitting // interactive mode. This method will be called before Shutdown, so it can // use member values that Shutdown will normally reset. - virtual bool DeferInteractiveCleanup() - { - return false; - } + virtual bool DeferInteractiveCleanup() { return false; } // If true, the controller will be created with server capabilities enabled, // such as advertising operational nodes over DNS-SD and accepting incoming // CASE sessions. - virtual bool NeedsOperationalAdvertising() - { - return mAdvertiseOperational; - } + virtual bool NeedsOperationalAdvertising() { return mAdvertiseOperational; } // Execute any deferred cleanups. Used when exiting interactive mode. static void ExecuteDeferredCleanups(intptr_t ignored); From 9843156324ddaa863eb8dab1f8d2dfe8ed330e2f Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 29 Dec 2023 22:01:19 +0000 Subject: [PATCH 86/93] Restyled by gn --- src/app/icd/client/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/BUILD.gn b/src/app/icd/client/BUILD.gn index 1288e510740540..fa32e9c68dbfd4 100644 --- a/src/app/icd/client/BUILD.gn +++ b/src/app/icd/client/BUILD.gn @@ -44,9 +44,9 @@ source_set("handler") { ] public_deps = [ ":manager", + "${chip_root}/src/app", "${chip_root}/src/lib/core", "${chip_root}/src/messaging", "${chip_root}/src/protocols", - "${chip_root}/src/app", ] } From 96b4d2b56ffdef656ef84fbab2bdeb0a9fddb719 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 29 Dec 2023 14:17:35 -0800 Subject: [PATCH 87/93] Added a temporary suppression of error for including --- src/app/icd/client/DefaultCheckInDelegate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index 4ab3b0d8f64e22..cbae7ad95e3f38 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -21,7 +21,7 @@ #include "ICDRefreshKeyInfo.h" #include #include -#include +#include TODO: update check_includes_config.py namespace chip { namespace app { From 0b54b26f9dde5d8cfd9d9253c98c9ce0d65a6adc Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 29 Dec 2023 14:33:46 -0800 Subject: [PATCH 88/93] Added a comment to suppress Lint error --- src/app/icd/client/DefaultCheckInDelegate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index cbae7ad95e3f38..deb784363a5d1e 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -21,7 +21,7 @@ #include "ICDRefreshKeyInfo.h" #include #include -#include TODO: update check_includes_config.py +#include //TODO: update check_includes_config.py namespace chip { namespace app { From 1eba16b2a7a10355e39de9f707a0fb05b1ca4f73 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Thu, 4 Jan 2024 12:13:53 -0800 Subject: [PATCH 89/93] Addressed review comments --- src/app/icd/client/CheckInDelegate.h | 4 +-- src/app/icd/client/CheckInHandler.cpp | 33 +++++++++++++++++------- src/app/icd/client/CheckInHandler.h | 36 ++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index fefe32793d4d82..1e722221c016b6 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -59,8 +59,8 @@ class DLL_EXPORT CheckInDelegate /** * @brief Callback used to retrieve the refresh key information from the application after establishing a new secure session for - * re-registration. The application should maintain a map to store the corresponding ICDRefreshKeyInfo for every peer node. - * Please refer to ICDRefreshKeyInfo.h for details. + * re-registration. The application should be able to store the corresponding ICDRefreshKeyInfo for every peer node. The + * application can determine the best way to do this. Please refer to ICDRefreshKeyInfo.h for details. * * @param[in] nodeId - node ID of the peer with whom the client needs to re-register with a new key to avoid rollover problems. * @param[out] refreshKeyInfo - stored refreshKeyInfo for the corresponding nodeId from the ICDRefreshKeyMap diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 606b3c29e49a47..20940af43e9add 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -22,6 +22,7 @@ * */ +#include #include #include #include @@ -34,7 +35,6 @@ #include #include -#include "controller/InvokeInteraction.h" #include #include #include @@ -128,13 +128,25 @@ CHIP_ERROR CheckInHandler::OnMessageReceived(Messaging::ExchangeContext * ec, co CHIP_ERROR CheckInHandler::RegisterClientWithNewKey(ICDClientInfo & clientInfo, ByteSpan newKey, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) { - Clusters::IcdManagement::Commands::RegisterClient::Type request; - request.checkInNodeID = clientInfo.peer_node.GetNodeId(); - request.monitoredSubject = clientInfo.monitored_subject; - request.key = newKey; - // TODO1 : We don't have plain data for the old key - // TODO2 : Find the right way to send the registration command. Both the success and failure callbacks for registration should - // call OnCheckInComplete + // using namespace Clusters::IcdManagement; + // TODO : Determine if using an Objectpool of commandSenders is the best approach here + // app::CommandSender registerCommandSender(®isterCommandSenderDelegate, &exchangeMgr); + + // auto commandPathParams = CommandPathParams(0, 0, Id, Commands::RegisterClient::Id, (CommandPathFlags::kEndpointIdValid)); + // ReturnErrorOnFailure(registerCommandSender.PrepareCommand(commandPathParams)); + + // chip::TLV::TLVWriter * writer = registerCommandSender.GetCommandDataIBTLVWriter(); + + // ReturnErrorOnFailure( + // writer->Put(chip::TLV::ContextTag(Commands::RegisterClient::Fields::kCheckInNodeID), clientInfo.peer_node.GetNodeId())); + // ReturnErrorOnFailure( + // writer->Put(chip::TLV::ContextTag(Commands::RegisterClient::Fields::kMonitoredSubject), clientInfo.monitored_subject)); + // ReturnErrorOnFailure(writer->Put(chip::TLV::ContextTag(Commands::RegisterClient::Fields::kKey), newKey)); + + // // TODO : We don't have plain data for the old key + + // ReturnErrorOnFailure(registerCommandSender.FinishCommand()); + // ReturnErrorOnFailure(registerCommandSender.SendCommandRequest(sessionHandle)); return CHIP_NO_ERROR; } @@ -159,7 +171,10 @@ void CheckInHandler::HandleDeviceConnected(void * context, Messaging::ExchangeMa ChipLogError(ICD, "Failed to retrieve a new key for re-registration of the ICD client"); } ByteSpan newKey(refreshKeyInfo.newKey); - _this->RegisterClientWithNewKey(refreshKeyInfo.clientInfo, newKey, exchangeMgr, sessionHandle); + if (CHIP_NO_ERROR != _this->RegisterClientWithNewKey(refreshKeyInfo.clientInfo, newKey, exchangeMgr, sessionHandle)) + { + ChipLogError(ICD, "Failed to send register client command"); + } } void CheckInHandler::HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err) diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index b0f3cd4bc5203f..78fb4aa70afb6f 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -26,6 +26,7 @@ #pragma once #include +#include #include #include #include @@ -47,10 +48,38 @@ class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::Uns virtual ~CheckInHandler() = default; + /** + * @brief Callback received on successfully establishing a CASE session in order to re-register the client with the peer node + * using a new key to avoid counter rollover problems. + * + * @param[in] context context of the client establishing the CASE session + * @param[in] exchangeMgr exchange manager to use for the re-registration + * @param[in] sessionHandle session handle to use for the re-registration + */ static void HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle); + /** + * @brief Callback received on failure to establish a CASE session in order to re-register the client with the peer node using a + * new key to avoid counter rollover problems. + * + * @param[in] context context of the client establishing the CASE session + * @param[in] peerId Node ID of the peer node + * @param[in] err failure reason + */ static void HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err); + class RegisterCommandSenderCallback : public CommandSender::Callback + { + public: + void OnResponse(chip::app::CommandSender * apCommandSender, const chip::app::ConcreteCommandPath & aPath, + const chip::app::StatusIB & aStatus, chip::TLV::TLVReader * aData) override + {} + void OnError(const chip::app::CommandSender * apCommandSender, CHIP_ERROR aError) override { mError = aError; } + void OnDone(chip::app::CommandSender * apCommandSender) override {} + + CHIP_ERROR mError = CHIP_NO_ERROR; + } registerCommandSenderDelegate; + protected: // ExchangeDelegate CHIP_ERROR @@ -71,14 +100,15 @@ class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::Uns * * @param[in] clientInfo clientInfo object * @param[in] keyData New key data to use to re-register the client with the server + * @param[in] exchangeMgr exchange manager to use for the re-registration + * @param[in] sessionHandle session handle to use for the re-registration */ CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, ByteSpan newKey, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle); /** - * @brief Sets up a CASE session to the peer, if we can locate a - * CASESessionManager. Returns error if we did not even manage to kick off - * a CASE attempt. + * @brief Sets up a CASE session to the peer for re-registering a client with the peer when a key refresh is required to avoid + * ICD counter rollover. Returns error if we did not even manage to kick off a CASE attempt. * * @param[in] peerId Node ID of the peer */ From 435ffe6c1269d324e9f918447c49215dbab5b21d Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Fri, 5 Jan 2024 11:02:30 -0800 Subject: [PATCH 90/93] Addressed review comments --- src/app/icd/client/CheckInHandler.h | 2 +- src/app/icd/client/ICDClientStorage.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index 78fb4aa70afb6f..4d520753a680c2 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -63,7 +63,7 @@ class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::Uns * new key to avoid counter rollover problems. * * @param[in] context context of the client establishing the CASE session - * @param[in] peerId Node ID of the peer node + * @param[in] peerId Scoped Node ID of the peer node * @param[in] err failure reason */ static void HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err); diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index ffcdfeaa84e281..81da805f4e8b0f 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -31,9 +31,6 @@ namespace chip { namespace app { -// 4 bytes for counter + 2 bytes for ActiveModeThreshold -inline constexpr uint8_t kAppDataLength = 6; - using namespace Protocols::SecureChannel; /** * The ICDClientStorage class is an abstract interface that defines the operations @@ -85,6 +82,9 @@ class ICDClientStorage * @param[out] counter counter value received in the CheckIn message */ virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, CounterType & counter) = 0; + + // 4 bytes for counter + 2 bytes for ActiveModeThreshold + static inline constexpr uint8_t kAppDataLength = 6; }; } // namespace app } // namespace chip From 6563e9741a923e2a90d7d2d53ba2319c60bdf6b7 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 9 Jan 2024 11:36:49 -0800 Subject: [PATCH 91/93] Addressed review comments. --- src/app/icd/client/CheckInHandler.cpp | 2 +- src/app/icd/client/DefaultICDClientStorage.cpp | 1 + src/app/icd/client/ICDClientStorage.h | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index 20940af43e9add..a1626a1331ca1a 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -18,7 +18,7 @@ /** * @file - * This file defines objects for a CHIP ICD handler which handles unsolicited Check-In messages. + * This file defines objects for a CHIP ICD handler which handles unsolicited check-in messages. * */ diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index b3a4894fccff49..7ada84287fa4df 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -468,6 +468,7 @@ CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & paylo uint8_t appDataBuffer[kAppDataLength]; MutableByteSpan appData(appDataBuffer); auto * iterator = IterateICDClientInfo(); + VerifyOrReturnError(iterator != nullptr, CHIP_ERROR_NO_MEMORY); while (iterator->Next(clientInfo)) { CHIP_ERROR err = chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload( diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 81da805f4e8b0f..d65a64ff8c21a8 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -74,12 +74,12 @@ class ICDClientStorage virtual CHIP_ERROR DeleteEntry(const ScopedNodeId & peerNode) = 0; /** - * Process received ICD Check-in message payload. The implementation needs to parse the payload, + * Process received ICD check-in message payload. The implementation needs to parse the payload, * look for a key that allows successfully decrypting the payload, verify that the counter in the payload is valid, * and populate the clientInfo with the stored information corresponding to the key. - * @param[in] payload received checkIn Message payload + * @param[in] payload received check-in Message payload * @param[out] clientInfo retrieved matched clientInfo from storage - * @param[out] counter counter value received in the CheckIn message + * @param[out] counter counter value received in the check-in message */ virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, CounterType & counter) = 0; From f83002f28b2655e4f8e2617f11b24cd9abca6937 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Tue, 9 Jan 2024 17:37:21 -0800 Subject: [PATCH 92/93] Removed code pertaining to key refresh. Will be a separate PR. --- src/app/icd/client/CheckInDelegate.h | 37 --------- src/app/icd/client/CheckInHandler.cpp | 83 ++----------------- src/app/icd/client/CheckInHandler.h | 54 ------------ src/app/icd/client/DefaultCheckInDelegate.cpp | 19 ----- src/app/icd/client/DefaultCheckInDelegate.h | 16 ---- src/app/icd/client/ICDRefreshKeyInfo.h | 45 ---------- 6 files changed, 9 insertions(+), 245 deletions(-) delete mode 100644 src/app/icd/client/ICDRefreshKeyInfo.h diff --git a/src/app/icd/client/CheckInDelegate.h b/src/app/icd/client/CheckInDelegate.h index 1e722221c016b6..50d378a87a58e7 100644 --- a/src/app/icd/client/CheckInDelegate.h +++ b/src/app/icd/client/CheckInDelegate.h @@ -19,7 +19,6 @@ #pragma once #include -#include namespace chip { namespace app { @@ -40,42 +39,6 @@ class DLL_EXPORT CheckInDelegate node that sent the check-in message. */ virtual void OnCheckInComplete(const ICDClientInfo & clientInfo) = 0; - - /** - * @brief Callback used to let the application know that a key refresh is - * needed to avoid counter rollover problems. - * - * The implementer of this function should generate a new key and store it in a map with peer nodeID as the key and - * ICDRefreshKeyInfo as the value. - * - * @param[in] clientInfo - ICDClientInfo object representing the state associated with the - node that sent the check-in message. The callee can use the clientInfo to determine the type of key - to generate. - * @param[out] keyData - pointer to the keyData buffer of size keyDataLength. The implementer of this callback should generate a - new key of size keyLength and copy it to the keyData buffer - * @param[in] keyLength - length of the new key to be generated - */ - virtual void OnRefreshKeyGenerate(const ICDClientInfo & clientInfo, uint8_t * keyData, uint8_t keyLength) = 0; - - /** - * @brief Callback used to retrieve the refresh key information from the application after establishing a new secure session for - * re-registration. The application should be able to store the corresponding ICDRefreshKeyInfo for every peer node. The - * application can determine the best way to do this. Please refer to ICDRefreshKeyInfo.h for details. - * - * @param[in] nodeId - node ID of the peer with whom the client needs to re-register with a new key to avoid rollover problems. - * @param[out] refreshKeyInfo - stored refreshKeyInfo for the corresponding nodeId from the ICDRefreshKeyMap - */ - virtual CHIP_ERROR OnRefreshKeyRetrieve(const ScopedNodeId & nodeId, ICDRefreshKeyInfo & refreshKeyInfo) = 0; - - /** - * @brief Callback used to let the application know that the re-registration with the new key was successful and provides the - * updated ICDClientInfo - * - * @param[in] clientInfo - ICDClientInfo object representing the state associated with the - node that sent the check-in message. This will have the new key used for registration and the updated icd - counter. - */ - virtual void OnRegistrationComplete(const ICDClientInfo & clientInfo) = 0; }; } // namespace app diff --git a/src/app/icd/client/CheckInHandler.cpp b/src/app/icd/client/CheckInHandler.cpp index a1626a1331ca1a..f05dcd8b7819ed 100644 --- a/src/app/icd/client/CheckInHandler.cpp +++ b/src/app/icd/client/CheckInHandler.cpp @@ -22,11 +22,9 @@ * */ -#include #include #include #include -#include #include @@ -35,8 +33,6 @@ #include #include -#include -#include #include namespace chip { @@ -45,9 +41,7 @@ namespace app { inline constexpr uint64_t kCheckInCounterMax = (1ULL << 32); inline constexpr uint32_t kKeyRefreshLimit = (1U << 31); -CheckInHandler::CheckInHandler() : - mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this) -{} +CheckInHandler::CheckInHandler() {} CHIP_ERROR CheckInHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage, CheckInDelegate * delegate) @@ -95,8 +89,12 @@ CHIP_ERROR CheckInHandler::OnMessageReceived(Messaging::ExchangeContext * ec, co ICDClientInfo clientInfo; CounterType counter = 0; // If the check-in message processing fails, return CHIP_NO_ERROR and exit. - VerifyOrReturnError(CHIP_NO_ERROR == mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter), - CHIP_NO_ERROR); + CHIP_ERROR err = mpICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, counter); + if (CHIP_NO_ERROR != err) + { + ChipLogError(ICD, "ProcessCheckInPayload failed: %" CHIP_ERROR_FORMAT, err.Format()); + return CHIP_NO_ERROR; + } CounterType receivedCheckInCounterOffset = (counter - clientInfo.start_icd_counter) % kCheckInCounterMax; // Detect duplicate check-in messages and return CHIP_NO_ERROR on receiving a duplicate message @@ -111,11 +109,8 @@ CHIP_ERROR CheckInHandler::OnMessageReceived(Messaging::ExchangeContext * ec, co if (refreshKey) { - uint8_t newKeyData[chip::Crypto::kAES_CCM128_Key_Length]; - mpCheckInDelegate->OnRefreshKeyGenerate(clientInfo, newKeyData, chip::Crypto::kAES_CCM128_Key_Length); - // A new session should be established to re-register the client using the new key. The registration will happen in - // mOnDeviceConnected callback - EstablishSessionToPeer(clientInfo.peer_node); + // TODO: A new CASE session should be established to re-register the client using a new key. The registration will happen in + // CASE session callback } else { @@ -125,66 +120,6 @@ CHIP_ERROR CheckInHandler::OnMessageReceived(Messaging::ExchangeContext * ec, co return CHIP_NO_ERROR; } -CHIP_ERROR CheckInHandler::RegisterClientWithNewKey(ICDClientInfo & clientInfo, ByteSpan newKey, - Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) -{ - // using namespace Clusters::IcdManagement; - // TODO : Determine if using an Objectpool of commandSenders is the best approach here - // app::CommandSender registerCommandSender(®isterCommandSenderDelegate, &exchangeMgr); - - // auto commandPathParams = CommandPathParams(0, 0, Id, Commands::RegisterClient::Id, (CommandPathFlags::kEndpointIdValid)); - // ReturnErrorOnFailure(registerCommandSender.PrepareCommand(commandPathParams)); - - // chip::TLV::TLVWriter * writer = registerCommandSender.GetCommandDataIBTLVWriter(); - - // ReturnErrorOnFailure( - // writer->Put(chip::TLV::ContextTag(Commands::RegisterClient::Fields::kCheckInNodeID), clientInfo.peer_node.GetNodeId())); - // ReturnErrorOnFailure( - // writer->Put(chip::TLV::ContextTag(Commands::RegisterClient::Fields::kMonitoredSubject), clientInfo.monitored_subject)); - // ReturnErrorOnFailure(writer->Put(chip::TLV::ContextTag(Commands::RegisterClient::Fields::kKey), newKey)); - - // // TODO : We don't have plain data for the old key - - // ReturnErrorOnFailure(registerCommandSender.FinishCommand()); - // ReturnErrorOnFailure(registerCommandSender.SendCommandRequest(sessionHandle)); - - return CHIP_NO_ERROR; -} - -void CheckInHandler::EstablishSessionToPeer(ScopedNodeId peerId) -{ - ChipLogProgress(ICD, "Trying to establish a CASE session for re-registering an ICD client"); - auto * caseSessionManager = InteractionModelEngine::GetInstance()->GetCASESessionManager(); - VerifyOrReturn(caseSessionManager != nullptr); - caseSessionManager->FindOrEstablishSession(peerId, &mOnConnectedCallback, &mOnConnectionFailureCallback); -} - -void CheckInHandler::HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr, - const SessionHandle & sessionHandle) -{ - CheckInHandler * const _this = static_cast(context); - VerifyOrDie(_this != nullptr); - ICDRefreshKeyInfo refreshKeyInfo; - if (CHIP_NO_ERROR != - _this->mpCheckInDelegate->OnRefreshKeyRetrieve(sessionHandle->AsSecureSession()->GetPeer(), refreshKeyInfo)) - { - ChipLogError(ICD, "Failed to retrieve a new key for re-registration of the ICD client"); - } - ByteSpan newKey(refreshKeyInfo.newKey); - if (CHIP_NO_ERROR != _this->RegisterClientWithNewKey(refreshKeyInfo.clientInfo, newKey, exchangeMgr, sessionHandle)) - { - ChipLogError(ICD, "Failed to send register client command"); - } -} - -void CheckInHandler::HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err) -{ - CheckInHandler * const _this = static_cast(context); - VerifyOrDie(_this != nullptr); - - ChipLogError(ICD, "Failed to establish CASE for re-registration with error '%" CHIP_ERROR_FORMAT "'", err.Format()); -} - void CheckInHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} } // namespace app diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index 4d520753a680c2..52d8669f716439 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -48,38 +48,6 @@ class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::Uns virtual ~CheckInHandler() = default; - /** - * @brief Callback received on successfully establishing a CASE session in order to re-register the client with the peer node - * using a new key to avoid counter rollover problems. - * - * @param[in] context context of the client establishing the CASE session - * @param[in] exchangeMgr exchange manager to use for the re-registration - * @param[in] sessionHandle session handle to use for the re-registration - */ - static void HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr, - const SessionHandle & sessionHandle); - /** - * @brief Callback received on failure to establish a CASE session in order to re-register the client with the peer node using a - * new key to avoid counter rollover problems. - * - * @param[in] context context of the client establishing the CASE session - * @param[in] peerId Scoped Node ID of the peer node - * @param[in] err failure reason - */ - static void HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err); - - class RegisterCommandSenderCallback : public CommandSender::Callback - { - public: - void OnResponse(chip::app::CommandSender * apCommandSender, const chip::app::ConcreteCommandPath & aPath, - const chip::app::StatusIB & aStatus, chip::TLV::TLVReader * aData) override - {} - void OnError(const chip::app::CommandSender * apCommandSender, CHIP_ERROR aError) override { mError = aError; } - void OnDone(chip::app::CommandSender * apCommandSender) override {} - - CHIP_ERROR mError = CHIP_NO_ERROR; - } registerCommandSenderDelegate; - protected: // ExchangeDelegate CHIP_ERROR @@ -95,28 +63,6 @@ class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::Uns Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return CheckInExchangeDispatch::Instance(); } private: - /** - * @brief Used by the application to set a new key to avoid counter rollover problems. - * - * @param[in] clientInfo clientInfo object - * @param[in] keyData New key data to use to re-register the client with the server - * @param[in] exchangeMgr exchange manager to use for the re-registration - * @param[in] sessionHandle session handle to use for the re-registration - */ - CHIP_ERROR RegisterClientWithNewKey(ICDClientInfo & clientInfo, ByteSpan newKey, Messaging::ExchangeManager & exchangeMgr, - const SessionHandle & sessionHandle); - - /** - * @brief Sets up a CASE session to the peer for re-registering a client with the peer when a key refresh is required to avoid - * ICD counter rollover. Returns error if we did not even manage to kick off a CASE attempt. - * - * @param[in] peerId Node ID of the peer - */ - void EstablishSessionToPeer(ScopedNodeId peerId); - - chip::Callback::Callback mOnConnectedCallback; - chip::Callback::Callback mOnConnectionFailureCallback; - class CheckInExchangeDispatch : public Messaging::ExchangeMessageDispatch { public: diff --git a/src/app/icd/client/DefaultCheckInDelegate.cpp b/src/app/icd/client/DefaultCheckInDelegate.cpp index 9dc2e636c62e2d..33f6631f2563fb 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.cpp +++ b/src/app/icd/client/DefaultCheckInDelegate.cpp @@ -40,24 +40,5 @@ void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo) clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node)); } -void DefaultCheckInDelegate::OnRefreshKeyGenerate(const ICDClientInfo & clientInfo, uint8_t * keyData, uint8_t keyLength) -{ - chip::Crypto::DRBG_get_bytes(keyData, keyLength); - ICDRefreshKeyInfo refreshKeyInfo; - refreshKeyInfo.clientInfo = clientInfo; - memcpy(&refreshKeyInfo.newKey, keyData, keyLength); - icdRefreshKeyMap.insert(make_pair(clientInfo.peer_node, refreshKeyInfo)); -} - -CHIP_ERROR DefaultCheckInDelegate::OnRefreshKeyRetrieve(const ScopedNodeId & nodeId, ICDRefreshKeyInfo & refreshKeyInfo) -{ - // Todo : Check if CHIP_ERROR_KEY_NOT_FOUND can be used here - VerifyOrReturnError(icdRefreshKeyMap.find(nodeId) != icdRefreshKeyMap.end(), CHIP_ERROR_KEY_NOT_FOUND); - refreshKeyInfo = icdRefreshKeyMap.at(nodeId); - return CHIP_NO_ERROR; -} - -void DefaultCheckInDelegate::OnRegistrationComplete(const ICDClientInfo & clientInfo) {} - } // namespace app } // namespace chip diff --git a/src/app/icd/client/DefaultCheckInDelegate.h b/src/app/icd/client/DefaultCheckInDelegate.h index deb784363a5d1e..5e77186b3c7c9d 100644 --- a/src/app/icd/client/DefaultCheckInDelegate.h +++ b/src/app/icd/client/DefaultCheckInDelegate.h @@ -18,10 +18,8 @@ #pragma once -#include "ICDRefreshKeyInfo.h" #include #include -#include //TODO: update check_includes_config.py namespace chip { namespace app { @@ -35,23 +33,9 @@ class DefaultCheckInDelegate : public CheckInDelegate virtual ~DefaultCheckInDelegate() {} CHIP_ERROR Init(ICDClientStorage * storage); void OnCheckInComplete(const ICDClientInfo & clientInfo) override; - void OnRefreshKeyGenerate(const ICDClientInfo & clientInfo, uint8_t * keyData, uint8_t keyLength) override; - CHIP_ERROR OnRefreshKeyRetrieve(const ScopedNodeId & nodeId, ICDRefreshKeyInfo & refreshKeyInfo) override; - void OnRegistrationComplete(const ICDClientInfo & clientInfo) override; - // Hash function for the map : {NodeId, ICDRefreshKeyInfo} - struct HashFunction - { - size_t operator()(const ScopedNodeId & peer) const - { - size_t nodeIdHash = std::hash()(peer.GetNodeId()); - size_t fabricIdxHash = std::hash()(peer.GetFabricIndex()) << 1; - return nodeIdHash ^ fabricIdxHash; - } - }; private: ICDClientStorage * mpStorage = nullptr; - unordered_map icdRefreshKeyMap; }; } // namespace app diff --git a/src/app/icd/client/ICDRefreshKeyInfo.h b/src/app/icd/client/ICDRefreshKeyInfo.h deleted file mode 100644 index 53dc6c506bc75f..00000000000000 --- a/src/app/icd/client/ICDRefreshKeyInfo.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * Copyright (c) 2023 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once -#include "ICDClientInfo.h" -#include - -namespace chip { -namespace app { - -/* - *@brief Structure to hold the clientInfo and the new key generated to re-register with a peer - */ -struct ICDRefreshKeyInfo -{ - ICDClientInfo clientInfo; - uint8_t newKey[chip::Crypto::kAES_CCM128_Key_Length]; - - ICDRefreshKeyInfo() {} - ICDRefreshKeyInfo(const ICDRefreshKeyInfo & other) { *this = other; } - - ICDRefreshKeyInfo & operator=(const ICDRefreshKeyInfo & other) - { - clientInfo = other.clientInfo; - memcpy(newKey, other.newKey, chip::Crypto::kAES_CCM128_Key_Length); - return *this; - } -}; - -} // namespace app -} // namespace chip From b51d58db6cb0927d9e38b0edf628f81d13806022 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 10 Jan 2024 11:26:51 -0800 Subject: [PATCH 93/93] Added a link to an issue --- src/app/icd/client/CheckInHandler.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/icd/client/CheckInHandler.h b/src/app/icd/client/CheckInHandler.h index 52d8669f716439..22170a1b332694 100644 --- a/src/app/icd/client/CheckInHandler.h +++ b/src/app/icd/client/CheckInHandler.h @@ -58,6 +58,7 @@ class CheckInHandler : public Messaging::ExchangeDelegate, public Messaging::Uns CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate + // https://github.com/project-chip/connectedhomeip/issues/31322 void OnResponseTimeout(Messaging::ExchangeContext * ec) override; Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return CheckInExchangeDispatch::Instance(); }