Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: net/base/x509_certificate_unittest.cc

Issue 8568040: Refuse to accept certificate chains containing any RSA public key smaller (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #if defined(USE_NSS)
6 #include <cert.h>
7 #endif
Ryan Sleevi 2011/11/16 23:40:54 This was in the correct place originally. Please m
8
5 #include "base/file_path.h" 9 #include "base/file_path.h"
6 #include "base/file_util.h" 10 #include "base/file_util.h"
7 #include "base/path_service.h" 11 #include "base/path_service.h"
8 #include "base/pickle.h" 12 #include "base/pickle.h"
9 #include "base/sha1.h" 13 #include "base/sha1.h"
10 #include "base/string_number_conversions.h" 14 #include "base/string_number_conversions.h"
11 #include "base/string_split.h" 15 #include "base/string_split.h"
12 #include "crypto/rsa_private_key.h" 16 #include "crypto/rsa_private_key.h"
13 #include "net/base/asn1_util.h" 17 #include "net/base/asn1_util.h"
14 #include "net/base/cert_status_flags.h" 18 #include "net/base/cert_status_flags.h"
15 #include "net/base/cert_test_util.h" 19 #include "net/base/cert_test_util.h"
16 #include "net/base/cert_verify_result.h" 20 #include "net/base/cert_verify_result.h"
17 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
18 #include "net/base/test_certificate_data.h" 22 #include "net/base/test_certificate_data.h"
19 #include "net/base/test_root_certs.h" 23 #include "net/base/test_root_certs.h"
20 #include "net/base/x509_certificate.h" 24 #include "net/base/x509_certificate.h"
21 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
22 26
23 #if defined(USE_NSS)
24 #include <cert.h>
25 #endif
26
27 // Unit tests aren't allowed to access external resources. Unfortunately, to 27 // Unit tests aren't allowed to access external resources. Unfortunately, to
28 // properly verify the EV-ness of a cert, we need to check for its revocation 28 // properly verify the EV-ness of a cert, we need to check for its revocation
29 // through online servers. If you're manually running unit tests, feel free to 29 // through online servers. If you're manually running unit tests, feel free to
30 // turn this on to test EV certs. But leave it turned off for the automated 30 // turn this on to test EV certs. But leave it turned off for the automated
31 // testing. 31 // testing.
32 #define ALLOW_EXTERNAL_ACCESS 0 32 #define ALLOW_EXTERNAL_ACCESS 0
33 33
34 #if ALLOW_EXTERNAL_ACCESS && defined(OS_WIN) 34 #if ALLOW_EXTERNAL_ACCESS && defined(OS_WIN)
35 #define TEST_EV 1 // Test CERT_STATUS_IS_EV 35 #define TEST_EV 1 // Test CERT_STATUS_IS_EV
36 #endif 36 #endif
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 &verify_result); 585 &verify_result);
586 EXPECT_NE(OK, error); 586 EXPECT_NE(OK, error);
587 587
588 // Now turn off revocation checking. Certificate verification should still 588 // Now turn off revocation checking. Certificate verification should still
589 // fail. 589 // fail.
590 flags = 0; 590 flags = 0;
591 error = cert_chain->Verify("mail.google.com", flags, NULL, &verify_result); 591 error = cert_chain->Verify("mail.google.com", flags, NULL, &verify_result);
592 EXPECT_NE(OK, error); 592 EXPECT_NE(OK, error);
593 } 593 }
594 594
595 TEST(X509CertificateTest, RejectWeakKeys) {
596 FilePath certs_dir = GetTestCertsDirectory();
597
598 // Self-signed cert with weak (768-bit) key.
599 scoped_refptr<X509Certificate> weak_cert =
600 ImportCertFromFile(certs_dir, "weak-key.pem");
601 ASSERT_NE(static_cast<X509Certificate*>(NULL), weak_cert);
602
603 CertVerifyResult verify_result;
604 int flags = 0;
605 int error = weak_cert->Verify("broken.example.com", flags, NULL,
606 &verify_result);
607 EXPECT_NE(OK, error);
608 EXPECT_EQ(CERT_STATUS_WEAK_KEY, verify_result.cert_status);
609
610 // EE has 2048-bit key, signer is weak_cert. Even though the EE is fine,
611 // we must still reject it.
612 scoped_refptr<X509Certificate> good_cert =
613 ImportCertFromFile(certs_dir, "strong-key-weak-signer.pem");
614 ASSERT_NE(static_cast<X509Certificate*>(NULL), good_cert);
615
616 X509Certificate::OSCertHandles intermediates;
617 intermediates.push_back(weak_cert->os_cert_handle());
618 scoped_refptr<X509Certificate> cert_chain =
619 X509Certificate::CreateFromHandle(good_cert->os_cert_handle(),
620 intermediates);
621
622 error = cert_chain->Verify("www.example.org", flags, NULL, &verify_result);
623 EXPECT_NE(OK, error);
624 EXPECT_EQ(CERT_STATUS_WEAK_KEY, verify_result.cert_status);
625 }
626
595 TEST(X509CertificateTest, DigiNotarCerts) { 627 TEST(X509CertificateTest, DigiNotarCerts) {
596 static const char* const kDigiNotarFilenames[] = { 628 static const char* const kDigiNotarFilenames[] = {
597 "diginotar_root_ca.pem", 629 "diginotar_root_ca.pem",
598 "diginotar_cyber_ca.pem", 630 "diginotar_cyber_ca.pem",
599 "diginotar_services_1024_ca.pem", 631 "diginotar_services_1024_ca.pem",
600 "diginotar_pkioverheid.pem", 632 "diginotar_pkioverheid.pem",
601 "diginotar_pkioverheid_g2.pem", 633 "diginotar_pkioverheid_g2.pem",
602 NULL, 634 NULL,
603 }; 635 };
604 636
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), 702 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(),
671 &derBytes)); 703 &derBytes));
672 704
673 base::StringPiece spkiBytes; 705 base::StringPiece spkiBytes;
674 EXPECT_TRUE(asn1::ExtractSPKIFromDERCert(derBytes, &spkiBytes)); 706 EXPECT_TRUE(asn1::ExtractSPKIFromDERCert(derBytes, &spkiBytes));
675 707
676 uint8 hash[base::kSHA1Length]; 708 uint8 hash[base::kSHA1Length];
677 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spkiBytes.data()), 709 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spkiBytes.data()),
678 spkiBytes.size(), hash); 710 spkiBytes.size(), hash);
679 711
680 EXPECT_TRUE(0 == memcmp(hash, nistSPKIHash, sizeof(hash))); 712 EXPECT_EQ(0, memcmp(hash, nistSPKIHash, sizeof(hash)));
681 } 713 }
682 714
683 TEST(X509CertificateTest, ExtractCRLURLsFromDERCert) { 715 TEST(X509CertificateTest, ExtractCRLURLsFromDERCert) {
684 FilePath certs_dir = GetTestCertsDirectory(); 716 FilePath certs_dir = GetTestCertsDirectory();
685 scoped_refptr<X509Certificate> cert = 717 scoped_refptr<X509Certificate> cert =
686 ImportCertFromFile(certs_dir, "nist.der"); 718 ImportCertFromFile(certs_dir, "nist.der");
687 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert); 719 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert);
688 720
689 std::string derBytes; 721 std::string derBytes;
690 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(), 722 EXPECT_TRUE(X509Certificate::GetDEREncoded(cert->os_cert_handle(),
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after
1325 { true, "f", "f" }, 1357 { true, "f", "f" },
1326 { false, "h", "i" }, 1358 { false, "h", "i" },
1327 { true, "bar.foo.com", "*.foo.com" }, 1359 { true, "bar.foo.com", "*.foo.com" },
1328 { true, "www.test.fr", "common.name", 1360 { true, "www.test.fr", "common.name",
1329 "*.test.com,*.test.co.uk,*.test.de,*.test.fr" }, 1361 "*.test.com,*.test.co.uk,*.test.de,*.test.fr" },
1330 { true, "wwW.tESt.fr", "common.name", 1362 { true, "wwW.tESt.fr", "common.name",
1331 ",*.*,*.test.de,*.test.FR,www" }, 1363 ",*.*,*.test.de,*.test.FR,www" },
1332 { false, "f.uk", ".uk" }, 1364 { false, "f.uk", ".uk" },
1333 { false, "w.bar.foo.com", "?.bar.foo.com" }, 1365 { false, "w.bar.foo.com", "?.bar.foo.com" },
1334 { false, "www.foo.com", "(www|ftp).foo.com" }, 1366 { false, "www.foo.com", "(www|ftp).foo.com" },
1335 { false, "www.foo.com", "www.foo.com#" }, // # = null char. 1367 { false, "www.foo.com", "www.foo.com#" }, // # = null char.
1336 { false, "www.foo.com", "", "www.foo.com#*.foo.com,#,#" }, 1368 { false, "www.foo.com", "", "www.foo.com#*.foo.com,#,#" },
1337 { false, "www.house.example", "ww.house.example" }, 1369 { false, "www.house.example", "ww.house.example" },
1338 { false, "test.org", "", "www.test.org,*.test.org,*.org" }, 1370 { false, "test.org", "", "www.test.org,*.test.org,*.org" },
1339 { false, "w.bar.foo.com", "w*.bar.foo.com" }, 1371 { false, "w.bar.foo.com", "w*.bar.foo.com" },
1340 { false, "www.bar.foo.com", "ww*ww.bar.foo.com" }, 1372 { false, "www.bar.foo.com", "ww*ww.bar.foo.com" },
1341 { false, "wwww.bar.foo.com", "ww*ww.bar.foo.com" }, 1373 { false, "wwww.bar.foo.com", "ww*ww.bar.foo.com" },
1342 { true, "wwww.bar.foo.com", "w*w.bar.foo.com" }, 1374 { true, "wwww.bar.foo.com", "w*w.bar.foo.com" },
1343 { false, "wwww.bar.foo.com", "w*w.bar.foo.c0m" }, 1375 { false, "wwww.bar.foo.com", "w*w.bar.foo.c0m" },
1344 { true, "WALLY.bar.foo.com", "wa*.bar.foo.com" }, 1376 { true, "WALLY.bar.foo.com", "wa*.bar.foo.com" },
1345 { true, "wally.bar.foo.com", "*Ly.bar.foo.com" }, 1377 { true, "wally.bar.foo.com", "*Ly.bar.foo.com" },
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 } 1495 }
1464 1496
1465 if (test_data.ip_addrs) { 1497 if (test_data.ip_addrs) {
1466 // Build up the certificate IP address list. 1498 // Build up the certificate IP address list.
1467 std::string ip_addrs_line(test_data.ip_addrs); 1499 std::string ip_addrs_line(test_data.ip_addrs);
1468 std::vector<std::string> ip_addressses_ascii; 1500 std::vector<std::string> ip_addressses_ascii;
1469 base::SplitString(ip_addrs_line, ',', &ip_addressses_ascii); 1501 base::SplitString(ip_addrs_line, ',', &ip_addressses_ascii);
1470 for (size_t i = 0; i < ip_addressses_ascii.size(); ++i) { 1502 for (size_t i = 0; i < ip_addressses_ascii.size(); ++i) {
1471 std::string& addr_ascii = ip_addressses_ascii[i]; 1503 std::string& addr_ascii = ip_addressses_ascii[i];
1472 ASSERT_NE(0U, addr_ascii.length()); 1504 ASSERT_NE(0U, addr_ascii.length());
1473 if (addr_ascii[0] == 'x') { // Hex encoded address 1505 if (addr_ascii[0] == 'x') { // Hex encoded address
1474 addr_ascii.erase(0, 1); 1506 addr_ascii.erase(0, 1);
1475 std::vector<uint8> bytes; 1507 std::vector<uint8> bytes;
1476 EXPECT_TRUE(base::HexStringToBytes(addr_ascii, &bytes)) 1508 EXPECT_TRUE(base::HexStringToBytes(addr_ascii, &bytes))
1477 << "Could not parse hex address " << addr_ascii << " i = " << i; 1509 << "Could not parse hex address " << addr_ascii << " i = " << i;
1478 ip_addressses.push_back(std::string(reinterpret_cast<char*>(&bytes[0]), 1510 ip_addressses.push_back(std::string(reinterpret_cast<char*>(&bytes[0]),
1479 bytes.size())); 1511 bytes.size()));
1480 ASSERT_EQ(16U, ip_addressses.back().size()) << i; 1512 ASSERT_EQ(16U, ip_addressses.back().size()) << i;
1481 } else { // Decimal groups 1513 } else { // Decimal groups
1482 std::vector<std::string> decimals_ascii; 1514 std::vector<std::string> decimals_ascii;
1483 base::SplitString(addr_ascii, '.', &decimals_ascii); 1515 base::SplitString(addr_ascii, '.', &decimals_ascii);
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
1701 #define MAYBE_VerifyMixed DISABLED_VerifyMixed 1733 #define MAYBE_VerifyMixed DISABLED_VerifyMixed
1702 #else 1734 #else
1703 #define MAYBE_VerifyMixed VerifyMixed 1735 #define MAYBE_VerifyMixed VerifyMixed
1704 #endif 1736 #endif
1705 WRAPPED_INSTANTIATE_TEST_CASE_P( 1737 WRAPPED_INSTANTIATE_TEST_CASE_P(
1706 MAYBE_VerifyMixed, 1738 MAYBE_VerifyMixed,
1707 X509CertificateWeakDigestTest, 1739 X509CertificateWeakDigestTest,
1708 testing::ValuesIn(kVerifyMixedTestData)); 1740 testing::ValuesIn(kVerifyMixedTestData));
1709 1741
1710 } // namespace net 1742 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698