-
Notifications
You must be signed in to change notification settings - Fork 697
/
s2n_security_policy_cert_preferences_test.c
293 lines (251 loc) · 11.7 KB
/
s2n_security_policy_cert_preferences_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_certificate_keys.h"
#include "tls/s2n_security_policies.h"
#include "tls/s2n_signature_scheme.h"
#define CHAIN_LENGTH 3
static S2N_RESULT s2n_test_construct_cert_chain(
struct s2n_cert *certs,
size_t certs_length,
struct s2n_cert_chain *cert_chain,
struct s2n_cert_chain_and_key *chain,
const struct s2n_cert_info *valid_info)
{
RESULT_ENSURE_REF(certs);
RESULT_ENSURE_REF(cert_chain);
RESULT_ENSURE_REF(chain);
RESULT_ENSURE_REF(valid_info);
for (size_t i = 0; i < certs_length; i++) {
certs[i].info = *valid_info;
if (i != certs_length - 1) {
certs[i].next = &certs[i + 1];
}
}
/* root cert */
certs[certs_length - 1].info.self_signed = true;
cert_chain->head = &certs[0];
chain->cert_chain = cert_chain;
return S2N_RESULT_OK;
}
int main(int argc, char **argv)
{
BEGIN_TEST();
const int valid_sig_nid = NID_ecdsa_with_SHA256;
const int valid_hash_nid = NID_sha256;
const int valid_key_size = 384;
const int valid_key_nid = NID_secp384r1;
const int invalid_sig_nid = NID_sha256WithRSAEncryption;
const int invalid_hash_nid = NID_sha1;
const int invalid_key_size = 256;
const int invalid_key_nid = NID_X9_62_prime256v1;
const struct s2n_cert_info valid_info = {
.self_signed = false,
.signature_nid = valid_sig_nid,
.signature_digest_nid = valid_hash_nid,
.public_key_bits = valid_key_size,
.public_key_nid = valid_key_nid,
};
const struct s2n_signature_scheme *const test_sig_scheme_list[] = {
&s2n_ecdsa_sha256,
&s2n_rsa_pkcs1_sha1,
};
const struct s2n_certificate_key *const test_cert_key_list[] = {
&s2n_ec_p384,
&s2n_rsa_rsae_3072,
};
const struct s2n_signature_preferences test_certificate_signature_preferences = {
.count = s2n_array_len(test_sig_scheme_list),
.signature_schemes = test_sig_scheme_list,
};
const struct s2n_certificate_key_preferences test_cert_key_preferences = {
.count = s2n_array_len(test_cert_key_list),
.certificate_keys = test_cert_key_list,
};
const struct s2n_security_policy test_sp = {
.certificate_signature_preferences = &test_certificate_signature_preferences,
.certificate_key_preferences = &test_cert_key_preferences,
.certificate_preferences_apply_locally = true,
};
const struct s2n_signature_scheme *const pss_sig_scheme_list[] = {
&s2n_rsa_pss_pss_sha256,
&s2n_rsa_pss_pss_sha384,
&s2n_rsa_pss_pss_sha512,
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_rsae_sha384,
&s2n_rsa_pss_rsae_sha512,
};
const struct s2n_signature_preferences pss_certificate_signature_preferences = {
.count = s2n_array_len(pss_sig_scheme_list),
.signature_schemes = pss_sig_scheme_list,
};
const struct s2n_security_policy test_pss_sp = {
.certificate_signature_preferences = &pss_certificate_signature_preferences,
};
/* s2n_security_policy_validate_cert_signature() */
{
/* Certificate signature algorithm is in test certificate signature preferences list */
{
struct s2n_cert_info info = {
.self_signed = false,
.signature_digest_nid = NID_sha256,
.signature_nid = NID_ecdsa_with_SHA256,
};
EXPECT_OK(s2n_security_policy_validate_cert_signature(&test_sp, &info,
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT));
};
/* Certificate signature algorithm is not in test certificate signature preferences list */
{
struct s2n_cert_info info = {
.self_signed = false,
.signature_digest_nid = NID_undef,
.signature_nid = NID_rsassaPss,
};
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_cert_signature(&test_sp, &info,
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT),
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
};
/* Certificates signed with an RSA PSS signature can be validated */
{
struct s2n_cert_info info = {
.self_signed = false,
.signature_digest_nid = NID_undef,
.signature_nid = NID_rsassaPss,
};
EXPECT_OK(s2n_security_policy_validate_cert_signature(&test_pss_sp, &info,
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT));
};
};
/* s2n_security_policy_validate_cert_key() */
{
/* Certificate key is in test certificate key list */
{
struct s2n_cert_info info = {
.public_key_nid = valid_key_nid,
.public_key_bits = valid_key_size,
};
EXPECT_OK(s2n_security_policy_validate_cert_key(&test_sp, &info,
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT));
};
/* Certificate key is not in test certificate key list */
{
struct s2n_cert_info info = {
.public_key_nid = invalid_key_nid,
.signature_nid = invalid_key_size,
};
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_cert_key(&test_sp, &info,
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT),
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
};
};
/* s2n_security_policy_validate_certificate_chain() */
{
/* a valid certificate chain passes validation */
{
struct s2n_cert certs[CHAIN_LENGTH] = { 0 };
struct s2n_cert_chain cert_chain = { 0 };
struct s2n_cert_chain_and_key chain = { 0 };
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info));
EXPECT_OK(s2n_security_policy_validate_certificate_chain(&test_sp, &chain));
};
/* test that failures can be detected for any cert in the chain */
for (size_t i = 0; i < CHAIN_LENGTH; i++) {
/* an invalid signature causes a failure */
{
struct s2n_cert certs[CHAIN_LENGTH] = { 0 };
struct s2n_cert_chain cert_chain = { 0 };
struct s2n_cert_chain_and_key chain = { 0 };
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info));
certs[i].info.signature_nid = invalid_sig_nid;
certs[i].info.signature_digest_nid = invalid_hash_nid;
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_certificate_chain(&test_sp, &chain),
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
};
/* an invalid key nid causes a failure */
{
struct s2n_cert certs[CHAIN_LENGTH] = { 0 };
struct s2n_cert_chain cert_chain = { 0 };
struct s2n_cert_chain_and_key chain = { 0 };
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info));
certs[i].info.public_key_nid = invalid_key_nid;
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_certificate_chain(&test_sp, &chain),
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
};
/* an invalid key size causes a failure */
{
struct s2n_cert certs[CHAIN_LENGTH] = { 0 };
struct s2n_cert_chain cert_chain = { 0 };
struct s2n_cert_chain_and_key chain = { 0 };
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info));
certs[i].info.public_key_bits = invalid_key_size;
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_certificate_chain(&test_sp, &chain),
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
};
/* when certificate_preferences_apply_locally is false then validation succeeds */
{
struct s2n_cert certs[CHAIN_LENGTH] = { 0 };
struct s2n_cert_chain cert_chain = { 0 };
struct s2n_cert_chain_and_key chain = { 0 };
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info));
struct s2n_security_policy test_sp_no_local = test_sp;
test_sp_no_local.certificate_preferences_apply_locally = false;
certs[i].info.signature_nid = invalid_sig_nid;
certs[i].info.public_key_nid = invalid_key_nid;
EXPECT_OK(s2n_security_policy_validate_certificate_chain(&test_sp_no_local, &chain));
};
}
};
/* s2n_connection_set_cipher_preferences */
{
DEFER_CLEANUP(struct s2n_cert_chain_and_key *invalid_cert = NULL, s2n_cert_chain_and_key_ptr_free);
EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&invalid_cert, "rsae", "pss", "4096", "sha384"));
struct s2n_security_policy rfc9151_applied_locally = security_policy_rfc9151;
rfc9151_applied_locally.certificate_preferences_apply_locally = true;
/* s2n_connection_set_cipher_preferences looks up the security policy from the security_policy_selection table
* but none of our current security policies apply certificate preferences locally. So instead we rewrite the
* rfc9151 policy from the table to apply cert preference locally. */
struct s2n_security_policy_selection *rfc9151_selection = NULL;
const struct s2n_security_policy *original_rfc9151 = NULL;
for (int i = 0; security_policy_selection[i].version != NULL; i++) {
if (strcasecmp("rfc9151", security_policy_selection[i].version) == 0) {
rfc9151_selection = &security_policy_selection[i];
break;
}
}
if (rfc9151_selection == NULL) {
FAIL_MSG("unable to find expected security policy");
}
original_rfc9151 = rfc9151_selection->security_policy;
rfc9151_selection->security_policy = &rfc9151_applied_locally;
/* when certificate preferences apply locally and the connection contains
* an invalid config then s2n_connection_set_cipher_preferences fails
*/
{
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(config);
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, invalid_cert));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_cipher_preferences(conn, "rfc9151"),
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
}
/* restore security_policy_selection */
rfc9151_selection->security_policy = original_rfc9151;
};
END_TEST();
return S2N_SUCCESS;
}