blob: 5a9510dda9828b463f2be189bbd949bae5e9afbe [file] [log] [blame]
Philippe Antoine72333522018-05-03 16:40:24 +02001#include "mbedtls/ssl.h"
2#include "mbedtls/entropy.h"
3#include "mbedtls/ctr_drbg.h"
4#include "mbedtls/certs.h"
5#include "mbedtls/ssl_ticket.h"
6#include <string.h>
7#include <stdlib.h>
8#include <stdbool.h>
9#include <stdint.h>
10
11
12const char *pers = "fuzz_server";
13static bool initialized = 0;
14#if defined(MBEDTLS_X509_CRT_PARSE_C)
15static mbedtls_x509_crt srvcert;
16static mbedtls_pk_context pkey;
17#endif
18const char *alpn_list[3];
19
20#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
21const unsigned char psk[] = {
22 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
23 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
24};
25const char psk_id[] = "Client_identity";
26#endif
27
28
29typedef struct fuzzBufferOffset
30{
31 const uint8_t *Data;
32 size_t Size;
33 size_t Offset;
34} fuzzBufferOffset_t;
35
36
37static int dummy_send( void *ctx, const unsigned char *buf, size_t len )
38{
39 //silence warning about unused parameter
40 (void) ctx;
41 (void) buf;
42
43 //pretends we wrote everything ok
44 return( len );
45}
46
47static int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
48{
49 //reads from the buffer from fuzzer
50 fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
51
52 if (biomemfuzz->Offset == biomemfuzz->Size) {
53 //EOF
54 return (0);
55 }
56 if (len + biomemfuzz->Offset > biomemfuzz->Size) {
57 //do not overflow
58 len = biomemfuzz->Size - biomemfuzz->Offset;
59 }
60 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
61 biomemfuzz->Offset += len;
62 return( 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{
81 int ret;
82 size_t i;
83
84 //use mbedtls_entropy_func to find bugs in it
85 ret = mbedtls_entropy_func(data, output, len);
86 for (i=0; i<len; i++) {
87 //replace result with pseudo random
88 output[i] = (unsigned char) random();
89 }
90 return( ret );
91}
92
93
94int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
95 int ret;
96 size_t len;
97 mbedtls_ssl_context ssl;
98 mbedtls_ssl_config conf;
99 mbedtls_ctr_drbg_context ctr_drbg;
100 mbedtls_entropy_context entropy;
101#if defined(MBEDTLS_SSL_SESSION_TICKETS)
102 mbedtls_ssl_ticket_context ticket_ctx;
103#endif
104 unsigned char buf[4096];
105 fuzzBufferOffset_t biomemfuzz;
106 uint8_t options;
107
108 //we take 1 byte as options input
109 if (Size < 1) {
110 return 0;
111 }
112 options = Data[Size - 1];
113
114 if (initialized == 0) {
115#if defined(MBEDTLS_X509_CRT_PARSE_C)
116 mbedtls_x509_crt_init( &srvcert );
117 mbedtls_pk_init( &pkey );
118 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
119 mbedtls_test_srv_crt_len ) != 0)
120 return 1;
121 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
122 mbedtls_test_cas_pem_len ) != 0)
123 return 1;
124 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
125 mbedtls_test_srv_key_len, NULL, 0 ) != 0)
126 return 1;
127#endif
128
129 alpn_list[0] = "HTTP";
130 alpn_list[1] = "fuzzalpn";
131 alpn_list[2] = NULL;
132
133 initialized = 1;
134 }
135 mbedtls_ssl_init( &ssl );
136 mbedtls_ssl_config_init( &conf );
137 mbedtls_ctr_drbg_init( &ctr_drbg );
138 mbedtls_entropy_init( &entropy );
139#if defined(MBEDTLS_SSL_SESSION_TICKETS)
140 mbedtls_ssl_ticket_init( &ticket_ctx );
141#endif
142
143 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
144 (const unsigned char *) pers, strlen( pers ) ) != 0 )
145 goto exit;
146
147
148 if( mbedtls_ssl_config_defaults( &conf,
149 MBEDTLS_SSL_IS_SERVER,
150 MBEDTLS_SSL_TRANSPORT_STREAM,
151 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
152 goto exit;
153
154 srandom(1);
155 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
156
157#if defined(MBEDTLS_X509_CRT_PARSE_C)
158 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
159 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
160 goto exit;
161#endif
162
163 mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED );
164#if defined(MBEDTLS_SSL_ALPN)
165 if (options & 0x2) {
166 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
167 }
168#endif
169#if defined(MBEDTLS_SSL_SESSION_TICKETS)
170 if( options & 0x4 )
171 {
172 if( mbedtls_ssl_ticket_setup( &ticket_ctx,
173 dummy_random, &ctr_drbg,
174 MBEDTLS_CIPHER_AES_256_GCM,
175 86400 ) != 0 )
176 goto exit;
177
178 mbedtls_ssl_conf_session_tickets_cb( &conf,
179 mbedtls_ssl_ticket_write,
180 mbedtls_ssl_ticket_parse,
181 &ticket_ctx );
182 }
183#endif
184#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
185 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 0x8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
186#endif
187#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
188 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
189#endif
190#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
191 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
192#endif
193#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
194 if (options & 0x40) {
195 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
196 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
197 }
198#endif
199#if defined(MBEDTLS_SSL_RENEGOTIATION)
200 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
201#endif
202
203 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
204 goto exit;
205
206 biomemfuzz.Data = Data;
207 biomemfuzz.Size = Size-1;
208 biomemfuzz.Offset = 0;
209 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
210
211 mbedtls_ssl_session_reset( &ssl );
212 ret = mbedtls_ssl_handshake( &ssl );
213 if( ret == 0 )
214 {
215 //keep reading data from server until the end
216 do
217 {
218 len = sizeof( buf ) - 1;
219 ret = mbedtls_ssl_read( &ssl, buf, len );
220
221 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
222 continue;
223 else if( ret <= 0 )
224 //EOF or error
225 break;
226 }
227 while( 1 );
228 }
229
230exit:
231#if defined(MBEDTLS_SSL_SESSION_TICKETS)
232 mbedtls_ssl_ticket_free( &ticket_ctx );
233#endif
234 mbedtls_entropy_free( &entropy );
235 mbedtls_ctr_drbg_free( &ctr_drbg );
236 mbedtls_ssl_config_free( &conf );
237 mbedtls_ssl_free( &ssl );
238
239 return 0;
240}