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

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

Issue 416683002: This CL corrects a bug in which the OnHandshakeComplete callback for an ssl session was never called (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@r2
Patch Set: Removed session to callback map from SSLSessionCallback, added unittest Created 6 years, 4 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 // OpenSSL binding for SSLClientSocket. The class layout and general principle 5 // OpenSSL binding for SSLClientSocket. The class layout and general principle
6 // of operation is derived from SSLClientSocketNSS. 6 // of operation is derived from SSLClientSocketNSS.
7 7
8 #include "net/socket/ssl_client_socket_openssl.h" 8 #include "net/socket/ssl_client_socket_openssl.h"
9 9
10 #include <errno.h> 10 #include <errno.h>
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 ssl_(NULL), 356 ssl_(NULL),
357 transport_bio_(NULL), 357 transport_bio_(NULL),
358 transport_(transport_socket.Pass()), 358 transport_(transport_socket.Pass()),
359 host_and_port_(host_and_port), 359 host_and_port_(host_and_port),
360 ssl_config_(ssl_config), 360 ssl_config_(ssl_config),
361 ssl_session_cache_shard_(context.ssl_session_cache_shard), 361 ssl_session_cache_shard_(context.ssl_session_cache_shard),
362 trying_cached_session_(false), 362 trying_cached_session_(false),
363 next_handshake_state_(STATE_NONE), 363 next_handshake_state_(STATE_NONE),
364 npn_status_(kNextProtoUnsupported), 364 npn_status_(kNextProtoUnsupported),
365 channel_id_xtn_negotiated_(false), 365 channel_id_xtn_negotiated_(false),
366 session_completion_count_(0),
366 net_log_(transport_->socket()->NetLog()) { 367 net_log_(transport_->socket()->NetLog()) {
367 } 368 }
368 369
369 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { 370 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() {
370 Disconnect(); 371 Disconnect();
371 } 372 }
372 373
373 bool SSLClientSocketOpenSSL::InSessionCache() const { 374 bool SSLClientSocketOpenSSL::InSessionCache() const {
374 SSLContext* context = SSLContext::GetInstance(); 375 SSLContext* context = SSLContext::GetInstance();
375 std::string cache_key = GetSessionCacheKey(); 376 std::string cache_key = GetSessionCacheKey();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { 429 int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) {
429 net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT); 430 net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT);
430 431
431 // Set up new ssl object. 432 // Set up new ssl object.
432 int rv = Init(); 433 int rv = Init();
433 if (rv != OK) { 434 if (rv != OK) {
434 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); 435 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv);
435 return rv; 436 return rv;
436 } 437 }
437 438
438 if (!handshake_completion_callback_.is_null()) {
439 SSLContext* context = SSLContext::GetInstance();
440 context->session_cache()->SetSessionAddedCallback(
441 ssl_,
442 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeCompletion,
443 base::Unretained(this)));
444 }
445
446 // Set SSL to client mode. Handshake happens in the loop below. 439 // Set SSL to client mode. Handshake happens in the loop below.
447 SSL_set_connect_state(ssl_); 440 SSL_set_connect_state(ssl_);
448 441
449 GotoState(STATE_HANDSHAKE); 442 GotoState(STATE_HANDSHAKE);
450 rv = DoHandshakeLoop(OK); 443 rv = DoHandshakeLoop(OK);
451 if (rv == ERR_IO_PENDING) { 444 if (rv == ERR_IO_PENDING) {
452 user_connect_callback_ = callback; 445 user_connect_callback_ = callback;
453 } else { 446 } else {
454 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); 447 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv);
455 if (rv < OK) 448 if (rv < OK)
456 OnHandshakeCompletion(); 449 OnHandshakeCompletion();
457 } 450 }
458 451
459 return rv > OK ? OK : rv; 452 return rv > OK ? OK : rv;
460 } 453 }
461 454
462 void SSLClientSocketOpenSSL::Disconnect() { 455 void SSLClientSocketOpenSSL::Disconnect() {
463 // If a handshake was pending (Connect() had been called), notify interested 456 // If a handshake was pending (Connect() had been called), notify interested
464 // parties that it's been aborted now. If the handshake had already 457 // parties that it's been aborted now. If the handshake had already
465 // completed, this is a no-op. 458 // completed, this is a no-op.
466 OnHandshakeCompletion(); 459 OnHandshakeCompletion();
467 if (ssl_) { 460 if (ssl_) {
468 SSLContext* context = SSLContext::GetInstance();
469 context->session_cache()->RemoveSessionAddedCallback(ssl_);
470 // Calling SSL_shutdown prevents the session from being marked as 461 // Calling SSL_shutdown prevents the session from being marked as
471 // unresumable. 462 // unresumable.
472 SSL_shutdown(ssl_); 463 SSL_shutdown(ssl_);
473 SSL_free(ssl_); 464 SSL_free(ssl_);
474 ssl_ = NULL; 465 ssl_ = NULL;
475 } 466 }
476 if (transport_bio_) { 467 if (transport_bio_) {
477 BIO_free_all(transport_bio_); 468 BIO_free_all(transport_bio_);
478 transport_bio_ = NULL; 469 transport_bio_ = NULL;
479 } 470 }
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 } 671 }
681 672
682 int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { 673 int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) {
683 return transport_->socket()->SetReceiveBufferSize(size); 674 return transport_->socket()->SetReceiveBufferSize(size);
684 } 675 }
685 676
686 int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { 677 int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) {
687 return transport_->socket()->SetSendBufferSize(size); 678 return transport_->socket()->SetSendBufferSize(size);
688 } 679 }
689 680
681 // static
682 void SSLClientSocketOpenSSL::OnSessionFinishedCallback(const SSL* ssl,
683 int result,
684 int unused) {
685 SSLClientSocketOpenSSL* ssl_socket =
686 SSLContext::GetInstance()->GetClientSocketFromSSL(ssl);
687 if (result == SSL_CB_HANDSHAKE_DONE)
688 ssl_socket->CheckIfSessionFinished();
689 }
690
690 int SSLClientSocketOpenSSL::Init() { 691 int SSLClientSocketOpenSSL::Init() {
691 DCHECK(!ssl_); 692 DCHECK(!ssl_);
692 DCHECK(!transport_bio_); 693 DCHECK(!transport_bio_);
693 694
694 SSLContext* context = SSLContext::GetInstance(); 695 SSLContext* context = SSLContext::GetInstance();
695 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 696 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
696 697
697 ssl_ = SSL_new(context->ssl_ctx()); 698 ssl_ = SSL_new(context->ssl_ctx());
698 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) 699 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this))
699 return ERR_UNEXPECTED; 700 return ERR_UNEXPECTED;
700 701
701 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) 702 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str()))
702 return ERR_UNEXPECTED; 703 return ERR_UNEXPECTED;
703 704
705 // Set an OpenSSL callback to monitor this SSL*'s connection.
706 SSL_set_info_callback(ssl_, &OnSessionFinishedCallback);
707
704 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( 708 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey(
705 ssl_, GetSessionCacheKey()); 709 ssl_, GetSessionCacheKey());
706 710
707 BIO* ssl_bio = NULL; 711 BIO* ssl_bio = NULL;
708 // 0 => use default buffer sizes. 712 // 0 => use default buffer sizes.
709 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) 713 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0))
710 return ERR_UNEXPECTED; 714 return ERR_UNEXPECTED;
711 DCHECK(ssl_bio); 715 DCHECK(ssl_bio);
712 DCHECK(transport_bio_); 716 DCHECK(transport_bio_);
713 717
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 net_log_); 1029 net_log_);
1026 } 1030 }
1027 1031
1028 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { 1032 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) {
1029 verifier_.reset(); 1033 verifier_.reset();
1030 1034
1031 if (result == OK) { 1035 if (result == OK) {
1032 // TODO(joth): Work out if we need to remember the intermediate CA certs 1036 // TODO(joth): Work out if we need to remember the intermediate CA certs
1033 // when the server sends them to us, and do so here. 1037 // when the server sends them to us, and do so here.
1034 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); 1038 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_);
1039 CheckIfSessionFinished();
1035 } else { 1040 } else {
1036 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) 1041 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result)
1037 << " (" << result << ")"; 1042 << " (" << result << ")";
1038 } 1043 }
1039 1044
1040 completed_handshake_ = true; 1045 completed_handshake_ = true;
1041 // Exit DoHandshakeLoop and return the result to the caller to Connect. 1046 // Exit DoHandshakeLoop and return the result to the caller to Connect.
1042 DCHECK_EQ(STATE_NONE, next_handshake_state_); 1047 DCHECK_EQ(STATE_NONE, next_handshake_state_);
1043 return result; 1048 return result;
1044 } 1049 }
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
1570 // write payload. If the current payload fails to write, the error will be 1575 // write payload. If the current payload fails to write, the error will be
1571 // reported in a future write or read to |bio|. 1576 // reported in a future write or read to |bio|.
1572 if (transport_write_error_ != OK) { 1577 if (transport_write_error_ != OK) {
1573 OpenSSLPutNetError(FROM_HERE, transport_write_error_); 1578 OpenSSLPutNetError(FROM_HERE, transport_write_error_);
1574 return -1; 1579 return -1;
1575 } 1580 }
1576 } 1581 }
1577 return retvalue; 1582 return retvalue;
1578 } 1583 }
1579 1584
1585 // Determines if the session for |ssl| is in the cache, and calls the
1586 // appropriate callback if that is the case.
1587 //
1588 // CheckIfSessionFinished is called twice per connection: once after
1589 // MarkSSLSessionAsGood, when the certificate has been verified, and
1590 // once via an OpenSSL callback when the handshake has completed. On the
1591 // second call, the connection's handshake completion callback is run.
1592 void SSLClientSocketOpenSSL::CheckIfSessionFinished() {
1593 // Increment the session's completion count.
1594 if (++session_completion_count_ == 2) {
Ryan Sleevi 2014/08/06 01:31:30 Is this still necessary / the right approach, now
mshelley 2014/08/06 03:09:31 Done.
1595 OnHandshakeCompletion();
1596 }
1597 }
1598
1580 // static 1599 // static
1581 long SSLClientSocketOpenSSL::BIOCallback( 1600 long SSLClientSocketOpenSSL::BIOCallback(
1582 BIO *bio, 1601 BIO *bio,
1583 int cmd, 1602 int cmd,
1584 const char *argp, int argi, long argl, 1603 const char *argp, int argi, long argl,
1585 long retvalue) { 1604 long retvalue) {
1586 SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>( 1605 SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>(
1587 BIO_get_callback_arg(bio)); 1606 BIO_get_callback_arg(bio));
1588 CHECK(socket); 1607 CHECK(socket);
1589 return socket->MaybeReplayTransportError( 1608 return socket->MaybeReplayTransportError(
1590 bio, cmd, argp, argi, argl, retvalue); 1609 bio, cmd, argp, argi, argl, retvalue);
1591 } 1610 }
1592 1611
1593 scoped_refptr<X509Certificate> 1612 scoped_refptr<X509Certificate>
1594 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { 1613 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const {
1595 return server_cert_; 1614 return server_cert_;
1596 } 1615 }
1597 1616
1598 } // namespace net 1617 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698