blob: 07ca96efa8ef86f4249adfb4ec89e83c8a7f7850 [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"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01004#include "test/certs.h"
Philippe Antoine08633822019-06-04 14:03:06 +02005#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02006#include <string.h>
7#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +02008#include <stdint.h>
9
10
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020011#if defined(MBEDTLS_SSL_CLI_C) && \
12 defined(MBEDTLS_ENTROPY_C) && \
13 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine42a2ce82019-07-10 14:26:31 +020014static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020015#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020016static mbedtls_x509_crt cacert;
17#endif
18const char *alpn_list[3];
19
20
Gilles Peskineeccd8882020-03-10 12:19:08 +010021#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Philippe Antoine72333522018-05-03 16:40:24 +020022const unsigned char psk[] = {
23 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
24 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
25};
26const char psk_id[] = "Client_identity";
27#endif
28
29const char *pers = "fuzz_client";
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020030#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoine72333522018-05-03 16:40:24 +020031
32
Gilles Peskine449bd832023-01-11 14:50:10 +010033int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
34{
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020035#if defined(MBEDTLS_SSL_CLI_C) && \
36 defined(MBEDTLS_ENTROPY_C) && \
37 defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine72333522018-05-03 16:40:24 +020038 int ret;
39 size_t len;
40 mbedtls_ssl_context ssl;
41 mbedtls_ssl_config conf;
42 mbedtls_ctr_drbg_context ctr_drbg;
43 mbedtls_entropy_context entropy;
44 unsigned char buf[4096];
45 fuzzBufferOffset_t biomemfuzz;
46 uint16_t options;
47
48 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020049#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_x509_crt_init(&cacert);
51 if (mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
52 mbedtls_test_cas_pem_len) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +020053 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010054 }
Philippe Antoine72333522018-05-03 16:40:24 +020055#endif
56
57 alpn_list[0] = "HTTP";
58 alpn_list[1] = "fuzzalpn";
59 alpn_list[2] = NULL;
60
Philippe Antoine08633822019-06-04 14:03:06 +020061 dummy_init();
62
Philippe Antoine72333522018-05-03 16:40:24 +020063 initialized = 1;
64 }
65
66 //we take 1 byte as options input
67 if (Size < 2) {
68 return 0;
69 }
70 options = (Data[Size - 2] << 8) | Data[Size - 1];
71 //Avoid warnings if compile options imply no options
72 (void) options;
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_ssl_init(&ssl);
75 mbedtls_ssl_config_init(&conf);
76 mbedtls_ctr_drbg_init(&ctr_drbg);
77 mbedtls_entropy_init(&entropy);
Philippe Antoine72333522018-05-03 16:40:24 +020078
Przemek Stekiel774f9de2023-04-19 11:47:01 +020079#if defined(MBEDTLS_USE_PSA_CRYPTO)
80 psa_status_t status = psa_crypto_init();
81 if (status != PSA_SUCCESS) {
82 goto exit;
83 }
84#endif /* MBEDTLS_USE_PSA_CRYPTO */
85
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 :
140 // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
141
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200142 srand(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
Philippe Antoine72333522018-05-03 16:40:24 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
Philippe Antoine72333522018-05-03 16:40:24 +0200148
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200149#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200150 if ((options & 1) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 }
Philippe Antoine72333522018-05-03 16:40:24 +0200154 }
155#endif
156
157 biomemfuzz.Data = Data;
158 biomemfuzz.Size = Size-2;
159 biomemfuzz.Offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
Philippe Antoine72333522018-05-03 16:40:24 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 ret = mbedtls_ssl_handshake(&ssl);
163 if (ret == 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200164 //keep reading data from server until the end
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 do {
166 len = sizeof(buf) - 1;
167 ret = mbedtls_ssl_read(&ssl, buf, len);
Philippe Antoine72333522018-05-03 16:40:24 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Philippe Antoine72333522018-05-03 16:40:24 +0200170 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 } else if (ret <= 0) {
Philippe Antoine72333522018-05-03 16:40:24 +0200172 //EOF or error
173 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 }
175 } while (1);
Philippe Antoine72333522018-05-03 16:40:24 +0200176 }
177
178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_entropy_free(&entropy);
180 mbedtls_ctr_drbg_free(&ctr_drbg);
181 mbedtls_ssl_config_free(&conf);
182 mbedtls_ssl_free(&ssl);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200183#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel774f9de2023-04-19 11:47:01 +0200184 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200185#endif /* MBEDTLS_USE_PSA_CRYPTO */
Philippe Antoine72333522018-05-03 16:40:24 +0200186
Philippe Antoinec32fd242019-06-06 09:12:53 +0200187#else
188 (void) Data;
189 (void) Size;
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +0200190#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Philippe Antoinec32fd242019-06-06 09:12:53 +0200191
Philippe Antoine72333522018-05-03 16:40:24 +0200192 return 0;
193}