xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/pki/path_builder_pkits_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
9 #include <openssl/pool.h>
10 #include "cert_issuer_source_static.h"
11 #include "common_cert_errors.h"
12 #include "crl.h"
13 #include "encode_values.h"
14 #include "input.h"
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"
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:
CrlCheckingPathBuilderDelegate(const std::vector<std::string> & der_crls,int64_t verify_time,int64_t max_age,size_t min_rsa_modulus_length_bits,DigestPolicy digest_policy)31   CrlCheckingPathBuilderDelegate(const std::vector<std::string> &der_crls,
32                                  int64_t verify_time, int64_t max_age,
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 
CheckPathAfterVerification(const CertPathBuilder & path_builder,CertPathBuilderResultPath * path)40   void CheckPathAfterVerification(const CertPathBuilder &path_builder,
41                                   CertPathBuilderResultPath *path) override {
42     SimplePathBuilderDelegate::CheckPathAfterVerification(path_builder, path);
43 
44     if (!path->IsValid()) {
45       return;
46     }
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.
52     const ParsedCertificateList &certs = path->certs;
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).
58       if (reverse_i == 0 && path->last_cert_trust.IsTrustAnchor()) {
59         continue;
60       }
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;
70       const ParsedDistributionPoint *cert_dp = &fake_cert_dp;
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.
86         for (const auto &dp : distribution_points) {
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.
94         if (cert_dp == &fake_cert_dp && !distribution_points.empty()) {
95           cert_dp = &distribution_points[0];
96         }
97       }
98 
99       bool cert_good = false;
100 
101       for (const auto &der_crl : der_crls_) {
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:
RunTest(std::vector<std::string> cert_ders,std::vector<std::string> crl_ders,const PkitsTestInfo & orig_info)134   static void RunTest(std::vector<std::string> cert_ders,
135                       std::vector<std::string> crl_ders,
136                       const PkitsTestInfo &orig_info) {
137     PkitsTestInfo info = orig_info;
138 
139     ASSERT_FALSE(cert_ders.empty());
140     ParsedCertificateList certs;
141     for (const std::string &der : cert_ders) {
142       CertErrors errors;
143       ASSERT_TRUE(ParsedCertificate::CreateAndAddToVector(
144           bssl::UniquePtr<CRYPTO_BUFFER>(
145               CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(der.data()),
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;
158     for (size_t i = 1; i < cert_ders.size() - 1; ++i) {
159       cert_issuer_source.AddCert(certs[i]);
160     }
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()) {
236       testing::Message msg;
237       for (size_t i = 0; i < result.paths.size(); ++i) {
238         const bssl::CertPathBuilderResultPath *result_path =
239             result.paths[i].get();
240         msg << "path " << i << " errors:\n"
241             << result_path->errors.ToDebugString(result_path->certs) << "\n";
242       }
243       ASSERT_EQ(info.should_validate, result.HasValidPath()) << msg;
244     }
245 
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 
255 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest01SignatureVerification,
256                                PathBuilderPkitsTestDelegate);
257 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest02ValidityPeriods,
258                                PathBuilderPkitsTestDelegate);
259 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest03VerifyingNameChaining,
260                                PathBuilderPkitsTestDelegate);
261 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
262                                PkitsTest04BasicCertificateRevocationTests,
263                                PathBuilderPkitsTestDelegate);
264 INSTANTIATE_TYPED_TEST_SUITE_P(
265     PathBuilder, PkitsTest05VerifyingPathswithSelfIssuedCertificates,
266     PathBuilderPkitsTestDelegate);
267 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
268                                PkitsTest06VerifyingBasicConstraints,
269                                PathBuilderPkitsTestDelegate);
270 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest07KeyUsage,
271                                PathBuilderPkitsTestDelegate);
272 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest08CertificatePolicies,
273                                PathBuilderPkitsTestDelegate);
274 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest09RequireExplicitPolicy,
275                                PathBuilderPkitsTestDelegate);
276 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest10PolicyMappings,
277                                PathBuilderPkitsTestDelegate);
278 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest11InhibitPolicyMapping,
279                                PathBuilderPkitsTestDelegate);
280 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest12InhibitAnyPolicy,
281                                PathBuilderPkitsTestDelegate);
282 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest13NameConstraints,
283                                PathBuilderPkitsTestDelegate);
284 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest14DistributionPoints,
285                                PathBuilderPkitsTestDelegate);
286 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder, PkitsTest15DeltaCRLs,
287                                PathBuilderPkitsTestDelegate);
288 INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
289                                PkitsTest16PrivateCertificateExtensions,
290                                PathBuilderPkitsTestDelegate);
291 
292 }  // namespace bssl
293