| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_fips.h" |
| 17 |
#include "crypto/s2n_rsa_pss.h" |
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "s2n_test.h" |
| 20 |
#include "testlib/s2n_testlib.h" |
| 21 |
#include "tls/s2n_cipher_suites.h" |
| 22 |
#include "tls/s2n_tls.h" |
| 23 |
#include "tls/s2n_tls13.h" |
| 24 |
#include "tls/s2n_tls13_certificate_verify.c" |
| 25 |
|
| 26 |
uint8_t hello[] = "Hello, World!\n"; |
| 27 |
uint8_t goodbye[] = "Goodbye, World!\n"; |
| 28 |
|
| 29 |
struct s2n_tls13_cert_verify_test { |
| 30 |
const char *const cert_file; |
| 31 |
const char *const key_file; |
| 32 |
const struct s2n_signature_scheme *sig_scheme; |
| 33 |
const struct s2n_signature_scheme *with_wrong_hash; |
| 34 |
}; |
| 35 |
|
| 36 |
const struct s2n_tls13_cert_verify_test test_cases[] = { |
| 37 |
{ |
| 38 |
.cert_file = S2N_ECDSA_P256_PKCS1_CERT_CHAIN, |
| 39 |
.key_file = S2N_ECDSA_P256_PKCS1_KEY, |
| 40 |
.sig_scheme = &s2n_ecdsa_sha256, |
| 41 |
.with_wrong_hash = &s2n_ecdsa_sha384, |
| 42 |
}, |
| 43 |
#if RSA_PSS_CERTS_SUPPORTED |
| 44 |
{ |
| 45 |
.cert_file = S2N_RSA_PSS_2048_SHA256_LEAF_CERT, |
| 46 |
.key_file = S2N_RSA_PSS_2048_SHA256_LEAF_KEY, |
| 47 |
.sig_scheme = &s2n_rsa_pss_pss_sha256, |
| 48 |
.with_wrong_hash = &s2n_rsa_pss_pss_sha384, |
| 49 |
}, |
| 50 |
#endif |
| 51 |
}; |
| 52 |
|
| 53 |
S2N_RESULT s2n_cert_verify_connection_setup_and_send( |
| 54 |
struct s2n_connection *sending_conn, struct s2n_connection *verifying_conn, |
| 55 |
struct s2n_config *config, struct s2n_cert_chain_and_key *cert_chain, |
| 56 |
struct s2n_signature_scheme *sig_scheme, struct s2n_blob *cert) |
| 57 |
{ |
| 58 |
sending_conn->handshake_params.our_chain_and_key = cert_chain; |
| 59 |
sending_conn->handshake_params.server_cert_sig_scheme = sig_scheme; |
| 60 |
sending_conn->handshake_params.client_cert_sig_scheme = sig_scheme; |
| 61 |
sending_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 62 |
sending_conn->actual_protocol_version = S2N_TLS13; |
| 63 |
EXPECT_SUCCESS(s2n_connection_set_config(sending_conn, config)); |
| 64 |
|
| 65 |
verifying_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 66 |
verifying_conn->actual_protocol_version = S2N_TLS13; |
| 67 |
EXPECT_SUCCESS(s2n_connection_set_config(verifying_conn, config)); |
| 68 |
|
| 69 |
|
| 70 |
s2n_pkey_type pkey_type = { 0 }; |
| 71 |
if (verifying_conn->mode == S2N_CLIENT) { |
| 72 |
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&verifying_conn->handshake_params.server_public_key, &pkey_type, cert)); |
| 73 |
EXPECT_SUCCESS(s2n_pkey_match(&verifying_conn->handshake_params.server_public_key, sending_conn->handshake_params.our_chain_and_key->private_key)); |
| 74 |
} else { |
| 75 |
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&verifying_conn->handshake_params.client_public_key, &pkey_type, cert)); |
| 76 |
EXPECT_SUCCESS(s2n_pkey_match(&verifying_conn->handshake_params.client_public_key, sending_conn->handshake_params.our_chain_and_key->private_key)); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
EXPECT_SUCCESS(s2n_hash_init(&sending_conn->handshake.hashes->sha256, S2N_HASH_SHA256)); |
| 81 |
EXPECT_SUCCESS(s2n_hash_update(&sending_conn->handshake.hashes->sha256, hello, sizeof(hello))); |
| 82 |
EXPECT_SUCCESS(s2n_hash_init(&verifying_conn->handshake.hashes->sha256, S2N_HASH_SHA256)); |
| 83 |
EXPECT_SUCCESS(s2n_hash_update(&verifying_conn->handshake.hashes->sha256, hello, sizeof(hello))); |
| 84 |
|
| 85 |
|
| 86 |
EXPECT_SUCCESS(s2n_tls13_cert_verify_send(sending_conn)); |
| 87 |
EXPECT_SUCCESS(s2n_stuffer_copy(&sending_conn->handshake.io, &verifying_conn->handshake.io, |
| 88 |
s2n_stuffer_data_available(&sending_conn->handshake.io))); |
| 89 |
|
| 90 |
return S2N_RESULT_OK; |
| 91 |
} |
| 92 |
|
| 93 |
int run_tests(const struct s2n_tls13_cert_verify_test *test_case, s2n_mode verifier_mode) |
| 94 |
{ |
| 95 |
const char *cert_file = test_case->cert_file; |
| 96 |
const char *key_file = test_case->key_file; |
| 97 |
struct s2n_signature_scheme sig_scheme = *test_case->sig_scheme; |
| 98 |
|
| 99 |
char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 }; |
| 100 |
char private_key_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 }; |
| 101 |
EXPECT_SUCCESS(s2n_read_test_pem(cert_file, &cert_chain_pem[0], S2N_MAX_TEST_PEM_SIZE)); |
| 102 |
EXPECT_SUCCESS(s2n_read_test_pem(key_file, &private_key_pem[0], S2N_MAX_TEST_PEM_SIZE)); |
| 103 |
|
| 104 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *cert_chain = s2n_cert_chain_and_key_new(), |
| 105 |
s2n_cert_chain_and_key_ptr_free); |
| 106 |
EXPECT_NOT_NULL(cert_chain); |
| 107 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(cert_chain, cert_chain_pem, private_key_pem)); |
| 108 |
|
| 109 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 110 |
EXPECT_NOT_NULL(config); |
| 111 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20200207")); |
| 112 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert_chain)); |
| 113 |
|
| 114 |
|
| 115 |
DEFER_CLEANUP(struct s2n_stuffer certificate_in = { 0 }, s2n_stuffer_free); |
| 116 |
DEFER_CLEANUP(struct s2n_stuffer certificate_out = { 0 }, s2n_stuffer_free); |
| 117 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&certificate_in, S2N_MAX_TEST_PEM_SIZE)); |
| 118 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&certificate_out, S2N_MAX_TEST_PEM_SIZE)); |
| 119 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&certificate_in, (uint8_t *) cert_chain_pem, sizeof(cert_chain_pem))); |
| 120 |
EXPECT_SUCCESS(s2n_stuffer_certificate_from_pem(&certificate_in, &certificate_out)); |
| 121 |
|
| 122 |
uint32_t available_size = s2n_stuffer_data_available(&certificate_out); |
| 123 |
struct s2n_blob cert = { 0 }; |
| 124 |
EXPECT_SUCCESS(s2n_blob_init(&cert, s2n_stuffer_raw_read(&certificate_out, available_size), available_size)); |
| 125 |
|
| 126 |
|
| 127 |
{ |
| 128 |
DEFER_CLEANUP(struct s2n_connection *sending_conn = s2n_connection_new(S2N_PEER_MODE(verifier_mode)), |
| 129 |
s2n_connection_ptr_free); |
| 130 |
EXPECT_NOT_NULL(sending_conn); |
| 131 |
|
| 132 |
DEFER_CLEANUP(struct s2n_connection *verifying_conn = s2n_connection_new(verifier_mode), |
| 133 |
s2n_connection_ptr_free); |
| 134 |
EXPECT_NOT_NULL(verifying_conn); |
| 135 |
|
| 136 |
EXPECT_OK(s2n_cert_verify_connection_setup_and_send(sending_conn, verifying_conn, config, cert_chain, &sig_scheme, &cert)); |
| 137 |
|
| 138 |
|
| 139 |
EXPECT_SUCCESS(s2n_tls13_cert_verify_recv(verifying_conn)); |
| 140 |
|
| 141 |
|
| 142 |
EXPECT_SUCCESS(s2n_tls13_cert_verify_send(sending_conn)); |
| 143 |
EXPECT_SUCCESS(s2n_stuffer_copy(&sending_conn->handshake.io, &verifying_conn->handshake.io, |
| 144 |
s2n_stuffer_data_available(&sending_conn->handshake.io))); |
| 145 |
EXPECT_SUCCESS(s2n_tls13_cert_verify_recv(verifying_conn)); |
| 146 |
|
| 147 |
|
| 148 |
verifying_conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384; |
| 149 |
EXPECT_SUCCESS(s2n_tls13_cert_verify_send(sending_conn)); |
| 150 |
EXPECT_SUCCESS(s2n_stuffer_copy(&sending_conn->handshake.io, &verifying_conn->handshake.io, |
| 151 |
s2n_stuffer_data_available(&sending_conn->handshake.io))); |
| 152 |
|
| 153 |
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(verifying_conn), S2N_ERR_VERIFY_SIGNATURE); |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
{ |
| 158 |
DEFER_CLEANUP(struct s2n_connection *sending_conn = s2n_connection_new(S2N_PEER_MODE(verifier_mode)), |
| 159 |
s2n_connection_ptr_free); |
| 160 |
EXPECT_NOT_NULL(sending_conn); |
| 161 |
|
| 162 |
DEFER_CLEANUP(struct s2n_connection *verifying_conn = s2n_connection_new(verifier_mode), |
| 163 |
s2n_connection_ptr_free); |
| 164 |
EXPECT_NOT_NULL(verifying_conn); |
| 165 |
|
| 166 |
EXPECT_OK(s2n_cert_verify_connection_setup_and_send(sending_conn, verifying_conn, config, cert_chain, &sig_scheme, &cert)); |
| 167 |
|
| 168 |
|
| 169 |
EXPECT_SUCCESS(s2n_hash_update(&verifying_conn->handshake.hashes->sha256, goodbye, sizeof(goodbye))); |
| 170 |
|
| 171 |
uint64_t verifying_bytes = 0; |
| 172 |
uint64_t sending_bytes = 0; |
| 173 |
EXPECT_SUCCESS(s2n_hash_get_currently_in_hash_total(&verifying_conn->handshake.hashes->sha256, &verifying_bytes)); |
| 174 |
EXPECT_SUCCESS(s2n_hash_get_currently_in_hash_total(&sending_conn->handshake.hashes->sha256, &sending_bytes)); |
| 175 |
EXPECT_NOT_EQUAL(sending_bytes, verifying_bytes); |
| 176 |
|
| 177 |
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(verifying_conn), S2N_ERR_VERIFY_SIGNATURE); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
{ |
| 182 |
DEFER_CLEANUP(struct s2n_connection *sending_conn = s2n_connection_new(S2N_PEER_MODE(verifier_mode)), |
| 183 |
s2n_connection_ptr_free); |
| 184 |
EXPECT_NOT_NULL(sending_conn); |
| 185 |
|
| 186 |
DEFER_CLEANUP(struct s2n_connection *verifying_conn = s2n_connection_new(verifier_mode), |
| 187 |
s2n_connection_ptr_free); |
| 188 |
EXPECT_NOT_NULL(verifying_conn); |
| 189 |
|
| 190 |
EXPECT_OK(s2n_cert_verify_connection_setup_and_send(sending_conn, verifying_conn, config, cert_chain, &sig_scheme, &cert)); |
| 191 |
|
| 192 |
|
| 193 |
EXPECT_TRUE(10 < s2n_stuffer_data_available(&verifying_conn->handshake.io)); |
| 194 |
verifying_conn->handshake.io.blob.data[10] ^= 1; |
| 195 |
|
| 196 |
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(verifying_conn), S2N_ERR_VERIFY_SIGNATURE); |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
{ |
| 201 |
DEFER_CLEANUP(struct s2n_connection *sending_conn = s2n_connection_new(S2N_PEER_MODE(verifier_mode)), |
| 202 |
s2n_connection_ptr_free); |
| 203 |
EXPECT_NOT_NULL(sending_conn); |
| 204 |
|
| 205 |
DEFER_CLEANUP(struct s2n_connection *verifying_conn = s2n_connection_new(verifier_mode), |
| 206 |
s2n_connection_ptr_free); |
| 207 |
EXPECT_NOT_NULL(verifying_conn); |
| 208 |
|
| 209 |
EXPECT_OK(s2n_cert_verify_connection_setup_and_send(sending_conn, verifying_conn, config, cert_chain, &sig_scheme, &cert)); |
| 210 |
|
| 211 |
|
| 212 |
struct s2n_stuffer rereader = verifying_conn->handshake.io; |
| 213 |
EXPECT_SUCCESS(s2n_stuffer_rewrite(&rereader)); |
| 214 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&rereader, test_case->with_wrong_hash->iana_value)); |
| 215 |
|
| 216 |
|
| 217 |
if (test_case->sig_scheme->sig_alg == S2N_SIGNATURE_ECDSA) { |
| 218 |
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(verifying_conn), S2N_ERR_INVALID_SIGNATURE_ALGORITHM); |
| 219 |
} else { |
| 220 |
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(verifying_conn), S2N_ERR_VERIFY_SIGNATURE); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
int main(int argc, char **argv) |
| 228 |
{ |
| 229 |
BEGIN_TEST(); |
| 230 |
|
| 231 |
EXPECT_SUCCESS(s2n_enable_tls13_in_test()); |
| 232 |
|
| 233 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 234 |
|
| 235 |
run_tests(&test_cases[i], S2N_CLIENT); |
| 236 |
|
| 237 |
|
| 238 |
run_tests(&test_cases[i], S2N_SERVER); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
if (s2n_is_tls13_fully_supported()) { |
| 245 |
struct s2n_tls13_cert_verify_test test_server_parameters[] = { |
| 246 |
{ |
| 247 |
.cert_file = S2N_RSA_2048_PKCS1_CERT_CHAIN, |
| 248 |
.key_file = S2N_RSA_2048_PKCS1_KEY, |
| 249 |
.sig_scheme = &s2n_rsa_pss_rsae_sha256, |
| 250 |
}, |
| 251 |
{ |
| 252 |
.cert_file = S2N_RSA_PSS_2048_SHA256_LEAF_CERT, |
| 253 |
.key_file = S2N_RSA_PSS_2048_SHA256_LEAF_KEY, |
| 254 |
.sig_scheme = &s2n_rsa_pss_pss_sha256, |
| 255 |
}, |
| 256 |
{ |
| 257 |
.cert_file = S2N_ECDSA_P256_PKCS1_CERT_CHAIN, |
| 258 |
.key_file = S2N_ECDSA_P256_PKCS1_KEY, |
| 259 |
.sig_scheme = &s2n_ecdsa_sha256, |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
const struct s2n_signature_scheme *test_client_sig_schemes[] = { |
| 264 |
&s2n_rsa_pss_rsae_sha256, |
| 265 |
&s2n_rsa_pss_pss_sha256, |
| 266 |
&s2n_ecdsa_sha256, |
| 267 |
}; |
| 268 |
|
| 269 |
for (size_t param_idx = 0; param_idx < s2n_array_len(test_server_parameters); param_idx++) { |
| 270 |
struct s2n_tls13_cert_verify_test server_parameters = test_server_parameters[param_idx]; |
| 271 |
|
| 272 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *cert_chain = NULL, s2n_cert_chain_and_key_ptr_free); |
| 273 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&cert_chain, server_parameters.cert_file, |
| 274 |
server_parameters.key_file)); |
| 275 |
|
| 276 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 277 |
EXPECT_NOT_NULL(config); |
| 278 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all_tls13")); |
| 279 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(config)); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert_chain)); |
| 287 |
|
| 288 |
for (size_t sig_idx = 0; sig_idx < s2n_array_len(test_client_sig_schemes); sig_idx++) { |
| 289 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), |
| 290 |
s2n_connection_ptr_free); |
| 291 |
EXPECT_NOT_NULL(server_conn); |
| 292 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING)); |
| 293 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 294 |
|
| 295 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), |
| 296 |
s2n_connection_ptr_free); |
| 297 |
EXPECT_NOT_NULL(client_conn); |
| 298 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING)); |
| 299 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
const struct s2n_signature_scheme *client_advertised_sig_scheme = test_client_sig_schemes[sig_idx]; |
| 306 |
struct s2n_signature_preferences test_sig_preferences = { |
| 307 |
.count = 1, |
| 308 |
.signature_schemes = &client_advertised_sig_scheme, |
| 309 |
}; |
| 310 |
struct s2n_security_policy client_policy = security_policy_test_all_tls13; |
| 311 |
client_policy.signature_preferences = &test_sig_preferences; |
| 312 |
client_conn->security_policy_override = &client_policy; |
| 313 |
|
| 314 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 315 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 316 |
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair)); |
| 317 |
|
| 318 |
|
| 319 |
EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, |
| 320 |
SERVER_CERT_VERIFY)); |
| 321 |
EXPECT_SUCCESS(s2n_stuffer_rewrite(&server_conn->handshake.io)); |
| 322 |
EXPECT_SUCCESS(s2n_tls13_cert_verify_send(server_conn)); |
| 323 |
|
| 324 |
|
| 325 |
EXPECT_EQUAL(server_conn->handshake_params.server_cert_sig_scheme, server_parameters.sig_scheme); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
struct s2n_stuffer cert_verify_stuffer = server_conn->handshake.io; |
| 333 |
EXPECT_SUCCESS(s2n_stuffer_rewrite(&cert_verify_stuffer)); |
| 334 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&cert_verify_stuffer, |
| 335 |
client_advertised_sig_scheme->iana_value)); |
| 336 |
|
| 337 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io)); |
| 338 |
EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, |
| 339 |
s2n_stuffer_data_available(&server_conn->handshake.io))); |
| 340 |
|
| 341 |
int ret = s2n_tls13_cert_verify_recv(client_conn); |
| 342 |
|
| 343 |
if (client_advertised_sig_scheme == server_parameters.sig_scheme) { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
EXPECT_SUCCESS(ret); |
| 348 |
} else { |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_INVALID_SIGNATURE_ALGORITHM); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
END_TEST(); |
| 360 |
} |