Debian Codegraph @

crt/s2n/tests/unit/s2n_server_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 ServerFinished */
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_server_finished(client_conn));
39
40 EXPECT_EQUAL(server_conn->server, server_conn->initial);
41
42 EXPECT_SUCCESS(s2n_server_finished_send(server_conn));
43 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
44 s2n_stuffer_data_available(&server_conn->handshake.io)));
45 EXPECT_SUCCESS(s2n_server_finished_recv(client_conn));
46
47 EXPECT_EQUAL(server_conn->server, server_conn->secure);
48 };
49
50 /* Client rejects incorrect ServerFinished */
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 /* Mutate valid verify_data */
63 POSIX_GUARD(s2n_prf_server_finished(client_conn));
64 client_conn->handshake.server_finished[0]++;
65
66 EXPECT_SUCCESS(s2n_server_finished_send(server_conn));
67 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
68 s2n_stuffer_data_available(&server_conn->handshake.io)));
69 EXPECT_FAILURE_WITH_ERRNO(s2n_client_finished_recv(client_conn), S2N_ERR_BAD_MESSAGE);
70 };
71
72 /* Error if local verify_data has wrong length */
73 {
74 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
75 EXPECT_NOT_NULL(server_conn);
76 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
77 server_conn->actual_protocol_version = S2N_TLS12;
78
79 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
80 EXPECT_NOT_NULL(client_conn);
81 client_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
82 client_conn->actual_protocol_version = S2N_TLS12;
83
84 /* Change the length of valid verify_data */
85 POSIX_GUARD(s2n_prf_server_finished(client_conn));
86 client_conn->handshake.finished_len = 1;
87
88 EXPECT_SUCCESS(s2n_server_finished_send(server_conn));
89 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
90 s2n_stuffer_data_available(&server_conn->handshake.io)));
91 EXPECT_FAILURE_WITH_ERRNO(s2n_client_finished_recv(client_conn), S2N_ERR_SAFETY);
92 };
93
94 END_TEST();
95 }