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" |
pneubeck (no reviews)
2014/01/24 13:18:02
missing #include "base/compiler_specific.h"
tbarzic
2014/01/25 00:26:27
Done.
| |
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; |
pneubeck (no reviews)
2014/01/24 13:18:02
missing forward declaration of net::CertificateLis
tbarzic
2014/01/25 00:26:27
Done.
| |
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. |
64 void StartWithNSSDB(net::NSSCertDatabase* database); | |
65 | |
66 // Sets the task runner that any slow calls will be made from, e.g. calls | |
pneubeck (no reviews)
2014/01/24 13:18:02
nit: indentation
tbarzic
2014/01/25 00:26:27
Done.
| |
67 // to the NSS database. If not set, uses base::WorkerPool. | |
71 void SetSlowTaskRunnerForTest( | 68 void SetSlowTaskRunnerForTest( |
72 const scoped_refptr<base::TaskRunner>& task_runner); | 69 const scoped_refptr<base::TaskRunner>& task_runner); |
73 | 70 |
74 void AddObserver(CertLoader::Observer* observer); | 71 void AddObserver(CertLoader::Observer* observer); |
75 void RemoveObserver(CertLoader::Observer* observer); | 72 void RemoveObserver(CertLoader::Observer* observer); |
76 | 73 |
77 // Returns true if the TPM is available for hardware-backed certificates. | 74 // Whether the certificate is hardware backed. Returns false if the CertLoader |
78 bool IsHardwareBacked() const; | 75 // was not yet started (both |CertificatesLoading()| and |
76 // |certificates_loaded()| are false). | |
77 bool IsCertificateHardwareBacked(const net::X509Certificate* cert) const; | |
79 | 78 |
80 // Returns true when the certificate list has been requested but not loaded. | 79 // Returns true when the certificate list has been requested but not loaded. |
81 bool CertificatesLoading() const; | 80 bool CertificatesLoading() const; |
82 | 81 |
83 bool certificates_loaded() const { return certificates_loaded_; } | 82 bool certificates_loaded() const { return certificates_loaded_; } |
84 | 83 |
85 // This will be empty until certificates_loaded() is true. | 84 // This will be empty until certificates_loaded() is true. |
86 const net::CertificateList& cert_list() const { return cert_list_; } | 85 const net::CertificateList& cert_list() const { return cert_list_; } |
87 | 86 |
88 // Getters for cached TPM token info. | 87 // Note that |is_hardware_backed()| will return false if the CertLoader was |
89 std::string tpm_user_pin() const { return tpm_user_pin_; } | 88 // not started. |
90 std::string tpm_token_name() const { return tpm_token_name_; } | 89 bool is_hardware_backed() const { return is_hardware_backed_; } |
90 void set_hardware_backed_for_test() { hardware_backed_for_test_ = true; } | |
91 int tpm_token_slot_id() const { return tpm_token_slot_id_; } | 91 int tpm_token_slot_id() const { return tpm_token_slot_id_; } |
92 | 92 |
93 private: | 93 private: |
94 CertLoader(); | 94 CertLoader(); |
95 virtual ~CertLoader(); | 95 virtual ~CertLoader(); |
96 | 96 |
97 // Starts certificate loading. | |
98 void RequestCertificates(); | |
99 | |
100 // Trigger a certificate load. If a certificate loading task is already in | 97 // Trigger a certificate load. If a certificate loading task is already in |
101 // progress, will start a reload once the current task finished. | 98 // progress, will start a reload once the current task is finished. |
102 void LoadCertificates(); | 99 void LoadCertificates(); |
103 | 100 |
104 // Called if a certificate load task is finished. | 101 // Called if a certificate load task is finished. |
105 void UpdateCertificates(net::CertificateList* cert_list); | 102 void UpdateCertificates(net::CertificateList* cert_list); |
106 | 103 |
107 void NotifyCertificatesLoaded(bool initial_load); | 104 void NotifyCertificatesLoaded(bool initial_load); |
108 | 105 |
109 // net::CertDatabase::Observer | 106 // net::CertDatabase::Observer |
110 virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE; | 107 virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE; |
111 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE; | 108 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE; |
112 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE; | 109 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE; |
113 | 110 |
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_; | 111 ObserverList<Observer> observers_; |
120 | 112 |
121 // Flags describing current CertLoader state. | 113 // Flags describing current CertLoader state. |
122 bool certificates_requested_; | 114 bool certificates_requested_; |
123 bool certificates_loaded_; | 115 bool certificates_loaded_; |
124 bool certificates_update_required_; | 116 bool certificates_update_required_; |
125 bool certificates_update_running_; | 117 bool certificates_update_running_; |
126 | 118 |
127 // Cached TPM token info. Set when the |OnTPMTokenReady| gets called. | 119 // The user-specific NSS certificate database from which the certificates |
128 std::string tpm_user_pin_; | 120 // should be loaded. |
129 std::string tpm_token_name_; | 121 net::NSSCertDatabase* database_; |
122 | |
123 // The user NSS database's private slot id. | |
130 int tpm_token_slot_id_; | 124 int tpm_token_slot_id_; |
131 | 125 |
132 // Cached Certificates. | 126 // Whether |database_| is hardware backed. |
127 bool is_hardware_backed_; | |
128 bool hardware_backed_for_test_; | |
129 | |
130 // Cached Certificates loaded from the database. | |
133 net::CertificateList cert_list_; | 131 net::CertificateList cert_list_; |
134 | 132 |
135 base::ThreadChecker thread_checker_; | 133 base::ThreadChecker thread_checker_; |
136 | 134 |
137 // TaskRunner for other slow tasks. May be set in tests. | 135 // TaskRunner that, if set, replaces base::WorkerPool. Should only be set in |
136 // tests. | |
138 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; | 137 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; |
139 | 138 |
140 base::WeakPtrFactory<CertLoader> weak_factory_; | 139 base::WeakPtrFactory<CertLoader> weak_factory_; |
141 | 140 |
142 DISALLOW_COPY_AND_ASSIGN(CertLoader); | 141 DISALLOW_COPY_AND_ASSIGN(CertLoader); |
143 }; | 142 }; |
144 | 143 |
145 } // namespace chromeos | 144 } // namespace chromeos |
146 | 145 |
147 #endif // CHROMEOS_CERT_LOADER_H_ | 146 #endif // CHROMEOS_CERT_LOADER_H_ |
OLD | NEW |