| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdint.h> |
| 17 |
#include <sys/socket.h> |
| 18 |
#include <sys/wait.h> |
| 19 |
#include <time.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
#include "api/s2n.h" |
| 23 |
#include "s2n_test.h" |
| 24 |
#include "testlib/s2n_testlib.h" |
| 25 |
#include "tls/s2n_connection.h" |
| 26 |
#include "tls/s2n_handshake.h" |
| 27 |
|
| 28 |
#define SUPPORTED_CERTIFICATE_FORMATS (2) |
| 29 |
|
| 30 |
static const char *certificate_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_RSA_2048_PKCS8_CERT_CHAIN }; |
| 31 |
static const char *private_key_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_KEY, S2N_RSA_2048_PKCS8_KEY }; |
| 32 |
|
| 33 |
void mock_client(struct s2n_test_io_pair *io_pair) |
| 34 |
{ |
| 35 |
struct s2n_connection *conn = NULL; |
| 36 |
struct s2n_config *config = NULL; |
| 37 |
s2n_blocked_status blocked; |
| 38 |
|
| 39 |
|
| 40 |
sleep(1); |
| 41 |
|
| 42 |
conn = s2n_connection_new(S2N_CLIENT); |
| 43 |
config = s2n_config_new(); |
| 44 |
s2n_config_disable_x509_verification(config); |
| 45 |
EXPECT_OK(s2n_config_set_tls12_security_policy(config)); |
| 46 |
s2n_connection_set_config(conn, config); |
| 47 |
|
| 48 |
s2n_connection_set_io_pair(conn, io_pair); |
| 49 |
|
| 50 |
int result = s2n_negotiate(conn, &blocked); |
| 51 |
if (result < 0) { |
| 52 |
exit(1); |
| 53 |
} |
| 54 |
|
| 55 |
result = s2n_connection_free_handshake(conn); |
| 56 |
if (result < 0) { |
| 57 |
exit(1); |
| 58 |
} |
| 59 |
|
| 60 |
#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) |
| 61 |
|
| 62 |
s2n_io_pair_close_one_end(io_pair, S2N_CLIENT); |
| 63 |
#else |
| 64 |
|
| 65 |
s2n_io_pair_shutdown_one_end(io_pair, S2N_CLIENT, SHUT_RD); |
| 66 |
#endif |
| 67 |
|
| 68 |
sleep(2); |
| 69 |
|
| 70 |
s2n_shutdown(conn, &blocked); |
| 71 |
|
| 72 |
result = s2n_connection_free(conn); |
| 73 |
if (result < 0) { |
| 74 |
exit(1); |
| 75 |
} |
| 76 |
|
| 77 |
result = s2n_config_free(config); |
| 78 |
if (result < 0) { |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
sleep(1); |
| 84 |
|
| 85 |
s2n_io_pair_shutdown_one_end(io_pair, S2N_CLIENT, SHUT_WR); |
| 86 |
s2n_io_pair_close_one_end(io_pair, S2N_CLIENT); |
| 87 |
|
| 88 |
exit(0); |
| 89 |
} |
| 90 |
|
| 91 |
int main(int argc, char **argv) |
| 92 |
{ |
| 93 |
struct s2n_connection *conn = NULL; |
| 94 |
struct s2n_config *config = NULL; |
| 95 |
s2n_blocked_status blocked; |
| 96 |
int status = 0; |
| 97 |
char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE]; |
| 98 |
char private_key_pem[S2N_MAX_TEST_PEM_SIZE]; |
| 99 |
char dhparams_pem[S2N_MAX_TEST_PEM_SIZE]; |
| 100 |
|
| 101 |
BEGIN_TEST(); |
| 102 |
|
| 103 |
for (int is_dh_key_exchange = 0; is_dh_key_exchange <= 1; is_dh_key_exchange++) { |
| 104 |
struct s2n_cert_chain_and_key *chain_and_keys[SUPPORTED_CERTIFICATE_FORMATS]; |
| 105 |
|
| 106 |
|
| 107 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 108 |
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair)); |
| 109 |
|
| 110 |
|
| 111 |
pid_t pid = fork(); |
| 112 |
if (pid == 0) { |
| 113 |
|
| 114 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 115 |
|
| 116 |
|
| 117 |
mock_client(&io_pair); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 122 |
|
| 123 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 124 |
|
| 125 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 126 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501")); |
| 127 |
for (int cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) { |
| 128 |
EXPECT_SUCCESS(s2n_read_test_pem(certificate_paths[cert], cert_chain_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 129 |
EXPECT_SUCCESS(s2n_read_test_pem(private_key_paths[cert], private_key_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 130 |
EXPECT_NOT_NULL(chain_and_keys[cert] = s2n_cert_chain_and_key_new()); |
| 131 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_keys[cert], cert_chain_pem, private_key_pem)); |
| 132 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_keys[cert])); |
| 133 |
} |
| 134 |
|
| 135 |
if (is_dh_key_exchange) { |
| 136 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 137 |
EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams_pem)); |
| 138 |
} |
| 139 |
|
| 140 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 141 |
|
| 142 |
|
| 143 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair)); |
| 144 |
|
| 145 |
|
| 146 |
EXPECT_SUCCESS(s2n_negotiate(conn, &blocked)); |
| 147 |
EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12); |
| 148 |
|
| 149 |
|
| 150 |
sleep(1); |
| 151 |
char buffer[1]; |
| 152 |
|
| 153 |
ssize_t w = s2n_send(conn, buffer, 1, &blocked); |
| 154 |
EXPECT_EQUAL(w, -1); |
| 155 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_IO); |
| 156 |
EXPECT_EQUAL(errno, EPIPE); |
| 157 |
|
| 158 |
|
| 159 |
w = s2n_shutdown(conn, &blocked); |
| 160 |
EXPECT_EQUAL(w, -1); |
| 161 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_IO); |
| 162 |
EXPECT_EQUAL(errno, 0); |
| 163 |
|
| 164 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 165 |
for (int cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) { |
| 166 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_keys[cert])); |
| 167 |
} |
| 168 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 169 |
|
| 170 |
|
| 171 |
EXPECT_EQUAL(waitpid(-1, &status, 0), pid); |
| 172 |
if (getenv("S2N_VALGRIND") == NULL) { |
| 173 |
EXPECT_EQUAL(status, 0); |
| 174 |
} |
| 175 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 176 |
} |
| 177 |
|
| 178 |
END_TEST(); |
| 179 |
} |