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 static constexpr hci::IoCapability kDefaultIoCapability = hci::IoCapability::DISPLAY_YES_NO; 37 static constexpr hci::OobDataPresent kDefaultOobDataPresent = hci::OobDataPresent::NOT_PRESENT; 38 static constexpr hci::AuthenticationRequirements kDefaultAuthenticationRequirements = 39 hci::AuthenticationRequirements::DEDICATED_BONDING_MITM_PROTECTION; 40 41 class ClassicPairingHandler : public PairingHandler { 42 public: ClassicPairingHandler(std::shared_ptr<l2cap::classic::FixedChannelManager> fixed_channel_manager,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)43 ClassicPairingHandler(std::shared_ptr<l2cap::classic::FixedChannelManager> fixed_channel_manager, 44 channel::SecurityManagerChannel* security_manager_channel, 45 std::shared_ptr<record::SecurityRecord> record, os::Handler* security_handler, 46 common::OnceCallback<void(hci::Address, PairingResultOrFailure)> complete_callback, 47 UI* user_interface, os::Handler* user_interface_handler, std::string device_name) 48 : PairingHandler(security_manager_channel, std::move(record)), 49 fixed_channel_manager_(std::move(fixed_channel_manager)), security_policy_(), 50 security_handler_(security_handler), remote_io_capability_(kDefaultIoCapability), 51 local_io_capability_(kDefaultIoCapability), local_oob_present_(kDefaultOobDataPresent), 52 local_authentication_requirements_(kDefaultAuthenticationRequirements), 53 complete_callback_(std::move(complete_callback)), user_interface_(user_interface), 54 user_interface_handler_(user_interface_handler), device_name_(device_name) {} 55 56 ~ClassicPairingHandler() override = default; 57 58 void Initiate(bool locally_initiated, hci::IoCapability io_capability, hci::OobDataPresent oob_present, 59 hci::AuthenticationRequirements auth_requirements) override; 60 void Cancel() override; 61 62 void OnReceive(hci::ChangeConnectionLinkKeyCompleteView packet) override; 63 void OnReceive(hci::MasterLinkKeyCompleteView packet) override; 64 void OnReceive(hci::PinCodeRequestView packet) override; 65 void OnReceive(hci::LinkKeyRequestView packet) override; 66 void OnReceive(hci::LinkKeyNotificationView packet) override; 67 void OnReceive(hci::IoCapabilityRequestView packet) override; 68 void OnReceive(hci::IoCapabilityResponseView packet) override; 69 void OnReceive(hci::SimplePairingCompleteView packet) override; 70 void OnReceive(hci::ReturnLinkKeysView packet) override; 71 void OnReceive(hci::EncryptionChangeView packet) override; 72 void OnReceive(hci::EncryptionKeyRefreshCompleteView packet) override; 73 void OnReceive(hci::RemoteOobDataRequestView packet) override; 74 void OnReceive(hci::UserPasskeyNotificationView packet) override; 75 void OnReceive(hci::KeypressNotificationView packet) override; 76 void OnReceive(hci::UserConfirmationRequestView packet) override; 77 void OnReceive(hci::UserPasskeyRequestView packet) override; 78 79 void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) override; 80 void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) override; 81 void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override; 82 83 private: 84 void OnRegistrationComplete(l2cap::classic::FixedChannelManager::RegistrationResult result, 85 std::unique_ptr<l2cap::classic::FixedChannelService> fixed_channel_service); 86 void OnUnregistered(); 87 void OnConnectionOpen(std::unique_ptr<l2cap::classic::FixedChannel> fixed_channel); 88 void OnConnectionFail(l2cap::classic::FixedChannelManager::ConnectionResult result); 89 void OnConnectionClose(hci::ErrorCode error_code); 90 void OnUserInput(bool user_input); 91 void OnPasskeyInput(uint32_t passkey); 92 void NotifyUiDisplayYesNo(uint32_t numeric_value); 93 void NotifyUiDisplayYesNo(); 94 void NotifyUiDisplayPasskey(uint32_t passkey); 95 void NotifyUiDisplayPasskeyInput(); 96 void NotifyUiDisplayCancel(); 97 void UserClickedYes(); 98 void UserClickedNo(); 99 100 std::shared_ptr<l2cap::classic::FixedChannelManager> fixed_channel_manager_; 101 std::unique_ptr<l2cap::classic::FixedChannelService> fixed_channel_service_{nullptr}; 102 l2cap::SecurityPolicy security_policy_ __attribute__((unused)); 103 os::Handler* security_handler_ __attribute__((unused)); 104 hci::IoCapability remote_io_capability_ __attribute__((unused)); 105 hci::IoCapability local_io_capability_ __attribute__((unused)); 106 hci::OobDataPresent local_oob_present_ __attribute__((unused)); 107 hci::AuthenticationRequirements local_authentication_requirements_ __attribute__((unused)); 108 std::unique_ptr<l2cap::classic::FixedChannel> fixed_channel_{nullptr}; 109 common::OnceCallback<void(hci::Address, PairingResultOrFailure)> complete_callback_; 110 UI* user_interface_; 111 os::Handler* user_interface_handler_; 112 std::string device_name_; 113 114 hci::ErrorCode last_status_; 115 bool locally_initiated_ = false; 116 uint32_t passkey_ = 0; 117 }; 118 119 } // namespace pairing 120 } // namespace security 121 } // namespace bluetooth 122