btm_ble_addr.cc 15 KB

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