Expose looking up slot flags, and a few other helpful functions.
authorStefan Walter <stefw@src.gnome.org>
Tue, 20 Jan 2009 16:41:14 +0000 (16:41 +0000)
committerStefan Walter <stefw@src.gnome.org>
Tue, 20 Jan 2009 16:41:14 +0000 (16:41 +0000)
* gp11/gp11.h:
* gp11/gp11-misc.c:
* gp11/gp11-module.c:
* gp11/gp11-private.h:
* gp11/gp11-session.c:
* gp11/gp11-slot.c: Expose looking up slot flags,
and a few other helpful functions.

svn path=/trunk/; revision=1469

gp11/gp11-misc.c
gp11/gp11-module.c
gp11/gp11-private.h
gp11/gp11-session.c
gp11/gp11-slot.c
gp11/gp11.h

index 1501757..11e9c06 100644 (file)
@@ -24,6 +24,7 @@
 #include "config.h"
 
 #include "gp11.h"
+#include "gp11-private.h"
 
 #include <glib/gi18n.h>
 
@@ -67,6 +68,27 @@ gp11_list_unref_free (GList *reflist)
 }
 
 /**
+ * gp11_list_ref_copy:
+ * reflist: List of GObject reference counted objects.
+ *
+ * Copy a list of GObject based pointers. All objects
+ * in the list will be reffed and the list will be copied.
+ *
+ * Return value: The copied and reffed list. When done, free it with
+ * gp11_list_unref_free ()
+ */
+GList*
+gp11_list_ref_copy (GList *reflist)
+{
+       GList *l, *copy = g_list_copy (reflist);
+       for (l = copy; l; l = g_list_next (l)) {
+               g_return_val_if_fail (G_IS_OBJECT (l->data), NULL);
+               g_object_ref (l->data);
+       }
+       return copy;
+}
+
+/**
  * gp11_message_from_rv:
  * rv: The PKCS#11 return value to get a message for.
  *
@@ -280,3 +302,21 @@ gp11_string_from_chars (const guchar *data, gsize max)
        g_strchomp (string);
        return string;
 }
+
+guint
+_gp11_ulong_hash (gconstpointer v)
+{
+       const signed char *p = v;
+       guint32 i, h = *p;
+
+       for(i = 0; i < sizeof (gulong); ++i)
+               h = (h << 5) - h + *(p++);
+
+       return h;
+}
+
+gboolean
+_gp11_ulong_equal (gconstpointer v1, gconstpointer v2)
+{
+       return *((const gulong*)v1) == *((const gulong*)v2);
+}
index fe39e75..5963475 100644 (file)
@@ -65,7 +65,6 @@ typedef struct _GP11ModulePrivate {
        gboolean finalized;
        GHashTable *open_sessions;
        gboolean auto_authenticate;
-       GP11TokenInfo *token_info;
 } GP11ModulePrivate;
 
 #define GP11_MODULE_GET_DATA(o) \
@@ -86,24 +85,6 @@ typedef struct _SessionPool {
  * HELPERS
  */
 
