xref: /freebsd-src/crypto/openssh/regress/misc/fuzz-harness/agent_fuzz_helper.c (revision a91a246563dffa876a52f53a98de4af9fa364c52)
119261079SEd Maste #include "fixed-keys.h"
219261079SEd Maste #include <assert.h>
319261079SEd Maste 
419261079SEd Maste #define main(ac, av) xxxmain(ac, av)
519261079SEd Maste #include "../../../ssh-agent.c"
619261079SEd Maste 
719261079SEd Maste void test_one(const uint8_t* s, size_t slen);
819261079SEd Maste 
919261079SEd Maste static int
devnull_or_die(void)1019261079SEd Maste devnull_or_die(void)
1119261079SEd Maste {
1219261079SEd Maste 	int fd;
1319261079SEd Maste 
1419261079SEd Maste 	if ((fd = open("/dev/null", O_RDWR)) == -1) {
1519261079SEd Maste 		error_f("open /dev/null: %s", strerror(errno));
1619261079SEd Maste 		abort();
1719261079SEd Maste 	}
1819261079SEd Maste 	return fd;
1919261079SEd Maste }
2019261079SEd Maste 
2119261079SEd Maste static struct sshkey *
pubkey_or_die(const char * s)2219261079SEd Maste pubkey_or_die(const char *s)
2319261079SEd Maste {
2419261079SEd Maste 	char *tmp, *cp;
2519261079SEd Maste 	struct sshkey *pubkey;
2619261079SEd Maste 	int r;
2719261079SEd Maste 
2819261079SEd Maste 	tmp = cp = xstrdup(s);
2919261079SEd Maste 	if ((pubkey = sshkey_new(KEY_UNSPEC)) == NULL)
3019261079SEd Maste 		abort();
3119261079SEd Maste 	if ((r = sshkey_read(pubkey, &cp)) != 0) {
3219261079SEd Maste 		error_fr(r, "parse");
3319261079SEd Maste 		abort();
3419261079SEd Maste 	}
3519261079SEd Maste 	free(tmp);
3619261079SEd Maste 	return pubkey;
3719261079SEd Maste }
3819261079SEd Maste 
3919261079SEd Maste static struct sshkey *
privkey_or_die(const char * s)4019261079SEd Maste privkey_or_die(const char *s)
4119261079SEd Maste {
4219261079SEd Maste 	int r;
4319261079SEd Maste 	struct sshbuf *b;
4419261079SEd Maste 	struct sshkey *privkey;
4519261079SEd Maste 
4619261079SEd Maste 	if ((b = sshbuf_from(s, strlen(s))) == NULL) {
4719261079SEd Maste 		error_f("sshbuf_from failed");
4819261079SEd Maste 		abort();
4919261079SEd Maste 	}
5019261079SEd Maste 	if ((r = sshkey_parse_private_fileblob(b, "", &privkey, NULL)) != 0) {
5119261079SEd Maste 		error_fr(r, "parse");
5219261079SEd Maste 		abort();
5319261079SEd Maste 	}
5419261079SEd Maste 	sshbuf_free(b);
5519261079SEd Maste 	return privkey;
5619261079SEd Maste }
5719261079SEd Maste 
5819261079SEd Maste static void
add_key(const char * privkey,const char * certpath)5919261079SEd Maste add_key(const char *privkey, const char *certpath)
6019261079SEd Maste {
6119261079SEd Maste 	Identity *id;
6219261079SEd Maste 	int r;
6319261079SEd Maste 	struct sshkey *cert;
6419261079SEd Maste 
6519261079SEd Maste 	id = xcalloc(1, sizeof(Identity));
6619261079SEd Maste 	TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
6719261079SEd Maste 	idtab->nentries++;
6819261079SEd Maste 	id->key = privkey_or_die(privkey);
6919261079SEd Maste 	id->comment = xstrdup("rhododaktulos Eos");
7019261079SEd Maste 	if (sshkey_is_sk(id->key))
7119261079SEd Maste 		id->sk_provider = xstrdup("internal");
7219261079SEd Maste 
7319261079SEd Maste 	/* Now the cert too */
7419261079SEd Maste 	id = xcalloc(1, sizeof(Identity));
7519261079SEd Maste 	TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
7619261079SEd Maste 	idtab->nentries++;
7719261079SEd Maste 	id->key = privkey_or_die(privkey);
7819261079SEd Maste 	cert = pubkey_or_die(certpath);
7919261079SEd Maste 	if ((r = sshkey_to_certified(id->key)) != 0) {
8019261079SEd Maste 		error_fr(r, "sshkey_to_certified");
8119261079SEd Maste 		abort();
8219261079SEd Maste 	}
8319261079SEd Maste 	if ((r = sshkey_cert_copy(cert, id->key)) != 0) {
8419261079SEd Maste 		error_fr(r, "sshkey_cert_copy");
8519261079SEd Maste 		abort();
8619261079SEd Maste 	}
8719261079SEd Maste 	sshkey_free(cert);
8819261079SEd Maste 	id->comment = xstrdup("outis");
8919261079SEd Maste 	if (sshkey_is_sk(id->key))
9019261079SEd Maste 		id->sk_provider = xstrdup("internal");
9119261079SEd Maste }
9219261079SEd Maste 
9319261079SEd Maste static void
cleanup_idtab(void)9419261079SEd Maste cleanup_idtab(void)
9519261079SEd Maste {
9619261079SEd Maste 	Identity *id;
9719261079SEd Maste 
9819261079SEd Maste 	if (idtab == NULL) return;
9919261079SEd Maste 	for (id = TAILQ_FIRST(&idtab->idlist); id;
10019261079SEd Maste 	    id = TAILQ_FIRST(&idtab->idlist)) {
10119261079SEd Maste 		TAILQ_REMOVE(&idtab->idlist, id, next);
10219261079SEd Maste 		free_identity(id);
10319261079SEd Maste 	}
10419261079SEd Maste 	free(idtab);
10519261079SEd Maste 	idtab = NULL;
10619261079SEd Maste }
10719261079SEd Maste 
10819261079SEd Maste static void
reset_idtab(void)10919261079SEd Maste reset_idtab(void)
11019261079SEd Maste {
11119261079SEd Maste 	cleanup_idtab();
11219261079SEd Maste 	idtab_init();
11319261079SEd Maste 	// Load keys.
11419261079SEd Maste 	add_key(PRIV_RSA, CERT_RSA);
11519261079SEd Maste 	add_key(PRIV_DSA, CERT_DSA);
11619261079SEd Maste 	add_key(PRIV_ECDSA, CERT_ECDSA);
11719261079SEd Maste 	add_key(PRIV_ED25519, CERT_ED25519);
11819261079SEd Maste 	add_key(PRIV_ECDSA_SK, CERT_ECDSA_SK);
11919261079SEd Maste 	add_key(PRIV_ED25519_SK, CERT_ED25519_SK);
12019261079SEd Maste }
12119261079SEd Maste 
12219261079SEd Maste static void
cleanup_sockettab(void)12319261079SEd Maste cleanup_sockettab(void)
12419261079SEd Maste {
12519261079SEd Maste 	u_int i;
12619261079SEd Maste 	for (i = 0; i < sockets_alloc; i++) {
12719261079SEd Maste 		if (sockets[i].type != AUTH_UNUSED)
12819261079SEd Maste 			close_socket(sockets + i);
12919261079SEd Maste 	}
13019261079SEd Maste 	free(sockets);
13119261079SEd Maste 	sockets = NULL;
13219261079SEd Maste 	sockets_alloc = 0;
13319261079SEd Maste }
13419261079SEd Maste 
13519261079SEd Maste static void
reset_sockettab(int devnull)13619261079SEd Maste reset_sockettab(int devnull)
13719261079SEd Maste {
13819261079SEd Maste 	int fd;
13919261079SEd Maste 
14019261079SEd Maste 	cleanup_sockettab();
14119261079SEd Maste 	if ((fd = dup(devnull)) == -1) {
14219261079SEd Maste 		error_f("dup: %s", strerror(errno));
14319261079SEd Maste 		abort();
14419261079SEd Maste 	}
14519261079SEd Maste 	new_socket(AUTH_CONNECTION, fd);
14619261079SEd Maste 	assert(sockets[0].type == AUTH_CONNECTION);
14719261079SEd Maste 	assert(sockets[0].fd == fd);
14819261079SEd Maste }
14919261079SEd Maste 
15019261079SEd Maste #define MAX_MESSAGES 256
15119261079SEd Maste void
test_one(const uint8_t * s,size_t slen)15219261079SEd Maste test_one(const uint8_t* s, size_t slen)
15319261079SEd Maste {
15419261079SEd Maste 	static int devnull = -1;
15519261079SEd Maste 	size_t i, olen, nlen;
15619261079SEd Maste 
15719261079SEd Maste 	if (devnull == -1) {
15819261079SEd Maste 		log_init(__progname, SYSLOG_LEVEL_DEBUG3,
15919261079SEd Maste 		    SYSLOG_FACILITY_AUTH, 1);
16019261079SEd Maste 		devnull = devnull_or_die();
16119261079SEd Maste 		allowed_providers = xstrdup("");
16219261079SEd Maste 		setenv("DISPLAY", "", 1); /* ban askpass */
16319261079SEd Maste 	}
16419261079SEd Maste 
16519261079SEd Maste 	reset_idtab();
16619261079SEd Maste 	reset_sockettab(devnull);
16719261079SEd Maste 	(void)sshbuf_put(sockets[0].input, s, slen);
16819261079SEd Maste 	for (i = 0; i < MAX_MESSAGES; i++) {
16919261079SEd Maste 		olen = sshbuf_len(sockets[0].input);
17019261079SEd Maste 		process_message(0);
17119261079SEd Maste 		nlen = sshbuf_len(sockets[0].input);
17219261079SEd Maste 		if (nlen == 0 || nlen == olen)
17319261079SEd Maste 			break;
17419261079SEd Maste 	}
17519261079SEd Maste 	cleanup_idtab();
17619261079SEd Maste 	cleanup_sockettab();
17719261079SEd Maste }
178*a91a2465SEd Maste 
179*a91a2465SEd Maste int
pkcs11_make_cert(const struct sshkey * priv,const struct sshkey * certpub,struct sshkey ** certprivp)180*a91a2465SEd Maste pkcs11_make_cert(const struct sshkey *priv,
181*a91a2465SEd Maste     const struct sshkey *certpub, struct sshkey **certprivp)
182*a91a2465SEd Maste {
183*a91a2465SEd Maste 	return -1; /* XXX */
184*a91a2465SEd Maste }
185