Skip to content

Commit 4ddcb46

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 4ddcb46

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
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

+15-17
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);
@@ -1120,11 +1116,12 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg, Step & n
11201116
std::copy(parsedSigma1.resumptionId.begin(), parsedSigma1.resumptionId.end(), mResumeResumptionId.begin());
11211117

11221118
// Next Step is to send Sigma2Resume message to the initiator
1123-
nextStep = Step::kSendSigma2Resume;
1119+
// TODO REMOVE
1120+
// nextStep = Step::kSendSigma2Resume;
11241121

11251122
// Early returning here, since the next Step is known to be Sigma2Resume, and no further processing is needed for the
11261123
// Sigma1 message
1127-
return CHIP_NO_ERROR;
1124+
return NextStep::Create<Step>(Step::kSendSigma2Resume);
11281125
}
11291126

11301127
// ParseSigma1 ensures that:
@@ -1143,7 +1140,7 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg, Step & n
11431140
// Side-effect of FindLocalNodeFromDestinationId success was that mFabricIndex/mLocalNodeId are now
11441141
// set to the local fabric and associated NodeId that was targeted by the initiator.
11451142

1146-
nextStep = Step::kSendSigma2;
1143+
return NextStep::Create<Step>(Step::kSendSigma2);
11471144
}
11481145
else
11491146
{
@@ -1152,12 +1149,13 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg, Step & n
11521149

11531150
// FindLocalNodeFromDestinationId returns CHIP_ERROR_KEY_NOT_FOUND if validation of DestinationID fails, which will trigger
11541151
// status Report with ProtocolCode = NoSharedTrustRoots.
1155-
nextStep = Step::kSendStatusReport;
1152+
// TODO remove
1153+
// nextStep = Step::kSendStatusReport;
11561154

1157-
return err;
1155+
return NextStep::Create<CHIP_ERROR>(err);
11581156
}
11591157

1160-
return CHIP_NO_ERROR;
1158+
return NextStep::Create<CHIP_ERROR>(CHIP_NO_ERROR);
11611159
}
11621160

11631161
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)