| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "s2n_test.h" |
| 17 |
#include "testlib/s2n_testlib.h" |
| 18 |
|
| 19 |
#define S2N_TEST_CERT_MEM 5000 |
| 20 |
|
| 21 |
S2N_RESULT s2n_async_pkey_op_copy_hash_state_for_testing(struct s2n_async_pkey_op *op, |
| 22 |
struct s2n_hash_state *copy); |
| 23 |
|
| 24 |
struct s2n_async_pkey_op *pkey_op = NULL; |
| 25 |
struct s2n_connection *pkey_op_conn = NULL; |
| 26 |
static int s2n_test_async_pkey_cb(struct s2n_connection *conn, struct s2n_async_pkey_op *op) |
| 27 |
{ |
| 28 |
pkey_op = op; |
| 29 |
pkey_op_conn = conn; |
| 30 |
return S2N_SUCCESS; |
| 31 |
} |
| 32 |
|
| 33 |
static S2N_RESULT s2n_test_pkey_sign(const struct s2n_pkey *pkey, |
| 34 |
struct s2n_blob *input, struct s2n_blob *output, |
| 35 |
s2n_signature_algorithm sig_alg) |
| 36 |
{ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
DEFER_CLEANUP(struct s2n_blob digest = { 0 }, s2n_free); |
| 47 |
RESULT_GUARD_POSIX(s2n_alloc(&digest, input->size)); |
| 48 |
DEFER_CLEANUP(struct s2n_hash_state digest_copy = { 0 }, s2n_hash_free); |
| 49 |
RESULT_GUARD_POSIX(s2n_hash_new(&digest_copy)); |
| 50 |
RESULT_GUARD(s2n_async_pkey_op_copy_hash_state_for_testing(pkey_op, &digest_copy)); |
| 51 |
RESULT_GUARD_POSIX(s2n_hash_digest(&digest_copy, digest.data, digest.size)); |
| 52 |
EXPECT_BYTEARRAY_EQUAL(digest.data, input->data, input->size); |
| 53 |
|
| 54 |
|
| 55 |
DEFER_CLEANUP(struct s2n_hash_state sign_copy = { 0 }, s2n_hash_free); |
| 56 |
RESULT_GUARD_POSIX(s2n_hash_new(&sign_copy)); |
| 57 |
RESULT_GUARD(s2n_async_pkey_op_copy_hash_state_for_testing(pkey_op, &sign_copy)); |
| 58 |
RESULT_GUARD_POSIX(s2n_pkey_sign(pkey, sig_alg, &sign_copy, output)); |
| 59 |
|
| 60 |
return S2N_RESULT_OK; |
| 61 |
} |
| 62 |
|
| 63 |
static S2N_RESULT s2n_async_pkey_sign_test(struct s2n_cert_chain_and_key *complete_chain) |
| 64 |
{ |
| 65 |
RESULT_ENSURE_REF(pkey_op); |
| 66 |
RESULT_ENSURE_REF(pkey_op_conn); |
| 67 |
RESULT_ENSURE_REF(complete_chain); |
| 68 |
|
| 69 |
|
| 70 |
uint32_t input_len = 0; |
| 71 |
DEFER_CLEANUP(struct s2n_blob input = { 0 }, s2n_free); |
| 72 |
RESULT_GUARD_POSIX(s2n_async_pkey_op_get_input_size(pkey_op, &input_len)); |
| 73 |
RESULT_GUARD_POSIX(s2n_realloc(&input, input_len)); |
| 74 |
RESULT_GUARD_POSIX(s2n_async_pkey_op_get_input(pkey_op, input.data, input.size)); |
| 75 |
|
| 76 |
|
| 77 |
uint32_t output_len = 0; |
| 78 |
DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free); |
| 79 |
RESULT_GUARD(s2n_pkey_size(complete_chain->private_key, &output_len)); |
| 80 |
RESULT_GUARD_POSIX(s2n_realloc(&output, output_len)); |
| 81 |
|
| 82 |
|
| 83 |
s2n_tls_signature_algorithm sig_alg = 0; |
| 84 |
const struct s2n_signature_scheme *sig_scheme = NULL; |
| 85 |
if (pkey_op_conn->mode == S2N_CLIENT) { |
| 86 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_client_cert_signature_algorithm(pkey_op_conn, &sig_alg)); |
| 87 |
sig_scheme = pkey_op_conn->handshake_params.client_cert_sig_scheme; |
| 88 |
} else { |
| 89 |
RESULT_GUARD_POSIX(s2n_connection_get_selected_signature_algorithm(pkey_op_conn, &sig_alg)); |
| 90 |
sig_scheme = pkey_op_conn->handshake_params.server_cert_sig_scheme; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
s2n_async_pkey_op_type op_type = 0; |
| 98 |
RESULT_GUARD_POSIX(s2n_async_pkey_op_get_op_type(pkey_op, &op_type)); |
| 99 |
if (op_type == S2N_ASYNC_DECRYPT) { |
| 100 |
output.size = S2N_TLS_SECRET_LEN; |
| 101 |
RESULT_GUARD_POSIX(s2n_pkey_decrypt(complete_chain->private_key, &input, &output)); |
| 102 |
} else if (op_type == S2N_ASYNC_SIGN) { |
| 103 |
RESULT_GUARD(s2n_test_pkey_sign(complete_chain->private_key, &input, &output, |
| 104 |
sig_scheme->sig_alg)); |
| 105 |
} else { |
| 106 |
RESULT_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
RESULT_GUARD_POSIX(s2n_async_pkey_op_set_output(pkey_op, output.data, output.size)); |
| 111 |
RESULT_GUARD_POSIX(s2n_async_pkey_op_apply(pkey_op, pkey_op_conn)); |
| 112 |
RESULT_GUARD_POSIX(s2n_async_pkey_op_free(pkey_op)); |
| 113 |
pkey_op = NULL; |
| 114 |
pkey_op_conn = NULL; |
| 115 |
|
| 116 |
return S2N_RESULT_OK; |
| 117 |
} |
| 118 |
|
| 119 |
static S2N_RESULT s2n_do_test_handshake(struct s2n_config *config, struct s2n_cert_chain_and_key *complete_chain, |
| 120 |
uint8_t expected_protocol_version, uint32_t expected_handshake_type) |
| 121 |
{ |
| 122 |
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT); |
| 123 |
RESULT_ENSURE_REF(client_conn); |
| 124 |
RESULT_GUARD_POSIX(s2n_connection_set_config(client_conn, config)); |
| 125 |
|
| 126 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 127 |
RESULT_ENSURE_REF(server_conn); |
| 128 |
RESULT_GUARD_POSIX(s2n_connection_set_config(server_conn, config)); |
| 129 |
|
| 130 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 131 |
RESULT_GUARD_POSIX(s2n_io_pair_init_non_blocking(&io_pair)); |
| 132 |
RESULT_GUARD_POSIX(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 133 |
RESULT_GUARD_POSIX(s2n_connection_set_io_pair(server_conn, &io_pair)); |
| 134 |
|
| 135 |
while (s2n_negotiate_test_server_and_client(server_conn, client_conn) != S2N_SUCCESS) { |
| 136 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_ASYNC_BLOCKED); |
| 137 |
RESULT_GUARD(s2n_async_pkey_sign_test(complete_chain)); |
| 138 |
} |
| 139 |
|
| 140 |
RESULT_ENSURE_EQ(client_conn->actual_protocol_version, expected_protocol_version); |
| 141 |
RESULT_ENSURE_EQ(server_conn->actual_protocol_version, expected_protocol_version); |
| 142 |
RESULT_ENSURE_EQ(client_conn->handshake.handshake_type, expected_handshake_type); |
| 143 |
RESULT_ENSURE_EQ(server_conn->handshake.handshake_type, expected_handshake_type); |
| 144 |
|
| 145 |
RESULT_GUARD_POSIX(s2n_connection_free(server_conn)); |
| 146 |
RESULT_GUARD_POSIX(s2n_connection_free(client_conn)); |
| 147 |
RESULT_GUARD_POSIX(s2n_io_pair_close(&io_pair)); |
| 148 |
|
| 149 |
return S2N_RESULT_OK; |
| 150 |
} |
| 151 |
|
| 152 |
int main(int argc, char **argv) |
| 153 |
{ |
| 154 |
BEGIN_TEST(); |
| 155 |
|
| 156 |
uint32_t pem_len = 0; |
| 157 |
uint8_t pem[S2N_TEST_CERT_MEM] = { 0 }; |
| 158 |
|
| 159 |
const char *tls12_policy = "ELBSecurityPolicy-2016-08"; |
| 160 |
const char *tls13_policy = "default_tls13"; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
const char *tls12_rsa_kex_policy = "test_all_rsa_kex"; |
| 167 |
|
| 168 |
uint32_t basic_handshake = NEGOTIATED | FULL_HANDSHAKE; |
| 169 |
uint32_t tls_13_handshake = (basic_handshake | MIDDLEBOX_COMPAT); |
| 170 |
uint32_t tls_12_handshake = (basic_handshake | TLS12_PERFECT_FORWARD_SECRECY); |
| 171 |
uint32_t expected_handshake_with_tls13_policy = s2n_is_tls13_fully_supported() ? tls_13_handshake : tls_12_handshake; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
struct s2n_cert_chain_and_key *ecdsa_complete_chain = NULL; |
| 179 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_complete_chain, |
| 180 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 181 |
|
| 182 |
struct s2n_cert_chain_and_key *rsa_complete_chain = NULL; |
| 183 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&rsa_complete_chain, |
| 184 |
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
struct s2n_cert_chain_and_key *ecdsa_cert_only_chain = s2n_cert_chain_and_key_new(); |
| 191 |
EXPECT_NOT_NULL(ecdsa_cert_only_chain); |
| 192 |
EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, pem, &pem_len, sizeof(pem))); |
| 193 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_public_pem_bytes(ecdsa_cert_only_chain, pem, pem_len)); |
| 194 |
|
| 195 |
struct s2n_cert_chain_and_key *rsa_cert_only_chain = s2n_cert_chain_and_key_new(); |
| 196 |
EXPECT_NOT_NULL(rsa_cert_only_chain); |
| 197 |
EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_CERT_CHAIN, pem, &pem_len, sizeof(pem))); |
| 198 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_public_pem_bytes(rsa_cert_only_chain, pem, pem_len)); |
| 199 |
|
| 200 |
|
| 201 |
{ |
| 202 |
struct s2n_config *config = s2n_config_new(); |
| 203 |
EXPECT_NOT_NULL(config); |
| 204 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 205 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_cert_only_chain)); |
| 206 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(config, s2n_test_async_pkey_cb)); |
| 207 |
|
| 208 |
|
| 209 |
{ |
| 210 |
|
| 211 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls12_policy)); |
| 212 |
EXPECT_OK(s2n_do_test_handshake(config, ecdsa_complete_chain, |
| 213 |
S2N_TLS12, basic_handshake | TLS12_PERFECT_FORWARD_SECRECY)); |
| 214 |
|
| 215 |
|
| 216 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls13_policy)); |
| 217 |
EXPECT_OK(s2n_do_test_handshake(config, ecdsa_complete_chain, |
| 218 |
s2n_get_highest_fully_supported_tls_version(), expected_handshake_with_tls13_policy)); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
{ |
| 223 |
EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED)); |
| 224 |
|
| 225 |
|
| 226 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls12_policy)); |
| 227 |
EXPECT_OK(s2n_do_test_handshake(config, ecdsa_complete_chain, |
| 228 |
S2N_TLS12, basic_handshake | CLIENT_AUTH | TLS12_PERFECT_FORWARD_SECRECY)); |
| 229 |
|
| 230 |
|
| 231 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls13_policy)); |
| 232 |
EXPECT_OK(s2n_do_test_handshake(config, ecdsa_complete_chain, |
| 233 |
s2n_get_highest_fully_supported_tls_version(), expected_handshake_with_tls13_policy | CLIENT_AUTH)); |
| 234 |
}; |
| 235 |
|
| 236 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 237 |
}; |
| 238 |
|
| 239 |
|
| 240 |
{ |
| 241 |
struct s2n_config *config = s2n_config_new(); |
| 242 |
EXPECT_NOT_NULL(config); |
| 243 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 244 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, rsa_cert_only_chain)); |
| 245 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(config, s2n_test_async_pkey_cb)); |
| 246 |
|
| 247 |
|
| 248 |
{ |
| 249 |
|
| 250 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls12_rsa_kex_policy)); |
| 251 |
EXPECT_OK(s2n_do_test_handshake(config, rsa_complete_chain, |
| 252 |
S2N_TLS12, basic_handshake)); |
| 253 |
|
| 254 |
|
| 255 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls12_policy)); |
| 256 |
EXPECT_OK(s2n_do_test_handshake(config, rsa_complete_chain, |
| 257 |
S2N_TLS12, basic_handshake | TLS12_PERFECT_FORWARD_SECRECY)); |
| 258 |
|
| 259 |
|
| 260 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls13_policy)); |
| 261 |
EXPECT_OK(s2n_do_test_handshake(config, rsa_complete_chain, |
| 262 |
s2n_get_highest_fully_supported_tls_version(), expected_handshake_with_tls13_policy)); |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
{ |
| 267 |
EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED)); |
| 268 |
|
| 269 |
|
| 270 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls12_rsa_kex_policy)); |
| 271 |
EXPECT_OK(s2n_do_test_handshake(config, rsa_complete_chain, |
| 272 |
S2N_TLS12, basic_handshake | CLIENT_AUTH)); |
| 273 |
|
| 274 |
|
| 275 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls12_policy)); |
| 276 |
EXPECT_OK(s2n_do_test_handshake(config, rsa_complete_chain, |
| 277 |
S2N_TLS12, basic_handshake | CLIENT_AUTH | TLS12_PERFECT_FORWARD_SECRECY)); |
| 278 |
|
| 279 |
|
| 280 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, tls13_policy)); |
| 281 |
EXPECT_OK(s2n_do_test_handshake(config, rsa_complete_chain, |
| 282 |
s2n_get_highest_fully_supported_tls_version(), expected_handshake_with_tls13_policy | CLIENT_AUTH)); |
| 283 |
}; |
| 284 |
|
| 285 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 286 |
}; |
| 287 |
|
| 288 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_complete_chain)); |
| 289 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_cert_only_chain)); |
| 290 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(rsa_complete_chain)); |
| 291 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(rsa_cert_only_chain)); |
| 292 |
|
| 293 |
END_TEST(); |
| 294 |
} |