blob: 64fe32d2689a1ceb481dadff7ab1e315a0732f20 [file] [log] [blame]
Felix Conway998760a2025-03-24 11:37:33 +00001#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
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
Gilles Peskine449bd832023-01-11 14:50:10 +010035int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
36{
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020037#if defined(MBEDTLS_SSL_SRV_C) && \
38 defined(MBEDTLS_ENTROPY_C) && \
39 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine72333522018-05-03 16:40:24 +020040 int ret;
41 size_t len;
42 mbedtls_ssl_context ssl;
43 mbedtls_ssl_config conf;
44 mbedtls_ctr_drbg_context ctr_drbg;
45 mbedtls_entropy_context entropy;
Przemek Stekiel68a01a62022-10-10 11:31:58 +020046#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Philippe Antoine72333522018-05-03 16:40:24 +020047 mbedtls_ssl_ticket_context ticket_ctx;
48#endif
49 unsigned char buf[4096];
50 fuzzBufferOffset_t biomemfuzz;
51 uint8_t options;
52
53 //we take 1 byte as options input
54 if (Size < 1) {
55 return 0;
56 }
57 options = Data[Size - 1];
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_ctr_drbg_init(&ctr_drbg);
60 mbedtls_entropy_init(&entropy);
Przemek Stekiel774f9de2023-04-19 11:47:01 +020061#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
62 mbedtls_x509_crt_init(&srvcert);
63 mbedtls_pk_init(&pkey);
64#endif
65 mbedtls_ssl_init(&ssl);
66 mbedtls_ssl_config_init(&conf);
67#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
68 mbedtls_ssl_ticket_init(&ticket_ctx);
69#endif
70#if defined(MBEDTLS_USE_PSA_CRYPTO)
71 psa_status_t status = psa_crypto_init();
72 if (status != PSA_SUCCESS) {
73 goto exit;
74 }
75#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
78 (const unsigned char *) pers, strlen(pers)) != 0) {
Paul Elliott00738bf2022-02-10 18:15:42 +000079 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010080 }
Paul Elliott00738bf2022-02-10 18:15:42 +000081
82 if (initialized == 0) {
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +020083
Philippe Antoinedaab28a2019-06-28 12:31:23 +020084#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
86 mbedtls_test_srv_crt_len) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020087 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 }
89 if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
90 mbedtls_test_cas_pem_len) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020091 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 }
93 if (mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020094 mbedtls_test_srv_key_len, NULL, 0,
Gilles Peskine449bd832023-01-11 14:50:10 +010095 dummy_random, &ctr_drbg) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020096 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010097 }
Philippe Antoine72333522018-05-03 16:40:24 +020098#endif
99
100 alpn_list[0] = "HTTP";
101 alpn_list[1] = "fuzzalpn";
102 alpn_list[2] = NULL;
103
Philippe Antoine08633822019-06-04 14:03:06 +0200104 dummy_init();
105
Philippe Antoine72333522018-05-03 16:40:24 +0200106 initialized = 1;
107 }
Philippe Antoine72333522018-05-03 16:40:24 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (mbedtls_ssl_config_defaults(&conf,
Philippe Antoine72333522018-05-03 16:40:24 +0200110 MBEDTLS_SSL_IS_SERVER,
111 MBEDTLS_SSL_TRANSPORT_STREAM,
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 }
Philippe Antoine72333522018-05-03 16:40:24 +0200115
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200116 srand(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
Philippe Antoine72333522018-05-03 16:40:24 +0200118
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200119#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
121 if (mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 }
Philippe Antoine72333522018-05-03 16:40:24 +0200124#endif
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_ssl_conf_cert_req_ca_list(&conf,
127 (options &
128 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200129#if defined(MBEDTLS_SSL_ALPN)
130 if (options & 0x2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
Philippe Antoine72333522018-05-03 16:40:24 +0200132 }
133#endif
Przemek Stekiel68a01a62022-10-10 11:31:58 +0200134#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (options & 0x4) {
Ben Taylor0c29cf82025-01-29 08:18:43 +0000136 if (mbedtls_ssl_ticket_setup(&ticket_ctx, //context
Ben Taylor0c29cf82025-01-29 08:18:43 +0000137 PSA_ALG_GCM, //alg
138 PSA_KEY_TYPE_AES, //key_type
139 256, //key_bits
140 86400) != 0) { //lifetime
Philippe Antoine72333522018-05-03 16:40:24 +0200141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 }
Philippe Antoine72333522018-05-03 16:40:24 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_ssl_conf_session_tickets_cb(&conf,
Philippe Antoine72333522018-05-03 16:40:24 +0200145 mbedtls_ssl_ticket_write,
146 mbedtls_ssl_ticket_parse,
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 &ticket_ctx);
Philippe Antoine72333522018-05-03 16:40:24 +0200148 }
149#endif
Philippe Antoine72333522018-05-03 16:40:24 +0200150#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_ssl_conf_extended_master_secret(&conf,
152 (options &
153 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200154#endif
155#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_ssl_conf_encrypt_then_mac(&conf,
157 (options &
158 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200159#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +0100160#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +0200161 if (options & 0x40) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
163 (const unsigned char *) psk_id, sizeof(psk_id) - 1);
Philippe Antoine72333522018-05-03 16:40:24 +0200164 }
165#endif
166#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_ssl_conf_renegotiation(&conf,
168 (options &
169 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200170#endif
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 }
Philippe Antoine72333522018-05-03 16:40:24 +0200175
176 biomemfuzz.Data = Data;
177 biomemfuzz.Size = Size-1;
178 biomemfuzz.Offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
Philippe Antoine72333522018-05-03 16:40:24 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_ssl_session_reset(&ssl);
182 ret = mbedtls_ssl_handshake(&ssl);
183 if (ret == 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200184 //keep reading data from server until the end
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 do {
186 len = sizeof(buf) - 1;
187 ret = mbedtls_ssl_read(&ssl, buf, len);
Philippe Antoine72333522018-05-03 16:40:24 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Philippe Antoine72333522018-05-03 16:40:24 +0200190 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 } else if (ret <= 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200192 //EOF or error
193 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 }
195 } while (1);
Philippe Antoine72333522018-05-03 16:40:24 +0200196 }
197
198exit:
Przemek Stekiel68a01a62022-10-10 11:31:58 +0200199#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_ssl_ticket_free(&ticket_ctx);
Philippe Antoine72333522018-05-03 16:40:24 +0200201#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_entropy_free(&entropy);
203 mbedtls_ctr_drbg_free(&ctr_drbg);
204 mbedtls_ssl_config_free(&conf);
Przemek Stekiel774f9de2023-04-19 11:47:01 +0200205#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
206 mbedtls_x509_crt_free(&srvcert);
207 mbedtls_pk_free(&pkey);
208#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_ssl_free(&ssl);
Przemek Stekiel774f9de2023-04-19 11:47:01 +0200210#if defined(MBEDTLS_USE_PSA_CRYPTO)
211 mbedtls_psa_crypto_free();
212#endif
Philippe Antoinec32fd242019-06-06 09:12:53 +0200213#else
214 (void) Data;
215 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200216#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200217
Philippe Antoine72333522018-05-03 16:40:24 +0200218 return 0;
219}