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

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

Powered by Google App Engine
This is Rietveld 408576698