id_manager.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "id_manager.h"
  13. #include <string.h>
  14. #include "nrf_soc.h"
  15. #include "ble_gap.h"
  16. #include "ble_conn_state.h"
  17. #include "peer_manager_types.h"
  18. #include "peer_database.h"
  19. #include "nordic_common.h"
  20. #include "sdk_common.h"
  21. #define IM_MAX_CONN_HANDLES 8
  22. #define IM_NO_INVALID_CONN_HANDLES 0xFF
  23. #define MAX_REGISTRANTS 3
  24. #define WHITELIST_MAX_COUNT MAX(BLE_GAP_WHITELIST_ADDR_MAX_COUNT, \
  25. BLE_GAP_WHITELIST_IRK_MAX_COUNT)
  26. #define IM_ADDR_CLEARTEXT_LENGTH 3
  27. #define IM_ADDR_CIPHERTEXT_LENGTH 3
  28. typedef struct
  29. {
  30. pm_peer_id_t peer_id;
  31. uint16_t conn_handle;
  32. ble_gap_addr_t peer_address;
  33. } im_connection_t;
  34. typedef struct
  35. {
  36. im_evt_handler_t evt_handlers[MAX_REGISTRANTS];
  37. uint8_t n_registrants;
  38. im_connection_t connections[8];
  39. pm_peer_id_t irk_whitelist_peer_ids[BLE_GAP_WHITELIST_IRK_MAX_COUNT];
  40. ble_gap_irk_t whitelist_irks[BLE_GAP_WHITELIST_IRK_MAX_COUNT];
  41. ble_gap_addr_t whitelist_addrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
  42. uint8_t n_irk_whitelist_peer_ids;
  43. ble_conn_state_user_flag_id_t conn_state_user_flag_id;
  44. } im_t;
  45. static im_t m_im = {.n_registrants = 0};
  46. #define MODULE_INITIALIZED (m_im.n_registrants > 0)
  47. #include "sdk_macros.h"
  48. static void internal_state_reset()
  49. {
  50. memset(&m_im, 0, sizeof(im_t));
  51. m_im.n_registrants = 0;
  52. m_im.n_irk_whitelist_peer_ids = 0;
  53. m_im.conn_state_user_flag_id = BLE_CONN_STATE_USER_FLAG_INVALID;
  54. for (uint32_t i = 0; i < IM_MAX_CONN_HANDLES; i++)
  55. {
  56. m_im.connections[i].conn_handle = BLE_CONN_HANDLE_INVALID;
  57. }
  58. }
  59. /**@brief Function for sending an event to all registered event handlers.
  60. *
  61. * @param[in] p_event The event to distribute.
  62. */
  63. static void evt_send(im_evt_t * p_event)
  64. {
  65. for (uint32_t i = 0; i < m_im.n_registrants; i++)
  66. {
  67. m_im.evt_handlers[i](p_event);
  68. }
  69. }
  70. /**@brief Function finding a free position in m_im.connections.
  71. *
  72. * @detail All connection handles in the m_im.connections array are checked against the connection
  73. * state module. The index of the first one that is not a connection handle for a current
  74. * connection is returned. This position in the array can safely be used for a new connection.
  75. *
  76. * @return Either the index of a free position in the array or IM_NO_INVALID_CONN_HANDLES if no free
  77. position exists.
  78. */
  79. uint8_t get_free_connection()
  80. {
  81. for (uint32_t i = 0; i < IM_MAX_CONN_HANDLES; i++)
  82. {
  83. // Query the connection state module to check if the connection handle does not belong to a
  84. // valid connection.
  85. if (!ble_conn_state_user_flag_get(m_im.connections[i].conn_handle, m_im.conn_state_user_flag_id))
  86. {
  87. return i;
  88. }
  89. }
  90. // If all connection handles belong to a valid connection, return IM_NO_INVALID_CONN_HANDLES.
  91. return IM_NO_INVALID_CONN_HANDLES;
  92. }
  93. /**@brief Function finding a particular connection handle m_im.connections.
  94. *
  95. * @param[in] conn_handle The handle to find.
  96. *
  97. * @return Either the index of the conn_handle in the array or IM_NO_INVALID_CONN_HANDLES if the
  98. * handle was not found.
  99. */
  100. uint8_t get_connection_by_conn_handle(uint16_t conn_handle)
  101. {
  102. if (ble_conn_state_user_flag_get(conn_handle, m_im.conn_state_user_flag_id))
  103. {
  104. for (uint32_t i = 0; i < IM_MAX_CONN_HANDLES; i++)
  105. {
  106. if (m_im.connections[i].conn_handle == conn_handle)
  107. {
  108. return i;
  109. }
  110. }
  111. }
  112. // If all connection handles belong to a valid connection, return IM_NO_INVALID_CONN_HANDLES.
  113. return IM_NO_INVALID_CONN_HANDLES;
  114. }
  115. /**@brief Function for registering a new connection instance.
  116. *
  117. * @param[in] conn_handle The handle of the new connection.
  118. * @param[in] p_ble_addr The address used to connect.
  119. *
  120. * @return Either the index of the new connection in the array or IM_NO_INVALID_CONN_HANDLES if no
  121. * free position exists.
  122. */
  123. uint8_t new_connection(uint16_t conn_handle, ble_gap_addr_t * p_ble_addr)
  124. {
  125. uint8_t conn_index = IM_NO_INVALID_CONN_HANDLES;
  126. if ((p_ble_addr != NULL) && (conn_handle != BLE_CONN_HANDLE_INVALID))
  127. {
  128. ble_conn_state_user_flag_set(conn_handle, m_im.conn_state_user_flag_id, true);
  129. conn_index = get_connection_by_conn_handle(conn_handle);
  130. if (conn_index == IM_NO_INVALID_CONN_HANDLES)
  131. {
  132. conn_index = get_free_connection();
  133. }
  134. if (conn_index != IM_NO_INVALID_CONN_HANDLES)
  135. {
  136. m_im.connections[conn_index].conn_handle = conn_handle;
  137. m_im.connections[conn_index].peer_id = PM_PEER_ID_INVALID;
  138. m_im.connections[conn_index].peer_address = *p_ble_addr;
  139. }
  140. }
  141. return conn_index;
  142. }
  143. /**@brief Function checking the validity of an IRK
  144. *
  145. * @detail An all-zero IRK is not valid. This function will check if a given IRK is valid.
  146. *
  147. * @param[in] irk The IRK for which the validity is going to be checked.
  148. *
  149. * @retval true The IRK is valid.
  150. * @retval false The IRK is invalid.
  151. */
  152. bool is_valid_irk(ble_gap_irk_t const * irk)
  153. {
  154. for (uint32_t i = 0; i < BLE_GAP_SEC_KEY_LEN; i++)
  155. {
  156. if (irk->irk[i] != 0)
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /**@brief Function for comparing two addresses to determine if they are identical
  164. *
  165. * @note The address type need to be identical, as well as every bit in the address itself.
  166. *
  167. * @param[in] p_addr1 The first address to be compared.
  168. * @param[in] p_addr2 The second address to be compared.
  169. *
  170. * @retval true The addresses are identical.
  171. * @retval false The addresses are not identical.
  172. */
  173. bool addr_compare(ble_gap_addr_t const * p_addr1, ble_gap_addr_t const * p_addr2)
  174. {
  175. if ((p_addr1 == NULL) || (p_addr2 == NULL))
  176. {
  177. return false;
  178. }
  179. // Check that the addr type is identical, return false if it is not
  180. if (p_addr1->addr_type != p_addr2->addr_type)
  181. {
  182. return false;
  183. }
  184. // Check if the addr bytes are is identical
  185. return (memcmp(p_addr1->addr, p_addr2->addr, BLE_GAP_ADDR_LEN) == 0);
  186. }
  187. void im_ble_evt_handler(ble_evt_t * ble_evt)
  188. {
  189. ret_code_t err_code;
  190. switch (ble_evt->header.evt_id)
  191. {
  192. case BLE_GAP_EVT_CONNECTED:
  193. {
  194. pm_peer_id_t bonded_matching_peer_id = PM_PEER_ID_INVALID;
  195. if (ble_evt->evt.gap_evt.params.connected.irk_match == 1)
  196. {
  197. // The peer was matched using a whitelist.
  198. bonded_matching_peer_id
  199. = m_im.irk_whitelist_peer_ids[ble_evt->evt.gap_evt.params.connected.irk_match_idx];
  200. }
  201. else if ( ble_evt->evt.gap_evt.params.connected.peer_addr.addr_type
  202. != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE)
  203. {
  204. /* Search the database for bonding data matching the one that triggered the event.
  205. * Public and static addresses can be matched on address alone, while resolvable
  206. * random addresses can be resolved agains known IRKs. Non-resolvable random addresses
  207. * are never matching because they are not longterm form of identification.
  208. */
  209. pm_peer_id_t compared_peer_id = pdb_next_peer_id_get(PM_PEER_ID_INVALID);
  210. while ( (compared_peer_id != PM_PEER_ID_INVALID)
  211. && (bonded_matching_peer_id == PM_PEER_ID_INVALID))
  212. {
  213. pm_peer_data_flash_t compared_data;
  214. switch (ble_evt->evt.gap_evt.params.connected.peer_addr.addr_type)
  215. {
  216. case BLE_GAP_ADDR_TYPE_PUBLIC:
  217. /* fall-through */
  218. case BLE_GAP_ADDR_TYPE_RANDOM_STATIC:
  219. err_code = pdb_read_buf_get(compared_peer_id,
  220. PM_PEER_DATA_ID_BONDING,
  221. &compared_data,
  222. NULL);
  223. if ((err_code == NRF_SUCCESS) &&
  224. addr_compare(&ble_evt->evt.gap_evt.params.connected.peer_addr,
  225. &compared_data.p_bonding_data->peer_id.id_addr_info)
  226. )
  227. {
  228. bonded_matching_peer_id = compared_peer_id;
  229. }
  230. break;
  231. case BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE:
  232. err_code = pdb_read_buf_get(compared_peer_id,
  233. PM_PEER_DATA_ID_BONDING,
  234. &compared_data,
  235. NULL);
  236. if (err_code == NRF_SUCCESS &&
  237. im_address_resolve(&ble_evt->evt.gap_evt.params.connected.peer_addr,
  238. &compared_data.p_bonding_data->peer_id.id_info)
  239. )
  240. {
  241. bonded_matching_peer_id = compared_peer_id;
  242. }
  243. break;
  244. case BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE:
  245. // Should not happen.
  246. break;
  247. default:
  248. break;
  249. }
  250. compared_peer_id = pdb_next_peer_id_get(compared_peer_id);
  251. }
  252. }
  253. uint8_t new_index = new_connection(ble_evt->evt.gap_evt.conn_handle, &ble_evt->evt.gap_evt.params.connected.peer_addr);
  254. UNUSED_VARIABLE(new_index);
  255. if (bonded_matching_peer_id != PM_PEER_ID_INVALID)
  256. {
  257. im_new_peer_id(ble_evt->evt.gap_evt.conn_handle, bonded_matching_peer_id);
  258. // Send a bonded peer event
  259. im_evt_t im_evt;
  260. im_evt.conn_handle = ble_evt->evt.gap_evt.conn_handle;
  261. im_evt.evt_id = IM_EVT_BONDED_PEER_CONNECTED;
  262. evt_send(&im_evt);
  263. }
  264. }
  265. }
  266. }
  267. /**@brief Function to compare two sets of bonding data to check if they belong to the same device.
  268. * @note Invalid irks will never match even though they are identical.
  269. *
  270. * @param[in] p_bonding_data1 First bonding data for comparison
  271. * @param[in] p_bonding_data2 Second bonding data for comparison
  272. *
  273. * @return True if the input matches, false if it does not.
  274. */
  275. bool is_duplicate_bonding_data(pm_peer_data_bonding_t const * p_bonding_data1,
  276. pm_peer_data_bonding_t const * p_bonding_data2)
  277. {
  278. bool valid_irk = is_valid_irk(&p_bonding_data1->peer_id.id_info);
  279. bool duplicate_irk = valid_irk &&
  280. (memcmp(p_bonding_data1->peer_id.id_info.irk,
  281. p_bonding_data2->peer_id.id_info.irk,
  282. BLE_GAP_SEC_KEY_LEN) == 0
  283. );
  284. bool duplicate_addr = addr_compare(&p_bonding_data1->peer_id.id_addr_info,
  285. &p_bonding_data2->peer_id.id_addr_info
  286. );
  287. return duplicate_irk || duplicate_addr;
  288. }
  289. /**@brief Event handler for events from the peer_database module.
  290. *
  291. * @param[in] p_event The event that has happend with peer id and flags.
  292. */
  293. static void pdb_evt_handler(pdb_evt_t const * p_event)
  294. {
  295. ret_code_t err_code;
  296. if ((p_event != NULL) && (p_event->evt_id == PDB_EVT_WRITE_BUF_STORED))
  297. {
  298. // If new data about peer id has been stored it is compared to other peers peer ids in
  299. // search of duplicates.
  300. if (p_event->data_id == PM_PEER_DATA_ID_BONDING)
  301. {
  302. pm_peer_data_flash_t written_data;
  303. err_code = pdb_read_buf_get(p_event->peer_id, PM_PEER_DATA_ID_BONDING, &written_data, NULL);
  304. if (err_code == NRF_SUCCESS)
  305. {
  306. pm_peer_id_t compared_peer_id = pdb_next_peer_id_get(PM_PEER_ID_INVALID);
  307. while (compared_peer_id != PM_PEER_ID_INVALID)
  308. {
  309. pm_peer_data_flash_t compared_data;
  310. err_code = pdb_read_buf_get(compared_peer_id,
  311. PM_PEER_DATA_ID_BONDING,
  312. &compared_data,
  313. NULL);
  314. if ( err_code == NRF_SUCCESS &&
  315. p_event->peer_id != compared_peer_id &&
  316. is_duplicate_bonding_data(written_data.p_bonding_data,
  317. compared_data.p_bonding_data)
  318. )
  319. {
  320. im_evt_t im_evt;
  321. im_evt.conn_handle = im_conn_handle_get(p_event->peer_id);
  322. im_evt.evt_id = IM_EVT_DUPLICATE_ID;
  323. im_evt.params.duplicate_id.peer_id_1 = p_event->peer_id;
  324. im_evt.params.duplicate_id.peer_id_2 = compared_peer_id;
  325. evt_send(&im_evt);
  326. }
  327. compared_peer_id = pdb_next_peer_id_get(compared_peer_id);
  328. }
  329. }
  330. }
  331. }
  332. }
  333. ret_code_t im_register(im_evt_handler_t evt_handler)
  334. {
  335. VERIFY_PARAM_NOT_NULL(evt_handler);
  336. ret_code_t err_code = NRF_SUCCESS;
  337. if (!MODULE_INITIALIZED)
  338. {
  339. internal_state_reset();
  340. m_im.conn_state_user_flag_id = ble_conn_state_user_flag_acquire();
  341. if (m_im.conn_state_user_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID)
  342. {
  343. err_code = NRF_ERROR_NO_MEM;
  344. }
  345. else
  346. {
  347. err_code = pdb_register(pdb_evt_handler);
  348. }
  349. }
  350. if (err_code == NRF_SUCCESS)
  351. {
  352. if ((m_im.n_registrants < MAX_REGISTRANTS))
  353. {
  354. m_im.evt_handlers[m_im.n_registrants++] = evt_handler;
  355. }
  356. else
  357. {
  358. err_code = NRF_ERROR_NO_MEM;
  359. }
  360. }
  361. return err_code;
  362. }
  363. pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle)
  364. {
  365. uint8_t conn_index = get_connection_by_conn_handle(conn_handle);
  366. if (MODULE_INITIALIZED && (conn_index != IM_NO_INVALID_CONN_HANDLES))
  367. {
  368. return m_im.connections[conn_index].peer_id;
  369. }
  370. return PM_PEER_ID_INVALID;
  371. }
  372. ret_code_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t * p_ble_addr)
  373. {
  374. VERIFY_MODULE_INITIALIZED();
  375. VERIFY_PARAM_NOT_NULL(p_ble_addr);
  376. uint8_t conn_index = get_connection_by_conn_handle(conn_handle);
  377. if (conn_index != IM_NO_INVALID_CONN_HANDLES)
  378. {
  379. *p_ble_addr = m_im.connections[conn_index].peer_address;
  380. return NRF_SUCCESS;
  381. }
  382. return NRF_ERROR_NOT_FOUND;
  383. }
  384. bool im_master_ids_compare(ble_gap_master_id_t const * p_master_id1,
  385. ble_gap_master_id_t const * p_master_id2)
  386. {
  387. if(!im_master_id_is_valid(p_master_id1))
  388. {
  389. return false;
  390. }
  391. if (p_master_id1->ediv != p_master_id2->ediv)
  392. {
  393. return false;
  394. }
  395. return (memcmp(p_master_id1->rand, p_master_id2->rand, BLE_GAP_SEC_RAND_LEN) == 0);
  396. }
  397. pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t * p_master_id)
  398. {
  399. ret_code_t err_code;
  400. // For each stored peer, check if the master_id match p_master_id
  401. pm_peer_id_t compared_peer_id = pdb_next_peer_id_get(PM_PEER_ID_INVALID);
  402. while (compared_peer_id != PM_PEER_ID_INVALID)
  403. {
  404. pm_peer_data_flash_t compared_data;
  405. ble_gap_master_id_t const * p_compared_master_id;
  406. err_code = pdb_read_buf_get(compared_peer_id, PM_PEER_DATA_ID_BONDING, &compared_data, NULL);
  407. if (err_code == NRF_SUCCESS)
  408. {
  409. p_compared_master_id = &compared_data.p_bonding_data->own_ltk.master_id;
  410. if (im_master_ids_compare(p_master_id, p_compared_master_id))
  411. {
  412. // If a matching master_id is found return the peer_id
  413. return compared_peer_id;
  414. }
  415. p_compared_master_id = &compared_data.p_bonding_data->peer_ltk.master_id;
  416. if (im_master_ids_compare(p_master_id, p_compared_master_id))
  417. {
  418. // If a matching master_id is found return the peer_id
  419. return compared_peer_id;
  420. }
  421. }
  422. compared_peer_id = pdb_next_peer_id_get(compared_peer_id);
  423. }
  424. // If no matching master_id is found return the PM_PEER_ID_INVALID
  425. return PM_PEER_ID_INVALID;
  426. }
  427. pm_peer_id_t im_peer_id_get_by_irk_match_idx(uint8_t irk_match_idx)
  428. {
  429. // Verify that the requested idx is within the list
  430. if (irk_match_idx < m_im.n_irk_whitelist_peer_ids)
  431. {
  432. // Return the peer_id from the white list
  433. return m_im.irk_whitelist_peer_ids[irk_match_idx];
  434. }
  435. else
  436. {
  437. // Return PM_PEER_ID_INVALID to indicate that there was no peer with the requested idx
  438. return PM_PEER_ID_INVALID;
  439. }
  440. }
  441. uint16_t im_conn_handle_get(pm_peer_id_t peer_id)
  442. {
  443. for (uint32_t i = 0; i < IM_MAX_CONN_HANDLES; i++)
  444. {
  445. if (peer_id == m_im.connections[i].peer_id)
  446. {
  447. return m_im.connections[i].conn_handle;
  448. }
  449. }
  450. return BLE_CONN_HANDLE_INVALID;
  451. }
  452. bool im_master_id_is_valid(ble_gap_master_id_t const * p_master_id)
  453. {
  454. if (p_master_id->ediv != 0)
  455. {
  456. return true;
  457. }
  458. for (uint32_t i = 0; i < BLE_GAP_SEC_RAND_LEN; i++)
  459. {
  460. if (p_master_id->rand[i] != 0)
  461. {
  462. return true;
  463. }
  464. }
  465. return false;
  466. }
  467. /**@brief Function to set the peer ID associated with a connection handle.
  468. *
  469. * @param[in] conn_handle The connection handle.
  470. * @param[in] peer_id The peer ID to associate with @c conn_handle.
  471. */
  472. static void peer_id_set(uint16_t conn_handle, pm_peer_id_t peer_id)
  473. {
  474. uint8_t conn_index = get_connection_by_conn_handle(conn_handle);
  475. if (conn_index != IM_NO_INVALID_CONN_HANDLES)
  476. {
  477. m_im.connections[conn_index].peer_id = peer_id;
  478. }
  479. }
  480. void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id)
  481. {
  482. peer_id_set(conn_handle, peer_id);
  483. }
  484. ret_code_t im_peer_free(pm_peer_id_t peer_id)
  485. {
  486. VERIFY_MODULE_INITIALIZED();
  487. uint16_t conn_handle = im_conn_handle_get(peer_id);
  488. ret_code_t err_code = pdb_peer_free(peer_id);
  489. if ((conn_handle != BLE_CONN_HANDLE_INVALID) && (err_code == NRF_SUCCESS))
  490. {
  491. peer_id_set(conn_handle, PM_PEER_ID_INVALID);
  492. }
  493. return err_code;
  494. }
  495. ret_code_t im_whitelist_create(pm_peer_id_t * p_peer_ids,
  496. uint8_t n_peer_ids,
  497. ble_gap_whitelist_t * p_whitelist)
  498. {
  499. VERIFY_MODULE_INITIALIZED();
  500. VERIFY_PARAM_NOT_NULL(p_whitelist);
  501. ret_code_t err_code;
  502. p_whitelist->addr_count = 0;
  503. p_whitelist->irk_count = 0;
  504. m_im.n_irk_whitelist_peer_ids = 0;
  505. for (uint32_t peer_index = 0; peer_index < n_peer_ids; peer_index++)
  506. {
  507. uint16_t conn_handle = im_conn_handle_get(p_peer_ids[peer_index]);
  508. if (ble_conn_state_status(conn_handle) != BLE_CONN_STATUS_CONNECTED)
  509. {
  510. pm_peer_data_flash_t peer_data;
  511. err_code = pdb_read_buf_get(p_peer_ids[peer_index], PM_PEER_DATA_ID_BONDING, &peer_data, NULL);
  512. if (err_code == NRF_ERROR_INVALID_PARAM || err_code == NRF_ERROR_NOT_FOUND)
  513. {
  514. return NRF_ERROR_INVALID_PARAM;
  515. }
  516. if (p_whitelist->pp_addrs != NULL &&
  517. peer_data.p_bonding_data->peer_id.id_addr_info.addr_type
  518. != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE &&
  519. peer_data.p_bonding_data->peer_id.id_addr_info.addr_type
  520. != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
  521. )
  522. {
  523. memcpy(m_im.whitelist_addrs[peer_index].addr,
  524. peer_data.p_bonding_data->peer_id.id_addr_info.addr,
  525. BLE_GAP_ADDR_LEN
  526. );
  527. m_im.whitelist_addrs[peer_index].addr_type =
  528. peer_data.p_bonding_data->peer_id.id_addr_info.addr_type;
  529. p_whitelist->pp_addrs[peer_index] = &m_im.whitelist_addrs[peer_index];
  530. p_whitelist->addr_count++;
  531. }
  532. if (p_whitelist->pp_irks != NULL &&
  533. is_valid_irk(&(peer_data.p_bonding_data->peer_id.id_info))
  534. )
  535. {
  536. memcpy(m_im.whitelist_irks[peer_index].irk,
  537. peer_data.p_bonding_data->peer_id.id_info.irk,
  538. BLE_GAP_SEC_KEY_LEN
  539. );
  540. p_whitelist->pp_irks[peer_index] = &m_im.whitelist_irks[peer_index];
  541. p_whitelist->irk_count++;
  542. m_im.irk_whitelist_peer_ids[peer_index] = p_peer_ids[peer_index];
  543. m_im.n_irk_whitelist_peer_ids++;
  544. }
  545. }
  546. }
  547. return NRF_SUCCESS;
  548. }
  549. ret_code_t im_whitelist_custom(ble_gap_whitelist_t const * p_whitelist)
  550. {
  551. ret_code_t err_code;
  552. pm_peer_id_t new_irk_whitelist_peer_ids[BLE_GAP_WHITELIST_IRK_MAX_COUNT];
  553. uint32_t n_new_irk_whitelist_peer_ids = 0;
  554. VERIFY_PARAM_NOT_NULL(p_whitelist);
  555. for (uint32_t i = 0; i < BLE_GAP_WHITELIST_IRK_MAX_COUNT; i++)
  556. {
  557. new_irk_whitelist_peer_ids[i] = PM_PEER_ID_INVALID;
  558. }
  559. pm_peer_id_t compared_peer_id = pdb_next_peer_id_get(PM_PEER_ID_INVALID);
  560. while (compared_peer_id != PM_PEER_ID_INVALID)
  561. {
  562. pm_peer_data_flash_t compared_data;
  563. err_code = pdb_read_buf_get(compared_peer_id, PM_PEER_DATA_ID_BONDING, &compared_data, NULL);
  564. if (err_code == NRF_SUCCESS)
  565. {
  566. for (uint32_t i = 0; i < p_whitelist->irk_count; i++)
  567. {
  568. bool valid_irk = is_valid_irk(&compared_data.p_bonding_data->peer_id.id_info);
  569. bool duplicate_irk = valid_irk &&
  570. (memcmp(p_whitelist->pp_irks[i]->irk,
  571. compared_data.p_bonding_data->peer_id.id_info.irk,
  572. BLE_GAP_SEC_KEY_LEN) == 0
  573. );
  574. if (duplicate_irk)
  575. {
  576. new_irk_whitelist_peer_ids[i] = compared_peer_id;
  577. n_new_irk_whitelist_peer_ids++;
  578. }
  579. }
  580. }
  581. compared_peer_id = pdb_next_peer_id_get(compared_peer_id);
  582. }
  583. if (n_new_irk_whitelist_peer_ids != p_whitelist->irk_count)
  584. {
  585. return NRF_ERROR_NOT_FOUND;
  586. }
  587. else
  588. {
  589. for (uint32_t i = 0; i < n_new_irk_whitelist_peer_ids; i++)
  590. {
  591. m_im.irk_whitelist_peer_ids[i] = new_irk_whitelist_peer_ids[i];
  592. }
  593. m_im.n_irk_whitelist_peer_ids = n_new_irk_whitelist_peer_ids;
  594. return NRF_SUCCESS;
  595. }
  596. }
  597. /**@brief Function for calculating the ah() hash function described in Bluetooth core specification
  598. * 4.2 section 3.H.2.2.2.
  599. *
  600. * @detail BLE uses a hash function to calculate the first half of a resolvable address
  601. * from the second half of the address and an irk. This function will use the ECB
  602. * periferal to hash these data acording to the Bluetooth core specification.
  603. *
  604. * @note The ECB expect little endian input and output.
  605. * This function expect big endian and will reverse the data as necessary.
  606. *
  607. * @param[in] p_k The key used in the hash function.
  608. * For address resolution this is should be the irk.
  609. * The array must have a length of 16.
  610. * @param[in] p_r The rand used in the hash function. For generating a new address
  611. * this would be a random number. For resolving a resolvable address
  612. * this would be the last half of the address being resolved.
  613. * The array must have a length of 3.
  614. * @param[out] p_local_hash The result of the hash operation. For address resolution this
  615. * will match the first half of the address being resolved if and only
  616. * if the irk used in the hash function is the same one used to generate
  617. * the address.
  618. * The array must have a length of 16.
  619. */
  620. void ah(uint8_t const * p_k, uint8_t const * p_r, uint8_t * p_local_hash)
  621. {
  622. ret_code_t err_code;
  623. nrf_ecb_hal_data_t ecb_hal_data;
  624. for (uint32_t i = 0; i < SOC_ECB_KEY_LENGTH; i++)
  625. {
  626. ecb_hal_data.key[i] = p_k[SOC_ECB_KEY_LENGTH - 1 - i];
  627. }
  628. memset(ecb_hal_data.cleartext, 0, SOC_ECB_KEY_LENGTH - IM_ADDR_CLEARTEXT_LENGTH);
  629. for (uint32_t i = 0; i < IM_ADDR_CLEARTEXT_LENGTH; i++)
  630. {
  631. ecb_hal_data.cleartext[SOC_ECB_KEY_LENGTH - 1 - i] = p_r[i];
  632. }
  633. err_code = sd_ecb_block_encrypt(&ecb_hal_data); // Can only return NRF_SUCCESS.
  634. UNUSED_VARIABLE(err_code);
  635. for (uint32_t i = 0; i < IM_ADDR_CIPHERTEXT_LENGTH; i++)
  636. {
  637. p_local_hash[i] = ecb_hal_data.ciphertext[SOC_ECB_KEY_LENGTH - 1 - i];
  638. }
  639. }
  640. bool im_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk)
  641. {
  642. if (p_addr->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE)
  643. {
  644. return false;
  645. }
  646. uint8_t hash[IM_ADDR_CIPHERTEXT_LENGTH];
  647. uint8_t local_hash[IM_ADDR_CIPHERTEXT_LENGTH];
  648. uint8_t prand[IM_ADDR_CLEARTEXT_LENGTH];
  649. memcpy(hash, p_addr->addr, IM_ADDR_CIPHERTEXT_LENGTH);
  650. memcpy(prand, &p_addr->addr[IM_ADDR_CIPHERTEXT_LENGTH], IM_ADDR_CLEARTEXT_LENGTH);
  651. ah(p_irk->irk, prand, local_hash);
  652. return (memcmp(hash, local_hash, IM_ADDR_CIPHERTEXT_LENGTH) == 0);
  653. }