blob: 70eb6564875c6f0c50b75c75de11c06bded2ce8a [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"
Anton Matkinbc487252025-06-16 13:37:03 +02004#include "mbedtls/private/entropy.h"
5#include "mbedtls/private/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01006#include "test/certs.h"
Ben Taylor52510b22025-06-04 09:35:35 +01007#include "fuzz_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
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_CLI_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;
46 unsigned char buf[4096];
47 fuzzBufferOffset_t biomemfuzz;
48 uint16_t options;
49
50 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020051#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_x509_crt_init(&cacert);
53 if (mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
54 mbedtls_test_cas_pem_len) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020055 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010056 }
Philippe Antoine72333522018-05-03 16:40:24 +020057#endif
58
59 alpn_list[0] = "HTTP";
60 alpn_list[1] = "fuzzalpn";
61 alpn_list[2] = NULL;
62
Philippe Antoine08633822019-06-04 14:03:06 +020063 dummy_init();
64
Philippe Antoine72333522018-05-03 16:40:24 +020065 initialized = 1;
66 }
67
68 //we take 1 byte as options input
69 if (Size < 2) {
70 return 0;
71 }
72 options = (Data[Size - 2] << 8) | Data[Size - 1];
73 //Avoid warnings if compile options imply no options
74 (void) options;
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_ssl_init(&ssl);
77 mbedtls_ssl_config_init(&conf);
78 mbedtls_ctr_drbg_init(&ctr_drbg);
79 mbedtls_entropy_init(&entropy);
Philippe Antoine72333522018-05-03 16:40:24 +020080
Przemek Stekiel774f9de2023-04-19 11:47:01 +020081 psa_status_t status = psa_crypto_init();
82 if (status != PSA_SUCCESS) {
83 goto exit;
84 }
Przemek Stekiel774f9de2023-04-19 11:47:01 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
87 (const unsigned char *) pers, strlen(pers)) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 }
Philippe Antoine72333522018-05-03 16:40:24 +020090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (mbedtls_ssl_config_defaults(&conf,
Philippe Antoine72333522018-05-03 16:40:24 +020092 MBEDTLS_SSL_IS_CLIENT,
93 MBEDTLS_SSL_TRANSPORT_STREAM,
Gilles Peskine449bd832023-01-11 14:50:10 +010094 MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010096 }
Philippe Antoine72333522018-05-03 16:40:24 +020097
Gilles Peskineeccd8882020-03-10 12:19:08 +010098#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020099 if (options & 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
101 (const unsigned char *) psk_id, sizeof(psk_id) - 1);
Philippe Antoine72333522018-05-03 16:40:24 +0200102 }
103#endif
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 if (options & 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
108 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
Philippe Antoine72333522018-05-03 16:40:24 +0200109 } else
110#endif
111 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
Philippe Antoine72333522018-05-03 16:40:24 +0200113 }
Philippe Antoine72333522018-05-03 16:40:24 +0200114#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_ssl_conf_extended_master_secret(&conf,
116 (options &
117 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200118#endif
119#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_ssl_conf_encrypt_then_mac(&conf,
121 (options &
122 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200123#endif
Philippe Antoine72333522018-05-03 16:40:24 +0200124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_ssl_conf_renegotiation(&conf,
126 (options &
127 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200128#endif
129#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_ssl_conf_session_tickets(&conf,
131 (options &
132 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200133#endif
134#if defined(MBEDTLS_SSL_ALPN)
135 if (options & 0x200) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
Philippe Antoine72333522018-05-03 16:40:24 +0200137 }
138#endif
139 //There may be other options to add :
Ben Taylor27a4cc92025-08-04 15:13:34 +0100140 // mbedtls_ssl_conf_cert_profile
Philippe Antoine72333522018-05-03 16:40:24 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 }
Philippe Antoine72333522018-05-03 16:40:24 +0200145
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200146#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200147 if ((options & 1) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Philippe Antoine72333522018-05-03 16:40:24 +0200151 }
152#endif
153
154 biomemfuzz.Data = Data;
155 biomemfuzz.Size = Size-2;
156 biomemfuzz.Offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
Philippe Antoine72333522018-05-03 16:40:24 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 ret = mbedtls_ssl_handshake(&ssl);
160 if (ret == 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200161 //keep reading data from server until the end
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 do {
163 len = sizeof(buf) - 1;
164 ret = mbedtls_ssl_read(&ssl, buf, len);
Philippe Antoine72333522018-05-03 16:40:24 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Philippe Antoine72333522018-05-03 16:40:24 +0200167 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 } else if (ret <= 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200169 //EOF or error
170 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 }
172 } while (1);
Philippe Antoine72333522018-05-03 16:40:24 +0200173 }
174
175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_entropy_free(&entropy);
177 mbedtls_ctr_drbg_free(&ctr_drbg);
178 mbedtls_ssl_config_free(&conf);
179 mbedtls_ssl_free(&ssl);
Przemek Stekiel774f9de2023-04-19 11:47:01 +0200180 mbedtls_psa_crypto_free();
Philippe Antoine72333522018-05-03 16:40:24 +0200181
Philippe Antoinec32fd242019-06-06 09:12:53 +0200182#else
183 (void) Data;
184 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200185#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200186
Philippe Antoine72333522018-05-03 16:40:24 +0200187 return 0;
188}