Skip to content

Commit

Permalink
Move PairingSession from transport/ to protocols/secure_channel/
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Apr 29, 2022
1 parent 2348898 commit bb708d3
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 231 deletions.
107 changes: 0 additions & 107 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,107 +0,0 @@
#
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

_bootstrap_or_activate() {
if [ -n "$BASH" ]; then
local _BOOTSTRAP_PATH="${BASH_SOURCE[0]}"
else
local _BOOTSTRAP_PATH="$1"
fi

local _BOOTSTRAP_NAME="${_BOOTSTRAP_PATH##*/}"
local _CHIP_ROOT="$(cd "${_BOOTSTRAP_PATH%/*}/.." && pwd)"

local _CONFIG_FILE="scripts/environment.json"

if [ "$_BOOTSTRAP_NAME" = "no_cipd_bootstrap.sh" ]; then
_CONFIG_FILE="scripts/environment_no_cipd.json"
_BOOTSTRAP_NAME="bootstrap.sh"
fi

if [ "$_BOOTSTRAP_NAME" = "bootstrap.sh" ] ||
[ ! -f "$_CHIP_ROOT/third_party/pigweed/repo/pw_env_setup/util.sh" ]; then
git submodule update --init
fi

local _CHIP_BANNER="$(
cat <<EOF
░▓░
▓█▓
▓█▓ ▒█ ▒█
▒██▒▒▓██▒███▒ ░▒▓▒░ ░▒▓▒░ ░░▓█▒░ ░█ █████████████░ ░▒█▒░ ░░▒░
░▓█████▓██░ ▒█▒░░▒▓██▓▒░░▒█▒ ░█▓▒░░▒████ █▓░ █▓░ ▒█▒░░░▒█▓░ ██▒░░
▒█▒░ ░██▒ ░█░ █▓ ░█░ ░▓▒ ░▓█ █▓ █▓ ▒█░░ ░██ ▒▓
░▓██▓░ ░██▓█░ ░█ ▓█ █░ ░█░ ██ █▓ █▓ ▓██████████ ▒█
░▓███▒ ▒███▒░ ░█ ▓█ █░ ██░ ░███ █▓ █▓ ░█▒░ ▒█
░▒████████░ ░███▓▓█▓▓▒ ░█ ▒▒ █░ ░█▓██▓█░▒█ ░▓▓█░ ░▓▓█░ ▒▓▓██▓█░ ▒▓
░██▒░ ▒██▒ ▒██░ ░▒█▓
░▓░ ░▓░
EOF
)"

PW_PROJECT_ROOT="$_CHIP_ROOT"
export PW_PROJECT_ROOT

PW_ROOT="$_CHIP_ROOT/third_party/pigweed/repo"
export PW_ROOT

# Do not force use Rosetta in Pigweed because Matter is using host toolchain
if [ -z "$PW_BOOTSTRAP_USE_ROSETTA" ]; then
export PW_BOOTSTRAP_USE_ROSETTA=false
fi

. "$_CHIP_ROOT/third_party/pigweed/repo/pw_env_setup/util.sh"

_chip_bootstrap_banner() {
if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
pw_bold_white "$_CHIP_BANNER\n"
fi
}

local _PW_BANNER_FUNC="_chip_bootstrap_banner"

export _PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
local _SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"

export PW_DOCTOR_SKIP_CIPD_CHECKS=1
export PATH # https://bugs.chromium.org/p/pigweed/issues/detail?id=281

if [ "$_BOOTSTRAP_NAME" = "bootstrap.sh" ] ||
[ ! -f "$_SETUP_SH" ] ||
[ ! -s "$_SETUP_SH" ]; then
pw_bootstrap --shell-file "$_SETUP_SH" \
--install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" \
--config-file "$_CHIP_ROOT/$_CONFIG_FILE" \
--virtualenv-gn-out-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT/gn_out"
pw_finalize bootstrap "$_SETUP_SH"
else
pw_activate
pw_finalize activate "$_SETUP_SH"
fi
}

_bootstrap_or_activate "$0"
unset -f _bootstrap_or_activate

pw_cleanup

unset PW_CIPD_INSTALL_DIR
unset CIPD_CACHE_DIR
unset _PW_BANNER_FUNC
unset _PW_TEXT
unset PW_DOCTOR_SKIP_CIPD_CHECKS

unset -f _chip_bootstrap_banner
2 changes: 2 additions & 0 deletions src/protocols/secure_channel/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ static_library("secure_channel") {
"CASESession.h",
"PASESession.cpp",
"PASESession.h",
"PairingSession.cpp",
"PairingSession.h",
"RendezvousParameters.h",
"SessionEstablishmentDelegate.h",
"SessionEstablishmentExchangeDispatch.cpp",
Expand Down
46 changes: 1 addition & 45 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
#include <lib/support/TypeTraits.h>
#include <protocols/Protocols.h>
#include <protocols/secure_channel/CASEDestinationId.h>
#include <protocols/secure_channel/PairingSession.h>
#include <protocols/secure_channel/SessionResumptionStorage.h>
#include <protocols/secure_channel/StatusReport.h>
#include <system/TLVPacketBufferBackingStore.h>
#include <trace/trace.h>
#include <transport/PairingSession.h>
#include <transport/SessionManager.h>
#if CHIP_CRYPTO_HSM
#include <crypto/hsm/CHIPCryptoPALHsm.h>
Expand Down Expand Up @@ -127,24 +127,6 @@ CASESession::~CASESession()
Clear();
}

void CASESession::Finish()
{
Transport::PeerAddress address = mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress();

// Discard the exchange so that Clear() doesn't try closing it. The exchange will handle that.
DiscardExchange();

CHIP_ERROR err = ActivateSecureSession(address);
if (err == CHIP_NO_ERROR)
{
mDelegate->OnSessionEstablished(mSecureSessionHolder.Get());
}
else
{
mDelegate->OnSessionEstablishmentError(err);
}
}

void CASESession::Clear()
{
// This function zeroes out and resets the memory used by the object.
Expand All @@ -162,32 +144,6 @@ void CASESession::Clear()
mFabricInfo = nullptr;
}

void CASESession::AbortExchange()
{
if (mExchangeCtxt != nullptr)
{
// The only time we reach this is if we are getting destroyed in the
// middle of our handshake. In that case, there is no point trying to
// do MRP resends of the last message we sent, so abort the exchange
// instead of just closing it.
mExchangeCtxt->Abort();
mExchangeCtxt = nullptr;
}
}

void CASESession::DiscardExchange()
{
if (mExchangeCtxt != nullptr)
{
// Make sure the exchange doesn't try to notify us when it closes,
// since we might be dead by then.
mExchangeCtxt->SetDelegate(nullptr);
// Null out mExchangeCtxt so that Clear() doesn't try closing it. The
// exchange will handle that.
mExchangeCtxt = nullptr;
}
}

CHIP_ERROR CASESession::Init(SessionManager & sessionManager, SessionEstablishmentDelegate * delegate)
{
VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
17 changes: 1 addition & 16 deletions src/protocols/secure_channel/CASESession.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
#include <messaging/ExchangeDelegate.h>
#include <protocols/secure_channel/CASEDestinationId.h>
#include <protocols/secure_channel/Constants.h>
#include <protocols/secure_channel/SessionEstablishmentDelegate.h>
#include <protocols/secure_channel/PairingSession.h>
#include <protocols/secure_channel/SessionEstablishmentExchangeDispatch.h>
#include <protocols/secure_channel/SessionResumptionStorage.h>
#include <system/SystemPacketBuffer.h>
#include <transport/CryptoContext.h>
#include <transport/PairingSession.h>
#include <transport/raw/MessageHeader.h>
#include <transport/raw/PeerAddress.h>

Expand Down Expand Up @@ -216,26 +215,13 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
void OnSuccessStatusReport() override;
CHIP_ERROR OnFailureStatusReport(Protocols::SecureChannel::GeneralStatusCode generalCode, uint16_t protocolCode) override;

// TODO: pull up Finish to PairingSession class
void Finish();

void AbortExchange();

/**
* Clear our reference to our exchange context pointer so that it can close
* itself at some later time.
*/
void DiscardExchange();

CHIP_ERROR GetHardcodedTime();

CHIP_ERROR SetEffectiveTime();

CHIP_ERROR ValidateReceivedMessage(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
const System::PacketBufferHandle & msg);

SessionEstablishmentDelegate * mDelegate = nullptr;

Crypto::Hash_SHA256_stream mCommissioningHash;
Crypto::P256PublicKey mRemotePubKey;
#ifdef ENABLE_HSM_CASE_EPHEMERAL_KEY
Expand All @@ -250,7 +236,6 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
uint8_t mMessageDigest[Crypto::kSHA256_Hash_Length];
uint8_t mIPK[kIPKSize];

Messaging::ExchangeContext * mExchangeCtxt = nullptr;
SessionResumptionStorage * mSessionResumptionStorage = nullptr;

FabricTable * mFabricsTable = nullptr;
Expand Down
38 changes: 1 addition & 37 deletions src/protocols/secure_channel/PASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,7 @@ PASESession::~PASESession()
void PASESession::Finish()
{
mPairingComplete = true;

Transport::PeerAddress address = mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress();

// Discard the exchange so that Clear() doesn't try closing it. The exchange will handle that.
DiscardExchange();

CHIP_ERROR err = ActivateSecureSession(address);
if (err == CHIP_NO_ERROR)
{
mDelegate->OnSessionEstablished(mSecureSessionHolder.Get());
}
else
{
mDelegate->OnSessionEstablishmentError(err);
}
PairingSession::Finish();
}

void PASESession::Clear()
Expand All @@ -113,28 +99,6 @@ void PASESession::Clear()
CloseExchange();
}

void PASESession::CloseExchange()
{
if (mExchangeCtxt != nullptr)
{
mExchangeCtxt->Close();
mExchangeCtxt = nullptr;
}
}

void PASESession::DiscardExchange()
{
if (mExchangeCtxt != nullptr)
{
// Make sure the exchange doesn't try to notify us when it closes,
// since we might be dead by then.
mExchangeCtxt->SetDelegate(nullptr);
// Null out mExchangeCtxt so that Clear() doesn't try closing it. The
// exchange will handle that.
mExchangeCtxt = nullptr;
}
}

CHIP_ERROR PASESession::Init(SessionManager & sessionManager, uint32_t setupCode, SessionEstablishmentDelegate * delegate)
{
VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
16 changes: 1 addition & 15 deletions src/protocols/secure_channel/PASESession.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
#include <messaging/ExchangeDelegate.h>
#include <messaging/ExchangeMessageDispatch.h>
#include <protocols/secure_channel/Constants.h>
#include <protocols/secure_channel/SessionEstablishmentDelegate.h>
#include <protocols/secure_channel/PairingSession.h>
#include <protocols/secure_channel/SessionEstablishmentExchangeDispatch.h>
#include <system/SystemPacketBuffer.h>
#include <transport/CryptoContext.h>
#include <transport/PairingSession.h>
#include <transport/raw/MessageHeader.h>
#include <transport/raw/PeerAddress.h>

Expand Down Expand Up @@ -208,19 +207,8 @@ class DLL_EXPORT PASESession : public Messaging::UnsolicitedMessageHandler,
void OnSuccessStatusReport() override;
CHIP_ERROR OnFailureStatusReport(Protocols::SecureChannel::GeneralStatusCode generalCode, uint16_t protocolCode) override;

// TODO: pull up Finish to PairingSession class
void Finish();

void CloseExchange();

/**
* Clear our reference to our exchange context pointer so that it can close
* itself at some later time.
*/
void DiscardExchange();

SessionEstablishmentDelegate * mDelegate = nullptr;

Protocols::SecureChannel::MsgType mNextExpectedMsg = Protocols::SecureChannel::MsgType::PASE_PakeError;

#ifdef ENABLE_HSM_SPAKE
Expand All @@ -242,8 +230,6 @@ class DLL_EXPORT PASESession : public Messaging::UnsolicitedMessageHandler,
uint16_t mSaltLength = 0;
uint8_t * mSalt = nullptr;

Messaging::ExchangeContext * mExchangeCtxt = nullptr;

struct Spake2pErrorMsg
{
Spake2pErrorType error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

#include <transport/PairingSession.h>
#include <protocols/secure_channel/PairingSession.h>

#include <lib/core/CHIPTLVTypes.h>
#include <lib/support/SafeInt.h>
Expand Down Expand Up @@ -57,6 +57,59 @@ CHIP_ERROR PairingSession::ActivateSecureSession(const Transport::PeerAddress &
return CHIP_NO_ERROR;
}

void PairingSession::Finish()
{
Transport::PeerAddress address = mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress();

// Discard the exchange so that Clear() doesn't try closing it. The exchange will handle that.
DiscardExchange();

CHIP_ERROR err = ActivateSecureSession(address);
if (err == CHIP_NO_ERROR)
{
mDelegate->OnSessionEstablished(mSecureSessionHolder.Get());
}
else
{
mDelegate->OnSessionEstablishmentError(err);
}
}

void PairingSession::AbortExchange()
{
if (mExchangeCtxt != nullptr)
{
// The only time we reach this is if we are getting destroyed in the
// middle of our handshake. In that case, there is no point trying to
// do MRP resends of the last message we sent, so abort the exchange
// instead of just closing it.
mExchangeCtxt->Abort();
mExchangeCtxt = nullptr;
}
}

void PairingSession::CloseExchange()
{
if (mExchangeCtxt != nullptr)
{
mExchangeCtxt->Close();
mExchangeCtxt = nullptr;
}
}

void PairingSession::DiscardExchange()
{
if (mExchangeCtxt != nullptr)
{
// Make sure the exchange doesn't try to notify us when it closes,
// since we might be dead by then.
mExchangeCtxt->SetDelegate(nullptr);
// Null out mExchangeCtxt so that Clear() doesn't try closing it. The
// exchange will handle that.
mExchangeCtxt = nullptr;
}
}

CHIP_ERROR PairingSession::EncodeMRPParameters(TLV::Tag tag, const ReliableMessageProtocolConfig & mrpConfig,
TLV::TLVWriter & tlvWriter)
{
Expand Down
Loading

0 comments on commit bb708d3

Please sign in to comment.