Skip to content

Commit

Permalink
Register to only receive unsolicited Message Counter Synchronization …
Browse files Browse the repository at this point in the history
…Request
  • Loading branch information
yufengwangca committed Feb 17, 2021
1 parent 2428bc8 commit 7bd07d8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/lib/core/CHIPError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ bool FormatCHIPError(char * buf, uint16_t bufSize, int32_t err)
case CHIP_ERROR_RATE_LIMIT_EXCEEDED:
desc = "Rate limit exceeded";
break;
case CHIP_ERROR_SECURITY_MANAGER_BUSY:
desc = "Security manager busy";
case CHIP_ERROR_MCSP_MANAGER_BUSY:
desc = "Message Counter Synchronization manager busy";
break;
case CHIP_ERROR_INVALID_PASE_PARAMETER:
desc = "Invalid PASE parameter";
Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,13 @@ typedef CHIP_CONFIG_ERROR_TYPE CHIP_ERROR;
#define CHIP_ERROR_RATE_LIMIT_EXCEEDED _CHIP_ERROR(54)

/**
* @def CHIP_ERROR_SECURITY_MANAGER_BUSY
* @def CHIP_ERROR_MCSP_MANAGER_BUSY
*
* @brief
* A security manager is busy.
* The Message Counter Synchronization manager is busy.
*
*/
#define CHIP_ERROR_SECURITY_MANAGER_BUSY _CHIP_ERROR(55)
#define CHIP_ERROR_MCSP_MANAGER_BUSY _CHIP_ERROR(55)

/**
* @def CHIP_ERROR_INVALID_PASE_PARAMETER
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/tests/TestCHIPErrorStr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static int32_t sContext[] =
CHIP_ERROR_UNSUPPORTED_DEVICE_DESCRIPTOR_VERSION,
CHIP_END_OF_INPUT,
CHIP_ERROR_RATE_LIMIT_EXCEEDED,
CHIP_ERROR_SECURITY_MANAGER_BUSY,
CHIP_ERROR_MCSP_MANAGER_BUSY,
CHIP_ERROR_INVALID_PASE_PARAMETER,
CHIP_ERROR_PASE_SUPPORTS_ONLY_CONFIG1,
CHIP_ERROR_KEY_CONFIRMATION_FAILED,
Expand Down
14 changes: 10 additions & 4 deletions src/messaging/MessageCounterSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ CHIP_ERROR MessageCounterSyncMgr::Init(Messaging::ExchangeManager * exchangeMgr)
VerifyOrReturnError(exchangeMgr != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
mExchangeMgr = exchangeMgr;

// Register to receive unsolicited Secure Channel Request messages from the exchange manager.
err = mExchangeMgr->RegisterUnsolicitedMessageHandlerForProtocol(Protocols::kProtocol_SecureChannel, this);
// Register to receive unsolicited Message Counter Synchronization Request message from the exchange manager.
err = mExchangeMgr->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq, this);

ReturnErrorOnFailure(err);

Expand All @@ -56,7 +56,7 @@ void MessageCounterSyncMgr::Shutdown()
{
if (mExchangeMgr != nullptr)
{
mExchangeMgr->UnregisterUnsolicitedMessageHandlerForProtocol(Protocols::kProtocol_SecureChannel);
mExchangeMgr->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq);
mExchangeMgr = nullptr;
}
}
Expand All @@ -76,6 +76,8 @@ void MessageCounterSyncMgr::OnMessageReceived(Messaging::ExchangeContext * excha

void MessageCounterSyncMgr::OnResponseTimeout(Messaging::ExchangeContext * exchangeContext)
{
mIsMsgCounterSyncReqInProgress = false;

// Close the exchange if MsgCounterSyncRsp is not received before kMsgCounterSyncTimeout.
if (exchangeContext != nullptr)
exchangeContext->Close();
Expand Down Expand Up @@ -133,6 +135,8 @@ CHIP_ERROR MessageCounterSyncMgr::SendMsgCounterSyncReq(SecureSessionHandle sess
err = exchangeContext->SendMessage(Protocols::SecureChannel::MsgType::MsgCounterSyncReq, std::move(msgBuf), sendFlags);
SuccessOrExit(err);

mIsMsgCounterSyncReqInProgress = true;

exit:
if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -232,6 +236,7 @@ void MessageCounterSyncMgr::HandleMsgCounterSyncResp(Messaging::ExchangeContext
size_t resplen = msgBuf->DataLength();

ChipLogDetail(ExchangeManager, "Received MsgCounterSyncResp response");
mIsMsgCounterSyncReqInProgress = false;

VerifyOrExit(msgBuf->DataLength() == kMsgCounterSyncRespMsgSize, err = CHIP_ERROR_INVALID_MESSAGE_LENGTH);
VerifyOrExit(ChipKeyId::IsAppGroupKey(packetHeader.GetEncryptionKeyID()), err = CHIP_ERROR_WRONG_KEY_TYPE);
Expand All @@ -241,14 +246,15 @@ void MessageCounterSyncMgr::HandleMsgCounterSyncResp(Messaging::ExchangeContext
VerifyOrExit(resplen == kMsgCounterSyncRespMsgSize, err = CHIP_ERROR_INVALID_MESSAGE_LENGTH);

syncCounter = chip::Encoding::LittleEndian::Read32(resp);
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);

memcpy(challenge, resp, kMsgCounterChallengeSize);

// Verify that the response field matches the expected Challenge field for the exchange.
VerifyOrExit(memcmp(exchangeContext->GetChallenge(), challenge, kMsgCounterChallengeSize) == 0,
err = CHIP_ERROR_INVALID_SIGNATURE);

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);

exit:
if (err != CHIP_NO_ERROR)
Expand Down
3 changes: 2 additions & 1 deletion src/messaging/MessageCounterSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ExchangeManager;
class MessageCounterSyncMgr : public Messaging::ExchangeDelegate
{
public:
MessageCounterSyncMgr() : mExchangeMgr(nullptr) {}
MessageCounterSyncMgr() : mExchangeMgr(nullptr), mIsMsgCounterSyncReqInProgress(false) {}

CHIP_ERROR Init(Messaging::ExchangeManager * exchangeMgr);
void Shutdown();
Expand All @@ -55,6 +55,7 @@ class MessageCounterSyncMgr : public Messaging::ExchangeDelegate

private:
Messaging::ExchangeManager * mExchangeMgr; // [READ ONLY] Associated Exchange Manager object.
bool mIsMsgCounterSyncReqInProgress; // Indicate if a peer's message counter synchronization is in progress.

CHIP_ERROR NewMsgCounterSyncExchange(SecureSessionHandle session, Messaging::ExchangeContext *& exchangeContext);

Expand Down

0 comments on commit 7bd07d8

Please sign in to comment.