1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include "pw_bluetooth_sapphire/internal/host/sm/test_security_manager.h"
15
16 #include <memory>
17
18 #include "pw_bluetooth_sapphire/internal/host/common/assert.h"
19 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
20 #include "pw_bluetooth_sapphire/internal/host/hci/connection.h"
21 #include "pw_bluetooth_sapphire/internal/host/sm/smp.h"
22
23 namespace bt::sm::testing {
24
TestSecurityManager(hci::LowEnergyConnection::WeakPtr link,l2cap::Channel::WeakPtr smp,IOCapability io_capability,Delegate::WeakPtr delegate,BondableMode bondable_mode,gap::LESecurityMode security_mode)25 TestSecurityManager::TestSecurityManager(hci::LowEnergyConnection::WeakPtr link,
26 l2cap::Channel::WeakPtr smp,
27 IOCapability io_capability,
28 Delegate::WeakPtr delegate,
29 BondableMode bondable_mode,
30 gap::LESecurityMode security_mode)
31 : SecurityManager(bondable_mode, security_mode),
32 role_(link->role() == pw::bluetooth::emboss::ConnectionRole::CENTRAL
33 ? Role::kInitiator
34 : Role::kResponder),
35 weak_self_(this) {}
36
AssignLongTermKey(const LTK & ltk)37 bool TestSecurityManager::AssignLongTermKey(const LTK& ltk) {
38 current_ltk_ = ltk;
39 if (role_ == Role::kInitiator) {
40 set_security(ltk.security());
41 }
42 return true;
43 }
44
UpgradeSecurity(SecurityLevel level,PairingCallback callback)45 void TestSecurityManager::UpgradeSecurity(SecurityLevel level,
46 PairingCallback callback) {
47 last_requested_upgrade_ = level;
48 set_security(SecurityProperties(
49 level, kMaxEncryptionKeySize, /*secure_connections=*/true));
50 callback(fit::ok(), security());
51 }
52
Reset(IOCapability io_capability)53 void TestSecurityManager::Reset(IOCapability io_capability) {}
Abort(ErrorCode ecode)54 void TestSecurityManager::Abort(ErrorCode ecode) {}
55
CreateSm(hci::LowEnergyConnection::WeakPtr link,l2cap::Channel::WeakPtr smp,IOCapability io_capability,Delegate::WeakPtr delegate,BondableMode bondable_mode,gap::LESecurityMode security_mode,pw::async::Dispatcher &)56 std::unique_ptr<SecurityManager> TestSecurityManagerFactory::CreateSm(
57 hci::LowEnergyConnection::WeakPtr link,
58 l2cap::Channel::WeakPtr smp,
59 IOCapability io_capability,
60 Delegate::WeakPtr delegate,
61 BondableMode bondable_mode,
62 gap::LESecurityMode security_mode,
63 pw::async::Dispatcher& /*dispatcher*/) {
64 hci_spec::ConnectionHandle conn = link->handle();
65 auto test_sm = std::unique_ptr<TestSecurityManager>(
66 new TestSecurityManager(std::move(link),
67 std::move(smp),
68 io_capability,
69 std::move(delegate),
70 bondable_mode,
71 security_mode));
72 test_sms_[conn] = test_sm->GetWeakPtr();
73 return test_sm;
74 }
75
GetTestSm(hci_spec::ConnectionHandle conn_handle)76 WeakSelf<TestSecurityManager>::WeakPtr TestSecurityManagerFactory::GetTestSm(
77 hci_spec::ConnectionHandle conn_handle) {
78 auto iter = test_sms_.find(conn_handle);
79 BT_ASSERT(iter != test_sms_.end());
80 return iter->second;
81 }
82
83 } // namespace bt::sm::testing
84