sig_fuzz.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // cc_fuzz_target test for public key parsing.
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. extern "C" {
  8. #include "includes.h"
  9. #include "sshkey.h"
  10. #include "ssherr.h"
  11. static struct sshkey *generate_or_die(int type, unsigned bits) {
  12. int r;
  13. struct sshkey *ret;
  14. if ((r = sshkey_generate(type, bits, &ret)) != 0) {
  15. fprintf(stderr, "generate(%d, %u): %s", type, bits, ssh_err(r));
  16. abort();
  17. }
  18. return ret;
  19. }
  20. int LLVMFuzzerTestOneInput(const uint8_t* sig, size_t slen)
  21. {
  22. #ifdef WITH_OPENSSL
  23. static struct sshkey *rsa = generate_or_die(KEY_RSA, 2048);
  24. static struct sshkey *dsa = generate_or_die(KEY_DSA, 1024);
  25. static struct sshkey *ecdsa256 = generate_or_die(KEY_ECDSA, 256);
  26. static struct sshkey *ecdsa384 = generate_or_die(KEY_ECDSA, 384);
  27. static struct sshkey *ecdsa521 = generate_or_die(KEY_ECDSA, 521);
  28. #endif
  29. struct sshkey_sig_details *details = NULL;
  30. static struct sshkey *ed25519 = generate_or_die(KEY_ED25519, 0);
  31. static const char *data = "If everyone started announcing his nose had "
  32. "run away, I don’t know how it would all end";
  33. static const size_t dlen = strlen(data);
  34. #ifdef WITH_OPENSSL
  35. sshkey_verify(rsa, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
  36. sshkey_sig_details_free(details);
  37. details = NULL;
  38. sshkey_verify(dsa, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
  39. sshkey_sig_details_free(details);
  40. details = NULL;
  41. sshkey_verify(ecdsa256, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
  42. sshkey_sig_details_free(details);
  43. details = NULL;
  44. sshkey_verify(ecdsa384, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
  45. sshkey_sig_details_free(details);
  46. details = NULL;
  47. sshkey_verify(ecdsa521, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
  48. sshkey_sig_details_free(details);
  49. details = NULL;
  50. #endif
  51. sshkey_verify(ed25519, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
  52. sshkey_sig_details_free(details);
  53. return 0;
  54. }
  55. } // extern