fuzz_server.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. {
  31. #if defined(MBEDTLS_SSL_SRV_C) && \
  32. defined(MBEDTLS_ENTROPY_C) && \
  33. defined(MBEDTLS_CTR_DRBG_C)
  34. int ret;
  35. size_t len;
  36. mbedtls_ssl_context ssl;
  37. mbedtls_ssl_config conf;
  38. mbedtls_ctr_drbg_context ctr_drbg;
  39. mbedtls_entropy_context entropy;
  40. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
  41. mbedtls_ssl_ticket_context ticket_ctx;
  42. #endif
  43. unsigned char buf[4096];
  44. fuzzBufferOffset_t biomemfuzz;
  45. uint8_t options;
  46. //we take 1 byte as options input
  47. if (Size < 1) {
  48. return 0;
  49. }
  50. options = Data[Size - 1];
  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. }
  57. if (initialized == 0) {
  58. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  59. mbedtls_x509_crt_init(&srvcert);
  60. mbedtls_pk_init(&pkey);
  61. if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  62. mbedtls_test_srv_crt_len) != 0) {
  63. return 1;
  64. }
  65. if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
  66. mbedtls_test_cas_pem_len) != 0) {
  67. return 1;
  68. }
  69. if (mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
  70. mbedtls_test_srv_key_len, NULL, 0,
  71. dummy_random, &ctr_drbg) != 0) {
  72. return 1;
  73. }
  74. #endif
  75. alpn_list[0] = "HTTP";
  76. alpn_list[1] = "fuzzalpn";
  77. alpn_list[2] = NULL;
  78. dummy_init();
  79. initialized = 1;
  80. }
  81. mbedtls_ssl_init(&ssl);
  82. mbedtls_ssl_config_init(&conf);
  83. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
  84. mbedtls_ssl_ticket_init(&ticket_ctx);
  85. #endif
  86. if (mbedtls_ssl_config_defaults(&conf,
  87. MBEDTLS_SSL_IS_SERVER,
  88. MBEDTLS_SSL_TRANSPORT_STREAM,
  89. MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
  90. goto exit;
  91. }
  92. srand(1);
  93. mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
  94. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
  95. mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
  96. if (mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey) != 0) {
  97. goto exit;
  98. }
  99. #endif
  100. mbedtls_ssl_conf_cert_req_ca_list(&conf,
  101. (options &
  102. 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
  103. #if defined(MBEDTLS_SSL_ALPN)
  104. if (options & 0x2) {
  105. mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
  106. }
  107. #endif
  108. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
  109. if (options & 0x4) {
  110. if (mbedtls_ssl_ticket_setup(&ticket_ctx,
  111. dummy_random, &ctr_drbg,
  112. MBEDTLS_CIPHER_AES_256_GCM,
  113. 86400) != 0) {
  114. goto exit;
  115. }
  116. mbedtls_ssl_conf_session_tickets_cb(&conf,
  117. mbedtls_ssl_ticket_write,
  118. mbedtls_ssl_ticket_parse,
  119. &ticket_ctx);
  120. }
  121. #endif
  122. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  123. mbedtls_ssl_conf_extended_master_secret(&conf,
  124. (options &
  125. 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
  126. #endif
  127. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  128. mbedtls_ssl_conf_encrypt_then_mac(&conf,
  129. (options &
  130. 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
  131. #endif
  132. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  133. if (options & 0x40) {
  134. mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
  135. (const unsigned char *) psk_id, sizeof(psk_id) - 1);
  136. }
  137. #endif
  138. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  139. mbedtls_ssl_conf_renegotiation(&conf,
  140. (options &
  141. 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
  142. #endif
  143. if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
  144. goto exit;
  145. }
  146. biomemfuzz.Data = Data;
  147. biomemfuzz.Size = Size-1;
  148. biomemfuzz.Offset = 0;
  149. mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
  150. mbedtls_ssl_session_reset(&ssl);
  151. ret = mbedtls_ssl_handshake(&ssl);
  152. if (ret == 0) {
  153. //keep reading data from server until the end
  154. do {
  155. len = sizeof(buf) - 1;
  156. ret = mbedtls_ssl_read(&ssl, buf, len);
  157. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  158. continue;
  159. } else if (ret <= 0) {
  160. //EOF or error
  161. break;
  162. }
  163. } while (1);
  164. }
  165. exit:
  166. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
  167. mbedtls_ssl_ticket_free(&ticket_ctx);
  168. #endif
  169. mbedtls_entropy_free(&entropy);
  170. mbedtls_ctr_drbg_free(&ctr_drbg);
  171. mbedtls_ssl_config_free(&conf);
  172. mbedtls_ssl_free(&ssl);
  173. #else
  174. (void) Data;
  175. (void) Size;
  176. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
  177. return 0;
  178. }