OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chromeos/attestation/attestation_flow.h" | 5 #include "chromeos/attestation/attestation_flow.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/timer/timer.h" | |
10 #include "chromeos/cryptohome/async_method_caller.h" | 14 #include "chromeos/cryptohome/async_method_caller.h" |
11 #include "chromeos/cryptohome/cryptohome_parameters.h" | 15 #include "chromeos/cryptohome/cryptohome_parameters.h" |
12 #include "chromeos/dbus/cryptohome_client.h" | 16 #include "chromeos/dbus/cryptohome_client.h" |
13 #include "components/signin/core/account_id/account_id.h" | 17 #include "components/signin/core/account_id/account_id.h" |
14 | 18 |
15 namespace chromeos { | 19 namespace chromeos { |
16 namespace attestation { | 20 namespace attestation { |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
24 // A reasonable reay timeout that gives enough time for attestation | |
Darren Krahn
2016/12/08 18:06:06
reay?
The one and only Dr. Crash
2016/12/08 19:41:10
You know... Ready :)
| |
25 // to be prepared, yet does not make the caller wait too long. | |
26 constexpr uint16_t kReadyTimeoutInSeconds = 180; | |
Darren Krahn
2016/12/08 18:06:05
This does make the caller wait too long -- maybe j
The one and only Dr. Crash
2016/12/13 22:37:17
Changed to 60 s.
| |
27 | |
28 // Delay before checking again whether the TPM has been prepared for | |
29 // attestation. | |
30 constexpr uint16_t kRetryDelayInSeconds = 9; | |
Darren Krahn
2016/12/08 18:06:06
Too long IMO, why not every 300ms?
The one and only Dr. Crash
2016/12/08 19:41:10
Sure. You know how quickly this should be ready be
| |
31 | |
20 // Redirects to one of three callbacks based on a boolean value and dbus call | 32 // Redirects to one of three callbacks based on a boolean value and dbus call |
21 // status. | 33 // status. |
22 // | 34 // |
23 // Parameters | 35 // Parameters |
24 // on_true - Called when status=succes and value=true. | 36 // on_true - Called when status=succes and value=true. |
25 // on_false - Called when status=success and value=false. | 37 // on_false - Called when status=success and value=false. |
26 // on_fail - Called when status=failure. | 38 // on_fail - Called when status=failure. |
27 // status - The D-Bus operation status. | 39 // status - The D-Bus operation status. |
28 // value - The value returned by the D-Bus operation. | 40 // value - The value returned by the D-Bus operation. |
29 void DBusBoolRedirectCallback(const base::Closure& on_true, | 41 void DBusBoolRedirectCallback(const base::Closure& on_true, |
30 const base::Closure& on_false, | 42 const base::Closure& on_false, |
31 const base::Closure& on_fail, | 43 const base::Closure& on_fail, |
44 const std::string& on_fail_message, | |
32 DBusMethodCallStatus status, | 45 DBusMethodCallStatus status, |
33 bool value) { | 46 bool value) { |
34 if (status != DBUS_METHOD_CALL_SUCCESS) { | 47 if (status != DBUS_METHOD_CALL_SUCCESS) { |
35 LOG(ERROR) << "Attestation: Failed to query enrollment state."; | 48 LOG(ERROR) << "Attestation: Failed to " << on_fail_message << "."; |
36 if (!on_fail.is_null()) | 49 if (!on_fail.is_null()) |
37 on_fail.Run(); | 50 on_fail.Run(); |
38 return; | 51 return; |
39 } | 52 } |
40 const base::Closure& task = value ? on_true : on_false; | 53 const base::Closure& task = value ? on_true : on_false; |
41 if (!task.is_null()) | 54 if (!task.is_null()) |
42 task.Run(); | 55 task.Run(); |
43 } | 56 } |
44 | 57 |
45 void DBusDataMethodCallback( | 58 void DBusDataMethodCallback( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 NOTREACHED(); | 101 NOTREACHED(); |
89 return ""; | 102 return ""; |
90 } | 103 } |
91 | 104 |
92 AttestationFlow::AttestationFlow(cryptohome::AsyncMethodCaller* async_caller, | 105 AttestationFlow::AttestationFlow(cryptohome::AsyncMethodCaller* async_caller, |
93 CryptohomeClient* cryptohome_client, | 106 CryptohomeClient* cryptohome_client, |
94 std::unique_ptr<ServerProxy> server_proxy) | 107 std::unique_ptr<ServerProxy> server_proxy) |
95 : async_caller_(async_caller), | 108 : async_caller_(async_caller), |
96 cryptohome_client_(cryptohome_client), | 109 cryptohome_client_(cryptohome_client), |
97 server_proxy_(std::move(server_proxy)), | 110 server_proxy_(std::move(server_proxy)), |
111 ready_timeout_(base::TimeDelta::FromSeconds(kReadyTimeoutInSeconds)), | |
112 retry_delay_(base::TimeDelta::FromSeconds(kRetryDelayInSeconds)), | |
98 weak_factory_(this) {} | 113 weak_factory_(this) {} |
99 | 114 |
100 AttestationFlow::~AttestationFlow() { | 115 AttestationFlow::~AttestationFlow() { |
101 } | 116 } |
102 | 117 |
103 void AttestationFlow::GetCertificate( | 118 void AttestationFlow::GetCertificate( |
104 AttestationCertificateProfile certificate_profile, | 119 AttestationCertificateProfile certificate_profile, |
105 const AccountId& account_id, | 120 const AccountId& account_id, |
106 const std::string& request_origin, | 121 const std::string& request_origin, |
107 bool force_new_key, | 122 bool force_new_key, |
108 const CertificateCallback& callback) { | 123 const CertificateCallback& callback) { |
109 // If this device has not enrolled with the Privacy CA, we need to do that | 124 // If this device has not enrolled with the Privacy CA, we need to do that |
110 // first. Once enrolled we can proceed with the certificate request. | 125 // first. Once enrolled we can proceed with the certificate request. |
111 base::Closure do_cert_request = base::Bind( | 126 const base::Closure do_cert_request = base::Bind( |
112 &AttestationFlow::StartCertificateRequest, weak_factory_.GetWeakPtr(), | 127 &AttestationFlow::StartCertificateRequest, weak_factory_.GetWeakPtr(), |
113 certificate_profile, account_id, request_origin, force_new_key, callback); | 128 certificate_profile, account_id, request_origin, force_new_key, callback); |
114 base::Closure on_enroll_failure = base::Bind(callback, false, ""); | 129 const base::Closure on_failure = base::Bind(callback, false, ""); |
115 base::Closure do_enroll = base::Bind(&AttestationFlow::StartEnroll, | 130 const base::Closure initiate_enroll = |
116 weak_factory_.GetWeakPtr(), | 131 base::Bind(&AttestationFlow::InitiateEnroll, weak_factory_.GetWeakPtr(), |
117 on_enroll_failure, | 132 on_failure, do_cert_request); |
118 do_cert_request); | 133 cryptohome_client_->TpmAttestationIsEnrolled( |
119 cryptohome_client_->TpmAttestationIsEnrolled(base::Bind( | 134 base::Bind(&DBusBoolRedirectCallback, |
120 &DBusBoolRedirectCallback, | 135 do_cert_request, // If enrolled, proceed with cert request. |
121 do_cert_request, // If enrolled, proceed with cert request. | 136 initiate_enroll, // If not enrolled, initiate enrollment. |
122 do_enroll, // If not enrolled, initiate enrollment. | 137 on_failure, "check enrollment state")); |
123 on_enroll_failure)); | 138 } |
139 | |
140 void AttestationFlow::InitiateEnroll(const base::Closure& on_failure, | |
Darren Krahn
2016/12/08 18:06:05
Eliminate this method? GetCertificate() can just a
The one and only Dr. Crash
2016/12/08 19:41:10
Yes. It used to be there because I allocated data
| |
141 const base::Closure& next_task) { | |
142 WaitForAttestationReadyAndStartEnroll( | |
143 base::TimeTicks::Now() + ready_timeout_, on_failure, | |
144 base::Bind(&AttestationFlow::StartEnroll, weak_factory_.GetWeakPtr(), | |
145 on_failure, next_task)); | |
146 } | |
147 | |
148 void AttestationFlow::WaitForAttestationReadyAndStartEnroll( | |
149 base::TimeTicks end_time, | |
150 const base::Closure& on_failure, | |
151 const base::Closure& next_task) { | |
152 const base::Closure retry_initiate_enroll = | |
153 base::Bind(&AttestationFlow::CheckAttestationReadyAndReschedule, | |
154 weak_factory_.GetWeakPtr(), end_time, on_failure, next_task); | |
155 cryptohome_client_->TpmAttestationIsPrepared( | |
156 base::Bind(&DBusBoolRedirectCallback, next_task, retry_initiate_enroll, | |
157 on_failure, "check for attestation readiness")); | |
124 } | 158 } |
125 | 159 |
126 void AttestationFlow::StartEnroll(const base::Closure& on_failure, | 160 void AttestationFlow::StartEnroll(const base::Closure& on_failure, |
127 const base::Closure& next_task) { | 161 const base::Closure& next_task) { |
128 // Get the attestation service to create a Privacy CA enrollment request. | 162 // Get the attestation service to create a Privacy CA enrollment request. |
163 LOG(WARNING) << "Start enroll"; | |
Darren Krahn
2016/12/08 18:06:05
Remove this?
The one and only Dr. Crash
2016/12/08 19:41:10
Good catch. Thanks.
| |
129 async_caller_->AsyncTpmAttestationCreateEnrollRequest( | 164 async_caller_->AsyncTpmAttestationCreateEnrollRequest( |
130 server_proxy_->GetType(), | 165 server_proxy_->GetType(), |
131 base::Bind(&AttestationFlow::SendEnrollRequestToPCA, | 166 base::Bind(&AttestationFlow::SendEnrollRequestToPCA, |
132 weak_factory_.GetWeakPtr(), | 167 weak_factory_.GetWeakPtr(), |
133 on_failure, | 168 on_failure, |
134 next_task)); | 169 next_task)); |
135 } | 170 } |
136 | 171 |
137 void AttestationFlow::SendEnrollRequestToPCA(const base::Closure& on_failure, | 172 void AttestationFlow::SendEnrollRequestToPCA(const base::Closure& on_failure, |
138 const base::Closure& next_task, | 173 const base::Closure& next_task, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 if (generate_new_key) { | 239 if (generate_new_key) { |
205 // Get the attestation service to create a Privacy CA certificate request. | 240 // Get the attestation service to create a Privacy CA certificate request. |
206 async_caller_->AsyncTpmAttestationCreateCertRequest( | 241 async_caller_->AsyncTpmAttestationCreateCertRequest( |
207 server_proxy_->GetType(), certificate_profile, | 242 server_proxy_->GetType(), certificate_profile, |
208 cryptohome::Identification(account_id), request_origin, | 243 cryptohome::Identification(account_id), request_origin, |
209 base::Bind(&AttestationFlow::SendCertificateRequestToPCA, | 244 base::Bind(&AttestationFlow::SendCertificateRequestToPCA, |
210 weak_factory_.GetWeakPtr(), key_type, account_id, key_name, | 245 weak_factory_.GetWeakPtr(), key_type, account_id, key_name, |
211 callback)); | 246 callback)); |
212 } else { | 247 } else { |
213 // If the key already exists, query the existing certificate. | 248 // If the key already exists, query the existing certificate. |
214 base::Closure on_key_exists = base::Bind( | 249 const base::Closure on_key_exists = base::Bind( |
215 &AttestationFlow::GetExistingCertificate, weak_factory_.GetWeakPtr(), | 250 &AttestationFlow::GetExistingCertificate, weak_factory_.GetWeakPtr(), |
216 key_type, account_id, key_name, callback); | 251 key_type, account_id, key_name, callback); |
217 // If the key does not exist, call this method back with |generate_new_key| | 252 // If the key does not exist, call this method back with |generate_new_key| |
218 // set to true. | 253 // set to true. |
219 base::Closure on_key_not_exists = base::Bind( | 254 const base::Closure on_key_not_exists = base::Bind( |
220 &AttestationFlow::StartCertificateRequest, weak_factory_.GetWeakPtr(), | 255 &AttestationFlow::StartCertificateRequest, weak_factory_.GetWeakPtr(), |
221 certificate_profile, account_id, request_origin, true, callback); | 256 certificate_profile, account_id, request_origin, true, callback); |
222 cryptohome_client_->TpmAttestationDoesKeyExist( | 257 cryptohome_client_->TpmAttestationDoesKeyExist( |
223 key_type, cryptohome::Identification(account_id), key_name, | 258 key_type, cryptohome::Identification(account_id), key_name, |
224 base::Bind(&DBusBoolRedirectCallback, on_key_exists, on_key_not_exists, | 259 base::Bind(&DBusBoolRedirectCallback, on_key_exists, on_key_not_exists, |
225 base::Bind(callback, false, ""))); | 260 base::Bind(callback, false, ""), |
261 "check for existence of attestation key")); | |
226 } | 262 } |
227 } | 263 } |
228 | 264 |
229 void AttestationFlow::SendCertificateRequestToPCA( | 265 void AttestationFlow::SendCertificateRequestToPCA( |
230 AttestationKeyType key_type, | 266 AttestationKeyType key_type, |
231 const AccountId& account_id, | 267 const AccountId& account_id, |
232 const std::string& key_name, | 268 const std::string& key_name, |
233 const CertificateCallback& callback, | 269 const CertificateCallback& callback, |
234 bool success, | 270 bool success, |
235 const std::string& data) { | 271 const std::string& data) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 void AttestationFlow::GetExistingCertificate( | 306 void AttestationFlow::GetExistingCertificate( |
271 AttestationKeyType key_type, | 307 AttestationKeyType key_type, |
272 const AccountId& account_id, | 308 const AccountId& account_id, |
273 const std::string& key_name, | 309 const std::string& key_name, |
274 const CertificateCallback& callback) { | 310 const CertificateCallback& callback) { |
275 cryptohome_client_->TpmAttestationGetCertificate( | 311 cryptohome_client_->TpmAttestationGetCertificate( |
276 key_type, cryptohome::Identification(account_id), key_name, | 312 key_type, cryptohome::Identification(account_id), key_name, |
277 base::Bind(&DBusDataMethodCallback, callback)); | 313 base::Bind(&DBusDataMethodCallback, callback)); |
278 } | 314 } |
279 | 315 |
316 void AttestationFlow::CheckAttestationReadyAndReschedule( | |
317 base::TimeTicks end_time, | |
318 const base::Closure& on_failure, | |
319 const base::Closure& next_task) { | |
320 if (base::TimeTicks::Now() < end_time) { | |
Darren Krahn
2016/12/08 18:06:05
Now() can move if the system clock is changed, rig
The one and only Dr. Crash
2016/12/08 19:41:10
Yes, but I wouldn't worry in this case... I did wr
Darren Krahn
2016/12/09 22:27:15
I think the worst case is a forward clock change a
The one and only Dr. Crash
2016/12/13 22:37:17
I think that's ok. The UI should have a retry mech
| |
321 LOG(WARNING) << "Attestation: Not prepared yet." | |
322 << " Retrying in " << retry_delay_ << "."; | |
323 base::MessageLoop::current()->task_runner()->PostDelayedTask( | |
324 FROM_HERE, | |
325 base::Bind(&AttestationFlow::WaitForAttestationReadyAndStartEnroll, | |
326 weak_factory_.GetWeakPtr(), end_time, on_failure, next_task), | |
327 retry_delay_); | |
328 } else { | |
329 LOG(ERROR) << "Attestation: Not prepared. Giving up on retrying."; | |
330 if (!on_failure.is_null()) | |
331 on_failure.Run(); | |
332 } | |
333 } | |
334 | |
280 ServerProxy::~ServerProxy() {} | 335 ServerProxy::~ServerProxy() {} |
281 | 336 |
282 PrivacyCAType ServerProxy::GetType() { | 337 PrivacyCAType ServerProxy::GetType() { |
283 return DEFAULT_PCA; | 338 return DEFAULT_PCA; |
284 } | 339 } |
285 | 340 |
286 } // namespace attestation | 341 } // namespace attestation |
287 } // namespace chromeos | 342 } // namespace chromeos |
OLD | NEW |