1 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
2 
3 #include "mbedtls/ssl.h"
4 #include "mbedtls/entropy.h"
5 #include "mbedtls/ctr_drbg.h"
6 #include "test/certs.h"
7 #include "common.h"
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 
12 
13 #if defined(MBEDTLS_SSL_CLI_C) && \
14     defined(MBEDTLS_ENTROPY_C) && \
15     defined(MBEDTLS_CTR_DRBG_C)
16 static int initialized = 0;
17 #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
18 static mbedtls_x509_crt cacert;
19 #endif
20 const char *alpn_list[3];
21 
22 
23 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
24 const unsigned char psk[] = {
25     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
26     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
27 };
28 const char psk_id[] = "Client_identity";
29 #endif
30 
31 const char *pers = "fuzz_client";
32 #endif /* MBEDTLS_SSL_CLI_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_CLI_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     unsigned char buf[4096];
47     fuzzBufferOffset_t biomemfuzz;
48     uint16_t options;
49 
50     if (initialized == 0) {
51 #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
52         mbedtls_x509_crt_init(&cacert);
53         if (mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
54                                    mbedtls_test_cas_pem_len) != 0) {
55             return 1;
56         }
57 #endif
58 
59         alpn_list[0] = "HTTP";
60         alpn_list[1] = "fuzzalpn";
61         alpn_list[2] = NULL;
62 
63         dummy_init();
64 
65         initialized = 1;
66     }
67 
68     //we take 1 byte as options input
69     if (Size < 2) {
70         return 0;
71     }
72     options = (Data[Size - 2] << 8) | Data[Size - 1];
73     //Avoid warnings if compile options imply no options
74     (void) options;
75 
76     mbedtls_ssl_init(&ssl);
77     mbedtls_ssl_config_init(&conf);
78     mbedtls_ctr_drbg_init(&ctr_drbg);
79     mbedtls_entropy_init(&entropy);
80 
81     if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
82                               (const unsigned char *) pers, strlen(pers)) != 0) {
83         goto exit;
84     }
85 
86     if (mbedtls_ssl_config_defaults(&conf,
87                                     MBEDTLS_SSL_IS_CLIENT,
88                                     MBEDTLS_SSL_TRANSPORT_STREAM,
89                                     MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
90         goto exit;
91     }
92 
93 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
94     if (options & 2) {
95         mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
96                              (const unsigned char *) psk_id, sizeof(psk_id) - 1);
97     }
98 #endif
99 
100 #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
101     if (options & 4) {
102         mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
103         mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
104     } else
105 #endif
106     {
107         mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
108     }
109 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
110     mbedtls_ssl_conf_extended_master_secret(&conf,
111                                             (options &
112                                              0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
113 #endif
114 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
115     mbedtls_ssl_conf_encrypt_then_mac(&conf,
116                                       (options &
117                                        0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
118 #endif
119 #if defined(MBEDTLS_SSL_RENEGOTIATION)
120     mbedtls_ssl_conf_renegotiation(&conf,
121                                    (options &
122                                     0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED);
123 #endif
124 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
125     mbedtls_ssl_conf_session_tickets(&conf,
126                                      (options &
127                                       0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
128 #endif
129 #if defined(MBEDTLS_SSL_ALPN)
130     if (options & 0x200) {
131         mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list);
132     }
133 #endif
134     //There may be other options to add :
135     // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
136 
137     srand(1);
138     mbedtls_ssl_conf_rng(&conf, dummy_random, &ctr_drbg);
139 
140     if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
141         goto exit;
142     }
143 
144 #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
145     if ((options & 1) == 0) {
146         if (mbedtls_ssl_set_hostname(&ssl, "localhost") != 0) {
147             goto exit;
148         }
149     }
150 #endif
151 
152     biomemfuzz.Data = Data;
153     biomemfuzz.Size = Size-2;
154     biomemfuzz.Offset = 0;
155     mbedtls_ssl_set_bio(&ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL);
156 
157     ret = mbedtls_ssl_handshake(&ssl);
158     if (ret == 0) {
159         //keep reading data from server until the end
160         do {
161             len = sizeof(buf) - 1;
162             ret = mbedtls_ssl_read(&ssl, buf, len);
163 
164             if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
165                 continue;
166             } else if (ret <= 0) {
167                 //EOF or error
168                 break;
169             }
170         } while (1);
171     }
172 
173 exit:
174     mbedtls_entropy_free(&entropy);
175     mbedtls_ctr_drbg_free(&ctr_drbg);
176     mbedtls_ssl_config_free(&conf);
177     mbedtls_ssl_free(&ssl);
178 
179 #else
180     (void) Data;
181     (void) Size;
182 #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
183 
184     return 0;
185 }
186