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

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

Issue 2350483002: Improve error pages on client certificate failures. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/ssl_client_socket.h" 5 #include "net/socket/ssl_client_socket.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <utility> 10 #include <utility>
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 if (result == ERR_IO_PENDING) 281 if (result == ERR_IO_PENDING)
282 return; 282 return;
283 283
284 user_read_buf_ = NULL; 284 user_read_buf_ = NULL;
285 base::ResetAndReturn(&user_read_callback_).Run(result); 285 base::ResetAndReturn(&user_read_callback_).Run(result);
286 } 286 }
287 287
288 // Simulates synchronously receiving an error during Read() or Write() 288 // Simulates synchronously receiving an error during Read() or Write()
289 class SynchronousErrorStreamSocket : public WrappedStreamSocket { 289 class SynchronousErrorStreamSocket : public WrappedStreamSocket {
290 public: 290 public:
291 explicit SynchronousErrorStreamSocket( 291 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport)
292 std::unique_ptr<StreamSocket> transport); 292 : WrappedStreamSocket(std::move(transport)) {}
293 ~SynchronousErrorStreamSocket() override {} 293 ~SynchronousErrorStreamSocket() override {}
294 294
295 // Socket implementation: 295 // Socket implementation:
296 int Read(IOBuffer* buf, 296 int Read(IOBuffer* buf,
297 int buf_len, 297 int buf_len,
298 const CompletionCallback& callback) override; 298 const CompletionCallback& callback) override;
299 int Write(IOBuffer* buf, 299 int Write(IOBuffer* buf,
300 int buf_len, 300 int buf_len,
301 const CompletionCallback& callback) override; 301 const CompletionCallback& callback) override;
302 302
(...skipping 11 matching lines...) Expand all
314 // If there is already a pending asynchronous write, the configured error 314 // If there is already a pending asynchronous write, the configured error
315 // will not be returned until that asynchronous write has completed and 315 // will not be returned until that asynchronous write has completed and
316 // Write() is called again. 316 // Write() is called again.
317 void SetNextWriteError(int error) { 317 void SetNextWriteError(int error) {
318 DCHECK_GE(0, error); 318 DCHECK_GE(0, error);
319 have_write_error_ = true; 319 have_write_error_ = true;
320 pending_write_error_ = error; 320 pending_write_error_ = error;
321 } 321 }
322 322
323 private: 323 private:
324 bool have_read_error_; 324 bool have_read_error_ = false;
325 int pending_read_error_; 325 int pending_read_error_ = OK;
326 326
327 bool have_write_error_; 327 bool have_write_error_ = false;
328 int pending_write_error_; 328 int pending_write_error_ = OK;
329 329
330 DISALLOW_COPY_AND_ASSIGN(SynchronousErrorStreamSocket); 330 DISALLOW_COPY_AND_ASSIGN(SynchronousErrorStreamSocket);
331 }; 331 };
332 332
333 SynchronousErrorStreamSocket::SynchronousErrorStreamSocket(
334 std::unique_ptr<StreamSocket> transport)
335 : WrappedStreamSocket(std::move(transport)),
336 have_read_error_(false),
337 pending_read_error_(OK),
338 have_write_error_(false),
339 pending_write_error_(OK) {}
340
341 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, 333 int SynchronousErrorStreamSocket::Read(IOBuffer* buf,
342 int buf_len, 334 int buf_len,
343 const CompletionCallback& callback) { 335 const CompletionCallback& callback) {
344 if (have_read_error_) 336 if (have_read_error_)
345 return pending_read_error_; 337 return pending_read_error_;
346 return transport_->Read(buf, buf_len, callback); 338 return transport_->Read(buf, buf_len, callback);
347 } 339 }
348 340
349 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, 341 int SynchronousErrorStreamSocket::Write(IOBuffer* buf,
350 int buf_len, 342 int buf_len,
351 const CompletionCallback& callback) { 343 const CompletionCallback& callback) {
352 if (have_write_error_) 344 if (have_write_error_)
353 return pending_write_error_; 345 return pending_write_error_;
354 return transport_->Write(buf, buf_len, callback); 346 return transport_->Write(buf, buf_len, callback);
355 } 347 }
356 348
357 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the 349 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the
358 // underlying transport needing to complete things asynchronously in a 350 // underlying transport needing to complete things asynchronously in a
359 // deterministic manner (e.g.: independent of the TestServer and the OS's 351 // deterministic manner (e.g.: independent of the TestServer and the OS's
360 // semantics). 352 // semantics).
361 class FakeBlockingStreamSocket : public WrappedStreamSocket { 353 class FakeBlockingStreamSocket : public WrappedStreamSocket {
362 public: 354 public:
363 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport); 355 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport)
356 : WrappedStreamSocket(std::move(transport)) {}
364 ~FakeBlockingStreamSocket() override {} 357 ~FakeBlockingStreamSocket() override {}
365 358
366 // Socket implementation: 359 // Socket implementation:
367 int Read(IOBuffer* buf, 360 int Read(IOBuffer* buf,
368 int buf_len, 361 int buf_len,
369 const CompletionCallback& callback) override; 362 const CompletionCallback& callback) override;
370 int Write(IOBuffer* buf, 363 int Write(IOBuffer* buf,
371 int buf_len, 364 int buf_len,
372 const CompletionCallback& callback) override; 365 const CompletionCallback& callback) override;
373 366
374 int pending_read_result() const { return pending_read_result_; } 367 int pending_read_result() const { return pending_read_result_; }
375 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } 368 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); }
376 369
377 // Blocks read results on the socket. Reads will not complete until 370 // Blocks read results on the socket. Reads will not complete until
378 // UnblockReadResult() has been called and a result is ready from the 371 // UnblockReadResult() has been called and a result is ready from the
379 // underlying transport. Note: if BlockReadResult() is called while there is a 372 // underlying transport. Note: if BlockReadResult() is called while there is a
380 // hanging asynchronous Read(), that Read is blocked. 373 // hanging asynchronous Read(), that Read is blocked.
381 void BlockReadResult(); 374 void BlockReadResult();
382 void UnblockReadResult(); 375 void UnblockReadResult();
383 376
377 // Replaces the pending read with |data|. Returns true on success or false if
378 // the caller's reads were too small.
379 bool ReplaceReadResult(const std::string& data);
380
384 // Waits for the blocked Read() call to be complete at the underlying 381 // Waits for the blocked Read() call to be complete at the underlying
385 // transport. 382 // transport.
386 void WaitForReadResult(); 383 void WaitForReadResult();
387 384
388 // Causes the next call to Write() to return ERR_IO_PENDING, not beginning the 385 // Causes the next call to Write() to return ERR_IO_PENDING, not beginning the
389 // underlying transport until UnblockWrite() has been called. Note: if there 386 // underlying transport until UnblockWrite() has been called. Note: if there
390 // is a pending asynchronous write, it is NOT blocked. For purposes of 387 // is a pending asynchronous write, it is NOT blocked. For purposes of
391 // blocking writes, data is considered to have reached the underlying 388 // blocking writes, data is considered to have reached the underlying
392 // transport as soon as Write() is called. 389 // transport as soon as Write() is called.
393 void BlockWrite(); 390 void BlockWrite();
394 void UnblockWrite(); 391 void UnblockWrite();
395 392
396 // Waits for the blocked Write() call to be scheduled. 393 // Waits for the blocked Write() call to be scheduled.
397 void WaitForWrite(); 394 void WaitForWrite();
398 395
399 private: 396 private:
400 // Handles completion from the underlying transport read. 397 // Handles completion from the underlying transport read.
401 void OnReadCompleted(int result); 398 void OnReadCompleted(int result);
402 399
400 // Finishes the current read.
401 void ReturnReadResult();
402
403 // True if read callbacks are blocked. 403 // True if read callbacks are blocked.
404 bool should_block_read_; 404 bool should_block_read_ = false;
405 405
406 // The buffer for the pending read, or NULL if not consumed. 406 // The buffer for the pending read, or NULL if not consumed.
407 scoped_refptr<IOBuffer> pending_read_buf_; 407 scoped_refptr<IOBuffer> pending_read_buf_;
408 408
409 // The size of the pending read buffer, or -1 if not set.
410 int pending_read_buf_len_ = -1;
411
409 // The user callback for the pending read call. 412 // The user callback for the pending read call.
410 CompletionCallback pending_read_callback_; 413 CompletionCallback pending_read_callback_;
411 414
412 // The result for the blocked read callback, or ERR_IO_PENDING if not 415 // The result for the blocked read callback, or ERR_IO_PENDING if not
413 // completed. 416 // completed.
414 int pending_read_result_; 417 int pending_read_result_ = ERR_IO_PENDING;
415 418
416 // WaitForReadResult() wait loop. 419 // WaitForReadResult() wait loop.
417 std::unique_ptr<base::RunLoop> read_loop_; 420 std::unique_ptr<base::RunLoop> read_loop_;
418 421
419 // True if write calls are blocked. 422 // True if write calls are blocked.
420 bool should_block_write_; 423 bool should_block_write_ = false;
421 424
422 // The buffer for the pending write, or NULL if not scheduled. 425 // The buffer for the pending write, or NULL if not scheduled.
423 scoped_refptr<IOBuffer> pending_write_buf_; 426 scoped_refptr<IOBuffer> pending_write_buf_;
424 427
425 // The callback for the pending write call. 428 // The callback for the pending write call.
426 CompletionCallback pending_write_callback_; 429 CompletionCallback pending_write_callback_;
427 430
428 // The length for the pending write, or -1 if not scheduled. 431 // The length for the pending write, or -1 if not scheduled.
429 int pending_write_len_; 432 int pending_write_len_ = -1;
430 433
431 // WaitForWrite() wait loop. 434 // WaitForWrite() wait loop.
432 std::unique_ptr<base::RunLoop> write_loop_; 435 std::unique_ptr<base::RunLoop> write_loop_;
433 }; 436 };
434 437
435 FakeBlockingStreamSocket::FakeBlockingStreamSocket(
436 std::unique_ptr<StreamSocket> transport)
437 : WrappedStreamSocket(std::move(transport)),
438 should_block_read_(false),
439 pending_read_result_(ERR_IO_PENDING),
440 should_block_write_(false),
441 pending_write_len_(-1) {}
442
443 int FakeBlockingStreamSocket::Read(IOBuffer* buf, 438 int FakeBlockingStreamSocket::Read(IOBuffer* buf,
444 int len, 439 int len,
445 const CompletionCallback& callback) { 440 const CompletionCallback& callback) {
446 DCHECK(!pending_read_buf_); 441 DCHECK(!pending_read_buf_);
447 DCHECK(pending_read_callback_.is_null()); 442 DCHECK(pending_read_callback_.is_null());
448 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); 443 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_);
449 DCHECK(!callback.is_null()); 444 DCHECK(!callback.is_null());
450 445
451 int rv = transport_->Read(buf, len, base::Bind( 446 int rv = transport_->Read(buf, len, base::Bind(
452 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); 447 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this)));
453 if (rv == ERR_IO_PENDING) { 448 if (rv == ERR_IO_PENDING || should_block_read_) {
454 // Save the callback to be called later. 449 // Save the callback to be called later.
455 pending_read_buf_ = buf; 450 pending_read_buf_ = buf;
451 pending_read_buf_len_ = len;
456 pending_read_callback_ = callback; 452 pending_read_callback_ = callback;
457 } else if (should_block_read_) { 453 // Save the read result.
458 // Save the callback and read result to be called later. 454 if (rv != ERR_IO_PENDING) {
459 pending_read_buf_ = buf; 455 OnReadCompleted(rv);
460 pending_read_callback_ = callback; 456 rv = ERR_IO_PENDING;
461 OnReadCompleted(rv); 457 }
462 rv = ERR_IO_PENDING;
463 } 458 }
464 return rv; 459 return rv;
465 } 460 }
466 461
467 int FakeBlockingStreamSocket::Write(IOBuffer* buf, 462 int FakeBlockingStreamSocket::Write(IOBuffer* buf,
468 int len, 463 int len,
469 const CompletionCallback& callback) { 464 const CompletionCallback& callback) {
470 DCHECK(buf); 465 DCHECK(buf);
471 DCHECK_LE(0, len); 466 DCHECK_LE(0, len);
472 467
(...skipping 17 matching lines...) Expand all
490 485
491 void FakeBlockingStreamSocket::BlockReadResult() { 486 void FakeBlockingStreamSocket::BlockReadResult() {
492 DCHECK(!should_block_read_); 487 DCHECK(!should_block_read_);
493 should_block_read_ = true; 488 should_block_read_ = true;
494 } 489 }
495 490
496 void FakeBlockingStreamSocket::UnblockReadResult() { 491 void FakeBlockingStreamSocket::UnblockReadResult() {
497 DCHECK(should_block_read_); 492 DCHECK(should_block_read_);
498 should_block_read_ = false; 493 should_block_read_ = false;
499 494
500 // If the operation is still pending in the underlying transport, immediately 495 // If the operation has since completed, return the result to the caller.
501 // return - OnReadCompleted() will handle invoking the callback once the 496 if (pending_read_result_ != ERR_IO_PENDING)
502 // transport has completed. 497 ReturnReadResult();
503 if (pending_read_result_ == ERR_IO_PENDING) 498 }
504 return; 499
505 int result = pending_read_result_; 500 bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) {
506 pending_read_buf_ = nullptr; 501 DCHECK(should_block_read_);
507 pending_read_result_ = ERR_IO_PENDING; 502 DCHECK_NE(ERR_IO_PENDING, pending_read_result_);
508 base::ResetAndReturn(&pending_read_callback_).Run(result); 503 DCHECK(pending_read_buf_);
504 DCHECK_NE(-1, pending_read_buf_len_);
505
506 if (static_cast<size_t>(pending_read_buf_len_) < data.size())
507 return false;
508
509 memcpy(pending_read_buf_->data(), data.data(), data.size());
510 pending_read_result_ = data.size();
511 return true;
509 } 512 }
510 513
511 void FakeBlockingStreamSocket::WaitForReadResult() { 514 void FakeBlockingStreamSocket::WaitForReadResult() {
512 DCHECK(should_block_read_); 515 DCHECK(should_block_read_);
513 DCHECK(!read_loop_); 516 DCHECK(!read_loop_);
514 517
515 if (pending_read_result_ != ERR_IO_PENDING) 518 if (pending_read_result_ != ERR_IO_PENDING)
516 return; 519 return;
517 read_loop_.reset(new base::RunLoop); 520 read_loop_.reset(new base::RunLoop);
518 read_loop_->Run(); 521 read_loop_->Run();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 write_loop_.reset(new base::RunLoop); 557 write_loop_.reset(new base::RunLoop);
555 write_loop_->Run(); 558 write_loop_->Run();
556 write_loop_.reset(); 559 write_loop_.reset();
557 DCHECK(pending_write_buf_.get()); 560 DCHECK(pending_write_buf_.get());
558 } 561 }
559 562
560 void FakeBlockingStreamSocket::OnReadCompleted(int result) { 563 void FakeBlockingStreamSocket::OnReadCompleted(int result) {
561 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); 564 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_);
562 DCHECK(!pending_read_callback_.is_null()); 565 DCHECK(!pending_read_callback_.is_null());
563 566
567 pending_read_result_ = result;
568
564 if (should_block_read_) { 569 if (should_block_read_) {
565 // Store the result so that the callback can be invoked once Unblock() is 570 // Defer the result until UnblockReadResult is called.
566 // called.
567 pending_read_result_ = result;
568
569 // Stop the WaitForReadResult() call if any.
570 if (read_loop_) 571 if (read_loop_)
571 read_loop_->Quit(); 572 read_loop_->Quit();
572 } else { 573 return;
573 // Either the Read() was never blocked or UnblockReadResult() was called
574 // before the Read() completed. Either way, return the result to the caller.
575 pending_read_buf_ = nullptr;
576 base::ResetAndReturn(&pending_read_callback_).Run(result);
577 } 574 }
575
576 ReturnReadResult();
577 }
578
579 void FakeBlockingStreamSocket::ReturnReadResult() {
580 int result = pending_read_result_;
581 pending_read_result_ = ERR_IO_PENDING;
582 pending_read_buf_ = nullptr;
583 pending_read_buf_len_ = -1;
584 base::ResetAndReturn(&pending_read_callback_).Run(result);
578 } 585 }
579 586
580 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of 587 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of
581 // reads and writes on the socket. 588 // reads and writes on the socket.
582 class CountingStreamSocket : public WrappedStreamSocket { 589 class CountingStreamSocket : public WrappedStreamSocket {
583 public: 590 public:
584 explicit CountingStreamSocket(std::unique_ptr<StreamSocket> transport) 591 explicit CountingStreamSocket(std::unique_ptr<StreamSocket> transport)
585 : WrappedStreamSocket(std::move(transport)), 592 : WrappedStreamSocket(std::move(transport)),
586 read_count_(0), 593 read_count_(0),
587 write_count_(0) {} 594 write_count_(0) {}
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 void EnableAsyncFailingChannelID() { 1020 void EnableAsyncFailingChannelID() {
1014 channel_id_service_.reset(new ChannelIDService( 1021 channel_id_service_.reset(new ChannelIDService(
1015 new AsyncFailingChannelIDStore(), base::ThreadTaskRunnerHandle::Get())); 1022 new AsyncFailingChannelIDStore(), base::ThreadTaskRunnerHandle::Get()));
1016 context_.channel_id_service = channel_id_service_.get(); 1023 context_.channel_id_service = channel_id_service_.get();
1017 } 1024 }
1018 1025
1019 private: 1026 private:
1020 std::unique_ptr<ChannelIDService> channel_id_service_; 1027 std::unique_ptr<ChannelIDService> channel_id_service_;
1021 }; 1028 };
1022 1029
1030 // Returns a serialized unencrypted TLS 1.2 alert record for the given alert
1031 // value.
1032 std::string FormatTLS12Alert(uint8_t alert) {
1033 std::string ret;
1034 // ContentType.alert
1035 ret.push_back(21);
1036 // Record-layer version. Assume TLS 1.2.
1037 ret.push_back(0x03);
1038 ret.push_back(0x03);
1039 // Record length.
1040 ret.push_back(0);
1041 ret.push_back(2);
1042 // AlertLevel.fatal.
1043 ret.push_back(2);
1044 // The alert itself.
1045 ret.push_back(alert);
1046 return ret;
1047 }
1048
1023 } // namespace 1049 } // namespace
1024 1050
1025 TEST_F(SSLClientSocketTest, Connect) { 1051 TEST_F(SSLClientSocketTest, Connect) {
1026 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1052 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1027 1053
1028 TestCompletionCallback callback; 1054 TestCompletionCallback callback;
1029 TestNetLog log; 1055 TestNetLog log;
1030 std::unique_ptr<StreamSocket> transport( 1056 std::unique_ptr<StreamSocket> transport(
1031 new TCPClientSocket(addr(), NULL, &log, NetLog::Source())); 1057 new TCPClientSocket(addr(), NULL, &log, NetLog::Source()));
1032 int rv = callback.GetResult(transport->Connect(callback.callback())); 1058 int rv = callback.GetResult(transport->Connect(callback.callback()));
(...skipping 2239 matching lines...) Expand 10 before | Expand all | Expand 10 after
3272 ssl_options.request_client_certificate = true; 3298 ssl_options.request_client_certificate = true;
3273 ssl_options.client_authorities.push_back( 3299 ssl_options.client_authorities.push_back(
3274 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); 3300 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem"));
3275 3301
3276 ASSERT_TRUE(StartTestServer(ssl_options)); 3302 ASSERT_TRUE(StartTestServer(ssl_options));
3277 3303
3278 base::FilePath certs_dir = GetTestCertsDirectory(); 3304 base::FilePath certs_dir = GetTestCertsDirectory();
3279 SSLConfig ssl_config; 3305 SSLConfig ssl_config;
3280 ssl_config.send_client_cert = true; 3306 ssl_config.send_client_cert = true;
3281 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); 3307 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem");
3282
3283 // This is required to ensure that signing works with the client
3284 // certificate's private key.
3285 ssl_config.client_private_key = 3308 ssl_config.client_private_key =
3286 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key")); 3309 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"));
3287 3310
3288 int rv; 3311 int rv;
3289 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); 3312 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
3290 3313
3291 EXPECT_THAT(rv, IsOk()); 3314 EXPECT_THAT(rv, IsOk());
3292 EXPECT_TRUE(sock_->IsConnected()); 3315 EXPECT_TRUE(sock_->IsConnected());
3293 3316
3294 SSLInfo ssl_info; 3317 SSLInfo ssl_info;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
3465 SSLInfo ssl_info; 3488 SSLInfo ssl_info;
3466 ASSERT_TRUE(sock_->GetSSLInfo(&ssl_info)); 3489 ASSERT_TRUE(sock_->GetSSLInfo(&ssl_info));
3467 3490
3468 EXPECT_THAT(rv, IsError(ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN)); 3491 EXPECT_THAT(rv, IsError(ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN));
3469 EXPECT_TRUE(ssl_info.cert_status & CERT_STATUS_PINNED_KEY_MISSING); 3492 EXPECT_TRUE(ssl_info.cert_status & CERT_STATUS_PINNED_KEY_MISSING);
3470 EXPECT_TRUE(ssl_info.cert_status & 3493 EXPECT_TRUE(ssl_info.cert_status &
3471 CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED); 3494 CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED);
3472 EXPECT_TRUE(sock_->IsConnected()); 3495 EXPECT_TRUE(sock_->IsConnected());
3473 } 3496 }
3474 3497
3498 // Test that handshake_failure alerts at the ServerHello are mapped to
3499 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
3500 TEST_F(SSLClientSocketTest, HandshakeFailureServerHello) {
3501 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
3502
3503 TestCompletionCallback callback;
3504 std::unique_ptr<StreamSocket> real_transport(
3505 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source()));
3506 std::unique_ptr<FakeBlockingStreamSocket> transport(
3507 new FakeBlockingStreamSocket(std::move(real_transport)));
3508 FakeBlockingStreamSocket* raw_transport = transport.get();
3509 int rv = callback.GetResult(transport->Connect(callback.callback()));
3510 ASSERT_THAT(rv, IsOk());
3511
3512 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
3513 std::move(transport), spawned_test_server()->host_port_pair(),
3514 SSLConfig()));
3515
3516 // Connect. Stop before the client processes ServerHello.
3517 raw_transport->BlockReadResult();
3518 rv = sock->Connect(callback.callback());
3519 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
3520 raw_transport->WaitForReadResult();
3521
3522 // Replace it with an alert.
3523 raw_transport->ReplaceReadResult(
3524 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */));
3525 raw_transport->UnblockReadResult();
3526
3527 rv = callback.GetResult(rv);
3528 EXPECT_THAT(rv, IsError(ERR_SSL_VERSION_OR_CIPHER_MISMATCH));
3529 }
3530
3531 // Test that handshake_failure alerts after the ServerHello but without a
3532 // CertificateRequest are mapped to ERR_SSL_PROTOCOL_ERROR.
3533 TEST_F(SSLClientSocketTest, HandshakeFailureNoClientCerts) {
3534 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
3535
3536 TestCompletionCallback callback;
3537 std::unique_ptr<StreamSocket> real_transport(
3538 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source()));
3539 std::unique_ptr<FakeBlockingStreamSocket> transport(
3540 new FakeBlockingStreamSocket(std::move(real_transport)));
3541 FakeBlockingStreamSocket* raw_transport = transport.get();
3542 int rv = callback.GetResult(transport->Connect(callback.callback()));
3543 ASSERT_THAT(rv, IsOk());
3544
3545 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
3546 std::move(transport), spawned_test_server()->host_port_pair(),
3547 SSLConfig()));
3548
3549 // Connect. Stop before the client processes ServerHello.
3550 raw_transport->BlockReadResult();
3551 rv = sock->Connect(callback.callback());
3552 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
3553 raw_transport->WaitForReadResult();
3554
3555 // Release the ServerHello and wait for the client to write its second flight.
3556 raw_transport->BlockWrite();
3557 raw_transport->UnblockReadResult();
3558 raw_transport->WaitForWrite();
3559
3560 // Wait for the server's final flight.
3561 raw_transport->BlockReadResult();
3562 raw_transport->UnblockWrite();
3563 raw_transport->WaitForReadResult();
3564
3565 // Replace it with an alert.
3566 raw_transport->ReplaceReadResult(
3567 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */));
3568 raw_transport->UnblockReadResult();
3569
3570 rv = callback.GetResult(rv);
3571 EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR));
3572 }
3573
3574 // Test that handshake_failure alerts after the ServerHello map to
3575 // ERR_BAD_SSL_CLIENT_AUTH_CERT if a client certificate was requested but not
3576 // supplied. TLS does not have an alert for this case, so handshake_failure is
3577 // common. See https://crbug.com/646567.
3578 TEST_F(SSLClientSocketTest, LateHandshakeFailureMissingClientCerts) {
3579 // Request a client certificate.
3580 SpawnedTestServer::SSLOptions ssl_options;
3581 ssl_options.request_client_certificate = true;
3582 ASSERT_TRUE(StartTestServer(ssl_options));
3583
3584 TestCompletionCallback callback;
3585 std::unique_ptr<StreamSocket> real_transport(
3586 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source()));
3587 std::unique_ptr<FakeBlockingStreamSocket> transport(
3588 new FakeBlockingStreamSocket(std::move(real_transport)));
3589 FakeBlockingStreamSocket* raw_transport = transport.get();
3590 int rv = callback.GetResult(transport->Connect(callback.callback()));
3591 ASSERT_THAT(rv, IsOk());
3592
3593 // Send no client certificate.
3594 SSLConfig config;
3595 config.send_client_cert = true;
3596 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
3597 std::move(transport), spawned_test_server()->host_port_pair(), config));
3598
3599 // Connect. Stop before the client processes ServerHello.
3600 raw_transport->BlockReadResult();
3601 rv = sock->Connect(callback.callback());
3602 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
3603 raw_transport->WaitForReadResult();
3604
3605 // Release the ServerHello and wait for the client to write its second flight.
3606 raw_transport->BlockWrite();
3607 raw_transport->UnblockReadResult();
3608 raw_transport->WaitForWrite();
3609
3610 // Wait for the server's final flight.
3611 raw_transport->BlockReadResult();
3612 raw_transport->UnblockWrite();
3613 raw_transport->WaitForReadResult();
3614
3615 // Replace it with an alert.
3616 raw_transport->ReplaceReadResult(
3617 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */));
3618 raw_transport->UnblockReadResult();
3619
3620 rv = callback.GetResult(rv);
3621 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT));
3622 }
3623
3624 // Test that handshake_failure alerts after the ServerHello map to
3625 // ERR_SSL_PROTOCOL_ERROR if received after sending a client certificate. It is
3626 // assumed servers will send a more appropriate alert in this case.
3627 TEST_F(SSLClientSocketTest, LateHandshakeFailureSendClientCerts) {
3628 // Request a client certificate.
3629 SpawnedTestServer::SSLOptions ssl_options;
3630 ssl_options.request_client_certificate = true;
3631 ASSERT_TRUE(StartTestServer(ssl_options));
3632
3633 TestCompletionCallback callback;
3634 std::unique_ptr<StreamSocket> real_transport(
3635 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source()));
3636 std::unique_ptr<FakeBlockingStreamSocket> transport(
3637 new FakeBlockingStreamSocket(std::move(real_transport)));
3638 FakeBlockingStreamSocket* raw_transport = transport.get();
3639 int rv = callback.GetResult(transport->Connect(callback.callback()));
3640 ASSERT_THAT(rv, IsOk());
3641
3642 // Send a client certificate.
3643 base::FilePath certs_dir = GetTestCertsDirectory();
3644 SSLConfig config;
3645 config.send_client_cert = true;
3646 config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem");
3647 config.client_private_key =
3648 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"));
3649 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
3650 std::move(transport), spawned_test_server()->host_port_pair(), config));
3651
3652 // Connect. Stop before the client processes ServerHello.
3653 raw_transport->BlockReadResult();
3654 rv = sock->Connect(callback.callback());
3655 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
3656 raw_transport->WaitForReadResult();
3657
3658 // Release the ServerHello and wait for the client to write its second flight.
3659 raw_transport->BlockWrite();
3660 raw_transport->UnblockReadResult();
3661 raw_transport->WaitForWrite();
3662
3663 // Wait for the server's final flight.
3664 raw_transport->BlockReadResult();
3665 raw_transport->UnblockWrite();
3666 raw_transport->WaitForReadResult();
3667
3668 // Replace it with an alert.
3669 raw_transport->ReplaceReadResult(
3670 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */));
3671 raw_transport->UnblockReadResult();
3672
3673 rv = callback.GetResult(rv);
3674 EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR));
3675 }
3676
3677 // Test that access_denied alerts are mapped to ERR_SSL_PROTOCOL_ERROR if
3678 // received on a connection not requesting client certificates. This is an
3679 // incorrect use of the alert but is common. See https://crbug.com/630883.
3680 TEST_F(SSLClientSocketTest, AccessDeniedNoClientCerts) {
3681 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
3682
3683 TestCompletionCallback callback;
3684 std::unique_ptr<StreamSocket> real_transport(
3685 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source()));
3686 std::unique_ptr<FakeBlockingStreamSocket> transport(
3687 new FakeBlockingStreamSocket(std::move(real_transport)));
3688 FakeBlockingStreamSocket* raw_transport = transport.get();
3689 int rv = callback.GetResult(transport->Connect(callback.callback()));
3690 ASSERT_THAT(rv, IsOk());
3691
3692 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
3693 std::move(transport), spawned_test_server()->host_port_pair(),
3694 SSLConfig()));
3695
3696 // Connect. Stop before the client processes ServerHello.
3697 raw_transport->BlockReadResult();
3698 rv = sock->Connect(callback.callback());
3699 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
3700 raw_transport->WaitForReadResult();
3701
3702 // Release the ServerHello and wait for the client to write its second flight.
3703 raw_transport->BlockWrite();
3704 raw_transport->UnblockReadResult();
3705 raw_transport->WaitForWrite();
3706
3707 // Wait for the server's final flight.
3708 raw_transport->BlockReadResult();
3709 raw_transport->UnblockWrite();
3710 raw_transport->WaitForReadResult();
3711
3712 // Replace it with an alert.
3713 raw_transport->ReplaceReadResult(
3714 FormatTLS12Alert(49 /* AlertDescription.access_denied */));
3715 raw_transport->UnblockReadResult();
3716
3717 rv = callback.GetResult(rv);
3718 EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR));
3719 }
3720
3721 // Test that access_denied alerts are mapped to ERR_BAD_SSL_CLIENT_AUTH_CERT if
3722 // received on a connection requesting client certificates.
3723 TEST_F(SSLClientSocketTest, AccessDeniedClientCerts) {
3724 // Request a client certificate.
3725 SpawnedTestServer::SSLOptions ssl_options;
3726 ssl_options.request_client_certificate = true;
3727 ASSERT_TRUE(StartTestServer(ssl_options));
3728
3729 TestCompletionCallback callback;
3730 std::unique_ptr<StreamSocket> real_transport(
3731 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source()));
3732 std::unique_ptr<FakeBlockingStreamSocket> transport(
3733 new FakeBlockingStreamSocket(std::move(real_transport)));
3734 FakeBlockingStreamSocket* raw_transport = transport.get();
3735 int rv = callback.GetResult(transport->Connect(callback.callback()));
3736 ASSERT_THAT(rv, IsOk());
3737
3738 // Send a client certificate.
3739 base::FilePath certs_dir = GetTestCertsDirectory();
3740 SSLConfig config;
3741 config.send_client_cert = true;
3742 config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem");
3743 config.client_private_key =
3744 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"));
3745 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
3746 std::move(transport), spawned_test_server()->host_port_pair(), config));
3747
3748 // Connect. Stop before the client processes ServerHello.
3749 raw_transport->BlockReadResult();
3750 rv = sock->Connect(callback.callback());
3751 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
3752 raw_transport->WaitForReadResult();
3753
3754 // Release the ServerHello and wait for the client to write its second flight.
3755 raw_transport->BlockWrite();
3756 raw_transport->UnblockReadResult();
3757 raw_transport->WaitForWrite();
3758
3759 // Wait for the server's final flight.
3760 raw_transport->BlockReadResult();
3761 raw_transport->UnblockWrite();
3762 raw_transport->WaitForReadResult();
3763
3764 // Replace it with an alert.
3765 raw_transport->ReplaceReadResult(
3766 FormatTLS12Alert(49 /* AlertDescription.access_denied */));
3767 raw_transport->UnblockReadResult();
3768
3769 rv = callback.GetResult(rv);
3770 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT));
3771 }
3772
3475 } // namespace net 3773 } // namespace net
OLDNEW
« components/error_page_strings.grdp ('K') | « net/socket/ssl_client_socket_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698