Skip to content

Commit eca5000

Browse files
committed
Using a Variant for the NextStep logic in HandleSigma1, to either send a Sigma message or a status report
1 parent bd0252a commit eca5000

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/lib/support/CodeUtils.h

+10
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@
156156
} \
157157
} while (false)
158158

159+
#define ReturnErrorVariantOnFailure(Variant, expr) \
160+
do \
161+
{ \
162+
auto __err = (expr); \
163+
if (!::chip::ChipError::IsSuccess(__err)) \
164+
{ \
165+
return Variant::Create<CHIP_ERROR>(__err); \
166+
} \
167+
} while (false)
168+
159169
/**
160170
* @def ReturnLogErrorOnFailure(expr)
161171
*

src/protocols/secure_channel/CASESession.cpp

+14-21
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,12 @@ CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle &
946946
MATTER_TRACE_SCOPE("HandleSigma1_and_SendSigma2", "CASESession");
947947

948948
CHIP_ERROR err = CHIP_NO_ERROR;
949-
Step nextStep = Step::kNone;
950949

951950
// Parse and Validate Received Sigma1, and decide next step
952-
SuccessOrExit(err = HandleSigma1(std::move(msg), nextStep));
951+
NextStep nextStep = HandleSigma1(std::move(msg));
952+
VerifyOrExit(nextStep.Is<Step>(), err = nextStep.Get<CHIP_ERROR>());
953953

954-
switch (nextStep)
954+
switch (nextStep.Get<Step>())
955955
{
956956
case Step::kSendSigma2: {
957957

@@ -982,15 +982,11 @@ CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle &
982982
mDelegate->OnSessionEstablishmentStarted();
983983
break;
984984
}
985-
// TODO should I keep this?
986-
case Step::kSendStatusReport:
987985
default:
988-
ExitNow();
989986
break;
990987
}
991988

992989
exit:
993-
994990
if (err == CHIP_ERROR_KEY_NOT_FOUND)
995991
{
996992
SendStatusReport(mExchangeCtxt, kProtocolCodeNoSharedRoot);
@@ -1082,23 +1078,23 @@ CHIP_ERROR CASESession::TryResumeSession(SessionResumptionStorage::ConstResumpti
10821078

10831079
return CHIP_NO_ERROR;
10841080
}
1085-
CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg, Step & nextStep)
1081+
CASESession::NextStep CASESession::HandleSigma1(System::PacketBufferHandle && msg)
10861082
{
10871083
MATTER_TRACE_SCOPE("HandleSigma1", "CASESession");
10881084
ChipLogProgress(SecureChannel, "Received Sigma1 msg");
10891085
MATTER_TRACE_COUNTER("Sigma1");
10901086

1091-
VerifyOrReturnError(mFabricsTable != nullptr, CHIP_ERROR_INCORRECT_STATE);
1087+
VerifyOrReturnError(mFabricsTable != nullptr, NextStep::Create<CHIP_ERROR>(CHIP_ERROR_INCORRECT_STATE));
10921088

1093-
ReturnErrorOnFailure(mCommissioningHash.AddData(ByteSpan{ msg->Start(), msg->DataLength() }));
1089+
ReturnErrorVariantOnFailure(NextStep, mCommissioningHash.AddData(ByteSpan{ msg->Start(), msg->DataLength() }));
10941090

10951091
System::PacketBufferTLVReader tlvReader;
10961092
tlvReader.Init(std::move(msg));
10971093

10981094
// Struct that will serve as output in ParseSigma1
10991095
ParsedSigma1 parsedSigma1;
11001096

1101-
ReturnErrorOnFailure(ParseSigma1(tlvReader, parsedSigma1));
1097+
ReturnErrorVariantOnFailure(NextStep, ParseSigma1(tlvReader, parsedSigma1));
11021098

11031099
ChipLogDetail(SecureChannel, "Peer assigned session key ID %d", parsedSigma1.initiatorSessionId);
11041100
SetPeerSessionId(parsedSigma1.initiatorSessionId);
@@ -1119,12 +1115,9 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg, Step & n
11191115
std::copy(parsedSigma1.initiatorRandom.begin(), parsedSigma1.initiatorRandom.end(), mInitiatorRandom);
11201116
std::copy(parsedSigma1.resumptionId.begin(), parsedSigma1.resumptionId.end(), mResumeResumptionId.begin());
11211117

1122-
// Next Step is to send Sigma2Resume message to the initiator
1123-
nextStep = Step::kSendSigma2Resume;
1124-
11251118
// Early returning here, since the next Step is known to be Sigma2Resume, and no further processing is needed for the
11261119
// Sigma1 message
1127-
return CHIP_NO_ERROR;
1120+
return NextStep::Create<Step>(Step::kSendSigma2Resume);
11281121
}
11291122

11301123
// ParseSigma1 ensures that:
@@ -1143,21 +1136,21 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg, Step & n
11431136
// Side-effect of FindLocalNodeFromDestinationId success was that mFabricIndex/mLocalNodeId are now
11441137
// set to the local fabric and associated NodeId that was targeted by the initiator.
11451138

1146-
nextStep = Step::kSendSigma2;
1139+
return NextStep::Create<Step>(Step::kSendSigma2);
11471140
}
11481141
else
11491142
{
11501143
ChipLogError(SecureChannel, "CASE failed to match destination ID with local fabrics");
11511144
ChipLogByteSpan(SecureChannel, parsedSigma1.destinationId);
11521145

1153-
// FindLocalNodeFromDestinationId returns CHIP_ERROR_KEY_NOT_FOUND if validation of DestinationID fails, which will trigger
1154-
// status Report with ProtocolCode = NoSharedTrustRoots.
1155-
nextStep = Step::kSendStatusReport;
1146+
// FindLocalNodeFromDestinationId returns CHIP_ERROR_KEY_NOT_FOUND if Sigma1's DestinationId does not match any
1147+
// candidateDestinationId, this will trigger a status Report with ProtocolCode = NoSharedTrustRoots.
11561148

1157-
return err;
1149+
// Returning a CHIP_ERROR variant that will trigger a corresponding Status Report.
1150+
return NextStep::Create<CHIP_ERROR>(err);
11581151
}
11591152

1160-
return CHIP_NO_ERROR;
1153+
return NextStep::Create<CHIP_ERROR>(CHIP_NO_ERROR);
11611154
}
11621155

11631156
CHIP_ERROR CASESession::PrepareSigma2Resume(EncodeSigma2ResumeInputs & outSigma2ResData)

src/protocols/secure_channel/CASESession.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,11 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
197197
kNone,
198198
kSendSigma2,
199199
kSendSigma2Resume,
200-
kSendStatusReport
201200
};
201+
// Making NextStep a Variant allows HandleSigma() to return either a Step value (indicating
202+
// the next Sigma step to send) or a CHIP_ERROR (indicating a failure that will trigger
203+
// a Status Report).
204+
using NextStep = Variant<Step, CHIP_ERROR>;
202205
// This struct only serves as a base struct for EncodedSigma1Inputs and ParsedSigma1
203206
struct Sigma1Param
204207
{
@@ -328,7 +331,7 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
328331

329332
CHIP_ERROR SendSigma1();
330333
CHIP_ERROR HandleSigma1_and_SendSigma2(System::PacketBufferHandle && msg);
331-
CHIP_ERROR HandleSigma1(System::PacketBufferHandle && ms, Step & nextStep);
334+
NextStep HandleSigma1(System::PacketBufferHandle && msg);
332335
CHIP_ERROR TryResumeSession(SessionResumptionStorage::ConstResumptionIdView resumptionId, ByteSpan resume1MIC,
333336
ByteSpan initiatorRandom);
334337

0 commit comments

Comments
 (0)