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