blob: ca7a8ab7ccc23adb308298a0357ab102315056ea [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>
Philippe Antoine72333522018-05-03 16:40:24 +02008#include <stdint.h>
9
10
Philippe Antoineadc23e62019-06-25 21:53:12 +020011#ifdef MBEDTLS_SSL_CLI_C
Philippe Antoine42a2ce82019-07-10 14:26:31 +020012static int initialized = 0;
Philippe Antoinedaab28a2019-06-28 12:31:23 +020013#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020014static 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";
Philippe Antoineadc23e62019-06-25 21:53:12 +020028#endif //MBEDTLS_SSL_CLI_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_CLI_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 unsigned char buf[4096];
40 fuzzBufferOffset_t biomemfuzz;
41 uint16_t options;
42
43 if (initialized == 0) {
Philippe Antoinedaab28a2019-06-28 12:31:23 +020044#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020045 mbedtls_x509_crt_init( &cacert );
46 if (mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
47 mbedtls_test_cas_pem_len ) != 0)
48 return 1;
49#endif
50
51 alpn_list[0] = "HTTP";
52 alpn_list[1] = "fuzzalpn";
53 alpn_list[2] = NULL;
54
Philippe Antoine08633822019-06-04 14:03:06 +020055 dummy_init();
56
Philippe Antoine72333522018-05-03 16:40:24 +020057 initialized = 1;
58 }
59
60 //we take 1 byte as options input
61 if (Size < 2) {
62 return 0;
63 }
64 options = (Data[Size - 2] << 8) | Data[Size - 1];
65 //Avoid warnings if compile options imply no options
66 (void) options;
67
68 mbedtls_ssl_init( &ssl );
69 mbedtls_ssl_config_init( &conf );
70 mbedtls_ctr_drbg_init( &ctr_drbg );
71 mbedtls_entropy_init( &entropy );
72
73 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
74 (const unsigned char *) pers, strlen( pers ) ) != 0 )
75 goto exit;
76
77 if( mbedtls_ssl_config_defaults( &conf,
78 MBEDTLS_SSL_IS_CLIENT,
79 MBEDTLS_SSL_TRANSPORT_STREAM,
80 MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
81 goto exit;
82
83#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
84 if (options & 2) {
85 mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
86 (const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
87 }
88#endif
89
Philippe Antoinedaab28a2019-06-28 12:31:23 +020090#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +020091 if (options & 4) {
92 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
93 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
94 } else
95#endif
96 {
97 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
98 }
99#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
100 mbedtls_ssl_conf_truncated_hmac( &conf, (options & 8) ? MBEDTLS_SSL_TRUNC_HMAC_ENABLED : MBEDTLS_SSL_TRUNC_HMAC_DISABLED);
101#endif
102#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
103 mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED);
104#endif
105#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
106 mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_DISABLED : MBEDTLS_SSL_ETM_ENABLED);
107#endif
108#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
109 mbedtls_ssl_conf_cbc_record_splitting( &conf, (options & 0x40) ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
110#endif
111#if defined(MBEDTLS_SSL_RENEGOTIATION)
112 mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED );
113#endif
114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
115 mbedtls_ssl_conf_session_tickets( &conf, (options & 0x100) ? MBEDTLS_SSL_SESSION_TICKETS_DISABLED : MBEDTLS_SSL_SESSION_TICKETS_ENABLED );
116#endif
117#if defined(MBEDTLS_SSL_ALPN)
118 if (options & 0x200) {
119 mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list );
120 }
121#endif
122 //There may be other options to add :
123 // mbedtls_ssl_conf_cert_profile, mbedtls_ssl_conf_sig_hashes
124
Philippe Antoine2b7c9a22019-06-04 12:05:36 +0200125 srand(1);
Philippe Antoine72333522018-05-03 16:40:24 +0200126 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
127
128 if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
129 goto exit;
130
Philippe Antoinedaab28a2019-06-28 12:31:23 +0200131#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C)
Philippe Antoine72333522018-05-03 16:40:24 +0200132 if ((options & 1) == 0) {
133 if( mbedtls_ssl_set_hostname( &ssl, "localhost" ) != 0 )
134 goto exit;
135 }
136#endif
137
138 biomemfuzz.Data = Data;
139 biomemfuzz.Size = Size-2;
140 biomemfuzz.Offset = 0;
141 mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL );
142
143 ret = mbedtls_ssl_handshake( &ssl );
144 if( ret == 0 )
145 {
146 //keep reading data from server until the end
147 do
148 {
149 len = sizeof( buf ) - 1;
150 ret = mbedtls_ssl_read( &ssl, buf, len );
151
152 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
153 continue;
154 else if( ret <= 0 )
155 //EOF or error
156 break;
157 }
158 while( 1 );
159 }
160
161exit:
162 mbedtls_entropy_free( &entropy );
163 mbedtls_ctr_drbg_free( &ctr_drbg );
164 mbedtls_ssl_config_free( &conf );
165 mbedtls_ssl_free( &ssl );
166
Philippe Antoinec32fd242019-06-06 09:12:53 +0200167#else
168 (void) Data;
169 (void) Size;
170#endif //MBEDTLS_SSL_CLI_C
171
Philippe Antoine72333522018-05-03 16:40:24 +0200172 return 0;
173}