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