blob: 4cde1fe6c762caa3edc86f3a9058421cee3201f7 [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#include "mbedtls/ssl_cookie.h"
12
13
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020014#if defined(MBEDTLS_SSL_SRV_C) && \
15 defined(MBEDTLS_ENTROPY_C) && \
16 defined(MBEDTLS_CTR_DRBG_C) && \
17 defined(MBEDTLS_TIMING_C)
Philippe Antoine72333522018-05-03 16:40:24 +020018const char *pers = "fuzz_dtlsserver";
19const unsigned char client_ip[4] = {0x7F, 0, 0, 1};
Philippe Antoine42a2ce82019-07-10 14:26:31 +020020static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020021#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020022static mbedtls_x509_crt srvcert;
23static mbedtls_pk_context pkey;
24#endif
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020025#endif
Philippe Antoineadc23e62019-06-25 21:53:12 +020026#endif // MBEDTLS_SSL_PROTO_DTLS
Philippe Antoine72333522018-05-03 16:40:24 +020027
28int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020029#if defined(MBEDTLS_SSL_PROTO_DTLS) && \
30 defined(MBEDTLS_SSL_SRV_C) && \
31 defined(MBEDTLS_ENTROPY_C) && \
32 defined(MBEDTLS_CTR_DRBG_C) && \
33 defined(MBEDTLS_TIMING_C)
Philippe Antoine72333522018-05-03 16:40:24 +020034 int ret;
35 size_t len;
36 mbedtls_ssl_context ssl;
37 mbedtls_ssl_config conf;
38 mbedtls_ctr_drbg_context ctr_drbg;
39 mbedtls_entropy_context entropy;
40 mbedtls_timing_delay_context timer;
41 mbedtls_ssl_cookie_ctx cookie_ctx;
42 unsigned char buf[4096];
43 fuzzBufferOffset_t biomemfuzz;
44
45 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020046#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020047 mbedtls_x509_crt_init( &srvcert );
48 mbedtls_pk_init( &pkey );
49 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
50 mbedtls_test_srv_crt_len ) != 0)
51 return 1;
52 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
53 mbedtls_test_cas_pem_len ) != 0)
54 return 1;
55 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
56 mbedtls_test_srv_key_len, NULL, 0 ) != 0)
57 return 1;
58#endif
Philippe Antoine08633822019-06-04 14:03:06 +020059 dummy_init();
60
Philippe Antoine72333522018-05-03 16:40:24 +020061 initialized = 1;
62 }
63 mbedtls_ssl_init( &ssl );
64 mbedtls_ssl_config_init( &conf );
65 mbedtls_ctr_drbg_init( &ctr_drbg );
66 mbedtls_entropy_init( &entropy );
67 mbedtls_ssl_cookie_init( &cookie_ctx );
68
69 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
70 (const unsigned char *) pers, strlen( pers ) ) != 0 )
71 goto exit;
72
73
74 if( mbedtls_ssl_config_defaults( &conf,
75 MBEDTLS_SSL_IS_SERVER,
76 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
77 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
78 goto exit;
79
80
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020081 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020082 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
83
Philippe Antoinedaab28a2019-06-28 12:31:23 +020084#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020085 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
86 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
87 goto exit;
88#endif
89
90 if( mbedtls_ssl_cookie_setup( &cookie_ctx, dummy_random, &ctr_drbg ) != 0 )
91 goto exit;
92
93 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &cookie_ctx );
94
95 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
96 goto exit;
97
98 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
99 mbedtls_timing_get_delay );
100
101 biomemfuzz.Data = Data;
102 biomemfuzz.Size = Size;
103 biomemfuzz.Offset = 0;
104 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
105 if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
106 goto exit;
107
108 ret = mbedtls_ssl_handshake( &ssl );
109
110 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
111 biomemfuzz.Offset = ssl.next_record_offset;
112 mbedtls_ssl_session_reset( &ssl );
113 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
114 if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
115 goto exit;
116
117 ret = mbedtls_ssl_handshake( &ssl );
118
119 if( ret == 0 )
120 {
121 //keep reading data from server until the end
122 do
123 {
124 len = sizeof( buf ) - 1;
125 ret = mbedtls_ssl_read( &ssl, buf, len );
126 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
127 continue;
128 else if( ret <= 0 )
129 //EOF or error
130 break;
131 }
132 while( 1 );
133 }
134 }
135
136exit:
137 mbedtls_ssl_cookie_free( &cookie_ctx );
138 mbedtls_entropy_free( &entropy );
139 mbedtls_ctr_drbg_free( &ctr_drbg );
140 mbedtls_ssl_config_free( &conf );
141 mbedtls_ssl_free( &ssl );
142
143#else
144 (void) Data;
145 (void) Size;
146#endif
147 return 0;
148}