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

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

Issue 2400033005: Use BoringSSL scopers in //net. (Closed)
Patch Set: eroman comments Created 4 years, 2 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
« no previous file with comments | « net/socket/ssl_client_socket_impl.h ('k') | net/socket/ssl_client_socket_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_impl.h" 5 #include "net/socket/ssl_client_socket_impl.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <openssl/bio.h> 8 #include <openssl/bio.h>
9 #include <openssl/bytestring.h> 9 #include <openssl/bytestring.h>
10 #include <openssl/err.h> 10 #include <openssl/err.h>
(...skipping 12 matching lines...) Expand all
23 #include "base/metrics/field_trial.h" 23 #include "base/metrics/field_trial.h"
24 #include "base/metrics/histogram_macros.h" 24 #include "base/metrics/histogram_macros.h"
25 #include "base/metrics/sparse_histogram.h" 25 #include "base/metrics/sparse_histogram.h"
26 #include "base/profiler/scoped_tracker.h" 26 #include "base/profiler/scoped_tracker.h"
27 #include "base/strings/string_number_conversions.h" 27 #include "base/strings/string_number_conversions.h"
28 #include "base/strings/string_piece.h" 28 #include "base/strings/string_piece.h"
29 #include "base/synchronization/lock.h" 29 #include "base/synchronization/lock.h"
30 #include "base/threading/thread_local.h" 30 #include "base/threading/thread_local.h"
31 #include "base/trace_event/trace_event.h" 31 #include "base/trace_event/trace_event.h"
32 #include "base/values.h" 32 #include "base/values.h"
33 #include "crypto/auto_cbb.h"
34 #include "crypto/ec_private_key.h" 33 #include "crypto/ec_private_key.h"
35 #include "crypto/openssl_util.h" 34 #include "crypto/openssl_util.h"
36 #include "crypto/scoped_openssl_types.h"
37 #include "net/base/ip_address.h" 35 #include "net/base/ip_address.h"
38 #include "net/base/net_errors.h" 36 #include "net/base/net_errors.h"
39 #include "net/cert/cert_verifier.h" 37 #include "net/cert/cert_verifier.h"
40 #include "net/cert/ct_ev_whitelist.h" 38 #include "net/cert/ct_ev_whitelist.h"
41 #include "net/cert/ct_policy_enforcer.h" 39 #include "net/cert/ct_policy_enforcer.h"
42 #include "net/cert/ct_policy_status.h" 40 #include "net/cert/ct_policy_status.h"
43 #include "net/cert/ct_verifier.h" 41 #include "net/cert/ct_verifier.h"
44 #include "net/cert/x509_certificate_net_log_param.h" 42 #include "net/cert/x509_certificate_net_log_param.h"
45 #include "net/cert/x509_util_openssl.h" 43 #include "net/cert/x509_util_openssl.h"
46 #include "net/http/transport_security_state.h" 44 #include "net/http/transport_security_state.h"
47 #include "net/log/net_log.h" 45 #include "net/log/net_log.h"
48 #include "net/log/net_log_event_type.h" 46 #include "net/log/net_log_event_type.h"
49 #include "net/log/net_log_parameters_callback.h" 47 #include "net/log/net_log_parameters_callback.h"
50 #include "net/ssl/scoped_openssl_types.h"
51 #include "net/ssl/ssl_cert_request_info.h" 48 #include "net/ssl/ssl_cert_request_info.h"
52 #include "net/ssl/ssl_cipher_suite_names.h" 49 #include "net/ssl/ssl_cipher_suite_names.h"
53 #include "net/ssl/ssl_client_session_cache.h" 50 #include "net/ssl/ssl_client_session_cache.h"
54 #include "net/ssl/ssl_connection_status_flags.h" 51 #include "net/ssl/ssl_connection_status_flags.h"
55 #include "net/ssl/ssl_info.h" 52 #include "net/ssl/ssl_info.h"
56 #include "net/ssl/ssl_private_key.h" 53 #include "net/ssl/ssl_private_key.h"
57 #include "net/ssl/token_binding.h" 54 #include "net/ssl/token_binding.h"
58 55
59 #if !defined(OS_NACL) 56 #if !defined(OS_NACL)
60 #include "net/ssl/ssl_key_logger.h" 57 #include "net/ssl/ssl_key_logger.h"
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 #if !defined(OS_NACL) 348 #if !defined(OS_NACL)
352 static void KeyLogCallback(const SSL* ssl, const char* line) { 349 static void KeyLogCallback(const SSL* ssl, const char* line) {
353 GetInstance()->ssl_key_logger_->WriteLine(line); 350 GetInstance()->ssl_key_logger_->WriteLine(line);
354 } 351 }
355 #endif 352 #endif
356 353
357 // This is the index used with SSL_get_ex_data to retrieve the owner 354 // This is the index used with SSL_get_ex_data to retrieve the owner
358 // SSLClientSocketImpl object from an SSL instance. 355 // SSLClientSocketImpl object from an SSL instance.
359 int ssl_socket_data_index_; 356 int ssl_socket_data_index_;
360 357
361 ScopedSSL_CTX ssl_ctx_; 358 bssl::UniquePtr<SSL_CTX> ssl_ctx_;
362 359
363 #if !defined(OS_NACL) 360 #if !defined(OS_NACL)
364 std::unique_ptr<SSLKeyLogger> ssl_key_logger_; 361 std::unique_ptr<SSLKeyLogger> ssl_key_logger_;
365 #endif 362 #endif
366 363
367 // TODO(davidben): Use a separate cache per URLRequestContext. 364 // TODO(davidben): Use a separate cache per URLRequestContext.
368 // https://crbug.com/458365 365 // https://crbug.com/458365
369 // 366 //
370 // TODO(davidben): Sessions should be invalidated on fatal 367 // TODO(davidben): Sessions should be invalidated on fatal
371 // alerts. https://crbug.com/466352 368 // alerts. https://crbug.com/466352
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 } 407 }
411 408
412 bool empty() const { return size() == 0; } 409 bool empty() const { return size() == 0; }
413 410
414 X509* Get(size_t index) const { 411 X509* Get(size_t index) const {
415 DCHECK_LT(index, size()); 412 DCHECK_LT(index, size());
416 return sk_X509_value(openssl_chain_.get(), index); 413 return sk_X509_value(openssl_chain_.get(), index);
417 } 414 }
418 415
419 private: 416 private:
420 ScopedX509Stack openssl_chain_; 417 bssl::UniquePtr<STACK_OF(X509)> openssl_chain_;
421 }; 418 };
422 419
423 SSLClientSocketImpl::PeerCertificateChain& 420 SSLClientSocketImpl::PeerCertificateChain&
424 SSLClientSocketImpl::PeerCertificateChain::operator=( 421 SSLClientSocketImpl::PeerCertificateChain::operator=(
425 const PeerCertificateChain& other) { 422 const PeerCertificateChain& other) {
426 if (this == &other) 423 if (this == &other)
427 return *this; 424 return *this;
428 425
429 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); 426 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get()));
430 return *this; 427 return *this;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 transport_write_error_(OK), 482 transport_write_error_(OK),
486 server_cert_chain_(new PeerCertificateChain(NULL)), 483 server_cert_chain_(new PeerCertificateChain(NULL)),
487 completed_connect_(false), 484 completed_connect_(false),
488 was_ever_used_(false), 485 was_ever_used_(false),
489 cert_verifier_(context.cert_verifier), 486 cert_verifier_(context.cert_verifier),
490 cert_transparency_verifier_(context.cert_transparency_verifier), 487 cert_transparency_verifier_(context.cert_transparency_verifier),
491 channel_id_service_(context.channel_id_service), 488 channel_id_service_(context.channel_id_service),
492 tb_was_negotiated_(false), 489 tb_was_negotiated_(false),
493 tb_negotiated_param_(TB_PARAM_ECDSAP256), 490 tb_negotiated_param_(TB_PARAM_ECDSAP256),
494 tb_signature_map_(10), 491 tb_signature_map_(10),
495 ssl_(NULL),
496 transport_bio_(NULL),
497 transport_(std::move(transport_socket)), 492 transport_(std::move(transport_socket)),
498 host_and_port_(host_and_port), 493 host_and_port_(host_and_port),
499 ssl_config_(ssl_config), 494 ssl_config_(ssl_config),
500 ssl_session_cache_shard_(context.ssl_session_cache_shard), 495 ssl_session_cache_shard_(context.ssl_session_cache_shard),
501 next_handshake_state_(STATE_NONE), 496 next_handshake_state_(STATE_NONE),
502 disconnected_(false), 497 disconnected_(false),
503 negotiated_protocol_(kProtoUnknown), 498 negotiated_protocol_(kProtoUnknown),
504 channel_id_sent_(false), 499 channel_id_sent_(false),
505 certificate_verified_(false), 500 certificate_verified_(false),
506 certificate_requested_(false), 501 certificate_requested_(false),
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 if (!key->ExportRawPublicKey(&raw_public_key)) 543 if (!key->ExportRawPublicKey(&raw_public_key))
549 return ERR_FAILED; 544 return ERR_FAILED;
550 auto it = tb_signature_map_.Get(std::make_pair(tb_type, raw_public_key)); 545 auto it = tb_signature_map_.Get(std::make_pair(tb_type, raw_public_key));
551 if (it != tb_signature_map_.end()) { 546 if (it != tb_signature_map_.end()) {
552 *out = it->second; 547 *out = it->second;
553 return OK; 548 return OK;
554 } 549 }
555 550
556 uint8_t tb_ekm_buf[32]; 551 uint8_t tb_ekm_buf[32];
557 static const char kTokenBindingExporterLabel[] = "EXPORTER-Token-Binding"; 552 static const char kTokenBindingExporterLabel[] = "EXPORTER-Token-Binding";
558 if (!SSL_export_keying_material(ssl_, tb_ekm_buf, sizeof(tb_ekm_buf), 553 if (!SSL_export_keying_material(ssl_.get(), tb_ekm_buf, sizeof(tb_ekm_buf),
559 kTokenBindingExporterLabel, 554 kTokenBindingExporterLabel,
560 strlen(kTokenBindingExporterLabel), nullptr, 555 strlen(kTokenBindingExporterLabel), nullptr,
561 0, false /* no context */)) { 556 0, false /* no context */)) {
562 return ERR_FAILED; 557 return ERR_FAILED;
563 } 558 }
564 559
565 if (!CreateTokenBindingSignature( 560 if (!CreateTokenBindingSignature(
566 base::StringPiece(reinterpret_cast<char*>(tb_ekm_buf), 561 base::StringPiece(reinterpret_cast<char*>(tb_ekm_buf),
567 sizeof(tb_ekm_buf)), 562 sizeof(tb_ekm_buf)),
568 tb_type, key, out)) 563 tb_type, key, out))
(...skipping 11 matching lines...) Expand all
580 bool has_context, 575 bool has_context,
581 const base::StringPiece& context, 576 const base::StringPiece& context,
582 unsigned char* out, 577 unsigned char* out,
583 unsigned int outlen) { 578 unsigned int outlen) {
584 if (!IsConnected()) 579 if (!IsConnected())
585 return ERR_SOCKET_NOT_CONNECTED; 580 return ERR_SOCKET_NOT_CONNECTED;
586 581
587 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 582 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
588 583
589 if (!SSL_export_keying_material( 584 if (!SSL_export_keying_material(
590 ssl_, out, outlen, label.data(), label.size(), 585 ssl_.get(), out, outlen, label.data(), label.size(),
591 reinterpret_cast<const unsigned char*>(context.data()), 586 reinterpret_cast<const unsigned char*>(context.data()),
592 context.length(), has_context ? 1 : 0)) { 587 context.length(), has_context ? 1 : 0)) {
593 LOG(ERROR) << "Failed to export keying material."; 588 LOG(ERROR) << "Failed to export keying material.";
594 return ERR_FAILED; 589 return ERR_FAILED;
595 } 590 }
596 591
597 return OK; 592 return OK;
598 } 593 }
599 594
600 int SSLClientSocketImpl::Connect(const CompletionCallback& callback) { 595 int SSLClientSocketImpl::Connect(const CompletionCallback& callback) {
601 // Although StreamSocket does allow calling Connect() after Disconnect(), 596 // Although StreamSocket does allow calling Connect() after Disconnect(),
602 // this has never worked for layered sockets. CHECK to detect any consumers 597 // this has never worked for layered sockets. CHECK to detect any consumers
603 // reconnecting an SSL socket. 598 // reconnecting an SSL socket.
604 // 599 //
605 // TODO(davidben,mmenke): Remove this API feature. See 600 // TODO(davidben,mmenke): Remove this API feature. See
606 // https://crbug.com/499289. 601 // https://crbug.com/499289.
607 CHECK(!disconnected_); 602 CHECK(!disconnected_);
608 603
609 net_log_.BeginEvent(NetLogEventType::SSL_CONNECT); 604 net_log_.BeginEvent(NetLogEventType::SSL_CONNECT);
610 605
611 // Set up new ssl object. 606 // Set up new ssl object.
612 int rv = Init(); 607 int rv = Init();
613 if (rv != OK) { 608 if (rv != OK) {
614 LogConnectEndEvent(rv); 609 LogConnectEndEvent(rv);
615 return rv; 610 return rv;
616 } 611 }
617 612
618 // Set SSL to client mode. Handshake happens in the loop below. 613 // Set SSL to client mode. Handshake happens in the loop below.
619 SSL_set_connect_state(ssl_); 614 SSL_set_connect_state(ssl_.get());
620 615
621 next_handshake_state_ = STATE_HANDSHAKE; 616 next_handshake_state_ = STATE_HANDSHAKE;
622 rv = DoHandshakeLoop(OK); 617 rv = DoHandshakeLoop(OK);
623 if (rv == ERR_IO_PENDING) { 618 if (rv == ERR_IO_PENDING) {
624 user_connect_callback_ = callback; 619 user_connect_callback_ = callback;
625 } else { 620 } else {
626 LogConnectEndEvent(rv); 621 LogConnectEndEvent(rv);
627 } 622 }
628 623
629 return rv > OK ? OK : rv; 624 return rv > OK ? OK : rv;
630 } 625 }
631 626
632 void SSLClientSocketImpl::Disconnect() { 627 void SSLClientSocketImpl::Disconnect() {
633 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); 628 crypto::OpenSSLErrStackTracer tracer(FROM_HERE);
634 629
635 if (ssl_) { 630 if (ssl_) {
636 // Calling SSL_shutdown prevents the session from being marked as 631 // Calling SSL_shutdown prevents the session from being marked as
637 // unresumable. 632 // unresumable.
638 SSL_shutdown(ssl_); 633 SSL_shutdown(ssl_.get());
639 SSL_free(ssl_); 634 ssl_.reset();
640 ssl_ = NULL;
641 } 635 }
642 if (transport_bio_) { 636 transport_bio_.reset();
643 BIO_free_all(transport_bio_);
644 transport_bio_ = NULL;
645 }
646 637
647 disconnected_ = true; 638 disconnected_ = true;
648 639
649 // Shut down anything that may call us back. 640 // Shut down anything that may call us back.
650 cert_verifier_request_.reset(); 641 cert_verifier_request_.reset();
651 transport_->socket()->Disconnect(); 642 transport_->socket()->Disconnect();
652 643
653 // Null all callbacks, delete all buffers. 644 // Null all callbacks, delete all buffers.
654 transport_send_busy_ = false; 645 transport_send_busy_ = false;
655 send_buffer_ = NULL; 646 send_buffer_ = NULL;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 if (user_read_buf_.get() || user_write_buf_.get()) 702 if (user_read_buf_.get() || user_write_buf_.get())
712 return false; 703 return false;
713 704
714 // If there is data read from the network that has not yet been consumed, do 705 // If there is data read from the network that has not yet been consumed, do
715 // not treat the connection as idle. 706 // not treat the connection as idle.
716 // 707 //
717 // Note that this does not check |BIO_pending|, whether there is ciphertext 708 // Note that this does not check |BIO_pending|, whether there is ciphertext
718 // that has not yet been flushed to the network. |Write| returns early, so 709 // that has not yet been flushed to the network. |Write| returns early, so
719 // this can cause race conditions which cause a socket to not be treated 710 // this can cause race conditions which cause a socket to not be treated
720 // reusable when it should be. See https://crbug.com/466147. 711 // reusable when it should be. See https://crbug.com/466147.
721 if (BIO_wpending(transport_bio_) > 0) 712 if (BIO_wpending(transport_bio_.get()) > 0)
722 return false; 713 return false;
723 714
724 return transport_->socket()->IsConnectedAndIdle(); 715 return transport_->socket()->IsConnectedAndIdle();
725 } 716 }
726 717
727 int SSLClientSocketImpl::GetPeerAddress(IPEndPoint* addressList) const { 718 int SSLClientSocketImpl::GetPeerAddress(IPEndPoint* addressList) const {
728 return transport_->socket()->GetPeerAddress(addressList); 719 return transport_->socket()->GetPeerAddress(addressList);
729 } 720 }
730 721
731 int SSLClientSocketImpl::GetLocalAddress(IPEndPoint* addressList) const { 722 int SSLClientSocketImpl::GetLocalAddress(IPEndPoint* addressList) const {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 ssl_info->client_cert_sent = 770 ssl_info->client_cert_sent =
780 ssl_config_.send_client_cert && ssl_config_.client_cert.get(); 771 ssl_config_.send_client_cert && ssl_config_.client_cert.get();
781 ssl_info->channel_id_sent = channel_id_sent_; 772 ssl_info->channel_id_sent = channel_id_sent_;
782 ssl_info->token_binding_negotiated = tb_was_negotiated_; 773 ssl_info->token_binding_negotiated = tb_was_negotiated_;
783 ssl_info->token_binding_key_param = tb_negotiated_param_; 774 ssl_info->token_binding_key_param = tb_negotiated_param_;
784 ssl_info->pinning_failure_log = pinning_failure_log_; 775 ssl_info->pinning_failure_log = pinning_failure_log_;
785 ssl_info->ocsp_result = server_cert_verify_result_.ocsp_result; 776 ssl_info->ocsp_result = server_cert_verify_result_.ocsp_result;
786 777
787 AddCTInfoToSSLInfo(ssl_info); 778 AddCTInfoToSSLInfo(ssl_info);
788 779
789 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); 780 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_.get());
790 CHECK(cipher); 781 CHECK(cipher);
791 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); 782 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL);
792 // Historically, the "group" was known as "curve". 783 // Historically, the "group" was known as "curve".
793 ssl_info->key_exchange_group = SSL_get_curve_id(ssl_); 784 ssl_info->key_exchange_group = SSL_get_curve_id(ssl_.get());
794 785
795 SSLConnectionStatusSetCipherSuite( 786 SSLConnectionStatusSetCipherSuite(
796 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), 787 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)),
797 &ssl_info->connection_status); 788 &ssl_info->connection_status);
798 SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_), 789 SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_.get()),
799 &ssl_info->connection_status); 790 &ssl_info->connection_status);
800 791
801 if (!SSL_get_secure_renegotiation_support(ssl_)) 792 if (!SSL_get_secure_renegotiation_support(ssl_.get()))
802 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; 793 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION;
803 794
804 ssl_info->handshake_type = SSL_session_reused(ssl_) 795 ssl_info->handshake_type = SSL_session_reused(ssl_.get())
805 ? SSLInfo::HANDSHAKE_RESUME 796 ? SSLInfo::HANDSHAKE_RESUME
806 : SSLInfo::HANDSHAKE_FULL; 797 : SSLInfo::HANDSHAKE_FULL;
807 798
808 return true; 799 return true;
809 } 800 }
810 801
811 void SSLClientSocketImpl::GetConnectionAttempts(ConnectionAttempts* out) const { 802 void SSLClientSocketImpl::GetConnectionAttempts(ConnectionAttempts* out) const {
812 out->clear(); 803 out->clear();
813 } 804 }
814 805
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 if (ssl_config_.cert_io_enabled) { 863 if (ssl_config_.cert_io_enabled) {
873 // TODO(davidben): Move this out of SSLClientSocket. See 864 // TODO(davidben): Move this out of SSLClientSocket. See
874 // https://crbug.com/539520. 865 // https://crbug.com/539520.
875 EnsureNSSHttpIOInit(); 866 EnsureNSSHttpIOInit();
876 } 867 }
877 #endif 868 #endif
878 869
879 SSLContext* context = SSLContext::GetInstance(); 870 SSLContext* context = SSLContext::GetInstance();
880 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 871 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
881 872
882 ssl_ = SSL_new(context->ssl_ctx()); 873 ssl_.reset(SSL_new(context->ssl_ctx()));
883 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) 874 if (!ssl_ || !context->SetClientSocketForSSL(ssl_.get(), this))
884 return ERR_UNEXPECTED; 875 return ERR_UNEXPECTED;
885 876
886 // SNI should only contain valid DNS hostnames, not IP addresses (see RFC 877 // SNI should only contain valid DNS hostnames, not IP addresses (see RFC
887 // 6066, Section 3). 878 // 6066, Section 3).
888 // 879 //
889 // TODO(rsleevi): Should this code allow hostnames that violate the LDH rule? 880 // TODO(rsleevi): Should this code allow hostnames that violate the LDH rule?
890 // See https://crbug.com/496472 and https://crbug.com/496468 for discussion. 881 // See https://crbug.com/496472 and https://crbug.com/496468 for discussion.
891 IPAddress unused; 882 IPAddress unused;
892 if (!unused.AssignFromIPLiteral(host_and_port_.host()) && 883 if (!unused.AssignFromIPLiteral(host_and_port_.host()) &&
893 !SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) { 884 !SSL_set_tlsext_host_name(ssl_.get(), host_and_port_.host().c_str())) {
894 return ERR_UNEXPECTED; 885 return ERR_UNEXPECTED;
895 } 886 }
896 887
897 ScopedSSL_SESSION session = 888 bssl::UniquePtr<SSL_SESSION> session =
898 context->session_cache()->Lookup(GetSessionCacheKey()); 889 context->session_cache()->Lookup(GetSessionCacheKey());
899 if (session) 890 if (session)
900 SSL_set_session(ssl_, session.get()); 891 SSL_set_session(ssl_.get(), session.get());
901 892
902 // Get read and write buffer sizes from field trials, if possible. If values 893 // Get read and write buffer sizes from field trials, if possible. If values
903 // not present, use default. Also make sure values are in reasonable range. 894 // not present, use default. Also make sure values are in reasonable range.
904 int send_buffer_size = kDefaultOpenSSLBufferSize; 895 int send_buffer_size = kDefaultOpenSSLBufferSize;
905 #if !defined(OS_NACL) 896 #if !defined(OS_NACL)
906 int override_send_buffer_size; 897 int override_send_buffer_size;
907 if (base::StringToInt(base::FieldTrialList::FindFullName("SSLBufferSizeSend"), 898 if (base::StringToInt(base::FieldTrialList::FindFullName("SSLBufferSizeSend"),
908 &override_send_buffer_size)) { 899 &override_send_buffer_size)) {
909 send_buffer_size = override_send_buffer_size; 900 send_buffer_size = override_send_buffer_size;
910 send_buffer_size = std::max(send_buffer_size, 1000); 901 send_buffer_size = std::max(send_buffer_size, 1000);
(...skipping 14 matching lines...) Expand all
925 recv_buffer_size = 916 recv_buffer_size =
926 std::min(recv_buffer_size, 2 * kDefaultOpenSSLBufferSize); 917 std::min(recv_buffer_size, 2 * kDefaultOpenSSLBufferSize);
927 } 918 }
928 #endif // !defined(OS_NACL) 919 #endif // !defined(OS_NACL)
929 recv_buffer_ = new GrowableIOBuffer(); 920 recv_buffer_ = new GrowableIOBuffer();
930 recv_buffer_->SetCapacity(recv_buffer_size); 921 recv_buffer_->SetCapacity(recv_buffer_size);
931 922
932 BIO* ssl_bio = NULL; 923 BIO* ssl_bio = NULL;
933 924
934 // SSLClientSocketImpl retains ownership of the BIO buffers. 925 // SSLClientSocketImpl retains ownership of the BIO buffers.
926 BIO* transport_bio_raw;
935 if (!BIO_new_bio_pair_external_buf( 927 if (!BIO_new_bio_pair_external_buf(
936 &ssl_bio, send_buffer_->capacity(), 928 &ssl_bio, send_buffer_->capacity(),
937 reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, 929 reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_raw,
938 recv_buffer_->capacity(), 930 recv_buffer_->capacity(),
939 reinterpret_cast<uint8_t*>(recv_buffer_->data()))) 931 reinterpret_cast<uint8_t*>(recv_buffer_->data())))
940 return ERR_UNEXPECTED; 932 return ERR_UNEXPECTED;
933 transport_bio_.reset(transport_bio_raw);
941 DCHECK(ssl_bio); 934 DCHECK(ssl_bio);
942 DCHECK(transport_bio_); 935 DCHECK(transport_bio_);
943 936
944 // Install a callback on OpenSSL's end to plumb transport errors through. 937 // Install a callback on OpenSSL's end to plumb transport errors through.
945 BIO_set_callback(ssl_bio, &SSLClientSocketImpl::BIOCallback); 938 BIO_set_callback(ssl_bio, &SSLClientSocketImpl::BIOCallback);
946 BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); 939 BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this));
947 940
948 SSL_set_bio(ssl_, ssl_bio, ssl_bio); 941 SSL_set_bio(ssl_.get(), ssl_bio, ssl_bio);
949 942
950 DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); 943 DCHECK_LT(SSL3_VERSION, ssl_config_.version_min);
951 DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); 944 DCHECK_LT(SSL3_VERSION, ssl_config_.version_max);
952 if (!SSL_set_min_proto_version(ssl_, ssl_config_.version_min) || 945 if (!SSL_set_min_proto_version(ssl_.get(), ssl_config_.version_min) ||
953 !SSL_set_max_proto_version(ssl_, ssl_config_.version_max)) { 946 !SSL_set_max_proto_version(ssl_.get(), ssl_config_.version_max)) {
954 return ERR_UNEXPECTED; 947 return ERR_UNEXPECTED;
955 } 948 }
956 949
957 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, 950 // OpenSSL defaults some options to on, others to off. To avoid ambiguity,
958 // set everything we care about to an absolute value. 951 // set everything we care about to an absolute value.
959 SslSetClearMask options; 952 SslSetClearMask options;
960 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); 953 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true);
961 954
962 // TODO(joth): Set this conditionally, see http://crbug.com/55410 955 // TODO(joth): Set this conditionally, see http://crbug.com/55410
963 options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); 956 options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true);
964 957
965 SSL_set_options(ssl_, options.set_mask); 958 SSL_set_options(ssl_.get(), options.set_mask);
966 SSL_clear_options(ssl_, options.clear_mask); 959 SSL_clear_options(ssl_.get(), options.clear_mask);
967 960
968 // Same as above, this time for the SSL mode. 961 // Same as above, this time for the SSL mode.
969 SslSetClearMask mode; 962 SslSetClearMask mode;
970 963
971 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); 964 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true);
972 mode.ConfigureFlag(SSL_MODE_CBC_RECORD_SPLITTING, true); 965 mode.ConfigureFlag(SSL_MODE_CBC_RECORD_SPLITTING, true);
973 966
974 mode.ConfigureFlag(SSL_MODE_ENABLE_FALSE_START, 967 mode.ConfigureFlag(SSL_MODE_ENABLE_FALSE_START,
975 ssl_config_.false_start_enabled); 968 ssl_config_.false_start_enabled);
976 969
977 SSL_set_mode(ssl_, mode.set_mask); 970 SSL_set_mode(ssl_.get(), mode.set_mask);
978 SSL_clear_mode(ssl_, mode.clear_mask); 971 SSL_clear_mode(ssl_.get(), mode.clear_mask);
979 972
980 // Use BoringSSL defaults, but disable HMAC-SHA256 and HMAC-SHA384 ciphers 973 // Use BoringSSL defaults, but disable HMAC-SHA256 and HMAC-SHA384 ciphers
981 // (note that SHA256 and SHA384 only select legacy CBC ciphers). Also disable 974 // (note that SHA256 and SHA384 only select legacy CBC ciphers). Also disable
982 // DHE_RSA_WITH_AES_256_GCM_SHA384. Historically, AES_256_GCM was not 975 // DHE_RSA_WITH_AES_256_GCM_SHA384. Historically, AES_256_GCM was not
983 // supported. As DHE is being deprecated, don't add a cipher only to remove it 976 // supported. As DHE is being deprecated, don't add a cipher only to remove it
984 // immediately. 977 // immediately.
985 std::string command; 978 std::string command;
986 if (SSLClientSocket::IsPostQuantumExperimentEnabled()) { 979 if (SSLClientSocket::IsPostQuantumExperimentEnabled()) {
987 // These are experimental, non-standard ciphersuites. They are part of an 980 // These are experimental, non-standard ciphersuites. They are part of an
988 // experiment in post-quantum cryptography. They're not intended to 981 // experiment in post-quantum cryptography. They're not intended to
(...skipping 25 matching lines...) Expand all
1014 1007
1015 // Remove any disabled ciphers. 1008 // Remove any disabled ciphers.
1016 for (uint16_t id : ssl_config_.disabled_cipher_suites) { 1009 for (uint16_t id : ssl_config_.disabled_cipher_suites) {
1017 const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); 1010 const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id);
1018 if (cipher) { 1011 if (cipher) {
1019 command.append(":!"); 1012 command.append(":!");
1020 command.append(SSL_CIPHER_get_name(cipher)); 1013 command.append(SSL_CIPHER_get_name(cipher));
1021 } 1014 }
1022 } 1015 }
1023 1016
1024 int rv = SSL_set_cipher_list(ssl_, command.c_str()); 1017 int rv = SSL_set_cipher_list(ssl_.get(), command.c_str());
1025 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. 1018 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL.
1026 // This will almost certainly result in the socket failing to complete the 1019 // This will almost certainly result in the socket failing to complete the
1027 // handshake at which point the appropriate error is bubbled up to the client. 1020 // handshake at which point the appropriate error is bubbled up to the client.
1028 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " 1021 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') "
1029 "returned " 1022 "returned "
1030 << rv; 1023 << rv;
1031 1024
1032 // TLS channel ids. 1025 // TLS channel ids.
1033 if (IsChannelIDEnabled()) { 1026 if (IsChannelIDEnabled()) {
1034 SSL_enable_tls_channel_id(ssl_); 1027 SSL_enable_tls_channel_id(ssl_.get());
1035 } 1028 }
1036 1029
1037 if (!ssl_config_.alpn_protos.empty()) { 1030 if (!ssl_config_.alpn_protos.empty()) {
1038 std::vector<uint8_t> wire_protos = 1031 std::vector<uint8_t> wire_protos =
1039 SerializeNextProtos(ssl_config_.alpn_protos); 1032 SerializeNextProtos(ssl_config_.alpn_protos);
1040 SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], 1033 SSL_set_alpn_protos(ssl_.get(),
1034 wire_protos.empty() ? NULL : &wire_protos[0],
1041 wire_protos.size()); 1035 wire_protos.size());
1042 } 1036 }
1043 1037
1044 if (ssl_config_.signed_cert_timestamps_enabled) { 1038 if (ssl_config_.signed_cert_timestamps_enabled) {
1045 SSL_enable_signed_cert_timestamps(ssl_); 1039 SSL_enable_signed_cert_timestamps(ssl_.get());
1046 SSL_enable_ocsp_stapling(ssl_); 1040 SSL_enable_ocsp_stapling(ssl_.get());
1047 } 1041 }
1048 1042
1049 if (cert_verifier_->SupportsOCSPStapling()) 1043 if (cert_verifier_->SupportsOCSPStapling())
1050 SSL_enable_ocsp_stapling(ssl_); 1044 SSL_enable_ocsp_stapling(ssl_.get());
1051 1045
1052 return OK; 1046 return OK;
1053 } 1047 }
1054 1048
1055 void SSLClientSocketImpl::DoReadCallback(int rv) { 1049 void SSLClientSocketImpl::DoReadCallback(int rv) {
1056 // Since Run may result in Read being called, clear |user_read_callback_| 1050 // Since Run may result in Read being called, clear |user_read_callback_|
1057 // up front. 1051 // up front.
1058 if (rv > 0) 1052 if (rv > 0)
1059 was_ever_used_ = true; 1053 was_ever_used_ = true;
1060 user_read_buf_ = NULL; 1054 user_read_buf_ = NULL;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 LAZY_INSTANCE_INITIALIZER; 1087 LAZY_INSTANCE_INITIALIZER;
1094 1088
1095 int SSLClientSocketImpl::DoHandshake() { 1089 int SSLClientSocketImpl::DoHandshake() {
1096 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1090 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1097 1091
1098 int rv; 1092 int rv;
1099 1093
1100 // TODO(cbentzel): Leave only 1 call to SSL_do_handshake once crbug.com/424386 1094 // TODO(cbentzel): Leave only 1 call to SSL_do_handshake once crbug.com/424386
1101 // is fixed. 1095 // is fixed.
1102 if (ssl_config_.send_client_cert && ssl_config_.client_cert.get()) { 1096 if (ssl_config_.send_client_cert && ssl_config_.client_cert.get()) {
1103 rv = SSL_do_handshake(ssl_); 1097 rv = SSL_do_handshake(ssl_.get());
1104 } else { 1098 } else {
1105 if (g_first_run_completed.Get().Get()) { 1099 if (g_first_run_completed.Get().Get()) {
1106 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/424386 is 1100 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/424386 is
1107 // fixed. 1101 // fixed.
1108 tracked_objects::ScopedTracker tracking_profile( 1102 tracked_objects::ScopedTracker tracking_profile(
1109 FROM_HERE_WITH_EXPLICIT_FUNCTION("424386 SSL_do_handshake()")); 1103 FROM_HERE_WITH_EXPLICIT_FUNCTION("424386 SSL_do_handshake()"));
1110 1104
1111 rv = SSL_do_handshake(ssl_); 1105 rv = SSL_do_handshake(ssl_.get());
1112 } else { 1106 } else {
1113 g_first_run_completed.Get().Set(true); 1107 g_first_run_completed.Get().Set(true);
1114 rv = SSL_do_handshake(ssl_); 1108 rv = SSL_do_handshake(ssl_.get());
1115 } 1109 }
1116 } 1110 }
1117 1111
1118 int net_error = OK; 1112 int net_error = OK;
1119 if (rv <= 0) { 1113 if (rv <= 0) {
1120 int ssl_error = SSL_get_error(ssl_, rv); 1114 int ssl_error = SSL_get_error(ssl_.get(), rv);
1121 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { 1115 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) {
1122 // The server supports channel ID. Stop to look one up before returning to 1116 // The server supports channel ID. Stop to look one up before returning to
1123 // the handshake. 1117 // the handshake.
1124 next_handshake_state_ = STATE_CHANNEL_ID_LOOKUP; 1118 next_handshake_state_ = STATE_CHANNEL_ID_LOOKUP;
1125 return OK; 1119 return OK;
1126 } 1120 }
1127 if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && 1121 if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP &&
1128 !ssl_config_.send_client_cert) { 1122 !ssl_config_.send_client_cert) {
1129 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; 1123 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED;
1130 } 1124 }
(...skipping 26 matching lines...) Expand all
1157 int SSLClientSocketImpl::DoHandshakeComplete(int result) { 1151 int SSLClientSocketImpl::DoHandshakeComplete(int result) {
1158 if (result < 0) 1152 if (result < 0)
1159 return result; 1153 return result;
1160 1154
1161 // DHE is offered on the deprecated cipher fallback and then rejected 1155 // DHE is offered on the deprecated cipher fallback and then rejected
1162 // afterwards. This is to aid in diagnosing connection failures because a 1156 // afterwards. This is to aid in diagnosing connection failures because a
1163 // server requires DHE ciphers. 1157 // server requires DHE ciphers.
1164 // 1158 //
1165 // TODO(davidben): A few releases after DHE's removal, remove this logic. 1159 // TODO(davidben): A few releases after DHE's removal, remove this logic.
1166 if (!ssl_config_.dhe_enabled && 1160 if (!ssl_config_.dhe_enabled &&
1167 SSL_CIPHER_is_DHE(SSL_get_current_cipher(ssl_))) { 1161 SSL_CIPHER_is_DHE(SSL_get_current_cipher(ssl_.get()))) {
1168 return ERR_SSL_OBSOLETE_CIPHER; 1162 return ERR_SSL_OBSOLETE_CIPHER;
1169 } 1163 }
1170 1164
1171 // Check that if token binding was negotiated, then extended master secret 1165 // Check that if token binding was negotiated, then extended master secret
1172 // and renegotiation indication must also be negotiated. 1166 // and renegotiation indication must also be negotiated.
1173 if (tb_was_negotiated_ && 1167 if (tb_was_negotiated_ &&
1174 !(SSL_get_extms_support(ssl_) && 1168 !(SSL_get_extms_support(ssl_.get()) &&
1175 SSL_get_secure_renegotiation_support(ssl_))) { 1169 SSL_get_secure_renegotiation_support(ssl_.get()))) {
1176 return ERR_SSL_PROTOCOL_ERROR; 1170 return ERR_SSL_PROTOCOL_ERROR;
1177 } 1171 }
1178 1172
1179 const uint8_t* alpn_proto = NULL; 1173 const uint8_t* alpn_proto = NULL;
1180 unsigned alpn_len = 0; 1174 unsigned alpn_len = 0;
1181 SSL_get0_alpn_selected(ssl_, &alpn_proto, &alpn_len); 1175 SSL_get0_alpn_selected(ssl_.get(), &alpn_proto, &alpn_len);
1182 if (alpn_len > 0) { 1176 if (alpn_len > 0) {
1183 base::StringPiece proto(reinterpret_cast<const char*>(alpn_proto), 1177 base::StringPiece proto(reinterpret_cast<const char*>(alpn_proto),
1184 alpn_len); 1178 alpn_len);
1185 negotiated_protocol_ = NextProtoFromString(proto); 1179 negotiated_protocol_ = NextProtoFromString(proto);
1186 } 1180 }
1187 1181
1188 RecordNegotiatedProtocol(); 1182 RecordNegotiatedProtocol();
1189 RecordChannelIDSupport(); 1183 RecordChannelIDSupport();
1190 1184
1191 const uint8_t* ocsp_response_raw; 1185 const uint8_t* ocsp_response_raw;
1192 size_t ocsp_response_len; 1186 size_t ocsp_response_len;
1193 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); 1187 SSL_get0_ocsp_response(ssl_.get(), &ocsp_response_raw, &ocsp_response_len);
1194 std::string ocsp_response; 1188 std::string ocsp_response;
1195 if (ocsp_response_len > 0) { 1189 if (ocsp_response_len > 0) {
1196 ocsp_response_.assign(reinterpret_cast<const char*>(ocsp_response_raw), 1190 ocsp_response_.assign(reinterpret_cast<const char*>(ocsp_response_raw),
1197 ocsp_response_len); 1191 ocsp_response_len);
1198 } 1192 }
1199 set_stapled_ocsp_response_received(ocsp_response_len != 0); 1193 set_stapled_ocsp_response_received(ocsp_response_len != 0);
1200 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); 1194 UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0);
1201 1195
1202 const uint8_t* sct_list; 1196 const uint8_t* sct_list;
1203 size_t sct_list_len; 1197 size_t sct_list_len;
1204 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); 1198 SSL_get0_signed_cert_timestamp_list(ssl_.get(), &sct_list, &sct_list_len);
1205 set_signed_cert_timestamps_received(sct_list_len != 0); 1199 set_signed_cert_timestamps_received(sct_list_len != 0);
1206 1200
1207 if (IsRenegotiationAllowed()) 1201 if (IsRenegotiationAllowed())
1208 SSL_set_renegotiate_mode(ssl_, ssl_renegotiate_freely); 1202 SSL_set_renegotiate_mode(ssl_.get(), ssl_renegotiate_freely);
1209 1203
1210 uint16_t signature_algorithm = SSL_get_peer_signature_algorithm(ssl_); 1204 uint16_t signature_algorithm = SSL_get_peer_signature_algorithm(ssl_.get());
1211 if (signature_algorithm != 0) { 1205 if (signature_algorithm != 0) {
1212 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLSignatureAlgorithm", 1206 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLSignatureAlgorithm",
1213 signature_algorithm); 1207 signature_algorithm);
1214 } 1208 }
1215 1209
1216 // Verify the certificate. 1210 // Verify the certificate.
1217 UpdateServerCert(); 1211 UpdateServerCert();
1218 next_handshake_state_ = STATE_VERIFY_CERT; 1212 next_handshake_state_ = STATE_VERIFY_CERT;
1219 return OK; 1213 return OK;
1220 } 1214 }
(...skipping 14 matching lines...) Expand all
1235 net_log_.EndEvent(NetLogEventType::SSL_GET_CHANNEL_ID, 1229 net_log_.EndEvent(NetLogEventType::SSL_GET_CHANNEL_ID,
1236 base::Bind(&NetLogChannelIDLookupCompleteCallback, 1230 base::Bind(&NetLogChannelIDLookupCompleteCallback,
1237 channel_id_key_.get(), result)); 1231 channel_id_key_.get(), result));
1238 if (result < 0) 1232 if (result < 0)
1239 return result; 1233 return result;
1240 1234
1241 // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key 1235 // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key
1242 // type. 1236 // type.
1243 DCHECK(channel_id_key_); 1237 DCHECK(channel_id_key_);
1244 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1238 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1245 if (!SSL_set1_tls_channel_id(ssl_, channel_id_key_->key())) { 1239 if (!SSL_set1_tls_channel_id(ssl_.get(), channel_id_key_->key())) {
1246 LOG(ERROR) << "Failed to set Channel ID."; 1240 LOG(ERROR) << "Failed to set Channel ID.";
1247 return ERR_FAILED; 1241 return ERR_FAILED;
1248 } 1242 }
1249 1243
1250 // Return to the handshake. 1244 // Return to the handshake.
1251 channel_id_sent_ = true; 1245 channel_id_sent_ = true;
1252 next_handshake_state_ = STATE_HANDSHAKE; 1246 next_handshake_state_ = STATE_HANDSHAKE;
1253 return OK; 1247 return OK;
1254 } 1248 }
1255 1249
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1350 1344
1351 void SSLClientSocketImpl::DoConnectCallback(int rv) { 1345 void SSLClientSocketImpl::DoConnectCallback(int rv) {
1352 if (!user_connect_callback_.is_null()) { 1346 if (!user_connect_callback_.is_null()) {
1353 CompletionCallback c = user_connect_callback_; 1347 CompletionCallback c = user_connect_callback_;
1354 user_connect_callback_.Reset(); 1348 user_connect_callback_.Reset();
1355 c.Run(rv > OK ? OK : rv); 1349 c.Run(rv > OK ? OK : rv);
1356 } 1350 }
1357 } 1351 }
1358 1352
1359 void SSLClientSocketImpl::UpdateServerCert() { 1353 void SSLClientSocketImpl::UpdateServerCert() {
1360 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); 1354 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_.get()));
1361 server_cert_ = server_cert_chain_->AsOSChain(); 1355 server_cert_ = server_cert_chain_->AsOSChain();
1362 if (server_cert_.get()) { 1356 if (server_cert_.get()) {
1363 net_log_.AddEvent(NetLogEventType::SSL_CERTIFICATES_RECEIVED, 1357 net_log_.AddEvent(NetLogEventType::SSL_CERTIFICATES_RECEIVED,
1364 base::Bind(&NetLogX509CertificateCallback, 1358 base::Bind(&NetLogX509CertificateCallback,
1365 base::Unretained(server_cert_.get()))); 1359 base::Unretained(server_cert_.get())));
1366 } 1360 }
1367 } 1361 }
1368 1362
1369 void SSLClientSocketImpl::OnHandshakeIOComplete(int result) { 1363 void SSLClientSocketImpl::OnHandshakeIOComplete(int result) {
1370 int rv = DoHandshakeLoop(result); 1364 int rv = DoHandshakeLoop(result);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1496 pending_read_error_info_)); 1490 pending_read_error_info_));
1497 } 1491 }
1498 pending_read_ssl_error_ = SSL_ERROR_NONE; 1492 pending_read_ssl_error_ = SSL_ERROR_NONE;
1499 pending_read_error_info_ = OpenSSLErrorInfo(); 1493 pending_read_error_info_ = OpenSSLErrorInfo();
1500 return rv; 1494 return rv;
1501 } 1495 }
1502 1496
1503 int total_bytes_read = 0; 1497 int total_bytes_read = 0;
1504 int ssl_ret; 1498 int ssl_ret;
1505 do { 1499 do {
1506 ssl_ret = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, 1500 ssl_ret = SSL_read(ssl_.get(), user_read_buf_->data() + total_bytes_read,
1507 user_read_buf_len_ - total_bytes_read); 1501 user_read_buf_len_ - total_bytes_read);
1508 if (ssl_ret > 0) 1502 if (ssl_ret > 0)
1509 total_bytes_read += ssl_ret; 1503 total_bytes_read += ssl_ret;
1510 } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0); 1504 } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0);
1511 1505
1512 // Although only the final SSL_read call may have failed, the failure needs to 1506 // Although only the final SSL_read call may have failed, the failure needs to
1513 // processed immediately, while the information still available in OpenSSL's 1507 // processed immediately, while the information still available in OpenSSL's
1514 // error queue. 1508 // error queue.
1515 if (ssl_ret <= 0) { 1509 if (ssl_ret <= 0) {
1516 // A zero return from SSL_read may mean any of: 1510 // A zero return from SSL_read may mean any of:
1517 // - The underlying BIO_read returned 0. 1511 // - The underlying BIO_read returned 0.
1518 // - The peer sent a close_notify. 1512 // - The peer sent a close_notify.
1519 // - Any arbitrary error. https://crbug.com/466303 1513 // - Any arbitrary error. https://crbug.com/466303
1520 // 1514 //
1521 // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED 1515 // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED
1522 // error, so it does not occur. The second and third are distinguished by 1516 // error, so it does not occur. The second and third are distinguished by
1523 // SSL_ERROR_ZERO_RETURN. 1517 // SSL_ERROR_ZERO_RETURN.
1524 pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); 1518 pending_read_ssl_error_ = SSL_get_error(ssl_.get(), ssl_ret);
1525 if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { 1519 if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) {
1526 pending_read_error_ = 0; 1520 pending_read_error_ = 0;
1527 } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && 1521 } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP &&
1528 !ssl_config_.send_client_cert) { 1522 !ssl_config_.send_client_cert) {
1529 pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; 1523 pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED;
1530 } else if (pending_read_ssl_error_ == 1524 } else if (pending_read_ssl_error_ ==
1531 SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { 1525 SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) {
1532 DCHECK(ssl_config_.client_private_key); 1526 DCHECK(ssl_config_.client_private_key);
1533 DCHECK_NE(kNoPendingResult, signature_result_); 1527 DCHECK_NE(kNoPendingResult, signature_result_);
1534 pending_read_error_ = ERR_IO_PENDING; 1528 pending_read_error_ = ERR_IO_PENDING;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1573 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, 1567 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_,
1574 pending_read_error_info_)); 1568 pending_read_error_info_));
1575 pending_read_ssl_error_ = SSL_ERROR_NONE; 1569 pending_read_ssl_error_ = SSL_ERROR_NONE;
1576 pending_read_error_info_ = OpenSSLErrorInfo(); 1570 pending_read_error_info_ = OpenSSLErrorInfo();
1577 } 1571 }
1578 return rv; 1572 return rv;
1579 } 1573 }
1580 1574
1581 int SSLClientSocketImpl::DoPayloadWrite() { 1575 int SSLClientSocketImpl::DoPayloadWrite() {
1582 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1576 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1583 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); 1577 int rv = SSL_write(ssl_.get(), user_write_buf_->data(), user_write_buf_len_);
1584 1578
1585 if (rv >= 0) { 1579 if (rv >= 0) {
1586 net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_SENT, rv, 1580 net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_SENT, rv,
1587 user_write_buf_->data()); 1581 user_write_buf_->data());
1588 return rv; 1582 return rv;
1589 } 1583 }
1590 1584
1591 int ssl_error = SSL_get_error(ssl_, rv); 1585 int ssl_error = SSL_get_error(ssl_.get(), rv);
1592 if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) 1586 if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION)
1593 return ERR_IO_PENDING; 1587 return ERR_IO_PENDING;
1594 OpenSSLErrorInfo error_info; 1588 OpenSSLErrorInfo error_info;
1595 int net_error = MapLastOpenSSLError(ssl_error, err_tracer, &error_info); 1589 int net_error = MapLastOpenSSLError(ssl_error, err_tracer, &error_info);
1596 1590
1597 if (net_error != ERR_IO_PENDING) { 1591 if (net_error != ERR_IO_PENDING) {
1598 net_log_.AddEvent( 1592 net_log_.AddEvent(
1599 NetLogEventType::SSL_WRITE_ERROR, 1593 NetLogEventType::SSL_WRITE_ERROR,
1600 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); 1594 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info));
1601 } 1595 }
(...skipping 27 matching lines...) Expand all
1629 DoWriteCallback(rv_write); 1623 DoWriteCallback(rv_write);
1630 } 1624 }
1631 1625
1632 int SSLClientSocketImpl::BufferSend(void) { 1626 int SSLClientSocketImpl::BufferSend(void) {
1633 if (transport_send_busy_) 1627 if (transport_send_busy_)
1634 return ERR_IO_PENDING; 1628 return ERR_IO_PENDING;
1635 1629
1636 size_t buffer_read_offset; 1630 size_t buffer_read_offset;
1637 uint8_t* read_buf; 1631 uint8_t* read_buf;
1638 size_t max_read; 1632 size_t max_read;
1639 int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, 1633 int status = BIO_zero_copy_get_read_buf(transport_bio_.get(), &read_buf,
1640 &buffer_read_offset, &max_read); 1634 &buffer_read_offset, &max_read);
1641 DCHECK_EQ(status, 1); // Should never fail. 1635 DCHECK_EQ(status, 1); // Should never fail.
1642 if (!max_read) 1636 if (!max_read)
1643 return 0; // Nothing pending in the OpenSSL write BIO. 1637 return 0; // Nothing pending in the OpenSSL write BIO.
1644 CHECK_EQ(read_buf, reinterpret_cast<uint8_t*>(send_buffer_->StartOfBuffer())); 1638 CHECK_EQ(read_buf, reinterpret_cast<uint8_t*>(send_buffer_->StartOfBuffer()));
1645 CHECK_LT(buffer_read_offset, static_cast<size_t>(send_buffer_->capacity())); 1639 CHECK_LT(buffer_read_offset, static_cast<size_t>(send_buffer_->capacity()));
1646 send_buffer_->set_offset(buffer_read_offset); 1640 send_buffer_->set_offset(buffer_read_offset);
1647 1641
1648 int rv = transport_->socket()->Write( 1642 int rv = transport_->socket()->Write(
1649 send_buffer_.get(), max_read, 1643 send_buffer_.get(), max_read,
1650 base::Bind(&SSLClientSocketImpl::BufferSendComplete, 1644 base::Bind(&SSLClientSocketImpl::BufferSendComplete,
1651 base::Unretained(this))); 1645 base::Unretained(this)));
1652 if (rv == ERR_IO_PENDING) { 1646 if (rv == ERR_IO_PENDING) {
1653 transport_send_busy_ = true; 1647 transport_send_busy_ = true;
1654 } else { 1648 } else {
1655 TransportWriteComplete(rv); 1649 TransportWriteComplete(rv);
1656 } 1650 }
1657 return rv; 1651 return rv;
1658 } 1652 }
1659 1653
1660 int SSLClientSocketImpl::BufferRecv(void) { 1654 int SSLClientSocketImpl::BufferRecv(void) {
1661 if (transport_recv_busy_) 1655 if (transport_recv_busy_)
1662 return ERR_IO_PENDING; 1656 return ERR_IO_PENDING;
1663 1657
1664 // Determine how much was requested from |transport_bio_| that was not 1658 // Determine how much was requested from |transport_bio_| that was not
1665 // actually available. 1659 // actually available.
1666 size_t requested = BIO_ctrl_get_read_request(transport_bio_); 1660 size_t requested = BIO_ctrl_get_read_request(transport_bio_.get());
1667 if (requested == 0) { 1661 if (requested == 0) {
1668 // This is not a perfect match of error codes, as no operation is 1662 // This is not a perfect match of error codes, as no operation is
1669 // actually pending. However, returning 0 would be interpreted as 1663 // actually pending. However, returning 0 would be interpreted as
1670 // a possible sign of EOF, which is also an inappropriate match. 1664 // a possible sign of EOF, which is also an inappropriate match.
1671 return ERR_IO_PENDING; 1665 return ERR_IO_PENDING;
1672 } 1666 }
1673 1667
1674 // Known Issue: While only reading |requested| data is the more correct 1668 // Known Issue: While only reading |requested| data is the more correct
1675 // implementation, it has the downside of resulting in frequent reads: 1669 // implementation, it has the downside of resulting in frequent reads:
1676 // One read for the SSL record header (~5 bytes) and one read for the SSL 1670 // One read for the SSL record header (~5 bytes) and one read for the SSL
1677 // record body. Rather than issuing these reads to the underlying socket 1671 // record body. Rather than issuing these reads to the underlying socket
1678 // (and constantly allocating new IOBuffers), a single Read() request to 1672 // (and constantly allocating new IOBuffers), a single Read() request to
1679 // fill |transport_bio_| is issued. As long as an SSL client socket cannot 1673 // fill |transport_bio_| is issued. As long as an SSL client socket cannot
1680 // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL 1674 // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL
1681 // traffic, this over-subscribed Read()ing will not cause issues. 1675 // traffic, this over-subscribed Read()ing will not cause issues.
1682 1676
1683 size_t buffer_write_offset; 1677 size_t buffer_write_offset;
1684 uint8_t* write_buf; 1678 uint8_t* write_buf;
1685 size_t max_write; 1679 size_t max_write;
1686 int status = BIO_zero_copy_get_write_buf(transport_bio_, &write_buf, 1680 int status = BIO_zero_copy_get_write_buf(transport_bio_.get(), &write_buf,
1687 &buffer_write_offset, &max_write); 1681 &buffer_write_offset, &max_write);
1688 DCHECK_EQ(status, 1); // Should never fail. 1682 DCHECK_EQ(status, 1); // Should never fail.
1689 if (!max_write) 1683 if (!max_write)
1690 return ERR_IO_PENDING; 1684 return ERR_IO_PENDING;
1691 1685
1692 CHECK_EQ(write_buf, 1686 CHECK_EQ(write_buf,
1693 reinterpret_cast<uint8_t*>(recv_buffer_->StartOfBuffer())); 1687 reinterpret_cast<uint8_t*>(recv_buffer_->StartOfBuffer()));
1694 CHECK_LT(buffer_write_offset, static_cast<size_t>(recv_buffer_->capacity())); 1688 CHECK_LT(buffer_write_offset, static_cast<size_t>(recv_buffer_->capacity()));
1695 1689
1696 recv_buffer_->set_offset(buffer_write_offset); 1690 recv_buffer_->set_offset(buffer_write_offset);
(...skipping 23 matching lines...) Expand all
1720 DCHECK(ERR_IO_PENDING != result); 1714 DCHECK(ERR_IO_PENDING != result);
1721 int bytes_written = 0; 1715 int bytes_written = 0;
1722 if (result < 0) { 1716 if (result < 0) {
1723 // Record the error. Save it to be reported in a future read or write on 1717 // Record the error. Save it to be reported in a future read or write on
1724 // transport_bio_'s peer. 1718 // transport_bio_'s peer.
1725 transport_write_error_ = result; 1719 transport_write_error_ = result;
1726 } else { 1720 } else {
1727 bytes_written = result; 1721 bytes_written = result;
1728 } 1722 }
1729 DCHECK_GE(send_buffer_->RemainingCapacity(), bytes_written); 1723 DCHECK_GE(send_buffer_->RemainingCapacity(), bytes_written);
1730 int ret = BIO_zero_copy_get_read_buf_done(transport_bio_, bytes_written); 1724 int ret =
1725 BIO_zero_copy_get_read_buf_done(transport_bio_.get(), bytes_written);
1731 DCHECK_EQ(1, ret); 1726 DCHECK_EQ(1, ret);
1732 transport_send_busy_ = false; 1727 transport_send_busy_ = false;
1733 } 1728 }
1734 1729
1735 int SSLClientSocketImpl::TransportReadComplete(int result) { 1730 int SSLClientSocketImpl::TransportReadComplete(int result) {
1736 DCHECK(ERR_IO_PENDING != result); 1731 DCHECK(ERR_IO_PENDING != result);
1737 // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError 1732 // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError
1738 // does not report success. 1733 // does not report success.
1739 if (result == 0) 1734 if (result == 0)
1740 result = ERR_CONNECTION_CLOSED; 1735 result = ERR_CONNECTION_CLOSED;
1741 int bytes_read = 0; 1736 int bytes_read = 0;
1742 if (result < 0) { 1737 if (result < 0) {
1743 // Received an error. Save it to be reported in a future read on 1738 // Received an error. Save it to be reported in a future read on
1744 // transport_bio_'s peer. 1739 // transport_bio_'s peer.
1745 transport_read_error_ = result; 1740 transport_read_error_ = result;
1746 } else { 1741 } else {
1747 bytes_read = result; 1742 bytes_read = result;
1748 } 1743 }
1749 DCHECK_GE(recv_buffer_->RemainingCapacity(), bytes_read); 1744 DCHECK_GE(recv_buffer_->RemainingCapacity(), bytes_read);
1750 int ret = BIO_zero_copy_get_write_buf_done(transport_bio_, bytes_read); 1745 int ret = BIO_zero_copy_get_write_buf_done(transport_bio_.get(), bytes_read);
1751 DCHECK_EQ(1, ret); 1746 DCHECK_EQ(1, ret);
1752 transport_recv_busy_ = false; 1747 transport_recv_busy_ = false;
1753 return result; 1748 return result;
1754 } 1749 }
1755 1750
1756 int SSLClientSocketImpl::VerifyCT() { 1751 int SSLClientSocketImpl::VerifyCT() {
1757 const uint8_t* sct_list_raw; 1752 const uint8_t* sct_list_raw;
1758 size_t sct_list_len; 1753 size_t sct_list_len;
1759 SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list_raw, &sct_list_len); 1754 SSL_get0_signed_cert_timestamp_list(ssl_.get(), &sct_list_raw, &sct_list_len);
1760 std::string sct_list; 1755 std::string sct_list;
1761 if (sct_list_len > 0) 1756 if (sct_list_len > 0)
1762 sct_list.assign(reinterpret_cast<const char*>(sct_list_raw), sct_list_len); 1757 sct_list.assign(reinterpret_cast<const char*>(sct_list_raw), sct_list_len);
1763 1758
1764 // Note that this is a completely synchronous operation: The CT Log Verifier 1759 // Note that this is a completely synchronous operation: The CT Log Verifier
1765 // gets all the data it needs for SCT verification and does not do any 1760 // gets all the data it needs for SCT verification and does not do any
1766 // external communication. 1761 // external communication.
1767 cert_transparency_verifier_->Verify( 1762 cert_transparency_verifier_->Verify(
1768 server_cert_verify_result_.verified_cert.get(), ocsp_response_, sct_list, 1763 server_cert_verify_result_.verified_cert.get(), ocsp_response_, sct_list,
1769 &ct_verify_result_, net_log_); 1764 &ct_verify_result_, net_log_);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1806 server_cert_verify_result_.public_key_hashes)) { 1801 server_cert_verify_result_.public_key_hashes)) {
1807 server_cert_verify_result_.cert_status |= 1802 server_cert_verify_result_.cert_status |=
1808 CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED; 1803 CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED;
1809 return ERR_CERTIFICATE_TRANSPARENCY_REQUIRED; 1804 return ERR_CERTIFICATE_TRANSPARENCY_REQUIRED;
1810 } 1805 }
1811 1806
1812 return OK; 1807 return OK;
1813 } 1808 }
1814 1809
1815 int SSLClientSocketImpl::ClientCertRequestCallback(SSL* ssl) { 1810 int SSLClientSocketImpl::ClientCertRequestCallback(SSL* ssl) {
1816 DCHECK(ssl == ssl_); 1811 DCHECK(ssl == ssl_.get());
1817 1812
1818 net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_REQUESTED); 1813 net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_REQUESTED);
1819 certificate_requested_ = true; 1814 certificate_requested_ = true;
1820 1815
1821 // Clear any currently configured certificates. 1816 // Clear any currently configured certificates.
1822 SSL_certs_clear(ssl_); 1817 SSL_certs_clear(ssl_.get());
1823 1818
1824 #if defined(OS_IOS) 1819 #if defined(OS_IOS)
1825 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). 1820 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954).
1826 LOG(WARNING) << "Client auth is not supported"; 1821 LOG(WARNING) << "Client auth is not supported";
1827 #else // !defined(OS_IOS) 1822 #else // !defined(OS_IOS)
1828 if (!ssl_config_.send_client_cert) { 1823 if (!ssl_config_.send_client_cert) {
1829 // First pass: we know that a client certificate is needed, but we do not 1824 // First pass: we know that a client certificate is needed, but we do not
1830 // have one at hand. 1825 // have one at hand.
1831 STACK_OF(X509_NAME)* authorities = SSL_get_client_CA_list(ssl); 1826 STACK_OF(X509_NAME)* authorities = SSL_get_client_CA_list(ssl);
1832 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { 1827 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) {
(...skipping 12 matching lines...) Expand all
1845 cert_key_types_.push_back( 1840 cert_key_types_.push_back(
1846 static_cast<SSLClientCertType>(client_cert_types[i])); 1841 static_cast<SSLClientCertType>(client_cert_types[i]));
1847 } 1842 }
1848 1843
1849 // Suspends handshake. SSL_get_error will return SSL_ERROR_WANT_X509_LOOKUP. 1844 // Suspends handshake. SSL_get_error will return SSL_ERROR_WANT_X509_LOOKUP.
1850 return -1; 1845 return -1;
1851 } 1846 }
1852 1847
1853 // Second pass: a client certificate should have been selected. 1848 // Second pass: a client certificate should have been selected.
1854 if (ssl_config_.client_cert.get()) { 1849 if (ssl_config_.client_cert.get()) {
1855 ScopedX509 leaf_x509 = 1850 bssl::UniquePtr<X509> leaf_x509 =
1856 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); 1851 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle());
1857 if (!leaf_x509) { 1852 if (!leaf_x509) {
1858 LOG(WARNING) << "Failed to import certificate"; 1853 LOG(WARNING) << "Failed to import certificate";
1859 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); 1854 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT);
1860 return -1; 1855 return -1;
1861 } 1856 }
1862 1857
1863 ScopedX509Stack chain = OSCertHandlesToOpenSSL( 1858 bssl::UniquePtr<STACK_OF(X509)> chain = OSCertHandlesToOpenSSL(
1864 ssl_config_.client_cert->GetIntermediateCertificates()); 1859 ssl_config_.client_cert->GetIntermediateCertificates());
1865 if (!chain) { 1860 if (!chain) {
1866 LOG(WARNING) << "Failed to import intermediate certificates"; 1861 LOG(WARNING) << "Failed to import intermediate certificates";
1867 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); 1862 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT);
1868 return -1; 1863 return -1;
1869 } 1864 }
1870 1865
1871 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || 1866 if (!SSL_use_certificate(ssl_.get(), leaf_x509.get()) ||
1872 !SSL_set1_chain(ssl_, chain.get())) { 1867 !SSL_set1_chain(ssl_.get(), chain.get())) {
1873 LOG(WARNING) << "Failed to set client certificate"; 1868 LOG(WARNING) << "Failed to set client certificate";
1874 return -1; 1869 return -1;
1875 } 1870 }
1876 1871
1877 if (!ssl_config_.client_private_key) { 1872 if (!ssl_config_.client_private_key) {
1878 // The caller supplied a null private key. Fail the handshake and surface 1873 // The caller supplied a null private key. Fail the handshake and surface
1879 // an appropriate error to the caller. 1874 // an appropriate error to the caller.
1880 LOG(WARNING) << "Client cert found without private key"; 1875 LOG(WARNING) << "Client cert found without private key";
1881 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); 1876 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY);
1882 return -1; 1877 return -1;
1883 } 1878 }
1884 1879
1885 SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); 1880 SSL_set_private_key_method(ssl_.get(), &SSLContext::kPrivateKeyMethod);
1886 1881
1887 std::vector<SSLPrivateKey::Hash> digest_prefs = 1882 std::vector<SSLPrivateKey::Hash> digest_prefs =
1888 ssl_config_.client_private_key->GetDigestPreferences(); 1883 ssl_config_.client_private_key->GetDigestPreferences();
1889 1884
1890 size_t digests_len = digest_prefs.size(); 1885 size_t digests_len = digest_prefs.size();
1891 std::vector<int> digests; 1886 std::vector<int> digests;
1892 for (size_t i = 0; i < digests_len; i++) { 1887 for (size_t i = 0; i < digests_len; i++) {
1893 switch (digest_prefs[i]) { 1888 switch (digest_prefs[i]) {
1894 case SSLPrivateKey::Hash::SHA1: 1889 case SSLPrivateKey::Hash::SHA1:
1895 digests.push_back(NID_sha1); 1890 digests.push_back(NID_sha1);
1896 break; 1891 break;
1897 case SSLPrivateKey::Hash::SHA256: 1892 case SSLPrivateKey::Hash::SHA256:
1898 digests.push_back(NID_sha256); 1893 digests.push_back(NID_sha256);
1899 break; 1894 break;
1900 case SSLPrivateKey::Hash::SHA384: 1895 case SSLPrivateKey::Hash::SHA384:
1901 digests.push_back(NID_sha384); 1896 digests.push_back(NID_sha384);
1902 break; 1897 break;
1903 case SSLPrivateKey::Hash::SHA512: 1898 case SSLPrivateKey::Hash::SHA512:
1904 digests.push_back(NID_sha512); 1899 digests.push_back(NID_sha512);
1905 break; 1900 break;
1906 case SSLPrivateKey::Hash::MD5_SHA1: 1901 case SSLPrivateKey::Hash::MD5_SHA1:
1907 // MD5-SHA1 is not used in TLS 1.2. 1902 // MD5-SHA1 is not used in TLS 1.2.
1908 break; 1903 break;
1909 } 1904 }
1910 } 1905 }
1911 1906
1912 SSL_set_private_key_digest_prefs(ssl_, digests.data(), digests.size()); 1907 SSL_set_private_key_digest_prefs(ssl_.get(), digests.data(),
1908 digests.size());
1913 1909
1914 int cert_count = 1 + sk_X509_num(chain.get()); 1910 int cert_count = 1 + sk_X509_num(chain.get());
1915 net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_PROVIDED, 1911 net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_PROVIDED,
1916 NetLog::IntCallback("cert_count", cert_count)); 1912 NetLog::IntCallback("cert_count", cert_count));
1917 return 1; 1913 return 1;
1918 } 1914 }
1919 #endif // defined(OS_IOS) 1915 #endif // defined(OS_IOS)
1920 1916
1921 // Send no client certificate. 1917 // Send no client certificate.
1922 net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_PROVIDED, 1918 net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_PROVIDED,
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
2141 // asynchronous private key operation. 2137 // asynchronous private key operation.
2142 PumpReadWriteEvents(); 2138 PumpReadWriteEvents();
2143 } 2139 }
2144 2140
2145 int SSLClientSocketImpl::TokenBindingAdd(const uint8_t** out, 2141 int SSLClientSocketImpl::TokenBindingAdd(const uint8_t** out,
2146 size_t* out_len, 2142 size_t* out_len,
2147 int* out_alert_value) { 2143 int* out_alert_value) {
2148 if (ssl_config_.token_binding_params.empty()) { 2144 if (ssl_config_.token_binding_params.empty()) {
2149 return 0; 2145 return 0;
2150 } 2146 }
2151 crypto::AutoCBB output; 2147 bssl::ScopedCBB output;
2152 CBB parameters_list; 2148 CBB parameters_list;
2153 if (!CBB_init(output.get(), 7) || 2149 if (!CBB_init(output.get(), 7) ||
2154 !CBB_add_u8(output.get(), kTbProtocolVersionMajor) || 2150 !CBB_add_u8(output.get(), kTbProtocolVersionMajor) ||
2155 !CBB_add_u8(output.get(), kTbProtocolVersionMinor) || 2151 !CBB_add_u8(output.get(), kTbProtocolVersionMinor) ||
2156 !CBB_add_u8_length_prefixed(output.get(), &parameters_list)) { 2152 !CBB_add_u8_length_prefixed(output.get(), &parameters_list)) {
2157 *out_alert_value = SSL_AD_INTERNAL_ERROR; 2153 *out_alert_value = SSL_AD_INTERNAL_ERROR;
2158 return -1; 2154 return -1;
2159 } 2155 }
2160 for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { 2156 for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) {
2161 if (!CBB_add_u8(&parameters_list, ssl_config_.token_binding_params[i])) { 2157 if (!CBB_add_u8(&parameters_list, ssl_config_.token_binding_params[i])) {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
2290 if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED && 2286 if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED &&
2291 !certificate_requested_) { 2287 !certificate_requested_) {
2292 net_error = ERR_SSL_PROTOCOL_ERROR; 2288 net_error = ERR_SSL_PROTOCOL_ERROR;
2293 } 2289 }
2294 } 2290 }
2295 2291
2296 return net_error; 2292 return net_error;
2297 } 2293 }
2298 2294
2299 } // namespace net 2295 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/ssl_client_socket_impl.h ('k') | net/socket/ssl_client_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698