Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 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 | #pragma once |
| 18 | |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 19 | #include <unordered_map> |
| 20 | #include <utility> |
| 21 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 22 | #include "hci/classic_device.h" |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 23 | #include "l2cap/classic/l2cap_classic_module.h" |
| 24 | #include "l2cap/le/l2cap_le_module.h" |
| 25 | #include "os/handler.h" |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 26 | #include "security/channel/security_manager_channel.h" |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 27 | #include "security/pairing/classic_pairing_handler.h" |
| 28 | #include "security/record/security_record.h" |
Jakub Pawlowski | 24d5f35 | 2019-11-18 18:54:09 +0100 | [diff] [blame] | 29 | |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 30 | namespace bluetooth { |
| 31 | namespace security { |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 32 | |
| 33 | class ISecurityManagerListener; |
| 34 | |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 35 | namespace internal { |
| 36 | |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 37 | class SecurityManagerImpl : public channel::ISecurityManagerChannelListener { |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 38 | public: |
| 39 | explicit SecurityManagerImpl(os::Handler* security_handler, l2cap::le::L2capLeModule* l2cap_le_module, |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 40 | l2cap::classic::L2capClassicModule* l2cap_classic_module, |
| 41 | channel::SecurityManagerChannel* security_manager_channel) |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 42 | : security_handler_(security_handler), l2cap_le_module_(l2cap_le_module), |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 43 | l2cap_classic_module_(l2cap_classic_module), security_manager_channel_(security_manager_channel) {} |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 44 | virtual ~SecurityManagerImpl() = default; |
| 45 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 46 | // All APIs must be invoked in SM layer handler |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 47 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Initialize the security record map from an internal device database. |
| 50 | */ |
| 51 | void Init(); |
| 52 | |
| 53 | /** |
| 54 | * Checks the device for existing bond, if not bonded, initiates pairing. |
| 55 | * |
| 56 | * @param device pointer to device we want to bond with |
| 57 | * @return true if bonded or pairing started successfully, false if currently pairing |
| 58 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 59 | void CreateBond(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 60 | |
| 61 | /* void CreateBond(std::shared_ptr<hci::LeDevice> device); */ |
| 62 | |
| 63 | /** |
| 64 | * Cancels the pairing process for this device. |
| 65 | * |
| 66 | * @param device pointer to device with which we want to cancel our bond |
| 67 | * @return <code>true</code> if successfully stopped |
| 68 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 69 | void CancelBond(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 70 | |
| 71 | /* void CancelBond(std::shared_ptr<hci::LeDevice> device); */ |
| 72 | |
| 73 | /** |
| 74 | * Disassociates the device and removes the persistent LTK |
| 75 | * |
| 76 | * @param device pointer to device we want to forget |
| 77 | * @return true if removed |
| 78 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 79 | void RemoveBond(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 80 | |
| 81 | /* void RemoveBond(std::shared_ptr<hci::LeDevice> device); */ |
| 82 | |
| 83 | /** |
| 84 | * Register to listen for callback events from SecurityManager |
| 85 | * |
| 86 | * @param listener ISecurityManagerListener instance to handle callbacks |
| 87 | */ |
Jakub Pawlowski | 24d5f35 | 2019-11-18 18:54:09 +0100 | [diff] [blame] | 88 | void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Unregister listener for callback events from SecurityManager |
| 92 | * |
| 93 | * @param listener ISecurityManagerListener instance to unregister |
| 94 | */ |
| 95 | void UnregisterCallbackListener(ISecurityManagerListener* listener); |
| 96 | |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 97 | // ISecurityManagerChannel |
| 98 | void OnHciEventReceived(hci::EventPacketView packet) override; |
| 99 | |
| 100 | void OnPairingHandlerComplete(hci::Address address); |
| 101 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 102 | protected: |
Jakub Pawlowski | 24d5f35 | 2019-11-18 18:54:09 +0100 | [diff] [blame] | 103 | std::vector<std::pair<ISecurityManagerListener*, os::Handler*>> listeners_; |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 104 | void NotifyDeviceBonded(hci::AddressWithType device); |
| 105 | void NotifyDeviceBondFailed(hci::AddressWithType device); |
| 106 | void NotifyDeviceUnbonded(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 107 | |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 108 | private: |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 109 | template <class T> |
| 110 | void HandleEvent(T packet); |
| 111 | |
| 112 | std::shared_ptr<record::SecurityRecord> CreateSecurityRecord(hci::Address address); |
| 113 | void DispatchPairingHandler(std::shared_ptr<record::SecurityRecord> record, bool locally_initiated); |
| 114 | |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 115 | os::Handler* security_handler_ __attribute__((unused)); |
| 116 | l2cap::le::L2capLeModule* l2cap_le_module_ __attribute__((unused)); |
| 117 | l2cap::classic::L2capClassicModule* l2cap_classic_module_ __attribute__((unused)); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 118 | channel::SecurityManagerChannel* security_manager_channel_ __attribute__((unused)); |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 119 | std::unordered_map<hci::Address, std::shared_ptr<record::SecurityRecord>> security_record_map_; |
| 120 | std::unordered_map<hci::Address, std::shared_ptr<pairing::PairingHandler>> pairing_handler_map_; |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 121 | }; |
| 122 | } // namespace internal |
| 123 | } // namespace security |
| 124 | } // namespace bluetooth |