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