Skip to content

Commit 0013419

Browse files
committed
Adding Encrypted2 datalen check
1 parent 7d0b4a6 commit 0013419

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/protocols/secure_channel/CASESession.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,9 @@ CHIP_ERROR CASESession::EncodeSigma2(System::PacketBufferHandle & msg_R2, Encode
13561356
// Check if msg_R2_Encrypted is not nullptr
13571357
VerifyOrReturnError(input.msg_R2_Encrypted, CHIP_ERROR_INCORRECT_STATE);
13581358

1359+
// Check if length of msg_R2_Encrypted is set and is at least larger than the MIC length
1360+
VerifyOrReturnError(input.encrypted2Length > CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES, CHIP_ERROR_INCORRECT_STATE);
1361+
13591362
ReturnErrorOnFailure(tlvWriterMsg2.PutBytes(TLV::ContextTag(kTag_Sigma2_Encrypted2), input.msg_R2_Encrypted.Get(),
13601363
static_cast<uint32_t>(input.encrypted2Length)));
13611364

src/protocols/secure_channel/CASESession.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
135135

136136
struct EncodeSigma1Param : Sigma1Param
137137
{
138-
const Crypto::P256PublicKey * pEphPubKey;
139-
const ReliableMessageProtocolConfig * initiatorMrpConfig;
138+
const Crypto::P256PublicKey * pEphPubKey = nullptr;
139+
const ReliableMessageProtocolConfig * initiatorMrpConfig = nullptr;
140140
uint8_t initiatorResume1MIC[Crypto::CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES];
141141
};
142142

@@ -191,9 +191,9 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
191191
{
192192
uint8_t responderRandom[kSigmaParamRandomNumberSize];
193193
uint16_t responderSessionId;
194-
const Crypto::P256PublicKey * pEphPubKey;
194+
const Crypto::P256PublicKey * pEphPubKey = nullptr;
195195
Platform::ScopedMemoryBuffer<uint8_t> msg_R2_Encrypted;
196-
size_t encrypted2Length;
196+
size_t encrypted2Length = 0;
197197
const ReliableMessageProtocolConfig * responderMrpConfig;
198198
};
199199

src/protocols/secure_channel/tests/TestCASESession.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ TEST_F(TestCASESession, EncodeSigma2Test)
981981
System::PacketBufferHandle msg;
982982
CASESession session;
983983
CASESession::EncodeSigma2Param encodeParams;
984+
constexpr uint8_t kEncrypted2datalen = 100U;
984985

985986
EXPECT_EQ(chip::Crypto::DRBG_get_bytes(&encodeParams.responderRandom[0], sizeof(encodeParams.responderRandom)), CHIP_NO_ERROR);
986987
encodeParams.responderSessionId = 7315;
@@ -992,7 +993,8 @@ TEST_F(TestCASESession, EncodeSigma2Test)
992993
encodeParams.pEphPubKey = &EphemeralKey->Pubkey();
993994

994995
// TBEData2Encrypted
995-
encodeParams.msg_R2_Encrypted.Alloc(100);
996+
encodeParams.encrypted2Length = kEncrypted2datalen + CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
997+
encodeParams.msg_R2_Encrypted.Alloc(encodeParams.encrypted2Length);
996998

997999
// responder Session Parameters
9981000
ReliableMessageProtocolConfig MRPConfig = GetDefaultMRPConfig();
@@ -1022,10 +1024,18 @@ TEST_F(TestCASESession, EncodeSigma2Test)
10221024
EXPECT_EQ(CHIP_ERROR_INCORRECT_STATE, session.EncodeSigma2(msg, encodeParams));
10231025
msg = nullptr;
10241026

1025-
encodeParams.msg_R2_Encrypted.Alloc(100);
1027+
encodeParams.msg_R2_Encrypted.Alloc(encodeParams.encrypted2Length);
10261028

10271029
EXPECT_EQ(CHIP_NO_ERROR, session.EncodeSigma2(msg, encodeParams));
10281030

1031+
// EncodeSigma1 should fail when the encrypted2Length is not set
1032+
encodeParams.encrypted2Length = 0;
1033+
EXPECT_EQ(CHIP_ERROR_INCORRECT_STATE, session.EncodeSigma2(msg, encodeParams));
1034+
msg = nullptr;
1035+
1036+
// Set encrypted2Length again
1037+
encodeParams.encrypted2Length = kEncrypted2datalen + CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
1038+
10291039
// EncodeSigma1 should fail when MRP config is missing
10301040
encodeParams.responderMrpConfig = nullptr;
10311041
EXPECT_EQ(CHIP_ERROR_INCORRECT_STATE, session.EncodeSigma2(msg, encodeParams));
@@ -1043,8 +1053,8 @@ struct SessionResumptionTestStorage : SessionResumptionStorage
10431053
{
10441054
SessionResumptionTestStorage(CHIP_ERROR findMethodReturnCode, ScopedNodeId peerNodeId, ResumptionIdStorage * resumptionId,
10451055
Crypto::P256ECDHDerivedSecret * sharedSecret) :
1046-
mFindMethodReturnCode(findMethodReturnCode),
1047-
mPeerNodeId(peerNodeId), mResumptionId(resumptionId), mSharedSecret(sharedSecret)
1056+
mFindMethodReturnCode(findMethodReturnCode), mPeerNodeId(peerNodeId), mResumptionId(resumptionId),
1057+
mSharedSecret(sharedSecret)
10481058
{}
10491059
SessionResumptionTestStorage(CHIP_ERROR findMethodReturnCode) : mFindMethodReturnCode(findMethodReturnCode) {}
10501060
CHIP_ERROR FindByScopedNodeId(const ScopedNodeId & node, ResumptionIdStorage & resumptionId,

0 commit comments

Comments
 (0)