| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "test_helpers.h" |
| 16 |
| 17 | #include <fstream> |
| 18 | #include <iostream> |
| 19 | #include <sstream> |
| 20 | #include <streambuf> |
| 21 | #include <string> |
| 22 | #include <string_view> |
| 23 |
| 24 | #include <gtest/gtest.h> |
| 25 |
| 26 | #include <openssl/bytestring.h> |
| 27 | #include <openssl/mem.h> |
| 28 | #include <openssl/pool.h> |
| 29 |
| 30 | #include "../crypto/test/test_data.h" |
| 31 | #include "cert_error_params.h" |
| 32 | #include "cert_errors.h" |
| 33 | #include "parser.h" |
| 34 | #include "pem.h" |
| 35 | #include "simple_path_builder_delegate.h" |
| 36 | #include "string_util.h" |
| 37 | #include "trust_store.h" |
| 38 |
| 39 | BSSL_NAMESPACE_BEGIN |
| 40 |
| 41 | namespace { |
| 42 |
| 43 | bool GetValue(std::string_view prefix, std::string_view line, |
| 44 | std::string *value, bool *has_value) { |
| 45 | if (!bssl::string_util::StartsWith(line, prefix)) { |
| 46 | return false; |
| 47 | } |
| 48 |
| 49 | if (*has_value) { |
| 50 | ADD_FAILURE() << "Duplicated " << prefix; |
| 51 | return false; |
| 52 | } |
| 53 |
| 54 | *has_value = true; |
| 55 | *value = std::string(line.substr(prefix.size())); |
| 56 | return true; |
| 57 | } |
| 58 |
| 59 | // Returns a string containing the dotted numeric form of |oid|, or a |
| 60 | // hex-encoded string on error. |
| 61 | std::string OidToString(der::Input oid) { |
| 62 | CBS cbs; |
| 63 | CBS_init(&cbs, oid.data(), oid.size()); |
| 64 | bssl::UniquePtr<char> text(CBS_asn1_oid_to_text(&cbs)); |
| 65 | if (!text) { |
| 66 | return "invalid:" + bssl::string_util::HexEncode(oid); |
| 67 | } |
| 68 | return text.get(); |
| 69 | } |
| 70 |
| 71 | std::string StrSetToString(const std::set<std::string> &str_set) { |
| 72 | std::string out; |
| 73 | for (const auto &s : str_set) { |
| 74 | EXPECT_FALSE(s.empty()); |
| 75 | if (!out.empty()) { |
| 76 | out += ", "; |
| 77 | } |
| 78 | out += s; |
| 79 | } |
| 80 | return out; |
| 81 | } |
| 82 |
| 83 | std::string StripString(std::string_view str) { |
| 84 | size_t start = str.find_first_not_of(' '); |
| 85 | if (start == str.npos) { |
| 86 | return std::string(); |
| 87 | } |
| 88 | str = str.substr(start); |
| 89 | size_t end = str.find_last_not_of(' '); |
| 90 | if (end != str.npos) { |
| 91 | ++end; |
| 92 | } |
| 93 | return std::string(str.substr(0, end)); |
| 94 | } |
| 95 |
| 96 | std::vector<std::string> SplitString(std::string_view str) { |
| 97 | std::vector<std::string_view> split = string_util::SplitString(str, ','); |
| 98 |
| 99 | std::vector<std::string> out; |
| 100 | for (const auto &s : split) { |
| 101 | out.push_back(StripString(s)); |
| 102 | } |
| 103 | return out; |
| 104 | } |
| 105 |
| 106 | } // namespace |
| 107 |
| 108 | namespace der { |
| 109 |
| 110 | void PrintTo(Input data, ::std::ostream *os) { |
| 111 | size_t len; |
| 112 | if (!EVP_EncodedLength(&len, data.size())) { |
| 113 | *os << "[]"; |
| 114 | return; |
| 115 | } |
| 116 | std::vector<uint8_t> encoded(len); |
| 117 | len = EVP_EncodeBlock(encoded.data(), data.data(), data.size()); |
| 118 | // Skip the trailing \0. |
| 119 | std::string b64_encoded(encoded.begin(), encoded.begin() + len); |
| 120 | *os << "[" << b64_encoded << "]"; |
| 121 | } |
| 122 |
| 123 | } // namespace der |
| 124 |
| 125 | der::Input SequenceValueFromString(std::string_view s) { |
| 126 | der::Parser parser((der::Input(s))); |
| 127 | der::Input data; |
| 128 | if (!parser.ReadTag(CBS_ASN1_SEQUENCE, &data)) { |
| 129 | ADD_FAILURE(); |
| 130 | return der::Input(); |
| 131 | } |
| 132 | if (parser.HasMore()) { |
| 133 | ADD_FAILURE(); |
| 134 | return der::Input(); |
| 135 | } |
| 136 | return data; |
| 137 | } |
| 138 |
| 139 | ::testing::AssertionResult ReadTestDataFromPemFile( |
| 140 | const std::string &file_path_ascii, const PemBlockMapping *mappings, |
| 141 | size_t mappings_length) { |
| 142 | std::string file_data = ReadTestFileToString(file_path_ascii); |
| 143 |
| 144 | // mappings_copy is used to keep track of which mappings have already been |
| 145 | // satisfied (by nulling the |value| field). This is used to track when |
| 146 | // blocks are mulitply defined. |
| 147 | std::vector<PemBlockMapping> mappings_copy(mappings, |
| 148 | mappings + mappings_length); |
| 149 |
| 150 | // Build the |pem_headers| vector needed for PEMTokenzier. |
| 151 | std::vector<std::string> pem_headers; |
| 152 | for (const auto &mapping : mappings_copy) { |
| 153 | pem_headers.push_back(mapping.block_name); |
| 154 | } |
| 155 |
| 156 | PEMTokenizer pem_tokenizer(file_data, pem_headers); |
| 157 | while (pem_tokenizer.GetNext()) { |
| 158 | for (auto &mapping : mappings_copy) { |
| 159 | // Find the mapping for this block type. |
| 160 | if (pem_tokenizer.block_type() == mapping.block_name) { |
| 161 | if (!mapping.value) { |
| 162 | return ::testing::AssertionFailure() |
| 163 | << "PEM block defined multiple times: " << mapping.block_name; |
| 164 | } |
| 165 |
| 166 | // Copy the data to the result. |
| 167 | mapping.value->assign(pem_tokenizer.data()); |
| 168 |
| 169 | // Mark the mapping as having been satisfied. |
| 170 | mapping.value = nullptr; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 |
| 175 | // Ensure that all specified blocks were found. |
| 176 | for (const auto &mapping : mappings_copy) { |
| 177 | if (mapping.value && !mapping.optional) { |
| 178 | return ::testing::AssertionFailure() |
| 179 | << "PEM block missing: " << mapping.block_name; |
| 180 | } |
| 181 | } |
| 182 |
| 183 | return ::testing::AssertionSuccess(); |
| 184 | } |
| 185 |
| 186 | VerifyCertChainTest::VerifyCertChainTest() |
| 187 | : user_initial_policy_set{der::Input(kAnyPolicyOid)} {} |
| 188 | VerifyCertChainTest::~VerifyCertChainTest() = default; |
| 189 |
| 190 | bool VerifyCertChainTest::HasHighSeverityErrors() const { |
| 191 | // This function assumes that high severity warnings are prefixed with |
| 192 | // "ERROR: " and warnings are prefixed with "WARNING: ". This is an |
| 193 | // implementation detail of CertError::ToDebugString). |
| 194 | // |
| 195 | // Do a quick sanity-check to confirm this. |
| 196 | CertError error(CertError::SEVERITY_HIGH, "unused", nullptr); |
| 197 | EXPECT_EQ("ERROR: unused\n", error.ToDebugString()); |
| 198 | CertError warning(CertError::SEVERITY_WARNING, "unused", nullptr); |
| 199 | EXPECT_EQ("WARNING: unused\n", warning.ToDebugString()); |
| 200 |
| 201 | // Do a simple substring test (not perfect, but good enough for our test |
| 202 | // corpus). |
| 203 | return expected_errors.find("ERROR: ") != std::string::npos; |
| 204 | } |
| 205 |
| 206 | bool ReadCertChainFromFile(const std::string &file_path_ascii, |
| 207 | ParsedCertificateList *chain) { |
| 208 | // Reset all the out parameters to their defaults. |
| 209 | *chain = ParsedCertificateList(); |
| 210 |
| 211 | std::string file_data = ReadTestFileToString(file_path_ascii); |
| 212 | if (file_data.empty()) { |
| 213 | return false; |
| 214 | } |
| 215 |
| 216 | std::vector<std::string> pem_headers = {"CERTIFICATE"}; |
| 217 |
| 218 | PEMTokenizer pem_tokenizer(file_data, pem_headers); |
| 219 | while (pem_tokenizer.GetNext()) { |
| 220 | const std::string &block_data = pem_tokenizer.data(); |
| 221 |
| 222 | CertErrors errors; |
| 223 | if (!ParsedCertificate::CreateAndAddToVector( |
| 224 | bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( |
| 225 | reinterpret_cast<const uint8_t *>(block_data.data()), |
| 226 | block_data.size(), nullptr)), |
| 227 | {}, chain, &errors)) { |
| 228 | ADD_FAILURE() << errors.ToDebugString(); |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 |
| 233 | return true; |
| 234 | } |
| 235 |
| 236 | std::shared_ptr<const ParsedCertificate> ReadCertFromFile( |
| 237 | const std::string &file_path_ascii) { |
| 238 | ParsedCertificateList chain; |
| 239 | if (!ReadCertChainFromFile(file_path_ascii, &chain)) { |
| 240 | return nullptr; |
| 241 | } |
| 242 | if (chain.size() != 1) { |
| 243 | return nullptr; |
| 244 | } |
| 245 | return chain[0]; |
| 246 | } |
| 247 |
| 248 | bool ReadVerifyCertChainTestFromFile(const std::string &file_path_ascii, |
| 249 | VerifyCertChainTest *test) { |
| 250 | // Reset all the out parameters to their defaults. |
| 251 | *test = {}; |
| 252 |
| 253 | std::string file_data = ReadTestFileToString(file_path_ascii); |
| 254 | if (file_data.empty()) { |
| 255 | return false; |
| 256 | } |
| 257 |
| 258 | bool has_chain = false; |
| 259 | bool has_trust = false; |
| 260 | bool has_time = false; |
| 261 | bool has_errors = false; |
| 262 | bool has_key_purpose = false; |
| 263 | bool has_digest_policy = false; |
| 264 | bool has_user_constrained_policy_set = false; |
| 265 |
| 266 | std::string kExpectedErrors = "expected_errors:"; |
| 267 |
| 268 | std::istringstream stream(file_data); |
| 269 | for (std::string line; std::getline(stream, line, '\n');) { |
| 270 | size_t start = line.find_first_not_of(" \n\t\r\f\v"); |
| 271 | if (start == std::string::npos) { |
| 272 | continue; |
| 273 | } |
| 274 | size_t end = line.find_last_not_of(" \n\t\r\f\v"); |
| 275 | if (end == std::string::npos) { |
| 276 | continue; |
| 277 | } |
| 278 | line = line.substr(start, end + 1); |
| 279 | if (line.empty()) { |
| 280 | continue; |
| 281 | } |
| 282 | std::string_view line_piece(line); |
| 283 |
| 284 | std::string value; |
| 285 |
| 286 | // For details on the file format refer to: |
| 287 | // net/data/verify_certificate_chain_unittest/README. |
| 288 | if (GetValue("chain: ", line_piece, &value, &has_chain)) { |
| 289 | // Interpret the |chain| path as being relative to the .test file. |
| 290 | size_t slash = file_path_ascii.rfind('/'); |
| 291 | if (slash == std::string::npos) { |
| 292 | ADD_FAILURE() << "Bad path - expecting slashes"; |
| 293 | return false; |
| 294 | } |
| 295 | std::string chain_path = file_path_ascii.substr(0, slash) + "/" + value; |
| 296 |
| 297 | ReadCertChainFromFile(chain_path, &test->chain); |
| 298 | } else if (GetValue("utc_time: ", line_piece, &value, &has_time)) { |
| 299 | if (value == "DEFAULT") { |
| 300 | value = "211005120000Z"; |
| 301 | } |
| 302 | if (!der::ParseUTCTime(der::Input(value), &test->time)) { |
| 303 | ADD_FAILURE() << "Failed parsing UTC time"; |
| 304 | return false; |
| 305 | } |
| 306 | } else if (GetValue("key_purpose: ", line_piece, &value, |
| 307 | &has_key_purpose)) { |
| 308 | if (value == "ANY_EKU") { |
| 309 | test->key_purpose = KeyPurpose::ANY_EKU; |
| 310 | } else if (value == "SERVER_AUTH") { |
| 311 | test->key_purpose = KeyPurpose::SERVER_AUTH; |
| 312 | } else if (value == "CLIENT_AUTH") { |
| 313 | test->key_purpose = KeyPurpose::CLIENT_AUTH; |
| 314 | } else if (value == "SERVER_AUTH_STRICT") { |
| 315 | test->key_purpose = KeyPurpose::SERVER_AUTH_STRICT; |
| 316 | } else if (value == "CLIENT_AUTH_STRICT") { |
| 317 | test->key_purpose = KeyPurpose::CLIENT_AUTH_STRICT; |
| 318 | } else if (value == "SERVER_AUTH_STRICT_LEAF") { |
| 319 | test->key_purpose = KeyPurpose::SERVER_AUTH_STRICT_LEAF; |
| 320 | } else if (value == "CLIENT_AUTH_STRICT_LEAF") { |
| 321 | test->key_purpose = KeyPurpose::CLIENT_AUTH_STRICT_LEAF; |
| 322 | } else if (value == "MLS_CLIENT_AUTH") { |
| 323 | test->key_purpose = KeyPurpose::RCS_MLS_CLIENT_AUTH; |
| 324 | } else { |
| 325 | ADD_FAILURE() << "Unrecognized key_purpose: " << value; |
| 326 | return false; |
| 327 | } |
| 328 | } else if (GetValue("last_cert_trust: ", line_piece, &value, &has_trust)) { |
| 329 | // TODO(mattm): convert test files to use |
| 330 | // CertificateTrust::FromDebugString strings. |
| 331 | if (value == "TRUSTED_ANCHOR") { |
| 332 | test->last_cert_trust = CertificateTrust::ForTrustAnchor(); |
| 333 | } else if (value == "TRUSTED_ANCHOR_WITH_EXPIRATION") { |
| 334 | test->last_cert_trust = |
| 335 | CertificateTrust::ForTrustAnchor().WithEnforceAnchorExpiry(); |
| 336 | } else if (value == "TRUSTED_ANCHOR_WITH_CONSTRAINTS") { |
| 337 | test->last_cert_trust = |
| 338 | CertificateTrust::ForTrustAnchor().WithEnforceAnchorConstraints(); |
| 339 | } else if (value == "TRUSTED_ANCHOR_WITH_REQUIRE_BASIC_CONSTRAINTS") { |
| 340 | test->last_cert_trust = CertificateTrust::ForTrustAnchor() |
| 341 | .WithRequireAnchorBasicConstraints(); |
| 342 | } else if (value == |
| 343 | "TRUSTED_ANCHOR_WITH_CONSTRAINTS_REQUIRE_BASIC_CONSTRAINTS") { |
| 344 | test->last_cert_trust = CertificateTrust::ForTrustAnchor() |
| 345 | .WithEnforceAnchorConstraints() |
| 346 | .WithRequireAnchorBasicConstraints(); |
| 347 | } else if (value == "TRUSTED_ANCHOR_WITH_EXPIRATION_AND_CONSTRAINTS") { |
| 348 | test->last_cert_trust = CertificateTrust::ForTrustAnchor() |
| 349 | .WithEnforceAnchorExpiry() |
| 350 | .WithEnforceAnchorConstraints(); |
| 351 | } else if (value == "TRUSTED_ANCHOR_OR_LEAF") { |
| 352 | test->last_cert_trust = CertificateTrust::ForTrustAnchorOrLeaf(); |
| 353 | } else if (value == "TRUSTED_LEAF") { |
| 354 | test->last_cert_trust = CertificateTrust::ForTrustedLeaf(); |
| 355 | } else if (value == "TRUSTED_LEAF_REQUIRE_SELF_SIGNED") { |
| 356 | test->last_cert_trust = |
| 357 | CertificateTrust::ForTrustedLeaf().WithRequireLeafSelfSigned(); |
| 358 | } else if (value == "DISTRUSTED") { |
| 359 | test->last_cert_trust = CertificateTrust::ForDistrusted(); |
| 360 | } else if (value == "UNSPECIFIED") { |
| 361 | test->last_cert_trust = CertificateTrust::ForUnspecified(); |
| 362 | } else { |
| 363 | ADD_FAILURE() << "Unrecognized last_cert_trust: " << value; |
| 364 | return false; |
| 365 | } |
| 366 | } else if (GetValue("digest_policy: ", line_piece, &value, |
| 367 | &has_digest_policy)) { |
| 368 | if (value == "STRONG") { |
| 369 | test->digest_policy = SimplePathBuilderDelegate::DigestPolicy::kStrong; |
| 370 | } else if (value == "ALLOW_SHA_1") { |
| 371 | test->digest_policy = |
| 372 | SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1; |
| 373 | } else { |
| 374 | ADD_FAILURE() << "Unrecognized digest_policy: " << value; |
| 375 | return false; |
| 376 | } |
| 377 | } else if (GetValue("expected_user_constrained_policy_set: ", line_piece, |
| 378 | &value, &has_user_constrained_policy_set)) { |
| 379 | std::vector<std::string> split_value(SplitString(value)); |
| 380 | test->expected_user_constrained_policy_set = |
| 381 | std::set<std::string>(split_value.begin(), split_value.end()); |
| 382 | } else if (bssl::string_util::StartsWith(line_piece, "#")) { |
| 383 | // Skip comments. |
| 384 | continue; |
| 385 | } else if (line_piece == kExpectedErrors) { |
| 386 | has_errors = true; |
| 387 | // The errors start on the next line, and extend until the end of the |
| 388 | // file. |
| 389 | std::string prefix = |
| 390 | std::string("\n") + kExpectedErrors + std::string("\n"); |
| 391 | size_t errors_start = file_data.find(prefix); |
| 392 | if (errors_start == std::string::npos) { |
| 393 | ADD_FAILURE() << "expected_errors not found"; |
| 394 | return false; |
| 395 | } |
| 396 | test->expected_errors = file_data.substr(errors_start + prefix.size()); |
| 397 | break; |
| 398 | } else { |
| 399 | ADD_FAILURE() << "Unknown line: " << line_piece; |
| 400 | return false; |
| 401 | } |
| 402 | } |
| 403 |
| 404 | if (!has_chain) { |
| 405 | ADD_FAILURE() << "Missing chain: "; |
| 406 | return false; |
| 407 | } |
| 408 |
| 409 | if (!has_trust) { |
| 410 | ADD_FAILURE() << "Missing last_cert_trust: "; |
| 411 | return false; |
| 412 | } |
| 413 |
| 414 | if (!has_time) { |
| 415 | ADD_FAILURE() << "Missing time: "; |
| 416 | return false; |
| 417 | } |
| 418 |
| 419 | if (!has_key_purpose) { |
| 420 | ADD_FAILURE() << "Missing key_purpose: "; |
| 421 | return false; |
| 422 | } |
| 423 |
| 424 | if (!has_errors) { |
| 425 | ADD_FAILURE() << "Missing errors:"; |
| 426 | return false; |
| 427 | } |
| 428 |
| 429 | // `has_user_constrained_policy_set` is intentionally not checked here. Not |
| 430 | // specifying expected_user_constrained_policy_set means the expected policy |
| 431 | // set is empty. |
| 432 |
| 433 | return true; |
| 434 | } |
| 435 |
| 436 | std::string ReadTestFileToString(const std::string &file_path_ascii) { |
| 437 | return GetTestData(("pki/" + file_path_ascii).c_str()); |
| 438 | } |
| 439 |
| 440 | void VerifyCertPathErrors(const std::string &expected_errors_str, |
| 441 | const CertPathErrors &actual_errors, |
| 442 | const ParsedCertificateList &chain, |
| 443 | const std::string &errors_file_path) { |
| 444 | std::string actual_errors_str = actual_errors.ToDebugString(chain); |
| 445 |
| 446 | if (expected_errors_str != actual_errors_str) { |
| 447 | ADD_FAILURE() << "Cert path errors don't match expectations (" |
| 448 | << errors_file_path << ")\n\n" |
| 449 | << "EXPECTED:\n\n" |
| 450 | << expected_errors_str << "\n" |
| 451 | << "ACTUAL:\n\n" |
| 452 | << actual_errors_str << "\n" |
| 453 | << "===> Use " |
| 454 | "pki/testdata/verify_certificate_chain_unittest/" |
| 455 | "rebase-errors.py to rebaseline.\n"; |
| 456 | } |
| 457 | } |
| 458 |
| 459 | void VerifyCertErrors(const std::string &expected_errors_str, |
| 460 | const CertErrors &actual_errors, |
| 461 | const std::string &errors_file_path) { |
| 462 | std::string actual_errors_str = actual_errors.ToDebugString(); |
| 463 |
| 464 | if (expected_errors_str != actual_errors_str) { |
| 465 | ADD_FAILURE() << "Cert errors don't match expectations (" |
| 466 | << errors_file_path << ")\n\n" |
| 467 | << "EXPECTED:\n\n" |
| 468 | << expected_errors_str << "\n" |
| 469 | << "ACTUAL:\n\n" |
| 470 | << actual_errors_str << "\n" |
| 471 | << "===> Use " |
| 472 | "pki/testdata/parse_certificate_unittest/" |
| 473 | "rebase-errors.py to rebaseline.\n"; |
| 474 | } |
| 475 | } |
| 476 |
| 477 | void VerifyUserConstrainedPolicySet( |
| 478 | const std::set<std::string> &expected_user_constrained_policy_str_set, |
| 479 | const std::set<der::Input> &actual_user_constrained_policy_set, |
| 480 | const std::string &errors_file_path) { |
| 481 | std::set<std::string> actual_user_constrained_policy_str_set; |
| 482 | for (der::Input der_oid : actual_user_constrained_policy_set) { |
| 483 | actual_user_constrained_policy_str_set.insert(OidToString(der_oid)); |
| 484 | } |
| 485 | if (expected_user_constrained_policy_str_set != |
| 486 | actual_user_constrained_policy_str_set) { |
| 487 | ADD_FAILURE() << "user_constrained_policy_set doesn't match expectations (" |
| 488 | << errors_file_path << ")\n\n" |
| 489 | << "EXPECTED: " |
| 490 | << StrSetToString(expected_user_constrained_policy_str_set) |
| 491 | << "\n" |
| 492 | << "ACTUAL: " |
| 493 | << StrSetToString(actual_user_constrained_policy_str_set) |
| 494 | << "\n"; |
| 495 | } |
| 496 | } |
| 497 |
| 498 | BSSL_NAMESPACE_END |
| 499 |