fuzz_client.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  2. #include "mbedtls/ssl.h"
  3. #include "mbedtls/entropy.h"
  4. #include "mbedtls/ctr_drbg.h"
  5. #include "test/certs.h"
  6. #include "common.h"
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #if defined(MBEDTLS_SSL_CLI_C) && \
  11. defined(MBEDTLS_ENTROPY_C) && \
  12. defined(MBEDTLS_CTR_DRBG_C)
  13. static int initialized = 0;
  14. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  15. static mbedtls_x509_crt cacert;
  16. #endif
  17. const char *alpn_list[3];
  18. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  19. const unsigned char psk[] = {
  20. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  22. };
  23. const char psk_id[] = "Client_identity";
  24. #endif
  25. const char *pers = "fuzz_client";
  26. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
  27. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  28. {
  29. #if defined(MBEDTLS_SSL_CLI_C) && \
  30. defined(MBEDTLS_ENTROPY_C) && \
  31. defined(MBEDTLS_CTR_DRBG_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. unsigned char buf[4096];
  39. fuzzBufferOffset_t biomemfuzz;
  40. uint16_t options;
  41. if (initialized == 0) {
  42. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  43. mbedtls_x509_crt_init(&cacert);
  44. if (mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
  45. mbedtls_test_cas_pem_len) != 0) {
  46. return 1;
  47. }
  48. #endif
  49. alpn_list[0] = "HTTP";
  50. alpn_list[1] = "fuzzalpn";
  51. alpn_list[2] = NULL;
  52. dummy_init();
  53. initialized = 1;
  54. }
  55. //we take 1 byte as options input
  56. if (Size < 2) {
  57. return 0;
  58. }
  59. options = (Data[Size - 2] << 8) | Data[Size - 1];
  60. //Avoid warnings if compile options imply no options
  61. (void) options;
  62. mbedtls_ssl_init(&ssl);
  63. mbedtls_ssl_config_init(&conf);
  64. mbedtls_ctr_drbg_init(&ctr_drbg);
  65. mbedtls_entropy_init(&entropy);
  66. if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
  67. (const unsigned char *) pers, strlen(pers)) != 0) {
  68. goto exit;
  69. }
  70. if (mbedtls_ssl_config_defaults(&conf,
  71. MBEDTLS_SSL_IS_CLIENT,
  72. MBEDTLS_SSL_TRANSPORT_STREAM,
  73. MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
  74. goto exit;
  75. }
  76. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  77. if (options & 2) {
  78. mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
  79. (const unsigned char *) psk_id, sizeof(psk_id) - 1);
  80. }
  81. #endif
  82. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  83. if (options & 4) {
  84. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  85. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
  86. } else
  87. #endif
  88. {
  89. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
  90. }
  91. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  92. mbedtls_ssl_conf_extended_master_secret(&conf,
  93. (options &
  94. 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
  95. #endif
  96. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  97. mbedtls_ssl_conf_encrypt_then_mac(&conf,
  98. (options &
  99. 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
  100. #endif
  101. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  102. mbedtls_ssl_conf_renegotiation(&conf,
  103. (options &
  104. 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
  105. #endif
  106. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  107. mbedtls_ssl_conf_session_tickets(&conf,
  108. (options &
  109. 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
  110. #endif
  111. #if defined(MBEDTLS_SSL_ALPN)
  112. if (options & 0x200) {
  113. mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
  114. }
  115. #endif
  116. //There may be other options to add :
  117. // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
  118. srand(1);
  119. mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
  120. if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
  121. goto exit;
  122. }
  123. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  124. if ((options & 1) == 0) {
  125. if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
  126. goto exit;
  127. }
  128. }
  129. #endif
  130. biomemfuzz.Data = Data;
  131. biomemfuzz.Size = Size-2;
  132. biomemfuzz.Offset = 0;
  133. mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
  134. ret = mbedtls_ssl_handshake(&ssl);
  135. if (ret == 0) {
  136. //keep reading data from server until the end
  137. do {
  138. len = sizeof(buf) - 1;
  139. ret = mbedtls_ssl_read(&ssl, buf, len);
  140. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  141. continue;
  142. } else if (ret <= 0) {
  143. //EOF or error
  144. break;
  145. }
  146. } while (1);
  147. }
  148. exit:
  149. mbedtls_entropy_free(&entropy);
  150. mbedtls_ctr_drbg_free(&ctr_drbg);
  151. mbedtls_ssl_config_free(&conf);
  152. mbedtls_ssl_free(&ssl);
  153. #else
  154. (void) Data;
  155. (void) Size;
  156. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
  157. return 0;
  158. }