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