Skip to content

Commit

Permalink
pw_bluetooth_sapphire: Construct SecurityManager with Peer::WeakPtr
Browse files Browse the repository at this point in the history
This will enable SecurityManager to get PairingTokens and check if the
other transport is pairing.

Bug: 388607971
Change-Id: Ic4d0a70b7bbdd3139ea456e622c5377208f6757f
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/259356
Commit-Queue: Auto-Submit <[email protected]>
Lint: Lint 🤖 <[email protected]>
Pigweed-Auto-Submit: Ben Lawson <[email protected]>
Docs-Not-Needed: Ben Lawson <[email protected]>
Reviewed-by: Jason Graffius <[email protected]>
  • Loading branch information
BenjaminLawson authored and CQ Bot Account committed Jan 10, 2025
1 parent cf38dfa commit 3c65121
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 23 deletions.
3 changes: 2 additions & 1 deletion pw_bluetooth_sapphire/host/gap/low_energy_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ bool LowEnergyConnection::OnL2capFixedChannelsOpened(
weak_delegate_.GetWeakPtr(),
connection_options.bondable_mode,
security_mode,
dispatcher_);
dispatcher_,
peer_);

// Provide SMP with the correct LTK from a previous pairing with the peer, if
// it exists. This will start encryption if the local device is the link-layer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_connector.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/channel_manager.h"
#include "pw_bluetooth_sapphire/internal/host/sm/error.h"
#include "pw_bluetooth_sapphire/internal/host/sm/security_manager.h"
#include "pw_bluetooth_sapphire/internal/host/sm/types.h"
#include "pw_bluetooth_sapphire/internal/host/transport/command_channel.h"
#include "pw_bluetooth_sapphire/internal/host/transport/control_packets.h"
Expand All @@ -44,17 +45,6 @@

