1 /******************************************************************************
2 *
3 * Copyright 2009-2013 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 #define LOG_TAG "bt_btif_gatt"
20
21 #include "btif_gatt_util.h"
22
23 #include <bluetooth/log.h>
24 #include <hardware/bluetooth.h>
25 #include <hardware/bt_gatt.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <algorithm>
30
31 #include "bta/include/bta_api_data_types.h"
32 #include "bta/include/bta_sec_api.h"
33 #include "btif_storage.h"
34 #include "common/init_flags.h"
35 #include "os/log.h"
36 #include "os/system_properties.h"
37 #include "osi/include/allocator.h"
38 #include "stack/btm/btm_sec.h"
39 #include "stack/include/acl_api.h"
40 #include "types/ble_address_with_type.h"
41 #include "types/bluetooth/uuid.h"
42 #include "types/bt_transport.h"
43 #include "types/raw_address.h"
44
45 using bluetooth::Uuid;
46 using namespace bluetooth;
47
48 /*******************************************************************************
49 * BTIF -> BTA conversion functions
50 ******************************************************************************/
btif_to_bta_response(tGATTS_RSP * p_dest,btgatt_response_t * p_src)51 void btif_to_bta_response(tGATTS_RSP* p_dest, btgatt_response_t* p_src) {
52 p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
53 p_dest->attr_value.handle = p_src->attr_value.handle;
54 p_dest->attr_value.len = std::min<uint16_t>(p_src->attr_value.len, GATT_MAX_ATTR_LEN);
55 p_dest->attr_value.offset = p_src->attr_value.offset;
56 memcpy(p_dest->attr_value.value, p_src->attr_value.value, p_dest->attr_value.len);
57 }
58
59 /*******************************************************************************
60 * Encrypted link map handling
61 ******************************************************************************/
62
btif_gatt_is_link_encrypted(const RawAddress & bd_addr)63 static bool btif_gatt_is_link_encrypted(const RawAddress& bd_addr) {
64 return BTM_IsEncrypted(bd_addr, BT_TRANSPORT_BR_EDR) ||
65 BTM_IsEncrypted(bd_addr, BT_TRANSPORT_LE);
66 }
67
btif_gatt_set_encryption_cb(const RawAddress &,tBT_TRANSPORT,tBTA_STATUS result)68 static void btif_gatt_set_encryption_cb(const RawAddress& /* bd_addr */,
69 tBT_TRANSPORT /* transport */,
70 tBTA_STATUS result) {
71 if (result != BTA_SUCCESS && result != BTA_BUSY) {
72 log::warn("Encryption failed ({})", result);
73 }
74 }
75
btif_gatt_check_encrypted_link(RawAddress bd_addr,tBT_TRANSPORT transport_link)76 void btif_gatt_check_encrypted_link(RawAddress bd_addr,
77 tBT_TRANSPORT transport_link) {
78 RawAddress raw_local_addr;
79 tBLE_ADDR_TYPE local_addr_type;
80 BTM_ReadConnectionAddr(bd_addr, raw_local_addr, &local_addr_type);
81 tBLE_BD_ADDR local_addr{local_addr_type, raw_local_addr};
82 if (!local_addr.IsPublic() && !local_addr.IsAddressResolvable()) {
83 log::debug("Not establishing encryption since address type is NRPA");
84 return;
85 }
86
87 static const bool check_encrypted = bluetooth::os::GetSystemPropertyBool(
88 "bluetooth.gatt.check_encrypted_link.enabled", true);
89 if (!check_encrypted) {
90 log::debug("Check skipped due to system config");
91 return;
92 }
93 tBTM_LE_PENC_KEYS key;
94 if ((btif_storage_get_ble_bonding_key(
95 bd_addr, BTM_LE_KEY_PENC, (uint8_t*)&key,
96 sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) &&
97 !btif_gatt_is_link_encrypted(bd_addr)) {
98 log::debug("Checking gatt link peer:{} transport:{}", bd_addr,
99 bt_transport_text(transport_link));
100 BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb,
101 BTM_BLE_SEC_ENCRYPT);
102 }
103 }
104