Debian Codegraph @

pkcs11/ssh-store/test-private-key.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-private-key.c: Test SSH Key Private key functionality
3
4 Copyright (C) 2009 Stefan Walter
5
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "mock-ssh-module.h"
26
27 #include "gkm/gkm-credential.h"
28 #include "gkm/gkm-session.h"
29 #include "gkm/gkm-module.h"
30
31 #include "ssh-store/gkm-ssh-private-key.h"
32
33 #include "egg/egg-testing.h"
34
35 #include "pkcs11i.h"
36
37 typedef struct {
38 GkmModule *module;
39 GkmSession *session;
40 GkmSshPrivateKey *rsakey;
41 GkmSshPrivateKey *dsakey;
42 GkmSshPrivateKey *ecdsakey;
43 } Test;
44
45 static void
46 setup_basic (Test *test,
47 gconstpointer unused)
48 {
49 test->module = test_ssh_module_initialize_and_enter ();
50 test->session = test_ssh_module_open_session (TRUE);
51 }
52
53 static void
54 teardown_basic (Test *test,
55 gconstpointer unused)
56 {
57 test_ssh_module_leave_and_finalize ();
58 }
59
60 static void
61 setup (Test *test,
62 gconstpointer unused)
63 {
64 gboolean ret;
65
66 setup_basic (test, unused);
67
68 /* RSA */
69 test->rsakey = gkm_ssh_private_key_new (test->module, "my-unique");
70 g_assert (GKM_IS_SSH_PRIVATE_KEY (test->rsakey));
71
72 ret = gkm_ssh_private_key_parse (test->rsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted.pub",
73 SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted", NULL);
74 g_assert (ret == TRUE);
75
76 /* DSA */
77 test->dsakey = gkm_ssh_private_key_new (test->module, "my-unique");
78 g_assert (GKM_IS_SSH_PRIVATE_KEY (test->dsakey));
79
80 ret = gkm_ssh_private_key_parse (test->dsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted.pub",
81 SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted", NULL);
82
83 /* ECDSA */
84 test->ecdsakey = gkm_ssh_private_key_new (test->module, "my-unique");
85 g_assert (GKM_IS_SSH_PRIVATE_KEY (test->ecdsakey));
86
87 ret = gkm_ssh_private_key_parse (test->ecdsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted.pub",
88 SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted", NULL);
89 }
90
91 static void
92 teardown (Test *test,
93 gconstpointer unused)
94 {
95 g_object_unref (test->rsakey);
96 g_object_unref (test->dsakey);
97 g_object_unref (test->ecdsakey);
98 teardown_basic (test, unused);
99 }
100
101 static void
102 test_parse_plain_rsa (Test *test, gconstpointer unused)
103 {
104 GkmSshPrivateKey *key;
105 gboolean ret;
106
107 key = gkm_ssh_private_key_new (test->module, "my-unique");
108 g_assert (GKM_IS_SSH_PRIVATE_KEY (key));
109
110 ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub",
111 SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain", NULL);
112 g_assert (ret == TRUE);
113
114 g_object_unref (key);
115 }
116
117 static void
118 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 ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain.pub",
127 SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain", NULL);
128 g_assert (ret == TRUE);
129
130 g_object_unref (key);
131 }
132
133 static void
134 test_parse_plain_ecdsa (Test *test, gconstpointer unused)
135 {
136 GkmSshPrivateKey *key;
137 gboolean ret;
138
139 key = gkm_ssh_private_key_new (test->module, "my-unique");
140 g_assert (GKM_IS_SSH_PRIVATE_KEY (key));
141
142 ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain.pub",
143 SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain", NULL);
144 g_assert (ret == TRUE);
145
146 g_object_unref (key);
147 }
148
149 static void
150 test_unlock (Test *test,
151 gconstpointer unused)
152 {
153 GkmCredential *cred;
154 CK_RV rv;
155
156 /* RSA */
157 rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->rsakey),
158 (guchar*)"password", 8, &cred);
159 g_assert (rv == CKR_OK);
160
161 g_object_unref (cred);
162
163 /* DSA */
164 rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->dsakey),
165 (guchar*)"password", 8, &cred);
166 g_assert (rv == CKR_OK);
167
168 g_object_unref (cred);
169
170 /* ECDSA */
171 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 static void
179 test_internal_sha1_compat (Test *test,
180 gconstpointer unused)
181 {
182 gpointer data;
183 gsize n_data;
184
185 data = gkm_object_get_attribute_data (GKM_OBJECT (test->dsakey), test->session,
186 CKA_GNOME_INTERNAL_SHA1, &n_data);
187
188 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);
189 g_free (data);
190 }
191
192 int
193 main (int argc, char **argv)
194 {
195 #if !GLIB_CHECK_VERSION(2,35,0)
196 g_type_init ();
197 #endif
198 g_test_init (&argc, &argv, NULL);
199
200 g_test_add ("/ssh-store/private-key/parse_plain_rsa", Test, NULL, setup_basic, test_parse_plain_rsa, teardown_basic);
201 g_test_add ("/ssh-store/private-key/parse_plain_dsa", Test, NULL, setup_basic, test_parse_plain_dsa, teardown_basic);
202 g_test_add ("/ssh-store/private-key/parse_plain_ecdsa", Test, NULL, setup_basic, test_parse_plain_ecdsa, teardown_basic);
203 g_test_add ("/ssh-store/private-key/unlock", Test, NULL, setup, test_unlock, teardown);
204 g_test_add ("/ssh-store/private-key/internal-sha1-compat", Test, NULL, setup, test_internal_sha1_compat, teardown);
205
206 return g_test_run ();
207 }