Debian Codegraph @

crt/s2n/tests/unit/s2n_drain_alert_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 #include <unistd.h>
18
19 #include "api/s2n.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22
23 #define ZERO_TO_THIRTY_ONE 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
24 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
25
26 #define INTERNAL_ERROR_ALERT_HEX 0x50
27
28 /* This test simulates a client sends a TLS alert record and closes its socket immediately after the ClientHello.
29 * We want to validate that s2n informs the caller of the alert instead of an I/O error. Both errors result
30 * in a failed handshake, but the alert is generally more useful.
31 */
32
33 int main(int argc, char **argv)
34 {
35 BEGIN_TEST();
36 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
37 uint8_t client_hello_message[] = {
38 /* Protocol version TLS 1.2 */
39 0x03, 0x03,
40 /* Client random */
41 ZERO_TO_THIRTY_ONE,
42 /* SessionID len - 32 bytes */
43 0x20,
44 /* Session ID */
45 ZERO_TO_THIRTY_ONE,
46 /* Cipher suites len */
47 0x00, 0x02,
48 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
49 0x00, 0x3C,
50 /* Compression methods len */
51 0x01,
52 /* Compression method - none */
53 0x00,
54 /* Extensions len */
55 0x00, 0x00
56 };
57 size_t body_len = sizeof(client_hello_message);
58 uint8_t message_header[] = {
59 /* Handshake message type CLIENT HELLO */
60 0x01,
61 /* Body len */
62 (body_len >> 16) & 0xff,
63 (body_len >> 8) & 0xff,
64 (body_len & 0xff),
65 };
66 size_t message_len = sizeof(message_header) + body_len;
67 uint8_t record_header[] = {
68 /* Record type HANDSHAKE */
69 0x16,
70 /* Protocol version TLS 1.2 */
71 0x03,
72 0x03,
73 /* Message len */
74 (message_len >> 8) & 0xff,
75 (message_len & 0xff),
76 };
77
78 uint8_t alert_record[] = {
79 /* Record type ALERT */
80 0x15,
81 /* Protocol version TLS 1.2 */
82 0x03,
83 0x03,
84 /* Length */
85 0x00,
86 0x02,
87 /* Fatal alert "internal_error" */
88 0x02,
89 INTERNAL_ERROR_ALERT_HEX,
90 };
91
92 struct s2n_connection *server_conn = NULL;
93 struct s2n_config *server_config = NULL;
94 s2n_blocked_status server_blocked;
95 char *cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE);
96 char *private_key = malloc(S2N_MAX_TEST_PEM_SIZE);
97 struct s2n_cert_chain_and_key *chain_and_key = NULL;
98
99 struct s2n_test_io_pair io_pair;
100 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
101
102 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
103 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
104
105 EXPECT_NOT_NULL(server_config = s2n_config_new());
106 /* Security policy must allow cipher suite hard coded into client hello */
107 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all"));
108 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
109 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
110 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
111 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain, private_key));
112 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
113 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
114
115 /* Send the client hello */
116 EXPECT_EQUAL(s2n_test_send(io_pair.client, record_header, sizeof(record_header)), sizeof(record_header));
117 EXPECT_EQUAL(s2n_test_send(io_pair.client, message_header, sizeof(message_header)), sizeof(message_header));
118 EXPECT_EQUAL(s2n_test_send(io_pair.client, client_hello_message, sizeof(client_hello_message)), sizeof(client_hello_message));
119
120 /* Send an alert from client to server */
121 EXPECT_EQUAL(s2n_test_send(io_pair.client, alert_record, sizeof(alert_record)), sizeof(alert_record));
122
123 /* Close the client read/write end */
124 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
125
126 /* Expect the server to fail due to an incoming alert. We should not fail due to an I/O error(EPIPE). */
127 s2n_negotiate(server_conn, &server_blocked);
128 EXPECT_EQUAL(s2n_error_get_type(s2n_errno), S2N_ERR_T_ALERT);
129 EXPECT_EQUAL(s2n_connection_get_alert(server_conn), INTERNAL_ERROR_ALERT_HEX);
130
131 EXPECT_SUCCESS(s2n_connection_free(server_conn));
132 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
133 EXPECT_SUCCESS(s2n_config_free(server_config));
134 free(cert_chain);
135 free(private_key);
136
137 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
138
139 END_TEST();
140 }