fuzz_dtlsclient.c 3.5 KB

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