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 | |
| 13 | |
Philippe Antoine | adc23e6 | 2019-06-25 21:53:12 +0200 | [diff] [blame] | 14 | #ifdef MBEDTLS_SSL_CLI_C |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 15 | static bool initialized = 0; |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 16 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 17 | static mbedtls_x509_crt cacert; |
| 18 | #endif |
| 19 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 20 | const char *pers = "fuzz_dtlsclient"; |
Philippe Antoine | adc23e6 | 2019-06-25 21:53:12 +0200 | [diff] [blame] | 21 | #endif // MBEDTLS_SSL_CLI_C |
| 22 | #endif // MBEDTLS_SSL_PROTO_DTLS |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 23 | |
| 24 | |
| 25 | |
| 26 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_CLI_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 28 | int ret; |
| 29 | size_t len; |
| 30 | mbedtls_ssl_context ssl; |
| 31 | mbedtls_ssl_config conf; |
| 32 | mbedtls_ctr_drbg_context ctr_drbg; |
| 33 | mbedtls_entropy_context entropy; |
| 34 | mbedtls_timing_delay_context timer; |
| 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( &cacert ); |
| 41 | if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem, |
| 42 | mbedtls_test_cas_pem_len ) != 0) |
| 43 | return 1; |
| 44 | #endif |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 45 | dummy_init(); |
| 46 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 47 | initialized = 1; |
| 48 | } |
| 49 | |
| 50 | mbedtls_ssl_init( &ssl ); |
| 51 | mbedtls_ssl_config_init( &conf ); |
| 52 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 53 | mbedtls_entropy_init( &entropy ); |
| 54 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 55 | srand(1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 56 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy, |
| 57 | (const unsigned char *) pers, strlen( pers ) ) != 0 ) |
| 58 | goto exit; |
| 59 | |
| 60 | if( mbedtls_ssl_config_defaults( &conf, |
| 61 | MBEDTLS_SSL_IS_CLIENT, |
| 62 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 63 | MBEDTLS_SSL_PRESET_DEFAULT ) != 0 ) |
| 64 | goto exit; |
| 65 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 67 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); |
| 68 | #endif |
| 69 | mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE ); |
| 70 | mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg ); |
| 71 | |
| 72 | if( mbedtls_ssl_setup( &ssl, &conf ) != 0 ) |
| 73 | goto exit; |
| 74 | |
| 75 | mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, |
| 76 | mbedtls_timing_get_delay ); |
| 77 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 78 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 79 | if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 ) |
| 80 | goto exit; |
| 81 | #endif |
| 82 | |
| 83 | biomemfuzz.Data = Data; |
| 84 | biomemfuzz.Size = Size; |
| 85 | biomemfuzz.Offset = 0; |
| 86 | mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout ); |
| 87 | |
| 88 | ret = mbedtls_ssl_handshake( &ssl ); |
| 89 | if( ret == 0 ) |
| 90 | { |
| 91 | //keep reading data from server until the end |
| 92 | do |
| 93 | { |
| 94 | len = sizeof( buf ) - 1; |
| 95 | ret = mbedtls_ssl_read( &ssl, buf, len ); |
| 96 | |
| 97 | if( ret == MBEDTLS_ERR_SSL_WANT_READ ) |
| 98 | continue; |
| 99 | else if( ret <= 0 ) |
| 100 | //EOF or error |
| 101 | break; |
| 102 | } |
| 103 | while( 1 ); |
| 104 | } |
| 105 | |
| 106 | exit: |
| 107 | mbedtls_entropy_free( &entropy ); |
| 108 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 109 | mbedtls_ssl_config_free( &conf ); |
| 110 | mbedtls_ssl_free( &ssl ); |
| 111 | |
| 112 | #else |
| 113 | (void) Data; |
| 114 | (void) Size; |
| 115 | #endif |
| 116 | return 0; |
| 117 | } |