diff options
Diffstat (limited to 'gck')
44 files changed, 0 insertions, 20336 deletions
diff --git a/gck/.gitignore b/gck/.gitignore deleted file mode 100644 index 4fbdec3a..00000000 --- a/gck/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/gck-*.pc -/gck.pc diff --git a/gck/Makefile.am b/gck/Makefile.am deleted file mode 100644 index 6a2a6c35..00000000 --- a/gck/Makefile.am +++ /dev/null @@ -1,161 +0,0 @@ - -SUBDIRS = . tests - -incdir = $(includedir)/gck-@GCK_MAJOR@/gck - -HEADER_FILES = \ - gck.h \ - gck-deprecated.h - -inc_HEADERS = \ - $(HEADER_FILES) \ - gck-enum-types.h - -INCLUDES = \ - -I$(top_builddir) \ - -I$(top_srcdir) \ - $(P11_KIT_CFLAGS) \ - $(GOBJECT_CFLAGS) \ - $(GTHREAD_CFLAGS) \ - $(GLIB_CFLAGS) \ - -DG_LOG_DOMAIN=\"Gck\" \ - -DGCK_API_SUBJECT_TO_CHANGE \ - -DP11_KIT_API_SUBJECT_TO_CHANGE \ - -DPKCS11_REGISTRY_DIR=\"$(libdir)/pkcs11\" - -BUILT_SOURCES = \ - gck-marshal.c gck-marshal.h \ - gck-enum-types.c gck-enum-types.h - -lib_LTLIBRARIES = libgck-@GCK_MAJOR@.la - -PUBLIC_FILES = \ - gck.h \ - gck-enum-types.h \ - gck-attributes.c \ - gck-dump.c \ - gck-enumerator.c \ - gck-misc.c \ - gck-module.c \ - gck-modules.c \ - gck-object.c \ - gck-password.c \ - gck-session.c \ - gck-slot.c \ - gck-uri.c - -INTERNAL_FILES = \ - gck-call.c \ - gck-debug.c gck-debug.h \ - gck-deprecated.h \ - gck-interaction.c \ - gck-private.h \ - pkcs11.h - -libgck_@GCK_MAJOR@_la_SOURCES = \ - $(PUBLIC_FILES) \ - $(INTERNAL_FILES) \ - $(BUILT_SOURCES) - -libgck_@GCK_MAJOR@_la_LDFLAGS = \ - -version-info $(GCK_LT_RELEASE) \ - -no-undefined \ - -export-symbols-regex '^gck_*' - -libgck_@GCK_MAJOR@_la_LIBADD = \ - $(top_builddir)/egg/libegg-hex.la \ - $(P11_KIT_LIBS) \ - $(GOBJECT_LIBS) \ - $(GTHREAD_LIBS) \ - $(GIO_LIBS) \ - $(GLIB_LIBS) - -noinst_LTLIBRARIES = libgck-testable.la -libgck_testable_la_SOURCES = \ - gck-mock.c \ - gck-mock.h \ - gck-test.c \ - gck-test.h -libgck_testable_la_LIBADD = $(libgck_@GCK_MAJOR@_la_OBJECTS) \ - $(libgck_@GCK_MAJOR@_la_LIBADD) -libgck_testable_la_DEPENDENCIES = $(libgck_@GCK_MAJOR@_la_OBJECTS) - -gck-marshal.h: gck-marshal.list $(GLIB_GENMARSHAL) - $(GLIB_GENMARSHAL) $< --header --prefix=_gck_marshal > $@ - -gck-marshal.c: gck-marshal.list $(GLIB_GENMARSHAL) - echo "#include \"gck-marshal.h\"" > $@ && \ - $(GLIB_GENMARSHAL) $< --body --prefix=_gck_marshal >> $@ - -gck-enum-types.h: gck.h gck-enum-types.h.template - $(AM_V_GEN) $(GLIB_MKENUMS) --template $(srcdir)/gck-enum-types.h.template \ - $(HEADER_FILES) > $@ - -gck-enum-types.c: gck.h gck-enum-types.c.template - $(AM_V_GEN) $(GLIB_MKENUMS) --template $(srcdir)/gck-enum-types.c.template \ - $(HEADER_FILES) > $@ - -# ------------------------------------------------------------------ -# INTROSPECTION - -if HAVE_INTROSPECTION - -include $(INTROSPECTION_MAKEFILE) - -INTROSPECTION_GIRS = Gck-@GCK_MAJOR@.gir -INTROSPECTION_SCANNER_ARGS = $(INTROSPECTION_FLAGS) --warn-all --add-include-path=$(srcdir) --add-include-path=. -INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir) --includedir=. - -Gck-@GCK_MAJOR@.gir: libgck-@GCK_MAJOR@.la - -Gck_@GCK_MAJOR@_gir_PACKAGES = gobject-2.0 gio-2.0 p11-kit-1 -Gck_@GCK_MAJOR@_gir_EXPORT_PACKAGES = gck-@GCK_MAJOR@ -Gck_@GCK_MAJOR@_gir_INCLUDES = GObject-2.0 Gio-2.0 -Gck_@GCK_MAJOR@_gir_LIBS = libgck-@GCK_MAJOR@.la -Gck_@GCK_MAJOR@_gir_CFLAGS = -I$(top_srcdir) -I$(top_builddir) -DGCK_COMPILATION -DGCK_API_SUBJECT_TO_CHANGE -Gck_@GCK_MAJOR@_gir_FILES = $(PUBLIC_FILES) -Gck_@GCK_MAJOR@_gir_SCANNERFLAGS = --c-include "gck.h" - -girdir = $(datadir)/gir-1.0 -gir_DATA = $(INTROSPECTION_GIRS) - -typelibsdir = $(libdir)/girepository-1.0 -typelibs_DATA = $(INTROSPECTION_GIRS:.gir=.typelib) - -endif - -# ---------------------------------------------------------------- - -pkgconfigdir = $(libdir)/pkgconfig -pkgconfig_DATA = gck-$(GCK_MAJOR).pc - -gck-expected.abi: gck.symbols - $(AM_V_GEN) cpp -P $< | sort > $@ - -gck-actual.abi: $(builddir)/.libs/libgck-@GCK_MAJOR@.so - $(AM_V_GEN) $(NM) -D -g --defined-only $< | \ - cut -d ' ' -f 3 | grep -Ev '^(__bss_start|_edata|_end)' | sort > $@ - -check-symbols: gck-expected.abi gck-actual.abi - $(AM_V_GEN) diff -U0 --ignore-blank-lines gck-expected.abi gck-actual.abi || \ - (echo "check-symbols: Symbols have CHANGED. Fix gck.symbols"; exit 1) - -check-local: check-symbols - -EXTRA_DIST = \ - gck.pc.in \ - gck-marshal.list \ - gck.symbols \ - gck-enum-types.h.template \ - gck-enum-types.c.template \ - pkcs11-trust-assertions.h - -CLEANFILES = \ - gck-actual.abi \ - gck-expected.abi - -DISTCLEANFILES = \ - gck-$(GCK_MAJOR).pc - -gck-$(GCK_MAJOR).pc: gck.pc - cp gck.pc gck-$(GCK_MAJOR).pc diff --git a/gck/gck-attributes.c b/gck/gck-attributes.c deleted file mode 100644 index cb48ca17..00000000 --- a/gck/gck-attributes.c +++ /dev/null @@ -1,1877 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-attribute.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" -#include "pkcs11-trust-assertions.h" - -#include <stdlib.h> -#include <string.h> - -/** - * SECTION:gck-attribute - * @title: GckAttribute - * @short_description: A PKCS11 attribute. - * - * This structure represents a PKCS11 CK_ATTRIBUTE. These attributes contain information - * about a PKCS11 object. Use gck_object_get() or gck_object_set() to set and retrieve - * attributes on an object. - */ - -/** - * GckAttribute: - * @type: The attribute type, such as CKA_LABEL. - * @value: (array length=length): The value of the attribute. May be NULL. - * @length: The length of the attribute. May be G_MAXULONG if the attribute is invalid. - * - * This structure represents a PKCS11 CK_ATTRIBUTE. - */ - -/** - * GCK_TYPE_ATTRIBUTES: - * - * Boxed type for #GckAttributes - */ - -static void -attribute_init (GckAttribute *attr, gulong attr_type, - gconstpointer value, gsize length, - GckAllocator allocator) -{ - g_assert (sizeof (GckAttribute) == sizeof (CK_ATTRIBUTE)); - g_assert (allocator); - - memset (attr, 0, sizeof (GckAttribute)); - attr->type = attr_type; - attr->length = length; - if (value) { - attr->value = (allocator) (NULL, length ? length : 1); - g_assert (attr->value); - memcpy ((gpointer)attr->value, value, length); - } -} - -/** - * gck_attribute_init: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: (array length=length): The raw value of the attribute. - * @length: The length of the raw value. - * - * Initialize a PKCS\#11 attribute. This copies the value memory - * into an internal buffer. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init (GckAttribute *attr, - gulong attr_type, - const guchar *value, - gsize length) -{ - g_return_if_fail (attr); - attribute_init (attr, attr_type, value, length, g_realloc); -} - -/** - * gck_attribute_init_invalid: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * - * Initialize a PKCS\#11 attribute to an 'invalid' or 'not found' - * state. Specifically this sets the value length to (CK_ULONG)-1 - * as specified in the PKCS\#11 specification. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init_invalid (GckAttribute *attr, gulong attr_type) -{ - g_return_if_fail (attr); - g_assert (sizeof (GckAttribute) == sizeof (CK_ATTRIBUTE)); - memset (attr, 0, sizeof (GckAttribute)); - attr->type = attr_type; - attr->length = (gulong)-1; -} - -/** - * gck_attribute_init_empty: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * - * Initialize a PKCS\#11 attribute to an empty state. The attribute - * type will be set, but no data will be set. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init_empty (GckAttribute *attr, gulong attr_type) -{ - g_return_if_fail (attr); - g_assert (sizeof (GckAttribute) == sizeof (CK_ATTRIBUTE)); - memset (attr, 0, sizeof (GckAttribute)); - attr->type = attr_type; - attr->length = 0; - attr->value = 0; -} - -static void -attribute_init_boolean (GckAttribute *attr, gulong attr_type, - gboolean value, GckAllocator allocator) -{ - CK_BBOOL bvalue = value ? CK_TRUE : CK_FALSE; - attribute_init (attr, attr_type, &bvalue, sizeof (bvalue), allocator); -} - -/** - * gck_attribute_init_boolean: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The boolean value of the attribute. - * - * Initialize a PKCS\#11 attribute to boolean. This will result - * in a CK_BBOOL attribute from the PKCS\#11 specs. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init_boolean (GckAttribute *attr, gulong attr_type, - gboolean value) -{ - g_return_if_fail (attr); - attribute_init_boolean (attr, attr_type, value, g_realloc); -} - -static void -attribute_init_date (GckAttribute *attr, gulong attr_type, - const GDate *value, GckAllocator allocator) -{ - gchar buffer[9]; - CK_DATE date; - g_assert (value); - g_snprintf (buffer, sizeof (buffer), "%04d%02d%02d", - (int)g_date_get_year (value), - (int)g_date_get_month (value), - (int)g_date_get_day (value)); - memcpy (&date.year, buffer + 0, 4); - memcpy (&date.month, buffer + 4, 2); - memcpy (&date.day, buffer + 6, 2); - attribute_init (attr, attr_type, &date, sizeof (CK_DATE), allocator); -} - -/** - * gck_attribute_init_date: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The date value of the attribute. - * - * Initialize a PKCS\#11 attribute to a date. This will result - * in a CK_DATE attribute from the PKCS\#11 specs. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init_date (GckAttribute *attr, gulong attr_type, - const GDate *value) -{ - g_return_if_fail (attr); - g_return_if_fail (value); - attribute_init_date (attr, attr_type, value, g_realloc); -} - -static void -attribute_init_ulong (GckAttribute *attr, gulong attr_type, - gulong value, GckAllocator allocator) -{ - CK_ULONG uvalue = value; - attribute_init (attr, attr_type, &uvalue, sizeof (uvalue), allocator); -} - -/** - * gck_attribute_init_ulong: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The ulong value of the attribute. - * - * Initialize a PKCS\#11 attribute to a unsigned long. This will result - * in a CK_ULONG attribute from the PKCS\#11 specs. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init_ulong (GckAttribute *attr, gulong attr_type, - gulong value) -{ - g_return_if_fail (attr); - attribute_init_ulong (attr, attr_type, value, g_realloc); -} - -static void -attribute_init_string (GckAttribute *attr, gulong attr_type, - const gchar *value, GckAllocator allocator) -{ - gsize len = value ? strlen (value) : 0; - attribute_init (attr, attr_type, (gpointer)value, len, allocator); -} - -/** - * gck_attribute_init_string: (skip) - * @attr: An uninitialized attribute. - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The null terminated string value of the attribute. - * - * Initialize a PKCS\#11 attribute to a string. This will result - * in an attribute containing the text, but not the null terminator. - * The text in the attribute will be of the same encoding as you pass - * to this function. - * - * When done with the attribute you should use gck_attribute_clear() - * to free the internal memory. - **/ -void -gck_attribute_init_string (GckAttribute *attr, gulong attr_type, - const gchar *value) -{ - g_return_if_fail (attr); - attribute_init_string (attr, attr_type, value, g_realloc); -} - -GType -gck_attribute_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckAttribute", - (GBoxedCopyFunc)gck_attribute_dup, - (GBoxedFreeFunc)gck_attribute_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_attribute_new: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The raw value of the attribute. - * @length: The length of the attribute. - * - * Create a new PKCS\#11 attribute. The value will be copied - * into the new attribute. - * - * Returns: (transfer full): the new attribute; when done with the attribute - * use gck_attribute_free() to free it - **/ -GckAttribute* -gck_attribute_new (gulong attr_type, gpointer value, gsize length) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - attribute_init (attr, attr_type, value, length, g_realloc); - return attr; -} - -/** - * gck_attribute_new_invalid: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * - * Create a new PKCS\#11 attribute as 'invalid' or 'not found' - * state. Specifically this sets the value length to (CK_ULONG)-1 - * as specified in the PKCS\#11 specification. - * - * Returns: (transfer full): the new attribute; when done with the attribute - * use gck_attribute_free() to free it - **/ -GckAttribute* -gck_attribute_new_invalid (gulong attr_type) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - gck_attribute_init_invalid (attr, attr_type); - return attr; -} - -/** - * gck_attribute_new_empty: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * - * Create a new PKCS\#11 attribute with empty data. - * - * Returns: (transfer full): the new attribute; when done with the attribute - * use gck_attribute_free() to free it - */ -GckAttribute* -gck_attribute_new_empty (gulong attr_type) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - gck_attribute_init_empty (attr, attr_type); - return attr; -} - -/** - * gck_attribute_new_boolean: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The boolean value of the attribute. - * - * Initialize a PKCS\#11 attribute to boolean. This will result - * in a CK_BBOOL attribute from the PKCS\#11 specs. - * - * Returns: (transfer full): the new attribute; when done with the attribute use - * gck_attribute_free() to free it - **/ -GckAttribute* -gck_attribute_new_boolean (gulong attr_type, gboolean value) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - attribute_init_boolean (attr, attr_type, value, g_realloc); - return attr; -} - -/** - * gck_attribute_new_date: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The date value of the attribute. - * - * Initialize a PKCS\#11 attribute to a date. This will result - * in a CK_DATE attribute from the PKCS\#11 specs. - * - * Returns: (transfer full): the new attribute; when done with the attribute use - * gck_attribute_free() to free it - **/ -GckAttribute* -gck_attribute_new_date (gulong attr_type, const GDate *value) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - attribute_init_date (attr, attr_type, value, g_realloc); - return attr; -} - -/** - * gck_attribute_new_ulong: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The ulong value of the attribute. - * - * Initialize a PKCS\#11 attribute to a unsigned long. This will result - * in a CK_ULONG attribute from the PKCS\#11 specs. - * - * Returns: (transfer full): the new attribute; when done with the attribute use - * gck_attribute_free() to free it - **/ -GckAttribute* -gck_attribute_new_ulong (gulong attr_type, gulong value) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - attribute_init_ulong (attr, attr_type, value, g_realloc); - return attr; -} - -/** - * gck_attribute_new_string: - * @attr_type: The PKCS\#11 attribute type to set on the attribute. - * @value: The null terminated string value of the attribute. - * - * Initialize a PKCS\#11 attribute to a string. This will result - * in an attribute containing the text, but not the null terminator. - * The text in the attribute will be of the same encoding as you pass - * to this function. - * - * Returns: (transfer full): the new attribute; when done with the attribute use - * gck_attribute_free() to free it - **/ -GckAttribute* -gck_attribute_new_string (gulong attr_type, const gchar *value) -{ - GckAttribute *attr = g_slice_new0 (GckAttribute); - attribute_init_string (attr, attr_type, value, g_realloc); - return attr; -} - -/** - * gck_attribute_is_invalid: - * @attr: The attribute to check. - * - * Check if the PKCS\#11 attribute represents 'invalid' or 'not found' - * according to the PKCS\#11 spec. That is, having length - * of (CK_ULONG)-1. - * - * Return value: Whether the attribute represents invalid or not. - */ -gboolean -gck_attribute_is_invalid (GckAttribute *attr) -{ - g_return_val_if_fail (attr, TRUE); - return attr->length == (gulong)-1; -} - -/** - * gck_attribute_get_boolean: - * @attr: The attribute to retrieve value from. - * - * Get the CK_BBOOL of a PKCS\#11 attribute. No conversion - * is performed. It is an error to pass an attribute to this - * function unless you're know it's supposed to contain a - * boolean value. - * - * Return value: The boolean value of the attribute. - */ -gboolean -gck_attribute_get_boolean (GckAttribute *attr) -{ - gboolean value; - - g_return_val_if_fail (attr, FALSE); - if (gck_attribute_is_invalid (attr)) - return FALSE; - if (!gck_value_to_boolean (attr->value, attr->length, &value)) - g_return_val_if_reached (FALSE); - return value; -} - -/** - * gck_attribute_get_ulong: - * @attr: The attribute to retrieve value from. - * - * Get the CK_ULONG value of a PKCS\#11 attribute. No - * conversion is performed. It is an error to pass an attribute - * to this function unless you're know it's supposed to contain - * a value of the right type. - * - * Return value: The ulong value of the attribute. - */ -gulong -gck_attribute_get_ulong (GckAttribute *attr) -{ - gulong value; - - g_return_val_if_fail (attr, FALSE); - if (gck_attribute_is_invalid (attr)) - return 0; - if (!gck_value_to_ulong (attr->value, attr->length, &value)) - g_return_val_if_reached ((gulong)-1); - return value; -} - -/** - * gck_attribute_get_string: - * @attr: The attribute to retrieve value from. - * - * Get the string value of a PKCS\#11 attribute. No - * conversion is performed. It is an error to pass an attribute - * to this function unless you're know it's supposed to contain - * a value of the right type. - * - * Return value: (allow-none): a null terminated string, to be freed with - * g_free(), or %NULL if the value was invalid - */ -gchar* -gck_attribute_get_string (GckAttribute *attr) -{ - g_return_val_if_fail (attr, NULL); - - if (gck_attribute_is_invalid (attr)) - return NULL; - if (!attr->value) - return NULL; - - return g_strndup ((gchar*)attr->value, attr->length); -} - -/** - * gck_attribute_get_date: - * @attr: The attribute to retrieve value from. - * @value: The date value to fill in with the parsed date. - * - * Get the CK_DATE of a PKCS\#11 attribute. No - * conversion is performed. It is an error to pass an attribute - * to this function unless you're know it's supposed to contain - * a value of the right type. - */ -void -gck_attribute_get_date (GckAttribute *attr, GDate *value) -{ - guint year, month, day; - gchar buffer[5]; - CK_DATE *date; - gchar *end; - - g_return_if_fail (attr); - - if (gck_attribute_is_invalid (attr)) { - g_date_clear (value, 1); - return; - } - - g_return_if_fail (attr->length == sizeof (CK_DATE)); - g_return_if_fail (attr->value); - date = (CK_DATE*)attr->value; - - memset (&buffer, 0, sizeof (buffer)); - memcpy (buffer, date->year, 4); - year = strtol (buffer, &end, 10); - g_return_if_fail (end != buffer && !*end); - - memset (&buffer, 0, sizeof (buffer)); - memcpy (buffer, date->month, 2); - month = strtol (buffer, &end, 10); - g_return_if_fail (end != buffer && !*end); - - memset (&buffer, 0, sizeof (buffer)); - memcpy (buffer, date->day, 2); - day = strtol (buffer, &end, 10); - g_return_if_fail (end != buffer && !*end); - - g_date_set_dmy (value, day, month, year); -} - -/** - * gck_attribute_dup: - * @attr: The attribute to duplicate. - * - * Duplicate the PKCS\#11 attribute. All value memory is - * also copied. - * - * Returns: (transfer full): the duplicated attribute; use gck_attribute_free() - * to free it - */ -GckAttribute* -gck_attribute_dup (GckAttribute *attr) -{ - GckAttribute *copy; - - if (!attr) - return NULL; - - copy = g_slice_new0 (GckAttribute); - gck_attribute_init_copy (copy, attr); - return copy; -} - -static void -attribute_init_copy (GckAttribute *dest, const GckAttribute *src, GckAllocator allocator) -{ - g_assert (dest); - g_assert (src); - g_assert (allocator); - - /* - * TODO: Handle stupid, dumb, broken, special cases like - * CKA_WRAP_TEMPLATE and CKA_UNWRAP_TEMPLATE. - */ - - memcpy (dest, src, sizeof (GckAttribute)); - if (src->value) { - dest->value = (allocator) (NULL, src->length ? src->length : 1); - g_assert (dest->value); - memcpy ((gpointer)dest->value, src->value, src->length); - } -} - -/** - * gck_attribute_init_copy: - * @dest: An uninitialized attribute. - * @src: An attribute to copy. - * - * Initialize a PKCS\#11 attribute as a copy of another attribute. - * This copies the value memory as well. - * - * When done with the copied attribute you should use - * gck_attribute_clear() to free the internal memory. - **/ -void -gck_attribute_init_copy (GckAttribute *dest, const GckAttribute *src) -{ - g_return_if_fail (dest); - g_return_if_fail (src); - attribute_init_copy (dest, src, g_realloc); -} - -static void -attribute_clear (GckAttribute *attr, GckAllocator allocator) -{ - g_assert (attr); - g_assert (allocator); - if (attr->value) - (allocator) ((gpointer)attr->value, 0); - attr->value = NULL; - attr->length = 0; -} - -/** - * gck_attribute_clear: - * @attr: Attribute to clear. - * - * Clear allocated memory held by a statically allocated attribute. - * These are usually initialized with gck_attribute_init() or a - * similar function. - * - * The type of the attribute will remain set. - **/ -void -gck_attribute_clear (GckAttribute *attr) -{ - g_return_if_fail (attr); - attribute_clear (attr, g_realloc); -} - -/** - * gck_attribute_free: - * @attr: Attribute to free. - * - * Free an attribute and its allocated memory. These is usually - * used with attributes that are allocated by gck_attribute_new() - * or a similar function. - **/ -void -gck_attribute_free (GckAttribute *attr) -{ - if (attr) { - attribute_clear (attr, g_realloc); - g_slice_free (GckAttribute, attr); - } -} - -/** - * gck_attribute_equal: - * @a: (type Gck.Attribute): first attribute to compare - * @b: (type Gck.Attribute): second attribute to compare - * - * Compare two attributes. Useful with <code>GHashTable</code>. - * - * Returns: %TRUE if the attributes are equal. - */ -gboolean -gck_attribute_equal (gconstpointer a, gconstpointer b) -{ - const GckAttribute *aa = a; - const GckAttribute *ab = b; - - if (!a && !b) - return TRUE; - if (!a || !b) - return FALSE; - - if (aa->type != ab->type) - return FALSE; - if (aa->length != ab->length) - return FALSE; - if (!aa->value && !ab->value) - return TRUE; - if (!aa->value || !ab->value) - return FALSE; - return memcmp (aa->value, ab->value, aa->length) == 0; -} - -/** - * SECTION:gck-attributes - * @title: GckAttributes - * @short_description: A set of PKCS11 attributes. - * - * A set of GckAttribute structures. These attributes contain information - * about a PKCS11 object. Use gck_object_get() or gck_object_set() to set and retrieve - * attributes on an object. - */ - -/** - * GckAttributes: - * - * A set of GckAttribute structures. - */ -struct _GckAttributes { - GArray *array; - GckAllocator allocator; - gboolean locked; - gint refs; -}; - -/** - * GckAllocator: - * @data: Memory to allocate or deallocate. - * @length: New length of memory. - * - * An allocator used to allocate data for the attributes in this GckAttributes set. - * - * This is a function that acts like g_realloc. Specifically it frees when length is - * set to zero, it allocates when data is set to NULL, and it reallocates when both - * are valid. - * - * Returns: The allocated memory, or NULL when freeing. - **/ - -GType -gck_attributes_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckAttributes", - (GBoxedCopyFunc)gck_attributes_ref, - (GBoxedFreeFunc)gck_attributes_unref); - g_once_init_leave (&initialized, 1); - } - return type; -} - -GType -gck_attributes_get_boxed_type (void) -{ - /* Deprecated version */ - return gck_attributes_get_type (); -} - -/** - * gck_attributes_new: - * - * Create a new GckAttributes array. - * - * Returns: (transfer full): the new attributes array; when done with the array - * release it with gck_attributes_unref(). - **/ -GckAttributes* -gck_attributes_new (void) -{ - return gck_attributes_new_full (g_realloc); -} - -/** - * gck_attributes_new_full: (skip) - * @allocator: Memory allocator for attribute data, or NULL for default. - * - * Create a new GckAttributes array. - * - * Returns: (transfer full): the new attributes array; when done with the array - * release it with gck_attributes_unref() - **/ -GckAttributes* -gck_attributes_new_full (GckAllocator allocator) -{ - GckAttributes *attrs; - - if (!allocator) - allocator = g_realloc; - - g_assert (sizeof (GckAttribute) == sizeof (CK_ATTRIBUTE)); - attrs = g_slice_new0 (GckAttributes); - attrs->array = g_array_new (0, 1, sizeof (GckAttribute)); - attrs->allocator = allocator; - attrs->refs = 1; - attrs->locked = FALSE; - return attrs; -} - -/** - * gck_attributes_new_empty: (skip) - * @attr_type: The first attribute type to add as empty. - * @...: The arguments should be values of attribute types, terminated with gck_INVALID. - * - * Creates an GckAttributes array with empty attributes. The arguments - * should be values of attribute types, terminated with gck_INVALID. - * - * Returns: (transfer full): the new attributes array; when done with the array - * release it with gck_attributes_unref() - **/ -GckAttributes* -gck_attributes_new_empty (gulong attr_type, ...) -{ - GckAttributes *attrs = gck_attributes_new_full (g_realloc); - va_list va; - - va_start (va, attr_type); - - while (attr_type != GCK_INVALID) { - gck_attributes_add_empty (attrs, attr_type); - attr_type = va_arg (va, gulong); - } - - va_end (va); - - return attrs; -} - -/** - * gck_attributes_at: - * @attrs: The attributes array. - * @index: The attribute index to retrieve. - * - * Get attribute at the specified index in the attribute array. - * - * Use gck_attributes_count() to determine how many attributes are - * in the array. - * - * Returns: (transfer none): the specified attribute - **/ -GckAttribute* -gck_attributes_at (GckAttributes *attrs, guint index) -{ - g_return_val_if_fail (attrs && attrs->array, NULL); - g_return_val_if_fail (index < attrs->array->len, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - return &g_array_index (attrs->array, GckAttribute, index); -} - -static GckAttribute* -attributes_push (GckAttributes *attrs) -{ - GckAttribute attr; - g_assert (!attrs->locked); - memset (&attr, 0, sizeof (attr)); - g_array_append_val (attrs->array, attr); - return &g_array_index (attrs->array, GckAttribute, attrs->array->len - 1); -} - -/** - * gck_attributes_add: - * @attrs: The attributes array to add to - * @attr: The attribute to add. - * - * Add the specified attribute to the array. - * - * The value stored in the attribute will be copied. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add (GckAttributes *attrs, GckAttribute *attr) -{ - GckAttribute *added; - g_return_val_if_fail (attrs && attrs->array, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - g_return_val_if_fail (attr, NULL); - added = attributes_push (attrs); - attribute_init_copy (added, attr, attrs->allocator); - return added; -} - -/** - * gck_attributes_add_data: - * @attrs: The attributes array to add to. - * @attr_type: The type of attribute to add. - * @value: (array length=length): the raw memory of the attribute value - * @length: The length of the attribute value. - * - * Add an attribute with the specified type and value to the array. - * - * The value stored in the attribute will be copied. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_data (GckAttributes *attrs, - gulong attr_type, - const guchar *value, - gsize length) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - attribute_init (added, attr_type, value, length, attrs->allocator); - return added; -} - -/** - * gck_attributes_add_invalid: - * @attrs: The attributes array to add to. - * @attr_type: The type of attribute to add. - * - * Add an attribute with the specified type and an 'invalid' value to the array. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_invalid (GckAttributes *attrs, gulong attr_type) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - gck_attribute_init_invalid (added, attr_type); - return added; -} - -/** - * gck_attributes_add_empty: - * @attrs: The attributes array to add. - * @attr_type: The type of attribute to add. - * - * Add an attribute with the specified type, with empty data. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_empty (GckAttributes *attrs, gulong attr_type) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - gck_attribute_init_empty (added, attr_type); - return added; -} - -/** - * gck_attributes_add_boolean: - * @attrs: The attributes array to add to. - * @attr_type: The type of attribute to add. - * @value: The boolean value to add. - * - * Add an attribute with the specified type and value to the array. - * - * The value will be stored as a CK_BBOOL PKCS\#11 style attribute. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_boolean (GckAttributes *attrs, gulong attr_type, gboolean value) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - attribute_init_boolean (added, attr_type, value, attrs->allocator); - return added; -} - -/** - * gck_attributes_set_boolean: - * @attrs: the attributes - * @attr_type: the type of attribute to set - * @value: boolean value to set - * - * Set the attribute of attr_type in the attribute array to the given value. - * If no such value exists, then add one. - */ -void -gck_attributes_set_boolean (GckAttributes *attrs, - gulong attr_type, - gboolean value) -{ - GckAttribute *attr; - - g_return_if_fail (attrs != NULL); - g_return_if_fail (!attrs->locked); - - attr = gck_attributes_find (attrs, attr_type); - if (attr == NULL) { - gck_attributes_add_boolean (attrs, attr_type, value); - } else { - attribute_clear (attr, attrs->allocator); - attribute_init_boolean (attr, attr_type, value, attrs->allocator); - } -} - -/** - * gck_attributes_add_string: - * @attrs: The attributes array to add to. - * @attr_type: The type of attribute to add. - * @value: The null terminated string value to add. - * - * Add an attribute with the specified type and value to the array. - * - * The value will be copied into the attribute. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_string (GckAttributes *attrs, gulong attr_type, const gchar *value) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - attribute_init_string (added, attr_type, value, attrs->allocator); - return added; -} - -/** - * gck_attributes_set_string: - * @attrs: the attributes - * @attr_type: the type of attribute to set - * @value: null terminated string value to set - * - * Set the attribute of attr_type in the attribute array to the given value. - * If no such value exists, then add one. - */ -void -gck_attributes_set_string (GckAttributes *attrs, - gulong attr_type, - const gchar *value) -{ - GckAttribute *attr; - - g_return_if_fail (attrs != NULL); - g_return_if_fail (!attrs->locked); - - attr = gck_attributes_find (attrs, attr_type); - if (attr == NULL) { - gck_attributes_add_string (attrs, attr_type, value); - } else { - attribute_clear (attr, attrs->allocator); - attribute_init_string (attr, attr_type, value, attrs->allocator); - } -} - -/** - * gck_attributes_add_date: - * @attrs: The attributes array to add to. - * @attr_type: The type of attribute to add. - * @value: The GDate value to add. - * - * Add an attribute with the specified type and value to the array. - * - * The value will be stored as a CK_DATE PKCS\#11 style attribute. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_date (GckAttributes *attrs, gulong attr_type, const GDate *value) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - attribute_init_date (added, attr_type, value, attrs->allocator); - return added; -} - -/** - * gck_attributes_set_date: - * @attrs: the attributes - * @attr_type: the type of attribute to set - * @value: date value to set - * - * Set the attribute of attr_type in the attribute array to the given value. - * If no such value exists, then add one. - */ -void -gck_attributes_set_date (GckAttributes *attrs, - gulong attr_type, - const GDate *value) -{ - GckAttribute *attr; - - g_return_if_fail (attrs != NULL); - g_return_if_fail (!attrs->locked); - - attr = gck_attributes_find (attrs, attr_type); - if (attr == NULL) { - gck_attributes_add_date (attrs, attr_type, value); - } else { - attribute_clear (attr, attrs->allocator); - attribute_init_date (attr, attr_type, value, attrs->allocator); - } -} - -/** - * gck_attributes_add_ulong: - * @attrs: The attributes array to add to. - * @attr_type: The type of attribute to add. - * @value: The gulong value to add. - * - * Add an attribute with the specified type and value to the array. - * - * The value will be stored as a CK_ULONG PKCS\#11 style attribute. - * - * Returns: (transfer none): the attribute that was added - **/ -GckAttribute * -gck_attributes_add_ulong (GckAttributes *attrs, gulong attr_type, gulong value) -{ - GckAttribute *added; - g_return_val_if_fail (attrs, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - added = attributes_push (attrs); - attribute_init_ulong (added, attr_type, value, attrs->allocator); - return added; -} - -/** - * gck_attributes_set_ulong: - * @attrs: the attributes - * @attr_type: the type of attribute to set - * @value: gulong value to set - * - * Set the attribute of attr_type in the attribute array to the given value. - * If no such value exists, then add one. - */ -void -gck_attributes_set_ulong (GckAttributes *attrs, - gulong attr_type, - gulong value) -{ - GckAttribute *attr; - - g_return_if_fail (attrs != NULL); - g_return_if_fail (!attrs->locked); - - attr = gck_attributes_find (attrs, attr_type); - if (attr == NULL) { - gck_attributes_add_ulong (attrs, attr_type, value); - } else { - attribute_clear (attr, attrs->allocator); - attribute_init_ulong (attr, attr_type, value, attrs->allocator); - } -} - -/** - * gck_attributes_add_all: - * @attrs: A set of attributes - * @from: Attributes to add - * - * Add all attributes in @from to @attrs. - */ -void -gck_attributes_add_all (GckAttributes *attrs, GckAttributes *from) -{ - GckAttribute *attr; - guint i; - - g_return_if_fail (attrs && attrs->array); - g_return_if_fail (from && from->array); - g_return_if_fail (!attrs->locked); - - for (i = 0; i < from->array->len; ++i) { - attr = &g_array_index (from->array, GckAttribute, i); - gck_attributes_add (attrs, attr); - } -} - -/** - * gck_attributes_count: - * @attrs: The attributes array to count. - * - * Get the number of attributes in this attribute array. - * - * Return value: The number of contained attributes. - **/ -gulong -gck_attributes_count (GckAttributes *attrs) -{ - g_return_val_if_fail (attrs, 0); - g_return_val_if_fail (!attrs->locked, 0); - return attrs->array->len; -} - -/** - * gck_attributes_find: - * @attrs: The attributes array to search. - * @attr_type: The type of attribute to find. - * - * Find an attribute with the specified type in the array. - * - * Returns: (transfer none): the first attribute found with the specified type, - * or %NULL - **/ -GckAttribute * -gck_attributes_find (GckAttributes *attrs, gulong attr_type) -{ - GckAttribute *attr; - guint i; - - g_return_val_if_fail (attrs && attrs->array, NULL); - g_return_val_if_fail (!attrs->locked, NULL); - - for (i = 0; i < attrs->array->len; ++i) { - attr = gck_attributes_at (attrs, i); - if (attr->type == attr_type) - return attr; - } - - return NULL; -} - -/** - * gck_attributes_find_boolean: - * @attrs: The attributes array to search. - * @attr_type: The type of attribute to find. - * @value: The resulting gboolean value. - * - * Find an attribute with the specified type in the array. - * - * The attribute (if found) must be of the right size to store - * a boolean value (ie: CK_BBOOL). If the attribute is marked invalid - * then it will be treated as not found. - * - * Return value: Whether a value was found or not. - **/ -gboolean -gck_attributes_find_boolean (GckAttributes *attrs, gulong attr_type, gboolean *value) -{ - GckAttribute *attr; - - g_return_val_if_fail (value, FALSE); - g_return_val_if_fail (!attrs->locked, FALSE); - - attr = gck_attributes_find (attrs, attr_type); - if (!attr || gck_attribute_is_invalid (attr)) - return FALSE; - *value = gck_attribute_get_boolean (attr); - return TRUE; -} - -/** - * gck_attributes_find_ulong: - * @attrs: The attributes array to search. - * @attr_type: The type of attribute to find. - * @value: The resulting gulong value. - * - * Find an attribute with the specified type in the array. - * - * The attribute (if found) must be of the right size to store - * a unsigned long value (ie: CK_ULONG). If the attribute is marked invalid - * then it will be treated as not found. - * - * Return value: Whether a value was found or not. - **/ -gboolean -gck_attributes_find_ulong (GckAttributes *attrs, gulong attr_type, gulong *value) -{ - GckAttribute *attr; - - g_return_val_if_fail (value, FALSE); - g_return_val_if_fail (!attrs->locked, FALSE); - - attr = gck_attributes_find (attrs, attr_type); - if (!attr || gck_attribute_is_invalid (attr)) - return FALSE; - *value = gck_attribute_get_ulong (attr); - return TRUE; -} - -/** - * gck_attributes_find_string: - * @attrs: The attributes array to search. - * @attr_type: The type of attribute to find. - * @value: The resulting string value. - * - * Find an attribute with the specified type in the array. - * - * If the attribute is marked invalid then it will be treated as not found. - * The resulting string will be null-terminated, and must be freed by the caller - * using g_free(). - * - * Return value: Whether a value was found or not. - **/ -gboolean -gck_attributes_find_string (GckAttributes *attrs, gulong attr_type, gchar **value) -{ - GckAttribute *attr; - - g_return_val_if_fail (value, FALSE); - g_return_val_if_fail (!attrs->locked, FALSE); - - attr = gck_attributes_find (attrs, attr_type); - if (!attr || gck_attribute_is_invalid (attr)) - return FALSE; - *value = gck_attribute_get_string (attr); - return TRUE; -} - -/** - * gck_attributes_find_date: - * @attrs: The attributes array to search. - * @attr_type: The type of attribute to find. - * @value: The resulting GDate value. - * - * Find an attribute with the specified type in the array. - * - * The attribute (if found) must be of the right size to store - * a date value (ie: CK_DATE). If the attribute is marked invalid - * then it will be treated as not found. - * - * Return value: Whether a value was found or not. - **/ -gboolean -gck_attributes_find_date (GckAttributes *attrs, gulong attr_type, GDate *value) -{ - GckAttribute *attr; - - g_return_val_if_fail (value, FALSE); - g_return_val_if_fail (!attrs->locked, FALSE); - - attr = gck_attributes_find (attrs, attr_type); - if (!attr || gck_attribute_is_invalid (attr)) - return FALSE; - gck_attribute_get_date (attr, value); - return TRUE; -} - -/** - * gck_attributes_ref: - * @attrs: An attribute array - * - * Reference this attributes array. - * - * Returns: (transfer full): the attributes - **/ -GckAttributes * -gck_attributes_ref (GckAttributes *attrs) -{ - g_return_val_if_fail (attrs, NULL); - g_atomic_int_inc (&attrs->refs); - return attrs; -} - -/** - * gck_attributes_unref: - * @attrs: An attribute array - * - * Unreference this attribute array. - * - * When all outstanding references are NULL, the array will be freed. - */ -void -gck_attributes_unref (GckAttributes *attrs) -{ - guint i; - - if (!attrs) - return; - - if (g_atomic_int_dec_and_test (&attrs->refs)) { - g_return_if_fail (attrs->array); - g_return_if_fail (!attrs->locked); - for (i = 0; i < attrs->array->len; ++i) - attribute_clear (gck_attributes_at (attrs, i), attrs->allocator); - g_array_free (attrs->array, TRUE); - attrs->array = NULL; - g_slice_free (GckAttributes, attrs); - } -} - -/** - * gck_attributes_contains: - * @attrs: The attributes to check - * @match: The attribute to find - * - * Check whether the attributes contain a certain attribute. - * - * Returns: %TRUE if the attributes contain the attribute. - */ -gboolean -gck_attributes_contains (GckAttributes *attrs, GckAttribute *match) -{ - GckAttribute *attr; - guint i; - - g_return_val_if_fail (attrs && attrs->array, FALSE); - - for (i = 0; i < attrs->array->len; ++i) { - attr = gck_attributes_at (attrs, i); - if (gck_attribute_equal (attr, match)) - return TRUE; - } - - return FALSE; -} - - - -/* ------------------------------------------------------------------------------------------- - * INTERNAL - * - * The idea is that while we're processing a GckAttributes array (via PKCS#11 - * C_GetAtributeValue for example) the calling application shouldn't access those - * attributes at all, except to ref or unref them. - * - * We try to help debug this with our 'locked' states. The various processing - * functions that accept GckAttributes lock the attributes while handing - * them off to be processed (perhaps in a different thread). We check this locked - * flag in all public functions accessing GckAttributes. - * - * The reason we don't use thread safe or atomic primitives here, is because: - * a) The attributes are 'locked' by the same thread that prepares the call. - * b) This is a debugging feature, and should not be relied on for correctness. - */ - -void -_gck_attributes_lock (GckAttributes *attrs) -{ - g_assert (attrs); - g_assert (!attrs->locked); - attrs->locked = TRUE; -} - -void -_gck_attributes_unlock (GckAttributes *attrs) -{ - g_assert (attrs); - g_assert (attrs->locked); - attrs->locked = FALSE; -} - -CK_ATTRIBUTE_PTR -_gck_attributes_prepare_in (GckAttributes *attrs, CK_ULONG_PTR n_attrs) -{ - GckAttribute *attr; - guint i; - - g_assert (attrs); - g_assert (n_attrs); - g_assert (attrs->locked); - - /* Prepare the attributes to receive their length */ - - for (i = 0; i < attrs->array->len; ++i) { - attr = &g_array_index (attrs->array, GckAttribute, i); - attribute_clear (attr, attrs->allocator); - } - - *n_attrs = attrs->array->len; - return (CK_ATTRIBUTE_PTR)attrs->array->data; -} - -CK_ATTRIBUTE_PTR -_gck_attributes_commit_in (GckAttributes *attrs, CK_ULONG_PTR n_attrs) -{ - GckAttribute *attr; - guint i; - - g_assert (attrs); - g_assert (n_attrs); - g_assert (attrs->locked); - - /* Allocate each attribute with the length that was set */ - - for (i = 0; i < attrs->array->len; ++i) { - attr = &g_array_index (attrs->array, GckAttribute, i); - g_assert (!attr->value); - if (attr->length != 0 && attr->length != (gulong)-1) { - attr->value = (attrs->allocator) (NULL, attr->length); - g_assert (attr->value); - } - } - - *n_attrs = attrs->array->len; - return (CK_ATTRIBUTE_PTR)attrs->array->data; -} - -CK_ATTRIBUTE_PTR -_gck_attributes_commit_out (GckAttributes *attrs, CK_ULONG_PTR n_attrs) -{ - g_assert (attrs); - g_assert (n_attrs); - g_assert (attrs->locked); - - *n_attrs = attrs->array->len; - return (CK_ATTRIBUTE_PTR)attrs->array->data; -} - -static gboolean -_gck_attribute_is_ulong_of_type (GckAttribute *attr, - gulong attr_type) -{ - if (attr->type != attr_type) - return FALSE; - if (attr->length != sizeof (gulong)) - return FALSE; - if (!attr->value) - return FALSE; - return TRUE; -} - -static gboolean -_gck_attribute_is_sensitive (GckAttribute *attr) -{ - /* - * Don't print any just attribute, since they may contain - * sensitive data - */ - - switch (attr->type) { - #define X(x) case x: return FALSE; - X (CKA_CLASS) - X (CKA_TOKEN) - X (CKA_PRIVATE) - X (CKA_LABEL) - X (CKA_APPLICATION) - X (CKA_OBJECT_ID) - X (CKA_CERTIFICATE_TYPE) - X (CKA_ISSUER) - X (CKA_SERIAL_NUMBER) - X (CKA_AC_ISSUER) - X (CKA_OWNER) - X (CKA_ATTR_TYPES) - X (CKA_TRUSTED) - X (CKA_CERTIFICATE_CATEGORY) - X (CKA_JAVA_MIDP_SECURITY_DOMAIN) - X (CKA_URL) - X (CKA_HASH_OF_SUBJECT_PUBLIC_KEY) - X (CKA_HASH_OF_ISSUER_PUBLIC_KEY) - X (CKA_CHECK_VALUE) - X (CKA_KEY_TYPE) - X (CKA_SUBJECT) - X (CKA_ID) - X (CKA_SENSITIVE) - X (CKA_ENCRYPT) - X (CKA_DECRYPT) - X (CKA_WRAP) - X (CKA_UNWRAP) - X (CKA_SIGN) - X (CKA_SIGN_RECOVER) - X (CKA_VERIFY) - X (CKA_VERIFY_RECOVER) - X (CKA_DERIVE) - X (CKA_START_DATE) - X (CKA_END_DATE) - X (CKA_MODULUS_BITS) - X (CKA_PRIME_BITS) - /* X (CKA_SUBPRIME_BITS) */ - /* X (CKA_SUB_PRIME_BITS) */ - X (CKA_VALUE_BITS) - X (CKA_VALUE_LEN) - X (CKA_EXTRACTABLE) - X (CKA_LOCAL) - X (CKA_NEVER_EXTRACTABLE) - X (CKA_ALWAYS_SENSITIVE) - X (CKA_KEY_GEN_MECHANISM) - X (CKA_MODIFIABLE) - X (CKA_SECONDARY_AUTH) - X (CKA_AUTH_PIN_FLAGS) - X (CKA_ALWAYS_AUTHENTICATE) - X (CKA_WRAP_WITH_TRUSTED) - X (CKA_WRAP_TEMPLATE) - X (CKA_UNWRAP_TEMPLATE) - X (CKA_HW_FEATURE_TYPE) - X (CKA_RESET_ON_INIT) - X (CKA_HAS_RESET) - X (CKA_PIXEL_X) - X (CKA_PIXEL_Y) - X (CKA_RESOLUTION) - X (CKA_CHAR_ROWS) - X (CKA_CHAR_COLUMNS) - X (CKA_COLOR) - X (CKA_BITS_PER_PIXEL) - X (CKA_CHAR_SETS) - X (CKA_ENCODING_METHODS) - X (CKA_MIME_TYPES) - X (CKA_MECHANISM_TYPE) - X (CKA_REQUIRED_CMS_ATTRIBUTES) - X (CKA_DEFAULT_CMS_ATTRIBUTES) - X (CKA_SUPPORTED_CMS_ATTRIBUTES) - X (CKA_ALLOWED_MECHANISMS) - X (CKA_X_ASSERTION_TYPE) - X (CKA_X_CERTIFICATE_VALUE) - X (CKA_X_PURPOSE) - X (CKA_X_PEER) - #undef X - } - - return TRUE; -} - -static void -_gck_format_class (GString *output, - CK_OBJECT_CLASS klass) -{ - const gchar *string = NULL; - - switch (klass) { - #define X(x) case x: string = #x; break; - X (CKO_DATA) - X (CKO_CERTIFICATE) - X (CKO_PUBLIC_KEY) - X (CKO_PRIVATE_KEY) - X (CKO_SECRET_KEY) - X (CKO_HW_FEATURE) - X (CKO_DOMAIN_PARAMETERS) - X (CKO_MECHANISM) - X (CKO_X_TRUST_ASSERTION) - } - - if (string != NULL) - g_string_append (output, string); - else - g_string_append_printf (output, "0x%08lX", klass); -} - -static void -_gck_format_assertion_type (GString *output, - CK_X_ASSERTION_TYPE type) -{ - const gchar *string = NULL; - - switch (type) { - #define X(x) case x: string = #x; break; - X (CKT_X_UNTRUSTED_CERTIFICATE) - X (CKT_X_PINNED_CERTIFICATE) - X (CKT_X_ANCHORED_CERTIFICATE) - #undef X - } - - if (string != NULL) - g_string_append (output, string); - else - g_string_append_printf (output, "0x%08lX", type); -} - -static void -_gck_format_key_type (GString *output, - CK_KEY_TYPE type) -{ - const gchar *string = NULL; - - switch (type) { - #define X(x) case x: string = #x; break; - X (CKK_RSA) - X (CKK_DSA) - X (CKK_DH) - /* X (CKK_ECDSA) */ - X (CKK_EC) - X (CKK_X9_42_DH) - X (CKK_KEA) - X (CKK_GENERIC_SECRET) - X (CKK_RC2) - X (CKK_RC4) - X (CKK_DES) - X (CKK_DES2) - X (CKK_DES3) - X (CKK_CAST) - X (CKK_CAST3) - X (CKK_CAST128) - X (CKK_RC5) - X (CKK_IDEA) - X (CKK_SKIPJACK) - X (CKK_BATON) - X (CKK_JUNIPER) - X (CKK_CDMF) - X (CKK_AES) - X (CKK_BLOWFISH) - X (CKK_TWOFISH) - #undef X - } - - if (string != NULL) - g_string_append (output, string); - else - g_string_append_printf (output, "0x%08lX", type); -} - -static void -_gck_format_certificate_type (GString *output, - CK_CERTIFICATE_TYPE type) -{ - const gchar *string = NULL; - - switch (type) { - #define X(x) case x: string = #x; break; - X (CKC_X_509) - X (CKC_X_509_ATTR_CERT) - X (CKC_WTLS) - } - - if (string != NULL) - g_string_append (output, string); - else - g_string_append_printf (output, "0x%08lX", type); -} - -static void -_gck_format_attribute_type (GString *output, - gulong type) -{ - const gchar *string = NULL; - - switch (type) { - #define X(x) case x: string = #x; break; - X (CKA_CLASS) - X (CKA_TOKEN) - X (CKA_PRIVATE) - X (CKA_LABEL) - X (CKA_APPLICATION) - X (CKA_VALUE) - X (CKA_OBJECT_ID) - X (CKA_CERTIFICATE_TYPE) - X (CKA_ISSUER) - X (CKA_SERIAL_NUMBER) - X (CKA_AC_ISSUER) - X (CKA_OWNER) - X (CKA_ATTR_TYPES) - X (CKA_TRUSTED) - X (CKA_CERTIFICATE_CATEGORY) - X (CKA_JAVA_MIDP_SECURITY_DOMAIN) - X (CKA_URL) - X (CKA_HASH_OF_SUBJECT_PUBLIC_KEY) - X (CKA_HASH_OF_ISSUER_PUBLIC_KEY) - X (CKA_CHECK_VALUE) - X (CKA_KEY_TYPE) - X (CKA_SUBJECT) - X (CKA_ID) - X (CKA_SENSITIVE) - X (CKA_ENCRYPT) - X (CKA_DECRYPT) - X (CKA_WRAP) - X (CKA_UNWRAP) - X (CKA_SIGN) - X (CKA_SIGN_RECOVER) - X (CKA_VERIFY) - X (CKA_VERIFY_RECOVER) - X (CKA_DERIVE) - X (CKA_START_DATE) - X (CKA_END_DATE) - X (CKA_MODULUS) - X (CKA_MODULUS_BITS) - X (CKA_PUBLIC_EXPONENT) - X (CKA_PRIVATE_EXPONENT) - X (CKA_PRIME_1) - X (CKA_PRIME_2) - X (CKA_EXPONENT_1) - X (CKA_EXPONENT_2) - X (CKA_COEFFICIENT) - X (CKA_PRIME) - X (CKA_SUBPRIME) - X (CKA_BASE) - X (CKA_PRIME_BITS) - /* X (CKA_SUBPRIME_BITS) */ - /* X (CKA_SUB_PRIME_BITS) */ - X (CKA_VALUE_BITS) - X (CKA_VALUE_LEN) - X (CKA_EXTRACTABLE) - X (CKA_LOCAL) - X (CKA_NEVER_EXTRACTABLE) - X (CKA_ALWAYS_SENSITIVE) - X (CKA_KEY_GEN_MECHANISM) - X (CKA_MODIFIABLE) - X (CKA_ECDSA_PARAMS) - /* X (CKA_EC_PARAMS) */ - X (CKA_EC_POINT) - X (CKA_SECONDARY_AUTH) - X (CKA_AUTH_PIN_FLAGS) - X (CKA_ALWAYS_AUTHENTICATE) - X (CKA_WRAP_WITH_TRUSTED) - X (CKA_WRAP_TEMPLATE) - X (CKA_UNWRAP_TEMPLATE) - X (CKA_HW_FEATURE_TYPE) - X (CKA_RESET_ON_INIT) - X (CKA_HAS_RESET) - X (CKA_PIXEL_X) - X (CKA_PIXEL_Y) - X (CKA_RESOLUTION) - X (CKA_CHAR_ROWS) - X (CKA_CHAR_COLUMNS) - X (CKA_COLOR) - X (CKA_BITS_PER_PIXEL) - X (CKA_CHAR_SETS) - X (CKA_ENCODING_METHODS) - X (CKA_MIME_TYPES) - X (CKA_MECHANISM_TYPE) - X (CKA_REQUIRED_CMS_ATTRIBUTES) - X (CKA_DEFAULT_CMS_ATTRIBUTES) - X (CKA_SUPPORTED_CMS_ATTRIBUTES) - X (CKA_ALLOWED_MECHANISMS) - X (CKA_X_ASSERTION_TYPE) - X (CKA_X_CERTIFICATE_VALUE) - X (CKA_X_PURPOSE) - X (CKA_X_PEER) - #undef X - } - - if (string != NULL) - g_string_append (output, string); - else - g_string_append_printf (output, "CKA_0x%08lX", type); -} - -static void -_gck_format_some_bytes (GString *output, - gconstpointer bytes, - gulong length) -{ - guchar ch; - const guchar *data = bytes; - gulong i; - - if (bytes == NULL) { - g_string_append (output, "NULL"); - return; - } - - g_string_append_c (output, '\"'); - for (i = 0; i < length && i < 128; i++) { - ch = data[i]; - if (ch == '\t') - g_string_append (output, "\\t"); - else if (ch == '\n') - g_string_append (output, "\\n"); - else if (ch == '\r') - g_string_append (output, "\\r"); - else if (ch >= 32 && ch < 127) - g_string_append_c (output, ch); - else - g_string_append_printf (output, "\\x%02x", ch); - } - - if (i < length) - g_string_append_printf (output, "..."); - g_string_append_c (output, '\"'); -} - -static void -_gck_format_attributes (GString *output, - GckAttributes *attrs) -{ - GckAttribute *attr; - guint count, i; - - count = attrs->array->len; - g_string_append_printf (output, "(%d) [", count); - for (i = 0; i < count; i++) { - attr = &g_array_index (attrs->array, GckAttribute, i); - if (i > 0) - g_string_append_c (output, ','); - g_string_append (output, " { "); - _gck_format_attribute_type (output, attr->type); - g_string_append (output, " = "); - if (attr->length == GCK_INVALID) { - g_string_append_printf (output, " (-1) INVALID"); - } else if (_gck_attribute_is_ulong_of_type (attr, CKA_CLASS)) { - _gck_format_class (output, *((CK_OBJECT_CLASS_PTR)attr->value)); - } else if (_gck_attribute_is_ulong_of_type (attr, CKA_X_ASSERTION_TYPE)) { - _gck_format_assertion_type (output, *((CK_X_ASSERTION_TYPE *)attr->value)); - } else if (_gck_attribute_is_ulong_of_type (attr, CKA_CERTIFICATE_TYPE)) { - _gck_format_certificate_type (output, *((CK_CERTIFICATE_TYPE *)attr->value)); - } else if (_gck_attribute_is_ulong_of_type (attr, CKA_KEY_TYPE)) { - _gck_format_key_type (output, *((CK_KEY_TYPE *)attr->value)); - } else if (_gck_attribute_is_sensitive (attr)) { - g_string_append_printf (output, " (%lu) NOT-PRINTED", attr->length); - } else { - g_string_append_printf (output, " (%lu) ", attr->length); - _gck_format_some_bytes (output, attr->value, attr->length); - } - g_string_append (output, " }"); - } - g_string_append (output, " ]"); -} - -gchar * -_gck_attributes_format (GckAttributes *attrs) -{ - GString *output = g_string_sized_new (128); - _gck_format_attributes (output, attrs); - return g_string_free (output, FALSE); -} diff --git a/gck/gck-call.c b/gck/gck-call.c deleted file mode 100644 index f8393303..00000000 --- a/gck/gck-call.c +++ /dev/null @@ -1,542 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-call.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck-private.h" - -#include <string.h> - -typedef struct _GckCallSource GckCallSource; - -static gpointer _gck_call_parent_class = NULL; - -struct _GckCall { - GObject parent; - GckModule *module; - - /* For making the call */ - GckPerformFunc perform; - GckCompleteFunc complete; - GckArguments *args; - GCancellable *cancellable; - GDestroyNotify destroy; - CK_RV rv; - - /* For result callback only */ - gpointer object; - GAsyncReadyCallback callback; - gpointer user_data; -}; - -struct _GckCallClass { - GObjectClass parent; - GThreadPool *thread_pool; - GAsyncQueue *completed_queue; - guint completed_id; -}; - -struct _GckCallSource { - GSource source; - GckCallClass *klass; -}; - -/* ---------------------------------------------------------------------------- - * HELPER FUNCTIONS - */ - -static CK_RV -perform_call (GckPerformFunc func, GCancellable *cancellable, GckArguments *args) -{ - CK_RV rv; - - /* Double check a few things */ - g_assert (func); - g_assert (args); - - if (cancellable) { - if (g_cancellable_is_cancelled (cancellable)) { - return CKR_FUNCTION_CANCELED; - } - - /* Push for the notify callback */ - g_object_ref (cancellable); - g_cancellable_push_current (cancellable); - } - - rv = (func) (args); - - if (cancellable) { - g_cancellable_pop_current (cancellable); - g_object_unref (cancellable); - } - - return rv; -} - -static gboolean -complete_call (GckCompleteFunc func, GckArguments *args, CK_RV result) -{ - /* Double check a few things */ - g_assert (args); - - /* If no complete function, then just ignore */ - if (!func) - return TRUE; - - return (func) (args, result); -} - - -static void -process_async_call (gpointer data, GckCallClass *klass) -{ - GckCall *call = GCK_CALL (data); - - g_assert (GCK_IS_CALL (call)); - - call->rv = perform_call (call->perform, call->cancellable, call->args); - - g_async_queue_push (klass->completed_queue, call); - - /* Wakeup main thread if on a separate thread */ - g_main_context_wakeup (NULL); -} - -static void -process_result (GckCall *call, gpointer unused) -{ - gboolean stop = FALSE; - - /* Double check a few things */ - g_assert (GCK_IS_CALL (call)); - - if (call->cancellable) { - /* Don't call the callback when cancelled */ - if (g_cancellable_is_cancelled (call->cancellable)) { - call->rv = CKR_FUNCTION_CANCELED; - stop = TRUE; - } - } - - /* - * Hmmm, does the function want to actually be done? - * If not, then queue this call again. - */ - if (!stop && !complete_call (call->complete, call->args, call->rv)) { - g_object_ref (call); - g_thread_pool_push (GCK_CALL_GET_CLASS (call)->thread_pool, call, NULL); - - /* All done, finish processing */ - } else if (call->callback) { - (call->callback) (call->object, G_ASYNC_RESULT (call), - call->user_data); - } -} - -static gboolean -process_completed (GckCallClass *klass) -{ - gpointer call; - - g_assert (klass->completed_queue); - - call = g_async_queue_try_pop (klass->completed_queue); - if (call) { - process_result (call, NULL); - g_object_unref (call); - return TRUE; - } - - return FALSE; -} - -static gboolean -completed_prepare(GSource* base, gint *timeout) -{ - GckCallSource *source = (GckCallSource*)base; - gboolean have; - - g_assert (source->klass->completed_queue); - have = g_async_queue_length (source->klass->completed_queue) > 0; - *timeout = have ? 0 : -1; - return have; -} - -static gboolean -completed_check(GSource* base) -{ - GckCallSource *source = (GckCallSource*)base; - g_assert (source->klass->completed_queue); - return g_async_queue_length (source->klass->completed_queue) > 0; -} - -static gboolean -completed_dispatch(GSource* base, GSourceFunc callback, gpointer user_data) -{ - GckCallSource *source = (GckCallSource*)base; - process_completed (source->klass); - return TRUE; -} - -static void -completed_finalize(GSource* base) -{ - -} - -static GSourceFuncs completed_functions = { - completed_prepare, - completed_check, - completed_dispatch, - completed_finalize -}; - -/* ---------------------------------------------------------------------------- - * OBJECT - */ - -static void -_gck_call_init (GckCall *call) -{ - call->rv = CKR_OK; -} - -static void -_gck_call_finalize (GObject *obj) -{ - GckCall *call = GCK_CALL (obj); - - if (call->module) - g_object_unref (call->module); - call->module = NULL; - - if (call->object) - g_object_unref (call->object); - call->object = NULL; - - if (call->cancellable) - g_object_unref (call->cancellable); - call->cancellable = NULL; - - if (call->destroy) - (call->destroy) (call->args); - call->destroy = NULL; - call->args = NULL; - - G_OBJECT_CLASS (_gck_call_parent_class)->finalize (obj); -} - -static gpointer -_gck_call_get_user_data (GAsyncResult *async_result) -{ - g_return_val_if_fail (GCK_IS_CALL (async_result), NULL); - return GCK_CALL (async_result)->user_data; -} - -static GObject* -_gck_call_get_source_object (GAsyncResult *async_result) -{ - g_return_val_if_fail (GCK_IS_CALL (async_result), NULL); - return GCK_CALL (async_result)->object; -} - -static void -_gck_call_implement_async_result (GAsyncResultIface *iface) -{ - iface->get_user_data = _gck_call_get_user_data; - iface->get_source_object = _gck_call_get_source_object; -} - -static void -_gck_call_class_init (GckCallClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - - _gck_call_parent_class = g_type_class_peek_parent (klass); - gobject_class->finalize = _gck_call_finalize; -} - -static void -_gck_call_base_init (GckCallClass *klass) -{ - GckCallSource *source; - GMainContext *context; - GError *err = NULL; - - klass->thread_pool = g_thread_pool_new ((GFunc)process_async_call, klass, 16, FALSE, &err); - if (!klass->thread_pool) { - g_critical ("couldn't create thread pool: %s", - err && err->message ? err->message : ""); - return; - } - - klass->completed_queue = g_async_queue_new_full (g_object_unref); - g_assert (klass->completed_queue); - - context = g_main_context_default (); - g_assert (context); - - /* Add our idle handler which processes other tasks */ - source = (GckCallSource*)g_source_new (&completed_functions, sizeof (GckCallSource)); - source->klass = klass; - klass->completed_id = g_source_attach ((GSource*)source, context); - g_source_set_callback ((GSource*)source, NULL, NULL, NULL); - g_source_unref ((GSource*)source); -} - -static void -_gck_call_base_finalize (GckCallClass *klass) -{ - GMainContext *context; - GSource *src; - - if (klass->thread_pool) { - g_assert (g_thread_pool_unprocessed (klass->thread_pool) == 0); - g_thread_pool_free (klass->thread_pool, FALSE, TRUE); - klass->thread_pool = NULL; - } - - if (klass->completed_id) { - context = g_main_context_default (); - g_return_if_fail (context); - - src = g_main_context_find_source_by_id (context, klass->completed_id); - g_assert (src); - g_source_destroy (src); - klass->completed_id = 0; - } - - if (klass->completed_queue) { - g_assert (g_async_queue_length (klass->completed_queue)); - g_async_queue_unref (klass->completed_queue); - klass->completed_queue = NULL; - } -} - -GType -_gck_call_get_type (void) -{ - static volatile gsize type_id__volatile = 0; - - if (g_once_init_enter (&type_id__volatile)) { - - static const GTypeInfo type_info = { - sizeof (GckCallClass), - (GBaseInitFunc)_gck_call_base_init, - (GBaseFinalizeFunc)_gck_call_base_finalize, - (GClassInitFunc)_gck_call_class_init, - (GClassFinalizeFunc)NULL, - NULL, // class_data - sizeof (GckCall), - 0, // n_preallocs - (GInstanceInitFunc)_gck_call_init, - }; - - static const GInterfaceInfo interface_info = { - (GInterfaceInitFunc)_gck_call_implement_async_result - }; - - GType type_id = g_type_register_static (G_TYPE_OBJECT, "_GckCall", &type_info, 0); - g_type_add_interface_static (type_id, G_TYPE_ASYNC_RESULT, &interface_info); - - g_once_init_leave (&type_id__volatile, type_id); - } - - return type_id__volatile; -} - -/* ---------------------------------------------------------------------------- - * PUBLIC - */ - -void -_gck_call_uninitialize (void) -{ - -} - -gboolean -_gck_call_sync (gpointer object, gpointer perform, gpointer complete, - gpointer data, GCancellable *cancellable, GError **err) -{ - GckArguments *args = (GckArguments*)data; - GckModule *module = NULL; - CK_RV rv; - - g_assert (!object || G_IS_OBJECT (object)); - g_assert (perform); - g_assert (args); - - if (object) { - g_object_get (object, "module", &module, "handle", &args->handle, NULL); - g_assert (GCK_IS_MODULE (module)); - - /* We now hold a reference to module until below */ - args->pkcs11 = gck_module_get_functions (module); - g_assert (args->pkcs11); - } - - do { - rv = perform_call (perform, cancellable, args); - if (rv == CKR_FUNCTION_CANCELED) - break; - - } while (!complete_call (complete, args, rv)); - - if (module) - g_object_unref (module); - - if (rv == CKR_OK) - return TRUE; - - g_set_error (err, GCK_ERROR, rv, "%s", gck_message_from_rv (rv)); - return FALSE; -} - -gpointer -_gck_call_async_prep (gpointer object, gpointer cb_object, gpointer perform, - gpointer complete, gsize args_size, gpointer destroy) -{ - GckArguments *args; - GckCall *call; - - g_assert (!object || G_IS_OBJECT (object)); - g_assert (!cb_object || G_IS_OBJECT (cb_object)); - g_assert (perform); - - if (!destroy) - destroy = g_free; - - if (args_size == 0) - args_size = sizeof (GckArguments); - g_assert (args_size >= sizeof (GckArguments)); - - args = g_malloc0 (args_size); - call = g_object_new (GCK_TYPE_CALL, NULL); - call->destroy = (GDestroyNotify)destroy; - call->perform = (GckPerformFunc)perform; - call->complete = (GckCompleteFunc)complete; - call->object = cb_object ? g_object_ref (cb_object) : NULL; - - /* Hook the two together */ - call->args = args; - call->args->call = call; - - /* Setup call object if available */ - if (object != NULL) - _gck_call_async_object (call, object); - - return args; -} - -void -_gck_call_async_object (GckCall *call, gpointer object) -{ - g_assert (GCK_IS_CALL (call)); - g_assert (call->args); - - if (call->module) - g_object_unref (call->module); - call->module = NULL; - - g_object_get (object, "module", &call->module, "handle", &call->args->handle, NULL); - g_assert (GCK_IS_MODULE (call->module)); - call->args->pkcs11 = gck_module_get_functions (call->module); - - /* We now hold a reference on module until finalize */ -} - -GckCall* -_gck_call_async_ready (gpointer data, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckArguments *args = (GckArguments*)data; - g_assert (GCK_IS_CALL (args->call)); - - args->call->cancellable = cancellable; - if (cancellable) { - g_assert (G_IS_CANCELLABLE (cancellable)); - g_object_ref (cancellable); - } - - args->call->callback = callback; - args->call->user_data = user_data; - - return args->call; -} - -void -_gck_call_async_go (GckCall *call) -{ - g_assert (GCK_IS_CALL (call)); - - /* To keep things balanced, process at one completed event */ - process_completed(GCK_CALL_GET_CLASS (call)); - - g_assert (GCK_CALL_GET_CLASS (call)->thread_pool); - g_thread_pool_push (GCK_CALL_GET_CLASS (call)->thread_pool, call, NULL); -} - -void -_gck_call_async_ready_go (gpointer data, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckCall *call = _gck_call_async_ready (data, cancellable, callback, user_data); - _gck_call_async_go (call); -} - -gboolean -_gck_call_basic_finish (GAsyncResult *result, GError **err) -{ - CK_RV rv; - - g_return_val_if_fail (GCK_IS_CALL (result), FALSE); - - rv = GCK_CALL (result)->rv; - if (rv == CKR_OK) - return TRUE; - - g_set_error (err, GCK_ERROR, rv, "%s", gck_message_from_rv (rv)); - return FALSE; -} - -void -_gck_call_async_short (GckCall *call, CK_RV rv) -{ - g_assert (GCK_IS_CALL (call)); - - call->rv = rv; - - /* Already complete, so just push it for processing in main loop */ - g_assert (GCK_CALL_GET_CLASS (call)->completed_queue); - g_async_queue_push (GCK_CALL_GET_CLASS (call)->completed_queue, call); - g_main_context_wakeup (NULL); -} - -gpointer -_gck_call_get_arguments (GckCall *call) -{ - g_assert (GCK_IS_CALL (call)); - return call->args; -} diff --git a/gck/gck-debug.c b/gck/gck-debug.c deleted file mode 100644 index c30da4de..00000000 --- a/gck/gck-debug.c +++ /dev/null @@ -1,118 +0,0 @@ -/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ -/* - * Copyright (C) 2007 Collabora Ltd. - * Copyright (C) 2007 Nokia Corporation - * Copyright (C) 2011 Collabora Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "config.h" - -#include "gck.h" -#include "gck-debug.h" - -#include <glib.h> -#include <glib/gstdio.h> - -#include "pkcs11.h" - -#ifdef WITH_DEBUG - -static gsize initialized_flags = 0; -static GckDebugFlags current_flags = 0; - -static GDebugKey keys[] = { - { "session", GCK_DEBUG_SESSION }, - { "enumerator", GCK_DEBUG_ENUMERATOR }, - { 0, } -}; - -static void -debug_set_flags (GckDebugFlags new_flags) -{ - current_flags |= new_flags; -} - -void -_gck_debug_set_flags (const gchar *flags_string) -{ - guint nkeys; - - for (nkeys = 0; keys[nkeys].value; nkeys++); - - if (flags_string) - debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys)); -} - -static void -initialize_debug_flags (void) -{ - if (g_once_init_enter (&initialized_flags)) { - _gck_debug_set_flags (g_getenv ("GCK_DEBUG")); - g_once_init_leave (&initialized_flags, 1); - } -} - -gboolean -_gck_debug_flag_is_set (GckDebugFlags flag) -{ - if G_UNLIKELY (!initialized_flags) - initialize_debug_flags (); - return (flag & current_flags) != 0; -} - -void -_gck_debug_message (GckDebugFlags flag, - const gchar *format, - ...) -{ - gchar *message; - va_list args; - - if G_UNLIKELY (!initialized_flags) - initialize_debug_flags (); - - va_start (args, format); - message = g_strdup_vprintf (format, args); - va_end (args); - - if (flag & current_flags) - g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message); - - g_free (message); -} - -#else /* !WITH_DEBUG */ - -gboolean -_gck_debug_flag_is_set (GckDebugFlags flag) -{ - return FALSE; -} - -void -_gck_debug_message (GckDebugFlags flag, - const gchar *format, - ...) -{ -} - -void -_gck_debug_set_flags (const gchar *flags_string) -{ -} - -#endif /* !WITH_DEBUG */ diff --git a/gck/gck-debug.h b/gck/gck-debug.h deleted file mode 100644 index 42432925..00000000 --- a/gck/gck-debug.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) 2007 Nokia Corporation - * Copyright (C) 2007-2011 Collabora Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef GCK_DEBUG_H -#define GCK_DEBUG_H - -#include "config.h" - -#include <glib.h> - -G_BEGIN_DECLS - -/* Please keep this enum in sync with #keys in gck-debug.c */ -typedef enum { - GCK_DEBUG_SESSION = 1 << 1, - GCK_DEBUG_ENUMERATOR = 1 << 2, -} GckDebugFlags; - -gboolean _gck_debug_flag_is_set (GckDebugFlags flag); - -void _gck_debug_set_flags (const gchar *flags_string); - -void _gck_debug_message (GckDebugFlags flag, - const gchar *format, - ...) G_GNUC_PRINTF (2, 3); - -void _gck_debug_init (void); - -G_END_DECLS - -#endif /* GCK_DEBUG_H */ - -/* ----------------------------------------------------------------------------- - * Below this point is outside the GCK_DEBUG_H guard - so it can take effect - * more than once. So you can do: - * - * #define DEBUG_FLAG GCK_DEBUG_ONE_THING - * #include "gck-debug.h" - * ... - * DEBUG ("if we're debugging one thing"); - * ... - * #undef DEBUG_FLAG - * #define DEBUG_FLAG GCK_DEBUG_OTHER_THING - * #include "gck-debug.h" - * ... - * DEBUG ("if we're debugging the other thing"); - * ... - */ - -#ifdef DEBUG_FLAG -#ifdef WITH_DEBUG - -#undef _gck_debug -#define _gck_debug(format, ...) \ - _gck_debug_message (DEBUG_FLAG, "%s: " format, G_STRFUNC, ##__VA_ARGS__) - -#undef _gck_debugging -#define _gck_debugging \ - _gck_debug_flag_is_set (DEBUG_FLAG) - -#else /* !defined (WITH_DEBUG) */ - -#undef _gck_debug -#define _gck_debug(format, ...) \ - do {} while (0) - -#undef _gck_debugging -#define _gck_debugging 0 - -#endif /* !defined (WITH_DEBUG) */ - -#endif /* defined (DEBUG_FLAG) */ diff --git a/gck/gck-deprecated.h b/gck/gck-deprecated.h deleted file mode 100644 index 3121a9da..00000000 --- a/gck/gck-deprecated.h +++ /dev/null @@ -1,51 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-deprecated.h - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#ifndef GCK_DEPRECATED_H -#define GCK_DEPRECATED_H - -#include "gck.h" - -G_BEGIN_DECLS - -#ifndef GCK_DISABLE_DEPRECATED - -typedef GArray GckMechanisms; - -#define gck_mechanisms_free(a) (g_array_free (a, TRUE)) - -#define CKR_GCK_MODULE_PROBLEM GCK_ERROR_MODULE_PROBLEM - -GQuark gck_get_error_quark (void); - -GQuark gck_uri_get_error_quark (void); - -#define GCK_URI_BAD_PREFIX GCK_URI_BAD_SCHEME - -GType gck_attributes_get_boxed_type (void) G_GNUC_CONST; - -#endif /* GCK_DISABLE_DEPRECATED */ - -G_END_DECLS - -#endif /* GCK_H */ diff --git a/gck/gck-dump.c b/gck/gck-dump.c deleted file mode 100644 index 9003ce86..00000000 --- a/gck/gck-dump.c +++ /dev/null @@ -1,361 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-dump - the GObject PKCS#11 wrapper library - - Copyright (C) 2010 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" - -#include "egg/egg-hex.h" - -#include <stdlib.h> -#include <string.h> - -#include "pkcs11/pkcs11i.h" -#include "pkcs11/pkcs11x.h" - -static void -dump_class_value (gulong klass) -{ - switch (klass) { - #define DX(x) case x: g_printerr ("%s", #x); break; - DX(CKO_DATA); - DX(CKO_CERTIFICATE); - DX(CKO_PUBLIC_KEY); - DX(CKO_PRIVATE_KEY); - DX(CKO_SECRET_KEY); - DX(CKO_HW_FEATURE); - DX(CKO_DOMAIN_PARAMETERS); - DX(CKO_MECHANISM); - DX(CKO_G_COLLECTION); - DX(CKO_G_SEARCH); - DX(CKO_G_CREDENTIAL); - DX(CKO_X_TRUST_ASSERTION); - #undef DX - - default: - g_printerr ("%s0x%08x", - (klass & CKO_VENDOR_DEFINED) == CKA_VENDOR_DEFINED ? - "CKO_VENDOR_DEFINED|" : "", - (unsigned int)klass); - break; - } -} - -static void -dump_assertion_type_value (gulong type) -{ - switch (type) { - #define DX(x) case x: g_printerr ("%s", #x); break; - DX(CKT_X_DISTRUSTED_CERTIFICATE); - DX(CKT_X_PINNED_CERTIFICATE); - DX(CKT_X_ANCHORED_CERTIFICATE); - #undef DX - - default: - g_printerr ("%u", (unsigned int)type); - break; - } -} - -static void -dump_attribute_value (GckAttribute *attr) -{ - gchar *data; - gsize len; - - g_assert (attr->length != G_MAXULONG); - - if (attr->value == NULL) { - g_printerr ("[null]"); - return; - } - - switch (attr->type) { - case CKA_CLASS: - if (attr->length == sizeof (CK_OBJECT_CLASS)) { - dump_class_value (*(CK_ULONG_PTR)attr->value); - return; - } - break; - - case CKA_X_ASSERTION_TYPE: - if (attr->length == sizeof (CK_X_ASSERTION_TYPE)) { - dump_assertion_type_value (*(CK_X_ASSERTION_TYPE*)attr->value); - return; - } - break; - - case CKA_CERTIFICATE_TYPE: - case CKA_CERTIFICATE_CATEGORY: - case CKA_JAVA_MIDP_SECURITY_DOMAIN: - case CKA_KEY_TYPE: - case CKA_PRIME_BITS: - case CKA_SUB_PRIME_BITS: - case CKA_VALUE_BITS: - case CKA_VALUE_LEN: - case CKA_KEY_GEN_MECHANISM: - case CKA_HW_FEATURE_TYPE: - case CKA_PIXEL_X: - case CKA_PIXEL_Y: - case CKA_RESOLUTION: - case CKA_CHAR_ROWS: - case CKA_CHAR_COLUMNS: - case CKA_BITS_PER_PIXEL: - case CKA_MECHANISM_TYPE: - case CKA_G_DESTRUCT_IDLE: - case CKA_G_DESTRUCT_AFTER: - case CKA_G_DESTRUCT_USES: - case CKA_G_OBJECT: - case CKA_G_CREDENTIAL: - if (attr->length == sizeof (CK_ULONG)) { - g_printerr ("%llu", (unsigned long long)*(CK_ULONG_PTR)attr->value); - return; - } - break; - - case CKA_TOKEN: - case CKA_PRIVATE: - case CKA_TRUSTED: - case CKA_SENSITIVE: - case CKA_ENCRYPT: - case CKA_DECRYPT: - case CKA_WRAP: - case CKA_UNWRAP: - case CKA_SIGN: - case CKA_SIGN_RECOVER: - case CKA_VERIFY: - case CKA_VERIFY_RECOVER: - case CKA_DERIVE: - case CKA_EXTRACTABLE: - case CKA_LOCAL: - case CKA_NEVER_EXTRACTABLE: - case CKA_ALWAYS_SENSITIVE: - case CKA_MODIFIABLE: - case CKA_ALWAYS_AUTHENTICATE: - case CKA_WRAP_WITH_TRUSTED: - case CKA_RESET_ON_INIT: - case CKA_HAS_RESET: - case CKA_COLOR: - case CKA_G_LOCKED: - case CKA_G_LOGIN_COLLECTION: - if (attr->length == sizeof (CK_BBOOL)) { - g_printerr ("%s", (*(CK_BBOOL*)attr->value) ? "TRUE" : "FALSE"); - return; - } - break; - - case CKA_LABEL: - case CKA_URL: - case CKA_CHAR_SETS: - case CKA_ENCODING_METHODS: - case CKA_MIME_TYPES: - case CKA_G_COLLECTION: - case CKA_G_SCHEMA: - case CKA_X_PURPOSE: - case CKA_X_PEER: - if (g_utf8_validate ((const gchar *)attr->value, attr->length, NULL)) { - int length = MIN (32, attr->length); - g_printerr ("%.*s%s", length, (gchar*)attr->value, - length < attr->length ? "..." : ""); - return; - } - break; - - case CKA_START_DATE: - case CKA_END_DATE: - case CKA_G_CREATED: - case CKA_G_MODIFIED: - if (attr->length == sizeof (CK_DATE)) { - const CK_DATE* date = (const CK_DATE *)attr->value; - g_printerr ("%.4s-%.2s-%.2s", date->year, date->month, date->day); - return; - } - break; - - default: - break; - }; - - len = MIN (20, attr->length); - data = egg_hex_encode_full (attr->value, len, TRUE, ':', 1); - g_printerr ("%s%s", data, len < attr->length ? "..." : ""); - g_free (data); -} - -static void -dump_attribute_type (GckAttribute *attr) -{ - switch (attr->type) { - #define DX(x) case x: g_printerr ("%s", #x); break; - DX(CKA_CLASS); - DX(CKA_TOKEN); - DX(CKA_PRIVATE); - DX(CKA_LABEL); - DX(CKA_APPLICATION); - DX(CKA_VALUE); - DX(CKA_OBJECT_ID); - DX(CKA_CERTIFICATE_TYPE); - DX(CKA_ISSUER); - DX(CKA_SERIAL_NUMBER); - DX(CKA_AC_ISSUER); - DX(CKA_OWNER); - DX(CKA_ATTR_TYPES); - DX(CKA_TRUSTED); - DX(CKA_CERTIFICATE_CATEGORY); - DX(CKA_JAVA_MIDP_SECURITY_DOMAIN); - DX(CKA_URL); - DX(CKA_HASH_OF_SUBJECT_PUBLIC_KEY); - DX(CKA_HASH_OF_ISSUER_PUBLIC_KEY); - DX(CKA_CHECK_VALUE); - DX(CKA_KEY_TYPE); - DX(CKA_SUBJECT); - DX(CKA_ID); - DX(CKA_SENSITIVE); - DX(CKA_ENCRYPT); - DX(CKA_DECRYPT); - DX(CKA_WRAP); - DX(CKA_UNWRAP); - DX(CKA_SIGN); - DX(CKA_SIGN_RECOVER); - DX(CKA_VERIFY); - DX(CKA_VERIFY_RECOVER); - DX(CKA_DERIVE); - DX(CKA_START_DATE); - DX(CKA_END_DATE); - DX(CKA_MODULUS); - DX(CKA_MODULUS_BITS); - DX(CKA_PUBLIC_EXPONENT); - DX(CKA_PRIVATE_EXPONENT); - DX(CKA_PRIME_1); - DX(CKA_PRIME_2); - DX(CKA_EXPONENT_1); - DX(CKA_EXPONENT_2); - DX(CKA_COEFFICIENT); - DX(CKA_PRIME); - DX(CKA_SUBPRIME); - DX(CKA_BASE); - DX(CKA_PRIME_BITS); - DX(CKA_SUB_PRIME_BITS); - DX(CKA_VALUE_BITS); - DX(CKA_VALUE_LEN); - DX(CKA_EXTRACTABLE); - DX(CKA_LOCAL); - DX(CKA_NEVER_EXTRACTABLE); - DX(CKA_ALWAYS_SENSITIVE); - DX(CKA_KEY_GEN_MECHANISM); - DX(CKA_MODIFIABLE); - /* DX(CKA_ECDSA_PARAMS); */ - DX(CKA_EC_PARAMS); - DX(CKA_EC_POINT); - DX(CKA_SECONDARY_AUTH); - DX(CKA_AUTH_PIN_FLAGS); - DX(CKA_ALWAYS_AUTHENTICATE); - DX(CKA_WRAP_WITH_TRUSTED); - DX(CKA_HW_FEATURE_TYPE); - DX(CKA_RESET_ON_INIT); - DX(CKA_HAS_RESET); - DX(CKA_PIXEL_X); - DX(CKA_PIXEL_Y); - DX(CKA_RESOLUTION); - DX(CKA_CHAR_ROWS); - DX(CKA_CHAR_COLUMNS); - DX(CKA_COLOR); - DX(CKA_BITS_PER_PIXEL); - DX(CKA_CHAR_SETS); - DX(CKA_ENCODING_METHODS); - DX(CKA_MIME_TYPES); - DX(CKA_MECHANISM_TYPE); - DX(CKA_REQUIRED_CMS_ATTRIBUTES); - DX(CKA_DEFAULT_CMS_ATTRIBUTES); - DX(CKA_SUPPORTED_CMS_ATTRIBUTES); - DX(CKA_WRAP_TEMPLATE); - DX(CKA_UNWRAP_TEMPLATE); - DX(CKA_ALLOWED_MECHANISMS); - - /* GNOME */ - DX(CKA_G_LOCKED); - DX(CKA_G_CREATED); - DX(CKA_G_MODIFIED); - DX(CKA_G_FIELDS); - DX(CKA_G_COLLECTION); - DX(CKA_G_MATCHED); - DX(CKA_G_SCHEMA); - DX(CKA_G_LOGIN_COLLECTION); - DX(CKA_G_DESTRUCT_IDLE); - DX(CKA_G_DESTRUCT_AFTER); - DX(CKA_G_DESTRUCT_USES); - DX(CKA_G_OBJECT); - DX(CKA_G_CREDENTIAL); - DX(CKA_G_CREDENTIAL_TEMPLATE); - DX(CKA_X_ASSERTION_TYPE); - DX(CKA_X_CERTIFICATE_VALUE); - DX(CKA_X_PURPOSE); - DX(CKA_X_PEER); - #undef DX - - default: - g_printerr ("%s0x%08x", - (attr->type & CKA_VENDOR_DEFINED) == CKA_VENDOR_DEFINED ? - "CKA_VENDOR_DEFINED|" : "", - (unsigned int)attr->type); - break; - } -} - -/** - * gck_attribute_dump: - * @attr: The attribute - * - * Dump the specified attribute using g_printerr(). - */ -void -gck_attribute_dump (GckAttribute *attr) -{ - dump_attribute_type (attr); - if (attr->length == G_MAXULONG) { - g_printerr ("\n [invalid]\n"); - } else { - g_printerr ("\n [%lu] ", (unsigned long)attr->length); - dump_attribute_value (attr); - g_printerr ("\n"); - } -} - -/** - * gck_attributes_dump: - * @attrs: The attributes - * - * Dump the attributes using g_printerr(). - */ -void -gck_attributes_dump (GckAttributes *attrs) -{ - GckAttribute *attr; - guint i, count; - - for (i = 0, count = gck_attributes_count (attrs); i < count; ++i) { - attr = gck_attributes_at (attrs, i); - gck_attribute_dump (attr); - } -} diff --git a/gck/gck-enum-types.c.template b/gck/gck-enum-types.c.template deleted file mode 100644 index d6095351..00000000 --- a/gck/gck-enum-types.c.template +++ /dev/null @@ -1,43 +0,0 @@ -/*** BEGIN file-header ***/ - -#include <glib-object.h> - -#ifndef GCR_COMPILATION -#define GCR_COMPILATION -#endif - -/*** END file-header ***/ - -/*** BEGIN file-production ***/ -#include "@filename@" -/* enumerations from "@filename@" */ -/*** END file-production ***/ - -/*** BEGIN value-header ***/ -GType @enum_name@_get_type (void) G_GNUC_CONST; - -GType -@enum_name@_get_type (void) -{ - static GType etype = 0; - if (G_UNLIKELY(etype == 0)) { - static const G@Type@Value values[] = { -/*** END value-header ***/ - -/*** BEGIN value-production ***/ - { @VALUENAME@, "@VALUENAME@", "@valuenick@" }, -/*** END value-production ***/ - -/*** BEGIN value-tail ***/ - { 0, NULL, NULL } - }; - etype = g_@type@_register_static (g_intern_static_string ("@EnumName@"), values); - } - return etype; -} - -/*** END value-tail ***/ - -/*** BEGIN file-tail ***/ - /**/ -/*** END file-tail ***/ diff --git a/gck/gck-enum-types.h.template b/gck/gck-enum-types.h.template deleted file mode 100644 index ecb732b5..00000000 --- a/gck/gck-enum-types.h.template +++ /dev/null @@ -1,28 +0,0 @@ -/*** BEGIN file-header ***/ -#if !defined (__GCK_INSIDE_HEADER__) && !defined (GCK_COMPILATION) -#error "Only <gck/gck.h> can be included directly." -#endif - -#ifndef __GCK_ENUM_TYPES_H__ -#define __GCK_ENUM_TYPES_H__ - -#include <glib-object.h> - -G_BEGIN_DECLS -/*** END file-header ***/ - -/*** BEGIN file-production ***/ - -/* enumerations from "@filename@" */ -/*** END file-production ***/ - -/*** BEGIN value-header ***/ -GType @enum_name@_get_type (void) G_GNUC_CONST; -#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ()) -/*** END value-header ***/ - -/*** BEGIN file-tail ***/ -G_END_DECLS - -#endif /* __GCK_ENUM_TYPES_H__ */ -/*** END file-tail ***/ diff --git a/gck/gck-enumerator.c b/gck/gck-enumerator.c deleted file mode 100644 index c57297e9..00000000 --- a/gck/gck-enumerator.c +++ /dev/null @@ -1,917 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-enumerator.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2010, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#define DEBUG_FLAG GCK_DEBUG_ENUMERATOR -#include "gck-debug.h" -#include "gck-private.h" - -#include <string.h> - -/** - * SECTION:gck-enumerator - * @title: GckEnumerator - * @short_description: Enumerates through PKCS\#11 objects. - * - * A GckEnumerator can be used to enumerate through PKCS\#11 objects. It will - * automatically create sessions as necessary. - * - * Use gck_modules_enumerate_objects() or gck_modules_enumerate_uri() to create - * an enumerator. To get the objects use gck_enumerator_next() or - * gck_enumerator_next_async() functions. - */ - -enum { - PROP_0, - PROP_INTERACTION -}; - -/** - * GckEnumerator: - * @parent: derived from this. - * - * An object that allows enumerating of objects across modules, tokens. - */ - -typedef struct _GckEnumeratorState GckEnumeratorState; - -typedef gpointer (*GckEnumeratorFunc) (GckEnumeratorState *args, gboolean forward); - -struct _GckEnumeratorState { - /* For the current call */ - gint want_objects; - - /* The state we're currently in */ - GckEnumeratorFunc handler; - - /* Input to enumerator */ - GList *modules; - GckUriData *match; - GckSessionOptions session_options; - GTlsInteraction *interaction; - - /* state_slots */ - GList *slots; - - /* state_slot */ - GckSlot *slot; - GckTokenInfo *token_info; - CK_FUNCTION_LIST_PTR funcs; - - /* state_session */ - GckSession *session; - - /* state_results */ - GArray *objects; - - /* Output from enumerator */ - GList *results; -}; - -struct _GckEnumeratorPrivate { - GMutex *mutex; - GckEnumeratorState *the_state; - GTlsInteraction *interaction; -}; - -G_DEFINE_TYPE (GckEnumerator, gck_enumerator, G_TYPE_OBJECT); - -static gpointer state_modules (GckEnumeratorState *args, gboolean forward); -static gpointer state_slots (GckEnumeratorState *args, gboolean forward); -static gpointer state_slot (GckEnumeratorState *args, gboolean forward); -static gpointer state_session (GckEnumeratorState *args, gboolean forward); -static gpointer state_authenticated (GckEnumeratorState *args, gboolean forward); -static gpointer state_results (GckEnumeratorState *args, gboolean forward); - -/* ---------------------------------------------------------------------------- - * INTERNAL - */ - -static gpointer -rewind_state (GckEnumeratorState *args, GckEnumeratorFunc handler) -{ - g_assert (args); - g_assert (handler); - g_assert (args->handler); - - while (handler != args->handler) { - args->handler = (args->handler) (args, FALSE); - g_assert (args->handler); - } - - return handler; -} - -static void -cleanup_state (GckEnumeratorState *args) -{ - g_assert (args); - - /* Have each state cleanup */ - rewind_state (args, state_modules); - - /* state_slots */ - g_assert (!args->slots); - - /* state_slot */ - g_assert (!args->slot); - g_assert (!args->token_info); - g_assert (!args->funcs); - - /* state_session */ - g_assert (!args->session); - - /* state_results */ - if (args->objects) - g_array_free (args->objects, TRUE); - args->objects = NULL; - - /* Other cleanup */ - gck_list_unref_free (args->results); - args->results = NULL; - - gck_list_unref_free (args->modules); - args->modules = NULL; - - g_clear_object (&args->interaction); - - if (args->match) { - if (args->match->attributes) - _gck_attributes_unlock (args->match->attributes); - gck_uri_data_free (args->match); - args->match = NULL; - } -} - -static gpointer -state_modules (GckEnumeratorState *args, gboolean forward) -{ - GckModule *module; - - g_assert (args->slots == NULL); - - if (forward) { - - /* There are no more modules? */ - if (!args->modules) { - _gck_debug ("no more modules, stopping enumerator"); - return NULL; - } - - /* Pop off the current module */ - module = args->modules->data; - g_assert (GCK_IS_MODULE (module)); - args->modules = g_list_delete_link (args->modules, args->modules); - - args->slots = gck_module_get_slots (module, TRUE); - - if (_gck_debugging) { - GckModuleInfo *info = gck_module_get_info (module); - _gck_debug ("enumerating into module: %s", info->library_description); - gck_module_info_free (info); - } - - g_object_unref (module); - return state_slots; - } - - /* Should never be asked to go backward from start state */ - g_assert_not_reached (); -} - -static gpointer -state_slots (GckEnumeratorState *args, gboolean forward) -{ - GckSlot *slot; - GckModule *module; - GckTokenInfo *token_info; - gboolean matched; - - g_assert (args->slot == NULL); - - /* slots to slot state */ - if (forward) { - - /* If there are no more slots go back to start state */ - if (!args->slots) { - _gck_debug ("no more slots, want next module"); - return rewind_state (args, state_modules); - } - - /* Pop the next slot off the stack */ - slot = args->slots->data; - args->slots = g_list_delete_link (args->slots, args->slots); - - token_info = gck_slot_get_token_info (slot); - if (!token_info) { - g_message ("couldn't get token info while enumerating"); - g_object_unref (slot); - return rewind_state (args, state_modules); - } - - /* Do we have unrecognized matches? */ - if (args->match->any_unrecognized) { - _gck_debug ("token uri had unrecognized, not matching any tokens"); - matched = FALSE; - - /* Are we trying to match the slot? */ - } else if (args->match->token_info) { - /* No match? Go to next slot */ - matched = _gck_token_info_match (args->match->token_info, token_info); - - _gck_debug ("%s token: %s", matched ? "matched" : "did not match", - token_info->label); - - } else { - _gck_debug ("matching all tokens: %s", token_info->label); - matched = TRUE; - } - - if (!matched) { - g_object_unref (slot); - gck_token_info_free (token_info); - return state_slots; - } - - module = gck_slot_get_module (slot); - args->funcs = gck_module_get_functions (module); - g_assert (args->funcs); - g_object_unref (module); - - /* We have a slot */ - args->slot = slot; - args->token_info = token_info; - return state_slot; - - /* slots state to modules state */ - } else { - - gck_list_unref_free (args->slots); - args->slots = NULL; - return state_modules; - } -} - -static gpointer -state_slot (GckEnumeratorState *args, gboolean forward) -{ - CK_SESSION_HANDLE session; - CK_FLAGS flags; - CK_RV rv; - - g_assert (args->slot); - g_assert (args->funcs); - g_assert (args->session == NULL); - - /* slot to session state */ - if (forward) { - flags = CKF_SERIAL_SESSION; - if ((args->session_options & GCK_SESSION_READ_WRITE) == GCK_SESSION_READ_WRITE) - flags |= CKF_RW_SESSION; - - rv = (args->funcs->C_OpenSession) (gck_slot_get_handle (args->slot), - flags, NULL, NULL, &session); - - if (rv != CKR_OK) { - g_message ("couldn't open session on module while enumerating objects: %s", - gck_message_from_rv (rv)); - return rewind_state (args, state_slots); - } - - _gck_debug ("opened %s session", flags & CKF_RW_SESSION ? "read-write" : "read-only"); - args->session = gck_session_from_handle (args->slot, session, args->session_options); - return state_session; - - /* slot to slots state */ - } else { - g_object_unref (args->slot); - args->slot = NULL; - args->funcs = NULL; - - gck_token_info_free (args->token_info); - args->token_info = NULL; - - return state_slots; - } -} - -static gpointer -state_session (GckEnumeratorState *args, gboolean forward) -{ - GTlsInteraction *interaction; - CK_RV rv; - - g_assert (args->funcs); - g_assert (args->session); - g_assert (args->token_info); - - /* session to authenticated state */ - if (forward) { - - /* Don't want to authenticate? */ - if ((args->session_options & GCK_SESSION_LOGIN_USER) == 0) { - _gck_debug ("no authentication necessary, skipping"); - return state_authenticated; - } - - /* Compatibility, hook into GckModule signals if no interaction set */ - if (args->interaction) - interaction = g_object_ref (args->interaction); - else - interaction = _gck_interaction_new (args->slot); - - rv = _gck_session_authenticate_token (args->funcs, - gck_session_get_handle (args->session), - args->slot, interaction, NULL); - - g_object_unref (interaction); - - if (rv != CKR_OK) - g_message ("couldn't authenticate when enumerating: %s", gck_message_from_rv (rv)); - - /* We try to proceed anyway with the enumeration */ - return state_authenticated; - - /* Session to slot state */ - } else { - g_object_unref (args->session); - args->session = NULL; - return state_slot; - } -} - -static gpointer -state_authenticated (GckEnumeratorState *args, gboolean forward) -{ - CK_OBJECT_HANDLE objects[128]; - CK_SESSION_HANDLE session; - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs, count; - CK_RV rv; - - /* Just go back, no logout */ - if (!forward) - return state_session; - - /* This is where we do the actual searching */ - - g_assert (args->session); - g_assert (args->want_objects); - g_assert (args->funcs); - - if (args->match->attributes) { - attrs = _gck_attributes_commit_out (args->match->attributes, &n_attrs); - if (_gck_debugging) { - gchar *string = _gck_attributes_format (args->match->attributes); - _gck_debug ("finding objects matching: %s", string); - g_free (string); - } - } else { - attrs = NULL; - n_attrs = 0; - _gck_debug ("finding all objects"); - } - - session = gck_session_get_handle (args->session); - g_return_val_if_fail (session, NULL); - - /* Get all the objects */ - rv = (args->funcs->C_FindObjectsInit) (session, attrs, n_attrs); - - if (rv == CKR_OK) { - for(;;) { - rv = (args->funcs->C_FindObjects) (session, objects, G_N_ELEMENTS (objects), &count); - if (rv != CKR_OK || count == 0) - break; - - if (!args->objects) - args->objects = g_array_new (FALSE, TRUE, sizeof (CK_OBJECT_HANDLE)); - _gck_debug ("matched %lu objects", count); - g_array_append_vals (args->objects, objects, count); - } - - (args->funcs->C_FindObjectsFinal) (session); - } - - _gck_debug ("finding objects completed with: %s", _gck_stringize_rv (rv)); - return state_results; -} - -static GckObject* -extract_result (GckEnumeratorState *args) -{ - CK_OBJECT_HANDLE handle; - - if (!args->objects || !args->objects->len) - return NULL; - - g_assert (args->session); - - handle = g_array_index (args->objects, CK_OBJECT_HANDLE, 0); - g_array_remove_index_fast (args->objects, 0); - - return gck_object_from_handle (args->session, handle); -} - -static gpointer -state_results (GckEnumeratorState *args, gboolean forward) -{ - GckObject *object; - guint have; - - g_assert (args->session); - - /* No cleanup, just unwind */ - if (!forward) - return state_authenticated; - - /* Create result objects from what we have */ - have = g_list_length (args->results); - - while (have < args->want_objects) { - - object = extract_result (args); - if (!object) { - _gck_debug ("wanted %d objects, have %d, looking for more", - args->want_objects, have); - return rewind_state (args, state_slots); - } - - args->results = g_list_append (args->results, object); - ++have; - } - - _gck_debug ("wanted %d objects, returned %d objects", - args->want_objects, have); - - /* We got all the results we wanted */ - return NULL; -} - -/* ---------------------------------------------------------------------------- - * OBJECT - */ - -static void -gck_enumerator_init (GckEnumerator *self) -{ - self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_ENUMERATOR, GckEnumeratorPrivate); - self->pv->mutex = g_mutex_new (); - self->pv->the_state = g_new0 (GckEnumeratorState, 1); -} - -static void -gck_enumerator_get_property (GObject *obj, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - GckEnumerator *self = GCK_ENUMERATOR (obj); - - switch (prop_id) { - case PROP_INTERACTION: - g_value_take_object (value, gck_enumerator_get_interaction (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_enumerator_set_property (GObject *obj, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - GckEnumerator *self = GCK_ENUMERATOR (obj); - - switch (prop_id) { - case PROP_INTERACTION: - gck_enumerator_set_interaction (self, g_value_get_object (value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_enumerator_dispose (GObject *obj) -{ - GckEnumerator *self = GCK_ENUMERATOR (obj); - - gck_enumerator_set_interaction (self, NULL); - - G_OBJECT_CLASS (gck_enumerator_parent_class)->dispose (obj); -} - -static void -gck_enumerator_finalize (GObject *obj) -{ - GckEnumerator *self = GCK_ENUMERATOR (obj); - - g_assert (self->pv->interaction == NULL); - - g_assert (self->pv->the_state != NULL); - cleanup_state (self->pv->the_state); - g_free (self->pv->the_state); - - g_mutex_free (self->pv->mutex); - - G_OBJECT_CLASS (gck_enumerator_parent_class)->finalize (obj); -} - -static void -gck_enumerator_class_init (GckEnumeratorClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - - gobject_class->get_property = gck_enumerator_get_property; - gobject_class->set_property = gck_enumerator_set_property; - gobject_class->dispose = gck_enumerator_dispose; - gobject_class->finalize = gck_enumerator_finalize; - - g_type_class_add_private (klass, sizeof (GckEnumeratorPrivate)); - - /** - * GckEnumerator:interaction: - * - * Interaction object used to ask the user for pins when opening - * sessions. Used if the session_options of the enumerator have - * %GCK_SESSION_LOGIN_USER - */ - g_object_class_install_property (gobject_class, PROP_INTERACTION, - g_param_spec_object ("interaction", "Interaction", "Interaction asking for pins", - G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE)); -} - -/* ---------------------------------------------------------------------------- - * PUBLIC - */ - -GckEnumerator* -_gck_enumerator_new (GList *modules_or_slots, - GckSessionOptions session_options, - GckUriData *uri_data) -{ - GckEnumerator *self; - GckEnumeratorState *state; - - self = g_object_new (GCK_TYPE_ENUMERATOR, NULL); - state = self->pv->the_state; - - state->session_options = session_options; - - if (modules_or_slots && GCK_IS_SLOT (modules_or_slots->data)) { - state->slots = gck_list_ref_copy (modules_or_slots); - state->modules = NULL; - state->handler = state_slots; - } else { - state->modules = gck_list_ref_copy (modules_or_slots); - state->slots = NULL; - state->handler = state_modules; - } - - state->match = uri_data; - if (uri_data->attributes) - _gck_attributes_lock (uri_data->attributes); - - if (_gck_debugging) { - gchar *attrs, *uri; - attrs = uri_data->attributes ? _gck_attributes_format (uri_data->attributes) : NULL; - uri = uri_data ? gck_uri_build (uri_data, GCK_URI_FOR_TOKEN | GCK_URI_FOR_MODULE) : NULL; - _gck_debug ("new enumerator: tokens = %s, objects = %s", uri, attrs); - g_free (attrs); - g_free (uri); - } - - return self; -} - -typedef struct _EnumerateNext { - GckArguments base; - GckEnumeratorState *state; -} EnumerateNext; - -static CK_RV -perform_enumerate_next (EnumerateNext *args) -{ - GckEnumeratorFunc handler; - GckEnumeratorState *state; - - g_assert (args->state); - state = args->state; - - g_assert (state->handler); - - for (;;) { - handler = (state->handler) (state, TRUE); - if (!handler) - break; - state->handler = handler; - } - - /* TODO: In some modes, errors */ - return CKR_OK; -} - -static void -free_enumerate_next (EnumerateNext *args) -{ - /* Should have been assigned back to enumerator */ - g_assert (!args->state); - - g_free (args); -} - -/** - * gck_enumerator_get_interaction: - * @self: the enumerator - * - * Get the interaction used when a pin is needed - * - * Returns: (transfer full) (allow-none): the interaction or %NULL - */ -GTlsInteraction * -gck_enumerator_get_interaction (GckEnumerator *self) -{ - GTlsInteraction *result = NULL; - - g_return_val_if_fail (GCK_IS_ENUMERATOR (self), NULL); - - g_mutex_lock (self->pv->mutex); - - if (self->pv->interaction) - result = g_object_ref (self->pv->interaction); - - g_mutex_unlock (self->pv->mutex); - - return result; -} - -/** - * gck_enumerator_set_interaction: - * @self: the enumerator - * @interaction: (allow-none): the interaction or %NULL - * - * Set the interaction used when a pin is needed - */ -void -gck_enumerator_set_interaction (GckEnumerator *self, - GTlsInteraction *interaction) -{ - GTlsInteraction *previous = NULL; - - g_return_if_fail (GCK_IS_ENUMERATOR (self)); - g_return_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction)); - - g_mutex_lock (self->pv->mutex); - - if (interaction != self->pv->interaction) { - previous = self->pv->interaction; - self->pv->interaction = interaction; - if (interaction) - g_object_ref (interaction); - } - - g_mutex_unlock (self->pv->mutex); - - g_clear_object (&previous); - g_object_notify (G_OBJECT (self), "interaction"); -} - -static GckEnumeratorState * -check_out_enumerator_state (GckEnumerator *self) -{ - GckEnumeratorState *state = NULL; - - g_mutex_lock (self->pv->mutex); - - if (self->pv->the_state) { - state = self->pv->the_state; - self->pv->the_state = NULL; - - g_clear_object (&state->interaction); - if (self->pv->interaction) - state->interaction = g_object_ref (self->pv->interaction); - } - - g_mutex_unlock (self->pv->mutex); - - if (state == NULL) - g_warning ("this enumerator is already running a next operation"); - - return state; -} - -static void -check_in_enumerator_state (GckEnumerator *self, - GckEnumeratorState *state) -{ - g_mutex_lock (self->pv->mutex); - - g_assert (self->pv->the_state == NULL); - self->pv->the_state = state; - - g_mutex_unlock (self->pv->mutex); -} - -/** - * gck_enumerator_next: - * @self: The enumerator - * @cancellable: A #GCancellable or %NULL - * @error: A location to store an error on failure - * - * Get the next object in the enumerator, or %NULL if there are no more objects. - * - * %NULL is also returned if the function fails. Use the @error to determine - * whether a failure occurred or not. - * - * Returns: (transfer full) (allow-none): The next object, which must be released - * using g_object_unref, or %NULL. - */ -GckObject* -gck_enumerator_next (GckEnumerator *self, GCancellable *cancellable, GError **error) -{ - EnumerateNext args = { GCK_ARGUMENTS_INIT, NULL, }; - GckObject *result = NULL; - - g_return_val_if_fail (GCK_IS_ENUMERATOR (self), NULL); - g_return_val_if_fail (!error || !*error, NULL); - - args.state = check_out_enumerator_state (self); - g_return_val_if_fail (args.state != NULL, NULL); - - /* A result from a previous run? */ - result = extract_result (args.state); - if (!result) { - args.state->want_objects = 1; - - /* Run the operation and steal away the results */ - if (_gck_call_sync (NULL, perform_enumerate_next, NULL, &args, cancellable, error)) { - if (args.state->results) { - g_assert (g_list_length (args.state->results) == 1); - result = g_object_ref (args.state->results->data); - gck_list_unref_free (args.state->results); - args.state->results = NULL; - } - } - - args.state->want_objects = 0; - } - - /* Put the state back */ - check_in_enumerator_state (self, args.state); - - return result; -} - -/** - * gck_enumerator_next_n: - * @self: An enumerator - * @max_objects: The maximum amount of objects to enumerate - * @cancellable: A #GCancellable or %NULL - * @error: A location to store an error on failure - * - * Get the next set of objects from the enumerator. The maximum number of - * objects can be specified with @max_objects. If -1 is specified, then all - * the remaining objects will be returned. - * - * %NULL is also returned if the function fails. Use the @error to determine - * whether a failure occurred or not. - * - * Returns: (transfer full) (element-type Gck.Object): A list of objects, which - * should be freed using gck_list_unref_free(). - */ -GList* -gck_enumerator_next_n (GckEnumerator *self, gint max_objects, GCancellable *cancellable, - GError **error) -{ - EnumerateNext args = { GCK_ARGUMENTS_INIT, NULL, }; - GList *results = NULL; - - g_return_val_if_fail (GCK_IS_ENUMERATOR (self), NULL); - g_return_val_if_fail (max_objects == -1 || max_objects > 0, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - /* Remove the state and own it ourselves */ - args.state = check_out_enumerator_state (self); - g_return_val_if_fail (args.state != NULL, NULL); - - args.state->want_objects = max_objects <= 0 ? G_MAXINT : max_objects; - - /* Run the operation and steal away the results */ - if (_gck_call_sync (NULL, perform_enumerate_next, NULL, &args, cancellable, error)) { - results = args.state->results; - args.state->results = NULL; - } - - args.state->want_objects = 0; - - /* Put the state back */ - check_in_enumerator_state (self, args.state); - - return results; -} - -/** - * gck_enumerator_next_async: - * @self: An enumerator - * @max_objects: The maximum number of objects to get - * @cancellable: A #GCancellable or %NULL - * @callback: Called when the result is ready - * @user_data: Data to pass to the callback - * - * Get the next set of objects from the enumerator. This operation completes - * asynchronously.The maximum number of objects can be specified with - * @max_objects. If -1 is specified, then all the remaining objects will be - * enumerated. - */ -void -gck_enumerator_next_async (GckEnumerator *self, gint max_objects, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckEnumeratorState *state; - EnumerateNext *args; - - g_return_if_fail (GCK_IS_ENUMERATOR (self)); - g_return_if_fail (max_objects == -1 || max_objects > 0); - - g_object_ref (self); - - /* Remove the state and own it ourselves */ - state = check_out_enumerator_state (self); - g_return_if_fail (state != NULL); - - state->want_objects = max_objects <= 0 ? G_MAXINT : max_objects; - args = _gck_call_async_prep (NULL, self, perform_enumerate_next, NULL, - sizeof (*args), free_enumerate_next); - - args->state = state; - _gck_call_async_ready_go (args, cancellable, callback, user_data); - g_object_unref (self); -} - -/** - * gck_enumerator_next_finish: - * @self: An enumerator - * @result: The result passed to the callback - * @error: A location to raise an error on failure. - * - * Complete an operation to enumerate next objects. - * - * %NULL is also returned if the function fails. Use the @error to determine - * whether a failure occurred or not. - * - * Returns: (element-type Gck.Module) (transfer full): The list of objects, which - * should be freed with gck_list_unref_free() - */ -GList* -gck_enumerator_next_finish (GckEnumerator *self, GAsyncResult *result, GError **error) -{ - EnumerateNext *args; - GckEnumeratorState *state; - GList *results = NULL; - - g_object_ref (self); - - args = _gck_call_arguments (result, EnumerateNext); - state = args->state; - args->state = NULL; - state->want_objects = 0; - - if (_gck_call_basic_finish (result, error)) { - results = state->results; - state->results = NULL; - } - - /* Put the state back */ - check_in_enumerator_state (self, state); - - g_object_unref (self); - - return results; -} diff --git a/gck/gck-interaction.c b/gck/gck-interaction.c deleted file mode 100644 index 33d5ad35..00000000 --- a/gck/gck-interaction.c +++ /dev/null @@ -1,176 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-interaction.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck-private.h" - -#include <string.h> - -#define GCK_INTERACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCK_TYPE_INTERACTION, GckInteraction)) -#define GCK_IS_INTERACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCK_TYPE_INTERACTION)) -#define GCK_INTERACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCK_TYPE_INTERACTION, GckInteractionClass)) - -typedef struct _GckInteractionClass GckInteractionClass; - -struct _GckInteraction { - GTlsInteraction interaction; - GckModule *module; -}; - -struct _GckInteractionClass { - GTlsInteractionClass parent; -}; - -enum { - PROP_0, - PROP_MODULE -}; - -G_DEFINE_TYPE (GckInteraction, _gck_interaction, G_TYPE_TLS_INTERACTION); - -static void -_gck_interaction_init (GckInteraction *self) -{ - -} - -static void -_gck_interaction_get_property (GObject *obj, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - GckInteraction *self = GCK_INTERACTION (obj); - - switch (prop_id) { - case PROP_MODULE: - g_value_set_object (value, self->module); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -_gck_interaction_set_property (GObject *obj, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - GckInteraction *self = GCK_INTERACTION (obj); - - switch (prop_id) { - case PROP_MODULE: - g_return_if_fail (self->module == NULL); - self->module = g_value_dup_object (value); - g_return_if_fail (self->module != NULL); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -_gck_interaction_dispose (GObject *obj) -{ - GckInteraction *self = GCK_INTERACTION (obj); - - g_clear_object (&self->module); - - G_OBJECT_CLASS (_gck_interaction_parent_class)->dispose (obj); -} - -static GTlsInteractionResult -_gck_interaction_ask_password (GTlsInteraction *interaction, - GTlsPassword *password, - GCancellable *cancellable, - GError **error) -{ - GckInteraction *self = GCK_INTERACTION (interaction); - gchar *value = NULL; - gboolean ret = FALSE; - GckSlot *token; - GckObject *key; - - if (!self->module) - return G_TLS_INTERACTION_UNHANDLED; - - token = gck_password_get_token (GCK_PASSWORD (password)); - if (token != NULL) { - g_signal_emit_by_name (self->module, "authenticate-slot", token, - g_tls_password_get_description (password), - &value, &ret); - g_object_unref (token); - - } else { - key = gck_password_get_key (GCK_PASSWORD (password)); - g_return_val_if_fail (GCK_IS_OBJECT (key), G_TLS_INTERACTION_UNHANDLED); - - g_signal_emit_by_name (self->module, "authenticate-object", key, - g_tls_password_get_description (password), - &value, &ret); - } - - if (ret) { - g_tls_password_set_value_full (password, (guchar *)value, -1, g_free); - return G_TLS_INTERACTION_HANDLED; - } else { - return G_TLS_INTERACTION_UNHANDLED; - } -} - -static void -_gck_interaction_class_init (GckInteractionClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass); - - object_class->get_property = _gck_interaction_get_property; - object_class->set_property = _gck_interaction_set_property; - object_class->dispose = _gck_interaction_dispose; - - interaction_class->ask_password = _gck_interaction_ask_password; - - g_object_class_install_property (object_class, PROP_MODULE, - g_param_spec_object ("module", "Module", "PKCS11 Module", - GCK_TYPE_MODULE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); -} - -GTlsInteraction * -_gck_interaction_new (gpointer token_or_key) -{ - GTlsInteraction *result; - GModule *module = NULL; - - g_return_val_if_fail (GCK_IS_SLOT (token_or_key) || - GCK_IS_OBJECT (token_or_key), NULL); - - g_object_get (token_or_key, "module", &module, NULL); - result = g_object_new (GCK_TYPE_INTERACTION, "module", module, NULL); - g_object_unref (module); - - return result; -} diff --git a/gck/gck-marshal.list b/gck/gck-marshal.list deleted file mode 100644 index d9553afa..00000000 --- a/gck/gck-marshal.list +++ /dev/null @@ -1,3 +0,0 @@ -BOOLEAN:STRING,POINTER -BOOLEAN:OBJECT,STRING,POINTER -BOOLEAN:ULONG diff --git a/gck/gck-misc.c b/gck/gck-misc.c deleted file mode 100644 index 47d444e7..00000000 --- a/gck/gck-misc.c +++ /dev/null @@ -1,465 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-misc.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" - -#include <p11-kit/p11-kit.h> - -#include <glib/gi18n-lib.h> - -/** - * SECTION:gck-error - * @title: Errors - * @short_description: Gck Errors and error codes. - * - * Errors are returned as GError structures. The code member of GError - * contains the raw PKCS11 CK_RV result value. - */ - -/** - * SECTION:gck-private - * @title: Private, not used in docs - * @short_description: Should not show up in docs - * - */ - -/** - * GCK_INVALID: - * - * Used as a terminator at the end of variable argument lists. - */ - -/** - * GCK_VENDOR_CODE: - * - * Custom PKCS11 errors that originate from the gck library, are - * based at this error code. - */ - -/** - * GckError: - * @GCK_ERROR_MODULE_PROBLEM: a result code that signifies there was a problem - * loading a PKCS\#11 module, usually a shared library - * - * Various error codes. All the CKR_XXX error codes from PKCS\#11 are also - * relevant error codes. - */ - -/** - * GCK_ERROR: - * - * The error domain for gck library errors. - */ - -GQuark -gck_get_error_quark (void) -{ - /* This is the deprecated version */ - return gck_error_get_quark (); -} - -GQuark -gck_error_get_quark (void) -{ - static GQuark domain = 0; - static volatile gsize quark_inited = 0; - - if (g_once_init_enter (&quark_inited)) { - domain = g_quark_from_static_string ("gck-error"); - g_once_init_leave (&quark_inited, 1); - } - - return domain; -} - -/** - * gck_message_from_rv: - * @rv: The PKCS\#11 return value to get a message for. - * - * Get a message for a PKCS\#11 return value or error code. Do not - * pass CKR_OK or other such non errors to this function. - * - * Return value: The user readable message. - **/ -const gchar* -gck_message_from_rv (gulong rv) -{ - switch (rv) { - - /* These are not really errors, or not current */ - case CKR_OK: - case CKR_NO_EVENT: - case CKR_FUNCTION_NOT_PARALLEL: - case CKR_SESSION_PARALLEL_NOT_SUPPORTED: - g_return_val_if_reached (""); - - default: - return p11_kit_strerror (rv); - } -} - -const gchar * -_gck_stringize_rv (CK_RV rv) -{ - switch(rv) { - #define X(x) case x: return #x; - X (CKR_OK) - X (CKR_CANCEL) - X (CKR_HOST_MEMORY) - X (CKR_SLOT_ID_INVALID) - X (CKR_GENERAL_ERROR) - X (CKR_FUNCTION_FAILED) - X (CKR_ARGUMENTS_BAD) - X (CKR_NO_EVENT) - X (CKR_NEED_TO_CREATE_THREADS) - X (CKR_CANT_LOCK) - X (CKR_ATTRIBUTE_READ_ONLY) - X (CKR_ATTRIBUTE_SENSITIVE) - X (CKR_ATTRIBUTE_TYPE_INVALID) - X (CKR_ATTRIBUTE_VALUE_INVALID) - X (CKR_DATA_INVALID) - X (CKR_DATA_LEN_RANGE) - X (CKR_DEVICE_ERROR) - X (CKR_DEVICE_MEMORY) - X (CKR_DEVICE_REMOVED) - X (CKR_ENCRYPTED_DATA_INVALID) - X (CKR_ENCRYPTED_DATA_LEN_RANGE) - X (CKR_FUNCTION_CANCELED) - X (CKR_FUNCTION_NOT_PARALLEL) - X (CKR_FUNCTION_NOT_SUPPORTED) - X (CKR_KEY_HANDLE_INVALID) - X (CKR_KEY_SIZE_RANGE) - X (CKR_KEY_TYPE_INCONSISTENT) - X (CKR_KEY_NOT_NEEDED) - X (CKR_KEY_CHANGED) - X (CKR_KEY_NEEDED) - X (CKR_KEY_INDIGESTIBLE) - X (CKR_KEY_FUNCTION_NOT_PERMITTED) - X (CKR_KEY_NOT_WRAPPABLE) - X (CKR_KEY_UNEXTRACTABLE) - X (CKR_MECHANISM_INVALID) - X (CKR_MECHANISM_PARAM_INVALID) - X (CKR_OBJECT_HANDLE_INVALID) - X (CKR_OPERATION_ACTIVE) - X (CKR_OPERATION_NOT_INITIALIZED) - X (CKR_PIN_INCORRECT) - X (CKR_PIN_INVALID) - X (CKR_PIN_LEN_RANGE) - X (CKR_PIN_EXPIRED) - X (CKR_PIN_LOCKED) - X (CKR_SESSION_CLOSED) - X (CKR_SESSION_COUNT) - X (CKR_SESSION_HANDLE_INVALID) - X (CKR_SESSION_PARALLEL_NOT_SUPPORTED) - X (CKR_SESSION_READ_ONLY) - X (CKR_SESSION_EXISTS) - X (CKR_SESSION_READ_ONLY_EXISTS) - X (CKR_SESSION_READ_WRITE_SO_EXISTS) - X (CKR_SIGNATURE_INVALID) - X (CKR_SIGNATURE_LEN_RANGE) - X (CKR_TEMPLATE_INCOMPLETE) - X (CKR_TEMPLATE_INCONSISTENT) - X (CKR_TOKEN_NOT_PRESENT) - X (CKR_TOKEN_NOT_RECOGNIZED) - X (CKR_TOKEN_WRITE_PROTECTED) - X (CKR_UNWRAPPING_KEY_HANDLE_INVALID) - X (CKR_UNWRAPPING_KEY_SIZE_RANGE) - X (CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT) - X (CKR_USER_ALREADY_LOGGED_IN) - X (CKR_USER_NOT_LOGGED_IN) - X (CKR_USER_PIN_NOT_INITIALIZED) - X (CKR_USER_TYPE_INVALID) - X (CKR_USER_ANOTHER_ALREADY_LOGGED_IN) - X (CKR_USER_TOO_MANY_TYPES) - X (CKR_WRAPPED_KEY_INVALID) - X (CKR_WRAPPED_KEY_LEN_RANGE) - X (CKR_WRAPPING_KEY_HANDLE_INVALID) - X (CKR_WRAPPING_KEY_SIZE_RANGE) - X (CKR_WRAPPING_KEY_TYPE_INCONSISTENT) - X (CKR_RANDOM_SEED_NOT_SUPPORTED) - X (CKR_RANDOM_NO_RNG) - X (CKR_DOMAIN_PARAMS_INVALID) - X (CKR_BUFFER_TOO_SMALL) - X (CKR_SAVED_STATE_INVALID) - X (CKR_INFORMATION_SENSITIVE) - X (CKR_STATE_UNSAVEABLE) - X (CKR_CRYPTOKI_NOT_INITIALIZED) - X (CKR_CRYPTOKI_ALREADY_INITIALIZED) - X (CKR_MUTEX_BAD) - X (CKR_MUTEX_NOT_LOCKED) - X (CKR_FUNCTION_REJECTED) - X (CKR_VENDOR_DEFINED) - #undef X - default: - return "CKR_??????"; - } -} - -CK_RV -_gck_rv_from_error (GError *error, - CK_RV catch_all_code) -{ - g_return_val_if_fail (error != NULL, CKR_GENERAL_ERROR); - - if (error->domain == GCK_ERROR) - return error->code; - - if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - return CKR_FUNCTION_CANCELED; - - return catch_all_code; -} - -/** - * SECTION:gck-misc - * @title: Miscellaneous Functions - * @short_description: Other miscellaneous functions. - * - * A few supporting functions that come in handy when dealing with the gck - * library or PKCS11 in general. - */ - -GType -gck_list_get_boxed_type (void) -{ - static GType type = 0; - if (!type) - type = g_boxed_type_register_static ("GckList", - (GBoxedCopyFunc)gck_list_ref_copy, - (GBoxedFreeFunc)gck_list_unref_free); - return type; - -} - -/** - * gck_list_unref_free: (skip) - * @reflist: (element-type GLib.Object): list of Gobject reference counted pointers - * - * Free a list of GObject based pointers. All objects in the list - * will be unreffed and then the list itself will be freed. - **/ -void -gck_list_unref_free (GList *reflist) -{ - GList *l; - for (l = reflist; l; l = g_list_next (l)) { - g_return_if_fail (G_IS_OBJECT (l->data)); - g_object_unref (l->data); - } - g_list_free (reflist); -} - -/** - * gck_list_ref_copy: (skip) - * @reflist: (element-type GLib.Object): 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: (transfer full) (element-type GLib.Object): the copied and - * reffed list, when done, free it with gck_list_unref_free () - **/ -GList * -gck_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; -} - -/** - * gck_string_from_chars: (skip) - * @data: The character data to turn into a null terminated string. - * @max: The maximum length of the charater data. - * - * Create a string from a set of PKCS\#11 characters. This is - * similar to g_strndup, except for that it also strips trailing - * spaces. These space padded strings are often used in PKCS\#11 - * structures. - * - * If the space padded string is filled with null characters then - * this function will return %NULL. - * - * Return value: The null terminated string. - */ -gchar* -gck_string_from_chars (const guchar *data, gsize max) -{ - gchar *string; - - g_return_val_if_fail (data, NULL); - g_return_val_if_fail (max, NULL); - - /* Means no value */ - if (!data[0]) - return NULL; - - string = g_strndup ((gchar*)data, max); - g_strchomp (string); - return string; -} - -/** - * gck_string_to_chars: (skip) - * @data: The character buffer to place string into. - * @max: The maximum length of the charater buffer. - * @string: The string to place in the buffer. - * - * Create a space padded PKCS\#11 string from a null terminated string. - * The string must be shorter than the buffer or %FALSE will be - * returned. - * - * If a %NULL string is passed, then the space padded string will be - * set to zero characters. - * - * Return value: The null terminated string. - */ -gboolean -gck_string_to_chars (guchar *data, gsize max, const gchar *string) -{ - gsize len; - - g_return_val_if_fail (data, FALSE); - g_return_val_if_fail (max, FALSE); - - if (!string) { - memset (data, 0, max); - return TRUE; - } - - len = strlen (string); - if (len > max) - return FALSE; - - memset (data, ' ', max); - memcpy (data, string, len); - return TRUE; -} - -guint -_gck_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 -_gck_ulong_equal (gconstpointer v1, gconstpointer v2) -{ - return *((const gulong*)v1) == *((const gulong*)v2); -} - -/** - * gck_value_to_ulong: - * @value: (array length=length): memory to convert - * @length: length of memory - * @result: A location to store the result - * - * Convert CK_ULONG type memory to a boolean. - * - * Returns: Whether the conversion was successful. - */ -gboolean -gck_value_to_ulong (const guchar *value, - gsize length, - gulong *result) -{ - if (!value || length != sizeof (CK_ULONG)) - return FALSE; - if (result) - *result = *((CK_ULONG*)value); - return TRUE; -} - -/** - * gck_value_to_boolean: - * @value: (array length=length): memory to convert - * @length: length of memory - * @result: A location to store the result - * - * Convert CK_BBOOL type memory to a boolean. - * - * Returns: Whether the conversion was successful. - */ -gboolean -gck_value_to_boolean (const guchar *value, - gsize length, - gboolean *result) -{ - if (!value || length != sizeof (CK_BBOOL)) - return FALSE; - if (result) - *result = *((CK_BBOOL*)value) ? TRUE : FALSE; - return TRUE; -} - -static gboolean -match_info_string (const gchar *match, const gchar *string) -{ - /* NULL matches anything */ - if (match == NULL) - return TRUE; - - if (string == NULL) - return FALSE; - - return g_str_equal (match, string); -} - -gboolean -_gck_module_info_match (GckModuleInfo *match, GckModuleInfo *info) -{ - /* Matches two GckModuleInfo for use in PKCS#11 URI's */ - - g_return_val_if_fail (match, FALSE); - g_return_val_if_fail (info, FALSE); - - return (match_info_string (match->library_description, info->library_description) && - match_info_string (match->manufacturer_id, info->manufacturer_id)); -} - -gboolean -_gck_token_info_match (GckTokenInfo *match, GckTokenInfo *info) -{ - /* Matches two GckTokenInfo for use in PKCS#11 URI's */ - - g_return_val_if_fail (match, FALSE); - g_return_val_if_fail (info, FALSE); - - return (match_info_string (match->label, info->label) && - match_info_string (match->manufacturer_id, info->manufacturer_id) && - match_info_string (match->model, info->model) && - match_info_string (match->serial_number, info->serial_number)); -} diff --git a/gck/gck-mock.c b/gck/gck-mock.c deleted file mode 100644 index 5e7ccd25..00000000 --- a/gck/gck-mock.c +++ /dev/null @@ -1,1808 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2010 Stefan Walter - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General License for more details. - * - * You should have received a copy of the GNU Lesser General - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - */ - -#include "config.h" - -#include "gck.h" -#include "gck-mock.h" - -#include "pkcs11/pkcs11.h" -#include "pkcs11/pkcs11i.h" - -#include <glib.h> - -#include <string.h> - -/* - * This is *NOT* how you'd want to implement a PKCS#11 module. This - * fake module simply provides enough for gck library to test against. - * It doesn't pass any tests, or behave as expected from a PKCS#11 module. - */ - - -static gboolean initialized = FALSE; -static gchar *the_pin = NULL; -static gulong n_the_pin = 0; - -static gboolean logged_in = FALSE; -static CK_USER_TYPE user_type = 0; -static CK_FUNCTION_LIST functionList; - -typedef enum _Operation { - OP_FIND = 1, - OP_CRYPTO -} Operation; - -typedef struct _Session { - CK_SESSION_HANDLE handle; - CK_SESSION_INFO info; - GHashTable *objects; - - Operation operation; - - /* For find operations */ - GList *matches; - - /* For crypto operations */ - CK_OBJECT_HANDLE crypto_key; - CK_ATTRIBUTE_TYPE crypto_method; - CK_MECHANISM_TYPE crypto_mechanism; - CK_BBOOL want_context_login; - - /* For 'signing' with CKM_MOCK_PREFIX */ - CK_BYTE sign_prefix[128]; - CK_ULONG n_sign_prefix; -} Session; - -static guint unique_identifier = 100; -static GHashTable *the_sessions = NULL; -static GHashTable *the_objects = NULL; - -enum { - PRIVATE_KEY_CAPITALIZE = 3, - PUBLIC_KEY_CAPITALIZE = 4, - PRIVATE_KEY_PREFIX = 5, - PUBLIC_KEY_PREFIX = 6 -}; - -#define SIGNED_PREFIX "signed-prefix:" - -static void -free_session (gpointer data) -{ - Session *sess = (Session*)data; - if (sess) - g_hash_table_destroy (sess->objects); - g_free (sess); -} - -static GckAttributes* -lookup_object (Session *session, CK_OBJECT_HANDLE hObject) -{ - GckAttributes *attrs; - attrs = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (hObject)); - if (!attrs) - attrs = g_hash_table_lookup (session->objects, GUINT_TO_POINTER (hObject)); - return attrs; -} - -CK_OBJECT_HANDLE -gck_mock_module_take_object (GckAttributes *attrs) -{ - gboolean token; - guint handle; - - g_return_val_if_fail (the_objects, 0); - - handle = ++unique_identifier; - if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token)) - g_return_val_if_fail (token == TRUE, 0); - else - gck_attributes_add_boolean (attrs, CKA_TOKEN, TRUE); - g_hash_table_insert (the_objects, GUINT_TO_POINTER (handle), attrs); - return handle; -} - -void -gck_mock_module_enumerate_objects (CK_SESSION_HANDLE handle, GckMockEnumerator func, - gpointer user_data) -{ - GHashTableIter iter; - gpointer key; - gpointer value; - Session *session; - - g_assert (the_objects); - g_assert (func); - - /* Token objects */ - g_hash_table_iter_init (&iter, the_objects); - while (g_hash_table_iter_next (&iter, &key, &value)) { - if (!(func) (GPOINTER_TO_UINT (key), value, user_data)) - return; - } - - /* session objects */ - if (handle) { - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (handle)); - if (session) { - g_hash_table_iter_init (&iter, session->objects); - while (g_hash_table_iter_next (&iter, &key, &value)) { - if (!(func) (GPOINTER_TO_UINT (key), value, user_data)) - return; - } - } - } -} - -typedef struct _FindObject { - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - CK_OBJECT_HANDLE object; -} FindObject; - -static gboolean -enumerate_and_find_object (CK_OBJECT_HANDLE object, GckAttributes *attrs, gpointer user_data) -{ - FindObject *ctx = user_data; - CK_ATTRIBUTE_PTR match; - GckAttribute *attr; - CK_ULONG i; - - for (i = 0; i < ctx->n_attrs; ++i) { - match = ctx->attrs + i; - attr = gck_attributes_find (attrs, match->type); - if (!attr) - return TRUE; /* Continue */ - - if (attr->length != match->ulValueLen || - memcmp (attr->value, match->pValue, attr->length) != 0) - return TRUE; /* Continue */ - } - - ctx->object = object; - return FALSE; /* Stop iteration */ -} - -CK_OBJECT_HANDLE -gck_mock_module_find_object (CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs) -{ - FindObject ctx; - - ctx.attrs = attrs; - ctx.n_attrs = n_attrs; - ctx.object = 0; - - gck_mock_module_enumerate_objects (session, enumerate_and_find_object, &ctx); - - return ctx.object; -} - -static gboolean -enumerate_and_count_objects (CK_OBJECT_HANDLE object, GckAttributes *attrs, gpointer user_data) -{ - guint *n_objects = user_data; - ++(*n_objects); - return TRUE; /* Continue */ -} - -guint -gck_mock_module_count_objects (CK_SESSION_HANDLE session) -{ - guint n_objects = 0; - gck_mock_module_enumerate_objects (session, enumerate_and_count_objects, &n_objects); - return n_objects; -} - -void -gck_mock_module_set_object (CK_OBJECT_HANDLE object, CK_ATTRIBUTE_PTR attrs, - CK_ULONG n_attrs) -{ - CK_ULONG i; - GckAttributes *atts; - GckAttribute *attr; - CK_ATTRIBUTE_PTR set; - - g_return_if_fail (object != 0); - g_return_if_fail (the_objects); - - atts = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (object)); - g_return_if_fail (atts); - - for (i = 0; i < n_attrs; ++i) { - set = attrs + i; - attr = gck_attributes_find (atts, set->type); - if (!attr) { - gck_attributes_add_data (atts, set->type, set->pValue, set->ulValueLen); - } else { - gck_attribute_clear (attr); - gck_attribute_init (attr, set->type, set->pValue, set->ulValueLen); - } - } -} - -void -gck_mock_module_set_pin (const gchar *password) -{ - g_free (the_pin); - the_pin = g_strdup (password); - n_the_pin = strlen (password); -} - -CK_RV -gck_mock_C_Initialize (CK_VOID_PTR pInitArgs) -{ - GckAttributes *attrs; - CK_ULONG value; - CK_C_INITIALIZE_ARGS_PTR args; - - g_return_val_if_fail (initialized == FALSE, CKR_CRYPTOKI_ALREADY_INITIALIZED); - - args = (CK_C_INITIALIZE_ARGS_PTR)pInitArgs; - if (args) { - g_return_val_if_fail( - (args->CreateMutex == NULL && args->DestroyMutex == NULL && - args->LockMutex == NULL && args->UnlockMutex == NULL) || - (args->CreateMutex != NULL && args->DestroyMutex != NULL && - args->LockMutex != NULL && args->UnlockMutex != NULL), - CKR_ARGUMENTS_BAD); - - /* Flags should allow OS locking and os threads */ - g_return_val_if_fail ((args->flags & CKF_OS_LOCKING_OK), CKR_CANT_LOCK); - g_return_val_if_fail ((args->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) == 0, CKR_NEED_TO_CREATE_THREADS); - } - - the_pin = g_strdup ("booo"); - n_the_pin = strlen (the_pin); - the_sessions = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, free_session); - the_objects = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gck_attributes_unref); - - /* Our token object */ - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST LABEL"); - g_hash_table_insert (the_objects, GUINT_TO_POINTER (2), attrs); - - /* Private capitalize key */ - value = CKM_MOCK_CAPITALIZE; - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PRIVATE_KEY); - gck_attributes_add_string (attrs, CKA_LABEL, "Private Capitalize Key"); - gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, (const guchar *)&value, sizeof (value)); - gck_attributes_add_boolean (attrs, CKA_DECRYPT, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_WRAP, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_UNWRAP, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_DERIVE, CK_TRUE); - gck_attributes_add_string (attrs, CKA_VALUE, "value"); - gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique1"); - g_hash_table_insert (the_objects, GUINT_TO_POINTER (PRIVATE_KEY_CAPITALIZE), attrs); - - /* Public capitalize key */ - value = CKM_MOCK_CAPITALIZE; - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PUBLIC_KEY); - gck_attributes_add_string (attrs, CKA_LABEL, "Public Capitalize Key"); - gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, (const guchar *)&value, sizeof (value)); - gck_attributes_add_boolean (attrs, CKA_ENCRYPT, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_FALSE); - gck_attributes_add_string (attrs, CKA_VALUE, "value"); - gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique2"); - g_hash_table_insert (the_objects, GUINT_TO_POINTER (PUBLIC_KEY_CAPITALIZE), attrs); - - /* Private prefix key */ - value = CKM_MOCK_PREFIX; - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PRIVATE_KEY); - gck_attributes_add_string (attrs, CKA_LABEL, "Private prefix key"); - gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, (const guchar *)&value, sizeof (value)); - gck_attributes_add_boolean (attrs, CKA_SIGN, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_ALWAYS_AUTHENTICATE, CK_TRUE); - gck_attributes_add_string (attrs, CKA_VALUE, "value"); - gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique3"); - g_hash_table_insert (the_objects, GUINT_TO_POINTER (PRIVATE_KEY_PREFIX), attrs); - - /* Private prefix key */ - value = CKM_MOCK_PREFIX; - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PUBLIC_KEY); - gck_attributes_add_string (attrs, CKA_LABEL, "Public prefix key"); - gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, (const guchar *)&value, sizeof (value)); - gck_attributes_add_boolean (attrs, CKA_VERIFY, CK_TRUE); - gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_FALSE); - gck_attributes_add_string (attrs, CKA_VALUE, "value"); - gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique4"); - g_hash_table_insert (the_objects, GUINT_TO_POINTER (PUBLIC_KEY_PREFIX), attrs); - - initialized = TRUE; - return CKR_OK; -} - -CK_RV -gck_mock_validate_and_C_Initialize (CK_VOID_PTR pInitArgs) -{ - CK_C_INITIALIZE_ARGS_PTR args; - void *mutex; - CK_RV rv; - - args = (CK_C_INITIALIZE_ARGS_PTR)pInitArgs; - if (args) { - g_assert ((args->CreateMutex) (NULL) == CKR_ARGUMENTS_BAD && "CreateMutex succeeded wrong"); - g_assert ((args->DestroyMutex) (NULL) == CKR_MUTEX_BAD && "DestroyMutex succeeded wrong"); - g_assert ((args->LockMutex) (NULL) == CKR_MUTEX_BAD && "LockMutex succeeded wrong"); - g_assert ((args->UnlockMutex) (NULL) == CKR_MUTEX_BAD && "UnlockMutex succeeded wrong"); - - /* Try to create an actual mutex */ - rv = (args->CreateMutex) (&mutex); - g_assert (rv == CKR_OK && "CreateMutex g_assert_not_reacheded"); - g_assert (mutex != NULL && "CreateMutex created null mutex"); - - /* Try and lock the mutex */ - rv = (args->LockMutex) (mutex); - g_assert (rv == CKR_OK && "LockMutex g_assert_not_reacheded"); - - /* Try and unlock the mutex */ - rv = (args->UnlockMutex) (mutex); - g_assert (rv == CKR_OK && "UnlockMutex g_assert_not_reacheded"); - - /* Try and destroy the mutex */ - rv = (args->DestroyMutex) (mutex); - g_assert (rv == CKR_OK && "DestroyMutex g_assert_not_reacheded"); - } - - return gck_mock_C_Initialize (pInitArgs); -} - -CK_RV -gck_mock_C_Finalize (CK_VOID_PTR pReserved) -{ - g_return_val_if_fail (pReserved == NULL, CKR_ARGUMENTS_BAD); - g_return_val_if_fail (initialized == TRUE, CKR_CRYPTOKI_NOT_INITIALIZED); - - initialized = FALSE; - logged_in = FALSE; - g_hash_table_destroy (the_objects); - the_objects = NULL; - - g_hash_table_destroy (the_sessions); - the_sessions = NULL; - - g_free (the_pin); - return CKR_OK; -} - -static const CK_INFO TEST_INFO = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, - "TEST MANUFACTURER ", - 0, - "TEST LIBRARY ", - { 45, 145 } -}; - -CK_RV -gck_mock_C_GetInfo (CK_INFO_PTR pInfo) -{ - g_return_val_if_fail (pInfo, CKR_ARGUMENTS_BAD); - memcpy (pInfo, &TEST_INFO, sizeof (*pInfo)); - return CKR_OK; -} - -CK_RV -gck_mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) -{ - g_return_val_if_fail (list, CKR_ARGUMENTS_BAD); - *list = &functionList; - return CKR_OK; -} - -/* - * Two slots - * ONE: token present - * TWO: token not present - */ - -CK_RV -gck_mock_C_GetSlotList (CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) -{ - CK_ULONG count; - - g_return_val_if_fail (pulCount, CKR_ARGUMENTS_BAD); - - count = tokenPresent ? 1 : 2; - - /* Application only wants to know the number of slots. */ - if (pSlotList == NULL) { - *pulCount = count; - return CKR_OK; - } - - if (*pulCount < count) - g_return_val_if_reached (CKR_BUFFER_TOO_SMALL); - - *pulCount = count; - pSlotList[0] = GCK_MOCK_SLOT_ONE_ID; - if (!tokenPresent) - pSlotList[1] = GCK_MOCK_SLOT_TWO_ID; - - return CKR_OK; -} - -/* Update gck-mock.h URIs when updating this */ - -static const CK_SLOT_INFO TEST_INFO_ONE = { - "TEST SLOT ", - "TEST MANUFACTURER ", - CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE, - { 55, 155 }, - { 65, 165 }, -}; - -/* Update gck-mock.h URIs when updating this */ - -static const CK_SLOT_INFO TEST_INFO_TWO = { - "TEST SLOT ", - "TEST MANUFACTURER ", - CKF_REMOVABLE_DEVICE, - { 55, 155 }, - { 65, 165 }, -}; - -CK_RV -gck_mock_C_GetSlotInfo (CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) -{ - g_return_val_if_fail (pInfo, CKR_ARGUMENTS_BAD); - - if (slotID == GCK_MOCK_SLOT_ONE_ID) { - memcpy (pInfo, &TEST_INFO_ONE, sizeof (*pInfo)); - return CKR_OK; - } else if (slotID == GCK_MOCK_SLOT_TWO_ID) { - memcpy (pInfo, &TEST_INFO_TWO, sizeof (*pInfo)); - return CKR_OK; - } else { - g_return_val_if_reached (CKR_SLOT_ID_INVALID); - } -} - -/* Update gck-mock.h URIs when updating this */ - -static const CK_TOKEN_INFO TEST_TOKEN_ONE = { - "TEST LABEL ", - "TEST MANUFACTURER ", - "TEST MODEL ", - "TEST SERIAL ", - CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - { 75, 175 }, - { 85, 185 }, - { '1', '9', '9', '9', '0', '5', '2', '5', '0', '9', '1', '9', '5', '9', '0', '0' } -}; - -CK_RV -gck_mock_C_GetTokenInfo (CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) -{ - g_return_val_if_fail (pInfo != NULL, CKR_ARGUMENTS_BAD); - - if (slotID == GCK_MOCK_SLOT_ONE_ID) { - memcpy (pInfo, &TEST_TOKEN_ONE, sizeof (*pInfo)); - return CKR_OK; - } else if (slotID == GCK_MOCK_SLOT_TWO_ID) { - return CKR_TOKEN_NOT_PRESENT; - } else { - g_return_val_if_reached (CKR_SLOT_ID_INVALID); - } -} - -CK_RV -gck_mock_fail_C_GetTokenInfo (CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) -{ - return CKR_GENERAL_ERROR; -} - -/* - * TWO mechanisms: - * CKM_MOCK_CAPITALIZE - * CKM_MOCK_PREFIX - */ - -CK_RV -gck_mock_C_GetMechanismList (CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList, - CK_ULONG_PTR pulCount) -{ - g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID); - g_return_val_if_fail (pulCount, CKR_ARGUMENTS_BAD); - - /* Application only wants to know the number of slots. */ - if (pMechanismList == NULL) { - *pulCount = 2; - return CKR_OK; - } - - if (*pulCount != 2) - g_return_val_if_reached (CKR_BUFFER_TOO_SMALL); - - pMechanismList[0] = CKM_MOCK_CAPITALIZE; - pMechanismList[1] = CKM_MOCK_PREFIX; - return CKR_OK; -} - -static const CK_MECHANISM_INFO TEST_MECH_CAPITALIZE = { - 512, 4096, 0 -}; - -static const CK_MECHANISM_INFO TEST_MECH_PREFIX = { - 2048, 2048, 0 -}; - -CK_RV -gck_mock_C_GetMechanismInfo (CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR pInfo) -{ - g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID); - g_return_val_if_fail (pInfo, CKR_ARGUMENTS_BAD); - - if (type == CKM_MOCK_CAPITALIZE) { - memcpy (pInfo, &TEST_MECH_CAPITALIZE, sizeof (*pInfo)); - return CKR_OK; - } else if (type == CKM_MOCK_PREFIX) { - memcpy (pInfo, &TEST_MECH_PREFIX, sizeof (*pInfo)); - return CKR_OK; - } else { - g_return_val_if_reached (CKR_MECHANISM_INVALID); - } -} - -CK_RV -gck_mock_specific_args_C_InitToken (CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen, - CK_UTF8CHAR_PTR pLabel) -{ - g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID); - - g_return_val_if_fail (pPin, CKR_PIN_INVALID); - g_return_val_if_fail (strlen ("TEST PIN") == ulPinLen, CKR_PIN_INVALID); - g_return_val_if_fail (strncmp ((gchar*)pPin, "TEST PIN", ulPinLen) == 0, CKR_PIN_INVALID); - g_return_val_if_fail (pLabel != NULL, CKR_PIN_INVALID); - g_return_val_if_fail (strcmp ((gchar*)pPin, "TEST LABEL") == 0, CKR_PIN_INVALID); - - g_free (the_pin); - the_pin = g_strndup ((gchar*)pPin, ulPinLen); - n_the_pin = ulPinLen; - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_WaitForSlotEvent (CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_OpenSession (CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, - CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession) -{ - Session *sess; - - g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID || slotID == GCK_MOCK_SLOT_TWO_ID, CKR_SLOT_ID_INVALID); - g_return_val_if_fail (phSession != NULL, CKR_ARGUMENTS_BAD); - g_return_val_if_fail ((flags & CKF_SERIAL_SESSION) == CKF_SERIAL_SESSION, CKR_SESSION_PARALLEL_NOT_SUPPORTED); - - sess = g_new0 (Session, 1); - sess->handle = ++unique_identifier; - sess->info.flags = flags; - sess->info.slotID = slotID; - sess->info.state = 0; - sess->info.ulDeviceError = 1414; - sess->objects = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gck_attributes_unref); - *phSession = sess->handle; - - g_hash_table_replace (the_sessions, GUINT_TO_POINTER (sess->handle), sess); - return CKR_OK; -} - -CK_RV -gck_mock_fail_C_OpenSession (CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, - CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession) -{ - return CKR_GENERAL_ERROR; -} - -CK_RV -gck_mock_C_CloseSession (CK_SESSION_HANDLE hSession) -{ - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - g_hash_table_remove (the_sessions, GUINT_TO_POINTER (hSession)); - return CKR_OK; -} - -CK_RV -gck_mock_C_CloseAllSessions (CK_SLOT_ID slotID) -{ - g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID); - - g_hash_table_remove_all (the_sessions); - return CKR_OK; -} - -CK_RV -gck_mock_C_GetFunctionStatus (CK_SESSION_HANDLE hSession) -{ - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -gck_mock_C_CancelFunction (CK_SESSION_HANDLE hSession) -{ - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -gck_mock_C_GetSessionInfo (CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo) -{ - Session *session; - - g_return_val_if_fail (pInfo != NULL, CKR_ARGUMENTS_BAD); - - 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; - - if (logged_in) { - if (session->info.flags & CKF_RW_SESSION) - session->info.state = CKS_RW_USER_FUNCTIONS; - else - session->info.state = CKS_RO_USER_FUNCTIONS; - } else { - if (session->info.flags & CKF_RW_SESSION) - session->info.state = CKS_RW_PUBLIC_SESSION; - else - session->info.state = CKS_RO_PUBLIC_SESSION; - } - - memcpy (pInfo, &session->info, sizeof (*pInfo)); - return CKR_OK; -} - -CK_RV -gck_mock_fail_C_GetSessionInfo (CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo) -{ - return CKR_GENERAL_ERROR; -} - -CK_RV -gck_mock_C_InitPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, - CK_ULONG ulPinLen) -{ - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - g_free (the_pin); - the_pin = g_strndup ((gchar*)pPin, ulPinLen); - n_the_pin = ulPinLen; - return CKR_OK; -} - -CK_RV -gck_mock_C_SetPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin, - CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen) -{ - Session *session; - gchar *old; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - old = g_strndup ((gchar*)pOldPin, ulOldLen); - if (!old || !g_str_equal (old, the_pin)) - return CKR_PIN_INCORRECT; - - g_free (the_pin); - the_pin = g_strndup ((gchar*)pNewPin, ulNewLen); - n_the_pin = ulNewLen; - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_GetOperationState (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState, - CK_ULONG_PTR pulOperationStateLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_SetOperationState (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState, - CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey, - CK_OBJECT_HANDLE hAuthenticationKey) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_Login (CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, - CK_UTF8CHAR_PTR pPin, CK_ULONG pPinLen) -{ - Session *session; - - g_return_val_if_fail (userType == CKU_SO || - userType == CKU_USER || - userType == CKU_CONTEXT_SPECIFIC, - CKR_USER_TYPE_INVALID); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (logged_in == FALSE, CKR_USER_ALREADY_LOGGED_IN); - - if (!pPin) - return CKR_PIN_INCORRECT; - - if (pPinLen != strlen (the_pin)) - return CKR_PIN_INCORRECT; - if (strncmp ((gchar*)pPin, the_pin, pPinLen) != 0) - return CKR_PIN_INCORRECT; - - if (userType == CKU_CONTEXT_SPECIFIC) { - g_return_val_if_fail (session->want_context_login == TRUE, CKR_OPERATION_NOT_INITIALIZED); - session->want_context_login = CK_FALSE; - } else { - logged_in = TRUE; - user_type = userType; - } - - return CKR_OK; -} - -CK_RV -gck_mock_C_Logout (CK_SESSION_HANDLE hSession) -{ - 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_assert (logged_in && "Not logged in"); - logged_in = FALSE; - user_type = 0; - return CKR_OK; -} - -CK_RV -gck_mock_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject) -{ - GckAttributes *attrs; - Session *session; - gboolean token, priv; - CK_OBJECT_CLASS klass; - CK_OBJECT_HANDLE object; - GckAttribute *attr; - CK_ULONG i; - - g_return_val_if_fail (phObject, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - attrs = gck_attributes_new (); - for (i = 0; i < ulCount; ++i) - gck_attributes_add_data (attrs, pTemplate[i].type, pTemplate[i].pValue, pTemplate[i].ulValueLen); - - if (gck_attributes_find_boolean (attrs, CKA_PRIVATE, &priv) && priv) { - if (!logged_in) { - gck_attributes_unref (attrs); - return CKR_USER_NOT_LOGGED_IN; - } - } - - /* In order to create a credential we must check CK_VALUE */ - if (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_G_CREDENTIAL) { - if (gck_attributes_find_ulong (attrs, CKA_G_OBJECT, &object)) { - attr = gck_attributes_find (attrs, CKA_VALUE); - if (!attr || attr->length != n_the_pin || - memcmp (attr->value, the_pin, attr->length) != 0) { - gck_attributes_unref (attrs); - return CKR_PIN_INCORRECT; - } - } - } - - *phObject = ++unique_identifier; - if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token) - g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phObject), attrs); - else - g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phObject), attrs); - - return CKR_OK; -} - -CK_RV -gck_mock_fail_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject) -{ - /* Always fails */ - return CKR_FUNCTION_FAILED; -} - -CK_RV -gck_mock_unsupported_C_CopyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phNewObject) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_DestroyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject) -{ - GckAttributes *attrs; - Session *session; - gboolean priv; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - attrs = lookup_object (session, hObject); - g_return_val_if_fail (attrs, CKR_OBJECT_HANDLE_INVALID); - - if (gck_attributes_find_boolean (attrs, CKA_PRIVATE, &priv) && priv) { - if (!logged_in) - return CKR_USER_NOT_LOGGED_IN; - } - - g_hash_table_remove (the_objects, GUINT_TO_POINTER (hObject)); - g_hash_table_remove (session->objects, GUINT_TO_POINTER (hObject)); - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_GetObjectSize (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, - CK_ULONG_PTR pulSize) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_GetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) -{ - CK_ATTRIBUTE_PTR result; - CK_RV ret = CKR_OK; - GckAttributes *attrs; - GckAttribute *attr; - Session *session; - CK_ULONG i; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - attrs = lookup_object (session, hObject); - if (!attrs) { - g_assert_not_reached (); /* "invalid object handle passed" */ - return CKR_OBJECT_HANDLE_INVALID; - } - - for (i = 0; i < ulCount; ++i) { - result = pTemplate + i; - attr = gck_attributes_find (attrs, result->type); - if (!attr) { - result->ulValueLen = (CK_ULONG)-1; - ret = CKR_ATTRIBUTE_TYPE_INVALID; - continue; - } - - if (!result->pValue) { - result->ulValueLen = attr->length; - continue; - } - - if (result->ulValueLen >= attr->length) { - memcpy (result->pValue, attr->value, attr->length); - continue; - } - - result->ulValueLen = (CK_ULONG)-1; - ret = CKR_BUFFER_TOO_SMALL; - } - - return ret; -} - -CK_RV -gck_mock_fail_C_GetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) -{ - return CKR_FUNCTION_FAILED; -} - -CK_RV -gck_mock_C_SetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) -{ - Session *session; - GckAttributes *attrs; - CK_ATTRIBUTE_PTR set; - GckAttribute *attr; - CK_ULONG i; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - attrs = lookup_object (session, hObject); - g_return_val_if_fail (attrs, CKR_OBJECT_HANDLE_INVALID); - - for (i = 0; i < ulCount; ++i) { - set = pTemplate + i; - attr = gck_attributes_find (attrs, set->type); - if (!attr) { - gck_attributes_add_data (attrs, set->type, set->pValue, set->ulValueLen); - } else { - gck_attribute_clear (attr); - gck_attribute_init (attr, set->type, set->pValue, set->ulValueLen); - } - } - - return CKR_OK; -} - -typedef struct _FindObjects { - CK_ATTRIBUTE_PTR template; - CK_ULONG count; - Session *session; -} FindObjects; - -static gboolean -enumerate_and_find_objects (CK_OBJECT_HANDLE object, GckAttributes *attrs, gpointer user_data) -{ - FindObjects *ctx = user_data; - CK_ATTRIBUTE_PTR match; - GckAttribute *attr; - CK_ULONG i; - - for (i = 0; i < ctx->count; ++i) { - match = ctx->template + i; - attr = gck_attributes_find (attrs, match->type); - if (!attr) - return TRUE; /* Continue */ - - if (attr->length != match->ulValueLen || - memcmp (attr->value, match->pValue, attr->length) != 0) - return TRUE; /* Continue */ - } - - ctx->session->matches = g_list_prepend (ctx->session->matches, GUINT_TO_POINTER (object)); - return TRUE; /* Continue */ -} - -CK_RV -gck_mock_C_FindObjectsInit (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount) -{ - Session *session; - FindObjects ctx; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - /* Starting an operation, cancels any previous one */ - if (session->operation != 0) - session->operation = 0; - - session->operation = OP_FIND; - - ctx.template = pTemplate; - ctx.count = ulCount; - ctx.session = session; - - gck_mock_module_enumerate_objects (hSession, enumerate_and_find_objects, &ctx); - return CKR_OK; -} - -CK_RV -gck_mock_fail_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, - CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount) -{ - /* Always fails */ - return CKR_FUNCTION_FAILED; -} - -CK_RV -gck_mock_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, - CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount) -{ - Session *session; - - g_return_val_if_fail (phObject, CKR_ARGUMENTS_BAD); - g_return_val_if_fail (pulObjectCount, CKR_ARGUMENTS_BAD); - g_return_val_if_fail (ulMaxObjectCount != 0, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (session->operation == OP_FIND, CKR_OPERATION_NOT_INITIALIZED); - - *pulObjectCount = 0; - while (ulMaxObjectCount > 0 && session->matches) { - *phObject = GPOINTER_TO_UINT (session->matches->data); - ++phObject; - --ulMaxObjectCount; - ++(*pulObjectCount); - session->matches = g_list_remove (session->matches, session->matches->data); - } - - return CKR_OK; -} - -CK_RV -gck_mock_C_FindObjectsFinal (CK_SESSION_HANDLE hSession) -{ - - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (session->operation == OP_FIND, CKR_OPERATION_NOT_INITIALIZED); - - session->operation = 0; - g_list_free (session->matches); - session->matches = NULL; - - return CKR_OK; -} - -CK_RV -gck_mock_C_EncryptInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey) -{ - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - /* Starting an operation, cancels any previous one */ - if (session->operation != 0) - session->operation = 0; - - g_assert (pMechanism); - g_assert (pMechanism->mechanism == CKM_MOCK_CAPITALIZE); - g_assert (hKey == PUBLIC_KEY_CAPITALIZE); - - session->operation = OP_CRYPTO; - session->crypto_method = CKA_ENCRYPT; - session->crypto_mechanism = CKM_MOCK_CAPITALIZE; - session->crypto_key = hKey; - return CKR_OK; -} - -CK_RV -gck_mock_C_Encrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, - CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen) -{ - Session *session; - CK_ULONG i; - - g_return_val_if_fail (pData, CKR_DATA_INVALID); - g_return_val_if_fail (pulEncryptedDataLen, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED); - g_return_val_if_fail (session->crypto_method == CKA_ENCRYPT, CKR_OPERATION_NOT_INITIALIZED); - - g_assert (session->crypto_mechanism == CKM_MOCK_CAPITALIZE); - g_assert (session->crypto_key == PUBLIC_KEY_CAPITALIZE); - - if (!pEncryptedData) { - *pulEncryptedDataLen = ulDataLen; - return CKR_OK; - } - - if (*pulEncryptedDataLen < ulDataLen) { - *pulEncryptedDataLen = ulDataLen; - return CKR_BUFFER_TOO_SMALL; - } - - for (i = 0; i < ulDataLen; ++i) - pEncryptedData[i] = g_ascii_toupper (pData[i]); - *pulEncryptedDataLen = ulDataLen; - - session->operation = 0; - session->crypto_method = 0; - session->crypto_mechanism = 0; - session->crypto_key = 0; - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_EncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, - CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, - CK_ULONG_PTR pulEncryptedPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_EncryptFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastEncryptedPart, - CK_ULONG_PTR pulLastEncryptedPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_DecryptInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey) -{ - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - /* Starting an operation, cancels any previous one */ - if (session->operation != 0) - session->operation = 0; - - g_assert (pMechanism); - g_assert (pMechanism->mechanism == CKM_MOCK_CAPITALIZE); - g_assert (hKey == PRIVATE_KEY_CAPITALIZE); - - session->operation = OP_CRYPTO; - session->crypto_method = CKA_DECRYPT; - session->crypto_mechanism = CKM_MOCK_CAPITALIZE; - session->crypto_key = hKey; - return CKR_OK; -} - -CK_RV -gck_mock_C_Decrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData, - CK_ULONG ulEncryptedDataLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) -{ - Session *session; - CK_ULONG i; - - g_return_val_if_fail (pEncryptedData, CKR_ENCRYPTED_DATA_INVALID); - g_return_val_if_fail (pulDataLen, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED); - g_return_val_if_fail (session->crypto_method == CKA_DECRYPT, CKR_OPERATION_NOT_INITIALIZED); - - g_assert (session->crypto_method == CKA_DECRYPT); - g_assert (session->crypto_mechanism == CKM_MOCK_CAPITALIZE); - g_assert (session->crypto_key == PRIVATE_KEY_CAPITALIZE); - - if (!pData) { - *pulDataLen = ulEncryptedDataLen; - return CKR_OK; - } - - if (*pulDataLen < ulEncryptedDataLen) { - *pulDataLen = ulEncryptedDataLen; - return CKR_BUFFER_TOO_SMALL; - } - - for (i = 0; i < ulEncryptedDataLen; ++i) - pData[i] = g_ascii_tolower (pEncryptedData[i]); - *pulDataLen = ulEncryptedDataLen; - - session->operation = 0; - session->crypto_method = 0; - session->crypto_mechanism = 0; - session->crypto_key = 0; - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_DecryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, - CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DecryptFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastPart, - CK_ULONG_PTR pulLastPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DigestInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_Digest (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, - CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DigestUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DigestKey (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DigestFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest, - CK_ULONG_PTR pulDigestLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_SignInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey) -{ - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - /* Starting an operation, cancels any previous one */ - if (session->operation != 0) - session->operation = 0; - - g_assert (pMechanism); - g_assert (pMechanism->mechanism == CKM_MOCK_PREFIX); - g_assert (hKey == PRIVATE_KEY_PREFIX); - - session->operation = OP_CRYPTO; - session->crypto_method = CKA_SIGN; - session->crypto_mechanism = CKM_MOCK_PREFIX; - session->crypto_key = hKey; - - if (pMechanism->pParameter) { - g_assert (pMechanism->ulParameterLen < sizeof (session->sign_prefix)); - memcpy (session->sign_prefix, pMechanism->pParameter, pMechanism->ulParameterLen); - session->n_sign_prefix = pMechanism->ulParameterLen; - } else { - g_assert (strlen (SIGNED_PREFIX) + 1 < sizeof (session->sign_prefix)); - strcpy ((gchar*)session->sign_prefix, SIGNED_PREFIX); - session->n_sign_prefix = strlen (SIGNED_PREFIX); - } - - /* The private key has CKA_ALWAYS_AUTHENTICATE above */ - session->want_context_login = CK_TRUE; - - return CKR_OK; -} - -CK_RV -gck_mock_C_Sign (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, - CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) -{ - Session *session; - CK_ULONG length; - - g_return_val_if_fail (pData, CKR_DATA_INVALID); - g_return_val_if_fail (pulSignatureLen, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED); - g_return_val_if_fail (session->crypto_method == CKA_SIGN, CKR_OPERATION_NOT_INITIALIZED); - - if (session->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - g_assert (session->crypto_method == CKA_SIGN); - g_assert (session->crypto_mechanism == CKM_MOCK_PREFIX); - g_assert (session->crypto_key == PRIVATE_KEY_PREFIX); - - length = session->n_sign_prefix + ulDataLen; - - if (!pSignature) { - *pulSignatureLen = length; - return CKR_OK; - } - - if (*pulSignatureLen < length) { - *pulSignatureLen = length; - return CKR_BUFFER_TOO_SMALL; - } - - memcpy (pSignature, session->sign_prefix, session->n_sign_prefix); - memcpy (pSignature + session->n_sign_prefix, pData, ulDataLen); - *pulSignatureLen = length; - - session->operation = 0; - session->crypto_method = 0; - session->crypto_mechanism = 0; - session->crypto_key = 0; - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_SignUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_SignFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, - CK_ULONG_PTR pulSignatureLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_SignRecoverInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_SignRecover (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, - CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_C_VerifyInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey) -{ - Session *session; - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - /* Starting an operation, cancels any previous one */ - if (session->operation != 0) - session->operation = 0; - - g_assert (pMechanism); - g_assert (pMechanism->mechanism == CKM_MOCK_PREFIX); - g_assert (hKey == PUBLIC_KEY_PREFIX); - - session->operation = OP_CRYPTO; - session->crypto_method = CKA_VERIFY; - session->crypto_mechanism = CKM_MOCK_PREFIX; - session->crypto_key = hKey; - - if (pMechanism->pParameter) { - g_assert (pMechanism->ulParameterLen < sizeof (session->sign_prefix)); - memcpy (session->sign_prefix, pMechanism->pParameter, pMechanism->ulParameterLen); - session->n_sign_prefix = pMechanism->ulParameterLen; - } else { - g_assert (strlen (SIGNED_PREFIX) + 1 < sizeof (session->sign_prefix)); - strcpy ((gchar*)session->sign_prefix, SIGNED_PREFIX); - session->n_sign_prefix = strlen (SIGNED_PREFIX); - } - - return CKR_OK; -} - -CK_RV -gck_mock_C_Verify (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, - CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen) -{ - Session *session; - CK_ULONG length; - - g_return_val_if_fail (pData, CKR_DATA_INVALID); - g_return_val_if_fail (pSignature, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED); - g_return_val_if_fail (session->crypto_method == CKA_VERIFY, CKR_OPERATION_NOT_INITIALIZED); - - g_assert (session->crypto_method == CKA_VERIFY); - g_assert (session->crypto_mechanism == CKM_MOCK_PREFIX); - g_assert (session->crypto_key == PUBLIC_KEY_PREFIX); - - length = session->n_sign_prefix + ulDataLen; - - if (ulSignatureLen < length) { - g_assert (FALSE); - return CKR_SIGNATURE_LEN_RANGE; - } - - if (memcmp (pSignature, session->sign_prefix, session->n_sign_prefix) == 0 && - memcmp (pSignature + session->n_sign_prefix, pData, ulDataLen) == 0) - return CKR_OK; - - return CKR_SIGNATURE_INVALID; -} - -CK_RV -gck_mock_unsupported_C_VerifyUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_VerifyFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, - CK_ULONG pulSignatureLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_VerifyRecoverInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_VerifyRecover (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, - CK_ULONG pulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DigestEncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, - CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, - CK_ULONG_PTR ulEncryptedPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DecryptDigestUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, - CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, - CK_ULONG_PTR pulPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_SignEncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, - CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, - CK_ULONG_PTR ulEncryptedPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_DecryptVerifyUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, - CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, - CK_ULONG_PTR pulPartLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_GenerateKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phKey) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_GenerateKeyPair (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount, - CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount, - CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey) -{ - GckAttributes *attrs; - Session *session; - gboolean token; - CK_ULONG i; - - g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID); - g_return_val_if_fail (pPublicKeyTemplate, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (ulPublicKeyAttributeCount, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (pPrivateKeyTemplate, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (ulPrivateKeyAttributeCount, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (phPublicKey, CKR_ARGUMENTS_BAD); - g_return_val_if_fail (phPrivateKey, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - if (pMechanism->mechanism != CKM_MOCK_GENERATE) - return CKR_MECHANISM_INVALID; - - if (!pMechanism->pParameter || pMechanism->ulParameterLen != 9 || - memcmp (pMechanism->pParameter, "generate", 9) != 0) - g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID); - - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_VALUE, "generated"); - for (i = 0; i < ulPublicKeyAttributeCount; ++i) - gck_attributes_add_data (attrs, pPublicKeyTemplate[i].type, - pPublicKeyTemplate[i].pValue, - pPublicKeyTemplate[i].ulValueLen); - *phPublicKey = ++unique_identifier; - if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token) - g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phPublicKey), attrs); - else - g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phPublicKey), attrs); - - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_VALUE, "generated"); - for (i = 0; i < ulPrivateKeyAttributeCount; ++i) - gck_attributes_add_data (attrs, pPrivateKeyTemplate[i].type, - pPrivateKeyTemplate[i].pValue, - pPrivateKeyTemplate[i].ulValueLen); - *phPrivateKey = ++unique_identifier; - if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token) - g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phPrivateKey), attrs); - else - g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phPrivateKey), attrs); - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_WrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey, - CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen) -{ - GckAttributes *attrs; - GckAttribute *attr; - Session *session; - - g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID); - g_return_val_if_fail (hWrappingKey, CKR_OBJECT_HANDLE_INVALID); - g_return_val_if_fail (hKey, CKR_OBJECT_HANDLE_INVALID); - g_return_val_if_fail (pulWrappedKeyLen, CKR_WRAPPED_KEY_LEN_RANGE); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - attrs = lookup_object (session, hWrappingKey); - g_return_val_if_fail (attrs, CKR_WRAPPING_KEY_HANDLE_INVALID); - - attrs = lookup_object (session, hKey); - g_return_val_if_fail (attrs, CKR_WRAPPED_KEY_INVALID); - - if (pMechanism->mechanism != CKM_MOCK_WRAP) - return CKR_MECHANISM_INVALID; - - if (pMechanism->pParameter) { - if (pMechanism->ulParameterLen != 4 || - memcmp (pMechanism->pParameter, "wrap", 4) != 0) - g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID); - } - - attr = gck_attributes_find (attrs, CKA_VALUE); - if (attr == NULL) - return CKR_WRAPPED_KEY_INVALID; - - if (!pWrappedKey) { - *pulWrappedKeyLen = attr->length; - return CKR_OK; - } - - if (*pulWrappedKeyLen < attr->length) { - *pulWrappedKeyLen = attr->length; - return CKR_BUFFER_TOO_SMALL; - } - - memcpy (pWrappedKey, attr->value, attr->length); - *pulWrappedKeyLen = attr->length; - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_UnwrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hUnwrappingKey, CK_BYTE_PTR pWrappedKey, - CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey) -{ - GckAttributes *attrs; - Session *session; - gboolean token; - CK_ULONG i; - - g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID); - g_return_val_if_fail (hUnwrappingKey, CKR_WRAPPING_KEY_HANDLE_INVALID); - g_return_val_if_fail (pWrappedKey, CKR_WRAPPED_KEY_INVALID); - g_return_val_if_fail (ulWrappedKeyLen, CKR_WRAPPED_KEY_LEN_RANGE); - g_return_val_if_fail (phKey, CKR_ARGUMENTS_BAD); - g_return_val_if_fail (pTemplate, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (ulCount, CKR_TEMPLATE_INCONSISTENT); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID); - - attrs = lookup_object (session, hUnwrappingKey); - g_return_val_if_fail (attrs, CKR_WRAPPING_KEY_HANDLE_INVALID); - - if (pMechanism->mechanism != CKM_MOCK_WRAP) - return CKR_MECHANISM_INVALID; - - if (pMechanism->pParameter) { - if (pMechanism->ulParameterLen != 4 || - memcmp (pMechanism->pParameter, "wrap", 4) != 0) - g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID); - } - - attrs = gck_attributes_new (); - gck_attributes_add_data (attrs, CKA_VALUE, pWrappedKey, ulWrappedKeyLen); - for (i = 0; i < ulCount; ++i) - gck_attributes_add_data (attrs, pTemplate[i].type, - pTemplate[i].pValue, - pTemplate[i].ulValueLen); - *phKey = ++unique_identifier; - if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token) - g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phKey), attrs); - else - g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phKey), attrs); - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_DeriveKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey) -{ - GckAttributes *attrs, *copy; - Session *session; - gboolean token; - CK_ULONG i; - - g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID); - g_return_val_if_fail (ulCount, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (pTemplate, CKR_TEMPLATE_INCOMPLETE); - g_return_val_if_fail (phKey, CKR_ARGUMENTS_BAD); - - session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession)); - g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID); - - attrs = lookup_object (session, hBaseKey); - g_return_val_if_fail (attrs, CKR_KEY_HANDLE_INVALID); - - if (pMechanism->mechanism != CKM_MOCK_DERIVE) - return CKR_MECHANISM_INVALID; - - if (pMechanism->pParameter) { - if (pMechanism->ulParameterLen != 6 || - memcmp (pMechanism->pParameter, "derive", 6) != 0) - g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID); - } - - copy = gck_attributes_new (); - gck_attributes_add_string (copy, CKA_VALUE, "derived"); - for (i = 0; i < ulCount; ++i) - gck_attributes_add_data (copy, pTemplate[i].type, - pTemplate[i].pValue, - pTemplate[i].ulValueLen); - for (i = 0; i < gck_attributes_count (attrs); ++i) - gck_attributes_add (copy, gck_attributes_at (attrs, i)); - *phKey = ++unique_identifier; - if (gck_attributes_find_boolean (copy, CKA_TOKEN, &token) && token) - g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phKey), copy); - else - g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phKey), copy); - - return CKR_OK; -} - -CK_RV -gck_mock_unsupported_C_SeedRandom (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -gck_mock_unsupported_C_GenerateRandom (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData, - CK_ULONG ulRandomLen) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -static CK_FUNCTION_LIST functionList = { - { 2, 11 }, /* version */ - gck_mock_validate_and_C_Initialize, - gck_mock_C_Finalize, - gck_mock_C_GetInfo, - gck_mock_C_GetFunctionList, - gck_mock_C_GetSlotList, - gck_mock_C_GetSlotInfo, - gck_mock_C_GetTokenInfo, - gck_mock_C_GetMechanismList, - gck_mock_C_GetMechanismInfo, - gck_mock_specific_args_C_InitToken, - gck_mock_C_InitPIN, - gck_mock_C_SetPIN, - gck_mock_C_OpenSession, - gck_mock_C_CloseSession, - gck_mock_C_CloseAllSessions, - gck_mock_C_GetSessionInfo, - gck_mock_unsupported_C_GetOperationState, - gck_mock_unsupported_C_SetOperationState, - gck_mock_C_Login, - gck_mock_C_Logout, - gck_mock_C_CreateObject, - gck_mock_unsupported_C_CopyObject, - gck_mock_C_DestroyObject, - gck_mock_unsupported_C_GetObjectSize, - gck_mock_C_GetAttributeValue, - gck_mock_C_SetAttributeValue, - gck_mock_C_FindObjectsInit, - gck_mock_C_FindObjects, - gck_mock_C_FindObjectsFinal, - gck_mock_C_EncryptInit, - gck_mock_C_Encrypt, - gck_mock_unsupported_C_EncryptUpdate, - gck_mock_unsupported_C_EncryptFinal, - gck_mock_C_DecryptInit, - gck_mock_C_Decrypt, - gck_mock_unsupported_C_DecryptUpdate, - gck_mock_unsupported_C_DecryptFinal, - gck_mock_unsupported_C_DigestInit, - gck_mock_unsupported_C_Digest, - gck_mock_unsupported_C_DigestUpdate, - gck_mock_unsupported_C_DigestKey, - gck_mock_unsupported_C_DigestFinal, - gck_mock_C_SignInit, - gck_mock_C_Sign, - gck_mock_unsupported_C_SignUpdate, - gck_mock_unsupported_C_SignFinal, - gck_mock_unsupported_C_SignRecoverInit, - gck_mock_unsupported_C_SignRecover, - gck_mock_C_VerifyInit, - gck_mock_C_Verify, - gck_mock_unsupported_C_VerifyUpdate, - gck_mock_unsupported_C_VerifyFinal, - gck_mock_unsupported_C_VerifyRecoverInit, - gck_mock_unsupported_C_VerifyRecover, - gck_mock_unsupported_C_DigestEncryptUpdate, - gck_mock_unsupported_C_DecryptDigestUpdate, - gck_mock_unsupported_C_SignEncryptUpdate, - gck_mock_unsupported_C_DecryptVerifyUpdate, - gck_mock_unsupported_C_GenerateKey, - gck_mock_unsupported_C_GenerateKeyPair, - gck_mock_unsupported_C_WrapKey, - gck_mock_unsupported_C_UnwrapKey, - gck_mock_unsupported_C_DeriveKey, - gck_mock_unsupported_C_SeedRandom, - gck_mock_unsupported_C_GenerateRandom, - gck_mock_C_GetFunctionStatus, - gck_mock_C_CancelFunction, - gck_mock_unsupported_C_WaitForSlotEvent -}; diff --git a/gck/gck-mock.h b/gck/gck-mock.h deleted file mode 100644 index 2b2c8e1d..00000000 --- a/gck/gck-mock.h +++ /dev/null @@ -1,420 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2010 Stefan Walter - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - */ - -#include <glib.h> - -#include "gck.h" - -#include "pkcs11/pkcs11.h" -#include "pkcs11/pkcs11i.h" - -#ifndef GCK_MOCK_H -#define GCK_MOCK_H - -CK_RV gck_mock_C_Initialize (CK_VOID_PTR pInitArgs); - -CK_RV gck_mock_validate_and_C_Initialize (CK_VOID_PTR pInitArgs); - -CK_RV gck_mock_C_Finalize (CK_VOID_PTR pReserved); - -CK_RV gck_mock_C_GetInfo (CK_INFO_PTR pInfo); - -CK_RV gck_mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list); - -CK_RV gck_mock_C_GetSlotList (CK_BBOOL tokenPresent, - CK_SLOT_ID_PTR pSlotList, - CK_ULONG_PTR pulCount); - -CK_RV gck_mock_C_GetSlotInfo (CK_SLOT_ID slotID, - CK_SLOT_INFO_PTR pInfo); - -CK_RV gck_mock_C_GetTokenInfo (CK_SLOT_ID slotID, - CK_TOKEN_INFO_PTR pInfo); - -CK_RV gck_mock_fail_C_GetTokenInfo (CK_SLOT_ID slotID, - CK_TOKEN_INFO_PTR pInfo); - -CK_RV gck_mock_C_GetMechanismList (CK_SLOT_ID slotID, - CK_MECHANISM_TYPE_PTR pMechanismList, - CK_ULONG_PTR pulCount); - -CK_RV gck_mock_C_GetMechanismInfo (CK_SLOT_ID slotID, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR pInfo); - -CK_RV gck_mock_specific_args_C_InitToken (CK_SLOT_ID slotID, - CK_UTF8CHAR_PTR pPin, - CK_ULONG ulPinLen, - CK_UTF8CHAR_PTR pLabel); - -CK_RV gck_mock_unsupported_C_WaitForSlotEvent (CK_FLAGS flags, - CK_SLOT_ID_PTR pSlot, - CK_VOID_PTR pReserved); - -CK_RV gck_mock_C_OpenSession (CK_SLOT_ID slotID, - CK_FLAGS flags, - CK_VOID_PTR pApplication, - CK_NOTIFY Notify, - CK_SESSION_HANDLE_PTR phSession); - -CK_RV gck_mock_fail_C_OpenSession (CK_SLOT_ID slotID, - CK_FLAGS flags, - CK_VOID_PTR pApplication, - CK_NOTIFY Notify, - CK_SESSION_HANDLE_PTR phSession); - -CK_RV gck_mock_C_CloseSession (CK_SESSION_HANDLE hSession); - -CK_RV gck_mock_C_CloseAllSessions (CK_SLOT_ID slotID); - -CK_RV gck_mock_C_GetFunctionStatus (CK_SESSION_HANDLE hSession); - -CK_RV gck_mock_C_CancelFunction (CK_SESSION_HANDLE hSession); - -CK_RV gck_mock_C_GetSessionInfo (CK_SESSION_HANDLE hSession, - CK_SESSION_INFO_PTR pInfo); - -CK_RV gck_mock_fail_C_GetSessionInfo (CK_SESSION_HANDLE hSession, - CK_SESSION_INFO_PTR pInfo); - -CK_RV gck_mock_C_InitPIN (CK_SESSION_HANDLE hSession, - CK_UTF8CHAR_PTR pPin, - CK_ULONG ulPinLen); - -CK_RV gck_mock_C_SetPIN (CK_SESSION_HANDLE hSession, - CK_UTF8CHAR_PTR pOldPin, - CK_ULONG ulOldLen, - CK_UTF8CHAR_PTR pNewPin, - CK_ULONG ulNewLen); - -CK_RV gck_mock_unsupported_C_GetOperationState (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pOperationState, - CK_ULONG_PTR pulOperationStateLen); - -CK_RV gck_mock_unsupported_C_SetOperationState (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pOperationState, - CK_ULONG ulOperationStateLen, - CK_OBJECT_HANDLE hEncryptionKey, - CK_OBJECT_HANDLE hAuthenticationKey); - -CK_RV gck_mock_C_Login (CK_SESSION_HANDLE hSession, - CK_USER_TYPE userType, - CK_UTF8CHAR_PTR pPin, - CK_ULONG pPinLen); - -CK_RV gck_mock_C_Logout (CK_SESSION_HANDLE hSession); - -CK_RV gck_mock_C_CreateObject (CK_SESSION_HANDLE hSession, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phObject); - -CK_RV gck_mock_fail_C_CreateObject (CK_SESSION_HANDLE hSession, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phObject); - -CK_RV gck_mock_unsupported_C_CopyObject (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phNewObject); - -CK_RV gck_mock_C_DestroyObject (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hObject); - -CK_RV gck_mock_unsupported_C_GetObjectSize (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hObject, - CK_ULONG_PTR pulSize); - -CK_RV gck_mock_C_GetAttributeValue (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount); - -CK_RV gck_mock_fail_C_GetAttributeValue (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount); - -CK_RV gck_mock_C_SetAttributeValue (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hObject, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount); - -CK_RV gck_mock_C_FindObjectsInit (CK_SESSION_HANDLE hSession, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount); - -CK_RV gck_mock_C_FindObjects (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE_PTR phObject, - CK_ULONG ulMaxObjectCount, - CK_ULONG_PTR pulObjectCount); - -CK_RV gck_mock_fail_C_FindObjects (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE_PTR phObject, - CK_ULONG ulMaxObjectCount, - CK_ULONG_PTR pulObjectCount); - -CK_RV gck_mock_C_FindObjectsFinal (CK_SESSION_HANDLE hSession); - -CK_RV gck_mock_C_EncryptInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_C_Encrypt (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pData, - CK_ULONG ulDataLen, - CK_BYTE_PTR pEncryptedData, - CK_ULONG_PTR pulEncryptedDataLen); - -CK_RV gck_mock_unsupported_C_EncryptUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pPart, - CK_ULONG ulPartLen, - CK_BYTE_PTR pEncryptedPart, - CK_ULONG_PTR pulEncryptedPartLen); - -CK_RV gck_mock_unsupported_C_EncryptFinal (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pLastEncryptedPart, - CK_ULONG_PTR pulLastEncryptedPartLen); - -CK_RV gck_mock_C_DecryptInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_C_Decrypt (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pEncryptedData, - CK_ULONG ulEncryptedDataLen, - CK_BYTE_PTR pData, - CK_ULONG_PTR pulDataLen); - -CK_RV gck_mock_unsupported_C_DecryptUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pEncryptedPart, - CK_ULONG ulEncryptedPartLen, - CK_BYTE_PTR pPart, - CK_ULONG_PTR pulPartLen); - -CK_RV gck_mock_unsupported_C_DecryptFinal (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pLastPart, - CK_ULONG_PTR pulLastPartLen); - -CK_RV gck_mock_unsupported_C_DigestInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism); - -CK_RV gck_mock_unsupported_C_Digest (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pData, - CK_ULONG ulDataLen, - CK_BYTE_PTR pDigest, - CK_ULONG_PTR pulDigestLen); - -CK_RV gck_mock_unsupported_C_DigestUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pPart, - CK_ULONG ulPartLen); - -CK_RV gck_mock_unsupported_C_DigestKey (CK_SESSION_HANDLE hSession, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_unsupported_C_DigestFinal (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pDigest, - CK_ULONG_PTR pulDigestLen); - -CK_RV gck_mock_C_SignInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_C_Sign (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pData, - CK_ULONG ulDataLen, - CK_BYTE_PTR pSignature, - CK_ULONG_PTR pulSignatureLen); - -CK_RV gck_mock_unsupported_C_SignUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pPart, - CK_ULONG ulPartLen); - -CK_RV gck_mock_unsupported_C_SignFinal (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pSignature, - CK_ULONG_PTR pulSignatureLen); - -CK_RV gck_mock_unsupported_C_SignRecoverInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_unsupported_C_SignRecover (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pData, - CK_ULONG ulDataLen, - CK_BYTE_PTR pSignature, - CK_ULONG_PTR pulSignatureLen); - -CK_RV gck_mock_C_VerifyInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_C_Verify (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pData, - CK_ULONG ulDataLen, - CK_BYTE_PTR pSignature, - CK_ULONG ulSignatureLen); - -CK_RV gck_mock_unsupported_C_VerifyUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pPart, - CK_ULONG ulPartLen); - -CK_RV gck_mock_unsupported_C_VerifyFinal (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pSignature, - CK_ULONG pulSignatureLen); - -CK_RV gck_mock_unsupported_C_VerifyRecoverInit (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hKey); - -CK_RV gck_mock_unsupported_C_VerifyRecover (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pSignature, - CK_ULONG pulSignatureLen, - CK_BYTE_PTR pData, - CK_ULONG_PTR pulDataLen); - -CK_RV gck_mock_unsupported_C_DigestEncryptUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pPart, - CK_ULONG ulPartLen, - CK_BYTE_PTR pEncryptedPart, - CK_ULONG_PTR ulEncryptedPartLen); - -CK_RV gck_mock_unsupported_C_DecryptDigestUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pEncryptedPart, - CK_ULONG ulEncryptedPartLen, - CK_BYTE_PTR pPart, - CK_ULONG_PTR pulPartLen); - -CK_RV gck_mock_unsupported_C_SignEncryptUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pPart, - CK_ULONG ulPartLen, - CK_BYTE_PTR pEncryptedPart, - CK_ULONG_PTR ulEncryptedPartLen); - -CK_RV gck_mock_unsupported_C_DecryptVerifyUpdate (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pEncryptedPart, - CK_ULONG ulEncryptedPartLen, - CK_BYTE_PTR pPart, - CK_ULONG_PTR pulPartLen); - -CK_RV gck_mock_unsupported_C_GenerateKey (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phKey); - -CK_RV gck_mock_unsupported_C_GenerateKeyPair (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_ATTRIBUTE_PTR pPublicKeyTemplate, - CK_ULONG ulPublicKeyAttributeCount, - CK_ATTRIBUTE_PTR pPrivateKeyTemplate, - CK_ULONG ulPrivateKeyAttributeCount, - CK_OBJECT_HANDLE_PTR phPublicKey, - CK_OBJECT_HANDLE_PTR phPrivateKey); - -CK_RV gck_mock_unsupported_C_WrapKey (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hWrappingKey, - CK_OBJECT_HANDLE hKey, - CK_BYTE_PTR pWrappedKey, - CK_ULONG_PTR pulWrappedKeyLen); - -CK_RV gck_mock_unsupported_C_UnwrapKey (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE pUnwrappingKey, - CK_BYTE_PTR pWrappedKey, - CK_ULONG pulWrappedKeyLen, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phKey); - -CK_RV gck_mock_unsupported_C_DeriveKey (CK_SESSION_HANDLE hSession, - CK_MECHANISM_PTR pMechanism, - CK_OBJECT_HANDLE hBaseKey, - CK_ATTRIBUTE_PTR pTemplate, - CK_ULONG ulCount, - CK_OBJECT_HANDLE_PTR phKey); - -CK_RV gck_mock_unsupported_C_SeedRandom (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pSeed, - CK_ULONG ulSeedLen); - -CK_RV gck_mock_unsupported_C_GenerateRandom (CK_SESSION_HANDLE hSession, - CK_BYTE_PTR pRandomData, - CK_ULONG ulRandomLen); - -CK_OBJECT_HANDLE gck_mock_module_find_object (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR attrs, - CK_ULONG n_attrs); - -guint gck_mock_module_count_objects (CK_SESSION_HANDLE session); - -typedef gboolean (*GckMockEnumerator) (CK_OBJECT_HANDLE handle, - GckAttributes *attrs, - gpointer user_data); - -void gck_mock_module_enumerate_objects (CK_SESSION_HANDLE session, - GckMockEnumerator func, - gpointer user_data); - -CK_OBJECT_HANDLE gck_mock_module_take_object (GckAttributes *attrs); - -void gck_mock_module_set_object (CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR attrs, - CK_ULONG n_attrs); - -void gck_mock_module_set_pin (const gchar *password); - -/* - * Some dumb crypto mechanisms for simple testing. - * - * CKM_T_CAPITALIZE (encrypt/decrypt) - * capitalizes to encrypt - * lowercase to decrypt - * - * CKM_T_PREFIX (sign/verify) - * sign prefixes data with key label - * verify unprefixes data with key label. - * - * CKM_T_GENERATE (generate-pair) - * generates a pair of keys, mechanism param should be 'generate' - * - * CKM_T_WRAP (wrap key) - * wraps key by returning value, mechanism param should be 'wrap' - * - * CKM_T_DERIVE (derive-key) - * derives key by setting value to 'derived'. - * mechanism param should be 'derive' - */ - -#define CKM_MOCK_CAPITALIZE (CKM_VENDOR_DEFINED | 1) -#define CKM_MOCK_PREFIX (CKM_VENDOR_DEFINED | 2) -#define CKM_MOCK_GENERATE (CKM_VENDOR_DEFINED | 3) -#define CKM_MOCK_WRAP (CKM_VENDOR_DEFINED | 4) -#define CKM_MOCK_DERIVE (CKM_VENDOR_DEFINED | 5) - -#define GCK_MOCK_SLOT_ONE_ID 52 -#define GCK_MOCK_SLOT_TWO_ID 134 - -#define GCK_MOCK_SLOT_ONE_URI "pkcs11:manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL" - -#endif /* GCK_MOCK_H */ diff --git a/gck/gck-module.c b/gck/gck-module.c deleted file mode 100644 index cdd9d451..00000000 --- a/gck/gck-module.c +++ /dev/null @@ -1,748 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-module.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" -#include "gck-marshal.h" - -#include <glib/gi18n.h> - -#include <p11-kit/p11-kit.h> - -#include <string.h> - -/** - * SECTION:gck-module - * @title: GckModule - * @short_description: A loaded and initialized PKCS\#11 module. - * - * A GckModule object holds a loaded PKCS\#11 module. A PKCS\#11 module is a shared library. - * - * You can load and initialize a PKCS\#11 module with the gck_module_initialize() call. If you already - * have a loaded and initialized module that you'd like to use with the various gck functions, then - * you can use gck_module_new(). - */ - -/** - * GckModule: - * @parent: derived from this. - * - * Holds a loaded and initialized PKCS\#11 module. - */ - -/** - * GckModuleInfo: - * @pkcs11_version_major: The major version of the module. - * @pkcs11_version_minor: The minor version of the module. - * @manufacturer_id: The module manufacturer. - * @flags: The module PKCS#11 flags. - * @library_description: The module description. - * @library_version_major: The major version of the library. - * @library_version_minor: The minor version of the library. - * - * Holds information about the PKCS#11 module. - * - * This structure corresponds to CK_MODULE_INFO in the PKCS\#11 standard. The - * strings are NULL terminated for easier use. - * - * Use gck_module_info_free() to release this structure when done with it. - */ - -/* - * MT safe - * - * The only thing that can change after object initialization in - * a GckModule is the finalized flag, which can be set - * to 1 in dispose. - */ - -enum { - PROP_0, - PROP_PATH, - PROP_FUNCTIONS -}; - -enum { - AUTHENTICATE_SLOT, - AUTHENTICATE_OBJECT, - LAST_SIGNAL -}; - -struct _GckModulePrivate { - GModule *module; - gchar *path; - gboolean initialized; - CK_FUNCTION_LIST_PTR funcs; - CK_C_INITIALIZE_ARGS init_args; - - /* Modified atomically */ - gint finalized; -}; - -G_DEFINE_TYPE (GckModule, gck_module, G_TYPE_OBJECT); - -static guint signals[LAST_SIGNAL] = { 0 }; - -/* ---------------------------------------------------------------------------- - * OBJECT - */ - -static gboolean -gck_module_real_authenticate_slot (GckModule *module, GckSlot *self, gchar *label, gchar **password) -{ - return FALSE; -} - -static gboolean -gck_module_real_authenticate_object (GckModule *module, GckObject *object, gchar *label, gchar **password) -{ - return FALSE; -} - -static void -gck_module_init (GckModule *self) -{ - self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_MODULE, GckModulePrivate); -} - -static void -gck_module_get_property (GObject *obj, guint prop_id, GValue *value, - GParamSpec *pspec) -{ - GckModule *self = GCK_MODULE (obj); - - switch (prop_id) { - case PROP_PATH: - g_value_set_string (value, gck_module_get_path (self)); - break; - case PROP_FUNCTIONS: - g_value_set_pointer (value, gck_module_get_functions (self)); - break; - } -} - -static void -gck_module_set_property (GObject *obj, guint prop_id, const GValue *value, - GParamSpec *pspec) -{ - GckModule *self = GCK_MODULE (obj); - - /* Only allowed during initialization */ - switch (prop_id) { - case PROP_PATH: - g_return_if_fail (!self->pv->path); - self->pv->path = g_value_dup_string (value); - break; - case PROP_FUNCTIONS: - g_return_if_fail (!self->pv->funcs); - self->pv->funcs = g_value_get_pointer (value); - break; - } -} - -static void -gck_module_dispose (GObject *obj) -{ - GckModule *self = GCK_MODULE (obj); - gboolean finalize = FALSE; - CK_RV rv; - - if (self->pv->initialized && self->pv->funcs) { - if (g_atomic_int_compare_and_exchange (&self->pv->finalized, 0, 1)) - finalize = TRUE; - } - - /* Must be careful when accessing funcs */ - if (finalize) { - rv = p11_kit_finalize_module (self->pv->funcs); - if (rv != CKR_OK) { - g_warning ("C_Finalize on module '%s' failed: %s", - self->pv->path, gck_message_from_rv (rv)); - } - } - - G_OBJECT_CLASS (gck_module_parent_class)->dispose (obj); -} - -static void -gck_module_finalize (GObject *obj) -{ - GckModule *self = GCK_MODULE (obj); - - self->pv->funcs = NULL; - - if (self->pv->module) { - if (!g_module_close (self->pv->module)) - g_warning ("failed to close the pkcs11 module: %s", - g_module_error ()); - self->pv->module = NULL; - } - - g_free (self->pv->path); - self->pv->path = NULL; - - G_OBJECT_CLASS (gck_module_parent_class)->finalize (obj); -} - - -static void -gck_module_class_init (GckModuleClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - gck_module_parent_class = g_type_class_peek_parent (klass); - - gobject_class->get_property = gck_module_get_property; - gobject_class->set_property = gck_module_set_property; - gobject_class->dispose = gck_module_dispose; - gobject_class->finalize = gck_module_finalize; - - klass->authenticate_object = gck_module_real_authenticate_object; - klass->authenticate_slot = gck_module_real_authenticate_slot; - - /** - * GckModule:path: - * - * The PKCS#11 module file path. - * - * This may be set to NULL if this object was created from an already - * initialized module via the gck_module_new() function. - */ - g_object_class_install_property (gobject_class, PROP_PATH, - g_param_spec_string ("path", "Module Path", "Path to the PKCS11 Module", - NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckModule:functions: - * - * The raw PKCS#11 function list for the module. - * - * This points to a CK_FUNCTION_LIST structure. - */ - g_object_class_install_property (gobject_class, PROP_FUNCTIONS, - g_param_spec_pointer ("functions", "Function List", "PKCS11 Function List", - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckModule::authenticate-slot: - * @module: The module - * @slot: The slot to be authenticated. - * @string: A displayable label which describes the object. - * @password: A gchar** where a password should be returned. - * - * Use gck_slot_set_interaction() instead of connecting to this signal. - * - * Deprecated: Since 3.4 - */ - signals[AUTHENTICATE_SLOT] = g_signal_new ("authenticate-slot", GCK_TYPE_MODULE, - G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GckModuleClass, authenticate_slot), - g_signal_accumulator_true_handled, NULL, _gck_marshal_BOOLEAN__OBJECT_STRING_POINTER, - G_TYPE_BOOLEAN, 3, GCK_TYPE_SLOT, G_TYPE_STRING, G_TYPE_POINTER); - - /** - * GckModule::authenticate-object: - * @module: The module. - * @object: The object to be authenticated. - * @label: A displayable label which describes the object. - * @password: A gchar** where a password should be returned. - * - * Use gck_slot_set_interaction() instead of connecting to this signal. - * - * Deprecated: Since 3.4 - */ - signals[AUTHENTICATE_OBJECT] = g_signal_new ("authenticate-object", GCK_TYPE_MODULE, - G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GckModuleClass, authenticate_object), - g_signal_accumulator_true_handled, NULL, _gck_marshal_BOOLEAN__OBJECT_STRING_POINTER, - G_TYPE_BOOLEAN, 3, GCK_TYPE_OBJECT, G_TYPE_STRING, G_TYPE_POINTER); - - g_type_class_add_private (gobject_class, sizeof (GckModulePrivate)); -} - -GType -gck_module_info_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckModuleInfo", - (GBoxedCopyFunc)gck_module_info_copy, - (GBoxedFreeFunc)gck_module_info_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_module_info_copy: - * @module_info: a module info - * - * Make a copy of the module info. - * - * Returns: (transfer full): a newly allocated copy module info - */ -GckModuleInfo * -gck_module_info_copy (GckModuleInfo *module_info) -{ - if (module_info == NULL) - return NULL; - - module_info = g_memdup (module_info, sizeof (GckModuleInfo)); - module_info->manufacturer_id = g_strdup (module_info->manufacturer_id); - module_info->library_description = g_strdup (module_info->library_description); - return module_info; - -} - -/** - * gck_module_info_free: - * @module_info: The module info to free, or NULL. - * - * Free a GckModuleInfo structure. - **/ -void -gck_module_info_free (GckModuleInfo *module_info) -{ - if (!module_info) - return; - g_free (module_info->library_description); - g_free (module_info->manufacturer_id); - g_free (module_info); -} - -typedef struct { - GckArguments base; - gchar *path; - GckModule *result; - GError *error; -} Initialize; - -static CK_RV -perform_initialize (Initialize *args) -{ - CK_C_GetFunctionList get_function_list; - CK_FUNCTION_LIST_PTR funcs; - GModule *module; - GckModule *result; - CK_RV rv; - - /* Load the actual module */ - module = g_module_open (args->path, 0); - if (!module) { - g_set_error (&args->error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM, - _("Error loading PKCS#11 module: %s"), g_module_error ()); - return CKR_GCK_MODULE_PROBLEM; - } - - /* Get the entry point */ - if (!g_module_symbol (module, "C_GetFunctionList", (void**)&get_function_list)) { - g_set_error (&args->error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM, - _("Invalid PKCS#11 module: %s"), g_module_error ()); - g_module_close (module); - return CKR_GCK_MODULE_PROBLEM; - } - - /* Get the function list */ - rv = (get_function_list) (&funcs); - if (rv != CKR_OK) { - g_set_error (&args->error, GCK_ERROR, rv, - _("Couldn't setup PKCS#11 module: %s"), - gck_message_from_rv (rv)); - g_module_close (module); - return rv; - } - - result = g_object_new (GCK_TYPE_MODULE, - "functions", funcs, - "path", args->path, - NULL); - result->pv->module = module; - - /* Now initialize the module */ - rv = p11_kit_initialize_module (funcs); - if (rv != CKR_OK) { - g_set_error (&args->error, GCK_ERROR, rv, - _("Couldn't initialize PKCS#11 module: %s"), - gck_message_from_rv (rv)); - g_object_unref (result); - return rv; - } - - result->pv->initialized = TRUE; - args->result = result; - return CKR_OK; -} - -static void -free_initialize (Initialize *args) -{ - g_free (args->path); - g_clear_error (&args->error); - g_clear_object (&args->result); - g_free (args); -} - -/** - * gck_module_initialize: - * @path: The file system path to the PKCS\#11 module to load. - * @cancellable: (allow-none): optional cancellation object - * @error: A location to store an error resulting from a failed load. - * - * Load and initialize a PKCS\#11 module represented by a GckModule object. - * - * Return value: (transfer full): The loaded PKCS\#11 module or %NULL if failed. - **/ -GckModule* -gck_module_initialize (const gchar *path, - GCancellable *cancellable, - GError **error) -{ - Initialize args = { GCK_ARGUMENTS_INIT, 0, }; - - g_return_val_if_fail (path != NULL, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - args.path = g_strdup (path); - - if (!_gck_call_sync (NULL, perform_initialize, NULL, &args, cancellable, error)) { - - /* A custom error from perform_initialize */ - if (args.error) { - g_clear_error (error); - g_propagate_error (error, args.error); - args.error = NULL; - } - } - - g_free (args.path); - g_clear_error (&args.error); - return args.result; -} - -/** - * gck_module_initialize_async: - * @path: the file system path to the PKCS\#11 module to load - * @cancellable: (allow-none): optional cancellation object - * @callback: a callback which will be called when the operation completes - * @user_data: data to pass to the callback - * - * Asynchronously load and initialize a PKCS\#11 module represented by a - * #GckModule object. - **/ -void -gck_module_initialize_async (const gchar *path, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) -{ - Initialize *args; - - g_return_if_fail (path != NULL); - - args = _gck_call_async_prep (NULL, NULL, perform_initialize, NULL, - sizeof (*args), free_initialize); - args->path = g_strdup (path); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_module_initialize_finish: - * @result: the asynchronous result - * @error: location to place an error on failure - * - * Finishes the asynchronous initialize operation. - * - * Returns: (transfer full) (allow-none): The initialized module, or NULL - */ -GckModule * -gck_module_initialize_finish (GAsyncResult *result, - GError **error) -{ - GckModule *module = NULL; - Initialize *args; - - args = _gck_call_arguments (result, Initialize); - if (_gck_call_basic_finish (result, error)) { - module = args->result; - args->result = NULL; - - } else { - /* A custom error from perform_initialize */ - if (args->error) { - g_clear_error (error); - g_propagate_error (error, args->error); - args->error = NULL; - } - } - - return module; -} - -/** - * gck_module_new: (skip) - * @funcs: Initialized PKCS\#11 function list pointer - * - * Create a GckModule representing a PKCS\#11 module. It is assumed that - * this the module is already initialized. In addition it will not be - * finalized when complete. - * - * Return value: The new PKCS\#11 module. - **/ -GckModule* -gck_module_new (CK_FUNCTION_LIST_PTR funcs) -{ - g_return_val_if_fail (funcs != NULL, NULL); - return g_object_new (GCK_TYPE_MODULE, "functions", funcs, NULL); -} - -GckModule* -_gck_module_new_initialized (CK_FUNCTION_LIST_PTR funcs) -{ - GckModule *module = gck_module_new (funcs); - module->pv->initialized = TRUE; /* As if we initialized it */ - return module; -} - -/** - * gck_module_equal: - * @module1: (type Gck.Module): a pointer to the first #GckModule - * @module2: (type Gck.Module): a pointer to the second #GckModule - * - * Checks equality of two modules. Two GckModule objects can point to the same - * underlying PKCS\#11 module. - * - * Return value: TRUE if module1 and module2 are equal. FALSE if either is not a GckModule. - **/ -gboolean -gck_module_equal (gconstpointer module1, gconstpointer module2) -{ - GckModule *mod1, *mod2; - - if (module1 == module2) - return TRUE; - if (!GCK_IS_MODULE (module1) || !GCK_IS_MODULE (module2)) - return FALSE; - - mod1 = GCK_MODULE (module1); - mod2 = GCK_MODULE (module2); - - return mod1->pv->funcs == mod2->pv->funcs; -} - -/** - * gck_module_hash: - * @module: (type Gck.Module): a pointer to a #GckModule - * - * Create a hash value for the GckModule. - * - * This function is intended for easily hashing a GckModule to add to - * a GHashTable or similar data structure. - * - * Return value: An integer that can be used as a hash value, or 0 if invalid. - **/ -guint -gck_module_hash (gconstpointer module) -{ - GckModule *self; - - g_return_val_if_fail (GCK_IS_MODULE (module), 0); - self = GCK_MODULE (module); - return g_direct_hash (self->pv->funcs); -} - -GckModuleInfo* -_gck_module_info_from_pkcs11 (CK_INFO_PTR info) -{ - GckModuleInfo *modinfo; - - modinfo = g_new0 (GckModuleInfo, 1); - modinfo->flags = info->flags; - modinfo->library_description = gck_string_from_chars (info->libraryDescription, - sizeof (info->libraryDescription)); - modinfo->manufacturer_id = gck_string_from_chars (info->manufacturerID, - sizeof (info->manufacturerID)); - modinfo->library_version_major = info->libraryVersion.major; - modinfo->library_version_minor = info->libraryVersion.minor; - modinfo->pkcs11_version_major = info->cryptokiVersion.major; - modinfo->pkcs11_version_minor = info->cryptokiVersion.minor; - - return modinfo; -} - -void -_gck_module_info_to_pkcs11 (GckModuleInfo* module_info, CK_INFO_PTR info) -{ - info->flags = module_info->flags; - if (!gck_string_to_chars (info->libraryDescription, - sizeof (info->libraryDescription), - module_info->library_description)) - g_return_if_reached (); - if (!gck_string_to_chars (info->manufacturerID, - sizeof (info->manufacturerID), - module_info->manufacturer_id)) - g_return_if_reached (); - - info->libraryVersion.major = module_info->library_version_major; - info->libraryVersion.minor = module_info->library_version_minor; - info->cryptokiVersion.major = module_info->pkcs11_version_major; - info->cryptokiVersion.minor = module_info->pkcs11_version_minor; -} - -/** - * gck_module_get_info: - * @self: The module to get info for. - * - * Get the info about a PKCS\#11 module. - * - * Returns: (transfer full): the module info; release this with gck_module_info_free() - **/ -GckModuleInfo* -gck_module_get_info (GckModule *self) -{ - CK_INFO info; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_MODULE (self), NULL); - g_return_val_if_fail (self->pv->funcs, NULL); - - memset (&info, 0, sizeof (info)); - rv = (self->pv->funcs->C_GetInfo (&info)); - if (rv != CKR_OK) { - g_warning ("couldn't get module info: %s", gck_message_from_rv (rv)); - return NULL; - } - - return _gck_module_info_from_pkcs11 (&info); -} - -/** - * gck_module_get_slots: - * @self: The module for which to get the slots. - * @token_present: Whether to limit only to slots with a token present. - * - * Get the GckSlot objects for a given module. - * - * Return value: (element-type Gck.Slot) (transfer full): The possibly empty - * list of slots. Release this with gck_list_unref_free(). - */ -GList* -gck_module_get_slots (GckModule *self, gboolean token_present) -{ - CK_SLOT_ID_PTR slot_list; - CK_ULONG count, i; - GList *result; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_MODULE (self), NULL); - g_return_val_if_fail (self->pv->funcs, NULL); - - rv = (self->pv->funcs->C_GetSlotList) (token_present ? CK_TRUE : CK_FALSE, NULL, &count); - if (rv != CKR_OK) { - g_warning ("couldn't get slot count: %s", gck_message_from_rv (rv)); - return NULL; - } - - if (!count) - return NULL; - - slot_list = g_new (CK_SLOT_ID, count); - rv = (self->pv->funcs->C_GetSlotList) (token_present ? CK_TRUE : CK_FALSE, slot_list, &count); - if (rv != CKR_OK) { - g_warning ("couldn't get slot list: %s", gck_message_from_rv (rv)); - g_free (slot_list); - return NULL; - } - - result = NULL; - for (i = 0; i < count; ++i) { - result = g_list_prepend (result, g_object_new (GCK_TYPE_SLOT, - "handle", slot_list[i], - "module", self, NULL)); - } - - g_free (slot_list); - return g_list_reverse (result); -} - -/** - * gck_module_get_path: - * @self: The module for which to get the path. - * - * Get the file path of this module. This may not be an absolute path, and - * usually reflects the path passed to gck_module_initialize(). - * - * Return value: The path, do not modify or free this value. - **/ -const gchar* -gck_module_get_path (GckModule *self) -{ - g_return_val_if_fail (GCK_IS_MODULE (self), NULL); - return self->pv->path; -} - -/** - * gck_module_get_functions: (skip) - * @self: The module for which to get the function list. - * - * Get the PKCS\#11 function list for the module. - * - * Return value: The function list, do not modify this structure. - **/ -CK_FUNCTION_LIST_PTR -gck_module_get_functions (GckModule *self) -{ - g_return_val_if_fail (GCK_IS_MODULE (self), NULL); - return self->pv->funcs; -} - -/** - * gck_module_match: - * @self: the module to match - * @uri: the uri to match against the module - * - * Check whether the PKCS\#11 URI matches the module - * - * Returns: whether the URI matches or not - */ -gboolean -gck_module_match (GckModule *self, - GckUriData *uri) -{ - gboolean match = TRUE; - GckModuleInfo *info; - - g_return_val_if_fail (GCK_IS_MODULE (self), FALSE); - g_return_val_if_fail (uri != NULL, FALSE); - - if (uri->any_unrecognized) - match = FALSE; - - if (match && uri->module_info) { - info = gck_module_get_info (self); - match = _gck_module_info_match (uri->module_info, info); - gck_module_info_free (info); - } - - return match; -} diff --git a/gck/gck-modules.c b/gck/gck-modules.c deleted file mode 100644 index 9a931038..00000000 --- a/gck/gck-modules.c +++ /dev/null @@ -1,433 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-modules.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2010, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" -#include "gck-marshal.h" - -#include <glib/gi18n.h> - -#define P11_KIT_FUTURE_UNSTABLE_API 1 -#include <p11-kit/p11-kit.h> - -#include <string.h> -#include <stdlib.h> - -/** - * SECTION:gck-modules - * @title: GckModule lists - * @short_description: Dealing with lists of PKCS\#11 modules. - * - * These functions are useful for dealing with lists of modules, and performing - * operations on all of them. - */ - -typedef struct { - GckArguments base; - GList *results; - GError *error; -} InitializeRegistered; - -static CK_RV -perform_initialize_registered (InitializeRegistered *args) -{ - GckModule *module; - CK_FUNCTION_LIST_PTR *modules, *funcs; - const gchar *message; - CK_RV rv; - - rv = p11_kit_initialize_registered (); - if (rv != CKR_OK) { - message = p11_kit_message (); - if (message == NULL) - message = gck_message_from_rv (rv); - g_set_error (&args->error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM, - _("Couldn't initialize registered PKCS#11 modules: %s"), message); - return rv; - } - - modules = p11_kit_registered_modules (); - for (funcs = modules; *funcs; ++funcs) { - module = _gck_module_new_initialized (*funcs); - args->results = g_list_prepend (args->results, module); - } - - free (modules); - return CKR_OK; -} - -static void -free_initialize_registered (InitializeRegistered *args) -{ - g_clear_error (&args->error); - gck_list_unref_free (args->results); - g_free (args); -} - -/** - * gck_modules_initialize_registered: - * @cancellable: (allow-none): optional cancellation object - * @error: (allow-none): location to place an error on failure - * - * Load and initialize all the registered modules. - * - * Returns: (transfer full) (element-type Gck.Module): A newly allocated list - * of #GckModule objects, which should be released with gck_list_unref_free(). - */ -GList* -gck_modules_initialize_registered (GCancellable *cancellable, - GError **error) -{ - InitializeRegistered args = { GCK_ARGUMENTS_INIT, 0, }; - - if (!_gck_call_sync (NULL, perform_initialize_registered, NULL, &args, cancellable, error)) { - if (args.error) { - g_clear_error (error); - g_propagate_error (error, args.error); - args.error = NULL; - } - } - - g_clear_error (&args.error); - return args.results; -} - -/** - * gck_modules_initialize_registered_async: - * @cancellable: optional cancellation object - * @callback: a callback which will be called when the operation completes - * @user_data: data to pass to the callback - * - * Load and initialize all the registered modules asynchronously. - */ -void -gck_modules_initialize_registered_async (GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) -{ - InitializeRegistered *args; - - args = _gck_call_async_prep (NULL, NULL, perform_initialize_registered, NULL, - sizeof (*args), free_initialize_registered); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_modules_initialize_registered_finish: - * @result: the asynchronous result - * @error: location to place an error on failure - * - * Finishes the asynchronous operation to initialize the registered - * PKCS\#11 modules. - * - * Returns: (transfer full) (element-type Gck.Module): a list of newly - * initialized #GckModule objects - */ -GList * -gck_modules_initialize_registered_finish (GAsyncResult *result, - GError **error) -{ - GList *modules = NULL; - InitializeRegistered *args; - - args = _gck_call_arguments (result, InitializeRegistered); - if (_gck_call_basic_finish (result, error)) { - modules = args->results; - args->results = NULL; - - } else { - /* A custom error from perform_initialize */ - if (args->error) { - g_clear_error (error); - g_propagate_error (error, args->error); - args->error = NULL; - } - } - - return modules; -} - -/** - * gck_modules_get_slots: - * @modules: (element-type Gck.Module): The modules - * @token_present: Whether to only list slots with token present - * - * Get a list of slots for across all of the modules. - * - * Returns: (transfer full) (element-type Gck.Slot): A list of #GckSlot - * objects, which should be freed with gck_list_unref_free(). - */ -GList* -gck_modules_get_slots (GList *modules, gboolean token_present) -{ - GList *result = NULL; - GList *m; - - for (m = modules; m; m = g_list_next (m)) { - result = g_list_concat (result, gck_module_get_slots (m->data, token_present)); - } - - return result; -} - -/** - * gck_modules_enumerate_objects: - * @modules: (element-type Gck.Module): The modules - * @attrs: Attributes that the objects must have, or empty for all objects - * @session_options: Options from GckSessionOptions - * - * Setup an enumerator for listing matching objects on the modules. - * - * This call will not block but will return an enumerator immediately. - - * Return value: (transfer full): A new enumerator, which should be released - * with g_object_unref(). - **/ -GckEnumerator* -gck_modules_enumerate_objects (GList *modules, - GckAttributes *attrs, - GckSessionOptions session_options) -{ - GckUriData *uri_data; - - g_return_val_if_fail (attrs, NULL); - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_ref (attrs); - - return _gck_enumerator_new (modules, session_options, uri_data); -} - -static GList * -tokens_for_uri (GList *modules, - const gchar *uri, - gboolean only_one, - GError **error) -{ - GckTokenInfo *token_info; - GList *results = NULL; - GckUriData *uri_data; - GckModuleInfo *module_info; - GList *slots; - GList *m, *s; - gboolean matched; - GckUriFlags flags; - - flags = GCK_URI_FOR_OBJECT_ON_TOKEN_AND_MODULE | GCK_URI_FOR_MODULE_WITH_VERSION; - uri_data = gck_uri_parse (uri, flags, error); - if (uri_data == NULL) - return NULL; - - if (!uri_data->any_unrecognized) { - for (m = modules; m != NULL; m = g_list_next (m)) { - if (only_one && results) - break; - if (uri_data->module_info) { - module_info = gck_module_get_info (m->data); - matched = _gck_module_info_match (uri_data->module_info, module_info); - gck_module_info_free (module_info); - if (!matched) - continue; - } - - slots = gck_module_get_slots (m->data, TRUE); - for (s = slots; s != NULL; s = g_list_next (s)) { - if (only_one && results) - break; - if (!uri_data->token_info) { - results = g_list_prepend (results, g_object_ref (s->data)); - } else { - token_info = gck_slot_get_token_info (s->data); - if (token_info && _gck_token_info_match (uri_data->token_info, token_info)) - results = g_list_prepend (results, g_object_ref (s->data)); - gck_token_info_free (token_info); - } - } - gck_list_unref_free (slots); - } - } - - gck_uri_data_free (uri_data); - return results; -} - -/** - * gck_modules_token_for_uri: - * @modules: (element-type Gck.Module): The modules - * @uri: The URI that the token must match - * @error: A location to raise an error on failure - * - * Lookup a token that matches the URI. - * - * Returns: (transfer full): A newly allocated #GckSlot or %NULL if no such - * token was found. - */ -GckSlot* -gck_modules_token_for_uri (GList *modules, - const gchar *uri, - GError **error) -{ - GList *results; - GckSlot *slot = NULL; - - g_return_val_if_fail (uri != NULL, NULL); - - results = tokens_for_uri (modules, uri, TRUE, error); - if (results) - slot = g_object_ref (results->data); - gck_list_unref_free (results); - - return slot; -} - -/** - * gck_modules_tokens_for_uri: - * @modules: (element-type Gck.Module): The modules - * @uri: The URI that the token must match - * @error: A location to raise an error on failure - * - * Lookup a token that matches the URI. - * - * Returns: (transfer full) (element-type Gck.Slot): A list of newly allocated - * #GckSlot objects. Use gck_list_unref_free() to release the list once you're - * done with it. - */ -GList * -gck_modules_tokens_for_uri (GList *modules, - const gchar *uri, - GError **error) -{ - g_return_val_if_fail (uri != NULL, NULL); - - return tokens_for_uri (modules, uri, FALSE, error); -} - -/** - * gck_modules_object_for_uri: - * @modules: (element-type Gck.Module): The modules - * @uri: The URI the objects must match - * @session_options: Options from GckSessionOptions - * @error: A location to raise an error on failure. - * - * Find an object that matches a URI. - * - * This call can block. Use gck_modules_enumerate_uri() for a non-blocking - * version. - * - * Returns: (transfer full) (allow-none): A new #GckObject which should be released with - * g_object_unref(), or %NULL if no matching object was found. - */ -GckObject* -gck_modules_object_for_uri (GList *modules, - const gchar *uri, - GckSessionOptions session_options, - GError **error) -{ - GckEnumerator *en; - GckObject *result; - - g_return_val_if_fail (uri != NULL, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - en = gck_modules_enumerate_uri (modules, uri, session_options, error); - if (en == NULL) - return NULL; - - result = gck_enumerator_next (en, NULL, error); - g_object_unref (en); - - return result; -} - -/** - * gck_modules_objects_for_uri: - * @modules: (element-type Gck.Module): The modules - * @uri: The URI the objects must match - * @session_options: Options from GckSessionOptions - * @error: A location to raise an error on failure. - * - * Find objects that match a URI. - * - * This call can block. Use gck_modules_enumerate_uri() for a non-blocking - * version. - * - * Returns: (transfer full) (element-type Gck.Object): A list of #GckObject which - * should be released with gck_list_unref_free(), or %NULL if no matching object - * was found. - */ -GList* -gck_modules_objects_for_uri (GList *modules, - const gchar *uri, - GckSessionOptions session_options, - GError **error) -{ - GckEnumerator *en; - GList *results; - - g_return_val_if_fail (uri != NULL, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - en = gck_modules_enumerate_uri (modules, uri, session_options, error); - if (en == NULL) - return NULL; - - results = gck_enumerator_next_n (en, -1, NULL, error); - g_object_unref (en); - - return results; -} - -/** - * gck_modules_enumerate_uri: - * @modules: (element-type Gck.Module): The modules - * @uri: The URI that the enumerator will match - * @session_options: Options from GckSessionOptions - * @error: A location to raise an error on failure. - * - * Enumerate objects that match a URI. - * - * This call will not block. Use the #GckEnumerator functions in order to - * get at the actual objects that match. - * - * Returns: (transfer full): A new #GckEnumerator, or %NULL if an error occurs. - */ -GckEnumerator* -gck_modules_enumerate_uri (GList *modules, - const gchar *uri, - GckSessionOptions session_options, - GError **error) -{ - GckUriData *uri_data; - - g_return_val_if_fail (uri != NULL, NULL); - - uri_data = gck_uri_parse (uri, GCK_URI_FOR_ANY, error); - if (uri_data == NULL) - return NULL; - - /* Takes ownership of uri_info */ - return _gck_enumerator_new (modules, session_options, uri_data); -} diff --git a/gck/gck-object.c b/gck/gck-object.c deleted file mode 100644 index 7798db20..00000000 --- a/gck/gck-object.c +++ /dev/null @@ -1,1326 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-object.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" - -#include <string.h> - -/** - * SECTION:gck-object - * @title: GckObject - * @short_description: Represents a PKCS11 object such as a key or certificate. - * - * A GckObject holds a handle to a PKCS11 object such as a key or certificate. Token objects - * are stored on the token persistently. Others are transient and are called session objects. - */ - -/** - * GckObject: - * @parent: derived from this. - * - * Represents a PKCS11 object handle such as a key or certifiacte. - */ - -/* - * MT safe -- Nothing in GckObjectData changes between - * init and finalize. All GckObjectPrivate access between init - * and finalize is locked. - */ - -enum { - PROP_0, - PROP_MODULE, - PROP_SESSION, - PROP_HANDLE -}; - -struct _GckObjectPrivate { - GckModule *module; - GckSession *session; - CK_OBJECT_HANDLE handle; -}; - -G_DEFINE_TYPE (GckObject, gck_object, G_TYPE_OBJECT); - -/* ---------------------------------------------------------------------------- - * OBJECT - */ - -static void -gck_object_init (GckObject *self) -{ - self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_OBJECT, GckObjectPrivate); -} - -static void -gck_object_get_property (GObject *obj, guint prop_id, GValue *value, - GParamSpec *pspec) -{ - GckObject *self = GCK_OBJECT (obj); - - switch (prop_id) { - case PROP_MODULE: - g_value_take_object (value, gck_object_get_module (self)); - break; - case PROP_SESSION: - g_value_take_object (value, gck_object_get_session (self)); - break; - case PROP_HANDLE: - g_value_set_ulong (value, gck_object_get_handle (self)); - break; - } -} - -static void -gck_object_set_property (GObject *obj, guint prop_id, const GValue *value, - GParamSpec *pspec) -{ - GckObject *self = GCK_OBJECT (obj); - - /* The sets to data below are only allowed during construction */ - - switch (prop_id) { - case PROP_MODULE: - g_return_if_fail (!self->pv->module); - self->pv->module = g_value_get_object (value); - g_return_if_fail (self->pv->module); - g_object_ref (self->pv->module); - break; - case PROP_SESSION: - g_return_if_fail (!self->pv->session); - self->pv->session = g_value_get_object (value); - g_return_if_fail (self->pv->session); - g_object_ref (self->pv->session); - break; - case PROP_HANDLE: - g_return_if_fail (!self->pv->handle); - self->pv->handle = g_value_get_ulong (value); - break; - } -} - -static void -gck_object_finalize (GObject *obj) -{ - GckObject *self = GCK_OBJECT (obj); - - if (self->pv->session) - g_object_unref (self->pv->session); - self->pv->session = NULL; - - if (self->pv->module) - g_object_unref (self->pv->module); - self->pv->module = NULL; - - self->pv->handle = 0; - - G_OBJECT_CLASS (gck_object_parent_class)->finalize (obj); -} - - -static void -gck_object_class_init (GckObjectClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - gck_object_parent_class = g_type_class_peek_parent (klass); - - gobject_class->get_property = gck_object_get_property; - gobject_class->set_property = gck_object_set_property; - gobject_class->finalize = gck_object_finalize; - - /** - * GckObject:module: - * - * The GckModule that this object belongs to. - */ - g_object_class_install_property (gobject_class, PROP_MODULE, - g_param_spec_object ("module", "Module", "PKCS11 Module", - GCK_TYPE_MODULE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckObject:handle: - * - * The raw PKCS11 handle for this object. - */ - g_object_class_install_property (gobject_class, PROP_HANDLE, - g_param_spec_ulong ("handle", "Object Handle", "PKCS11 Object Handle", - 0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckObject:session: - * - * The PKCS11 session to make calls on when this object needs to - * perform operations on itself. - * - * If this is NULL then a new session is opened for each operation, - * such as gck_object_get(), gck_object_set() or gck_object_destroy(). - */ - g_object_class_install_property (gobject_class, PROP_SESSION, - g_param_spec_object ("session", "session", "PKCS11 Session to make calls on", - GCK_TYPE_SESSION, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - g_type_class_add_private (klass, sizeof (GckObjectPrivate)); -} - -/* ---------------------------------------------------------------------------- - * PUBLIC - */ - -/** - * gck_object_from_handle: - * @session: The session through which this object is accessed or created. - * @object_handle: The raw CK_OBJECT_HANDLE of the object. - * - * Initialize a GckObject from a raw PKCS\#11 handle. Normally you would use - * gck_session_create_object() or gck_session_find_objects() to access objects. - * - * Return value: (transfer full): The new GckObject. You should use - * g_object_unref() when done with this object. - **/ -GckObject * -gck_object_from_handle (GckSession *session, - gulong object_handle) -{ - GckModule *module = NULL; - GckObject *object; - - g_return_val_if_fail (GCK_IS_SESSION (session), NULL); - - module = gck_session_get_module (session); - object = g_object_new (GCK_TYPE_OBJECT, - "module", module, - "handle", object_handle, - "session", session, - NULL); - g_object_unref (module); - - return object; -} - -/** - * gck_objects_from_handle_array: - * @session: The session for these objects - * @object_handles: (array length=n_object_handles): The raw object handles. - * @n_object_handles: The number of raw object handles. - * - * Initialize a list of GckObject from raw PKCS\#11 handles. The handles argument must contain - * contiguous CK_OBJECT_HANDLE handles in an array. - * - * Returns: (transfer full) (element-type Gck.Object): The list of #GckObject - * objects. You should use gck_list_unref_free() when done with this - * list. - **/ -GList * -gck_objects_from_handle_array (GckSession *session, - gulong *object_handles, - gulong n_object_handles) -{ - GList *results = NULL; - CK_ULONG i; - - g_return_val_if_fail (GCK_IS_SESSION (session), NULL); - g_return_val_if_fail (n_object_handles == 0 || object_handles != NULL, NULL); - - for (i = 0; i < n_object_handles; ++i) - results = g_list_prepend (results, gck_object_from_handle (session, object_handles[i])); - return g_list_reverse (results); -} - -/** - * gck_object_equal: - * @object1: (type Gck.Object): a pointer to the first #GckObject - * @object2: (type Gck.Object): a pointer to the second #GckObject - * - * Checks equality of two objects. Two GckObject objects can point to the same - * underlying PKCS\#11 object. - * - * Return value: TRUE if object1 and object2 are equal. FALSE if either is not a GckObject. - **/ -gboolean -gck_object_equal (gconstpointer object1, gconstpointer object2) -{ - GckObject *obj1, *obj2; - GckSlot *slot1, *slot2; - gboolean ret; - - if (object1 == object2) - return TRUE; - if (!GCK_IS_OBJECT (object1) || !GCK_IS_OBJECT (object2)) - return FALSE; - - obj1 = GCK_OBJECT (object1); - obj2 = GCK_OBJECT (object2); - - slot1 = gck_session_get_slot (obj1->pv->session); - slot2 = gck_session_get_slot (obj2->pv->session); - - ret = obj1->pv->handle == obj2->pv->handle && - gck_slot_equal (slot1, slot2); - - g_object_unref (slot1); - g_object_unref (slot2); - - return ret; -} - -/** - * gck_object_hash: - * @object: (type Gck.Object): a pointer to a #GckObject - * - * Create a hash value for the GckObject. - * - * This function is intended for easily hashing a GckObject to add to - * a GHashTable or similar data structure. - * - * Return value: An integer that can be used as a hash value, or 0 if invalid. - **/ -guint -gck_object_hash (gconstpointer object) -{ - GckObject *self; - GckSlot *slot; - guint hash; - - g_return_val_if_fail (GCK_IS_OBJECT (object), 0); - - self = GCK_OBJECT (object); - slot = gck_session_get_slot (self->pv->session); - - hash = _gck_ulong_hash (&self->pv->handle) ^ - gck_slot_hash (slot); - - g_object_unref (slot); - - return hash; -} - - -/** - * gck_object_get_handle: - * @self: The object. - * - * Get the raw PKCS\#11 handle of a GckObject. - * - * Return value: the raw CK_OBJECT_HANDLE object handle - **/ -gulong -gck_object_get_handle (GckObject *self) -{ - g_return_val_if_fail (GCK_IS_OBJECT (self), (CK_OBJECT_HANDLE)-1); - return self->pv->handle; -} - -/** - * gck_object_get_module: - * @self: The object. - * - * Get the PKCS\#11 module to which this object belongs. - * - * Returns: (transfer full): the module, which should be unreffed after use - **/ -GckModule * -gck_object_get_module (GckObject *self) -{ - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (GCK_IS_MODULE (self->pv->module), NULL); - return g_object_ref (self->pv->module); -} - - -/** - * gck_object_get_session: - * @self: The object - * - * Get the PKCS\#11 session assigned to make calls on when operating - * on this object. - * - * This will only return a session if it was set explitly on this - * object. By default an object will open and close sessions - * appropriate for its calls. - * - * Returns: (transfer full): the assigned session, which must be unreffed after use - **/ -GckSession * -gck_object_get_session (GckObject *self) -{ - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (GCK_IS_SESSION (self->pv->session), NULL); - return g_object_ref (self->pv->session); -} - -/* -------------------------------------------------------------------------------------- - * DESTROY - */ - -typedef struct _Destroy { - GckArguments base; - CK_OBJECT_HANDLE object; -} Destroy; - -static CK_RV -perform_destroy (Destroy *args) -{ - g_assert (args); - return (args->base.pkcs11->C_DestroyObject) (args->base.handle, args->object); -} - -/** - * gck_object_destroy: - * @self: The object to destroy. - * @cancellable: Optional cancellable object, or NULL to ignore. - * @error: A location to return an error. - * - * Destroy a PKCS\#11 object, deleting it from storage or the session. - * This call may block for an indefinite period. - * - * Return value: Whether the call was successful or not. - **/ -gboolean -gck_object_destroy (GckObject *self, GCancellable *cancellable, GError **error) -{ - Destroy args = { GCK_ARGUMENTS_INIT, 0 }; - - g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE); - g_return_val_if_fail (GCK_IS_SESSION (self->pv->session), FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - args.object = self->pv->handle; - return _gck_call_sync (self->pv->session, perform_destroy, NULL, &args, cancellable, error); -} - -/** - * gck_object_destroy_async: - * @self: The object to destroy. - * @cancellable: Optional cancellable object, or NULL to ignore. - * @callback: Callback which is called when operation completes. - * @user_data: Data to pass to the callback. - * - * Destroy a PKCS\#11 object, deleting it from storage or the session. - * This call will return immediately and complete asynchronously. - **/ -void -gck_object_destroy_async (GckObject *self, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - Destroy* args; - - g_return_if_fail (GCK_IS_OBJECT (self)); - g_return_if_fail (GCK_IS_SESSION (self->pv->session)); - - args = _gck_call_async_prep (self->pv->session, self, perform_destroy, NULL, sizeof (*args), NULL); - args->object = self->pv->handle; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_object_destroy_finish: - * @self: The object being destroyed. - * @result: The result of the destory operation passed to the callback. - * @error: A location to store an error. - * - * Get the status of the operation to destroy a PKCS\#11 object, begun with - * gck_object_destroy_async(). - * - * Return value: Whether the object was destroyed successfully or not. - */ -gboolean -gck_object_destroy_finish (GckObject *self, GAsyncResult *result, GError **error) -{ - g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE); - g_return_val_if_fail (GCK_IS_CALL (result), FALSE); - return _gck_call_basic_finish (result, error); -} - -/* -------------------------------------------------------------------------------------- - * SET ATTRIBUTES - */ - -typedef struct _SetAttributes { - GckArguments base; - GckAttributes *attrs; - CK_OBJECT_HANDLE object; -} SetAttributes; - -static CK_RV -perform_set_attributes (SetAttributes *args) -{ - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - - g_assert (args); - attrs = _gck_attributes_commit_out (args->attrs, &n_attrs); - - return (args->base.pkcs11->C_SetAttributeValue) (args->base.handle, args->object, - attrs, n_attrs); -} - -static void -free_set_attributes (SetAttributes *args) -{ - g_assert (args); - gck_attributes_unref (args->attrs); - g_free (args); -} - -/** - * gck_object_set: - * @self: The object to set attributes on. - * @attrs: The attributes to set on the object. - * @cancellable: Optional cancellable object, or NULL to ignore. - * @error: A location to return an error. - * - * Set PKCS\#11 attributes on an object. This call may block for an indefinite period. - * - * Return value: Whether the call was successful or not. - **/ -gboolean -gck_object_set (GckObject *self, GckAttributes *attrs, - GCancellable *cancellable, GError **error) -{ - SetAttributes args; - gboolean ret = FALSE; - - g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE); - g_return_val_if_fail (attrs != NULL, FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - _gck_attributes_lock (attrs); - - memset (&args, 0, sizeof (args)); - args.attrs = attrs; - args.object = self->pv->handle; - - ret = _gck_call_sync (self->pv->session, perform_set_attributes, NULL, &args, cancellable, error); - - _gck_attributes_unlock (attrs); - return ret; -} - -/** - * gck_object_set_async: - * @self: The object to set attributes on. - * @attrs: The attributes to set on the object. - * @cancellable: Optional cancellable object, or NULL to ignore. - * @callback: Callback which is called when operation completes. - * @user_data: Data to pass to the callback. - * - * Set PKCS\#11 attributes on an object. This call will return - * immediately and completes asynchronously. - **/ -void -gck_object_set_async (GckObject *self, GckAttributes *attrs, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - SetAttributes *args; - - g_return_if_fail (GCK_IS_OBJECT (self)); - g_return_if_fail (attrs != NULL); - - args = _gck_call_async_prep (self->pv->session, self, perform_set_attributes, - NULL, sizeof (*args), free_set_attributes); - - _gck_attributes_lock (attrs); - args->attrs = gck_attributes_ref (attrs); - args->object = self->pv->handle; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_object_set_finish: - * @self: The object to set attributes on. - * @result: The result of the destory operation passed to the callback. - * @error: A location to store an error. - * - * Get the status of the operation to set attributes on a PKCS\#11 object, - * begun with gck_object_set_async(). - * - * Return value: Whether the attributes were successfully set on the object or not. - */ -gboolean -gck_object_set_finish (GckObject *self, GAsyncResult *result, GError **error) -{ - SetAttributes *args; - - g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE); - g_return_val_if_fail (GCK_IS_CALL (result), FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - /* Unlock the attributes we were using */ - args = _gck_call_arguments (result, SetAttributes); - g_assert (args->attrs); - _gck_attributes_unlock (args->attrs); - - return _gck_call_basic_finish (result, error); -} - -/* ------------------------------------------------------------------------------------ - * GET ATTRIBUTES - */ - -typedef struct _GetAttributes { - GckArguments base; - CK_OBJECT_HANDLE object; - GckAttributes *attrs; -} GetAttributes; - -/* - * Certain failure return values only apply to individual attributes - * being retrieved. These are ignored, since the attribute should - * already have -1 set as the length. - */ -static gboolean -is_ok_get_attributes_rv (CK_RV rv) -{ - switch (rv) { - case CKR_OK: - case CKR_ATTRIBUTE_SENSITIVE: - case CKR_ATTRIBUTE_TYPE_INVALID: - return TRUE; - default: - return FALSE; - } -} - -static CK_RV -perform_get_attributes (GetAttributes *args) -{ - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - CK_RV rv; - - g_assert (args); - g_assert (args->attrs); - - /* Prepare all the attributes */ - attrs = _gck_attributes_prepare_in (args->attrs, &n_attrs); - - /* Get the size of each value */ - rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, - attrs, n_attrs); - if (!is_ok_get_attributes_rv (rv)) - return rv; - - /* Allocate memory for each value */ - attrs = _gck_attributes_commit_in (args->attrs, &n_attrs); - - /* Now get the actual values */ - rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, - attrs, n_attrs); - - if (is_ok_get_attributes_rv (rv)) - rv = CKR_OK; - - return rv; -} - -static void -free_get_attributes (GetAttributes *args) -{ - g_assert (args); - g_assert (args->attrs); - gck_attributes_unref (args->attrs); - g_free (args); -} - - -/** - * gck_object_get: - * @self: The object to get attributes from. - * @cancellable: A #GCancellable or %NULL - * @error: A location to store an error. - * @...: The attribute types to get. - * - * Get the specified attributes from the object. This call may - * block for an indefinite period. - * - * Returns: (transfer full): the resulting PKCS\#11 attributes, or %NULL if an - * error occurred; the result must be unreffed when you're finished - * with it - **/ -GckAttributes * -gck_object_get (GckObject *self, GCancellable *cancellable, GError **error, ...) -{ - GckAttributes *attrs; - GArray *array; - va_list va; - gulong type; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (!error || !*error, NULL); - - array = g_array_new (FALSE, TRUE, sizeof (gulong)); - va_start (va, error); - for (;;) { - type = va_arg (va, gulong); - if (type == GCK_INVALID) - break; - g_array_append_val (array, type); - } - va_end (va); - - attrs = gck_object_get_full (self, (gulong*)array->data, array->len, cancellable, error); - g_array_free (array, TRUE); - - return attrs; -} - -/** - * gck_object_get_full: - * @self: The object to get attributes from. - * @attr_types: (array length=n_attr_types): the types of the attributes to get - * @n_attr_types: the number of attr_types - * @cancellable: optional cancellation object, or %NULL - * @error: A location to store an error. - * - * Get the specified attributes from the object. This call may - * block for an indefinite period. - * - * No extra references are added to the returned attributes pointer. - * During this call you may not access the attributes in any way. - * - * Returns: (transfer full): a pointer to the filled in attributes if successful, - * or %NULL if not - **/ -GckAttributes * -gck_object_get_full (GckObject *self, gulong *attr_types, guint n_attr_types, - GCancellable *cancellable, GError **error) -{ - GetAttributes args; - GckAttributes *attrs; - gboolean ret; - guint i; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (!error || !*error, NULL); - - attrs = gck_attributes_new (); - for (i = 0; i < n_attr_types; ++i) - gck_attributes_add_empty (attrs, attr_types[i]); - - _gck_attributes_lock (attrs); - - memset (&args, 0, sizeof (args)); - args.attrs = attrs; - args.object = self->pv->handle; - - ret = _gck_call_sync (self->pv->session, perform_get_attributes, NULL, &args, cancellable, error); - _gck_attributes_unlock (attrs); - - if (!ret) { - gck_attributes_unref (attrs); - attrs = NULL; - } - - return attrs; -} - -/** - * gck_object_get_async: - * @self: The object to get attributes from. - * @attr_types: (array length=n_attr_types): the types of the attributes to get - * @n_attr_types: the number of attr_types - * @cancellable: optional cancellation object, or %NULL - * @callback: A callback which is called when the operation completes. - * @user_data: Data to be passed to the callback. - * - * Get the specified attributes from the object. The attributes will be cleared - * of their current values, and new attributes will be stored. The attributes - * should not be accessed in any way except for referencing and unreferencing - * them until gck_object_get_finish() is called. - * - * This call returns immediately and completes asynchronously. - **/ -void -gck_object_get_async (GckObject *self, gulong *attr_types, guint n_attr_types, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckAttributes *attrs; - GetAttributes *args; - guint i; - - g_return_if_fail (GCK_IS_OBJECT (self)); - - attrs = gck_attributes_new (); - for (i = 0; i < n_attr_types; ++i) - gck_attributes_add_empty (attrs, attr_types[i]); - - args = _gck_call_async_prep (self->pv->session, self, perform_get_attributes, - NULL, sizeof (*args), free_get_attributes); - - _gck_attributes_lock (attrs); - args->attrs = attrs; - args->object = self->pv->handle; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_object_get_finish: - * @self: The object to get attributes from. - * @result: The result passed to the callback. - * @error: A location to store an error. - * - * Get the result of a get operation and return specified attributes from - * the object. - * - * No extra references are added to the returned attributes pointer. - * - * Return value: The filled in attributes structure if successful or - * NULL if not successful. - **/ -GckAttributes* -gck_object_get_finish (GckObject *self, GAsyncResult *result, GError **error) -{ - GetAttributes *args; - GckAttributes *attrs; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (GCK_IS_CALL (result), NULL); - g_return_val_if_fail (!error || !*error, NULL); - - args = _gck_call_arguments (result, GetAttributes); - _gck_attributes_unlock (args->attrs); - attrs = gck_attributes_ref (args->attrs); - - if (!_gck_call_basic_finish (result, error)) { - gck_attributes_unref (attrs); - attrs = NULL; - } - - return attrs; -} - -/* --------------------------------------------------------------------------------- - * GET ATTRIBUTE DATA - */ - -typedef struct _GetAttributeData { - GckArguments base; - CK_OBJECT_HANDLE object; - CK_ATTRIBUTE_TYPE type; - GckAllocator allocator; - guchar *result; - gsize n_result; -} GetAttributeData; - -static CK_RV -perform_get_attribute_data (GetAttributeData *args) -{ - CK_ATTRIBUTE attr; - CK_RV rv; - - g_assert (args); - g_assert (args->allocator); - - attr.type = args->type; - attr.ulValueLen = 0; - attr.pValue = 0; - - /* Get the size of the value */ - rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, - &attr, 1); - if (rv != CKR_OK) - return rv; - - /* Allocate memory for the value */ - args->result = (args->allocator) (NULL, attr.ulValueLen + 1); - g_assert (args->result); - attr.pValue = args->result; - - /* Now get the actual value */ - rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, - &attr, 1); - - if (rv == CKR_OK) { - args->n_result = attr.ulValueLen; - args->result[args->n_result] = 0; - } - - return rv; -} - -static void -free_get_attribute_data (GetAttributeData *args) -{ - g_assert (args); - g_free (args->result); - g_free (args); -} - -/** - * gck_object_get_data: - * @self: The object to get attribute data from. - * @attr_type: The attribute to get data for. - * @cancellable: A #GCancellable or %NULL - * @n_data: The length of the resulting data. - * @error: A location to store an error. - * - * Get the data for the specified attribute from the object. For convenience - * the returned data has a null terminator. - * - * This call may block for an indefinite period. - * - * Returns: (transfer full) (array length=n_data): the resulting PKCS\#11 - * attribute data, or %NULL if an error occurred - **/ -guchar * -gck_object_get_data (GckObject *self, - gulong attr_type, - GCancellable *cancellable, - gsize *n_data, - GError **error) -{ - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (n_data, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - return gck_object_get_data_full (self, attr_type, g_realloc, cancellable, n_data, error); -} - -/** - * gck_object_get_data_full: (skip) - * @self: The object to get attribute data from. - * @attr_type: The attribute to get data for. - * @allocator: An allocator with which to allocate memory for the data, or NULL for default. - * @cancellable: Optional cancellation object, or NULL. - * @n_data: The length of the resulting data. - * @error: A location to store an error. - * - * Get the data for the specified attribute from the object. For convenience - * the returned data has an extra null terminator, not included in the returned length. - * - * This call may block for an indefinite period. - * - * Returns: (transfer full) (array length=n_data): The resulting PKCS\#11 - * attribute data, or %NULL if an error occurred. - **/ -guchar * -gck_object_get_data_full (GckObject *self, gulong attr_type, GckAllocator allocator, - GCancellable *cancellable, gsize *n_data, GError **error) -{ - GetAttributeData args; - gboolean ret; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (n_data, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - if (!allocator) - allocator = g_realloc; - - memset (&args, 0, sizeof (args)); - args.allocator = allocator; - args.object = self->pv->handle; - args.type = attr_type; - - ret = _gck_call_sync (self->pv->session, perform_get_attribute_data, NULL, &args, cancellable, error); - - /* Free any value if failed */ - if (!ret) { - if (args.result) - (allocator) (args.result, 0); - return NULL; - } - - *n_data = args.n_result; - return args.result; -} - -/** - * gck_object_get_data_async: - * @self: The object to get attribute data from. - * @attr_type: The attribute to get data for. - * @allocator: (skip): An allocator with which to allocate memory for the data, or NULL for default. - * @cancellable: Optional cancellation object, or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to be passed to the callback. - * - * Get the data for the specified attribute from the object. - * - * This call will return immediately and complete asynchronously. - **/ -void -gck_object_get_data_async (GckObject *self, gulong attr_type, GckAllocator allocator, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - GetAttributeData *args; - - g_return_if_fail (GCK_IS_OBJECT (self)); - - if (!allocator) - allocator = g_realloc; - - args = _gck_call_async_prep (self->pv->session, self, perform_get_attribute_data, - NULL, sizeof (*args), free_get_attribute_data); - - args->allocator = allocator; - args->object = self->pv->handle; - args->type = attr_type; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_object_get_data_finish: - * @self: The object to get an attribute from. - * @result: The result passed to the callback. - * @n_data: The length of the resulting data. - * @error: A location to store an error. - * - * Get the result of an operation to get attribute data from - * an object. For convenience the returned data has an extra null terminator, - * not included in the returned length. - * - * Returns: (transfer full) (array length=n_data): The PKCS\#11 attribute data - * or %NULL if an error occurred. - **/ -guchar * -gck_object_get_data_finish (GckObject *self, - GAsyncResult *result, - gsize *n_data, - GError **error) -{ - GetAttributeData *args; - guchar *data; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (GCK_IS_CALL (result), NULL); - g_return_val_if_fail (n_data, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - - args = _gck_call_arguments (result, GetAttributeData); - - *n_data = args->n_result; - data = args->result; - args->result = NULL; - - return data; -} - -/* --------------------------------------------------------------------------------------- - * SET TEMPLATE - */ - -typedef struct _set_template_args { - GckArguments base; - CK_OBJECT_HANDLE object; - CK_ATTRIBUTE_TYPE type; - GckAttributes *attrs; -} set_template_args; - -static CK_RV -perform_set_template (set_template_args *args) -{ - CK_ATTRIBUTE attr; - CK_ULONG n_attrs; - - g_assert (args); - - attr.type = args->type; - attr.pValue = _gck_attributes_commit_out (args->attrs, &n_attrs); - attr.ulValueLen = n_attrs * sizeof (CK_ATTRIBUTE); - - return (args->base.pkcs11->C_SetAttributeValue) (args->base.handle, args->object, &attr, 1); -} - -static void -free_set_template (set_template_args *args) -{ - g_assert (args); - gck_attributes_unref (args->attrs); - g_free (args); -} - -/** - * gck_object_set_template: - * @self: The object to set an attribute template on. - * @attr_type: The attribute template type. - * @attrs: The attribute template. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to store an error. - * - * Set an attribute template on the object. The attr_type must be for - * an attribute which contains a template. - * - * This call may block for an indefinite period. - * - * Return value: TRUE if the operation succeeded. - **/ -gboolean -gck_object_set_template (GckObject *self, gulong attr_type, GckAttributes *attrs, - GCancellable *cancellable, GError **error) -{ - set_template_args args; - gboolean ret = FALSE; - - g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE); - g_return_val_if_fail (attrs, FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - _gck_attributes_lock (attrs); - - memset (&args, 0, sizeof (args)); - args.attrs = attrs; - args.type = attr_type; - args.object = self->pv->handle; - - ret = _gck_call_sync (self->pv->session, perform_set_template, NULL, &args, cancellable, error); - - _gck_attributes_unlock (attrs); - return ret; -} - -/** - * gck_object_set_template_async: - * @self: The object to set an attribute template on. - * @attr_type: The attribute template type. - * @attrs: The attribute template. - * @cancellable: Optional cancellation object, or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to be passed to the callback. - * - * Set an attribute template on the object. The attr_type must be for - * an attribute which contains a template. - * - * This call will return immediately and complete asynchronously. - **/ -void -gck_object_set_template_async (GckObject *self, gulong attr_type, GckAttributes *attrs, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - set_template_args *args; - - g_return_if_fail (GCK_IS_OBJECT (self)); - g_return_if_fail (attrs); - - args = _gck_call_async_prep (self->pv->session, self, perform_set_template, - NULL, sizeof (*args), free_set_template); - - _gck_attributes_lock (attrs); - args->attrs = gck_attributes_ref (attrs); - args->type = attr_type; - args->object = self->pv->handle; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_object_set_template_finish: - * @self: The object to set an attribute template on. - * @result: The result passed to the callback. - * @error: A location to store an error. - * - * Get the result of an operation to set attribute template on - * an object. - * - * Return value: TRUE if the operation succeeded. - **/ -gboolean -gck_object_set_template_finish (GckObject *self, GAsyncResult *result, GError **error) -{ - set_template_args *args; - - g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE); - g_return_val_if_fail (GCK_IS_CALL (result), FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - /* Unlock the attributes we were using */ - args = _gck_call_arguments (result, set_template_args); - g_assert (args->attrs); - _gck_attributes_unlock (args->attrs); - - return _gck_call_basic_finish (result, error); -} - -/* --------------------------------------------------------------------------------------- - * GET TEMPLATE - */ - -typedef struct _get_template_args { - GckArguments base; - CK_OBJECT_HANDLE object; - CK_ATTRIBUTE_TYPE type; - GckAttributes *attrs; -} get_template_args; - -static CK_RV -perform_get_template (get_template_args *args) -{ - CK_ATTRIBUTE attr; - CK_ULONG n_attrs, i; - CK_RV rv; - - g_assert (args); - g_assert (!args->attrs); - - args->attrs = gck_attributes_new (); - attr.type = args->type; - attr.ulValueLen = 0; - attr.pValue = 0; - - /* Get the length of the entire template */ - rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1); - if (rv != CKR_OK) - return rv; - - /* Number of attributes, rounded down */ - n_attrs = (attr.ulValueLen / sizeof (CK_ATTRIBUTE)); - for (i = 0; i < n_attrs; ++i) - gck_attributes_add_empty (args->attrs, 0); - - /* Prepare all the attributes */ - _gck_attributes_lock (args->attrs); - attr.pValue = _gck_attributes_prepare_in (args->attrs, &n_attrs); - - /* Get the size of each value */ - rv = (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1); - if (rv != CKR_OK) - return rv; - - /* Allocate memory for each value */ - attr.pValue = _gck_attributes_commit_in (args->attrs, &n_attrs); - - /* Now get the actual values */ - return (args->base.pkcs11->C_GetAttributeValue) (args->base.handle, args->object, &attr, 1); -} - -static void -free_get_template (get_template_args *args) -{ - g_assert (args); - gck_attributes_unref (args->attrs); - g_free (args); -} - -/** - * gck_object_get_template: - * @self: The object to get an attribute template from. - * @attr_type: The template attribute type. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to store an error. - * - * Get an attribute template from the object. The attr_type must be for - * an attribute which returns a template. - * - * This call may block for an indefinite period. - * - * Returns: (transfer full): the resulting PKCS\#11 attribute template, or %NULL - * if an error occurred - **/ -GckAttributes * -gck_object_get_template (GckObject *self, gulong attr_type, - GCancellable *cancellable, GError **error) -{ - get_template_args args; - gboolean ret; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (!error || !*error, NULL); - - memset (&args, 0, sizeof (args)); - args.object = self->pv->handle; - args.type = attr_type; - - ret = _gck_call_sync (self->pv->session, perform_get_template, NULL, &args, cancellable, error); - - _gck_attributes_unlock (args.attrs); - - /* Free any value if failed */ - if (!ret) { - gck_attributes_unref (args.attrs); - args.attrs = NULL; - } - - return args.attrs; -} - -/** - * gck_object_get_template_async: - * @self: The object to get an attribute template from. - * @attr_type: The template attribute type. - * @cancellable: Optional cancellation object, or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to be passed to the callback. - * - * Get an attribute template from the object. The attr_type must be for - * an attribute which returns a template. - * - * This call will return immediately and complete asynchronously. - **/ -void -gck_object_get_template_async (GckObject *self, gulong attr_type, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - get_template_args *args; - - g_return_if_fail (GCK_IS_OBJECT (self)); - - args = _gck_call_async_prep (self->pv->session, self, perform_get_template, - NULL, sizeof (*args), free_get_template); - - args->object = self->pv->handle; - args->type = attr_type; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_object_get_template_finish: - * @self: The object to get an attribute from. - * @result: The result passed to the callback. - * @error: A location to store an error. - * - * Get the result of an operation to get attribute template from - * an object. - * - * Returns: (transfer full): the resulting PKCS\#11 attribute template, or %NULL - * if an error occurred - **/ -GckAttributes * -gck_object_get_template_finish (GckObject *self, GAsyncResult *result, - GError **error) -{ - get_template_args *args; - - g_return_val_if_fail (GCK_IS_OBJECT (self), NULL); - g_return_val_if_fail (GCK_IS_CALL (result), NULL); - g_return_val_if_fail (!error || !*error, NULL); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - - args = _gck_call_arguments (result, get_template_args); - _gck_attributes_unlock (args->attrs); - return gck_attributes_ref (args->attrs); -} diff --git a/gck/gck-password.c b/gck/gck-password.c deleted file mode 100644 index 56200ea1..00000000 --- a/gck/gck-password.c +++ /dev/null @@ -1,255 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-password.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" - -#include "egg/egg-timegm.h" - -#include <string.h> - -/** - * SECTION:gck-password - * @title: GckPassword - * @short_description: Represents a password hich is requested of the user - * - * This is used in conjuction with GTlsInteraction. #GckPassword is a - * GTlsPassword which contains additional information about which PKCS\#11 - * token or key the password is being requested for. - */ - -/** - * GckPassword: - * @parent: parent object - * - * A #GTlsPasswordClass that contains information about the PKCS\#11 token - * or key the password is being requested for. - */ - -/** - * GckPasswordClass: - * @parent: parent class - * - * The class for #GTlsPassword. - */ -enum { - PROP_0, - PROP_MODULE, - PROP_TOKEN, - PROP_KEY -}; - -struct _GckPasswordPrivate { - gboolean for_token; - gpointer token_or_key; -}; - -G_DEFINE_TYPE (GckPassword, gck_password, G_TYPE_TLS_PASSWORD); - -static void -gck_password_init (GckPassword *self) -{ - self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_PASSWORD, GckPasswordPrivate); -} - -static void -gck_password_constructed (GObject *obj) -{ - GckPassword *self = GCK_PASSWORD (obj); - - G_OBJECT_CLASS (gck_password_parent_class)->constructed (obj); - - g_return_if_fail (GCK_IS_SLOT (self->pv->token_or_key) || - GCK_IS_OBJECT (self->pv->token_or_key)); -} - -static void -gck_password_get_property (GObject *obj, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - GckPassword *self = GCK_PASSWORD (obj); - - switch (prop_id) { - case PROP_MODULE: - g_value_take_object (value, gck_password_get_module (self)); - break; - case PROP_TOKEN: - g_value_take_object (value, gck_password_get_token (self)); - break; - case PROP_KEY: - g_value_take_object (value, gck_password_get_key (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_password_set_property (GObject *obj, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - GckPassword *self = GCK_PASSWORD (obj); - gpointer object; - - /* All writes to data members below, happen only during construct phase */ - - switch (prop_id) { - case PROP_TOKEN: - object = g_value_dup_object (value); - if (object != NULL) { - g_assert (self->pv->token_or_key == NULL); - self->pv->token_or_key = object; - self->pv->for_token = TRUE; - } - break; - case PROP_KEY: - object = g_value_dup_object (value); - if (object != NULL) { - g_assert (self->pv->token_or_key == NULL); - self->pv->token_or_key = object; - self->pv->for_token = FALSE; - } - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_password_finalize (GObject *obj) -{ - GckPassword *self = GCK_PASSWORD (obj); - - g_clear_object (&self->pv->token_or_key); - - G_OBJECT_CLASS (gck_password_parent_class)->finalize (obj); -} - -static void -gck_password_class_init (GckPasswordClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - - gobject_class->constructed = gck_password_constructed; - gobject_class->get_property = gck_password_get_property; - gobject_class->set_property = gck_password_set_property; - gobject_class->finalize = gck_password_finalize; - - /** - * GckPassword:module: - * - * The PKCS\#11 module that is requesting the password - */ - g_object_class_install_property (gobject_class, PROP_MODULE, - g_param_spec_object ("module", "Module", "PKCS11 Module", - GCK_TYPE_MODULE, G_PARAM_READABLE)); - - /** - * GckPassword:token: - * - * The PKCS\#11 token the password is for, if this is set then - * the GckPassword:object property will be %NULL - */ - g_object_class_install_property (gobject_class, PROP_TOKEN, - g_param_spec_object ("token", "Token", "PKCS11 Token", - GCK_TYPE_SLOT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckPassword:key: - * - * The PKCS\#11 key that the password is being requested for. If this - * is set then the GckPassword:token property will be %NULL - */ - g_object_class_install_property (gobject_class, PROP_KEY, - g_param_spec_object ("key", "Object", "PKCS11 Key Object", - GCK_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - g_type_class_add_private (gobject_class, sizeof (GckPasswordPrivate)); -} - -/** - * gck_password_get_module: - * @self: the password object - * - * Get the PKCS\#11 module that is requesting the password. - * - * Returns: (transfer full): the module that is requesting the password, which - * must be unreferenced after use - */ -GckModule * -gck_password_get_module (GckPassword *self) -{ - g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL); - if (self->pv->for_token) - return gck_slot_get_module (self->pv->token_or_key); - else - return gck_object_get_module (self->pv->token_or_key); -} - -/** - * gck_password_get_token: - * @self: the password object - * - * If the password request is to unlock a PKCS\#11 token, then this is the - * slot containing that token. - * - * Returns: (transfer full): the slot that contains the token, or %NULL if not - * being requested for a token; must be unreferenced after use - */ -GckSlot * -gck_password_get_token (GckPassword *self) -{ - g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL); - if (!self->pv->for_token) - return NULL; - g_return_val_if_fail (GCK_IS_SLOT (self->pv->token_or_key), NULL); - return g_object_ref (self->pv->token_or_key); -} - -/** - * gck_password_get_key: - * @self: the password object - * - * If the password request is to unlock a PKCS\#11 key, then this is the - * the object representing that key. - * - * Returns: (transfer full): the password is for this key, or %NULL if not - * being requested for a key; must be unreferenced after use - */ -GckObject * -gck_password_get_key (GckPassword *self) -{ - g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL); - if (self->pv->for_token) - return NULL; - g_return_val_if_fail (GCK_IS_OBJECT (self->pv->token_or_key), NULL); - return g_object_ref (self->pv->token_or_key); -} diff --git a/gck/gck-private.h b/gck/gck-private.h deleted file mode 100644 index c2919cfb..00000000 --- a/gck/gck-private.h +++ /dev/null @@ -1,209 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-private.h - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#ifndef GCK_PRIVATE_H_ -#define GCK_PRIVATE_H_ - -#include "gck.h" - -#include <glib.h> -#include <glib-object.h> -#include <gio/gio.h> - -G_BEGIN_DECLS - -/* --------------------------------------------------------------------------- - * ATTRIBUTE INTERNALS - */ - -void _gck_attributes_lock (GckAttributes *attrs); - -void _gck_attributes_unlock (GckAttributes *attrs); - -CK_ATTRIBUTE_PTR _gck_attributes_prepare_in (GckAttributes *attrs, - CK_ULONG_PTR n_attrs); - -CK_ATTRIBUTE_PTR _gck_attributes_commit_in (GckAttributes *attrs, - CK_ULONG_PTR n_attrs); - -CK_ATTRIBUTE_PTR _gck_attributes_commit_out (GckAttributes *attrs, - CK_ULONG_PTR n_attrs); - -gchar * _gck_attributes_format (GckAttributes *attrs); - -/* ---------------------------------------------------------------------------- - * MISC - */ - -guint _gck_ulong_hash (gconstpointer v); - -gboolean _gck_ulong_equal (gconstpointer v1, - gconstpointer v2); - -const gchar * _gck_stringize_rv (CK_RV rv); - -CK_RV _gck_rv_from_error (GError *error, - CK_RV catch_all_code); - -/* ---------------------------------------------------------------------------- - * MODULE - */ - -GckModule* _gck_module_new_initialized (CK_FUNCTION_LIST_PTR funcs); - -GckModuleInfo* _gck_module_info_from_pkcs11 (CK_INFO_PTR info); - -void _gck_module_info_to_pkcs11 (GckModuleInfo* module_info, - CK_INFO_PTR info); - -gboolean _gck_module_info_match (GckModuleInfo *match, - GckModuleInfo *module_info); - -/* ----------------------------------------------------------------------------- - * ENUMERATOR - */ - -GckEnumerator* _gck_enumerator_new (GList *modules, - GckSessionOptions session_options, - GckUriData *uri_data); - -/* ---------------------------------------------------------------------------- - * SLOT - */ - -GckTokenInfo* _gck_token_info_from_pkcs11 (CK_TOKEN_INFO_PTR info); - -void _gck_token_info_to_pkcs11 (GckTokenInfo *token_info, - CK_TOKEN_INFO_PTR info); - -gboolean _gck_token_info_match (GckTokenInfo *match, - GckTokenInfo *info); - -CK_RV _gck_session_authenticate_token (CK_FUNCTION_LIST_PTR funcs, - CK_SESSION_HANDLE session, - GckSlot *token, - GTlsInteraction *interaction, - GCancellable *cancellable); - -CK_RV _gck_session_authenticate_key (CK_FUNCTION_LIST_PTR funcs, - CK_SESSION_HANDLE session, - GckObject *key, - GTlsInteraction *interaction, - GCancellable *cancellable); - -/* ---------------------------------------------------------------------------- - * PASSWORD - */ - -void _gck_password_update (GckPassword *self, - gboolean request_retry); - -/* ---------------------------------------------------------------------------- - * INTERACTION - */ - -#define GCK_TYPE_INTERACTION (_gck_interaction_get_type ()) -#define GCK_INTERACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCK_TYPE_INTERACTION, GckInteraction)) -#define GCK_IS_INTERACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCK_TYPE_INTERACTION)) - -typedef struct _GckInteraction GckInteraction; - -GType _gck_interaction_get_type (void) G_GNUC_CONST; - -GTlsInteraction * _gck_interaction_new (gpointer token_or_key); - -/* ---------------------------------------------------------------------------- - * CALL - */ - -typedef CK_RV (*GckPerformFunc) (gpointer call_data); -typedef gboolean (*GckCompleteFunc) (gpointer call_data, CK_RV result); - -typedef struct _GckCall GckCall; - -typedef struct _GckArguments { - GckCall *call; - - /* For the call function to use */ - CK_FUNCTION_LIST_PTR pkcs11; - CK_ULONG handle; - -} GckArguments; - -#define GCK_MECHANISM_EMPTY { 0UL, NULL, 0 } - -#define GCK_ARGUMENTS_INIT { NULL, NULL, 0 } - -#define GCK_TYPE_CALL (_gck_call_get_type()) -#define GCK_CALL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCK_TYPE_CALL, GckCall)) -#define GCK_CALL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCK_TYPE_CALL, GckCall)) -#define GCK_IS_CALL(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCK_TYPE_CALL)) -#define GCK_IS_CALL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCK_TYPE_CALL)) -#define GCK_CALL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCK_TYPE_CALL, GckCallClass)) - -typedef struct _GckCallClass GckCallClass; - -GType _gck_call_get_type (void) G_GNUC_CONST; - -#define _gck_call_arguments(call, type) (type*)(_gck_call_get_arguments (GCK_CALL (call))) - -gpointer _gck_call_get_arguments (GckCall *call); - -void _gck_call_uninitialize (void); - -gboolean _gck_call_sync (gpointer object, - gpointer perform, - gpointer complete, - gpointer args, - GCancellable *cancellable, - GError **err); - -gpointer _gck_call_async_prep (gpointer object, - gpointer cb_object, - gpointer perform, - gpointer complete, - gsize args_size, - gpointer destroy_func); - -GckCall* _gck_call_async_ready (gpointer args, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -void _gck_call_async_go (GckCall *call); - -void _gck_call_async_ready_go (gpointer args, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -void _gck_call_async_short (GckCall *call, - CK_RV rv); - -gboolean _gck_call_basic_finish (GAsyncResult *result, - GError **err); - -void _gck_call_async_object (GckCall *call, - gpointer object); - -#endif /* GCK_PRIVATE_H_ */ diff --git a/gck/gck-session.c b/gck/gck-session.c deleted file mode 100644 index c984eb6f..00000000 --- a/gck/gck-session.c +++ /dev/null @@ -1,2980 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-session.h - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#define DEBUG_FLAG GCK_DEBUG_SESSION -#include "gck-debug.h" -#include "gck-marshal.h" -#include "gck-private.h" - -#include <string.h> - -#include <glib/gi18n.h> - -/** - * SECTION:gck-session - * @title: GckSession - * @short_description: Represents an open PKCS11 session. - * - * Before performing any PKCS11 operations, a session must be opened. This is - * analogous to an open database handle, or a file handle. - */ - -/** - * GckSession: - * @parent: derived from this. - * - * Represents an open PKCS11 session. - */ - -/** - * GckSessionOptions: - * @GCK_SESSION_READ_ONLY: Open session as read only - * @GCK_SESSION_READ_WRITE: Open sessions as read/write - * @GCK_SESSION_LOGIN_USER: Login as user on new sessions - * @GCK_SESSION_AUTHENTICATE: Authenticate as necessary - * - * Options for creating sessions. - */ - -/** - * GckMechanism: - * @type: The mechanism type - * @parameter: Mechanism specific data. - * @n_parameter: Length of mechanism specific data. - * - * Represents a mechanism used with crypto operations. - */ - -enum { - DISCARD_HANDLE, - LAST_SIGNAL -}; - -enum { - PROP_0, - PROP_MODULE, - PROP_HANDLE, - PROP_INTERACTION, - PROP_SLOT, - PROP_OPTIONS, -}; - -struct _GckSessionPrivate { - GckSlot *slot; - GckModule *module; - CK_SESSION_HANDLE handle; - GTlsInteraction *interaction; - GckSessionOptions options; - - /* Modified atomically */ - gint discarded; -}; - -G_DEFINE_TYPE (GckSession, gck_session, G_TYPE_OBJECT); - -static guint signals[LAST_SIGNAL] = { 0 }; - -/* ---------------------------------------------------------------------------- - * OBJECT - */ - -static gboolean -gck_session_real_discard_handle (GckSession *self, CK_OBJECT_HANDLE handle) -{ - CK_FUNCTION_LIST_PTR funcs; - CK_RV rv; - - /* The default functionality, close the handle */ - - g_return_val_if_fail (self->pv->module, FALSE); - g_object_ref (self->pv->module); - - funcs = gck_module_get_functions (self->pv->module); - g_return_val_if_fail (funcs, FALSE); - - rv = (funcs->C_CloseSession) (handle); - if (rv != CKR_OK) { - g_warning ("couldn't close session properly: %s", - gck_message_from_rv (rv)); - } - - g_object_unref (self->pv->module); - return TRUE; -} - -static void -gck_session_init (GckSession *self) -{ - self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_SESSION, GckSessionPrivate); -} - -static void -gck_session_get_property (GObject *obj, guint prop_id, GValue *value, - GParamSpec *pspec) -{ - GckSession *self = GCK_SESSION (obj); - - switch (prop_id) { - case PROP_MODULE: - g_value_take_object (value, gck_session_get_module (self)); - break; - case PROP_HANDLE: - g_value_set_ulong (value, gck_session_get_handle (self)); - break; - case PROP_SLOT: - g_value_take_object (value, gck_session_get_slot (self)); - break; - case PROP_OPTIONS: - g_value_set_uint (value, gck_session_get_options (self)); - break; - case PROP_INTERACTION: - g_value_take_object (value, self->pv->interaction); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_session_set_property (GObject *obj, guint prop_id, const GValue *value, - GParamSpec *pspec) -{ - GckSession *self = GCK_SESSION (obj); - - /* Only valid calls are from constructor */ - - switch (prop_id) { - case PROP_MODULE: - g_return_if_fail (!self->pv->module); - self->pv->module = g_value_dup_object (value); - g_return_if_fail (self->pv->module); - break; - case PROP_HANDLE: - g_return_if_fail (!self->pv->handle); - self->pv->handle = g_value_get_ulong (value); - break; - case PROP_INTERACTION: - g_return_if_fail (self->pv->interaction == NULL); - self->pv->interaction = g_value_dup_object (value); - break; - case PROP_SLOT: - g_return_if_fail (!self->pv->slot); - self->pv->slot = g_value_dup_object (value); - g_return_if_fail (self->pv->slot); - break; - case PROP_OPTIONS: - g_return_if_fail (!self->pv->options); - self->pv->options = g_value_get_flags (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_session_dispose (GObject *obj) -{ - GckSession *self = GCK_SESSION (obj); - gboolean handled; - - g_return_if_fail (GCK_IS_SESSION (self)); - - if (g_atomic_int_compare_and_exchange (&self->pv->discarded, 0, 1)) { - - /* - * Let the world know that we're discarding the session - * handle. This allows any necessary session reuse to work. - */ - - g_signal_emit_by_name (self, "discard-handle", self->pv->handle, &handled); - g_return_if_fail (handled); - } - - G_OBJECT_CLASS (gck_session_parent_class)->dispose (obj); -} - -static void -gck_session_finalize (GObject *obj) -{ - GckSession *self = GCK_SESSION (obj); - - g_assert (g_atomic_int_get (&self->pv->discarded) != 0); - - g_clear_object (&self->pv->interaction); - g_clear_object (&self->pv->slot); - g_clear_object (&self->pv->module); - - G_OBJECT_CLASS (gck_session_parent_class)->finalize (obj); -} - -static void -gck_session_class_init (GckSessionClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - gck_session_parent_class = g_type_class_peek_parent (klass); - - gobject_class->get_property = gck_session_get_property; - gobject_class->set_property = gck_session_set_property; - gobject_class->dispose = gck_session_dispose; - gobject_class->finalize = gck_session_finalize; - - klass->discard_handle = gck_session_real_discard_handle; - - /** - * GckSession:module: - * - * The GckModule that this session is opened on. - */ - g_object_class_install_property (gobject_class, PROP_MODULE, - g_param_spec_object ("module", "Module", "PKCS11 Module", - GCK_TYPE_MODULE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckSession:handle: - * - * The raw CK_SESSION_HANDLE handle of this session. - */ - g_object_class_install_property (gobject_class, PROP_HANDLE, - g_param_spec_ulong ("handle", "Session Handle", "PKCS11 Session Handle", - 0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckSession:slot: - * - * The GckSlot this session is opened on. - */ - g_object_class_install_property (gobject_class, PROP_SLOT, - g_param_spec_object ("slot", "Slot that this session uses", "PKCS11 Slot", - GCK_TYPE_SLOT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckSession:options: - * - * The options this session was opened with. - */ - g_object_class_install_property (gobject_class, PROP_OPTIONS, - g_param_spec_flags ("options", "Session Options", "Session Options", - GCK_TYPE_SESSION_OPTIONS, GCK_SESSION_READ_ONLY, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckSession:interaction: - * - * Interaction object used to ask the user for pins when opening - * sessions. Used if the session_options of the enumerator have - * %GCK_SESSION_LOGIN_USER - */ - g_object_class_install_property (gobject_class, PROP_INTERACTION, - g_param_spec_object ("interaction", "Interaction", "Interaction asking for pins", - G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE)); - - /** - * GckSession::discard-handle: - * @session: The session. - * @handle: The handle being discarded. - * - * When a GckSession is being disposed of it emits this signal to allow - * a session pool to pick up the handle and keep it around. - * - * If no signal handler claims the handle, then it is closed. This is used by - * gck_module_set_pool_sessions() to implement the module session pool. - * - * Returns: Whether or not this handle was claimed. - */ - signals[DISCARD_HANDLE] = g_signal_new ("discard-handle", GCK_TYPE_SESSION, - G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GckSessionClass, discard_handle), - g_signal_accumulator_true_handled, NULL, - _gck_marshal_BOOLEAN__ULONG, G_TYPE_BOOLEAN, 1, G_TYPE_ULONG); - - g_type_class_add_private (klass, sizeof (GckSessionPrivate)); -} - -/* ---------------------------------------------------------------------------- - * PUBLIC - */ - -/** - * GckSessionInfo: - * @slot_id: The handle of the PKCS11 slot that this session is opened on. - * @state: The user login state of the session. - * @flags: Various PKCS11 flags. - * @device_error: The last device error that occurred from an operation on this session. - * - * Information about the session. This is analogous to a CK_SESSION_INFO structure. - * - * When done with this structure, release it using gck_session_info_free(). - */ - -GType -gck_session_info_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckSessionInfo", - (GBoxedCopyFunc)gck_session_info_copy, - (GBoxedFreeFunc)gck_session_info_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_session_info_copy: - * @session_info: a session info structure - * - * Make a new copy of a session info structure. - * - * Returns: (transfer full): a new copy of the session info - */ -GckSessionInfo * -gck_session_info_copy (GckSessionInfo *session_info) -{ - return g_memdup (session_info, sizeof (GckSessionInfo)); -} - -/** - * gck_session_info_free: - * @session_info: Session info to free. - * - * Free the GckSessionInfo structure and all associated memory. - **/ -void -gck_session_info_free (GckSessionInfo *session_info) -{ - if (!session_info) - return; - g_free (session_info); -} - -/** - * gck_session_from_handle: - * @slot: The slot which the session belongs to. - * @session_handle: the raw PKCS\#11 handle of the session - * @options: Session options. Those which are used during opening a session have no effect. - * - * Initialize a GckSession object from a raw PKCS\#11 session handle. - * Usually one would use the gck_slot_open_session() function to - * create a session. - * - * Returns: (transfer full): the new GckSession object - **/ -GckSession * -gck_session_from_handle (GckSlot *slot, - gulong session_handle, - GckSessionOptions options) -{ - GTlsInteraction *interaction; - GckModule *module; - GckSession *session; - - g_return_val_if_fail (GCK_IS_SLOT (slot), NULL); - - module = gck_slot_get_module (slot); - interaction = gck_slot_get_interaction (slot); - - session = g_object_new (GCK_TYPE_SESSION, - "module", module, - "interaction", interaction, - "handle", session_handle, - "slot", slot, - "options", options, - NULL); - - g_object_unref (module); - g_clear_object (&interaction); - - return session; -} - -/** - * gck_session_get_handle: - * @self: The session object. - * - * Get the raw PKCS\#11 session handle from a GckSession object. - * - * Return value: The raw session handle. - **/ -gulong -gck_session_get_handle (GckSession *self) -{ - g_return_val_if_fail (GCK_IS_SESSION (self), (CK_SESSION_HANDLE)-1); - return self->pv->handle; -} - -/** - * gck_session_get_module: - * @self: The session object. - * - * Get the PKCS\#11 module to which this session belongs. - * - * Returns: (transfer full): the module, which should be unreffed after use - **/ -GckModule * -gck_session_get_module (GckSession *self) -{ - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - g_return_val_if_fail (GCK_IS_MODULE (self->pv->module), NULL); - return g_object_ref (self->pv->module); -} - -/** - * gck_session_get_slot: - * @self: The session object. - * - * Get the PKCS\#11 slot to which this session belongs. - * - * Return value: (transfer full): The slot, which should be unreffed after use. - **/ -GckSlot * -gck_session_get_slot (GckSession *self) -{ - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - g_return_val_if_fail (GCK_IS_SLOT (self->pv->slot), NULL); - return g_object_ref (self->pv->slot); -} - -/** - * gck_session_get_info: - * @self: The session object. - * - * Get information about the session. - * - * Returns: (transfer full): the session info. Use the gck_session_info_free() - * to release when done - **/ -GckSessionInfo* -gck_session_get_info (GckSession *self) -{ - GckSessionInfo *sessioninfo; - CK_FUNCTION_LIST_PTR funcs; - CK_SESSION_INFO info; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - g_return_val_if_fail (GCK_IS_MODULE (self->pv->module), NULL); - - g_object_ref (self->pv->module); - - funcs = gck_module_get_functions (self->pv->module); - g_return_val_if_fail (funcs, NULL); - - memset (&info, 0, sizeof (info)); - rv = (funcs->C_GetSessionInfo) (self->pv->handle, &info); - - g_object_unref (self->pv->module); - - if (rv != CKR_OK) { - g_warning ("couldn't get session info: %s", gck_message_from_rv (rv)); - return NULL; - } - - sessioninfo = g_new0 (GckSessionInfo, 1); - sessioninfo->flags = info.flags; - sessioninfo->slot_id = info.slotID; - sessioninfo->state = info.state; - sessioninfo->device_error = info.ulDeviceError; - - return sessioninfo; -} - -/** - * gck_session_get_state: - * @self: the session - * - * Get the session state. The state is the various PKCS\#11 CKS_XXX flags. - * - * Returns: the session state - */ -gulong -gck_session_get_state (GckSession *self) -{ - CK_FUNCTION_LIST_PTR funcs; - CK_SESSION_INFO info; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SESSION (self), 0); - g_return_val_if_fail (GCK_IS_MODULE (self->pv->module), 0); - - g_object_ref (self->pv->module); - - funcs = gck_module_get_functions (self->pv->module); - g_return_val_if_fail (funcs, 0); - - memset (&info, 0, sizeof (info)); - rv = (funcs->C_GetSessionInfo) (self->pv->handle, &info); - - g_object_unref (self->pv->module); - - if (rv != CKR_OK) { - g_warning ("couldn't get session info: %s", gck_message_from_rv (rv)); - return 0; - } - - return info.state; -} - -/** - * gck_session_get_options: - * @self: The session to get options from. - * - * Get the options this session was opened with. - * - * Return value: The session options. - **/ -GckSessionOptions -gck_session_get_options (GckSession *self) -{ - g_return_val_if_fail (GCK_IS_SESSION (self), 0); - return self->pv->options; -} - -/** - * gck_session_get_interaction: - * @self: the session - * - * Get the interaction object set on this session, which is used to prompt - * for pins and the like. - * - * Returns: (transfer full) (allow-none): the interaction object, or %NULL - */ -GTlsInteraction * -gck_session_get_interaction (GckSession *self) -{ - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - - if (self->pv->interaction) - return g_object_ref (self->pv->interaction); - - return NULL; -} - -/* --------------------------------------------------------------------------------------------- - * INIT PIN - */ - -typedef struct _InitPin { - GckArguments 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); -} - -/** - * gck_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. - * @cancellable: Optional cancellation object, or NULL. - * @error: 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 -gck_session_init_pin (GckSession *self, const guchar *pin, gsize n_pin, - GCancellable *cancellable, GError **error) -{ - InitPin args = { GCK_ARGUMENTS_INIT, (guchar*)pin, n_pin }; - return _gck_call_sync (self, perform_init_pin, NULL, &args, cancellable, error); - -} - -/** - * gck_session_init_pin_async: - * @self: Initialize PIN for this session's slot. - * @pin: (allow-none) (array length=n_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 -gck_session_init_pin_async (GckSession *self, const guchar *pin, gsize n_pin, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - InitPin* args = _gck_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; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_init_pin_finish: - * @self: The session. - * @result: The result passed to the callback. - * @error: 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 -gck_session_init_pin_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - return _gck_call_basic_finish (result, error); -} - - -/* --------------------------------------------------------------------------------------------- - * SET PIN - */ - -typedef struct _SetPin { - GckArguments 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); -} - -/** - * gck_session_set_pin: - * @self: Change the PIN for this session's slot. - * @old_pin: (allow-none) (array length=n_old_pin): the user's old PIN, or %NULL - * for protected authentication path. - * @n_old_pin: The length of the PIN. - * @new_pin: (allow-none) (array length=n_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. - * @error: 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 -gck_session_set_pin (GckSession *self, const guchar *old_pin, gsize n_old_pin, - const guchar *new_pin, gsize n_new_pin, GCancellable *cancellable, - GError **error) -{ - SetPin args = { GCK_ARGUMENTS_INIT, (guchar*)old_pin, n_old_pin, (guchar*)new_pin, n_new_pin }; - return _gck_call_sync (self, perform_set_pin, NULL, &args, cancellable, error); -} - -/** - * gck_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 -gck_session_set_pin_async (GckSession *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 = _gck_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; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_set_pin_finish: - * @self: The session. - * @result: The result passed to the callback. - * @error: 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 -gck_session_set_pin_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - return _gck_call_basic_finish (result, error); -} - - -/* --------------------------------------------------------------------------------------------- - * LOGIN - */ - -typedef struct _Login { - GckArguments base; - gulong user_type; - guchar *pin; - gsize n_pin; -} Login; - -static void -free_login (Login *args) -{ - g_free (args->pin); - g_free (args); -} - -static CK_RV -perform_login (Login *args) -{ - return (args->base.pkcs11->C_Login) (args->base.handle, args->user_type, - (CK_BYTE_PTR)args->pin, args->n_pin); -} - -/** - * gck_session_login: - * @self: Log in to this session. - * @user_type: The type of login user. - * @pin: (allow-none) (array length=n_pin): the user's PIN, or %NULL for - * protected authentication path - * @n_pin: The length of the PIN. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error. - * - * Login the user on the session. This call may block for - * an indefinite period. - * - * Return value: Whether successful or not. - **/ -gboolean -gck_session_login (GckSession *self, gulong user_type, const guchar *pin, - gsize n_pin, GCancellable *cancellable, GError **error) -{ - Login args = { GCK_ARGUMENTS_INIT, user_type, (guchar*)pin, n_pin }; - return _gck_call_sync (self, perform_login, NULL, &args, cancellable, error); - -} - -/** - * gck_session_login_async: - * @self: Log in to this session. - * @user_type: The type of login user. - * @pin: (allow-none) (array length=n_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. - * - * Login the user on the session. This call will return - * immediately and completes asynchronously. - **/ -void -gck_session_login_async (GckSession *self, gulong user_type, const guchar *pin, - gsize n_pin, GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - Login* args = _gck_call_async_prep (self, self, perform_login, NULL, sizeof (*args), free_login); - - args->user_type = user_type; - args->pin = pin && n_pin ? g_memdup (pin, n_pin) : NULL; - args->n_pin = n_pin; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); - -} - -/** - * gck_session_login_finish: - * @self: The session logged into. - * @result: The result passed to the callback. - * @error: A location to return an error. - * - * Get the result of a login operation. - * - * Return value: Whether the operation was successful or not. - **/ -gboolean -gck_session_login_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - return _gck_call_basic_finish (result, error); -} - - - - -/* LOGOUT */ - -static CK_RV -perform_logout (GckArguments *args) -{ - return (args->pkcs11->C_Logout) (args->handle); -} - -/** - * gck_session_logout: - * @self: Logout of this session. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error. - * - * Log out of the session. This call may block for an indefinite period. - * - * Return value: Whether the logout was successful or not. - **/ -gboolean -gck_session_logout (GckSession *self, GCancellable *cancellable, GError **error) -{ - GckArguments args = GCK_ARGUMENTS_INIT; - return _gck_call_sync (self, perform_logout, NULL, &args, cancellable, error); -} - -/** - * gck_session_logout_async: - * @self: Logout of this session. - * @cancellable: Optional cancellation object, or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Log out of the session. This call returns immediately and completes - * asynchronously. - **/ -void -gck_session_logout_async (GckSession *self, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckArguments *args = _gck_call_async_prep (self, self, perform_logout, NULL, 0, NULL); - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_logout_finish: - * @self: Logout of this session. - * @result: The result passed to the callback. - * @error: A location to return an error. - * - * Get the result of logging out of a session. - * - * Return value: Whether the logout was successful or not. - **/ -gboolean -gck_session_logout_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - return _gck_call_basic_finish (result, error); -} - - - - -/* CREATE OBJECT */ - -typedef struct _CreateObject { - GckArguments base; - GckAttributes *attrs; - CK_OBJECT_HANDLE object; -} CreateObject; - -static void -free_create_object (CreateObject *args) -{ - gck_attributes_unref (args->attrs); - g_free (args); -} - -static CK_RV -perform_create_object (CreateObject *args) -{ - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - CK_RV rv; - - attrs = _gck_attributes_commit_out (args->attrs, &n_attrs); - - rv = (args->base.pkcs11->C_CreateObject) (args->base.handle, - attrs, n_attrs, - &args->object); - - if (_gck_debugging) { - gchar *string = _gck_attributes_format (args->attrs); - if (rv == CKR_OK) - _gck_debug ("created object: %s", string); - else - _gck_debug ("failed %s to create object: %s", - _gck_stringize_rv (rv), string); - g_free (string); - } - - return rv; -} - -/** - * gck_session_create_object: - * @self: The session to create the object on. - * @attrs: The attributes to create the object with. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Create a new PKCS\#11 object. This call may block for an - * indefinite period. - * - * Returns: (transfer full): the newly created object or %NULL if an error occurred - **/ -GckObject * -gck_session_create_object (GckSession *self, GckAttributes *attrs, - GCancellable *cancellable, GError **error) -{ - CreateObject args = { GCK_ARGUMENTS_INIT, attrs, 0 }; - gboolean ret; - - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - g_return_val_if_fail (attrs != NULL, NULL); - - _gck_attributes_lock (attrs); - ret = _gck_call_sync (self, perform_create_object, NULL, &args, cancellable, error); - _gck_attributes_unlock (attrs); - - if (!ret) - return NULL; - - return gck_object_from_handle (self, args.object); -} - -/** - * gck_session_create_object_async: - * @self: The session to create the object on. - * @attrs: The attributes to create the object with. - * @cancellable: Optional cancellation object or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Create a new PKCS\#11 object. This call will return immediately - * and complete asynchronously. - **/ -void -gck_session_create_object_async (GckSession *self, GckAttributes *attrs, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - CreateObject *args = _gck_call_async_prep (self, self, perform_create_object, - NULL, sizeof (*args), free_create_object); - - g_return_if_fail (attrs); - - args->attrs = gck_attributes_ref (attrs); - _gck_attributes_lock (attrs); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_create_object_finish: - * @self: The session to create the object on. - * @result: The result passed to the callback. - * @error: A location to return an error, or NULL. - * - * Get the result of creating a new PKCS\#11 object. - * - * Return value: (transfer full): the newly created object or NULL if an error occurred - **/ -GckObject * -gck_session_create_object_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - CreateObject *args; - - args = _gck_call_arguments (result, CreateObject); - _gck_attributes_unlock (args->attrs); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - return gck_object_from_handle (self, args->object); -} - - - -/* FIND OBJECTS */ - -typedef struct _FindObjects { - GckArguments base; - GckAttributes *attrs; - CK_OBJECT_HANDLE_PTR objects; - CK_ULONG n_objects; -} FindObjects; - -static void -free_find_objects (FindObjects *args) -{ - gck_attributes_unref (args->attrs); - g_free (args->objects); -} - -static CK_RV -perform_find_objects (FindObjects *args) -{ - CK_OBJECT_HANDLE_PTR batch; - CK_ULONG n_batch, n_found; - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - GArray *array; - CK_RV rv; - - if (_gck_debugging) { - gchar *string = _gck_attributes_format (args->attrs); - _gck_debug ("matching: %s", string); - g_free (string); - } - - attrs = _gck_attributes_commit_out (args->attrs, &n_attrs); - - rv = (args->base.pkcs11->C_FindObjectsInit) (args->base.handle, - attrs, n_attrs); - if (rv != CKR_OK) - return rv; - - batch = NULL; - n_found = n_batch = 4; - array = g_array_new (0, 1, sizeof (CK_OBJECT_HANDLE)); - - do { - /* - * Reallocate and double in size: - * - First time. - * - Each time we found as many as batch - */ - - if (n_found == n_batch) { - n_batch *= 2; - batch = g_realloc (batch, sizeof (CK_OBJECT_HANDLE) * n_batch); - } - - rv = (args->base.pkcs11->C_FindObjects) (args->base.handle, - batch, n_batch, &n_found); - if (rv != CKR_OK) - break; - - g_array_append_vals (array, batch, n_found); - - } while (n_found > 0); - - g_free (batch); - - if (rv == CKR_OK) { - args->n_objects = array->len; - args->objects = (CK_OBJECT_HANDLE_PTR)g_array_free (array, FALSE); - rv = (args->base.pkcs11->C_FindObjectsFinal) (args->base.handle); - } else { - args->objects = NULL; - args->n_objects = 0; - g_array_free (array, TRUE); - } - - return rv; -} - -static GList* -objlist_from_handles (GckSession *self, CK_OBJECT_HANDLE_PTR objects, - CK_ULONG n_objects) -{ - GList *results = NULL; - - while (n_objects > 0) { - results = g_list_prepend (results, - gck_object_from_handle (self, objects[--n_objects])); - } - - return g_list_reverse (results); -} - -/** - * gck_session_find_objects: - * @self: The session to find objects on. - * @attrs: The attributes to match. - * @cancellable: Optional cancellation object or NULL. - * @error: A location to return an error or NULL. - * - * Find the objects matching the passed attributes. This call may - * block for an indefinite period. - * - * Returns: (transfer full) (element-type Gck.Object): a list of the matching - * objects, which may be empty - **/ -GList * -gck_session_find_objects (GckSession *self, GckAttributes *attrs, - GCancellable *cancellable, GError **error) -{ - FindObjects args = { GCK_ARGUMENTS_INIT, attrs, NULL, 0 }; - GList *results = NULL; - - g_return_val_if_fail (attrs, NULL); - _gck_attributes_lock (attrs); - - if (_gck_call_sync (self, perform_find_objects, NULL, &args, cancellable, error)) - results = objlist_from_handles (self, args.objects, args.n_objects); - - g_free (args.objects); - _gck_attributes_unlock (attrs); - return results; -} - -/** - * gck_session_find_objects_async: - * @self: The session to find objects on. - * @attrs: The attributes to match. - * @cancellable: Optional cancellation object or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Find the objects matching the passed attributes. This call will - * return immediately and complete asynchronously. - **/ -void -gck_session_find_objects_async (GckSession *self, GckAttributes *attrs, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - FindObjects *args = _gck_call_async_prep (self, self, perform_find_objects, - NULL, sizeof (*args), free_find_objects); - args->attrs = gck_attributes_ref (attrs); - _gck_attributes_lock (attrs); - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_find_objects_finish: - * @self: The session to find objects on. - * @result: The attributes to match. - * @error: A location to return an error. - * - * Get the result of a find operation. - * - * Returns: (transfer full) (element-type Gck.Object): a list of the matching - * objects, which may be empty - **/ -GList * -gck_session_find_objects_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - FindObjects *args; - - args = _gck_call_arguments (result, FindObjects); - _gck_attributes_unlock (args->attrs); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - return objlist_from_handles (self, args->objects, args->n_objects); -} - -/* ----------------------------------------------------------------------------- - * KEY PAIR GENERATION - */ - -typedef struct _GenerateKeyPair { - GckArguments base; - GckMechanism mechanism; - GckAttributes *public_attrs; - GckAttributes *private_attrs; - CK_OBJECT_HANDLE public_key; - CK_OBJECT_HANDLE private_key; -} GenerateKeyPair; - -static void -free_generate_key_pair (GenerateKeyPair *args) -{ - gck_attributes_unref (args->public_attrs); - gck_attributes_unref (args->private_attrs); - g_free (args); -} - -static CK_RV -perform_generate_key_pair (GenerateKeyPair *args) -{ - CK_ATTRIBUTE_PTR pub_attrs, priv_attrs; - CK_ULONG n_pub_attrs, n_priv_attrs; - - g_assert (sizeof (CK_MECHANISM) == sizeof (GckMechanism)); - - pub_attrs = _gck_attributes_commit_out (args->public_attrs, &n_pub_attrs); - priv_attrs = _gck_attributes_commit_out (args->private_attrs, &n_priv_attrs); - - return (args->base.pkcs11->C_GenerateKeyPair) (args->base.handle, - (CK_MECHANISM_PTR)&(args->mechanism), - pub_attrs, n_pub_attrs, - priv_attrs, n_priv_attrs, - &args->public_key, - &args->private_key); -} - -/** - * gck_session_generate_key_pair: - * @self: The session to use. - * @mech_type: The mechanism type to use for key generation. - * @public_attrs: Additional attributes for the generated public key. - * @private_attrs: Additional attributes for the generated private key. - * @public_key: A location to return the resulting public key. - * @private_key: A location to return the resulting private key. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Generate a new key pair of public and private keys. This call may block for an - * indefinite period. - * - * Return value: TRUE if the operation succeeded. - **/ -gboolean -gck_session_generate_key_pair (GckSession *self, gulong mech_type, - GckAttributes *public_attrs, GckAttributes *private_attrs, - GckObject **public_key, GckObject **private_key, - GCancellable *cancellable, GError **error) -{ - GckMechanism mech = { mech_type, NULL, 0 }; - return gck_session_generate_key_pair_full (self, &mech, public_attrs, private_attrs, public_key, private_key, cancellable, error); -} - -/** - * gck_session_generate_key_pair_full: - * @self: The session to use. - * @mechanism: The mechanism to use for key generation. - * @public_attrs: Additional attributes for the generated public key. - * @private_attrs: Additional attributes for the generated private key. - * @public_key: A location to return the resulting public key. - * @private_key: A location to return the resulting private key. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Generate a new key pair of public and private keys. This call may block for an - * indefinite period. - * - * Return value: TRUE if the operation succeeded. - **/ -gboolean -gck_session_generate_key_pair_full (GckSession *self, GckMechanism *mechanism, - GckAttributes *public_attrs, GckAttributes *private_attrs, - GckObject **public_key, GckObject **private_key, - GCancellable *cancellable, GError **error) -{ - GenerateKeyPair args = { GCK_ARGUMENTS_INIT, GCK_MECHANISM_EMPTY, public_attrs, private_attrs, 0, 0 }; - gboolean ret; - - g_return_val_if_fail (GCK_IS_SESSION (self), FALSE); - g_return_val_if_fail (mechanism, FALSE); - g_return_val_if_fail (public_attrs, FALSE); - g_return_val_if_fail (private_attrs, FALSE); - g_return_val_if_fail (public_key, FALSE); - g_return_val_if_fail (private_key, FALSE); - - /* Shallow copy of the mechanism structure */ - memcpy (&args.mechanism, mechanism, sizeof (args.mechanism)); - - _gck_attributes_lock (public_attrs); - if (public_attrs != private_attrs) - _gck_attributes_lock (private_attrs); - ret = _gck_call_sync (self, perform_generate_key_pair, NULL, &args, cancellable, error); - if (public_attrs != private_attrs) - _gck_attributes_unlock (private_attrs); - _gck_attributes_unlock (public_attrs); - - if (!ret) - return FALSE; - - *public_key = gck_object_from_handle (self, args.public_key); - *private_key = gck_object_from_handle (self, args.private_key); - return TRUE; -} - -/** - * gck_session_generate_key_pair_async: - * @self: The session to use. - * @mechanism: The mechanism to use for key generation. - * @public_attrs: Additional attributes for the generated public key. - * @private_attrs: Additional attributes for the generated private key. - * @cancellable: Optional cancellation object or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Generate a new key pair of public and private keys. This call will - * return immediately and complete asynchronously. - **/ -void -gck_session_generate_key_pair_async (GckSession *self, GckMechanism *mechanism, - GckAttributes *public_attrs, GckAttributes *private_attrs, - GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data) -{ - GenerateKeyPair *args = _gck_call_async_prep (self, self, perform_generate_key_pair, - NULL, sizeof (*args), free_generate_key_pair); - - g_return_if_fail (GCK_IS_SESSION (self)); - g_return_if_fail (mechanism); - g_return_if_fail (public_attrs); - g_return_if_fail (private_attrs); - - /* Shallow copy of the mechanism structure */ - memcpy (&args->mechanism, mechanism, sizeof (args->mechanism)); - - args->public_attrs = gck_attributes_ref (public_attrs); - _gck_attributes_lock (public_attrs); - args->private_attrs = gck_attributes_ref (private_attrs); - if (public_attrs != private_attrs) - _gck_attributes_lock (private_attrs); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_generate_key_pair_finish: - * @self: The session to use. - * @result: The async result passed to the callback. - * @public_key: A location to return the resulting public key. - * @private_key: A location to return the resulting private key. - * @error: A location to return an error. - * - * Get the result of a generate key pair operation. - * - * Return value: TRUE if the operation succeeded. - **/ -gboolean -gck_session_generate_key_pair_finish (GckSession *self, GAsyncResult *result, - GckObject **public_key, GckObject **private_key, - GError **error) -{ - GenerateKeyPair *args; - - g_return_val_if_fail (GCK_IS_SESSION (self), FALSE); - g_return_val_if_fail (public_key, FALSE); - g_return_val_if_fail (private_key, FALSE); - - args = _gck_call_arguments (result, GenerateKeyPair); - _gck_attributes_unlock (args->public_attrs); - if (args->public_attrs != args->private_attrs) - _gck_attributes_unlock (args->private_attrs); - - if (!_gck_call_basic_finish (result, error)) - return FALSE; - - *public_key = gck_object_from_handle (self, args->public_key); - *private_key = gck_object_from_handle (self, args->private_key); - return TRUE; -} - -/* ----------------------------------------------------------------------------- - * KEY WRAPPING - */ - -typedef struct _WrapKey { - GckArguments base; - GckMechanism mechanism; - CK_OBJECT_HANDLE wrapper; - CK_OBJECT_HANDLE wrapped; - gpointer result; - gulong n_result; -} WrapKey; - -static void -free_wrap_key (WrapKey *args) -{ - g_free (args->result); - g_free (args); -} - -static CK_RV -perform_wrap_key (WrapKey *args) -{ - CK_RV rv; - - g_assert (sizeof (CK_MECHANISM) == sizeof (GckMechanism)); - - /* Get the length of the result */ - rv = (args->base.pkcs11->C_WrapKey) (args->base.handle, - (CK_MECHANISM_PTR)&(args->mechanism), - args->wrapper, args->wrapped, - NULL, &args->n_result); - if (rv != CKR_OK) - return rv; - - /* And try again with a real buffer */ - args->result = g_malloc0 (args->n_result); - return (args->base.pkcs11->C_WrapKey) (args->base.handle, - (CK_MECHANISM_PTR)&(args->mechanism), - args->wrapper, args->wrapped, - args->result, &args->n_result); -} - -/** - * gck_session_wrap_key: - * @self: The session to use. - * @wrapper: The key to use for wrapping. - * @mech_type: The mechanism type to use for wrapping. - * @wrapped: The key to wrap. - * @n_result: A location in which to return the length of the wrapped data. - * @cancellable: A #GCancellable or %NULL - * @error: A location to return an error, or NULL. - * - * Wrap a key into a byte stream. This call may block for an - * indefinite period. - * - * Returns: (transfer full) (array length=n_result): the wrapped data or %NULL - * if the operation failed - **/ -guchar * -gck_session_wrap_key (GckSession *self, GckObject *key, gulong mech_type, - GckObject *wrapped, gsize *n_result, GCancellable *cancellable, GError **error) -{ - GckMechanism mech = { mech_type, NULL, 0 }; - return gck_session_wrap_key_full (self, key, &mech, wrapped, n_result, cancellable, error); -} - -/** - * gck_session_wrap_key_full: - * @self: The session to use. - * @wrapper: The key to use for wrapping. - * @mechanism: The mechanism to use for wrapping. - * @wrapped: The key to wrap. - * @n_result: A location in which to return the length of the wrapped data. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Wrap a key into a byte stream. This call may block for an - * indefinite period. - * - * Returns: (transfer full) (array length=n_result): the wrapped data or %NULL - * if the operation failed - **/ -guchar * -gck_session_wrap_key_full (GckSession *self, GckObject *wrapper, GckMechanism *mechanism, - GckObject *wrapped, gsize *n_result, GCancellable *cancellable, - GError **error) -{ - WrapKey args = { GCK_ARGUMENTS_INIT, GCK_MECHANISM_EMPTY, 0, 0, NULL, 0 }; - gboolean ret; - - g_return_val_if_fail (GCK_IS_SESSION (self), FALSE); - g_return_val_if_fail (mechanism, FALSE); - g_return_val_if_fail (GCK_IS_OBJECT (wrapped), FALSE); - g_return_val_if_fail (GCK_IS_OBJECT (wrapper), FALSE); - g_return_val_if_fail (n_result, FALSE); - - /* Shallow copy of the mechanism structure */ - memcpy (&args.mechanism, mechanism, sizeof (args.mechanism)); - - g_object_get (wrapper, "handle", &args.wrapper, NULL); - g_return_val_if_fail (args.wrapper != 0, NULL); - g_object_get (wrapped, "handle", &args.wrapped, NULL); - g_return_val_if_fail (args.wrapped != 0, NULL); - - ret = _gck_call_sync (self, perform_wrap_key, NULL, &args, cancellable, error); - - if (!ret) - return FALSE; - - *n_result = args.n_result; - return args.result; -} - -/** - * gck_session_wrap_key_async: - * @self: The session to use. - * @wrapper: The key to use for wrapping. - * @mechanism: The mechanism to use for wrapping. - * @wrapped: The key to wrap. - * @cancellable: Optional cancellation object or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Wrap a key into a byte stream. This call will - * return immediately and complete asynchronously. - **/ -void -gck_session_wrap_key_async (GckSession *self, GckObject *key, GckMechanism *mechanism, - GckObject *wrapped, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - WrapKey *args = _gck_call_async_prep (self, self, perform_wrap_key, - NULL, sizeof (*args), free_wrap_key); - - g_return_if_fail (GCK_IS_SESSION (self)); - g_return_if_fail (mechanism); - g_return_if_fail (GCK_IS_OBJECT (wrapped)); - g_return_if_fail (GCK_IS_OBJECT (key)); - - /* Shallow copy of the mechanism structure */ - memcpy (&args->mechanism, mechanism, sizeof (args->mechanism)); - - g_object_get (key, "handle", &args->wrapper, NULL); - g_return_if_fail (args->wrapper != 0); - g_object_get (wrapped, "handle", &args->wrapped, NULL); - g_return_if_fail (args->wrapped != 0); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_wrap_key_finish: - * @self: The session to use. - * @result: The async result passed to the callback. - * @n_result: A location in which to return the length of the wrapped data. - * @error: A location to return an error. - * - * Get the result of a wrap key operation. - * - * Returns: (transfer full) (array length=n_result): the wrapped data or %NULL - * if the operation failed - **/ -guchar * -gck_session_wrap_key_finish (GckSession *self, GAsyncResult *result, - gsize *n_result, GError **error) -{ - WrapKey *args; - gpointer ret; - - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - g_return_val_if_fail (n_result, NULL); - - args = _gck_call_arguments (result, WrapKey); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - - *n_result = args->n_result; - args->n_result = 0; - ret = args->result; - args->result = NULL; - - return ret; -} - -/* ----------------------------------------------------------------------------- - * KEY UNWRAPPING - */ - -typedef struct _UnwrapKey { - GckArguments base; - GckMechanism mechanism; - GckAttributes *attrs; - CK_OBJECT_HANDLE wrapper; - gconstpointer input; - gulong n_input; - CK_OBJECT_HANDLE unwrapped; -} UnwrapKey; - -static void -free_unwrap_key (UnwrapKey *args) -{ - gck_attributes_unref (args->attrs); - g_free (args); -} - -static CK_RV -perform_unwrap_key (UnwrapKey *args) -{ - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - - g_assert (sizeof (CK_MECHANISM) == sizeof (GckMechanism)); - - attrs = _gck_attributes_commit_out (args->attrs, &n_attrs); - - return (args->base.pkcs11->C_UnwrapKey) (args->base.handle, - (CK_MECHANISM_PTR)&(args->mechanism), - args->wrapper, (CK_BYTE_PTR)args->input, - args->n_input, attrs, n_attrs, - &args->unwrapped); -} - -/** - * gck_session_unwrap_key: - * @self: The session to use. - * @wrapper: The key to use for unwrapping. - * @mech_type: The mechanism to use for unwrapping. - * @input: (array length=n_input): the wrapped data as a byte stream - * @n_input: The length of the wrapped data. - * @attrs: Additional attributes for the unwrapped key. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Unwrap a key from a byte stream. This call may block for an - * indefinite period. - * - * Returns: (transfer full): the new unwrapped key or NULL if the - * operation failed - **/ -GckObject * -gck_session_unwrap_key (GckSession *self, - GckObject *wrapper, - gulong mech_type, - const guchar *input, - gsize n_input, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error) -{ - GckMechanism mech = { mech_type, NULL, 0 }; - return gck_session_unwrap_key_full (self, wrapper, &mech, input, n_input, attrs, cancellable, error); -} - -/** - * gck_session_unwrap_key_full: - * @self: The session to use. - * @wrapper: The key to use for unwrapping. - * @mechanism: The mechanism to use for unwrapping. - * @input: (array length=n_input): the wrapped data as a byte stream - * @n_input: The length of the wrapped data. - * @attrs: Additional attributes for the unwrapped key. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Unwrap a key from a byte stream. This call may block for an - * indefinite period. - * - * Returns: (transfer full): the new unwrapped key or NULL if the operation - * failed - **/ -GckObject * -gck_session_unwrap_key_full (GckSession *self, - GckObject *wrapper, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error) -{ - UnwrapKey args = { GCK_ARGUMENTS_INIT, GCK_MECHANISM_EMPTY, attrs, 0, input, n_input, 0 }; - gboolean ret; - - g_return_val_if_fail (GCK_IS_SESSION (self), FALSE); - g_return_val_if_fail (GCK_IS_OBJECT (wrapper), FALSE); - g_return_val_if_fail (mechanism, FALSE); - g_return_val_if_fail (attrs, FALSE); - - /* Shallow copy of the mechanism structure */ - memcpy (&args.mechanism, mechanism, sizeof (args.mechanism)); - - g_object_get (wrapper, "handle", &args.wrapper, NULL); - g_return_val_if_fail (args.wrapper != 0, NULL); - - _gck_attributes_lock (attrs); - ret = _gck_call_sync (self, perform_unwrap_key, NULL, &args, cancellable, error); - _gck_attributes_unlock (attrs); - - if (!ret) - return NULL; - - return gck_object_from_handle (self, args.unwrapped); -} - -/** - * gck_session_unwrap_key_async: - * @self: The session to use. - * @wrapper: The key to use for unwrapping. - * @mechanism: The mechanism to use for unwrapping. - * @input: (array length=n_input): the wrapped data as a byte stream - * @n_input: The length of the wrapped data. - * @attrs: Additional attributes for the unwrapped key. - * @cancellable: Optional cancellation object or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Unwrap a key from a byte stream. This call will - * return immediately and complete asynchronously. - **/ -void -gck_session_unwrap_key_async (GckSession *self, - GckObject *wrapper, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) -{ - UnwrapKey *args = _gck_call_async_prep (self, self, perform_unwrap_key, - NULL, sizeof (*args), free_unwrap_key); - - g_return_if_fail (GCK_IS_SESSION (self)); - g_return_if_fail (GCK_IS_OBJECT (wrapper)); - g_return_if_fail (attrs); - - g_object_get (wrapper, "handle", &args->wrapper, NULL); - g_return_if_fail (args->wrapper != 0); - - /* Shallow copy of the mechanism structure */ - memcpy (&args->mechanism, mechanism, sizeof (args->mechanism)); - - args->attrs = gck_attributes_ref (attrs); - args->input = input; - args->n_input = n_input; - _gck_attributes_lock (attrs); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_unwrap_key_finish: - * @self: The session to use. - * @result: The async result passed to the callback. - * @error: A location to return an error. - * - * Get the result of a unwrap key operation. - * - * Returns: (transfer full): the new unwrapped key or %NULL if the operation - * failed. - **/ -GckObject * -gck_session_unwrap_key_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - UnwrapKey *args; - - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - - args = _gck_call_arguments (result, UnwrapKey); - _gck_attributes_unlock (args->attrs); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - return gck_object_from_handle (self, args->unwrapped); -} - -/* ----------------------------------------------------------------------------- - * KEY DERIVATION - */ - -typedef struct _DeriveKey { - GckArguments base; - GckMechanism mechanism; - GckAttributes *attrs; - CK_OBJECT_HANDLE key; - CK_OBJECT_HANDLE derived; -} DeriveKey; - -static void -free_derive_key (DeriveKey *args) -{ - gck_attributes_unref (args->attrs); - g_free (args); -} - -static CK_RV -perform_derive_key (DeriveKey *args) -{ - CK_ATTRIBUTE_PTR attrs; - CK_ULONG n_attrs; - - g_assert (sizeof (CK_MECHANISM) == sizeof (GckMechanism)); - - attrs = _gck_attributes_commit_out (args->attrs, &n_attrs); - - return (args->base.pkcs11->C_DeriveKey) (args->base.handle, - (CK_MECHANISM_PTR)&(args->mechanism), - args->key, attrs, n_attrs, - &args->derived); -} - -/** - * gck_session_derive_key: - * @self: The session to use. - * @base: The key to derive from. - * @mech_type: The mechanism to use for derivation. - * @attrs: Additional attributes for the derived key. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Derive a key from another key. This call may block for an - * indefinite period. - * - * Returns: (transfer full): the new derived key or NULL if the operation - * failed - **/ -GckObject * -gck_session_derive_key (GckSession *self, GckObject *base, gulong mech_type, - GckAttributes *attrs, GCancellable *cancellable, GError **error) -{ - GckMechanism mech = { mech_type, NULL, 0 }; - return gck_session_derive_key_full (self, base, &mech, attrs, cancellable, error); -} - -/** - * gck_session_derive_key_full: - * @self: The session to use. - * @base: The key to derive from. - * @mechanism: The mechanism to use for derivation. - * @attrs: Additional attributes for the derived key. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Derive a key from another key. This call may block for an - * indefinite period. - * - * Returns: (transfer full): the new derived key or NULL if the operation - * failed - **/ -GckObject* -gck_session_derive_key_full (GckSession *self, GckObject *base, GckMechanism *mechanism, - GckAttributes *attrs, GCancellable *cancellable, GError **error) -{ - DeriveKey args = { GCK_ARGUMENTS_INIT, GCK_MECHANISM_EMPTY, attrs, 0, 0 }; - gboolean ret; - - g_return_val_if_fail (GCK_IS_SESSION (self), FALSE); - g_return_val_if_fail (GCK_IS_OBJECT (base), FALSE); - g_return_val_if_fail (mechanism, FALSE); - g_return_val_if_fail (attrs, FALSE); - - /* Shallow copy of the mechanism structure */ - memcpy (&args.mechanism, mechanism, sizeof (args.mechanism)); - - g_object_get (base, "handle", &args.key, NULL); - g_return_val_if_fail (args.key != 0, NULL); - - _gck_attributes_lock (attrs); - ret = _gck_call_sync (self, perform_derive_key, NULL, &args, cancellable, error); - _gck_attributes_unlock (attrs); - - if (!ret) - return NULL; - - return gck_object_from_handle (self, args.derived); -} - -/** - * gck_session_derive_key_async: - * @self: The session to use. - * @base: The key to derive from. - * @mechanism: The mechanism to use for derivation. - * @attrs: Additional attributes for the derived key. - * @cancellable: Optional cancellation object or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Derive a key from another key. This call will - * return immediately and complete asynchronously. - **/ -void -gck_session_derive_key_async (GckSession *self, GckObject *base, GckMechanism *mechanism, - GckAttributes *attrs, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - DeriveKey *args = _gck_call_async_prep (self, self, perform_derive_key, - NULL, sizeof (*args), free_derive_key); - - g_return_if_fail (GCK_IS_SESSION (self)); - g_return_if_fail (GCK_IS_OBJECT (base)); - g_return_if_fail (attrs); - - g_object_get (base, "handle", &args->key, NULL); - g_return_if_fail (args->key != 0); - - /* Shallow copy of the mechanism structure */ - memcpy (&args->mechanism, mechanism, sizeof (args->mechanism)); - - args->attrs = gck_attributes_ref (attrs); - _gck_attributes_lock (attrs); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_derive_key_finish: - * @self: The session to use. - * @result: The async result passed to the callback. - * @error: A location to return an error. - * - * Get the result of a derive key operation. - * - * Returns: (transfer full): the new derived key or %NULL if the operation - * failed - **/ -GckObject * -gck_session_derive_key_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - DeriveKey *args; - - g_return_val_if_fail (GCK_IS_SESSION (self), NULL); - - args = _gck_call_arguments (result, DeriveKey); - _gck_attributes_unlock (args->attrs); - - if (!_gck_call_basic_finish (result, error)) - return NULL; - - return gck_object_from_handle (self, args->derived); -} - -/* -------------------------------------------------------------------------------------------------- - * COMMON CRYPTO ROUTINES - */ - -typedef struct _Crypt { - GckArguments base; - - /* Functions to call */ - CK_C_EncryptInit init_func; - CK_C_Encrypt complete_func; - - /* Interaction */ - GckObject *key_object; - GTlsInteraction *interaction; - - /* Input */ - CK_OBJECT_HANDLE key; - GckMechanism mechanism; - guchar *input; - CK_ULONG n_input; - - /* Output */ - guchar *result; - CK_ULONG n_result; - -} Crypt; - -static CK_RV -perform_crypt (Crypt *args) -{ - GTlsInteraction *interaction; - CK_RV rv; - - g_assert (args); - g_assert (args->init_func); - g_assert (args->complete_func); - g_assert (!args->result); - g_assert (!args->n_result); - - /* Initialize the crypt operation */ - rv = (args->init_func) (args->base.handle, (CK_MECHANISM_PTR)&(args->mechanism), args->key); - if (rv != CKR_OK) - return rv; - - /* Compatibility, hook into GckModule signals if no interaction set */ - if (args->interaction) - interaction = g_object_ref (args->interaction); - else - interaction = _gck_interaction_new (args->key_object); - - rv = _gck_session_authenticate_key (args->base.pkcs11, args->base.handle, - args->key_object, interaction, NULL); - - g_object_unref (interaction); - - if (rv != CKR_OK) - return rv; - - /* Get the length of the result */ - rv = (args->complete_func) (args->base.handle, args->input, args->n_input, NULL, &args->n_result); - if (rv != CKR_OK) - return rv; - - /* And try again with a real buffer */ - args->result = g_malloc0 (args->n_result); - return (args->complete_func) (args->base.handle, args->input, args->n_input, args->result, &args->n_result); -} - -static void -free_crypt (Crypt *args) -{ - g_clear_object (&args->interaction); - g_clear_object (&args->key_object); - - g_free (args->input); - g_free (args->result); - g_free (args); -} - -static guchar* -crypt_sync (GckSession *self, GckObject *key, GckMechanism *mechanism, const guchar *input, - gsize n_input, gsize *n_result, GCancellable *cancellable, GError **error, - CK_C_EncryptInit init_func, CK_C_Encrypt complete_func) -{ - Crypt args; - - g_return_val_if_fail (GCK_IS_OBJECT (key), NULL); - g_return_val_if_fail (mechanism, NULL); - g_return_val_if_fail (init_func, NULL); - g_return_val_if_fail (complete_func, NULL); - - memset (&args, 0, sizeof (args)); - g_object_get (key, "handle", &args.key, NULL); - g_return_val_if_fail (args.key != 0, NULL); - - /* Shallow copy of the mechanism structure */ - memcpy (&args.mechanism, mechanism, sizeof (args.mechanism)); - - /* No need to copy in this case */ - args.input = (guchar*)input; - args.n_input = n_input; - - args.init_func = init_func; - args.complete_func = complete_func; - - args.key_object = key; - args.interaction = self->pv->interaction; - - if (!_gck_call_sync (self, perform_crypt, NULL, &args, cancellable, error)) { - g_free (args.result); - return NULL; - } - - *n_result = args.n_result; - return args.result; -} - -static void -crypt_async (GckSession *self, GckObject *key, GckMechanism *mechanism, const guchar *input, - gsize n_input, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data, - CK_C_EncryptInit init_func, CK_C_Encrypt complete_func) -{ - Crypt *args = _gck_call_async_prep (self, self, perform_crypt, NULL, sizeof (*args), free_crypt); - - g_return_if_fail (GCK_IS_OBJECT (key)); - g_return_if_fail (mechanism); - g_return_if_fail (init_func); - g_return_if_fail (complete_func); - - g_object_get (key, "handle", &args->key, NULL); - g_return_if_fail (args->key != 0); - - /* Shallow copy of the mechanism structure */ - memcpy (&args->mechanism, mechanism, sizeof (args->mechanism)); - - args->input = input && n_input ? g_memdup (input, n_input) : NULL; - args->n_input = n_input; - - args->init_func = init_func; - args->complete_func = complete_func; - - args->key_object = g_object_ref (key); - args->interaction = gck_session_get_interaction (self); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -static guchar* -crypt_finish (GckSession *self, GAsyncResult *result, gsize *n_result, GError **error) -{ - Crypt *args; - guchar *res; - - if (!_gck_call_basic_finish (result, error)) - return NULL; - args = _gck_call_arguments (result, Crypt); - - /* Steal the values from the results */ - res = args->result; - args->result = NULL; - *n_result = args->n_result; - args->n_result = 0; - - return res; -} - -/* -------------------------------------------------------------------------------------------------- - * ENCRYPT - */ - -/** - * gck_session_encrypt: - * @self: The session. - * @key: The key to encrypt with. - * @mech_type: The mechanism type to use for encryption. - * @input: The data to encrypt. - * @n_input: The length of the data to encrypt. - * @n_result: A location to store the length of the result data. - * @cancellable: Optional cancellation object, or %NULL - * @error: A location to place error information. - * - * Encrypt data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: (transfer full) (array length=n_result): the data that was encrypted, - * or %NULL if an error occured. - */ -guchar * -gck_session_encrypt (GckSession *self, GckObject *key, gulong mech_type, const guchar *input, - gsize n_input, gsize *n_result, GCancellable *cancellable, GError **error) -{ - GckMechanism mechanism = { mech_type, NULL, 0 }; - return gck_session_encrypt_full (self, key, &mechanism, input, n_input, n_result, cancellable, error); -} - -/** - * gck_session_encrypt_full: - * @self: The session. - * @key: The key to encrypt with. - * @mechanism: The mechanism type and parameters to use for encryption. - * @input: The data to encrypt. - * @n_input: The length of the data to encrypt. - * @n_result: A location to store the length of the result data. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @error: A location to place error information. - * - * Encrypt data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: (transfer full) (array length=n_result): the data that was encrypted, - * or %NULL if an error occured - */ -guchar * -gck_session_encrypt_full (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, gsize *n_result, - GCancellable *cancellable, GError **error) -{ - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - guchar *ret; - - g_object_get (self, "module", &module, NULL); - g_return_val_if_fail (module != NULL, NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (module != NULL, NULL); - - ret = crypt_sync (self, key, mechanism, input, n_input, n_result, cancellable, error, - funcs->C_EncryptInit, funcs->C_Encrypt); - - g_object_unref (module); - return ret; -} - -/** - * gck_session_encrypt_async: - * @self: The session. - * @key: The key to encrypt with. - * @mechanism: The mechanism type and parameters to use for encryption. - * @input: The data to encrypt. - * @n_input: The length of the data to encrypt. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @callback: Called when the operation completes. - * @user_data: A pointer to pass to the callback. - * - * Encrypt data in a mechanism specific manner. This call will - * return immediately and complete asynchronously. - **/ -void -gck_session_encrypt_async (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - - g_object_get (self, "module", &module, NULL); - g_return_if_fail (module != NULL); - - funcs = gck_module_get_functions (module); - g_return_if_fail (module != NULL); - - crypt_async (self, key, mechanism, input, n_input, cancellable, callback, user_data, - funcs->C_EncryptInit, funcs->C_Encrypt); - - g_object_unref (module); -} - -/** - * gck_session_encrypt_finish: - * @self: The session. - * @result: The result object passed to the callback. - * @n_result: A location to store the length of the result data. - * @error: A location to place error information. - * - * Get the result of an encryption operation. - * - * Returns: (transfer full) (array length=n_result): the data that was encrypted, - * or %NULL if an error occurred. - */ -guchar* -gck_session_encrypt_finish (GckSession *self, GAsyncResult *result, gsize *n_result, - GError **error) -{ - return crypt_finish (self, result, n_result, error); -} - -/* -------------------------------------------------------------------------------------------------- - * DECRYPT - */ - -/** - * gck_session_decrypt: - * @self: The session. - * @key: The key to decrypt with. - * @mech_type: The mechanism type to use for decryption. - * @input: The data to decrypt. - * @n_input: The length of the data to decrypt. - * @n_result: A location to store the length of the result data. - * @cancellable: Optional cancellation object, or %NULL - * @error: A location to place an error. - * - * Decrypt data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: (transfer full) (array length=n_result): the data that was decrypted, - * or NULL if an error occured - */ -guchar * -gck_session_decrypt (GckSession *self, GckObject *key, gulong mech_type, const guchar *input, - gsize n_input, gsize *n_result, GCancellable *cancellable, GError **error) -{ - GckMechanism mechanism = { mech_type, NULL, 0 }; - return gck_session_decrypt_full (self, key, &mechanism, input, n_input, n_result, cancellable, error); -} - -/** - * gck_session_decrypt_full: - * @self: The session. - * @key: The key to decrypt with. - * @mechanism: The mechanism type and parameters to use for decryption. - * @input: The data to decrypt. - * @n_input: The length of the data to decrypt. - * @n_result: A location to store the length of the result data. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @error: A location to place error information. - * - * Decrypt data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: (transfer full) (array length=n_result): the data that was decrypted, - * or %NULL if an error occured - */ -guchar * -gck_session_decrypt_full (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, gsize *n_result, - GCancellable *cancellable, GError **error) -{ - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - guchar *ret; - - g_object_get (self, "module", &module, NULL); - g_return_val_if_fail (module != NULL, NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (module != NULL, NULL); - - ret = crypt_sync (self, key, mechanism, input, n_input, n_result, cancellable, error, - funcs->C_DecryptInit, funcs->C_Decrypt); - g_object_unref (module); - return ret; -} - -/** - * gck_session_decrypt_async: - * @self: The session. - * @key: The key to decrypt with. - * @mechanism: The mechanism type and parameters to use for decryption. - * @input: The data to decrypt. - * @n_input: The length of the data to decrypt. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @callback: Called when the operation completes. - * @user_data: A pointer to pass to the callback. - * - * Decrypt data in a mechanism specific manner. This call will - * return immediately and complete asynchronously. - */ -void -gck_session_decrypt_async (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - - g_object_get (self, "module", &module, NULL); - g_return_if_fail (module != NULL); - - funcs = gck_module_get_functions (module); - g_return_if_fail (module != NULL); - - crypt_async (self, key, mechanism, input, n_input, cancellable, callback, user_data, - funcs->C_DecryptInit, funcs->C_Decrypt); - g_object_unref (module); -} - -/** - * gck_session_decrypt_finish: - * @self: The session. - * @result: The result object passed to the callback. - * @n_result: A location to store the length of the result data. - * @error: A location to place error information. - * - * Get the result of an decryption operation. - * - * Returns: (transfer full) (array length=n_result): the data that was decrypted, - * or NULL if an error occurred - */ -guchar* -gck_session_decrypt_finish (GckSession *self, GAsyncResult *result, - gsize *n_result, GError **error) -{ - return crypt_finish (self, result, n_result, error); -} - -/* -------------------------------------------------------------------------------------------------- - * SIGN - */ - -/** - * gck_session_sign: - * @self: The session. - * @key: The key to sign with. - * @mech_type: The mechanism type to use for signing. - * @input: The data to sign. - * @n_input: The length of the data to sign. - * @n_result: A location to store the length of the result data. - * @cancellable: Optional cancellation object, or %NULL - * @error: A location to place an error. - * - * Sign data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: (transfer full) (array length=n_result): the data that was signed, - * or %NULL if an error occured - */ -guchar * -gck_session_sign (GckSession *self, GckObject *key, gulong mech_type, const guchar *input, - gsize n_input, gsize *n_result, GCancellable *cancellable, GError **error) -{ - GckMechanism mechanism = { mech_type, NULL, 0 }; - return gck_session_sign_full (self, key, &mechanism, input, n_input, n_result, NULL, error); -} - -/** - * gck_session_sign_full: - * @self: The session. - * @key: The key to sign with. - * @mechanism: The mechanism type and parameters to use for signing. - * @input: The data to sign. - * @n_input: The length of the data to sign. - * @n_result: A location to store the length of the result data. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @error: A location to place error information. - * - * Sign data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: The data that was signed, or NULL if an error occured. - */ -guchar* -gck_session_sign_full (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, gsize *n_result, - GCancellable *cancellable, GError **error) -{ - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - guchar *ret; - - g_object_get (self, "module", &module, NULL); - g_return_val_if_fail (module != NULL, NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (module != NULL, NULL); - - ret = crypt_sync (self, key, mechanism, input, n_input, n_result, cancellable, error, - funcs->C_SignInit, funcs->C_Sign); - g_object_unref (module); - return ret; -} - -/** - * gck_session_sign_async: - * @self: The session. - * @key: The key to sign with. - * @mechanism: The mechanism type and parameters to use for signing. - * @input: The data to sign. - * @n_input: The length of the data to sign. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @callback: Called when the operation completes. - * @user_data: A pointer to pass to the callback. - * - * Sign data in a mechanism specific manner. This call will - * return immediately and complete asynchronously. - */ -void -gck_session_sign_async (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - - g_object_get (self, "module", &module, NULL); - g_return_if_fail (module != NULL); - - funcs = gck_module_get_functions (module); - g_return_if_fail (module != NULL); - - crypt_async (self, key, mechanism, input, n_input, cancellable, callback, user_data, - funcs->C_SignInit, funcs->C_Sign); - g_object_unref (module); -} - -/** - * gck_session_sign_finish: - * @self: The session. - * @result: The result object passed to the callback. - * @n_result: A location to store the length of the result data. - * @error: A location to place error information. - * - * Get the result of an signing operation. - * - * Returns: (transfer full) (array length=n_result): the data that was signed, - * or %NULL if an error occurred - */ -guchar * -gck_session_sign_finish (GckSession *self, GAsyncResult *result, - gsize *n_result, GError **error) -{ - return crypt_finish (self, result, n_result, error); -} - -/* -------------------------------------------------------------------------------------------------- - * VERIFY - */ - -typedef struct _Verify { - GckArguments base; - - /* Interaction */ - GckObject *key_object; - GTlsInteraction *interaction; - - /* Input */ - CK_OBJECT_HANDLE key; - GckMechanism mechanism; - guchar *input; - CK_ULONG n_input; - guchar *signature; - CK_ULONG n_signature; - -} Verify; - -static CK_RV -perform_verify (Verify *args) -{ - GTlsInteraction *interaction; - CK_RV rv; - - /* Initialize the crypt operation */ - rv = (args->base.pkcs11->C_VerifyInit) (args->base.handle, (CK_MECHANISM_PTR)&(args->mechanism), args->key); - if (rv != CKR_OK) - return rv; - - /* Compatibility, hook into GckModule signals if no interaction set */ - if (args->interaction) - interaction = g_object_ref (args->interaction); - else - interaction = _gck_interaction_new (args->key_object); - - - rv = _gck_session_authenticate_key (args->base.pkcs11, args->base.handle, - args->key_object, interaction, NULL); - - g_object_unref (interaction); - - if (rv != CKR_OK) - return rv; - - /* Do the actual verify */ - return (args->base.pkcs11->C_Verify) (args->base.handle, args->input, args->n_input, - args->signature, args->n_signature); -} - -static void -free_verify (Verify *args) -{ - g_clear_object (&args->interaction); - g_clear_object (&args->key_object); - - g_free (args->input); - g_free (args->signature); - g_free (args); -} - -/** - * gck_session_verify: - * @self: The session. - * @key: The key to verify with. - * @mech_type: The mechanism type to use for verifying. - * @input: The data to verify. - * @n_input: The length of the data to verify. - * @signature: The signature. - * @n_signature: The length of the signature. - * @cancellable: Optional cancellation object, or %NULL - * @error: A location to place an error. - * - * Verify data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: TRUE if the data verified correctly, otherwise a failure or error occurred. - */ -gboolean -gck_session_verify (GckSession *self, GckObject *key, gulong mech_type, const guchar *input, - gsize n_input, const guchar *signature, gsize n_signature, GCancellable *cancellable, GError **error) -{ - GckMechanism mechanism = { mech_type, NULL, 0 }; - return gck_session_verify_full (self, key, &mechanism, input, n_input, - signature, n_signature, NULL, error); -} - -/** - * gck_session_verify_full: - * @self: The session. - * @key: The key to verify with. - * @mechanism: The mechanism type and parameters to use for signing. - * @input: The data to verify. - * @n_input: The length of the data to verify. - * @signature: The signature. - * @n_signature: The length of the signature. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @error: A location to place an error. - * - * Verify data in a mechanism specific manner. This call may - * block for an indefinite period. - * - * Returns: TRUE if the data verified correctly, otherwise a failure or error occurred. - */ -gboolean -gck_session_verify_full (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, const guchar *signature, - gsize n_signature, GCancellable *cancellable, GError **error) -{ - Verify args; - - g_return_val_if_fail (GCK_IS_OBJECT (key), FALSE); - g_return_val_if_fail (mechanism, FALSE); - - memset (&args, 0, sizeof (args)); - g_object_get (key, "handle", &args.key, NULL); - g_return_val_if_fail (args.key != 0, FALSE); - - /* Shallow copy of the mechanism structure */ - memcpy (&args.mechanism, mechanism, sizeof (args.mechanism)); - - /* No need to copy in this case */ - args.input = (guchar*)input; - args.n_input = n_input; - args.signature = (guchar*)signature; - args.n_signature = n_signature; - - args.key_object = key; - args.interaction = self->pv->interaction; - - return _gck_call_sync (self, perform_verify, NULL, &args, cancellable, error); -} - -/** - * gck_session_verify_async: - * @self: The session. - * @key: The key to verify with. - * @mechanism: The mechanism type and parameters to use for signing. - * @input: The data to verify. - * @n_input: The length of the data to verify. - * @signature: The signature. - * @n_signature: The length of the signature. - * @cancellable: A GCancellable which can be used to cancel the operation. - * @callback: Called when the operation completes. - * @user_data: A pointer to pass to the callback. - * - * Verify data in a mechanism specific manner. This call returns - * immediately and completes asynchronously. - */ -void -gck_session_verify_async (GckSession *self, GckObject *key, GckMechanism *mechanism, - const guchar *input, gsize n_input, const guchar *signature, - gsize n_signature, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - Verify *args = _gck_call_async_prep (self, self, perform_verify, NULL, sizeof (*args), free_verify); - - g_return_if_fail (GCK_IS_OBJECT (key)); - g_return_if_fail (mechanism); - - g_object_get (key, "handle", &args->key, NULL); - g_return_if_fail (args->key != 0); - - /* Shallow copy of the mechanism structure */ - memcpy (&args->mechanism, mechanism, sizeof (args->mechanism)); - - args->input = input && n_input ? g_memdup (input, n_input) : NULL; - args->n_input = n_input; - args->signature = signature && n_signature ? g_memdup (signature, n_signature) : NULL; - args->n_signature = n_signature; - - args->key_object = g_object_ref (key); - args->interaction = gck_session_get_interaction (self); - - _gck_call_async_ready_go (args, cancellable, callback, user_data); -} - -/** - * gck_session_verify_finish: - * @self: The session. - * @result: The result object passed to the callback. - * @error: A location to place error information. - * - * Get the result of an verify operation. - * - * Returns: TRUE if the data verified correctly, otherwise a failure or error occurred. - */ -gboolean -gck_session_verify_finish (GckSession *self, GAsyncResult *result, GError **error) -{ - return _gck_call_basic_finish (result, error); -} - -static void -update_password_for_token (GTlsPassword *password, - CK_TOKEN_INFO *token_info, - gboolean request_retry) -{ - GTlsPasswordFlags flags; - gchar *label; - - label = gck_string_from_chars (token_info->label, sizeof (token_info->label)); - g_tls_password_set_description (password, label); - g_free (label); - - flags = 0; - if (request_retry) - flags |= G_TLS_PASSWORD_RETRY; - if (token_info && token_info->flags & CKF_USER_PIN_COUNT_LOW) - flags |= G_TLS_PASSWORD_MANY_TRIES; - if (token_info && token_info->flags & CKF_USER_PIN_FINAL_TRY) - flags |= G_TLS_PASSWORD_FINAL_TRY; - g_tls_password_set_flags (password, flags); -} - -CK_RV -_gck_session_authenticate_token (CK_FUNCTION_LIST_PTR funcs, - CK_SESSION_HANDLE session, - GckSlot *token, - GTlsInteraction *interaction, - GCancellable *cancellable) -{ - CK_SESSION_INFO session_info; - GTlsPassword *password = NULL; - CK_TOKEN_INFO token_info; - GTlsInteractionResult res; - gboolean request_retry; - CK_SLOT_ID slot_id; - CK_BYTE_PTR pin; - CK_ULONG n_pin; - CK_RV rv = CKR_OK; - GError *error = NULL; - - g_assert (funcs != NULL); - g_assert (GCK_IS_SLOT (token)); - - slot_id = gck_slot_get_handle (token); - request_retry = FALSE; - - do { - if (g_cancellable_is_cancelled (cancellable)) { - rv = CKR_FUNCTION_CANCELED; - break; - } - - rv = (funcs->C_GetTokenInfo) (slot_id, &token_info); - if (rv != CKR_OK) { - g_warning ("couldn't get token info when logging in: %s", - gck_message_from_rv (rv)); - break; - } - - /* No login necessary? */ - if ((token_info.flags & CKF_LOGIN_REQUIRED) == 0) { - _gck_debug ("no login required for token, skipping login"); - rv = CKR_OK; - break; - } - - /* Next check if session is logged in? */ - rv = (funcs->C_GetSessionInfo) (session, &session_info); - if (rv != CKR_OK) { - g_warning ("couldn't get session info when logging in: %s", - gck_message_from_rv (rv)); - break; - } - - /* Already logged in? */ - if (session_info.state == CKS_RW_USER_FUNCTIONS || - session_info.state == CKS_RO_USER_FUNCTIONS || - session_info.state == CKS_RW_SO_FUNCTIONS) { - _gck_debug ("already logged in, skipping login"); - rv = CKR_OK; - break; - } - - if (token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) { - _gck_debug ("trying to log into session: protected authentication path, no password"); - - /* No password passed for PAP */ - pin = NULL; - n_pin = 0; - - - /* Not protected auth path */ - } else { - _gck_debug ("trying to log into session: want password %s", - request_retry ? "login was incorrect" : ""); - - if (password == NULL) - password = g_object_new (GCK_TYPE_PASSWORD, "token", token, NULL); - - update_password_for_token (password, &token_info, request_retry); - - if (interaction == NULL) - res = G_TLS_INTERACTION_UNHANDLED; - - else - res = g_tls_interaction_invoke_ask_password (interaction, - G_TLS_PASSWORD (password), - NULL, &error); - - if (res == G_TLS_INTERACTION_FAILED) { - g_message ("interaction couldn't ask password: %s", error->message); - rv = _gck_rv_from_error (error, CKR_USER_NOT_LOGGED_IN); - g_clear_error (&error); - break; - - } else if (res == G_TLS_INTERACTION_UNHANDLED) { - g_message ("couldn't authenticate: no interaction handler"); - rv = CKR_USER_NOT_LOGGED_IN; - break; - } - - pin = (CK_BYTE_PTR)g_tls_password_get_value (password, &n_pin); - } - - /* Try to log in */ - rv = (funcs->C_Login) (session, CKU_USER, (CK_BYTE_PTR)pin, n_pin); - - /* Only one C_Login call if protected auth path */ - if (token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) - break; - - request_retry = TRUE; - } while (rv == CKR_PIN_INCORRECT); - - g_clear_object (&password); - - return rv; -} - -static void -update_password_for_key (GTlsPassword *password, - CK_TOKEN_INFO *token_info, - gboolean request_retry) -{ - GTlsPasswordFlags flags; - - flags = 0; - if (request_retry) - flags |= G_TLS_PASSWORD_RETRY; - if (token_info && token_info->flags & CKF_USER_PIN_COUNT_LOW) - flags |= G_TLS_PASSWORD_MANY_TRIES; - if (token_info && token_info->flags & CKF_USER_PIN_FINAL_TRY) - flags |= G_TLS_PASSWORD_FINAL_TRY; - g_tls_password_set_flags (password, flags); -} - -CK_RV -_gck_session_authenticate_key (CK_FUNCTION_LIST_PTR funcs, - CK_SESSION_HANDLE session, - GckObject *key, - GTlsInteraction *interaction, - GCancellable *cancellable) -{ - CK_ATTRIBUTE attrs[2]; - CK_SESSION_INFO session_info; - CK_TOKEN_INFO token_info; - GTlsPassword *password = NULL; - CK_OBJECT_HANDLE handle; - GTlsInteractionResult res; - gboolean request_retry; - GError *error = NULL; - CK_BYTE_PTR pin; - gsize pin_len; - CK_BBOOL bvalue; - gboolean got_label; - CK_RV rv; - - g_assert (funcs != NULL); - - handle = gck_object_get_handle (key); - - attrs[0].type = CKA_LABEL; - attrs[0].pValue = NULL; - attrs[0].ulValueLen = 0; - attrs[1].type = CKA_ALWAYS_AUTHENTICATE; - attrs[1].pValue = &bvalue; - attrs[1].ulValueLen = sizeof (bvalue); - - rv = (funcs->C_GetAttributeValue) (session, handle, attrs, 2); - if (rv == CKR_ATTRIBUTE_TYPE_INVALID) { - bvalue = CK_FALSE; - - } else if (rv != CKR_OK) { - g_message ("couldn't check whether key requires authentication, assuming it doesn't: %s", - gck_message_from_rv (rv)); - return CKR_OK; - } - - /* No authentication needed, on this object */ - if (bvalue != CK_TRUE) { - _gck_debug ("key does not require authentication"); - return CKR_OK; - } - - got_label = FALSE; - request_retry = FALSE; - - do { - if (g_cancellable_is_cancelled (cancellable)) { - rv = CKR_FUNCTION_CANCELED; - break; - } - - rv = (funcs->C_GetSessionInfo) (session, &session_info); - if (rv != CKR_OK) { - g_warning ("couldn't get session info when authenticating key: %s", - gck_message_from_rv (rv)); - return rv; - } - - rv = (funcs->C_GetTokenInfo) (session_info.slotID, &token_info); - if (rv != CKR_OK) { - g_warning ("couldn't get token info when authenticating key: %s", - gck_message_from_rv (rv)); - return rv; - } - - /* Protected authentication path, just use NULL passwords */ - if (token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) { - - password = NULL; - pin = NULL; - pin_len = 0; - - /* Need to prompt for a password */ - } else { - _gck_debug ("trying to log into session: want password %s", - request_retry ? "login was incorrect" : ""); - - if (password == NULL) - password = g_object_new (GCK_TYPE_PASSWORD, "key", key, NULL); - - /* Set the password */ - update_password_for_key (password, &token_info, request_retry); - - /* Set the label properly */ - if (!got_label) { - if (attrs[0].ulValueLen && attrs[0].ulValueLen != GCK_INVALID) { - attrs[0].pValue = g_malloc0 (attrs[0].ulValueLen + 1); - rv = (funcs->C_GetAttributeValue) (session, handle, attrs, 1); - if (rv == CKR_OK) { - ((gchar *)attrs[0].pValue)[attrs[0].ulValueLen] = 0; - g_tls_password_set_description (password, attrs[0].pValue); - } - g_free (attrs[0].pValue); - attrs[0].pValue = NULL; - } - - got_label = TRUE; - } - - if (interaction == NULL) - res = G_TLS_INTERACTION_UNHANDLED; - - else - res = g_tls_interaction_invoke_ask_password (interaction, - G_TLS_PASSWORD (password), - NULL, &error); - - if (res == G_TLS_INTERACTION_FAILED) { - g_message ("interaction couldn't ask password: %s", error->message); - rv = _gck_rv_from_error (error, CKR_USER_NOT_LOGGED_IN); - g_clear_error (&error); - break; - - } else if (res == G_TLS_INTERACTION_UNHANDLED) { - g_message ("couldn't authenticate: no interaction handler"); - rv = CKR_USER_NOT_LOGGED_IN; - break; - } - - pin = (CK_BYTE_PTR)g_tls_password_get_value (G_TLS_PASSWORD (password), &pin_len); - } - - /* Try to log in */ - rv = (funcs->C_Login) (session, CKU_CONTEXT_SPECIFIC, pin, pin_len); - - /* Only one C_Login call if protected auth path */ - if (token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) - break; - - request_retry = TRUE; - } while (rv == CKR_PIN_INCORRECT); - - g_clear_object (&password); - - return rv; -} diff --git a/gck/gck-slot.c b/gck/gck-slot.c deleted file mode 100644 index 2be6a83a..00000000 --- a/gck/gck-slot.c +++ /dev/null @@ -1,1302 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-slot.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" - -#include "egg/egg-timegm.h" - -#include <string.h> - -/** - * SECTION:gck-slot - * @title: GckSlot - * @short_description: Represents a PKCS\#11 slot that can contain a token. - * - * A PKCS11 slot can contain a token. As an example, a slot might be a card reader, and the token - * the card. If the PKCS\#11 module is not a hardware driver, often the slot and token are equivalent. - */ - -/** - * GckSlot: - * @parent: derived from this. - * - * Represents a PKCS11 slot. - */ - -enum { - PROP_0, - PROP_MODULE, - PROP_HANDLE, - PROP_INTERACTION -}; - -struct _GckSlotPrivate { - GckModule *module; - CK_SLOT_ID handle; - - /* Changable data locked by mutex */ - GMutex *mutex; - GTlsInteraction *interaction; -}; - -G_DEFINE_TYPE (GckSlot, gck_slot, G_TYPE_OBJECT); - -/* ---------------------------------------------------------------------------- - * HELPERS - */ - -static GckSession* -make_session_object (GckSlot *self, - GckSessionOptions options, - CK_SESSION_HANDLE handle) -{ - GckSession *session; - GckModule *module; - - g_return_val_if_fail (handle != 0, NULL); - - module = gck_slot_get_module (self); - - session = gck_session_from_handle (self, handle, options); - g_return_val_if_fail (session != NULL, NULL); - - g_object_unref (module); - - return session; -} - -/* ---------------------------------------------------------------------------- - * OBJECT - */ - -static void -gck_slot_init (GckSlot *self) -{ - self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_SLOT, GckSlotPrivate); - self->pv->mutex = g_mutex_new (); -} - -static void -gck_slot_get_property (GObject *obj, guint prop_id, GValue *value, - GParamSpec *pspec) -{ - GckSlot *self = GCK_SLOT (obj); - - switch (prop_id) { - case PROP_MODULE: - g_value_take_object (value, gck_slot_get_module (self)); - break; - case PROP_HANDLE: - g_value_set_ulong (value, gck_slot_get_handle (self)); - break; - case PROP_INTERACTION: - g_value_take_object (value, gck_slot_get_interaction (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_slot_set_property (GObject *obj, guint prop_id, const GValue *value, - GParamSpec *pspec) -{ - GckSlot *self = GCK_SLOT (obj); - - /* All writes to data members below, happen only during construct phase */ - - switch (prop_id) { - case PROP_MODULE: - g_assert (!self->pv->module); - self->pv->module = g_value_get_object (value); - g_assert (self->pv->module); - g_object_ref (self->pv->module); - break; - case PROP_HANDLE: - g_assert (!self->pv->handle); - self->pv->handle = g_value_get_ulong (value); - break; - case PROP_INTERACTION: - gck_slot_set_interaction (self, g_value_get_object (value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -gck_slot_dispose (GObject *obj) -{ - GckSlot *self = GCK_SLOT (obj); - - gck_slot_set_interaction (self, NULL); - - G_OBJECT_CLASS (gck_slot_parent_class)->dispose (obj); -} - -static void -gck_slot_finalize (GObject *obj) -{ - GckSlot *self = GCK_SLOT (obj); - - g_assert (self->pv->interaction == NULL); - - self->pv->handle = 0; - g_clear_object (&self->pv->module); - g_mutex_free (self->pv->mutex); - - G_OBJECT_CLASS (gck_slot_parent_class)->finalize (obj); -} - -static void -gck_slot_class_init (GckSlotClass *klass) -{ - GObjectClass *gobject_class = (GObjectClass*)klass; - gck_slot_parent_class = g_type_class_peek_parent (klass); - - gobject_class->get_property = gck_slot_get_property; - gobject_class->set_property = gck_slot_set_property; - gobject_class->dispose = gck_slot_dispose; - gobject_class->finalize = gck_slot_finalize; - - /** - * GckSlot:module: - * - * The PKCS11 object that this slot is a part of. - */ - g_object_class_install_property (gobject_class, PROP_MODULE, - g_param_spec_object ("module", "Module", "PKCS11 Module", - GCK_TYPE_MODULE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckSlot:handle: - * - * The raw CK_SLOT_ID handle of this slot. - */ - g_object_class_install_property (gobject_class, PROP_HANDLE, - g_param_spec_ulong ("handle", "Handle", "PKCS11 Slot ID", - 0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - /** - * GckSlot:interaction: - * - * Interaction object used to ask the user for pins when opening - * sessions. Used if the session_options of the enumerator have - * %GCK_SESSION_LOGIN_USER or %GCK_SESSION_AUTHENTICATE - */ - g_object_class_install_property (gobject_class, PROP_INTERACTION, - g_param_spec_object ("interaction", "Interaction", "Interaction asking for pins", - G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE)); - - g_type_class_add_private (gobject_class, sizeof (GckSlotPrivate)); -} - -/* ---------------------------------------------------------------------------- - * PUBLIC - */ - -/** - * GckSlotInfo: - * @slot_description: Description of the slot. - * @manufacturer_id: The manufacturer of this slot. - * @flags: Various PKCS11 flags that apply to this slot. - * @hardware_version_major: The major version of the hardware. - * @hardware_version_minor: The minor version of the hardware. - * @firmware_version_major: The major version of the firmware. - * @firmware_version_minor: The minor version of the firmware. - * - * Represents information about a PKCS11 slot. - * - * This is analogous to a CK_SLOT_INFO structure, but the - * strings are far more usable. - * - * When you're done with this structure it should be released with - * gck_slot_info_free(). - */ - -GType -gck_slot_info_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckSlotInfo", - (GBoxedCopyFunc)gck_slot_info_copy, - (GBoxedFreeFunc)gck_slot_info_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_slot_info_copy: - * @slot_info: a slot info - * - * Make a copy of the slot info. - * - * Returns: (transfer full): a newly allocated copy slot info - */ -GckSlotInfo * -gck_slot_info_copy (GckSlotInfo *slot_info) -{ - if (slot_info == NULL) - return NULL; - - slot_info = g_memdup (slot_info, sizeof (GckSlotInfo)); - slot_info->manufacturer_id = g_strdup (slot_info->manufacturer_id); - slot_info->slot_description = g_strdup (slot_info->slot_description); - - return slot_info; -} - - -/** - * gck_slot_info_free: - * @slot_info: The slot info to free, or NULL. - * - * Free the GckSlotInfo and associated resources. - **/ -void -gck_slot_info_free (GckSlotInfo *slot_info) -{ - if (!slot_info) - return; - g_free (slot_info->slot_description); - g_free (slot_info->manufacturer_id); - g_free (slot_info); -} - -/** - * GckTokenInfo: - * @label: The displayable token label. - * @manufacturer_id: The manufacturer of this slot. - * @model: The token model number as a string. - * @serial_number: The token serial number as a string. - * @flags: Various PKCS11 flags that apply to this token. - * @max_session_count: The maximum number of sessions allowed on this token. - * @session_count: The number of sessions open on this token. - * @max_rw_session_count: The maximum number of read/write sessions allowed on this token. - * @rw_session_count: The number of sessions open on this token. - * @max_pin_len: The maximum length of a PIN for locking this token. - * @min_pin_len: The minimum length of a PIN for locking this token. - * @total_public_memory: The total amount of memory on this token for storing public objects. - * @free_public_memory: The available amount of memory on this token for storing public objects. - * @total_private_memory: The total amount of memory on this token for storing private objects. - * @free_private_memory: The available amount of memory on this token for storing private objects. - * @hardware_version_major: The major version of the hardware. - * @hardware_version_minor: The minor version of the hardware. - * @firmware_version_major: The major version of the firmware. - * @firmware_version_minor: The minor version of the firmware. - * @utc_time: If the token has a hardware clock, this is set to the number of seconds since the epoch. - * - * Represents information about a PKCS11 token. - * - * This is analogous to a CK_TOKEN_INFO structure, but the - * strings are far more usable. - * - * When you're done with this structure it should be released with - * gck_token_info_free(). - */ - -GType -gck_token_info_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckTokenInfo", - (GBoxedCopyFunc)gck_token_info_copy, - (GBoxedFreeFunc)gck_token_info_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_token_info_copy: - * @token_info: a token info - * - * Make a copy of the token info. - * - * Returns: (transfer full): a newly allocated copy token info - */ -GckTokenInfo * -gck_token_info_copy (GckTokenInfo *token_info) -{ - if (token_info == NULL) - return NULL; - - token_info = g_memdup (token_info, sizeof (GckTokenInfo)); - token_info->label = g_strdup (token_info->label); - token_info->manufacturer_id = g_strdup (token_info->manufacturer_id); - token_info->model = g_strdup (token_info->model); - token_info->serial_number = g_strdup (token_info->serial_number); - return token_info; -} - -/** - * gck_token_info_free: - * @token_info: The token info to free, or NULL. - * - * Free the GckTokenInfo and associated resources. - **/ -void -gck_token_info_free (GckTokenInfo *token_info) -{ - if (!token_info) - return; - g_free (token_info->label); - g_free (token_info->manufacturer_id); - g_free (token_info->model); - g_free (token_info->serial_number); - g_free (token_info); -} - -/** - * GckMechanismInfo: - * @min_key_size: The minimum key size that can be used with this mechanism. - * @max_key_size: The maximum key size that can be used with this mechanism. - * @flags: Various PKCS11 flags that apply to this mechanism. - * - * Represents information about a PKCS11 mechanism. - * - * This is analogous to a CK_MECHANISM_INFO structure. - * - * When you're done with this structure it should be released with - * gck_mechanism_info_free(). - */ - -GType -gck_mechanism_info_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckMechanismInfo", - (GBoxedCopyFunc)gck_mechanism_info_copy, - (GBoxedFreeFunc)gck_mechanism_info_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_mechanism_info_copy: - * @mech_info: a mechanism info - * - * Make a copy of the mechanism info. - * - * Returns: (transfer full): a newly allocated copy mechanism info - */ -GckMechanismInfo * -gck_mechanism_info_copy (GckMechanismInfo *mech_info) -{ - return g_memdup (mech_info, sizeof (GckMechanismInfo)); -} - -/** - * gck_mechanism_info_free: - * @mech_info: The mechanism info to free, or NULL. - * - * Free the GckMechanismInfo and associated resources. - **/ -void -gck_mechanism_info_free (GckMechanismInfo *mech_info) -{ - if (!mech_info) - return; - g_free (mech_info); -} - -/** - * GckMechanisms: - * - * A set of GckMechanismInfo structures. - */ - -/** - * gck_mechanisms_length: - * @a: A GckMechanisms set. - * - * Get the number of GckMechanismInfo in the set. - * - * Returns: The number in the set. - */ - -/** - * gck_mechanisms_at: - * @a: A GckMechanisms set. - * @i: The index of a mechanism - * - * Get a specific mechanism in a the set. - * - * Returns: the mechanism - */ - -/** - * gck_mechanisms_free: - * @a: A GckMechanism set. - * - * Free a GckMechanisms set. - */ - -/** - * gck_mechanisms_check: - * @mechanisms: A list of mechanisms, perhaps retrieved from gck_slot_get_mechanisms(). - * @...: A list of mechanism types followed by GCK_INVALID. - * - * Check whether all the mechanism types are in the list. - * - * The arguments should be a list of CKM_XXX mechanism types. The last argument - * should be GCK_INVALID. - * - * Return value: Whether the mechanism is in the list or not. - **/ -gboolean -gck_mechanisms_check (GArray *mechanisms, ...) -{ - gboolean found = TRUE; - va_list va; - gulong mech; - gsize i; - - g_return_val_if_fail (mechanisms != NULL, FALSE); - - va_start (va, mechanisms); - for (;;) { - mech = va_arg (va, gulong); - if (mech == GCK_INVALID) - break; - - found = FALSE; - for (i = 0; i < gck_mechanisms_length (mechanisms); ++i) { - if (gck_mechanisms_at (mechanisms, i) == mech) { - found = TRUE; - break; - } - } - - if (found == FALSE) - break; - - } - va_end (va); - - return found; -} - -/** - * gck_slot_equal: - * @slot1: (type Gck.Slot): a pointer to the first #GckSlot - * @slot2: (type Gck.Slot): a pointer to the second #GckSlot - * - * Checks equality of two slots. Two GckSlot objects can point to the same - * underlying PKCS\#11 slot. - * - * Return value: TRUE if slot1 and slot2 are equal. FALSE if either is not a GckSlot. - **/ -gboolean -gck_slot_equal (gconstpointer slot1, gconstpointer slot2) -{ - GckSlot *s1, *s2; - - if (slot1 == slot2) - return TRUE; - if (!GCK_IS_SLOT (slot1) || !GCK_IS_SLOT (slot2)) - return FALSE; - - s1 = GCK_SLOT (slot1); - s2 = GCK_SLOT (slot2); - - return s1->pv->handle == s2->pv->handle && - gck_module_equal (s1->pv->module, s2->pv->module); -} - -/** - * gck_slot_hash: - * @slot: (type Gck.Slot): a pointer to a #GckSlot - * - * Create a hash value for the GckSlot. - * - * This function is intended for easily hashing a GckSlot to add to - * a GHashTable or similar data structure. - * - * Return value: An integer that can be used as a hash value, or 0 if invalid. - **/ -guint -gck_slot_hash (gconstpointer slot) -{ - GckSlot *self; - - g_return_val_if_fail (GCK_IS_SLOT (slot), 0); - - self = GCK_SLOT (slot); - - return _gck_ulong_hash (&self->pv->handle) ^ - gck_module_hash (self->pv->module); -} - -/** - * gck_slot_from_handle: - * @module: The module that this slot is on. - * @slot_id: The raw PKCS\#11 handle or slot id of this slot. - * - * Create a new GckSlot object for a raw PKCS\#11 handle. - * - * Returns: (transfer full): The new GckSlot object. - **/ -GckSlot * -gck_slot_from_handle (GckModule *module, - gulong slot_id) -{ - return g_object_new (GCK_TYPE_SLOT, - "module", module, - "handle", slot_id, - NULL); -} - -/** - * gck_slot_get_handle: - * @self: The slot to get the handle of. - * - * Get the raw PKCS\#11 handle of a slot. - * - * Return value: the raw CK_SLOT_ID handle - **/ -gulong -gck_slot_get_handle (GckSlot *self) -{ - g_return_val_if_fail (GCK_IS_SLOT (self), (CK_SLOT_ID)-1); - return self->pv->handle; -} - -/** - * gck_slot_get_module: - * @self: The slot to get the module for. - * - * Get the module that this slot is on. - * - * Returns: (transfer full): The module, you must unreference this after - * you're done with it. - */ -GckModule * -gck_slot_get_module (GckSlot *self) -{ - g_return_val_if_fail (GCK_IS_SLOT (self), NULL); - g_return_val_if_fail (GCK_IS_MODULE (self->pv->module), NULL); - return g_object_ref (self->pv->module); -} - -/** - * gck_slot_get_info: - * @self: The slot to get info for. - * - * Get the information for this slot. - * - * Returns: (transfer full): the slot information, when done, use gck_slot_info_free() - * to release it. - **/ -GckSlotInfo * -gck_slot_get_info (GckSlot *self) -{ - CK_SLOT_ID handle = (CK_SLOT_ID)-1; - GckModule *module = NULL; - CK_FUNCTION_LIST_PTR funcs; - GckSlotInfo *slotinfo; - CK_SLOT_INFO info; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SLOT (self), NULL); - - g_object_get (self, "module", &module, "handle", &handle, NULL); - g_return_val_if_fail (GCK_IS_MODULE (module), NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (funcs, NULL); - - memset (&info, 0, sizeof (info)); - rv = (funcs->C_GetSlotInfo) (handle, &info); - - g_object_unref (module); - - if (rv != CKR_OK) { - g_warning ("couldn't get slot info: %s", gck_message_from_rv (rv)); - return NULL; - } - - slotinfo = g_new0 (GckSlotInfo, 1); - slotinfo->slot_description = gck_string_from_chars (info.slotDescription, - sizeof (info.slotDescription)); - slotinfo->manufacturer_id = gck_string_from_chars (info.manufacturerID, - sizeof (info.manufacturerID)); - slotinfo->flags = info.flags; - slotinfo->hardware_version_major = info.hardwareVersion.major; - slotinfo->hardware_version_minor = info.hardwareVersion.minor; - slotinfo->firmware_version_major = info.firmwareVersion.major; - slotinfo->firmware_version_minor = info.firmwareVersion.minor; - - return slotinfo; -} - -GckTokenInfo* -_gck_token_info_from_pkcs11 (CK_TOKEN_INFO_PTR info) -{ - GckTokenInfo *token_info; - gchar *string; - struct tm tm; - - token_info = g_new0 (GckTokenInfo, 1); - token_info->label = gck_string_from_chars (info->label, sizeof (info->label)); - token_info->model = gck_string_from_chars (info->model, sizeof (info->model)); - token_info->manufacturer_id = gck_string_from_chars (info->manufacturerID, - sizeof (info->manufacturerID)); - token_info->serial_number = gck_string_from_chars (info->serialNumber, - sizeof (info->serialNumber)); - token_info->flags = info->flags; - token_info->max_session_count = info->ulMaxSessionCount; - token_info->session_count = info->ulSessionCount; - token_info->max_rw_session_count = info->ulMaxRwSessionCount; - token_info->rw_session_count = info->ulRwSessionCount; - token_info->max_pin_len = info->ulMaxPinLen; - token_info->min_pin_len = info->ulMinPinLen; - token_info->total_public_memory = info->ulTotalPublicMemory; - token_info->total_private_memory = info->ulTotalPrivateMemory; - token_info->free_private_memory = info->ulFreePrivateMemory; - token_info->free_public_memory = info->ulFreePublicMemory; - token_info->hardware_version_major = info->hardwareVersion.major; - token_info->hardware_version_minor = info->hardwareVersion.minor; - token_info->firmware_version_major = info->firmwareVersion.major; - token_info->firmware_version_minor = info->firmwareVersion.minor; - - /* Parse the time into seconds since epoch */ - if (info->flags & CKF_CLOCK_ON_TOKEN) { - string = g_strndup ((gchar*)info->utcTime, MIN (14, sizeof (info->utcTime))); - if (!strptime (string, "%Y%m%d%H%M%S", &tm)) - token_info->utc_time = -1; - else - token_info->utc_time = timegm (&tm); - g_free (string); - } else { - token_info->utc_time = -1; - } - - return token_info; -} - -void -_gck_token_info_to_pkcs11 (GckTokenInfo *token_info, CK_TOKEN_INFO_PTR info) -{ - gchar buffer[64]; - struct tm tm; - time_t tim; - gsize len; - - if (!gck_string_to_chars (info->label, - sizeof (info->label), - token_info->label)) - g_return_if_reached (); - if (!gck_string_to_chars (info->model, - sizeof (info->model), - token_info->model)) - g_return_if_reached (); - if (!gck_string_to_chars (info->manufacturerID, - sizeof (info->manufacturerID), - token_info->manufacturer_id)) - g_return_if_reached (); - if (!gck_string_to_chars (info->serialNumber, - sizeof (info->serialNumber), - token_info->serial_number)) - g_return_if_reached (); - - info->flags = token_info->flags; - info->ulMaxSessionCount = token_info->max_session_count; - info->ulSessionCount = token_info->session_count; - info->ulMaxRwSessionCount = token_info->max_rw_session_count; - info->ulRwSessionCount = token_info->rw_session_count; - info->ulMaxPinLen = token_info->max_pin_len; - info->ulMinPinLen = token_info->min_pin_len; - info->ulTotalPublicMemory = token_info->total_public_memory; - info->ulTotalPrivateMemory = token_info->total_private_memory; - info->ulFreePrivateMemory = token_info->free_private_memory; - info->ulFreePublicMemory = token_info->free_public_memory; - info->hardwareVersion.major = token_info->hardware_version_major; - info->hardwareVersion.minor = token_info->hardware_version_minor; - info->firmwareVersion.major = token_info->firmware_version_major; - info->firmwareVersion.minor = token_info->firmware_version_minor; - - /* Parse the time into seconds since epoch */ - if (token_info->flags & CKF_CLOCK_ON_TOKEN) { - tim = token_info->utc_time; - if (!gmtime_r (&tim, &tm)) - g_return_if_reached (); - len = strftime (buffer, sizeof (buffer), "%Y%m%d%H%M%S", &tm); - g_return_if_fail (len == sizeof (info->utcTime)); - memcpy (info->utcTime, buffer, sizeof (info->utcTime)); - } else { - memset (info->utcTime, 0, sizeof (info->utcTime)); - } -} - -/** - * gck_slot_get_token_info: - * @self: The slot to get info for. - * - * Get the token information for this slot. - * - * Returns: (transfer full): the token information; when done, use gck_token_info_free() - * to release it - **/ -GckTokenInfo * -gck_slot_get_token_info (GckSlot *self) -{ - CK_SLOT_ID handle = (CK_SLOT_ID)-1; - CK_FUNCTION_LIST_PTR funcs; - GckModule *module = NULL; - CK_TOKEN_INFO info; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SLOT (self), NULL); - - g_object_get (self, "module", &module, "handle", &handle, NULL); - g_return_val_if_fail (GCK_IS_MODULE (module), NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (funcs, NULL); - - 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", gck_message_from_rv (rv)); - return NULL; - } - - return _gck_token_info_from_pkcs11 (&info); -} - -/** - * gck_slot_get_mechanisms: - * @self: The slot to get mechanisms for. - * - * Get the available mechanisms for this slot. - * - * Returns: (transfer full): a list of the mechanisms for this slot, which should - * be freed with g_array_free () - **/ -GArray * -gck_slot_get_mechanisms (GckSlot *self) -{ - CK_SLOT_ID handle = (CK_SLOT_ID)-1; - CK_FUNCTION_LIST_PTR funcs; - GckModule *module = NULL; - CK_MECHANISM_TYPE_PTR mech_list = NULL; - CK_ULONG count, i; - GArray *result; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SLOT (self), NULL); - - g_object_get (self, "module", &module, "handle", &handle, NULL); - g_return_val_if_fail (GCK_IS_MODULE (module), NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (funcs, NULL); - - rv = (funcs->C_GetMechanismList) (handle, NULL, &count); - if (rv != CKR_OK) { - g_warning ("couldn't get mechanism count: %s", gck_message_from_rv (rv)); - count = 0; - } else { - mech_list = g_new (CK_MECHANISM_TYPE, count); - rv = (funcs->C_GetMechanismList) (handle, mech_list, &count); - if (rv != CKR_OK) { - g_warning ("couldn't get mechanism list: %s", gck_message_from_rv (rv)); - g_free (mech_list); - count = 0; - } - } - - g_object_unref (module); - - if (!count) - return NULL; - - result = g_array_new (FALSE, TRUE, sizeof (CK_MECHANISM_TYPE)); - for (i = 0; i < count; ++i) - g_array_append_val (result, mech_list[i]); - - g_free (mech_list); - return result; - -} - -/** - * gck_slot_get_mechanism_info: - * @self: The slot to get mechanism info from. - * @mech_type: The mechanisms type to get info for. - * - * Get information for the specified mechanism. - * - * Returns: (transfer full): the mechanism information, or NULL if failed; use - * gck_mechanism_info_free() when done with it - **/ -GckMechanismInfo* -gck_slot_get_mechanism_info (GckSlot *self, gulong mech_type) -{ - CK_SLOT_ID handle = (CK_SLOT_ID)-1; - CK_FUNCTION_LIST_PTR funcs; - GckMechanismInfo *mechinfo; - GckModule *module = NULL; - CK_MECHANISM_INFO info; - struct tm; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SLOT (self), NULL); - - g_object_get (self, "module", &module, "handle", &handle, NULL); - g_return_val_if_fail (GCK_IS_MODULE (module), NULL); - - funcs = gck_module_get_functions (module); - g_return_val_if_fail (funcs, NULL); - - memset (&info, 0, sizeof (info)); - rv = (funcs->C_GetMechanismInfo) (handle, mech_type, &info); - - g_object_unref (module); - - if (rv != CKR_OK) { - g_warning ("couldn't get mechanism info: %s", gck_message_from_rv (rv)); - return NULL; - } - - mechinfo = g_new0 (GckMechanismInfo, 1); - mechinfo->flags = info.flags; - mechinfo->max_key_size = info.ulMaxKeySize; - mechinfo->min_key_size = info.ulMinKeySize; - - return mechinfo; -} - -/** - * gck_slot_has_flags: - * @self: The GckSlot object. - * @flags: The flags to check. - * - * Check if the PKCS11 slot has the given flags. - * - * Returns: Whether one or more flags exist. - */ -gboolean -gck_slot_has_flags (GckSlot *self, gulong flags) -{ - CK_FUNCTION_LIST_PTR funcs; - GckModule *module = NULL; - CK_TOKEN_INFO info; - CK_SLOT_ID handle; - CK_RV rv; - - g_return_val_if_fail (GCK_IS_SLOT (self), FALSE); - - g_object_get (self, "module", &module, "handle", &handle, NULL); - g_return_val_if_fail (GCK_IS_MODULE (module), FALSE); - - funcs = gck_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", gck_message_from_rv (rv)); - return FALSE; - } - - return (info.flags & flags) != 0; -} - -/** - * gck_slot_get_interaction: - * @self: the slot - * - * Get the interaction used when a pin is needed - * - * Returns: (transfer full) (allow-none): the interaction or %NULL - */ -GTlsInteraction * -gck_slot_get_interaction (GckSlot *self) -{ - GTlsInteraction *result = NULL; - - g_return_val_if_fail (GCK_IS_SLOT (self), NULL); - - g_mutex_lock (self->pv->mutex); - - if (self->pv->interaction) - result = g_object_ref (self->pv->interaction); - - g_mutex_unlock (self->pv->mutex); - - return result; -} - -/** - * gck_slot_set_interaction: - * @self: the slot - * @interaction: (allow-none): the interaction or %NULL - * - * Set the interaction used when a pin is needed - */ -void -gck_slot_set_interaction (GckSlot *self, - GTlsInteraction *interaction) -{ - GTlsInteraction *previous = NULL; - - g_return_if_fail (GCK_IS_SLOT (self)); - g_return_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction)); - - g_mutex_lock (self->pv->mutex); - - if (interaction != self->pv->interaction) { - previous = self->pv->interaction; - self->pv->interaction = interaction; - if (interaction) - g_object_ref (interaction); - } - - g_mutex_unlock (self->pv->mutex); - - g_clear_object (&previous); - g_object_notify (G_OBJECT (self), "interaction"); -} - -/** - * gck_slots_enumerate_objects: - * @slots: (element-type Gck.Slot): a list of #GckSlot to enumerate objects on. - * @attrs: Attributes that the objects must have, or empty for all objects. - * @options: options for opening a session - * - * Setup an enumerator for listing matching objects on the slots. - * - * This call will not block but will return an enumerator immediately. - * - * Returns: (transfer full): a new enumerator - **/ -GckEnumerator* -gck_slots_enumerate_objects (GList *slots, - GckAttributes *attrs, - GckSessionOptions options) -{ - GckUriData *uri_data; - - g_return_val_if_fail (attrs, NULL); - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_ref (attrs); - - return _gck_enumerator_new (slots, options, uri_data); -} - - -typedef struct OpenSession { - GckArguments base; - GTlsInteraction *interaction; - GckSlot *slot; - gulong flags; - gpointer app_data; - CK_NOTIFY notify; - gchar *password; - gboolean auto_login; - CK_SESSION_HANDLE session; -} OpenSession; - -static CK_RV -perform_open_session (OpenSession *args) -{ - GTlsInteraction *interaction; - CK_RV rv = CKR_OK; - - /* Can be called multiple times */ - - /* First step, open session */ - if (!args->session) { - rv = (args->base.pkcs11->C_OpenSession) (args->base.handle, args->flags, - args->app_data, args->notify, &args->session); - } - - if (rv != CKR_OK || !args->auto_login) - return rv; - - /* Compatibility, hook into GckModule signals if no interaction set */ - if (args->interaction) - interaction = g_object_ref (args->interaction); - else - interaction = _gck_interaction_new (args->slot); - - rv = _gck_session_authenticate_token (args->base.pkcs11, args->session, - args->slot, interaction, NULL); - - g_object_unref (interaction); - - return rv; -} - -static void -free_open_session (OpenSession *args) -{ - g_clear_object (&args->interaction); - g_clear_object (&args->slot); - g_assert (!args->password); - g_free (args); -} - -/** - * gck_slot_open_session: - * @self: The slot ot open a session on. - * @options: The #GckSessionOptions to open a session with. - * @cancellable: An optional cancellation object, or %NULL. - * @error: A location to return an error, or %NULL. - * - * Open a session on the slot. If the 'auto reuse' setting is set, - * then this may be a recycled session with the same flags. - * - * This call may block for an indefinite period. - * - * Returns: (transfer full): a new session or %NULL if an error occurs - **/ -GckSession * -gck_slot_open_session (GckSlot *self, - GckSessionOptions options, - GCancellable *cancellable, - GError **error) -{ - return gck_slot_open_session_full (self, options, 0, NULL, NULL, cancellable, error); -} - -/** - * gck_slot_open_session_full: (skip) - * @self: The slot to open a session on. - * @options: The options to open the new session with. - * @pkcs11_flags: Additional raw PKCS\#11 flags. - * @app_data: Application data for notification callback. - * @notify: PKCS\#11 notification callback. - * @cancellable: Optional cancellation object, or NULL. - * @error: A location to return an error, or NULL. - * - * Open a session on the slot. If the 'auto reuse' setting is set, - * then this may be a recycled session with the same flags. - * - * This call may block for an indefinite period. - * - * Returns: (transfer full): a new session or %NULL if an error occurs - **/ -GckSession * -gck_slot_open_session_full (GckSlot *self, - GckSessionOptions options, - gulong pkcs11_flags, - gpointer app_data, - CK_NOTIFY notify, - GCancellable *cancellable, - GError **error) -{ - OpenSession args = { GCK_ARGUMENTS_INIT, 0, }; - GckSession *session = NULL; - GckModule *module = NULL; - - g_object_ref (self); - - module = gck_slot_get_module (self); - - /* Open a new session */ - args.slot = self; - args.app_data = app_data; - args.notify = notify; - args.password = NULL; - args.session = 0; - args.interaction = gck_slot_get_interaction (self); - - args.auto_login = ((options & GCK_SESSION_LOGIN_USER) == GCK_SESSION_LOGIN_USER); - - args.flags = pkcs11_flags | CKF_SERIAL_SESSION; - if ((options & GCK_SESSION_READ_WRITE) == GCK_SESSION_READ_WRITE) - args.flags |= CKF_RW_SESSION; - - if (_gck_call_sync (self, perform_open_session, NULL, &args, cancellable, error)) - session = make_session_object (self, options, args.session); - - g_clear_object (&args.interaction); - g_object_unref (module); - g_object_unref (self); - - return session; -} - -/** - * gck_slot_open_session_async: - * @self: The slot to open a session on. - * @options: The options to open the new session with. - * @cancellable: Optional cancellation object, or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Open a session on the slot. If the 'auto reuse' setting is set, - * then this may be a recycled session with the same flags. - * - * This call will return immediately and complete asynchronously. - **/ -void -gck_slot_open_session_async (GckSlot *self, - GckSessionOptions options, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) -{ - gck_slot_open_session_full_async (self, options, 0UL, NULL, NULL, cancellable, callback, user_data); -} - -/** - * gck_slot_open_session_full_async: (skip) - * @self: The slot to open a session on. - * @options: Options to open the new session with. - * @pkcs11_flags: Additional raw PKCS\#11 flags. - * @app_data: Application data for notification callback. - * @notify: PKCS\#11 notification callback. - * @cancellable: Optional cancellation object, or NULL. - * @callback: Called when the operation completes. - * @user_data: Data to pass to the callback. - * - * Open a session on the slot. If the 'auto reuse' setting is set, - * then this may be a recycled session with the same flags. - * - * This call will return immediately and complete asynchronously. - **/ -void -gck_slot_open_session_full_async (GckSlot *self, - GckSessionOptions options, - gulong pkcs11_flags, - gpointer app_data, - CK_NOTIFY notify, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) -{ - OpenSession *args; - - g_object_ref (self); - - args = _gck_call_async_prep (self, self, perform_open_session, NULL, - sizeof (*args), free_open_session); - - args->app_data = app_data; - args->notify = notify; - args->slot = g_object_ref (self); - args->interaction = gck_slot_get_interaction (self); - - args->auto_login = ((options & GCK_SESSION_LOGIN_USER) == GCK_SESSION_LOGIN_USER); - - args->flags = pkcs11_flags | CKF_SERIAL_SESSION; - if ((options & GCK_SESSION_READ_WRITE) == GCK_SESSION_READ_WRITE) - args->flags |= CKF_RW_SESSION; - - _gck_call_async_ready_go (args, cancellable, callback, user_data); - g_object_unref (self); -} - -/** - * gck_slot_open_session_finish: - * @self: The slot to open a session on. - * @result: The result passed to the callback. - * @error: A location to return an error or NULL. - * - * Get the result of an open session operation. If the 'auto reuse' setting is set, - * then this may be a recycled session with the same flags. - * - * Returns: (transfer full): the new session or %NULL if an error occurs - */ -GckSession* -gck_slot_open_session_finish (GckSlot *self, GAsyncResult *result, GError **err) -{ - GckSession *session = NULL; - - g_object_ref (self); - - { - OpenSession *args; - - if (_gck_call_basic_finish (result, err)) { - args = _gck_call_arguments (result, OpenSession); - session = make_session_object (self, args->flags, args->session); - } - } - - g_object_unref (self); - - return session; -} - -/** - * gck_slot_match: - * @self: the slot to match - * @uri: the uri to match against the slot - * - * Check whether the PKCS\#11 URI matches the slot - * - * Returns: whether the URI matches or not - */ -gboolean -gck_slot_match (GckSlot *self, - GckUriData *uri) -{ - GckModule *module; - GckTokenInfo *info; - gboolean match = TRUE; - - g_return_val_if_fail (GCK_IS_SLOT (self), FALSE); - g_return_val_if_fail (uri != NULL, FALSE); - - if (uri->any_unrecognized) - match = FALSE; - - if (match && uri->module_info) { - module = gck_slot_get_module (self); - match = gck_module_match (module, uri); - g_object_unref (module); - } - - if (match && uri->token_info) { - info = gck_slot_get_token_info (self); - match = _gck_token_info_match (uri->token_info, info); - gck_token_info_free (info); - } - - return match; -} diff --git a/gck/gck-test.c b/gck/gck-test.c deleted file mode 100644 index ce59aebf..00000000 --- a/gck/gck-test.c +++ /dev/null @@ -1,165 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2010 Stefan Walter - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General License for more details. - * - * You should have received a copy of the GNU Lesser General - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - */ - -#include "config.h" - -#include "gck-test.h" - -#include "pkcs11/pkcs11.h" - -#include <glib.h> - -#include <string.h> - -static const gchar* -defined_rv_to_string (CK_RV rv) -{ - #define GKM_X(rv) case rv: return #rv; - switch (rv) { - - /* These are not really errors, or not current */ - GKM_X (CKR_OK) - GKM_X (CKR_NO_EVENT) - GKM_X (CKR_FUNCTION_NOT_PARALLEL) - GKM_X (CKR_SESSION_PARALLEL_NOT_SUPPORTED) - GKM_X (CKR_CANCEL) - GKM_X (CKR_FUNCTION_CANCELED) - GKM_X (CKR_HOST_MEMORY) - GKM_X (CKR_SLOT_ID_INVALID) - GKM_X (CKR_GENERAL_ERROR) - GKM_X (CKR_FUNCTION_FAILED) - GKM_X (CKR_ARGUMENTS_BAD) - GKM_X (CKR_NEED_TO_CREATE_THREADS) - GKM_X (CKR_CANT_LOCK) - GKM_X (CKR_ATTRIBUTE_READ_ONLY) - GKM_X (CKR_ATTRIBUTE_SENSITIVE) - GKM_X (CKR_ATTRIBUTE_TYPE_INVALID) - GKM_X (CKR_ATTRIBUTE_VALUE_INVALID) - GKM_X (CKR_DATA_INVALID) - GKM_X (CKR_DATA_LEN_RANGE) - GKM_X (CKR_DEVICE_ERROR) - GKM_X (CKR_DEVICE_MEMORY) - GKM_X (CKR_DEVICE_REMOVED) - GKM_X (CKR_ENCRYPTED_DATA_INVALID) - GKM_X (CKR_ENCRYPTED_DATA_LEN_RANGE) - GKM_X (CKR_FUNCTION_NOT_SUPPORTED) - GKM_X (CKR_KEY_HANDLE_INVALID) - GKM_X (CKR_KEY_SIZE_RANGE) - GKM_X (CKR_KEY_TYPE_INCONSISTENT) - GKM_X (CKR_KEY_NOT_NEEDED) - GKM_X (CKR_KEY_CHANGED) - GKM_X (CKR_KEY_NEEDED) - GKM_X (CKR_KEY_INDIGESTIBLE) - GKM_X (CKR_KEY_FUNCTION_NOT_PERMITTED) - GKM_X (CKR_KEY_NOT_WRAPPABLE) - GKM_X (CKR_KEY_UNEXTRACTABLE) - GKM_X (CKR_MECHANISM_INVALID) - GKM_X (CKR_MECHANISM_PARAM_INVALID) - GKM_X (CKR_OBJECT_HANDLE_INVALID) - GKM_X (CKR_OPERATION_ACTIVE) - GKM_X (CKR_OPERATION_NOT_INITIALIZED) - GKM_X (CKR_PIN_INCORRECT) - GKM_X (CKR_PIN_INVALID) - GKM_X (CKR_PIN_LEN_RANGE) - GKM_X (CKR_PIN_EXPIRED) - GKM_X (CKR_PIN_LOCKED) - GKM_X (CKR_SESSION_CLOSED) - GKM_X (CKR_SESSION_COUNT) - GKM_X (CKR_SESSION_HANDLE_INVALID) - GKM_X (CKR_SESSION_READ_ONLY) - GKM_X (CKR_SESSION_EXISTS) - GKM_X (CKR_SESSION_READ_ONLY_EXISTS) - GKM_X (CKR_SESSION_READ_WRITE_SO_EXISTS) - GKM_X (CKR_SIGNATURE_INVALID) - GKM_X (CKR_SIGNATURE_LEN_RANGE) - GKM_X (CKR_TEMPLATE_INCOMPLETE) - GKM_X (CKR_TEMPLATE_INCONSISTENT) - GKM_X (CKR_TOKEN_NOT_PRESENT) - GKM_X (CKR_TOKEN_NOT_RECOGNIZED) - GKM_X (CKR_TOKEN_WRITE_PROTECTED) - GKM_X (CKR_UNWRAPPING_KEY_HANDLE_INVALID) - GKM_X (CKR_UNWRAPPING_KEY_SIZE_RANGE) - GKM_X (CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT) - GKM_X (CKR_USER_ALREADY_LOGGED_IN) - GKM_X (CKR_USER_NOT_LOGGED_IN) - GKM_X (CKR_USER_PIN_NOT_INITIALIZED) - GKM_X (CKR_USER_TYPE_INVALID) - GKM_X (CKR_USER_ANOTHER_ALREADY_LOGGED_IN) - GKM_X (CKR_USER_TOO_MANY_TYPES) - GKM_X (CKR_WRAPPED_KEY_INVALID) - GKM_X (CKR_WRAPPED_KEY_LEN_RANGE) - GKM_X (CKR_WRAPPING_KEY_HANDLE_INVALID) - GKM_X (CKR_WRAPPING_KEY_SIZE_RANGE) - GKM_X (CKR_WRAPPING_KEY_TYPE_INCONSISTENT) - GKM_X (CKR_RANDOM_SEED_NOT_SUPPORTED) - GKM_X (CKR_RANDOM_NO_RNG) - GKM_X (CKR_DOMAIN_PARAMS_INVALID) - GKM_X (CKR_BUFFER_TOO_SMALL) - GKM_X (CKR_SAVED_STATE_INVALID) - GKM_X (CKR_INFORMATION_SENSITIVE) - GKM_X (CKR_STATE_UNSAVEABLE) - GKM_X (CKR_CRYPTOKI_NOT_INITIALIZED) - GKM_X (CKR_CRYPTOKI_ALREADY_INITIALIZED) - GKM_X (CKR_MUTEX_BAD) - GKM_X (CKR_MUTEX_NOT_LOCKED) - GKM_X (CKR_FUNCTION_REJECTED) - default: - return NULL; - } - - #undef GKM_X -} - -static gchar* -rv_to_string (CK_RV rv) -{ - gchar *string = g_strdup (defined_rv_to_string (rv)); - if (string == NULL) - string = g_strdup_printf ("0x%08lx", (gulong)rv); - return string; -} - -void -gck_assertion_message_cmprv (const gchar *domain, const gchar *file, gint line, - const gchar *func, const gchar *expr, - CK_RV arg1, const gchar *cmp, CK_RV arg2) -{ - gchar *a1, *a2, *s; - a1 = rv_to_string (arg1); - a2 = rv_to_string (arg2); - s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2); - g_free (a1); - g_free (a2); - g_assertion_message (domain, file, line, func, s); - g_free (s); -} - -void -gck_assertion_message_cmpulong (const gchar *domain, const gchar *file, gint line, - const gchar *func, const gchar *expr, - CK_ULONG arg1, const gchar *cmp, CK_ULONG arg2) -{ - char *s = NULL; - s = g_strdup_printf ("assertion failed (%s): (0x%08llx %s 0x%08llx)", expr, - (long long unsigned)arg1, cmp, (long long unsigned)arg2); - g_assertion_message (domain, file, line, func, s); - g_free (s); -} diff --git a/gck/gck-test.h b/gck/gck-test.h deleted file mode 100644 index 186d6e5f..00000000 --- a/gck/gck-test.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2010 Stefan Walter - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - */ - -#include <glib.h> - -#include "gck/gck.h" - -#include "pkcs11/pkcs11.h" -#include "pkcs11/pkcs11i.h" - -#ifndef GCK_TEST_H -#define GCK_TEST_H - -#define gck_assert_cmprv(v1, cmp, v2) \ - do { CK_RV __v1 = (v1), __v2 = (v2); \ - if (__v1 cmp __v2) ; else \ - gck_assertion_message_cmprv (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ - #v1 " " #cmp " " #v2, __v1, #cmp, __v2); \ - } while (0) - -#define gck_assert_cmpulong(v1, cmp, v2) \ - do { CK_RV __v1 = (v1), __v2 = (v2); \ - if (__v1 cmp __v2) ; else \ - gck_assertion_message_cmpulong (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ - #v1 " " #cmp " " #v2, __v1, #cmp, __v2); \ - } while (0) - -void gck_assertion_message_cmprv (const gchar *domain, - const gchar *file, - int line, - const gchar *func, - const gchar *expr, - CK_RV arg1, - const gchar *cmp, - CK_RV arg2); - -void gck_assertion_message_cmpulong (const gchar *domain, - const gchar *file, - gint line, - const gchar *func, - const gchar *expr, - CK_ULONG arg1, - const gchar *cmp, - CK_ULONG arg2); - -#endif /* GCK_TEST_H */ diff --git a/gck/gck-uri.c b/gck/gck-uri.c deleted file mode 100644 index 3d66c835..00000000 --- a/gck/gck-uri.c +++ /dev/null @@ -1,365 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck-uri.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2010, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include "config.h" - -#include "gck.h" -#include "gck-private.h" -#include "gck-marshal.h" - -#include <glib/gi18n-lib.h> - -#include <p11-kit/uri.h> - -#include <string.h> -#include <stdlib.h> - -/** - * SECTION:gck-uri - * @title: PKCS11 URIs - * @short_description: Parsing and building PKCS\#11 URIs. - * - * <ulink href='http://tools.ietf.org/html/draft-pechanec-pkcs11uri-03'>PKCS\#11 URIs</ulink> - * are a standard for referring to PKCS\#11 modules, tokens, or objects. What the - * PKCS\#11 URI refers to depends on the context in which it is used. - * - * A PKCS\#11 URI can always resolve to more than one object, token or module. A - * PKCS\#11 URI that refers to a token, would (when used in a context that expects - * objects) refer to all the token on that module. - * - * In most cases the parsing or building of URIs is handled elsewhere in the GCK - * library. For example to enumerate objects that match a PKCS\#11 URI use the - * gck_modules_enumerate_uri() function. - * - * To parse a PKCS\#11 URI use the gck_uri_parse() function passing in the type of - * context in which you're using the URI. To build a URI use the gck_uri_build() - * function. - **/ - -/** - * GckUriData: - * @any_unrecognized: whether any parts of the PKCS\#11 URI were unsupported or unrecognized. - * @module_info: information about the PKCS\#11 modules matching the URI. - * @token_info: information about the PKCS\#11 tokens matching the URI. - * @attributes: information about the PKCS\#11 objects matching the URI. - * - * Information about the contents of a PKCS\#11 URI. Various fields may be %NULL - * depending on the context that the URI was parsed for. - * - * Since PKCS\#11 URIs represent a set which results from the intersections of - * all of the URI parts, if @any_recognized is set to %TRUE then usually the URI - * should be treated as not matching anything. - */ - -/** - * GckUriFlags: - * @GCK_URI_FOR_MODULE: the URI will be used to match modules. - * @GCK_URI_FOR_TOKEN: the URI will be used to match tokens. - * @GCK_URI_FOR_OBJECT: the URI will be used to match objects. - * @GCK_URI_WITH_VERSION: the URI has specific version numbers for module and/or token - * @GCK_URI_FOR_ANY: parse all recognized components of the URI. - * - * Which parts of the PKCS\#11 URI will be parsed or formatted. These can be - * combined. - */ - -/** - * GCK_URI_FOR_MODULE_WITH_VERSION: - * - * The URI will match specific version of modules. To be used as a GckUriFlags argument. - */ - -/** - * GCK_URI_FOR_OBJECT_ON_TOKEN: - * - * The URI will match objects on a specific token. To be used as a GckUriFlags argument. - */ - -/** - * GCK_URI_FOR_OBJECT_ON_TOKEN_AND_MODULE: - * - * The token inserted into a device with a specific module. - */ - -/** - * GckUriError: - * @GCK_URI_BAD_SCHEME: invalid URI scheme - * @GCK_URI_BAD_ENCODING: bad URI encoding - * @GCK_URI_BAD_SYNTAX: bad URI syntax - * @GCK_URI_BAD_VERSION: bad URI version component - * @GCK_URI_NOT_FOUND: piece of the URI was not found - * - * Various error codes used with PKCS\#11 URIs - */ - -/** - * GCK_URI_ERROR: - * - * Error domain for URI errors. - */ - -/** - * GCK_URI_BAD_PREFIX: - * - * Use %GCK_URI_BAD_SCHEME instead. - * - * Deprecated: Since 3.2 - */ - -/** - * CKR_GCK_MODULE_PROBLEM: - * - * Use %GCK_ERROR_MODULE_PROBLEM instead. - * - * Deprecated: Since 3.4 - */ - -#define URI_PREFIX "pkcs11:" -#define N_URI_PREFIX 7 - -struct _GckUri { - gboolean any_unrecognized; - GckModuleInfo *module_info; - GckTokenInfo *token_info; - GckAttributes *attributes; -}; - -GQuark -gck_uri_get_error_quark (void) -{ - /* This is deprecated version */ - return gck_uri_get_error_quark (); -} - -GQuark -gck_uri_error_get_quark (void) -{ - static GQuark domain = 0; - static volatile gsize quark_inited = 0; - - if (g_once_init_enter (&quark_inited)) { - domain = g_quark_from_static_string ("gck-uri-error"); - g_once_init_leave (&quark_inited, 1); - } - - return domain; -} - -/** - * gck_uri_data_new: - * - * Allocate a new GckUriData structure. None of the fields - * will be set. - * - * Returns: (transfer full): a newly allocated GckUriData, free with - * gck_uri_data_free() - */ -GckUriData * -gck_uri_data_new (void) -{ - return g_slice_new0 (GckUriData); -} - -/** - * gck_uri_parse: - * @string: the URI to parse. - * @flags: the context in which the URI will be used. - * @error: a #GError, or %NULL. - * - * Parse a PKCS\#11 URI for use in a given context. - * - * The result will contain the fields that are relevant for - * the given context. See #GckUriData for more info. - * Other fields will be set to %NULL. - * - * Returns: (transfer full): a newly allocated #GckUriData; which should be - * freed with gck_uri_data_free() - */ -GckUriData* -gck_uri_parse (const gchar *string, GckUriFlags flags, GError **error) -{ - GckUriData *uri_data = NULL; - CK_ATTRIBUTE_PTR attrs; - CK_ULONG i, n_attrs; - P11KitUri *p11_uri; - gint res; - - g_return_val_if_fail (string, FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - p11_uri = p11_kit_uri_new (); - if (!p11_uri) - g_error ("failed to allocate P11KitUri"); - - res = p11_kit_uri_parse (string, flags, p11_uri); - if (res != P11_KIT_URI_OK) { - p11_kit_uri_free (p11_uri); - switch (res) { - case P11_KIT_URI_NO_MEMORY: - g_error ("failed to allocate memory in p11_kit_uri_parse()"); - break; - case P11_KIT_URI_BAD_ENCODING: - g_set_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING, - _("The URI has invalid encoding.")); - break; - case P11_KIT_URI_BAD_SCHEME: - g_set_error_literal (error, GCK_URI_ERROR, GCK_URI_BAD_SCHEME, - _("The URI does not have the 'pkcs11' scheme.")); - break; - case P11_KIT_URI_BAD_SYNTAX: - g_set_error_literal (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX, - _("The URI has bad syntax.")); - break; - case P11_KIT_URI_BAD_VERSION: - g_set_error_literal (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX, - _("The URI has a bad version number.")); - break; - case P11_KIT_URI_NOT_FOUND: - g_assert_not_reached (); - break; - }; - return NULL; - } - - /* Convert it to a GckUri */ - uri_data = gck_uri_data_new (); - if (flags & GCK_URI_FOR_MODULE_WITH_VERSION) - uri_data->module_info = _gck_module_info_from_pkcs11 (p11_kit_uri_get_module_info (p11_uri)); - if (flags & GCK_URI_FOR_TOKEN) - uri_data->token_info = _gck_token_info_from_pkcs11 (p11_kit_uri_get_token_info (p11_uri)); - if (flags & GCK_URI_FOR_OBJECT) { - attrs = p11_kit_uri_get_attributes (p11_uri, &n_attrs); - uri_data->attributes = gck_attributes_new (); - for (i = 0; i < n_attrs; ++i) - gck_attributes_add (uri_data->attributes, (GckAttribute*)&attrs[i]); - } - uri_data->any_unrecognized = p11_kit_uri_any_unrecognized (p11_uri); - - p11_kit_uri_free (p11_uri); - return uri_data; -} - -/** - * gck_uri_build: - * @uri_data: the info to build the URI from. - * @flags: The context that the URI is for - * - * Build a PKCS\#11 URI. The various parts relevant to the flags - * specified will be used to build the URI. - * - * Return value: a newly allocated string containing a PKCS\#11 URI. - */ -gchar* -gck_uri_build (GckUriData *uri_data, GckUriFlags flags) -{ - GckAttribute *attr; - P11KitUri *p11_uri = 0; - gchar *string; - int res; - guint i; - - g_return_val_if_fail (uri_data != NULL, NULL); - - p11_uri = p11_kit_uri_new (); - - if ((flags & GCK_URI_FOR_MODULE_WITH_VERSION) && uri_data->module_info) - _gck_module_info_to_pkcs11 (uri_data->module_info, - p11_kit_uri_get_module_info (p11_uri)); - if ((flags & GCK_URI_FOR_TOKEN) && uri_data->token_info) - _gck_token_info_to_pkcs11 (uri_data->token_info, - p11_kit_uri_get_token_info (p11_uri)); - if ((flags & GCK_URI_FOR_OBJECT) && uri_data->attributes) { - for (i = 0; i < gck_attributes_count (uri_data->attributes); ++i) { - attr = gck_attributes_at (uri_data->attributes, i); - res = p11_kit_uri_set_attribute (p11_uri, (CK_ATTRIBUTE_PTR)attr); - if (res == P11_KIT_URI_NO_MEMORY) - g_error ("failed to allocate memory in p11_kit_uri_set_attribute()"); - else if (res != P11_KIT_URI_NOT_FOUND && res != P11_KIT_URI_OK) - g_return_val_if_reached (NULL); - } - } - - res = p11_kit_uri_format (p11_uri, flags & GCK_URI_FOR_ANY, &string); - if (res == P11_KIT_URI_NO_MEMORY) - g_error ("failed to allocate memory in p11_kit_uri_format()"); - else if (res != P11_KIT_URI_OK) - g_return_val_if_reached (NULL); - - p11_kit_uri_free (p11_uri); - return string; -} - -GType -gck_uri_data_get_type (void) -{ - static volatile gsize initialized = 0; - static GType type = 0; - if (g_once_init_enter (&initialized)) { - type = g_boxed_type_register_static ("GckUriData", - (GBoxedCopyFunc)gck_uri_data_copy, - (GBoxedFreeFunc)gck_uri_data_free); - g_once_init_leave (&initialized, 1); - } - return type; -} - -/** - * gck_uri_data_copy: - * @uri_data: URI data to copy - * - * Copy a #GckUriData - * - * Returns: (transfer full): newly allocated copy of the uri data - */ -GckUriData * -gck_uri_data_copy (GckUriData *uri_data) -{ - GckUriData *copy; - - copy = g_memdup (uri_data, sizeof (GckUriData)); - copy->attributes = gck_attributes_new (); - gck_attributes_add_all (copy->attributes, uri_data->attributes); - copy->module_info = gck_module_info_copy (copy->module_info); - copy->token_info = gck_token_info_copy (copy->token_info); - return copy; -} - -/** - * gck_uri_data_free: - * @uri_data: URI data to free. - * - * Free a #GckUriData. - */ -void -gck_uri_data_free (GckUriData *uri_data) -{ - if (uri_data) { - if (uri_data->attributes) - gck_attributes_unref (uri_data->attributes); - if (uri_data->module_info) - gck_module_info_free (uri_data->module_info); - if (uri_data->token_info) - gck_token_info_free (uri_data->token_info); - g_slice_free (GckUriData, uri_data); - } -} diff --git a/gck/gck.h b/gck/gck.h deleted file mode 100644 index 588a09f9..00000000 --- a/gck/gck.h +++ /dev/null @@ -1,1324 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* gck.h - the GObject PKCS#11 wrapper library - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -#ifndef GCK_H -#define GCK_H - -#include <glib.h> -#include <glib-object.h> -#include <gio/gio.h> - -#include "pkcs11.h" - -#define __GCK_INSIDE_HEADER__ - -#include "gck-deprecated.h" -#include "gck-enum-types.h" - -G_BEGIN_DECLS - -/* - * To use this API, you need to be prepared for changes to the API, - * and add the C flag: -DGCK_API_SUBJECT_TO_CHANGE - */ - -#ifndef GCK_API_SUBJECT_TO_CHANGE -#error "This API has not yet reached stability." -#endif - -#define GCK_VENDOR_CODE 0x47434B00 /* GCK */ - -/* An error code which results from a failure to load the PKCS11 module */ -typedef enum { - GCK_ERROR_MODULE_PROBLEM = (CKR_VENDOR_DEFINED | (GCK_VENDOR_CODE + 1)), -} GckError; - -#define GCK_ERROR (gck_error_get_quark ()) - -GQuark gck_error_get_quark (void) G_GNUC_CONST; - -#define GCK_TYPE_LIST (gck_list_get_boxed_type ()) - -GType gck_list_get_boxed_type (void) G_GNUC_CONST; - -GList* gck_list_ref_copy (GList *reflist); - -void gck_list_unref_free (GList *reflist); - -const gchar* gck_message_from_rv (gulong rv); - -gboolean gck_string_to_chars (guchar *data, - gsize max, - const gchar *string); - -gchar* gck_string_from_chars (const guchar *data, - gsize max); - -typedef gpointer (*GckAllocator) (gpointer data, gsize length); - -typedef enum { - GCK_SESSION_READ_ONLY = 0, - GCK_SESSION_READ_WRITE = 1 << 1, - GCK_SESSION_LOGIN_USER = 1 << 2, - GCK_SESSION_AUTHENTICATE = 1 << 3, -} GckSessionOptions; - -typedef struct _GckMechanism GckMechanism; - -struct _GckMechanism { - gulong type; - gconstpointer parameter; - gulong n_parameter; -}; - -typedef struct _GckAttribute GckAttribute; - -struct _GckAttribute { - gulong type; - guchar *value; - gulong length; -}; - -#define GCK_INVALID G_MAXULONG - -gboolean gck_value_to_ulong (const guchar *value, - gsize length, - gulong *result); - -gboolean gck_value_to_boolean (const guchar *value, - gsize length, - gboolean *result); - -void gck_attribute_init (GckAttribute *attr, - gulong attr_type, - const guchar *value, - gsize length); - -void gck_attribute_init_invalid (GckAttribute *attr, - gulong attr_type); - -void gck_attribute_init_empty (GckAttribute *attr, - gulong attr_type); - -void gck_attribute_init_boolean (GckAttribute *attr, - gulong attr_type, - gboolean value); - -void gck_attribute_init_date (GckAttribute *attr, - gulong attr_type, - const GDate *value); - -void gck_attribute_init_ulong (GckAttribute *attr, - gulong attr_type, - gulong value); - -void gck_attribute_init_string (GckAttribute *attr, - gulong attr_type, - const gchar *value); - -void gck_attribute_init_copy (GckAttribute *dest, - const GckAttribute *src); - -#define GCK_TYPE_ATTRIBUTE (gck_attribute_get_type ()) - -GType gck_attribute_get_type (void) G_GNUC_CONST; - -GckAttribute* gck_attribute_new (gulong attr_type, - gpointer value, - gsize length); - -GckAttribute* gck_attribute_new_invalid (gulong attr_type); - -GckAttribute* gck_attribute_new_empty (gulong attr_type); - -GckAttribute* gck_attribute_new_boolean (gulong attr_type, - gboolean value); - -GckAttribute* gck_attribute_new_date (gulong attr_type, - const GDate *value); - -GckAttribute* gck_attribute_new_ulong (gulong attr_type, - gulong value); - -GckAttribute* gck_attribute_new_string (gulong attr_type, - const gchar *value); - -gboolean gck_attribute_is_invalid (GckAttribute *attr); - -gboolean gck_attribute_get_boolean (GckAttribute *attr); - -gulong gck_attribute_get_ulong (GckAttribute *attr); - -gchar* gck_attribute_get_string (GckAttribute *attr); - -void gck_attribute_get_date (GckAttribute *attr, - GDate* value); - -gboolean gck_attribute_equal (gconstpointer a, - gconstpointer b); - -GckAttribute* gck_attribute_dup (GckAttribute *attr); - -void gck_attribute_clear (GckAttribute *attr); - -void gck_attribute_free (GckAttribute *attr); - -void gck_attribute_dump (GckAttribute *attr); - -typedef struct _GckAttributes GckAttributes; - -#define GCK_TYPE_ATTRIBUTES (gck_attributes_get_boxed_type ()) - -GType gck_attributes_get_type (void) G_GNUC_CONST; - -GckAttributes* gck_attributes_new (void); - -GckAttributes* gck_attributes_new_empty (gulong attr_type, - ...); - -GckAttributes* gck_attributes_new_full (GckAllocator allocator); - -GckAttribute* gck_attributes_at (GckAttributes *attrs, - guint index); - -GckAttribute* gck_attributes_add (GckAttributes *attrs, - GckAttribute *attr); - -void gck_attributes_add_all (GckAttributes *attrs, - GckAttributes *from); - -GckAttribute* gck_attributes_add_data (GckAttributes *attrs, - gulong attr_type, - const guchar *value, - gsize length); - -GckAttribute* gck_attributes_add_invalid (GckAttributes *attrs, - gulong attr_type); - -GckAttribute* gck_attributes_add_empty (GckAttributes *attrs, - gulong attr_type); - -GckAttribute* gck_attributes_add_boolean (GckAttributes *attrs, - gulong attr_type, - gboolean value); - -GckAttribute* gck_attributes_add_string (GckAttributes *attrs, - gulong attr_type, - const gchar *value); - -GckAttribute* gck_attributes_add_date (GckAttributes *attrs, - gulong attr_type, - const GDate *value); - -GckAttribute* gck_attributes_add_ulong (GckAttributes *attrs, - gulong attr_type, - gulong value); - -GckAttribute* gck_attributes_find (GckAttributes *attrs, - gulong attr_type); - -gboolean gck_attributes_find_boolean (GckAttributes *attrs, - gulong attr_type, - gboolean *value); - -gboolean gck_attributes_find_ulong (GckAttributes *attrs, - gulong attr_type, - gulong *value); - -gboolean gck_attributes_find_string (GckAttributes *attrs, - gulong attr_type, - gchar **value); - -gboolean gck_attributes_find_date (GckAttributes *attrs, - gulong attr_type, - GDate *value); - -void gck_attributes_set_boolean (GckAttributes *attrs, - gulong attr_type, - gboolean value); - -void gck_attributes_set_ulong (GckAttributes *attrs, - gulong attr_type, - gulong value); - -void gck_attributes_set_string (GckAttributes *attrs, - gulong attr_type, - const gchar *value); - -void gck_attributes_set_date (GckAttributes *attrs, - gulong attr_type, - const GDate *value); - -gulong gck_attributes_count (GckAttributes *attrs); - -GckAttributes* gck_attributes_ref (GckAttributes *attrs); - -void gck_attributes_unref (GckAttributes *attrs); - -gboolean gck_attributes_contains (GckAttributes *attrs, - GckAttribute *match); - -void gck_attributes_dump (GckAttributes *attrs); - -/* ------------------------------------------------------------------------- - * FORWARDS - */ -typedef struct _GckSlot GckSlot; -typedef struct _GckModule GckModule; -typedef struct _GckSession GckSession; -typedef struct _GckObject GckObject; -typedef struct _GckEnumerator GckEnumerator; -typedef struct _GckUriData GckUriData; - -/* ------------------------------------------------------------------------- - * MODULE - */ - -typedef struct _GckModuleInfo GckModuleInfo; - -struct _GckModuleInfo { - guint8 pkcs11_version_major; - guint8 pkcs11_version_minor; - - gchar *manufacturer_id; - gulong flags; - - gchar *library_description; - guint8 library_version_major; - guint8 library_version_minor; -}; - -#define GCK_TYPE_MODULE_INFO (gck_module_info_get_type ()) - -GType gck_module_info_get_type (void) G_GNUC_CONST; - -GckModuleInfo * gck_module_info_copy (GckModuleInfo *module_info); - -void gck_module_info_free (GckModuleInfo *module_info); - -#define GCK_TYPE_MODULE (gck_module_get_type()) -#define GCK_MODULE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCK_TYPE_MODULE, GckModule)) -#define GCK_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCK_TYPE_MODULE, GckModule)) -#define GCK_IS_MODULE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCK_TYPE_MODULE)) -#define GCK_IS_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCK_TYPE_MODULE)) -#define GCK_MODULE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCK_TYPE_MODULE, GckModuleClass)) - -typedef struct _GckModuleClass GckModuleClass; -typedef struct _GckModulePrivate GckModulePrivate; - -struct _GckModule { - GObject parent; - - /*< private >*/ - GckModulePrivate *pv; - gpointer reserved[4]; -}; - -struct _GckModuleClass { - GObjectClass parent; - - gboolean (*authenticate_slot) (GckModule *self, GckSlot *slot, gchar *label, gchar **password); - - gboolean (*authenticate_object) (GckModule *self, GckObject *object, gchar *label, gchar **password); - - /*< private >*/ - gpointer reserved[8]; -}; - -GType gck_module_get_type (void) G_GNUC_CONST; - -GckModule* gck_module_new (CK_FUNCTION_LIST_PTR funcs); - -GckModule* gck_module_initialize (const gchar *path, - GCancellable *cancellable, - GError **error); - -void gck_module_initialize_async (const gchar *path, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckModule * gck_module_initialize_finish (GAsyncResult *result, - GError **error); - -gboolean gck_module_equal (gconstpointer module1, - gconstpointer module2); - -guint gck_module_hash (gconstpointer module); - -gboolean gck_module_match (GckModule *self, - GckUriData *uri); - -const gchar* gck_module_get_path (GckModule *self); - -CK_FUNCTION_LIST_PTR gck_module_get_functions (GckModule *self); - -GckModuleInfo* gck_module_get_info (GckModule *self); - -GList* gck_module_get_slots (GckModule *self, - gboolean token_present); - -GList* gck_modules_initialize_registered (GCancellable *cancellable, - GError **error); - -void gck_modules_initialize_registered_async (GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GList * gck_modules_initialize_registered_finish (GAsyncResult *result, - GError **error); - -GList* gck_modules_get_slots (GList *modules, - gboolean token_present); - -GckEnumerator* gck_modules_enumerate_objects (GList *modules, - GckAttributes *attrs, - GckSessionOptions session_options); - -GckSlot* gck_modules_token_for_uri (GList *modules, - const gchar *uri, - GError **error); - -GList * gck_modules_tokens_for_uri (GList *modules, - const gchar *uri, - GError **error); - -GckObject* gck_modules_object_for_uri (GList *modules, - const gchar *uri, - GckSessionOptions session_options, - GError **error); - -GList* gck_modules_objects_for_uri (GList *modules, - const gchar *uri, - GckSessionOptions session_options, - GError **error); - -GckEnumerator* gck_modules_enumerate_uri (GList *modules, - const gchar *uri, - GckSessionOptions session_options, - GError **error); - - -/* ------------------------------------------------------------------------ - * ENUMERATOR - */ - -#define GCK_TYPE_ENUMERATOR (gck_enumerator_get_type()) -#define GCK_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCK_TYPE_ENUMERATOR, GckEnumerator)) -#define GCK_ENUMERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCK_TYPE_ENUMERATOR, GckEnumerator)) -#define GCK_IS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCK_TYPE_ENUMERATOR)) -#define GCK_IS_ENUMERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCK_TYPE_ENUMERATOR)) -#define GCK_ENUMERATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCK_TYPE_ENUMERATOR, GckEnumeratorClass)) - -typedef struct _GckEnumeratorClass GckEnumeratorClass; -typedef struct _GckEnumeratorPrivate GckEnumeratorPrivate; - -struct _GckEnumerator { - GObject parent; - - /*< private >*/ - GckEnumeratorPrivate *pv; - gpointer reserved[2]; -}; - -struct _GckEnumeratorClass { - GObjectClass parent; - - /*< private >*/ - gpointer reserved[2]; -}; - -GType gck_enumerator_get_type (void) G_GNUC_CONST; - -GTlsInteraction * gck_enumerator_get_interaction (GckEnumerator *self); - -void gck_enumerator_set_interaction (GckEnumerator *self, - GTlsInteraction *interaction); - -GckObject* gck_enumerator_next (GckEnumerator *self, - GCancellable *cancellable, - GError **error); - -GList* gck_enumerator_next_n (GckEnumerator *self, - gint max_objects, - GCancellable *cancellable, - GError **error); - -void gck_enumerator_next_async (GckEnumerator *self, - gint max_objects, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GList* gck_enumerator_next_finish (GckEnumerator *self, - GAsyncResult *result, - GError **error); - -/* ------------------------------------------------------------------------ - * SLOT - */ - -typedef struct _GckSlotInfo GckSlotInfo; - -struct _GckSlotInfo { - gchar *slot_description; - gchar *manufacturer_id; - gulong flags; - guint8 hardware_version_major; - guint8 hardware_version_minor; - guint8 firmware_version_major; - guint8 firmware_version_minor; -}; - -#define GCK_TYPE_SLOT_INFO (gck_slot_info_get_type ()) - -GType gck_slot_info_get_type (void) G_GNUC_CONST; - -GckSlotInfo * gck_slot_info_copy (GckSlotInfo *slot_info); - -void gck_slot_info_free (GckSlotInfo *slot_info); - -typedef struct _GckTokenInfo GckTokenInfo; - -struct _GckTokenInfo { - gchar *label; - gchar *manufacturer_id; - gchar *model; - gchar *serial_number; - gulong flags; - glong max_session_count; - glong session_count; - glong max_rw_session_count; - glong rw_session_count; - glong max_pin_len; - glong min_pin_len; - glong total_public_memory; - glong free_public_memory; - glong total_private_memory; - glong free_private_memory; - guint8 hardware_version_major; - guint8 hardware_version_minor; - guint8 firmware_version_major; - guint8 firmware_version_minor; - gint64 utc_time; -}; - -#define GCK_TYPE_TOKEN_INFO (gck_token_info_get_type ()) - -GType gck_token_info_get_type (void) G_GNUC_CONST; - -GckTokenInfo * gck_token_info_copy (GckTokenInfo *token_info); - -void gck_token_info_free (GckTokenInfo *token_info); - -typedef struct _GckMechanismInfo GckMechanismInfo; - -struct _GckMechanismInfo { - gulong min_key_size; - gulong max_key_size; - gulong flags; -}; - -#define GCK_TYPE_MECHANISM_INFO (gck_mechanism_info_get_type ()) - -GType gck_mechanism_info_get_type (void) G_GNUC_CONST; - -GckMechanismInfo * gck_mechanism_info_copy (GckMechanismInfo *mech_info); - -void gck_mechanism_info_free (GckMechanismInfo *mech_info); - -#define gck_mechanisms_length(a) ((a)->len) - -#define gck_mechanisms_at(a, i) (g_array_index (a, CK_MECHANISM_TYPE, i)) - -gboolean gck_mechanisms_check (GArray *mechanisms, - ...); - -#define GCK_TYPE_SLOT (gck_slot_get_type()) -#define GCK_SLOT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCK_TYPE_SLOT, GckSlot)) -#define GCK_SLOT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCK_TYPE_SLOT, GckSlot)) -#define GCK_IS_SLOT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCK_TYPE_SLOT)) -#define GCK_IS_SLOT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCK_TYPE_SLOT)) -#define GCK_SLOT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCK_TYPE_SLOT, GckSlotClass)) - -typedef struct _GckSlotClass GckSlotClass; -typedef struct _GckSlotPrivate GckSlotPrivate; - -struct _GckSlot { - GObject parent; - - /*< private >*/ - GckSlotPrivate *pv; - gpointer reserved[4]; -}; - -struct _GckSlotClass { - GObjectClass parent; - - /*< private >*/ - gpointer reserved[9]; -}; - -GType gck_slot_get_type (void) G_GNUC_CONST; - -gboolean gck_slot_equal (gconstpointer slot1, - gconstpointer slot2); - -guint gck_slot_hash (gconstpointer slot); - -gboolean gck_slot_match (GckSlot *self, - GckUriData *uri); - -GckSlot* gck_slot_from_handle (GckModule *module, - gulong slot_id); - -GckModule* gck_slot_get_module (GckSlot *self); - -gulong gck_slot_get_handle (GckSlot *self); - -GckSlotInfo* gck_slot_get_info (GckSlot *self); - -GckTokenInfo* gck_slot_get_token_info (GckSlot *self); - -GArray * gck_slot_get_mechanisms (GckSlot *self); - -GckMechanismInfo* gck_slot_get_mechanism_info (GckSlot *self, - gulong mech_type); - -gboolean gck_slot_has_flags (GckSlot *self, - gulong flags); - -GTlsInteraction * gck_slot_get_interaction (GckSlot *self); - -void gck_slot_set_interaction (GckSlot *self, - GTlsInteraction *interaction); - -GckSession* gck_slot_open_session (GckSlot *self, - GckSessionOptions options, - GCancellable *cancellable, - GError **error); - -GckSession* gck_slot_open_session_full (GckSlot *self, - GckSessionOptions options, - gulong pkcs11_flags, - gpointer app_data, - CK_NOTIFY notify, - GCancellable *cancellable, - GError **error); - -void gck_slot_open_session_async (GckSlot *self, - GckSessionOptions options, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -void gck_slot_open_session_full_async (GckSlot *self, - GckSessionOptions options, - gulong pkcs11_flags, - gpointer app_data, - CK_NOTIFY notify, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckSession* gck_slot_open_session_finish (GckSlot *self, - GAsyncResult *result, - GError **error); - -GckEnumerator* gck_slots_enumerate_objects (GList *slots, - GckAttributes *attrs, - GckSessionOptions options); - -/* ------------------------------------------------------------------------ - * SESSION - */ - -typedef struct _GckSessionInfo GckSessionInfo; - -struct _GckSessionInfo { - gulong slot_id; - gulong state; - gulong flags; - gulong device_error; -}; - -#define GCK_TYPE_SESSION_INFO (gck_session_info_get_type ()) - -GType gck_session_info_get_type (void) G_GNUC_CONST; - -GckSessionInfo * gck_session_info_copy (GckSessionInfo *session_info); - -void gck_session_info_free (GckSessionInfo *session_info); - -#define GCK_TYPE_SESSION (gck_session_get_type()) -#define GCK_SESSION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCK_TYPE_SESSION, GckSession)) -#define GCK_SESSION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCK_TYPE_SESSION, GckSession)) -#define GCK_IS_SESSION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCK_TYPE_SESSION)) -#define GCK_IS_SESSION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCK_TYPE_SESSION)) -#define GCK_SESSION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCK_TYPE_SESSION, GckSessionClass)) - -typedef struct _GckSessionClass GckSessionClass; -typedef struct _GckSessionPrivate GckSessionPrivate; - -struct _GckSession { - GObject parent; - - /*< private >*/ - GckSessionPrivate *pv; - gpointer reserved[4]; -}; - -struct _GckSessionClass { - GObjectClass parent; - - gboolean (*discard_handle) (GckSession *session, CK_SESSION_HANDLE handle); - - /*< private >*/ - gpointer reserved[8]; -}; - -GType gck_session_get_type (void) G_GNUC_CONST; - -GckSession * gck_session_from_handle (GckSlot *slot, - gulong session_handle, - GckSessionOptions options); - -GckModule* gck_session_get_module (GckSession *self); - -GckSlot* gck_session_get_slot (GckSession *self); - -gulong gck_session_get_handle (GckSession *self); - -GckSessionInfo* gck_session_get_info (GckSession *self); - -gulong gck_session_get_state (GckSession *self); - -GckSessionOptions gck_session_get_options (GckSession *self); - -GTlsInteraction * gck_session_get_interaction (GckSession *self); - -gboolean gck_session_init_pin (GckSession *self, - const guchar *pin, - gsize n_pin, - GCancellable *cancellable, - GError **error); - -void gck_session_init_pin_async (GckSession *self, - const guchar *pin, - gsize n_pin, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_session_init_pin_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -gboolean gck_session_set_pin (GckSession *self, - const guchar *old_pin, - gsize n_old_pin, - const guchar *new_pin, - gsize n_new_pin, - GCancellable *cancellable, - GError **error); - -void gck_session_set_pin_async (GckSession *self, - const guchar *old_pin, - gsize n_old_pin, - const guchar *new_pin, - gsize n_new_pin, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_session_set_pin_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -gboolean gck_session_login (GckSession *self, - gulong user_type, - const guchar *pin, - gsize n_pin, - GCancellable *cancellable, - GError **error); - -void gck_session_login_async (GckSession *self, - gulong user_type, - const guchar *pin, - gsize n_pin, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_session_login_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -gboolean gck_session_logout (GckSession *self, - GCancellable *cancellable, - GError **error); - -void gck_session_logout_async (GckSession *self, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_session_logout_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -GckObject* gck_session_create_object (GckSession *self, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -void gck_session_create_object_async (GckSession *self, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckObject* gck_session_create_object_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -GList* gck_session_find_objects (GckSession *self, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -void gck_session_find_objects_async (GckSession *self, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GList* gck_session_find_objects_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -gboolean gck_session_generate_key_pair (GckSession *self, - gulong mech_type, - GckAttributes *public_attrs, - GckAttributes *private_attrs, - GckObject **public_key, - GckObject **private_key, - GCancellable *cancellable, - GError **error); - -gboolean gck_session_generate_key_pair_full (GckSession *self, - GckMechanism *mechanism, - GckAttributes *public_attrs, - GckAttributes *private_attrs, - GckObject **public_key, - GckObject **private_key, - GCancellable *cancellable, - GError **error); - -void gck_session_generate_key_pair_async (GckSession *self, - GckMechanism *mechanism, - GckAttributes *public_attrs, - GckAttributes *private_attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_session_generate_key_pair_finish (GckSession *self, - GAsyncResult *result, - GckObject **public_key, - GckObject **private_key, - GError **error); - -guchar* gck_session_encrypt (GckSession *self, - GckObject *key, - gulong mech_type, - const guchar *input, - gsize n_input, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -guchar* gck_session_encrypt_full (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -void gck_session_encrypt_async (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -guchar* gck_session_encrypt_finish (GckSession *self, - GAsyncResult *result, - gsize *n_result, - GError **error); - -guchar* gck_session_decrypt (GckSession *self, - GckObject *key, - gulong mech_type, - const guchar *input, - gsize n_input, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -guchar* gck_session_decrypt_full (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -void gck_session_decrypt_async (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -guchar* gck_session_decrypt_finish (GckSession *self, - GAsyncResult *result, - gsize *n_result, - GError **error); - -guchar* gck_session_sign (GckSession *self, - GckObject *key, - gulong mech_type, - const guchar *input, - gsize n_input, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -guchar* gck_session_sign_full (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -void gck_session_sign_async (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -guchar* gck_session_sign_finish (GckSession *self, - GAsyncResult *result, - gsize *n_result, - GError **error); - -gboolean gck_session_verify (GckSession *self, - GckObject *key, - gulong mech_type, - const guchar *input, - gsize n_input, - const guchar *signature, - gsize n_signature, - GCancellable *cancellable, - GError **error); - -gboolean gck_session_verify_full (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - const guchar *signature, - gsize n_signature, - GCancellable *cancellable, - GError **error); - -void gck_session_verify_async (GckSession *self, - GckObject *key, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - const guchar *signature, - gsize n_signature, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_session_verify_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -guchar * gck_session_wrap_key (GckSession *self, - GckObject *wrapper, - gulong mech_type, - GckObject *wrapped, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -guchar * gck_session_wrap_key_full (GckSession *self, - GckObject *wrapper, - GckMechanism *mechanism, - GckObject *wrapped, - gsize *n_result, - GCancellable *cancellable, - GError **error); - -void gck_session_wrap_key_async (GckSession *self, - GckObject *wrapper, - GckMechanism *mechanism, - GckObject *wrapped, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -guchar * gck_session_wrap_key_finish (GckSession *self, - GAsyncResult *result, - gsize *n_result, - GError **error); - -GckObject* gck_session_unwrap_key (GckSession *self, - GckObject *wrapper, - gulong mech_type, - const guchar *input, - gsize n_input, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -GckObject* gck_session_unwrap_key_full (GckSession *self, - GckObject *wrapper, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -void gck_session_unwrap_key_async (GckSession *self, - GckObject *wrapper, - GckMechanism *mechanism, - const guchar *input, - gsize n_input, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckObject* gck_session_unwrap_key_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -GckObject* gck_session_derive_key (GckSession *self, - GckObject *base, - gulong mech_type, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -GckObject* gck_session_derive_key_full (GckSession *self, - GckObject *base, - GckMechanism *mechanism, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -void gck_session_derive_key_async (GckSession *self, - GckObject *base, - GckMechanism *mechanism, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckObject* gck_session_derive_key_finish (GckSession *self, - GAsyncResult *result, - GError **error); - -/* ------------------------------------------------------------------------ - * OBJECT - */ - -#define GCK_TYPE_OBJECT (gck_object_get_type()) -#define GCK_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCK_TYPE_OBJECT, GckObject)) -#define GCK_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCK_TYPE_OBJECT, GckObject)) -#define GCK_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCK_TYPE_OBJECT)) -#define GCK_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCK_TYPE_OBJECT)) -#define GCK_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCK_TYPE_OBJECT, GckObjectClass)) - -typedef struct _GckObjectClass GckObjectClass; -typedef struct _GckObjectPrivate GckObjectPrivate; - -struct _GckObject { - GObject parent; - - /*< private >*/ - GckObjectPrivate *pv; - gpointer reserved[4]; -}; - -struct _GckObjectClass { - GObjectClass parent; - gpointer reserved[8]; -}; - -GType gck_object_get_type (void) G_GNUC_CONST; - -GckObject * gck_object_from_handle (GckSession *session, - gulong object_handle); - -GList* gck_objects_from_handle_array (GckSession *session, - gulong *object_handles, - gulong n_object_handles); - -gboolean gck_object_equal (gconstpointer object1, - gconstpointer object2); - -guint gck_object_hash (gconstpointer object); - -GckModule* gck_object_get_module (GckObject *self); - -gulong gck_object_get_handle (GckObject *self); - -GckSession* gck_object_get_session (GckObject *self); - -gboolean gck_object_destroy (GckObject *self, - GCancellable *cancellable, - GError **error); - -void gck_object_destroy_async (GckObject *self, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_object_destroy_finish (GckObject *self, - GAsyncResult *result, - GError **error); - -gboolean gck_object_set (GckObject *self, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -void gck_object_set_async (GckObject *self, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_object_set_finish (GckObject *self, - GAsyncResult *result, - GError **error); - -GckAttributes* gck_object_get (GckObject *self, - GCancellable *cancellable, - GError **error, - ...); - -GckAttributes* gck_object_get_full (GckObject *self, - gulong *attr_types, - guint n_attr_types, - GCancellable *cancellable, - GError **error); - -void gck_object_get_async (GckObject *self, - gulong *attr_types, - guint n_attr_types, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckAttributes* gck_object_get_finish (GckObject *self, - GAsyncResult *result, - GError **error); - -guchar * gck_object_get_data (GckObject *self, - gulong attr_type, - GCancellable *cancellable, - gsize *n_data, - GError **error); - -guchar * gck_object_get_data_full (GckObject *self, - gulong attr_type, - GckAllocator allocator, - GCancellable *cancellable, - gsize *n_data, - GError **error); - -void gck_object_get_data_async (GckObject *self, - gulong attr_type, - GckAllocator allocator, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -guchar * gck_object_get_data_finish (GckObject *self, - GAsyncResult *result, - gsize *n_data, - GError **error); - -gboolean gck_object_set_template (GckObject *self, - gulong attr_type, - GckAttributes *attrs, - GCancellable *cancellable, - GError **error); - -void gck_object_set_template_async (GckObject *self, - gulong attr_type, - GckAttributes *attrs, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean gck_object_set_template_finish (GckObject *self, - GAsyncResult *result, - GError **error); - -GckAttributes* gck_object_get_template (GckObject *self, - gulong attr_type, - GCancellable *cancellable, - GError **error); - -void gck_object_get_template_async (GckObject *self, - gulong attr_type, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -GckAttributes* gck_object_get_template_finish (GckObject *self, - GAsyncResult *result, - GError **error); - -/* ------------------------------------------------------------------------ - * PASSWORD - */ - -#define GCK_TYPE_PASSWORD (gck_password_get_type ()) -#define GCK_PASSWORD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCK_TYPE_PASSWORD, GckPassword)) -#define GCK_PASSWORD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCK_TYPE_PASSWORD, GckPassword)) -#define GCK_IS_PASSWORD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCK_TYPE_PASSWORD)) -#define GCK_IS_PASSWORD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCK_TYPE_PASSWORD)) -#define GCK_PASSWORD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCK_TYPE_PASSWORD, GckPasswordClass)) - -typedef struct _GckPassword GckPassword; -typedef struct _GckPasswordClass GckPasswordClass; -typedef struct _GckPasswordPrivate GckPasswordPrivate; - -struct _GckPassword { - GTlsPassword parent; - - /*< private >*/ - GckPasswordPrivate *pv; - gpointer reserved[4]; -}; - -struct _GckPasswordClass { - GTlsPasswordClass parent; - - /*< private >*/ - gpointer reserved[4]; -}; - -GType gck_password_get_type (void) G_GNUC_CONST; - -GckModule * gck_password_get_module (GckPassword *self); - -GckSlot * gck_password_get_token (GckPassword *self); - -GckObject * gck_password_get_key (GckPassword *self); - -/* ---------------------------------------------------------------------------- - * URI - */ - -typedef enum { - GCK_URI_BAD_SCHEME = 1, - GCK_URI_BAD_ENCODING, - GCK_URI_BAD_SYNTAX, - GCK_URI_BAD_VERSION, - GCK_URI_NOT_FOUND -} GckUriError; - -/* WARNING: Don't modify these without syncing with p11-kit */ -typedef enum { - GCK_URI_FOR_OBJECT = (1 << 1), - GCK_URI_FOR_TOKEN = (1 << 2), - GCK_URI_FOR_MODULE = (1 << 3), - GCK_URI_WITH_VERSION = (1 << 4), - GCK_URI_FOR_ANY = 0x0000FFFF, -} GckUriFlags; - -#define GCK_URI_FOR_MODULE_WITH_VERSION (GCK_URI_WITH_VERSION | GCK_URI_FOR_MODULE) - -#define GCK_URI_FOR_OBJECT_ON_TOKEN (GCK_URI_FOR_OBJECT | GCK_URI_FOR_TOKEN) - -#define GCK_URI_FOR_OBJECT_ON_TOKEN_AND_MODULE (GCK_URI_FOR_OBJECT_ON_TOKEN | GCK_URI_FOR_MODULE) - -struct _GckUriData { - gboolean any_unrecognized; - GckModuleInfo *module_info; - GckTokenInfo *token_info; - GckAttributes *attributes; - - /*< private >*/ - gpointer dummy[4]; -}; - -#define GCK_URI_ERROR (gck_uri_error_get_quark ()) - -GQuark gck_uri_error_get_quark (void) G_GNUC_CONST; - -GckUriData* gck_uri_data_new (void); - -gchar* gck_uri_build (GckUriData *uri_data, - GckUriFlags flags); - -GckUriData* gck_uri_parse (const gchar *string, - GckUriFlags flags, - GError **error); - -#define GCK_URI_DATA_TYPE (gck_uri_data_get_type ()) - -GType gck_uri_data_get_type (void) G_GNUC_CONST; - -GckUriData * gck_uri_data_copy (GckUriData *uri_data); - -void gck_uri_data_free (GckUriData *uri_data); - -G_END_DECLS - -#undef __GCK_INSIDE_HEADER__ - -#endif /* GCK_H */ diff --git a/gck/gck.pc.in b/gck/gck.pc.in deleted file mode 100644 index 1f0f8acf..00000000 --- a/gck/gck.pc.in +++ /dev/null @@ -1,14 +0,0 @@ -prefix=@prefix@ -exec_prefix=@exec_prefix@ -libdir=@libdir@ -includedir=@includedir@ -datarootdir=@datarootdir@ -datadir=@datadir@ -sysconfdir=@sysconfdir@ - -Name: gck-@GCK_MAJOR@ -Description: GObject bindings for PKCS#11 -Version: @VERSION@ -Requires: glib-2.0 p11-kit-1 -Libs: -L${libdir} -lgck-@GCK_MAJOR@ -Cflags: -I${includedir}/gck-@GCK_MAJOR@ diff --git a/gck/gck.symbols b/gck/gck.symbols deleted file mode 100644 index e5cb4036..00000000 --- a/gck/gck.symbols +++ /dev/null @@ -1,237 +0,0 @@ -/* This file lists all exported symbols. It is used to guarantee that we - * only consciously change our ABI. - */ - -gck_attribute_clear -gck_attribute_dump -gck_attribute_dup -gck_attribute_equal -gck_attribute_free -gck_attribute_get_boolean -gck_attribute_get_date -gck_attribute_get_string -gck_attribute_get_type -gck_attribute_get_ulong -gck_attribute_init -gck_attribute_init_boolean -gck_attribute_init_copy -gck_attribute_init_date -gck_attribute_init_empty -gck_attribute_init_invalid -gck_attribute_init_string -gck_attribute_init_ulong -gck_attribute_is_invalid -gck_attribute_new -gck_attribute_new_boolean -gck_attribute_new_date -gck_attribute_new_empty -gck_attribute_new_invalid -gck_attribute_new_string -gck_attribute_new_ulong -gck_attributes_add -gck_attributes_add_all -gck_attributes_add_boolean -gck_attributes_add_data -gck_attributes_add_date -gck_attributes_add_empty -gck_attributes_add_invalid -gck_attributes_add_string -gck_attributes_add_ulong -gck_attributes_at -gck_attributes_contains -gck_attributes_count -gck_attributes_dump -gck_attributes_find -gck_attributes_find_boolean -gck_attributes_find_date -gck_attributes_find_string -gck_attributes_find_ulong -gck_attributes_get_boxed_type -gck_attributes_get_type -gck_attributes_new -gck_attributes_new_empty -gck_attributes_new_full -gck_attributes_ref -gck_attributes_set_boolean -gck_attributes_set_date -gck_attributes_set_string -gck_attributes_set_ulong -gck_attributes_unref -gck_enumerator_get_interaction -gck_enumerator_get_type -gck_enumerator_next -gck_enumerator_next_async -gck_enumerator_next_finish -gck_enumerator_next_n -gck_enumerator_set_interaction -gck_error_get_quark -gck_error_get_type -gck_get_error_quark -gck_list_ref_copy -gck_list_unref_free -gck_list_get_boxed_type -gck_mechanism_info_copy -gck_mechanism_info_free -gck_mechanism_info_get_type -gck_mechanisms_check -gck_message_from_rv -gck_module_equal -gck_module_get_functions -gck_module_get_info -gck_module_get_path -gck_module_get_slots -gck_module_get_type -gck_module_hash -gck_module_info_copy -gck_module_info_free -gck_module_info_get_type -gck_module_initialize -gck_module_initialize_async -gck_module_initialize_finish -gck_module_match -gck_module_new -gck_modules_enumerate_objects -gck_modules_enumerate_uri -gck_modules_get_slots -gck_modules_initialize_registered -gck_modules_initialize_registered_async -gck_modules_initialize_registered_finish -gck_modules_object_for_uri -gck_modules_objects_for_uri -gck_modules_token_for_uri -gck_modules_tokens_for_uri -gck_object_destroy -gck_object_destroy_async -gck_object_destroy_finish -gck_object_equal -gck_object_from_handle -gck_object_get -gck_object_get_async -gck_object_get_data -gck_object_get_data_async -gck_object_get_data_finish -gck_object_get_data_full -gck_object_get_finish -gck_object_get_full -gck_object_get_handle -gck_object_get_module -gck_object_get_session -gck_object_get_template -gck_object_get_template_async -gck_object_get_template_finish -gck_object_get_type -gck_object_hash -gck_object_set -gck_object_set_async -gck_object_set_finish -gck_object_set_template -gck_object_set_template_async -gck_object_set_template_finish -gck_objects_from_handle_array -gck_password_get_key -gck_password_get_module -gck_password_get_token -gck_password_get_type -gck_session_create_object -gck_session_create_object_async -gck_session_create_object_finish -gck_session_decrypt -gck_session_decrypt_async -gck_session_decrypt_finish -gck_session_decrypt_full -gck_session_derive_key -gck_session_derive_key_async -gck_session_derive_key_finish -gck_session_derive_key_full -gck_session_encrypt -gck_session_encrypt_async -gck_session_encrypt_finish -gck_session_encrypt_full -gck_session_find_objects -gck_session_find_objects_async -gck_session_find_objects_finish -gck_session_from_handle -gck_session_generate_key_pair -gck_session_generate_key_pair_async -gck_session_generate_key_pair_finish -gck_session_generate_key_pair_full -gck_session_get_handle -gck_session_get_info -gck_session_get_interaction -gck_session_get_module -gck_session_get_options -gck_session_get_slot -gck_session_get_state -gck_session_get_type -gck_session_info_copy -gck_session_info_free -gck_session_info_get_type -gck_session_init_pin -gck_session_init_pin_async -gck_session_init_pin_finish -gck_session_login -gck_session_login_async -gck_session_login_finish -gck_session_logout -gck_session_logout_async -gck_session_logout_finish -gck_session_options_get_type -gck_session_set_pin -gck_session_set_pin_async -gck_session_set_pin_finish -gck_session_sign -gck_session_sign_async -gck_session_sign_finish -gck_session_sign_full -gck_session_unwrap_key -gck_session_unwrap_key_async -gck_session_unwrap_key_finish -gck_session_unwrap_key_full -gck_session_verify -gck_session_verify_async -gck_session_verify_finish -gck_session_verify_full -gck_session_wrap_key -gck_session_wrap_key_async -gck_session_wrap_key_finish -gck_session_wrap_key_full -gck_slot_equal -gck_slot_from_handle -gck_slot_get_handle -gck_slot_get_info -gck_slot_get_interaction -gck_slot_get_mechanism_info -gck_slot_get_mechanisms -gck_slot_get_module -gck_slot_get_token_info -gck_slot_get_type -gck_slot_has_flags -gck_slot_hash -gck_slot_info_copy -gck_slot_info_free -gck_slot_info_get_type -gck_slot_match -gck_slot_open_session -gck_slot_open_session_async -gck_slot_open_session_finish -gck_slot_open_session_full -gck_slot_open_session_full_async -gck_slot_set_interaction -gck_slots_enumerate_objects -gck_string_from_chars -gck_string_to_chars -gck_token_info_copy -gck_token_info_free -gck_token_info_get_type -gck_uri_build -gck_uri_data_copy -gck_uri_data_free -gck_uri_data_get_type -gck_uri_data_new -gck_uri_error_get_quark -gck_uri_error_get_type -gck_uri_flags_get_type -gck_uri_get_error_quark -gck_uri_parse -gck_value_to_boolean -gck_value_to_ulong diff --git a/gck/pkcs11-trust-assertions.h b/gck/pkcs11-trust-assertions.h deleted file mode 100644 index ed8bb6bd..00000000 --- a/gck/pkcs11-trust-assertions.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * pkcs11x.h - * Copyright 2010 Collabora, Ltd - * - * This file is free software; as a special exception the author gives - * unlimited permission to copy and/or distribute it, with or without - * modifications, as long as this notice is preserved. - * - * This file is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY, to the extent permitted by law; without even - * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. - */ - -/* - * The latest version of this file is at: - * - * git://thewalter.net/git/pkcs11-trust-assertions - * - * or viewable on the web at: - * - * http://thewalter.net/git/cgit.cgi/pkcs11-trust-assertions/tree/pkcs11-trust-assertions.h - * - */ - -#ifndef PKCS11_TRUST_ASSERTIONS_H -#define PKCS11_TRUST_ASSERTIONS_H - -#include <p11-kit/pkcs11.h> - -#define CKA_XDG (CKA_VENDOR_DEFINED | 0x58444700UL /* XDG0 */ ) -#define CKO_XDG (CKA_VENDOR_DEFINED | 0x58444700UL /* XDG0 */ ) - -/* ------------------------------------------------------------------- - * TRUST ASSERTIONS - */ - -#define CKO_X_TRUST_ASSERTION (CKO_XDG + 100) - -#define CKA_X_ASSERTION_TYPE (CKA_XDG + 1) - -#define CKA_X_CERTIFICATE_VALUE (CKA_XDG + 2) - -#define CKA_X_PURPOSE (CKA_XDG + 3) - -#define CKA_X_PEER (CKA_XDG + 4) - -typedef CK_ULONG CK_X_ASSERTION_TYPE; - -#define CKT_X_UNTRUSTED_CERTIFICATE 1UL - -#define CKT_X_PINNED_CERTIFICATE 2UL - -#define CKT_X_ANCHORED_CERTIFICATE 3UL - -#endif /* PKCS11_TRUST_ASSERTIONS_H */ diff --git a/gck/pkcs11.h b/gck/pkcs11.h deleted file mode 100644 index 9a34c588..00000000 --- a/gck/pkcs11.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* pkcs11.h - Dummy pkcs11 file while building - - Copyright (C) 2008, Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <nielsen@memberwebs.com> -*/ - -/* - * This file is not installed. The one pkcs11/pkcs11.h is installed with the - * gck.h header. However while building we included it here, so that relative - * includes work from within gck.h - */ - -#include "pkcs11/pkcs11.h" diff --git a/gck/tests/Makefile.am b/gck/tests/Makefile.am deleted file mode 100644 index 734e9d86..00000000 --- a/gck/tests/Makefile.am +++ /dev/null @@ -1,58 +0,0 @@ - -INCLUDES = \ - -I$(top_builddir) \ - -I$(top_srcdir) \ - -DSRCDIR="\"@abs_srcdir@\"" \ - -DBUILDDIR="\"$(builddir)\"" \ - -DGCK_API_SUBJECT_TO_CHANGE \ - $(GLIB_CFLAGS) - -LDADD = \ - libmock-test-module.la \ - $(top_builddir)/gck/libgck-testable.la \ - $(top_builddir)/egg/libegg-test.la \ - $(top_builddir)/egg/libegg-hex.la \ - $(GTHREAD_LIBS) \ - $(GLIB_LIBS) \ - $(GIO_LIBS) - -TEST_PROGS = \ - test-gck-attributes \ - test-gck-module \ - test-gck-slot \ - test-gck-session \ - test-gck-object \ - test-gck-crypto \ - test-gck-uri \ - test-gck-enumerator \ - test-gck-modules - -test_gck_enumerator_SOURCES = \ - test-gck-enumerator.c \ - mock-interaction.c mock-interaction.h - -check_PROGRAMS = $(TEST_PROGS) - -test: $(TEST_PROGS) - gtester --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) - -check-local: test - -all-local: $(check_PROGRAMS) - -lib_LTLIBRARIES = libmock-test-module.la - -libmock_test_module_la_LDFLAGS = \ - -avoid-version - -libmock_test_module_la_CFLAGS = \ - -I$(top_srcdir)/gck \ - -I$(top_srcdir) \ - $(GLIB_CFLAGS) \ - -DGCK_API_SUBJECT_TO_CHANGE - -libmock_test_module_la_SOURCES = \ - mock-test-module.c - -libmock_test_module_la_LIBADD = \ - $(top_builddir)/gck/libgck-testable.la diff --git a/gck/tests/mock-interaction.c b/gck/tests/mock-interaction.c deleted file mode 100644 index ed25a47f..00000000 --- a/gck/tests/mock-interaction.c +++ /dev/null @@ -1,97 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* mock-interaction.c - - Copyright (C) 2011 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "mock-interaction.h" - -#define MOCK_INTERACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOCK_TYPE_INTERACTION, MockInteraction)) -#define MOCK_IS_INTERACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOCK_TYPE_INTERACTION)) -#define MOCK_INTERACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOCK_TYPE_INTERACTION, MockInteractionClass)) - -typedef struct _MockInteractionClass MockInteractionClass; - -struct _MockInteraction { - GTlsInteraction interaction; - gchar *password; -}; - -struct _MockInteractionClass { - GTlsInteractionClass parent; -}; - -G_DEFINE_TYPE (MockInteraction, mock_interaction, G_TYPE_TLS_INTERACTION); - -static void -mock_interaction_init (MockInteraction *self) -{ - -} - -static void -mock_interaction_finalize (GObject *obj) -{ - MockInteraction *self = MOCK_INTERACTION (obj); - - g_free (self->password); - - G_OBJECT_CLASS (mock_interaction_parent_class)->dispose (obj); -} - -static GTlsInteractionResult -mock_interaction_ask_password (GTlsInteraction *interaction, - GTlsPassword *password, - GCancellable *cancellable, - GError **error) -{ - MockInteraction *self = MOCK_INTERACTION (interaction); - - if (self->password) { - g_tls_password_set_value (password, (const guchar *)self->password, -1); - return G_TLS_INTERACTION_HANDLED; - } else { - return G_TLS_INTERACTION_UNHANDLED; - } -} - -static void -mock_interaction_class_init (MockInteractionClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass); - - object_class->finalize = mock_interaction_finalize; - - interaction_class->ask_password = mock_interaction_ask_password; -} - -GTlsInteraction * -mock_interaction_new (const gchar *password) -{ - MockInteraction *result; - - result = g_object_new (MOCK_TYPE_INTERACTION, NULL); - result->password = g_strdup (password); - - return G_TLS_INTERACTION (result); -} diff --git a/gck/tests/mock-interaction.h b/gck/tests/mock-interaction.h deleted file mode 100644 index 0747f4b0..00000000 --- a/gck/tests/mock-interaction.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* mock-interaction.h - - Copyright (C) 2011 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#ifndef MOCK_INTERACTION_H -#define MOCK_INTERACTION_H - -#include <gio/gio.h> - -G_BEGIN_DECLS - -#define MOCK_TYPE_INTERACTION (mock_interaction_get_type ()) -#define MOCK_INTERACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOCK_TYPE_INTERACTION, MockInteraction)) -#define MOCK_IS_INTERACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MOCK_TYPE_INTERACTION)) - -typedef struct _MockInteraction MockInteraction; - -GType mock_interaction_get_type (void) G_GNUC_CONST; - -GTlsInteraction * mock_interaction_new (const gchar *password); - -G_END_DECLS - -#endif /* MOCK_INTERACTION_H */ diff --git a/gck/tests/mock-test-module.c b/gck/tests/mock-test-module.c deleted file mode 100644 index c5431e70..00000000 --- a/gck/tests/mock-test-module.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "config.h" - -#include "gck-mock.h" -#include "pkcs11.h" - -CK_RV -C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) -{ - return gck_mock_C_GetFunctionList (list); -} diff --git a/gck/tests/test-gck-attributes.c b/gck/tests/test-gck-attributes.c deleted file mode 100644 index bf2fc039..00000000 --- a/gck/tests/test-gck-attributes.c +++ /dev/null @@ -1,617 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-attributes.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include <glib.h> -#include <string.h> - -#include "gck/gck.h" - -#define ATTR_TYPE 55 -#define ATTR_DATA "TEST DATA" -#define N_ATTR_DATA ((gsize)9) - -static void -test_init_memory (void) -{ - GckAttribute attr; - - g_assert (sizeof (attr) == sizeof (CK_ATTRIBUTE)); - - gck_attribute_init (&attr, ATTR_TYPE, (const guchar *)ATTR_DATA, N_ATTR_DATA); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == N_ATTR_DATA); - g_assert (memcmp (attr.value, ATTR_DATA, attr.length) == 0); - - gck_attribute_clear (&attr); -} - -static void -test_value_to_boolean (void) -{ - CK_BBOOL data = CK_TRUE; - gboolean result = FALSE; - - if (!gck_value_to_boolean (&data, sizeof (data), &result)) - g_assert_not_reached (); - - g_assert (result == TRUE); - - if (!gck_value_to_boolean (&data, sizeof (data), NULL)) - g_assert_not_reached (); - - /* Should fail */ - if (gck_value_to_boolean (&data, 0, NULL)) - g_assert_not_reached (); - if (gck_value_to_boolean (&data, 2, NULL)) - g_assert_not_reached (); - if (gck_value_to_boolean (&data, (CK_ULONG)-1, NULL)) - g_assert_not_reached (); -} - -static void -test_value_to_ulong (void) -{ - CK_ULONG data = 34343; - gulong result = 0; - - if (!gck_value_to_ulong ((const guchar *)&data, sizeof (data), &result)) - g_assert_not_reached (); - - g_assert (result == 34343); - - if (!gck_value_to_ulong ((const guchar *)&data, sizeof (data), NULL)) - g_assert_not_reached (); - - /* Should fail */ - if (gck_value_to_ulong ((const guchar *)&data, 0, NULL)) - g_assert_not_reached (); - if (gck_value_to_ulong ((const guchar *)&data, 2, NULL)) - g_assert_not_reached (); - if (gck_value_to_ulong ((const guchar *)&data, (CK_ULONG)-1, NULL)) - g_assert_not_reached (); -} - -static void -test_init_boolean (void) -{ - GckAttribute attr; - - gck_attribute_init_boolean (&attr, ATTR_TYPE, TRUE); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == sizeof (CK_BBOOL)); - g_assert (*((CK_BBOOL*)attr.value) == CK_TRUE); - - gck_attribute_clear (&attr); -} - -static void -test_init_date (void) -{ - GckAttribute attr; - CK_DATE ck_date; - GDate *date; - - date = g_date_new_dmy(05, 06, 1960); - memcpy (ck_date.year, "1960", 4); - memcpy (ck_date.month, "06", 2); - memcpy (ck_date.day, "05", 2); - gck_attribute_init_date (&attr, ATTR_TYPE, date); - g_date_free (date); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == sizeof (CK_DATE)); - g_assert (memcmp (attr.value, &ck_date, attr.length) == 0); - - gck_attribute_clear (&attr); -} - -static void -test_init_ulong (void) -{ - GckAttribute attr; - - gck_attribute_init_ulong (&attr, ATTR_TYPE, 88); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == sizeof (CK_ULONG)); - g_assert (*((CK_ULONG*)attr.value) == 88); - - gck_attribute_clear (&attr); -} - -static void -test_init_string (void) -{ - GckAttribute attr; - - gck_attribute_init_string (&attr, ATTR_TYPE, "a test string"); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == strlen ("a test string")); - g_assert (memcmp (attr.value, "a test string", attr.length) == 0); - - gck_attribute_clear (&attr); -} - -static void -test_init_invalid (void) -{ - GckAttribute attr; - - gck_attribute_init_invalid (&attr, ATTR_TYPE); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == (gulong)-1); - g_assert (attr.value == NULL); - - g_assert (gck_attribute_is_invalid (&attr)); - gck_attribute_clear (&attr); -} - -static void -test_init_empty (void) -{ - GckAttribute attr; - - gck_attribute_init_empty (&attr, ATTR_TYPE); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == 0); - g_assert (attr.value == NULL); - - gck_attribute_clear (&attr); -} - -static void -test_new_memory (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new (ATTR_TYPE, ATTR_DATA, N_ATTR_DATA); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == N_ATTR_DATA); - g_assert (memcmp (attr->value, ATTR_DATA, attr->length) == 0); - - gck_attribute_free (attr); -} - -static void -test_new_boolean (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == sizeof (CK_BBOOL)); - g_assert (*((CK_BBOOL*)attr->value) == CK_TRUE); - - gck_attribute_free (attr); -} - -static void -test_new_date (void) -{ - GckAttribute *attr; - CK_DATE ck_date; - GDate *date; - - date = g_date_new_dmy(05, 06, 1800); - memcpy (ck_date.year, "1800", 4); - memcpy (ck_date.month, "06", 2); - memcpy (ck_date.day, "05", 2); - attr = gck_attribute_new_date (ATTR_TYPE, date); - g_date_free (date); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == sizeof (CK_DATE)); - g_assert (memcmp (attr->value, &ck_date, attr->length) == 0); - - gck_attribute_free (attr); -} - -static void -test_new_ulong (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_ulong (ATTR_TYPE, 88); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == sizeof (CK_ULONG)); - g_assert (*((CK_ULONG*)attr->value) == 88); - - gck_attribute_free (attr); -} - -static void -test_new_string (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_string (ATTR_TYPE, "a test string"); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == strlen ("a test string")); - g_assert (memcmp (attr->value, "a test string", attr->length) == 0); - - gck_attribute_free (attr); -} - -static void -test_new_invalid (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_invalid (ATTR_TYPE); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == (gulong)-1); - g_assert (attr->value == NULL); - - g_assert (gck_attribute_is_invalid (attr)); - - gck_attribute_free (attr); -} - -static void -test_new_empty (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_empty (ATTR_TYPE); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == 0); - g_assert (attr->value == NULL); - - gck_attribute_free (attr); -} - -static void -test_get_boolean (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE); - g_assert (gck_attribute_get_boolean (attr) == TRUE); - gck_attribute_free (attr); -} - -static void -test_get_date (void) -{ - GckAttribute *attr; - CK_DATE ck_date; - GDate date, date2; - - g_date_set_dmy(&date, 05, 06, 1800); - memcpy (ck_date.year, "1800", 4); - memcpy (ck_date.month, "06", 2); - memcpy (ck_date.day, "05", 2); - attr = gck_attribute_new_date (ATTR_TYPE, &date); - gck_attribute_get_date (attr, &date2); - g_assert (g_date_compare (&date, &date2) == 0); - gck_attribute_free (attr); -} - -static void -test_get_ulong (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_ulong (ATTR_TYPE, 88); - g_assert (gck_attribute_get_ulong (attr) == 88); - gck_attribute_free (attr); -} - -static void -test_get_string (void) -{ - GckAttribute *attr; - gchar *value; - - attr = gck_attribute_new_string (ATTR_TYPE, "a test string"); - value = gck_attribute_get_string (attr); - g_assert (strcmp ("a test string", value) == 0); - g_free (value); - gck_attribute_free (attr); - - /* Should be able to store null strings */ - attr = gck_attribute_new_string (ATTR_TYPE, NULL); - value = gck_attribute_get_string (attr); - g_assert (value == NULL); - gck_attribute_free (attr); -} - -static void -test_dup_attribute (void) -{ - GckAttribute attr, *dup; - - gck_attribute_init_ulong (&attr, ATTR_TYPE, 88); - dup = gck_attribute_dup (&attr); - gck_attribute_clear (&attr); - g_assert (gck_attribute_get_ulong (dup) == 88); - g_assert (dup->type == ATTR_TYPE); - gck_attribute_free (dup); - - /* Should be able to dup null */ - dup = gck_attribute_dup (NULL); - g_assert (dup == NULL); -} - -static void -test_copy_attribute (void) -{ - GckAttribute attr, copy; - - gck_attribute_init_ulong (&attr, ATTR_TYPE, 88); - gck_attribute_init_copy (©, &attr); - gck_attribute_clear (&attr); - g_assert (gck_attribute_get_ulong (©) == 88); - g_assert (copy.type == ATTR_TYPE); - gck_attribute_clear (©); -} - -static void -test_new_attributes (void) -{ - GckAttributes *attrs; - - attrs = gck_attributes_new (); - g_assert (attrs != NULL); - g_assert (gck_attributes_count (attrs) == 0); - - gck_attributes_ref (attrs); - gck_attributes_unref (attrs); - - gck_attributes_unref (attrs); - - /* Can unref NULL */ - gck_attributes_unref (NULL); -} - -static void -test_attributes_contents (GckAttributes *attrs, gboolean extras) -{ - GckAttribute *attr; - gchar *value; - GDate date, *check; - guint count; - - g_assert (attrs != NULL); - count = extras ? 7 : 5; - g_assert_cmpuint (gck_attributes_count (attrs), ==, count); - - attr = gck_attributes_at (attrs, 0); - g_assert (attr->type == 0); - g_assert (gck_attribute_get_boolean (attr) == TRUE); - - attr = gck_attributes_at (attrs, 1); - g_assert (attr->type == 101); - g_assert (gck_attribute_get_ulong (attr) == 888); - - attr = gck_attributes_at (attrs, 2); - g_assert (attr->type == 202); - value = gck_attribute_get_string (attr); - g_assert (strcmp (value, "string") == 0); - g_free (value); - - attr = gck_attributes_at (attrs, 3); - g_assert (attr->type == 303); - check = g_date_new_dmy (11, 12, 2008); - gck_attribute_get_date (attr, &date); - g_assert (g_date_compare (&date, check) == 0); - g_date_free (check); - - attr = gck_attributes_at (attrs, 4); - g_assert (attr->type == 404); - g_assert (attr->length == N_ATTR_DATA); - g_assert (memcmp (attr->value, ATTR_DATA, N_ATTR_DATA) == 0); - - if (!extras) - return; - - attr = gck_attributes_at (attrs, 5); - g_assert (attr->type == 505); - g_assert (attr->length == (gulong)-1); - g_assert (attr->value == NULL); - g_assert (gck_attribute_is_invalid (attr)); - - attr = gck_attributes_at (attrs, 6); - g_assert (attr->type == 606); - g_assert (attr->length == 0); - g_assert (attr->value == NULL); -} - -static void -test_new_empty_attributes (void) -{ - GckAttributes *attrs = gck_attributes_new_empty (101UL, 202UL, 303UL, 404UL, GCK_INVALID); - GckAttribute *attr; - guint i; - - g_assert_cmpuint (gck_attributes_count (attrs), ==, 4); - for (i = 0; i < gck_attributes_count (attrs); ++i) { - attr = gck_attributes_at (attrs, i); - g_assert (attr->type == ((i + 1) * 100) + i + 1); - g_assert (attr->value == NULL); - g_assert (attr->length == 0); - } -} - -static void -test_add_data_attributes (void) -{ - GckAttributes *attrs; - GDate *date = g_date_new_dmy (11, 12, 2008); - attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, 0UL, TRUE); - gck_attributes_add_ulong (attrs, 101UL, 888); - gck_attributes_add_string (attrs, 202UL, "string"); - gck_attributes_add_date (attrs, 303UL, date); - g_date_free (date); - gck_attributes_add_data (attrs, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - gck_attributes_add_invalid (attrs, 505UL); - gck_attributes_add_empty (attrs, 606UL); - test_attributes_contents (attrs, TRUE); - gck_attributes_unref (attrs); -} - -static void -test_add_attributes (void) -{ - GckAttributes *attrs; - GckAttribute attr; - - GDate *date = g_date_new_dmy (11, 12, 2008); - attrs = gck_attributes_new (); - - gck_attribute_init_boolean (&attr, 0UL, TRUE); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_ulong (&attr, 101UL, 888); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - gck_attribute_init_string (&attr, 202UL, "string"); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_date (&attr, 303UL, date); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - g_date_free (date); - - gck_attribute_init (&attr, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_invalid (&attr, 505UL); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_empty (&attr, 606UL); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - test_attributes_contents (attrs, TRUE); - gck_attributes_unref (attrs); -} - -static void -test_add_all_attributes (void) -{ - GckAttributes *attrs; - GckAttributes *copy; - GDate *date = g_date_new_dmy (11, 12, 2008); - attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, 0UL, TRUE); - gck_attributes_add_ulong (attrs, 101UL, 888); - gck_attributes_add_string (attrs, 202UL, "string"); - gck_attributes_add_date (attrs, 303UL, date); - g_date_free (date); - gck_attributes_add_data (attrs, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - gck_attributes_add_invalid (attrs, 505UL); - gck_attributes_add_empty (attrs, 606UL); - - copy = gck_attributes_new (); - gck_attributes_add_all (copy, attrs); - test_attributes_contents (copy, TRUE); - - gck_attributes_unref (attrs); - gck_attributes_unref (copy); -} - - -static void -test_find_attributes (void) -{ - GckAttribute *attr; - GDate check, *date = g_date_new_dmy (13, 12, 2008); - gboolean bvalue, ret; - gulong uvalue; - gchar *svalue; - - GckAttributes *attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, 0UL, TRUE); - gck_attributes_add_ulong (attrs, 101UL, 888UL); - gck_attributes_add_string (attrs, 202UL, "string"); - gck_attributes_add_date (attrs, 303UL, date); - gck_attributes_add_data (attrs, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - - attr = gck_attributes_find (attrs, 404); - g_assert (attr != NULL); - g_assert (attr->length == N_ATTR_DATA); - g_assert (memcmp (attr->value, ATTR_DATA, N_ATTR_DATA) == 0); - - ret = gck_attributes_find_boolean (attrs, 0UL, &bvalue); - g_assert (ret == TRUE); - g_assert (bvalue == TRUE); - - ret = gck_attributes_find_ulong (attrs, 101UL, &uvalue); - g_assert (ret == TRUE); - g_assert (uvalue == 888); - - ret = gck_attributes_find_string (attrs, 202UL, &svalue); - g_assert (ret == TRUE); - g_assert (svalue != NULL); - g_assert (strcmp (svalue, "string") == 0); - g_free (svalue); - - ret = gck_attributes_find_date (attrs, 303UL, &check); - g_assert (ret == TRUE); - g_assert (g_date_compare (date, &check) == 0); - - gck_attributes_unref (attrs); - g_date_free (date); -} - -int -main (int argc, char **argv) -{ - g_test_init (&argc, &argv, NULL); - - g_test_add_func ("/gck/attributes/init_memory", test_init_memory); - g_test_add_func ("/gck/attributes/value_to_boolean", test_value_to_boolean); - g_test_add_func ("/gck/attributes/value_to_ulong", test_value_to_ulong); - g_test_add_func ("/gck/attributes/init_boolean", test_init_boolean); - g_test_add_func ("/gck/attributes/init_date", test_init_date); - g_test_add_func ("/gck/attributes/init_ulong", test_init_ulong); - g_test_add_func ("/gck/attributes/init_string", test_init_string); - g_test_add_func ("/gck/attributes/init_invalid", test_init_invalid); - g_test_add_func ("/gck/attributes/init_empty", test_init_empty); - g_test_add_func ("/gck/attributes/new_memory", test_new_memory); - g_test_add_func ("/gck/attributes/new_boolean", test_new_boolean); - g_test_add_func ("/gck/attributes/new_date", test_new_date); - g_test_add_func ("/gck/attributes/new_ulong", test_new_ulong); - g_test_add_func ("/gck/attributes/new_string", test_new_string); - g_test_add_func ("/gck/attributes/new_invalid", test_new_invalid); - g_test_add_func ("/gck/attributes/new_empty", test_new_empty); - g_test_add_func ("/gck/attributes/get_boolean", test_get_boolean); - g_test_add_func ("/gck/attributes/get_date", test_get_date); - g_test_add_func ("/gck/attributes/get_ulong", test_get_ulong); - g_test_add_func ("/gck/attributes/get_string", test_get_string); - g_test_add_func ("/gck/attributes/dup_attribute", test_dup_attribute); - g_test_add_func ("/gck/attributes/copy_attribute", test_copy_attribute); - g_test_add_func ("/gck/attributes/new_attributes", test_new_attributes); - g_test_add_func ("/gck/attributes/new_empty_attributes", test_new_empty_attributes); - g_test_add_func ("/gck/attributes/add_data_attributes", test_add_data_attributes); - g_test_add_func ("/gck/attributes/add_attributes", test_add_attributes); - g_test_add_func ("/gck/attributes/add_all_attributes", test_add_all_attributes); - g_test_add_func ("/gck/attributes/find_attributes", test_find_attributes); - - return g_test_run (); -} diff --git a/gck/tests/test-gck-crypto.c b/gck/tests/test-gck-crypto.c deleted file mode 100644 index 9630123c..00000000 --- a/gck/tests/test-gck-crypto.c +++ /dev/null @@ -1,647 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-crypto.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GckModule *module; - GckSession *session; - GckSession *session_with_auth; -} Test; - -static gboolean -on_discard_handle_ignore (GckSession *self, CK_OBJECT_HANDLE handle, gpointer unused) -{ - /* Don't close the handle for this session, since it's a duplicate */ - return TRUE; -} - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - GckSlot *slot; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->session = gck_slot_open_session (slots->data, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (test->session)); - - slot = gck_session_get_slot (test->session); - g_assert (slot); - - test->session_with_auth = gck_session_from_handle (slot, gck_session_get_handle (test->session), GCK_SESSION_AUTHENTICATE); - g_signal_connect (test->session_with_auth, "discard-handle", G_CALLBACK (on_discard_handle_ignore), NULL); - g_assert (test->session_with_auth); - - g_object_unref (slot); - gck_list_unref_free (slots); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->session); - g_object_unref (test->module); - g_object_unref (test->session_with_auth); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static GckObject* -find_key (GckSession *session, CK_ATTRIBUTE_TYPE method, CK_MECHANISM_TYPE mech) -{ - GList *objects, *l; - GckAttributes *attrs; - GckObject *object = NULL; - CK_MECHANISM_TYPE_PTR mechs; - gsize n_mechs; - - attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, method, TRUE); - objects = gck_session_find_objects (session, attrs, NULL, NULL); - gck_attributes_unref (attrs); - g_assert (objects); - - for (l = objects; l; l = g_list_next (l)) { - if (mech) { - mechs = (gulong *)gck_object_get_data (l->data, CKA_ALLOWED_MECHANISMS, - NULL, &n_mechs, NULL); - g_assert (mechs); - g_assert (n_mechs == sizeof (CK_MECHANISM_TYPE)); - /* We know all of them only have one allowed mech */ - if (*mechs != mech) - continue; - } - object = l->data; - g_object_ref (object); - break; - } - - gck_list_unref_free (objects); - return object; -} - -static GckObject* -find_key_with_value (GckSession *session, const gchar *value) -{ - GList *objects; - GckAttributes *attrs; - GckObject *object; - - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_VALUE, value); - objects = gck_session_find_objects (session, attrs, NULL, NULL); - gck_attributes_unref (attrs); - g_assert (objects); - - object = g_object_ref (objects->data); - gck_list_unref_free (objects); - return object; -} - -static void -check_key_with_value (GckSession *session, GckObject *key, CK_OBJECT_CLASS klass, const gchar *value) -{ - GckAttributes *attrs; - GckAttribute *attr; - gulong check; - - attrs = gck_object_get (key, NULL, NULL, CKA_CLASS, CKA_VALUE, GCK_INVALID); - g_assert (attrs); - - if (!gck_attributes_find_ulong (attrs, CKA_CLASS, &check)) - g_assert_not_reached (); - g_assert (check == klass); - - attr = gck_attributes_find (attrs, CKA_VALUE); - g_assert (attr); - g_assert (!gck_attribute_is_invalid (attr)); - egg_assert_cmpsize (attr->length, ==, strlen (value)); - g_assert (memcmp (attr->value, value, attr->length) == 0); - - gck_attributes_unref (attrs); -} - -static gboolean -authenticate_object (GckSlot *module, GckObject *object, gchar *label, gchar **password) -{ - g_assert (GCK_IS_MODULE (module)); - g_assert (GCK_IS_OBJECT (object)); - g_assert (password); - g_assert (!*password); - - *password = g_strdup ("booo"); - return TRUE; -} - -static void -test_encrypt (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Find the right key */ - key = find_key (test->session, CKA_ENCRYPT, CKM_MOCK_CAPITALIZE); - g_assert (key); - - /* Simple one */ - output = gck_session_encrypt (test->session, key, CKM_MOCK_CAPITALIZE, (const guchar*)"blah blah", 10, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 10); - g_assert_cmpstr ((gchar*)output, ==, "BLAH BLAH"); - g_free (output); - - /* Asynchronous one */ - gck_session_encrypt_async (test->session, key, &mech, (const guchar*)"second chance", 14, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - output = gck_session_encrypt_finish (test->session, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 14); - g_assert_cmpstr ((gchar*)output, ==, "SECOND CHANCE"); - g_free (output); - - g_object_unref (result); - g_object_unref (key); -} - -static void -test_decrypt (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Find the right key */ - key = find_key (test->session, CKA_DECRYPT, CKM_MOCK_CAPITALIZE); - g_assert (key); - - /* Simple one */ - output = gck_session_decrypt (test->session, key, CKM_MOCK_CAPITALIZE, (const guchar*)"FRY???", 7, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 7); - g_assert_cmpstr ((gchar*)output, ==, "fry???"); - g_free (output); - - /* Asynchronous one */ - gck_session_decrypt_async (test->session, key, &mech, (const guchar*)"FAT CHANCE", 11, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - output = gck_session_decrypt_finish (test->session, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 11); - g_assert_cmpstr ((gchar*)output, ==, "fat chance"); - g_free (output); - - g_object_unref (result); - g_object_unref (key); -} - -static void -test_login_context_specific (Test *test, gconstpointer unused) -{ - /* The test module won't let us sign without doing a login, check that */ - - GError *error = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Find the right key */ - key = find_key (test->session, CKA_SIGN, CKM_MOCK_PREFIX); - g_assert (key); - - /* Simple one */ - output = gck_session_sign (test->session, key, CKM_MOCK_PREFIX, (const guchar*)"TV Monster", 11, &n_output, NULL, &error); - g_assert_error (error, GCK_ERROR, CKR_USER_NOT_LOGGED_IN); - g_assert (output == NULL); - - g_object_unref (key); -} - -static void -test_sign (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_PREFIX, (guchar *)"my-prefix:", 10 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Enable auto-login on this test->session, see previous test */ - g_signal_connect (test->module, "authenticate-object", G_CALLBACK (authenticate_object), NULL); - - /* Find the right key */ - key = find_key (test->session_with_auth, CKA_SIGN, CKM_MOCK_PREFIX); - g_assert (key); - - /* Simple one */ - output = gck_session_sign (test->session_with_auth, key, CKM_MOCK_PREFIX, (const guchar*)"Labarbara", 10, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - g_assert_cmpuint (n_output, ==, 24); - g_assert_cmpstr ((gchar*)output, ==, "signed-prefix:Labarbara"); - g_free (output); - - /* Asynchronous one */ - gck_session_sign_async (test->session_with_auth, key, &mech, (const guchar*)"Conrad", 7, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - output = gck_session_sign_finish (test->session_with_auth, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - g_assert_cmpuint (n_output, ==, 17); - g_assert_cmpstr ((gchar*)output, ==, "my-prefix:Conrad"); - g_free (output); - - g_object_unref (result); - g_object_unref (key); -} - -static void -test_verify (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_PREFIX, (guchar *)"my-prefix:", 10 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - gboolean ret; - - /* Enable auto-login on this session, shouldn't be needed */ - g_signal_connect (test->module, "authenticate-object", G_CALLBACK (authenticate_object), NULL); - - /* Find the right key */ - key = find_key (test->session, CKA_VERIFY, CKM_MOCK_PREFIX); - g_assert (key); - - /* Simple one */ - ret = gck_session_verify (test->session, key, CKM_MOCK_PREFIX, (const guchar*)"Labarbara", 10, - (const guchar*)"signed-prefix:Labarbara", 24, NULL, &error); - g_assert_no_error (error); - g_assert (ret); - - /* Failure one */ - ret = gck_session_verify_full (test->session, key, &mech, (const guchar*)"Labarbara", 10, - (const guchar*)"my-prefix:Loborboro", 20, NULL, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - - /* Asynchronous one */ - gck_session_verify_async (test->session, key, &mech, (const guchar*)"Labarbara", 10, - (const guchar*)"my-prefix:Labarbara", 20, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_verify_finish (test->session, result, &error); - g_assert_no_error (error); - g_assert (ret); - g_object_unref (result); - - /* Asynchronous failure */ - result = NULL; - gck_session_verify_async (test->session, key, &mech, (const guchar*)"Labarbara", 10, - (const guchar*)"my-prefix:Labarxoro", 20, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_verify_finish (test->session, result, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - g_object_unref (result); - - g_object_unref (key); -} - -static void -test_generate_key_pair (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_GENERATE, (guchar *)"generate", 9 }; - GckAttributes *pub_attrs, *prv_attrs; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *pub_key, *prv_key; - gboolean ret; - - pub_attrs = gck_attributes_new (); - gck_attributes_add_ulong (pub_attrs, CKA_CLASS, CKO_PUBLIC_KEY); - prv_attrs = gck_attributes_new (); - gck_attributes_add_ulong (prv_attrs, CKA_CLASS, CKO_PRIVATE_KEY); - - /* Full One*/ - ret = gck_session_generate_key_pair_full (test->session, &mech, pub_attrs, prv_attrs, - &pub_key, &prv_key, NULL, &error); - g_assert_no_error (error); - g_assert (ret); - g_object_unref (pub_key); - g_object_unref (prv_key); - - /* Failure one */ - mech.type = 0; - pub_key = prv_key = NULL; - ret = gck_session_generate_key_pair_full (test->session, &mech, pub_attrs, prv_attrs, - &pub_key, &prv_key, NULL, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - g_assert (pub_key == NULL); - g_assert (prv_key == NULL); - - /* Asynchronous one */ - mech.type = CKM_MOCK_GENERATE; - gck_session_generate_key_pair_async (test->session, &mech, pub_attrs, prv_attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_generate_key_pair_finish (test->session, result, &pub_key, &prv_key, &error); - g_assert_no_error (error); - g_assert (ret); - g_object_unref (result); - g_object_unref (pub_key); - g_object_unref (prv_key); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - pub_key = prv_key = NULL; - gck_session_generate_key_pair_async (test->session, &mech, pub_attrs, prv_attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_generate_key_pair_finish (test->session, result, &pub_key, &prv_key, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - g_object_unref (result); - g_assert (pub_key == NULL); - g_assert (prv_key == NULL); - - gck_attributes_unref (pub_attrs); - gck_attributes_unref (prv_attrs); -} - -static void -test_wrap_key (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_WRAP, (guchar *)"wrap", 4 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *wrapper, *wrapped; - gpointer output; - gsize n_output; - - wrapper = find_key (test->session, CKA_WRAP, 0); - wrapped = find_key_with_value (test->session, "value"); - - /* Simple One */ - output = gck_session_wrap_key (test->session, wrapper, CKM_MOCK_WRAP, wrapped, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - egg_assert_cmpsize (n_output, ==, 5); - g_assert (memcmp (output, "value", 5) == 0); - g_free (output); - - /* Full One*/ - output = gck_session_wrap_key_full (test->session, wrapper, &mech, wrapped, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - egg_assert_cmpsize (n_output, ==, 5); - g_assert (memcmp (output, "value", 5) == 0); - g_free (output); - - /* Failure one */ - mech.type = 0; - n_output = 0; - output = gck_session_wrap_key_full (test->session, wrapper, &mech, wrapped, &n_output, NULL, &error); - g_assert (error != NULL); - g_assert (!output); - g_clear_error (&error); - egg_assert_cmpsize (n_output, ==, 0); - - /* Asynchronous one */ - mech.type = CKM_MOCK_WRAP; - gck_session_wrap_key_async (test->session, wrapper, &mech, wrapped, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - output = gck_session_wrap_key_finish (test->session, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - egg_assert_cmpsize (n_output, ==, 5); - g_assert (memcmp (output, "value", 5) == 0); - g_object_unref (result); - g_free (output); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - n_output = 0; - gck_session_wrap_key_async (test->session, wrapper, &mech, wrapped, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - output = gck_session_wrap_key_finish (test->session, result, &n_output, &error); - g_assert (error != NULL); - g_assert (!output); - g_clear_error (&error); - egg_assert_cmpsize (n_output, ==, 0); - g_object_unref (result); - - g_object_unref (wrapper); - g_object_unref (wrapped); -} - -static void -test_unwrap_key (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_WRAP, (guchar *)"wrap", 4 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *wrapper, *unwrapped; - GckAttributes *attrs; - - wrapper = find_key (test->session, CKA_UNWRAP, 0); - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_SECRET_KEY); - - /* Full One*/ - unwrapped = gck_session_unwrap_key_full (test->session, wrapper, &mech, (const guchar *)"special", 7, attrs, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (unwrapped)); - check_key_with_value (test->session, unwrapped, CKO_SECRET_KEY, "special"); - g_object_unref (unwrapped); - - /* Failure one */ - mech.type = 0; - unwrapped = gck_session_unwrap_key_full (test->session, wrapper, &mech, (const guchar *)"special", 7, attrs, NULL, &error); - g_assert (error != NULL); - g_assert (!unwrapped); - g_clear_error (&error); - - /* Asynchronous one */ - mech.type = CKM_MOCK_WRAP; - gck_session_unwrap_key_async (test->session, wrapper, &mech, (const guchar *)"special", 7, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - unwrapped = gck_session_unwrap_key_finish (test->session, result, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (unwrapped)); - check_key_with_value (test->session, unwrapped, CKO_SECRET_KEY, "special"); - g_object_unref (unwrapped); - g_object_unref (result); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - gck_session_unwrap_key_async (test->session, wrapper, &mech, (const guchar *)"special", 6, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - unwrapped = gck_session_unwrap_key_finish (test->session, result, &error); - g_assert (error != NULL); - g_assert (!unwrapped); - g_clear_error (&error); - g_object_unref (result); - - g_object_unref (wrapper); - gck_attributes_unref (attrs); -} - -static void -test_derive_key (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_DERIVE, (guchar *)"derive", 6 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *wrapper, *derived; - GckAttributes *attrs; - - wrapper = find_key (test->session, CKA_DERIVE, 0); - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_SECRET_KEY); - - /* Full One*/ - derived = gck_session_derive_key_full (test->session, wrapper, &mech, attrs, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (derived)); - check_key_with_value (test->session, derived, CKO_SECRET_KEY, "derived"); - g_object_unref (derived); - - /* Failure one */ - mech.type = 0; - derived = gck_session_derive_key_full (test->session, wrapper, &mech, attrs, NULL, &error); - g_assert (error != NULL); - g_assert (!derived); - g_clear_error (&error); - - /* Asynchronous one */ - mech.type = CKM_MOCK_DERIVE; - gck_session_derive_key_async (test->session, wrapper, &mech, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - derived = gck_session_derive_key_finish (test->session, result, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (derived)); - check_key_with_value (test->session, derived, CKO_SECRET_KEY, "derived"); - g_object_unref (derived); - g_object_unref (result); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - gck_session_derive_key_async (test->session, wrapper, &mech, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - derived = gck_session_derive_key_finish (test->session, result, &error); - g_assert (error != NULL); - g_assert (!derived); - g_clear_error (&error); - g_object_unref (result); - - g_object_unref (wrapper); - gck_attributes_unref (attrs); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_set_prgname ("test-gck-crypto"); - - g_test_add ("/gck/crypto/encrypt", Test, NULL, setup, test_encrypt, teardown); - g_test_add ("/gck/crypto/decrypt", Test, NULL, setup, test_decrypt, teardown); - g_test_add ("/gck/crypto/login_context_specific", Test, NULL, setup, test_login_context_specific, teardown); - g_test_add ("/gck/crypto/sign", Test, NULL, setup, test_sign, teardown); - g_test_add ("/gck/crypto/verify", Test, NULL, setup, test_verify, teardown); - g_test_add ("/gck/crypto/generate_key_pair", Test, NULL, setup, test_generate_key_pair, teardown); - g_test_add ("/gck/crypto/wrap_key", Test, NULL, setup, test_wrap_key, teardown); - g_test_add ("/gck/crypto/unwrap_key", Test, NULL, setup, test_unwrap_key, teardown); - g_test_add ("/gck/crypto/derive_key", Test, NULL, setup, test_derive_key, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-enumerator.c b/gck/tests/test-gck-enumerator.c deleted file mode 100644 index d5d669e3..00000000 --- a/gck/tests/test-gck-enumerator.c +++ /dev/null @@ -1,359 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-enumerator.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -#include "mock-interaction.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GList *modules; - GckModule *module; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - test->modules = g_list_append (NULL, g_object_ref (test->module)); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - gck_list_unref_free (test->modules); - test->modules = NULL; - - g_object_unref (test->module); - test->module = NULL; -} - -static void -test_create (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GckEnumerator *en; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - g_object_unref (en); -} - -static void -test_create_slots (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GckEnumerator *en; - GList *slots; - - uri_data = gck_uri_data_new (); - slots = gck_module_get_slots (test->module, FALSE); - en = _gck_enumerator_new (slots, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - g_object_unref (en); - gck_list_unref_free (slots); -} - -static void -test_next (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); -} - -static void -test_next_slots (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GList *slots = NULL; - GckEnumerator *en; - GckObject *obj; - - uri_data = gck_uri_data_new (); - slots = gck_module_get_slots (test->module, FALSE); - en = _gck_enumerator_new (slots, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); - gck_list_unref_free (slots); -} - -static void -test_next_and_resume (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj, *obj2; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (obj)); - - obj2 = gck_enumerator_next (en, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (obj2)); - - g_assert (!gck_object_equal (obj, obj2)); - - g_object_unref (obj); - g_object_unref (obj2); - g_object_unref (en); -} - -static void -test_next_n (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GList *objects, *l; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 5); - for (l = objects; l; l = g_list_next (l)) - g_assert (GCK_IS_OBJECT (l->data)); - - gck_list_unref_free (objects); - g_object_unref (en); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_next_async (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GAsyncResult *result = NULL; - GError *error = NULL; - GckEnumerator *en; - GList *objects, *l; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - gck_enumerator_next_async (en, -1, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result); - - objects = gck_enumerator_next_finish (en, result, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 5); - for (l = objects; l; l = g_list_next (l)) - g_assert (GCK_IS_OBJECT (l->data)); - - g_object_unref (result); - gck_list_unref_free (objects); - g_object_unref (en); -} - -static void -test_attributes (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GList *objects; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_string (uri_data->attributes, CKA_LABEL, "Private Capitalize Key"); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 1); - g_assert (GCK_IS_OBJECT (objects->data)); - - gck_list_unref_free (objects); - g_object_unref (en); -} - -static void -test_authenticate_interaction (Test *test, - gconstpointer unused) -{ - GTlsInteraction *interaction; - GTlsInteraction *check; - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, GCK_SESSION_LOGIN_USER, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - interaction = mock_interaction_new ("booo"); - g_object_set (en, "interaction", interaction, NULL); - - check = NULL; - g_object_get (en, "interaction", &check, NULL); - g_assert (interaction == check); - g_object_unref (interaction); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); -} - -static gboolean -on_authenticate_token (GckModule *module, - GckSlot *slot, - gchar *label, - gchar **password, - gpointer unused) -{ - g_assert (unused == GUINT_TO_POINTER (35)); - g_assert (password != NULL); - g_assert (*password == NULL); - g_assert (GCK_IS_MODULE (module)); - g_assert (GCK_IS_SLOT (slot)); - - *password = g_strdup ("booo"); - return TRUE; -} - -static void -test_authenticate_compat (Test *test, - gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj; - - g_signal_connect (test->modules->data, "authenticate-slot", - G_CALLBACK (on_authenticate_token), GUINT_TO_POINTER (35)); - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, GCK_SESSION_LOGIN_USER, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); -} - -static void -test_token_match (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GList *objects; - - uri_data = gck_uri_data_new (); - uri_data->token_info = g_new0 (GckTokenInfo, 1); - uri_data->token_info->label = g_strdup ("Invalid token name"); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_cmpint (g_list_length (objects), ==, 0); - g_assert (error == NULL); - - gck_list_unref_free (objects); - g_object_unref (en); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_set_prgname ("test-gck-enumerator"); - - g_test_add ("/gck/enumerator/create", Test, NULL, setup, test_create, teardown); - g_test_add ("/gck/enumerator/create_slots", Test, NULL, setup, test_create_slots, teardown); - g_test_add ("/gck/enumerator/next", Test, NULL, setup, test_next, teardown); - g_test_add ("/gck/enumerator/next_slots", Test, NULL, setup, test_next_slots, teardown); - g_test_add ("/gck/enumerator/next_and_resume", Test, NULL, setup, test_next_and_resume, teardown); - g_test_add ("/gck/enumerator/next_n", Test, NULL, setup, test_next_n, teardown); - g_test_add ("/gck/enumerator/next_async", Test, NULL, setup, test_next_async, teardown); - g_test_add ("/gck/enumerator/authenticate-interaction", Test, NULL, setup, test_authenticate_interaction, teardown); - g_test_add ("/gck/enumerator/authenticate-compat", Test, NULL, setup, test_authenticate_compat, teardown); - g_test_add ("/gck/enumerator/attributes", Test, NULL, setup, test_attributes, teardown); - g_test_add ("/gck/enumerator/token_match", Test, NULL, setup, test_token_match, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-module.c b/gck/tests/test-gck-module.c deleted file mode 100644 index b5db530a..00000000 --- a/gck/tests/test-gck-module.c +++ /dev/null @@ -1,176 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-module.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include <errno.h> -#include <glib.h> -#include <string.h> - -#include "egg/egg-testing.h" - -#include "gck/gck.h" -#include "gck/gck-test.h" - -typedef struct { - GckModule *module; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (test->module); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->module); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_initialize_async (void) -{ - GckModule *module; - GAsyncResult *result; - GError *error = NULL; - - /* Shouldn't be able to load modules */ - gck_module_initialize_async (BUILDDIR "/.libs/libmock-test-module.so", - NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - module = gck_module_initialize_finish (result, &error); - g_assert_no_error (error); - g_assert (GCK_IS_MODULE (module)); - - g_object_unref (result); - g_object_unref (module); -} - - -static void -test_invalid_modules (Test *test, gconstpointer unused) -{ - GckModule *invalid; - GError *error = NULL; - - /* Shouldn't be able to load modules */ - invalid = gck_module_initialize ("blah-blah-non-existant", NULL, &error); - g_assert_error (error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM); - g_assert (invalid == NULL); - - g_clear_error (&error); - - /* Shouldn't be able to load any file successfully */ - invalid = gck_module_initialize ("/usr/lib/libm.so", NULL, &error); - g_assert_error (error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM); - g_assert (invalid == NULL); - - g_clear_error (&error); -} - -static void -test_module_equals_hash (Test *test, gconstpointer unused) -{ - GckModule *other; - GObject *obj; - guint hash; - - hash = gck_module_hash (test->module); - g_assert (hash != 0); - - g_assert (gck_module_equal (test->module, test->module)); - - other = gck_module_new (gck_module_get_functions (test->module)); - obj = g_object_new (G_TYPE_OBJECT, NULL); - - g_assert (gck_module_equal (test->module, other)); - - /* TODO: Could do with another test for inequality */ - g_assert (!gck_module_equal (test->module, obj)); - - g_object_unref (other); - g_object_unref (obj); -} - -static void -test_module_props (Test *test, gconstpointer unused) -{ - gchar *path; - - g_object_get (test->module, "path", &path, NULL); - g_assert (path != NULL && "no module-path"); - g_assert (strcmp (BUILDDIR "/.libs/libmock-test-module.so", path) == 0 && "module path wrong"); - g_free (path); -} - -static void -test_module_info (Test *test, gconstpointer unused) -{ - GckModuleInfo *info; - - info = gck_module_get_info (test->module); - g_assert (info != NULL && "no module info"); - - g_assert (info->pkcs11_version_major == CRYPTOKI_VERSION_MAJOR && "wrong major version"); - g_assert (info->pkcs11_version_minor == CRYPTOKI_VERSION_MINOR && "wrong minor version"); - g_assert (strcmp ("TEST MANUFACTURER", info->manufacturer_id) == 0); - g_assert (strcmp ("TEST LIBRARY", info->library_description) == 0); - g_assert (0 == info->flags); - g_assert (45 == info->library_version_major); - g_assert (145 == info->library_version_minor); - - gck_module_info_free (info); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add_func ("/gck/module/initialize_async", test_initialize_async); - g_test_add ("/gck/module/invalid_modules", Test, NULL, setup, test_invalid_modules, teardown); - g_test_add ("/gck/module/module_equals_hash", Test, NULL, setup, test_module_equals_hash, teardown); - g_test_add ("/gck/module/module_props", Test, NULL, setup, test_module_props, teardown); - g_test_add ("/gck/module/module_info", Test, NULL, setup, test_module_info, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-modules.c b/gck/tests/test-gck-modules.c deleted file mode 100644 index 246c771f..00000000 --- a/gck/tests/test-gck-modules.c +++ /dev/null @@ -1,210 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-modules.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GList *modules; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GckModule *module; - GError *err = NULL; - - /* Successful load */ - module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (module)); - - test->modules = g_list_append (NULL, module); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - gck_list_unref_free (test->modules); - test->modules = NULL; -} - -static void -test_enumerate_objects (Test *test, gconstpointer unused) -{ - GckAttributes *attrs; - GError *error = NULL; - GckEnumerator *en; - GList *objects; - - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_LABEL, "Private Capitalize Key"); - en = gck_modules_enumerate_objects (test->modules, attrs, 0); - g_assert (GCK_IS_ENUMERATOR (en)); - gck_attributes_unref (attrs); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 1); - g_assert (GCK_IS_OBJECT (objects->data)); - - gck_list_unref_free (objects); - g_object_unref (en); -} - - -static void -test_token_for_uri (Test *test, gconstpointer unused) -{ - GckSlot *slot; - GError *error = NULL; - - slot = gck_modules_token_for_uri (test->modules, "pkcs11:token=TEST%20LABEL", &error); - g_assert (GCK_IS_SLOT (slot)); - - g_object_unref (slot); -} - -static void -test_token_for_uri_not_found (Test *test, gconstpointer unused) -{ - GckSlot *slot; - GError *error = NULL; - - slot = gck_modules_token_for_uri (test->modules, "pkcs11:token=UNKNOWN", &error); - g_assert (slot == NULL); - g_assert (error == NULL); -} - -static void -test_token_for_uri_error (Test *test, gconstpointer unused) -{ - GckSlot *slot; - GError *error = NULL; - - slot = gck_modules_token_for_uri (test->modules, "http://invalid.uri", &error); - g_assert (slot == NULL); - g_assert (error != NULL); - g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX)); - g_error_free (error); -} - -static void -test_object_for_uri (Test *test, gconstpointer unused) -{ - GckObject *object; - GError *error = NULL; - - object = gck_modules_object_for_uri (test->modules, "pkcs11:object=Public%20Capitalize%20Key;objecttype=public", 0, &error); - g_assert (GCK_IS_OBJECT (object)); - g_object_unref (object); -} - -static void -test_object_for_uri_not_found (Test *test, gconstpointer unused) -{ - GckObject *object; - GError *error = NULL; - - object = gck_modules_object_for_uri (test->modules, "pkcs11:object=Unknown%20Label", 0, &error); - g_assert (object == NULL); - g_assert (error == NULL); -} - -static void -test_object_for_uri_error (Test *test, gconstpointer unused) -{ - GckObject *object; - GError *error = NULL; - - object = gck_modules_object_for_uri (test->modules, "http://invalid.uri", 0, &error); - g_assert (object == NULL); - g_assert (error != NULL); - g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX)); - g_error_free (error); -} - -static void -test_objects_for_uri (Test *test, gconstpointer unused) -{ - GList *objects; - GError *error = NULL; - - objects = gck_modules_objects_for_uri (test->modules, "pkcs11:token=TEST%20LABEL", 0, &error); - g_assert (objects); - g_assert (!error); - g_assert_cmpint (g_list_length (objects), ==, 5); - - gck_list_unref_free (objects); -} - -static void -test_enumerate_uri (Test *test, gconstpointer unused) -{ - GckEnumerator *en; - GList *objects; - GError *error = NULL; - - en = gck_modules_enumerate_uri (test->modules, "pkcs11:token=TEST%20LABEL", 0, &error); - g_assert (GCK_IS_ENUMERATOR (en)); - g_assert (!error); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_cmpint (g_list_length (objects), ==, 5); - g_assert (!error); - - g_object_unref (en); - gck_list_unref_free (objects); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add ("/gck/modules/enumerate_objects", Test, NULL, setup, test_enumerate_objects, teardown); - g_test_add ("/gck/modules/token_for_uri", Test, NULL, setup, test_token_for_uri, teardown); - g_test_add ("/gck/modules/token_for_uri_not_found", Test, NULL, setup, test_token_for_uri_not_found, teardown); - g_test_add ("/gck/modules/token_for_uri_error", Test, NULL, setup, test_token_for_uri_error, teardown); - g_test_add ("/gck/modules/object_for_uri", Test, NULL, setup, test_object_for_uri, teardown); - g_test_add ("/gck/modules/object_for_uri_not_found", Test, NULL, setup, test_object_for_uri_not_found, teardown); - g_test_add ("/gck/modules/object_for_uri_error", Test, NULL, setup, test_object_for_uri_error, teardown); - g_test_add ("/gck/modules/objects_for_uri", Test, NULL, setup, test_objects_for_uri, teardown); - g_test_add ("/gck/modules/enumerate_uri", Test, NULL, setup, test_enumerate_uri, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-object.c b/gck/tests/test-gck-object.c deleted file mode 100644 index f1026201..00000000 --- a/gck/tests/test-gck-object.c +++ /dev/null @@ -1,419 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-object.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GckModule *module; - GckSlot *slot; - GckSession *session; - GckObject *object; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->slot = GCK_SLOT (slots->data); - g_object_ref (test->slot); - gck_list_unref_free (slots); - - test->session = gck_slot_open_session (test->slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (test->session)); - - /* Our module always exports a token object with this */ - test->object = gck_object_from_handle (test->session, 2); - g_assert (test->object != NULL); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->object); - g_object_unref (test->session); - g_object_unref (test->slot); - g_object_unref (test->module); -} - -static void -test_object_props (Test *test, gconstpointer unused) -{ - GckSession *sess; - GckModule *mod; - CK_OBJECT_HANDLE handle; - g_object_get (test->object, "session", &sess, "module", &mod, "handle", &handle, NULL); - g_assert (test->session == sess); - g_object_unref (sess); - g_assert (test->module == mod); - g_object_unref (mod); - g_assert (handle == 2); -} - -static void -test_object_equals_hash (Test *test, gconstpointer unused) -{ - GckSlot *other_slot; - GckSession *other_session; - GckObject *other_object; - GObject *obj; - GError *err = NULL; - guint hash; - - hash = gck_object_hash (test->object); - g_assert (hash != 0); - - g_assert (gck_object_equal (test->object, test->object)); - - other_slot = g_object_new (GCK_TYPE_SLOT, "module", test->module, "handle", GCK_MOCK_SLOT_TWO_ID, NULL); - other_session = gck_slot_open_session (other_slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (other_session)); - other_object = gck_object_from_handle (other_session, gck_object_get_handle (test->object)); - g_assert (!gck_object_equal (test->object, other_object)); - g_object_unref (other_slot); - g_object_unref (other_session); - g_object_unref (other_object); - - obj = g_object_new (G_TYPE_OBJECT, NULL); - g_assert (!gck_object_equal (test->object, obj)); - g_object_unref (obj); - - other_object = gck_object_from_handle (test->session, 383838); - g_assert (!gck_object_equal (test->object, other_object)); - g_object_unref (other_object); - - other_object = gck_object_from_handle (test->session, gck_object_get_handle (test->object)); - g_assert (gck_object_equal (test->object, other_object)); - g_object_unref (other_object); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_create_object (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs; - GckObject *object; - CK_OBJECT_HANDLE last_handle; - GError *err = NULL; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST LABEL"); - gck_attributes_add_boolean (attrs, CKA_TOKEN, CK_FALSE); - gck_attributes_add_data (attrs, CKA_VALUE, (const guchar *)"BLAH", 4); - - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert (GCK_IS_OBJECT (object)); - g_assert_no_error (err); - - last_handle = gck_object_get_handle (object); - g_object_unref (object); - - /* Using async */ - gck_session_create_object_async (test->session, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - object = gck_session_create_object_finish (test->session, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - - g_assert (last_handle != gck_object_get_handle (object)); - g_object_unref (object); - - gck_attributes_unref (attrs); -} - -static void -test_destroy_object (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs; - GckObject *object; - GError *err = NULL; - gboolean ret; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST OBJECT"); - gck_attributes_add_boolean (attrs, CKA_TOKEN, CK_TRUE); - - /* Using simple */ - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - - ret = gck_object_destroy (object, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (object); - - /* Using async */ - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - - /* Using async */ - gck_object_destroy_async (object, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_object_destroy_finish (object, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (object); -} - -static void -test_get_attributes (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs; - gulong attr_types[2]; - GError *err = NULL; - gulong klass; - gchar *value = NULL; - - attr_types[0] = CKA_CLASS; - attr_types[1] = CKA_LABEL; - - /* Simple */ - attrs = gck_object_get (test->object, NULL, &err, CKA_CLASS, CKA_LABEL, GCK_INVALID); - g_assert_no_error (err); - if (attrs != NULL) { - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_DATA); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "TEST LABEL") == 0); - g_free (value); value = NULL; - } - gck_attributes_unref (attrs); - - /* Full */ - attrs = gck_object_get_full (test->object, attr_types, G_N_ELEMENTS (attr_types), NULL, &err); - g_assert_no_error (err); - g_assert (attrs); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_DATA); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "TEST LABEL") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); - - /* Async */ - gck_object_get_async (test->object, attr_types, G_N_ELEMENTS (attr_types), NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - attrs = gck_object_get_finish (test->object, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (attrs); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_DATA); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "TEST LABEL") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); -} - -static void -test_get_data_attribute (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - CK_OBJECT_CLASS_PTR klass; - gsize n_data; - GError *err = NULL; - - /* Simple */ - klass = (gulong *)gck_object_get_data (test->object, CKA_CLASS, NULL, &n_data, &err); - g_assert_no_error (err); - g_assert (klass); - g_assert (n_data == sizeof (CK_OBJECT_CLASS)); - g_assert (*klass == CKO_DATA); - g_free (klass); - - /* Full */ - klass = (gulong *)gck_object_get_data_full (test->object, CKA_CLASS, NULL, NULL, &n_data, &err); - g_assert_no_error (err); - g_assert (klass); - g_assert (n_data == sizeof (CK_OBJECT_CLASS)); - g_assert (*klass == CKO_DATA); - g_free (klass); - - /* Async */ - gck_object_get_data_async (test->object, CKA_CLASS, NULL, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - klass = (gulong *)gck_object_get_data_finish (test->object, result, &n_data, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (klass); - g_assert (n_data == sizeof (CK_OBJECT_CLASS)); - g_assert (*klass == CKO_DATA); - g_free (klass); - -} - -static void -test_set_attributes (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs, *templ; - GError *err = NULL; - gulong klass; - gchar *value = NULL; - gboolean ret; - - templ = gck_attributes_new (); - gck_attributes_add_ulong (templ, CKA_CLASS, 6); - gck_attributes_add_string (templ, CKA_LABEL, "CHANGE TWO"); - - /* Full */ - ret = gck_object_set (test->object, templ, NULL, &err); - gck_attributes_unref (templ); - g_assert_no_error (err); - g_assert (ret); - attrs = gck_object_get (test->object, NULL, &err, CKA_CLASS, CKA_LABEL, GCK_INVALID); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == 6); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "CHANGE TWO") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); - - templ = gck_attributes_new (); - gck_attributes_add_ulong (templ, CKA_CLASS, 7); - gck_attributes_add_string (templ, CKA_LABEL, "CHANGE THREE"); - - /* Async */ - gck_object_set_async (test->object, templ, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_object_set_finish (test->object, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (ret); - attrs = gck_object_get (test->object, NULL, &err, CKA_CLASS, CKA_LABEL, GCK_INVALID); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == 7); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "CHANGE THREE") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); -} - -static void -test_find_objects (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *templ, *attrs; - GList *objects; - GckObject *testobj; - GError *err = NULL; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "UNIQUE LABEL"); - testobj = gck_session_create_object (test->session, attrs, NULL, &err); - gck_attributes_unref (attrs); - g_object_unref (testobj); - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "OTHER LABEL"); - testobj = gck_session_create_object (test->session, attrs, NULL, &err); - gck_attributes_unref (attrs); - g_object_unref (testobj); - - /* Simple, "TEST LABEL" */ - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_LABEL, "UNIQUE LABEL"); - objects = gck_session_find_objects (test->session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (g_list_length (objects) == 1); - gck_list_unref_free (objects); - gck_attributes_unref (attrs); - - /* Full, All */ - templ = gck_attributes_new (); - objects = gck_session_find_objects (test->session, templ, NULL, &err); - g_assert_no_error (err); - g_assert (g_list_length (objects) > 1); - gck_list_unref_free (objects); - - /* Async, None */ - gck_attributes_add_string (templ, CKA_LABEL, "blah blah"); - gck_session_find_objects_async (test->session, templ, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - objects = gck_session_find_objects_finish (test->session, result, &err); - g_object_unref (result); - g_assert (objects == NULL); - gck_list_unref_free (objects); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add ("/gck/object/object_props", Test, NULL, setup, test_object_props, teardown); - g_test_add ("/gck/object/object_equals_hash", Test, NULL, setup, test_object_equals_hash, teardown); - g_test_add ("/gck/object/create_object", Test, NULL, setup, test_create_object, teardown); - g_test_add ("/gck/object/destroy_object", Test, NULL, setup, test_destroy_object, teardown); - g_test_add ("/gck/object/get_attributes", Test, NULL, setup, test_get_attributes, teardown); - g_test_add ("/gck/object/get_data_attribute", Test, NULL, setup, test_get_data_attribute, teardown); - g_test_add ("/gck/object/set_attributes", Test, NULL, setup, test_set_attributes, teardown); - g_test_add ("/gck/object/find_objects", Test, NULL, setup, test_find_objects, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-session.c b/gck/tests/test-gck-session.c deleted file mode 100644 index 632a7600..00000000 --- a/gck/tests/test-gck-session.c +++ /dev/null @@ -1,319 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-session.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GckModule *module; - GckSlot *slot; - GckSession *session; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->slot = GCK_SLOT (slots->data); - g_object_ref (test->slot); - gck_list_unref_free (slots); - - test->session = gck_slot_open_session (test->slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (test->session)); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->session); - g_object_unref (test->slot); - g_object_unref (test->module); -} - -static void -test_session_props (Test *test, gconstpointer unused) -{ - GckModule *mod; - GckSlot *sl; - gulong handle; - - g_object_get (test->session, "module", &mod, "handle", &handle, "slot", &sl, NULL); - g_assert (mod == test->module); - g_assert (sl == test->slot); - g_object_unref (mod); - g_object_unref (sl); - - g_assert (handle != 0); - g_assert (gck_session_get_handle (test->session) == handle); -} - -static void -test_session_info (Test *test, gconstpointer unused) -{ - GckSessionInfo *info; - - info = gck_session_get_info (test->session); - g_assert (info != NULL && "no session info"); - - g_assert (info->slot_id == gck_slot_get_handle (test->slot)); - g_assert ((info->flags & CKF_SERIAL_SESSION) == CKF_SERIAL_SESSION); - g_assert (info->device_error == 1414); - gck_session_info_free (info); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_open_close_session (Test *test, gconstpointer unused) -{ - GckSession *sess; - GAsyncResult *result = NULL; - GError *err = NULL; - - sess = gck_slot_open_session (test->slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (sess)); - - g_object_unref (sess); - - /* Test opening async */ - gck_slot_open_session_async (test->slot, 0, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - sess = gck_slot_open_session_finish (test->slot, result, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (sess)); - - g_object_unref (result); - g_object_unref (sess); -} - -static void -test_init_set_pin (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GError *err = NULL; - gboolean ret; - - /* init pin */ - ret = gck_session_init_pin (test->session, (guchar*)"booo", 4, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - /* set pin */ - ret = gck_session_set_pin (test->session, (guchar*)"booo", 4, (guchar*)"tooo", 4, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - /* init pin async */ - gck_session_init_pin_async (test->session, (guchar*)"booo", 4, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_init_pin_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (result); - result = NULL; - - /* set pin async */ - gck_session_set_pin_async (test->session, (guchar*)"booo", 4, (guchar*)"tooo", 4, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_set_pin_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (result); - result = NULL; -} - - -static void -test_login_logout (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GError *err = NULL; - gboolean ret; - - /* login/logout */ - ret = gck_session_login (test->session, CKU_USER, (guchar*)"booo", 4, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - ret = gck_session_logout (test->session, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - /* login async */ - gck_session_login_async (test->session, CKU_USER, (guchar*)"booo", 4, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_session_login_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (result); - result = NULL; - - /* logout async */ - gck_session_logout_async (test->session, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_session_logout_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (result); - result = NULL; - -} - -static gboolean -authenticate_token (GckModule *module, GckSlot *slot, gchar *label, gchar **password, gpointer unused) -{ - g_assert (unused == GUINT_TO_POINTER (35)); - g_assert (password != NULL); - g_assert (*password == NULL); - g_assert (GCK_IS_MODULE (module)); - g_assert (GCK_IS_SLOT (slot)); - - *password = g_strdup ("booo"); - return TRUE; -} - -static void -test_auto_login (Test *test, gconstpointer unused) -{ - GckObject *object; - GckSession *new_session; - GAsyncResult *result = NULL; - GError *err = NULL; - GckAttributes *attrs; - gboolean ret; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST OBJECT"); - gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_TRUE); - - /* Try to do something that requires a login */ - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert (!object); - g_assert (err && err->code == CKR_USER_NOT_LOGGED_IN); - g_clear_error (&err); - - /* Setup for auto login */ - g_signal_connect (test->module, "authenticate-slot", G_CALLBACK (authenticate_token), GUINT_TO_POINTER (35)); - new_session = gck_slot_open_session (test->slot, GCK_SESSION_READ_WRITE | GCK_SESSION_LOGIN_USER, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (new_session)); - - /* Try again to do something that requires a login */ - object = gck_session_create_object (new_session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - g_object_unref (object); - - /* We should now be logged in, try to log out */ - ret = gck_session_logout (new_session, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (new_session); - - /* Now try the same thing, but asyncronously */ - gck_slot_open_session_async (test->slot, GCK_SESSION_READ_WRITE | GCK_SESSION_LOGIN_USER, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - new_session = gck_slot_open_session_finish (test->slot, result, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (new_session)); - g_object_unref (result); - - result = NULL; - gck_session_create_object_async (new_session, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - object = gck_session_create_object_finish (new_session, result, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - g_object_unref (result); - g_object_unref (object); - - /* We should now be logged in, try to log out */ - ret = gck_session_logout (new_session, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (new_session); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_set_prgname ("test-gck-session"); - - g_test_add ("/gck/session/session_props", Test, NULL, setup, test_session_props, teardown); - g_test_add ("/gck/session/session_info", Test, NULL, setup, test_session_info, teardown); - g_test_add ("/gck/session/open_close_session", Test, NULL, setup, test_open_close_session, teardown); - g_test_add ("/gck/session/init_set_pin", Test, NULL, setup, test_init_set_pin, teardown); - g_test_add ("/gck/session/login_logout", Test, NULL, setup, test_login_logout, teardown); - g_test_add ("/gck/session/auto_login", Test, NULL, setup, test_auto_login, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-slot.c b/gck/tests/test-gck-slot.c deleted file mode 100644 index affc3b26..00000000 --- a/gck/tests/test-gck-slot.c +++ /dev/null @@ -1,257 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-slot.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include <errno.h> -#include <glib.h> -#include <string.h> - -#include "gck/gck.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -typedef struct { - GckModule *module; - GckSlot *slot; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->slot = GCK_SLOT (slots->data); - g_object_ref (test->slot); - gck_list_unref_free (slots); - -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->slot); - g_object_unref (test->module); -} - -static void -test_slot_info (Test *test, gconstpointer unused) -{ - GckSlotInfo *info; - GckTokenInfo *token; - GList *slots, *l; - - slots = gck_module_get_slots (test->module, FALSE); - g_assert (2 == g_list_length (slots) && "wrong number of slots returned"); - g_assert (GCK_IS_SLOT (slots->data) && "missing slot one"); - g_assert (GCK_IS_SLOT (slots->next->data) && "missing slot two"); - - for (l = slots; l; l = g_list_next (l)) { - info = gck_slot_get_info (GCK_SLOT (l->data)); - g_assert (info != NULL && "no slot info"); - - g_assert (strcmp("TEST MANUFACTURER", info->manufacturer_id) == 0); - g_assert (strcmp("TEST SLOT", info->slot_description) == 0); - g_assert (55 == info->hardware_version_major); - g_assert (155 == info->hardware_version_minor); - g_assert (65 == info->firmware_version_major); - g_assert (165 == info->firmware_version_minor); - - if (info->flags & CKF_TOKEN_PRESENT) { - token = gck_slot_get_token_info (test->slot); - g_assert (token != NULL && "no token info"); - - g_assert (strcmp ("TEST MANUFACTURER", token->manufacturer_id) == 0); - g_assert (strcmp ("TEST LABEL", token->label) == 0); - g_assert (strcmp ("TEST MODEL", token->model) == 0); - g_assert (strcmp ("TEST SERIAL", token->serial_number) == 0); - g_assert (1 == token->max_session_count); - g_assert (2 == token->session_count); - g_assert (3 == token->max_rw_session_count); - g_assert (4 == token->rw_session_count); - g_assert (5 == token->max_pin_len); - g_assert (6 == token->min_pin_len); - g_assert (7 == token->total_public_memory); - g_assert (8 == token->free_public_memory); - g_assert (9 == token->total_private_memory); - g_assert (10 == token->free_private_memory); - g_assert (75 == token->hardware_version_major); - g_assert (175 == token->hardware_version_minor); - g_assert (85 == token->firmware_version_major); - g_assert (185 == token->firmware_version_minor); - g_assert (927623999 == token->utc_time); - - gck_token_info_free (token); - } - - gck_slot_info_free (info); - } - - gck_list_unref_free (slots); -} - -static void -test_slot_props (Test *test, gconstpointer unused) -{ - GckModule *mod; - CK_SLOT_ID slot_id; - - g_object_get (test->slot, "module", &mod, "handle", &slot_id, NULL); - g_assert (mod == test->module); - g_assert (slot_id == 52); - - g_object_unref (mod); -} - -static void -test_slot_equals_hash (Test *test, gconstpointer unused) -{ - GckModule *other_mod; - GckSlot *other_slot; - GObject *obj; - guint hash; - - hash = gck_slot_hash (test->slot); - g_assert (hash != 0); - - g_assert (gck_slot_equal (test->slot, test->slot)); - - other_mod = gck_module_new (gck_module_get_functions (test->module)); - other_slot = g_object_new (GCK_TYPE_SLOT, "module", other_mod, "handle", gck_slot_get_handle (test->slot), NULL); - g_assert (gck_slot_equal (test->slot, other_slot)); - g_object_unref (other_mod); - g_object_unref (other_slot); - - obj = g_object_new (G_TYPE_OBJECT, NULL); - g_assert (!gck_slot_equal (test->slot, obj)); - g_object_unref (obj); - - other_slot = g_object_new (GCK_TYPE_SLOT, "module", test->module, "handle", 8909, NULL); - g_assert (!gck_slot_equal (test->slot, obj)); - g_object_unref (other_slot); -} - -static void -test_slot_mechanisms (Test *test, gconstpointer unused) -{ - GArray *mechs; - GckMechanismInfo *info; - guint i; - - mechs = gck_slot_get_mechanisms (test->slot); - g_assert (2 == gck_mechanisms_length (mechs) && "wrong number of mech types returned"); - - for (i = 0; i < gck_mechanisms_length (mechs); ++i) { - - info = gck_slot_get_mechanism_info (test->slot, gck_mechanisms_at (mechs, i)); - g_assert (info != NULL && "no mech info returned"); - - gck_mechanism_info_free (info); - } - - gck_mechanisms_free (mechs); -} - -static void -test_token_info_match_null (Test *test, gconstpointer unused) -{ - GckTokenInfo *match; - GckTokenInfo *token; - gboolean ret; - - token = gck_slot_get_token_info (test->slot); - match = g_new0 (GckTokenInfo, 1); - - /* Should match, since no fields are set */ - ret = _gck_token_info_match (match, token); - g_assert (ret); - - gck_token_info_free (match); - gck_token_info_free (token); -} - -static void -test_token_info_match_label (Test *test, gconstpointer unused) -{ - GckTokenInfo *match; - GckTokenInfo *token; - gboolean ret; - - token = gck_slot_get_token_info (test->slot); - match = g_new0 (GckTokenInfo, 1); - - /* Should match since the label and serial are matching */ - match->label = g_strdup (token->label); - match->serial_number = g_strdup (token->serial_number); - ret = _gck_token_info_match (match, token); - g_assert (ret); - - gck_token_info_free (match); - gck_token_info_free (token); -} - -static void -test_token_info_match_different (Test *test, gconstpointer unused) -{ - GckTokenInfo *match; - GckTokenInfo *token; - gboolean ret; - - token = gck_slot_get_token_info (test->slot); - match = g_new0 (GckTokenInfo, 1); - - /* Should not match since serial is different */ - match->label = g_strdup (token->label); - match->serial_number = g_strdup ("393939393939393"); - ret = _gck_token_info_match (match, token); - g_assert (!ret); - - gck_token_info_free (match); - gck_token_info_free (token); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add ("/gck/slot/slot_info", Test, NULL, setup, test_slot_info, teardown); - g_test_add ("/gck/slot/slot_props", Test, NULL, setup, test_slot_props, teardown); - g_test_add ("/gck/slot/slot_equals_hash", Test, NULL, setup, test_slot_equals_hash, teardown); - g_test_add ("/gck/slot/slot_mechanisms", Test, NULL, setup, test_slot_mechanisms, teardown); - g_test_add ("/gck/slot/token_info_match_null", Test, NULL, setup, test_token_info_match_null, teardown); - g_test_add ("/gck/slot/token_info_match_label", Test, NULL, setup, test_token_info_match_label, teardown); - g_test_add ("/gck/slot/token_info_match_different", Test, NULL, setup, test_token_info_match_different, teardown); - - return g_test_run (); -} diff --git a/gck/tests/test-gck-uri.c b/gck/tests/test-gck-uri.c deleted file mode 100644 index b8d640f0..00000000 --- a/gck/tests/test-gck-uri.c +++ /dev/null @@ -1,556 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-uri.c: Test routines for PKCS#11 URIs - - Copyright (C) 2011, Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -#include <glib.h> - -#include <errno.h> -#include <string.h> - -static void -test_parse (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:", GCK_URI_FOR_MODULE, &error); - g_assert (uri_data != NULL); - g_assert_no_error (error); - - g_assert (uri_data->attributes == NULL); - g_assert (uri_data->token_info == NULL); - - g_assert (uri_data->module_info != NULL); - g_assert (uri_data->module_info->library_description == NULL); - g_assert (uri_data->module_info->manufacturer_id == NULL); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_bad_scheme (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("http:\\example.com\test", GCK_URI_FOR_ANY, &error); - g_assert (uri_data == NULL); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX); - g_error_free (error); -} - -static void -test_parse_with_label (void) -{ - GError *error = NULL; - GckUriData *uri_data; - gchar *value; - - uri_data = gck_uri_parse ("pkcs11:object=Test%20Label", GCK_URI_FOR_ANY, &error); - g_assert (uri_data != NULL); - g_assert (uri_data->attributes != NULL); - - if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value)) - g_assert_not_reached (); - - g_assert_cmpstr (value, ==, "Test Label"); - g_free (value); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_label_and_klass (void) -{ - GError *error = NULL; - GckUriData *uri_data; - gchar *value; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:object=Test%20Label;objecttype=cert", GCK_URI_FOR_ANY, &error); - g_assert (uri_data); - g_assert (uri_data->attributes); - - if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value)) - g_assert_not_reached (); - - if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - - g_assert_cmpstr (value, ==, "Test Label"); - g_assert (klass == CKO_CERTIFICATE); - g_free (value); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_id (void) -{ - GError *error = NULL; - GckAttribute *attr; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:id=%54%45%53%54%00", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data != NULL); - g_assert (uri_data->attributes != NULL); - - attr = gck_attributes_find (uri_data->attributes, CKA_ID); - g_assert (attr); - g_assert (attr->value); - g_assert (attr->length == 5); - g_assert (memcmp (attr->value, "TEST", 5) == 0); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_bad_string_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:object=Test%", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data == NULL); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_parse_with_bad_binary_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - uri_data = gck_uri_parse ("pkcs11:id=%%", GCK_URI_FOR_ANY, &error); - g_assert (!uri_data); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_parse_with_token (void) -{ - GError *error = NULL; - GckUriData *uri_data = NULL; - - uri_data = gck_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me", - GCK_URI_FOR_TOKEN, &error); - - g_assert (uri_data); - g_assert (uri_data->token_info); - g_assert_cmpstr (uri_data->token_info->label, ==, "Token Label"); - g_assert_cmpstr (uri_data->token_info->serial_number, ==, "3333"); - g_assert_cmpstr (uri_data->token_info->model, ==, "Deluxe"); - g_assert_cmpstr (uri_data->token_info->manufacturer_id, ==, "Me"); - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_token_bad_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:token=Token%", GCK_URI_FOR_TOKEN, &error); - g_assert (!uri_data); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_parse_with_bad_syntax (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:token", GCK_URI_FOR_ANY, &error); - g_assert (uri_data == NULL); - g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX)); - g_error_free (error); -} - -static void -test_parse_with_library (void) -{ - GError *error = NULL; - GckUriData *uri_data = NULL; - - uri_data = gck_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me", - GCK_URI_FOR_MODULE, &error); - - g_assert (uri_data); - g_assert (uri_data->module_info); - g_assert_cmpstr (uri_data->module_info->manufacturer_id, ==, "Me"); - g_assert_cmpstr (uri_data->module_info->library_description, ==, "The Library"); - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_library_bad_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:library-description=Library%", GCK_URI_FOR_MODULE, &error); - g_assert (!uri_data); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_build_empty (void) -{ - GckUriData uri_data; - gchar *uri; - - memset (&uri_data, 0, sizeof (uri_data)); - uri = gck_uri_build (&uri_data, 0); - g_assert_cmpstr (uri, ==, "pkcs11:"); - g_free (uri); -} - -static void -test_build_with_token_info (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - GckUriData *check; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.token_info = g_new0 (GckTokenInfo, 1); - uri_data.token_info->label = g_strdup ("The Label"); - uri_data.token_info->serial_number = g_strdup ("44444"); - uri_data.token_info->manufacturer_id = g_strdup ("Me"); - uri_data.token_info->model = g_strdup ("Deluxe"); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN); - g_assert (uri); - - check = gck_uri_parse (uri, GCK_URI_FOR_TOKEN, NULL); - g_assert (check); - g_assert (check->token_info); - - g_assert (_gck_token_info_match (uri_data.token_info, check->token_info)); - - gck_token_info_free (uri_data.token_info); - gck_uri_data_free (check); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "token=The%20Label")); - g_assert (strstr (uri, "serial=44444")); - g_assert (strstr (uri, "manufacturer=Me")); - g_assert (strstr (uri, "model=Deluxe")); - - g_free (uri); -} - -static void -test_build_with_token_null_info (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.token_info = g_new0 (GckTokenInfo, 1); - uri_data.token_info->label = g_strdup ("The Label"); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN); - g_assert (uri); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "token=The%20Label")); - g_assert (!strstr (uri, "serial=")); - - gck_token_info_free (uri_data.token_info); - g_free (uri); -} - -static void -test_build_with_token_empty_info (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.token_info = g_new0 (GckTokenInfo, 1); - uri_data.token_info->label = g_strdup ("The Label"); - uri_data.token_info->serial_number = g_strdup (""); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN); - g_assert (uri); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "token=The%20Label")); - g_assert (strstr (uri, "serial=")); - - gck_token_info_free (uri_data.token_info); - g_free (uri); -} - -static void -test_build_with_attributes (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - GckUriData *check; - gchar *string; - gulong value; - GckAttribute *attr; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.attributes = gck_attributes_new (); - gck_attributes_add_string (uri_data.attributes, CKA_LABEL, "The Label"); - gck_attributes_add_ulong (uri_data.attributes, CKA_CLASS, CKO_DATA); - gck_attributes_add_data (uri_data.attributes, CKA_ID, (const guchar *)"TEST", 5); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - - gck_attributes_unref (uri_data.attributes); - - check = gck_uri_parse (uri, GCK_URI_FOR_ANY, NULL); - g_assert (check); - g_assert (check->attributes); - - if (!gck_attributes_find_string (check->attributes, CKA_LABEL, &string)) - g_assert_not_reached (); - g_assert_cmpstr (string, ==, "The Label"); - - if (!gck_attributes_find_ulong (check->attributes, CKA_CLASS, &value)) - g_assert_not_reached (); - g_assert (value == CKO_DATA); - - attr = gck_attributes_find (check->attributes, CKA_ID); - g_assert (attr); - g_assert (attr->length == 5); - g_assert (memcmp (attr->value, "TEST", 5) == 0); - - gck_uri_data_free (check); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "object=The%20Label")); - g_assert (strstr (uri, "object-type=data")); - g_assert (strstr (uri, "id=TEST%00")); - - g_free (uri); -} - -static void -test_parse_private_key (void) -{ - GckUriData *uri_data; - GError *error = NULL; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:objecttype=private", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data); - g_assert_no_error (error); - - g_assert (uri_data->attributes); - if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - gck_assert_cmpulong (klass, ==, CKO_PRIVATE_KEY); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_secret_key (void) -{ - GckUriData *uri_data; - GError *error = NULL; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:objecttype=secretkey", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data); - g_assert_no_error (error); - - g_assert (uri_data->attributes); - if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - gck_assert_cmpulong (klass, ==, CKO_SECRET_KEY); - - gck_uri_data_free (uri_data); -} - - -static void -test_parse_unknown_objecttype (void) -{ - GckUriData *uri_data; - GError *error = NULL; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:objecttype=unknown", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data); - g_assert_no_error (error); - - g_assert (uri_data->attributes); - g_assert (uri_data->any_unrecognized == TRUE); - if (gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - - gck_uri_data_free (uri_data); -} - -static void -test_build_objecttype_cert (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_CERTIFICATE); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=cert")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_objecttype_private (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_PRIVATE_KEY); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=private")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_objecttype_public (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_PUBLIC_KEY); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=public")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_objecttype_secret (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_SECRET_KEY); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=secret-key")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_with_library (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->module_info = g_new0 (GckModuleInfo, 1); - uri_data->module_info->library_description = g_strdup ("The Description"); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_MODULE); - g_assert (uri); - g_assert (strstr (uri, "library-description=The%20Description")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - - -static void -null_log_handler (const gchar *log_domain, GLogLevelFlags log_level, - const gchar *message, gpointer user_data) -{ - -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - /* Suppress these messages in tests */ - g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, - null_log_handler, NULL); - - g_test_add_func ("/gck/uri/parse", test_parse); - g_test_add_func ("/gck/uri/parse_bad_scheme", test_parse_bad_scheme); - g_test_add_func ("/gck/uri/parse_with_label", test_parse_with_label); - g_test_add_func ("/gck/uri/parse_with_label_and_klass", test_parse_with_label_and_klass); - g_test_add_func ("/gck/uri/parse_with_id", test_parse_with_id); - g_test_add_func ("/gck/uri/parse_with_bad_string_encoding", test_parse_with_bad_string_encoding); - g_test_add_func ("/gck/uri/parse_with_bad_binary_encoding", test_parse_with_bad_binary_encoding); - g_test_add_func ("/gck/uri/parse_with_token", test_parse_with_token); - g_test_add_func ("/gck/uri/parse_with_token_bad_encoding", test_parse_with_token_bad_encoding); - g_test_add_func ("/gck/uri/parse_with_bad_syntax", test_parse_with_bad_syntax); - g_test_add_func ("/gck/uri/parse_with_library", test_parse_with_library); - g_test_add_func ("/gck/uri/parse_with_library_bad_encoding", test_parse_with_library_bad_encoding); - g_test_add_func ("/gck/uri/build_empty", test_build_empty); - g_test_add_func ("/gck/uri/build_with_token_info", test_build_with_token_info); - g_test_add_func ("/gck/uri/build_with_token_null_info", test_build_with_token_null_info); - g_test_add_func ("/gck/uri/build_with_token_empty_info", test_build_with_token_empty_info); - g_test_add_func ("/gck/uri/build_with_attributes", test_build_with_attributes); - g_test_add_func ("/gck/uri/parse_private_key", test_parse_private_key); - g_test_add_func ("/gck/uri/parse_secret_key", test_parse_secret_key); - g_test_add_func ("/gck/uri/parse_unknown_objecttype", test_parse_unknown_objecttype); - g_test_add_func ("/gck/uri/build_objecttype_cert", test_build_objecttype_cert); - g_test_add_func ("/gck/uri/build_objecttype_private", test_build_objecttype_private); - g_test_add_func ("/gck/uri/build_objecttype_public", test_build_objecttype_public); - g_test_add_func ("/gck/uri/build_objecttype_secret", test_build_objecttype_secret); - g_test_add_func ("/gck/uri/build_with_library", test_build_with_library); - - return g_test_run (); -} |