-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathpath_builder_unittest.cc
More file actions
3065 lines (2587 loc) · 117 KB
/
path_builder_unittest.cc
File metadata and controls
3065 lines (2587 loc) · 117 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 The Chromium Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "path_builder.h"
#include <algorithm>
#include <memory>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <openssl/pool.h>
#include <openssl/pki/verify.h>
#include "cert_error_params.h"
#include "cert_issuer_source.h"
#include "cert_issuer_source_static.h"
#include "certificate_policies.h"
#include "common_cert_errors.h"
#include "input.h"
#include "mock_signature_verify_cache.h"
#include "parsed_certificate.h"
#include "simple_path_builder_delegate.h"
#include "string_util.h"
#include "test_helpers.h"
#include "trust_store.h"
#include "trust_store_collection.h"
#include "trust_store_in_memory.h"
#include "verify_certificate_chain.h"
BSSL_NAMESPACE_BEGIN
// TODO(crbug.com/634443): Assert the errors for each ResultPath.
namespace {
using ::testing::_;
using ::testing::Invoke;
using ::testing::StrictMock;
class TestPathBuilderDelegate : public SimplePathBuilderDelegate {
public:
TestPathBuilderDelegate(size_t min_rsa_modulus_length_bits,
DigestPolicy digest_policy)
: SimplePathBuilderDelegate(min_rsa_modulus_length_bits, digest_policy) {}
bool IsDeadlineExpired() override { return deadline_is_expired_; }
void SetDeadlineExpiredForTesting(bool deadline_is_expired) {
deadline_is_expired_ = deadline_is_expired;
}
SignatureVerifyCache *GetVerifyCache() override {
return use_signature_cache_ ? &cache_ : nullptr;
}
void ActivateCache() { use_signature_cache_ = true; }
void DeActivateCache() { use_signature_cache_ = false; }
MockSignatureVerifyCache *GetMockVerifyCache() { return &cache_; }
void AllowPrecert() { allow_precertificate_ = true; }
void DisallowPrecert() { allow_precertificate_ = false; }
bool AcceptPreCertificates() override {
return allow_precertificate_;
}
private:
bool deadline_is_expired_ = false;
bool use_signature_cache_ = false;
bool allow_precertificate_ = false;
MockSignatureVerifyCache cache_;
};
class CertPathBuilderDelegateBase : public SimplePathBuilderDelegate {
public:
CertPathBuilderDelegateBase()
: SimplePathBuilderDelegate(
1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1) {}
void CheckPathAfterVerification(const CertPathBuilder &path_builder,
CertPathBuilderResultPath *path) override {
ADD_FAILURE() << "Tests must override this";
}
};
class MockPathBuilderDelegate : public CertPathBuilderDelegateBase {
public:
MOCK_METHOD2(CheckPathAfterVerification,
void(const CertPathBuilder &path_builder,
CertPathBuilderResultPath *path));
};
// AsyncCertIssuerSourceStatic always returns its certs asynchronously.
class AsyncCertIssuerSourceStatic : public CertIssuerSource {
public:
class StaticAsyncRequest : public Request {
public:
explicit StaticAsyncRequest(ParsedCertificateList &&issuers) {
issuers_.swap(issuers);
issuers_iter_ = issuers_.begin();
}
StaticAsyncRequest(const StaticAsyncRequest &) = delete;
StaticAsyncRequest &operator=(const StaticAsyncRequest &) = delete;
~StaticAsyncRequest() override = default;
void GetNext(ParsedCertificateList *out_certs) override {
if (issuers_iter_ != issuers_.end()) {
out_certs->push_back(std::move(*issuers_iter_++));
}
}
ParsedCertificateList issuers_;
ParsedCertificateList::iterator issuers_iter_;
};
~AsyncCertIssuerSourceStatic() override = default;
void SetAsyncGetCallback(std::function<void()> closure) {
async_get_callback_ = std::move(closure);
}
void AddCert(std::shared_ptr<const ParsedCertificate> cert) {
static_cert_issuer_source_.AddCert(std::move(cert));
}
void SyncGetIssuersOf(const ParsedCertificate *cert,
ParsedCertificateList *issuers) override {}
void AsyncGetIssuersOf(const ParsedCertificate *cert,
std::unique_ptr<Request> *out_req) override {
num_async_gets_++;
ParsedCertificateList issuers;
static_cert_issuer_source_.SyncGetIssuersOf(cert, &issuers);
auto req = std::make_unique<StaticAsyncRequest>(std::move(issuers));
*out_req = std::move(req);
if (async_get_callback_) {
async_get_callback_();
}
}
int num_async_gets() const { return num_async_gets_; }
private:
CertIssuerSourceStatic static_cert_issuer_source_;
int num_async_gets_ = 0;
std::function<void()> async_get_callback_ = nullptr;
};
::testing::AssertionResult ReadTestPem(const std::string &file_name,
const std::string &block_name,
std::string *result) {
const PemBlockMapping mappings[] = {
{block_name.c_str(), result},
};
return ReadTestDataFromPemFile(file_name, mappings);
}
::testing::AssertionResult ReadTestCert(
const std::string &file_name,
std::shared_ptr<const ParsedCertificate> *result) {
std::string der;
::testing::AssertionResult r = ReadTestPem(
"testdata/path_builder_unittest/" + file_name, "CERTIFICATE", &der);
if (!r) {
return r;
}
CertErrors errors;
*result = ParsedCertificate::Create(
bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new(
reinterpret_cast<const uint8_t *>(der.data()), der.size(), nullptr)),
{}, &errors);
if (!*result) {
return ::testing::AssertionFailure()
<< "ParseCertificate::Create() failed:\n"
<< errors.ToDebugString();
}
return ::testing::AssertionSuccess();
}
class PathBuilderMultiRootTest : public ::testing::Test {
public:
PathBuilderMultiRootTest()
: delegate_(1024, TestPathBuilderDelegate::DigestPolicy::kWeakAllowSha1) {
}
void SetUp() override {
ASSERT_TRUE(ReadTestCert("multi-root-A-by-B.pem", &a_by_b_));
ASSERT_TRUE(ReadTestCert("multi-root-B-by-C.pem", &b_by_c_));
ASSERT_TRUE(ReadTestCert("multi-root-B-by-F.pem", &b_by_f_));
ASSERT_TRUE(ReadTestCert("multi-root-C-by-D.pem", &c_by_d_));
ASSERT_TRUE(ReadTestCert("multi-root-C-by-E.pem", &c_by_e_));
ASSERT_TRUE(ReadTestCert("multi-root-D-by-D.pem", &d_by_d_));
ASSERT_TRUE(ReadTestCert("multi-root-E-by-E.pem", &e_by_e_));
ASSERT_TRUE(ReadTestCert("multi-root-F-by-E.pem", &f_by_e_));
}
protected:
std::shared_ptr<const ParsedCertificate> a_by_b_, b_by_c_, b_by_f_, c_by_d_,
c_by_e_, d_by_d_, e_by_e_, f_by_e_;
TestPathBuilderDelegate delegate_;
der::GeneralizedTime time_ = {2017, 3, 1, 0, 0, 0};
const InitialExplicitPolicy initial_explicit_policy_ =
InitialExplicitPolicy::kFalse;
const std::set<der::Input> user_initial_policy_set_ = {
der::Input(kAnyPolicyOid)};
const InitialPolicyMappingInhibit initial_policy_mapping_inhibit_ =
InitialPolicyMappingInhibit::kFalse;
const InitialAnyPolicyInhibit initial_any_policy_inhibit_ =
InitialAnyPolicyInhibit::kFalse;
};
// Tests when the target cert has the same name and key as a trust anchor,
// however is signed by a different trust anchor. This should successfully build
// a path, however the trust anchor will be the signer of this cert.
//
// (This test is very similar to TestEndEntityHasSameNameAndSpkiAsTrustAnchor
// but with different data; also in this test the target cert itself is in the
// trust store).
TEST_F(PathBuilderMultiRootTest, TargetHasNameAndSpkiOfTrustAnchor) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(a_by_b_);
trust_store.AddTrustAnchor(b_by_f_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
auto result = path_builder.Run();
ASSERT_TRUE(result.HasValidPath());
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
const auto &path = *result.GetBestValidPath();
ASSERT_EQ(2U, path.certs.size());
EXPECT_EQ(a_by_b_, path.certs[0]);
EXPECT_EQ(b_by_f_, path.certs[1]);
}
// If the target cert is has the same name and key as a trust anchor, however
// is NOT itself signed by a trust anchor, it fails. Although the provided SPKI
// is trusted, the certificate contents cannot be verified.
TEST_F(PathBuilderMultiRootTest, TargetWithSameNameAsTrustAnchorFails) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(a_by_b_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
auto result = path_builder.Run();
EXPECT_FALSE(result.HasValidPath());
EXPECT_EQ(1U, result.max_depth_seen);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND)
<< error.DiagnosticString();
}
// Test a failed path building when the trust anchor is provided as a
// supplemental certificate. Conceptually the following paths could be built:
//
// B(C) <- C(D) <- [Trust anchor D]
// B(C) <- C(D) <- D(D) <- [Trust anchor D]
//
// However the second one is extraneous given the shorter path.
TEST_F(PathBuilderMultiRootTest, SelfSignedTrustAnchorSupplementalCert) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
// The (extraneous) trust anchor D(D) is supplied as a certificate, as is the
// intermediate needed for path building C(D).
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(d_by_d_);
sync_certs.AddCert(c_by_d_);
// C(D) is not valid at this time, so path building will fail.
der::GeneralizedTime expired_time = {2016, 1, 1, 0, 0, 0};
CertPathBuilder path_builder(
b_by_c_, &trust_store, &delegate_, expired_time, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
EXPECT_FALSE(result.HasValidPath());
ASSERT_EQ(1U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
const auto &path0 = *result.paths[0];
ASSERT_EQ(3U, path0.certs.size());
EXPECT_EQ(b_by_c_, path0.certs[0]);
EXPECT_EQ(c_by_d_, path0.certs[1]);
EXPECT_EQ(d_by_d_, path0.certs[2]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::CERTIFICATE_NOT_YET_VALID)
<< error.DiagnosticString();
}
// Test verifying a certificate that is a trust anchor.
TEST_F(PathBuilderMultiRootTest, TargetIsSelfSignedTrustAnchor) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(e_by_e_);
// This is not necessary for the test, just an extra...
trust_store.AddTrustAnchor(f_by_e_);
CertPathBuilder path_builder(
e_by_e_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
auto result = path_builder.Run();
ASSERT_TRUE(result.HasValidPath());
// Verifying a trusted leaf certificate is not permitted, however this
// certificate is self-signed, and can chain to itself.
const auto &path = *result.GetBestValidPath();
ASSERT_EQ(2U, path.certs.size());
EXPECT_EQ(e_by_e_, path.certs[0]);
EXPECT_EQ(e_by_e_, path.certs[1]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
// If the target cert is directly issued by a trust anchor, it should verify
// without any intermediate certs being provided.
TEST_F(PathBuilderMultiRootTest, TargetDirectlySignedByTrustAnchor) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(b_by_f_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
auto result = path_builder.Run();
ASSERT_TRUE(result.HasValidPath());
const auto &path = *result.GetBestValidPath();
ASSERT_EQ(2U, path.certs.size());
EXPECT_EQ(a_by_b_, path.certs[0]);
EXPECT_EQ(b_by_f_, path.certs[1]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
// Test that async cert queries are not made if the path can be successfully
// built with synchronously available certs.
TEST_F(PathBuilderMultiRootTest, TriesSyncFirst) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(e_by_e_);
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_f_);
sync_certs.AddCert(f_by_e_);
AsyncCertIssuerSourceStatic async_certs;
async_certs.AddCert(b_by_c_);
async_certs.AddCert(c_by_e_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&async_certs);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
EXPECT_TRUE(result.HasValidPath());
EXPECT_EQ(0, async_certs.num_async_gets());
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
// If async queries are needed, all async sources will be queried
// simultaneously.
TEST_F(PathBuilderMultiRootTest, TestAsyncSimultaneous) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(e_by_e_);
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
sync_certs.AddCert(b_by_f_);
AsyncCertIssuerSourceStatic async_certs1;
async_certs1.AddCert(c_by_e_);
AsyncCertIssuerSourceStatic async_certs2;
async_certs2.AddCert(f_by_e_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&async_certs1);
path_builder.AddCertIssuerSource(&async_certs2);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
EXPECT_TRUE(result.HasValidPath());
EXPECT_EQ(1, async_certs1.num_async_gets());
EXPECT_EQ(1, async_certs2.num_async_gets());
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
// Test that PathBuilder does not generate longer paths than necessary if one of
// the supplied certs is itself a trust anchor.
TEST_F(PathBuilderMultiRootTest, TestLongChain) {
// Both D(D) and C(D) are trusted roots.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
trust_store.AddTrustAnchor(c_by_d_);
// Certs B(C), and C(D) are all supplied.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
sync_certs.AddCert(c_by_d_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
ASSERT_TRUE(result.HasValidPath());
// The result path should be A(B) <- B(C) <- C(D)
// not the longer but also valid A(B) <- B(C) <- C(D) <- D(D)
EXPECT_EQ(3U, result.GetBestValidPath()->certs.size());
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
// Test that PathBuilder will backtrack and try a different path if the first
// one doesn't work out.
TEST_F(PathBuilderMultiRootTest, TestBacktracking) {
// Only D(D) is a trusted root.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
// Certs B(F) and F(E) are supplied synchronously, thus the path
// A(B) <- B(F) <- F(E) should be built first, though it won't verify.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_f_);
sync_certs.AddCert(f_by_e_);
// Certs B(C), and C(D) are supplied asynchronously, so the path
// A(B) <- B(C) <- C(D) <- D(D) should be tried second.
AsyncCertIssuerSourceStatic async_certs;
async_certs.AddCert(b_by_c_);
async_certs.AddCert(c_by_d_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
path_builder.AddCertIssuerSource(&async_certs);
auto result = path_builder.Run();
ASSERT_TRUE(result.HasValidPath());
// The partial path should be returned even though it didn't reach a trust
// anchor.
ASSERT_EQ(2U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]);
EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]);
// The result path should be A(B) <- B(C) <- C(D) <- D(D)
EXPECT_EQ(1U, result.best_result_index);
EXPECT_TRUE(result.paths[1]->IsValid());
const auto &path = *result.GetBestValidPath();
ASSERT_EQ(4U, path.certs.size());
EXPECT_EQ(a_by_b_, path.certs[0]);
EXPECT_EQ(b_by_c_, path.certs[1]);
EXPECT_EQ(c_by_d_, path.certs[2]);
EXPECT_EQ(d_by_d_, path.certs[3]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
// Test that if no path to a trust anchor was found, the partial path is
// returned.
TEST_F(PathBuilderMultiRootTest, TestOnlyPartialPathResult) {
TrustStoreInMemory trust_store;
// Certs B(F) and F(E) are supplied synchronously, thus the path
// A(B) <- B(F) <- F(E) should be built first, though it won't verify.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_f_);
sync_certs.AddCert(f_by_e_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
EXPECT_FALSE(result.HasValidPath());
// The partial path should be returned even though it didn't reach a trust
// anchor.
ASSERT_EQ(1U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]);
EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND)
<< error.DiagnosticString();
}
// Test that if two partial paths are returned, the first is marked as the best
// path.
TEST_F(PathBuilderMultiRootTest, TestTwoPartialPathResults) {
TrustStoreInMemory trust_store;
// Certs B(F) and F(E) are supplied synchronously, thus the path
// A(B) <- B(F) <- F(E) should be built first, though it won't verify.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_f_);
sync_certs.AddCert(f_by_e_);
// Certs B(C), and C(D) are supplied asynchronously, so the path
// A(B) <- B(C) <- C(D) <- D(D) should be tried second.
AsyncCertIssuerSourceStatic async_certs;
async_certs.AddCert(b_by_c_);
async_certs.AddCert(c_by_d_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
path_builder.AddCertIssuerSource(&async_certs);
auto result = path_builder.Run();
EXPECT_FALSE(result.HasValidPath());
// First partial path found should be marked as the best one.
EXPECT_EQ(0U, result.best_result_index);
ASSERT_EQ(2U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]);
EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]);
EXPECT_FALSE(result.paths[1]->IsValid());
ASSERT_EQ(3U, result.paths[1]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[1]->certs[0]);
EXPECT_EQ(b_by_c_, result.paths[1]->certs[1]);
EXPECT_EQ(c_by_d_, result.paths[1]->certs[2]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND)
<< error.DiagnosticString();
}
// Test that if no valid path is found, and the first invalid path is a partial
// path, but the 2nd invalid path ends with a cert with a trust record, the 2nd
// path should be preferred.
TEST_F(PathBuilderMultiRootTest, TestDistrustedPathPreferredOverPartialPath) {
// Only D(D) has a trust record, but it is distrusted.
TrustStoreInMemory trust_store;
trust_store.AddDistrustedCertificateForTest(d_by_d_);
// Certs B(F) and F(E) are supplied synchronously, thus the path
// A(B) <- B(F) <- F(E) should be built first, though it won't verify.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_f_);
sync_certs.AddCert(f_by_e_);
// Certs B(C), and C(D) are supplied asynchronously, so the path
// A(B) <- B(C) <- C(D) <- D(D) should be tried second.
AsyncCertIssuerSourceStatic async_certs;
async_certs.AddCert(b_by_c_);
async_certs.AddCert(c_by_d_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
path_builder.AddCertIssuerSource(&async_certs);
auto result = path_builder.Run();
EXPECT_FALSE(result.HasValidPath());
// The partial path should be returned even though it didn't reach a trust
// anchor.
ASSERT_EQ(2U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_f_, result.paths[0]->certs[1]);
EXPECT_EQ(f_by_e_, result.paths[0]->certs[2]);
// The result path should be A(B) <- B(C) <- C(D) <- D(D)
EXPECT_EQ(1U, result.best_result_index);
EXPECT_FALSE(result.paths[1]->IsValid());
const auto &path = *result.GetBestPathPossiblyInvalid();
ASSERT_EQ(4U, path.certs.size());
EXPECT_EQ(a_by_b_, path.certs[0]);
EXPECT_EQ(b_by_c_, path.certs[1]);
EXPECT_EQ(c_by_d_, path.certs[2]);
EXPECT_EQ(d_by_d_, path.certs[3]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_NOT_FOUND)
<< error.DiagnosticString();
}
// Test that whichever order CertIssuerSource returns the issuers, the path
// building still succeeds.
TEST_F(PathBuilderMultiRootTest, TestCertIssuerOrdering) {
// Only D(D) is a trusted root.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
for (bool reverse_order : {false, true}) {
SCOPED_TRACE(reverse_order);
std::vector<std::shared_ptr<const ParsedCertificate>> certs = {
b_by_c_, b_by_f_, f_by_e_, c_by_d_, c_by_e_};
CertIssuerSourceStatic sync_certs;
if (reverse_order) {
for (auto it = certs.rbegin(); it != certs.rend(); ++it) {
sync_certs.AddCert(*it);
}
} else {
for (const auto &cert : certs) {
sync_certs.AddCert(cert);
}
}
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
ASSERT_TRUE(result.HasValidPath());
// The result path should be A(B) <- B(C) <- C(D) <- D(D)
const auto &path = *result.GetBestValidPath();
ASSERT_EQ(4U, path.certs.size());
EXPECT_EQ(a_by_b_, path.certs[0]);
EXPECT_EQ(b_by_c_, path.certs[1]);
EXPECT_EQ(c_by_d_, path.certs[2]);
EXPECT_EQ(d_by_d_, path.certs[3]);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
}
TEST_F(PathBuilderMultiRootTest, TestIterationLimit) {
// D(D) is the trust root.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
// Certs B(C) and C(D) are supplied.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
sync_certs.AddCert(c_by_d_);
for (const bool insufficient_limit : {true, false}) {
SCOPED_TRACE(insufficient_limit);
StrictMock<MockPathBuilderDelegate> mock_delegate;
// The CheckPathAfterVerification delegate should be called regardless if
// the iteration limit is reached.
EXPECT_CALL(mock_delegate, CheckPathAfterVerification(_, _));
CertPathBuilder path_builder(
a_by_b_, &trust_store, &mock_delegate, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
if (insufficient_limit) {
// A limit of one is insufficient to build a path in this case. Therefore
// building is expected to fail in this case.
path_builder.SetIterationLimit(1);
} else {
// The other tests in this file exercise the case that |SetIterationLimit|
// isn't called. Therefore set a sufficient limit for the path to be
// found.
path_builder.SetIterationLimit(5);
}
auto result = path_builder.Run();
EXPECT_EQ(!insufficient_limit, result.HasValidPath());
EXPECT_EQ(insufficient_limit, result.exceeded_iteration_limit);
VerifyError error = result.GetBestPathVerifyError();
if (insufficient_limit) {
EXPECT_EQ(2U, result.iteration_count);
ASSERT_EQ(error.Code(),
VerifyError::StatusCode::PATH_ITERATION_COUNT_EXCEEDED)
<< error.DiagnosticString();
} else {
EXPECT_EQ(3U, result.iteration_count);
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
}
}
TEST_F(PathBuilderMultiRootTest, TestTrivialDeadline) {
// C(D) is the trust root.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(c_by_d_);
// Cert B(C) is supplied.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
for (const bool insufficient_limit : {true, false}) {
SCOPED_TRACE(insufficient_limit);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
// Make the deadline either expired or not.
delegate_.SetDeadlineExpiredForTesting(insufficient_limit);
auto result = path_builder.Run();
EXPECT_EQ(!insufficient_limit, result.HasValidPath());
EXPECT_EQ(insufficient_limit, result.exceeded_deadline);
EXPECT_EQ(delegate_.IsDeadlineExpired(), insufficient_limit);
if (insufficient_limit) {
ASSERT_EQ(1U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
ASSERT_EQ(1U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_TRUE(result.paths[0]->errors.ContainsError(
cert_errors::kDeadlineExceeded));
} else {
ASSERT_EQ(1U, result.paths.size());
EXPECT_TRUE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_c_, result.paths[0]->certs[1]);
EXPECT_EQ(c_by_d_, result.paths[0]->certs[2]);
}
}
}
TEST_F(PathBuilderMultiRootTest, TestVerifyCache) {
// C(D) is the trust root.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(c_by_d_);
// Cert B(C) is supplied.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
// Test Activation / DeActivation of the cache.
EXPECT_FALSE(delegate_.GetVerifyCache());
delegate_.ActivateCache();
EXPECT_TRUE(delegate_.GetVerifyCache());
delegate_.DeActivateCache();
EXPECT_FALSE(delegate_.GetVerifyCache());
delegate_.ActivateCache();
EXPECT_TRUE(delegate_.GetVerifyCache());
for (size_t i = 0; i < 3; i++) {
SCOPED_TRACE(i);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
auto result = path_builder.Run();
ASSERT_EQ(1U, result.paths.size());
EXPECT_TRUE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_c_, result.paths[0]->certs[1]);
EXPECT_EQ(c_by_d_, result.paths[0]->certs[2]);
// The path is 3 certificates long, so requires 2 distinct signature
// verifications. The first time through the loop will cause 2 cache misses
// and stores, subsequent iterations will repeat the same verifications,
// causing 2 cache hits.
EXPECT_EQ(delegate_.GetMockVerifyCache()->CacheHits(), i * 2);
EXPECT_EQ(delegate_.GetMockVerifyCache()->CacheMisses(), 2U);
EXPECT_EQ(delegate_.GetMockVerifyCache()->CacheStores(), 2U);
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
}
TEST_F(PathBuilderMultiRootTest, TestDeadline) {
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
// Cert B(C) is supplied statically.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
// Cert C(D) is supplied asynchronously and will expire the deadline before
// returning the async result.
AsyncCertIssuerSourceStatic async_certs;
async_certs.AddCert(c_by_d_);
async_certs.SetAsyncGetCallback(
[&] { delegate_.SetDeadlineExpiredForTesting(true); });
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
path_builder.AddCertIssuerSource(&async_certs);
auto result = path_builder.Run();
EXPECT_FALSE(result.HasValidPath());
EXPECT_TRUE(result.exceeded_deadline);
EXPECT_TRUE(delegate_.IsDeadlineExpired());
// The chain returned should end in c_by_d_, since the deadline would only be
// checked again after the async results had been checked (since
// AsyncCertIssuerSourceStatic makes the async results available immediately.)
ASSERT_EQ(1U, result.paths.size());
EXPECT_FALSE(result.paths[0]->IsValid());
ASSERT_EQ(3U, result.paths[0]->certs.size());
EXPECT_EQ(a_by_b_, result.paths[0]->certs[0]);
EXPECT_EQ(b_by_c_, result.paths[0]->certs[1]);
EXPECT_EQ(c_by_d_, result.paths[0]->certs[2]);
EXPECT_TRUE(
result.paths[0]->errors.ContainsError(cert_errors::kDeadlineExceeded));
VerifyError error = result.GetBestPathVerifyError();
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_DEADLINE_EXCEEDED)
<< error.DiagnosticString();
}
TEST_F(PathBuilderMultiRootTest, TestDepthLimit) {
// D(D) is the trust root.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(d_by_d_);
// Certs B(C) and C(D) are supplied.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
sync_certs.AddCert(c_by_d_);
for (const bool insufficient_limit : {true, false}) {
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
if (insufficient_limit) {
// A limit of depth equal to 2 is insufficient to build the path.
// Therefore, building is expected to fail.
path_builder.SetDepthLimit(2);
} else {
// The other tests in this file exercise the case that |SetDepthLimit|
// isn't called. Therefore, set a sufficient limit for the path to be
// found.
path_builder.SetDepthLimit(5);
}
auto result = path_builder.Run();
EXPECT_EQ(!insufficient_limit, result.HasValidPath());
EXPECT_EQ(insufficient_limit,
result.AnyPathContainsError(cert_errors::kDepthLimitExceeded));
VerifyError error = result.GetBestPathVerifyError();
if (insufficient_limit) {
EXPECT_EQ(2U, result.max_depth_seen);
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_DEPTH_LIMIT_REACHED)
<< error.DiagnosticString();
} else {
EXPECT_EQ(4U, result.max_depth_seen);
ASSERT_EQ(error.Code(), VerifyError::StatusCode::PATH_VERIFIED)
<< error.DiagnosticString();
}
}
}
TEST_F(PathBuilderMultiRootTest, TestDepthLimitMultiplePaths) {
// This case tests path building backtracking due to reaching the path depth
// limit. Given the root and issuer certificates below, there can be two paths
// from between the leaf to a trusted root, one has length of 3 and the other
// has length of 4. These certificates are specifically chosen because path
// building will first explore the 4-certificate long path then the
// 3-certificate long path. So with a depth limit of 3, we can test the
// backtracking code path.
// E(E) and C(D) are the trust roots.
TrustStoreInMemory trust_store;
trust_store.AddTrustAnchor(e_by_e_);
trust_store.AddTrustAnchor(c_by_d_);
// Certs B(C). B(F) and F(E) are supplied.
CertIssuerSourceStatic sync_certs;
sync_certs.AddCert(b_by_c_);
sync_certs.AddCert(b_by_f_);
sync_certs.AddCert(f_by_e_);
CertPathBuilder path_builder(
a_by_b_, &trust_store, &delegate_, time_, KeyPurpose::ANY_EKU,
initial_explicit_policy_, user_initial_policy_set_,
initial_policy_mapping_inhibit_, initial_any_policy_inhibit_);
path_builder.AddCertIssuerSource(&sync_certs);
path_builder.SetDepthLimit(3);
auto result = path_builder.Run();
EXPECT_TRUE(result.HasValidPath());
EXPECT_TRUE(result.AnyPathContainsError(cert_errors::kDepthLimitExceeded));
ASSERT_EQ(result.paths.size(), 2u);
const CertPathBuilderResultPath *truncated_path = result.paths[0].get();
EXPECT_FALSE(truncated_path->IsValid());
EXPECT_TRUE(
truncated_path->errors.ContainsError(cert_errors::kDepthLimitExceeded));
ASSERT_EQ(truncated_path->certs.size(), 3u);
EXPECT_EQ(a_by_b_, truncated_path->certs[0]);
EXPECT_EQ(b_by_f_, truncated_path->certs[1]);
EXPECT_EQ(f_by_e_, truncated_path->certs[2]);
const CertPathBuilderResultPath *valid_path = result.paths[1].get();
EXPECT_TRUE(valid_path->IsValid());
EXPECT_FALSE(
valid_path->errors.ContainsError(cert_errors::kDepthLimitExceeded));
ASSERT_EQ(valid_path->certs.size(), 3u);
EXPECT_EQ(a_by_b_, valid_path->certs[0]);