Skip to content

Commit

Permalink
Delete RendezvousSession, and move code to controller and server (#6437)
Browse files Browse the repository at this point in the history
* Delete RendezvousSession, and move code to controller and server

* some cleanup

* some more cleanup

* fix Android build

* more fixes to Android build

* update commented out code
  • Loading branch information
pan-apple authored and pull[bot] committed May 13, 2021
1 parent 8d2857f commit 3268876
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 561 deletions.
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ CHIP_ERROR PairingCommand::Unpair(NodeId remoteId)
return mCommissioner.UnpairDevice(remoteId);
}

void PairingCommand::OnStatusUpdate(RendezvousSessionDelegate::Status status)
void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
{
switch (status)
{
case RendezvousSessionDelegate::Status::SecurePairingSuccess:
case DevicePairingDelegate::Status::SecurePairingSuccess:
ChipLogProgress(chipTool, "Secure Pairing Success");
break;
case RendezvousSessionDelegate::Status::SecurePairingFailed:
case DevicePairingDelegate::Status::SecurePairingFailed:
ChipLogError(chipTool, "Secure Pairing Failed");
break;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class PairingCommand : public Command,
CHIP_ERROR Run(PersistentStorage & storage, NodeId localId, NodeId remoteId) override;

/////////// DevicePairingDelegate Interface /////////
void OnStatusUpdate(chip::RendezvousSessionDelegate::Status status) override;
void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override;
void OnPairingComplete(CHIP_ERROR error) override;
void OnPairingDeleted(CHIP_ERROR error) override;

Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Example:
$ diffsyms.py --demangle ${IMAGE1} ${IMAGE2}
symbol a b
chip::Inet::InetLayer::NewUDPEndPoint(chip::Inet::UDPEndPoint**) 196 194
chip::Transport::BLE::Init(chip::RendezvousSessionDelegate*, chip::RendezvousParameters const&) 80 100
chip::Transport::BLE::Init(chip::DevicePairingDelegate*, chip::RendezvousParameters const&) 80 100
```

### block.py
Expand Down
132 changes: 84 additions & 48 deletions src/app/server/RendezvousServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,76 +33,112 @@ using namespace ::chip::Transport;
using namespace ::chip::DeviceLayer;

namespace chip {

RendezvousServer::RendezvousServer() : mRendezvousSession(this) {}
static constexpr uint32_t kSpake2p_Iteration_Count = 100;
static const char * kSpake2pKeyExchangeSalt = "SPAKE2P Key Salt";

CHIP_ERROR RendezvousServer::WaitForPairing(const RendezvousParameters & params, Messaging::ExchangeManager * exchangeManager,
TransportMgrBase * transportMgr, SecureSessionMgr * sessionMgr,
Transport::AdminPairingInfo * admin)
{
return mRendezvousSession.Init(params, exchangeManager, transportMgr, sessionMgr, admin);
VerifyOrReturnError(transportMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(sessionMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(admin != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(params.HasSetupPINCode() || params.HasPASEVerifier(), CHIP_ERROR_INVALID_ARGUMENT);

#if CONFIG_NETWORK_LAYER_BLE
VerifyOrReturnError(params.HasAdvertisementDelegate(), CHIP_ERROR_INVALID_ARGUMENT);
#endif

mAdvDelegate = params.GetAdvertisementDelegate();

// Note: Since BLE is only used for initial setup, enable BLE advertisement in rendezvous session can be expected.
if (params.GetPeerAddress().GetTransportType() == Transport::Type::kBle)
#if CONFIG_NETWORK_LAYER_BLE
{
ReturnErrorOnFailure(GetAdvertisementDelegate()->StartAdvertisement());
}
#else
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}
#endif
mSessionMgr = sessionMgr;
mAdmin = admin;
mExchangeManager = exchangeManager;

ReturnErrorOnFailure(mExchangeManager->RegisterUnsolicitedMessageHandlerForType(
Protocols::SecureChannel::MsgType::PBKDFParamRequest, &mPairingSession));

if (params.HasPASEVerifier())
{
ReturnErrorOnFailure(mPairingSession.WaitForPairing(params.GetPASEVerifier(), mNextKeyId++, this));
}
else
{
ReturnErrorOnFailure(mPairingSession.WaitForPairing(params.GetSetupPINCode(), kSpake2p_Iteration_Count,
reinterpret_cast<const unsigned char *>(kSpake2pKeyExchangeSalt),
strlen(kSpake2pKeyExchangeSalt), mNextKeyId++, this));
}

ReturnErrorOnFailure(mPairingSession.MessageDispatch().Init(transportMgr));
mPairingSession.MessageDispatch().SetPeerAddress(params.GetPeerAddress());

return CHIP_NO_ERROR;
}

void RendezvousServer::OnRendezvousError(CHIP_ERROR err)
void RendezvousServer::Cleanup()
{
ChipLogProgress(AppServer, "OnRendezvousError: %s", ErrorStr(err));
mExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::PBKDFParamRequest);

if (HasAdvertisementDelegate())
{
GetAdvertisementDelegate()->StopAdvertisement();
}
}

void RendezvousServer::OnRendezvousConnectionOpened()
void RendezvousServer::OnSessionEstablishmentError(CHIP_ERROR err)
{
ChipLogProgress(AppServer, "OnRendezvousConnectionOpened");
Cleanup();

ChipLogProgress(AppServer, "OnSessionEstablishmentError: %s", ErrorStr(err));
ChipLogProgress(AppServer, "Failed in SPAKE2+ handshake");

if (mDelegate != nullptr)
{
mDelegate->OnRendezvousStopped();
}
}

void RendezvousServer::OnRendezvousConnectionClosed()
void RendezvousServer::OnSessionEstablished()
{
ChipLogProgress(AppServer, "OnRendezvousConnectionClosed");
}
CHIP_ERROR err =
mSessionMgr->NewPairing(Optional<Transport::PeerAddress>::Value(mPairingSession.PeerConnection().GetPeerAddress()),
mPairingSession.PeerConnection().GetPeerNodeId(), &mPairingSession,
SecureSessionMgr::PairingDirection::kResponder, mAdmin->GetAdminId(), nullptr);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Ble, "Failed in setting up secure channel: err %s", ErrorStr(err));
OnSessionEstablishmentError(err);
return;
}

ChipLogProgress(AppServer, "Device completed SPAKE2+ handshake");
if (mDelegate != nullptr)
{
mDelegate->OnRendezvousStarted();
}

void RendezvousServer::OnRendezvousMessageReceived(const PacketHeader & packetHeader, const PeerAddress & peerAddress,
System::PacketBufferHandle buffer)
{}
Cleanup();

void RendezvousServer::OnRendezvousComplete()
{
ChipLogProgress(AppServer, "Device completed Rendezvous process");
StorablePeerConnection connection(mRendezvousSession.GetPairingSession(), mRendezvousSession.GetAdminId());
StorablePeerConnection connection(mPairingSession, mAdmin->GetAdminId());

VerifyOrReturn(mStorage != nullptr,
ChipLogError(AppServer, "Storage delegate is not available. Cannot store the connection state"));
VerifyOrReturn(connection.StoreIntoKVS(*mStorage) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Failed to store the connection state"));

uint16_t nextKeyId = mRendezvousSession.GetNextKeyId();
mStorage->SyncSetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, sizeof(nextKeyId));
}

void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)
{
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(AppServer, "OnRendezvousStatusUpdate: %s", chip::ErrorStr(err)));

switch (status)
{
case RendezvousSessionDelegate::SecurePairingSuccess:
ChipLogProgress(AppServer, "Device completed SPAKE2+ handshake");
if (mDelegate != nullptr)
{
mDelegate->OnRendezvousStarted();
}
break;

case RendezvousSessionDelegate::SecurePairingFailed:
ChipLogProgress(AppServer, "Failed in SPAKE2+ handshake");
if (mDelegate != nullptr)
{
mDelegate->OnRendezvousStopped();
}
break;

default:
break;
};

exit:
return;
mStorage->SyncSetKeyValue(kStorablePeerConnectionCountKey, &mNextKeyId, sizeof(mNextKeyId));
}
} // namespace chip
37 changes: 22 additions & 15 deletions src/app/server/RendezvousServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
#include <core/CHIPPersistentStorageDelegate.h>
#include <messaging/ExchangeMgr.h>
#include <platform/CHIPDeviceLayer.h>
#include <protocols/secure_channel/RendezvousSession.h>
#include <protocols/secure_channel/RendezvousParameters.h>

namespace chip {

class RendezvousServer : public RendezvousSessionDelegate
class RendezvousServer : public SessionEstablishmentDelegate
{
public:
RendezvousServer();

CHIP_ERROR WaitForPairing(const RendezvousParameters & params, Messaging::ExchangeManager * exchangeManager,
TransportMgrBase * transportMgr, SecureSessionMgr * sessionMgr, Transport::AdminPairingInfo * admin);

Expand All @@ -41,21 +39,30 @@ class RendezvousServer : public RendezvousSessionDelegate
return CHIP_NO_ERROR;
}

//////////////// RendezvousSessionDelegate Implementation ///////////////////
//////////// SessionEstablishmentDelegate Implementation ///////////////
void OnSessionEstablishmentError(CHIP_ERROR error) override;
void OnSessionEstablished() override;

void Cleanup();

void OnRendezvousConnectionOpened() override;
void OnRendezvousConnectionClosed() override;
void OnRendezvousError(CHIP_ERROR err) override;
void OnRendezvousMessageReceived(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle buffer) override;
void OnRendezvousComplete() override;
void OnRendezvousStatusUpdate(Status status, CHIP_ERROR err) override;
RendezvousSession * GetRendezvousSession() { return &mRendezvousSession; };
uint16_t GetNextKeyId() const { return mNextKeyId; }
void SetNextKeyId(uint16_t id) { mNextKeyId = id; }

private:
RendezvousSession mRendezvousSession;
AppDelegate * mDelegate;
PersistentStorageDelegate * mStorage = nullptr;
PersistentStorageDelegate * mStorage = nullptr;
Messaging::ExchangeManager * mExchangeManager = nullptr;

PASESession mPairingSession;
uint16_t mNextKeyId = 0;
SecureSessionMgr * mSessionMgr = nullptr;

Transport::AdminPairingInfo * mAdmin = nullptr;

const RendezvousAdvertisementDelegate * mAdvDelegate;

bool HasAdvertisementDelegate() const { return mAdvDelegate != nullptr; }
const RendezvousAdvertisementDelegate * GetAdvertisementDelegate() const { return mAdvDelegate; }
};

} // namespace chip
9 changes: 5 additions & 4 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static CHIP_ERROR RestoreAllSessionsFromKVS(SecureSessionMgr & sessionMgr, Rende

chip::Platform::Delete(session);

server.GetRendezvousSession()->SetNextKeyId(nextSessionKeyId);
server.SetNextKeyId(nextSessionKeyId);
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -262,9 +262,10 @@ class ServerRendezvousAdvertisementDelegate : public RendezvousAdvertisementDele
{
ReturnErrorOnFailure(chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(false));
}

if (mDelegate != nullptr)
{
if (mDelegate != nullptr)
mDelegate->OnPairingWindowClosed();
mDelegate->OnPairingWindowClosed();
}

AdminPairingInfo * admin = gAdminPairings.FindAdmin(mAdmin);
Expand Down Expand Up @@ -429,7 +430,7 @@ CHIP_ERROR OpenDefaultPairingWindow(ResetAdmins resetAdmins, chip::PairingWindow

if (resetAdmins == ResetAdmins::kYes)
{
uint16_t nextKeyId = gRendezvousServer.GetRendezvousSession()->GetNextKeyId();
uint16_t nextKeyId = gRendezvousServer.GetNextKeyId();
EraseAllAdminPairingsUpTo(gNextAvailableAdminId);
EraseAllSessionsUpTo(nextKeyId);
gNextAvailableAdminId = 0;
Expand Down
13 changes: 0 additions & 13 deletions src/channel/ChannelContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,6 @@ void ChannelContext::HandleNodeIdResolve(CHIP_ERROR error, uint64_t nodeId, cons
}
}

// Session establishment
CHIP_ERROR ChannelContext::SendSessionEstablishmentMessage(const PacketHeader & header, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle msgIn)
{
return mExchangeManager->GetSessionMgr()->GetTransportManager()->SendMessage(header, peerAddress, std::move(msgIn));
}

CHIP_ERROR ChannelContext::HandlePairingMessage(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg)
{
return CHIP_ERROR_INCORRECT_STATE;
}

void ChannelContext::EnterCasePairingState()
{
mStateVars.mPreparing.mState = PrepareState::kCasePairing;
Expand Down
4 changes: 0 additions & 4 deletions src/channel/ChannelContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ class ChannelContext : public ReferenceCounted<ChannelContext, ChannelContextDel
void OnConnectionExpired(SecureSessionHandle session);

// Pairing callbacks
CHIP_ERROR HandlePairingMessage(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle && msg);
CHIP_ERROR SendSessionEstablishmentMessage(const PacketHeader & header, const Transport::PeerAddress & peerAddress,
System::PacketBufferHandle msgIn) override;
void OnSessionEstablishmentError(CHIP_ERROR error) override;
void OnSessionEstablished() override;

Expand Down
Loading

0 comments on commit 3268876

Please sign in to comment.