blob: 618eda2656c5e37705ba6cc4a10b89f497dd4353 [file] [log] [blame]
Philippe Antoine72333522018-05-03 16:40:24 +02001#include "mbedtls/ssl.h"
2#include "mbedtls/entropy.h"
3#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01004#include "test/certs.h"
Philippe Antoine08633822019-06-04 14:03:06 +02005#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02006#include <string.h>
7#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +02008#include <stdint.h>
9
10
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020011#if defined(MBEDTLS_SSL_CLI_C) && \
12 defined(MBEDTLS_ENTROPY_C) && \
13 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine42a2ce82019-07-10 14:26:31 +020014static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020015#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020016static mbedtls_x509_crt cacert;
17#endif
18const char *alpn_list[3];
19
20
Gilles Peskineeccd8882020-03-10 12:19:08 +010021#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020022const unsigned char psk[] = {
23 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
24 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
25};
26const char psk_id[] = "Client_identity";
27#endif
28
29const char *pers = "fuzz_client";
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020030#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoine72333522018-05-03 16:40:24 +020031
32
Philippe Antoine72333522018-05-03 16:40:24 +020033int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020034#if defined(MBEDTLS_SSL_CLI_C) && \
35 defined(MBEDTLS_ENTROPY_C) && \
36 defined(MBEDTLS_CTR_DRBG_C)
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 unsigned char buf[4096];
44 fuzzBufferOffset_t biomemfuzz;
45 uint16_t options;
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( &cacert );
50 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
51 mbedtls_test_cas_pem_len ) != 0)
52 return 1;
53#endif
54
55 alpn_list[0] = "HTTP";
56 alpn_list[1] = "fuzzalpn";
57 alpn_list[2] = NULL;
58
Philippe Antoine08633822019-06-04 14:03:06 +020059 dummy_init();
60
Philippe Antoine72333522018-05-03 16:40:24 +020061 initialized = 1;
62 }
63
64 //we take 1 byte as options input
65 if (Size < 2) {
66 return 0;
67 }
68 options = (Data[Size - 2] << 8) | Data[Size - 1];
69 //Avoid warnings if compile options imply no options
70 (void) options;
71
72 mbedtls_ssl_init( &ssl );
73 mbedtls_ssl_config_init( &conf );
74 mbedtls_ctr_drbg_init( &ctr_drbg );
75 mbedtls_entropy_init( &entropy );
76
77 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
78 (const unsigned char *) pers, strlen( pers ) ) != 0 )
79 goto exit;
80
81 if( mbedtls_ssl_config_defaults( &conf,
82 MBEDTLS_SSL_IS_CLIENT,
83 MBEDTLS_SSL_TRANSPORT_STREAM,
84 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
85 goto exit;
86
Gilles Peskineeccd8882020-03-10 12:19:08 +010087#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020088 if (options & 2) {
89 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
90 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
91 }
92#endif
93
Philippe Antoinedaab28a2019-06-28 12:31:23 +020094#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020095 if (options & 4) {
96 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
97 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
98 } else
99#endif
100 {
101 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
102 }
103#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
104 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
105#endif
106#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
107 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
108#endif
109#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
110 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
111#endif
112#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
113 mbedtls_ssl_conf_cbc_record_splitting( &conf, (options & 0x40) ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
114#endif
115#if defined(MBEDTLS_SSL_RENEGOTIATION)
116 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
117#endif
118#if defined(MBEDTLS_SSL_SESSION_TICKETS)
119 mbedtls_ssl_conf_session_tickets( &conf, (options & 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED );
120#endif
121#if defined(MBEDTLS_SSL_ALPN)
122 if (options & 0x200) {
123 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
124 }
125#endif
126 //There may be other options to add :
127 // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
128
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200129 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +0200130 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
131
132 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
133 goto exit;
134
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200135#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200136 if ((options & 1) == 0) {
137 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
138 goto exit;
139 }
140#endif
141
142 biomemfuzz.Data = Data;
143 biomemfuzz.Size = Size-2;
144 biomemfuzz.Offset = 0;
145 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
146
147 ret = mbedtls_ssl_handshake( &ssl );
148 if( ret == 0 )
149 {
150 //keep reading data from server until the end
151 do
152 {
153 len = sizeof( buf ) - 1;
154 ret = mbedtls_ssl_read( &ssl, buf, len );
155
156 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
157 continue;
158 else if( ret <= 0 )
159 //EOF or error
160 break;
161 }
162 while( 1 );
163 }
164
165exit:
166 mbedtls_entropy_free( &entropy );
167 mbedtls_ctr_drbg_free( &ctr_drbg );
168 mbedtls_ssl_config_free( &conf );
169 mbedtls_ssl_free( &ssl );
170
Philippe Antoinec32fd242019-06-06 09:12:53 +0200171#else
172 (void) Data;
173 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200174#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200175
Philippe Antoine72333522018-05-03 16:40:24 +0200176 return 0;
177}