OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_TPM_TOKEN_LOADER_H_ | 5 #ifndef CHROMEOS_TPM_TOKEN_LOADER_H_ |
6 #define CHROMEOS_TPM_TOKEN_LOADER_H_ | 6 #define CHROMEOS_TPM_TOKEN_LOADER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // should all be called from the UI thread. When the TPM token is loaded, | 28 // should all be called from the UI thread. When the TPM token is loaded, |
29 // or if the TPM should stay disabled for the session, the observers are | 29 // or if the TPM should stay disabled for the session, the observers are |
30 // notified using |OnTPMTokenReady|. | 30 // notified using |OnTPMTokenReady|. |
31 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { | 31 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { |
32 public: | 32 public: |
33 class Observer { | 33 class Observer { |
34 public: | 34 public: |
35 virtual ~Observer() {} | 35 virtual ~Observer() {} |
36 | 36 |
37 // Called when the TPM token initialization is done or the case where TPM | 37 // Called when the TPM token initialization is done or the case where TPM |
38 // should stay disabled is detected (e.g. on guest login). If TPM is | 38 // should stay disabled is detected (e.g. on guest login). |
39 // disabled, |tpm_user_pin|, |tpm_token_name| and |tpm_token_slot_id| will | 39 virtual void OnTPMTokenReady() = 0; |
40 // not be set. | |
41 virtual void OnTPMTokenReady(const std::string& tpm_user_pin, | |
42 const std::string& tpm_token_name, | |
43 int tpm_token_slot_id) = 0; | |
44 }; | 40 }; |
45 | 41 |
46 // Sets the global instance. Must be called before any calls to Get(). | 42 // Sets the global instance. Must be called before any calls to Get(). |
47 // The global instance will immediately start observing |LoginState|. | 43 // The global instance will immediately start observing |LoginState|. |
48 static void Initialize(); | 44 static void Initialize(); |
49 | 45 |
| 46 // Sets the global. stubbed out, instance. To be used in tests. |
| 47 static void InitializeForTest(); |
| 48 |
50 // Destroys the global instance. | 49 // Destroys the global instance. |
51 static void Shutdown(); | 50 static void Shutdown(); |
52 | 51 |
53 // Gets the global instance. Initialize() must be called before this. | 52 // Gets the global instance. Initialize() must be called before this. |
54 static TPMTokenLoader* Get(); | 53 static TPMTokenLoader* Get(); |
55 | 54 |
56 // Returns true if the global instance has been initialized. | 55 // Returns true if the global instance has been initialized. |
57 static bool IsInitialized(); | 56 static bool IsInitialized(); |
58 | 57 |
59 // By default, TPMTokenLoader tries to load the TPMToken only if running | |
60 // in a ChromeOS environment. Tests can call this function after Initialize() | |
61 // and before SetCryptoTaskRunner() to enable the TPM initialization. | |
62 void InitializeTPMForTest(); | |
63 | |
64 // |crypto_task_runner| is the task runner that any synchronous crypto calls | 58 // |crypto_task_runner| is the task runner that any synchronous crypto calls |
65 // should be made from, e.g. in Chrome this is the IO thread. Must be called | 59 // should be made from, e.g. in Chrome this is the IO thread. Must be called |
66 // after the thread is started. When called, this will attempt to start TPM | 60 // after the thread is started. When called, this will attempt to start TPM |
67 // token loading. | 61 // token loading. |
68 void SetCryptoTaskRunner( | 62 void SetCryptoTaskRunner( |
69 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); | 63 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); |
70 | 64 |
71 void AddObserver(TPMTokenLoader::Observer* observer); | 65 void AddObserver(TPMTokenLoader::Observer* observer); |
72 void RemoveObserver(TPMTokenLoader::Observer* observer); | 66 void RemoveObserver(TPMTokenLoader::Observer* observer); |
73 | 67 |
74 // Checks if the TPM token in ready to be used. | 68 // Checks if the TPM token in ready to be used. |
75 bool IsTPMTokenReady() const; | 69 bool IsTPMTokenReady() const; |
76 | 70 |
| 71 std::string tpm_user_pin() const { return tpm_user_pin_; } |
| 72 |
77 private: | 73 private: |
78 TPMTokenLoader(); | 74 explicit TPMTokenLoader(bool for_test); |
79 virtual ~TPMTokenLoader(); | 75 virtual ~TPMTokenLoader(); |
80 | 76 |
81 // Starts tpm token initialization if the user is logged in and the crypto | 77 // Starts tpm token initialization if the user is logged in and the crypto |
82 // task runner is set. | 78 // task runner is set. |
83 void MaybeStartTokenInitialization(); | 79 void MaybeStartTokenInitialization(); |
84 | 80 |
85 // This is the cyclic chain of callbacks to initialize the TPM token. | 81 // This is the cyclic chain of callbacks to initialize the TPM token. |
86 void ContinueTokenInitialization(); | 82 void ContinueTokenInitialization(); |
87 void OnPersistentNSSDBOpened(); | 83 void OnPersistentNSSDBOpened(); |
88 void OnTpmIsEnabled(DBusMethodCallStatus call_status, | 84 void OnTpmIsEnabled(DBusMethodCallStatus call_status, |
89 bool tpm_is_enabled); | 85 bool tpm_is_enabled); |
90 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | 86 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, |
91 bool is_tpm_token_ready); | 87 bool is_tpm_token_ready); |
92 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, | 88 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, |
93 const std::string& token_name, | 89 const std::string& token_name, |
94 const std::string& user_pin, | 90 const std::string& user_pin, |
95 int token_slot_id); | 91 int token_slot_id); |
96 void OnTPMTokenInitialized(bool success); | 92 void OnTPMTokenInitialized(bool success); |
97 | 93 |
98 // If token initialization step fails (e.g. if tpm token is not yet ready) | 94 // If token initialization step fails (e.g. if tpm token is not yet ready) |
99 // schedules the initialization step retry attempt after a timeout. | 95 // schedules the initialization step retry attempt after a timeout. |
100 void RetryTokenInitializationLater(); | 96 void RetryTokenInitializationLater(); |
101 | 97 |
102 // Notifies observers that the TPM token is ready. | 98 // Notifies observers that the TPM token is ready. |
103 void NotifyTPMTokenReady(); | 99 void NotifyTPMTokenReady(); |
104 | 100 |
105 // LoginState::Observer | 101 // LoginState::Observer |
106 virtual void LoggedInStateChanged() OVERRIDE; | 102 virtual void LoggedInStateChanged() OVERRIDE; |
107 | 103 |
108 bool initialize_tpm_for_test_; | 104 bool initialized_for_test_; |
109 | 105 |
110 ObserverList<Observer> observers_; | 106 ObserverList<Observer> observers_; |
111 | 107 |
112 // The states are traversed in this order but some might get omitted or never | 108 // The states are traversed in this order but some might get omitted or never |
113 // be left. | 109 // be left. |
114 enum TPMTokenState { | 110 enum TPMTokenState { |
115 TPM_STATE_UNKNOWN, | 111 TPM_STATE_UNKNOWN, |
116 TPM_INITIALIZATION_STARTED, | 112 TPM_INITIALIZATION_STARTED, |
117 TPM_DB_OPENED, | 113 TPM_DB_OPENED, |
118 TPM_DISABLED, | 114 TPM_DISABLED, |
(...skipping 19 matching lines...) Expand all Loading... |
138 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; | 134 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; |
139 | 135 |
140 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; | 136 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; |
141 | 137 |
142 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); | 138 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); |
143 }; | 139 }; |
144 | 140 |
145 } // namespace chromeos | 141 } // namespace chromeos |
146 | 142 |
147 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ | 143 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ |
OLD | NEW |