| 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/parse_certificate.h" | 5 #include "net/cert/internal/parse_certificate.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "net/cert/internal/cert_errors.h" | 8 #include "net/cert/internal/cert_errors.h" |
| 9 // TODO(eroman): These tests should be moved into | 9 // TODO(eroman): These tests should be moved into |
| 10 // parsed_certificate_unittest.cc; this include dependency should | 10 // parsed_certificate_unittest.cc; this include dependency should |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 TEST(ParseTbsCertificateTest, ValidityGeneralizedTimeAndUTCTime) { | 355 TEST(ParseTbsCertificateTest, ValidityGeneralizedTimeAndUTCTime) { |
| 356 RunTbsCertificateTest("tbs_validity_generalized_time_and_utc_time.pem"); | 356 RunTbsCertificateTest("tbs_validity_generalized_time_and_utc_time.pem"); |
| 357 } | 357 } |
| 358 | 358 |
| 359 // Parses a TBSCertificate whose "validity" field does not strictly follow | 359 // Parses a TBSCertificate whose "validity" field does not strictly follow |
| 360 // the DER rules (and fails to be parsed). | 360 // the DER rules (and fails to be parsed). |
| 361 TEST(ParseTbsCertificateTest, ValidityRelaxed) { | 361 TEST(ParseTbsCertificateTest, ValidityRelaxed) { |
| 362 RunTbsCertificateTest("tbs_validity_relaxed.pem"); | 362 RunTbsCertificateTest("tbs_validity_relaxed.pem"); |
| 363 } | 363 } |
| 364 | 364 |
| 365 // Reads a PEM file containing a block "EXTENSION". This input will be | 365 der::Input DavidBenOid() { |
| 366 // passed to ParseExtension, and the results filled in |out|. | 366 // This OID corresponds with |
| 367 bool ParseExtensionFromFile(const std::string& file_name, | 367 // 1.2.840.113554.4.1.72585.0 (https://davidben.net/oid) |
| 368 ParsedExtension* out, | 368 static const uint8_t kOid[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, |
| 369 std::string* data) { | 369 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00}; |
| 370 const PemBlockMapping mappings[] = { | 370 return der::Input(kOid); |
| 371 {"EXTENSION", data}, | |
| 372 }; | |
| 373 | |
| 374 EXPECT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); | |
| 375 return ParseExtension(der::Input(data), out); | |
| 376 } | 371 } |
| 377 | 372 |
| 378 // Parses an Extension whose critical field is true (255). | 373 // Parses an Extension whose critical field is true (255). |
| 379 TEST(ParseExtensionTest, Critical) { | 374 TEST(ParseCertificateTest, ExtensionCritical) { |
| 380 std::string data; | 375 scoped_refptr<ParsedCertificate> cert = |
| 381 ParsedExtension extension; | 376 ParseCertificateFromFile("extension_critical.pem"); |
| 382 ASSERT_TRUE( | 377 ASSERT_TRUE(cert); |
| 383 ParseExtensionFromFile("extension_critical.pem", &extension, &data)); | 378 |
| 379 const uint8_t kExpectedValue[] = {0x30, 0x00}; |
| 380 |
| 381 auto it = cert->unparsed_extensions().find(DavidBenOid()); |
| 382 ASSERT_NE(cert->unparsed_extensions().end(), it); |
| 383 const auto& extension = it->second; |
| 384 | 384 |
| 385 EXPECT_TRUE(extension.critical); | 385 EXPECT_TRUE(extension.critical); |
| 386 | 386 EXPECT_EQ(DavidBenOid(), extension.oid); |
| 387 const uint8_t kExpectedOid[] = {0x55, 0x1d, 0x13}; | |
| 388 EXPECT_EQ(der::Input(kExpectedOid), extension.oid); | |
| 389 | |
| 390 const uint8_t kExpectedValue[] = {0x30, 0x00}; | |
| 391 EXPECT_EQ(der::Input(kExpectedValue), extension.value); | 387 EXPECT_EQ(der::Input(kExpectedValue), extension.value); |
| 392 } | 388 } |
| 393 | 389 |
| 394 // Parses an Extension whose critical field is false (omitted). | 390 // Parses an Extension whose critical field is false (omitted). |
| 395 TEST(ParseExtensionTest, NotCritical) { | 391 TEST(ParseCertificateTest, ExtensionNotCritical) { |
| 396 std::string data; | 392 scoped_refptr<ParsedCertificate> cert = |
| 397 ParsedExtension extension; | 393 ParseCertificateFromFile("extension_not_critical.pem"); |
| 398 ASSERT_TRUE( | 394 ASSERT_TRUE(cert); |
| 399 ParseExtensionFromFile("extension_not_critical.pem", &extension, &data)); | 395 |
| 396 const uint8_t kExpectedValue[] = {0x30, 0x00}; |
| 397 |
| 398 auto it = cert->unparsed_extensions().find(DavidBenOid()); |
| 399 ASSERT_NE(cert->unparsed_extensions().end(), it); |
| 400 const auto& extension = it->second; |
| 400 | 401 |
| 401 EXPECT_FALSE(extension.critical); | 402 EXPECT_FALSE(extension.critical); |
| 402 | 403 EXPECT_EQ(DavidBenOid(), extension.oid); |
| 403 const uint8_t kExpectedOid[] = {0x55, 0x1d, 0x13}; | |
| 404 EXPECT_EQ(der::Input(kExpectedOid), extension.oid); | |
| 405 | |
| 406 const uint8_t kExpectedValue[] = {0x30, 0x00}; | |
| 407 EXPECT_EQ(der::Input(kExpectedValue), extension.value); | 404 EXPECT_EQ(der::Input(kExpectedValue), extension.value); |
| 408 } | 405 } |
| 409 | 406 |
| 410 // Parses an Extension whose critical field is 0. This is in one sense FALSE, | 407 // Parses an Extension whose critical field is 0. This is in one sense FALSE, |
| 411 // however because critical has DEFAULT of false this is in fact invalid | 408 // however because critical has DEFAULT of false this is in fact invalid |
| 412 // DER-encoding. | 409 // DER-encoding. |
| 413 TEST(ParseExtensionTest, Critical0) { | 410 TEST(ParseCertificateTest, ExtensionCritical0) { |
| 414 std::string data; | 411 ASSERT_FALSE(ParseCertificateFromFile("extension_critical_0.pem")); |
| 415 ParsedExtension extension; | |
| 416 ASSERT_FALSE( | |
| 417 ParseExtensionFromFile("extension_critical_0.pem", &extension, &data)); | |
| 418 } | 412 } |
| 419 | 413 |
| 420 // Parses an Extension whose critical field is 3. Under DER-encoding BOOLEAN | 414 // Parses an Extension whose critical field is 3. Under DER-encoding BOOLEAN |
| 421 // values must an octet of either all zero bits, or all 1 bits, so this is not | 415 // values must an octet of either all zero bits, or all 1 bits, so this is not |
| 422 // valid. | 416 // valid. |
| 423 TEST(ParseExtensionTest, Critical3) { | 417 TEST(ParseCertificateTest, ExtensionCritical3) { |
| 424 std::string data; | 418 ASSERT_FALSE(ParseCertificateFromFile("extension_critical_3.pem")); |
| 425 ParsedExtension extension; | |
| 426 ASSERT_FALSE( | |
| 427 ParseExtensionFromFile("extension_critical_3.pem", &extension, &data)); | |
| 428 } | 419 } |
| 429 | 420 |
| 430 // Runs a test for extensions parsing. The input file is a PEM file which | 421 // Runs a test for extensions parsing. The input file is a PEM file which |
| 431 // contains a DER-encoded Extensions sequence, as well as the expected value | 422 // contains a DER-encoded Extensions sequence, as well as the expected value |
| 432 // for each contained extension. | 423 // for each contained extension. |
| 433 void EnsureParsingExtensionsSucceeds( | 424 void EnsureParsingExtensionsSucceeds( |
| 434 const std::string& file_name, | 425 const std::string& file_name, |
| 435 std::map<der::Input, ParsedExtension>* extensions, | 426 std::map<der::Input, ParsedExtension>* extensions, |
| 436 std::string* data) { | 427 std::string* data) { |
| 437 const PemBlockMapping mappings[] = { | 428 const PemBlockMapping mappings[] = { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 } | 469 } |
| 479 | 470 |
| 480 // Parses an Extensions that contains an unknown critical extension. | 471 // Parses an Extensions that contains an unknown critical extension. |
| 481 TEST(ParseExtensionsTest, UnknownCritical) { | 472 TEST(ParseExtensionsTest, UnknownCritical) { |
| 482 std::string data; | 473 std::string data; |
| 483 std::map<der::Input, ParsedExtension> extensions; | 474 std::map<der::Input, ParsedExtension> extensions; |
| 484 EnsureParsingExtensionsSucceeds("extensions_unknown_critical.pem", | 475 EnsureParsingExtensionsSucceeds("extensions_unknown_critical.pem", |
| 485 &extensions, &data); | 476 &extensions, &data); |
| 486 | 477 |
| 487 ASSERT_EQ(1u, extensions.size()); | 478 ASSERT_EQ(1u, extensions.size()); |
| 488 // This OID corresponds with | 479 auto iter = extensions.find(DavidBenOid()); |
| 489 // 1.2.840.113554.4.1.72585.0 (https://davidben.net/oid) | |
| 490 const uint8_t oid[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, | |
| 491 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00}; | |
| 492 | |
| 493 auto iter = extensions.find(der::Input(oid)); | |
| 494 ASSERT_TRUE(iter != extensions.end()); | 480 ASSERT_TRUE(iter != extensions.end()); |
| 495 EXPECT_TRUE(iter->second.critical); | 481 EXPECT_TRUE(iter->second.critical); |
| 496 EXPECT_EQ(4u, iter->second.value.Length()); | 482 EXPECT_EQ(4u, iter->second.value.Length()); |
| 497 } | 483 } |
| 498 | 484 |
| 499 // Parses an Extensions that contains an unknown non-critical extension. | 485 // Parses an Extensions that contains an unknown non-critical extension. |
| 500 TEST(ParseExtensionsTest, UnknownNonCritical) { | 486 TEST(ParseExtensionsTest, UnknownNonCritical) { |
| 501 std::string data; | 487 std::string data; |
| 502 std::map<der::Input, ParsedExtension> extensions; | 488 std::map<der::Input, ParsedExtension> extensions; |
| 503 EnsureParsingExtensionsSucceeds("extensions_unknown_non_critical.pem", | 489 EnsureParsingExtensionsSucceeds("extensions_unknown_non_critical.pem", |
| 504 &extensions, &data); | 490 &extensions, &data); |
| 505 | 491 |
| 506 ASSERT_EQ(1u, extensions.size()); | 492 ASSERT_EQ(1u, extensions.size()); |
| 507 // This OID corresponds with | 493 auto iter = extensions.find(DavidBenOid()); |
| 508 // 1.2.840.113554.4.1.72585.0 (https://davidben.net/oid) | |
| 509 const uint8_t oid[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, | |
| 510 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00}; | |
| 511 | |
| 512 auto iter = extensions.find(der::Input(oid)); | |
| 513 ASSERT_TRUE(iter != extensions.end()); | 494 ASSERT_TRUE(iter != extensions.end()); |
| 514 EXPECT_FALSE(iter->second.critical); | 495 EXPECT_FALSE(iter->second.critical); |
| 515 EXPECT_EQ(4u, iter->second.value.Length()); | 496 EXPECT_EQ(4u, iter->second.value.Length()); |
| 516 } | 497 } |
| 517 | 498 |
| 518 // Parses an Extensions that contains a basic constraints. | 499 // Parses an Extensions that contains a basic constraints. |
| 519 TEST(ParseExtensionsTest, BasicConstraints) { | 500 TEST(ParseExtensionsTest, BasicConstraints) { |
| 520 std::string data; | 501 std::string data; |
| 521 std::map<der::Input, ParsedExtension> extensions; | 502 std::map<der::Input, ParsedExtension> extensions; |
| 522 EnsureParsingExtensionsSucceeds("extensions_basic_constraints.pem", | 503 EnsureParsingExtensionsSucceeds("extensions_basic_constraints.pem", |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 0x00, // Number of unused bits | 793 0x00, // Number of unused bits |
| 813 }; | 794 }; |
| 814 | 795 |
| 815 der::BitString key_usage; | 796 der::BitString key_usage; |
| 816 ASSERT_FALSE(ParseKeyUsage(der::Input(der), &key_usage)); | 797 ASSERT_FALSE(ParseKeyUsage(der::Input(der), &key_usage)); |
| 817 } | 798 } |
| 818 | 799 |
| 819 } // namespace | 800 } // namespace |
| 820 | 801 |
| 821 } // namespace net | 802 } // namespace net |
| OLD | NEW |