Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "path_builder.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 9 | #include <openssl/pool.h> |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 10 | #include "cert_issuer_source_static.h" |
| 11 | #include "common_cert_errors.h" |
| 12 | #include "crl.h" |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 13 | #include "encode_values.h" |
| 14 | #include "input.h" |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 15 | #include "parse_certificate.h" |
| 16 | #include "parsed_certificate.h" |
| 17 | #include "simple_path_builder_delegate.h" |
| 18 | #include "trust_store_in_memory.h" |
| 19 | #include "verify_certificate_chain.h" |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 20 | |
| 21 | #include "nist_pkits_unittest.h" |
| 22 | |
| 23 | constexpr int64_t kOneYear = 60 * 60 * 24 * 365; |
| 24 | |
| 25 | namespace bssl { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class CrlCheckingPathBuilderDelegate : public SimplePathBuilderDelegate { |
| 30 | public: |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 31 | CrlCheckingPathBuilderDelegate(const std::vector<std::string> &der_crls, |
| 32 | int64_t verify_time, int64_t max_age, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 33 | size_t min_rsa_modulus_length_bits, |
| 34 | DigestPolicy digest_policy) |
| 35 | : SimplePathBuilderDelegate(min_rsa_modulus_length_bits, digest_policy), |
| 36 | der_crls_(der_crls), |
| 37 | verify_time_(verify_time), |
| 38 | max_age_(max_age) {} |
| 39 | |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 40 | void CheckPathAfterVerification(const CertPathBuilder &path_builder, |
| 41 | CertPathBuilderResultPath *path) override { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 42 | SimplePathBuilderDelegate::CheckPathAfterVerification(path_builder, path); |
| 43 | |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 44 | if (!path->IsValid()) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 45 | return; |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 46 | } |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 47 | |
| 48 | // It would be preferable if this test could use |
| 49 | // CheckValidatedChainRevocation somehow, but that only supports getting |
| 50 | // CRLs by http distributionPoints. So this just settles for writing a |
| 51 | // little bit of wrapper code to test CheckCRL directly. |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 52 | const ParsedCertificateList &certs = path->certs; |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 53 | for (size_t reverse_i = 0; reverse_i < certs.size(); ++reverse_i) { |
| 54 | size_t i = certs.size() - reverse_i - 1; |
| 55 | |
| 56 | // Trust anchors bypass OCSP/CRL revocation checks. (The only way to |
| 57 | // revoke trust anchors is via CRLSet or the built-in SPKI block list). |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 58 | if (reverse_i == 0 && path->last_cert_trust.IsTrustAnchor()) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 59 | continue; |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 60 | } |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 61 | |
| 62 | // RFC 5280 6.3.3. [If the CRL was not specified in a distribution |
| 63 | // point], assume a DP with both the reasons and the |
| 64 | // cRLIssuer fields omitted and a distribution point |
| 65 | // name of the certificate issuer. |
| 66 | // Since this implementation only supports URI names in distribution |
| 67 | // points, this means a default-initialized ParsedDistributionPoint is |
| 68 | // sufficient. |
| 69 | ParsedDistributionPoint fake_cert_dp; |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 70 | const ParsedDistributionPoint *cert_dp = &fake_cert_dp; |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 71 | |
| 72 | // If the target cert does have a distribution point, use it. |
| 73 | std::vector<ParsedDistributionPoint> distribution_points; |
| 74 | ParsedExtension crl_dp_extension; |
| 75 | if (certs[i]->GetExtension(der::Input(kCrlDistributionPointsOid), |
| 76 | &crl_dp_extension)) { |
| 77 | ASSERT_TRUE(ParseCrlDistributionPoints(crl_dp_extension.value, |
| 78 | &distribution_points)); |
| 79 | // TODO(mattm): some test cases (some of the 4.14.* onlySomeReasons |
| 80 | // tests)) have two CRLs and two distribution points, one point |
| 81 | // corresponding to each CRL. Should select the matching point for |
| 82 | // each CRL. (Doesn't matter currently since we don't support |
| 83 | // reasons.) |
| 84 | |
| 85 | // Look for a DistributionPoint without reasons. |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 86 | for (const auto &dp : distribution_points) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 87 | if (!dp.reasons) { |
| 88 | cert_dp = &dp; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | // If there were only DistributionPoints with reasons, just use the |
| 93 | // first one. |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 94 | if (cert_dp == &fake_cert_dp && !distribution_points.empty()) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 95 | cert_dp = &distribution_points[0]; |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 96 | } |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool cert_good = false; |
| 100 | |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 101 | for (const auto &der_crl : der_crls_) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 102 | CRLRevocationStatus crl_status = |
| 103 | CheckCRL(der_crl, certs, i, *cert_dp, verify_time_, max_age_); |
| 104 | if (crl_status == CRLRevocationStatus::REVOKED) { |
| 105 | path->errors.GetErrorsForCert(i)->AddError( |
| 106 | cert_errors::kCertificateRevoked); |
| 107 | return; |
| 108 | } |
| 109 | if (crl_status == CRLRevocationStatus::GOOD) { |
| 110 | cert_good = true; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | if (!cert_good) { |
| 115 | // PKITS tests assume hard-fail revocation checking. |
| 116 | // From PKITS 4.4: "When running the tests in this section, the |
| 117 | // application should be configured in such a way that the |
| 118 | // certification path is not accepted unless valid, up-to-date |
| 119 | // revocation data is available for every certificate in the path." |
| 120 | path->errors.GetErrorsForCert(i)->AddError( |
| 121 | cert_errors::kUnableToCheckRevocation); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | std::vector<std::string> der_crls_; |
| 128 | int64_t verify_time_; |
| 129 | int64_t max_age_; |
| 130 | }; |
| 131 | |
| 132 | class PathBuilderPkitsTestDelegate { |
| 133 | public: |
| 134 | static void RunTest(std::vector<std::string> cert_ders, |
| 135 | std::vector<std::string> crl_ders, |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 136 | const PkitsTestInfo &orig_info) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 137 | PkitsTestInfo info = orig_info; |
| 138 | |
| 139 | ASSERT_FALSE(cert_ders.empty()); |
| 140 | ParsedCertificateList certs; |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 141 | for (const std::string &der : cert_ders) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 142 | CertErrors errors; |
| 143 | ASSERT_TRUE(ParsedCertificate::CreateAndAddToVector( |
| 144 | bssl::UniquePtr<CRYPTO_BUFFER>( |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 145 | CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(der.data()), |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 146 | der.size(), nullptr)), |
| 147 | {}, &certs, &errors)) |
| 148 | << errors.ToDebugString(); |
| 149 | } |
| 150 | // First entry in the PKITS chain is the trust anchor. |
| 151 | // TODO(mattm): test with all possible trust anchors in the trust store? |
| 152 | TrustStoreInMemory trust_store; |
| 153 | |
| 154 | trust_store.AddTrustAnchor(certs[0]); |
| 155 | |
| 156 | // TODO(mattm): test with other irrelevant certs in cert_issuer_sources? |
| 157 | CertIssuerSourceStatic cert_issuer_source; |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 158 | for (size_t i = 1; i < cert_ders.size() - 1; ++i) { |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 159 | cert_issuer_source.AddCert(certs[i]); |
Bob Beck | 6beabf3 | 2023-11-21 09:43:52 -0700 | [diff] [blame] | 160 | } |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 161 | |
| 162 | std::shared_ptr<const ParsedCertificate> target_cert(certs.back()); |
| 163 | |
| 164 | int64_t verify_time; |
| 165 | ASSERT_TRUE(der::GeneralizedTimeToPosixTime(info.time, &verify_time)); |
| 166 | CrlCheckingPathBuilderDelegate path_builder_delegate( |
| 167 | crl_ders, verify_time, /*max_age=*/kOneYear * 2, 1024, |
| 168 | SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 169 | |
| 170 | std::string_view test_number = info.test_number; |
| 171 | if (test_number == "4.4.19" || test_number == "4.5.3" || |
| 172 | test_number == "4.5.4" || test_number == "4.5.6") { |
| 173 | // 4.4.19 - fails since CRL is signed by a certificate that is not part |
| 174 | // of the verified chain, which is not supported. |
| 175 | // 4.5.3 - fails since non-URI distribution point names are not supported |
| 176 | // 4.5.4, 4.5.6 - fails since CRL is signed by a certificate that is not |
| 177 | // part of verified chain, and also non-URI distribution |
| 178 | // point names not supported |
| 179 | info.should_validate = false; |
| 180 | } else if (test_number == "4.14.1" || test_number == "4.14.4" || |
| 181 | test_number == "4.14.5" || test_number == "4.14.7" || |
| 182 | test_number == "4.14.18" || test_number == "4.14.19" || |
| 183 | test_number == "4.14.22" || test_number == "4.14.24" || |
| 184 | test_number == "4.14.25" || test_number == "4.14.28" || |
| 185 | test_number == "4.14.29" || test_number == "4.14.30" || |
| 186 | test_number == "4.14.33") { |
| 187 | // 4.14 tests: |
| 188 | // .1 - fails since non-URI distribution point names not supported |
| 189 | // .2, .3 - fails since non-URI distribution point names not supported |
| 190 | // (but test is expected to fail for other reason) |
| 191 | // .4, .5 - fails since non-URI distribution point names not supported, |
| 192 | // also uses nameRelativeToCRLIssuer which is not supported |
| 193 | // .6 - fails since non-URI distribution point names not supported, also |
| 194 | // uses nameRelativeToCRLIssuer which is not supported (but test is |
| 195 | // expected to fail for other reason) |
| 196 | // .7 - fails since relative distributionPointName not supported |
| 197 | // .8, .9 - fails since relative distributionPointName not supported (but |
| 198 | // test is expected to fail for other reason) |
| 199 | // .10, .11, .12, .13, .14, .27, .35 - PASS |
| 200 | // .15, .16, .17, .20, .21 - fails since onlySomeReasons is not supported |
| 201 | // (but test is expected to fail for other |
| 202 | // reason) |
| 203 | // .18, .19 - fails since onlySomeReasons is not supported |
| 204 | // .22, .24, .25, .28, .29, .30, .33 - fails since indirect CRLs are not |
| 205 | // supported |
| 206 | // .23, .26, .31, .32, .34 - fails since indirect CRLs are not supported |
| 207 | // (but test is expected to fail for other |
| 208 | // reason) |
| 209 | info.should_validate = false; |
| 210 | } else if (test_number == "4.15.1" || test_number == "4.15.5") { |
| 211 | // 4.15 tests: |
| 212 | // .1 - fails due to unhandled critical deltaCRLIndicator extension |
| 213 | // .2, .3, .6, .7, .8, .9, .10 - PASS since expected cert status is |
| 214 | // reflected in base CRL and delta CRL is |
| 215 | // ignored |
| 216 | // .5 - fails, cert status is "on hold" in base CRL but the delta CRL |
| 217 | // which removes the cert from CRL is ignored |
| 218 | info.should_validate = false; |
| 219 | } else if (test_number == "4.15.4") { |
| 220 | // 4.15.4 - Invalid delta-CRL Test4 has the target cert marked revoked in |
| 221 | // a delta-CRL. Since delta-CRLs are not supported, the chain validates |
| 222 | // successfully. |
| 223 | info.should_validate = true; |
| 224 | } |
| 225 | |
| 226 | CertPathBuilder path_builder( |
| 227 | std::move(target_cert), &trust_store, &path_builder_delegate, info.time, |
| 228 | KeyPurpose::ANY_EKU, info.initial_explicit_policy, |
| 229 | info.initial_policy_set, info.initial_policy_mapping_inhibit, |
| 230 | info.initial_inhibit_any_policy); |
| 231 | path_builder.AddCertIssuerSource(&cert_issuer_source); |
| 232 | |
| 233 | CertPathBuilder::Result result = path_builder.Run(); |
| 234 | |
| 235 | if (info.should_validate != result.HasValidPath()) { |
Bob Beck | 64a9fb1 | 2023-11-22 17:37:31 -0700 | [diff] [blame] | 236 | testing::Message msg; |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 237 | for (size_t i = 0; i < result.paths.size(); ++i) { |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 238 | const bssl::CertPathBuilderResultPath *result_path = |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 239 | result.paths[i].get(); |
Bob Beck | 64a9fb1 | 2023-11-22 17:37:31 -0700 | [diff] [blame] | 240 | msg << "path " << i << " errors:\n" |
David Benjamin | 81138bc | 2024-01-23 14:53:40 -0500 | [diff] [blame] | 241 | << result_path->errors.ToDebugString(result_path->certs) << "\n"; |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 242 | } |
Bob Beck | 64a9fb1 | 2023-11-22 17:37:31 -0700 | [diff] [blame] | 243 | ASSERT_EQ(info.should_validate, result.HasValidPath()) << msg; |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 244 | } |
| 245 | |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 246 | if (result.HasValidPath()) { |
| 247 | EXPECT_EQ(info.user_constrained_policy_set, |
| 248 | result.GetBestValidPath()->user_constrained_policy_set); |
| 249 | } |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | } // namespace |
| 254 | |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 255 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest01SignatureVerification, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 256 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 257 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest02ValidityPeriods, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 258 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 259 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest03VerifyingNameChaining, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 260 | PathBuilderPkitsTestDelegate); |
| 261 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, |
| 262 | PkitsTest04BasicCertificateRevocationTests, |
| 263 | PathBuilderPkitsTestDelegate); |
| 264 | INSTANTIATE_TYPED_TEST_SUITE_P( |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 265 | PathBuilder, PkitsTest05VerifyingPathswithSelfIssuedCertificates, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 266 | PathBuilderPkitsTestDelegate); |
| 267 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, |
| 268 | PkitsTest06VerifyingBasicConstraints, |
| 269 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 270 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest07KeyUsage, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 271 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 272 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest08CertificatePolicies, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 273 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 274 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest09RequireExplicitPolicy, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 275 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 276 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest10PolicyMappings, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 277 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 278 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest11InhibitPolicyMapping, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 279 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 280 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest12InhibitAnyPolicy, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 281 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 282 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest13NameConstraints, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 283 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 284 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest14DistributionPoints, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 285 | PathBuilderPkitsTestDelegate); |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 286 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest15DeltaCRLs, |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 287 | PathBuilderPkitsTestDelegate); |
| 288 | INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, |
| 289 | PkitsTest16PrivateCertificateExtensions, |
| 290 | PathBuilderPkitsTestDelegate); |
| 291 | |
Bob Beck | 5c7a2a0 | 2023-11-20 17:28:21 -0700 | [diff] [blame] | 292 | } // namespace bssl |