Linux bluetooth development
 help / color / mirror / Atom feed
From: Lukasz Rymanowski <lukasz.rymanowski@tieto.com>
To: linux-bluetooth@vger.kernel.org
Cc: szymon.janc@tieto.com, johan.hedberg@gmail.com,
	Lukasz Rymanowski <lukasz.rymanowski@tieto.com>
Subject: [PATCH v3 09/12] android/bluetooth: Store sign counter needed for aes-cmac sign
Date: Thu, 22 May 2014 21:06:23 +0200	[thread overview]
Message-ID: <1400785586-22710-10-git-send-email-lukasz.rymanowski@tieto.com> (raw)
In-Reply-To: <1400785586-22710-1-git-send-email-lukasz.rymanowski@tieto.com>

If CSRK is valid between sessions we should remember sign counter.
Therefor store it.
---
 android/bluetooth.c | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index f8a7b3d..445aceb 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -1878,8 +1878,6 @@ static void new_csrk_callback(uint16_t index, uint16_t length,
 
 	if (ev->store_hint)
 		store_csrk(dev);
-
-	/*TODO: Store sign counter */
 }
 
 static void register_mgmt_handlers(void)
@@ -2353,6 +2351,9 @@ static struct device *create_device_from_info(GKeyFile *key_file,
 			sscanf(str + (i * 2), "%02hhX", &dev->local_csrk[i]);
 
 		g_free(str);
+
+		dev->local_sign_cnt = g_key_file_get_integer(key_file, peer,
+						"LocalCSRKSignCounter", NULL);
 	}
 
 	str = g_key_file_get_string(key_file, peer, "RemoteCSRK", NULL);
@@ -2364,6 +2365,9 @@ static struct device *create_device_from_info(GKeyFile *key_file,
 			sscanf(str + (i * 2), "%02hhX", &dev->remote_csrk[i]);
 
 		g_free(str);
+
+		dev->remote_sign_cnt = g_key_file_get_integer(key_file, peer,
+						"RemoteCSRKSignCounter", NULL);
 	}
 
 	str = g_key_file_get_string(key_file, peer, "Name", NULL);
@@ -3334,6 +3338,37 @@ bool bt_get_csrk(const bdaddr_t *addr, enum bt_csrk_type type, uint8_t key[16],
 	return true;
 }
 
+static void store_sign_counter(struct device *dev, enum bt_csrk_type type)
+{
+	const char *sign_cnt_s;
+	uint32_t sign_cnt;
+	GKeyFile *key_file;
+	bool local = (type == LOCAL_CSRK);
+
+	gsize length = 0;
+	char addr[18];
+	char *data;
+
+	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;
+	}
+
+	ba2str(&dev->bdaddr, addr);
+
+	sign_cnt_s = local ? "LocalCSRKSignCounter" : "RemoteCSRKSignCounter";
+	sign_cnt = local ? dev->local_sign_cnt : dev->remote_sign_cnt;
+
+	g_key_file_set_integer(key_file, addr, sign_cnt_s, sign_cnt);
+
+	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);
+}
+
 void bt_update_sign_counter(const bdaddr_t *addr, enum bt_csrk_type type)
 {
 	struct device *dev;
@@ -3346,6 +3381,8 @@ void bt_update_sign_counter(const bdaddr_t *addr, enum bt_csrk_type type)
 		dev->local_sign_cnt++;
 	else
 		dev->remote_sign_cnt++;
+
+	store_sign_counter(dev, type);
 }
 
 static uint8_t set_adapter_scan_mode(const void *buf, uint16_t len)
-- 
1.8.4


  parent reply	other threads:[~2014-05-22 19:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-22 19:06 [PATCH v3 00/12] android/gatt: Add support for write signature command Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 01/12] shared/crypto: Extend bt_crypto_sign_att with sign counter Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 02/12] attrib: Add helpers to enc and dec signed write command Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 03/12] attrib/gatt: Add wrapper to send " Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 04/12] android/bluetooth: Expose API to get CSRK for device Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 05/12] android/bluetooth: Expose API to update sign counter Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 06/12] android/bluetooth: Add support to read CSRK from the kernel Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 07/12] android/bluetooth: Store CSRK Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 08/12] android/bluetooth: Read CSRK from the storage on startup Lukasz Rymanowski
2014-05-22 19:06 ` Lukasz Rymanowski [this message]
2014-05-22 19:06 ` [PATCH v3 10/12] android/gatt: Add crypto needed for sign write Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 11/12] android/gatt: Add support for signed write command Lukasz Rymanowski
2014-05-22 19:06 ` [PATCH v3 12/12] android/gatt: Add handling signed write from remote device Lukasz Rymanowski
2014-05-26  8:41 ` [PATCH v3 00/12] android/gatt: Add support for write signature command Szymon Janc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1400785586-22710-10-git-send-email-lukasz.rymanowski@tieto.com \
    --to=lukasz.rymanowski@tieto.com \
    --cc=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=szymon.janc@tieto.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox