OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/platform_keys/platform_keys.h" | 5 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
18 #include "base/threading/worker_pool.h" | 18 #include "base/threading/worker_pool.h" |
| 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/browser_process_platform_part_chromeos.h" |
| 21 #include "chrome/browser/chromeos/net/client_cert_filter_chromeos.h" |
| 22 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 23 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
19 #include "chrome/browser/extensions/api/enterprise_platform_keys/enterprise_plat
form_keys_api.h" | 24 #include "chrome/browser/extensions/api/enterprise_platform_keys/enterprise_plat
form_keys_api.h" |
20 #include "chrome/browser/net/nss_context.h" | 25 #include "chrome/browser/net/nss_context.h" |
| 26 #include "chrome/browser/profiles/profile.h" |
| 27 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
21 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
22 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
23 #include "crypto/rsa_private_key.h" | 30 #include "crypto/rsa_private_key.h" |
24 #include "net/base/crypto_module.h" | 31 #include "net/base/crypto_module.h" |
25 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
26 #include "net/cert/cert_database.h" | 33 #include "net/cert/cert_database.h" |
27 #include "net/cert/nss_cert_database.h" | 34 #include "net/cert/nss_cert_database.h" |
28 #include "net/cert/x509_certificate.h" | 35 #include "net/cert/x509_certificate.h" |
| 36 #include "net/ssl/client_cert_store_chromeos.h" |
| 37 #include "net/ssl/ssl_cert_request_info.h" |
29 | 38 |
30 using content::BrowserContext; | 39 using content::BrowserContext; |
31 using content::BrowserThread; | 40 using content::BrowserThread; |
32 | 41 |
33 namespace { | 42 namespace { |
34 const char kErrorInternal[] = "Internal Error."; | 43 const char kErrorInternal[] = "Internal Error."; |
35 const char kErrorKeyNotFound[] = "Key not found."; | 44 const char kErrorKeyNotFound[] = "Key not found."; |
36 const char kErrorCertificateNotFound[] = "Certificate could not be found."; | 45 const char kErrorCertificateNotFound[] = "Certificate could not be found."; |
37 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported."; | 46 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported."; |
38 | 47 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 190 |
182 const std::string public_key_; | 191 const std::string public_key_; |
183 HashAlgorithm hash_algorithm_; | 192 HashAlgorithm hash_algorithm_; |
184 const std::string data_; | 193 const std::string data_; |
185 | 194 |
186 private: | 195 private: |
187 // Must be called on origin thread, therefore use CallBack(). | 196 // Must be called on origin thread, therefore use CallBack(). |
188 subtle::SignCallback callback_; | 197 subtle::SignCallback callback_; |
189 }; | 198 }; |
190 | 199 |
| 200 class SelectCertificatesState : public NSSOperationState { |
| 201 public: |
| 202 explicit SelectCertificatesState( |
| 203 const std::string& username_hash, |
| 204 const bool use_system_key_slot, |
| 205 scoped_refptr<net::SSLCertRequestInfo> request, |
| 206 const subtle::SelectCertificatesCallback& callback); |
| 207 ~SelectCertificatesState() override {} |
| 208 |
| 209 void OnError(const tracked_objects::Location& from, |
| 210 const std::string& error_message) override { |
| 211 CallBack(from, scoped_ptr<net::CertificateList>() /* no matches */, |
| 212 error_message); |
| 213 } |
| 214 |
| 215 void CallBack(const tracked_objects::Location& from, |
| 216 scoped_ptr<net::CertificateList> matches, |
| 217 const std::string& error_message) { |
| 218 origin_task_runner_->PostTask( |
| 219 from, base::Bind(callback_, base::Passed(&matches), error_message)); |
| 220 } |
| 221 |
| 222 const std::string username_hash_; |
| 223 const bool use_system_key_slot_; |
| 224 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
| 225 scoped_ptr<net::ClientCertStore> cert_store_; |
| 226 scoped_ptr<net::CertificateList> certs_; |
| 227 |
| 228 private: |
| 229 // Must be called on origin thread, therefore use CallBack(). |
| 230 subtle::SelectCertificatesCallback callback_; |
| 231 }; |
| 232 |
191 class GetCertificatesState : public NSSOperationState { | 233 class GetCertificatesState : public NSSOperationState { |
192 public: | 234 public: |
193 explicit GetCertificatesState(const GetCertificatesCallback& callback); | 235 explicit GetCertificatesState(const GetCertificatesCallback& callback); |
194 ~GetCertificatesState() override {} | 236 ~GetCertificatesState() override {} |
195 | 237 |
196 void OnError(const tracked_objects::Location& from, | 238 void OnError(const tracked_objects::Location& from, |
197 const std::string& error_message) override { | 239 const std::string& error_message) override { |
198 CallBack(from, | 240 CallBack(from, |
199 scoped_ptr<net::CertificateList>() /* no certificates */, | 241 scoped_ptr<net::CertificateList>() /* no certificates */, |
200 error_message); | 242 error_message); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 SignState::SignState(const std::string& public_key, | 339 SignState::SignState(const std::string& public_key, |
298 HashAlgorithm hash_algorithm, | 340 HashAlgorithm hash_algorithm, |
299 const std::string& data, | 341 const std::string& data, |
300 const subtle::SignCallback& callback) | 342 const subtle::SignCallback& callback) |
301 : public_key_(public_key), | 343 : public_key_(public_key), |
302 hash_algorithm_(hash_algorithm), | 344 hash_algorithm_(hash_algorithm), |
303 data_(data), | 345 data_(data), |
304 callback_(callback) { | 346 callback_(callback) { |
305 } | 347 } |
306 | 348 |
| 349 SelectCertificatesState::SelectCertificatesState( |
| 350 const std::string& username_hash, |
| 351 const bool use_system_key_slot, |
| 352 scoped_refptr<net::SSLCertRequestInfo> cert_request_info, |
| 353 const subtle::SelectCertificatesCallback& callback) |
| 354 : username_hash_(username_hash), |
| 355 use_system_key_slot_(use_system_key_slot), |
| 356 cert_request_info_(cert_request_info), |
| 357 callback_(callback) { |
| 358 } |
| 359 |
307 GetCertificatesState::GetCertificatesState( | 360 GetCertificatesState::GetCertificatesState( |
308 const GetCertificatesCallback& callback) | 361 const GetCertificatesCallback& callback) |
309 : callback_(callback) { | 362 : callback_(callback) { |
310 } | 363 } |
311 | 364 |
312 ImportCertificateState::ImportCertificateState( | 365 ImportCertificateState::ImportCertificateState( |
313 scoped_refptr<net::X509Certificate> certificate, | 366 scoped_refptr<net::X509Certificate> certificate, |
314 const ImportCertificateCallback& callback) | 367 const ImportCertificateCallback& callback) |
315 : certificate_(certificate), callback_(callback) { | 368 : certificate_(certificate), callback_(callback) { |
316 } | 369 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 // Continues signing with the obtained NSSCertDatabase. Used by Sign(). | 465 // Continues signing with the obtained NSSCertDatabase. Used by Sign(). |
413 void RSASignWithDB(scoped_ptr<SignState> state, net::NSSCertDatabase* cert_db) { | 466 void RSASignWithDB(scoped_ptr<SignState> state, net::NSSCertDatabase* cert_db) { |
414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
415 // Only the slot and not the NSSCertDatabase is required. Ignore |cert_db|. | 468 // Only the slot and not the NSSCertDatabase is required. Ignore |cert_db|. |
416 base::WorkerPool::PostTask( | 469 base::WorkerPool::PostTask( |
417 FROM_HERE, | 470 FROM_HERE, |
418 base::Bind(&RSASignOnWorkerThread, base::Passed(&state)), | 471 base::Bind(&RSASignOnWorkerThread, base::Passed(&state)), |
419 true /*task is slow*/); | 472 true /*task is slow*/); |
420 } | 473 } |
421 | 474 |
| 475 // Called when ClientCertStoreChromeOS::GetClientCerts is done. Builds the list |
| 476 // of net::CertificateList and calls back. Used by |
| 477 // SelectCertificatesOnIOThread(). |
| 478 void DidSelectCertificatesOnIOThread( |
| 479 scoped_ptr<SelectCertificatesState> state) { |
| 480 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 481 state->CallBack(FROM_HERE, state->certs_.Pass(), |
| 482 std::string() /* no error */); |
| 483 } |
| 484 |
| 485 // Continues selecting certificates on the IO thread. Used by |
| 486 // SelectClientCertificates(). |
| 487 void SelectCertificatesOnIOThread(scoped_ptr<SelectCertificatesState> state) { |
| 488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 489 state->cert_store_.reset(new net::ClientCertStoreChromeOS( |
| 490 make_scoped_ptr(new chromeos::ClientCertFilterChromeOS( |
| 491 state->use_system_key_slot_, state->username_hash_)), |
| 492 net::ClientCertStoreChromeOS::PasswordDelegateFactory())); |
| 493 |
| 494 state->certs_.reset(new net::CertificateList); |
| 495 |
| 496 SelectCertificatesState* state_ptr = state.get(); |
| 497 state_ptr->cert_store_->GetClientCerts( |
| 498 *state_ptr->cert_request_info_, state_ptr->certs_.get(), |
| 499 base::Bind(&DidSelectCertificatesOnIOThread, base::Passed(&state))); |
| 500 } |
| 501 |
422 // Filters the obtained certificates on a worker thread. Used by | 502 // Filters the obtained certificates on a worker thread. Used by |
423 // DidGetCertificates(). | 503 // DidGetCertificates(). |
424 void FilterCertificatesOnWorkerThread(scoped_ptr<GetCertificatesState> state) { | 504 void FilterCertificatesOnWorkerThread(scoped_ptr<GetCertificatesState> state) { |
425 scoped_ptr<net::CertificateList> client_certs(new net::CertificateList); | 505 scoped_ptr<net::CertificateList> client_certs(new net::CertificateList); |
426 for (net::CertificateList::const_iterator it = state->certs_->begin(); | 506 for (net::CertificateList::const_iterator it = state->certs_->begin(); |
427 it != state->certs_->end(); | 507 it != state->certs_->end(); |
428 ++it) { | 508 ++it) { |
429 net::X509Certificate::OSCertHandle cert_handle = (*it)->os_cert_handle(); | 509 net::X509Certificate::OSCertHandle cert_handle = (*it)->os_cert_handle(); |
430 crypto::ScopedPK11Slot cert_slot(PK11_KeyForCertExists(cert_handle, | 510 crypto::ScopedPK11Slot cert_slot(PK11_KeyForCertExists(cert_handle, |
431 NULL, // keyPtr | 511 NULL, // keyPtr |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 | 669 |
590 // The NSSCertDatabase object is not required. But in case it's not available | 670 // The NSSCertDatabase object is not required. But in case it's not available |
591 // we would get more informative error messages and we can double check that | 671 // we would get more informative error messages and we can double check that |
592 // we use a key of the correct token. | 672 // we use a key of the correct token. |
593 GetCertDatabase(token_id, | 673 GetCertDatabase(token_id, |
594 base::Bind(&RSASignWithDB, base::Passed(&state)), | 674 base::Bind(&RSASignWithDB, base::Passed(&state)), |
595 browser_context, | 675 browser_context, |
596 state_ptr); | 676 state_ptr); |
597 } | 677 } |
598 | 678 |
| 679 void SelectClientCertificates(const ClientCertificateRequest& request, |
| 680 const SelectCertificatesCallback& callback, |
| 681 content::BrowserContext* browser_context) { |
| 682 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 683 |
| 684 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( |
| 685 new net::SSLCertRequestInfo); |
| 686 cert_request_info->cert_key_types = request.certificate_key_types; |
| 687 cert_request_info->cert_authorities = request.certificate_authorities; |
| 688 |
| 689 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
| 690 Profile::FromBrowserContext(browser_context)); |
| 691 |
| 692 // Use the device-wide system key slot only if the user is of the same |
| 693 // domain as the device is registered to. |
| 694 policy::BrowserPolicyConnectorChromeOS* connector = |
| 695 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
| 696 bool use_system_key_slot = connector->GetUserAffiliation(user->email()) == |
| 697 policy::USER_AFFILIATION_MANAGED; |
| 698 |
| 699 scoped_ptr<SelectCertificatesState> state(new SelectCertificatesState( |
| 700 user->username_hash(), use_system_key_slot, cert_request_info, callback)); |
| 701 |
| 702 BrowserThread::PostTask( |
| 703 BrowserThread::IO, FROM_HERE, |
| 704 base::Bind(&SelectCertificatesOnIOThread, base::Passed(&state))); |
| 705 } |
| 706 |
599 } // namespace subtle | 707 } // namespace subtle |
600 | 708 |
601 void GetCertificates(const std::string& token_id, | 709 void GetCertificates(const std::string& token_id, |
602 const GetCertificatesCallback& callback, | 710 const GetCertificatesCallback& callback, |
603 BrowserContext* browser_context) { | 711 BrowserContext* browser_context) { |
604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 712 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
605 scoped_ptr<GetCertificatesState> state(new GetCertificatesState(callback)); | 713 scoped_ptr<GetCertificatesState> state(new GetCertificatesState(callback)); |
606 // Get the pointer to |state| before base::Passed releases |state|. | 714 // Get the pointer to |state| before base::Passed releases |state|. |
607 NSSOperationState* state_ptr = state.get(); | 715 NSSOperationState* state_ptr = state.get(); |
608 GetCertDatabase(token_id, | 716 GetCertDatabase(token_id, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 NSSOperationState* state_ptr = state.get(); | 764 NSSOperationState* state_ptr = state.get(); |
657 GetCertDatabase(std::string() /* don't get any specific slot */, | 765 GetCertDatabase(std::string() /* don't get any specific slot */, |
658 base::Bind(&GetTokensWithDB, base::Passed(&state)), | 766 base::Bind(&GetTokensWithDB, base::Passed(&state)), |
659 browser_context, | 767 browser_context, |
660 state_ptr); | 768 state_ptr); |
661 } | 769 } |
662 | 770 |
663 } // namespace platform_keys | 771 } // namespace platform_keys |
664 | 772 |
665 } // namespace chromeos | 773 } // namespace chromeos |
OLD | NEW |