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

Restyle [msg] Authenticate message before counter and duplicate processing. #10103

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/transport/SecureMessageCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ using System::PacketBufferHandle;

namespace SecureMessageCodec {

CHIP_ERROR Encode(Transport::SecureSession * state, PayloadHeader & payloadHeader, PacketHeader & packetHeader,
System::PacketBufferHandle & msgBuf, MessageCounter & counter)
CHIP_ERROR Encrypt(Transport::SecureSession * state, PayloadHeader & payloadHeader, PacketHeader & packetHeader,
System::PacketBufferHandle & msgBuf, MessageCounter & counter)
{
VerifyOrReturnError(!msgBuf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(!msgBuf->HasChainedBuffer(), CHIP_ERROR_INVALID_MESSAGE_LENGTH);
Expand Down Expand Up @@ -72,8 +72,8 @@ CHIP_ERROR Encode(Transport::SecureSession * state, PayloadHeader & payloadHeade
return CHIP_NO_ERROR;
}

CHIP_ERROR Decode(Transport::SecureSession * state, PayloadHeader & payloadHeader, const PacketHeader & packetHeader,
System::PacketBufferHandle & msg)
CHIP_ERROR Decrypt(Transport::SecureSession * state, PayloadHeader & payloadHeader, const PacketHeader & packetHeader,
System::PacketBufferHandle & msg)
{
ReturnErrorCodeIf(msg.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);

Expand Down
8 changes: 4 additions & 4 deletions src/transport/SecureMessageCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ namespace SecureMessageCodec {
* @param counter The local counter object to be used
* @ return CHIP_ERROR The result of the encode operation
*/
CHIP_ERROR Encode(Transport::SecureSession * state, PayloadHeader & payloadHeader, PacketHeader & packetHeader,
System::PacketBufferHandle & msgBuf, MessageCounter & counter);
CHIP_ERROR Encrypt(Transport::SecureSession * state, PayloadHeader & payloadHeader, PacketHeader & packetHeader,
System::PacketBufferHandle & msgBuf, MessageCounter & counter);

/**
* @brief
Expand All @@ -66,8 +66,8 @@ CHIP_ERROR Encode(Transport::SecureSession * state, PayloadHeader & payloadHeade
* unencrypted message.
* @ return CHIP_ERROR The result of the decode operation
*/
CHIP_ERROR Decode(Transport::SecureSession * state, PayloadHeader & payloadHeader, const PacketHeader & packetHeader,
System::PacketBufferHandle & msgBuf);
CHIP_ERROR Decrypt(Transport::SecureSession * state, PayloadHeader & payloadHeader, const PacketHeader & packetHeader,
System::PacketBufferHandle & msgBuf);
} // namespace SecureMessageCodec

} // namespace chip
10 changes: 5 additions & 5 deletions src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ CHIP_ERROR SessionManager::PrepareMessage(SessionHandle session, PayloadHeader &
}

MessageCounter & counter = GetSendCounterForPacket(payloadHeader, *state);
ReturnErrorOnFailure(SecureMessageCodec::Encode(state, payloadHeader, packetHeader, message, counter));
ReturnErrorOnFailure(SecureMessageCodec::Encrypt(state, payloadHeader, packetHeader, message, counter));

#if CHIP_PROGRESS_LOGGING
destination = state->GetPeerNodeId();
Expand Down Expand Up @@ -399,6 +399,10 @@ void SessionManager::SecureMessageDispatch(const PacketHeader & packetHeader, co
ExitNow(err = CHIP_ERROR_KEY_NOT_FOUND_FROM_PEER);
}

// Decrypt and verify the message before message counter verification or any further processing.
VerifyOrExit(CHIP_NO_ERROR == SecureMessageCodec::Decrypt(state, payloadHeader, packetHeader, msg),
ChipLogError(Inet, "Secure transport received message, but failed to decode/authenticate it, discarding"));

// Verify message counter
if (packetHeader.GetFlags().Has(Header::FlagValues::kSecureSessionControlMessage))
{
Expand Down Expand Up @@ -445,10 +449,6 @@ void SessionManager::SecureMessageDispatch(const PacketHeader & packetHeader, co

mPeerConnections.MarkConnectionActive(state);

// Decode the message
VerifyOrExit(CHIP_NO_ERROR == SecureMessageCodec::Decode(state, payloadHeader, packetHeader, msg),
ChipLogError(Inet, "Secure transport received message, but failed to decode it, discarding"));

if (isDuplicate == SessionManagerDelegate::DuplicateMessage::Yes && !payloadHeader.NeedsAck())
{
ChipLogDetail(Inet,
Expand Down