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/evp.h> | |
57 #include <openssl/pem.h> | |
58 #include <string.h> | |
59 | |
60 #include "crypto/scoped_openssl_types.h" | |
61 #include "net/ssl/test_ssl_private_key.h" | |
62 #endif | |
63 | |
52 using testing::_; | 64 using testing::_; |
53 using testing::Return; | 65 using testing::Return; |
54 using testing::Truly; | 66 using testing::Truly; |
55 | 67 |
56 namespace net { | 68 namespace net { |
57 | 69 |
58 namespace { | 70 namespace { |
59 | 71 |
60 // WrappedStreamSocket is a base class that wraps an existing StreamSocket, | 72 // WrappedStreamSocket is a base class that wraps an existing StreamSocket, |
61 // forwarding the Socket and StreamSocket interfaces to the underlying | 73 // forwarding the Socket and StreamSocket interfaces to the underlying |
(...skipping 3171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3233 | 3245 |
3234 int rv; | 3246 int rv; |
3235 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); | 3247 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); |
3236 EXPECT_EQ(OK, rv); | 3248 EXPECT_EQ(OK, rv); |
3237 | 3249 |
3238 std::string proto; | 3250 std::string proto; |
3239 EXPECT_EQ(SSLClientSocket::kNextProtoUnsupported, | 3251 EXPECT_EQ(SSLClientSocket::kNextProtoUnsupported, |
3240 sock_->GetNextProto(&proto)); | 3252 sock_->GetNextProto(&proto)); |
3241 } | 3253 } |
3242 | 3254 |
3255 // Client auth is not supported in NSS ports. | |
3256 #if defined(USE_OPENSSL) | |
3257 | |
3258 namespace { | |
3259 | |
3260 // Loads a PEM-encoded private key file into a SSLPrivateKey object. | |
3261 // |filepath| is the private key file path. | |
3262 // Returns the new SSLPrivateKey. | |
3263 scoped_refptr<SSLPrivateKey> LoadPrivateKeyOpenSSL( | |
3264 const base::FilePath& filepath) { | |
3265 std::string data; | |
3266 if (!base::ReadFileToString(filepath, &data)) { | |
3267 LOG(ERROR) << "Could not read private key file: " << filepath.value(); | |
3268 return nullptr; | |
3269 } | |
3270 crypto::ScopedBIO bio(BIO_new_mem_buf(const_cast<char*>(data.data()), | |
3271 static_cast<int>(data.size()))); | |
3272 if (!bio) { | |
3273 LOG(ERROR) << "Could not allocate BIO for buffer?"; | |
3274 return nullptr; | |
3275 } | |
3276 crypto::ScopedEVP_PKEY result( | |
3277 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); | |
3278 if (!result) { | |
3279 LOG(ERROR) << "Could not decode private key file: " << filepath.value(); | |
3280 return nullptr; | |
3281 } | |
3282 return WrapOpenSSLPrivateKey(std::move(result)); | |
3283 } | |
3284 | |
3285 } // namespace | |
3286 | |
3287 // Connect to a server requesting client authentication, do not send | |
3288 // any client certificates. It should refuse the connection. | |
3289 TEST_F(SSLClientSocketTest, NoCert) { | |
3290 SpawnedTestServer::SSLOptions ssl_options; | |
3291 ssl_options.request_client_certificate = true; | |
3292 ASSERT_TRUE(StartTestServer(ssl_options)); | |
3293 | |
3294 int rv; | |
3295 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | |
3296 | |
3297 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | |
3298 EXPECT_FALSE(sock_->IsConnected()); | |
3299 } | |
3300 | |
3301 // Connect to a server requesting client authentication, and send it | |
3302 // an empty certificate. | |
3303 TEST_F(SSLClientSocketTest, SendEmptyCert) { | |
3304 SpawnedTestServer::SSLOptions ssl_options; | |
3305 ssl_options.request_client_certificate = true; | |
3306 ssl_options.client_authorities.push_back( | |
3307 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); | |
3308 | |
3309 ASSERT_TRUE(StartTestServer(ssl_options)); | |
3310 | |
3311 SSLConfig ssl_config; | |
3312 ssl_config.send_client_cert = true; | |
3313 ssl_config.client_cert = nullptr; | |
3314 ssl_config.client_private_key = nullptr; | |
3315 | |
3316 int rv; | |
3317 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
3318 | |
davidben
2016/01/29 19:19:56
Probably worth an EXPECT_FALSE (or whichever it ac
| |
3319 EXPECT_EQ(OK, rv); | |
3320 EXPECT_TRUE(sock_->IsConnected()); | |
3321 } | |
3322 | |
3323 // Connect to a server requesting client authentication. Send it a | |
3324 // matching certificate. It should allow the connection. | |
3325 TEST_F(SSLClientSocketTest, SendGoodCert) { | |
3326 SpawnedTestServer::SSLOptions ssl_options; | |
3327 ssl_options.request_client_certificate = true; | |
3328 ssl_options.client_authorities.push_back( | |
3329 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); | |
3330 | |
3331 ASSERT_TRUE(StartTestServer(ssl_options)); | |
3332 | |
3333 base::FilePath certs_dir = GetTestCertsDirectory(); | |
3334 SSLConfig ssl_config; | |
3335 ssl_config.send_client_cert = true; | |
3336 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); | |
3337 | |
3338 // This is required to ensure that signing works with the client | |
3339 // certificate's private key. | |
3340 ssl_config.client_private_key = | |
3341 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key")); | |
3342 | |
3343 int rv; | |
3344 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
3345 | |
3346 EXPECT_EQ(OK, rv); | |
3347 EXPECT_TRUE(sock_->IsConnected()); | |
3348 | |
3349 SSLInfo ssl_info; | |
3350 ASSERT_TRUE(sock_->GetSSLInfo(&ssl_info)); | |
3351 EXPECT_TRUE(ssl_info.client_cert_sent); | |
3352 | |
3353 sock_->Disconnect(); | |
3354 EXPECT_FALSE(sock_->IsConnected()); | |
3355 } | |
3356 #endif // defined(USE_OPENSSL) | |
3357 | |
3243 } // namespace net | 3358 } // namespace net |
OLD | NEW |