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 47319f6c874659..39c3e9b38c390a 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 @@ -437,10 +438,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 adc8c69113a700..24e05821f3b58f 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -62,7 +62,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 4df2c961260104..ba6f9ea7353ae0 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -89,7 +89,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 358133a42b81b1..c6afac9f9d3e44 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;