OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/gcd_private/privet_v3_context_getter.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/common/chrome_content_client.h" |
| 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/cert/cert_verifier.h" |
| 12 #include "net/cert/cert_verify_result.h" |
| 13 #include "net/cert/x509_certificate.h" |
| 14 #include "net/url_request/url_request_context.h" |
| 15 #include "net/url_request/url_request_context_builder.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 // Class verifies certificate by its fingerprint received using different |
| 20 // channel. It's the only know information about device with self-signed |
| 21 // certificate. |
| 22 class FingerprintVerifier : public net::CertVerifier { |
| 23 public: |
| 24 explicit FingerprintVerifier( |
| 25 const net::SHA256HashValue& certificate_fingerprint) |
| 26 : certificate_fingerprint_(certificate_fingerprint) {} |
| 27 |
| 28 int Verify(net::X509Certificate* cert, |
| 29 const std::string& hostname, |
| 30 const std::string& ocsp_response, |
| 31 int flags, |
| 32 net::CRLSet* crl_set, |
| 33 net::CertVerifyResult* verify_result, |
| 34 const net::CompletionCallback& callback, |
| 35 scoped_ptr<Request>* out_req, |
| 36 const net::BoundNetLog& net_log) override { |
| 37 // Mark certificate as invalid as we didn't check it. |
| 38 verify_result->Reset(); |
| 39 verify_result->verified_cert = cert; |
| 40 verify_result->cert_status = net::CERT_STATUS_INVALID; |
| 41 |
| 42 auto fingerprint = |
| 43 net::X509Certificate::CalculateFingerprint256(cert->os_cert_handle()); |
| 44 |
| 45 return certificate_fingerprint_.Equals(fingerprint) ? net::OK |
| 46 : net::ERR_CERT_INVALID; |
| 47 } |
| 48 |
| 49 private: |
| 50 net::SHA256HashValue certificate_fingerprint_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(FingerprintVerifier); |
| 53 }; |
| 54 |
| 55 PrivetV3ContextGetter::PrivetV3ContextGetter( |
| 56 const scoped_refptr<base::SingleThreadTaskRunner>& net_task_runner, |
| 57 const net::SHA256HashValue& certificate_fingerprint) |
| 58 : verifier_(new FingerprintVerifier(certificate_fingerprint)), |
| 59 net_task_runner_(net_task_runner) { |
| 60 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 61 switches::kEnablePrivetV3)); |
| 62 } |
| 63 |
| 64 net::URLRequestContext* PrivetV3ContextGetter::GetURLRequestContext() { |
| 65 DCHECK(net_task_runner_->BelongsToCurrentThread()); |
| 66 if (!context_) { |
| 67 net::URLRequestContextBuilder builder; |
| 68 builder.set_proxy_service(net::ProxyService::CreateDirect()); |
| 69 builder.SetSpdyAndQuicEnabled(false, false); |
| 70 builder.DisableHttpCache(); |
| 71 builder.SetCertVerifier(verifier_.Pass()); |
| 72 builder.set_user_agent(::GetUserAgent()); |
| 73 context_ = builder.Build(); |
| 74 } |
| 75 return context_.get(); |
| 76 } |
| 77 |
| 78 scoped_refptr<base::SingleThreadTaskRunner> |
| 79 PrivetV3ContextGetter::GetNetworkTaskRunner() const { |
| 80 return net_task_runner_; |
| 81 } |
| 82 |
| 83 PrivetV3ContextGetter::~PrivetV3ContextGetter() { |
| 84 DCHECK(net_task_runner_->BelongsToCurrentThread()); |
| 85 } |
| 86 |
| 87 } // namespace extensions |
OLD | NEW |