blob: 23116aea0b3633039d135fe608e474534b046646 [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#include "mbedtls/ssl_cookie.h"
12
13
14const char *pers = "fuzz_dtlsserver";
15const unsigned char client_ip[4] = {0x7F, 0, 0, 1};
16static bool initialized = 0;
17#if defined(MBEDTLS_X509_CRT_PARSE_C)
18static mbedtls_x509_crt srvcert;
19static mbedtls_pk_context pkey;
20#endif
21
22typedef struct fuzzBufferOffset
23{
24 const uint8_t *Data;
25 size_t Size;
26 size_t Offset;
27} fuzzBufferOffset_t;
28
29
30static int dummy_send( void *ctx, const unsigned char *buf, size_t len )
31{
32 //silence warning about unused parameter
33 (void) ctx;
34 (void) buf;
35
36 //pretends we wrote everything ok
37 return( len );
38}
39
40static int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
41{
42 //reads from the buffer from fuzzer
43 fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
44
45 if (biomemfuzz->Offset == biomemfuzz->Size) {
46 //EOF
47 return (0);
48 }
49 if (len + biomemfuzz->Offset > biomemfuzz->Size) {
50 //do not overflow
51 len = biomemfuzz->Size - biomemfuzz->Offset;
52 }
53 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
54 biomemfuzz->Offset += len;
55 return( len );
56}
57
58static int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
59 uint32_t timeout )
60{
61 (void) timeout;
62
63 return fuzz_recv(ctx, buf, len);
64}
65
66static int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
67{
68 int ret;
69 size_t i;
70
71 //use mbedtls_ctr_drbg_random to find bugs in it
72 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
73 for (i=0; i<output_len; i++) {
74 //replace result with pseudo random
75 output[i] = (unsigned char) random();
76 }
77 return( ret );
78}
79
80static int dummy_entropy( void *data, unsigned char *output, size_t len )
81{
82 int ret;
83 size_t i;
84
85 //use mbedtls_entropy_func to find bugs in it
86 ret = mbedtls_entropy_func(data, output, len);
87 for (i=0; i<len; i++) {
88 //replace result with pseudo random
89 output[i] = (unsigned char) random();
90 }
91 return( ret );
92}
93#endif
94
95int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
96#if defined(MBEDTLS_SSL_PROTO_DTLS)
97 int ret;
98 size_t len;
99 mbedtls_ssl_context ssl;
100 mbedtls_ssl_config conf;
101 mbedtls_ctr_drbg_context ctr_drbg;
102 mbedtls_entropy_context entropy;
103 mbedtls_timing_delay_context timer;
104 mbedtls_ssl_cookie_ctx cookie_ctx;
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( &srvcert );
111 mbedtls_pk_init( &pkey );
112 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
113 mbedtls_test_srv_crt_len ) != 0)
114 return 1;
115 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
116 mbedtls_test_cas_pem_len ) != 0)
117 return 1;
118 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
119 mbedtls_test_srv_key_len, NULL, 0 ) != 0)
120 return 1;
121#endif
122 initialized = 1;
123 }
124 mbedtls_ssl_init( &ssl );
125 mbedtls_ssl_config_init( &conf );
126 mbedtls_ctr_drbg_init( &ctr_drbg );
127 mbedtls_entropy_init( &entropy );
128 mbedtls_ssl_cookie_init( &cookie_ctx );
129
130 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
131 (const unsigned char *) pers, strlen( pers ) ) != 0 )
132 goto exit;
133
134
135 if( mbedtls_ssl_config_defaults( &conf,
136 MBEDTLS_SSL_IS_SERVER,
137 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
138 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
139 goto exit;
140
141
142 srandom(1);
143 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
144
145#if defined(MBEDTLS_X509_CRT_PARSE_C)
146 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
147 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
148 goto exit;
149#endif
150
151 if( mbedtls_ssl_cookie_setup( &cookie_ctx, dummy_random, &ctr_drbg ) != 0 )
152 goto exit;
153
154 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &cookie_ctx );
155
156 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
157 goto exit;
158
159 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
160 mbedtls_timing_get_delay );
161
162 biomemfuzz.Data = Data;
163 biomemfuzz.Size = Size;
164 biomemfuzz.Offset = 0;
165 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
166 if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
167 goto exit;
168
169 ret = mbedtls_ssl_handshake( &ssl );
170
171 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
172 biomemfuzz.Offset = ssl.next_record_offset;
173 mbedtls_ssl_session_reset( &ssl );
174 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
175 if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
176 goto exit;
177
178 ret = mbedtls_ssl_handshake( &ssl );
179
180 if( ret == 0 )
181 {
182 //keep reading data from server until the end
183 do
184 {
185 len = sizeof( buf ) - 1;
186 ret = mbedtls_ssl_read( &ssl, buf, len );
187 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
188 continue;
189 else if( ret <= 0 )
190 //EOF or error
191 break;
192 }
193 while( 1 );
194 }
195 }
196
197exit:
198 mbedtls_ssl_cookie_free( &cookie_ctx );
199 mbedtls_entropy_free( &entropy );
200 mbedtls_ctr_drbg_free( &ctr_drbg );
201 mbedtls_ssl_config_free( &conf );
202 mbedtls_ssl_free( &ssl );
203
204#else
205 (void) Data;
206 (void) Size;
207#endif
208 return 0;
209}