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