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/test_helpers.h" | 5 #include "net/cert/internal/test_helpers.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/base_paths.h" | 8 #include "base/base_paths.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/strings/string_split.h" | |
11 #include "net/cert/internal/cert_errors.h" | 12 #include "net/cert/internal/cert_errors.h" |
12 #include "net/cert/pem_tokenizer.h" | 13 #include "net/cert/pem_tokenizer.h" |
13 #include "net/der/parser.h" | 14 #include "net/der/parser.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/boringssl/src/include/openssl/pool.h" | 16 #include "third_party/boringssl/src/include/openssl/pool.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
20 namespace { | |
21 | |
22 bool GetValue(base::StringPiece prefix, | |
23 base::StringPiece line, | |
24 std::string* value, | |
25 bool* has_value) { | |
26 if (!line.starts_with(prefix)) | |
27 return false; | |
28 | |
29 if (*has_value) { | |
30 ADD_FAILURE() << "Duplicated " << prefix; | |
31 return false; | |
32 } | |
33 | |
34 *has_value = true; | |
35 *value = line.substr(prefix.size()).as_string(); | |
36 return true; | |
37 } | |
38 | |
39 } // namespace | |
40 | |
19 namespace der { | 41 namespace der { |
20 | 42 |
21 void PrintTo(const Input& data, ::std::ostream* os) { | 43 void PrintTo(const Input& data, ::std::ostream* os) { |
22 std::string b64; | 44 std::string b64; |
23 base::Base64Encode( | 45 base::Base64Encode( |
24 base::StringPiece(reinterpret_cast<const char*>(data.UnsafeData()), | 46 base::StringPiece(reinterpret_cast<const char*>(data.UnsafeData()), |
25 data.Length()), | 47 data.Length()), |
26 &b64); | 48 &b64); |
27 | 49 |
28 *os << "[" << b64 << "]"; | 50 *os << "[" << b64 << "]"; |
(...skipping 20 matching lines...) Expand all Loading... | |
49 const PemBlockMapping* mappings, | 71 const PemBlockMapping* mappings, |
50 size_t mappings_length) { | 72 size_t mappings_length) { |
51 // Compute the full path, relative to the src/ directory. | 73 // Compute the full path, relative to the src/ directory. |
52 base::FilePath src_root; | 74 base::FilePath src_root; |
53 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | 75 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); |
54 base::FilePath filepath = src_root.AppendASCII(file_path_ascii); | 76 base::FilePath filepath = src_root.AppendASCII(file_path_ascii); |
55 | 77 |
56 // Read the full contents of the PEM file. | 78 // Read the full contents of the PEM file. |
57 std::string file_data; | 79 std::string file_data; |
58 if (!base::ReadFileToString(filepath, &file_data)) { | 80 if (!base::ReadFileToString(filepath, &file_data)) { |
59 return ::testing::AssertionFailure() << "Couldn't read file: " | 81 return ::testing::AssertionFailure() |
60 << filepath.value(); | 82 << "Couldn't read file: " << filepath.value(); |
61 } | 83 } |
62 | 84 |
63 // mappings_copy is used to keep track of which mappings have already been | 85 // mappings_copy is used to keep track of which mappings have already been |
64 // satisfied (by nulling the |value| field). This is used to track when | 86 // satisfied (by nulling the |value| field). This is used to track when |
65 // blocks are mulitply defined. | 87 // blocks are mulitply defined. |
66 std::vector<PemBlockMapping> mappings_copy(mappings, | 88 std::vector<PemBlockMapping> mappings_copy(mappings, |
67 mappings + mappings_length); | 89 mappings + mappings_length); |
68 | 90 |
69 // Build the |pem_headers| vector needed for PEMTokenzier. | 91 // Build the |pem_headers| vector needed for PEMTokenzier. |
70 std::vector<std::string> pem_headers; | 92 std::vector<std::string> pem_headers; |
(...skipping 16 matching lines...) Expand all Loading... | |
87 | 109 |
88 // Mark the mapping as having been satisfied. | 110 // Mark the mapping as having been satisfied. |
89 mapping.value = nullptr; | 111 mapping.value = nullptr; |
90 } | 112 } |
91 } | 113 } |
92 } | 114 } |
93 | 115 |
94 // Ensure that all specified blocks were found. | 116 // Ensure that all specified blocks were found. |
95 for (const auto& mapping : mappings_copy) { | 117 for (const auto& mapping : mappings_copy) { |
96 if (mapping.value && !mapping.optional) { | 118 if (mapping.value && !mapping.optional) { |
97 return ::testing::AssertionFailure() << "PEM block missing: " | 119 return ::testing::AssertionFailure() |
98 << mapping.block_name; | 120 << "PEM block missing: " << mapping.block_name; |
99 } | 121 } |
100 } | 122 } |
101 | 123 |
102 return ::testing::AssertionSuccess(); | 124 return ::testing::AssertionSuccess(); |
103 } | 125 } |
104 | 126 |
105 VerifyCertChainTest::VerifyCertChainTest() = default; | 127 VerifyCertChainTest::VerifyCertChainTest() = default; |
106 VerifyCertChainTest::~VerifyCertChainTest() = default; | 128 VerifyCertChainTest::~VerifyCertChainTest() = default; |
107 | 129 |
108 void ReadVerifyCertChainTestFromFile(const std::string& file_path_ascii, | 130 bool ReadCertChainFromFile(const std::string& file_path_ascii, |
131 ParsedCertificateList* chain) { | |
132 // Reset all the out parameters to their defaults. | |
133 *chain = ParsedCertificateList(); | |
134 | |
135 std::string file_data = ReadTestFileToString(file_path_ascii); | |
136 if (file_data.empty()) | |
137 return false; | |
138 | |
139 std::vector<std::string> pem_headers = {"CERTIFICATE"}; | |
140 | |
141 PEMTokenizer pem_tokenizer(file_data, pem_headers); | |
142 while (pem_tokenizer.GetNext()) { | |
143 const std::string& block_data = pem_tokenizer.data(); | |
144 | |
145 CertErrors errors; | |
146 if (!net::ParsedCertificate::CreateAndAddToVector( | |
147 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( | |
148 reinterpret_cast<const uint8_t*>(block_data.data()), | |
149 block_data.size(), nullptr)), | |
150 {}, chain, &errors)) { | |
151 ADD_FAILURE() << errors.ToDebugString(); | |
152 return false; | |
153 } | |
154 } | |
155 | |
156 return true; | |
157 } | |
158 | |
159 bool ReadVerifyCertChainTestFromFile(const std::string& file_path_ascii, | |
109 VerifyCertChainTest* test) { | 160 VerifyCertChainTest* test) { |
110 // Reset all the out parameters to their defaults. | 161 // Reset all the out parameters to their defaults. |
111 *test = {}; | 162 *test = {}; |
112 | 163 |
113 std::string file_data = ReadTestFileToString(file_path_ascii); | 164 std::string file_data = ReadTestFileToString(file_path_ascii); |
165 if (file_data.empty()) | |
166 return false; | |
114 | 167 |
115 std::vector<std::string> pem_headers; | 168 std::vector<std::string> lines = base::SplitString( |
169 file_data, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
116 | 170 |
117 // For details on the file format refer to: | 171 bool has_chain = false; |
118 // net/data/verify_certificate_chain_unittest/README. | 172 bool has_trust = false; |
119 const char kCertificateHeader[] = "CERTIFICATE"; | |
120 const char kTrustAnchorUnconstrained[] = "TRUST_ANCHOR_UNCONSTRAINED"; | |
121 const char kTrustAnchorConstrained[] = "TRUST_ANCHOR_CONSTRAINED"; | |
122 const char kTimeHeader[] = "TIME"; | |
123 const char kResultHeader[] = "VERIFY_RESULT"; | |
124 const char kErrorsHeader[] = "ERRORS"; | |
125 const char kKeyPurpose[] = "KEY_PURPOSE"; | |
126 | |
127 pem_headers.push_back(kCertificateHeader); | |
128 pem_headers.push_back(kTrustAnchorUnconstrained); | |
129 pem_headers.push_back(kTrustAnchorConstrained); | |
130 pem_headers.push_back(kTimeHeader); | |
131 pem_headers.push_back(kResultHeader); | |
132 pem_headers.push_back(kErrorsHeader); | |
133 pem_headers.push_back(kKeyPurpose); | |
134 | |
135 bool has_time = false; | 173 bool has_time = false; |
136 bool has_result = false; | |
137 bool has_errors = false; | 174 bool has_errors = false; |
138 bool has_key_purpose = false; | 175 bool has_key_purpose = false; |
139 bool has_trust_anchor = false; | |
140 | 176 |
141 PEMTokenizer pem_tokenizer(file_data, pem_headers); | 177 for (const std::string& line : lines) { |
142 while (pem_tokenizer.GetNext()) { | 178 base::StringPiece line_piece(line); |
143 const std::string& block_type = pem_tokenizer.block_type(); | |
144 const std::string& block_data = pem_tokenizer.data(); | |
145 | 179 |
146 if (block_type == kCertificateHeader) { | 180 std::string value; |
147 ASSERT_FALSE(has_trust_anchor) << "Trust anchor must appear last"; | |
148 CertErrors errors; | |
149 ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector( | |
150 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( | |
151 reinterpret_cast<const uint8_t*>(block_data.data()), | |
152 block_data.size(), nullptr)), | |
153 {}, &test->chain, &errors)) | |
154 << errors.ToDebugString(); | |
155 } else if (block_type == kTrustAnchorUnconstrained || | |
156 block_type == kTrustAnchorConstrained) { | |
157 ASSERT_FALSE(has_trust_anchor) << "Duplicate trust anchor"; | |
158 CertErrors errors; | |
159 scoped_refptr<ParsedCertificate> root = net::ParsedCertificate::Create( | |
160 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( | |
161 reinterpret_cast<const uint8_t*>(block_data.data()), | |
162 block_data.size(), nullptr)), | |
163 {}, &errors); | |
164 ASSERT_TRUE(root) << errors.ToDebugString(); | |
165 test->chain.push_back(std::move(root)); | |
166 test->last_cert_trust = | |
167 (block_type == kTrustAnchorUnconstrained) | |
168 ? CertificateTrust::ForTrustAnchor() | |
169 : CertificateTrust::ForTrustAnchorEnforcingConstraints(); | |
170 has_trust_anchor = true; | |
171 } else if (block_type == kTimeHeader) { | |
172 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader; | |
173 has_time = true; | |
174 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), &test->time)); | |
175 } else if (block_type == kKeyPurpose) { | |
176 ASSERT_FALSE(has_key_purpose) << "Duplicate " << kKeyPurpose; | |
177 has_key_purpose = true; | |
178 | 181 |
179 if (block_data == "anyExtendedKeyUsage") { | 182 // For details on the file format refer to: |
183 // net/data/verify_certificate_chain_unittest/README. | |
184 if (GetValue("chain: ", line_piece, &value, &has_chain)) { | |
185 std::string chain_path = file_path_ascii; | |
mattm
2017/05/02 06:43:46
initialized value here isn't used.
eroman
2017/05/02 19:20:23
Done.
| |
186 size_t slash = file_path_ascii.rfind('/'); | |
187 if (slash == std::string::npos) { | |
188 ADD_FAILURE() << "Bad path - expecting slashes"; | |
189 return false; | |
190 } | |
191 chain_path = file_path_ascii.substr(0, slash) + "/" + value; | |
192 | |
193 ReadCertChainFromFile(chain_path, &test->chain); | |
194 } else if (GetValue("utc_time: ", line_piece, &value, &has_time)) { | |
195 if (!der::ParseUTCTime(der::Input(&value), &test->time)) { | |
196 ADD_FAILURE() << "Failed parsing UTC time"; | |
197 return false; | |
198 } | |
199 } else if (GetValue("key_purpose: ", line_piece, &value, | |
200 &has_key_purpose)) { | |
201 if (value == "anyExtendedKeyUsage") { | |
180 test->key_purpose = KeyPurpose::ANY_EKU; | 202 test->key_purpose = KeyPurpose::ANY_EKU; |
181 } else if (block_data == "serverAuth") { | 203 } else if (value == "serverAuth") { |
182 test->key_purpose = KeyPurpose::SERVER_AUTH; | 204 test->key_purpose = KeyPurpose::SERVER_AUTH; |
183 } else if (block_data == "clientAuth") { | 205 } else if (value == "clientAuth") { |
184 test->key_purpose = KeyPurpose::CLIENT_AUTH; | 206 test->key_purpose = KeyPurpose::CLIENT_AUTH; |
185 } else { | 207 } else { |
186 ADD_FAILURE() << "Unrecognized " << block_type << ": " << block_data; | 208 ADD_FAILURE() << "Unrecognized key_purpose: " << value; |
209 return false; | |
187 } | 210 } |
188 } else if (block_type == kResultHeader) { | 211 } else if (GetValue("last_cert_trust: ", line_piece, &value, &has_trust)) { |
189 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader; | 212 if (value == "trustAnchor") { |
190 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL") | 213 test->last_cert_trust = CertificateTrust::ForTrustAnchor(); |
191 << "Unrecognized result: " << block_data; | 214 } else if (value == "trustAnchor (enforcesConstraints)") { |
192 has_result = true; | 215 test->last_cert_trust = |
193 test->expected_result = block_data == "SUCCESS"; | 216 CertificateTrust::ForTrustAnchorEnforcingConstraints(); |
194 } else if (block_type == kErrorsHeader) { | 217 } else if (value == "distrusted") { |
195 ASSERT_FALSE(has_errors) << "Duplicate " << kErrorsHeader; | 218 test->last_cert_trust = CertificateTrust::ForDistrusted(); |
196 has_errors = true; | 219 } else if (value == "unspecified") { |
197 test->expected_errors = block_data; | 220 test->last_cert_trust = CertificateTrust::ForUnspecified(); |
221 } else { | |
222 ADD_FAILURE() << "Unrecognized last_cert_trust: " << value; | |
223 return false; | |
224 } | |
225 } else if (GetValue("expected_errors:", line_piece, &value, &has_errors)) { | |
226 // The errors start on the next line, and extend until the end of the | |
227 // file. | |
228 size_t errors_start = file_data.find("\nexpected_errors:\n"); | |
229 if (errors_start == std::string::npos) { | |
230 ADD_FAILURE() << "expected_errors not found"; | |
231 return false; | |
232 } | |
233 test->expected_errors = file_data.substr(errors_start + 18); | |
mattm
2017/05/02 06:43:46
I guess "\nexpected_errors:\n" could be a constant
eroman
2017/05/02 19:20:23
Done.
| |
234 break; | |
198 } | 235 } |
mattm
2017/05/02 06:43:46
should it error on unrecognized lines?
eroman
2017/05/02 19:20:23
Done.
| |
199 } | 236 } |
200 | 237 |
201 ASSERT_TRUE(has_time); | 238 if (!has_chain) { |
202 ASSERT_TRUE(has_result); | 239 ADD_FAILURE() << "Missing chain: "; |
203 ASSERT_TRUE(has_trust_anchor); | 240 return false; |
204 ASSERT_TRUE(has_key_purpose); | 241 } |
242 | |
243 if (!has_trust) { | |
244 ADD_FAILURE() << "Missing last_cert_trust: "; | |
245 return false; | |
246 } | |
247 | |
248 if (!has_time) { | |
249 ADD_FAILURE() << "Missing time: "; | |
250 return false; | |
251 } | |
252 | |
253 if (!has_key_purpose) { | |
254 ADD_FAILURE() << "Missing key_purpose: "; | |
255 return false; | |
256 } | |
257 | |
258 return true; | |
205 } | 259 } |
206 | 260 |
207 std::string ReadTestFileToString(const std::string& file_path_ascii) { | 261 std::string ReadTestFileToString(const std::string& file_path_ascii) { |
208 // Compute the full path, relative to the src/ directory. | 262 // Compute the full path, relative to the src/ directory. |
209 base::FilePath src_root; | 263 base::FilePath src_root; |
210 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | 264 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); |
211 base::FilePath filepath = src_root.AppendASCII(file_path_ascii); | 265 base::FilePath filepath = src_root.AppendASCII(file_path_ascii); |
212 | 266 |
213 // Read the full contents of the file. | 267 // Read the full contents of the file. |
214 std::string file_data; | 268 std::string file_data; |
215 if (!base::ReadFileToString(filepath, &file_data)) { | 269 if (!base::ReadFileToString(filepath, &file_data)) { |
216 ADD_FAILURE() << "Couldn't read file: " << filepath.value(); | 270 ADD_FAILURE() << "Couldn't read file: " << filepath.value(); |
217 return std::string(); | 271 return std::string(); |
218 } | 272 } |
219 | 273 |
220 return file_data; | 274 return file_data; |
221 } | 275 } |
222 | 276 |
223 } // namespace net | 277 } // namespace net |
OLD | NEW |