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 <memory>
20 #include "module.h"
21 #include "os/handler.h"
22 #include "os/log.h"
23 
24 #include "hci/acl_manager.h"
25 #include "hci/hci_layer.h"
26 #include "l2cap/le/l2cap_le_module.h"
27 #include "security/channel/security_manager_channel.h"
28 #include "security/facade_configuration_api.h"
29 #include "security/internal/security_manager_impl.h"
30 #include "security/l2cap_security_module_interface.h"
31 #include "security/security_module.h"
32 #include "storage/storage_module.h"
33 
34 namespace bluetooth {
35 namespace security {
36 
__anonb56d472a0102() 37 const ModuleFactory SecurityModule::Factory = ModuleFactory([]() { return new SecurityModule(); });
38 
39 struct SecurityModule::impl {
implbluetooth::security::SecurityModule::impl40   impl(
41       os::Handler* security_handler,
42       l2cap::le::L2capLeModule* l2cap_le_module,
43       l2cap::classic::L2capClassicModule* l2cap_classic_module,
44       hci::HciLayer* hci_layer,
45       hci::AclManager* acl_manager,
46       storage::StorageModule* storage_module)
47       : security_handler_(security_handler),
48         l2cap_classic_module_(l2cap_classic_module),
49         l2cap_le_module_(l2cap_le_module),
50         security_manager_channel_(new channel::SecurityManagerChannel(security_handler_, hci_layer)),
51         hci_layer_(hci_layer),
52         acl_manager_(acl_manager),
53         storage_module_(storage_module),
54         l2cap_security_interface_(&security_manager_impl, security_handler) {
55     l2cap_classic_module->InjectSecurityEnforcementInterface(&l2cap_security_interface_);
56     l2cap_le_module->InjectSecurityEnforcementInterface(&l2cap_security_interface_);
57     security_manager_channel_->SetSecurityInterface(
58         l2cap_classic_module->GetSecurityInterface(security_handler_, security_manager_channel_));
59   }
60 
61   os::Handler* security_handler_;
62   l2cap::classic::L2capClassicModule* l2cap_classic_module_;
63   l2cap::le::L2capLeModule* l2cap_le_module_;
64   channel::SecurityManagerChannel* security_manager_channel_;
65   hci::HciLayer* hci_layer_;
66   hci::AclManager* acl_manager_;
67   storage::StorageModule* storage_module_;
68   L2capSecurityModuleInterface l2cap_security_interface_;
69 
70   internal::SecurityManagerImpl security_manager_impl{
71       security_handler_, l2cap_le_module_, security_manager_channel_, hci_layer_, acl_manager_, storage_module_};
72 
~implbluetooth::security::SecurityModule::impl73   ~impl() {
74     delete security_manager_channel_;
75     l2cap_classic_module_->InjectSecurityEnforcementInterface(nullptr);
76     l2cap_le_module_->InjectSecurityEnforcementInterface(nullptr);
77   }
78 };
79 
ListDependencies(ModuleList * list)80 void SecurityModule::ListDependencies(ModuleList* list) {
81   list->add<l2cap::le::L2capLeModule>();
82   list->add<l2cap::classic::L2capClassicModule>();
83   list->add<hci::HciLayer>();
84   list->add<hci::AclManager>();
85   list->add<storage::StorageModule>();
86 }
87 
Start()88 void SecurityModule::Start() {
89   pimpl_ = std::make_unique<impl>(
90       GetHandler(),
91       GetDependency<l2cap::le::L2capLeModule>(),
92       GetDependency<l2cap::classic::L2capClassicModule>(),
93       GetDependency<hci::HciLayer>(),
94       GetDependency<hci::AclManager>(),
95       GetDependency<storage::StorageModule>());
96 
97   GetDependency<hci::AclManager>()->SetSecurityModule(this);
98 }
99 
Stop()100 void SecurityModule::Stop() {
101   pimpl_.reset();
102 }
103 
ToString() const104 std::string SecurityModule::ToString() const {
105   return "Security Module";
106 }
107 
GetSecurityManager()108 std::unique_ptr<SecurityManager> SecurityModule::GetSecurityManager() {
109   return std::unique_ptr<SecurityManager>(
110       new SecurityManager(pimpl_->security_handler_, &pimpl_->security_manager_impl));
111 }
112 
GetFacadeConfigurationApi()113 std::unique_ptr<FacadeConfigurationApi> SecurityModule::GetFacadeConfigurationApi() {
114   return std::unique_ptr<FacadeConfigurationApi>(
115       new FacadeConfigurationApi(pimpl_->security_handler_, &pimpl_->security_manager_impl));
116 }
117 
118 }  // namespace security
119 }  // namespace bluetooth