blob: 40ca90a9196cb359a3190b4de47af1195b907f3b [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 "mbedtls/ssl.h"
4#include "mbedtls/entropy.h"
5#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01006#include "test/certs.h"
Philippe Antoine08633822019-06-04 14:03:06 +02007#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02008#include <string.h>
9#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +020010#include <stdint.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)
Philippe Antoine42a2ce82019-07-10 14:26:31 +020016static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020017#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020018static mbedtls_x509_crt cacert;
19#endif
20const char *alpn_list[3];
21
22
Gilles Peskineeccd8882020-03-10 12:19:08 +010023#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020024const unsigned char psk[] = {
25 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
26 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
27};
28const char psk_id[] = "Client_identity";
29#endif
30
31const char *pers = "fuzz_client";
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020032#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoine72333522018-05-03 16:40:24 +020033
34
Philippe Antoine72333522018-05-03 16:40:24 +020035int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020036#if defined(MBEDTLS_SSL_CLI_C) && \
37 defined(MBEDTLS_ENTROPY_C) && \
38 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine72333522018-05-03 16:40:24 +020039 int ret;
40 size_t len;
41 mbedtls_ssl_context ssl;
42 mbedtls_ssl_config conf;
43 mbedtls_ctr_drbg_context ctr_drbg;
44 mbedtls_entropy_context entropy;
45 unsigned char buf[4096];
46 fuzzBufferOffset_t biomemfuzz;
47 uint16_t options;
48
49 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020050#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020051 mbedtls_x509_crt_init( &cacert );
52 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
53 mbedtls_test_cas_pem_len ) != 0)
54 return 1;
55#endif
56
57 alpn_list[0] = "HTTP";
58 alpn_list[1] = "fuzzalpn";
59 alpn_list[2] = NULL;
60
Philippe Antoine08633822019-06-04 14:03:06 +020061 dummy_init();
62
Philippe Antoine72333522018-05-03 16:40:24 +020063 initialized = 1;
64 }
65
66 //we take 1 byte as options input
67 if (Size < 2) {
68 return 0;
69 }
70 options = (Data[Size - 2] << 8) | Data[Size - 1];
71 //Avoid warnings if compile options imply no options
72 (void) options;
73
74 mbedtls_ssl_init( &ssl );
75 mbedtls_ssl_config_init( &conf );
76 mbedtls_ctr_drbg_init( &ctr_drbg );
77 mbedtls_entropy_init( &entropy );
78
79 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
80 (const unsigned char *) pers, strlen( pers ) ) != 0 )
81 goto exit;
82
83 if( mbedtls_ssl_config_defaults( &conf,
84 MBEDTLS_SSL_IS_CLIENT,
85 MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
87 goto exit;
88
Gilles Peskineeccd8882020-03-10 12:19:08 +010089#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020090 if (options & 2) {
91 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
92 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
93 }
94#endif
95
Philippe Antoinedaab28a2019-06-28 12:31:23 +020096#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020097 if (options & 4) {
98 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
99 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
100 } else
101#endif
102 {
103 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
104 }
105#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
106 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
107#endif
108#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
109 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
110#endif
111#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
112 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
113#endif
114#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
115 mbedtls_ssl_conf_cbc_record_splitting( &conf, (options & 0x40) ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
116#endif
117#if defined(MBEDTLS_SSL_RENEGOTIATION)
118 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
119#endif
120#if defined(MBEDTLS_SSL_SESSION_TICKETS)
121 mbedtls_ssl_conf_session_tickets( &conf, (options & 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED );
122#endif
123#if defined(MBEDTLS_SSL_ALPN)
124 if (options & 0x200) {
125 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
126 }
127#endif
128 //There may be other options to add :
129 // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
130
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200131 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +0200132 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
133
134 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
135 goto exit;
136
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200137#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200138 if ((options & 1) == 0) {
139 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
140 goto exit;
141 }
142#endif
143
144 biomemfuzz.Data = Data;
145 biomemfuzz.Size = Size-2;
146 biomemfuzz.Offset = 0;
147 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
148
149 ret = mbedtls_ssl_handshake( &ssl );
150 if( ret == 0 )
151 {
152 //keep reading data from server until the end
153 do
154 {
155 len = sizeof( buf ) - 1;
156 ret = mbedtls_ssl_read( &ssl, buf, len );
157
158 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
159 continue;
160 else if( ret <= 0 )
161 //EOF or error
162 break;
163 }
164 while( 1 );
165 }
166
167exit:
168 mbedtls_entropy_free( &entropy );
169 mbedtls_ctr_drbg_free( &ctr_drbg );
170 mbedtls_ssl_config_free( &conf );
171 mbedtls_ssl_free( &ssl );
172
Philippe Antoinec32fd242019-06-06 09:12:53 +0200173#else
174 (void) Data;
175 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200176#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200177
Philippe Antoine72333522018-05-03 16:40:24 +0200178 return 0;
179}