OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/http/transport_security_state.h" | 5 #include "net/http/transport_security_state.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
15 #include "base/sha1.h" | 15 #include "base/sha1.h" |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
17 #include "base/test/histogram_tester.h" | 17 #include "base/test/histogram_tester.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "crypto/openssl_util.h" | 19 #include "crypto/openssl_util.h" |
20 #include "crypto/sha2.h" | 20 #include "crypto/sha2.h" |
21 #include "net/base/host_port_pair.h" | 21 #include "net/base/host_port_pair.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
24 #include "net/base/test_data_directory.h" | 24 #include "net/base/test_data_directory.h" |
25 #include "net/cert/asn1_util.h" | 25 #include "net/cert/asn1_util.h" |
26 #include "net/cert/cert_verifier.h" | 26 #include "net/cert/cert_verifier.h" |
27 #include "net/cert/cert_verify_result.h" | 27 #include "net/cert/cert_verify_result.h" |
28 #include "net/cert/ct_policy_status.h" | 28 #include "net/cert/ct_policy_status.h" |
| 29 #include "net/cert/internal/test_helpers.h" |
| 30 #include "net/cert/ocsp_staple.h" |
29 #include "net/cert/test_root_certs.h" | 31 #include "net/cert/test_root_certs.h" |
30 #include "net/cert/x509_cert_types.h" | 32 #include "net/cert/x509_cert_types.h" |
31 #include "net/cert/x509_certificate.h" | 33 #include "net/cert/x509_certificate.h" |
32 #include "net/http/http_util.h" | 34 #include "net/http/http_util.h" |
33 #include "net/log/net_log.h" | 35 #include "net/log/net_log.h" |
34 #include "net/ssl/ssl_info.h" | 36 #include "net/ssl/ssl_info.h" |
35 #include "net/test/cert_test_util.h" | 37 #include "net/test/cert_test_util.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
37 | 39 |
38 namespace net { | 40 namespace net { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 const char* const kBadPath[] = { | 73 const char* const kBadPath[] = { |
72 "sha1/111111111111111111111111111=", | 74 "sha1/111111111111111111111111111=", |
73 "sha1/222222222222222222222222222=", | 75 "sha1/222222222222222222222222222=", |
74 "sha1/333333333333333333333333333=", | 76 "sha1/333333333333333333333333333=", |
75 "sha256/1111111111111111111111111111111111111111111=", | 77 "sha256/1111111111111111111111111111111111111111111=", |
76 "sha256/2222222222222222222222222222222222222222222=", | 78 "sha256/2222222222222222222222222222222222222222222=", |
77 "sha256/3333333333333333333333333333333333333333333=", | 79 "sha256/3333333333333333333333333333333333333333333=", |
78 nullptr, | 80 nullptr, |
79 }; | 81 }; |
80 | 82 |
| 83 const char kOCSPPathPrefix[] = "net/data/parse_ocsp_unittest/"; |
| 84 |
| 85 const base::TimeDelta& kAgeTenYears = base::TimeDelta::FromDays(3650); |
| 86 |
81 // A mock ReportSender that just remembers the latest report | 87 // A mock ReportSender that just remembers the latest report |
82 // URI and report to be sent. | 88 // URI and report to be sent. |
83 class MockCertificateReportSender | 89 class MockCertificateReportSender |
84 : public TransportSecurityState::ReportSender { | 90 : public TransportSecurityState::ReportSender { |
85 public: | 91 public: |
86 MockCertificateReportSender() {} | 92 MockCertificateReportSender() {} |
87 ~MockCertificateReportSender() override {} | 93 ~MockCertificateReportSender() override {} |
88 | 94 |
89 void Send(const GURL& report_uri, const std::string& report) override { | 95 void Send(const GURL& report_uri, const std::string& report) override { |
90 latest_report_uri_ = report_uri; | 96 latest_report_uri_ = report_uri; |
(...skipping 1753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1844 state.ProcessExpectCTHeader("preload", host_port, ssl_info); | 1850 state.ProcessExpectCTHeader("preload", host_port, ssl_info); |
1845 EXPECT_EQ(1u, reporter.num_failures()); | 1851 EXPECT_EQ(1u, reporter.num_failures()); |
1846 EXPECT_TRUE(reporter.ssl_info().ct_compliance_details_available); | 1852 EXPECT_TRUE(reporter.ssl_info().ct_compliance_details_available); |
1847 EXPECT_EQ(ssl_info.ct_cert_policy_compliance, | 1853 EXPECT_EQ(ssl_info.ct_cert_policy_compliance, |
1848 reporter.ssl_info().ct_cert_policy_compliance); | 1854 reporter.ssl_info().ct_cert_policy_compliance); |
1849 EXPECT_EQ(host_port.host(), reporter.host_port_pair().host()); | 1855 EXPECT_EQ(host_port.host(), reporter.host_port_pair().host()); |
1850 EXPECT_EQ(host_port.port(), reporter.host_port_pair().port()); | 1856 EXPECT_EQ(host_port.port(), reporter.host_port_pair().port()); |
1851 EXPECT_EQ(GURL(kExpectCTStaticReportURI), reporter.report_uri()); | 1857 EXPECT_EQ(GURL(kExpectCTStaticReportURI), reporter.report_uri()); |
1852 } | 1858 } |
1853 | 1859 |
| 1860 class MockExpectStapleReportSender : public MockCertificateReportSender { |
| 1861 public: |
| 1862 bool ReportSent() { return latest_report() != ""; } |
| 1863 }; |
| 1864 |
| 1865 class ExpectStapleTest : public TransportSecurityStateTest { |
| 1866 public: |
| 1867 void SetUp() override { |
| 1868 TransportSecurityStateTest::SetUp(); |
| 1869 security_state_.SetReportSender(&report_sender_); |
| 1870 EnableStaticExpectStaple(&security_state_); |
| 1871 verify_time_ = base::Time::Now(); |
| 1872 } |
| 1873 |
| 1874 struct OCSPTest { |
| 1875 std::string response; |
| 1876 scoped_refptr<X509Certificate> certificate; |
| 1877 }; |
| 1878 |
| 1879 static bool LoadOCSPFromFile(std::string file_name, OCSPTest* ocsp) { |
| 1880 std::string ca_data; |
| 1881 std::string cert_data; |
| 1882 const PemBlockMapping mappings[] = { |
| 1883 {"OCSP RESPONSE", &ocsp->response}, |
| 1884 {"CA CERTIFICATE", &ca_data}, |
| 1885 {"CERTIFICATE", &cert_data}, |
| 1886 }; |
| 1887 std::string full_path = std::string(kOCSPPathPrefix) + file_name; |
| 1888 if (!ReadTestDataFromPemFile(full_path, mappings)) |
| 1889 return false; |
| 1890 |
| 1891 // Parse the server certificate |
| 1892 CertificateList server_cert_list = |
| 1893 X509Certificate::CreateCertificateListFromBytes( |
| 1894 cert_data.data(), cert_data.size(), |
| 1895 X509Certificate::FORMAT_SINGLE_CERTIFICATE); |
| 1896 ocsp->certificate = server_cert_list[0]; |
| 1897 return true; |
| 1898 } |
| 1899 |
| 1900 static void CheckExpectStapleReport( |
| 1901 const std::string& serialized_report, |
| 1902 const HostPortPair& host_port, |
| 1903 const X509Certificate& served_certificate_chain, |
| 1904 const ExpectStapleReport& report) { |
| 1905 std::unique_ptr<base::Value> value( |
| 1906 base::JSONReader::Read(serialized_report)); |
| 1907 ASSERT_TRUE(value); |
| 1908 ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 1909 |
| 1910 base::DictionaryValue* report_dict; |
| 1911 ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 1912 std::string report_hostname; |
| 1913 EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 1914 EXPECT_EQ(host_port.host(), report_hostname); |
| 1915 int report_port; |
| 1916 EXPECT_TRUE(report_dict->GetInteger("port", &report_port)); |
| 1917 EXPECT_EQ(host_port.port(), report_port); |
| 1918 |
| 1919 // Check certificate chain. |
| 1920 const base::ListValue* report_served_certificate_chain = nullptr; |
| 1921 ASSERT_TRUE(report_dict->GetList("served-certificate-chain", |
| 1922 &report_served_certificate_chain)); |
| 1923 std::vector<std::string> pem_encoded_chain; |
| 1924 served_certificate_chain.GetPEMEncodedChain(&pem_encoded_chain); |
| 1925 ASSERT_EQ(pem_encoded_chain.size(), |
| 1926 report_served_certificate_chain->GetSize()); |
| 1927 for (size_t i = 0; i < pem_encoded_chain.size(); i++) { |
| 1928 std::string cert_pem; |
| 1929 ASSERT_TRUE(report_served_certificate_chain->GetString(i, &cert_pem)); |
| 1930 EXPECT_EQ(pem_encoded_chain[i], cert_pem); |
| 1931 } |
| 1932 |
| 1933 // Check stapled responses. |
| 1934 const base::ListValue* report_ocsp_responses = nullptr; |
| 1935 ASSERT_TRUE(report_dict->GetList("ocsp-responses", &report_ocsp_responses)); |
| 1936 const auto& ocsp_responses = report.stapled_responses(); |
| 1937 ASSERT_EQ(ocsp_responses.size(), report_ocsp_responses->GetSize()); |
| 1938 for (size_t i = 0; i < ocsp_responses.size(); i++) { |
| 1939 const base::DictionaryValue* report_ocsp_response = nullptr; |
| 1940 report_ocsp_responses->GetDictionary(i, &report_ocsp_response); |
| 1941 bool is_date_valid; |
| 1942 ASSERT_TRUE( |
| 1943 report_ocsp_response->GetBoolean("is-date-valid", &is_date_valid)); |
| 1944 EXPECT_EQ(ocsp_responses[i].is_date_valid, is_date_valid); |
| 1945 bool is_correct_certificate; |
| 1946 ASSERT_TRUE(report_ocsp_response->GetBoolean("is-correct-certificate", |
| 1947 &is_correct_certificate)); |
| 1948 EXPECT_EQ(ocsp_responses[i].is_correct_certificate, |
| 1949 is_correct_certificate); |
| 1950 } |
| 1951 } |
| 1952 |
| 1953 protected: |
| 1954 static bool SerializeExpectStapleReport( |
| 1955 const HostPortPair& host_port_pair, |
| 1956 const X509Certificate& unverified_certificate, |
| 1957 const ExpectStapleReport& report, |
| 1958 std::string* serialized_report) { |
| 1959 return TransportSecurityState::SerializeExpectStapleReport( |
| 1960 host_port_pair, unverified_certificate, report, serialized_report); |
| 1961 } |
| 1962 |
| 1963 TransportSecurityState security_state_; |
| 1964 MockExpectStapleReportSender report_sender_; |
| 1965 base::Time verify_time_; |
| 1966 }; |
| 1967 |
| 1968 TEST_F(ExpectStapleTest, NoReportGoodResponse) { |
| 1969 OCSPTest ocsp_test; |
| 1970 ASSERT_TRUE(LoadOCSPFromFile("good_response.pem", &ocsp_test)); |
| 1971 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 1972 security_state_.CheckExpectStaple(host_port, *ocsp_test.certificate, |
| 1973 *ocsp_test.certificate, verify_time_, |
| 1974 kAgeTenYears, ocsp_test.response); |
| 1975 EXPECT_FALSE(report_sender_.ReportSent()); |
| 1976 }; |
| 1977 |
| 1978 TEST_F(ExpectStapleTest, ReportMissingResponse) { |
| 1979 OCSPTest ocsp_test; |
| 1980 ASSERT_TRUE(LoadOCSPFromFile("missing_response.pem", &ocsp_test)); |
| 1981 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 1982 security_state_.CheckExpectStaple(host_port, *ocsp_test.certificate, |
| 1983 *ocsp_test.certificate, verify_time_, |
| 1984 kAgeTenYears, ocsp_test.response); |
| 1985 EXPECT_TRUE(report_sender_.ReportSent()); |
| 1986 }; |
| 1987 |
| 1988 TEST_F(ExpectStapleTest, ReportOldResponse) { |
| 1989 OCSPTest ocsp_test; |
| 1990 ASSERT_TRUE(LoadOCSPFromFile("good_response.pem", &ocsp_test)); |
| 1991 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 1992 security_state_.CheckExpectStaple( |
| 1993 host_port, *ocsp_test.certificate, *ocsp_test.certificate, verify_time_, |
| 1994 base::TimeDelta::FromDays(7), ocsp_test.response); |
| 1995 EXPECT_TRUE(report_sender_.ReportSent()); |
| 1996 }; |
| 1997 |
| 1998 TEST_F(ExpectStapleTest, RevokedResponse) { |
| 1999 OCSPTest ocsp_test; |
| 2000 ASSERT_TRUE(LoadOCSPFromFile("revoke_response.pem", &ocsp_test)); |
| 2001 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 2002 security_state_.CheckExpectStaple(host_port, *ocsp_test.certificate, |
| 2003 *ocsp_test.certificate, verify_time_, |
| 2004 kAgeTenYears, ocsp_test.response); |
| 2005 EXPECT_TRUE(report_sender_.ReportSent()); |
| 2006 }; |
| 2007 |
| 2008 TEST_F(ExpectStapleTest, HasExtensions) { |
| 2009 OCSPTest ocsp_test; |
| 2010 ASSERT_TRUE(LoadOCSPFromFile("has_extension.pem", &ocsp_test)); |
| 2011 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 2012 security_state_.CheckExpectStaple(host_port, *ocsp_test.certificate, |
| 2013 *ocsp_test.certificate, verify_time_, |
| 2014 kAgeTenYears, ocsp_test.response); |
| 2015 EXPECT_FALSE(report_sender_.ReportSent()); |
| 2016 }; |
| 2017 |
| 2018 TEST_F(ExpectStapleTest, MultipleResponse) { |
| 2019 OCSPTest ocsp_test; |
| 2020 ASSERT_TRUE(LoadOCSPFromFile("multiple_response.pem", &ocsp_test)); |
| 2021 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 2022 security_state_.CheckExpectStaple(host_port, *ocsp_test.certificate, |
| 2023 *ocsp_test.certificate, verify_time_, |
| 2024 kAgeTenYears, ocsp_test.response); |
| 2025 EXPECT_FALSE(report_sender_.ReportSent()); |
| 2026 }; |
| 2027 |
| 2028 TEST_F(ExpectStapleTest, SerializeEmptyReport) { |
| 2029 OCSPTest ocsp_test; |
| 2030 ASSERT_TRUE(LoadOCSPFromFile("missing_response.pem", &ocsp_test)); |
| 2031 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 2032 std::unique_ptr<ExpectStapleReport> report = |
| 2033 ExpectStapleReport::FromRawOCSPResponse(ocsp_test.response, verify_time_, |
| 2034 kAgeTenYears, |
| 2035 *ocsp_test.certificate); |
| 2036 ASSERT_TRUE(report); |
| 2037 std::string serialized_report; |
| 2038 ASSERT_TRUE(SerializeExpectStapleReport(host_port, *ocsp_test.certificate, |
| 2039 *report, &serialized_report)); |
| 2040 ASSERT_NO_FATAL_FAILURE(CheckExpectStapleReport( |
| 2041 serialized_report, host_port, *ocsp_test.certificate, *report)); |
| 2042 }; |
| 2043 |
| 2044 TEST_F(ExpectStapleTest, SerializeMultipleResponses) { |
| 2045 OCSPTest ocsp_test; |
| 2046 ASSERT_TRUE(LoadOCSPFromFile("multiple_response.pem", &ocsp_test)); |
| 2047 HostPortPair host_port(kExpectStapleStaticHostname, 443); |
| 2048 std::unique_ptr<ExpectStapleReport> report = |
| 2049 ExpectStapleReport::FromRawOCSPResponse(ocsp_test.response, verify_time_, |
| 2050 kAgeTenYears, |
| 2051 *ocsp_test.certificate); |
| 2052 ASSERT_TRUE(report); |
| 2053 std::string serialized_report; |
| 2054 ASSERT_TRUE(SerializeExpectStapleReport(host_port, *ocsp_test.certificate, |
| 2055 *report, &serialized_report)); |
| 2056 ASSERT_NO_FATAL_FAILURE(CheckExpectStapleReport( |
| 2057 serialized_report, host_port, *ocsp_test.certificate, *report)); |
| 2058 }; |
| 2059 |
1854 } // namespace net | 2060 } // namespace net |
OLD | NEW |