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

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

Issue 994743003: Support for client certs in ssl_server_socket. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Passing this CL to RyanChung for further work. Created 5 years, 1 month 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
« no previous file with comments | « net/socket/ssl_server_socket_openssl.cc ('k') | net/ssl/ssl_cert_request_info.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 //
(...skipping 12 matching lines...) Expand all
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"
42 #include "net/cert/x509_certificate.h" 43 #include "net/cert/x509_certificate.h"
43 #include "net/http/transport_security_state.h" 44 #include "net/http/transport_security_state.h"
44 #include "net/log/net_log.h" 45 #include "net/log/net_log.h"
45 #include "net/socket/client_socket_factory.h" 46 #include "net/socket/client_socket_factory.h"
46 #include "net/socket/socket_test_util.h" 47 #include "net/socket/socket_test_util.h"
47 #include "net/socket/ssl_client_socket.h" 48 #include "net/socket/ssl_client_socket.h"
48 #include "net/socket/stream_socket.h" 49 #include "net/socket/stream_socket.h"
50 #include "net/ssl/ssl_cert_request_info.h"
49 #include "net/ssl/ssl_cipher_suite_names.h" 51 #include "net/ssl/ssl_cipher_suite_names.h"
50 #include "net/ssl/ssl_connection_status_flags.h" 52 #include "net/ssl/ssl_connection_status_flags.h"
51 #include "net/ssl/ssl_info.h" 53 #include "net/ssl/ssl_info.h"
54 #include "net/ssl/ssl_private_key.h"
52 #include "net/ssl/ssl_server_config.h" 55 #include "net/ssl/ssl_server_config.h"
53 #include "net/test/cert_test_util.h" 56 #include "net/test/cert_test_util.h"
54 #include "testing/gtest/include/gtest/gtest.h" 57 #include "testing/gtest/include/gtest/gtest.h"
55 #include "testing/platform_test.h" 58 #include "testing/platform_test.h"
56 59
57 namespace net { 60 namespace net {
58 61
59 namespace { 62 namespace {
60 63
61 class FakeDataChannel { 64 class FakeDataChannel {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 weak_factory_.GetWeakPtr())); 105 weak_factory_.GetWeakPtr()));
103 return buf_len; 106 return buf_len;
104 } 107 }
105 108
106 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, 109 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
107 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that 110 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
108 // after the FakeDataChannel is closed, the first Write() call completes 111 // after the FakeDataChannel is closed, the first Write() call completes
109 // asynchronously, which is necessary to reproduce bug 127822. 112 // asynchronously, which is necessary to reproduce bug 127822.
110 void Close() { 113 void Close() {
111 closed_ = true; 114 closed_ = true;
115 if (!read_callback_.is_null()) {
116 base::MessageLoop::current()->PostTask(
117 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
118 weak_factory_.GetWeakPtr()));
119 }
112 } 120 }
113 121
114 private: 122 private:
115 void DoReadCallback() { 123 void DoReadCallback() {
116 if (read_callback_.is_null() || data_.empty()) 124 if (read_callback_.is_null() || data_.empty())
117 return; 125 return;
118
119 int copied = PropagateData(read_buf_, read_buf_len_); 126 int copied = PropagateData(read_buf_, read_buf_len_);
120 CompletionCallback callback = read_callback_; 127 CompletionCallback callback = read_callback_;
121 read_callback_.Reset(); 128 read_callback_.Reset();
122 read_buf_ = NULL; 129 read_buf_ = NULL;
123 read_buf_len_ = 0; 130 read_buf_len_ = 0;
124 callback.Run(copied); 131 callback.Run(copied);
125 } 132 }
126 133
127 void DoWriteCallback() { 134 void DoWriteCallback() {
128 if (write_callback_.is_null()) 135 if (write_callback_.is_null())
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 } 254 }
248 255
249 private: 256 private:
250 BoundNetLog net_log_; 257 BoundNetLog net_log_;
251 FakeDataChannel* incoming_; 258 FakeDataChannel* incoming_;
252 FakeDataChannel* outgoing_; 259 FakeDataChannel* outgoing_;
253 260
254 DISALLOW_COPY_AND_ASSIGN(FakeSocket); 261 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
255 }; 262 };
256 263
264 class TestSSLPrivateKey : public SSLPrivateKey {
265 public:
266 TestSSLPrivateKey(crypto::RSAPrivateKey* rsa_private_key)
267 : rsa_private_key_(rsa_private_key) {}
268
269 ~TestSSLPrivateKey() override {}
270
271 Type GetType() override { return SSLPrivateKey::Type::RSA; }
272
273 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override {
274 static const SSLPrivateKey::Hash kHashes[] = {SSLPrivateKey::Hash::SHA256,
275 SSLPrivateKey::Hash::SHA1};
276 return std::vector<SSLPrivateKey::Hash>(kHashes,
277 kHashes + arraysize(kHashes));
278 }
279
280 // NOTE: The following algorithm assumes the answer is a power of 2, which is
281 // true for the test keys in use.
282 size_t GetMaxSignatureLengthInBytes() override {
283 std::vector<uint8> public_key_info;
284 rsa_private_key_->ExportPublicKey(&public_key_info);
285 uint result = 1;
286 while ((result << 1) < public_key_info.size())
287 result <<= 1;
288 return result;
289 }
290
291 void SignDigest(Hash hash,
292 const base::StringPiece& input,
293 const SignCallback& callback) override {
294 std::vector<uint8> signature;
295 crypto::SignatureCreator::HashAlgorithm hash_alg;
296 switch (hash) {
297 case Hash::SHA1:
298 hash_alg = crypto::SignatureCreator::SHA1;
299 break;
300
301 case Hash::SHA256:
302 hash_alg = crypto::SignatureCreator::SHA256;
303 break;
304
305 default:
306 FAIL() << "Unsupported hash function";
307 }
308 crypto::SignatureCreator::Sign(rsa_private_key_.get(), hash_alg,
309 reinterpret_cast<const uint8*>(input.data()),
310 input.size(), &signature);
311 base::ThreadTaskRunnerHandle::Get()->PostTask(
312 FROM_HERE, base::Bind(callback, OK, signature));
313 }
314
315 private:
316 void CompleteSignDigest(Error err, const std::vector<uint8_t>& signature) {}
317 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_;
318
319 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey);
320 };
321
257 } // namespace 322 } // namespace
258 323
259 // Verify the correctness of the test helper classes first. 324 // Verify the correctness of the test helper classes first.
260 TEST(FakeSocketTest, DataTransfer) { 325 TEST(FakeSocketTest, DataTransfer) {
261 // Establish channels between two sockets. 326 // Establish channels between two sockets.
262 FakeDataChannel channel_1; 327 FakeDataChannel channel_1;
263 FakeDataChannel channel_2; 328 FakeDataChannel channel_2;
264 FakeSocket client(&channel_1, &channel_2); 329 FakeSocket client(&channel_1, &channel_2);
265 FakeSocket server(&channel_2, &channel_1); 330 FakeSocket server(&channel_2, &channel_1);
266 331
(...skipping 24 matching lines...) Expand all
291 EXPECT_LE(written, kTestDataSize); 356 EXPECT_LE(written, kTestDataSize);
292 357
293 read = callback.WaitForResult(); 358 read = callback.WaitForResult();
294 EXPECT_GT(read, 0); 359 EXPECT_GT(read, 0);
295 EXPECT_LE(read, written); 360 EXPECT_LE(read, written);
296 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); 361 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
297 } 362 }
298 363
299 class SSLServerSocketTest : public PlatformTest { 364 class SSLServerSocketTest : public PlatformTest {
300 public: 365 public:
366 enum ClientCertSupply {
367 kNoneSupplied = 0,
368 kCorrectCertSupplied = 1,
369 kWrongCertSupplied = 2
370 };
371
372 enum ClientCertExpect { kNoneExpected = 0, kCertRequired = 2 };
373
301 SSLServerSocketTest() 374 SSLServerSocketTest()
302 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), 375 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
303 cert_verifier_(new MockCertVerifier()), 376 cert_verifier_(new MockCertVerifier()),
377 client_cert_verifier_(new MockCertVerifier()),
304 transport_security_state_(new TransportSecurityState) { 378 transport_security_state_(new TransportSecurityState) {
305 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); 379 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID);
380 client_cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID);
306 } 381 }
307 382
308 protected: 383 protected:
309 void Initialize() { 384 void Initialize() {
310 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); 385 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle);
311 client_connection->SetSocket( 386 client_connection->SetSocket(
312 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); 387 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_)));
313 scoped_ptr<StreamSocket> server_socket( 388 scoped_ptr<StreamSocket> server_socket(
314 new FakeSocket(&channel_2_, &channel_1_)); 389 new FakeSocket(&channel_2_, &channel_1_));
315 390
316 base::FilePath certs_dir(GetTestCertsDirectory()); 391 std::string server_cert_der;
317 392 scoped_refptr<X509Certificate> server_cert(
318 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); 393 ReadTestCert("unittest.selfsigned.der", &server_cert_der));
319 std::string cert_der; 394 scoped_ptr<crypto::RSAPrivateKey> server_private_key(
320 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der)); 395 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 396
336 client_ssl_config_.false_start_enabled = false; 397 client_ssl_config_.false_start_enabled = false;
337 client_ssl_config_.channel_id_enabled = false; 398 client_ssl_config_.channel_id_enabled = false;
338 399
339 // Certificate provided by the host doesn't need authority. 400 // Certificate provided by the host doesn't need authority.
340 SSLConfig::CertAndStatus cert_and_status; 401 SSLConfig::CertAndStatus cert_and_status;
341 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; 402 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
342 cert_and_status.der_cert = cert_der; 403 cert_and_status.der_cert = server_cert_der;
343 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); 404 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status);
344 405
406 SSLConfig ssl_server_config;
345 HostPortPair host_and_pair("unittest", 0); 407 HostPortPair host_and_pair("unittest", 0);
346 SSLClientSocketContext context; 408 SSLClientSocketContext context;
347 context.cert_verifier = cert_verifier_.get(); 409 context.cert_verifier = cert_verifier_.get();
348 context.transport_security_state = transport_security_state_.get(); 410 context.transport_security_state = transport_security_state_.get();
411 socket_factory_->ClearSSLSessionCache();
349 client_socket_ = socket_factory_->CreateSSLClientSocket( 412 client_socket_ = socket_factory_->CreateSSLClientSocket(
350 client_connection.Pass(), host_and_pair, client_ssl_config_, context); 413 client_connection.Pass(), host_and_pair, client_ssl_config_, context);
351 server_socket_ = 414 server_socket_ =
352 CreateSSLServerSocket(server_socket.Pass(), cert.get(), 415 CreateSSLServerSocket(server_socket.Pass(), server_cert.get(),
353 private_key.get(), server_ssl_config_); 416 server_private_key.get(), server_ssl_config_);
417 }
418
419 void InitializeClientCertsForClient(ClientCertSupply supply) {
420 scoped_refptr<X509Certificate> cert;
421 scoped_ptr<net::SSLPrivateKey> key;
422 if (supply != kNoneSupplied) {
423 const char* cert_file_name = supply == kCorrectCertSupplied
424 ? kClientCertFileName
425 : kWrongClientCertFileName;
426 const char* private_key_file_name = supply == kCorrectCertSupplied
427 ? kClientPrivateKeyFileName
428 : kWrongClientPrivateKeyFileName;
429 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name);
430 key.reset(new TestSSLPrivateKey(ReadTestKey(private_key_file_name)));
431 }
432 client_socket_->ForceClientCertificateAndKeyForTest(cert, key.Pass());
433 }
434
435 void InitializeClientCertsForServer(ClientCertExpect expect) {
436 if (expect == kNoneExpected)
437 return;
438
439 server_socket_->SetRequireClientCert(true);
440
441 if (expect == kCertRequired) {
442 scoped_refptr<X509Certificate> expected_client_ca_cert(
443 ImportCertFromFile(GetTestCertsDirectory(), kClientCertCAFileName));
444 CertificateList ca_list;
445 ca_list.push_back(expected_client_ca_cert);
446 server_socket_->SetClientCertCAList(ca_list);
447 scoped_refptr<X509Certificate> expected_client_cert(
448 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName));
449 CertVerifyResult ignored;
450 ignored.verified_cert = expected_client_cert;
451 ignored.cert_status = 0;
452 client_cert_verifier_->AddResultForCert(expected_client_cert.get(),
453 ignored, OK);
454 server_socket_->SetClientCertVerifier(client_cert_verifier_.get());
455 }
456 }
457
458 X509Certificate* ReadTestCert(const base::StringPiece& name,
459 std::string* cert_der) {
460 base::FilePath certs_dir(GetTestCertsDirectory());
461 base::FilePath cert_path = certs_dir.AppendASCII(name);
462 std::string unneeded;
463 if (!cert_der) {
464 cert_der = &unneeded;
465 }
466 if (!base::ReadFileToString(cert_path, cert_der))
467 return NULL;
468 return X509Certificate::CreateFromBytes(cert_der->data(), cert_der->size());
469 }
470
471 crypto::RSAPrivateKey* ReadTestKey(const base::StringPiece& name) {
472 base::FilePath certs_dir(GetTestCertsDirectory());
473 base::FilePath key_path = certs_dir.AppendASCII(name);
474 std::string key_string;
475 if (!base::ReadFileToString(key_path, &key_string))
476 return NULL;
477 std::vector<uint8> key_vector(
478 reinterpret_cast<const uint8*>(key_string.data()),
479 reinterpret_cast<const uint8*>(key_string.data() +
480 key_string.length()));
481 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector);
354 } 482 }
355 483
356 FakeDataChannel channel_1_; 484 FakeDataChannel channel_1_;
357 FakeDataChannel channel_2_; 485 FakeDataChannel channel_2_;
358 SSLConfig client_ssl_config_; 486 SSLConfig client_ssl_config_;
359 SSLServerConfig server_ssl_config_; 487 SSLServerConfig server_ssl_config_;
360 scoped_ptr<SSLClientSocket> client_socket_; 488 scoped_ptr<SSLClientSocket> client_socket_;
361 scoped_ptr<SSLServerSocket> server_socket_; 489 scoped_ptr<SSLServerSocket> server_socket_;
362 ClientSocketFactory* socket_factory_; 490 ClientSocketFactory* socket_factory_;
363 scoped_ptr<MockCertVerifier> cert_verifier_; 491 scoped_ptr<MockCertVerifier> cert_verifier_;
492 scoped_ptr<MockCertVerifier> client_cert_verifier_;
364 scoped_ptr<TransportSecurityState> transport_security_state_; 493 scoped_ptr<TransportSecurityState> transport_security_state_;
494 CertificateList trusted_certs_;
495
496 const char* kClientCertFileName = "client_1.pem";
497 const char* kClientPrivateKeyFileName = "client_1.pk8";
498 const char* kWrongClientCertFileName = "client_2.pem";
499 const char* kWrongClientPrivateKeyFileName = "client_2.pk8";
500 const char* kClientCertCAFileName = "client_1_ca.pem";
365 }; 501 };
366 502
367 // This test only executes creation of client and server sockets. This is to 503 // 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 504 // test that creation of sockets doesn't crash and have minimal code to run
369 // under valgrind in order to help debugging memory problems. 505 // under valgrind in order to help debugging memory problems.
370 TEST_F(SSLServerSocketTest, Initialize) { 506 TEST_F(SSLServerSocketTest, Initialize) {
371 Initialize(); 507 Initialize();
372 } 508 }
373 509
374 // This test executes Connect() on SSLClientSocket and Handshake() on 510 // This test executes Connect() on SSLClientSocket and Handshake() on
(...skipping 29 matching lines...) Expand all
404 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); 540 SSLConnectionStatusToCipherSuite(ssl_info.connection_status);
405 const char* key_exchange; 541 const char* key_exchange;
406 const char* cipher; 542 const char* cipher;
407 const char* mac; 543 const char* mac;
408 bool is_aead; 544 bool is_aead;
409 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); 545 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
410 EXPECT_STREQ("ECDHE_RSA", key_exchange); 546 EXPECT_STREQ("ECDHE_RSA", key_exchange);
411 EXPECT_TRUE(is_aead); 547 EXPECT_TRUE(is_aead);
412 } 548 }
413 549
550 // TODO(dougsteed). The following tests using client certificates cannot
551 // be performed if NSS with platform-based client auth is in use. That's because
552 // the tests use SSLClientSocket to make requests against the server, and on
553 // those builds, that class does not support supplying of a test key and cert.
554 // An alternative approach that would broaden the applicability of these tests
555 // would be to build and use the openssl flavor of SSLClientSocket, even
556 // on NSS platforms.
557 #if !defined(USE_NSS) || !defined(NSS_PLATFORM_CLIENT_AUTH)
558
559 // This test executes Connect() on SSLClientSocket and Handshake() on
560 // SSLServerSocket to make sure handshaking between the two sockets is
561 // completed successfully, using client certificate.
562 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) {
563 scoped_refptr<X509Certificate> client_cert =
564 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
565 Initialize();
566 InitializeClientCertsForServer(kCertRequired);
567 InitializeClientCertsForClient(kCorrectCertSupplied);
568
569 TestCompletionCallback connect_callback;
570 TestCompletionCallback handshake_callback;
571
572 int server_ret = server_socket_->Handshake(handshake_callback.callback());
573 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
574
575 int client_ret = client_socket_->Connect(connect_callback.callback());
576 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
577
578 if (client_ret == ERR_IO_PENDING) {
579 EXPECT_EQ(OK, connect_callback.WaitForResult());
580 }
581 if (server_ret == ERR_IO_PENDING) {
582 EXPECT_EQ(OK, handshake_callback.WaitForResult());
583 }
584
585 // Make sure the cert status is expected.
586 SSLInfo ssl_info;
587 client_socket_->GetSSLInfo(&ssl_info);
588 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
589 server_socket_->GetSSLInfo(&ssl_info);
590 EXPECT_TRUE(ssl_info.client_cert_sent);
591 EXPECT_TRUE(ssl_info.client_cert_sent);
592 EXPECT_TRUE(ssl_info.cert.get());
593 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get()));
594 }
595
596 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) {
597 Initialize();
598 InitializeClientCertsForServer(kCertRequired);
599 // We use the default setting for the client socket. This causes the client to
600 // get SSL_CLIENT_AUTH_CERT_NEEDED. This code path allows us to access the
601 // cert_authorities from the CertificateRequest.
602
603 TestCompletionCallback connect_callback;
604 TestCompletionCallback handshake_callback;
605
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 Initialize();
640 InitializeClientCertsForServer(kCertRequired);
641 InitializeClientCertsForClient(kWrongCertSupplied);
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_NSS) || !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
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
OLDNEW
« no previous file with comments | « net/socket/ssl_server_socket_openssl.cc ('k') | net/ssl/ssl_cert_request_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698