Debian Codegraph @

crt/s2n/tests/unit/s2n_async_pkey_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 "tls/s2n_async_pkey.h"
17
18 #include "api/s2n.h"
19 #include "error/s2n_errno.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tls/s2n_cipher_suites.h"
23 #include "tls/s2n_connection.h"
24 #include "tls/s2n_security_policies.h"
25 #include "utils/s2n_safety.h"
26
27 struct s2n_async_pkey_op *pkey_op = NULL;
28
29 uint8_t test_digest_data[] = "I hashed this";
30 const uint32_t test_digest_size = sizeof(test_digest_data);
31 const uint8_t test_signature_data[] = "I signed this";
32 const uint32_t test_signature_size = sizeof(test_signature_data);
33 uint8_t test_encrypted_data[] = "I encrypted this";
34 const uint32_t test_encrypted_size = sizeof(test_encrypted_data);
35 uint8_t test_decrypted_data[] = "I decrypted this";
36 const uint32_t test_decrypted_size = sizeof(test_decrypted_data);
37
38 uint8_t offload_callback_count = 0;
39
40 typedef int(async_handler)(struct s2n_connection *conn);
41
42 /* Declaring a flag to check if sign operation is called at least once for all cipher_suites
43 * while performing handshake through handler (async_handler_sign_with_different_pkey_and_apply) */
44 static bool async_handler_sign_operation_called = false;
45
46 static int async_handler_fail(struct s2n_connection *conn)
47 {
48 FAIL_MSG("async_handler_fail should never get invoked");
49 return S2N_FAILURE;
50 }
51
52 static int async_handler_wipe_connection_and_apply(struct s2n_connection *conn)
53 {
54 /* Check that we have pkey_op */
55 EXPECT_NOT_NULL(pkey_op);
56
57 /* Extract pkey */
58 struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(conn);
59 EXPECT_NOT_NULL(chain_and_key);
60
61 s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key);
62 EXPECT_NOT_NULL(pkey);
63
64 /* Wipe connection */
65 EXPECT_SUCCESS(s2n_connection_wipe(conn));
66
67 /* Test that we can perform pkey operation, even if original connection was wiped */
68 EXPECT_SUCCESS(s2n_async_pkey_op_perform(pkey_op, pkey));
69
70 /* Test that pkey op can't be applied to wiped connection */
71 EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_apply(pkey_op, conn), S2N_ERR_ASYNC_WRONG_CONNECTION);
72
73 /* Free the pkey op */
74 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
75 pkey_op = NULL;
76
77 return S2N_FAILURE;
78 }
79
80 static int async_handler_sign_with_different_pkey_and_apply(struct s2n_connection *conn)
81 {
82 /* Check that we have pkey_op */
83 EXPECT_NOT_NULL(pkey_op);
84
85 /* Extract pkey */
86 struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(conn);
87 EXPECT_NOT_NULL(chain_and_key);
88 s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key);
89 EXPECT_NOT_NULL(pkey);
90
91 /* Test that we can perform pkey operation */
92 EXPECT_SUCCESS(s2n_async_pkey_op_perform(pkey_op, pkey));
93
94 /* Get type for pkey_op */
95 s2n_async_pkey_op_type type = { 0 };
96 EXPECT_SUCCESS(s2n_async_pkey_op_get_op_type(pkey_op, &type));
97
98 /* Test apply with different certificate chain only for sign operation */
99 if (type == S2N_ASYNC_SIGN) {
100 /* Create new chain and key, and modify current server conn */
101 struct s2n_cert_chain_and_key *chain_and_key_2 = NULL;
102 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key_2,
103 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
104
105 /* Change server conn cert data */
106 EXPECT_NOT_NULL(conn->handshake_params.our_chain_and_key);
107 conn->handshake_params.our_chain_and_key = chain_and_key_2;
108
109 /* Test that async sign operation will fail as signature was performed over different private key */
110 EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_apply(pkey_op, conn), S2N_ERR_VERIFY_SIGNATURE);
111
112 /* Set pkey_op's validation mode to S2N_ASYNC_PKEY_VALIDATION_FAST and test that async sign apply will pass now */
113 EXPECT_SUCCESS(s2n_async_pkey_op_set_validation_mode(pkey_op, S2N_ASYNC_PKEY_VALIDATION_FAST));
114 EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn));
115
116 /* Set chain_and_key back to original value and free new chain_and_key */
117 conn->handshake_params.our_chain_and_key = chain_and_key;
118 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key_2));
119
120 /* Update async_handler_sign_operation_called flag to true */
121 async_handler_sign_operation_called = true;
122 } else {
123 /* Test decrypt operation passes */
124 EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn));
125 }
126
127 /* Free the pkey op */
128 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
129 pkey_op = NULL;
130
131 return S2N_SUCCESS;
132 }
133
134 static int async_handler_free_pkey_op(struct s2n_connection *conn)
135 {
136 static int function_entered = 0;
137
138 /* Return failure on the second entrance into function so that we drop from try_handshake */
139 if (function_entered++ % 2 == 1) {
140 return S2N_FAILURE;
141 }
142
143 /* Check that we have pkey_op */
144 EXPECT_NOT_NULL(pkey_op);
145
146 /* Free the pkey op */
147 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
148 pkey_op = NULL;
149
150 /* Return success so that try_handshake calls s2n_negotiate again */
151 return S2N_SUCCESS;
152 }
153
154 static int try_handshake(struct s2n_connection *server_conn, struct s2n_connection *client_conn, async_handler handler)
155 {
156 s2n_blocked_status server_blocked;
157 s2n_blocked_status client_blocked;
158
159 int tries = 0;
160 do {
161 int client_rc = s2n_negotiate(client_conn, &client_blocked);
162 if (!(client_rc == 0 || (client_blocked && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED))) {
163 return S2N_FAILURE;
164 }
165
166 int server_rc = s2n_negotiate(server_conn, &server_blocked);
167 if (!(server_rc == 0 || (server_blocked && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED))) {
168 return S2N_FAILURE;
169 }
170
171 if (server_blocked == S2N_BLOCKED_ON_APPLICATION_INPUT) {
172 POSIX_GUARD(handler(server_conn));
173 }
174
175 EXPECT_NOT_EQUAL(++tries, 5);
176 } while (client_blocked || server_blocked);
177
178 POSIX_GUARD(s2n_shutdown_test_server_and_client(server_conn, client_conn));
179
180 return S2N_SUCCESS;
181 }
182
183 int async_pkey_apply_in_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
184 {
185 /* Check that we have op */
186 EXPECT_NOT_NULL(op);
187
188 /* Extract pkey */
189 struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(conn);
190 EXPECT_NOT_NULL(chain_and_key);
191
192 s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key);
193 EXPECT_NOT_NULL(pkey);
194
195 /* Perform the op */
196 EXPECT_SUCCESS(s2n_async_pkey_op_perform(op, pkey));
197
198 /* Test that op can be applied inside the callback */
199 EXPECT_SUCCESS(s2n_async_pkey_op_apply(op, conn));
200
201 /* Free the op */
202 EXPECT_SUCCESS(s2n_async_pkey_op_free(op));
203
204 return S2N_SUCCESS;
205 }
206
207 int async_pkey_store_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
208 {
209 pkey_op = op;
210 return S2N_SUCCESS;
211 }
212
213 int async_pkey_signature_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
214 {
215 pkey_op = op;
216
217 s2n_async_pkey_op_type type = { 0 };
218 EXPECT_SUCCESS(s2n_async_pkey_op_get_op_type(op, &type));
219 EXPECT_EQUAL(type, S2N_ASYNC_SIGN);
220
221 uint8_t expected_size = 0;
222 EXPECT_SUCCESS(s2n_hash_digest_size(S2N_HASH_SHA256, &expected_size));
223
224 uint32_t input_size = 0;
225 EXPECT_SUCCESS(s2n_async_pkey_op_get_input_size(op, &input_size));
226 EXPECT_EQUAL(input_size, expected_size);
227
228 struct s2n_blob input1 = { 0 };
229 EXPECT_SUCCESS(s2n_alloc(&input1, input_size));
230
231 struct s2n_blob input2 = { 0 };
232 EXPECT_SUCCESS(s2n_alloc(&input2, input_size));
233
234 struct s2n_blob expected_digest = { 0 };
235 EXPECT_SUCCESS(s2n_alloc(&expected_digest, expected_size));
236
237 struct s2n_hash_state digest = { 0 };
238 EXPECT_SUCCESS(s2n_hash_new(&digest));
239 EXPECT_SUCCESS(s2n_hash_init(&digest, S2N_HASH_SHA256));
240 EXPECT_SUCCESS(s2n_hash_update(&digest, test_digest_data, test_digest_size));
241 EXPECT_SUCCESS(s2n_hash_digest(&digest, expected_digest.data, expected_digest.size));
242 EXPECT_SUCCESS(s2n_hash_free(&digest));
243
244 /* Make sure that s2n_async_pkey_op_get_input can be called multiple times, and the returned values are the same. */
245 EXPECT_SUCCESS(s2n_async_pkey_op_get_input(op, input1.data, input1.size));
246 EXPECT_SUCCESS(s2n_async_pkey_op_get_input(op, input2.data, input2.size));
247
248 EXPECT_EQUAL(input1.size, input2.size);
249 EXPECT_BYTEARRAY_EQUAL(input1.data, input2.data, input1.size);
250 EXPECT_BYTEARRAY_EQUAL(input1.data, expected_digest.data, expected_size);
251
252 EXPECT_SUCCESS(s2n_free(&input1));
253 EXPECT_SUCCESS(s2n_free(&input2));
254 EXPECT_SUCCESS(s2n_free(&expected_digest));
255
256 EXPECT_SUCCESS(s2n_async_pkey_op_set_output(op, test_signature_data, test_signature_size));
257 offload_callback_count++;
258
259 return S2N_SUCCESS;
260 }
261
262 int async_pkey_decrypt_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
263 {
264 pkey_op = op;
265
266 s2n_async_pkey_op_type type = { 0 };
267 EXPECT_SUCCESS(s2n_async_pkey_op_get_op_type(op, &type));
268 EXPECT_EQUAL(type, S2N_ASYNC_DECRYPT);
269
270 uint32_t input_size = 0;
271 EXPECT_SUCCESS(s2n_async_pkey_op_get_input_size(op, &input_size));
272 EXPECT_EQUAL(input_size, test_encrypted_size);
273
274 struct s2n_blob input_buffer1 = { 0 };
275 EXPECT_SUCCESS(s2n_alloc(&input_buffer1, input_size));
276
277 struct s2n_blob input_buffer2 = { 0 };
278 EXPECT_SUCCESS(s2n_alloc(&input_buffer2, input_size));
279
280 /* Make sure that s2n_async_pkey_op_get_input can be called multiple times, and the returned values are the same. */
281 EXPECT_SUCCESS(s2n_async_pkey_op_get_input(op, input_buffer1.data, input_buffer1.size));
282 EXPECT_BYTEARRAY_EQUAL(input_buffer1.data, test_encrypted_data, test_encrypted_size);
283
284 EXPECT_SUCCESS(s2n_async_pkey_op_get_input(op, input_buffer2.data, input_buffer2.size));
285 EXPECT_BYTEARRAY_EQUAL(input_buffer2.data, test_encrypted_data, test_encrypted_size);
286
287 EXPECT_EQUAL(input_buffer1.size, input_buffer2.size);
288 EXPECT_EQUAL(input_buffer1.size, test_encrypted_size);
289 EXPECT_BYTEARRAY_EQUAL(input_buffer1.data, input_buffer2.data, test_encrypted_size);
290
291 EXPECT_SUCCESS(s2n_free(&input_buffer1));
292 EXPECT_SUCCESS(s2n_free(&input_buffer2));
293
294 EXPECT_SUCCESS(s2n_async_pkey_op_set_output(op, test_decrypted_data, test_decrypted_size));
295 offload_callback_count++;
296
297 return S2N_SUCCESS;
298 }
299
300 int s2n_async_sign_complete(struct s2n_connection *conn, struct s2n_blob *signature)
301 {
302 EXPECT_NOT_NULL(conn);
303 EXPECT_NOT_NULL(signature);
304
305 EXPECT_EQUAL(signature->size, test_signature_size);
306 EXPECT_BYTEARRAY_EQUAL(signature->data, test_signature_data, test_signature_size);
307 offload_callback_count++;
308
309 return S2N_SUCCESS;
310 }
311
312 int s2n_async_decrypt_complete(struct s2n_connection *conn, bool rsa_failed, struct s2n_blob *decrypted)
313 {
314 EXPECT_NOT_NULL(conn);
315 EXPECT_NOT_NULL(decrypted);
316 EXPECT_FALSE(rsa_failed);
317
318 EXPECT_EQUAL(decrypted->size, test_decrypted_size);
319 EXPECT_BYTEARRAY_EQUAL(decrypted->data, test_decrypted_data, test_decrypted_size);
320 offload_callback_count++;
321
322 return S2N_SUCCESS;
323 }
324
325 int async_pkey_invalid_input_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
326 {
327 pkey_op = op;
328
329 EXPECT_FAILURE(s2n_async_pkey_op_get_op_type(op, NULL));
330 EXPECT_FAILURE(s2n_async_pkey_op_get_input_size(op, NULL));
331 EXPECT_FAILURE(s2n_async_pkey_op_get_input(op, NULL, 0));
332
333 uint32_t input_size = 0;
334 EXPECT_SUCCESS(s2n_async_pkey_op_get_input_size(op, &input_size));
335
336 uint8_t placeholder_buffer[] = { 0x0, 0x0, 0x0, 0x0 };
337
338 /* Buffer too small to contain data. */
339 EXPECT_FAILURE(s2n_async_pkey_op_get_input(op, placeholder_buffer, input_size - 1));
340
341 EXPECT_FAILURE(s2n_async_pkey_op_set_output(op, NULL, test_signature_size));
342 offload_callback_count++;
343
344 return S2N_FAILURE;
345 }
346
347 int async_pkey_invalid_complete(struct s2n_connection *conn, struct s2n_blob *signature)
348 {
349 FAIL_MSG("Invalid async pkey callback was invoked. The callback should never be invoked if there was an earlier"
350 " failure in the async_pkey_op.");
351 return S2N_FAILURE;
352 }
353
354 static int s2n_test_bad_sign(const struct s2n_pkey *pub_key, s2n_signature_algorithm sig_alg,
355 struct s2n_hash_state *digest, struct s2n_blob *signature)
356 {
357 /* Just write all zeroes.
358 * This could accidentally be the correct signature, but it's very unlikely.
359 */
360 POSIX_GUARD(s2n_blob_zero(signature));
361 return S2N_SUCCESS;
362 }
363
364 int main(int argc, char **argv)
365 {
366 BEGIN_TEST();
367 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
368
369 char dhparams_pem[S2N_MAX_TEST_PEM_SIZE];
370 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
371
372 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
373 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
374 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
375
376 DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
377 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key,
378 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
379
380 /* Run all tests for 2 cipher suites to test both sign and decrypt operations */
381 struct s2n_cipher_suite *test_cipher_suites[] = {
382 &s2n_rsa_with_aes_128_gcm_sha256,
383 &s2n_ecdhe_rsa_with_aes_128_gcm_sha256,
384 };
385
386 for (size_t i = 0; i < s2n_array_len(test_cipher_suites); i++) {
387 struct s2n_cipher_preferences server_cipher_preferences = {
388 .count = 1,
389 .suites = &test_cipher_suites[i],
390 };
391
392 struct s2n_security_policy server_security_policy = {
393 .minimum_protocol_version = S2N_TLS12,
394 .cipher_preferences = &server_cipher_preferences,
395 .kem_preferences = &kem_preferences_null,
396 .signature_preferences = &s2n_signature_preferences_20200207,
397 .ecc_preferences = &s2n_ecc_preferences_20200310,
398 };
399
400 EXPECT_TRUE(test_cipher_suites[i]->available);
401
402 TEST_DEBUG_PRINT("Testing %s\n", test_cipher_suites[i]->name);
403
404 /* Test: apply while invoking callback */
405 {
406 struct s2n_config *server_config = NULL, *client_config = NULL;
407 EXPECT_NOT_NULL(server_config = s2n_config_new());
408 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
409 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
410 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_apply_in_callback));
411 server_config->security_policy = &server_security_policy;
412
413 EXPECT_NOT_NULL(client_config = s2n_config_new());
414 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
415 /* Security policy must support all cipher suites in test_cipher_suites above */
416 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
417
418 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
419
420 /* Create connection */
421 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
422 EXPECT_NOT_NULL(client_conn);
423 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
424
425 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
426 EXPECT_NOT_NULL(server_conn);
427 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
428
429 /* Create nonblocking pipes */
430 struct s2n_test_io_pair io_pair;
431 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
432 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
433 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
434
435 EXPECT_SUCCESS(try_handshake(server_conn, client_conn, async_handler_fail));
436
437 /* Free the data */
438 EXPECT_SUCCESS(s2n_connection_free(server_conn));
439 EXPECT_SUCCESS(s2n_connection_free(client_conn));
440 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
441 EXPECT_SUCCESS(s2n_config_free(server_config));
442 EXPECT_SUCCESS(s2n_config_free(client_config));
443 }
444
445 /* Test: wipe connection and then perform and apply pkey op */
446 {
447 struct s2n_config *server_config = NULL, *client_config = NULL;
448 EXPECT_NOT_NULL(server_config = s2n_config_new());
449 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
450 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
451 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_store_callback));
452 server_config->security_policy = &server_security_policy;
453
454 EXPECT_NOT_NULL(client_config = s2n_config_new());
455 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
456 /* Security policy must support all cipher suites in test_cipher_suites above */
457 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
458
459 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
460
461 /* Create connection */
462 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
463 EXPECT_NOT_NULL(client_conn);
464 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
465
466 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
467 EXPECT_NOT_NULL(server_conn);
468 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
469
470 /* Create nonblocking pipes */
471 struct s2n_test_io_pair io_pair;
472 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
473 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
474 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
475
476 EXPECT_EQUAL(try_handshake(server_conn, client_conn, async_handler_wipe_connection_and_apply), S2N_FAILURE);
477
478 /* Free the data */
479 EXPECT_SUCCESS(s2n_connection_free(server_conn));
480 EXPECT_SUCCESS(s2n_connection_free(client_conn));
481 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
482 EXPECT_SUCCESS(s2n_config_free(server_config));
483 EXPECT_SUCCESS(s2n_config_free(client_config));
484 }
485
486 /* Test: free the pkey op and try s2n_negotiate again */
487 {
488 struct s2n_config *server_config = NULL, *client_config = NULL;
489 EXPECT_NOT_NULL(server_config = s2n_config_new());
490 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
491 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
492 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_store_callback));
493 server_config->security_policy = &server_security_policy;
494
495 EXPECT_NOT_NULL(client_config = s2n_config_new());
496 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
497 /* Security policy must support all cipher suites in test_cipher_suites above */
498 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
499
500 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
501
502 /* Create connection */
503 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
504 EXPECT_NOT_NULL(client_conn);
505 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
506
507 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
508 EXPECT_NOT_NULL(server_conn);
509 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
510
511 /* Create nonblocking pipes */
512 struct s2n_test_io_pair io_pair;
513 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
514 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
515 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
516
517 EXPECT_FAILURE_WITH_ERRNO(
518 try_handshake(server_conn, client_conn, async_handler_free_pkey_op), S2N_ERR_ASYNC_BLOCKED);
519
520 /* Free the data */
521 EXPECT_SUCCESS(s2n_connection_free(server_conn));
522 EXPECT_SUCCESS(s2n_connection_free(client_conn));
523 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
524 EXPECT_SUCCESS(s2n_config_free(server_config));
525 EXPECT_SUCCESS(s2n_config_free(client_config));
526 }
527
528 /* Test: Apply invalid signature */
529 {
530 struct s2n_config *server_config = NULL, *client_config = NULL;
531 EXPECT_NOT_NULL(server_config = s2n_config_new());
532 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
533 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
534 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_store_callback));
535 server_config->security_policy = &server_security_policy;
536 /* Enable signature validation for async sign call */
537 EXPECT_SUCCESS(s2n_config_set_async_pkey_validation_mode(server_config, S2N_ASYNC_PKEY_VALIDATION_STRICT));
538
539 EXPECT_NOT_NULL(client_config = s2n_config_new());
540 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
541 /* Security policy must support all cipher suites in test_cipher_suites above */
542 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
543
544 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
545
546 /* Create connection */
547 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
548 EXPECT_NOT_NULL(client_conn);
549 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
550
551 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
552 EXPECT_NOT_NULL(server_conn);
553 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
554
555 /* Create nonblocking pipes */
556 struct s2n_test_io_pair io_pair;
557 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
558 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
559 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
560
561 EXPECT_SUCCESS(try_handshake(server_conn, client_conn, async_handler_sign_with_different_pkey_and_apply));
562
563 /* Free the data */
564 EXPECT_SUCCESS(s2n_connection_free(server_conn));
565 EXPECT_SUCCESS(s2n_connection_free(client_conn));
566 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
567 EXPECT_SUCCESS(s2n_config_free(server_config));
568 EXPECT_SUCCESS(s2n_config_free(client_config));
569 }
570
571 /* Test: Apply invalid signature, when signature validation is enabled for all sync / async signatures */
572 {
573 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
574 EXPECT_NOT_NULL(server_config);
575 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
576 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
577 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_store_callback));
578 server_config->security_policy = &server_security_policy;
579
580 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
581 EXPECT_NOT_NULL(client_config);
582 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
583 /* Security policy must support all cipher suites in test_cipher_suites above */
584 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
585 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
586
587 /* Create connection */
588 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
589 EXPECT_NOT_NULL(client_conn);
590 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
591
592 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
593 EXPECT_NOT_NULL(server_conn);
594 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
595
596 /* Create nonblocking pipes */
597 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
598 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
599 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
600 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
601
602 /* Enable signature validation */
603 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(server_config, S2N_VERIFY_AFTER_SIGN_ENABLED));
604 EXPECT_SUCCESS(try_handshake(server_conn, client_conn, async_handler_sign_with_different_pkey_and_apply));
605 }
606 }
607
608 /* Test if sign operation was called at least once for 'Test: Apply invalid signature',
609 * the flag holds the value after executing handshakes for all cipher_suites */
610 EXPECT_EQUAL(async_handler_sign_operation_called, true);
611
612 DEFER_CLEANUP(struct s2n_hash_state digest = { 0 }, s2n_hash_free);
613 EXPECT_SUCCESS(s2n_hash_new(&digest));
614 EXPECT_SUCCESS(s2n_hash_init(&digest, S2N_HASH_SHA256));
615 EXPECT_SUCCESS(s2n_hash_update(&digest, test_digest_data, test_digest_size));
616
617 /* Test: signature offload. */
618 {
619 EXPECT_EQUAL(0, offload_callback_count);
620 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
621 EXPECT_NOT_NULL(conn);
622 conn->config->async_pkey_cb = async_pkey_signature_callback;
623
624 EXPECT_FALSE(s2n_result_is_ok(s2n_async_pkey_sign(conn, S2N_SIGNATURE_ECDSA, &digest, s2n_async_sign_complete)));
625 EXPECT_TRUE(s2n_errno == S2N_ERR_ASYNC_BLOCKED);
626 EXPECT_EQUAL(1, offload_callback_count);
627
628 EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn));
629 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
630 EXPECT_EQUAL(2, offload_callback_count);
631
632 EXPECT_SUCCESS(s2n_connection_free(conn));
633 }
634
635 /* Test: decrypt offload. */
636 {
637 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
638 EXPECT_NOT_NULL(conn);
639 conn->config->async_pkey_cb = async_pkey_decrypt_callback;
640
641 struct s2n_blob encrypted_data = { 0 };
642 EXPECT_SUCCESS(s2n_blob_init(&encrypted_data, test_encrypted_data, test_encrypted_size));
643
644 struct s2n_blob decrypted_data = { 0 };
645 /* Re-use the encrypted data buffer to make sure that the data was actually transformed in the callback.
646 * If we filled this with the decrypted data, we would not know if the decryption happened in the callback. */
647 EXPECT_SUCCESS(s2n_blob_init(&decrypted_data, test_encrypted_data, test_encrypted_size));
648
649 EXPECT_FALSE(s2n_result_is_ok(s2n_async_pkey_decrypt(conn, &encrypted_data, &decrypted_data, s2n_async_decrypt_complete)));
650 EXPECT_TRUE(s2n_errno == S2N_ERR_ASYNC_BLOCKED);
651 EXPECT_EQUAL(3, offload_callback_count);
652
653 EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, conn));
654 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
655 EXPECT_EQUAL(4, offload_callback_count);
656
657 EXPECT_SUCCESS(s2n_connection_free(conn));
658 }
659
660 /* Test: errors in callback. */
661 {
662 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
663 EXPECT_NOT_NULL(conn);
664 conn->config->async_pkey_cb = async_pkey_invalid_input_callback;
665
666 EXPECT_FALSE(s2n_result_is_ok(s2n_async_pkey_sign(conn, S2N_SIGNATURE_ECDSA, &digest, async_pkey_invalid_complete)));
667 EXPECT_TRUE(s2n_errno == S2N_ERR_ASYNC_CALLBACK_FAILED);
668 EXPECT_EQUAL(5, offload_callback_count);
669
670 EXPECT_FAILURE(s2n_async_pkey_op_apply(pkey_op, conn));
671 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
672 EXPECT_EQUAL(5, offload_callback_count);
673
674 EXPECT_SUCCESS(s2n_connection_free(conn));
675 }
676
677 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
678
679 /* Test: Apply invalid signature to sync operation */
680 {
681 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
682 EXPECT_NOT_NULL(config);
683 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
684 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
685 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key));
686
687 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
688 EXPECT_NOT_NULL(client_conn);
689 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
690
691 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
692 EXPECT_NOT_NULL(server_conn);
693 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
694 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
695
696 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
697 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
698 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
699 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
700
701 /* Start the handshake.
702 * We need to perform enough of the handshake to choose a certificate / private key.
703 */
704 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(config, S2N_VERIFY_AFTER_SIGN_ENABLED));
705 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
706
707 /* Setup the pkey verify operation to fail for the chosen private key */
708 EXPECT_NOT_NULL(server_conn->handshake_params.our_chain_and_key);
709 EXPECT_NOT_NULL(server_conn->handshake_params.our_chain_and_key->private_key);
710 struct s2n_pkey *original_pkey = server_conn->handshake_params.our_chain_and_key->private_key;
711 struct s2n_pkey bad_pkey = *original_pkey;
712 bad_pkey.sign = s2n_test_bad_sign;
713 server_conn->handshake_params.our_chain_and_key->private_key = &bad_pkey;
714
715 /* Verify after sign should fail */
716 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
717 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(config, S2N_VERIFY_AFTER_SIGN_ENABLED));
718 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(server_conn, &blocked), S2N_ERR_VERIFY_SIGNATURE);
719
720 /* Reset pkey for cleanup */
721 server_conn->handshake_params.our_chain_and_key->private_key = original_pkey;
722 }
723
724 END_TEST();
725 }