fuzz_dtlsclient.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include "common.h"
  6. #include "mbedtls/ssl.h"
  7. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  8. #include "mbedtls/entropy.h"
  9. #include "mbedtls/ctr_drbg.h"
  10. #include "mbedtls/timing.h"
  11. #include "test/certs.h"
  12. #if defined(MBEDTLS_SSL_CLI_C) && \
  13. defined(MBEDTLS_ENTROPY_C) && \
  14. defined(MBEDTLS_CTR_DRBG_C) && \
  15. defined(MBEDTLS_TIMING_C)
  16. static int initialized = 0;
  17. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  18. static mbedtls_x509_crt cacert;
  19. #endif
  20. const char *pers = "fuzz_dtlsclient";
  21. #endif
  22. #endif // MBEDTLS_SSL_PROTO_DTLS
  23. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  24. {
  25. #if defined(MBEDTLS_SSL_PROTO_DTLS) && \
  26. defined(MBEDTLS_SSL_CLI_C) && \
  27. defined(MBEDTLS_ENTROPY_C) && \
  28. defined(MBEDTLS_CTR_DRBG_C) && \
  29. defined(MBEDTLS_TIMING_C)
  30. int ret;
  31. size_t len;
  32. mbedtls_ssl_context ssl;
  33. mbedtls_ssl_config conf;
  34. mbedtls_ctr_drbg_context ctr_drbg;
  35. mbedtls_entropy_context entropy;
  36. mbedtls_timing_delay_context timer;
  37. unsigned char buf[4096];
  38. fuzzBufferOffset_t biomemfuzz;
  39. if (initialized == 0) {
  40. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  41. mbedtls_x509_crt_init(&cacert);
  42. if (mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
  43. mbedtls_test_cas_pem_len) != 0) {
  44. return 1;
  45. }
  46. #endif
  47. dummy_init();
  48. initialized = 1;
  49. }
  50. mbedtls_ssl_init(&ssl);
  51. mbedtls_ssl_config_init(&conf);
  52. mbedtls_ctr_drbg_init(&ctr_drbg);
  53. mbedtls_entropy_init(&entropy);
  54. srand(1);
  55. if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
  56. (const unsigned char *) pers, strlen(pers)) != 0) {
  57. goto exit;
  58. }
  59. if (mbedtls_ssl_config_defaults(&conf,
  60. MBEDTLS_SSL_IS_CLIENT,
  61. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  62. MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
  63. goto exit;
  64. }
  65. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  66. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  67. #endif
  68. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
  69. mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
  70. if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
  71. goto exit;
  72. }
  73. mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
  74. mbedtls_timing_get_delay);
  75. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  76. if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
  77. goto exit;
  78. }
  79. #endif
  80. biomemfuzz.Data = Data;
  81. biomemfuzz.Size = Size;
  82. biomemfuzz.Offset = 0;
  83. mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout);
  84. ret = mbedtls_ssl_handshake(&ssl);
  85. if (ret == 0) {
  86. //keep reading data from server until the end
  87. do {
  88. len = sizeof(buf) - 1;
  89. ret = mbedtls_ssl_read(&ssl, buf, len);
  90. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  91. continue;
  92. } else if (ret <= 0) {
  93. //EOF or error
  94. break;
  95. }
  96. } while (1);
  97. }
  98. exit:
  99. mbedtls_entropy_free(&entropy);
  100. mbedtls_ctr_drbg_free(&ctr_drbg);
  101. mbedtls_ssl_config_free(&conf);
  102. mbedtls_ssl_free(&ssl);
  103. #else
  104. (void) Data;
  105. (void) Size;
  106. #endif
  107. return 0;
  108. }