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 address management.
22 *
23 ******************************************************************************/
24
25 #include <base/bind.h>
26 #include <string.h>
27
28 #include "btm_ble_int.h"
29 #include "device/include/controller.h"
30 #include "gap_api.h"
31 #include "main/shim/shim.h"
32 #include "osi/include/osi.h" // UNUSED_ATTR
33 #include "stack/btm/btm_dev.h"
34 #include "stack/crypto_toolbox/crypto_toolbox.h"
35 #include "stack/include/acl_api.h"
36 #include "stack/include/bt_octets.h"
37 #include "types/ble_address_with_type.h"
38 #include "types/raw_address.h"
39
40 extern tBTM_CB btm_cb;
41
42 /* This function generates Resolvable Private Address (RPA) from Identity
43 * Resolving Key |irk| and |random|*/
generate_rpa_from_irk_and_rand(const Octet16 & irk,BT_OCTET8 random)44 static RawAddress generate_rpa_from_irk_and_rand(const Octet16& irk,
45 BT_OCTET8 random) {
46 random[2] &= (~BLE_RESOLVE_ADDR_MASK);
47 random[2] |= BLE_RESOLVE_ADDR_MSB;
48
49 RawAddress address;
50 address.address[2] = random[0];
51 address.address[1] = random[1];
52 address.address[0] = random[2];
53
54 /* encrypt with IRK */
55 Octet16 p = crypto_toolbox::aes_128(irk, random, 3);
56
57 /* set hash to be LSB of rpAddress */
58 address.address[5] = p[0];
59 address.address[4] = p[1];
60 address.address[3] = p[2];
61 return address;
62 }
63
64 /** This function is called when random address for local controller was
65 * generated */
btm_gen_resolve_paddr_low(const RawAddress & address)66 void btm_gen_resolve_paddr_low(const RawAddress& address) {
67 /* when GD advertising and scanning modules are enabled, set random address
68 * via address manager in GD */
69 LOG_INFO("GD advertising and scanning modules are enabled, skip");
70 }
71
72 /** This function generate a resolvable private address using local IRK */
btm_gen_resolvable_private_addr(base::Callback<void (const RawAddress &)> cb)73 void btm_gen_resolvable_private_addr(
74 base::Callback<void(const RawAddress&)> cb) {
75 /* generate 3B rand as BD LSB, SRK with it, get BD MSB */
76 btsnd_hcic_ble_rand(base::Bind(
77 [](base::Callback<void(const RawAddress&)> cb, BT_OCTET8 random) {
78 const Octet16& irk = BTM_GetDeviceIDRoot();
79 cb.Run(generate_rpa_from_irk_and_rand(irk, random));
80 },
81 std::move(cb)));
82 }
83
btm_get_next_private_addrress_interval_ms()84 uint64_t btm_get_next_private_addrress_interval_ms() {
85 /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
86 const uint64_t interval_min_ms = (7 * 60 * 1000);
87 const uint64_t interval_random_part_max_ms = (8 * 60 * 1000);
88
89 return interval_min_ms + std::rand() % interval_random_part_max_ms;
90 }
91
92 /*******************************************************************************
93 * Utility functions for Random address resolving
94 ******************************************************************************/
95
96 /*******************************************************************************
97 *
98 * Function btm_ble_init_pseudo_addr
99 *
100 * Description This function is used to initialize pseudo address.
101 * If pseudo address is not available, use dummy address
102 *
103 * Returns true is updated; false otherwise.
104 *
105 ******************************************************************************/
btm_ble_init_pseudo_addr(tBTM_SEC_DEV_REC * p_dev_rec,const RawAddress & new_pseudo_addr)106 bool btm_ble_init_pseudo_addr(tBTM_SEC_DEV_REC* p_dev_rec,
107 const RawAddress& new_pseudo_addr) {
108 if (p_dev_rec->ble.pseudo_addr.IsEmpty()) {
109 p_dev_rec->ble.pseudo_addr = new_pseudo_addr;
110 return true;
111 }
112
113 return false;
114 }
115
116 /* Return true if given Resolvable Privae Address |rpa| matches Identity
117 * Resolving Key |irk| */
rpa_matches_irk(const RawAddress & rpa,const Octet16 & irk)118 static bool rpa_matches_irk(const RawAddress& rpa, const Octet16& irk) {
119 /* use the 3 MSB of bd address as prand */
120 uint8_t rand[3];
121 rand[0] = rpa.address[2];
122 rand[1] = rpa.address[1];
123 rand[2] = rpa.address[0];
124
125 /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
126 Octet16 x = crypto_toolbox::aes_128(irk, &rand[0], 3);
127
128 rand[0] = rpa.address[5];
129 rand[1] = rpa.address[4];
130 rand[2] = rpa.address[3];
131
132 if (memcmp(x.data(), &rand[0], 3) == 0) {
133 // match
134 return true;
135 }
136 // not a match
137 return false;
138 }
139
140 /** This function checks if a RPA is resolvable by the device key.
141 * Returns true is resolvable; false otherwise.
142 */
btm_ble_addr_resolvable(const RawAddress & rpa,tBTM_SEC_DEV_REC * p_dev_rec)143 bool btm_ble_addr_resolvable(const RawAddress& rpa,
144 tBTM_SEC_DEV_REC* p_dev_rec) {
145 if (!BTM_BLE_IS_RESOLVE_BDA(rpa)) return false;
146
147 if ((p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) &&
148 (p_dev_rec->ble.key_type & BTM_LE_KEY_PID)) {
149 BTM_TRACE_DEBUG("%s try to resolve", __func__);
150
151 if (rpa_matches_irk(rpa, p_dev_rec->ble.keys.irk)) {
152 btm_ble_init_pseudo_addr(p_dev_rec, rpa);
153 return true;
154 }
155 }
156 return false;
157 }
158
159 /** This function match the random address to the appointed device record,
160 * starting from calculating IRK. If the record index exceeds the maximum record
161 * number, matching failed and send a callback. */
btm_ble_match_random_bda(void * data,void * context)162 static bool btm_ble_match_random_bda(void* data, void* context) {
163 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
164 RawAddress* random_bda = static_cast<RawAddress*>(context);
165
166 if (!(p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) ||
167 !(p_dev_rec->ble.key_type & BTM_LE_KEY_PID))
168 // Match fails preconditions
169 return true;
170
171 if (rpa_matches_irk(*random_bda, p_dev_rec->ble.keys.irk)) {
172 // Matched
173 return false;
174 }
175
176 // This item not a match, continue iteration
177 return true;
178 }
179
180 /** This function is called to resolve a random address.
181 * Returns pointer to the security record of the device whom a random address is
182 * matched to.
183 */
btm_ble_resolve_random_addr(const RawAddress & random_bda)184 tBTM_SEC_DEV_REC* btm_ble_resolve_random_addr(const RawAddress& random_bda) {
185 if (btm_cb.sec_dev_rec == nullptr) return nullptr;
186 list_node_t* n = list_foreach(btm_cb.sec_dev_rec, btm_ble_match_random_bda,
187 (void*)&random_bda);
188 return (n == nullptr) ? (nullptr)
189 : (static_cast<tBTM_SEC_DEV_REC*>(list_node(n)));
190 }
191
192 /*******************************************************************************
193 * address mapping between pseudo address and real connection address
194 ******************************************************************************/
195 /** Find the security record whose LE identity address is matching */
btm_find_dev_by_identity_addr(const RawAddress & bd_addr,uint8_t addr_type)196 static tBTM_SEC_DEV_REC* btm_find_dev_by_identity_addr(
197 const RawAddress& bd_addr, uint8_t addr_type) {
198 if (btm_cb.sec_dev_rec == nullptr) return nullptr;
199
200 list_node_t* end = list_end(btm_cb.sec_dev_rec);
201 for (list_node_t* node = list_begin(btm_cb.sec_dev_rec); node != end;
202 node = list_next(node)) {
203 tBTM_SEC_DEV_REC* p_dev_rec =
204 static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
205 if (p_dev_rec->ble.identity_address_with_type.bda == bd_addr) {
206 if ((p_dev_rec->ble.identity_address_with_type.type &
207 (~BLE_ADDR_TYPE_ID_BIT)) != (addr_type & (~BLE_ADDR_TYPE_ID_BIT)))
208 BTM_TRACE_WARNING(
209 "%s find pseudo->random match with diff addr type: %d vs %d",
210 __func__, p_dev_rec->ble.identity_address_with_type.type,
211 addr_type);
212
213 /* found the match */
214 return p_dev_rec;
215 }
216 }
217
218 return NULL;
219 }
220
221 /*******************************************************************************
222 *
223 * Function btm_identity_addr_to_random_pseudo
224 *
225 * Description This function map a static BD address to a pseudo random
226 * address in security database.
227 *
228 ******************************************************************************/
btm_identity_addr_to_random_pseudo(RawAddress * bd_addr,tBLE_ADDR_TYPE * p_addr_type,bool refresh)229 bool btm_identity_addr_to_random_pseudo(RawAddress* bd_addr,
230 tBLE_ADDR_TYPE* p_addr_type,
231 bool refresh) {
232 tBTM_SEC_DEV_REC* p_dev_rec =
233 btm_find_dev_by_identity_addr(*bd_addr, *p_addr_type);
234 if (p_dev_rec == nullptr) {
235 return false;
236 }
237
238 /* evt reported on static address, map static address to random pseudo */
239 /* if RPA offloading is supported, or 4.2 controller, do RPA refresh */
240 if (refresh &&
241 controller_get_interface()->get_ble_resolving_list_max_size() != 0) {
242 btm_ble_read_resolving_list_entry(p_dev_rec);
243 }
244
245 /* assign the original address to be the current report address */
246 if (!btm_ble_init_pseudo_addr(p_dev_rec, *bd_addr)) {
247 *bd_addr = p_dev_rec->ble.pseudo_addr;
248 }
249
250 *p_addr_type = p_dev_rec->ble.AddressType();
251 return true;
252 }
253
btm_identity_addr_to_random_pseudo_from_address_with_type(tBLE_BD_ADDR * address_with_type,bool refresh)254 bool btm_identity_addr_to_random_pseudo_from_address_with_type(
255 tBLE_BD_ADDR* address_with_type, bool refresh) {
256 return btm_identity_addr_to_random_pseudo(
257 &(address_with_type->bda), &(address_with_type->type), refresh);
258 }
259
260 /*******************************************************************************
261 *
262 * Function btm_random_pseudo_to_identity_addr
263 *
264 * Description This function map a random pseudo address to a public
265 * address. random_pseudo is input and output parameter
266 *
267 ******************************************************************************/
btm_random_pseudo_to_identity_addr(RawAddress * random_pseudo,tBLE_ADDR_TYPE * p_identity_addr_type)268 bool btm_random_pseudo_to_identity_addr(RawAddress* random_pseudo,
269 tBLE_ADDR_TYPE* p_identity_addr_type) {
270 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(*random_pseudo);
271
272 if (p_dev_rec != NULL) {
273 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
274 *p_identity_addr_type = p_dev_rec->ble.identity_address_with_type.type;
275 *random_pseudo = p_dev_rec->ble.identity_address_with_type.bda;
276 if (controller_get_interface()->supports_ble_privacy())
277 *p_identity_addr_type |= BLE_ADDR_TYPE_ID_BIT;
278 return true;
279 }
280 }
281 return false;
282 }
283
284 /*******************************************************************************
285 *
286 * Function btm_ble_refresh_peer_resolvable_private_addr
287 *
288 * Description This function refresh the currently used resolvable remote
289 * private address into security database and set active
290 * connection address.
291 *
292 ******************************************************************************/
btm_ble_refresh_peer_resolvable_private_addr(const RawAddress & pseudo_bda,const RawAddress & rpa,tBTM_SEC_BLE::tADDRESS_TYPE rra_type)293 void btm_ble_refresh_peer_resolvable_private_addr(
294 const RawAddress& pseudo_bda, const RawAddress& rpa,
295 tBTM_SEC_BLE::tADDRESS_TYPE rra_type) {
296 tBTM_SEC_DEV_REC* p_sec_rec = btm_find_dev(pseudo_bda);
297 if (p_sec_rec == nullptr) {
298 LOG_WARN("%s No matching known device in record", __func__);
299 return;
300 }
301
302 p_sec_rec->ble.cur_rand_addr = rpa;
303
304 if (rra_type == tBTM_SEC_BLE::BTM_BLE_ADDR_PSEUDO) {
305 p_sec_rec->ble.active_addr_type = rpa.IsEmpty()
306 ? tBTM_SEC_BLE::BTM_BLE_ADDR_STATIC
307 : tBTM_SEC_BLE::BTM_BLE_ADDR_RRA;
308 } else {
309 p_sec_rec->ble.active_addr_type = rra_type;
310 }
311
312 /* connection refresh remote address */
313 const auto& identity_address = p_sec_rec->ble.identity_address_with_type.bda;
314 auto identity_address_type = p_sec_rec->ble.identity_address_with_type.type;
315
316 if (!acl_refresh_remote_address(identity_address, identity_address_type,
317 p_sec_rec->bd_addr, rra_type, rpa)) {
318 // Try looking up the pseudo random address
319 if (!acl_refresh_remote_address(identity_address, identity_address_type,
320 p_sec_rec->ble.pseudo_addr, rra_type,
321 rpa)) {
322 LOG_ERROR("%s Unknown device to refresh remote device", __func__);
323 }
324 }
325 }
326