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

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

Issue 1474983003: Support for client certs in ssl_server_socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses reviewer comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This test suite uses SSLClientSocket to test the implementation of 5 // This test suite uses SSLClientSocket to test the implementation of
6 // SSLServerSocket. In order to establish connections between the sockets 6 // SSLServerSocket. In order to establish connections between the sockets
7 // we need two additional classes: 7 // we need two additional classes:
8 // 1. FakeSocket 8 // 1. FakeSocket
9 // Connects SSL socket to FakeDataChannel. This class is just a stub. 9 // Connects SSL socket to FakeDataChannel. This class is just a stub.
10 // 10 //
11 // 2. FakeDataChannel 11 // 2. FakeDataChannel
12 // Implements the actual exchange of data between two FakeSockets. 12 // Implements the actual exchange of data between two FakeSockets.
13 // 13 //
14 // Implementations of these two classes are included in this file. 14 // Implementations of these two classes are included in this file.
15 15
16 #include "net/socket/ssl_server_socket.h" 16 #include "net/socket/ssl_server_socket.h"
17 17
18 #include <openssl/evp.h>
19
18 #include <stdint.h> 20 #include <stdint.h>
19 #include <stdlib.h> 21 #include <stdlib.h>
20 22
21 #include <queue> 23 #include <queue>
22 24
23 #include "base/compiler_specific.h" 25 #include "base/compiler_specific.h"
24 #include "base/files/file_path.h" 26 #include "base/files/file_path.h"
25 #include "base/files/file_util.h" 27 #include "base/files/file_util.h"
26 #include "base/location.h" 28 #include "base/location.h"
27 #include "base/logging.h" 29 #include "base/logging.h"
28 #include "base/message_loop/message_loop.h" 30 #include "base/message_loop/message_loop.h"
29 #include "base/single_thread_task_runner.h" 31 #include "base/single_thread_task_runner.h"
30 #include "base/thread_task_runner_handle.h" 32 #include "base/thread_task_runner_handle.h"
31 #include "crypto/nss_util.h" 33 #include "crypto/nss_util.h"
32 #include "crypto/rsa_private_key.h" 34 #include "crypto/rsa_private_key.h"
35 #include "crypto/signature_creator.h"
33 #include "net/base/address_list.h" 36 #include "net/base/address_list.h"
34 #include "net/base/completion_callback.h" 37 #include "net/base/completion_callback.h"
35 #include "net/base/host_port_pair.h" 38 #include "net/base/host_port_pair.h"
36 #include "net/base/io_buffer.h" 39 #include "net/base/io_buffer.h"
37 #include "net/base/ip_endpoint.h" 40 #include "net/base/ip_endpoint.h"
38 #include "net/base/net_errors.h" 41 #include "net/base/net_errors.h"
39 #include "net/base/test_data_directory.h" 42 #include "net/base/test_data_directory.h"
40 #include "net/cert/cert_status_flags.h" 43 #include "net/cert/cert_status_flags.h"
41 #include "net/cert/mock_cert_verifier.h" 44 #include "net/cert/mock_cert_verifier.h"
45 #include "net/cert/mock_client_cert_verifier.h"
42 #include "net/cert/x509_certificate.h" 46 #include "net/cert/x509_certificate.h"
43 #include "net/http/transport_security_state.h" 47 #include "net/http/transport_security_state.h"
44 #include "net/log/net_log.h" 48 #include "net/log/net_log.h"
45 #include "net/socket/client_socket_factory.h" 49 #include "net/socket/client_socket_factory.h"
46 #include "net/socket/socket_test_util.h" 50 #include "net/socket/socket_test_util.h"
47 #include "net/socket/ssl_client_socket.h" 51 #include "net/socket/ssl_client_socket.h"
48 #include "net/socket/stream_socket.h" 52 #include "net/socket/stream_socket.h"
53 #include "net/ssl/ssl_cert_request_info.h"
49 #include "net/ssl/ssl_cipher_suite_names.h" 54 #include "net/ssl/ssl_cipher_suite_names.h"
50 #include "net/ssl/ssl_connection_status_flags.h" 55 #include "net/ssl/ssl_connection_status_flags.h"
51 #include "net/ssl/ssl_info.h" 56 #include "net/ssl/ssl_info.h"
57 #include "net/ssl/ssl_private_key.h"
52 #include "net/ssl/ssl_server_config.h" 58 #include "net/ssl/ssl_server_config.h"
53 #include "net/test/cert_test_util.h" 59 #include "net/test/cert_test_util.h"
54 #include "testing/gtest/include/gtest/gtest.h" 60 #include "testing/gtest/include/gtest/gtest.h"
55 #include "testing/platform_test.h" 61 #include "testing/platform_test.h"
56 62
57 namespace net { 63 namespace net {
58 64
59 namespace { 65 namespace {
60 66
67 const char kClientCertFileName[] = "client_1.pem";
68 const char kClientPrivateKeyFileName[] = "client_1.pk8";
69 const char kWrongClientCertFileName[] = "client_2.pem";
70 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8";
71 const char kClientCertCAFileName[] = "client_1_ca.pem";
72
61 class FakeDataChannel { 73 class FakeDataChannel {
62 public: 74 public:
63 FakeDataChannel() 75 FakeDataChannel()
64 : read_buf_len_(0), 76 : read_buf_len_(0),
65 closed_(false), 77 closed_(false),
66 write_called_after_close_(false), 78 write_called_after_close_(false),
67 weak_factory_(this) { 79 weak_factory_(this) {
68 } 80 }
69 81
70 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { 82 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 weak_factory_.GetWeakPtr())); 114 weak_factory_.GetWeakPtr()));
103 return buf_len; 115 return buf_len;
104 } 116 }
105 117
106 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, 118 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
107 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that 119 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
108 // after the FakeDataChannel is closed, the first Write() call completes 120 // after the FakeDataChannel is closed, the first Write() call completes
109 // asynchronously, which is necessary to reproduce bug 127822. 121 // asynchronously, which is necessary to reproduce bug 127822.
110 void Close() { 122 void Close() {
111 closed_ = true; 123 closed_ = true;
124 data_.push(
125 new DrainableIOBuffer(new StringIOBuffer(std::string("0", 1)), 1));
126 if (!read_callback_.is_null()) {
127 base::MessageLoop::current()->PostTask(
Ryan Sleevi 2015/12/17 03:47:36 BUG: Use base::ThreadTaskRunnerHandle() over base:
ryanchung 2015/12/18 00:00:56 Done.
128 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
129 weak_factory_.GetWeakPtr()));
130 }
112 } 131 }
113 132
114 private: 133 private:
115 void DoReadCallback() { 134 void DoReadCallback() {
116 if (read_callback_.is_null() || data_.empty()) 135 if (read_callback_.is_null() || data_.empty())
117 return; 136 return;
118
119 int copied = PropagateData(read_buf_, read_buf_len_); 137 int copied = PropagateData(read_buf_, read_buf_len_);
120 CompletionCallback callback = read_callback_; 138 CompletionCallback callback = read_callback_;
121 read_callback_.Reset(); 139 read_callback_.Reset();
122 read_buf_ = NULL; 140 read_buf_ = NULL;
123 read_buf_len_ = 0; 141 read_buf_len_ = 0;
124 callback.Run(copied); 142 callback.Run(copied);
125 } 143 }
126 144
127 void DoWriteCallback() { 145 void DoWriteCallback() {
128 if (write_callback_.is_null()) 146 if (write_callback_.is_null())
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 } 265 }
248 266
249 private: 267 private:
250 BoundNetLog net_log_; 268 BoundNetLog net_log_;
251 FakeDataChannel* incoming_; 269 FakeDataChannel* incoming_;
252 FakeDataChannel* outgoing_; 270 FakeDataChannel* outgoing_;
253 271
254 DISALLOW_COPY_AND_ASSIGN(FakeSocket); 272 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
255 }; 273 };
256 274
275 class TestSSLPrivateKey : public SSLPrivateKey {
276 public:
277 TestSSLPrivateKey(crypto::RSAPrivateKey* rsa_private_key)
278 : rsa_private_key_(rsa_private_key) {}
279
280 Type GetType() override { return SSLPrivateKey::Type::RSA; }
281
282 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override {
283 static const SSLPrivateKey::Hash kHashes[] = {SSLPrivateKey::Hash::SHA256,
284 SSLPrivateKey::Hash::SHA1};
285 return std::vector<SSLPrivateKey::Hash>(kHashes,
286 kHashes + arraysize(kHashes));
Ryan Sleevi 2015/12/17 03:47:36 Could do return std::vector<SSLPrivateKey::Hash>(
ryanchung 2015/12/18 00:00:56 Done.
287 }
288
289 // NOTE: The following algorithm assumes the answer is a power of 2, which is
290 // true for the test keys in use.
291 size_t GetMaxSignatureLengthInBytes() override {
292 #if defined(USE_OPENSSL)
293 return EVP_PKEY_size(rsa_private_key_->key());
294 #else
295 NOTIMPLEMENTED();
296 return 0;
297 #endif
298 }
299
300 void SignDigest(Hash hash,
301 const base::StringPiece& input,
302 const SignCallback& callback) override {
303 std::vector<uint8_t> signature;
304 crypto::SignatureCreator::HashAlgorithm hash_alg;
305 switch (hash) {
306 case Hash::SHA1:
307 hash_alg = crypto::SignatureCreator::SHA1;
308 break;
309
310 case Hash::SHA256:
311 hash_alg = crypto::SignatureCreator::SHA256;
312 break;
313
314 default:
315 FAIL() << "Unsupported hash function";
316 }
317 crypto::SignatureCreator::Sign(
318 rsa_private_key_.get(), hash_alg,
319 reinterpret_cast<const uint8_t*>(input.data()), input.size(),
320 &signature);
321 base::ThreadTaskRunnerHandle::Get()->PostTask(
322 FROM_HERE, base::Bind(callback, OK, signature));
323 }
324
325 private:
326 ~TestSSLPrivateKey() override {}
327
328 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_;
329
330 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey);
331 };
332
257 } // namespace 333 } // namespace
258 334
259 // Verify the correctness of the test helper classes first. 335 // Verify the correctness of the test helper classes first.
260 TEST(FakeSocketTest, DataTransfer) { 336 TEST(FakeSocketTest, DataTransfer) {
261 // Establish channels between two sockets. 337 // Establish channels between two sockets.
262 FakeDataChannel channel_1; 338 FakeDataChannel channel_1;
263 FakeDataChannel channel_2; 339 FakeDataChannel channel_2;
264 FakeSocket client(&channel_1, &channel_2); 340 FakeSocket client(&channel_1, &channel_2);
265 FakeSocket server(&channel_2, &channel_1); 341 FakeSocket server(&channel_2, &channel_1);
266 342
(...skipping 27 matching lines...) Expand all
294 EXPECT_GT(read, 0); 370 EXPECT_GT(read, 0);
295 EXPECT_LE(read, written); 371 EXPECT_LE(read, written);
296 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); 372 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
297 } 373 }
298 374
299 class SSLServerSocketTest : public PlatformTest { 375 class SSLServerSocketTest : public PlatformTest {
300 public: 376 public:
301 SSLServerSocketTest() 377 SSLServerSocketTest()
302 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), 378 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
303 cert_verifier_(new MockCertVerifier()), 379 cert_verifier_(new MockCertVerifier()),
380 client_cert_verifier_(new MockClientCertVerifier()),
304 transport_security_state_(new TransportSecurityState) { 381 transport_security_state_(new TransportSecurityState) {
305 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); 382 cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID);
383 client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID);
306 } 384 }
307 385
308 protected: 386 protected:
309 void Initialize() { 387 void Initialize() {
310 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); 388 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle);
311 client_connection->SetSocket( 389 client_connection->SetSocket(
312 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); 390 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_)));
313 scoped_ptr<StreamSocket> server_socket( 391 scoped_ptr<StreamSocket> server_socket(
314 new FakeSocket(&channel_2_, &channel_1_)); 392 new FakeSocket(&channel_2_, &channel_1_));
315 393
316 base::FilePath certs_dir(GetTestCertsDirectory()); 394 std::string server_cert_der;
317 395 scoped_refptr<X509Certificate> server_cert(
318 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); 396 ReadTestCert("unittest.selfsigned.der", &server_cert_der));
319 std::string cert_der; 397 scoped_ptr<crypto::RSAPrivateKey> server_private_key(
320 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der)); 398 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 399
336 client_ssl_config_.false_start_enabled = false; 400 client_ssl_config_.false_start_enabled = false;
337 client_ssl_config_.channel_id_enabled = false; 401 client_ssl_config_.channel_id_enabled = false;
338 402
339 // Certificate provided by the host doesn't need authority. 403 // Certificate provided by the host doesn't need authority.
340 SSLConfig::CertAndStatus cert_and_status; 404 SSLConfig::CertAndStatus cert_and_status;
341 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; 405 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
342 cert_and_status.der_cert = cert_der; 406 cert_and_status.der_cert = server_cert_der;
343 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); 407 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status);
344 408
345 HostPortPair host_and_pair("unittest", 0); 409 HostPortPair host_and_pair("unittest", 0);
346 SSLClientSocketContext context; 410 SSLClientSocketContext context;
347 context.cert_verifier = cert_verifier_.get(); 411 context.cert_verifier = cert_verifier_.get();
348 context.transport_security_state = transport_security_state_.get(); 412 context.transport_security_state = transport_security_state_.get();
413 socket_factory_->ClearSSLSessionCache();
349 client_socket_ = socket_factory_->CreateSSLClientSocket( 414 client_socket_ = socket_factory_->CreateSSLClientSocket(
350 client_connection.Pass(), host_and_pair, client_ssl_config_, context); 415 client_connection.Pass(), host_and_pair, client_ssl_config_, context);
351 server_socket_ = 416 server_socket_ =
352 CreateSSLServerSocket(server_socket.Pass(), cert.get(), 417 CreateSSLServerSocket(server_socket.Pass(), server_cert.get(),
353 private_key.get(), server_ssl_config_); 418 server_private_key.get(), server_ssl_config_);
419 }
420
421 void ConfigureClientCertsForClient(const char* cert_file_name,
422 const char* private_key_file_name) {
423 scoped_refptr<X509Certificate> cert;
424 scoped_refptr<net::SSLPrivateKey> key;
425 if (cert_file_name && private_key_file_name) {
426 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name);
427 key = new TestSSLPrivateKey(ReadTestKey(private_key_file_name));
428 }
429 client_ssl_config_.send_client_cert = true;
430 client_ssl_config_.client_cert = cert;
431 client_ssl_config_.client_private_key = key;
432 }
433
434 void ConfigureClientCertsForServer(bool cert_expected) {
435 if (!cert_expected)
436 return;
437
438 server_ssl_config_.require_client_cert = true;
439
440 scoped_refptr<X509Certificate> expected_client_ca_cert(
441 ImportCertFromFile(GetTestCertsDirectory(), kClientCertCAFileName));
442 CertificateList ca_list;
443 ca_list.push_back(expected_client_ca_cert);
444 server_ssl_config_.client_cert_ca_list = ca_list;
445 scoped_refptr<X509Certificate> expected_client_cert(
446 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName));
447 client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK);
448
449 server_ssl_config_.client_cert_verifier = client_cert_verifier_.get();
450 }
451
452 X509Certificate* ReadTestCert(const base::StringPiece& name,
453 std::string* cert_der) {
454 base::FilePath certs_dir(GetTestCertsDirectory());
455 base::FilePath cert_path = certs_dir.AppendASCII(name);
456 std::string unused;
457 if (!cert_der)
458 cert_der = &unused;
459 if (!base::ReadFileToString(cert_path, cert_der))
460 return NULL;
461 return X509Certificate::CreateFromBytes(cert_der->data(), cert_der->size());
462 }
463
464 crypto::RSAPrivateKey* ReadTestKey(const base::StringPiece& name) {
465 base::FilePath certs_dir(GetTestCertsDirectory());
466 base::FilePath key_path = certs_dir.AppendASCII(name);
467 std::string key_string;
468 if (!base::ReadFileToString(key_path, &key_string))
469 return NULL;
470 std::vector<uint8_t> key_vector(
471 reinterpret_cast<const uint8_t*>(key_string.data()),
472 reinterpret_cast<const uint8_t*>(key_string.data() +
473 key_string.length()));
474 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector);
354 } 475 }
355 476
356 FakeDataChannel channel_1_; 477 FakeDataChannel channel_1_;
357 FakeDataChannel channel_2_; 478 FakeDataChannel channel_2_;
358 SSLConfig client_ssl_config_; 479 SSLConfig client_ssl_config_;
359 SSLServerConfig server_ssl_config_; 480 SSLServerConfig server_ssl_config_;
360 scoped_ptr<SSLClientSocket> client_socket_; 481 scoped_ptr<SSLClientSocket> client_socket_;
361 scoped_ptr<SSLServerSocket> server_socket_; 482 scoped_ptr<SSLServerSocket> server_socket_;
362 ClientSocketFactory* socket_factory_; 483 ClientSocketFactory* socket_factory_;
363 scoped_ptr<MockCertVerifier> cert_verifier_; 484 scoped_ptr<MockCertVerifier> cert_verifier_;
485 scoped_ptr<MockClientCertVerifier> client_cert_verifier_;
364 scoped_ptr<TransportSecurityState> transport_security_state_; 486 scoped_ptr<TransportSecurityState> transport_security_state_;
487 CertificateList trusted_certs_;
365 }; 488 };
366 489
367 // This test only executes creation of client and server sockets. This is to 490 // 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 491 // test that creation of sockets doesn't crash and have minimal code to run
369 // under valgrind in order to help debugging memory problems. 492 // under valgrind in order to help debugging memory problems.
370 TEST_F(SSLServerSocketTest, Initialize) { 493 TEST_F(SSLServerSocketTest, Initialize) {
371 Initialize(); 494 Initialize();
372 } 495 }
373 496
374 // This test executes Connect() on SSLClientSocket and Handshake() on 497 // This test executes Connect() on SSLClientSocket and Handshake() on
375 // SSLServerSocket to make sure handshaking between the two sockets is 498 // SSLServerSocket to make sure handshaking between the two sockets is
376 // completed successfully. 499 // completed successfully.
377 TEST_F(SSLServerSocketTest, Handshake) { 500 TEST_F(SSLServerSocketTest, Handshake) {
378 Initialize(); 501 Initialize();
379 502
380 TestCompletionCallback connect_callback; 503 TestCompletionCallback connect_callback;
381 TestCompletionCallback handshake_callback; 504 TestCompletionCallback handshake_callback;
382 505
383 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 506 int server_ret = server_socket_->Handshake(handshake_callback.callback());
384 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); 507 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
385 508
386 int client_ret = client_socket_->Connect(connect_callback.callback()); 509 int client_ret = client_socket_->Connect(connect_callback.callback());
387 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); 510 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
388 511
389 if (client_ret == ERR_IO_PENDING) { 512 if (client_ret == ERR_IO_PENDING) {
390 EXPECT_EQ(OK, connect_callback.WaitForResult()); 513 EXPECT_EQ(OK, connect_callback.GetResult(client_ret));
391 } 514 }
392 if (server_ret == ERR_IO_PENDING) { 515 if (server_ret == ERR_IO_PENDING) {
393 EXPECT_EQ(OK, handshake_callback.WaitForResult()); 516 EXPECT_EQ(OK, handshake_callback.GetResult(server_ret));
394 } 517 }
395 518
396 // Make sure the cert status is expected. 519 // Make sure the cert status is expected.
397 SSLInfo ssl_info; 520 SSLInfo ssl_info;
398 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); 521 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info));
399 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); 522 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
400 523
401 // The default cipher suite should be ECDHE and, unless on NSS and the 524 // The default cipher suite should be ECDHE and, unless on NSS and the
402 // platform doesn't support it, an AEAD. 525 // platform doesn't support it, an AEAD.
403 uint16_t cipher_suite = 526 uint16_t cipher_suite =
404 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); 527 SSLConnectionStatusToCipherSuite(ssl_info.connection_status);
405 const char* key_exchange; 528 const char* key_exchange;
406 const char* cipher; 529 const char* cipher;
407 const char* mac; 530 const char* mac;
408 bool is_aead; 531 bool is_aead;
409 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); 532 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
410 EXPECT_STREQ("ECDHE_RSA", key_exchange); 533 EXPECT_STREQ("ECDHE_RSA", key_exchange);
411 EXPECT_TRUE(is_aead); 534 EXPECT_TRUE(is_aead);
412 } 535 }
413 536
537 // NSS ports don't support client certificates
538 #if defined(USE_OPENSSL)
539
540 // This test executes Connect() on SSLClientSocket and Handshake() on
541 // SSLServerSocket to make sure handshaking between the two sockets is
542 // completed successfully, using client certificate.
543 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) {
544 scoped_refptr<X509Certificate> client_cert =
545 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
546 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName);
547 ConfigureClientCertsForServer(true);
548 Initialize();
549
550 TestCompletionCallback connect_callback;
551 TestCompletionCallback handshake_callback;
Ryan Sleevi 2015/12/17 03:47:36 You should declare your variables where they're us
ryanchung 2015/12/18 00:00:56 Done.
552
553 int server_ret = server_socket_->Handshake(handshake_callback.callback());
554 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
Ryan Sleevi 2015/12/17 03:47:36 Should be ASSERT
ryanchung 2015/12/18 00:00:55 Done.
555
556 int client_ret = client_socket_->Connect(connect_callback.callback());
557 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
Ryan Sleevi 2015/12/17 03:47:36 Should be ASSERT
ryanchung 2015/12/18 00:00:56 Done.
558
559 if (client_ret == ERR_IO_PENDING) {
560 EXPECT_EQ(OK, connect_callback.GetResult(client_ret));
561 }
562 if (server_ret == ERR_IO_PENDING) {
563 EXPECT_EQ(OK, handshake_callback.GetResult(server_ret));
564 }
Ryan Sleevi 2015/12/17 03:47:36 1) No braces throughout for client certs 2) Just d
ryanchung 2015/12/18 00:00:55 Done.
565
566 // Make sure the cert status is expected.
567 SSLInfo ssl_info;
568 client_socket_->GetSSLInfo(&ssl_info);
569 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
570 server_socket_->GetSSLInfo(&ssl_info);
571 EXPECT_TRUE(ssl_info.cert.get());
572 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get()));
573 }
574
575 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) {
576 ConfigureClientCertsForServer(true);
577 Initialize();
578 // We use the default setting for the client socket. This causes the client to
579 // get ERR_SSL_CLIENT_AUTH_CERT_NEEDED. This code path allows us to access the
580 // cert_authorities from the CertificateRequest.
Ryan Sleevi 2015/12/17 03:47:36 Don't use pronouns in comments. https://groups.goo
ryanchung 2015/12/18 00:00:55 Done.
581
582 TestCompletionCallback connect_callback;
583 TestCompletionCallback handshake_callback;
Ryan Sleevi 2015/12/17 03:47:36 Same comments throughout the test.
ryanchung 2015/12/18 00:00:55 Done.
584 int server_ret = server_socket_->Handshake(handshake_callback.callback());
585 EXPECT_EQ(server_ret, ERR_IO_PENDING);
Ryan Sleevi 2015/12/17 03:47:36 ASSERT_EQ
ryanchung 2015/12/18 00:00:56 Done.
586
587 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
588 connect_callback.GetResult(
589 client_socket_->Connect(connect_callback.callback())));
590
591 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo();
592 client_socket_->GetSSLCertRequestInfo(request_info.get());
593
594 // Check that the authority name that arrived in the CertificateRequest
595 // handshake message is as expected.
596 scoped_refptr<X509Certificate> client_cert =
597 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
598 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities));
599
600 client_socket_->Disconnect();
601
602 if (server_ret == ERR_IO_PENDING) {
Ryan Sleevi 2015/12/17 03:47:36 This conditional isn't necessary with the above AS
ryanchung 2015/12/18 00:00:56 Done.
603 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret));
604 }
Ryan Sleevi 2015/12/17 03:47:36 No braces
ryanchung 2015/12/18 00:00:56 Done.
605 }
606
607 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) {
608 scoped_refptr<X509Certificate> client_cert =
609 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName);
610 ConfigureClientCertsForClient(kWrongClientCertFileName,
611 kWrongClientPrivateKeyFileName);
612 ConfigureClientCertsForServer(true);
613 Initialize();
614
615 TestCompletionCallback connect_callback;
616 TestCompletionCallback handshake_callback;
Ryan Sleevi 2015/12/17 03:47:36 Same comments about ordering.
ryanchung 2015/12/18 00:00:56 Done.
617
618 int server_ret = server_socket_->Handshake(handshake_callback.callback());
619 EXPECT_EQ(server_ret, ERR_IO_PENDING);
620
621 int client_ret = client_socket_->Connect(connect_callback.callback());
622
623 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT,
624 connect_callback.GetResult(client_ret));
625 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT,
626 handshake_callback.GetResult(server_ret));
627 }
628 #endif // defined(USE_OPENSSL)
629
414 TEST_F(SSLServerSocketTest, DataTransfer) { 630 TEST_F(SSLServerSocketTest, DataTransfer) {
415 Initialize(); 631 Initialize();
416 632
417 TestCompletionCallback connect_callback; 633 TestCompletionCallback connect_callback;
418 TestCompletionCallback handshake_callback; 634 TestCompletionCallback handshake_callback;
419 635
420 // Establish connection. 636 // Establish connection.
421 int client_ret = client_socket_->Connect(connect_callback.callback()); 637 int client_ret = client_socket_->Connect(connect_callback.callback());
422 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); 638 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
423 639
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 int server_ret = server_socket_->Handshake(handshake_callback.callback()); 837 int server_ret = server_socket_->Handshake(handshake_callback.callback());
622 838
623 client_ret = connect_callback.GetResult(client_ret); 839 client_ret = connect_callback.GetResult(client_ret);
624 server_ret = handshake_callback.GetResult(server_ret); 840 server_ret = handshake_callback.GetResult(server_ret);
625 841
626 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); 842 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret);
627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); 843 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret);
628 } 844 }
629 845
630 } // namespace net 846 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698