fuzz_client.c 5.5 KB

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