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