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