Branch data Line data Source code
1 : : /*
2 : : * gnome-keyring
3 : : *
4 : : * Copyright (C) 2018 Red Hat, Inc.
5 : : *
6 : : * This program is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU Lesser General Public License as
8 : : * published by the Free Software Foundation; either version 2.1 of
9 : : * the License, or (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful, but
12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this program; if not, see
18 : : * <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Daiki Ueno
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include "gcr-ssh-agent-test.h"
26 : :
27 : : #include "gcr-ssh-agent-private.h"
28 : : #include "gcr-ssh-agent-util.h"
29 : :
30 : : /* RSA private key blob decoded from fixtures/ssh-agent/id_rsa_plain */
31 : : static const guint8 private_blob[4*6 + 0x101 + 0x1 + 0x101 + 0x80 + 0x81 + 0x81] = {
32 : : /* n */
33 : : 0x00, 0x00, 0x01, 0x01, 0x00, 0xa0, 0x3e, 0x95, 0x2a, 0xa9, 0x21, 0x6b,
34 : : 0x2e, 0xa9, 0x28, 0x74, 0x91, 0x8c, 0x01, 0x96, 0x59, 0xf1, 0x4f, 0x53,
35 : : 0xcc, 0x5f, 0xb2, 0x2d, 0xa0, 0x9c, 0xec, 0x0f, 0xfc, 0x1d, 0x54, 0x1c,
36 : : 0x3a, 0x33, 0xb7, 0x1d, 0xdc, 0xce, 0x13, 0xbe, 0xa7, 0x2f, 0xdf, 0x4e,
37 : : 0x58, 0x42, 0x9d, 0x23, 0xf5, 0x8e, 0xc8, 0xe4, 0xad, 0x52, 0x19, 0x72,
38 : : 0x7c, 0xda, 0x87, 0x67, 0xd4, 0x34, 0x51, 0x51, 0x81, 0x2e, 0x3e, 0x8d,
39 : : 0x13, 0x81, 0xb6, 0xf6, 0xe0, 0x1e, 0xc4, 0xbb, 0xd9, 0x5d, 0x44, 0xeb,
40 : : 0xe6, 0x68, 0x81, 0x5f, 0xa6, 0x04, 0x95, 0x96, 0x02, 0x1c, 0x34, 0x88,
41 : : 0xfa, 0xe6, 0x43, 0x72, 0xaf, 0x9b, 0x7f, 0x03, 0xdc, 0xf0, 0x72, 0xa3,
42 : : 0x96, 0x3b, 0xc8, 0xa3, 0xb9, 0x90, 0x81, 0xb6, 0x2e, 0x5a, 0x18, 0x2e,
43 : : 0x3a, 0x2c, 0x27, 0x91, 0x78, 0xb3, 0x1d, 0xb1, 0x87, 0x4b, 0xb3, 0xdb,
44 : : 0x05, 0xcd, 0xb6, 0x76, 0x35, 0x6f, 0x9c, 0x61, 0x7b, 0x6f, 0x95, 0x12,
45 : : 0x4b, 0x26, 0xf4, 0xe0, 0x7e, 0x15, 0x76, 0x94, 0x91, 0x90, 0xb6, 0x7d,
46 : : 0x0a, 0xd3, 0x36, 0x8f, 0x19, 0x18, 0x52, 0x50, 0x48, 0x57, 0x7c, 0x91,
47 : : 0x48, 0x48, 0x7d, 0xb5, 0x03, 0x26, 0x69, 0x58, 0xb9, 0x9f, 0xaf, 0xbc,
48 : : 0x73, 0x3e, 0x03, 0x72, 0xdc, 0xf6, 0xb1, 0xf2, 0x5b, 0x82, 0x0f, 0x69,
49 : : 0x1c, 0xb1, 0x15, 0x07, 0x22, 0x46, 0x66, 0xfe, 0x65, 0x0a, 0x94, 0xda,
50 : : 0xe4, 0x9d, 0x39, 0x70, 0x21, 0x83, 0x5e, 0xe5, 0xb2, 0x4b, 0x97, 0xfe,
51 : : 0xaf, 0x32, 0x08, 0x8e, 0x47, 0xcb, 0x97, 0x83, 0x89, 0xc0, 0xb6, 0xdb,
52 : : 0x6a, 0x14, 0x31, 0xd2, 0x53, 0xb5, 0x88, 0x30, 0x5f, 0x87, 0x50, 0x09,
53 : : 0x4f, 0x13, 0x20, 0x25, 0xa1, 0xc5, 0xbd, 0xf1, 0xe1, 0x10, 0x95, 0xfa,
54 : : 0x0e, 0xc3, 0xf7, 0xdf, 0xad, 0x90, 0x8b, 0xef, 0xfb,
55 : : /* e */
56 : : 0x00, 0x00, 0x00, 0x01, 0x23,
57 : : /* d */
58 : : 0x00, 0x00, 0x01, 0x01, 0x00, 0x9b, 0xaa, 0x82, 0x46, 0xb2, 0xed, 0x43,
59 : : 0x8c, 0x69, 0xcf, 0x87, 0x2e, 0x4d, 0x7d, 0xe2, 0x83, 0x42, 0x2f, 0xcd,
60 : : 0xbf, 0x38, 0x63, 0xf1, 0xcf, 0x39, 0x5a, 0x58, 0xab, 0xc4, 0xb8, 0x1b,
61 : : 0x6b, 0xbd, 0x35, 0x8a, 0xb9, 0x3d, 0x37, 0xc0, 0x85, 0x27, 0x30, 0xb2,
62 : : 0x81, 0x9f, 0xcb, 0xd9, 0xc9, 0xf8, 0x6b, 0x61, 0xcc, 0xf0, 0xab, 0x01,
63 : : 0x80, 0x99, 0xc5, 0x5d, 0x8c, 0x50, 0x14, 0x7b, 0x0f, 0xc6, 0x85, 0xe8,
64 : : 0x21, 0x93, 0xf3, 0x90, 0xbc, 0x75, 0xa9, 0x2b, 0x82, 0xb2, 0x60, 0x35,
65 : : 0x9d, 0xff, 0x1e, 0x97, 0x6e, 0x13, 0x14, 0xf8, 0x1f, 0x4e, 0x99, 0x6f,
66 : : 0x1f, 0x9d, 0xdb, 0x1e, 0xf3, 0xbb, 0x9f, 0xf5, 0x1f, 0xc5, 0x01, 0xa6,
67 : : 0x3a, 0x2b, 0x72, 0x73, 0x29, 0x4a, 0x8c, 0xa2, 0x58, 0xe9, 0xce, 0x58,
68 : : 0xca, 0xcb, 0xce, 0xaa, 0x92, 0x82, 0x1c, 0xd8, 0x57, 0x8b, 0x5e, 0x42,
69 : : 0x79, 0x21, 0x0e, 0x63, 0x13, 0x0e, 0x03, 0xff, 0x2f, 0x7f, 0x64, 0xf6,
70 : : 0x82, 0xe1, 0xfe, 0x0b, 0xc3, 0x1e, 0x4c, 0x50, 0x11, 0x3f, 0xc8, 0x8a,
71 : : 0xba, 0xcc, 0xde, 0x24, 0xf7, 0xae, 0x96, 0x6c, 0x5e, 0x3b, 0x00, 0xfa,
72 : : 0xf0, 0x0e, 0xac, 0x3a, 0xeb, 0xb1, 0xab, 0x8f, 0x3f, 0xdb, 0x80, 0xb3,
73 : : 0x06, 0x91, 0x18, 0xe1, 0xf5, 0x3b, 0xec, 0x5d, 0x01, 0xcf, 0xd0, 0x1f,
74 : : 0xaf, 0xe3, 0xd9, 0x12, 0xba, 0x7b, 0x0f, 0xee, 0x20, 0x29, 0x74, 0x57,
75 : : 0xdc, 0x58, 0x75, 0xd4, 0xb0, 0xf4, 0xb4, 0xa4, 0x93, 0x48, 0x2b, 0x7b,
76 : : 0x6b, 0x1d, 0x77, 0xbc, 0xf3, 0xfe, 0xbd, 0xad, 0xd6, 0x83, 0x05, 0x16,
77 : : 0xca, 0xbe, 0x31, 0xa4, 0x39, 0x53, 0x29, 0xf3, 0xd3, 0x39, 0xb0, 0xa5,
78 : : 0xef, 0xf0, 0xc9, 0x08, 0xd6, 0x63, 0x52, 0x0b, 0xcb, 0xfc, 0x1c, 0x21,
79 : : 0xd3, 0xa9, 0x2f, 0x23, 0x92, 0x3d, 0x46, 0x8c, 0x4b,
80 : : /* iqmp */
81 : : 0x00, 0x00, 0x00, 0x80, 0x15, 0x40, 0xcc, 0xa4, 0x83, 0xdf, 0x26, 0xbe,
82 : : 0x55, 0x82, 0x85, 0x0f, 0x71, 0x3c, 0x19, 0xa8, 0x8b, 0x42, 0x80, 0xa5,
83 : : 0x24, 0x5d, 0xad, 0xf5, 0x99, 0x33, 0xaf, 0x7c, 0xb2, 0x27, 0xae, 0x7b,
84 : : 0x0b, 0x0b, 0xa0, 0x03, 0xfd, 0xae, 0x53, 0x6f, 0xf1, 0xdd, 0x83, 0x54,
85 : : 0xde, 0xf2, 0xbd, 0x87, 0x2c, 0xa9, 0x4d, 0x7b, 0xa5, 0x6e, 0xdb, 0x5e,
86 : : 0x89, 0xf4, 0x5c, 0x79, 0x22, 0xc3, 0xc4, 0x40, 0x50, 0xeb, 0xb7, 0xf4,
87 : : 0x17, 0x78, 0x2f, 0x06, 0xa5, 0x3a, 0x65, 0x4d, 0x85, 0x98, 0x3e, 0xd8,
88 : : 0x4d, 0x3b, 0xfc, 0xd8, 0x9b, 0xe5, 0xd1, 0x47, 0xb6, 0xe3, 0xda, 0x2e,
89 : : 0xc5, 0x18, 0xce, 0x37, 0xd9, 0xd7, 0x9a, 0xbf, 0xba, 0xa9, 0xef, 0xf2,
90 : : 0xaf, 0x9b, 0xc8, 0x46, 0x57, 0x11, 0x8c, 0xa9, 0x5f, 0x68, 0x8c, 0x43,
91 : : 0x2f, 0xb5, 0x7a, 0x39, 0x38, 0x30, 0x79, 0xd5, 0x30, 0xa8, 0x2b, 0x98,
92 : : /* p */
93 : : 0x00, 0x00, 0x00, 0x81, 0x00, 0xcc, 0x50, 0xb1, 0x2c, 0x5f, 0xe4, 0x02,
94 : : 0x85, 0x7d, 0xce, 0x77, 0xd8, 0x27, 0xc1, 0xf6, 0xee, 0xe2, 0x2b, 0x7b,
95 : : 0x29, 0x83, 0x95, 0xf1, 0x5e, 0x3d, 0xe5, 0xa9, 0x75, 0x62, 0xc6, 0x84,
96 : : 0xc9, 0x97, 0x26, 0x70, 0xf4, 0x0d, 0x28, 0x6a, 0xc6, 0x88, 0x7c, 0xa3,
97 : : 0x0d, 0x35, 0xa3, 0x8f, 0xdc, 0x34, 0x4c, 0x78, 0x6b, 0xcc, 0x5d, 0x99,
98 : : 0x7e, 0x45, 0xb0, 0xdf, 0xe3, 0x77, 0x48, 0x77, 0xd8, 0xa9, 0x1c, 0x74,
99 : : 0xf9, 0xbc, 0xcc, 0x82, 0xdb, 0x44, 0x10, 0x96, 0xda, 0x00, 0x23, 0xaa,
100 : : 0x04, 0x93, 0xcc, 0x98, 0xec, 0x26, 0x8b, 0x7d, 0x08, 0xf4, 0x82, 0xdc,
101 : : 0x9a, 0xc4, 0x8c, 0xc8, 0xe9, 0x3e, 0x5b, 0xd6, 0xc7, 0x28, 0xf4, 0x38,
102 : : 0x3a, 0x3c, 0x08, 0x56, 0xbb, 0xa2, 0xca, 0xfb, 0x05, 0xa0, 0xb7, 0xe1,
103 : : 0x70, 0x59, 0xb4, 0x86, 0x2b, 0x29, 0x89, 0xb5, 0x82, 0x2a, 0x79, 0x61,
104 : : 0x51,
105 : : /* q */
106 : : 0x00, 0x00, 0x00, 0x81, 0x00, 0xc8, 0xc7, 0xe6, 0x93, 0x90, 0x59, 0xe7,
107 : : 0x54, 0x1b, 0xcf, 0x9c, 0xb0, 0x07, 0x80, 0x37, 0xcd, 0xdf, 0x65, 0xf4,
108 : : 0x29, 0x1e, 0x4a, 0x93, 0x73, 0xd1, 0x7b, 0x47, 0x1d, 0x36, 0x87, 0x89,
109 : : 0x1d, 0xbf, 0xd5, 0x1e, 0x02, 0xc2, 0xd1, 0x2b, 0xb3, 0x67, 0x07, 0x65,
110 : : 0xf9, 0xbc, 0xcb, 0x74, 0x4c, 0x83, 0x68, 0xa8, 0x6d, 0x30, 0x68, 0x8f,
111 : : 0xb5, 0xb9, 0x44, 0x86, 0xb8, 0xde, 0x4e, 0xfc, 0x02, 0x1e, 0x9c, 0x05,
112 : : 0x3b, 0x23, 0x1b, 0xdf, 0x79, 0x58, 0x73, 0x51, 0x27, 0xf0, 0xbd, 0x83,
113 : : 0x34, 0x38, 0xcb, 0xd0, 0x20, 0x12, 0xcd, 0x1a, 0x07, 0x6e, 0xf7, 0x0a,
114 : : 0x92, 0x29, 0xff, 0x2f, 0xbf, 0x30, 0x2a, 0x69, 0x15, 0x4d, 0x8e, 0x6e,
115 : : 0x17, 0x26, 0x7b, 0x43, 0xfe, 0x52, 0xd1, 0x83, 0x65, 0x19, 0x22, 0x8b,
116 : : 0xd3, 0x6f, 0x97, 0x51, 0x11, 0x3f, 0x17, 0xfe, 0x05, 0xcc, 0xa4, 0x49,
117 : : 0x8b
118 : : };
119 : :
120 : : void
121 : 17 : prepare_request_identities (EggBuffer *req)
122 : : {
123 : : gboolean ret;
124 : :
125 : 17 : egg_buffer_reset (req);
126 : :
127 : 17 : ret = egg_buffer_add_uint32 (req, 1);
128 [ - + ]: 17 : g_assert_true (ret);
129 : :
130 : 17 : ret = egg_buffer_add_byte (req, GCR_SSH_OP_REQUEST_IDENTITIES);
131 [ - + ]: 17 : g_assert_true (ret);
132 : 17 : }
133 : :
134 : : void
135 : 17 : check_identities_answer (EggBuffer *resp, gsize count)
136 : : {
137 : : uint32_t length;
138 : : unsigned char code;
139 : : size_t offset;
140 : : gboolean ret;
141 : :
142 : 17 : offset = 0;
143 : 17 : ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
144 [ - + ]: 17 : g_assert_true (ret);
145 [ - + ]: 17 : g_assert_cmpint (length, ==, resp->len - 4);
146 : :
147 : 17 : code = 0;
148 : 17 : ret = egg_buffer_get_byte (resp, offset, &offset, &code);
149 [ - + ]: 17 : g_assert_true (ret);
150 [ - + ]: 17 : g_assert_cmpint (code, ==, GCR_SSH_RES_IDENTITIES_ANSWER);
151 : :
152 : 17 : ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
153 [ - + ]: 17 : g_assert_true (ret);
154 [ - + ]: 17 : g_assert_cmpint (length, ==, count);
155 : 17 : }
156 : :
157 : : void
158 : 11 : prepare_add_identity (EggBuffer *req)
159 : : {
160 : : gboolean ret;
161 : :
162 : 11 : egg_buffer_reset (req);
163 : :
164 : 11 : ret = egg_buffer_add_uint32 (req, 0);
165 [ - + ]: 11 : g_assert_true (ret);
166 : :
167 : 11 : ret = egg_buffer_add_byte (req, GCR_SSH_OP_ADD_IDENTITY);
168 [ - + ]: 11 : g_assert_true (ret);
169 : :
170 : 11 : ret = egg_buffer_add_string (req, "ssh-rsa");
171 [ - + ]: 11 : g_assert_true (ret);
172 : :
173 : 11 : ret = egg_buffer_append (req, private_blob, G_N_ELEMENTS(private_blob));
174 [ - + ]: 11 : g_assert_true (ret);
175 : :
176 : 11 : ret = egg_buffer_add_string (req, "comment");
177 [ - + ]: 11 : g_assert_true (ret);
178 : :
179 : 11 : ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
180 [ - + ]: 11 : g_assert_true (ret);
181 : 11 : }
182 : :
183 : : GBytes *
184 : 9 : public_key_from_file (const gchar *path, gchar **comment)
185 : : {
186 : : GBytes *public_bytes;
187 : : GBytes *public_key;
188 : :
189 : 9 : GError *error = NULL;
190 : : gchar *contents;
191 : : gsize length;
192 : :
193 [ - + ]: 9 : if (!g_file_get_contents (path, &contents, &length, &error)) {
194 : 0 : g_message ("couldn't read file: %s: %s", path, error->message);
195 : 0 : g_error_free (error);
196 : 0 : return NULL;
197 : : }
198 : :
199 : 9 : public_bytes = g_bytes_new_take (contents, length);
200 : 9 : public_key = _gcr_ssh_agent_parse_public_key (public_bytes, comment);
201 : 9 : g_bytes_unref (public_bytes);
202 : :
203 : 9 : return public_key;
204 : : }
205 : :
206 : : void
207 : 3 : prepare_remove_identity (EggBuffer *req)
208 : : {
209 : : GBytes *public_key;
210 : : gchar *comment;
211 : : gsize length;
212 : : const guchar *blob;
213 : : gboolean ret;
214 : :
215 : 3 : public_key = public_key_from_file (SRCDIR "/gcr/fixtures/ssh-agent/id_rsa_plain.pub", &comment);
216 : 3 : g_free (comment);
217 : 3 : blob = g_bytes_get_data (public_key, &length);
218 : :
219 : 3 : egg_buffer_reset (req);
220 : 3 : ret = egg_buffer_add_uint32 (req, 0);
221 [ - + ]: 3 : g_assert_true (ret);
222 : :
223 : 3 : ret = egg_buffer_add_byte (req, GCR_SSH_OP_REMOVE_IDENTITY);
224 [ - + ]: 3 : g_assert_true (ret);
225 : :
226 : 3 : ret = egg_buffer_add_byte_array (req, blob, length);
227 [ - + ]: 3 : g_assert_true (ret);
228 : :
229 : 3 : ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
230 [ - + ]: 3 : g_assert_true (ret);
231 : :
232 : 3 : g_bytes_unref (public_key);
233 : 3 : }
234 : :
235 : : void
236 : 3 : prepare_remove_all_identities (EggBuffer *req)
237 : : {
238 : : gboolean ret;
239 : :
240 : 3 : egg_buffer_reset (req);
241 : 3 : ret = egg_buffer_add_uint32 (req, 1);
242 [ - + ]: 3 : g_assert_true (ret);
243 : :
244 : 3 : ret = egg_buffer_add_byte (req, GCR_SSH_OP_REMOVE_ALL_IDENTITIES);
245 [ - + ]: 3 : g_assert_true (ret);
246 : 3 : }
247 : :
248 : : void
249 : 22 : check_response (EggBuffer *resp, unsigned char expected)
250 : : {
251 : : uint32_t length;
252 : : unsigned char code;
253 : : size_t offset;
254 : : gboolean ret;
255 : :
256 : 22 : offset = 0;
257 : 22 : ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
258 [ - + ]: 22 : g_assert_true (ret);
259 [ - + ]: 22 : g_assert_cmpint (length, ==, resp->len - 4);
260 : :
261 : 22 : code = 0;
262 : 22 : ret = egg_buffer_get_byte (resp, offset, &offset, &code);
263 [ - + ]: 22 : g_assert_true (ret);
264 [ - + ]: 22 : g_assert_cmpint (expected, ==, code);
265 : 22 : }
266 : :
267 : : void
268 : 17 : check_success (EggBuffer *resp)
269 : : {
270 : 17 : check_response (resp, GCR_SSH_RES_SUCCESS);
271 : 17 : }
272 : :
273 : : void
274 : 5 : check_failure (EggBuffer *resp)
275 : : {
276 : 5 : check_response (resp, GCR_SSH_RES_FAILURE);
277 : 5 : }
278 : :
279 : : void
280 : 4 : prepare_sign_request (EggBuffer *req)
281 : : {
282 : : GBytes *public_key;
283 : : gchar *comment;
284 : : gsize length;
285 : : const guchar *blob;
286 : : gboolean ret;
287 : :
288 : 4 : public_key = public_key_from_file (SRCDIR "/gcr/fixtures/ssh-agent/id_rsa_plain.pub", &comment);
289 : 4 : g_free (comment);
290 : 4 : blob = g_bytes_get_data (public_key, &length);
291 : :
292 : 4 : egg_buffer_reset (req);
293 : 4 : ret = egg_buffer_add_uint32 (req, 0);
294 [ - + ]: 4 : g_assert_true (ret);
295 : :
296 : 4 : ret = egg_buffer_add_byte (req, GCR_SSH_OP_SIGN_REQUEST);
297 [ - + ]: 4 : g_assert_true (ret);
298 : :
299 : 4 : ret = egg_buffer_add_byte_array (req, blob, length);
300 [ - + ]: 4 : g_assert_true (ret);
301 : :
302 : 4 : ret = egg_buffer_add_string (req, "data");
303 [ - + ]: 4 : g_assert_true (ret);
304 : :
305 : 4 : ret = egg_buffer_add_uint32 (req, 0);
306 [ - + ]: 4 : g_assert_true (ret);
307 : :
308 : 4 : ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
309 [ - + ]: 4 : g_assert_true (ret);
310 : :
311 : 4 : g_bytes_unref (public_key);
312 : 4 : }
313 : :
314 : : void
315 : 3 : check_sign_response (EggBuffer *resp)
316 : : {
317 : : uint32_t length;
318 : : unsigned char code;
319 : : size_t offset;
320 : : gboolean ret;
321 : :
322 : 3 : offset = 0;
323 : 3 : ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
324 [ - + ]: 3 : g_assert_true (ret);
325 [ - + ]: 3 : g_assert_cmpint (length, ==, resp->len - 4);
326 : :
327 : 3 : code = 0;
328 : 3 : ret = egg_buffer_get_byte (resp, offset, &offset, &code);
329 [ - + ]: 3 : g_assert_true (ret);
330 [ - + ]: 3 : g_assert_cmpint (code, ==, GCR_SSH_RES_SIGN_RESPONSE);
331 : :
332 : 3 : ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
333 [ - + ]: 3 : g_assert_true (ret);
334 : 3 : }
|