OLD | NEW |
| (Empty) |
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 | |
5 #ifndef CHROMEOS_NETWORK_CERT_LOADER_H_ | |
6 #define CHROMEOS_NETWORK_CERT_LOADER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/observer_list_threadsafe.h" | |
14 #include "base/threading/thread_checker.h" | |
15 #include "chromeos/chromeos_export.h" | |
16 #include "chromeos/dbus/dbus_method_call_status.h" | |
17 #include "chromeos/login/login_state.h" | |
18 #include "chromeos/network/network_handler.h" | |
19 #include "net/cert/cert_database.h" | |
20 #include "net/cert/x509_certificate.h" | |
21 | |
22 namespace crypto { | |
23 class SymmetricKey; | |
24 } | |
25 | |
26 namespace chromeos { | |
27 | |
28 // This class is responsible for initializing the TPM token and loading | |
29 // certificates once the TPM is initialized. It is expected to be constructed | |
30 // on the UI thread and public methods should all be called from the UI thread. | |
31 // When certificates have been loaded (after login completes), or the cert | |
32 // database changes, observers are called with OnCertificatesLoaded(). | |
33 class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer, | |
34 public LoginState::Observer { | |
35 public: | |
36 class Observer { | |
37 public: | |
38 virtual ~Observer() {} | |
39 | |
40 // Called when the certificates, passed for convenience as |cert_list|, | |
41 // have completed loading. |initial_load| is true the first time this | |
42 // is called. | |
43 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, | |
44 bool initial_load) = 0; | |
45 | |
46 protected: | |
47 Observer() {} | |
48 | |
49 private: | |
50 DISALLOW_COPY_AND_ASSIGN(Observer); | |
51 }; | |
52 | |
53 virtual ~CertLoader(); | |
54 | |
55 void AddObserver(CertLoader::Observer* observer); | |
56 void RemoveObserver(CertLoader::Observer* observer); | |
57 | |
58 // Returns true when the certificate list has been requested but not loaded. | |
59 bool CertificatesLoading() const; | |
60 | |
61 // Returns true if the TPM is available for hardware-backed certificates. | |
62 bool IsHardwareBacked() const; | |
63 | |
64 std::string GetPkcs11IdForCert(const net::X509Certificate& cert) const; | |
65 | |
66 bool certificates_loaded() const { return certificates_loaded_; } | |
67 | |
68 // TPM info is only valid once the TPM is available (IsHardwareBacked is | |
69 // true). Otherwise empty strings will be returned. | |
70 const std::string& tpm_token_name() const { return tpm_token_name_; } | |
71 const std::string& tpm_token_slot() const { return tpm_token_slot_; } | |
72 const std::string& tpm_user_pin() const { return tpm_user_pin_; } | |
73 | |
74 // This will be empty until certificates_loaded() is true. | |
75 const net::CertificateList& cert_list() const { return cert_list_; } | |
76 | |
77 private: | |
78 friend class NetworkHandler; | |
79 CertLoader(); | |
80 | |
81 void RequestCertificates(); | |
82 | |
83 // This is the cyclic chain of callbacks to initialize the TPM token and to | |
84 // kick off the update of the certificate list. | |
85 void InitializeTokenAndLoadCertificates(); | |
86 void RetryTokenInitializationLater(); | |
87 void OnTpmIsEnabled(DBusMethodCallStatus call_status, | |
88 bool tpm_is_enabled); | |
89 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | |
90 bool is_tpm_token_ready); | |
91 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, | |
92 const std::string& token_name, | |
93 const std::string& user_pin); | |
94 void InitializeNSSForTPMToken(); | |
95 | |
96 // These calls handle the updating of the certificate list after the TPM token | |
97 // was initialized. | |
98 void StartLoadCertificates(); | |
99 void UpdateCertificates(net::CertificateList* cert_list); | |
100 | |
101 void NotifyCertificatesLoaded(bool initial_load); | |
102 | |
103 // net::CertDatabase::Observer | |
104 virtual void OnCertTrustChanged(const net::X509Certificate* cert) OVERRIDE; | |
105 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE; | |
106 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE; | |
107 | |
108 // LoginState::Observer | |
109 virtual void LoggedInStateChanged(LoginState::LoggedInState state) OVERRIDE; | |
110 | |
111 ObserverList<Observer> observers_; | |
112 | |
113 bool certificates_requested_; | |
114 bool certificates_loaded_; | |
115 bool certificates_update_required_; | |
116 bool certificates_update_running_; | |
117 | |
118 // The states are traversed in this order but some might get omitted or never | |
119 // be left. | |
120 enum TPMTokenState { | |
121 TPM_STATE_UNKNOWN, | |
122 TPM_DISABLED, | |
123 TPM_ENABLED, | |
124 TPM_TOKEN_READY, | |
125 TPM_TOKEN_INFO_RECEIVED, | |
126 TPM_TOKEN_NSS_INITIALIZED, | |
127 }; | |
128 TPMTokenState tpm_token_state_; | |
129 | |
130 // The current request delay before the next attempt to initialize the | |
131 // TPM. Will be adapted after each attempt. | |
132 base::TimeDelta tpm_request_delay_; | |
133 | |
134 // Cached TPM token info. | |
135 std::string tpm_token_name_; | |
136 std::string tpm_token_slot_; | |
137 std::string tpm_user_pin_; | |
138 | |
139 // Cached Certificates. | |
140 net::CertificateList cert_list_; | |
141 | |
142 base::ThreadChecker thread_checker_; | |
143 | |
144 // This factory should be used only for callbacks during TPMToken | |
145 // initialization. | |
146 base::WeakPtrFactory<CertLoader> initialize_token_factory_; | |
147 | |
148 // This factory should be used only for callbacks during updating the | |
149 // certificate list. | |
150 base::WeakPtrFactory<CertLoader> update_certificates_factory_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(CertLoader); | |
153 }; | |
154 | |
155 } // namespace chromeos | |
156 | |
157 #endif // CHROMEOS_NETWORK_CERT_LOADER_H_ | |
OLD | NEW |