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