1a8e1175bSopenharmony_ci#include <string.h>
2a8e1175bSopenharmony_ci#include <stdlib.h>
3a8e1175bSopenharmony_ci#include <stdint.h>
4a8e1175bSopenharmony_ci#include "common.h"
5a8e1175bSopenharmony_ci#include "mbedtls/ssl.h"
6a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
7a8e1175bSopenharmony_ci#include "mbedtls/entropy.h"
8a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h"
9a8e1175bSopenharmony_ci#include "mbedtls/timing.h"
10a8e1175bSopenharmony_ci#include "test/certs.h"
11a8e1175bSopenharmony_ci
12a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C) && \
13a8e1175bSopenharmony_ci    defined(MBEDTLS_ENTROPY_C) && \
14a8e1175bSopenharmony_ci    defined(MBEDTLS_CTR_DRBG_C) && \
15a8e1175bSopenharmony_ci    defined(MBEDTLS_TIMING_C)
16a8e1175bSopenharmony_cistatic int initialized = 0;
17a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
18a8e1175bSopenharmony_cistatic mbedtls_x509_crt cacert;
19a8e1175bSopenharmony_ci#endif
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ciconst char *pers = "fuzz_dtlsclient";
22a8e1175bSopenharmony_ci#endif
23a8e1175bSopenharmony_ci#endif // MBEDTLS_SSL_PROTO_DTLS
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_ci
27a8e1175bSopenharmony_ciint LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
28a8e1175bSopenharmony_ci{
29a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS) && \
30a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_CLI_C) && \
31a8e1175bSopenharmony_ci    defined(MBEDTLS_ENTROPY_C) && \
32a8e1175bSopenharmony_ci    defined(MBEDTLS_CTR_DRBG_C) && \
33a8e1175bSopenharmony_ci    defined(MBEDTLS_TIMING_C)
34a8e1175bSopenharmony_ci    int ret;
35a8e1175bSopenharmony_ci    size_t len;
36a8e1175bSopenharmony_ci    mbedtls_ssl_context ssl;
37a8e1175bSopenharmony_ci    mbedtls_ssl_config conf;
38a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_context ctr_drbg;
39a8e1175bSopenharmony_ci    mbedtls_entropy_context entropy;
40a8e1175bSopenharmony_ci    mbedtls_timing_delay_context timer;
41a8e1175bSopenharmony_ci    unsigned char buf[4096];
42a8e1175bSopenharmony_ci    fuzzBufferOffset_t biomemfuzz;
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci    if (initialized == 0) {
45a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
46a8e1175bSopenharmony_ci        mbedtls_x509_crt_init(&cacert);
47a8e1175bSopenharmony_ci        if (mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
48a8e1175bSopenharmony_ci                                   mbedtls_test_cas_pem_len) != 0) {
49a8e1175bSopenharmony_ci            return 1;
50a8e1175bSopenharmony_ci        }
51a8e1175bSopenharmony_ci#endif
52a8e1175bSopenharmony_ci        dummy_init();
53a8e1175bSopenharmony_ci
54a8e1175bSopenharmony_ci        initialized = 1;
55a8e1175bSopenharmony_ci    }
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ci    mbedtls_ssl_init(&ssl);
58a8e1175bSopenharmony_ci    mbedtls_ssl_config_init(&conf);
59a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_init(&ctr_drbg);
60a8e1175bSopenharmony_ci    mbedtls_entropy_init(&entropy);
61a8e1175bSopenharmony_ci
62a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
63a8e1175bSopenharmony_ci    psa_status_t status = psa_crypto_init();
64a8e1175bSopenharmony_ci    if (status != PSA_SUCCESS) {
65a8e1175bSopenharmony_ci        goto exit;
66a8e1175bSopenharmony_ci    }
67a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_ci    srand(1);
70a8e1175bSopenharmony_ci    if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
71a8e1175bSopenharmony_ci                              (const unsigned char *) pers, strlen(pers)) != 0) {
72a8e1175bSopenharmony_ci        goto exit;
73a8e1175bSopenharmony_ci    }
74a8e1175bSopenharmony_ci
75a8e1175bSopenharmony_ci    if (mbedtls_ssl_config_defaults(&conf,
76a8e1175bSopenharmony_ci                                    MBEDTLS_SSL_IS_CLIENT,
77a8e1175bSopenharmony_ci                                    MBEDTLS_SSL_TRANSPORT_DATAGRAM,
78a8e1175bSopenharmony_ci                                    MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
79a8e1175bSopenharmony_ci        goto exit;
80a8e1175bSopenharmony_ci    }
81a8e1175bSopenharmony_ci
82a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
83a8e1175bSopenharmony_ci    mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
84a8e1175bSopenharmony_ci#endif
85a8e1175bSopenharmony_ci    mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
86a8e1175bSopenharmony_ci    mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
87a8e1175bSopenharmony_ci
88a8e1175bSopenharmony_ci    if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
89a8e1175bSopenharmony_ci        goto exit;
90a8e1175bSopenharmony_ci    }
91a8e1175bSopenharmony_ci
92a8e1175bSopenharmony_ci    mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
93a8e1175bSopenharmony_ci                             mbedtls_timing_get_delay);
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
96a8e1175bSopenharmony_ci    if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
97a8e1175bSopenharmony_ci        goto exit;
98a8e1175bSopenharmony_ci    }
99a8e1175bSopenharmony_ci#endif
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci    biomemfuzz.Data = Data;
102a8e1175bSopenharmony_ci    biomemfuzz.Size = Size;
103a8e1175bSopenharmony_ci    biomemfuzz.Offset = 0;
104a8e1175bSopenharmony_ci    mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, fuzz_recv_timeout);
105a8e1175bSopenharmony_ci
106a8e1175bSopenharmony_ci    ret = mbedtls_ssl_handshake(&ssl);
107a8e1175bSopenharmony_ci    if (ret == 0) {
108a8e1175bSopenharmony_ci        //keep reading data from server until the end
109a8e1175bSopenharmony_ci        do {
110a8e1175bSopenharmony_ci            len = sizeof(buf) - 1;
111a8e1175bSopenharmony_ci            ret = mbedtls_ssl_read(&ssl, buf, len);
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci            if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
114a8e1175bSopenharmony_ci                continue;
115a8e1175bSopenharmony_ci            } else if (ret <= 0) {
116a8e1175bSopenharmony_ci                //EOF or error
117a8e1175bSopenharmony_ci                break;
118a8e1175bSopenharmony_ci            }
119a8e1175bSopenharmony_ci        } while (1);
120a8e1175bSopenharmony_ci    }
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ciexit:
123a8e1175bSopenharmony_ci    mbedtls_entropy_free(&entropy);
124a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_free(&ctr_drbg);
125a8e1175bSopenharmony_ci    mbedtls_ssl_config_free(&conf);
126a8e1175bSopenharmony_ci    mbedtls_ssl_free(&ssl);
127a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
128a8e1175bSopenharmony_ci    mbedtls_psa_crypto_free();
129a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
130a8e1175bSopenharmony_ci
131a8e1175bSopenharmony_ci#else
132a8e1175bSopenharmony_ci    (void) Data;
133a8e1175bSopenharmony_ci    (void) Size;
134a8e1175bSopenharmony_ci#endif
135a8e1175bSopenharmony_ci    return 0;
136a8e1175bSopenharmony_ci}
137