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