blob: c88b33b7398ac77740454fd5144d5e41d06bae13 [file] [log] [blame]
Philippe Antoine72333522018-05-03 16:40:24 +02001#include <string.h>
2#include <stdlib.h>
3#include <stdbool.h>
4#include <stdint.h>
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
12
13static bool initialized = 0;
14#if defined(MBEDTLS_X509_CRT_PARSE_C)
15static mbedtls_x509_crt cacert;
16#endif
17
18
19const char *pers = "fuzz_dtlsclient";
20
21
22typedef struct fuzzBufferOffset
23{
24 const uint8_t *Data;
25 size_t Size;
26 size_t Offset;
27} fuzzBufferOffset_t;
28
29static int dummy_send( void *ctx, const unsigned char *buf, size_t len )
30{
31 //silence warning about unused parameter
32 (void) ctx;
33 (void) buf;
34
35 //pretends we wrote everything ok
36 return( len );
37}
38
39static int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
40{
41 //reads from the buffer from fuzzer
42 fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
43
44 if (biomemfuzz->Offset == biomemfuzz->Size) {
45 //EOF
46 return (0);
47 }
48 if (len + biomemfuzz->Offset > biomemfuzz->Size) {
49 //do not overflow
50 len = biomemfuzz->Size - biomemfuzz->Offset;
51 }
52 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
53 biomemfuzz->Offset += len;
54 return( len );
55}
56
57static int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
58 uint32_t timeout )
59{
60 (void) timeout;
61
62 return fuzz_recv(ctx, buf, len);
63}
64
65static int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
66{
67 int ret;
68 size_t i;
69
70 //use mbedtls_ctr_drbg_random to find bugs in it
71 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
72 for (i=0; i<output_len; i++) {
73 //replace result with pseudo random
74 output[i] = (unsigned char) random();
75 }
76 return( ret );
77}
78
79static int dummy_entropy( void *data, unsigned char *output, size_t len )
80{
Philippe Antoine72333522018-05-03 16:40:24 +020081 size_t i;
82
83 //use mbedtls_entropy_func to find bugs in it
Philippe Antoine9c7b6982018-05-29 17:00:39 +020084 //test performance impact of entropy
85 //ret = mbedtls_entropy_func(data, output, len);
Philippe Antoine72333522018-05-03 16:40:24 +020086 for (i=0; i<len; i++) {
87 //replace result with pseudo random
88 output[i] = (unsigned char) random();
89 }
Philippe Antoine9c7b6982018-05-29 17:00:39 +020090 return( 0 );
Philippe Antoine72333522018-05-03 16:40:24 +020091}
92#endif
93
94
95
96int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
97#if defined(MBEDTLS_SSL_PROTO_DTLS)
98 int ret;
99 size_t len;
100 mbedtls_ssl_context ssl;
101 mbedtls_ssl_config conf;
102 mbedtls_ctr_drbg_context ctr_drbg;
103 mbedtls_entropy_context entropy;
104 mbedtls_timing_delay_context timer;
105 unsigned char buf[4096];
106 fuzzBufferOffset_t biomemfuzz;
107
108 if (initialized == 0) {
109#if defined(MBEDTLS_X509_CRT_PARSE_C)
110 mbedtls_x509_crt_init( &cacert );
111 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
112 mbedtls_test_cas_pem_len ) != 0)
113 return 1;
114#endif
115 initialized = 1;
116 }
117
118 mbedtls_ssl_init( &ssl );
119 mbedtls_ssl_config_init( &conf );
120 mbedtls_ctr_drbg_init( &ctr_drbg );
121 mbedtls_entropy_init( &entropy );
122
123 srandom(1);
124 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
125 (const unsigned char *) pers, strlen( pers ) ) != 0 )
126 goto exit;
127
128 if( mbedtls_ssl_config_defaults( &conf,
129 MBEDTLS_SSL_IS_CLIENT,
130 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
131 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
132 goto exit;
133
134#if defined(MBEDTLS_X509_CRT_PARSE_C)
135 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
136#endif
137 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
138 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
139
140 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
141 goto exit;
142
143 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
144 mbedtls_timing_get_delay );
145
146#if defined(MBEDTLS_X509_CRT_PARSE_C)
147 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
148 goto exit;
149#endif
150
151 biomemfuzz.Data = Data;
152 biomemfuzz.Size = Size;
153 biomemfuzz.Offset = 0;
154 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
155
156 ret = mbedtls_ssl_handshake( &ssl );
157 if( ret == 0 )
158 {
159 //keep reading data from server until the end
160 do
161 {
162 len = sizeof( buf ) - 1;
163 ret = mbedtls_ssl_read( &ssl, buf, len );
164
165 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
166 continue;
167 else if( ret <= 0 )
168 //EOF or error
169 break;
170 }
171 while( 1 );
172 }
173
174exit:
175 mbedtls_entropy_free( &entropy );
176 mbedtls_ctr_drbg_free( &ctr_drbg );
177 mbedtls_ssl_config_free( &conf );
178 mbedtls_ssl_free( &ssl );
179
180#else
181 (void) Data;
182 (void) Size;
183#endif
184 return 0;
185}