| 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 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace chromeos { | 26 namespace chromeos { |
| 27 | 27 |
| 28 // This class is responsible for loading certificates once the TPM is | 28 // This class is responsible for loading certificates once the TPM is |
| 29 // initialized. It is expected to be constructed on the UI thread and public | 29 // initialized. It is expected to be constructed on the UI thread and public |
| 30 // methods should all be called from the UI thread. | 30 // methods should all be called from the UI thread. |
| 31 // When certificates have been loaded (after login completes and tpm token is | 31 // When certificates have been loaded (after login completes and tpm token is |
| 32 // initialized), or the cert database changes, observers are called with | 32 // initialized), or the cert database changes, observers are called with |
| 33 // OnCertificatesLoaded(). | 33 // OnCertificatesLoaded(). |
| 34 class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer { | 34 class CHROMEOS_EXPORT CertLoader { |
| 35 public: | 35 public: |
| 36 class Observer { | 36 class Observer { |
| 37 public: | 37 public: |
| 38 // Called when the certificates, passed for convenience as |all_certs|, | 38 // Called when the certificates, passed for convenience as |all_certs|, |
| 39 // have completed loading. |initial_load| is true the first time this | 39 // have completed loading. |initial_load| is true the first time this |
| 40 // is called. | 40 // is called. It will be false if this is called because another slot has |
| 41 // been added to CertLoader's data sources. |
| 41 virtual void OnCertificatesLoaded(const net::CertificateList& all_certs, | 42 virtual void OnCertificatesLoaded(const net::CertificateList& all_certs, |
| 42 bool initial_load) = 0; | 43 bool initial_load) = 0; |
| 43 | 44 |
| 44 protected: | 45 protected: |
| 45 virtual ~Observer() {} | 46 virtual ~Observer() {} |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 // Sets the global instance. Must be called before any calls to Get(). | 49 // Sets the global instance. Must be called before any calls to Get(). |
| 49 static void Initialize(); | 50 static void Initialize(); |
| 50 | 51 |
| 51 // Destroys the global instance. | 52 // Destroys the global instance. |
| 52 static void Shutdown(); | 53 static void Shutdown(); |
| 53 | 54 |
| 54 // Gets the global instance. Initialize() must be called first. | 55 // Gets the global instance. Initialize() must be called first. |
| 55 static CertLoader* Get(); | 56 static CertLoader* Get(); |
| 56 | 57 |
| 57 // Returns true if the global instance has been initialized. | 58 // Returns true if the global instance has been initialized. |
| 58 static bool IsInitialized(); | 59 static bool IsInitialized(); |
| 59 | 60 |
| 60 // Returns the PKCS#11 attribute CKA_ID for a certificate as an upper-case | 61 // Returns the PKCS#11 attribute CKA_ID for a certificate as an upper-case |
| 61 // hex string and sets |slot_id| to the id of the containing slot, or returns | 62 // hex string and sets |slot_id| to the id of the containing slot, or returns |
| 62 // an empty string and doesn't modify |slot_id| if the PKCS#11 id could not be | 63 // an empty string and doesn't modify |slot_id| if the PKCS#11 id could not be |
| 63 // determined. | 64 // determined. |
| 64 static std::string GetPkcs11IdAndSlotForCert(const net::X509Certificate& cert, | 65 static std::string GetPkcs11IdAndSlotForCert(const net::X509Certificate& cert, |
| 65 int* slot_id); | 66 int* slot_id); |
| 66 | 67 |
| 67 // Starts the CertLoader with the NSS cert database. | 68 // Starts the CertLoader with the passed user NSS cert database. |
| 68 // The CertLoader will _not_ take the ownership of the database, but it | 69 // The CertLoader will _not_ take the ownership of the database, but it |
| 69 // expects it to stay alive at least until the shutdown starts on the main | 70 // expects it to stay alive at least until the shutdown starts on the main |
| 70 // thread. This assumes that |StartWithNSSDB| and other methods directly | 71 // thread. This assumes that |StartWithUserNSSDB| and other methods directly |
| 71 // using |database_| are not called during shutdown. | 72 // using |database_| are not called during shutdown. |
| 72 void StartWithNSSDB(net::NSSCertDatabase* database); | 73 void StartWithUserNSSDB(net::NSSCertDatabase* user_database); |
| 74 |
| 75 // Starts the CertLoader with the passed system NSS cert database. |
| 76 // The CertLoader will _not_ take ownership of the database - see comment on |
| 77 // StartWithUserNSSDB. |
| 78 void StartWithSystemNSSDB(net::NSSCertDatabase* system_slot_database); |
| 73 | 79 |
| 74 void AddObserver(CertLoader::Observer* observer); | 80 void AddObserver(CertLoader::Observer* observer); |
| 75 void RemoveObserver(CertLoader::Observer* observer); | 81 void RemoveObserver(CertLoader::Observer* observer); |
| 76 | 82 |
| 77 // Returns true if |cert| is hardware backed. See also | 83 // Returns true if |cert| is hardware backed. See also |
| 78 // ForceHardwareBackedForTesting(). | 84 // ForceHardwareBackedForTesting(). |
| 79 static bool IsCertificateHardwareBacked(const net::X509Certificate* cert); | 85 static bool IsCertificateHardwareBacked(const net::X509Certificate* cert); |
| 80 | 86 |
| 81 // Returns true when the certificate list has been requested but not loaded. | 87 // Returns true when the certificate list has been requested but not loaded. |
| 82 bool CertificatesLoading() const; | 88 bool CertificatesLoading() const; |
| 83 | 89 |
| 84 bool certificates_loaded() const { return certificates_loaded_; } | 90 bool certificates_loaded() const; |
| 85 | 91 |
| 86 // Returns all certificates. This will be empty until certificates_loaded() is | 92 // Returns all certificates. This will be empty until certificates_loaded() is |
| 87 // true. | 93 // true. |
| 88 const net::CertificateList& all_certs() const { | 94 const net::CertificateList& all_certs() const { |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 return *all_certs_; | 96 return *all_certs_; |
| 91 } | 97 } |
| 92 | 98 |
| 93 // Returns certificates from the system token. This will be empty until | 99 // Returns certificates from the system token. This will be empty until |
| 94 // certificates_loaded() is true. | 100 // certificates_loaded() is true. |
| 95 const net::CertificateList& system_certs() const { | 101 const net::CertificateList& system_certs() const { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 return *system_certs_; | 103 return *system_certs_; |
| 98 } | 104 } |
| 99 | 105 |
| 100 // Called in tests if |IsCertificateHardwareBacked()| should always return | 106 // Called in tests if |IsCertificateHardwareBacked()| should always return |
| 101 // true. | 107 // true. |
| 102 static void ForceHardwareBackedForTesting(); | 108 static void ForceHardwareBackedForTesting(); |
| 103 | 109 |
| 104 private: | 110 private: |
| 111 class CertCache; |
| 112 |
| 105 CertLoader(); | 113 CertLoader(); |
| 106 ~CertLoader() override; | 114 ~CertLoader(); |
| 107 | 115 |
| 108 // Trigger a certificate load. If a certificate loading task is already in | 116 // Called by |system_cert_cache_| or |user_cert_cache| when these had an |
| 109 // progress, will start a reload once the current task is finished. | 117 // update. |
| 110 void LoadCertificates(); | 118 void CacheUpdated(); |
| 111 | |
| 112 // Called when the underlying NSS database finished loading certificates. | |
| 113 void CertificatesLoaded(std::unique_ptr<net::CertificateList> all_certs); | |
| 114 | 119 |
| 115 // Called if a certificate load task is finished. | 120 // Called if a certificate load task is finished. |
| 116 void UpdateCertificates(std::unique_ptr<net::CertificateList> all_certs, | 121 void UpdateCertificates(std::unique_ptr<net::CertificateList> all_certs, |
| 117 std::unique_ptr<net::CertificateList> system_certs); | 122 std::unique_ptr<net::CertificateList> system_certs); |
| 118 | 123 |
| 119 void NotifyCertificatesLoaded(bool initial_load); | 124 void NotifyCertificatesLoaded(bool initial_load); |
| 120 | 125 |
| 121 // net::CertDatabase::Observer | 126 // True if the initial load of CertLoader is still pending. |
| 122 void OnCertDBChanged() override; | 127 bool pending_initial_load_; |
| 123 | 128 |
| 124 base::ObserverList<Observer> observers_; | 129 base::ObserverList<Observer> observers_; |
| 125 | 130 |
| 126 // Flags describing current CertLoader state. | 131 std::unique_ptr<CertCache> system_cert_cache_; |
| 127 bool certificates_loaded_; | 132 std::unique_ptr<CertCache> user_cert_cache_; |
| 128 bool certificates_update_required_; | |
| 129 bool certificates_update_running_; | |
| 130 | 133 |
| 131 // The user-specific NSS certificate database from which the certificates | 134 // Cached certificates loaded from the database(s). |
| 132 // should be loaded. | |
| 133 net::NSSCertDatabase* database_; | |
| 134 | |
| 135 // Cached certificates loaded from the database. | |
| 136 std::unique_ptr<net::CertificateList> all_certs_; | 135 std::unique_ptr<net::CertificateList> all_certs_; |
| 137 | 136 |
| 138 // Cached certificates from system token. Currently this is a sublist of | 137 // Cached certificates from system token. |
| 139 // |all_certs_|. | |
| 140 std::unique_ptr<net::CertificateList> system_certs_; | 138 std::unique_ptr<net::CertificateList> system_certs_; |
| 141 | 139 |
| 142 base::ThreadChecker thread_checker_; | 140 base::ThreadChecker thread_checker_; |
| 143 | 141 |
| 144 base::WeakPtrFactory<CertLoader> weak_factory_; | 142 base::WeakPtrFactory<CertLoader> weak_factory_; |
| 145 | 143 |
| 146 DISALLOW_COPY_AND_ASSIGN(CertLoader); | 144 DISALLOW_COPY_AND_ASSIGN(CertLoader); |
| 147 }; | 145 }; |
| 148 | 146 |
| 149 } // namespace chromeos | 147 } // namespace chromeos |
| 150 | 148 |
| 151 #endif // CHROMEOS_CERT_LOADER_H_ | 149 #endif // CHROMEOS_CERT_LOADER_H_ |
| OLD | NEW |