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

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

Issue 3146034: Allow the constructed certificate chain to be returned in CertVerifyResult (Closed)
Patch Set: Created 10 years, 4 months 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
« no previous file with comments | « net/base/cert_verify_result.cc ('k') | net/base/x509_chain.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "net/base/cert_status_flags.h" 9 #include "net/base/cert_status_flags.h"
10 #include "net/base/cert_test_util.h" 10 #include "net/base/cert_test_util.h"
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 webkit_cert->os_cert_handle())); 618 webkit_cert->os_cert_handle()));
619 EXPECT_TRUE(cert2->HasIntermediateCertificate( 619 EXPECT_TRUE(cert2->HasIntermediateCertificate(
620 thawte_cert->os_cert_handle())); 620 thawte_cert->os_cert_handle()));
621 EXPECT_FALSE(cert2->HasIntermediateCertificate( 621 EXPECT_FALSE(cert2->HasIntermediateCertificate(
622 paypal_cert->os_cert_handle())); 622 paypal_cert->os_cert_handle()));
623 623
624 // Cleanup 624 // Cleanup
625 X509Certificate::FreeOSCertHandle(google_handle); 625 X509Certificate::FreeOSCertHandle(google_handle);
626 } 626 }
627 627
628 TEST(X509CertificateTest, VerifyReturnChainBasic) {
629 FilePath certs_dir = GetTestCertsDirectory();
630 CertificateList certs =
631 CreateCertificateListFromFile(certs_dir, "google.full_chain.pem",
632 X509Certificate::FORMAT_AUTO);
633 ASSERT_EQ(3U, certs.size());
634
635 X509Certificate::OSCertHandles intermediates;
636 intermediates.push_back(certs[1]->os_cert_handle());
637 intermediates.push_back(certs[2]->os_cert_handle());
638
639 scoped_refptr<X509Certificate> google_full_chain =
640 X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(),
641 intermediates);
642 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_full_chain);
643 ASSERT_EQ(2U, google_full_chain->GetIntermediateCertificates().size());
644
645 int flags = x509_chain::VERIFY_RETURN_CHAIN;
646 CertVerifyResult verify_result;
647 EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.certificate);
648 int error = x509_chain::VerifySSLServer(google_full_chain,
649 "www.google.com", flags,
650 &verify_result);
651 EXPECT_EQ(OK, error);
652 ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.certificate);
653
654 EXPECT_TRUE(X509Certificate::IsSameOSCert(
655 google_full_chain->os_cert_handle(),
656 verify_result.certificate->os_cert_handle()));
657 const X509Certificate::OSCertHandles& return_intermediates =
658 verify_result.certificate->GetIntermediateCertificates();
659 EXPECT_EQ(2U, return_intermediates.size());
660 EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[0],
661 certs[1]->os_cert_handle()));
662 EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[1],
663 certs[2]->os_cert_handle()));
664 }
665
666 // Test that the certificate returned in CertVerifyResult is an actual
667 // certificate chain by supplying the original chain out of order, which
668 // will not properly validate unless the underlying crypto library reorders
669 // the certificates into a valid chain.
670 TEST(X509CertificateTest, VerifyReturnChainProperlyOrders) {
671 FilePath certs_dir = GetTestCertsDirectory();
672 CertificateList certs =
673 CreateCertificateListFromFile(certs_dir, "google.full_chain.pem",
674 X509Certificate::FORMAT_AUTO);
675 ASSERT_EQ(3U, certs.size());
676
677 // Construct the chain out of order.
678 X509Certificate::OSCertHandles intermediates;
679 intermediates.push_back(certs[2]->os_cert_handle());
680 intermediates.push_back(certs[1]->os_cert_handle());
681
682 scoped_refptr<X509Certificate> google_full_chain =
683 X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(),
684 intermediates);
685 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_full_chain);
686 ASSERT_EQ(2U, google_full_chain->GetIntermediateCertificates().size());
687
688 int flags = x509_chain::VERIFY_RETURN_CHAIN;
689 CertVerifyResult verify_result;
690 EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.certificate);
691 int error = x509_chain::VerifySSLServer(google_full_chain,
692 "www.google.com", flags,
693 &verify_result);
694 EXPECT_EQ(OK, error);
695 ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.certificate);
696
697 EXPECT_TRUE(X509Certificate::IsSameOSCert(
698 google_full_chain->os_cert_handle(),
699 verify_result.certificate->os_cert_handle()));
700 const X509Certificate::OSCertHandles& return_intermediates =
701 verify_result.certificate->GetIntermediateCertificates();
702 EXPECT_EQ(2U, return_intermediates.size());
703 EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[0],
704 certs[1]->os_cert_handle()));
705 EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[1],
706 certs[2]->os_cert_handle()));
707 }
708
709 // Test that certificates supplied, but that are unrelated to the
710 // certificate for which the chain is being constructed and verified, are
711 // properly filtered out in the return chain.
712 TEST(X509CertificateTest, VerifyReturnChainFiltersUnrelatedCerts) {
713 FilePath certs_dir = GetTestCertsDirectory();
714 CertificateList certs =
715 CreateCertificateListFromFile(certs_dir, "google.full_chain.pem",
716 X509Certificate::FORMAT_AUTO);
717 ASSERT_EQ(3U, certs.size());
718 scoped_refptr<X509Certificate> unrelated_dod_certificate =
719 ImportCertFromFile(certs_dir, "dod_ca_17_cert.der");
720 scoped_refptr<X509Certificate> unrelated_dod_certificate2 =
721 ImportCertFromFile(certs_dir, "dod_root_ca_2_cert.der");
722 ASSERT_NE(static_cast<X509Certificate*>(NULL), unrelated_dod_certificate);
723 ASSERT_NE(static_cast<X509Certificate*>(NULL), unrelated_dod_certificate2);
724
725 // Interject unrelated certificates into the list of intermediates.
726 X509Certificate::OSCertHandles intermediates;
727 intermediates.push_back(unrelated_dod_certificate->os_cert_handle());
728 intermediates.push_back(certs[1]->os_cert_handle());
729 intermediates.push_back(unrelated_dod_certificate2->os_cert_handle());
730 intermediates.push_back(certs[2]->os_cert_handle());
731
732 scoped_refptr<X509Certificate> google_full_chain =
733 X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(),
734 intermediates);
735 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_full_chain);
736 ASSERT_EQ(4U, google_full_chain->GetIntermediateCertificates().size());
737
738 int flags = x509_chain::VERIFY_RETURN_CHAIN;
739 CertVerifyResult verify_result;
740 EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.certificate);
741 int error = x509_chain::VerifySSLServer(google_full_chain,
742 "www.google.com", flags,
743 &verify_result);
744 EXPECT_EQ(OK, error);
745 ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.certificate);
746
747 EXPECT_TRUE(X509Certificate::IsSameOSCert(
748 google_full_chain->os_cert_handle(),
749 verify_result.certificate->os_cert_handle()));
750 const X509Certificate::OSCertHandles& return_intermediates =
751 verify_result.certificate->GetIntermediateCertificates();
752 EXPECT_EQ(2U, return_intermediates.size());
753 EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[0],
754 certs[1]->os_cert_handle()));
755 EXPECT_TRUE(X509Certificate::IsSameOSCert(return_intermediates[1],
756 certs[2]->os_cert_handle()));
757 }
758
759 #if defined(ALLOW_EXTERNAL_ACCESS)
760 // Test verification using only an end entity certificate, but one which has
761 // an Authority Information Access extension containing a a location where
762 // the intermediate certificate can be downloaded. It is not guaranteed that
763 // the underlying crypto library will need to retrieve it using the AIA
764 // extension, as it may already have a local copy installed or cached. In
765 // order to keep this test reliable, however, and because it may make a
766 // request to an external resource, this test is guarded by the define.
767 TEST(X509CertificateTest, VerifyReturnsChainUsingAIA) {
768 FilePath certs_dir = GetTestCertsDirectory();
769 scoped_refptr<X509Certificate> google_cert =
770 ImportCertFromFile(certs_dir, "google.single.der");
771 ASSERT_NE(static_cast<X509Certificate*>(NULL), google_cert);
772 EXPECT_EQ(0U, google_cert->GetIntermediateCertificates().size());
773
774 int flags = x509_chain::VERIFY_RETURN_CHAIN;
775 CertVerifyResult verify_result;
776 EXPECT_EQ(static_cast<X509Certificate*>(NULL), verify_result.certificate);
777 int error = x509_chain::VerifySSLServer(google_cert, "www.google.com",
778 flags, &verify_result);
779 EXPECT_EQ(OK, error);
780 ASSERT_NE(static_cast<X509Certificate*>(NULL), verify_result.certificate);
781
782 EXPECT_TRUE(X509Certificate::IsSameOSCert(
783 google_cert->os_cert_handle(),
784 verify_result.certificate->os_cert_handle()));
785 const X509Certificate::OSCertHandles& intermediates =
786 verify_result.certificate->GetIntermediateCertificates();
787 EXPECT_EQ(2U, intermediates.size());
788 }
789 #endif
790
628 #if defined(OS_MACOSX) 791 #if defined(OS_MACOSX)
629 TEST(X509CertificateTest, IsIssuedBy) { 792 TEST(X509CertificateTest, IsIssuedBy) {
630 FilePath certs_dir = GetTestCertsDirectory(); 793 FilePath certs_dir = GetTestCertsDirectory();
631 794
632 // Test a client certificate from MIT. 795 // Test a client certificate from MIT.
633 scoped_refptr<X509Certificate> mit_davidben_cert = 796 scoped_refptr<X509Certificate> mit_davidben_cert =
634 ImportCertFromFile(certs_dir, "mit.davidben.der"); 797 ImportCertFromFile(certs_dir, "mit.davidben.der");
635 ASSERT_NE(static_cast<X509Certificate*>(NULL), mit_davidben_cert); 798 ASSERT_NE(static_cast<X509Certificate*>(NULL), mit_davidben_cert);
636 799
637 CertPrincipal mit_issuer; 800 CertPrincipal mit_issuer;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 877
715 for (size_t j = 0; j < 20; ++j) 878 for (size_t j = 0; j < 20; ++j)
716 EXPECT_EQ(expected_fingerprint[j], actual_fingerprint.data[j]); 879 EXPECT_EQ(expected_fingerprint[j], actual_fingerprint.data[j]);
717 } 880 }
718 } 881 }
719 882
720 INSTANTIATE_TEST_CASE_P(, X509CertificateParseTest, 883 INSTANTIATE_TEST_CASE_P(, X509CertificateParseTest,
721 testing::ValuesIn(FormatTestData)); 884 testing::ValuesIn(FormatTestData));
722 885
723 } // namespace net 886 } // namespace net
OLDNEW
« no previous file with comments | « net/base/cert_verify_result.cc ('k') | net/base/x509_chain.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698