blob: 014f386efe4d7fb3829dbf373b874e8344da1134 [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"
4#include "mbedtls/certs.h"
5#include "mbedtls/ssl_ticket.h"
Philippe Antoine08633822019-06-04 14:03:06 +02006#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02007#include <string.h>
8#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +02009#include <stdint.h>
10
11
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020012#if defined(MBEDTLS_SSL_SRV_C) && \
13 defined(MBEDTLS_ENTROPY_C) && \
14 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine72333522018-05-03 16:40:24 +020015const char *pers = "fuzz_server";
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 srvcert;
19static mbedtls_pk_context pkey;
20#endif
21const char *alpn_list[3];
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
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020030#endif // MBEDTLS_SSL_SRV_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_SRV_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#if defined(MBEDTLS_SSL_SESSION_TICKETS)
44 mbedtls_ssl_ticket_context ticket_ctx;
45#endif
46 unsigned char buf[4096];
47 fuzzBufferOffset_t biomemfuzz;
48 uint8_t options;
49
50 //we take 1 byte as options input
51 if (Size < 1) {
52 return 0;
53 }
54 options = Data[Size - 1];
55
56 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020057#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020058 mbedtls_x509_crt_init( &srvcert );
59 mbedtls_pk_init( &pkey );
60 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
61 mbedtls_test_srv_crt_len ) != 0)
62 return 1;
63 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
64 mbedtls_test_cas_pem_len ) != 0)
65 return 1;
66 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
67 mbedtls_test_srv_key_len, NULL, 0 ) != 0)
68 return 1;
69#endif
70
71 alpn_list[0] = "HTTP";
72 alpn_list[1] = "fuzzalpn";
73 alpn_list[2] = NULL;
74
Philippe Antoine08633822019-06-04 14:03:06 +020075 dummy_init();
76
Philippe Antoine72333522018-05-03 16:40:24 +020077 initialized = 1;
78 }
79 mbedtls_ssl_init( &ssl );
80 mbedtls_ssl_config_init( &conf );
81 mbedtls_ctr_drbg_init( &ctr_drbg );
82 mbedtls_entropy_init( &entropy );
83#if defined(MBEDTLS_SSL_SESSION_TICKETS)
84 mbedtls_ssl_ticket_init( &ticket_ctx );
85#endif
86
87 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
88 (const unsigned char *) pers, strlen( pers ) ) != 0 )
89 goto exit;
90
91
92 if( mbedtls_ssl_config_defaults( &conf,
93 MBEDTLS_SSL_IS_SERVER,
94 MBEDTLS_SSL_TRANSPORT_STREAM,
95 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
96 goto exit;
97
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020098 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020099 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
100
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200101#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200102 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
103 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
104 goto exit;
105#endif
106
107 mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED );
108#if defined(MBEDTLS_SSL_ALPN)
109 if (options & 0x2) {
110 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
111 }
112#endif
113#if defined(MBEDTLS_SSL_SESSION_TICKETS)
114 if( options & 0x4 )
115 {
116 if( mbedtls_ssl_ticket_setup( &ticket_ctx,
117 dummy_random, &ctr_drbg,
118 MBEDTLS_CIPHER_AES_256_GCM,
119 86400 ) != 0 )
120 goto exit;
121
122 mbedtls_ssl_conf_session_tickets_cb( &conf,
123 mbedtls_ssl_ticket_write,
124 mbedtls_ssl_ticket_parse,
125 &ticket_ctx );
126 }
127#endif
128#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
129 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 0x8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
130#endif
131#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
132 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
133#endif
134#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
135 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
136#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +0100137#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +0200138 if (options & 0x40) {
139 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
140 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
141 }
142#endif
143#if defined(MBEDTLS_SSL_RENEGOTIATION)
144 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
145#endif
146
147 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
148 goto exit;
149
150 biomemfuzz.Data = Data;
151 biomemfuzz.Size = Size-1;
152 biomemfuzz.Offset = 0;
153 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
154
155 mbedtls_ssl_session_reset( &ssl );
156 ret = mbedtls_ssl_handshake( &ssl );
157 if( ret == 0 )
158 {
159 //keep reading data from server until the end
160 do
161 {
162 len = sizeof( buf ) - 1;
163 ret = mbedtls_ssl_read( &ssl, buf, len );
164
165 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
166 continue;
167 else if( ret <= 0 )
168 //EOF or error
169 break;
170 }
171 while( 1 );
172 }
173
174exit:
175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
176 mbedtls_ssl_ticket_free( &ticket_ctx );
177#endif
178 mbedtls_entropy_free( &entropy );
179 mbedtls_ctr_drbg_free( &ctr_drbg );
180 mbedtls_ssl_config_free( &conf );
181 mbedtls_ssl_free( &ssl );
182
Philippe Antoinec32fd242019-06-06 09:12:53 +0200183#else
184 (void) Data;
185 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200186#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200187
Philippe Antoine72333522018-05-03 16:40:24 +0200188 return 0;
189}