namespace bt {

namespace sm {
using SecurityManagerFactory = std::function<std::unique_ptr<SecurityManager>(
hci::LowEnergyConnection::WeakPtr,
l2cap::Channel::WeakPtr,
IOCapability,
Delegate::WeakPtr,
BondableMode,
gap::LESecurityMode,
pw::async::Dispatcher&)>;
} // namespace sm

namespace hci {
class LocalAddressDelegate;
} // namespace hci
Expand Down
1 change: 1 addition & 0 deletions pw_bluetooth_sapphire/host/sm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ cc_library(
":definitions",
"//pw_bluetooth_sapphire/host/common",
"//pw_bluetooth_sapphire/host/gap:definitions",
"//pw_bluetooth_sapphire/host/gap:peer",
"//pw_bluetooth_sapphire/host/hci",
"//pw_bluetooth_sapphire/host/l2cap",
"//pw_bluetooth_sapphire/lib/cpp-string",
Expand Down
1 change: 1 addition & 0 deletions pw_bluetooth_sapphire/host/sm/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pw_source_set("sm") {
":definitions",
"$dir_pw_bluetooth_sapphire:config",
"$dir_pw_bluetooth_sapphire/host/gap:definitions",
"$dir_pw_bluetooth_sapphire/host/gap:peer",
"$dir_pw_bluetooth_sapphire/host/hci",
"$dir_pw_bluetooth_sapphire/host/l2cap",
"$dir_pw_third_party/boringssl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <memory>

#include "pw_bluetooth_sapphire/internal/host/gap/gap.h"
#include "pw_bluetooth_sapphire/internal/host/gap/peer.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_connection.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/channel.h"
#include "pw_bluetooth_sapphire/internal/host/sm/delegate.h"
Expand Down Expand Up @@ -47,17 +48,21 @@ class SecurityManager {
// |smp|: The L2CAP LE SMP fixed channel that operates over |link|.
// |io_capability|: The initial I/O capability.
// |delegate|: Delegate which handles SMP interactions with the rest of the
// Bluetooth stack. |bondable_mode|: the operating bondable mode of the device
// (see v5.2, Vol. 3, Part C 9.4). |security_mode|: the security mode of this
// SecurityManager (see v5.2, Vol. 3, Part C 10.2).
// Bluetooth stack.
// |bondable_mode|: the operating bondable mode of the device
// (see v5.2, Vol. 3, Part C 9.4).
// |security_mode|: the security mode of this SecurityManager (see v5.2, Vol.
// 3, Part C 10.2).
// |peer|: The peer that the SMP fixed channel corresponds to.
static std::unique_ptr<SecurityManager> Create(
hci::LowEnergyConnection::WeakPtr link,
l2cap::Channel::WeakPtr smp,
IOCapability io_capability,
Delegate::WeakPtr delegate,
BondableMode bondable_mode,
gap::LESecurityMode security_mode,
pw::async::Dispatcher& dispatcher);
pw::async::Dispatcher& dispatcher,
bt::gap::Peer::WeakPtr peer);
virtual ~SecurityManager() = default;
// Assigns the requested |ltk| to this connection, adopting the security
// properties of |ltk|. If the local device is the central of the underlying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "pw_bluetooth_sapphire/internal/host/gap/gap.h"
#include "pw_bluetooth_sapphire/internal/host/hci-spec/link_key.h"
#include "pw_bluetooth_sapphire/internal/host/hci/connection.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_connection.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/channel.h"
#include "pw_bluetooth_sapphire/internal/host/sm/delegate.h"
#include "pw_bluetooth_sapphire/internal/host/sm/error.h"
Expand Down Expand Up @@ -94,7 +95,8 @@ class TestSecurityManagerFactory {
Delegate::WeakPtr delegate,
BondableMode bondable_mode,
gap::LESecurityMode security_mode,
pw::async::Dispatcher& dispatcher);
pw::async::Dispatcher& dispatcher,
gap::Peer::WeakPtr peer);

// Obtain a reference to the TestSecurityManager associated with
// |conn_handle|'s connection for use in test code.
Expand Down
15 changes: 11 additions & 4 deletions pw_bluetooth_sapphire/host/sm/security_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class SecurityManagerImpl final : public SecurityManager,
Delegate::WeakPtr delegate,
BondableMode bondable_mode,
gap::LESecurityMode security_mode,
pw::async::Dispatcher& dispatcher);
pw::async::Dispatcher& dispatcher,
bt::gap::Peer::WeakPtr peer);
// SecurityManager overrides:
bool AssignLongTermKey(const LTK& ltk) override;
void UpgradeSecurity(SecurityLevel level, PairingCallback callback) override;
Expand Down Expand Up @@ -230,6 +231,8 @@ class SecurityManagerImpl final : public SecurityManager,

SmartTask timeout_task_{pw_dispatcher_};

bt::gap::Peer::WeakPtr peer_;

