2014-06-11 08:00:13

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCHv2 1/2] android: Store remote's Identity Resolving Key

Persistent storage is used so we can load IRKs on startup.
---
android/bluetooth.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 36b073e..8638f84 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -1964,6 +1964,62 @@ static void new_csrk_callback(uint16_t index, uint16_t length,
store_csrk(dev);
}

+static void store_irk(struct device *dev, const uint8_t *val)
+{
+ GKeyFile *key_file;
+ char key_str[33];
+ char addr[18];
+ int i;
+ gsize length = 0;
+ char *data;
+
+ ba2str(&dev->bdaddr, addr);
+
+ key_file = g_key_file_new();
+ if (!g_key_file_load_from_file(key_file, DEVICES_FILE, 0, NULL)) {
+ g_key_file_free(key_file);
+ return;
+ }
+
+ for (i = 0; i < 16; i++)
+ sprintf(key_str + (i * 2), "%2.2X", val[i]);
+
+ g_key_file_set_string(key_file, addr, "IdentityResolvingKey", key_str);
+
+ data = g_key_file_to_data(key_file, &length, NULL);
+ g_file_set_contents(DEVICES_FILE, data, length, NULL);
+ g_free(data);
+
+ g_key_file_free(key_file);
+}
+
+static void new_irk_callback(uint16_t index, uint16_t length,
+ const void *param, void *user_data)
+{
+ const struct mgmt_ev_new_irk *ev = param;
+ const struct mgmt_addr_info *addr = &ev->key.addr;
+ struct device *dev;
+ char dst[18], rpa[18];
+
+ if (length < sizeof(*ev)) {
+ error("To small New Irk Event (%u bytes)", length);
+ return;
+ }
+
+ ba2str(&ev->key.addr.bdaddr, dst);
+ ba2str(&ev->rpa, rpa);
+
+ DBG("new IRK for %s, RPA %s", dst, rpa);
+
+ /* TODO: handle new Identity to RPA mapping */
+ dev = find_device(&addr->bdaddr);
+ if (!dev)
+ return;
+
+ if (ev->store_hint)
+ store_irk(dev, ev->key.val);
+}
+
static void register_mgmt_handlers(void)
{
mgmt_register(mgmt_if, MGMT_EV_NEW_SETTINGS, adapter.index,
@@ -2017,6 +2073,9 @@ static void register_mgmt_handlers(void)
mgmt_register(mgmt_if, MGMT_EV_NEW_CSRK, adapter.index,
new_csrk_callback, NULL, NULL);

+ mgmt_register(mgmt_if, MGMT_EV_NEW_IRK, adapter.index, new_irk_callback,
+ NULL, NULL);
+
}

static void load_link_keys_complete(uint8_t status, uint16_t length,
--
2.0.0



2014-06-12 10:16:12

by Szymon Janc

[permalink] [raw]
Subject: Re: [PATCHv2 1/2] android: Store remote's Identity Resolving Key

Hi Jakub,

On Wednesday 11 of June 2014 10:00:13 Jakub Tyszkowski wrote:
> Persistent storage is used so we can load IRKs on startup.
> ---
> android/bluetooth.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 59 insertions(+)
>
> diff --git a/android/bluetooth.c b/android/bluetooth.c
> index 36b073e..8638f84 100644
> --- a/android/bluetooth.c
> +++ b/android/bluetooth.c
> @@ -1964,6 +1964,62 @@ static void new_csrk_callback(uint16_t index, uint16_t length,
> store_csrk(dev);
> }
>
> +static void store_irk(struct device *dev, const uint8_t *val)
> +{
> + GKeyFile *key_file;
> + char key_str[33];
> + char addr[18];
> + int i;
> + gsize length = 0;
> + char *data;
> +
> + ba2str(&dev->bdaddr, addr);
> +
> + key_file = g_key_file_new();
> + if (!g_key_file_load_from_file(key_file, DEVICES_FILE, 0, NULL)) {
> + g_key_file_free(key_file);
> + return;
> + }
> +
> + for (i = 0; i < 16; i++)
> + sprintf(key_str + (i * 2), "%2.2X", val[i]);
> +
> + g_key_file_set_string(key_file, addr, "IdentityResolvingKey", key_str);
> +
> + data = g_key_file_to_data(key_file, &length, NULL);
> + g_file_set_contents(DEVICES_FILE, data, length, NULL);
> + g_free(data);
> +
> + g_key_file_free(key_file);
> +}
> +
> +static void new_irk_callback(uint16_t index, uint16_t length,
> + const void *param, void *user_data)
> +{
> + const struct mgmt_ev_new_irk *ev = param;
> + const struct mgmt_addr_info *addr = &ev->key.addr;
> + struct device *dev;
> + char dst[18], rpa[18];
> +
> + if (length < sizeof(*ev)) {
> + error("To small New Irk Event (%u bytes)", length);
> + return;
> + }
> +
> + ba2str(&ev->key.addr.bdaddr, dst);
> + ba2str(&ev->rpa, rpa);
> +
> + DBG("new IRK for %s, RPA %s", dst, rpa);
> +
> + /* TODO: handle new Identity to RPA mapping */
> + dev = find_device(&addr->bdaddr);
> + if (!dev)
> + return;
> +
> + if (ev->store_hint)
> + store_irk(dev, ev->key.val);
> +}
> +
> static void register_mgmt_handlers(void)
> {
> mgmt_register(mgmt_if, MGMT_EV_NEW_SETTINGS, adapter.index,
> @@ -2017,6 +2073,9 @@ static void register_mgmt_handlers(void)
> mgmt_register(mgmt_if, MGMT_EV_NEW_CSRK, adapter.index,
> new_csrk_callback, NULL, NULL);
>
> + mgmt_register(mgmt_if, MGMT_EV_NEW_IRK, adapter.index, new_irk_callback,
> + NULL, NULL);
> +
> }
>
> static void load_link_keys_complete(uint8_t status, uint16_t length,
>

Both patches applied, thanks.

--
Best regards,
Szymon Janc

2014-06-11 08:00:14

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCHv2 2/2] android: Load Identity Resolving Keys

Load IRKs from file.
---
android/bluetooth.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 8638f84..91a3400 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -2168,6 +2168,33 @@ static void load_ltks(GSList *ltks)
g_free(cp);
}

+static void load_irks(GSList *irks)
+{
+ struct mgmt_cp_load_irks *cp;
+ struct mgmt_irk_info *irk;
+ size_t irk_count, cp_size;
+ GSList *l;
+
+ irk_count = g_slist_length(irks);
+
+ DBG("irks %zu", irk_count);
+
+ cp_size = sizeof(*cp) + (irk_count * sizeof(*irk));
+
+ cp = g_malloc0(cp_size);
+
+ cp->irk_count = htobs(irk_count);
+
+ for (l = irks, irk = cp->irks; l != NULL; l = g_slist_next(l), irk++)
+ memcpy(irk, irks->data, sizeof(*irk));
+
+ if (mgmt_send(mgmt_if, MGMT_OP_LOAD_IRKS, adapter.index, cp_size, cp,
+ NULL, NULL, NULL) == 0)
+ error("Failed to load IRKs");
+
+ g_free(cp);
+}
+
static uint8_t get_adapter_uuids(void)
{
struct hal_ev_adapter_props_changed *ev;
@@ -2639,6 +2666,32 @@ failed:
return info;
}

+static struct mgmt_irk_info *get_irk_info(GKeyFile *key_file, const char *peer)
+{
+ struct mgmt_irk_info *info = NULL;
+ unsigned int i;
+ char *str;
+
+ str = g_key_file_get_string(key_file, peer, "IdentityResolvingKey",
+ NULL);
+ if (!str || strlen(str) != 32)
+ goto failed;
+
+ info = g_new0(struct mgmt_irk_info, 1);
+
+ str2ba(peer, &info->addr.bdaddr);
+
+ info->addr.type = g_key_file_get_integer(key_file, peer, "Type", NULL);
+
+ for (i = 0; i < sizeof(info->val); i++)
+ sscanf(str + (i * 2), "%02hhX", &info->val[i]);
+
+failed:
+ g_free(str);
+
+ return info;
+}
+
static time_t device_timestamp(const struct device *dev)
{
if (dev->bredr && dev->le) {
@@ -2696,6 +2749,7 @@ static void load_devices_info(bt_bluetooth_ready cb)
unsigned int i;
GSList *keys = NULL;
GSList *ltks = NULL;
+ GSList *irks = NULL;

key_file = g_key_file_new();

@@ -2706,10 +2760,12 @@ static void load_devices_info(bt_bluetooth_ready cb)
for (i = 0; i < len; i++) {
struct mgmt_link_key_info *key_info;
struct mgmt_ltk_info *ltk_info;
+ struct mgmt_irk_info *irk_info;
struct mgmt_ltk_info *slave_ltk_info;
struct device *dev;

key_info = get_key_info(key_file, devs[i]);
+ irk_info = get_irk_info(key_file, devs[i]);
ltk_info = get_ltk_info(key_file, devs[i], true);
slave_ltk_info = get_ltk_info(key_file, devs[i], false);

@@ -2722,6 +2778,9 @@ static void load_devices_info(bt_bluetooth_ready cb)
if (key_info)
keys = g_slist_prepend(keys, key_info);

+ if (irk_info)
+ irks = g_slist_prepend(irks, irk_info);
+
if (ltk_info)
ltks = g_slist_prepend(ltks, ltk_info);

@@ -2736,6 +2795,9 @@ static void load_devices_info(bt_bluetooth_ready cb)
load_ltks(ltks);
g_slist_free_full(ltks, g_free);

+ load_irks(irks);
+ g_slist_free_full(irks, g_free);
+
load_link_keys(keys, cb);
g_slist_free_full(keys, g_free);

--
2.0.0