nimble/att: Add mimimum encryption key size for attribute

Each attribute in database can have minimum encryption key size which
is required to access this attribute.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/7cd398d8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/7cd398d8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/7cd398d8

Branch: refs/heads/develop
Commit: 7cd398d8a70052849b2195a1bfb4bb25f805c238
Parents: a9deae1
Author: Andrzej Kaczmarek <[email protected]>
Authored: Tue Jan 3 15:24:12 2017 +0100
Committer: Andrzej Kaczmarek <[email protected]>
Committed: Wed Jan 4 14:14:24 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gatt.h       |  6 ++++
 net/nimble/host/src/ble_att_priv.h            |  8 ++---
 net/nimble/host/src/ble_att_svr.c             | 13 +++++---
 net/nimble/host/src/ble_gatts.c               | 17 +++++-----
 net/nimble/host/test/src/ble_att_svr_test.c   | 36 +++++++++++-----------
 net/nimble/host/test/src/ble_gatt_conn_test.c |  4 +--
 6 files changed, 47 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7cd398d8/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h 
b/net/nimble/host/include/host/ble_gatt.h
index 072c168..5055beb 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -225,6 +225,9 @@ struct ble_gatt_chr_def {
     /** Specifies the set of permitted operations for this characteristic. */
     ble_gatt_chr_flags flags;
 
+    /** Specifies minimum required key size to access this characteristic. */
+    uint8_t min_key_size;
+
     /** 
      * At registration time, this is filled in with the characteristic's value
      * attribute handle.
@@ -271,6 +274,9 @@ struct ble_gatt_dsc_def {
     /** Specifies the set of permitted operations for this descriptor. */
     uint8_t att_flags;
 
+    /** Specifies minimum required key size to access this descriptor. */
+    uint8_t min_key_size;
+
     /** Callback that gets executed when the descriptor is read or written. */
     ble_gatt_access_fn *access_cb;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7cd398d8/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_priv.h 
b/net/nimble/host/src/ble_att_priv.h
index 5a0eb35..44e4d23 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -140,18 +140,18 @@ typedef int ble_att_svr_access_fn(uint16_t conn_handle, 
uint16_t attr_handle,
                                   struct os_mbuf **om, void *arg);
 
 int ble_att_svr_register(const uint8_t *uuid, uint8_t flags,
-                         uint16_t *handle_id,
+                         uint8_t min_key_size, uint16_t *handle_id,
                          ble_att_svr_access_fn *cb, void *cb_arg);
 int ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags,
-                                uint16_t *handle_id, ble_att_svr_access_fn *cb,
-                                void *cb_arg);
+                                uint8_t min_key_size, uint16_t *handle_id,
+                                ble_att_svr_access_fn *cb, void *cb_arg);
 
 struct ble_att_svr_entry {
     STAILQ_ENTRY(ble_att_svr_entry) ha_next;
 
     uint8_t ha_uuid[16];
     uint8_t ha_flags;
-    uint8_t ha_pad1;
+    uint8_t ha_min_key_size;
     uint16_t ha_handle_id;
     ble_att_svr_access_fn *ha_cb;
     void *ha_cb_arg;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7cd398d8/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c 
b/net/nimble/host/src/ble_att_svr.c
index 3d85bd1..47a4cff 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -97,8 +97,9 @@ ble_att_svr_next_id(void)
  * @return 0 on success, non-zero error code on failure.
  */
 int
-ble_att_svr_register(const uint8_t *uuid, uint8_t flags, uint16_t *handle_id,
-                     ble_att_svr_access_fn *cb, void *cb_arg)
+ble_att_svr_register(const uint8_t *uuid, uint8_t flags, uint8_t min_key_size,
+                     uint16_t *handle_id,  ble_att_svr_access_fn *cb,
+                     void *cb_arg)
 {
     struct ble_att_svr_entry *entry;
 
@@ -109,6 +110,7 @@ ble_att_svr_register(const uint8_t *uuid, uint8_t flags, 
uint16_t *handle_id,
 
     memcpy(&entry->ha_uuid, uuid, sizeof entry->ha_uuid);
     entry->ha_flags = flags;
+    entry->ha_min_key_size = min_key_size;
     entry->ha_handle_id = ble_att_svr_next_id();
     entry->ha_cb = cb;
     entry->ha_cb_arg = cb_arg;
@@ -124,8 +126,8 @@ ble_att_svr_register(const uint8_t *uuid, uint8_t flags, 
uint16_t *handle_id,
 
 int
 ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags,
-                            uint16_t *handle_id, ble_att_svr_access_fn *cb,
-                            void *cb_arg)
+                            uint8_t min_key_size, uint16_t *handle_id,
+                            ble_att_svr_access_fn *cb, void *cb_arg)
 {
     uint8_t uuid128[16];
     int rc;
@@ -135,7 +137,8 @@ ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags,
         return rc;
     }
 
-    rc = ble_att_svr_register(uuid128, flags, handle_id, cb, cb_arg);
+    rc = ble_att_svr_register(uuid128, flags, min_key_size, handle_id, cb,
+                              cb_arg);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7cd398d8/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 9801f1b..4a5ddfb 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -423,7 +423,7 @@ ble_gatts_register_inc(struct ble_gatts_svc_entry *entry)
     BLE_HS_DBG_ASSERT(entry->handle != 0);
     BLE_HS_DBG_ASSERT(entry->end_group_handle != 0xffff);
 
-    rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_INCLUDE, BLE_ATT_F_READ,
+    rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_INCLUDE, BLE_ATT_F_READ, 0,
                                      &handle, ble_gatts_inc_access, entry);
     if (rc != 0) {
         return rc;
@@ -516,8 +516,8 @@ ble_gatts_register_dsc(const struct ble_gatt_svc_def *svc,
         return BLE_HS_EINVAL;
     }
 
-    rc = ble_att_svr_register(dsc->uuid128, dsc->att_flags, &dsc_handle,
-                              ble_gatts_dsc_access, (void *)dsc);
+    rc = ble_att_svr_register(dsc->uuid128, dsc->att_flags, dsc->min_key_size,
+                              &dsc_handle, ble_gatts_dsc_access, (void *)dsc);
     if (rc != 0) {
         return rc;
     }
@@ -752,7 +752,7 @@ ble_gatts_register_clt_cfg_dsc(uint16_t *att_handle)
         return rc;
     }
 
-    rc = ble_att_svr_register(uuid128, BLE_ATT_F_READ | BLE_ATT_F_WRITE,
+    rc = ble_att_svr_register(uuid128, BLE_ATT_F_READ | BLE_ATT_F_WRITE, 0,
                               att_handle, ble_gatts_clt_cfg_access, NULL);
     if (rc != 0) {
         return rc;
@@ -791,7 +791,7 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc,
      * callback arg).
      */
     rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_CHARACTERISTIC,
-                                     BLE_ATT_F_READ, &def_handle,
+                                     BLE_ATT_F_READ, 0, &def_handle,
                                      ble_gatts_chr_def_access, (void *)chr);
     if (rc != 0) {
         return rc;
@@ -801,8 +801,9 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc,
      * arg).
      */
     att_flags = ble_gatts_att_flags_from_chr_flags(chr->flags);
-    rc = ble_att_svr_register(chr->uuid128, att_flags, &val_handle,
-                              ble_gatts_chr_val_access, (void *)chr);
+    rc = ble_att_svr_register(chr->uuid128, att_flags, chr->min_key_size,
+                              &val_handle, ble_gatts_chr_val_access,
+                              (void *)chr);
     if (rc != 0) {
         return rc;
     }
@@ -907,7 +908,7 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
     /* Register service definition attribute (cast away const on callback
      * arg).
      */
-    rc = ble_att_svr_register_uuid16(uuid16, BLE_ATT_F_READ, out_handle,
+    rc = ble_att_svr_register_uuid16(uuid16, BLE_ATT_F_READ, 0, out_handle,
                                      ble_gatts_svc_access, (void *)svc);
     if (rc != 0) {
         return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7cd398d8/net/nimble/host/test/src/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_att_svr_test.c 
b/net/nimble/host/test/src/ble_att_svr_test.c
index 25b6a1d..c24693f 100644
--- a/net/nimble/host/test/src/ble_att_svr_test.c
+++ b/net/nimble/host/test/src/ble_att_svr_test.c
@@ -213,7 +213,7 @@ ble_att_svr_test_misc_register_uuid128(uint8_t *uuid128, 
uint8_t flags,
     uint16_t handle;
     int rc;
 
-    rc = ble_att_svr_register(uuid128, flags, &handle, fn, NULL);
+    rc = ble_att_svr_register(uuid128, flags, 0, &handle, fn, NULL);
     TEST_ASSERT_FATAL(rc == 0);
     TEST_ASSERT_FATAL(handle == expected_handle);
 }
@@ -804,7 +804,7 @@ TEST_CASE(ble_att_svr_test_read)
     /*** Successful read. */
     ble_att_svr_test_attr_r_1 = (uint8_t[]){0,1,2,3,4,5,6,7};
     ble_att_svr_test_attr_r_1_len = 8;
-    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, &attr_handle,
+    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, 0, &attr_handle,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -826,7 +826,7 @@ TEST_CASE(ble_att_svr_test_read)
 
     /*** Read requires encryption. */
     /* Insufficient authentication. */
-    rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_READ | BLE_ATT_F_READ_ENC,
+    rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_READ | BLE_ATT_F_READ_ENC, 0,
                               &attr_handle,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
@@ -880,7 +880,7 @@ TEST_CASE(ble_att_svr_test_read_blob)
         (uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
                     22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
     ble_att_svr_test_attr_r_1_len = 40;
-    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, &attr_handle,
+    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, 0, &attr_handle,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -932,12 +932,12 @@ TEST_CASE(ble_att_svr_test_read_mult)
     ble_att_svr_test_attr_r_2 = attrs[1].value;
     ble_att_svr_test_attr_r_2_len = attrs[1].value_len;
 
-    rc = ble_att_svr_register(BLE_UUID16(0x1111), HA_FLAG_PERM_RW,
+    rc = ble_att_svr_register(BLE_UUID16(0x1111), HA_FLAG_PERM_RW, 0,
                               &attrs[0].handle,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
-    rc = ble_att_svr_register(BLE_UUID16(0x2222), HA_FLAG_PERM_RW,
+    rc = ble_att_svr_register(BLE_UUID16(0x2222), HA_FLAG_PERM_RW, 0,
                               &attrs[1].handle,
                               ble_att_svr_test_misc_attr_fn_r_2, NULL);
     TEST_ASSERT(rc == 0);
@@ -1005,7 +1005,7 @@ TEST_CASE(ble_att_svr_test_write)
 
     /*** Write not permitted if non-local. */
     /* Non-local write (fail). */
-    rc = ble_att_svr_register(uuid_r, BLE_ATT_F_READ, &attr_handle,
+    rc = ble_att_svr_register(uuid_r, BLE_ATT_F_READ, 0, &attr_handle,
                               ble_att_svr_test_misc_attr_fn_w_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1026,7 +1026,7 @@ TEST_CASE(ble_att_svr_test_write)
     TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
 
     /*** Successful write. */
-    rc = ble_att_svr_register(uuid_rw, HA_FLAG_PERM_RW, &attr_handle,
+    rc = ble_att_svr_register(uuid_rw, HA_FLAG_PERM_RW, 0, &attr_handle,
                               ble_att_svr_test_misc_attr_fn_w_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1038,7 +1038,7 @@ TEST_CASE(ble_att_svr_test_write)
     /*** Write requires encryption. */
     /* Insufficient authentication. */
     rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC,
-                              &attr_handle,
+                              0, &attr_handle,
                               ble_att_svr_test_misc_attr_fn_w_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1105,7 +1105,7 @@ TEST_CASE(ble_att_svr_test_find_info)
         BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
 
     /*** Range too late. */
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle1,
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, 0, &handle1,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1126,7 +1126,7 @@ TEST_CASE(ble_att_svr_test_find_info)
         } }));
 
     /*** Two 128-bit entries. */
-    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle2,
+    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, 0, &handle2,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1144,7 +1144,7 @@ TEST_CASE(ble_att_svr_test_find_info)
         } }));
 
     /*** Two 128-bit entries; 16-bit entry doesn't get sent. */
-    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, &handle3,
+    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, 0, &handle3,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1227,7 +1227,7 @@ TEST_CASE(ble_att_svr_test_find_type_value)
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 
     /*** Range too late. */
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle1,
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, 0, &handle1,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1253,7 +1253,7 @@ TEST_CASE(ble_att_svr_test_find_type_value)
         } }));
 
     /*** One entry, two attributes. */
