Chromium Code Reviews

Side by Side Diff: net/socket/ssl_client_socket_unittest.cc

Issue 173853014: Make OpenSSL UpdateServerCert() OS independent. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added test case for retrieving unverified server cert chain. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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/socket/ssl_client_socket.h" 5 #include "net/socket/ssl_client_socket.h"
6 6
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "net/base/address_list.h" 9 #include "net/base/address_list.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
(...skipping 506 matching lines...)
517 return socket_factory_->CreateSSLClientSocket( 517 return socket_factory_->CreateSSLClientSocket(
518 connection.Pass(), host_and_port, ssl_config, context_); 518 connection.Pass(), host_and_port, ssl_config, context_);
519 } 519 }
520 520
521 ClientSocketFactory* socket_factory_; 521 ClientSocketFactory* socket_factory_;
522 scoped_ptr<MockCertVerifier> cert_verifier_; 522 scoped_ptr<MockCertVerifier> cert_verifier_;
523 scoped_ptr<TransportSecurityState> transport_security_state_; 523 scoped_ptr<TransportSecurityState> transport_security_state_;
524 SSLClientSocketContext context_; 524 SSLClientSocketContext context_;
525 }; 525 };
526 526
527 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest {
haavardm 2014/03/03 19:11:26 Moved this class from below into the anonymous nam
528 protected:
529 // Creates a test server with the given SSLOptions, connects to it and returns
530 // the SSLCertRequestInfo reported by the socket.
531 scoped_refptr<SSLCertRequestInfo> GetCertRequest(
532 SpawnedTestServer::SSLOptions ssl_options) {
533 SpawnedTestServer test_server(
534 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath());
535 if (!test_server.Start())
536 return NULL;
537
538 AddressList addr;
539 if (!test_server.GetAddressList(&addr))
540 return NULL;
541
542 TestCompletionCallback callback;
543 CapturingNetLog log;
544 scoped_ptr<StreamSocket> transport(
545 new TCPClientSocket(addr, &log, NetLog::Source()));
546 int rv = transport->Connect(callback.callback());
547 if (rv == ERR_IO_PENDING)
548 rv = callback.WaitForResult();
549 EXPECT_EQ(OK, rv);
550
551 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
552 transport.Pass(), test_server.host_port_pair(), kDefaultSSLConfig));
553 EXPECT_FALSE(sock->IsConnected());
554
555 rv = sock->Connect(callback.callback());
556 if (rv == ERR_IO_PENDING)
557 rv = callback.WaitForResult();
558 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo();
559 sock->GetSSLCertRequestInfo(request_info.get());
560 sock->Disconnect();
561 EXPECT_FALSE(sock->IsConnected());
562
563 return request_info;
564 }
565 };
566
527 //----------------------------------------------------------------------------- 567 //-----------------------------------------------------------------------------
528 568
529 // LogContainsSSLConnectEndEvent returns true if the given index in the given 569 // LogContainsSSLConnectEndEvent returns true if the given index in the given
530 // log is an SSL connect end event. The NSS sockets will cork in an attempt to 570 // log is an SSL connect end event. The NSS sockets will cork in an attempt to
531 // merge the first application data record with the Finished message when false 571 // merge the first application data record with the Finished message when false
532 // starting. However, in order to avoid the server timing out the handshake, 572 // starting. However, in order to avoid the server timing out the handshake,
533 // they'll give up waiting for application data and send the Finished after a 573 // they'll give up waiting for application data and send the Finished after a
534 // timeout. This means that an SSL connect end event may appear as a socket 574 // timeout. This means that an SSL connect end event may appear as a socket
535 // write. 575 // write.
536 static bool LogContainsSSLConnectEndEvent( 576 static bool LogContainsSSLConnectEndEvent(
wtc 2014/03/10 21:45:34 Nit: you can remove "static" because this function
537 const CapturingNetLog::CapturedEntryList& log, 577 const CapturingNetLog::CapturedEntryList& log,
538 int i) { 578 int i) {
539 return LogContainsEndEvent(log, i, NetLog::TYPE_SSL_CONNECT) || 579 return LogContainsEndEvent(log, i, NetLog::TYPE_SSL_CONNECT) ||
540 LogContainsEvent( 580 LogContainsEvent(
541 log, i, NetLog::TYPE_SOCKET_BYTES_SENT, NetLog::PHASE_NONE); 581 log, i, NetLog::TYPE_SOCKET_BYTES_SENT, NetLog::PHASE_NONE);
542 } 582 }
543 583
584 } // namespace
haavardm 2014/03/03 19:11:26 Moved tests out of the anonymous namespace as it g
585
544 TEST_F(SSLClientSocketTest, Connect) { 586 TEST_F(SSLClientSocketTest, Connect) {
545 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, 587 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS,
546 SpawnedTestServer::kLocalhost, 588 SpawnedTestServer::kLocalhost,
547 base::FilePath()); 589 base::FilePath());
548 ASSERT_TRUE(test_server.Start()); 590 ASSERT_TRUE(test_server.Start());
549 591
550 AddressList addr; 592 AddressList addr;
551 ASSERT_TRUE(test_server.GetAddressList(&addr)); 593 ASSERT_TRUE(test_server.GetAddressList(&addr));
552 594
553 TestCompletionCallback callback; 595 TestCompletionCallback callback;
(...skipping 1147 matching lines...)
1701 EXPECT_EQ(rv, OK); 1743 EXPECT_EQ(rv, OK);
1702 EXPECT_NE(memcmp(client_out1, client_out2, kKeyingMaterialSize), 0); 1744 EXPECT_NE(memcmp(client_out1, client_out2, kKeyingMaterialSize), 0);
1703 } 1745 }
1704 1746
1705 // Verifies that SSLClientSocket::ClearSessionCache can be called without 1747 // Verifies that SSLClientSocket::ClearSessionCache can be called without
1706 // explicit NSS initialization. 1748 // explicit NSS initialization.
1707 TEST(SSLClientSocket, ClearSessionCache) { 1749 TEST(SSLClientSocket, ClearSessionCache) {
1708 SSLClientSocket::ClearSessionCache(); 1750 SSLClientSocket::ClearSessionCache();
1709 } 1751 }
1710 1752
1753 // Test that the server certificates are properly retrieved from the underlying
wtc 2014/03/10 21:45:34 Nit: I don't understand why it is important to tes
Ryan Sleevi 2014/03/11 00:15:15 because we weren't before, and we were buggy :)
1754 // SSL stack.
1755 TEST_F(SSLClientSocketTest, VerifyServerChainProperlyOrdered) {
1756 // The connection does not have to be successful
wtc 2014/03/10 21:45:34 Nit: missing a period (.) at the end of the senten
1757 cert_verifier_->set_default_result(ERR_CERT_INVALID);
1758
1759 // Set up a test server with CERT_CHAIN_WRONG_ROOT.
wtc 2014/03/10 21:45:34 Nit: it seems better to send a valid certificate c
haavardm 2014/03/11 18:43:21 This was to reuse the certs set in BaseTestServer
1760 // This makes the server present redundant-server-chain.pem, which contains
1761 // intermediate certificates.
1762 SpawnedTestServer::SSLOptions ssl_options(
1763 SpawnedTestServer::SSLOptions::CERT_CHAIN_WRONG_ROOT);
1764 SpawnedTestServer test_server(
1765 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath());
1766 ASSERT_TRUE(test_server.Start());
1767
1768 AddressList addr;
1769 ASSERT_TRUE(test_server.GetAddressList(&addr));
1770
1771 TestCompletionCallback callback;
1772 scoped_ptr<StreamSocket> transport(
1773 new TCPClientSocket(addr, NULL, NetLog::Source()));
1774 int rv = transport->Connect(callback.callback());
1775 if (rv == ERR_IO_PENDING)
1776 rv = callback.WaitForResult();
wtc 2014/03/10 21:45:34 Just FYI: there is a callback.GetResult() method t
haavardm 2014/03/11 18:43:21 Ah, right. I always forget about that one :) On 2
1777 EXPECT_EQ(OK, rv);
1778
1779 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
1780 transport.Pass(), test_server.host_port_pair(), kDefaultSSLConfig));
1781 EXPECT_FALSE(sock->IsConnected());
1782 rv = sock->Connect(callback.callback());
1783
1784 if (rv == ERR_IO_PENDING)
1785 rv = callback.WaitForResult();
1786
1787 EXPECT_EQ(ERR_CERT_INVALID, rv);
1788 EXPECT_TRUE(sock->IsConnected());
1789
1790 // When given option CERT_CHAIN_WRONG_ROOT, SpawnedTestServer will present
1791 // certs from redundant-server-chain.pem.
1792 CertificateList server_certs =
1793 CreateCertificateListFromFile(GetTestCertsDirectory(),
1794 "redundant-server-chain.pem",
1795 X509Certificate::FORMAT_AUTO);
1796
1797 // Get the server certificate as received client side
1798 const scoped_refptr<X509Certificate> server_certificate =
1799 sock->GetUnverifiedServerCertificate();
1800
1801 // Get the intermediates as received client side
1802 const X509Certificate::OSCertHandles& server_intermediates =
1803 server_certificate->GetIntermediateCertificates();
1804
1805 // Check that the unverified server certificate chain is properly retrieved
1806 // from the underlying ssl stack.
1807 ASSERT_EQ(3U, server_intermediates.size());
wtc 2014/03/10 21:45:34 1. Should we also assert that server_certs.size()
haavardm 2014/03/11 18:43:21 Done.
1808 EXPECT_TRUE(X509Certificate::IsSameOSCert(
1809 server_certificate->os_cert_handle(), server_certs[0]->os_cert_handle()));
1810 EXPECT_TRUE(X509Certificate::IsSameOSCert(server_intermediates[0],
1811 server_certs[1]->os_cert_handle()));
1812 EXPECT_TRUE(X509Certificate::IsSameOSCert(server_intermediates[1],
1813 server_certs[2]->os_cert_handle()));
1814 EXPECT_TRUE(X509Certificate::IsSameOSCert(server_intermediates[2],
1815 server_certs[3]->os_cert_handle()));
1816
1817 sock->Disconnect();
1818 EXPECT_FALSE(sock->IsConnected());
1819 }
1820
1711 // This tests that SSLInfo contains a properly re-constructed certificate 1821 // This tests that SSLInfo contains a properly re-constructed certificate
1712 // chain. That, in turn, verifies that GetSSLInfo is giving us the chain as 1822 // chain. That, in turn, verifies that GetSSLInfo is giving us the chain as
1713 // verified, not the chain as served by the server. (They may be different.) 1823 // verified, not the chain as served by the server. (They may be different.)
1714 // 1824 //
1715 // CERT_CHAIN_WRONG_ROOT is redundant-server-chain.pem. It contains A 1825 // CERT_CHAIN_WRONG_ROOT is redundant-server-chain.pem. It contains A
1716 // (end-entity) -> B -> C, and C is signed by D. redundant-validated-chain.pem 1826 // (end-entity) -> B -> C, and C is signed by D. redundant-validated-chain.pem
1717 // contains a chain of A -> B -> C2, where C2 is the same public key as C, but 1827 // contains a chain of A -> B -> C2, where C2 is the same public key as C, but
1718 // a self-signed root. Such a situation can occur when a new root (C2) is 1828 // a self-signed root. Such a situation can occur when a new root (C2) is
1719 // cross-certified by an old root (D) and has two different versions of its 1829 // cross-certified by an old root (D) and has two different versions of its
1720 // floating around. Servers may supply C2 as an intermediate, but the 1830 // floating around. Servers may supply C2 as an intermediate, but the
(...skipping 78 matching lines...)
1799 certs[0]->os_cert_handle())); 1909 certs[0]->os_cert_handle()));
1800 EXPECT_TRUE(X509Certificate::IsSameOSCert(intermediates[0], 1910 EXPECT_TRUE(X509Certificate::IsSameOSCert(intermediates[0],
1801 certs[1]->os_cert_handle())); 1911 certs[1]->os_cert_handle()));
1802 EXPECT_TRUE(X509Certificate::IsSameOSCert(intermediates[1], 1912 EXPECT_TRUE(X509Certificate::IsSameOSCert(intermediates[1],
1803 certs[2]->os_cert_handle())); 1913 certs[2]->os_cert_handle()));
1804 1914
1805 sock->Disconnect(); 1915 sock->Disconnect();
1806 EXPECT_FALSE(sock->IsConnected()); 1916 EXPECT_FALSE(sock->IsConnected());
1807 } 1917 }
1808 1918
1809 // Verifies the correctness of GetSSLCertRequestInfo. 1919 // Verifies the correctness of GetSSLCertRequestInfo.
wtc 2014/03/10 21:45:34 I think this comment should be moved along with th
haavardm 2014/03/11 18:43:21 Done.
1810 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest {
1811 protected:
1812 // Creates a test server with the given SSLOptions, connects to it and returns
1813 // the SSLCertRequestInfo reported by the socket.
1814 scoped_refptr<SSLCertRequestInfo> GetCertRequest(
1815 SpawnedTestServer::SSLOptions ssl_options) {
1816 SpawnedTestServer test_server(
1817 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath());
1818 if (!test_server.Start())
1819 return NULL;
1820
1821 AddressList addr;
1822 if (!test_server.GetAddressList(&addr))
1823 return NULL;
1824
1825 TestCompletionCallback callback;
1826 CapturingNetLog log;
1827 scoped_ptr<StreamSocket> transport(
1828 new TCPClientSocket(addr, &log, NetLog::Source()));
1829 int rv = transport->Connect(callback.callback());
1830 if (rv == ERR_IO_PENDING)
1831 rv = callback.WaitForResult();
1832 EXPECT_EQ(OK, rv);
1833
1834 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
1835 transport.Pass(), test_server.host_port_pair(), kDefaultSSLConfig));
1836 EXPECT_FALSE(sock->IsConnected());
1837
1838 rv = sock->Connect(callback.callback());
1839 if (rv == ERR_IO_PENDING)
1840 rv = callback.WaitForResult();
1841 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo();
1842 sock->GetSSLCertRequestInfo(request_info.get());
1843 sock->Disconnect();
1844 EXPECT_FALSE(sock->IsConnected());
1845
1846 return request_info;
1847 }
1848 };
1849
1850 TEST_F(SSLClientSocketCertRequestInfoTest, NoAuthorities) { 1920 TEST_F(SSLClientSocketCertRequestInfoTest, NoAuthorities) {
1851 SpawnedTestServer::SSLOptions ssl_options; 1921 SpawnedTestServer::SSLOptions ssl_options;
1852 ssl_options.request_client_certificate = true; 1922 ssl_options.request_client_certificate = true;
1853 scoped_refptr<SSLCertRequestInfo> request_info = GetCertRequest(ssl_options); 1923 scoped_refptr<SSLCertRequestInfo> request_info = GetCertRequest(ssl_options);
1854 ASSERT_TRUE(request_info.get()); 1924 ASSERT_TRUE(request_info.get());
1855 EXPECT_EQ(0u, request_info->cert_authorities.size()); 1925 EXPECT_EQ(0u, request_info->cert_authorities.size());
1856 } 1926 }
1857 1927
1858 TEST_F(SSLClientSocketCertRequestInfoTest, TwoAuthorities) { 1928 TEST_F(SSLClientSocketCertRequestInfoTest, TwoAuthorities) {
1859 const base::FilePath::CharType kThawteFile[] = 1929 const base::FilePath::CharType kThawteFile[] =
(...skipping 31 matching lines...)
1891 scoped_refptr<SSLCertRequestInfo> request_info = GetCertRequest(ssl_options); 1961 scoped_refptr<SSLCertRequestInfo> request_info = GetCertRequest(ssl_options);
1892 ASSERT_TRUE(request_info.get()); 1962 ASSERT_TRUE(request_info.get());
1893 ASSERT_EQ(2u, request_info->cert_authorities.size()); 1963 ASSERT_EQ(2u, request_info->cert_authorities.size());
1894 EXPECT_EQ(std::string(reinterpret_cast<const char*>(kThawteDN), kThawteLen), 1964 EXPECT_EQ(std::string(reinterpret_cast<const char*>(kThawteDN), kThawteLen),
1895 request_info->cert_authorities[0]); 1965 request_info->cert_authorities[0]);
1896 EXPECT_EQ( 1966 EXPECT_EQ(
1897 std::string(reinterpret_cast<const char*>(kDiginotarDN), kDiginotarLen), 1967 std::string(reinterpret_cast<const char*>(kDiginotarDN), kDiginotarLen),
1898 request_info->cert_authorities[1]); 1968 request_info->cert_authorities[1]);
1899 } 1969 }
1900 1970
1901 } // namespace
1902
1903 TEST_F(SSLClientSocketTest, ConnectSignedCertTimestampsEnabledTLSExtension) { 1971 TEST_F(SSLClientSocketTest, ConnectSignedCertTimestampsEnabledTLSExtension) {
1904 SpawnedTestServer::SSLOptions ssl_options; 1972 SpawnedTestServer::SSLOptions ssl_options;
1905 ssl_options.signed_cert_timestamps_tls_ext = "test"; 1973 ssl_options.signed_cert_timestamps_tls_ext = "test";
1906 1974
1907 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, 1975 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS,
1908 ssl_options, 1976 ssl_options,
1909 base::FilePath()); 1977 base::FilePath());
1910 ASSERT_TRUE(test_server.Start()); 1978 ASSERT_TRUE(test_server.Start());
1911 1979
1912 AddressList addr; 1980 AddressList addr;
(...skipping 139 matching lines...)
2052 log.GetEntries(&entries); 2120 log.GetEntries(&entries);
2053 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); 2121 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
2054 2122
2055 EXPECT_FALSE(sock->signed_cert_timestamps_received_); 2123 EXPECT_FALSE(sock->signed_cert_timestamps_received_);
2056 2124
2057 sock->Disconnect(); 2125 sock->Disconnect();
2058 EXPECT_FALSE(sock->IsConnected()); 2126 EXPECT_FALSE(sock->IsConnected());
2059 } 2127 }
2060 2128
2061 } // namespace net 2129 } // namespace net
OLDNEW
« net/socket/ssl_client_socket_openssl.cc ('K') | « net/socket/ssl_client_socket_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine