agent_fuzz_helper.c 3.5 KB

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