From 3a4ea12754506efd77dc371916896b606a0092fe Mon Sep 17 00:00:00 2001 From: Stefan Walter Date: Thu, 26 Feb 2009 22:11:53 +0000 Subject: Implement a testing tool to dump gck data files such as the user key store. svn path=/trunk/; revision=1621 --- ChangeLog | 10 ++++ pkcs11/gck/gck-data-file.c | 52 ++++++++++++++++++++ pkcs11/gck/gck-data-file.h | 5 ++ pkcs11/gck/tests/.gitignore | 1 + pkcs11/gck/tests/Makefile.am | 14 ++++++ pkcs11/gck/tests/dump-data-file.c | 100 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 pkcs11/gck/tests/dump-data-file.c diff --git a/ChangeLog b/ChangeLog index 8b8a08cc..b98f66c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2009-02-25 Stef Walter + + * pkcs11/gck/gck-data-file.c: + * pkcs11/gck/gck-data-file.h: + * pkcs11/gck/tests/.gitignore: + * pkcs11/gck/tests/Makefile.am: + * pkcs11/gck/tests/dump-data-file.c: (added) + Implement a testing tool to dump gck data files such as the user + key store. + 2009-02-24 Stef Walter * tool/gkr-tool-import.c: Exit when an error occurs on import. diff --git a/pkcs11/gck/gck-data-file.c b/pkcs11/gck/gck-data-file.c index 8cef5f5f..f5f6205c 100644 --- a/pkcs11/gck/gck-data-file.c +++ b/pkcs11/gck/gck-data-file.c @@ -29,6 +29,7 @@ #include "gck-util.h" #include "egg/egg-buffer.h" +#include "egg/egg-hex.h" #include "egg/egg-secure-memory.h" #include "egg/egg-symkey.h" @@ -994,6 +995,46 @@ emit_each_added_identifier (gpointer key, gpointer value, gpointer data) g_signal_emit (self, signals[ENTRY_ADDED], 0, key); } +static void +dump_attributes (gpointer key, gpointer value, gpointer user_data) +{ + CK_ATTRIBUTE_PTR attr = value; + gulong *type = key; + gchar *text; + + g_assert (type); + g_assert (value); + + if (attr->pValue == NULL) + text = g_strdup ("NULL"); + else + text = egg_hex_encode_full (attr->pValue, attr->ulValueLen, TRUE, ' ', 1); + + g_print ("\t0x%08x: %s\n", (guint)*type, text); + g_free (text); +} + +static void +dump_identifier_and_attributes (GckDataFile *self, const gchar *identifier, gpointer user_data) +{ + GHashTable *attributes; + guint section; + + g_assert (GCK_IS_DATA_FILE (self)); + + if (!gck_data_file_lookup_entry (self, identifier, §ion)) + g_assert_not_reached (); + + if (GPOINTER_TO_UINT (user_data) == section) { + g_print ("%s\n", identifier); + if (identifier_to_attributes (self, identifier, &attributes) != GCK_DATA_SUCCESS) + g_assert_not_reached (); + g_hash_table_foreach (attributes, dump_attributes, NULL); + g_print ("\n"); + } +} + + /* ----------------------------------------------------------------------------- * OBJECT */ @@ -1431,3 +1472,14 @@ gck_data_file_have_section (GckDataFile *self, guint section) { return (self->sections & section) ? TRUE : FALSE; } + +void +gck_data_file_dump (GckDataFile *self) +{ + g_print ("PUBLIC:\n\n"); + gck_data_file_foreach_entry (self, dump_identifier_and_attributes, + GUINT_TO_POINTER (GCK_DATA_FILE_SECTION_PUBLIC)); + g_print ("PRIVATE:\n\n"); + gck_data_file_foreach_entry (self, dump_identifier_and_attributes, + GUINT_TO_POINTER (GCK_DATA_FILE_SECTION_PRIVATE)); +} diff --git a/pkcs11/gck/gck-data-file.h b/pkcs11/gck/gck-data-file.h index 32c7b7dc..bee45245 100644 --- a/pkcs11/gck/gck-data-file.h +++ b/pkcs11/gck/gck-data-file.h @@ -100,5 +100,10 @@ GckDataResult gck_data_file_read_value (GckDataFile *sel gulong type, gconstpointer *value, gsize *n_value); + +void gck_data_file_foreach_value (GckDataFile *self, + const gchar *identifier); +void gck_data_file_dump (GckDataFile *self); + #endif /* __GCK_DATA_FILE_H__ */ diff --git a/pkcs11/gck/tests/.gitignore b/pkcs11/gck/tests/.gitignore index a2339325..e13930f6 100644 --- a/pkcs11/gck/tests/.gitignore +++ b/pkcs11/gck/tests/.gitignore @@ -6,3 +6,4 @@ /Makefile.in /run-auto-test /run-prompt-test +/dump-data-file diff --git a/pkcs11/gck/tests/Makefile.am b/pkcs11/gck/tests/Makefile.am index c0ed0df8..5e3ba540 100644 --- a/pkcs11/gck/tests/Makefile.am +++ b/pkcs11/gck/tests/Makefile.am @@ -29,3 +29,17 @@ EXTRA_DIST = \ test-data include $(top_srcdir)/tests/gtest.make + +# --------------------------------------------------------------------- + +noinst_PROGRAMS += \ + dump-data-file + +dump_data_file_SOURCES = \ + dump-data-file.c + +dump_data_file_LDADD = \ + $(top_builddir)/pkcs11/gck/libgck.la \ + $(top_builddir)/egg/libegg.la \ + $(DAEMON_LIBS) + \ No newline at end of file diff --git a/pkcs11/gck/tests/dump-data-file.c b/pkcs11/gck/tests/dump-data-file.c new file mode 100644 index 00000000..0882b8eb --- /dev/null +++ b/pkcs11/gck/tests/dump-data-file.c @@ -0,0 +1,100 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* dump-data-file.c: Dump a gck data file + + Copyright (C) 2009 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 +*/ + +#include "gck-crypto.h" +#include "gck-data-file.h" + +#include "egg/egg-secure-memory.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +void egg_memory_lock (void) + { } +void egg_memory_unlock (void) + { } +void* egg_memory_fallback (void *p, size_t sz) + { return g_realloc (p, sz); } + +static void G_GNUC_NORETURN +failure (const gchar* message, ...) +{ + va_list va; + va_start (va, message); + vfprintf (stderr, message, va); + fputc ('\n', stderr); + va_end (va); + exit (1); +} + +int +main(int argc, char* argv[]) +{ + const gchar *password; + GckDataResult res; + GckDataFile *file; + GckLogin *login; + int fd; + + g_type_init (); + gck_crypto_initialize (); + + if (argc != 2) + failure ("usage: dump-data-file filename"); + + fd = open (argv[1], O_RDONLY, 0); + if (fd == -1) + failure ("dump-data-file: couldn't open file: %s: %s", argv[1], g_strerror (errno)); + + password = getpass ("Password: "); + login = gck_login_new ((guchar*)password, strlen (password)); + + file = gck_data_file_new (); + res = gck_data_file_read_fd (file, fd, login); + g_object_unref (login); + + switch(res) { + case GCK_DATA_FAILURE: + failure ("dump-data-file: failed to read file: %s", argv[1]); + case GCK_DATA_LOCKED: + failure ("dump-data-file: invalid password for file: %s", argv[1]); + case GCK_DATA_UNRECOGNIZED: + failure ("dump-data-file: unparseable file format: %s", argv[1]); + case GCK_DATA_SUCCESS: + break; + default: + g_assert_not_reached (); + } + + gck_data_file_dump (file); + g_object_unref (file); + + return 0; +} -- cgit v1.2.1