blob: 21d03a3c34c3430e51cfcf57bd111fd32551863b [file] [log] [blame]
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +02001
Philippe Antoine72333522018-05-03 16:40:24 +02002#include "mbedtls/ssl.h"
3#include "mbedtls/entropy.h"
4#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01005#include "test/certs.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_CLI_C) && \
13 defined(MBEDTLS_ENTROPY_C) && \
14 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine42a2ce82019-07-10 14:26:31 +020015static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020016#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020017static mbedtls_x509_crt cacert;
18#endif
19const char *alpn_list[3];
20
21
Gilles Peskineeccd8882020-03-10 12:19:08 +010022#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020023const unsigned char psk[] = {
24 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
25 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
26};
27const char psk_id[] = "Client_identity";
28#endif
29
30const char *pers = "fuzz_client";
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020031#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoine72333522018-05-03 16:40:24 +020032
33
Gilles Peskine449bd832023-01-11 14:50:10 +010034int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
35{
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)
Gilles Peskine449bd832023-01-11 14:50:10 +010051 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) {
Philippe Antoine72333522018-05-03 16:40:24 +020054 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 }
Philippe Antoine72333522018-05-03 16:40:24 +020056#endif
57
58 alpn_list[0] = "HTTP";
59 alpn_list[1] = "fuzzalpn";
60 alpn_list[2] = NULL;
61
Philippe Antoine08633822019-06-04 14:03:06 +020062 dummy_init();
63
Philippe Antoine72333522018-05-03 16:40:24 +020064 initialized = 1;
65 }
66
67 //we take 1 byte as options input
68 if (Size < 2) {
69 return 0;
70 }
71 options = (Data[Size - 2] << 8) | Data[Size - 1];
72 //Avoid warnings if compile options imply no options
73 (void) options;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_ssl_init(&ssl);
76 mbedtls_ssl_config_init(&conf);
77 mbedtls_ctr_drbg_init(&ctr_drbg);
78 mbedtls_entropy_init(&entropy);
Philippe Antoine72333522018-05-03 16:40:24 +020079
Przemek Stekiel774f9de2023-04-19 11:47:01 +020080#if defined(MBEDTLS_USE_PSA_CRYPTO)
81 psa_status_t status = psa_crypto_init();
82 if (status != PSA_SUCCESS) {
83 goto exit;
84 }
85#endif /* MBEDTLS_USE_PSA_CRYPTO */
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
88 (const unsigned char *) pers, strlen(pers)) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020089 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 }
Philippe Antoine72333522018-05-03 16:40:24 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (mbedtls_ssl_config_defaults(&conf,
Philippe Antoine72333522018-05-03 16:40:24 +020093 MBEDTLS_SSL_IS_CLIENT,
94 MBEDTLS_SSL_TRANSPORT_STREAM,
Gilles Peskine449bd832023-01-11 14:50:10 +010095 MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020096 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010097 }
Philippe Antoine72333522018-05-03 16:40:24 +020098
Gilles Peskineeccd8882020-03-10 12:19:08 +010099#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +0200100 if (options & 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
102 (const unsigned char *) psk_id, sizeof(psk_id) - 1);
Philippe Antoine72333522018-05-03 16:40:24 +0200103 }
104#endif
105
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200106#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200107 if (options & 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
109 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
Philippe Antoine72333522018-05-03 16:40:24 +0200110 } else
111#endif
112 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
Philippe Antoine72333522018-05-03 16:40:24 +0200114 }
Philippe Antoine72333522018-05-03 16:40:24 +0200115#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_conf_extended_master_secret(&conf,
117 (options &
118 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200119#endif
120#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_ssl_conf_encrypt_then_mac(&conf,
122 (options &
123 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200124#endif
Philippe Antoine72333522018-05-03 16:40:24 +0200125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_ssl_conf_renegotiation(&conf,
127 (options &
128 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200129#endif
130#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_ssl_conf_session_tickets(&conf,
132 (options &
133 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Philippe Antoine72333522018-05-03 16:40:24 +0200134#endif
135#if defined(MBEDTLS_SSL_ALPN)
136 if (options & 0x200) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
Philippe Antoine72333522018-05-03 16:40:24 +0200138 }
139#endif
140 //There may be other options to add :
141 // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
142
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200143 srand(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
Philippe Antoine72333522018-05-03 16:40:24 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 }
Philippe Antoine72333522018-05-03 16:40:24 +0200149
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200150#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200151 if ((options & 1) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 }
Philippe Antoine72333522018-05-03 16:40:24 +0200155 }
156#endif
157
158 biomemfuzz.Data = Data;
159 biomemfuzz.Size = Size-2;
160 biomemfuzz.Offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
Philippe Antoine72333522018-05-03 16:40:24 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 ret = mbedtls_ssl_handshake(&ssl);
164 if (ret == 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200165 //keep reading data from server until the end
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 do {
167 len = sizeof(buf) - 1;
168 ret = mbedtls_ssl_read(&ssl, buf, len);
Philippe Antoine72333522018-05-03 16:40:24 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Philippe Antoine72333522018-05-03 16:40:24 +0200171 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 } else if (ret <= 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200173 //EOF or error
174 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 }
176 } while (1);
Philippe Antoine72333522018-05-03 16:40:24 +0200177 }
178
179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_entropy_free(&entropy);
181 mbedtls_ctr_drbg_free(&ctr_drbg);
182 mbedtls_ssl_config_free(&conf);
183 mbedtls_ssl_free(&ssl);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200184#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel774f9de2023-04-19 11:47:01 +0200185 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Philippe Antoine72333522018-05-03 16:40:24 +0200187
Philippe Antoinec32fd242019-06-06 09:12:53 +0200188#else
189 (void) Data;
190 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200191#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200192
Philippe Antoine72333522018-05-03 16:40:24 +0200193 return 0;
194}