Debian Codegraph @

crt/s2n/tests/unit/s2n_client_finished_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 #include "tls/s2n_tls.h"
20
21 int main(int argc, char **argv)
22 {
23 BEGIN_TEST();
24
25 /* Send and receive correct ClientFinished */
26 {
27 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
28 EXPECT_NOT_NULL(server_conn);
29 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
30 server_conn->actual_protocol_version = S2N_TLS12;
31
32 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
33 EXPECT_NOT_NULL(client_conn);
34 client_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
35 client_conn->actual_protocol_version = S2N_TLS12;
36
37 /* Calculate valid verify_data */
38 POSIX_GUARD(s2n_prf_client_finished(server_conn));
39
40 EXPECT_EQUAL(client_conn->client, client_conn->initial);
41
42 EXPECT_SUCCESS(s2n_client_finished_send(client_conn));
43 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
44 s2n_stuffer_data_available(&client_conn->handshake.io)));
45 EXPECT_SUCCESS(s2n_client_finished_recv(server_conn));
46
47 EXPECT_EQUAL(client_conn->client, client_conn->secure);
48 };
49
50 /* Server rejects incorrect ClientFinished */
51 {
52 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
53 EXPECT_NOT_NULL(server_conn);
54 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
55 server_conn->actual_protocol_version = S2N_TLS12;
56
57 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
58 EXPECT_NOT_NULL(client_conn);
59 client_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
60 client_conn->actual_protocol_version = S2N_TLS12;
61
62 EXPECT_SUCCESS(s2n_client_finished_send(client_conn));
63
64 /* s2n_client_finished_send calculates and writes the handshake verify data
65 * to the io stuffer. Since the verify data is at least 12 bytes long
66 * we can flip a random bit in that range to mutate it. */
67 client_conn->handshake.io.blob.data[10]++;
68 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
69 s2n_stuffer_data_available(&client_conn->handshake.io)));
70 EXPECT_FAILURE_WITH_ERRNO(s2n_client_finished_recv(server_conn), S2N_ERR_BAD_MESSAGE);
71 };
72
73 /* Error if local verify_data has wrong length */
74 {
75 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
76 EXPECT_NOT_NULL(server_conn);
77 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
78 server_conn->actual_protocol_version = S2N_TLS12;
79
80 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
81 EXPECT_NOT_NULL(client_conn);
82 client_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
83 client_conn->actual_protocol_version = S2N_TLS12;
84
85 /* Change the length of valid verify_data */
86 POSIX_GUARD(s2n_prf_client_finished(server_conn));
87 server_conn->handshake.finished_len = 1;
88
89 EXPECT_SUCCESS(s2n_client_finished_send(client_conn));
90 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
91 s2n_stuffer_data_available(&client_conn->handshake.io)));
92 EXPECT_FAILURE_WITH_ERRNO(s2n_client_finished_recv(server_conn), S2N_ERR_SAFETY);
93 };
94
95 END_TEST();
96 }