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 12 matching lines...) Expand all Loading... |
23 #include "base/compiler_specific.h" | 23 #include "base/compiler_specific.h" |
24 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
25 #include "base/files/file_util.h" | 25 #include "base/files/file_util.h" |
26 #include "base/location.h" | 26 #include "base/location.h" |
27 #include "base/logging.h" | 27 #include "base/logging.h" |
28 #include "base/message_loop/message_loop.h" | 28 #include "base/message_loop/message_loop.h" |
29 #include "base/single_thread_task_runner.h" | 29 #include "base/single_thread_task_runner.h" |
30 #include "base/thread_task_runner_handle.h" | 30 #include "base/thread_task_runner_handle.h" |
31 #include "crypto/nss_util.h" | 31 #include "crypto/nss_util.h" |
32 #include "crypto/rsa_private_key.h" | 32 #include "crypto/rsa_private_key.h" |
| 33 #include "crypto/signature_creator.h" |
33 #include "net/base/address_list.h" | 34 #include "net/base/address_list.h" |
34 #include "net/base/completion_callback.h" | 35 #include "net/base/completion_callback.h" |
35 #include "net/base/host_port_pair.h" | 36 #include "net/base/host_port_pair.h" |
36 #include "net/base/io_buffer.h" | 37 #include "net/base/io_buffer.h" |
37 #include "net/base/ip_endpoint.h" | 38 #include "net/base/ip_endpoint.h" |
38 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
39 #include "net/base/test_data_directory.h" | 40 #include "net/base/test_data_directory.h" |
40 #include "net/cert/cert_status_flags.h" | 41 #include "net/cert/cert_status_flags.h" |
41 #include "net/cert/mock_cert_verifier.h" | 42 #include "net/cert/mock_cert_verifier.h" |
| 43 #include "net/cert/mock_client_cert_verifier.h" |
42 #include "net/cert/x509_certificate.h" | 44 #include "net/cert/x509_certificate.h" |
43 #include "net/http/transport_security_state.h" | 45 #include "net/http/transport_security_state.h" |
44 #include "net/log/net_log.h" | 46 #include "net/log/net_log.h" |
45 #include "net/socket/client_socket_factory.h" | 47 #include "net/socket/client_socket_factory.h" |
46 #include "net/socket/socket_test_util.h" | 48 #include "net/socket/socket_test_util.h" |
47 #include "net/socket/ssl_client_socket.h" | 49 #include "net/socket/ssl_client_socket.h" |
48 #include "net/socket/stream_socket.h" | 50 #include "net/socket/stream_socket.h" |
| 51 #include "net/ssl/ssl_cert_request_info.h" |
49 #include "net/ssl/ssl_cipher_suite_names.h" | 52 #include "net/ssl/ssl_cipher_suite_names.h" |
50 #include "net/ssl/ssl_connection_status_flags.h" | 53 #include "net/ssl/ssl_connection_status_flags.h" |
51 #include "net/ssl/ssl_info.h" | 54 #include "net/ssl/ssl_info.h" |
| 55 #include "net/ssl/ssl_private_key.h" |
52 #include "net/ssl/ssl_server_config.h" | 56 #include "net/ssl/ssl_server_config.h" |
53 #include "net/test/cert_test_util.h" | 57 #include "net/test/cert_test_util.h" |
54 #include "testing/gtest/include/gtest/gtest.h" | 58 #include "testing/gtest/include/gtest/gtest.h" |
55 #include "testing/platform_test.h" | 59 #include "testing/platform_test.h" |
56 | 60 |
57 namespace net { | 61 namespace net { |
58 | 62 |
59 namespace { | 63 namespace { |
60 | 64 |
| 65 const char kClientCertFileName[] = "client_1.pem"; |
| 66 const char kClientPrivateKeyFileName[] = "client_1.pk8"; |
| 67 const char kWrongClientCertFileName[] = "client_2.pem"; |
| 68 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; |
| 69 const char kClientCertCAFileName[] = "client_1_ca.pem"; |
| 70 |
61 class FakeDataChannel { | 71 class FakeDataChannel { |
62 public: | 72 public: |
63 FakeDataChannel() | 73 FakeDataChannel() |
64 : read_buf_len_(0), | 74 : read_buf_len_(0), |
65 closed_(false), | 75 closed_(false), |
66 write_called_after_close_(false), | 76 write_called_after_close_(false), |
67 weak_factory_(this) { | 77 weak_factory_(this) { |
68 } | 78 } |
69 | 79 |
70 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 80 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 weak_factory_.GetWeakPtr())); | 112 weak_factory_.GetWeakPtr())); |
103 return buf_len; | 113 return buf_len; |
104 } | 114 } |
105 | 115 |
106 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, | 116 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
107 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that | 117 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
108 // after the FakeDataChannel is closed, the first Write() call completes | 118 // after the FakeDataChannel is closed, the first Write() call completes |
109 // asynchronously, which is necessary to reproduce bug 127822. | 119 // asynchronously, which is necessary to reproduce bug 127822. |
110 void Close() { | 120 void Close() { |
111 closed_ = true; | 121 closed_ = true; |
| 122 data_.push( |
| 123 new DrainableIOBuffer(new StringIOBuffer(std::string("0", 1)), 1)); |
| 124 if (!read_callback_.is_null()) { |
| 125 base::MessageLoop::current()->PostTask( |
| 126 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, |
| 127 weak_factory_.GetWeakPtr())); |
| 128 } |
112 } | 129 } |
113 | 130 |
114 private: | 131 private: |
115 void DoReadCallback() { | 132 void DoReadCallback() { |
116 if (read_callback_.is_null() || data_.empty()) | 133 if (read_callback_.is_null() || data_.empty()) |
117 return; | 134 return; |
118 | |
119 int copied = PropagateData(read_buf_, read_buf_len_); | 135 int copied = PropagateData(read_buf_, read_buf_len_); |
120 CompletionCallback callback = read_callback_; | 136 CompletionCallback callback = read_callback_; |
121 read_callback_.Reset(); | 137 read_callback_.Reset(); |
122 read_buf_ = NULL; | 138 read_buf_ = NULL; |
123 read_buf_len_ = 0; | 139 read_buf_len_ = 0; |
124 callback.Run(copied); | 140 callback.Run(copied); |
125 } | 141 } |
126 | 142 |
127 void DoWriteCallback() { | 143 void DoWriteCallback() { |
128 if (write_callback_.is_null()) | 144 if (write_callback_.is_null()) |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 263 } |
248 | 264 |
249 private: | 265 private: |
250 BoundNetLog net_log_; | 266 BoundNetLog net_log_; |
251 FakeDataChannel* incoming_; | 267 FakeDataChannel* incoming_; |
252 FakeDataChannel* outgoing_; | 268 FakeDataChannel* outgoing_; |
253 | 269 |
254 DISALLOW_COPY_AND_ASSIGN(FakeSocket); | 270 DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
255 }; | 271 }; |
256 | 272 |
| 273 class TestSSLPrivateKey : public SSLPrivateKey { |
| 274 public: |
| 275 TestSSLPrivateKey(crypto::RSAPrivateKey* rsa_private_key) |
| 276 : rsa_private_key_(rsa_private_key) {} |
| 277 |
| 278 Type GetType() override { return SSLPrivateKey::Type::RSA; } |
| 279 |
| 280 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { |
| 281 static const SSLPrivateKey::Hash kHashes[] = {SSLPrivateKey::Hash::SHA256, |
| 282 SSLPrivateKey::Hash::SHA1}; |
| 283 return std::vector<SSLPrivateKey::Hash>(kHashes, |
| 284 kHashes + arraysize(kHashes)); |
| 285 } |
| 286 |
| 287 // NOTE: The following algorithm assumes the answer is a power of 2, which is |
| 288 // true for the test keys in use. |
| 289 size_t GetMaxSignatureLengthInBytes() override { |
| 290 std::vector<uint8> public_key_info; |
| 291 rsa_private_key_->ExportPublicKey(&public_key_info); |
| 292 uint result = 1; |
| 293 while ((result << 1) < public_key_info.size()) |
| 294 result <<= 1; |
| 295 return result; |
| 296 } |
| 297 |
| 298 void SignDigest(Hash hash, |
| 299 const base::StringPiece& input, |
| 300 const SignCallback& callback) override { |
| 301 std::vector<uint8> signature; |
| 302 crypto::SignatureCreator::HashAlgorithm hash_alg; |
| 303 switch (hash) { |
| 304 case Hash::SHA1: |
| 305 hash_alg = crypto::SignatureCreator::SHA1; |
| 306 break; |
| 307 |
| 308 case Hash::SHA256: |
| 309 hash_alg = crypto::SignatureCreator::SHA256; |
| 310 break; |
| 311 |
| 312 default: |
| 313 FAIL() << "Unsupported hash function"; |
| 314 } |
| 315 crypto::SignatureCreator::Sign(rsa_private_key_.get(), hash_alg, |
| 316 reinterpret_cast<const uint8*>(input.data()), |
| 317 input.size(), &signature); |
| 318 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 319 FROM_HERE, base::Bind(callback, OK, signature)); |
| 320 } |
| 321 |
| 322 private: |
| 323 ~TestSSLPrivateKey() override {} |
| 324 void CompleteSignDigest(Error err, const std::vector<uint8_t>& signature) {} |
| 325 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_; |
| 326 |
| 327 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey); |
| 328 }; |
| 329 |
257 } // namespace | 330 } // namespace |
258 | 331 |
259 // Verify the correctness of the test helper classes first. | 332 // Verify the correctness of the test helper classes first. |
260 TEST(FakeSocketTest, DataTransfer) { | 333 TEST(FakeSocketTest, DataTransfer) { |
261 // Establish channels between two sockets. | 334 // Establish channels between two sockets. |
262 FakeDataChannel channel_1; | 335 FakeDataChannel channel_1; |
263 FakeDataChannel channel_2; | 336 FakeDataChannel channel_2; |
264 FakeSocket client(&channel_1, &channel_2); | 337 FakeSocket client(&channel_1, &channel_2); |
265 FakeSocket server(&channel_2, &channel_1); | 338 FakeSocket server(&channel_2, &channel_1); |
266 | 339 |
(...skipping 24 matching lines...) Expand all Loading... |
291 EXPECT_LE(written, kTestDataSize); | 364 EXPECT_LE(written, kTestDataSize); |
292 | 365 |
293 read = callback.WaitForResult(); | 366 read = callback.WaitForResult(); |
294 EXPECT_GT(read, 0); | 367 EXPECT_GT(read, 0); |
295 EXPECT_LE(read, written); | 368 EXPECT_LE(read, written); |
296 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); | 369 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
297 } | 370 } |
298 | 371 |
299 class SSLServerSocketTest : public PlatformTest { | 372 class SSLServerSocketTest : public PlatformTest { |
300 public: | 373 public: |
| 374 enum ClientCertSupply { |
| 375 kNoneSupplied = 0, |
| 376 kCorrectCertSupplied = 1, |
| 377 kWrongCertSupplied = 2 |
| 378 }; |
| 379 |
| 380 enum ClientCertExpect { kNoneExpected = 0, kCertRequired = 2 }; |
| 381 |
301 SSLServerSocketTest() | 382 SSLServerSocketTest() |
302 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 383 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
303 cert_verifier_(new MockCertVerifier()), | 384 cert_verifier_(new MockCertVerifier()), |
| 385 client_cert_verifier_(new MockClientCertVerifier()), |
304 transport_security_state_(new TransportSecurityState) { | 386 transport_security_state_(new TransportSecurityState) { |
305 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); | 387 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); |
| 388 client_cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); |
306 } | 389 } |
307 | 390 |
308 protected: | 391 protected: |
309 void Initialize() { | 392 void Initialize() { |
310 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); | 393 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); |
311 client_connection->SetSocket( | 394 client_connection->SetSocket( |
312 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); | 395 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); |
313 scoped_ptr<StreamSocket> server_socket( | 396 scoped_ptr<StreamSocket> server_socket( |
314 new FakeSocket(&channel_2_, &channel_1_)); | 397 new FakeSocket(&channel_2_, &channel_1_)); |
315 | 398 |
316 base::FilePath certs_dir(GetTestCertsDirectory()); | 399 std::string server_cert_der; |
317 | 400 scoped_refptr<X509Certificate> server_cert( |
318 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 401 ReadTestCert("unittest.selfsigned.der", &server_cert_der)); |
319 std::string cert_der; | 402 scoped_ptr<crypto::RSAPrivateKey> server_private_key( |
320 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der)); | 403 ReadTestKey("unittest.key.bin")); |
321 | |
322 scoped_refptr<X509Certificate> cert = | |
323 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size()); | |
324 | |
325 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | |
326 std::string key_string; | |
327 ASSERT_TRUE(base::ReadFileToString(key_path, &key_string)); | |
328 std::vector<uint8> key_vector( | |
329 reinterpret_cast<const uint8*>(key_string.data()), | |
330 reinterpret_cast<const uint8*>(key_string.data() + | |
331 key_string.length())); | |
332 | |
333 scoped_ptr<crypto::RSAPrivateKey> private_key( | |
334 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | |
335 | 404 |
336 client_ssl_config_.false_start_enabled = false; | 405 client_ssl_config_.false_start_enabled = false; |
337 client_ssl_config_.channel_id_enabled = false; | 406 client_ssl_config_.channel_id_enabled = false; |
338 | 407 |
339 // Certificate provided by the host doesn't need authority. | 408 // Certificate provided by the host doesn't need authority. |
340 SSLConfig::CertAndStatus cert_and_status; | 409 SSLConfig::CertAndStatus cert_and_status; |
341 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; | 410 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; |
342 cert_and_status.der_cert = cert_der; | 411 cert_and_status.der_cert = server_cert_der; |
343 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); | 412 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); |
344 | 413 |
345 HostPortPair host_and_pair("unittest", 0); | 414 HostPortPair host_and_pair("unittest", 0); |
346 SSLClientSocketContext context; | 415 SSLClientSocketContext context; |
347 context.cert_verifier = cert_verifier_.get(); | 416 context.cert_verifier = cert_verifier_.get(); |
348 context.transport_security_state = transport_security_state_.get(); | 417 context.transport_security_state = transport_security_state_.get(); |
| 418 socket_factory_->ClearSSLSessionCache(); |
349 client_socket_ = socket_factory_->CreateSSLClientSocket( | 419 client_socket_ = socket_factory_->CreateSSLClientSocket( |
350 client_connection.Pass(), host_and_pair, client_ssl_config_, context); | 420 client_connection.Pass(), host_and_pair, client_ssl_config_, context); |
351 server_socket_ = | 421 server_socket_ = |
352 CreateSSLServerSocket(server_socket.Pass(), cert.get(), | 422 CreateSSLServerSocket(server_socket.Pass(), server_cert.get(), |
353 private_key.get(), server_ssl_config_); | 423 server_private_key.get(), server_ssl_config_); |
| 424 } |
| 425 |
| 426 void InitializeClientCertsForClient(ClientCertSupply supply) { |
| 427 scoped_refptr<X509Certificate> cert; |
| 428 scoped_refptr<net::SSLPrivateKey> key; |
| 429 if (supply != kNoneSupplied) { |
| 430 const char* cert_file_name = supply == kCorrectCertSupplied |
| 431 ? kClientCertFileName |
| 432 : kWrongClientCertFileName; |
| 433 const char* private_key_file_name = supply == kCorrectCertSupplied |
| 434 ? kClientPrivateKeyFileName |
| 435 : kWrongClientPrivateKeyFileName; |
| 436 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); |
| 437 key = new TestSSLPrivateKey(ReadTestKey(private_key_file_name)); |
| 438 } |
| 439 client_ssl_config_.send_client_cert = true; |
| 440 client_ssl_config_.client_cert = cert; |
| 441 client_ssl_config_.client_private_key = key; |
| 442 } |
| 443 |
| 444 void InitializeClientCertsForServer(ClientCertExpect expect) { |
| 445 if (expect == kNoneExpected) |
| 446 return; |
| 447 |
| 448 server_ssl_config_.require_client_cert = true; |
| 449 |
| 450 if (expect == kCertRequired) { |
| 451 scoped_refptr<X509Certificate> expected_client_ca_cert( |
| 452 ImportCertFromFile(GetTestCertsDirectory(), kClientCertCAFileName)); |
| 453 CertificateList ca_list; |
| 454 ca_list.push_back(expected_client_ca_cert); |
| 455 server_ssl_config_.client_cert_ca_list = ca_list; |
| 456 scoped_refptr<X509Certificate> expected_client_cert( |
| 457 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName)); |
| 458 CertVerifyResult ignored; |
| 459 ignored.verified_cert = expected_client_cert; |
| 460 ignored.cert_status = 0; |
| 461 client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK); |
| 462 |
| 463 server_ssl_config_.client_cert_verifier = client_cert_verifier_.get(); |
| 464 } |
| 465 } |
| 466 |
| 467 X509Certificate* ReadTestCert(const base::StringPiece& name, |
| 468 std::string* cert_der) { |
| 469 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 470 base::FilePath cert_path = certs_dir.AppendASCII(name); |
| 471 std::string unneeded; |
| 472 if (!cert_der) |
| 473 cert_der = &unneeded; |
| 474 if (!base::ReadFileToString(cert_path, cert_der)) |
| 475 return NULL; |
| 476 return X509Certificate::CreateFromBytes(cert_der->data(), cert_der->size()); |
| 477 } |
| 478 |
| 479 crypto::RSAPrivateKey* ReadTestKey(const base::StringPiece& name) { |
| 480 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 481 base::FilePath key_path = certs_dir.AppendASCII(name); |
| 482 std::string key_string; |
| 483 if (!base::ReadFileToString(key_path, &key_string)) |
| 484 return NULL; |
| 485 std::vector<uint8> key_vector( |
| 486 reinterpret_cast<const uint8*>(key_string.data()), |
| 487 reinterpret_cast<const uint8*>(key_string.data() + |
| 488 key_string.length())); |
| 489 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector); |
354 } | 490 } |
355 | 491 |
356 FakeDataChannel channel_1_; | 492 FakeDataChannel channel_1_; |
357 FakeDataChannel channel_2_; | 493 FakeDataChannel channel_2_; |
358 SSLConfig client_ssl_config_; | 494 SSLConfig client_ssl_config_; |
359 SSLServerConfig server_ssl_config_; | 495 SSLServerConfig server_ssl_config_; |
360 scoped_ptr<SSLClientSocket> client_socket_; | 496 scoped_ptr<SSLClientSocket> client_socket_; |
361 scoped_ptr<SSLServerSocket> server_socket_; | 497 scoped_ptr<SSLServerSocket> server_socket_; |
362 ClientSocketFactory* socket_factory_; | 498 ClientSocketFactory* socket_factory_; |
363 scoped_ptr<MockCertVerifier> cert_verifier_; | 499 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 500 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; |
364 scoped_ptr<TransportSecurityState> transport_security_state_; | 501 scoped_ptr<TransportSecurityState> transport_security_state_; |
| 502 CertificateList trusted_certs_; |
365 }; | 503 }; |
366 | 504 |
367 // This test only executes creation of client and server sockets. This is to | 505 // This test only executes creation of client and server sockets. This is to |
368 // test that creation of sockets doesn't crash and have minimal code to run | 506 // test that creation of sockets doesn't crash and have minimal code to run |
369 // under valgrind in order to help debugging memory problems. | 507 // under valgrind in order to help debugging memory problems. |
370 TEST_F(SSLServerSocketTest, Initialize) { | 508 TEST_F(SSLServerSocketTest, Initialize) { |
371 Initialize(); | 509 Initialize(); |
372 } | 510 } |
373 | 511 |
374 // This test executes Connect() on SSLClientSocket and Handshake() on | 512 // This test executes Connect() on SSLClientSocket and Handshake() on |
(...skipping 29 matching lines...) Expand all Loading... |
404 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); | 542 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
405 const char* key_exchange; | 543 const char* key_exchange; |
406 const char* cipher; | 544 const char* cipher; |
407 const char* mac; | 545 const char* mac; |
408 bool is_aead; | 546 bool is_aead; |
409 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); | 547 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
410 EXPECT_STREQ("ECDHE_RSA", key_exchange); | 548 EXPECT_STREQ("ECDHE_RSA", key_exchange); |
411 EXPECT_TRUE(is_aead); | 549 EXPECT_TRUE(is_aead); |
412 } | 550 } |
413 | 551 |
| 552 // TODO(dougsteed) The following tests using client certificates cannot |
| 553 // be performed if NSS with platform-based client auth is in use. That's because |
| 554 // the tests use SSLClientSocket to make requests against the server, and on |
| 555 // those builds, that class does not support supplying of a test key and cert. |
| 556 // An alternative approach that would broaden the applicability of these tests |
| 557 // would be to build and use the openssl flavor of SSLClientSocket, even |
| 558 // on NSS platforms. |
| 559 #if defined(USE_OPENSSL) || !defined(NSS_PLATFORM_CLIENT_AUTH) |
| 560 |
| 561 // This test executes Connect() on SSLClientSocket and Handshake() on |
| 562 // SSLServerSocket to make sure handshaking between the two sockets is |
| 563 // completed successfully, using client certificate. |
| 564 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { |
| 565 scoped_refptr<X509Certificate> client_cert = |
| 566 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 567 InitializeClientCertsForClient(kCorrectCertSupplied); |
| 568 InitializeClientCertsForServer(kCertRequired); |
| 569 Initialize(); |
| 570 |
| 571 TestCompletionCallback connect_callback; |
| 572 TestCompletionCallback handshake_callback; |
| 573 |
| 574 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 575 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 576 |
| 577 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 578 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 579 |
| 580 if (client_ret == ERR_IO_PENDING) { |
| 581 EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 582 } |
| 583 if (server_ret == ERR_IO_PENDING) { |
| 584 EXPECT_EQ(OK, handshake_callback.WaitForResult()); |
| 585 } |
| 586 |
| 587 // Make sure the cert status is expected. |
| 588 SSLInfo ssl_info; |
| 589 client_socket_->GetSSLInfo(&ssl_info); |
| 590 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 591 server_socket_->GetSSLInfo(&ssl_info); |
| 592 EXPECT_TRUE(ssl_info.client_cert_sent); |
| 593 EXPECT_TRUE(ssl_info.cert.get()); |
| 594 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); |
| 595 } |
| 596 |
| 597 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { |
| 598 InitializeClientCertsForServer(kCertRequired); |
| 599 Initialize(); |
| 600 // We use the default setting for the client socket. This causes the client to |
| 601 // get SSL_CLIENT_AUTH_CERT_NEEDED. This code path allows us to access the |
| 602 // cert_authorities from the CertificateRequest. |
| 603 |
| 604 TestCompletionCallback connect_callback; |
| 605 TestCompletionCallback handshake_callback; |
| 606 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 607 EXPECT_TRUE(server_ret == ERR_IO_PENDING); |
| 608 |
| 609 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 610 EXPECT_TRUE(client_ret == ERR_SSL_CLIENT_AUTH_CERT_NEEDED || |
| 611 client_ret == ERR_IO_PENDING); |
| 612 |
| 613 if (client_ret == ERR_IO_PENDING) { |
| 614 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
| 615 connect_callback.WaitForResult()); |
| 616 } |
| 617 |
| 618 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); |
| 619 client_socket_->GetSSLCertRequestInfo(request_info.get()); |
| 620 |
| 621 // Check that the authority name that arrived in the CertificateRequest |
| 622 // handshake message is as expected. |
| 623 scoped_refptr<X509Certificate> client_cert = |
| 624 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 625 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); |
| 626 |
| 627 client_socket_->Disconnect(); |
| 628 |
| 629 if (server_ret == ERR_IO_PENDING) { |
| 630 server_ret = handshake_callback.WaitForResult(); |
| 631 EXPECT_TRUE(server_ret == ERR_CONNECTION_CLOSED || |
| 632 server_ret == ERR_FAILED); |
| 633 } |
| 634 } |
| 635 |
| 636 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { |
| 637 scoped_refptr<X509Certificate> client_cert = |
| 638 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 639 InitializeClientCertsForClient(kWrongCertSupplied); |
| 640 InitializeClientCertsForServer(kCertRequired); |
| 641 Initialize(); |
| 642 |
| 643 TestCompletionCallback connect_callback; |
| 644 TestCompletionCallback handshake_callback; |
| 645 |
| 646 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 647 EXPECT_TRUE(server_ret == ERR_IO_PENDING); |
| 648 |
| 649 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 650 EXPECT_TRUE(client_ret == ERR_BAD_SSL_CLIENT_AUTH_CERT || |
| 651 client_ret == ERR_IO_PENDING); |
| 652 |
| 653 if (client_ret == ERR_IO_PENDING) { |
| 654 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, connect_callback.WaitForResult()); |
| 655 } |
| 656 |
| 657 server_ret = handshake_callback.WaitForResult(); |
| 658 // We get a different result on NSS and OpenSSL. That's because an error |
| 659 // mapping with OpenSSL makes an assumption that is true for SSLClientSocket |
| 660 // but not SSLServerSocket (namely that peer cert rejection only occurs due to |
| 661 // a cert change during renego). |
| 662 EXPECT_TRUE(server_ret == ERR_BAD_SSL_CLIENT_AUTH_CERT || |
| 663 server_ret == ERR_SSL_SERVER_CERT_CHANGED); |
| 664 } |
| 665 #endif // defined(USE_OPENSSL) || !defined(NSS_PLATFORM_CLIENT_AUTH) |
| 666 |
414 TEST_F(SSLServerSocketTest, DataTransfer) { | 667 TEST_F(SSLServerSocketTest, DataTransfer) { |
415 Initialize(); | 668 Initialize(); |
416 | 669 |
417 TestCompletionCallback connect_callback; | 670 TestCompletionCallback connect_callback; |
418 TestCompletionCallback handshake_callback; | 671 TestCompletionCallback handshake_callback; |
419 | 672 |
420 // Establish connection. | 673 // Establish connection. |
421 int client_ret = client_socket_->Connect(connect_callback.callback()); | 674 int client_ret = client_socket_->Connect(connect_callback.callback()); |
422 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 675 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
423 | 676 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 874 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
622 | 875 |
623 client_ret = connect_callback.GetResult(client_ret); | 876 client_ret = connect_callback.GetResult(client_ret); |
624 server_ret = handshake_callback.GetResult(server_ret); | 877 server_ret = handshake_callback.GetResult(server_ret); |
625 | 878 |
626 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 879 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 880 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
628 } | 881 } |
629 | 882 |
630 } // namespace net | 883 } // namespace net |
OLD | NEW |