blob: 8197a6484ddeae6b6d486cb5d02c682892ee2b58 [file] [log] [blame]
Philippe Antoine72333522018-05-03 16:40:24 +02001#include <string.h>
2#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +02003#include <stdint.h>
Philippe Antoine08633822019-06-04 14:03:06 +02004#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02005#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
12
Philippe Antoineadc23e62019-06-25 21:53:12 +020013#ifdef MBEDTLS_SSL_CLI_C
Philippe Antoine42a2ce82019-07-10 14:26:31 +020014static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020015#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020016static mbedtls_x509_crt cacert;
17#endif
18
Philippe Antoine72333522018-05-03 16:40:24 +020019const char *pers = "fuzz_dtlsclient";
Philippe Antoineadc23e62019-06-25 21:53:12 +020020#endif // MBEDTLS_SSL_CLI_C
21#endif // MBEDTLS_SSL_PROTO_DTLS
Philippe Antoine72333522018-05-03 16:40:24 +020022
23
24
25int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Philippe Antoinec32fd242019-06-06 09:12:53 +020026#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_CLI_C)
Philippe Antoine72333522018-05-03 16:40:24 +020027 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 unsigned char buf[4096];
35 fuzzBufferOffset_t biomemfuzz;
36
37 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020038#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020039 mbedtls_x509_crt_init( &cacert );
40 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
41 mbedtls_test_cas_pem_len ) != 0)
42 return 1;
43#endif
Philippe Antoine08633822019-06-04 14:03:06 +020044 dummy_init();
45
Philippe Antoine72333522018-05-03 16:40:24 +020046 initialized = 1;
47 }
48
49 mbedtls_ssl_init( &ssl );
50 mbedtls_ssl_config_init( &conf );
51 mbedtls_ctr_drbg_init( &ctr_drbg );
52 mbedtls_entropy_init( &entropy );
53
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020054 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020055 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
56 (const unsigned char *) pers, strlen( pers ) ) != 0 )
57 goto exit;
58
59 if( mbedtls_ssl_config_defaults( &conf,
60 MBEDTLS_SSL_IS_CLIENT,
61 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
62 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
63 goto exit;
64
Philippe Antoinedaab28a2019-06-28 12:31:23 +020065#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020066 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
67#endif
68 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
69 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
70
71 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
72 goto exit;
73
74 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
75 mbedtls_timing_get_delay );
76
Philippe Antoinedaab28a2019-06-28 12:31:23 +020077#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020078 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
79 goto exit;
80#endif
81
82 biomemfuzz.Data = Data;
83 biomemfuzz.Size = Size;
84 biomemfuzz.Offset = 0;
85 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
86
87 ret = mbedtls_ssl_handshake( &ssl );
88 if( ret == 0 )
89 {
90 //keep reading data from server until the end
91 do
92 {
93 len = sizeof( buf ) - 1;
94 ret = mbedtls_ssl_read( &ssl, buf, len );
95
96 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
97 continue;
98 else if( ret <= 0 )
99 //EOF or error
100 break;
101 }
102 while( 1 );
103 }
104
105exit:
106 mbedtls_entropy_free( &entropy );
107 mbedtls_ctr_drbg_free( &ctr_drbg );
108 mbedtls_ssl_config_free( &conf );
109 mbedtls_ssl_free( &ssl );
110
111#else
112 (void) Data;
113 (void) Size;
114#endif
115 return 0;
116}