Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_compute_shared_secret_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 <stdlib.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_security_policies.h"
22 #include "tls/s2n_tls.h"
23 #include "tls/s2n_tls13_handshake.c"
24
25 int main(int argc, char **argv)
26 {
27 BEGIN_TEST();
28
29 if (!s2n_is_tls13_fully_supported()) {
30 END_TEST();
31 }
32
33 struct s2n_cert_chain_and_key *cert_chain = NULL;
34 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&cert_chain,
35 S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
36 EXPECT_NOT_NULL(cert_chain);
37
38 struct s2n_config *config = s2n_config_new();
39 EXPECT_NOT_NULL(config);
40 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert_chain));
41 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
42
43 struct s2n_connection *client_conn = NULL;
44
45 /* This test ensures that if the server did not send a keyshare extension in the server hello function,
46 * a null pointer error is correctly thrown.
47 */
48 {
49 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
50
51 client_conn->actual_protocol_version = S2N_TLS13;
52
53 const struct s2n_ecc_preferences *ecc_pref = NULL;
54 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(client_conn, &ecc_pref));
55 EXPECT_NOT_NULL(ecc_pref);
56
57 /* Select curve and generate key for client */
58 client_conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
59 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_conn->kex_params.client_ecc_evp_params));
60 /* Recreating conditions where negotiated curve was not set */
61 struct s2n_ecc_evp_params missing_params = { NULL, NULL };
62 client_conn->kex_params.server_ecc_evp_params = missing_params;
63 DEFER_CLEANUP(struct s2n_blob client_shared_secret = { 0 }, s2n_free);
64 /* Compute fails because server's curve and public key are missing. */
65 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_compute_shared_secret(client_conn, &client_shared_secret), S2N_ERR_NULL);
66
67 EXPECT_SUCCESS(s2n_connection_free(client_conn));
68 };
69
70 /* This test ensures that if a server sent a keyshare extension without a public key, a null pointer
71 * error is correctly thrown.
72 */
73 {
74 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
75
76 client_conn->actual_protocol_version = S2N_TLS13;
77
78 const struct s2n_ecc_preferences *ecc_pref = NULL;
79 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(client_conn, &ecc_pref));
80 EXPECT_NOT_NULL(ecc_pref);
81
82 /* Select curve and generate key for client */
83 client_conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
84 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_conn->kex_params.client_ecc_evp_params));
85
86 /* Set curve server sent in server hello */
87 client_conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
88
89 DEFER_CLEANUP(struct s2n_blob client_shared_secret = { 0 }, s2n_free);
90 /* Compute fails because server's public key is missing */
91 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_compute_shared_secret(client_conn, &client_shared_secret), S2N_ERR_NULL);
92
93 EXPECT_SUCCESS(s2n_connection_free(client_conn));
94 };
95
96 /* This test ensures that if a server sent a keyshare extension with a public key and curve, a client can
97 * generate a shared secret from it.
98 */
99 {
100 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
101
102 const struct s2n_ecc_preferences *ecc_pref = NULL;
103 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(client_conn, &ecc_pref));
104 EXPECT_NOT_NULL(ecc_pref);
105
106 client_conn->actual_protocol_version = S2N_TLS13;
107
108 /* Select curve and generate key for client */
109 client_conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
110 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_conn->kex_params.client_ecc_evp_params));
111
112 /* Set curve server sent in server hello */
113 client_conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
114
115 /* Generate public key server sent in server hello */
116 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_conn->kex_params.server_ecc_evp_params));
117 DEFER_CLEANUP(struct s2n_blob client_shared_secret = { 0 }, s2n_free);
118 EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(client_conn, &client_shared_secret));
119
120 EXPECT_SUCCESS(s2n_connection_free(client_conn));
121 };
122
123 /* This test ensures that the shared secrets computed by a client and server after negotiation match */
124 {
125 client_conn = s2n_connection_new(S2N_CLIENT);
126 EXPECT_NOT_NULL(client_conn);
127 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
128
129 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
130 EXPECT_NOT_NULL(server_conn);
131 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
132
133 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
134 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
135 s2n_stuffer_data_available(&client_conn->handshake.io)));
136 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
137
138 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
139 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
140 s2n_stuffer_data_available(&server_conn->handshake.io)));
141 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
142
143 DEFER_CLEANUP(struct s2n_blob client_shared_secret = { 0 }, s2n_free);
144 EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(client_conn, &client_shared_secret));
145 EXPECT_TRUE(client_shared_secret.size > 0);
146
147 DEFER_CLEANUP(struct s2n_blob server_shared_secret = { 0 }, s2n_free);
148 EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(server_conn, &server_shared_secret));
149 EXPECT_TRUE(server_shared_secret.size > 0);
150
151 S2N_BLOB_EXPECT_EQUAL(server_shared_secret, client_shared_secret);
152
153 EXPECT_SUCCESS(s2n_connection_free(client_conn));
154 EXPECT_SUCCESS(s2n_connection_free(server_conn));
155 };
156
157 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(cert_chain));
158 EXPECT_SUCCESS(s2n_config_free(config));
159
160 END_TEST();
161 }