blob: 17caab211c68ce0130a70d080eabc2e8073370bb [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"
Andrzej Kurekeabeb302022-10-17 07:52:51 -040014#include "mbedtls/legacy_or_psa.h"
Philippe Antoine72333522018-05-03 16:40:24 +020015
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) && \
Andrzej Kurekeabeb302022-10-17 07:52:51 -040019 defined(MBEDTLS_TIMING_C) && \
20 ( defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
21 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) )
Philippe Antoine72333522018-05-03 16:40:24 +020022const char *pers = "fuzz_dtlsserver";
23const unsigned char client_ip[4] = {0x7F, 0, 0, 1};
Philippe Antoine42a2ce82019-07-10 14:26:31 +020024static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020025#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020026static mbedtls_x509_crt srvcert;
27static mbedtls_pk_context pkey;
28#endif
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020029#endif
Philippe Antoineadc23e62019-06-25 21:53:12 +020030#endif // MBEDTLS_SSL_PROTO_DTLS
Philippe Antoine72333522018-05-03 16:40:24 +020031
32int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020033#if defined(MBEDTLS_SSL_PROTO_DTLS) && \
34 defined(MBEDTLS_SSL_SRV_C) && \
35 defined(MBEDTLS_ENTROPY_C) && \
36 defined(MBEDTLS_CTR_DRBG_C) && \
Andrzej Kurekeabeb302022-10-17 07:52:51 -040037 defined(MBEDTLS_TIMING_C) && \
38 ( defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
39 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) )
Philippe Antoine72333522018-05-03 16:40:24 +020040 int ret;
41 size_t len;
42 mbedtls_ssl_context ssl;
43 mbedtls_ssl_config conf;
44 mbedtls_ctr_drbg_context ctr_drbg;
45 mbedtls_entropy_context entropy;
46 mbedtls_timing_delay_context timer;
47 mbedtls_ssl_cookie_ctx cookie_ctx;
48 unsigned char buf[4096];
49 fuzzBufferOffset_t biomemfuzz;
50
Paul Elliottbb016812022-02-14 15:57:11 +000051 mbedtls_ctr_drbg_init( &ctr_drbg );
52 mbedtls_entropy_init( &entropy );
53
54 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
55 ( const unsigned char * ) pers, strlen( pers ) ) != 0 )
56 goto exit;
57
Philippe Antoine72333522018-05-03 16:40:24 +020058 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020059#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020060 mbedtls_x509_crt_init( &srvcert );
61 mbedtls_pk_init( &pkey );
62 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
63 mbedtls_test_srv_crt_len ) != 0)
64 return 1;
65 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
66 mbedtls_test_cas_pem_len ) != 0)
67 return 1;
68 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020069 mbedtls_test_srv_key_len, NULL, 0,
Paul Elliottbb016812022-02-14 15:57:11 +000070 dummy_random, &ctr_drbg ) != 0)
Philippe Antoine72333522018-05-03 16:40:24 +020071 return 1;
72#endif
Philippe Antoine08633822019-06-04 14:03:06 +020073 dummy_init();
74
Philippe Antoine72333522018-05-03 16:40:24 +020075 initialized = 1;
76 }
77 mbedtls_ssl_init( &ssl );
78 mbedtls_ssl_config_init( &conf );
Philippe Antoine72333522018-05-03 16:40:24 +020079 mbedtls_ssl_cookie_init( &cookie_ctx );
80
Philippe Antoine72333522018-05-03 16:40:24 +020081 if( mbedtls_ssl_config_defaults( &conf,
82 MBEDTLS_SSL_IS_SERVER,
83 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
84 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
85 goto exit;
86
87
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020088 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020089 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
90
Philippe Antoinedaab28a2019-06-28 12:31:23 +020091#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020092 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
93 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
94 goto exit;
95#endif
96
97 if( mbedtls_ssl_cookie_setup( &cookie_ctx, dummy_random, &ctr_drbg ) != 0 )
98 goto exit;
99
100 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &cookie_ctx );
101
102 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
103 goto exit;
104
105 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
106 mbedtls_timing_get_delay );
107
108 biomemfuzz.Data = Data;
109 biomemfuzz.Size = Size;
110 biomemfuzz.Offset = 0;
111 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
112 if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
113 goto exit;
114
115 ret = mbedtls_ssl_handshake( &ssl );
116
117 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
118 biomemfuzz.Offset = ssl.next_record_offset;
119 mbedtls_ssl_session_reset( &ssl );
120 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
121 if( mbedtls_ssl_set_client_transport_id( &ssl, client_ip, sizeof(client_ip) ) != 0 )
122 goto exit;
123
124 ret = mbedtls_ssl_handshake( &ssl );
125
126 if( ret == 0 )
127 {
128 //keep reading data from server until the end
129 do
130 {
131 len = sizeof( buf ) - 1;
132 ret = mbedtls_ssl_read( &ssl, buf, len );
133 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
134 continue;
135 else if( ret <= 0 )
136 //EOF or error
137 break;
138 }
139 while( 1 );
140 }
141 }
142
143exit:
144 mbedtls_ssl_cookie_free( &cookie_ctx );
145 mbedtls_entropy_free( &entropy );
146 mbedtls_ctr_drbg_free( &ctr_drbg );
147 mbedtls_ssl_config_free( &conf );
148 mbedtls_ssl_free( &ssl );
149
150#else
151 (void) Data;
152 (void) Size;
153#endif
154 return 0;
155}