@@ -1265,7 +1265,7 @@ CHIP_ERROR CASESession::PrepareSigma2(EncodeSigma2Inputs & outSigma2Data)
1265
1265
size_t msgR2SignedLen = EstimateStructOverhead (kMaxCHIPCertLength , // responderNoc
1266
1266
kMaxCHIPCertLength , // responderICAC
1267
1267
kP256_PublicKey_Length , // responderEphPubKey
1268
- kP256_PublicKey_Length // InitiatorEphPubKey
1268
+ kP256_PublicKey_Length // initiatorEphPubKey
1269
1269
);
1270
1270
1271
1271
P256ECDSASignature tbsData2Signature;
@@ -1494,7 +1494,7 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1494
1494
1495
1495
chip::Platform::ScopedMemoryBuffer<uint8_t > msg_R2_Encrypted;
1496
1496
1497
- chip::Platform::ScopedMemoryBuffer<uint8_t > msg_R2_Signed ;
1497
+ chip::Platform::ScopedMemoryBuffer<uint8_t > msgR2Signed ;
1498
1498
1499
1499
AutoReleaseSessionKey sr2k (*mSessionManager ->GetSessionKeystore ());
1500
1500
@@ -1510,9 +1510,9 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1510
1510
// uint16_t responderSessionId;
1511
1511
1512
1512
// TLVType containerType = kTLVType_Structure;
1513
- size_t msg_r2_signed_len ;
1513
+ size_t msgR2SignedLen ;
1514
1514
1515
- size_t msg_r2_encrypted_len = 0 ;
1515
+ size_t msgR2EncryptedLen = 0 ;
1516
1516
1517
1517
ChipLogProgress (SecureChannel, " Received Sigma2 msg" );
1518
1518
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -1534,7 +1534,7 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1534
1534
VerifyOrExit (mEphemeralKey != nullptr , err = CHIP_ERROR_INTERNAL);
1535
1535
VerifyOrExit (buf != nullptr , err = CHIP_ERROR_MESSAGE_INCOMPLETE);
1536
1536
1537
- ParseSigma2 (tlvReader, parsedSigma2);
1537
+ SuccessOrExit (err = ParseSigma2 (tlvReader, parsedSigma2) );
1538
1538
1539
1539
ChipLogDetail (SecureChannel, " Peer assigned session key ID %d" , parsedSigma2.responderSessionId );
1540
1540
SetPeerSessionId (parsedSigma2.responderSessionId );
@@ -1553,8 +1553,7 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1553
1553
err = ConstructSaltSigma2 (ByteSpan (parsedSigma2.responderRandom ), mRemotePubKey , ByteSpan (mIPK ), saltSpan));
1554
1554
ReturnErrorOnFailure (err = DeriveSigmaKey (saltSpan, ByteSpan (kKDFSR2Info ), sr2k));
1555
1555
}
1556
- // TODO verify location of this
1557
- // TODO why does this trigger a fialure if i move it just after call to PArseSigma2
1556
+ // Msg2 should only be added to MessageDigest after we construct SaltSigma2 that is used to derive S2K
1558
1557
ReturnErrorOnFailure (err = mCommissioningHash .AddData (ByteSpan{ buf, buflen }));
1559
1558
1560
1559
if (parsedSigma2.responderMrpParamsPresent )
@@ -1566,19 +1565,19 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1566
1565
/* *********************************************************************************************************************************
1567
1566
*/
1568
1567
1569
- msg_r2_encrypted_len = parsedSigma2.msgR2Encrypted .AllocatedSize () - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
1568
+ msgR2EncryptedLen = parsedSigma2.msgR2Encrypted .AllocatedSize () - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
1570
1569
1571
- SuccessOrExit (err = AES_CCM_decrypt (parsedSigma2.msgR2Encrypted .Get (), msg_r2_encrypted_len , nullptr , 0 ,
1572
- parsedSigma2.msgR2Encrypted .Get () + msg_r2_encrypted_len , CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES,
1570
+ SuccessOrExit (err = AES_CCM_decrypt (parsedSigma2.msgR2Encrypted .Get (), msgR2EncryptedLen , nullptr , 0 ,
1571
+ parsedSigma2.msgR2Encrypted .Get () + msgR2EncryptedLen , CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES,
1573
1572
sr2k.KeyHandle (), kTBEData2_Nonce , kTBEDataNonceLength , parsedSigma2.msgR2Encrypted .Get ()));
1574
1573
1575
- decryptedDataTlvReader.Init (parsedSigma2.msgR2Encrypted .Get (), msg_r2_encrypted_len );
1574
+ decryptedDataTlvReader.Init (parsedSigma2.msgR2Encrypted .Get (), msgR2EncryptedLen );
1576
1575
1577
- ParseSigma2TBEData (decryptedDataTlvReader, parsedSigma2TBEData);
1576
+ SuccessOrExit (err = ParseSigma2TBEData (decryptedDataTlvReader, parsedSigma2TBEData) );
1578
1577
1579
1578
std::copy (parsedSigma2TBEData.resumptionId .begin (), parsedSigma2TBEData.resumptionId .end (), mNewResumptionId .begin ());
1580
1579
1581
- // Validate responder identity located in msg_r2_encrypted
1580
+ // Validate responder identity located in msgR2Encrypted
1582
1581
// Constructing responder identity
1583
1582
{
1584
1583
CompressedFabricId unused;
@@ -1593,20 +1592,20 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1593
1592
VerifyOrExit (mPeerNodeId == responderNodeId, err = CHIP_ERROR_INVALID_CASE_PARAMETER);
1594
1593
}
1595
1594
1596
- // Construct msg_R2_Signed and validate the signature in msg_r2_encrypted
1597
- msg_r2_signed_len =
1595
+ // Construct msgR2Signed and validate the signature in msgR2Encrypted.
1596
+ msgR2SignedLen =
1598
1597
EstimateStructOverhead (sizeof (uint16_t ), parsedSigma2TBEData.responderNOC .size (), parsedSigma2TBEData.responderICAC .size (),
1599
1598
kP256_PublicKey_Length , kP256_PublicKey_Length );
1600
1599
1601
- VerifyOrExit (msg_R2_Signed .Alloc (msg_r2_signed_len ), err = CHIP_ERROR_NO_MEMORY);
1600
+ VerifyOrExit (msgR2Signed .Alloc (msgR2SignedLen ), err = CHIP_ERROR_NO_MEMORY);
1602
1601
1603
1602
SuccessOrExit (err = ConstructTBSData (parsedSigma2TBEData.responderNOC , parsedSigma2TBEData.responderICAC ,
1604
1603
ByteSpan (mRemotePubKey , mRemotePubKey .Length ()),
1605
- ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()), msg_R2_Signed .Get (),
1606
- msg_r2_signed_len ));
1604
+ ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()), msgR2Signed .Get (),
1605
+ msgR2SignedLen ));
1607
1606
1608
1607
// Validate signature
1609
- SuccessOrExit (err = responderPublicKey.ECDSA_validate_msg_signature (msg_R2_Signed .Get (), msg_r2_signed_len ,
1608
+ SuccessOrExit (err = responderPublicKey.ECDSA_validate_msg_signature (msgR2Signed .Get (), msgR2SignedLen ,
1610
1609
parsedSigma2TBEData.tbsData2Signature ));
1611
1610
1612
1611
// Retrieve peer CASE Authenticated Tags (CATs) from peer's NOC.
@@ -1631,12 +1630,15 @@ CHIP_ERROR CASESession::ParseSigma2TBEData(ContiguousBufferTLVReader & decrypted
1631
1630
1632
1631
ReturnErrorOnFailure (err = decryptedDataTlvReader.Next (kTLVType_ByteString , AsTlvContextTag (TBEDataTags::kSenderNOC )));
1633
1632
ReturnErrorOnFailure (err = decryptedDataTlvReader.GetByteView (outParsedSigma2TBE.responderNOC ));
1633
+ VerifyOrReturnError (outParsedSigma2TBE.responderNOC .size () <= kMaxCHIPCertLength , CHIP_ERROR_INVALID_CASE_PARAMETER);
1634
1634
1635
1635
ReturnErrorOnFailure (err = decryptedDataTlvReader.Next ());
1636
1636
if (decryptedDataTlvReader.GetTag () == AsTlvContextTag (TBEDataTags::kSenderICAC ))
1637
1637
{
1638
1638
VerifyOrReturnError (decryptedDataTlvReader.GetType () == kTLVType_ByteString , err = CHIP_ERROR_WRONG_TLV_TYPE);
1639
1639
ReturnErrorOnFailure (err = decryptedDataTlvReader.GetByteView (outParsedSigma2TBE.responderICAC ));
1640
+ VerifyOrReturnError (outParsedSigma2TBE.responderICAC .size () <= kMaxCHIPCertLength , CHIP_ERROR_INVALID_CASE_PARAMETER);
1641
+
1640
1642
ReturnErrorOnFailure (err = decryptedDataTlvReader.Next (kTLVType_ByteString , AsTlvContextTag (TBEDataTags::kSignature )));
1641
1643
}
1642
1644
@@ -1651,6 +1653,8 @@ CHIP_ERROR CASESession::ParseSigma2TBEData(ContiguousBufferTLVReader & decrypted
1651
1653
// Retrieve session resumption ID
1652
1654
ReturnErrorOnFailure (err = decryptedDataTlvReader.Next (kTLVType_ByteString , AsTlvContextTag (TBEDataTags::kResumptionID )));
1653
1655
ReturnErrorOnFailure (err = decryptedDataTlvReader.GetByteView (outParsedSigma2TBE.resumptionId ));
1656
+ VerifyOrReturnError (outParsedSigma2TBE.resumptionId .size () == SessionResumptionStorage::kResumptionIdSize ,
1657
+ CHIP_ERROR_INVALID_CASE_PARAMETER);
1654
1658
1655
1659
return CHIP_NO_ERROR;
1656
1660
}
@@ -2412,30 +2416,30 @@ CHIP_ERROR CASESession::ParseSigma2(ContiguousBufferTLVReader & tlvReader, Parse
2412
2416
ReturnErrorOnFailure (err = tlvReader.Next (kTLVType_ByteString , AsTlvContextTag (Sigma2Tags::kEncrypted2 )));
2413
2417
2414
2418
// TODO find a solution to this
2415
- // size_t msg_r2_encrypted_len = 0;
2419
+ // size_t msgR2EncryptedLen = 0;
2416
2420
2417
- size_t msg_r2_encrypted_len_with_tag = 0 ;
2421
+ size_t msgR2EncryptedLenWithTag = 0 ;
2418
2422
2419
2423
constexpr size_t kCaseOverheadForFutureTbeData = 128 ;
2420
2424
2421
- size_t max_msg_r2_signed_enc_len ;
2425
+ size_t maxMsgR2SignedEncLen ;
2422
2426
2423
- max_msg_r2_signed_enc_len =
2424
- EstimateStructOverhead (Credentials::kMaxCHIPCertLength , Credentials::kMaxCHIPCertLength , tbsData2Signature.Length (),
2425
- SessionResumptionStorage::kResumptionIdSize , kCaseOverheadForFutureTbeData );
2426
- msg_r2_encrypted_len_with_tag = tlvReader.GetLength ();
2427
+ maxMsgR2SignedEncLen = EstimateStructOverhead (kMaxCHIPCertLength , kMaxCHIPCertLength , tbsData2Signature.Length (),
2428
+ SessionResumptionStorage::kResumptionIdSize , kCaseOverheadForFutureTbeData );
2429
+ msgR2EncryptedLenWithTag = tlvReader.GetLength ();
2427
2430
2428
2431
// Validate we did not receive a buffer larger than legal
2429
2432
// TODO why are comparing this to the "signed one"
2430
2433
// Maybe it is signed encrypted, but is the estimation calculated well? document it
2431
- VerifyOrReturnError (msg_r2_encrypted_len_with_tag <= max_msg_r2_signed_enc_len, err = CHIP_ERROR_INVALID_TLV_ELEMENT);
2432
- VerifyOrReturnError (msg_r2_encrypted_len_with_tag > CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES, err = CHIP_ERROR_INVALID_TLV_ELEMENT);
2433
- VerifyOrReturnError (outParsedSigma2.msgR2Encrypted .Alloc (msg_r2_encrypted_len_with_tag), err = CHIP_ERROR_NO_MEMORY);
2434
+ VerifyOrReturnError (msgR2EncryptedLenWithTag <= maxMsgR2SignedEncLen, err = CHIP_ERROR_INVALID_TLV_ELEMENT);
2435
+ VerifyOrReturnError (msgR2EncryptedLenWithTag > CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES, err = CHIP_ERROR_INVALID_TLV_ELEMENT);
2436
+ // Allocate after making sure that msgR2EncryptedLenWithTag is within bounds
2437
+ VerifyOrReturnError (outParsedSigma2.msgR2Encrypted .Alloc (msgR2EncryptedLenWithTag), err = CHIP_ERROR_NO_MEMORY);
2434
2438
2435
2439
// TODO, should I keep this as GetBytes? or should I use GetByteView for consistency and do something else?
2436
2440
ReturnErrorOnFailure (
2437
2441
err = tlvReader.GetBytes (outParsedSigma2.msgR2Encrypted .Get (), outParsedSigma2.msgR2Encrypted .AllocatedSize ()));
2438
- // msg_r2_encrypted_len = msg_r2_encrypted_len_with_tag - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
2442
+ // msgR2EncryptedLen = msgR2EncryptedLenWithTag - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
2439
2443
2440
2444
// Retrieve responderMRPParams if present
2441
2445
if (tlvReader.Next () != CHIP_END_OF_TLV)
0 commit comments