[gnome-keyring] gcr: Add support for listing secret gnupg keys.
- From: Stefan Walter <stefw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-keyring] gcr: Add support for listing secret gnupg keys.
- Date: Sun, 15 May 2011 11:09:02 +0000 (UTC)
commit abfe89a22746a263f0cdec9f9c0802dc16920829
Author: Stef Walter <stefw collabora co uk>
Date: Tue Apr 26 23:06:48 2011 +0200
gcr: Add support for listing secret gnupg keys.
* Also add support to GcrGnupgKey for holding the secret info.
https://bugzilla.gnome.org/show_bug.cgi?id=648019
docs/reference/gcr/gcr-sections.txt | 2 +
gcr/gcr-colons.h | 9 ++
gcr/gcr-gnupg-collection.c | 186 +++++++++++++++++++++++++----------
gcr/gcr-gnupg-key.c | 109 ++++++++++++++++-----
gcr/gcr-gnupg-key.h | 9 ++-
5 files changed, 236 insertions(+), 79 deletions(-)
---
diff --git a/docs/reference/gcr/gcr-sections.txt b/docs/reference/gcr/gcr-sections.txt
index 2f652b9..b28a131 100644
--- a/docs/reference/gcr/gcr-sections.txt
+++ b/docs/reference/gcr/gcr-sections.txt
@@ -435,6 +435,7 @@ GcrUnlockOptionsWidgetPrivate
<SUBSECTION Private>
GCR_COLONS_SCHEMA_PUB
GCR_COLONS_SCHEMA_UID
+GCR_COLONS_SCHEMA_SEC
GCR_GNUPG_COLLECTION
GCR_GNUPG_COLLECTION_CLASS
GCR_GNUPG_COLLECTION_GET_CLASS
@@ -451,6 +452,7 @@ GCR_TYPE_GNUPG_KEY
GcrColonColumns
GcrColonPubColumns
GcrColonUidColumns
+GcrColonSecColumns
GcrColons
GcrGnupgCollection
GcrGnupgCollectionClass
diff --git a/gcr/gcr-colons.h b/gcr/gcr-colons.h
index 5333998..ac89681 100644
--- a/gcr/gcr-colons.h
+++ b/gcr/gcr-colons.h
@@ -52,6 +52,7 @@ G_BEGIN_DECLS
#define GCR_COLONS_SCHEMA_UID (g_quark_from_static_string ("uid"))
#define GCR_COLONS_SCHEMA_PUB (g_quark_from_static_string ("pub"))
+#define GCR_COLONS_SCHEMA_SEC (g_quark_from_static_string ("sec"))
/* Common columns for all schemas */
typedef enum {
@@ -67,6 +68,14 @@ typedef enum {
} GcrColonPubColumns;
/*
+ * Columns for sec schema, add them as they're used. eg:
+ * sec::2048:1:293FC71A513189BD:1299771018::::::::::
+ */
+typedef enum {
+ GCR_COLONS_SEC_KEYID = 4
+} GcrColonSecColumns;
+
+/*
* Columns for uid schema, add them as they're used. eg:
* pub:f:1024:17:6C7EE1B8621CC013:899817715:1055898235::m:::scESC:
*/
diff --git a/gcr/gcr-gnupg-collection.c b/gcr/gcr-gnupg-collection.c
index c6accf4..ffdb097 100644
--- a/gcr/gcr-gnupg-collection.c
+++ b/gcr/gcr-gnupg-collection.c
@@ -41,11 +41,17 @@ enum {
PROP_MAIN_CONTEXT
};
+enum {
+ PHASE_PUBLIC_KEYS = 1,
+ PHASE_SECRET_KEYS = 2,
+};
+
struct _GcrGnupgCollectionPrivate {
- GHashTable *items; /* Map of char *keyid -> GcrGnupgKey *key */
+ GHashTable *items; /* char *keyid -> GcrGnupgKey* */
gchar *directory;
};
+/* Forward declarations */
static void _gcr_collection_iface (GcrCollectionIface *iface);
G_DEFINE_TYPE_WITH_CODE (GcrGnupgCollection, _gcr_gnupg_collection, G_TYPE_OBJECT,
@@ -192,6 +198,7 @@ _gcr_gnupg_collection_new (const gchar *directory)
typedef struct {
GcrGnupgCollection *collection; /* reffed pointer back to collection */
+ gint loading_phase;
GPtrArray *dataset; /* GcrColons* not yet made into a key */
guint spawn_sig;
guint child_sig;
@@ -201,6 +208,9 @@ typedef struct {
GHashTable *difference; /* Hashset gchar *keyid -> gchar *keyid */
} GcrGnupgCollectionLoad;
+/* Forward declarations */
+static void spawn_gnupg_list_process (GcrGnupgCollectionLoad *load, GSimpleAsyncResult *res);
+
static void
_gcr_gnupg_collection_load_free (gpointer data)
{
@@ -225,11 +235,51 @@ _gcr_gnupg_collection_load_free (gpointer data)
}
static void
+process_dataset_as_public_key (GcrGnupgCollectionLoad *load, GPtrArray *dataset,
+ const gchar *keyid)
+{
+ GcrGnupgKey *key;
+
+ /* Note that we've seen this keyid */
+ g_hash_table_remove (load->difference, keyid);
+
+ key = g_hash_table_lookup (load->collection->pv->items, keyid);
+
+ /* Already have this key, just update */
+ if (key) {
+ _gcr_gnupg_key_set_public_dataset (key, dataset);
+
+ /* Add a new key */
+ } else {
+ key = _gcr_gnupg_key_new (dataset);
+ g_hash_table_insert (load->collection->pv->items, g_strdup (keyid), key);
+ gcr_collection_emit_added (GCR_COLLECTION (load->collection), G_OBJECT (key));
+ }
+}
+
+static void
+process_dataset_as_secret_key (GcrGnupgCollectionLoad *load, GPtrArray *dataset,
+ const gchar *keyid)
+{
+ GcrGnupgKey *key;
+
+ key = g_hash_table_lookup (load->collection->pv->items, keyid);
+
+ /* Don't have this key */
+ if (key == NULL)
+ g_message ("secret key seen but no public key for: %s", keyid);
+
+ /* Tell the private key that it's a secret one */
+ else
+ _gcr_gnupg_key_set_secret_dataset (key, dataset);
+}
+
+static void
process_dataset_as_key (GcrGnupgCollectionLoad *load)
{
- const gchar *keyid;
GPtrArray *dataset;
- GcrGnupgKey *key;
+ const gchar *keyid;
+ GQuark schema;
g_assert (load->dataset->len);
@@ -238,21 +288,18 @@ process_dataset_as_key (GcrGnupgCollectionLoad *load)
keyid = _gcr_gnupg_key_get_keyid_for_colons (dataset);
if (keyid) {
- /* Note that we've seen this keyid */
- g_hash_table_remove (load->difference, keyid);
+ schema = _gcr_colons_get_schema (dataset->pdata[0]);
- key = g_hash_table_lookup (load->collection->pv->items, keyid);
+ /* A public key */
+ if (schema == GCR_COLONS_SCHEMA_PUB)
+ process_dataset_as_public_key (load, dataset, keyid);
- /* Already have this key, just update */
- if (key) {
- _gcr_gnupg_key_set_dataset (key, dataset);
+ /* A secret key */
+ else if (schema == GCR_COLONS_SCHEMA_SEC)
+ process_dataset_as_secret_key (load, dataset, keyid);
- /* Add a new key */
- } else {
- key = _gcr_gnupg_key_new (dataset);
- g_hash_table_insert (load->collection->pv->items, g_strdup (keyid), key);
- gcr_collection_emit_added (GCR_COLLECTION (load->collection), G_OBJECT (key));
- }
+ else
+ g_assert_not_reached ();
} else {
g_warning ("parsed gnupg data had no keyid");
@@ -277,10 +324,10 @@ on_line_parse_output (const gchar *line, gpointer user_data)
schema = _gcr_colons_get_schema (colons);
/*
- * Each time we see a line with 'pub' schema we assume that it's
- * a new key being listed.
+ * Each time we see a line with 'pub' or 'sec' schema we assume that
+ * it's a new key being listed.
*/
- if (schema == GCR_COLONS_SCHEMA_PUB) {
+ if (schema == GCR_COLONS_SCHEMA_PUB || schema == GCR_COLONS_SCHEMA_SEC) {
if (load->dataset->len)
process_dataset_as_key (load);
g_assert (!load->dataset->len);
@@ -371,6 +418,19 @@ on_spawn_completed (gpointer user_data)
if (load->dataset->len)
process_dataset_as_key (load);
+ /* If we completed loading public keys, then go and load secret */
+ switch (load->loading_phase) {
+ case PHASE_PUBLIC_KEYS:
+ load->loading_phase = PHASE_SECRET_KEYS;
+ spawn_gnupg_list_process (load, res);
+ return;
+ case PHASE_SECRET_KEYS:
+ /* continue below */
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
/* Remove any keys that we still have in the difference */
g_hash_table_iter_init (&iter, load->difference);
while (g_hash_table_iter_next (&iter, &keyid, NULL)) {
@@ -419,6 +479,61 @@ static EggSpawnCallbacks spawn_callbacks = {
NULL
};
+static void
+spawn_gnupg_list_process (GcrGnupgCollectionLoad *load, GSimpleAsyncResult *res)
+{
+ GError *error = NULL;
+ GPtrArray *argv;
+
+ argv = g_ptr_array_new ();
+ g_ptr_array_add (argv, (gpointer)GPG_EXECUTABLE);
+
+ switch (load->loading_phase) {
+ case PHASE_PUBLIC_KEYS:
+ g_ptr_array_add (argv, (gpointer)"--list-keys");
+ break;
+ case PHASE_SECRET_KEYS:
+ g_ptr_array_add (argv, (gpointer)"--list-secret-keys");
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ g_ptr_array_add (argv, (gpointer)"--fixed-list-mode");
+ g_ptr_array_add (argv, (gpointer)"--with-colons");
+ if (load->collection->pv->directory) {
+ g_ptr_array_add (argv, (gpointer)"--homedir");
+ g_ptr_array_add (argv, (gpointer)load->collection->pv->directory);
+ }
+ g_ptr_array_add (argv, NULL);
+
+ g_assert (!load->spawn_sig);
+ g_assert (!load->gnupg_pid);
+
+ load->spawn_sig = egg_spawn_async_with_callbacks (load->collection->pv->directory,
+ (gchar**)argv->pdata, NULL,
+ G_SPAWN_DO_NOT_REAP_CHILD,
+ &load->gnupg_pid,
+ &spawn_callbacks,
+ g_object_ref (res),
+ NULL, &error);
+
+ if (error) {
+ g_simple_async_result_set_from_error (res, error);
+ g_simple_async_result_complete_in_idle (res);
+ g_clear_error (&error);
+ } else {
+ g_assert (!load->child_sig);
+ load->child_sig = g_child_watch_add_full (G_PRIORITY_DEFAULT,
+ load->gnupg_pid,
+ on_child_exited,
+ g_object_ref (res),
+ g_object_unref);
+ }
+
+ g_ptr_array_unref (argv);
+}
+
/**
* _gcr_gnupg_collection_load_async:
* @self: The collection
@@ -435,8 +550,6 @@ _gcr_gnupg_collection_load_async (GcrGnupgCollection *self, GCancellable *cancel
{
GSimpleAsyncResult *res;
GcrGnupgCollectionLoad *load;
- GError *error = NULL;
- GPtrArray *argv;
GHashTableIter iter;
gpointer keyid;
@@ -444,17 +557,6 @@ _gcr_gnupg_collection_load_async (GcrGnupgCollection *self, GCancellable *cancel
/* TODO: Cancellation not yet implemented */
- argv = g_ptr_array_new ();
- g_ptr_array_add (argv, (gpointer)GPG_EXECUTABLE);
- g_ptr_array_add (argv, (gpointer)"--list-keys");
- g_ptr_array_add (argv, (gpointer)"--fixed-list-mode");
- g_ptr_array_add (argv, (gpointer)"--with-colons");
- if (self->pv->directory) {
- g_ptr_array_add (argv, (gpointer)"--homedir");
- g_ptr_array_add (argv, (gpointer)self->pv->directory);
- }
- g_ptr_array_add (argv, NULL);
-
res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
_gcr_gnupg_collection_load_async);
@@ -476,28 +578,10 @@ _gcr_gnupg_collection_load_async (GcrGnupgCollection *self, GCancellable *cancel
g_simple_async_result_set_op_res_gpointer (res, load,
_gcr_gnupg_collection_load_free);
- load->spawn_sig = egg_spawn_async_with_callbacks (self->pv->directory,
- (gchar**)argv->pdata, NULL,
- G_SPAWN_DO_NOT_REAP_CHILD,
- &load->gnupg_pid,
- &spawn_callbacks,
- g_object_ref (res),
- NULL, &error);
-
- if (error) {
- g_simple_async_result_set_from_error (res, error);
- g_simple_async_result_complete_in_idle (res);
- g_clear_error (&error);
- } else {
- load->child_sig = g_child_watch_add_full (G_PRIORITY_DEFAULT,
- load->gnupg_pid,
- on_child_exited,
- g_object_ref (res),
- g_object_unref);
- }
+ load->loading_phase = PHASE_PUBLIC_KEYS;
+ spawn_gnupg_list_process (load, res);
g_object_unref (res);
- g_ptr_array_unref (argv);
}
/**
diff --git a/gcr/gcr-gnupg-key.c b/gcr/gcr-gnupg-key.c
index 874a3ba..0bf2c15 100644
--- a/gcr/gcr-gnupg-key.c
+++ b/gcr/gcr-gnupg-key.c
@@ -31,7 +31,8 @@
enum {
PROP_0,
- PROP_DATASET,
+ PROP_PUBLIC_DATASET,
+ PROP_SECRET_DATASET,
PROP_LABEL,
PROP_MARKUP,
PROP_DESCRIPTION,
@@ -39,7 +40,8 @@ enum {
};
struct _GcrGnupgKeyPrivate {
- GPtrArray *dataset;
+ GPtrArray *public_dataset;
+ GPtrArray *secret_dataset;
};
G_DEFINE_TYPE (GcrGnupgKey, _gcr_gnupg_key, G_TYPE_OBJECT);
@@ -53,7 +55,7 @@ calculate_name (GcrGnupgKey *self)
{
GcrColons* colons;
- colons = _gcr_colons_find (self->pv->dataset, GCR_COLONS_SCHEMA_UID);
+ colons = _gcr_colons_find (self->pv->public_dataset, GCR_COLONS_SCHEMA_UID);
g_return_val_if_fail (colons, NULL);
return _gcr_colons_get_string (colons, GCR_COLONS_UID_NAME);
@@ -79,7 +81,7 @@ calculate_keyid (GcrGnupgKey *self)
const gchar *keyid;
gsize length;
- keyid = _gcr_gnupg_key_get_keyid_for_colons (self->pv->dataset);
+ keyid = _gcr_gnupg_key_get_keyid_for_colons (self->pv->public_dataset);
if (keyid == NULL)
return NULL;
@@ -101,8 +103,10 @@ _gcr_gnupg_key_finalize (GObject *obj)
{
GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
- if (self->pv->dataset)
- g_ptr_array_free (self->pv->dataset, TRUE);
+ if (self->pv->public_dataset)
+ g_ptr_array_free (self->pv->public_dataset, TRUE);
+ if (self->pv->secret_dataset)
+ g_ptr_array_free (self->pv->secret_dataset, TRUE);
G_OBJECT_CLASS (_gcr_gnupg_key_parent_class)->finalize (obj);
}
@@ -114,8 +118,11 @@ _gcr_gnupg_key_set_property (GObject *obj, guint prop_id, const GValue *value,
GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
switch (prop_id) {
- case PROP_DATASET:
- _gcr_gnupg_key_set_dataset (self, g_value_get_boxed (value));
+ case PROP_PUBLIC_DATASET:
+ _gcr_gnupg_key_set_public_dataset (self, g_value_get_boxed (value));
+ break;
+ case PROP_SECRET_DATASET:
+ _gcr_gnupg_key_set_secret_dataset (self, g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
@@ -130,8 +137,11 @@ _gcr_gnupg_key_get_property (GObject *obj, guint prop_id, GValue *value,
GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
switch (prop_id) {
- case PROP_DATASET:
- g_value_set_boxed (value, self->pv->dataset);
+ case PROP_PUBLIC_DATASET:
+ g_value_set_boxed (value, self->pv->public_dataset);
+ break;
+ case PROP_SECRET_DATASET:
+ g_value_set_boxed (value, self->pv->secret_dataset);
break;
case PROP_LABEL:
g_value_take_string (value, calculate_name (self));
@@ -163,8 +173,12 @@ _gcr_gnupg_key_class_init (GcrGnupgKeyClass *klass)
gobject_class->set_property = _gcr_gnupg_key_set_property;
gobject_class->get_property = _gcr_gnupg_key_get_property;
- g_object_class_install_property (gobject_class, PROP_DATASET,
- g_param_spec_boxed ("dataset", "Dataset", "Colon Dataset",
+ g_object_class_install_property (gobject_class, PROP_PUBLIC_DATASET,
+ g_param_spec_boxed ("public-dataset", "Public Dataset", "Public Key Colon Dataset",
+ G_TYPE_PTR_ARRAY, G_PARAM_READWRITE));
+
+ g_object_class_install_property (gobject_class, PROP_SECRET_DATASET,
+ g_param_spec_boxed ("secret-dataset", "Secret Dataset", "Secret Key Colon Dataset",
G_TYPE_PTR_ARRAY, G_PARAM_READWRITE));
g_object_class_install_property (gobject_class, PROP_LABEL,
@@ -197,11 +211,11 @@ GcrGnupgKey*
_gcr_gnupg_key_new (GPtrArray *dataset)
{
g_return_val_if_fail (dataset, NULL);
- return g_object_new (GCR_TYPE_GNUPG_KEY, "dataset", dataset, NULL);
+ return g_object_new (GCR_TYPE_GNUPG_KEY, "public-dataset", dataset, NULL);
}
/**
- * _gcr_gnupg_key_get_dataset:
+ * _gcr_gnupg_key_get_public_dataset:
* @self: The key
*
* Get the colons data this key is based on.
@@ -209,21 +223,21 @@ _gcr_gnupg_key_new (GPtrArray *dataset)
* Returns: An array of GcrColons*, owned by the key.
*/
GPtrArray*
-_gcr_gnupg_key_get_dataset (GcrGnupgKey *self)
+_gcr_gnupg_key_get_public_dataset (GcrGnupgKey *self)
{
g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
- return self->pv->dataset;
+ return self->pv->public_dataset;
}
/**
- * _gcr_gnupg_key_set_dataset:
+ * _gcr_gnupg_key_set_public_dataset:
* @self: The key
* @dataset: The new array of GcrColons*
*
* Change the colons data that this key is based on.
*/
void
-_gcr_gnupg_key_set_dataset (GcrGnupgKey *self, GPtrArray *dataset)
+_gcr_gnupg_key_set_public_dataset (GcrGnupgKey *self, GPtrArray *dataset)
{
GObject *obj;
@@ -231,13 +245,13 @@ _gcr_gnupg_key_set_dataset (GcrGnupgKey *self, GPtrArray *dataset)
g_return_if_fail (dataset);
g_ptr_array_ref (dataset);
- if (self->pv->dataset)
- g_ptr_array_unref (self->pv->dataset);
- self->pv->dataset = dataset;
+ if (self->pv->public_dataset)
+ g_ptr_array_unref (self->pv->public_dataset);
+ self->pv->public_dataset = dataset;
obj = G_OBJECT (self);
g_object_freeze_notify (obj);
- g_object_notify (obj, "dataset");
+ g_object_notify (obj, "public-dataset");
g_object_notify (obj, "label");
g_object_notify (obj, "markup");
g_object_notify (obj, "keyid");
@@ -245,6 +259,47 @@ _gcr_gnupg_key_set_dataset (GcrGnupgKey *self, GPtrArray *dataset)
}
/**
+ * _gcr_gnupg_key_get_secret_dataset:
+ * @self: The key
+ *
+ * Get the colons secret data this key is based on. %NULL if a public key.
+ *
+ * Returns: An array of GcrColons*, owned by the key.
+ */
+GPtrArray*
+_gcr_gnupg_key_get_secret_dataset (GcrGnupgKey *self)
+{
+ g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
+ return self->pv->secret_dataset;
+}
+
+/**
+ * _gcr_gnupg_key_set_secret_dataset:
+ * @self: The key
+ * @dataset: The new array of GcrColons*
+ *
+ * Set the secret data for this key. %NULL if public key.
+ */
+void
+_gcr_gnupg_key_set_secret_dataset (GcrGnupgKey *self, GPtrArray *dataset)
+{
+ GObject *obj;
+
+ g_return_if_fail (GCR_IS_GNUPG_KEY (self));
+ g_return_if_fail (dataset);
+
+ g_ptr_array_ref (dataset);
+ if (self->pv->secret_dataset)
+ g_ptr_array_unref (self->pv->secret_dataset);
+ self->pv->secret_dataset = dataset;
+
+ obj = G_OBJECT (self);
+ g_object_freeze_notify (obj);
+ g_object_notify (obj, "secret-dataset");
+ g_object_thaw_notify (obj);
+}
+
+/**
* _gcr_gnupg_key_get_keyid_for_colons:
* @dataset: Array of GcrColons*
*
@@ -258,10 +313,12 @@ _gcr_gnupg_key_get_keyid_for_colons (GPtrArray *dataset)
GcrColons *colons;
colons = _gcr_colons_find (dataset, GCR_COLONS_SCHEMA_PUB);
- if (colons == NULL)
- return NULL;
-
- return _gcr_colons_get_raw (colons, GCR_COLONS_PUB_KEYID);
+ if (colons != NULL)
+ return _gcr_colons_get_raw (colons, GCR_COLONS_PUB_KEYID);
+ colons = _gcr_colons_find (dataset, GCR_COLONS_SCHEMA_SEC);
+ if (colons != NULL)
+ return _gcr_colons_get_raw (colons, GCR_COLONS_SEC_KEYID);
+ return NULL;
}
/**
diff --git a/gcr/gcr-gnupg-key.h b/gcr/gcr-gnupg-key.h
index b1b8765..1fe45e1 100644
--- a/gcr/gcr-gnupg-key.h
+++ b/gcr/gcr-gnupg-key.h
@@ -63,9 +63,14 @@ const GcrColumn* _gcr_gnupg_key_get_columns (void);
GcrGnupgKey* _gcr_gnupg_key_new (GPtrArray *dataset);
-GPtrArray* _gcr_gnupg_key_get_dataset (GcrGnupgKey *self);
+GPtrArray* _gcr_gnupg_key_get_public_dataset (GcrGnupgKey *self);
-void _gcr_gnupg_key_set_dataset (GcrGnupgKey *self,
+void _gcr_gnupg_key_set_public_dataset (GcrGnupgKey *self,
+ GPtrArray *dataset);
+
+GPtrArray* _gcr_gnupg_key_get_secret_dataset (GcrGnupgKey *self);
+
+void _gcr_gnupg_key_set_secret_dataset (GcrGnupgKey *self,
GPtrArray *dataset);
const gchar* _gcr_gnupg_key_get_keyid_for_colons (GPtrArray *dataset);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]