Debian Codegraph @

crt/s2n/tests/unit/s2n_key_share_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 <stdint.h>
17
18 #include "crypto/s2n_ecc_evp.h"
19 #include "s2n_test.h"
20 #include "stuffer/s2n_stuffer.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tls/extensions/s2n_key_share.h"
23 #include "tls/s2n_tls.h"
24 #include "utils/s2n_safety.h"
25
26 int main(int argc, char **argv)
27 {
28 BEGIN_TEST();
29 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
30 const struct s2n_ecc_named_curve *test_curve = s2n_all_supported_curves_list[0];
31
32 /* Test s2n_ecdhe_parameters_send write with valid ecc params */
33 {
34 struct s2n_stuffer out = { 0 };
35
36 struct s2n_ecc_evp_params ecc_evp_params;
37 ecc_evp_params.negotiated_curve = test_curve;
38 ecc_evp_params.evp_pkey = NULL;
39
40 EXPECT_SUCCESS(s2n_stuffer_alloc(&out, test_curve->share_size + 4));
41 EXPECT_SUCCESS(s2n_ecdhe_parameters_send(&ecc_evp_params, &out));
42 S2N_STUFFER_READ_EXPECT_EQUAL(&out, test_curve->iana_id, uint16);
43 S2N_STUFFER_READ_EXPECT_EQUAL(&out, test_curve->share_size, uint16);
44 EXPECT_EQUAL(s2n_stuffer_data_available(&out), test_curve->share_size);
45
46 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&ecc_evp_params));
47 EXPECT_SUCCESS(s2n_stuffer_free(&out));
48 };
49
50 /* Test s2n_ecdhe_parameters_send failure with bad ecc params */
51 {
52 struct s2n_stuffer out = { 0 };
53
54 struct s2n_ecc_evp_params ecc_evp_params;
55 const struct s2n_ecc_named_curve bad_curve = {
56 .iana_id = 12345,
57 .libcrypto_nid = 0,
58 .name = test_curve->name,
59 .share_size = test_curve->share_size
60 };
61
62 ecc_evp_params.negotiated_curve = &bad_curve;
63 ecc_evp_params.evp_pkey = NULL;
64
65 EXPECT_SUCCESS(s2n_stuffer_alloc(&out, bad_curve.share_size + 4));
66 /* generating an ECDHE key should fail */
67 EXPECT_FAILURE(s2n_ecdhe_parameters_send(&ecc_evp_params, &out));
68
69 EXPECT_SUCCESS(s2n_ecc_evp_params_free(&ecc_evp_params));
70 EXPECT_SUCCESS(s2n_stuffer_free(&out));
71 };
72
73 END_TEST();
74 }