blob: fc4393540b0fec6c257818181bde9379566bd0aa [file] [log] [blame]
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +02001/*
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 Brabham80854c22019-11-12 14:52:42 -080019#include <unordered_map>
20#include <utility>
21
Martin Brabham605d6f12019-03-29 12:02:30 -070022#include "hci/classic_device.h"
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020023#include "l2cap/classic/l2cap_classic_module.h"
24#include "l2cap/le/l2cap_le_module.h"
25#include "os/handler.h"
Martin Brabham605d6f12019-03-29 12:02:30 -070026#include "security/channel/security_manager_channel.h"
Martin Brabham80854c22019-11-12 14:52:42 -080027#include "security/pairing/classic_pairing_handler.h"
28#include "security/record/security_record.h"
Jakub Pawlowski24d5f352019-11-18 18:54:09 +010029
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020030namespace bluetooth {
31namespace security {
Jakub Pawlowski5a67e402019-11-18 16:14:22 +010032
33class ISecurityManagerListener;
34
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020035namespace internal {
36
Martin Brabham80854c22019-11-12 14:52:42 -080037class SecurityManagerImpl : public channel::ISecurityManagerChannelListener {
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020038 public:
39 explicit SecurityManagerImpl(os::Handler* security_handler, l2cap::le::L2capLeModule* l2cap_le_module,
Martin Brabham605d6f12019-03-29 12:02:30 -070040 l2cap::classic::L2capClassicModule* l2cap_classic_module,
41 channel::SecurityManagerChannel* security_manager_channel)
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020042 : security_handler_(security_handler), l2cap_le_module_(l2cap_le_module),
Martin Brabham605d6f12019-03-29 12:02:30 -070043 l2cap_classic_module_(l2cap_classic_module), security_manager_channel_(security_manager_channel) {}
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020044 virtual ~SecurityManagerImpl() = default;
45
Martin Brabham605d6f12019-03-29 12:02:30 -070046 // All APIs must be invoked in SM layer handler
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +020047
Martin Brabham605d6f12019-03-29 12:02:30 -070048 /**
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 Brabham80854c22019-11-12 14:52:42 -080059 void CreateBond(hci::AddressWithType device);
Martin Brabham605d6f12019-03-29 12:02:30 -070060
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 Brabham80854c22019-11-12 14:52:42 -080069 void CancelBond(hci::AddressWithType device);
Martin Brabham605d6f12019-03-29 12:02:30 -070070
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 Brabham80854c22019-11-12 14:52:42 -080079 void RemoveBond(hci::AddressWithType device);
Martin Brabham605d6f12019-03-29 12:02:30 -070080
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 Pawlowski24d5f352019-11-18 18:54:09 +010088 void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler);
Martin Brabham605d6f12019-03-29 12:02:30 -070089
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 Brabham80854c22019-11-12 14:52:42 -080097 // ISecurityManagerChannel
98 void OnHciEventReceived(hci::EventPacketView packet) override;
99
100 void OnPairingHandlerComplete(hci::Address address);
101
Martin Brabham605d6f12019-03-29 12:02:30 -0700102 protected:
Jakub Pawlowski24d5f352019-11-18 18:54:09 +0100103 std::vector<std::pair<ISecurityManagerListener*, os::Handler*>> listeners_;
Jakub Pawlowskid2c82f52019-11-22 15:33:12 +0100104 void NotifyDeviceBonded(hci::AddressWithType device);
105 void NotifyDeviceBondFailed(hci::AddressWithType device);
106 void NotifyDeviceUnbonded(hci::AddressWithType device);
Martin Brabham605d6f12019-03-29 12:02:30 -0700107
Jakub Pawlowskifa057bd2019-10-10 14:36:29 +0200108 private:
Martin Brabham80854c22019-11-12 14:52:42 -0800109 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 Pawlowskifa057bd2019-10-10 14:36:29 +0200115 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 Brabham605d6f12019-03-29 12:02:30 -0700118 channel::SecurityManagerChannel* security_manager_channel_ __attribute__((unused));
Martin Brabham80854c22019-11-12 14:52:42 -0800119 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 Pawlowskifa057bd2019-10-10 14:36:29 +0200121};
122} // namespace internal
123} // namespace security
124} // namespace bluetooth