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