-static guint
-ulong_hash (gconstpointer v)
-{
-       const signed char *p = v;
-       guint32 i, h = *p;
-
-       for(i = 0; i < sizeof (gulong); ++i)
-               h = (h << 5) - h + *(p++);
-
-       return h;
-}
-
-static gboolean
-ulong_equal (gconstpointer v1, gconstpointer v2)
-{
-       return *((const gulong*)v1) == *((const gulong*)v2);
-}
-
 static CK_RV
 create_mutex (void **mutex)
 {
@@ -302,7 +283,7 @@ static void
 create_session_table (GP11ModulePrivate *pv)
 {
        if (!pv->open_sessions)
-               pv->open_sessions = g_hash_table_new_full (ulong_hash, ulong_equal, g_free, free_session_pool);
+               pv->open_sessions = g_hash_table_new_full (_gp11_ulong_hash, _gp11_ulong_equal, g_free, free_session_pool);
 }
 
 CK_SESSION_HANDLE
index 713ebef..e323ca1 100644 (file)
@@ -50,6 +50,15 @@ CK_ATTRIBUTE_PTR    _gp11_attributes_commit_out             (GP11Attributes *att
                                                              CK_ULONG_PTR n_attrs);
 
 /* ----------------------------------------------------------------------------
+ * MISC
+ */
+
+guint               _gp11_ulong_hash                        (gconstpointer v);
+
+gboolean            _gp11_ulong_equal                       (gconstpointer v1,
+                                                             gconstpointer v2);
+
+/* ----------------------------------------------------------------------------
  * MODULE
  */
 
@@ -75,7 +84,8 @@ CK_SESSION_HANDLE   _gp11_module_pooled_session_handle      (GP11Module *module,
  * SLOT
  */
 
-gboolean            _gp11_slot_is_protected_auth_path       (GP11Slot *slot);
+GP11Object*         _gp11_slot_object_from_handle           (GP11Slot *slot,
+                                                             CK_OBJECT_HANDLE handle);
 
 /* ----------------------------------------------------------------------------
  * CALL
index fe84a56..737ead3 100644 (file)
@@ -1138,7 +1138,7 @@ authenticate_init (Authenticate *auth, GP11Slot *slot, GP11Object *object)
        module = gp11_slot_get_module (slot);
        if (gp11_module_get_auto_authenticate (module)) {
                auth->state = AUTHENTICATE_CAN;
-               auth->protected_auth = _gp11_slot_is_protected_auth_path (slot);
+               auth->protected_auth = gp11_slot_has_flags (slot, CKF_PROTECTED_AUTHENTICATION_PATH);
                auth->module = module;
                auth->object = g_object_ref (object);
        } else {
index ecb45f7..ee14ea2 100644 (file)
@@ -199,25 +199,6 @@ gp11_slot_class_init (GP11SlotClass *klass)
 }
 
 /* ----------------------------------------------------------------------------
- * INTERNAL AUTHENTICATION
- */
-
-gboolean
-_gp11_slot_is_protected_auth_path (GP11Slot *self)
-{
-       GP11TokenInfo *info;
-       gboolean ret;
-
-       g_assert (GP11_IS_SLOT (self));
-
-       info = gp11_slot_get_token_info (self);
-       ret = (info && info->flags & CKF_PROTECTED_AUTHENTICATION_PATH);
-       gp11_token_info_free (info);
-
-       return ret;
-}
-
-/* ----------------------------------------------------------------------------
  * PUBLIC
  */
 
@@ -576,6 +557,36 @@ gp11_slot_get_mechanism_info (GP11Slot *self, gulong mech_type)
        return mechinfo;
 }
 
+gboolean
+gp11_slot_has_flags (GP11Slot *self, gulong flags)
+{
+       CK_FUNCTION_LIST_PTR funcs;
+       GP11Module *module = NULL;
+       CK_TOKEN_INFO info;
+       CK_SLOT_ID handle;
+       CK_RV rv;
+
+       g_return_val_if_fail (GP11_IS_SLOT (self), FALSE);
+
+       g_object_get (self, "module", &module, "handle", &handle, NULL);
+       g_return_val_if_fail (GP11_IS_MODULE (module), FALSE);
+
+       funcs = gp11_module_get_functions (module);
+       g_return_val_if_fail (funcs, FALSE);
+
+       memset (&info, 0, sizeof (info));
+       rv = (funcs->C_GetTokenInfo) (handle, &info);
+
+       g_object_unref (module);
+
+       if (rv != CKR_OK) {
+               g_warning ("couldn't get slot info: %s", gp11_message_from_rv (rv));
+               return FALSE;
+       }
+
+       return (info.flags & flags) != 0;
+}
+
 #if UNIMPLEMENTED
 
 typedef struct InitToken {
index 78c735e..90990f8 100644 (file)
@@ -41,6 +41,8 @@ G_BEGIN_DECLS
 
 GQuark              gp11_get_error_quark                    (void);
 
+GList*              gp11_list_ref_copy                      (GList *reflist);
+
 void                gp11_list_unref_free                    (GList *reflist);
 
 const gchar*        gp11_message_from_rv                    (CK_RV rv);
@@ -441,6 +443,9 @@ GP11Mechanisms*     gp11_slot_get_mechanisms                (GP11Slot *self);
 GP11MechanismInfo*  gp11_slot_get_mechanism_info            (GP11Slot *self,
                                                              gulong mech_type);
 
+gboolean            gp11_slot_has_flags                     (GP11Slot *self,
+                                                             gulong flags);
+
 #if UNIMPLEMENTED
 
 gboolean            gp11_slot_init_token                    (GP11Slot *self,