Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame] | 1 | /* |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 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 | * |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame] | 17 | */ |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 18 | |
| 19 | #pragma once |
| 20 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 24 | #include "hci/address_with_type.h" |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 25 | #include "security/internal/security_manager_impl.h" |
| 26 | |
| 27 | namespace bluetooth { |
| 28 | namespace security { |
| 29 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 30 | /** |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 31 | * Callback interface from SecurityManager. |
| 32 | */ |
| 33 | class ISecurityManagerListener { |
| 34 | public: |
Jakub Pawlowski | af9379b | 2019-11-18 17:51:13 +0100 | [diff] [blame] | 35 | virtual ~ISecurityManagerListener() = 0; |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Called when a device is successfully bonded. |
| 39 | * |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 40 | * @param address of the newly bonded device |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 41 | */ |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 42 | virtual void OnDeviceBonded(bluetooth::hci::AddressWithType device) = 0; |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Called when a device is successfully un-bonded. |
| 46 | * |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 47 | * @param address of device that is no longer bonded |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 48 | */ |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 49 | virtual void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) = 0; |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Called as a result of a failure during the bonding process. |
| 53 | * |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 54 | * @param address of the device that failed to bond |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 55 | */ |
Jakub Pawlowski | d2c82f5 | 2019-11-22 15:33:12 +0100 | [diff] [blame] | 56 | virtual void OnDeviceBondFailed(bluetooth::hci::AddressWithType device) = 0; |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /** |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 60 | * Manages the security attributes, pairing, bonding of devices, and the |
| 61 | * encryption/decryption of communications. |
| 62 | */ |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 63 | class SecurityManager { |
| 64 | public: |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 65 | friend class SecurityModule; |
| 66 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Initialize the security record map from an internal device database. |
| 69 | */ |
| 70 | void Init(); |
| 71 | |
| 72 | /** |
| 73 | * Checks the device for existing bond, if not bonded, initiates pairing. |
| 74 | * |
| 75 | * @param device pointer to device we want to bond with |
| 76 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame] | 77 | void CreateBond(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Cancels the pairing process for this device. |
| 81 | * |
| 82 | * @param device pointer to device with which we want to cancel our bond |
| 83 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame] | 84 | void CancelBond(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Disassociates the device and removes the persistent LTK |
| 88 | * |
| 89 | * @param device pointer to device we want to forget |
| 90 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame] | 91 | void RemoveBond(hci::AddressWithType device); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Register to listen for callback events from SecurityManager |
| 95 | * |
| 96 | * @param listener ISecurityManagerListener instance to handle callbacks |
| 97 | */ |
Jakub Pawlowski | 24d5f35 | 2019-11-18 18:54:09 +0100 | [diff] [blame] | 98 | void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Unregister listener for callback events from SecurityManager |
| 102 | * |
| 103 | * @param listener ISecurityManagerListener instance to unregister |
| 104 | */ |
Jakub Pawlowski | 5a67e40 | 2019-11-18 16:14:22 +0100 | [diff] [blame] | 105 | void UnregisterCallbackListener(ISecurityManagerListener* listener); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 106 | |
| 107 | protected: |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 108 | SecurityManager(os::Handler* security_handler, internal::SecurityManagerImpl* security_manager_impl) |
| 109 | : security_handler_(security_handler), security_manager_impl_(security_manager_impl) {} |
| 110 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 111 | private: |
Jakub Pawlowski | fa057bd | 2019-10-10 14:36:29 +0200 | [diff] [blame] | 112 | os::Handler* security_handler_ = nullptr; |
| 113 | internal::SecurityManagerImpl* security_manager_impl_; |
| 114 | DISALLOW_COPY_AND_ASSIGN(SecurityManager); |
| 115 | }; |
| 116 | |
| 117 | } // namespace security |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 118 | } // namespace bluetooth |