Skip to content

Commit

Permalink
[Group] Add Incoming Group message Dispatch (project-chip#10985)
Browse files Browse the repository at this point in the history
* Add path for group message processing

* fix styling
  • Loading branch information
jepenven-silabs authored and carol-apple committed Oct 28, 2021
1 parent 8c630fb commit dd8cda9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/credentials/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static_library("credentials") {
"${chip_root}/src/lib/asn1",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
"${chip_root}/src/transport/raw",
"${nlassert_root}:nlassert",
]
}
4 changes: 4 additions & 0 deletions src/credentials/GroupDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <lib/core/CHIPError.h>
#include <stdint.h>
#include <sys/types.h>
#include <transport/raw/MessageHeader.h>

namespace chip {
namespace Credentials {
Expand Down Expand Up @@ -198,6 +199,9 @@ class GroupDataProvider
// Fabrics
virtual CHIP_ERROR RemoveFabric(chip::FabricIndex fabric_index) = 0;

// General
virtual CHIP_ERROR Decrypt(PacketHeader packetHeader, PayloadHeader & payloadHeader, System::PacketBufferHandle && msg) = 0;

// Listener
void SetListener(GroupListener * listener) { mListener = listener; };
void RemoveListener() { mListener = nullptr; };
Expand Down
7 changes: 7 additions & 0 deletions src/credentials/examples/GroupDataProviderExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,13 @@ class StaticGroupsProvider : public GroupDataProvider
return CHIP_NO_ERROR;
}

CHIP_ERROR Decrypt(PacketHeader packetHeader, PayloadHeader & payloadHeader, System::PacketBufferHandle && msg) override
{
// TODO

return CHIP_NO_ERROR;
}

private:
bool mInitialized = false;
Fabric mFabrics[kNumFabrics];
Expand Down
69 changes: 66 additions & 3 deletions src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <string.h>

#include <app/util/basic-types.h>
#include <credentials/GroupDataProvider.h>
#include <lib/core/CHIPKeyIds.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
Expand Down Expand Up @@ -328,7 +329,14 @@ void SessionManager::OnMessageReceived(const PeerAddress & peerAddress, System::

if (packetHeader.IsEncrypted())
{
SecureMessageDispatch(packetHeader, peerAddress, std::move(msg));
if (packetHeader.IsGroupSession())
{
SecureGroupMessageDispatch(packetHeader, peerAddress, std::move(msg));
}
else
{
SecureUnicastMessageDispatch(packetHeader, peerAddress, std::move(msg));
}
}
else
{
Expand Down Expand Up @@ -379,8 +387,8 @@ void SessionManager::MessageDispatch(const PacketHeader & packetHeader, const Tr
}
}

void SessionManager::SecureMessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg)
void SessionManager::SecureUnicastMessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg)
{
CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -493,6 +501,61 @@ void SessionManager::SecureMessageDispatch(const PacketHeader & packetHeader, co
}
}

void SessionManager::SecureGroupMessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg)
{
CHIP_ERROR err = CHIP_NO_ERROR;
PayloadHeader payloadHeader;
SessionManagerDelegate::DuplicateMessage isDuplicate = SessionManagerDelegate::DuplicateMessage::No;
// Credentials::GroupDataProvider * groups = Credentials::GetGroupDataProvider();

VerifyOrExit(!msg.IsNull(), ChipLogError(Inet, "Secure transport received NULL packet, discarding"));

// TODO: Handle Group message counter here spec 4.7.3
// spec 4.5.1.2 for msg counter

// Trial decryption with GroupDataProvider. TODO: Implement the GroupDataProvider Class
// VerifyOrExit(CHIP_NO_ERROR == groups->DecryptMessage(packetHeader, payloadHeader, msg),
// ChipLogError(Inet, "Secure transport received group message, but failed to decode it, discarding"));

if (isDuplicate == SessionManagerDelegate::DuplicateMessage::Yes && !payloadHeader.NeedsAck())
{
ChipLogDetail(Inet,
"Received a duplicate message with MessageCounter:" ChipLogFormatMessageCounter
" on exchange " ChipLogFormatExchangeId,
packetHeader.GetMessageCounter(), ChipLogValueExchangeIdFromSentHeader(payloadHeader));
if (!payloadHeader.NeedsAck())
{
// If it's a duplicate message, but doesn't require an ack, let's drop it right here to save CPU
// cycles on further message processing.
ExitNow(err = CHIP_NO_ERROR);
}
}

if (packetHeader.IsSecureSessionControlMsg())
{
// TODO: control message counter is not implemented yet
}
else
{
// TODO: Commit Group Message Counter
}

if (mCB != nullptr)
{
// TODO: Update Session Handle for Group messages.
// SessionHandle session(state->GetPeerNodeId(), state->GetLocalSessionId(), state->GetPeerSessionId(),
// state->GetFabricIndex());
// mCB->OnMessageReceived(packetHeader, payloadHeader, nullptr, peerAddress, isDuplicate, std::move(msg));
}

exit:
if (err != CHIP_NO_ERROR && mCB != nullptr)
{
mCB->OnReceiveError(err, peerAddress);
}
}

void SessionManager::HandleConnectionExpired(const Transport::SecureSession & state)
{
ChipLogDetail(Inet, "Marking old secure session for device 0x" ChipLogFormatX64 " as expired",
Expand Down
8 changes: 6 additions & 2 deletions src/transport/SessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,12 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate
*/
static void ExpiryTimerCallback(System::Layer * layer, void * param);

void SecureMessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg);
void SecureUnicastMessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg);

void SecureGroupMessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg);

void MessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg);

Expand Down

0 comments on commit dd8cda9

Please sign in to comment.