Debian Codegraph @

crt/s2n/tests/unit/s2n_test_mismatched_curves.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 "crypto/s2n_mldsa.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 DEFER_CLEANUP(struct s2n_config *ecdsa_config = s2n_config_new(), s2n_config_ptr_free);
26 EXPECT_NOT_NULL(ecdsa_config);
27 EXPECT_SUCCESS(s2n_config_disable_x509_verification(ecdsa_config));
28 DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_cert_chain = NULL, s2n_cert_chain_and_key_ptr_free);
29
30 /* Note that this certificate will only be able to produce signatures with the secp256r1 curve */
31 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_cert_chain, S2N_ECDSA_P256_PKCS1_CERT_CHAIN,
32 S2N_ECDSA_P256_PKCS1_KEY));
33 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(ecdsa_config, ecdsa_cert_chain));
34
35 /* This testfile includes various scenarios where the signature writer ignores the curve preferences of the
36 * peer when creating the transcript signature. The setup is slightly different depending on the protocol
37 * version being negotiated.
38 *
39 * In TLS1.2, an endpoint advertised its supported curves list through the supported curves extension.
40 * In TLS1.2, we error when the peer's public key curve type is not in our supported
41 * curves list.
42 * In TLS1.3, signature algorithms now imply a specific curve.
43 * In TLS1.3 we error if the curve in the peer's public key doesn't match the signature algorithm's
44 * intended curve.
45 */
46
47 /* Test TLS1.3 codepaths */
48 if (s2n_is_tls13_fully_supported()) {
49 {
50 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
51 s2n_connection_ptr_free);
52 EXPECT_NOT_NULL(server);
53
54 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
55 s2n_connection_ptr_free);
56 EXPECT_NOT_NULL(client);
57
58 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
59 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
60 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
61
62 EXPECT_SUCCESS(s2n_connection_set_config(server, ecdsa_config));
63 EXPECT_SUCCESS(s2n_connection_set_config(client, ecdsa_config));
64 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "test_all_tls13"));
65 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "test_all_tls13"));
66
67 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT_VERIFY));
68
69 /* Set an ECDSA signature scheme. This results in the wire bytes signifying ecdsa_secp384r1_sha384
70 * but because the only cert key available is secp256r1 the actual signature produced will be
71 * ecdsa with a 384 hash alg and a secp256r1 key. */
72 server->handshake_params.server_cert_sig_scheme = &s2n_ecdsa_sha384;
73
74 /* Send cert verify */
75 EXPECT_SUCCESS(s2n_tls13_cert_verify_send(server));
76 EXPECT_SUCCESS(s2n_stuffer_copy(&server->handshake.io, &client->handshake.io,
77 s2n_stuffer_data_available(&server->handshake.io)));
78
79 /* Sanity check that we actually did write the intended iana id */
80 uint16_t iana_id = 0;
81 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&client->handshake.io, &iana_id));
82 EXPECT_EQUAL(iana_id, TLS_SIGNATURE_SCHEME_ECDSA_SHA384);
83 EXPECT_SUCCESS(s2n_stuffer_reread(&client->handshake.io));
84
85 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(client), S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
86 };
87
88 /* MTLS codepath */
89 {
90 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
91 s2n_connection_ptr_free);
92 EXPECT_NOT_NULL(server);
93
94 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
95 s2n_connection_ptr_free);
96 EXPECT_NOT_NULL(client);
97
98 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
99 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
100 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
101
102 EXPECT_SUCCESS(s2n_connection_set_config(server, ecdsa_config));
103 EXPECT_SUCCESS(s2n_connection_set_config(client, ecdsa_config));
104 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "test_all_tls13"));
105 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "test_all_tls13"));
106 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, S2N_CERT_AUTH_REQUIRED));
107 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client, S2N_CERT_AUTH_REQUIRED));
108
109 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, CLIENT_CERT_VERIFY));
110
111 /* Set an ECDSA signature scheme. This results in the wire bytes signifying ecdsa_secp384r1_sha384
112 * but because the only cert key available is secp256r1 the actual signature produced will be
113 * ecdsa with a sha384 hash and a secp256r1 key. */
114 client->handshake_params.client_cert_sig_scheme = &s2n_ecdsa_sha384;
115
116 /* Send cert verify */
117 EXPECT_SUCCESS(s2n_tls13_cert_verify_send(client));
118 EXPECT_SUCCESS(s2n_stuffer_copy(&client->handshake.io, &server->handshake.io,
119 s2n_stuffer_data_available(&client->handshake.io)));
120
121 /* Sanity check that we actually did write the intended iana id */
122 uint16_t iana_id = 0;
123 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&server->handshake.io, &iana_id));
124 EXPECT_EQUAL(iana_id, TLS_SIGNATURE_SCHEME_ECDSA_SHA384);
125 EXPECT_SUCCESS(s2n_stuffer_reread(&server->handshake.io));
126
127 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(server), S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
128 };
129 }
130
131 /* Test TLS1.2 codepaths */
132 {
133 {
134 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
135 s2n_connection_ptr_free);
136 EXPECT_NOT_NULL(server);
137
138 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
139 s2n_connection_ptr_free);
140 EXPECT_NOT_NULL(client);
141
142 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
143 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
144 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
145
146 EXPECT_SUCCESS(s2n_connection_set_config(server, ecdsa_config));
147 EXPECT_SUCCESS(s2n_connection_set_config(client, ecdsa_config));
148 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "test_all_tls12"));
149 /* This policy only supports curve secp384r1 in its ECC list */
150 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20210816"));
151
152 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_KEY));
153
154 /* Send cert verify */
155 EXPECT_SUCCESS(s2n_server_key_send(server));
156 EXPECT_SUCCESS(s2n_stuffer_copy(&server->handshake.io, &client->handshake.io,
157 s2n_stuffer_data_available(&server->handshake.io)));
158
159 EXPECT_FAILURE_WITH_ERRNO(s2n_server_key_recv(client), S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
160 };
161
162 /* MTLS codepath */
163 {
164 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
165 s2n_connection_ptr_free);
166 EXPECT_NOT_NULL(server);
167
168 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
169 s2n_connection_ptr_free);
170 EXPECT_NOT_NULL(client);
171
172 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
173 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
174 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
175
176 EXPECT_SUCCESS(s2n_connection_set_config(server, ecdsa_config));
177 EXPECT_SUCCESS(s2n_connection_set_config(client, ecdsa_config));
178
179 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, S2N_CERT_AUTH_REQUIRED));
180 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client, S2N_CERT_AUTH_REQUIRED));
181
182 /* This policy only supports curve secp384r1 in its ECC list */
183 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "20210816"));
184 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "test_all_tls12"));
185
186 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, CLIENT_CERT_VERIFY));
187
188 /* Send cert verify */
189 EXPECT_SUCCESS(s2n_client_cert_verify_send(client));
190 EXPECT_SUCCESS(s2n_stuffer_copy(&client->handshake.io, &server->handshake.io,
191 s2n_stuffer_data_available(&client->handshake.io)));
192
193 EXPECT_FAILURE_WITH_ERRNO(s2n_client_cert_verify_recv(server), S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
194 };
195 };
196
197 /* ML-DSA signature test: ML-DSA is supported only by TLS 1.3 */
198 if (s2n_is_tls13_fully_supported() && s2n_mldsa_is_supported()) {
199 DEFER_CLEANUP(struct s2n_cert_chain_and_key *mldsa_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
200 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&mldsa_chain_and_key, S2N_MLDSA44_CERT, S2N_MLDSA44_KEY));
201
202 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
203 EXPECT_NOT_NULL(server_config);
204 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, mldsa_chain_and_key));
205
206 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
207 EXPECT_NOT_NULL(server);
208 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
209 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "test_all"));
210
211 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
212 EXPECT_NOT_NULL(client_config);
213 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
214
215 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
216 EXPECT_NOT_NULL(client);
217 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
218 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "cnsa_2"));
219
220 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
221 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
222 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
223
224 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT_VERIFY));
225
226 /* Set the ML-DSA-87 signature scheme. */
227 server->handshake_params.server_cert_sig_scheme = &s2n_mldsa87;
228
229 /* Send cert verify */
230 EXPECT_SUCCESS(s2n_tls13_cert_verify_send(server));
231 EXPECT_SUCCESS(s2n_stuffer_copy(&server->handshake.io, &client->handshake.io,
232 s2n_stuffer_data_available(&server->handshake.io)));
233
234 /* Sanity check that we actually did write the intended iana id */
235 uint16_t iana_id = 0;
236 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&client->handshake.io, &iana_id));
237 EXPECT_EQUAL(iana_id, TLS_SIGNATURE_SCHEME_MLDSA87);
238 EXPECT_SUCCESS(s2n_stuffer_reread(&client->handshake.io));
239
240 /* Handshake fail due to ML-DSA key type does not match. */
241 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_verify_recv(client), S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
242 }
243
244 END_TEST();
245 }