test-private-key.c 6.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-private-key.c: Test SSH Key Private key functionality

   Copyright (C) 2009 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
18
   <http://www.gnu.org/licenses/>.
19 20 21 22

   Author: Stef Walter <stef@memberwebs.com>
*/

23 24 25
#include "config.h"

#include "mock-ssh-module.h"
26

27 28 29
#include "gkm/gkm-credential.h"
#include "gkm/gkm-session.h"
#include "gkm/gkm-module.h"
30

31
#include "ssh-store/gkm-ssh-private-key.h"
32

33 34
#include "egg/egg-testing.h"

35
#include "pkcs11i.h"
36

37 38 39
typedef struct {
	GkmModule *module;
	GkmSession *session;
40 41 42
	GkmSshPrivateKey *rsakey;
	GkmSshPrivateKey *dsakey;
	GkmSshPrivateKey *ecdsakey;
43
} Test;
44

45
static void
46 47
setup_basic (Test *test,
             gconstpointer unused)
48
{
49 50
	test->module = test_ssh_module_initialize_and_enter ();
	test->session = test_ssh_module_open_session (TRUE);
51 52
}

53
static void
54 55
teardown_basic (Test *test,
                gconstpointer unused)
56 57 58 59
{
	test_ssh_module_leave_and_finalize ();
}

60 61 62 63 64 65 66 67
static void
setup (Test *test,
       gconstpointer unused)
{
	gboolean ret;

	setup_basic (test, unused);

68 69 70
	/* RSA */
	test->rsakey = gkm_ssh_private_key_new (test->module, "my-unique");
	g_assert (GKM_IS_SSH_PRIVATE_KEY (test->rsakey));
71

72 73
	ret = gkm_ssh_private_key_parse (test->rsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted.pub",
	                                 SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted", NULL);
74
	g_assert (ret == TRUE);
75 76 77 78 79 80 81 82 83 84 85 86 87 88

	/* DSA */
	test->dsakey = gkm_ssh_private_key_new (test->module, "my-unique");
	g_assert (GKM_IS_SSH_PRIVATE_KEY (test->dsakey));

	ret = gkm_ssh_private_key_parse (test->dsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted.pub",
	                                 SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted", NULL);

	/* ECDSA */
	test->ecdsakey = gkm_ssh_private_key_new (test->module, "my-unique");
	g_assert (GKM_IS_SSH_PRIVATE_KEY (test->ecdsakey));

	ret = gkm_ssh_private_key_parse (test->ecdsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted.pub",
	                                 SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted", NULL);
89 90 91 92 93 94
}

static void
teardown (Test *test,
          gconstpointer unused)
{
95 96 97
	g_object_unref (test->rsakey);
	g_object_unref (test->dsakey);
	g_object_unref (test->ecdsakey);
98 99 100
	teardown_basic (test, unused);
}

101
static void
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
test_parse_plain_rsa (Test *test, gconstpointer unused)
{
	GkmSshPrivateKey *key;
	gboolean ret;

	key = gkm_ssh_private_key_new (test->module, "my-unique");
	g_assert (GKM_IS_SSH_PRIVATE_KEY (key));

	ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub",
	                                 SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain", NULL);
	g_assert (ret == TRUE);

	g_object_unref (key);
}

static void
test_parse_plain_dsa (Test *test, gconstpointer unused)
119
{
120
	GkmSshPrivateKey *key;
121
	gboolean ret;
122

123
	key = gkm_ssh_private_key_new (test->module, "my-unique");
124
	g_assert (GKM_IS_SSH_PRIVATE_KEY (key));
125

126 127
	ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain.pub",
	                                 SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain", NULL);
128
	g_assert (ret == TRUE);
129

130 131 132
	g_object_unref (key);
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static void
test_parse_plain_ecdsa (Test *test, gconstpointer unused)
{
	GkmSshPrivateKey *key;
	gboolean ret;

	key = gkm_ssh_private_key_new (test->module, "my-unique");
	g_assert (GKM_IS_SSH_PRIVATE_KEY (key));

	ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain.pub",
	                                 SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain", NULL);
	g_assert (ret == TRUE);

	g_object_unref (key);
}

149
static void
150 151
test_unlock (Test *test,
             gconstpointer unused)
152
{
153
	GkmCredential *cred;
154
	CK_RV rv;
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	/* RSA */
	rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->rsakey),
	                            (guchar*)"password", 8, &cred);
	g_assert (rv == CKR_OK);

	g_object_unref (cred);

	/* DSA */
	rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->dsakey),
	                            (guchar*)"password", 8, &cred);
	g_assert (rv == CKR_OK);

	g_object_unref (cred);

	/* ECDSA */
	rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->ecdsakey),
172
	                            (guchar*)"password", 8, &cred);
173
	g_assert (rv == CKR_OK);
174 175

	g_object_unref (cred);
176 177 178 179 180 181 182 183 184
}

static void
test_internal_sha1_compat (Test *test,
                           gconstpointer unused)
{
	gpointer data;
	gsize n_data;

185
	data = gkm_object_get_attribute_data (GKM_OBJECT (test->dsakey), test->session,
186 187 188 189
	                                      CKA_GNOME_INTERNAL_SHA1, &n_data);

	egg_assert_cmpmem (data, n_data, ==, "\x33\x37\x31\x31\x64\x33\x33\x65\x61\x34\x31\x31\x33\x61\x35\x64\x32\x35\x38\x37\x63\x36\x66\x32\x35\x66\x39\x35\x35\x36\x39\x66\x65\x65\x38\x31\x38\x35\x39\x34", 40);
	g_free (data);
190
}
191 192 193 194

int
main (int argc, char **argv)
{
195
#if !GLIB_CHECK_VERSION(2,35,0)
196
	g_type_init ();
197
#endif
198 199
	g_test_init (&argc, &argv, NULL);

200 201 202
	g_test_add ("/ssh-store/private-key/parse_plain_rsa", Test, NULL, setup_basic, test_parse_plain_rsa, teardown_basic);
	g_test_add ("/ssh-store/private-key/parse_plain_dsa", Test, NULL, setup_basic, test_parse_plain_dsa, teardown_basic);
	g_test_add ("/ssh-store/private-key/parse_plain_ecdsa", Test, NULL, setup_basic, test_parse_plain_ecdsa, teardown_basic);
203 204
	g_test_add ("/ssh-store/private-key/unlock", Test, NULL, setup, test_unlock, teardown);
	g_test_add ("/ssh-store/private-key/internal-sha1-compat", Test, NULL, setup, test_internal_sha1_compat, teardown);
205 206 207

	return g_test_run ();
}