bt_keys.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /****************************************************************************
  2. * wireless/bluetooth/bt_keys.c
  3. * Bluetooth key handling
  4. *
  5. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Ported from the Intel/Zephyr arduino101_firmware_source-v1.tar package
  9. * where the code was released with a compatible 3-clause BSD license:
  10. *
  11. * Copyright (c) 2016, Intel Corporation
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are
  16. * met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * 3. Neither the name of the copyright holder nor the names of its
  26. * contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  31. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS
  36. * ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  37. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  38. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  39. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. ****************************************************************************/
  42. /****************************************************************************
  43. * Included Files
  44. ****************************************************************************/
  45. #include <nuttx/config.h>
  46. #include <string.h>
  47. #include <debug.h>
  48. #include <nuttx/wireless/bluetooth/bt_core.h>
  49. #include <nuttx/wireless/bluetooth/bt_hci.h>
  50. #include "bt_hcicore.h"
  51. #include "bt_smp.h"
  52. #include "bt_keys.h"
  53. /****************************************************************************
  54. * Pre-processor Definitions
  55. ****************************************************************************/
  56. #define bt_keys_foreach(list, cur, member) \
  57. for (cur = list; *cur; cur = &(*cur)->member)
  58. /****************************************************************************
  59. * Private Data
  60. ****************************************************************************/
  61. static struct bt_keys_s g_key_pool[CONFIG_BLUETOOTH_MAX_PAIRED];
  62. static FAR struct bt_keys_s *g_ltks;
  63. static FAR struct bt_keys_s *g_slave_ltks;
  64. static FAR struct bt_keys_s *g_irks;
  65. static FAR struct bt_keys_s *g_local_csrks;
  66. static FAR struct bt_keys_s *g_remote_csrks;
  67. /****************************************************************************
  68. * Public Functions
  69. ****************************************************************************/
  70. FAR struct bt_keys_s *bt_keys_get_addr(FAR const bt_addr_le_t *addr)
  71. {
  72. FAR struct bt_keys_s *keys;
  73. int i;
  74. wlinfo("%s\n", bt_addr_le_str(addr));
  75. for (i = 0; i < CONFIG_BLUETOOTH_MAX_PAIRED; i++)
  76. {
  77. keys = &g_key_pool[i];
  78. if (!bt_addr_le_cmp(&keys->addr, addr))
  79. {
  80. return keys;
  81. }
  82. if (!bt_addr_le_cmp(&keys->addr, BT_ADDR_LE_ANY))
  83. {
  84. bt_addr_le_copy(&keys->addr, addr);
  85. wlinfo("created %p for %s\n", keys, bt_addr_le_str(addr));
  86. return keys;
  87. }
  88. }
  89. wlinfo("unable to create keys for %s\n", bt_addr_le_str(addr));
  90. return NULL;
  91. }
  92. void bt_keys_clear(FAR struct bt_keys_s *keys, int type)
  93. {
  94. FAR struct bt_keys_s **cur;
  95. wlinfo("keys for %s type %d\n", bt_addr_le_str(&keys->addr), type);
  96. if (((type & keys->keys) & BT_KEYS_SLAVE_LTK))
  97. {
  98. bt_keys_foreach(&g_slave_ltks, cur, slave_ltk.next)
  99. {
  100. if (*cur == keys)
  101. {
  102. *cur = (*cur)->slave_ltk.next;
  103. break;
  104. }
  105. }
  106. keys->keys &= ~BT_KEYS_SLAVE_LTK;
  107. }
  108. if (((type & keys->keys) & BT_KEYS_LTK))
  109. {
  110. bt_keys_foreach(&g_ltks, cur, ltk.next)
  111. {
  112. if (*cur == keys)
  113. {
  114. *cur = (*cur)->ltk.next;
  115. break;
  116. }
  117. }
  118. keys->keys &= ~BT_KEYS_LTK;
  119. }
  120. if (((type & keys->keys) & BT_KEYS_IRK))
  121. {
  122. bt_keys_foreach(&g_irks, cur, irk.next)
  123. {
  124. if (*cur == keys)
  125. {
  126. *cur = (*cur)->irk.next;
  127. break;
  128. }
  129. }
  130. keys->keys &= ~BT_KEYS_IRK;
  131. }
  132. if (((type & keys->keys) & BT_KEYS_LOCAL_CSRK))
  133. {
  134. bt_keys_foreach(&g_local_csrks, cur, local_csrk.next)
  135. {
  136. if (*cur == keys)
  137. {
  138. *cur = (*cur)->local_csrk.next;
  139. break;
  140. }
  141. }
  142. keys->keys &= ~BT_KEYS_LOCAL_CSRK;
  143. }
  144. if (((type & keys->keys) & BT_KEYS_REMOTE_CSRK))
  145. {
  146. bt_keys_foreach(&g_remote_csrks, cur, remote_csrk.next)
  147. {
  148. if (*cur == keys)
  149. {
  150. *cur = (*cur)->remote_csrk.next;
  151. break;
  152. }
  153. }
  154. keys->keys &= ~BT_KEYS_REMOTE_CSRK;
  155. }
  156. if (!keys->keys)
  157. {
  158. memset(keys, 0, sizeof(*keys));
  159. }
  160. }
  161. FAR struct bt_keys_s *bt_keys_find(int type, FAR const bt_addr_le_t *addr)
  162. {
  163. FAR struct bt_keys_s **cur;
  164. wlinfo("type %d %s\n", type, bt_addr_le_str(addr));
  165. switch (type)
  166. {
  167. case BT_KEYS_SLAVE_LTK:
  168. bt_keys_foreach(&g_slave_ltks, cur, slave_ltk.next)
  169. {
  170. if (!bt_addr_le_cmp(&(*cur)->addr, addr))
  171. {
  172. break;
  173. }
  174. }
  175. return *cur;
  176. case BT_KEYS_LTK:
  177. bt_keys_foreach(&g_ltks, cur, ltk.next)
  178. {
  179. if (!bt_addr_le_cmp(&(*cur)->addr, addr))
  180. {
  181. break;
  182. }
  183. }
  184. return *cur;
  185. case BT_KEYS_IRK:
  186. bt_keys_foreach(&g_irks, cur, irk.next)
  187. {
  188. if (!bt_addr_le_cmp(&(*cur)->addr, addr))
  189. {
  190. break;
  191. }
  192. }
  193. return *cur;
  194. case BT_KEYS_LOCAL_CSRK:
  195. bt_keys_foreach(&g_local_csrks, cur, local_csrk.next)
  196. {
  197. if (!bt_addr_le_cmp(&(*cur)->addr, addr))
  198. {
  199. break;
  200. }
  201. }
  202. return *cur;
  203. case BT_KEYS_REMOTE_CSRK:
  204. bt_keys_foreach(&g_remote_csrks, cur, remote_csrk.next)
  205. {
  206. if (!bt_addr_le_cmp(&(*cur)->addr, addr))
  207. {
  208. break;
  209. }
  210. }
  211. return *cur;
  212. default:
  213. return NULL;
  214. }
  215. }
  216. void bt_keys_add_type(FAR struct bt_keys_s *keys, int type)
  217. {
  218. if ((keys->keys & type) != 0)
  219. {
  220. return;
  221. }
  222. switch (type)
  223. {
  224. case BT_KEYS_SLAVE_LTK:
  225. keys->slave_ltk.next = g_slave_ltks;
  226. g_slave_ltks = keys;
  227. break;
  228. case BT_KEYS_LTK:
  229. keys->ltk.next = g_ltks;
  230. g_ltks = keys;
  231. break;
  232. case BT_KEYS_IRK:
  233. keys->irk.next = g_irks;
  234. g_irks = keys;
  235. break;
  236. case BT_KEYS_LOCAL_CSRK:
  237. keys->local_csrk.next = g_local_csrks;
  238. g_local_csrks = keys;
  239. break;
  240. case BT_KEYS_REMOTE_CSRK:
  241. keys->remote_csrk.next = g_remote_csrks;
  242. g_remote_csrks = keys;
  243. break;
  244. default:
  245. wlerr("ERROR: Unknown key type %d\n", type);
  246. return;
  247. }
  248. keys->keys |= type;
  249. }
  250. FAR struct bt_keys_s *bt_keys_get_type(int type,
  251. FAR const bt_addr_le_t *addr)
  252. {
  253. FAR struct bt_keys_s *keys;
  254. wlinfo("type %d %s\n", type, bt_addr_le_str(addr));
  255. keys = bt_keys_find(type, addr);
  256. if (keys)
  257. {
  258. return keys;
  259. }
  260. keys = bt_keys_get_addr(addr);
  261. if (!keys)
  262. {
  263. return NULL;
  264. }
  265. bt_keys_add_type(keys, type);
  266. return keys;
  267. }
  268. FAR struct bt_keys_s *bt_keys_find_irk(FAR const bt_addr_le_t * addr)
  269. {
  270. FAR struct bt_keys_s **cur;
  271. wlinfo("%s\n", bt_addr_le_str(addr));
  272. if (!bt_addr_le_is_rpa(addr))
  273. {
  274. return NULL;
  275. }
  276. bt_keys_foreach(&g_irks, cur, irk.next)
  277. {
  278. FAR struct bt_irk_s *irk = &(*cur)->irk;
  279. if (!bt_addr_cmp((bt_addr_t *) addr->val, &irk->rpa))
  280. {
  281. wlinfo("cached RPA %s for %s\n", bt_addr_str(&irk->rpa),
  282. bt_addr_le_str(&(*cur)->addr));
  283. return *cur;
  284. }
  285. if (bt_smp_irk_matches(irk->val, (bt_addr_t *) addr->val))
  286. {
  287. FAR struct bt_keys_s *match = *cur;
  288. wlinfo("RPA %s matches %s\n", bt_addr_str(&irk->rpa),
  289. bt_addr_le_str(&(*cur)->addr));
  290. bt_addr_copy(&irk->rpa, (bt_addr_t *) addr->val);
  291. /* Move to the beginning of the list for faster future lookups. */
  292. if (match != g_irks)
  293. {
  294. /* Remove match from list */
  295. *cur = irk->next;
  296. /* Add match to the beginning */
  297. irk->next = g_irks;
  298. g_irks = match;
  299. }
  300. return match;
  301. }
  302. }
  303. wlinfo("No IRK for %s\n", bt_addr_le_str(addr));
  304. return NULL;
  305. }