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