@@ -1054,7 +1054,7 @@ CASESession::NextStep CASESession::HandleSigma1(System::PacketBufferHandle && ms
1054
1054
1055
1055
ReturnErrorVariantOnFailure (NextStep, ParseSigma1 (tlvReader, parsedSigma1));
1056
1056
1057
- ChipLogDetail (SecureChannel, " Peer assigned session key ID %d" , parsedSigma1.initiatorSessionId );
1057
+ ChipLogDetail (SecureChannel, " Peer (Initiator) assigned session ID %d" , parsedSigma1.initiatorSessionId );
1058
1058
SetPeerSessionId (parsedSigma1.initiatorSessionId );
1059
1059
1060
1060
// Set the Session parameters provided in the Sigma1 message
@@ -1222,23 +1222,23 @@ CHIP_ERROR CASESession::PrepareSigma2(EncodeSigma2Inputs & outSigma2Data)
1222
1222
ReturnErrorOnFailure (DeriveSigmaKey (saltSpan, ByteSpan (kKDFSR2Info ), sr2k));
1223
1223
1224
1224
// Construct Sigma2 TBS Data
1225
- size_t msgR2SignedLen = EstimateStructOverhead (kMaxCHIPCertLength , // responderNoc
1226
- kMaxCHIPCertLength , // responderICAC
1227
- kP256_PublicKey_Length , // responderEphPubKey
1228
- kP256_PublicKey_Length // InitiatorEphPubKey
1229
- );
1230
-
1231
1225
P256ECDSASignature tbsData2Signature;
1232
1226
{
1227
+ size_t msgR2SignedLen = EstimateStructOverhead (kMaxCHIPCertLength , // responderNoc
1228
+ kMaxCHIPCertLength , // responderICAC
1229
+ kP256_PublicKey_Length , // responderEphPubKey
1230
+ kP256_PublicKey_Length // InitiatorEphPubKey
1231
+ );
1232
+
1233
1233
chip::Platform::ScopedMemoryBuffer<uint8_t > msgR2Signed;
1234
1234
VerifyOrReturnError (msgR2Signed.Alloc (msgR2SignedLen), CHIP_ERROR_NO_MEMORY);
1235
+ MutableByteSpan msgR2SignedSpan{ msgR2Signed.Get (), msgR2SignedLen };
1235
1236
1236
1237
ReturnErrorOnFailure (ConstructTBSData (nocCert, icaCert, ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()),
1237
- ByteSpan (mRemotePubKey , mRemotePubKey .Length ()), msgR2Signed. Get (), msgR2SignedLen ));
1238
+ ByteSpan (mRemotePubKey , mRemotePubKey .Length ()), msgR2SignedSpan ));
1238
1239
1239
1240
// Generate a Signature
1240
- ReturnErrorOnFailure (
1241
- mFabricsTable ->SignWithOpKeypair (mFabricIndex , ByteSpan{ msgR2Signed.Get (), msgR2SignedLen }, tbsData2Signature));
1241
+ ReturnErrorOnFailure (mFabricsTable ->SignWithOpKeypair (mFabricIndex , msgR2SignedSpan, tbsData2Signature));
1242
1242
}
1243
1243
// Construct Sigma2 TBE Data
1244
1244
size_t msgR2SignedEncLen = EstimateStructOverhead (nocCert.size (), // responderNoc
@@ -1381,7 +1381,8 @@ CHIP_ERROR CASESession::HandleSigma2Resume(System::PacketBufferHandle && msg)
1381
1381
GetRemoteSessionParameters ());
1382
1382
}
1383
1383
1384
- ChipLogDetail (SecureChannel, " Peer assigned session ID %d" , parsedSigma2Resume.responderSessionId );
1384
+ ChipLogDetail (SecureChannel, " Peer " ChipLogFormatScopedNodeId " assigned session ID %d" , ChipLogValueScopedNodeId (GetPeer ()),
1385
+ parsedSigma2Resume.responderSessionId );
1385
1386
SetPeerSessionId (parsedSigma2Resume.responderSessionId );
1386
1387
1387
1388
if (mSessionResumptionStorage != nullptr )
@@ -1517,12 +1518,15 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1517
1518
nullptr , 0 , parsedSigma2.msgR2MIC .data (), parsedSigma2.msgR2MIC .size (), sr2k.KeyHandle (),
1518
1519
kTBEData2_Nonce , kTBEDataNonceLength , parsedSigma2.msgR2EncryptedPayload .data ()));
1519
1520
1521
+ parsedSigma2.msgR2Decrypted = std::move (parsedSigma2.msgR2Encrypted );
1522
+ size_t msgR2DecryptedLength = parsedSigma2.msgR2EncryptedPayload .size ();
1523
+
1520
1524
ContiguousBufferTLVReader decryptedDataTlvReader;
1521
- decryptedDataTlvReader.Init (parsedSigma2.msgR2EncryptedPayload . data (), parsedSigma2. msgR2EncryptedPayload . size () );
1525
+ decryptedDataTlvReader.Init (parsedSigma2.msgR2Decrypted . Get (), msgR2DecryptedLength );
1522
1526
ParsedSigma2TBEData parsedSigma2TBEData;
1523
1527
ReturnErrorOnFailure (ParseSigma2TBEData (decryptedDataTlvReader, parsedSigma2TBEData));
1524
1528
1525
- // Validate responder identity located in msgR2Encrypted
1529
+ // Validate responder identity located in msgR2Decrypted
1526
1530
// Constructing responder identity
1527
1531
P256PublicKey responderPublicKey;
1528
1532
{
@@ -1540,7 +1544,7 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1540
1544
VerifyOrReturnError (mPeerNodeId == responderNodeId, CHIP_ERROR_INVALID_CASE_PARAMETER);
1541
1545
}
1542
1546
1543
- // Construct msgR2Signed and validate the signature in msgR2Encrypted .
1547
+ // Construct msgR2Signed and validate the signature in msgR2Decrypted .
1544
1548
size_t msgR2SignedLen = EstimateStructOverhead (parsedSigma2TBEData.responderNOC .size (), // resonderNOC
1545
1549
parsedSigma2TBEData.responderICAC .size (), // responderICAC
1546
1550
kP256_PublicKey_Length , // responderEphPubKey
@@ -1549,16 +1553,18 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1549
1553
1550
1554
chip::Platform::ScopedMemoryBuffer<uint8_t > msgR2Signed;
1551
1555
VerifyOrReturnError (msgR2Signed.Alloc (msgR2SignedLen), CHIP_ERROR_NO_MEMORY);
1556
+ MutableByteSpan msgR2SignedSpan{ msgR2Signed.Get (), msgR2SignedLen };
1552
1557
1553
- ReturnErrorOnFailure (ConstructTBSData (
1554
- parsedSigma2TBEData. responderNOC , parsedSigma2TBEData. responderICAC , ByteSpan (mRemotePubKey , mRemotePubKey .Length ()),
1555
- ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()), msgR2Signed. Get (), msgR2SignedLen ));
1558
+ ReturnErrorOnFailure (ConstructTBSData (parsedSigma2TBEData. responderNOC , parsedSigma2TBEData. responderICAC ,
1559
+ ByteSpan (mRemotePubKey , mRemotePubKey .Length ()),
1560
+ ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()), msgR2SignedSpan ));
1556
1561
1557
1562
// Validate signature
1558
- ReturnErrorOnFailure (
1559
- responderPublicKey. ECDSA_validate_msg_signature (msgR2Signed. Get (), msgR2SignedLen, parsedSigma2TBEData.tbsData2Signature ));
1563
+ ReturnErrorOnFailure (responderPublicKey. ECDSA_validate_msg_signature (msgR2SignedSpan. data (), msgR2SignedSpan. size (),
1564
+ parsedSigma2TBEData.tbsData2Signature ));
1560
1565
1561
- ChipLogDetail (SecureChannel, " Peer assigned session ID %d" , parsedSigma2.responderSessionId );
1566
+ ChipLogDetail (SecureChannel, " Peer " ChipLogFormatScopedNodeId " assigned session ID %d" , ChipLogValueScopedNodeId (GetPeer ()),
1567
+ parsedSigma2.responderSessionId );
1562
1568
SetPeerSessionId (parsedSigma2.responderSessionId );
1563
1569
1564
1570
std::copy (parsedSigma2TBEData.resumptionId .begin (), parsedSigma2TBEData.resumptionId .end (), mNewResumptionId .begin ());
@@ -1728,14 +1734,18 @@ CHIP_ERROR CASESession::SendSigma3a()
1728
1734
ReturnErrorOnFailure (mFabricsTable ->FetchNOCCert (mFabricIndex , data.nocCert ));
1729
1735
1730
1736
// Prepare Sigma3 TBS Data Blob
1731
- data.msg_r3_signed_len =
1732
- EstimateStructOverhead (data.icaCert .size (), data.nocCert .size (), kP256_PublicKey_Length , kP256_PublicKey_Length );
1737
+ size_t msgR3SignedLen = EstimateStructOverhead (data.nocCert .size (), // initiatorNOC
1738
+ data.icaCert .size (), // initiatorICAC
1739
+ kP256_PublicKey_Length , // initiatorEphPubKey
1740
+ kP256_PublicKey_Length // responderEphPubKey
1741
+ );
1733
1742
1734
- VerifyOrReturnError (data.msg_R3_Signed .Alloc (data.msg_r3_signed_len ), CHIP_ERROR_NO_MEMORY);
1743
+ VerifyOrReturnError (data.msgR3Signed .Alloc (msgR3SignedLen), CHIP_ERROR_NO_MEMORY);
1744
+ data.msgR3SignedSpan = MutableByteSpan{ data.msgR3Signed .Get (), msgR3SignedLen };
1735
1745
1736
- ReturnErrorOnFailure (
1737
- ConstructTBSData (data. nocCert , data. icaCert , ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()),
1738
- ByteSpan (mRemotePubKey , mRemotePubKey .Length ()), data.msg_R3_Signed . Get (), data. msg_r3_signed_len ));
1746
+ ReturnErrorOnFailure (ConstructTBSData (data. nocCert , data. icaCert ,
1747
+ ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()),
1748
+ ByteSpan (mRemotePubKey , mRemotePubKey .Length ()), data.msgR3SignedSpan ));
1739
1749
1740
1750
if (data.keystore != nullptr )
1741
1751
{
@@ -1759,14 +1769,12 @@ CHIP_ERROR CASESession::SendSigma3b(SendSigma3Data & data, bool & cancel)
1759
1769
if (data.keystore != nullptr )
1760
1770
{
1761
1771
// Recommended case: delegate to operational keystore
1762
- ReturnErrorOnFailure (data.keystore ->SignWithOpKeypair (
1763
- data.fabricIndex , ByteSpan{ data.msg_R3_Signed .Get (), data.msg_r3_signed_len }, data.tbsData3Signature ));
1772
+ ReturnErrorOnFailure (data.keystore ->SignWithOpKeypair (data.fabricIndex , data.msgR3SignedSpan , data.tbsData3Signature ));
1764
1773
}
1765
1774
else
1766
1775
{
1767
1776
// Legacy case: delegate to fabric table fabric info
1768
- ReturnErrorOnFailure (data.fabricTable ->SignWithOpKeypair (
1769
- data.fabricIndex , ByteSpan{ data.msg_R3_Signed .Get (), data.msg_r3_signed_len }, data.tbsData3Signature ));
1777
+ ReturnErrorOnFailure (data.fabricTable ->SignWithOpKeypair (data.fabricIndex , data.msgR3SignedSpan , data.tbsData3Signature ));
1770
1778
}
1771
1779
1772
1780
// Prepare Sigma3 TBE Data Blob
@@ -1950,17 +1958,18 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
1950
1958
SuccessOrExit (err = ParseSigma3TBEData (decryptedDataTlvReader, data));
1951
1959
1952
1960
// Step 3 - Construct Sigma3 TBS Data
1953
- data. msgR3SignedLen = TLV::EstimateStructOverhead (data.initiatorNOC .size (), // initiatorNOC
1954
- data.initiatorICAC .size (), // initiatorICAC
1955
- kP256_PublicKey_Length , // initiatorEphPubKey
1956
- kP256_PublicKey_Length // responderEphPubKey
1961
+ size_t msgR3SignedLen = TLV::EstimateStructOverhead (data.initiatorNOC .size (), // initiatorNOC
1962
+ data.initiatorICAC .size (), // initiatorICAC
1963
+ kP256_PublicKey_Length , // initiatorEphPubKey
1964
+ kP256_PublicKey_Length // responderEphPubKey
1957
1965
);
1958
1966
1959
- VerifyOrExit (data.msgR3Signed .Alloc (data.msgR3SignedLen ), err = CHIP_ERROR_NO_MEMORY);
1967
+ VerifyOrExit (data.msgR3Signed .Alloc (msgR3SignedLen), err = CHIP_ERROR_NO_MEMORY);
1968
+ data.msgR3SignedSpan = MutableByteSpan{ data.msgR3Signed .Get (), msgR3SignedLen };
1960
1969
1961
1970
SuccessOrExit (err = ConstructTBSData (data.initiatorNOC , data.initiatorICAC , ByteSpan (mRemotePubKey , mRemotePubKey .Length ()),
1962
1971
ByteSpan (mEphemeralKey ->Pubkey (), mEphemeralKey ->Pubkey ().Length ()),
1963
- data.msgR3Signed . Get (), data. msgR3SignedLen ));
1972
+ data.msgR3SignedSpan ));
1964
1973
1965
1974
// Prepare for Step 4/5
1966
1975
{
@@ -1977,9 +1986,9 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
1977
1986
1978
1987
// initiatorNOC and initiatorICAC are spans into msgR3Encrypted
1979
1988
// which is going away, so to save memory, redirect them to their
1980
- // copies in msg_R3_signed , which is staying around
1989
+ // copies in msgR3Signed , which is staying around
1981
1990
TLV::ContiguousBufferTLVReader signedDataTlvReader;
1982
- signedDataTlvReader.Init (data.msgR3Signed . Get (), data. msgR3SignedLen );
1991
+ signedDataTlvReader.Init (data.msgR3SignedSpan );
1983
1992
SuccessOrExit (err = signedDataTlvReader.Next (containerType, AnonymousTag ()));
1984
1993
SuccessOrExit (err = signedDataTlvReader.EnterContainer (containerType));
1985
1994
@@ -2089,14 +2098,9 @@ CHIP_ERROR CASESession::HandleSigma3b(HandleSigma3Data & data, bool & cancel)
2089
2098
unused, initiatorFabricId, data.initiatorNodeId , initiatorPublicKey));
2090
2099
VerifyOrReturnError (data.fabricId == initiatorFabricId, CHIP_ERROR_INVALID_CASE_PARAMETER);
2091
2100
2092
- // TODO - Validate message signature prior to validating the received operational credentials.
2093
- // The op cert check requires traversal of cert chain, that is a more expensive operation.
2094
- // If message signature check fails, the cert chain check will be unnecessary, but with the
2095
- // current flow of code, a malicious node can trigger a DoS style attack on the device.
2096
- // The same change should be made in Sigma2 processing.
2097
2101
// Step 7 - Validate Signature
2098
- ReturnErrorOnFailure (
2099
- initiatorPublicKey. ECDSA_validate_msg_signature (data. msgR3Signed . Get (), data. msgR3SignedLen , data.tbsData3Signature ));
2102
+ ReturnErrorOnFailure (initiatorPublicKey. ECDSA_validate_msg_signature (data. msgR3SignedSpan . data (), data. msgR3SignedSpan . size (),
2103
+ data.tbsData3Signature ));
2100
2104
2101
2105
return CHIP_NO_ERROR;
2102
2106
}
@@ -2241,12 +2245,12 @@ CHIP_ERROR CASESession::ValidateSigmaResumeMIC(const ByteSpan & resumeMIC, const
2241
2245
}
2242
2246
2243
2247
CHIP_ERROR CASESession::ConstructTBSData (const ByteSpan & senderNOC, const ByteSpan & senderICAC, const ByteSpan & senderPubKey,
2244
- const ByteSpan & receiverPubKey, uint8_t * tbsData, size_t & tbsDataLen )
2248
+ const ByteSpan & receiverPubKey, MutableByteSpan & outTbsData )
2245
2249
{
2246
2250
TLVWriter tlvWriter;
2247
2251
TLVType outerContainerType = kTLVType_NotSpecified ;
2248
2252
2249
- tlvWriter.Init (tbsData, tbsDataLen );
2253
+ tlvWriter.Init (outTbsData );
2250
2254
ReturnErrorOnFailure (tlvWriter.StartContainer (AnonymousTag (), kTLVType_Structure , outerContainerType));
2251
2255
ReturnErrorOnFailure (tlvWriter.Put (AsTlvContextTag (TBSDataTags::kSenderNOC ), senderNOC));
2252
2256
if (!senderICAC.empty ())
@@ -2257,7 +2261,7 @@ CHIP_ERROR CASESession::ConstructTBSData(const ByteSpan & senderNOC, const ByteS
2257
2261
ReturnErrorOnFailure (tlvWriter.Put (AsTlvContextTag (TBSDataTags::kReceiverPubKey ), receiverPubKey));
2258
2262
ReturnErrorOnFailure (tlvWriter.EndContainer (outerContainerType));
2259
2263
ReturnErrorOnFailure (tlvWriter.Finalize ());
2260
- tbsDataLen = static_cast <size_t >(tlvWriter.GetLengthWritten ());
2264
+ outTbsData. reduce_size ( static_cast <size_t >(tlvWriter.GetLengthWritten () ));
2261
2265
2262
2266
return CHIP_NO_ERROR;
2263
2267
}
0 commit comments