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 "bt_types.h"
29 #include "btm_int.h"
30 #include "btu.h"
31 #include "device/include/controller.h"
32 #include "gap_api.h"
33 #include "hcimsgs.h"
34
35 #include "btm_ble_int.h"
36 #include "stack/crypto_toolbox/crypto_toolbox.h"
37
38 /* This function generates Resolvable Private Address (RPA) from Identity
39 * Resolving Key |irk| and |random|*/
generate_rpa_from_irk_and_rand(const Octet16 & irk,BT_OCTET8 random)40 RawAddress generate_rpa_from_irk_and_rand(const Octet16& irk,
41 BT_OCTET8 random) {
42 random[2] &= (~BLE_RESOLVE_ADDR_MASK);
43 random[2] |= BLE_RESOLVE_ADDR_MSB;
44
45 RawAddress address;
46 address.address[2] = random[0];
47 address.address[1] = random[1];
48 address.address[0] = random[2];
49
50 /* encrypt with IRK */
51 Octet16 p = crypto_toolbox::aes_128(irk, random, 3);
52
53 /* set hash to be LSB of rpAddress */
54 address.address[5] = p[0];
55 address.address[4] = p[1];
56 address.address[3] = p[2];
57 return address;
58 }
59
60 /** This function is called when random address for local controller was
61 * generated */
btm_gen_resolve_paddr_low(const RawAddress & address)62 void btm_gen_resolve_paddr_low(const RawAddress& address) {
63 tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
64
65 BTM_TRACE_EVENT("btm_gen_resolve_paddr_low");
66
67 p_cb->private_addr = address;
68
69 /* set it to controller */
70 btm_ble_set_random_address(p_cb->private_addr);
71
72 p_cb->own_addr_type = BLE_ADDR_RANDOM;
73
74 /* start a periodical timer to refresh random addr */
75 uint64_t interval_ms = btm_get_next_private_addrress_interval_ms();
76 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE)
77 interval_ms = btm_cb.ble_ctr_cb.rpa_tout * 1000;
78 #endif
79 alarm_set_on_mloop(p_cb->refresh_raddr_timer, interval_ms,
80 btm_ble_refresh_raddr_timer_timeout, NULL);
81 }
82
83 /** This function generate a resolvable private address using local IRK */
btm_gen_resolvable_private_addr(base::Callback<void (const RawAddress &)> cb)84 void btm_gen_resolvable_private_addr(
85 base::Callback<void(const RawAddress&)> cb) {
86 BTM_TRACE_EVENT("%s", __func__);
87 /* generate 3B rand as BD LSB, SRK with it, get BD MSB */
88 btsnd_hcic_ble_rand(base::Bind(
89 [](base::Callback<void(const RawAddress&)> cb, BT_OCTET8 random) {
90 const Octet16& irk = BTM_GetDeviceIDRoot();
91 cb.Run(generate_rpa_from_irk_and_rand(irk, random));
92 },
93 std::move(cb)));
94 }
95
btm_get_next_private_addrress_interval_ms()96 uint64_t btm_get_next_private_addrress_interval_ms() {
97 /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
98 const uint64_t interval_min_ms = (7 * 60 * 1000);
99 const uint64_t interval_random_part_max_ms = (8 * 60 * 1000);
100
101 return interval_min_ms + std::rand() % interval_random_part_max_ms;
102 }
103
104 /*******************************************************************************
105 * Utility functions for Random address resolving
106 ******************************************************************************/
107
108 /*******************************************************************************
109 *
110 * Function btm_ble_init_pseudo_addr
111 *
112 * Description This function is used to initialize pseudo address.
113 * If pseudo address is not available, use dummy address
114 *
115 * Returns true is updated; false otherwise.
116 *
117 ******************************************************************************/
btm_ble_init_pseudo_addr(tBTM_SEC_DEV_REC * p_dev_rec,const RawAddress & new_pseudo_addr)118 bool btm_ble_init_pseudo_addr(tBTM_SEC_DEV_REC* p_dev_rec,
119 const RawAddress& new_pseudo_addr) {
120 if (p_dev_rec->ble.pseudo_addr.IsEmpty()) {
121 p_dev_rec->ble.pseudo_addr = new_pseudo_addr;
122 return true;
123 }
124
125 return false;
126 }
127
128 /* Return true if given Resolvable Privae Address |rpa| matches Identity
129 * Resolving Key |irk| */
rpa_matches_irk(const RawAddress & rpa,const Octet16 & irk)130 static bool rpa_matches_irk(const RawAddress& rpa, const Octet16& irk) {
131 /* use the 3 MSB of bd address as prand */
132 uint8_t rand[3];
133 rand[0] = rpa.address[2];
134 rand[1] = rpa.address[1];
135 rand[2] = rpa.address[0];
136
137 /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
138 Octet16 x = crypto_toolbox::aes_128(irk, &rand[0], 3);
139
140 rand[0] = rpa.address[5];
141 rand[1] = rpa.address[4];
142 rand[2] = rpa.address[3];
143
144 if (memcmp(x.data(), &rand[0], 3) == 0) {
145 // match
146 return true;
147 }
148 // not a match
149 return false;
150 }
151
152 /** This function checks if a RPA is resolvable by the device key.
153 * Returns true is resolvable; false otherwise.
154 */
btm_ble_addr_resolvable(const RawAddress & rpa,tBTM_SEC_DEV_REC * p_dev_rec)155 bool btm_ble_addr_resolvable(const RawAddress& rpa,
156 tBTM_SEC_DEV_REC* p_dev_rec) {
157 if (!BTM_BLE_IS_RESOLVE_BDA(rpa)) return false;
158
159 if ((p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) &&
160 (p_dev_rec->ble.key_type & BTM_LE_KEY_PID)) {
161 BTM_TRACE_DEBUG("%s try to resolve", __func__);
162
163 if (rpa_matches_irk(rpa, p_dev_rec->ble.keys.irk)) {
164 btm_ble_init_pseudo_addr(p_dev_rec, rpa);
165 return true;
166 }
167 }
168 return false;
169 }
170
171 /** This function match the random address to the appointed device record,
172 * starting from calculating IRK. If the record index exceeds the maximum record
173 * number, matching failed and send a callback. */
btm_ble_match_random_bda(void * data,void * context)174 static bool btm_ble_match_random_bda(void* data, void* context) {
175 BTM_TRACE_EVENT("%s next iteration", __func__);
176 RawAddress* random_bda = (RawAddress*)context;
177
178 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
179
180 BTM_TRACE_DEBUG("sec_flags = %02x device_type = %d", p_dev_rec->sec_flags,
181 p_dev_rec->device_type);
182
183 if (!(p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) ||
184 !(p_dev_rec->ble.key_type & BTM_LE_KEY_PID))
185 return true;
186
187 if (rpa_matches_irk(*random_bda, p_dev_rec->ble.keys.irk)) {
188 BTM_TRACE_EVENT("match is found");
189 // if it was match, finish iteration, otherwise continue
190 return false;
191 }
192
193 // not a match, continue iteration
194 return true;
195 }
196
197 /** This function is called to resolve a random address.
198 * Returns pointer to the security record of the device whom a random address is
199 * matched to.
200 */
btm_ble_resolve_random_addr(const RawAddress & random_bda)201 tBTM_SEC_DEV_REC* btm_ble_resolve_random_addr(const RawAddress& random_bda) {
202 BTM_TRACE_EVENT("%s", __func__);
203
204 /* start to resolve random address */
205 /* check for next security record */
206
207 list_node_t* n = list_foreach(btm_cb.sec_dev_rec, btm_ble_match_random_bda,
208 (void*)&random_bda);
209 tBTM_SEC_DEV_REC* p_dev_rec = nullptr;
210 if (n != nullptr) p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
211
212 BTM_TRACE_EVENT("%s: %sresolved", __func__,
213 (p_dev_rec == nullptr ? "not " : ""));
214 return p_dev_rec;
215 }
216
217 /*******************************************************************************
218 * address mapping between pseudo address and real connection address
219 ******************************************************************************/
220 /** Find the security record whose LE identity address is matching */
btm_find_dev_by_identity_addr(const RawAddress & bd_addr,uint8_t addr_type)221 tBTM_SEC_DEV_REC* btm_find_dev_by_identity_addr(const RawAddress& bd_addr,
222 uint8_t addr_type) {
223 #if (BLE_PRIVACY_SPT == TRUE)
224 list_node_t* end = list_end(btm_cb.sec_dev_rec);
225 for (list_node_t* node = list_begin(btm_cb.sec_dev_rec); node != end;
226 node = list_next(node)) {
227 tBTM_SEC_DEV_REC* p_dev_rec =
228 static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
229 if (p_dev_rec->ble.identity_addr == bd_addr) {
230 if ((p_dev_rec->ble.identity_addr_type & (~BLE_ADDR_TYPE_ID_BIT)) !=
231 (addr_type & (~BLE_ADDR_TYPE_ID_BIT)))
232 BTM_TRACE_WARNING(
233 "%s find pseudo->random match with diff addr type: %d vs %d",
234 __func__, p_dev_rec->ble.identity_addr_type, addr_type);
235
236 /* found the match */
237 return p_dev_rec;
238 }
239 }
240 #endif
241
242 return NULL;
243 }
244
245 /*******************************************************************************
246 *
247 * Function btm_identity_addr_to_random_pseudo
248 *
249 * Description This function map a static BD address to a pseudo random
250 * address in security database.
251 *
252 ******************************************************************************/
btm_identity_addr_to_random_pseudo(RawAddress * bd_addr,uint8_t * p_addr_type,bool refresh)253 bool btm_identity_addr_to_random_pseudo(RawAddress* bd_addr,
254 uint8_t* p_addr_type, bool refresh) {
255 #if (BLE_PRIVACY_SPT == TRUE)
256 tBTM_SEC_DEV_REC* p_dev_rec =
257 btm_find_dev_by_identity_addr(*bd_addr, *p_addr_type);
258
259 BTM_TRACE_EVENT("%s", __func__);
260 /* evt reported on static address, map static address to random pseudo */
261 if (p_dev_rec != NULL) {
262 /* if RPA offloading is supported, or 4.2 controller, do RPA refresh */
263 if (refresh &&
264 controller_get_interface()->get_ble_resolving_list_max_size() != 0)
265 btm_ble_read_resolving_list_entry(p_dev_rec);
266
267 /* assign the original address to be the current report address */
268 if (!btm_ble_init_pseudo_addr(p_dev_rec, *bd_addr))
269 *bd_addr = p_dev_rec->ble.pseudo_addr;
270
271 *p_addr_type = p_dev_rec->ble.ble_addr_type;
272 return true;
273 }
274 #endif
275 return false;
276 }
277
278 /*******************************************************************************
279 *
280 * Function btm_random_pseudo_to_identity_addr
281 *
282 * Description This function map a random pseudo address to a public
283 * address. random_pseudo is input and output parameter
284 *
285 ******************************************************************************/
btm_random_pseudo_to_identity_addr(RawAddress * random_pseudo,uint8_t * p_identity_addr_type)286 bool btm_random_pseudo_to_identity_addr(RawAddress* random_pseudo,
287 uint8_t* p_identity_addr_type) {
288 #if (BLE_PRIVACY_SPT == TRUE)
289 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(*random_pseudo);
290
291 if (p_dev_rec != NULL) {
292 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
293 *p_identity_addr_type = p_dev_rec->ble.identity_addr_type;
294 *random_pseudo = p_dev_rec->ble.identity_addr;
295 if (controller_get_interface()->supports_ble_privacy())
296 *p_identity_addr_type |= BLE_ADDR_TYPE_ID_BIT;
297 return true;
298 }
299 }
300 #endif
301 return false;
302 }
303
304 /*******************************************************************************
305 *
306 * Function btm_ble_refresh_peer_resolvable_private_addr
307 *
308 * Description This function refresh the currently used resolvable remote
309 * private address into security database and set active
310 * connection address.
311 *
312 ******************************************************************************/
btm_ble_refresh_peer_resolvable_private_addr(const RawAddress & pseudo_bda,const RawAddress & rpa,uint8_t rra_type)313 void btm_ble_refresh_peer_resolvable_private_addr(const RawAddress& pseudo_bda,
314 const RawAddress& rpa,
315 uint8_t rra_type) {
316 #if (BLE_PRIVACY_SPT == TRUE)
317 uint8_t rra_dummy = false;
318 if (rpa.IsEmpty()) rra_dummy = true;
319
320 /* update security record here, in adv event or connection complete process */
321 tBTM_SEC_DEV_REC* p_sec_rec = btm_find_dev(pseudo_bda);
322 if (p_sec_rec != NULL) {
323 p_sec_rec->ble.cur_rand_addr = rpa;
324
325 /* unknown, if dummy address, set to static */
326 if (rra_type == BTM_BLE_ADDR_PSEUDO)
327 p_sec_rec->ble.active_addr_type =
328 rra_dummy ? BTM_BLE_ADDR_STATIC : BTM_BLE_ADDR_RRA;
329 else
330 p_sec_rec->ble.active_addr_type = rra_type;
331 } else {
332 BTM_TRACE_ERROR("No matching known device in record");
333 return;
334 }
335
336 BTM_TRACE_DEBUG("%s: active_addr_type: %d ", __func__,
337 p_sec_rec->ble.active_addr_type);
338
339 /* connection refresh remote address */
340 tACL_CONN* p_acl = btm_bda_to_acl(p_sec_rec->bd_addr, BT_TRANSPORT_LE);
341 if (p_acl == NULL)
342 p_acl = btm_bda_to_acl(p_sec_rec->ble.pseudo_addr, BT_TRANSPORT_LE);
343
344 if (p_acl != NULL) {
345 if (rra_type == BTM_BLE_ADDR_PSEUDO) {
346 /* use identity address, resolvable_private_addr is empty */
347 if (rra_dummy) {
348 p_acl->active_remote_addr_type = p_sec_rec->ble.identity_addr_type;
349 p_acl->active_remote_addr = p_sec_rec->ble.identity_addr;
350 } else {
351 p_acl->active_remote_addr_type = BLE_ADDR_RANDOM;
352 p_acl->active_remote_addr = rpa;
353 }
354 } else {
355 p_acl->active_remote_addr_type = rra_type;
356 p_acl->active_remote_addr = rpa;
357 }
358
359 BTM_TRACE_DEBUG("p_acl->active_remote_addr_type: %d ",
360 p_acl->active_remote_addr_type);
361 VLOG(1) << __func__ << " conn_addr: " << p_acl->active_remote_addr;
362 }
363 #endif
364 }
365
366 /*******************************************************************************
367 *
368 * Function btm_ble_refresh_local_resolvable_private_addr
369 *
370 * Description This function refresh the currently used resolvable private
371 * address for the active link to the remote device
372 *
373 ******************************************************************************/
btm_ble_refresh_local_resolvable_private_addr(const RawAddress & pseudo_addr,const RawAddress & local_rpa)374 void btm_ble_refresh_local_resolvable_private_addr(
375 const RawAddress& pseudo_addr, const RawAddress& local_rpa) {
376 #if (BLE_PRIVACY_SPT == TRUE)
377 tACL_CONN* p = btm_bda_to_acl(pseudo_addr, BT_TRANSPORT_LE);
378
379 if (p != NULL) {
380 if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
381 p->conn_addr_type = BLE_ADDR_RANDOM;
382 if (!local_rpa.IsEmpty())
383 p->conn_addr = local_rpa;
384 else
385 p->conn_addr = btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr;
386 } else {
387 p->conn_addr_type = BLE_ADDR_PUBLIC;
388 p->conn_addr = *controller_get_interface()->get_address();
389 }
390 }
391 #endif
392 }
393