1 /******************************************************************************
2  *
3  * Copyright 2023 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 /******************************************************************************
20  *
21  *  This is the API implementation file for the BTA device manager.
22  *
23  ******************************************************************************/
24 
25 #include <base/functional/bind.h>
26 #include <bluetooth/log.h>
27 #include <com_android_bluetooth_flags.h>
28 
29 #include "bta/dm/bta_dm_sec_int.h"
30 #include "stack/btm/btm_sec.h"
31 #include "stack/include/bt_octets.h"
32 #include "stack/include/btm_ble_sec_api.h"
33 #include "stack/include/btm_client_interface.h"
34 #include "stack/include/main_thread.h"
35 #include "types/raw_address.h"
36 
37 using namespace bluetooth;
38 
39 /** This function initiates a bonding procedure with a peer device */
BTA_DmBond(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_TRANSPORT transport,tBT_DEVICE_TYPE device_type)40 void BTA_DmBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
41                 tBT_TRANSPORT transport, tBT_DEVICE_TYPE device_type) {
42   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
43     bta_dm_bond(bd_addr, addr_type, transport, device_type);
44   } else {
45     do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_bond, bd_addr, addr_type,
46                                                 transport, device_type));
47   }
48 }
49 
50 /** This function cancels the bonding procedure with a peer device
51  */
BTA_DmBondCancel(const RawAddress & bd_addr)52 void BTA_DmBondCancel(const RawAddress& bd_addr) {
53   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
54     bta_dm_bond_cancel(bd_addr);
55   } else {
56     do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_bond_cancel, bd_addr));
57   }
58 }
59 
60 /*******************************************************************************
61  *
62  * Function         BTA_DmPinReply
63  *
64  * Description      This function provides a pincode for a remote device when
65  *                  one is requested by DM through BTA_DM_PIN_REQ_EVT
66  *
67  *
68  * Returns          void
69  *
70  ******************************************************************************/
BTA_DmPinReply(const RawAddress & bd_addr,bool accept,uint8_t pin_len,uint8_t * p_pin)71 void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len,
72                     uint8_t* p_pin) {
73   std::unique_ptr<tBTA_DM_API_PIN_REPLY> msg =
74       std::make_unique<tBTA_DM_API_PIN_REPLY>();
75 
76   msg->bd_addr = bd_addr;
77   msg->accept = accept;
78   if (accept) {
79     msg->pin_len = pin_len;
80     memcpy(msg->p_pin, p_pin, pin_len);
81   }
82 
83   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
84     bta_dm_pin_reply(std::move(msg));
85   } else {
86     do_in_main_thread(FROM_HERE,
87                       base::Bind(bta_dm_pin_reply, base::Passed(&msg)));
88   }
89 }
90 
91 /*******************************************************************************
92  *
93  * Function         BTA_DmLocalOob
94  *
95  * Description      This function retrieves the OOB data from local controller.
96  *                  The result is reported by:
97  *                  - bta_dm_co_loc_oob_ext() if device supports secure
98  *                    connections (SC)
99  *                  - bta_dm_co_loc_oob() if device doesn't support SC
100  *
101  * Returns          void
102  *
103  ******************************************************************************/
BTA_DmLocalOob(void)104 void BTA_DmLocalOob(void) {
105   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
106     BTM_ReadLocalOobData();
107   } else {
108     do_in_main_thread(FROM_HERE, base::BindOnce(BTM_ReadLocalOobData));
109   }
110 }
111 
112 /*******************************************************************************
113  *
114  * Function         BTA_DmConfirm
115  *
116  * Description      This function accepts or rejects the numerical value of the
117  *                  Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
118  *
119  * Returns          void
120  *
121  ******************************************************************************/
BTA_DmConfirm(const RawAddress & bd_addr,bool accept)122 void BTA_DmConfirm(const RawAddress& bd_addr, bool accept) {
123   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
124     bta_dm_confirm(bd_addr, accept);
125   } else {
126     do_in_main_thread(FROM_HERE,
127                       base::BindOnce(bta_dm_confirm, bd_addr, accept));
128   }
129 }
130 
131 /*******************************************************************************
132  *
133  * Function         BTA_DmAddDevice
134  *
135  * Description      This function adds a device to the security database list of
136  *                  peer device
137  *
138  * Returns          void
139  *
140  ******************************************************************************/
BTA_DmAddDevice(RawAddress bd_addr,DEV_CLASS dev_class,LinkKey link_key,uint8_t key_type,uint8_t pin_length)141 void BTA_DmAddDevice(RawAddress bd_addr, DEV_CLASS dev_class, LinkKey link_key,
142                      uint8_t key_type, uint8_t pin_length) {
143   auto closure =
144       base::Bind(get_btm_client_interface().security.BTM_SecAddDevice, bd_addr,
145                  dev_class, link_key, key_type, pin_length);
146 
147   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
148     closure.Run();
149   } else {
150     do_in_main_thread(FROM_HERE, closure);
151   }
152 }
153 
154 /** This function removes a device fromthe security database list of peer
155  * device. It manages unpairing even while connected */
BTA_DmRemoveDevice(const RawAddress & bd_addr)156 tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
157   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
158     bta_dm_remove_device(bd_addr);
159   } else {
160     do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_remove_device, bd_addr));
161   }
162   return BTA_SUCCESS;
163 }
164 
165 /*******************************************************************************
166  *
167  * Function         BTA_DmAddBleKey
168  *
169  * Description      Add/modify LE device information.  This function will be
170  *                  normally called during host startup to restore all required
171  *                  information stored in the NVRAM.
172  *
173  * Parameters:      bd_addr          - BD address of the peer
174  *                  p_le_key         - LE key values.
175  *                  key_type         - LE SMP key type.
176  *
177  * Returns          BTA_SUCCESS if successful
178  *                  BTA_FAIL if operation failed.
179  *
180  ******************************************************************************/
BTA_DmAddBleKey(const RawAddress & bd_addr,tBTA_LE_KEY_VALUE * p_le_key,tBTM_LE_KEY_TYPE key_type)181 void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
182                      tBTM_LE_KEY_TYPE key_type) {
183   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
184     bta_dm_add_blekey(bd_addr, *p_le_key, key_type);
185   } else {
186     do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_add_blekey, bd_addr,
187                                                 *p_le_key, key_type));
188   }
189 }
190 
191 /*******************************************************************************
192  *
193  * Function         BTA_DmAddBleDevice
194  *
195  * Description      Add a BLE device.  This function will be normally called
196  *                  during host startup to restore all required information
197  *                  for a LE device stored in the NVRAM.
198  *
199  * Parameters:      bd_addr          - BD address of the peer
200  *                  dev_type         - Remote device's device type.
201  *                  addr_type        - LE device address type.
202  *
203  * Returns          void
204  *
205  ******************************************************************************/
BTA_DmAddBleDevice(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_DEVICE_TYPE dev_type)206 void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
207                         tBT_DEVICE_TYPE dev_type) {
208   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
209     bta_dm_add_ble_device(bd_addr, addr_type, dev_type);
210   } else {
211     do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_add_ble_device, bd_addr,
212                                                 addr_type, dev_type));
213   }
214 }
215 
216 /*******************************************************************************
217  *
218  * Function         BTA_DmBlePasskeyReply
219  *
220  * Description      Send BLE SMP passkey reply.
221  *
222  * Parameters:      bd_addr          - BD address of the peer
223  *                  accept           - passkey entry successful or declined.
224  *                  passkey          - passkey value, must be a 6 digit number,
225  *                                     can be lead by 0.
226  *
227  * Returns          void
228  *
229  ******************************************************************************/
BTA_DmBlePasskeyReply(const RawAddress & bd_addr,bool accept,uint32_t passkey)230 void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept,
231                            uint32_t passkey) {
232   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
233     bta_dm_ble_passkey_reply(bd_addr, accept, accept ? passkey : 0);
234   } else {
235     do_in_main_thread(FROM_HERE,
236                       base::BindOnce(bta_dm_ble_passkey_reply, bd_addr, accept,
237                                      accept ? passkey : 0));
238   }
239 }
240 
241 /*******************************************************************************
242  *
243  * Function         BTA_DmBleConfirmReply
244  *
245  * Description      Send BLE SMP SC user confirmation reply.
246  *
247  * Parameters:      bd_addr          - BD address of the peer
248  *                  accept           - numbers to compare are the same or
249  *                                     different.
250  *
251  * Returns          void
252  *
253  ******************************************************************************/
BTA_DmBleConfirmReply(const RawAddress & bd_addr,bool accept)254 void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
255   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
256     bta_dm_ble_confirm_reply(bd_addr, accept);
257   } else {
258     do_in_main_thread(
259         FROM_HERE, base::BindOnce(bta_dm_ble_confirm_reply, bd_addr, accept));
260   }
261 }
262 
263 /*******************************************************************************
264  *
265  * Function         BTA_DmBleSecurityGrant
266  *
267  * Description      Grant security request access.
268  *
269  * Parameters:      bd_addr          - BD address of the peer
270  *                  res              - security grant status.
271  *
272  * Returns          void
273  *
274  ******************************************************************************/
BTA_DmBleSecurityGrant(const RawAddress & bd_addr,tBTA_DM_BLE_SEC_GRANT res)275 void BTA_DmBleSecurityGrant(const RawAddress& bd_addr,
276                             tBTA_DM_BLE_SEC_GRANT res) {
277   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
278     BTM_SecurityGrant(bd_addr, res);
279   } else {
280     do_in_main_thread(FROM_HERE,
281                       base::BindOnce(BTM_SecurityGrant, bd_addr, res));
282   }
283 }
284 
285 /*******************************************************************************
286  *
287  * Function         BTA_DmSetEncryption
288  *
289  * Description      This function is called to ensure that connection is
290  *                  encrypted.  Should be called only on an open connection.
291  *                  Typically only needed for connections that first want to
292  *                  bring up unencrypted links, then later encrypt them.
293  *
294  * Parameters:      bd_addr       - Address of the peer device
295  *                  transport     - transport of the link to be encruypted
296  *                  p_callback    - Pointer to callback function to indicat the
297  *                                  link encryption status
298  *                  sec_act       - This is the security action to indicate
299  *                                  what kind of BLE security level is required
300  *                                  for the BLE link if BLE is supported.
301  *                                  Note: This parameter is ignored for the
302  *                                        BR/EDR or if BLE is not supported.
303  *
304  * Returns          void
305  *
306  ******************************************************************************/
BTA_DmSetEncryption(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTM_BLE_SEC_ACT sec_act)307 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBT_TRANSPORT transport,
308                          tBTA_DM_ENCRYPT_CBACK* p_callback,
309                          tBTM_BLE_SEC_ACT sec_act) {
310   log::verbose("");
311   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
312     bta_dm_set_encryption(bd_addr, transport, p_callback, sec_act);
313   } else {
314     do_in_main_thread(FROM_HERE,
315                       base::BindOnce(bta_dm_set_encryption, bd_addr, transport,
316                                      p_callback, sec_act));
317   }
318 }
319 
320 /*******************************************************************************
321  *
322  * Function         BTA_DmSirkSecCbRegister
323  *
324  * Description      This procedure registeres in requested a callback for
325  *                  verification by CSIP potential set member.
326  *
327  * Parameters       p_cback     - callback to member verificator
328  *
329  * Returns          void
330  *
331  ******************************************************************************/
BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK * p_cback)332 void BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK* p_cback) {
333   log::debug("");
334   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
335     bta_dm_ble_sirk_sec_cb_register(p_cback);
336   } else {
337     do_in_main_thread(FROM_HERE,
338                       base::BindOnce(bta_dm_ble_sirk_sec_cb_register, p_cback));
339   }
340 }
341 
342 /*******************************************************************************
343  *
344  * Function         BTA_DmSirkConfirmDeviceReply
345  *
346  * Description      This procedure confirms requested to validate set device.
347  *
348  * Parameters       bd_addr     - BD address of the peer
349  *                  accept      - True if device is authorized by CSIP, false
350  *                                otherwise.
351  *
352  * Returns          void
353  *
354  ******************************************************************************/
BTA_DmSirkConfirmDeviceReply(const RawAddress & bd_addr,bool accept)355 void BTA_DmSirkConfirmDeviceReply(const RawAddress& bd_addr, bool accept) {
356   log::debug("");
357   if (com::android::bluetooth::flags::synchronous_bta_sec()) {
358     bta_dm_ble_sirk_confirm_device_reply(bd_addr, accept);
359   } else {
360     do_in_main_thread(
361         FROM_HERE,
362         base::BindOnce(bta_dm_ble_sirk_confirm_device_reply, bd_addr, accept));
363   }
364 }
365 
366