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" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 4 | #include "mbedtls/ssl_ticket.h" |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 5 | #include "test/certs.h" |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 6 | #include "common.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
| 11 | |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 12 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 13 | defined(MBEDTLS_ENTROPY_C) && \ |
| 14 | defined(MBEDTLS_CTR_DRBG_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 15 | const char *pers = "fuzz_server"; |
Philippe Antoine | 42a2ce8 | 2019-07-10 14:26:31 +0200 | [diff] [blame] | 16 | static int initialized = 0; |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 17 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 18 | static mbedtls_x509_crt srvcert; |
| 19 | static mbedtls_pk_context pkey; |
| 20 | #endif |
| 21 | const char *alpn_list[3]; |
| 22 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 23 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 24 | const unsigned char psk[] = { |
| 25 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 26 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 27 | }; |
| 28 | const char psk_id[] = "Client_identity"; |
| 29 | #endif |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 30 | #endif // MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 31 | |
| 32 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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_SRV_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; |
Przemek Stekiel | 68a01a6 | 2022-10-10 11:31:58 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 45 | mbedtls_ssl_ticket_context ticket_ctx; |
| 46 | #endif |
| 47 | unsigned char buf[4096]; |
| 48 | fuzzBufferOffset_t biomemfuzz; |
| 49 | uint8_t options; |
| 50 | |
| 51 | //we take 1 byte as options input |
| 52 | if (Size < 1) { |
| 53 | return 0; |
| 54 | } |
| 55 | options = Data[Size - 1]; |
| 56 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 58 | mbedtls_entropy_init(&entropy); |
Przemek Stekiel | 774f9de | 2023-04-19 11:47:01 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
| 60 | mbedtls_x509_crt_init(&srvcert); |
| 61 | mbedtls_pk_init(&pkey); |
| 62 | #endif |
| 63 | mbedtls_ssl_init(&ssl); |
| 64 | mbedtls_ssl_config_init(&conf); |
| 65 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
| 66 | mbedtls_ssl_ticket_init(&ticket_ctx); |
| 67 | #endif |
| 68 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 69 | psa_status_t status = psa_crypto_init(); |
| 70 | if (status != PSA_SUCCESS) { |
| 71 | goto exit; |
| 72 | } |
| 73 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 7f93da1 | 2021-06-16 10:20:30 +0200 | [diff] [blame] | 74 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy, |
| 76 | (const unsigned char *) pers, strlen(pers)) != 0) { |
Paul Elliott | 00738bf | 2022-02-10 18:15:42 +0000 | [diff] [blame] | 77 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | } |
Paul Elliott | 00738bf | 2022-02-10 18:15:42 +0000 | [diff] [blame] | 79 | |
| 80 | if (initialized == 0) { |
Manuel Pégourié-Gonnard | 7f93da1 | 2021-06-16 10:20:30 +0200 | [diff] [blame] | 81 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 82 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 84 | mbedtls_test_srv_crt_len) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 85 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | } |
| 87 | if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 88 | mbedtls_test_cas_pem_len) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 89 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | } |
| 91 | if (mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 92 | mbedtls_test_srv_key_len, NULL, 0, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | dummy_random, &ctr_drbg) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 94 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | alpn_list[0] = "HTTP"; |
| 99 | alpn_list[1] = "fuzzalpn"; |
| 100 | alpn_list[2] = NULL; |
| 101 | |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 102 | dummy_init(); |
| 103 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 104 | initialized = 1; |
| 105 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | if (mbedtls_ssl_config_defaults(&conf, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 108 | MBEDTLS_SSL_IS_SERVER, |
| 109 | MBEDTLS_SSL_TRANSPORT_STREAM, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | MBEDTLS_SSL_PRESET_DEFAULT) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 111 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 113 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 114 | srand(1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 116 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL); |
| 119 | if (mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 120 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 122 | #endif |
| 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | mbedtls_ssl_conf_cert_req_ca_list(&conf, |
| 125 | (options & |
| 126 | 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 127 | #if defined(MBEDTLS_SSL_ALPN) |
| 128 | if (options & 0x2) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 130 | } |
| 131 | #endif |
Przemek Stekiel | 68a01a6 | 2022-10-10 11:31:58 +0200 | [diff] [blame] | 132 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | if (options & 0x4) { |
| 134 | if (mbedtls_ssl_ticket_setup(&ticket_ctx, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 135 | dummy_random, &ctr_drbg, |
| 136 | MBEDTLS_CIPHER_AES_256_GCM, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | 86400) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 138 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | mbedtls_ssl_conf_session_tickets_cb(&conf, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 142 | mbedtls_ssl_ticket_write, |
| 143 | mbedtls_ssl_ticket_parse, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | &ticket_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 145 | } |
| 146 | #endif |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | mbedtls_ssl_conf_extended_master_secret(&conf, |
| 149 | (options & |
| 150 | 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 151 | #endif |
| 152 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | mbedtls_ssl_conf_encrypt_then_mac(&conf, |
| 154 | (options & |
| 155 | 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 156 | #endif |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 157 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 158 | if (options & 0x40) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk), |
| 160 | (const unsigned char *) psk_id, sizeof(psk_id) - 1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 161 | } |
| 162 | #endif |
| 163 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | mbedtls_ssl_conf_renegotiation(&conf, |
| 165 | (options & |
| 166 | 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 167 | #endif |
| 168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | if (mbedtls_ssl_setup(&ssl, &conf) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 170 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 172 | |
| 173 | biomemfuzz.Data = Data; |
| 174 | biomemfuzz.Size = Size-1; |
| 175 | biomemfuzz.Offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | mbedtls_ssl_session_reset(&ssl); |
| 179 | ret = mbedtls_ssl_handshake(&ssl); |
| 180 | if (ret == 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 181 | //keep reading data from server until the end |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | do { |
| 183 | len = sizeof(buf) - 1; |
| 184 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 187 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | } else if (ret <= 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 189 | //EOF or error |
| 190 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | } |
| 192 | } while (1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | exit: |
Przemek Stekiel | 68a01a6 | 2022-10-10 11:31:58 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | mbedtls_ssl_ticket_free(&ticket_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 198 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | mbedtls_entropy_free(&entropy); |
| 200 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 201 | mbedtls_ssl_config_free(&conf); |
Przemek Stekiel | 774f9de | 2023-04-19 11:47:01 +0200 | [diff] [blame] | 202 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
| 203 | mbedtls_x509_crt_free(&srvcert); |
| 204 | mbedtls_pk_free(&pkey); |
| 205 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | mbedtls_ssl_free(&ssl); |
Przemek Stekiel | 774f9de | 2023-04-19 11:47:01 +0200 | [diff] [blame] | 207 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 208 | mbedtls_psa_crypto_free(); |
| 209 | #endif |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 210 | #else |
| 211 | (void) Data; |
| 212 | (void) Size; |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 213 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 214 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 215 | return 0; |
| 216 | } |