Debian Codegraph @

crt/s2n/tests/unit/s2n_ems_extension_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 "tls/extensions/s2n_ems.h"
19 #include "tls/extensions/s2n_extension_list.h"
20 #include "tls/s2n_connection.h"
21
22 int main(int argc, char **argv)
23 {
24 BEGIN_TEST();
25
26 /* s2n_server_ems_should_send */
27 {
28 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
29 EXPECT_NOT_NULL(conn);
30
31 /* Protocol version is too high */
32 conn->actual_protocol_version = S2N_TLS13;
33 EXPECT_FALSE(s2n_server_ems_extension.should_send(conn));
34
35 /* Protocol version is less than TLS1.3 */
36 conn->actual_protocol_version = S2N_TLS12;
37 EXPECT_TRUE(s2n_server_ems_extension.should_send(conn));
38
39 EXPECT_SUCCESS(s2n_connection_free(conn));
40 };
41
42 /* Test that the ems_negotiated flag is set when the EMS extension is received */
43 {
44 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
45 EXPECT_NOT_NULL(server_conn);
46 EXPECT_OK(s2n_connection_set_tls12_security_policy(server_conn));
47
48 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
49 EXPECT_NOT_NULL(client_conn);
50 EXPECT_OK(s2n_connection_set_tls12_security_policy(client_conn));
51
52 /* This extension is only relevant for TLS1.2 */
53 server_conn->actual_protocol_version = S2N_TLS12;
54 client_conn->actual_protocol_version = S2N_TLS12;
55
56 DEFER_CLEANUP(struct s2n_stuffer stuffer, s2n_stuffer_free);
57 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
58 EXPECT_SUCCESS(s2n_extension_list_send(S2N_EXTENSION_LIST_CLIENT_HELLO, client_conn, &stuffer));
59 EXPECT_FALSE(client_conn->ems_negotiated);
60
61 EXPECT_SUCCESS(s2n_extension_list_recv(S2N_EXTENSION_LIST_CLIENT_HELLO, server_conn, &stuffer));
62 EXPECT_TRUE(server_conn->ems_negotiated);
63
64 EXPECT_SUCCESS(s2n_extension_list_send(S2N_EXTENSION_LIST_SERVER_HELLO_DEFAULT, server_conn, &stuffer));
65 EXPECT_SUCCESS(s2n_extension_list_recv(S2N_EXTENSION_LIST_SERVER_HELLO_DEFAULT, client_conn, &stuffer));
66 EXPECT_TRUE(client_conn->ems_negotiated);
67
68 EXPECT_SUCCESS(s2n_connection_free(server_conn));
69 EXPECT_SUCCESS(s2n_connection_free(client_conn));
70 };
71
72 /* s2n_server_ems_is_missing */
73 {
74 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
75 EXPECT_NOT_NULL(conn);
76
77 /**
78 *= https://www.rfc-editor.org/rfc/rfc7627#section-5.3
79 *= type=test
80 *# If the original session used the extension but the new ServerHello
81 *# does not contain the extension, the client MUST abort the
82 *# handshake.
83 **/
84 conn->ems_negotiated = true;
85 EXPECT_FAILURE_WITH_ERRNO(s2n_server_ems_extension.if_missing(conn), S2N_ERR_MISSING_EXTENSION);
86
87 conn->ems_negotiated = false;
88 EXPECT_SUCCESS(s2n_server_ems_extension.if_missing(conn));
89
90 EXPECT_SUCCESS(s2n_connection_free(conn));
91 };
92
93 /* s2n_client_ems_should_send */
94 {
95 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
96 EXPECT_NOT_NULL(conn);
97
98 /* In the default case we should always be sending this extension */
99 EXPECT_TRUE(s2n_client_ems_extension.should_send(conn));
100
101 conn->set_session = true;
102 conn->ems_negotiated = true;
103 /* If we have set a ticket on the connection we only send this extension
104 * if the previous session negotiated EMS. */
105 EXPECT_TRUE(s2n_client_ems_extension.should_send(conn));
106
107 /* Don't send this extension if we have set a ticket on the connection
108 * and the previous session did not negotiate EMS */
109 conn->ems_negotiated = false;
110 EXPECT_FALSE(s2n_client_ems_extension.should_send(conn));
111
112 EXPECT_SUCCESS(s2n_connection_free(conn));
113 };
114
115 END_TEST();
116 }