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