fuzzer-verify.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "fuzz.h"
  2. #include "session.h"
  3. #include "fuzz-wrapfd.h"
  4. #include "debug.h"
  5. #include "dss.h"
  6. static void setup_fuzzer(void) {
  7. fuzz_common_setup();
  8. }
  9. static buffer *verifydata;
  10. /* Tests reading a public key and verifying a signature */
  11. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  12. static int once = 0;
  13. if (!once) {
  14. setup_fuzzer();
  15. verifydata = buf_new(30);
  16. buf_putstring(verifydata, "x", 1);
  17. once = 1;
  18. }
  19. if (fuzz_set_input(Data, Size) == DROPBEAR_FAILURE) {
  20. return 0;
  21. }
  22. m_malloc_set_epoch(1);
  23. if (setjmp(fuzz.jmp) == 0) {
  24. sign_key *key = new_sign_key();
  25. enum signkey_type keytype = DROPBEAR_SIGNKEY_ANY;
  26. if (buf_get_pub_key(fuzz.input, key, &keytype) == DROPBEAR_SUCCESS) {
  27. enum signature_type sigtype;
  28. if (keytype == DROPBEAR_SIGNKEY_RSA) {
  29. /* Flip a coin to decide rsa signature type */
  30. int flag = buf_getbyte(fuzz.input);
  31. if (flag & 0x01) {
  32. sigtype = DROPBEAR_SIGNATURE_RSA_SHA256;
  33. } else {
  34. sigtype = DROPBEAR_SIGNATURE_RSA_SHA1;
  35. }
  36. } else {
  37. sigtype = signature_type_from_signkey(keytype);
  38. }
  39. if (buf_verify(fuzz.input, key, sigtype, verifydata) == DROPBEAR_SUCCESS) {
  40. /* The fuzzer is capable of generating keys with a signature to match.
  41. We don't want false positives if the key is bogus, since a client/server
  42. wouldn't be trusting a bogus key anyway */
  43. int boguskey = 0;
  44. if (keytype == DROPBEAR_SIGNKEY_DSS) {
  45. /* So far have seen dss keys with bad p/q/g domain parameters */
  46. int pprime, qprime, trials;
  47. trials = mp_prime_rabin_miller_trials(mp_count_bits(key->dsskey->p));
  48. assert(mp_prime_is_prime(key->dsskey->p, trials, &pprime) == MP_OKAY);
  49. trials = mp_prime_rabin_miller_trials(mp_count_bits(key->dsskey->q));
  50. assert(mp_prime_is_prime(key->dsskey->q, trials, &qprime) == MP_OKAY);
  51. boguskey = !(pprime && qprime);
  52. /* Could also check g**q mod p == 1 */
  53. }
  54. if (!boguskey) {
  55. printf("Random key/signature managed to verify!\n");
  56. abort();
  57. }
  58. }
  59. }
  60. sign_key_free(key);
  61. m_malloc_free_epoch(1, 0);
  62. } else {
  63. m_malloc_free_epoch(1, 1);
  64. TRACE(("dropbear_exit longjmped"))
  65. /* dropbear_exit jumped here */
  66. }
  67. return 0;
  68. }