blob: ff258bcc7116150f1b1e04f243886d8f63712c25 [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
12
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020013#if defined(MBEDTLS_SSL_CLI_C) && \
14 defined(MBEDTLS_ENTROPY_C) && \
15 defined(MBEDTLS_CTR_DRBG_C) && \
16 defined(MBEDTLS_TIMING_C)
Philippe Antoine42a2ce82019-07-10 14:26:31 +020017static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020018#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020019static mbedtls_x509_crt cacert;
20#endif
21
Philippe Antoine72333522018-05-03 16:40:24 +020022const char *pers = "fuzz_dtlsclient";
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020023#endif
Philippe Antoineadc23e62019-06-25 21:53:12 +020024#endif // MBEDTLS_SSL_PROTO_DTLS
Philippe Antoine72333522018-05-03 16:40:24 +020025
26
27
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_CLI_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 unsigned char buf[4096];
42 fuzzBufferOffset_t biomemfuzz;
43
44 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020045#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020046 mbedtls_x509_crt_init( &cacert );
47 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
48 mbedtls_test_cas_pem_len ) != 0)
49 return 1;
50#endif
Philippe Antoine08633822019-06-04 14:03:06 +020051 dummy_init();
52
Philippe Antoine72333522018-05-03 16:40:24 +020053 initialized = 1;
54 }
55
56 mbedtls_ssl_init( &ssl );
57 mbedtls_ssl_config_init( &conf );
58 mbedtls_ctr_drbg_init( &ctr_drbg );
59 mbedtls_entropy_init( &entropy );
60
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020061 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020062 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
63 (const unsigned char *) pers, strlen( pers ) ) != 0 )
64 goto exit;
65
66 if( mbedtls_ssl_config_defaults( &conf,
67 MBEDTLS_SSL_IS_CLIENT,
68 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
69 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
70 goto exit;
71
Philippe Antoinedaab28a2019-06-28 12:31:23 +020072#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020073 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
74#endif
75 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
76 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
77
78 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
79 goto exit;
80
81 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
82 mbedtls_timing_get_delay );
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 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
86 goto exit;
87#endif
88
89 biomemfuzz.Data = Data;
90 biomemfuzz.Size = Size;
91 biomemfuzz.Offset = 0;
92 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout );
93
94 ret = mbedtls_ssl_handshake( &ssl );
95 if( ret == 0 )
96 {
97 //keep reading data from server until the end
98 do
99 {
100 len = sizeof( buf ) - 1;
101 ret = mbedtls_ssl_read( &ssl, buf, len );
102
103 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
104 continue;
105 else if( ret <= 0 )
106 //EOF or error
107 break;
108 }
109 while( 1 );
110 }
111
112exit:
113 mbedtls_entropy_free( &entropy );
114 mbedtls_ctr_drbg_free( &ctr_drbg );
115 mbedtls_ssl_config_free( &conf );
116 mbedtls_ssl_free( &ssl );
117
118#else
119 (void) Data;
120 (void) Size;
121#endif
122 return 0;
123}