blob: 5bc0703359f7e91a5f6824db1ec0bafd954c1151 [file] [log] [blame]
Bob Beckbc97b7a2023-04-18 08:35:15 -06001// 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 Beck5c7a2a02023-11-20 17:28:21 -07009#include <openssl/pool.h>
Bob Beckbc97b7a2023-04-18 08:35:15 -060010#include "cert_issuer_source_static.h"
11#include "common_cert_errors.h"
12#include "crl.h"
Bob Beck5c7a2a02023-11-20 17:28:21 -070013#include "encode_values.h"
14#include "input.h"
Bob Beckbc97b7a2023-04-18 08:35:15 -060015#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 Beckbc97b7a2023-04-18 08:35:15 -060020
21#include "nist_pkits_unittest.h"
22
23constexpr int64_t kOneYear = 60 * 60 * 24 * 365;
24
25namespace bssl {
26
27namespace {
28
29class CrlCheckingPathBuilderDelegate : public SimplePathBuilderDelegate {
30 public:
Bob Beck5c7a2a02023-11-20 17:28:21 -070031 CrlCheckingPathBuilderDelegate(const std::vector<std::string> &der_crls,
32 int64_t verify_time, int64_t max_age,
Bob Beckbc97b7a2023-04-18 08:35:15 -060033 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 Beck5c7a2a02023-11-20 17:28:21 -070040 void CheckPathAfterVerification(const CertPathBuilder &path_builder,
41 CertPathBuilderResultPath *path) override {
Bob Beckbc97b7a2023-04-18 08:35:15 -060042 SimplePathBuilderDelegate::CheckPathAfterVerification(path_builder, path);
43
Bob Beck6beabf32023-11-21 09:43:52 -070044 if (!path->IsValid()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060045 return;
Bob Beck6beabf32023-11-21 09:43:52 -070046 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060047
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 Beck5c7a2a02023-11-20 17:28:21 -070052 const ParsedCertificateList &certs = path->certs;
Bob Beckbc97b7a2023-04-18 08:35:15 -060053 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 Beck6beabf32023-11-21 09:43:52 -070058 if (reverse_i == 0 && path->last_cert_trust.IsTrustAnchor()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060059 continue;
Bob Beck6beabf32023-11-21 09:43:52 -070060 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060061
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 Beck5c7a2a02023-11-20 17:28:21 -070070 const ParsedDistributionPoint *cert_dp = &fake_cert_dp;
Bob Beckbc97b7a2023-04-18 08:35:15 -060071
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 Beck5c7a2a02023-11-20 17:28:21 -070086 for (const auto &dp : distribution_points) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060087 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 Beck6beabf32023-11-21 09:43:52 -070094 if (cert_dp == &fake_cert_dp && !distribution_points.empty()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060095 cert_dp = &distribution_points[0];
Bob Beck6beabf32023-11-21 09:43:52 -070096 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060097 }
98
99 bool cert_good = false;
100
Bob Beck5c7a2a02023-11-20 17:28:21 -0700101 for (const auto &der_crl : der_crls_) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600102 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
132class PathBuilderPkitsTestDelegate {
133 public:
134 static void RunTest(std::vector<std::string> cert_ders,
135 std::vector<std::string> crl_ders,
Bob Beck5c7a2a02023-11-20 17:28:21 -0700136 const PkitsTestInfo &orig_info) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600137 PkitsTestInfo info = orig_info;
138
139 ASSERT_FALSE(cert_ders.empty());
140 ParsedCertificateList certs;
Bob Beck5c7a2a02023-11-20 17:28:21 -0700141 for (const std::string &der : cert_ders) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600142 CertErrors errors;
143 ASSERT_TRUE(ParsedCertificate::CreateAndAddToVector(
144 bssl::UniquePtr<CRYPTO_BUFFER>(
Bob Beck5c7a2a02023-11-20 17:28:21 -0700145 CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(der.data()),
Bob Beckbc97b7a2023-04-18 08:35:15 -0600146 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 Beck6beabf32023-11-21 09:43:52 -0700158 for (size_t i = 1; i < cert_ders.size() - 1; ++i) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600159 cert_issuer_source.AddCert(certs[i]);
Bob Beck6beabf32023-11-21 09:43:52 -0700160 }
Bob Beckbc97b7a2023-04-18 08:35:15 -0600161
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 Beck64a9fb12023-11-22 17:37:31 -0700236 testing::Message msg;
Bob Beckbc97b7a2023-04-18 08:35:15 -0600237 for (size_t i = 0; i < result.paths.size(); ++i) {
Bob Beck5c7a2a02023-11-20 17:28:21 -0700238 const bssl::CertPathBuilderResultPath *result_path =
Bob Beckbc97b7a2023-04-18 08:35:15 -0600239 result.paths[i].get();
Bob Beck64a9fb12023-11-22 17:37:31 -0700240 msg << "path " << i << " errors:\n"
David Benjamin81138bc2024-01-23 14:53:40 -0500241 << result_path->errors.ToDebugString(result_path->certs) << "\n";
Bob Beckbc97b7a2023-04-18 08:35:15 -0600242 }
Bob Beck64a9fb12023-11-22 17:37:31 -0700243 ASSERT_EQ(info.should_validate, result.HasValidPath()) << msg;
Bob Beckbc97b7a2023-04-18 08:35:15 -0600244 }
245
Bob Beckbc97b7a2023-04-18 08:35:15 -0600246 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 Beck5c7a2a02023-11-20 17:28:21 -0700255INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest01SignatureVerification,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600256 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700257INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest02ValidityPeriods,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600258 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700259INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest03VerifyingNameChaining,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600260 PathBuilderPkitsTestDelegate);
261INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
262 PkitsTest04BasicCertificateRevocationTests,
263 PathBuilderPkitsTestDelegate);
264INSTANTIATE_TYPED_TEST_SUITE_P(
Bob Beck5c7a2a02023-11-20 17:28:21 -0700265 PathBuilder, PkitsTest05VerifyingPathswithSelfIssuedCertificates,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600266 PathBuilderPkitsTestDelegate);
267INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
268 PkitsTest06VerifyingBasicConstraints,
269 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700270INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest07KeyUsage,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600271 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700272INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest08CertificatePolicies,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600273 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700274INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest09RequireExplicitPolicy,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600275 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700276INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest10PolicyMappings,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600277 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700278INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest11InhibitPolicyMapping,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600279 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700280INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest12InhibitAnyPolicy,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600281 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700282INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest13NameConstraints,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600283 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700284INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest14DistributionPoints,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600285 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700286INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest15DeltaCRLs,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600287 PathBuilderPkitsTestDelegate);
288INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
289 PkitsTest16PrivateCertificateExtensions,
290 PathBuilderPkitsTestDelegate);
291
Bob Beck5c7a2a02023-11-20 17:28:21 -0700292} // namespace bssl