summaryrefslogtreecommitdiff
path: root/chromium/net/cert/pki/path_builder_verify_certificate_chain_unittest.cc
blob: 1db806bb67a598b7059515308496a880297c370e (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/cert/pki/path_builder.h"

#include "net/cert/pki/cert_issuer_source_static.h"
#include "net/cert/pki/simple_path_builder_delegate.h"
#include "net/cert/pki/trust_store_in_memory.h"
#include "net/cert/pki/verify_certificate_chain_typed_unittest.h"

namespace net {

namespace {

class PathBuilderTestDelegate {
 public:
  static void Verify(const VerifyCertChainTest& test,
                     const std::string& test_file_path) {
    SimplePathBuilderDelegate path_builder_delegate(
        1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1);
    ASSERT_FALSE(test.chain.empty());

    TrustStoreInMemory trust_store;

    switch (test.last_cert_trust.type) {
      case CertificateTrustType::TRUSTED_ANCHOR:
        trust_store.AddTrustAnchor(test.chain.back());
        break;
      case CertificateTrustType::TRUSTED_ANCHOR_WITH_EXPIRATION:
        trust_store.AddTrustAnchorWithExpiration(test.chain.back());
        break;
      case CertificateTrustType::TRUSTED_ANCHOR_WITH_CONSTRAINTS:
        trust_store.AddTrustAnchorWithConstraints(test.chain.back());
        break;
      case CertificateTrustType::UNSPECIFIED:
        trust_store.AddCertificateWithUnspecifiedTrust(test.chain.back());
        break;
      case CertificateTrustType::DISTRUSTED:
        trust_store.AddDistrustedCertificateForTest(test.chain.back());
        break;
    }

    CertIssuerSourceStatic intermediate_cert_issuer_source;
    for (size_t i = 1; i < test.chain.size(); ++i)
      intermediate_cert_issuer_source.AddCert(test.chain[i]);

    // First cert in the |chain| is the target.
    CertPathBuilder path_builder(
        test.chain.front(), &trust_store, &path_builder_delegate, test.time,
        test.key_purpose, test.initial_explicit_policy,
        test.user_initial_policy_set, test.initial_policy_mapping_inhibit,
        test.initial_any_policy_inhibit);
    path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source);

    CertPathBuilder::Result result = path_builder.Run();
    EXPECT_EQ(!test.HasHighSeverityErrors(), result.HasValidPath());
  }
};

}  // namespace

INSTANTIATE_TYPED_TEST_SUITE_P(PathBuilder,
                               VerifyCertificateChainSingleRootTest,
                               PathBuilderTestDelegate);

}  // namespace net