1 /******************************************************************************
2 *
3 * Copyright (C) 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 the Bluetooth Device Manager
22 *
23 ******************************************************************************/
24
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "bt_common.h"
31 #include "bt_types.h"
32 #include "btm_api.h"
33 #include "btm_int.h"
34 #include "btu.h"
35 #include "device/include/controller.h"
36 #include "hcidefs.h"
37 #include "hcimsgs.h"
38 #include "l2c_api.h"
39
40 /*******************************************************************************
41 *
42 * Function BTM_SecAddDevice
43 *
44 * Description Add/modify device. This function will be normally called
45 * during host startup to restore all required information
46 * stored in the NVRAM.
47 *
48 * Parameters: bd_addr - BD address of the peer
49 * dev_class - Device Class
50 * bd_name - Name of the peer device. NULL if unknown.
51 * features - Remote device's features (up to 3 pages).
52 * NULL if not known
53 * trusted_mask - Bitwise OR of services that do not
54 * require authorization.
55 * (array of uint32_t)
56 * link_key - Connection link key. NULL if unknown.
57 *
58 * Returns true if added OK, else false
59 *
60 ******************************************************************************/
BTM_SecAddDevice(const RawAddress & bd_addr,DEV_CLASS dev_class,BD_NAME bd_name,uint8_t * features,uint32_t trusted_mask[],LINK_KEY link_key,uint8_t key_type,tBTM_IO_CAP io_cap,uint8_t pin_length)61 bool BTM_SecAddDevice(const RawAddress& bd_addr, DEV_CLASS dev_class,
62 BD_NAME bd_name, uint8_t* features,
63 uint32_t trusted_mask[], LINK_KEY link_key,
64 uint8_t key_type, tBTM_IO_CAP io_cap,
65 uint8_t pin_length) {
66 BTM_TRACE_API("%s: link key type:%x", __func__, key_type);
67
68 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
69 if (!p_dev_rec) {
70 p_dev_rec = btm_sec_allocate_dev_rec();
71
72 p_dev_rec->bd_addr = bd_addr;
73 p_dev_rec->hci_handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_BR_EDR);
74
75 /* use default value for background connection params */
76 /* update conn params, use default value for background connection params */
77 memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
78 } else {
79 /* "Bump" timestamp for existing record */
80 p_dev_rec->timestamp = btm_cb.dev_rec_count++;
81
82 /* TODO(eisenbach):
83 * Small refactor, but leaving original logic for now.
84 * On the surface, this does not make any sense at all. Why change the
85 * bond state for an existing device here? This logic should be verified
86 * as part of a larger refactor.
87 */
88 p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
89 }
90
91 if (dev_class) memcpy(p_dev_rec->dev_class, dev_class, DEV_CLASS_LEN);
92
93 memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
94
95 if (bd_name && bd_name[0]) {
96 p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
97 strlcpy((char*)p_dev_rec->sec_bd_name, (char*)bd_name,
98 BTM_MAX_REM_BD_NAME_LEN);
99 }
100
101 p_dev_rec->num_read_pages = 0;
102 if (features) {
103 bool found = false;
104 memcpy(p_dev_rec->feature_pages, features,
105 sizeof(p_dev_rec->feature_pages));
106 for (int i = HCI_EXT_FEATURES_PAGE_MAX; !found && i >= 0; i--) {
107 for (int j = 0; j < HCI_FEATURE_BYTES_PER_PAGE; j++) {
108 if (p_dev_rec->feature_pages[i][j] != 0) {
109 found = true;
110 p_dev_rec->num_read_pages = i + 1;
111 break;
112 }
113 }
114 }
115 } else {
116 memset(p_dev_rec->feature_pages, 0, sizeof(p_dev_rec->feature_pages));
117 }
118
119 BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
120
121 if (link_key) {
122 VLOG(2) << __func__ << ": BDA: " << bd_addr;
123 p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_KNOWN;
124 memcpy(p_dev_rec->link_key, link_key, LINK_KEY_LEN);
125 p_dev_rec->link_key_type = key_type;
126 p_dev_rec->pin_code_length = pin_length;
127
128 if (pin_length >= 16 || key_type == BTM_LKEY_TYPE_AUTH_COMB ||
129 key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
130 // Set the flag if the link key was made by using either a 16 digit
131 // pin or MITM.
132 p_dev_rec->sec_flags |=
133 BTM_SEC_16_DIGIT_PIN_AUTHED | BTM_SEC_LINK_KEY_AUTHED;
134 }
135 }
136
137 #if (BTIF_MIXED_MODE_INCLUDED == TRUE)
138 if (key_type < BTM_MAX_PRE_SM4_LKEY_TYPE)
139 p_dev_rec->sm4 = BTM_SM4_KNOWN;
140 else
141 p_dev_rec->sm4 = BTM_SM4_TRUE;
142 #endif
143
144 p_dev_rec->rmt_io_caps = io_cap;
145 p_dev_rec->device_type |= BT_DEVICE_TYPE_BREDR;
146
147 return true;
148 }
149
150 /** Free resources associated with the device associated with |bd_addr| address.
151 *
152 * *** WARNING ***
153 * tBTM_SEC_DEV_REC associated with bd_addr becomes invalid after this function
154 * is called, also any of it's fields. i.e. if you use p_dev_rec->bd_addr, it is
155 * no longer valid!
156 * *** WARNING ***
157 *
158 * Returns true if removed OK, false if not found or ACL link is active.
159 */
BTM_SecDeleteDevice(const RawAddress & bd_addr)160 bool BTM_SecDeleteDevice(const RawAddress& bd_addr) {
161 if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE) ||
162 BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_BR_EDR)) {
163 BTM_TRACE_WARNING("%s FAILED: Cannot Delete when connection is active",
164 __func__);
165 return false;
166 }
167
168 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
169 if (p_dev_rec != NULL) {
170 RawAddress bda = p_dev_rec->bd_addr;
171 btm_sec_free_dev(p_dev_rec);
172 /* Tell controller to get rid of the link key, if it has one stored */
173 BTM_DeleteStoredLinkKey(&bda, NULL);
174 }
175
176 return true;
177 }
178
179 /*******************************************************************************
180 *
181 * Function BTM_SecClearSecurityFlags
182 *
183 * Description Reset the security flags (mark as not-paired) for a given
184 * remove device.
185 *
186 ******************************************************************************/
BTM_SecClearSecurityFlags(const RawAddress & bd_addr)187 extern void BTM_SecClearSecurityFlags(const RawAddress& bd_addr) {
188 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
189 if (p_dev_rec == NULL) return;
190
191 p_dev_rec->sec_flags = 0;
192 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
193 p_dev_rec->sm4 = BTM_SM4_UNKNOWN;
194 }
195
196 /*******************************************************************************
197 *
198 * Function BTM_SecReadDevName
199 *
200 * Description Looks for the device name in the security database for the
201 * specified BD address.
202 *
203 * Returns Pointer to the name or NULL
204 *
205 ******************************************************************************/
BTM_SecReadDevName(const RawAddress & bd_addr)206 char* BTM_SecReadDevName(const RawAddress& bd_addr) {
207 char* p_name = NULL;
208 tBTM_SEC_DEV_REC* p_srec;
209
210 p_srec = btm_find_dev(bd_addr);
211 if (p_srec != NULL) p_name = (char*)p_srec->sec_bd_name;
212
213 return (p_name);
214 }
215
216 /*******************************************************************************
217 *
218 * Function btm_sec_alloc_dev
219 *
220 * Description Look for the record in the device database for the record
221 * with specified address
222 *
223 * Returns Pointer to the record or NULL
224 *
225 ******************************************************************************/
btm_sec_alloc_dev(const RawAddress & bd_addr)226 tBTM_SEC_DEV_REC* btm_sec_alloc_dev(const RawAddress& bd_addr) {
227 tBTM_INQ_INFO* p_inq_info;
228 BTM_TRACE_EVENT("btm_sec_alloc_dev");
229
230 tBTM_SEC_DEV_REC* p_dev_rec = btm_sec_allocate_dev_rec();
231
232 /* Check with the BT manager if details about remote device are known */
233 /* outgoing connection */
234 p_inq_info = BTM_InqDbRead(bd_addr);
235 if (p_inq_info != NULL) {
236 memcpy(p_dev_rec->dev_class, p_inq_info->results.dev_class, DEV_CLASS_LEN);
237
238 p_dev_rec->device_type = p_inq_info->results.device_type;
239 p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
240 } else if (bd_addr == btm_cb.connecting_bda)
241 memcpy(p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
242
243 /* update conn params, use default value for background connection params */
244 memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
245
246 p_dev_rec->bd_addr = bd_addr;
247
248 p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_LE);
249 p_dev_rec->hci_handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_BR_EDR);
250
251 return (p_dev_rec);
252 }
253
254 /*******************************************************************************
255 *
256 * Function btm_sec_free_dev
257 *
258 * Description Mark device record as not used
259 *
260 ******************************************************************************/
btm_sec_free_dev(tBTM_SEC_DEV_REC * p_dev_rec)261 void btm_sec_free_dev(tBTM_SEC_DEV_REC* p_dev_rec) {
262 /* Clear out any saved BLE keys */
263 btm_sec_clear_ble_keys(p_dev_rec);
264 list_remove(btm_cb.sec_dev_rec, p_dev_rec);
265 }
266
267 /*******************************************************************************
268 *
269 * Function btm_dev_support_switch
270 *
271 * Description This function is called by the L2CAP to check if remote
272 * device supports role switch
273 *
274 * Parameters: bd_addr - Address of the peer device
275 *
276 * Returns true if device is known and role switch is supported
277 *
278 ******************************************************************************/
btm_dev_support_switch(const RawAddress & bd_addr)279 bool btm_dev_support_switch(const RawAddress& bd_addr) {
280 tBTM_SEC_DEV_REC* p_dev_rec;
281 uint8_t xx;
282 bool feature_empty = true;
283
284 #if (BTM_SCO_INCLUDED == TRUE)
285 /* Role switch is not allowed if a SCO is up */
286 if (btm_is_sco_active_by_bdaddr(bd_addr)) return (false);
287 #endif
288 p_dev_rec = btm_find_dev(bd_addr);
289 if (p_dev_rec &&
290 controller_get_interface()->supports_master_slave_role_switch()) {
291 if (HCI_SWITCH_SUPPORTED(p_dev_rec->feature_pages[0])) {
292 BTM_TRACE_DEBUG("btm_dev_support_switch return true (feature found)");
293 return (true);
294 }
295
296 /* If the feature field is all zero, we never received them */
297 for (xx = 0; xx < BD_FEATURES_LEN; xx++) {
298 if (p_dev_rec->feature_pages[0][xx] != 0x00) {
299 feature_empty = false; /* at least one is != 0 */
300 break;
301 }
302 }
303
304 /* If we don't know peer's capabilities, assume it supports Role-switch */
305 if (feature_empty) {
306 BTM_TRACE_DEBUG("btm_dev_support_switch return true (feature empty)");
307 return (true);
308 }
309 }
310
311 BTM_TRACE_DEBUG("btm_dev_support_switch return false");
312 return (false);
313 }
314
is_handle_equal(void * data,void * context)315 bool is_handle_equal(void* data, void* context) {
316 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
317 uint16_t* handle = static_cast<uint16_t*>(context);
318
319 if (p_dev_rec->hci_handle == *handle || p_dev_rec->ble_hci_handle == *handle)
320 return false;
321
322 return true;
323 }
324
325 /*******************************************************************************
326 *
327 * Function btm_find_dev_by_handle
328 *
329 * Description Look for the record in the device database for the record
330 * with specified handle
331 *
332 * Returns Pointer to the record or NULL
333 *
334 ******************************************************************************/
btm_find_dev_by_handle(uint16_t handle)335 tBTM_SEC_DEV_REC* btm_find_dev_by_handle(uint16_t handle) {
336 list_node_t* n = list_foreach(btm_cb.sec_dev_rec, is_handle_equal, &handle);
337 if (n) return static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
338
339 return NULL;
340 }
341
is_address_equal(void * data,void * context)342 bool is_address_equal(void* data, void* context) {
343 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
344 const RawAddress* bd_addr = ((RawAddress*)context);
345
346 if (p_dev_rec->bd_addr == *bd_addr) return false;
347 // If a LE random address is looking for device record
348 if (p_dev_rec->ble.pseudo_addr == *bd_addr) return false;
349
350 if (btm_ble_addr_resolvable(*bd_addr, p_dev_rec)) return false;
351 return true;
352 }
353
354 /*******************************************************************************
355 *
356 * Function btm_find_dev
357 *
358 * Description Look for the record in the device database for the record
359 * with specified BD address
360 *
361 * Returns Pointer to the record or NULL
362 *
363 ******************************************************************************/
btm_find_dev(const RawAddress & bd_addr)364 tBTM_SEC_DEV_REC* btm_find_dev(const RawAddress& bd_addr) {
365 list_node_t* n =
366 list_foreach(btm_cb.sec_dev_rec, is_address_equal, (void*)&bd_addr);
367 if (n) return static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
368
369 return NULL;
370 }
371
372 /*******************************************************************************
373 *
374 * Function btm_consolidate_dev
375 5**
376 * Description combine security records if identified as same peer
377 *
378 * Returns none
379 *
380 ******************************************************************************/
btm_consolidate_dev(tBTM_SEC_DEV_REC * p_target_rec)381 void btm_consolidate_dev(tBTM_SEC_DEV_REC* p_target_rec) {
382 tBTM_SEC_DEV_REC temp_rec = *p_target_rec;
383
384 BTM_TRACE_DEBUG("%s", __func__);
385
386 list_node_t* end = list_end(btm_cb.sec_dev_rec);
387 list_node_t* node = list_begin(btm_cb.sec_dev_rec);
388 while (node != end) {
389 tBTM_SEC_DEV_REC* p_dev_rec =
390 static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
391
392 // we do list_remove in some cases, must grab next before removing
393 node = list_next(node);
394
395 if (p_target_rec == p_dev_rec) continue;
396
397 if (p_dev_rec->bd_addr == p_target_rec->bd_addr) {
398 memcpy(p_target_rec, p_dev_rec, sizeof(tBTM_SEC_DEV_REC));
399 p_target_rec->ble = temp_rec.ble;
400 p_target_rec->ble_hci_handle = temp_rec.ble_hci_handle;
401 p_target_rec->enc_key_size = temp_rec.enc_key_size;
402 p_target_rec->conn_params = temp_rec.conn_params;
403 p_target_rec->device_type |= temp_rec.device_type;
404 p_target_rec->sec_flags |= temp_rec.sec_flags;
405
406 p_target_rec->new_encryption_key_is_p256 =
407 temp_rec.new_encryption_key_is_p256;
408 p_target_rec->no_smp_on_br = temp_rec.no_smp_on_br;
409 p_target_rec->bond_type = temp_rec.bond_type;
410
411 /* remove the combined record */
412 list_remove(btm_cb.sec_dev_rec, p_dev_rec);
413 }
414
415 /* an RPA device entry is a duplicate of the target record */
416 if (btm_ble_addr_resolvable(p_dev_rec->bd_addr, p_target_rec)) {
417 if (p_target_rec->ble.pseudo_addr == p_dev_rec->bd_addr) {
418 p_target_rec->ble.ble_addr_type = p_dev_rec->ble.ble_addr_type;
419 p_target_rec->device_type |= p_dev_rec->device_type;
420
421 /* remove the combined record */
422 list_remove(btm_cb.sec_dev_rec, p_dev_rec);
423 }
424 }
425 }
426 }
427
428 /*******************************************************************************
429 *
430 * Function btm_find_or_alloc_dev
431 *
432 * Description Look for the record in the device database for the record
433 * with specified BD address
434 *
435 * Returns Pointer to the record or NULL
436 *
437 ******************************************************************************/
btm_find_or_alloc_dev(const RawAddress & bd_addr)438 tBTM_SEC_DEV_REC* btm_find_or_alloc_dev(const RawAddress& bd_addr) {
439 tBTM_SEC_DEV_REC* p_dev_rec;
440 BTM_TRACE_EVENT("btm_find_or_alloc_dev");
441 p_dev_rec = btm_find_dev(bd_addr);
442 if (p_dev_rec == NULL) {
443 /* Allocate a new device record or reuse the oldest one */
444 p_dev_rec = btm_sec_alloc_dev(bd_addr);
445 }
446 return (p_dev_rec);
447 }
448
449 /*******************************************************************************
450 *
451 * Function btm_find_oldest_dev_rec
452 *
453 * Description Locates the oldest device in use. It first looks for
454 * the oldest non-paired device. If all devices are paired it
455 * returns the oldest paired device.
456 *
457 * Returns Pointer to the record or NULL
458 *
459 ******************************************************************************/
btm_find_oldest_dev_rec(void)460 static tBTM_SEC_DEV_REC* btm_find_oldest_dev_rec(void) {
461 tBTM_SEC_DEV_REC* p_oldest = NULL;
462 uint32_t ts_oldest = 0xFFFFFFFF;
463 tBTM_SEC_DEV_REC* p_oldest_paired = NULL;
464 uint32_t ts_oldest_paired = 0xFFFFFFFF;
465
466 list_node_t* end = list_end(btm_cb.sec_dev_rec);
467 for (list_node_t* node = list_begin(btm_cb.sec_dev_rec); node != end;
468 node = list_next(node)) {
469 tBTM_SEC_DEV_REC* p_dev_rec =
470 static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
471
472 if ((p_dev_rec->sec_flags &
473 (BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LE_LINK_KEY_KNOWN)) == 0) {
474 // Device is not paired
475 if (p_dev_rec->timestamp < ts_oldest) {
476 p_oldest = p_dev_rec;
477 ts_oldest = p_dev_rec->timestamp;
478 }
479 } else {
480 // Paired device
481 if (p_dev_rec->timestamp < ts_oldest_paired) {
482 p_oldest_paired = p_dev_rec;
483 ts_oldest_paired = p_dev_rec->timestamp;
484 }
485 }
486 }
487
488 // If we did not find any non-paired devices, use the oldest paired one...
489 if (ts_oldest == 0xFFFFFFFF) p_oldest = p_oldest_paired;
490
491 return p_oldest;
492 }
493
494 /*******************************************************************************
495 *
496 * Function btm_sec_allocate_dev_rec
497 *
498 * Description Attempts to allocate a new device record. If we have
499 * exceeded the maximum number of allowable records to
500 * allocate, the oldest record will be deleted to make room
501 * for the new record.
502 *
503 * Returns Pointer to the newly allocated record
504 *
505 ******************************************************************************/
btm_sec_allocate_dev_rec(void)506 tBTM_SEC_DEV_REC* btm_sec_allocate_dev_rec(void) {
507 tBTM_SEC_DEV_REC* p_dev_rec = NULL;
508
509 if (list_length(btm_cb.sec_dev_rec) > BTM_SEC_MAX_DEVICE_RECORDS) {
510 p_dev_rec = btm_find_oldest_dev_rec();
511 list_remove(btm_cb.sec_dev_rec, p_dev_rec);
512 }
513
514 p_dev_rec =
515 static_cast<tBTM_SEC_DEV_REC*>(osi_calloc(sizeof(tBTM_SEC_DEV_REC)));
516 list_append(btm_cb.sec_dev_rec, p_dev_rec);
517
518 // Initialize defaults
519 p_dev_rec->sec_flags = BTM_SEC_IN_USE;
520 p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
521 p_dev_rec->timestamp = btm_cb.dev_rec_count++;
522 p_dev_rec->rmt_io_caps = BTM_IO_CAP_UNKNOWN;
523
524 return p_dev_rec;
525 }
526
527 /*******************************************************************************
528 *
529 * Function btm_get_bond_type_dev
530 *
531 * Description Get the bond type for a device in the device database
532 * with specified BD address
533 *
534 * Returns The device bond type if known, otherwise BOND_TYPE_UNKNOWN
535 *
536 ******************************************************************************/
btm_get_bond_type_dev(const RawAddress & bd_addr)537 tBTM_BOND_TYPE btm_get_bond_type_dev(const RawAddress& bd_addr) {
538 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
539
540 if (p_dev_rec == NULL) return BOND_TYPE_UNKNOWN;
541
542 return p_dev_rec->bond_type;
543 }
544
545 /*******************************************************************************
546 *
547 * Function btm_set_bond_type_dev
548 *
549 * Description Set the bond type for a device in the device database
550 * with specified BD address
551 *
552 * Returns true on success, otherwise false
553 *
554 ******************************************************************************/
btm_set_bond_type_dev(const RawAddress & bd_addr,tBTM_BOND_TYPE bond_type)555 bool btm_set_bond_type_dev(const RawAddress& bd_addr,
556 tBTM_BOND_TYPE bond_type) {
557 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
558
559 if (p_dev_rec == NULL) return false;
560
561 p_dev_rec->bond_type = bond_type;
562 return true;
563 }
564