Debian Codegraph @

crt/s2n/tests/unit/s2n_kex_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 "tls/s2n_kex.h"
17
18 #include <openssl/bn.h>
19 #include <openssl/dh.h>
20
21 #include "crypto/s2n_dhe.h"
22 #include "tests/s2n_test.h"
23
24 /* Test DH parameters (2048-bit prime from RFC 3526) */
25 static const char dhparams_pem[] =
26 "-----BEGIN DH PARAMETERS-----\n"
27 "MIIBCAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
28 "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
29 "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
30 "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
31 "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
32 "5RXSJhiY+gUQFXKOWoqsqmj//////////wIBAg==\n"
33 "-----END DH PARAMETERS-----\n";
34
35 int main(int argc, char **argv)
36 {
37 BEGIN_TEST();
38 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
39
40 /* Test safety checks */
41 {
42 struct s2n_connection conn = { 0 };
43 struct s2n_blob blob = { 0 };
44 struct s2n_kex_raw_server_data test_raw_server_data = { 0 };
45 struct s2n_cipher_suite test_cipher = s2n_ecdhe_rsa_with_aes_256_cbc_sha;
46 struct s2n_cipher_suite test_cipher_with_null_kex = test_cipher;
47 test_cipher_with_null_kex.key_exchange_alg = NULL;
48
49 /* Null cipher suite kex - possible with tls1.3 cipher suites */
50 EXPECT_ERROR(s2n_configure_kex(NULL, &conn));
51 EXPECT_ERROR(s2n_configure_kex(&test_cipher_with_null_kex, NULL));
52
53 /* Null kex -- possible with tls1.3 cipher suites */
54 bool is_ephemeral = false;
55 EXPECT_ERROR(s2n_kex_is_ephemeral(NULL, &is_ephemeral));
56 EXPECT_ERROR(s2n_kex_is_ephemeral(&s2n_rsa, NULL));
57 EXPECT_ERROR(s2n_kex_server_key_recv_parse_data(NULL, &conn, &test_raw_server_data));
58 EXPECT_ERROR(s2n_kex_server_key_recv_read_data(NULL, &conn, &blob, &test_raw_server_data));
59 EXPECT_ERROR(s2n_kex_server_key_send(NULL, &conn, &blob));
60 EXPECT_ERROR(s2n_kex_client_key_recv(NULL, &conn, &blob));
61 EXPECT_ERROR(s2n_kex_client_key_send(NULL, &conn, &blob));
62 EXPECT_ERROR(s2n_kex_tls_prf(NULL, &conn, &blob));
63 };
64
65 /* Test s2n_kex_includes */
66 {
67 /* True if same kex */
68 EXPECT_TRUE(s2n_kex_includes(NULL, NULL));
69 EXPECT_TRUE(s2n_kex_includes(&s2n_rsa, &s2n_rsa));
70 EXPECT_TRUE(s2n_kex_includes(&s2n_hybrid_ecdhe_kem, &s2n_hybrid_ecdhe_kem));
71
72 /* False if different kex */
73 EXPECT_FALSE(s2n_kex_includes(&s2n_rsa, &s2n_dhe));
74 EXPECT_FALSE(s2n_kex_includes(&s2n_kem, &s2n_ecdhe));
75
76 /* True if hybrid that contains */
77 EXPECT_TRUE(s2n_kex_includes(&s2n_hybrid_ecdhe_kem, &s2n_ecdhe));
78 EXPECT_TRUE(s2n_kex_includes(&s2n_hybrid_ecdhe_kem, &s2n_kem));
79
80 /* False if hybrid "contains" relationship reversed */
81 EXPECT_FALSE(s2n_kex_includes(&s2n_ecdhe, &s2n_hybrid_ecdhe_kem));
82 EXPECT_FALSE(s2n_kex_includes(&s2n_kem, &s2n_hybrid_ecdhe_kem));
83
84 /* False if hybrid that does not contain */
85 EXPECT_FALSE(s2n_kex_includes(&s2n_hybrid_ecdhe_kem, &s2n_rsa));
86 EXPECT_FALSE(s2n_kex_includes(&s2n_hybrid_ecdhe_kem, &s2n_dhe));
87
88 /* False if one kex null */
89 EXPECT_FALSE(s2n_kex_includes(&s2n_rsa, NULL));
90 EXPECT_FALSE(s2n_kex_includes(NULL, &s2n_rsa));
91 };
92
93 /* DHE Test: Client sends Yc_length larger than server DH params size */
94 {
95 struct s2n_dh_params server_dh_params = { 0 };
96 struct s2n_blob shared_key = { 0 };
97 struct s2n_stuffer Yc_in = { 0 };
98 struct s2n_stuffer dhparams_in = { 0 };
99 struct s2n_stuffer dhparams_out = { 0 };
100
101 /* Setup server DH params (2048-bit = 256 bytes) */
102 EXPECT_SUCCESS(s2n_stuffer_alloc(&dhparams_in, sizeof(dhparams_pem)));
103 EXPECT_SUCCESS(s2n_stuffer_alloc(&dhparams_out, sizeof(dhparams_pem)));
104 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&dhparams_in, (uint8_t *) dhparams_pem, sizeof(dhparams_pem)));
105 EXPECT_SUCCESS(s2n_stuffer_dhparams_from_pem(&dhparams_in, &dhparams_out));
106
107 uint32_t available_size = s2n_stuffer_data_available(&dhparams_out);
108 struct s2n_blob dhparams_blob = { 0 };
109 EXPECT_SUCCESS(s2n_blob_init(&dhparams_blob, s2n_stuffer_raw_read(&dhparams_out, available_size), available_size));
110
111 /* s2n_pkcs3_to_dh_params calls DH_check() which has different behavior
112 * in some libcrypto versions (e.g. OpenSSL 1.0.2, AWS-LC FIPS 2022).
113 * Skip this test if DH param validation fails. */
114 if (s2n_pkcs3_to_dh_params(&server_dh_params, &dhparams_blob) == S2N_SUCCESS) {
115 EXPECT_SUCCESS(s2n_dh_generate_ephemeral_key(&server_dh_params));
116 int server_dh_size = DH_size(server_dh_params.dh);
117 EXPECT_EQUAL(server_dh_size, 256); /* 2048 bits = 256 bytes */
118
119 /* Allocate stuffer for client-controlled input */
120 EXPECT_SUCCESS(s2n_stuffer_alloc(&Yc_in, 1024));
121 /* Client sends Yc_length = 512 (larger than server_dh_size = 256) */
122 uint16_t malicious_Yc_length = 512;
123 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&Yc_in, malicious_Yc_length));
124
125 /* Fill with dummy data to satisfy stuffer bounds check */
126 for (int i = 0; i < malicious_Yc_length; i++) {
127 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&Yc_in, 0xFF));
128 }
129 /* Verify the oversized input: Yc_length (512) > server_dh_size (256) */
130 EXPECT_TRUE(malicious_Yc_length > server_dh_size);
131
132 /* This function should fail due to the bound check */
133 EXPECT_FAILURE_WITH_ERRNO(s2n_dh_compute_shared_secret_as_server(&server_dh_params, &Yc_in, &shared_key),
134 S2N_ERR_DH_SHARED_SECRET);
135
136 /* Cleanup */
137 EXPECT_SUCCESS(s2n_free(&shared_key));
138 EXPECT_SUCCESS(s2n_stuffer_free(&Yc_in));
139 EXPECT_SUCCESS(s2n_dh_params_free(&server_dh_params));
140 }
141
142 EXPECT_SUCCESS(s2n_stuffer_free(&dhparams_in));
143 EXPECT_SUCCESS(s2n_stuffer_free(&dhparams_out));
144 };
145
146 /* DHE Test: DH public key range validation per RFC 2631 ยง2.1.5
147 * Valid range is [2, p-1]; values outside must be rejected. */
148 {
149 struct s2n_stuffer dhparams_in = { 0 };
150 struct s2n_stuffer dhparams_out = { 0 };
151 struct s2n_dh_params base_params = { 0 };
152
153 EXPECT_SUCCESS(s2n_stuffer_alloc(&dhparams_in, sizeof(dhparams_pem)));
154 EXPECT_SUCCESS(s2n_stuffer_alloc(&dhparams_out, sizeof(dhparams_pem)));
155 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&dhparams_in, (uint8_t *) dhparams_pem, sizeof(dhparams_pem)));
156 EXPECT_SUCCESS(s2n_stuffer_dhparams_from_pem(&dhparams_in, &dhparams_out));
157
158 uint32_t available_size = s2n_stuffer_data_available(&dhparams_out);
159 struct s2n_blob dhparams_blob = { 0 };
160 EXPECT_SUCCESS(s2n_blob_init(&dhparams_blob, s2n_stuffer_raw_read(&dhparams_out, available_size), available_size));
161
162 /* Skip if DH param validation fails (libcrypto-dependent) */
163 if (s2n_pkcs3_to_dh_params(&base_params, &dhparams_blob) == S2N_SUCCESS) {
164 EXPECT_SUCCESS(s2n_dh_generate_ephemeral_key(&base_params));
165
166 struct s2n_stuffer pgy_out = { 0 };
167 struct s2n_blob pgy_blob = { 0 };
168 EXPECT_SUCCESS(s2n_stuffer_alloc(&pgy_out, 1024));
169 EXPECT_SUCCESS(s2n_dh_params_to_p_g_Ys(&base_params, &pgy_out, &pgy_blob));
170
171 /* Parse serialized format: [uint16 p_len][p][uint16 g_len][g][uint16 Ys_len][Ys] */
172 uint16_t p_len = 0;
173 uint16_t g_len = 0;
174 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&pgy_out, &p_len));
175 uint8_t *p_data = s2n_stuffer_raw_read(&pgy_out, p_len);
176 EXPECT_NOT_NULL(p_data);
177 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&pgy_out, &g_len));
178 uint8_t *g_data = s2n_stuffer_raw_read(&pgy_out, g_len);
179 EXPECT_NOT_NULL(g_data);
180
181 struct s2n_blob p_blob = { 0 };
182 struct s2n_blob g_blob = { 0 };
183 EXPECT_SUCCESS(s2n_blob_init(&p_blob, p_data, p_len));
184 EXPECT_SUCCESS(s2n_blob_init(&g_blob, g_data, g_len));
185
186 /* Get p as a BIGNUM for constructing boundary test values */
187 BIGNUM *bn_p = BN_bin2bn(p_data, p_len, NULL);
188 EXPECT_NOT_NULL(bn_p);
189 struct {
190 BIGNUM *ys;
191 bool expect_success;
192 } test_cases[] = {
193 { .ys = BN_new(), .expect_success = false }, /* Ys = 1 (below valid range) */
194 { .ys = BN_new(), .expect_success = true }, /* Ys = 2 (min valid) */
195 { .ys = BN_dup(bn_p), .expect_success = true }, /* Ys = p-1 (max valid) */
196 { .ys = BN_dup(bn_p), .expect_success = false }, /* Ys = p (above valid range) */
197 };
198 BN_set_word(test_cases[0].ys, 1);
199 BN_set_word(test_cases[1].ys, 2);
200 BN_sub_word(test_cases[2].ys, 1);
201 /* test_cases[3].ys is already p */
202
203 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
204 uint8_t ys_buf[256] = { 0 };
205 int ys_len = BN_bn2bin(test_cases[i].ys, ys_buf);
206 struct s2n_blob ys_blob = { 0 };
207 EXPECT_SUCCESS(s2n_blob_init(&ys_blob, ys_buf, ys_len));
208
209 struct s2n_dh_params test_params = { 0 };
210 if (test_cases[i].expect_success) {
211 EXPECT_SUCCESS(s2n_dh_p_g_Ys_to_dh_params(&test_params, &p_blob, &g_blob, &ys_blob));
212 } else {
213 EXPECT_FAILURE_WITH_ERRNO(
214 s2n_dh_p_g_Ys_to_dh_params(&test_params, &p_blob, &g_blob, &ys_blob),
215 S2N_ERR_DH_PARAMS_CREATE);
216 }
217 EXPECT_SUCCESS(s2n_dh_params_free(&test_params));
218 BN_free(test_cases[i].ys);
219 }
220 BN_free(bn_p);
221 EXPECT_SUCCESS(s2n_stuffer_free(&pgy_out));
222 }
223 EXPECT_SUCCESS(s2n_dh_params_free(&base_params));
224 EXPECT_SUCCESS(s2n_stuffer_free(&dhparams_in));
225 EXPECT_SUCCESS(s2n_stuffer_free(&dhparams_out));
226 };
227
228 END_TEST();
229 }