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 <vector>
22 
23 #include "hci/address_with_type.h"
24 #include "hci/le_address_manager.h"
25 #include "security/internal/security_manager_impl.h"
26 #include "security/pairing/oob_data.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   SecurityManager(const SecurityManager&) = delete;
39   SecurityManager& operator=(const SecurityManager&) = delete;
40 
41   friend class SecurityModule;
42 
43   /**
44    * Initialize the security record map from an internal device database.
45    */
46   void Init();
47 
48   /**
49    * Initiates bond over Classic transport with device, if not bonded yet.
50    *
51    * This will initiate the Numeric Comparison bonding method
52    *
53    * @param address device address we want to bond with
54    */
55   void CreateBond(hci::AddressWithType address);
56 
57   /**
58    * Initiates bond over Classic transport with device, if not bonded yet.
59    *
60    * This will initiate the Out of Band bonding method
61    *
62    * @param address device address we want to bond with
63    * @param remote_p192_oob_data comparison and random for p192
64    * @param remote_p256_oob_data comparison and random for p256
65    */
66   void CreateBondOutOfBand(
67       hci::AddressWithType address, pairing::OobData remote_p192_oob_data, pairing::OobData remote_p256_oob_data);
68 
69   /**
70    * Get the out of band data from the controller to send to another device
71    *
72    * @param callback pointer to callback used for notifying that a security HCI command completed
73    */
74   void GetOutOfBandData(channel::SecurityCommandStatusCallback callback);
75 
76   /**
77    * Initiates bond over Low Energy transport with device, if not bonded yet.
78    *
79    * @param address device address we want to bond with
80    */
81   void CreateBondLe(hci::AddressWithType address);
82 
83   /**
84    * Cancels the pairing process for this device.
85    *
86    * @param device pointer to device with which we want to cancel our bond
87    */
88   void CancelBond(hci::AddressWithType device);
89 
90   /**
91    * Disassociates the device and removes the persistent LTK
92    *
93    * @param device pointer to device we want to forget
94    */
95   void RemoveBond(hci::AddressWithType device);
96 
97   /**
98    * Register Security UI handler, for handling prompts around the Pairing process.
99    */
100   void SetUserInterfaceHandler(UI* user_interface, os::Handler* handler);
101 
102   /**
103    * Specify the initiator address policy used for LE transport. Can only be called once.
104    */
105   void SetLeInitiatorAddressPolicyForTest(
106       hci::LeAddressManager::AddressPolicy address_policy,
107       hci::AddressWithType fixed_address,
108       hci::Octet16 rotation_irk,
109       std::chrono::milliseconds minimum_rotation_time,
110       std::chrono::milliseconds maximum_rotation_time);
111 
112   /**
113    * Register to listen for callback events from SecurityManager
114    *
115    * @param listener ISecurityManagerListener instance to handle callbacks
116    */
117   void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler);
118 
119   /**
120    * Unregister listener for callback events from SecurityManager
121    *
122    * @param listener ISecurityManagerListener instance to unregister
123    */
124   void UnregisterCallbackListener(ISecurityManagerListener* listener);
125 
126   void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
127   void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
128   void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override;
129   void OnPinEntry(const bluetooth::hci::AddressWithType& address, std::vector<uint8_t> pin) override;
130 
131  protected:
SecurityManager(os::Handler * security_handler,internal::SecurityManagerImpl * security_manager_impl)132   SecurityManager(os::Handler* security_handler, internal::SecurityManagerImpl* security_manager_impl)
133       : security_handler_(security_handler), security_manager_impl_(security_manager_impl) {}
134 
135  private:
136   os::Handler* security_handler_ = nullptr;
137   internal::SecurityManagerImpl* security_manager_impl_;
138 };
139 
140 }  // namespace security
141 }  // namespace bluetooth
142