1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
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 file contains functions for BLE device control utilities, and LE
22 * security functions.
23 *
24 ******************************************************************************/
25
26 #define LOG_TAG "bt_btm_ble"
27
28 #include "bt_target.h"
29
30 #include <base/bind.h>
31 #include <string.h>
32
33 #include "bt_types.h"
34 #include "bt_utils.h"
35 #include "btm_ble_api.h"
36 #include "btm_int.h"
37 #include "btu.h"
38 #include "device/include/controller.h"
39 #include "gap_api.h"
40 #include "gatt_api.h"
41 #include "hcimsgs.h"
42 #include "l2c_int.h"
43 #include "log/log.h"
44 #include "main/shim/btm_api.h"
45 #include "main/shim/shim.h"
46 #include "osi/include/log.h"
47 #include "osi/include/osi.h"
48 #include "stack/crypto_toolbox/crypto_toolbox.h"
49
50 extern void gatt_notify_phy_updated(uint8_t status, uint16_t handle,
51 uint8_t tx_phy, uint8_t rx_phy);
52
53 /******************************************************************************/
54 /* External Function to be called by other modules */
55 /******************************************************************************/
56 /********************************************************
57 *
58 * Function BTM_SecAddBleDevice
59 *
60 * Description Add/modify device. This function will be normally called
61 * during host startup to restore all required information
62 * for a LE device stored in the NVRAM.
63 *
64 * Parameters: bd_addr - BD address of the peer
65 * bd_name - Name of the peer device. NULL if unknown.
66 * dev_type - Remote device's device type.
67 * addr_type - LE device address type.
68 *
69 * Returns true if added OK, else false
70 *
71 ******************************************************************************/
BTM_SecAddBleDevice(const RawAddress & bd_addr,BD_NAME bd_name,tBT_DEVICE_TYPE dev_type,tBLE_ADDR_TYPE addr_type)72 bool BTM_SecAddBleDevice(const RawAddress& bd_addr, BD_NAME bd_name,
73 tBT_DEVICE_TYPE dev_type, tBLE_ADDR_TYPE addr_type) {
74 if (bluetooth::shim::is_gd_shim_enabled()) {
75 return bluetooth::shim::BTM_SecAddBleDevice(bd_addr, bd_name, dev_type,
76 addr_type);
77 }
78
79 BTM_TRACE_DEBUG("%s: dev_type=0x%x", __func__, dev_type);
80
81 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
82 if (!p_dev_rec) {
83 p_dev_rec = btm_sec_allocate_dev_rec();
84
85 p_dev_rec->bd_addr = bd_addr;
86 p_dev_rec->hci_handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_BR_EDR);
87 p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_LE);
88
89 /* update conn params, use default value for background connection params */
90 p_dev_rec->conn_params.min_conn_int = BTM_BLE_CONN_PARAM_UNDEF;
91 p_dev_rec->conn_params.max_conn_int = BTM_BLE_CONN_PARAM_UNDEF;
92 p_dev_rec->conn_params.supervision_tout = BTM_BLE_CONN_PARAM_UNDEF;
93 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_PARAM_UNDEF;
94
95 BTM_TRACE_DEBUG("%s: Device added, handle=0x%x, p_dev_rec=%p, bd_addr=%s",
96 __func__, p_dev_rec->ble_hci_handle, p_dev_rec,
97 bd_addr.ToString().c_str());
98 }
99
100 memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
101
102 if (bd_name && bd_name[0]) {
103 p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
104 strlcpy((char*)p_dev_rec->sec_bd_name, (char*)bd_name,
105 BTM_MAX_REM_BD_NAME_LEN);
106 }
107 p_dev_rec->device_type |= dev_type;
108 p_dev_rec->ble.ble_addr_type = addr_type;
109
110 p_dev_rec->ble.pseudo_addr = bd_addr;
111 /* sync up with the Inq Data base*/
112 tBTM_INQ_INFO* p_info = BTM_InqDbRead(bd_addr);
113 if (p_info) {
114 p_info->results.ble_addr_type = p_dev_rec->ble.ble_addr_type;
115 p_info->results.device_type = p_dev_rec->device_type;
116 BTM_TRACE_DEBUG("InqDb device_type =0x%x addr_type=0x%x",
117 p_info->results.device_type, p_info->results.ble_addr_type);
118 }
119
120 return true;
121 }
122
123 /*******************************************************************************
124 *
125 * Function BTM_SecAddBleKey
126 *
127 * Description Add/modify LE device information. This function will be
128 * normally called during host startup to restore all required
129 * information stored in the NVRAM.
130 *
131 * Parameters: bd_addr - BD address of the peer
132 * p_le_key - LE key values.
133 * key_type - LE SMP key type.
134 *
135 * Returns true if added OK, else false
136 *
137 ******************************************************************************/
BTM_SecAddBleKey(const RawAddress & bd_addr,tBTM_LE_KEY_VALUE * p_le_key,tBTM_LE_KEY_TYPE key_type)138 bool BTM_SecAddBleKey(const RawAddress& bd_addr, tBTM_LE_KEY_VALUE* p_le_key,
139 tBTM_LE_KEY_TYPE key_type) {
140 if (bluetooth::shim::is_gd_shim_enabled()) {
141 return bluetooth::shim::BTM_SecAddBleKey(bd_addr, p_le_key, key_type);
142 }
143
144 tBTM_SEC_DEV_REC* p_dev_rec;
145 BTM_TRACE_DEBUG("BTM_SecAddBleKey");
146 p_dev_rec = btm_find_dev(bd_addr);
147 if (!p_dev_rec || !p_le_key ||
148 (key_type != BTM_LE_KEY_PENC && key_type != BTM_LE_KEY_PID &&
149 key_type != BTM_LE_KEY_PCSRK && key_type != BTM_LE_KEY_LENC &&
150 key_type != BTM_LE_KEY_LCSRK && key_type != BTM_LE_KEY_LID)) {
151 LOG(WARNING) << __func__
152 << " Wrong Type, or No Device record for bdaddr: " << bd_addr
153 << ", Type: " << key_type;
154 return (false);
155 }
156
157 VLOG(1) << __func__ << " BDA: " << bd_addr << ", Type: " << key_type;
158
159 btm_sec_save_le_key(bd_addr, key_type, p_le_key, false);
160
161 #if (BLE_PRIVACY_SPT == TRUE)
162 if (key_type == BTM_LE_KEY_PID || key_type == BTM_LE_KEY_LID)
163 btm_ble_resolving_list_load_dev(p_dev_rec);
164 #endif
165
166 return (true);
167 }
168
169 /*******************************************************************************
170 *
171 * Function BTM_BleLoadLocalKeys
172 *
173 * Description Local local identity key, encryption root or sign counter.
174 *
175 * Parameters: key_type: type of key, can be BTM_BLE_KEY_TYPE_ID,
176 * BTM_BLE_KEY_TYPE_ER
177 * or BTM_BLE_KEY_TYPE_COUNTER.
178 * p_key: pointer to the key.
179 *
180 * Returns non2.
181 *
182 ******************************************************************************/
BTM_BleLoadLocalKeys(uint8_t key_type,tBTM_BLE_LOCAL_KEYS * p_key)183 void BTM_BleLoadLocalKeys(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key) {
184 if (bluetooth::shim::is_gd_shim_enabled()) {
185 return bluetooth::shim::BTM_BleLoadLocalKeys(key_type, p_key);
186 }
187
188 tBTM_DEVCB* p_devcb = &btm_cb.devcb;
189 BTM_TRACE_DEBUG("%s", __func__);
190 if (p_key != NULL) {
191 switch (key_type) {
192 case BTM_BLE_KEY_TYPE_ID:
193 memcpy(&p_devcb->id_keys, &p_key->id_keys,
194 sizeof(tBTM_BLE_LOCAL_ID_KEYS));
195 break;
196
197 case BTM_BLE_KEY_TYPE_ER:
198 p_devcb->ble_encryption_key_value = p_key->er;
199 break;
200
201 default:
202 BTM_TRACE_ERROR("unknow local key type: %d", key_type);
203 break;
204 }
205 }
206 }
207
208 /** Returns local device encryption root (ER) */
BTM_GetDeviceEncRoot()209 const Octet16& BTM_GetDeviceEncRoot() {
210 if (bluetooth::shim::is_gd_shim_enabled()) {
211 return bluetooth::shim::BTM_GetDeviceEncRoot();
212 }
213 return btm_cb.devcb.ble_encryption_key_value;
214 }
215
216 /** Returns local device identity root (IR). */
BTM_GetDeviceIDRoot()217 const Octet16& BTM_GetDeviceIDRoot() {
218 if (bluetooth::shim::is_gd_shim_enabled()) {
219 return bluetooth::shim::BTM_GetDeviceIDRoot();
220 }
221 return btm_cb.devcb.id_keys.irk;
222 }
223
224 /** Return local device DHK. */
BTM_GetDeviceDHK()225 const Octet16& BTM_GetDeviceDHK() {
226 if (bluetooth::shim::is_gd_shim_enabled()) {
227 return bluetooth::shim::BTM_GetDeviceDHK();
228 }
229 return btm_cb.devcb.id_keys.dhk;
230 }
231
232 /*******************************************************************************
233 *
234 * Function BTM_ReadConnectionAddr
235 *
236 * Description This function is called to get the local device address
237 * information.
238 *
239 * Returns void
240 *
241 ******************************************************************************/
BTM_ReadConnectionAddr(const RawAddress & remote_bda,RawAddress & local_conn_addr,tBLE_ADDR_TYPE * p_addr_type)242 void BTM_ReadConnectionAddr(const RawAddress& remote_bda,
243 RawAddress& local_conn_addr,
244 tBLE_ADDR_TYPE* p_addr_type) {
245 if (bluetooth::shim::is_gd_shim_enabled()) {
246 return bluetooth::shim::BTM_ReadConnectionAddr(remote_bda, local_conn_addr,
247 p_addr_type);
248 }
249 tACL_CONN* p_acl = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
250
251 if (p_acl == NULL) {
252 BTM_TRACE_ERROR("No connection exist!");
253 return;
254 }
255 local_conn_addr = p_acl->conn_addr;
256 *p_addr_type = p_acl->conn_addr_type;
257
258 BTM_TRACE_DEBUG("BTM_ReadConnectionAddr address type: %d addr: 0x%02x",
259 p_acl->conn_addr_type, p_acl->conn_addr.address[0]);
260 }
261
262 /*******************************************************************************
263 *
264 * Function BTM_IsBleConnection
265 *
266 * Description This function is called to check if the connection handle
267 * for an LE link
268 *
269 * Returns true if connection is LE link, otherwise false.
270 *
271 ******************************************************************************/
BTM_IsBleConnection(uint16_t conn_handle)272 bool BTM_IsBleConnection(uint16_t conn_handle) {
273 if (bluetooth::shim::is_gd_shim_enabled()) {
274 ASSERT_LOG(false, "This should not be invoked from code path");
275 }
276 uint8_t xx;
277 tACL_CONN* p;
278
279 BTM_TRACE_API("BTM_IsBleConnection: conn_handle: %d", conn_handle);
280
281 xx = btm_handle_to_acl_index(conn_handle);
282 if (xx >= MAX_L2CAP_LINKS) return false;
283
284 p = &btm_cb.acl_db[xx];
285
286 return (p->transport == BT_TRANSPORT_LE);
287 }
288
289 /*******************************************************************************
290 *
291 * Function BTM_ReadRemoteConnectionAddr
292 *
293 * Description This function is read the remote device address currently used
294 *
295 * Parameters pseudo_addr: pseudo random address available
296 * conn_addr:connection address used
297 * p_addr_type : BD Address type, Public or Random of the address
298 * used
299 *
300 * Returns bool, true if connection to remote device exists, else false
301 *
302 ******************************************************************************/
BTM_ReadRemoteConnectionAddr(const RawAddress & pseudo_addr,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type)303 bool BTM_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr,
304 RawAddress& conn_addr,
305 tBLE_ADDR_TYPE* p_addr_type) {
306 if (bluetooth::shim::is_gd_shim_enabled()) {
307 return bluetooth::shim::BTM_ReadRemoteConnectionAddr(pseudo_addr, conn_addr,
308 p_addr_type);
309 }
310 bool st = true;
311 #if (BLE_PRIVACY_SPT == TRUE)
312 tACL_CONN* p = btm_bda_to_acl(pseudo_addr, BT_TRANSPORT_LE);
313
314 if (p == NULL) {
315 BTM_TRACE_ERROR(
316 "BTM_ReadRemoteConnectionAddr can not find connection"
317 " with matching address");
318 return false;
319 }
320
321 conn_addr = p->active_remote_addr;
322 *p_addr_type = p->active_remote_addr_type;
323 #else
324 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(pseudo_addr);
325
326 conn_addr = pseudo_addr;
327 if (p_dev_rec != NULL) {
328 *p_addr_type = p_dev_rec->ble.ble_addr_type;
329 }
330 #endif
331 return st;
332 }
333 /*******************************************************************************
334 *
335 * Function BTM_SecurityGrant
336 *
337 * Description This function is called to grant security process.
338 *
339 * Parameters bd_addr - peer device bd address.
340 * res - result of the operation BTM_SUCCESS if success.
341 * Otherwise, BTM_REPEATED_ATTEMPTS if too many
342 * attempts.
343 *
344 * Returns None
345 *
346 ******************************************************************************/
BTM_SecurityGrant(const RawAddress & bd_addr,uint8_t res)347 void BTM_SecurityGrant(const RawAddress& bd_addr, uint8_t res) {
348 if (bluetooth::shim::is_gd_shim_enabled()) {
349 return bluetooth::shim::BTM_SecurityGrant(bd_addr, res);
350 }
351 tSMP_STATUS res_smp =
352 (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_REPEATED_ATTEMPTS;
353 BTM_TRACE_DEBUG("BTM_SecurityGrant");
354 SMP_SecurityGrant(bd_addr, res_smp);
355 }
356
357 /*******************************************************************************
358 *
359 * Function BTM_BlePasskeyReply
360 *
361 * Description This function is called after Security Manager submitted
362 * passkey request to the application.
363 *
364 * Parameters: bd_addr - Address of the device for which passkey was
365 * requested
366 * res - result of the operation BTM_SUCCESS if success
367 * key_len - length in bytes of the Passkey
368 * p_passkey - pointer to array with the passkey
369 * trusted_mask - bitwise OR of trusted services (array of
370 * uint32_t)
371 *
372 ******************************************************************************/
BTM_BlePasskeyReply(const RawAddress & bd_addr,uint8_t res,uint32_t passkey)373 void BTM_BlePasskeyReply(const RawAddress& bd_addr, uint8_t res,
374 uint32_t passkey) {
375 if (bluetooth::shim::is_gd_shim_enabled()) {
376 ASSERT_LOG(false, "This should not be invoked from code path");
377 }
378 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
379 tSMP_STATUS res_smp =
380 (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
381
382 if (p_dev_rec == NULL) {
383 BTM_TRACE_ERROR("Passkey reply to Unknown device");
384 return;
385 }
386
387 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
388 BTM_TRACE_DEBUG("BTM_BlePasskeyReply");
389 SMP_PasskeyReply(bd_addr, res_smp, passkey);
390 }
391
392 /*******************************************************************************
393 *
394 * Function BTM_BleConfirmReply
395 *
396 * Description This function is called after Security Manager submitted
397 * numeric comparison request to the application.
398 *
399 * Parameters: bd_addr - Address of the device with which numeric
400 * comparison was requested
401 * res - comparison result BTM_SUCCESS if success
402 *
403 ******************************************************************************/
BTM_BleConfirmReply(const RawAddress & bd_addr,uint8_t res)404 void BTM_BleConfirmReply(const RawAddress& bd_addr, uint8_t res) {
405 if (bluetooth::shim::is_gd_shim_enabled()) {
406 ASSERT_LOG(false, "This should not be invoked from code path");
407 }
408 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
409 tSMP_STATUS res_smp =
410 (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
411
412 if (p_dev_rec == NULL) {
413 BTM_TRACE_ERROR("Passkey reply to Unknown device");
414 return;
415 }
416
417 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
418 BTM_TRACE_DEBUG("%s", __func__);
419 SMP_ConfirmReply(bd_addr, res_smp);
420 }
421
422 /*******************************************************************************
423 *
424 * Function BTM_BleOobDataReply
425 *
426 * Description This function is called to provide the OOB data for
427 * SMP in response to BTM_LE_OOB_REQ_EVT
428 *
429 * Parameters: bd_addr - Address of the peer device
430 * res - result of the operation SMP_SUCCESS if success
431 * p_data - oob data, depending on transport and
432 * capabilities.
433 * Might be "Simple Pairing Randomizer", or
434 * "Security Manager TK Value".
435 *
436 ******************************************************************************/
BTM_BleOobDataReply(const RawAddress & bd_addr,uint8_t res,uint8_t len,uint8_t * p_data)437 void BTM_BleOobDataReply(const RawAddress& bd_addr, uint8_t res, uint8_t len,
438 uint8_t* p_data) {
439 if (bluetooth::shim::is_gd_shim_enabled()) {
440 return bluetooth::shim::BTM_BleOobDataReply(bd_addr, res, len, p_data);
441 }
442 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_OOB_FAIL;
443 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
444
445 BTM_TRACE_DEBUG("%s:", __func__);
446
447 if (p_dev_rec == NULL) {
448 BTM_TRACE_ERROR("%s: Unknown device", __func__);
449 return;
450 }
451
452 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
453 SMP_OobDataReply(bd_addr, res_smp, len, p_data);
454 }
455
456 /*******************************************************************************
457 *
458 * Function BTM_BleSecureConnectionOobDataReply
459 *
460 * Description This function is called to provide the OOB data for
461 * SMP in response to BTM_LE_OOB_REQ_EVT when secure connection
462 * data is available
463 *
464 * Parameters: bd_addr - Address of the peer device
465 * p_c - pointer to Confirmation.
466 * p_r - pointer to Randomizer
467 *
468 ******************************************************************************/
BTM_BleSecureConnectionOobDataReply(const RawAddress & bd_addr,uint8_t * p_c,uint8_t * p_r)469 void BTM_BleSecureConnectionOobDataReply(const RawAddress& bd_addr,
470 uint8_t* p_c, uint8_t* p_r) {
471 if (bluetooth::shim::is_gd_shim_enabled()) {
472 return bluetooth::shim::BTM_BleSecureConnectionOobDataReply(bd_addr, p_c,
473 p_r);
474 }
475 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
476
477 BTM_TRACE_DEBUG("%s:", __func__);
478
479 if (p_dev_rec == NULL) {
480 BTM_TRACE_ERROR("%s: Unknown device", __func__);
481 return;
482 }
483
484 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
485
486 tSMP_SC_OOB_DATA oob;
487 memset(&oob, 0, sizeof(tSMP_SC_OOB_DATA));
488
489 oob.peer_oob_data.present = true;
490 memcpy(&oob.peer_oob_data.randomizer, p_r, OCTET16_LEN);
491 memcpy(&oob.peer_oob_data.commitment, p_c, OCTET16_LEN);
492 oob.peer_oob_data.addr_rcvd_from.type = p_dev_rec->ble.ble_addr_type;
493 oob.peer_oob_data.addr_rcvd_from.bda = bd_addr;
494
495 SMP_SecureConnectionOobDataReply((uint8_t*)&oob);
496 }
497
498 /********************************************************
499 *
500 * Function BTM_BleSetPrefConnParams
501 *
502 * Description Set a peripheral's preferred connection parameters
503 *
504 * Parameters: bd_addr - BD address of the peripheral
505 * scan_interval: scan interval
506 * scan_window: scan window
507 * min_conn_int - minimum preferred connection interval
508 * max_conn_int - maximum preferred connection interval
509 * slave_latency - preferred slave latency
510 * supervision_tout - preferred supervision timeout
511 *
512 * Returns void
513 *
514 ******************************************************************************/
BTM_BleSetPrefConnParams(const RawAddress & bd_addr,uint16_t min_conn_int,uint16_t max_conn_int,uint16_t slave_latency,uint16_t supervision_tout)515 void BTM_BleSetPrefConnParams(const RawAddress& bd_addr, uint16_t min_conn_int,
516 uint16_t max_conn_int, uint16_t slave_latency,
517 uint16_t supervision_tout) {
518 if (bluetooth::shim::is_gd_shim_enabled()) {
519 return bluetooth::shim::BTM_BleSetPrefConnParams(
520 bd_addr, min_conn_int, max_conn_int, slave_latency, supervision_tout);
521 }
522 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
523
524 BTM_TRACE_API(
525 "BTM_BleSetPrefConnParams min: %u max: %u latency: %u \
526 tout: %u",
527 min_conn_int, max_conn_int, slave_latency, supervision_tout);
528
529 if (BTM_BLE_ISVALID_PARAM(min_conn_int, BTM_BLE_CONN_INT_MIN,
530 BTM_BLE_CONN_INT_MAX) &&
531 BTM_BLE_ISVALID_PARAM(max_conn_int, BTM_BLE_CONN_INT_MIN,
532 BTM_BLE_CONN_INT_MAX) &&
533 BTM_BLE_ISVALID_PARAM(supervision_tout, BTM_BLE_CONN_SUP_TOUT_MIN,
534 BTM_BLE_CONN_SUP_TOUT_MAX) &&
535 (slave_latency <= BTM_BLE_CONN_LATENCY_MAX ||
536 slave_latency == BTM_BLE_CONN_PARAM_UNDEF)) {
537 if (p_dev_rec) {
538 /* expect conn int and stout and slave latency to be updated all together
539 */
540 if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF ||
541 max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
542 if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
543 p_dev_rec->conn_params.min_conn_int = min_conn_int;
544 else
545 p_dev_rec->conn_params.min_conn_int = max_conn_int;
546
547 if (max_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
548 p_dev_rec->conn_params.max_conn_int = max_conn_int;
549 else
550 p_dev_rec->conn_params.max_conn_int = min_conn_int;
551
552 if (slave_latency != BTM_BLE_CONN_PARAM_UNDEF)
553 p_dev_rec->conn_params.slave_latency = slave_latency;
554 else
555 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
556
557 if (supervision_tout != BTM_BLE_CONN_PARAM_UNDEF)
558 p_dev_rec->conn_params.supervision_tout = supervision_tout;
559 else
560 p_dev_rec->conn_params.supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
561 }
562
563 } else {
564 BTM_TRACE_ERROR("Unknown Device, setting rejected");
565 }
566 } else {
567 BTM_TRACE_ERROR("Illegal Connection Parameters");
568 }
569 }
570
571 /*******************************************************************************
572 *
573 * Function BTM_ReadDevInfo
574 *
575 * Description This function is called to read the device/address type
576 * of BD address.
577 *
578 * Parameter remote_bda: remote device address
579 * p_dev_type: output parameter to read the device type.
580 * p_addr_type: output parameter to read the address type.
581 *
582 ******************************************************************************/
BTM_ReadDevInfo(const RawAddress & remote_bda,tBT_DEVICE_TYPE * p_dev_type,tBLE_ADDR_TYPE * p_addr_type)583 void BTM_ReadDevInfo(const RawAddress& remote_bda, tBT_DEVICE_TYPE* p_dev_type,
584 tBLE_ADDR_TYPE* p_addr_type) {
585 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(remote_bda);
586 tBTM_INQ_INFO* p_inq_info = BTM_InqDbRead(remote_bda);
587
588 *p_addr_type = BLE_ADDR_PUBLIC;
589
590 if (!p_dev_rec) {
591 *p_dev_type = BT_DEVICE_TYPE_BREDR;
592 /* Check with the BT manager if details about remote device are known */
593 if (p_inq_info != NULL) {
594 *p_dev_type = p_inq_info->results.device_type;
595 *p_addr_type = p_inq_info->results.ble_addr_type;
596 } else {
597 /* unknown device, assume BR/EDR */
598 BTM_TRACE_DEBUG("btm_find_dev_type - unknown device, BR/EDR assumed");
599 }
600 } else /* there is a security device record exisitng */
601 {
602 /* new inquiry result, overwrite device type in security device record */
603 if (p_inq_info) {
604 p_dev_rec->device_type = p_inq_info->results.device_type;
605 p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
606 }
607 if (p_dev_rec->bd_addr == remote_bda &&
608 p_dev_rec->ble.pseudo_addr == remote_bda) {
609 *p_dev_type = p_dev_rec->device_type;
610 *p_addr_type = p_dev_rec->ble.ble_addr_type;
611 } else if (p_dev_rec->ble.pseudo_addr == remote_bda) {
612 *p_dev_type = BT_DEVICE_TYPE_BLE;
613 *p_addr_type = p_dev_rec->ble.ble_addr_type;
614 } else /* matching static adddress only */
615 {
616 *p_dev_type = BT_DEVICE_TYPE_BREDR;
617 *p_addr_type = BLE_ADDR_PUBLIC;
618 }
619 }
620
621 BTM_TRACE_DEBUG("btm_find_dev_type - device_type = %d addr_type = %d",
622 *p_dev_type, *p_addr_type);
623 }
624
625 /*******************************************************************************
626 *
627 * Function BTM_ReadConnectedTransportAddress
628 *
629 * Description This function is called to read the paired device/address
630 * type of other device paired corresponding to the BD_address
631 *
632 * Parameter remote_bda: remote device address, carry out the transport
633 * address
634 * transport: active transport
635 *
636 * Return true if an active link is identified; false otherwise
637 *
638 ******************************************************************************/
BTM_ReadConnectedTransportAddress(RawAddress * remote_bda,tBT_TRANSPORT transport)639 bool BTM_ReadConnectedTransportAddress(RawAddress* remote_bda,
640 tBT_TRANSPORT transport) {
641 if (bluetooth::shim::is_gd_shim_enabled()) {
642 return bluetooth::shim::BTM_ReadConnectedTransportAddress(remote_bda,
643 transport);
644 }
645 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(*remote_bda);
646
647 /* if no device can be located, return */
648 if (p_dev_rec == NULL) return false;
649
650 if (transport == BT_TRANSPORT_BR_EDR) {
651 if (btm_bda_to_acl(p_dev_rec->bd_addr, transport) != NULL) {
652 *remote_bda = p_dev_rec->bd_addr;
653 return true;
654 } else if (p_dev_rec->device_type & BT_DEVICE_TYPE_BREDR) {
655 *remote_bda = p_dev_rec->bd_addr;
656 } else
657 *remote_bda = RawAddress::kEmpty;
658 return false;
659 }
660
661 if (transport == BT_TRANSPORT_LE) {
662 *remote_bda = p_dev_rec->ble.pseudo_addr;
663 if (btm_bda_to_acl(p_dev_rec->ble.pseudo_addr, transport) != NULL)
664 return true;
665 else
666 return false;
667 }
668
669 return false;
670 }
671
672 /*******************************************************************************
673 *
674 * Function BTM_BleReceiverTest
675 *
676 * Description This function is called to start the LE Receiver test
677 *
678 * Parameter rx_freq - Frequency Range
679 * p_cmd_cmpl_cback - Command Complete callback
680 *
681 ******************************************************************************/
BTM_BleReceiverTest(uint8_t rx_freq,tBTM_CMPL_CB * p_cmd_cmpl_cback)682 void BTM_BleReceiverTest(uint8_t rx_freq, tBTM_CMPL_CB* p_cmd_cmpl_cback) {
683 if (bluetooth::shim::is_gd_shim_enabled()) {
684 return bluetooth::shim::BTM_BleReceiverTest(rx_freq, p_cmd_cmpl_cback);
685 }
686 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
687
688 btsnd_hcic_ble_receiver_test(rx_freq);
689 }
690
691 /*******************************************************************************
692 *
693 * Function BTM_BleTransmitterTest
694 *
695 * Description This function is called to start the LE Transmitter test
696 *
697 * Parameter tx_freq - Frequency Range
698 * test_data_len - Length in bytes of payload data in each
699 * packet
700 * packet_payload - Pattern to use in the payload
701 * p_cmd_cmpl_cback - Command Complete callback
702 *
703 ******************************************************************************/
BTM_BleTransmitterTest(uint8_t tx_freq,uint8_t test_data_len,uint8_t packet_payload,tBTM_CMPL_CB * p_cmd_cmpl_cback)704 void BTM_BleTransmitterTest(uint8_t tx_freq, uint8_t test_data_len,
705 uint8_t packet_payload,
706 tBTM_CMPL_CB* p_cmd_cmpl_cback) {
707 if (bluetooth::shim::is_gd_shim_enabled()) {
708 return bluetooth::shim::BTM_BleTransmitterTest(
709 tx_freq, test_data_len, packet_payload, p_cmd_cmpl_cback);
710 }
711 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
712 btsnd_hcic_ble_transmitter_test(tx_freq, test_data_len, packet_payload);
713 }
714
715 /*******************************************************************************
716 *
717 * Function BTM_BleTestEnd
718 *
719 * Description This function is called to stop the in-progress TX or RX
720 * test
721 *
722 * Parameter p_cmd_cmpl_cback - Command complete callback
723 *
724 ******************************************************************************/
BTM_BleTestEnd(tBTM_CMPL_CB * p_cmd_cmpl_cback)725 void BTM_BleTestEnd(tBTM_CMPL_CB* p_cmd_cmpl_cback) {
726 if (bluetooth::shim::is_gd_shim_enabled()) {
727 return bluetooth::shim::BTM_BleTestEnd(p_cmd_cmpl_cback);
728 }
729 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
730
731 btsnd_hcic_ble_test_end();
732 }
733
734 /*******************************************************************************
735 * Internal Functions
736 ******************************************************************************/
btm_ble_test_command_complete(uint8_t * p)737 void btm_ble_test_command_complete(uint8_t* p) {
738 tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_le_test_cmd_cmpl_cb;
739
740 btm_cb.devcb.p_le_test_cmd_cmpl_cb = NULL;
741
742 if (p_cb) {
743 (*p_cb)(p);
744 }
745 }
746
747 /*******************************************************************************
748 *
749 * Function BTM_UseLeLink
750 *
751 * Description This function is to select the underlying physical link to
752 * use.
753 *
754 * Returns true to use LE, false use BR/EDR.
755 *
756 ******************************************************************************/
BTM_UseLeLink(const RawAddress & bd_addr)757 bool BTM_UseLeLink(const RawAddress& bd_addr) {
758 if (bluetooth::shim::is_gd_shim_enabled()) {
759 return bluetooth::shim::BTM_UseLeLink(bd_addr);
760 }
761 tACL_CONN* p;
762 tBT_DEVICE_TYPE dev_type;
763 tBLE_ADDR_TYPE addr_type;
764 bool use_le = false;
765
766 p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
767 if (p != NULL) {
768 return use_le;
769 } else {
770 p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
771 if (p != NULL) {
772 use_le = true;
773 } else {
774 BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
775 use_le = (dev_type == BT_DEVICE_TYPE_BLE);
776 }
777 }
778 return use_le;
779 }
780
781 /*******************************************************************************
782 *
783 * Function BTM_SetBleDataLength
784 *
785 * Description This function is to set maximum BLE transmission packet size
786 *
787 * Returns BTM_SUCCESS if success; otherwise failed.
788 *
789 ******************************************************************************/
BTM_SetBleDataLength(const RawAddress & bd_addr,uint16_t tx_pdu_length)790 tBTM_STATUS BTM_SetBleDataLength(const RawAddress& bd_addr,
791 uint16_t tx_pdu_length) {
792 if (bluetooth::shim::is_gd_shim_enabled()) {
793 return bluetooth::shim::BTM_SetBleDataLength(bd_addr, tx_pdu_length);
794 }
795 tACL_CONN* p_acl = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
796 uint16_t tx_time = BTM_BLE_DATA_TX_TIME_MAX_LEGACY;
797
798 if (p_acl == NULL) {
799 BTM_TRACE_ERROR("%s: Wrong mode: no LE link exist or LE not supported",
800 __func__);
801 return BTM_WRONG_MODE;
802 }
803
804 BTM_TRACE_DEBUG("%s: tx_pdu_length =%d", __func__, tx_pdu_length);
805
806 if (!controller_get_interface()->supports_ble_packet_extension()) {
807 BTM_TRACE_ERROR("%s failed, request not supported", __func__);
808 return BTM_ILLEGAL_VALUE;
809 }
810
811 if (!HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl->peer_le_features)) {
812 BTM_TRACE_ERROR("%s failed, peer does not support request", __func__);
813 return BTM_ILLEGAL_VALUE;
814 }
815
816 if (tx_pdu_length > BTM_BLE_DATA_SIZE_MAX)
817 tx_pdu_length = BTM_BLE_DATA_SIZE_MAX;
818 else if (tx_pdu_length < BTM_BLE_DATA_SIZE_MIN)
819 tx_pdu_length = BTM_BLE_DATA_SIZE_MIN;
820
821 if (controller_get_interface()->get_bt_version()->hci_version >= HCI_PROTO_VERSION_5_0)
822 tx_time = BTM_BLE_DATA_TX_TIME_MAX;
823
824 btsnd_hcic_ble_set_data_length(p_acl->hci_handle, tx_pdu_length, tx_time);
825
826 return BTM_SUCCESS;
827 }
828
read_phy_cb(base::Callback<void (uint8_t tx_phy,uint8_t rx_phy,uint8_t status)> cb,uint8_t * data,uint16_t len)829 void read_phy_cb(
830 base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb,
831 uint8_t* data, uint16_t len) {
832 uint8_t status, tx_phy, rx_phy;
833 uint16_t handle;
834
835 LOG_ASSERT(len == 5) << "Received bad response length: " << len;
836 uint8_t* pp = data;
837 STREAM_TO_UINT8(status, pp);
838 STREAM_TO_UINT16(handle, pp);
839 handle = handle & 0x0FFF;
840 STREAM_TO_UINT8(tx_phy, pp);
841 STREAM_TO_UINT8(rx_phy, pp);
842
843 DVLOG(1) << __func__ << " Received read_phy_cb";
844 cb.Run(tx_phy, rx_phy, status);
845 }
846
847 /*******************************************************************************
848 *
849 * Function BTM_BleReadPhy
850 *
851 * Description To read the current PHYs for specified LE connection
852 *
853 *
854 * Returns BTM_SUCCESS if command successfully sent to controller,
855 * BTM_MODE_UNSUPPORTED if local controller doesn't support LE
856 * 2M or LE Coded PHY,
857 * BTM_WRONG_MODE if Device in wrong mode for request.
858 *
859 ******************************************************************************/
BTM_BleReadPhy(const RawAddress & bd_addr,base::Callback<void (uint8_t tx_phy,uint8_t rx_phy,uint8_t status)> cb)860 void BTM_BleReadPhy(
861 const RawAddress& bd_addr,
862 base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb) {
863 if (bluetooth::shim::is_gd_shim_enabled()) {
864 return bluetooth::shim::BTM_BleReadPhy(bd_addr, cb);
865 }
866 BTM_TRACE_DEBUG("%s", __func__);
867
868 tACL_CONN* p_acl = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
869
870 if (p_acl == NULL) {
871 BTM_TRACE_ERROR("%s: Wrong mode: no LE link exist or LE not supported",
872 __func__);
873 cb.Run(0, 0, HCI_ERR_NO_CONNECTION);
874 return;
875 }
876
877 // checking if local controller supports it!
878 if (!controller_get_interface()->supports_ble_2m_phy() &&
879 !controller_get_interface()->supports_ble_coded_phy()) {
880 BTM_TRACE_ERROR("%s failed, request not supported in local controller!",
881 __func__);
882 cb.Run(0, 0, GATT_REQ_NOT_SUPPORTED);
883 return;
884 }
885
886 uint16_t handle = p_acl->hci_handle;
887
888 const uint8_t len = HCIC_PARAM_SIZE_BLE_READ_PHY;
889 uint8_t data[len];
890 uint8_t* pp = data;
891 UINT16_TO_STREAM(pp, handle);
892 btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_READ_PHY, data, len,
893 base::Bind(&read_phy_cb, std::move(cb)));
894 return;
895 }
896
doNothing(uint8_t * data,uint16_t len)897 void doNothing(uint8_t* data, uint16_t len) {}
898
899 /*******************************************************************************
900 *
901 * Function BTM_BleSetPhy
902 *
903 * Description To set PHY preferences for specified LE connection
904 *
905 *
906 * Returns BTM_SUCCESS if command successfully sent to controller,
907 * BTM_MODE_UNSUPPORTED if local controller doesn't support LE
908 * 2M or LE Coded PHY,
909 * BTM_ILLEGAL_VALUE if specified remote doesn't support LE 2M
910 * or LE Coded PHY,
911 * BTM_WRONG_MODE if Device in wrong mode for request.
912 *
913 ******************************************************************************/
BTM_BleSetPhy(const RawAddress & bd_addr,uint8_t tx_phys,uint8_t rx_phys,uint16_t phy_options)914 void BTM_BleSetPhy(const RawAddress& bd_addr, uint8_t tx_phys, uint8_t rx_phys,
915 uint16_t phy_options) {
916 if (bluetooth::shim::is_gd_shim_enabled()) {
917 return bluetooth::shim::BTM_BleSetPhy(bd_addr, tx_phys, rx_phys,
918 phy_options);
919 }
920 tACL_CONN* p_acl = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
921
922 if (p_acl == NULL) {
923 BTM_TRACE_ERROR("%s: Wrong mode: no LE link exist or LE not supported",
924 __func__);
925 return;
926 }
927
928 uint8_t all_phys = 0;
929 if (tx_phys == 0) all_phys &= 0x01;
930 if (rx_phys == 0) all_phys &= 0x02;
931
932 BTM_TRACE_DEBUG(
933 "%s: all_phys = 0x%02x, tx_phys = 0x%02x, rx_phys = 0x%02x, phy_options "
934 "= 0x%04x",
935 __func__, all_phys, tx_phys, rx_phys, phy_options);
936
937 uint16_t handle = p_acl->hci_handle;
938
939 // checking if local controller supports it!
940 if (!controller_get_interface()->supports_ble_2m_phy() &&
941 !controller_get_interface()->supports_ble_coded_phy()) {
942 BTM_TRACE_ERROR("%s failed, request not supported in local controller!",
943 __func__);
944 gatt_notify_phy_updated(GATT_REQ_NOT_SUPPORTED, handle, tx_phys, rx_phys);
945 return;
946 }
947
948 if (!HCI_LE_2M_PHY_SUPPORTED(p_acl->peer_le_features) &&
949 !HCI_LE_CODED_PHY_SUPPORTED(p_acl->peer_le_features)) {
950 BTM_TRACE_ERROR("%s failed, peer does not support request", __func__);
951 gatt_notify_phy_updated(GATT_REQ_NOT_SUPPORTED, handle, tx_phys, rx_phys);
952 return;
953 }
954
955 const uint8_t len = HCIC_PARAM_SIZE_BLE_SET_PHY;
956 uint8_t data[len];
957 uint8_t* pp = data;
958 UINT16_TO_STREAM(pp, handle);
959 UINT8_TO_STREAM(pp, all_phys);
960 UINT8_TO_STREAM(pp, tx_phys);
961 UINT8_TO_STREAM(pp, rx_phys);
962 UINT16_TO_STREAM(pp, phy_options);
963 btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_SET_PHY, data, len,
964 base::Bind(doNothing));
965 }
966
967 /*******************************************************************************
968 *
969 * Function btm_ble_determine_security_act
970 *
971 * Description This function checks the security of current LE link
972 * and returns the appropriate action that needs to be
973 * taken to achieve the required security.
974 *
975 * Parameter is_originator - True if outgoing connection
976 * bdaddr: remote device address
977 * security_required: Security required for the service.
978 *
979 * Returns The appropriate security action required.
980 *
981 ******************************************************************************/
btm_ble_determine_security_act(bool is_originator,const RawAddress & bdaddr,uint16_t security_required)982 tBTM_SEC_ACTION btm_ble_determine_security_act(bool is_originator,
983 const RawAddress& bdaddr,
984 uint16_t security_required) {
985 tBTM_LE_AUTH_REQ auth_req = 0x00;
986
987 if (is_originator) {
988 if ((security_required & BTM_SEC_OUT_FLAGS) == 0 &&
989 (security_required & BTM_SEC_OUT_MITM) == 0) {
990 BTM_TRACE_DEBUG("%s No security required for outgoing connection",
991 __func__);
992 return BTM_SEC_OK;
993 }
994
995 if (security_required & BTM_SEC_OUT_MITM) auth_req |= BTM_LE_AUTH_REQ_MITM;
996 } else {
997 if ((security_required & BTM_SEC_IN_FLAGS) == 0 &&
998 (security_required & BTM_SEC_IN_MITM) == 0) {
999 BTM_TRACE_DEBUG("%s No security required for incoming connection",
1000 __func__);
1001 return BTM_SEC_OK;
1002 }
1003
1004 if (security_required & BTM_SEC_IN_MITM) auth_req |= BTM_LE_AUTH_REQ_MITM;
1005 }
1006
1007 tBTM_BLE_SEC_REQ_ACT ble_sec_act;
1008 btm_ble_link_sec_check(bdaddr, auth_req, &ble_sec_act);
1009
1010 BTM_TRACE_DEBUG("%s ble_sec_act %d", __func__, ble_sec_act);
1011
1012 if (ble_sec_act == BTM_BLE_SEC_REQ_ACT_DISCARD) return BTM_SEC_ENC_PENDING;
1013
1014 if (ble_sec_act == BTM_BLE_SEC_REQ_ACT_NONE) return BTM_SEC_OK;
1015
1016 uint8_t sec_flag = 0;
1017 BTM_GetSecurityFlagsByTransport(bdaddr, &sec_flag, BT_TRANSPORT_LE);
1018
1019 bool is_link_encrypted = false;
1020 bool is_key_mitm = false;
1021 if (sec_flag & (BTM_SEC_FLAG_ENCRYPTED | BTM_SEC_FLAG_LKEY_KNOWN)) {
1022 if (sec_flag & BTM_SEC_FLAG_ENCRYPTED) is_link_encrypted = true;
1023
1024 if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) is_key_mitm = true;
1025 }
1026
1027 if (auth_req & BTM_LE_AUTH_REQ_MITM) {
1028 if (!is_key_mitm) {
1029 return BTM_SEC_ENCRYPT_MITM;
1030 } else {
1031 if (is_link_encrypted)
1032 return BTM_SEC_OK;
1033 else
1034 return BTM_SEC_ENCRYPT;
1035 }
1036 } else {
1037 if (is_link_encrypted)
1038 return BTM_SEC_OK;
1039 else
1040 return BTM_SEC_ENCRYPT_NO_MITM;
1041 }
1042
1043 return BTM_SEC_OK;
1044 }
1045
1046 /*******************************************************************************
1047 *
1048 * Function btm_ble_start_sec_check
1049 *
1050 * Description This function is to check and set the security required for
1051 * LE link for LE COC.
1052 *
1053 * Parameter bdaddr: remote device address.
1054 * psm : PSM of the LE COC sevice.
1055 * is_originator: true if outgoing connection.
1056 * p_callback : Pointer to the callback function.
1057 * p_ref_data : Pointer to be returned along with the callback.
1058 *
1059 * Returns Returns - L2CAP LE Connection Response Result Code.
1060 *
1061 ******************************************************************************/
btm_ble_start_sec_check(const RawAddress & bd_addr,uint16_t psm,bool is_originator,tBTM_SEC_CALLBACK * p_callback,void * p_ref_data)1062 tL2CAP_LE_RESULT_CODE btm_ble_start_sec_check(const RawAddress& bd_addr,
1063 uint16_t psm, bool is_originator,
1064 tBTM_SEC_CALLBACK* p_callback,
1065 void* p_ref_data) {
1066 /* Find the service record for the PSM */
1067 tBTM_SEC_SERV_REC* p_serv_rec = btm_sec_find_first_serv(is_originator, psm);
1068
1069 /* If there is no application registered with this PSM do not allow connection
1070 */
1071 if (!p_serv_rec) {
1072 BTM_TRACE_WARNING("%s PSM: %d no application registerd", __func__, psm);
1073 (*p_callback)(&bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_MODE_UNSUPPORTED);
1074 return L2CAP_LE_RESULT_NO_PSM;
1075 }
1076 uint8_t sec_flag = 0;
1077 BTM_GetSecurityFlagsByTransport(bd_addr, &sec_flag, BT_TRANSPORT_LE);
1078
1079 if (!is_originator) {
1080 if ((p_serv_rec->security_flags & BTM_SEC_IN_ENCRYPT) &&
1081 !(sec_flag & BTM_SEC_ENCRYPTED)) {
1082 BTM_TRACE_ERROR(
1083 "%s: L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP. service "
1084 "security_flags=0x%x, "
1085 "sec_flag=0x%x",
1086 __func__, p_serv_rec->security_flags, sec_flag);
1087 return L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP;
1088 } else if ((p_serv_rec->security_flags & BTM_SEC_IN_AUTHENTICATE) &&
1089 !(sec_flag &
1090 (BTM_SEC_LINK_KEY_AUTHED | BTM_SEC_AUTHENTICATED))) {
1091 BTM_TRACE_ERROR(
1092 "%s: L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION. service "
1093 "security_flags=0x%x, "
1094 "sec_flag=0x%x",
1095 __func__, p_serv_rec->security_flags, sec_flag);
1096 return L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION;
1097 }
1098 /* TODO: When security is required, then must check that the key size of our
1099 service is equal or smaller than the incoming connection key size. */
1100 }
1101
1102 tBTM_SEC_ACTION sec_act = btm_ble_determine_security_act(
1103 is_originator, bd_addr, p_serv_rec->security_flags);
1104
1105 tBTM_BLE_SEC_ACT ble_sec_act = BTM_BLE_SEC_NONE;
1106 tL2CAP_LE_RESULT_CODE result = L2CAP_LE_RESULT_CONN_OK;
1107
1108 switch (sec_act) {
1109 case BTM_SEC_OK:
1110 BTM_TRACE_DEBUG("%s Security met", __func__);
1111 p_callback(&bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_SUCCESS);
1112 result = L2CAP_LE_RESULT_CONN_OK;
1113 break;
1114
1115 case BTM_SEC_ENCRYPT:
1116 BTM_TRACE_DEBUG("%s Encryption needs to be done", __func__);
1117 ble_sec_act = BTM_BLE_SEC_ENCRYPT;
1118 break;
1119
1120 case BTM_SEC_ENCRYPT_MITM:
1121 BTM_TRACE_DEBUG("%s Pairing with MITM needs to be done", __func__);
1122 ble_sec_act = BTM_BLE_SEC_ENCRYPT_MITM;
1123 break;
1124
1125 case BTM_SEC_ENCRYPT_NO_MITM:
1126 BTM_TRACE_DEBUG("%s Pairing with No MITM needs to be done", __func__);
1127 ble_sec_act = BTM_BLE_SEC_ENCRYPT_NO_MITM;
1128 break;
1129
1130 case BTM_SEC_ENC_PENDING:
1131 BTM_TRACE_DEBUG("%s Ecryption pending", __func__);
1132 break;
1133 }
1134
1135 if (ble_sec_act == BTM_BLE_SEC_NONE) return result;
1136
1137 tL2C_LCB* p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1138 p_lcb->sec_act = sec_act;
1139 BTM_SetEncryption(bd_addr, BT_TRANSPORT_LE, p_callback, p_ref_data,
1140 ble_sec_act);
1141
1142 return L2CAP_LE_RESULT_CONN_OK;
1143 }
1144
1145 /*******************************************************************************
1146 *
1147 * Function btm_ble_rand_enc_complete
1148 *
1149 * Description This function is the callback functions for HCI_Rand command
1150 * and HCI_Encrypt command is completed.
1151 * This message is received from the HCI.
1152 *
1153 * Returns void
1154 *
1155 ******************************************************************************/
btm_ble_rand_enc_complete(uint8_t * p,uint16_t op_code,tBTM_RAND_ENC_CB * p_enc_cplt_cback)1156 void btm_ble_rand_enc_complete(uint8_t* p, uint16_t op_code,
1157 tBTM_RAND_ENC_CB* p_enc_cplt_cback) {
1158 tBTM_RAND_ENC params;
1159 uint8_t* p_dest = params.param_buf;
1160
1161 BTM_TRACE_DEBUG("btm_ble_rand_enc_complete");
1162
1163 memset(¶ms, 0, sizeof(tBTM_RAND_ENC));
1164
1165 /* If there was a callback address for vcs complete, call it */
1166 if (p_enc_cplt_cback && p) {
1167 /* Pass paramters to the callback function */
1168 STREAM_TO_UINT8(params.status, p); /* command status */
1169
1170 if (params.status == HCI_SUCCESS) {
1171 params.opcode = op_code;
1172
1173 if (op_code == HCI_BLE_RAND)
1174 params.param_len = BT_OCTET8_LEN;
1175 else
1176 params.param_len = OCTET16_LEN;
1177
1178 /* Fetch return info from HCI event message */
1179 memcpy(p_dest, p, params.param_len);
1180 }
1181 if (p_enc_cplt_cback) /* Call the Encryption complete callback function */
1182 (*p_enc_cplt_cback)(¶ms);
1183 }
1184 }
1185
1186 /*******************************************************************************
1187 *
1188 * Function btm_ble_get_enc_key_type
1189 *
1190 * Description This function is to increment local sign counter
1191 * Returns None
1192 *
1193 ******************************************************************************/
btm_ble_increment_sign_ctr(const RawAddress & bd_addr,bool is_local)1194 void btm_ble_increment_sign_ctr(const RawAddress& bd_addr, bool is_local) {
1195 tBTM_SEC_DEV_REC* p_dev_rec;
1196
1197 BTM_TRACE_DEBUG("btm_ble_increment_sign_ctr is_local=%d", is_local);
1198
1199 p_dev_rec = btm_find_dev(bd_addr);
1200 if (p_dev_rec != NULL) {
1201 if (is_local)
1202 p_dev_rec->ble.keys.local_counter++;
1203 else
1204 p_dev_rec->ble.keys.counter++;
1205 BTM_TRACE_DEBUG("is_local=%d local sign counter=%d peer sign counter=%d",
1206 is_local, p_dev_rec->ble.keys.local_counter,
1207 p_dev_rec->ble.keys.counter);
1208 }
1209 }
1210
1211 /*******************************************************************************
1212 *
1213 * Function btm_ble_get_enc_key_type
1214 *
1215 * Description This function is to get the BLE key type that has been
1216 * exchanged betweem the local device and the peer device.
1217 *
1218 * Returns p_key_type: output parameter to carry the key type value.
1219 *
1220 ******************************************************************************/
btm_ble_get_enc_key_type(const RawAddress & bd_addr,uint8_t * p_key_types)1221 bool btm_ble_get_enc_key_type(const RawAddress& bd_addr, uint8_t* p_key_types) {
1222 tBTM_SEC_DEV_REC* p_dev_rec;
1223
1224 BTM_TRACE_DEBUG("btm_ble_get_enc_key_type");
1225
1226 p_dev_rec = btm_find_dev(bd_addr);
1227 if (p_dev_rec != NULL) {
1228 *p_key_types = p_dev_rec->ble.key_type;
1229 return true;
1230 }
1231 return false;
1232 }
1233
1234 /*******************************************************************************
1235 *
1236 * Function btm_get_local_div
1237 *
1238 * Description This function is called to read the local DIV
1239 *
1240 * Returns TURE - if a valid DIV is availavle
1241 ******************************************************************************/
btm_get_local_div(const RawAddress & bd_addr,uint16_t * p_div)1242 bool btm_get_local_div(const RawAddress& bd_addr, uint16_t* p_div) {
1243 tBTM_SEC_DEV_REC* p_dev_rec;
1244 bool status = false;
1245 VLOG(1) << __func__ << " bd_addr: " << bd_addr;
1246
1247 *p_div = 0;
1248 p_dev_rec = btm_find_dev(bd_addr);
1249
1250 if (p_dev_rec && p_dev_rec->ble.keys.div) {
1251 status = true;
1252 *p_div = p_dev_rec->ble.keys.div;
1253 }
1254 BTM_TRACE_DEBUG("btm_get_local_div status=%d (1-OK) DIV=0x%x", status,
1255 *p_div);
1256 return status;
1257 }
1258
1259 /*******************************************************************************
1260 *
1261 * Function btm_sec_save_le_key
1262 *
1263 * Description This function is called by the SMP to update
1264 * an BLE key. SMP is internal, whereas all the keys shall
1265 * be sent to the application. The function is also called
1266 * when application passes ble key stored in NVRAM to the
1267 * btm_sec.
1268 * pass_to_application parameter is false in this case.
1269 *
1270 * Returns void
1271 *
1272 ******************************************************************************/
btm_sec_save_le_key(const RawAddress & bd_addr,tBTM_LE_KEY_TYPE key_type,tBTM_LE_KEY_VALUE * p_keys,bool pass_to_application)1273 void btm_sec_save_le_key(const RawAddress& bd_addr, tBTM_LE_KEY_TYPE key_type,
1274 tBTM_LE_KEY_VALUE* p_keys, bool pass_to_application) {
1275 tBTM_SEC_DEV_REC* p_rec;
1276 tBTM_LE_EVT_DATA cb_data;
1277
1278 BTM_TRACE_DEBUG("btm_sec_save_le_key key_type=0x%x pass_to_application=%d",
1279 key_type, pass_to_application);
1280 /* Store the updated key in the device database */
1281
1282 VLOG(1) << "bd_addr:" << bd_addr;
1283
1284 if ((p_rec = btm_find_dev(bd_addr)) != NULL &&
1285 (p_keys || key_type == BTM_LE_KEY_LID)) {
1286 btm_ble_init_pseudo_addr(p_rec, bd_addr);
1287
1288 switch (key_type) {
1289 case BTM_LE_KEY_PENC:
1290 p_rec->ble.keys.pltk = p_keys->penc_key.ltk;
1291 memcpy(p_rec->ble.keys.rand, p_keys->penc_key.rand, BT_OCTET8_LEN);
1292 p_rec->ble.keys.sec_level = p_keys->penc_key.sec_level;
1293 p_rec->ble.keys.ediv = p_keys->penc_key.ediv;
1294 p_rec->ble.keys.key_size = p_keys->penc_key.key_size;
1295 p_rec->ble.key_type |= BTM_LE_KEY_PENC;
1296 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
1297 if (p_keys->penc_key.sec_level == SMP_SEC_AUTHENTICATED)
1298 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
1299 else
1300 p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
1301 BTM_TRACE_DEBUG(
1302 "BTM_LE_KEY_PENC key_type=0x%x sec_flags=0x%x sec_leve=0x%x",
1303 p_rec->ble.key_type, p_rec->sec_flags, p_rec->ble.keys.sec_level);
1304 break;
1305
1306 case BTM_LE_KEY_PID:
1307 p_rec->ble.keys.irk = p_keys->pid_key.irk;
1308 p_rec->ble.identity_addr = p_keys->pid_key.identity_addr;
1309 p_rec->ble.identity_addr_type = p_keys->pid_key.identity_addr_type;
1310 p_rec->ble.key_type |= BTM_LE_KEY_PID;
1311 BTM_TRACE_DEBUG(
1312 "%s: BTM_LE_KEY_PID key_type=0x%x save peer IRK, change bd_addr=%s "
1313 "to id_addr=%s id_addr_type=0x%x",
1314 __func__, p_rec->ble.key_type, p_rec->bd_addr.ToString().c_str(),
1315 p_keys->pid_key.identity_addr.ToString().c_str(),
1316 p_keys->pid_key.identity_addr_type);
1317 /* update device record address as identity address */
1318 p_rec->bd_addr = p_keys->pid_key.identity_addr;
1319 /* combine DUMO device security record if needed */
1320 btm_consolidate_dev(p_rec);
1321 break;
1322
1323 case BTM_LE_KEY_PCSRK:
1324 p_rec->ble.keys.pcsrk = p_keys->pcsrk_key.csrk;
1325 p_rec->ble.keys.srk_sec_level = p_keys->pcsrk_key.sec_level;
1326 p_rec->ble.keys.counter = p_keys->pcsrk_key.counter;
1327 p_rec->ble.key_type |= BTM_LE_KEY_PCSRK;
1328 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
1329 if (p_keys->pcsrk_key.sec_level == SMP_SEC_AUTHENTICATED)
1330 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
1331 else
1332 p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
1333
1334 BTM_TRACE_DEBUG(
1335 "BTM_LE_KEY_PCSRK key_type=0x%x sec_flags=0x%x sec_level=0x%x "
1336 "peer_counter=%d",
1337 p_rec->ble.key_type, p_rec->sec_flags,
1338 p_rec->ble.keys.srk_sec_level, p_rec->ble.keys.counter);
1339 break;
1340
1341 case BTM_LE_KEY_LENC:
1342 p_rec->ble.keys.lltk = p_keys->lenc_key.ltk;
1343 p_rec->ble.keys.div = p_keys->lenc_key.div; /* update DIV */
1344 p_rec->ble.keys.sec_level = p_keys->lenc_key.sec_level;
1345 p_rec->ble.keys.key_size = p_keys->lenc_key.key_size;
1346 p_rec->ble.key_type |= BTM_LE_KEY_LENC;
1347
1348 BTM_TRACE_DEBUG(
1349 "BTM_LE_KEY_LENC key_type=0x%x DIV=0x%x key_size=0x%x "
1350 "sec_level=0x%x",
1351 p_rec->ble.key_type, p_rec->ble.keys.div, p_rec->ble.keys.key_size,
1352 p_rec->ble.keys.sec_level);
1353 break;
1354
1355 case BTM_LE_KEY_LCSRK: /* local CSRK has been delivered */
1356 p_rec->ble.keys.lcsrk = p_keys->lcsrk_key.csrk;
1357 p_rec->ble.keys.div = p_keys->lcsrk_key.div; /* update DIV */
1358 p_rec->ble.keys.local_csrk_sec_level = p_keys->lcsrk_key.sec_level;
1359 p_rec->ble.keys.local_counter = p_keys->lcsrk_key.counter;
1360 p_rec->ble.key_type |= BTM_LE_KEY_LCSRK;
1361 BTM_TRACE_DEBUG(
1362 "BTM_LE_KEY_LCSRK key_type=0x%x DIV=0x%x scrk_sec_level=0x%x "
1363 "local_counter=%d",
1364 p_rec->ble.key_type, p_rec->ble.keys.div,
1365 p_rec->ble.keys.local_csrk_sec_level,
1366 p_rec->ble.keys.local_counter);
1367 break;
1368
1369 case BTM_LE_KEY_LID:
1370 p_rec->ble.key_type |= BTM_LE_KEY_LID;
1371 break;
1372 default:
1373 BTM_TRACE_WARNING("btm_sec_save_le_key (Bad key_type 0x%02x)",
1374 key_type);
1375 return;
1376 }
1377
1378 VLOG(1) << "BLE key type 0x" << loghex(key_type)
1379 << " updated for BDA: " << bd_addr << " (btm_sec_save_le_key)";
1380
1381 /* Notify the application that one of the BLE keys has been updated
1382 If link key is in progress, it will get sent later.*/
1383 if (pass_to_application && btm_cb.api.p_le_callback) {
1384 cb_data.key.p_key_value = p_keys;
1385 cb_data.key.key_type = key_type;
1386
1387 (*btm_cb.api.p_le_callback)(BTM_LE_KEY_EVT, bd_addr, &cb_data);
1388 }
1389 return;
1390 }
1391
1392 LOG(WARNING) << "BLE key type 0x" << loghex(key_type)
1393 << " called for Unknown BDA or type: " << bd_addr
1394 << "(btm_sec_save_le_key)";
1395
1396 if (p_rec) {
1397 BTM_TRACE_DEBUG("sec_flags=0x%x", p_rec->sec_flags);
1398 }
1399 }
1400
1401 /*******************************************************************************
1402 *
1403 * Function btm_ble_update_sec_key_size
1404 *
1405 * Description update the current lin kencryption key size
1406 *
1407 * Returns void
1408 *
1409 ******************************************************************************/
btm_ble_update_sec_key_size(const RawAddress & bd_addr,uint8_t enc_key_size)1410 void btm_ble_update_sec_key_size(const RawAddress& bd_addr,
1411 uint8_t enc_key_size) {
1412 tBTM_SEC_DEV_REC* p_rec;
1413
1414 BTM_TRACE_DEBUG("btm_ble_update_sec_key_size enc_key_size = %d",
1415 enc_key_size);
1416
1417 p_rec = btm_find_dev(bd_addr);
1418 if (p_rec != NULL) {
1419 p_rec->enc_key_size = enc_key_size;
1420 }
1421 }
1422
1423 /*******************************************************************************
1424 *
1425 * Function btm_ble_read_sec_key_size
1426 *
1427 * Description update the current lin kencryption key size
1428 *
1429 * Returns void
1430 *
1431 ******************************************************************************/
btm_ble_read_sec_key_size(const RawAddress & bd_addr)1432 uint8_t btm_ble_read_sec_key_size(const RawAddress& bd_addr) {
1433 tBTM_SEC_DEV_REC* p_rec;
1434
1435 p_rec = btm_find_dev(bd_addr);
1436 if (p_rec != NULL) {
1437 return p_rec->enc_key_size;
1438 } else
1439 return 0;
1440 }
1441
1442 /*******************************************************************************
1443 *
1444 * Function btm_ble_link_sec_check
1445 *
1446 * Description Check BLE link security level match.
1447 *
1448 * Returns true: check is OK and the *p_sec_req_act contain the action
1449 *
1450 ******************************************************************************/
btm_ble_link_sec_check(const RawAddress & bd_addr,tBTM_LE_AUTH_REQ auth_req,tBTM_BLE_SEC_REQ_ACT * p_sec_req_act)1451 void btm_ble_link_sec_check(const RawAddress& bd_addr,
1452 tBTM_LE_AUTH_REQ auth_req,
1453 tBTM_BLE_SEC_REQ_ACT* p_sec_req_act) {
1454 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
1455 uint8_t req_sec_level = BTM_LE_SEC_NONE, cur_sec_level = BTM_LE_SEC_NONE;
1456
1457 BTM_TRACE_DEBUG("btm_ble_link_sec_check auth_req =0x%x", auth_req);
1458
1459 if (p_dev_rec == NULL) {
1460 BTM_TRACE_ERROR("btm_ble_link_sec_check received for unknown device");
1461 return;
1462 }
1463
1464 if (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
1465 p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING) {
1466 /* race condition: discard the security request while master is encrypting
1467 * the link */
1468 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
1469 } else {
1470 req_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
1471 if (auth_req & BTM_LE_AUTH_REQ_MITM) {
1472 req_sec_level = BTM_LE_SEC_AUTHENTICATED;
1473 }
1474
1475 BTM_TRACE_DEBUG("dev_rec sec_flags=0x%x", p_dev_rec->sec_flags);
1476
1477 /* currently encrpted */
1478 if (p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED) {
1479 if (p_dev_rec->sec_flags & BTM_SEC_LE_AUTHENTICATED)
1480 cur_sec_level = BTM_LE_SEC_AUTHENTICATED;
1481 else
1482 cur_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
1483 } else /* unencrypted link */
1484 {
1485 /* if bonded, get the key security level */
1486 if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC)
1487 cur_sec_level = p_dev_rec->ble.keys.sec_level;
1488 else
1489 cur_sec_level = BTM_LE_SEC_NONE;
1490 }
1491
1492 if (cur_sec_level >= req_sec_level) {
1493 /* To avoid re-encryption on an encrypted link for an equal condition
1494 * encryption */
1495 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_ENCRYPT;
1496 } else {
1497 /* start the pariring process to upgrade the keys*/
1498 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_PAIR;
1499 }
1500 }
1501
1502 BTM_TRACE_DEBUG("cur_sec_level=%d req_sec_level=%d sec_req_act=%d",
1503 cur_sec_level, req_sec_level, *p_sec_req_act);
1504 }
1505
1506 /*******************************************************************************
1507 *
1508 * Function btm_ble_set_encryption
1509 *
1510 * Description This function is called to ensure that LE connection is
1511 * encrypted. Should be called only on an open connection.
1512 * Typically only needed for connections that first want to
1513 * bring up unencrypted links, then later encrypt them.
1514 *
1515 * Returns void
1516 * the local device ER is copied into er
1517 *
1518 ******************************************************************************/
btm_ble_set_encryption(const RawAddress & bd_addr,tBTM_BLE_SEC_ACT sec_act,uint8_t link_role)1519 tBTM_STATUS btm_ble_set_encryption(const RawAddress& bd_addr,
1520 tBTM_BLE_SEC_ACT sec_act,
1521 uint8_t link_role) {
1522 tBTM_STATUS cmd = BTM_NO_RESOURCES;
1523 tBTM_SEC_DEV_REC* p_rec = btm_find_dev(bd_addr);
1524 tBTM_BLE_SEC_REQ_ACT sec_req_act;
1525 tBTM_LE_AUTH_REQ auth_req;
1526
1527 if (p_rec == NULL) {
1528 BTM_TRACE_WARNING(
1529 "btm_ble_set_encryption (NULL device record!! sec_act=0x%x", sec_act);
1530 return (BTM_WRONG_MODE);
1531 }
1532
1533 BTM_TRACE_DEBUG("btm_ble_set_encryption sec_act=0x%x role_master=%d", sec_act,
1534 p_rec->role_master);
1535
1536 if (sec_act == BTM_BLE_SEC_ENCRYPT_MITM) {
1537 p_rec->security_required |= BTM_SEC_IN_MITM;
1538 }
1539
1540 switch (sec_act) {
1541 case BTM_BLE_SEC_ENCRYPT:
1542 if (link_role == BTM_ROLE_MASTER) {
1543 /* start link layer encryption using the security info stored */
1544 cmd = btm_ble_start_encrypt(bd_addr, false, NULL);
1545 break;
1546 }
1547 /* if salve role then fall through to call SMP_Pair below which will send a
1548 sec_request to request the master to encrypt the link */
1549 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1550 case BTM_BLE_SEC_ENCRYPT_NO_MITM:
1551 case BTM_BLE_SEC_ENCRYPT_MITM:
1552 auth_req = (sec_act == BTM_BLE_SEC_ENCRYPT_NO_MITM)
1553 ? SMP_AUTH_BOND
1554 : (SMP_AUTH_BOND | SMP_AUTH_YN_BIT);
1555 btm_ble_link_sec_check(bd_addr, auth_req, &sec_req_act);
1556 if (sec_req_act == BTM_BLE_SEC_REQ_ACT_NONE ||
1557 sec_req_act == BTM_BLE_SEC_REQ_ACT_DISCARD) {
1558 BTM_TRACE_DEBUG("%s, no action needed. Ignore", __func__);
1559 cmd = BTM_SUCCESS;
1560 break;
1561 }
1562 if (link_role == BTM_ROLE_MASTER) {
1563 if (sec_req_act == BTM_BLE_SEC_REQ_ACT_ENCRYPT) {
1564 cmd = btm_ble_start_encrypt(bd_addr, false, NULL);
1565 break;
1566 }
1567 }
1568
1569 if (SMP_Pair(bd_addr) == SMP_STARTED) {
1570 cmd = BTM_CMD_STARTED;
1571 p_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
1572 }
1573 break;
1574
1575 default:
1576 cmd = BTM_WRONG_MODE;
1577 break;
1578 }
1579 return cmd;
1580 }
1581
1582 /*******************************************************************************
1583 *
1584 * Function btm_ble_ltk_request
1585 *
1586 * Description This function is called when encryption request is received
1587 * on a slave device.
1588 *
1589 *
1590 * Returns void
1591 *
1592 ******************************************************************************/
btm_ble_ltk_request(uint16_t handle,uint8_t rand[8],uint16_t ediv)1593 void btm_ble_ltk_request(uint16_t handle, uint8_t rand[8], uint16_t ediv) {
1594 tBTM_CB* p_cb = &btm_cb;
1595 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
1596
1597 BTM_TRACE_DEBUG("btm_ble_ltk_request");
1598
1599 p_cb->ediv = ediv;
1600
1601 memcpy(p_cb->enc_rand, rand, BT_OCTET8_LEN);
1602
1603 if (p_dev_rec != NULL) {
1604 if (!smp_proc_ltk_request(p_dev_rec->bd_addr)) {
1605 btm_ble_ltk_request_reply(p_dev_rec->bd_addr, false, Octet16{0});
1606 }
1607 }
1608 }
1609
1610 /** This function is called to start LE encryption.
1611 * Returns BTM_SUCCESS if encryption was started successfully
1612 */
btm_ble_start_encrypt(const RawAddress & bda,bool use_stk,Octet16 * p_stk)1613 tBTM_STATUS btm_ble_start_encrypt(const RawAddress& bda, bool use_stk,
1614 Octet16* p_stk) {
1615 tBTM_CB* p_cb = &btm_cb;
1616 tBTM_SEC_DEV_REC* p_rec = btm_find_dev(bda);
1617 BT_OCTET8 dummy_rand = {0};
1618
1619 BTM_TRACE_DEBUG("btm_ble_start_encrypt");
1620
1621 if (!p_rec) {
1622 BTM_TRACE_ERROR("Link is not active, can not encrypt!");
1623 return BTM_WRONG_MODE;
1624 }
1625
1626 if (p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
1627 BTM_TRACE_WARNING("Link Encryption is active, Busy!");
1628 return BTM_BUSY;
1629 }
1630
1631 p_cb->enc_handle = p_rec->ble_hci_handle;
1632
1633 if (use_stk) {
1634 btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, dummy_rand, 0, *p_stk);
1635 } else if (p_rec->ble.key_type & BTM_LE_KEY_PENC) {
1636 btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, p_rec->ble.keys.rand,
1637 p_rec->ble.keys.ediv, p_rec->ble.keys.pltk);
1638 } else {
1639 BTM_TRACE_ERROR("No key available to encrypt the link");
1640 return BTM_NO_RESOURCES;
1641 }
1642
1643 if (p_rec->sec_state == BTM_SEC_STATE_IDLE)
1644 p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
1645
1646 return BTM_CMD_STARTED;
1647 }
1648
1649 /*******************************************************************************
1650 *
1651 * Function btm_ble_link_encrypted
1652 *
1653 * Description This function is called when LE link encrption status is
1654 * changed.
1655 *
1656 * Returns void
1657 *
1658 ******************************************************************************/
btm_ble_link_encrypted(const RawAddress & bd_addr,uint8_t encr_enable)1659 void btm_ble_link_encrypted(const RawAddress& bd_addr, uint8_t encr_enable) {
1660 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
1661 bool enc_cback;
1662
1663 if (!p_dev_rec) {
1664 BTM_TRACE_WARNING(
1665 "btm_ble_link_encrypted (No Device Found!) encr_enable=%d",
1666 encr_enable);
1667 return;
1668 }
1669
1670 BTM_TRACE_DEBUG("btm_ble_link_encrypted encr_enable=%d", encr_enable);
1671
1672 enc_cback = (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING);
1673
1674 smp_link_encrypted(bd_addr, encr_enable);
1675
1676 BTM_TRACE_DEBUG(" p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
1677
1678 if (encr_enable && p_dev_rec->enc_key_size == 0)
1679 p_dev_rec->enc_key_size = p_dev_rec->ble.keys.key_size;
1680
1681 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
1682 if (p_dev_rec->p_callback && enc_cback) {
1683 if (encr_enable)
1684 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_SUCCESS, true);
1685 else if (p_dev_rec->sec_flags & ~BTM_SEC_LE_LINK_KEY_KNOWN) {
1686 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_FAILED_ON_SECURITY, true);
1687 } else if (p_dev_rec->role_master)
1688 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_ERR_PROCESSING, true);
1689 }
1690 /* to notify GATT to send data if any request is pending */
1691 gatt_notify_enc_cmpl(p_dev_rec->ble.pseudo_addr);
1692 }
1693
1694 /*******************************************************************************
1695 *
1696 * Function btm_ble_ltk_request_reply
1697 *
1698 * Description This function is called to send a LTK request reply on a
1699 * slave
1700 * device.
1701 *
1702 * Returns void
1703 *
1704 ******************************************************************************/
btm_ble_ltk_request_reply(const RawAddress & bda,bool use_stk,const Octet16 & stk)1705 void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk,
1706 const Octet16& stk) {
1707 tBTM_SEC_DEV_REC* p_rec = btm_find_dev(bda);
1708 tBTM_CB* p_cb = &btm_cb;
1709
1710 if (p_rec == NULL) {
1711 BTM_TRACE_ERROR("btm_ble_ltk_request_reply received for unknown device");
1712 return;
1713 }
1714
1715 BTM_TRACE_DEBUG("btm_ble_ltk_request_reply");
1716 p_cb->enc_handle = p_rec->ble_hci_handle;
1717 p_cb->key_size = p_rec->ble.keys.key_size;
1718
1719 BTM_TRACE_ERROR("key size = %d", p_rec->ble.keys.key_size);
1720 if (use_stk) {
1721 btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, stk);
1722 } else /* calculate LTK using peer device */
1723 {
1724 if (p_rec->ble.key_type & BTM_LE_KEY_LENC)
1725 btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, p_rec->ble.keys.lltk);
1726 else
1727 btsnd_hcic_ble_ltk_req_neg_reply(btm_cb.enc_handle);
1728 }
1729 }
1730
1731 /*******************************************************************************
1732 *
1733 * Function btm_ble_io_capabilities_req
1734 *
1735 * Description This function is called to handle SMP get IO capability
1736 * request.
1737 *
1738 * Returns void
1739 *
1740 ******************************************************************************/
btm_ble_io_capabilities_req(tBTM_SEC_DEV_REC * p_dev_rec,tBTM_LE_IO_REQ * p_data)1741 uint8_t btm_ble_io_capabilities_req(tBTM_SEC_DEV_REC* p_dev_rec,
1742 tBTM_LE_IO_REQ* p_data) {
1743 uint8_t callback_rc = BTM_SUCCESS;
1744 BTM_TRACE_DEBUG("btm_ble_io_capabilities_req");
1745 if (btm_cb.api.p_le_callback) {
1746 /* the callback function implementation may change the IO capability... */
1747 callback_rc = (*btm_cb.api.p_le_callback)(
1748 BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr, (tBTM_LE_EVT_DATA*)p_data);
1749 }
1750 if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != p_data->oob_data)) {
1751 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE)
1752 if (btm_cb.devcb.keep_rfu_in_auth_req) {
1753 BTM_TRACE_DEBUG("btm_ble_io_capabilities_req keep_rfu_in_auth_req = %u",
1754 btm_cb.devcb.keep_rfu_in_auth_req);
1755 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK_KEEP_RFU;
1756 btm_cb.devcb.keep_rfu_in_auth_req = false;
1757 } else { /* default */
1758 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
1759 }
1760 #else
1761 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
1762 #endif
1763
1764 BTM_TRACE_DEBUG(
1765 "btm_ble_io_capabilities_req 1: p_dev_rec->security_required = %d "
1766 "auth_req:%d",
1767 p_dev_rec->security_required, p_data->auth_req);
1768 BTM_TRACE_DEBUG(
1769 "btm_ble_io_capabilities_req 2: i_keys=0x%x r_keys=0x%x (bit 0-LTK "
1770 "1-IRK 2-CSRK)",
1771 p_data->init_keys, p_data->resp_keys);
1772
1773 /* if authentication requires MITM protection, put on the mask */
1774 if (p_dev_rec->security_required & BTM_SEC_IN_MITM)
1775 p_data->auth_req |= BTM_LE_AUTH_REQ_MITM;
1776
1777 if (!(p_data->auth_req & SMP_AUTH_BOND)) {
1778 BTM_TRACE_DEBUG("Non bonding: No keys should be exchanged");
1779 p_data->init_keys = 0;
1780 p_data->resp_keys = 0;
1781 }
1782
1783 BTM_TRACE_DEBUG("btm_ble_io_capabilities_req 3: auth_req:%d",
1784 p_data->auth_req);
1785 BTM_TRACE_DEBUG("btm_ble_io_capabilities_req 4: i_keys=0x%x r_keys=0x%x",
1786 p_data->init_keys, p_data->resp_keys);
1787
1788 BTM_TRACE_DEBUG(
1789 "btm_ble_io_capabilities_req 5: p_data->io_cap = %d auth_req:%d",
1790 p_data->io_cap, p_data->auth_req);
1791
1792 /* remove MITM protection requirement if IO cap does not allow it */
1793 if ((p_data->io_cap == BTM_IO_CAP_NONE) && p_data->oob_data == SMP_OOB_NONE)
1794 p_data->auth_req &= ~BTM_LE_AUTH_REQ_MITM;
1795
1796 if (!(p_data->auth_req & SMP_SC_SUPPORT_BIT)) {
1797 /* if Secure Connections are not supported then remove LK derivation,
1798 ** and keypress notifications.
1799 */
1800 BTM_TRACE_DEBUG(
1801 "%s-SC not supported -> No LK derivation, no keypress notifications",
1802 __func__);
1803 p_data->auth_req &= ~SMP_KP_SUPPORT_BIT;
1804 p_data->init_keys &= ~SMP_SEC_KEY_TYPE_LK;
1805 p_data->resp_keys &= ~SMP_SEC_KEY_TYPE_LK;
1806 }
1807
1808 BTM_TRACE_DEBUG(
1809 "btm_ble_io_capabilities_req 6: IO_CAP:%d oob_data:%d auth_req:0x%02x",
1810 p_data->io_cap, p_data->oob_data, p_data->auth_req);
1811 }
1812 return callback_rc;
1813 }
1814
1815 /*******************************************************************************
1816 *
1817 * Function btm_ble_br_keys_req
1818 *
1819 * Description This function is called to handle SMP request for keys sent
1820 * over BR/EDR.
1821 *
1822 * Returns void
1823 *
1824 ******************************************************************************/
btm_ble_br_keys_req(tBTM_SEC_DEV_REC * p_dev_rec,tBTM_LE_IO_REQ * p_data)1825 uint8_t btm_ble_br_keys_req(tBTM_SEC_DEV_REC* p_dev_rec,
1826 tBTM_LE_IO_REQ* p_data) {
1827 uint8_t callback_rc = BTM_SUCCESS;
1828 BTM_TRACE_DEBUG("%s", __func__);
1829 if (btm_cb.api.p_le_callback) {
1830 /* the callback function implementation may change the IO capability... */
1831 callback_rc = (*btm_cb.api.p_le_callback)(
1832 BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr, (tBTM_LE_EVT_DATA*)p_data);
1833 }
1834
1835 return callback_rc;
1836 }
1837
1838 /*******************************************************************************
1839 *
1840 * Function btm_ble_connected
1841 *
1842 * Description This function is when a LE connection to the peer device is
1843 * establsihed
1844 *
1845 * Returns void
1846 *
1847 ******************************************************************************/
btm_ble_connected(const RawAddress & bda,uint16_t handle,uint8_t enc_mode,uint8_t role,tBLE_ADDR_TYPE addr_type,UNUSED_ATTR bool addr_matched)1848 void btm_ble_connected(const RawAddress& bda, uint16_t handle, uint8_t enc_mode,
1849 uint8_t role, tBLE_ADDR_TYPE addr_type,
1850 UNUSED_ATTR bool addr_matched) {
1851 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bda);
1852 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
1853
1854 BTM_TRACE_EVENT("btm_ble_connected");
1855
1856 /* Commenting out trace due to obf/compilation problems.
1857 */
1858 if (p_dev_rec) {
1859 VLOG(1) << __func__ << " Security Manager: handle:" << handle
1860 << " enc_mode:" << loghex(enc_mode) << " bda: " << bda
1861 << " RName: " << p_dev_rec->sec_bd_name
1862 << " p_dev_rec:" << p_dev_rec;
1863
1864 BTM_TRACE_DEBUG("btm_ble_connected sec_flags=0x%x", p_dev_rec->sec_flags);
1865 } else {
1866 VLOG(1) << __func__ << " Security Manager: handle:" << handle
1867 << " enc_mode:" << loghex(enc_mode) << " bda: " << bda
1868 << " p_dev_rec:" << p_dev_rec;
1869 }
1870
1871 if (!p_dev_rec) {
1872 /* There is no device record for new connection. Allocate one */
1873 p_dev_rec = btm_sec_alloc_dev(bda);
1874 if (p_dev_rec == NULL) return;
1875 } else /* Update the timestamp for this device */
1876 {
1877 p_dev_rec->timestamp = btm_cb.dev_rec_count++;
1878 }
1879
1880 /* update device information */
1881 p_dev_rec->device_type |= BT_DEVICE_TYPE_BLE;
1882 p_dev_rec->ble_hci_handle = handle;
1883 p_dev_rec->ble.ble_addr_type = addr_type;
1884 /* update pseudo address */
1885 p_dev_rec->ble.pseudo_addr = bda;
1886
1887 p_dev_rec->role_master = false;
1888 if (role == HCI_ROLE_MASTER) p_dev_rec->role_master = true;
1889
1890 #if (BLE_PRIVACY_SPT == TRUE)
1891 if (!addr_matched) p_dev_rec->ble.active_addr_type = BTM_BLE_ADDR_PSEUDO;
1892
1893 if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM && !addr_matched)
1894 p_dev_rec->ble.cur_rand_addr = bda;
1895 #endif
1896
1897 p_cb->inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
1898
1899 return;
1900 }
1901
1902 /*****************************************************************************
1903 * Function btm_proc_smp_cback
1904 *
1905 * Description This function is the SMP callback handler.
1906 *
1907 *****************************************************************************/
btm_proc_smp_cback(tSMP_EVT event,const RawAddress & bd_addr,tSMP_EVT_DATA * p_data)1908 uint8_t btm_proc_smp_cback(tSMP_EVT event, const RawAddress& bd_addr,
1909 tSMP_EVT_DATA* p_data) {
1910 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
1911 uint8_t res = 0;
1912
1913 BTM_TRACE_DEBUG("btm_proc_smp_cback event = %d", event);
1914
1915 if (p_dev_rec != NULL) {
1916 switch (event) {
1917 case SMP_IO_CAP_REQ_EVT:
1918 btm_ble_io_capabilities_req(p_dev_rec,
1919 (tBTM_LE_IO_REQ*)&p_data->io_req);
1920 break;
1921
1922 case SMP_BR_KEYS_REQ_EVT:
1923 btm_ble_br_keys_req(p_dev_rec, (tBTM_LE_IO_REQ*)&p_data->io_req);
1924 break;
1925
1926 case SMP_PASSKEY_REQ_EVT:
1927 case SMP_PASSKEY_NOTIF_EVT:
1928 case SMP_OOB_REQ_EVT:
1929 case SMP_NC_REQ_EVT:
1930 case SMP_SC_OOB_REQ_EVT:
1931 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
1932 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1933
1934 case SMP_SEC_REQUEST_EVT:
1935 if (event == SMP_SEC_REQUEST_EVT &&
1936 btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
1937 BTM_TRACE_DEBUG("%s: Ignoring SMP Security request", __func__);
1938 break;
1939 }
1940 btm_cb.pairing_bda = bd_addr;
1941 p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
1942 btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
1943 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1944
1945 case SMP_COMPLT_EVT:
1946 if (btm_cb.api.p_le_callback) {
1947 /* the callback function implementation may change the IO
1948 * capability... */
1949 BTM_TRACE_DEBUG("btm_cb.api.p_le_callback=0x%x",
1950 btm_cb.api.p_le_callback);
1951 (*btm_cb.api.p_le_callback)(event, bd_addr,
1952 (tBTM_LE_EVT_DATA*)p_data);
1953 }
1954
1955 if (event == SMP_COMPLT_EVT) {
1956 p_dev_rec = btm_find_dev(bd_addr);
1957 if (p_dev_rec == NULL) {
1958 BTM_TRACE_ERROR("%s: p_dev_rec is NULL", __func__);
1959 android_errorWriteLog(0x534e4554, "120612744");
1960 return 0;
1961 }
1962 BTM_TRACE_DEBUG(
1963 "evt=SMP_COMPLT_EVT before update sec_level=0x%x sec_flags=0x%x",
1964 p_data->cmplt.sec_level, p_dev_rec->sec_flags);
1965
1966 res = (p_data->cmplt.reason == SMP_SUCCESS) ? BTM_SUCCESS
1967 : BTM_ERR_PROCESSING;
1968
1969 BTM_TRACE_DEBUG(
1970 "after update result=%d sec_level=0x%x sec_flags=0x%x", res,
1971 p_data->cmplt.sec_level, p_dev_rec->sec_flags);
1972
1973 if (p_data->cmplt.is_pair_cancel &&
1974 btm_cb.api.p_bond_cancel_cmpl_callback) {
1975 BTM_TRACE_DEBUG("Pairing Cancel completed");
1976 (*btm_cb.api.p_bond_cancel_cmpl_callback)(BTM_SUCCESS);
1977 }
1978 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE)
1979 if (res != BTM_SUCCESS) {
1980 if (!btm_cb.devcb.no_disc_if_pair_fail &&
1981 p_data->cmplt.reason != SMP_CONN_TOUT) {
1982 BTM_TRACE_DEBUG("Pairing failed - prepare to remove ACL");
1983 l2cu_start_post_bond_timer(p_dev_rec->ble_hci_handle);
1984 } else {
1985 BTM_TRACE_DEBUG("Pairing failed - Not Removing ACL");
1986 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
1987 }
1988 }
1989 #else
1990 if (res != BTM_SUCCESS && p_data->cmplt.reason != SMP_CONN_TOUT) {
1991 BTM_TRACE_DEBUG("Pairing failed - prepare to remove ACL");
1992 l2cu_start_post_bond_timer(p_dev_rec->ble_hci_handle);
1993 }
1994 #endif
1995
1996 BTM_TRACE_DEBUG(
1997 "btm_cb pairing_state=%x pairing_flags=%x pin_code_len=%x",
1998 btm_cb.pairing_state, btm_cb.pairing_flags, btm_cb.pin_code_len);
1999 VLOG(1) << "btm_cb.pairing_bda: " << btm_cb.pairing_bda;
2000
2001 /* Reset btm state only if the callback address matches pairing
2002 * address*/
2003 if (bd_addr == btm_cb.pairing_bda) {
2004 btm_cb.pairing_bda = RawAddress::kAny;
2005 btm_cb.pairing_state = BTM_PAIR_STATE_IDLE;
2006 btm_cb.pairing_flags = 0;
2007 }
2008
2009 if (res == BTM_SUCCESS) {
2010 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
2011 #if (BLE_PRIVACY_SPT == TRUE)
2012 /* add all bonded device into resolving list if IRK is available*/
2013 btm_ble_resolving_list_load_dev(p_dev_rec);
2014 #endif
2015 }
2016
2017 btm_sec_dev_rec_cback_event(p_dev_rec, res, true);
2018 }
2019 break;
2020
2021 default:
2022 BTM_TRACE_DEBUG("unknown event = %d", event);
2023 break;
2024 }
2025 } else {
2026 BTM_TRACE_ERROR("btm_proc_smp_cback received for unknown device");
2027 }
2028
2029 return 0;
2030 }
2031
2032 /*******************************************************************************
2033 *
2034 * Function BTM_BleDataSignature
2035 *
2036 * Description This function is called to sign the data using AES128 CMAC
2037 * algorith.
2038 *
2039 * Parameter bd_addr: target device the data to be signed for.
2040 * p_text: singing data
2041 * len: length of the data to be signed.
2042 * signature: output parameter where data signature is going to
2043 * be stored.
2044 *
2045 * Returns true if signing sucessul, otherwise false.
2046 *
2047 ******************************************************************************/
BTM_BleDataSignature(const RawAddress & bd_addr,uint8_t * p_text,uint16_t len,BLE_SIGNATURE signature)2048 bool BTM_BleDataSignature(const RawAddress& bd_addr, uint8_t* p_text,
2049 uint16_t len, BLE_SIGNATURE signature) {
2050 if (bluetooth::shim::is_gd_shim_enabled()) {
2051 return bluetooth::shim::BTM_BleDataSignature(bd_addr, p_text, len,
2052 signature);
2053 }
2054 tBTM_SEC_DEV_REC* p_rec = btm_find_dev(bd_addr);
2055
2056 BTM_TRACE_DEBUG("%s", __func__);
2057 if (p_rec == NULL) {
2058 BTM_TRACE_ERROR("%s-data signing can not be done from unknown device",
2059 __func__);
2060 return false;
2061 }
2062
2063 uint8_t* p_mac = (uint8_t*)signature;
2064 uint8_t* pp;
2065 uint8_t* p_buf = (uint8_t*)osi_malloc(len + 4);
2066
2067 BTM_TRACE_DEBUG("%s-Start to generate Local CSRK", __func__);
2068 pp = p_buf;
2069 /* prepare plain text */
2070 if (p_text) {
2071 memcpy(p_buf, p_text, len);
2072 pp = (p_buf + len);
2073 }
2074
2075 UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
2076 UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
2077
2078 crypto_toolbox::aes_cmac(p_rec->ble.keys.lcsrk, p_buf, (uint16_t)(len + 4),
2079 BTM_CMAC_TLEN_SIZE, p_mac);
2080 btm_ble_increment_sign_ctr(bd_addr, true);
2081
2082 BTM_TRACE_DEBUG("%s p_mac = %d", __func__, p_mac);
2083 BTM_TRACE_DEBUG(
2084 "p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = "
2085 "0x%02x",
2086 *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
2087 BTM_TRACE_DEBUG(
2088 "p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = "
2089 "0x%02x",
2090 *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
2091 osi_free(p_buf);
2092 return true;
2093 }
2094
2095 /*******************************************************************************
2096 *
2097 * Function BTM_BleVerifySignature
2098 *
2099 * Description This function is called to verify the data signature
2100 *
2101 * Parameter bd_addr: target device the data to be signed for.
2102 * p_orig: original data before signature.
2103 * len: length of the signing data
2104 * counter: counter used when doing data signing
2105 * p_comp: signature to be compared against.
2106
2107 * Returns true if signature verified correctly; otherwise false.
2108 *
2109 ******************************************************************************/
BTM_BleVerifySignature(const RawAddress & bd_addr,uint8_t * p_orig,uint16_t len,uint32_t counter,uint8_t * p_comp)2110 bool BTM_BleVerifySignature(const RawAddress& bd_addr, uint8_t* p_orig,
2111 uint16_t len, uint32_t counter, uint8_t* p_comp) {
2112 if (bluetooth::shim::is_gd_shim_enabled()) {
2113 return bluetooth::shim::BTM_BleVerifySignature(bd_addr, p_orig, len,
2114 counter, p_comp);
2115 }
2116 bool verified = false;
2117 tBTM_SEC_DEV_REC* p_rec = btm_find_dev(bd_addr);
2118 uint8_t p_mac[BTM_CMAC_TLEN_SIZE];
2119
2120 if (p_rec == NULL || (p_rec && !(p_rec->ble.key_type & BTM_LE_KEY_PCSRK))) {
2121 BTM_TRACE_ERROR("can not verify signature for unknown device");
2122 } else if (counter < p_rec->ble.keys.counter) {
2123 BTM_TRACE_ERROR("signature received with out dated sign counter");
2124 } else if (p_orig == NULL) {
2125 BTM_TRACE_ERROR("No signature to verify");
2126 } else {
2127 BTM_TRACE_DEBUG("%s rcv_cnt=%d >= expected_cnt=%d", __func__, counter,
2128 p_rec->ble.keys.counter);
2129
2130 crypto_toolbox::aes_cmac(p_rec->ble.keys.pcsrk, p_orig, len,
2131 BTM_CMAC_TLEN_SIZE, p_mac);
2132 if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0) {
2133 btm_ble_increment_sign_ctr(bd_addr, false);
2134 verified = true;
2135 }
2136 }
2137 return verified;
2138 }
2139
2140 /*******************************************************************************
2141 * Utility functions for LE device IR/ER generation
2142 ******************************************************************************/
2143 /** This function is to notify application new keys have been generated. */
btm_notify_new_key(uint8_t key_type)2144 static void btm_notify_new_key(uint8_t key_type) {
2145 tBTM_BLE_LOCAL_KEYS* p_local_keys = NULL;
2146
2147 BTM_TRACE_DEBUG("btm_notify_new_key key_type=%d", key_type);
2148
2149 if (btm_cb.api.p_le_key_callback) {
2150 switch (key_type) {
2151 case BTM_BLE_KEY_TYPE_ID:
2152 BTM_TRACE_DEBUG("BTM_BLE_KEY_TYPE_ID");
2153 p_local_keys = (tBTM_BLE_LOCAL_KEYS*)&btm_cb.devcb.id_keys;
2154 break;
2155
2156 case BTM_BLE_KEY_TYPE_ER:
2157 BTM_TRACE_DEBUG("BTM_BLE_KEY_TYPE_ER");
2158 p_local_keys =
2159 (tBTM_BLE_LOCAL_KEYS*)&btm_cb.devcb.ble_encryption_key_value;
2160 break;
2161
2162 default:
2163 BTM_TRACE_ERROR("unknown key type: %d", key_type);
2164 break;
2165 }
2166 if (p_local_keys != NULL)
2167 (*btm_cb.api.p_le_key_callback)(key_type, p_local_keys);
2168 }
2169 }
2170
2171 /** implementation of btm_ble_reset_id */
btm_ble_reset_id_impl(const Octet16 & rand1,const Octet16 & rand2)2172 static void btm_ble_reset_id_impl(const Octet16& rand1, const Octet16& rand2) {
2173 /* Regenerate Identity Root */
2174 btm_cb.devcb.id_keys.ir = rand1;
2175 uint8_t btm_ble_dhk_pt = 0x03;
2176
2177 /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */
2178 btm_cb.devcb.id_keys.dhk =
2179 crypto_toolbox::aes_128(btm_cb.devcb.id_keys.ir, &btm_ble_dhk_pt, 1);
2180
2181 uint8_t btm_ble_irk_pt = 0x01;
2182 /* IRK = D1(IR, 1) */
2183 btm_cb.devcb.id_keys.irk =
2184 crypto_toolbox::aes_128(btm_cb.devcb.id_keys.ir, &btm_ble_irk_pt, 1);
2185
2186 btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);
2187
2188 #if (BLE_PRIVACY_SPT == TRUE)
2189 /* if privacy is enabled, new RPA should be calculated */
2190 if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
2191 btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
2192 }
2193 #endif
2194
2195 /* proceed generate ER */
2196 btm_cb.devcb.ble_encryption_key_value = rand2;
2197 btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
2198 }
2199
2200 struct reset_id_data {
2201 Octet16 rand1;
2202 Octet16 rand2;
2203 };
2204
2205 /** This function is called to reset LE device identity. */
btm_ble_reset_id(void)2206 void btm_ble_reset_id(void) {
2207 BTM_TRACE_DEBUG("btm_ble_reset_id");
2208
2209 /* In order to reset identity, we need four random numbers. Make four nested
2210 * calls to generate them first, then proceed to perform the actual reset in
2211 * btm_ble_reset_id_impl. */
2212 btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand) {
2213 reset_id_data tmp;
2214 memcpy(tmp.rand1.data(), rand, BT_OCTET8_LEN);
2215 btsnd_hcic_ble_rand(base::Bind(
2216 [](reset_id_data tmp, BT_OCTET8 rand) {
2217 memcpy(tmp.rand1.data() + 8, rand, BT_OCTET8_LEN);
2218 btsnd_hcic_ble_rand(base::Bind(
2219 [](reset_id_data tmp, BT_OCTET8 rand) {
2220 memcpy(tmp.rand2.data(), rand, BT_OCTET8_LEN);
2221 btsnd_hcic_ble_rand(base::Bind(
2222 [](reset_id_data tmp, BT_OCTET8 rand) {
2223 memcpy(tmp.rand2.data() + 8, rand, BT_OCTET8_LEN);
2224 // when all random numbers are ready, do the actual reset.
2225 btm_ble_reset_id_impl(tmp.rand1, tmp.rand2);
2226 },
2227 tmp));
2228 },
2229 tmp));
2230 },
2231 tmp));
2232 }));
2233 }
2234
2235 /* This function set a random address to local controller. It also temporarily
2236 * disable scans and adv before sending the command to the controller. */
btm_ble_set_random_address(const RawAddress & random_bda)2237 void btm_ble_set_random_address(const RawAddress& random_bda) {
2238 tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2239 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
2240 bool adv_mode = btm_cb.ble_ctr_cb.inq_var.adv_mode;
2241
2242 BTM_TRACE_DEBUG("%s", __func__);
2243
2244 if (adv_mode == BTM_BLE_ADV_ENABLE)
2245 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_DISABLE);
2246 if (BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_stop_scan();
2247 btm_ble_suspend_bg_conn();
2248
2249 p_cb->private_addr = random_bda;
2250 btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
2251
2252 if (adv_mode == BTM_BLE_ADV_ENABLE)
2253 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_ENABLE);
2254 if (BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_start_scan();
2255 btm_ble_resume_bg_conn();
2256 }
2257
2258 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
2259 /*******************************************************************************
2260 *
2261 * Function btm_ble_set_no_disc_if_pair_fail
2262 *
2263 * Description This function indicates whether no disconnect of the ACL
2264 * should be used if pairing failed
2265 *
2266 * Returns void
2267 *
2268 ******************************************************************************/
btm_ble_set_no_disc_if_pair_fail(bool disable_disc)2269 void btm_ble_set_no_disc_if_pair_fail(bool disable_disc) {
2270 BTM_TRACE_DEBUG("btm_ble_set_disc_enable_if_pair_fail disable_disc=%d",
2271 disable_disc);
2272 btm_cb.devcb.no_disc_if_pair_fail = disable_disc;
2273 }
2274
2275 /*******************************************************************************
2276 *
2277 * Function btm_ble_set_test_mac_value
2278 *
2279 * Description This function set test MAC value
2280 *
2281 * Returns void
2282 *
2283 ******************************************************************************/
btm_ble_set_test_mac_value(bool enable,uint8_t * p_test_mac_val)2284 void btm_ble_set_test_mac_value(bool enable, uint8_t* p_test_mac_val) {
2285 BTM_TRACE_DEBUG("btm_ble_set_test_mac_value enable=%d", enable);
2286 btm_cb.devcb.enable_test_mac_val = enable;
2287 memcpy(btm_cb.devcb.test_mac, p_test_mac_val, BT_OCTET8_LEN);
2288 }
2289
2290 /*******************************************************************************
2291 *
2292 * Function btm_ble_set_test_local_sign_cntr_value
2293 *
2294 * Description This function set test local sign counter value
2295 *
2296 * Returns void
2297 *
2298 ******************************************************************************/
btm_ble_set_test_local_sign_cntr_value(bool enable,uint32_t test_local_sign_cntr)2299 void btm_ble_set_test_local_sign_cntr_value(bool enable,
2300 uint32_t test_local_sign_cntr) {
2301 BTM_TRACE_DEBUG(
2302 "btm_ble_set_test_local_sign_cntr_value enable=%d local_sign_cntr=%d",
2303 enable, test_local_sign_cntr);
2304 btm_cb.devcb.enable_test_local_sign_cntr = enable;
2305 btm_cb.devcb.test_local_sign_cntr = test_local_sign_cntr;
2306 }
2307
2308 /*******************************************************************************
2309 *
2310 * Function btm_ble_set_keep_rfu_in_auth_req
2311 *
2312 * Description This function indicates if RFU bits have to be kept as is
2313 * (by default they have to be set to 0 by the sender).
2314 *
2315 * Returns void
2316 *
2317 ******************************************************************************/
btm_ble_set_keep_rfu_in_auth_req(bool keep_rfu)2318 void btm_ble_set_keep_rfu_in_auth_req(bool keep_rfu) {
2319 BTM_TRACE_DEBUG("btm_ble_set_keep_rfu_in_auth_req keep_rfus=%d", keep_rfu);
2320 btm_cb.devcb.keep_rfu_in_auth_req = keep_rfu;
2321 }
2322
2323 #endif /* BTM_BLE_CONFORMANCE_TESTING */
2324