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 // 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 ERR_print_errors_cb(&LogErrorCallback, NULL); | 212 ERR_print_errors_cb(&LogErrorCallback, NULL); |
213 } else { | 213 } else { |
214 SSL_CTX_set_keylog_bio(ssl_ctx_.get(), bio); | 214 SSL_CTX_set_keylog_bio(ssl_ctx_.get(), bio); |
215 } | 215 } |
216 } | 216 } |
217 } | 217 } |
218 | 218 |
219 static std::string GetSessionCacheKey(const SSL* ssl) { | 219 static std::string GetSessionCacheKey(const SSL* ssl) { |
220 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 220 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
221 DCHECK(socket); | 221 DCHECK(socket); |
222 return socket->GetSessionCacheKey(); | 222 return GetSocketSessionCacheKey(*socket); |
davidben
2015/03/09 18:02:36
I think you forgot to restore this function.
| |
223 } | 223 } |
224 | 224 |
225 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; | 225 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; |
226 | 226 |
227 static int ClientCertRequestCallback(SSL* ssl, void* arg) { | 227 static int ClientCertRequestCallback(SSL* ssl, void* arg) { |
228 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 228 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
229 DCHECK(socket); | 229 DCHECK(socket); |
230 return socket->ClientCertRequestCallback(ssl); | 230 return socket->ClientCertRequestCallback(ssl); |
231 } | 231 } |
232 | 232 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 const HostPortPair& host_and_port, | 363 const HostPortPair& host_and_port, |
364 const SSLConfig& ssl_config, | 364 const SSLConfig& ssl_config, |
365 const SSLClientSocketContext& context) | 365 const SSLClientSocketContext& context) |
366 : transport_send_busy_(false), | 366 : transport_send_busy_(false), |
367 transport_recv_busy_(false), | 367 transport_recv_busy_(false), |
368 pending_read_error_(kNoPendingReadResult), | 368 pending_read_error_(kNoPendingReadResult), |
369 pending_read_ssl_error_(SSL_ERROR_NONE), | 369 pending_read_ssl_error_(SSL_ERROR_NONE), |
370 transport_read_error_(OK), | 370 transport_read_error_(OK), |
371 transport_write_error_(OK), | 371 transport_write_error_(OK), |
372 server_cert_chain_(new PeerCertificateChain(NULL)), | 372 server_cert_chain_(new PeerCertificateChain(NULL)), |
373 completed_connect_(false), | 373 completed_handshake_(false), |
374 was_ever_used_(false), | 374 was_ever_used_(false), |
375 client_auth_cert_needed_(false), | 375 client_auth_cert_needed_(false), |
376 cert_verifier_(context.cert_verifier), | 376 cert_verifier_(context.cert_verifier), |
377 cert_transparency_verifier_(context.cert_transparency_verifier), | 377 cert_transparency_verifier_(context.cert_transparency_verifier), |
378 channel_id_service_(context.channel_id_service), | 378 channel_id_service_(context.channel_id_service), |
379 ssl_(NULL), | 379 ssl_(NULL), |
380 transport_bio_(NULL), | 380 transport_bio_(NULL), |
381 transport_(transport_socket.Pass()), | 381 transport_(transport_socket.Pass()), |
382 host_and_port_(host_and_port), | 382 host_and_port_(host_and_port), |
383 ssl_config_(ssl_config), | 383 ssl_config_(ssl_config), |
384 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 384 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
385 trying_cached_session_(false), | 385 trying_cached_session_(false), |
386 next_handshake_state_(STATE_NONE), | 386 next_handshake_state_(STATE_NONE), |
387 npn_status_(kNextProtoUnsupported), | 387 npn_status_(kNextProtoUnsupported), |
388 channel_id_xtn_negotiated_(false), | 388 channel_id_xtn_negotiated_(false), |
389 handshake_succeeded_(false), | |
390 marked_session_as_good_(false), | |
391 transport_security_state_(context.transport_security_state), | 389 transport_security_state_(context.transport_security_state), |
392 policy_enforcer_(context.cert_policy_enforcer), | 390 policy_enforcer_(context.cert_policy_enforcer), |
393 net_log_(transport_->socket()->NetLog()), | 391 net_log_(transport_->socket()->NetLog()), |
394 weak_factory_(this) { | 392 weak_factory_(this) { |
395 } | 393 } |
396 | 394 |
397 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 395 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
398 Disconnect(); | 396 Disconnect(); |
399 } | 397 } |
400 | 398 |
401 std::string SSLClientSocketOpenSSL::GetSessionCacheKey() const { | |
402 std::string result = host_and_port_.ToString(); | |
403 result.append("/"); | |
404 result.append(ssl_session_cache_shard_); | |
405 | |
406 // Shard the session cache based on maximum protocol version. This causes | |
407 // fallback connections to use a separate session cache. | |
davidben
2015/03/09 18:02:36
GetSocketSessionCacheKey got lost but, when you br
| |
408 result.append("/"); | |
409 switch (ssl_config_.version_max) { | |
410 case SSL_PROTOCOL_VERSION_SSL3: | |
411 result.append("ssl3"); | |
412 break; | |
413 case SSL_PROTOCOL_VERSION_TLS1: | |
414 result.append("tls1"); | |
415 break; | |
416 case SSL_PROTOCOL_VERSION_TLS1_1: | |
417 result.append("tls1.1"); | |
418 break; | |
419 case SSL_PROTOCOL_VERSION_TLS1_2: | |
420 result.append("tls1.2"); | |
421 break; | |
422 default: | |
423 NOTREACHED(); | |
424 } | |
425 | |
426 return result; | |
427 } | |
428 | |
429 bool SSLClientSocketOpenSSL::InSessionCache() const { | |
430 SSLContext* context = SSLContext::GetInstance(); | |
431 std::string cache_key = GetSessionCacheKey(); | |
432 return context->session_cache()->SSLSessionIsInCache(cache_key); | |
433 } | |
434 | |
435 void SSLClientSocketOpenSSL::SetHandshakeCompletionCallback( | |
436 const base::Closure& callback) { | |
437 handshake_completion_callback_ = callback; | |
438 } | |
439 | |
440 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 399 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
441 SSLCertRequestInfo* cert_request_info) { | 400 SSLCertRequestInfo* cert_request_info) { |
442 cert_request_info->host_and_port = host_and_port_; | 401 cert_request_info->host_and_port = host_and_port_; |
443 cert_request_info->cert_authorities = cert_authorities_; | 402 cert_request_info->cert_authorities = cert_authorities_; |
444 cert_request_info->cert_key_types = cert_key_types_; | 403 cert_request_info->cert_key_types = cert_key_types_; |
445 } | 404 } |
446 | 405 |
447 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( | 406 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
448 std::string* proto) { | 407 std::string* proto) { |
449 *proto = npn_proto_; | 408 *proto = npn_proto_; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
502 SSL_enable_fastradio_padding(ssl_, | 461 SSL_enable_fastradio_padding(ssl_, |
503 ssl_config_.fastradio_padding_enabled && | 462 ssl_config_.fastradio_padding_enabled && |
504 ssl_config_.fastradio_padding_eligible); | 463 ssl_config_.fastradio_padding_eligible); |
505 | 464 |
506 GotoState(STATE_HANDSHAKE); | 465 GotoState(STATE_HANDSHAKE); |
507 rv = DoHandshakeLoop(OK); | 466 rv = DoHandshakeLoop(OK); |
508 if (rv == ERR_IO_PENDING) { | 467 if (rv == ERR_IO_PENDING) { |
509 user_connect_callback_ = callback; | 468 user_connect_callback_ = callback; |
510 } else { | 469 } else { |
511 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 470 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
512 if (rv < OK) | |
513 OnHandshakeCompletion(); | |
514 } | 471 } |
515 | 472 |
516 return rv > OK ? OK : rv; | 473 return rv > OK ? OK : rv; |
517 } | 474 } |
518 | 475 |
519 void SSLClientSocketOpenSSL::Disconnect() { | 476 void SSLClientSocketOpenSSL::Disconnect() { |
520 // If a handshake was pending (Connect() had been called), notify interested | |
521 // parties that it's been aborted now. If the handshake had already | |
522 // completed, this is a no-op. | |
523 OnHandshakeCompletion(); | |
524 if (ssl_) { | 477 if (ssl_) { |
525 // Calling SSL_shutdown prevents the session from being marked as | 478 // Calling SSL_shutdown prevents the session from being marked as |
526 // unresumable. | 479 // unresumable. |
527 SSL_shutdown(ssl_); | 480 SSL_shutdown(ssl_); |
528 SSL_free(ssl_); | 481 SSL_free(ssl_); |
529 ssl_ = NULL; | 482 ssl_ = NULL; |
530 } | 483 } |
531 if (transport_bio_) { | 484 if (transport_bio_) { |
532 BIO_free_all(transport_bio_); | 485 BIO_free_all(transport_bio_); |
533 transport_bio_ = NULL; | 486 transport_bio_ = NULL; |
(...skipping 18 matching lines...) Expand all Loading... | |
552 user_write_buf_len_ = 0; | 505 user_write_buf_len_ = 0; |
553 | 506 |
554 pending_read_error_ = kNoPendingReadResult; | 507 pending_read_error_ = kNoPendingReadResult; |
555 pending_read_ssl_error_ = SSL_ERROR_NONE; | 508 pending_read_ssl_error_ = SSL_ERROR_NONE; |
556 pending_read_error_info_ = OpenSSLErrorInfo(); | 509 pending_read_error_info_ = OpenSSLErrorInfo(); |
557 | 510 |
558 transport_read_error_ = OK; | 511 transport_read_error_ = OK; |
559 transport_write_error_ = OK; | 512 transport_write_error_ = OK; |
560 | 513 |
561 server_cert_verify_result_.Reset(); | 514 server_cert_verify_result_.Reset(); |
562 completed_connect_ = false; | 515 completed_handshake_ = false; |
563 | 516 |
564 cert_authorities_.clear(); | 517 cert_authorities_.clear(); |
565 cert_key_types_.clear(); | 518 cert_key_types_.clear(); |
566 client_auth_cert_needed_ = false; | 519 client_auth_cert_needed_ = false; |
567 | 520 |
568 start_cert_verification_time_ = base::TimeTicks(); | 521 start_cert_verification_time_ = base::TimeTicks(); |
569 | 522 |
570 npn_status_ = kNextProtoUnsupported; | 523 npn_status_ = kNextProtoUnsupported; |
571 npn_proto_.clear(); | 524 npn_proto_.clear(); |
572 | 525 |
573 channel_id_xtn_negotiated_ = false; | 526 channel_id_xtn_negotiated_ = false; |
574 channel_id_request_handle_.Cancel(); | 527 channel_id_request_handle_.Cancel(); |
575 } | 528 } |
576 | 529 |
577 bool SSLClientSocketOpenSSL::IsConnected() const { | 530 bool SSLClientSocketOpenSSL::IsConnected() const { |
578 // If the handshake has not yet completed. | 531 // If the handshake has not yet completed. |
579 if (!completed_connect_) | 532 if (!completed_handshake_) |
580 return false; | 533 return false; |
581 // If an asynchronous operation is still pending. | 534 // If an asynchronous operation is still pending. |
582 if (user_read_buf_.get() || user_write_buf_.get()) | 535 if (user_read_buf_.get() || user_write_buf_.get()) |
583 return true; | 536 return true; |
584 | 537 |
585 return transport_->socket()->IsConnected(); | 538 return transport_->socket()->IsConnected(); |
586 } | 539 } |
587 | 540 |
588 bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { | 541 bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { |
589 // If the handshake has not yet completed. | 542 // If the handshake has not yet completed. |
590 if (!completed_connect_) | 543 if (!completed_handshake_) |
591 return false; | 544 return false; |
592 // If an asynchronous operation is still pending. | 545 // If an asynchronous operation is still pending. |
593 if (user_read_buf_.get() || user_write_buf_.get()) | 546 if (user_read_buf_.get() || user_write_buf_.get()) |
594 return false; | 547 return false; |
595 // If there is data waiting to be sent, or data read from the network that | 548 // If there is data waiting to be sent, or data read from the network that |
596 // has not yet been consumed. | 549 // has not yet been consumed. |
597 if (BIO_pending(transport_bio_) > 0 || | 550 if (BIO_pending(transport_bio_) > 0 || |
598 BIO_wpending(transport_bio_) > 0) { | 551 BIO_wpending(transport_bio_) > 0) { |
599 return false; | 552 return false; |
600 } | 553 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
692 | 645 |
693 int rv = DoReadLoop(); | 646 int rv = DoReadLoop(); |
694 | 647 |
695 if (rv == ERR_IO_PENDING) { | 648 if (rv == ERR_IO_PENDING) { |
696 user_read_callback_ = callback; | 649 user_read_callback_ = callback; |
697 } else { | 650 } else { |
698 if (rv > 0) | 651 if (rv > 0) |
699 was_ever_used_ = true; | 652 was_ever_used_ = true; |
700 user_read_buf_ = NULL; | 653 user_read_buf_ = NULL; |
701 user_read_buf_len_ = 0; | 654 user_read_buf_len_ = 0; |
702 if (rv <= 0) { | |
703 // Failure of a read attempt may indicate a failed false start | |
704 // connection. | |
705 OnHandshakeCompletion(); | |
706 } | |
707 } | 655 } |
708 | 656 |
709 return rv; | 657 return rv; |
710 } | 658 } |
711 | 659 |
712 int SSLClientSocketOpenSSL::Write(IOBuffer* buf, | 660 int SSLClientSocketOpenSSL::Write(IOBuffer* buf, |
713 int buf_len, | 661 int buf_len, |
714 const CompletionCallback& callback) { | 662 const CompletionCallback& callback) { |
715 user_write_buf_ = buf; | 663 user_write_buf_ = buf; |
716 user_write_buf_len_ = buf_len; | 664 user_write_buf_len_ = buf_len; |
717 | 665 |
718 int rv = DoWriteLoop(); | 666 int rv = DoWriteLoop(); |
719 | 667 |
720 if (rv == ERR_IO_PENDING) { | 668 if (rv == ERR_IO_PENDING) { |
721 user_write_callback_ = callback; | 669 user_write_callback_ = callback; |
722 } else { | 670 } else { |
723 if (rv > 0) | 671 if (rv > 0) |
724 was_ever_used_ = true; | 672 was_ever_used_ = true; |
725 user_write_buf_ = NULL; | 673 user_write_buf_ = NULL; |
726 user_write_buf_len_ = 0; | 674 user_write_buf_len_ = 0; |
727 if (rv < 0) { | |
728 // Failure of a write attempt may indicate a failed false start | |
729 // connection. | |
730 OnHandshakeCompletion(); | |
731 } | |
732 } | 675 } |
733 | 676 |
734 return rv; | 677 return rv; |
735 } | 678 } |
736 | 679 |
737 int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { | 680 int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { |
738 return transport_->socket()->SetReceiveBufferSize(size); | 681 return transport_->socket()->SetReceiveBufferSize(size); |
739 } | 682 } |
740 | 683 |
741 int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { | 684 int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { |
742 return transport_->socket()->SetSendBufferSize(size); | 685 return transport_->socket()->SetSendBufferSize(size); |
743 } | 686 } |
744 | 687 |
745 int SSLClientSocketOpenSSL::Init() { | 688 int SSLClientSocketOpenSSL::Init() { |
746 DCHECK(!ssl_); | 689 DCHECK(!ssl_); |
747 DCHECK(!transport_bio_); | 690 DCHECK(!transport_bio_); |
748 | 691 |
749 SSLContext* context = SSLContext::GetInstance(); | 692 SSLContext* context = SSLContext::GetInstance(); |
750 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 693 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
751 | 694 |
752 ssl_ = SSL_new(context->ssl_ctx()); | 695 ssl_ = SSL_new(context->ssl_ctx()); |
753 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) | 696 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
754 return ERR_UNEXPECTED; | 697 return ERR_UNEXPECTED; |
755 | 698 |
756 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) | 699 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) |
757 return ERR_UNEXPECTED; | 700 return ERR_UNEXPECTED; |
758 | 701 |
759 // Set an OpenSSL callback to monitor this SSL*'s connection. | |
760 SSL_set_info_callback(ssl_, &InfoCallback); | |
761 | |
762 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( | 702 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( |
763 ssl_, GetSessionCacheKey()); | 703 ssl_, GetSocketSessionCacheKey(*this)); |
764 | 704 |
765 send_buffer_ = new GrowableIOBuffer(); | 705 send_buffer_ = new GrowableIOBuffer(); |
766 send_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); | 706 send_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); |
767 recv_buffer_ = new GrowableIOBuffer(); | 707 recv_buffer_ = new GrowableIOBuffer(); |
768 recv_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); | 708 recv_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); |
769 | 709 |
770 BIO* ssl_bio = NULL; | 710 BIO* ssl_bio = NULL; |
771 | 711 |
772 // SSLClientSocketOpenSSL retains ownership of the BIO buffers. | 712 // SSLClientSocketOpenSSL retains ownership of the BIO buffers. |
773 if (!BIO_new_bio_pair_external_buf( | 713 if (!BIO_new_bio_pair_external_buf( |
774 &ssl_bio, send_buffer_->capacity(), | 714 &ssl_bio, send_buffer_->capacity(), |
775 reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, | 715 reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, |
776 recv_buffer_->capacity(), | 716 recv_buffer_->capacity(), |
777 reinterpret_cast<uint8_t*>(recv_buffer_->data()))) | 717 reinterpret_cast<uint8_t*>(recv_buffer_->data()))) |
778 return ERR_UNEXPECTED; | 718 return ERR_UNEXPECTED; |
779 DCHECK(ssl_bio); | 719 DCHECK(ssl_bio); |
780 DCHECK(transport_bio_); | 720 DCHECK(transport_bio_); |
781 | 721 |
782 // Install a callback on OpenSSL's end to plumb transport errors through. | 722 // Install a callback on OpenSSL's end to plumb transport errors through. |
783 BIO_set_callback(ssl_bio, BIOCallback); | 723 BIO_set_callback(ssl_bio, &SSLClientSocketOpenSSL::BIOCallback); |
784 BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); | 724 BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); |
785 | 725 |
786 SSL_set_bio(ssl_, ssl_bio, ssl_bio); | 726 SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
787 | 727 |
788 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, | 728 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
789 // set everything we care about to an absolute value. | 729 // set everything we care about to an absolute value. |
790 SslSetClearMask options; | 730 SslSetClearMask options; |
791 options.ConfigureFlag(SSL_OP_NO_SSLv2, true); | 731 options.ConfigureFlag(SSL_OP_NO_SSLv2, true); |
792 bool ssl3_enabled = (ssl_config_.version_min == SSL_PROTOCOL_VERSION_SSL3); | 732 bool ssl3_enabled = (ssl_config_.version_min == SSL_PROTOCOL_VERSION_SSL3); |
793 options.ConfigureFlag(SSL_OP_NO_SSLv3, !ssl3_enabled); | 733 options.ConfigureFlag(SSL_OP_NO_SSLv3, !ssl3_enabled); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
910 return OK; | 850 return OK; |
911 } | 851 } |
912 | 852 |
913 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { | 853 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
914 // Since Run may result in Read being called, clear |user_read_callback_| | 854 // Since Run may result in Read being called, clear |user_read_callback_| |
915 // up front. | 855 // up front. |
916 if (rv > 0) | 856 if (rv > 0) |
917 was_ever_used_ = true; | 857 was_ever_used_ = true; |
918 user_read_buf_ = NULL; | 858 user_read_buf_ = NULL; |
919 user_read_buf_len_ = 0; | 859 user_read_buf_len_ = 0; |
920 if (rv <= 0) { | |
921 // Failure of a read attempt may indicate a failed false start | |
922 // connection. | |
923 OnHandshakeCompletion(); | |
924 } | |
925 base::ResetAndReturn(&user_read_callback_).Run(rv); | 860 base::ResetAndReturn(&user_read_callback_).Run(rv); |
926 } | 861 } |
927 | 862 |
928 void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { | 863 void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { |
929 // Since Run may result in Write being called, clear |user_write_callback_| | 864 // Since Run may result in Write being called, clear |user_write_callback_| |
930 // up front. | 865 // up front. |
931 if (rv > 0) | 866 if (rv > 0) |
932 was_ever_used_ = true; | 867 was_ever_used_ = true; |
933 user_write_buf_ = NULL; | 868 user_write_buf_ = NULL; |
934 user_write_buf_len_ = 0; | 869 user_write_buf_len_ = 0; |
935 if (rv < 0) { | |
936 // Failure of a write attempt may indicate a failed false start | |
937 // connection. | |
938 OnHandshakeCompletion(); | |
939 } | |
940 base::ResetAndReturn(&user_write_callback_).Run(rv); | 870 base::ResetAndReturn(&user_write_callback_).Run(rv); |
941 } | 871 } |
942 | 872 |
943 void SSLClientSocketOpenSSL::OnHandshakeCompletion() { | |
944 if (!handshake_completion_callback_.is_null()) | |
945 base::ResetAndReturn(&handshake_completion_callback_).Run(); | |
946 } | |
947 | |
948 bool SSLClientSocketOpenSSL::DoTransportIO() { | 873 bool SSLClientSocketOpenSSL::DoTransportIO() { |
949 bool network_moved = false; | 874 bool network_moved = false; |
950 int rv; | 875 int rv; |
951 // Read and write as much data as possible. The loop is necessary because | 876 // Read and write as much data as possible. The loop is necessary because |
952 // Write() may return synchronously. | 877 // Write() may return synchronously. |
953 do { | 878 do { |
954 rv = BufferSend(); | 879 rv = BufferSend(); |
955 if (rv != ERR_IO_PENDING && rv != 0) | 880 if (rv != ERR_IO_PENDING && rv != 0) |
956 network_moved = true; | 881 network_moved = true; |
957 } while (rv > 0); | 882 } while (rv > 0); |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1252 } | 1177 } |
1253 | 1178 |
1254 if (result == OK) { | 1179 if (result == OK) { |
1255 // Only check Certificate Transparency if there were no other errors with | 1180 // Only check Certificate Transparency if there were no other errors with |
1256 // the connection. | 1181 // the connection. |
1257 VerifyCT(); | 1182 VerifyCT(); |
1258 | 1183 |
1259 // TODO(joth): Work out if we need to remember the intermediate CA certs | 1184 // TODO(joth): Work out if we need to remember the intermediate CA certs |
1260 // when the server sends them to us, and do so here. | 1185 // when the server sends them to us, and do so here. |
1261 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); | 1186 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); |
1262 marked_session_as_good_ = true; | |
1263 CheckIfHandshakeFinished(); | |
1264 } else { | 1187 } else { |
1265 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) | 1188 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) |
1266 << " (" << result << ")"; | 1189 << " (" << result << ")"; |
1267 } | 1190 } |
1268 | 1191 |
1269 completed_connect_ = true; | 1192 completed_handshake_ = true; |
1270 | |
1271 // Exit DoHandshakeLoop and return the result to the caller to Connect. | 1193 // Exit DoHandshakeLoop and return the result to the caller to Connect. |
1272 DCHECK_EQ(STATE_NONE, next_handshake_state_); | 1194 DCHECK_EQ(STATE_NONE, next_handshake_state_); |
1273 return result; | 1195 return result; |
1274 } | 1196 } |
1275 | 1197 |
1276 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { | 1198 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
1277 if (rv < OK) | |
1278 OnHandshakeCompletion(); | |
1279 if (!user_connect_callback_.is_null()) { | 1199 if (!user_connect_callback_.is_null()) { |
1280 CompletionCallback c = user_connect_callback_; | 1200 CompletionCallback c = user_connect_callback_; |
1281 user_connect_callback_.Reset(); | 1201 user_connect_callback_.Reset(); |
1282 c.Run(rv > OK ? OK : rv); | 1202 c.Run(rv > OK ? OK : rv); |
1283 } | 1203 } |
1284 } | 1204 } |
1285 | 1205 |
1286 void SSLClientSocketOpenSSL::UpdateServerCert() { | 1206 void SSLClientSocketOpenSSL::UpdateServerCert() { |
1287 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. | 1207 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
1288 tracked_objects::ScopedTracker tracking_profile( | 1208 tracked_objects::ScopedTracker tracking_profile( |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1479 } | 1399 } |
1480 | 1400 |
1481 bool network_moved = DoTransportIO(); | 1401 bool network_moved = DoTransportIO(); |
1482 if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { | 1402 if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
1483 // In general we exit the loop if rv is ERR_IO_PENDING. In this | 1403 // In general we exit the loop if rv is ERR_IO_PENDING. In this |
1484 // special case we keep looping even if rv is ERR_IO_PENDING because | 1404 // special case we keep looping even if rv is ERR_IO_PENDING because |
1485 // the transport IO may allow DoHandshake to make progress. | 1405 // the transport IO may allow DoHandshake to make progress. |
1486 rv = OK; // This causes us to stay in the loop. | 1406 rv = OK; // This causes us to stay in the loop. |
1487 } | 1407 } |
1488 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); | 1408 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
1489 | |
1490 return rv; | 1409 return rv; |
1491 } | 1410 } |
1492 | 1411 |
1493 int SSLClientSocketOpenSSL::DoReadLoop() { | 1412 int SSLClientSocketOpenSSL::DoReadLoop() { |
1494 bool network_moved; | 1413 bool network_moved; |
1495 int rv; | 1414 int rv; |
1496 do { | 1415 do { |
1497 rv = DoPayloadRead(); | 1416 rv = DoPayloadRead(); |
1498 network_moved = DoTransportIO(); | 1417 network_moved = DoTransportIO(); |
1499 } while (rv == ERR_IO_PENDING && network_moved); | 1418 } while (rv == ERR_IO_PENDING && network_moved); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1600 pending_read_error_info_)); | 1519 pending_read_error_info_)); |
1601 pending_read_ssl_error_ = SSL_ERROR_NONE; | 1520 pending_read_ssl_error_ = SSL_ERROR_NONE; |
1602 pending_read_error_info_ = OpenSSLErrorInfo(); | 1521 pending_read_error_info_ = OpenSSLErrorInfo(); |
1603 } | 1522 } |
1604 return rv; | 1523 return rv; |
1605 } | 1524 } |
1606 | 1525 |
1607 int SSLClientSocketOpenSSL::DoPayloadWrite() { | 1526 int SSLClientSocketOpenSSL::DoPayloadWrite() { |
1608 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1527 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
1609 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); | 1528 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
1529 | |
davidben
2015/03/09 18:02:36
Spurious? (It was probably a spurious change befor
| |
1610 if (rv >= 0) { | 1530 if (rv >= 0) { |
1611 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, | 1531 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
1612 user_write_buf_->data()); | 1532 user_write_buf_->data()); |
1613 return rv; | 1533 return rv; |
1614 } | 1534 } |
1615 | 1535 |
1616 int ssl_error = SSL_get_error(ssl_, rv); | 1536 int ssl_error = SSL_get_error(ssl_, rv); |
1617 OpenSSLErrorInfo error_info; | 1537 OpenSSLErrorInfo error_info; |
1618 int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, | 1538 int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, |
1619 &error_info); | 1539 &error_info); |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1853 NetLog::IntegerCallback("cert_count", 0)); | 1773 NetLog::IntegerCallback("cert_count", 0)); |
1854 return 1; | 1774 return 1; |
1855 } | 1775 } |
1856 | 1776 |
1857 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1777 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
1858 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. | 1778 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
1859 tracked_objects::ScopedTracker tracking_profile( | 1779 tracked_objects::ScopedTracker tracking_profile( |
1860 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1780 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
1861 "424386 SSLClientSocketOpenSSL::CertVerifyCallback")); | 1781 "424386 SSLClientSocketOpenSSL::CertVerifyCallback")); |
1862 | 1782 |
1863 if (!completed_connect_) { | 1783 if (!completed_handshake_) { |
1864 // If the first handshake hasn't completed then we accept any certificates | 1784 // If the first handshake hasn't completed then we accept any certificates |
1865 // because we verify after the handshake. | 1785 // because we verify after the handshake. |
1866 return 1; | 1786 return 1; |
1867 } | 1787 } |
1868 | 1788 |
1869 // Disallow the server certificate to change in a renegotiation. | 1789 // Disallow the server certificate to change in a renegotiation. |
1870 if (server_cert_chain_->empty()) { | 1790 if (server_cert_chain_->empty()) { |
1871 LOG(ERROR) << "Received invalid certificate chain between handshakes"; | 1791 LOG(ERROR) << "Received invalid certificate chain between handshakes"; |
1872 return 0; | 1792 return 0; |
1873 } | 1793 } |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1984 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1904 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
1985 "424386 SSLClientSocketOpenSSL::BIOCallback")); | 1905 "424386 SSLClientSocketOpenSSL::BIOCallback")); |
1986 | 1906 |
1987 SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>( | 1907 SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>( |
1988 BIO_get_callback_arg(bio)); | 1908 BIO_get_callback_arg(bio)); |
1989 CHECK(socket); | 1909 CHECK(socket); |
1990 return socket->MaybeReplayTransportError( | 1910 return socket->MaybeReplayTransportError( |
1991 bio, cmd, argp, argi, argl, retvalue); | 1911 bio, cmd, argp, argi, argl, retvalue); |
1992 } | 1912 } |
1993 | 1913 |
1994 // static | |
1995 void SSLClientSocketOpenSSL::InfoCallback(const SSL* ssl, | |
1996 int type, | |
1997 int /*val*/) { | |
1998 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. | |
1999 tracked_objects::ScopedTracker tracking_profile( | |
2000 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
2001 "424386 SSLClientSocketOpenSSL::InfoCallback")); | |
2002 | |
2003 if (type == SSL_CB_HANDSHAKE_DONE) { | |
2004 SSLClientSocketOpenSSL* ssl_socket = | |
2005 SSLContext::GetInstance()->GetClientSocketFromSSL(ssl); | |
2006 ssl_socket->handshake_succeeded_ = true; | |
2007 ssl_socket->CheckIfHandshakeFinished(); | |
2008 } | |
2009 } | |
2010 | |
2011 // Determines if both the handshake and certificate verification have completed | |
2012 // successfully, and calls the handshake completion callback if that is the | |
2013 // case. | |
2014 // | |
2015 // CheckIfHandshakeFinished is called twice per connection: once after | |
2016 // MarkSSLSessionAsGood, when the certificate has been verified, and | |
2017 // once via an OpenSSL callback when the handshake has completed. On the | |
2018 // second call, when the certificate has been verified and the handshake | |
2019 // has completed, the connection's handshake completion callback is run. | |
2020 void SSLClientSocketOpenSSL::CheckIfHandshakeFinished() { | |
2021 if (handshake_succeeded_ && marked_session_as_good_) | |
2022 OnHandshakeCompletion(); | |
2023 } | |
2024 | |
2025 void SSLClientSocketOpenSSL::AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const { | 1914 void SSLClientSocketOpenSSL::AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
2026 for (ct::SCTList::const_iterator iter = | 1915 for (ct::SCTList::const_iterator iter = |
2027 ct_verify_result_.verified_scts.begin(); | 1916 ct_verify_result_.verified_scts.begin(); |
2028 iter != ct_verify_result_.verified_scts.end(); ++iter) { | 1917 iter != ct_verify_result_.verified_scts.end(); ++iter) { |
2029 ssl_info->signed_certificate_timestamps.push_back( | 1918 ssl_info->signed_certificate_timestamps.push_back( |
2030 SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_OK)); | 1919 SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_OK)); |
2031 } | 1920 } |
2032 for (ct::SCTList::const_iterator iter = | 1921 for (ct::SCTList::const_iterator iter = |
2033 ct_verify_result_.invalid_scts.begin(); | 1922 ct_verify_result_.invalid_scts.begin(); |
2034 iter != ct_verify_result_.invalid_scts.end(); ++iter) { | 1923 iter != ct_verify_result_.invalid_scts.end(); ++iter) { |
2035 ssl_info->signed_certificate_timestamps.push_back( | 1924 ssl_info->signed_certificate_timestamps.push_back( |
2036 SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_INVALID)); | 1925 SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_INVALID)); |
2037 } | 1926 } |
2038 for (ct::SCTList::const_iterator iter = | 1927 for (ct::SCTList::const_iterator iter = |
2039 ct_verify_result_.unknown_logs_scts.begin(); | 1928 ct_verify_result_.unknown_logs_scts.begin(); |
2040 iter != ct_verify_result_.unknown_logs_scts.end(); ++iter) { | 1929 iter != ct_verify_result_.unknown_logs_scts.end(); ++iter) { |
2041 ssl_info->signed_certificate_timestamps.push_back( | 1930 ssl_info->signed_certificate_timestamps.push_back( |
2042 SignedCertificateTimestampAndStatus(*iter, | 1931 SignedCertificateTimestampAndStatus(*iter, |
2043 ct::SCT_STATUS_LOG_UNKNOWN)); | 1932 ct::SCT_STATUS_LOG_UNKNOWN)); |
2044 } | 1933 } |
2045 } | 1934 } |
2046 | 1935 |
2047 scoped_refptr<X509Certificate> | 1936 scoped_refptr<X509Certificate> |
2048 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1937 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
2049 return server_cert_; | 1938 return server_cert_; |
2050 } | 1939 } |
2051 | 1940 |
2052 } // namespace net | 1941 } // namespace net |
OLD | NEW |