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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/files/file_util.h" | |
10 #include "base/location.h" | 11 #include "base/location.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "net/base/address_list.h" | 18 #include "net/base/address_list.h" |
18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
42 #include "net/ssl/ssl_cert_request_info.h" | 43 #include "net/ssl/ssl_cert_request_info.h" |
43 #include "net/ssl/ssl_config_service.h" | 44 #include "net/ssl/ssl_config_service.h" |
44 #include "net/ssl/ssl_connection_status_flags.h" | 45 #include "net/ssl/ssl_connection_status_flags.h" |
45 #include "net/ssl/ssl_info.h" | 46 #include "net/ssl/ssl_info.h" |
46 #include "net/test/cert_test_util.h" | 47 #include "net/test/cert_test_util.h" |
47 #include "net/test/spawned_test_server/spawned_test_server.h" | 48 #include "net/test/spawned_test_server/spawned_test_server.h" |
48 #include "testing/gmock/include/gmock/gmock.h" | 49 #include "testing/gmock/include/gmock/gmock.h" |
49 #include "testing/gtest/include/gtest/gtest.h" | 50 #include "testing/gtest/include/gtest/gtest.h" |
50 #include "testing/platform_test.h" | 51 #include "testing/platform_test.h" |
51 | 52 |
53 #if defined(USE_OPENSSL) | |
54 #include <errno.h> | |
55 #include <openssl/bio.h> | |
56 #include <openssl/bn.h> | |
davidben
2016/01/28 22:24:10
Unused?
svaldez
2016/01/29 17:19:29
Done.
| |
57 #include <openssl/evp.h> | |
58 #include <openssl/pem.h> | |
59 #include <openssl/rsa.h> | |
davidben
2016/01/28 22:24:09
Unused?
svaldez
2016/01/29 17:19:29
Done.
| |
60 #include <string.h> | |
61 #include <utility> | |
davidben
2016/01/28 22:24:10
Already included up top.
svaldez
2016/01/29 17:19:30
Done.
| |
62 | |
63 #include "crypto/openssl_util.h" | |
davidben
2016/01/28 22:24:10
Unused?
svaldez
2016/01/29 17:19:29
Done.
| |
64 #include "crypto/scoped_openssl_types.h" | |
65 #include "net/ssl/ssl_platform_key_test.h" | |
66 #endif | |
67 | |
52 using testing::_; | 68 using testing::_; |
53 using testing::Return; | 69 using testing::Return; |
54 using testing::Truly; | 70 using testing::Truly; |
55 | 71 |
56 namespace net { | 72 namespace net { |
57 | 73 |
58 namespace { | 74 namespace { |
59 | 75 |
60 // WrappedStreamSocket is a base class that wraps an existing StreamSocket, | 76 // WrappedStreamSocket is a base class that wraps an existing StreamSocket, |
61 // forwarding the Socket and StreamSocket interfaces to the underlying | 77 // forwarding the Socket and StreamSocket interfaces to the underlying |
(...skipping 3171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3233 | 3249 |
3234 int rv; | 3250 int rv; |
3235 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); | 3251 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); |
3236 EXPECT_EQ(OK, rv); | 3252 EXPECT_EQ(OK, rv); |
3237 | 3253 |
3238 std::string proto; | 3254 std::string proto; |
3239 EXPECT_EQ(SSLClientSocket::kNextProtoUnsupported, | 3255 EXPECT_EQ(SSLClientSocket::kNextProtoUnsupported, |
3240 sock_->GetNextProto(&proto)); | 3256 sock_->GetNextProto(&proto)); |
3241 } | 3257 } |
3242 | 3258 |
3259 // These client auth tests are currently dependent on OpenSSL's struct X509. | |
davidben
2016/01/28 22:24:09
// Client auth is not supported in NSS ports.
or
svaldez
2016/01/29 17:19:30
Done.
| |
3260 #if defined(USE_OPENSSL) | |
3261 | |
3262 namespace { | |
3263 | |
3264 // Loads a PEM-encoded private key file into a SSLPrivateKey object. | |
3265 // |filepath| is the private key file path. | |
3266 // |*pkey| is reset to the new SSLPrivateKey on success, untouched otherwise. | |
3267 // Returns true on success, false on failure. | |
3268 bool LoadPrivateKeyOpenSSL(const base::FilePath& filepath, | |
3269 scoped_refptr<SSLPrivateKey>* pkey) { | |
davidben
2016/01/28 22:24:09
Why not just return a scoped_refptr<Thingy>?
svaldez
2016/01/29 17:19:30
Done.
| |
3270 std::string data; | |
3271 if (!base::ReadFileToString(filepath, &data)) { | |
3272 LOG(ERROR) << "Could not read private key file: " | |
3273 << filepath.value() << ": " << strerror(errno); | |
davidben
2016/01/28 22:24:10
I'd drop the errno bit. I'm not sure that works on
svaldez
2016/01/29 17:19:29
Done.
| |
3274 return false; | |
3275 } | |
3276 crypto::ScopedBIO bio(BIO_new_mem_buf( | |
3277 const_cast<char*>(reinterpret_cast<const char*>(data.data())), | |
davidben
2016/01/28 22:24:10
I think you only need the const_cast.
svaldez
2016/01/29 17:19:29
Done.
| |
3278 static_cast<int>(data.size()))); | |
3279 if (!bio.get()) { | |
davidben
2016/01/28 22:24:10
!bio also works.
svaldez
2016/01/29 17:19:30
Done.
| |
3280 LOG(ERROR) << "Could not allocate BIO for buffer?"; | |
3281 return false; | |
3282 } | |
3283 EVP_PKEY* result = PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL); | |
davidben
2016/01/28 22:24:09
crypto::ScopedEVP_PKEY result(PEM_read_bio_Private
svaldez
2016/01/29 17:19:30
Done.
| |
3284 if (result == NULL) { | |
3285 LOG(ERROR) << "Could not decode private key file: " | |
3286 << filepath.value(); | |
3287 return false; | |
3288 } | |
3289 *pkey = WrapOpenSSLPrivateKey(crypto::ScopedEVP_PKEY(result)); | |
3290 return true; | |
3291 } | |
3292 | |
3293 class SSLClientSocketOpenSSLClientAuthTest : public PlatformTest { | |
davidben
2016/01/28 22:24:09
I think this class is more-or-less a subset of SSL
svaldez
2016/01/29 17:19:29
Done.
| |
3294 public: | |
3295 SSLClientSocketOpenSSLClientAuthTest() | |
3296 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | |
3297 cert_verifier_(new MockCertVerifier), | |
3298 transport_security_state_(new TransportSecurityState) { | |
3299 cert_verifier_->set_default_result(OK); | |
3300 context_.cert_verifier = cert_verifier_.get(); | |
3301 context_.transport_security_state = transport_security_state_.get(); | |
3302 } | |
3303 | |
3304 ~SSLClientSocketOpenSSLClientAuthTest() override {} | |
3305 | |
3306 protected: | |
3307 scoped_ptr<SSLClientSocket> CreateSSLClientSocket( | |
3308 scoped_ptr<StreamSocket> transport_socket, | |
3309 const HostPortPair& host_and_port, | |
3310 const SSLConfig& ssl_config) { | |
3311 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | |
3312 connection->SetSocket(std::move(transport_socket)); | |
3313 return socket_factory_->CreateSSLClientSocket( | |
3314 std::move(connection), host_and_port, ssl_config, context_); | |
3315 } | |
3316 | |
3317 // Connect to a HTTPS test server. | |
3318 bool ConnectToTestServer(SpawnedTestServer::SSLOptions& ssl_options) { | |
3319 test_server_.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTPS, | |
3320 ssl_options, | |
3321 base::FilePath())); | |
3322 if (!test_server_->Start()) { | |
3323 LOG(ERROR) << "Could not start SpawnedTestServer"; | |
3324 return false; | |
3325 } | |
3326 | |
3327 if (!test_server_->GetAddressList(&addr_)) { | |
3328 LOG(ERROR) << "Could not get SpawnedTestServer address list"; | |
3329 return false; | |
3330 } | |
3331 | |
3332 transport_.reset(new TCPClientSocket( | |
3333 addr_, &log_, NetLog::Source())); | |
3334 int rv = callback_.GetResult( | |
3335 transport_->Connect(callback_.callback())); | |
3336 if (rv != OK) { | |
3337 LOG(ERROR) << "Could not connect to SpawnedTestServer"; | |
3338 return false; | |
3339 } | |
3340 return true; | |
3341 } | |
3342 | |
3343 // Create an SSLClientSocket object and use it to connect to a test | |
3344 // server, then wait for connection results. This must be called after | |
3345 // a succesful ConnectToTestServer() call. | |
3346 // |ssl_config| the SSL configuration to use. | |
3347 // |result| will retrieve the ::Connect() result value. | |
3348 // Returns true on succes, false otherwise. Success means that the socket | |
3349 // could be created and its Connect() was called, not that the connection | |
3350 // itself was a success. | |
3351 bool CreateAndConnectSSLClientSocket(const SSLConfig& ssl_config, | |
3352 int* result) { | |
3353 sock_ = CreateSSLClientSocket(std::move(transport_), | |
3354 test_server_->host_port_pair(), ssl_config); | |
3355 | |
3356 if (sock_->IsConnected()) { | |
3357 LOG(ERROR) << "SSL Socket prematurely connected"; | |
3358 return false; | |
3359 } | |
3360 | |
3361 *result = callback_.GetResult(sock_->Connect(callback_.callback())); | |
3362 return true; | |
3363 } | |
3364 | |
3365 | |
3366 // Check that the client certificate was sent. | |
3367 // Returns true on success. | |
3368 bool CheckSSLClientSocketSentCert() { | |
3369 SSLInfo ssl_info; | |
3370 sock_->GetSSLInfo(&ssl_info); | |
3371 return ssl_info.client_cert_sent; | |
3372 } | |
3373 | |
3374 ClientSocketFactory* socket_factory_; | |
3375 scoped_ptr<MockCertVerifier> cert_verifier_; | |
3376 scoped_ptr<TransportSecurityState> transport_security_state_; | |
3377 SSLClientSocketContext context_; | |
3378 scoped_ptr<SpawnedTestServer> test_server_; | |
3379 AddressList addr_; | |
3380 TestCompletionCallback callback_; | |
3381 NetLog log_; | |
3382 scoped_ptr<StreamSocket> transport_; | |
3383 scoped_ptr<SSLClientSocket> sock_; | |
3384 }; | |
3385 | |
3386 // Connect to a server requesting client authentication, do not send | |
3387 // any client certificates. It should refuse the connection. | |
3388 TEST_F(SSLClientSocketOpenSSLClientAuthTest, NoCert) { | |
davidben
2016/01/28 22:24:10
Why is this test in the anonymous namespace and no
svaldez
2016/01/29 17:19:30
Done.
| |
3389 SpawnedTestServer::SSLOptions ssl_options; | |
3390 ssl_options.request_client_certificate = true; | |
3391 | |
3392 ASSERT_TRUE(ConnectToTestServer(ssl_options)); | |
3393 | |
3394 base::FilePath certs_dir = GetTestCertsDirectory(); | |
3395 | |
3396 int rv; | |
3397 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | |
3398 | |
3399 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | |
3400 EXPECT_FALSE(sock_->IsConnected()); | |
3401 } | |
3402 | |
3403 } // namespace | |
3404 | |
3405 // Connect to a server requesting client authentication, and send it | |
3406 // an empty certificate. It should refuse the connection. | |
davidben
2016/01/28 22:24:09
It doesn't seem to actually refuse the connection.
davidben
2016/01/28 22:24:10
an empty -> no
svaldez
2016/01/29 17:19:30
This sends an empty cert (send_client_cert is true
| |
3407 TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendEmptyCert) { | |
3408 SpawnedTestServer::SSLOptions ssl_options; | |
3409 ssl_options.request_client_certificate = true; | |
3410 ssl_options.client_authorities.push_back( | |
3411 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); | |
3412 | |
3413 ASSERT_TRUE(ConnectToTestServer(ssl_options)); | |
3414 | |
3415 base::FilePath certs_dir = GetTestCertsDirectory(); | |
davidben
2016/01/28 22:24:09
This looks unused.
svaldez
2016/01/29 17:19:29
Done.
| |
3416 SSLConfig ssl_config; | |
3417 ssl_config.send_client_cert = true; | |
3418 ssl_config.client_cert = NULL; | |
davidben
2016/01/28 22:24:09
nullptr
svaldez
2016/01/29 17:19:29
Done.
| |
3419 ssl_config.client_private_key = NULL; | |
3420 | |
3421 int rv; | |
3422 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
3423 | |
3424 EXPECT_EQ(OK, rv); | |
3425 EXPECT_TRUE(sock_->IsConnected()); | |
3426 } | |
3427 | |
3428 // Connect to a server requesting client authentication. Send it a | |
3429 // matching certificate. It should allow the connection. | |
3430 TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendGoodCert) { | |
3431 SpawnedTestServer::SSLOptions ssl_options; | |
3432 ssl_options.request_client_certificate = true; | |
3433 ssl_options.client_authorities.push_back( | |
3434 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); | |
3435 | |
3436 ASSERT_TRUE(ConnectToTestServer(ssl_options)); | |
3437 | |
3438 base::FilePath certs_dir = GetTestCertsDirectory(); | |
3439 SSLConfig ssl_config; | |
3440 ssl_config.send_client_cert = true; | |
3441 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); | |
3442 | |
3443 // This is required to ensure that signing works with the client | |
3444 // certificate's private key. | |
3445 ASSERT_TRUE(LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"), | |
3446 &(ssl_config.client_private_key))); | |
3447 | |
3448 int rv; | |
3449 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
3450 | |
3451 EXPECT_EQ(OK, rv); | |
3452 EXPECT_TRUE(sock_->IsConnected()); | |
3453 | |
3454 EXPECT_TRUE(CheckSSLClientSocketSentCert()); | |
3455 | |
3456 sock_->Disconnect(); | |
3457 EXPECT_FALSE(sock_->IsConnected()); | |
3458 } | |
3459 #endif // defined(USE_OPENSSL) | |
3460 | |
3243 } // namespace net | 3461 } // namespace net |
OLD | NEW |