blob: bb3d6e6566f83d7a55123b2dc975bfa0c15ce541 [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"
Philippe Antoine08633822019-06-04 14:03:06 +02005#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02006#include <string.h>
7#include <stdlib.h>
8#include <stdbool.h>
9#include <stdint.h>
10
11
12static bool initialized = 0;
13#if defined(MBEDTLS_X509_CRT_PARSE_C)
14static mbedtls_x509_crt cacert;
15#endif
16const char *alpn_list[3];
17
18
19#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
20const unsigned char psk[] = {
21 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
22 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
23};
24const char psk_id[] = "Client_identity";
25#endif
26
27const char *pers = "fuzz_client";
28
29
Philippe Antoine72333522018-05-03 16:40:24 +020030int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
31 int ret;
32 size_t len;
33 mbedtls_ssl_context ssl;
34 mbedtls_ssl_config conf;
35 mbedtls_ctr_drbg_context ctr_drbg;
36 mbedtls_entropy_context entropy;
37 unsigned char buf[4096];
38 fuzzBufferOffset_t biomemfuzz;
39 uint16_t options;
40
41 if (initialized == 0) {
42#if defined(MBEDTLS_X509_CRT_PARSE_C)
43 mbedtls_x509_crt_init( &cacert );
44 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
45 mbedtls_test_cas_pem_len ) != 0)
46 return 1;
47#endif
48
49 alpn_list[0] = "HTTP";
50 alpn_list[1] = "fuzzalpn";
51 alpn_list[2] = NULL;
52
Philippe Antoine08633822019-06-04 14:03:06 +020053 dummy_init();
54
Philippe Antoine72333522018-05-03 16:40:24 +020055 initialized = 1;
56 }
57
58 //we take 1 byte as options input
59 if (Size < 2) {
60 return 0;
61 }
62 options = (Data[Size - 2] << 8) | Data[Size - 1];
63 //Avoid warnings if compile options imply no options
64 (void) options;
65
66 mbedtls_ssl_init( &ssl );
67 mbedtls_ssl_config_init( &conf );
68 mbedtls_ctr_drbg_init( &ctr_drbg );
69 mbedtls_entropy_init( &entropy );
70
71 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
72 (const unsigned char *) pers, strlen( pers ) ) != 0 )
73 goto exit;
74
75 if( mbedtls_ssl_config_defaults( &conf,
76 MBEDTLS_SSL_IS_CLIENT,
77 MBEDTLS_SSL_TRANSPORT_STREAM,
78 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
79 goto exit;
80
81#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
82 if (options & 2) {
83 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
84 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
85 }
86#endif
87
88#if defined(MBEDTLS_X509_CRT_PARSE_C)
89 if (options & 4) {
90 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
91 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
92 } else
93#endif
94 {
95 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
96 }
97#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
98 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
99#endif
100#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
101 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
102#endif
103#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
104 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
105#endif
106#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
107 mbedtls_ssl_conf_cbc_record_splitting( &conf, (options & 0x40) ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
108#endif
109#if defined(MBEDTLS_SSL_RENEGOTIATION)
110 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
111#endif
112#if defined(MBEDTLS_SSL_SESSION_TICKETS)
113 mbedtls_ssl_conf_session_tickets( &conf, (options & 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED );
114#endif
115#if defined(MBEDTLS_SSL_ALPN)
116 if (options & 0x200) {
117 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
118 }
119#endif
120 //There may be other options to add :
121 // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
122
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200123 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +0200124 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
125
126 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
127 goto exit;
128
129#if defined(MBEDTLS_X509_CRT_PARSE_C)
130 if ((options & 1) == 0) {
131 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
132 goto exit;
133 }
134#endif
135
136 biomemfuzz.Data = Data;
137 biomemfuzz.Size = Size-2;
138 biomemfuzz.Offset = 0;
139 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
140
141 ret = mbedtls_ssl_handshake( &ssl );
142 if( ret == 0 )
143 {
144 //keep reading data from server until the end
145 do
146 {
147 len = sizeof( buf ) - 1;
148 ret = mbedtls_ssl_read( &ssl, buf, len );
149
150 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
151 continue;
152 else if( ret <= 0 )
153 //EOF or error
154 break;
155 }
156 while( 1 );
157 }
158
159exit:
160 mbedtls_entropy_free( &entropy );
161 mbedtls_ctr_drbg_free( &ctr_drbg );
162 mbedtls_ssl_config_free( &conf );
163 mbedtls_ssl_free( &ssl );
164
165 return 0;
166}