OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/verify_certificate_chain.h" | 5 #include "net/cert/internal/verify_certificate_chain.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 return std::string(); | 40 return std::string(); |
41 } | 41 } |
42 | 42 |
43 return file_data; | 43 return file_data; |
44 } | 44 } |
45 | 45 |
46 // Adds the certificate |cert_der| as a trust anchor to |trust_store|. | 46 // Adds the certificate |cert_der| as a trust anchor to |trust_store|. |
47 void AddCertificateToTrustStore(const std::string& cert_der, | 47 void AddCertificateToTrustStore(const std::string& cert_der, |
48 TrustStore* trust_store) { | 48 TrustStore* trust_store) { |
49 ParsedCertificate cert; | 49 ParsedCertificate cert; |
50 ASSERT_TRUE(ParseCertificate(InputFromString(&cert_der), &cert)); | 50 ASSERT_TRUE(ParseCertificate(der::Input(&cert_der), &cert)); |
51 | 51 |
52 ParsedTbsCertificate tbs; | 52 ParsedTbsCertificate tbs; |
53 ASSERT_TRUE(ParseTbsCertificate(cert.tbs_certificate_tlv, &tbs)); | 53 ASSERT_TRUE(ParseTbsCertificate(cert.tbs_certificate_tlv, &tbs)); |
54 TrustAnchor anchor = {tbs.spki_tlv.AsString(), tbs.subject_tlv.AsString()}; | 54 TrustAnchor anchor = {tbs.spki_tlv.AsString(), tbs.subject_tlv.AsString()}; |
55 trust_store->anchors.push_back(anchor); | 55 trust_store->anchors.push_back(anchor); |
56 } | 56 } |
57 | 57 |
58 // Reads a test case from |file_name|. Test cases are comprised of a | 58 // Reads a test case from |file_name|. Test cases are comprised of a |
59 // certificate chain, trust store, a timestamp to validate at, and the | 59 // certificate chain, trust store, a timestamp to validate at, and the |
60 // expected result of verification. | 60 // expected result of verification. |
(...skipping 27 matching lines...) Expand all Loading... |
88 const std::string& block_type = pem_tokenizer.block_type(); | 88 const std::string& block_type = pem_tokenizer.block_type(); |
89 const std::string& block_data = pem_tokenizer.data(); | 89 const std::string& block_data = pem_tokenizer.data(); |
90 | 90 |
91 if (block_type == kCertificateHeader) { | 91 if (block_type == kCertificateHeader) { |
92 chain->push_back(block_data); | 92 chain->push_back(block_data); |
93 } else if (block_type == kTrustedCertificateHeader) { | 93 } else if (block_type == kTrustedCertificateHeader) { |
94 AddCertificateToTrustStore(block_data, trust_store); | 94 AddCertificateToTrustStore(block_data, trust_store); |
95 } else if (block_type == kTimeHeader) { | 95 } else if (block_type == kTimeHeader) { |
96 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader; | 96 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader; |
97 has_time = true; | 97 has_time = true; |
98 ASSERT_TRUE(der::ParseUTCTime(InputFromString(&block_data), time)); | 98 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), time)); |
99 } else if (block_type == kResultHeader) { | 99 } else if (block_type == kResultHeader) { |
100 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader; | 100 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader; |
101 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL") | 101 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL") |
102 << "Unrecognized result: " << block_data; | 102 << "Unrecognized result: " << block_data; |
103 has_result = true; | 103 has_result = true; |
104 *verify_result = block_data == "SUCCESS"; | 104 *verify_result = block_data == "SUCCESS"; |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 ASSERT_TRUE(has_time); | 108 ASSERT_TRUE(has_time); |
109 ASSERT_TRUE(has_result); | 109 ASSERT_TRUE(has_result); |
110 } | 110 } |
111 | 111 |
112 void RunTest(const char* file_name) { | 112 void RunTest(const char* file_name) { |
113 std::vector<std::string> chain; | 113 std::vector<std::string> chain; |
114 TrustStore trust_store; | 114 TrustStore trust_store; |
115 der::GeneralizedTime time; | 115 der::GeneralizedTime time; |
116 bool expected_result; | 116 bool expected_result; |
117 | 117 |
118 ReadTestFromFile(file_name, &chain, &trust_store, &time, &expected_result); | 118 ReadTestFromFile(file_name, &chain, &trust_store, &time, &expected_result); |
119 | 119 |
120 std::vector<der::Input> input_chain; | 120 std::vector<der::Input> input_chain; |
121 for (const auto& cert_str : chain) | 121 for (const auto& cert_str : chain) |
122 input_chain.push_back(InputFromString(&cert_str)); | 122 input_chain.push_back(der::Input(&cert_str)); |
123 | 123 |
124 SimpleSignaturePolicy signature_policy(1024); | 124 SimpleSignaturePolicy signature_policy(1024); |
125 | 125 |
126 bool result = | 126 bool result = |
127 VerifyCertificateChain(input_chain, trust_store, &signature_policy, time); | 127 VerifyCertificateChain(input_chain, trust_store, &signature_policy, time); |
128 | 128 |
129 ASSERT_EQ(expected_result, result); | 129 ASSERT_EQ(expected_result, result); |
130 } | 130 } |
131 | 131 |
132 TEST(VerifyCertificateChainTest, TargetAndIntermediary) { | 132 TEST(VerifyCertificateChainTest, TargetAndIntermediary) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 } | 237 } |
238 | 238 |
239 // TODO(eroman): Add test that invalidate validity dates where the day or month | 239 // TODO(eroman): Add test that invalidate validity dates where the day or month |
240 // ordinal not in range, like "March 39, 2016" are rejected. | 240 // ordinal not in range, like "March 39, 2016" are rejected. |
241 | 241 |
242 } // namespace | 242 } // namespace |
243 | 243 |
244 } // namespace net | 244 } // namespace net |
245 | 245 |
246 #endif | 246 #endif |
OLD | NEW |