| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/local_discovery/privet_http_impl.h" | 5 #include "chrome/browser/local_discovery/privet_http_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "chrome/browser/extensions/api/gcd_private/privet_v3_context_getter.h" |
| 18 #include "chrome/browser/local_discovery/privet_constants.h" | 18 #include "chrome/browser/local_discovery/privet_constants.h" |
| 19 #include "chrome/common/chrome_content_client.h" | 19 #include "chrome/common/chrome_content_client.h" |
| 20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/cloud_print/cloud_print_constants.h" | 21 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 22 #include "net/base/url_util.h" | 22 #include "net/base/url_util.h" |
| 23 #include "net/cert/cert_verifier.h" | |
| 24 #include "net/cert/cert_verify_result.h" | |
| 25 #include "net/url_request/url_request_context.h" | |
| 26 #include "net/url_request/url_request_context_builder.h" | |
| 27 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 28 | 24 |
| 29 #if defined(ENABLE_PRINT_PREVIEW) | 25 #if defined(ENABLE_PRINT_PREVIEW) |
| 30 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 26 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
| 31 #include "components/cloud_devices/common/printer_description.h" | 27 #include "components/cloud_devices/common/printer_description.h" |
| 32 #include "printing/pdf_render_settings.h" | 28 #include "printing/pdf_render_settings.h" |
| 33 #include "printing/pwg_raster_settings.h" | 29 #include "printing/pwg_raster_settings.h" |
| 34 #include "ui/gfx/text_elider.h" | 30 #include "ui/gfx/text_elider.h" |
| 35 #endif // ENABLE_PRINT_PREVIEW | 31 #endif // ENABLE_PRINT_PREVIEW |
| 36 | 32 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 const std::string& query_params) { | 78 const std::string& query_params) { |
| 83 GURL url(kUrlPlaceHolder); | 79 GURL url(kUrlPlaceHolder); |
| 84 GURL::Replacements replacements; | 80 GURL::Replacements replacements; |
| 85 replacements.SetPathStr(path); | 81 replacements.SetPathStr(path); |
| 86 if (!query_params.empty()) { | 82 if (!query_params.empty()) { |
| 87 replacements.SetQueryStr(query_params); | 83 replacements.SetQueryStr(query_params); |
| 88 } | 84 } |
| 89 return url.ReplaceComponents(replacements); | 85 return url.ReplaceComponents(replacements); |
| 90 } | 86 } |
| 91 | 87 |
| 92 // Class verifies certificate by its fingerprint received using different | |
| 93 // channel. It's the only know information about device with self-signed | |
| 94 // certificate. | |
| 95 class FingerprintVerifier : public net::CertVerifier { | |
| 96 public: | |
| 97 explicit FingerprintVerifier( | |
| 98 const net::SHA256HashValue& certificate_fingerprint) | |
| 99 : certificate_fingerprint_(certificate_fingerprint) {} | |
| 100 | |
| 101 int Verify(net::X509Certificate* cert, | |
| 102 const std::string& hostname, | |
| 103 const std::string& ocsp_response, | |
| 104 int flags, | |
| 105 net::CRLSet* crl_set, | |
| 106 net::CertVerifyResult* verify_result, | |
| 107 const net::CompletionCallback& callback, | |
| 108 scoped_ptr<Request>* out_req, | |
| 109 const net::BoundNetLog& net_log) override { | |
| 110 // Mark certificate as invalid as we didn't check it. | |
| 111 verify_result->Reset(); | |
| 112 verify_result->verified_cert = cert; | |
| 113 verify_result->cert_status = net::CERT_STATUS_INVALID; | |
| 114 | |
| 115 auto fingerprint = | |
| 116 net::X509Certificate::CalculateFingerprint256(cert->os_cert_handle()); | |
| 117 | |
| 118 return certificate_fingerprint_.Equals(fingerprint) ? net::OK | |
| 119 : net::ERR_CERT_INVALID; | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 net::SHA256HashValue certificate_fingerprint_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(FingerprintVerifier); | |
| 126 }; | |
| 127 | |
| 128 class PrivetContextGetter : public net::URLRequestContextGetter { | |
| 129 public: | |
| 130 PrivetContextGetter( | |
| 131 const scoped_refptr<base::SingleThreadTaskRunner>& net_task_runner, | |
| 132 const net::SHA256HashValue& certificate_fingerprint) | |
| 133 : verifier_(new FingerprintVerifier(certificate_fingerprint)), | |
| 134 net_task_runner_(net_task_runner) { | |
| 135 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 136 switches::kEnablePrivetV3)); | |
| 137 } | |
| 138 | |
| 139 net::URLRequestContext* GetURLRequestContext() override { | |
| 140 DCHECK(net_task_runner_->BelongsToCurrentThread()); | |
| 141 if (!context_) { | |
| 142 net::URLRequestContextBuilder builder; | |
| 143 builder.set_proxy_service(net::ProxyService::CreateDirect()); | |
| 144 builder.SetSpdyAndQuicEnabled(false, false); | |
| 145 builder.DisableHttpCache(); | |
| 146 builder.SetCertVerifier(verifier_.Pass()); | |
| 147 builder.set_user_agent(::GetUserAgent()); | |
| 148 context_ = builder.Build(); | |
| 149 } | |
| 150 return context_.get(); | |
| 151 } | |
| 152 | |
| 153 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | |
| 154 const override { | |
| 155 return net_task_runner_; | |
| 156 } | |
| 157 | |
| 158 protected: | |
| 159 ~PrivetContextGetter() override { | |
| 160 DCHECK(net_task_runner_->BelongsToCurrentThread()); | |
| 161 } | |
| 162 | |
| 163 private: | |
| 164 scoped_ptr<net::CertVerifier> verifier_; | |
| 165 scoped_ptr<net::URLRequestContext> context_; | |
| 166 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner_; | |
| 167 | |
| 168 DISALLOW_COPY_AND_ASSIGN(PrivetContextGetter); | |
| 169 }; | |
| 170 | |
| 171 } // namespace | 88 } // namespace |
| 172 | 89 |
| 173 PrivetInfoOperationImpl::PrivetInfoOperationImpl( | 90 PrivetInfoOperationImpl::PrivetInfoOperationImpl( |
| 174 PrivetHTTPClient* privet_client, | 91 PrivetHTTPClient* privet_client, |
| 175 const PrivetJSONOperation::ResultCallback& callback) | 92 const PrivetJSONOperation::ResultCallback& callback) |
| 176 : privet_client_(privet_client), callback_(callback) { | 93 : privet_client_(privet_client), callback_(callback) { |
| 177 } | 94 } |
| 178 | 95 |
| 179 PrivetInfoOperationImpl::~PrivetInfoOperationImpl() { | 96 PrivetInfoOperationImpl::~PrivetInfoOperationImpl() { |
| 180 } | 97 } |
| (...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 819 base::Unretained(this))); | 736 base::Unretained(this))); |
| 820 info_operation_->Start(); | 737 info_operation_->Start(); |
| 821 } | 738 } |
| 822 } | 739 } |
| 823 | 740 |
| 824 void PrivetHTTPClientImpl::SwitchToHttps( | 741 void PrivetHTTPClientImpl::SwitchToHttps( |
| 825 uint16_t port, | 742 uint16_t port, |
| 826 const net::SHA256HashValue& certificate_fingerprint) { | 743 const net::SHA256HashValue& certificate_fingerprint) { |
| 827 use_https_ = true; | 744 use_https_ = true; |
| 828 host_port_.set_port(port); | 745 host_port_.set_port(port); |
| 829 context_getter_ = new PrivetContextGetter( | 746 context_getter_ = new extensions::PrivetV3ContextGetter( |
| 830 context_getter_->GetNetworkTaskRunner(), certificate_fingerprint); | 747 context_getter_->GetNetworkTaskRunner(), certificate_fingerprint); |
| 831 } | 748 } |
| 832 | 749 |
| 833 bool PrivetHTTPClientImpl::IsInHttpsMode() const { | 750 bool PrivetHTTPClientImpl::IsInHttpsMode() const { |
| 834 return use_https_; | 751 return use_https_; |
| 835 } | 752 } |
| 836 | 753 |
| 837 void PrivetHTTPClientImpl::OnPrivetInfoDone( | 754 void PrivetHTTPClientImpl::OnPrivetInfoDone( |
| 838 const base::DictionaryValue* value) { | 755 const base::DictionaryValue* value) { |
| 839 info_operation_.reset(); | 756 info_operation_.reset(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 891 PrivetLocalPrintOperation::Delegate* delegate) { | 808 PrivetLocalPrintOperation::Delegate* delegate) { |
| 892 #if defined(ENABLE_PRINT_PREVIEW) | 809 #if defined(ENABLE_PRINT_PREVIEW) |
| 893 return scoped_ptr<PrivetLocalPrintOperation>( | 810 return scoped_ptr<PrivetLocalPrintOperation>( |
| 894 new PrivetLocalPrintOperationImpl(info_client(), delegate)); | 811 new PrivetLocalPrintOperationImpl(info_client(), delegate)); |
| 895 #else | 812 #else |
| 896 return scoped_ptr<PrivetLocalPrintOperation>(); | 813 return scoped_ptr<PrivetLocalPrintOperation>(); |
| 897 #endif // ENABLE_PRINT_PREVIEW | 814 #endif // ENABLE_PRINT_PREVIEW |
| 898 } | 815 } |
| 899 | 816 |
| 900 } // namespace local_discovery | 817 } // namespace local_discovery |
| OLD | NEW |