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