fuzz_dtlsclient.c 3.5 KB

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