Debian Codegraph @

crt/s2n/tests/unit/s2n_sslv3_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18 #include "utils/s2n_random.h"
19
20 #define S2N_TEST_DATA_SIZE 100
21
22 S2N_RESULT s2n_test_send_receive_data(struct s2n_connection *sender, struct s2n_connection *receiver)
23 {
24 uint8_t test_data[S2N_TEST_DATA_SIZE] = { 0 };
25 struct s2n_blob test_data_blob = { 0 };
26 EXPECT_SUCCESS(s2n_blob_init(&test_data_blob, test_data, sizeof(test_data)));
27 EXPECT_OK(s2n_get_public_random_data(&test_data_blob));
28
29 /* Send data */
30 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
31 ssize_t bytes_written = 0;
32 while (bytes_written < S2N_TEST_DATA_SIZE) {
33 ssize_t w = s2n_send(sender, test_data + bytes_written, S2N_TEST_DATA_SIZE - bytes_written, &blocked);
34 EXPECT_TRUE(w >= 0);
35 bytes_written += w;
36 }
37
38 /* Receive data */
39 uint8_t buffer[S2N_TEST_DATA_SIZE] = { 0 };
40 ssize_t bytes_received = 0;
41 while (bytes_received < S2N_TEST_DATA_SIZE) {
42 ssize_t r = s2n_recv(receiver, buffer + bytes_received, S2N_TEST_DATA_SIZE - bytes_received, &blocked);
43 EXPECT_TRUE(r > 0);
44 bytes_received += r;
45 }
46
47 EXPECT_BYTEARRAY_EQUAL(test_data, buffer, S2N_TEST_DATA_SIZE);
48
49 return S2N_RESULT_OK;
50 }
51
52 int main(int argc, char **argv)
53 {
54 BEGIN_TEST();
55
56 DEFER_CLEANUP(struct s2n_cert_chain_and_key *rsa_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
57 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&rsa_chain_and_key,
58 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
59
60 DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
61 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key,
62 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
63
64 char dhparams_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
65 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
66
67 /* Self-talk test */
68 {
69 size_t supported_record_alg_count = 0;
70
71 for (size_t i = 0; i < security_policy_test_all.cipher_preferences->count; i++) {
72 struct s2n_cipher_suite *cipher_suite = security_policy_test_all.cipher_preferences->suites[i];
73
74 /* Skip non-sslv3 cipher suites. */
75 if (!cipher_suite->sslv3_record_alg) {
76 continue;
77 }
78
79 /* Skip unsupported record algorithms. */
80 if (!cipher_suite->sslv3_record_alg->cipher->is_available()) {
81 continue;
82 }
83 supported_record_alg_count += 1;
84
85 struct s2n_cipher_preferences test_cipher_preferences = {
86 .count = 1,
87 .suites = &cipher_suite,
88 };
89 struct s2n_security_policy test_policy = security_policy_test_all;
90 test_policy.cipher_preferences = &test_cipher_preferences;
91
92 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
93 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
94 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
95 client_config->security_policy = &test_policy;
96
97 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
98 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_chain_and_key));
99 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_chain_and_key));
100 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
101 server_config->security_policy = &test_policy;
102
103 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
104 s2n_connection_ptr_free);
105 EXPECT_NOT_NULL(client);
106 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
107 client->client_protocol_version = S2N_SSLv3;
108
109 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
110 s2n_connection_ptr_free);
111 EXPECT_NOT_NULL(server);
112 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
113
114 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
115 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
116 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
117
118 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
119 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(server), S2N_SSLv3);
120 EXPECT_EQUAL(s2n_connection_get_actual_protocol_version(server), S2N_SSLv3);
121
122 EXPECT_OK(s2n_test_send_receive_data(client, server));
123 EXPECT_OK(s2n_test_send_receive_data(server, client));
124 }
125
126 /* Ensure that a supported record algorithm was found, and SSLv3 was tested at least once. */
127 EXPECT_TRUE(supported_record_alg_count > 0);
128 }
129
130 END_TEST();
131 }