| 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_async_pkey.h" |
| 22 |
#include "tls/s2n_connection.h" |
| 23 |
#include "tls/s2n_tls.h" |
| 24 |
#include "utils/s2n_result.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
|
| 27 |
static bool async_callback_invoked = false; |
| 28 |
static bool async_sign_operation_called_s2n_client = false; |
| 29 |
static struct s2n_async_pkey_op *pkey_op = NULL; |
| 30 |
static struct s2n_connection *pkey_conn = NULL; |
| 31 |
|
| 32 |
const uint8_t test_signature_data[] = "I signed this"; |
| 33 |
const uint32_t test_signature_size = sizeof(test_signature_data); |
| 34 |
const uint32_t test_max_signature_size = 2 * sizeof(test_signature_data); |
| 35 |
|
| 36 |
typedef int(async_handler)(struct s2n_connection *conn, s2n_blocked_status *block); |
| 37 |
|
| 38 |
static S2N_RESULT test_size(const struct s2n_pkey *pkey, uint32_t *size_out) |
| 39 |
{ |
| 40 |
*size_out = test_max_signature_size; |
| 41 |
return S2N_RESULT_OK; |
| 42 |
} |
| 43 |
|
| 44 |
static int test_sign(const struct s2n_pkey *priv_key, s2n_signature_algorithm sig_alg, |
| 45 |
struct s2n_hash_state *digest, struct s2n_blob *signature) |
| 46 |
{ |
| 47 |
POSIX_CHECKED_MEMCPY(signature->data, test_signature_data, test_signature_size); |
| 48 |
signature->size = test_signature_size; |
| 49 |
return S2N_SUCCESS; |
| 50 |
} |
| 51 |
|
| 52 |
int s2n_async_pkey_store_op(struct s2n_connection *conn, struct s2n_async_pkey_op *op) |
| 53 |
{ |
| 54 |
EXPECT_NOT_NULL(conn); |
| 55 |
EXPECT_NOT_NULL(op); |
| 56 |
|
| 57 |
async_callback_invoked = true; |
| 58 |
pkey_op = op; |
| 59 |
pkey_conn = conn; |
| 60 |
|
| 61 |
return S2N_SUCCESS; |
| 62 |
} |
| 63 |
|
| 64 |
static int s2n_test_negotiate_with_async_pkey_op_handler(struct s2n_connection *conn, s2n_blocked_status *block) |
| 65 |
{ |
| 66 |
int rc = s2n_negotiate(conn, block); |
| 67 |
if (!(rc == 0 || (*block && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED))) { |
| 68 |
return S2N_FAILURE; |
| 69 |
} |
| 70 |
|
| 71 |
if (*block == S2N_BLOCKED_ON_APPLICATION_INPUT && pkey_op != NULL) { |
| 72 |
struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(pkey_conn); |
| 73 |
EXPECT_NOT_NULL(chain_and_key); |
| 74 |
|
| 75 |
s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key); |
| 76 |
EXPECT_NOT_NULL(pkey); |
| 77 |
|
| 78 |
EXPECT_SUCCESS(s2n_async_pkey_op_perform(pkey_op, pkey)); |
| 79 |
EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn)); |
| 80 |
EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op)); |
| 81 |
pkey_op = NULL; |
| 82 |
pkey_conn = NULL; |
| 83 |
} |
| 84 |
|
| 85 |
return S2N_SUCCESS; |
| 86 |
} |
| 87 |
|
| 88 |
static int s2n_test_apply_with_invalid_signature_handler(struct s2n_connection *conn, s2n_blocked_status *block) |
| 89 |
{ |
| 90 |
int rc = s2n_negotiate(conn, block); |
| 91 |
if (!(rc == 0 || (*block && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED))) { |
| 92 |
return S2N_FAILURE; |
| 93 |
} |
| 94 |
|
| 95 |
if (*block == S2N_BLOCKED_ON_APPLICATION_INPUT && pkey_op != NULL) { |
| 96 |
struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(pkey_conn); |
| 97 |
EXPECT_NOT_NULL(chain_and_key); |
| 98 |
|
| 99 |
s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key); |
| 100 |
EXPECT_NOT_NULL(pkey); |
| 101 |
|
| 102 |
EXPECT_SUCCESS(s2n_async_pkey_op_perform(pkey_op, pkey)); |
| 103 |
|
| 104 |
|
| 105 |
s2n_async_pkey_op_type type = { 0 }; |
| 106 |
EXPECT_SUCCESS(s2n_async_pkey_op_get_op_type(pkey_op, &type)); |
| 107 |
|
| 108 |
|
| 109 |
if (type == S2N_ASYNC_SIGN) { |
| 110 |
|
| 111 |
struct s2n_cert_chain_and_key *chain_and_key_2 = NULL; |
| 112 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key_2, |
| 113 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 114 |
|
| 115 |
|
| 116 |
EXPECT_NOT_NULL(conn->handshake_params.our_chain_and_key); |
| 117 |
conn->handshake_params.our_chain_and_key = chain_and_key_2; |
| 118 |
|
| 119 |
|
| 120 |
EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_apply(pkey_op, conn), S2N_ERR_VERIFY_SIGNATURE); |
| 121 |
|
| 122 |
|
| 123 |
EXPECT_SUCCESS(s2n_async_pkey_op_set_validation_mode(pkey_op, S2N_ASYNC_PKEY_VALIDATION_FAST)); |
| 124 |
EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn)); |
| 125 |
|
| 126 |
|
| 127 |
conn->handshake_params.our_chain_and_key = chain_and_key; |
| 128 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key_2)); |
| 129 |
|
| 130 |
|
| 131 |
if (conn->mode == S2N_CLIENT) { |
| 132 |
async_sign_operation_called_s2n_client = true; |
| 133 |
} |
| 134 |
} else { |
| 135 |
EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn)); |
| 136 |
} |
| 137 |
|
| 138 |
EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op)); |
| 139 |
pkey_op = NULL; |
| 140 |
pkey_conn = NULL; |
| 141 |
} |
| 142 |
|
| 143 |
return S2N_SUCCESS; |
| 144 |
} |
| 145 |
|
| 146 |
static int s2n_try_handshake_with_async_pkey_op(struct s2n_connection *server_conn, struct s2n_connection *client_conn, |
| 147 |
async_handler handler) |
| 148 |
{ |
| 149 |
s2n_blocked_status server_blocked = { 0 }; |
| 150 |
s2n_blocked_status client_blocked = { 0 }; |
| 151 |
|
| 152 |
do { |
| 153 |
EXPECT_SUCCESS(handler(client_conn, &client_blocked)); |
| 154 |
EXPECT_SUCCESS(handler(server_conn, &server_blocked)); |
| 155 |
} while (client_blocked || server_blocked); |
| 156 |
|
| 157 |
POSIX_GUARD(s2n_shutdown_test_server_and_client(server_conn, client_conn)); |
| 158 |
|
| 159 |
return S2N_SUCCESS; |
| 160 |
} |
| 161 |
|
| 162 |
int main(int argc, char **argv) |
| 163 |
{ |
| 164 |
BEGIN_TEST(); |
| 165 |
|
| 166 |
|
| 167 |
{ |
| 168 |
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT); |
| 169 |
EXPECT_NOT_NULL(conn); |
| 170 |
|
| 171 |
|
| 172 |
conn->handshake_params.client_cert_sig_scheme = &s2n_rsa_pkcs1_md5_sha1; |
| 173 |
|
| 174 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 175 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 176 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 177 |
chain_and_key->private_key->size = test_size; |
| 178 |
chain_and_key->private_key->sign = test_sign; |
| 179 |
conn->handshake_params.our_chain_and_key = chain_and_key; |
| 180 |
|
| 181 |
EXPECT_SUCCESS(s2n_client_cert_verify_send(conn)); |
| 182 |
|
| 183 |
uint16_t signature_scheme_iana = 0; |
| 184 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&conn->handshake.io, &signature_scheme_iana)); |
| 185 |
EXPECT_EQUAL(signature_scheme_iana, s2n_rsa_pkcs1_md5_sha1.iana_value); |
| 186 |
|
| 187 |
uint16_t signature_size = 0; |
| 188 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&conn->handshake.io, &signature_size)); |
| 189 |
EXPECT_NOT_EQUAL(signature_size, test_max_signature_size); |
| 190 |
EXPECT_EQUAL(signature_size, test_signature_size); |
| 191 |
EXPECT_EQUAL(signature_size, s2n_stuffer_data_available(&conn->handshake.io)); |
| 192 |
|
| 193 |
uint8_t *signature_data = s2n_stuffer_raw_read(&conn->handshake.io, test_signature_size); |
| 194 |
EXPECT_NOT_NULL(signature_data); |
| 195 |
EXPECT_BYTEARRAY_EQUAL(signature_data, test_signature_data, test_signature_size); |
| 196 |
|
| 197 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 198 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
{ |
| 203 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 204 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 205 |
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 206 |
|
| 207 |
struct s2n_config *client_config = NULL; |
| 208 |
EXPECT_NOT_NULL(client_config = s2n_config_new()); |
| 209 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, chain_and_key)); |
| 210 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(client_config, s2n_async_pkey_store_op)); |
| 211 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config)); |
| 212 |
|
| 213 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20170210")); |
| 214 |
|
| 215 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_validation_mode(client_config, S2N_ASYNC_PKEY_VALIDATION_STRICT)); |
| 216 |
|
| 217 |
|
| 218 |
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT); |
| 219 |
EXPECT_NOT_NULL(client_conn); |
| 220 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config)); |
| 221 |
EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED)); |
| 222 |
|
| 223 |
struct s2n_config *server_config = NULL; |
| 224 |
EXPECT_NOT_NULL(server_config = s2n_config_new()); |
| 225 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key)); |
| 226 |
EXPECT_OK(s2n_config_set_tls12_security_policy(server_config)); |
| 227 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(server_config)); |
| 228 |
|
| 229 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 230 |
EXPECT_NOT_NULL(server_conn); |
| 231 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config)); |
| 232 |
EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED)); |
| 233 |
|
| 234 |
|
| 235 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 236 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 237 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 238 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair)); |
| 239 |
|
| 240 |
EXPECT_SUCCESS(s2n_try_handshake_with_async_pkey_op(server_conn, client_conn, |
| 241 |
s2n_test_negotiate_with_async_pkey_op_handler)); |
| 242 |
|
| 243 |
|
| 244 |
EXPECT_TRUE(async_callback_invoked); |
| 245 |
|
| 246 |
|
| 247 |
EXPECT_TRUE(s2n_connection_client_cert_used(server_conn)); |
| 248 |
EXPECT_TRUE(s2n_connection_client_cert_used(client_conn)); |
| 249 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12); |
| 250 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12); |
| 251 |
|
| 252 |
|
| 253 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 254 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 255 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 256 |
EXPECT_SUCCESS(s2n_config_free(client_config)); |
| 257 |
EXPECT_SUCCESS(s2n_config_free(server_config)); |
| 258 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
{ |
| 263 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 264 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 265 |
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 266 |
|
| 267 |
struct s2n_config *client_config = NULL; |
| 268 |
EXPECT_NOT_NULL(client_config = s2n_config_new()); |
| 269 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, chain_and_key)); |
| 270 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(client_config, s2n_async_pkey_store_op)); |
| 271 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config)); |
| 272 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20170210")); |
| 273 |
|
| 274 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_validation_mode(client_config, S2N_ASYNC_PKEY_VALIDATION_STRICT)); |
| 275 |
|
| 276 |
|
| 277 |
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT); |
| 278 |
EXPECT_NOT_NULL(client_conn); |
| 279 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config)); |
| 280 |
EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED)); |
| 281 |
|
| 282 |
struct s2n_config *server_config = NULL; |
| 283 |
EXPECT_NOT_NULL(server_config = s2n_config_new()); |
| 284 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key)); |
| 285 |
EXPECT_OK(s2n_config_set_tls12_security_policy(server_config)); |
| 286 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(server_config)); |
| 287 |
|
| 288 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 289 |
EXPECT_NOT_NULL(server_conn); |
| 290 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config)); |
| 291 |
EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED)); |
| 292 |
|
| 293 |
|
| 294 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 295 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 296 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 297 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair)); |
| 298 |
|
| 299 |
EXPECT_SUCCESS(s2n_try_handshake_with_async_pkey_op(server_conn, client_conn, |
| 300 |
s2n_test_apply_with_invalid_signature_handler)); |
| 301 |
|
| 302 |
|
| 303 |
EXPECT_TRUE(async_callback_invoked); |
| 304 |
|
| 305 |
|
| 306 |
EXPECT_TRUE(async_sign_operation_called_s2n_client); |
| 307 |
|
| 308 |
|
| 309 |
EXPECT_TRUE(s2n_connection_client_cert_used(server_conn)); |
| 310 |
EXPECT_TRUE(s2n_connection_client_cert_used(client_conn)); |
| 311 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12); |
| 312 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12); |
| 313 |
|
| 314 |
|
| 315 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 316 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 317 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 318 |
EXPECT_SUCCESS(s2n_config_free(client_config)); |
| 319 |
EXPECT_SUCCESS(s2n_config_free(server_config)); |
| 320 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 321 |
}; |
| 322 |
|
| 323 |
END_TEST(); |
| 324 |
} |