Chromium Code Reviews| 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 // 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 // |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 namespace net { | 66 namespace net { |
| 67 | 67 |
| 68 namespace { | 68 namespace { |
| 69 | 69 |
| 70 const char kClientCertFileName[] = "client_1.pem"; | 70 const char kClientCertFileName[] = "client_1.pem"; |
| 71 const char kClientPrivateKeyFileName[] = "client_1.pk8"; | 71 const char kClientPrivateKeyFileName[] = "client_1.pk8"; |
| 72 const char kWrongClientCertFileName[] = "client_2.pem"; | 72 const char kWrongClientCertFileName[] = "client_2.pem"; |
| 73 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; | 73 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; |
| 74 const char kClientCertCAFileName[] = "client_1_ca.pem"; | 74 const char kClientCertCAFileName[] = "client_1_ca.pem"; |
| 75 | 75 |
| 76 class FakeDataChannel { | 76 class FakeDataChannel : public base::RefCountedThreadSafe<FakeDataChannel> { |
|
davidben
2016/01/22 23:57:48
Reference-counted mutable classes are usually an i
ryanchung
2016/01/29 23:28:16
Done. Changed it back.
These were used by both the
| |
| 77 public: | 77 public: |
| 78 FakeDataChannel() | 78 FakeDataChannel() |
| 79 : read_buf_len_(0), | 79 : read_buf_len_(0), |
| 80 closed_(false), | 80 closed_(false), |
| 81 write_called_after_close_(false), | 81 write_called_after_close_(false), |
| 82 weak_factory_(this) { | 82 weak_factory_(this) { |
| 83 } | 83 } |
| 84 | 84 |
| 85 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 85 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
| 86 DCHECK(read_callback_.is_null()); | 86 DCHECK(read_callback_.is_null()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 data_.push( | 127 data_.push( |
| 128 new DrainableIOBuffer(new StringIOBuffer(std::string("0", 1)), 1)); | 128 new DrainableIOBuffer(new StringIOBuffer(std::string("0", 1)), 1)); |
| 129 if (!read_callback_.is_null()) { | 129 if (!read_callback_.is_null()) { |
| 130 base::ThreadTaskRunnerHandle::Get()->PostTask( | 130 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 131 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, | 131 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, |
| 132 weak_factory_.GetWeakPtr())); | 132 weak_factory_.GetWeakPtr())); |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 private: | 136 private: |
| 137 ~FakeDataChannel() {} | |
| 138 friend class base::RefCountedThreadSafe<FakeDataChannel>; | |
| 139 | |
| 137 void DoReadCallback() { | 140 void DoReadCallback() { |
| 138 if (read_callback_.is_null() || data_.empty()) | 141 if (read_callback_.is_null() || data_.empty()) |
| 139 return; | 142 return; |
| 140 int copied = PropagateData(read_buf_, read_buf_len_); | 143 int copied = PropagateData(read_buf_, read_buf_len_); |
| 141 CompletionCallback callback = read_callback_; | 144 CompletionCallback callback = read_callback_; |
| 142 read_callback_.Reset(); | 145 read_callback_.Reset(); |
| 143 read_buf_ = NULL; | 146 read_buf_ = NULL; |
| 144 read_buf_len_ = 0; | 147 read_buf_len_ = 0; |
| 145 callback.Run(copied); | 148 callback.Run(copied); |
| 146 } | 149 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 // asynchronously. | 184 // asynchronously. |
| 182 bool write_called_after_close_; | 185 bool write_called_after_close_; |
| 183 | 186 |
| 184 base::WeakPtrFactory<FakeDataChannel> weak_factory_; | 187 base::WeakPtrFactory<FakeDataChannel> weak_factory_; |
| 185 | 188 |
| 186 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); | 189 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); |
| 187 }; | 190 }; |
| 188 | 191 |
| 189 class FakeSocket : public StreamSocket { | 192 class FakeSocket : public StreamSocket { |
| 190 public: | 193 public: |
| 191 FakeSocket(FakeDataChannel* incoming_channel, | 194 FakeSocket(const scoped_refptr<FakeDataChannel>& incoming_channel, |
| 192 FakeDataChannel* outgoing_channel) | 195 const scoped_refptr<FakeDataChannel>& outgoing_channel) { |
| 193 : incoming_(incoming_channel), | 196 incoming_ = incoming_channel; |
| 194 outgoing_(outgoing_channel) { | 197 outgoing_ = outgoing_channel; |
| 195 } | 198 } |
| 196 | 199 |
| 197 ~FakeSocket() override {} | 200 ~FakeSocket() override {} |
| 198 | 201 |
| 199 int Read(IOBuffer* buf, | 202 int Read(IOBuffer* buf, |
| 200 int buf_len, | 203 int buf_len, |
| 201 const CompletionCallback& callback) override { | 204 const CompletionCallback& callback) override { |
| 202 // Read random number of bytes. | 205 // Read random number of bytes. |
| 203 buf_len = rand() % buf_len + 1; | 206 buf_len = rand() % buf_len + 1; |
| 204 return incoming_->Read(buf, buf_len, callback); | 207 return incoming_->Read(buf, buf_len, callback); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 | 265 |
| 263 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} | 266 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
| 264 | 267 |
| 265 int64_t GetTotalReceivedBytes() const override { | 268 int64_t GetTotalReceivedBytes() const override { |
| 266 NOTIMPLEMENTED(); | 269 NOTIMPLEMENTED(); |
| 267 return 0; | 270 return 0; |
| 268 } | 271 } |
| 269 | 272 |
| 270 private: | 273 private: |
| 271 BoundNetLog net_log_; | 274 BoundNetLog net_log_; |
| 272 FakeDataChannel* incoming_; | 275 scoped_refptr<FakeDataChannel> incoming_; |
| 273 FakeDataChannel* outgoing_; | 276 scoped_refptr<FakeDataChannel> outgoing_; |
| 274 | 277 |
| 275 DISALLOW_COPY_AND_ASSIGN(FakeSocket); | 278 DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
| 276 }; | 279 }; |
| 277 | 280 |
| 278 class TestSSLPrivateKey : public SSLPrivateKey { | 281 class TestSSLPrivateKey : public SSLPrivateKey { |
| 279 public: | 282 public: |
| 280 TestSSLPrivateKey(crypto::RSAPrivateKey* rsa_private_key) | 283 TestSSLPrivateKey(crypto::RSAPrivateKey* rsa_private_key) |
| 281 : rsa_private_key_(rsa_private_key) {} | 284 : rsa_private_key_(rsa_private_key) {} |
| 282 | 285 |
| 283 Type GetType() override { return SSLPrivateKey::Type::RSA; } | 286 Type GetType() override { return SSLPrivateKey::Type::RSA; } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_; | 334 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_; |
| 332 | 335 |
| 333 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey); | 336 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey); |
| 334 }; | 337 }; |
| 335 | 338 |
| 336 } // namespace | 339 } // namespace |
| 337 | 340 |
| 338 // Verify the correctness of the test helper classes first. | 341 // Verify the correctness of the test helper classes first. |
| 339 TEST(FakeSocketTest, DataTransfer) { | 342 TEST(FakeSocketTest, DataTransfer) { |
| 340 // Establish channels between two sockets. | 343 // Establish channels between two sockets. |
| 341 FakeDataChannel channel_1; | 344 scoped_refptr<FakeDataChannel> channel_1(new FakeDataChannel()); |
| 342 FakeDataChannel channel_2; | 345 scoped_refptr<FakeDataChannel> channel_2(new FakeDataChannel()); |
| 343 FakeSocket client(&channel_1, &channel_2); | 346 FakeSocket client(channel_1, channel_2); |
| 344 FakeSocket server(&channel_2, &channel_1); | 347 FakeSocket server(channel_2, channel_1); |
| 345 | 348 |
| 346 const char kTestData[] = "testing123"; | 349 const char kTestData[] = "testing123"; |
| 347 const int kTestDataSize = strlen(kTestData); | 350 const int kTestDataSize = strlen(kTestData); |
| 348 const int kReadBufSize = 1024; | 351 const int kReadBufSize = 1024; |
| 349 scoped_refptr<IOBuffer> write_buf = new StringIOBuffer(kTestData); | 352 scoped_refptr<IOBuffer> write_buf = new StringIOBuffer(kTestData); |
| 350 scoped_refptr<IOBuffer> read_buf = new IOBuffer(kReadBufSize); | 353 scoped_refptr<IOBuffer> read_buf = new IOBuffer(kReadBufSize); |
| 351 | 354 |
| 352 // Write then read. | 355 // Write then read. |
| 353 int written = | 356 int written = |
| 354 server.Write(write_buf.get(), kTestDataSize, CompletionCallback()); | 357 server.Write(write_buf.get(), kTestDataSize, CompletionCallback()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 374 EXPECT_LE(read, written); | 377 EXPECT_LE(read, written); |
| 375 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); | 378 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
| 376 } | 379 } |
| 377 | 380 |
| 378 class SSLServerSocketTest : public PlatformTest { | 381 class SSLServerSocketTest : public PlatformTest { |
| 379 public: | 382 public: |
| 380 SSLServerSocketTest() | 383 SSLServerSocketTest() |
| 381 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 384 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 382 cert_verifier_(new MockCertVerifier()), | 385 cert_verifier_(new MockCertVerifier()), |
| 383 client_cert_verifier_(new MockClientCertVerifier()), | 386 client_cert_verifier_(new MockClientCertVerifier()), |
| 384 transport_security_state_(new TransportSecurityState) { | 387 transport_security_state_(new TransportSecurityState), |
| 388 initialized_(false) { | |
| 385 cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); | 389 cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); |
| 386 client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); | 390 client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); |
| 387 } | 391 } |
| 388 | 392 |
| 389 protected: | 393 protected: |
| 390 void Initialize() { | 394 void Initialize(bool reinitialize_server_context = false) { |
|
davidben
2016/01/22 23:57:48
Style: no default arguments.
ryanchung
2016/01/29 23:28:16
Done.
| |
| 395 scoped_refptr<FakeDataChannel> channel_1_ = new FakeDataChannel(); | |
| 396 scoped_refptr<FakeDataChannel> channel_2_ = new FakeDataChannel(); | |
| 391 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); | 397 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); |
| 392 client_connection->SetSocket( | 398 client_connection->SetSocket( |
| 393 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); | 399 scoped_ptr<StreamSocket>(new FakeSocket(channel_1_, channel_2_))); |
| 394 scoped_ptr<StreamSocket> server_socket( | 400 scoped_ptr<StreamSocket> server_socket( |
| 395 new FakeSocket(&channel_2_, &channel_1_)); | 401 new FakeSocket(channel_2_, channel_1_)); |
| 396 | 402 |
| 397 std::string server_cert_der; | 403 std::string server_cert_der; |
| 398 scoped_refptr<X509Certificate> server_cert( | 404 if (!initialized_ || reinitialize_server_context) { |
| 399 ReadTestCert("unittest.selfsigned.der", &server_cert_der)); | 405 scoped_refptr<X509Certificate> server_cert( |
| 400 scoped_ptr<crypto::RSAPrivateKey> server_private_key( | 406 ReadTestCert("unittest.selfsigned.der", &server_cert_der)); |
| 401 ReadTestKey("unittest.key.bin")); | 407 scoped_ptr<crypto::RSAPrivateKey> server_private_key( |
| 408 ReadTestKey("unittest.key.bin")); | |
| 409 server_context_ = CreateSSLServerSocketContext( | |
| 410 server_cert.get(), *server_private_key, server_ssl_config_); | |
| 411 } | |
| 402 | 412 |
| 403 client_ssl_config_.false_start_enabled = false; | 413 if (!initialized_) { |
| 404 client_ssl_config_.channel_id_enabled = false; | 414 client_ssl_config_.false_start_enabled = false; |
| 415 client_ssl_config_.channel_id_enabled = false; | |
| 405 | 416 |
| 406 // Certificate provided by the host doesn't need authority. | 417 // Certificate provided by the host doesn't need authority. |
| 407 SSLConfig::CertAndStatus cert_and_status; | 418 SSLConfig::CertAndStatus cert_and_status; |
| 408 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; | 419 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; |
| 409 cert_and_status.der_cert = server_cert_der; | 420 cert_and_status.der_cert = server_cert_der; |
| 410 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); | 421 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); |
| 422 | |
| 423 socket_factory_->ClearSSLSessionCache(); | |
| 424 } | |
| 411 | 425 |
| 412 HostPortPair host_and_pair("unittest", 0); | 426 HostPortPair host_and_pair("unittest", 0); |
| 413 SSLClientSocketContext context; | 427 SSLClientSocketContext context; |
| 414 context.cert_verifier = cert_verifier_.get(); | 428 context.cert_verifier = cert_verifier_.get(); |
| 415 context.transport_security_state = transport_security_state_.get(); | 429 context.transport_security_state = transport_security_state_.get(); |
| 416 socket_factory_->ClearSSLSessionCache(); | |
| 417 client_socket_ = socket_factory_->CreateSSLClientSocket( | 430 client_socket_ = socket_factory_->CreateSSLClientSocket( |
| 418 std::move(client_connection), host_and_pair, client_ssl_config_, | 431 std::move(client_connection), host_and_pair, client_ssl_config_, |
| 419 context); | 432 context); |
| 433 | |
| 420 server_socket_ = | 434 server_socket_ = |
| 421 CreateSSLServerSocket(std::move(server_socket), server_cert.get(), | 435 server_context_->CreateSSLServerSocket(std::move(server_socket)); |
| 422 *server_private_key, server_ssl_config_); | 436 initialized_ = true; |
| 423 } | 437 } |
| 424 | 438 |
| 425 void ConfigureClientCertsForClient(const char* cert_file_name, | 439 void ConfigureClientCertsForClient(const char* cert_file_name, |
| 426 const char* private_key_file_name) { | 440 const char* private_key_file_name) { |
| 427 scoped_refptr<X509Certificate> cert; | 441 scoped_refptr<X509Certificate> cert; |
| 428 scoped_refptr<net::SSLPrivateKey> key; | 442 scoped_refptr<net::SSLPrivateKey> key; |
| 429 if (cert_file_name && private_key_file_name) { | 443 if (cert_file_name && private_key_file_name) { |
| 430 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); | 444 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); |
| 431 key = new TestSSLPrivateKey(ReadTestKey(private_key_file_name)); | 445 key = new TestSSLPrivateKey(ReadTestKey(private_key_file_name)); |
| 432 } | 446 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 481 std::string key_string; | 495 std::string key_string; |
| 482 if (!base::ReadFileToString(key_path, &key_string)) | 496 if (!base::ReadFileToString(key_path, &key_string)) |
| 483 return NULL; | 497 return NULL; |
| 484 std::vector<uint8_t> key_vector( | 498 std::vector<uint8_t> key_vector( |
| 485 reinterpret_cast<const uint8_t*>(key_string.data()), | 499 reinterpret_cast<const uint8_t*>(key_string.data()), |
| 486 reinterpret_cast<const uint8_t*>(key_string.data() + | 500 reinterpret_cast<const uint8_t*>(key_string.data() + |
| 487 key_string.length())); | 501 key_string.length())); |
| 488 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector); | 502 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector); |
| 489 } | 503 } |
| 490 | 504 |
| 491 FakeDataChannel channel_1_; | |
| 492 FakeDataChannel channel_2_; | |
| 493 SSLConfig client_ssl_config_; | 505 SSLConfig client_ssl_config_; |
| 494 SSLServerConfig server_ssl_config_; | 506 SSLServerConfig server_ssl_config_; |
| 495 scoped_ptr<SSLClientSocket> client_socket_; | 507 scoped_ptr<SSLClientSocket> client_socket_; |
| 496 scoped_ptr<SSLServerSocket> server_socket_; | 508 scoped_ptr<SSLServerSocket> server_socket_; |
| 497 ClientSocketFactory* socket_factory_; | 509 ClientSocketFactory* socket_factory_; |
| 498 scoped_ptr<MockCertVerifier> cert_verifier_; | 510 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 499 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; | 511 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; |
| 500 scoped_ptr<TransportSecurityState> transport_security_state_; | 512 scoped_ptr<TransportSecurityState> transport_security_state_; |
| 513 scoped_ptr<SSLServerSocketContext> server_context_; | |
| 501 CertificateList trusted_certs_; | 514 CertificateList trusted_certs_; |
| 515 bool initialized_; | |
| 502 }; | 516 }; |
| 503 | 517 |
| 504 // This test only executes creation of client and server sockets. This is to | 518 // This test only executes creation of client and server sockets. This is to |
| 505 // test that creation of sockets doesn't crash and have minimal code to run | 519 // test that creation of sockets doesn't crash and have minimal code to run |
| 506 // under valgrind in order to help debugging memory problems. | 520 // under valgrind in order to help debugging memory problems. |
| 507 TEST_F(SSLServerSocketTest, Initialize) { | 521 TEST_F(SSLServerSocketTest, Initialize) { |
| 508 Initialize(); | 522 Initialize(); |
| 509 } | 523 } |
| 510 | 524 |
| 511 // This test executes Connect() on SSLClientSocket and Handshake() on | 525 // This test executes Connect() on SSLClientSocket and Handshake() on |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 539 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); | 553 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
| 540 const char* key_exchange; | 554 const char* key_exchange; |
| 541 const char* cipher; | 555 const char* cipher; |
| 542 const char* mac; | 556 const char* mac; |
| 543 bool is_aead; | 557 bool is_aead; |
| 544 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); | 558 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 545 EXPECT_STREQ("ECDHE_RSA", key_exchange); | 559 EXPECT_STREQ("ECDHE_RSA", key_exchange); |
| 546 EXPECT_TRUE(is_aead); | 560 EXPECT_TRUE(is_aead); |
| 547 } | 561 } |
| 548 | 562 |
| 563 // This tests makes sure the session cache is working | |
| 564 TEST_F(SSLServerSocketTest, HandshakeCached) { | |
| 565 Initialize(); | |
| 566 | |
| 567 TestCompletionCallback handshake_callback; | |
| 568 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 569 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | |
|
davidben
2016/01/22 23:57:48
I wouldn't bother with assertions like this one. T
ryanchung
2016/01/29 23:28:16
Done.
| |
| 570 | |
| 571 TestCompletionCallback connect_callback; | |
| 572 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 573 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | |
| 574 | |
| 575 client_ret = connect_callback.GetResult(client_ret); | |
| 576 server_ret = handshake_callback.GetResult(server_ret); | |
| 577 | |
| 578 ASSERT_EQ(OK, client_ret); | |
| 579 ASSERT_EQ(OK, server_ret); | |
| 580 | |
| 581 // Make sure the cert status is expected. | |
| 582 SSLInfo ssl_info; | |
| 583 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); | |
| 584 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | |
| 585 EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); | |
| 586 | |
| 587 // Make sure the second connection is cached | |
| 588 Initialize(); | |
| 589 | |
| 590 TestCompletionCallback handshake_callback2; | |
| 591 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); | |
| 592 ASSERT_TRUE(server_ret2 == OK || server_ret2 == ERR_IO_PENDING); | |
| 593 | |
| 594 TestCompletionCallback connect_callback2; | |
| 595 int client_ret2 = client_socket_->Connect(connect_callback2.callback()); | |
| 596 ASSERT_TRUE(client_ret2 == OK || client_ret2 == ERR_IO_PENDING); | |
| 597 | |
| 598 client_ret2 = connect_callback2.GetResult(client_ret2); | |
| 599 server_ret2 = handshake_callback2.GetResult(server_ret2); | |
| 600 | |
| 601 ASSERT_EQ(OK, client_ret2); | |
| 602 ASSERT_EQ(OK, server_ret2); | |
| 603 | |
| 604 // Make sure the cert status is expected. | |
| 605 SSLInfo ssl_info2; | |
| 606 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info2)); | |
| 607 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info2.cert_status); | |
| 608 EXPECT_EQ(ssl_info2.handshake_type, SSLInfo::HANDSHAKE_RESUME); | |
| 609 } | |
| 610 | |
| 611 // This tests makes sure the session cache separates out by server context | |
| 612 TEST_F(SSLServerSocketTest, HandshakeCachedContextSwitch) { | |
| 613 Initialize(); | |
| 614 | |
| 615 TestCompletionCallback handshake_callback; | |
| 616 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 617 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | |
| 618 | |
| 619 TestCompletionCallback connect_callback; | |
| 620 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 621 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | |
| 622 | |
| 623 client_ret = connect_callback.GetResult(client_ret); | |
| 624 server_ret = handshake_callback.GetResult(server_ret); | |
| 625 | |
| 626 ASSERT_EQ(OK, client_ret); | |
| 627 ASSERT_EQ(OK, server_ret); | |
| 628 | |
| 629 // Make sure the cert status is expected. | |
| 630 SSLInfo ssl_info; | |
| 631 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); | |
| 632 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | |
| 633 EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); | |
| 634 | |
| 635 // Make sure the second connection is NOT cached when using a new context. | |
| 636 Initialize(true); | |
| 637 | |
| 638 TestCompletionCallback handshake_callback2; | |
| 639 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); | |
| 640 ASSERT_TRUE(server_ret2 == OK || server_ret2 == ERR_IO_PENDING); | |
| 641 | |
| 642 TestCompletionCallback connect_callback2; | |
| 643 int client_ret2 = client_socket_->Connect(connect_callback2.callback()); | |
| 644 ASSERT_TRUE(client_ret2 == OK || client_ret2 == ERR_IO_PENDING); | |
| 645 | |
| 646 client_ret2 = connect_callback2.GetResult(client_ret2); | |
| 647 server_ret2 = handshake_callback2.GetResult(server_ret2); | |
| 648 | |
| 649 ASSERT_EQ(OK, client_ret2); | |
| 650 ASSERT_EQ(OK, server_ret2); | |
| 651 | |
| 652 // Make sure the cert status is expected. | |
| 653 SSLInfo ssl_info2; | |
| 654 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info2)); | |
| 655 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info2.cert_status); | |
| 656 EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); | |
| 657 } | |
| 658 | |
| 549 // NSS ports don't support client certificates | 659 // NSS ports don't support client certificates |
| 550 #if defined(USE_OPENSSL) | 660 #if defined(USE_OPENSSL) |
| 551 | 661 |
| 552 // This test executes Connect() on SSLClientSocket and Handshake() on | 662 // This test executes Connect() on SSLClientSocket and Handshake() on |
| 553 // SSLServerSocket to make sure handshaking between the two sockets is | 663 // SSLServerSocket to make sure handshaking between the two sockets is |
| 554 // completed successfully, using client certificate. | 664 // completed successfully, using client certificate. |
| 555 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { | 665 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { |
| 556 scoped_refptr<X509Certificate> client_cert = | 666 scoped_refptr<X509Certificate> client_cert = |
| 557 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | 667 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 558 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName); | 668 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 575 | 685 |
| 576 // Make sure the cert status is expected. | 686 // Make sure the cert status is expected. |
| 577 SSLInfo ssl_info; | 687 SSLInfo ssl_info; |
| 578 client_socket_->GetSSLInfo(&ssl_info); | 688 client_socket_->GetSSLInfo(&ssl_info); |
| 579 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | 689 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 580 server_socket_->GetSSLInfo(&ssl_info); | 690 server_socket_->GetSSLInfo(&ssl_info); |
| 581 EXPECT_TRUE(ssl_info.cert.get()); | 691 EXPECT_TRUE(ssl_info.cert.get()); |
| 582 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); | 692 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); |
| 583 } | 693 } |
| 584 | 694 |
| 695 // This test executes Connect() on SSLClientSocket and Handshake() twice on | |
| 696 // SSLServerSocket to make sure handshaking between the two sockets is | |
| 697 // completed successfully, using client certificate. The second connection is | |
| 698 // expected to succeed through the session cache. | |
| 699 TEST_F(SSLServerSocketTest, HandshakeWithClientCertCached) { | |
| 700 scoped_refptr<X509Certificate> client_cert = | |
| 701 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
| 702 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName); | |
| 703 ConfigureClientCertsForServer(true); | |
| 704 Initialize(); | |
| 705 | |
| 706 TestCompletionCallback handshake_callback; | |
| 707 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 708 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | |
| 709 | |
| 710 TestCompletionCallback connect_callback; | |
| 711 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 712 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | |
| 713 | |
| 714 client_ret = connect_callback.GetResult(client_ret); | |
| 715 server_ret = handshake_callback.GetResult(server_ret); | |
| 716 | |
| 717 ASSERT_EQ(OK, client_ret); | |
| 718 ASSERT_EQ(OK, server_ret); | |
| 719 | |
| 720 // Make sure the cert status is expected. | |
| 721 SSLInfo ssl_info; | |
| 722 client_socket_->GetSSLInfo(&ssl_info); | |
| 723 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | |
| 724 server_socket_->GetSSLInfo(&ssl_info); | |
| 725 EXPECT_TRUE(ssl_info.cert.get()); | |
| 726 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); | |
| 727 EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); | |
| 728 server_socket_->Disconnect(); | |
| 729 client_socket_->Disconnect(); | |
| 730 | |
| 731 // Create the connection again | |
| 732 Initialize(); | |
| 733 | |
| 734 TestCompletionCallback handshake_callback2; | |
| 735 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); | |
| 736 ASSERT_TRUE(server_ret2 == OK || server_ret2 == ERR_IO_PENDING); | |
| 737 | |
| 738 TestCompletionCallback connect_callback2; | |
| 739 int client_ret2 = client_socket_->Connect(connect_callback2.callback()); | |
| 740 ASSERT_TRUE(client_ret2 == OK || client_ret2 == ERR_IO_PENDING); | |
| 741 | |
| 742 client_ret2 = connect_callback2.GetResult(client_ret2); | |
| 743 server_ret2 = handshake_callback2.GetResult(server_ret2); | |
| 744 | |
| 745 ASSERT_EQ(OK, client_ret2); | |
| 746 ASSERT_EQ(OK, server_ret2); | |
| 747 | |
| 748 // Make sure the cert status is expected. | |
| 749 SSLInfo ssl_info2; | |
| 750 client_socket_->GetSSLInfo(&ssl_info2); | |
| 751 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info2.cert_status); | |
| 752 server_socket_->GetSSLInfo(&ssl_info2); | |
| 753 EXPECT_TRUE(ssl_info2.cert.get()); | |
| 754 EXPECT_TRUE(client_cert->Equals(ssl_info2.cert.get())); | |
| 755 EXPECT_EQ(ssl_info2.handshake_type, SSLInfo::HANDSHAKE_RESUME); | |
| 756 } | |
| 757 | |
| 585 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { | 758 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { |
| 586 ConfigureClientCertsForServer(true); | 759 ConfigureClientCertsForServer(true); |
| 587 Initialize(); | 760 Initialize(); |
| 588 // Use the default setting for the client socket, which is to not send | 761 // Use the default setting for the client socket, which is to not send |
| 589 // a client certificate. This will cause the client to receive an | 762 // a client certificate. This will cause the client to receive an |
| 590 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the | 763 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the |
| 591 // requested cert_authorities from the CertificateRequest sent by the | 764 // requested cert_authorities from the CertificateRequest sent by the |
| 592 // server. | 765 // server. |
| 593 | 766 |
| 594 TestCompletionCallback handshake_callback; | 767 TestCompletionCallback handshake_callback; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 607 // handshake message is as expected. | 780 // handshake message is as expected. |
| 608 scoped_refptr<X509Certificate> client_cert = | 781 scoped_refptr<X509Certificate> client_cert = |
| 609 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | 782 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 610 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); | 783 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); |
| 611 | 784 |
| 612 client_socket_->Disconnect(); | 785 client_socket_->Disconnect(); |
| 613 | 786 |
| 614 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret)); | 787 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret)); |
| 615 } | 788 } |
| 616 | 789 |
| 790 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSuppliedCached) { | |
| 791 ConfigureClientCertsForServer(true); | |
| 792 Initialize(); | |
| 793 // Use the default setting for the client socket, which is to not send | |
| 794 // a client certificate. This will cause the client to receive an | |
| 795 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the | |
| 796 // requested cert_authorities from the CertificateRequest sent by the | |
| 797 // server. | |
| 798 | |
| 799 TestCompletionCallback handshake_callback; | |
| 800 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 801 ASSERT_EQ(server_ret, ERR_IO_PENDING); | |
| 802 | |
| 803 TestCompletionCallback connect_callback; | |
| 804 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, | |
| 805 connect_callback.GetResult( | |
| 806 client_socket_->Connect(connect_callback.callback()))); | |
| 807 | |
| 808 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); | |
| 809 client_socket_->GetSSLCertRequestInfo(request_info.get()); | |
| 810 | |
| 811 // Check that the authority name that arrived in the CertificateRequest | |
| 812 // handshake message is as expected. | |
| 813 scoped_refptr<X509Certificate> client_cert = | |
| 814 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
| 815 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); | |
| 816 | |
| 817 client_socket_->Disconnect(); | |
| 818 | |
| 819 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret)); | |
| 820 server_socket_->Disconnect(); | |
| 821 | |
| 822 // Below, check that the cache didn't store the result of a failed handshake | |
| 823 Initialize(); | |
| 824 | |
| 825 TestCompletionCallback handshake_callback2; | |
| 826 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); | |
| 827 ASSERT_EQ(server_ret2, ERR_IO_PENDING); | |
| 828 | |
| 829 TestCompletionCallback connect_callback2; | |
| 830 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, | |
| 831 connect_callback2.GetResult( | |
| 832 client_socket_->Connect(connect_callback2.callback()))); | |
| 833 | |
| 834 scoped_refptr<SSLCertRequestInfo> request_info2 = new SSLCertRequestInfo(); | |
| 835 client_socket_->GetSSLCertRequestInfo(request_info2.get()); | |
| 836 | |
| 837 // Check that the authority name that arrived in the CertificateRequest | |
| 838 // handshake message is as expected. | |
| 839 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info2->cert_authorities)); | |
| 840 | |
| 841 client_socket_->Disconnect(); | |
| 842 | |
| 843 EXPECT_EQ(ERR_FAILED, handshake_callback2.GetResult(server_ret2)); | |
| 844 } | |
| 845 | |
| 617 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { | 846 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { |
| 618 scoped_refptr<X509Certificate> client_cert = | 847 scoped_refptr<X509Certificate> client_cert = |
| 619 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | 848 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 620 ConfigureClientCertsForClient(kWrongClientCertFileName, | 849 ConfigureClientCertsForClient(kWrongClientCertFileName, |
| 621 kWrongClientPrivateKeyFileName); | 850 kWrongClientPrivateKeyFileName); |
| 622 ConfigureClientCertsForServer(true); | 851 ConfigureClientCertsForServer(true); |
| 623 Initialize(); | 852 Initialize(); |
| 624 | 853 |
| 625 TestCompletionCallback handshake_callback; | 854 TestCompletionCallback handshake_callback; |
| 626 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 855 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 627 EXPECT_EQ(server_ret, ERR_IO_PENDING); | 856 EXPECT_EQ(server_ret, ERR_IO_PENDING); |
| 628 | 857 |
| 629 TestCompletionCallback connect_callback; | 858 TestCompletionCallback connect_callback; |
| 630 int client_ret = client_socket_->Connect(connect_callback.callback()); | 859 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 631 | 860 |
| 632 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | 861 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 633 connect_callback.GetResult(client_ret)); | 862 connect_callback.GetResult(client_ret)); |
| 634 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | 863 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 635 handshake_callback.GetResult(server_ret)); | 864 handshake_callback.GetResult(server_ret)); |
| 636 } | 865 } |
| 866 | |
| 867 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSuppliedCached) { | |
| 868 scoped_refptr<X509Certificate> client_cert = | |
| 869 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
| 870 ConfigureClientCertsForClient(kWrongClientCertFileName, | |
| 871 kWrongClientPrivateKeyFileName); | |
| 872 ConfigureClientCertsForServer(true); | |
| 873 Initialize(); | |
| 874 | |
| 875 TestCompletionCallback handshake_callback; | |
| 876 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 877 EXPECT_EQ(server_ret, ERR_IO_PENDING); | |
| 878 | |
| 879 TestCompletionCallback connect_callback; | |
| 880 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 881 | |
| 882 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
| 883 connect_callback.GetResult(client_ret)); | |
| 884 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
| 885 handshake_callback.GetResult(server_ret)); | |
| 886 | |
| 887 client_socket_->Disconnect(); | |
| 888 server_socket_->Disconnect(); | |
| 889 | |
| 890 // Below, check that the cache didn't store the result of a failed handshake | |
| 891 Initialize(); | |
| 892 | |
| 893 TestCompletionCallback handshake_callback2; | |
| 894 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); | |
| 895 EXPECT_EQ(server_ret, ERR_IO_PENDING); | |
| 896 | |
| 897 TestCompletionCallback connect_callback2; | |
| 898 int client_ret2 = client_socket_->Connect(connect_callback2.callback()); | |
| 899 | |
| 900 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
| 901 connect_callback2.GetResult(client_ret2)); | |
| 902 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
| 903 handshake_callback2.GetResult(server_ret2)); | |
| 904 } | |
| 637 #endif // defined(USE_OPENSSL) | 905 #endif // defined(USE_OPENSSL) |
| 638 | 906 |
| 639 TEST_F(SSLServerSocketTest, DataTransfer) { | 907 TEST_F(SSLServerSocketTest, DataTransfer) { |
| 640 Initialize(); | 908 Initialize(); |
| 641 | 909 |
| 642 // Establish connection. | 910 // Establish connection. |
| 643 TestCompletionCallback connect_callback; | 911 TestCompletionCallback connect_callback; |
| 644 int client_ret = client_socket_->Connect(connect_callback.callback()); | 912 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 645 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 913 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 646 | 914 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 843 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 1111 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 844 | 1112 |
| 845 client_ret = connect_callback.GetResult(client_ret); | 1113 client_ret = connect_callback.GetResult(client_ret); |
| 846 server_ret = handshake_callback.GetResult(server_ret); | 1114 server_ret = handshake_callback.GetResult(server_ret); |
| 847 | 1115 |
| 848 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 1116 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
| 849 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 1117 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
| 850 } | 1118 } |
| 851 | 1119 |
| 852 } // namespace net | 1120 } // namespace net |
| OLD | NEW |