| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdint.h> |
| 17 |
|
| 18 |
#include "api/s2n.h" |
| 19 |
#include "s2n_test.h" |
| 20 |
#include "testlib/s2n_testlib.h" |
| 21 |
#include "tls/s2n_connection.h" |
| 22 |
|
| 23 |
#define SUPPORTED_CERTIFICATE_FORMATS (2) |
| 24 |
|
| 25 |
static const char *certificate_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_RSA_2048_PKCS8_CERT_CHAIN }; |
| 26 |
static const char *private_key_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_KEY, S2N_RSA_2048_PKCS8_KEY }; |
| 27 |
|
| 28 |
#define HISTOGRAM_SIZE 100 |
| 29 |
static uint32_t histogram_values[HISTOGRAM_SIZE] = { 0 }; |
| 30 |
static uint32_t histogram_counts[HISTOGRAM_SIZE] = { 0 }; |
| 31 |
|
| 32 |
static int custom_mem_init(void) |
| 33 |
{ |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
static int custom_mem_cleanup(void) |
| 38 |
{ |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
static int custom_mem_malloc(void **ptr, uint32_t requested, uint32_t *allocated) |
| 43 |
{ |
| 44 |
int i = 0; |
| 45 |
for (i = 0; i < HISTOGRAM_SIZE; i++) { |
| 46 |
if (histogram_values[i] == 0) { |
| 47 |
histogram_values[i] = requested; |
| 48 |
} |
| 49 |
|
| 50 |
if (histogram_values[i] == requested) { |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (i < HISTOGRAM_SIZE) { |
| 56 |
histogram_counts[i] += 1; |
| 57 |
} |
| 58 |
|
| 59 |
*ptr = malloc(requested); |
| 60 |
*allocated = requested; |
| 61 |
|
| 62 |
|
| 63 |
memset(*ptr, 'a', requested); |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static int custom_mem_free(void *ptr, uint32_t size) |
| 69 |
{ |
| 70 |
free(ptr); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static uint64_t s2n_test_mock_time = 0; |
| 75 |
|
| 76 |
static int s2n_test_mock_clock(void *in, uint64_t *out) |
| 77 |
{ |
| 78 |
*out = s2n_test_mock_time; |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int main(int argc, char **argv) |
| 83 |
{ |
| 84 |
s2n_blocked_status blocked; |
| 85 |
char *cert_chain_pem = NULL; |
| 86 |
char *private_key_pem = NULL; |
| 87 |
char *dhparams_pem = NULL; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
int rc = s2n_mem_set_callbacks(custom_mem_init, custom_mem_cleanup, custom_mem_malloc, custom_mem_free); |
| 93 |
|
| 94 |
BEGIN_TEST(); |
| 95 |
EXPECT_SUCCESS(s2n_disable_tls13_in_test()); |
| 96 |
|
| 97 |
|
| 98 |
EXPECT_FAILURE(s2n_mem_set_callbacks(custom_mem_init, custom_mem_cleanup, custom_mem_malloc, custom_mem_free)); |
| 99 |
|
| 100 |
EXPECT_SUCCESS(rc); |
| 101 |
|
| 102 |
for (size_t is_dh_key_exchange = 0; is_dh_key_exchange <= 1; is_dh_key_exchange++) { |
| 103 |
struct s2n_cert_chain_and_key *chain_and_keys[SUPPORTED_CERTIFICATE_FORMATS]; |
| 104 |
|
| 105 |
EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 106 |
EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 107 |
EXPECT_NOT_NULL(dhparams_pem = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 108 |
|
| 109 |
|
| 110 |
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free); |
| 111 |
EXPECT_NOT_NULL(server_config); |
| 112 |
EXPECT_OK(s2n_config_set_tls12_security_policy(server_config)); |
| 113 |
EXPECT_SUCCESS(s2n_config_set_monotonic_clock(server_config, s2n_test_mock_clock, NULL)); |
| 114 |
|
| 115 |
for (size_t cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) { |
| 116 |
EXPECT_SUCCESS(s2n_read_test_pem(certificate_paths[cert], cert_chain_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 117 |
EXPECT_SUCCESS(s2n_read_test_pem(private_key_paths[cert], private_key_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 118 |
EXPECT_NOT_NULL(chain_and_keys[cert] = s2n_cert_chain_and_key_new()); |
| 119 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_keys[cert], cert_chain_pem, private_key_pem)); |
| 120 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_keys[cert])); |
| 121 |
} |
| 122 |
|
| 123 |
if (is_dh_key_exchange) { |
| 124 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 125 |
EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem)); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free); |
| 130 |
EXPECT_NOT_NULL(client_config); |
| 131 |
EXPECT_OK(s2n_config_set_tls12_security_policy(client_config)); |
| 132 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config)); |
| 133 |
EXPECT_SUCCESS(s2n_config_set_monotonic_clock(client_config, s2n_test_mock_clock, NULL)); |
| 134 |
|
| 135 |
|
| 136 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 137 |
EXPECT_NOT_NULL(server_conn); |
| 138 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config)); |
| 139 |
|
| 140 |
|
| 141 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 142 |
EXPECT_NOT_NULL(client_conn); |
| 143 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config)); |
| 144 |
|
| 145 |
|
| 146 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 147 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 148 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, &io_pair)); |
| 149 |
|
| 150 |
|
| 151 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn)); |
| 152 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12); |
| 153 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12); |
| 154 |
|
| 155 |
|
| 156 |
EXPECT_SUCCESS(s2n_connection_free_handshake(client_conn)); |
| 157 |
|
| 158 |
|
| 159 |
char send_buffer[0xffff]; |
| 160 |
char recv_buffer[0xffff]; |
| 161 |
|
| 162 |
uint16_t timeout = 1; |
| 163 |
EXPECT_SUCCESS(s2n_connection_set_dynamic_record_threshold(client_conn, 0x7fff, timeout)); |
| 164 |
|
| 165 |
int i = 0; |
| 166 |
for (i = 1; i < 0xffff - 100; i += 100) { |
| 167 |
for (int j = 0; j < i; j++) { |
| 168 |
send_buffer[j] = 33; |
| 169 |
} |
| 170 |
EXPECT_EQUAL(s2n_send(client_conn, send_buffer, i, &blocked), i); |
| 171 |
|
| 172 |
|
| 173 |
char *ptr = recv_buffer; |
| 174 |
int size = i; |
| 175 |
do { |
| 176 |
int bytes_read = 0; |
| 177 |
EXPECT_SUCCESS(bytes_read = s2n_recv(server_conn, ptr, size, &blocked)); |
| 178 |
size -= bytes_read; |
| 179 |
ptr += bytes_read; |
| 180 |
} while (size); |
| 181 |
|
| 182 |
for (int j = 0; j < i; j++) { |
| 183 |
EXPECT_EQUAL(recv_buffer[j], 33); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
EXPECT_SUCCESS(s2n_connection_release_buffers(server_conn)); |
| 188 |
|
| 189 |
|
| 190 |
EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), 0); |
| 191 |
EXPECT_SUCCESS(s2n_stuffer_rewrite(&io_pair.server_in)); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
for (int j = 0; j < i; j++) { |
| 196 |
send_buffer[j] = 33; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
EXPECT_SUCCESS(s2n_connection_release_buffers(client_conn)); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
s2n_test_mock_time += (uint64_t) timeout * 1000000000 + 1; |
| 208 |
|
| 209 |
ssize_t bytes_written = s2n_send(client_conn, send_buffer, i, &blocked); |
| 210 |
EXPECT_TRUE(bytes_written > 0); |
| 211 |
EXPECT_EQUAL((uint64_t) bytes_written, client_conn->active_application_bytes_consumed); |
| 212 |
|
| 213 |
|
| 214 |
{ |
| 215 |
char *ptr = recv_buffer; |
| 216 |
int size = i; |
| 217 |
do { |
| 218 |
int bytes_read = 0; |
| 219 |
EXPECT_SUCCESS(bytes_read = s2n_recv(server_conn, ptr, size, &blocked)); |
| 220 |
size -= bytes_read; |
| 221 |
ptr += bytes_read; |
| 222 |
} while (size); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn)); |
| 227 |
|
| 228 |
|
| 229 |
for (size_t cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) { |
| 230 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_keys[cert])); |
| 231 |
} |
| 232 |
|
| 233 |
free(cert_chain_pem); |
| 234 |
free(private_key_pem); |
| 235 |
free(dhparams_pem); |
| 236 |
} |
| 237 |
|
| 238 |
#if defined(S2N_TEST_DEBUG) |
| 239 |
|
| 240 |
uint32_t spare_value, spare_count; |
| 241 |
for (int i = 0; i < HISTOGRAM_SIZE; i++) { |
| 242 |
if (histogram_counts[i] == 0) { |
| 243 |
break; |
| 244 |
} |
| 245 |
|
| 246 |
for (int j = i + 1; j < HISTOGRAM_SIZE; j++) { |
| 247 |
if (histogram_counts[j] == 0) { |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
if (histogram_values[j] < histogram_values[i]) { |
| 252 |
spare_value = histogram_values[i]; |
| 253 |
spare_count = histogram_counts[i]; |
| 254 |
|
| 255 |
histogram_values[i] = histogram_values[j]; |
| 256 |
histogram_counts[i] = histogram_counts[j]; |
| 257 |
|
| 258 |
histogram_values[j] = spare_value; |
| 259 |
histogram_counts[j] = spare_count; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
TEST_DEBUG_PRINT("\n\n"); |
| 266 |
for (int i = 0; i < HISTOGRAM_SIZE; i++) { |
| 267 |
if (histogram_values[i] == 0) { |
| 268 |
break; |
| 269 |
} |
| 270 |
TEST_DEBUG_PRINT("Allocated %d bytes %d times\n", histogram_values[i], histogram_counts[i]); |
| 271 |
} |
| 272 |
TEST_DEBUG_PRINT("\n"); |
| 273 |
#endif |
| 274 |
|
| 275 |
END_TEST(); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |