[gnome-keyring] [gp11] Implement C_InitPIN and C_SetPIN in gp11 library.
- From: Stefan Walter <stefw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-keyring] [gp11] Implement C_InitPIN and C_SetPIN in gp11 library.
- Date: Thu, 17 Dec 2009 05:33:02 +0000 (UTC)
commit 72fdb68afd13f424eb1ad73d62422940fdb3ed33
Author: Stef Walter <stef memberwebs com>
Date: Wed Dec 16 03:58:22 2009 +0000
[gp11] Implement C_InitPIN and C_SetPIN in gp11 library.
gp11/gp11-session.c | 240 ++++++++++++++++++++++++++++++++++-
gp11/gp11.h | 20 +++-
gp11/tests/gp11-test-module.c | 31 ++++-
gp11/tests/unit-test-gp11-session.c | 34 +++++
4 files changed, 317 insertions(+), 8 deletions(-)
---
diff --git a/gp11/gp11-session.c b/gp11/gp11-session.c
index e8bb807..f03bfa9 100644
--- a/gp11/gp11-session.c
+++ b/gp11/gp11-session.c
@@ -472,9 +472,247 @@ gp11_session_get_info (GP11Session *self)
return sessioninfo;
}
+/* ---------------------------------------------------------------------------------------------
+ * INIT PIN
+ */
+
+typedef struct _InitPin {
+ GP11Arguments base;
+ guchar *pin;
+ gsize n_pin;
+} InitPin;
+
+
+static void
+free_init_pin (InitPin *args)
+{
+ g_free (args->pin);
+ g_free (args);
+}
+static CK_RV
+perform_init_pin (InitPin *args)
+{
+ return (args->base.pkcs11->C_InitPIN) (args->base.handle, (CK_BYTE_PTR)args->pin,
+ args->n_pin);
+}
+
+/**
+ * gp11_session_init_pin:
+ * @self: Initialize PIN for this session's slot.
+ * @pin: The user's PIN, or NULL for protected authentication path.
+ * @n_pin: The length of the PIN.
+ * @err: A location to return an error.
+ *
+ * Initialize the user's pin on this slot that this session is opened on.
+ * According to the PKCS#11 standards, the session must be logged in with
+ * the CKU_SO user type.
+ *
+ * This call may block for an indefinite period.
+ *
+ * Return value: Whether successful or not.
+ **/
+gboolean
+gp11_session_init_pin (GP11Session *self, const guchar *pin, gsize n_pin,
+ GError **err)
+{
+ return gp11_session_init_pin_full (self, pin, n_pin, NULL, err);
+}
-/* LOGIN */
+/**
+ * gp11_session_init_pin_full:
+ * @self: Initialize PIN for this session's slot.
+ * @pin: The user's PIN, or NULL for protected authentication path.
+ * @n_pin: The length of the PIN.
+ * @cancellable: Optional cancellation object, or NULL.
+ * @err: A location to return an error.
+ *
+ * Initialize the user's pin on this slot that this session is opened on.
+ * According to the PKCS#11 standards, the session must be logged in with
+ * the CKU_SO user type.
+ *
+ * This call may block for an indefinite period.
+ *
+ * Return value: Whether successful or not.
+ **/
+gboolean
+gp11_session_init_pin_full (GP11Session *self, const guchar *pin, gsize n_pin,
+ GCancellable *cancellable, GError **err)
+{
+ InitPin args = { GP11_ARGUMENTS_INIT, (guchar*)pin, n_pin };
+ return _gp11_call_sync (self, perform_init_pin, NULL, &args, cancellable, err);
+
+}
+
+/**
+ * gp11_session_init_pin_async:
+ * @self: Initialize PIN for this session's slot.
+ * @pin: The user's PIN, or NULL for protected authentication path.
+ * @n_pin: The length of the PIN.
+ * @cancellable: Optional cancellation object, or NULL.
+ * @callback: Called when the operation completes.
+ * @user_data: Data to pass to the callback.
+ *
+ * Initialize the user's pin on this slot that this session is opened on.
+ * According to the PKCS#11 standards, the session must be logged in with
+ * the CKU_SO user type.
+ *
+ * This call will return immediately and completes asynchronously.
+ **/
+void
+gp11_session_init_pin_async (GP11Session *self, const guchar *pin, gsize n_pin,
+ GCancellable *cancellable, GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ InitPin* args = _gp11_call_async_prep (self, self, perform_init_pin, NULL, sizeof (*args), free_init_pin);
+
+ args->pin = pin && n_pin ? g_memdup (pin, n_pin) : NULL;
+ args->n_pin = n_pin;
+
+ _gp11_call_async_ready_go (args, cancellable, callback, user_data);
+}
+
+/**
+ * gp11_session_init_pin_finish:
+ * @self: The session.
+ * @result: The result passed to the callback.
+ * @err: A location to return an error.
+ *
+ * Get the result of initializing a user's PIN.
+ *
+ * Return value: Whether the operation was successful or not.
+ **/
+gboolean
+gp11_session_init_pin_finish (GP11Session *self, GAsyncResult *result, GError **err)
+{
+ return _gp11_call_basic_finish (result, err);
+}
+
+
+/* ---------------------------------------------------------------------------------------------
+ * SET PIN
+ */
+
+typedef struct _SetPin {
+ GP11Arguments base;
+ guchar *old_pin;
+ gsize n_old_pin;
+ guchar *new_pin;
+ gsize n_new_pin;
+} SetPin;
+
+static void
+free_set_pin (SetPin *args)
+{
+ g_free (args->old_pin);
+ g_free (args->new_pin);
+ g_free (args);
+}
+
+static CK_RV
+perform_set_pin (SetPin *args)
+{
+ return (args->base.pkcs11->C_SetPIN) (args->base.handle, (CK_BYTE_PTR)args->old_pin,
+ args->n_old_pin, args->new_pin, args->n_new_pin);
+}
+
+/**
+ * gp11_session_set_pin:
+ * @self: Change the PIN for this session's slot.
+ * @old_pin: The user's old PIN, or NULL for protected authentication path.
+ * @n_old_pin: The length of the PIN.
+ * @new_pin: The user's new PIN, or NULL for protected authentication path.
+ * @n_new_pin: The length of the PIN.
+ * @err: A location to return an error.
+ *
+ * Change the user's pin on this slot that this session is opened on.
+ *
+ * This call may block for an indefinite period.
+ *
+ * Return value: Whether successful or not.
+ **/
+gboolean
+gp11_session_set_pin (GP11Session *self, const guchar *old_pin, gsize n_old_pin,
+ const guchar *new_pin, gsize n_new_pin, GError **err)
+{
+ return gp11_session_set_pin_full (self, old_pin, n_old_pin, new_pin, n_new_pin, NULL, err);
+}
+
+/**
+ * gp11_session_set_pin_full:
+ * @self: Change the PIN for this session's slot.
+ * @old_pin: The user's old PIN, or NULL for protected authentication path.
+ * @n_old_pin: The length of the PIN.
+ * @new_pin: The user's new PIN, or NULL for protected authentication path.
+ * @n_new_pin: The length of the PIN.
+ * @cancellable: Optional cancellation object, or NULL.
+ * @err: A location to return an error.
+ *
+ * Change the user's pin on this slot that this session is opened on.
+ *
+ * This call may block for an indefinite period.
+ *
+ * Return value: Whether successful or not.
+ **/
+gboolean
+gp11_session_set_pin_full (GP11Session *self, const guchar *old_pin, gsize n_old_pin,
+ const guchar *new_pin, gsize n_new_pin, GCancellable *cancellable,
+ GError **err)
+{
+ SetPin args = { GP11_ARGUMENTS_INIT, (guchar*)old_pin, n_old_pin, (guchar*)new_pin, n_new_pin };
+ return _gp11_call_sync (self, perform_set_pin, NULL, &args, cancellable, err);
+}
+
+/**
+ * gp11_session_set_pin_async:
+ * @self: Change the PIN for this session's slot.
+ * @old_pin: The user's old PIN, or NULL for protected authentication path.
+ * @n_old_pin: The length of the PIN.
+ * @new_pin: The user's new PIN, or NULL for protected authentication path.
+ * @n_new_pin: The length of the PIN.
+ * @cancellable: Optional cancellation object, or NULL.
+ * @callback: Called when the operation completes.
+ * @user_data: Data to pass to the callback.
+ *
+ * Change the user's pin on this slot that this session is opened on.
+ *
+ * This call will return immediately and completes asynchronously.
+ **/
+void
+gp11_session_set_pin_async (GP11Session *self, const guchar *old_pin, gsize n_old_pin,
+ const guchar *new_pin, gsize n_new_pin, GCancellable *cancellable,
+ GAsyncReadyCallback callback, gpointer user_data)
+{
+ SetPin* args = _gp11_call_async_prep (self, self, perform_set_pin, NULL, sizeof (*args), free_set_pin);
+
+ args->old_pin = old_pin && n_old_pin ? g_memdup (old_pin, n_old_pin) : NULL;
+ args->n_old_pin = n_old_pin;
+ args->new_pin = new_pin && n_new_pin ? g_memdup (new_pin, n_new_pin) : NULL;
+ args->n_new_pin = n_new_pin;
+
+ _gp11_call_async_ready_go (args, cancellable, callback, user_data);
+}
+
+/**
+ * gp11_session_set_pin_finish:
+ * @self: The session.
+ * @result: The result passed to the callback.
+ * @err: A location to return an error.
+ *
+ * Get the result of changing a user's PIN.
+ *
+ * Return value: Whether the operation was successful or not.
+ **/
+gboolean
+gp11_session_set_pin_finish (GP11Session *self, GAsyncResult *result, GError **err)
+{
+ return _gp11_call_basic_finish (result, err);
+}
+
+
+/* ---------------------------------------------------------------------------------------------
+ * LOGIN
+ */
typedef struct _Login {
GP11Arguments base;
diff --git a/gp11/gp11.h b/gp11/gp11.h
index 009ef38..f7c69d3 100644
--- a/gp11/gp11.h
+++ b/gp11/gp11.h
@@ -560,16 +560,21 @@ CK_SESSION_HANDLE gp11_session_get_handle (GP11Session *self);
GP11SessionInfo* gp11_session_get_info (GP11Session *self);
-#if UNIMPLEMENTED
-
gboolean gp11_session_init_pin (GP11Session *self,
const guchar *pin,
gsize n_pin,
GError **err);
+gboolean gp11_session_init_pin_full (GP11Session *self,
+ const guchar *pin,
+ gsize n_pin,
+ GCancellable *cancellable,
+ GError **err);
+
void gp11_session_init_pin_async (GP11Session *self,
const guchar *pin,
gsize n_pin,
+ GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
@@ -584,11 +589,20 @@ gboolean gp11_session_set_pin (GP11Session *self,
gsize n_new_pin,
GError **err);
+gboolean gp11_session_set_pin_full (GP11Session *self,
+ const guchar *old_pin,
+ gsize n_old_pin,
+ const guchar *new_pin,
+ gsize n_new_pin,
+ GCancellable *cancellable,
+ GError **err);
+
void gp11_session_set_pin_async (GP11Session *self,
const guchar *old_pin,
gsize n_old_pin,
const guchar *new_pin,
gsize n_new_pin,
+ GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
@@ -596,6 +610,8 @@ gboolean gp11_session_set_pin_finish (GP11Session *self,
GAsyncResult *result,
GError **err);
+#if UNIMPLEMENTED
+
guchar* gp11_session_get_operation_state (GP11Session *self,
gsize *n_result,
GError **err);
diff --git a/gp11/tests/gp11-test-module.c b/gp11/tests/gp11-test-module.c
index 41980b7..4c403fe 100644
--- a/gp11/tests/gp11-test-module.c
+++ b/gp11/tests/gp11-test-module.c
@@ -499,16 +499,37 @@ static CK_RV
test_C_InitPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin,
CK_ULONG ulPinLen)
{
- g_assert_not_reached (); /* Not yet used by library */
- return CKR_FUNCTION_NOT_SUPPORTED;
+ Session *session;
+
+ session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
+ g_assert (session != NULL && "No such session found");
+ if (!session)
+ return CKR_SESSION_HANDLE_INVALID;
+
+ g_free (the_pin);
+ the_pin = g_strndup ((gchar*)pPin, ulPinLen);
+ return CKR_OK;
}
static CK_RV
test_C_SetPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
- CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen)
+ CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen)
{
- g_assert_not_reached (); /* Not yet used by library */
- return CKR_FUNCTION_NOT_SUPPORTED;
+ Session *session;
+ gchar *old;
+
+ session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
+ g_assert (session != NULL && "No such session found");
+ if (!session)
+ return CKR_SESSION_HANDLE_INVALID;
+
+ old = g_strndup ((gchar*)pOldPin, ulOldLen);
+ if (!g_str_equal (old, the_pin))
+ return CKR_PIN_INCORRECT;
+
+ g_free (the_pin);
+ the_pin = g_strndup ((gchar*)pNewPin, ulNewLen);
+ return CKR_OK;
}
static CK_RV
diff --git a/gp11/tests/unit-test-gp11-session.c b/gp11/tests/unit-test-gp11-session.c
index 6141157..a909b97 100644
--- a/gp11/tests/unit-test-gp11-session.c
+++ b/gp11/tests/unit-test-gp11-session.c
@@ -160,6 +160,40 @@ DEFINE_TEST(open_reused)
}
+DEFINE_TEST(init_set_pin)
+{
+ GAsyncResult *result = NULL;
+ GError *err = NULL;
+ gboolean ret;
+
+ /* init pin */
+ ret = gp11_session_init_pin (session, (guchar*)"booo", 4, &err);
+ SUCCESS_RES (ret, err);
+
+ /* set pin */
+ ret = gp11_session_set_pin (session, (guchar*)"booo", 4, (guchar*)"tooo", 4, &err);
+ SUCCESS_RES (ret, err);
+
+ /* init pin async */
+ gp11_session_init_pin_async (session, (guchar*)"booo", 4, NULL, fetch_async_result, &result);
+ WAIT_UNTIL (result);
+ g_assert (result != NULL);
+ ret = gp11_session_init_pin_finish (session, result, &err);
+ SUCCESS_RES (ret, err);
+ g_object_unref (result);
+ result = NULL;
+
+ /* set pin async */
+ gp11_session_set_pin_async (session, (guchar*)"booo", 4, (guchar*)"tooo", 4, NULL, fetch_async_result, &result);
+ WAIT_UNTIL (result);
+ g_assert (result != NULL);
+ ret = gp11_session_set_pin_finish (session, result, &err);
+ SUCCESS_RES (ret, err);
+ g_object_unref (result);
+ result = NULL;
+}
+
+
DEFINE_TEST(login_logout)
{
GAsyncResult *result = NULL;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]