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