Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: net/cert/internal/path_builder_unittest.cc

Issue 2225493003: Don't treat trust anchors as certificates during path building. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address moar feedback Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/cert/internal/path_builder.h" 5 #include "net/cert/internal/path_builder.h"
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/cancelable_callback.h" 8 #include "base/cancelable_callback.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 216 }
217 217
218 protected: 218 protected:
219 scoped_refptr<ParsedCertificate> a_by_b_, b_by_c_, b_by_f_, c_by_d_, c_by_e_, 219 scoped_refptr<ParsedCertificate> a_by_b_, b_by_c_, b_by_f_, c_by_d_, c_by_e_,
220 d_by_d_, e_by_e_, f_by_e_; 220 d_by_d_, e_by_e_, f_by_e_;
221 221
222 SimpleSignaturePolicy signature_policy_; 222 SimpleSignaturePolicy signature_policy_;
223 der::GeneralizedTime time_ = {2016, 4, 11, 0, 0, 0}; 223 der::GeneralizedTime time_ = {2016, 4, 11, 0, 0, 0};
224 }; 224 };
225 225
226 // If the target cert is a trust anchor, it should verify and should not include 226 void AddTrustedCertificate(scoped_refptr<ParsedCertificate> cert,
227 // anything else in the path. 227 TrustStore* trust_store) {
228 TEST_F(PathBuilderMultiRootTest, TargetIsTrustAnchor) { 228 ASSERT_TRUE(cert.get());
229 scoped_refptr<TrustAnchor> anchor =
230 TrustAnchor::CreateFromCertificateNoConstraints(std::move(cert));
231 ASSERT_TRUE(anchor.get());
232 trust_store->AddTrustAnchor(std::move(anchor));
233 }
234
235 // If the target cert is has the same name and key as a trust anchor, however
236 // is signed but a different trust anchor. This should successfully build a
237 // path, however the trust anchor will be the signer of this cert.
238 //
239 // (This test is very similar to TestEndEntityHasSameNameAndSpkiAsTrustAnchor
240 // but with different data; also in this test the target cert itself is in the
241 // trust store).
242 TEST_F(PathBuilderMultiRootTest, TargetHasNameAndSpkiOfTrustAnchor) {
229 TrustStore trust_store; 243 TrustStore trust_store;
230 trust_store.AddTrustedCertificate(a_by_b_); 244 AddTrustedCertificate(a_by_b_, &trust_store);
231 trust_store.AddTrustedCertificate(b_by_f_); 245 AddTrustedCertificate(b_by_f_, &trust_store);
232 246
233 CertPathBuilder::Result result; 247 CertPathBuilder::Result result;
234 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_, 248 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
235 &result); 249 &result);
236 250
237 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 251 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
238 252
239 EXPECT_EQ(OK, result.error()); 253 EXPECT_EQ(OK, result.error());
240 EXPECT_EQ(1U, result.paths[result.best_result_index]->path.size()); 254 const auto& path = result.paths[result.best_result_index]->path;
241 EXPECT_EQ(a_by_b_, result.paths[result.best_result_index]->path[0]); 255 EXPECT_EQ(1U, path.certs.size());
256 EXPECT_EQ(a_by_b_, path.certs[0]);
257 EXPECT_EQ(b_by_f_, path.trust_anchor->cert());
258 }
259
260 // If the target cert is has the same name and key as a trust anchor, however
261 // is NOT itself signed by a trust anchor, it fails. Although the provided SPKI
262 // is trusted, the certificate contents cannot be verified.
263 TEST_F(PathBuilderMultiRootTest, TargetWithSameNameAsTrustAnchorFails) {
264 TrustStore trust_store;
265 AddTrustedCertificate(a_by_b_, &trust_store);
266
267 CertPathBuilder::Result result;
268 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
269 &result);
270
271 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
272
273 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error());
274 }
275
276 // Test a failed path building when the trust anchor is provided as a
277 // supplemental certificate. Conceptually the following paths can be built:
278 //
279 // B(C) <- C(D) <- [Trust anchor D]
280 // B(C) <- C(D) <- D(D) <- [Trust anchor D]
281 //
282 // The second one is extraneous given the shorter one, however path building
283 // will enumerate it if the shorter one failed validation.
284 TEST_F(PathBuilderMultiRootTest, SelfSignedTrustAnchorSupplementalCert) {
285 TrustStore trust_store;
286 AddTrustedCertificate(d_by_d_, &trust_store);
287
288 // The (extraneous) trust anchor D(D) is supplied as a certificate, as is the
289 // intermediate needed for path building C(D).
290 CertIssuerSourceStatic sync_certs;
291 sync_certs.AddCert(d_by_d_);
292 sync_certs.AddCert(c_by_d_);
293
294 // C(D) is not valid at this time, so path building will fail.
295 der::GeneralizedTime expired_time = {2016, 1, 1, 0, 0, 0};
296
297 CertPathBuilder::Result result;
298 CertPathBuilder path_builder(b_by_c_, &trust_store, &signature_policy_,
299 expired_time, &result);
300 path_builder.AddCertIssuerSource(&sync_certs);
301
302 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
303
304 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error());
305 ASSERT_EQ(2U, result.paths.size());
306
307 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
308 const auto& path0 = result.paths[0]->path;
309 ASSERT_EQ(2U, path0.certs.size());
310 EXPECT_EQ(b_by_c_, path0.certs[0]);
311 EXPECT_EQ(c_by_d_, path0.certs[1]);
312 EXPECT_EQ(d_by_d_, path0.trust_anchor->cert());
313
314 const auto& path1 = result.paths[1]->path;
315 ASSERT_EQ(3U, path1.certs.size());
316 EXPECT_EQ(b_by_c_, path1.certs[0]);
317 EXPECT_EQ(c_by_d_, path1.certs[1]);
318 EXPECT_EQ(d_by_d_, path1.certs[2]);
319 EXPECT_EQ(d_by_d_, path1.trust_anchor->cert());
320 }
321
322 // If the target cert is a self-signed cert whose key is a trust anchor, it
323 // should verify.
324 TEST_F(PathBuilderMultiRootTest, TargetIsSelfSignedTrustAnchor) {
325 TrustStore trust_store;
326 AddTrustedCertificate(e_by_e_, &trust_store);
327 // This is not necessary for the test, just an extra...
328 AddTrustedCertificate(f_by_e_, &trust_store);
329
330 CertPathBuilder::Result result;
331 CertPathBuilder path_builder(e_by_e_, &trust_store, &signature_policy_, time_,
332 &result);
333
334 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
335
336 EXPECT_EQ(OK, result.error());
337 const auto& path = result.paths[result.best_result_index]->path;
338 EXPECT_EQ(1U, path.certs.size());
339 EXPECT_EQ(e_by_e_, path.certs[0]);
340 EXPECT_EQ(e_by_e_, path.trust_anchor->cert());
242 } 341 }
243 342
244 // If the target cert is directly issued by a trust anchor, it should verify 343 // If the target cert is directly issued by a trust anchor, it should verify
245 // without any intermediate certs being provided. 344 // without any intermediate certs being provided.
246 TEST_F(PathBuilderMultiRootTest, TargetDirectlySignedByTrustAnchor) { 345 TEST_F(PathBuilderMultiRootTest, TargetDirectlySignedByTrustAnchor) {
247 TrustStore trust_store; 346 TrustStore trust_store;
248 trust_store.AddTrustedCertificate(b_by_f_); 347 AddTrustedCertificate(b_by_f_, &trust_store);
249 348
250 CertPathBuilder::Result result; 349 CertPathBuilder::Result result;
251 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_, 350 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
252 &result); 351 &result);
253 352
254 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 353 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
255 354
256 EXPECT_EQ(OK, result.error()); 355 ASSERT_EQ(OK, result.error());
257 EXPECT_EQ(2U, result.paths[result.best_result_index]->path.size()); 356 const auto& path = result.paths[result.best_result_index]->path;
258 EXPECT_EQ(a_by_b_, result.paths[result.best_result_index]->path[0]); 357 EXPECT_EQ(1U, path.certs.size());
259 EXPECT_EQ(b_by_f_, result.paths[result.best_result_index]->path[1]); 358 EXPECT_EQ(a_by_b_, path.certs[0]);
359 EXPECT_EQ(b_by_f_, path.trust_anchor->cert());
260 } 360 }
261 361
262 // Test that async cert queries are not made if the path can be successfully 362 // Test that async cert queries are not made if the path can be successfully
263 // built with synchronously available certs. 363 // built with synchronously available certs.
264 TEST_F(PathBuilderMultiRootTest, TriesSyncFirst) { 364 TEST_F(PathBuilderMultiRootTest, TriesSyncFirst) {
265 TrustStore trust_store; 365 TrustStore trust_store;
266 trust_store.AddTrustedCertificate(e_by_e_); 366 AddTrustedCertificate(e_by_e_, &trust_store);
267 367
268 CertIssuerSourceStatic sync_certs; 368 CertIssuerSourceStatic sync_certs;
269 sync_certs.AddCert(b_by_f_); 369 sync_certs.AddCert(b_by_f_);
270 sync_certs.AddCert(f_by_e_); 370 sync_certs.AddCert(f_by_e_);
271 371
272 AsyncCertIssuerSourceStatic async_certs; 372 AsyncCertIssuerSourceStatic async_certs;
273 async_certs.AddCert(b_by_c_); 373 async_certs.AddCert(b_by_c_);
274 async_certs.AddCert(c_by_e_); 374 async_certs.AddCert(c_by_e_);
275 375
276 CertPathBuilder::Result result; 376 CertPathBuilder::Result result;
277 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_, 377 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
278 &result); 378 &result);
279 path_builder.AddCertIssuerSource(&async_certs); 379 path_builder.AddCertIssuerSource(&async_certs);
280 path_builder.AddCertIssuerSource(&sync_certs); 380 path_builder.AddCertIssuerSource(&sync_certs);
281 381
282 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 382 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
283 383
284 EXPECT_EQ(OK, result.error()); 384 EXPECT_EQ(OK, result.error());
285 EXPECT_EQ(0, async_certs.num_async_gets()); 385 EXPECT_EQ(0, async_certs.num_async_gets());
286 } 386 }
287 387
288 // Test that async cert queries are not made if no callback is provided. 388 // Test that async cert queries are not made if no callback is provided.
289 TEST_F(PathBuilderMultiRootTest, SychronousOnlyMode) { 389 TEST_F(PathBuilderMultiRootTest, SychronousOnlyMode) {
290 TrustStore trust_store; 390 TrustStore trust_store;
291 trust_store.AddTrustedCertificate(e_by_e_); 391 AddTrustedCertificate(e_by_e_, &trust_store);
292 392
293 CertIssuerSourceStatic sync_certs; 393 CertIssuerSourceStatic sync_certs;
294 sync_certs.AddCert(f_by_e_); 394 sync_certs.AddCert(f_by_e_);
295 395
296 AsyncCertIssuerSourceStatic async_certs; 396 AsyncCertIssuerSourceStatic async_certs;
297 async_certs.AddCert(b_by_f_); 397 async_certs.AddCert(b_by_f_);
298 398
299 CertPathBuilder::Result result; 399 CertPathBuilder::Result result;
300 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_, 400 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
301 &result); 401 &result);
302 path_builder.AddCertIssuerSource(&async_certs); 402 path_builder.AddCertIssuerSource(&async_certs);
303 path_builder.AddCertIssuerSource(&sync_certs); 403 path_builder.AddCertIssuerSource(&sync_certs);
304 404
305 EXPECT_EQ(CompletionStatus::SYNC, path_builder.Run(base::Closure())); 405 EXPECT_EQ(CompletionStatus::SYNC, path_builder.Run(base::Closure()));
306 406
307 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error()); 407 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error());
308 EXPECT_EQ(0, async_certs.num_async_gets()); 408 EXPECT_EQ(0, async_certs.num_async_gets());
309 } 409 }
310 410
311 // If async queries are needed, all async sources will be queried 411 // If async queries are needed, all async sources will be queried
312 // simultaneously. 412 // simultaneously.
313 TEST_F(PathBuilderMultiRootTest, TestAsyncSimultaneous) { 413 TEST_F(PathBuilderMultiRootTest, TestAsyncSimultaneous) {
314 TrustStore trust_store; 414 TrustStore trust_store;
315 trust_store.AddTrustedCertificate(e_by_e_); 415 AddTrustedCertificate(e_by_e_, &trust_store);
316 416
317 CertIssuerSourceStatic sync_certs; 417 CertIssuerSourceStatic sync_certs;
318 sync_certs.AddCert(b_by_c_); 418 sync_certs.AddCert(b_by_c_);
319 sync_certs.AddCert(b_by_f_); 419 sync_certs.AddCert(b_by_f_);
320 420
321 AsyncCertIssuerSourceStatic async_certs1; 421 AsyncCertIssuerSourceStatic async_certs1;
322 async_certs1.AddCert(c_by_e_); 422 async_certs1.AddCert(c_by_e_);
323 423
324 AsyncCertIssuerSourceStatic async_certs2; 424 AsyncCertIssuerSourceStatic async_certs2;
325 async_certs2.AddCert(f_by_e_); 425 async_certs2.AddCert(f_by_e_);
(...skipping 10 matching lines...) Expand all
336 EXPECT_EQ(OK, result.error()); 436 EXPECT_EQ(OK, result.error());
337 EXPECT_EQ(1, async_certs1.num_async_gets()); 437 EXPECT_EQ(1, async_certs1.num_async_gets());
338 EXPECT_EQ(1, async_certs2.num_async_gets()); 438 EXPECT_EQ(1, async_certs2.num_async_gets());
339 } 439 }
340 440
341 // Test that PathBuilder does not generate longer paths than necessary if one of 441 // Test that PathBuilder does not generate longer paths than necessary if one of
342 // the supplied certs is itself a trust anchor. 442 // the supplied certs is itself a trust anchor.
343 TEST_F(PathBuilderMultiRootTest, TestLongChain) { 443 TEST_F(PathBuilderMultiRootTest, TestLongChain) {
344 // Both D(D) and C(D) are trusted roots. 444 // Both D(D) and C(D) are trusted roots.
345 TrustStore trust_store; 445 TrustStore trust_store;
346 trust_store.AddTrustedCertificate(d_by_d_); 446 AddTrustedCertificate(d_by_d_, &trust_store);
347 trust_store.AddTrustedCertificate(c_by_d_); 447 AddTrustedCertificate(c_by_d_, &trust_store);
348 448
349 // Certs B(C), and C(D) are all supplied. 449 // Certs B(C), and C(D) are all supplied.
350 CertIssuerSourceStatic sync_certs; 450 CertIssuerSourceStatic sync_certs;
351 sync_certs.AddCert(b_by_c_); 451 sync_certs.AddCert(b_by_c_);
352 sync_certs.AddCert(c_by_d_); 452 sync_certs.AddCert(c_by_d_);
353 453
354 CertPathBuilder::Result result; 454 CertPathBuilder::Result result;
355 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_, 455 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
356 &result); 456 &result);
357 path_builder.AddCertIssuerSource(&sync_certs); 457 path_builder.AddCertIssuerSource(&sync_certs);
358 458
359 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 459 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
360 460
361 EXPECT_EQ(OK, result.error()); 461 EXPECT_EQ(OK, result.error());
362 462
363 // The result path should be A(B) <- B(C) <- C(D) 463 // The result path should be A(B) <- B(C) <- C(D)
364 // not the longer but also valid A(B) <- B(C) <- C(D) <- D(D) 464 // not the longer but also valid A(B) <- B(C) <- C(D) <- D(D)
365 EXPECT_EQ(3U, result.paths[result.best_result_index]->path.size()); 465 const auto& path = result.paths[result.best_result_index]->path;
466 EXPECT_EQ(2U, path.certs.size());
366 } 467 }
367 468
368 // Test that PathBuilder will backtrack and try a different path if the first 469 // Test that PathBuilder will backtrack and try a different path if the first
369 // one doesn't work out. 470 // one doesn't work out.
370 TEST_F(PathBuilderMultiRootTest, TestBacktracking) { 471 TEST_F(PathBuilderMultiRootTest, TestBacktracking) {
371 // Only D(D) is a trusted root. 472 // Only D(D) is a trusted root.
372 TrustStore trust_store; 473 TrustStore trust_store;
373 trust_store.AddTrustedCertificate(d_by_d_); 474 AddTrustedCertificate(d_by_d_, &trust_store);
374 475
375 // Certs B(F) and F(E) are supplied synchronously, thus the path 476 // Certs B(F) and F(E) are supplied synchronously, thus the path
376 // A(B) <- B(F) <- F(E) should be built first, though it won't verify. 477 // A(B) <- B(F) <- F(E) should be built first, though it won't verify.
377 CertIssuerSourceStatic sync_certs; 478 CertIssuerSourceStatic sync_certs;
378 sync_certs.AddCert(b_by_f_); 479 sync_certs.AddCert(b_by_f_);
379 sync_certs.AddCert(f_by_e_); 480 sync_certs.AddCert(f_by_e_);
380 481
381 // Certs B(C), and C(D) are supplied asynchronously, so the path 482 // Certs B(C), and C(D) are supplied asynchronously, so the path
382 // A(B) <- B(C) <- C(D) <- D(D) should be tried second. 483 // A(B) <- B(C) <- C(D) <- D(D) should be tried second.
383 AsyncCertIssuerSourceStatic async_certs; 484 AsyncCertIssuerSourceStatic async_certs;
384 async_certs.AddCert(b_by_c_); 485 async_certs.AddCert(b_by_c_);
385 async_certs.AddCert(c_by_d_); 486 async_certs.AddCert(c_by_d_);
386 487
387 CertPathBuilder::Result result; 488 CertPathBuilder::Result result;
388 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_, 489 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, time_,
389 &result); 490 &result);
390 path_builder.AddCertIssuerSource(&sync_certs); 491 path_builder.AddCertIssuerSource(&sync_certs);
391 path_builder.AddCertIssuerSource(&async_certs); 492 path_builder.AddCertIssuerSource(&async_certs);
392 493
393 EXPECT_EQ(CompletionStatus::ASYNC, RunPathBuilder(&path_builder)); 494 EXPECT_EQ(CompletionStatus::ASYNC, RunPathBuilder(&path_builder));
394 495
395 EXPECT_EQ(OK, result.error()); 496 EXPECT_EQ(OK, result.error());
396 497
397 // The result path should be A(B) <- B(C) <- C(D) <- D(D) 498 // The result path should be A(B) <- B(C) <- C(D) <- D(D)
398 ASSERT_EQ(4U, result.paths[result.best_result_index]->path.size()); 499 const auto& path = result.paths[result.best_result_index]->path;
399 EXPECT_EQ(a_by_b_, result.paths[result.best_result_index]->path[0]); 500 ASSERT_EQ(3U, path.certs.size());
400 EXPECT_EQ(b_by_c_, result.paths[result.best_result_index]->path[1]); 501 EXPECT_EQ(a_by_b_, path.certs[0]);
401 EXPECT_EQ(c_by_d_, result.paths[result.best_result_index]->path[2]); 502 EXPECT_EQ(b_by_c_, path.certs[1]);
402 EXPECT_EQ(d_by_d_, result.paths[result.best_result_index]->path[3]); 503 EXPECT_EQ(c_by_d_, path.certs[2]);
504 EXPECT_EQ(d_by_d_, path.trust_anchor->cert());
403 } 505 }
404 506
405 // Test that whichever order CertIssuerSource returns the issuers, the path 507 // Test that whichever order CertIssuerSource returns the issuers, the path
406 // building still succeeds. 508 // building still succeeds.
407 TEST_F(PathBuilderMultiRootTest, TestCertIssuerOrdering) { 509 TEST_F(PathBuilderMultiRootTest, TestCertIssuerOrdering) {
408 // Only D(D) is a trusted root. 510 // Only D(D) is a trusted root.
409 TrustStore trust_store; 511 TrustStore trust_store;
410 trust_store.AddTrustedCertificate(d_by_d_); 512 AddTrustedCertificate(d_by_d_, &trust_store);
411 513
412 for (bool reverse_order : {false, true}) { 514 for (bool reverse_order : {false, true}) {
413 SCOPED_TRACE(reverse_order); 515 SCOPED_TRACE(reverse_order);
414 std::vector<scoped_refptr<ParsedCertificate>> certs = { 516 std::vector<scoped_refptr<ParsedCertificate>> certs = {
415 b_by_c_, b_by_f_, f_by_e_, c_by_d_, c_by_e_}; 517 b_by_c_, b_by_f_, f_by_e_, c_by_d_, c_by_e_};
416 CertIssuerSourceStatic sync_certs; 518 CertIssuerSourceStatic sync_certs;
417 if (reverse_order) { 519 if (reverse_order) {
418 for (auto it = certs.rbegin(); it != certs.rend(); ++it) 520 for (auto it = certs.rbegin(); it != certs.rend(); ++it)
419 sync_certs.AddCert(*it); 521 sync_certs.AddCert(*it);
420 } else { 522 } else {
421 for (const auto& cert : certs) 523 for (const auto& cert : certs)
422 sync_certs.AddCert(cert); 524 sync_certs.AddCert(cert);
423 } 525 }
424 526
425 CertPathBuilder::Result result; 527 CertPathBuilder::Result result;
426 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_, 528 CertPathBuilder path_builder(a_by_b_, &trust_store, &signature_policy_,
427 time_, &result); 529 time_, &result);
428 path_builder.AddCertIssuerSource(&sync_certs); 530 path_builder.AddCertIssuerSource(&sync_certs);
429 531
430 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 532 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
431 533
432 EXPECT_EQ(OK, result.error()); 534 EXPECT_EQ(OK, result.error());
433 535
434 // The result path should be A(B) <- B(C) <- C(D) <- D(D) 536 // The result path should be A(B) <- B(C) <- C(D) <- D(D)
435 ASSERT_EQ(4U, result.paths[result.best_result_index]->path.size()); 537 const auto& path = result.paths[result.best_result_index]->path;
436 EXPECT_EQ(a_by_b_, result.paths[result.best_result_index]->path[0]); 538 ASSERT_EQ(3U, path.certs.size());
437 EXPECT_EQ(b_by_c_, result.paths[result.best_result_index]->path[1]); 539 EXPECT_EQ(a_by_b_, path.certs[0]);
438 EXPECT_EQ(c_by_d_, result.paths[result.best_result_index]->path[2]); 540 EXPECT_EQ(b_by_c_, path.certs[1]);
439 EXPECT_EQ(d_by_d_, result.paths[result.best_result_index]->path[3]); 541 EXPECT_EQ(c_by_d_, path.certs[2]);
542 EXPECT_EQ(d_by_d_, path.trust_anchor->cert());
440 } 543 }
441 } 544 }
442 545
443 class PathBuilderKeyRolloverTest : public ::testing::Test { 546 class PathBuilderKeyRolloverTest : public ::testing::Test {
444 public: 547 public:
445 PathBuilderKeyRolloverTest() : signature_policy_(1024) {} 548 PathBuilderKeyRolloverTest() : signature_policy_(1024) {}
446 549
447 void SetUp() override { 550 void SetUp() override {
448 std::vector<std::string> path; 551 std::vector<std::string> path;
449 552
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 594
492 SimpleSignaturePolicy signature_policy_; 595 SimpleSignaturePolicy signature_policy_;
493 der::GeneralizedTime time_; 596 der::GeneralizedTime time_;
494 }; 597 };
495 598
496 // Tests that if only the old root cert is trusted, the path builder can build a 599 // Tests that if only the old root cert is trusted, the path builder can build a
497 // path through the new intermediate and rollover cert to the old root. 600 // path through the new intermediate and rollover cert to the old root.
498 TEST_F(PathBuilderKeyRolloverTest, TestRolloverOnlyOldRootTrusted) { 601 TEST_F(PathBuilderKeyRolloverTest, TestRolloverOnlyOldRootTrusted) {
499 // Only oldroot is trusted. 602 // Only oldroot is trusted.
500 TrustStore trust_store; 603 TrustStore trust_store;
501 trust_store.AddTrustedCertificate(oldroot_); 604 AddTrustedCertificate(oldroot_, &trust_store);
502 605
503 // Old intermediate cert is not provided, so the pathbuilder will need to go 606 // Old intermediate cert is not provided, so the pathbuilder will need to go
504 // through the rollover cert. 607 // through the rollover cert.
505 CertIssuerSourceStatic sync_certs; 608 CertIssuerSourceStatic sync_certs;
506 sync_certs.AddCert(newintermediate_); 609 sync_certs.AddCert(newintermediate_);
507 sync_certs.AddCert(newrootrollover_); 610 sync_certs.AddCert(newrootrollover_);
508 611
509 CertPathBuilder::Result result; 612 CertPathBuilder::Result result;
510 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 613 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
511 &result); 614 &result);
512 path_builder.AddCertIssuerSource(&sync_certs); 615 path_builder.AddCertIssuerSource(&sync_certs);
513 616
514 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 617 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
515 618
516 EXPECT_EQ(OK, result.error()); 619 EXPECT_EQ(OK, result.error());
517 620
518 // Path builder will first attempt: target <- newintermediate <- oldroot 621 // Path builder will first attempt: target <- newintermediate <- oldroot
519 // but it will fail since newintermediate is signed by newroot. 622 // but it will fail since newintermediate is signed by newroot.
520 ASSERT_EQ(2U, result.paths.size()); 623 ASSERT_EQ(2U, result.paths.size());
624 const auto& path0 = result.paths[0]->path;
521 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 625 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
522 ASSERT_EQ(3U, result.paths[0]->path.size()); 626 ASSERT_EQ(2U, path0.certs.size());
523 EXPECT_EQ(target_, result.paths[0]->path[0]); 627 EXPECT_EQ(target_, path0.certs[0]);
524 EXPECT_EQ(newintermediate_, result.paths[0]->path[1]); 628 EXPECT_EQ(newintermediate_, path0.certs[1]);
525 EXPECT_EQ(oldroot_, result.paths[0]->path[2]); 629 EXPECT_EQ(oldroot_, path0.trust_anchor->cert());
526 630
527 // Path builder will next attempt: 631 // Path builder will next attempt:
528 // target <- newintermediate <- newrootrollover <- oldroot 632 // target <- newintermediate <- newrootrollover <- oldroot
529 // which will succeed. 633 // which will succeed.
634 const auto& path1 = result.paths[1]->path;
530 EXPECT_EQ(1U, result.best_result_index); 635 EXPECT_EQ(1U, result.best_result_index);
531 EXPECT_EQ(OK, result.paths[1]->error); 636 EXPECT_EQ(OK, result.paths[1]->error);
532 ASSERT_EQ(4U, result.paths[1]->path.size()); 637 ASSERT_EQ(3U, path1.certs.size());
533 EXPECT_EQ(target_, result.paths[1]->path[0]); 638 EXPECT_EQ(target_, path1.certs[0]);
534 EXPECT_EQ(newintermediate_, result.paths[1]->path[1]); 639 EXPECT_EQ(newintermediate_, path1.certs[1]);
535 EXPECT_EQ(newrootrollover_, result.paths[1]->path[2]); 640 EXPECT_EQ(newrootrollover_, path1.certs[2]);
536 EXPECT_EQ(oldroot_, result.paths[1]->path[3]); 641 EXPECT_EQ(oldroot_, path1.trust_anchor->cert());
537 } 642 }
538 643
539 // Tests that if both old and new roots are trusted it can build a path through 644 // Tests that if both old and new roots are trusted it can build a path through
540 // either. 645 // either.
541 // TODO(mattm): Once prioritization is implemented, it should test that it 646 // TODO(mattm): Once prioritization is implemented, it should test that it
542 // always builds the path through the new intermediate and new root. 647 // always builds the path through the new intermediate and new root.
543 TEST_F(PathBuilderKeyRolloverTest, TestRolloverBothRootsTrusted) { 648 TEST_F(PathBuilderKeyRolloverTest, TestRolloverBothRootsTrusted) {
544 // Both oldroot and newroot are trusted. 649 // Both oldroot and newroot are trusted.
545 TrustStore trust_store; 650 TrustStore trust_store;
546 trust_store.AddTrustedCertificate(oldroot_); 651 AddTrustedCertificate(oldroot_, &trust_store);
547 trust_store.AddTrustedCertificate(newroot_); 652 AddTrustedCertificate(newroot_, &trust_store);
548 653
549 // Both old and new intermediates + rollover cert are provided. 654 // Both old and new intermediates + rollover cert are provided.
550 CertIssuerSourceStatic sync_certs; 655 CertIssuerSourceStatic sync_certs;
551 sync_certs.AddCert(oldintermediate_); 656 sync_certs.AddCert(oldintermediate_);
552 sync_certs.AddCert(newintermediate_); 657 sync_certs.AddCert(newintermediate_);
553 sync_certs.AddCert(newrootrollover_); 658 sync_certs.AddCert(newrootrollover_);
554 659
555 CertPathBuilder::Result result; 660 CertPathBuilder::Result result;
556 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 661 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
557 &result); 662 &result);
558 path_builder.AddCertIssuerSource(&sync_certs); 663 path_builder.AddCertIssuerSource(&sync_certs);
559 664
560 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 665 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
561 666
562 EXPECT_EQ(OK, result.error()); 667 EXPECT_EQ(OK, result.error());
563 668
564 // Path builder willattempt one of: 669 // Path builder willattempt one of:
565 // target <- oldintermediate <- oldroot 670 // target <- oldintermediate <- oldroot
566 // target <- newintermediate <- newroot 671 // target <- newintermediate <- newroot
567 // either will succeed. 672 // either will succeed.
568 ASSERT_EQ(1U, result.paths.size()); 673 ASSERT_EQ(1U, result.paths.size());
674 const auto& path = result.paths[0]->path;
569 EXPECT_EQ(OK, result.paths[0]->error); 675 EXPECT_EQ(OK, result.paths[0]->error);
570 ASSERT_EQ(3U, result.paths[0]->path.size()); 676 ASSERT_EQ(2U, path.certs.size());
571 EXPECT_EQ(target_, result.paths[0]->path[0]); 677 EXPECT_EQ(target_, path.certs[0]);
572 if (result.paths[0]->path[1] != newintermediate_) { 678 if (path.certs[1] != newintermediate_) {
573 DVLOG(1) << "USED OLD"; 679 DVLOG(1) << "USED OLD";
574 EXPECT_EQ(oldintermediate_, result.paths[0]->path[1]); 680 EXPECT_EQ(oldintermediate_, path.certs[1]);
575 EXPECT_EQ(oldroot_, result.paths[0]->path[2]); 681 EXPECT_EQ(oldroot_, path.trust_anchor->cert());
576 } else { 682 } else {
577 DVLOG(1) << "USED NEW"; 683 DVLOG(1) << "USED NEW";
578 EXPECT_EQ(newintermediate_, result.paths[0]->path[1]); 684 EXPECT_EQ(newintermediate_, path.certs[1]);
579 EXPECT_EQ(newroot_, result.paths[0]->path[2]); 685 EXPECT_EQ(newroot_, path.trust_anchor->cert());
580 } 686 }
581 } 687 }
582 688
583 // Tests that multiple trust root matches on a single path will be considered. 689 // Tests that multiple trust root matches on a single path will be considered.
584 // Both roots have the same subject but different keys. Only one of them will 690 // Both roots have the same subject but different keys. Only one of them will
585 // verify. 691 // verify.
586 TEST_F(PathBuilderKeyRolloverTest, TestMultipleRootMatchesOnlyOneWorks) { 692 TEST_F(PathBuilderKeyRolloverTest, TestMultipleRootMatchesOnlyOneWorks) {
587 // Both newroot and oldroot are trusted. 693 // Both newroot and oldroot are trusted.
588 TrustStore trust_store; 694 TrustStore trust_store;
589 trust_store.AddTrustedCertificate(newroot_); 695 AddTrustedCertificate(newroot_, &trust_store);
590 trust_store.AddTrustedCertificate(oldroot_); 696 AddTrustedCertificate(oldroot_, &trust_store);
591 697
592 // Only oldintermediate is supplied, so the path with newroot should fail, 698 // Only oldintermediate is supplied, so the path with newroot should fail,
593 // oldroot should succeed. 699 // oldroot should succeed.
594 CertIssuerSourceStatic sync_certs; 700 CertIssuerSourceStatic sync_certs;
595 sync_certs.AddCert(oldintermediate_); 701 sync_certs.AddCert(oldintermediate_);
596 702
597 CertPathBuilder::Result result; 703 CertPathBuilder::Result result;
598 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 704 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
599 &result); 705 &result);
600 path_builder.AddCertIssuerSource(&sync_certs); 706 path_builder.AddCertIssuerSource(&sync_certs);
601 707
602 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 708 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
603 709
604 EXPECT_EQ(OK, result.error()); 710 EXPECT_EQ(OK, result.error());
605 // There may be one or two paths attempted depending if the path builder tried 711 // There may be one or two paths attempted depending if the path builder tried
606 // using newroot first. 712 // using newroot first.
607 // TODO(mattm): Once TrustStore is an interface, this could be fixed with a 713 // TODO(mattm): Once TrustStore is an interface, this could be fixed with a
608 // mock version of TrustStore that returns roots in a deterministic order. 714 // mock version of TrustStore that returns roots in a deterministic order.
609 ASSERT_LE(1U, result.paths.size()); 715 ASSERT_LE(1U, result.paths.size());
610 ASSERT_GE(2U, result.paths.size()); 716 ASSERT_GE(2U, result.paths.size());
611 717
612 if (result.paths.size() == 2) { 718 if (result.paths.size() == 2) {
613 // Path builder may first attempt: target <- oldintermediate <- newroot 719 // Path builder may first attempt: target <- oldintermediate <- newroot
614 // but it will fail since oldintermediate is signed by oldroot. 720 // but it will fail since oldintermediate is signed by oldroot.
615 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 721 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
616 ASSERT_EQ(3U, result.paths[0]->path.size()); 722 const auto& path = result.paths[0]->path;
617 EXPECT_EQ(target_, result.paths[0]->path[0]); 723 ASSERT_EQ(2U, path.certs.size());
618 EXPECT_EQ(oldintermediate_, result.paths[0]->path[1]); 724 EXPECT_EQ(target_, path.certs[0]);
619 EXPECT_EQ(newroot_, result.paths[0]->path[2]); 725 EXPECT_EQ(oldintermediate_, path.certs[1]);
726 EXPECT_EQ(newroot_, path.trust_anchor->cert());
620 } 727 }
621 728
622 // Path builder will next attempt: 729 // Path builder will next attempt:
623 // target <- old intermediate <- oldroot 730 // target <- old intermediate <- oldroot
624 // which should succeed. 731 // which should succeed.
625 EXPECT_EQ(OK, result.paths[result.best_result_index]->error); 732 EXPECT_EQ(OK, result.paths[result.best_result_index]->error);
626 ASSERT_EQ(3U, result.paths[result.best_result_index]->path.size()); 733 const auto& path = result.paths[result.best_result_index]->path;
627 EXPECT_EQ(target_, result.paths[result.best_result_index]->path[0]); 734 ASSERT_EQ(2U, path.certs.size());
628 EXPECT_EQ(oldintermediate_, result.paths[result.best_result_index]->path[1]); 735 EXPECT_EQ(target_, path.certs[0]);
629 EXPECT_EQ(oldroot_, result.paths[result.best_result_index]->path[2]); 736 EXPECT_EQ(oldintermediate_, path.certs[1]);
737 EXPECT_EQ(oldroot_, path.trust_anchor->cert());
630 } 738 }
631 739
632 // Tests that the path builder doesn't build longer than necessary paths. 740 // Tests that the path builder doesn't build longer than necessary paths.
633 TEST_F(PathBuilderKeyRolloverTest, TestRolloverLongChain) { 741 TEST_F(PathBuilderKeyRolloverTest, TestRolloverLongChain) {
634 // Only oldroot is trusted. 742 // Only oldroot is trusted.
635 TrustStore trust_store; 743 TrustStore trust_store;
636 trust_store.AddTrustedCertificate(oldroot_); 744 AddTrustedCertificate(oldroot_, &trust_store);
637 745
638 // New intermediate and new root are provided synchronously. 746 // New intermediate and new root are provided synchronously.
639 CertIssuerSourceStatic sync_certs; 747 CertIssuerSourceStatic sync_certs;
640 sync_certs.AddCert(newintermediate_); 748 sync_certs.AddCert(newintermediate_);
641 sync_certs.AddCert(newroot_); 749 sync_certs.AddCert(newroot_);
642 750
643 // Rollover cert is only provided asynchronously. This will force the 751 // Rollover cert is only provided asynchronously. This will force the
644 // pathbuilder to first try building a longer than necessary path. 752 // pathbuilder to first try building a longer than necessary path.
645 AsyncCertIssuerSourceStatic async_certs; 753 AsyncCertIssuerSourceStatic async_certs;
646 async_certs.AddCert(newrootrollover_); 754 async_certs.AddCert(newrootrollover_);
647 755
648 CertPathBuilder::Result result; 756 CertPathBuilder::Result result;
649 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 757 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
650 &result); 758 &result);
651 path_builder.AddCertIssuerSource(&sync_certs); 759 path_builder.AddCertIssuerSource(&sync_certs);
652 path_builder.AddCertIssuerSource(&async_certs); 760 path_builder.AddCertIssuerSource(&async_certs);
653 761
654 EXPECT_EQ(CompletionStatus::ASYNC, RunPathBuilder(&path_builder)); 762 EXPECT_EQ(CompletionStatus::ASYNC, RunPathBuilder(&path_builder));
655 763
656 EXPECT_EQ(OK, result.error()); 764 EXPECT_EQ(OK, result.error());
657 ASSERT_EQ(3U, result.paths.size()); 765 ASSERT_EQ(3U, result.paths.size());
658 766
659 // Path builder will first attempt: target <- newintermediate <- oldroot 767 // Path builder will first attempt: target <- newintermediate <- oldroot
660 // but it will fail since newintermediate is signed by newroot. 768 // but it will fail since newintermediate is signed by newroot.
661 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 769 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
662 ASSERT_EQ(3U, result.paths[0]->path.size()); 770 const auto& path0 = result.paths[0]->path;
663 EXPECT_EQ(target_, result.paths[0]->path[0]); 771 ASSERT_EQ(2U, path0.certs.size());
664 EXPECT_EQ(newintermediate_, result.paths[0]->path[1]); 772 EXPECT_EQ(target_, path0.certs[0]);
665 EXPECT_EQ(oldroot_, result.paths[0]->path[2]); 773 EXPECT_EQ(newintermediate_, path0.certs[1]);
774 EXPECT_EQ(oldroot_, path0.trust_anchor->cert());
666 775
667 // Path builder will next attempt: 776 // Path builder will next attempt:
668 // target <- newintermediate <- newroot <- oldroot 777 // target <- newintermediate <- newroot <- oldroot
669 // but it will fail since newroot is self-signed. 778 // but it will fail since newroot is self-signed.
670 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[1]->error); 779 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[1]->error);
671 ASSERT_EQ(4U, result.paths[1]->path.size()); 780 const auto& path1 = result.paths[1]->path;
672 EXPECT_EQ(target_, result.paths[1]->path[0]); 781 ASSERT_EQ(3U, path1.certs.size());
673 EXPECT_EQ(newintermediate_, result.paths[1]->path[1]); 782 EXPECT_EQ(target_, path1.certs[0]);
674 EXPECT_EQ(newroot_, result.paths[1]->path[2]); 783 EXPECT_EQ(newintermediate_, path1.certs[1]);
675 EXPECT_EQ(oldroot_, result.paths[1]->path[3]); 784 EXPECT_EQ(newroot_, path1.certs[2]);
785 EXPECT_EQ(oldroot_, path1.trust_anchor->cert());
676 786
677 // Path builder will skip: 787 // Path builder will skip:
678 // target <- newintermediate <- newroot <- newrootrollover <- ... 788 // target <- newintermediate <- newroot <- newrootrollover <- ...
679 // Since newroot and newrootrollover have the same Name+SAN+SPKI. 789 // Since newroot and newrootrollover have the same Name+SAN+SPKI.
680 790
681 // Finally path builder will use: 791 // Finally path builder will use:
682 // target <- newintermediate <- newrootrollover <- oldroot 792 // target <- newintermediate <- newrootrollover <- oldroot
683 EXPECT_EQ(2U, result.best_result_index); 793 EXPECT_EQ(2U, result.best_result_index);
684 EXPECT_EQ(OK, result.paths[2]->error); 794 EXPECT_EQ(OK, result.paths[2]->error);
685 ASSERT_EQ(4U, result.paths[2]->path.size()); 795 const auto& path2 = result.paths[2]->path;
686 EXPECT_EQ(target_, result.paths[2]->path[0]); 796 ASSERT_EQ(3U, path2.certs.size());
687 EXPECT_EQ(newintermediate_, result.paths[2]->path[1]); 797 EXPECT_EQ(target_, path2.certs[0]);
688 EXPECT_EQ(newrootrollover_, result.paths[2]->path[2]); 798 EXPECT_EQ(newintermediate_, path2.certs[1]);
689 EXPECT_EQ(oldroot_, result.paths[2]->path[3]); 799 EXPECT_EQ(newrootrollover_, path2.certs[2]);
800 EXPECT_EQ(oldroot_, path2.trust_anchor->cert());
690 } 801 }
691 802
692 // If the target cert is a trust root, that alone is a valid path. 803 // If the target cert is a trust anchor, however is not itself *signed* by a
804 // trust anchor, then it is not considered valid (the SPKI and name of the
805 // trust anchor matches the SPKI and subject of the targe certificate, but the
806 // rest of the certificate cannot be verified).
693 TEST_F(PathBuilderKeyRolloverTest, TestEndEntityIsTrustRoot) { 807 TEST_F(PathBuilderKeyRolloverTest, TestEndEntityIsTrustRoot) {
694 // Trust newintermediate. 808 // Trust newintermediate.
695 TrustStore trust_store; 809 TrustStore trust_store;
696 trust_store.AddTrustedCertificate(newintermediate_); 810 AddTrustedCertificate(newintermediate_, &trust_store);
697 811
698 CertPathBuilder::Result result; 812 CertPathBuilder::Result result;
699 // Newintermediate is also the target cert. 813 // Newintermediate is also the target cert.
700 CertPathBuilder path_builder(newintermediate_, &trust_store, 814 CertPathBuilder path_builder(newintermediate_, &trust_store,
701 &signature_policy_, time_, &result); 815 &signature_policy_, time_, &result);
702 816
703 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 817 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
704 818
705 EXPECT_EQ(OK, result.error()); 819 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error());
706
707 ASSERT_EQ(1U, result.paths.size());
708 EXPECT_EQ(OK, result.paths[0]->error);
709 ASSERT_EQ(1U, result.paths[0]->path.size());
710 EXPECT_EQ(newintermediate_, result.paths[0]->path[0]);
711 } 820 }
712 821
713 // If target has same Name+SAN+SPKI as a necessary intermediate, test if a path 822 // If target has same Name+SAN+SPKI as a necessary intermediate, test if a path
714 // can still be built. 823 // can still be built.
715 // Since LoopChecker will prevent the intermediate from being included, this 824 // Since LoopChecker will prevent the intermediate from being included, this
716 // currently does NOT verify. This case shouldn't occur in the web PKI. 825 // currently does NOT verify. This case shouldn't occur in the web PKI.
717 TEST_F(PathBuilderKeyRolloverTest, 826 TEST_F(PathBuilderKeyRolloverTest,
718 TestEndEntityHasSameNameAndSpkiAsIntermediate) { 827 TestEndEntityHasSameNameAndSpkiAsIntermediate) {
719 // Trust oldroot. 828 // Trust oldroot.
720 TrustStore trust_store; 829 TrustStore trust_store;
721 trust_store.AddTrustedCertificate(oldroot_); 830 AddTrustedCertificate(oldroot_, &trust_store);
722 831
723 // New root rollover is provided synchronously. 832 // New root rollover is provided synchronously.
724 CertIssuerSourceStatic sync_certs; 833 CertIssuerSourceStatic sync_certs;
725 sync_certs.AddCert(newrootrollover_); 834 sync_certs.AddCert(newrootrollover_);
726 835
727 CertPathBuilder::Result result; 836 CertPathBuilder::Result result;
728 // Newroot is the target cert. 837 // Newroot is the target cert.
729 CertPathBuilder path_builder(newroot_, &trust_store, &signature_policy_, 838 CertPathBuilder path_builder(newroot_, &trust_store, &signature_policy_,
730 time_, &result); 839 time_, &result);
731 path_builder.AddCertIssuerSource(&sync_certs); 840 path_builder.AddCertIssuerSource(&sync_certs);
732 841
733 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 842 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
734 843
735 // This could actually be OK, but CertPathBuilder does not build the 844 // This could actually be OK, but CertPathBuilder does not build the
736 // newroot <- newrootrollover <- oldroot path. 845 // newroot <- newrootrollover <- oldroot path.
737 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error()); 846 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error());
738 } 847 }
739 848
740 // If target has same Name+SAN+SPKI as the trust root, test that a (trivial) 849 // If target has same Name+SAN+SPKI as the trust root, test that a (trivial)
741 // path can still be built. 850 // path can still be built.
742 TEST_F(PathBuilderKeyRolloverTest, 851 TEST_F(PathBuilderKeyRolloverTest,
743 TestEndEntityHasSameNameAndSpkiAsTrustAnchor) { 852 TestEndEntityHasSameNameAndSpkiAsTrustAnchor) {
744 // Trust newrootrollover. 853 // Trust newrootrollover.
745 TrustStore trust_store; 854 TrustStore trust_store;
746 trust_store.AddTrustedCertificate(newrootrollover_); 855 AddTrustedCertificate(newrootrollover_, &trust_store);
747 856
748 CertPathBuilder::Result result; 857 CertPathBuilder::Result result;
749 // Newroot is the target cert. 858 // Newroot is the target cert.
750 CertPathBuilder path_builder(newroot_, &trust_store, &signature_policy_, 859 CertPathBuilder path_builder(newroot_, &trust_store, &signature_policy_,
751 time_, &result); 860 time_, &result);
752 861
753 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 862 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
754 863
755 EXPECT_EQ(OK, result.error()); 864 EXPECT_EQ(OK, result.error());
756 865
757 ASSERT_FALSE(result.paths.empty()); 866 ASSERT_FALSE(result.paths.empty());
758 const CertPathBuilder::ResultPath* best_result = 867 const CertPathBuilder::ResultPath* best_result =
759 result.paths[result.best_result_index].get(); 868 result.paths[result.best_result_index].get();
760 869
761 // Newroot has same name+SPKI as newrootrollover, thus the path is valid and 870 // Newroot has same name+SPKI as newrootrollover, thus the path is valid and
762 // only contains newroot. 871 // only contains newroot.
763 EXPECT_EQ(OK, best_result->error); 872 EXPECT_EQ(OK, best_result->error);
764 ASSERT_EQ(1U, best_result->path.size()); 873 ASSERT_EQ(1U, best_result->path.certs.size());
765 EXPECT_EQ(newroot_, best_result->path[0]); 874 EXPECT_EQ(newroot_, best_result->path.certs[0]);
875 EXPECT_EQ(newrootrollover_, best_result->path.trust_anchor->cert());
766 } 876 }
767 877
768 // Test that PathBuilder will not try the same path twice if multiple 878 // Test that PathBuilder will not try the same path twice if multiple
769 // CertIssuerSources provide the same certificate. 879 // CertIssuerSources provide the same certificate.
770 TEST_F(PathBuilderKeyRolloverTest, TestDuplicateIntermediates) { 880 TEST_F(PathBuilderKeyRolloverTest, TestDuplicateIntermediates) {
771 // Create a separate copy of oldintermediate. 881 // Create a separate copy of oldintermediate.
772 scoped_refptr<ParsedCertificate> oldintermediate_dupe( 882 scoped_refptr<ParsedCertificate> oldintermediate_dupe(
773 ParsedCertificate::CreateFromCertificateCopy( 883 ParsedCertificate::CreateFromCertificateCopy(
774 oldintermediate_->der_cert().AsStringPiece(), {})); 884 oldintermediate_->der_cert().AsStringPiece(), {}));
775 885
776 // Only newroot is a trusted root. 886 // Only newroot is a trusted root.
777 TrustStore trust_store; 887 TrustStore trust_store;
778 trust_store.AddTrustedCertificate(newroot_); 888 AddTrustedCertificate(newroot_, &trust_store);
779 889
780 // The oldintermediate is supplied synchronously by |sync_certs1| and 890 // The oldintermediate is supplied synchronously by |sync_certs1| and
781 // another copy of oldintermediate is supplied synchronously by |sync_certs2|. 891 // another copy of oldintermediate is supplied synchronously by |sync_certs2|.
782 // The path target <- oldintermediate <- newroot should be built first, 892 // The path target <- oldintermediate <- newroot should be built first,
783 // though it won't verify. It should not be attempted again even though 893 // though it won't verify. It should not be attempted again even though
784 // oldintermediate was supplied twice. 894 // oldintermediate was supplied twice.
785 CertIssuerSourceStatic sync_certs1; 895 CertIssuerSourceStatic sync_certs1;
786 sync_certs1.AddCert(oldintermediate_); 896 sync_certs1.AddCert(oldintermediate_);
787 CertIssuerSourceStatic sync_certs2; 897 CertIssuerSourceStatic sync_certs2;
788 sync_certs2.AddCert(oldintermediate_dupe); 898 sync_certs2.AddCert(oldintermediate_dupe);
(...skipping 11 matching lines...) Expand all
800 path_builder.AddCertIssuerSource(&async_certs); 910 path_builder.AddCertIssuerSource(&async_certs);
801 911
802 EXPECT_EQ(CompletionStatus::ASYNC, RunPathBuilder(&path_builder)); 912 EXPECT_EQ(CompletionStatus::ASYNC, RunPathBuilder(&path_builder));
803 913
804 EXPECT_EQ(OK, result.error()); 914 EXPECT_EQ(OK, result.error());
805 ASSERT_EQ(2U, result.paths.size()); 915 ASSERT_EQ(2U, result.paths.size());
806 916
807 // Path builder will first attempt: target <- oldintermediate <- newroot 917 // Path builder will first attempt: target <- oldintermediate <- newroot
808 // but it will fail since oldintermediate is signed by oldroot. 918 // but it will fail since oldintermediate is signed by oldroot.
809 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 919 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
810 ASSERT_EQ(3U, result.paths[0]->path.size()); 920 const auto& path0 = result.paths[0]->path;
811 EXPECT_EQ(target_, result.paths[0]->path[0]); 921
922 ASSERT_EQ(2U, path0.certs.size());
923 EXPECT_EQ(target_, path0.certs[0]);
812 // Compare the DER instead of ParsedCertificate pointer, don't care which copy 924 // Compare the DER instead of ParsedCertificate pointer, don't care which copy
813 // of oldintermediate was used in the path. 925 // of oldintermediate was used in the path.
814 EXPECT_EQ(oldintermediate_->der_cert(), result.paths[0]->path[1]->der_cert()); 926 EXPECT_EQ(oldintermediate_->der_cert(), path0.certs[1]->der_cert());
815 EXPECT_EQ(newroot_, result.paths[0]->path[2]); 927 EXPECT_EQ(newroot_, path0.trust_anchor->cert());
816 928
817 // Path builder will next attempt: target <- newintermediate <- newroot 929 // Path builder will next attempt: target <- newintermediate <- newroot
818 // which will succeed. 930 // which will succeed.
819 EXPECT_EQ(1U, result.best_result_index); 931 EXPECT_EQ(1U, result.best_result_index);
820 EXPECT_EQ(OK, result.paths[1]->error); 932 EXPECT_EQ(OK, result.paths[1]->error);
821 ASSERT_EQ(3U, result.paths[1]->path.size()); 933 const auto& path1 = result.paths[1]->path;
822 EXPECT_EQ(target_, result.paths[1]->path[0]); 934 ASSERT_EQ(2U, path1.certs.size());
823 EXPECT_EQ(newintermediate_, result.paths[1]->path[1]); 935 EXPECT_EQ(target_, path1.certs[0]);
824 EXPECT_EQ(newroot_, result.paths[1]->path[2]); 936 EXPECT_EQ(newintermediate_, path1.certs[1]);
937 EXPECT_EQ(newroot_, path1.trust_anchor->cert());
825 } 938 }
826 939
827 // Test that PathBuilder will not try the same path twice if the same cert is 940 // Test when PathBuilder is given a cert CertIssuerSources that has the same
828 // presented via a CertIssuerSources and a TrustAnchor. 941 // SPKI as a TrustAnchor.
829 TEST_F(PathBuilderKeyRolloverTest, TestDuplicateIntermediateAndRoot) { 942 TEST_F(PathBuilderKeyRolloverTest, TestDuplicateIntermediateAndRoot) {
830 // Create a separate copy of newroot. 943 // Create a separate copy of newroot.
831 scoped_refptr<ParsedCertificate> newroot_dupe( 944 scoped_refptr<ParsedCertificate> newroot_dupe(
832 ParsedCertificate::CreateFromCertificateCopy( 945 ParsedCertificate::CreateFromCertificateCopy(
833 newroot_->der_cert().AsStringPiece(), {})); 946 newroot_->der_cert().AsStringPiece(), {}));
834 947
835 // Only newroot is a trusted root. 948 // Only newroot is a trusted root.
836 TrustStore trust_store; 949 TrustStore trust_store;
837 trust_store.AddTrustedCertificate(newroot_); 950 AddTrustedCertificate(newroot_, &trust_store);
838 951
839 // The oldintermediate and newroot are supplied synchronously by |sync_certs|. 952 // The oldintermediate and newroot are supplied synchronously by |sync_certs|.
840 CertIssuerSourceStatic sync_certs; 953 CertIssuerSourceStatic sync_certs;
841 sync_certs.AddCert(oldintermediate_); 954 sync_certs.AddCert(oldintermediate_);
842 sync_certs.AddCert(newroot_dupe); 955 sync_certs.AddCert(newroot_dupe);
843 956
844 CertPathBuilder::Result result; 957 CertPathBuilder::Result result;
845 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 958 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
846 &result); 959 &result);
847 path_builder.AddCertIssuerSource(&sync_certs); 960 path_builder.AddCertIssuerSource(&sync_certs);
848 961
849 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder)); 962 EXPECT_EQ(CompletionStatus::SYNC, RunPathBuilder(&path_builder));
850 963
851 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error()); 964 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.error());
852 ASSERT_EQ(1U, result.paths.size()); 965 ASSERT_EQ(2U, result.paths.size());
966 // TODO(eroman): Is this right?
853 967
854 // Path builder attempt: target <- oldintermediate <- newroot 968 // Path builder attempt: target <- oldintermediate <- newroot
855 // but it will fail since oldintermediate is signed by oldroot. 969 // but it will fail since oldintermediate is signed by oldroot.
856 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 970 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
857 ASSERT_EQ(3U, result.paths[0]->path.size()); 971 const auto& path = result.paths[0]->path;
858 EXPECT_EQ(target_, result.paths[0]->path[0]); 972 ASSERT_EQ(2U, path.certs.size());
859 EXPECT_EQ(oldintermediate_, result.paths[0]->path[1]); 973 EXPECT_EQ(target_, path.certs[0]);
974 EXPECT_EQ(oldintermediate_, path.certs[1]);
860 // Compare the DER instead of ParsedCertificate pointer, don't care which copy 975 // Compare the DER instead of ParsedCertificate pointer, don't care which copy
861 // of newroot was used in the path. 976 // of newroot was used in the path.
862 EXPECT_EQ(newroot_->der_cert(), result.paths[0]->path[2]->der_cert()); 977 EXPECT_EQ(newroot_->der_cert(), path.trust_anchor->cert()->der_cert());
863 } 978 }
864 979
865 class MockCertIssuerSourceRequest : public CertIssuerSource::Request { 980 class MockCertIssuerSourceRequest : public CertIssuerSource::Request {
866 public: 981 public:
867 MOCK_METHOD1(GetNext, CompletionStatus(scoped_refptr<ParsedCertificate>*)); 982 MOCK_METHOD1(GetNext, CompletionStatus(scoped_refptr<ParsedCertificate>*));
868 }; 983 };
869 984
870 class MockCertIssuerSource : public CertIssuerSource { 985 class MockCertIssuerSource : public CertIssuerSource {
871 public: 986 public:
872 MOCK_METHOD2(SyncGetIssuersOf, 987 MOCK_METHOD2(SyncGetIssuersOf,
(...skipping 22 matching lines...) Expand all
895 }; 1010 };
896 1011
897 // Test that a single CertIssuerSource returning multiple async batches of 1012 // Test that a single CertIssuerSource returning multiple async batches of
898 // issuers is handled correctly. Due to the StrictMocks, it also tests that path 1013 // issuers is handled correctly. Due to the StrictMocks, it also tests that path
899 // builder does not request issuers of certs that it shouldn't. 1014 // builder does not request issuers of certs that it shouldn't.
900 TEST_F(PathBuilderKeyRolloverTest, TestMultipleAsyncCallbacksFromSingleSource) { 1015 TEST_F(PathBuilderKeyRolloverTest, TestMultipleAsyncCallbacksFromSingleSource) {
901 StrictMock<MockCertIssuerSource> cert_issuer_source; 1016 StrictMock<MockCertIssuerSource> cert_issuer_source;
902 1017
903 // Only newroot is a trusted root. 1018 // Only newroot is a trusted root.
904 TrustStore trust_store; 1019 TrustStore trust_store;
905 trust_store.AddTrustedCertificate(newroot_); 1020 AddTrustedCertificate(newroot_, &trust_store);
906 1021
907 CertPathBuilder::Result result; 1022 CertPathBuilder::Result result;
908 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 1023 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
909 &result); 1024 &result);
910 path_builder.AddCertIssuerSource(&cert_issuer_source); 1025 path_builder.AddCertIssuerSource(&cert_issuer_source);
911 1026
912 CertIssuerSource::IssuerCallback target_issuers_callback; 1027 CertIssuerSource::IssuerCallback target_issuers_callback;
913 // Create the mock CertIssuerSource::Request... 1028 // Create the mock CertIssuerSource::Request...
914 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>> 1029 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>>
915 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>()); 1030 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 1087
973 // Ensure pathbuilder finished and filled result. 1088 // Ensure pathbuilder finished and filled result.
974 callback.WaitForResult(); 1089 callback.WaitForResult();
975 1090
976 EXPECT_EQ(OK, result.error()); 1091 EXPECT_EQ(OK, result.error());
977 ASSERT_EQ(2U, result.paths.size()); 1092 ASSERT_EQ(2U, result.paths.size());
978 1093
979 // Path builder first attempts: target <- oldintermediate <- newroot 1094 // Path builder first attempts: target <- oldintermediate <- newroot
980 // but it will fail since oldintermediate is signed by oldroot. 1095 // but it will fail since oldintermediate is signed by oldroot.
981 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 1096 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
982 ASSERT_EQ(3U, result.paths[0]->path.size()); 1097 const auto& path0 = result.paths[0]->path;
983 EXPECT_EQ(target_, result.paths[0]->path[0]); 1098 ASSERT_EQ(2U, path0.certs.size());
984 EXPECT_EQ(oldintermediate_, result.paths[0]->path[1]); 1099 EXPECT_EQ(target_, path0.certs[0]);
985 EXPECT_EQ(newroot_, result.paths[0]->path[2]); 1100 EXPECT_EQ(oldintermediate_, path0.certs[1]);
1101 EXPECT_EQ(newroot_, path0.trust_anchor->cert());
986 1102
987 // After the second batch of async results, path builder will attempt: 1103 // After the second batch of async results, path builder will attempt:
988 // target <- newintermediate <- newroot which will succeed. 1104 // target <- newintermediate <- newroot which will succeed.
989 EXPECT_EQ(OK, result.paths[1]->error); 1105 EXPECT_EQ(OK, result.paths[1]->error);
990 ASSERT_EQ(3U, result.paths[1]->path.size()); 1106 const auto& path1 = result.paths[1]->path;
991 EXPECT_EQ(target_, result.paths[1]->path[0]); 1107 ASSERT_EQ(2U, path1.certs.size());
992 EXPECT_EQ(newintermediate_, result.paths[1]->path[1]); 1108 EXPECT_EQ(target_, path1.certs[0]);
993 EXPECT_EQ(newroot_, result.paths[1]->path[2]); 1109 EXPECT_EQ(newintermediate_, path1.certs[1]);
1110 EXPECT_EQ(newroot_, path1.trust_anchor->cert());
994 } 1111 }
995 1112
996 // Test that PathBuilder will not try the same path twice if CertIssuerSources 1113 // Test that PathBuilder will not try the same path twice if CertIssuerSources
997 // asynchronously provide the same certificate multiple times. 1114 // asynchronously provide the same certificate multiple times.
998 TEST_F(PathBuilderKeyRolloverTest, TestDuplicateAsyncIntermediates) { 1115 TEST_F(PathBuilderKeyRolloverTest, TestDuplicateAsyncIntermediates) {
999 StrictMock<MockCertIssuerSource> cert_issuer_source; 1116 StrictMock<MockCertIssuerSource> cert_issuer_source;
1000 1117
1001 // Only newroot is a trusted root. 1118 // Only newroot is a trusted root.
1002 TrustStore trust_store; 1119 TrustStore trust_store;
1003 trust_store.AddTrustedCertificate(newroot_); 1120 AddTrustedCertificate(newroot_, &trust_store);
1004 1121
1005 CertPathBuilder::Result result; 1122 CertPathBuilder::Result result;
1006 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 1123 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
1007 &result); 1124 &result);
1008 path_builder.AddCertIssuerSource(&cert_issuer_source); 1125 path_builder.AddCertIssuerSource(&cert_issuer_source);
1009 1126
1010 CertIssuerSource::IssuerCallback target_issuers_callback; 1127 CertIssuerSource::IssuerCallback target_issuers_callback;
1011 // Create the mock CertIssuerSource::Request... 1128 // Create the mock CertIssuerSource::Request...
1012 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>> 1129 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>>
1013 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>()); 1130 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>());
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 1202
1086 // Ensure pathbuilder finished and filled result. 1203 // Ensure pathbuilder finished and filled result.
1087 callback.WaitForResult(); 1204 callback.WaitForResult();
1088 1205
1089 EXPECT_EQ(OK, result.error()); 1206 EXPECT_EQ(OK, result.error());
1090 ASSERT_EQ(2U, result.paths.size()); 1207 ASSERT_EQ(2U, result.paths.size());
1091 1208
1092 // Path builder first attempts: target <- oldintermediate <- newroot 1209 // Path builder first attempts: target <- oldintermediate <- newroot
1093 // but it will fail since oldintermediate is signed by oldroot. 1210 // but it will fail since oldintermediate is signed by oldroot.
1094 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error); 1211 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, result.paths[0]->error);
1095 ASSERT_EQ(3U, result.paths[0]->path.size()); 1212 const auto& path0 = result.paths[0]->path;
1096 EXPECT_EQ(target_, result.paths[0]->path[0]); 1213 ASSERT_EQ(2U, path0.certs.size());
1097 EXPECT_EQ(oldintermediate_, result.paths[0]->path[1]); 1214 EXPECT_EQ(target_, path0.certs[0]);
1098 EXPECT_EQ(newroot_, result.paths[0]->path[2]); 1215 EXPECT_EQ(oldintermediate_, path0.certs[1]);
1216 EXPECT_EQ(newroot_, path0.trust_anchor->cert());
1099 1217
1100 // The second async result does not generate any path. 1218 // The second async result does not generate any path.
1101 1219
1102 // After the third batch of async results, path builder will attempt: 1220 // After the third batch of async results, path builder will attempt:
1103 // target <- newintermediate <- newroot which will succeed. 1221 // target <- newintermediate <- newroot which will succeed.
1104 EXPECT_EQ(OK, result.paths[1]->error); 1222 EXPECT_EQ(OK, result.paths[1]->error);
1105 ASSERT_EQ(3U, result.paths[1]->path.size()); 1223 const auto& path1 = result.paths[1]->path;
1106 EXPECT_EQ(target_, result.paths[1]->path[0]); 1224 ASSERT_EQ(2U, path1.certs.size());
1107 EXPECT_EQ(newintermediate_, result.paths[1]->path[1]); 1225 EXPECT_EQ(target_, path1.certs[0]);
1108 EXPECT_EQ(newroot_, result.paths[1]->path[2]); 1226 EXPECT_EQ(newintermediate_, path1.certs[1]);
1227 EXPECT_EQ(newroot_, path1.trust_anchor->cert());
1109 } 1228 }
1110 1229
1111 } // namespace 1230 } // namespace
1112 1231
1113 } // namespace net 1232 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/internal/path_builder_pkits_unittest.cc ('k') | net/cert/internal/path_builder_verify_certificate_chain_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698