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 #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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 if (result == ERR_IO_PENDING) | 283 if (result == ERR_IO_PENDING) |
284 return; | 284 return; |
285 | 285 |
286 user_read_buf_ = NULL; | 286 user_read_buf_ = NULL; |
287 base::ResetAndReturn(&user_read_callback_).Run(result); | 287 base::ResetAndReturn(&user_read_callback_).Run(result); |
288 } | 288 } |
289 | 289 |
290 // Simulates synchronously receiving an error during Read() or Write() | 290 // Simulates synchronously receiving an error during Read() or Write() |
291 class SynchronousErrorStreamSocket : public WrappedStreamSocket { | 291 class SynchronousErrorStreamSocket : public WrappedStreamSocket { |
292 public: | 292 public: |
293 explicit SynchronousErrorStreamSocket( | 293 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport) |
294 std::unique_ptr<StreamSocket> transport); | 294 : WrappedStreamSocket(std::move(transport)) {} |
295 ~SynchronousErrorStreamSocket() override {} | 295 ~SynchronousErrorStreamSocket() override {} |
296 | 296 |
297 // Socket implementation: | 297 // Socket implementation: |
298 int Read(IOBuffer* buf, | 298 int Read(IOBuffer* buf, |
299 int buf_len, | 299 int buf_len, |
300 const CompletionCallback& callback) override; | 300 const CompletionCallback& callback) override; |
301 int Write(IOBuffer* buf, | 301 int Write(IOBuffer* buf, |
302 int buf_len, | 302 int buf_len, |
303 const CompletionCallback& callback) override; | 303 const CompletionCallback& callback) override; |
304 | 304 |
(...skipping 11 matching lines...) Expand all Loading... |
316 // If there is already a pending asynchronous write, the configured error | 316 // If there is already a pending asynchronous write, the configured error |
317 // will not be returned until that asynchronous write has completed and | 317 // will not be returned until that asynchronous write has completed and |
318 // Write() is called again. | 318 // Write() is called again. |
319 void SetNextWriteError(int error) { | 319 void SetNextWriteError(int error) { |
320 DCHECK_GE(0, error); | 320 DCHECK_GE(0, error); |
321 have_write_error_ = true; | 321 have_write_error_ = true; |
322 pending_write_error_ = error; | 322 pending_write_error_ = error; |
323 } | 323 } |
324 | 324 |
325 private: | 325 private: |
326 bool have_read_error_; | 326 bool have_read_error_ = false; |
327 int pending_read_error_; | 327 int pending_read_error_ = OK; |
328 | 328 |
329 bool have_write_error_; | 329 bool have_write_error_ = false; |
330 int pending_write_error_; | 330 int pending_write_error_ = OK; |
331 | 331 |
332 DISALLOW_COPY_AND_ASSIGN(SynchronousErrorStreamSocket); | 332 DISALLOW_COPY_AND_ASSIGN(SynchronousErrorStreamSocket); |
333 }; | 333 }; |
334 | 334 |
335 SynchronousErrorStreamSocket::SynchronousErrorStreamSocket( | |
336 std::unique_ptr<StreamSocket> transport) | |
337 : WrappedStreamSocket(std::move(transport)), | |
338 have_read_error_(false), | |
339 pending_read_error_(OK), | |
340 have_write_error_(false), | |
341 pending_write_error_(OK) {} | |
342 | |
343 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, | 335 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, |
344 int buf_len, | 336 int buf_len, |
345 const CompletionCallback& callback) { | 337 const CompletionCallback& callback) { |
346 if (have_read_error_) | 338 if (have_read_error_) |
347 return pending_read_error_; | 339 return pending_read_error_; |
348 return transport_->Read(buf, buf_len, callback); | 340 return transport_->Read(buf, buf_len, callback); |
349 } | 341 } |
350 | 342 |
351 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, | 343 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, |
352 int buf_len, | 344 int buf_len, |
353 const CompletionCallback& callback) { | 345 const CompletionCallback& callback) { |
354 if (have_write_error_) | 346 if (have_write_error_) |
355 return pending_write_error_; | 347 return pending_write_error_; |
356 return transport_->Write(buf, buf_len, callback); | 348 return transport_->Write(buf, buf_len, callback); |
357 } | 349 } |
358 | 350 |
359 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the | 351 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the |
360 // underlying transport needing to complete things asynchronously in a | 352 // underlying transport needing to complete things asynchronously in a |
361 // deterministic manner (e.g.: independent of the TestServer and the OS's | 353 // deterministic manner (e.g.: independent of the TestServer and the OS's |
362 // semantics). | 354 // semantics). |
363 class FakeBlockingStreamSocket : public WrappedStreamSocket { | 355 class FakeBlockingStreamSocket : public WrappedStreamSocket { |
364 public: | 356 public: |
365 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport); | 357 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport) |
| 358 : WrappedStreamSocket(std::move(transport)) {} |
366 ~FakeBlockingStreamSocket() override {} | 359 ~FakeBlockingStreamSocket() override {} |
367 | 360 |
368 // Socket implementation: | 361 // Socket implementation: |
369 int Read(IOBuffer* buf, | 362 int Read(IOBuffer* buf, |
370 int buf_len, | 363 int buf_len, |
371 const CompletionCallback& callback) override; | 364 const CompletionCallback& callback) override; |
372 int Write(IOBuffer* buf, | 365 int Write(IOBuffer* buf, |
373 int buf_len, | 366 int buf_len, |
374 const CompletionCallback& callback) override; | 367 const CompletionCallback& callback) override; |
375 | 368 |
376 int pending_read_result() const { return pending_read_result_; } | 369 int pending_read_result() const { return pending_read_result_; } |
377 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } | 370 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } |
378 | 371 |
379 // Blocks read results on the socket. Reads will not complete until | 372 // Blocks read results on the socket. Reads will not complete until |
380 // UnblockReadResult() has been called and a result is ready from the | 373 // UnblockReadResult() has been called and a result is ready from the |
381 // underlying transport. Note: if BlockReadResult() is called while there is a | 374 // underlying transport. Note: if BlockReadResult() is called while there is a |
382 // hanging asynchronous Read(), that Read is blocked. | 375 // hanging asynchronous Read(), that Read is blocked. |
383 void BlockReadResult(); | 376 void BlockReadResult(); |
384 void UnblockReadResult(); | 377 void UnblockReadResult(); |
385 | 378 |
| 379 // Replaces the pending read with |data|. Returns true on success or false if |
| 380 // the caller's reads were too small. |
| 381 bool ReplaceReadResult(const std::string& data); |
| 382 |
386 // Waits for the blocked Read() call to be complete at the underlying | 383 // Waits for the blocked Read() call to be complete at the underlying |
387 // transport. | 384 // transport. |
388 void WaitForReadResult(); | 385 void WaitForReadResult(); |
389 | 386 |
390 // Causes the next call to Write() to return ERR_IO_PENDING, not beginning the | 387 // Causes the next call to Write() to return ERR_IO_PENDING, not beginning the |
391 // underlying transport until UnblockWrite() has been called. Note: if there | 388 // underlying transport until UnblockWrite() has been called. Note: if there |
392 // is a pending asynchronous write, it is NOT blocked. For purposes of | 389 // is a pending asynchronous write, it is NOT blocked. For purposes of |
393 // blocking writes, data is considered to have reached the underlying | 390 // blocking writes, data is considered to have reached the underlying |
394 // transport as soon as Write() is called. | 391 // transport as soon as Write() is called. |
395 void BlockWrite(); | 392 void BlockWrite(); |
396 void UnblockWrite(); | 393 void UnblockWrite(); |
397 | 394 |
398 // Waits for the blocked Write() call to be scheduled. | 395 // Waits for the blocked Write() call to be scheduled. |
399 void WaitForWrite(); | 396 void WaitForWrite(); |
400 | 397 |
401 private: | 398 private: |
402 // Handles completion from the underlying transport read. | 399 // Handles completion from the underlying transport read. |
403 void OnReadCompleted(int result); | 400 void OnReadCompleted(int result); |
404 | 401 |
| 402 // Finishes the current read. |
| 403 void ReturnReadResult(); |
| 404 |
405 // True if read callbacks are blocked. | 405 // True if read callbacks are blocked. |
406 bool should_block_read_; | 406 bool should_block_read_ = false; |
407 | 407 |
408 // The buffer for the pending read, or NULL if not consumed. | 408 // The buffer for the pending read, or NULL if not consumed. |
409 scoped_refptr<IOBuffer> pending_read_buf_; | 409 scoped_refptr<IOBuffer> pending_read_buf_; |
410 | 410 |
| 411 // The size of the pending read buffer, or -1 if not set. |
| 412 int pending_read_buf_len_ = -1; |
| 413 |
411 // The user callback for the pending read call. | 414 // The user callback for the pending read call. |
412 CompletionCallback pending_read_callback_; | 415 CompletionCallback pending_read_callback_; |
413 | 416 |
414 // The result for the blocked read callback, or ERR_IO_PENDING if not | 417 // The result for the blocked read callback, or ERR_IO_PENDING if not |
415 // completed. | 418 // completed. |
416 int pending_read_result_; | 419 int pending_read_result_ = ERR_IO_PENDING; |
417 | 420 |
418 // WaitForReadResult() wait loop. | 421 // WaitForReadResult() wait loop. |
419 std::unique_ptr<base::RunLoop> read_loop_; | 422 std::unique_ptr<base::RunLoop> read_loop_; |
420 | 423 |
421 // True if write calls are blocked. | 424 // True if write calls are blocked. |
422 bool should_block_write_; | 425 bool should_block_write_ = false; |
423 | 426 |
424 // The buffer for the pending write, or NULL if not scheduled. | 427 // The buffer for the pending write, or NULL if not scheduled. |
425 scoped_refptr<IOBuffer> pending_write_buf_; | 428 scoped_refptr<IOBuffer> pending_write_buf_; |
426 | 429 |
427 // The callback for the pending write call. | 430 // The callback for the pending write call. |
428 CompletionCallback pending_write_callback_; | 431 CompletionCallback pending_write_callback_; |
429 | 432 |
430 // The length for the pending write, or -1 if not scheduled. | 433 // The length for the pending write, or -1 if not scheduled. |
431 int pending_write_len_; | 434 int pending_write_len_ = -1; |
432 | 435 |
433 // WaitForWrite() wait loop. | 436 // WaitForWrite() wait loop. |
434 std::unique_ptr<base::RunLoop> write_loop_; | 437 std::unique_ptr<base::RunLoop> write_loop_; |
435 }; | 438 }; |
436 | 439 |
437 FakeBlockingStreamSocket::FakeBlockingStreamSocket( | |
438 std::unique_ptr<StreamSocket> transport) | |
439 : WrappedStreamSocket(std::move(transport)), | |
440 should_block_read_(false), | |
441 pending_read_result_(ERR_IO_PENDING), | |
442 should_block_write_(false), | |
443 pending_write_len_(-1) {} | |
444 | |
445 int FakeBlockingStreamSocket::Read(IOBuffer* buf, | 440 int FakeBlockingStreamSocket::Read(IOBuffer* buf, |
446 int len, | 441 int len, |
447 const CompletionCallback& callback) { | 442 const CompletionCallback& callback) { |
448 DCHECK(!pending_read_buf_); | 443 DCHECK(!pending_read_buf_); |
449 DCHECK(pending_read_callback_.is_null()); | 444 DCHECK(pending_read_callback_.is_null()); |
450 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | 445 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); |
451 DCHECK(!callback.is_null()); | 446 DCHECK(!callback.is_null()); |
452 | 447 |
453 int rv = transport_->Read(buf, len, base::Bind( | 448 int rv = transport_->Read(buf, len, base::Bind( |
454 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); | 449 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); |
455 if (rv == ERR_IO_PENDING) { | 450 if (rv == ERR_IO_PENDING || should_block_read_) { |
456 // Save the callback to be called later. | 451 // Save the callback to be called later. |
457 pending_read_buf_ = buf; | 452 pending_read_buf_ = buf; |
| 453 pending_read_buf_len_ = len; |
458 pending_read_callback_ = callback; | 454 pending_read_callback_ = callback; |
459 } else if (should_block_read_) { | 455 // Save the read result. |
460 // Save the callback and read result to be called later. | 456 if (rv != ERR_IO_PENDING) { |
461 pending_read_buf_ = buf; | 457 OnReadCompleted(rv); |
462 pending_read_callback_ = callback; | 458 rv = ERR_IO_PENDING; |
463 OnReadCompleted(rv); | 459 } |
464 rv = ERR_IO_PENDING; | |
465 } | 460 } |
466 return rv; | 461 return rv; |
467 } | 462 } |
468 | 463 |
469 int FakeBlockingStreamSocket::Write(IOBuffer* buf, | 464 int FakeBlockingStreamSocket::Write(IOBuffer* buf, |
470 int len, | 465 int len, |
471 const CompletionCallback& callback) { | 466 const CompletionCallback& callback) { |
472 DCHECK(buf); | 467 DCHECK(buf); |
473 DCHECK_LE(0, len); | 468 DCHECK_LE(0, len); |
474 | 469 |
(...skipping 17 matching lines...) Expand all Loading... |
492 | 487 |
493 void FakeBlockingStreamSocket::BlockReadResult() { | 488 void FakeBlockingStreamSocket::BlockReadResult() { |
494 DCHECK(!should_block_read_); | 489 DCHECK(!should_block_read_); |
495 should_block_read_ = true; | 490 should_block_read_ = true; |
496 } | 491 } |
497 | 492 |
498 void FakeBlockingStreamSocket::UnblockReadResult() { | 493 void FakeBlockingStreamSocket::UnblockReadResult() { |
499 DCHECK(should_block_read_); | 494 DCHECK(should_block_read_); |
500 should_block_read_ = false; | 495 should_block_read_ = false; |
501 | 496 |
502 // If the operation is still pending in the underlying transport, immediately | 497 // If the operation has since completed, return the result to the caller. |
503 // return - OnReadCompleted() will handle invoking the callback once the | 498 if (pending_read_result_ != ERR_IO_PENDING) |
504 // transport has completed. | 499 ReturnReadResult(); |
505 if (pending_read_result_ == ERR_IO_PENDING) | 500 } |
506 return; | 501 |
507 int result = pending_read_result_; | 502 bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) { |
508 pending_read_buf_ = nullptr; | 503 DCHECK(should_block_read_); |
509 pending_read_result_ = ERR_IO_PENDING; | 504 DCHECK_NE(ERR_IO_PENDING, pending_read_result_); |
510 base::ResetAndReturn(&pending_read_callback_).Run(result); | 505 DCHECK(pending_read_buf_); |
| 506 DCHECK_NE(-1, pending_read_buf_len_); |
| 507 |
| 508 if (static_cast<size_t>(pending_read_buf_len_) < data.size()) |
| 509 return false; |
| 510 |
| 511 memcpy(pending_read_buf_->data(), data.data(), data.size()); |
| 512 pending_read_result_ = data.size(); |
| 513 return true; |
511 } | 514 } |
512 | 515 |
513 void FakeBlockingStreamSocket::WaitForReadResult() { | 516 void FakeBlockingStreamSocket::WaitForReadResult() { |
514 DCHECK(should_block_read_); | 517 DCHECK(should_block_read_); |
515 DCHECK(!read_loop_); | 518 DCHECK(!read_loop_); |
516 | 519 |
517 if (pending_read_result_ != ERR_IO_PENDING) | 520 if (pending_read_result_ != ERR_IO_PENDING) |
518 return; | 521 return; |
519 read_loop_.reset(new base::RunLoop); | 522 read_loop_.reset(new base::RunLoop); |
520 read_loop_->Run(); | 523 read_loop_->Run(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 write_loop_.reset(new base::RunLoop); | 559 write_loop_.reset(new base::RunLoop); |
557 write_loop_->Run(); | 560 write_loop_->Run(); |
558 write_loop_.reset(); | 561 write_loop_.reset(); |
559 DCHECK(pending_write_buf_.get()); | 562 DCHECK(pending_write_buf_.get()); |
560 } | 563 } |
561 | 564 |
562 void FakeBlockingStreamSocket::OnReadCompleted(int result) { | 565 void FakeBlockingStreamSocket::OnReadCompleted(int result) { |
563 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | 566 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); |
564 DCHECK(!pending_read_callback_.is_null()); | 567 DCHECK(!pending_read_callback_.is_null()); |
565 | 568 |
| 569 pending_read_result_ = result; |
| 570 |
566 if (should_block_read_) { | 571 if (should_block_read_) { |
567 // Store the result so that the callback can be invoked once Unblock() is | 572 // Defer the result until UnblockReadResult is called. |
568 // called. | |
569 pending_read_result_ = result; | |
570 | |
571 // Stop the WaitForReadResult() call if any. | |
572 if (read_loop_) | 573 if (read_loop_) |
573 read_loop_->Quit(); | 574 read_loop_->Quit(); |
574 } else { | 575 return; |
575 // Either the Read() was never blocked or UnblockReadResult() was called | |
576 // before the Read() completed. Either way, return the result to the caller. | |
577 pending_read_buf_ = nullptr; | |
578 base::ResetAndReturn(&pending_read_callback_).Run(result); | |
579 } | 576 } |
| 577 |
| 578 ReturnReadResult(); |
| 579 } |
| 580 |
| 581 void FakeBlockingStreamSocket::ReturnReadResult() { |
| 582 int result = pending_read_result_; |
| 583 pending_read_result_ = ERR_IO_PENDING; |
| 584 pending_read_buf_ = nullptr; |
| 585 pending_read_buf_len_ = -1; |
| 586 base::ResetAndReturn(&pending_read_callback_).Run(result); |
580 } | 587 } |
581 | 588 |
582 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of | 589 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of |
583 // reads and writes on the socket. | 590 // reads and writes on the socket. |
584 class CountingStreamSocket : public WrappedStreamSocket { | 591 class CountingStreamSocket : public WrappedStreamSocket { |
585 public: | 592 public: |
586 explicit CountingStreamSocket(std::unique_ptr<StreamSocket> transport) | 593 explicit CountingStreamSocket(std::unique_ptr<StreamSocket> transport) |
587 : WrappedStreamSocket(std::move(transport)), | 594 : WrappedStreamSocket(std::move(transport)), |
588 read_count_(0), | 595 read_count_(0), |
589 write_count_(0) {} | 596 write_count_(0) {} |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1016 void EnableAsyncFailingChannelID() { | 1023 void EnableAsyncFailingChannelID() { |
1017 channel_id_service_.reset(new ChannelIDService( | 1024 channel_id_service_.reset(new ChannelIDService( |
1018 new AsyncFailingChannelIDStore(), base::ThreadTaskRunnerHandle::Get())); | 1025 new AsyncFailingChannelIDStore(), base::ThreadTaskRunnerHandle::Get())); |
1019 context_.channel_id_service = channel_id_service_.get(); | 1026 context_.channel_id_service = channel_id_service_.get(); |
1020 } | 1027 } |
1021 | 1028 |
1022 private: | 1029 private: |
1023 std::unique_ptr<ChannelIDService> channel_id_service_; | 1030 std::unique_ptr<ChannelIDService> channel_id_service_; |
1024 }; | 1031 }; |
1025 | 1032 |
| 1033 // Returns a serialized unencrypted TLS 1.2 alert record for the given alert |
| 1034 // value. |
| 1035 std::string FormatTLS12Alert(uint8_t alert) { |
| 1036 std::string ret; |
| 1037 // ContentType.alert |
| 1038 ret.push_back(21); |
| 1039 // Record-layer version. Assume TLS 1.2. |
| 1040 ret.push_back(0x03); |
| 1041 ret.push_back(0x03); |
| 1042 // Record length. |
| 1043 ret.push_back(0); |
| 1044 ret.push_back(2); |
| 1045 // AlertLevel.fatal. |
| 1046 ret.push_back(2); |
| 1047 // The alert itself. |
| 1048 ret.push_back(alert); |
| 1049 return ret; |
| 1050 } |
| 1051 |
1026 } // namespace | 1052 } // namespace |
1027 | 1053 |
1028 TEST_F(SSLClientSocketTest, Connect) { | 1054 TEST_F(SSLClientSocketTest, Connect) { |
1029 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1055 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1030 | 1056 |
1031 TestCompletionCallback callback; | 1057 TestCompletionCallback callback; |
1032 TestNetLog log; | 1058 TestNetLog log; |
1033 std::unique_ptr<StreamSocket> transport( | 1059 std::unique_ptr<StreamSocket> transport( |
1034 new TCPClientSocket(addr(), NULL, &log, NetLog::Source())); | 1060 new TCPClientSocket(addr(), NULL, &log, NetLog::Source())); |
1035 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1061 int rv = callback.GetResult(transport->Connect(callback.callback())); |
(...skipping 2197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3233 ssl_options.request_client_certificate = true; | 3259 ssl_options.request_client_certificate = true; |
3234 ssl_options.client_authorities.push_back( | 3260 ssl_options.client_authorities.push_back( |
3235 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); | 3261 GetTestClientCertsDirectory().AppendASCII("client_1_ca.pem")); |
3236 | 3262 |
3237 ASSERT_TRUE(StartTestServer(ssl_options)); | 3263 ASSERT_TRUE(StartTestServer(ssl_options)); |
3238 | 3264 |
3239 base::FilePath certs_dir = GetTestCertsDirectory(); | 3265 base::FilePath certs_dir = GetTestCertsDirectory(); |
3240 SSLConfig ssl_config; | 3266 SSLConfig ssl_config; |
3241 ssl_config.send_client_cert = true; | 3267 ssl_config.send_client_cert = true; |
3242 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); | 3268 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); |
3243 | |
3244 // This is required to ensure that signing works with the client | |
3245 // certificate's private key. | |
3246 ssl_config.client_private_key = | 3269 ssl_config.client_private_key = |
3247 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key")); | 3270 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key")); |
3248 | 3271 |
3249 int rv; | 3272 int rv; |
3250 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 3273 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
3251 | 3274 |
3252 EXPECT_THAT(rv, IsOk()); | 3275 EXPECT_THAT(rv, IsOk()); |
3253 EXPECT_TRUE(sock_->IsConnected()); | 3276 EXPECT_TRUE(sock_->IsConnected()); |
3254 | 3277 |
3255 SSLInfo ssl_info; | 3278 SSLInfo ssl_info; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3426 SSLInfo ssl_info; | 3449 SSLInfo ssl_info; |
3427 ASSERT_TRUE(sock_->GetSSLInfo(&ssl_info)); | 3450 ASSERT_TRUE(sock_->GetSSLInfo(&ssl_info)); |
3428 | 3451 |
3429 EXPECT_THAT(rv, IsError(ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN)); | 3452 EXPECT_THAT(rv, IsError(ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN)); |
3430 EXPECT_TRUE(ssl_info.cert_status & CERT_STATUS_PINNED_KEY_MISSING); | 3453 EXPECT_TRUE(ssl_info.cert_status & CERT_STATUS_PINNED_KEY_MISSING); |
3431 EXPECT_TRUE(ssl_info.cert_status & | 3454 EXPECT_TRUE(ssl_info.cert_status & |
3432 CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED); | 3455 CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED); |
3433 EXPECT_TRUE(sock_->IsConnected()); | 3456 EXPECT_TRUE(sock_->IsConnected()); |
3434 } | 3457 } |
3435 | 3458 |
| 3459 // Test that handshake_failure alerts at the ServerHello are mapped to |
| 3460 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 3461 TEST_F(SSLClientSocketTest, HandshakeFailureServerHello) { |
| 3462 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
| 3463 |
| 3464 TestCompletionCallback callback; |
| 3465 std::unique_ptr<StreamSocket> real_transport( |
| 3466 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source())); |
| 3467 std::unique_ptr<FakeBlockingStreamSocket> transport( |
| 3468 new FakeBlockingStreamSocket(std::move(real_transport))); |
| 3469 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 3470 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 3471 ASSERT_THAT(rv, IsOk()); |
| 3472 |
| 3473 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 3474 std::move(transport), spawned_test_server()->host_port_pair(), |
| 3475 SSLConfig())); |
| 3476 |
| 3477 // Connect. Stop before the client processes ServerHello. |
| 3478 raw_transport->BlockReadResult(); |
| 3479 rv = sock->Connect(callback.callback()); |
| 3480 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 3481 raw_transport->WaitForReadResult(); |
| 3482 |
| 3483 // Replace it with an alert. |
| 3484 raw_transport->ReplaceReadResult( |
| 3485 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */)); |
| 3486 raw_transport->UnblockReadResult(); |
| 3487 |
| 3488 rv = callback.GetResult(rv); |
| 3489 EXPECT_THAT(rv, IsError(ERR_SSL_VERSION_OR_CIPHER_MISMATCH)); |
| 3490 } |
| 3491 |
| 3492 // Test that handshake_failure alerts after the ServerHello but without a |
| 3493 // CertificateRequest are mapped to ERR_SSL_PROTOCOL_ERROR. |
| 3494 TEST_F(SSLClientSocketTest, HandshakeFailureNoClientCerts) { |
| 3495 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
| 3496 |
| 3497 TestCompletionCallback callback; |
| 3498 std::unique_ptr<StreamSocket> real_transport( |
| 3499 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source())); |
| 3500 std::unique_ptr<FakeBlockingStreamSocket> transport( |
| 3501 new FakeBlockingStreamSocket(std::move(real_transport))); |
| 3502 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 3503 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 3504 ASSERT_THAT(rv, IsOk()); |
| 3505 |
| 3506 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 3507 std::move(transport), spawned_test_server()->host_port_pair(), |
| 3508 SSLConfig())); |
| 3509 |
| 3510 // Connect. Stop before the client processes ServerHello. |
| 3511 raw_transport->BlockReadResult(); |
| 3512 rv = sock->Connect(callback.callback()); |
| 3513 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 3514 raw_transport->WaitForReadResult(); |
| 3515 |
| 3516 // Release the ServerHello and wait for the client to write its second flight. |
| 3517 raw_transport->BlockWrite(); |
| 3518 raw_transport->UnblockReadResult(); |
| 3519 raw_transport->WaitForWrite(); |
| 3520 |
| 3521 // Wait for the server's final flight. |
| 3522 raw_transport->BlockReadResult(); |
| 3523 raw_transport->UnblockWrite(); |
| 3524 raw_transport->WaitForReadResult(); |
| 3525 |
| 3526 // Replace it with an alert. |
| 3527 raw_transport->ReplaceReadResult( |
| 3528 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */)); |
| 3529 raw_transport->UnblockReadResult(); |
| 3530 |
| 3531 rv = callback.GetResult(rv); |
| 3532 EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 3533 } |
| 3534 |
| 3535 // Test that handshake_failure alerts after the ServerHello map to |
| 3536 // ERR_BAD_SSL_CLIENT_AUTH_CERT if a client certificate was requested but not |
| 3537 // supplied. TLS does not have an alert for this case, so handshake_failure is |
| 3538 // common. See https://crbug.com/646567. |
| 3539 TEST_F(SSLClientSocketTest, LateHandshakeFailureMissingClientCerts) { |
| 3540 // Request a client certificate. |
| 3541 SpawnedTestServer::SSLOptions ssl_options; |
| 3542 ssl_options.request_client_certificate = true; |
| 3543 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 3544 |
| 3545 TestCompletionCallback callback; |
| 3546 std::unique_ptr<StreamSocket> real_transport( |
| 3547 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source())); |
| 3548 std::unique_ptr<FakeBlockingStreamSocket> transport( |
| 3549 new FakeBlockingStreamSocket(std::move(real_transport))); |
| 3550 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 3551 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 3552 ASSERT_THAT(rv, IsOk()); |
| 3553 |
| 3554 // Send no client certificate. |
| 3555 SSLConfig config; |
| 3556 config.send_client_cert = true; |
| 3557 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 3558 std::move(transport), spawned_test_server()->host_port_pair(), config)); |
| 3559 |
| 3560 // Connect. Stop before the client processes ServerHello. |
| 3561 raw_transport->BlockReadResult(); |
| 3562 rv = sock->Connect(callback.callback()); |
| 3563 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 3564 raw_transport->WaitForReadResult(); |
| 3565 |
| 3566 // Release the ServerHello and wait for the client to write its second flight. |
| 3567 raw_transport->BlockWrite(); |
| 3568 raw_transport->UnblockReadResult(); |
| 3569 raw_transport->WaitForWrite(); |
| 3570 |
| 3571 // Wait for the server's final flight. |
| 3572 raw_transport->BlockReadResult(); |
| 3573 raw_transport->UnblockWrite(); |
| 3574 raw_transport->WaitForReadResult(); |
| 3575 |
| 3576 // Replace it with an alert. |
| 3577 raw_transport->ReplaceReadResult( |
| 3578 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */)); |
| 3579 raw_transport->UnblockReadResult(); |
| 3580 |
| 3581 rv = callback.GetResult(rv); |
| 3582 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); |
| 3583 } |
| 3584 |
| 3585 // Test that handshake_failure alerts after the ServerHello map to |
| 3586 // ERR_SSL_PROTOCOL_ERROR if received after sending a client certificate. It is |
| 3587 // assumed servers will send a more appropriate alert in this case. |
| 3588 TEST_F(SSLClientSocketTest, LateHandshakeFailureSendClientCerts) { |
| 3589 // Request a client certificate. |
| 3590 SpawnedTestServer::SSLOptions ssl_options; |
| 3591 ssl_options.request_client_certificate = true; |
| 3592 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 3593 |
| 3594 TestCompletionCallback callback; |
| 3595 std::unique_ptr<StreamSocket> real_transport( |
| 3596 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source())); |
| 3597 std::unique_ptr<FakeBlockingStreamSocket> transport( |
| 3598 new FakeBlockingStreamSocket(std::move(real_transport))); |
| 3599 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 3600 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 3601 ASSERT_THAT(rv, IsOk()); |
| 3602 |
| 3603 // Send a client certificate. |
| 3604 base::FilePath certs_dir = GetTestCertsDirectory(); |
| 3605 SSLConfig config; |
| 3606 config.send_client_cert = true; |
| 3607 config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); |
| 3608 config.client_private_key = |
| 3609 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key")); |
| 3610 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 3611 std::move(transport), spawned_test_server()->host_port_pair(), config)); |
| 3612 |
| 3613 // Connect. Stop before the client processes ServerHello. |
| 3614 raw_transport->BlockReadResult(); |
| 3615 rv = sock->Connect(callback.callback()); |
| 3616 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 3617 raw_transport->WaitForReadResult(); |
| 3618 |
| 3619 // Release the ServerHello and wait for the client to write its second flight. |
| 3620 raw_transport->BlockWrite(); |
| 3621 raw_transport->UnblockReadResult(); |
| 3622 raw_transport->WaitForWrite(); |
| 3623 |
| 3624 // Wait for the server's final flight. |
| 3625 raw_transport->BlockReadResult(); |
| 3626 raw_transport->UnblockWrite(); |
| 3627 raw_transport->WaitForReadResult(); |
| 3628 |
| 3629 // Replace it with an alert. |
| 3630 raw_transport->ReplaceReadResult( |
| 3631 FormatTLS12Alert(40 /* AlertDescription.handshake_failure */)); |
| 3632 raw_transport->UnblockReadResult(); |
| 3633 |
| 3634 rv = callback.GetResult(rv); |
| 3635 EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 3636 } |
| 3637 |
| 3638 // Test that access_denied alerts are mapped to ERR_SSL_PROTOCOL_ERROR if |
| 3639 // received on a connection not requesting client certificates. This is an |
| 3640 // incorrect use of the alert but is common. See https://crbug.com/630883. |
| 3641 TEST_F(SSLClientSocketTest, AccessDeniedNoClientCerts) { |
| 3642 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
| 3643 |
| 3644 TestCompletionCallback callback; |
| 3645 std::unique_ptr<StreamSocket> real_transport( |
| 3646 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source())); |
| 3647 std::unique_ptr<FakeBlockingStreamSocket> transport( |
| 3648 new FakeBlockingStreamSocket(std::move(real_transport))); |
| 3649 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 3650 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 3651 ASSERT_THAT(rv, IsOk()); |
| 3652 |
| 3653 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 3654 std::move(transport), spawned_test_server()->host_port_pair(), |
| 3655 SSLConfig())); |
| 3656 |
| 3657 // Connect. Stop before the client processes ServerHello. |
| 3658 raw_transport->BlockReadResult(); |
| 3659 rv = sock->Connect(callback.callback()); |
| 3660 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 3661 raw_transport->WaitForReadResult(); |
| 3662 |
| 3663 // Release the ServerHello and wait for the client to write its second flight. |
| 3664 raw_transport->BlockWrite(); |
| 3665 raw_transport->UnblockReadResult(); |
| 3666 raw_transport->WaitForWrite(); |
| 3667 |
| 3668 // Wait for the server's final flight. |
| 3669 raw_transport->BlockReadResult(); |
| 3670 raw_transport->UnblockWrite(); |
| 3671 raw_transport->WaitForReadResult(); |
| 3672 |
| 3673 // Replace it with an alert. |
| 3674 raw_transport->ReplaceReadResult( |
| 3675 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); |
| 3676 raw_transport->UnblockReadResult(); |
| 3677 |
| 3678 rv = callback.GetResult(rv); |
| 3679 EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 3680 } |
| 3681 |
| 3682 // Test that access_denied alerts are mapped to ERR_BAD_SSL_CLIENT_AUTH_CERT if |
| 3683 // received on a connection requesting client certificates. |
| 3684 TEST_F(SSLClientSocketTest, AccessDeniedClientCerts) { |
| 3685 // Request a client certificate. |
| 3686 SpawnedTestServer::SSLOptions ssl_options; |
| 3687 ssl_options.request_client_certificate = true; |
| 3688 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 3689 |
| 3690 TestCompletionCallback callback; |
| 3691 std::unique_ptr<StreamSocket> real_transport( |
| 3692 new TCPClientSocket(addr(), NULL, NULL, NetLog::Source())); |
| 3693 std::unique_ptr<FakeBlockingStreamSocket> transport( |
| 3694 new FakeBlockingStreamSocket(std::move(real_transport))); |
| 3695 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 3696 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 3697 ASSERT_THAT(rv, IsOk()); |
| 3698 |
| 3699 // Send a client certificate. |
| 3700 base::FilePath certs_dir = GetTestCertsDirectory(); |
| 3701 SSLConfig config; |
| 3702 config.send_client_cert = true; |
| 3703 config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); |
| 3704 config.client_private_key = |
| 3705 LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key")); |
| 3706 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 3707 std::move(transport), spawned_test_server()->host_port_pair(), config)); |
| 3708 |
| 3709 // Connect. Stop before the client processes ServerHello. |
| 3710 raw_transport->BlockReadResult(); |
| 3711 rv = sock->Connect(callback.callback()); |
| 3712 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 3713 raw_transport->WaitForReadResult(); |
| 3714 |
| 3715 // Release the ServerHello and wait for the client to write its second flight. |
| 3716 raw_transport->BlockWrite(); |
| 3717 raw_transport->UnblockReadResult(); |
| 3718 raw_transport->WaitForWrite(); |
| 3719 |
| 3720 // Wait for the server's final flight. |
| 3721 raw_transport->BlockReadResult(); |
| 3722 raw_transport->UnblockWrite(); |
| 3723 raw_transport->WaitForReadResult(); |
| 3724 |
| 3725 // Replace it with an alert. |
| 3726 raw_transport->ReplaceReadResult( |
| 3727 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); |
| 3728 raw_transport->UnblockReadResult(); |
| 3729 |
| 3730 rv = callback.GetResult(rv); |
| 3731 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); |
| 3732 } |
| 3733 |
3436 } // namespace net | 3734 } // namespace net |
OLD | NEW |