keyring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. #define _GNU_SOURCE
  14. #include <fcntl.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. #include <ell/ell.h>
  22. #include "mesh/mesh-defs.h"
  23. #include "mesh/dbus.h"
  24. #include "mesh/node.h"
  25. #include "mesh/keyring.h"
  26. const char *dev_key_dir = "/dev_keys";
  27. const char *app_key_dir = "/app_keys";
  28. const char *net_key_dir = "/net_keys";
  29. static int open_key_file(struct mesh_node *node, const char *key_dir,
  30. uint16_t idx, int flags)
  31. {
  32. const char *node_path;
  33. char fname[PATH_MAX];
  34. if (!node)
  35. return -1;
  36. node_path = node_get_storage_dir(node);
  37. if (strlen(node_path) + strlen(key_dir) + 1 + 3 >= PATH_MAX)
  38. return -1;
  39. if (flags & O_CREAT) {
  40. snprintf(fname, PATH_MAX, "%s%s", node_path, key_dir);
  41. mkdir(fname, 0755);
  42. }
  43. snprintf(fname, PATH_MAX, "%s%s/%3.3x", node_path, key_dir, idx);
  44. if (flags & O_CREAT)
  45. return open(fname, flags, 0600);
  46. else
  47. return open(fname, flags);
  48. }
  49. bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
  50. struct keyring_net_key *key)
  51. {
  52. bool result = false;
  53. int fd;
  54. if (!key)
  55. return false;
  56. fd = open_key_file(node, net_key_dir, net_idx,
  57. O_WRONLY | O_CREAT | O_TRUNC);
  58. if (fd < 0)
  59. return false;
  60. if (write(fd, key, sizeof(*key)) == sizeof(*key))
  61. result = true;
  62. close(fd);
  63. return result;
  64. }
  65. bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
  66. uint16_t net_idx, struct keyring_app_key *key)
  67. {
  68. bool result = false;
  69. int fd;
  70. if (!key)
  71. return false;
  72. fd = open_key_file(node, app_key_dir, app_idx, O_RDWR);
  73. if (fd >= 0) {
  74. struct keyring_app_key old_key;
  75. if (read(fd, &old_key, sizeof(old_key)) == sizeof(old_key)) {
  76. if (old_key.net_idx != net_idx) {
  77. close(fd);
  78. return false;
  79. }
  80. }
  81. lseek(fd, 0, SEEK_SET);
  82. } else
  83. fd = open_key_file(node, app_key_dir, app_idx,
  84. O_WRONLY | O_CREAT | O_TRUNC);
  85. if (fd < 0)
  86. return false;
  87. if (write(fd, key, sizeof(*key)) == sizeof(*key))
  88. result = true;
  89. close(fd);
  90. return result;
  91. }
  92. static void finalize(int dir_fd, const char *fname, uint16_t net_idx)
  93. {
  94. struct keyring_app_key key;
  95. int fd;
  96. fd = openat(dir_fd, fname, O_RDWR);
  97. if (fd < 0)
  98. return;
  99. if (read(fd, &key, sizeof(key)) != sizeof(key) ||
  100. key.net_idx != net_idx)
  101. goto done;
  102. l_debug("Finalize %s", fname);
  103. memcpy(key.old_key, key.new_key, 16);
  104. lseek(fd, 0, SEEK_SET);
  105. if (write(fd, &key, sizeof(key)) != sizeof(key))
  106. goto done;
  107. done:
  108. close(fd);
  109. }
  110. bool keyring_finalize_app_keys(struct mesh_node *node, uint16_t net_idx)
  111. {
  112. const char *node_path;
  113. char key_dir[PATH_MAX];
  114. DIR *dir;
  115. int dir_fd;
  116. struct dirent *entry;
  117. if (!node)
  118. return false;
  119. node_path = node_get_storage_dir(node);
  120. if (strlen(node_path) + strlen(app_key_dir) + 1 >= PATH_MAX)
  121. return false;
  122. snprintf(key_dir, PATH_MAX, "%s%s", node_path, app_key_dir);
  123. dir = opendir(key_dir);
  124. if (!dir) {
  125. if (errno == ENOENT)
  126. return true;
  127. l_error("Failed to open AppKey storage directory: %s", key_dir);
  128. return false;
  129. }
  130. dir_fd = dirfd(dir);
  131. while ((entry = readdir(dir)) != NULL) {
  132. /* AppKeys are stored in regular files */
  133. if (entry->d_type == DT_REG)
  134. finalize(dir_fd, entry->d_name, net_idx);
  135. }
  136. closedir(dir);
  137. return true;
  138. }
  139. bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
  140. uint8_t count, uint8_t dev_key[16])
  141. {
  142. const char *node_path;
  143. char key_file[PATH_MAX];
  144. bool result = true;
  145. int fd, i;
  146. if (!IS_UNICAST_RANGE(unicast, count))
  147. return false;
  148. if (!node)
  149. return false;
  150. node_path = node_get_storage_dir(node);
  151. if (strlen(node_path) + strlen(dev_key_dir) + 1 + 4 >= PATH_MAX)
  152. return false;
  153. snprintf(key_file, PATH_MAX, "%s%s", node_path, dev_key_dir);
  154. mkdir(key_file, 0755);
  155. for (i = 0; i < count; i++) {
  156. snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path,
  157. dev_key_dir, unicast + i);
  158. l_debug("Put Dev Key %s", key_file);
  159. fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  160. if (fd >= 0) {
  161. if (write(fd, dev_key, 16) != 16)
  162. result = false;
  163. close(fd);
  164. } else
  165. result = false;
  166. }
  167. return result;
  168. }
  169. static bool get_key(struct mesh_node *node, const char *key_dir,
  170. uint16_t key_idx, void *key, ssize_t sz)
  171. {
  172. bool result = false;
  173. int fd;
  174. if (!key)
  175. return false;
  176. fd = open_key_file(node, key_dir, key_idx, O_RDONLY);
  177. if (fd >= 0) {
  178. if (read(fd, key, sz) == sz)
  179. result = true;
  180. close(fd);
  181. }
  182. return result;
  183. }
  184. bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,
  185. struct keyring_net_key *key)
  186. {
  187. return get_key(node, net_key_dir, net_idx, key, sizeof(*key));
  188. }
  189. bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,
  190. struct keyring_app_key *key)
  191. {
  192. return get_key(node, app_key_dir, app_idx, key, sizeof(*key));
  193. }
  194. bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,
  195. uint8_t dev_key[16])
  196. {
  197. const char *node_path;
  198. char key_file[PATH_MAX];
  199. bool result = false;
  200. int fd;
  201. if (!IS_UNICAST(unicast))
  202. return false;
  203. if (!node)
  204. return false;
  205. node_path = node_get_storage_dir(node);
  206. snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path, dev_key_dir,
  207. unicast);
  208. fd = open(key_file, O_RDONLY);
  209. if (fd >= 0) {
  210. if (read(fd, dev_key, 16) == 16)
  211. result = true;
  212. close(fd);
  213. }
  214. return result;
  215. }
  216. bool keyring_del_net_key(struct mesh_node *node, uint16_t net_idx)
  217. {
  218. const char *node_path;
  219. char key_file[PATH_MAX];
  220. if (!node)
  221. return false;
  222. node_path = node_get_storage_dir(node);
  223. snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, net_key_dir,
  224. net_idx);
  225. l_debug("RM Net Key %s", key_file);
  226. remove(key_file);
  227. /* TODO: See if it is easiest to delete all bound App keys here */
  228. /* TODO: see nftw() */
  229. return true;
  230. }
  231. bool keyring_del_app_key(struct mesh_node *node, uint16_t app_idx)
  232. {
  233. const char *node_path;
  234. char key_file[PATH_MAX];
  235. if (!node)
  236. return false;
  237. node_path = node_get_storage_dir(node);
  238. snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, app_key_dir,
  239. app_idx);
  240. l_debug("RM App Key %s", key_file);
  241. remove(key_file);
  242. return true;
  243. }
  244. bool keyring_del_remote_dev_key(struct mesh_node *node, uint16_t unicast,
  245. uint8_t count)
  246. {
  247. const char *node_path;
  248. char key_file[PATH_MAX];
  249. int i;
  250. if (!IS_UNICAST_RANGE(unicast, count))
  251. return false;
  252. if (!node)
  253. return false;
  254. node_path = node_get_storage_dir(node);
  255. for (i = 0; i < count; i++) {
  256. snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path,
  257. dev_key_dir, unicast + i);
  258. l_debug("RM Dev Key %s", key_file);
  259. remove(key_file);
  260. }
  261. return true;
  262. }
  263. static DIR *open_key_dir(const char *node_path, const char *key_dir_name)
  264. {
  265. char dir_path[PATH_MAX];
  266. DIR *key_dir;
  267. if (strlen(node_path) + strlen(key_dir_name) + 1 >= PATH_MAX)
  268. return NULL;
  269. snprintf(dir_path, PATH_MAX, "%s%s", node_path, key_dir_name);
  270. key_dir = opendir(dir_path);
  271. if (!key_dir) {
  272. l_error("Failed to open keyring storage directory: %s",
  273. dir_path);
  274. return NULL;
  275. }
  276. return key_dir;
  277. }
  278. static int open_key_dir_entry(int dir_fd, struct dirent *entry,
  279. uint8_t fname_len)
  280. {
  281. if (entry->d_type != DT_REG)
  282. return -1;
  283. /* Check the file name length */
  284. if (strlen(entry->d_name) != fname_len)
  285. return -1;
  286. return openat(dir_fd, entry->d_name, O_RDONLY);
  287. }
  288. static void append_old_key(struct l_dbus_message_builder *builder,
  289. const uint8_t key[16])
  290. {
  291. l_dbus_message_builder_enter_dict(builder, "sv");
  292. l_dbus_message_builder_append_basic(builder, 's', "OldKey");
  293. l_dbus_message_builder_enter_variant(builder, "ay");
  294. dbus_append_byte_array(builder, key, 16);
  295. l_dbus_message_builder_leave_variant(builder);
  296. l_dbus_message_builder_leave_dict(builder);
  297. }
  298. static void build_app_keys_reply(const char *node_path,
  299. struct l_dbus_message_builder *builder,
  300. uint16_t net_idx, uint8_t phase)
  301. {
  302. DIR *key_dir;
  303. int key_dir_fd;
  304. struct dirent *entry;
  305. key_dir = open_key_dir(node_path, app_key_dir);
  306. if (!key_dir)
  307. return;
  308. key_dir_fd = dirfd(key_dir);
  309. l_dbus_message_builder_enter_dict(builder, "sv");
  310. l_dbus_message_builder_append_basic(builder, 's', "AppKeys");
  311. l_dbus_message_builder_enter_variant(builder, "a(qaya{sv})");
  312. l_dbus_message_builder_enter_array(builder, "(qaya{sv})");
  313. while ((entry = readdir(key_dir)) != NULL) {
  314. struct keyring_app_key key;
  315. int fd = open_key_dir_entry(key_dir_fd, entry, 3);
  316. if (fd < 0)
  317. continue;
  318. if (read(fd, &key, sizeof(key)) != sizeof(key) ||
  319. key.net_idx != net_idx) {
  320. close(fd);
  321. continue;
  322. }
  323. close(fd);
  324. l_dbus_message_builder_enter_struct(builder, "qaya{sv}");
  325. l_dbus_message_builder_append_basic(builder, 'q', &key.app_idx);
  326. dbus_append_byte_array(builder, key.new_key, 16);
  327. l_dbus_message_builder_enter_array(builder, "{sv}");
  328. if (phase != KEY_REFRESH_PHASE_NONE)
  329. append_old_key(builder, key.old_key);
  330. l_dbus_message_builder_leave_array(builder);
  331. l_dbus_message_builder_leave_struct(builder);
  332. }
  333. l_dbus_message_builder_leave_array(builder);
  334. l_dbus_message_builder_leave_variant(builder);
  335. l_dbus_message_builder_leave_dict(builder);
  336. closedir(key_dir);
  337. }
  338. static bool build_net_keys_reply(const char *node_path,
  339. struct l_dbus_message_builder *builder)
  340. {
  341. DIR *key_dir;
  342. int key_dir_fd;
  343. struct dirent *entry;
  344. bool result = false;
  345. key_dir = open_key_dir(node_path, net_key_dir);
  346. if (!key_dir)
  347. return false;
  348. key_dir_fd = dirfd(key_dir);
  349. l_dbus_message_builder_enter_dict(builder, "sv");
  350. l_dbus_message_builder_append_basic(builder, 's', "NetKeys");
  351. l_dbus_message_builder_enter_variant(builder, "a(qaya{sv})");
  352. l_dbus_message_builder_enter_array(builder, "(qaya{sv})");
  353. while ((entry = readdir(key_dir)) != NULL) {
  354. struct keyring_net_key key;
  355. int fd = open_key_dir_entry(key_dir_fd, entry, 3);
  356. if (fd < 0)
  357. continue;
  358. if (read(fd, &key, sizeof(key)) != sizeof(key)) {
  359. close(fd);
  360. goto done;
  361. }
  362. close(fd);
  363. /*
  364. * If network key is stuck in phase 3, keyring
  365. * write failed and this key info is unreliable.
  366. */
  367. if (key.phase == KEY_REFRESH_PHASE_THREE)
  368. continue;
  369. l_dbus_message_builder_enter_struct(builder, "qaya{sv}");
  370. l_dbus_message_builder_append_basic(builder, 'q', &key.net_idx);
  371. dbus_append_byte_array(builder, key.new_key, 16);
  372. l_dbus_message_builder_enter_array(builder, "{sv}");
  373. if (key.phase != KEY_REFRESH_PHASE_NONE) {
  374. dbus_append_dict_entry_basic(builder, "Phase", "y",
  375. &key.phase);
  376. append_old_key(builder, key.old_key);
  377. }
  378. build_app_keys_reply(node_path, builder, key.net_idx,
  379. key.phase);
  380. l_dbus_message_builder_leave_array(builder);
  381. l_dbus_message_builder_leave_struct(builder);
  382. }
  383. l_dbus_message_builder_leave_array(builder);
  384. l_dbus_message_builder_leave_variant(builder);
  385. l_dbus_message_builder_leave_dict(builder);
  386. result = true;
  387. done:
  388. closedir(key_dir);
  389. return result;
  390. }
  391. struct dev_key_entry {
  392. uint16_t unicast;
  393. uint8_t value[16];
  394. };
  395. static bool match_key_value(const void *a, const void *b)
  396. {
  397. const struct dev_key_entry *key = a;
  398. const uint8_t *value = b;
  399. return (memcmp(key->value, value, 16) == 0);
  400. }
  401. static void build_dev_key_entry(void *a, void *b)
  402. {
  403. struct dev_key_entry *key = a;
  404. struct l_dbus_message_builder *builder = b;
  405. l_dbus_message_builder_enter_struct(builder, "qay");
  406. l_dbus_message_builder_append_basic(builder, 'q', &key->unicast);
  407. dbus_append_byte_array(builder, key->value, 16);
  408. l_dbus_message_builder_leave_struct(builder);
  409. }
  410. static bool build_dev_keys_reply(const char *node_path,
  411. struct l_dbus_message_builder *builder)
  412. {
  413. DIR *key_dir;
  414. int key_dir_fd;
  415. struct dirent *entry;
  416. struct l_queue *keys;
  417. bool result = false;
  418. key_dir = open_key_dir(node_path, dev_key_dir);
  419. /*
  420. * There is always at least one device key present for a local node.
  421. * Therefore, return false, if the directory does not exist.
  422. */
  423. if (!key_dir)
  424. return false;
  425. key_dir_fd = dirfd(key_dir);
  426. keys = l_queue_new();
  427. while ((entry = readdir(key_dir)) != NULL) {
  428. uint8_t buf[16];
  429. uint16_t unicast;
  430. struct dev_key_entry *key;
  431. int fd = open_key_dir_entry(key_dir_fd, entry, 4);
  432. if (fd < 0)
  433. continue;
  434. if (read(fd, buf, 16) != 16) {
  435. close(fd);
  436. goto done;
  437. }
  438. close(fd);
  439. if (sscanf(entry->d_name, "%04hx", &unicast) != 1)
  440. goto done;
  441. key = l_queue_find(keys, match_key_value, buf);
  442. if (key) {
  443. if (key->unicast > unicast)
  444. key->unicast = unicast;
  445. continue;
  446. }
  447. key = l_new(struct dev_key_entry, 1);
  448. key->unicast = unicast;
  449. memcpy(key->value, buf, 16);
  450. l_queue_push_tail(keys, key);
  451. }
  452. l_dbus_message_builder_enter_dict(builder, "sv");
  453. l_dbus_message_builder_append_basic(builder, 's', "DevKeys");
  454. l_dbus_message_builder_enter_variant(builder, "a(qay)");
  455. l_dbus_message_builder_enter_array(builder, "(qay)");
  456. l_queue_foreach(keys, build_dev_key_entry, builder);
  457. l_dbus_message_builder_leave_array(builder);
  458. l_dbus_message_builder_leave_variant(builder);
  459. l_dbus_message_builder_leave_dict(builder);
  460. result = true;
  461. done:
  462. l_queue_destroy(keys, l_free);
  463. closedir(key_dir);
  464. return result;
  465. }
  466. bool keyring_build_export_keys_reply(struct mesh_node *node,
  467. struct l_dbus_message_builder *builder)
  468. {
  469. const char *node_path;
  470. if (!node)
  471. return false;
  472. node_path = node_get_storage_dir(node);
  473. if (!build_net_keys_reply(node_path, builder))
  474. return false;
  475. return build_dev_keys_reply(node_path, builder);
  476. }