1 /*
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 #pragma once
20 
21 #include <memory>
22 #include <vector>
23 
24 #include "hci/address_with_type.h"
25 #include "hci/le_address_manager.h"
26 #include "security/internal/security_manager_impl.h"
27 #include "security/security_manager_listener.h"
28 
29 namespace bluetooth {
30 namespace security {
31 
32 /**
33  * Manages the security attributes, pairing, bonding of devices, and the
34  * encryption/decryption of communications.
35  */
36 class SecurityManager : public UICallbacks {
37  public:
38   friend class SecurityModule;
39 
40   /**
41    * Initialize the security record map from an internal device database.
42    */
43   void Init();
44 
45   /**
46    * Initiates bond over Classic transport with device, if not bonded yet.
47    *
48    * @param address device address we want to bond with
49    */
50   void CreateBond(hci::AddressWithType address);
51 
52   /**
53    * Initiates bond over Low Energy transport with device, if not bonded yet.
54    *
55    * @param address device address we want to bond with
56    */
57   void CreateBondLe(hci::AddressWithType address);
58 
59   /**
60    * Cancels the pairing process for this device.
61    *
62    * @param device pointer to device with which we want to cancel our bond
63    */
64   void CancelBond(hci::AddressWithType device);
65 
66   /**
67    * Disassociates the device and removes the persistent LTK
68    *
69    * @param device pointer to device we want to forget
70    */
71   void RemoveBond(hci::AddressWithType device);
72 
73   /**
74    * Register Security UI handler, for handling prompts around the Pairing process.
75    */
76   void SetUserInterfaceHandler(UI* user_interface, os::Handler* handler);
77 
78   /**
79    * Specify the initiator address policy used for LE transport. Can only be called once.
80    */
81   void SetLeInitiatorAddressPolicyForTest(
82       hci::LeAddressManager::AddressPolicy address_policy,
83       hci::AddressWithType fixed_address,
84       crypto_toolbox::Octet16 rotation_irk,
85       std::chrono::milliseconds minimum_rotation_time,
86       std::chrono::milliseconds maximum_rotation_time);
87 
88   /**
89    * Register to listen for callback events from SecurityManager
90    *
91    * @param listener ISecurityManagerListener instance to handle callbacks
92    */
93   void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler);
94 
95   /**
96    * Unregister listener for callback events from SecurityManager
97    *
98    * @param listener ISecurityManagerListener instance to unregister
99    */
100   void UnregisterCallbackListener(ISecurityManagerListener* listener);
101 
102   void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
103   void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
104   void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override;
105 
106  protected:
SecurityManager(os::Handler * security_handler,internal::SecurityManagerImpl * security_manager_impl)107   SecurityManager(os::Handler* security_handler, internal::SecurityManagerImpl* security_manager_impl)
108       : security_handler_(security_handler), security_manager_impl_(security_manager_impl) {}
109 
110  private:
111   os::Handler* security_handler_ = nullptr;
112   internal::SecurityManagerImpl* security_manager_impl_;
113   DISALLOW_COPY_AND_ASSIGN(SecurityManager);
114 };
115 
116 }  // namespace security
117 }  // namespace bluetooth
118