| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "path_builder.h" |
| 16 |
| 17 | #include <algorithm> |
| 18 |
| 19 | #include "cert_error_params.h" |
| 20 | #include "cert_issuer_source_static.h" |
| 21 | #include "common_cert_errors.h" |
| 22 | #include "input.h" |
| 23 | #include "mock_signature_verify_cache.h" |
| 24 | #include "parsed_certificate.h" |
| 25 | #include "simple_path_builder_delegate.h" |
| 26 | #include "test_helpers.h" |
| 27 | #include "trust_store_collection.h" |
| 28 | #include "trust_store_in_memory.h" |
| 29 | #include "verify_certificate_chain.h" |
| 30 |
| 31 | #include <gmock/gmock.h> |
| 32 | #include <gtest/gtest.h> |
| 33 | #include <openssl/pool.h> |
| 34 |
| 35 | BSSL_NAMESPACE_BEGIN |
| 36 |
| 37 | // TODO(crbug.com/634443): Assert the errors for each ResultPath. |
| 38 |
| 39 | namespace { |
| 40 |
| 41 | using ::testing::_; |
| 42 | using ::testing::Invoke; |
| 43 | using ::testing::StrictMock; |
| 44 |
| 45 | class TestPathBuilderDelegate : public SimplePathBuilderDelegate { |
| 46 | public: |
| 47 | TestPathBuilderDelegate(size_t min_rsa_modulus_length_bits, |
| 48 | DigestPolicy digest_policy) |
| 49 | : SimplePathBuilderDelegate(min_rsa_modulus_length_bits, digest_policy) {} |
| 50 |
| 51 | bool IsDeadlineExpired() override { return deadline_is_expired_; } |
| 52 |
| 53 | void SetDeadlineExpiredForTesting(bool deadline_is_expired) { |
| 54 | deadline_is_expired_ = deadline_is_expired; |
| 55 | } |
| 56 |
| 57 | SignatureVerifyCache *GetVerifyCache() override { |
| 58 | return use_signature_cache_ ? &cache_ : nullptr; |
| 59 | } |
| 60 |
| 61 | void ActivateCache() { use_signature_cache_ = true; } |
| 62 |
| 63 | void DeActivateCache() { use_signature_cache_ = false; } |
| 64 |
| 65 | MockSignatureVerifyCache *GetMockVerifyCache() { return &cache_; } |
| 66 |
| 67 | void AllowPrecert() { allow_precertificate_ = true; } |
| 68 |
| 69 | void DisallowPrecert() { allow_precertificate_ = false; } |
| 70 |
| 71 | bool AcceptPreCertificates() override { |
| 72 | return allow_precertificate_; |
| 73 | } |
| 74 |
| 75 | private: |
| 76 | bool deadline_is_expired_ = false; |
| 77 | bool use_signature_cache_ = false; |
| 78 | bool allow_precertificate_ = false; |
| 79 | MockSignatureVerifyCache cache_; |
| 80 | }; |
| 81 |
| 82 | class CertPathBuilderDelegateBase : public SimplePathBuilderDelegate { |
| 83 | public: |
| 84 | CertPathBuilderDelegateBase() |
| 85 | : SimplePathBuilderDelegate( |
| 86 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1) {} |
| 87 | void CheckPathAfterVerification(const CertPathBuilder &path_builder, |
| 88 | CertPathBuilderResultPath *path) override { |
| 89 | ADD_FAILURE() << "Tests must override this"; |
| 90 | } |
| 91 | }; |
| 92 |
| 93 | class MockPathBuilderDelegate : public CertPathBuilderDelegateBase { |
| 94 | public: |
| 95 | MOCK_METHOD2(CheckPathAfterVerification, |
| 96 | void(const CertPathBuilder &path_builder, |
| 97 | CertPathBuilderResultPath *path)); |
| 98 | }; |
| 99 |
| 100 | // AsyncCertIssuerSourceStatic always returns its certs asynchronously. |
| 101 | class AsyncCertIssuerSourceStatic : public CertIssuerSource { |
| 102 | public: |
| 103 | class StaticAsyncRequest : public Request { |
| 104 | public: |
| 105 | explicit StaticAsyncRequest(ParsedCertificateList &&issuers) { |
| 106 | issuers_.swap(issuers); |
| 107 | issuers_iter_ = issuers_.begin(); |
| 108 | } |
| 109 |
| 110 | StaticAsyncRequest(const StaticAsyncRequest &) = delete; |
| 111 | StaticAsyncRequest &operator=(const StaticAsyncRequest &) = delete; |
| 112 |
| 113 | ~StaticAsyncRequest() override = default; |
| 114 |
| 115 | void GetNext(ParsedCertificateList *out_certs) override { |
| 116 | if (issuers_iter_ != issuers_.end()) { |
| 117 | out_certs->push_back(std::move(*issuers_iter_++)); |
| 118 | } |
| 119 | } |
| 120 |
| 121 | ParsedCertificateList issuers_; |
| 122 | ParsedCertificateList::iterator issuers_iter_; |
| 123 | }; |
| 124 |
| 125 | ~AsyncCertIssuerSourceStatic() override = default; |
| 126 |
| 127 | void SetAsyncGetCallback(std::function<void()> closure) { |
| 128 | async_get_callback_ = std::move(closure); |
| 129 | } |
| 130 |
| 131 | void AddCert(std::shared_ptr<const ParsedCertificate> cert) { |
| 132 | static_cert_issuer_source_.AddCert(std::move(cert)); |
| 133 | } |
| 134 |
| 135 | void SyncGetIssuersOf(const ParsedCertificate *cert, |
| 136 | ParsedCertificateList *issuers) override {} |
| 137 | void AsyncGetIssuersOf(const ParsedCertificate *cert, |
| 138 | std::unique_ptr<Request> *out_req) override { |
| 139 | num_async_gets_++; |
| 140 | ParsedCertificateList issuers; |
| 141 | static_cert_issuer_source_.SyncGetIssuersOf(cert, &issuers); |
| 142 | auto req = std::make_unique<StaticAsyncRequest>(std::move(issuers)); |
| 143 | *out_req = std::move(req); |
| 144 | if (async_get_callback_) { |
| 145 | async_get_callback_(); |
| 146 | } |
| 147 | } |
| 148 | int num_async_gets() const { return num_async_gets_; } |
| 149 |
| 150 | private: |
| 151 | CertIssuerSourceStatic static_cert_issuer_source_; |
| 152 |
| 153 | int num_async_gets_ = 0; |
| 154 | std::function<void()> async_get_callback_ = nullptr; |
| 155 | }; |
| 156 |
| 157 | ::testing::AssertionResult ReadTestPem(const std::string &file_name, |
| 158 | const std::string &block_name, |
| 159 | std::string *result) { |
| 160 | const PemBlockMapping mappings[] = { |
| 161 | {block_name.c_str(), result}, |
| 162 | }; |
| 163 |
| 164 | return ReadTestDataFromPemFile(file_name, mappings); |
| 165 | } |
| 166 |
| 167 | ::testing::AssertionResult ReadTestCert( |
| 168 | const std::string &file_name, |
| 169 | std::shared_ptr<const ParsedCertificate> *result) { |
| 170 | std::string der; |
| 171 | ::testing::AssertionResult r = ReadTestPem( |
| 172 | "testdata/path_builder_unittest/" + file_name, "CERTIFICATE", &der); |
| 173 | if (!r) { |
| 174 | return r; |
| 175 | } |
| 176 | CertErrors errors; |
| 177 | *result = ParsedCertificate::Create( |
| 178 | bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( |
| 179 | reinterpret_cast<const uint8_t *>(der.data()), der.size(), nullptr)), |
| 180 | {}, &errors); |
| 181 | if (!*result) { |
| 182 | return ::testing::AssertionFailure() |
| 183 | << "ParseCertificate::Create() failed:\n" |
| 184 | << errors.ToDebugString(); |
| 185 | } |
| 186 | return ::testing::AssertionSuccess(); |
| 187 | } |
| 188 |
| 189 | class PathBuilderMultiRootTest : public ::testing::Test { |
| 190 | public: |
| 191 | PathBuilderMultiRootTest() |
| 192 | : delegate_(1024, TestPathBuilderDelegate::DigestPolicy::kWeakAllowSha1) { |
| 193 | } |
| 194 |
| 195 | void SetUp() override { |
| 196 | ASSERT_TRUE(ReadTestCert("multi-root-A-by-B.pem", &a_by_b_)); |
| 197 | ASSERT_TRUE(ReadTestCert("multi-root-B-by-C.pem", &b_by_c_)); |
| 198 | ASSERT_TRUE(ReadTestCert("multi-root-B-by-F.pem", &b_by_f_)); |
| 199 | ASSERT_TRUE(ReadTestCert("multi-root-C-by-D.pem", &c_by_d_)); |
| 200 | ASSERT_TRUE(ReadTestCert("multi-root-C-by-E.pem", &c_by_e_)); |
| 201 | ASSERT_TRUE(ReadTestCert("multi-root-D-by-D.pem", &d_by_d_)); |
| 202 | ASSERT_TRUE(ReadTestCert("multi-root-E-by-E.pem", &e_by_e_)); |
| 203 | ASSERT_TRUE(ReadTestCert("multi-root-F-by-E.pem", &f_by_e_)); |
| 204 | } |
| 205 |
| 206 | protected: |
| 207 | std::shared_ptr<const ParsedCertificate> a_by_b_, b_by_c_, b_by_f_, c_by_d_, |
| 208 | c_by_e_, d_by_d_, e_by_e_, f_by_e_; |
| 209 |
| 210 | TestPathBuilderDelegate delegate_; |
| 211 | der::GeneralizedTime time_ = {2017, 3, 1, 0, 0, 0}; |
| 212 |
| 213 | const InitialExplicitPolicy initial_explicit_policy_ = |
| 214 | InitialExplicitPolicy::kFalse; |
| 215 | const std::set<der::Input> user_initial_policy_set_ = { |
| 216 | der::Input(kAnyPolicyOid)}; |
| 217 | const InitialPolicyMappingInhibit initial_policy_mapping_inhibit_ = |
| 218 | InitialPolicyMappingInhibit::kFalse; |
| 219 | const InitialAnyPolicyInhibit initial_any_policy_inhibit_ = |
| 220 | InitialAnyPolicyInhibit::kFalse; |
| 221 | }; |
| 222 |
| 223 | // Tests when the target cert has the same name and key as a trust anchor, |
| 224 | // however is signed by a different trust anchor. This should successfully build |
| 225 | // a path, however the trust anchor will be the signer of this cert. |
| 226 | // |
| 227 | // (This test is very similar to TestEndEntityHasSameNameAndSpkiAsTrustAnchor |
| 228 | // but with different data; also in this test the target cert itself is in the |
| 229 | // trust store). |
| 230 | TEST_F(PathBuilderMultiRootTest, TargetHasNameAndSpkiOfTrustAnchor) { |
| 231 | TrustStoreInMemory trust_store; |
| 232 | trust_store.AddTrustAnchor(a_by_b_); |
| 233 | trust_store.AddTrustAnchor(b_by_f_); |
| 234 |
| 235 | CertPathBuilder path_builder( |
| 236 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 237 | initial_explicit_policy_, user_initial_policy_set_, |
| 238 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 239 |
| 240 | auto result = path_builder.Run(); |
| 241 |
| 242 | ASSERT_TRUE(result.HasValidPath()); |
| 243 | VerifyError error = result.GetBestPathVerifyError(); |
| 244 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 245 | << error.DiagnosticString(); |
| 246 | const auto &path = *result.GetBestValidPath(); |
| 247 | ASSERT_EQ(2U, path.certs.size()); |
| 248 | EXPECT_EQ(a_by_b_, path.certs[0]); |
| 249 | EXPECT_EQ(b_by_f_, path.certs[1]); |
| 250 | } |
| 251 |
| 252 | // If the target cert is has the same name and key as a trust anchor, however |
| 253 | // is NOT itself signed by a trust anchor, it fails. Although the provided SPKI |
| 254 | // is trusted, the certificate contents cannot be verified. |
| 255 | TEST_F(PathBuilderMultiRootTest, TargetWithSameNameAsTrustAnchorFails) { |
| 256 | TrustStoreInMemory trust_store; |
| 257 | trust_store.AddTrustAnchor(a_by_b_); |
| 258 |
| 259 | CertPathBuilder path_builder( |
| 260 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 261 | initial_explicit_policy_, user_initial_policy_set_, |
| 262 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 263 |
| 264 | auto result = path_builder.Run(); |
| 265 |
| 266 | EXPECT_FALSE(result.HasValidPath()); |
| 267 | EXPECT_EQ(1U, result.max_depth_seen); |
| 268 | VerifyError error = result.GetBestPathVerifyError(); |
| 269 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 270 | << error.DiagnosticString(); |
| 271 | } |
| 272 |
| 273 | // Test a failed path building when the trust anchor is provided as a |
| 274 | // supplemental certificate. Conceptually the following paths could be built: |
| 275 | // |
| 276 | // B(C) <- C(D) <- [Trust anchor D] |
| 277 | // B(C) <- C(D) <- D(D) <- [Trust anchor D] |
| 278 | // |
| 279 | // However the second one is extraneous given the shorter path. |
| 280 | TEST_F(PathBuilderMultiRootTest, SelfSignedTrustAnchorSupplementalCert) { |
| 281 | TrustStoreInMemory trust_store; |
| 282 | trust_store.AddTrustAnchor(d_by_d_); |
| 283 |
| 284 | // The (extraneous) trust anchor D(D) is supplied as a certificate, as is the |
| 285 | // intermediate needed for path building C(D). |
| 286 | CertIssuerSourceStatic sync_certs; |
| 287 | sync_certs.AddCert(d_by_d_); |
| 288 | sync_certs.AddCert(c_by_d_); |
| 289 |
| 290 | // C(D) is not valid at this time, so path building will fail. |
| 291 | der::GeneralizedTime expired_time = {2016, 1, 1, 0, 0, 0}; |
| 292 |
| 293 | CertPathBuilder path_builder( |
| 294 | b_by_c_, &trust_store, &delegate_, expired_time, KeyPurpose::ANY_EKU, |
| 295 | initial_explicit_policy_, user_initial_policy_set_, |
| 296 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 297 | path_builder.AddCertIssuerSource(&sync_certs); |
| 298 |
| 299 | auto result = path_builder.Run(); |
| 300 |
| 301 | EXPECT_FALSE(result.HasValidPath()); |
| 302 | ASSERT_EQ(1U, result.paths.size()); |
| 303 |
| 304 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 305 | const auto &path0 = *result.paths[0]; |
| 306 | ASSERT_EQ(3U, path0.certs.size()); |
| 307 | EXPECT_EQ(b_by_c_, path0.certs[0]); |
| 308 | EXPECT_EQ(c_by_d_, path0.certs[1]); |
| 309 | EXPECT_EQ(d_by_d_, path0.certs[2]); |
| 310 |
| 311 | VerifyError error = result.GetBestPathVerifyError(); |
| 312 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::CERTIFICATE_NOT_YET_VALID) |
| 313 | << error.DiagnosticString(); |
| 314 | } |
| 315 |
| 316 | // Test verifying a certificate that is a trust anchor. |
| 317 | TEST_F(PathBuilderMultiRootTest, TargetIsSelfSignedTrustAnchor) { |
| 318 | TrustStoreInMemory trust_store; |
| 319 | trust_store.AddTrustAnchor(e_by_e_); |
| 320 | // This is not necessary for the test, just an extra... |
| 321 | trust_store.AddTrustAnchor(f_by_e_); |
| 322 |
| 323 | CertPathBuilder path_builder( |
| 324 | e_by_e_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 325 | initial_explicit_policy_, user_initial_policy_set_, |
| 326 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 327 |
| 328 | auto result = path_builder.Run(); |
| 329 |
| 330 | ASSERT_TRUE(result.HasValidPath()); |
| 331 |
| 332 | // Verifying a trusted leaf certificate is not permitted, however this |
| 333 | // certificate is self-signed, and can chain to itself. |
| 334 | const auto &path = *result.GetBestValidPath(); |
| 335 | ASSERT_EQ(2U, path.certs.size()); |
| 336 | EXPECT_EQ(e_by_e_, path.certs[0]); |
| 337 | EXPECT_EQ(e_by_e_, path.certs[1]); |
| 338 |
| 339 | VerifyError error = result.GetBestPathVerifyError(); |
| 340 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 341 | << error.DiagnosticString(); |
| 342 | } |
| 343 |
| 344 | // If the target cert is directly issued by a trust anchor, it should verify |
| 345 | // without any intermediate certs being provided. |
| 346 | TEST_F(PathBuilderMultiRootTest, TargetDirectlySignedByTrustAnchor) { |
| 347 | TrustStoreInMemory trust_store; |
| 348 | trust_store.AddTrustAnchor(b_by_f_); |
| 349 |
| 350 | CertPathBuilder path_builder( |
| 351 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 352 | initial_explicit_policy_, user_initial_policy_set_, |
| 353 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 354 |
| 355 | auto result = path_builder.Run(); |
| 356 |
| 357 | ASSERT_TRUE(result.HasValidPath()); |
| 358 | const auto &path = *result.GetBestValidPath(); |
| 359 | ASSERT_EQ(2U, path.certs.size()); |
| 360 | EXPECT_EQ(a_by_b_, path.certs[0]); |
| 361 | EXPECT_EQ(b_by_f_, path.certs[1]); |
| 362 |
| 363 | VerifyError error = result.GetBestPathVerifyError(); |
| 364 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 365 | << error.DiagnosticString(); |
| 366 | } |
| 367 |
| 368 | // Test that async cert queries are not made if the path can be successfully |
| 369 | // built with synchronously available certs. |
| 370 | TEST_F(PathBuilderMultiRootTest, TriesSyncFirst) { |
| 371 | TrustStoreInMemory trust_store; |
| 372 | trust_store.AddTrustAnchor(e_by_e_); |
| 373 |
| 374 | CertIssuerSourceStatic sync_certs; |
| 375 | sync_certs.AddCert(b_by_f_); |
| 376 | sync_certs.AddCert(f_by_e_); |
| 377 |
| 378 | AsyncCertIssuerSourceStatic async_certs; |
| 379 | async_certs.AddCert(b_by_c_); |
| 380 | async_certs.AddCert(c_by_e_); |
| 381 |
| 382 | CertPathBuilder path_builder( |
| 383 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 384 | initial_explicit_policy_, user_initial_policy_set_, |
| 385 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 386 | path_builder.AddCertIssuerSource(&async_certs); |
| 387 | path_builder.AddCertIssuerSource(&sync_certs); |
| 388 |
| 389 | auto result = path_builder.Run(); |
| 390 |
| 391 | EXPECT_TRUE(result.HasValidPath()); |
| 392 | EXPECT_EQ(0, async_certs.num_async_gets()); |
| 393 |
| 394 | VerifyError error = result.GetBestPathVerifyError(); |
| 395 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 396 | << error.DiagnosticString(); |
| 397 | } |
| 398 |
| 399 | // If async queries are needed, all async sources will be queried |
| 400 | // simultaneously. |
| 401 | TEST_F(PathBuilderMultiRootTest, TestAsyncSimultaneous) { |
| 402 | TrustStoreInMemory trust_store; |
| 403 | trust_store.AddTrustAnchor(e_by_e_); |
| 404 |
| 405 | CertIssuerSourceStatic sync_certs; |
| 406 | sync_certs.AddCert(b_by_c_); |
| 407 | sync_certs.AddCert(b_by_f_); |
| 408 |
| 409 | AsyncCertIssuerSourceStatic async_certs1; |
| 410 | async_certs1.AddCert(c_by_e_); |
| 411 |
| 412 | AsyncCertIssuerSourceStatic async_certs2; |
| 413 | async_certs2.AddCert(f_by_e_); |
| 414 |
| 415 | CertPathBuilder path_builder( |
| 416 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 417 | initial_explicit_policy_, user_initial_policy_set_, |
| 418 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 419 | path_builder.AddCertIssuerSource(&async_certs1); |
| 420 | path_builder.AddCertIssuerSource(&async_certs2); |
| 421 | path_builder.AddCertIssuerSource(&sync_certs); |
| 422 |
| 423 | auto result = path_builder.Run(); |
| 424 |
| 425 | EXPECT_TRUE(result.HasValidPath()); |
| 426 | EXPECT_EQ(1, async_certs1.num_async_gets()); |
| 427 | EXPECT_EQ(1, async_certs2.num_async_gets()); |
| 428 |
| 429 | VerifyError error = result.GetBestPathVerifyError(); |
| 430 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 431 | << error.DiagnosticString(); |
| 432 | } |
| 433 |
| 434 | // Test that PathBuilder does not generate longer paths than necessary if one of |
| 435 | // the supplied certs is itself a trust anchor. |
| 436 | TEST_F(PathBuilderMultiRootTest, TestLongChain) { |
| 437 | // Both D(D) and C(D) are trusted roots. |
| 438 | TrustStoreInMemory trust_store; |
| 439 | trust_store.AddTrustAnchor(d_by_d_); |
| 440 | trust_store.AddTrustAnchor(c_by_d_); |
| 441 |
| 442 | // Certs B(C), and C(D) are all supplied. |
| 443 | CertIssuerSourceStatic sync_certs; |
| 444 | sync_certs.AddCert(b_by_c_); |
| 445 | sync_certs.AddCert(c_by_d_); |
| 446 |
| 447 | CertPathBuilder path_builder( |
| 448 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 449 | initial_explicit_policy_, user_initial_policy_set_, |
| 450 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 451 | path_builder.AddCertIssuerSource(&sync_certs); |
| 452 |
| 453 | auto result = path_builder.Run(); |
| 454 |
| 455 | ASSERT_TRUE(result.HasValidPath()); |
| 456 |
| 457 | // The result path should be A(B) <- B(C) <- C(D) |
| 458 | // not the longer but also valid A(B) <- B(C) <- C(D) <- D(D) |
| 459 | EXPECT_EQ(3U, result.GetBestValidPath()->certs.size()); |
| 460 |
| 461 | VerifyError error = result.GetBestPathVerifyError(); |
| 462 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 463 | << error.DiagnosticString(); |
| 464 | } |
| 465 |
| 466 | // Test that PathBuilder will backtrack and try a different path if the first |
| 467 | // one doesn't work out. |
| 468 | TEST_F(PathBuilderMultiRootTest, TestBacktracking) { |
| 469 | // Only D(D) is a trusted root. |
| 470 | TrustStoreInMemory trust_store; |
| 471 | trust_store.AddTrustAnchor(d_by_d_); |
| 472 |
| 473 | // Certs B(F) and F(E) are supplied synchronously, thus the path |
| 474 | // A(B) <- B(F) <- F(E) should be built first, though it won't verify. |
| 475 | CertIssuerSourceStatic sync_certs; |
| 476 | sync_certs.AddCert(b_by_f_); |
| 477 | sync_certs.AddCert(f_by_e_); |
| 478 |
| 479 | // Certs B(C), and C(D) are supplied asynchronously, so the path |
| 480 | // A(B) <- B(C) <- C(D) <- D(D) should be tried second. |
| 481 | AsyncCertIssuerSourceStatic async_certs; |
| 482 | async_certs.AddCert(b_by_c_); |
| 483 | async_certs.AddCert(c_by_d_); |
| 484 |
| 485 | CertPathBuilder path_builder( |
| 486 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 487 | initial_explicit_policy_, user_initial_policy_set_, |
| 488 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 489 | path_builder.AddCertIssuerSource(&sync_certs); |
| 490 | path_builder.AddCertIssuerSource(&async_certs); |
| 491 |
| 492 | auto result = path_builder.Run(); |
| 493 |
| 494 | ASSERT_TRUE(result.HasValidPath()); |
| 495 |
| 496 | // The partial path should be returned even though it didn't reach a trust |
| 497 | // anchor. |
| 498 | ASSERT_EQ(2U, result.paths.size()); |
| 499 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 500 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 501 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 502 | EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]); |
| 503 | EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]); |
| 504 |
| 505 | // The result path should be A(B) <- B(C) <- C(D) <- D(D) |
| 506 | EXPECT_EQ(1U, result.best_result_index); |
| 507 | EXPECT_TRUE(result.paths[1]->IsValid()); |
| 508 | const auto &path = *result.GetBestValidPath(); |
| 509 | ASSERT_EQ(4U, path.certs.size()); |
| 510 | EXPECT_EQ(a_by_b_, path.certs[0]); |
| 511 | EXPECT_EQ(b_by_c_, path.certs[1]); |
| 512 | EXPECT_EQ(c_by_d_, path.certs[2]); |
| 513 | EXPECT_EQ(d_by_d_, path.certs[3]); |
| 514 |
| 515 | VerifyError error = result.GetBestPathVerifyError(); |
| 516 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 517 | << error.DiagnosticString(); |
| 518 | } |
| 519 |
| 520 | // Test that if no path to a trust anchor was found, the partial path is |
| 521 | // returned. |
| 522 | TEST_F(PathBuilderMultiRootTest, TestOnlyPartialPathResult) { |
| 523 | TrustStoreInMemory trust_store; |
| 524 |
| 525 | // Certs B(F) and F(E) are supplied synchronously, thus the path |
| 526 | // A(B) <- B(F) <- F(E) should be built first, though it won't verify. |
| 527 | CertIssuerSourceStatic sync_certs; |
| 528 | sync_certs.AddCert(b_by_f_); |
| 529 | sync_certs.AddCert(f_by_e_); |
| 530 |
| 531 | CertPathBuilder path_builder( |
| 532 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 533 | initial_explicit_policy_, user_initial_policy_set_, |
| 534 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 535 | path_builder.AddCertIssuerSource(&sync_certs); |
| 536 |
| 537 | auto result = path_builder.Run(); |
| 538 |
| 539 | EXPECT_FALSE(result.HasValidPath()); |
| 540 |
| 541 | // The partial path should be returned even though it didn't reach a trust |
| 542 | // anchor. |
| 543 | ASSERT_EQ(1U, result.paths.size()); |
| 544 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 545 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 546 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 547 | EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]); |
| 548 | EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]); |
| 549 |
| 550 | VerifyError error = result.GetBestPathVerifyError(); |
| 551 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 552 | << error.DiagnosticString(); |
| 553 | } |
| 554 |
| 555 | // Test that if two partial paths are returned, the first is marked as the best |
| 556 | // path. |
| 557 | TEST_F(PathBuilderMultiRootTest, TestTwoPartialPathResults) { |
| 558 | TrustStoreInMemory trust_store; |
| 559 |
| 560 | // Certs B(F) and F(E) are supplied synchronously, thus the path |
| 561 | // A(B) <- B(F) <- F(E) should be built first, though it won't verify. |
| 562 | CertIssuerSourceStatic sync_certs; |
| 563 | sync_certs.AddCert(b_by_f_); |
| 564 | sync_certs.AddCert(f_by_e_); |
| 565 |
| 566 | // Certs B(C), and C(D) are supplied asynchronously, so the path |
| 567 | // A(B) <- B(C) <- C(D) <- D(D) should be tried second. |
| 568 | AsyncCertIssuerSourceStatic async_certs; |
| 569 | async_certs.AddCert(b_by_c_); |
| 570 | async_certs.AddCert(c_by_d_); |
| 571 |
| 572 | CertPathBuilder path_builder( |
| 573 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 574 | initial_explicit_policy_, user_initial_policy_set_, |
| 575 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 576 | path_builder.AddCertIssuerSource(&sync_certs); |
| 577 | path_builder.AddCertIssuerSource(&async_certs); |
| 578 |
| 579 | auto result = path_builder.Run(); |
| 580 |
| 581 | EXPECT_FALSE(result.HasValidPath()); |
| 582 |
| 583 | // First partial path found should be marked as the best one. |
| 584 | EXPECT_EQ(0U, result.best_result_index); |
| 585 |
| 586 | ASSERT_EQ(2U, result.paths.size()); |
| 587 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 588 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 589 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 590 | EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]); |
| 591 | EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]); |
| 592 |
| 593 | EXPECT_FALSE(result.paths[1]->IsValid()); |
| 594 | ASSERT_EQ(3U, result.paths[1]->certs.size()); |
| 595 | EXPECT_EQ(a_by_b_, result.paths[1]->certs[0]); |
| 596 | EXPECT_EQ(b_by_c_, result.paths[1]->certs[1]); |
| 597 | EXPECT_EQ(c_by_d_, result.paths[1]->certs[2]); |
| 598 |
| 599 | VerifyError error = result.GetBestPathVerifyError(); |
| 600 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 601 | << error.DiagnosticString(); |
| 602 | } |
| 603 |
| 604 | // Test that if no valid path is found, and the first invalid path is a partial |
| 605 | // path, but the 2nd invalid path ends with a cert with a trust record, the 2nd |
| 606 | // path should be preferred. |
| 607 | TEST_F(PathBuilderMultiRootTest, TestDistrustedPathPreferredOverPartialPath) { |
| 608 | // Only D(D) has a trust record, but it is distrusted. |
| 609 | TrustStoreInMemory trust_store; |
| 610 | trust_store.AddDistrustedCertificateForTest(d_by_d_); |
| 611 |
| 612 | // Certs B(F) and F(E) are supplied synchronously, thus the path |
| 613 | // A(B) <- B(F) <- F(E) should be built first, though it won't verify. |
| 614 | CertIssuerSourceStatic sync_certs; |
| 615 | sync_certs.AddCert(b_by_f_); |
| 616 | sync_certs.AddCert(f_by_e_); |
| 617 |
| 618 | // Certs B(C), and C(D) are supplied asynchronously, so the path |
| 619 | // A(B) <- B(C) <- C(D) <- D(D) should be tried second. |
| 620 | AsyncCertIssuerSourceStatic async_certs; |
| 621 | async_certs.AddCert(b_by_c_); |
| 622 | async_certs.AddCert(c_by_d_); |
| 623 |
| 624 | CertPathBuilder path_builder( |
| 625 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 626 | initial_explicit_policy_, user_initial_policy_set_, |
| 627 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 628 | path_builder.AddCertIssuerSource(&sync_certs); |
| 629 | path_builder.AddCertIssuerSource(&async_certs); |
| 630 |
| 631 | auto result = path_builder.Run(); |
| 632 |
| 633 | EXPECT_FALSE(result.HasValidPath()); |
| 634 |
| 635 | // The partial path should be returned even though it didn't reach a trust |
| 636 | // anchor. |
| 637 | ASSERT_EQ(2U, result.paths.size()); |
| 638 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 639 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 640 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 641 | EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]); |
| 642 | EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]); |
| 643 |
| 644 | // The result path should be A(B) <- B(C) <- C(D) <- D(D) |
| 645 | EXPECT_EQ(1U, result.best_result_index); |
| 646 | EXPECT_FALSE(result.paths[1]->IsValid()); |
| 647 | const auto &path = *result.GetBestPathPossiblyInvalid(); |
| 648 | ASSERT_EQ(4U, path.certs.size()); |
| 649 | EXPECT_EQ(a_by_b_, path.certs[0]); |
| 650 | EXPECT_EQ(b_by_c_, path.certs[1]); |
| 651 | EXPECT_EQ(c_by_d_, path.certs[2]); |
| 652 | EXPECT_EQ(d_by_d_, path.certs[3]); |
| 653 |
| 654 | VerifyError error = result.GetBestPathVerifyError(); |
| 655 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 656 | << error.DiagnosticString(); |
| 657 | } |
| 658 |
| 659 | // Test that whichever order CertIssuerSource returns the issuers, the path |
| 660 | // building still succeeds. |
| 661 | TEST_F(PathBuilderMultiRootTest, TestCertIssuerOrdering) { |
| 662 | // Only D(D) is a trusted root. |
| 663 | TrustStoreInMemory trust_store; |
| 664 | trust_store.AddTrustAnchor(d_by_d_); |
| 665 |
| 666 | for (bool reverse_order : {false, true}) { |
| 667 | SCOPED_TRACE(reverse_order); |
| 668 | std::vector<std::shared_ptr<const ParsedCertificate>> certs = { |
| 669 | b_by_c_, b_by_f_, f_by_e_, c_by_d_, c_by_e_}; |
| 670 | CertIssuerSourceStatic sync_certs; |
| 671 | if (reverse_order) { |
| 672 | for (auto it = certs.rbegin(); it != certs.rend(); ++it) { |
| 673 | sync_certs.AddCert(*it); |
| 674 | } |
| 675 | } else { |
| 676 | for (const auto &cert : certs) { |
| 677 | sync_certs.AddCert(cert); |
| 678 | } |
| 679 | } |
| 680 |
| 681 | CertPathBuilder path_builder( |
| 682 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 683 | initial_explicit_policy_, user_initial_policy_set_, |
| 684 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 685 | path_builder.AddCertIssuerSource(&sync_certs); |
| 686 |
| 687 | auto result = path_builder.Run(); |
| 688 |
| 689 | ASSERT_TRUE(result.HasValidPath()); |
| 690 |
| 691 | // The result path should be A(B) <- B(C) <- C(D) <- D(D) |
| 692 | const auto &path = *result.GetBestValidPath(); |
| 693 | ASSERT_EQ(4U, path.certs.size()); |
| 694 | EXPECT_EQ(a_by_b_, path.certs[0]); |
| 695 | EXPECT_EQ(b_by_c_, path.certs[1]); |
| 696 | EXPECT_EQ(c_by_d_, path.certs[2]); |
| 697 | EXPECT_EQ(d_by_d_, path.certs[3]); |
| 698 |
| 699 | VerifyError error = result.GetBestPathVerifyError(); |
| 700 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 701 | << error.DiagnosticString(); |
| 702 | } |
| 703 | } |
| 704 |
| 705 | TEST_F(PathBuilderMultiRootTest, TestIterationLimit) { |
| 706 | // D(D) is the trust root. |
| 707 | TrustStoreInMemory trust_store; |
| 708 | trust_store.AddTrustAnchor(d_by_d_); |
| 709 |
| 710 | // Certs B(C) and C(D) are supplied. |
| 711 | CertIssuerSourceStatic sync_certs; |
| 712 | sync_certs.AddCert(b_by_c_); |
| 713 | sync_certs.AddCert(c_by_d_); |
| 714 |
| 715 | for (const bool insufficient_limit : {true, false}) { |
| 716 | SCOPED_TRACE(insufficient_limit); |
| 717 |
| 718 | StrictMock<MockPathBuilderDelegate> mock_delegate; |
| 719 | // The CheckPathAfterVerification delegate should be called regardless if |
| 720 | // the iteration limit is reached. |
| 721 | EXPECT_CALL(mock_delegate, CheckPathAfterVerification(_, _)); |
| 722 |
| 723 | CertPathBuilder path_builder( |
| 724 | a_by_b_, &trust_store, &mock_delegate, time_, KeyPurpose::ANY_EKU, |
| 725 | initial_explicit_policy_, user_initial_policy_set_, |
| 726 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 727 | path_builder.AddCertIssuerSource(&sync_certs); |
| 728 |
| 729 | if (insufficient_limit) { |
| 730 | // A limit of one is insufficient to build a path in this case. Therefore |
| 731 | // building is expected to fail in this case. |
| 732 | path_builder.SetIterationLimit(1); |
| 733 | } else { |
| 734 | // The other tests in this file exercise the case that |SetIterationLimit| |
| 735 | // isn't called. Therefore set a sufficient limit for the path to be |
| 736 | // found. |
| 737 | path_builder.SetIterationLimit(5); |
| 738 | } |
| 739 |
| 740 | auto result = path_builder.Run(); |
| 741 |
| 742 | EXPECT_EQ(!insufficient_limit, result.HasValidPath()); |
| 743 | EXPECT_EQ(insufficient_limit, result.exceeded_iteration_limit); |
| 744 |
| 745 | VerifyError error = result.GetBestPathVerifyError(); |
| 746 | if (insufficient_limit) { |
| 747 | EXPECT_EQ(2U, result.iteration_count); |
| 748 | ASSERT_EQ(error.Code(), |
| 749 | VerifyError::StatusCode::PATH_ITERATION_COUNT_EXCEEDED) |
| 750 | << error.DiagnosticString(); |
| 751 | } else { |
| 752 | EXPECT_EQ(3U, result.iteration_count); |
| 753 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 754 | << error.DiagnosticString(); |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 |
| 759 | TEST_F(PathBuilderMultiRootTest, TestTrivialDeadline) { |
| 760 | // C(D) is the trust root. |
| 761 | TrustStoreInMemory trust_store; |
| 762 | trust_store.AddTrustAnchor(c_by_d_); |
| 763 |
| 764 | // Cert B(C) is supplied. |
| 765 | CertIssuerSourceStatic sync_certs; |
| 766 | sync_certs.AddCert(b_by_c_); |
| 767 |
| 768 | for (const bool insufficient_limit : {true, false}) { |
| 769 | SCOPED_TRACE(insufficient_limit); |
| 770 |
| 771 | CertPathBuilder path_builder( |
| 772 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 773 | initial_explicit_policy_, user_initial_policy_set_, |
| 774 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 775 | path_builder.AddCertIssuerSource(&sync_certs); |
| 776 |
| 777 | // Make the deadline either expired or not. |
| 778 | delegate_.SetDeadlineExpiredForTesting(insufficient_limit); |
| 779 |
| 780 | auto result = path_builder.Run(); |
| 781 |
| 782 | EXPECT_EQ(!insufficient_limit, result.HasValidPath()); |
| 783 | EXPECT_EQ(insufficient_limit, result.exceeded_deadline); |
| 784 | EXPECT_EQ(delegate_.IsDeadlineExpired(), insufficient_limit); |
| 785 |
| 786 | if (insufficient_limit) { |
| 787 | ASSERT_EQ(1U, result.paths.size()); |
| 788 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 789 | ASSERT_EQ(1U, result.paths[0]->certs.size()); |
| 790 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 791 | EXPECT_TRUE(result.paths[0]->errors.ContainsError( |
| 792 | cert_errors::kDeadlineExceeded)); |
| 793 | } else { |
| 794 | ASSERT_EQ(1U, result.paths.size()); |
| 795 | EXPECT_TRUE(result.paths[0]->IsValid()); |
| 796 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 797 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 798 | EXPECT_EQ(b_by_c_, result.paths[0]->certs[1]); |
| 799 | EXPECT_EQ(c_by_d_, result.paths[0]->certs[2]); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 |
| 804 | TEST_F(PathBuilderMultiRootTest, TestVerifyCache) { |
| 805 | // C(D) is the trust root. |
| 806 | TrustStoreInMemory trust_store; |
| 807 | trust_store.AddTrustAnchor(c_by_d_); |
| 808 |
| 809 | // Cert B(C) is supplied. |
| 810 | CertIssuerSourceStatic sync_certs; |
| 811 | sync_certs.AddCert(b_by_c_); |
| 812 |
| 813 | // Test Activation / DeActivation of the cache. |
| 814 | EXPECT_FALSE(delegate_.GetVerifyCache()); |
| 815 | delegate_.ActivateCache(); |
| 816 | EXPECT_TRUE(delegate_.GetVerifyCache()); |
| 817 | delegate_.DeActivateCache(); |
| 818 | EXPECT_FALSE(delegate_.GetVerifyCache()); |
| 819 | delegate_.ActivateCache(); |
| 820 | EXPECT_TRUE(delegate_.GetVerifyCache()); |
| 821 | for (size_t i = 0; i < 3; i++) { |
| 822 | SCOPED_TRACE(i); |
| 823 |
| 824 | CertPathBuilder path_builder( |
| 825 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 826 | initial_explicit_policy_, user_initial_policy_set_, |
| 827 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 828 | path_builder.AddCertIssuerSource(&sync_certs); |
| 829 |
| 830 | auto result = path_builder.Run(); |
| 831 |
| 832 | ASSERT_EQ(1U, result.paths.size()); |
| 833 | EXPECT_TRUE(result.paths[0]->IsValid()); |
| 834 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 835 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 836 | EXPECT_EQ(b_by_c_, result.paths[0]->certs[1]); |
| 837 | EXPECT_EQ(c_by_d_, result.paths[0]->certs[2]); |
| 838 |
| 839 | // The path is 3 certificates long, so requires 2 distinct signature |
| 840 | // verifications. The first time through the loop will cause 2 cache misses |
| 841 | // and stores, subsequent iterations will repeat the same verifications, |
| 842 | // causing 2 cache hits. |
| 843 | EXPECT_EQ(delegate_.GetMockVerifyCache()->CacheHits(), i * 2); |
| 844 | EXPECT_EQ(delegate_.GetMockVerifyCache()->CacheMisses(), 2U); |
| 845 | EXPECT_EQ(delegate_.GetMockVerifyCache()->CacheStores(), 2U); |
| 846 |
| 847 | VerifyError error = result.GetBestPathVerifyError(); |
| 848 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 849 | << error.DiagnosticString(); |
| 850 | } |
| 851 | } |
| 852 |
| 853 | TEST_F(PathBuilderMultiRootTest, TestDeadline) { |
| 854 | TrustStoreInMemory trust_store; |
| 855 | trust_store.AddTrustAnchor(d_by_d_); |
| 856 |
| 857 | // Cert B(C) is supplied statically. |
| 858 | CertIssuerSourceStatic sync_certs; |
| 859 | sync_certs.AddCert(b_by_c_); |
| 860 |
| 861 | // Cert C(D) is supplied asynchronously and will expire the deadline before |
| 862 | // returning the async result. |
| 863 | AsyncCertIssuerSourceStatic async_certs; |
| 864 | async_certs.AddCert(c_by_d_); |
| 865 | async_certs.SetAsyncGetCallback( |
| 866 | [&] { delegate_.SetDeadlineExpiredForTesting(true); }); |
| 867 |
| 868 | CertPathBuilder path_builder( |
| 869 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 870 | initial_explicit_policy_, user_initial_policy_set_, |
| 871 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 872 | path_builder.AddCertIssuerSource(&sync_certs); |
| 873 | path_builder.AddCertIssuerSource(&async_certs); |
| 874 |
| 875 | auto result = path_builder.Run(); |
| 876 |
| 877 | EXPECT_FALSE(result.HasValidPath()); |
| 878 | EXPECT_TRUE(result.exceeded_deadline); |
| 879 | EXPECT_TRUE(delegate_.IsDeadlineExpired()); |
| 880 |
| 881 | // The chain returned should end in c_by_d_, since the deadline would only be |
| 882 | // checked again after the async results had been checked (since |
| 883 | // AsyncCertIssuerSourceStatic makes the async results available immediately.) |
| 884 | ASSERT_EQ(1U, result.paths.size()); |
| 885 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 886 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 887 | EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]); |
| 888 | EXPECT_EQ(b_by_c_, result.paths[0]->certs[1]); |
| 889 | EXPECT_EQ(c_by_d_, result.paths[0]->certs[2]); |
| 890 | EXPECT_TRUE( |
| 891 | result.paths[0]->errors.ContainsError(cert_errors::kDeadlineExceeded)); |
| 892 |
| 893 | VerifyError error = result.GetBestPathVerifyError(); |
| 894 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_DEADLINE_EXCEEDED) |
| 895 | << error.DiagnosticString(); |
| 896 | } |
| 897 |
| 898 | TEST_F(PathBuilderMultiRootTest, TestDepthLimit) { |
| 899 | // D(D) is the trust root. |
| 900 | TrustStoreInMemory trust_store; |
| 901 | trust_store.AddTrustAnchor(d_by_d_); |
| 902 |
| 903 | // Certs B(C) and C(D) are supplied. |
| 904 | CertIssuerSourceStatic sync_certs; |
| 905 | sync_certs.AddCert(b_by_c_); |
| 906 | sync_certs.AddCert(c_by_d_); |
| 907 |
| 908 | for (const bool insufficient_limit : {true, false}) { |
| 909 | CertPathBuilder path_builder( |
| 910 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 911 | initial_explicit_policy_, user_initial_policy_set_, |
| 912 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 913 | path_builder.AddCertIssuerSource(&sync_certs); |
| 914 |
| 915 | if (insufficient_limit) { |
| 916 | // A limit of depth equal to 2 is insufficient to build the path. |
| 917 | // Therefore, building is expected to fail. |
| 918 | path_builder.SetDepthLimit(2); |
| 919 | } else { |
| 920 | // The other tests in this file exercise the case that |SetDepthLimit| |
| 921 | // isn't called. Therefore, set a sufficient limit for the path to be |
| 922 | // found. |
| 923 | path_builder.SetDepthLimit(5); |
| 924 | } |
| 925 |
| 926 | auto result = path_builder.Run(); |
| 927 |
| 928 | EXPECT_EQ(!insufficient_limit, result.HasValidPath()); |
| 929 | EXPECT_EQ(insufficient_limit, |
| 930 | result.AnyPathContainsError(cert_errors::kDepthLimitExceeded)); |
| 931 | VerifyError error = result.GetBestPathVerifyError(); |
| 932 | if (insufficient_limit) { |
| 933 | EXPECT_EQ(2U, result.max_depth_seen); |
| 934 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_DEPTH_LIMIT_REACHED) |
| 935 | << error.DiagnosticString(); |
| 936 | } else { |
| 937 | EXPECT_EQ(4U, result.max_depth_seen); |
| 938 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 939 | << error.DiagnosticString(); |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 |
| 944 | TEST_F(PathBuilderMultiRootTest, TestDepthLimitMultiplePaths) { |
| 945 | // This case tests path building backtracking due to reaching the path depth |
| 946 | // limit. Given the root and issuer certificates below, there can be two paths |
| 947 | // from between the leaf to a trusted root, one has length of 3 and the other |
| 948 | // has length of 4. These certificates are specifically chosen because path |
| 949 | // building will first explore the 4-certificate long path then the |
| 950 | // 3-certificate long path. So with a depth limit of 3, we can test the |
| 951 | // backtracking code path. |
| 952 |
| 953 | // E(E) and C(D) are the trust roots. |
| 954 | TrustStoreInMemory trust_store; |
| 955 | trust_store.AddTrustAnchor(e_by_e_); |
| 956 | trust_store.AddTrustAnchor(c_by_d_); |
| 957 |
| 958 | // Certs B(C). B(F) and F(E) are supplied. |
| 959 | CertIssuerSourceStatic sync_certs; |
| 960 | sync_certs.AddCert(b_by_c_); |
| 961 | sync_certs.AddCert(b_by_f_); |
| 962 | sync_certs.AddCert(f_by_e_); |
| 963 |
| 964 | CertPathBuilder path_builder( |
| 965 | a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 966 | initial_explicit_policy_, user_initial_policy_set_, |
| 967 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 968 | path_builder.AddCertIssuerSource(&sync_certs); |
| 969 |
| 970 | path_builder.SetDepthLimit(3); |
| 971 |
| 972 | auto result = path_builder.Run(); |
| 973 |
| 974 | EXPECT_TRUE(result.HasValidPath()); |
| 975 | EXPECT_TRUE(result.AnyPathContainsError(cert_errors::kDepthLimitExceeded)); |
| 976 |
| 977 | ASSERT_EQ(result.paths.size(), 2u); |
| 978 |
| 979 | const CertPathBuilderResultPath *truncated_path = result.paths[0].get(); |
| 980 | EXPECT_FALSE(truncated_path->IsValid()); |
| 981 | EXPECT_TRUE( |
| 982 | truncated_path->errors.ContainsError(cert_errors::kDepthLimitExceeded)); |
| 983 | ASSERT_EQ(truncated_path->certs.size(), 3u); |
| 984 | EXPECT_EQ(a_by_b_, truncated_path->certs[0]); |
| 985 | EXPECT_EQ(b_by_f_, truncated_path->certs[1]); |
| 986 | EXPECT_EQ(f_by_e_, truncated_path->certs[2]); |
| 987 |
| 988 | const CertPathBuilderResultPath *valid_path = result.paths[1].get(); |
| 989 | EXPECT_TRUE(valid_path->IsValid()); |
| 990 | EXPECT_FALSE( |
| 991 | valid_path->errors.ContainsError(cert_errors::kDepthLimitExceeded)); |
| 992 | ASSERT_EQ(valid_path->certs.size(), 3u); |
| 993 | EXPECT_EQ(a_by_b_, valid_path->certs[0]); |
| 994 | EXPECT_EQ(b_by_c_, valid_path->certs[1]); |
| 995 | EXPECT_EQ(c_by_d_, valid_path->certs[2]); |
| 996 |
| 997 | VerifyError error = result.GetBestPathVerifyError(); |
| 998 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 999 | << error.DiagnosticString(); |
| 1000 | } |
| 1001 |
| 1002 | TEST_F(PathBuilderMultiRootTest, TestPreCertificate) { |
| 1003 |
| 1004 | std::string test_dir = |
| 1005 | "testdata/path_builder_unittest/precertificate/"; |
| 1006 | std::shared_ptr<const ParsedCertificate> root1 = |
| 1007 | ReadCertFromFile(test_dir + "root.pem"); |
| 1008 | ASSERT_TRUE(root1); |
| 1009 | std::shared_ptr<const ParsedCertificate> target = |
| 1010 | ReadCertFromFile(test_dir + "precertificate.pem"); |
| 1011 | ASSERT_TRUE(target); |
| 1012 |
| 1013 | der::GeneralizedTime precert_time = {2023, 10, 1, 0, 0, 0}; |
| 1014 |
| 1015 | TrustStoreInMemory trust_store; |
| 1016 | trust_store.AddTrustAnchor(root1); |
| 1017 |
| 1018 | // PreCertificate should be rejected by default. |
| 1019 | EXPECT_FALSE(delegate_.AcceptPreCertificates()); |
| 1020 | CertPathBuilder path_builder( |
| 1021 | target, &trust_store, &delegate_, precert_time, KeyPurpose::ANY_EKU, |
| 1022 | initial_explicit_policy_, user_initial_policy_set_, |
| 1023 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1024 | auto result = path_builder.Run(); |
| 1025 | ASSERT_EQ(1U, result.paths.size()); |
| 1026 | ASSERT_FALSE(result.paths[0]->IsValid()) |
| 1027 | << result.paths[0]->errors.ToDebugString(result.paths[0]->certs); |
| 1028 | VerifyError error = result.GetBestPathVerifyError(); |
| 1029 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::CERTIFICATE_INVALID) |
| 1030 | << error.DiagnosticString(); |
| 1031 |
| 1032 |
| 1033 | // PreCertificate should be accepted if configured. |
| 1034 | delegate_.AllowPrecert(); |
| 1035 | EXPECT_TRUE(delegate_.AcceptPreCertificates()); |
| 1036 | CertPathBuilder path_builder2( |
| 1037 | target, &trust_store, &delegate_, precert_time, KeyPurpose::ANY_EKU, |
| 1038 | initial_explicit_policy_, user_initial_policy_set_, |
| 1039 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1040 | auto result2 = path_builder2.Run(); |
| 1041 | ASSERT_EQ(1U, result2.paths.size()); |
| 1042 | ASSERT_TRUE(result2.paths[0]->IsValid()) |
| 1043 | << result2.paths[0]->errors.ToDebugString(result.paths[0]->certs); |
| 1044 | VerifyError error2 = result2.GetBestPathVerifyError(); |
| 1045 | ASSERT_EQ(error2.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1046 | << error2.DiagnosticString(); |
| 1047 | } |
| 1048 |
| 1049 | class PathBuilderKeyRolloverTest : public ::testing::Test { |
| 1050 | public: |
| 1051 | PathBuilderKeyRolloverTest() |
| 1052 | : delegate_(1024, |
| 1053 | SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1) {} |
| 1054 |
| 1055 | void SetUp() override { |
| 1056 | ParsedCertificateList path; |
| 1057 |
| 1058 | VerifyCertChainTest test; |
| 1059 | ASSERT_TRUE(ReadVerifyCertChainTestFromFile( |
| 1060 | "testdata/verify_certificate_chain_unittest/key-rollover/oldchain.test", |
| 1061 | &test)); |
| 1062 | path = test.chain; |
| 1063 | ASSERT_EQ(3U, path.size()); |
| 1064 | target_ = path[0]; |
| 1065 | oldintermediate_ = path[1]; |
| 1066 | oldroot_ = path[2]; |
| 1067 | time_ = test.time; |
| 1068 |
| 1069 | ASSERT_TRUE(target_); |
| 1070 | ASSERT_TRUE(oldintermediate_); |
| 1071 |
| 1072 | ASSERT_TRUE(ReadVerifyCertChainTestFromFile( |
| 1073 | "testdata/verify_certificate_chain_unittest/" |
| 1074 | "key-rollover/longrolloverchain.test", |
| 1075 | &test)); |
| 1076 | path = test.chain; |
| 1077 |
| 1078 | ASSERT_EQ(5U, path.size()); |
| 1079 | newintermediate_ = path[1]; |
| 1080 | newroot_ = path[2]; |
| 1081 | newrootrollover_ = path[3]; |
| 1082 | ASSERT_TRUE(newintermediate_); |
| 1083 | ASSERT_TRUE(newroot_); |
| 1084 | ASSERT_TRUE(newrootrollover_); |
| 1085 | } |
| 1086 |
| 1087 | protected: |
| 1088 | // oldroot-------->newrootrollover newroot |
| 1089 | // | | | |
| 1090 | // v v v |
| 1091 | // oldintermediate newintermediate |
| 1092 | // | | |
| 1093 | // +------------+-------------+ |
| 1094 | // | |
| 1095 | // v |
| 1096 | // target |
| 1097 | std::shared_ptr<const ParsedCertificate> target_; |
| 1098 | std::shared_ptr<const ParsedCertificate> oldintermediate_; |
| 1099 | std::shared_ptr<const ParsedCertificate> newintermediate_; |
| 1100 | std::shared_ptr<const ParsedCertificate> oldroot_; |
| 1101 | std::shared_ptr<const ParsedCertificate> newroot_; |
| 1102 | std::shared_ptr<const ParsedCertificate> newrootrollover_; |
| 1103 |
| 1104 | SimplePathBuilderDelegate delegate_; |
| 1105 | der::GeneralizedTime time_; |
| 1106 |
| 1107 | const InitialExplicitPolicy initial_explicit_policy_ = |
| 1108 | InitialExplicitPolicy::kFalse; |
| 1109 | const std::set<der::Input> user_initial_policy_set_ = { |
| 1110 | der::Input(kAnyPolicyOid)}; |
| 1111 | const InitialPolicyMappingInhibit initial_policy_mapping_inhibit_ = |
| 1112 | InitialPolicyMappingInhibit::kFalse; |
| 1113 | const InitialAnyPolicyInhibit initial_any_policy_inhibit_ = |
| 1114 | InitialAnyPolicyInhibit::kFalse; |
| 1115 | }; |
| 1116 |
| 1117 | // Tests that if only the old root cert is trusted, the path builder can build a |
| 1118 | // path through the new intermediate and rollover cert to the old root. |
| 1119 | TEST_F(PathBuilderKeyRolloverTest, TestRolloverOnlyOldRootTrusted) { |
| 1120 | // Only oldroot is trusted. |
| 1121 | TrustStoreInMemory trust_store; |
| 1122 | trust_store.AddTrustAnchor(oldroot_); |
| 1123 |
| 1124 | // Old intermediate cert is not provided, so the pathbuilder will need to go |
| 1125 | // through the rollover cert. |
| 1126 | CertIssuerSourceStatic sync_certs; |
| 1127 | sync_certs.AddCert(newintermediate_); |
| 1128 | sync_certs.AddCert(newrootrollover_); |
| 1129 |
| 1130 | CertPathBuilder path_builder( |
| 1131 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1132 | initial_explicit_policy_, user_initial_policy_set_, |
| 1133 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1134 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1135 |
| 1136 | auto result = path_builder.Run(); |
| 1137 |
| 1138 | EXPECT_TRUE(result.HasValidPath()); |
| 1139 |
| 1140 | // Due to authorityKeyIdentifier prioritization, path builder will first |
| 1141 | // attempt: target <- newintermediate <- newrootrollover <- oldroot |
| 1142 | // which will succeed. |
| 1143 | ASSERT_EQ(1U, result.paths.size()); |
| 1144 | const auto &path0 = *result.paths[0]; |
| 1145 | EXPECT_EQ(0U, result.best_result_index); |
| 1146 | EXPECT_TRUE(path0.IsValid()); |
| 1147 | ASSERT_EQ(4U, path0.certs.size()); |
| 1148 | EXPECT_EQ(target_, path0.certs[0]); |
| 1149 | EXPECT_EQ(newintermediate_, path0.certs[1]); |
| 1150 | EXPECT_EQ(newrootrollover_, path0.certs[2]); |
| 1151 | EXPECT_EQ(oldroot_, path0.certs[3]); |
| 1152 |
| 1153 | VerifyError error = result.GetBestPathVerifyError(); |
| 1154 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1155 | << error.DiagnosticString(); |
| 1156 | } |
| 1157 |
| 1158 | // Tests that if both old and new roots are trusted it builds a path through |
| 1159 | // the new intermediate. |
| 1160 | TEST_F(PathBuilderKeyRolloverTest, TestRolloverBothRootsTrusted) { |
| 1161 | // Both oldroot and newroot are trusted. |
| 1162 | TrustStoreInMemory trust_store; |
| 1163 | trust_store.AddTrustAnchor(oldroot_); |
| 1164 | trust_store.AddTrustAnchor(newroot_); |
| 1165 |
| 1166 | // Both old and new intermediates + rollover cert are provided. |
| 1167 | CertIssuerSourceStatic sync_certs; |
| 1168 | sync_certs.AddCert(oldintermediate_); |
| 1169 | sync_certs.AddCert(newintermediate_); |
| 1170 | sync_certs.AddCert(newrootrollover_); |
| 1171 |
| 1172 | CertPathBuilder path_builder( |
| 1173 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1174 | initial_explicit_policy_, user_initial_policy_set_, |
| 1175 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1176 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1177 |
| 1178 | auto result = path_builder.Run(); |
| 1179 |
| 1180 | EXPECT_TRUE(result.HasValidPath()); |
| 1181 |
| 1182 | ASSERT_EQ(1U, result.paths.size()); |
| 1183 | const auto &path = *result.paths[0]; |
| 1184 | EXPECT_TRUE(result.paths[0]->IsValid()); |
| 1185 | ASSERT_EQ(3U, path.certs.size()); |
| 1186 | EXPECT_EQ(target_, path.certs[0]); |
| 1187 | // The newer intermediate should be used as newer certs are prioritized in |
| 1188 | // path building. |
| 1189 | EXPECT_EQ(newintermediate_, path.certs[1]); |
| 1190 | EXPECT_EQ(newroot_, path.certs[2]); |
| 1191 |
| 1192 | VerifyError error = result.GetBestPathVerifyError(); |
| 1193 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1194 | << error.DiagnosticString(); |
| 1195 | } |
| 1196 |
| 1197 | // If trust anchor query returned no results, and there are no issuer |
| 1198 | // sources, path building should fail at that point. |
| 1199 | TEST_F(PathBuilderKeyRolloverTest, TestAnchorsNoMatchAndNoIssuerSources) { |
| 1200 | TrustStoreInMemory trust_store; |
| 1201 | trust_store.AddTrustAnchor(newroot_); |
| 1202 |
| 1203 | CertPathBuilder path_builder( |
| 1204 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1205 | initial_explicit_policy_, user_initial_policy_set_, |
| 1206 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1207 |
| 1208 | auto result = path_builder.Run(); |
| 1209 |
| 1210 | EXPECT_FALSE(result.HasValidPath()); |
| 1211 |
| 1212 | ASSERT_EQ(1U, result.paths.size()); |
| 1213 | const auto &path = *result.paths[0]; |
| 1214 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 1215 | ASSERT_EQ(1U, path.certs.size()); |
| 1216 | EXPECT_EQ(target_, path.certs[0]); |
| 1217 |
| 1218 | VerifyError error = result.GetBestPathVerifyError(); |
| 1219 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 1220 | << error.DiagnosticString(); |
| 1221 | } |
| 1222 |
| 1223 | // If a path to a trust anchor could not be found, and the last issuer(s) in |
| 1224 | // the chain were culled by the loop checker, the partial path up to that point |
| 1225 | // should be returned. |
| 1226 | TEST_F(PathBuilderKeyRolloverTest, TestReturnsPartialPathEndedByLoopChecker) { |
| 1227 | TrustStoreInMemory trust_store; |
| 1228 |
| 1229 | CertIssuerSourceStatic sync_certs; |
| 1230 | sync_certs.AddCert(newintermediate_); |
| 1231 | sync_certs.AddCert(newroot_); |
| 1232 |
| 1233 | CertIssuerSourceStatic rollover_certs; |
| 1234 | rollover_certs.AddCert(newrootrollover_); |
| 1235 |
| 1236 | CertPathBuilder path_builder( |
| 1237 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1238 | initial_explicit_policy_, user_initial_policy_set_, |
| 1239 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1240 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1241 | // The rollover root is added as a second issuer source to ensure we get paths |
| 1242 | // back in a deterministic order, otherwise newroot and newrootrollover do not |
| 1243 | // differ in any way that the path builder would use for prioritizing which |
| 1244 | // path comes back first. |
| 1245 | path_builder.AddCertIssuerSource(&rollover_certs); |
| 1246 |
| 1247 | auto result = path_builder.Run(); |
| 1248 |
| 1249 | EXPECT_FALSE(result.HasValidPath()); |
| 1250 | ASSERT_EQ(2U, result.paths.size()); |
| 1251 |
| 1252 | // Since none of the certs are trusted, the path builder should build 4 |
| 1253 | // candidate paths, all of which are disallowed due to the loop checker: |
| 1254 | // target->newintermediate->newroot->newroot |
| 1255 | // target->newintermediate->newroot->newrootrollover |
| 1256 | // target->newintermediate->newrootrollover->newroot |
| 1257 | // target->newintermediate->newrootrollover->newrootrollover |
| 1258 | // This should end up returning the 2 partial paths which are the longest |
| 1259 | // paths for which no acceptable issuers could be found: |
| 1260 | // target->newintermediate->newroot |
| 1261 | // target->newintermediate->newrootrollover |
| 1262 |
| 1263 | { |
| 1264 | const auto &path = *result.paths[0]; |
| 1265 | EXPECT_FALSE(path.IsValid()); |
| 1266 | ASSERT_EQ(3U, path.certs.size()); |
| 1267 | EXPECT_EQ(target_, path.certs[0]); |
| 1268 | EXPECT_EQ(newintermediate_, path.certs[1]); |
| 1269 | EXPECT_EQ(newroot_, path.certs[2]); |
| 1270 | EXPECT_TRUE(path.errors.ContainsError(cert_errors::kNoIssuersFound)); |
| 1271 | } |
| 1272 |
| 1273 | { |
| 1274 | const auto &path = *result.paths[1]; |
| 1275 | EXPECT_FALSE(path.IsValid()); |
| 1276 | ASSERT_EQ(3U, path.certs.size()); |
| 1277 | EXPECT_EQ(target_, path.certs[0]); |
| 1278 | EXPECT_EQ(newintermediate_, path.certs[1]); |
| 1279 | EXPECT_EQ(newrootrollover_, path.certs[2]); |
| 1280 | EXPECT_TRUE(path.errors.ContainsError(cert_errors::kNoIssuersFound)); |
| 1281 | } |
| 1282 |
| 1283 | VerifyError error = result.GetBestPathVerifyError(); |
| 1284 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 1285 | << error.DiagnosticString(); |
| 1286 | } |
| 1287 |
| 1288 | // Tests that multiple trust root matches on a single path will be considered. |
| 1289 | // Both roots have the same subject but different keys. Only one of them will |
| 1290 | // verify. |
| 1291 | TEST_F(PathBuilderKeyRolloverTest, TestMultipleRootMatchesOnlyOneWorks) { |
| 1292 | TrustStoreCollection trust_store_collection; |
| 1293 | TrustStoreInMemory trust_store1; |
| 1294 | TrustStoreInMemory trust_store2; |
| 1295 | trust_store_collection.AddTrustStore(&trust_store1); |
| 1296 | trust_store_collection.AddTrustStore(&trust_store2); |
| 1297 | // Add two trust anchors (newroot_ and oldroot_). Path building will attempt |
| 1298 | // them in this same order, as trust_store1 was added to |
| 1299 | // trust_store_collection first. |
| 1300 | trust_store1.AddTrustAnchor(newroot_); |
| 1301 | trust_store2.AddTrustAnchor(oldroot_); |
| 1302 |
| 1303 | // Only oldintermediate is supplied, so the path with newroot should fail, |
| 1304 | // oldroot should succeed. |
| 1305 | CertIssuerSourceStatic sync_certs; |
| 1306 | sync_certs.AddCert(oldintermediate_); |
| 1307 |
| 1308 | CertPathBuilder path_builder( |
| 1309 | target_, &trust_store_collection, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1310 | initial_explicit_policy_, user_initial_policy_set_, |
| 1311 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1312 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1313 |
| 1314 | auto result = path_builder.Run(); |
| 1315 |
| 1316 | EXPECT_TRUE(result.HasValidPath()); |
| 1317 | ASSERT_EQ(1U, result.paths.size()); |
| 1318 |
| 1319 | // Due to authorityKeyIdentifier prioritization, path builder will first |
| 1320 | // attempt: target <- old intermediate <- oldroot |
| 1321 | // which should succeed. |
| 1322 | EXPECT_TRUE(result.paths[result.best_result_index]->IsValid()); |
| 1323 | const auto &path = *result.paths[result.best_result_index]; |
| 1324 | ASSERT_EQ(3U, path.certs.size()); |
| 1325 | EXPECT_EQ(target_, path.certs[0]); |
| 1326 | EXPECT_EQ(oldintermediate_, path.certs[1]); |
| 1327 | EXPECT_EQ(oldroot_, path.certs[2]); |
| 1328 |
| 1329 | VerifyError error = result.GetBestPathVerifyError(); |
| 1330 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1331 | << error.DiagnosticString(); |
| 1332 | } |
| 1333 |
| 1334 | // Tests that the path builder doesn't build longer than necessary paths, |
| 1335 | // by skipping certs where the same Name+SAN+SPKI is already in the current |
| 1336 | // path. |
| 1337 | TEST_F(PathBuilderKeyRolloverTest, TestRolloverLongChain) { |
| 1338 | // Only oldroot is trusted. |
| 1339 | TrustStoreInMemory trust_store; |
| 1340 | trust_store.AddTrustAnchor(oldroot_); |
| 1341 |
| 1342 | // New intermediate and new root are provided synchronously. |
| 1343 | CertIssuerSourceStatic sync_certs; |
| 1344 | sync_certs.AddCert(newintermediate_); |
| 1345 | sync_certs.AddCert(newroot_); |
| 1346 |
| 1347 | // Rollover cert is only provided asynchronously. This will force the |
| 1348 | // pathbuilder to first try building a longer than necessary path. |
| 1349 | AsyncCertIssuerSourceStatic async_certs; |
| 1350 | async_certs.AddCert(newrootrollover_); |
| 1351 |
| 1352 | CertPathBuilder path_builder( |
| 1353 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1354 | initial_explicit_policy_, user_initial_policy_set_, |
| 1355 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1356 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1357 | path_builder.AddCertIssuerSource(&async_certs); |
| 1358 |
| 1359 | auto result = path_builder.Run(); |
| 1360 |
| 1361 | EXPECT_TRUE(result.HasValidPath()); |
| 1362 | ASSERT_EQ(3U, result.paths.size()); |
| 1363 |
| 1364 | // Path builder will first attempt: |
| 1365 | // target <- newintermediate <- newroot <- oldroot |
| 1366 | // but it will fail since newroot is self-signed. |
| 1367 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 1368 | const auto &path0 = *result.paths[0]; |
| 1369 | ASSERT_EQ(4U, path0.certs.size()); |
| 1370 | EXPECT_EQ(target_, path0.certs[0]); |
| 1371 | EXPECT_EQ(newintermediate_, path0.certs[1]); |
| 1372 | EXPECT_EQ(newroot_, path0.certs[2]); |
| 1373 | EXPECT_EQ(oldroot_, path0.certs[3]); |
| 1374 |
| 1375 | // Path builder will next attempt: target <- newintermediate <- oldroot |
| 1376 | // but it will fail since newintermediate is signed by newroot. |
| 1377 | EXPECT_FALSE(result.paths[1]->IsValid()); |
| 1378 | const auto &path1 = *result.paths[1]; |
| 1379 | ASSERT_EQ(3U, path1.certs.size()); |
| 1380 | EXPECT_EQ(target_, path1.certs[0]); |
| 1381 | EXPECT_EQ(newintermediate_, path1.certs[1]); |
| 1382 | EXPECT_EQ(oldroot_, path1.certs[2]); |
| 1383 |
| 1384 | // Path builder will skip: |
| 1385 | // target <- newintermediate <- newroot <- newrootrollover <- ... |
| 1386 | // Since newroot and newrootrollover have the same Name+SAN+SPKI. |
| 1387 |
| 1388 | // Finally path builder will use: |
| 1389 | // target <- newintermediate <- newrootrollover <- oldroot |
| 1390 | EXPECT_EQ(2U, result.best_result_index); |
| 1391 | EXPECT_TRUE(result.paths[2]->IsValid()); |
| 1392 | const auto &path2 = *result.paths[2]; |
| 1393 | ASSERT_EQ(4U, path2.certs.size()); |
| 1394 | EXPECT_EQ(target_, path2.certs[0]); |
| 1395 | EXPECT_EQ(newintermediate_, path2.certs[1]); |
| 1396 | EXPECT_EQ(newrootrollover_, path2.certs[2]); |
| 1397 | EXPECT_EQ(oldroot_, path2.certs[3]); |
| 1398 |
| 1399 | VerifyError error = result.GetBestPathVerifyError(); |
| 1400 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1401 | << error.DiagnosticString(); |
| 1402 | } |
| 1403 |
| 1404 | // Tests that when SetExploreAllPaths is combined with SetIterationLimit the |
| 1405 | // path builder will return all the paths that were able to be built before the |
| 1406 | // iteration limit was reached. |
| 1407 | TEST_F(PathBuilderKeyRolloverTest, ExploreAllPathsWithIterationLimit) { |
| 1408 | struct Expectation { |
| 1409 | int iteration_limit; |
| 1410 | size_t expected_num_paths; |
| 1411 | std::vector<std::shared_ptr<const ParsedCertificate>> partial_path; |
| 1412 | } kExpectations[] = { |
| 1413 | // No iteration limit. All possible paths should be built. |
| 1414 | {0, 4, {}}, |
| 1415 | // Limit 1 is only enough to reach the intermediate, no complete path |
| 1416 | // should be built. |
| 1417 | {1, 0, {target_, newintermediate_}}, |
| 1418 | // Limit 2 allows reaching the root on the first path. |
| 1419 | {2, 1, {target_, newintermediate_}}, |
| 1420 | // Next iteration uses oldroot instead of newroot. |
| 1421 | {3, 2, {target_, newintermediate_}}, |
| 1422 | // Backtracking to the target cert. |
| 1423 | {4, 2, {target_}}, |
| 1424 | // Adding oldintermediate. |
| 1425 | {5, 2, {target_, oldintermediate_}}, |
| 1426 | // Trying oldroot. |
| 1427 | {6, 3, {target_, oldintermediate_}}, |
| 1428 | // Trying newroot. |
| 1429 | {7, 4, {target_, oldintermediate_}}, |
| 1430 | }; |
| 1431 |
| 1432 | // Trust both old and new roots. |
| 1433 | TrustStoreInMemory trust_store; |
| 1434 | trust_store.AddTrustAnchor(oldroot_); |
| 1435 | trust_store.AddTrustAnchor(newroot_); |
| 1436 |
| 1437 | // Intermediates and root rollover are all provided synchronously. |
| 1438 | CertIssuerSourceStatic sync_certs; |
| 1439 | sync_certs.AddCert(oldintermediate_); |
| 1440 | sync_certs.AddCert(newintermediate_); |
| 1441 |
| 1442 | for (const auto &expectation : kExpectations) { |
| 1443 | SCOPED_TRACE(expectation.iteration_limit); |
| 1444 |
| 1445 | CertPathBuilder path_builder( |
| 1446 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1447 | initial_explicit_policy_, user_initial_policy_set_, |
| 1448 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1449 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1450 |
| 1451 | // Explore all paths, rather than stopping at the first valid path. |
| 1452 | path_builder.SetExploreAllPaths(true); |
| 1453 |
| 1454 | // Limit the number of iterations. |
| 1455 | path_builder.SetIterationLimit(expectation.iteration_limit); |
| 1456 |
| 1457 | auto result = path_builder.Run(); |
| 1458 |
| 1459 | EXPECT_EQ(expectation.expected_num_paths > 0, result.HasValidPath()); |
| 1460 | VerifyError error = result.GetBestPathVerifyError(); |
| 1461 | if (expectation.expected_num_paths > 0) { |
| 1462 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1463 | << error.DiagnosticString(); |
| 1464 | } else { |
| 1465 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_ITERATION_COUNT_EXCEEDED) |
| 1466 | << error.DiagnosticString(); |
| 1467 | } |
| 1468 |
| 1469 | if (expectation.partial_path.empty()) { |
| 1470 | ASSERT_EQ(expectation.expected_num_paths, result.paths.size()); |
| 1471 | } else { |
| 1472 | ASSERT_EQ(1 + expectation.expected_num_paths, result.paths.size()); |
| 1473 | const auto &path = *result.paths[result.paths.size() - 1]; |
| 1474 | EXPECT_FALSE(path.IsValid()); |
| 1475 | EXPECT_EQ(expectation.partial_path, path.certs); |
| 1476 | EXPECT_TRUE( |
| 1477 | path.errors.ContainsError(cert_errors::kIterationLimitExceeded)); |
| 1478 | } |
| 1479 |
| 1480 | if (expectation.expected_num_paths > 0) { |
| 1481 | // Path builder will first build path: target <- newintermediate <- |
| 1482 | // newroot |
| 1483 | const auto &path0 = *result.paths[0]; |
| 1484 | EXPECT_TRUE(path0.IsValid()); |
| 1485 | ASSERT_EQ(3U, path0.certs.size()); |
| 1486 | EXPECT_EQ(target_, path0.certs[0]); |
| 1487 | EXPECT_EQ(newintermediate_, path0.certs[1]); |
| 1488 | EXPECT_EQ(newroot_, path0.certs[2]); |
| 1489 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1490 | } |
| 1491 |
| 1492 | if (expectation.expected_num_paths > 1) { |
| 1493 | // Next path: target <- newintermediate <- oldroot |
| 1494 | const auto &path1 = *result.paths[1]; |
| 1495 | EXPECT_FALSE(path1.IsValid()); |
| 1496 | ASSERT_EQ(3U, path1.certs.size()); |
| 1497 | EXPECT_EQ(target_, path1.certs[0]); |
| 1498 | EXPECT_EQ(newintermediate_, path1.certs[1]); |
| 1499 | EXPECT_EQ(oldroot_, path1.certs[2]); |
| 1500 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1501 | } |
| 1502 |
| 1503 | if (expectation.expected_num_paths > 2) { |
| 1504 | // Next path: target <- oldintermediate <- oldroot |
| 1505 | const auto &path2 = *result.paths[2]; |
| 1506 | EXPECT_TRUE(path2.IsValid()); |
| 1507 | ASSERT_EQ(3U, path2.certs.size()); |
| 1508 | EXPECT_EQ(target_, path2.certs[0]); |
| 1509 | EXPECT_EQ(oldintermediate_, path2.certs[1]); |
| 1510 | EXPECT_EQ(oldroot_, path2.certs[2]); |
| 1511 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1512 | } |
| 1513 |
| 1514 | if (expectation.expected_num_paths > 3) { |
| 1515 | // Final path: target <- oldintermediate <- newroot |
| 1516 | const auto &path3 = *result.paths[3]; |
| 1517 | EXPECT_FALSE(path3.IsValid()); |
| 1518 | ASSERT_EQ(3U, path3.certs.size()); |
| 1519 | EXPECT_EQ(target_, path3.certs[0]); |
| 1520 | EXPECT_EQ(oldintermediate_, path3.certs[1]); |
| 1521 | EXPECT_EQ(newroot_, path3.certs[2]); |
| 1522 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 |
| 1527 | // Tests that when SetValidPathLimit is used path builder returns the number of |
| 1528 | // valid paths we expect before the valid path limit was reached. |
| 1529 | TEST_F(PathBuilderKeyRolloverTest, ExplorePathsWithPathLimit) { |
| 1530 | struct Expectation { |
| 1531 | size_t valid_path_limit; |
| 1532 | size_t expected_num_paths; |
| 1533 | } kExpectations[] = { |
| 1534 | {0, 4}, // No path limit. Three valid, one partial path should be built |
| 1535 | {1, 1}, // One valid path |
| 1536 | {2, 3}, // Two valid, one partial |
| 1537 | {3, 4}, {4, 4}, {5, 4}, |
| 1538 | }; |
| 1539 |
| 1540 | // Trust both old and new roots. |
| 1541 | TrustStoreInMemory trust_store; |
| 1542 | trust_store.AddTrustAnchor(oldroot_); |
| 1543 | trust_store.AddTrustAnchor(newroot_); |
| 1544 |
| 1545 | // Intermediates and root rollover are all provided synchronously. |
| 1546 | CertIssuerSourceStatic sync_certs; |
| 1547 | sync_certs.AddCert(oldintermediate_); |
| 1548 | sync_certs.AddCert(newintermediate_); |
| 1549 |
| 1550 | for (const auto &expectation : kExpectations) { |
| 1551 | SCOPED_TRACE(expectation.valid_path_limit); |
| 1552 |
| 1553 | CertPathBuilder path_builder( |
| 1554 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1555 | initial_explicit_policy_, user_initial_policy_set_, |
| 1556 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1557 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1558 |
| 1559 | // Stop after finding enough valid paths. |
| 1560 | path_builder.SetValidPathLimit(expectation.valid_path_limit); |
| 1561 |
| 1562 | auto result = path_builder.Run(); |
| 1563 |
| 1564 | EXPECT_TRUE(result.HasValidPath()); |
| 1565 | ASSERT_EQ(expectation.expected_num_paths, result.paths.size()); |
| 1566 |
| 1567 | if (result.paths.size() > 0) { |
| 1568 | // Path builder will first build path: target <- newintermediate <- |
| 1569 | // newroot |
| 1570 | const auto &path0 = *result.paths[0]; |
| 1571 | EXPECT_TRUE(path0.IsValid()); |
| 1572 | ASSERT_EQ(3U, path0.certs.size()); |
| 1573 | EXPECT_EQ(target_, path0.certs[0]); |
| 1574 | EXPECT_EQ(newintermediate_, path0.certs[1]); |
| 1575 | EXPECT_EQ(newroot_, path0.certs[2]); |
| 1576 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1577 | } |
| 1578 |
| 1579 | if (result.paths.size() > 1) { |
| 1580 | // Next path: target <- newintermediate <- oldroot |
| 1581 | const auto &path1 = *result.paths[1]; |
| 1582 | EXPECT_FALSE(path1.IsValid()); |
| 1583 | ASSERT_EQ(3U, path1.certs.size()); |
| 1584 | EXPECT_EQ(target_, path1.certs[0]); |
| 1585 | EXPECT_EQ(newintermediate_, path1.certs[1]); |
| 1586 | EXPECT_EQ(oldroot_, path1.certs[2]); |
| 1587 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1588 | } |
| 1589 |
| 1590 | if (result.paths.size() > 2) { |
| 1591 | // Next path: target <- oldintermediate <- oldroot |
| 1592 | const auto &path2 = *result.paths[2]; |
| 1593 | EXPECT_TRUE(path2.IsValid()); |
| 1594 | ASSERT_EQ(3U, path2.certs.size()); |
| 1595 | EXPECT_EQ(target_, path2.certs[0]); |
| 1596 | EXPECT_EQ(oldintermediate_, path2.certs[1]); |
| 1597 | EXPECT_EQ(oldroot_, path2.certs[2]); |
| 1598 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1599 | } |
| 1600 |
| 1601 | if (result.paths.size() > 3) { |
| 1602 | // Final path: target <- oldintermediate <- newroot |
| 1603 | const auto &path3 = *result.paths[3]; |
| 1604 | EXPECT_FALSE(path3.IsValid()); |
| 1605 | ASSERT_EQ(3U, path3.certs.size()); |
| 1606 | EXPECT_EQ(target_, path3.certs[0]); |
| 1607 | EXPECT_EQ(oldintermediate_, path3.certs[1]); |
| 1608 | EXPECT_EQ(newroot_, path3.certs[2]); |
| 1609 | EXPECT_EQ(3U, result.max_depth_seen); |
| 1610 | } |
| 1611 |
| 1612 | VerifyError error = result.GetBestPathVerifyError(); |
| 1613 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1614 | << error.DiagnosticString(); |
| 1615 | } |
| 1616 | } |
| 1617 |
| 1618 | // If the target cert is a trust anchor, however is not itself *signed* by a |
| 1619 | // trust anchor, then it is not considered valid (the SPKI and name of the |
| 1620 | // trust anchor matches the SPKI and subject of the target certificate, but the |
| 1621 | // rest of the certificate cannot be verified). |
| 1622 | TEST_F(PathBuilderKeyRolloverTest, TestEndEntityIsTrustRoot) { |
| 1623 | // Trust newintermediate. |
| 1624 | TrustStoreInMemory trust_store; |
| 1625 | trust_store.AddTrustAnchor(newintermediate_); |
| 1626 |
| 1627 | // Newintermediate is also the target cert. |
| 1628 | CertPathBuilder path_builder( |
| 1629 | newintermediate_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1630 | initial_explicit_policy_, user_initial_policy_set_, |
| 1631 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1632 |
| 1633 | auto result = path_builder.Run(); |
| 1634 |
| 1635 | EXPECT_FALSE(result.HasValidPath()); |
| 1636 |
| 1637 | VerifyError error = result.GetBestPathVerifyError(); |
| 1638 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 1639 | << error.DiagnosticString(); |
| 1640 | } |
| 1641 |
| 1642 | // If target has same Name+SAN+SPKI as a necessary intermediate, test if a path |
| 1643 | // can still be built. |
| 1644 | // Since LoopChecker will prevent the intermediate from being included, this |
| 1645 | // currently does NOT verify. This case shouldn't occur in the web PKI. |
| 1646 | TEST_F(PathBuilderKeyRolloverTest, |
| 1647 | TestEndEntityHasSameNameAndSpkiAsIntermediate) { |
| 1648 | // Trust oldroot. |
| 1649 | TrustStoreInMemory trust_store; |
| 1650 | trust_store.AddTrustAnchor(oldroot_); |
| 1651 |
| 1652 | // New root rollover is provided synchronously. |
| 1653 | CertIssuerSourceStatic sync_certs; |
| 1654 | sync_certs.AddCert(newrootrollover_); |
| 1655 |
| 1656 | // Newroot is the target cert. |
| 1657 | CertPathBuilder path_builder( |
| 1658 | newroot_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1659 | initial_explicit_policy_, user_initial_policy_set_, |
| 1660 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1661 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1662 |
| 1663 | auto result = path_builder.Run(); |
| 1664 |
| 1665 | // This could actually be OK, but CertPathBuilder does not build the |
| 1666 | // newroot <- newrootrollover <- oldroot path. |
| 1667 | EXPECT_FALSE(result.HasValidPath()); |
| 1668 |
| 1669 | VerifyError error = result.GetBestPathVerifyError(); |
| 1670 | ASSERT_EQ(error.Code(), |
| 1671 | VerifyError::StatusCode::CERTIFICATE_INVALID_SIGNATURE) |
| 1672 | << error.DiagnosticString(); |
| 1673 | } |
| 1674 |
| 1675 | // If target has same Name+SAN+SPKI as the trust root, test that a (trivial) |
| 1676 | // path can still be built. |
| 1677 | TEST_F(PathBuilderKeyRolloverTest, |
| 1678 | TestEndEntityHasSameNameAndSpkiAsTrustAnchor) { |
| 1679 | // Trust newrootrollover. |
| 1680 | TrustStoreInMemory trust_store; |
| 1681 | trust_store.AddTrustAnchor(newrootrollover_); |
| 1682 |
| 1683 | // Newroot is the target cert. |
| 1684 | CertPathBuilder path_builder( |
| 1685 | newroot_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1686 | initial_explicit_policy_, user_initial_policy_set_, |
| 1687 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1688 |
| 1689 | auto result = path_builder.Run(); |
| 1690 |
| 1691 | ASSERT_TRUE(result.HasValidPath()); |
| 1692 |
| 1693 | const CertPathBuilderResultPath *best_result = result.GetBestValidPath(); |
| 1694 |
| 1695 | // Newroot has same name+SPKI as newrootrollover, thus the path is valid and |
| 1696 | // only contains newroot. |
| 1697 | EXPECT_TRUE(best_result->IsValid()); |
| 1698 | ASSERT_EQ(2U, best_result->certs.size()); |
| 1699 | EXPECT_EQ(newroot_, best_result->certs[0]); |
| 1700 | EXPECT_EQ(newrootrollover_, best_result->certs[1]); |
| 1701 |
| 1702 | VerifyError error = result.GetBestPathVerifyError(); |
| 1703 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1704 | << error.DiagnosticString(); |
| 1705 | } |
| 1706 |
| 1707 | // Test that PathBuilder will not try the same path twice if multiple |
| 1708 | // CertIssuerSources provide the same certificate. |
| 1709 | TEST_F(PathBuilderKeyRolloverTest, TestDuplicateIntermediates) { |
| 1710 | // Create a separate copy of oldintermediate. |
| 1711 | std::shared_ptr<const ParsedCertificate> oldintermediate_dupe( |
| 1712 | ParsedCertificate::Create( |
| 1713 | bssl::UniquePtr<CRYPTO_BUFFER>( |
| 1714 | CRYPTO_BUFFER_new(oldintermediate_->der_cert().data(), |
| 1715 | oldintermediate_->der_cert().size(), nullptr)), |
| 1716 | {}, nullptr)); |
| 1717 |
| 1718 | // Only newroot is a trusted root. |
| 1719 | TrustStoreInMemory trust_store; |
| 1720 | trust_store.AddTrustAnchor(newroot_); |
| 1721 |
| 1722 | // The oldintermediate is supplied synchronously by |sync_certs1| and |
| 1723 | // another copy of oldintermediate is supplied synchronously by |sync_certs2|. |
| 1724 | // The path target <- oldintermediate <- newroot should be built first, |
| 1725 | // though it won't verify. It should not be attempted again even though |
| 1726 | // oldintermediate was supplied twice. |
| 1727 | CertIssuerSourceStatic sync_certs1; |
| 1728 | sync_certs1.AddCert(oldintermediate_); |
| 1729 | CertIssuerSourceStatic sync_certs2; |
| 1730 | sync_certs2.AddCert(oldintermediate_dupe); |
| 1731 |
| 1732 | // The newintermediate is supplied asynchronously, so the path |
| 1733 | // target <- newintermediate <- newroot should be tried second. |
| 1734 | AsyncCertIssuerSourceStatic async_certs; |
| 1735 | async_certs.AddCert(newintermediate_); |
| 1736 |
| 1737 | CertPathBuilder path_builder( |
| 1738 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1739 | initial_explicit_policy_, user_initial_policy_set_, |
| 1740 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1741 | path_builder.AddCertIssuerSource(&sync_certs1); |
| 1742 | path_builder.AddCertIssuerSource(&sync_certs2); |
| 1743 | path_builder.AddCertIssuerSource(&async_certs); |
| 1744 |
| 1745 | auto result = path_builder.Run(); |
| 1746 |
| 1747 | EXPECT_TRUE(result.HasValidPath()); |
| 1748 | ASSERT_EQ(2U, result.paths.size()); |
| 1749 |
| 1750 | // Path builder will first attempt: target <- oldintermediate <- newroot |
| 1751 | // but it will fail since oldintermediate is signed by oldroot. |
| 1752 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 1753 | const auto &path0 = *result.paths[0]; |
| 1754 |
| 1755 | ASSERT_EQ(3U, path0.certs.size()); |
| 1756 | EXPECT_EQ(target_, path0.certs[0]); |
| 1757 | // Compare the DER instead of ParsedCertificate pointer, don't care which copy |
| 1758 | // of oldintermediate was used in the path. |
| 1759 | EXPECT_EQ(oldintermediate_->der_cert(), path0.certs[1]->der_cert()); |
| 1760 | EXPECT_EQ(newroot_, path0.certs[2]); |
| 1761 |
| 1762 | // Path builder will next attempt: target <- newintermediate <- newroot |
| 1763 | // which will succeed. |
| 1764 | EXPECT_EQ(1U, result.best_result_index); |
| 1765 | EXPECT_TRUE(result.paths[1]->IsValid()); |
| 1766 | const auto &path1 = *result.paths[1]; |
| 1767 | ASSERT_EQ(3U, path1.certs.size()); |
| 1768 | EXPECT_EQ(target_, path1.certs[0]); |
| 1769 | EXPECT_EQ(newintermediate_, path1.certs[1]); |
| 1770 | EXPECT_EQ(newroot_, path1.certs[2]); |
| 1771 |
| 1772 | VerifyError error = result.GetBestPathVerifyError(); |
| 1773 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1774 | << error.DiagnosticString(); |
| 1775 | } |
| 1776 |
| 1777 | // Test when PathBuilder is given a cert via CertIssuerSources that has the same |
| 1778 | // SPKI as a trust anchor. |
| 1779 | TEST_F(PathBuilderKeyRolloverTest, TestDuplicateIntermediateAndRoot) { |
| 1780 | // Create a separate copy of newroot. |
| 1781 | std::shared_ptr<const ParsedCertificate> newroot_dupe( |
| 1782 | ParsedCertificate::Create( |
| 1783 | bssl::UniquePtr<CRYPTO_BUFFER>( |
| 1784 | CRYPTO_BUFFER_new(newroot_->der_cert().data(), |
| 1785 | newroot_->der_cert().size(), nullptr)), |
| 1786 | {}, nullptr)); |
| 1787 |
| 1788 | // Only newroot is a trusted root. |
| 1789 | TrustStoreInMemory trust_store; |
| 1790 | trust_store.AddTrustAnchor(newroot_); |
| 1791 |
| 1792 | // The oldintermediate and newroot are supplied synchronously by |sync_certs|. |
| 1793 | CertIssuerSourceStatic sync_certs; |
| 1794 | sync_certs.AddCert(oldintermediate_); |
| 1795 | sync_certs.AddCert(newroot_dupe); |
| 1796 |
| 1797 | CertPathBuilder path_builder( |
| 1798 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1799 | initial_explicit_policy_, user_initial_policy_set_, |
| 1800 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1801 | path_builder.AddCertIssuerSource(&sync_certs); |
| 1802 |
| 1803 | auto result = path_builder.Run(); |
| 1804 |
| 1805 | EXPECT_FALSE(result.HasValidPath()); |
| 1806 | ASSERT_EQ(1U, result.paths.size()); |
| 1807 |
| 1808 | // Path builder attempt: target <- oldintermediate <- newroot |
| 1809 | // but it will fail since oldintermediate is signed by oldroot. |
| 1810 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 1811 | const auto &path = *result.paths[0]; |
| 1812 | ASSERT_EQ(3U, path.certs.size()); |
| 1813 | EXPECT_EQ(target_, path.certs[0]); |
| 1814 | EXPECT_EQ(oldintermediate_, path.certs[1]); |
| 1815 | // Compare the DER instead of ParsedCertificate pointer, don't care which copy |
| 1816 | // of newroot was used in the path. |
| 1817 | EXPECT_EQ(newroot_->der_cert(), path.certs[2]->der_cert()); |
| 1818 |
| 1819 | VerifyError error = result.GetBestPathVerifyError(); |
| 1820 | ASSERT_EQ(error.Code(), |
| 1821 | VerifyError::StatusCode::CERTIFICATE_INVALID_SIGNATURE) |
| 1822 | << error.DiagnosticString(); |
| 1823 | } |
| 1824 |
| 1825 | class MockCertIssuerSourceRequest : public CertIssuerSource::Request { |
| 1826 | public: |
| 1827 | MOCK_METHOD1(GetNext, void(ParsedCertificateList *)); |
| 1828 | }; |
| 1829 |
| 1830 | class MockCertIssuerSource : public CertIssuerSource { |
| 1831 | public: |
| 1832 | MOCK_METHOD2(SyncGetIssuersOf, |
| 1833 | void(const ParsedCertificate *, ParsedCertificateList *)); |
| 1834 | MOCK_METHOD2(AsyncGetIssuersOf, |
| 1835 | void(const ParsedCertificate *, std::unique_ptr<Request> *)); |
| 1836 | }; |
| 1837 |
| 1838 | // Helper class to pass the Request to the PathBuilder when it calls |
| 1839 | // AsyncGetIssuersOf. (GoogleMock has a ByMove helper, but it apparently can |
| 1840 | // only be used with Return, not SetArgPointee.) |
| 1841 | class CertIssuerSourceRequestMover { |
| 1842 | public: |
| 1843 | explicit CertIssuerSourceRequestMover( |
| 1844 | std::unique_ptr<CertIssuerSource::Request> req) |
| 1845 | : request_(std::move(req)) {} |
| 1846 | void MoveIt(const ParsedCertificate *cert, |
| 1847 | std::unique_ptr<CertIssuerSource::Request> *out_req) { |
| 1848 | *out_req = std::move(request_); |
| 1849 | } |
| 1850 |
| 1851 | private: |
| 1852 | std::unique_ptr<CertIssuerSource::Request> request_; |
| 1853 | }; |
| 1854 |
| 1855 | // Functor that when called with a ParsedCertificateList* will append the |
| 1856 | // specified certificate. |
| 1857 | class AppendCertToList { |
| 1858 | public: |
| 1859 | explicit AppendCertToList( |
| 1860 | const std::shared_ptr<const ParsedCertificate> &cert) |
| 1861 | : cert_(cert) {} |
| 1862 |
| 1863 | void operator()(ParsedCertificateList *out) { out->push_back(cert_); } |
| 1864 |
| 1865 | private: |
| 1866 | std::shared_ptr<const ParsedCertificate> cert_; |
| 1867 | }; |
| 1868 |
| 1869 | // Test that a single CertIssuerSource returning multiple async batches of |
| 1870 | // issuers is handled correctly. Due to the StrictMocks, it also tests that path |
| 1871 | // builder does not request issuers of certs that it shouldn't. |
| 1872 | TEST_F(PathBuilderKeyRolloverTest, TestMultipleAsyncIssuersFromSingleSource) { |
| 1873 | StrictMock<MockCertIssuerSource> cert_issuer_source; |
| 1874 |
| 1875 | // Only newroot is a trusted root. |
| 1876 | TrustStoreInMemory trust_store; |
| 1877 | trust_store.AddTrustAnchor(newroot_); |
| 1878 |
| 1879 | CertPathBuilder path_builder( |
| 1880 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1881 | initial_explicit_policy_, user_initial_policy_set_, |
| 1882 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1883 | path_builder.AddCertIssuerSource(&cert_issuer_source); |
| 1884 |
| 1885 | // Create the mock CertIssuerSource::Request... |
| 1886 | auto target_issuers_req_owner = |
| 1887 | std::make_unique<StrictMock<MockCertIssuerSourceRequest>>(); |
| 1888 | // Keep a raw pointer to the Request... |
| 1889 | StrictMock<MockCertIssuerSourceRequest> *target_issuers_req = |
| 1890 | target_issuers_req_owner.get(); |
| 1891 | // Setup helper class to pass ownership of the Request to the PathBuilder when |
| 1892 | // it calls AsyncGetIssuersOf. |
| 1893 | CertIssuerSourceRequestMover req_mover(std::move(target_issuers_req_owner)); |
| 1894 | { |
| 1895 | ::testing::InSequence s; |
| 1896 | EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(target_.get(), _)); |
| 1897 | EXPECT_CALL(cert_issuer_source, AsyncGetIssuersOf(target_.get(), _)) |
| 1898 | .WillOnce(Invoke(&req_mover, &CertIssuerSourceRequestMover::MoveIt)); |
| 1899 | } |
| 1900 |
| 1901 | EXPECT_CALL(*target_issuers_req, GetNext(_)) |
| 1902 | // First async batch: return oldintermediate_. |
| 1903 | .WillOnce(Invoke(AppendCertToList(oldintermediate_))) |
| 1904 | // Second async batch: return newintermediate_. |
| 1905 | .WillOnce(Invoke(AppendCertToList(newintermediate_))); |
| 1906 | { |
| 1907 | ::testing::InSequence s; |
| 1908 | // oldintermediate_ does not create a valid path, so both sync and async |
| 1909 | // lookups are expected. |
| 1910 | EXPECT_CALL(cert_issuer_source, |
| 1911 | SyncGetIssuersOf(oldintermediate_.get(), _)); |
| 1912 | EXPECT_CALL(cert_issuer_source, |
| 1913 | AsyncGetIssuersOf(oldintermediate_.get(), _)); |
| 1914 | } |
| 1915 |
| 1916 | // newroot_ is in the trust store, so this path will be completed |
| 1917 | // synchronously. AsyncGetIssuersOf will not be called on newintermediate_. |
| 1918 | EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(newintermediate_.get(), _)); |
| 1919 |
| 1920 | // Ensure pathbuilder finished and filled result. |
| 1921 | auto result = path_builder.Run(); |
| 1922 |
| 1923 | // Note that VerifyAndClearExpectations(target_issuers_req) is not called |
| 1924 | // here. PathBuilder could have destroyed it already, so just let the |
| 1925 | // expectations get checked by the destructor. |
| 1926 | ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source); |
| 1927 |
| 1928 | EXPECT_TRUE(result.HasValidPath()); |
| 1929 | ASSERT_EQ(2U, result.paths.size()); |
| 1930 |
| 1931 | // Path builder first attempts: target <- oldintermediate <- newroot |
| 1932 | // but it will fail since oldintermediate is signed by oldroot. |
| 1933 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 1934 | const auto &path0 = *result.paths[0]; |
| 1935 | ASSERT_EQ(3U, path0.certs.size()); |
| 1936 | EXPECT_EQ(target_, path0.certs[0]); |
| 1937 | EXPECT_EQ(oldintermediate_, path0.certs[1]); |
| 1938 | EXPECT_EQ(newroot_, path0.certs[2]); |
| 1939 |
| 1940 | // After the second batch of async results, path builder will attempt: |
| 1941 | // target <- newintermediate <- newroot which will succeed. |
| 1942 | EXPECT_TRUE(result.paths[1]->IsValid()); |
| 1943 | const auto &path1 = *result.paths[1]; |
| 1944 | ASSERT_EQ(3U, path1.certs.size()); |
| 1945 | EXPECT_EQ(target_, path1.certs[0]); |
| 1946 | EXPECT_EQ(newintermediate_, path1.certs[1]); |
| 1947 | EXPECT_EQ(newroot_, path1.certs[2]); |
| 1948 |
| 1949 | VerifyError error = result.GetBestPathVerifyError(); |
| 1950 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 1951 | << error.DiagnosticString(); |
| 1952 | } |
| 1953 |
| 1954 | // Test that PathBuilder will not try the same path twice if CertIssuerSources |
| 1955 | // asynchronously provide the same certificate multiple times. |
| 1956 | TEST_F(PathBuilderKeyRolloverTest, TestDuplicateAsyncIntermediates) { |
| 1957 | StrictMock<MockCertIssuerSource> cert_issuer_source; |
| 1958 |
| 1959 | // Only newroot is a trusted root. |
| 1960 | TrustStoreInMemory trust_store; |
| 1961 | trust_store.AddTrustAnchor(newroot_); |
| 1962 |
| 1963 | CertPathBuilder path_builder( |
| 1964 | target_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU, |
| 1965 | initial_explicit_policy_, user_initial_policy_set_, |
| 1966 | initial_policy_mapping_inhibit_, initial_any_policy_inhibit_); |
| 1967 | path_builder.AddCertIssuerSource(&cert_issuer_source); |
| 1968 |
| 1969 | // Create the mock CertIssuerSource::Request... |
| 1970 | auto target_issuers_req_owner = |
| 1971 | std::make_unique<StrictMock<MockCertIssuerSourceRequest>>(); |
| 1972 | // Keep a raw pointer to the Request... |
| 1973 | StrictMock<MockCertIssuerSourceRequest> *target_issuers_req = |
| 1974 | target_issuers_req_owner.get(); |
| 1975 | // Setup helper class to pass ownership of the Request to the PathBuilder when |
| 1976 | // it calls AsyncGetIssuersOf. |
| 1977 | CertIssuerSourceRequestMover req_mover(std::move(target_issuers_req_owner)); |
| 1978 | { |
| 1979 | ::testing::InSequence s; |
| 1980 | EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(target_.get(), _)); |
| 1981 | EXPECT_CALL(cert_issuer_source, AsyncGetIssuersOf(target_.get(), _)) |
| 1982 | .WillOnce(Invoke(&req_mover, &CertIssuerSourceRequestMover::MoveIt)); |
| 1983 | } |
| 1984 |
| 1985 | std::shared_ptr<const ParsedCertificate> oldintermediate_dupe( |
| 1986 | ParsedCertificate::Create( |
| 1987 | bssl::UniquePtr<CRYPTO_BUFFER>( |
| 1988 | CRYPTO_BUFFER_new(oldintermediate_->der_cert().data(), |
| 1989 | oldintermediate_->der_cert().size(), nullptr)), |
| 1990 | {}, nullptr)); |
| 1991 |
| 1992 | EXPECT_CALL(*target_issuers_req, GetNext(_)) |
| 1993 | // First async batch: return oldintermediate_. |
| 1994 | .WillOnce(Invoke(AppendCertToList(oldintermediate_))) |
| 1995 | // Second async batch: return a different copy of oldintermediate_ again. |
| 1996 | .WillOnce(Invoke(AppendCertToList(oldintermediate_dupe))) |
| 1997 | // Third async batch: return newintermediate_. |
| 1998 | .WillOnce(Invoke(AppendCertToList(newintermediate_))); |
| 1999 |
| 2000 | { |
| 2001 | ::testing::InSequence s; |
| 2002 | // oldintermediate_ does not create a valid path, so both sync and async |
| 2003 | // lookups are expected. |
| 2004 | EXPECT_CALL(cert_issuer_source, |
| 2005 | SyncGetIssuersOf(oldintermediate_.get(), _)); |
| 2006 | EXPECT_CALL(cert_issuer_source, |
| 2007 | AsyncGetIssuersOf(oldintermediate_.get(), _)); |
| 2008 | } |
| 2009 |
| 2010 | // newroot_ is in the trust store, so this path will be completed |
| 2011 | // synchronously. AsyncGetIssuersOf will not be called on newintermediate_. |
| 2012 | EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(newintermediate_.get(), _)); |
| 2013 |
| 2014 | // Ensure pathbuilder finished and filled result. |
| 2015 | auto result = path_builder.Run(); |
| 2016 |
| 2017 | ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source); |
| 2018 |
| 2019 | EXPECT_TRUE(result.HasValidPath()); |
| 2020 | ASSERT_EQ(2U, result.paths.size()); |
| 2021 |
| 2022 | // Path builder first attempts: target <- oldintermediate <- newroot |
| 2023 | // but it will fail since oldintermediate is signed by oldroot. |
| 2024 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 2025 | const auto &path0 = *result.paths[0]; |
| 2026 | ASSERT_EQ(3U, path0.certs.size()); |
| 2027 | EXPECT_EQ(target_, path0.certs[0]); |
| 2028 | EXPECT_EQ(oldintermediate_, path0.certs[1]); |
| 2029 | EXPECT_EQ(newroot_, path0.certs[2]); |
| 2030 |
| 2031 | // The second async result does not generate any path. |
| 2032 |
| 2033 | // After the third batch of async results, path builder will attempt: |
| 2034 | // target <- newintermediate <- newroot which will succeed. |
| 2035 | EXPECT_TRUE(result.paths[1]->IsValid()); |
| 2036 | const auto &path1 = *result.paths[1]; |
| 2037 | ASSERT_EQ(3U, path1.certs.size()); |
| 2038 | EXPECT_EQ(target_, path1.certs[0]); |
| 2039 | EXPECT_EQ(newintermediate_, path1.certs[1]); |
| 2040 | EXPECT_EQ(newroot_, path1.certs[2]); |
| 2041 |
| 2042 | VerifyError error = result.GetBestPathVerifyError(); |
| 2043 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 2044 | << error.DiagnosticString(); |
| 2045 | } |
| 2046 |
| 2047 | class PathBuilderSimpleChainTest : public ::testing::Test { |
| 2048 | public: |
| 2049 | PathBuilderSimpleChainTest() = default; |
| 2050 |
| 2051 | protected: |
| 2052 | void SetUp() override { |
| 2053 | // Read a simple test chain comprised of a target, intermediate, and root. |
| 2054 | ASSERT_TRUE(ReadVerifyCertChainTestFromFile( |
| 2055 | "testdata/verify_certificate_chain_unittest/target-and-intermediate/" |
| 2056 | "main.test", |
| 2057 | &test_)); |
| 2058 | ASSERT_EQ(3u, test_.chain.size()); |
| 2059 | } |
| 2060 |
| 2061 | // Runs the path builder for the target certificate while |distrusted_cert| is |
| 2062 | // blocked, and |delegate| if non-null. |
| 2063 | CertPathBuilder::Result RunPathBuilder( |
| 2064 | const std::shared_ptr<const ParsedCertificate> &distrusted_cert, |
| 2065 | CertPathBuilderDelegate *optional_delegate) { |
| 2066 | // Set up the trust store such that |distrusted_cert| is blocked, and |
| 2067 | // the root is trusted (except if it was |distrusted_cert|). |
| 2068 | TrustStoreInMemory trust_store; |
| 2069 | if (distrusted_cert != test_.chain.back()) { |
| 2070 | trust_store.AddTrustAnchor(test_.chain.back()); |
| 2071 | } |
| 2072 | if (distrusted_cert) { |
| 2073 | trust_store.AddDistrustedCertificateForTest(distrusted_cert); |
| 2074 | } |
| 2075 |
| 2076 | // Add the single intermediate. |
| 2077 | CertIssuerSourceStatic intermediates; |
| 2078 | intermediates.AddCert(test_.chain[1]); |
| 2079 |
| 2080 | SimplePathBuilderDelegate default_delegate( |
| 2081 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 2082 | CertPathBuilderDelegate *delegate = |
| 2083 | optional_delegate ? optional_delegate : &default_delegate; |
| 2084 |
| 2085 | const InitialExplicitPolicy initial_explicit_policy = |
| 2086 | InitialExplicitPolicy::kFalse; |
| 2087 | const std::set<der::Input> user_initial_policy_set = { |
| 2088 | der::Input(kAnyPolicyOid)}; |
| 2089 | const InitialPolicyMappingInhibit initial_policy_mapping_inhibit = |
| 2090 | InitialPolicyMappingInhibit::kFalse; |
| 2091 | const InitialAnyPolicyInhibit initial_any_policy_inhibit = |
| 2092 | InitialAnyPolicyInhibit::kFalse; |
| 2093 |
| 2094 | CertPathBuilder path_builder( |
| 2095 | test_.chain.front(), &trust_store, delegate, test_.time, |
| 2096 | KeyPurpose::ANY_EKU, initial_explicit_policy, user_initial_policy_set, |
| 2097 | initial_policy_mapping_inhibit, initial_any_policy_inhibit); |
| 2098 | path_builder.AddCertIssuerSource(&intermediates); |
| 2099 | return path_builder.Run(); |
| 2100 | } |
| 2101 |
| 2102 | protected: |
| 2103 | VerifyCertChainTest test_; |
| 2104 | }; |
| 2105 |
| 2106 | // Test fixture for running the path builder over a simple chain, while varying |
| 2107 | // the trustedness of certain certificates. |
| 2108 | class PathBuilderDistrustTest : public PathBuilderSimpleChainTest { |
| 2109 | public: |
| 2110 | PathBuilderDistrustTest() = default; |
| 2111 |
| 2112 | protected: |
| 2113 | // Runs the path builder for the target certificate while |distrusted_cert| is |
| 2114 | // blocked. |
| 2115 | CertPathBuilder::Result RunPathBuilderWithDistrustedCert( |
| 2116 | const std::shared_ptr<const ParsedCertificate> &distrusted_cert) { |
| 2117 | return RunPathBuilder(distrusted_cert, nullptr); |
| 2118 | } |
| 2119 | }; |
| 2120 |
| 2121 | // Tests that path building fails when the target, intermediate, or root are |
| 2122 | // distrusted (but the path is otherwise valid). |
| 2123 | TEST_F(PathBuilderDistrustTest, TargetIntermediateRoot) { |
| 2124 | // First do a control test -- path building without any blocked |
| 2125 | // certificates should work. |
| 2126 | CertPathBuilder::Result result = RunPathBuilderWithDistrustedCert(nullptr); |
| 2127 | { |
| 2128 | ASSERT_TRUE(result.HasValidPath()); |
| 2129 | // The built path should be identical the the one read from disk. |
| 2130 | const auto &path = *result.GetBestValidPath(); |
| 2131 | ASSERT_EQ(test_.chain.size(), path.certs.size()); |
| 2132 | for (size_t i = 0; i < test_.chain.size(); ++i) { |
| 2133 | EXPECT_EQ(test_.chain[i], path.certs[i]); |
| 2134 | } |
| 2135 | } |
| 2136 |
| 2137 | // Try path building when only the target is blocked - should fail. |
| 2138 | result = RunPathBuilderWithDistrustedCert(test_.chain[0]); |
| 2139 | { |
| 2140 | EXPECT_FALSE(result.HasValidPath()); |
| 2141 | ASSERT_LT(result.best_result_index, result.paths.size()); |
| 2142 | const auto &best_path = result.paths[result.best_result_index]; |
| 2143 |
| 2144 | // The built chain has length 1 since path building stopped once |
| 2145 | // it encountered the blocked certificate (target). |
| 2146 | ASSERT_EQ(1u, best_path->certs.size()); |
| 2147 | EXPECT_EQ(best_path->certs[0], test_.chain[0]); |
| 2148 | EXPECT_TRUE(best_path->errors.ContainsHighSeverityErrors()); |
| 2149 | best_path->errors.ContainsError(cert_errors::kDistrustedByTrustStore); |
| 2150 | } |
| 2151 |
| 2152 | // Try path building when only the intermediate is blocked - should fail. |
| 2153 | result = RunPathBuilderWithDistrustedCert(test_.chain[1]); |
| 2154 | { |
| 2155 | EXPECT_FALSE(result.HasValidPath()); |
| 2156 | ASSERT_LT(result.best_result_index, result.paths.size()); |
| 2157 | const auto &best_path = result.paths[result.best_result_index]; |
| 2158 |
| 2159 | // The built chain has length 2 since path building stopped once |
| 2160 | // it encountered the blocked certificate (intermediate). |
| 2161 | ASSERT_EQ(2u, best_path->certs.size()); |
| 2162 | EXPECT_EQ(best_path->certs[0], test_.chain[0]); |
| 2163 | EXPECT_EQ(best_path->certs[1], test_.chain[1]); |
| 2164 | EXPECT_TRUE(best_path->errors.ContainsHighSeverityErrors()); |
| 2165 | best_path->errors.ContainsError(cert_errors::kDistrustedByTrustStore); |
| 2166 | } |
| 2167 |
| 2168 | // Try path building when only the root is blocked - should fail. |
| 2169 | result = RunPathBuilderWithDistrustedCert(test_.chain[2]); |
| 2170 | { |
| 2171 | EXPECT_FALSE(result.HasValidPath()); |
| 2172 | ASSERT_LT(result.best_result_index, result.paths.size()); |
| 2173 | const auto &best_path = result.paths[result.best_result_index]; |
| 2174 |
| 2175 | // The built chain has length 3 since path building stopped once |
| 2176 | // it encountered the blocked certificate (root). |
| 2177 | ASSERT_EQ(3u, best_path->certs.size()); |
| 2178 | EXPECT_EQ(best_path->certs[0], test_.chain[0]); |
| 2179 | EXPECT_EQ(best_path->certs[1], test_.chain[1]); |
| 2180 | EXPECT_EQ(best_path->certs[2], test_.chain[2]); |
| 2181 | EXPECT_TRUE(best_path->errors.ContainsHighSeverityErrors()); |
| 2182 | best_path->errors.ContainsError(cert_errors::kDistrustedByTrustStore); |
| 2183 | } |
| 2184 | } |
| 2185 |
| 2186 | // Test fixture for running the path builder over a simple chain, while varying |
| 2187 | // what CheckPathAfterVerification() does. |
| 2188 | class PathBuilderCheckPathAfterVerificationTest |
| 2189 | : public PathBuilderSimpleChainTest {}; |
| 2190 |
| 2191 | TEST_F(PathBuilderCheckPathAfterVerificationTest, NoOpToValidPath) { |
| 2192 | StrictMock<MockPathBuilderDelegate> delegate; |
| 2193 | // Just verify that the hook is called. |
| 2194 | EXPECT_CALL(delegate, CheckPathAfterVerification(_, _)); |
| 2195 |
| 2196 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2197 | EXPECT_TRUE(result.HasValidPath()); |
| 2198 | } |
| 2199 |
| 2200 | DEFINE_CERT_ERROR_ID(kWarningFromDelegate, "Warning from delegate"); |
| 2201 |
| 2202 | class AddWarningPathBuilderDelegate : public CertPathBuilderDelegateBase { |
| 2203 | public: |
| 2204 | void CheckPathAfterVerification(const CertPathBuilder &path_builder, |
| 2205 | CertPathBuilderResultPath *path) override { |
| 2206 | path->errors.GetErrorsForCert(1)->AddWarning(kWarningFromDelegate, nullptr); |
| 2207 | } |
| 2208 | }; |
| 2209 |
| 2210 | TEST_F(PathBuilderCheckPathAfterVerificationTest, AddsWarningToValidPath) { |
| 2211 | AddWarningPathBuilderDelegate delegate; |
| 2212 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2213 | ASSERT_TRUE(result.HasValidPath()); |
| 2214 |
| 2215 | // A warning should have been added to certificate at index 1 in the path. |
| 2216 | const CertErrors *cert1_errors = |
| 2217 | result.GetBestValidPath()->errors.GetErrorsForCert(1); |
| 2218 | ASSERT_TRUE(cert1_errors); |
| 2219 | EXPECT_TRUE(cert1_errors->ContainsErrorWithSeverity( |
| 2220 | kWarningFromDelegate, CertError::SEVERITY_WARNING)); |
| 2221 |
| 2222 | // The warning should not affect the VerifyError |
| 2223 | VerifyError error = result.GetBestPathVerifyError(); |
| 2224 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 2225 | << error.DiagnosticString(); |
| 2226 | } |
| 2227 |
| 2228 | TEST_F(PathBuilderCheckPathAfterVerificationTest, TestVerifyErrorMapping) { |
| 2229 | struct error_mapping { |
| 2230 | CertErrorId internal_error; |
| 2231 | VerifyError::StatusCode code; |
| 2232 | }; |
| 2233 | struct error_mapping AllErrors[] = { |
| 2234 | {cert_errors::kInternalError, |
| 2235 | VerifyError::StatusCode::VERIFICATION_FAILURE}, |
| 2236 | {cert_errors::kValidityFailedNotAfter, |
| 2237 | VerifyError::StatusCode::CERTIFICATE_EXPIRED}, |
| 2238 | {cert_errors::kValidityFailedNotBefore, |
| 2239 | VerifyError::StatusCode::CERTIFICATE_NOT_YET_VALID}, |
| 2240 | {cert_errors::kDistrustedByTrustStore, |
| 2241 | VerifyError::StatusCode::PATH_NOT_FOUND}, |
| 2242 | {cert_errors::kSignatureAlgorithmMismatch, |
| 2243 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2244 | {cert_errors::kChainIsEmpty, |
| 2245 | VerifyError::StatusCode::VERIFICATION_FAILURE}, |
| 2246 | {cert_errors::kUnconsumedCriticalExtension, |
| 2247 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2248 | {cert_errors::kKeyCertSignBitNotSet, |
| 2249 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2250 | {cert_errors::kMaxPathLengthViolated, |
| 2251 | VerifyError::StatusCode::PATH_NOT_FOUND}, |
| 2252 | {cert_errors::kBasicConstraintsIndicatesNotCa, |
| 2253 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2254 | {cert_errors::kTargetCertShouldNotBeCa, |
| 2255 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2256 | {cert_errors::kMissingBasicConstraints, |
| 2257 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2258 | {cert_errors::kNotPermittedByNameConstraints, |
| 2259 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2260 | {cert_errors::kTooManyNameConstraintChecks, |
| 2261 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2262 | {cert_errors::kSubjectDoesNotMatchIssuer, |
| 2263 | VerifyError::StatusCode::PATH_NOT_FOUND}, |
| 2264 | {cert_errors::kVerifySignedDataFailed, |
| 2265 | VerifyError::StatusCode::CERTIFICATE_INVALID_SIGNATURE}, |
| 2266 | {cert_errors::kSignatureAlgorithmsDifferentEncoding, |
| 2267 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2268 | {cert_errors::kEkuLacksServerAuth, |
| 2269 | VerifyError::StatusCode::CERTIFICATE_NO_MATCHING_EKU}, |
| 2270 | {cert_errors::kEkuLacksServerAuthButHasAnyEKU, |
| 2271 | VerifyError::StatusCode::CERTIFICATE_NO_MATCHING_EKU}, |
| 2272 | {cert_errors::kEkuLacksClientAuth, |
| 2273 | VerifyError::StatusCode::CERTIFICATE_NO_MATCHING_EKU}, |
| 2274 | {cert_errors::kEkuLacksClientAuthButHasAnyEKU, |
| 2275 | VerifyError::StatusCode::CERTIFICATE_NO_MATCHING_EKU}, |
| 2276 | {cert_errors::kEkuLacksClientAuthOrServerAuth, |
| 2277 | VerifyError::StatusCode::CERTIFICATE_NO_MATCHING_EKU}, |
| 2278 | {cert_errors::kEkuHasProhibitedOCSPSigning, |
| 2279 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2280 | {cert_errors::kEkuHasProhibitedTimeStamping, |
| 2281 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2282 | {cert_errors::kEkuHasProhibitedCodeSigning, |
| 2283 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2284 | {cert_errors::kEkuNotPresent, |
| 2285 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2286 | {cert_errors::kCertIsNotTrustAnchor, |
| 2287 | VerifyError::StatusCode::PATH_NOT_FOUND}, |
| 2288 | {cert_errors::kNoValidPolicy, |
| 2289 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2290 | {cert_errors::kPolicyMappingAnyPolicy, |
| 2291 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2292 | {cert_errors::kFailedParsingSpki, |
| 2293 | VerifyError::StatusCode::CERTIFICATE_INVALID}, |
| 2294 | {cert_errors::kUnacceptableSignatureAlgorithm, |
| 2295 | VerifyError::StatusCode::CERTIFICATE_UNSUPPORTED_SIGNATURE_ALGORITHM}, |
| 2296 | {cert_errors::kUnacceptablePublicKey, |
| 2297 | VerifyError::StatusCode::CERTIFICATE_UNSUPPORTED_KEY}, |
| 2298 | {cert_errors::kCertificateRevoked, |
| 2299 | VerifyError::StatusCode::CERTIFICATE_REVOKED}, |
| 2300 | {cert_errors::kNoRevocationMechanism, |
| 2301 | VerifyError::StatusCode::CERTIFICATE_NO_REVOCATION_MECHANISM}, |
| 2302 | {cert_errors::kUnableToCheckRevocation, |
| 2303 | VerifyError::StatusCode::CERTIFICATE_UNABLE_TO_CHECK_REVOCATION}, |
| 2304 | {cert_errors::kNoIssuersFound, VerifyError::StatusCode::PATH_NOT_FOUND}, |
| 2305 | {cert_errors::kDeadlineExceeded, |
| 2306 | VerifyError::StatusCode::PATH_DEADLINE_EXCEEDED}, |
| 2307 | {cert_errors::kIterationLimitExceeded, |
| 2308 | VerifyError::StatusCode::PATH_ITERATION_COUNT_EXCEEDED}, |
| 2309 | {cert_errors::kDepthLimitExceeded, |
| 2310 | VerifyError::StatusCode::PATH_DEPTH_LIMIT_REACHED}, |
| 2311 | }; |
| 2312 |
| 2313 | for (struct error_mapping mapping : AllErrors) { |
| 2314 | AddWarningPathBuilderDelegate delegate; |
| 2315 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2316 | ASSERT_TRUE(result.HasValidPath()); |
| 2317 |
| 2318 | CertErrors *errors = |
| 2319 | (CertErrors *)result.GetBestValidPath()->errors.GetErrorsForCert(1); |
| 2320 | errors->AddError(mapping.internal_error, nullptr); |
| 2321 |
| 2322 | VerifyError error = result.GetBestPathVerifyError(); |
| 2323 | ASSERT_EQ(error.Code(), mapping.code) |
| 2324 | << error.DiagnosticString(); |
| 2325 | } |
| 2326 | } |
| 2327 |
| 2328 | TEST_F(PathBuilderCheckPathAfterVerificationTest, |
| 2329 | TestVerifyErrorMulipleMapping) { |
| 2330 | AddWarningPathBuilderDelegate delegate; |
| 2331 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2332 | ASSERT_TRUE(result.HasValidPath()); |
| 2333 |
| 2334 | CertErrors *errors = |
| 2335 | (CertErrors *)result.GetBestValidPath()->errors.GetErrorsForCert(1); |
| 2336 | errors->AddError(cert_errors::kEkuNotPresent, nullptr); |
| 2337 | errors->AddError(cert_errors::kNoValidPolicy, nullptr); |
| 2338 |
| 2339 | VerifyError error = result.GetBestPathVerifyError(); |
| 2340 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_MULTIPLE_ERRORS) |
| 2341 | << error.DiagnosticString(); |
| 2342 | } |
| 2343 |
| 2344 |
| 2345 | DEFINE_CERT_ERROR_ID(kErrorFromDelegate, "Error from delegate"); |
| 2346 |
| 2347 | class AddErrorPathBuilderDelegate : public CertPathBuilderDelegateBase { |
| 2348 | public: |
| 2349 | void CheckPathAfterVerification(const CertPathBuilder &path_builder, |
| 2350 | CertPathBuilderResultPath *path) override { |
| 2351 | path->errors.GetErrorsForCert(2)->AddError(kErrorFromDelegate, nullptr); |
| 2352 | } |
| 2353 | }; |
| 2354 |
| 2355 | TEST_F(PathBuilderCheckPathAfterVerificationTest, AddsErrorToValidPath) { |
| 2356 | AddErrorPathBuilderDelegate delegate; |
| 2357 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2358 |
| 2359 | // Verification failed. |
| 2360 | ASSERT_FALSE(result.HasValidPath()); |
| 2361 |
| 2362 | ASSERT_LT(result.best_result_index, result.paths.size()); |
| 2363 | const CertPathBuilderResultPath *failed_path = |
| 2364 | result.paths[result.best_result_index].get(); |
| 2365 | ASSERT_TRUE(failed_path); |
| 2366 |
| 2367 | // An error should have been added to certificate at index 2 in the path. |
| 2368 | const CertErrors *cert2_errors = failed_path->errors.GetErrorsForCert(2); |
| 2369 | ASSERT_TRUE(cert2_errors); |
| 2370 | EXPECT_TRUE(cert2_errors->ContainsError(kErrorFromDelegate)); |
| 2371 |
| 2372 | // The newly defined delegate error should map to CERTIFICATE_INVALID |
| 2373 | // since it is associated with a certificate (at index 2) |
| 2374 | VerifyError error = result.GetBestPathVerifyError(); |
| 2375 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::CERTIFICATE_INVALID) |
| 2376 | << error.DiagnosticString(); |
| 2377 | } |
| 2378 |
| 2379 | class AddOtherErrorPathBuilderDelegate : public CertPathBuilderDelegateBase { |
| 2380 | public: |
| 2381 | void CheckPathAfterVerification(const CertPathBuilder &path_builder, |
| 2382 | CertPathBuilderResultPath *path) override { |
| 2383 | path->errors.GetOtherErrors()->AddError(kErrorFromDelegate, nullptr); |
| 2384 | } |
| 2385 | }; |
| 2386 |
| 2387 | TEST_F(PathBuilderCheckPathAfterVerificationTest, AddsErrorToOtherErrors) { |
| 2388 | AddOtherErrorPathBuilderDelegate delegate; |
| 2389 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2390 |
| 2391 | // Verification failed. |
| 2392 | ASSERT_FALSE(result.HasValidPath()); |
| 2393 |
| 2394 | ASSERT_LT(result.best_result_index, result.paths.size()); |
| 2395 | const CertPathBuilderResultPath *failed_path = |
| 2396 | result.paths[result.best_result_index].get(); |
| 2397 | ASSERT_TRUE(failed_path); |
| 2398 |
| 2399 | // An error should have been added to other errors |
| 2400 | const CertErrors *other_errors = failed_path->errors.GetOtherErrors(); |
| 2401 | ASSERT_TRUE(other_errors); |
| 2402 | EXPECT_TRUE(other_errors->ContainsError(kErrorFromDelegate)); |
| 2403 |
| 2404 | // The newly defined delegate error should map to VERIFICATION_FAILURE |
| 2405 | // since the error is not associated to a certificate. |
| 2406 | VerifyError error = result.GetBestPathVerifyError(); |
| 2407 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::VERIFICATION_FAILURE) |
| 2408 | << error.DiagnosticString(); |
| 2409 | } |
| 2410 |
| 2411 |
| 2412 | TEST_F(PathBuilderCheckPathAfterVerificationTest, NoopToAlreadyInvalidPath) { |
| 2413 | StrictMock<MockPathBuilderDelegate> delegate; |
| 2414 | // Just verify that the hook is called (on an invalid path). |
| 2415 | EXPECT_CALL(delegate, CheckPathAfterVerification(_, _)); |
| 2416 |
| 2417 | // Run the pathbuilder with certificate at index 1 actively distrusted. |
| 2418 | CertPathBuilder::Result result = RunPathBuilder(test_.chain[1], &delegate); |
| 2419 | EXPECT_FALSE(result.HasValidPath()); |
| 2420 |
| 2421 | VerifyError error = result.GetBestPathVerifyError(); |
| 2422 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 2423 | << error.DiagnosticString(); |
| 2424 | } |
| 2425 |
| 2426 | struct DelegateData : public CertPathBuilderDelegateData { |
| 2427 | int value = 0xB33F; |
| 2428 | }; |
| 2429 |
| 2430 | class SetsDelegateDataPathBuilderDelegate : public CertPathBuilderDelegateBase { |
| 2431 | public: |
| 2432 | void CheckPathAfterVerification(const CertPathBuilder &path_builder, |
| 2433 | CertPathBuilderResultPath *path) override { |
| 2434 | path->delegate_data = std::make_unique<DelegateData>(); |
| 2435 | } |
| 2436 | }; |
| 2437 |
| 2438 | TEST_F(PathBuilderCheckPathAfterVerificationTest, SetsDelegateData) { |
| 2439 | SetsDelegateDataPathBuilderDelegate delegate; |
| 2440 | CertPathBuilder::Result result = RunPathBuilder(nullptr, &delegate); |
| 2441 | ASSERT_TRUE(result.HasValidPath()); |
| 2442 |
| 2443 | DelegateData *data = reinterpret_cast<DelegateData *>( |
| 2444 | result.GetBestValidPath()->delegate_data.get()); |
| 2445 |
| 2446 | EXPECT_EQ(0xB33F, data->value); |
| 2447 | } |
| 2448 |
| 2449 | TEST(PathBuilderPrioritizationTest, DatePrioritization) { |
| 2450 | std::string test_dir = |
| 2451 | "testdata/path_builder_unittest/validity_date_prioritization/"; |
| 2452 | std::shared_ptr<const ParsedCertificate> root = |
| 2453 | ReadCertFromFile(test_dir + "root.pem"); |
| 2454 | ASSERT_TRUE(root); |
| 2455 | std::shared_ptr<const ParsedCertificate> int_ac = |
| 2456 | ReadCertFromFile(test_dir + "int_ac.pem"); |
| 2457 | ASSERT_TRUE(int_ac); |
| 2458 | std::shared_ptr<const ParsedCertificate> int_ad = |
| 2459 | ReadCertFromFile(test_dir + "int_ad.pem"); |
| 2460 | ASSERT_TRUE(int_ad); |
| 2461 | std::shared_ptr<const ParsedCertificate> int_bc = |
| 2462 | ReadCertFromFile(test_dir + "int_bc.pem"); |
| 2463 | ASSERT_TRUE(int_bc); |
| 2464 | std::shared_ptr<const ParsedCertificate> int_bd = |
| 2465 | ReadCertFromFile(test_dir + "int_bd.pem"); |
| 2466 | ASSERT_TRUE(int_bd); |
| 2467 | std::shared_ptr<const ParsedCertificate> target = |
| 2468 | ReadCertFromFile(test_dir + "target.pem"); |
| 2469 | ASSERT_TRUE(target); |
| 2470 |
| 2471 | SimplePathBuilderDelegate delegate( |
| 2472 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 2473 | der::GeneralizedTime verify_time = {2017, 3, 1, 0, 0, 0}; |
| 2474 |
| 2475 | // Distrust the root certificate. This will force the path builder to attempt |
| 2476 | // all possible paths. |
| 2477 | TrustStoreInMemory trust_store; |
| 2478 | trust_store.AddDistrustedCertificateForTest(root); |
| 2479 |
| 2480 | for (bool reverse_input_order : {false, true}) { |
| 2481 | SCOPED_TRACE(reverse_input_order); |
| 2482 |
| 2483 | CertIssuerSourceStatic intermediates; |
| 2484 | // Test with the intermediates supplied in two different orders to ensure |
| 2485 | // the results don't depend on input ordering. |
| 2486 | if (reverse_input_order) { |
| 2487 | intermediates.AddCert(int_bd); |
| 2488 | intermediates.AddCert(int_bc); |
| 2489 | intermediates.AddCert(int_ad); |
| 2490 | intermediates.AddCert(int_ac); |
| 2491 | } else { |
| 2492 | intermediates.AddCert(int_ac); |
| 2493 | intermediates.AddCert(int_ad); |
| 2494 | intermediates.AddCert(int_bc); |
| 2495 | intermediates.AddCert(int_bd); |
| 2496 | } |
| 2497 |
| 2498 | CertPathBuilder path_builder( |
| 2499 | target, &trust_store, &delegate, verify_time, KeyPurpose::ANY_EKU, |
| 2500 | InitialExplicitPolicy::kFalse, {der::Input(kAnyPolicyOid)}, |
| 2501 | InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse); |
| 2502 | path_builder.AddCertIssuerSource(&intermediates); |
| 2503 |
| 2504 | CertPathBuilder::Result result = path_builder.Run(); |
| 2505 | EXPECT_FALSE(result.HasValidPath()); |
| 2506 |
| 2507 | VerifyError error = result.GetBestPathVerifyError(); |
| 2508 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 2509 | << error.DiagnosticString(); |
| 2510 |
| 2511 | ASSERT_EQ(4U, result.paths.size()); |
| 2512 |
| 2513 | // Path builder should have attempted paths using the intermediates in |
| 2514 | // order: bd, bc, ad, ac |
| 2515 |
| 2516 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 2517 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 2518 | EXPECT_EQ(target, result.paths[0]->certs[0]); |
| 2519 | EXPECT_EQ(int_bd, result.paths[0]->certs[1]); |
| 2520 | EXPECT_EQ(root, result.paths[0]->certs[2]); |
| 2521 |
| 2522 | EXPECT_FALSE(result.paths[1]->IsValid()); |
| 2523 | ASSERT_EQ(3U, result.paths[1]->certs.size()); |
| 2524 | EXPECT_EQ(target, result.paths[1]->certs[0]); |
| 2525 | EXPECT_EQ(int_bc, result.paths[1]->certs[1]); |
| 2526 | EXPECT_EQ(root, result.paths[1]->certs[2]); |
| 2527 |
| 2528 | EXPECT_FALSE(result.paths[2]->IsValid()); |
| 2529 | ASSERT_EQ(3U, result.paths[2]->certs.size()); |
| 2530 | EXPECT_EQ(target, result.paths[2]->certs[0]); |
| 2531 | EXPECT_EQ(int_ad, result.paths[2]->certs[1]); |
| 2532 | EXPECT_EQ(root, result.paths[2]->certs[2]); |
| 2533 |
| 2534 | EXPECT_FALSE(result.paths[3]->IsValid()); |
| 2535 | ASSERT_EQ(3U, result.paths[3]->certs.size()); |
| 2536 | EXPECT_EQ(target, result.paths[3]->certs[0]); |
| 2537 | EXPECT_EQ(int_ac, result.paths[3]->certs[1]); |
| 2538 | EXPECT_EQ(root, result.paths[3]->certs[2]); |
| 2539 | } |
| 2540 | } |
| 2541 |
| 2542 | TEST(PathBuilderPrioritizationTest, KeyIdPrioritization) { |
| 2543 | std::string test_dir = |
| 2544 | "testdata/path_builder_unittest/key_id_prioritization/"; |
| 2545 | std::shared_ptr<const ParsedCertificate> root = |
| 2546 | ReadCertFromFile(test_dir + "root.pem"); |
| 2547 | ASSERT_TRUE(root); |
| 2548 | std::shared_ptr<const ParsedCertificate> int_matching_ski_a = |
| 2549 | ReadCertFromFile(test_dir + "int_matching_ski_a.pem"); |
| 2550 | ASSERT_TRUE(int_matching_ski_a); |
| 2551 | std::shared_ptr<const ParsedCertificate> int_matching_ski_b = |
| 2552 | ReadCertFromFile(test_dir + "int_matching_ski_b.pem"); |
| 2553 | ASSERT_TRUE(int_matching_ski_b); |
| 2554 | std::shared_ptr<const ParsedCertificate> int_no_ski_a = |
| 2555 | ReadCertFromFile(test_dir + "int_no_ski_a.pem"); |
| 2556 | ASSERT_TRUE(int_no_ski_a); |
| 2557 | std::shared_ptr<const ParsedCertificate> int_no_ski_b = |
| 2558 | ReadCertFromFile(test_dir + "int_no_ski_b.pem"); |
| 2559 | ASSERT_TRUE(int_no_ski_b); |
| 2560 | std::shared_ptr<const ParsedCertificate> int_different_ski_a = |
| 2561 | ReadCertFromFile(test_dir + "int_different_ski_a.pem"); |
| 2562 | ASSERT_TRUE(int_different_ski_a); |
| 2563 | std::shared_ptr<const ParsedCertificate> int_different_ski_b = |
| 2564 | ReadCertFromFile(test_dir + "int_different_ski_b.pem"); |
| 2565 | ASSERT_TRUE(int_different_ski_b); |
| 2566 | std::shared_ptr<const ParsedCertificate> target = |
| 2567 | ReadCertFromFile(test_dir + "target.pem"); |
| 2568 | ASSERT_TRUE(target); |
| 2569 |
| 2570 | SimplePathBuilderDelegate delegate( |
| 2571 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 2572 | der::GeneralizedTime verify_time = {2017, 3, 1, 0, 0, 0}; |
| 2573 |
| 2574 | // Distrust the root certificate. This will force the path builder to attempt |
| 2575 | // all possible paths. |
| 2576 | TrustStoreInMemory trust_store; |
| 2577 | trust_store.AddDistrustedCertificateForTest(root); |
| 2578 |
| 2579 | for (bool reverse_input_order : {false, true}) { |
| 2580 | SCOPED_TRACE(reverse_input_order); |
| 2581 |
| 2582 | CertIssuerSourceStatic intermediates; |
| 2583 | // Test with the intermediates supplied in two different orders to ensure |
| 2584 | // the results don't depend on input ordering. |
| 2585 | if (reverse_input_order) { |
| 2586 | intermediates.AddCert(int_different_ski_b); |
| 2587 | intermediates.AddCert(int_different_ski_a); |
| 2588 | intermediates.AddCert(int_no_ski_b); |
| 2589 | intermediates.AddCert(int_no_ski_a); |
| 2590 | intermediates.AddCert(int_matching_ski_b); |
| 2591 | intermediates.AddCert(int_matching_ski_a); |
| 2592 | } else { |
| 2593 | intermediates.AddCert(int_matching_ski_a); |
| 2594 | intermediates.AddCert(int_matching_ski_b); |
| 2595 | intermediates.AddCert(int_no_ski_a); |
| 2596 | intermediates.AddCert(int_no_ski_b); |
| 2597 | intermediates.AddCert(int_different_ski_a); |
| 2598 | intermediates.AddCert(int_different_ski_b); |
| 2599 | } |
| 2600 |
| 2601 | CertPathBuilder path_builder( |
| 2602 | target, &trust_store, &delegate, verify_time, KeyPurpose::ANY_EKU, |
| 2603 | InitialExplicitPolicy::kFalse, {der::Input(kAnyPolicyOid)}, |
| 2604 | InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse); |
| 2605 | path_builder.AddCertIssuerSource(&intermediates); |
| 2606 |
| 2607 | CertPathBuilder::Result result = path_builder.Run(); |
| 2608 | EXPECT_FALSE(result.HasValidPath()); |
| 2609 | ASSERT_EQ(6U, result.paths.size()); |
| 2610 |
| 2611 | // Path builder should have attempted paths using the intermediates in |
| 2612 | // order: matching_ski_b, matching_ski_a, no_ski_b, no_ski_a, |
| 2613 | // different_ski_b, different_ski_a |
| 2614 |
| 2615 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 2616 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 2617 | EXPECT_EQ(target, result.paths[0]->certs[0]); |
| 2618 | EXPECT_EQ(int_matching_ski_b, result.paths[0]->certs[1]); |
| 2619 | EXPECT_EQ(root, result.paths[0]->certs[2]); |
| 2620 |
| 2621 | EXPECT_FALSE(result.paths[1]->IsValid()); |
| 2622 | ASSERT_EQ(3U, result.paths[1]->certs.size()); |
| 2623 | EXPECT_EQ(target, result.paths[1]->certs[0]); |
| 2624 | EXPECT_EQ(int_matching_ski_a, result.paths[1]->certs[1]); |
| 2625 | EXPECT_EQ(root, result.paths[1]->certs[2]); |
| 2626 |
| 2627 | EXPECT_FALSE(result.paths[2]->IsValid()); |
| 2628 | ASSERT_EQ(3U, result.paths[2]->certs.size()); |
| 2629 | EXPECT_EQ(target, result.paths[2]->certs[0]); |
| 2630 | EXPECT_EQ(int_no_ski_b, result.paths[2]->certs[1]); |
| 2631 | EXPECT_EQ(root, result.paths[2]->certs[2]); |
| 2632 |
| 2633 | EXPECT_FALSE(result.paths[3]->IsValid()); |
| 2634 | ASSERT_EQ(3U, result.paths[3]->certs.size()); |
| 2635 | EXPECT_EQ(target, result.paths[3]->certs[0]); |
| 2636 | EXPECT_EQ(int_no_ski_a, result.paths[3]->certs[1]); |
| 2637 | EXPECT_EQ(root, result.paths[3]->certs[2]); |
| 2638 |
| 2639 | EXPECT_FALSE(result.paths[4]->IsValid()); |
| 2640 | ASSERT_EQ(3U, result.paths[4]->certs.size()); |
| 2641 | EXPECT_EQ(target, result.paths[4]->certs[0]); |
| 2642 | EXPECT_EQ(int_different_ski_b, result.paths[4]->certs[1]); |
| 2643 | EXPECT_EQ(root, result.paths[4]->certs[2]); |
| 2644 |
| 2645 | EXPECT_FALSE(result.paths[5]->IsValid()); |
| 2646 | ASSERT_EQ(3U, result.paths[5]->certs.size()); |
| 2647 | EXPECT_EQ(target, result.paths[5]->certs[0]); |
| 2648 | EXPECT_EQ(int_different_ski_a, result.paths[5]->certs[1]); |
| 2649 | EXPECT_EQ(root, result.paths[5]->certs[2]); |
| 2650 | } |
| 2651 | } |
| 2652 |
| 2653 | TEST(PathBuilderPrioritizationTest, TrustAndKeyIdPrioritization) { |
| 2654 | std::string test_dir = |
| 2655 | "testdata/path_builder_unittest/key_id_prioritization/"; |
| 2656 | std::shared_ptr<const ParsedCertificate> root = |
| 2657 | ReadCertFromFile(test_dir + "root.pem"); |
| 2658 | ASSERT_TRUE(root); |
| 2659 | std::shared_ptr<const ParsedCertificate> trusted_and_matching = |
| 2660 | ReadCertFromFile(test_dir + "int_matching_ski_a.pem"); |
| 2661 | ASSERT_TRUE(trusted_and_matching); |
| 2662 | std::shared_ptr<const ParsedCertificate> matching = |
| 2663 | ReadCertFromFile(test_dir + "int_matching_ski_b.pem"); |
| 2664 | ASSERT_TRUE(matching); |
| 2665 | std::shared_ptr<const ParsedCertificate> distrusted_and_matching = |
| 2666 | ReadCertFromFile(test_dir + "int_matching_ski_c.pem"); |
| 2667 | ASSERT_TRUE(distrusted_and_matching); |
| 2668 | std::shared_ptr<const ParsedCertificate> trusted_and_no_match_data = |
| 2669 | ReadCertFromFile(test_dir + "int_no_ski_a.pem"); |
| 2670 | ASSERT_TRUE(trusted_and_no_match_data); |
| 2671 | std::shared_ptr<const ParsedCertificate> no_match_data = |
| 2672 | ReadCertFromFile(test_dir + "int_no_ski_b.pem"); |
| 2673 | ASSERT_TRUE(no_match_data); |
| 2674 | std::shared_ptr<const ParsedCertificate> distrusted_and_no_match_data = |
| 2675 | ReadCertFromFile(test_dir + "int_no_ski_c.pem"); |
| 2676 | ASSERT_TRUE(distrusted_and_no_match_data); |
| 2677 | std::shared_ptr<const ParsedCertificate> trusted_and_mismatch = |
| 2678 | ReadCertFromFile(test_dir + "int_different_ski_a.pem"); |
| 2679 | ASSERT_TRUE(trusted_and_mismatch); |
| 2680 | std::shared_ptr<const ParsedCertificate> mismatch = |
| 2681 | ReadCertFromFile(test_dir + "int_different_ski_b.pem"); |
| 2682 | ASSERT_TRUE(mismatch); |
| 2683 | std::shared_ptr<const ParsedCertificate> distrusted_and_mismatch = |
| 2684 | ReadCertFromFile(test_dir + "int_different_ski_c.pem"); |
| 2685 | ASSERT_TRUE(distrusted_and_mismatch); |
| 2686 | std::shared_ptr<const ParsedCertificate> target = |
| 2687 | ReadCertFromFile(test_dir + "target.pem"); |
| 2688 | ASSERT_TRUE(target); |
| 2689 |
| 2690 | SimplePathBuilderDelegate delegate( |
| 2691 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 2692 | der::GeneralizedTime verify_time = {2017, 3, 1, 0, 0, 0}; |
| 2693 |
| 2694 | for (bool reverse_input_order : {false, true}) { |
| 2695 | SCOPED_TRACE(reverse_input_order); |
| 2696 |
| 2697 | TrustStoreInMemory trust_store; |
| 2698 | // Test with the intermediates supplied in two different orders to ensure |
| 2699 | // the results don't depend on input ordering. |
| 2700 | if (reverse_input_order) { |
| 2701 | trust_store.AddTrustAnchor(trusted_and_matching); |
| 2702 | trust_store.AddCertificateWithUnspecifiedTrust(matching); |
| 2703 | trust_store.AddDistrustedCertificateForTest(distrusted_and_matching); |
| 2704 | trust_store.AddTrustAnchor(trusted_and_no_match_data); |
| 2705 | trust_store.AddCertificateWithUnspecifiedTrust(no_match_data); |
| 2706 | trust_store.AddDistrustedCertificateForTest(distrusted_and_no_match_data); |
| 2707 | trust_store.AddTrustAnchor(trusted_and_mismatch); |
| 2708 | trust_store.AddCertificateWithUnspecifiedTrust(mismatch); |
| 2709 | trust_store.AddDistrustedCertificateForTest(distrusted_and_mismatch); |
| 2710 | } else { |
| 2711 | trust_store.AddDistrustedCertificateForTest(distrusted_and_matching); |
| 2712 | trust_store.AddCertificateWithUnspecifiedTrust(no_match_data); |
| 2713 | trust_store.AddTrustAnchor(trusted_and_no_match_data); |
| 2714 | trust_store.AddTrustAnchor(trusted_and_matching); |
| 2715 | trust_store.AddCertificateWithUnspecifiedTrust(matching); |
| 2716 | trust_store.AddCertificateWithUnspecifiedTrust(mismatch); |
| 2717 | trust_store.AddDistrustedCertificateForTest(distrusted_and_no_match_data); |
| 2718 | trust_store.AddTrustAnchor(trusted_and_mismatch); |
| 2719 | trust_store.AddDistrustedCertificateForTest(distrusted_and_mismatch); |
| 2720 | } |
| 2721 | // Also distrust the root certificate. This will force the path builder to |
| 2722 | // report paths that included an unspecified trust intermediate. |
| 2723 | trust_store.AddDistrustedCertificateForTest(root); |
| 2724 |
| 2725 | CertPathBuilder path_builder( |
| 2726 | target, &trust_store, &delegate, verify_time, KeyPurpose::ANY_EKU, |
| 2727 | InitialExplicitPolicy::kFalse, {der::Input(kAnyPolicyOid)}, |
| 2728 | InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse); |
| 2729 | path_builder.SetValidPathLimit(0); |
| 2730 |
| 2731 | CertPathBuilder::Result result = path_builder.Run(); |
| 2732 | EXPECT_TRUE(result.HasValidPath()); |
| 2733 | ASSERT_EQ(9U, result.paths.size()); |
| 2734 |
| 2735 | // Path builder should have attempted paths using the intermediates in |
| 2736 | // order: trusted_and_matching, trusted_and_no_match_data, matching, |
| 2737 | // no_match_data, trusted_and_mismatch, mismatch, distrusted_and_matching, |
| 2738 | // distrusted_and_no_match_data, distrusted_and_mismatch. |
| 2739 |
| 2740 | EXPECT_TRUE(result.paths[0]->IsValid()); |
| 2741 | ASSERT_EQ(2U, result.paths[0]->certs.size()); |
| 2742 | EXPECT_EQ(target, result.paths[0]->certs[0]); |
| 2743 | EXPECT_EQ(trusted_and_matching, result.paths[0]->certs[1]); |
| 2744 |
| 2745 | EXPECT_TRUE(result.paths[1]->IsValid()); |
| 2746 | ASSERT_EQ(2U, result.paths[1]->certs.size()); |
| 2747 | EXPECT_EQ(target, result.paths[1]->certs[0]); |
| 2748 | EXPECT_EQ(trusted_and_no_match_data, result.paths[1]->certs[1]); |
| 2749 |
| 2750 | EXPECT_FALSE(result.paths[2]->IsValid()); |
| 2751 | ASSERT_EQ(3U, result.paths[2]->certs.size()); |
| 2752 | EXPECT_EQ(target, result.paths[2]->certs[0]); |
| 2753 | EXPECT_EQ(matching, result.paths[2]->certs[1]); |
| 2754 | EXPECT_EQ(root, result.paths[2]->certs[2]); |
| 2755 |
| 2756 | EXPECT_FALSE(result.paths[3]->IsValid()); |
| 2757 | ASSERT_EQ(3U, result.paths[3]->certs.size()); |
| 2758 | EXPECT_EQ(target, result.paths[3]->certs[0]); |
| 2759 | EXPECT_EQ(no_match_data, result.paths[3]->certs[1]); |
| 2760 | EXPECT_EQ(root, result.paths[3]->certs[2]); |
| 2761 |
| 2762 | // Although this intermediate is trusted, it has the wrong key, so |
| 2763 | // the path should not be valid. |
| 2764 | EXPECT_FALSE(result.paths[4]->IsValid()); |
| 2765 | ASSERT_EQ(2U, result.paths[4]->certs.size()); |
| 2766 | EXPECT_EQ(target, result.paths[4]->certs[0]); |
| 2767 | EXPECT_EQ(trusted_and_mismatch, result.paths[4]->certs[1]); |
| 2768 |
| 2769 | EXPECT_FALSE(result.paths[5]->IsValid()); |
| 2770 | ASSERT_EQ(3U, result.paths[5]->certs.size()); |
| 2771 | EXPECT_EQ(target, result.paths[5]->certs[0]); |
| 2772 | EXPECT_EQ(mismatch, result.paths[5]->certs[1]); |
| 2773 | EXPECT_EQ(root, result.paths[5]->certs[2]); |
| 2774 |
| 2775 | EXPECT_FALSE(result.paths[6]->IsValid()); |
| 2776 | ASSERT_EQ(2U, result.paths[6]->certs.size()); |
| 2777 | EXPECT_EQ(target, result.paths[6]->certs[0]); |
| 2778 | EXPECT_EQ(distrusted_and_matching, result.paths[6]->certs[1]); |
| 2779 |
| 2780 | EXPECT_FALSE(result.paths[7]->IsValid()); |
| 2781 | ASSERT_EQ(2U, result.paths[7]->certs.size()); |
| 2782 | EXPECT_EQ(target, result.paths[7]->certs[0]); |
| 2783 | EXPECT_EQ(distrusted_and_no_match_data, result.paths[7]->certs[1]); |
| 2784 |
| 2785 | EXPECT_FALSE(result.paths[8]->IsValid()); |
| 2786 | ASSERT_EQ(2U, result.paths[8]->certs.size()); |
| 2787 | EXPECT_EQ(target, result.paths[8]->certs[0]); |
| 2788 | EXPECT_EQ(distrusted_and_mismatch, result.paths[8]->certs[1]); |
| 2789 | } |
| 2790 | } |
| 2791 |
| 2792 | // PathBuilder does not support prioritization based on the issuer name & |
| 2793 | // serial in authorityKeyIdentifier, so this test just ensures that it does not |
| 2794 | // affect prioritization order and that it is generally just ignored |
| 2795 | // completely. |
| 2796 | TEST(PathBuilderPrioritizationTest, KeyIdNameAndSerialPrioritization) { |
| 2797 | std::string test_dir = |
| 2798 | "testdata/path_builder_unittest/key_id_name_and_serial_prioritization/"; |
| 2799 | std::shared_ptr<const ParsedCertificate> root = |
| 2800 | ReadCertFromFile(test_dir + "root.pem"); |
| 2801 | ASSERT_TRUE(root); |
| 2802 | std::shared_ptr<const ParsedCertificate> root2 = |
| 2803 | ReadCertFromFile(test_dir + "root2.pem"); |
| 2804 | ASSERT_TRUE(root2); |
| 2805 | std::shared_ptr<const ParsedCertificate> int_matching = |
| 2806 | ReadCertFromFile(test_dir + "int_matching.pem"); |
| 2807 | ASSERT_TRUE(int_matching); |
| 2808 | std::shared_ptr<const ParsedCertificate> int_match_name_only = |
| 2809 | ReadCertFromFile(test_dir + "int_match_name_only.pem"); |
| 2810 | ASSERT_TRUE(int_match_name_only); |
| 2811 | std::shared_ptr<const ParsedCertificate> int_mismatch = |
| 2812 | ReadCertFromFile(test_dir + "int_mismatch.pem"); |
| 2813 | ASSERT_TRUE(int_mismatch); |
| 2814 | std::shared_ptr<const ParsedCertificate> target = |
| 2815 | ReadCertFromFile(test_dir + "target.pem"); |
| 2816 | ASSERT_TRUE(target); |
| 2817 |
| 2818 | SimplePathBuilderDelegate delegate( |
| 2819 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 2820 | der::GeneralizedTime verify_time = {2017, 3, 1, 0, 0, 0}; |
| 2821 |
| 2822 | // Distrust the root certificates. This will force the path builder to attempt |
| 2823 | // all possible paths. |
| 2824 | TrustStoreInMemory trust_store; |
| 2825 | trust_store.AddDistrustedCertificateForTest(root); |
| 2826 | trust_store.AddDistrustedCertificateForTest(root2); |
| 2827 |
| 2828 | for (bool reverse_input_order : {false, true}) { |
| 2829 | SCOPED_TRACE(reverse_input_order); |
| 2830 |
| 2831 | CertIssuerSourceStatic intermediates; |
| 2832 | // Test with the intermediates supplied in two different orders to ensure |
| 2833 | // the results don't depend on input ordering. |
| 2834 | if (reverse_input_order) { |
| 2835 | intermediates.AddCert(int_mismatch); |
| 2836 | intermediates.AddCert(int_match_name_only); |
| 2837 | intermediates.AddCert(int_matching); |
| 2838 | } else { |
| 2839 | intermediates.AddCert(int_matching); |
| 2840 | intermediates.AddCert(int_match_name_only); |
| 2841 | intermediates.AddCert(int_mismatch); |
| 2842 | } |
| 2843 |
| 2844 | CertPathBuilder path_builder( |
| 2845 | target, &trust_store, &delegate, verify_time, KeyPurpose::ANY_EKU, |
| 2846 | InitialExplicitPolicy::kFalse, {der::Input(kAnyPolicyOid)}, |
| 2847 | InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse); |
| 2848 | path_builder.AddCertIssuerSource(&intermediates); |
| 2849 |
| 2850 | CertPathBuilder::Result result = path_builder.Run(); |
| 2851 | EXPECT_FALSE(result.HasValidPath()); |
| 2852 | ASSERT_EQ(3U, result.paths.size()); |
| 2853 | VerifyError error = result.GetBestPathVerifyError(); |
| 2854 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND) |
| 2855 | << error.DiagnosticString(); |
| 2856 |
| 2857 | // The serial & issuer method is not used in prioritization, so the certs |
| 2858 | // should have been prioritized based on dates. The test certs have the |
| 2859 | // date priority order in the reverse of what authorityKeyIdentifier |
| 2860 | // prioritization would have done if it were supported. |
| 2861 | // Path builder should have attempted paths using the intermediates in |
| 2862 | // order: mismatch, match_name_only, matching |
| 2863 |
| 2864 | EXPECT_FALSE(result.paths[0]->IsValid()); |
| 2865 | ASSERT_EQ(3U, result.paths[0]->certs.size()); |
| 2866 | EXPECT_EQ(target, result.paths[0]->certs[0]); |
| 2867 | EXPECT_EQ(int_mismatch, result.paths[0]->certs[1]); |
| 2868 | EXPECT_EQ(root2, result.paths[0]->certs[2]); |
| 2869 |
| 2870 | EXPECT_FALSE(result.paths[1]->IsValid()); |
| 2871 | ASSERT_EQ(3U, result.paths[1]->certs.size()); |
| 2872 | EXPECT_EQ(target, result.paths[1]->certs[0]); |
| 2873 | EXPECT_EQ(int_match_name_only, result.paths[1]->certs[1]); |
| 2874 | EXPECT_EQ(root, result.paths[1]->certs[2]); |
| 2875 |
| 2876 | EXPECT_FALSE(result.paths[2]->IsValid()); |
| 2877 | ASSERT_EQ(3U, result.paths[2]->certs.size()); |
| 2878 | EXPECT_EQ(target, result.paths[2]->certs[0]); |
| 2879 | EXPECT_EQ(int_matching, result.paths[2]->certs[1]); |
| 2880 | EXPECT_EQ(root, result.paths[2]->certs[2]); |
| 2881 | } |
| 2882 | } |
| 2883 |
| 2884 | TEST(PathBuilderPrioritizationTest, SelfIssuedPrioritization) { |
| 2885 | std::string test_dir = |
| 2886 | "testdata/path_builder_unittest/self_issued_prioritization/"; |
| 2887 | std::shared_ptr<const ParsedCertificate> root1 = |
| 2888 | ReadCertFromFile(test_dir + "root1.pem"); |
| 2889 | ASSERT_TRUE(root1); |
| 2890 | std::shared_ptr<const ParsedCertificate> root1_cross = |
| 2891 | ReadCertFromFile(test_dir + "root1_cross.pem"); |
| 2892 | ASSERT_TRUE(root1_cross); |
| 2893 | std::shared_ptr<const ParsedCertificate> target = |
| 2894 | ReadCertFromFile(test_dir + "target.pem"); |
| 2895 | ASSERT_TRUE(target); |
| 2896 |
| 2897 | SimplePathBuilderDelegate delegate( |
| 2898 | 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
| 2899 | der::GeneralizedTime verify_time = {2017, 3, 1, 0, 0, 0}; |
| 2900 |
| 2901 | TrustStoreInMemory trust_store; |
| 2902 | trust_store.AddTrustAnchor(root1); |
| 2903 | trust_store.AddTrustAnchor(root1_cross); |
| 2904 | CertPathBuilder path_builder( |
| 2905 | target, &trust_store, &delegate, verify_time, KeyPurpose::ANY_EKU, |
| 2906 | InitialExplicitPolicy::kFalse, {der::Input(kAnyPolicyOid)}, |
| 2907 | InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse); |
| 2908 | path_builder.SetValidPathLimit(0); |
| 2909 |
| 2910 | CertPathBuilder::Result result = path_builder.Run(); |
| 2911 | EXPECT_TRUE(result.HasValidPath()); |
| 2912 | VerifyError error = result.GetBestPathVerifyError(); |
| 2913 | ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED) |
| 2914 | << error.DiagnosticString(); |
| 2915 |
| 2916 | // Path builder should have built paths to both trusted roots. |
| 2917 | ASSERT_EQ(2U, result.paths.size()); |
| 2918 |
| 2919 | // |root1| should have been preferred because it is self-issued, even though |
| 2920 | // the notBefore date is older than |root1_cross|. |
| 2921 | EXPECT_TRUE(result.paths[0]->IsValid()); |
| 2922 | ASSERT_EQ(2U, result.paths[0]->certs.size()); |
| 2923 | EXPECT_EQ(target, result.paths[0]->certs[0]); |
| 2924 | EXPECT_EQ(root1, result.paths[0]->certs[1]); |
| 2925 |
| 2926 | EXPECT_TRUE(result.paths[1]->IsValid()); |
| 2927 | ASSERT_EQ(2U, result.paths[1]->certs.size()); |
| 2928 | EXPECT_EQ(target, result.paths[1]->certs[0]); |
| 2929 | EXPECT_EQ(root1_cross, result.paths[1]->certs[1]); |
| 2930 | } |
| 2931 |
| 2932 | } // namespace |
| 2933 |
| 2934 | BSSL_NAMESPACE_END |
| 2935 |