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

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

Issue 1474983003: Support for client certs in ssl_server_socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Free results from SSL_load_client_CA_file Created 4 years, 10 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
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 // This test suite uses SSLClientSocket to test the implementation of 5 // This test suite uses SSLClientSocket to test the implementation of
6 // SSLServerSocket. In order to establish connections between the sockets 6 // SSLServerSocket. In order to establish connections between the sockets
7 // we need two additional classes: 7 // we need two additional classes:
8 // 1. FakeSocket 8 // 1. FakeSocket
9 // Connects SSL socket to FakeDataChannel. This class is just a stub. 9 // Connects SSL socket to FakeDataChannel. This class is just a stub.
10 // 10 //
11 // 2. FakeDataChannel 11 // 2. FakeDataChannel
12 // Implements the actual exchange of data between two FakeSockets. 12 // Implements the actual exchange of data between two FakeSockets.
13 // 13 //
14 // Implementations of these two classes are included in this file. 14 // Implementations of these two classes are included in this file.
15 15
16 #include "net/socket/ssl_server_socket.h" 16 #include "net/socket/ssl_server_socket.h"
17 17
18 #if defined(USE_OPENSSL)
19 #include <openssl/evp.h>
20 #include <openssl/ssl.h>
21 #include <openssl/x509.h>
22 #endif
davidben 2016/02/04 00:40:11 Move this after line 67. The problem is ssl_server
ryanchung 2016/02/05 01:56:13 Done.
23
18 #include <stdint.h> 24 #include <stdint.h>
19 #include <stdlib.h> 25 #include <stdlib.h>
20 #include <queue> 26 #include <queue>
21 #include <utility> 27 #include <utility>
22 28
23 #include "base/compiler_specific.h" 29 #include "base/compiler_specific.h"
24 #include "base/files/file_path.h" 30 #include "base/files/file_path.h"
25 #include "base/files/file_util.h" 31 #include "base/files/file_util.h"
26 #include "base/location.h" 32 #include "base/location.h"
27 #include "base/logging.h" 33 #include "base/logging.h"
28 #include "base/macros.h" 34 #include "base/macros.h"
29 #include "base/message_loop/message_loop.h" 35 #include "base/message_loop/message_loop.h"
30 #include "base/single_thread_task_runner.h" 36 #include "base/single_thread_task_runner.h"
31 #include "base/thread_task_runner_handle.h" 37 #include "base/thread_task_runner_handle.h"
38 #include "build/build_config.h"
32 #include "crypto/nss_util.h" 39 #include "crypto/nss_util.h"
33 #include "crypto/rsa_private_key.h" 40 #include "crypto/rsa_private_key.h"
41 #include "crypto/signature_creator.h"
34 #include "net/base/address_list.h" 42 #include "net/base/address_list.h"
35 #include "net/base/completion_callback.h" 43 #include "net/base/completion_callback.h"
36 #include "net/base/host_port_pair.h" 44 #include "net/base/host_port_pair.h"
37 #include "net/base/io_buffer.h" 45 #include "net/base/io_buffer.h"
38 #include "net/base/ip_endpoint.h" 46 #include "net/base/ip_endpoint.h"
39 #include "net/base/net_errors.h" 47 #include "net/base/net_errors.h"
40 #include "net/base/test_data_directory.h" 48 #include "net/base/test_data_directory.h"
41 #include "net/cert/cert_status_flags.h" 49 #include "net/cert/cert_status_flags.h"
42 #include "net/cert/mock_cert_verifier.h" 50 #include "net/cert/mock_cert_verifier.h"
51 #include "net/cert/mock_client_cert_verifier.h"
43 #include "net/cert/x509_certificate.h" 52 #include "net/cert/x509_certificate.h"
44 #include "net/http/transport_security_state.h" 53 #include "net/http/transport_security_state.h"
45 #include "net/log/net_log.h" 54 #include "net/log/net_log.h"
46 #include "net/socket/client_socket_factory.h" 55 #include "net/socket/client_socket_factory.h"
47 #include "net/socket/socket_test_util.h" 56 #include "net/socket/socket_test_util.h"
48 #include "net/socket/ssl_client_socket.h" 57 #include "net/socket/ssl_client_socket.h"
49 #include "net/socket/stream_socket.h" 58 #include "net/socket/stream_socket.h"
59 #include "net/ssl/ssl_cert_request_info.h"
50 #include "net/ssl/ssl_cipher_suite_names.h" 60 #include "net/ssl/ssl_cipher_suite_names.h"
51 #include "net/ssl/ssl_connection_status_flags.h" 61 #include "net/ssl/ssl_connection_status_flags.h"
52 #include "net/ssl/ssl_info.h" 62 #include "net/ssl/ssl_info.h"
63 #include "net/ssl/ssl_private_key.h"
53 #include "net/ssl/ssl_server_config.h" 64 #include "net/ssl/ssl_server_config.h"
54 #include "net/test/cert_test_util.h" 65 #include "net/test/cert_test_util.h"
55 #include "testing/gtest/include/gtest/gtest.h" 66 #include "testing/gtest/include/gtest/gtest.h"
56 #include "testing/platform_test.h" 67 #include "testing/platform_test.h"
57 68
58 namespace net { 69 namespace net {
59 70
60 namespace { 71 namespace {
61 72
73 const char kClientCertFileName[] = "client_1.pem";
74 const char kClientPrivateKeyFileName[] = "client_1.pk8";
75 const char kWrongClientCertFileName[] = "client_2.pem";
76 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8";
77 const char kClientCertCAFileName[] = "client_1_ca.pem";
78
62 class FakeDataChannel { 79 class FakeDataChannel {
63 public: 80 public:
64 FakeDataChannel() 81 FakeDataChannel()
65 : read_buf_len_(0), 82 : read_buf_len_(0),
66 closed_(false), 83 closed_(false),
67 write_called_after_close_(false), 84 write_called_after_close_(false),
68 weak_factory_(this) { 85 weak_factory_(this) {
69 } 86 }
70 87
71 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { 88 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 weak_factory_.GetWeakPtr())); 120 weak_factory_.GetWeakPtr()));
104 return buf_len; 121 return buf_len;
105 } 122 }
106 123
107 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, 124 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
108 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that 125 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
109 // after the FakeDataChannel is closed, the first Write() call completes 126 // after the FakeDataChannel is closed, the first Write() call completes
110 // asynchronously, which is necessary to reproduce bug 127822. 127 // asynchronously, which is necessary to reproduce bug 127822.
111 void Close() { 128 void Close() {
112 closed_ = true; 129 closed_ = true;
130 data_.push(
131 new DrainableIOBuffer(new StringIOBuffer(std::string("0", 1)), 1));
davidben 2016/02/04 00:40:11 We chatted over IM, but rather than push fake data
ryanchung 2016/02/05 01:56:13 Done. That works.
132 if (!read_callback_.is_null()) {
133 base::ThreadTaskRunnerHandle::Get()->PostTask(
134 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
135 weak_factory_.GetWeakPtr()));
136 }
113 } 137 }
114 138
115 private: 139 private:
116 void DoReadCallback() { 140 void DoReadCallback() {
117 if (read_callback_.is_null() || data_.empty()) 141 if (read_callback_.is_null() || data_.empty())
118 return; 142 return;
119
davidben 2016/02/04 00:40:11 Stray backspace? (Unless you intentionally remove
ryanchung 2016/02/05 01:56:13 Done. Unintentional.
120 int copied = PropagateData(read_buf_, read_buf_len_); 143 int copied = PropagateData(read_buf_, read_buf_len_);
121 CompletionCallback callback = read_callback_; 144 CompletionCallback callback = read_callback_;
122 read_callback_.Reset(); 145 read_callback_.Reset();
123 read_buf_ = NULL; 146 read_buf_ = NULL;
124 read_buf_len_ = 0; 147 read_buf_len_ = 0;
125 callback.Run(copied); 148 callback.Run(copied);
126 } 149 }
127 150
128 void DoWriteCallback() { 151 void DoWriteCallback() {
129 if (write_callback_.is_null()) 152 if (write_callback_.is_null())
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 } 271 }
249 272
250 private: 273 private:
251 BoundNetLog net_log_; 274 BoundNetLog net_log_;
252 FakeDataChannel* incoming_; 275 FakeDataChannel* incoming_;
253 FakeDataChannel* outgoing_; 276 FakeDataChannel* outgoing_;
254 277
255 DISALLOW_COPY_AND_ASSIGN(FakeSocket); 278 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
256 }; 279 };
257 280
281 #if defined(USE_OPENSSL)
282 class TestSSLPrivateKey : public SSLPrivateKey {
davidben 2016/02/04 00:40:11 If you rebase, you can replace this class with a c
ryanchung 2016/02/05 01:56:13 Done.
283 public:
284 TestSSLPrivateKey(scoped_ptr<crypto::RSAPrivateKey> rsa_private_key)
285 : rsa_private_key_(std::move(rsa_private_key)) {}
286
287 Type GetType() override { return SSLPrivateKey::Type::RSA; }
288
289 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override {
290 static const SSLPrivateKey::Hash kHashes[] = {SSLPrivateKey::Hash::SHA256,
291 SSLPrivateKey::Hash::SHA1};
292 return std::vector<SSLPrivateKey::Hash>(std::begin(kHashes),
293 std::end(kHashes));
294 }
295
296 size_t GetMaxSignatureLengthInBytes() override {
297 return EVP_PKEY_size(rsa_private_key_->key());
298 }
299
300 void SignDigest(Hash hash,
301 const base::StringPiece& input,
302 const SignCallback& callback) override {
303 std::vector<uint8_t> signature;
304 crypto::SignatureCreator::HashAlgorithm hash_alg;
305 switch (hash) {
306 case Hash::SHA1:
307 hash_alg = crypto::SignatureCreator::SHA1;
308 break;
309
310 case Hash::SHA256:
311 hash_alg = crypto::SignatureCreator::SHA256;
312 break;
313
314 default:
315 FAIL() << "Unsupported hash function";
316 }
317 crypto::SignatureCreator::Sign(
318 rsa_private_key_.get(), hash_alg,
319 reinterpret_cast<const uint8_t*>(input.data()), input.size(),
320 &signature);
321 base::ThreadTaskRunnerHandle::Get()->PostTask(
322 FROM_HERE, base::Bind(callback, OK, signature));
323 }
324
325 private:
326 ~TestSSLPrivateKey() override {}
327
328 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_;
329
330 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey);
331 };
332 #endif
333
258 } // namespace 334 } // namespace
259 335
260 // Verify the correctness of the test helper classes first. 336 // Verify the correctness of the test helper classes first.
261 TEST(FakeSocketTest, DataTransfer) { 337 TEST(FakeSocketTest, DataTransfer) {
262 // Establish channels between two sockets. 338 // Establish channels between two sockets.
263 FakeDataChannel channel_1; 339 FakeDataChannel channel_1;
264 FakeDataChannel channel_2; 340 FakeDataChannel channel_2;
265 FakeSocket client(&channel_1, &channel_2); 341 FakeSocket client(&channel_1, &channel_2);
266 FakeSocket server(&channel_2, &channel_1); 342 FakeSocket server(&channel_2, &channel_1);
267 343
(...skipping 27 matching lines...) Expand all
295 EXPECT_GT(read, 0); 371 EXPECT_GT(read, 0);
296 EXPECT_LE(read, written); 372 EXPECT_LE(read, written);
297 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); 373 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
298 } 374 }
299 375
300 class SSLServerSocketTest : public PlatformTest { 376 class SSLServerSocketTest : public PlatformTest {
301 public: 377 public:
302 SSLServerSocketTest() 378 SSLServerSocketTest()
303 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), 379 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
304 cert_verifier_(new MockCertVerifier()), 380 cert_verifier_(new MockCertVerifier()),
381 client_cert_verifier_(new MockClientCertVerifier()),
305 transport_security_state_(new TransportSecurityState) { 382 transport_security_state_(new TransportSecurityState) {
306 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); 383 cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID);
384 client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID);
307 } 385 }
308 386
309 protected: 387 protected:
310 void Initialize() { 388 void Initialize() {
311 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); 389 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle);
312 client_connection->SetSocket( 390 client_connection->SetSocket(
313 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); 391 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_)));
314 scoped_ptr<StreamSocket> server_socket( 392 scoped_ptr<StreamSocket> server_socket(
315 new FakeSocket(&channel_2_, &channel_1_)); 393 new FakeSocket(&channel_2_, &channel_1_));
316 394
317 base::FilePath certs_dir(GetTestCertsDirectory()); 395 scoped_refptr<X509Certificate> server_cert(
318 396 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"));
319 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); 397 scoped_ptr<crypto::RSAPrivateKey> server_private_key(
320 std::string cert_der; 398 ReadTestKey("unittest.key.bin"));
321 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der));
322
323 scoped_refptr<X509Certificate> cert =
324 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
325
326 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
327 std::string key_string;
328 ASSERT_TRUE(base::ReadFileToString(key_path, &key_string));
329 std::vector<uint8_t> key_vector(
330 reinterpret_cast<const uint8_t*>(key_string.data()),
331 reinterpret_cast<const uint8_t*>(key_string.data() +
332 key_string.length()));
333
334 scoped_ptr<crypto::RSAPrivateKey> private_key(
335 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
336 399
337 client_ssl_config_.false_start_enabled = false; 400 client_ssl_config_.false_start_enabled = false;
338 client_ssl_config_.channel_id_enabled = false; 401 client_ssl_config_.channel_id_enabled = false;
339 402
340 // Certificate provided by the host doesn't need authority. 403 // Certificate provided by the host doesn't need authority.
341 SSLConfig::CertAndStatus cert_and_status; 404 SSLConfig::CertAndStatus cert_and_status;
342 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; 405 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
343 cert_and_status.der_cert = cert_der; 406 std::string server_cert_der;
407 CHECK(X509Certificate::GetDEREncoded(server_cert->os_cert_handle(),
408 &server_cert_der));
409 cert_and_status.der_cert = server_cert_der;
344 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); 410 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status);
345 411
346 HostPortPair host_and_pair("unittest", 0); 412 HostPortPair host_and_pair("unittest", 0);
347 SSLClientSocketContext context; 413 SSLClientSocketContext context;
348 context.cert_verifier = cert_verifier_.get(); 414 context.cert_verifier = cert_verifier_.get();
349 context.transport_security_state = transport_security_state_.get(); 415 context.transport_security_state = transport_security_state_.get();
416 socket_factory_->ClearSSLSessionCache();
350 client_socket_ = socket_factory_->CreateSSLClientSocket( 417 client_socket_ = socket_factory_->CreateSSLClientSocket(
351 std::move(client_connection), host_and_pair, client_ssl_config_, 418 std::move(client_connection), host_and_pair, client_ssl_config_,
352 context); 419 context);
353 server_socket_ = CreateSSLServerSocket(std::move(server_socket), cert.get(), 420 server_socket_ =
354 *private_key, server_ssl_config_); 421 CreateSSLServerSocket(std::move(server_socket), server_cert.get(),
422 *server_private_key, server_ssl_config_);
355 } 423 }
356 424
425 #if defined(USE_OPENSSL)
426 void ConfigureClientCertsForClient(const char* cert_file_name,
427 const char* private_key_file_name) {
428 scoped_refptr<X509Certificate> cert;
429 scoped_refptr<net::SSLPrivateKey> key;
430 if (cert_file_name && private_key_file_name) {
431 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name);
432 key = new TestSSLPrivateKey(ReadTestKey(private_key_file_name));
433 }
434 client_ssl_config_.send_client_cert = true;
435 client_ssl_config_.client_cert = cert;
436 client_ssl_config_.client_private_key = key;
437 }
438
439 void ConfigureClientCertsForServer() {
440 server_ssl_config_.require_client_cert = true;
441
442 STACK_OF(X509_NAME)* cert_names;
443 cert_names = SSL_load_client_CA_file(GetTestCertsDirectory()
davidben 2016/02/04 00:40:11 ScopedX509NameStack cert_name(SSL_load_.....);
ryanchung 2016/02/05 01:56:13 Done.
444 .AppendASCII(kClientCertCAFileName)
445 .MaybeAsASCII()
446 .c_str());
447 ASSERT_TRUE(cert_names != nullptr);
davidben 2016/02/04 00:40:11 Nit: This can just be ASSERT_TRUE(cert_names);
ryanchung 2016/02/05 01:56:13 Done.
448 for (size_t i = 0; i < sk_X509_NAME_num(cert_names); ++i) {
449 uint8_t* str = nullptr;
450 int length = i2d_X509_NAME(sk_X509_NAME_value(cert_names, i), &str);
451 server_ssl_config_.cert_authorities_.push_back(std::string(
452 reinterpret_cast<const char*>(str), static_cast<size_t>(length)));
453 OPENSSL_free(str);
454 }
455 sk_X509_NAME_pop_free(cert_names, X509_NAME_free);
456
457 scoped_refptr<X509Certificate> expected_client_cert(
458 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName));
459 client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK);
460
461 server_ssl_config_.client_cert_verifier = client_cert_verifier_.get();
462 }
463
464 scoped_ptr<crypto::RSAPrivateKey> ReadTestKey(const base::StringPiece& name) {
465 base::FilePath certs_dir(GetTestCertsDirectory());
466 base::FilePath key_path = certs_dir.AppendASCII(name);
467 std::string key_string;
468 if (!base::ReadFileToString(key_path, &key_string))
469 return nullptr;
470 std::vector<uint8_t> key_vector(
471 reinterpret_cast<const uint8_t*>(key_string.data()),
472 reinterpret_cast<const uint8_t*>(key_string.data() +
473 key_string.length()));
474 scoped_ptr<crypto::RSAPrivateKey> key(
475 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
476 return key;
477 }
478 #endif
479
357 FakeDataChannel channel_1_; 480 FakeDataChannel channel_1_;
358 FakeDataChannel channel_2_; 481 FakeDataChannel channel_2_;
359 SSLConfig client_ssl_config_; 482 SSLConfig client_ssl_config_;
360 SSLServerConfig server_ssl_config_; 483 SSLServerConfig server_ssl_config_;
361 scoped_ptr<SSLClientSocket> client_socket_; 484 scoped_ptr<SSLClientSocket> client_socket_;
362 scoped_ptr<SSLServerSocket> server_socket_; 485 scoped_ptr<SSLServerSocket> server_socket_;
363 ClientSocketFactory* socket_factory_; 486 ClientSocketFactory* socket_factory_;
364 scoped_ptr<MockCertVerifier> cert_verifier_; 487 scoped_ptr<MockCertVerifier> cert_verifier_;
488 scoped_ptr<MockClientCertVerifier> client_cert_verifier_;
365 scoped_ptr<TransportSecurityState> transport_security_state_; 489 scoped_ptr<TransportSecurityState> transport_security_state_;
366 }; 490 };
367 491
368 // This test only executes creation of client and server sockets. This is to 492 // This test only executes creation of client and server sockets. This is to
369 // test that creation of sockets doesn't crash and have minimal code to run 493 // test that creation of sockets doesn't crash and have minimal code to run
370 // under valgrind in order to help debugging memory problems. 494 // under valgrind in order to help debugging memory problems.
371 TEST_F(SSLServerSocketTest, Initialize) { 495 TEST_F(SSLServerSocketTest, Initialize) {
372 Initialize(); 496 Initialize();
373 } 497 }
374 498
375 // This test executes Connect() on SSLClientSocket and Handshake() on 499 // This test executes Connect() on SSLClientSocket and Handshake() on
376 // SSLServerSocket to make sure handshaking between the two sockets is 500 // SSLServerSocket to make sure handshaking between the two sockets is
377 // completed successfully. 501 // completed successfully.
378 TEST_F(SSLServerSocketTest, Handshake) { 502 TEST_F(SSLServerSocketTest, Handshake) {
379 Initialize(); 503 Initialize();
380 504
505 TestCompletionCallback handshake_callback;
506 int server_ret = server_socket_->Handshake(handshake_callback.callback());
507
381 TestCompletionCallback connect_callback; 508 TestCompletionCallback connect_callback;
382 TestCompletionCallback handshake_callback; 509 int client_ret = client_socket_->Connect(connect_callback.callback());
383 510
384 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 511 client_ret = connect_callback.GetResult(client_ret);
385 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); 512 server_ret = handshake_callback.GetResult(server_ret);
386 513
387 int client_ret = client_socket_->Connect(connect_callback.callback()); 514 ASSERT_EQ(OK, client_ret);
388 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); 515 ASSERT_EQ(OK, server_ret);
389
390 if (client_ret == ERR_IO_PENDING) {
391 EXPECT_EQ(OK, connect_callback.WaitForResult());
392 }
393 if (server_ret == ERR_IO_PENDING) {
394 EXPECT_EQ(OK, handshake_callback.WaitForResult());
395 }
396 516
397 // Make sure the cert status is expected. 517 // Make sure the cert status is expected.
398 SSLInfo ssl_info; 518 SSLInfo ssl_info;
399 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); 519 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info));
400 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); 520 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
401 521
402 // The default cipher suite should be ECDHE and, unless on NSS and the 522 // The default cipher suite should be ECDHE and, unless on NSS and the
403 // platform doesn't support it, an AEAD. 523 // platform doesn't support it, an AEAD.
404 uint16_t cipher_suite = 524 uint16_t cipher_suite =
405 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); 525 SSLConnectionStatusToCipherSuite(ssl_info.connection_status);
406 const char* key_exchange; 526 const char* key_exchange;
407 const char* cipher; 527 const char* cipher;
408 const char* mac; 528 const char* mac;
409 bool is_aead; 529 bool is_aead;
410 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); 530 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
411 EXPECT_STREQ("ECDHE_RSA", key_exchange); 531 EXPECT_STREQ("ECDHE_RSA", key_exchange);
412 EXPECT_TRUE(is_aead); 532 EXPECT_TRUE(is_aead);
413 } 533 }
414 534
535 // NSS ports don't support client certificates.
536 #if defined(USE_OPENSSL)
537
538 // This test executes Connect() on SSLClientSocket and Handshake() on
539 // SSLServerSocket to make sure handshaking between the two sockets is
540 // completed successfully, using client certificate.
541 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) {
542 scoped_refptr<X509Certificate> client_cert =
543 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
544 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName);
545 ConfigureClientCertsForServer();
546 Initialize();
547
548 TestCompletionCallback handshake_callback;
549 int server_ret = server_socket_->Handshake(handshake_callback.callback());
550
551 TestCompletionCallback connect_callback;
552 int client_ret = client_socket_->Connect(connect_callback.callback());
553
554 client_ret = connect_callback.GetResult(client_ret);
555 server_ret = handshake_callback.GetResult(server_ret);
556
557 ASSERT_EQ(OK, client_ret);
558 ASSERT_EQ(OK, server_ret);
559
560 // Make sure the cert status is expected.
561 SSLInfo ssl_info;
562 client_socket_->GetSSLInfo(&ssl_info);
563 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
564 server_socket_->GetSSLInfo(&ssl_info);
565 EXPECT_TRUE(ssl_info.cert.get());
566 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get()));
567 }
568
569 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) {
570 ConfigureClientCertsForServer();
571 Initialize();
572 // Use the default setting for the client socket, which is to not send
573 // a client certificate. This will cause the client to receive an
574 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the
575 // requested cert_authorities from the CertificateRequest sent by the
576 // server.
577
578 TestCompletionCallback handshake_callback;
579 int server_ret = server_socket_->Handshake(handshake_callback.callback());
580
581 TestCompletionCallback connect_callback;
582 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
583 connect_callback.GetResult(
584 client_socket_->Connect(connect_callback.callback())));
585
586 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo();
587 client_socket_->GetSSLCertRequestInfo(request_info.get());
588
589 // Check that the authority name that arrived in the CertificateRequest
590 // handshake message is as expected.
591 scoped_refptr<X509Certificate> client_cert =
592 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
593 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities));
594
595 client_socket_->Disconnect();
596
597 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret));
598 }
599
600 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) {
601 scoped_refptr<X509Certificate> client_cert =
602 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
603 ConfigureClientCertsForClient(kWrongClientCertFileName,
604 kWrongClientPrivateKeyFileName);
605 ConfigureClientCertsForServer();
606 Initialize();
607
608 TestCompletionCallback handshake_callback;
609 int server_ret = server_socket_->Handshake(handshake_callback.callback());
610
611 TestCompletionCallback connect_callback;
612 int client_ret = client_socket_->Connect(connect_callback.callback());
613
614 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT,
615 connect_callback.GetResult(client_ret));
616 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT,
617 handshake_callback.GetResult(server_ret));
618 }
619 #endif // defined(USE_OPENSSL)
620
415 TEST_F(SSLServerSocketTest, DataTransfer) { 621 TEST_F(SSLServerSocketTest, DataTransfer) {
416 Initialize(); 622 Initialize();
417 623
624 // Establish connection.
418 TestCompletionCallback connect_callback; 625 TestCompletionCallback connect_callback;
419 TestCompletionCallback handshake_callback;
420
421 // Establish connection.
422 int client_ret = client_socket_->Connect(connect_callback.callback()); 626 int client_ret = client_socket_->Connect(connect_callback.callback());
423 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); 627 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
424 628
629 TestCompletionCallback handshake_callback;
425 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 630 int server_ret = server_socket_->Handshake(handshake_callback.callback());
426 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); 631 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
427 632
428 client_ret = connect_callback.GetResult(client_ret); 633 client_ret = connect_callback.GetResult(client_ret);
429 ASSERT_EQ(OK, client_ret); 634 ASSERT_EQ(OK, client_ret);
430 server_ret = handshake_callback.GetResult(server_ret); 635 server_ret = handshake_callback.GetResult(server_ret);
431 ASSERT_EQ(OK, server_ret); 636 ASSERT_EQ(OK, server_ret);
432 637
433 const int kReadBufSize = 1024; 638 const int kReadBufSize = 1024;
434 scoped_refptr<StringIOBuffer> write_buf = 639 scoped_refptr<StringIOBuffer> write_buf =
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); 697 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
493 } 698 }
494 699
495 // A regression test for bug 127822 (http://crbug.com/127822). 700 // A regression test for bug 127822 (http://crbug.com/127822).
496 // If the server closes the connection after the handshake is finished, 701 // If the server closes the connection after the handshake is finished,
497 // the client's Write() call should not cause an infinite loop. 702 // the client's Write() call should not cause an infinite loop.
498 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. 703 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket.
499 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { 704 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) {
500 Initialize(); 705 Initialize();
501 706
502 TestCompletionCallback connect_callback;
503 TestCompletionCallback handshake_callback;
504 707
505 // Establish connection. 708 // Establish connection.
709 TestCompletionCallback connect_callback;
506 int client_ret = client_socket_->Connect(connect_callback.callback()); 710 int client_ret = client_socket_->Connect(connect_callback.callback());
507 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); 711 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
508 712
713 TestCompletionCallback handshake_callback;
509 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 714 int server_ret = server_socket_->Handshake(handshake_callback.callback());
510 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); 715 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
511 716
512 client_ret = connect_callback.GetResult(client_ret); 717 client_ret = connect_callback.GetResult(client_ret);
513 ASSERT_EQ(OK, client_ret); 718 ASSERT_EQ(OK, client_ret);
514 server_ret = handshake_callback.GetResult(server_ret); 719 server_ret = handshake_callback.GetResult(server_ret);
515 ASSERT_EQ(OK, server_ret); 720 ASSERT_EQ(OK, server_ret);
516 721
517 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); 722 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123");
518 723
519 // The server closes the connection. The server needs to write some 724 // The server closes the connection. The server needs to write some
520 // data first so that the client's Read() calls from the transport 725 // data first so that the client's Read() calls from the transport
521 // socket won't return ERR_IO_PENDING. This ensures that the client 726 // socket won't return ERR_IO_PENDING. This ensures that the client
522 // will call Read() on the transport socket again. 727 // will call Read() on the transport socket again.
523 TestCompletionCallback write_callback; 728 TestCompletionCallback write_callback;
524
525 server_ret = server_socket_->Write( 729 server_ret = server_socket_->Write(
526 write_buf.get(), write_buf->size(), write_callback.callback()); 730 write_buf.get(), write_buf->size(), write_callback.callback());
527 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); 731 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
528 732
529 server_ret = write_callback.GetResult(server_ret); 733 server_ret = write_callback.GetResult(server_ret);
530 EXPECT_GT(server_ret, 0); 734 EXPECT_GT(server_ret, 0);
531 735
532 server_socket_->Disconnect(); 736 server_socket_->Disconnect();
533 737
534 // The client writes some data. This should not cause an infinite loop. 738 // The client writes some data. This should not cause an infinite loop.
(...skipping 10 matching lines...) Expand all
545 base::MessageLoop::current()->Run(); 749 base::MessageLoop::current()->Run();
546 } 750 }
547 751
548 // This test executes ExportKeyingMaterial() on the client and server sockets, 752 // This test executes ExportKeyingMaterial() on the client and server sockets,
549 // after connecting them, and verifies that the results match. 753 // after connecting them, and verifies that the results match.
550 // This test will fail if False Start is enabled (see crbug.com/90208). 754 // This test will fail if False Start is enabled (see crbug.com/90208).
551 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { 755 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
552 Initialize(); 756 Initialize();
553 757
554 TestCompletionCallback connect_callback; 758 TestCompletionCallback connect_callback;
555 TestCompletionCallback handshake_callback;
556
557 int client_ret = client_socket_->Connect(connect_callback.callback()); 759 int client_ret = client_socket_->Connect(connect_callback.callback());
558 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); 760 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
559 761
762 TestCompletionCallback handshake_callback;
560 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 763 int server_ret = server_socket_->Handshake(handshake_callback.callback());
561 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); 764 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
562 765
563 if (client_ret == ERR_IO_PENDING) { 766 if (client_ret == ERR_IO_PENDING) {
564 ASSERT_EQ(OK, connect_callback.WaitForResult()); 767 ASSERT_EQ(OK, connect_callback.WaitForResult());
565 } 768 }
566 if (server_ret == ERR_IO_PENDING) { 769 if (server_ret == ERR_IO_PENDING) {
567 ASSERT_EQ(OK, handshake_callback.WaitForResult()); 770 ASSERT_EQ(OK, handshake_callback.WaitForResult());
568 } 771 }
569 772
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 }; 812 };
610 client_ssl_config_.disabled_cipher_suites.assign( 813 client_ssl_config_.disabled_cipher_suites.assign(
611 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); 814 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers));
612 815
613 // Require ECDHE on the server. 816 // Require ECDHE on the server.
614 server_ssl_config_.require_ecdhe = true; 817 server_ssl_config_.require_ecdhe = true;
615 818
616 Initialize(); 819 Initialize();
617 820
618 TestCompletionCallback connect_callback; 821 TestCompletionCallback connect_callback;
822 int client_ret = client_socket_->Connect(connect_callback.callback());
823
619 TestCompletionCallback handshake_callback; 824 TestCompletionCallback handshake_callback;
620
621 int client_ret = client_socket_->Connect(connect_callback.callback());
622 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 825 int server_ret = server_socket_->Handshake(handshake_callback.callback());
623 826
624 client_ret = connect_callback.GetResult(client_ret); 827 client_ret = connect_callback.GetResult(client_ret);
625 server_ret = handshake_callback.GetResult(server_ret); 828 server_ret = handshake_callback.GetResult(server_ret);
626 829
627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); 830 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret);
628 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); 831 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret);
629 } 832 }
630 833
631 } // namespace net 834 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698