1 /******************************************************************************
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
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 
19 #pragma once
20 
21 #include "hci/address_with_type.h"
22 
23 namespace bluetooth {
24 namespace security {
25 
26 class ConfirmationData {
27  public:
ConfirmationData()28   ConfirmationData() : address_with_type_(hci::AddressWithType()), name_("No name set") {}
ConfirmationData(bluetooth::hci::AddressWithType address_with_type,std::string name)29   ConfirmationData(bluetooth::hci::AddressWithType address_with_type, std::string name)
30       : address_with_type_(address_with_type), name_(name) {}
ConfirmationData(bluetooth::hci::AddressWithType address_with_type,std::string name,uint32_t numeric_value)31   ConfirmationData(bluetooth::hci::AddressWithType address_with_type, std::string name, uint32_t numeric_value)
32       : address_with_type_(address_with_type), name_(name), numeric_value_(numeric_value) {}
33 
GetAddressWithType()34   const bluetooth::hci::AddressWithType& GetAddressWithType() {
35     return address_with_type_;
36   }
37 
GetName()38   std::string GetName() {
39     return name_;
40   }
41 
GetNumericValue()42   uint32_t GetNumericValue() {
43     return numeric_value_;
44   }
45 
GetRemoteIoCaps()46   hci::IoCapability GetRemoteIoCaps() const {
47     return remote_io_caps_;
48   }
SetRemoteIoCaps(hci::IoCapability remote_io_caps)49   void SetRemoteIoCaps(hci::IoCapability remote_io_caps) {
50     remote_io_caps_ = remote_io_caps;
51   }
52 
GetRemoteAuthReqs()53   hci::AuthenticationRequirements GetRemoteAuthReqs() const {
54     return remote_auth_reqs_;
55   }
56 
SetRemoteAuthReqs(hci::AuthenticationRequirements remote_auth_reqs)57   void SetRemoteAuthReqs(hci::AuthenticationRequirements remote_auth_reqs) {
58     remote_auth_reqs_ = remote_auth_reqs;
59   }
60 
GetRemoteOobDataPresent()61   hci::OobDataPresent GetRemoteOobDataPresent() const {
62     return remote_oob_data_present_;
63   }
64 
SetRemoteOobDataPresent(hci::OobDataPresent remote_oob_data_present)65   void SetRemoteOobDataPresent(hci::OobDataPresent remote_oob_data_present) {
66     remote_oob_data_present_ = remote_oob_data_present;
67   }
68 
IsJustWorks()69   bool IsJustWorks() const {
70     return just_works_;
71   }
72 
SetJustWorks(bool just_works)73   void SetJustWorks(bool just_works) {
74     just_works_ = just_works;
75   }
76 
77  private:
78   bluetooth::hci::AddressWithType address_with_type_;
79   std::string name_;
80   // Can either be the confirmation value or the passkey
81   uint32_t numeric_value_ = 0;
82 
83   // TODO(optedoblivion): Revisit after shim/BTA layer is gone
84   // Extra data is a hack to get data from the module to the shim
85   hci::IoCapability remote_io_caps_ = hci::IoCapability::DISPLAY_YES_NO;
86   hci::AuthenticationRequirements remote_auth_reqs_ = hci::AuthenticationRequirements::DEDICATED_BONDING;
87   hci::OobDataPresent remote_oob_data_present_ = hci::OobDataPresent::NOT_PRESENT;
88   bool just_works_ = false;
89 };
90 
91 // Through this interface we talk to the user, asking for confirmations/acceptance.
92 class UI {
93  public:
94   virtual ~UI() = default;
95 
96   /* Remote LE device tries to initiate pairing, ask user to confirm */
97   virtual void DisplayPairingPrompt(const bluetooth::hci::AddressWithType& address, std::string name) = 0;
98 
99   /* Remove the pairing prompt from DisplayPairingPrompt, i.e. remote device disconnected, or some application requested
100    * bond with this device */
101   virtual void Cancel(const bluetooth::hci::AddressWithType& address) = 0;
102 
103   /* Display value for Comparison, user responds yes/no */
104   virtual void DisplayConfirmValue(ConfirmationData data) = 0;
105 
106   /* Display Yes/No dialog, Classic pairing, numeric comparison with NoInputNoOutput device */
107   virtual void DisplayYesNoDialog(ConfirmationData data) = 0;
108 
109   /* Display a dialog box that will let user enter the Passkey */
110   virtual void DisplayEnterPasskeyDialog(ConfirmationData data) = 0;
111 
112   /* Present the passkey value to the user, user compares with other device */
113   virtual void DisplayPasskey(ConfirmationData data) = 0;
114 
115   /* Ask the user to enter a PIN */
116   virtual void DisplayEnterPinDialog(ConfirmationData data) = 0;
117 };
118 
119 /* Through this interface, UI provides us with user choices. */
120 class UICallbacks {
121  public:
122   virtual ~UICallbacks() = default;
123 
124   /* User accepted pairing prompt */
125   virtual void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) = 0;
126 
127   /* User confirmed that displayed value matches the value on the other device */
128   virtual void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) = 0;
129 
130   /* User typed the value displayed on the other device. This is either Passkey or the Confirm value */
131   virtual void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) = 0;
132 
133   /* User typed the PIN for the other device. */
134   virtual void OnPinEntry(const bluetooth::hci::AddressWithType& address, std::vector<uint8_t> pin) = 0;
135 };
136 
137 }  // namespace security
138 }  // namespace bluetooth
139