blob: 94c80f6c3f1581fc60e8bc5df839e37b7b743d90 [file] [log] [blame]
Philippe Antoine72333522018-05-03 16:40:24 +02001#include "mbedtls/ssl.h"
2#include "mbedtls/entropy.h"
3#include "mbedtls/ctr_drbg.h"
4#include "mbedtls/certs.h"
5#include "mbedtls/ssl_ticket.h"
Philippe Antoine08633822019-06-04 14:03:06 +02006#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02007#include <string.h>
8#include <stdlib.h>
9#include <stdbool.h>
10#include <stdint.h>
11
12
Philippe Antoineadc23e62019-06-25 21:53:12 +020013#ifdef MBEDTLS_SSL_SRV_C
Philippe Antoine72333522018-05-03 16:40:24 +020014const char *pers = "fuzz_server";
15static bool initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020016#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020017static mbedtls_x509_crt srvcert;
18static mbedtls_pk_context pkey;
19#endif
20const char *alpn_list[3];
21
22#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
23const unsigned char psk[] = {
24 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
25 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
26};
27const char psk_id[] = "Client_identity";
28#endif
Philippe Antoineadc23e62019-06-25 21:53:12 +020029#endif // MBEDTLS_SSL_SRV_C
Philippe Antoine72333522018-05-03 16:40:24 +020030
31
Philippe Antoine72333522018-05-03 16:40:24 +020032int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Philippe Antoinec32fd242019-06-06 09:12:53 +020033#ifdef MBEDTLS_SSL_SRV_C
Philippe Antoine72333522018-05-03 16:40:24 +020034 int ret;
35 size_t len;
36 mbedtls_ssl_context ssl;
37 mbedtls_ssl_config conf;
38 mbedtls_ctr_drbg_context ctr_drbg;
39 mbedtls_entropy_context entropy;
40#if defined(MBEDTLS_SSL_SESSION_TICKETS)
41 mbedtls_ssl_ticket_context ticket_ctx;
42#endif
43 unsigned char buf[4096];
44 fuzzBufferOffset_t biomemfuzz;
45 uint8_t options;
46
47 //we take 1 byte as options input
48 if (Size < 1) {
49 return 0;
50 }
51 options = Data[Size - 1];
52
53 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020054#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020055 mbedtls_x509_crt_init( &srvcert );
56 mbedtls_pk_init( &pkey );
57 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
58 mbedtls_test_srv_crt_len ) != 0)
59 return 1;
60 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
61 mbedtls_test_cas_pem_len ) != 0)
62 return 1;
63 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
64 mbedtls_test_srv_key_len, NULL, 0 ) != 0)
65 return 1;
66#endif
67
68 alpn_list[0] = "HTTP";
69 alpn_list[1] = "fuzzalpn";
70 alpn_list[2] = NULL;
71
Philippe Antoine08633822019-06-04 14:03:06 +020072 dummy_init();
73
Philippe Antoine72333522018-05-03 16:40:24 +020074 initialized = 1;
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#if defined(MBEDTLS_SSL_SESSION_TICKETS)
81 mbedtls_ssl_ticket_init( &ticket_ctx );
82#endif
83
84 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
85 (const unsigned char *) pers, strlen( pers ) ) != 0 )
86 goto exit;
87
88
89 if( mbedtls_ssl_config_defaults( &conf,
90 MBEDTLS_SSL_IS_SERVER,
91 MBEDTLS_SSL_TRANSPORT_STREAM,
92 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
93 goto exit;
94
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020095 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020096 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
97
Philippe Antoinedaab28a2019-06-28 12:31:23 +020098#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020099 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
100 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
101 goto exit;
102#endif
103
104 mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED );
105#if defined(MBEDTLS_SSL_ALPN)
106 if (options & 0x2) {
107 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
108 }
109#endif
110#if defined(MBEDTLS_SSL_SESSION_TICKETS)
111 if( options & 0x4 )
112 {
113 if( mbedtls_ssl_ticket_setup( &ticket_ctx,
114 dummy_random, &ctr_drbg,
115 MBEDTLS_CIPHER_AES_256_GCM,
116 86400 ) != 0 )
117 goto exit;
118
119 mbedtls_ssl_conf_session_tickets_cb( &conf,
120 mbedtls_ssl_ticket_write,
121 mbedtls_ssl_ticket_parse,
122 &ticket_ctx );
123 }
124#endif
125#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
126 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 0x8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
127#endif
128#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
129 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
130#endif
131#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
132 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
133#endif
134#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
135 if (options & 0x40) {
136 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
137 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
138 }
139#endif
140#if defined(MBEDTLS_SSL_RENEGOTIATION)
141 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
142#endif
143
144 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
145 goto exit;
146
147 biomemfuzz.Data = Data;
148 biomemfuzz.Size = Size-1;
149 biomemfuzz.Offset = 0;
150 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
151
152 mbedtls_ssl_session_reset( &ssl );
153 ret = mbedtls_ssl_handshake( &ssl );
154 if( ret == 0 )
155 {
156 //keep reading data from server until the end
157 do
158 {
159 len = sizeof( buf ) - 1;
160 ret = mbedtls_ssl_read( &ssl, buf, len );
161
162 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
163 continue;
164 else if( ret <= 0 )
165 //EOF or error
166 break;
167 }
168 while( 1 );
169 }
170
171exit:
172#if defined(MBEDTLS_SSL_SESSION_TICKETS)
173 mbedtls_ssl_ticket_free( &ticket_ctx );
174#endif
175 mbedtls_entropy_free( &entropy );
176 mbedtls_ctr_drbg_free( &ctr_drbg );
177 mbedtls_ssl_config_free( &conf );
178 mbedtls_ssl_free( &ssl );
179
Philippe Antoinec32fd242019-06-06 09:12:53 +0200180#else
181 (void) Data;
182 (void) Size;
183#endif //MBEDTLS_SSL_SRV_C
184
Philippe Antoine72333522018-05-03 16:40:24 +0200185 return 0;
186}