fuzz_server.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "mbedtls/ssl.h"
  2. #include "mbedtls/entropy.h"
  3. #include "mbedtls/ctr_drbg.h"
  4. #include "mbedtls/certs.h"
  5. #include "mbedtls/ssl_ticket.h"
  6. #include "common.h"
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #if defined(MBEDTLS_SSL_SRV_C) && \
  11. defined(MBEDTLS_ENTROPY_C) && \
  12. defined(MBEDTLS_CTR_DRBG_C)
  13. const char *pers = "fuzz_server";
  14. static int initialized = 0;
  15. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  16. static mbedtls_x509_crt srvcert;
  17. static mbedtls_pk_context pkey;
  18. #endif
  19. const char *alpn_list[3];
  20. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  21. const unsigned char psk[] = {
  22. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  24. };
  25. const char psk_id[] = "Client_identity";
  26. #endif
  27. #endif // MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C
  28. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  29. #if defined(MBEDTLS_SSL_SRV_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. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  39. mbedtls_ssl_ticket_context ticket_ctx;
  40. #endif
  41. unsigned char buf[4096];
  42. fuzzBufferOffset_t biomemfuzz;
  43. uint8_t options;
  44. //we take 1 byte as options input
  45. if (Size < 1) {
  46. return 0;
  47. }
  48. options = Data[Size - 1];
  49. if (initialized == 0) {
  50. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  51. mbedtls_x509_crt_init( &srvcert );
  52. mbedtls_pk_init( &pkey );
  53. if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  54. mbedtls_test_srv_crt_len ) != 0)
  55. return 1;
  56. if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
  57. mbedtls_test_cas_pem_len ) != 0)
  58. return 1;
  59. if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
  60. mbedtls_test_srv_key_len, NULL, 0 ) != 0)
  61. return 1;
  62. #endif
  63. alpn_list[0] = "HTTP";
  64. alpn_list[1] = "fuzzalpn";
  65. alpn_list[2] = NULL;
  66. dummy_init();
  67. initialized = 1;
  68. }
  69. mbedtls_ssl_init( &ssl );
  70. mbedtls_ssl_config_init( &conf );
  71. mbedtls_ctr_drbg_init( &ctr_drbg );
  72. mbedtls_entropy_init( &entropy );
  73. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  74. mbedtls_ssl_ticket_init( &ticket_ctx );
  75. #endif
  76. if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
  77. (const unsigned char *) pers, strlen( pers ) ) != 0 )
  78. goto exit;
  79. if( mbedtls_ssl_config_defaults( &conf,
  80. MBEDTLS_SSL_IS_SERVER,
  81. MBEDTLS_SSL_TRANSPORT_STREAM,
  82. MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
  83. goto exit;
  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. #endif
  91. mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED );
  92. #if defined(MBEDTLS_SSL_ALPN)
  93. if (options & 0x2) {
  94. mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
  95. }
  96. #endif
  97. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  98. if( options & 0x4 )
  99. {
  100. if( mbedtls_ssl_ticket_setup( &ticket_ctx,
  101. dummy_random, &ctr_drbg,
  102. MBEDTLS_CIPHER_AES_256_GCM,
  103. 86400 ) != 0 )
  104. goto exit;
  105. mbedtls_ssl_conf_session_tickets_cb( &conf,
  106. mbedtls_ssl_ticket_write,
  107. mbedtls_ssl_ticket_parse,
  108. &ticket_ctx );
  109. }
  110. #endif
  111. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  112. mbedtls_ssl_conf_truncated_hmac( &conf, (options & 0x8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
  113. #endif
  114. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  115. mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
  116. #endif
  117. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  118. mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
  119. #endif
  120. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  121. if (options & 0x40) {
  122. mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
  123. (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
  124. }
  125. #endif
  126. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  127. mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
  128. #endif
  129. if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
  130. goto exit;
  131. biomemfuzz.Data = Data;
  132. biomemfuzz.Size = Size-1;
  133. biomemfuzz.Offset = 0;
  134. mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
  135. mbedtls_ssl_session_reset( &ssl );
  136. ret = mbedtls_ssl_handshake( &ssl );
  137. if( ret == 0 )
  138. {
  139. //keep reading data from server until the end
  140. do
  141. {
  142. len = sizeof( buf ) - 1;
  143. ret = mbedtls_ssl_read( &ssl, buf, len );
  144. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  145. continue;
  146. else if( ret <= 0 )
  147. //EOF or error
  148. break;
  149. }
  150. while( 1 );
  151. }
  152. exit:
  153. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  154. mbedtls_ssl_ticket_free( &ticket_ctx );
  155. #endif
  156. mbedtls_entropy_free( &entropy );
  157. mbedtls_ctr_drbg_free( &ctr_drbg );
  158. mbedtls_ssl_config_free( &conf );
  159. mbedtls_ssl_free( &ssl );
  160. #else
  161. (void) Data;
  162. (void) Size;
  163. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
  164. return 0;
  165. }