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
453
454
|
// libfuzzer driver for key exchange fuzzing.
#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
extern "C" {
#include "includes.h"
#include "ssherr.h"
#include "ssh_api.h"
#include "sshbuf.h"
#include "packet.h"
#include "myproposal.h"
#include "xmalloc.h"
#include "authfile.h"
#include "log.h"
#include "fixed-keys.h"
// Define if you want to generate traces.
/* #define STANDALONE 1 */
static int prepare_key(struct shared_state *st, int keytype, int bits);
struct shared_state {
size_t nkeys;
struct sshkey **privkeys, **pubkeys;
};
struct test_state {
struct sshbuf *smsgs, *cmsgs; /* output, for standalone mode */
struct sshbuf *sin, *cin; /* input; setup per-test in do_kex_with_key */
struct sshbuf *s_template, *c_template; /* main copy of input */
};
static int
do_send_and_receive(struct ssh *from, struct ssh *to,
struct sshbuf *store, int clobber, size_t *n)
{
u_char type;
size_t len;
const u_char *buf;
int r;
for (*n = 0;; (*n)++) {
if ((r = ssh_packet_next(from, &type)) != 0) {
debug_fr(r, "ssh_packet_next");
return r;
}
if (type != 0)
return 0;
buf = ssh_output_ptr(from, &len);
debug_f("%zu%s", len, clobber ? " ignore" : "");
if (len == 0)
return 0;
if ((r = ssh_output_consume(from, len)) != 0) {
debug_fr(r, "ssh_output_consume");
return r;
}
if (store != NULL && (r = sshbuf_put(store, buf, len)) != 0) {
debug_fr(r, "sshbuf_put");
return r;
}
if (!clobber && (r = ssh_input_append(to, buf, len)) != 0) {
debug_fr(r, "ssh_input_append");
return r;
}
}
}
static int
run_kex(struct test_state *ts, struct ssh *client, struct ssh *server)
{
int r = 0;
size_t cn, sn;
/* If fuzzing, replace server/client input */
if (ts->sin != NULL) {
if ((r = ssh_input_append(server, sshbuf_ptr(ts->sin),
sshbuf_len(ts->sin))) != 0) {
error_fr(r, "ssh_input_append");
return r;
}
sshbuf_reset(ts->sin);
}
if (ts->cin != NULL) {
if ((r = ssh_input_append(client, sshbuf_ptr(ts->cin),
sshbuf_len(ts->cin))) != 0) {
error_fr(r, "ssh_input_append");
return r;
}
sshbuf_reset(ts->cin);
}
while (!server->kex->done || !client->kex->done) {
cn = sn = 0;
debug_f("S:");
if ((r = do_send_and_receive(server, client,
ts->smsgs, ts->cin != NULL, &sn)) != 0) {
debug_fr(r, "S->C");
break;
}
debug_f("C:");
if ((r = do_send_and_receive(client, server,
ts->cmsgs, ts->sin != NULL, &cn)) != 0) {
debug_fr(r, "C->S");
break;
}
if (cn == 0 && sn == 0) {
debug_f("kex stalled");
r = SSH_ERR_PROTOCOL_ERROR;
break;
}
}
debug_fr(r, "done");
return r;
}
static void
store_key(struct shared_state *st, struct sshkey *pubkey,
struct sshkey *privkey)
{
if (st == NULL || pubkey->type < 0 || pubkey->type > INT_MAX ||
privkey->type != pubkey->type ||
((size_t)pubkey->type < st->nkeys &&
st->pubkeys[pubkey->type] != NULL))
abort();
if ((size_t)pubkey->type >= st->nkeys) {
st->pubkeys = (struct sshkey **)xrecallocarray(st->pubkeys,
st->nkeys, pubkey->type + 1, sizeof(*st->pubkeys));
st->privkeys = (struct sshkey **)xrecallocarray(st->privkeys,
st->nkeys, privkey->type + 1, sizeof(*st->privkeys));
st->nkeys = privkey->type + 1;
}
debug_f("store %s at %d", sshkey_ssh_name(pubkey), pubkey->type);
st->pubkeys[pubkey->type] = pubkey;
st->privkeys[privkey->type] = privkey;
}
static int
prepare_keys(struct shared_state *st)
{
if (prepare_key(st, KEY_RSA, 2048) != 0 ||
prepare_key(st, KEY_ECDSA, 256) != 0 ||
prepare_key(st, KEY_ED25519, 256) != 0) {
error_f("key prepare failed");
return -1;
}
return 0;
}
static struct sshkey *
get_pubkey(struct shared_state *st, int keytype)
{
if (st == NULL || keytype < 0 || (size_t)keytype >= st->nkeys ||
st->pubkeys == NULL || st->pubkeys[keytype] == NULL)
abort();
return st->pubkeys[keytype];
}
static struct sshkey *
get_privkey(struct shared_state *st, int keytype)
{
if (st == NULL || keytype < 0 || (size_t)keytype >= st->nkeys ||
st->privkeys == NULL || st->privkeys[keytype] == NULL)
abort();
return st->privkeys[keytype];
}
static int
do_kex_with_key(struct shared_state *st, struct test_state *ts,
const char *kex, int keytype)
{
struct ssh *client = NULL, *server = NULL;
struct sshkey *privkey = NULL, *pubkey = NULL;
struct sshbuf *state = NULL;
struct kex_params kex_params;
const char *ccp, *proposal[PROPOSAL_MAX] = { KEX_CLIENT };
char *myproposal[PROPOSAL_MAX] = {0}, *keyname = NULL;
int i, r;
ts->cin = ts->sin = NULL;
if (ts->c_template != NULL &&
(ts->cin = sshbuf_fromb(ts->c_template)) == NULL)
abort();
if (ts->s_template != NULL &&
(ts->sin = sshbuf_fromb(ts->s_template)) == NULL)
abort();
pubkey = get_pubkey(st, keytype);
privkey = get_privkey(st, keytype);
keyname = xstrdup(sshkey_ssh_name(privkey));
if (ts->cin != NULL) {
debug_f("%s %s clobber client %zu", kex, keyname,
sshbuf_len(ts->cin));
} else if (ts->sin != NULL) {
debug_f("%s %s clobber server %zu", kex, keyname,
sshbuf_len(ts->sin));
} else
debug_f("%s %s noclobber", kex, keyname);
for (i = 0; i < PROPOSAL_MAX; i++) {
ccp = proposal[i];
#ifdef CIPHER_NONE_AVAIL
if (i == PROPOSAL_ENC_ALGS_CTOS || i == PROPOSAL_ENC_ALGS_STOC)
ccp = "none";
#endif
if (i == PROPOSAL_SERVER_HOST_KEY_ALGS)
ccp = keyname;
else if (i == PROPOSAL_KEX_ALGS && kex != NULL)
ccp = kex;
if ((myproposal[i] = strdup(ccp)) == NULL) {
error_f("strdup prop %d", i);
goto fail;
}
}
memcpy(kex_params.proposal, myproposal, sizeof(myproposal));
if ((r = ssh_init(&client, 0, &kex_params)) != 0) {
error_fr(r, "init client");
goto fail;
}
if ((r = ssh_init(&server, 1, &kex_params)) != 0) {
error_fr(r, "init server");
goto fail;
}
if ((r = ssh_add_hostkey(server, privkey)) != 0 ||
(r = ssh_add_hostkey(client, pubkey)) != 0) {
error_fr(r, "add hostkeys");
goto fail;
}
if ((r = run_kex(ts, client, server)) != 0) {
error_fr(r, "kex");
goto fail;
}
/* XXX rekex, set_state, etc */
fail:
for (i = 0; i < PROPOSAL_MAX; i++)
free(myproposal[i]);
sshbuf_free(ts->sin);
sshbuf_free(ts->cin);
sshbuf_free(state);
ssh_free(client);
ssh_free(server);
free(keyname);
return r;
}
static int
prepare_key(struct shared_state *st, int kt, int bits)
{
const char *pubstr = NULL;
const char *privstr = NULL;
char *tmp, *cp;
struct sshkey *privkey = NULL, *pubkey = NULL;
struct sshbuf *b = NULL;
int r;
switch (kt) {
case KEY_RSA:
pubstr = PUB_RSA;
privstr = PRIV_RSA;
break;
case KEY_ECDSA:
pubstr = PUB_ECDSA;
privstr = PRIV_ECDSA;
break;
case KEY_ED25519:
pubstr = PUB_ED25519;
privstr = PRIV_ED25519;
break;
default:
abort();
}
if ((b = sshbuf_from(privstr, strlen(privstr))) == NULL)
abort();
if ((r = sshkey_parse_private_fileblob(b, "", &privkey, NULL)) != 0) {
error_fr(r, "priv %d", kt);
abort();
}
sshbuf_free(b);
tmp = cp = xstrdup(pubstr);
if ((pubkey = sshkey_new(KEY_UNSPEC)) == NULL)
abort();
if ((r = sshkey_read(pubkey, &cp)) != 0) {
error_fr(r, "pub %d", kt);
abort();
}
free(tmp);
store_key(st, pubkey, privkey);
return 0;
}
#if defined(STANDALONE)
#if 0 /* use this if generating new keys to embed above */
static int
prepare_key(struct shared_state *st, int keytype, int bits)
{
struct sshkey *privkey = NULL, *pubkey = NULL;
int r;
if ((r = sshkey_generate(keytype, bits, &privkey)) != 0) {
error_fr(r, "generate");
abort();
}
if ((r = sshkey_from_private(privkey, &pubkey)) != 0) {
error_fr(r, "make pubkey");
abort();
}
store_key(st, pubkey, privkey);
return 0;
}
#endif
int main(void)
{
static struct shared_state *st;
struct test_state *ts;
const int keytypes[] = { KEY_RSA, KEY_ECDSA, KEY_ED25519, -1 };
static const char * const kextypes[] = {
"sntrup761x25519-sha512@openssh.com",
"curve25519-sha256@libssh.org",
"ecdh-sha2-nistp256",
"diffie-hellman-group1-sha1",
"diffie-hellman-group-exchange-sha1",
NULL,
};
int i, j;
char *path;
FILE *f;
log_init("kex_fuzz", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 1);
if (st == NULL) {
st = (struct shared_state *)xcalloc(1, sizeof(*st));
prepare_keys(st);
}
/* Run each kex method for each key and save client/server packets */
for (i = 0; keytypes[i] != -1; i++) {
for (j = 0; kextypes[j] != NULL; j++) {
ts = (struct test_state *)xcalloc(1, sizeof(*ts));
ts->smsgs = sshbuf_new();
ts->cmsgs = sshbuf_new();
do_kex_with_key(st, ts, kextypes[j], keytypes[i]);
xasprintf(&path, "S2C-%s-%s",
kextypes[j], sshkey_type(st->pubkeys[keytypes[i]]));
debug_f("%s", path);
if ((f = fopen(path, "wb+")) == NULL)
abort();
if (fwrite(sshbuf_ptr(ts->smsgs), 1,
sshbuf_len(ts->smsgs), f) != sshbuf_len(ts->smsgs))
abort();
fclose(f);
free(path);
//sshbuf_dump(ts->smsgs, stderr);
xasprintf(&path, "C2S-%s-%s",
kextypes[j], sshkey_type(st->pubkeys[keytypes[i]]));
debug_f("%s", path);
if ((f = fopen(path, "wb+")) == NULL)
abort();
if (fwrite(sshbuf_ptr(ts->cmsgs), 1,
sshbuf_len(ts->cmsgs), f) != sshbuf_len(ts->cmsgs))
abort();
fclose(f);
free(path);
//sshbuf_dump(ts->cmsgs, stderr);
sshbuf_free(ts->smsgs);
sshbuf_free(ts->cmsgs);
free(ts);
}
}
for (i = 0; keytypes[i] != -1; i++) {
xasprintf(&path, "%s.priv",
sshkey_type(st->privkeys[keytypes[i]]));
debug_f("%s", path);
if (sshkey_save_private(st->privkeys[keytypes[i]], path,
"", "", SSHKEY_PRIVATE_OPENSSH, NULL, 0) != 0)
abort();
free(path);
xasprintf(&path, "%s.pub",
sshkey_type(st->pubkeys[keytypes[i]]));
debug_f("%s", path);
if (sshkey_save_public(st->pubkeys[keytypes[i]], path, "") != 0)
abort();
free(path);
}
}
#else /* !STANDALONE */
static void
do_kex(struct shared_state *st, struct test_state *ts, const char *kex)
{
do_kex_with_key(st, ts, kex, KEY_RSA);
do_kex_with_key(st, ts, kex, KEY_ECDSA);
do_kex_with_key(st, ts, kex, KEY_ED25519);
}
static void
kex_tests(struct shared_state *st, struct test_state *ts)
{
do_kex(st, ts, "sntrup761x25519-sha512@openssh.com");
do_kex(st, ts, "curve25519-sha256@libssh.org");
do_kex(st, ts, "ecdh-sha2-nistp256");
do_kex(st, ts, "diffie-hellman-group1-sha1");
do_kex(st, ts, "diffie-hellman-group-exchange-sha1");
}
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
static struct shared_state *st;
struct test_state *ts;
u_char crbuf[SSH_MAX_PRE_BANNER_LINES * 4];
u_char zbuf[4096] = {0};
static LogLevel loglevel = SYSLOG_LEVEL_INFO;
if (st == NULL) {
if (getenv("DEBUG") != NULL || getenv("KEX_FUZZ_DEBUG") != NULL)
loglevel = SYSLOG_LEVEL_DEBUG3;
log_init("kex_fuzz",
loglevel, SYSLOG_FACILITY_AUTH, 1);
st = (struct shared_state *)xcalloc(1, sizeof(*st));
prepare_keys(st);
}
/* Ensure that we can complete (fail) banner exchange at least */
memset(crbuf, '\n', sizeof(crbuf));
ts = (struct test_state *)xcalloc(1, sizeof(*ts));
if ((ts->s_template = sshbuf_new()) == NULL ||
sshbuf_put(ts->s_template, data, size) != 0 ||
sshbuf_put(ts->s_template, crbuf, sizeof(crbuf)) != 0 ||
sshbuf_put(ts->s_template, zbuf, sizeof(zbuf)) != 0)
abort();
kex_tests(st, ts);
sshbuf_free(ts->s_template);
free(ts);
ts = (struct test_state *)xcalloc(1, sizeof(*ts));
if ((ts->c_template = sshbuf_new()) == NULL ||
sshbuf_put(ts->c_template, data, size) != 0 ||
sshbuf_put(ts->c_template, crbuf, sizeof(crbuf)) != 0 ||
sshbuf_put(ts->c_template, zbuf, sizeof(zbuf)) != 0)
abort();
kex_tests(st, ts);
sshbuf_free(ts->c_template);
free(ts);
return 0;
}
#endif /* STANDALONE */
} /* extern "C" */
|