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