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,neighbor::NameDbModule * name_db_module)38   ClassicPairingHandler(
39       channel::SecurityManagerChannel* security_manager_channel,
40       std::shared_ptr<record::SecurityRecord> record,
41       os::Handler* security_handler,
42       common::OnceCallback<void(hci::Address, PairingResultOrFailure)> complete_callback,
43       UI* user_interface,
44       os::Handler* user_interface_handler,
45       std::string device_name,
46       neighbor::NameDbModule* name_db_module)
47       : PairingHandler(security_manager_channel, std::move(record), name_db_module),
48         security_handler_(security_handler),
49         remote_io_capability_(hci::IoCapability::DISPLAY_YES_NO),
50         remote_oob_present_(hci::OobDataPresent::NOT_PRESENT),
51         remote_authentication_requirements_(hci::AuthenticationRequirements::DEDICATED_BONDING_MITM_PROTECTION),
52         local_io_capability_(hci::IoCapability::DISPLAY_YES_NO),
53         local_oob_present_(hci::OobDataPresent::NOT_PRESENT),
54         local_authentication_requirements_(hci::AuthenticationRequirements::DEDICATED_BONDING_MITM_PROTECTION),
55         complete_callback_(std::move(complete_callback)),
56         user_interface_(user_interface),
57         user_interface_handler_(user_interface_handler),
58         device_name_(std::move(device_name)) {}
59 
60   ~ClassicPairingHandler() = default;
61 
62   void Initiate(
63       bool locally_initiated,
64       hci::IoCapability io_capability,
65       hci::AuthenticationRequirements auth_requirements,
66       OobData remote_p192_oob_data,
67       OobData remote_p256_oob_data) override;
68   void Cancel() override;
69 
70   void OnReceive(hci::ChangeConnectionLinkKeyCompleteView packet) override;
71   void OnReceive(hci::CentralLinkKeyCompleteView packet) override;
72   void OnReceive(hci::PinCodeRequestView packet) override;
73   void OnReceive(hci::LinkKeyRequestView packet) override;
74   void OnReceive(hci::LinkKeyNotificationView packet) override;
75   void OnReceive(hci::IoCapabilityRequestView packet) override;
76   void OnReceive(hci::IoCapabilityResponseView packet) override;
77   void OnReceive(hci::SimplePairingCompleteView packet) override;
78   void OnReceive(hci::ReturnLinkKeysView packet) override;
79   void OnReceive(hci::EncryptionChangeView packet) override;
80   void OnReceive(hci::EncryptionKeyRefreshCompleteView packet) override;
81   void OnReceive(hci::RemoteOobDataRequestView packet) override;
82   void OnReceive(hci::UserPasskeyNotificationView packet) override;
83   void OnReceive(hci::KeypressNotificationView packet) override;
84   void OnReceive(hci::UserConfirmationRequestView packet) override;
85   void OnReceive(hci::UserPasskeyRequestView packet) override;
86 
87   void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
88   void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) override;
89   void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override;
90   void OnPinEntry(const bluetooth::hci::AddressWithType& address, std::vector<uint8_t> pin) override;
91 
92   void OnNameRequestComplete(hci::Address address, bool success);
93 
94  private:
95   void OnUserInput(bool user_input);
96   void OnPasskeyInput(uint32_t passkey);
97   void NotifyUiDisplayYesNo(uint32_t numeric_value);
98   void NotifyUiDisplayYesNo();
99   void NotifyUiDisplayPasskey(uint32_t passkey);
100   void NotifyUiDisplayPasskeyInput();
101   void NotifyUiDisplayPinCodeInput();
102   void NotifyUiDisplayCancel();
103   void UserClickedYes();
104   void UserClickedNo();
105 
106   os::Handler* security_handler_ __attribute__((unused));
107   hci::IoCapability remote_io_capability_;
108   hci::OobDataPresent remote_oob_present_ __attribute__((unused));
109   hci::AuthenticationRequirements remote_authentication_requirements_ __attribute__((unused));
110   hci::IoCapability local_io_capability_;
111   hci::OobDataPresent local_oob_present_ __attribute__((unused));
112   hci::AuthenticationRequirements local_authentication_requirements_ __attribute__((unused));
113   OobData remote_p192_oob_data_;
114   OobData remote_p256_oob_data_;
115   common::OnceCallback<void(hci::Address, PairingResultOrFailure)> complete_callback_;
116   UI* user_interface_;
117   os::Handler* user_interface_handler_;
118   std::string device_name_;
119   bool is_cancelled_ = false;
120 
121   bool has_gotten_io_cap_response_ = false;
122   bool has_gotten_name_response_ = false;
123   std::optional<hci::UserConfirmationRequestView> user_confirmation_request_ = std::nullopt;
124   std::optional<hci::LinkKeyNotificationView> link_key_notification_ = std::nullopt;
125 
126   hci::ErrorCode last_status_ = hci::ErrorCode::UNKNOWN_HCI_COMMAND;
127   bool locally_initiated_ = false;
128   uint32_t passkey_ = 0;
129   bool already_link_key_replied_ = false;
130   bool secure_connections_enabled_ = true;
131   bool is_legacy_pin_code_ = false;
132 };
133 
134 }  // namespace pairing
135 }  // namespace security
136 }  // namespace bluetooth
137