Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 1 | #include "mbedtls/ssl.h" |
| 2 | #include "mbedtls/entropy.h" |
| 3 | #include "mbedtls/ctr_drbg.h" |
| 4 | #include "mbedtls/certs.h" |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 5 | #include "common.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 11 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 12 | defined(MBEDTLS_ENTROPY_C) && \ |
| 13 | defined(MBEDTLS_CTR_DRBG_C) |
Philippe Antoine | 42a2ce8 | 2019-07-10 14:26:31 +0200 | [diff] [blame] | 14 | static int initialized = 0; |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 15 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 16 | static mbedtls_x509_crt cacert; |
| 17 | #endif |
| 18 | const char *alpn_list[3]; |
| 19 | |
| 20 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 21 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 22 | const unsigned char psk[] = { |
| 23 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 24 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 25 | }; |
| 26 | const char psk_id[] = "Client_identity"; |
| 27 | #endif |
| 28 | |
| 29 | const char *pers = "fuzz_client"; |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 30 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 31 | |
| 32 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 33 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 34 | { |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 36 | defined(MBEDTLS_ENTROPY_C) && \ |
| 37 | defined(MBEDTLS_CTR_DRBG_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 38 | 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 Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 50 | 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 Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 53 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 54 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | alpn_list[0] = "HTTP"; |
| 58 | alpn_list[1] = "fuzzalpn"; |
| 59 | alpn_list[2] = NULL; |
| 60 | |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 61 | dummy_init(); |
| 62 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 63 | 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 74 | mbedtls_ssl_init(&ssl); |
| 75 | mbedtls_ssl_config_init(&conf); |
| 76 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 77 | mbedtls_entropy_init(&entropy); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 78 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 79 | if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy, |
| 80 | (const unsigned char *) pers, strlen(pers)) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 81 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 82 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 84 | if (mbedtls_ssl_config_defaults(&conf, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 85 | MBEDTLS_SSL_IS_CLIENT, |
| 86 | MBEDTLS_SSL_TRANSPORT_STREAM, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 87 | MBEDTLS_SSL_PRESET_DEFAULT) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 88 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 89 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 90 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 91 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 92 | if (options & 2) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 93 | mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk), |
| 94 | (const unsigned char *) psk_id, sizeof(psk_id) - 1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 95 | } |
| 96 | #endif |
| 97 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 98 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 99 | if (options & 4) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 100 | mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); |
| 101 | mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 102 | } else |
| 103 | #endif |
| 104 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 105 | mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 106 | } |
| 107 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 108 | mbedtls_ssl_conf_truncated_hmac(&conf, |
| 109 | (options & |
| 110 | 8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 111 | #endif |
| 112 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 113 | mbedtls_ssl_conf_extended_master_secret(&conf, |
| 114 | (options & |
| 115 | 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 116 | #endif |
| 117 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 118 | mbedtls_ssl_conf_encrypt_then_mac(&conf, |
| 119 | (options & |
| 120 | 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 121 | #endif |
| 122 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 123 | mbedtls_ssl_conf_cbc_record_splitting(&conf, |
| 124 | (options & |
| 125 | 0x40) ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 126 | #endif |
| 127 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 128 | mbedtls_ssl_conf_renegotiation(&conf, |
| 129 | (options & |
| 130 | 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 131 | #endif |
| 132 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 133 | mbedtls_ssl_conf_session_tickets(&conf, |
| 134 | (options & |
| 135 | 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 136 | #endif |
| 137 | #if defined(MBEDTLS_SSL_ALPN) |
| 138 | if (options & 0x200) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 139 | mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 140 | } |
| 141 | #endif |
| 142 | //There may be other options to add : |
| 143 | // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes |
| 144 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 145 | srand(1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 146 | mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 147 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 148 | if (mbedtls_ssl_setup(&ssl, &conf) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 149 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 150 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 151 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 152 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 153 | if ((options & 1) == 0) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 154 | if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 155 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 156 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 157 | } |
| 158 | #endif |
| 159 | |
| 160 | biomemfuzz.Data = Data; |
| 161 | biomemfuzz.Size = Size-2; |
| 162 | biomemfuzz.Offset = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 163 | mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 164 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 165 | ret = mbedtls_ssl_handshake(&ssl); |
| 166 | if (ret == 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 167 | //keep reading data from server until the end |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 168 | do { |
| 169 | len = sizeof(buf) - 1; |
| 170 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 172 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 173 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 174 | } else if (ret <= 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 175 | //EOF or error |
| 176 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 177 | } |
| 178 | } while (1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 182 | mbedtls_entropy_free(&entropy); |
| 183 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 184 | mbedtls_ssl_config_free(&conf); |
| 185 | mbedtls_ssl_free(&ssl); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 186 | |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 187 | #else |
| 188 | (void) Data; |
| 189 | (void) Size; |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 190 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 191 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 192 | return 0; |
| 193 | } |