fuzz_dtlsserver.c 5.2 KB

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