Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 604 int flags = X509Certificate::VERIFY_REV_CHECKING_ENABLED | | 604 int flags = X509Certificate::VERIFY_REV_CHECKING_ENABLED | |
| 605 X509Certificate::VERIFY_EV_CERT; | 605 X509Certificate::VERIFY_EV_CERT; |
| 606 int error = cert_chain->Verify("2029.globalsign.com", flags, NULL, | 606 int error = cert_chain->Verify("2029.globalsign.com", flags, NULL, |
| 607 &verify_result); | 607 &verify_result); |
| 608 if (error == OK) | 608 if (error == OK) |
| 609 EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_IS_EV); | 609 EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| 610 else | 610 else |
| 611 EXPECT_EQ(ERR_CERT_DATE_INVALID, error); | 611 EXPECT_EQ(ERR_CERT_DATE_INVALID, error); |
| 612 } | 612 } |
| 613 | 613 |
| 614 // Currently, only RSA and DSA keys are checked for weakness, and our example | |
| 615 // weak size is 768. These could change in the future. | |
| 616 static bool IsWeakKeyType(const std::string& key_type) { | |
| 617 size_t pos = key_type.find("-"); | |
| 618 std::string size = key_type.substr(0, pos); | |
| 619 std::string type = key_type.substr(pos + 1); | |
| 620 | |
| 621 if (type == "rsa" || type == "dsa") | |
| 622 return size == "768"; | |
| 623 | |
| 624 return false; | |
|
wtc
2011/12/14 00:18:03
Nit: same here -- I think a comment to document th
| |
| 625 } | |
| 626 | |
| 627 TEST(X509CertificateTest, RejectWeakKeys) { | |
| 628 FilePath certs_dir = GetTestCertsDirectory(); | |
| 629 typedef std::vector<std::string> Strings; | |
| 630 Strings key_types; | |
| 631 | |
| 632 // generate-weak-test-chains.sh currently has: | |
| 633 // key_types="768-rsa 1024-rsa 2048-rsa prime256v1-ecdsa" | |
| 634 // We must use the same key types here. The filenames generated look like: | |
| 635 // 2048-rsa-ee-by-768-rsa-intermediate.pem | |
| 636 key_types.push_back("768-rsa"); | |
| 637 key_types.push_back("1024-rsa"); | |
| 638 key_types.push_back("2048-rsa"); | |
| 639 key_types.push_back("prime256v1-ecdsa"); | |
| 640 | |
| 641 // Add the root that signed the intermediates for this test. | |
| 642 scoped_refptr<X509Certificate> root_cert = | |
| 643 ImportCertFromFile(certs_dir, "2048-rsa-root.pem"); | |
| 644 ASSERT_NE(static_cast<X509Certificate*>(NULL), root_cert); | |
| 645 TestRootCerts::GetInstance()->Add(root_cert.get()); | |
|
Ryan Sleevi
2011/12/13 23:54:16
Possible flakiness BUG: Make sure to call TestRoot
| |
| 646 | |
| 647 // Now test each chain. | |
| 648 for (Strings::const_iterator ee_type = key_types.begin(); | |
| 649 ee_type != key_types.end(); ++ee_type) { | |
| 650 for (Strings::const_iterator signer_type = key_types.begin(); | |
| 651 signer_type != key_types.end(); ++signer_type) { | |
| 652 std::string basename = *ee_type + "-ee-by-" + *signer_type + | |
| 653 "-intermediate.pem"; | |
| 654 scoped_refptr<X509Certificate> ee_cert = | |
| 655 ImportCertFromFile(certs_dir, basename); | |
| 656 ASSERT_NE(static_cast<X509Certificate*>(NULL), ee_cert); | |
| 657 | |
| 658 basename = *signer_type + "-intermediate.pem"; | |
| 659 scoped_refptr<X509Certificate> intermediate = | |
| 660 ImportCertFromFile(certs_dir, basename); | |
| 661 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate); | |
| 662 | |
| 663 X509Certificate::OSCertHandles intermediates; | |
| 664 intermediates.push_back(intermediate->os_cert_handle()); | |
| 665 scoped_refptr<X509Certificate> cert_chain = | |
| 666 X509Certificate::CreateFromHandle(ee_cert->os_cert_handle(), | |
| 667 intermediates); | |
| 668 | |
| 669 CertVerifyResult verify_result; | |
| 670 int error = cert_chain->Verify("127.0.0.1", 0, NULL, &verify_result); | |
| 671 | |
| 672 if (IsWeakKeyType(*ee_type) || IsWeakKeyType(*signer_type)) { | |
| 673 EXPECT_NE(OK, error); | |
| 674 EXPECT_EQ(CERT_STATUS_WEAK_KEY, | |
| 675 verify_result.cert_status & CERT_STATUS_WEAK_KEY); | |
| 676 } else { | |
| 677 EXPECT_EQ(OK, error); | |
| 678 EXPECT_EQ(0U, verify_result.cert_status & CERT_STATUS_WEAK_KEY); | |
| 679 } | |
| 680 } | |
| 681 } | |
| 682 } | |
| 683 | |
| 614 // Test for bug 94673. | 684 // Test for bug 94673. |
| 615 TEST(X509CertificateTest, GoogleDigiNotarTest) { | 685 TEST(X509CertificateTest, GoogleDigiNotarTest) { |
| 616 FilePath certs_dir = GetTestCertsDirectory(); | 686 FilePath certs_dir = GetTestCertsDirectory(); |
| 617 | 687 |
| 618 scoped_refptr<X509Certificate> server_cert = | 688 scoped_refptr<X509Certificate> server_cert = |
| 619 ImportCertFromFile(certs_dir, "google_diginotar.pem"); | 689 ImportCertFromFile(certs_dir, "google_diginotar.pem"); |
| 620 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); | 690 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); |
| 621 | 691 |
| 622 scoped_refptr<X509Certificate> intermediate_cert = | 692 scoped_refptr<X509Certificate> intermediate_cert = |
| 623 ImportCertFromFile(certs_dir, "diginotar_public_ca_2025.pem"); | 693 ImportCertFromFile(certs_dir, "diginotar_public_ca_2025.pem"); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 720 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), | 790 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), |
| 721 &derBytes)); | 791 &derBytes)); |
| 722 | 792 |
| 723 base::StringPiece spkiBytes; | 793 base::StringPiece spkiBytes; |
| 724 EXPECT_TRUE(asn1::ExtractSPKIFromDERCert(derBytes, &spkiBytes)); | 794 EXPECT_TRUE(asn1::ExtractSPKIFromDERCert(derBytes, &spkiBytes)); |
| 725 | 795 |
| 726 uint8 hash[base::kSHA1Length]; | 796 uint8 hash[base::kSHA1Length]; |
| 727 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spkiBytes.data()), | 797 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spkiBytes.data()), |
| 728 spkiBytes.size(), hash); | 798 spkiBytes.size(), hash); |
| 729 | 799 |
| 730 EXPECT_TRUE(0 == memcmp(hash, nistSPKIHash, sizeof(hash))); | 800 EXPECT_EQ(0, memcmp(hash, nistSPKIHash, sizeof(hash))); |
| 731 } | 801 } |
| 732 | 802 |
| 733 TEST(X509CertificateTest, ExtractCRLURLsFromDERCert) { | 803 TEST(X509CertificateTest, ExtractCRLURLsFromDERCert) { |
| 734 FilePath certs_dir = GetTestCertsDirectory(); | 804 FilePath certs_dir = GetTestCertsDirectory(); |
| 735 scoped_refptr<X509Certificate> cert = | 805 scoped_refptr<X509Certificate> cert = |
| 736 ImportCertFromFile(certs_dir, "nist.der"); | 806 ImportCertFromFile(certs_dir, "nist.der"); |
| 737 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert); | 807 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert); |
| 738 | 808 |
| 739 std::string derBytes; | 809 std::string derBytes; |
| 740 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), | 810 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), |
| (...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1375 { true, "f", "f" }, | 1445 { true, "f", "f" }, |
| 1376 { false, "h", "i" }, | 1446 { false, "h", "i" }, |
| 1377 { true, "bar.foo.com", "*.foo.com" }, | 1447 { true, "bar.foo.com", "*.foo.com" }, |
| 1378 { true, "www.test.fr", "common.name", | 1448 { true, "www.test.fr", "common.name", |
| 1379 "*.test.com,*.test.co.uk,*.test.de,*.test.fr" }, | 1449 "*.test.com,*.test.co.uk,*.test.de,*.test.fr" }, |
| 1380 { true, "wwW.tESt.fr", "common.name", | 1450 { true, "wwW.tESt.fr", "common.name", |
| 1381 ",*.*,*.test.de,*.test.FR,www" }, | 1451 ",*.*,*.test.de,*.test.FR,www" }, |
| 1382 { false, "f.uk", ".uk" }, | 1452 { false, "f.uk", ".uk" }, |
| 1383 { false, "w.bar.foo.com", "?.bar.foo.com" }, | 1453 { false, "w.bar.foo.com", "?.bar.foo.com" }, |
| 1384 { false, "www.foo.com", "(www|ftp).foo.com" }, | 1454 { false, "www.foo.com", "(www|ftp).foo.com" }, |
| 1385 { false, "www.foo.com", "www.foo.com#" }, // # = null char. | 1455 { false, "www.foo.com", "www.foo.com#" }, // # = null char. |
| 1386 { false, "www.foo.com", "", "www.foo.com#*.foo.com,#,#" }, | 1456 { false, "www.foo.com", "", "www.foo.com#*.foo.com,#,#" }, |
| 1387 { false, "www.house.example", "ww.house.example" }, | 1457 { false, "www.house.example", "ww.house.example" }, |
| 1388 { false, "test.org", "", "www.test.org,*.test.org,*.org" }, | 1458 { false, "test.org", "", "www.test.org,*.test.org,*.org" }, |
| 1389 { false, "w.bar.foo.com", "w*.bar.foo.com" }, | 1459 { false, "w.bar.foo.com", "w*.bar.foo.com" }, |
| 1390 { false, "www.bar.foo.com", "ww*ww.bar.foo.com" }, | 1460 { false, "www.bar.foo.com", "ww*ww.bar.foo.com" }, |
| 1391 { false, "wwww.bar.foo.com", "ww*ww.bar.foo.com" }, | 1461 { false, "wwww.bar.foo.com", "ww*ww.bar.foo.com" }, |
| 1392 { true, "wwww.bar.foo.com", "w*w.bar.foo.com" }, | 1462 { true, "wwww.bar.foo.com", "w*w.bar.foo.com" }, |
| 1393 { false, "wwww.bar.foo.com", "w*w.bar.foo.c0m" }, | 1463 { false, "wwww.bar.foo.com", "w*w.bar.foo.c0m" }, |
| 1394 { true, "WALLY.bar.foo.com", "wa*.bar.foo.com" }, | 1464 { true, "WALLY.bar.foo.com", "wa*.bar.foo.com" }, |
| 1395 { true, "wally.bar.foo.com", "*Ly.bar.foo.com" }, | 1465 { true, "wally.bar.foo.com", "*Ly.bar.foo.com" }, |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1513 } | 1583 } |
| 1514 | 1584 |
| 1515 if (test_data.ip_addrs) { | 1585 if (test_data.ip_addrs) { |
| 1516 // Build up the certificate IP address list. | 1586 // Build up the certificate IP address list. |
| 1517 std::string ip_addrs_line(test_data.ip_addrs); | 1587 std::string ip_addrs_line(test_data.ip_addrs); |
| 1518 std::vector<std::string> ip_addressses_ascii; | 1588 std::vector<std::string> ip_addressses_ascii; |
| 1519 base::SplitString(ip_addrs_line, ',', &ip_addressses_ascii); | 1589 base::SplitString(ip_addrs_line, ',', &ip_addressses_ascii); |
| 1520 for (size_t i = 0; i < ip_addressses_ascii.size(); ++i) { | 1590 for (size_t i = 0; i < ip_addressses_ascii.size(); ++i) { |
| 1521 std::string& addr_ascii = ip_addressses_ascii[i]; | 1591 std::string& addr_ascii = ip_addressses_ascii[i]; |
| 1522 ASSERT_NE(0U, addr_ascii.length()); | 1592 ASSERT_NE(0U, addr_ascii.length()); |
| 1523 if (addr_ascii[0] == 'x') { // Hex encoded address | 1593 if (addr_ascii[0] == 'x') { // Hex encoded address |
| 1524 addr_ascii.erase(0, 1); | 1594 addr_ascii.erase(0, 1); |
| 1525 std::vector<uint8> bytes; | 1595 std::vector<uint8> bytes; |
| 1526 EXPECT_TRUE(base::HexStringToBytes(addr_ascii, &bytes)) | 1596 EXPECT_TRUE(base::HexStringToBytes(addr_ascii, &bytes)) |
| 1527 << "Could not parse hex address " << addr_ascii << " i = " << i; | 1597 << "Could not parse hex address " << addr_ascii << " i = " << i; |
| 1528 ip_addressses.push_back(std::string(reinterpret_cast<char*>(&bytes[0]), | 1598 ip_addressses.push_back(std::string(reinterpret_cast<char*>(&bytes[0]), |
| 1529 bytes.size())); | 1599 bytes.size())); |
| 1530 ASSERT_EQ(16U, ip_addressses.back().size()) << i; | 1600 ASSERT_EQ(16U, ip_addressses.back().size()) << i; |
| 1531 } else { // Decimal groups | 1601 } else { // Decimal groups |
| 1532 std::vector<std::string> decimals_ascii; | 1602 std::vector<std::string> decimals_ascii; |
| 1533 base::SplitString(addr_ascii, '.', &decimals_ascii); | 1603 base::SplitString(addr_ascii, '.', &decimals_ascii); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1751 #define MAYBE_VerifyMixed DISABLED_VerifyMixed | 1821 #define MAYBE_VerifyMixed DISABLED_VerifyMixed |
| 1752 #else | 1822 #else |
| 1753 #define MAYBE_VerifyMixed VerifyMixed | 1823 #define MAYBE_VerifyMixed VerifyMixed |
| 1754 #endif | 1824 #endif |
| 1755 WRAPPED_INSTANTIATE_TEST_CASE_P( | 1825 WRAPPED_INSTANTIATE_TEST_CASE_P( |
| 1756 MAYBE_VerifyMixed, | 1826 MAYBE_VerifyMixed, |
| 1757 X509CertificateWeakDigestTest, | 1827 X509CertificateWeakDigestTest, |
| 1758 testing::ValuesIn(kVerifyMixedTestData)); | 1828 testing::ValuesIn(kVerifyMixedTestData)); |
| 1759 | 1829 |
| 1760 } // namespace net | 1830 } // namespace net |
| OLD | NEW |