| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/extensions/api/platform_keys/verify_trust_api.h" | 5 #include "chrome/browser/extensions/api/platform_keys/verify_trust_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 // used on the IO thread only. | 36 // used on the IO thread only. |
| 37 // It is created on the UI thread and afterwards lives on the IO thread. | 37 // It is created on the UI thread and afterwards lives on the IO thread. |
| 38 class VerifyTrustAPI::IOPart { | 38 class VerifyTrustAPI::IOPart { |
| 39 public: | 39 public: |
| 40 ~IOPart(); | 40 ~IOPart(); |
| 41 | 41 |
| 42 // Verifies the certificate as stated by |params| and calls back |callback| | 42 // Verifies the certificate as stated by |params| and calls back |callback| |
| 43 // with the result (see the declaration of VerifyCallback). | 43 // with the result (see the declaration of VerifyCallback). |
| 44 // Will not call back after this object is destructed or the verifier for this | 44 // Will not call back after this object is destructed or the verifier for this |
| 45 // extension is deleted (see OnExtensionUnloaded). | 45 // extension is deleted (see OnExtensionUnloaded). |
| 46 void Verify(scoped_ptr<Params> params, | 46 void Verify(std::unique_ptr<Params> params, |
| 47 const std::string& extension_id, | 47 const std::string& extension_id, |
| 48 const VerifyCallback& callback); | 48 const VerifyCallback& callback); |
| 49 | 49 |
| 50 // Must be called when the extension with id |extension_id| is unloaded. | 50 // Must be called when the extension with id |extension_id| is unloaded. |
| 51 // Deletes the verifier for |extension_id| and cancels all pending | 51 // Deletes the verifier for |extension_id| and cancels all pending |
| 52 // verifications of this verifier. | 52 // verifications of this verifier. |
| 53 void OnExtensionUnloaded(const std::string& extension_id); | 53 void OnExtensionUnloaded(const std::string& extension_id); |
| 54 | 54 |
| 55 private: | 55 private: |
| 56 struct RequestState { | 56 struct RequestState { |
| 57 RequestState() {} | 57 RequestState() {} |
| 58 | 58 |
| 59 scoped_ptr<net::CertVerifier::Request> request; | 59 std::unique_ptr<net::CertVerifier::Request> request; |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(RequestState); | 62 DISALLOW_COPY_AND_ASSIGN(RequestState); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 // Calls back |callback| with the result and no error. | 65 // Calls back |callback| with the result and no error. |
| 66 void CallBackWithResult(const VerifyCallback& callback, | 66 void CallBackWithResult(const VerifyCallback& callback, |
| 67 scoped_ptr<net::CertVerifyResult> verify_result, | 67 std::unique_ptr<net::CertVerifyResult> verify_result, |
| 68 RequestState* request_state, | 68 RequestState* request_state, |
| 69 int return_value); | 69 int return_value); |
| 70 | 70 |
| 71 // One CertVerifier per extension to verify trust. Each verifier is created on | 71 // One CertVerifier per extension to verify trust. Each verifier is created on |
| 72 // first usage and deleted when this IOPart is destructed or the respective | 72 // first usage and deleted when this IOPart is destructed or the respective |
| 73 // extension is unloaded. | 73 // extension is unloaded. |
| 74 std::map<std::string, linked_ptr<net::CertVerifier>> extension_to_verifier_; | 74 std::map<std::string, linked_ptr<net::CertVerifier>> extension_to_verifier_; |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 // static | 77 // static |
| (...skipping 12 matching lines...) Expand all Loading... |
| 90 VerifyTrustAPI::VerifyTrustAPI(content::BrowserContext* context) | 90 VerifyTrustAPI::VerifyTrustAPI(content::BrowserContext* context) |
| 91 : io_part_(new IOPart), registry_observer_(this), weak_factory_(this) { | 91 : io_part_(new IOPart), registry_observer_(this), weak_factory_(this) { |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 93 registry_observer_.Add(ExtensionRegistry::Get(context)); | 93 registry_observer_.Add(ExtensionRegistry::Get(context)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 VerifyTrustAPI::~VerifyTrustAPI() { | 96 VerifyTrustAPI::~VerifyTrustAPI() { |
| 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void VerifyTrustAPI::Verify(scoped_ptr<Params> params, | 100 void VerifyTrustAPI::Verify(std::unique_ptr<Params> params, |
| 101 const std::string& extension_id, | 101 const std::string& extension_id, |
| 102 const VerifyCallback& ui_callback) { | 102 const VerifyCallback& ui_callback) { |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 104 | 104 |
| 105 // Call back through the VerifyTrustAPI object on the UIThread. Because of the | 105 // Call back through the VerifyTrustAPI object on the UIThread. Because of the |
| 106 // WeakPtr usage, this will ensure that |ui_callback| is not called after the | 106 // WeakPtr usage, this will ensure that |ui_callback| is not called after the |
| 107 // API is destroyed. | 107 // API is destroyed. |
| 108 VerifyCallback finish_callback(base::Bind( | 108 VerifyCallback finish_callback(base::Bind( |
| 109 &CallBackOnUI, base::Bind(&VerifyTrustAPI::FinishedVerificationOnUI, | 109 &CallBackOnUI, base::Bind(&VerifyTrustAPI::FinishedVerificationOnUI, |
| 110 weak_factory_.GetWeakPtr(), ui_callback))); | 110 weak_factory_.GetWeakPtr(), ui_callback))); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 141 int cert_status) { | 141 int cert_status) { |
| 142 content::BrowserThread::PostTask( | 142 content::BrowserThread::PostTask( |
| 143 content::BrowserThread::UI, FROM_HERE, | 143 content::BrowserThread::UI, FROM_HERE, |
| 144 base::Bind(ui_callback, error, return_value, cert_status)); | 144 base::Bind(ui_callback, error, return_value, cert_status)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 VerifyTrustAPI::IOPart::~IOPart() { | 147 VerifyTrustAPI::IOPart::~IOPart() { |
| 148 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 148 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void VerifyTrustAPI::IOPart::Verify(scoped_ptr<Params> params, | 151 void VerifyTrustAPI::IOPart::Verify(std::unique_ptr<Params> params, |
| 152 const std::string& extension_id, | 152 const std::string& extension_id, |
| 153 const VerifyCallback& callback) { | 153 const VerifyCallback& callback) { |
| 154 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 154 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 155 | 155 |
| 156 const api::platform_keys::VerificationDetails& details = params->details; | 156 const api::platform_keys::VerificationDetails& details = params->details; |
| 157 | 157 |
| 158 if (details.server_certificate_chain.empty()) { | 158 if (details.server_certificate_chain.empty()) { |
| 159 callback.Run(kErrorEmptyCertificateChain, 0, 0); | 159 callback.Run(kErrorEmptyCertificateChain, 0, 0); |
| 160 return; | 160 return; |
| 161 } | 161 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 175 callback.Run(platform_keys::kErrorInvalidX509Cert, 0, 0); | 175 callback.Run(platform_keys::kErrorInvalidX509Cert, 0, 0); |
| 176 return; | 176 return; |
| 177 } | 177 } |
| 178 | 178 |
| 179 if (!ContainsKey(extension_to_verifier_, extension_id)) { | 179 if (!ContainsKey(extension_to_verifier_, extension_id)) { |
| 180 extension_to_verifier_[extension_id] = | 180 extension_to_verifier_[extension_id] = |
| 181 make_linked_ptr(net::CertVerifier::CreateDefault().release()); | 181 make_linked_ptr(net::CertVerifier::CreateDefault().release()); |
| 182 } | 182 } |
| 183 net::CertVerifier* verifier = extension_to_verifier_[extension_id].get(); | 183 net::CertVerifier* verifier = extension_to_verifier_[extension_id].get(); |
| 184 | 184 |
| 185 scoped_ptr<net::CertVerifyResult> verify_result(new net::CertVerifyResult); | 185 std::unique_ptr<net::CertVerifyResult> verify_result( |
| 186 scoped_ptr<net::BoundNetLog> net_log(new net::BoundNetLog); | 186 new net::CertVerifyResult); |
| 187 std::unique_ptr<net::BoundNetLog> net_log(new net::BoundNetLog); |
| 187 const int flags = 0; | 188 const int flags = 0; |
| 188 | 189 |
| 189 std::string ocsp_response; | 190 std::string ocsp_response; |
| 190 net::CertVerifyResult* const verify_result_ptr = verify_result.get(); | 191 net::CertVerifyResult* const verify_result_ptr = verify_result.get(); |
| 191 | 192 |
| 192 RequestState* request_state = new RequestState(); | 193 RequestState* request_state = new RequestState(); |
| 193 base::Callback<void(int)> bound_callback( | 194 base::Callback<void(int)> bound_callback( |
| 194 base::Bind(&IOPart::CallBackWithResult, base::Unretained(this), callback, | 195 base::Bind(&IOPart::CallBackWithResult, base::Unretained(this), callback, |
| 195 base::Passed(&verify_result), base::Owned(request_state))); | 196 base::Passed(&verify_result), base::Owned(request_state))); |
| 196 | 197 |
| 197 const int return_value = verifier->Verify( | 198 const int return_value = verifier->Verify( |
| 198 cert_chain.get(), details.hostname, ocsp_response, flags, | 199 cert_chain.get(), details.hostname, ocsp_response, flags, |
| 199 net::SSLConfigService::GetCRLSet().get(), verify_result_ptr, | 200 net::SSLConfigService::GetCRLSet().get(), verify_result_ptr, |
| 200 bound_callback, &request_state->request, *net_log); | 201 bound_callback, &request_state->request, *net_log); |
| 201 | 202 |
| 202 if (return_value != net::ERR_IO_PENDING) { | 203 if (return_value != net::ERR_IO_PENDING) { |
| 203 bound_callback.Run(return_value); | 204 bound_callback.Run(return_value); |
| 204 return; | 205 return; |
| 205 } | 206 } |
| 206 } | 207 } |
| 207 | 208 |
| 208 void VerifyTrustAPI::IOPart::OnExtensionUnloaded( | 209 void VerifyTrustAPI::IOPart::OnExtensionUnloaded( |
| 209 const std::string& extension_id) { | 210 const std::string& extension_id) { |
| 210 extension_to_verifier_.erase(extension_id); | 211 extension_to_verifier_.erase(extension_id); |
| 211 } | 212 } |
| 212 | 213 |
| 213 void VerifyTrustAPI::IOPart::CallBackWithResult( | 214 void VerifyTrustAPI::IOPart::CallBackWithResult( |
| 214 const VerifyCallback& callback, | 215 const VerifyCallback& callback, |
| 215 scoped_ptr<net::CertVerifyResult> verify_result, | 216 std::unique_ptr<net::CertVerifyResult> verify_result, |
| 216 RequestState* request_state, | 217 RequestState* request_state, |
| 217 int return_value) { | 218 int return_value) { |
| 218 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 219 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 219 | 220 |
| 220 callback.Run(std::string() /* no error message */, return_value, | 221 callback.Run(std::string() /* no error message */, return_value, |
| 221 verify_result->cert_status); | 222 verify_result->cert_status); |
| 222 } | 223 } |
| 223 | 224 |
| 224 } // namespace extensions | 225 } // namespace extensions |
| OLD | NEW |