/src/hpn-ssh/regress/misc/fuzz-harness/agent_fuzz_helper.c
Line  | Count  | Source  | 
1  |  | #include "fixed-keys.h"  | 
2  |  | #include <assert.h>  | 
3  |  |  | 
4  |  | #define main(ac, av) xxxmain(ac, av)  | 
5  |  | #include "../../../ssh-agent.c"  | 
6  |  |  | 
7  |  | void test_one(const uint8_t* s, size_t slen);  | 
8  |  |  | 
9  |  | static int  | 
10  |  | devnull_or_die(void)  | 
11  | 1  | { | 
12  | 1  |   int fd;  | 
13  |  |  | 
14  | 1  |   if ((fd = open("/dev/null", O_RDWR)) == -1) { | 
15  | 0  |     error_f("open /dev/null: %s", strerror(errno)); | 
16  | 0  |     abort();  | 
17  | 0  |   }  | 
18  | 1  |   return fd;  | 
19  | 1  | }  | 
20  |  |  | 
21  |  | static struct sshkey *  | 
22  |  | pubkey_or_die(const char *s)  | 
23  | 5.78k  | { | 
24  | 5.78k  |   char *tmp, *cp;  | 
25  | 5.78k  |   struct sshkey *pubkey;  | 
26  | 5.78k  |   int r;  | 
27  |  |  | 
28  | 5.78k  |   tmp = cp = xstrdup(s);  | 
29  | 5.78k  |   if ((pubkey = sshkey_new(KEY_UNSPEC)) == NULL)  | 
30  | 0  |     abort();  | 
31  | 5.78k  |   if ((r = sshkey_read(pubkey, &cp)) != 0) { | 
32  | 0  |     error_fr(r, "parse");  | 
33  | 0  |     abort();  | 
34  | 0  |   }  | 
35  | 5.78k  |   free(tmp);  | 
36  | 5.78k  |   return pubkey;  | 
37  | 5.78k  | }  | 
38  |  |  | 
39  |  | static struct sshkey *  | 
40  |  | privkey_or_die(const char *s)  | 
41  | 11.5k  | { | 
42  | 11.5k  |   int r;  | 
43  | 11.5k  |   struct sshbuf *b;  | 
44  | 11.5k  |   struct sshkey *privkey;  | 
45  |  |  | 
46  | 11.5k  |   if ((b = sshbuf_from(s, strlen(s))) == NULL) { | 
47  | 0  |     error_f("sshbuf_from failed"); | 
48  | 0  |     abort();  | 
49  | 0  |   }  | 
50  | 11.5k  |   if ((r = sshkey_parse_private_fileblob(b, "", &privkey, NULL)) != 0) { | 
51  | 0  |     error_fr(r, "parse");  | 
52  | 0  |     abort();  | 
53  | 0  |   }  | 
54  | 11.5k  |   sshbuf_free(b);  | 
55  | 11.5k  |   return privkey;  | 
56  | 11.5k  | }  | 
57  |  |  | 
58  |  | static void  | 
59  |  | add_key(const char *privkey, const char *certpath)  | 
60  | 5.78k  | { | 
61  | 5.78k  |   Identity *id;  | 
62  | 5.78k  |   int r;  | 
63  | 5.78k  |   struct sshkey *cert;  | 
64  |  |  | 
65  | 5.78k  |   id = xcalloc(1, sizeof(Identity));  | 
66  | 5.78k  |   TAILQ_INSERT_TAIL(&idtab->idlist, id, next);  | 
67  | 5.78k  |   idtab->nentries++;  | 
68  | 5.78k  |   id->key = privkey_or_die(privkey);  | 
69  | 5.78k  |   id->comment = xstrdup("rhododaktulos Eos"); | 
70  | 5.78k  |   if (sshkey_is_sk(id->key))  | 
71  | 2.31k  |     id->sk_provider = xstrdup("internal"); | 
72  |  |  | 
73  |  |   /* Now the cert too */  | 
74  | 5.78k  |   id = xcalloc(1, sizeof(Identity));  | 
75  | 5.78k  |   TAILQ_INSERT_TAIL(&idtab->idlist, id, next);  | 
76  | 5.78k  |   idtab->nentries++;  | 
77  | 5.78k  |   id->key = privkey_or_die(privkey);  | 
78  | 5.78k  |   cert = pubkey_or_die(certpath);  | 
79  | 5.78k  |   if ((r = sshkey_to_certified(id->key)) != 0) { | 
80  | 0  |     error_fr(r, "sshkey_to_certified");  | 
81  | 0  |     abort();  | 
82  | 0  |   }  | 
83  | 5.78k  |   if ((r = sshkey_cert_copy(cert, id->key)) != 0) { | 
84  | 0  |     error_fr(r, "sshkey_cert_copy");  | 
85  | 0  |     abort();  | 
86  | 0  |   }  | 
87  | 5.78k  |   sshkey_free(cert);  | 
88  | 5.78k  |   id->comment = xstrdup("outis"); | 
89  | 5.78k  |   if (sshkey_is_sk(id->key))  | 
90  | 2.31k  |     id->sk_provider = xstrdup("internal"); | 
91  | 5.78k  | }  | 
92  |  |  | 
93  |  | static void  | 
94  |  | cleanup_idtab(void)  | 
95  | 2.31k  | { | 
96  | 2.31k  |   Identity *id;  | 
97  |  |  | 
98  | 2.31k  |   if (idtab == NULL) return;  | 
99  | 12.6k  |   for (id = TAILQ_FIRST(&idtab->idlist); id;  | 
100  | 11.5k  |       id = TAILQ_FIRST(&idtab->idlist)) { | 
101  | 11.5k  |     TAILQ_REMOVE(&idtab->idlist, id, next);  | 
102  | 11.5k  |     free_identity(id);  | 
103  | 11.5k  |   }  | 
104  | 1.15k  |   free(idtab);  | 
105  | 1.15k  |   idtab = NULL;  | 
106  | 1.15k  | }  | 
107  |  |  | 
108  |  | static void  | 
109  |  | reset_idtab(void)  | 
110  | 1.15k  | { | 
111  | 1.15k  |   cleanup_idtab();  | 
112  | 1.15k  |   idtab_init();  | 
113  |  |   // Load keys.  | 
114  | 1.15k  |   add_key(PRIV_RSA, CERT_RSA);  | 
115  | 1.15k  |   add_key(PRIV_ECDSA, CERT_ECDSA);  | 
116  | 1.15k  |   add_key(PRIV_ED25519, CERT_ED25519);  | 
117  | 1.15k  |   add_key(PRIV_ECDSA_SK, CERT_ECDSA_SK);  | 
118  | 1.15k  |   add_key(PRIV_ED25519_SK, CERT_ED25519_SK);  | 
119  | 1.15k  | }  | 
120  |  |  | 
121  |  | static void  | 
122  |  | cleanup_sockettab(void)  | 
123  | 2.31k  | { | 
124  | 2.31k  |   u_int i;  | 
125  | 13.8k  |   for (i = 0; i < sockets_alloc; i++) { | 
126  | 11.5k  |     if (sockets[i].type != AUTH_UNUSED)  | 
127  | 1.15k  |       close_socket(sockets + i);  | 
128  | 11.5k  |   }  | 
129  | 2.31k  |   free(sockets);  | 
130  | 2.31k  |   sockets = NULL;  | 
131  | 2.31k  |   sockets_alloc = 0;  | 
132  | 2.31k  | }  | 
133  |  |  | 
134  |  | static void  | 
135  |  | reset_sockettab(int devnull)  | 
136  | 1.15k  | { | 
137  | 1.15k  |   int fd;  | 
138  |  |  | 
139  | 1.15k  |   cleanup_sockettab();  | 
140  | 1.15k  |   if ((fd = dup(devnull)) == -1) { | 
141  | 0  |     error_f("dup: %s", strerror(errno)); | 
142  | 0  |     abort();  | 
143  | 0  |   }  | 
144  | 1.15k  |   new_socket(AUTH_CONNECTION, fd);  | 
145  | 1.15k  |   assert(sockets[0].type == AUTH_CONNECTION);  | 
146  | 1.15k  |   assert(sockets[0].fd == fd);  | 
147  | 1.15k  | }  | 
148  |  |  | 
149  | 90.1k  | #define MAX_MESSAGES 256  | 
150  |  | void  | 
151  |  | test_one(const uint8_t* s, size_t slen)  | 
152  | 1.15k  | { | 
153  | 1.15k  |   static int devnull = -1;  | 
154  | 1.15k  |   size_t i, olen, nlen;  | 
155  |  |  | 
156  | 1.15k  |   if (devnull == -1) { | 
157  | 1  |     log_init(__progname, SYSLOG_LEVEL_DEBUG3,  | 
158  | 1  |         SYSLOG_FACILITY_AUTH, 1);  | 
159  | 1  |     devnull = devnull_or_die();  | 
160  | 1  |     allowed_providers = xstrdup(""); | 
161  | 1  |     websafe_allowlist = xstrdup("*"); | 
162  | 1  |     setenv("DISPLAY", "", 1); /* ban askpass */ | 
163  | 1  |   }  | 
164  |  |  | 
165  | 1.15k  |   reset_idtab();  | 
166  | 1.15k  |   reset_sockettab(devnull);  | 
167  | 1.15k  |   (void)sshbuf_put(sockets[0].input, s, slen);  | 
168  | 90.1k  |   for (i = 0; i < MAX_MESSAGES; i++) { | 
169  | 90.0k  |     olen = sshbuf_len(sockets[0].input);  | 
170  | 90.0k  |     process_message(0);  | 
171  | 90.0k  |     nlen = sshbuf_len(sockets[0].input);  | 
172  | 90.0k  |     if (nlen == 0 || nlen == olen)  | 
173  | 1.08k  |       break;  | 
174  | 90.0k  |   }  | 
175  | 1.15k  |   cleanup_idtab();  | 
176  | 1.15k  |   cleanup_sockettab();  | 
177  | 1.15k  | }  |