Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Message Counter Synchronization Protocol part #4712

Merged
merged 3 commits into from
Feb 17, 2021
Merged

Implement Message Counter Synchronization Protocol part #4712

merged 3 commits into from
Feb 17, 2021

Conversation

yufengwangca
Copy link
Contributor

Problem

Peer message counter synchronization is an essential part of enabling secure CHIP messaging between members of an application group.

Summary of Changes

Implement the protocol used to synchronize message counters between peers when using the same symmetric group key.

This PR does not handle the Initiating Message Counter Synchronization part which is depending on the following issues:
#4628
#4571

Fixes #4626

@todo
Copy link

todo bot commented Feb 8, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenage[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 17fd7cd in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4641) We shall also set the C flag in the packet header, this will be used for message protection later.

// TODO:(#4641) We shall also set the C flag in the packet header, this will be used for message protection later.
sendFlags.Set(Messaging::SendMessageFlags::kNoAutoRequestAck, true).Set(Messaging::SendMessageFlags::kExpectResponse, true);
// Arm a timer to enforce that a MsgCounterSyncRsp is received before kMsgCounterSyncTimeout.
ec->SetResponseTimeout(kMsgCounterSyncTimeout);
// Send the message counter synchronization request in a Secure Channel Protocol::MsgCounterSyncReq message.
err = ec->SendMessage(Protocols::SecureChannel::MsgType::MsgCounterSyncReq, std::move(msgBuf), sendFlags);
SuccessOrExit(err);
exit:


This comment was generated by todo based on a TODO comment in 17fd7cd in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (ec != nullptr)
ec->Close();


This comment was generated by todo based on a ToDo comment in 17fd7cd in #4712. cc @yufengwangca.

@yufengwangca yufengwangca changed the title Implement Message Counter Synchronization Protocol (MCSP) part Implement Message Counter Synchronization Protocol part Feb 8, 2021
src/messaging/ReliableMessageMgr.cpp Outdated Show resolved Hide resolved
@@ -0,0 +1,269 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove copyright line (years are annoying to update).

@woody-apple - is this ok with you? I find updating years tedious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer a separate PR to update the Copyright header part unanimously after we finalize what should be kept and what should be removed.


/**
* @file
* This file implements the CHIP Secure Channel protocol.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this @file ?

@@ -0,0 +1,272 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If keeping this should be just 2021 probably.

However I would still remove both Copyright and All rights reserved lines.


/**
* @file
* This file implements unit tests for the ExchangeManager implementation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop this @file comment: generally I have not found any of these @file comments valuable when reading code and they seem to have a high probability of being copy & pasted - like this one I believe it should not be ExchangeManager but rather SecureChannelManager.

A comment saying that 'TestSecureChannelMgr.cpp implements unit tests for SecureChannelManager' seems redundant.

@todo
Copy link

todo bot commented Feb 8, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenage[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 492f55a in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (ec != nullptr)
ec->Close();


This comment was generated by todo based on a ToDo comment in 492f55a in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenage[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 7e5a1bb in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (ec != nullptr)
ec->Close();


This comment was generated by todo based on a ToDo comment in 7e5a1bb in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenage[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 16fd9bb in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (ec != nullptr)
ec->Close();


This comment was generated by todo based on a ToDo comment in 16fd9bb in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 1db754e in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (ec != nullptr)
ec->Close();


This comment was generated by todo based on a ToDo comment in 1db754e in #4712. cc @yufengwangca.

src/messaging/SecureChannelMgr.cpp Outdated Show resolved Hide resolved
src/messaging/SecureChannelMgr.cpp Outdated Show resolved Hide resolved
@todo
Copy link

todo bot commented Feb 8, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in b949ac5 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 8, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in b949ac5 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 9, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 2703eab in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 9, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 2703eab in #4712. cc @yufengwangca.

@woody-apple
Copy link
Contributor

@pan-apple ?

@todo
Copy link

todo bot commented Feb 11, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 1d4d7d5 in #4712. cc @yufengwangca.

@yufengwangca
Copy link
Contributor Author

@yufengwangca , the file name SecureChannelMgr seems to specifically implement MessageCounterSync protocol. The current file name is confusing, given we already have SecureSessionMgr class in the code base. How do you feel about renaming SecureChannelMgr.cpp/.h to MessageCounterSync.cpp/.h?

Acked, SecureChannelMgr was originally designed to run MCSP and CASE protocols, since we have figured out how to implement multiple CASE sessions using ephemeral TCP ports within Transport layer, we can rename SecureChannelMgr to MessageCounterSyncMgr

@todo
Copy link

todo bot commented Feb 11, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 187aad2 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 11, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 187aad2 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 12, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 11dc138 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 12, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 11dc138 in #4712. cc @yufengwangca.

@yufengwangca
Copy link
Contributor Author

@pan-apple @woody-apple ?

Copy link
Contributor

@pan-apple pan-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yufengwangca , there are some minor comments. Once those are addressed, it looks good to me.

src/messaging/ExchangeMgr.h Outdated Show resolved Hide resolved
src/messaging/MessageCounterSync.cpp Outdated Show resolved Hide resolved
@@ -33,6 +33,9 @@
#include <transport/SecureSessionMgr.h>

namespace chip {

constexpr uint16_t kMsgCounterChallengeSize = 8; // The size of the message counter synchronization request message.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to MessageCounterSync.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause mutual including during compiling.

The mChallenge should not be declared in ExchangeContext.h, it will be moved to MessageCounterSync.h after:
// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual actions with a delegate pattern.

@todo
Copy link

todo bot commented Feb 16, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 979fa5d in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 16, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 979fa5d in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 16, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 2428bc8 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 16, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 2428bc8 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 17, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 7bd07d8 in #4712. cc @yufengwangca.

@boring-cyborg boring-cyborg bot added the lib label Feb 17, 2021
@todo
Copy link

todo bot commented Feb 17, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 7bd07d8 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 17, 2021

#4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual

// [TODO: #4711]: this field need to be moved to appState object which implement 'exchange-specific' contextual
// actions with a delegate pattern.
uint8_t mChallenge[kMsgCounterChallengeSize]; // Challenge number to identify the sychronization request cryptographically.
BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags
/**


This comment was generated by todo based on a TODO comment in 2428bc8 in #4712. cc @yufengwangca.

@todo
Copy link

todo bot commented Feb 17, 2021

(#4628)Initialize/synchronize peer's message counter to FabricState.

// ToDo:(#4628)Initialize/synchronize peer's message counter to FabricState.
VerifyOrExit(syncCounter != 0, err = CHIP_ERROR_READ_FAILED);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to handle MsgCounterSyncResp message with error:%s", ErrorStr(err));
}
if (exchangeContext != nullptr)
exchangeContext->Close();


This comment was generated by todo based on a ToDo comment in 2428bc8 in #4712. cc @yufengwangca.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Message Counter Synchronization Protocol (MCSP).
7 participants