1 /* $OpenBSD: kexfuzz.c,v 1.4 2017/04/30 23:34:55 djm Exp $ */
3 * Fuzz harness for KEX code
5 * Placed in the public domain
10 #include <sys/types.h>
11 #include <sys/param.h>
28 #include "myproposal.h"
32 struct ssh *active_state = NULL; /* XXX - needed for linking */
35 static int do_debug = 0;
37 enum direction { S2C, C2S };
40 struct ssh *client, *server, *server2;
42 int trigger_direction, packet_index;
43 const char *dump_path;
44 struct sshbuf *replace_data;
48 packet_hook(struct ssh *ssh, struct sshbuf *packet, u_char *typep, void *_ctx)
50 struct hook_ctx *ctx = (struct hook_ctx *)_ctx;
51 int mydirection = ssh == ctx->client ? S2C : C2S;
52 int *packet_count = mydirection == S2C ? ctx->s2c : ctx->c2s;
57 printf("%s packet %d type %u:\n",
58 mydirection == S2C ? "s2c" : "c2s",
59 *packet_count, *typep);
60 sshbuf_dump(packet, stdout);
62 if (mydirection == ctx->trigger_direction &&
63 ctx->packet_index == *packet_count) {
64 if (ctx->replace_data != NULL) {
66 /* Type is first byte of packet */
67 if ((r = sshbuf_get_u8(ctx->replace_data,
69 (r = sshbuf_putb(packet, ctx->replace_data)) != 0)
72 printf("***** replaced packet type %u\n",
74 sshbuf_dump(packet, stdout);
76 } else if (ctx->dump_path != NULL) {
77 if ((dumpfile = fopen(ctx->dump_path, "w+")) == NULL)
78 err(1, "fopen %s", ctx->dump_path);
79 /* Write { type, packet } */
80 if (fwrite(typep, 1, 1, dumpfile) != 1)
81 err(1, "fwrite type %s", ctx->dump_path);
82 if (sshbuf_len(packet) != 0 &&
83 fwrite(sshbuf_ptr(packet), sshbuf_len(packet),
85 err(1, "fwrite body %s", ctx->dump_path);
87 printf("***** dumped packet type %u len %zu\n",
88 *typep, sshbuf_len(packet));
91 /* No point in continuing */
100 do_send_and_receive(struct ssh *from, struct ssh *to)
108 if ((r = ssh_packet_next(from, &type)) != 0) {
109 fprintf(stderr, "ssh_packet_next: %s\n", ssh_err(r));
115 buf = ssh_output_ptr(from, &len);
118 if ((r = ssh_input_append(to, buf, len)) != 0) {
119 debug("ssh_input_append: %s", ssh_err(r));
122 if ((r = ssh_output_consume(from, len)) != 0) {
123 debug("ssh_output_consume: %s", ssh_err(r));
129 /* Minimal test_helper.c scaffholding to make this standalone */
130 const char *in_test = NULL;
131 #define TEST_START(a) \
135 fprintf(stderr, "test %s starting\n", in_test); \
137 #define TEST_DONE() \
140 fprintf(stderr, "test %s done\n", \
141 in_test ? in_test : "???"); \
144 #define ASSERT_INT_EQ(a, b) \
146 if ((int)(a) != (int)(b)) { \
147 fprintf(stderr, "%s %s:%d " \
148 "%s (%d) != expected %s (%d)\n", \
149 in_test ? in_test : "(none)", \
150 __func__, __LINE__, #a, (int)(a), #b, (int)(b)); \
154 #define ASSERT_INT_GE(a, b) \
156 if ((int)(a) < (int)(b)) { \
157 fprintf(stderr, "%s %s:%d " \
158 "%s (%d) < expected %s (%d)\n", \
159 in_test ? in_test : "(none)", \
160 __func__, __LINE__, #a, (int)(a), #b, (int)(b)); \
164 #define ASSERT_PTR_NE(a, b) \
167 fprintf(stderr, "%s %s:%d " \
168 "%s (%p) != expected %s (%p)\n", \
169 in_test ? in_test : "(none)", \
170 __func__, __LINE__, #a, (a), #b, (b)); \
177 run_kex(struct ssh *client, struct ssh *server)
181 while (!server->kex->done || !client->kex->done) {
182 if ((r = do_send_and_receive(server, client)) != 0) {
183 debug("do_send_and_receive S2C: %s", ssh_err(r));
186 if ((r = do_send_and_receive(client, server)) != 0) {
187 debug("do_send_and_receive C2S: %s", ssh_err(r));
192 printf("done: %s\n", ssh_err(r));
194 ASSERT_INT_EQ(server->kex->done, 1);
195 ASSERT_INT_EQ(client->kex->done, 1);
199 do_kex_with_key(const char *kex, struct sshkey *prvkey, int *c2s, int *s2c,
200 int direction, int packet_index,
201 const char *dump_path, struct sshbuf *replace_data)
203 struct ssh *client = NULL, *server = NULL, *server2 = NULL;
204 struct sshkey *pubkey = NULL;
205 struct sshbuf *state;
206 struct kex_params kex_params;
207 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
208 char *keyname = NULL;
209 struct hook_ctx hook_ctx;
211 TEST_START("sshkey_from_private");
212 ASSERT_INT_EQ(sshkey_from_private(prvkey, &pubkey), 0);
215 TEST_START("ssh_init");
216 memcpy(kex_params.proposal, myproposal, sizeof(myproposal));
218 kex_params.proposal[PROPOSAL_KEX_ALGS] = strdup(kex);
219 keyname = strdup(sshkey_ssh_name(prvkey));
220 ASSERT_PTR_NE(keyname, NULL);
221 kex_params.proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = keyname;
222 ASSERT_INT_EQ(ssh_init(&client, 0, &kex_params), 0);
223 ASSERT_INT_EQ(ssh_init(&server, 1, &kex_params), 0);
224 ASSERT_INT_EQ(ssh_init(&server2, 1, NULL), 0);
225 ASSERT_PTR_NE(client, NULL);
226 ASSERT_PTR_NE(server, NULL);
227 ASSERT_PTR_NE(server2, NULL);
232 hook_ctx.trigger_direction = direction;
233 hook_ctx.packet_index = packet_index;
234 hook_ctx.dump_path = dump_path;
235 hook_ctx.replace_data = replace_data;
236 hook_ctx.client = client;
237 hook_ctx.server = server;
238 hook_ctx.server2 = server2;
239 ssh_packet_set_input_hook(client, packet_hook, &hook_ctx);
240 ssh_packet_set_input_hook(server, packet_hook, &hook_ctx);
241 ssh_packet_set_input_hook(server2, packet_hook, &hook_ctx);
243 TEST_START("ssh_add_hostkey");
244 ASSERT_INT_EQ(ssh_add_hostkey(server, prvkey), 0);
245 ASSERT_INT_EQ(ssh_add_hostkey(client, pubkey), 0);
249 run_kex(client, server);
252 TEST_START("rekeying client");
253 ASSERT_INT_EQ(kex_send_kexinit(client), 0);
254 run_kex(client, server);
257 TEST_START("rekeying server");
258 ASSERT_INT_EQ(kex_send_kexinit(server), 0);
259 run_kex(client, server);
262 TEST_START("ssh_packet_get_state");
263 state = sshbuf_new();
264 ASSERT_PTR_NE(state, NULL);
265 ASSERT_INT_EQ(ssh_packet_get_state(server, state), 0);
266 ASSERT_INT_GE(sshbuf_len(state), 1);
269 TEST_START("ssh_packet_set_state");
270 ASSERT_INT_EQ(ssh_add_hostkey(server2, prvkey), 0);
271 kex_free(server2->kex); /* XXX or should ssh_packet_set_state()? */
272 ASSERT_INT_EQ(ssh_packet_set_state(server2, state), 0);
273 ASSERT_INT_EQ(sshbuf_len(state), 0);
275 ASSERT_PTR_NE(server2->kex, NULL);
276 /* XXX we need to set the callbacks */
278 server2->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
279 server2->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
280 server2->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
281 server2->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
282 server2->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
283 server2->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
284 server2->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
285 # ifdef OPENSSL_HAS_ECC
286 server2->kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
289 server2->kex->kex[KEX_C25519_SHA256] = kexc25519_server;
290 server2->kex->load_host_public_key = server->kex->load_host_public_key;
291 server2->kex->load_host_private_key = server->kex->load_host_private_key;
292 server2->kex->sign = server->kex->sign;
295 TEST_START("rekeying server2");
296 ASSERT_INT_EQ(kex_send_kexinit(server2), 0);
297 run_kex(client, server2);
298 ASSERT_INT_EQ(kex_send_kexinit(client), 0);
299 run_kex(client, server2);
302 TEST_START("cleanup");
315 "Usage: kexfuzz [-hcdrv] [-D direction] [-f data_file]\n"
316 " [-K kex_alg] [-k private_key] [-i packet_index]\n"
319 " -h Display this help\n"
320 " -c Count packets sent during KEX\n"
321 " -d Dump mode: record KEX packet to data file\n"
322 " -r Replace mode: replace packet with data file\n"
323 " -v Turn on verbose logging\n"
324 " -D S2C|C2S Packet direction for replacement or dump\n"
325 " -f data_file Path to data file for replacement or dump\n"
326 " -K kex_alg Name of KEX algorithm to test (see below)\n"
327 " -k private_key Path to private key file\n"
328 " -i packet_index Index of packet to replace or dump (from 0)\n"
330 "Available KEX algorithms: %s\n", kex_alg_list(' '));
334 badusage(const char *bad)
336 fprintf(stderr, "Invalid options\n");
337 fprintf(stderr, "%s\n", bad);
343 main(int argc, char **argv)
346 int count_flag = 0, dump_flag = 0, replace_flag = 0;
347 int packet_index = -1, direction = -1;
348 int s2c = 0, c2s = 0; /* packet counts */
349 const char *kex = NULL, *kpath = NULL, *data_path = NULL;
350 struct sshkey *key = NULL;
351 struct sshbuf *replace_data = NULL;
353 setvbuf(stdout, NULL, _IONBF, 0);
354 while ((ch = getopt(argc, argv, "hcdrvD:f:K:k:i:")) != -1) {
373 if (strcasecmp(optarg, "s2c") == 0)
375 else if (strcasecmp(optarg, "c2s") == 0)
378 badusage("Invalid direction (-D)");
390 packet_index = atoi(optarg);
391 if (packet_index < 0)
392 badusage("Invalid packet index");
395 badusage("unsupported flag");
401 log_init(argv[0], do_debug ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
402 SYSLOG_FACILITY_USER, 1);
404 /* Must select a single mode */
405 if ((count_flag + dump_flag + replace_flag) != 1)
406 badusage("Must select one mode: -c, -d or -r");
407 /* KEX type is mandatory */
408 if (kex == NULL || !kex_names_valid(kex) || strchr(kex, ',') != NULL)
409 badusage("Missing or invalid kex type (-K flag)");
410 /* Valid key is mandatory */
412 badusage("Missing private key (-k flag)");
413 if ((fd = open(kpath, O_RDONLY)) == -1)
414 err(1, "open %s", kpath);
415 if ((r = sshkey_load_private_type_fd(fd, KEY_UNSPEC, NULL,
417 errx(1, "Unable to load key %s: %s", kpath, ssh_err(r));
419 /* XXX check that it is a private key */
420 /* XXX support certificates */
421 if (key == NULL || key->type == KEY_UNSPEC)
422 badusage("Invalid key file (-k flag)");
424 /* Replace (fuzz) mode */
426 if (packet_index == -1 || direction == -1 || data_path == NULL)
427 badusage("Replace (-r) mode must specify direction "
428 "(-D) packet index (-i) and data path (-f)");
429 if ((fd = open(data_path, O_RDONLY)) == -1)
430 err(1, "open %s", data_path);
431 replace_data = sshbuf_new();
432 if ((r = sshkey_load_file(fd, replace_data)) != 0)
433 errx(1, "read %s: %s", data_path, ssh_err(r));
439 if (packet_index == -1 || direction == -1 || data_path == NULL)
440 badusage("Dump (-d) mode must specify direction "
441 "(-D), packet index (-i) and data path (-f)");
444 /* Count mode needs no further flags */
446 do_kex_with_key(kex, key, &c2s, &s2c,
447 direction, packet_index,
448 dump_flag ? data_path : NULL,
449 replace_flag ? replace_data : NULL);
451 sshbuf_free(replace_data);
454 printf("S2C: %d\n", s2c);
455 printf("C2S: %d\n", c2s);