blob: 0a2f3a90c733ff716fcfe28ecbb99adb132a2c1f [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
13const char *pers = "fuzz_server";
14static bool initialized = 0;
15#if defined(MBEDTLS_X509_CRT_PARSE_C)
16static 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
28
29
Philippe Antoine72333522018-05-03 16:40:24 +020030int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Philippe Antoinec32fd242019-06-06 09:12:53 +020031#ifdef MBEDTLS_SSL_SRV_C
Philippe Antoine72333522018-05-03 16:40:24 +020032 int ret;
33 size_t len;
34 mbedtls_ssl_context ssl;
35 mbedtls_ssl_config conf;
36 mbedtls_ctr_drbg_context ctr_drbg;
37 mbedtls_entropy_context entropy;
38#if defined(MBEDTLS_SSL_SESSION_TICKETS)
39 mbedtls_ssl_ticket_context ticket_ctx;
40#endif
41 unsigned char buf[4096];
42 fuzzBufferOffset_t biomemfuzz;
43 uint8_t options;
44
45 //we take 1 byte as options input
46 if (Size < 1) {
47 return 0;
48 }
49 options = Data[Size - 1];
50
51 if (initialized == 0) {
52#if defined(MBEDTLS_X509_CRT_PARSE_C)
53 mbedtls_x509_crt_init( &srvcert );
54 mbedtls_pk_init( &pkey );
55 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
56 mbedtls_test_srv_crt_len ) != 0)
57 return 1;
58 if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
59 mbedtls_test_cas_pem_len ) != 0)
60 return 1;
61 if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
62 mbedtls_test_srv_key_len, NULL, 0 ) != 0)
63 return 1;
64#endif
65
66 alpn_list[0] = "HTTP";
67 alpn_list[1] = "fuzzalpn";
68 alpn_list[2] = NULL;
69
Philippe Antoine08633822019-06-04 14:03:06 +020070 dummy_init();
71
Philippe Antoine72333522018-05-03 16:40:24 +020072 initialized = 1;
73 }
74 mbedtls_ssl_init( &ssl );
75 mbedtls_ssl_config_init( &conf );
76 mbedtls_ctr_drbg_init( &ctr_drbg );
77 mbedtls_entropy_init( &entropy );
78#if defined(MBEDTLS_SSL_SESSION_TICKETS)
79 mbedtls_ssl_ticket_init( &ticket_ctx );
80#endif
81
82 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
83 (const unsigned char *) pers, strlen( pers ) ) != 0 )
84 goto exit;
85
86
87 if( mbedtls_ssl_config_defaults( &conf,
88 MBEDTLS_SSL_IS_SERVER,
89 MBEDTLS_SSL_TRANSPORT_STREAM,
90 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
91 goto exit;
92
Philippe Antoine2b7c9a22019-06-04 12:05:36 +020093 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +020094 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
95
96#if defined(MBEDTLS_X509_CRT_PARSE_C)
97 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
98 if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 )
99 goto exit;
100#endif
101
102 mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED );
103#if defined(MBEDTLS_SSL_ALPN)
104 if (options & 0x2) {
105 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
106 }
107#endif
108#if defined(MBEDTLS_SSL_SESSION_TICKETS)
109 if( options & 0x4 )
110 {
111 if( mbedtls_ssl_ticket_setup( &ticket_ctx,
112 dummy_random, &ctr_drbg,
113 MBEDTLS_CIPHER_AES_256_GCM,
114 86400 ) != 0 )
115 goto exit;
116
117 mbedtls_ssl_conf_session_tickets_cb( &conf,
118 mbedtls_ssl_ticket_write,
119 mbedtls_ssl_ticket_parse,
120 &ticket_ctx );
121 }
122#endif
123#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
124 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 0x8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
125#endif
126#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
127 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
128#endif
129#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
130 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED);
131#endif
132#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
133 if (options & 0x40) {
134 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
135 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
136 }
137#endif
138#if defined(MBEDTLS_SSL_RENEGOTIATION)
139 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
140#endif
141
142 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
143 goto exit;
144
145 biomemfuzz.Data = Data;
146 biomemfuzz.Size = Size-1;
147 biomemfuzz.Offset = 0;
148 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
149
150 mbedtls_ssl_session_reset( &ssl );
151 ret = mbedtls_ssl_handshake( &ssl );
152 if( ret == 0 )
153 {
154 //keep reading data from server until the end
155 do
156 {
157 len = sizeof( buf ) - 1;
158 ret = mbedtls_ssl_read( &ssl, buf, len );
159
160 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
161 continue;
162 else if( ret <= 0 )
163 //EOF or error
164 break;
165 }
166 while( 1 );
167 }
168
169exit:
170#if defined(MBEDTLS_SSL_SESSION_TICKETS)
171 mbedtls_ssl_ticket_free( &ticket_ctx );
172#endif
173 mbedtls_entropy_free( &entropy );
174 mbedtls_ctr_drbg_free( &ctr_drbg );
175 mbedtls_ssl_config_free( &conf );
176 mbedtls_ssl_free( &ssl );
177
Philippe Antoinec32fd242019-06-06 09:12:53 +0200178#else
179 (void) Data;
180 (void) Size;
181#endif //MBEDTLS_SSL_SRV_C
182
Philippe Antoine72333522018-05-03 16:40:24 +0200183 return 0;
184}