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