1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Copyright (C) 2008-2009 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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 General Public License
* for more details.
*/
#include "agent_private_key.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <errno.h>
#include <library.h>
#include <chunk.h>
#include <debug.h>
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif /* UNIX_PATH_MAX */
typedef struct private_agent_private_key_t private_agent_private_key_t;
typedef enum agent_msg_type_t agent_msg_type_t;
/**
* Private data of a agent_private_key_t object.
*/
struct private_agent_private_key_t {
/**
* Public interface for this signer.
*/
agent_private_key_t public;
/**
* ssh-agent unix socket connection
*/
int socket;
/**
* key identity blob in ssh format
*/
chunk_t key;
/**
* keysize in bytes
*/
size_t key_size;
/**
* reference count
*/
refcount_t ref;
};
/**
* Message types for ssh-agent protocol
*/
enum agent_msg_type_t {
SSH_AGENT_FAILURE = 5,
SSH_AGENT_SUCCESS = 6,
SSH_AGENT_ID_REQUEST = 11,
SSH_AGENT_ID_RESPONSE = 12,
SSH_AGENT_SIGN_REQUEST = 13,
SSH_AGENT_SIGN_RESPONSE = 14,
};
/**
* read a byte from a blob
*/
static u_char read_byte(chunk_t *blob)
{
u_char val;
if (blob->len < sizeof(u_char))
{
return 0;
}
val = *(blob->ptr);
*blob = chunk_skip(*blob, sizeof(u_char));
return val;
}
/**
* read a u_int32_t from a blob
*/
static u_int32_t read_uint32(chunk_t *blob)
{
u_int32_t val;
if (blob->len < sizeof(u_int32_t))
{
return 0;
}
val = ntohl(*(u_int32_t*)blob->ptr);
*blob = chunk_skip(*blob, sizeof(u_int32_t));
return val;
}
/**
* read a ssh-agent "string" length/value from a blob
*/
static chunk_t read_string(chunk_t *blob)
{
int len;
chunk_t str;
len = read_uint32(blob);
if (len > blob->len)
{
return chunk_empty;
}
str = chunk_create(blob->ptr, len);
*blob = chunk_skip(*blob, + len);
return str;
}
/**
* open socket connection to the ssh-agent
*/
static int open_connection(char *path)
{
struct sockaddr_un addr;
int s;
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s == -1)
{
DBG1("opening ssh-agent socket %s failed: %s:", path, strerror(errno));
return -1;
}
addr.sun_family = AF_UNIX;
addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
strncpy(addr.sun_path, path, UNIX_PATH_MAX - 1);
if (connect(s, (struct sockaddr*)&addr, SUN_LEN(&addr)) != 0)
{
DBG1("connecting to ssh-agent socket failed: %s", strerror(errno));
close(s);
return -1;
}
return s;
}
/**
* Get the first usable key from the agent
*/
static bool read_key(private_agent_private_key_t *this, public_key_t *pubkey)
{
int len, count;
char buf[2048];
chunk_t blob, key, type, n;
len = htonl(1);
buf[0] = SSH_AGENT_ID_REQUEST;
if (write(this->socket, &len, sizeof(len)) != sizeof(len) ||
write(this->socket, &buf, 1) != 1)
{
DBG1("writing to ssh-agent failed");
return FALSE;
}
blob = chunk_create(buf, sizeof(buf));
blob.len = read(this->socket, blob.ptr, blob.len);
if (blob.len < sizeof(u_int32_t) + sizeof(u_char) ||
read_uint32(&blob) != blob.len ||
read_byte(&blob) != SSH_AGENT_ID_RESPONSE)
{
DBG1("received invalid ssh-agent identity response");
return FALSE;
}
count = read_uint32(&blob);
while (blob.len)
{
key = read_string(&blob);
if (!key.len)
{
break;
}
this->key = key;
type = read_string(&key);
if (!type.len || !strneq("ssh-rsa", type.ptr, type.len))
{
break;
}
read_string(&key);
n = read_string(&key);
if (n.len <= 512/8)
{
break;;
}
if (pubkey && !private_key_belongs_to(&this->public.interface, pubkey))
{
continue;
}
this->key_size = n.len;
if (n.ptr[0] == 0)
{
this->key_size--;
}
this->key = chunk_clone(this->key);
return TRUE;
}
this->key = chunk_empty;
return FALSE;
}
/**
* Implementation of agent_private_key.destroy.
*/
static bool sign(private_agent_private_key_t *this, signature_scheme_t scheme,
chunk_t data, chunk_t *signature)
{
u_int32_t len, flags;
char buf[2048];
chunk_t blob;
if (scheme != SIGN_RSA_EMSA_PKCS1_SHA1)
{
DBG1("signature scheme %N not supported by ssh-agent",
signature_scheme_names, scheme);
return FALSE;
}
len = htonl(1 + sizeof(u_int32_t) * 3 + this->key.len + data.len);
buf[0] = SSH_AGENT_SIGN_REQUEST;
if (write(this->socket, &len, sizeof(len)) != sizeof(len) ||
write(this->socket, &buf, 1) != 1)
{
DBG1("writing to ssh-agent failed");
return FALSE;
}
len = htonl(this->key.len);
if (write(this->socket, &len, sizeof(len)) != sizeof(len) ||
write(this->socket, this->key.ptr, this->key.len) != this->key.len)
{
DBG1("writing to ssh-agent failed");
return FALSE;
}
len = htonl(data.len);
if (write(this->socket, &len, sizeof(len)) != sizeof(len) ||
write(this->socket, data.ptr, data.len) != data.len)
{
DBG1("writing to ssh-agent failed");
return FALSE;
}
flags = htonl(0);
if (write(this->socket, &flags, sizeof(flags)) != sizeof(flags))
{
DBG1("writing to ssh-agent failed");
return FALSE;
}
blob = chunk_create(buf, sizeof(buf));
blob.len = read(this->socket, blob.ptr, blob.len);
if (blob.len < sizeof(u_int32_t) + sizeof(u_char) ||
read_uint32(&blob) != blob.len ||
read_byte(&blob) != SSH_AGENT_SIGN_RESPONSE)
{
DBG1("received invalid ssh-agent signature response");
return FALSE;
}
/* parse length */
blob = read_string(&blob);
/* skip sig type */
read_string(&blob);
/* parse length */
blob = read_string(&blob);
if (!blob.len)
{
DBG1("received invalid ssh-agent signature response");
return FALSE;
}
*signature = chunk_clone(blob);
return TRUE;
}
/**
* Implementation of agent_private_key.destroy.
*/
static key_type_t get_type(private_agent_private_key_t *this)
{
return KEY_RSA;
}
/**
* Implementation of agent_private_key.destroy.
*/
static bool decrypt(private_agent_private_key_t *this,
chunk_t crypto, chunk_t *plain)
{
DBG1("private key decryption not supported by ssh-agent");
return FALSE;
}
/**
* Implementation of agent_private_key.destroy.
*/
static size_t get_keysize(private_agent_private_key_t *this)
{
return this->key_size;
}
/**
* Implementation of agent_private_key.get_public_key.
*/
static public_key_t* get_public_key(private_agent_private_key_t *this)
{
chunk_t key, n, e;
key = this->key;
read_string(&key);
e = read_string(&key);
n = read_string(&key);
return lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
BUILD_RSA_MODULUS, n, BUILD_RSA_PUB_EXP, e, BUILD_END);
}
/**
* Implementation of private_key_t.get_encoding
*/
static bool get_encoding(private_agent_private_key_t *this,
key_encoding_type_t type, chunk_t *encoding)
{
return FALSE;
}
/**
* Implementation of private_key_t.get_fingerprint
*/
static bool get_fingerprint(private_agent_private_key_t *this,
key_encoding_type_t type, chunk_t *fp)
{
chunk_t n, e, key;
if (lib->encoding->get_cache(lib->encoding, type, this, fp))
{
return TRUE;
}
key = this->key;
read_string(&key);
e = read_string(&key);
n = read_string(&key);
return lib->encoding->encode(lib->encoding, type, this, fp,
KEY_PART_RSA_MODULUS, n, KEY_PART_RSA_PUB_EXP, e, KEY_PART_END);
}
/**
* Implementation of agent_private_key.get_ref.
*/
static private_agent_private_key_t* get_ref(private_agent_private_key_t *this)
{
ref_get(&this->ref);
return this;
}
/**
* Implementation of agent_private_key.destroy.
*/
static void destroy(private_agent_private_key_t *this)
{
if (ref_put(&this->ref))
{
close(this->socket);
free(this->key.ptr);
lib->encoding->clear_cache(lib->encoding, this);
free(this);
}
}
/**
* See header.
*/
agent_private_key_t *agent_private_key_open(key_type_t type, va_list args)
{
private_agent_private_key_t *this;
public_key_t *pubkey = NULL;
char *path = NULL;
while (TRUE)
{
switch (va_arg(args, builder_part_t))
{
case BUILD_AGENT_SOCKET:
path = va_arg(args, char*);
continue;
case BUILD_PUBLIC_KEY:
pubkey = va_arg(args, public_key_t*);
continue;
case BUILD_END:
break;
default:
return NULL;
}
break;
}
if (!path)
{
return FALSE;
}
this = malloc_thing(private_agent_private_key_t);
this->public.interface.get_type = (key_type_t (*)(private_key_t *this))get_type;
this->public.interface.sign = (bool (*)(private_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t *signature))sign;
this->public.interface.decrypt = (bool (*)(private_key_t *this, chunk_t crypto, chunk_t *plain))decrypt;
this->public.interface.get_keysize = (size_t (*) (private_key_t *this))get_keysize;
this->public.interface.get_public_key = (public_key_t* (*)(private_key_t *this))get_public_key;
this->public.interface.belongs_to = private_key_belongs_to;
this->public.interface.equals = private_key_equals;
this->public.interface.get_fingerprint = (bool(*)(private_key_t*, key_encoding_type_t type, chunk_t *fp))get_fingerprint;
this->public.interface.has_fingerprint = (bool(*)(private_key_t*, chunk_t fp))private_key_has_fingerprint;
this->public.interface.get_encoding = (bool(*)(private_key_t*, key_encoding_type_t type, chunk_t *encoding))get_encoding;
this->public.interface.get_ref = (private_key_t* (*)(private_key_t *this))get_ref;
this->public.interface.destroy = (void (*)(private_key_t *this))destroy;
this->socket = open_connection(path);
if (this->socket < 0)
{
free(this);
return NULL;
}
this->key = chunk_empty;
this->ref = 1;
if (!read_key(this, pubkey))
{
destroy(this);
return NULL;
}
return &this->public;
}
|