2021-05-19 17:54:54 +02:00
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2018-05-03 16:40:24 +02:00
|
|
|
#include "mbedtls/ssl.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/ssl_ticket.h"
|
2021-02-08 15:34:42 +01:00
|
|
|
#include "test/certs.h"
|
2019-06-04 14:03:06 +02:00
|
|
|
#include "common.h"
|
2018-05-03 16:40:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
2020-05-20 10:35:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && \
|
|
|
|
defined(MBEDTLS_ENTROPY_C) && \
|
|
|
|
defined(MBEDTLS_CTR_DRBG_C)
|
2018-05-03 16:40:24 +02:00
|
|
|
const char *pers = "fuzz_server";
|
2019-07-10 14:26:31 +02:00
|
|
|
static int initialized = 0;
|
2019-06-28 12:31:23 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
|
2018-05-03 16:40:24 +02:00
|
|
|
static mbedtls_x509_crt srvcert;
|
|
|
|
static mbedtls_pk_context pkey;
|
|
|
|
#endif
|
|
|
|
const char *alpn_list[3];
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2018-05-03 16:40:24 +02:00
|
|
|
const unsigned char psk[] = {
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
|
|
|
};
|
|
|
|
const char psk_id[] = "Client_identity";
|
|
|
|
#endif
|
2020-05-20 10:35:01 +02:00
|
|
|
#endif // MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C
|
2018-05-03 16:40:24 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
|
|
|
|
{
|
2020-05-20 10:35:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && \
|
|
|
|
defined(MBEDTLS_ENTROPY_C) && \
|
|
|
|
defined(MBEDTLS_CTR_DRBG_C)
|
2018-05-03 16:40:24 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
mbedtls_ssl_context ssl;
|
|
|
|
mbedtls_ssl_config conf;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_entropy_context entropy;
|
2022-10-10 11:31:58 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
|
2018-05-03 16:40:24 +02:00
|
|
|
mbedtls_ssl_ticket_context ticket_ctx;
|
|
|
|
#endif
|
|
|
|
unsigned char buf[4096];
|
|
|
|
fuzzBufferOffset_t biomemfuzz;
|
|
|
|
uint8_t options;
|
|
|
|
|
|
|
|
//we take 1 byte as options input
|
|
|
|
if (Size < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
options = Data[Size - 1];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
mbedtls_entropy_init(&entropy);
|
2023-04-19 11:47:01 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
|
|
|
|
mbedtls_x509_crt_init(&srvcert);
|
|
|
|
mbedtls_pk_init(&pkey);
|
|
|
|
#endif
|
|
|
|
mbedtls_ssl_init(&ssl);
|
|
|
|
mbedtls_ssl_config_init(&conf);
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
|
|
|
|
mbedtls_ssl_ticket_init(&ticket_ctx);
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status = psa_crypto_init();
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-10 19:15:42 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
|
|
|
|
(const unsigned char *) pers, strlen(pers)) != 0) {
|
2022-02-10 19:15:42 +01:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-06-16 10:20:30 +02:00
|
|
|
|
2022-02-10 19:15:42 +01:00
|
|
|
if (initialized == 0) {
|
2021-06-16 10:20:30 +02:00
|
|
|
|
2019-06-28 12:31:23 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
|
|
|
|
mbedtls_test_srv_crt_len) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
|
|
|
|
mbedtls_test_cas_pem_len) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
|
2021-06-15 11:29:26 +02:00
|
|
|
mbedtls_test_srv_key_len, NULL, 0,
|
2023-01-11 14:50:10 +01:00
|
|
|
dummy_random, &ctr_drbg) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
alpn_list[0] = "HTTP";
|
|
|
|
alpn_list[1] = "fuzzalpn";
|
|
|
|
alpn_list[2] = NULL;
|
|
|
|
|
2019-06-04 14:03:06 +02:00
|
|
|
dummy_init();
|
|
|
|
|
2018-05-03 16:40:24 +02:00
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_config_defaults(&conf,
|
2018-05-03 16:40:24 +02:00
|
|
|
MBEDTLS_SSL_IS_SERVER,
|
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2019-06-04 12:05:36 +02:00
|
|
|
srand(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2019-06-28 12:31:23 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
|
|
|
|
if (mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_cert_req_ca_list(&conf,
|
|
|
|
(options &
|
|
|
|
0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED);
|
2018-05-03 16:40:24 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
if (options & 0x2) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
|
2018-05-03 16:40:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
2022-10-10 11:31:58 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (options & 0x4) {
|
|
|
|
if (mbedtls_ssl_ticket_setup(&ticket_ctx,
|
2018-05-03 16:40:24 +02:00
|
|
|
dummy_random, &ctr_drbg,
|
|
|
|
MBEDTLS_CIPHER_AES_256_GCM,
|
2023-01-11 14:50:10 +01:00
|
|
|
86400) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_session_tickets_cb(&conf,
|
2018-05-03 16:40:24 +02:00
|
|
|
mbedtls_ssl_ticket_write,
|
|
|
|
mbedtls_ssl_ticket_parse,
|
2023-01-11 14:50:10 +01:00
|
|
|
&ticket_ctx);
|
2018-05-03 16:40:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_extended_master_secret(&conf,
|
|
|
|
(options &
|
|
|
|
0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_encrypt_then_mac(&conf,
|
|
|
|
(options &
|
|
|
|
0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2018-05-03 16:40:24 +02:00
|
|
|
if (options & 0x40) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
|
|
|
|
(const unsigned char *) psk_id, sizeof(psk_id) - 1);
|
2018-05-03 16:40:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_renegotiation(&conf,
|
|
|
|
(options &
|
|
|
|
0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
|
|
|
|
biomemfuzz.Data = Data;
|
|
|
|
biomemfuzz.Size = Size-1;
|
|
|
|
biomemfuzz.Offset = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_reset(&ssl);
|
|
|
|
ret = mbedtls_ssl_handshake(&ssl);
|
|
|
|
if (ret == 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
//keep reading data from server until the end
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
ret = mbedtls_ssl_read(&ssl, buf, len);
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
2018-05-03 16:40:24 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ret <= 0) {
|
2018-05-03 16:40:24 +02:00
|
|
|
//EOF or error
|
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} while (1);
|
2018-05-03 16:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2022-10-10 11:31:58 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_ticket_free(&ticket_ctx);
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&entropy);
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
|
mbedtls_ssl_config_free(&conf);
|
2023-04-19 11:47:01 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
|
|
|
|
mbedtls_x509_crt_free(&srvcert);
|
|
|
|
mbedtls_pk_free(&pkey);
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_free(&ssl);
|
2023-04-19 11:47:01 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
mbedtls_psa_crypto_free();
|
|
|
|
#endif
|
2019-06-06 09:12:53 +02:00
|
|
|
#else
|
|
|
|
(void) Data;
|
|
|
|
(void) Size;
|
2020-05-20 10:35:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
2019-06-06 09:12:53 +02:00
|
|
|
|
2018-05-03 16:40:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|