Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include <stdlib.h> |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 3 | #include <stdint.h> |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 4 | #include "common.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 5 | #include "mbedtls/ssl.h" |
| 6 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7 | #include "mbedtls/entropy.h" |
| 8 | #include "mbedtls/ctr_drbg.h" |
| 9 | #include "mbedtls/certs.h" |
| 10 | #include "mbedtls/timing.h" |
| 11 | #include "mbedtls/ssl_cookie.h" |
| 12 | |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 13 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 14 | defined(MBEDTLS_ENTROPY_C) && \ |
| 15 | defined(MBEDTLS_CTR_DRBG_C) && \ |
Andrzej Kurek | e470821 | 2022-10-20 06:46:16 -0400 | [diff] [blame] | 16 | defined(MBEDTLS_TIMING_C) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 17 | (defined(MBEDTLS_SHA256_C) || \ |
| 18 | (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 19 | const char *pers = "fuzz_dtlsserver"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 20 | const unsigned char client_ip[4] = { 0x7F, 0, 0, 1 }; |
Philippe Antoine | 42a2ce8 | 2019-07-10 14:26:31 +0200 | [diff] [blame] | 21 | static int initialized = 0; |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 23 | static mbedtls_x509_crt srvcert; |
| 24 | static mbedtls_pk_context pkey; |
| 25 | #endif |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 26 | #endif |
Philippe Antoine | adc23e6 | 2019-06-25 21:53:12 +0200 | [diff] [blame] | 27 | #endif // MBEDTLS_SSL_PROTO_DTLS |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 28 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 30 | { |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && \ |
| 32 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 33 | defined(MBEDTLS_ENTROPY_C) && \ |
| 34 | defined(MBEDTLS_CTR_DRBG_C) && \ |
Andrzej Kurek | e470821 | 2022-10-20 06:46:16 -0400 | [diff] [blame] | 35 | defined(MBEDTLS_TIMING_C) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | (defined(MBEDTLS_SHA256_C) || \ |
| 37 | (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))) |
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 | mbedtls_timing_delay_context timer; |
| 45 | mbedtls_ssl_cookie_ctx cookie_ctx; |
| 46 | unsigned char buf[4096]; |
| 47 | fuzzBufferOffset_t biomemfuzz; |
| 48 | |
| 49 | if (initialized == 0) { |
Gilles Peskine | 90420aa | 2023-02-01 18:20:16 +0100 | [diff] [blame] | 50 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) && \ |
| 51 | defined(MBEDTLS_CERTS_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | mbedtls_x509_crt_init(&srvcert); |
| 53 | mbedtls_pk_init(&pkey); |
| 54 | if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 55 | mbedtls_test_srv_crt_len) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 56 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | } |
| 58 | if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 59 | mbedtls_test_cas_pem_len) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 60 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | } |
| 62 | if (mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key, |
| 63 | mbedtls_test_srv_key_len, NULL, 0) != 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 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 66 | #endif |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 67 | dummy_init(); |
| 68 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 69 | initialized = 1; |
| 70 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | mbedtls_ssl_init(&ssl); |
| 72 | mbedtls_ssl_config_init(&conf); |
| 73 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 74 | mbedtls_entropy_init(&entropy); |
| 75 | mbedtls_ssl_cookie_init(&cookie_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 76 | |
Przemek Stekiel | 8fa17b6 | 2023-04-19 11:47:01 +0200 | [diff] [blame^] | 77 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 78 | psa_status_t status = psa_crypto_init(); |
| 79 | if (status != PSA_SUCCESS) { |
| 80 | goto exit; |
| 81 | } |
| 82 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy, |
| 85 | (const unsigned char *) pers, strlen(pers)) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 86 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 87 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 88 | |
| 89 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | if (mbedtls_ssl_config_defaults(&conf, |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 91 | MBEDTLS_SSL_IS_SERVER, |
| 92 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | MBEDTLS_SSL_PRESET_DEFAULT) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 94 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 96 | |
| 97 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 98 | srand(1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 100 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 101 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL); |
| 103 | if (mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 104 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 106 | #endif |
| 107 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | if (mbedtls_ssl_cookie_setup(&cookie_ctx, dummy_random, &ctr_drbg) != 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | mbedtls_ssl_conf_dtls_cookies(&conf, |
| 113 | mbedtls_ssl_cookie_write, |
| 114 | mbedtls_ssl_cookie_check, |
| 115 | &cookie_ctx); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 116 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 117 | if (mbedtls_ssl_setup(&ssl, &conf) != 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, |
| 122 | mbedtls_timing_get_delay); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 123 | |
| 124 | biomemfuzz.Data = Data; |
| 125 | biomemfuzz.Size = Size; |
| 126 | biomemfuzz.Offset = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout); |
| 128 | if (mbedtls_ssl_set_client_transport_id(&ssl, client_ip, sizeof(client_ip)) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 129 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | ret = mbedtls_ssl_handshake(&ssl); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 133 | |
| 134 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
| 135 | biomemfuzz.Offset = ssl.next_record_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | mbedtls_ssl_session_reset(&ssl); |
| 137 | mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout); |
| 138 | if (mbedtls_ssl_set_client_transport_id(&ssl, client_ip, sizeof(client_ip)) != 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 139 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | } |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 141 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | ret = mbedtls_ssl_handshake(&ssl); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | if (ret == 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 145 | //keep reading data from server until the end |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | do { |
| 147 | len = sizeof(buf) - 1; |
| 148 | ret = mbedtls_ssl_read(&ssl, buf, len); |
| 149 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 150 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | } else if (ret <= 0) { |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 152 | //EOF or error |
| 153 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | } |
| 155 | } while (1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | mbedtls_ssl_cookie_free(&cookie_ctx); |
| 161 | mbedtls_entropy_free(&entropy); |
Przemek Stekiel | 8fa17b6 | 2023-04-19 11:47:01 +0200 | [diff] [blame^] | 162 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
| 163 | mbedtls_pk_free(&pkey); |
| 164 | mbedtls_x509_crt_free(&srvcert); |
| 165 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 167 | mbedtls_ssl_config_free(&conf); |
| 168 | mbedtls_ssl_free(&ssl); |
Przemek Stekiel | 8fa17b6 | 2023-04-19 11:47:01 +0200 | [diff] [blame^] | 169 | mbedtls_psa_crypto_free(); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 170 | |
| 171 | #else |
| 172 | (void) Data; |
| 173 | (void) Size; |
| 174 | #endif |
| 175 | return 0; |
| 176 | } |