1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "security"
18 
19 #include "security/security_module.h"
20 
21 #include <memory>
22 
23 #include "hci/acl_manager.h"
24 #include "hci/hci_layer.h"
25 #include "l2cap/classic/l2cap_classic_module.h"
26 #include "l2cap/le/l2cap_le_module.h"
27 #include "module.h"
28 #include "neighbor/name_db.h"
29 #include "os/handler.h"
30 #include "security/channel/security_manager_channel.h"
31 #include "security/facade_configuration_api.h"
32 #include "security/internal/security_manager_impl.h"
33 #include "security/l2cap_security_module_interface.h"
34 #include "storage/storage_module.h"
35 
36 namespace bluetooth {
37 namespace security {
38 
__anon3465d3400102() 39 const ModuleFactory SecurityModule::Factory = ModuleFactory([]() { return new SecurityModule(); });
40 
41 struct SecurityModule::impl {
implbluetooth::security::SecurityModule::impl42   impl(
43       os::Handler* security_handler,
44       l2cap::le::L2capLeModule* l2cap_le_module,
45       l2cap::classic::L2capClassicModule* l2cap_classic_module,
46       hci::HciLayer* hci_layer,
47       hci::AclManager* acl_manager,
48       hci::Controller* controller,
49       storage::StorageModule* storage_module,
50       neighbor::NameDbModule* name_db_module)
51       : security_handler_(security_handler),
52         l2cap_classic_module_(l2cap_classic_module),
53         l2cap_le_module_(l2cap_le_module),
54         security_manager_channel_(new channel::SecurityManagerChannel(security_handler_, hci_layer)),
55         hci_layer_(hci_layer),
56         acl_manager_(acl_manager),
57         controller_(controller),
58         storage_module_(storage_module),
59         l2cap_security_interface_(&security_manager_impl, security_handler),
60         name_db_module_(name_db_module) {
61     l2cap_classic_module->InjectSecurityEnforcementInterface(&l2cap_security_interface_);
62     l2cap_le_module->InjectSecurityEnforcementInterface(&l2cap_security_interface_);
63     security_manager_channel_->SetSecurityInterface(
64         l2cap_classic_module->GetSecurityInterface(security_handler_, security_manager_channel_));
65   }
66 
67   os::Handler* security_handler_;
68   l2cap::classic::L2capClassicModule* l2cap_classic_module_;
69   l2cap::le::L2capLeModule* l2cap_le_module_;
70   channel::SecurityManagerChannel* security_manager_channel_;
71   hci::HciLayer* hci_layer_;
72   hci::AclManager* acl_manager_;
73   hci::Controller* controller_;
74   storage::StorageModule* storage_module_;
75   L2capSecurityModuleInterface l2cap_security_interface_;
76   neighbor::NameDbModule* name_db_module_;
77 
78   internal::SecurityManagerImpl security_manager_impl{security_handler_,
79                                                       l2cap_le_module_,
80                                                       security_manager_channel_,
81                                                       hci_layer_,
82                                                       acl_manager_,
83                                                       controller_,
84                                                       storage_module_,
85                                                       name_db_module_};
86 
~implbluetooth::security::SecurityModule::impl87   ~impl() {
88     delete security_manager_channel_;
89     l2cap_classic_module_->InjectSecurityEnforcementInterface(nullptr);
90     l2cap_le_module_->InjectSecurityEnforcementInterface(nullptr);
91   }
92 };
93 
ListDependencies(ModuleList * list) const94 void SecurityModule::ListDependencies(ModuleList* list) const {
95   list->add<l2cap::le::L2capLeModule>();
96   list->add<l2cap::classic::L2capClassicModule>();
97   list->add<hci::HciLayer>();
98   list->add<hci::AclManager>();
99   list->add<hci::Controller>();
100   list->add<storage::StorageModule>();
101   list->add<neighbor::NameDbModule>();
102 }
103 
Start()104 void SecurityModule::Start() {
105   pimpl_ = std::make_unique<impl>(
106       GetHandler(),
107       GetDependency<l2cap::le::L2capLeModule>(),
108       GetDependency<l2cap::classic::L2capClassicModule>(),
109       GetDependency<hci::HciLayer>(),
110       GetDependency<hci::AclManager>(),
111       GetDependency<hci::Controller>(),
112       GetDependency<storage::StorageModule>(),
113       GetDependency<neighbor::NameDbModule>());
114 }
115 
Stop()116 void SecurityModule::Stop() {
117   pimpl_.reset();
118 }
119 
ToString() const120 std::string SecurityModule::ToString() const {
121   return "Security Module";
122 }
123 
GetSecurityManager()124 std::unique_ptr<SecurityManager> SecurityModule::GetSecurityManager() {
125   return std::unique_ptr<SecurityManager>(
126       new SecurityManager(pimpl_->security_handler_, &pimpl_->security_manager_impl));
127 }
128 
GetFacadeConfigurationApi()129 std::unique_ptr<FacadeConfigurationApi> SecurityModule::GetFacadeConfigurationApi() {
130   return std::unique_ptr<FacadeConfigurationApi>(
131       new FacadeConfigurationApi(pimpl_->security_handler_, &pimpl_->security_manager_impl));
132 }
133 
134 }  // namespace security
135 }  // namespace bluetooth
136