Debian Codegraph @

crt/s2n/tests/unit/s2n_seccomp_handshake_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 "api/s2n.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19
20 int main(int argc, char **argv)
21 {
22 /* We need to execute s2n_init before the seccomp filter is applied.
23 * Some one-time initialization involves opening files, like "dev/urandom".
24 * If built with aws-lc, s2n-tls also needs to call CRYPTO_pre_sandbox_init()
25 * before seccomp starts sandboxing.
26 *
27 * An application using s2n-tls with seccomp would need to do the same.
28 */
29 BEGIN_TEST();
30
31 /* One of the primary purposes of seccomp is to block opening new files.
32 * So before we enable seccomp, we need to open any files that the test would
33 * need. In this case, we need to load certificate pems from files.
34 *
35 * An application using s2n-tls with seccomp would need to do the same.
36 */
37 char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
38 char private_key_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
39 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
40 cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
41 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY,
42 private_key_pem, S2N_MAX_TEST_PEM_SIZE));
43
44 /* No unexpected syscalls allowed beyond this point */
45 EXPECT_OK(s2n_seccomp_init());
46
47 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
48 s2n_cert_chain_and_key_ptr_free);
49 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
50
51 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
52 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
53 EXPECT_SUCCESS(s2n_config_add_pem_to_trust_store(config, cert_chain_pem));
54
55 const char *security_policies[] = { "test_all_tls12", "default_tls13" };
56
57 for (size_t i = 0; i < s2n_array_len(security_policies); i++) {
58 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
59 s2n_connection_ptr_free);
60 EXPECT_NOT_NULL(client);
61 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
62 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, security_policies[i]));
63 EXPECT_SUCCESS(s2n_set_server_name(client, "127.0.0.1"));
64
65 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
66 s2n_connection_ptr_free);
67 EXPECT_NOT_NULL(server);
68 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
69 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, security_policies[i]));
70
71 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
72 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
73 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
74 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
75
76 const uint8_t data[] = "hello world";
77 uint8_t buffer[100] = { 0 };
78 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
79 EXPECT_EQUAL(s2n_send(client, data, sizeof(data), &blocked), sizeof(data));
80 EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(data));
81 EXPECT_BYTEARRAY_EQUAL(buffer, data, sizeof(data));
82 }
83
84 END_TEST();
85 }