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 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" |
| 11 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 12 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
| 13 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" |
| 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chromeos/attestation/attestation_flow.h" |
| 16 #include "chromeos/cryptohome/async_method_caller.h" |
| 17 #include "chromeos/dbus/cryptohome_client.h" |
| 18 #include "chromeos/dbus/dbus_method_call_status.h" |
| 19 #include "chromeos/dbus/dbus_thread_manager.h" |
| 20 #include "content/public/browser/notification_details.h" |
| 21 |
| 22 using std::string; |
| 23 |
| 24 namespace { |
| 25 |
| 26 // A dbus callback which handles a boolean result. |
| 27 // |
| 28 // Parameters |
| 29 // on_true - Called when status=success and value=true. |
| 30 // on_false - Called when status=success and value=false. |
| 31 // status - The dbus operation status. |
| 32 // value - The value returned by the dbus operation. |
| 33 void DBusBoolRedirectCallback(const base::Closure& on_true, |
| 34 const base::Closure& on_false, |
| 35 chromeos::DBusMethodCallStatus status, |
| 36 bool value) { |
| 37 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS) |
| 38 return; |
| 39 const base::Closure& task = value ? on_true : on_false; |
| 40 if (!task.is_null()) |
| 41 task.Run(); |
| 42 } |
| 43 |
| 44 // A dbus callback which handles a string result. |
| 45 // |
| 46 // Parameters |
| 47 // on_success - Called when status=success and result=true. |
| 48 // status - The dbus operation status. |
| 49 // result - The result returned by the dbus operation. |
| 50 // data - The data returned by the dbus operation. |
| 51 void DBusStringCallback(const base::Callback<void(const string&)> on_success, |
| 52 chromeos::DBusMethodCallStatus status, |
| 53 bool result, |
| 54 const string& data) { |
| 55 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) |
| 56 return; |
| 57 on_success.Run(data); |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
| 62 namespace chromeos { |
| 63 namespace attestation { |
| 64 |
| 65 const char AttestationPolicyObserver::kEnterpriseMachineKey[] = |
| 66 "attest-ent-machine"; |
| 67 |
| 68 AttestationPolicyObserver::AttestationPolicyObserver( |
| 69 policy::CloudPolicyClient* policy_client) |
| 70 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 71 cros_settings_(CrosSettings::Get()), |
| 72 policy_client_(policy_client), |
| 73 cryptohome_client_(NULL), |
| 74 attestation_flow_(NULL) { |
| 75 cros_settings_->AddSettingsObserver(kDeviceAttestationEnabled, this); |
| 76 Start(); |
| 77 } |
| 78 |
| 79 AttestationPolicyObserver::AttestationPolicyObserver( |
| 80 policy::CloudPolicyClient* policy_client, |
| 81 CryptohomeClient* cryptohome_client, |
| 82 AttestationFlow* attestation_flow) |
| 83 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 84 cros_settings_(CrosSettings::Get()), |
| 85 policy_client_(policy_client), |
| 86 cryptohome_client_(cryptohome_client), |
| 87 attestation_flow_(attestation_flow) { |
| 88 cros_settings_->AddSettingsObserver(kDeviceAttestationEnabled, this); |
| 89 Start(); |
| 90 } |
| 91 |
| 92 AttestationPolicyObserver::~AttestationPolicyObserver() { |
| 93 cros_settings_->RemoveSettingsObserver(kDeviceAttestationEnabled, this); |
| 94 } |
| 95 |
| 96 void AttestationPolicyObserver::Observe( |
| 97 int type, |
| 98 const content::NotificationSource& source, |
| 99 const content::NotificationDetails& details) { |
| 100 string* path = content::Details<string>(details).ptr(); |
| 101 if (type != chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED || |
| 102 *path != kDeviceAttestationEnabled) { |
| 103 LOG(WARNING) << "AttestationPolicyObserver: Unexpected event received."; |
| 104 return; |
| 105 } |
| 106 Start(); |
| 107 } |
| 108 |
| 109 void AttestationPolicyObserver::Start() { |
| 110 // If attestation is not enabled, there is nothing to do. |
| 111 bool enabled = false; |
| 112 if (!cros_settings_->GetBoolean(kDeviceAttestationEnabled, &enabled) || |
| 113 !enabled) |
| 114 return; |
| 115 |
| 116 // We expect a registered CloudPolicyClient. |
| 117 if (!policy_client_->is_registered()) { |
| 118 LOG(ERROR) << "AttestationPolicyObserver: Invalid CloudPolicyClient."; |
| 119 return; |
| 120 } |
| 121 |
| 122 if (!cryptohome_client_) |
| 123 cryptohome_client_ = DBusThreadManager::Get()->GetCryptohomeClient(); |
| 124 |
| 125 if (!attestation_flow_) { |
| 126 scoped_ptr<ServerProxy> attestation_ca_client(new AttestationCAClient()); |
| 127 default_attestation_flow_.reset(new AttestationFlow( |
| 128 cryptohome::AsyncMethodCaller::GetInstance(), |
| 129 cryptohome_client_, |
| 130 attestation_ca_client.Pass())); |
| 131 attestation_flow_ = default_attestation_flow_.get(); |
| 132 } |
| 133 |
| 134 // Start a dbus call to check if an Enterprise Machine Key already exists. |
| 135 base::Closure on_does_exist = |
| 136 base::Bind(&AttestationPolicyObserver::GetExistingCertificate, |
| 137 weak_factory_.GetWeakPtr()); |
| 138 base::Closure on_does_not_exist = |
| 139 base::Bind(&AttestationPolicyObserver::GetNewCertificate, |
| 140 weak_factory_.GetWeakPtr()); |
| 141 cryptohome_client_->TpmAttestationDoesKeyExist( |
| 142 CryptohomeClient::DEVICE_KEY, |
| 143 kEnterpriseMachineKey, |
| 144 base::Bind(DBusBoolRedirectCallback, on_does_exist, on_does_not_exist)); |
| 145 } |
| 146 |
| 147 void AttestationPolicyObserver::GetNewCertificate() { |
| 148 // We can reuse the dbus callback handler logic. |
| 149 attestation_flow_->GetCertificate( |
| 150 kEnterpriseMachineKey, |
| 151 base::Bind(DBusStringCallback, |
| 152 base::Bind(&AttestationPolicyObserver::UploadCertificate, |
| 153 weak_factory_.GetWeakPtr()), |
| 154 DBUS_METHOD_CALL_SUCCESS)); |
| 155 } |
| 156 |
| 157 void AttestationPolicyObserver::GetExistingCertificate() { |
| 158 cryptohome_client_->TpmAttestationGetCertificate( |
| 159 CryptohomeClient::DEVICE_KEY, |
| 160 kEnterpriseMachineKey, |
| 161 base::Bind(DBusStringCallback, |
| 162 base::Bind(&AttestationPolicyObserver::CheckCertificateExpiry, |
| 163 weak_factory_.GetWeakPtr()))); |
| 164 } |
| 165 |
| 166 void AttestationPolicyObserver::CheckCertificateExpiry( |
| 167 const string& certificate) { |
| 168 // TODO(dkrahn): Check if the certificate will expire soon, for now assume no. |
| 169 CheckIfUploaded(certificate); |
| 170 } |
| 171 |
| 172 void AttestationPolicyObserver::UploadCertificate(const string& certificate) { |
| 173 // TODO(dkrahn): Upload the certificate. |
| 174 } |
| 175 |
| 176 void AttestationPolicyObserver::CheckIfUploaded(const string& certificate) { |
| 177 // TODO(dkrahn): Check if we've already uploaded the certificate. |
| 178 } |
| 179 |
| 180 } // namespace attestation |
| 181 } // namespace chromeos |
OLD | NEW |