1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "bt_bta_dm_sec"
18 
19 #include <bluetooth/log.h>
20 
21 #include <cstdint>
22 
23 #include "bta/dm/bta_dm_act.h"
24 #include "bta/dm/bta_dm_int.h"
25 #include "bta/dm/bta_dm_sec_int.h"
26 #include "bta/include/bta_dm_ci.h"  // bta_dm_ci_rmt_oob
27 #include "btif/include/btif_dm.h"
28 #include "internal_include/bt_target.h"
29 #include "stack/include/bt_dev_class.h"
30 #include "stack/include/btm_ble_sec_api_types.h"
31 #include "stack/include/btm_client_interface.h"
32 #include "stack/include/btm_sec_api.h"
33 #include "stack/include/btm_status.h"
34 #include "stack/include/gatt_api.h"
35 #include "stack/include/rnr_interface.h"
36 #include "stack/include/security_client_callbacks.h"
37 #include "types/bt_transport.h"
38 #include "types/raw_address.h"
39 
40 // TODO(b/369381361) Enfore -Wmissing-prototypes
41 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
42 
43 using namespace bluetooth;
44 
45 static tBTM_STATUS bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data);
46 static tBTM_STATUS bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
47                                         tBTM_LE_EVT_DATA* p_data);
48 static tBTM_STATUS bta_dm_new_link_key_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
49                                              BD_NAME bd_name, const LinkKey& key, uint8_t key_type,
50                                              bool is_ctkd);
51 static tBTM_STATUS bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
52                                     const BD_NAME bd_name, bool min_16_digit);
53 static tBTM_STATUS bta_dm_sirk_verifiction_cback(const RawAddress& bd_addr);
54 static void bta_dm_authentication_complete_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
55                                                  BD_NAME bd_name, tHCI_REASON result);
56 static void bta_dm_ble_id_key_cback(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key);
57 static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result);
58 static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr);
59 static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr);
60 
61 /* bta security callback */
62 const tBTM_APPL_INFO bta_security = {
63         .p_pin_callback = &bta_dm_pin_cback,
64         .p_link_key_callback = &bta_dm_new_link_key_cback,
65         .p_auth_complete_callback = &bta_dm_authentication_complete_cback,
66         .p_bond_cancel_cmpl_callback = &bta_dm_bond_cancel_complete_cback,
67         .p_sp_callback = &bta_dm_sp_cback,
68         .p_le_callback = &bta_dm_ble_smp_cback,
69         .p_le_key_callback = &bta_dm_ble_id_key_cback,
70         .p_sirk_verification_callback = &bta_dm_sirk_verifiction_cback};
71 
btm_sec_on_hw_on()72 void btm_sec_on_hw_on() {
73   tBTA_DM_SEC_CBACK* temp_sec_cback = bta_dm_sec_cb.p_sec_cback;
74   bta_dm_sec_cb = {};
75   bta_dm_sec_cb.p_sec_cback = temp_sec_cback;
76 }
77 
bta_dm_ble_sirk_sec_cb_register(tBTA_DM_SEC_CBACK * p_cback)78 void bta_dm_ble_sirk_sec_cb_register(tBTA_DM_SEC_CBACK* p_cback) {
79   /* Save the callback to be called when a request of member validation will be
80    * needed. */
81   bta_dm_sec_cb.p_sec_sirk_cback = p_cback;
82 }
83 
bta_dm_ble_sirk_confirm_device_reply(const RawAddress & bd_addr,bool accept)84 void bta_dm_ble_sirk_confirm_device_reply(const RawAddress& bd_addr, bool accept) {
85   log::debug("addr:{}", bd_addr);
86   get_btm_client_interface().security.BTM_BleSirkConfirmDeviceReply(
87           bd_addr, accept ? tBTM_STATUS::BTM_SUCCESS : tBTM_STATUS::BTM_NOT_AUTHORIZED);
88 }
89 
bta_dm_consolidate(const RawAddress & identity_addr,const RawAddress & rpa)90 void bta_dm_consolidate(const RawAddress& identity_addr, const RawAddress& rpa) {
91   for (auto i = 0; i < bta_dm_cb.device_list.count; i++) {
92     if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr != rpa) {
93       continue;
94     }
95 
96     log::info("consolidating bda_dm_cb record {} -> {}", rpa, identity_addr);
97     bta_dm_cb.device_list.peer_device[i].peer_bdaddr = identity_addr;
98   }
99 }
100 
btm_dm_sec_init()101 void btm_dm_sec_init() { get_btm_client_interface().security.BTM_SecRegister(&bta_security); }
102 
103 /** Initialises the BT device security manager */
bta_dm_sec_enable(tBTA_DM_SEC_CBACK * p_sec_cback)104 void bta_dm_sec_enable(tBTA_DM_SEC_CBACK* p_sec_cback) {
105   /* make sure security callback is saved - if no callback, do not erase the
106   previous one,
107   it could be an error recovery mechanism */
108   if (p_sec_cback != NULL) {
109     bta_dm_sec_cb.p_sec_cback = p_sec_cback;
110   }
111 }
112 
bta_dm_on_encryption_change(bt_encryption_change_evt encryption_change)113 void bta_dm_on_encryption_change(bt_encryption_change_evt encryption_change) {
114   if (bta_dm_sec_cb.p_sec_cback) {
115     tBTA_DM_SEC sec_event;
116     sec_event.encryption_change = encryption_change;
117     bta_dm_sec_cb.p_sec_cback(BTA_DM_ENCRYPTION_CHANGE_EVT, &sec_event);
118   }
119 }
120 
bta_dm_remote_key_missing(const RawAddress bd_addr)121 void bta_dm_remote_key_missing(const RawAddress bd_addr) {
122   if (bta_dm_sec_cb.p_sec_cback) {
123     tBTA_DM_SEC sec_event;
124     sec_event.key_missing.bd_addr = bd_addr;
125     bta_dm_sec_cb.p_sec_cback(BTA_DM_KEY_MISSING_EVT, &sec_event);
126   }
127 }
128 
129 /** Bonds with peer device */
bta_dm_bond(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_TRANSPORT transport,tBT_DEVICE_TYPE device_type)130 void bta_dm_bond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
131                  tBT_DEVICE_TYPE device_type) {
132   log::debug("Bonding with peer device:{} type:{} transport:{} type:{}", bd_addr,
133              AddressTypeText(addr_type), bt_transport_text(transport), DeviceTypeText(device_type));
134 
135   tBTA_DM_SEC sec_event;
136 
137   tBTM_STATUS status = get_btm_client_interface().security.BTM_SecBond(bd_addr, addr_type,
138                                                                        transport, device_type);
139 
140   if (bta_dm_sec_cb.p_sec_cback && (status != tBTM_STATUS::BTM_CMD_STARTED)) {
141     memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
142     sec_event.auth_cmpl.bd_addr = bd_addr;
143     bd_name_from_char_pointer(sec_event.auth_cmpl.bd_name,
144                               get_btm_client_interface().security.BTM_SecReadDevName(bd_addr));
145 
146     /*      taken care of by memset [above]
147             sec_event.auth_cmpl.key_present = false;
148             sec_event.auth_cmpl.success = false;
149     */
150     sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
151     if (status == tBTM_STATUS::BTM_SUCCESS) {
152       sec_event.auth_cmpl.success = true;
153     } else {
154       /* delete this device entry from Sec Dev DB */
155       bta_dm_remove_sec_dev_entry(bd_addr);
156     }
157     bta_dm_sec_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
158   }
159 }
160 
161 /** Cancels bonding with a peer device */
bta_dm_bond_cancel(const RawAddress & bd_addr)162 void bta_dm_bond_cancel(const RawAddress& bd_addr) {
163   tBTM_STATUS status;
164   tBTA_DM_SEC sec_event;
165 
166   log::debug("addr:{}", bd_addr);
167 
168   status = get_btm_client_interface().security.BTM_SecBondCancel(bd_addr);
169 
170   if (bta_dm_sec_cb.p_sec_cback &&
171       (status != tBTM_STATUS::BTM_CMD_STARTED && status != tBTM_STATUS::BTM_SUCCESS)) {
172     sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
173 
174     bta_dm_sec_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
175   }
176 }
177 
178 /** Send the pin_reply to a request from BTM */
bta_dm_pin_reply(std::unique_ptr<tBTA_DM_API_PIN_REPLY> msg)179 void bta_dm_pin_reply(std::unique_ptr<tBTA_DM_API_PIN_REPLY> msg) {
180   if (msg->accept) {
181     get_btm_client_interface().security.BTM_PINCodeReply(msg->bd_addr, tBTM_STATUS::BTM_SUCCESS,
182                                                          msg->pin_len, msg->p_pin);
183   } else {
184     get_btm_client_interface().security.BTM_PINCodeReply(msg->bd_addr,
185                                                          tBTM_STATUS::BTM_NOT_AUTHORIZED, 0, NULL);
186   }
187 }
188 
189 /** Send the user confirm request reply in response to a request from BTM */
bta_dm_confirm(const RawAddress & bd_addr,bool accept)190 void bta_dm_confirm(const RawAddress& bd_addr, bool accept) {
191   get_btm_client_interface().security.BTM_SecConfirmReqReply(
192           accept ? tBTM_STATUS::BTM_SUCCESS : tBTM_STATUS::BTM_NOT_AUTHORIZED, BT_TRANSPORT_BR_EDR,
193           bd_addr);
194 }
195 
196 /** respond to the OOB data request for the remote device from BTM */
bta_dm_ci_rmt_oob_act(std::unique_ptr<tBTA_DM_CI_RMT_OOB> msg)197 void bta_dm_ci_rmt_oob_act(std::unique_ptr<tBTA_DM_CI_RMT_OOB> msg) {
198   get_btm_client_interface().security.BTM_RemoteOobDataReply(
199           msg->accept ? tBTM_STATUS::BTM_SUCCESS : tBTM_STATUS::BTM_NOT_AUTHORIZED, msg->bd_addr,
200           msg->c, msg->r);
201 }
202 
203 /*******************************************************************************
204  *
205  * Function         bta_dm_pinname_cback
206  *
207  * Description      Callback requesting pin_key
208  *
209  * Returns          void
210  *
211  ******************************************************************************/
bta_dm_pinname_cback(const tBTM_REMOTE_DEV_NAME * p_data)212 static void bta_dm_pinname_cback(const tBTM_REMOTE_DEV_NAME* p_data) {
213   tBTM_REMOTE_DEV_NAME* p_result = (tBTM_REMOTE_DEV_NAME*)p_data;
214   tBTA_DM_SEC sec_event;
215   tBTA_DM_SEC_EVT event = bta_dm_sec_cb.pin_evt;
216 
217   if (BTA_DM_SP_CFM_REQ_EVT == event) {
218     /* Retrieved saved device class and bd_addr */
219     sec_event.cfm_req.bd_addr = bta_dm_sec_cb.pin_bd_addr;
220     sec_event.cfm_req.dev_class = bta_dm_sec_cb.pin_dev_class;
221     log::info("CoD: sec_event.cfm_req.dev_class = {}", dev_class_text(sec_event.cfm_req.dev_class));
222 
223     if (p_result && p_result->btm_status == tBTM_STATUS::BTM_SUCCESS) {
224       bd_name_copy(sec_event.cfm_req.bd_name, p_result->remote_bd_name);
225     } else { /* No name found */
226       sec_event.cfm_req.bd_name[0] = 0;
227     }
228 
229     sec_event.key_notif.passkey = bta_dm_sec_cb.num_val; /* get PIN code numeric number */
230 
231     /* 1 additional event data fields for this event */
232     sec_event.cfm_req.just_works = bta_dm_sec_cb.just_works;
233     /* retrieve the loc and rmt caps */
234     sec_event.cfm_req.loc_io_caps = bta_dm_sec_cb.loc_io_caps;
235     sec_event.cfm_req.rmt_io_caps = bta_dm_sec_cb.rmt_io_caps;
236     sec_event.cfm_req.loc_auth_req = bta_dm_sec_cb.loc_auth_req;
237     sec_event.cfm_req.rmt_auth_req = bta_dm_sec_cb.rmt_auth_req;
238 
239   } else {
240     /* Retrieved saved device class and bd_addr */
241     sec_event.pin_req.bd_addr = bta_dm_sec_cb.pin_bd_addr;
242     sec_event.pin_req.dev_class = bta_dm_sec_cb.pin_dev_class;
243 
244     if (p_result && p_result->btm_status == tBTM_STATUS::BTM_SUCCESS) {
245       bd_name_copy(sec_event.pin_req.bd_name, p_result->remote_bd_name);
246     } else { /* No name found */
247       sec_event.pin_req.bd_name[0] = 0;
248     }
249 
250     event = bta_dm_sec_cb.pin_evt;
251     sec_event.key_notif.passkey = bta_dm_sec_cb.num_val; /* get PIN code numeric number */
252   }
253 
254   if (bta_dm_sec_cb.p_sec_cback) {
255     bta_dm_sec_cb.p_sec_cback(event, &sec_event);
256   }
257 }
258 
259 /*******************************************************************************
260  *
261  * Function         bta_dm_pin_cback
262  *
263  * Description      Callback requesting pin_key
264  *
265  * Returns          void
266  *
267  ******************************************************************************/
bta_dm_pin_cback(const RawAddress & bd_addr,DEV_CLASS dev_class,const BD_NAME bd_name,bool min_16_digit)268 static tBTM_STATUS bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
269                                     const BD_NAME bd_name, bool min_16_digit) {
270   if (!bta_dm_sec_cb.p_sec_cback) {
271     return tBTM_STATUS::BTM_NOT_AUTHORIZED;
272   }
273 
274   /* If the device name is not known, save bdaddr and devclass and initiate a
275    * name request */
276   if (bd_name[0] == 0) {
277     bta_dm_sec_cb.pin_evt = BTA_DM_PIN_REQ_EVT;
278     bta_dm_sec_cb.pin_bd_addr = bd_addr;
279     bta_dm_sec_cb.pin_dev_class = dev_class;
280     if ((get_stack_rnr_interface().BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback,
281                                                             BT_TRANSPORT_BR_EDR)) ==
282         tBTM_STATUS::BTM_CMD_STARTED) {
283       return tBTM_STATUS::BTM_CMD_STARTED;
284     }
285 
286     log::warn("Failed to start Remote Name Request, addr:{}", bd_addr);
287   }
288 
289   tBTA_DM_SEC sec_event = {.pin_req = {
290                                    .bd_addr = bd_addr,
291                                    .dev_class = dev_class,
292                                    .bd_name = "",
293                                    .min_16_digit = min_16_digit,
294                            }};
295   bd_name_copy(sec_event.pin_req.bd_name, bd_name);
296 
297   bta_dm_sec_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
298   return tBTM_STATUS::BTM_CMD_STARTED;
299 }
300 
301 /*******************************************************************************
302  *
303  * Function         bta_dm_new_link_key_cback
304  *
305  * Description      Callback from BTM to notify new link key
306  *
307  * Returns          void
308  *
309  ******************************************************************************/
bta_dm_new_link_key_cback(const RawAddress & bd_addr,DEV_CLASS,BD_NAME bd_name,const LinkKey & key,uint8_t key_type,bool is_ctkd)310 static tBTM_STATUS bta_dm_new_link_key_cback(const RawAddress& bd_addr, DEV_CLASS /* dev_class */,
311                                              BD_NAME bd_name, const LinkKey& key, uint8_t key_type,
312                                              bool is_ctkd) {
313   tBTA_DM_SEC sec_event;
314   tBTA_DM_AUTH_CMPL* p_auth_cmpl;
315   tBTA_DM_SEC_EVT event = BTA_DM_AUTH_CMPL_EVT;
316 
317   memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
318 
319   p_auth_cmpl = &sec_event.auth_cmpl;
320 
321   p_auth_cmpl->bd_addr = bd_addr;
322 
323   bd_name_copy(p_auth_cmpl->bd_name, bd_name);
324   p_auth_cmpl->key_present = true;
325   p_auth_cmpl->key_type = key_type;
326   p_auth_cmpl->success = true;
327   p_auth_cmpl->key = key;
328   p_auth_cmpl->is_ctkd = is_ctkd;
329 
330   sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
331 
332   // Report the BR link key based on the BR/EDR address and type
333   get_btm_client_interface().peer.BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
334                                                   &sec_event.auth_cmpl.addr_type);
335   if (bta_dm_sec_cb.p_sec_cback) {
336     bta_dm_sec_cb.p_sec_cback(event, &sec_event);
337   }
338 
339   // Setting remove_dev_pending flag to false, where it will avoid deleting
340   // the
341   // security device record when the ACL connection link goes down in case of
342   // reconnection.
343   if (bta_dm_cb.device_list.count) {
344     bta_dm_reset_sec_dev_pending(p_auth_cmpl->bd_addr);
345   }
346 
347   return tBTM_STATUS::BTM_CMD_STARTED;
348 }
349 
350 /*******************************************************************************
351  *
352  * Function         bta_dm_authentication_complete_cback
353  *
354  * Description      Authentication complete callback from BTM
355  *
356  * Returns          void
357  *
358  ******************************************************************************/
bta_dm_authentication_complete_cback(const RawAddress & bd_addr,DEV_CLASS,BD_NAME bd_name,tHCI_REASON reason)359 static void bta_dm_authentication_complete_cback(const RawAddress& bd_addr,
360                                                  DEV_CLASS /* dev_class */, BD_NAME bd_name,
361                                                  tHCI_REASON reason) {
362   if (reason != HCI_SUCCESS) {
363     if (bta_dm_sec_cb.p_sec_cback) {
364       // Build out the security event data structure
365       tBTA_DM_SEC sec_event = {
366               .auth_cmpl =
367                       {
368                               .bd_addr = bd_addr,
369                       },
370       };
371       bd_name_copy(sec_event.auth_cmpl.bd_name, bd_name);
372 
373       // Report the BR link key based on the BR/EDR address and type
374       get_btm_client_interface().peer.BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
375                                                       &sec_event.auth_cmpl.addr_type);
376       sec_event.auth_cmpl.fail_reason = reason;
377 
378       bta_dm_sec_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
379     }
380 
381     switch (reason) {
382       case HCI_ERR_AUTH_FAILURE:
383       case HCI_ERR_KEY_MISSING:
384       case HCI_ERR_HOST_REJECT_SECURITY:
385       case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
386         log::warn("authentication failed entry:{}, reason:{}", bd_addr,
387                   hci_reason_code_text(reason));
388         break;
389 
390       default:
391         break;
392     }
393   }
394 }
395 
396 /*******************************************************************************
397  *
398  * Function         bta_dm_sp_cback
399  *
400  * Description      simple pairing callback from BTM
401  *
402  * Returns          void
403  *
404  ******************************************************************************/
bta_dm_sp_cback(tBTM_SP_EVT event,tBTM_SP_EVT_DATA * p_data)405 static tBTM_STATUS bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
406   tBTM_STATUS status = tBTM_STATUS::BTM_CMD_STARTED;
407   tBTA_DM_SEC sec_event = {};
408   tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
409 
410   log::verbose("event:{}", sp_evt_to_text(event));
411   if (!bta_dm_sec_cb.p_sec_cback) {
412     return tBTM_STATUS::BTM_NOT_AUTHORIZED;
413   }
414 
415   bool sp_rmt_result = false;
416   /* TODO_SP */
417   switch (event) {
418     case BTM_SP_IO_REQ_EVT:
419       /* translate auth_req */
420       btif_dm_set_oob_for_io_req(&p_data->io_req.oob_data);
421       btif_dm_proc_io_req(&p_data->io_req.auth_req, p_data->io_req.is_orig);
422       log::verbose("io mitm: {} oob_data:{}", p_data->io_req.auth_req, p_data->io_req.oob_data);
423       break;
424     case BTM_SP_IO_RSP_EVT:
425       btif_dm_proc_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap, p_data->io_rsp.oob_data,
426                           p_data->io_rsp.auth_req);
427       break;
428 
429     case BTM_SP_CFM_REQ_EVT:
430       pin_evt = BTA_DM_SP_CFM_REQ_EVT;
431       bta_dm_sec_cb.just_works = sec_event.cfm_req.just_works = p_data->cfm_req.just_works;
432       sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
433       sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
434       sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
435       sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
436 
437       [[fallthrough]];
438     /* Passkey entry mode, mobile device with output capability is very
439         unlikely to receive key request, so skip this event */
440     /*case BTM_SP_KEY_REQ_EVT: */
441     case BTM_SP_KEY_NOTIF_EVT:
442       // TODO PleaseFix: This assignment only works with event
443       // BTM_SP_KEY_NOTIF_EVT
444       bta_dm_sec_cb.num_val = sec_event.key_notif.passkey = p_data->key_notif.passkey;
445 
446       if (BTM_SP_CFM_REQ_EVT == event) {
447         /* Due to the switch case falling through below to
448            BTM_SP_KEY_NOTIF_EVT,
449            copy these values into key_notif from cfm_req */
450         sec_event.key_notif.bd_addr = p_data->cfm_req.bd_addr;
451         sec_event.key_notif.dev_class = p_data->cfm_req.dev_class;
452         log::info("CoD: sec_event.key_notif.dev_class = {}",
453                   dev_class_text(sec_event.key_notif.dev_class));
454         bd_name_copy(sec_event.key_notif.bd_name, p_data->cfm_req.bd_name);
455         /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
456            call remote name request using values from cfm_req */
457         if (p_data->cfm_req.bd_name[0] == 0) {
458           bta_dm_sec_cb.pin_evt = pin_evt;
459           bta_dm_sec_cb.pin_bd_addr = p_data->cfm_req.bd_addr;
460           bta_dm_sec_cb.rmt_io_caps = sec_event.cfm_req.rmt_io_caps;
461           bta_dm_sec_cb.loc_io_caps = sec_event.cfm_req.loc_io_caps;
462           bta_dm_sec_cb.rmt_auth_req = sec_event.cfm_req.rmt_auth_req;
463           bta_dm_sec_cb.loc_auth_req = sec_event.cfm_req.loc_auth_req;
464 
465           bta_dm_sec_cb.pin_dev_class = p_data->cfm_req.dev_class;
466           log::info("CoD: bta_dm_sec_cb.pin_dev_class = {}",
467                     dev_class_text(bta_dm_sec_cb.pin_dev_class));
468           {
469             const tBTM_STATUS btm_status = get_stack_rnr_interface().BTM_ReadRemoteDeviceName(
470                     p_data->cfm_req.bd_addr, bta_dm_pinname_cback, BT_TRANSPORT_BR_EDR);
471             switch (btm_status) {
472               case tBTM_STATUS::BTM_CMD_STARTED:
473                 return btm_status;
474               default:
475                 // NOTE: This will issue callback on this failure path
476                 log::warn("Failed to start Remote Name Request btm_status:{}",
477                           btm_status_text(btm_status));
478             };
479           }
480         }
481       }
482 
483       if (BTM_SP_KEY_NOTIF_EVT == event) {
484         /* If the device name is not known, save bdaddr and devclass
485            and initiate a name request with values from key_notif */
486         if (p_data->key_notif.bd_name[0] == 0) {
487           bta_dm_sec_cb.pin_evt = pin_evt;
488           bta_dm_sec_cb.pin_bd_addr = p_data->key_notif.bd_addr;
489           bta_dm_sec_cb.pin_dev_class = p_data->key_notif.dev_class;
490           if ((get_stack_rnr_interface().BTM_ReadRemoteDeviceName(
491                       p_data->key_notif.bd_addr, bta_dm_pinname_cback, BT_TRANSPORT_BR_EDR)) ==
492               tBTM_STATUS::BTM_CMD_STARTED) {
493             return tBTM_STATUS::BTM_CMD_STARTED;
494           }
495           log::warn("Failed to start Remote Name Request, addr:{}", p_data->key_notif.bd_addr);
496         } else {
497           sec_event.key_notif.bd_addr = p_data->key_notif.bd_addr;
498           sec_event.key_notif.dev_class = p_data->key_notif.dev_class;
499           bd_name_copy(sec_event.key_notif.bd_name, p_data->key_notif.bd_name);
500           sec_event.key_notif.bd_name[BD_NAME_LEN] = 0;
501         }
502       }
503 
504       bta_dm_sec_cb.p_sec_cback(pin_evt, &sec_event);
505 
506       break;
507 
508     case BTM_SP_LOC_OOB_EVT:
509       // BR/EDR OOB pairing is not supported with Secure Connections
510       btif_dm_proc_loc_oob(BT_TRANSPORT_BR_EDR,
511                            (bool)(p_data->loc_oob.status == tBTM_STATUS::BTM_SUCCESS),
512                            p_data->loc_oob.c_192, p_data->loc_oob.r_192);
513       break;
514 
515     case BTM_SP_RMT_OOB_EVT: {
516       Octet16 c;
517       Octet16 r;
518       sp_rmt_result = false;
519       sp_rmt_result = btif_dm_proc_rmt_oob(p_data->rmt_oob.bd_addr, &c, &r);
520       log::verbose("result={}", sp_rmt_result);
521       bta_dm_ci_rmt_oob(sp_rmt_result, p_data->rmt_oob.bd_addr, c, r);
522       break;
523     }
524 
525     default:
526       status = tBTM_STATUS::BTM_NOT_AUTHORIZED;
527       break;
528   }
529   log::verbose("dm status:{}", status);
530   return status;
531 }
532 
533 /*******************************************************************************
534  *
535  * Function         bta_dm_reset_sec_dev_pending
536  *
537  * Description      Setting the remove device pending status to false from
538  *                  security device DB, when the link key notification
539  *                  event comes.
540  *
541  * Returns          void
542  *
543  ******************************************************************************/
bta_dm_reset_sec_dev_pending(const RawAddress & remote_bd_addr)544 static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr) {
545   for (size_t i = 0; i < bta_dm_cb.device_list.count; i++) {
546     auto& dev = bta_dm_cb.device_list.peer_device[i];
547     if (dev.peer_bdaddr == remote_bd_addr) {
548       if (dev.remove_dev_pending) {
549         log::info("Clearing remove_dev_pending for {}", dev.peer_bdaddr);
550         dev.remove_dev_pending = false;
551       }
552       return;
553     }
554   }
555 }
556 
557 /*******************************************************************************
558  *
559  * Function         bta_dm_remove_sec_dev_entry
560  *
561  * Description      Removes device entry from Security device DB if ACL
562  connection with
563  *                  remtoe device does not exist, else schedule for dev entry
564  removal upon
565                      ACL close
566  *
567  * Returns          void
568  *
569  ******************************************************************************/
bta_dm_remove_sec_dev_entry(const RawAddress & remote_bd_addr)570 static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr) {
571   if (get_btm_client_interface().peer.BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
572       get_btm_client_interface().peer.BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
573     log::debug("ACL is not down. Schedule for Dev Removal when ACL closes:{}", remote_bd_addr);
574     get_btm_client_interface().security.BTM_SecClearSecurityFlags(remote_bd_addr);
575     for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
576       auto& dev = bta_dm_cb.device_list.peer_device[i];
577       if (dev.peer_bdaddr == remote_bd_addr) {
578         log::info("Setting remove_dev_pending for {}", dev.peer_bdaddr);
579         dev.remove_dev_pending = TRUE;
580         break;
581       }
582     }
583   } else {
584     // remote_bd_addr comes from security record, which is removed in
585     // BTM_SecDeleteDevice.
586     RawAddress addr_copy = remote_bd_addr;
587     bta_dm_process_remove_device_no_callback(addr_copy);
588   }
589 }
590 
591 /*******************************************************************************
592  *
593  * Function         bta_dm_bond_cancel_complete_cback
594  *
595  * Description      Authentication complete callback from BTM
596  *
597  * Returns          void
598  *
599  ******************************************************************************/
bta_dm_bond_cancel_complete_cback(tBTM_STATUS result)600 static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) {
601   tBTA_DM_SEC sec_event;
602 
603   if (result == tBTM_STATUS::BTM_SUCCESS) {
604     sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
605   } else {
606     sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
607   }
608 
609   if (bta_dm_sec_cb.p_sec_cback) {
610     bta_dm_sec_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
611   }
612 }
613 
ble_io_req(const RawAddress & bd_addr,tBTM_IO_CAP * p_io_cap,tBTM_OOB_DATA * p_oob_data,tBTM_LE_AUTH_REQ * p_auth_req,uint8_t * p_max_key_size,tBTM_LE_KEY_TYPE * p_init_key,tBTM_LE_KEY_TYPE * p_resp_key)614 static void ble_io_req(const RawAddress& bd_addr, tBTM_IO_CAP* p_io_cap, tBTM_OOB_DATA* p_oob_data,
615                        tBTM_LE_AUTH_REQ* p_auth_req, uint8_t* p_max_key_size,
616                        tBTM_LE_KEY_TYPE* p_init_key, tBTM_LE_KEY_TYPE* p_resp_key) {
617   /* Retrieve the properties from file system if possible */
618   tBTE_APPL_CFG nv_config;
619   if (btif_dm_get_smp_config(&nv_config)) {
620     bte_appl_cfg = nv_config;
621   }
622 
623   /* *p_auth_req by default is false for devices with NoInputNoOutput; true for
624    * other devices. */
625 
626   if (bte_appl_cfg.ble_auth_req) {
627     *p_auth_req =
628             bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
629   }
630 
631   /* if OOB is not supported, this call-out function does not need to do
632    * anything
633    * otherwise, look for the OOB data associated with the address and set
634    * *p_oob_data accordingly.
635    * If the answer can not be obtained right away,
636    * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the
637    * answer is available.
638    */
639 
640   btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req);
641 
642   if (bte_appl_cfg.ble_io_cap <= 4) {
643     *p_io_cap = static_cast<tBTM_IO_CAP>(bte_appl_cfg.ble_io_cap);
644   }
645 
646   if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE) {
647     *p_init_key = bte_appl_cfg.ble_init_key;
648   }
649 
650   if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE) {
651     *p_resp_key = bte_appl_cfg.ble_resp_key;
652   }
653 
654   if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16) {
655     *p_max_key_size = bte_appl_cfg.ble_max_key_size;
656   }
657 }
658 
659 /*******************************************************************************
660  *
661  * Function         bta_dm_ble_smp_cback
662  *
663  * Description      Callback for BLE SMP
664  *
665  *
666  * Returns          void
667  *
668  ******************************************************************************/
bta_dm_ble_smp_cback(tBTM_LE_EVT event,const RawAddress & bda,tBTM_LE_EVT_DATA * p_data)669 static tBTM_STATUS bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
670                                         tBTM_LE_EVT_DATA* p_data) {
671   tBTM_STATUS status = tBTM_STATUS::BTM_SUCCESS;
672   tBTA_DM_SEC sec_event = {};
673 
674   log::debug("addr:{},event:{}", bda, ble_evt_to_text(event));
675 
676   if (!bta_dm_sec_cb.p_sec_cback) {
677     return tBTM_STATUS::BTM_NOT_AUTHORIZED;
678   }
679 
680   DEV_CLASS dev_class = get_btm_client_interface().security.BTM_SecReadDevClass(bda);
681   if (!com::android::bluetooth::flags::read_le_appearance()) {
682     dev_class = kDevClassEmpty;
683   }
684 
685   switch (event) {
686     case BTM_LE_IO_REQ_EVT:
687       ble_io_req(bda, &p_data->io_req.io_cap, &p_data->io_req.oob_data, &p_data->io_req.auth_req,
688                  &p_data->io_req.max_key_size, &p_data->io_req.init_keys,
689                  &p_data->io_req.resp_keys);
690       log::info("io mitm:{} oob_data:{}", p_data->io_req.auth_req, p_data->io_req.oob_data);
691       break;
692 
693     case BTM_LE_CONSENT_REQ_EVT:
694       sec_event.ble_req.bd_addr = bda;
695       bd_name_from_char_pointer(sec_event.ble_req.bd_name,
696                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
697       sec_event.ble_req.dev_class = dev_class;
698       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_CONSENT_REQ_EVT, &sec_event);
699       break;
700 
701     case BTM_LE_SEC_REQUEST_EVT:
702       sec_event.ble_req.bd_addr = bda;
703       bd_name_from_char_pointer(sec_event.ble_req.bd_name,
704                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
705       sec_event.ble_req.dev_class = dev_class;
706       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
707       break;
708 
709     case BTM_LE_KEY_NOTIF_EVT:
710       sec_event.key_notif.bd_addr = bda;
711       bd_name_from_char_pointer(sec_event.key_notif.bd_name,
712                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
713       sec_event.key_notif.dev_class = dev_class;
714       sec_event.key_notif.passkey = p_data->key_notif;
715       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
716       break;
717 
718     case BTM_LE_KEY_REQ_EVT:
719       sec_event.pin_req.bd_addr = bda;
720       bd_name_from_char_pointer(sec_event.pin_req.bd_name,
721                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
722       sec_event.pin_req.dev_class = dev_class;
723       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
724       break;
725 
726     case BTM_LE_OOB_REQ_EVT:
727       sec_event.rmt_oob.bd_addr = bda;
728       bd_name_from_char_pointer(sec_event.rmt_oob.bd_name,
729                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
730       sec_event.rmt_oob.dev_class = dev_class;
731       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
732       break;
733 
734     case BTM_LE_NC_REQ_EVT:
735       sec_event.key_notif.bd_addr = bda;
736       bd_name_from_char_pointer(sec_event.key_notif.bd_name,
737                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
738       sec_event.key_notif.dev_class = dev_class;
739       sec_event.key_notif.passkey = p_data->key_notif;
740       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
741       break;
742 
743     case BTM_LE_SC_OOB_REQ_EVT:
744       sec_event.rmt_oob.bd_addr = bda;
745       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
746       break;
747 
748     case BTM_LE_SC_LOC_OOB_EVT:
749       tBTA_DM_LOC_OOB_DATA local_oob_data;
750       local_oob_data.local_oob_c = p_data->local_oob_data.commitment;
751       local_oob_data.local_oob_r = p_data->local_oob_data.randomizer;
752       sec_event.local_oob_data = local_oob_data;
753       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_SC_CR_LOC_OOB_EVT, &sec_event);
754       break;
755 
756     case BTM_LE_KEY_EVT:
757       sec_event.ble_key.bd_addr = bda;
758       sec_event.ble_key.key_type = p_data->key.key_type;
759       sec_event.ble_key.p_key_value = p_data->key.p_key_value;
760       bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
761       break;
762 
763     case BTM_LE_COMPLT_EVT:
764       sec_event.auth_cmpl.bd_addr = bda;
765       get_btm_client_interface().peer.BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type,
766                                                       &sec_event.auth_cmpl.addr_type);
767       bd_name_from_char_pointer(sec_event.auth_cmpl.bd_name,
768                                 get_btm_client_interface().security.BTM_SecReadDevName(bda));
769 
770       if (p_data->complt.reason != SMP_SUCCESS) {
771         // TODO This is not a proper use of this type
772         sec_event.auth_cmpl.fail_reason = static_cast<tHCI_STATUS>(
773                 BTA_DM_AUTH_CONVERT_SMP_CODE(static_cast<uint8_t>(p_data->complt.reason)));
774 
775         if (btm_sec_is_a_bonded_dev(bda) && p_data->complt.reason == SMP_CONN_TOUT &&
776             !p_data->complt.smp_over_br) {
777           // Bonded device failed to encrypt - to test this remove battery from
778           // HID device right after connection, but before encryption is
779           // established
780           log::warn("bonded device disconnected when encrypting - no reason to unbond");
781         } else {
782           /* delete this device entry from Sec Dev DB */
783           bta_dm_remove_sec_dev_entry(bda);
784         }
785 
786       } else {
787         sec_event.auth_cmpl.success = true;
788         if (!p_data->complt.smp_over_br) {
789           GATT_ConfigServiceChangeCCC(bda, true, BT_TRANSPORT_LE);
790         }
791       }
792 
793       if (bta_dm_sec_cb.p_sec_cback) {
794         // bta_dm_sec_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
795         bta_dm_sec_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
796       }
797       break;
798 
799     case BTM_LE_ADDR_ASSOC_EVT:
800       sec_event.proc_id_addr.pairing_bda = bda;
801       sec_event.proc_id_addr.id_addr = p_data->id_addr_with_type.bda;
802       sec_event.proc_id_addr.id_addr_type = p_data->id_addr_with_type.type;
803       bta_dm_sec_cb.p_sec_cback(BTA_DM_LE_ADDR_ASSOC_EVT, &sec_event);
804       break;
805 
806     default:
807       status = tBTM_STATUS::BTM_NOT_AUTHORIZED;
808       break;
809   }
810   return status;
811 }
812 
813 /*******************************************************************************
814  *
815  * Function         bta_dm_encrypt_cback
816  *
817  * Description      link encryption complete callback.
818  *
819  * Returns         None
820  *
821  ******************************************************************************/
bta_dm_encrypt_cback(RawAddress bd_addr,tBT_TRANSPORT transport,void *,tBTM_STATUS result)822 void bta_dm_encrypt_cback(RawAddress bd_addr, tBT_TRANSPORT transport, void* /* p_ref_data */,
823                           tBTM_STATUS result) {
824   tBTA_DM_ENCRYPT_CBACK* p_callback = nullptr;
825   tBTA_DM_PEER_DEVICE* device = find_connected_device(bd_addr, transport);
826   if (device != nullptr) {
827     p_callback = device->p_encrypt_cback;
828     device->p_encrypt_cback = nullptr;
829   }
830 
831   log::debug("Encrypted:{:c}, peer:{} transport:{} status:{} callback:{:c}",
832              result == tBTM_STATUS::BTM_SUCCESS ? 'T' : 'F', bd_addr, bt_transport_text(transport),
833              btm_status_text(result), (p_callback) ? 'T' : 'F');
834 
835   tBTA_STATUS bta_status = BTA_SUCCESS;
836   switch (result) {
837     case tBTM_STATUS::BTM_SUCCESS:
838       break;
839     case tBTM_STATUS::BTM_WRONG_MODE:
840       bta_status = BTA_WRONG_MODE;
841       break;
842     case tBTM_STATUS::BTM_NO_RESOURCES:
843       bta_status = BTA_NO_RESOURCES;
844       break;
845     case tBTM_STATUS::BTM_BUSY:
846       bta_status = BTA_BUSY;
847       break;
848     default:
849       bta_status = BTA_FAILURE;
850       break;
851   }
852 
853   if (p_callback) {
854     (*p_callback)(bd_addr, transport, bta_status);
855   }
856 }
857 
858 /**This function to encrypt the link */
bta_dm_set_encryption(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTM_BLE_SEC_ACT sec_act)859 void bta_dm_set_encryption(const RawAddress& bd_addr, tBT_TRANSPORT transport,
860                            tBTA_DM_ENCRYPT_CBACK* p_callback, tBTM_BLE_SEC_ACT sec_act) {
861   if (p_callback == nullptr) {
862     log::error("callback is not provided,addr:{}", bd_addr);
863     return;
864   }
865 
866   tBTA_DM_PEER_DEVICE* device = find_connected_device(bd_addr, transport);
867   if (device == nullptr) {
868     log::error("Unable to find active ACL connection device:{} transport:{}", bd_addr,
869                bt_transport_text(transport));
870     return;
871   }
872 
873   if (device->p_encrypt_cback) {
874     log::error("Unable to start encryption as already in progress peer:{} transport:{}", bd_addr,
875                bt_transport_text(transport));
876     (*p_callback)(bd_addr, transport, BTA_BUSY);
877     return;
878   }
879 
880   if (get_btm_client_interface().security.BTM_SetEncryption(bd_addr, transport,
881                                                             bta_dm_encrypt_cback, NULL, sec_act) ==
882       tBTM_STATUS::BTM_CMD_STARTED) {
883     device->p_encrypt_cback = p_callback;
884     log::debug("Started encryption peer:{} transport:{}", bd_addr, bt_transport_text(transport));
885   } else {
886     log::error("Unable to start encryption process peer:{} transport:{}", bd_addr,
887                bt_transport_text(transport));
888   }
889 }
890 
891 /*******************************************************************************
892  *
893  * Function         bta_dm_ble_id_key_cback
894  *
895  * Description      Callback for BLE local ID keys
896  *
897  *
898  * Returns          void
899  *
900  ******************************************************************************/
bta_dm_ble_id_key_cback(uint8_t key_type,tBTM_BLE_LOCAL_KEYS * p_key)901 static void bta_dm_ble_id_key_cback(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key) {
902   switch (key_type) {
903     case BTM_BLE_KEY_TYPE_ID:
904     case BTM_BLE_KEY_TYPE_ER:
905       if (bta_dm_sec_cb.p_sec_cback) {
906         tBTA_DM_SEC dm_key = {
907                 .ble_id_keys = {},
908         };
909         memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
910 
911         tBTA_DM_SEC_EVT evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT
912                                                                 : BTA_DM_BLE_LOCAL_ER_EVT;
913         bta_dm_sec_cb.p_sec_cback(evt, &dm_key);
914       }
915       break;
916 
917     default:
918       log::verbose("Unknown key type {}", key_type);
919       break;
920   }
921   return;
922 }
923 
924 /*******************************************************************************
925  *
926  * Function         bta_dm_sirk_verifiction_cback
927  *
928  * Description      SIRK verification when pairing CSIP set member.
929  *
930  * Returns          void
931  *
932  ******************************************************************************/
bta_dm_sirk_verifiction_cback(const RawAddress & bd_addr)933 static tBTM_STATUS bta_dm_sirk_verifiction_cback(const RawAddress& bd_addr) {
934   tBTA_DM_SEC sec_event = {.ble_req = {
935                                    .bd_addr = bd_addr,
936                            }};
937 
938   if (bta_dm_sec_cb.p_sec_sirk_cback) {
939     log::debug("callback called");
940     bta_dm_sec_cb.p_sec_sirk_cback(BTA_DM_SIRK_VERIFICATION_REQ_EVT, &sec_event);
941     return tBTM_STATUS::BTM_CMD_STARTED;
942   }
943 
944   log::debug("no callback registered");
945 
946   return tBTM_STATUS::BTM_SUCCESS_NO_SECURITY;
947 }
948 
949 /*******************************************************************************
950  *
951  * Function         bta_dm_add_blekey
952  *
953  * Description      This function adds a BLE Key to an security database entry.
954  *                  This function shall only be called AFTER BTA_DmAddBleDevice
955  *                  has been called.
956  *                  It is normally called during host startup to restore all
957  *                  required information stored in the NVRAM.
958  *
959  * Parameters:
960  *
961  ******************************************************************************/
bta_dm_add_blekey(const RawAddress & bd_addr,tBTA_LE_KEY_VALUE blekey,tBTM_LE_KEY_TYPE key_type)962 void bta_dm_add_blekey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE blekey,
963                        tBTM_LE_KEY_TYPE key_type) {
964   get_btm_client_interface().security.BTM_SecAddBleKey(bd_addr, (tBTM_LE_KEY_VALUE*)&blekey,
965                                                        key_type);
966 }
967 
968 /*******************************************************************************
969  *
970  * Function         bta_dm_add_ble_device
971  *
972  * Description      This function adds a BLE device to an security database
973  *                  entry.
974  *                  It is normally called during host startup to restore all
975  *                  required information stored in the NVRAM.
976  *
977  * Parameters:
978  *
979  ******************************************************************************/
bta_dm_add_ble_device(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_DEVICE_TYPE dev_type)980 void bta_dm_add_ble_device(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
981                            tBT_DEVICE_TYPE dev_type) {
982   get_btm_client_interface().security.BTM_SecAddBleDevice(bd_addr, dev_type, addr_type);
983 }
984 
985 /*******************************************************************************
986  *
987  * Function         bta_dm_add_ble_device
988  *
989  * Description      This function adds a BLE device to an security database
990  *                  entry.
991  *                  It is normally called during host startup to restore all
992  *                  required information stored in the NVRAM.
993  *
994  * Parameters:
995  *
996  ******************************************************************************/
bta_dm_ble_passkey_reply(const RawAddress & bd_addr,bool accept,uint32_t passkey)997 void bta_dm_ble_passkey_reply(const RawAddress& bd_addr, bool accept, uint32_t passkey) {
998   get_btm_client_interface().security.BTM_BlePasskeyReply(
999           bd_addr, accept ? tBTM_STATUS::BTM_SUCCESS : tBTM_STATUS::BTM_NOT_AUTHORIZED, passkey);
1000 }
1001 
1002 /** This is response to SM numeric comparison request submitted to application.
1003  */
bta_dm_ble_confirm_reply(const RawAddress & bd_addr,bool accept)1004 void bta_dm_ble_confirm_reply(const RawAddress& bd_addr, bool accept) {
1005   get_btm_client_interface().security.BTM_SecConfirmReqReply(
1006           accept ? tBTM_STATUS::BTM_SUCCESS : tBTM_STATUS::BTM_NOT_AUTHORIZED, BT_TRANSPORT_LE,
1007           bd_addr);
1008 }
1009 
1010 /** This function set the local device LE privacy settings. */
bta_dm_ble_config_local_privacy(bool privacy_enable)1011 void bta_dm_ble_config_local_privacy(bool privacy_enable) { BTM_BleConfigPrivacy(privacy_enable); }
1012 
1013 namespace bluetooth {
1014 namespace legacy {
1015 namespace testing {
bta_dm_sp_cback(tBTM_SP_EVT event,tBTM_SP_EVT_DATA * p_data)1016 tBTM_STATUS bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
1017   return ::bta_dm_sp_cback(event, p_data);
1018 }
1019 
1020 }  // namespace testing
1021 }  // namespace legacy
1022 }  // namespace bluetooth
1023