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 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" | 5 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | |
11 #include "base/time.h" | |
10 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" | 12 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" |
13 #include "chrome/browser/chromeos/attestation/attestation_key_payload.pb.h" | |
11 #include "chrome/browser/chromeos/settings/cros_settings.h" | 14 #include "chrome/browser/chromeos/settings/cros_settings.h" |
12 #include "chrome/browser/policy/cloud/cloud_policy_client.h" | 15 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
13 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" |
14 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
15 #include "chromeos/attestation/attestation_flow.h" | 18 #include "chromeos/attestation/attestation_flow.h" |
16 #include "chromeos/cryptohome/async_method_caller.h" | 19 #include "chromeos/cryptohome/async_method_caller.h" |
17 #include "chromeos/dbus/cryptohome_client.h" | 20 #include "chromeos/dbus/cryptohome_client.h" |
18 #include "chromeos/dbus/dbus_method_call_status.h" | 21 #include "chromeos/dbus/dbus_method_call_status.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
25 #include "net/cert/x509_certificate.h" | |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 // A dbus callback which handles a boolean result. | 29 // A dbus callback which handles a boolean result. |
26 // | 30 // |
27 // Parameters | 31 // Parameters |
28 // on_true - Called when status=success and value=true. | 32 // on_true - Called when status=success and value=true. |
29 // on_false - Called when status=success and value=false. | 33 // on_false - Called when status=success and value=false. |
30 // status - The dbus operation status. | 34 // status - The dbus operation status. |
31 // value - The value returned by the dbus operation. | 35 // value - The value returned by the dbus operation. |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 cryptohome_client_->TpmAttestationGetCertificate( | 166 cryptohome_client_->TpmAttestationGetCertificate( |
163 CryptohomeClient::DEVICE_KEY, | 167 CryptohomeClient::DEVICE_KEY, |
164 kEnterpriseMachineKey, | 168 kEnterpriseMachineKey, |
165 base::Bind(DBusStringCallback, | 169 base::Bind(DBusStringCallback, |
166 base::Bind(&AttestationPolicyObserver::CheckCertificateExpiry, | 170 base::Bind(&AttestationPolicyObserver::CheckCertificateExpiry, |
167 weak_factory_.GetWeakPtr()))); | 171 weak_factory_.GetWeakPtr()))); |
168 } | 172 } |
169 | 173 |
170 void AttestationPolicyObserver::CheckCertificateExpiry( | 174 void AttestationPolicyObserver::CheckCertificateExpiry( |
171 const std::string& certificate) { | 175 const std::string& certificate) { |
172 // TODO(dkrahn): Check if the certificate will expire soon, for now assume no. | 176 scoped_refptr<net::X509Certificate> x509( |
173 CheckIfUploaded(certificate); | 177 net::X509Certificate::CreateFromBytes(certificate.data(), |
178 certificate.length())); | |
179 if (!x509.get() || x509->valid_expiry().is_null()) { | |
180 LOG(WARNING) << "Failed to parse certificate, cannot check expiry."; | |
181 } else { | |
182 base::TimeDelta threshold = base::TimeDelta::FromDays(30); | |
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
const
We usually put constant decls in an anonymo
dkrahn
2013/04/22 22:33:31
Added a constant member to the class.
The PCA i
Mattias Nissler (ping if slow)
2013/04/23 10:53:01
Fair. Would be good to include this additional con
| |
183 if ((base::Time::Now() + threshold) > x509->valid_expiry()) { | |
184 // The certificate has expired or will soon, replace it. | |
185 GetNewCertificate(); | |
186 return; | |
187 } | |
188 } | |
189 | |
190 // Get the payload and check if the certificate has already been uploaded. | |
191 GetKeyPayload(base::Bind(&AttestationPolicyObserver::CheckIfUploaded, | |
192 weak_factory_.GetWeakPtr(), | |
193 certificate)); | |
174 } | 194 } |
175 | 195 |
176 void AttestationPolicyObserver::UploadCertificate( | 196 void AttestationPolicyObserver::UploadCertificate( |
177 const std::string& certificate) { | 197 const std::string& certificate) { |
178 // TODO(dkrahn): Upload the certificate. | 198 policy_client_->UploadCertificate( |
199 certificate, | |
200 base::Bind(&AttestationPolicyObserver::OnUploadComplete, | |
201 weak_factory_.GetWeakPtr())); | |
179 } | 202 } |
180 | 203 |
181 void AttestationPolicyObserver::CheckIfUploaded( | 204 void AttestationPolicyObserver::CheckIfUploaded( |
182 const std::string& certificate) { | 205 const std::string& certificate, |
183 // TODO(dkrahn): Check if we've already uploaded the certificate. | 206 const std::string& key_payload) { |
207 AttestationKeyPayload payload_pb; | |
208 if (!key_payload.empty() && | |
209 payload_pb.ParseFromString(key_payload) && | |
210 payload_pb.is_certificate_uploaded()) { | |
211 // Already uploaded... nothing more to do. | |
212 return; | |
213 } | |
214 UploadCertificate(certificate); | |
215 } | |
216 | |
217 void AttestationPolicyObserver::GetKeyPayload( | |
218 base::Callback<void(const std::string&)> callback) { | |
219 cryptohome_client_->TpmAttestationGetKeyPayload( | |
220 CryptohomeClient::DEVICE_KEY, | |
221 kEnterpriseMachineKey, | |
222 base::Bind(DBusStringCallback, callback)); | |
223 } | |
224 | |
225 void AttestationPolicyObserver::OnUploadComplete(bool status) { | |
226 if (!status) | |
227 return; | |
228 GetKeyPayload(base::Bind(&AttestationPolicyObserver::MarkAsUploaded, | |
229 weak_factory_.GetWeakPtr())); | |
230 } | |
231 | |
232 void AttestationPolicyObserver::MarkAsUploaded(const std::string& key_payload) { | |
233 AttestationKeyPayload payload_pb; | |
234 if (!key_payload.empty()) | |
235 payload_pb.ParseFromString(key_payload); | |
236 payload_pb.set_is_certificate_uploaded(true); | |
237 std::string new_payload; | |
238 if (!payload_pb.SerializeToString(&new_payload)) { | |
239 LOG(WARNING) << "Failed to serialize key payload."; | |
240 return; | |
241 } | |
242 cryptohome_client_->TpmAttestationSetKeyPayload( | |
243 CryptohomeClient::DEVICE_KEY, | |
244 kEnterpriseMachineKey, | |
245 new_payload, | |
246 base::Bind(DBusBoolRedirectCallback, base::Closure(), base::Closure())); | |
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
Here and elsewhere: Should we have some logging in
dkrahn
2013/04/22 22:33:31
Added logging to dbus callbacks which reports fail
| |
184 } | 247 } |
185 | 248 |
186 } // namespace attestation | 249 } // namespace attestation |
187 } // namespace chromeos | 250 } // namespace chromeos |
OLD | NEW |