Skip to content

Commit

Permalink
Making Parsing and Encoding methods protected
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed Dec 13, 2024
1 parent 5673728 commit 2722dc5
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 109 deletions.
16 changes: 11 additions & 5 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle &
case Step::kSendSigma2Resume: {

System::PacketBufferHandle msg_R2_resume;
EncodeSigma2ResParam encodeSigma2Resume;
EncodeSigma2ResInputs encodeSigma2Resume;

MATTER_LOG_METRIC_BEGIN(kMetricDeviceCASESessionSigma2Resume);

Expand Down Expand Up @@ -1142,7 +1142,7 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg)
return CHIP_NO_ERROR;
}

CHIP_ERROR CASESession::PrepareSigma2Resume(EncodeSigma2ResParam & output)
CHIP_ERROR CASESession::PrepareSigma2Resume(EncodeSigma2ResInputs & output)
{
MATTER_TRACE_SCOPE("PrepareSigma2Resume", "CASESession");

Expand All @@ -1165,7 +1165,7 @@ CHIP_ERROR CASESession::PrepareSigma2Resume(EncodeSigma2ResParam & output)
return CHIP_NO_ERROR;
}

CHIP_ERROR CASESession::EncodeSigma2Resume(System::PacketBufferHandle & msg_R2_resume, EncodeSigma2ResParam & input)
CHIP_ERROR CASESession::EncodeSigma2Resume(System::PacketBufferHandle & msg_R2_resume, EncodeSigma2ResInputs & input)
{
MATTER_TRACE_SCOPE("EncodeSigma2Resume", "CASESession");
size_t max_sigma2_resume_data_len = TLV::EstimateStructOverhead(SessionResumptionStorage::kResumptionIdSize, // resumptionID
Expand All @@ -1174,18 +1174,24 @@ CHIP_ERROR CASESession::EncodeSigma2Resume(System::PacketBufferHandle & msg_R2_r
SessionParameters::kEstimatedTLVSize // responderSessionParams
);

System::PacketBufferTLVWriter tlvWriter;
TLV::TLVType outerContainerType = TLV::kTLVType_NotSpecified;
// the passed PacketBufferHandler should be empty
VerifyOrReturnError(msg_R2_resume.IsNull(), CHIP_ERROR_INCORRECT_STATE);

msg_R2_resume = System::PacketBufferHandle::New(max_sigma2_resume_data_len);
VerifyOrReturnError(!msg_R2_resume.IsNull(), CHIP_ERROR_NO_MEMORY);

System::PacketBufferTLVWriter tlvWriter;
TLV::TLVType outerContainerType = TLV::kTLVType_NotSpecified;

tlvWriter.Init(std::move(msg_R2_resume));

ReturnErrorOnFailure(tlvWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outerContainerType));
ReturnErrorOnFailure(tlvWriter.Put(TLV::ContextTag(kTag_Sigma2Res_ResumptionID), input.resumptionId));
ReturnErrorOnFailure(tlvWriter.Put(TLV::ContextTag(kTag_Sigma2Res_Sigma2ResumeMIC), input.resumeMICSpan));
ReturnErrorOnFailure(tlvWriter.Put(TLV::ContextTag(kTag_Sigma2Res_ResponderSessionID), input.responderSessionId));

VerifyOrReturnError(input.responderMrpConfig != nullptr, CHIP_ERROR_INCORRECT_STATE);

ReturnErrorOnFailure(
EncodeSessionParameters(TLV::ContextTag(kTag_Sigma2Res_ResponderMRPParams), *input.responderMrpConfig, tlvWriter));

Expand Down
120 changes: 58 additions & 62 deletions src/protocols/secure_channel/CASESession.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@

namespace chip {

namespace Testing {
class FuzzCASESession;
}

// TODO: temporary derive from Messaging::UnsolicitedMessageHandler, actually the CASEServer should be the umh, it will be fixed
// when implementing concurrent CASE session.
class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
Expand Down Expand Up @@ -146,36 +142,6 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
bool initiatorMrpParamsPresent = false;
};

/**
* @brief Encodes a Sigma1 message into TLV format and allocates a buffer for it within the provided PacketBufferHandle.
*
* @param outMsg PacketBufferHandle passed by reference. The handle must be empty prior to calling this method,
* as a new buffer will be allocated and assigned to it within the method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
* @note the passed PacketBufferHandle `outMsg` must be in a null state.
**/
CHIP_ERROR EncodeSigma1(System::PacketBufferHandle & outMsg, EncodeSigma1Inputs & inParam);

/**
* Parse a sigma1 message. This function will return success only if the
* message passes schema checks. Specifically:
* * The tags come in order.
* * The required tags are present.
* * The values for the tags that are present satisfy schema requirements
* (e.g. constraints on octet string lengths)
* * Either resumptionID and initiatorResume1MIC are both present or both
* absent.
*
* On success, the members of outParam will be set to the values corresponding to the message.
*
* On success, either the sessionResumptionRequested outParam will be set to true
* and the resumptionID and initiatorResumeMICSpan outparams will be set to
* valid values, or the sessionResumptionRequested outParam will be set to false.
*/
CHIP_ERROR ParseSigma1(TLV::ContiguousBufferTLVReader & tlvReader, ParsedSigma1 & outParam);

// Helper Enum for usage in HandleSigma1_and_SendSigma2
enum class Step : uint8_t
{
Expand All @@ -197,7 +163,7 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
const ReliableMessageProtocolConfig * responderMrpConfig;
};

struct EncodeSigma2ResParam
struct EncodeSigma2ResInputs
{
ByteSpan resumptionId;
uint8_t sigma2ResumeMIC[Crypto::CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES];
Expand All @@ -206,31 +172,6 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
const ReliableMessageProtocolConfig * responderMrpConfig;
};

/**
* @brief Encodes a Sigma2 message into TLV format and allocates a buffer for it within the provided PacketBufferHandle.
*
* @param outMsg PacketBufferHandle passed by reference. The handle must be empty prior to calling this method,
* as a new buffer will be allocated and assigned to it within the method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
* @note the passed PacketBufferHandle `outMsg` must be in a null state.
**/

CHIP_ERROR EncodeSigma2(System::PacketBufferHandle & outMsg, EncodeSigma2Inputs & inParam);

/**
* @brief Encodes a Sigma2_Resume message into TLV format and allocates a buffer for it within the provided PacketBufferHandle.
*
* @param outMsg PacketBufferHandle passed by reference. The handle must be empty prior to calling this method,
* as a new buffer will be allocated and assigned to it within the method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
* @note the passed PacketBufferHandle `outMsg` must be in a null state.
**/
CHIP_ERROR EncodeSigma2Resume(System::PacketBufferHandle & outMsg, EncodeSigma2ResParam & inParam);

/**
* @brief
* Derive a secure session from the established session. The API will return error if called before session is established.
Expand Down Expand Up @@ -303,9 +244,64 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
// If this function returns true, the CASE session has been reset and is ready for a new session establishment.
bool InvokeBackgroundWorkWatchdog();

protected:
/**
* @brief Encodes a Sigma1 message into TLV format and allocates a buffer for it within the provided PacketBufferHandle.
*
* @param outMsg PacketBufferHandle passed by reference. The handle must be empty prior to calling this method,
* as a new buffer will be allocated and assigned to it within the method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
* @note the passed PacketBufferHandle `outMsg` must be in a null state.
**/
CHIP_ERROR EncodeSigma1(System::PacketBufferHandle & outMsg, EncodeSigma1Inputs & inParam);

/**
* Parse a sigma1 message. This function will return success only if the
* message passes schema checks. Specifically:
* * The tags come in order.
* * The required tags are present.
* * The values for the tags that are present satisfy schema requirements
* (e.g. constraints on octet string lengths)
* * Either resumptionID and initiatorResume1MIC are both present or both
* absent.
*
* On success, the members of outParam will be set to the values corresponding to the message.
*
* On success, either the sessionResumptionRequested outParam will be set to true
* and the resumptionID and initiatorResumeMICSpan outparams will be set to
* valid values, or the sessionResumptionRequested outParam will be set to false.
*/
CHIP_ERROR ParseSigma1(TLV::ContiguousBufferTLVReader & tlvReader, ParsedSigma1 & outParam);

/**
* @brief Encodes a Sigma2 message into TLV format and allocates a buffer for it within the provided PacketBufferHandle.
*
* @param outMsg PacketBufferHandle passed by reference. The handle must be empty prior to calling this method,
* as a new buffer will be allocated and assigned to it within the method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
* @note the passed PacketBufferHandle `outMsg` must be in a null state.
**/

CHIP_ERROR EncodeSigma2(System::PacketBufferHandle & outMsg, EncodeSigma2Inputs & inParam);

/**
* @brief Encodes a Sigma2_Resume message into TLV format and allocates a buffer for it within the provided PacketBufferHandle.
*
* @param outMsg PacketBufferHandle passed by reference. The handle must be empty prior to calling this method,
* as a new buffer will be allocated and assigned to it within the method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
* @note the passed PacketBufferHandle `outMsg` must be in a null state.
**/
CHIP_ERROR EncodeSigma2Resume(System::PacketBufferHandle & outMsg, EncodeSigma2ResInputs & inParam);

private:
friend class TestCASESession;
friend class Testing::FuzzCASESession;

using AutoReleaseSessionKey = Crypto::AutoReleaseSymmetricKey<Crypto::Aes128KeyHandle>;

Expand Down Expand Up @@ -334,7 +330,7 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
ByteSpan initiatorRandom);

CHIP_ERROR PrepareSigma2(EncodeSigma2Inputs & output);
CHIP_ERROR PrepareSigma2Resume(EncodeSigma2ResParam & output);
CHIP_ERROR PrepareSigma2Resume(EncodeSigma2ResInputs & output);
CHIP_ERROR SendSigma2(System::PacketBufferHandle & msg_R2);
CHIP_ERROR SendSigma2Resume(System::PacketBufferHandle & msg_R2_resume);

Expand Down
Loading

0 comments on commit 2722dc5

Please sign in to comment.