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