| 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/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <openssl/bio.h> | 10 #include <openssl/bio.h> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "net/base/test_completion_callback.h" | 28 #include "net/base/test_completion_callback.h" |
| 29 #include "net/base/test_data_directory.h" | 29 #include "net/base/test_data_directory.h" |
| 30 #include "net/cert/mock_cert_verifier.h" | 30 #include "net/cert/mock_cert_verifier.h" |
| 31 #include "net/cert/test_root_certs.h" | 31 #include "net/cert/test_root_certs.h" |
| 32 #include "net/dns/host_resolver.h" | 32 #include "net/dns/host_resolver.h" |
| 33 #include "net/http/transport_security_state.h" | 33 #include "net/http/transport_security_state.h" |
| 34 #include "net/socket/client_socket_factory.h" | 34 #include "net/socket/client_socket_factory.h" |
| 35 #include "net/socket/client_socket_handle.h" | 35 #include "net/socket/client_socket_handle.h" |
| 36 #include "net/socket/socket_test_util.h" | 36 #include "net/socket/socket_test_util.h" |
| 37 #include "net/socket/tcp_client_socket.h" | 37 #include "net/socket/tcp_client_socket.h" |
| 38 #include "net/ssl/default_server_bound_cert_store.h" | |
| 39 #include "net/ssl/openssl_client_key_store.h" | 38 #include "net/ssl/openssl_client_key_store.h" |
| 40 #include "net/ssl/server_bound_cert_service.h" | |
| 41 #include "net/ssl/ssl_cert_request_info.h" | 39 #include "net/ssl/ssl_cert_request_info.h" |
| 42 #include "net/ssl/ssl_config_service.h" | 40 #include "net/ssl/ssl_config_service.h" |
| 43 #include "net/test/cert_test_util.h" | 41 #include "net/test/cert_test_util.h" |
| 44 #include "net/test/spawned_test_server/spawned_test_server.h" | 42 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 45 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 46 #include "testing/platform_test.h" | 44 #include "testing/platform_test.h" |
| 47 | 45 |
| 48 namespace net { | 46 namespace net { |
| 49 | 47 |
| 50 namespace { | 48 namespace { |
| 51 | 49 |
| 52 // These client auth tests are currently dependent on OpenSSL's struct X509. | 50 // These client auth tests are currently dependent on OpenSSL's struct X509. |
| 53 #if defined(USE_OPENSSL_CERTS) | 51 #if defined(USE_OPENSSL_CERTS) |
| 54 typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY; | 52 typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY; |
| 55 | 53 |
| 56 // BIO_free is a macro, it can't be used as a template parameter. | 54 // BIO_free is a macro, it can't be used as a template parameter. |
| 57 void BIO_free_func(BIO* bio) { | 55 void BIO_free_func(BIO* bio) { |
| 58 BIO_free(bio); | 56 BIO_free(bio); |
| 59 } | 57 } |
| 60 | 58 |
| 61 typedef crypto::ScopedOpenSSL<BIO, BIO_free_func> ScopedBIO; | 59 typedef crypto::ScopedOpenSSL<BIO, BIO_free_func> ScopedBIO; |
| 62 typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA; | 60 typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA; |
| 63 typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM; | 61 typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM; |
| 64 | 62 |
| 65 const SSLConfig kDefaultSSLConfig; | 63 const SSLConfig kDefaultSSLConfig; |
| 66 | 64 |
| 67 // A ServerBoundCertStore that always returns an error when asked for a | |
| 68 // certificate. | |
| 69 class FailingServerBoundCertStore : public ServerBoundCertStore { | |
| 70 virtual int GetServerBoundCert(const std::string& server_identifier, | |
| 71 base::Time* expiration_time, | |
| 72 std::string* private_key_result, | |
| 73 std::string* cert_result, | |
| 74 const GetCertCallback& callback) OVERRIDE { | |
| 75 return ERR_UNEXPECTED; | |
| 76 } | |
| 77 virtual void SetServerBoundCert(const std::string& server_identifier, | |
| 78 base::Time creation_time, | |
| 79 base::Time expiration_time, | |
| 80 const std::string& private_key, | |
| 81 const std::string& cert) OVERRIDE {} | |
| 82 virtual void DeleteServerBoundCert(const std::string& server_identifier, | |
| 83 const base::Closure& completion_callback) | |
| 84 OVERRIDE {} | |
| 85 virtual void DeleteAllCreatedBetween(base::Time delete_begin, | |
| 86 base::Time delete_end, | |
| 87 const base::Closure& completion_callback) | |
| 88 OVERRIDE {} | |
| 89 virtual void DeleteAll(const base::Closure& completion_callback) OVERRIDE {} | |
| 90 virtual void GetAllServerBoundCerts(const GetCertListCallback& callback) | |
| 91 OVERRIDE {} | |
| 92 virtual int GetCertCount() OVERRIDE { return 0; } | |
| 93 virtual void SetForceKeepSessionState() OVERRIDE {} | |
| 94 }; | |
| 95 | |
| 96 // Loads a PEM-encoded private key file into a scoped EVP_PKEY object. | 65 // Loads a PEM-encoded private key file into a scoped EVP_PKEY object. |
| 97 // |filepath| is the private key file path. | 66 // |filepath| is the private key file path. |
| 98 // |*pkey| is reset to the new EVP_PKEY on success, untouched otherwise. | 67 // |*pkey| is reset to the new EVP_PKEY on success, untouched otherwise. |
| 99 // Returns true on success, false on failure. | 68 // Returns true on success, false on failure. |
| 100 bool LoadPrivateKeyOpenSSL( | 69 bool LoadPrivateKeyOpenSSL( |
| 101 const base::FilePath& filepath, | 70 const base::FilePath& filepath, |
| 102 OpenSSLClientKeyStore::ScopedEVP_PKEY* pkey) { | 71 OpenSSLClientKeyStore::ScopedEVP_PKEY* pkey) { |
| 103 std::string data; | 72 std::string data; |
| 104 if (!base::ReadFileToString(filepath, &data)) { | 73 if (!base::ReadFileToString(filepath, &data)) { |
| 105 LOG(ERROR) << "Could not read private key file: " | 74 LOG(ERROR) << "Could not read private key file: " |
| (...skipping 28 matching lines...) Expand all Loading... |
| 134 context_.cert_verifier = cert_verifier_.get(); | 103 context_.cert_verifier = cert_verifier_.get(); |
| 135 context_.transport_security_state = transport_security_state_.get(); | 104 context_.transport_security_state = transport_security_state_.get(); |
| 136 key_store_ = net::OpenSSLClientKeyStore::GetInstance(); | 105 key_store_ = net::OpenSSLClientKeyStore::GetInstance(); |
| 137 } | 106 } |
| 138 | 107 |
| 139 virtual ~SSLClientSocketOpenSSLClientAuthTest() { | 108 virtual ~SSLClientSocketOpenSSLClientAuthTest() { |
| 140 key_store_->Flush(); | 109 key_store_->Flush(); |
| 141 } | 110 } |
| 142 | 111 |
| 143 protected: | 112 protected: |
| 144 void EnabledChannelID() { | |
| 145 cert_service_.reset( | |
| 146 new ServerBoundCertService(new DefaultServerBoundCertStore(NULL), | |
| 147 base::MessageLoopProxy::current())); | |
| 148 context_.server_bound_cert_service = cert_service_.get(); | |
| 149 } | |
| 150 | |
| 151 void EnabledFailingChannelID() { | |
| 152 cert_service_.reset( | |
| 153 new ServerBoundCertService(new FailingServerBoundCertStore(), | |
| 154 base::MessageLoopProxy::current())); | |
| 155 context_.server_bound_cert_service = cert_service_.get(); | |
| 156 } | |
| 157 | |
| 158 scoped_ptr<SSLClientSocket> CreateSSLClientSocket( | 113 scoped_ptr<SSLClientSocket> CreateSSLClientSocket( |
| 159 scoped_ptr<StreamSocket> transport_socket, | 114 scoped_ptr<StreamSocket> transport_socket, |
| 160 const HostPortPair& host_and_port, | 115 const HostPortPair& host_and_port, |
| 161 const SSLConfig& ssl_config) { | 116 const SSLConfig& ssl_config) { |
| 162 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | 117 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 163 connection->SetSocket(transport_socket.Pass()); | 118 connection->SetSocket(transport_socket.Pass()); |
| 164 return socket_factory_->CreateSSLClientSocket(connection.Pass(), | 119 return socket_factory_->CreateSSLClientSocket(connection.Pass(), |
| 165 host_and_port, | 120 host_and_port, |
| 166 ssl_config, | 121 ssl_config, |
| 167 context_); | 122 context_); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 184 |
| 230 | 185 |
| 231 // Check that the client certificate was sent. | 186 // Check that the client certificate was sent. |
| 232 // Returns true on success. | 187 // Returns true on success. |
| 233 bool CheckSSLClientSocketSentCert() { | 188 bool CheckSSLClientSocketSentCert() { |
| 234 SSLInfo ssl_info; | 189 SSLInfo ssl_info; |
| 235 sock_->GetSSLInfo(&ssl_info); | 190 sock_->GetSSLInfo(&ssl_info); |
| 236 return ssl_info.client_cert_sent; | 191 return ssl_info.client_cert_sent; |
| 237 } | 192 } |
| 238 | 193 |
| 239 scoped_ptr<ServerBoundCertService> cert_service_; | |
| 240 ClientSocketFactory* socket_factory_; | 194 ClientSocketFactory* socket_factory_; |
| 241 scoped_ptr<MockCertVerifier> cert_verifier_; | 195 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 242 scoped_ptr<TransportSecurityState> transport_security_state_; | 196 scoped_ptr<TransportSecurityState> transport_security_state_; |
| 243 SSLClientSocketContext context_; | 197 SSLClientSocketContext context_; |
| 244 OpenSSLClientKeyStore* key_store_; | 198 OpenSSLClientKeyStore* key_store_; |
| 245 scoped_ptr<SpawnedTestServer> test_server_; | 199 scoped_ptr<SpawnedTestServer> test_server_; |
| 246 AddressList addr_; | 200 AddressList addr_; |
| 247 TestCompletionCallback callback_; | 201 TestCompletionCallback callback_; |
| 248 CapturingNetLog log_; | 202 CapturingNetLog log_; |
| 249 scoped_ptr<StreamSocket> transport_; | 203 scoped_ptr<StreamSocket> transport_; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 270 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
| 317 | 271 |
| 318 EXPECT_EQ(OK, rv); | 272 EXPECT_EQ(OK, rv); |
| 319 EXPECT_TRUE(sock_->IsConnected()); | 273 EXPECT_TRUE(sock_->IsConnected()); |
| 320 | 274 |
| 321 EXPECT_TRUE(CheckSSLClientSocketSentCert()); | 275 EXPECT_TRUE(CheckSSLClientSocketSentCert()); |
| 322 | 276 |
| 323 sock_->Disconnect(); | 277 sock_->Disconnect(); |
| 324 EXPECT_FALSE(sock_->IsConnected()); | 278 EXPECT_FALSE(sock_->IsConnected()); |
| 325 } | 279 } |
| 326 | |
| 327 // Connect to a server using channel id. It should allow the connection. | |
| 328 TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendChannelID) { | |
| 329 SpawnedTestServer::SSLOptions ssl_options; | |
| 330 | |
| 331 ASSERT_TRUE(ConnectToTestServer(ssl_options)); | |
| 332 | |
| 333 EnabledChannelID(); | |
| 334 SSLConfig ssl_config = kDefaultSSLConfig; | |
| 335 ssl_config.channel_id_enabled = true; | |
| 336 | |
| 337 int rv; | |
| 338 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
| 339 | |
| 340 EXPECT_EQ(OK, rv); | |
| 341 EXPECT_TRUE(sock_->IsConnected()); | |
| 342 EXPECT_TRUE(sock_->WasChannelIDSent()); | |
| 343 | |
| 344 sock_->Disconnect(); | |
| 345 EXPECT_FALSE(sock_->IsConnected()); | |
| 346 } | |
| 347 | |
| 348 // Connect to a server using channel id but without sending a key. It should | |
| 349 // fail. | |
| 350 TEST_F(SSLClientSocketOpenSSLClientAuthTest, FailingChannelID) { | |
| 351 SpawnedTestServer::SSLOptions ssl_options; | |
| 352 | |
| 353 ASSERT_TRUE(ConnectToTestServer(ssl_options)); | |
| 354 | |
| 355 EnabledFailingChannelID(); | |
| 356 SSLConfig ssl_config = kDefaultSSLConfig; | |
| 357 ssl_config.channel_id_enabled = true; | |
| 358 | |
| 359 int rv; | |
| 360 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
| 361 | |
| 362 EXPECT_EQ(ERR_UNEXPECTED, rv); | |
| 363 EXPECT_FALSE(sock_->IsConnected()); | |
| 364 } | |
| 365 #endif // defined(USE_OPENSSL_CERTS) | 280 #endif // defined(USE_OPENSSL_CERTS) |
| 366 | 281 |
| 367 } // namespace | 282 } // namespace |
| 368 } // namespace net | 283 } // namespace net |
| OLD | NEW |