fuzz_dtlsserver.c 4.7 KB

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