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/location.h" |
| 12 #include "base/time.h" |
10 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" | 13 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" |
| 14 #include "chrome/browser/chromeos/attestation/attestation_key_payload.pb.h" |
11 #include "chrome/browser/chromeos/settings/cros_settings.h" | 15 #include "chrome/browser/chromeos/settings/cros_settings.h" |
12 #include "chrome/browser/policy/cloud/cloud_policy_client.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
13 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" | 17 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" |
14 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
15 #include "chromeos/attestation/attestation_flow.h" | 19 #include "chromeos/attestation/attestation_flow.h" |
16 #include "chromeos/cryptohome/async_method_caller.h" | 20 #include "chromeos/cryptohome/async_method_caller.h" |
17 #include "chromeos/dbus/cryptohome_client.h" | 21 #include "chromeos/dbus/cryptohome_client.h" |
18 #include "chromeos/dbus/dbus_method_call_status.h" | 22 #include "chromeos/dbus/dbus_method_call_status.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 23 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
| 26 #include "net/cert/x509_certificate.h" |
22 | 27 |
23 namespace { | 28 namespace { |
24 | 29 |
| 30 const char kEnterpriseMachineKey[] = "attest-ent-machine"; |
| 31 |
| 32 // The number of days before a certificate expires during which it is |
| 33 // considered 'expiring soon' and replacement is initiated. The Chrome OS CA |
| 34 // issues certificates with an expiry of at least two years. This value has |
| 35 // been set large enough so that the majority of users will have gone through |
| 36 // a full sign-in during the period. |
| 37 const int kExpiryThresholdInDays = 30; |
| 38 |
25 // A dbus callback which handles a boolean result. | 39 // A dbus callback which handles a boolean result. |
26 // | 40 // |
27 // Parameters | 41 // Parameters |
28 // on_true - Called when status=success and value=true. | 42 // on_true - Called when status=success and value=true. |
29 // on_false - Called when status=success and value=false. | 43 // on_false - Called when status=success and value=false. |
30 // status - The dbus operation status. | 44 // status - The dbus operation status. |
31 // value - The value returned by the dbus operation. | 45 // value - The value returned by the dbus operation. |
32 void DBusBoolRedirectCallback(const base::Closure& on_true, | 46 void DBusBoolRedirectCallback(const base::Closure& on_true, |
33 const base::Closure& on_false, | 47 const base::Closure& on_false, |
| 48 const tracked_objects::Location& from_here, |
34 chromeos::DBusMethodCallStatus status, | 49 chromeos::DBusMethodCallStatus status, |
35 bool value) { | 50 bool value) { |
36 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS) | 51 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS) { |
| 52 LOG(ERROR) << "Cryptohome DBus method failed: " << from_here.ToString() |
| 53 << " - " << status; |
37 return; | 54 return; |
| 55 } |
38 const base::Closure& task = value ? on_true : on_false; | 56 const base::Closure& task = value ? on_true : on_false; |
39 if (!task.is_null()) | 57 if (!task.is_null()) |
40 task.Run(); | 58 task.Run(); |
41 } | 59 } |
42 | 60 |
43 // A dbus callback which handles a string result. | 61 // A dbus callback which handles a string result. |
44 // | 62 // |
45 // Parameters | 63 // Parameters |
46 // on_success - Called when status=success and result=true. | 64 // on_success - Called when status=success and result=true. |
47 // status - The dbus operation status. | 65 // status - The dbus operation status. |
48 // result - The result returned by the dbus operation. | 66 // result - The result returned by the dbus operation. |
49 // data - The data returned by the dbus operation. | 67 // data - The data returned by the dbus operation. |
50 void DBusStringCallback( | 68 void DBusStringCallback( |
51 const base::Callback<void(const std::string&)> on_success, | 69 const base::Callback<void(const std::string&)> on_success, |
| 70 const tracked_objects::Location& from_here, |
52 chromeos::DBusMethodCallStatus status, | 71 chromeos::DBusMethodCallStatus status, |
53 bool result, | 72 bool result, |
54 const std::string& data) { | 73 const std::string& data) { |
55 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) | 74 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) { |
| 75 LOG(ERROR) << "Cryptohome DBus method failed: " << from_here.ToString() |
| 76 << " - " << status << " - " << result; |
56 return; | 77 return; |
| 78 } |
57 on_success.Run(data); | 79 on_success.Run(data); |
58 } | 80 } |
59 | 81 |
60 } // namespace | 82 } // namespace |
61 | 83 |
62 namespace chromeos { | 84 namespace chromeos { |
63 namespace attestation { | 85 namespace attestation { |
64 | 86 |
65 const char AttestationPolicyObserver::kEnterpriseMachineKey[] = | |
66 "attest-ent-machine"; | |
67 | |
68 AttestationPolicyObserver::AttestationPolicyObserver( | 87 AttestationPolicyObserver::AttestationPolicyObserver( |
69 policy::CloudPolicyClient* policy_client) | 88 policy::CloudPolicyClient* policy_client) |
70 : cros_settings_(CrosSettings::Get()), | 89 : cros_settings_(CrosSettings::Get()), |
71 policy_client_(policy_client), | 90 policy_client_(policy_client), |
72 cryptohome_client_(NULL), | 91 cryptohome_client_(NULL), |
73 attestation_flow_(NULL), | 92 attestation_flow_(NULL), |
74 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 93 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
76 cros_settings_->AddSettingsObserver(kDeviceAttestationEnabled, this); | 95 cros_settings_->AddSettingsObserver(kDeviceAttestationEnabled, this); |
77 Start(); | 96 Start(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // Start a dbus call to check if an Enterprise Machine Key already exists. | 157 // Start a dbus call to check if an Enterprise Machine Key already exists. |
139 base::Closure on_does_exist = | 158 base::Closure on_does_exist = |
140 base::Bind(&AttestationPolicyObserver::GetExistingCertificate, | 159 base::Bind(&AttestationPolicyObserver::GetExistingCertificate, |
141 weak_factory_.GetWeakPtr()); | 160 weak_factory_.GetWeakPtr()); |
142 base::Closure on_does_not_exist = | 161 base::Closure on_does_not_exist = |
143 base::Bind(&AttestationPolicyObserver::GetNewCertificate, | 162 base::Bind(&AttestationPolicyObserver::GetNewCertificate, |
144 weak_factory_.GetWeakPtr()); | 163 weak_factory_.GetWeakPtr()); |
145 cryptohome_client_->TpmAttestationDoesKeyExist( | 164 cryptohome_client_->TpmAttestationDoesKeyExist( |
146 KEY_DEVICE, | 165 KEY_DEVICE, |
147 kEnterpriseMachineKey, | 166 kEnterpriseMachineKey, |
148 base::Bind(DBusBoolRedirectCallback, on_does_exist, on_does_not_exist)); | 167 base::Bind(DBusBoolRedirectCallback, |
| 168 on_does_exist, |
| 169 on_does_not_exist, |
| 170 FROM_HERE)); |
149 } | 171 } |
150 | 172 |
151 void AttestationPolicyObserver::GetNewCertificate() { | 173 void AttestationPolicyObserver::GetNewCertificate() { |
152 // We can reuse the dbus callback handler logic. | 174 // We can reuse the dbus callback handler logic. |
153 attestation_flow_->GetCertificate( | 175 attestation_flow_->GetCertificate( |
154 PROFILE_ENTERPRISE_MACHINE_CERTIFICATE, | 176 PROFILE_ENTERPRISE_MACHINE_CERTIFICATE, |
155 true, // Force a new key to be generated. | 177 true, // Force a new key to be generated. |
156 base::Bind(DBusStringCallback, | 178 base::Bind(DBusStringCallback, |
157 base::Bind(&AttestationPolicyObserver::UploadCertificate, | 179 base::Bind(&AttestationPolicyObserver::UploadCertificate, |
158 weak_factory_.GetWeakPtr()), | 180 weak_factory_.GetWeakPtr()), |
| 181 FROM_HERE, |
159 DBUS_METHOD_CALL_SUCCESS)); | 182 DBUS_METHOD_CALL_SUCCESS)); |
160 } | 183 } |
161 | 184 |
162 void AttestationPolicyObserver::GetExistingCertificate() { | 185 void AttestationPolicyObserver::GetExistingCertificate() { |
163 cryptohome_client_->TpmAttestationGetCertificate( | 186 cryptohome_client_->TpmAttestationGetCertificate( |
164 KEY_DEVICE, | 187 KEY_DEVICE, |
165 kEnterpriseMachineKey, | 188 kEnterpriseMachineKey, |
166 base::Bind(DBusStringCallback, | 189 base::Bind(DBusStringCallback, |
167 base::Bind(&AttestationPolicyObserver::CheckCertificateExpiry, | 190 base::Bind(&AttestationPolicyObserver::CheckCertificateExpiry, |
168 weak_factory_.GetWeakPtr()))); | 191 weak_factory_.GetWeakPtr()), |
| 192 FROM_HERE)); |
169 } | 193 } |
170 | 194 |
171 void AttestationPolicyObserver::CheckCertificateExpiry( | 195 void AttestationPolicyObserver::CheckCertificateExpiry( |
172 const std::string& certificate) { | 196 const std::string& certificate) { |
173 // TODO(dkrahn): Check if the certificate will expire soon, for now assume no. | 197 scoped_refptr<net::X509Certificate> x509( |
174 CheckIfUploaded(certificate); | 198 net::X509Certificate::CreateFromBytes(certificate.data(), |
| 199 certificate.length())); |
| 200 if (!x509.get() || x509->valid_expiry().is_null()) { |
| 201 LOG(WARNING) << "Failed to parse certificate, cannot check expiry."; |
| 202 } else { |
| 203 const base::TimeDelta threshold = |
| 204 base::TimeDelta::FromDays(kExpiryThresholdInDays); |
| 205 if ((base::Time::Now() + threshold) > x509->valid_expiry()) { |
| 206 // The certificate has expired or will soon, replace it. |
| 207 GetNewCertificate(); |
| 208 return; |
| 209 } |
| 210 } |
| 211 |
| 212 // Get the payload and check if the certificate has already been uploaded. |
| 213 GetKeyPayload(base::Bind(&AttestationPolicyObserver::CheckIfUploaded, |
| 214 weak_factory_.GetWeakPtr(), |
| 215 certificate)); |
175 } | 216 } |
176 | 217 |
177 void AttestationPolicyObserver::UploadCertificate( | 218 void AttestationPolicyObserver::UploadCertificate( |
178 const std::string& certificate) { | 219 const std::string& certificate) { |
179 // TODO(dkrahn): Upload the certificate. | 220 policy_client_->UploadCertificate( |
| 221 certificate, |
| 222 base::Bind(&AttestationPolicyObserver::OnUploadComplete, |
| 223 weak_factory_.GetWeakPtr())); |
180 } | 224 } |
181 | 225 |
182 void AttestationPolicyObserver::CheckIfUploaded( | 226 void AttestationPolicyObserver::CheckIfUploaded( |
183 const std::string& certificate) { | 227 const std::string& certificate, |
184 // TODO(dkrahn): Check if we've already uploaded the certificate. | 228 const std::string& key_payload) { |
| 229 AttestationKeyPayload payload_pb; |
| 230 if (!key_payload.empty() && |
| 231 payload_pb.ParseFromString(key_payload) && |
| 232 payload_pb.is_certificate_uploaded()) { |
| 233 // Already uploaded... nothing more to do. |
| 234 return; |
| 235 } |
| 236 UploadCertificate(certificate); |
| 237 } |
| 238 |
| 239 void AttestationPolicyObserver::GetKeyPayload( |
| 240 base::Callback<void(const std::string&)> callback) { |
| 241 cryptohome_client_->TpmAttestationGetKeyPayload( |
| 242 KEY_DEVICE, |
| 243 kEnterpriseMachineKey, |
| 244 base::Bind(DBusStringCallback, callback, FROM_HERE)); |
| 245 } |
| 246 |
| 247 void AttestationPolicyObserver::OnUploadComplete(bool status) { |
| 248 if (!status) |
| 249 return; |
| 250 GetKeyPayload(base::Bind(&AttestationPolicyObserver::MarkAsUploaded, |
| 251 weak_factory_.GetWeakPtr())); |
| 252 } |
| 253 |
| 254 void AttestationPolicyObserver::MarkAsUploaded(const std::string& key_payload) { |
| 255 AttestationKeyPayload payload_pb; |
| 256 if (!key_payload.empty()) |
| 257 payload_pb.ParseFromString(key_payload); |
| 258 payload_pb.set_is_certificate_uploaded(true); |
| 259 std::string new_payload; |
| 260 if (!payload_pb.SerializeToString(&new_payload)) { |
| 261 LOG(WARNING) << "Failed to serialize key payload."; |
| 262 return; |
| 263 } |
| 264 cryptohome_client_->TpmAttestationSetKeyPayload( |
| 265 KEY_DEVICE, |
| 266 kEnterpriseMachineKey, |
| 267 new_payload, |
| 268 base::Bind(DBusBoolRedirectCallback, |
| 269 base::Closure(), |
| 270 base::Closure(), |
| 271 FROM_HERE)); |
185 } | 272 } |
186 | 273 |
187 } // namespace attestation | 274 } // namespace attestation |
188 } // namespace chromeos | 275 } // namespace chromeos |
OLD | NEW |