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