1 /*
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License") override;
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 #pragma once
19 
20 #include "security/pairing/pairing_handler.h"
21 
22 #include <utility>
23 
24 #include "common/callback.h"
25 #include "l2cap/classic/l2cap_classic_module.h"
26 #include "security/initial_informations.h"
27 #include "security/security_manager_listener.h"
28 
29 namespace bluetooth {
30 namespace security {
31 
32 class ISecurityManagerListener;
33 
34 namespace pairing {
35 
36 class ClassicPairingHandler : public PairingHandler {
37  public:
ClassicPairingHandler(channel::SecurityManagerChannel * security_manager_channel,std::shared_ptr<record::SecurityRecord> record,os::Handler * security_handler,common::OnceCallback<void (hci::Address,PairingResultOrFailure)> complete_callback,UI * user_interface,os::Handler * user_interface_handler,std::string device_name)38   ClassicPairingHandler(channel::SecurityManagerChannel* security_manager_channel,
39                         std::shared_ptr<record::SecurityRecord> record, os::Handler* security_handler,
40                         common::OnceCallback<void(hci::Address, PairingResultOrFailure)> complete_callback,
41                         UI* user_interface, os::Handler* user_interface_handler, std::string device_name)
42       : PairingHandler(security_manager_channel, std::move(record)), security_handler_(security_handler),
43         remote_io_capability_(hci::IoCapability::DISPLAY_YES_NO), remote_oob_present_(hci::OobDataPresent::NOT_PRESENT),
44         remote_authentication_requirements_(hci::AuthenticationRequirements::DEDICATED_BONDING_MITM_PROTECTION),
45         local_io_capability_(hci::IoCapability::DISPLAY_YES_NO), local_oob_present_(hci::OobDataPresent::NOT_PRESENT),
46         local_authentication_requirements_(hci::AuthenticationRequirements::DEDICATED_BONDING_MITM_PROTECTION),
47         complete_callback_(std::move(complete_callback)), user_interface_(user_interface),
48         user_interface_handler_(user_interface_handler), device_name_(std::move(device_name)) {}
49 
50   ~ClassicPairingHandler() override = default;
51 
52   void Initiate(bool locally_initiated, hci::IoCapability io_capability, hci::OobDataPresent oob_present,
53                 hci::AuthenticationRequirements auth_requirements) override;
54   void Cancel() override;
55 
56   void OnReceive(hci::ChangeConnectionLinkKeyCompleteView packet) override;
57   void OnReceive(hci::MasterLinkKeyCompleteView packet) override;
58   void OnReceive(hci::PinCodeRequestView packet) override;
59   void OnReceive(hci::LinkKeyRequestView packet) override;
60   void OnReceive(hci::LinkKeyNotificationView packet) override;
61   void OnReceive(hci::IoCapabilityRequestView packet) override;
62   void OnReceive(hci::IoCapabilityResponseView packet) override;
63   void OnReceive(hci::SimplePairingCompleteView packet) override;
64   void OnReceive(hci::ReturnLinkKeysView packet) override;
65   void OnReceive(hci::EncryptionChangeView packet) override;
66   void OnReceive(hci::EncryptionKeyRefreshCompleteView packet) override;
67   void OnReceive(hci::RemoteOobDataRequestView packet) override;
68   void OnReceive(hci::UserPasskeyNotificationView packet) override;
69   void OnReceive(hci::KeypressNotificationView packet) override;
70   void OnReceive(hci::UserConfirmationRequestView packet) override;
71   void OnReceive(hci::UserPasskeyRequestView packet) override;
72 
73   void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
74   void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
75   void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override;
76 
77  private:
78   void OnUserInput(bool user_input);
79   void OnPasskeyInput(uint32_t passkey);
80   void NotifyUiDisplayYesNo(uint32_t numeric_value);
81   void NotifyUiDisplayYesNo();
82   void NotifyUiDisplayPasskey(uint32_t passkey);
83   void NotifyUiDisplayPasskeyInput();
84   void NotifyUiDisplayCancel();
85   void UserClickedYes();
86   void UserClickedNo();
87 
88   os::Handler* security_handler_ __attribute__((unused));
89   hci::IoCapability remote_io_capability_;
90   hci::OobDataPresent remote_oob_present_ __attribute__((unused));
91   hci::AuthenticationRequirements remote_authentication_requirements_ __attribute__((unused));
92   hci::IoCapability local_io_capability_;
93   hci::OobDataPresent local_oob_present_ __attribute__((unused));
94   hci::AuthenticationRequirements local_authentication_requirements_ __attribute__((unused));
95   common::OnceCallback<void(hci::Address, PairingResultOrFailure)> complete_callback_;
96   UI* user_interface_;
97   os::Handler* user_interface_handler_;
98   std::string device_name_;
99   bool is_cancelled_ = false;
100 
101   hci::ErrorCode last_status_ = hci::ErrorCode::UNKNOWN_HCI_COMMAND;
102   bool locally_initiated_ = false;
103   uint32_t passkey_ = 0;
104   bool already_link_key_replied_ = false;
105 };
106 
107 }  // namespace pairing
108 }  // namespace security
109 }  // namespace bluetooth
110