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