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" |
| 5 | #include "mbedtls/ssl_ticket.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 | 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_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 | 7aca4e4 | 2022-10-10 14:14:13 +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 | |
| 57 | if (initialized == 0) { |
Gilles Peskine | 90420aa | 2023-02-01 18:20:16 +0100 | [diff] [blame] | 58 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) && \ |
| 59 | defined(MBEDTLS_CERTS_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 60 | mbedtls_x509_crt_init(&srvcert); |
| 61 | mbedtls_pk_init(&pkey); |
| 62 | if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 63 | mbedtls_test_srv_crt_len) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 64 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | } |
| 66 | if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 67 | mbedtls_test_cas_pem_len) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 68 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | } |
| 70 | if (mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key, |
| 71 | mbedtls_test_srv_key_len, NULL, 0) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 72 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | alpn_list[0] = "HTTP"; |
| 77 | alpn_list[1] = "fuzzalpn"; |
| 78 | alpn_list[2] = NULL; |
| 79 | |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 80 | dummy_init(); |
| 81 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 82 | initialized = 1; |
| 83 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | mbedtls_ssl_init(&ssl); |
| 85 | mbedtls_ssl_config_init(&conf); |
| 86 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 87 | mbedtls_entropy_init(&entropy); |
Przemek Stekiel | 7aca4e4 | 2022-10-10 14:14:13 +0200 | [diff] [blame] | 88 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | mbedtls_ssl_ticket_init(&ticket_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 90 | #endif |
| 91 | |
Przemek Stekiel | 8fa17b6 | 2023-04-19 11:47:01 +0200 | [diff] [blame] | 92 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 93 | psa_status_t status = psa_crypto_init(); |
| 94 | if (status != PSA_SUCCESS) { |
| 95 | goto exit; |
| 96 | } |
| 97 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 98 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy, |
| 100 | (const unsigned char *) pers, strlen(pers)) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 101 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 103 | |
| 104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | if (mbedtls_ssl_config_defaults(&conf, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 106 | MBEDTLS_SSL_IS_SERVER, |
| 107 | MBEDTLS_SSL_TRANSPORT_STREAM, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | MBEDTLS_SSL_PRESET_DEFAULT) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 109 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 111 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 112 | srand(1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 114 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 115 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL); |
| 117 | if (mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 118 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 120 | #endif |
| 121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | mbedtls_ssl_conf_cert_req_ca_list(&conf, |
| 123 | (options & |
| 124 | 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] | 125 | #if defined(MBEDTLS_SSL_ALPN) |
| 126 | if (options & 0x2) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 128 | } |
| 129 | #endif |
Przemek Stekiel | 7aca4e4 | 2022-10-10 14:14:13 +0200 | [diff] [blame] | 130 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | if (options & 0x4) { |
| 132 | if (mbedtls_ssl_ticket_setup(&ticket_ctx, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 133 | dummy_random, &ctr_drbg, |
| 134 | MBEDTLS_CIPHER_AES_256_GCM, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | 86400) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 136 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 137 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | mbedtls_ssl_conf_session_tickets_cb(&conf, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 140 | mbedtls_ssl_ticket_write, |
| 141 | mbedtls_ssl_ticket_parse, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | &ticket_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 143 | } |
| 144 | #endif |
| 145 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | mbedtls_ssl_conf_truncated_hmac(&conf, |
| 147 | (options & |
| 148 | 0x8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 149 | #endif |
| 150 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | mbedtls_ssl_conf_extended_master_secret(&conf, |
| 152 | (options & |
| 153 | 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 154 | #endif |
| 155 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | mbedtls_ssl_conf_encrypt_then_mac(&conf, |
| 157 | (options & |
| 158 | 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 159 | #endif |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 160 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 161 | if (options & 0x40) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk), |
| 163 | (const unsigned char *) psk_id, sizeof(psk_id) - 1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | mbedtls_ssl_conf_renegotiation(&conf, |
| 168 | (options & |
| 169 | 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 170 | #endif |
| 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | if (mbedtls_ssl_setup(&ssl, &conf) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 173 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 175 | |
| 176 | biomemfuzz.Data = Data; |
| 177 | biomemfuzz.Size = Size-1; |
| 178 | biomemfuzz.Offset = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | mbedtls_ssl_session_reset(&ssl); |
| 182 | ret = mbedtls_ssl_handshake(&ssl); |
| 183 | if (ret == 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 184 | //keep reading data from server until the end |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | do { |
| 186 | len = sizeof(buf) - 1; |
| 187 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 190 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 191 | } else if (ret <= 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 192 | //EOF or error |
| 193 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | } |
| 195 | } while (1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | exit: |
Przemek Stekiel | 7aca4e4 | 2022-10-10 14:14:13 +0200 | [diff] [blame] | 199 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | mbedtls_ssl_ticket_free(&ticket_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 201 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | mbedtls_entropy_free(&entropy); |
| 203 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 204 | mbedtls_ssl_config_free(&conf); |
Przemek Stekiel | 8fa17b6 | 2023-04-19 11:47:01 +0200 | [diff] [blame] | 205 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
| 206 | mbedtls_x509_crt_free(&srvcert); |
| 207 | mbedtls_pk_free(&pkey); |
| 208 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | mbedtls_ssl_free(&ssl); |
Przemek Stekiel | 8fa17b6 | 2023-04-19 11:47:01 +0200 | [diff] [blame] | 210 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 211 | mbedtls_psa_crypto_free(); |
| 212 | #endif |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 213 | #else |
| 214 | (void) Data; |
| 215 | (void) Size; |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 216 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 217 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 218 | return 0; |
| 219 | } |