Debian Codegraph @

crt/s2n/tests/unit/s2n_client_key_exchange_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
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_kex.h"
22 #include "utils/s2n_random.h"
23
24 DEFINE_POINTER_CLEANUP_FUNC(struct s2n_async_pkey_op *, s2n_async_pkey_op_free);
25
26 struct s2n_test_rsa_client_key_send_ctx {
27 bool override_premaster_secret_size;
28 uint32_t size;
29
30 bool invalidate_padded_premaster_secret;
31 uint32_t invalidate_index;
32
33 bool override_premaster_secret_version;
34 uint8_t version;
35 };
36
37 DEFINE_POINTER_CLEANUP_FUNC(RSA *, RSA_free);
38
39 static S2N_RESULT s2n_test_rsa_pkcs1_v15_padding_encrypt(struct s2n_connection *conn, struct s2n_blob *in,
40 struct s2n_blob *out)
41 {
42 /* RSAES-PKCS1-V1_5-ENCRYPT ((n, e), M) from https://www.rfc-editor.org/rfc/rfc8017#section-7.2.1 */
43
44 /* Input:
45 * (n, e) recipient's RSA public key (k denotes the length in
46 * octets of the modulus n)
47 */
48 DEFER_CLEANUP(RSA *n_e = EVP_PKEY_get1_RSA(conn->handshake_params.server_public_key.pkey),
49 RSA_free_pointer);
50 RESULT_ENSURE_REF(n_e);
51 const int k = RSA_size(n_e);
52 RESULT_ENSURE_GT(k, 0);
53
54 /* M message to be encrypted, an octet string of length
55 * mLen, where mLen <= k - 11
56 */
57 struct s2n_blob *M = in;
58 uint32_t mLen = M->size;
59
60 /* Output:
61 * C ciphertext, an octet string of length k
62 */
63 struct s2n_blob *C = out;
64 RESULT_ENSURE_GTE(C->size, k);
65
66 /* Steps:
67 * 1. Length checking: If mLen > k - 11, output "message too long"
68 * and stop.
69 */
70 RESULT_ENSURE_LTE(mLen, k - 11);
71
72 /* 2. EME-PKCS1-v1_5 encoding:
73 * a. Generate an octet string PS of length k - mLen - 3
74 * consisting of pseudo-randomly generated nonzero octets.
75 * The length of PS will be at least eight octets.
76 * b. Concatenate PS, the message M, and other padding to form
77 * an encoded message EM of length k octets as
78 * EM = 0x00 || 0x02 || PS || 0x00 || M.
79 */
80 uint8_t EM_data[4096] = { 0 };
81 struct s2n_blob EM_blob = { 0 };
82 RESULT_GUARD_POSIX(s2n_blob_init(&EM_blob, EM_data, sizeof(EM_data)));
83 struct s2n_stuffer EM_stuffer = { 0 };
84 RESULT_GUARD_POSIX(s2n_stuffer_init(&EM_stuffer, &EM_blob));
85
86 /* 0x00 || 0x02 */
87 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&EM_stuffer, 0x00));
88 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&EM_stuffer, 0x02));
89
90 /* || PS */
91 struct s2n_blob PS_blob = { 0 };
92 uint32_t PS_len = k - mLen - 3;
93 uint8_t *PS_data = s2n_stuffer_raw_write(&EM_stuffer, PS_len);
94 RESULT_ENSURE_REF(PS_data);
95 RESULT_GUARD_POSIX(s2n_blob_init(&PS_blob, PS_data, PS_len));
96 RESULT_GUARD(s2n_get_public_random_data(&PS_blob));
97
98 /* Ensure random bytes are nonzero */
99 for (size_t i = 0; i < PS_len; i++) {
100 PS_data[i] = (PS_data[i] % (UINT8_MAX - 1)) + 1;
101 }
102
103 /* || 0x00 */
104 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&EM_stuffer, 0x00));
105
106 /* || M */
107 RESULT_GUARD_POSIX(s2n_stuffer_write(&EM_stuffer, M));
108
109 uint8_t *EM = s2n_stuffer_raw_read(&EM_stuffer, k);
110 RESULT_ENSURE_REF(EM);
111 RESULT_ENSURE_EQ(s2n_stuffer_data_available(&EM_stuffer), 0);
112
113 struct s2n_test_rsa_client_key_send_ctx *ctx = s2n_connection_get_ctx(conn);
114 RESULT_ENSURE_REF(ctx);
115 if (ctx->invalidate_padded_premaster_secret) {
116 RESULT_ENSURE_LT(ctx->invalidate_index, k);
117 if (ctx->invalidate_index > 1 && ctx->invalidate_index < 2 + PS_len) {
118 /* PS bytes must not be 0. */
119 EM[ctx->invalidate_index] = 0;
120 } else {
121 /* Otherwise bytes can be invalidated by modifying them arbitrarily. */
122 EM[ctx->invalidate_index] += 1;
123 }
124 }
125
126 /* 3. RSA encryption:
127 * b. Apply the RSAEP encryption primitive (Section 5.1.1) to
128 * the RSA public key (n, e) and the message representative m
129 * to produce an integer ciphertext representative c:
130 * c = RSAEP ((n, e), m).
131 */
132 int r = RSA_public_encrypt(k, (unsigned char *) EM, (unsigned char *) C->data,
133 n_e, RSA_NO_PADDING);
134 RESULT_ENSURE((int64_t) r == (int64_t) C->size, S2N_ERR_SIZE_MISMATCH);
135
136 return S2N_RESULT_OK;
137 }
138
139 /* More general version of s2n_rsa_client_key_send() that allows the premaster secret to be
140 * invalidated by configuring a s2n_test_rsa_client_key_send_ctx on the connection.
141 */
142 static int s2n_test_rsa_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key)
143 {
144 POSIX_ENSURE_REF(conn);
145 POSIX_ENSURE_REF(shared_key);
146
147 struct s2n_test_rsa_client_key_send_ctx *ctx = s2n_connection_get_ctx(conn);
148 POSIX_ENSURE_REF(ctx);
149
150 uint8_t client_hello_version = s2n_connection_get_client_hello_version(conn);
151 if (ctx->override_premaster_secret_version) {
152 client_hello_version = ctx->version;
153 }
154 uint8_t client_hello_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
155 client_hello_protocol_version[0] = client_hello_version / 10;
156 client_hello_protocol_version[1] = client_hello_version % 10;
157
158 shared_key->data = conn->secrets.version.tls12.rsa_premaster_secret;
159 uint32_t secret_size = S2N_TLS_SECRET_LEN;
160 if (ctx->override_premaster_secret_size) {
161 secret_size = ctx->size;
162 }
163 POSIX_ENSURE_LTE(secret_size, sizeof(conn->secrets.version.tls12.rsa_premaster_secret));
164 shared_key->size = secret_size;
165
166 POSIX_GUARD_RESULT(s2n_get_private_random_data(shared_key));
167
168 /* The first two bytes of the premaster secret contain the client hello version. */
169 POSIX_CHECKED_MEMCPY(conn->secrets.version.tls12.rsa_premaster_secret, client_hello_protocol_version,
170 S2N_TLS_PROTOCOL_VERSION_LEN);
171
172 uint32_t encrypted_size = 0;
173 POSIX_GUARD_RESULT(s2n_pkey_size(&conn->handshake_params.server_public_key, &encrypted_size));
174 POSIX_ENSURE_LTE(encrypted_size, 0xffff);
175
176 /* Write the length. */
177 POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, encrypted_size));
178
179 struct s2n_blob encrypted = { 0 };
180 encrypted.data = s2n_stuffer_raw_write(&conn->handshake.io, encrypted_size);
181 encrypted.size = encrypted_size;
182 POSIX_ENSURE_REF(encrypted.data);
183
184 POSIX_GUARD_RESULT(s2n_test_rsa_pkcs1_v15_padding_encrypt(conn, shared_key, &encrypted));
185
186 POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.server_public_key));
187
188 return S2N_SUCCESS;
189 }
190
191 struct s2n_test_async_pkey_cb_ctx {
192 uint32_t async_invoked_count;
193 uint32_t offload_invoked_count;
194 };
195
196 static int s2n_test_async_pkey_decrypt_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op_in)
197 {
198 POSIX_ENSURE_REF(conn);
199 POSIX_ENSURE_REF(op_in);
200
201 DEFER_CLEANUP(struct s2n_async_pkey_op *op = op_in, s2n_async_pkey_op_free_pointer);
202
203 struct s2n_test_async_pkey_cb_ctx *ctx = s2n_connection_get_ctx(conn);
204 POSIX_ENSURE_REF(ctx);
205 ctx->async_invoked_count += 1;
206
207 s2n_async_pkey_op_type op_type = 0;
208 EXPECT_SUCCESS(s2n_async_pkey_op_get_op_type(op, &op_type));
209 EXPECT_EQUAL(op_type, S2N_ASYNC_DECRYPT);
210
211 struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(conn);
212 EXPECT_NOT_NULL(chain_and_key);
213 s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key);
214 EXPECT_NOT_NULL(pkey);
215
216 EXPECT_SUCCESS(s2n_async_pkey_op_perform(op, pkey));
217 EXPECT_SUCCESS(s2n_async_pkey_op_apply(op, conn));
218
219 return S2N_SUCCESS;
220 }
221
222 static int s2n_test_offload_pkey_decrypt_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op_in)
223 {
224 POSIX_ENSURE_REF(conn);
225 POSIX_ENSURE_REF(op_in);
226
227 DEFER_CLEANUP(struct s2n_async_pkey_op *op = op_in, s2n_async_pkey_op_free_pointer);
228
229 struct s2n_test_async_pkey_cb_ctx *ctx = s2n_connection_get_ctx(conn);
230 POSIX_ENSURE_REF(ctx);
231 ctx->offload_invoked_count += 1;
232
233 s2n_async_pkey_op_type op_type = 0;
234 EXPECT_SUCCESS(s2n_async_pkey_op_get_op_type(op, &op_type));
235 EXPECT_EQUAL(op_type, S2N_ASYNC_DECRYPT);
236
237 struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(conn);
238 EXPECT_NOT_NULL(chain_and_key);
239 s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key);
240 EXPECT_NOT_NULL(pkey);
241
242 uint8_t input_data[4096] = { 0 };
243 struct s2n_blob input_blob = { 0 };
244 EXPECT_SUCCESS(s2n_blob_init(&input_blob, input_data, sizeof(input_data)));
245
246 uint32_t input_size = 0;
247 EXPECT_SUCCESS(s2n_async_pkey_op_get_input_size(op, &input_size));
248 EXPECT_TRUE(input_size <= input_blob.size);
249 EXPECT_SUCCESS(s2n_async_pkey_op_get_input(op, input_data, sizeof(input_data)));
250 input_blob.size = input_size;
251
252 uint8_t output_data[S2N_TLS_SECRET_LEN] = { 0 };
253 struct s2n_blob output_blob = { 0 };
254 EXPECT_SUCCESS(s2n_blob_init(&output_blob, output_data, sizeof(output_data)));
255
256 EXPECT_SUCCESS(s2n_pkey_decrypt(pkey, &input_blob, &output_blob));
257 EXPECT_SUCCESS(s2n_async_pkey_op_set_output(op, output_blob.data, output_blob.size));
258 EXPECT_SUCCESS(s2n_async_pkey_op_apply(op, conn));
259
260 return S2N_SUCCESS;
261 }
262
263 typedef enum {
264 S2N_PKEY_TEST_DEFAULT,
265 S2N_PKEY_TEST_ASYNC,
266 S2N_PKEY_TEST_OFFLOAD,
267 S2N_PKEY_TEST_COUNT,
268 } s2n_pkey_test_mode;
269
270 static S2N_RESULT s2n_validate_test_async_pkey_ctx(struct s2n_test_async_pkey_cb_ctx *ctx, s2n_pkey_test_mode pkey_mode)
271 {
272 RESULT_ENSURE_REF(ctx);
273
274 switch (pkey_mode) {
275 case S2N_PKEY_TEST_DEFAULT:
276 RESULT_ENSURE_EQ(ctx->async_invoked_count, 0);
277 RESULT_ENSURE_EQ(ctx->offload_invoked_count, 0);
278 break;
279 case S2N_PKEY_TEST_ASYNC:
280 RESULT_ENSURE_EQ(ctx->async_invoked_count, 1);
281 RESULT_ENSURE_EQ(ctx->offload_invoked_count, 0);
282 break;
283 case S2N_PKEY_TEST_OFFLOAD:
284 RESULT_ENSURE_EQ(ctx->async_invoked_count, 0);
285 RESULT_ENSURE_EQ(ctx->offload_invoked_count, 1);
286 break;
287 default:
288 RESULT_BAIL(S2N_ERR_INVALID_ARGUMENT);
289 }
290
291 return S2N_RESULT_OK;
292 }
293
294 int main(int argc, char **argv)
295 {
296 BEGIN_TEST();
297
298 /* Test: same error + location for all Bleichenbacher attack cases.
299 *
300 * This doesn't prove safety, since Bleichenbacher is a timing side-channel attack. But if this
301 * test DOES fail, we likely have an issue.
302 */
303 for (size_t pkey_test_mode = 0; pkey_test_mode < S2N_PKEY_TEST_COUNT; pkey_test_mode++) {
304 /* We must use an RSA cert so that we can test RSA kex */
305 DEFER_CLEANUP(struct s2n_cert_chain_and_key *rsa_cert_chain = NULL, s2n_cert_chain_and_key_ptr_free);
306 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&rsa_cert_chain,
307 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
308
309 struct s2n_pkey *rsa_private_key = s2n_cert_chain_and_key_get_private_key(rsa_cert_chain);
310 EXPECT_NOT_NULL(rsa_private_key);
311 uint32_t key_size = 0;
312 EXPECT_OK(s2n_pkey_size(rsa_private_key, &key_size));
313 EXPECT_TRUE(key_size > 0);
314
315 struct s2n_kex rsa_kex = s2n_rsa;
316 rsa_kex.client_key_send = s2n_test_rsa_client_key_send;
317
318 struct s2n_cipher_suite rsa_kex_cipher_suite = s2n_rsa_with_aes_128_gcm_sha256;
319 rsa_kex_cipher_suite.key_exchange_alg = &rsa_kex;
320
321 struct s2n_cipher_suite *rsa_kex_cipher_suites[1] = { &rsa_kex_cipher_suite };
322 struct s2n_cipher_preferences rsa_kex_cipher_pref = {
323 .suites = rsa_kex_cipher_suites,
324 .count = 1,
325 };
326 struct s2n_security_policy test_rsa_policy = security_policy_test_all;
327 test_rsa_policy.cipher_preferences = &rsa_kex_cipher_pref;
328
329 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
330 EXPECT_NOT_NULL(client_config);
331 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
332 client_config->security_policy = &test_rsa_policy;
333
334 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
335 EXPECT_NOT_NULL(server_config);
336 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_cert_chain));
337 server_config->security_policy = &test_rsa_policy;
338
339 if (pkey_test_mode == S2N_PKEY_TEST_ASYNC) {
340 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, s2n_test_async_pkey_decrypt_callback));
341 } else if (pkey_test_mode == S2N_PKEY_TEST_OFFLOAD) {
342 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, s2n_test_offload_pkey_decrypt_callback));
343 }
344
345 /* Sanity check: ensure s2n_test_rsa_client_key_send sends a valid premaster secret by default */
346 for (size_t i = 0; i < 100; i++) {
347 struct s2n_test_rsa_client_key_send_ctx rsa_send_ctx = { 0 };
348
349 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
350 s2n_connection_ptr_free);
351 EXPECT_NOT_NULL(client);
352 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
353 EXPECT_SUCCESS(s2n_connection_set_ctx(client, &rsa_send_ctx));
354
355 struct s2n_test_async_pkey_cb_ctx async_pkey_ctx = { 0 };
356
357 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
358 s2n_connection_ptr_free);
359 EXPECT_NOT_NULL(server);
360 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
361 EXPECT_SUCCESS(s2n_connection_set_ctx(server, &async_pkey_ctx));
362 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
363
364 DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
365 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
366 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
367
368 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
369
370 EXPECT_OK(s2n_validate_test_async_pkey_ctx(&async_pkey_ctx, pkey_test_mode));
371 }
372
373 /* All the tests for the Bleichenbacher attack cases should result in the same error from
374 * s2n_aead_cipher_aes_gcm.c. However, the line number of the error can change depending on
375 * how s2n-tls is built. s2n_sterror_source() is called from the first test case to
376 * determine the expected error source for the remaining tests.
377 */
378 int expected_error = S2N_ERR_DECRYPT;
379 const char *expected_source_str = NULL;
380
381 /* Test: client sends invalid premaster secret */
382 {
383 struct s2n_test_rsa_client_key_send_ctx ctx = {
384 .invalidate_padded_premaster_secret = true,
385 .invalidate_index = key_size - 1,
386 };
387
388 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
389 s2n_connection_ptr_free);
390 EXPECT_NOT_NULL(client);
391 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
392 EXPECT_SUCCESS(s2n_connection_set_ctx(client, &ctx));
393
394 struct s2n_test_async_pkey_cb_ctx async_pkey_ctx = { 0 };
395
396 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
397 s2n_connection_ptr_free);
398 EXPECT_NOT_NULL(server);
399 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
400 EXPECT_SUCCESS(s2n_connection_set_ctx(server, &async_pkey_ctx));
401 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
402
403 DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
404 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
405 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
406
407 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_negotiate_test_server_and_client(server, client), expected_error);
408 expected_source_str = s2n_strerror_source(s2n_errno);
409
410 /* Ensure that the error came from s2n_aead_cipher_aes_gcm.c. */
411 EXPECT_NOT_NULL(strstr(expected_source_str, "s2n_aead_cipher_aes_gcm.c"));
412
413 EXPECT_OK(s2n_validate_test_async_pkey_ctx(&async_pkey_ctx, pkey_test_mode));
414 }
415
416 /* Test: wrong version */
417 for (uint8_t version = 0; version < S2N_TLS13 + 10; version++) {
418 struct s2n_test_rsa_client_key_send_ctx rsa_send_ctx = {
419 .override_premaster_secret_version = true,
420 .version = version,
421 };
422
423 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
424 s2n_connection_ptr_free);
425 EXPECT_NOT_NULL(client);
426 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
427 EXPECT_SUCCESS(s2n_connection_set_ctx(client, &rsa_send_ctx));
428
429 struct s2n_test_async_pkey_cb_ctx async_pkey_ctx = { 0 };
430
431 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
432 s2n_connection_ptr_free);
433 EXPECT_NOT_NULL(server);
434 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
435 EXPECT_SUCCESS(s2n_connection_set_ctx(server, &async_pkey_ctx));
436 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
437
438 DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
439 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
440 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
441
442 if (version == S2N_TLS12) {
443 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
444 } else {
445 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_negotiate_test_server_and_client(server, client),
446 expected_error);
447 EXPECT_STRING_EQUAL(s2n_strerror_source(s2n_errno), expected_source_str);
448 }
449
450 EXPECT_OK(s2n_validate_test_async_pkey_ctx(&async_pkey_ctx, pkey_test_mode));
451 }
452
453 /* Test: wrong plaintext size */
454 for (uint32_t size = 0; size <= S2N_TLS_SECRET_LEN; size++) {
455 struct s2n_test_rsa_client_key_send_ctx ctx = {
456 .override_premaster_secret_size = true,
457 .size = size,
458 };
459
460 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
461 s2n_connection_ptr_free);
462 EXPECT_NOT_NULL(client);
463 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
464 EXPECT_SUCCESS(s2n_connection_set_ctx(client, &ctx));
465
466 struct s2n_test_async_pkey_cb_ctx async_pkey_ctx = { 0 };
467
468 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
469 s2n_connection_ptr_free);
470 EXPECT_NOT_NULL(server);
471 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
472 EXPECT_SUCCESS(s2n_connection_set_ctx(server, &async_pkey_ctx));
473 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
474
475 DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
476 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
477 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
478
479 if (size == S2N_TLS_SECRET_LEN) {
480 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
481 } else {
482 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_negotiate_test_server_and_client(server, client),
483 expected_error);
484 EXPECT_STRING_EQUAL(s2n_strerror_source(s2n_errno), expected_source_str);
485 }
486
487 EXPECT_OK(s2n_validate_test_async_pkey_ctx(&async_pkey_ctx, pkey_test_mode));
488 }
489
490 /* Test: wrong padding
491 *
492 * Each of the padding bytes are invalidated before encrypting, and the resulting
493 * ciphertext is sent to the server. PKCS1 v1.5 padding starts at 0 and ends before the
494 * plaintext.
495 */
496 for (uint32_t invalidate_index = 0; invalidate_index < key_size - S2N_TLS_SECRET_LEN; invalidate_index++) {
497 struct s2n_test_rsa_client_key_send_ctx ctx = {
498 .invalidate_padded_premaster_secret = true,
499 .invalidate_index = invalidate_index,
500 };
501
502 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
503 s2n_connection_ptr_free);
504 EXPECT_NOT_NULL(client);
505 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
506 EXPECT_SUCCESS(s2n_connection_set_ctx(client, &ctx));
507
508 struct s2n_test_async_pkey_cb_ctx async_pkey_ctx = { 0 };
509
510 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
511 s2n_connection_ptr_free);
512 EXPECT_NOT_NULL(server);
513 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
514 EXPECT_SUCCESS(s2n_connection_set_ctx(server, &async_pkey_ctx));
515 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
516
517 DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
518 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
519 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
520
521 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_negotiate_test_server_and_client(server, client), expected_error);
522 EXPECT_STRING_EQUAL(s2n_strerror_source(s2n_errno), expected_source_str);
523
524 EXPECT_OK(s2n_validate_test_async_pkey_ctx(&async_pkey_ctx, pkey_test_mode));
525 }
526 }
527
528 END_TEST();
529 }