[Cryptech-Commits] [sw/libhal] 03/03: Shake first round of bugs out of hal_rpc_pkey_match().

git at cryptech.is git at cryptech.is
Wed Oct 19 06:05:41 UTC 2016


This is an automated email from the git hooks/post-receive script.

sra at hactrn.net pushed a commit to branch ksng
in repository sw/libhal.

commit b252694ce6aafcdec2dd268196029f17ab0c6335
Author: Rob Austein <sra at hactrn.net>
AuthorDate: Wed Oct 19 02:00:43 2016 -0400

    Shake first round of bugs out of hal_rpc_pkey_match().
    
    The filtering code for this function has not been tested yet.
---
 ks_flash.c    |  6 +++++-
 ks_index.c    |  5 ++---
 ks_volatile.c |  6 +++++-
 rpc_server.c  | 24 ++++++++++++++----------
 4 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/ks_flash.c b/ks_flash.c
index 9f0a03e..7c96f90 100644
--- a/ks_flash.c
+++ b/ks_flash.c
@@ -1170,7 +1170,11 @@ static hal_error_t ks_match(hal_ks_t *ks,
 
   *result_len = 0;
 
-  if ((err = hal_ks_index_find(&db.ksi, previous_uuid, 0, NULL, &i)) != HAL_OK)
+  err = hal_ks_index_find(&db.ksi, previous_uuid, 0, NULL, &i);
+
+  if (err == HAL_ERROR_KEY_NOT_FOUND)
+    i--;
+  else if (err != HAL_OK)
     return err;
 
   while (*result_len < result_max) {
diff --git a/ks_index.c b/ks_index.c
index 5bf2ce5..23ba8c1 100644
--- a/ks_index.c
+++ b/ks_index.c
@@ -171,8 +171,7 @@ hal_error_t hal_ks_index_find(hal_ks_index_t *ksi,
 
   int where;
 
-  if (!ks_find(ksi, name, chunk, hint, &where))
-    return HAL_ERROR_KEY_NOT_FOUND;
+  int ok = ks_find(ksi, name, chunk, hint, &where);
 
   if (blockno != NULL)
     *blockno = ksi->index[where];
@@ -180,7 +179,7 @@ hal_error_t hal_ks_index_find(hal_ks_index_t *ksi,
   if (hint != NULL)
     *hint = where;
 
-  return HAL_OK;
+  return ok ? HAL_OK : HAL_ERROR_KEY_NOT_FOUND;
 }
 
 hal_error_t hal_ks_index_find_range(hal_ks_index_t *ksi,
diff --git a/ks_volatile.c b/ks_volatile.c
index df26471..2018adc 100644
--- a/ks_volatile.c
+++ b/ks_volatile.c
@@ -396,7 +396,11 @@ static hal_error_t ks_match(hal_ks_t *ks,
 
   *result_len = 0;
 
-  if ((err = hal_ks_index_find(&ksv->db->ksi, previous_uuid, 0, NULL, &i)) != HAL_OK)
+  err = hal_ks_index_find(&ksv->db->ksi, previous_uuid, 0, NULL, &i);
+
+  if (err == HAL_ERROR_KEY_NOT_FOUND)
+    i--;
+  else if (err != HAL_OK)
     return err;
 
   while (*result_len < result_max) {
diff --git a/rpc_server.c b/rpc_server.c
index d9e640a..18f6823 100644
--- a/rpc_server.c
+++ b/rpc_server.c
@@ -672,6 +672,7 @@ static hal_error_t pkey_match(const uint8_t **iptr, const uint8_t * const ilimit
     hal_session_handle_t session;
     uint32_t type, curve, attributes_len, result_max, previous_uuid_len;
     const uint8_t *previous_uuid_ptr;
+    hal_uuid_t previous_uuid;
     hal_key_flags_t flags;
     hal_error_t ret;
 
@@ -695,27 +696,30 @@ static hal_error_t pkey_match(const uint8_t **iptr, const uint8_t * const ilimit
     check(hal_xdr_decode_int(iptr, ilimit, &result_max));
     check(hal_xdr_decode_buffer_in_place(iptr, ilimit, &previous_uuid_ptr, &previous_uuid_len));
 
-    if (previous_uuid_len != sizeof(hal_uuid_t))
+    if (previous_uuid_len != sizeof(previous_uuid.uuid))
         return HAL_ERROR_KEY_NAME_TOO_LONG;
 
+    memcpy(previous_uuid.uuid, previous_uuid_ptr, sizeof(previous_uuid.uuid));
+
     hal_uuid_t result[result_max];
     unsigned result_len;
 
     ret = hal_rpc_local_pkey_dispatch.match(client, session, type, curve, flags,
                                             attributes, attributes_len,
                                             result, &result_len, result_max,
-                                            (hal_uuid_t *) previous_uuid_ptr);
+                                            &previous_uuid);
 
     if (ret == HAL_OK) {
         uint8_t *optr_orig = *optr;
-        check(hal_xdr_encode_int(optr, olimit, result_len));
-        for (int i = 0; i < result_len; ++i) {
-            if ((ret = hal_xdr_encode_buffer(optr, olimit, result[i].uuid,
-                                             sizeof(result[i].uuid))) != HAL_OK) {
-                *optr = optr_orig;
-                break;
-            }
-        }
+        ret = hal_xdr_encode_int(optr, olimit, result_len);
+        for (int i = 0; ret == HAL_OK && i < result_len; ++i)
+            ret = hal_xdr_encode_buffer(optr, olimit, result[i].uuid,
+                                        sizeof(result[i].uuid));
+        if (ret == HAL_OK)
+            ret = hal_xdr_encode_buffer(optr, olimit, previous_uuid.uuid,
+                                        sizeof(previous_uuid.uuid));
+        if (ret != HAL_OK)
+            *optr = optr_orig;
     }
 
     return ret;



More information about the Commits mailing list