-    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle2,
+    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, 0, &handle2,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 
@@ -1274,19 +1274,19 @@ TEST_CASE(ble_att_svr_test_find_type_value)
     ble_att_svr_test_attr_r_2 = (uint8_t[]){0x00, 0x00};
     ble_att_svr_test_attr_r_2_len = 2;
 
-    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, &handle_desc,
+    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, 0, &handle_desc,
                               ble_att_svr_test_misc_attr_fn_r_2, NULL);
     TEST_ASSERT(rc == 0);
 
-    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle3,
+    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, 0, &handle3,
                               ble_att_svr_test_misc_attr_fn_r_2, NULL);
     TEST_ASSERT(rc == 0);
 
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle4,
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, 0, &handle4,
                               ble_att_svr_test_misc_attr_fn_r_2, NULL);
     TEST_ASSERT(rc == 0);
 
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle5,
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, 0, &handle5,
                               ble_att_svr_test_misc_attr_fn_r_1, NULL);
     TEST_ASSERT(rc == 0);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7cd398d8/net/nimble/host/test/src/ble_gatt_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_conn_test.c 
b/net/nimble/host/test/src/ble_gatt_conn_test.c
index a0c4f9f..4add074 100644
--- a/net/nimble/host/test/src/ble_gatt_conn_test.c
+++ b/net/nimble/host/test/src/ble_gatt_conn_test.c
@@ -389,7 +389,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect)
     ble_gatt_conn_test_util_init();
 
     /*** Register an attribute to allow indicatations to be sent. */
-    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ,
+    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ, 0,
                               &attr_handle,
                               ble_gatt_conn_test_attr_cb, NULL);
     TEST_ASSERT(rc == 0);
@@ -614,7 +614,7 @@ TEST_CASE(ble_gatt_conn_test_timeout)
     TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
 
     /*** Register an attribute to allow indicatations to be sent. */
-    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ,
+    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ, 0,
                               &attr_handle,
                               ble_gatt_conn_test_attr_cb, NULL);
     TEST_ASSERT(rc == 0);

Reply via email to