Debian Codegraph @

crt/s2n/tests/unit/s2n_ecc_evp_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 "crypto/s2n_ecc_evp.h"
17
18 #include "api/s2n.h"
19 #include "crypto/s2n_fips.h"
20 #include "crypto/s2n_libcrypto.h"
21 #include "s2n_test.h"
22 #include "stuffer/s2n_stuffer.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/s2n_connection.h"
25 #include "tls/s2n_security_policies.h"
26 #include "utils/s2n_mem.h"
27
28 #define ECDHE_PARAMS_LEGACY_FORM 4
29
30 extern const struct s2n_ecc_named_curve s2n_unsupported_curve;
31
32 DEFINE_POINTER_CLEANUP_FUNC(EC_KEY*, EC_KEY_free);
33 DEFINE_POINTER_CLEANUP_FUNC(EC_POINT*, EC_POINT_free);
34
35 int main(int argc, char** argv)
36 {
37 BEGIN_TEST();
38 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
39
40 /* Test the EC_KEY_CHECK_FIPS feature probe. AWS-LC is a libcrypto known to support this feature. */
41 if (s2n_libcrypto_is_awslc()) {
42 EXPECT_TRUE(s2n_ecc_evp_supports_fips_check());
43 }
44
45 {
46 /* Test generate ephemeral keys for all supported curves */
47 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
48 struct s2n_ecc_evp_params evp_params = { 0 };
49 /* Server generates a key */
50 evp_params.negotiated_curve = s2n_all_supported_curves_list[i];
51 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&evp_params));
52 EXPECT_NOT_NULL(evp_params.evp_pkey);
53 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&evp_params));
54 }
55 };
56 {
57 /* Test failure case for generate ephemeral key when the negotiated curve is not set */
58 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
59 struct s2n_ecc_evp_params evp_params = { 0 };
60 /* Server generates a key */
61 evp_params.negotiated_curve = NULL;
62 EXPECT_FAILURE(s2n_ecc_evp_generate_ephemeral_key(&evp_params));
63 EXPECT_NULL(evp_params.evp_pkey);
64 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&evp_params));
65 }
66 };
67 {
68 /* Test generate ephemeral key and compute shared key for all supported curves */
69 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
70 struct s2n_ecc_evp_params server_params = { 0 };
71 struct s2n_ecc_evp_params client_params = { 0 };
72 struct s2n_blob server_shared = { 0 };
73 struct s2n_blob client_shared = { 0 };
74
75 /* Server generates a key */
76 server_params.negotiated_curve = s2n_all_supported_curves_list[i];
77 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_params));
78 EXPECT_NOT_NULL(server_params.evp_pkey);
79
80 /* Client generates a key */
81 client_params.negotiated_curve = s2n_all_supported_curves_list[i];
82 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_params));
83 EXPECT_NOT_NULL(client_params.evp_pkey);
84
85 /* Compute shared secret for server */
86 EXPECT_SUCCESS(
87 s2n_ecc_evp_compute_shared_secret_from_params(&server_params, &client_params, &server_shared));
88
89 /* Compute shared secret for client */
90 EXPECT_SUCCESS(
91 s2n_ecc_evp_compute_shared_secret_from_params(&client_params, &server_params, &client_shared));
92
93 /* Check if the shared secret computed is the same for the client
94 * and the server */
95 EXPECT_EQUAL(client_shared.size, server_shared.size);
96 EXPECT_BYTEARRAY_EQUAL(client_shared.data, server_shared.data, client_shared.size);
97
98 /* Clean up */
99 EXPECT_SUCCESS(s2n_free(&server_shared));
100 EXPECT_SUCCESS(s2n_free(&client_shared));
101 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&server_params));
102 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&client_params));
103 }
104 };
105 {
106 /* Test failure case for computing shared key for all supported curves when the server
107 and client curves do not match */
108 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
109 for (size_t j = 0; j < s2n_all_supported_curves_list_len; j++) {
110 struct s2n_ecc_evp_params server_params = { 0 };
111 struct s2n_ecc_evp_params client_params = { 0 };
112 struct s2n_blob server_shared = { 0 };
113 struct s2n_blob client_shared = { 0 };
114 if (i == j) {
115 continue;
116 }
117
118 /* Server generates a key */
119 server_params.negotiated_curve = s2n_all_supported_curves_list[j];
120
121 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_params));
122 EXPECT_NOT_NULL(server_params.evp_pkey);
123
124 /* Client generates a key */
125 client_params.negotiated_curve = s2n_all_supported_curves_list[i];
126 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_params));
127 EXPECT_NOT_NULL(client_params.evp_pkey);
128
129 /* Compute shared secret for server */
130 EXPECT_FAILURE(
131 s2n_ecc_evp_compute_shared_secret_from_params(&server_params, &client_params, &server_shared));
132
133 /* Compute shared secret for client */
134 EXPECT_FAILURE(
135 s2n_ecc_evp_compute_shared_secret_from_params(&client_params, &server_params, &client_shared));
136
137 /* Clean up */
138 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&server_params));
139 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&client_params));
140 }
141 }
142 };
143 {
144 /* Test s2n_ecc_evp_write_params_point for all supported curves */
145 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
146 struct s2n_ecc_evp_params test_params = { 0 };
147 struct s2n_stuffer wire = { 0 };
148 uint8_t legacy_form = 0;
149
150 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 0));
151
152 /* Server generates a key for a given curve */
153 test_params.negotiated_curve = s2n_all_supported_curves_list[i];
154 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&test_params));
155 EXPECT_NOT_NULL(test_params.evp_pkey);
156 EXPECT_SUCCESS(s2n_ecc_evp_write_params_point(&test_params, &wire));
157
158 /* Verify output is of the right length */
159 uint32_t avail = s2n_stuffer_data_available(&wire);
160 EXPECT_EQUAL(avail, s2n_all_supported_curves_list[i]->share_size);
161
162 /* Verify output starts with the known legacy form for curves secp256r1
163 * and secp384r1*/
164 if (s2n_all_supported_curves_list[i]->iana_id == TLS_EC_CURVE_SECP_256_R1 || s2n_all_supported_curves_list[i]->iana_id == TLS_EC_CURVE_SECP_384_R1) {
165 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&wire, &legacy_form));
166 EXPECT_EQUAL(legacy_form, ECDHE_PARAMS_LEGACY_FORM);
167 }
168
169 /* Clean up */
170 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&test_params));
171 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
172 }
173 };
174 {
175 /* TEST s2n_ecc_evp_read_params_point for all supported curves */
176 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
177 struct s2n_ecc_evp_params write_params = { 0 };
178 struct s2n_blob point_blob = { 0 };
179 struct s2n_stuffer wire = { 0 };
180
181 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 0));
182
183 /* Server generates a key for a given curve */
184 write_params.negotiated_curve = s2n_all_supported_curves_list[i];
185 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&write_params));
186 EXPECT_NOT_NULL(write_params.evp_pkey);
187 EXPECT_SUCCESS(s2n_ecc_evp_write_params_point(&write_params, &wire));
188
189 /* Read point back in */
190 EXPECT_SUCCESS(
191 s2n_ecc_evp_read_params_point(&wire, s2n_all_supported_curves_list[i]->share_size, &point_blob));
192
193 /* Check that the blob looks generally correct. */
194 EXPECT_EQUAL(point_blob.size, s2n_all_supported_curves_list[i]->share_size);
195 EXPECT_NOT_NULL(point_blob.data);
196
197 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&write_params));
198 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
199 }
200 };
201 {
202 /* TEST s2n_ecc_evp_parse_params_point for all supported curves */
203 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
204 struct s2n_ecc_evp_params write_params = { 0 };
205 struct s2n_ecc_evp_params read_params = { 0 };
206 struct s2n_blob point_blob = { 0 };
207 struct s2n_stuffer wire = { 0 };
208
209 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 0));
210
211 write_params.negotiated_curve = s2n_all_supported_curves_list[i];
212 read_params.negotiated_curve = s2n_all_supported_curves_list[i];
213
214 /* Server generates a key for a given curve */
215 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&write_params));
216 EXPECT_NOT_NULL(write_params.evp_pkey);
217 EXPECT_SUCCESS(s2n_ecc_evp_write_params_point(&write_params, &wire));
218
219 /* Read point back in */
220 EXPECT_SUCCESS(
221 s2n_ecc_evp_read_params_point(&wire, s2n_all_supported_curves_list[i]->share_size, &point_blob));
222 EXPECT_SUCCESS(s2n_ecc_evp_parse_params_point(&point_blob, &read_params));
223 /* Check that the point we read is the same we wrote */
224 EXPECT_TRUE(EVP_PKEY_cmp(write_params.evp_pkey, read_params.evp_pkey));
225
226 /* Clean up */
227 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&write_params));
228 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&read_params));
229 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
230 }
231 };
232 {
233 DEFER_CLEANUP(struct s2n_connection* conn = s2n_connection_new(S2N_CLIENT),
234 s2n_connection_ptr_free);
235 EXPECT_NOT_NULL(conn);
236 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
237 /* Test read/write/parse params for all supported curves */
238 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
239 struct s2n_ecc_evp_params write_params = { 0 };
240 struct s2n_ecc_evp_params read_params = { 0 };
241 struct s2n_stuffer wire = { 0 };
242 struct s2n_blob ecdh_params_sent, ecdh_params_received;
243
244 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 1024));
245
246 write_params.negotiated_curve = s2n_all_supported_curves_list[i];
247 read_params.negotiated_curve = s2n_all_supported_curves_list[i];
248
249 /* Server generates a key for a given curve */
250 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&write_params));
251 EXPECT_NOT_NULL(write_params.evp_pkey);
252
253 /* Write params points to wire */
254 EXPECT_SUCCESS(s2n_ecc_evp_write_params(&write_params, &wire, &ecdh_params_sent));
255 struct s2n_ecdhe_raw_server_params ecdhe_data = { 0 };
256
257 /* Read params points from the wire */
258 EXPECT_SUCCESS(s2n_ecc_evp_read_params(&wire, &ecdh_params_received, &ecdhe_data));
259 EXPECT_SUCCESS(s2n_ecc_evp_parse_params(conn, &ecdhe_data, &read_params));
260
261 /* Check that the point we read is the same we wrote */
262 EXPECT_TRUE(EVP_PKEY_cmp(write_params.evp_pkey, read_params.evp_pkey));
263
264 /* Clean up */
265 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
266 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&write_params));
267 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&read_params));
268 }
269 };
270 {
271 DEFER_CLEANUP(struct s2n_connection* conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
272 EXPECT_NOT_NULL(conn);
273 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
274 /* Test generate/read/write/parse and compute shared secrets for all supported curves */
275 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
276 struct s2n_ecc_evp_params server_params = { 0 };
277 struct s2n_ecc_evp_params read_params = { 0 };
278 struct s2n_ecc_evp_params client_params = { 0 };
279 struct s2n_stuffer wire = { 0 };
280 struct s2n_blob ecdh_params_sent, ecdh_params_received;
281 struct s2n_blob server_shared_secret, client_shared_secret;
282
283 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 1024));
284
285 server_params.negotiated_curve = s2n_all_supported_curves_list[i];
286 read_params.negotiated_curve = s2n_all_supported_curves_list[i];
287
288 /* Server generates a key for a given curve */
289 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_params));
290 EXPECT_NOT_NULL(server_params.evp_pkey);
291
292 /* Server sends the public */
293 EXPECT_SUCCESS(s2n_ecc_evp_write_params(&server_params, &wire, &ecdh_params_sent));
294
295 /* Client reads the public */
296 struct s2n_ecdhe_raw_server_params ecdhe_data = { 0 };
297 EXPECT_SUCCESS(s2n_ecc_evp_read_params(&wire, &ecdh_params_received, &ecdhe_data));
298 EXPECT_SUCCESS(s2n_ecc_evp_parse_params(conn, &ecdhe_data, &read_params));
299
300 /* Verify if the client correctly read the server public */
301 EXPECT_TRUE(EVP_PKEY_cmp(server_params.evp_pkey, read_params.evp_pkey));
302
303 /* Client generates its key for the given curve */
304 client_params.negotiated_curve = s2n_all_supported_curves_list[i];
305 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_params));
306 EXPECT_NOT_NULL(client_params.evp_pkey);
307
308 /* Compute shared secret for the server */
309 EXPECT_SUCCESS(
310 s2n_ecc_evp_compute_shared_secret_from_params(&server_params, &client_params, &server_shared_secret));
311
312 /* Compute shared secret for the client */
313 EXPECT_SUCCESS(
314 s2n_ecc_evp_compute_shared_secret_from_params(&client_params, &read_params, &client_shared_secret));
315
316 /* Verify that shared is the same for the client and the server */
317 EXPECT_EQUAL(client_shared_secret.size, server_shared_secret.size);
318 EXPECT_BYTEARRAY_EQUAL(client_shared_secret.data, server_shared_secret.data, client_shared_secret.size);
319
320 /* Clean up */
321 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
322 EXPECT_SUCCESS(s2n_free(&server_shared_secret));
323 EXPECT_SUCCESS(s2n_free(&client_shared_secret));
324 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&server_params));
325 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&read_params));
326 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&client_params));
327 }
328 };
329 {
330 DEFER_CLEANUP(struct s2n_connection* conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
331 EXPECT_NOT_NULL(conn);
332 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
333 /* Test generate->write->read->compute_shared with all supported curves */
334 for (size_t i = 0; i < s2n_all_supported_curves_list_len; i++) {
335 struct s2n_ecc_evp_params server_params = { 0 }, client_params = { 0 };
336 struct s2n_stuffer wire = { 0 };
337 struct s2n_blob server_shared, client_shared, ecdh_params_sent, ecdh_params_received;
338
339 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 1024));
340
341 /* Server generates a key for a given curve */
342 server_params.negotiated_curve = s2n_all_supported_curves_list[i];
343 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_params));
344 EXPECT_NOT_NULL(server_params.evp_pkey);
345 /* Server sends the public */
346 EXPECT_SUCCESS(s2n_ecc_evp_write_params(&server_params, &wire, &ecdh_params_sent));
347 /* Client reads the public */
348 struct s2n_ecdhe_raw_server_params ecdhe_data = { 0 };
349 EXPECT_SUCCESS(s2n_ecc_evp_read_params(&wire, &ecdh_params_received, &ecdhe_data));
350 EXPECT_SUCCESS(s2n_ecc_evp_parse_params(conn, &ecdhe_data, &client_params));
351
352 /* The client got the curve */
353 EXPECT_EQUAL(client_params.negotiated_curve, server_params.negotiated_curve);
354
355 /* Client sends its public */
356 EXPECT_SUCCESS(s2n_ecc_evp_compute_shared_secret_as_client(&client_params, &wire, &client_shared));
357 /* Server receives it */
358 EXPECT_SUCCESS(s2n_ecc_evp_compute_shared_secret_as_server(&server_params, &wire, &server_shared));
359 /* Shared is the same for the client and the server */
360 EXPECT_EQUAL(client_shared.size, server_shared.size);
361 EXPECT_BYTEARRAY_EQUAL(client_shared.data, server_shared.data, client_shared.size);
362
363 /* Clean up */
364 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
365 EXPECT_SUCCESS(s2n_free(&server_shared));
366 EXPECT_SUCCESS(s2n_free(&client_shared));
367 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&server_params));
368 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&client_params));
369 }
370 };
371
372 /* Test that the client does not negotiate a group that was not
373 * offered in EC preferences */
374 {
375 const struct s2n_security_policy* security_policy = NULL;
376 DEFER_CLEANUP(struct s2n_connection* conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
377 EXPECT_NOT_NULL(conn);
378 /* Version does not include the unsupported curve and secp521r1, which will be used by a malicious server */
379 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "20190802"));
380 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
381
382 /* Setup & verify invalid curves, which will be selected by a malicious server */
383 const struct s2n_ecc_named_curve* const unrequested_curves[] = {
384 &s2n_unsupported_curve,
385 &s2n_ecc_curve_secp521r1,
386 };
387
388 /* Verify that the client errors when the server attempts to
389 * negotiate a curve that was never offered */
390 for (size_t i = 0; i < s2n_array_len(unrequested_curves); i++) {
391 struct s2n_ecc_evp_params server_params = { 0 };
392 struct s2n_ecc_evp_params client_params = { 0 };
393 struct s2n_stuffer wire = { 0 };
394 struct s2n_blob ecdh_params_sent = { 0 }, ecdh_params_received = { 0 };
395
396 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire, 1024));
397
398 /* Server maliciously chooses an unsupported curve */
399 server_params.negotiated_curve = unrequested_curves[i];
400 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_params));
401 EXPECT_NOT_NULL(server_params.evp_pkey);
402 /* Server sends the public */
403 EXPECT_SUCCESS(s2n_ecc_evp_write_params(&server_params, &wire, &ecdh_params_sent));
404 /* Client reads the public */
405 struct s2n_ecdhe_raw_server_params ecdhe_data = { 0 };
406 EXPECT_SUCCESS(s2n_ecc_evp_read_params(&wire, &ecdh_params_received, &ecdhe_data));
407 EXPECT_FAILURE_WITH_ERRNO(
408 s2n_ecc_evp_parse_params(conn, &ecdhe_data, &client_params), S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
409
410 /* The client didn't agree on a curve */
411 EXPECT_NULL(client_params.negotiated_curve);
412
413 /* Clean up */
414 EXPECT_SUCCESS(s2n_stuffer_free(&wire));
415 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&server_params));
416 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&client_params));
417 }
418 };
419
420 /**
421 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.8.2
422 *= type=test
423 *# For the curves secp256r1, secp384r1, and secp521r1, peers MUST
424 *# validate each other's public value Q by ensuring that the point is a
425 *# valid point on the elliptic curve. The appropriate validation
426 *# procedures are defined in Section 4.3.7 of [ECDSA] and alternatively
427 *# in Section 5.6.2.3 of [KEYAGREEMENT]. This process consists of three
428 *# steps: (1) verify that Q is not the point at infinity (O), (2) verify
429 *# that for Q = (x, y) both integers x and y are in the correct
430 *# interval, and (3) ensure that (x, y) is a correct solution to the
431 *# elliptic curve equation. For these curves, implementors do not need
432 *# to verify membership in the correct subgroup.
433 *
434 * s2n-tls performs this validation by invoking the libcrypto APIs: EC_KEY_check_key, and
435 * EC_KEY_check_fips. To ensure that these APIs are properly called, step (1) is invalidated.
436 */
437 {
438 const struct s2n_ecc_named_curve* const nist_curves[] = {
439 &s2n_ecc_curve_secp256r1,
440 &s2n_ecc_curve_secp384r1,
441 &s2n_ecc_curve_secp521r1,
442 };
443
444 for (size_t i = 0; i < s2n_array_len(nist_curves); i++) {
445 const struct s2n_ecc_named_curve* curve = nist_curves[i];
446
447 DEFER_CLEANUP(struct s2n_ecc_evp_params server_params = { 0 }, s2n_ecc_evp_params_free);
448 DEFER_CLEANUP(struct s2n_ecc_evp_params client_params = { 0 }, s2n_ecc_evp_params_free);
449 DEFER_CLEANUP(struct s2n_blob shared_key = { 0 }, s2n_free);
450
451 /* Create a server key. */
452 server_params.negotiated_curve = curve;
453 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_params));
454 EXPECT_NOT_NULL(server_params.evp_pkey);
455
456 /* Create a client key. */
457 client_params.negotiated_curve = curve;
458 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_params));
459 EXPECT_NOT_NULL(client_params.evp_pkey);
460
461 /* Retrieve the existing client public key. */
462 DEFER_CLEANUP(EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(client_params.evp_pkey),
463 EC_KEY_free_pointer);
464 EXPECT_NOT_NULL(ec_key);
465 const EC_GROUP* group = EC_KEY_get0_group(ec_key);
466 EXPECT_NOT_NULL(group);
467 const EC_POINT* public_key = EC_KEY_get0_public_key(ec_key);
468 EXPECT_NOT_NULL(public_key);
469
470 /* Invalidate the public key by setting the coordinate to infinity. */
471 DEFER_CLEANUP(EC_POINT* invalid_public_key = EC_POINT_dup(public_key, group),
472 EC_POINT_free_pointer);
473 EXPECT_NOT_NULL(invalid_public_key);
474 EXPECT_EQUAL(EC_POINT_set_to_infinity(group, invalid_public_key), 1);
475 int ec_key_was_overwritten = EC_KEY_set_public_key(ec_key, invalid_public_key);
476 EXPECT_EQUAL(EVP_PKEY_set1_EC_KEY(client_params.evp_pkey, ec_key), 1);
477 /* Compute the server's shared secret. */
478 int ret = s2n_ecc_evp_compute_shared_secret_from_params(&server_params,
479 &client_params, &shared_key);
480
481 if (ec_key_was_overwritten) {
482 /* If s2n-tls is in FIPS mode and the libcrypto supports the EC_KEY_check_fips API,
483 * ensure that this API is called by checking for the correct error.
484 */
485 if (s2n_is_in_fips_mode() && s2n_ecc_evp_supports_fips_check()) {
486 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_ECDHE_INVALID_PUBLIC_KEY_FIPS);
487 } else {
488 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_ECDHE_INVALID_PUBLIC_KEY);
489 }
490 } else {
491 EXPECT_SUCCESS(ret);
492 }
493 }
494 }
495
496 END_TEST();
497 }