Mateusz Starzyk | 6c2e9b6 | 2021-05-19 17:54:54 +0200 | [diff] [blame] | 1 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 2 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 3 | #include "mbedtls/ssl.h" |
| 4 | #include "mbedtls/entropy.h" |
| 5 | #include "mbedtls/ctr_drbg.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 6 | #include "mbedtls/ssl_ticket.h" |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 7 | #include "test/certs.h" |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 8 | #include "common.h" |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | |
| 13 | |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 14 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 15 | defined(MBEDTLS_ENTROPY_C) && \ |
| 16 | defined(MBEDTLS_CTR_DRBG_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 17 | const char *pers = "fuzz_server"; |
Philippe Antoine | 42a2ce8 | 2019-07-10 14:26:31 +0200 | [diff] [blame] | 18 | static int initialized = 0; |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 20 | static mbedtls_x509_crt srvcert; |
| 21 | static mbedtls_pk_context pkey; |
| 22 | #endif |
| 23 | const char *alpn_list[3]; |
| 24 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 25 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 26 | const unsigned char psk[] = { |
| 27 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 28 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 29 | }; |
| 30 | const char psk_id[] = "Client_identity"; |
| 31 | #endif |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 32 | #endif // MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 33 | |
| 34 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 35 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 37 | defined(MBEDTLS_ENTROPY_C) && \ |
| 38 | defined(MBEDTLS_CTR_DRBG_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 39 | int ret; |
| 40 | size_t len; |
| 41 | mbedtls_ssl_context ssl; |
| 42 | mbedtls_ssl_config conf; |
| 43 | mbedtls_ctr_drbg_context ctr_drbg; |
| 44 | mbedtls_entropy_context entropy; |
| 45 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 46 | mbedtls_ssl_ticket_context ticket_ctx; |
| 47 | #endif |
| 48 | unsigned char buf[4096]; |
| 49 | fuzzBufferOffset_t biomemfuzz; |
| 50 | uint8_t options; |
| 51 | |
| 52 | //we take 1 byte as options input |
| 53 | if (Size < 1) { |
| 54 | return 0; |
| 55 | } |
| 56 | options = Data[Size - 1]; |
| 57 | |
| 58 | if (initialized == 0) { |
Manuel Pégourié-Gonnard | 7f93da1 | 2021-06-16 10:20:30 +0200 | [diff] [blame] | 59 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 60 | mbedtls_entropy_init( &entropy ); |
| 61 | |
| 62 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy, |
| 63 | (const unsigned char *) pers, strlen( pers ) ) != 0 ) |
| 64 | return 1; |
| 65 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 67 | mbedtls_x509_crt_init( &srvcert ); |
| 68 | mbedtls_pk_init( &pkey ); |
| 69 | if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 70 | mbedtls_test_srv_crt_len ) != 0) |
| 71 | return 1; |
| 72 | if (mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 73 | mbedtls_test_cas_pem_len ) != 0) |
| 74 | return 1; |
| 75 | if (mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 76 | mbedtls_test_srv_key_len, NULL, 0, |
Manuel Pégourié-Gonnard | 7f93da1 | 2021-06-16 10:20:30 +0200 | [diff] [blame] | 77 | dummy_random, &ctr_drbg ) != 0) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 78 | return 1; |
| 79 | #endif |
| 80 | |
| 81 | alpn_list[0] = "HTTP"; |
| 82 | alpn_list[1] = "fuzzalpn"; |
| 83 | alpn_list[2] = NULL; |
| 84 | |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 85 | dummy_init(); |
| 86 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 87 | initialized = 1; |
| 88 | } |
| 89 | mbedtls_ssl_init( &ssl ); |
| 90 | mbedtls_ssl_config_init( &conf ); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 91 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 92 | mbedtls_ssl_ticket_init( &ticket_ctx ); |
| 93 | #endif |
| 94 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 95 | if( mbedtls_ssl_config_defaults( &conf, |
| 96 | MBEDTLS_SSL_IS_SERVER, |
| 97 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 98 | MBEDTLS_SSL_PRESET_DEFAULT ) != 0 ) |
| 99 | goto exit; |
| 100 | |
Philippe Antoine | 2b7c9a2 | 2019-06-04 12:05:36 +0200 | [diff] [blame] | 101 | srand(1); |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 102 | mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg ); |
| 103 | |
Philippe Antoine | daab28a | 2019-06-28 12:31:23 +0200 | [diff] [blame] | 104 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_PEM_PARSE_C) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 105 | mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL ); |
| 106 | if( mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) != 0 ) |
| 107 | goto exit; |
| 108 | #endif |
| 109 | |
| 110 | mbedtls_ssl_conf_cert_req_ca_list( &conf, (options & 0x1) ? MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED : MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED ); |
| 111 | #if defined(MBEDTLS_SSL_ALPN) |
| 112 | if (options & 0x2) { |
| 113 | mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ); |
| 114 | } |
| 115 | #endif |
| 116 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 117 | if( options & 0x4 ) |
| 118 | { |
| 119 | if( mbedtls_ssl_ticket_setup( &ticket_ctx, |
| 120 | dummy_random, &ctr_drbg, |
| 121 | MBEDTLS_CIPHER_AES_256_GCM, |
| 122 | 86400 ) != 0 ) |
| 123 | goto exit; |
| 124 | |
| 125 | mbedtls_ssl_conf_session_tickets_cb( &conf, |
| 126 | mbedtls_ssl_ticket_write, |
| 127 | mbedtls_ssl_ticket_parse, |
| 128 | &ticket_ctx ); |
| 129 | } |
| 130 | #endif |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 131 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 132 | mbedtls_ssl_conf_extended_master_secret( &conf, (options & 0x10) ? MBEDTLS_SSL_EXTENDED_MS_DISABLED : MBEDTLS_SSL_EXTENDED_MS_ENABLED); |
| 133 | #endif |
| 134 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 135 | mbedtls_ssl_conf_encrypt_then_mac( &conf, (options & 0x20) ? MBEDTLS_SSL_ETM_ENABLED : MBEDTLS_SSL_ETM_DISABLED); |
| 136 | #endif |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 137 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 138 | if (options & 0x40) { |
| 139 | mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ), |
| 140 | (const unsigned char *) psk_id, sizeof( psk_id ) - 1 ); |
| 141 | } |
| 142 | #endif |
| 143 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 144 | mbedtls_ssl_conf_renegotiation( &conf, (options & 0x80) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : MBEDTLS_SSL_RENEGOTIATION_DISABLED ); |
| 145 | #endif |
| 146 | |
| 147 | if( mbedtls_ssl_setup( &ssl, &conf ) != 0 ) |
| 148 | goto exit; |
| 149 | |
| 150 | biomemfuzz.Data = Data; |
| 151 | biomemfuzz.Size = Size-1; |
| 152 | biomemfuzz.Offset = 0; |
| 153 | mbedtls_ssl_set_bio( &ssl, &biomemfuzz, dummy_send, fuzz_recv, NULL ); |
| 154 | |
| 155 | mbedtls_ssl_session_reset( &ssl ); |
| 156 | ret = mbedtls_ssl_handshake( &ssl ); |
| 157 | if( ret == 0 ) |
| 158 | { |
| 159 | //keep reading data from server until the end |
| 160 | do |
| 161 | { |
| 162 | len = sizeof( buf ) - 1; |
| 163 | ret = mbedtls_ssl_read( &ssl, buf, len ); |
| 164 | |
| 165 | if( ret == MBEDTLS_ERR_SSL_WANT_READ ) |
| 166 | continue; |
| 167 | else if( ret <= 0 ) |
| 168 | //EOF or error |
| 169 | break; |
| 170 | } |
| 171 | while( 1 ); |
| 172 | } |
| 173 | |
| 174 | exit: |
| 175 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 176 | mbedtls_ssl_ticket_free( &ticket_ctx ); |
| 177 | #endif |
| 178 | mbedtls_entropy_free( &entropy ); |
| 179 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 180 | mbedtls_ssl_config_free( &conf ); |
| 181 | mbedtls_ssl_free( &ssl ); |
| 182 | |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 183 | #else |
| 184 | (void) Data; |
| 185 | (void) Size; |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 186 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Philippe Antoine | c32fd24 | 2019-06-06 09:12:53 +0200 | [diff] [blame] | 187 | |
Philippe Antoine | 7233352 | 2018-05-03 16:40:24 +0200 | [diff] [blame] | 188 | return 0; |
| 189 | } |