Mateusz Starzyk | 6c2e9b6 | 2021-05-19 17:54:54 +0200 | [diff] [blame] | 1 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 2 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 5 | #include <stdint.h> |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 6 | #include "common.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 7 | #include "mbedtls/ssl.h" |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 8 | #include "test/certs.h" |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame^] | 9 | #include "test/random.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11 | #include "mbedtls/entropy.h" |
| 12 | #include "mbedtls/ctr_drbg.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 13 | #include "mbedtls/timing.h" |
| 14 | #include "mbedtls/ssl_cookie.h" |
| 15 | |
| 16 | |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 17 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 18 | defined(MBEDTLS_ENTROPY_C) && \ |
| 19 | defined(MBEDTLS_CTR_DRBG_C) && \ |
| 20 | defined(MBEDTLS_TIMING_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 21 | const char *pers = "fuzz_dtlsserver"; |
| 22 | const unsigned char client_ip[4] = {0x7F, 0, 0, 1}; |
Philippe Antoine | 42a2ce8 | 2019-07-10 14:26:31 +0200 | [diff] [blame] | 23 | static int initialized = 0; |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 25 | static mbedtls_x509_crt srvcert; |
| 26 | static mbedtls_pk_context pkey; |
| 27 | #endif |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 28 | #endif |
Philippe Antoine | adc23e6 | 2019-06-25 21:53:12 +0200 | [diff] [blame] | 29 | #endif // MBEDTLS_SSL_PROTO_DTLS |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 30 | |
| 31 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && \ |
| 33 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 34 | defined(MBEDTLS_ENTROPY_C) && \ |
| 35 | defined(MBEDTLS_CTR_DRBG_C) && \ |
| 36 | defined(MBEDTLS_TIMING_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 37 | int ret; |
| 38 | size_t len; |
| 39 | mbedtls_ssl_context ssl; |
| 40 | mbedtls_ssl_config conf; |
| 41 | mbedtls_ctr_drbg_context ctr_drbg; |
| 42 | mbedtls_entropy_context entropy; |
| 43 | mbedtls_timing_delay_context timer; |
| 44 | mbedtls_ssl_cookie_ctx cookie_ctx; |
| 45 | unsigned char buf[4096]; |
| 46 | fuzzBufferOffset_t biomemfuzz; |
| 47 | |
| 48 | if (initialized == 0) { |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 50 | mbedtls_x509_crt_init( &srvcert ); |
| 51 | mbedtls_pk_init( &pkey ); |
| 52 | if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 53 | mbedtls_test_srv_crt_len ) != 0) |
| 54 | return 1; |
| 55 | if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 56 | mbedtls_test_cas_pem_len ) != 0) |
| 57 | return 1; |
| 58 | if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame^] | 59 | mbedtls_test_srv_key_len, NULL, 0, |
| 60 | mbedtls_test_rnd_std_rand, NULL ) != 0) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 61 | return 1; |
| 62 | #endif |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 63 | dummy_init(); |
| 64 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 65 | initialized = 1; |
| 66 | } |
| 67 | mbedtls_ssl_init( &ssl ); |
| 68 | mbedtls_ssl_config_init( &conf ); |
| 69 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 70 | mbedtls_entropy_init( &entropy ); |
| 71 | mbedtls_ssl_cookie_init( &cookie_ctx ); |
| 72 | |
| 73 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy, |
| 74 | (const unsigned char *) pers, strlen( pers ) ) != 0 ) |
| 75 | goto exit; |
| 76 | |
| 77 | |
| 78 | if( mbedtls_ssl_config_defaults( &conf, |
| 79 | MBEDTLS_SSL_IS_SERVER, |
| 80 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 81 | MBEDTLS_SSL_PRESET_DEFAULT ) != 0 ) |
| 82 | goto exit; |
| 83 | |
| 84 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 85 | srand(1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 86 | mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg ); |
| 87 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 88 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 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 | |
| 94 | if( mbedtls_ssl_cookie_setup( &cookie_ctx, dummy_random, &ctr_drbg ) != 0 ) |
| 95 | goto exit; |
| 96 | |
| 97 | mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &cookie_ctx ); |
| 98 | |
| 99 | if( mbedtls_ssl_setup( &ssl, &conf ) != 0 ) |
| 100 | goto exit; |
| 101 | |
| 102 | mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, |
| 103 | mbedtls_timing_get_delay ); |
| 104 | |
| 105 | biomemfuzz.Data = Data; |
| 106 | biomemfuzz.Size = Size; |
| 107 | biomemfuzz.Offset = 0; |
| 108 | mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout ); |
| 109 | if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 ) |
| 110 | goto exit; |
| 111 | |
| 112 | ret = mbedtls_ssl_handshake( &ssl ); |
| 113 | |
| 114 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
| 115 | biomemfuzz.Offset = ssl.next_record_offset; |
| 116 | mbedtls_ssl_session_reset( &ssl ); |
| 117 | mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout ); |
| 118 | if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 ) |
| 119 | goto exit; |
| 120 | |
| 121 | ret = mbedtls_ssl_handshake( &ssl ); |
| 122 | |
| 123 | if( ret == 0 ) |
| 124 | { |
| 125 | //keep reading data from server until the end |
| 126 | do |
| 127 | { |
| 128 | len = sizeof( buf ) - 1; |
| 129 | ret = mbedtls_ssl_read( &ssl, buf, len ); |
| 130 | if( ret == MBEDTLS_ERR_SSL_WANT_READ ) |
| 131 | continue; |
| 132 | else if( ret <= 0 ) |
| 133 | //EOF or error |
| 134 | break; |
| 135 | } |
| 136 | while( 1 ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | exit: |
| 141 | mbedtls_ssl_cookie_free( &cookie_ctx ); |
| 142 | mbedtls_entropy_free( &entropy ); |
| 143 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 144 | mbedtls_ssl_config_free( &conf ); |
| 145 | mbedtls_ssl_free( &ssl ); |
| 146 | |
| 147 | #else |
| 148 | (void) Data; |
| 149 | (void) Size; |
| 150 | #endif |
| 151 | return 0; |
| 152 | } |