| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_libcrypto.h" |
| 17 |
#include "s2n_test.h" |
| 18 |
#include "testlib/s2n_testlib.h" |
| 19 |
#include "tls/s2n_certificate_keys.h" |
| 20 |
#include "tls/s2n_security_policies.h" |
| 21 |
#include "tls/s2n_signature_scheme.h" |
| 22 |
#include "utils/s2n_map.h" |
| 23 |
|
| 24 |
#define CHAIN_LENGTH 3 |
| 25 |
|
| 26 |
static S2N_RESULT s2n_test_construct_cert_chain( |
| 27 |
struct s2n_cert *certs, |
| 28 |
size_t certs_length, |
| 29 |
struct s2n_cert_chain *cert_chain, |
| 30 |
struct s2n_cert_chain_and_key *chain, |
| 31 |
const struct s2n_cert_info *valid_info) |
| 32 |
{ |
| 33 |
RESULT_ENSURE_REF(certs); |
| 34 |
RESULT_ENSURE_REF(cert_chain); |
| 35 |
RESULT_ENSURE_REF(chain); |
| 36 |
RESULT_ENSURE_REF(valid_info); |
| 37 |
|
| 38 |
for (size_t i = 0; i < certs_length; i++) { |
| 39 |
certs[i].info = *valid_info; |
| 40 |
if (i != certs_length - 1) { |
| 41 |
certs[i].next = &certs[i + 1]; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
certs[certs_length - 1].info.self_signed = true; |
| 47 |
|
| 48 |
cert_chain->head = &certs[0]; |
| 49 |
chain->cert_chain = cert_chain; |
| 50 |
|
| 51 |
return S2N_RESULT_OK; |
| 52 |
} |
| 53 |
|
| 54 |
int main(int argc, char **argv) |
| 55 |
{ |
| 56 |
BEGIN_TEST(); |
| 57 |
|
| 58 |
const int valid_sig_nid = NID_ecdsa_with_SHA256; |
| 59 |
const int valid_hash_nid = NID_sha256; |
| 60 |
const int valid_key_size = 384; |
| 61 |
const int valid_key_nid = NID_secp384r1; |
| 62 |
|
| 63 |
const int invalid_sig_nid = NID_sha256WithRSAEncryption; |
| 64 |
const int invalid_hash_nid = NID_sha1; |
| 65 |
const int invalid_key_size = 256; |
| 66 |
const int invalid_key_nid = NID_X9_62_prime256v1; |
| 67 |
|
| 68 |
const struct s2n_cert_info valid_info = { |
| 69 |
.self_signed = false, |
| 70 |
.signature_nid = valid_sig_nid, |
| 71 |
.signature_digest_nid = valid_hash_nid, |
| 72 |
.public_key_bits = valid_key_size, |
| 73 |
.public_key_nid = valid_key_nid, |
| 74 |
}; |
| 75 |
|
| 76 |
const struct s2n_signature_scheme *const test_sig_scheme_list[] = { |
| 77 |
&s2n_ecdsa_sha256, |
| 78 |
&s2n_rsa_pkcs1_sha1, |
| 79 |
}; |
| 80 |
|
| 81 |
const struct s2n_certificate_key *const test_cert_key_list[] = { |
| 82 |
&s2n_ec_p384, |
| 83 |
&s2n_rsa_rsae_3072, |
| 84 |
}; |
| 85 |
|
| 86 |
const struct s2n_signature_preferences test_certificate_signature_preferences = { |
| 87 |
.count = s2n_array_len(test_sig_scheme_list), |
| 88 |
.signature_schemes = test_sig_scheme_list, |
| 89 |
}; |
| 90 |
|
| 91 |
const struct s2n_certificate_key_preferences test_cert_key_preferences = { |
| 92 |
.count = s2n_array_len(test_cert_key_list), |
| 93 |
.certificate_keys = test_cert_key_list, |
| 94 |
}; |
| 95 |
|
| 96 |
const struct s2n_security_policy test_sp = { |
| 97 |
.certificate_signature_preferences = &test_certificate_signature_preferences, |
| 98 |
.certificate_key_preferences = &test_cert_key_preferences, |
| 99 |
.certificate_preferences_apply_locally = true, |
| 100 |
}; |
| 101 |
|
| 102 |
const struct s2n_signature_scheme *const pss_sig_scheme_list[] = { |
| 103 |
&s2n_rsa_pss_pss_sha256, |
| 104 |
&s2n_rsa_pss_pss_sha384, |
| 105 |
&s2n_rsa_pss_pss_sha512, |
| 106 |
&s2n_rsa_pss_rsae_sha256, |
| 107 |
&s2n_rsa_pss_rsae_sha384, |
| 108 |
&s2n_rsa_pss_rsae_sha512, |
| 109 |
}; |
| 110 |
|
| 111 |
const struct s2n_signature_preferences pss_certificate_signature_preferences = { |
| 112 |
.count = s2n_array_len(pss_sig_scheme_list), |
| 113 |
.signature_schemes = pss_sig_scheme_list, |
| 114 |
}; |
| 115 |
|
| 116 |
const struct s2n_security_policy test_pss_sp = { |
| 117 |
.certificate_signature_preferences = &pss_certificate_signature_preferences, |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
{ |
| 122 |
|
| 123 |
{ |
| 124 |
struct s2n_cert_info info = { |
| 125 |
.self_signed = false, |
| 126 |
.signature_digest_nid = NID_sha256, |
| 127 |
.signature_nid = NID_ecdsa_with_SHA256, |
| 128 |
}; |
| 129 |
|
| 130 |
EXPECT_OK(s2n_security_policy_validate_cert_signature(&test_sp, &info, |
| 131 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT)); |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
{ |
| 136 |
struct s2n_cert_info info = { |
| 137 |
.self_signed = false, |
| 138 |
.signature_digest_nid = NID_undef, |
| 139 |
.signature_nid = NID_rsassaPss, |
| 140 |
}; |
| 141 |
|
| 142 |
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_cert_signature(&test_sp, &info, |
| 143 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT), |
| 144 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
{ |
| 149 |
struct s2n_cert_info info = { |
| 150 |
.self_signed = false, |
| 151 |
.signature_digest_nid = NID_undef, |
| 152 |
.signature_nid = NID_rsassaPss, |
| 153 |
}; |
| 154 |
|
| 155 |
EXPECT_OK(s2n_security_policy_validate_cert_signature(&test_pss_sp, &info, |
| 156 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT)); |
| 157 |
}; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
{ |
| 162 |
|
| 163 |
{ |
| 164 |
struct s2n_cert_info info = { |
| 165 |
.public_key_nid = valid_key_nid, |
| 166 |
.public_key_bits = valid_key_size, |
| 167 |
}; |
| 168 |
|
| 169 |
EXPECT_OK(s2n_security_policy_validate_cert_key(&test_sp, &info, |
| 170 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT)); |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
{ |
| 175 |
struct s2n_cert_info info = { |
| 176 |
.public_key_nid = invalid_key_nid, |
| 177 |
.signature_nid = invalid_key_size, |
| 178 |
}; |
| 179 |
|
| 180 |
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_cert_key(&test_sp, &info, |
| 181 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT), |
| 182 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 183 |
}; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
{ |
| 188 |
|
| 189 |
{ |
| 190 |
struct s2n_cert certs[CHAIN_LENGTH] = { 0 }; |
| 191 |
struct s2n_cert_chain cert_chain = { 0 }; |
| 192 |
struct s2n_cert_chain_and_key chain = { 0 }; |
| 193 |
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info)); |
| 194 |
EXPECT_OK(s2n_security_policy_validate_certificate_chain(&test_sp, &chain)); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
for (size_t i = 0; i < CHAIN_LENGTH; i++) { |
| 199 |
|
| 200 |
{ |
| 201 |
struct s2n_cert certs[CHAIN_LENGTH] = { 0 }; |
| 202 |
struct s2n_cert_chain cert_chain = { 0 }; |
| 203 |
struct s2n_cert_chain_and_key chain = { 0 }; |
| 204 |
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info)); |
| 205 |
certs[i].info.signature_nid = invalid_sig_nid; |
| 206 |
certs[i].info.signature_digest_nid = invalid_hash_nid; |
| 207 |
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_certificate_chain(&test_sp, &chain), |
| 208 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
{ |
| 213 |
struct s2n_cert certs[CHAIN_LENGTH] = { 0 }; |
| 214 |
struct s2n_cert_chain cert_chain = { 0 }; |
| 215 |
struct s2n_cert_chain_and_key chain = { 0 }; |
| 216 |
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info)); |
| 217 |
certs[i].info.public_key_nid = invalid_key_nid; |
| 218 |
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_certificate_chain(&test_sp, &chain), |
| 219 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
{ |
| 224 |
struct s2n_cert certs[CHAIN_LENGTH] = { 0 }; |
| 225 |
struct s2n_cert_chain cert_chain = { 0 }; |
| 226 |
struct s2n_cert_chain_and_key chain = { 0 }; |
| 227 |
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info)); |
| 228 |
certs[i].info.public_key_bits = invalid_key_size; |
| 229 |
EXPECT_ERROR_WITH_ERRNO(s2n_security_policy_validate_certificate_chain(&test_sp, &chain), |
| 230 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
{ |
| 235 |
struct s2n_cert certs[CHAIN_LENGTH] = { 0 }; |
| 236 |
struct s2n_cert_chain cert_chain = { 0 }; |
| 237 |
struct s2n_cert_chain_and_key chain = { 0 }; |
| 238 |
EXPECT_OK(s2n_test_construct_cert_chain(certs, CHAIN_LENGTH, &cert_chain, &chain, &valid_info)); |
| 239 |
|
| 240 |
struct s2n_security_policy test_sp_no_local = test_sp; |
| 241 |
test_sp_no_local.certificate_preferences_apply_locally = false; |
| 242 |
|
| 243 |
certs[i].info.signature_nid = invalid_sig_nid; |
| 244 |
certs[i].info.public_key_nid = invalid_key_nid; |
| 245 |
EXPECT_OK(s2n_security_policy_validate_certificate_chain(&test_sp_no_local, &chain)); |
| 246 |
}; |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
{ |
| 252 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *invalid_cert = NULL, s2n_cert_chain_and_key_ptr_free); |
| 253 |
EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&invalid_cert, "rsae", "pss", "4096", "sha384")); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
{ |
| 259 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 260 |
EXPECT_NOT_NULL(config); |
| 261 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 262 |
EXPECT_NOT_NULL(conn); |
| 263 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, invalid_cert)); |
| 264 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 265 |
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_cipher_preferences(conn, "rfc9151"), |
| 266 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
|
| 271 |
{ |
| 272 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *invalid_cert = NULL, s2n_cert_chain_and_key_ptr_free); |
| 273 |
EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&invalid_cert, |
| 274 |
"ec", "ecdsa", "p384", "sha256")); |
| 275 |
|
| 276 |
|
| 277 |
{ |
| 278 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 279 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "rfc9151")); |
| 280 |
|
| 281 |
EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_cert_chain_and_key_to_store(config, invalid_cert), |
| 282 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 283 |
|
| 284 |
|
| 285 |
uint32_t domain_certs = 0; |
| 286 |
EXPECT_EQUAL(s2n_config_get_num_default_certs(config), 0); |
| 287 |
EXPECT_OK(s2n_map_size(config->domain_name_to_cert_map, &domain_certs)); |
| 288 |
EXPECT_EQUAL(domain_certs, 0); |
| 289 |
EXPECT_EQUAL(s2n_config_get_num_default_certs(config), 0); |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
{ |
| 294 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 295 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, invalid_cert)); |
| 296 |
const struct s2n_security_policy *default_sp = config->security_policy; |
| 297 |
EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_cipher_preferences(config, "rfc9151"), |
| 298 |
S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT); |
| 299 |
|
| 300 |
|
| 301 |
EXPECT_EQUAL(config->security_policy, default_sp); |
| 302 |
}; |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if (!s2n_libcrypto_is_openssl_fips()) { |
| 319 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *cert = NULL, s2n_cert_chain_and_key_ptr_free); |
| 320 |
|
| 321 |
EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&cert, "rsae", "pkcs", "1024", "sha1")); |
| 322 |
|
| 323 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 324 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert)); |
| 325 |
EXPECT_EQUAL(s2n_config_get_num_default_certs(config), 1); |
| 326 |
}; |
| 327 |
|
| 328 |
END_TEST(); |
| 329 |
return S2N_SUCCESS; |
| 330 |
} |