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