blob: e912e467599611189c16b557f36272efb39c175f [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 Beck9c0f22c2023-06-29 11:18:37 -06009#include "fillins/log.h"
Bob Beckbc97b7a2023-04-18 08:35:15 -060010
Bob Beck5c7a2a02023-11-20 17:28:21 -070011#include <openssl/pool.h>
Bob Beckbc97b7a2023-04-18 08:35:15 -060012#include "cert_issuer_source_static.h"
13#include "common_cert_errors.h"
14#include "crl.h"
Bob Beck5c7a2a02023-11-20 17:28:21 -070015#include "encode_values.h"
16#include "input.h"
Bob Beckbc97b7a2023-04-18 08:35:15 -060017#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 Beckbc97b7a2023-04-18 08:35:15 -060022
23#include "nist_pkits_unittest.h"
24
25constexpr int64_t kOneYear = 60 * 60 * 24 * 365;
26
27namespace bssl {
28
29namespace {
30
31class CrlCheckingPathBuilderDelegate : public SimplePathBuilderDelegate {
32 public:
Bob Beck5c7a2a02023-11-20 17:28:21 -070033 CrlCheckingPathBuilderDelegate(const std::vector<std::string> &der_crls,
34 int64_t verify_time, int64_t max_age,
Bob Beckbc97b7a2023-04-18 08:35:15 -060035 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 Beck5c7a2a02023-11-20 17:28:21 -070042 void CheckPathAfterVerification(const CertPathBuilder &path_builder,
43 CertPathBuilderResultPath *path) override {
Bob Beckbc97b7a2023-04-18 08:35:15 -060044 SimplePathBuilderDelegate::CheckPathAfterVerification(path_builder, path);
45
Bob Beck6beabf32023-11-21 09:43:52 -070046 if (!path->IsValid()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060047 return;
Bob Beck6beabf32023-11-21 09:43:52 -070048 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060049
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 Beck5c7a2a02023-11-20 17:28:21 -070054 const ParsedCertificateList &certs = path->certs;
Bob Beckbc97b7a2023-04-18 08:35:15 -060055 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 Beck6beabf32023-11-21 09:43:52 -070060 if (reverse_i == 0 && path->last_cert_trust.IsTrustAnchor()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060061 continue;
Bob Beck6beabf32023-11-21 09:43:52 -070062 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060063
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 Beck5c7a2a02023-11-20 17:28:21 -070072 const ParsedDistributionPoint *cert_dp = &fake_cert_dp;
Bob Beckbc97b7a2023-04-18 08:35:15 -060073
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 Beck5c7a2a02023-11-20 17:28:21 -070088 for (const auto &dp : distribution_points) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060089 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 Beck6beabf32023-11-21 09:43:52 -070096 if (cert_dp == &fake_cert_dp && !distribution_points.empty()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060097 cert_dp = &distribution_points[0];
Bob Beck6beabf32023-11-21 09:43:52 -070098 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060099 }
100
101 bool cert_good = false;
102
Bob Beck5c7a2a02023-11-20 17:28:21 -0700103 for (const auto &der_crl : der_crls_) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600104 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
134class PathBuilderPkitsTestDelegate {
135 public:
136 static void RunTest(std::vector<std::string> cert_ders,
137 std::vector<std::string> crl_ders,
Bob Beck5c7a2a02023-11-20 17:28:21 -0700138 const PkitsTestInfo &orig_info) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600139 PkitsTestInfo info = orig_info;
140
141 ASSERT_FALSE(cert_ders.empty());
142 ParsedCertificateList certs;
Bob Beck5c7a2a02023-11-20 17:28:21 -0700143 for (const std::string &der : cert_ders) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600144 CertErrors errors;
145 ASSERT_TRUE(ParsedCertificate::CreateAndAddToVector(
146 bssl::UniquePtr<CRYPTO_BUFFER>(
Bob Beck5c7a2a02023-11-20 17:28:21 -0700147 CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(der.data()),
Bob Beckbc97b7a2023-04-18 08:35:15 -0600148 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 Beck6beabf32023-11-21 09:43:52 -0700160 for (size_t i = 1; i < cert_ders.size() - 1; ++i) {
Bob Beckbc97b7a2023-04-18 08:35:15 -0600161 cert_issuer_source.AddCert(certs[i]);
Bob Beck6beabf32023-11-21 09:43:52 -0700162 }
Bob Beckbc97b7a2023-04-18 08:35:15 -0600163
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 Beck5c7a2a02023-11-20 17:28:21 -0700239 const bssl::CertPathBuilderResultPath *result_path =
Bob Beckbc97b7a2023-04-18 08:35:15 -0600240 result.paths[i].get();
Bob Beck2e119172023-08-14 11:06:38 -0600241 LOG(ERROR) << "path " << i << " errors:\n"
Bob Beckbc97b7a2023-04-18 08:35:15 -0600242 << result_path->errors.ToDebugString(result_path->certs);
Bob Beckbc97b7a2023-04-18 08:35:15 -0600243 }
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 Beck5c7a2a02023-11-20 17:28:21 -0700257INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest01SignatureVerification,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600258 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700259INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest02ValidityPeriods,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600260 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700261INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest03VerifyingNameChaining,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600262 PathBuilderPkitsTestDelegate);
263INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
264 PkitsTest04BasicCertificateRevocationTests,
265 PathBuilderPkitsTestDelegate);
266INSTANTIATE_TYPED_TEST_SUITE_P(
Bob Beck5c7a2a02023-11-20 17:28:21 -0700267 PathBuilder, PkitsTest05VerifyingPathswithSelfIssuedCertificates,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600268 PathBuilderPkitsTestDelegate);
269INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
270 PkitsTest06VerifyingBasicConstraints,
271 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700272INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest07KeyUsage,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600273 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700274INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest08CertificatePolicies,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600275 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700276INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest09RequireExplicitPolicy,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600277 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700278INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest10PolicyMappings,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600279 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700280INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest11InhibitPolicyMapping,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600281 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700282INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest12InhibitAnyPolicy,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600283 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700284INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest13NameConstraints,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600285 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700286INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest14DistributionPoints,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600287 PathBuilderPkitsTestDelegate);
Bob Beck5c7a2a02023-11-20 17:28:21 -0700288INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest15DeltaCRLs,
Bob Beckbc97b7a2023-04-18 08:35:15 -0600289 PathBuilderPkitsTestDelegate);
290INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
291 PkitsTest16PrivateCertificateExtensions,
292 PathBuilderPkitsTestDelegate);
293
Bob Beck5c7a2a02023-11-20 17:28:21 -0700294} // namespace bssl