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