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