// The presence of a particular phase in this variant indicates that a
// security upgrade is in progress at the stored phase. No security upgrade is
// in progress if std::monostate is present.
Expand Down Expand Up @@ -264,7 +267,8 @@ SecurityManagerImpl::SecurityManagerImpl(hci::LowEnergyConnection::WeakPtr link,
Delegate::WeakPtr delegate,
BondableMode bondable_mode,
gap::LESecurityMode security_mode,
pw::async::Dispatcher& dispatcher)
pw::async::Dispatcher& dispatcher,
bt::gap::Peer::WeakPtr peer)
: SecurityManager(bondable_mode, security_mode),
pw_dispatcher_(dispatcher),
next_pairing_id_(0),
Expand All @@ -276,6 +280,7 @@ SecurityManagerImpl::SecurityManagerImpl(hci::LowEnergyConnection::WeakPtr link,
role_(le_link_->role() == pw::bluetooth::emboss::ConnectionRole::CENTRAL
? Role::kInitiator
: Role::kResponder),
peer_(std::move(peer)),
weak_self_(this),
weak_listener_(this),
weak_handler_(this) {
Expand Down Expand Up @@ -1021,14 +1026,16 @@ std::unique_ptr<SecurityManager> SecurityManager::Create(
Delegate::WeakPtr delegate,
BondableMode bondable_mode,
gap::LESecurityMode security_mode,
pw::async::Dispatcher& dispatcher) {
pw::async::Dispatcher& dispatcher,
bt::gap::Peer::WeakPtr peer) {
return std::make_unique<SecurityManagerImpl>(std::move(link),
std::move(smp),
io_capability,
std::move(delegate),
bondable_mode,
security_mode,
dispatcher);
dispatcher,
std::move(peer));
}

SecurityManager::SecurityManager(BondableMode bondable_mode,
Expand Down
30 changes: 29 additions & 1 deletion pw_bluetooth_sapphire/host/sm/security_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <chrono>
#include <cstdlib>

#include "pw_async/fake_dispatcher.h"
#include "pw_bluetooth_sapphire/internal/host/common/macros.h"
#include "pw_bluetooth_sapphire/internal/host/common/random.h"
#include "pw_bluetooth_sapphire/internal/host/gap/gap.h"
Expand Down Expand Up @@ -47,6 +48,8 @@ const DeviceAddress kLocalAddr(DeviceAddress::Type::kLEPublic,
const DeviceAddress kPeerAddr(DeviceAddress::Type::kLERandom,
{0xB6, 0xB5, 0xB4, 0xB3, 0xB2, 0xB1});

const PeerId kPeerId(2);

const PairingRandomValue kHardCodedPairingRandom = {0x0,
0x1,
0x2,
Expand Down Expand Up @@ -119,13 +122,33 @@ class SecurityManagerTest : public l2cap::testing::FakeChannelTest,
link_role,
transport_->GetWeakPtr());

InitializePeer();

pairing_ = SecurityManager::Create(fake_link_->GetWeakPtr(),
fake_chan_->GetWeakPtr(),
ioc,
weak_delegate_.GetWeakPtr(),
bondable_mode,
gap::LESecurityMode::Mode1,
dispatcher());
dispatcher(),
peer_->GetWeakPtr());
}

void InitializePeer() {
auto listeners_cb = [](const gap::Peer&, gap::Peer::NotifyListenersChange) {
};
auto expiry_cb = [](const gap::Peer&) {};
auto dual_mode_cb = [](const gap::Peer&) {};
auto store_le_bond_cb = [](const sm::PairingData&) { return true; };
peer_.emplace(std::move(listeners_cb),
std::move(expiry_cb),
std::move(dual_mode_cb),
std::move(store_le_bond_cb),
kPeerId,
kPeerAddr,
/*connectable=*/true,
&peer_metrics_,
dispatcher_);
}

void DestroySecurityManager() { pairing_ = nullptr; }
Expand Down Expand Up @@ -535,6 +558,11 @@ class SecurityManagerTest : public l2cap::testing::FakeChannelTest,
hci::DataBufferInfo(1, 1));
}

pw::async::test::FakeDispatcher dispatcher_;

gap::PeerMetrics peer_metrics_;
std::optional<gap::Peer> peer_;

testing::MockController::WeakPtr controller_;
std::unique_ptr<hci::Transport> transport_;

Expand Down
3 changes: 2 additions & 1 deletion pw_bluetooth_sapphire/host/sm/test_security_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ std::unique_ptr<SecurityManager> TestSecurityManagerFactory::CreateSm(
Delegate::WeakPtr delegate,
BondableMode bondable_mode,
gap::LESecurityMode security_mode,
pw::async::Dispatcher& /*dispatcher*/) {
pw::async::Dispatcher& /*dispatcher*/,
gap::Peer::WeakPtr) {
hci_spec::ConnectionHandle conn = link->handle();
auto test_sm = std::unique_ptr<TestSecurityManager>(
new TestSecurityManager(std::move(link),
Expand Down

0 comments on commit 3c65121

Please sign in to comment.