net-keys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <ell/ell.h>
  14. #include "mesh/mesh-defs.h"
  15. #include "mesh/util.h"
  16. #include "mesh/crypto.h"
  17. #include "mesh/mesh-io.h"
  18. #include "mesh/net.h"
  19. #include "mesh/net-keys.h"
  20. #define BEACON_TYPE_SNB 0x01
  21. #define KEY_REFRESH 0x01
  22. #define IV_INDEX_UPDATE 0x02
  23. #define BEACON_INTERVAL_MIN 10
  24. #define BEACON_INTERVAL_MAX 600
  25. struct net_beacon {
  26. struct l_timeout *timeout;
  27. uint32_t ts;
  28. uint16_t observe_period;
  29. uint16_t observed;
  30. uint16_t expected;
  31. bool half_period;
  32. uint8_t beacon[23];
  33. };
  34. struct net_key {
  35. uint32_t id;
  36. struct net_beacon snb;
  37. uint16_t ref_cnt;
  38. uint16_t beacon_enables;
  39. uint8_t friend_key;
  40. uint8_t nid;
  41. uint8_t flooding[16];
  42. uint8_t encrypt[16];
  43. uint8_t privacy[16];
  44. uint8_t beacon[16];
  45. uint8_t network[8];
  46. };
  47. static struct l_queue *keys = NULL;
  48. static uint32_t last_flooding_id = 0;
  49. /* To avoid re-decrypting same packet for multiple nodes, cache and check */
  50. static uint8_t cache_pkt[29];
  51. static uint8_t cache_plain[29];
  52. static size_t cache_len;
  53. static size_t cache_plainlen;
  54. static uint32_t cache_id;
  55. static uint32_t cache_iv_index;
  56. static bool match_flooding(const void *a, const void *b)
  57. {
  58. const struct net_key *key = a;
  59. return (memcmp(key->flooding, b, sizeof(key->flooding)) == 0);
  60. }
  61. static bool match_id(const void *a, const void *b)
  62. {
  63. const struct net_key *key = a;
  64. uint32_t id = L_PTR_TO_UINT(b);
  65. return id == key->id;
  66. }
  67. static bool match_network(const void *a, const void *b)
  68. {
  69. const struct net_key *key = a;
  70. const uint8_t *network = b;
  71. return memcmp(key->network, network, sizeof(key->network)) == 0;
  72. }
  73. /* Key added from Provisioning, NetKey Add or NetKey update */
  74. uint32_t net_key_add(const uint8_t flooding[16])
  75. {
  76. struct net_key *key = l_queue_find(keys, match_flooding, flooding);
  77. uint8_t p[] = {0};
  78. bool result;
  79. if (key) {
  80. key->ref_cnt++;
  81. return key->id;
  82. }
  83. if (!keys)
  84. keys = l_queue_new();
  85. key = l_new(struct net_key, 1);
  86. memcpy(key->flooding, flooding, 16);
  87. key->ref_cnt++;
  88. result = mesh_crypto_k2(flooding, p, sizeof(p), &key->nid, key->encrypt,
  89. key->privacy);
  90. if (!result)
  91. goto fail;
  92. result = mesh_crypto_k3(flooding, key->network);
  93. if (!result)
  94. goto fail;
  95. result = mesh_crypto_nkbk(flooding, key->beacon);
  96. if (!result)
  97. goto fail;
  98. key->id = ++last_flooding_id;
  99. l_queue_push_tail(keys, key);
  100. return key->id;
  101. fail:
  102. l_free(key);
  103. return 0;
  104. }
  105. uint32_t net_key_frnd_add(uint32_t flooding_id, uint16_t lpn, uint16_t frnd,
  106. uint16_t lp_cnt, uint16_t fn_cnt)
  107. {
  108. const struct net_key *key = l_queue_find(keys, match_id,
  109. L_UINT_TO_PTR(flooding_id));
  110. struct net_key *frnd_key;
  111. uint8_t p[9] = {0x01};
  112. bool result;
  113. if (!key || key->friend_key)
  114. return 0;
  115. frnd_key = l_new(struct net_key, 1);
  116. l_put_be16(lpn, p + 1);
  117. l_put_be16(frnd, p + 3);
  118. l_put_be16(lp_cnt, p + 5);
  119. l_put_be16(fn_cnt, p + 7);
  120. result = mesh_crypto_k2(key->flooding, p, sizeof(p), &frnd_key->nid,
  121. frnd_key->encrypt, frnd_key->privacy);
  122. if (!result) {
  123. l_free(frnd_key);
  124. return 0;
  125. }
  126. frnd_key->friend_key = true;
  127. frnd_key->ref_cnt++;
  128. frnd_key->id = ++last_flooding_id;
  129. l_queue_push_head(keys, frnd_key);
  130. return frnd_key->id;
  131. }
  132. void net_key_unref(uint32_t id)
  133. {
  134. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  135. if (key && key->ref_cnt) {
  136. if (--key->ref_cnt == 0) {
  137. l_timeout_remove(key->snb.timeout);
  138. l_queue_remove(keys, key);
  139. l_free(key);
  140. }
  141. }
  142. }
  143. bool net_key_confirm(uint32_t id, const uint8_t flooding[16])
  144. {
  145. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  146. if (key)
  147. return !memcmp(key->flooding, flooding, sizeof(key->flooding));
  148. return false;
  149. }
  150. bool net_key_retrieve(uint32_t id, uint8_t *flooding)
  151. {
  152. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  153. if (key) {
  154. memcpy(flooding, key->flooding, sizeof(key->flooding));
  155. return true;
  156. }
  157. return false;
  158. }
  159. static void decrypt_net_pkt(void *a, void *b)
  160. {
  161. const struct net_key *key = a;
  162. bool result;
  163. if (cache_id || !key->ref_cnt || (cache_pkt[0] & 0x7f) != key->nid)
  164. return;
  165. result = mesh_crypto_packet_decode(cache_pkt, cache_len, false,
  166. cache_plain, cache_iv_index,
  167. key->encrypt, key->privacy);
  168. if (result) {
  169. cache_id = key->id;
  170. if (cache_plain[1] & 0x80)
  171. cache_plainlen = cache_len - 8;
  172. else
  173. cache_plainlen = cache_len - 4;
  174. }
  175. }
  176. uint32_t net_key_decrypt(uint32_t iv_index, const uint8_t *pkt, size_t len,
  177. uint8_t **plain, size_t *plain_len)
  178. {
  179. /* If we already successfully decrypted this packet, use cached data */
  180. if (cache_id && cache_len == len && !memcmp(pkt, cache_pkt, len)) {
  181. /* IV Index must match what was used to decrypt */
  182. if (cache_iv_index != iv_index)
  183. return 0;
  184. goto done;
  185. }
  186. cache_id = 0;
  187. memcpy(cache_pkt, pkt, len);
  188. cache_len = len;
  189. cache_iv_index = iv_index;
  190. /* Try all network keys known to us */
  191. l_queue_foreach(keys, decrypt_net_pkt, NULL);
  192. done:
  193. if (cache_id) {
  194. *plain = cache_plain;
  195. *plain_len = cache_plainlen;
  196. }
  197. return cache_id;
  198. }
  199. bool net_key_encrypt(uint32_t id, uint32_t iv_index, uint8_t *pkt, size_t len)
  200. {
  201. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  202. bool result;
  203. if (!key)
  204. return false;
  205. result = mesh_crypto_packet_encode(pkt, len, iv_index, key->encrypt,
  206. key->privacy);
  207. if (!result)
  208. return false;
  209. result = mesh_crypto_packet_label(pkt, len, iv_index, key->nid);
  210. return result;
  211. }
  212. uint32_t net_key_network_id(const uint8_t network[8])
  213. {
  214. struct net_key *key = l_queue_find(keys, match_network, network);
  215. if (!key)
  216. return 0;
  217. return key->id;
  218. }
  219. bool net_key_snb_check(uint32_t id, uint32_t iv_index, bool kr, bool ivu,
  220. uint64_t cmac)
  221. {
  222. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  223. uint64_t cmac_check;
  224. if (!key)
  225. return false;
  226. /* Any behavioral changes must pass CMAC test */
  227. if (!mesh_crypto_beacon_cmac(key->beacon, key->network, iv_index, kr,
  228. ivu, &cmac_check)) {
  229. l_error("mesh_crypto_beacon_cmac failed");
  230. return false;
  231. }
  232. if (cmac != cmac_check) {
  233. l_error("cmac compare failed 0x%16" PRIx64 " != 0x%16" PRIx64,
  234. cmac, cmac_check);
  235. return false;
  236. }
  237. return true;
  238. }
  239. bool net_key_snb_compose(uint32_t id, uint32_t iv_index, bool kr, bool ivu,
  240. uint8_t *snb)
  241. {
  242. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  243. uint64_t cmac;
  244. if (!key)
  245. return false;
  246. /* Any behavioral changes must pass CMAC test */
  247. if (!mesh_crypto_beacon_cmac(key->beacon, key->network, iv_index, kr,
  248. ivu, &cmac)) {
  249. l_error("mesh_crypto_beacon_cmac failed");
  250. return false;
  251. }
  252. snb[0] = MESH_AD_TYPE_BEACON;
  253. snb[1] = BEACON_TYPE_SNB;
  254. snb[2] = 0;
  255. if (kr)
  256. snb[2] |= KEY_REFRESH;
  257. if (ivu)
  258. snb[2] |= IV_INDEX_UPDATE;
  259. memcpy(snb + 3, key->network, 8);
  260. l_put_be32(iv_index, snb + 11);
  261. l_put_be64(cmac, snb + 15);
  262. return true;
  263. }
  264. static void send_network_beacon(struct net_key *key)
  265. {
  266. struct mesh_io_send_info info = {
  267. .type = MESH_IO_TIMING_TYPE_GENERAL,
  268. .u.gen.interval = 100,
  269. .u.gen.cnt = 1,
  270. .u.gen.min_delay = DEFAULT_MIN_DELAY,
  271. .u.gen.max_delay = DEFAULT_MAX_DELAY
  272. };
  273. mesh_io_send(NULL, &info, key->snb.beacon, sizeof(key->snb.beacon));
  274. }
  275. static void snb_timeout(struct l_timeout *timeout, void *user_data)
  276. {
  277. struct net_key *key = user_data;
  278. uint32_t interval, scale_factor;
  279. /* Always send at least one beacon */
  280. send_network_beacon(key);
  281. /* Count our own beacons towards the vicinity total */
  282. key->snb.observed++;
  283. if (!key->snb.half_period) {
  284. l_debug("beacon %d for %d nodes, period %d, obs %d, exp %d",
  285. key->id,
  286. key->beacon_enables,
  287. key->snb.observe_period,
  288. key->snb.observed,
  289. key->snb.expected);
  290. interval = (key->snb.observe_period * key->snb.observed)
  291. / key->snb.expected;
  292. /* Limit Increases and Decreases by 10 seconds Up and
  293. * 20 seconds down each step, to avoid going nearly silent
  294. * in highly populated environments.
  295. */
  296. if (interval - 10 > key->snb.observe_period)
  297. interval = key->snb.observe_period + 10;
  298. else if (interval + 20 < key->snb.observe_period)
  299. interval = key->snb.observe_period - 20;
  300. /* Beaconing must be no *slower* than once every 10 minutes,
  301. * and no *faster* than once every 10 seconds, per spec.
  302. * Observation period is twice beaconing period.
  303. */
  304. if (interval < BEACON_INTERVAL_MIN * 2)
  305. interval = BEACON_INTERVAL_MIN * 2;
  306. else if (interval > BEACON_INTERVAL_MAX * 2)
  307. interval = BEACON_INTERVAL_MAX * 2;
  308. key->snb.observe_period = interval;
  309. key->snb.observed = 0;
  310. /* To prevent "over slowing" of the beaconing frequency,
  311. * require more significant "over observing" the slower
  312. * our own beaconing frequency.
  313. */
  314. key->snb.expected = interval / 10;
  315. scale_factor = interval / 60;
  316. key->snb.expected += scale_factor * 3;
  317. }
  318. interval = key->snb.observe_period / 2;
  319. key->snb.half_period = !key->snb.half_period;
  320. if (key->beacon_enables)
  321. l_timeout_modify(timeout, interval);
  322. else {
  323. l_timeout_remove(timeout);
  324. key->snb.timeout = NULL;
  325. }
  326. }
  327. void net_key_beacon_seen(uint32_t id)
  328. {
  329. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  330. if (key) {
  331. key->snb.observed++;
  332. key->snb.ts = get_timestamp_secs();
  333. }
  334. }
  335. uint32_t net_key_beacon_last_seen(uint32_t id)
  336. {
  337. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  338. if (key)
  339. return key->snb.ts;
  340. return 0;
  341. }
  342. void net_key_beacon_enable(uint32_t id)
  343. {
  344. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  345. bool enabled;
  346. uint32_t rand_ms;
  347. if (!key)
  348. return;
  349. enabled = !!key->beacon_enables;
  350. key->beacon_enables++;
  351. /* If already Enabled, do nothing */
  352. if (enabled)
  353. return;
  354. /* Randomize first timeout to avoid bursts of beacons */
  355. l_getrandom(&rand_ms, sizeof(rand_ms));
  356. rand_ms %= (BEACON_INTERVAL_MIN * 1000);
  357. rand_ms++;
  358. /* Enable Periodic Beaconing on this key */
  359. key->snb.observe_period = BEACON_INTERVAL_MIN * 2;
  360. key->snb.expected = 2;
  361. key->snb.observed = 0;
  362. key->snb.half_period = true;
  363. l_timeout_remove(key->snb.timeout);
  364. key->snb.timeout = l_timeout_create_ms(rand_ms, snb_timeout, key, NULL);
  365. }
  366. bool net_key_beacon_refresh(uint32_t id, uint32_t iv_index, bool kr, bool ivu)
  367. {
  368. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  369. uint8_t beacon[23];
  370. uint32_t rand_ms;
  371. if (!key)
  372. return false;
  373. if (!net_key_snb_compose(id, iv_index, kr, ivu, beacon))
  374. return false;
  375. if (memcmp(key->snb.beacon, beacon, sizeof(beacon)))
  376. memcpy(key->snb.beacon, beacon, sizeof(beacon));
  377. else
  378. return false;
  379. l_debug("Setting SNB: IVI: %8.8x, IVU: %d, KR: %d", iv_index, ivu, kr);
  380. print_packet("Set SNB Beacon to", beacon, sizeof(beacon));
  381. /* Propagate changes to all local nodes */
  382. net_local_beacon(id, beacon);
  383. /* Send one new SNB soon, after all nodes have seen it */
  384. l_getrandom(&rand_ms, sizeof(rand_ms));
  385. rand_ms %= 1000;
  386. key->snb.expected++;
  387. if (key->snb.timeout)
  388. l_timeout_modify_ms(key->snb.timeout, 500 + rand_ms);
  389. else
  390. key->snb.timeout = l_timeout_create_ms(500 + rand_ms,
  391. snb_timeout, key, NULL);
  392. return true;
  393. }
  394. void net_key_beacon_disable(uint32_t id)
  395. {
  396. struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
  397. if (!key || !key->beacon_enables)
  398. return;
  399. key->beacon_enables--;
  400. if (key->beacon_enables)
  401. return;
  402. /* Disable periodic Beaconing on this key */
  403. l_timeout_remove(key->snb.timeout);
  404. key->snb.timeout = NULL;
  405. }
  406. void net_key_cleanup(void)
  407. {
  408. l_queue_destroy(keys, l_free);
  409. keys = NULL;
  410. }