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

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

Issue 517083002: Enable Certificate Transparency in the OpenSSL port. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ct-objects-extractor
Patch Set: Created 6 years, 3 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/socket/ssl_client_socket_openssl.cc ('k') | no next file » | 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) 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 "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "net/base/address_list.h" 11 #include "net/base/address_list.h"
12 #include "net/base/io_buffer.h" 12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
14 #include "net/base/net_log.h" 14 #include "net/base/net_log.h"
15 #include "net/base/net_log_unittest.h" 15 #include "net/base/net_log_unittest.h"
16 #include "net/base/test_completion_callback.h" 16 #include "net/base/test_completion_callback.h"
17 #include "net/base/test_data_directory.h" 17 #include "net/base/test_data_directory.h"
18 #include "net/cert/asn1_util.h"
19 #include "net/cert/ct_verifier.h"
18 #include "net/cert/mock_cert_verifier.h" 20 #include "net/cert/mock_cert_verifier.h"
19 #include "net/cert/test_root_certs.h" 21 #include "net/cert/test_root_certs.h"
20 #include "net/dns/host_resolver.h" 22 #include "net/dns/host_resolver.h"
21 #include "net/http/transport_security_state.h" 23 #include "net/http/transport_security_state.h"
22 #include "net/socket/client_socket_factory.h" 24 #include "net/socket/client_socket_factory.h"
23 #include "net/socket/client_socket_handle.h" 25 #include "net/socket/client_socket_handle.h"
24 #include "net/socket/socket_test_util.h" 26 #include "net/socket/socket_test_util.h"
25 #include "net/socket/tcp_client_socket.h" 27 #include "net/socket/tcp_client_socket.h"
26 #include "net/ssl/channel_id_service.h" 28 #include "net/ssl/channel_id_service.h"
27 #include "net/ssl/default_channel_id_store.h" 29 #include "net/ssl/default_channel_id_store.h"
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 base::Time delete_end, 651 base::Time delete_end,
650 const base::Closure& completion_callback) 652 const base::Closure& completion_callback)
651 OVERRIDE {} 653 OVERRIDE {}
652 virtual void DeleteAll(const base::Closure& completion_callback) OVERRIDE {} 654 virtual void DeleteAll(const base::Closure& completion_callback) OVERRIDE {}
653 virtual void GetAllChannelIDs(const GetChannelIDListCallback& callback) 655 virtual void GetAllChannelIDs(const GetChannelIDListCallback& callback)
654 OVERRIDE {} 656 OVERRIDE {}
655 virtual int GetChannelIDCount() OVERRIDE { return 0; } 657 virtual int GetChannelIDCount() OVERRIDE { return 0; }
656 virtual void SetForceKeepSessionState() OVERRIDE {} 658 virtual void SetForceKeepSessionState() OVERRIDE {}
657 }; 659 };
658 660
661 // A mock CTVerifier that records every call to Verify but doesn't verify
662 // anything.
663 class MockCTVerifier : public CTVerifier {
664 public:
665 // A single call to Verify.
666 struct Call {
667 scoped_refptr<X509Certificate> cert;
668 std::string stapled_ocsp_response;
669 std::string sct_list_from_tls_extension;
670 };
671
672 MockCTVerifier() {}
673 const std::vector<Call>& calls() const { return calls_; }
674
675 virtual int Verify(X509Certificate* cert,
676 const std::string& stapled_ocsp_response,
677 const std::string& sct_list_from_tls_extension,
678 ct::CTVerifyResult* result,
679 const BoundNetLog& net_log) OVERRIDE {
680 // Save the call and otherwise do nothing.
681 Call call;
682 call.cert = cert;
683 call.stapled_ocsp_response = stapled_ocsp_response;
684 call.sct_list_from_tls_extension = sct_list_from_tls_extension;
685 calls_.push_back(call);
686 return ERR_CT_NO_SCTS_VERIFIED_OK;
687 }
688
689
690 private:
691 std::vector<Call> calls_;
692
693 DISALLOW_COPY_AND_ASSIGN(MockCTVerifier);
694 };
695
659 class SSLClientSocketTest : public PlatformTest { 696 class SSLClientSocketTest : public PlatformTest {
660 public: 697 public:
661 SSLClientSocketTest() 698 SSLClientSocketTest()
662 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), 699 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
663 cert_verifier_(new MockCertVerifier), 700 cert_verifier_(new MockCertVerifier),
701 ct_verifier_(new MockCTVerifier),
664 transport_security_state_(new TransportSecurityState), 702 transport_security_state_(new TransportSecurityState),
665 ran_handshake_completion_callback_(false) { 703 ran_handshake_completion_callback_(false) {
666 cert_verifier_->set_default_result(OK); 704 cert_verifier_->set_default_result(OK);
667 context_.cert_verifier = cert_verifier_.get(); 705 context_.cert_verifier = cert_verifier_.get();
706 context_.cert_transparency_verifier = ct_verifier_.get();
668 context_.transport_security_state = transport_security_state_.get(); 707 context_.transport_security_state = transport_security_state_.get();
669 } 708 }
670 709
671 void RecordCompletedHandshake() { ran_handshake_completion_callback_ = true; } 710 void RecordCompletedHandshake() { ran_handshake_completion_callback_ = true; }
672 711
673 protected: 712 protected:
674 // The address of the spawned test server, after calling StartTestServer(). 713 // The address of the spawned test server, after calling StartTestServer().
675 const AddressList& addr() const { return addr_; } 714 const AddressList& addr() const { return addr_; }
676 715
677 // The SpawnedTestServer object, after calling StartTestServer(). 716 // The SpawnedTestServer object, after calling StartTestServer().
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 LOG(ERROR) << "SSL Socket prematurely connected"; 774 LOG(ERROR) << "SSL Socket prematurely connected";
736 return false; 775 return false;
737 } 776 }
738 777
739 *result = callback_.GetResult(sock_->Connect(callback_.callback())); 778 *result = callback_.GetResult(sock_->Connect(callback_.callback()));
740 return true; 779 return true;
741 } 780 }
742 781
743 ClientSocketFactory* socket_factory_; 782 ClientSocketFactory* socket_factory_;
744 scoped_ptr<MockCertVerifier> cert_verifier_; 783 scoped_ptr<MockCertVerifier> cert_verifier_;
784 scoped_ptr<MockCTVerifier> ct_verifier_;
745 scoped_ptr<TransportSecurityState> transport_security_state_; 785 scoped_ptr<TransportSecurityState> transport_security_state_;
746 SSLClientSocketContext context_; 786 SSLClientSocketContext context_;
747 scoped_ptr<SSLClientSocket> sock_; 787 scoped_ptr<SSLClientSocket> sock_;
748 CapturingNetLog log_; 788 CapturingNetLog log_;
749 bool ran_handshake_completion_callback_; 789 bool ran_handshake_completion_callback_;
750 790
751 private: 791 private:
752 scoped_ptr<StreamSocket> transport_; 792 scoped_ptr<StreamSocket> transport_;
753 scoped_ptr<SpawnedTestServer> test_server_; 793 scoped_ptr<SpawnedTestServer> test_server_;
754 TestCompletionCallback callback_; 794 TestCompletionCallback callback_;
(...skipping 1749 matching lines...) Expand 10 before | Expand all | Expand 10 after
2504 CapturingNetLog::CapturedEntryList entries; 2544 CapturingNetLog::CapturedEntryList entries;
2505 log.GetEntries(&entries); 2545 log.GetEntries(&entries);
2506 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); 2546 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT));
2507 if (rv == ERR_IO_PENDING) 2547 if (rv == ERR_IO_PENDING)
2508 rv = callback.WaitForResult(); 2548 rv = callback.WaitForResult();
2509 EXPECT_EQ(OK, rv); 2549 EXPECT_EQ(OK, rv);
2510 EXPECT_TRUE(sock->IsConnected()); 2550 EXPECT_TRUE(sock->IsConnected());
2511 log.GetEntries(&entries); 2551 log.GetEntries(&entries);
2512 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); 2552 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
2513 2553
2514 #if !defined(USE_OPENSSL) 2554 // Check that the SCT extension was extracted in the expected format.
2515 EXPECT_TRUE(sock->signed_cert_timestamps_received_); 2555 EXPECT_TRUE(sock->signed_cert_timestamps_received_);
2516 #else 2556 ASSERT_EQ(1u, ct_verifier_->calls().size());
2517 // Enabling CT for OpenSSL is currently a noop. 2557 MockCTVerifier::Call call = ct_verifier_->calls()[0];
2518 EXPECT_FALSE(sock->signed_cert_timestamps_received_); 2558 EXPECT_EQ("", call.stapled_ocsp_response);
Ryan Sleevi 2014/08/28 21:01:02 EXPECT_TRUE(.empty())
davidben 2014/08/28 21:23:25 Done.
2519 #endif 2559 EXPECT_EQ("test", call.sct_list_from_tls_extension);
2520 2560
2521 sock->Disconnect(); 2561 sock->Disconnect();
2522 EXPECT_FALSE(sock->IsConnected()); 2562 EXPECT_FALSE(sock->IsConnected());
2523 } 2563 }
2524 2564
2525 // Test that enabling Signed Certificate Timestamps enables OCSP stapling. 2565 // Test that enabling Signed Certificate Timestamps enables OCSP stapling.
2526 TEST_F(SSLClientSocketTest, ConnectSignedCertTimestampsEnabledOCSP) { 2566 TEST_F(SSLClientSocketTest, ConnectSignedCertTimestampsEnabledOCSP) {
2527 SpawnedTestServer::SSLOptions ssl_options; 2567 SpawnedTestServer::SSLOptions ssl_options;
2528 ssl_options.staple_ocsp_response = true; 2568 ssl_options.staple_ocsp_response = true;
2529 // The test server currently only knows how to generate OCSP responses 2569 // The test server currently only knows how to generate OCSP responses
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2563 CapturingNetLog::CapturedEntryList entries; 2603 CapturingNetLog::CapturedEntryList entries;
2564 log.GetEntries(&entries); 2604 log.GetEntries(&entries);
2565 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); 2605 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT));
2566 if (rv == ERR_IO_PENDING) 2606 if (rv == ERR_IO_PENDING)
2567 rv = callback.WaitForResult(); 2607 rv = callback.WaitForResult();
2568 EXPECT_EQ(OK, rv); 2608 EXPECT_EQ(OK, rv);
2569 EXPECT_TRUE(sock->IsConnected()); 2609 EXPECT_TRUE(sock->IsConnected());
2570 log.GetEntries(&entries); 2610 log.GetEntries(&entries);
2571 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); 2611 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
2572 2612
2573 #if !defined(USE_OPENSSL) 2613 // Check that the OCSP response was extracted in the expected format.
2574 EXPECT_TRUE(sock->stapled_ocsp_response_received_); 2614 EXPECT_TRUE(sock->stapled_ocsp_response_received_);
2575 #else 2615 EXPECT_EQ(1u, ct_verifier_->calls().size());
2576 // OCSP stapling isn't currently supported in the OpenSSL socket. 2616 MockCTVerifier::Call call = ct_verifier_->calls()[0];
2577 EXPECT_FALSE(sock->stapled_ocsp_response_received_); 2617 EXPECT_EQ("", call.sct_list_from_tls_extension);
Ryan Sleevi 2014/08/28 21:01:02 EXPECT_TRUE(.empty())
davidben 2014/08/28 21:23:25 Done.
2578 #endif 2618
2619 // The stapled OCSPResponse is generated each time. It should be the DER
2620 // encoding of an OCSPResponse (RFC 2560), so check that it consists of a
2621 // SEQUENCE of an ENUMERATED type and an element tagged with [0] EXPLICIT.
2622 base::StringPiece ocsp_response(call.stapled_ocsp_response);
2623 base::StringPiece sequence, response_status, response_bytes;
2624 EXPECT_TRUE(asn1::GetElement(&ocsp_response, asn1::kSEQUENCE, &sequence));
2625 EXPECT_TRUE(ocsp_response.empty());
2626 EXPECT_TRUE(asn1::GetElement(&sequence, asn1::kENUMERATED, &response_status));
2627 EXPECT_TRUE(asn1::GetElement(&sequence,
2628 asn1::kContextSpecific | asn1::kConstructed | 0,
2629 &response_status));
2630 EXPECT_TRUE(sequence.empty());
2579 2631
2580 sock->Disconnect(); 2632 sock->Disconnect();
2581 EXPECT_FALSE(sock->IsConnected()); 2633 EXPECT_FALSE(sock->IsConnected());
2582 } 2634 }
2583 2635
2584 TEST_F(SSLClientSocketTest, ConnectSignedCertTimestampsDisabled) { 2636 TEST_F(SSLClientSocketTest, ConnectSignedCertTimestampsDisabled) {
2585 SpawnedTestServer::SSLOptions ssl_options; 2637 SpawnedTestServer::SSLOptions ssl_options;
2586 ssl_options.signed_cert_timestamps_tls_ext = "test"; 2638 ssl_options.signed_cert_timestamps_tls_ext = "test";
2587 2639
2588 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, 2640 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS,
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
2990 ssl_config.channel_id_enabled = true; 3042 ssl_config.channel_id_enabled = true;
2991 3043
2992 int rv; 3044 int rv;
2993 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); 3045 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
2994 3046
2995 EXPECT_EQ(ERR_UNEXPECTED, rv); 3047 EXPECT_EQ(ERR_UNEXPECTED, rv);
2996 EXPECT_FALSE(sock_->IsConnected()); 3048 EXPECT_FALSE(sock_->IsConnected());
2997 } 3049 }
2998 3050
2999 } // namespace net 3051 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/ssl_client_socket_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698