fuzz_dtlsserver.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "test/certs.h"
  8. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  9. #include "mbedtls/entropy.h"
  10. #include "mbedtls/ctr_drbg.h"
  11. #include "mbedtls/timing.h"
  12. #include "mbedtls/ssl_cookie.h"
  13. #if defined(MBEDTLS_SSL_SRV_C) && \
  14. defined(MBEDTLS_ENTROPY_C) && \
  15. defined(MBEDTLS_CTR_DRBG_C) && \
  16. defined(MBEDTLS_TIMING_C)
  17. const char *pers = "fuzz_dtlsserver";
  18. const unsigned char client_ip[4] = {0x7F, 0, 0, 1};
  19. static int initialized = 0;
  20. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  21. static mbedtls_x509_crt srvcert;
  22. static mbedtls_pk_context pkey;
  23. #endif
  24. #endif
  25. #endif // MBEDTLS_SSL_PROTO_DTLS
  26. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  27. #if defined(MBEDTLS_SSL_PROTO_DTLS) && \
  28. defined(MBEDTLS_SSL_SRV_C) && \
  29. defined(MBEDTLS_ENTROPY_C) && \
  30. defined(MBEDTLS_CTR_DRBG_C) && \
  31. defined(MBEDTLS_TIMING_C)
  32. int ret;
  33. size_t len;
  34. mbedtls_ssl_context ssl;
  35. mbedtls_ssl_config conf;
  36. mbedtls_ctr_drbg_context ctr_drbg;
  37. mbedtls_entropy_context entropy;
  38. mbedtls_timing_delay_context timer;
  39. mbedtls_ssl_cookie_ctx cookie_ctx;
  40. unsigned char buf[4096];
  41. fuzzBufferOffset_t biomemfuzz;
  42. if (initialized == 0) {
  43. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  44. mbedtls_x509_crt_init( &srvcert );
  45. mbedtls_pk_init( &pkey );
  46. if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  47. mbedtls_test_srv_crt_len ) != 0)
  48. return 1;
  49. if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
  50. mbedtls_test_cas_pem_len ) != 0)
  51. return 1;
  52. if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
  53. mbedtls_test_srv_key_len, NULL, 0,
  54. dummy_random, NULL ) != 0)
  55. return 1;
  56. #endif
  57. dummy_init();
  58. initialized = 1;
  59. }
  60. mbedtls_ssl_init( &ssl );
  61. mbedtls_ssl_config_init( &conf );
  62. mbedtls_ctr_drbg_init( &ctr_drbg );
  63. mbedtls_entropy_init( &entropy );
  64. mbedtls_ssl_cookie_init( &cookie_ctx );
  65. if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
  66. (const unsigned char *) pers, strlen( pers ) ) != 0 )
  67. goto exit;
  68. if( mbedtls_ssl_config_defaults( &conf,
  69. MBEDTLS_SSL_IS_SERVER,
  70. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  71. MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
  72. goto exit;
  73. srand(1);
  74. mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
  75. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  76. mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
  77. if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
  78. goto exit;
  79. #endif
  80. if( mbedtls_ssl_cookie_setup( &cookie_ctx, dummy_random, &ctr_drbg ) != 0 )
  81. goto exit;
  82. mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &cookie_ctx );
  83. if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
  84. goto exit;
  85. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  86. mbedtls_timing_get_delay );
  87. biomemfuzz.Data = Data;
  88. biomemfuzz.Size = Size;
  89. biomemfuzz.Offset = 0;
  90. mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
  91. if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
  92. goto exit;
  93. ret = mbedtls_ssl_handshake( &ssl );
  94. if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
  95. biomemfuzz.Offset = ssl.next_record_offset;
  96. mbedtls_ssl_session_reset( &ssl );
  97. mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
  98. if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
  99. goto exit;
  100. ret = mbedtls_ssl_handshake( &ssl );
  101. if( ret == 0 )
  102. {
  103. //keep reading data from server until the end
  104. do
  105. {
  106. len = sizeof( buf ) - 1;
  107. ret = mbedtls_ssl_read( &ssl, buf, len );
  108. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  109. continue;
  110. else if( ret <= 0 )
  111. //EOF or error
  112. break;
  113. }
  114. while( 1 );
  115. }
  116. }
  117. exit:
  118. mbedtls_ssl_cookie_free( &cookie_ctx );
  119. mbedtls_entropy_free( &entropy );
  120. mbedtls_ctr_drbg_free( &ctr_drbg );
  121. mbedtls_ssl_config_free( &conf );
  122. mbedtls_ssl_free( &ssl );
  123. #else
  124. (void) Data;
  125. (void) Size;
  126. #endif
  127. return 0;
  128. }