blob: 424af811171590ff77833ad8cd2c1f5eff9fb05d [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;
Philippe Antoinef0493042019-06-04 12:01:51 +020082 (void) data;
Philippe Antoine72333522018-05-03 16:40:24 +020083
84 //use mbedtls_entropy_func to find bugs in it
Philippe Antoine9c7b6982018-05-29 17:00:39 +020085 //test performance impact of entropy
86 //ret = mbedtls_entropy_func(data, output, len);
Philippe Antoine72333522018-05-03 16:40:24 +020087 for (i=0; i<len; i++) {
88 //replace result with pseudo random
89 output[i] = (unsigned char) random();
90 }
Philippe Antoine9c7b6982018-05-29 17:00:39 +020091 return( 0 );
Philippe Antoine72333522018-05-03 16:40:24 +020092}
93#endif
94
95
96
97int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
98#if defined(MBEDTLS_SSL_PROTO_DTLS)
99 int ret;
100 size_t len;
101 mbedtls_ssl_context ssl;
102 mbedtls_ssl_config conf;
103 mbedtls_ctr_drbg_context ctr_drbg;
104 mbedtls_entropy_context entropy;
105 mbedtls_timing_delay_context timer;
106 unsigned char buf[4096];
107 fuzzBufferOffset_t biomemfuzz;
108
109 if (initialized == 0) {
110#if defined(MBEDTLS_X509_CRT_PARSE_C)
111 mbedtls_x509_crt_init( &cacert );
112 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
113 mbedtls_test_cas_pem_len ) != 0)
114 return 1;
115#endif
116 initialized = 1;
117 }
118
119 mbedtls_ssl_init( &ssl );
120 mbedtls_ssl_config_init( &conf );
121 mbedtls_ctr_drbg_init( &ctr_drbg );
122 mbedtls_entropy_init( &entropy );
123
124 srandom(1);
125 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
126 (const unsigned char *) pers, strlen( pers ) ) != 0 )
127 goto exit;
128
129 if( mbedtls_ssl_config_defaults( &conf,
130 MBEDTLS_SSL_IS_CLIENT,
131 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
132 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
133 goto exit;
134
135#if defined(MBEDTLS_X509_CRT_PARSE_C)
136 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
137#endif
138 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
139 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
140
141 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
142 goto exit;
143
144 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
145 mbedtls_timing_get_delay );
146
147#if defined(MBEDTLS_X509_CRT_PARSE_C)
148 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
149 goto exit;
150#endif
151
152 biomemfuzz.Data = Data;
153 biomemfuzz.Size = Size;
154 biomemfuzz.Offset = 0;
155 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
156
157 ret = mbedtls_ssl_handshake( &ssl );
158 if( ret == 0 )
159 {
160 //keep reading data from server until the end
161 do
162 {
163 len = sizeof( buf ) - 1;
164 ret = mbedtls_ssl_read( &ssl, buf, len );
165
166 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
167 continue;
168 else if( ret <= 0 )
169 //EOF or error
170 break;
171 }
172 while( 1 );
173 }
174
175exit:
176 mbedtls_entropy_free( &entropy );
177 mbedtls_ctr_drbg_free( &ctr_drbg );
178 mbedtls_ssl_config_free( &conf );
179 mbedtls_ssl_free( &ssl );
180
181#else
182 (void) Data;
183 (void) Size;
184#endif
185 return 0;
186}