summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2013-08-28 09:45:21 +0200
committerStef Walter <stef@thewalter.net>2013-08-29 10:29:53 +0200
commit570403f3421b222167196d380c60eb8430eb4cd7 (patch)
tree5cd1351db179dfee24f0c586038fb13e7839414c
parent58466648aa84ea10c20213d4665c5c93dbf285e9 (diff)
downloadp11-kit-570403f3421b222167196d380c60eb8430eb4cd7.tar.gz
trust: Add index callback for when an object is removed
This allows a token to remove the file if desired
-rw-r--r--trust/index.c25
-rw-r--r--trust/index.h5
-rw-r--r--trust/session.c2
-rw-r--r--trust/tests/test-builder.c2
-rw-r--r--trust/tests/test-index.c99
-rw-r--r--trust/token.c1
6 files changed, 125 insertions, 9 deletions
diff --git a/trust/index.c b/trust/index.c
index 83a4503..9a193c5 100644
--- a/trust/index.c
+++ b/trust/index.c
@@ -80,6 +80,9 @@ struct _p11_index {
/* Called after each object ready to be stored */
p11_index_store_cb store;
+ /* Called after an object has been removed */
+ p11_index_remove_cb remove;
+
/* Called after objects change */
p11_index_notify_cb notify;
@@ -129,9 +132,18 @@ default_notify (void *data,
}
+static CK_RV
+default_remove (void *data,
+ p11_index *index,
+ CK_ATTRIBUTE *attrs)
+{
+ return CKR_OK;
+}
+
p11_index *
p11_index_new (p11_index_build_cb build,
p11_index_store_cb store,
+ p11_index_remove_cb remove,
p11_index_notify_cb notify,
void *data)
{
@@ -146,10 +158,13 @@ p11_index_new (p11_index_build_cb build,
store = default_store;
if (notify == NULL)
notify = default_notify;
+ if (remove == NULL)
+ remove = default_remove;
index->build = build;
index->store = store;
index->notify = notify;
+ index->remove = remove;
index->data = data;
index->objects = p11_dict_new (p11_dict_ulongptr_hash,
@@ -578,12 +593,22 @@ p11_index_remove (p11_index *index,
CK_OBJECT_HANDLE handle)
{
index_object *obj;
+ CK_RV rv;
return_val_if_fail (index != NULL, CKR_GENERAL_ERROR);
if (!p11_dict_steal (index->objects, &handle, NULL, (void **)&obj))
return CKR_OBJECT_HANDLE_INVALID;
+ rv = (index->remove) (index->data, index, obj->attrs);
+
+ /* If the writer failed the remove, then add it back */
+ if (rv != CKR_OK) {
+ if (!p11_dict_set (index->objects, &obj->handle, obj))
+ return_val_if_reached (CKR_HOST_MEMORY);
+ return rv;
+ }
+
/* This takes ownership of the attributes */
index_notify (index, handle, obj->attrs);
obj->attrs = NULL;
diff --git a/trust/index.h b/trust/index.h
index 192bfcd..3ae24a1 100644
--- a/trust/index.h
+++ b/trust/index.h
@@ -53,6 +53,10 @@ typedef CK_RV (* p11_index_store_cb) (void *data,
CK_OBJECT_HANDLE handle,
CK_ATTRIBUTE **attrs);
+typedef CK_RV (* p11_index_remove_cb) (void *data,
+ p11_index *index,
+ CK_ATTRIBUTE *attrs);
+
typedef void (* p11_index_notify_cb) (void *data,
p11_index *index,
CK_OBJECT_HANDLE handle,
@@ -60,6 +64,7 @@ typedef void (* p11_index_notify_cb) (void *data,
p11_index * p11_index_new (p11_index_build_cb build,
p11_index_store_cb store,
+ p11_index_remove_cb remove,
p11_index_notify_cb notify,
void *data);
diff --git a/trust/session.c b/trust/session.c
index 76a9acf..b93a5c3 100644
--- a/trust/session.c
+++ b/trust/session.c
@@ -61,7 +61,7 @@ p11_session_new (p11_token *token)
session->builder = p11_builder_new (P11_BUILDER_FLAG_NONE);
return_val_if_fail (session->builder, NULL);
- session->index = p11_index_new (p11_builder_build, NULL,
+ session->index = p11_index_new (p11_builder_build, NULL, NULL,
p11_builder_changed,
session->builder);
return_val_if_fail (session->index != NULL, NULL);
diff --git a/trust/tests/test-builder.c b/trust/tests/test-builder.c
index 6e061aa..5c3c15b 100644
--- a/trust/tests/test-builder.c
+++ b/trust/tests/test-builder.c
@@ -77,7 +77,7 @@ setup (void *unused)
test.builder = p11_builder_new (P11_BUILDER_FLAG_TOKEN);
assert_ptr_not_null (test.builder);
- test.index = p11_index_new (p11_builder_build, NULL, p11_builder_changed, test.builder);
+ test.index = p11_index_new (p11_builder_build, NULL, NULL, p11_builder_changed, test.builder);
assert_ptr_not_null (test.index);
}
diff --git a/trust/tests/test-index.c b/trust/tests/test-index.c
index 074ab2d..fc861b2 100644
--- a/trust/tests/test-index.c
+++ b/trust/tests/test-index.c
@@ -53,7 +53,7 @@ struct {
static void
setup (void *unused)
{
- test.index = p11_index_new (NULL, NULL, NULL, NULL);
+ test.index = p11_index_new (NULL, NULL, NULL, NULL, NULL);
assert_ptr_not_null (test.index);
}
@@ -688,7 +688,7 @@ test_replace_all_build_fails (void)
p11_index *index;
CK_RV rv;
- index = p11_index_new (on_index_build_fail, NULL, NULL, &match);
+ index = p11_index_new (on_index_build_fail, NULL, NULL, NULL, &match);
assert_ptr_not_null (index);
array = p11_array_new (p11_attrs_free);
@@ -745,7 +745,7 @@ test_build_populate (void)
p11_index *index;
CK_RV rv;
- index = p11_index_new (on_build_populate, NULL, NULL, "blah");
+ index = p11_index_new (on_build_populate, NULL, NULL, NULL, "blah");
assert_ptr_not_null (index);
rv = p11_index_add (index, original, 2, &handle);
@@ -808,7 +808,7 @@ test_build_fail (void)
p11_index *index;
CK_RV rv;
- index = p11_index_new (on_build_fail, NULL, NULL, "testo");
+ index = p11_index_new (on_build_fail, NULL, NULL, NULL, "testo");
assert_ptr_not_null (index);
rv = p11_index_add (index, okay, 2, &handle);
@@ -872,7 +872,7 @@ test_change_called (void)
p11_index *index;
CK_RV rv;
- index = p11_index_new (NULL, NULL, on_change_check, "change-check");
+ index = p11_index_new (NULL, NULL, NULL, on_change_check, "change-check");
assert_ptr_not_null (index);
on_change_removing = false;
@@ -917,7 +917,7 @@ test_change_batch (void)
p11_index *index;
CK_RV rv;
- index = p11_index_new (NULL, NULL, on_change_check, "change-check");
+ index = p11_index_new (NULL, NULL, NULL, on_change_check, "change-check");
assert_ptr_not_null (index);
on_change_batching = true;
@@ -1008,7 +1008,7 @@ test_change_nested (void)
p11_index *index;
CK_RV rv;
- index = p11_index_new (NULL, NULL, on_change_nested, "change-nested");
+ index = p11_index_new (NULL, NULL, NULL, on_change_nested, "change-nested");
assert_ptr_not_null (index);
on_change_called = 0;
@@ -1027,6 +1027,89 @@ test_change_nested (void)
p11_index_free (index);
}
+static CK_RV
+on_remove_callback (void *data,
+ p11_index *index,
+ CK_ATTRIBUTE *attrs)
+{
+ int *removed = data;
+ assert_ptr_not_null (removed);
+ assert_num_eq (*removed, 0);
+ *removed = 1;
+ return CKR_OK;
+}
+
+static void
+test_remove_callback (void)
+{
+ CK_ATTRIBUTE original[] = {
+ { CKA_LABEL, "yay", 3 },
+ { CKA_VALUE, "eight", 5 },
+ { CKA_INVALID }
+
+ };
+
+ CK_OBJECT_HANDLE handle;
+ p11_index *index;
+ int removed = 0;
+ CK_RV rv;
+
+ index = p11_index_new (NULL, NULL, on_remove_callback, NULL, &removed);
+ assert_ptr_not_null (index);
+
+ rv = p11_index_add (index, original, 2, &handle);
+ assert_num_eq (rv, CKR_OK);
+
+ assert_ptr_not_null (p11_index_lookup (index, handle));
+
+ rv = p11_index_remove (index, handle);
+ assert_num_eq (rv, CKR_OK);
+
+ assert_num_eq (removed, 1);
+ assert_ptr_eq (p11_index_lookup (index, handle), NULL);
+
+ p11_index_free (index);
+}
+
+static CK_RV
+on_remove_fail (void *data,
+ p11_index *index,
+ CK_ATTRIBUTE *attrs)
+{
+ assert_str_eq (data, "remove-fail");
+ return CKR_DEVICE_REMOVED;
+}
+
+static void
+test_remove_fail (void)
+{
+ CK_ATTRIBUTE original[] = {
+ { CKA_LABEL, "yay", 3 },
+ { CKA_VALUE, "eight", 5 },
+ { CKA_INVALID }
+
+ };
+
+ CK_OBJECT_HANDLE handle;
+ p11_index *index;
+ CK_RV rv;
+
+ index = p11_index_new (NULL, NULL, on_remove_fail, NULL, "remove-fail");
+ assert_ptr_not_null (index);
+
+ rv = p11_index_add (index, original, 2, &handle);
+ assert (rv == CKR_OK);
+
+ assert_ptr_not_null (p11_index_lookup (index, handle));
+
+ rv = p11_index_remove (index, handle);
+ assert_num_eq (rv, CKR_DEVICE_REMOVED);
+
+ assert_ptr_not_null (p11_index_lookup (index, handle));
+
+ p11_index_free (index);
+}
+
int
main (int argc,
char *argv[])
@@ -1054,6 +1137,8 @@ main (int argc,
p11_test (test_change_batch, "/index/change_batch");
p11_test (test_change_nested, "/index/change_nested");
p11_test (test_replace_all_build_fails, "/index/replace-all-build-fails");
+ p11_test (test_remove_callback, "/index/remove-callback");
+ p11_test (test_remove_fail, "/index/remove-fail");
return p11_test_run (argc, argv);
}
diff --git a/trust/token.c b/trust/token.c
index cfcdba6..427c1d5 100644
--- a/trust/token.c
+++ b/trust/token.c
@@ -742,6 +742,7 @@ p11_token_new (CK_SLOT_ID slot,
token->index = p11_index_new (on_index_build,
on_index_store,
+ NULL,
on_index_notify,
token);
return_val_if_fail (token->index != NULL, NULL);