[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 5 | #ifndef CHROMEOS_CERT_LOADER_H_ |
| 6 | #define CHROMEOS_CERT_LOADER_H_ |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 13 | #include "base/observer_list.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 14 | #include "base/threading/thread_checker.h" |
| 15 | #include "chromeos/chromeos_export.h" |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 16 | #include "chromeos/tpm_token_loader.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 17 | #include "net/cert/cert_database.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 18 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 19 | namespace base { |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 20 | class TaskRunner; |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 21 | } |
| 22 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 23 | namespace net { |
| 24 | class X509Certificate; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | namespace chromeos { |
| 28 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 29 | // This class is responsible for loading certificates once the TPM is |
| 30 | // initialized. It is expected to be constructed on the UI thread and public |
| 31 | // methods should all be called from the UI thread. |
| 32 | // When certificates have been loaded (after login completes and tpm token is |
| 33 | // initialized), or the cert database changes, observers are called with |
| 34 | // OnCertificatesLoaded(). |
| 35 | // TODO(tbarzic): Remove direct dependency on TPMTokenLoader. The reason |
| 36 | // TPMTokenLoader has to be observed is to make sure singleton NSS DB is |
| 37 | // initialized before certificate loading starts. CertLoader should use |
| 38 | // (primary) user specific NSS DB, whose loading already takes this into |
| 39 | // account (crypto::GetPrivateSlotForChromeOSUser waits until TPM token is |
| 40 | // ready). |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 41 | class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer, |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 42 | public TPMTokenLoader::Observer { |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 43 | public: |
| 44 | class Observer { |
| 45 | public: |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 46 | // Called when the certificates, passed for convenience as |cert_list|, |
| 47 | // have completed loading. |initial_load| is true the first time this |
| 48 | // is called. |
| 49 | virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, |
| 50 | bool initial_load) = 0; |
[email protected] | bba6b96 | 2014-01-24 00:48:10 | [diff] [blame^] | 51 | |
| 52 | protected: |
| 53 | virtual ~Observer() {} |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 54 | }; |
| 55 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 56 | // Sets the global instance. Must be called before any calls to Get(). |
| 57 | static void Initialize(); |
| 58 | |
| 59 | // Destroys the global instance. |
| 60 | static void Shutdown(); |
| 61 | |
| 62 | // Gets the global instance. Initialize() must be called first. |
| 63 | static CertLoader* Get(); |
| 64 | |
| 65 | // Returns true if the global instance has been initialized. |
| 66 | static bool IsInitialized(); |
| 67 | |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 68 | static std::string GetPkcs11IdForCert(const net::X509Certificate& cert); |
| 69 | |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 70 | // Sets the task runner that any slow calls will be made from, e.g. calls |
| 71 | // to the NSS database. If not set, uses base::WorkerPool. |
| 72 | void SetSlowTaskRunnerForTest( |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 73 | const scoped_refptr<base::TaskRunner>& task_runner); |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 74 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 75 | void AddObserver(CertLoader::Observer* observer); |
| 76 | void RemoveObserver(CertLoader::Observer* observer); |
| 77 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 78 | // Returns true if the TPM is available for hardware-backed certificates. |
| 79 | bool IsHardwareBacked() const; |
| 80 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 81 | // Returns true when the certificate list has been requested but not loaded. |
| 82 | bool CertificatesLoading() const; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 83 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 84 | bool certificates_loaded() const { return certificates_loaded_; } |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 85 | |
| 86 | // This will be empty until certificates_loaded() is true. |
| 87 | const net::CertificateList& cert_list() const { return cert_list_; } |
| 88 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 89 | // Getters for cached TPM token info. |
| 90 | std::string tpm_user_pin() const { return tpm_user_pin_; } |
| 91 | std::string tpm_token_name() const { return tpm_token_name_; } |
| 92 | int tpm_token_slot_id() const { return tpm_token_slot_id_; } |
| 93 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 94 | private: |
| 95 | CertLoader(); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 96 | virtual ~CertLoader(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 97 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 98 | // Starts certificate loading. |
| 99 | void RequestCertificates(); |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 100 | |
| 101 | // Trigger a certificate load. If a certificate loading task is already in |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 102 | // progress, will start a reload once the current task finished. |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 103 | void LoadCertificates(); |
| 104 | |
| 105 | // Called if a certificate load task is finished. |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 106 | void UpdateCertificates(net::CertificateList* cert_list); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 107 | |
| 108 | void NotifyCertificatesLoaded(bool initial_load); |
| 109 | |
| 110 | // net::CertDatabase::Observer |
[email protected] | c157b2e2 | 2013-10-31 01:38:33 | [diff] [blame] | 111 | virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 112 | virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE; |
| 113 | virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE; |
| 114 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 115 | // chromeos::TPMTokenLoader::Observer |
| 116 | virtual void OnTPMTokenReady(const std::string& tpm_user_pin, |
| 117 | const std::string& tpm_token_name, |
| 118 | int tpm_token_slot_id) OVERRIDE; |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 119 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 120 | ObserverList<Observer> observers_; |
| 121 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 122 | // Flags describing current CertLoader state. |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 123 | bool certificates_requested_; |
| 124 | bool certificates_loaded_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 125 | bool certificates_update_required_; |
| 126 | bool certificates_update_running_; |
| 127 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 128 | // Cached TPM token info. Set when the |OnTPMTokenReady| gets called. |
| 129 | std::string tpm_user_pin_; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 130 | std::string tpm_token_name_; |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 131 | int tpm_token_slot_id_; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 132 | |
| 133 | // Cached Certificates. |
| 134 | net::CertificateList cert_list_; |
| 135 | |
| 136 | base::ThreadChecker thread_checker_; |
| 137 | |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 138 | // TaskRunner for other slow tasks. May be set in tests. |
| 139 | scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; |
| 140 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 141 | base::WeakPtrFactory<CertLoader> weak_factory_; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 142 | |
| 143 | DISALLOW_COPY_AND_ASSIGN(CertLoader); |
| 144 | }; |
| 145 | |
| 146 | } // namespace chromeos |
| 147 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 148 | #endif // CHROMEOS_CERT_LOADER_H_ |