fuzz_server.c 6.0 KB

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