keys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* keys.c - Bluetooth key handling */
  2. /*
  3. * Copyright (c) 2015-2016 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include "base/atomic.h"
  11. #include "base/common.h"
  12. #include "keys.h"
  13. #include <bluetooth/bluetooth.h>
  14. #include <bluetooth/conn.h>
  15. #include <bluetooth/hci.h>
  16. #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_KEYS)
  17. #define LOG_MODULE_NAME bt_keys
  18. #include "logging/bt_log.h"
  19. #include "common/rpa.h"
  20. #include "gatt_internal.h"
  21. #include "hci_core.h"
  22. #include "smp.h"
  23. #include "common/bt_storage_kv.h"
  24. #if defined(CONFIG_BT_SMP)
  25. static struct bt_keys key_pool[CONFIG_BT_MAX_PAIRED];
  26. #define BT_KEYS_STORAGE_LEN_COMPAT (BT_KEYS_STORAGE_LEN - sizeof(uint32_t))
  27. #if IS_ENABLED(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
  28. static uint32_t aging_counter_val;
  29. static struct bt_keys *last_keys_updated;
  30. #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
  31. #define BT_SETTINGS_KEY_MAX (0x10)
  32. #define BT_KEYS_LIST_INFO_MAGIC_INFO (0xaabb)
  33. struct bt_storage_kv_key_list_item
  34. {
  35. uint8_t id;
  36. uint8_t index;
  37. bt_addr_le_t addr;
  38. };
  39. struct bt_storage_kv_key_list_header
  40. {
  41. uint16_t magic;
  42. uint16_t cnt;
  43. struct bt_storage_kv_key_list_item items[BT_SETTINGS_KEY_MAX];
  44. };
  45. void bt_storage_kv_init_key_list_info(struct bt_storage_kv_key_list_header *list_info)
  46. {
  47. list_info->magic = BT_KEYS_LIST_INFO_MAGIC_INFO;
  48. list_info->cnt = 0;
  49. }
  50. void bt_storage_kv_get_key_list_info(struct bt_storage_kv_key_list_header *list_info)
  51. {
  52. uint16_t len = sizeof(struct bt_storage_kv_key_list_header);
  53. int ret = bt_storage_kv_get(KEY_INDEX_LE_KEY_INFO_LIST, (uint8_t *)list_info, &len);
  54. if ((ret < 0) || (len != sizeof(struct bt_storage_kv_key_list_header)) ||
  55. (list_info->magic != BT_KEYS_LIST_INFO_MAGIC_INFO))
  56. {
  57. bt_storage_kv_init_key_list_info(list_info);
  58. }
  59. }
  60. int bt_storage_kv_get_key_list_info_pos(struct bt_storage_kv_key_list_header *list_info, uint8_t id,
  61. const bt_addr_le_t *addr)
  62. {
  63. int pos = -1;
  64. for (int i = 0; i < list_info->cnt; i++)
  65. {
  66. struct bt_storage_kv_key_list_item *item = &list_info->items[i];
  67. if (id == item->id && !bt_addr_le_cmp(&item->addr, addr))
  68. {
  69. pos = i;
  70. break;
  71. }
  72. }
  73. return pos;
  74. }
  75. int bt_storage_kv_get_key_list_info_index(struct bt_storage_kv_key_list_header *list_info,
  76. uint8_t id, const bt_addr_le_t *addr)
  77. {
  78. int select_index = -1;
  79. int pos = bt_storage_kv_get_key_list_info_pos(list_info, id, addr);
  80. if (pos >= 0)
  81. {
  82. select_index = list_info->items[pos].index;
  83. }
  84. return select_index;
  85. }
  86. void bt_storage_kv_set_key_list_info_append(struct bt_storage_kv_key_list_header *list_info,
  87. uint8_t id, const bt_addr_le_t *addr, uint8_t index)
  88. {
  89. // TODO: check the old one?
  90. uint8_t store_index = list_info->cnt;
  91. if (store_index >= BT_SETTINGS_KEY_MAX)
  92. {
  93. store_index = 0;
  94. }
  95. else
  96. {
  97. list_info->cnt++;
  98. }
  99. struct bt_storage_kv_key_list_item *item = &list_info->items[store_index];
  100. item->id = id;
  101. item->index = index;
  102. bt_addr_le_copy(&item->addr, addr);
  103. bt_storage_kv_set(KEY_INDEX_LE_KEY_INFO_LIST, (uint8_t *)list_info,
  104. sizeof(struct bt_storage_kv_key_list_header));
  105. }
  106. void bt_storage_kv_set_key_list_info_delete(struct bt_storage_kv_key_list_header *list_info,
  107. uint8_t id, const bt_addr_le_t *addr)
  108. {
  109. // TODO: check the old one?
  110. int pos = bt_storage_kv_get_key_list_info_pos(list_info, id, addr);
  111. if (pos > 0)
  112. {
  113. list_info->cnt--;
  114. if (list_info->cnt > 0)
  115. {
  116. for (int i = pos; i < list_info->cnt; i++)
  117. {
  118. memcpy(&list_info->items[i], &list_info->items[i + 1],
  119. sizeof(struct bt_storage_kv_key_list_item));
  120. }
  121. }
  122. bt_storage_kv_set(KEY_INDEX_LE_KEY_INFO_LIST, (uint8_t *)list_info,
  123. sizeof(struct bt_storage_kv_key_list_header));
  124. }
  125. }
  126. void bt_storage_kv_set_key_item(uint8_t index, struct bt_keys *keys)
  127. {
  128. // TODO: check the old one?
  129. BT_ASSERT(index < BT_SETTINGS_KEY_MAX);
  130. bt_storage_kv_set(KEY_INDEX_LE_KEY_INFO_ITEM(index), keys->storage_start, BT_KEYS_STORAGE_LEN);
  131. }
  132. int bt_storage_kv_get_key_item(uint8_t index, struct bt_keys *keys)
  133. {
  134. // TODO: check the old one?
  135. BT_ASSERT(index < BT_SETTINGS_KEY_MAX);
  136. uint16_t len = BT_KEYS_STORAGE_LEN;
  137. // TODO: length judge?
  138. return bt_storage_kv_get(KEY_INDEX_LE_KEY_INFO_ITEM(index), keys->storage_start, &len);
  139. }
  140. #if defined(CONFIG_BT_SETTINGS)
  141. static void bt_storage_kv_key_store(struct bt_keys *keys)
  142. {
  143. uint8_t id = keys->id;
  144. bt_addr_le_t *addr = &keys->addr;
  145. int select_index = -1;
  146. struct bt_storage_kv_key_list_header list_info;
  147. bt_storage_kv_get_key_list_info(&list_info);
  148. select_index = bt_storage_kv_get_key_list_info_index(&list_info, id, addr);
  149. if (select_index < 0)
  150. {
  151. select_index = 0;
  152. }
  153. bt_storage_kv_set_key_list_info_append(&list_info, id, addr, select_index);
  154. bt_storage_kv_set_key_item(select_index, keys);
  155. }
  156. #endif
  157. __unused
  158. static int bt_storage_kv_key_get(struct bt_keys *keys)
  159. {
  160. uint8_t id = keys->id;
  161. bt_addr_le_t *addr = &keys->addr;
  162. struct bt_storage_kv_key_list_header list_info;
  163. bt_storage_kv_get_key_list_info(&list_info);
  164. int select_index = bt_storage_kv_get_key_list_info_index(&list_info, id, addr);
  165. if (select_index < 0)
  166. {
  167. return -1;
  168. }
  169. bt_storage_kv_get_key_item(select_index, keys);
  170. return 0;
  171. }
  172. #if defined(CONFIG_BT_SETTINGS)
  173. static void bt_storage_kv_key_delete(struct bt_keys *keys)
  174. {
  175. uint8_t id = keys->id;
  176. bt_addr_le_t *addr = &keys->addr;
  177. struct bt_storage_kv_key_list_header list_info;
  178. bt_storage_kv_get_key_list_info(&list_info);
  179. bt_storage_kv_set_key_list_info_delete(&list_info, id, addr);
  180. }
  181. #endif
  182. #if IS_ENABLED(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
  183. static uint32_t aging_counter_val;
  184. static struct bt_keys *last_keys_updated;
  185. struct key_data
  186. {
  187. bool in_use;
  188. uint8_t id;
  189. };
  190. static void find_key_in_use(struct bt_conn *conn, void *data)
  191. {
  192. struct key_data *kdata = data;
  193. struct bt_keys *key;
  194. if (conn->state == BT_CONN_CONNECTED)
  195. {
  196. key = bt_keys_find_addr(conn->id, bt_conn_get_dst(conn));
  197. if (key == NULL)
  198. {
  199. return;
  200. }
  201. if (bt_addr_cmp(&key->addr.a, &key_pool[kdata->id].addr.a) == 0)
  202. {
  203. kdata->in_use = true;
  204. BT_DBG("Connected device %s is using key_pool[%d]",
  205. bt_addr_le_str(bt_conn_get_dst(conn)), kdata->id);
  206. }
  207. }
  208. }
  209. static bool key_is_in_use(uint8_t id)
  210. {
  211. struct key_data kdata = {false, id};
  212. bt_conn_foreach(BT_CONN_TYPE_ALL, find_key_in_use, &kdata);
  213. return kdata.in_use;
  214. }
  215. #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
  216. struct bt_keys *bt_keys_get_addr(uint8_t id, const bt_addr_le_t *addr)
  217. {
  218. struct bt_keys *keys;
  219. int i;
  220. size_t first_free_slot = ARRAY_SIZE(key_pool);
  221. BT_DBG("%s", bt_addr_le_str(addr));
  222. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  223. {
  224. keys = &key_pool[i];
  225. if (keys->id == id && !bt_addr_le_cmp(&keys->addr, addr))
  226. {
  227. return keys;
  228. }
  229. if (first_free_slot == ARRAY_SIZE(key_pool) && !bt_addr_le_cmp(&keys->addr, BT_ADDR_LE_ANY))
  230. {
  231. first_free_slot = i;
  232. }
  233. }
  234. #if IS_ENABLED(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
  235. if (first_free_slot == ARRAY_SIZE(key_pool))
  236. {
  237. struct bt_keys *oldest = NULL;
  238. bt_addr_le_t oldest_addr;
  239. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  240. {
  241. struct bt_keys *current = &key_pool[i];
  242. bool key_in_use = (CONFIG_BT_MAX_CONN > 1) && key_is_in_use(i);
  243. if (key_in_use)
  244. {
  245. continue;
  246. }
  247. if ((oldest == NULL) || (current->aging_counter < oldest->aging_counter))
  248. {
  249. oldest = current;
  250. }
  251. }
  252. if (oldest == NULL)
  253. {
  254. BT_DBG("unable to create keys for %s", bt_addr_le_str(addr));
  255. return NULL;
  256. }
  257. /* Use a copy as bt_unpair will clear the oldest key. */
  258. bt_addr_le_copy(&oldest_addr, &oldest->addr);
  259. bt_unpair(oldest->id, &oldest_addr);
  260. if (!bt_addr_le_cmp(&oldest->addr, BT_ADDR_LE_ANY))
  261. {
  262. first_free_slot = oldest - &key_pool[0];
  263. }
  264. }
  265. #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
  266. if (first_free_slot < ARRAY_SIZE(key_pool))
  267. {
  268. keys = &key_pool[first_free_slot];
  269. keys->id = id;
  270. bt_addr_le_copy(&keys->addr, addr);
  271. #if IS_ENABLED(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
  272. keys->aging_counter = ++aging_counter_val;
  273. last_keys_updated = keys;
  274. #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
  275. BT_DBG("created %p for %s", keys, bt_addr_le_str(addr));
  276. return keys;
  277. }
  278. BT_DBG("unable to create keys for %s", bt_addr_le_str(addr));
  279. return NULL;
  280. }
  281. void bt_foreach_bond(uint8_t id, void (*func)(const struct bt_bond_info *info, void *user_data),
  282. void *user_data)
  283. {
  284. int i;
  285. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  286. {
  287. struct bt_keys *keys = &key_pool[i];
  288. if (keys->keys && keys->id == id)
  289. {
  290. struct bt_bond_info info;
  291. bt_addr_le_copy(&info.addr, &keys->addr);
  292. func(&info, user_data);
  293. }
  294. }
  295. }
  296. void bt_keys_foreach(int type, void (*func)(struct bt_keys *keys, void *data), void *data)
  297. {
  298. int i;
  299. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  300. {
  301. if ((key_pool[i].keys & type))
  302. {
  303. func(&key_pool[i], data);
  304. }
  305. }
  306. }
  307. struct bt_keys *bt_keys_find(int type, uint8_t id, const bt_addr_le_t *addr)
  308. {
  309. int i;
  310. BT_DBG("type %d %s", type, bt_addr_le_str(addr));
  311. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  312. {
  313. if ((key_pool[i].keys & type) && key_pool[i].id == id &&
  314. !bt_addr_le_cmp(&key_pool[i].addr, addr))
  315. {
  316. return &key_pool[i];
  317. }
  318. }
  319. return NULL;
  320. }
  321. struct bt_keys *bt_keys_get_type(int type, uint8_t id, const bt_addr_le_t *addr)
  322. {
  323. struct bt_keys *keys;
  324. BT_DBG("type %d %s", type, bt_addr_le_str(addr));
  325. keys = bt_keys_find(type, id, addr);
  326. if (keys)
  327. {
  328. return keys;
  329. }
  330. keys = bt_keys_get_addr(id, addr);
  331. if (!keys)
  332. {
  333. return NULL;
  334. }
  335. bt_keys_add_type(keys, type);
  336. return keys;
  337. }
  338. struct bt_keys *bt_keys_find_irk(uint8_t id, const bt_addr_le_t *addr)
  339. {
  340. int i;
  341. BT_DBG("%s", bt_addr_le_str(addr));
  342. if (!bt_addr_le_is_rpa(addr))
  343. {
  344. return NULL;
  345. }
  346. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  347. {
  348. // BT_DBG("i: 0x%x, keys: 0x%x", i, key_pool[i].keys);
  349. if (!(key_pool[i].keys & BT_KEYS_IRK))
  350. {
  351. continue;
  352. }
  353. // BT_DBG("id: %d, id1: %d, rpa: %s", id
  354. // , key_pool[i].id, bt_addr_str(&key_pool[i].irk.rpa));
  355. if (key_pool[i].id == id && !bt_addr_cmp(&addr->a, &key_pool[i].irk.rpa))
  356. {
  357. BT_DBG("cached RPA %s for %s", bt_addr_str(&key_pool[i].irk.rpa),
  358. bt_addr_le_str(&key_pool[i].addr));
  359. return &key_pool[i];
  360. }
  361. }
  362. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  363. {
  364. // BT_DBG("i: 0x%x, keys: 0x%x", i, key_pool[i].keys);
  365. if (!(key_pool[i].keys & BT_KEYS_IRK))
  366. {
  367. continue;
  368. }
  369. if (key_pool[i].id != id)
  370. {
  371. continue;
  372. }
  373. if (bt_rpa_irk_matches(key_pool[i].irk.val, &addr->a))
  374. {
  375. BT_DBG("RPA %s matches %s", bt_addr_str(&key_pool[i].irk.rpa),
  376. bt_addr_le_str(&key_pool[i].addr));
  377. bt_addr_copy(&key_pool[i].irk.rpa, &addr->a);
  378. return &key_pool[i];
  379. }
  380. }
  381. BT_DBG("No IRK for %s", bt_addr_le_str(addr));
  382. return NULL;
  383. }
  384. struct bt_keys *bt_keys_find_addr(uint8_t id, const bt_addr_le_t *addr)
  385. {
  386. int i;
  387. BT_DBG("%s", bt_addr_le_str(addr));
  388. for (i = 0; i < ARRAY_SIZE(key_pool); i++)
  389. {
  390. if (key_pool[i].id == id && !bt_addr_le_cmp(&key_pool[i].addr, addr))
  391. {
  392. return &key_pool[i];
  393. }
  394. }
  395. return NULL;
  396. }
  397. void bt_keys_add_type(struct bt_keys *keys, int type)
  398. {
  399. keys->keys |= type;
  400. }
  401. void bt_keys_clear(struct bt_keys *keys)
  402. {
  403. BT_DBG("%s (keys 0x%04x)", bt_addr_le_str(&keys->addr), keys->keys);
  404. if (keys->state & BT_KEYS_ID_ADDED)
  405. {
  406. // bt_id_del(keys);
  407. }
  408. #if defined(CONFIG_BT_SETTINGS)
  409. // BT_DBG("Deleting key %s", key);
  410. bt_storage_kv_key_delete(keys);
  411. #endif
  412. (void)memset(keys, 0, sizeof(*keys));
  413. }
  414. #if defined(CONFIG_BT_SETTINGS)
  415. int bt_keys_store(struct bt_keys *keys)
  416. {
  417. bt_storage_kv_key_store(keys);
  418. BT_DBG("Stored keys for %s", bt_addr_le_str(&keys->addr));
  419. return 0;
  420. }
  421. __unused
  422. static void id_add(struct bt_keys *keys, void *user_data)
  423. {
  424. // bt_id_add(keys);
  425. }
  426. int bt_keys_loading(void)
  427. {
  428. char addr_str[BT_ADDR_LE_STR_LEN];
  429. uint8_t id;
  430. bt_addr_le_t *addr;
  431. int index;
  432. struct bt_keys *keys;
  433. struct bt_storage_kv_key_list_header list_info;
  434. bt_storage_kv_get_key_list_info(&list_info);
  435. BT_INFO("Load key info, total cnt: %d", list_info.cnt);
  436. for (int i = 0; i < list_info.cnt; i++)
  437. {
  438. id = list_info.items[i].id;
  439. addr = &list_info.items[i].addr;
  440. index = list_info.items[i].index;
  441. bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
  442. BT_INFO("Load list key info, id: %d, addr: %s, index: %d", id, addr_str, index);
  443. keys = bt_keys_get_addr(id, addr);
  444. if (!keys)
  445. {
  446. BT_ERR("Failed to allocate keys for %s", bt_addr_le_str(addr));
  447. return -ENOMEM;
  448. }
  449. bt_storage_kv_get_key_item(index, keys);
  450. }
  451. return 0;
  452. }
  453. #endif /* CONFIG_BT_SETTINGS */
  454. #if IS_ENABLED(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
  455. void bt_keys_update_usage(uint8_t id, const bt_addr_le_t *addr)
  456. {
  457. struct bt_keys *keys = bt_keys_find_addr(id, addr);
  458. if (!keys)
  459. {
  460. return;
  461. }
  462. if (last_keys_updated == keys)
  463. {
  464. return;
  465. }
  466. keys->aging_counter = ++aging_counter_val;
  467. last_keys_updated = keys;
  468. BT_DBG("Aging counter for %s is set to %u", bt_addr_le_str(addr), keys->aging_counter);
  469. if (IS_ENABLED(CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING))
  470. {
  471. bt_keys_store(keys);
  472. }
  473. }
  474. #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
  475. #if defined(CONFIG_BT_LOG_SNIFFER_INFO)
  476. void bt_keys_show_sniffer_info(struct bt_keys *keys, void *data)
  477. {
  478. uint8_t ltk[16];
  479. if (keys->keys & BT_KEYS_LTK_P256)
  480. {
  481. sys_memcpy_swap(ltk, keys->ltk.val, keys->enc_size);
  482. BT_INFO("SC LTK: 0x%s", bt_hex(ltk, keys->enc_size));
  483. }
  484. #if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
  485. if (keys->keys & BT_KEYS_PERIPH_LTK)
  486. {
  487. sys_memcpy_swap(ltk, keys->periph_ltk.val, keys->enc_size);
  488. BT_INFO("Legacy LTK: 0x%s (peripheral)", bt_hex(ltk, keys->enc_size));
  489. }
  490. #endif /* !CONFIG_BT_SMP_SC_PAIR_ONLY */
  491. if (keys->keys & BT_KEYS_LTK)
  492. {
  493. sys_memcpy_swap(ltk, keys->ltk.val, keys->enc_size);
  494. BT_INFO("Legacy LTK: 0x%s (central)", bt_hex(ltk, keys->enc_size));
  495. }
  496. }
  497. #endif /* defined(CONFIG_BT_LOG_SNIFFER_INFO) */
  498. #endif /* defined(CONFIG_BT_SMP) */