blob: 95f43b8ffa6ee7392d5816373b4b4d010b4f83f4 [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"
Philippe Antoine72333522018-05-03 16:40:24 +02006#include "mbedtls/ssl_ticket.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01007#include "test/certs.h"
Philippe Antoine08633822019-06-04 14:03:06 +02008#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02009#include <string.h>
10#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +020011#include <stdint.h>
12
13
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020014#if defined(MBEDTLS_SSL_SRV_C) && \
15 defined(MBEDTLS_ENTROPY_C) && \
16 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine72333522018-05-03 16:40:24 +020017const char *pers = "fuzz_server";
Philippe Antoine42a2ce82019-07-10 14:26:31 +020018static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020019#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020020static mbedtls_x509_crt srvcert;
21static mbedtls_pk_context pkey;
22#endif
23const char *alpn_list[3];
24
Gilles Peskineeccd8882020-03-10 12:19:08 +010025#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020026const unsigned char psk[] = {
27 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
28 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
29};
30const char psk_id[] = "Client_identity";
31#endif
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020032#endif // MBEDTLS_SSL_SRV_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_SRV_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;
Przemek Stekiel68a01a62022-10-10 11:31:58 +020045#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Philippe Antoine72333522018-05-03 16:40:24 +020046 mbedtls_ssl_ticket_context ticket_ctx;
47#endif
48 unsigned char buf[4096];
49 fuzzBufferOffset_t biomemfuzz;
50 uint8_t options;
51
52 //we take 1 byte as options input
53 if (Size < 1) {
54 return 0;
55 }
56 options = Data[Size - 1];
57
Paul Elliott00738bf2022-02-10 18:15:42 +000058 mbedtls_ctr_drbg_init( &ctr_drbg );
59 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +020060
Paul Elliott00738bf2022-02-10 18:15:42 +000061 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
62 ( const unsigned char * ) pers, strlen( pers ) ) != 0 )
63 return 1;
64
65 if (initialized == 0) {
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +020066
Philippe Antoinedaab28a2019-06-28 12:31:23 +020067#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020068 mbedtls_x509_crt_init( &srvcert );
69 mbedtls_pk_init( &pkey );
70 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
71 mbedtls_test_srv_crt_len ) != 0)
72 return 1;
73 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
74 mbedtls_test_cas_pem_len ) != 0)
75 return 1;
76 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020077 mbedtls_test_srv_key_len, NULL, 0,
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +020078 dummy_random, &ctr_drbg ) != 0)
Philippe Antoine72333522018-05-03 16:40:24 +020079 return 1;
80#endif
81
82 alpn_list[0] = "HTTP";
83 alpn_list[1] = "fuzzalpn";
84 alpn_list[2] = NULL;
85
Philippe Antoine08633822019-06-04 14:03:06 +020086 dummy_init();
87
Philippe Antoine72333522018-05-03 16:40:24 +020088 initialized = 1;
89 }
90 mbedtls_ssl_init( &ssl );
91 mbedtls_ssl_config_init( &conf );
Przemek Stekiel68a01a62022-10-10 11:31:58 +020092#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Philippe Antoine72333522018-05-03 16:40:24 +020093 mbedtls_ssl_ticket_init( &ticket_ctx );
94#endif
95
Philippe Antoine72333522018-05-03 16:40:24 +020096 if( mbedtls_ssl_config_defaults( &conf,
97 MBEDTLS_SSL_IS_SERVER,
98 MBEDTLS_SSL_TRANSPORT_STREAM,
99 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
100 goto exit;
101
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200102 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +0200103 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
104
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200105#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200106 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
107 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
108 goto exit;
109#endif
110
111 mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED );
112#if defined(MBEDTLS_SSL_ALPN)
113 if (options & 0x2) {
114 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
115 }
116#endif
Przemek Stekiel68a01a62022-10-10 11:31:58 +0200117#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200118 if( options & 0x4 )
119 {
120 if( mbedtls_ssl_ticket_setup( &ticket_ctx,
121 dummy_random, &ctr_drbg,
122 MBEDTLS_CIPHER_AES_256_GCM,
123 86400 ) != 0 )
124 goto exit;
125
126 mbedtls_ssl_conf_session_tickets_cb( &conf,
127 mbedtls_ssl_ticket_write,
128 mbedtls_ssl_ticket_parse,
129 &ticket_ctx );
130 }
131#endif
Philippe Antoine72333522018-05-03 16:40:24 +0200132#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
133 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
134#endif
135#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
136 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
137#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +0100138#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +0200139 if (options & 0x40) {
140 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
141 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
142 }
143#endif
144#if defined(MBEDTLS_SSL_RENEGOTIATION)
145 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
146#endif
147
148 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
149 goto exit;
150
151 biomemfuzz.Data = Data;
152 biomemfuzz.Size = Size-1;
153 biomemfuzz.Offset = 0;
154 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
155
156 mbedtls_ssl_session_reset( &ssl );
157 ret = mbedtls_ssl_handshake( &ssl );
158 if( ret == 0 )
159 {
160 //keep reading data from server until the end
161 do
162 {
163 len = sizeof( buf ) - 1;
164 ret = mbedtls_ssl_read( &ssl, buf, len );
165
166 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
167 continue;
168 else if( ret <= 0 )
169 //EOF or error
170 break;
171 }
172 while( 1 );
173 }
174
175exit:
Przemek Stekiel68a01a62022-10-10 11:31:58 +0200176#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200177 mbedtls_ssl_ticket_free( &ticket_ctx );
178#endif
179 mbedtls_entropy_free( &entropy );
180 mbedtls_ctr_drbg_free( &ctr_drbg );
181 mbedtls_ssl_config_free( &conf );
182 mbedtls_ssl_free( &ssl );
183
Philippe Antoinec32fd242019-06-06 09:12:53 +0200184#else
185 (void) Data;
186 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200187#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200188
Philippe Antoine72333522018-05-03 16:40:24 +0200189 return 0;
190}