| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_connection.h" |
| 17 |
|
| 18 |
#ifndef _WIN32 |
| 19 |
#include "api/unstable/ktls.h" |
| 20 |
#endif |
| 21 |
#include "crypto/s2n_hash.h" |
| 22 |
#include "s2n_test.h" |
| 23 |
#include "testlib/s2n_testlib.h" |
| 24 |
#include "tls/extensions/s2n_client_server_name.h" |
| 25 |
#include "tls/extensions/s2n_extension_list.h" |
| 26 |
#include "tls/s2n_internal.h" |
| 27 |
#include "tls/s2n_tls.h" |
| 28 |
|
| 29 |
const uint8_t actual_version = 1, client_version = 2, server_version = 3; |
| 30 |
static int s2n_set_test_protocol_versions(struct s2n_connection *conn) |
| 31 |
{ |
| 32 |
conn->actual_protocol_version = actual_version; |
| 33 |
conn->client_protocol_version = client_version; |
| 34 |
conn->server_protocol_version = server_version; |
| 35 |
return S2N_SUCCESS; |
| 36 |
} |
| 37 |
|
| 38 |
bool s2n_server_name_test_callback_flag = false; |
| 39 |
static int s2n_server_name_test_callback(struct s2n_connection *conn, void *ctx) |
| 40 |
{ |
| 41 |
const char *expected_server_name = *(const char **) ctx; |
| 42 |
|
| 43 |
const char *actual_server_name = NULL; |
| 44 |
EXPECT_NOT_NULL(actual_server_name = s2n_get_server_name(conn)); |
| 45 |
EXPECT_STRING_EQUAL(actual_server_name, expected_server_name); |
| 46 |
|
| 47 |
s2n_server_name_test_callback_flag = true; |
| 48 |
return S2N_SUCCESS; |
| 49 |
} |
| 50 |
|
| 51 |
S2N_RESULT s2n_test_signature_scheme_valid(s2n_tls_signature_algorithm expected_sig_alg, |
| 52 |
s2n_tls_signature_algorithm server_sig_alg, s2n_tls_signature_algorithm client_sig_alg, |
| 53 |
s2n_tls_hash_algorithm server_hash_alg, s2n_tls_hash_algorithm client_hash_alg) |
| 54 |
{ |
| 55 |
|
| 56 |
RESULT_ENSURE_EQ(server_sig_alg, client_sig_alg); |
| 57 |
RESULT_ENSURE_EQ(server_hash_alg, client_hash_alg); |
| 58 |
|
| 59 |
|
| 60 |
RESULT_ENSURE_EQ(server_sig_alg, expected_sig_alg); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
RESULT_ENSURE_NE(server_hash_alg, S2N_TLS_HASH_NONE); |
| 66 |
RESULT_ENSURE_NE(server_hash_alg, S2N_TLS_HASH_MD5); |
| 67 |
RESULT_ENSURE_NE(server_hash_alg, S2N_TLS_HASH_SHA1); |
| 68 |
RESULT_ENSURE_NE(server_hash_alg, S2N_TLS_HASH_MD5_SHA1); |
| 69 |
|
| 70 |
return S2N_RESULT_OK; |
| 71 |
} |
| 72 |
|
| 73 |
S2N_RESULT s2n_test_all_signature_schemes_valid(s2n_tls_signature_algorithm expected_sig_alg, |
| 74 |
struct s2n_connection *server_conn, struct s2n_connection *client_conn) |
| 75 |
{ |
| 76 |
s2n_tls_signature_algorithm server_sig_alg = 0, client_sig_alg = 0; |
| 77 |
s2n_tls_hash_algorithm server_hash_alg = 0, client_hash_alg = 0; |
| 78 |
|
| 79 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_signature_algorithm(client_conn, &client_sig_alg)); |
| 80 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_signature_algorithm(server_conn, &server_sig_alg)); |
| 81 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_digest_algorithm(client_conn, &client_hash_alg)); |
| 82 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_digest_algorithm(server_conn, &server_hash_alg)); |
| 83 |
RESULT_GUARD(s2n_test_signature_scheme_valid(expected_sig_alg, |
| 84 |
server_sig_alg, client_sig_alg, server_hash_alg, client_hash_alg)); |
| 85 |
|
| 86 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_client_cert_signature_algorithm(client_conn, &client_sig_alg)); |
| 87 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_client_cert_signature_algorithm(server_conn, &server_sig_alg)); |
| 88 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_client_cert_digest_algorithm(client_conn, &client_hash_alg)); |
| 89 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_client_cert_digest_algorithm(server_conn, &server_hash_alg)); |
| 90 |
RESULT_GUARD(s2n_test_signature_scheme_valid(expected_sig_alg, |
| 91 |
server_sig_alg, client_sig_alg, server_hash_alg, client_hash_alg)); |
| 92 |
|
| 93 |
return S2N_RESULT_OK; |
| 94 |
} |
| 95 |
|
| 96 |
int s2n_noop_recv_cb(void *io_context, uint8_t *buf, uint32_t len) |
| 97 |
{ |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
int s2n_noop_send_cb(void *io_context, const uint8_t *buf, uint32_t len) |
| 102 |
{ |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
int main(int argc, char **argv) |
| 107 |
{ |
| 108 |
BEGIN_TEST(); |
| 109 |
|
| 110 |
struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL; |
| 111 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key, |
| 112 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 113 |
|
| 114 |
struct s2n_cert_chain_and_key *rsa_chain_and_key = NULL; |
| 115 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&rsa_chain_and_key, |
| 116 |
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 117 |
|
| 118 |
|
| 119 |
{ |
| 120 |
const char *test_server_name = "A server name"; |
| 121 |
|
| 122 |
|
| 123 |
EXPECT_NULL(s2n_get_server_name(NULL)); |
| 124 |
|
| 125 |
|
| 126 |
{ |
| 127 |
struct s2n_connection *conn = NULL; |
| 128 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 129 |
|
| 130 |
EXPECT_NULL(s2n_get_server_name(conn)); |
| 131 |
|
| 132 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
{ |
| 137 |
struct s2n_connection *conn = NULL; |
| 138 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 139 |
EXPECT_SUCCESS(s2n_set_server_name(conn, test_server_name)); |
| 140 |
|
| 141 |
const char *actual_server_name = NULL; |
| 142 |
EXPECT_NOT_NULL(actual_server_name = s2n_get_server_name(conn)); |
| 143 |
EXPECT_STRING_EQUAL(actual_server_name, test_server_name); |
| 144 |
|
| 145 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
{ |
| 150 |
struct s2n_connection *client_conn = NULL, *server_conn = NULL; |
| 151 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 152 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 153 |
|
| 154 |
DEFER_CLEANUP(struct s2n_stuffer stuffer, s2n_stuffer_free); |
| 155 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0)); |
| 156 |
EXPECT_SUCCESS(s2n_set_server_name(client_conn, test_server_name)); |
| 157 |
EXPECT_SUCCESS(s2n_client_server_name_extension.send(client_conn, &stuffer)); |
| 158 |
|
| 159 |
s2n_extension_type_id extension_id = 0; |
| 160 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SERVER_NAME, &extension_id)); |
| 161 |
server_conn->client_hello.extensions.parsed_extensions[extension_id].extension_type = TLS_EXTENSION_SERVER_NAME; |
| 162 |
server_conn->client_hello.extensions.parsed_extensions[extension_id].extension = stuffer.blob; |
| 163 |
|
| 164 |
const char *actual_server_name = NULL; |
| 165 |
EXPECT_NOT_NULL(actual_server_name = s2n_get_server_name(server_conn)); |
| 166 |
EXPECT_STRING_EQUAL(actual_server_name, test_server_name); |
| 167 |
|
| 168 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 169 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
{ |
| 175 |
s2n_server_name_test_callback_flag = false; |
| 176 |
|
| 177 |
struct s2n_config *config = NULL; |
| 178 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 179 |
EXPECT_OK(s2n_config_set_tls12_security_policy(config)); |
| 180 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_server_name_test_callback, &test_server_name)); |
| 181 |
|
| 182 |
struct s2n_connection *client_conn = NULL; |
| 183 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 184 |
EXPECT_OK(s2n_connection_set_tls12_security_policy(client_conn)); |
| 185 |
EXPECT_SUCCESS(s2n_set_server_name(client_conn, test_server_name)); |
| 186 |
|
| 187 |
struct s2n_connection *server_conn = NULL; |
| 188 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 189 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 190 |
|
| 191 |
EXPECT_SUCCESS(s2n_client_hello_send(client_conn)); |
| 192 |
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io, |
| 193 |
s2n_stuffer_data_available(&client_conn->handshake.io))); |
| 194 |
|
| 195 |
|
| 196 |
s2n_client_hello_recv(server_conn); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
EXPECT_TRUE(s2n_server_name_test_callback_flag); |
| 201 |
|
| 202 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 203 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 204 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 205 |
}; |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
{ |
| 210 |
struct s2n_connection *client_conn = NULL, *server_conn = NULL; |
| 211 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 212 |
EXPECT_SUCCESS(s2n_set_test_protocol_versions(client_conn)); |
| 213 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 214 |
EXPECT_SUCCESS(s2n_set_test_protocol_versions(server_conn)); |
| 215 |
|
| 216 |
|
| 217 |
EXPECT_EQUAL(s2n_connection_get_protocol_version(NULL), S2N_UNKNOWN_PROTOCOL_VERSION); |
| 218 |
|
| 219 |
|
| 220 |
EXPECT_EQUAL(s2n_connection_get_protocol_version(client_conn), actual_version); |
| 221 |
EXPECT_EQUAL(s2n_connection_get_protocol_version(server_conn), actual_version); |
| 222 |
|
| 223 |
|
| 224 |
client_conn->actual_protocol_version = S2N_UNKNOWN_PROTOCOL_VERSION; |
| 225 |
EXPECT_EQUAL(s2n_connection_get_protocol_version(client_conn), client_version); |
| 226 |
server_conn->actual_protocol_version = S2N_UNKNOWN_PROTOCOL_VERSION; |
| 227 |
EXPECT_EQUAL(s2n_connection_get_protocol_version(server_conn), server_version); |
| 228 |
|
| 229 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 230 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
{ |
| 235 |
struct s2n_connection *conn = NULL; |
| 236 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 237 |
|
| 238 |
s2n_tls_hash_algorithm output = { 0 }; |
| 239 |
|
| 240 |
EXPECT_FAILURE(s2n_connection_get_selected_digest_algorithm(NULL, &output)); |
| 241 |
EXPECT_FAILURE(s2n_connection_get_selected_digest_algorithm(conn, NULL)); |
| 242 |
EXPECT_FAILURE(s2n_connection_get_selected_client_cert_digest_algorithm(NULL, &output)); |
| 243 |
EXPECT_FAILURE(s2n_connection_get_selected_client_cert_digest_algorithm(conn, NULL)); |
| 244 |
|
| 245 |
EXPECT_SUCCESS(s2n_connection_get_selected_client_cert_digest_algorithm(conn, &output)); |
| 246 |
EXPECT_EQUAL(S2N_TLS_HASH_NONE, output); |
| 247 |
|
| 248 |
EXPECT_SUCCESS(s2n_connection_get_selected_digest_algorithm(conn, &output)); |
| 249 |
EXPECT_EQUAL(S2N_TLS_HASH_NONE, output); |
| 250 |
|
| 251 |
s2n_tls_hash_algorithm expected_output[S2N_HASH_ALGS_COUNT] = { |
| 252 |
[S2N_HASH_MD5] = S2N_TLS_HASH_MD5, |
| 253 |
[S2N_HASH_SHA1] = S2N_TLS_HASH_SHA1, |
| 254 |
[S2N_HASH_SHA224] = S2N_TLS_HASH_SHA224, |
| 255 |
[S2N_HASH_SHA256] = S2N_TLS_HASH_SHA256, |
| 256 |
[S2N_HASH_SHA384] = S2N_TLS_HASH_SHA384, |
| 257 |
[S2N_HASH_SHA512] = S2N_TLS_HASH_SHA512, |
| 258 |
[S2N_HASH_MD5_SHA1] = S2N_TLS_HASH_MD5_SHA1, |
| 259 |
}; |
| 260 |
|
| 261 |
for (size_t i = S2N_TLS_HASH_NONE; i <= UINT16_MAX; i++) { |
| 262 |
struct s2n_signature_scheme test_scheme = *conn->handshake_params.client_cert_sig_scheme; |
| 263 |
test_scheme.hash_alg = i; |
| 264 |
conn->handshake_params.client_cert_sig_scheme = &test_scheme; |
| 265 |
conn->handshake_params.server_cert_sig_scheme = &test_scheme; |
| 266 |
if (i < S2N_HASH_ALGS_COUNT) { |
| 267 |
EXPECT_SUCCESS(s2n_connection_get_selected_client_cert_digest_algorithm(conn, &output)); |
| 268 |
EXPECT_EQUAL(expected_output[i], output); |
| 269 |
|
| 270 |
EXPECT_SUCCESS(s2n_connection_get_selected_digest_algorithm(conn, &output)); |
| 271 |
EXPECT_EQUAL(expected_output[i], output); |
| 272 |
} else { |
| 273 |
EXPECT_SUCCESS(s2n_connection_get_selected_client_cert_digest_algorithm(conn, &output)); |
| 274 |
EXPECT_EQUAL(S2N_TLS_HASH_NONE, output); |
| 275 |
|
| 276 |
EXPECT_SUCCESS(s2n_connection_get_selected_digest_algorithm(conn, &output)); |
| 277 |
EXPECT_EQUAL(S2N_TLS_HASH_NONE, output); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
{ |
| 286 |
struct s2n_connection *conn = NULL; |
| 287 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 288 |
|
| 289 |
s2n_tls_signature_algorithm output = { 0 }; |
| 290 |
|
| 291 |
EXPECT_FAILURE(s2n_connection_get_selected_signature_algorithm(NULL, &output)); |
| 292 |
EXPECT_FAILURE(s2n_connection_get_selected_signature_algorithm(conn, NULL)); |
| 293 |
EXPECT_FAILURE(s2n_connection_get_selected_client_cert_signature_algorithm(NULL, &output)); |
| 294 |
EXPECT_FAILURE(s2n_connection_get_selected_client_cert_signature_algorithm(conn, NULL)); |
| 295 |
|
| 296 |
EXPECT_SUCCESS(s2n_connection_get_selected_client_cert_signature_algorithm(conn, &output)); |
| 297 |
EXPECT_EQUAL(S2N_TLS_SIGNATURE_ANONYMOUS, output); |
| 298 |
|
| 299 |
EXPECT_SUCCESS(s2n_connection_get_selected_signature_algorithm(conn, &output)); |
| 300 |
EXPECT_EQUAL(S2N_TLS_SIGNATURE_ANONYMOUS, output); |
| 301 |
|
| 302 |
s2n_tls_signature_algorithm expected_output[] = { |
| 303 |
[S2N_SIGNATURE_ANONYMOUS] = S2N_TLS_SIGNATURE_ANONYMOUS, |
| 304 |
[S2N_SIGNATURE_RSA] = S2N_TLS_SIGNATURE_RSA, |
| 305 |
[S2N_SIGNATURE_ECDSA] = S2N_TLS_SIGNATURE_ECDSA, |
| 306 |
[S2N_SIGNATURE_RSA_PSS_RSAE] = S2N_TLS_SIGNATURE_RSA_PSS_RSAE, |
| 307 |
[S2N_SIGNATURE_RSA_PSS_PSS] = S2N_TLS_SIGNATURE_RSA_PSS_PSS, |
| 308 |
[S2N_SIGNATURE_MLDSA] = S2N_TLS_SIGNATURE_MLDSA, |
| 309 |
}; |
| 310 |
|
| 311 |
for (size_t i = 0; i <= UINT16_MAX; i++) { |
| 312 |
struct s2n_signature_scheme test_scheme = *conn->handshake_params.client_cert_sig_scheme; |
| 313 |
test_scheme.sig_alg = i; |
| 314 |
conn->handshake_params.client_cert_sig_scheme = &test_scheme; |
| 315 |
conn->handshake_params.server_cert_sig_scheme = &test_scheme; |
| 316 |
|
| 317 |
if (i < s2n_array_len(expected_output)) { |
| 318 |
EXPECT_SUCCESS(s2n_connection_get_selected_client_cert_signature_algorithm(conn, &output)); |
| 319 |
EXPECT_EQUAL(expected_output[i], output); |
| 320 |
|
| 321 |
EXPECT_SUCCESS(s2n_connection_get_selected_signature_algorithm(conn, &output)); |
| 322 |
EXPECT_EQUAL(expected_output[i], output); |
| 323 |
} else { |
| 324 |
EXPECT_SUCCESS(s2n_connection_get_selected_client_cert_signature_algorithm(conn, &output)); |
| 325 |
EXPECT_EQUAL(S2N_TLS_SIGNATURE_ANONYMOUS, output); |
| 326 |
|
| 327 |
EXPECT_SUCCESS(s2n_connection_get_selected_signature_algorithm(conn, &output)); |
| 328 |
EXPECT_EQUAL(S2N_TLS_SIGNATURE_ANONYMOUS, output); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
{ |
| 340 |
|
| 341 |
if (s2n_is_tls13_fully_supported()) { |
| 342 |
struct s2n_config *config = s2n_config_new(); |
| 343 |
EXPECT_NOT_NULL(config); |
| 344 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 345 |
EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED)); |
| 346 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key)); |
| 347 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 348 |
|
| 349 |
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT); |
| 350 |
EXPECT_NOT_NULL(client_conn); |
| 351 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 352 |
|
| 353 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 354 |
EXPECT_NOT_NULL(server_conn); |
| 355 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 356 |
|
| 357 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 358 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 359 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 360 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair)); |
| 361 |
|
| 362 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn)); |
| 363 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13); |
| 364 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13); |
| 365 |
EXPECT_OK(s2n_test_all_signature_schemes_valid(S2N_TLS_SIGNATURE_ECDSA, server_conn, client_conn)); |
| 366 |
|
| 367 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 368 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 369 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 370 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
{ |
| 375 |
struct s2n_config *config = s2n_config_new(); |
| 376 |
EXPECT_NOT_NULL(config); |
| 377 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 378 |
EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED)); |
| 379 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, rsa_chain_and_key)); |
| 380 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20170210")); |
| 381 |
|
| 382 |
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT); |
| 383 |
EXPECT_NOT_NULL(client_conn); |
| 384 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 385 |
|
| 386 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 387 |
EXPECT_NOT_NULL(server_conn); |
| 388 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 389 |
|
| 390 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 391 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 392 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 393 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair)); |
| 394 |
|
| 395 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn)); |
| 396 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12); |
| 397 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12); |
| 398 |
EXPECT_OK(s2n_test_all_signature_schemes_valid(S2N_TLS_SIGNATURE_RSA, server_conn, client_conn)); |
| 399 |
|
| 400 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 401 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 402 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 403 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 404 |
}; |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
{ |
| 409 |
const uint8_t mfl_code = S2N_TLS_MAX_FRAG_LEN_1024; |
| 410 |
const uint16_t mfl_code_value = 1024; |
| 411 |
const uint16_t low_mfl = 10; |
| 412 |
const uint16_t high_mfl = UINT16_MAX; |
| 413 |
|
| 414 |
|
| 415 |
EXPECT_ERROR_WITH_ERRNO(s2n_connection_set_max_fragment_length(NULL, 1), S2N_ERR_NULL); |
| 416 |
|
| 417 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 418 |
EXPECT_NOT_NULL(conn); |
| 419 |
|
| 420 |
|
| 421 |
{ |
| 422 |
conn->max_outgoing_fragment_length = 1; |
| 423 |
EXPECT_OK(s2n_connection_set_max_fragment_length(conn, high_mfl)); |
| 424 |
EXPECT_EQUAL(conn->max_outgoing_fragment_length, high_mfl); |
| 425 |
EXPECT_EQUAL(conn->out.blob.size, 0); |
| 426 |
}; |
| 427 |
|
| 428 |
|
| 429 |
{ |
| 430 |
conn->max_outgoing_fragment_length = 1; |
| 431 |
EXPECT_OK(s2n_connection_set_max_fragment_length(conn, low_mfl)); |
| 432 |
EXPECT_EQUAL(conn->max_outgoing_fragment_length, low_mfl); |
| 433 |
EXPECT_EQUAL(conn->out.blob.size, 0); |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
{ |
| 438 |
conn->negotiated_mfl_code = mfl_code; |
| 439 |
conn->max_outgoing_fragment_length = 1; |
| 440 |
EXPECT_OK(s2n_connection_set_max_fragment_length(conn, high_mfl)); |
| 441 |
EXPECT_EQUAL(conn->max_outgoing_fragment_length, mfl_code_value); |
| 442 |
EXPECT_EQUAL(conn->out.blob.size, 0); |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
{ |
| 447 |
conn->negotiated_mfl_code = mfl_code; |
| 448 |
conn->max_outgoing_fragment_length = 1; |
| 449 |
EXPECT_OK(s2n_connection_set_max_fragment_length(conn, low_mfl)); |
| 450 |
EXPECT_EQUAL(conn->max_outgoing_fragment_length, low_mfl); |
| 451 |
EXPECT_EQUAL(conn->out.blob.size, 0); |
| 452 |
}; |
| 453 |
|
| 454 |
|
| 455 |
{ |
| 456 |
conn->negotiated_mfl_code = UINT8_MAX; |
| 457 |
EXPECT_ERROR_WITH_ERRNO(s2n_connection_set_max_fragment_length(conn, low_mfl), S2N_ERR_SAFETY); |
| 458 |
conn->negotiated_mfl_code = 0; |
| 459 |
}; |
| 460 |
|
| 461 |
|
| 462 |
{ |
| 463 |
EXPECT_SUCCESS(s2n_realloc(&conn->out.blob, 1)); |
| 464 |
EXPECT_OK(s2n_connection_set_max_fragment_length(conn, S2N_TLS_MAXIMUM_FRAGMENT_LENGTH)); |
| 465 |
EXPECT_EQUAL(conn->max_outgoing_fragment_length, S2N_TLS_MAXIMUM_FRAGMENT_LENGTH); |
| 466 |
EXPECT_EQUAL(conn->out.blob.size, S2N_TLS_MAXIMUM_RECORD_LENGTH); |
| 467 |
EXPECT_SUCCESS(s2n_free(&conn->out.blob)); |
| 468 |
}; |
| 469 |
|
| 470 |
|
| 471 |
{ |
| 472 |
EXPECT_SUCCESS(s2n_realloc(&conn->out.blob, UINT16_MAX)); |
| 473 |
EXPECT_OK(s2n_connection_set_max_fragment_length(conn, low_mfl)); |
| 474 |
EXPECT_EQUAL(conn->max_outgoing_fragment_length, low_mfl); |
| 475 |
EXPECT_EQUAL(conn->out.blob.size, UINT16_MAX); |
| 476 |
EXPECT_SUCCESS(s2n_free(&conn->out.blob)); |
| 477 |
} |
| 478 |
|
| 479 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 480 |
}; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
#ifndef _WIN32 |
| 487 |
{ |
| 488 |
static const int READFD = 1; |
| 489 |
static const int WRITEFD = 2; |
| 490 |
static int getReadFd, getWriteFd; |
| 491 |
|
| 492 |
|
| 493 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_fd(NULL, READFD), S2N_ERR_NULL); |
| 494 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_read_fd(NULL, READFD), S2N_ERR_NULL); |
| 495 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_write_fd(NULL, WRITEFD), S2N_ERR_NULL); |
| 496 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_write_fd(NULL, &getWriteFd), S2N_ERR_NULL); |
| 497 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_read_fd(NULL, &getReadFd), S2N_ERR_NULL); |
| 498 |
|
| 499 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 500 |
EXPECT_NOT_NULL(conn); |
| 501 |
|
| 502 |
|
| 503 |
EXPECT_SUCCESS(s2n_connection_set_fd(conn, READFD)); |
| 504 |
EXPECT_SUCCESS(s2n_connection_get_write_fd(conn, &getWriteFd)); |
| 505 |
EXPECT_SUCCESS(s2n_connection_get_read_fd(conn, &getReadFd)); |
| 506 |
EXPECT_EQUAL(getReadFd, READFD); |
| 507 |
EXPECT_EQUAL(getWriteFd, READFD); |
| 508 |
|
| 509 |
|
| 510 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 511 |
EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, READFD)); |
| 512 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_write_fd(conn, &getWriteFd), S2N_ERR_INVALID_STATE); |
| 513 |
EXPECT_SUCCESS(s2n_connection_get_read_fd(conn, &getReadFd)); |
| 514 |
EXPECT_EQUAL(getReadFd, READFD); |
| 515 |
|
| 516 |
|
| 517 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 518 |
EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, WRITEFD)); |
| 519 |
EXPECT_SUCCESS(s2n_connection_get_write_fd(conn, &getWriteFd)); |
| 520 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_read_fd(conn, &getReadFd), S2N_ERR_INVALID_STATE); |
| 521 |
EXPECT_EQUAL(getWriteFd, WRITEFD); |
| 522 |
|
| 523 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 524 |
}; |
| 525 |
|
| 526 |
|
| 527 |
{ |
| 528 |
static const int OLDFD = 1; |
| 529 |
static const int NEWFD = 2; |
| 530 |
static int getReadFd, getWriteFd; |
| 531 |
|
| 532 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 533 |
EXPECT_NOT_NULL(conn); |
| 534 |
|
| 535 |
EXPECT_SUCCESS(s2n_connection_set_fd(conn, OLDFD)); |
| 536 |
EXPECT_SUCCESS(s2n_connection_set_fd(conn, NEWFD)); |
| 537 |
|
| 538 |
EXPECT_SUCCESS(s2n_connection_get_write_fd(conn, &getWriteFd)); |
| 539 |
EXPECT_SUCCESS(s2n_connection_get_read_fd(conn, &getReadFd)); |
| 540 |
EXPECT_EQUAL(getReadFd, NEWFD); |
| 541 |
EXPECT_EQUAL(getWriteFd, NEWFD); |
| 542 |
|
| 543 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 544 |
}; |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
{ |
| 549 |
static const int OLDFD = 1; |
| 550 |
static const int NEWFD = 2; |
| 551 |
static int getReadFd, getWriteFd; |
| 552 |
|
| 553 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 554 |
EXPECT_NOT_NULL(conn); |
| 555 |
|
| 556 |
EXPECT_SUCCESS(s2n_connection_set_fd(conn, OLDFD)); |
| 557 |
EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, NEWFD)); |
| 558 |
EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, NEWFD)); |
| 559 |
|
| 560 |
EXPECT_SUCCESS(s2n_connection_get_write_fd(conn, &getWriteFd)); |
| 561 |
EXPECT_SUCCESS(s2n_connection_get_read_fd(conn, &getReadFd)); |
| 562 |
EXPECT_EQUAL(getReadFd, NEWFD); |
| 563 |
EXPECT_EQUAL(getWriteFd, NEWFD); |
| 564 |
|
| 565 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 566 |
}; |
| 567 |
|
| 568 |
|
| 569 |
{ |
| 570 |
static const int READFD = 1; |
| 571 |
static const int WRITEFD = 2; |
| 572 |
uint8_t socket_ctx[] = { "Some test context" }; |
| 573 |
|
| 574 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 575 |
EXPECT_NOT_NULL(conn); |
| 576 |
|
| 577 |
EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, READFD)); |
| 578 |
EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_noop_send_cb)); |
| 579 |
EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, socket_ctx)); |
| 580 |
|
| 581 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 582 |
|
| 583 |
EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, WRITEFD)); |
| 584 |
EXPECT_SUCCESS(s2n_connection_set_recv_cb(conn, s2n_noop_recv_cb)); |
| 585 |
EXPECT_SUCCESS(s2n_connection_set_recv_ctx(conn, socket_ctx)); |
| 586 |
|
| 587 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 588 |
}; |
| 589 |
|
| 590 |
|
| 591 |
{ |
| 592 |
static const int READFD = 1; |
| 593 |
uint8_t socket_ctx[] = { "Some test context" }; |
| 594 |
|
| 595 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 596 |
EXPECT_NOT_NULL(conn); |
| 597 |
|
| 598 |
|
| 599 |
EXPECT_SUCCESS(s2n_connection_set_fd(conn, READFD)); |
| 600 |
EXPECT_NOT_NULL(conn->send); |
| 601 |
EXPECT_NOT_NULL(conn->recv); |
| 602 |
|
| 603 |
|
| 604 |
EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, socket_ctx)); |
| 605 |
EXPECT_SUCCESS(s2n_connection_set_recv_ctx(conn, socket_ctx)); |
| 606 |
EXPECT_NULL(conn->send); |
| 607 |
EXPECT_NULL(conn->recv); |
| 608 |
|
| 609 |
|
| 610 |
EXPECT_SUCCESS(s2n_connection_set_fd(conn, READFD)); |
| 611 |
EXPECT_NOT_NULL(conn->send_io_context); |
| 612 |
EXPECT_NOT_NULL(conn->recv_io_context); |
| 613 |
|
| 614 |
|
| 615 |
EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_noop_send_cb)); |
| 616 |
EXPECT_SUCCESS(s2n_connection_set_recv_cb(conn, s2n_noop_recv_cb)); |
| 617 |
EXPECT_NULL(conn->send_io_context); |
| 618 |
EXPECT_NULL(conn->recv_io_context); |
| 619 |
|
| 620 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 621 |
}; |
| 622 |
#endif |
| 623 |
|
| 624 |
|
| 625 |
{ |
| 626 |
struct s2n_config *returned_config = NULL; |
| 627 |
struct s2n_config *config = s2n_config_new(); |
| 628 |
EXPECT_NOT_NULL(config); |
| 629 |
|
| 630 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 631 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_config(conn, &returned_config), S2N_ERR_NULL); |
| 632 |
|
| 633 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 634 |
EXPECT_SUCCESS(s2n_connection_get_config(conn, &returned_config)); |
| 635 |
EXPECT_EQUAL(returned_config, config); |
| 636 |
|
| 637 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 638 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 639 |
}; |
| 640 |
|
| 641 |
|
| 642 |
{ |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
{ |
| 647 |
struct s2n_security_policy rfc9151_applied_locally = security_policy_20250429; |
| 648 |
rfc9151_applied_locally.certificate_preferences_apply_locally = true; |
| 649 |
|
| 650 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 651 |
EXPECT_NOT_NULL(config); |
| 652 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 653 |
EXPECT_NOT_NULL(conn); |
| 654 |
|
| 655 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *invalid_cert = NULL, s2n_cert_chain_and_key_ptr_free); |
| 656 |
EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&invalid_cert, "rsae", "pss", "4096", "sha384")); |
| 657 |
|
| 658 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, invalid_cert)); |
| 659 |
conn->security_policy_override = &rfc9151_applied_locally; |
| 660 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_config(conn, config), S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 661 |
}; |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
{ |
| 670 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *invalid_cert = NULL, s2n_cert_chain_and_key_ptr_free); |
| 671 |
EXPECT_SUCCESS( |
| 672 |
s2n_test_cert_permutation_load_server_chain(&invalid_cert, "ec", "ecdsa", "p384", "sha256")); |
| 673 |
|
| 674 |
DEFER_CLEANUP(struct s2n_config *invalid_config = s2n_config_new(), s2n_config_ptr_free); |
| 675 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(invalid_config, invalid_cert)); |
| 676 |
|
| 677 |
|
| 678 |
const struct s2n_security_policy *security_policy = NULL; |
| 679 |
POSIX_GUARD(s2n_find_security_policy_from_version("rfc9151", &security_policy)); |
| 680 |
invalid_config->security_policy = security_policy; |
| 681 |
|
| 682 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 683 |
POSIX_ENSURE_REF(conn); |
| 684 |
|
| 685 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, invalid_config)); |
| 686 |
}; |
| 687 |
}; |
| 688 |
|
| 689 |
|
| 690 |
{ |
| 691 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 692 |
s2n_connection_ptr_free); |
| 693 |
EXPECT_NOT_NULL(conn); |
| 694 |
EXPECT_EQUAL(0, s2n_connection_get_wire_bytes_out(conn)); |
| 695 |
|
| 696 |
uint64_t magic_number = 123456; |
| 697 |
conn->wire_bytes_out = magic_number; |
| 698 |
EXPECT_EQUAL(magic_number, s2n_connection_get_wire_bytes_out(conn)); |
| 699 |
}; |
| 700 |
|
| 701 |
|
| 702 |
{ |
| 703 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 704 |
EXPECT_NOT_NULL(config); |
| 705 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 706 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 707 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key)); |
| 708 |
|
| 709 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), |
| 710 |
s2n_connection_ptr_free); |
| 711 |
EXPECT_NOT_NULL(client_conn); |
| 712 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 713 |
|
| 714 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), |
| 715 |
s2n_connection_ptr_free); |
| 716 |
EXPECT_NOT_NULL(server_conn); |
| 717 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 718 |
|
| 719 |
uint8_t app_data[100] = "hello world"; |
| 720 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 721 |
|
| 722 |
for (size_t i = 0; i < 10; i++) { |
| 723 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 724 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 725 |
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair)); |
| 726 |
|
| 727 |
|
| 728 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn)); |
| 729 |
|
| 730 |
|
| 731 |
EXPECT_SUCCESS(s2n_connection_free_handshake(client_conn)); |
| 732 |
EXPECT_SUCCESS(s2n_connection_free_handshake(server_conn)); |
| 733 |
|
| 734 |
|
| 735 |
EXPECT_EQUAL(s2n_send(client_conn, app_data, sizeof(app_data), &blocked), sizeof(app_data)); |
| 736 |
EXPECT_EQUAL(s2n_recv(server_conn, app_data, sizeof(app_data), &blocked), sizeof(app_data)); |
| 737 |
EXPECT_EQUAL(s2n_send(server_conn, app_data, sizeof(app_data), &blocked), sizeof(app_data)); |
| 738 |
EXPECT_EQUAL(s2n_recv(client_conn, app_data, sizeof(app_data), &blocked), sizeof(app_data)); |
| 739 |
|
| 740 |
|
| 741 |
EXPECT_SUCCESS(s2n_connection_release_buffers(client_conn)); |
| 742 |
EXPECT_SUCCESS(s2n_connection_release_buffers(server_conn)); |
| 743 |
|
| 744 |
|
| 745 |
EXPECT_SUCCESS(s2n_connection_wipe(client_conn)); |
| 746 |
EXPECT_SUCCESS(s2n_connection_wipe(server_conn)); |
| 747 |
} |
| 748 |
}; |
| 749 |
|
| 750 |
|
| 751 |
{ |
| 752 |
const uint32_t size = 10; |
| 753 |
|
| 754 |
|
| 755 |
{ |
| 756 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 757 |
s2n_connection_ptr_free); |
| 758 |
EXPECT_NOT_NULL(conn); |
| 759 |
|
| 760 |
|
| 761 |
for (size_t i = 0; i < 3; i++) { |
| 762 |
EXPECT_FALSE(conn->post_handshake.in.growable); |
| 763 |
EXPECT_FALSE(conn->post_handshake.in.alloced); |
| 764 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 765 |
|
| 766 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 767 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
for (size_t i = 0; i < 3; i++) { |
| 772 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&conn->post_handshake.in, size)); |
| 773 |
EXPECT_TRUE(conn->post_handshake.in.growable); |
| 774 |
EXPECT_TRUE(conn->post_handshake.in.alloced); |
| 775 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, size); |
| 776 |
|
| 777 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 778 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
for (size_t i = 0; i < 3; i++) { |
| 783 |
struct s2n_blob static_blob = { 0 }; |
| 784 |
EXPECT_SUCCESS(s2n_blob_init(&static_blob, conn->post_handshake.header_in, |
| 785 |
sizeof(conn->post_handshake.header_in))); |
| 786 |
EXPECT_SUCCESS(s2n_stuffer_init(&conn->post_handshake.in, &static_blob)); |
| 787 |
EXPECT_FALSE(conn->post_handshake.in.growable); |
| 788 |
EXPECT_FALSE(conn->post_handshake.in.alloced); |
| 789 |
EXPECT_NOT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 790 |
|
| 791 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 792 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 793 |
} |
| 794 |
}; |
| 795 |
|
| 796 |
|
| 797 |
{ |
| 798 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 799 |
s2n_connection_ptr_free); |
| 800 |
EXPECT_NOT_NULL(conn); |
| 801 |
|
| 802 |
|
| 803 |
for (size_t i = 0; i < 3; i++) { |
| 804 |
EXPECT_FALSE(conn->post_handshake.in.growable); |
| 805 |
EXPECT_FALSE(conn->post_handshake.in.alloced); |
| 806 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 807 |
|
| 808 |
EXPECT_SUCCESS(s2n_connection_release_buffers(conn)); |
| 809 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
for (size_t i = 0; i < 3; i++) { |
| 814 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&conn->post_handshake.in, size)); |
| 815 |
EXPECT_TRUE(conn->post_handshake.in.growable); |
| 816 |
EXPECT_TRUE(conn->post_handshake.in.alloced); |
| 817 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, size); |
| 818 |
|
| 819 |
EXPECT_SUCCESS(s2n_connection_release_buffers(conn)); |
| 820 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
for (size_t i = 0; i < 3; i++) { |
| 825 |
struct s2n_blob static_blob = { 0 }; |
| 826 |
EXPECT_SUCCESS(s2n_blob_init(&static_blob, conn->post_handshake.header_in, |
| 827 |
sizeof(conn->post_handshake.header_in))); |
| 828 |
EXPECT_SUCCESS(s2n_stuffer_init(&conn->post_handshake.in, &static_blob)); |
| 829 |
EXPECT_FALSE(conn->post_handshake.in.growable); |
| 830 |
EXPECT_FALSE(conn->post_handshake.in.alloced); |
| 831 |
EXPECT_NOT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 832 |
|
| 833 |
EXPECT_SUCCESS(s2n_connection_release_buffers(conn)); |
| 834 |
EXPECT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
{ |
| 839 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&conn->post_handshake.in, size)); |
| 840 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->post_handshake.in, 1)); |
| 841 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_release_buffers(conn), |
| 842 |
S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA); |
| 843 |
EXPECT_NOT_EQUAL(conn->post_handshake.in.blob.size, 0); |
| 844 |
}; |
| 845 |
}; |
| 846 |
}; |
| 847 |
|
| 848 |
|
| 849 |
{ |
| 850 |
|
| 851 |
{ |
| 852 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 853 |
s2n_connection_ptr_free); |
| 854 |
EXPECT_NOT_NULL(conn); |
| 855 |
|
| 856 |
EXPECT_FALSE(s2n_connection_check_io_status(NULL, S2N_IO_WRITABLE)); |
| 857 |
EXPECT_FALSE(s2n_connection_check_io_status(NULL, S2N_IO_READABLE)); |
| 858 |
EXPECT_FALSE(s2n_connection_check_io_status(NULL, S2N_IO_FULL_DUPLEX)); |
| 859 |
EXPECT_FALSE(s2n_connection_check_io_status(NULL, S2N_IO_CLOSED)); |
| 860 |
EXPECT_FALSE(s2n_connection_check_io_status(NULL, 10)); |
| 861 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, 10)); |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
{ |
| 866 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 867 |
s2n_connection_ptr_free); |
| 868 |
EXPECT_NOT_NULL(conn); |
| 869 |
conn->actual_protocol_version = S2N_TLS12; |
| 870 |
|
| 871 |
|
| 872 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 873 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 874 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 875 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 876 |
|
| 877 |
|
| 878 |
s2n_atomic_flag_set(&conn->write_closed); |
| 879 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 880 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 881 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 882 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 883 |
s2n_atomic_flag_clear(&conn->write_closed); |
| 884 |
|
| 885 |
|
| 886 |
s2n_atomic_flag_set(&conn->read_closed); |
| 887 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 888 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 889 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 890 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 891 |
s2n_atomic_flag_clear(&conn->read_closed); |
| 892 |
|
| 893 |
|
| 894 |
s2n_atomic_flag_set(&conn->read_closed); |
| 895 |
s2n_atomic_flag_set(&conn->write_closed); |
| 896 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 897 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 898 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 899 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 900 |
}; |
| 901 |
|
| 902 |
|
| 903 |
{ |
| 904 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 905 |
s2n_connection_ptr_free); |
| 906 |
EXPECT_NOT_NULL(conn); |
| 907 |
conn->actual_protocol_version = S2N_TLS13; |
| 908 |
|
| 909 |
|
| 910 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 911 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 912 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 913 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 914 |
|
| 915 |
|
| 916 |
s2n_atomic_flag_set(&conn->write_closed); |
| 917 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 918 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 919 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 920 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 921 |
s2n_atomic_flag_clear(&conn->write_closed); |
| 922 |
|
| 923 |
|
| 924 |
s2n_atomic_flag_set(&conn->read_closed); |
| 925 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 926 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 927 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 928 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 929 |
s2n_atomic_flag_clear(&conn->read_closed); |
| 930 |
|
| 931 |
|
| 932 |
s2n_atomic_flag_set(&conn->read_closed); |
| 933 |
s2n_atomic_flag_set(&conn->write_closed); |
| 934 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 935 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 936 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 937 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 938 |
}; |
| 939 |
}; |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
#ifndef _WIN32 |
| 946 |
{ |
| 947 |
|
| 948 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 949 |
s2n_connection_ptr_free); |
| 950 |
EXPECT_NOT_NULL(conn); |
| 951 |
uint8_t send_count = 0, recv_count = 0; |
| 952 |
EXPECT_FAILURE_WITH_ERRNO( |
| 953 |
s2n_connection_get_key_update_counts(NULL, &send_count, &recv_count), |
| 954 |
S2N_ERR_NULL); |
| 955 |
EXPECT_FAILURE_WITH_ERRNO( |
| 956 |
s2n_connection_get_key_update_counts(conn, NULL, &recv_count), |
| 957 |
S2N_ERR_NULL); |
| 958 |
EXPECT_FAILURE_WITH_ERRNO( |
| 959 |
s2n_connection_get_key_update_counts(conn, &send_count, NULL), |
| 960 |
S2N_ERR_NULL); |
| 961 |
|
| 962 |
|
| 963 |
const uint8_t expected_send_count = 10; |
| 964 |
conn->send_key_updated = expected_send_count; |
| 965 |
const uint8_t expected_recv_count = 255; |
| 966 |
conn->recv_key_updated = expected_recv_count; |
| 967 |
EXPECT_SUCCESS(s2n_connection_get_key_update_counts(conn, &send_count, &recv_count)); |
| 968 |
EXPECT_EQUAL(send_count, expected_send_count); |
| 969 |
EXPECT_EQUAL(recv_count, expected_recv_count); |
| 970 |
} |
| 971 |
#endif |
| 972 |
|
| 973 |
|
| 974 |
{ |
| 975 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 976 |
|
| 977 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 978 |
s2n_connection_ptr_free); |
| 979 |
EXPECT_NOT_NULL(client); |
| 980 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 981 |
|
| 982 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 983 |
s2n_connection_ptr_free); |
| 984 |
EXPECT_NOT_NULL(server); |
| 985 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 986 |
|
| 987 |
|
| 988 |
{ |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
server->client_cert_auth_type = S2N_CERT_AUTH_REQUIRED; |
| 993 |
client->client_cert_auth_type = S2N_CERT_AUTH_REQUIRED; |
| 994 |
|
| 995 |
s2n_cert_auth_type auth_type = S2N_CERT_AUTH_REQUIRED; |
| 996 |
EXPECT_SUCCESS(s2n_connection_get_client_auth_type(client, &auth_type)); |
| 997 |
EXPECT_EQUAL(auth_type, S2N_CERT_AUTH_OPTIONAL); |
| 998 |
EXPECT_SUCCESS(s2n_connection_get_client_auth_type(server, &auth_type)); |
| 999 |
EXPECT_EQUAL(auth_type, S2N_CERT_AUTH_NONE); |
| 1000 |
}; |
| 1001 |
|
| 1002 |
|
| 1003 |
{ |
| 1004 |
EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED)); |
| 1005 |
|
| 1006 |
s2n_cert_auth_type auth_type = S2N_CERT_AUTH_NONE; |
| 1007 |
EXPECT_SUCCESS(s2n_connection_get_client_auth_type(client, &auth_type)); |
| 1008 |
EXPECT_EQUAL(auth_type, S2N_CERT_AUTH_REQUIRED); |
| 1009 |
EXPECT_SUCCESS(s2n_connection_get_client_auth_type(server, &auth_type)); |
| 1010 |
EXPECT_EQUAL(auth_type, S2N_CERT_AUTH_REQUIRED); |
| 1011 |
}; |
| 1012 |
|
| 1013 |
|
| 1014 |
{ |
| 1015 |
EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client, S2N_CERT_AUTH_NONE)); |
| 1016 |
EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, S2N_CERT_AUTH_OPTIONAL)); |
| 1017 |
|
| 1018 |
s2n_cert_auth_type auth_type = S2N_CERT_AUTH_REQUIRED; |
| 1019 |
EXPECT_SUCCESS(s2n_connection_get_client_auth_type(client, &auth_type)); |
| 1020 |
EXPECT_EQUAL(auth_type, S2N_CERT_AUTH_NONE); |
| 1021 |
EXPECT_SUCCESS(s2n_connection_get_client_auth_type(server, &auth_type)); |
| 1022 |
EXPECT_EQUAL(auth_type, S2N_CERT_AUTH_OPTIONAL); |
| 1023 |
}; |
| 1024 |
}; |
| 1025 |
|
| 1026 |
|
| 1027 |
{ |
| 1028 |
|
| 1029 |
{ |
| 1030 |
struct s2n_connection conn = { 0 }; |
| 1031 |
const char *name = NULL; |
| 1032 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_signature_scheme(&conn, NULL), |
| 1033 |
S2N_ERR_NULL); |
| 1034 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_signature_scheme(NULL, &name), |
| 1035 |
S2N_ERR_NULL); |
| 1036 |
}; |
| 1037 |
|
| 1038 |
|
| 1039 |
{ |
| 1040 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 1041 |
s2n_connection_ptr_free); |
| 1042 |
const char *name = NULL; |
| 1043 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_signature_scheme(conn, &name), |
| 1044 |
S2N_ERR_INVALID_STATE); |
| 1045 |
}; |
| 1046 |
|
| 1047 |
|
| 1048 |
{ |
| 1049 |
const struct s2n_signature_preferences *all_prefs = &s2n_signature_preferences_all; |
| 1050 |
for (size_t i = 0; i < all_prefs->count; i++) { |
| 1051 |
for (size_t version = 0; version <= S2N_TLS13; version++) { |
| 1052 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 1053 |
s2n_connection_ptr_free); |
| 1054 |
conn->handshake.handshake_type = NEGOTIATED; |
| 1055 |
conn->handshake_params.server_cert_sig_scheme = all_prefs->signature_schemes[i]; |
| 1056 |
conn->actual_protocol_version = version; |
| 1057 |
|
| 1058 |
const char *name = NULL; |
| 1059 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &name)); |
| 1060 |
EXPECT_NOT_NULL(name); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
}; |
| 1064 |
|
| 1065 |
|
| 1066 |
{ |
| 1067 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 1068 |
s2n_connection_ptr_free); |
| 1069 |
conn->handshake.handshake_type = NEGOTIATED; |
| 1070 |
|
| 1071 |
conn->handshake_params.server_cert_sig_scheme = &s2n_rsa_pss_rsae_sha256; |
| 1072 |
const char *rsa_name = NULL; |
| 1073 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &rsa_name)); |
| 1074 |
EXPECT_STRING_EQUAL(rsa_name, "rsa_pss_rsae_sha256"); |
| 1075 |
|
| 1076 |
conn->handshake_params.server_cert_sig_scheme = &s2n_mldsa65; |
| 1077 |
const char *mldsa_name = NULL; |
| 1078 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &mldsa_name)); |
| 1079 |
EXPECT_STRING_EQUAL(mldsa_name, "mldsa65"); |
| 1080 |
|
| 1081 |
conn->handshake_params.server_cert_sig_scheme = &s2n_ecdsa_sha224; |
| 1082 |
const char *legacy_ecdsa = NULL; |
| 1083 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &legacy_ecdsa)); |
| 1084 |
EXPECT_STRING_EQUAL(legacy_ecdsa, "legacy_ecdsa_sha224"); |
| 1085 |
}; |
| 1086 |
|
| 1087 |
|
| 1088 |
{ |
| 1089 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 1090 |
s2n_connection_ptr_free); |
| 1091 |
conn->handshake.handshake_type = NEGOTIATED; |
| 1092 |
const char *name = NULL; |
| 1093 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &name)); |
| 1094 |
EXPECT_STRING_EQUAL(name, "none"); |
| 1095 |
}; |
| 1096 |
|
| 1097 |
|
| 1098 |
{ |
| 1099 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 1100 |
s2n_connection_ptr_free); |
| 1101 |
conn->handshake.handshake_type = NEGOTIATED; |
| 1102 |
conn->handshake_params.server_cert_sig_scheme = &s2n_ecdsa_sha256; |
| 1103 |
|
| 1104 |
const char *tls13_name = NULL; |
| 1105 |
conn->actual_protocol_version = S2N_TLS13; |
| 1106 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &tls13_name)); |
| 1107 |
EXPECT_STRING_EQUAL(tls13_name, s2n_ecdsa_sha256.tls13_name); |
| 1108 |
EXPECT_STRING_EQUAL(tls13_name, "ecdsa_secp256r1_sha256"); |
| 1109 |
|
| 1110 |
const char *tls12_name = NULL; |
| 1111 |
conn->actual_protocol_version = S2N_TLS12; |
| 1112 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &tls12_name)); |
| 1113 |
EXPECT_STRING_EQUAL(tls12_name, s2n_ecdsa_sha256.legacy_name); |
| 1114 |
EXPECT_STRING_EQUAL(tls12_name, "legacy_ecdsa_sha256"); |
| 1115 |
|
| 1116 |
const char *tls10_name = NULL; |
| 1117 |
conn->actual_protocol_version = S2N_TLS10; |
| 1118 |
EXPECT_SUCCESS(s2n_connection_get_signature_scheme(conn, &tls10_name)); |
| 1119 |
EXPECT_STRING_EQUAL(tls10_name, s2n_ecdsa_sha256.legacy_name); |
| 1120 |
}; |
| 1121 |
}; |
| 1122 |
|
| 1123 |
|
| 1124 |
{ |
| 1125 |
|
| 1126 |
EXPECT_EQUAL(s2n_connection_get_mode(NULL), S2N_SERVER); |
| 1127 |
|
| 1128 |
|
| 1129 |
{ |
| 1130 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 1131 |
s2n_connection_ptr_free); |
| 1132 |
EXPECT_EQUAL(s2n_connection_get_mode(conn), S2N_CLIENT); |
| 1133 |
|
| 1134 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 1135 |
s2n_connection_ptr_free); |
| 1136 |
EXPECT_EQUAL(s2n_connection_get_mode(server), S2N_SERVER); |
| 1137 |
}; |
| 1138 |
}; |
| 1139 |
|
| 1140 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_chain_and_key)); |
| 1141 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(rsa_chain_and_key)); |
| 1142 |
END_TEST(); |
| 1143 |
} |