[gnome-keyring/wip/dueno/ssh-agent-2] ssh-agent: Use stock ssh-agent



commit 90fb7fec727081d28946827e31f4330715dad283
Author: Stef Walter <stefw gnome org>
Date:   Fri Feb 23 16:37:01 2018 +0100

    ssh-agent: Use stock ssh-agent
    
    This patch removes our own implementation of ssh-agent and switches to
    using the ssh-agent program provided by OpenSSH.  We can't simply drop
    the ssh-agent functionality from gnome-keyring, as it enables the
    following:
    
     * Automatic loading and unlocking of keys
     * Prompting in the UI
    
    Instead we wrap the ssh-agent program as a subprocess and augment
    the protocol as we need.
    
    Signed-off-by: Stef Walter <stefw gnome org>
    Signed-off-by: Daiki Ueno <dueno src gnome org>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775981

 configure.ac                                  |    7 +
 daemon/Makefile.am                            |    2 +-
 daemon/gkd-glue.c                             |   38 +-
 daemon/gkd-pkcs11.c                           |   49 +-
 daemon/gkd-pkcs11.h                           |    2 -
 daemon/ssh-agent/Makefile.am                  |   76 +-
 daemon/ssh-agent/gkd-ssh-agent-ops.c          | 1492 -------------------------
 daemon/ssh-agent/gkd-ssh-agent-preload.c      |  279 +++++
 daemon/ssh-agent/gkd-ssh-agent-preload.h      |   50 +
 daemon/ssh-agent/gkd-ssh-agent-private.h      |  152 ---
 daemon/ssh-agent/gkd-ssh-agent-process.c      |  291 +++++
 daemon/ssh-agent/gkd-ssh-agent-process.h      |   45 +
 daemon/ssh-agent/gkd-ssh-agent-proto.c        |  914 ---------------
 daemon/ssh-agent/gkd-ssh-agent-service.c      |  661 +++++++++++
 daemon/ssh-agent/gkd-ssh-agent-service.h      |   54 +
 daemon/ssh-agent/gkd-ssh-agent-standalone.c   |  126 ---
 daemon/ssh-agent/gkd-ssh-agent-util.c         |  163 +++
 daemon/ssh-agent/gkd-ssh-agent-util.h         |   43 +
 daemon/ssh-agent/gkd-ssh-agent.c              |  451 --------
 daemon/ssh-agent/gkd-ssh-agent.h              |   40 -
 daemon/ssh-agent/test-common.c                |  347 ++++++
 daemon/ssh-agent/test-common.h                |   94 ++
 daemon/ssh-agent/test-communication.c         |  449 --------
 daemon/ssh-agent/test-gkd-ssh-agent-preload.c |  166 +++
 daemon/ssh-agent/test-gkd-ssh-agent-process.c |  217 ++++
 daemon/ssh-agent/test-gkd-ssh-agent-service.c |  617 ++++++++++
 daemon/ssh-agent/test-gkd-ssh-agent-util.c    |   84 ++
 daemon/ssh-agent/test-keytypes.c              |  197 ----
 28 files changed, 3182 insertions(+), 3924 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 34a50a2..e74decc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -348,6 +348,13 @@ AC_ARG_ENABLE([ssh-agent],
                [Don't include SSH agent in gnome-keyring]))
 
 if test "$enable_ssh_agent" != "no"; then
+       AC_PATH_PROG([SSH_AGENT], [ssh-agent], [no])
+       AC_PATH_PROG([SSH_ADD], [ssh-add], [no])
+       if test "$SSH_AGENT" = "no" -o "$SSH_ADD" = "no"; then
+               AC_MSG_ERROR([the ssh-agent and ssh-add commands were not found])
+       fi
+       AC_DEFINE_UNQUOTED(SSH_AGENT, "$SSH_AGENT", [The path to ssh-agent])
+       AC_DEFINE_UNQUOTED(SSH_ADD, "$SSH_ADD", [The path to ssh-add])
        AC_DEFINE(WITH_SSH, 1, [Whether to build SSH agent or not])
        ssh_status="yes"
 else
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 3611d42..48c1652 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -16,9 +16,9 @@ gnome_keyring_daemon_SOURCES = \
        $(NULL)
 gnome_keyring_daemon_LDADD = \
        libgkd-dbus.la \
-       libgkd-login.la \
        libgkd-control.la \
        libgkd-ssh-agent.la \
+       libgkd-login.la \
        libgkm-wrap-layer.la \
        libgkm-rpc-layer.la \
        libgkm-secret-store.la \
diff --git a/daemon/gkd-glue.c b/daemon/gkd-glue.c
index 329a37e..44ee213 100644
--- a/daemon/gkd-glue.c
+++ b/daemon/gkd-glue.c
@@ -23,46 +23,44 @@
 #include "gkd-glue.h"
 #include "gkd-util.h"
 
-#include "ssh-agent/gkd-ssh-agent.h"
+#include "ssh-agent/gkd-ssh-agent-service.h"
+#include "ssh-agent/gkd-ssh-agent-interaction.h"
 
 #include "egg/egg-cleanup.h"
 
 static void
-pkcs11_ssh_cleanup (gpointer unused)
+pkcs11_ssh_cleanup (gpointer data)
 {
-       gkd_ssh_agent_shutdown ();
-}
-
-static gboolean
-accept_ssh_client (GIOChannel *channel, GIOCondition cond, gpointer unused)
-{
-       if (cond == G_IO_IN)
-               gkd_ssh_agent_accept ();
-       return TRUE;
+       GkdSshAgentService *service = GKD_SSH_AGENT_SERVICE (data);
+       gkd_ssh_agent_service_stop (service);
+       g_object_unref (service);
 }
 
 gboolean
 gkd_daemon_startup_ssh (void)
 {
-       GIOChannel *channel;
        const gchar *base_dir;
-       int sock;
+       GTlsInteraction *interaction;
+       GkdSshAgentPreload *preload;
+       GkdSshAgentService *service;
 
        base_dir = gkd_util_get_master_directory ();
        g_return_val_if_fail (base_dir, FALSE);
 
-       sock = gkd_ssh_agent_startup (base_dir);
-       if (sock == -1)
-               return FALSE;
+       interaction = gkd_ssh_agent_interaction_new (NULL);
+       preload = gkd_ssh_agent_preload_new ("~/.ssh");
+
+       service = gkd_ssh_agent_service_new (base_dir, interaction, preload);
+       g_object_unref (interaction);
+       g_object_unref (preload);
 
-       channel = g_io_channel_unix_new (sock);
-       g_io_add_watch (channel, G_IO_IN | G_IO_HUP, accept_ssh_client, NULL);
-       g_io_channel_unref (channel);
+       if (!gkd_ssh_agent_service_start (service))
+               return FALSE;
 
        /* ssh-agent sets the environment variable */
        gkd_util_push_environment ("SSH_AUTH_SOCK", g_getenv ("SSH_AUTH_SOCK"));
 
-       egg_cleanup_register (pkcs11_ssh_cleanup, NULL);
+       egg_cleanup_register (pkcs11_ssh_cleanup, service);
 
        return TRUE;
 }
diff --git a/daemon/gkd-pkcs11.c b/daemon/gkd-pkcs11.c
index 71fdfe2..4d8ecc0 100644
--- a/daemon/gkd-pkcs11.c
+++ b/daemon/gkd-pkcs11.c
@@ -32,7 +32,7 @@
 #include "pkcs11/gnome2-store/gkm-gnome2-store.h"
 #include "pkcs11/xdg-store/gkm-xdg-store.h"
 
-#include "ssh-agent/gkd-ssh-agent.h"
+#include "ssh-agent/gkd-ssh-agent-service.h"
 
 #include <string.h>
 
@@ -49,7 +49,6 @@ pkcs11_daemon_cleanup (gpointer unused)
 
        g_assert (pkcs11_roof);
 
-       gkd_ssh_agent_uninitialize ();
        gkm_rpc_layer_uninitialize ();
        rv = (pkcs11_roof->C_Finalize) (NULL);
 
@@ -67,7 +66,6 @@ gkd_pkcs11_initialize (void)
        CK_FUNCTION_LIST_PTR gnome2_store;
        CK_FUNCTION_LIST_PTR xdg_store;
        CK_C_INITIALIZE_ARGS init_args;
-       gboolean ret;
        CK_RV rv;
 
        /* Secrets */
@@ -113,10 +111,7 @@ gkd_pkcs11_initialize (void)
 
        egg_cleanup_register (pkcs11_daemon_cleanup, NULL);
 
-       ret = gkd_ssh_agent_initialize (pkcs11_roof) &&
-             gkm_rpc_layer_initialize (pkcs11_roof);
-
-       return ret;
+       return gkm_rpc_layer_initialize (pkcs11_roof);
 }
 
 static void
@@ -157,46 +152,6 @@ gkd_pkcs11_startup_pkcs11 (void)
        return TRUE;
 }
 
-static void
-pkcs11_ssh_cleanup (gpointer unused)
-{
-       gkd_ssh_agent_shutdown ();
-}
-
-static gboolean
-accept_ssh_client (GIOChannel *channel, GIOCondition cond, gpointer unused)
-{
-       if (cond == G_IO_IN)
-               gkd_ssh_agent_accept ();
-       return TRUE;
-}
-
-gboolean
-gkd_pkcs11_startup_ssh (void)
-{
-       GIOChannel *channel;
-       const gchar *base_dir;
-       int sock;
-
-       base_dir = gkd_util_get_master_directory ();
-       g_return_val_if_fail (base_dir, FALSE);
-
-       sock = gkd_ssh_agent_startup (base_dir);
-       if (sock == -1)
-               return FALSE;
-
-       channel = g_io_channel_unix_new (sock);
-       g_io_add_watch (channel, G_IO_IN | G_IO_HUP, accept_ssh_client, NULL);
-       g_io_channel_unref (channel);
-
-       /* gkm-ssh-agent sets the environment variable */
-       gkd_util_push_environment ("SSH_AUTH_SOCK", g_getenv ("SSH_AUTH_SOCK"));
-
-       egg_cleanup_register (pkcs11_ssh_cleanup, NULL);
-
-       return TRUE;
-}
-
 CK_FUNCTION_LIST_PTR
 gkd_pkcs11_get_functions (void)
 {
diff --git a/daemon/gkd-pkcs11.h b/daemon/gkd-pkcs11.h
index 1da7b09..38e3f15 100644
--- a/daemon/gkd-pkcs11.h
+++ b/daemon/gkd-pkcs11.h
@@ -29,8 +29,6 @@ gboolean               gkd_pkcs11_initialize           (void);
 
 gboolean               gkd_pkcs11_startup_pkcs11       (void);
 
-gboolean               gkd_pkcs11_startup_ssh          (void);
-
 CK_FUNCTION_LIST_PTR   gkd_pkcs11_get_functions        (void);
 
 CK_FUNCTION_LIST_PTR   gkd_pkcs11_get_base_functions   (void);
diff --git a/daemon/ssh-agent/Makefile.am b/daemon/ssh-agent/Makefile.am
index 8ed821b..ff7793a 100644
--- a/daemon/ssh-agent/Makefile.am
+++ b/daemon/ssh-agent/Makefile.am
@@ -6,63 +6,73 @@ noinst_LTLIBRARIES += \
        libgkd-ssh-agent.la
 
 libgkd_ssh_agent_la_SOURCES = \
-       daemon/ssh-agent/gkd-ssh-agent.c \
-       daemon/ssh-agent/gkd-ssh-agent.h \
        daemon/ssh-agent/gkd-ssh-agent-interaction.c \
        daemon/ssh-agent/gkd-ssh-agent-interaction.h \
+       daemon/ssh-agent/gkd-ssh-agent-process.h \
+       daemon/ssh-agent/gkd-ssh-agent-process.c \
+       daemon/ssh-agent/gkd-ssh-agent-preload.h \
+       daemon/ssh-agent/gkd-ssh-agent-preload.c \
        daemon/ssh-agent/gkd-ssh-agent-private.h \
-       daemon/ssh-agent/gkd-ssh-agent-ops.c \
-       daemon/ssh-agent/gkd-ssh-agent-proto.c
+       daemon/ssh-agent/gkd-ssh-agent-service.c \
+       daemon/ssh-agent/gkd-ssh-agent-service.h \
+       daemon/ssh-agent/gkd-ssh-agent-util.c \
+       daemon/ssh-agent/gkd-ssh-agent-util.h \
+       $(NULL)
 libgkd_ssh_agent_la_CFLAGS = \
        $(DAEMON_CFLAGS)
 
-# ------------------------------------------------------------------------------
-# Standalone binary
+# Tests
+
+noinst_LTLIBRARIES += \
+       libgkd-ssh-agent-test.la
 
-noinst_PROGRAMS += \
-       gkd-ssh-agent-standalone
+libgkd_ssh_agent_test_la_SOURCES = \
+       daemon/ssh-agent/test-common.c \
+       daemon/ssh-agent/test-common.h
 
-gkd_ssh_agent_standalone_SOURCES = \
-       daemon/ssh-agent/gkd-ssh-agent-standalone.c
-gkd_ssh_agent_standalone_CFLAGS = \
+libgkd_ssh_agent_test_la_CFLAGS = \
        $(DAEMON_CFLAGS)
-gkd_ssh_agent_standalone_LDADD = \
-       libgkd-ssh-agent.la \
+
+libgkd_ssh_agent_test_la_LIBADD = \
        libegg-buffer.la \
-       libegg-secure.la \
-       libgkm.la \
+       libgkm-wrap-layer.la \
        $(DAEMON_LIBS)
 
-# ------------------------------------------------------------------------------
-# Tests
-
 ssh_agent_CFLAGS = \
-       $(GCK_CFLAGS)
+       $(DAEMON_CFLAGS)
 
 ssh_agent_LIBS = \
        libgkd-ssh-agent.la \
-       libgkm.la \
+       libgkd-ssh-agent-test.la \
        libegg.la \
-       $(GLIB_LIBS) \
-       $(GCK_LIBS)
-       $(GTHREAD_LIBS)
+       $(DAEMON_LIBS)
 
 ssh_agent_TESTS = \
-       test-communication \
-       test-keytypes \
-       test-gkd-ssh-agent-interaction
+       test-gkd-ssh-agent-interaction \
+       test-gkd-ssh-agent-preload \
+       test-gkd-ssh-agent-process \
+       test-gkd-ssh-agent-service \
+       test-gkd-ssh-agent-util
+
+test_gkd_ssh_agent_preload_SOURCES = daemon/ssh-agent/test-gkd-ssh-agent-preload.c
+test_gkd_ssh_agent_preload_CFLAGS = $(ssh_agent_CFLAGS)
+test_gkd_ssh_agent_preload_LDADD = $(ssh_agent_LIBS)
+
+test_gkd_ssh_agent_process_SOURCES = daemon/ssh-agent/test-gkd-ssh-agent-process.c
+test_gkd_ssh_agent_process_CFLAGS = $(ssh_agent_CFLAGS)
+test_gkd_ssh_agent_process_LDADD = $(ssh_agent_LIBS)
 
-test_keytypes_SOURCES = daemon/ssh-agent/test-keytypes.c
-test_keytypes_CFLAGS = $(ssh_agent_CFLAGS)
-test_keytypes_LDADD = $(ssh_agent_LIBS)
+test_gkd_ssh_agent_service_SOURCES = daemon/ssh-agent/test-gkd-ssh-agent-service.c
+test_gkd_ssh_agent_service_CFLAGS = $(ssh_agent_CFLAGS)
+test_gkd_ssh_agent_service_LDADD = $(ssh_agent_LIBS) libgkd-login.la libgkm-wrap-layer.la
 
-test_communication_SOURCES = daemon/ssh-agent/test-communication.c
-test_communication_CFLAGS = $(ssh_agent_CFLAGS)
-test_communication_LDADD = $(ssh_agent_LIBS)
+test_gkd_ssh_agent_util_SOURCES = daemon/ssh-agent/test-gkd-ssh-agent-util.c
+test_gkd_ssh_agent_util_CFLAGS = $(ssh_agent_CFLAGS)
+test_gkd_ssh_agent_util_LDADD = $(ssh_agent_LIBS)
 
 test_gkd_ssh_agent_interaction_SOURCES = daemon/ssh-agent/test-gkd-ssh-agent-interaction.c
 test_gkd_ssh_agent_interaction_CFLAGS = $(ssh_agent_CFLAGS)
-test_gkd_ssh_agent_interaction_LDADD = $(ssh_agent_LIBS) libgkd-login.la libgkm-wrap-layer.la
+test_gkd_ssh_agent_interaction_LDADD = $(ssh_agent_LIBS) libgkd-login.la
 
 check_PROGRAMS += $(ssh_agent_TESTS)
 TESTS += $(ssh_agent_TESTS)
diff --git a/daemon/ssh-agent/gkd-ssh-agent-preload.c b/daemon/ssh-agent/gkd-ssh-agent-preload.c
new file mode 100644
index 0000000..acc8cd8
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-preload.c
@@ -0,0 +1,279 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2014 Stef Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#include "config.h"
+
+#include "gkd-ssh-agent-preload.h"
+#include "gkd-ssh-agent-util.h"
+
+#include "egg/egg-file-tracker.h"
+#include <string.h>
+
+enum {
+       PROP_0,
+       PROP_PATH
+};
+
+struct _GkdSshAgentPreload
+{
+       GObject object;
+
+       gchar *path;
+       GHashTable *keys_by_public_filename;
+       GHashTable *keys_by_public_key;
+       EggFileTracker *file_tracker;
+       GMutex lock;
+};
+
+G_DEFINE_TYPE (GkdSshAgentPreload, gkd_ssh_agent_preload, G_TYPE_OBJECT);
+
+void
+gkd_ssh_agent_key_info_free (gpointer boxed)
+{
+       GkdSshAgentKeyInfo *info = boxed;
+       if (!info)
+               return;
+       g_bytes_unref (info->public_key);
+       g_free (info->comment);
+       g_free (info->filename);
+       g_free (info);
+}
+
+gpointer
+gkd_ssh_agent_key_info_copy (gpointer boxed)
+{
+       GkdSshAgentKeyInfo *info = boxed;
+       GkdSshAgentKeyInfo *copy = g_new0 (GkdSshAgentKeyInfo, 1);
+       copy->public_key = g_bytes_ref (info->public_key);
+       copy->comment = g_strdup (info->comment);
+       copy->filename = g_strdup (info->filename);
+       return copy;
+}
+
+static void file_load_inlock   (EggFileTracker *tracker,
+                                const gchar *path,
+                                gpointer user_data);
+static void file_remove_inlock (EggFileTracker *tracker,
+                                const gchar *path,
+                                gpointer user_data);
+
+static void
+gkd_ssh_agent_preload_init (GkdSshAgentPreload *self)
+{
+       self->keys_by_public_filename = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+       self->keys_by_public_key = g_hash_table_new_full (g_bytes_hash, g_bytes_equal, NULL, 
gkd_ssh_agent_key_info_free);
+}
+
+static void
+gkd_ssh_agent_preload_constructed (GObject *object)
+{
+       GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (object);
+
+       self->file_tracker = egg_file_tracker_new (self->path, "*.pub", NULL);
+       g_signal_connect (self->file_tracker, "file-added", G_CALLBACK (file_load_inlock), self);
+       g_signal_connect (self->file_tracker, "file-removed", G_CALLBACK (file_remove_inlock), self);
+       g_signal_connect (self->file_tracker, "file-changed", G_CALLBACK (file_load_inlock), self);
+
+       G_OBJECT_CLASS (gkd_ssh_agent_preload_parent_class)->constructed (object);
+}
+
+static void
+gkd_ssh_agent_preload_set_property (GObject *object,
+                                    guint prop_id,
+                                    const GValue *value,
+                                    GParamSpec *pspec)
+{
+       GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (object);
+
+       switch (prop_id) {
+       case PROP_PATH:
+               self->path = g_value_dup_string (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gkd_ssh_agent_preload_finalize (GObject *object)
+{
+       GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (object);
+
+       g_free (self->path);
+       g_clear_pointer (&self->keys_by_public_key, (GDestroyNotify) g_hash_table_unref);
+       g_clear_pointer (&self->keys_by_public_filename, (GDestroyNotify) g_hash_table_unref);
+       g_clear_object (&self->file_tracker);
+
+       g_mutex_clear (&self->lock);
+
+       G_OBJECT_CLASS (gkd_ssh_agent_preload_parent_class)->finalize (object);
+}
+
+static void
+gkd_ssh_agent_preload_class_init (GkdSshAgentPreloadClass *klass)
+{
+       GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+       gobject_class->constructed = gkd_ssh_agent_preload_constructed;
+       gobject_class->set_property = gkd_ssh_agent_preload_set_property;
+       gobject_class->finalize = gkd_ssh_agent_preload_finalize;
+       g_object_class_install_property (gobject_class, PROP_PATH,
+                g_param_spec_string ("path", "Path", "Path",
+                                     "",
+                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+}
+
+static gchar *
+private_path_for_public (const gchar *public_path)
+{
+       if (g_str_has_suffix (public_path, ".pub"))
+               return g_strndup (public_path, strlen (public_path) - 4);
+
+       return NULL;
+}
+
+static GBytes *
+file_get_contents (const gchar *path,
+                   gboolean must_be_present)
+{
+       GError *error = NULL;
+       gchar *contents;
+       gsize length;
+
+       if (!g_file_get_contents (path, &contents, &length, &error)) {
+               if (must_be_present || error->code != G_FILE_ERROR_NOENT)
+                       g_message ("couldn't read file: %s: %s", path, error->message);
+               g_error_free (error);
+               return NULL;
+       }
+
+       return g_bytes_new_take (contents, length);
+}
+
+static void
+file_remove_inlock (EggFileTracker *tracker,
+                    const gchar *path,
+                    gpointer user_data)
+{
+       GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (user_data);
+       GkdSshAgentKeyInfo *info;
+
+       info = g_hash_table_lookup (self->keys_by_public_filename, path);
+       if (info) {
+               g_hash_table_remove (self->keys_by_public_filename, path);
+               g_hash_table_remove (self->keys_by_public_key, info->public_key);
+       }
+}
+
+static void
+file_load_inlock (EggFileTracker *tracker,
+                  const gchar *path,
+                  gpointer user_data)
+{
+       GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (user_data);
+       gchar *private_path;
+       GBytes *private_bytes;
+       GBytes *public_bytes;
+       GBytes *public_key;
+       GkdSshAgentKeyInfo *info;
+       gchar *comment;
+
+       file_remove_inlock (tracker, path, user_data);
+
+       private_path = private_path_for_public (path);
+
+       private_bytes = file_get_contents (private_path, FALSE);
+       if (!private_bytes) {
+               g_debug ("no private key present for public key: %s", path);
+               g_free (private_path);
+               return;
+       }
+
+       public_bytes = file_get_contents (path, TRUE);
+       if (public_bytes) {
+               public_key = _gkd_ssh_agent_parse_public_key (public_bytes, &comment);
+               if (public_key) {
+                       info = g_new0 (GkdSshAgentKeyInfo, 1);
+                       info->filename = private_path;
+                       private_path = NULL;
+                       info->public_key = public_key;
+                       info->comment = comment;
+                       g_hash_table_replace (self->keys_by_public_filename, g_strdup (path), info);
+                       g_hash_table_replace (self->keys_by_public_key, info->public_key, info);
+               } else {
+                       g_message ("failed to parse ssh public key: %s", path);
+               }
+
+               g_bytes_unref (public_bytes);
+       }
+
+       g_bytes_unref (private_bytes);
+       g_free (private_path);
+}
+
+GkdSshAgentPreload *
+gkd_ssh_agent_preload_new (const gchar *path)
+{
+       g_return_val_if_fail (path, NULL);
+
+       return g_object_new (GKD_TYPE_SSH_AGENT_PRELOAD, "path", path, NULL);
+}
+
+GList *
+gkd_ssh_agent_preload_get_keys (GkdSshAgentPreload *self)
+{
+       GList *keys = NULL;
+       GHashTableIter iter;
+       GkdSshAgentKeyInfo *info;
+
+       g_mutex_lock (&self->lock);
+
+       egg_file_tracker_refresh (self->file_tracker, FALSE);
+
+       g_hash_table_iter_init (&iter, self->keys_by_public_key);
+       while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
+               keys = g_list_prepend (keys, gkd_ssh_agent_key_info_copy (info));
+
+       g_mutex_unlock (&self->lock);
+
+       return keys;
+}
+
+GkdSshAgentKeyInfo *
+gkd_ssh_agent_preload_lookup_by_public_key (GkdSshAgentPreload *self,
+                                           GBytes *public_key)
+{
+       GkdSshAgentKeyInfo *info;
+
+       g_mutex_lock (&self->lock);
+
+       egg_file_tracker_refresh (self->file_tracker, FALSE);
+
+       info = g_hash_table_lookup (self->keys_by_public_key, public_key);
+       if (info)
+               info = gkd_ssh_agent_key_info_copy (info);
+
+       g_mutex_unlock (&self->lock);
+
+       return info;
+}
diff --git a/daemon/ssh-agent/gkd-ssh-agent-preload.h b/daemon/ssh-agent/gkd-ssh-agent-preload.h
new file mode 100644
index 0000000..c626c50
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-preload.h
@@ -0,0 +1,50 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2014 Stef Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#ifndef __GKD_SSH_AGENT_PRELOAD_H__
+#define __GKD_SSH_AGENT_PRELOAD_H__
+
+#include <glib-object.h>
+
+typedef struct {
+       gchar *filename;
+       GBytes *public_key;
+       gchar *comment;
+} GkdSshAgentKeyInfo;
+
+void                gkd_ssh_agent_key_info_free    (gpointer boxed);
+gpointer            gkd_ssh_agent_key_info_copy    (gpointer boxed);
+
+#define GKD_TYPE_SSH_AGENT_PRELOAD gkd_ssh_agent_preload_get_type ()
+G_DECLARE_FINAL_TYPE (GkdSshAgentPreload, gkd_ssh_agent_preload, GKD, SSH_AGENT_PRELOAD, GObject)
+
+GkdSshAgentPreload *gkd_ssh_agent_preload_new      (const gchar *path);
+
+GList              *gkd_ssh_agent_preload_get_keys (GkdSshAgentPreload *self);
+
+GkdSshAgentKeyInfo *gkd_ssh_agent_preload_lookup_by_public_key
+                                                   (GkdSshAgentPreload *self,
+                                                    GBytes *public_key);
+
+#endif /* __GKD_SSH_AGENT_PRELOAD_H__ */
+
diff --git a/daemon/ssh-agent/gkd-ssh-agent-private.h b/daemon/ssh-agent/gkd-ssh-agent-private.h
index a6c35a4..97c5a09 100644
--- a/daemon/ssh-agent/gkd-ssh-agent-private.h
+++ b/daemon/ssh-agent/gkd-ssh-agent-private.h
@@ -23,21 +23,6 @@
 #ifndef GKDSSHPRIVATE_H_
 #define GKDSSHPRIVATE_H_
 
-#include "egg/egg-buffer.h"
-
-#include "pkcs11/pkcs11.h"
-
-#include <gck/gck.h>
-
-#include <glib.h>
-
-typedef struct _GkdSshAgentCall {
-       int sock;
-       GList *modules;
-       EggBuffer *req;
-       EggBuffer *resp;
-} GkdSshAgentCall;
-
 /* -----------------------------------------------------------------------------
  * SSH OPERATIONS and CONSTANTS
  */
@@ -82,141 +67,4 @@ typedef struct _GkdSshAgentCall {
 #define        GKD_SSH_FLAG_RSA_SHA2_256                       0x02
 #define        GKD_SSH_FLAG_RSA_SHA2_512                       0x04
 
-/* -----------------------------------------------------------------------------
- * gkd-ssh-agent-ops.c
- */
-
-typedef gboolean (*GkdSshAgentOperation) (GkdSshAgentCall *call);
-extern const GkdSshAgentOperation gkd_ssh_agent_operations[GKD_SSH_OP_MAX];
-
-/* -----------------------------------------------------------------------------
- * gkd-ssh-agent.c
- */
-
-gboolean              gkd_ssh_agent_initialize_with_module          (GckModule *module);
-
-GckSession*           gkd_ssh_agent_checkout_main_session           (void);
-
-void                  gkd_ssh_agent_checkin_main_session            (GckSession* session);
-
-/* -----------------------------------------------------------------------------
- * gkd-ssh-agent-proto.c
- */
-
-gulong                gkd_ssh_agent_proto_keytype_to_algo           (const gchar *salgo);
-
-const gchar*          gkd_ssh_agent_proto_rsa_algo_to_keytype       (GChecksumType halgo);
-
-const gchar*          gkd_ssh_agent_proto_dsa_algo_to_keytype       (void);
-
-const gchar*          gkd_ssh_agent_proto_ecc_algo_to_keytype       (GQuark oid);
-
-GQuark                gkd_ssh_agent_proto_curve_to_oid              (const gchar *salgo);
-
-const gchar*          gkd_ssh_agent_proto_oid_to_curve              (GQuark oid);
-
-gint                  gkd_ssh_agent_proto_curve_oid_to_hash_algo    (GQuark oid);
-
-GQuark                gkd_ssh_agent_proto_find_curve_oid            (GckAttributes *attrs);
-
-gboolean              gkd_ssh_agent_proto_read_mpi                  (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs,
-                                                                     CK_ATTRIBUTE_TYPE type);
-
-gboolean              gkd_ssh_agent_proto_read_mpi_v1               (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs,
-                                                                     CK_ATTRIBUTE_TYPE type);
-
-const guchar*         gkd_ssh_agent_proto_read_challenge_v1         (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     gsize *n_challenge);
-
-gboolean              gkd_ssh_agent_proto_read_string_to_der        (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs,
-                                                                     CK_ATTRIBUTE_TYPE type);
-
-gboolean              gkd_ssh_agent_proto_read_ecdsa_curve          (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs);
-
-gboolean              gkd_ssh_agent_proto_write_mpi                 (EggBuffer *resp,
-                                                                     const GckAttribute *attr);
-
-gboolean              gkd_ssh_agent_proto_write_mpi_v1              (EggBuffer *resp,
-                                                                     const GckAttribute *attr);
-
-gboolean              gkd_ssh_agent_proto_write_string              (EggBuffer *resp,
-                                                                     const GckAttribute *attr);
-
-gboolean              gkd_ssh_agent_proto_read_public               (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs,
-                                                                     gulong *algo);
-
-gboolean              gkd_ssh_agent_proto_read_public_rsa           (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs);
-
-gboolean              gkd_ssh_agent_proto_read_public_dsa           (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs);
-
-gboolean              gkd_ssh_agent_proto_read_public_ecdsa         (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs);
-
-gboolean              gkd_ssh_agent_proto_read_public_v1            (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *attrs);
-
-gboolean              gkd_ssh_agent_proto_read_pair_rsa             (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *priv,
-                                                                     GckBuilder *pub);
-
-gboolean              gkd_ssh_agent_proto_read_pair_dsa             (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *priv,
-                                                                     GckBuilder *pub);
-
-gboolean              gkd_ssh_agent_proto_read_pair_ecdsa           (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *priv,
-                                                                     GckBuilder *pub);
-
-gboolean              gkd_ssh_agent_proto_read_pair_v1              (EggBuffer *req,
-                                                                     gsize *offset,
-                                                                     GckBuilder *priv,
-                                                                     GckBuilder *pub);
-
-gboolean              gkd_ssh_agent_proto_write_public              (EggBuffer *resp,
-                                                                     GckAttributes *attrs);
-
-gboolean              gkd_ssh_agent_proto_write_public_rsa          (EggBuffer *resp,
-                                                                     GckAttributes *attrs);
-
-gboolean              gkd_ssh_agent_proto_write_public_dsa          (EggBuffer *resp,
-                                                                     GckAttributes *attrs);
-
-gboolean              gkd_ssh_agent_proto_write_public_ecdsa        (EggBuffer *resp,
-                                                                     GckAttributes *attrs);
-
-gboolean              gkd_ssh_agent_proto_write_public_v1           (EggBuffer *resp,
-                                                                     GckAttributes *attrs);
-
-gboolean              gkd_ssh_agent_proto_write_signature_rsa       (EggBuffer *resp,
-                                                                     CK_BYTE_PTR signature,
-                                                                     CK_ULONG n_signature);
-
-gboolean              gkd_ssh_agent_proto_write_signature_dsa       (EggBuffer *resp,
-                                                                     CK_BYTE_PTR signature,
-                                                                     CK_ULONG n_signature);
-
-gboolean              gkd_ssh_agent_proto_write_signature_ecdsa     (EggBuffer *resp,
-                                                                     CK_BYTE_PTR signature,
-                                                                     CK_ULONG n_signature);
-
 #endif /*GKDSSHPRIVATE_H_*/
diff --git a/daemon/ssh-agent/gkd-ssh-agent-process.c b/daemon/ssh-agent/gkd-ssh-agent-process.c
new file mode 100644
index 0000000..b6585d7
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-process.c
@@ -0,0 +1,291 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2014 Stef Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#include "config.h"
+
+#include "gkd-ssh-agent-process.h"
+#include "gkd-ssh-agent-private.h"
+#include "gkd-ssh-agent-util.h"
+
+#include <gio/gunixsocketaddress.h>
+#include <glib-unix.h>
+#include <glib/gstdio.h>
+
+enum {
+       PROP_0,
+       PROP_PATH
+};
+
+enum {
+       CLOSED,
+       LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+struct _GkdSshAgentProcess
+{
+       GObject object;
+       gchar *path;
+       GSocketConnection *connection;
+       gint output;
+       GMutex lock;
+       GPid pid;
+       guint output_id;
+       guint child_id;
+       gboolean ready;
+};
+
+G_DEFINE_TYPE (GkdSshAgentProcess, gkd_ssh_agent_process, G_TYPE_OBJECT);
+
+static void
+gkd_ssh_agent_process_init (GkdSshAgentProcess *self)
+{
+       self->output = -1;
+       g_mutex_init (&self->lock);
+}
+
+static void
+gkd_ssh_agent_process_finalize (GObject *object)
+{
+       GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (object);
+
+       g_clear_object (&self->connection);
+       if (self->output != -1)
+               close (self->output);
+       if (self->output_id)
+               g_source_remove (self->output_id);
+       if (self->child_id)
+               g_source_remove (self->child_id);
+       if (self->pid)
+               kill (self->pid, SIGTERM);
+       g_unlink (self->path);
+       g_free (self->path);
+       g_mutex_clear (&self->lock);
+
+       G_OBJECT_CLASS (gkd_ssh_agent_process_parent_class)->finalize (object);
+}
+
+static void
+gkd_ssh_agent_process_set_property (GObject *object,
+                                    guint prop_id,
+                                    const GValue *value,
+                                    GParamSpec *pspec)
+{
+       GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (object);
+
+       switch (prop_id) {
+       case PROP_PATH:
+               self->path = g_value_dup_string (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gkd_ssh_agent_process_class_init (GkdSshAgentProcessClass *klass)
+{
+       GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+       gobject_class->finalize = gkd_ssh_agent_process_finalize;
+       gobject_class->set_property = gkd_ssh_agent_process_set_property;
+       g_object_class_install_property (gobject_class, PROP_PATH,
+                g_param_spec_string ("path", "Path", "Path",
+                                     "",
+                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+       signals[CLOSED] = g_signal_new_class_handler ("closed",
+                                                     G_TYPE_FROM_CLASS (klass),
+                                                     G_SIGNAL_RUN_LAST,
+                                                     NULL, NULL, NULL, NULL,
+                                                     G_TYPE_NONE, 0);
+}
+
+static void
+on_child_watch (GPid pid,
+                gint status,
+                gpointer user_data)
+{
+       GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (user_data);
+       GError *error = NULL;
+
+       if (pid != self->pid)
+               return;
+
+       g_mutex_lock (&self->lock);
+
+       self->pid = 0;
+       self->output_id = 0;
+       self->child_id = 0;
+
+       if (!g_spawn_check_exit_status (status, &error)) {
+               g_message ("ssh-agent: %s", error->message);
+               g_error_free (error);
+       }
+
+       g_spawn_close_pid (pid);
+
+       g_mutex_unlock (&self->lock);
+
+       g_signal_emit (self, signals[CLOSED], 0);
+}
+
+static gboolean
+on_output_watch (gint fd,
+                GIOCondition condition,
+                gpointer user_data)
+{
+       GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (user_data);
+       guint8 buf[1024];
+       gssize len;
+
+       if (condition & G_IO_IN) {
+               self->ready = TRUE;
+
+               len = read (fd, buf, sizeof (buf));
+               if (len < 0) {
+                       if (errno != EAGAIN && errno != EINTR)
+                               g_message ("couldn't read from ssh-agent stdout: %m");
+                       condition |= G_IO_ERR;
+               }
+       }
+
+       if (condition & G_IO_HUP || condition & G_IO_ERR)
+               return FALSE;
+
+       return TRUE;
+}
+
+static gboolean
+agent_start_inlock (GkdSshAgentProcess *self,
+                   GError **error)
+{
+       const gchar *argv[] = { SSH_AGENT, "-D", "-a", self->path, NULL };
+       GPid pid;
+
+       if (!g_spawn_async_with_pipes ("/", (gchar **)argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
+                                      NULL, NULL, &pid, NULL, &self->output, NULL, error))
+               return FALSE;
+
+       self->ready = FALSE;
+       self->output_id = g_unix_fd_add (self->output,
+                                        G_IO_IN | G_IO_HUP | G_IO_ERR,
+                                        on_output_watch, self);
+
+       self->pid = pid;
+       self->child_id = g_child_watch_add (self->pid, on_child_watch, self);
+
+       return TRUE;
+}
+
+static gboolean
+on_timeout (gpointer user_data)
+{
+       gboolean *timedout = user_data;
+       *timedout = TRUE;
+       return TRUE;
+}
+
+gboolean
+gkd_ssh_agent_process_connect (GkdSshAgentProcess *self,
+                              GCancellable *cancellable,
+                              GError **error)
+{
+       gboolean started = FALSE;
+       gboolean timedout = FALSE;
+       guint source;
+       GSocketClient *client;
+       GSocketAddress *address;
+       GSocketConnection *connection;
+
+       g_mutex_lock (&self->lock);
+
+       if (self->pid == 0) {
+               if (!agent_start_inlock (self, error)) {
+                       g_mutex_unlock (&self->lock);
+                       return FALSE;
+               }
+               started = TRUE;
+       }
+
+       if (started && !self->ready) {
+               source = g_timeout_add_seconds (5, on_timeout, &timedout);
+               while (!self->ready && !timedout)
+                       g_main_context_iteration (NULL, FALSE);
+               g_source_remove (source);
+       }
+
+       if (!self->ready) {
+               g_mutex_unlock (&self->lock);
+               g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                            "ssh-agent process is not ready");
+               return FALSE;
+       }
+
+       address = g_unix_socket_address_new (self->path);
+       client = g_socket_client_new ();
+
+       connection = g_socket_client_connect (client,
+                                             G_SOCKET_CONNECTABLE (address),
+                                             cancellable,
+                                             error);
+       g_object_unref (address);
+       g_object_unref (client);
+       if (!connection) {
+               g_mutex_unlock (&self->lock);
+               return FALSE;
+       }
+
+       g_clear_object (&self->connection);
+       self->connection = connection;
+
+       g_mutex_unlock (&self->lock);
+
+       return TRUE;
+}
+
+gboolean
+gkd_ssh_agent_process_call (GkdSshAgentProcess *self,
+                            EggBuffer*req,
+                            EggBuffer *resp,
+                           GCancellable *cancellable,
+                           GError **error)
+{
+       g_return_val_if_fail (self->connection != NULL, FALSE);
+       return _gkd_ssh_agent_write_packet (self->connection, req, cancellable, error) &&
+               _gkd_ssh_agent_read_packet (self->connection, resp, cancellable, error);
+}
+
+GkdSshAgentProcess *
+gkd_ssh_agent_process_new (const gchar *path)
+{
+       g_return_val_if_fail (path, NULL);
+
+       return g_object_new (GKD_TYPE_SSH_AGENT_PROCESS, "path", path, NULL);
+}
+
+GPid
+gkd_ssh_agent_process_get_pid (GkdSshAgentProcess *self)
+{
+       return self->pid;
+}
diff --git a/daemon/ssh-agent/gkd-ssh-agent-process.h b/daemon/ssh-agent/gkd-ssh-agent-process.h
new file mode 100644
index 0000000..350bc95
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-process.h
@@ -0,0 +1,45 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2014 Stef Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#ifndef __GKD_SSH_AGENT_PROCESS_H__
+#define __GKD_SSH_AGENT_PROCESS_H__
+
+#include <gio/gio.h>
+
+#include "egg/egg-buffer.h"
+
+#define GKD_TYPE_SSH_AGENT_PROCESS gkd_ssh_agent_process_get_type ()
+G_DECLARE_FINAL_TYPE(GkdSshAgentProcess, gkd_ssh_agent_process, GKD, SSH_AGENT_PROCESS, GObject)
+
+GkdSshAgentProcess *gkd_ssh_agent_process_new         (const gchar *path);
+gboolean            gkd_ssh_agent_process_connect     (GkdSshAgentProcess *self,
+                                                       GCancellable *cancellable,
+                                                       GError **error);
+gboolean            gkd_ssh_agent_process_call        (GkdSshAgentProcess *self,
+                                                       EggBuffer *req,
+                                                       EggBuffer *resp,
+                                                       GCancellable *cancellable,
+                                                       GError **error);
+GPid                gkd_ssh_agent_process_get_pid     (GkdSshAgentProcess *self);
+
+#endif /* __GKD_SSH_AGENT_PROCESS_H__ */
diff --git a/daemon/ssh-agent/gkd-ssh-agent-service.c b/daemon/ssh-agent/gkd-ssh-agent-service.c
new file mode 100644
index 0000000..8b9a8f1
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-service.c
@@ -0,0 +1,661 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2007 Stefan Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#include "config.h"
+
+#include <gio/gunixsocketaddress.h>
+#include <glib/gstdio.h>
+
+#include <gcr/gcr-base.h>
+
+#include "gkd-ssh-agent-service.h"
+#include "gkd-ssh-agent-preload.h"
+#include "gkd-ssh-agent-private.h"
+#include "gkd-ssh-agent-process.h"
+#include "gkd-ssh-agent-util.h"
+#include "daemon/login/gkd-login-interaction.h"
+
+#include "egg/egg-buffer.h"
+#include "egg/egg-error.h"
+#include "egg/egg-secure-memory.h"
+
+#include <glib/gi18n-lib.h>
+
+EGG_SECURE_DECLARE (ssh_agent);
+
+typedef gboolean (*GkdSshAgentOperation) (GkdSshAgentService *agent, EggBuffer *req, EggBuffer *resp, 
GCancellable *cancellable, GError **error);
+static const GkdSshAgentOperation operations[GKD_SSH_OP_MAX];
+
+enum {
+       PROP_0,
+       PROP_PATH,
+       PROP_INTERACTION,
+       PROP_PRELOAD
+};
+
+struct _GkdSshAgentService
+{
+       GObject object;
+       gchar *path;
+       GTlsInteraction *interaction;
+       GkdSshAgentPreload *preload;
+       GkdSshAgentProcess *process;
+       GSocketAddress *address;
+       GSocketListener *listener;
+       GHashTable *keys;
+       GMutex lock;
+       GCancellable *cancellable;
+};
+
+G_DEFINE_TYPE (GkdSshAgentService, gkd_ssh_agent_service, G_TYPE_OBJECT);
+
+static void
+gkd_ssh_agent_service_init (GkdSshAgentService *self)
+{
+       self->keys = g_hash_table_new_full (g_bytes_hash, g_bytes_equal,
+                                           (GDestroyNotify)g_bytes_unref, NULL);
+       g_mutex_init (&self->lock);
+}
+
+static void
+gkd_ssh_agent_service_constructed (GObject *object)
+{
+       GkdSshAgentService *self = GKD_SSH_AGENT_SERVICE (object);
+       gchar *path;
+
+       path = g_strdup_printf ("%s/.ssh", self->path);
+       self->process = gkd_ssh_agent_process_new (path);
+       g_free (path);
+
+       self->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
+       self->cancellable = g_cancellable_new ();
+
+       G_OBJECT_CLASS (gkd_ssh_agent_service_parent_class)->constructed (object);
+}
+
+static void
+gkd_ssh_agent_service_set_property (GObject *object,
+                            guint prop_id,
+                            const GValue *value,
+                            GParamSpec *pspec)
+{
+       GkdSshAgentService *self = GKD_SSH_AGENT_SERVICE (object);
+
+       switch (prop_id) {
+       case PROP_PATH:
+               self->path = g_value_dup_string (value);
+               break;
+       case PROP_INTERACTION:
+               self->interaction = g_value_dup_object (value);
+               break;
+       case PROP_PRELOAD:
+               self->preload = g_value_dup_object (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gkd_ssh_agent_service_finalize (GObject *object)
+{
+       GkdSshAgentService *self = GKD_SSH_AGENT_SERVICE (object);
+
+       g_free (self->path);
+       g_object_unref (self->interaction);
+       g_object_unref (self->preload);
+
+       g_object_unref (self->process);
+       g_object_unref (self->listener);
+       g_clear_object (&self->address);
+       g_mutex_clear (&self->lock);
+       g_hash_table_unref (self->keys);
+       g_object_unref (self->cancellable);
+
+       G_OBJECT_CLASS (gkd_ssh_agent_service_parent_class)->finalize (object);
+}
+
+static void
+gkd_ssh_agent_service_class_init (GkdSshAgentServiceClass *klass)
+{
+       GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+       gobject_class->constructed = gkd_ssh_agent_service_constructed;
+       gobject_class->set_property = gkd_ssh_agent_service_set_property;
+       gobject_class->finalize = gkd_ssh_agent_service_finalize;
+       g_object_class_install_property (gobject_class, PROP_PATH,
+                g_param_spec_string ("path", "Path", "Path",
+                                     "",
+                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+       g_object_class_install_property (gobject_class, PROP_INTERACTION,
+                g_param_spec_object ("interaction", "Interaction", "Interaction",
+                                     G_TYPE_TLS_INTERACTION,
+                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+       g_object_class_install_property (gobject_class, PROP_PRELOAD,
+                g_param_spec_object ("preload", "Preload", "Preload",
+                                     GKD_TYPE_SSH_AGENT_PRELOAD,
+                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+}
+
+static gboolean
+relay_request (GkdSshAgentService *self,
+              EggBuffer *req,
+              EggBuffer *resp,
+              GCancellable *cancellable,
+              GError **error)
+{
+       return gkd_ssh_agent_process_call (self->process, req, resp, cancellable, error);
+}
+
+static gboolean
+handle_request (GkdSshAgentService *self,
+               EggBuffer *req,
+               EggBuffer *resp,
+               GCancellable *cancellable,
+               GError **error)
+{
+       GkdSshAgentOperation func;
+       guchar op;
+
+       egg_buffer_reset (resp);
+       egg_buffer_add_uint32 (resp, 0);
+
+       /* Decode the operation; on failure, just pass through */
+       if (egg_buffer_get_byte (req, 4, NULL, &op) &&
+           op <= GKD_SSH_OP_MAX && operations[op] != NULL)
+               func = operations[op];
+       else
+               func = relay_request;
+
+       return func (self, req, resp, cancellable, error);
+}
+
+static void
+add_key (GkdSshAgentService *self,
+        GBytes *key)
+{
+       g_mutex_lock (&self->lock);
+       g_hash_table_add (self->keys, g_bytes_ref (key));
+       g_mutex_unlock (&self->lock);
+}
+
+static void
+remove_key (GkdSshAgentService *self,
+           GBytes *key)
+{
+       g_mutex_lock (&self->lock);
+       g_hash_table_remove (self->keys, key);
+       g_mutex_unlock (&self->lock);
+}
+
+static void
+clear_keys (GkdSshAgentService *self)
+{
+       g_mutex_lock (&self->lock);
+       g_hash_table_remove_all (self->keys);
+       g_mutex_unlock (&self->lock);
+}
+
+static void
+ensure_key (GkdSshAgentService *self,
+                         GBytes *key)
+{
+       GcrSshAskpass *askpass;
+       GError *error = NULL;
+       gint status;
+       GkdSshAgentKeyInfo *info;
+       gchar *unique;
+       const gchar *label;
+       GHashTable *fields;
+       GTlsInteraction *interaction;
+
+       gchar *argv[] = {
+               SSH_ADD,
+               NULL,
+               NULL
+       };
+
+       if (gkd_ssh_agent_service_lookup_key (self, key))
+               return;
+
+       info = gkd_ssh_agent_preload_lookup_by_public_key (self->preload, key);
+       if (!info)
+               return;
+
+       argv[1] = info->filename;
+
+       fields = g_hash_table_new (g_str_hash, g_str_equal);
+       g_hash_table_insert (fields, "xdg:schema", "org.freedesktop.Secret.Generic");
+       unique = g_strdup_printf ("ssh-store:%s", info->filename);
+       g_hash_table_insert (fields, "unique", unique);
+
+       label = info->comment[0] != '\0' ? info->comment : _("Unnamed");
+
+       interaction = gkd_login_interaction_new (self->interaction, NULL, label, fields);
+       askpass = gcr_ssh_askpass_new (interaction);
+       g_object_unref (interaction);
+
+       if (!g_spawn_sync (NULL, argv, NULL,
+                          G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
+                          gcr_ssh_askpass_child_setup, askpass,
+                          NULL, NULL, &status, &error)) {
+               g_warning ("couldn't run %s: %s", argv[0], error->message);
+               g_error_free (error);
+       } else if (!g_spawn_check_exit_status (status, &error)) {
+               g_message ("the %s command failed: %s", argv[0], error->message);
+               g_error_free (error);
+       } else {
+               add_key (self, key);
+       }
+
+       g_hash_table_unref (fields);
+       g_free (unique);
+       gkd_ssh_agent_key_info_free (info);
+       g_object_unref (askpass);
+}
+
+static gboolean
+on_run (GThreadedSocketService *service,
+       GSocketConnection *connection,
+       GObject *source_object,
+       gpointer user_data)
+{
+       GkdSshAgentService *self = g_object_ref (GKD_SSH_AGENT_SERVICE (user_data));
+       EggBuffer req;
+       EggBuffer resp;
+       GError *error;
+       gboolean ret;
+
+       egg_buffer_init_full (&req, 128, egg_secure_realloc);
+       egg_buffer_init_full (&resp, 128, (EggBufferAllocator)g_realloc);
+
+       error = NULL;
+       if (!gkd_ssh_agent_process_connect (self->process, self->cancellable, &error)) {
+               g_warning ("couldn't connect to ssh-agent: %s", error->message);
+               g_error_free (error);
+               goto out;
+       }
+
+       while (TRUE) {
+               /* Read in the request */
+               error = NULL;
+               if (!_gkd_ssh_agent_read_packet (connection, &req, self->cancellable, &error)) {
+                       if (error->code != G_IO_ERROR_CANCELLED)
+                               g_message ("couldn't read from client: %s", error->message);
+                       g_error_free (error);
+                       break;
+               }
+
+               /* Handle the request */
+               error = NULL;
+               while (!(ret = handle_request (self, &req, &resp, self->cancellable, &error))) {
+                       if (gkd_ssh_agent_process_get_pid (self->process) != 0) {
+                               if (error->code != G_IO_ERROR_CANCELLED)
+                                       g_message ("couldn't handle client request: %s", error->message);
+                               g_error_free (error);
+                               goto out;
+                       }
+
+                       /* Reconnect to the ssh-agent */
+                       g_clear_error (&error);
+                       if (!gkd_ssh_agent_process_connect (self->process, self->cancellable, &error)) {
+                               if (error->code != G_IO_ERROR_CANCELLED)
+                                       g_message ("couldn't connect to ssh-agent: %s", error->message);
+                               g_error_free (error);
+                               goto out;
+                       }
+               }
+
+               /* Write the reply back out */
+               error = NULL;
+               if (!_gkd_ssh_agent_write_packet (connection, &resp, self->cancellable, &error)) {
+                       if (error->code != G_IO_ERROR_CANCELLED)
+                               g_message ("couldn't write to client: %s", error->message);
+                       g_error_free (error);
+                       break;
+               }
+       }
+
+ out:
+       egg_buffer_uninit (&req);
+       egg_buffer_uninit (&resp);
+
+       g_object_unref (self);
+
+       return TRUE;
+}
+
+static void
+on_closed (GkdSshAgentProcess *process,
+          gpointer user_data)
+{
+       GkdSshAgentService *self = GKD_SSH_AGENT_SERVICE (user_data);
+       clear_keys (self);
+}
+
+gboolean
+gkd_ssh_agent_service_start (GkdSshAgentService *self)
+{
+       gchar *path;
+       GError *error;
+
+       path = g_strdup_printf ("%s/ssh", self->path);
+       g_unlink (path);
+       self->address = g_unix_socket_address_new (path);
+       g_free (path);
+
+       error = NULL;
+       if (!g_socket_listener_add_address (self->listener,
+                                           self->address,
+                                           G_SOCKET_TYPE_STREAM,
+                                           G_SOCKET_PROTOCOL_DEFAULT,
+                                           NULL,
+                                           NULL,
+                                           &error)) {
+               g_warning ("couldn't listen on %s: %s",
+                          g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (self->address)),
+                          error->message);
+               g_error_free (error);
+               return FALSE;
+       }
+
+       g_signal_connect (self->listener, "run", G_CALLBACK (on_run), self);
+       g_signal_connect (self->process, "closed", G_CALLBACK (on_closed), self);
+
+       g_setenv ("SSH_AUTH_SOCK", g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (self->address)), 
TRUE);
+
+       g_socket_service_start (G_SOCKET_SERVICE (self->listener));
+
+       return TRUE;
+}
+
+void
+gkd_ssh_agent_service_stop (GkdSshAgentService *self)
+{
+       if (self->address)
+               g_unlink (g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (self->address)));
+
+       g_cancellable_cancel (self->cancellable);
+       g_socket_service_stop (G_SOCKET_SERVICE (self->listener));
+}
+
+GkdSshAgentService *
+gkd_ssh_agent_service_new (const gchar *path,
+                  GTlsInteraction *interaction,
+                  GkdSshAgentPreload *preload)
+{
+       g_return_val_if_fail (path, NULL);
+       g_return_val_if_fail (interaction, NULL);
+       g_return_val_if_fail (preload, NULL);
+
+       return g_object_new (GKD_TYPE_SSH_AGENT_SERVICE,
+                            "path", path,
+                            "interaction", interaction,
+                            "preload", preload,
+                            NULL);
+}
+
+GkdSshAgentPreload *
+gkd_ssh_agent_service_get_preload (GkdSshAgentService *self)
+{
+       return self->preload;
+}
+
+GkdSshAgentProcess *
+gkd_ssh_agent_service_get_process (GkdSshAgentService *self)
+{
+       return self->process;
+}
+
+gboolean
+gkd_ssh_agent_service_lookup_key (GkdSshAgentService *self,
+                         GBytes *key)
+{
+       gboolean ret;
+       g_mutex_lock (&self->lock);
+       ret = g_hash_table_contains (self->keys, key);
+       g_mutex_unlock (&self->lock);
+       return ret;
+}
+
+/* ---------------------------------------------------------------------------- */
+
+static gboolean
+op_add_identity (GkdSshAgentService *self,
+                EggBuffer *req,
+                EggBuffer *resp,
+                GCancellable *cancellable,
+                GError **error)
+{
+       const guchar *blob;
+       gsize offset = 5;
+       gsize length;
+       GBytes *key = NULL;
+       gboolean ret;
+
+       /* If parsing the request fails, just pass through */
+       ret = egg_buffer_get_byte_array (req, offset, &offset, &blob, &length);
+       if (ret)
+               key = g_bytes_new (blob, length);
+       else
+               g_message ("got unparseable add identity request for ssh-agent");
+
+       ret = relay_request (self, req, resp, cancellable, error);
+       if (key) {
+               if (ret)
+                       add_key (self, key);
+               g_bytes_unref (key);
+       }
+
+       return ret;
+}
+
+static GHashTable *
+parse_identities_answer (EggBuffer *resp)
+{
+       GHashTable *answer;
+       const guchar *blob;
+       gchar *comment;
+       gsize length;
+       gsize offset = 4;
+       guint32 count;
+       guchar op;
+       guint32 i;
+
+       if (!egg_buffer_get_byte (resp, offset, &offset, &op) ||
+           op != GKD_SSH_RES_IDENTITIES_ANSWER ||
+           !egg_buffer_get_uint32 (resp, offset, &offset, &count)) {
+               g_message ("got unexpected response back from ssh-agent when requesting identities");
+               return NULL;
+       }
+
+       answer = g_hash_table_new_full (g_bytes_hash, g_bytes_equal, (GDestroyNotify)g_bytes_unref, g_free);
+
+       for (i = 0; i < count; i++) {
+               if (!egg_buffer_get_byte_array (resp, offset, &offset, &blob, &length) ||
+                   !egg_buffer_get_string (resp, offset, &offset, &comment, g_realloc)) {
+                       g_message ("got unparseable response back from ssh-agent when requesting identities");
+                       g_hash_table_unref (answer);
+                       return NULL;
+               }
+               g_hash_table_insert (answer, g_bytes_new (blob, length), comment);
+       }
+
+       return answer;
+}
+
+
+static gboolean
+op_request_identities (GkdSshAgentService *self,
+                      EggBuffer *req,
+                      EggBuffer *resp,
+                      GCancellable *cancellable,
+                      GError **error)
+{
+       GHashTable *answer;
+       GHashTableIter iter;
+       gsize length;
+       guint32 added;
+       GBytes *key;
+       GList *keys;
+       GList *l;
+       GkdSshAgentPreload *preload;
+
+       if (!relay_request (self, req, resp, cancellable, error))
+               return FALSE;
+
+       /* Parse all the keys, and if it fails, just fall through */
+       answer = parse_identities_answer (resp);
+       if (!answer)
+               return TRUE;
+
+       g_hash_table_iter_init (&iter, answer);
+       while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
+               add_key (self, key);
+
+       added = 0;
+
+       /* Add any preloaded keys not already in answer */
+       preload = gkd_ssh_agent_service_get_preload (self);
+       keys = gkd_ssh_agent_preload_get_keys (preload);
+       for (l = keys; l != NULL; l = g_list_next (l)) {
+               GkdSshAgentKeyInfo *info = l->data;
+               if (!g_hash_table_contains (answer, info->public_key)) {
+                       const guchar *blob = g_bytes_get_data (info->public_key, &length);
+                       egg_buffer_add_byte_array (resp, blob, length);
+                       egg_buffer_add_string (resp, info->comment);
+                       added++;
+               }
+       }
+
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+
+       /* Set the correct amount of keys including the ones we added */
+       egg_buffer_set_uint32 (resp, 5, added + g_hash_table_size (answer));
+       g_hash_table_unref (answer);
+
+       /* Set the correct total size of the payload */
+       egg_buffer_set_uint32 (resp, 0, resp->len - 4);
+
+       return TRUE;
+}
+
+static gboolean
+op_sign_request (GkdSshAgentService *self,
+                EggBuffer *req,
+                EggBuffer *resp,
+                GCancellable *cancellable,
+                GError **error)
+{
+       const guchar *blob;
+       gsize length;
+       gsize offset = 5;
+       GBytes *key;
+
+       /* If parsing the request fails, just pass through */
+       if (egg_buffer_get_byte_array (req, offset, &offset, &blob, &length)) {
+               key = g_bytes_new (blob, length);
+               ensure_key (self, key);
+               g_bytes_unref (key);
+       } else {
+               g_message ("got unparseable sign request for ssh-agent");
+       }
+
+       return relay_request (self, req, resp, cancellable, error);
+}
+
+static gboolean
+op_remove_identity (GkdSshAgentService *self,
+                   EggBuffer *req,
+                   EggBuffer *resp,
+                   GCancellable *cancellable,
+                   GError **error)
+{
+       const guchar *blob;
+       gsize length;
+       gsize offset = 5;
+       GBytes *key = NULL;
+       gboolean ret;
+
+       /* If parsing the request fails, just pass through */
+       ret = egg_buffer_get_byte_array (req, offset, &offset, &blob, &length);
+       if (ret)
+               key = g_bytes_new (blob, length);
+       else
+               g_message ("got unparseable remove request for ssh-agent");
+
+       /* Call out ssh-agent anyway to make sure that the key is removed */
+       ret = relay_request (self, req, resp, cancellable, error);
+       if (key) {
+               if (ret)
+                       remove_key (self, key);
+               g_bytes_unref (key);
+       }
+       return ret;
+}
+
+static gboolean
+op_remove_all_identities (GkdSshAgentService *self,
+                         EggBuffer *req,
+                         EggBuffer *resp,
+                         GCancellable *cancellable,
+                         GError **error)
+{
+       gboolean ret;
+
+       ret = relay_request (self, req, resp, cancellable, error);
+       if (ret)
+               clear_keys (self);
+
+       return ret;
+}
+
+static const GkdSshAgentOperation operations[GKD_SSH_OP_MAX] = {
+       NULL,                                 /* 0 */
+       NULL,                                 /* GKR_SSH_OP_REQUEST_RSA_IDENTITIES */
+       NULL,                                 /* 2 */
+       NULL,                                 /* GKR_SSH_OP_RSA_CHALLENGE */
+       NULL,                                 /* 4 */
+       NULL,                                 /* 5 */
+       NULL,                                 /* 6 */
+       NULL,                                 /* GKR_SSH_OP_ADD_RSA_IDENTITY */
+       NULL,                                 /* GKR_SSH_OP_REMOVE_RSA_IDENTITY */
+       NULL,                                 /* GKR_SSH_OP_REMOVE_ALL_RSA_IDENTITIES */
+       NULL,                                 /* 10 */
+       op_request_identities,                /* GKR_SSH_OP_REQUEST_IDENTITIES */
+       NULL,                                 /* 12 */
+       op_sign_request,                      /* GKR_SSH_OP_SIGN_REQUEST */
+       NULL,                                 /* 14 */
+       NULL,                                 /* 15 */
+       NULL,                                 /* 16 */
+       op_add_identity,                      /* GKR_SSH_OP_ADD_IDENTITY */
+       op_remove_identity,                   /* GKR_SSH_OP_REMOVE_IDENTITY */
+       op_remove_all_identities,             /* GKR_SSH_OP_REMOVE_ALL_IDENTITIES */
+       NULL,                                 /* GKR_SSH_OP_ADD_SMARTCARD_KEY */
+       NULL,                                 /* GKR_SSH_OP_REMOVE_SMARTCARD_KEY */
+       NULL,                                 /* GKR_SSH_OP_LOCK */
+       NULL,                                 /* GKR_SSH_OP_UNLOCK */
+       NULL,                                 /* GKR_SSH_OP_ADD_RSA_ID_CONSTRAINED */
+       op_add_identity,                      /* GKR_SSH_OP_ADD_ID_CONSTRAINED */
+       NULL,                                 /* GKR_SSH_OP_ADD_SMARTCARD_KEY_CONSTRAINED */
+};
diff --git a/daemon/ssh-agent/gkd-ssh-agent-service.h b/daemon/ssh-agent/gkd-ssh-agent-service.h
new file mode 100644
index 0000000..9d3b2f1
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-service.h
@@ -0,0 +1,54 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2007 Stefan Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#ifndef __GKD_SSH_AGENT_SERVICE_H__
+#define __GKD_SSH_AGENT_SERVICE_H__
+
+#include <gio/gio.h>
+#include "gkd-ssh-agent-preload.h"
+#include "gkd-ssh-agent-process.h"
+#include "egg/egg-buffer.h"
+
+#define GKD_TYPE_SSH_AGENT_SERVICE gkd_ssh_agent_service_get_type ()
+G_DECLARE_FINAL_TYPE (GkdSshAgentService, gkd_ssh_agent_service, GKD, SSH_AGENT_SERVICE, GObject);
+
+GkdSshAgentService *gkd_ssh_agent_service_new  (const gchar        *path,
+                                                GTlsInteraction    *interaction,
+                                                GkdSshAgentPreload *preload);
+
+gboolean            gkd_ssh_agent_service_start
+                                               (GkdSshAgentService *self);
+
+void                gkd_ssh_agent_service_stop (GkdSshAgentService *self);
+
+GkdSshAgentPreload *gkd_ssh_agent_service_get_preload
+                                               (GkdSshAgentService *self);
+
+GkdSshAgentProcess *gkd_ssh_agent_service_get_process
+                                               (GkdSshAgentService *self);
+
+gboolean            gkd_ssh_agent_service_lookup_key
+                                               (GkdSshAgentService *self,
+                                                GBytes             *key);
+
+#endif /* __GKD_SSH_AGENT_SERVICE_H__ */
diff --git a/daemon/ssh-agent/gkd-ssh-agent-util.c b/daemon/ssh-agent/gkd-ssh-agent-util.c
new file mode 100644
index 0000000..464cd1c
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-util.c
@@ -0,0 +1,163 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2014 Stef Walter
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>, Daiki Ueno
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gkd-ssh-agent-util.h"
+
+gboolean
+_gkd_ssh_agent_read_packet (GSocketConnection *connection,
+                           EggBuffer *buffer,
+                           GCancellable *cancellable,
+                           GError **error)
+{
+       GInputStream *stream;
+       guint32 packet_size;
+       gsize bytes_read;
+
+       stream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
+
+       egg_buffer_reset (buffer);
+       egg_buffer_resize (buffer, 4);
+
+       if (!g_input_stream_read_all (stream, buffer->buf, 4, &bytes_read, cancellable, error))
+               return FALSE;
+
+       if (!egg_buffer_get_uint32 (buffer, 0, NULL, &packet_size) ||
+           packet_size < 1) {
+               g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                            "invalid packet size %u",
+                            packet_size);
+               return FALSE;
+       }
+
+       egg_buffer_resize (buffer, packet_size + 4);
+       if (!g_input_stream_read_all (stream, buffer->buf + 4, packet_size, &bytes_read, cancellable, error))
+               return FALSE;
+
+       return TRUE;
+}
+
+gboolean
+_gkd_ssh_agent_write_packet (GSocketConnection *connection,
+                            EggBuffer *buffer,
+                            GCancellable *cancellable,
+                            GError **error)
+{
+       GOutputStream *stream;
+       gsize bytes_written;
+
+       stream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
+       if (!egg_buffer_set_uint32 (buffer, 0, buffer->len - 4))
+               g_return_val_if_reached (FALSE);
+       return g_output_stream_write_all (stream, buffer->buf, buffer->len, &bytes_written, cancellable, 
error);
+}
+
+GBytes *
+_gkd_ssh_agent_parse_public_key (GBytes *input,
+                                gchar **comment)
+{
+       const guchar *at;
+       guchar *decoded;
+       gsize n_decoded;
+       gint state;
+       guint save;
+       const guchar *data;
+       gsize n_data;
+
+       g_return_val_if_fail (input, NULL);
+
+       data = g_bytes_get_data (input, &n_data);
+
+       /* Look for a key line */
+       for (;;) {
+               /* Eat space at the front */
+               while (n_data > 0 && g_ascii_isspace (data[0])) {
+                       ++data;
+                       --n_data;
+               }
+
+               /* Not a comment or blank line? Then parse... */
+               if (data[0] != '#')
+                       break;
+
+               /* Skip to the next line */
+               at = memchr (data, '\n', n_data);
+               if (!at)
+                       return NULL;
+               at += 1;
+               n_data -= (at - data);
+               data = at;
+       }
+
+       /* Limit to use only the first line */
+       at = memchr (data, '\n', n_data);
+       if (at != NULL)
+               n_data = at - data;
+
+       /* Find the first space */
+       at = memchr (data, ' ', n_data);
+       if (!at) {
+               g_message ("SSH public key missing space");
+               return NULL;
+       }
+
+       /* Skip more whitespace */
+       n_data -= (at - data);
+       data = at;
+       while (n_data > 0 && (data[0] == ' ' || data[0] == '\t')) {
+               ++data;
+               --n_data;
+       }
+
+       /* Find the next whitespace, or the end */
+       at = memchr (data, ' ', n_data);
+       if (at == NULL)
+               at = data + n_data;
+
+       /* Decode the base64 key */
+       save = state = 0;
+       decoded = g_malloc (n_data * 3 / 4);
+       n_decoded = g_base64_decode_step ((gchar*)data, at - data, decoded, &state, &save);
+
+       if (!n_decoded) {
+               g_free (decoded);
+               return NULL;
+       }
+
+       /* Skip more whitespace */
+       n_data -= (at - data);
+       data = at;
+       while (n_data > 0 && (data[0] == ' ' || data[0] == '\t')) {
+               ++data;
+               --n_data;
+       }
+
+       /* If there's data left, its the comment */
+       if (comment)
+               *comment = n_data ? g_strndup ((gchar*)data, n_data) : g_strdup ("");
+
+       return g_bytes_new_take (decoded, n_decoded);
+}
diff --git a/daemon/ssh-agent/gkd-ssh-agent-util.h b/daemon/ssh-agent/gkd-ssh-agent-util.h
new file mode 100644
index 0000000..8b16b3e
--- /dev/null
+++ b/daemon/ssh-agent/gkd-ssh-agent-util.h
@@ -0,0 +1,43 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daiki Ueno
+ */
+
+#include <gio/gio.h>
+#include "egg/egg-buffer.h"
+
+#ifndef __GKD_SSH_AGENT_UTIL_H__
+#define __GKD_SSH_AGENT_UTIL_H__ 1
+
+gboolean _gkd_ssh_agent_read_packet      (GSocketConnection  *connection,
+                                          EggBuffer          *buffer,
+                                          GCancellable       *cancellable,
+                                          GError            **error);
+
+gboolean _gkd_ssh_agent_write_packet     (GSocketConnection  *connection,
+                                          EggBuffer          *buffer,
+                                          GCancellable       *cancellable,
+                                          GError            **error);
+
+GBytes  *_gkd_ssh_agent_parse_public_key (GBytes             *input,
+                                          gchar             **comment);
+
+
+#endif /* __GKD_SSH_AGENT_UTIL_H__ */
diff --git a/daemon/ssh-agent/test-common.c b/daemon/ssh-agent/test-common.c
new file mode 100644
index 0000000..76b1c7b
--- /dev/null
+++ b/daemon/ssh-agent/test-common.c
@@ -0,0 +1,347 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daiki Ueno
+ */
+
+#include "config.h"
+
+#include "test-common.h"
+
+#include "gkd-ssh-agent-private.h"
+#include "gkd-ssh-agent-util.h"
+#include "pkcs11/gkm/gkm-mock.h"
+
+/* RSA private key blob decoded from pkcs11/ssh-store/fixtures/id_rsa_plain */
+static const guint8 private_blob[4*6 + 0x101 + 0x1 + 0x101 + 0x80 + 0x81 + 0x81] = {
+       /* n */
+       0x00, 0x00, 0x01, 0x01, 0x00, 0xa0, 0x3e, 0x95, 0x2a, 0xa9, 0x21, 0x6b,
+       0x2e, 0xa9, 0x28, 0x74, 0x91, 0x8c, 0x01, 0x96, 0x59, 0xf1, 0x4f, 0x53,
+       0xcc, 0x5f, 0xb2, 0x2d, 0xa0, 0x9c, 0xec, 0x0f, 0xfc, 0x1d, 0x54, 0x1c,
+       0x3a, 0x33, 0xb7, 0x1d, 0xdc, 0xce, 0x13, 0xbe, 0xa7, 0x2f, 0xdf, 0x4e,
+       0x58, 0x42, 0x9d, 0x23, 0xf5, 0x8e, 0xc8, 0xe4, 0xad, 0x52, 0x19, 0x72,
+       0x7c, 0xda, 0x87, 0x67, 0xd4, 0x34, 0x51, 0x51, 0x81, 0x2e, 0x3e, 0x8d,
+       0x13, 0x81, 0xb6, 0xf6, 0xe0, 0x1e, 0xc4, 0xbb, 0xd9, 0x5d, 0x44, 0xeb,
+       0xe6, 0x68, 0x81, 0x5f, 0xa6, 0x04, 0x95, 0x96, 0x02, 0x1c, 0x34, 0x88,
+       0xfa, 0xe6, 0x43, 0x72, 0xaf, 0x9b, 0x7f, 0x03, 0xdc, 0xf0, 0x72, 0xa3,
+       0x96, 0x3b, 0xc8, 0xa3, 0xb9, 0x90, 0x81, 0xb6, 0x2e, 0x5a, 0x18, 0x2e,
+       0x3a, 0x2c, 0x27, 0x91, 0x78, 0xb3, 0x1d, 0xb1, 0x87, 0x4b, 0xb3, 0xdb,
+       0x05, 0xcd, 0xb6, 0x76, 0x35, 0x6f, 0x9c, 0x61, 0x7b, 0x6f, 0x95, 0x12,
+       0x4b, 0x26, 0xf4, 0xe0, 0x7e, 0x15, 0x76, 0x94, 0x91, 0x90, 0xb6, 0x7d,
+       0x0a, 0xd3, 0x36, 0x8f, 0x19, 0x18, 0x52, 0x50, 0x48, 0x57, 0x7c, 0x91,
+       0x48, 0x48, 0x7d, 0xb5, 0x03, 0x26, 0x69, 0x58, 0xb9, 0x9f, 0xaf, 0xbc,
+       0x73, 0x3e, 0x03, 0x72, 0xdc, 0xf6, 0xb1, 0xf2, 0x5b, 0x82, 0x0f, 0x69,
+       0x1c, 0xb1, 0x15, 0x07, 0x22, 0x46, 0x66, 0xfe, 0x65, 0x0a, 0x94, 0xda,
+       0xe4, 0x9d, 0x39, 0x70, 0x21, 0x83, 0x5e, 0xe5, 0xb2, 0x4b, 0x97, 0xfe,
+       0xaf, 0x32, 0x08, 0x8e, 0x47, 0xcb, 0x97, 0x83, 0x89, 0xc0, 0xb6, 0xdb,
+       0x6a, 0x14, 0x31, 0xd2, 0x53, 0xb5, 0x88, 0x30, 0x5f, 0x87, 0x50, 0x09,
+       0x4f, 0x13, 0x20, 0x25, 0xa1, 0xc5, 0xbd, 0xf1, 0xe1, 0x10, 0x95, 0xfa,
+       0x0e, 0xc3, 0xf7, 0xdf, 0xad, 0x90, 0x8b, 0xef, 0xfb,
+       /* e */
+       0x00, 0x00, 0x00, 0x01, 0x23,
+       /* d */
+       0x00, 0x00, 0x01, 0x01, 0x00, 0x9b, 0xaa, 0x82, 0x46, 0xb2, 0xed, 0x43,
+       0x8c, 0x69, 0xcf, 0x87, 0x2e, 0x4d, 0x7d, 0xe2, 0x83, 0x42, 0x2f, 0xcd,
+       0xbf, 0x38, 0x63, 0xf1, 0xcf, 0x39, 0x5a, 0x58, 0xab, 0xc4, 0xb8, 0x1b,
+       0x6b, 0xbd, 0x35, 0x8a, 0xb9, 0x3d, 0x37, 0xc0, 0x85, 0x27, 0x30, 0xb2,
+       0x81, 0x9f, 0xcb, 0xd9, 0xc9, 0xf8, 0x6b, 0x61, 0xcc, 0xf0, 0xab, 0x01,
+       0x80, 0x99, 0xc5, 0x5d, 0x8c, 0x50, 0x14, 0x7b, 0x0f, 0xc6, 0x85, 0xe8,
+       0x21, 0x93, 0xf3, 0x90, 0xbc, 0x75, 0xa9, 0x2b, 0x82, 0xb2, 0x60, 0x35,
+       0x9d, 0xff, 0x1e, 0x97, 0x6e, 0x13, 0x14, 0xf8, 0x1f, 0x4e, 0x99, 0x6f,
+       0x1f, 0x9d, 0xdb, 0x1e, 0xf3, 0xbb, 0x9f, 0xf5, 0x1f, 0xc5, 0x01, 0xa6,
+       0x3a, 0x2b, 0x72, 0x73, 0x29, 0x4a, 0x8c, 0xa2, 0x58, 0xe9, 0xce, 0x58,
+       0xca, 0xcb, 0xce, 0xaa, 0x92, 0x82, 0x1c, 0xd8, 0x57, 0x8b, 0x5e, 0x42,
+       0x79, 0x21, 0x0e, 0x63, 0x13, 0x0e, 0x03, 0xff, 0x2f, 0x7f, 0x64, 0xf6,
+       0x82, 0xe1, 0xfe, 0x0b, 0xc3, 0x1e, 0x4c, 0x50, 0x11, 0x3f, 0xc8, 0x8a,
+       0xba, 0xcc, 0xde, 0x24, 0xf7, 0xae, 0x96, 0x6c, 0x5e, 0x3b, 0x00, 0xfa,
+       0xf0, 0x0e, 0xac, 0x3a, 0xeb, 0xb1, 0xab, 0x8f, 0x3f, 0xdb, 0x80, 0xb3,
+       0x06, 0x91, 0x18, 0xe1, 0xf5, 0x3b, 0xec, 0x5d, 0x01, 0xcf, 0xd0, 0x1f,
+       0xaf, 0xe3, 0xd9, 0x12, 0xba, 0x7b, 0x0f, 0xee, 0x20, 0x29, 0x74, 0x57,
+       0xdc, 0x58, 0x75, 0xd4, 0xb0, 0xf4, 0xb4, 0xa4, 0x93, 0x48, 0x2b, 0x7b,
+       0x6b, 0x1d, 0x77, 0xbc, 0xf3, 0xfe, 0xbd, 0xad, 0xd6, 0x83, 0x05, 0x16,
+       0xca, 0xbe, 0x31, 0xa4, 0x39, 0x53, 0x29, 0xf3, 0xd3, 0x39, 0xb0, 0xa5,
+       0xef, 0xf0, 0xc9, 0x08, 0xd6, 0x63, 0x52, 0x0b, 0xcb, 0xfc, 0x1c, 0x21,
+       0xd3, 0xa9, 0x2f, 0x23, 0x92, 0x3d, 0x46, 0x8c, 0x4b,
+       /* iqmp */
+       0x00, 0x00, 0x00, 0x80, 0x15, 0x40, 0xcc, 0xa4, 0x83, 0xdf, 0x26, 0xbe,
+       0x55, 0x82, 0x85, 0x0f, 0x71, 0x3c, 0x19, 0xa8, 0x8b, 0x42, 0x80, 0xa5,
+       0x24, 0x5d, 0xad, 0xf5, 0x99, 0x33, 0xaf, 0x7c, 0xb2, 0x27, 0xae, 0x7b,
+       0x0b, 0x0b, 0xa0, 0x03, 0xfd, 0xae, 0x53, 0x6f, 0xf1, 0xdd, 0x83, 0x54,
+       0xde, 0xf2, 0xbd, 0x87, 0x2c, 0xa9, 0x4d, 0x7b, 0xa5, 0x6e, 0xdb, 0x5e,
+       0x89, 0xf4, 0x5c, 0x79, 0x22, 0xc3, 0xc4, 0x40, 0x50, 0xeb, 0xb7, 0xf4,
+       0x17, 0x78, 0x2f, 0x06, 0xa5, 0x3a, 0x65, 0x4d, 0x85, 0x98, 0x3e, 0xd8,
+       0x4d, 0x3b, 0xfc, 0xd8, 0x9b, 0xe5, 0xd1, 0x47, 0xb6, 0xe3, 0xda, 0x2e,
+       0xc5, 0x18, 0xce, 0x37, 0xd9, 0xd7, 0x9a, 0xbf, 0xba, 0xa9, 0xef, 0xf2,
+       0xaf, 0x9b, 0xc8, 0x46, 0x57, 0x11, 0x8c, 0xa9, 0x5f, 0x68, 0x8c, 0x43,
+       0x2f, 0xb5, 0x7a, 0x39, 0x38, 0x30, 0x79, 0xd5, 0x30, 0xa8, 0x2b, 0x98,
+       /* p */
+       0x00, 0x00, 0x00, 0x81, 0x00, 0xcc, 0x50, 0xb1, 0x2c, 0x5f, 0xe4, 0x02,
+       0x85, 0x7d, 0xce, 0x77, 0xd8, 0x27, 0xc1, 0xf6, 0xee, 0xe2, 0x2b, 0x7b,
+       0x29, 0x83, 0x95, 0xf1, 0x5e, 0x3d, 0xe5, 0xa9, 0x75, 0x62, 0xc6, 0x84,
+       0xc9, 0x97, 0x26, 0x70, 0xf4, 0x0d, 0x28, 0x6a, 0xc6, 0x88, 0x7c, 0xa3,
+       0x0d, 0x35, 0xa3, 0x8f, 0xdc, 0x34, 0x4c, 0x78, 0x6b, 0xcc, 0x5d, 0x99,
+       0x7e, 0x45, 0xb0, 0xdf, 0xe3, 0x77, 0x48, 0x77, 0xd8, 0xa9, 0x1c, 0x74,
+       0xf9, 0xbc, 0xcc, 0x82, 0xdb, 0x44, 0x10, 0x96, 0xda, 0x00, 0x23, 0xaa,
+       0x04, 0x93, 0xcc, 0x98, 0xec, 0x26, 0x8b, 0x7d, 0x08, 0xf4, 0x82, 0xdc,
+       0x9a, 0xc4, 0x8c, 0xc8, 0xe9, 0x3e, 0x5b, 0xd6, 0xc7, 0x28, 0xf4, 0x38,
+       0x3a, 0x3c, 0x08, 0x56, 0xbb, 0xa2, 0xca, 0xfb, 0x05, 0xa0, 0xb7, 0xe1,
+       0x70, 0x59, 0xb4, 0x86, 0x2b, 0x29, 0x89, 0xb5, 0x82, 0x2a, 0x79, 0x61,
+       0x51,
+       /* q */
+       0x00, 0x00, 0x00, 0x81, 0x00, 0xc8, 0xc7, 0xe6, 0x93, 0x90, 0x59, 0xe7,
+       0x54, 0x1b, 0xcf, 0x9c, 0xb0, 0x07, 0x80, 0x37, 0xcd, 0xdf, 0x65, 0xf4,
+       0x29, 0x1e, 0x4a, 0x93, 0x73, 0xd1, 0x7b, 0x47, 0x1d, 0x36, 0x87, 0x89,
+       0x1d, 0xbf, 0xd5, 0x1e, 0x02, 0xc2, 0xd1, 0x2b, 0xb3, 0x67, 0x07, 0x65,
+       0xf9, 0xbc, 0xcb, 0x74, 0x4c, 0x83, 0x68, 0xa8, 0x6d, 0x30, 0x68, 0x8f,
+       0xb5, 0xb9, 0x44, 0x86, 0xb8, 0xde, 0x4e, 0xfc, 0x02, 0x1e, 0x9c, 0x05,
+       0x3b, 0x23, 0x1b, 0xdf, 0x79, 0x58, 0x73, 0x51, 0x27, 0xf0, 0xbd, 0x83,
+       0x34, 0x38, 0xcb, 0xd0, 0x20, 0x12, 0xcd, 0x1a, 0x07, 0x6e, 0xf7, 0x0a,
+       0x92, 0x29, 0xff, 0x2f, 0xbf, 0x30, 0x2a, 0x69, 0x15, 0x4d, 0x8e, 0x6e,
+       0x17, 0x26, 0x7b, 0x43, 0xfe, 0x52, 0xd1, 0x83, 0x65, 0x19, 0x22, 0x8b,
+       0xd3, 0x6f, 0x97, 0x51, 0x11, 0x3f, 0x17, 0xfe, 0x05, 0xcc, 0xa4, 0x49,
+       0x8b
+};
+
+/* define gkd_pkcs11_get_base_functions() for gkd-login.c */
+CK_FUNCTION_LIST_PTR
+gkd_pkcs11_get_base_functions (void);
+
+CK_FUNCTION_LIST_PTR
+gkd_pkcs11_get_base_functions (void)
+{
+       CK_FUNCTION_LIST_PTR funcs;
+       gkm_mock_C_GetFunctionList (&funcs);
+       return funcs;
+}
+
+void
+prepare_request_identities (EggBuffer *req)
+{
+       gboolean ret;
+
+       egg_buffer_reset (req);
+
+       ret = egg_buffer_add_uint32 (req, 1);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (req, GKD_SSH_OP_REQUEST_IDENTITIES);
+       g_assert_true (ret);
+}
+
+void
+check_identities_answer (EggBuffer *resp, gsize count)
+{
+       uint32_t length;
+       unsigned char code;
+       size_t offset;
+       gboolean ret;
+
+       offset = 0;
+       ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
+       g_assert_true (ret);
+       g_assert_cmpint (length, ==, resp->len - 4);
+
+       code = 0;
+       ret = egg_buffer_get_byte (resp, offset, &offset, &code);
+       g_assert_true (ret);
+       g_assert_cmpint (code, ==, GKD_SSH_RES_IDENTITIES_ANSWER);
+
+       ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
+       g_assert_true (ret);
+       g_assert_cmpint (length, ==, count);
+}
+
+void
+prepare_add_identity (EggBuffer *req)
+{
+       gboolean ret;
+
+       egg_buffer_reset (req);
+
+       ret = egg_buffer_add_uint32 (req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (req, GKD_SSH_OP_ADD_IDENTITY);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_string (req, "ssh-rsa");
+       g_assert_true (ret);
+
+       ret = egg_buffer_append (req, private_blob, G_N_ELEMENTS(private_blob));
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_string (req, "comment");
+       g_assert_true (ret);
+
+       ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
+       g_assert_true (ret);
+}
+
+GBytes *
+public_key_from_file (const gchar *path, gchar **comment)
+{
+       GBytes *public_bytes;
+       GBytes *public_key;
+
+       GError *error = NULL;
+       gchar *contents;
+       gsize length;
+
+       if (!g_file_get_contents (path, &contents, &length, &error)) {
+               g_message ("couldn't read file: %s: %s", path, error->message);
+               g_error_free (error);
+               return NULL;
+       }
+
+       public_bytes = g_bytes_new_take (contents, length);
+       public_key = _gkd_ssh_agent_parse_public_key (public_bytes, comment);
+       g_bytes_unref (public_bytes);
+
+       return public_key;
+}
+
+void
+prepare_remove_identity (EggBuffer *req)
+{
+       GBytes *public_key;
+       gchar *comment;
+       gsize length;
+       const guchar *blob;
+       gboolean ret;
+
+       public_key = public_key_from_file (SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub", &comment);
+       g_free (comment);
+       blob = g_bytes_get_data (public_key, &length);
+
+       egg_buffer_reset (req);
+       ret = egg_buffer_add_uint32 (req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (req, GKD_SSH_OP_REMOVE_IDENTITY);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte_array (req, blob, length);
+       g_assert_true (ret);
+
+       ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
+       g_assert_true (ret);
+
+       g_bytes_unref (public_key);
+}
+
+void
+prepare_remove_all_identities (EggBuffer *req)
+{
+       gboolean ret;
+
+       egg_buffer_reset (req);
+       ret = egg_buffer_add_uint32 (req, 1);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (req, GKD_SSH_OP_REMOVE_ALL_IDENTITIES);
+       g_assert_true (ret);
+}
+
+void
+check_response (EggBuffer *resp, unsigned char expected)
+{
+       uint32_t length;
+       unsigned char code;
+       size_t offset;
+       gboolean ret;
+
+       offset = 0;
+       ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
+       g_assert_true (ret);
+       g_assert_cmpint (length, ==, resp->len - 4);
+
+       code = 0;
+       ret = egg_buffer_get_byte (resp, offset, &offset, &code);
+       g_assert_true (ret);
+       g_assert_cmpint (expected, ==, code);
+}
+
+void
+check_success (EggBuffer *resp)
+{
+       check_response (resp, GKD_SSH_RES_SUCCESS);
+}
+
+void
+check_failure (EggBuffer *resp)
+{
+       check_response (resp, GKD_SSH_RES_FAILURE);
+}
+
+void
+prepare_sign_request (EggBuffer *req)
+{
+       GBytes *public_key;
+       gchar *comment;
+       gsize length;
+       const guchar *blob;
+       gboolean ret;
+
+       public_key = public_key_from_file (SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub", &comment);
+       g_free (comment);
+       blob = g_bytes_get_data (public_key, &length);
+
+       egg_buffer_reset (req);
+       ret = egg_buffer_add_uint32 (req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (req, GKD_SSH_OP_SIGN_REQUEST);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte_array (req, blob, length);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_string (req, "data");
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_uint32 (req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
+       g_assert_true (ret);
+
+       g_bytes_unref (public_key);
+}
+
+void
+check_sign_response (EggBuffer *resp)
+{
+       uint32_t length;
+       unsigned char code;
+       size_t offset;
+       gboolean ret;
+
+       offset = 0;
+       ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
+       g_assert_true (ret);
+       g_assert_cmpint (length, ==, resp->len - 4);
+
+       code = 0;
+       ret = egg_buffer_get_byte (resp, offset, &offset, &code);
+       g_assert_true (ret);
+       g_assert_cmpint (code, ==, GKD_SSH_RES_SIGN_RESPONSE);
+
+       ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
+       g_assert_true (ret);
+}
diff --git a/daemon/ssh-agent/test-common.h b/daemon/ssh-agent/test-common.h
new file mode 100644
index 0000000..f4849fd
--- /dev/null
+++ b/daemon/ssh-agent/test-common.h
@@ -0,0 +1,94 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daiki Ueno
+ */
+
+#include <glib.h>
+#include "egg/egg-buffer.h"
+
+void prepare_request_identities (EggBuffer *req);
+void prepare_add_identity (EggBuffer *req);
+void prepare_remove_identity (EggBuffer *req);
+void prepare_remove_all_identities (EggBuffer *req);
+void prepare_sign_request (EggBuffer *req);
+
+void check_identities_answer (EggBuffer *resp, gsize count);
+void check_sign_response (EggBuffer *resp);
+void check_response (EggBuffer *resp, unsigned char expected);
+void check_success (EggBuffer *resp);
+void check_failure (EggBuffer *resp);
+
+GBytes *public_key_from_file (const gchar *path, gchar **comment);
+
+#define DEFINE_CALL_FUNCS(Test, Call)                  \
+static inline void                                     \
+call_request_identities (Test *test, gsize count)      \
+{                                                      \
+       egg_buffer_reset (&test->req);                  \
+       egg_buffer_reset (&test->resp);                 \
+                                                       \
+       prepare_request_identities (&test->req);        \
+       Call (test);                                    \
+       check_identities_answer (&test->resp, count);   \
+}                                                      \
+                                                       \
+static inline void                                     \
+call_add_identity (Test *test)                         \
+{                                                      \
+       egg_buffer_reset (&test->req);                  \
+       egg_buffer_reset (&test->resp);                 \
+                                                       \
+       prepare_add_identity (&test->req);              \
+       Call (test);                                    \
+       check_success (&test->resp);                    \
+}                                                      \
+                                                       \
+static inline void                                     \
+call_remove_identity (Test *test)                      \
+{                                                      \
+       egg_buffer_reset (&test->req);                  \
+       egg_buffer_reset (&test->resp);                 \
+                                                       \
+       prepare_remove_identity (&test->req);           \
+       Call (test);                                    \
+       check_success (&test->resp);                    \
+}                                                      \
+                                                       \
+static inline void                                     \
+call_remove_all_identities (Test *test)                        \
+{                                                      \
+       egg_buffer_reset (&test->req);                  \
+       egg_buffer_reset (&test->resp);                 \
+                                                       \
+       prepare_remove_all_identities (&test->req);     \
+       Call (test);                                    \
+       check_success (&test->resp);                    \
+}                                                      \
+                                                       \
+static inline void                                     \
+call_sign (Test *test)                                 \
+{                                                      \
+       egg_buffer_reset (&test->req);                  \
+       egg_buffer_reset (&test->resp);                 \
+                                                       \
+       prepare_sign_request (&test->req);              \
+       Call (test);                                    \
+       check_sign_response (&test->resp);              \
+}
diff --git a/daemon/ssh-agent/test-gkd-ssh-agent-preload.c b/daemon/ssh-agent/test-gkd-ssh-agent-preload.c
new file mode 100644
index 0000000..bce2197
--- /dev/null
+++ b/daemon/ssh-agent/test-gkd-ssh-agent-preload.c
@@ -0,0 +1,166 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daiki Ueno
+ */
+
+#include "config.h"
+
+#include "gkd-ssh-agent-preload.h"
+#include "egg/egg-testing.h"
+
+#include <glib/gstdio.h>
+#include <unistd.h>
+
+typedef struct {
+       gchar *directory;
+       GkdSshAgentPreload *preload;
+} Test;
+
+static void
+setup (Test *test, gconstpointer unused)
+{
+       test->directory = egg_tests_create_scratch_directory (NULL, NULL);
+
+       egg_tests_copy_scratch_file (test->directory, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain");
+       egg_tests_copy_scratch_file (test->directory, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub");
+
+       test->preload = gkd_ssh_agent_preload_new (test->directory);
+}
+
+static void
+teardown (Test *test, gconstpointer unused)
+{
+       g_object_unref (test->preload);
+
+       egg_tests_remove_scratch_directory (test->directory);
+       g_free (test->directory);
+}
+
+static void
+test_list (Test *test, gconstpointer unused)
+{
+       GList *keys;
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (1, ==, g_list_length (keys));
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+}
+
+static void
+test_added (Test *test, gconstpointer unused)
+{
+       GList *keys;
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (1, ==, g_list_length (keys));
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+
+       /* Mtime must change so wait between tests */
+       sleep (1);
+
+       egg_tests_copy_scratch_file (test->directory, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain");
+       egg_tests_copy_scratch_file (test->directory, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain.pub");
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (2, ==, g_list_length (keys));
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+}
+
+static void
+test_removed (Test *test, gconstpointer unused)
+{
+       GList *keys;
+       gchar *path;
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (1, ==, g_list_length (keys));
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+
+       /* Mtime must change so wait between tests */
+       sleep (1);
+
+       path = g_build_filename (test->directory, "id_rsa_plain.pub", NULL);
+       g_unlink (path);
+       g_free (path);
+
+       path = g_build_filename (test->directory, "id_rsa_plain", NULL);
+       g_unlink (path);
+       g_free (path);
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (0, ==, g_list_length (keys));
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+}
+
+static void
+test_changed (Test *test, gconstpointer unused)
+{
+       GList *keys;
+       gchar *path;
+       gchar *contents;
+       gsize length;
+       GError *error;
+       gchar *p;
+       gboolean ret;
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (1, ==, g_list_length (keys));
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+
+       /* Mtime must change so wait between tests */
+       sleep (1);
+
+       path = g_build_filename (test->directory, "id_rsa_plain.pub", NULL);
+       error = NULL;
+       ret = g_file_get_contents (path, &contents, &length, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+
+#define COMMENT "comment"
+       contents = g_realloc (contents, length + strlen (COMMENT) + 1);
+       p = strchr (contents, '\n');
+       g_assert_nonnull (p);
+       memcpy (p, " " COMMENT "\n", strlen (COMMENT) + 2);
+       error = NULL;
+       ret = g_file_set_contents (path, contents, length + strlen (COMMENT), &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+       g_free (path);
+       g_free (contents);
+
+       keys = gkd_ssh_agent_preload_get_keys (test->preload);
+       g_assert_cmpint (1, ==, g_list_length (keys));
+       g_assert_cmpstr (COMMENT, ==, ((GkdSshAgentKeyInfo *)keys->data)->comment);
+       g_list_free_full (keys, (GDestroyNotify)gkd_ssh_agent_key_info_free);
+#undef COMMENT
+}
+
+int
+main (int argc, char **argv)
+{
+       g_test_init (&argc, &argv, NULL);
+
+       g_test_add ("/ssh-agent/preload/list", Test, NULL, setup, test_list, teardown);
+       g_test_add ("/ssh-agent/preload/added", Test, NULL, setup, test_added, teardown);
+       g_test_add ("/ssh-agent/preload/removed", Test, NULL, setup, test_removed, teardown);
+       g_test_add ("/ssh-agent/preload/changed", Test, NULL, setup, test_changed, teardown);
+
+       return g_test_run ();
+}
diff --git a/daemon/ssh-agent/test-gkd-ssh-agent-process.c b/daemon/ssh-agent/test-gkd-ssh-agent-process.c
new file mode 100644
index 0000000..329eda1
--- /dev/null
+++ b/daemon/ssh-agent/test-gkd-ssh-agent-process.c
@@ -0,0 +1,217 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daiki Ueno
+ */
+
+#include "config.h"
+
+#include "gkd-ssh-agent-private.h"
+#include "gkd-ssh-agent-process.h"
+#include "gkd-ssh-agent-util.h"
+#include "test-common.h"
+#include "egg/egg-testing.h"
+
+#include <glib.h>
+
+typedef struct {
+       gchar *directory;
+       EggBuffer req;
+       EggBuffer resp;
+       GkdSshAgentProcess *process;
+       GMainLoop *loop;
+} Test;
+
+static void
+setup (Test *test, gconstpointer unused)
+{
+       gchar *path;
+
+       test->directory = egg_tests_create_scratch_directory (NULL, NULL);
+
+       egg_buffer_init_full (&test->req, 128, (EggBufferAllocator)g_realloc);
+       egg_buffer_init_full (&test->resp, 128, (EggBufferAllocator)g_realloc);
+
+       path = g_strdup_printf ("%s/.ssh.sock", test->directory);
+       test->process = gkd_ssh_agent_process_new (path);
+       g_free (path);
+       g_assert_nonnull (test->process);
+}
+
+static void
+teardown (Test *test, gconstpointer unused)
+{
+       g_clear_object (&test->process);
+
+       egg_buffer_uninit (&test->req);
+       egg_buffer_uninit (&test->resp);
+
+       egg_tests_remove_scratch_directory (test->directory);
+       free (test->directory);
+}
+
+static void
+connect_to_process (Test *test)
+{
+       GError *error;
+       gboolean ret;
+
+       error = NULL;
+       ret = gkd_ssh_agent_process_connect (test->process, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+}
+
+static void
+test_connect (Test *test, gconstpointer unused)
+{
+       connect_to_process (test);
+}
+
+static void
+call (Test *test)
+{
+       GError *error;
+       gboolean ret;
+
+       error = NULL;
+       ret = gkd_ssh_agent_process_call (test->process, &test->req, &test->resp, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+}
+
+DEFINE_CALL_FUNCS(Test, call)
+
+static void
+test_list (Test *test, gconstpointer unused)
+{
+       connect_to_process (test);
+       call_request_identities(test, 0);
+}
+
+static void
+test_add (Test *test, gconstpointer unused)
+{
+       connect_to_process (test);
+       call_add_identity (test);
+       call_request_identities (test, 1);
+}
+
+static void
+test_remove (Test *test, gconstpointer unused)
+{
+       connect_to_process (test);
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       call_remove_identity (test);
+       call_request_identities (test, 0);
+}
+
+static void
+test_remove_all (Test *test, gconstpointer unused)
+{
+       connect_to_process (test);
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       call_remove_all_identities (test);
+       call_request_identities (test, 0);
+}
+
+static void
+test_sign (Test *test, gconstpointer unused)
+{
+       connect_to_process (test);
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       call_sign (test);
+
+       call_remove_all_identities (test);
+       call_request_identities (test, 0);
+}
+
+static gpointer
+kill_thread (gpointer data)
+{
+       Test *test = data;
+       GPid pid;
+
+       pid = gkd_ssh_agent_process_get_pid (test->process);
+       g_assert_cmpint (-1, !=, pid);
+
+       kill (pid, SIGTERM);
+
+       return NULL;
+}
+
+static void
+on_closed (GkdSshAgentProcess *self, gpointer data)
+{
+       GMainLoop *loop = data;
+
+       g_main_loop_quit (loop);
+       g_main_loop_unref (loop);
+}
+
+static void
+test_restart (Test *test, gconstpointer unused)
+{
+       GPid pid;
+       GMainLoop *loop;
+       GThread *thread;
+
+       connect_to_process (test);
+
+       pid = gkd_ssh_agent_process_get_pid (test->process);
+       g_assert_cmpint (0, !=, pid);
+
+       thread = g_thread_new ("kill", kill_thread, test);
+
+       loop = g_main_loop_new (NULL, FALSE);
+       g_signal_connect (test->process, "closed", G_CALLBACK (on_closed), loop);
+       g_main_loop_run (loop);
+
+       g_thread_join (thread);
+
+       pid = gkd_ssh_agent_process_get_pid (test->process);
+       g_assert_cmpint (0, ==, pid);
+
+       connect_to_process (test);
+
+       pid = gkd_ssh_agent_process_get_pid (test->process);
+       g_assert_cmpint (0, !=, pid);
+}
+
+int
+main (int argc, char **argv)
+{
+       g_test_init (&argc, &argv, NULL);
+
+       g_test_add ("/ssh-agent/process/connect", Test, NULL, setup, test_connect, teardown);
+       g_test_add ("/ssh-agent/process/list", Test, NULL, setup, test_list, teardown);
+       g_test_add ("/ssh-agent/process/add", Test, NULL, setup, test_add, teardown);
+       g_test_add ("/ssh-agent/process/remove", Test, NULL, setup, test_remove, teardown);
+       g_test_add ("/ssh-agent/process/remove_all", Test, NULL, setup, test_remove_all, teardown);
+       g_test_add ("/ssh-agent/process/sign", Test, NULL, setup, test_sign, teardown);
+       g_test_add ("/ssh-agent/process/restart", Test, NULL, setup, test_restart, teardown);
+
+       return g_test_run ();
+}
diff --git a/daemon/ssh-agent/test-gkd-ssh-agent-service.c b/daemon/ssh-agent/test-gkd-ssh-agent-service.c
new file mode 100644
index 0000000..d02d163
--- /dev/null
+++ b/daemon/ssh-agent/test-gkd-ssh-agent-service.c
@@ -0,0 +1,617 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daiki Ueno
+ */
+
+#include "config.h"
+
+#include "gkd-ssh-agent-service.h"
+#include "gkd-ssh-agent-private.h"
+#include "gkd-ssh-agent-util.h"
+#include "test-common.h"
+#include "egg/egg-testing.h"
+#include "egg/mock-interaction.h"
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gio/gunixsocketaddress.h>
+
+typedef struct {
+       gchar *directory;
+       EggBuffer req;
+       EggBuffer resp;
+       GkdSshAgentService *service;
+       GMainLoop *loop;
+       GSocketConnection *connection;
+       GThread *thread;
+       GMutex lock;
+       GCond cond;
+} Test;
+
+static gpointer
+server_thread (gpointer data)
+{
+       Test *test = data;
+       GMainContext *context;
+       gboolean ret;
+
+       context = g_main_context_new ();
+       test->loop = g_main_loop_new (context, FALSE);
+
+       g_main_context_push_thread_default (context);
+
+       ret = gkd_ssh_agent_service_start (test->service);
+       g_assert_true (ret);
+
+       g_mutex_lock (&test->lock);
+       g_cond_signal (&test->cond);
+       g_mutex_unlock (&test->lock);
+
+       g_main_loop_run (test->loop);
+
+       g_main_context_pop_thread_default (context);
+
+       g_main_context_unref (context);
+       g_main_loop_unref (test->loop);
+
+       return NULL;
+}
+
+static void
+connect_to_server (Test *test)
+{
+       const gchar *envvar;
+       GSocketClient *client;
+       GSocketAddress *address;
+       GError *error;
+
+       envvar = g_getenv ("SSH_AUTH_SOCK");
+       g_assert_nonnull (envvar);
+       address = g_unix_socket_address_new (envvar);
+
+       client = g_socket_client_new ();
+
+       error = NULL;
+       test->connection = g_socket_client_connect (client,
+                                                   G_SOCKET_CONNECTABLE (address),
+                                                   NULL,
+                                                   &error);
+       g_assert_nonnull (test->connection);
+       g_assert_no_error (error);
+
+       g_object_unref (address);
+       g_object_unref (client);
+}
+
+static void
+setup (Test *test, gconstpointer unused)
+{
+       GTlsInteraction *interaction;
+       GkdSshAgentPreload *preload;
+       gchar *sockets_path;
+       gchar *preload_path;
+       gchar *path;
+
+       test->directory = egg_tests_create_scratch_directory (NULL, NULL);
+
+       sockets_path = g_build_filename (test->directory, "sockets", NULL);
+       g_mkdir (sockets_path, 0700);
+
+       preload_path = g_build_filename (test->directory, "preload", NULL);
+       g_mkdir (preload_path, 0700);
+
+       egg_tests_copy_scratch_file (preload_path, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain");
+       egg_tests_copy_scratch_file (preload_path, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub");
+
+       path = g_build_filename (preload_path, "id_rsa_plain", NULL);
+       g_chmod (path, 0600);
+       g_free (path);
+
+       egg_buffer_init_full (&test->req, 128, (EggBufferAllocator)g_realloc);
+       egg_buffer_init_full (&test->resp, 128, (EggBufferAllocator)g_realloc);
+
+       interaction = mock_interaction_new ("password");
+       preload = gkd_ssh_agent_preload_new (preload_path);
+       g_free (preload_path);
+
+       test->service = gkd_ssh_agent_service_new (sockets_path, interaction, preload);
+       g_free (sockets_path);
+
+       g_object_unref (interaction);
+       g_object_unref (preload);
+
+       g_mutex_init (&test->lock);
+       g_cond_init (&test->cond);
+
+       test->thread = g_thread_new ("ssh-agent", server_thread, test);
+
+       /* Wait until the server is up */
+       g_mutex_lock (&test->lock);
+       g_cond_wait (&test->cond, &test->lock);
+       g_mutex_unlock (&test->lock);
+}
+
+static void
+teardown (Test *test, gconstpointer unused)
+{
+       g_main_loop_quit (test->loop);
+       g_thread_join (test->thread);
+
+       g_clear_object (&test->connection);
+
+       gkd_ssh_agent_service_stop (test->service);
+       g_object_unref (test->service);
+
+       egg_buffer_uninit (&test->req);
+       egg_buffer_uninit (&test->resp);
+
+       egg_tests_remove_scratch_directory (test->directory);
+       g_free (test->directory);
+
+       g_cond_clear (&test->cond);
+       g_mutex_clear (&test->lock);
+}
+
+static void
+call (Test *test)
+{
+       GError *error;
+       gboolean ret;
+
+       error = NULL;
+       ret = _gkd_ssh_agent_write_packet (test->connection, &test->req, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+
+       error = NULL;
+       ret = _gkd_ssh_agent_read_packet (test->connection, &test->resp, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+}
+
+static void
+call_error_or_failure (Test *test, gint dom, gint code)
+{
+       GError *error;
+       gboolean ret;
+
+       error = NULL;
+       ret = _gkd_ssh_agent_write_packet (test->connection, &test->req, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+
+       error = NULL;
+       ret = _gkd_ssh_agent_read_packet (test->connection, &test->resp, NULL, &error);
+       if (ret)
+               check_failure (&test->resp);
+       else {
+               g_assert_false (ret);
+               g_assert_error (error, dom, code);
+       }
+}
+
+DEFINE_CALL_FUNCS(Test, call)
+
+static void
+call_unparseable_add (Test *test)
+{
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       prepare_add_identity (&test->req);
+       egg_buffer_set_uint32 (&test->req, 5, 0x80000000);
+       call_error_or_failure (test, G_IO_ERROR, G_IO_ERROR_FAILED);
+}
+
+static void
+call_unparseable_remove (Test *test)
+{
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       prepare_remove_identity (&test->req);
+       egg_buffer_set_uint32 (&test->req, 5, 0x80000000);
+       call_error_or_failure (test, G_IO_ERROR, G_IO_ERROR_FAILED);
+}
+
+static void
+call_unparseable_sign (Test *test)
+{
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       prepare_sign_request (&test->req);
+       egg_buffer_set_uint32 (&test->req, 5, 0x80000000);
+       call_error_or_failure (test, G_IO_ERROR, G_IO_ERROR_FAILED);
+}
+
+static void
+prepare_sign_request_unknown (EggBuffer *req)
+{
+       GBytes *public_key;
+       gchar *comment;
+       gsize length;
+       const guchar *blob;
+       gboolean ret;
+
+       public_key = public_key_from_file (SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain.pub", &comment);
+       g_free (comment);
+       blob = g_bytes_get_data (public_key, &length);
+
+       egg_buffer_reset (req);
+       ret = egg_buffer_add_uint32 (req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (req, GKD_SSH_OP_SIGN_REQUEST);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte_array (req, blob, length);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_string (req, "data");
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_uint32 (req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
+       g_assert_true (ret);
+
+       g_bytes_unref (public_key);
+}
+
+static void
+call_sign_unknown (Test *test)
+{
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       prepare_sign_request_unknown (&test->req);
+       call (test);
+       check_failure (&test->resp);
+}
+
+static void
+call_empty (Test *test)
+{
+       GError *error;
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       ret = egg_buffer_add_uint32 (&test->req, 0);
+       g_assert_true (ret);
+
+       error = NULL;
+       ret = _gkd_ssh_agent_write_packet (test->connection, &test->req, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+
+       error = NULL;
+       ret = _gkd_ssh_agent_read_packet (test->connection, &test->resp, NULL, &error);
+       g_assert_false (ret);
+       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
+}
+
+static void
+call_unknown (Test *test)
+{
+       GError *error;
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       ret = egg_buffer_add_uint32 (&test->req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (&test->req, 255);
+       g_assert_true (ret);
+
+       error = NULL;
+       ret = _gkd_ssh_agent_write_packet (test->connection, &test->req, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+
+       error = NULL;
+       ret = _gkd_ssh_agent_read_packet (test->connection, &test->resp, NULL, &error);
+       g_assert_true (ret);
+       g_assert_no_error (error);
+
+       check_failure (&test->resp);
+}
+
+static void
+call_lock (Test *test)
+{
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       ret = egg_buffer_add_uint32 (&test->req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (&test->req, GKD_SSH_OP_LOCK);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_string (&test->req, "password");
+       g_assert_true (ret);
+
+       ret = egg_buffer_set_uint32 (&test->req, 0, test->req.len - 4);
+       g_assert_true (ret);
+
+       call (test);
+
+       check_success (&test->resp);
+}
+
+static void
+call_unlock (Test *test)
+{
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       ret = egg_buffer_add_uint32 (&test->req, 0);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_byte (&test->req, GKD_SSH_OP_UNLOCK);
+       g_assert_true (ret);
+
+       ret = egg_buffer_add_string (&test->req, "password");
+       g_assert_true (ret);
+
+       ret = egg_buffer_set_uint32 (&test->req, 0, test->req.len - 4);
+       g_assert_true (ret);
+
+       call (test);
+
+       check_success (&test->resp);
+}
+
+static void
+test_startup_shutdown (Test *test, gconstpointer unused)
+{
+}
+
+static void
+test_list (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_request_identities (test, 1);
+}
+
+static void
+test_add (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       /* Adding an identity from the preloaded location doesn't
+        * change the total number of keys returned from
+        * GKD_SSH_OP_REQUEST_IDENTITIES */
+       call_add_identity (test);
+       call_request_identities (test, 1);
+}
+
+static void
+test_unparseable_add (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_unparseable_add (test);
+}
+
+static void
+test_unparseable_remove (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_unparseable_remove (test); /* This closes the connection */
+}
+
+static void
+test_unparseable_sign (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_unparseable_sign (test); /* This closes the connection */
+}
+
+static void
+test_remove (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       /* Adding an identity from the preloaded location doesn't
+        * change the total number of keys returned from
+        * GKD_SSH_OP_REQUEST_IDENTITIES */
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       /* Removing an identity from the preloaded location doesn't
+        * change the total number of keys returned from
+        * GKD_SSH_OP_REQUEST_IDENTITIES */
+       call_remove_identity (test);
+       call_request_identities (test, 1);
+}
+
+static void
+test_remove_all (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       /* Adding an identity from the preloaded location doesn't
+        * change the total number of keys returned from
+        * GKD_SSH_OP_REQUEST_IDENTITIES */
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       /* Removing an identity from the preloaded location doesn't
+        * change the total number of keys returned from
+        * GKD_SSH_OP_REQUEST_IDENTITIES */
+       call_remove_all_identities (test);
+       call_request_identities (test, 1);
+}
+
+static void
+test_sign_loaded (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       /* Adding an identity from the preloaded location doesn't
+        * change the total number of keys returned from
+        * GKD_SSH_OP_REQUEST_IDENTITIES */
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       call_sign (test);
+}
+
+static void
+test_sign (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_sign (test);
+}
+
+static void
+test_sign_unknown (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_sign_unknown (test);
+}
+
+static gpointer
+kill_thread (gpointer data)
+{
+       Test *test = data;
+       GkdSshAgentProcess *process;
+       GPid pid;
+
+       process = gkd_ssh_agent_service_get_process (test->service);
+       pid = gkd_ssh_agent_process_get_pid (process);
+       g_assert_cmpint (-1, !=, pid);
+
+       kill (pid, SIGTERM);
+
+       return NULL;
+}
+
+static void
+on_closed (GkdSshAgentProcess *self, gpointer data)
+{
+       GMainLoop *loop = data;
+
+       g_main_loop_quit (loop);
+       g_main_loop_unref (loop);
+}
+
+static void
+test_restart (Test *test, gconstpointer unused)
+{
+       GkdSshAgentProcess *process;
+       GThread *thread;
+       GMainLoop *loop;
+       GBytes *public_key;
+       gchar *comment;
+
+       connect_to_server (test);
+
+       public_key = public_key_from_file (SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub", &comment);
+       g_free (comment);
+
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       g_assert_true (gkd_ssh_agent_service_lookup_key (test->service, public_key));
+
+       thread = g_thread_new ("kill", kill_thread, test);
+
+       loop = g_main_loop_new (NULL, FALSE);
+
+       process = gkd_ssh_agent_service_get_process (test->service);
+       g_signal_connect (process, "closed", G_CALLBACK (on_closed), loop);
+       g_main_loop_run (loop);
+
+       g_thread_join (thread);
+
+       g_assert_false (gkd_ssh_agent_service_lookup_key (test->service, public_key));
+
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       g_assert_true (gkd_ssh_agent_service_lookup_key (test->service, public_key));
+       g_bytes_unref (public_key);
+}
+
+static void
+test_empty (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_empty (test);
+}
+
+static void
+test_unknown (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_unknown (test);
+}
+
+static void
+test_lock (Test *test, gconstpointer unused)
+{
+       connect_to_server (test);
+
+       call_lock (test);
+       call_unlock (test);
+}
+
+int
+main (int argc, char **argv)
+{
+       g_test_init (&argc, &argv, NULL);
+
+       g_test_add ("/ssh-agent/service/startup_shutdown", Test, NULL, setup, test_startup_shutdown, 
teardown);
+       g_test_add ("/ssh-agent/service/list", Test, NULL, setup, test_list, teardown);
+       g_test_add ("/ssh-agent/service/add", Test, NULL, setup, test_add, teardown);
+       g_test_add ("/ssh-agent/service/remove", Test, NULL, setup, test_remove, teardown);
+       g_test_add ("/ssh-agent/service/remove_all", Test, NULL, setup, test_remove_all, teardown);
+       g_test_add ("/ssh-agent/service/sign_loaded", Test, NULL, setup, test_sign_loaded, teardown);
+       g_test_add ("/ssh-agent/service/sign", Test, NULL, setup, test_sign, teardown);
+       g_test_add ("/ssh-agent/service/sign_unknown", Test, NULL, setup, test_sign_unknown, teardown);
+       g_test_add ("/ssh-agent/service/empty", Test, NULL, setup, test_empty, teardown);
+       g_test_add ("/ssh-agent/service/unknown", Test, NULL, setup, test_unknown, teardown);
+       g_test_add ("/ssh-agent/service/unparseable_add", Test, NULL, setup, test_unparseable_add, teardown);
+       g_test_add ("/ssh-agent/service/unparseable_remove", Test, NULL, setup, test_unparseable_remove, 
teardown);
+       g_test_add ("/ssh-agent/service/unparseable_sign", Test, NULL, setup, test_unparseable_sign, 
teardown);
+       g_test_add ("/ssh-agent/service/restart", Test, NULL, setup, test_restart, teardown);
+       g_test_add ("/ssh-agent/service/lock", Test, NULL, setup, test_lock, teardown);
+
+       return g_test_run ();
+}
diff --git a/daemon/ssh-agent/test-gkd-ssh-agent-util.c b/daemon/ssh-agent/test-gkd-ssh-agent-util.c
new file mode 100644
index 0000000..89c10d2
--- /dev/null
+++ b/daemon/ssh-agent/test-gkd-ssh-agent-util.c
@@ -0,0 +1,84 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2008 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Stef Walter <stef thewalter net>
+ */
+
+#include "config.h"
+
+#include "gkd-ssh-agent-util.h"
+
+#include <glib.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+static struct {
+       const char *filename;
+       const char *encoded;
+} PUBLIC_FILES[] = {
+       { SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_test.pub",
+         
"AAAAB3NzaC1yc2EAAAABIwAAAQEAoD6VKqkhay6pKHSRjAGWWfFPU8xfsi2gnOwP/B1UHDoztx3czhO+py/fTlhCnSP1jsjkrVIZcnzah2fUNFFRgS4+jROBtvbgHsS72V1E6+ZogV+mBJWWAhw0iPrmQ3Kvm38D3PByo5Y7yKO5kIG2LloYLjosJ5F4sx2xh0uz2wXNtnY1b5xhe2+VEksm9OB+FXaUkZC2fQrTNo8ZGFJQSFd8kUhIfbUDJmlYuZ+vvHM+A3Lc9rHyW4IPaRyxFQciRmb+ZQqU2uSdOXAhg17lskuX/q8yCI5Hy5eDicC222oUMdJTtYgwX4dQCU8TICWhxb3x4RCV+g7D99+tkIvv+w=="
 },
+       { SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_test.pub",
+         
"AAAAB3NzaC1kc3MAAACBANHNmw2YHEodUj4Ae27i8Rm8uoLnpS68QEiCJx8bv9P1o0AaD0w55sH+TBzlo7vtAEDlAzIOBY3PMpy5WarELTIeXmFPzKfHL8tuxMbOPaN/wDkDZNnJZsqlyRwlQKStPcAlvLBNuMjA53u2ndMTVghtUHXETQzwxKhXf7TmvfLBAAAAFQDnF/Y8MgFCP0PpRC5ZAQo1dyDEwwAAAIEAr4iOpTeZx8i1QgQpRl+dmbBAtHTXbPiophzNJBge9lixqF0T3egN2B9wGGnumIXmnst9RPPjuu+cHCLfxhXHzLlW8MLwoiF6ZQOx9M8WcfWIl5oiGyr2e969woRf5OcMGQPOQBdws6MEtemRqq5gu6dqDqVl3xfhSZSP9LpqAI8AAACAUjiuQ3qGErsCz++qd0qrR++QA185XGXAPZqQEHcr4iKSlO17hSUYA03kOWtDaeRtJOlxjIjl9iLo3juKGFgxUfo2StScOSO2saTWFGjA4MybHCK1+mIYXRcYrq314yK2Tmbql/UGDWpcCCGXLWpSFHTaXTbJjPd6VL+TO9/8tFk="
 }
+};
+
+#define COMMENT "A public key comment"
+
+static void
+test_parse_public (void)
+{
+       GBytes *input_bytes, *output_bytes;
+       gchar *comment;
+       guchar *data;
+       const guchar *blob;
+       gsize n_data;
+       gchar *encoded;
+       gsize i;
+
+       for (i = 0; i < G_N_ELEMENTS (PUBLIC_FILES); ++i) {
+               if (!g_file_get_contents (PUBLIC_FILES[i].filename, (gchar **)&data, &n_data, NULL))
+                       g_assert_not_reached ();
+
+               input_bytes = g_bytes_new_take (data, n_data);
+               output_bytes = _gkd_ssh_agent_parse_public_key (input_bytes, &comment);
+               g_bytes_unref (input_bytes);
+               g_assert (output_bytes);
+
+               blob = g_bytes_get_data (output_bytes, &n_data);
+               encoded = g_base64_encode (blob, n_data);
+               g_bytes_unref (output_bytes);
+               g_assert_cmpstr (encoded, ==, PUBLIC_FILES[i].encoded);
+               g_free (encoded);
+
+               g_assert_cmpstr (comment, ==, COMMENT);
+               g_free (comment);
+       }
+}
+
+int
+main (int argc, char **argv)
+{
+       g_test_init (&argc, &argv, NULL);
+
+       g_test_add_func ("/ssh-agent/util/parse_public", test_parse_public);
+
+       return g_test_run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]